Ryujinx/Ryujinx.HLE/Loaders/Mods/IPSPatcher.cs
mageven 189c0c9c72
Implement modding support (#1249)
* Implement Modding Support

* Executables: Rewrite to use contiguous mem and Spans

* Reorder ExeFs, Npdm, ControlData and SaveData calls

After discussion with gdkchan, it was decided it's best to call
LoadExeFs after all other loads are done as it starts the guest process.

* Build RomFs manually instead of Layering FS

Layered FS approach has considerable latency when building the final
romfs. So, we manually replace files in a single romfs instance.

* Add RomFs modding via storage file

* Fix and cleanup MemPatch

* Add dynamically loaded NRO patching

* Support exefs file replacement

* Rewrite ModLoader to use mods-search architecture

* Disable PPTC when exefs patches are detected

Disable PPTC on exefs replacements too

* Rewrite ModLoader, again

* Increased maintainability and matches Atmosphere closely
* Creates base mods structure if it doesn't exist
* Add Exefs partition replacement
* IPSwitch: Fix nsobid parsing

* Move mod logs to new LogClass

* Allow custom suffixes to title dirs again

* Address nits

* Add a per-App "Open Mods Directory" context menu item

Creates the path if not present.

* Normalize tooltips verbiage

* Use LocalStorage and remove unused namespaces
2020-07-09 14:31:15 +10:00

117 lines
No EOL
3.1 KiB
C#

using Ryujinx.Common.Logging;
using System;
using System.IO;
using System.Text;
namespace Ryujinx.HLE.Loaders.Mods
{
class IpsPatcher
{
MemPatch _patches;
public IpsPatcher(BinaryReader reader)
{
_patches = ParseIps(reader);
if (_patches != null)
{
Logger.PrintInfo(LogClass.ModLoader, "IPS patch loaded successfully");
}
}
private static MemPatch ParseIps(BinaryReader reader)
{
Span<byte> IpsHeaderMagic = Encoding.ASCII.GetBytes("PATCH").AsSpan();
Span<byte> IpsTailMagic = Encoding.ASCII.GetBytes("EOF").AsSpan();
Span<byte> Ips32HeaderMagic = Encoding.ASCII.GetBytes("IPS32").AsSpan();
Span<byte> Ips32TailMagic = Encoding.ASCII.GetBytes("EEOF").AsSpan();
MemPatch patches = new MemPatch();
var header = reader.ReadBytes(IpsHeaderMagic.Length).AsSpan();
if (header.Length != IpsHeaderMagic.Length)
{
return null;
}
bool is32;
Span<byte> tailSpan;
if (header.SequenceEqual(IpsHeaderMagic))
{
is32 = false;
tailSpan = IpsTailMagic;
}
else if (header.SequenceEqual(Ips32HeaderMagic))
{
is32 = true;
tailSpan = Ips32TailMagic;
}
else
{
return null;
}
byte[] buf = new byte[tailSpan.Length];
bool ReadNext(int size) => reader.Read(buf, 0, size) != size;
while (true)
{
if (ReadNext(buf.Length))
{
return null;
}
if (buf.AsSpan().SequenceEqual(tailSpan))
{
break;
}
int patchOffset = is32 ? buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3]
: buf[0] << 16 | buf[1] << 8 | buf[2];
if (ReadNext(2))
{
return null;
}
int patchSize = buf[0] << 8 | buf[1];
if (patchSize == 0) // RLE/Fill mode
{
if (ReadNext(2))
{
return null;
}
int fillLength = buf[0] << 8 | buf[1];
if (ReadNext(1))
{
return null;
}
patches.AddFill((uint)patchOffset, fillLength, buf[0]);
}
else // Copy mode
{
var patch = reader.ReadBytes(patchSize);
if (patch.Length != patchSize)
{
return null;
}
patches.Add((uint)patchOffset, patch);
}
}
return patches;
}
public void AddPatches(MemPatch patches)
{
patches.AddFrom(_patches);
}
}
}