static HarmonySharedState() { // create singleton type var type = GetOrCreateSharedStateType(); // copy 'actualVersion' over to our fields var versionField = type.GetField("version"); if ((int)versionField.GetValue(null) == 0) { versionField.SetValue(null, internalVersion); } actualVersion = (int)versionField.GetValue(null); // get or initialize global 'state' field var stateField = type.GetField("state"); if (stateField.GetValue(null) is null) { stateField.SetValue(null, new Dictionary <MethodBase, byte[]>()); } // get or initialize global 'originals' field var originalsField = type.GetField("originals"); if (originalsField != null && originalsField.GetValue(null) is null) { originalsField.SetValue(null, new Dictionary <MethodInfo, MethodBase>()); } // copy 'state' over to our fields state = (Dictionary <MethodBase, byte[]>)stateField.GetValue(null); // copy 'originals' over to our fields originals = new Dictionary <MethodInfo, MethodBase>(); if (originalsField != null) // may not exist in older versions { originals = (Dictionary <MethodInfo, MethodBase>)originalsField.GetValue(null); } // newer .NET versions can re-jit methods so we need to patch them after that happens DetourHelper.Runtime.OnMethodCompiled += (MethodBase method, IntPtr codeStart, ulong codeLen) => { if (method == null) { return; } var info = GetPatchInfo(method); if (info == null) { return; } PatchFunctions.UpdateRecompiledMethod(method, codeStart, info); }; }
static void OnCompileMethod(MethodBase method, IntPtr codeStart, ulong codeLen) { if (method == null) { return; } var info = GetPatchInfo(method); if (info == null) { return; } PatchFunctions.UpdateRecompiledMethod(method, codeStart, info); }