Harmony icon indicating copy to clipboard operation
Harmony copied to clipboard

Harmony AccessTools.MakeDeepCopy fails when target contains a constant or static readonly field

Open xADDBx opened this issue 1 year ago • 1 comments
trafficstars

Describe the bug When using AccessTools.MakeDeepCopy on a class which contains a constant field, Harmony will cause a System.FieldAccessException: Cannot set a constant field.

To Reproduce

  1. Create an executable program
  2. Create another class with a constant field in it
  3. In the Main method, create an instance of the class and call MakeDeepCopy on it
System.FieldAccessException: Cannot set a constant field.
   at System.Reflection.MdFieldInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
   at HarmonyLib.Traverse.SetValue(Object value)
   at HarmonyLib.AccessTools.<>c__DisplayClass102_0.<MakeDeepCopy>b__0(String name, Traverse src, Traverse dst)
   at HarmonyLib.Traverse.<>c__DisplayClass39_0.<IterateFields>b__0(String f)
   at System.Collections.Generic.List`1.ForEach(Action`1 action)
   at HarmonyLib.Traverse.IterateFields(Object source, Object target, Action`3 action)
   at HarmonyLib.AccessTools.MakeDeepCopy(Object source, Type resultType, Func`4 processor, String pathRoot)
   at HarmonyLib.AccessTools.MakeDeepCopy[T](Object source)
   at Harmonyissue.Program.Main(String[] args) in D:\Programming\Projects\Modding\HarmonyStuff\Harmonyissue\Harmonyissue\Program.cs:line 16

Expected behavior Const fields should be ignored when creating a DeepCopy

Screenshots / Code grafik grafik

Runtime environment (please complete the following information):

  • OS: Windows 10, 64bit
  • .NET Version: I've managed to reproduce this error in .NET Framework 4.8.1 (Unity and non-Unity runtime), .NET 6.0 and .NET 8.0. I assume it's an issue on all versions
  • Harmony Version: Tested on Harmony 2.3.3 and Harmony 2.3.1.1.
  • Runtime: I've reproduced it in both Pathfinder: Wrath of the Righteous and Warhammer 40,000: Rogue Trader additionally I can reproduce this in a simple console application

xADDBx avatar Aug 14 '24 00:08 xADDBx