public void HotPatch(string assemblyFilename) { Debug.LogFormat("Started hotpatching {0}", assemblyFilename); var newAssembly = Cecil.AssemblyDefinition.ReadAssembly(assemblyFilename); foreach (var method in IterateMethods(newAssembly)) { int hashcode; if (methodHashes.TryGetValue(method.FullName, out hashcode)) { if (hashcode == GenBodyHashcode(method)) { continue; } string oldBody = methodBodies[method.FullName]; string newBody = ConcatBody(method); Debug.Log("Trying to hotpatch " + method.FullName); var ourType = recompiler.FindType(method.DeclaringType); if (ourType == null) { continue; } var patchField = ourType.GetField(method.GetHotpatchFieldName(), BindingFlags.Static | BindingFlags.NonPublic); if (patchField == null) { Debug.LogWarningFormat("Cannot patch {0} - function is not hotpatchable", method.FullName); continue; } Debug.Trace("Hotswapping {0}", method.FullName); Debug.Trace("Method signature was: {0}, is: {1}", methodHashes[method.FullName], GenBodyHashcode(method)); Debug.Trace("Method body was:"); Debug.Trace(methodBodies[method.FullName]); Debug.Trace("Method body is:"); Debug.Trace(ConcatBody(method)); try { var dynmethod = recompiler.RecompileMethod(method); if (dynmethod != null) { patchField.SetValue(null, dynmethod); methodHashes[method.FullName] = GenBodyHashcode(method); methodBodies[method.FullName] = ConcatBody(method); } } catch (Exception e) { Debug.LogWarningFormat("Failed to patch {0} - {1}. See full stacktrace in Temp/hotpatch.log.", method.FullName, e.Message); Debug.Trace(e.StackTrace); } } } }
public void LoadLocalAssembly(string assemblyFilename) { var currentAssembly = Cecil.AssemblyDefinition.ReadAssembly(assemblyFilename); foreach (var method in IterateMethods(currentAssembly)) { var lm = new LocalMethod(); lm.CurrentMethodDef = method; lm.CurrentSwap = null; lm.BodyString = ConcatBody(method); lm.BodyHashCode = lm.BodyString.GetHashCode(); try { var localType = recompiler.FindType(method.DeclaringType); lm.Type = localType; if (localType != null) { lm.Method = recompiler.FindMethod(localType, method); } localMethods[method.FullName] = lm; Debug.Trace($"Loading method: {method.FullName}"); } catch (Exception e) { Debug.Trace($"Failed to load method: {method.FullName}"); } } }