Skip to content

Commit

Permalink
Show error message for NSZ files
Browse files Browse the repository at this point in the history
  • Loading branch information
garoxas committed Oct 29, 2019
1 parent 82a4149 commit e5f77c3
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 34 deletions.
8 changes: 8 additions & 0 deletions NX_Game_Info/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ public char CsvSeparator
set { this["CsvSeparator"] = value; }
}

[UserScopedSetting()]
[DefaultSettingValue("False")]
public bool NszExtension
{
get { return (bool)this["NszExtension"]; }
set { this["NszExtension"] = value; }
}

#if WINDOWS
[UserScopedSetting()]
[DefaultSettingValue("0, 0")]
Expand Down
146 changes: 118 additions & 28 deletions NX_Game_Info/Process.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,12 +372,22 @@ public static Title processXci(string filename)
{
if (entry.Name.EndsWith(".cnmt.nca"))
{
using (var cnmtNca = xci.SecurePartition.OpenFile(entry))
try
{
var nca = processCnmtNca(cnmtNca, ref title);
if (nca.Item1 != null && (nca.Item2 != null || title.type == TitleType.AddOnContent))
using (var cnmtNca = xci.SecurePartition.OpenFile(entry))
{
(biggestNca, controlNca) = nca;
var nca = processCnmtNca(cnmtNca, ref title);
if (nca.Item1 != null && (nca.Item2 != null || title.type == TitleType.AddOnContent))
{
(biggestNca, controlNca) = nca;
}
}
}
catch (FileNotFoundException)
{
if (xci.SecurePartition.FileExists(entry.Name.Replace(".nca", ".ncz")))
{
title.error = "Unsupported Format: Compressed NCA";
}
}

Expand All @@ -389,13 +399,23 @@ public static Title processXci(string filename)
}
else if (entry.Name.EndsWith(".tik"))
{
using (var tik = xci.SecurePartition.OpenFile(entry))
try
{
if (entry.Name.Split('.')[0].TryToBytes(out byte[] rightsId))
using (var tik = xci.SecurePartition.OpenFile(entry))
{
processTik(tik, rightsId, ref keyset, out byte[] titleKey);
if (entry.Name.Split('.')[0].TryToBytes(out byte[] rightsId))
{
processTik(tik, rightsId, ref keyset, out byte[] titleKey);

title.titleKey = BitConverter.ToString(titleKey).Replace("-", "").ToUpper();
title.titleKey = BitConverter.ToString(titleKey).Replace("-", "").ToUpper();
}
}
}
catch (FileNotFoundException)
{
if (xci.SecurePartition.FileExists(entry.Name.Replace(".nca", ".ncz")))
{
title.error = "Unsupported Format: Compressed NCA";
}
}

Expand All @@ -405,17 +425,37 @@ public static Title processXci(string filename)

if (!String.IsNullOrEmpty(biggestNca))
{
using (var biggest = xci.SecurePartition.OpenFile(biggestNca))
try
{
processBiggestNca(biggest, ref title);
using (var biggest = xci.SecurePartition.OpenFile(biggestNca))
{
processBiggestNca(biggest, ref title);
}
}
catch (FileNotFoundException)
{
if (xci.SecurePartition.FileExists(biggestNca.Replace(".nca", ".ncz")))
{
title.error = "Unsupported Format: Compressed NCA";
}
}
}

if (!String.IsNullOrEmpty(controlNca))
{
using (var control = xci.SecurePartition.OpenFile(controlNca))
try
{
processControlNca(control, ref title);
using (var control = xci.SecurePartition.OpenFile(controlNca))
{
processControlNca(control, ref title);
}
}
catch (FileNotFoundException)
{
if (xci.SecurePartition.FileExists(controlNca.Replace(".nca", ".ncz")))
{
title.error = "Unsupported Format: Compressed NCA";
}
}
}

Expand Down Expand Up @@ -468,21 +508,35 @@ public static Title processNsp(string filename)
{
if (entry.Name.EndsWith(".cnmt.xml"))
{
using (var cnmtXml = pfs.OpenFile(entry))
try
{
(biggestNca, controlNca) = processCnmtXml(cnmtXml, ref title);
using (var cnmtXml = pfs.OpenFile(entry))
{
(biggestNca, controlNca) = processCnmtXml(cnmtXml, ref title);
}
}
catch (FileNotFoundException) { }

title.structure.Add(Title.Structure.CnmtXml);
}
else if (entry.Name.EndsWith(".cnmt.nca"))
{
using (var cnmtNca = pfs.OpenFile(entry))
try
{
using (var cnmtNca = pfs.OpenFile(entry))
{
var nca = processCnmtNca(cnmtNca, ref title);
if (nca.Item1 != null && (nca.Item2 != null || title.type == TitleType.AddOnContent))
{
(biggestNca, controlNca) = nca;
}
}
}
catch (FileNotFoundException)
{
var nca = processCnmtNca(cnmtNca, ref title);
if (nca.Item1 != null && (nca.Item2 != null || title.type == TitleType.AddOnContent))
if (pfs.FileExists(entry.Name.Replace(".nca", ".ncz")))
{
(biggestNca, controlNca) = nca;
title.error = "Unsupported Format: Compressed NCA";
}
}

Expand All @@ -494,13 +548,23 @@ public static Title processNsp(string filename)
}
else if (entry.Name.EndsWith(".tik"))
{
using (var tik = pfs.OpenFile(entry))
try
{
if (entry.Name.Split('.')[0].TryToBytes(out byte[] rightsId))
using (var tik = pfs.OpenFile(entry))
{
processTik(tik, rightsId, ref keyset, out byte[] titleKey);
if (entry.Name.Split('.')[0].TryToBytes(out byte[] rightsId))
{
processTik(tik, rightsId, ref keyset, out byte[] titleKey);

title.titleKey = BitConverter.ToString(titleKey).Replace("-", "").ToUpper();
title.titleKey = BitConverter.ToString(titleKey).Replace("-", "").ToUpper();
}
}
}
catch (FileNotFoundException)
{
if (pfs.FileExists(entry.Name.Replace(".nca", ".ncz")))
{
title.error = "Unsupported Format: Compressed NCA";
}
}

Expand All @@ -512,10 +576,14 @@ public static Title processNsp(string filename)
}
else if (entry.Name.EndsWith(".nacp.xml"))
{
using (var nacpXml = pfs.OpenFile(entry))
try
{
processNacpXml(nacpXml, ref title);
using (var nacpXml = pfs.OpenFile(entry))
{
processNacpXml(nacpXml, ref title);
}
}
catch (FileNotFoundException) { }

title.structure.Add(Title.Structure.NacpXml);
}
Expand All @@ -535,17 +603,37 @@ public static Title processNsp(string filename)

if (!String.IsNullOrEmpty(biggestNca))
{
using (var biggest = pfs.OpenFile(biggestNca))
try
{
using (var biggest = pfs.OpenFile(biggestNca))
{
processBiggestNca(biggest, ref title);
}
}
catch (FileNotFoundException)
{
processBiggestNca(biggest, ref title);
if (pfs.FileExists(biggestNca.Replace(".nca", ".ncz")))
{
title.error = "Unsupported Format: Compressed NCA";
}
}
}

if (!String.IsNullOrEmpty(controlNca))
{
using (var control = pfs.OpenFile(controlNca))
try
{
using (var control = pfs.OpenFile(controlNca))
{
processControlNca(control, ref title);
}
}
catch (FileNotFoundException)
{
processControlNca(control, ref title);
if (pfs.FileExists(controlNca.Replace(".nca", ".ncz")))
{
title.error = "Unsupported Format: Compressed NCA";
}
}
}
}
Expand Down Expand Up @@ -853,6 +941,7 @@ private static (string, string) processCnmtNca(Nca nca, ref Title title, bool cn

log?.WriteLine(title.error);
}
catch (FileNotFoundException) { }

return (biggestNca, controlNca);
}
Expand Down Expand Up @@ -993,6 +1082,7 @@ private static void processControlNca(Nca nca, ref Title title)

log?.WriteLine(title.error);
}
catch (FileNotFoundException) { }
}
}

