Skip to content

Commit

Permalink
v1.0.5 release
Browse files Browse the repository at this point in the history
Added IL code processing
Hopefully fixed 'dynamic link library initialization routine failed'
Refactored a few things
  • Loading branch information
pardeike committed Feb 4, 2017
1 parent dc02976 commit 39b990f
Show file tree
Hide file tree
Showing 18 changed files with 425 additions and 439 deletions.
12 changes: 9 additions & 3 deletions Harmony/Harmony.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,14 @@
<PropertyGroup>
<SignAssembly>false</SignAssembly>
</PropertyGroup>
<PropertyGroup>
<Win32Resource>
</Win32Resource>
</PropertyGroup>
<ItemGroup>
<Compile Include="HarmonyModifier.cs" />
<Compile Include="HarmonyProcessor.cs" />
<Compile Include="HarmonySharedState.cs" />
<Compile Include="ILCopying\ILProcessor.cs" />
<Compile Include="Tools\AccessCache.cs" />
<Compile Include="Tools\AccessTools.cs" />
<Compile Include="Attributes.cs" />
Expand All @@ -59,13 +64,14 @@
<Compile Include="Extras\MethodInvoker.cs" />
<Compile Include="MethodPatcher.cs" />
<Compile Include="Patch.cs" />
<Compile Include="Patcher.cs" />
<Compile Include="PatchProcessor.cs" />
<Compile Include="PatchFunctions.cs" />
<Compile Include="Tools\FileLog.cs" />
<Compile Include="Tools\PatchTools.cs" />
<Compile Include="Priority.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Tools\DynamicTools.cs" />
<Compile Include="Traverse.cs" />
<Compile Include="Tools\Traverse.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="System" />
Expand Down
33 changes: 20 additions & 13 deletions Harmony/HarmonyInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ public ReadOnlyCollection<string> Owners
}
}

public Patches(Patch[] prefixes, Patch[] postfixes)
public Patches(List<Patch> prefixes, List<Patch> postfixes)
{
if (prefixes == null) prefixes = new Patch[0];
if (postfixes == null) postfixes = new Patch[0];
if (prefixes == null) prefixes = new List<Patch>();
if (postfixes == null) postfixes = new List<Patch>();

Prefixes = prefixes.ToList().AsReadOnly();
Postfixes = postfixes.ToList().AsReadOnly();
Prefixes = prefixes.AsReadOnly();
Postfixes = postfixes.AsReadOnly();
}
}

Expand All @@ -37,12 +37,9 @@ public class HarmonyInstance
readonly string id;
public string Id => id;

readonly Patcher patcher;

HarmonyInstance(string id)
{
this.id = id;
patcher = new Patcher(this);
}

public static HarmonyInstance Create(string id)
Expand All @@ -51,19 +48,29 @@ public static HarmonyInstance Create(string id)
return new HarmonyInstance(id);
}

public void PatchAll(Module module)
public void PatchAll(Assembly assembly)
{
patcher.PatchAll(module);
assembly.GetTypes().Do(type =>
{
var parentMethodInfos = type.GetHarmonyMethods();
if (parentMethodInfos != null && parentMethodInfos.Count() > 0)
{
var info = HarmonyMethod.Merge(parentMethodInfos);
var processor = new PatchProcessor(this, type, info);
processor.Patch();
}
});
}

public void Patch(MethodBase original, HarmonyMethod prefix, HarmonyMethod postfix)
public void Patch(MethodBase original, HarmonyMethod prefix, HarmonyMethod postfix, HarmonyProcessor infix = null)
{
patcher.Patch(original, prefix, postfix);
var processor = new PatchProcessor(this, original, prefix, postfix, infix);
processor.Patch();
}

public Patches IsPatched(MethodBase method)
{
return patcher.IsPatched(method);
return PatchProcessor.IsPatched(method);
}
}
}
39 changes: 0 additions & 39 deletions Harmony/HarmonyModifier.cs

This file was deleted.

27 changes: 27 additions & 0 deletions Harmony/HarmonyProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Harmony.ILCopying;
using System.Collections.Generic;

