Skip to content

Commit

Permalink
Export to CSV and XLSX for CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
garoxas committed Apr 22, 2020
1 parent 70d92f6 commit e603a1c
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 12 deletions.
8 changes: 4 additions & 4 deletions Windows/NX_Game_Info.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
<ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>
<ItemGroup>
<Reference Include="EPPlus, Version=4.5.3.2, Culture=neutral, PublicKeyToken=ea159fdaa78159a1, processorArchitecture=MSIL">
<HintPath>..\packages\EPPlus.4.5.3.2\lib\net40\EPPlus.dll</HintPath>
<Reference Include="EPPlus, Version=5.1.0.0, Culture=neutral, PublicKeyToken=ea159fdaa78159a1, processorArchitecture=MSIL">
<HintPath>..\packages\EPPlus.5.1.0\lib\net45\EPPlus.dll</HintPath>
</Reference>
<Reference Include="ListViewPrinter, Version=2.7.1.31255, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\ObjectListView.2.7.1.5\lib\ListViewPrinter.dll</HintPath>
Expand All @@ -63,8 +63,8 @@
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Numerics" />
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.5.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.6.0\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.6.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.7.1\lib\net461\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.Security" />
<Reference Include="System.Xml.Linq" />
Expand Down
4 changes: 2 additions & 2 deletions Windows/packages.config
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EPPlus" version="4.5.3.2" targetFramework="net471" />
<package id="EPPlus" version="5.1.0" targetFramework="net471" />
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net471" />
<package id="ObjectListView" version="2.7.1.5" targetFramework="net471" />
<package id="PortableSettingsProvider" version="0.2.3" targetFramework="net471" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.6.0" targetFramework="net471" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.7.1" targetFramework="net471" />
</packages>
11 changes: 9 additions & 2 deletions cli/NX_Game_Info.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,22 @@
<ApplicationIcon>NX_Game_Info.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<Reference Include="EPPlus, Version=5.1.0.0, Culture=neutral, PublicKeyToken=ea159fdaa78159a1, processorArchitecture=MSIL">
<HintPath>..\packages\EPPlus.5.1.0\lib\net45\EPPlus.dll</HintPath>
</Reference>
<Reference Include="PortableSettingsProvider, Version=0.2.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\PortableSettingsProvider.0.2.3\lib\net45\PortableSettingsProvider.dll</HintPath>
</Reference>
<Reference Include="PresentationCore" />
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.5.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.6.0\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
<Reference Include="System.Drawing" />
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.6.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.7.1\lib\net461\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.Security" />
<Reference Include="System.Threading.Tasks" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand Down
216 changes: 213 additions & 3 deletions cli/Program.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using LibHac;
using Mono.Options;
using OfficeOpenXml;
using FsTitle = LibHac.Title;
using Title = NX_Game_Info.Common.Title;

Expand All @@ -34,12 +36,15 @@ static void Main(string[] args)

bool sdcard = false;
string sort = "";
string export = "";

OptionSet options = null;
options = new OptionSet()
{
{ "c|sdcard", "open path as sdcard", v => sdcard = v != null },
{ "s|sort=", "sort by titleid, titlename or filename [default: filename]", (string s) => sort = s },
{ "x|export=", "export filename, only *.csv or *.xlsx supported", (string s) => export = s },
{ "l|delimiter=", "csv delimiter character [default: ,]", (char c) => Common.Settings.Default.CsvSeparator = c },
{ "h|help", "show this help message and exit", v => printHelp(options) },
{ "z|nsz", "enable nsz extension", v => Common.Settings.Default.NszExtension = v != null, true },
{ "d|debug", "enable debug log", v => Common.Settings.Default.DebugLog = v != null },
Expand Down Expand Up @@ -76,18 +81,18 @@ static void Main(string[] args)
Environment.Exit(-1);
}

processPaths(paths, sort, sdcard);
processPaths(paths, sort, export, sdcard);
}

static void printHelp(OptionSet options)
{
Console.Error.WriteLine("usage: {0} [-h|--help] [-d|--debug] [-c|--sdcard] [-s(titleid|titlename|filename)|--sort=(titleid|titlename|filename)] paths...\n", Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().Location).Quote());
Console.Error.WriteLine("usage: {0} [-h|--help] [-d|--debug] [-c|--sdcard] [-s(titleid|titlename|filename)|--sort=(titleid|titlename|filename)] [-x(<filename.csv>|<filename.xlsx>)|--export=(<filename.csv>|<filename.xlsx>)] [-l(<delimiter>)|--delimiter=(<delimiter>)] paths...\n", Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().Location).Quote());
options.WriteOptionDescriptions(Console.Error);