Expand Down
6 changes: 4 additions & 2 deletions Windows/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ private void openFileToolStripMenuItem_Click(object sender, EventArgs e)

OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "Open NX Game Files";
openFileDialog.Filter = "NX Game Files (*.xci;*.nsp;*.nro)|*.xci;*.nsp;*.nro|Gamecard Files (*.xci)|*.xci|Package Files (*.nsp)|*.nsp|Homebrew Files (*.nro)|*.nro|All Files (*.*)|*.*";
openFileDialog.Filter = String.Format("NX Game Files (*.xci;*.nsp;{0}*.nro)|*.xci;*.nsp;{0}*.nro|Gamecard Files (*.xci{1})|*.xci{1}|Package Files (*.nsp{2})|*.nsp{2}|Homebrew Files (*.nro)|*.nro|All Files (*.*)|*.*",
Common.Settings.Default.NszExtension ? "*.xcz;*.nsz;" : "", Common.Settings.Default.NszExtension ? ";*.xcz" : "", Common.Settings.Default.NszExtension ? ";*.nsz" : "");
openFileDialog.Multiselect = true;
openFileDialog.InitialDirectory = !String.IsNullOrEmpty(Common.Settings.Default.InitialDirectory) && Directory.Exists(Common.Settings.Default.InitialDirectory) ? Common.Settings.Default.InitialDirectory : Directory.GetDirectoryRoot(Directory.GetCurrentDirectory());

