Skip to content

Commit 36055ca

Browse files
committed
Replaced NativeHelper with CoreFunctionsManager.
1 parent 3761f47 commit 36055ca

33 files changed

+666
-1072
lines changed

Core/CoreFunctionsManager.cs

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics.Contracts;
4+
using System.Linq;
5+
using ReClassNET.Debugger;
6+
using ReClassNET.Memory;
7+
using ReClassNET.Native;
8+
using ReClassNET.Util;
9+
10+
namespace ReClassNET.Core
11+
{
12+
public class CoreFunctionsManager : IDisposable
13+
{
14+
private const string CoreFunctionsModuleWindows = "NativeHelper.dll";
15+
private const string CoreFunctionsModuleUnix = "NativeHelper.so";
16+
17+
private readonly Dictionary<string, ICoreProcessFunctions> functionsRegistry = new Dictionary<string, ICoreProcessFunctions>();
18+
19+
public IEnumerable<string> FunctionProviders => functionsRegistry.Keys;
20+
21+
private IntPtr internalCoreFunctionsHandle;
22+
23+
private readonly InternalCoreFunctions internalCoreFunctions;
24+
25+
private ICoreProcessFunctions currentFunctions;
26+
27+
public CoreFunctionsManager()
28+
{
29+
internalCoreFunctionsHandle = NativeMethods.LoadLibrary(
30+
NativeMethods.IsUnix()
31+
? CoreFunctionsModuleUnix
32+
: CoreFunctionsModuleWindows
33+
);
34+
35+
if (internalCoreFunctionsHandle.IsNull())
36+
{
37+
throw new Exception();
38+
}
39+
40+
internalCoreFunctions = new InternalCoreFunctions(internalCoreFunctionsHandle);
41+
42+
RegisterFunctions("Default", internalCoreFunctions);
43+
44+
currentFunctions = internalCoreFunctions;
45+
}
46+
47+
#region IDisposable Support
48+
49+
~CoreFunctionsManager()
50+
{
51+
Dispose(false);
52+
}
53+
54+
protected virtual void Dispose(bool disposing)
55+
{
56+
if (!internalCoreFunctionsHandle.IsNull())
57+
{
58+
NativeMethods.FreeLibrary(internalCoreFunctionsHandle);
59+
60+
internalCoreFunctionsHandle = IntPtr.Zero;
61+
}
62+
}
63+
64+
public void Dispose()
65+
{
66+
Dispose(true);
67+
68+
GC.SuppressFinalize(this);
69+
}
70+
71+
#endregion
72+
73+
public void RegisterFunctions(string provider, ICoreProcessFunctions functions)
74+
{
75+
Contract.Requires(provider != null);
76+
Contract.Requires(functions != null);
77+
78+
functionsRegistry.Add(provider, functions);
79+
}
80+
81+
public void SetActiveFunctionsProvider(string provider)
82+
{
83+
ICoreProcessFunctions functions;
84+
if (!functionsRegistry.TryGetValue(provider, out functions))
85+
{
86+
throw new ArgumentException();
87+
}
88+
89+
currentFunctions = functions;
90+
}
91+
92+
#region Plugin Functions
93+
94+
public void EnumerateProcesses(Action<Tuple<IntPtr, string>> callbackProcess)
95+
{
96+
currentFunctions.EnumerateProcesses(callbackProcess);
97+
}
98+
99+
public void EnumerateRemoteSectionsAndModules(IntPtr process, Action<Section> callbackSection, Action<Module> callbackModule)
100+
{
101+
currentFunctions.EnumerateRemoteSectionsAndModules(process, callbackSection, callbackModule);
102+
}
103+
104+
public IntPtr OpenRemoteProcess(IntPtr pid, ProcessAccess desiredAccess)
105+
{
106+
return currentFunctions.OpenRemoteProcess(pid, desiredAccess);
107+
}
108+
109+
public bool IsProcessValid(IntPtr process)
110+
{
111+
return currentFunctions.IsProcessValid(process);
112+
}
113+
114+
public void CloseRemoteProcess(IntPtr process)
115+
{
116+
currentFunctions.CloseRemoteProcess(process);
117+
}
118+
119+
public bool ReadRemoteMemory(IntPtr process, IntPtr address, ref byte[] buffer, int offset, int size)
120+
{
121+
return currentFunctions.ReadRemoteMemory(process, address, ref buffer, offset, size);
122+
}
123+
124+
public bool WriteRemoteMemory(IntPtr process, IntPtr address, ref byte[] buffer, int offset, int size)
125+
{
126+
return currentFunctions.WriteRemoteMemory(process, address, ref buffer, offset, size);
127+
}
128+
129+
public void ControlRemoteProcess(IntPtr process, ControlRemoteProcessAction action)
130+
{
131+
currentFunctions.ControlRemoteProcess(process, action);
132+
}
133+
134+
public bool AttachDebuggerToProcess(IntPtr id)
135+
{
136+
return currentFunctions.AttachDebuggerToProcess(id);
137+
}
138+
139+
public void DetachDebuggerFromProcess(IntPtr id)
140+
{
141+
currentFunctions.DetachDebuggerFromProcess(id);
142+
}
143+
144+
public void HandleDebugEvent(ref DebugEvent evt)
145+
{
146+
currentFunctions.HandleDebugEvent(ref evt);
147+
}
148+
149+
public bool AwaitDebugEvent(ref DebugEvent evt, int timeoutInMilliseconds)
150+
{
151+
return currentFunctions.AwaitDebugEvent(ref evt, timeoutInMilliseconds);
152+
}
153+
154+
public bool SetHardwareBreakpoint(IntPtr id, IntPtr address, HardwareBreakpointRegister register, HardwareBreakpointTrigger trigger, HardwareBreakpointSize size, bool set)
155+
{
156+
return currentFunctions.SetHardwareBreakpoint(id, address, register, trigger, size, set);
157+
}
158+
159+
#endregion
160+
161+
#region Internal Core Functions
162+
163+
public bool DisassembleCode(IntPtr address, int length, IntPtr virtualAddress, out InstructionData instruction)
164+
{
165+
return internalCoreFunctions.DisassembleCode(address, length, virtualAddress, out instruction);
166+
}
167+
168+
#endregion
169+
}
170+
}

