Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions OpenKh.Tools.ModsManager/Services/GameService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public static class GameService
UniqueFileName = "CST_sora.pss",
Detectors = new()
{
new GameDetectorModel { FileName = "SYSTEM.CNF;1", ProductId = "SLPM_666.76;1" },
new GameDetectorModel { FileName = "SYSTEM.CNF;1", ProductId = "SLUS_217.99;1" },
}
}
Expand Down
134 changes: 134 additions & 0 deletions OpenKh.Tools.ModsManager/Services/Pcsx2Injector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ private class Offsets
public int LoadFile { get; init; }
public int GetFileSize { get; init; }
public int LoadFileTask { get; init; }
public int LoadFileTaskVanilla { get; init; }
public int LoadFileAsync { get; init; }
public int LoadFileAsyncJp { get; init; }

public int GetFileSizeRecom { get; init; }
public int GetFileSizeRecomJp { get; init; }
public int RegionInit { get; init; }
public int BufferPointer { get; init; }
public int RegionForce { get; init; }
Expand Down Expand Up @@ -421,6 +424,36 @@ public enum Operation
NOP(),
};

private static readonly uint[] LoadFileTaskHookVanilla = new uint[]
{
// Input:
// S0 DstPtr
// S1 Filename
// T4 return program counter
// T5 Operation
// V0 IdxFilePtr
//
// Work:
// T6 Hook stack
// V0 Return value
//
LUI(T6, HookStack),
SW(S1, T6, Param1), // Filename
SW(S0, T6, Param2), // DstPtr
SW(V0, T6, Param3), // LoadFileTask
SW(T5, T6, ParamOperator), // Operation
LW(T5, T6, ParamOperator),
BNE(T5, (byte)Operation.HookExit, -2),
LW(V1, T6, ParamReturn),
BEQ(V1, Zero, 3),
MOVE(S2, V0),
BEQ(Zero, Zero, 2),
ADDIU(RA, RA, 0x64), // skip the remainder of the function
LI(V0, -1),
JR(RA),
NOP(),
};

private static readonly uint[] LoadFileAsyncHook = new uint[]
{
// Input:
Expand Down Expand Up @@ -482,6 +515,67 @@ public enum Operation
SW(V1, S1, 0x28),
};

private static readonly uint[] LoadFileAsyncHookJp = new uint[]
{
// Input:
//
// Work:
//
LUI(T6, HookStack),
LW(T2, V0, 0x18B0),
LW(T3, V0, 0x189C),
ADDI(T4, V0, 0x18C8),
LW(T0, T4, 0),
ADDIU(T1, T3, 8),
LW(T4, T3, 0x38),
LW(T4, T4, 0),
SW(T0, T6, Param1), // FileDirID
BEQ(T2, T4, 9),
SW(T1, T6, Param2), // FileNamePtr
ADDIU(T4, Zero, (byte)Operation.GetFileSizeRecom),
SW(T4, T6, ParamOperator), // Operation
LW(T4, T6, ParamOperator),
BNE(T4, (byte)Operation.HookExit, -2),
LW(T4, T6, ParamReturn),
BEQ(T4, Zero, 7),
NOP(),
BEQ(Zero, Zero, 8),
SW(T2, T6, Param3), // MemDstPtr
SW(T5, T6, ParamOperator), // Operation
LW(T5, T6, ParamOperator),
BNE(T5, (byte)Operation.HookExit, -2),
LW(T4, T6, ParamReturn),
LUI(V1, 0x5B), // For Fallback
BEQ(T4, Zero, 5),
LW(A2, V0, 0x18CC),
ADD(T2, T2, T4),
SW(T2, V0, 0x18B0),
ADDIU(V0, Zero, 1),
ADDIU(RA, RA, 0x64),
JR(RA),
NOP(),
};

private static readonly uint[] GetFileSizeRecomHookJp = new uint[]
{
LUI(T6, HookStack),
LUI(V1, 0x5C),
ADDI(T4, V1, 0x18C8),
LW(T4, T4, 0),
SW(T4, T6, Param1), // FileDirID
SW(S2, T6, Param2), // FileNamePtr
SW(T5, T6, ParamOperator), // Operation
LW(T5, T6, ParamOperator),
BNE(T5, (byte)Operation.HookExit, -2),
LW(V1, T6, ParamReturn),
// For Fallback
BNE(V1, Zero, 2),
NOP(),
LW(V1, S2, 0x18),
JR(RA),
SW(V1, S1, 0x28),
};



