summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI.AssemblyRewriters/Finders
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-03-26 19:17:48 -0400
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-03-26 19:17:48 -0400
commit8bf3ef118a822afd1c7d7f80f6cf6eaeed346167 (patch)
tree75e51b9764337e06883c70d0bc93022bdeaadde2 /src/StardewModdingAPI.AssemblyRewriters/Finders
parent85ed48809032fdbb8461ce4c34acfbe06f68652b (diff)
downloadSMAPI-8bf3ef118a822afd1c7d7f80f6cf6eaeed346167.tar.gz
SMAPI-8bf3ef118a822afd1c7d7f80f6cf6eaeed346167.tar.bz2
SMAPI-8bf3ef118a822afd1c7d7f80f6cf6eaeed346167.zip
add support for rewriting method definitions (#254)
Diffstat (limited to 'src/StardewModdingAPI.AssemblyRewriters/Finders')
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/Finders/EventFinder.cs19
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/Finders/FieldFinder.cs17
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/Finders/MethodFinder.cs17
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/Finders/TypeFinder.cs36
4 files changed, 76 insertions, 13 deletions
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/EventFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/EventFinder.cs
index bcceee32..c0051469 100644
--- a/src/StardewModdingAPI.AssemblyRewriters/Finders/EventFinder.cs
+++ b/src/StardewModdingAPI.AssemblyRewriters/Finders/EventFinder.cs
@@ -37,6 +37,18 @@ namespace StardewModdingAPI.AssemblyRewriters.Finders
this.NounPhrase = nounPhrase ?? $"{fullTypeName}.{eventName} event";
}
+ /// <summary>Rewrite a method definition for compatibility.</summary>
+ /// <param name="module">The module being rewritten.</param>
+ /// <param name="method">The method definition to rewrite.</param>
+ /// <param name="assemblyMap">Metadata for mapping assemblies to the current platform.</param>
+ /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param>
+ /// <returns>Returns whether the instruction was rewritten.</returns>
+ /// <exception cref="IncompatibleInstructionException">The CIL instruction is not compatible, and can't be rewritten.</exception>
+ public virtual bool Rewrite(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged)
+ {
+ return false;
+ }
+
/// <summary>Rewrite a CIL instruction for compatibility.</summary>
/// <param name="module">The module being rewritten.</param>
/// <param name="cil">The CIL rewriter.</param>
@@ -45,9 +57,9 @@ namespace StardewModdingAPI.AssemblyRewriters.Finders
/// <param name="platformChanged">Whether the mod was compiled on a different platform.</param>
/// <returns>Returns whether the instruction was rewritten.</returns>
/// <exception cref="IncompatibleInstructionException">The CIL instruction is not compatible, and can't be rewritten.</exception>
- public bool Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
+ public virtual bool Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
{
- if (!this.IsMatch(instruction, platformChanged))
+ if (!this.IsMatch(instruction))
return false;
throw new IncompatibleInstructionException(this.NounPhrase);
@@ -59,8 +71,7 @@ namespace StardewModdingAPI.AssemblyRewriters.Finders
*********/
/// <summary>Get whether a CIL instruction matches.</summary>
/// <param name="instruction">The IL instruction.</param>
- /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param>
- protected bool IsMatch(Instruction instruction, bool platformChanged)
+ protected bool IsMatch(Instruction instruction)
{
MethodReference methodRef = RewriteHelper.AsMethodReference(instruction);
return
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/FieldFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/FieldFinder.cs
index cdfc3bd5..b44883e9 100644
--- a/src/StardewModdingAPI.AssemblyRewriters/Finders/FieldFinder.cs
+++ b/src/StardewModdingAPI.AssemblyRewriters/Finders/FieldFinder.cs
@@ -37,6 +37,18 @@ namespace StardewModdingAPI.AssemblyRewriters.Finders
this.NounPhrase = nounPhrase ?? $"{fullTypeName}.{fieldName} field";
}
+ /// <summary>Rewrite a method definition for compatibility.</summary>
+ /// <param name="module">The module being rewritten.</param>
+ /// <param name="method">The method definition to rewrite.</param>
+ /// <param name="assemblyMap">Metadata for mapping assemblies to the current platform.</param>
+ /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param>
+ /// <returns>Returns whether the instruction was rewritten.</returns>
+ /// <exception cref="IncompatibleInstructionException">The CIL instruction is not compatible, and can't be rewritten.</exception>
+ public virtual bool Rewrite(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged)
+ {
+ return false;
+ }
+
/// <summary>Rewrite a CIL instruction for compatibility.</summary>
/// <param name="module">The module being rewritten.</param>
/// <param name="cil">The CIL rewriter.</param>
@@ -47,7 +59,7 @@ namespace StardewModdingAPI.AssemblyRewriters.Finders
/// <exception cref="IncompatibleInstructionException">The CIL instruction is not compatible, and can't be rewritten.</exception>
public virtual bool Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
{
- if (!this.IsMatch(instruction, platformChanged))
+ if (!this.IsMatch(instruction))
return false;
throw new IncompatibleInstructionException(this.NounPhrase);
@@ -59,8 +71,7 @@ namespace StardewModdingAPI.AssemblyRewriters.Finders
*********/
/// <summary>Get whether a CIL instruction matches.</summary>
/// <param name="instruction">The IL instruction.</param>
- /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param>
- protected bool IsMatch(Instruction instruction, bool platformChanged)
+ protected bool IsMatch(Instruction instruction)
{
FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction);
return
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/MethodFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/MethodFinder.cs
index 2efcbb0f..19dda58a 100644
--- a/src/StardewModdingAPI.AssemblyRewriters/Finders/MethodFinder.cs
+++ b/src/StardewModdingAPI.AssemblyRewriters/Finders/MethodFinder.cs
@@ -37,6 +37,18 @@ namespace StardewModdingAPI.AssemblyRewriters.Finders
this.NounPhrase = nounPhrase ?? $"{fullTypeName}.{methodName} method";
}
+ /// <summary>Rewrite a method definition for compatibility.</summary>
+ /// <param name="module">The module being rewritten.</param>
+ /// <param name="method">The method definition to rewrite.</param>
+ /// <param name="assemblyMap">Metadata for mapping assemblies to the current platform.</param>
+ /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param>
+ /// <returns>Returns whether the instruction was rewritten.</returns>
+ /// <exception cref="IncompatibleInstructionException">The CIL instruction is not compatible, and can't be rewritten.</exception>
+ public virtual bool Rewrite(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged)
+ {
+ return false;
+ }
+
/// <summary>Rewrite a CIL instruction for compatibility.</summary>
/// <param name="module">The module being rewritten.</param>
/// <param name="cil">The CIL rewriter.</param>
@@ -47,7 +59,7 @@ namespace StardewModdingAPI.AssemblyRewriters.Finders
/// <exception cref="IncompatibleInstructionException">The CIL instruction is not compatible, and can't be rewritten.</exception>
public bool Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
{
- if (!this.IsMatch(instruction, platformChanged))
+ if (!this.IsMatch(instruction))
return false;
throw new IncompatibleInstructionException(this.NounPhrase);
@@ -59,8 +71,7 @@ namespace StardewModdingAPI.AssemblyRewriters.Finders
*********/
/// <summary>Get whether a CIL instruction matches.</summary>
/// <param name="instruction">The IL instruction.</param>
- /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param>
- protected bool IsMatch(Instruction instruction, bool platformChanged)
+ protected bool IsMatch(Instruction instruction)
{
MethodReference methodRef = RewriteHelper.AsMethodReference(instruction);
return
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/TypeFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/TypeFinder.cs
index 96cbb229..0e4d6824 100644
--- a/src/StardewModdingAPI.AssemblyRewriters/Finders/TypeFinder.cs
+++ b/src/StardewModdingAPI.AssemblyRewriters/Finders/TypeFinder.cs
@@ -33,6 +33,21 @@ namespace StardewModdingAPI.AssemblyRewriters.Finders
this.NounPhrase = nounPhrase ?? $"{fullTypeName} type";
}
+ /// <summary>Rewrite a method definition for compatibility.</summary>
+ /// <param name="module">The module being rewritten.</param>
+ /// <param name="method">The method definition to rewrite.</param>
+ /// <param name="assemblyMap">Metadata for mapping assemblies to the current platform.</param>
+ /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param>
+ /// <returns>Returns whether the instruction was rewritten.</returns>
+ /// <exception cref="IncompatibleInstructionException">The CIL instruction is not compatible, and can't be rewritten.</exception>
+ public virtual bool Rewrite(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged)
+ {
+ if (!this.IsMatch(method))
+ return false;
+
+ throw new IncompatibleInstructionException(this.NounPhrase);
+ }
+
/// <summary>Rewrite a CIL instruction for compatibility.</summary>
/// <param name="module">The module being rewritten.</param>
/// <param name="cil">The CIL rewriter.</param>
@@ -43,7 +58,7 @@ namespace StardewModdingAPI.AssemblyRewriters.Finders
/// <exception cref="IncompatibleInstructionException">The CIL instruction is not compatible, and can't be rewritten.</exception>
public virtual bool Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
{
- if (!this.IsMatch(instruction, platformChanged))
+ if (!this.IsMatch(instruction))
return false;
throw new IncompatibleInstructionException(this.NounPhrase);
@@ -54,9 +69,24 @@ namespace StardewModdingAPI.AssemblyRewriters.Finders
** Protected methods
*********/
/// <summary>Get whether a CIL instruction matches.</summary>
+ /// <param name="method">The method deifnition.</param>
+ protected bool IsMatch(MethodDefinition method)
+ {
+ if (method.ReturnType.FullName == this.FullTypeName)
+ return true;
+
+ foreach (VariableDefinition variable in method.Body.Variables)
+ {
+ if (variable.VariableType.FullName == this.FullTypeName)
+ return true;
+ }
+
+ return false;
+ }
+
+ /// <summary>Get whether a CIL instruction matches.</summary>
/// <param name="instruction">The IL instruction.</param>
- /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param>
- protected bool IsMatch(Instruction instruction, bool platformChanged)
+ protected bool IsMatch(Instruction instruction)
{
string fullName = this.FullTypeName;