Skip to content

Instantly share code, notes, and snippets.

@spacechase0
Created August 14, 2020 10:56
Show Gist options
  • Save spacechase0/0d616874dae4a0025e25dfbe95f8c78b to your computer and use it in GitHub Desktop.
Save spacechase0/0d616874dae4a0025e25dfbe95f8c78b to your computer and use it in GitHub Desktop.
BiggerCraftables.Cecil
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