Expand Down Expand Up @@ -783,7 +784,8 @@ private void backgroundWorkerProcess_DoWork(object sender, System.ComponentModel
if (argumentPath.Item1 == Worker.Directory && argumentPath.Item2 is string path)
{
List<string> filenames = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories)
.Where(filename => filename.ToLower().EndsWith(".xci") || filename.ToLower().EndsWith(".nsp") || filename.ToLower().EndsWith(".nro")).ToList();
.Where(filename => filename.ToLower().EndsWith(".xci") || filename.ToLower().EndsWith(".nsp") || filename.ToLower().EndsWith(".nro") ||
(Common.Settings.Default.NszExtension && (filename.ToLower().EndsWith(".xcz") || filename.ToLower().EndsWith(".nsz")))).ToList();
filenames.Sort();

Process.log?.WriteLine("{0} files selected", filenames.Count);
Expand Down
6 changes: 4 additions & 2 deletions cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ static void Main(string[] args)
{ "c|sdcard", "open path as sdcard", v => sdcard = v != null },
{ "s|sort=", "sort by titleid, titlename or filename [default: filename]", (string s) => sort = s },
{ "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 @@ -101,7 +102,7 @@ static void processPaths(List<string> paths, string sort, bool sdcard)
titles.AddRange(openDirectory(path));
}
}
else if (File.Exists(path) && new string[] { ".xci", ".nsp", ".nro" }.Any(ext => ext.Equals(Path.GetExtension(path).ToLower())) && !sdcard)
else if (File.Exists(path) && (Common.Settings.Default.NszExtension ? new string[] { ".xci", ".nsp", ".xcz", ".nsz", ".nro" } : new string[] { ".xci", ".nsp", ".nro" }).Any(ext => ext.Equals(Path.GetExtension(path).ToLower())) && !sdcard)
{
Title title = openFile(path);
if (title != null)
Expand Down Expand Up @@ -205,7 +206,8 @@ static List<Title> openDirectory(string path)
Process.log?.WriteLine("\nOpen Directory");

List<string> filenames = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories)
.Where(filename => filename.ToLower().EndsWith(".xci") || filename.ToLower().EndsWith(".nsp") || filename.ToLower().EndsWith(".nro")).ToList();
.Where(filename => filename.ToLower().EndsWith(".xci") || filename.ToLower().EndsWith(".nsp") || filename.ToLower().EndsWith(".nro") ||
(Common.Settings.Default.NszExtension && (filename.ToLower().EndsWith(".xcz") || filename.ToLower().EndsWith(".nsz")))).ToList();
filenames.Sort();

Process.log?.WriteLine("{0} files selected", filenames.Count);
Expand Down
5 changes: 3 additions & 2 deletions macOS/MainWindowController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public void OpenFile(NSMenuItem menuItem)
openPanel.CanChooseFiles = true;
openPanel.CanChooseDirectories = false;
openPanel.AllowsMultipleSelection = true;
openPanel.AllowedFileTypes = new string[] { "xci", "nsp", "nro" };
openPanel.AllowedFileTypes = Common.Settings.Default.NszExtension ? new string[] { "xci", "nsp", "xcz", "nsz", "nro" } : new string[] { "xci", "nsp", "nro" };
openPanel.DirectoryUrl = NSUrl.FromFilename(!String.IsNullOrEmpty(Common.Settings.Default.InitialDirectory) && Directory.Exists(Common.Settings.Default.InitialDirectory) ? Common.Settings.Default.InitialDirectory : Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
openPanel.Title = "Open NX Game Files";

Expand Down Expand Up @@ -688,7 +688,8 @@ void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
if (argumentPath.Item1 == Worker.Directory && argumentPath.Item2 is string path)
{
List<string> filenames = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories)
.Where(filename => filename.ToLower().EndsWith(".xci") || filename.ToLower().EndsWith(".nsp") || filename.ToLower().EndsWith(".nro")).ToList();
.Where(filename => filename.ToLower().EndsWith(".xci") || filename.ToLower().EndsWith(".nsp") || filename.ToLower().EndsWith(".nro") ||
(Common.Settings.Default.NszExtension && (filename.ToLower().EndsWith(".xcz") || filename.ToLower().EndsWith(".nsz")))).ToList();
filenames.Sort();

Process.log?.WriteLine("{0} files selected", filenames.Count);
Expand Down

0 comments on commit e5f77c3

Please sign in to comment.