From fd6835555ccdfecc09d87aab29b9c5cec3820720 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 26 Aug 2020 21:55:04 -0400 Subject: fix InvalidProgramException when replacing CIL instructions in some cases --- .../ModLoading/Rewriters/FieldReplaceRewriter.cs | 4 +-- .../Rewriters/FieldToPropertyRewriter.cs | 5 ++-- .../Rewriters/StaticFieldToConstantRewriter.cs | 30 ++++++++++------------ 3 files changed, 18 insertions(+), 21 deletions(-) (limited to 'src/SMAPI/Framework/ModLoading/Rewriters') 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 - *********/ - /// Create a CIL constant value instruction. - /// The CIL processor. - /// The constant value to set. - 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(); } } } -- cgit