pardeike / Harmony

A library for patching, replacing and decorating .NET and Mono methods during runtime
https://www.patreon.com/pardeike
MIT License
5.28k stars 492 forks source link

Can I use Transpiler to modify a readonly(const) value in .cctor? #535

Closed S-i-l-v-e-t closed 1 year ago

S-i-l-v-e-t commented 1 year ago

Here is my code, but it wont work. May anyone give me some suggestion?

        [HarmonyTranspiler]
        [HarmonyPatch(typeof(OcDefine), MethodType.StaticConstructor)]
        public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
        {
            var codes = instructions.ToList();
            codes[41].opcode = OpCodes.Ldc_I8;
            codes[41].operand = (byte)255L;
            codes[43].opcode = OpCodes.Ldc_I8;
            codes[43].operand = (byte)255L;
            codes[51].opcode = OpCodes.Ldc_I8;
            codes[51].operand = (byte)255L;
            return codes.AsEnumerable();
        }

my propose is to change these three const readonly value to 0xFF

S-i-l-v-e-t commented 1 year ago

Here is log.

[Error  :  HarmonyX] Failed to patch static void Oc.OcDefine::.cctor(): System.ArgumentException: opcode
  at Mono.Cecil.Cil.Instruction.Create (Mono.Cecil.Cil.OpCode opcode, System.Byte value) [0x00018] in <6034b380a22b41a596c9dc29d282c0a9>:0
  at Mono.Cecil.Cil.ILProcessor.Create (Mono.Cecil.Cil.OpCode opcode, System.Byte value) [0x00041] in <6034b380a22b41a596c9dc29d282c0a9>:0
  at MonoMod.Utils.Cil.CecilILGenerator.Emit (System.Reflection.Emit.OpCode opcode, System.Byte arg) [0x00033] in <43bfc4c6532a47f6b28212e11323aa80>:0
  at (wrapper dynamic-method) MonoMod.Utils.DynamicMethodDefinition.EmitOpcodeWithOperand(MonoMod.Utils.Cil.CecilILGenerator,System.Reflection.Emit.OpCode,object)
  at (wrapper delegate-invoke) System.Action`3[MonoMod.Utils.Cil.CecilILGenerator,System.Reflection.Emit.OpCode,System.Object].invoke_void_T1_T2_T3(MonoMod.Utils.Cil.CecilILGenerator,System.Reflection.Emit.OpCode,object)
  at HarmonyLib.Internal.Util.EmitterExtensions.Emit (MonoMod.Utils.Cil.CecilILGenerator il, System.Reflection.Emit.OpCode opcode, System.Object operand) [0x00000] in <116dd996dda44480a48a0fc9ee2308ed>:0
  at HarmonyLib.Internal.Patching.ILManipulator.WriteTo (Mono.Cecil.Cil.MethodBody body, System.Reflection.MethodBase original) [0x0012b] in <116dd996dda44480a48a0fc9ee2308ed>:0
  at HarmonyLib.Public.Patching.HarmonyManipulator.WriteTranspiledMethod (MonoMod.Cil.ILContext ctx, System.Reflection.MethodBase original, System.Collections.Generic.List`1[T] transpilers, System.Boolean debug) [0x00073] in <116dd996dda44480a48a0fc9ee2308ed>:0
  at HarmonyLib.Public.Patching.HarmonyManipulator.MakePatched (System.Reflection.MethodBase original, MonoMod.Cil.ILContext ctx, System.Collections.Generic.List`1[T] prefixes, System.Collections.Generic.List`1[T] postfixes, System.Collections.Generic.List`1[T] transpilers, System.Collections.Generic.List`1[T] finalizers, System.Collections.Generic.List`1[T] ilmanipulators, System.Boolean debug) [0x0003b] in <116dd996dda44480a48a0fc9ee2308ed>:0
[Error  : Unity Log] InvalidProgramException: Invalid IL code in (wrapper dynamic-method) Oc.OcDefine:DMD<Oc.OcDefine::.cctor> (): IL_00c2: conv.r.un

Stack trace:
System.RuntimeMethodHandle.GetFunctionPointer () (at <d6232873609549b8a045fa15811a5bd3>:0)
MonoMod.RuntimeDetour.Platforms.DetourRuntimeILPlatform.GetFunctionPointer (System.Reflection.MethodBase method, System.RuntimeMethodHandle handle) (at <33dff776d87748ecb372a90c5ddfc734>:0)
MonoMod.RuntimeDetour.Platforms.DetourRuntimeILPlatform.GetNativeStart (System.Reflection.MethodBase method) (at <33dff776d87748ecb372a90c5ddfc734>:0)
MonoMod.RuntimeDetour.DetourHelper.GetNativeStart (System.Reflection.MethodBase method) (at <33dff776d87748ecb372a90c5ddfc734>:0)
MonoMod.RuntimeDetour.Detour._TopApply () (at <33dff776d87748ecb372a90c5ddfc734>:0)
MonoMod.RuntimeDetour.Detour._RefreshChain (System.Reflection.MethodBase method) (at <33dff776d87748ecb372a90c5ddfc734>:0)
MonoMod.RuntimeDetour.Detour.Apply () (at <33dff776d87748ecb372a90c5ddfc734>:0)
MonoMod.RuntimeDetour.Detour..ctor (System.Reflection.MethodBase from, System.Reflection.MethodBase to, MonoMod.RuntimeDetour.DetourConfig& config) (at <33dff776d87748ecb372a90c5ddfc734>:0)
(wrapper dynamic-method) MonoMod.RuntimeDetour.ILHook+Context.DMD<MonoMod.RuntimeDetour.ILHook+Context::Refresh>(MonoMod.RuntimeDetour.ILHook/Context)
(wrapper dynamic-method) MonoMod.Utils.DynamicMethodDefinition.Trampoline<MonoMod.RuntimeDetour.ILHook+Context::Refresh>?1562309794(object)
HarmonyLib.Internal.RuntimeFixes.StackTraceFixes.OnILChainRefresh (System.Object self) (at <116dd996dda44480a48a0fc9ee2308ed>:0)
MonoMod.RuntimeDetour.ILHook.Apply () (at <33dff776d87748ecb372a90c5ddfc734>:0)
HarmonyLib.Public.Patching.ManagedMethodPatcher.DetourTo (System.Reflection.MethodBase replacement) (at <116dd996dda44480a48a0fc9ee2308ed>:0)
Rethrow as HarmonyException: IL Compile Error (unknown location)
HarmonyLib.Public.Patching.ManagedMethodPatcher.DetourTo (System.Reflection.MethodBase replacement) (at <116dd996dda44480a48a0fc9ee2308ed>:0)
HarmonyLib.PatchFunctions.UpdateWrapper (System.Reflection.MethodBase original, HarmonyLib.PatchInfo patchInfo) (at <116dd996dda44480a48a0fc9ee2308ed>:0)
Rethrow as HarmonyException: IL Compile Error (unknown location)
HarmonyLib.PatchClassProcessor.ReportException (System.Exception exception, System.Reflection.MethodBase original) (at <116dd996dda44480a48a0fc9ee2308ed>:0)
HarmonyLib.PatchClassProcessor.Patch () (at <116dd996dda44480a48a0fc9ee2308ed>:0)
HarmonyLib.Harmony.<PatchAll>b__11_0 (System.Type type) (at <116dd996dda44480a48a0fc9ee2308ed>:0)
HarmonyLib.CollectionExtensions.Do[T] (System.Collections.Generic.IEnumerable`1[T] sequence, System.Action`1[T] action) (at <116dd996dda44480a48a0fc9ee2308ed>:0)
HarmonyLib.Harmony.PatchAll (System.Reflection.Assembly assembly) (at <116dd996dda44480a48a0fc9ee2308ed>:0)
HarmonyLib.Harmony.PatchAll () (at <116dd996dda44480a48a0fc9ee2308ed>:0)
SilvetCommandLineTool.BootProccess.Awake () (at <1feb63e49ec940a799e9cc6f17fb8ea7>:0)
UnityEngine.GameObject:AddComponent(Type)
BepInEx.Bootstrap.Chainloader:Start()
UnityEngine.Application:.cctor()
UniStormAtmosphericFogFeature:Create()
UnityEngine.Rendering.Universal.ScriptableRendererFeature:OnEnable()
pardeike commented 1 year ago

Sorry but not my product.