Skip to content

Commit

Permalink
Fixed bug when prefixes try to assign __result and return false
Browse files Browse the repository at this point in the history
Fixed early bail out of parameter loop in call parameter method
Remove debug calls
Cleaned up code
  • Loading branch information
pardeike committed Feb 12, 2017
1 parent 0a571a6 commit e23cf68
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
20 changes: 11 additions & 9 deletions Harmony/MethodPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static DynamicMethod CreatePatchedMethod(MethodBase original, List<Method
var patch = DynamicTools.CreateDynamicMethod(original, "_Patch" + idx);
var il = patch.GetILGenerator();
var originalVariables = DynamicTools.DeclareLocalVariables(original, il);
var resultVariable = DynamicTools.DeclareReturnVar(il, original);
var resultVariable = DynamicTools.DeclareReturnVar(original, il);

// TODO: this is broken because it has only one var for all patches together.
// to make this useful, we need one var for each patch pair
Expand All @@ -61,17 +61,19 @@ public static DynamicMethod CreatePatchedMethod(MethodBase original, List<Method
privateVars[RESULT_VAR] = resultVariable;
privateVars[STATE_VAR] = privateStateVariable;

var afterOriginal = il.DefineLabel();
AddPrefixes(il, original, prefixes, privateVars, afterOriginal);
var afterOriginal1 = il.DefineLabel();
var afterOriginal2 = il.DefineLabel();
AddPrefixes(il, original, prefixes, privateVars, afterOriginal2);

var copier = new MethodCopier(original, patch, originalVariables);
foreach (var processor in processors)
copier.AddReplacement(processor);
copier.AddReplacement(new RetToBrAfterProcessor(afterOriginal));
copier.AddReplacement(new RetToBrAfterProcessor(afterOriginal1));
copier.Emit();
il.MarkLabel(afterOriginal);
il.MarkLabel(afterOriginal1);
if (resultVariable != null)
il.Emit(OpCodes.Stloc, resultVariable);
il.MarkLabel(afterOriginal2);

AddPostfixes(il, original, postfixes, privateVars);

Expand Down Expand Up @@ -128,7 +130,7 @@ static void EmitCallParameter(ILGenerator il, MethodBase original, MethodInfo pa
if (patchParam.Name == STATE_VAR)
{
var ldlocCode = patchParam.ParameterType.IsByRef ? OpCodes.Ldloca : OpCodes.Ldloc;
il.Emit(ldlocCode, variables.GetValueSafe(STATE_VAR));
il.Emit(ldlocCode, variables[STATE_VAR]);
continue;
}

Expand All @@ -137,7 +139,7 @@ static void EmitCallParameter(ILGenerator il, MethodBase original, MethodInfo pa
if (AccessTools.GetReturnedType(original) == typeof(void))
throw new Exception("Cannot get result from void method " + original);
var ldlocCode = patchParam.ParameterType.IsByRef ? OpCodes.Ldloca : OpCodes.Ldloc;
il.Emit(ldlocCode, variables.GetValueSafe(RESULT_VAR));
il.Emit(ldlocCode, variables[RESULT_VAR]);
continue;
}

Expand All @@ -159,14 +161,14 @@ static void EmitCallParameter(ILGenerator il, MethodBase original, MethodInfo pa
if (originalIsNormal == patchIsNormal)
{
il.Emit(OpCodes.Ldarg, patchArgIndex);
return;
continue;
}

// Case 2
if (originalIsNormal && patchIsNormal == false)
{
il.Emit(OpCodes.Ldarga, patchArgIndex);
return;
continue;
}

// Case 3
Expand Down
6 changes: 0 additions & 6 deletions Harmony/PatchFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,8 @@ public static void UpdateWrapper(MethodBase original, PatchInfo patchInfo)
var replacement = MethodPatcher.CreatePatchedMethod(original, sortedPrefixes, sortedPostfixes, sortedProcessors);
if (replacement == null) throw new MissingMethodException("Cannot create dynamic replacement for " + original);

FileLog.Log("Getting code start for " + original);
var originalCodeStart = Memory.GetMethodStart(original);
FileLog.Log("=> " + originalCodeStart);

FileLog.Log("Getting code start for " + original);
var patchCodeStart = Memory.GetMethodStart(replacement);
FileLog.Log("=> " + patchCodeStart);

Memory.WriteJump(originalCodeStart, patchCodeStart);

PatchTools.RememberObject(original, replacement); // no gc for new value + release old value to gc
Expand Down
2 changes: 1 addition & 1 deletion Harmony/Tools/DynamicTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static LocalBuilder[] DeclareLocalVariables(MethodBase original, ILGenera
).ToArray();
}

public static LocalBuilder DeclareReturnVar(ILGenerator il, MethodBase original)
public static LocalBuilder DeclareReturnVar(MethodBase original, ILGenerator il)
{
var type = AccessTools.GetReturnedType(original);
if (AccessTools.isClass(type))
Expand Down

0 comments on commit e23cf68

Please sign in to comment.