Skip to content

Commit

Permalink
Update 1.0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
SniperGER committed Jul 16, 2023
1 parent bfa8789 commit 7bc2c79
Show file tree
Hide file tree
Showing 24 changed files with 606 additions and 121 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## 1.0.1.1
- Added patch pre-comparison to prevent accidentally patching assemblies multiple times
- Added file hash verification to patches
- Added command line parameters for use in a "silent" environment (eg. installers)
- Added a proper icon
- Fixed some instructions throwing errors when applied to an assembly
- Improved assembly directory selection
- Improved error handling when loading invalid patches

## 1.0.0.1
- Initial release
19 changes: 19 additions & 0 deletions Instructions/Call.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Mono.Cecil;
using Mono.Cecil.Cil;
using System;
using System.Linq;

namespace UniversalUnityPatcher.Instructions {
Expand Down Expand Up @@ -42,5 +43,23 @@ public override Instruction ParseInstruction(ILProcessor processor, AssemblyDefi
// Return Instruction
return processor.Create(OpCodes.Call, methodReference);
}

public override bool CompareInstruction(Instruction a, Instruction b) {
if (!a.OpCode.Equals(b.OpCode)) return false;
if (!a.Operand.GetType().Equals(b.Operand.GetType())) return false;

if (a.Operand is MethodReference && b.Operand is MethodReference) {
var _a = (MethodReference)a.Operand;
var _b = (MethodReference)b.Operand;

return _a.FullName == _b.FullName &&
_a.DeclaringType.FullName == _b.DeclaringType.FullName &&
_a.ReturnType.FullName == _b.ReturnType.FullName &&
_a.Parameters.All(aParam => _b.Parameters.Any(bParam => aParam.Name == bParam.Name && aParam.ParameterType.FullName == bParam.ParameterType.FullName)) &&
_a.GenericParameters.All(aParam => _b.GenericParameters.Any(bParam => aParam.Name == bParam.Name));
}

return false;
}
}
}
18 changes: 18 additions & 0 deletions Instructions/Callvirt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,23 @@ public override Instruction ParseInstruction(ILProcessor processor, AssemblyDefi
// Return Instruction
return processor.Create(OpCodes.Callvirt, methodReference);
}

public override bool CompareInstruction(Instruction a, Instruction b) {
if (!a.OpCode.Equals(b.OpCode)) return false;
if (!a.Operand.GetType().Equals(b.Operand.GetType())) return false;

if (a.Operand is MethodReference && b.Operand is MethodReference) {
var _a = (MethodReference)a.Operand;
var _b = (MethodReference)b.Operand;

return _a.FullName == _b.FullName &&
_a.DeclaringType.FullName == _b.DeclaringType.FullName &&
_a.ReturnType.FullName == _b.ReturnType.FullName &&
_a.Parameters.All(aParam => _b.Parameters.Any(bParam => aParam.Name == bParam.Name && aParam.ParameterType.FullName == bParam.ParameterType.FullName)) &&
_a.GenericParameters.All(aParam => _b.GenericParameters.Any(bParam => aParam.Name == bParam.Name));
}

return false;
}
}
}
4 changes: 4 additions & 0 deletions Instructions/InstructionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@ public abstract class InstructionBase {
public virtual Instruction ParseInstruction(ILProcessor processor, AssemblyDefinition assemblyDef, TypeDefinition typeDef, MethodDefinition methodDef, PatchInstruction patchInstruction) {
throw new NotImplementedException();
}

public virtual bool CompareInstruction(Instruction a, Instruction b) {
return false;
}
}
}
63 changes: 59 additions & 4 deletions Instructions/Jmp.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Mono.Cecil;
using Mono.Cecil.Cil;
using System;
using System.Linq;