Environment.Exit(-1);
}

static void processPaths(List<string> paths, string sort, bool sdcard)
static void processPaths(List<string> paths, string sort, string export, bool sdcard)
{
List<Title> titles = new List<Title>();

Expand Down Expand Up @@ -187,6 +192,8 @@ static void processPaths(List<string> paths, string sort, bool sdcard)

Process.log?.WriteLine("\n{0} titles processed", titles.Count);
Console.Error.WriteLine("\n{0} titles processed", titles.Count);

exportTitles(titles, export);
}

static Title openFile(string filename)
Expand Down Expand Up @@ -264,5 +271,208 @@ static List<Title> openSDCard(string pathSd)

return titles;
}

static void exportTitles(List<Title> titles, string filename)
{
FileVersionInfo info = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location);

if (filename.EndsWith(".csv", StringComparison.OrdinalIgnoreCase))
{
try
{
Directory.CreateDirectory(Path.GetDirectoryName(filename));
}
catch
{
Console.Error.WriteLine("\n{0} is not supported or not a valid path", filename);
}

using (var writer = new StreamWriter(filename))
{
char separator = Common.Settings.Default.CsvSeparator;
if (separator != '\0')
{
writer.WriteLine("sep={0}", separator);
}
else
{
separator = ',';
}

if (info != null)
{
writer.WriteLine("# publisher {0} {1}", info.ProductName, info.ProductVersion);
}
else
{
writer.WriteLine("# publisher {0} {1}", Assembly.GetExecutingAssembly().GetName().Name, Assembly.GetExecutingAssembly().GetName().Version);
}

writer.WriteLine("# updated {0}", String.Format("{0:F}", DateTime.Now));

writer.WriteLine(String.Join(separator.ToString(), Common.Title.Properties));

uint index = 0, count = (uint)titles.Count;

foreach (var title in titles)
{
index++;

writer.WriteLine(String.Join(separator.ToString(), new string[] {
title.titleID.Quote(separator),
title.baseTitleID.Quote(separator),
title.titleName.Quote(separator),
title.displayVersion.Quote(separator),
title.versionString.Quote(separator),
title.latestVersionString.Quote(separator),
title.systemUpdateString.Quote(separator),
title.systemVersionString.Quote(separator),
title.applicationVersionString.Quote(separator),
title.masterkeyString.Quote(separator),
title.titleKey.Quote(separator),
title.publisher.Quote(separator),
title.languagesString.Quote(separator),
title.filename.Quote(separator),
title.filesizeString.Quote(separator),
title.typeString.Quote(separator),
title.distribution.ToString().Quote(separator),
title.structureString.Quote(separator),
title.signatureString.Quote(separator),
title.permissionString.Quote(separator),
title.error.Quote(separator),
}));
}

Process.log?.WriteLine("\n{0} of {1} titles exported to {2}", index, titles.Count, filename);
Console.Error.WriteLine("\n{0} of {1} titles exported to {2}", index, titles.Count, filename);
}
}
else if (filename.EndsWith(".xlsx", StringComparison.OrdinalIgnoreCase))
{
try
{
Directory.CreateDirectory(Path.GetDirectoryName(filename));
}
catch
{
Console.Error.WriteLine("\n{0} is not supported or not a valid path", filename);
}

using (ExcelPackage excel = new ExcelPackage())
{
ExcelWorksheet worksheet = excel.Workbook.Worksheets.Add(DateTime.Now.ToString("dd MMMM yyyy HH:mm:ss"));

worksheet.Cells[1, 1, 1, Title.Properties.Count()].LoadFromArrays(new List<string[]> { Title.Properties });
worksheet.Cells["1:1"].Style.Font.Bold = true;
worksheet.Cells["1:1"].Style.Font.Color.SetColor(Color.White);
worksheet.Cells["1:1"].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
worksheet.Cells["1:1"].Style.Fill.BackgroundColor.SetColor(Color.MidnightBlue);

uint index = 0, count = (uint)titles.Count;

foreach (var title in titles)
{
index++;

var data = new List<string[]>
{
new string[] {
title.titleID,
title.baseTitleID,
title.titleName,
title.displayVersion,
title.versionString,
title.latestVersionString,
title.systemUpdateString,
title.systemVersionString,
title.applicationVersionString,
title.masterkeyString,
title.titleKey,
title.publisher,
title.languagesString,
title.filename,
title.filesizeString,
title.typeString,
title.distribution.ToString(),
title.structureString,
title.signatureString,
title.permissionString,
title.error,
}
};

worksheet.Cells[(int)index + 1, 1].LoadFromArrays(data);

string titleID = title.type == TitleType.AddOnContent ? title.titleID : title.baseTitleID ?? "";

Process.latestVersions.TryGetValue(titleID, out uint latestVersion);
Process.versionList.TryGetValue(titleID, out uint version);
Process.titleVersions.TryGetValue(titleID, out uint titleVersion);

if (latestVersion < version || latestVersion < titleVersion)
{
worksheet.Cells[(int)index + 1, 1, (int)index + 1, Title.Properties.Count()].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
worksheet.Cells[(int)index + 1, 1, (int)index + 1, Title.Properties.Count()].Style.Fill.BackgroundColor.SetColor(title.signature != true ? Color.OldLace : Color.LightYellow);
}
else if (title.signature != true)
{
worksheet.Cells[(int)index + 1, 1, (int)index + 1, Title.Properties.Count()].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
worksheet.Cells[(int)index + 1, 1, (int)index + 1, Title.Properties.Count()].Style.Fill.BackgroundColor.SetColor(Color.WhiteSmoke);
}

if (title.permission == Title.Permission.Dangerous)
{
worksheet.Cells[(int)index + 1, 1, (int)index + 1, Title.Properties.Count()].Style.Font.Color.SetColor(Color.DarkRed);
}
else if (title.permission == Title.Permission.Unsafe)
{
worksheet.Cells[(int)index + 1, 1, (int)index + 1, Title.Properties.Count()].Style.Font.Color.SetColor(Color.Indigo);
}
}

ExcelRange range = worksheet.Cells[1, 1, (int)count + 1, Title.Properties.Count()];
range.Style.Border.Top.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
range.Style.Border.Left.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
range.Style.Border.Right.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
range.Style.Border.Bottom.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;

worksheet.Column(1).Width = 18;
worksheet.Column(2).Width = 18;
worksheet.Column(3).AutoFit();
worksheet.Column(3).Width = Math.Max(worksheet.Column(3).Width, 30);
worksheet.Column(4).Width = 16;
worksheet.Column(5).Width = 16;
worksheet.Column(6).Width = 16;
worksheet.Column(7).Width = 16;
worksheet.Column(8).Width = 16;
worksheet.Column(9).Width = 16;
worksheet.Column(10).Width = 16;
worksheet.Column(11).AutoFit();
worksheet.Column(11).Width = Math.Max(worksheet.Column(11).Width, 36);
worksheet.Column(12).AutoFit();
worksheet.Column(12).Width = Math.Max(worksheet.Column(12).Width, 30);
worksheet.Column(13).Width = 18;
worksheet.Column(14).AutoFit();
worksheet.Column(14).Width = Math.Max(worksheet.Column(14).Width, 54);
worksheet.Column(15).Width = 10;
worksheet.Column(16).Width = 10;
worksheet.Column(17).Width = 12;
worksheet.Column(18).Width = 12;
worksheet.Column(19).Width = 10;
worksheet.Column(20).Width = 10;
worksheet.Column(21).Width = 40;

excel.SaveAs(new FileInfo(filename));

Process.log?.WriteLine("\n{0} of {1} titles exported to {2}", index, titles.Count, filename);
Console.Error.WriteLine("\n{0} of {1} titles exported to {2}", index, titles.Count, filename);
}
}
else
{
Process.log?.WriteLine("\nExport to {0} file type is not supported", Path.GetExtension(filename));
Console.Error.WriteLine("\nExport to {0} file type is not supported", Path.GetExtension(filename));
}
}
}
}
3 changes: 2 additions & 1 deletion cli/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EPPlus" version="5.1.0" targetFramework="net471" />
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net471" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.6.0" targetFramework="net471" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.7.1" targetFramework="net471" />
</packages>

0 comments on commit e603a1c

Please sign in to comment.