private static readonly uint[] RegionInitPatch = new uint[]
Expand All @@ -499,6 +593,16 @@ public enum Operation

private static readonly Offsets[] _offsets = new Offsets[]
{
new Offsets
{
GameName = "SLPS_251.05;1",
LoadFileTaskVanilla = 0x120308,
},
new Offsets
{
GameName = "SLUS_203.70;1",
LoadFileTaskVanilla = 0x11FEA8,
},
new Offsets
{
GameName = "SLPS_251.97;1",
Expand All @@ -514,6 +618,11 @@ public enum Operation
LoadFileAsync = 0x1A0C6C,
GetFileSizeRecom = 0x1A11A0,
},
new Offsets {
GameName = "SLPM_666.76;1",
LoadFileAsyncJp = 0x1A0CFC,
GetFileSizeRecomJp = 0x1A1230,
},
new Offsets
{
GameName = "SLPM_662.33;1",
Expand Down Expand Up @@ -826,6 +935,15 @@ private void WritePatch(Stream stream, Offsets offsets)
ADDIU(T5, Zero, (byte)Operation.LoadFileTask));
}

if (offsets.LoadFileTaskVanilla > 0)
{
Log.Info("Injecting {0} function", nameof(offsets.LoadFileTaskVanilla));
WritePatch(stream, offsets.LoadFileTaskVanilla,
ADDIU(T4, RA, 0),
JAL(WriteHook(stream, LoadFileTaskHookVanilla)),
ADDIU(T5, Zero, (byte)Operation.LoadFileTask));
}

if (offsets.LoadFileAsync > 0)
{
Log.Info("Injecting {0} function", nameof(offsets.LoadFileAsync));
Expand All @@ -834,6 +952,14 @@ private void WritePatch(Stream stream, Offsets offsets)
ADDIU(T5, Zero, (byte)Operation.LoadFileAsync));
}

if (offsets.LoadFileAsyncJp > 0)
{
Log.Info("Injecting {0} function", nameof(offsets.LoadFileAsyncJp));
WritePatch(stream, offsets.LoadFileAsyncJp,
JAL(WriteHook(stream, LoadFileAsyncHookJp)),
ADDIU(T5, Zero, (byte)Operation.LoadFileAsync));
}

if (offsets.LoadFile > 0)
{
Log.Info("Injecting {0} function", nameof(offsets.LoadFile));
Expand Down Expand Up @@ -869,6 +995,14 @@ private void WritePatch(Stream stream, Offsets offsets)
ADDIU(T5, Zero, (byte)Operation.GetFileSizeRecom));
}

if (offsets.GetFileSizeRecomJp > 0)
{
Log.Info("Injecting {0} function", nameof(offsets.GetFileSizeRecomJp));
WritePatch(stream, offsets.GetFileSizeRecomJp,
JAL(WriteHook(stream, GetFileSizeRecomHookJp)),
ADDIU(T5, Zero, (byte)Operation.GetFileSizeRecom));
}

if (RegionId > 0)
{
Log.Info("Injecting {0} function", "RegionInit");
Expand Down
2 changes: 1 addition & 1 deletion OpenKh.Tools.ModsManager/Views/SetupWizardWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
<StackPanel Margin="10 3 0 10">
<TextBlock>Kingdom Hearts I (Final Mix)</TextBlock>
<TextBlock>Kingdom Hearts II (JP, US, EU, Final Mix)</TextBlock>
<TextBlock>Kingdom Hearts Re:Chain of Memories (US)</TextBlock>
<TextBlock>Kingdom Hearts Re:Chain of Memories (JP, US)</TextBlock>
</StackPanel>
<TextBlock Margin="0 0 0 3">Please select the location of the PlayStation 2 ISO Images.</TextBlock>
<TextBlock Margin="0 0 0 3">Kingdom Hearts 2 PlayStation 2 ISO Image.</TextBlock>
Expand Down