2020-07-09 06:31:15 +02:00
using LibHac.Common ;
using LibHac.Fs ;
2020-09-01 22:08:59 +02:00
using LibHac.Fs.Fsa ;
2020-07-09 06:31:15 +02:00
using LibHac.FsSystem ;
2021-08-12 23:56:24 +02:00
using LibHac.Loader ;
2022-01-12 12:22:19 +01:00
using LibHac.Tools.FsSystem ;
using LibHac.Tools.FsSystem.RomFs ;
2020-08-30 18:51:53 +02:00
using Ryujinx.Common.Configuration ;
2020-07-09 06:31:15 +02:00
using Ryujinx.Common.Logging ;
2023-03-04 14:43:08 +01:00
using Ryujinx.HLE.HOS.Kernel.Process ;
2020-07-09 06:31:15 +02:00
using Ryujinx.HLE.Loaders.Executables ;
2023-03-04 14:43:08 +01:00
using Ryujinx.HLE.Loaders.Mods ;
2023-03-31 21:16:46 +02:00
using Ryujinx.HLE.Loaders.Processes ;
2020-07-09 06:31:15 +02:00
using System ;
using System.Collections.Generic ;
using System.Collections.Specialized ;
2021-03-27 15:12:05 +01:00
using System.Globalization ;
2023-03-04 14:43:08 +01:00
using System.IO ;
using System.Linq ;
2021-12-23 17:55:50 +01:00
using Path = System . IO . Path ;
2020-07-09 06:31:15 +02:00
namespace Ryujinx.HLE.HOS
{
public class ModLoader
{
private const string RomfsDir = "romfs" ;
private const string ExefsDir = "exefs" ;
2021-03-27 15:12:05 +01:00
private const string CheatDir = "cheats" ;
2020-07-09 06:31:15 +02:00
private const string RomfsContainer = "romfs.bin" ;
private const string ExefsContainer = "exefs.nsp" ;
private const string StubExtension = ".stub" ;
2021-03-27 15:12:05 +01:00
private const string CheatExtension = ".txt" ;
private const string DefaultCheatName = "<default>" ;
2020-07-09 06:31:15 +02:00
private const string AmsContentsDir = "contents" ;
private const string AmsNsoPatchDir = "exefs_patches" ;
private const string AmsNroPatchDir = "nro_patches" ;
private const string AmsKipPatchDir = "kip_patches" ;
2022-12-05 14:47:39 +01:00
public readonly struct Mod < T > where T : FileSystemInfo
2020-07-09 06:31:15 +02:00
{
public readonly string Name ;
public readonly T Path ;
public Mod ( string name , T path )
{
Name = name ;
Path = path ;
}
}
2021-03-27 15:12:05 +01:00
public struct Cheat
{
// Atmosphere identifies the executables with the first 8 bytes
// of the build id, which is equivalent to 16 hex digits.
public const int CheatIdSize = 16 ;
public readonly string Name ;
public readonly FileInfo Path ;
public readonly IEnumerable < String > Instructions ;
public Cheat ( string name , FileInfo path , IEnumerable < String > instructions )
{
Name = name ;
Path = path ;
Instructions = instructions ;
}
}
2020-07-09 06:31:15 +02:00
// Title dependent mods
public class ModCache
{
public List < Mod < FileInfo > > RomfsContainers { get ; }
public List < Mod < FileInfo > > ExefsContainers { get ; }
public List < Mod < DirectoryInfo > > RomfsDirs { get ; }
public List < Mod < DirectoryInfo > > ExefsDirs { get ; }
2021-03-27 15:12:05 +01:00
public List < Cheat > Cheats { get ; }
2020-07-09 06:31:15 +02:00
public ModCache ( )
{
RomfsContainers = new List < Mod < FileInfo > > ( ) ;
ExefsContainers = new List < Mod < FileInfo > > ( ) ;
RomfsDirs = new List < Mod < DirectoryInfo > > ( ) ;
ExefsDirs = new List < Mod < DirectoryInfo > > ( ) ;
2021-03-27 15:12:05 +01:00
Cheats = new List < Cheat > ( ) ;
2020-07-09 06:31:15 +02:00
}
}
// Title independent mods
2023-05-05 09:39:08 +02:00
private class PatchCache
2020-07-09 06:31:15 +02:00
{
public List < Mod < DirectoryInfo > > NsoPatches { get ; }
public List < Mod < DirectoryInfo > > NroPatches { get ; }
public List < Mod < DirectoryInfo > > KipPatches { get ; }
2021-02-20 01:25:01 +01:00
internal bool Initialized { get ; set ; }
2020-07-09 06:31:15 +02:00
public PatchCache ( )
{
NsoPatches = new List < Mod < DirectoryInfo > > ( ) ;
NroPatches = new List < Mod < DirectoryInfo > > ( ) ;
KipPatches = new List < Mod < DirectoryInfo > > ( ) ;
2021-02-20 01:25:01 +01:00
Initialized = false ;
2020-07-09 06:31:15 +02:00
}
}
2023-05-05 09:39:08 +02:00
private readonly Dictionary < ulong , ModCache > _appMods ; // key is TitleId
private PatchCache _patches ;
2020-07-09 06:31:15 +02:00
2023-05-05 09:39:08 +02:00
private static readonly EnumerationOptions DirEnumOptions ;
2020-07-09 06:31:15 +02:00
static ModLoader ( )
{
2023-05-05 09:39:08 +02:00
DirEnumOptions = new EnumerationOptions
2020-07-09 06:31:15 +02:00
{
MatchCasing = MatchCasing . CaseInsensitive ,
MatchType = MatchType . Simple ,
RecurseSubdirectories = false ,
ReturnSpecialDirectories = false
} ;
}
public ModLoader ( )
{
2023-05-05 09:39:08 +02:00
_appMods = new Dictionary < ulong , ModCache > ( ) ;
_patches = new PatchCache ( ) ;
2020-07-09 06:31:15 +02:00
}
2023-05-05 09:39:08 +02:00
private void Clear ( )
2020-07-09 06:31:15 +02:00
{
2023-05-05 09:39:08 +02:00
_appMods . Clear ( ) ;
_patches = new PatchCache ( ) ;
2020-07-09 06:31:15 +02:00
}
private static bool StrEquals ( string s1 , string s2 ) = > string . Equals ( s1 , s2 , StringComparison . OrdinalIgnoreCase ) ;
2023-05-05 09:39:08 +02:00
public static string GetModsBasePath ( ) = > EnsureBaseDirStructure ( AppDataManager . GetModsPath ( ) ) ;
public static string GetSdModsBasePath ( ) = > EnsureBaseDirStructure ( AppDataManager . GetSdModsPath ( ) ) ;
2020-08-30 18:51:53 +02:00
2023-05-05 09:39:08 +02:00
private static string EnsureBaseDirStructure ( string modsBasePath )
2020-07-09 06:31:15 +02:00
{
var modsDir = new DirectoryInfo ( modsBasePath ) ;
modsDir . CreateSubdirectory ( AmsContentsDir ) ;
modsDir . CreateSubdirectory ( AmsNsoPatchDir ) ;
modsDir . CreateSubdirectory ( AmsNroPatchDir ) ;
2023-05-05 09:39:08 +02:00
// TODO: uncomment when KIPs are supported
// modsDir.CreateSubdirectory(AmsKipPatchDir);
2020-08-30 18:51:53 +02:00
return modsDir . FullName ;
2020-07-09 06:31:15 +02:00
}
private static DirectoryInfo FindTitleDir ( DirectoryInfo contentsDir , string titleId )
2023-05-05 09:39:08 +02:00
= > contentsDir . EnumerateDirectories ( $"{titleId}*" , DirEnumOptions ) . FirstOrDefault ( ) ;
2020-07-09 06:31:15 +02:00
2023-05-05 09:39:08 +02:00
private static void AddModsFromDirectory ( ModCache mods , DirectoryInfo dir , string titleId )
{
System . Text . StringBuilder types = new ( ) ;
foreach ( var modDir in dir . EnumerateDirectories ( ) )
{
types . Clear ( ) ;
Mod < DirectoryInfo > mod = new ( "" , null ) ;
if ( StrEquals ( RomfsDir , modDir . Name ) )
{
mods . RomfsDirs . Add ( mod = new Mod < DirectoryInfo > ( $"<{titleId} RomFs>" , modDir ) ) ;
types . Append ( 'R' ) ;
}
else if ( StrEquals ( ExefsDir , modDir . Name ) )
{
mods . ExefsDirs . Add ( mod = new Mod < DirectoryInfo > ( $"<{titleId} ExeFs>" , modDir ) ) ;
types . Append ( 'E' ) ;
}
else if ( StrEquals ( CheatDir , modDir . Name ) )
{
types . Append ( 'C' , QueryCheatsDir ( mods , modDir ) ) ;
}
else
{
AddModsFromDirectory ( mods , modDir , titleId ) ;
}
if ( types . Length > 0 )
{
Logger . Info ? . Print ( LogClass . ModLoader , $"Found mod '{mod.Name}' [{types}]" ) ;
}
}
}
public static string GetTitleDir ( string modsBasePath , string titleId )
2020-07-09 06:31:15 +02:00
{
var contentsDir = new DirectoryInfo ( Path . Combine ( modsBasePath , AmsContentsDir ) ) ;
var titleModsPath = FindTitleDir ( contentsDir , titleId ) ;
if ( titleModsPath = = null )
{
2022-04-08 11:09:35 +02:00
Logger . Info ? . Print ( LogClass . ModLoader , $"Creating mods directory for Title {titleId.ToUpper()}" ) ;
2020-07-09 06:31:15 +02:00
titleModsPath = contentsDir . CreateSubdirectory ( titleId ) ;
}
return titleModsPath . FullName ;
}
// Static Query Methods
2023-05-05 09:39:08 +02:00
private static void QueryPatchDirs ( PatchCache cache , DirectoryInfo patchDir )
2020-07-09 06:31:15 +02:00
{
2023-05-05 09:39:08 +02:00
if ( cache . Initialized | | ! patchDir . Exists )
{
return ;
}
2020-07-09 06:31:15 +02:00
2023-05-05 09:39:08 +02:00
List < Mod < DirectoryInfo > > patches ;
string type ;
2020-07-09 06:31:15 +02:00
2023-05-05 09:39:08 +02:00
if ( StrEquals ( AmsNsoPatchDir , patchDir . Name ) )
{
patches = cache . NsoPatches ; type = "NSO" ;
}
else if ( StrEquals ( AmsNroPatchDir , patchDir . Name ) )
{
patches = cache . NroPatches ; type = "NRO" ;
}
else if ( StrEquals ( AmsKipPatchDir , patchDir . Name ) )
{
patches = cache . KipPatches ; type = "KIP" ;
}
else
{
return ;
}
2020-07-09 06:31:15 +02:00
foreach ( var modDir in patchDir . EnumerateDirectories ( ) )
{
patches . Add ( new Mod < DirectoryInfo > ( modDir . Name , modDir ) ) ;
2020-08-04 01:32:53 +02:00
Logger . Info ? . Print ( LogClass . ModLoader , $"Found {type} patch '{modDir.Name}'" ) ;
2020-07-09 06:31:15 +02:00
}
}
2023-05-05 09:39:08 +02:00
private static void QueryTitleDir ( ModCache mods , DirectoryInfo titleDir )
2020-07-09 06:31:15 +02:00
{
2023-05-05 09:39:08 +02:00
if ( ! titleDir . Exists )
{
return ;
}
2020-07-09 06:31:15 +02:00
var fsFile = new FileInfo ( Path . Combine ( titleDir . FullName , RomfsContainer ) ) ;
if ( fsFile . Exists )
{
mods . RomfsContainers . Add ( new Mod < FileInfo > ( $"<{titleDir.Name} RomFs>" , fsFile ) ) ;
}
fsFile = new FileInfo ( Path . Combine ( titleDir . FullName , ExefsContainer ) ) ;
if ( fsFile . Exists )
{
mods . ExefsContainers . Add ( new Mod < FileInfo > ( $"<{titleDir.Name} ExeFs>" , fsFile ) ) ;
}
2023-05-05 09:39:08 +02:00
AddModsFromDirectory ( mods , titleDir , titleDir . Name ) ;
2020-07-09 06:31:15 +02:00
}
public static void QueryContentsDir ( ModCache mods , DirectoryInfo contentsDir , ulong titleId )
{
2023-05-05 09:39:08 +02:00
if ( ! contentsDir . Exists )
{
return ;
}
2020-07-09 06:31:15 +02:00
2021-02-20 01:25:01 +01:00
Logger . Info ? . Print ( LogClass . ModLoader , $"Searching mods for {((titleId & 0x1000) != 0 ? " DLC " : " Title ")} {titleId:X16}" ) ;
2020-07-09 06:31:15 +02:00
var titleDir = FindTitleDir ( contentsDir , $"{titleId:x16}" ) ;
if ( titleDir ! = null )
{
QueryTitleDir ( mods , titleDir ) ;
}
}
2021-03-27 15:12:05 +01:00
private static int QueryCheatsDir ( ModCache mods , DirectoryInfo cheatsDir )
{
if ( ! cheatsDir . Exists )
{
return 0 ;
}
int numMods = 0 ;
foreach ( FileInfo file in cheatsDir . EnumerateFiles ( ) )
{
if ( ! StrEquals ( CheatExtension , file . Extension ) )
{
continue ;
}
string cheatId = Path . GetFileNameWithoutExtension ( file . Name ) ;
if ( cheatId . Length ! = Cheat . CheatIdSize )
{
continue ;
}
if ( ! ulong . TryParse ( cheatId , NumberStyles . HexNumber , CultureInfo . InvariantCulture , out _ ) )
{
continue ;
}
2023-05-05 09:39:08 +02:00
int oldCheatsCount = mods . Cheats . Count ;
2021-03-27 15:12:05 +01:00
// A cheat file can contain several cheats for the same executable, so the file must be parsed in
// order to properly enumerate them.
mods . Cheats . AddRange ( GetCheatsInFile ( file ) ) ;
2023-05-05 09:39:08 +02:00
if ( mods . Cheats . Count - oldCheatsCount > 0 )
{
numMods + + ;
}
2021-03-27 15:12:05 +01:00
}
return numMods ;
}
private static IEnumerable < Cheat > GetCheatsInFile ( FileInfo cheatFile )
{
string cheatName = DefaultCheatName ;
2023-05-05 09:39:08 +02:00
List < string > instructions = new ( ) ;
List < Cheat > cheats = new ( ) ;
2021-03-27 15:12:05 +01:00
2023-05-05 09:39:08 +02:00
using StreamReader cheatData = cheatFile . OpenText ( ) ;
while ( cheatData . ReadLine ( ) is { } line )
2021-03-27 15:12:05 +01:00
{
2023-05-05 09:39:08 +02:00
line = line . Trim ( ) ;
2023-05-03 11:20:05 +02:00
2023-05-05 09:39:08 +02:00
if ( line . StartsWith ( '[' ) )
{
// This line starts a new cheat section.
if ( ! line . EndsWith ( ']' ) | | line . Length < 3 )
2021-03-27 15:12:05 +01:00
{
2023-05-05 09:39:08 +02:00
// Skip the entire file if there's any error while parsing the cheat file.
2021-03-27 15:12:05 +01:00
2023-05-05 09:39:08 +02:00
Logger . Warning ? . Print ( LogClass . ModLoader , $"Ignoring cheat '{cheatFile.FullName}' because it is malformed" ) ;
2021-03-27 15:12:05 +01:00
2023-05-05 09:39:08 +02:00
return Array . Empty < Cheat > ( ) ;
2023-05-03 11:20:05 +02:00
}
2023-05-05 09:39:08 +02:00
// Add the previous section to the list.
if ( instructions . Count > 0 )
2021-03-27 15:12:05 +01:00
{
2023-05-05 09:39:08 +02:00
cheats . Add ( new Cheat ( $"<{cheatName} Cheat>" , cheatFile , instructions ) ) ;
2021-03-27 15:12:05 +01:00
}
2023-05-03 11:20:05 +02:00
2023-05-05 09:39:08 +02:00
// Start a new cheat section.
cheatName = line . Substring ( 1 , line . Length - 2 ) ;
instructions = new List < string > ( ) ;
}
else if ( line . Length > 0 )
2021-03-27 15:12:05 +01:00
{
2023-05-05 09:39:08 +02:00
// The line contains an instruction.
instructions . Add ( line ) ;
2021-03-27 15:12:05 +01:00
}
}
2023-05-05 09:39:08 +02:00
// Add the last section being processed.
if ( instructions . Count > 0 )
{
cheats . Add ( new Cheat ( $"<{cheatName} Cheat>" , cheatFile , instructions ) ) ;
}
2021-03-27 15:12:05 +01:00
return cheats ;
}
2021-02-20 01:25:01 +01:00
// Assumes searchDirPaths don't overlap
2023-05-05 09:39:08 +02:00
private static void CollectMods ( Dictionary < ulong , ModCache > modCaches , PatchCache patches , params string [ ] searchDirPaths )
2020-07-09 06:31:15 +02:00
{
static bool IsPatchesDir ( string name ) = > StrEquals ( AmsNsoPatchDir , name ) | |
StrEquals ( AmsNroPatchDir , name ) | |
StrEquals ( AmsKipPatchDir , name ) ;
2021-02-20 01:25:01 +01:00
static bool IsContentsDir ( string name ) = > StrEquals ( AmsContentsDir , name ) ;
static bool TryQuery ( DirectoryInfo searchDir , PatchCache patches , Dictionary < ulong , ModCache > modCaches )
2020-07-09 06:31:15 +02:00
{
2021-02-20 01:25:01 +01:00
if ( IsContentsDir ( searchDir . Name ) )
2020-07-09 06:31:15 +02:00
{
2023-05-05 09:39:08 +02:00
foreach ( ( ulong titleId , ModCache cache ) in modCaches )
2020-07-09 06:31:15 +02:00
{
2021-02-20 01:25:01 +01:00
QueryContentsDir ( cache , searchDir , titleId ) ;
2020-07-09 06:31:15 +02:00
}
2021-02-20 01:25:01 +01:00
return true ;
2020-07-09 06:31:15 +02:00
}
2021-02-20 01:25:01 +01:00
else if ( IsPatchesDir ( searchDir . Name ) )
2020-07-09 06:31:15 +02:00
{
2021-02-20 01:25:01 +01:00
QueryPatchDirs ( patches , searchDir ) ;
2020-07-09 06:31:15 +02:00
return true ;
}
return false ;
}
foreach ( var path in searchDirPaths )
{
2021-02-20 01:25:01 +01:00
var searchDir = new DirectoryInfo ( path ) ;
if ( ! searchDir . Exists )
2020-07-09 06:31:15 +02:00
{
2021-02-20 01:25:01 +01:00
Logger . Warning ? . Print ( LogClass . ModLoader , $"Mod Search Dir '{searchDir.FullName}' doesn't exist" ) ;
2020-07-09 06:31:15 +02:00
continue ;
}
2021-02-20 01:25:01 +01:00
if ( ! TryQuery ( searchDir , patches , modCaches ) )
2020-07-09 06:31:15 +02:00
{
2021-02-20 01:25:01 +01:00
foreach ( var subdir in searchDir . EnumerateDirectories ( ) )
2020-07-09 06:31:15 +02:00
{
2021-02-20 01:25:01 +01:00
TryQuery ( subdir , patches , modCaches ) ;
2020-07-09 06:31:15 +02:00
}
}
}
2021-02-20 01:25:01 +01:00
patches . Initialized = true ;
2020-07-09 06:31:15 +02:00
}
2021-02-20 01:25:01 +01:00
public void CollectMods ( IEnumerable < ulong > titles , params string [ ] searchDirPaths )
2020-07-09 06:31:15 +02:00
{
2021-02-20 01:25:01 +01:00
Clear ( ) ;
foreach ( ulong titleId in titles )
2020-07-09 06:31:15 +02:00
{
2023-05-05 09:39:08 +02:00
_appMods [ titleId ] = new ModCache ( ) ;
2020-07-09 06:31:15 +02:00
}
2023-05-05 09:39:08 +02:00
CollectMods ( _appMods , _patches , searchDirPaths ) ;
2020-07-09 06:31:15 +02:00
}
internal IStorage ApplyRomFsMods ( ulong titleId , IStorage baseStorage )
{
2023-05-05 09:39:08 +02:00
if ( ! _appMods . TryGetValue ( titleId , out ModCache mods ) | | mods . RomfsDirs . Count + mods . RomfsContainers . Count = = 0 )
2020-07-09 06:31:15 +02:00
{
return baseStorage ;
}
var fileSet = new HashSet < string > ( ) ;
var builder = new RomFsBuilder ( ) ;
int count = 0 ;
2020-08-04 01:32:53 +02:00
Logger . Info ? . Print ( LogClass . ModLoader , $"Applying RomFS mods for Title {titleId:X16}" ) ;
2020-07-09 06:31:15 +02:00
// Prioritize loose files first
foreach ( var mod in mods . RomfsDirs )
{
using ( IFileSystem fs = new LocalFileSystem ( mod . Path . FullName ) )
{
AddFiles ( fs , mod . Name , fileSet , builder ) ;
}
count + + ;
}
// Then files inside images
foreach ( var mod in mods . RomfsContainers )
{
2020-08-04 01:32:53 +02:00
Logger . Info ? . Print ( LogClass . ModLoader , $"Found 'romfs.bin' for Title {titleId:X16}" ) ;
2020-07-09 06:31:15 +02:00
using ( IFileSystem fs = new RomFsFileSystem ( mod . Path . OpenRead ( ) . AsStorage ( ) ) )
{
AddFiles ( fs , mod . Name , fileSet , builder ) ;
}
count + + ;
}
if ( fileSet . Count = = 0 )
{
2020-08-04 01:32:53 +02:00
Logger . Info ? . Print ( LogClass . ModLoader , "No files found. Using base RomFS" ) ;
2020-07-09 06:31:15 +02:00
return baseStorage ;
}
2020-08-04 01:32:53 +02:00
Logger . Info ? . Print ( LogClass . ModLoader , $"Replaced {fileSet.Count} file(s) over {count} mod(s). Processing base storage..." ) ;
2020-07-09 06:31:15 +02:00
// And finally, the base romfs
var baseRom = new RomFsFileSystem ( baseStorage ) ;
foreach ( var entry in baseRom . EnumerateEntries ( )
. Where ( f = > f . Type = = DirectoryEntryType . File & & ! fileSet . Contains ( f . FullPath ) )
. OrderBy ( f = > f . FullPath , StringComparer . Ordinal ) )
{
2021-12-23 17:55:50 +01:00
using var file = new UniqueRef < IFile > ( ) ;
2023-03-02 03:42:27 +01:00
baseRom . OpenFile ( ref file . Ref , entry . FullPath . ToU8Span ( ) , OpenMode . Read ) . ThrowIfFailure ( ) ;
2021-12-23 17:55:50 +01:00
builder . AddFile ( entry . FullPath , file . Release ( ) ) ;
2020-07-09 06:31:15 +02:00
}
2020-08-04 01:32:53 +02:00
Logger . Info ? . Print ( LogClass . ModLoader , "Building new RomFS..." ) ;
2020-07-09 06:31:15 +02:00
IStorage newStorage = builder . Build ( ) ;
2020-08-04 01:32:53 +02:00
Logger . Info ? . Print ( LogClass . ModLoader , "Using modded RomFS" ) ;
2020-07-09 06:31:15 +02:00
return newStorage ;
}
2023-05-05 09:39:08 +02:00
private static void AddFiles ( IFileSystem fs , string modName , ISet < string > fileSet , RomFsBuilder builder )
2020-07-09 06:31:15 +02:00
{
foreach ( var entry in fs . EnumerateEntries ( )
. Where ( f = > f . Type = = DirectoryEntryType . File )
. OrderBy ( f = > f . FullPath , StringComparer . Ordinal ) )
{
2021-12-23 17:55:50 +01:00
using var file = new UniqueRef < IFile > ( ) ;
2023-03-02 03:42:27 +01:00
fs . OpenFile ( ref file . Ref , entry . FullPath . ToU8Span ( ) , OpenMode . Read ) . ThrowIfFailure ( ) ;
2020-07-09 06:31:15 +02:00
if ( fileSet . Add ( entry . FullPath ) )
{
2021-12-23 17:55:50 +01:00
builder . AddFile ( entry . FullPath , file . Release ( ) ) ;
2020-07-09 06:31:15 +02:00
}
else
{
2020-08-04 01:32:53 +02:00
Logger . Warning ? . Print ( LogClass . ModLoader , $" Skipped duplicate file '{entry.FullPath}' from '{modName}'" , "ApplyRomFsMods" ) ;
2020-07-09 06:31:15 +02:00
}
}
}
internal bool ReplaceExefsPartition ( ulong titleId , ref IFileSystem exefs )
{
2023-05-05 09:39:08 +02:00
if ( ! _appMods . TryGetValue ( titleId , out ModCache mods ) | | mods . ExefsContainers . Count = = 0 )
2020-07-09 06:31:15 +02:00
{
return false ;
}
if ( mods . ExefsContainers . Count > 1 )
{
2020-08-04 01:32:53 +02:00
Logger . Warning ? . Print ( LogClass . ModLoader , "Multiple ExeFS partition replacements detected" ) ;
2020-07-09 06:31:15 +02:00
}
2020-08-04 01:32:53 +02:00
Logger . Info ? . Print ( LogClass . ModLoader , $"Using replacement ExeFS partition" ) ;
2020-07-09 06:31:15 +02:00
exefs = new PartitionFileSystem ( mods . ExefsContainers [ 0 ] . Path . OpenRead ( ) . AsStorage ( ) ) ;
return true ;
}
2020-12-29 20:54:32 +01:00
public struct ModLoadResult
2020-07-09 06:31:15 +02:00
{
2020-12-29 20:54:32 +01:00
public BitVector32 Stubs ;
public BitVector32 Replaces ;
2021-08-12 23:56:24 +02:00
public MetaLoader Npdm ;
2020-12-29 20:54:32 +01:00
public bool Modified = > ( Stubs . Data | Replaces . Data ) ! = 0 ;
}
internal ModLoadResult ApplyExefsMods ( ulong titleId , NsoExecutable [ ] nsos )
{
2023-05-05 09:39:08 +02:00
ModLoadResult modLoadResult = new ( )
2020-12-29 20:54:32 +01:00
{
Stubs = new BitVector32 ( ) ,
Replaces = new BitVector32 ( )
} ;
2023-05-05 09:39:08 +02:00
if ( ! _appMods . TryGetValue ( titleId , out ModCache mods ) | | mods . ExefsDirs . Count = = 0 )
2020-07-09 06:31:15 +02:00
{
2020-12-29 20:54:32 +01:00
return modLoadResult ;
2020-07-09 06:31:15 +02:00
}
2023-03-31 21:16:46 +02:00
if ( nsos . Length ! = ProcessConst . ExeFsPrefixes . Length )
2020-07-09 06:31:15 +02:00
{
2020-12-29 20:54:32 +01:00
throw new ArgumentOutOfRangeException ( "NSO Count is incorrect" ) ;
2020-07-09 06:31:15 +02:00
}
var exeMods = mods . ExefsDirs ;
foreach ( var mod in exeMods )
{
2023-03-31 21:16:46 +02:00
for ( int i = 0 ; i < ProcessConst . ExeFsPrefixes . Length ; + + i )
2020-07-09 06:31:15 +02:00
{
2023-03-31 21:16:46 +02:00
var nsoName = ProcessConst . ExeFsPrefixes [ i ] ;
2020-07-09 06:31:15 +02:00
2023-05-05 09:39:08 +02:00
FileInfo nsoFile = new ( Path . Combine ( mod . Path . FullName , nsoName ) ) ;
2020-07-09 06:31:15 +02:00
if ( nsoFile . Exists )
{
2020-12-29 20:54:32 +01:00
if ( modLoadResult . Replaces [ 1 < < i ] )
2020-07-09 06:31:15 +02:00
{
2020-08-04 01:32:53 +02:00
Logger . Warning ? . Print ( LogClass . ModLoader , $"Multiple replacements to '{nsoName}'" ) ;
2020-12-29 20:54:32 +01:00
2020-07-09 06:31:15 +02:00
continue ;
}
2020-12-29 20:54:32 +01:00
modLoadResult . Replaces [ 1 < < i ] = true ;
2020-07-09 06:31:15 +02:00
nsos [ i ] = new NsoExecutable ( nsoFile . OpenRead ( ) . AsStorage ( ) , nsoName ) ;
2020-08-04 01:32:53 +02:00
Logger . Info ? . Print ( LogClass . ModLoader , $"NSO '{nsoName}' replaced" ) ;
2020-12-29 20:54:32 +01:00
}
2020-07-09 06:31:15 +02:00
2020-12-29 20:54:32 +01:00
modLoadResult . Stubs [ 1 < < i ] | = File . Exists ( Path . Combine ( mod . Path . FullName , nsoName + StubExtension ) ) ;
}
2023-05-05 09:39:08 +02:00
FileInfo npdmFile = new ( Path . Combine ( mod . Path . FullName , "main.npdm" ) ) ;
2021-01-03 12:30:31 +01:00
if ( npdmFile . Exists )
2020-12-29 20:54:32 +01:00
{
2021-01-03 12:30:31 +01:00
if ( modLoadResult . Npdm ! = null )
2020-12-29 20:54:32 +01:00
{
Logger . Warning ? . Print ( LogClass . ModLoader , "Multiple replacements to 'main.npdm'" ) ;
2020-07-09 06:31:15 +02:00
continue ;
}
2021-08-12 23:56:24 +02:00
modLoadResult . Npdm = new MetaLoader ( ) ;
modLoadResult . Npdm . Load ( File . ReadAllBytes ( npdmFile . FullName ) ) ;
2021-02-20 01:25:01 +01:00
2021-08-12 23:56:24 +02:00
Logger . Info ? . Print ( LogClass . ModLoader , "main.npdm replaced" ) ;
2020-07-09 06:31:15 +02:00
}
}
2023-03-31 21:16:46 +02:00
for ( int i = ProcessConst . ExeFsPrefixes . Length - 1 ; i > = 0 ; - - i )
2020-07-09 06:31:15 +02:00
{
2020-12-29 20:54:32 +01:00
if ( modLoadResult . Stubs [ 1 < < i ] & & ! modLoadResult . Replaces [ 1 < < i ] ) // Prioritizes replacements over stubs
2020-07-09 06:31:15 +02:00
{
2020-08-04 01:32:53 +02:00
Logger . Info ? . Print ( LogClass . ModLoader , $" NSO '{nsos[i].Name}' stubbed" ) ;
2020-12-29 20:54:32 +01:00
nsos [ i ] = null ;
2020-07-09 06:31:15 +02:00
}
}
2020-12-29 20:54:32 +01:00
return modLoadResult ;
2020-07-09 06:31:15 +02:00
}
internal void ApplyNroPatches ( NroExecutable nro )
{
2023-05-05 09:39:08 +02:00
var nroPatches = _patches . NroPatches ;
2020-07-09 06:31:15 +02:00
if ( nroPatches . Count = = 0 ) return ;
// NRO patches aren't offset relative to header unlike NSO
// according to Atmosphere's ro patcher module
ApplyProgramPatches ( nroPatches , 0 , nro ) ;
}
internal bool ApplyNsoPatches ( ulong titleId , params IExecutable [ ] programs )
{
2023-05-05 09:39:08 +02:00
IEnumerable < Mod < DirectoryInfo > > nsoMods = _patches . NsoPatches ;
2020-07-15 01:40:17 +02:00
2023-05-05 09:39:08 +02:00
if ( _appMods . TryGetValue ( titleId , out ModCache mods ) )
2020-07-15 01:40:17 +02:00
{
nsoMods = nsoMods . Concat ( mods . ExefsDirs ) ;
}
2020-07-09 06:31:15 +02:00
// NSO patches are created with offset 0 according to Atmosphere's patcher module
// But `Program` doesn't contain the header which is 0x100 bytes. So, we adjust for that here
return ApplyProgramPatches ( nsoMods , 0x100 , programs ) ;
}
2021-03-27 15:12:05 +01:00
internal void LoadCheats ( ulong titleId , ProcessTamperInfo tamperInfo , TamperMachine tamperMachine )
{
2023-05-05 09:39:08 +02:00
if ( tamperInfo ? . BuildIds = = null | | tamperInfo . CodeAddresses = = null )
2021-03-27 15:12:05 +01:00
{
Logger . Error ? . Print ( LogClass . ModLoader , "Unable to install cheat because the associated process is invalid" ) ;
2021-04-02 15:42:25 +02:00
return ;
2021-03-27 15:12:05 +01:00
}
Logger . Info ? . Print ( LogClass . ModLoader , $"Build ids found for title {titleId:X16}:\n {String.Join(" \ n ", tamperInfo.BuildIds)}" ) ;
2023-05-05 09:39:08 +02:00
if ( ! _appMods . TryGetValue ( titleId , out ModCache mods ) | | mods . Cheats . Count = = 0 )
2021-03-27 15:12:05 +01:00
{
return ;
}
var cheats = mods . Cheats ;
var processExes = tamperInfo . BuildIds . Zip ( tamperInfo . CodeAddresses , ( k , v ) = > new { k , v } )
2023-05-05 09:39:08 +02:00
. ToDictionary ( x = > x . k [ . . Math . Min ( Cheat . CheatIdSize , x . k . Length ) ] , x = > x . v ) ;
2021-03-27 15:12:05 +01:00
foreach ( var cheat in cheats )
{
string cheatId = Path . GetFileNameWithoutExtension ( cheat . Path . Name ) . ToUpper ( ) ;
if ( ! processExes . TryGetValue ( cheatId , out ulong exeAddress ) )
{
Logger . Warning ? . Print ( LogClass . ModLoader , $"Skipping cheat '{cheat.Name}' because no executable matches its BuildId {cheatId} (check if the game title and version are correct)" ) ;
continue ;
}
Logger . Info ? . Print ( LogClass . ModLoader , $"Installing cheat '{cheat.Name}'" ) ;
2022-01-03 09:39:43 +01:00
tamperMachine . InstallAtmosphereCheat ( cheat . Name , cheatId , cheat . Instructions , tamperInfo , exeAddress ) ;
}
EnableCheats ( titleId , tamperMachine ) ;
}
internal void EnableCheats ( ulong titleId , TamperMachine tamperMachine )
{
var contentDirectory = FindTitleDir ( new DirectoryInfo ( Path . Combine ( GetModsBasePath ( ) , AmsContentsDir ) ) , $"{titleId:x16}" ) ;
string enabledCheatsPath = Path . Combine ( contentDirectory . FullName , CheatDir , "enabled.txt" ) ;
if ( File . Exists ( enabledCheatsPath ) )
{
tamperMachine . EnableCheats ( File . ReadAllLines ( enabledCheatsPath ) ) ;
2021-03-27 15:12:05 +01:00
}
}
2020-07-09 06:31:15 +02:00
private static bool ApplyProgramPatches ( IEnumerable < Mod < DirectoryInfo > > mods , int protectedOffset , params IExecutable [ ] programs )
{
int count = 0 ;
MemPatch [ ] patches = new MemPatch [ programs . Length ] ;
for ( int i = 0 ; i < patches . Length ; + + i )
{
patches [ i ] = new MemPatch ( ) ;
}
var buildIds = programs . Select ( p = > p switch
{
2023-02-08 14:54:58 +01:00
NsoExecutable nso = > Convert . ToHexString ( nso . BuildId . ItemsRo . ToArray ( ) ) . TrimEnd ( '0' ) ,
NroExecutable nro = > Convert . ToHexString ( nro . Header . BuildId ) . TrimEnd ( '0' ) ,
2020-07-09 06:31:15 +02:00
_ = > string . Empty
} ) . ToList ( ) ;
int GetIndex ( string buildId ) = > buildIds . FindIndex ( id = > id = = buildId ) ; // O(n) but list is small
// Collect patches
foreach ( var mod in mods )
{
var patchDir = mod . Path ;
foreach ( var patchFile in patchDir . EnumerateFiles ( ) )
{
if ( StrEquals ( ".ips" , patchFile . Extension ) ) // IPS|IPS32
{
string filename = Path . GetFileNameWithoutExtension ( patchFile . FullName ) . Split ( '.' ) [ 0 ] ;
string buildId = filename . TrimEnd ( '0' ) ;
int index = GetIndex ( buildId ) ;
if ( index = = - 1 )
{
continue ;
}
2020-08-04 01:32:53 +02:00
Logger . Info ? . Print ( LogClass . ModLoader , $"Matching IPS patch '{patchFile.Name}' in '{mod.Name}' bid={buildId}" ) ;
2020-07-09 06:31:15 +02:00
using var fs = patchFile . OpenRead ( ) ;
using var reader = new BinaryReader ( fs ) ;
var patcher = new IpsPatcher ( reader ) ;
patcher . AddPatches ( patches [ index ] ) ;
}
else if ( StrEquals ( ".pchtxt" , patchFile . Extension ) ) // IPSwitch
{
using var fs = patchFile . OpenRead ( ) ;
using var reader = new StreamReader ( fs ) ;
var patcher = new IPSwitchPatcher ( reader ) ;
int index = GetIndex ( patcher . BuildId ) ;
if ( index = = - 1 )
{
continue ;
}
2020-08-04 01:32:53 +02:00
Logger . Info ? . Print ( LogClass . ModLoader , $"Matching IPSwitch patch '{patchFile.Name}' in '{mod.Name}' bid={patcher.BuildId}" ) ;
2020-07-09 06:31:15 +02:00
patcher . AddPatches ( patches [ index ] ) ;
}
}
}
// Apply patches
for ( int i = 0 ; i < programs . Length ; + + i )
{
count + = patches [ i ] . Patch ( programs [ i ] . Program , protectedOffset ) ;
}
return count > 0 ;
}
}
2023-05-05 09:39:08 +02:00
}