From f62b34905e7f0e42dee2d5eeb840262fb6239dd4 Mon Sep 17 00:00:00 2001 From: shadowninja108 Date: Mon, 17 Dec 2018 18:58:05 -0700 Subject: [PATCH] Fixes and an ugly hack in DiscUtils --- .../DiscUtils.Core/Internal/Utilities.cs | 6 +++++- src/LibHac.Nand/NandPartition.cs | 14 +++++++------- src/LibHac/FileSystem/IFileSystem.cs | 15 ++++++++++++++- src/LibHac/FileSystem/LocalFileSystem.cs | 13 +++++++++---- src/LibHac/Keyset.cs | 4 ++-- 5 files changed, 37 insertions(+), 15 deletions(-) diff --git a/src/LibHac.Nand/DiscUtils.Core/Internal/Utilities.cs b/src/LibHac.Nand/DiscUtils.Core/Internal/Utilities.cs index fe97c3f0..3ab869c9 100644 --- a/src/LibHac.Nand/DiscUtils.Core/Internal/Utilities.cs +++ b/src/LibHac.Nand/DiscUtils.Core/Internal/Utilities.cs @@ -435,7 +435,11 @@ namespace DiscUtils.Internal pattern += "."; } - string query = "^" + Regex.Escape(pattern).Replace(@"\*", ".*").Replace(@"\?", "[^.]") + "$"; + string query; + if (pattern == "*.") + query = ".*"; + else + query = "^" + Regex.Escape(pattern).Replace(@"\*", ".*").Replace(@"\?", "[^.]") + "$"; return new Regex(query, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); } diff --git a/src/LibHac.Nand/NandPartition.cs b/src/LibHac.Nand/NandPartition.cs index 5c5734b6..2ec992ab 100644 --- a/src/LibHac.Nand/NandPartition.cs +++ b/src/LibHac.Nand/NandPartition.cs @@ -28,6 +28,11 @@ namespace LibHac.Nand return Fs.DirectoryExists(directory.Path); } + public override long GetSize(IFile file) + { + return Fs.GetFileInfo(file.Path).Length; + } + public new virtual IStorage OpenFile(IFile file, FileMode mode) { return Fs.OpenFile(file.Path, mode).AsStorage(); @@ -49,17 +54,12 @@ namespace LibHac.Nand string[] dirs = Fs.GetDirectories(directory.Path, searchPattern, searchOption); List entries = new List(); for (int i = 0; i < dirs.Length; i++) - entries.Add(new IDirectory(this, dirs[i])); + entries.Add(GetDirectory(dirs[i])); for (int i = 0; i < files.Length; i++) - entries.Add(new IFile(this, files[i])); + entries.Add(GetFile(files[i])); return entries.ToArray(); } - public string GetFullPath(string path) - { - return path; - } - protected override IDirectory GetPath(string path) { return new IDirectory(this, path); diff --git a/src/LibHac/FileSystem/IFileSystem.cs b/src/LibHac/FileSystem/IFileSystem.cs index 1e42c187..ea4417ae 100644 --- a/src/LibHac/FileSystem/IFileSystem.cs +++ b/src/LibHac/FileSystem/IFileSystem.cs @@ -11,6 +11,7 @@ namespace LibHac public abstract bool FileExists(IFile path); public abstract bool DirectoryExists(IDirectory path); + public abstract long GetSize(IFile file); public IStorage OpenFile(IFile file, FileMode mode) { @@ -51,6 +52,7 @@ namespace LibHac { public abstract IFileSystem FileSystem { get; } public abstract string Path { get; } + public virtual string Name => System.IO.Path.GetFileName(Path); public abstract bool Exists { get; } public IDirectory Parent @@ -108,9 +110,9 @@ namespace LibHac public override string Path { get; } public override bool Exists => FileSystem.FileExists(this); - public string Name => System.IO.Path.GetFileNameWithoutExtension(Path); public string Extension => System.IO.Path.GetExtension(Path); public string FileName => System.IO.Path.GetFileName(Path); + public long Length => FileSystem.GetSize(this); public IFile(IFileSystem filesystem, string path) { @@ -127,5 +129,16 @@ namespace LibHac { return FileSystem.OpenFile(this, mode, access); } + + public override bool Equals(object obj) + { + IFile other = (IFile) obj; + return other.FileSystem == FileSystem && other.Path == Path; + } + + public override int GetHashCode() + { + return base.GetHashCode(); + } } } diff --git a/src/LibHac/FileSystem/LocalFileSystem.cs b/src/LibHac/FileSystem/LocalFileSystem.cs index 863df473..51eba7b2 100644 --- a/src/LibHac/FileSystem/LocalFileSystem.cs +++ b/src/LibHac/FileSystem/LocalFileSystem.cs @@ -27,23 +27,28 @@ namespace LibHac return Directory.Exists(Path.Combine(Root, directory.Path)); } + public override long GetSize(IFile file) + { + return new FileInfo(GetFullPath(file)).Length; + } + public override IStorage OpenFile(IFile file, FileMode mode, FileAccess access) { - return new FileStream(GetFullPath(file), mode, access).AsStorage(); + return new StreamStorage(new FileStream(GetFullPath(file), mode, access), false); } public override IFileSytemEntry[] GetFileSystemEntries(IDirectory path, string searchPattern, SearchOption searchOption) { var result = new List(); - + Console.WriteLine(searchPattern); DirectoryInfo root = new DirectoryInfo(GetFullPath(path)); foreach(FileSystemInfo info in root.EnumerateFileSystemInfos(searchPattern, searchOption)) { string relativePath = Util.GetRelativePath(info.FullName, Root); if (info.Attributes.HasFlag(FileAttributes.Directory)) - result.Add(new IDirectory(this, relativePath)); + result.Add(GetDirectory(relativePath)); else - result.Add(new LocalFile(this, relativePath)); + result.Add(GetFile(relativePath)); } return result.ToArray(); diff --git a/src/LibHac/Keyset.cs b/src/LibHac/Keyset.cs index 9bfb3cf8..4683a34d 100644 --- a/src/LibHac/Keyset.cs +++ b/src/LibHac/Keyset.cs @@ -545,7 +545,6 @@ namespace LibHac new KeyValue("bis_kek_source", 0x10, set => set.BisKekSource), new KeyValue("save_mac_kek_source", 0x10, set => set.SaveMacKekSource), new KeyValue("save_mac_key_source", 0x10, set => set.SaveMacKeySource), - new KeyValue("save_mac_key", 0x10, set => set.SaveMacKey) }; for (int slot = 0; slot < 0x20; slot++) @@ -585,7 +584,8 @@ namespace LibHac new KeyValue("secure_boot_key", 0x10, set => set.SecureBootKey), new KeyValue("tsec_key", 0x10, set => set.TsecKey), new KeyValue("device_key", 0x10, set => set.DeviceKey), - new KeyValue("sd_seed", 0x10, set => set.SdSeed) + new KeyValue("sd_seed", 0x10, set => set.SdSeed), + new KeyValue("save_mac_key", 0x10, set => set.SaveMacKey) }; for (int slot = 0; slot < 0x20; slot++)