private void Save(dnpatch.Patcher patcher) { try { patcher.Save(true); } catch (Exception ex) { _writer.WriteLine("Error while saving patched game assembly: " + ex); throw; } }
private void Patch(dnpatch.Patcher patcher, Target target) { try { patcher.Patch(target); } catch (Exception ex) { _writer.WriteLine("Error while patching: " + ex); throw; } }
public void PatchGame() { var patcher = new dnpatch.Patcher($"{_config.ManagedPath}/Assembly-CSharp.dll"); var target = new Target { Class = "PermanentManager", Method = "Awake" }; var newInstructions = new[] { Instruction.Create(OpCodes.Call, patcher.BuildCall(typeof(ModLoader.ModLoader), "LoadMods", typeof(void), new Type[] { })), Instruction.Create(OpCodes.Ret) }; var instructions = patcher.GetInstructions(target).ToList(); if (IsPatched(instructions)) { ReplacePatchedInstructions(instructions, newInstructions); } else { AddNewInstructions(instructions, newInstructions); } target.Instructions = instructions.ToArray(); try { patcher.Patch(target); } catch (Exception ex) { Console.WriteLine("Error while patching game assembly: " + ex); throw; } try { patcher.Save(true); } catch (Exception ex) { Console.WriteLine("Error while saving patched game assembly: " + ex); throw; } }
private void PatchAssembly() { var patcher = new dnpatch.Patcher($"{_owmlConfig.ManagedPath}/Assembly-CSharp.dll"); var target = new Target { Class = PatchClass, Method = PatchMethod }; var instructions = patcher.GetInstructions(target).ToList(); var patchedInstructions = GetPatchedInstructions(instructions); if (patchedInstructions.Count == 1) { _writer.WriteLine($"{PatchClass}.{PatchMethod} is already patched."); return; } if (patchedInstructions.Count > 1) { _writer.WriteLine($"Removing corrupted patch from {PatchClass}.{PatchMethod}."); foreach (var patchedInstruction in patchedInstructions) { instructions.Remove(patchedInstruction); } } _writer.WriteLine($"Adding patch in {PatchClass}.{PatchMethod}."); var newInstruction = Instruction.Create(OpCodes.Call, patcher.BuildCall(typeof(ModLoader.ModLoader), "LoadMods", typeof(void), new Type[] { })); instructions.Insert(instructions.Count - 1, newInstruction); target.Instructions = instructions.ToArray(); Patch(patcher, target); Save(patcher); }