namespace UniversalUnityPatcher.Instructions {
Expand All @@ -8,15 +9,69 @@ namespace UniversalUnityPatcher.Instructions {
/// </summary>
public class Jmp : InstructionBase {
public override Instruction ParseInstruction(ILProcessor processor, AssemblyDefinition assemblyDef, TypeDefinition typeDef, MethodDefinition methodDef, PatchInstruction patchInstruction) {
if (patchInstruction.TargetIndex < 0)
//if (patchInstruction.TargetIndex < 0)
// return null;

//Instruction targetInstruction = methodDef.Body.Instructions.ElementAt(patchInstruction.TargetIndex);

//if (targetInstruction == null)
// return null;

//Console.WriteLine(targetInstruction);
//Console.WriteLine(OpCodes.Jmp.OperandType);
//return processor.Create(OpCodes.Jmp, targetInstruction);

if (patchInstruction.Type == null || patchInstruction.Method == null)
return null;

Instruction targetInstruction = methodDef.Body.Instructions.ElementAt(patchInstruction.TargetIndex);
string typeName = patchInstruction.Type;
string methodName = patchInstruction.Method;

if (targetInstruction == null)
AssemblyDefinition _assemblyDef = assemblyDef;
if (patchInstruction.Assembly != null)
_assemblyDef = Patcher.ResolveAssembly(patchInstruction.Assembly);

if (_assemblyDef == null)
return null;

return processor.Create(OpCodes.Jmp, targetInstruction);
// Get Type
TypeDefinition typeDefinition = _assemblyDef.MainModule.GetType(typeName);

// Get Method
MethodDefinition methodDefinition = typeDefinition.Methods.FirstOrDefault(m => m.Name == methodName);
MethodReference methodReference = assemblyDef.MainModule.ImportReference(methodDefinition);

//if (patchInstruction.Parameters.Count > 0) {
// List<TypeReference> genericParameters = new List<TypeReference>();

// foreach (PatchInstructionParameter genericAssembly in patchInstruction.Parameters) {
// AssemblyDefinition genericAssemblyDef = Patcher.ResolveAssembly(genericAssembly.Assembly);
// TypeReference genericTypeRef = assemblyDef.MainModule.ImportReference(genericAssemblyDef.MainModule.GetType(genericAssembly.Type));

// methodReference.GenericParameters.Add(new GenericParameter(genericTypeRef.Name, methodDefinition));
// }
//}

// Return Instruction
return processor.Create(OpCodes.Jmp, methodReference);
}

public override bool CompareInstruction(Instruction a, Instruction b) {
if (!a.OpCode.Equals(b.OpCode)) return false;
if (!a.Operand.GetType().Equals(b.Operand.GetType())) return false;

if (a.Operand is MethodReference && b.Operand is MethodReference) {
var _a = (MethodReference)a.Operand;
var _b = (MethodReference)b.Operand;

return _a.FullName == _b.FullName &&
_a.DeclaringType.FullName == _b.DeclaringType.FullName &&
_a.ReturnType.FullName == _b.ReturnType.FullName &&
_a.Parameters.All(aParam => _b.Parameters.Any(bParam => aParam.Name == bParam.Name && aParam.ParameterType.FullName == bParam.ParameterType.FullName)) &&
_a.GenericParameters.All(aParam => _b.GenericParameters.Any(bParam => aParam.Name == bParam.Name));
}

return false;
}
}
}
16 changes: 16 additions & 0 deletions Instructions/Ldfld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,21 @@ public override Instruction ParseInstruction(ILProcessor processor, AssemblyDefi

return processor.Create(OpCodes.Ldfld, fieldDefinition);
}

public override bool CompareInstruction(Instruction a, Instruction b) {
if (!a.OpCode.Equals(b.OpCode)) return false;
if (!a.Operand.GetType().Equals(b.Operand.GetType())) return false;

if (a.Operand is FieldReference && b.Operand is FieldReference) {
var _a = (FieldReference)a.Operand;
var _b = (FieldReference)b.Operand;

return _a.FullName == _b.FullName &&
_a.DeclaringType.FullName == _b.DeclaringType.FullName &&
_a.FieldType.FullName == _b.FieldType.FullName;
}

return false;
}
}
}
18 changes: 18 additions & 0 deletions Instructions/Ldftn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,23 @@ public override Instruction ParseInstruction(ILProcessor processor, AssemblyDefi

return processor.Create(OpCodes.Ldftn, methodReference);
}

public override bool CompareInstruction(Instruction a, Instruction b) {
if (!a.OpCode.Equals(b.OpCode)) return false;
if (!a.Operand.GetType().Equals(b.Operand.GetType())) return false;

if (a.Operand is MethodReference && b.Operand is MethodReference) {
var _a = (MethodReference)a.Operand;
var _b = (MethodReference)b.Operand;

return _a.FullName == _b.FullName &&
_a.DeclaringType.FullName == _b.DeclaringType.FullName &&
_a.ReturnType.FullName == _b.ReturnType.FullName &&
_a.Parameters.All(aParam => _b.Parameters.Any(bParam => aParam.Name == bParam.Name && aParam.ParameterType.FullName == bParam.ParameterType.FullName)) &&
_a.GenericParameters.All(aParam => _b.GenericParameters.Any(bParam => aParam.Name == bParam.Name));
}

return false;
}
}
}
16 changes: 16 additions & 0 deletions Instructions/Ldsfld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,21 @@ public override Instruction ParseInstruction(ILProcessor processor, AssemblyDefi

return processor.Create(OpCodes.Ldsfld, fieldDefinition);
}

