mirror of
https://git.ryujinx.app/projects/Kenji-NX.git
synced 2026-09-20 17:51:13 +02:00
misc: chore: Use explicit types & fix object creation
This commit is contained in:
@@ -3,6 +3,7 @@ using LibHac.Fs;
|
||||
using LibHac.Fs.Fsa;
|
||||
using LibHac.FsSystem;
|
||||
using LibHac.Loader;
|
||||
using LibHac.Tools.Fs;
|
||||
using LibHac.Tools.FsSystem;
|
||||
using LibHac.Tools.FsSystem.RomFs;
|
||||
using LibHac.Util;
|
||||
@@ -150,7 +151,7 @@ namespace Ryujinx.HLE.HOS
|
||||
|
||||
private static string EnsureBaseDirStructure(string modsBasePath)
|
||||
{
|
||||
var modsDir = new DirectoryInfo(modsBasePath);
|
||||
DirectoryInfo modsDir = new(modsBasePath);
|
||||
|
||||
modsDir.CreateSubdirectory(AmsContentsDir);
|
||||
modsDir.CreateSubdirectory(AmsNsoPatchDir);
|
||||
@@ -168,23 +169,23 @@ namespace Ryujinx.HLE.HOS
|
||||
{
|
||||
System.Text.StringBuilder types = new();
|
||||
|
||||
foreach (var modDir in dir.EnumerateDirectories())
|
||||
foreach (DirectoryInfo modDir in dir.EnumerateDirectories())
|
||||
{
|
||||
types.Clear();
|
||||
Mod<DirectoryInfo> mod = new("", null, true);
|
||||
|
||||
if (StrEquals(RomfsDir, modDir.Name) && modDir.Parent != null)
|
||||
{
|
||||
var modData = modMetadata.Mods.FirstOrDefault(x => modDir.Parent.FullName.Equals(x.Path));
|
||||
var enabled = modData?.Enabled ?? true;
|
||||
Mod modData = modMetadata.Mods.FirstOrDefault(x => modDir.Parent.FullName.Equals(x.Path));
|
||||
bool enabled = modData?.Enabled ?? true;
|
||||
|
||||
mods.RomfsDirs.Add(mod = new Mod<DirectoryInfo>(dir.Name, modDir, enabled));
|
||||
types.Append('R');
|
||||
}
|
||||
else if (StrEquals(ExefsDir, modDir.Name) && modDir.Parent != null)
|
||||
{
|
||||
var modData = modMetadata.Mods.FirstOrDefault(x => modDir.Parent.FullName.Equals(x.Path));
|
||||
var enabled = modData?.Enabled ?? true;
|
||||
Mod modData = modMetadata.Mods.FirstOrDefault(x => modDir.Parent.FullName.Equals(x.Path));
|
||||
bool enabled = modData?.Enabled ?? true;
|
||||
|
||||
mods.ExefsDirs.Add(mod = new Mod<DirectoryInfo>(dir.Name, modDir, enabled));
|
||||
types.Append('E');
|
||||
@@ -207,8 +208,8 @@ namespace Ryujinx.HLE.HOS
|
||||
|
||||
public static string GetApplicationDir(string modsBasePath, string applicationId)
|
||||
{
|
||||
var contentsDir = new DirectoryInfo(Path.Combine(modsBasePath, AmsContentsDir));
|
||||
var applicationModsPath = FindApplicationDir(contentsDir, applicationId);
|
||||
DirectoryInfo contentsDir = new(Path.Combine(modsBasePath, AmsContentsDir));
|
||||
DirectoryInfo applicationModsPath = FindApplicationDir(contentsDir, applicationId);
|
||||
|
||||
if (applicationModsPath == null)
|
||||
{
|
||||
@@ -250,7 +251,7 @@ namespace Ryujinx.HLE.HOS
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var modDir in patchDir.EnumerateDirectories())
|
||||
foreach (DirectoryInfo modDir in patchDir.EnumerateDirectories())
|
||||
{
|
||||
patches.Add(new Mod<DirectoryInfo>(modDir.Name, modDir, true));
|
||||
Logger.Info?.Print(LogClass.ModLoader, $"Found {type} patch '{modDir.Name}'");
|
||||
@@ -279,11 +280,11 @@ namespace Ryujinx.HLE.HOS
|
||||
}
|
||||
}
|
||||
|
||||
var fsFile = new FileInfo(Path.Combine(applicationDir.FullName, RomfsContainer));
|
||||
FileInfo fsFile = new(Path.Combine(applicationDir.FullName, RomfsContainer));
|
||||
if (fsFile.Exists)
|
||||
{
|
||||
var modData = modMetadata.Mods.FirstOrDefault(x => fsFile.FullName.Contains(x.Path));
|
||||
var enabled = modData == null || modData.Enabled;
|
||||
Mod modData = modMetadata.Mods.FirstOrDefault(x => fsFile.FullName.Contains(x.Path));
|
||||
bool enabled = modData == null || modData.Enabled;
|
||||
|
||||
mods.RomfsContainers.Add(new Mod<FileInfo>($"<{applicationDir.Name} RomFs>", fsFile, enabled));
|
||||
}
|
||||
@@ -291,8 +292,8 @@ namespace Ryujinx.HLE.HOS
|
||||
fsFile = new FileInfo(Path.Combine(applicationDir.FullName, ExefsContainer));
|
||||
if (fsFile.Exists)
|
||||
{
|
||||
var modData = modMetadata.Mods.FirstOrDefault(x => fsFile.FullName.Contains(x.Path));
|
||||
var enabled = modData == null || modData.Enabled;
|
||||
Mod modData = modMetadata.Mods.FirstOrDefault(x => fsFile.FullName.Contains(x.Path));
|
||||
bool enabled = modData == null || modData.Enabled;
|
||||
|
||||
mods.ExefsContainers.Add(new Mod<FileInfo>($"<{applicationDir.Name} ExeFs>", fsFile, enabled));
|
||||
}
|
||||
@@ -309,7 +310,7 @@ namespace Ryujinx.HLE.HOS
|
||||
|
||||
Logger.Info?.Print(LogClass.ModLoader, $"Searching mods for {((applicationId & 0x1000) != 0 ? "DLC" : "Application")} {applicationId:X16} in \"{contentsDir.FullName}\"");
|
||||
|
||||
var applicationDir = FindApplicationDir(contentsDir, $"{applicationId:x16}");
|
||||
DirectoryInfo applicationDir = FindApplicationDir(contentsDir, $"{applicationId:x16}");
|
||||
|
||||
if (applicationDir != null)
|
||||
{
|
||||
@@ -449,9 +450,9 @@ namespace Ryujinx.HLE.HOS
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (var path in searchDirPaths)
|
||||
foreach (string path in searchDirPaths)
|
||||
{
|
||||
var searchDir = new DirectoryInfo(path);
|
||||
DirectoryInfo searchDir = new(path);
|
||||
if (!searchDir.Exists)
|
||||
{
|
||||
Logger.Warning?.Print(LogClass.ModLoader, $"Mod Search Dir '{searchDir.FullName}' doesn't exist");
|
||||
@@ -460,7 +461,7 @@ namespace Ryujinx.HLE.HOS
|
||||
|
||||
if (!TryQuery(searchDir, patches, modCaches))
|
||||
{
|
||||
foreach (var subdir in searchDir.EnumerateDirectories())
|
||||
foreach (DirectoryInfo subdir in searchDir.EnumerateDirectories())
|
||||
{
|
||||
TryQuery(subdir, patches, modCaches);
|
||||
}
|
||||
@@ -489,14 +490,14 @@ namespace Ryujinx.HLE.HOS
|
||||
return baseStorage;
|
||||
}
|
||||
|
||||
var fileSet = new HashSet<string>();
|
||||
var builder = new RomFsBuilder();
|
||||
HashSet<string> fileSet = new();
|
||||
RomFsBuilder builder = new();
|
||||
int count = 0;
|
||||
|
||||
Logger.Info?.Print(LogClass.ModLoader, $"Applying RomFS mods for Application {applicationId:X16}");
|
||||
|
||||
// Prioritize loose files first
|
||||
foreach (var mod in mods.RomfsDirs)
|
||||
foreach (Mod<DirectoryInfo> mod in mods.RomfsDirs)
|
||||
{
|
||||
if (!mod.Enabled)
|
||||
{
|
||||
@@ -511,7 +512,7 @@ namespace Ryujinx.HLE.HOS
|
||||
}
|
||||
|
||||
// Then files inside images
|
||||
foreach (var mod in mods.RomfsContainers)
|
||||
foreach (Mod<FileInfo> mod in mods.RomfsContainers)
|
||||
{
|
||||
if (!mod.Enabled)
|
||||
{
|
||||
@@ -536,12 +537,12 @@ namespace Ryujinx.HLE.HOS
|
||||
Logger.Info?.Print(LogClass.ModLoader, $"Replaced {fileSet.Count} file(s) over {count} mod(s). Processing base storage...");
|
||||
|
||||
// And finally, the base romfs
|
||||
var baseRom = new RomFsFileSystem(baseStorage);
|
||||
foreach (var entry in baseRom.EnumerateEntries()
|
||||
RomFsFileSystem baseRom = new(baseStorage);
|
||||
foreach (DirectoryEntryEx entry in baseRom.EnumerateEntries()
|
||||
.Where(f => f.Type == DirectoryEntryType.File && !fileSet.Contains(f.FullPath))
|
||||
.OrderBy(f => f.FullPath, StringComparer.Ordinal))
|
||||
{
|
||||
using var file = new UniqueRef<IFile>();
|
||||
using UniqueRef<IFile> file = new();
|
||||
|
||||
baseRom.OpenFile(ref file.Ref, entry.FullPath.ToU8Span(), OpenMode.Read).ThrowIfFailure();
|
||||
builder.AddFile(entry.FullPath, file.Release());
|
||||
@@ -556,12 +557,12 @@ namespace Ryujinx.HLE.HOS
|
||||
|
||||
private static void AddFiles(IFileSystem fs, string modName, string rootPath, ISet<string> fileSet, RomFsBuilder builder)
|
||||
{
|
||||
foreach (var entry in fs.EnumerateEntries()
|
||||
foreach (DirectoryEntryEx entry in fs.EnumerateEntries()
|
||||
.AsParallel()
|
||||
.Where(f => f.Type == DirectoryEntryType.File)
|
||||
.OrderBy(f => f.FullPath, StringComparer.Ordinal))
|
||||
{
|
||||
var file = new LazyFile(entry.FullPath, rootPath, fs);
|
||||
LazyFile file = new(entry.FullPath, rootPath, fs);
|
||||
|
||||
if (fileSet.Add(entry.FullPath))
|
||||
{
|
||||
@@ -588,7 +589,7 @@ namespace Ryujinx.HLE.HOS
|
||||
|
||||
Logger.Info?.Print(LogClass.ModLoader, "Using replacement ExeFS partition");
|
||||
|
||||
var pfs = new PartitionFileSystem();
|
||||
PartitionFileSystem pfs = new();
|
||||
pfs.Initialize(mods.ExefsContainers[0].Path.OpenRead().AsStorage()).ThrowIfFailure();
|
||||
exefs = pfs;
|
||||
|
||||
@@ -626,9 +627,9 @@ namespace Ryujinx.HLE.HOS
|
||||
throw new ArgumentOutOfRangeException(nameof(nsos), nsos.Length, "NSO Count is incorrect");
|
||||
}
|
||||
|
||||
var exeMods = mods.ExefsDirs;
|
||||
List<Mod<DirectoryInfo>> exeMods = mods.ExefsDirs;
|
||||
|
||||
foreach (var mod in exeMods)
|
||||
foreach (Mod<DirectoryInfo> mod in exeMods)
|
||||
{
|
||||
if (!mod.Enabled)
|
||||
{
|
||||
@@ -637,7 +638,7 @@ namespace Ryujinx.HLE.HOS
|
||||
|
||||
for (int i = 0; i < ProcessConst.ExeFsPrefixes.Length; ++i)
|
||||
{
|
||||
var nsoName = ProcessConst.ExeFsPrefixes[i];
|
||||
string nsoName = ProcessConst.ExeFsPrefixes[i];
|
||||
|
||||
FileInfo nsoFile = new(Path.Combine(mod.Path.FullName, nsoName));
|
||||
if (nsoFile.Exists)
|
||||
@@ -705,7 +706,7 @@ namespace Ryujinx.HLE.HOS
|
||||
|
||||
internal void ApplyNroPatches(NroExecutable nro)
|
||||
{
|
||||
var nroPatches = _patches.NroPatches;
|
||||
List<Mod<DirectoryInfo>> nroPatches = _patches.NroPatches;
|
||||
|
||||
if (nroPatches.Count == 0)
|
||||
{
|
||||
@@ -747,11 +748,11 @@ namespace Ryujinx.HLE.HOS
|
||||
return;
|
||||
}
|
||||
|
||||
var cheats = mods.Cheats;
|
||||
var processExes = tamperInfo.BuildIds.Zip(tamperInfo.CodeAddresses, (k, v) => new { k, v })
|
||||
List<Cheat> cheats = mods.Cheats;
|
||||
Dictionary<string, ulong> processExes = tamperInfo.BuildIds.Zip(tamperInfo.CodeAddresses, (k, v) => new { k, v })
|
||||
.ToDictionary(x => x.k[..Math.Min(Cheat.CheatIdSize, x.k.Length)], x => x.v);
|
||||
|
||||
foreach (var cheat in cheats)
|
||||
foreach (Cheat cheat in cheats)
|
||||
{
|
||||
string cheatId = Path.GetFileNameWithoutExtension(cheat.Path.Name).ToUpper();
|
||||
|
||||
@@ -772,7 +773,7 @@ namespace Ryujinx.HLE.HOS
|
||||
|
||||
internal static void EnableCheats(ulong applicationId, TamperMachine tamperMachine)
|
||||
{
|
||||
var contentDirectory = FindApplicationDir(new DirectoryInfo(Path.Combine(GetModsBasePath(), AmsContentsDir)), $"{applicationId:x16}");
|
||||
DirectoryInfo contentDirectory = FindApplicationDir(new DirectoryInfo(Path.Combine(GetModsBasePath(), AmsContentsDir)), $"{applicationId:x16}");
|
||||
string enabledCheatsPath = Path.Combine(contentDirectory.FullName, CheatDir, "enabled.txt");
|
||||
|
||||
if (File.Exists(enabledCheatsPath))
|
||||
@@ -792,11 +793,11 @@ namespace Ryujinx.HLE.HOS
|
||||
patches[i] = new MemPatch();
|
||||
}
|
||||
|
||||
var buildIds = new List<string>(programs.Length);
|
||||
List<string> buildIds = new(programs.Length);
|
||||
|
||||
foreach (IExecutable p in programs)
|
||||
{
|
||||
var buildId = p switch
|
||||
string buildId = p switch
|
||||
{
|
||||
NsoExecutable nso => Convert.ToHexString(nso.BuildId).TrimEnd('0'),
|
||||
NroExecutable nro => Convert.ToHexString(nro.Header.BuildId).TrimEnd('0'),
|
||||
@@ -808,15 +809,15 @@ namespace Ryujinx.HLE.HOS
|
||||
int GetIndex(string buildId) => buildIds.FindIndex(id => id == buildId); // O(n) but list is small
|
||||
|
||||
// Collect patches
|
||||
foreach (var mod in mods)
|
||||
foreach (Mod<DirectoryInfo> mod in mods)
|
||||
{
|
||||
if (!mod.Enabled)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var patchDir = mod.Path;
|
||||
foreach (var patchFile in patchDir.EnumerateFiles())
|
||||
DirectoryInfo patchDir = mod.Path;
|
||||
foreach (FileInfo patchFile in patchDir.EnumerateFiles())
|
||||
{
|
||||
if (StrEquals(".ips", patchFile.Extension)) // IPS|IPS32
|
||||
{
|
||||
@@ -831,18 +832,18 @@ namespace Ryujinx.HLE.HOS
|
||||
|
||||
Logger.Info?.Print(LogClass.ModLoader, $"Matching IPS patch '{patchFile.Name}' in '{mod.Name}' bid={buildId}");
|
||||
|
||||
using var fs = patchFile.OpenRead();
|
||||
using var reader = new BinaryReader(fs);
|
||||
using FileStream fs = patchFile.OpenRead();
|
||||
using BinaryReader reader = new(fs);
|
||||
|
||||
var patcher = new IpsPatcher(reader);
|
||||
IpsPatcher patcher = new(reader);
|
||||
patcher.AddPatches(patches[index]);
|
||||
}
|
||||
else if (StrEquals(".pchtxt", patchFile.Extension)) // IPSwitch
|
||||
{
|
||||
using var fs = patchFile.OpenRead();
|
||||
using var reader = new StreamReader(fs);
|
||||
using FileStream fs = patchFile.OpenRead();
|
||||
using StreamReader reader = new(fs);
|
||||
|
||||
var patcher = new IPSwitchPatcher(reader);
|
||||
IPSwitchPatcher patcher = new(reader);
|
||||
|
||||
int index = GetIndex(patcher.BuildId);
|
||||
if (index == -1)
|
||||
|
||||
Reference in New Issue
Block a user