summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/ModLoading/Rewriters
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2020-08-26 21:55:04 -0400
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2020-08-26 21:55:04 -0400
commitfd6835555ccdfecc09d87aab29b9c5cec3820720 (patch)
tree7fa39ec63dc12b84328d3d3dccd4c3a8892e5b50 /src/SMAPI/Framework/ModLoading/Rewriters
parenta4938fc4653ad386e9ac88c2e6a0d95347b3f6f7 (diff)
downloadSMAPI-fd6835555ccdfecc09d87aab29b9c5cec3820720.tar.gz
SMAPI-fd6835555ccdfecc09d87aab29b9c5cec3820720.tar.bz2
SMAPI-fd6835555ccdfecc09d87aab29b9c5cec3820720.zip
fix InvalidProgramException when replacing CIL instructions in some cases
Diffstat (limited to 'src/SMAPI/Framework/ModLoading/Rewriters')
-rw-r--r--src/SMAPI/Framework/ModLoading/Rewriters/FieldReplaceRewriter.cs4
-rw-r--r--src/SMAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs5
-rw-r--r--src/SMAPI/Framework/ModLoading/Rewriters/StaticFieldToConstantRewriter.cs30
3 files changed, 18 insertions, 21 deletions
diff --git a/src/SMAPI/Framework/ModLoading/Rewriters/FieldReplaceRewriter.cs b/src/SMAPI/Framework/ModLoading/Rewriters/FieldReplaceRewriter.cs
index 2b8fbe06..c251a30c 100644
--- a/src/SMAPI/Framework/ModLoading/Rewriters/FieldReplaceRewriter.cs
+++ b/src/SMAPI/Framework/ModLoading/Rewriters/FieldReplaceRewriter.cs
@@ -58,8 +58,8 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters
return false;
// replace with new field
- FieldReference newRef = module.ImportReference(this.ToField);
- replaceWith(cil.Create(instruction.OpCode, newRef));
+ instruction.Operand = module.ImportReference(this.ToField);
+
return this.MarkRewritten();
}
}
diff --git a/src/SMAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs b/src/SMAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs
index de123cc2..0a99bde0 100644
--- a/src/SMAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs
+++ b/src/SMAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs
@@ -49,8 +49,9 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters
return false;
// rewrite field to property
- MethodReference propertyRef = module.ImportReference(method);
- replaceWith(cil.Create(OpCodes.Call, propertyRef));
+ instruction.OpCode = OpCodes.Call;
+ instruction.Operand = module.ImportReference(method);
+
this.Phrases.Add($"{fieldRef.DeclaringType.Name}.{fieldRef.Name} (field => property)");
return this.MarkRewritten();
}
diff --git a/src/SMAPI/Framework/ModLoading/Rewriters/StaticFieldToConstantRewriter.cs b/src/SMAPI/Framework/ModLoading/Rewriters/StaticFieldToConstantRewriter.cs
index 6ea59d1c..2f1122b4 100644
--- a/src/SMAPI/Framework/ModLoading/Rewriters/StaticFieldToConstantRewriter.cs
+++ b/src/SMAPI/Framework/ModLoading/Rewriters/StaticFieldToConstantRewriter.cs
@@ -46,24 +46,20 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters
return false;
// rewrite to constant
- replaceWith(this.CreateConstantInstruction(cil, this.Value));
- return this.MarkRewritten();
- }
-
-
- /*********
- ** Private methods
- *********/
- /// <summary>Create a CIL constant value instruction.</summary>
- /// <param name="cil">The CIL processor.</param>
- /// <param name="value">The constant value to set.</param>
- private Instruction CreateConstantInstruction(ILProcessor cil, object value)
- {
if (typeof(TValue) == typeof(int))
- return cil.Create(OpCodes.Ldc_I4, (int)value);
- if (typeof(TValue) == typeof(string))
- return cil.Create(OpCodes.Ldstr, (string)value);
- throw new NotSupportedException($"Rewriting to constant values of type {typeof(TValue)} isn't currently supported.");
+ {
+ instruction.OpCode = OpCodes.Ldc_I4;
+ instruction.Operand = this.Value;
+ }
+ else if (typeof(TValue) == typeof(string))
+ {
+ instruction.OpCode = OpCodes.Ldstr;
+ instruction.Operand = this.Value;
+ }
+ else
+ throw new NotSupportedException($"Rewriting to constant values of type {typeof(TValue)} isn't currently supported.");
+
+ return this.MarkRewritten();
}
}
}