Created
August 14, 2020 10:56
-
-
Save spacechase0/0d616874dae4a0025e25dfbe95f8c78b to your computer and use it in GitHub Desktop.
BiggerCraftables.Cecil
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Mono.Cecil; | |
using Mono.Cecil.Cil; | |
using Netcode; | |
using StardewModdingAPI; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace BiggerCraftables.Cecil | |
{ | |
public class CecilMod : StardewModdingAPI.RewriterMod | |
{ | |
public override void Entry( AssemblyDefinition gameAssembly ) | |
{ | |
var sobj = gameAssembly.MainModule.Types.Single(t => t.Namespace == "StardewValley" && t.Name == "Object"); | |
TypeReference netInt = gameAssembly.MainModule.ImportReference( typeof( NetInt ) ); | |
FieldDefinition field = new FieldDefinition( "BiggerIndex", FieldAttributes.Public | FieldAttributes.InitOnly, netInt ); | |
sobj.Fields.Add( field ); | |
Monitor.Log( "Added BiggerIndex field to StardewValley.Object", LogLevel.Debug ); | |
foreach ( var meth in sobj.Methods ) | |
{ | |
if ( !meth.IsConstructor ) | |
continue; | |
meth.Body.Instructions.Insert( 0, Instruction.Create(OpCodes.Ldarg_0) ); | |
meth.Body.Instructions.Insert( 1, Instruction.Create(OpCodes.Newobj, gameAssembly.MainModule.ImportReference( netInt.Resolve().Methods.Single( md => md.IsConstructor && !md.HasParameters ) ) ) ); | |
meth.Body.Instructions.Insert( 2, Instruction.Create(OpCodes.Stfld, field ) ); | |
Monitor.Log( "Added BiggerIndex initialization to " + meth ); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment