blob: 0a1f12b186c7cb75ae6bba75f028f399ae0588f3 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
using Ryujinx.Common.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Ryujinx.HLE.Loaders.Mods
{
public class MemPatch
{
readonly Dictionary<uint, byte[]> _patches = new();
/// <summary>
/// Adds a patch to specified offset. Overwrites if already present.
/// </summary>
/// <param name="offset">Memory offset</param>
/// <param name="patch">The patch to add</param>
public void Add(uint offset, byte[] patch)
{
_patches[offset] = patch;
}
/// <summary>
/// Adds a patch in the form of an RLE (Fill mode).
/// </summary>
/// <param name="offset">Memory offset</param>
/// <param name="length"The fill length</param>
/// <param name="filler">The byte to fill</param>
public void AddFill(uint offset, int length, byte filler)
{
// TODO: Can be made space efficient by changing `_patches`
// Should suffice for now
byte[] patch = new byte[length];
patch.AsSpan().Fill(filler);
_patches[offset] = patch;
}
/// <summary>
/// Adds all patches from an existing MemPatch
/// </summary>
/// <param name="patches">The patches to add</param>
public void AddFrom(MemPatch patches)
{
if (patches == null)
{
return;
}
foreach (var (patchOffset, patch) in patches._patches)
{
_patches[patchOffset] = patch;
}
}
/// <summary>
/// Applies all the patches added to this instance.
/// </summary>
/// <remarks>
/// Patches are applied in ascending order of offsets to guarantee
/// overlapping patches always apply the same way.
/// </remarks>
/// <param name="memory">The span of bytes to patch</param>
/// <param name="maxSize">The maximum size of the slice of patchable memory</param>
/// <param name="protectedOffset">A secondary offset used in special cases (NSO header)</param>
/// <returns>Successful patches count</returns>
public int Patch(Span<byte> memory, int protectedOffset = 0)
{
int count = 0;
foreach (var (offset, patch) in _patches.OrderBy(item => item.Key))
{
int patchOffset = (int)offset;
int patchSize = patch.Length;
if (patchOffset < protectedOffset || patchOffset > memory.Length)
{
continue; // Add warning?
}
patchOffset -= protectedOffset;
if (patchOffset + patchSize > memory.Length)
{
patchSize = memory.Length - patchOffset; // Add warning?
}
Logger.Info?.Print(LogClass.ModLoader, $"Patching address offset {patchOffset:x} <= {BitConverter.ToString(patch).Replace('-', ' ')} len={patchSize}");
patch.AsSpan(0, patchSize).CopyTo(memory.Slice(patchOffset, patchSize));
count++;
}
return count;
}
}
}
|