namespace Harmony
{
public class HarmonyProcessor
{
public List<IILProcessor> processors;

public readonly int priority;
public readonly string[] before;
public readonly string[] after;

public HarmonyProcessor(int priority, string[] before, string[] after)
{
this.priority = priority;
this.before = before;
this.after = after;
processors = new List<IILProcessor>();
}

public void AddILProcessor(IILProcessor processor)
{
processors.Add(processor);
}
}
}
24 changes: 16 additions & 8 deletions Harmony/HarmonySharedState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ namespace Harmony
public static class HarmonySharedState
{
static readonly string name = "HarmonySharedState";
public static readonly int internalVersion = 100;
public static int actualVersion = -1;

static Dictionary<object, byte[]> GetState()
static Dictionary<MethodBase, byte[]> GetState()
{
lock (name)
{
Expand All @@ -23,18 +25,24 @@ static Dictionary<object, byte[]> GetState()
var moduleBuilder = assemblyBuilder.DefineDynamicModule(name);
var typeAttributes = TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Sealed | TypeAttributes.Abstract;
var typeBuilder = moduleBuilder.DefineType(name, typeAttributes);
typeBuilder.DefineField(name, typeof(Dictionary<object, byte[]>), FieldAttributes.Static | FieldAttributes.Public);
typeBuilder.DefineField("state", typeof(Dictionary<MethodBase, byte[]>), FieldAttributes.Static | FieldAttributes.Public);
typeBuilder.DefineField("version", typeof(int), FieldAttributes.Static | FieldAttributes.Public).SetConstant(internalVersion);
typeBuilder.CreateType();

assembly = AppDomain.CurrentDomain.GetAssemblies()
.Where(a => a.GetName().Name == name)
.FirstOrDefault();
if (assembly == null) throw new Exception("Cannot find or create harmony shared state");
}
var field = assembly.GetType(name).GetField(name);
if (field == null) throw new Exception("Cannot find harmony shared state field");
if (field.GetValue(null) == null) field.SetValue(null, new Dictionary<object, byte[]>());
return (Dictionary<object, byte[]>)field.GetValue(null);

var versionField = assembly.GetType(name).GetField("version");
if (versionField == null) throw new Exception("Cannot find harmony state version field");
actualVersion = (int)versionField.GetValue(null);

var stateField = assembly.GetType(name).GetField("state");
if (versionField == null) throw new Exception("Cannot find harmony state field");
if (stateField.GetValue(null) == null) stateField.SetValue(null, new Dictionary<MethodBase, byte[]>());
return (Dictionary<MethodBase, byte[]>)stateField.GetValue(null);
}
}

Expand All @@ -45,9 +53,9 @@ public static PatchInfo GetPatchInfo(MethodBase method)
return PatchInfoSerialization.Deserialize(bytes);
}

internal static void SetPatchInfo(MethodBase method, PatchInfo info)
public static void UpdatePatchInfo(MethodBase method, PatchInfo patchInfo)
{
GetState()[method] = PatchInfoSerialization.Serialize(info);
GetState()[method] = PatchInfoSerialization.Serialize(patchInfo);
}
}
}
123 changes: 11 additions & 112 deletions Harmony/ILCopying/ILInstruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,129 +3,28 @@

namespace Harmony.ILCopying
{
public class ILCode
{
readonly OpCode opcode;
readonly bool hasOpcode;
readonly object operand;
readonly bool hasOperand;

public ILCode(OpCode opcode)
{
this.opcode = opcode;
hasOpcode = true;
}

public ILCode(object operand)
{
this.operand = operand;
hasOperand = true;
}

public ILCode(OpCode opcode, object operand)
{
this.opcode = opcode;
hasOpcode = true;
this.operand = operand;
hasOperand = true;
}

public ILCode(ILInstruction instruction)
{
opcode = instruction.OpCode;
hasOpcode = true;
operand = instruction.Operand;
hasOperand = true;
}

public void Apply(ILInstruction instruction)
{
if (hasOpcode) instruction.OpCode = opcode;
if (hasOperand)
{
instruction.Operand = operand;
instruction.Argument = operand;
}
}

public bool Equals(ILCode other)
{
var result = (!hasOpcode || !other.hasOpcode || opcode == other.opcode) && (!hasOperand || !other.hasOperand || operand == other.operand);
//if (result) FileLog.Log("# " + this.ToString() + " == " + other.ToString() + " is " + result);
return result;
}

public override string ToString()
{
return "" + (hasOpcode ? opcode.Name : "*") + "_" + (hasOperand ? (operand == null ? "NULL" : operand.GetType().Name) : "*");
}
}

public class ILInstruction
{
int offset;
OpCode opcode;
object operand;
object argument;

ILInstruction previous;
ILInstruction next;

List<Label> labels;

public int Offset
{
get { return offset; }
set { offset = value; }
}

public OpCode OpCode
{
get { return opcode; }
set { opcode = value; }
}

public object Operand
{
get { return operand; }
set { operand = value; }
}

public object Argument
{
get { return argument; }
set { argument = value; }
}

public List<Label> Labels
{
get { return labels; }
}

public ILInstruction Previous
{
get { return previous; }
set { previous = value; }
}

public ILInstruction Next
{
get { return next; }
set { next = value; }
}
public int offset;
public OpCode opcode;
public object operand;
public object argument;

//
public List<Label> labels = new List<Label>();

public ILInstruction(OpCode opcode, object operand = null)
{
this.opcode = opcode;
this.operand = operand;
labels = new List<Label>();
}

public void AddLabel(Label label)
public ILInstruction Copy()
{
labels.Add(label);
var instr = new ILInstruction(opcode, operand);
instr.offset = offset;
instr.argument = argument;
instr.labels = labels;
return instr;
}

public int GetSize()
Expand Down
Loading

0 comments on commit 39b990f

Please sign in to comment.