Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/pardeike/Harmony
Browse files Browse the repository at this point in the history
  • Loading branch information
pardeike committed Aug 6, 2018
2 parents 6cbd1ea + 64f84a3 commit 8d9e0f2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
33 changes: 28 additions & 5 deletions Harmony/MethodPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public static class MethodPatcher
public static string ORIGINAL_METHOD_PARAM = "__originalMethod";
public static string RESULT_VAR = "__result";
public static string STATE_VAR = "__state";
public static string PARAM_INDEX_PREFIX = "__";
public static string INSTANCE_FIELD_PREFIX = "___";

// in case of trouble, set to true to write dynamic method to desktop as a dll
Expand Down Expand Up @@ -273,9 +274,19 @@ static void EmitCallParameter(ILGenerator il, MethodBase original, MethodInfo pa
if (patchParam.Name.StartsWith(INSTANCE_FIELD_PREFIX))
{
var fieldName = patchParam.Name.Substring(INSTANCE_FIELD_PREFIX.Length);
var fieldInfo = AccessTools.Field(original.DeclaringType, fieldName);
if (fieldInfo == null)
throw new ArgumentException("No such field defined in class " + original.DeclaringType.FullName, fieldName);
FieldInfo fieldInfo;
if (fieldName.All(char.IsDigit))
{
fieldInfo = AccessTools.Field(original.DeclaringType, int.Parse(fieldName));
if (fieldInfo == null)
throw new ArgumentException("No field found at given index in class " + original.DeclaringType.FullName, fieldName);
}
else
{
fieldInfo = AccessTools.Field(original.DeclaringType, fieldName);
if (fieldInfo == null)
throw new ArgumentException("No such field defined in class " + original.DeclaringType.FullName, fieldName);
}

if (fieldInfo.IsStatic)
{
Expand Down Expand Up @@ -316,8 +327,20 @@ static void EmitCallParameter(ILGenerator il, MethodBase original, MethodInfo pa
continue;
}

var idx = GetArgumentIndex(patch, originalParameterNames, patchParam);
if (idx == -1) throw new Exception("Parameter \"" + patchParam.Name + "\" not found in method " + original.FullDescription());
int idx;
if (patchParam.Name.StartsWith(PARAM_INDEX_PREFIX))
{
var val = patchParam.Name.Substring(PARAM_INDEX_PREFIX.Length);
if (!int.TryParse(val, out idx))
throw new Exception("Parameter " + patchParam.Name + " does not contain a valid index");
if (idx < 0 || idx >= originalParameters.Length)
throw new Exception("No parameter found at index " + idx);
}
else
{
idx = GetArgumentIndex(patch, originalParameterNames, patchParam);
if (idx == -1) throw new Exception("Parameter \"" + patchParam.Name + "\" not found in method " + original.FullDescription());
}

// original -> patch opcode
// --------------------------------------
Expand Down
5 changes: 5 additions & 0 deletions Harmony/Tools/AccessTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public static FieldInfo Field(Type type, string name)
return FindIncludingBaseTypes(type, t => t.GetField(name, all));
}

public static FieldInfo Field(Type type, int idx)
{
return GetDeclaredFields(type).ElementAtOrDefault(idx);
}

public static PropertyInfo DeclaredProperty(Type type, string name)
{
if (type == null || name == null) return null;
Expand Down

0 comments on commit 8d9e0f2

Please sign in to comment.