Skip to content

Commit

Permalink
Made MethodType optional and introduce default value in final calcula…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
pardeike committed Jul 30, 2018
1 parent bf3ebb4 commit bb8bb87
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 42 deletions.
106 changes: 67 additions & 39 deletions Harmony/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,103 +49,131 @@ public HarmonyPatch()
{
}

// simple types (designed to be combined with each other)
// ======================================================
// starting with 'Type'

public HarmonyPatch(Type declaringType)
{
info.declaringType = declaringType;
}

public HarmonyPatch(string methodName, MethodType methodType = MethodType.Normal)
{
info.methodName = methodName;
info.methodType = methodType;
}

// new in v1.1.1
public HarmonyPatch(MethodType methodType = MethodType.Normal)
{
info.methodType = methodType;
}

// added params in v1.1.1
public HarmonyPatch(params Type[] argumentTypes)
public HarmonyPatch(Type declaringType, Type[] argumentTypes)
{
info.declaringType = declaringType;
info.argumentTypes = argumentTypes;
}

// new in v1.1.1
public HarmonyPatch(Type[] argumentTypes, ArgumentType[] argumentVariations)
public HarmonyPatch(Type declaringType, string methodName)
{
ParseSpecialArguments(argumentTypes, argumentVariations);
info.declaringType = declaringType;
info.methodName = methodName;
}

// combined types - methods
// ========================

// added params in v1.1.1 (simple)
public HarmonyPatch(Type declaringType, string methodName, params Type[] argumentTypes)
{
info.declaringType = declaringType;
info.methodName = methodName;
info.argumentTypes = argumentTypes;
}

// new in v1.1.1 (complex)
public HarmonyPatch(Type declaringType, string methodName, Type[] argumentTypes, ArgumentType[] argumentVariations)
{
info.declaringType = declaringType;
info.methodName = methodName;
ParseSpecialArguments(argumentTypes, argumentVariations);
}

// combined types - constructors
// =============================
public HarmonyPatch(Type declaringType, MethodType methodType)
{
info.declaringType = declaringType;
info.methodType = methodType;
}

// new in v1.1.1 (simple)
public HarmonyPatch(Type declaringType, MethodType methodType, params Type[] argumentTypes)
{
info.declaringType = declaringType;
info.methodType = methodType;
info.argumentTypes = argumentTypes;
}

// new in v1.1.1 (complex)
public HarmonyPatch(Type declaringType, MethodType methodType, Type[] argumentTypes, ArgumentType[] argumentVariations)
{
info.declaringType = declaringType;
info.methodType = methodType;
ParseSpecialArguments(argumentTypes, argumentVariations);
}

// combined types - properties
// ===========================

// new in v1.1.1
public HarmonyPatch(Type declaringType, string propertyName, MethodType methodType)
{
info.declaringType = declaringType;
info.methodName = propertyName;
info.methodType = methodType;
}

//
// starting with 'string'

[Obsolete("This attribute will be removed in the next major version. Use HarmonyPatch together with MethodType.Getter or MethodType.Setter instead")]
public HarmonyPatch(string propertyName, PropertyMethod type)
public HarmonyPatch(string methodName)
{
info.methodName = methodName;
}

public HarmonyPatch(string methodName, params Type[] argumentTypes)
{
info.methodName = methodName;
info.argumentTypes = argumentTypes;
}

public HarmonyPatch(string methodName, Type[] argumentTypes, ArgumentType[] argumentVariations)
{
info.methodName = methodName;
ParseSpecialArguments(argumentTypes, argumentVariations);
}

public HarmonyPatch(string propertyName, MethodType methodType)
{
info.methodName = propertyName;
info.methodType = type == PropertyMethod.Getter ? MethodType.Getter : MethodType.Setter;
info.methodType = methodType;
}

[Obsolete("This attribute will be removed in the next major version. Use a combination of the other attributes instead")]
public HarmonyPatch(Type declaringType, Type[] argumentTypes)
// starting with 'MethodType'

public HarmonyPatch(MethodType methodType)
{
info.declaringType = declaringType;
info.methodType = methodType;
}

public HarmonyPatch(MethodType methodType, params Type[] argumentTypes)
{
info.methodType = methodType;
info.argumentTypes = argumentTypes;
}

public HarmonyPatch(MethodType methodType, Type[] argumentTypes, ArgumentType[] argumentVariations)
{
info.methodType = methodType;
ParseSpecialArguments(argumentTypes, argumentVariations);
}

// starting with 'Type[]'

public HarmonyPatch(Type[] argumentTypes)
{
info.argumentTypes = argumentTypes;
}

public HarmonyPatch(Type[] argumentTypes, ArgumentType[] argumentVariations)
{
ParseSpecialArguments(argumentTypes, argumentVariations);
}

// Obsolete attributes

[Obsolete("This attribute will be removed in the next major version. Use HarmonyPatch together with MethodType.Getter or MethodType.Setter instead")]
public HarmonyPatch(string propertyName, PropertyMethod type)
{
info.methodName = propertyName;
info.methodType = type == PropertyMethod.Getter ? MethodType.Getter : MethodType.Setter;
}

//

private void ParseSpecialArguments(Type[] argumentTypes, ArgumentType[] argumentVariations)
Expand Down
3 changes: 1 addition & 2 deletions Harmony/HarmonyMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ public class HarmonyMethod

public Type declaringType;
public string methodName;
public MethodType methodType;
public MethodType? methodType;
public Type[] argumentTypes;
public int prioritiy = -1;
public string[] before;
public string[] after;

public HarmonyMethod()
{
methodType = MethodType.Normal;
}

void ImportMethod(MethodInfo theMethod)
Expand Down
8 changes: 7 additions & 1 deletion Harmony/PatchProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ void PrepareType()
}
else
{
var originalMethodType = containerAttributes.methodType;

// MethodType default is Normal
if (containerAttributes.methodType == null)
containerAttributes.methodType = MethodType.Normal;

var isPatchAll = Attribute.GetCustomAttribute(container, typeof(HarmonyPatchAll)) != null;
if (isPatchAll)
{
Expand All @@ -159,7 +165,7 @@ void PrepareType()
var info = "(";
info += "declaringType=" + containerAttributes.declaringType + ", ";
info += "methodName =" + containerAttributes.methodName + ", ";
info += "methodType=" + containerAttributes.methodType + ", ";
info += "methodType=" + originalMethodType + ", ";
info += "argumentTypes=" + containerAttributes.argumentTypes.Description();
info += ")";
throw new ArgumentException("No target method specified for class " + container.FullName + " " + info);
Expand Down

0 comments on commit bb8bb87

Please sign in to comment.