forked from UnknownX7/OOBlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Memory.cs
85 lines (72 loc) · 2.49 KB
/
Memory.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using Dalamud.Logging;
namespace OOBlugin
{
public static class Memory
{
public class Replacer : IDisposable
{
public nint Address { get; private set; } = nint.Zero;
private readonly byte[] newBytes;
private readonly byte[] oldBytes;
public bool IsEnabled { get; private set; } = false;
public bool IsValid => Address != nint.Zero;
public string ReadBytes => !IsValid ? string.Empty : oldBytes.Aggregate(string.Empty, (current, b) => current + (b.ToString("X2") + " "));
public Replacer(nint addr, byte[] bytes, bool startEnabled = false)
{
if (addr == nint.Zero) return;
Address = addr;
newBytes = bytes;
SafeMemory.ReadBytes(addr, bytes.Length, out oldBytes);
createdReplacers.Add(this);
if (startEnabled)
Enable();
}
public Replacer(string sig, byte[] bytes, bool startEnabled = false)
{
var addr = nint.Zero;
try { addr = DalamudApi.SigScanner.ScanModule(sig); }
catch { PluginLog.LogError($"Failed to find signature {sig}"); }
if (addr == nint.Zero) return;
Address = addr;
newBytes = bytes;
SafeMemory.ReadBytes(addr, bytes.Length, out oldBytes);
createdReplacers.Add(this);
if (startEnabled)
Enable();
}
public void Enable()
{
if (!IsValid) return;
SafeMemory.WriteBytes(Address, newBytes);
IsEnabled = true;
}
public void Disable()
{
if (!IsValid) return;
SafeMemory.WriteBytes(Address, oldBytes);
IsEnabled = false;
}
public void Toggle()
{
if (!IsEnabled)
Enable();
else
Disable();
}
public void Dispose()
{
if (IsEnabled)
Disable();
}
}
private static readonly List<Replacer> createdReplacers = new();
public static void Dispose()
{
foreach (var rep in createdReplacers)
rep.Dispose();
}
}
}