Core/DataExchange.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Runtime.InteropServices;
2+
3+
namespace ReClassNET.Core
4+
{
5+
public enum ProcessAccess
6+
{
7+
Read,
8+
Write,
9+
Full
10+
};
11+
12+
public enum ControlRemoteProcessAction
13+
{
14+
Suspend,
15+
Resume,
16+
Terminate
17+
}
18+
19+
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1)]
20+
public struct InstructionData
21+
{
22+
public int Length;
23+
24+
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 15)]
25+
public byte[] Data;
26+
27+
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
28+
public string Instruction;
29+
};
30+
}

Core/ICoreProcessFunctions.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using ReClassNET.Debugger;
3+
using ReClassNET.Memory;
4+
using ReClassNET.Native;
5+
6+
namespace ReClassNET.Core
7+
{
8+
public interface ICoreProcessFunctions
9+
{
10+
void EnumerateProcesses(Action<Tuple<IntPtr, string>> callbackProcess);
11+
12+
void EnumerateRemoteSectionsAndModules(IntPtr process, Action<Section> callbackSection, Action<Module> callbackModule);
13+
14+
IntPtr OpenRemoteProcess(IntPtr pid, ProcessAccess desiredAccess);
15+
16+
bool IsProcessValid(IntPtr process);
17+
18+
void CloseRemoteProcess(IntPtr process);
19+
20+
bool ReadRemoteMemory(IntPtr process, IntPtr address, ref byte[] buffer, int offset, int size);
21+
22+
bool WriteRemoteMemory(IntPtr process, IntPtr address, ref byte[] buffer, int offset, int size);
23+
24+
void ControlRemoteProcess(IntPtr process, ControlRemoteProcessAction action);
25+
26+
bool AttachDebuggerToProcess(IntPtr id);
27+
28+
void DetachDebuggerFromProcess(IntPtr id);
29+
30+
bool AwaitDebugEvent(ref DebugEvent evt, int timeoutInMilliseconds);
31+
32+
void HandleDebugEvent(ref DebugEvent evt);
33+
34+
bool SetHardwareBreakpoint(IntPtr id, IntPtr address, HardwareBreakpointRegister register, HardwareBreakpointTrigger trigger, HardwareBreakpointSize size, bool set);
35+
}
36+
}

Core/InternalCoreFunctions.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
using ReClassNET.Native;
4+
5+
namespace ReClassNET.Core
6+
{
7+
internal class InternalCoreFunctions : NativeCoreWrapper
8+
{
9+
[return: MarshalAs(UnmanagedType.I1)]
10+
private delegate bool DisassembleCodeDelegate(IntPtr address, IntPtr length, IntPtr virtualAddress, out InstructionData instruction);
11+
12+
private DisassembleCodeDelegate disassembleCodeDelegate;
13+
14+
public InternalCoreFunctions(IntPtr handle)
15+
: base(handle)
16+
{
17+
disassembleCodeDelegate = GetFunctionDelegate<DisassembleCodeDelegate>(handle, "DisassembleCode");
18+
}
19+
20+
public bool DisassembleCode(IntPtr address, int length, IntPtr virtualAddress, out InstructionData instruction)
21+
{
22+
return disassembleCodeDelegate(address, (IntPtr)length, virtualAddress, out instruction);
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)