|
| 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 | +} |
0 commit comments