public override bool CompareInstruction(Instruction a, Instruction b) {
if (!a.OpCode.Equals(b.OpCode)) return false;
if (!a.Operand.GetType().Equals(b.Operand.GetType())) return false;

if (a.Operand is FieldReference && b.Operand is FieldReference) {
var _a = (FieldReference)a.Operand;
var _b = (FieldReference)b.Operand;

return _a.FullName == _b.FullName &&
_a.DeclaringType.FullName == _b.DeclaringType.FullName &&
_a.FieldType.FullName == _b.FieldType.FullName;
}

return false;
}
}
}
7 changes: 7 additions & 0 deletions Instructions/Ldstr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,12 @@ public override Instruction ParseInstruction(ILProcessor processor, AssemblyDefi

return processor.Create(OpCodes.Ldstr, patchInstruction.Value);
}

public override bool CompareInstruction(Instruction a, Instruction b) {
if (!a.OpCode.Equals(b.OpCode)) return false;
if (!a.Operand.GetType().Equals(b.Operand.GetType())) return false;

return true;
}
}
}
27 changes: 21 additions & 6 deletions Instructions/Newobj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Mono.Cecil.Rocks;
using System;
using System.Linq;
using System.Reflection;

namespace UniversalUnityPatcher.Instructions {
/// <summary>
Expand All @@ -24,14 +25,28 @@ public override Instruction ParseInstruction(ILProcessor processor, AssemblyDefi

TypeDefinition typeDefinition = _assemblyDef.MainModule.GetType(typeName);

var genericAssembly = patchInstruction.Parameters.First();
AssemblyDefinition genericAssemblyDef = Patcher.ResolveAssembly(genericAssembly.Assembly);
TypeReference genericTypeRef = assemblyDef.MainModule.ImportReference(genericAssemblyDef.MainModule.GetType(genericAssembly.Type));
MethodDefinition methodDefinition = typeDefinition.GetConstructors().FirstOrDefault();
MethodReference methodReference = assemblyDef.MainModule.ImportReference(methodDefinition);

GenericInstanceType genericInstanceType = assemblyDef.MainModule.ImportReference(Type.GetType(patchInstruction.Type)).MakeGenericInstanceType(genericTypeRef);
MethodReference genericInstanceCtor = Patcher.MakeHostInstanceGeneric(assemblyDef.MainModule.ImportReference(genericInstanceType.Resolve().Methods.First(_ => _.Name == patchInstruction.Method)), genericTypeRef);
return processor.Create(OpCodes.Newobj, methodReference);
}

public override bool CompareInstruction(Instruction a, Instruction b) {
if (!a.OpCode.Equals(b.OpCode)) return false;
if (!a.Operand.GetType().Equals(b.Operand.GetType())) return false;

if (a.Operand is MethodReference && b.Operand is MethodReference) {
var _a = (MethodReference)a.Operand;
var _b = (MethodReference)b.Operand;

return _a.FullName == _b.FullName &&
_a.DeclaringType.FullName == _b.DeclaringType.FullName &&
_a.ReturnType.FullName == _b.ReturnType.FullName &&
_a.Parameters.All(aParam => _b.Parameters.Any(bParam => aParam.Name == bParam.Name && aParam.ParameterType.FullName == bParam.ParameterType.FullName)) &&
_a.GenericParameters.All(aParam => _b.GenericParameters.Any(bParam => aParam.Name == bParam.Name));
}

return processor.Create(OpCodes.Newobj, genericInstanceCtor);
return false;
}
}
}
16 changes: 16 additions & 0 deletions Instructions/Stfld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,21 @@ public override Instruction ParseInstruction(ILProcessor processor, AssemblyDefi

return processor.Create(OpCodes.Stfld, fieldDefinition);
}

public override bool CompareInstruction(Instruction a, Instruction b) {
if (!a.OpCode.Equals(b.OpCode)) return false;
if (!a.Operand.GetType().Equals(b.Operand.GetType())) return false;

if (a.Operand is FieldReference && b.Operand is FieldReference) {
var _a = (FieldReference)a.Operand;
var _b = (FieldReference)b.Operand;

return _a.FullName == _b.FullName &&
_a.DeclaringType.FullName == _b.DeclaringType.FullName &&
_a.FieldType.FullName == _b.FieldType.FullName;
}

return false;
}
}
}
16 changes: 16 additions & 0 deletions Instructions/Stsfld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,21 @@ public override Instruction ParseInstruction(ILProcessor processor, AssemblyDefi

return processor.Create(OpCodes.Stsfld, assemblyDef.MainModule.ImportReference(fieldDefinition));
}

public override bool CompareInstruction(Instruction a, Instruction b) {
if (!a.OpCode.Equals(b.OpCode)) return false;
if (!a.Operand.GetType().Equals(b.Operand.GetType())) return false;

if (a.Operand is FieldReference && b.Operand is FieldReference) {
var _a = (FieldReference)a.Operand;
var _b = (FieldReference)b.Operand;

return _a.FullName == _b.FullName &&
_a.DeclaringType.FullName == _b.DeclaringType.FullName &&
_a.FieldType.FullName == _b.FieldType.FullName;
}

return false;
}
}
}
Loading

0 comments on commit 7bc2c79

Please sign in to comment.