Giter Site home page Giter Site logo

Trouble patching properties. about harmony HOT 3 CLOSED

pardeike avatar pardeike commented on August 25, 2024
Trouble patching properties.

from harmony.

Comments (3)

pardeike avatar pardeike commented on August 25, 2024

I reopen this as a reminder to check if structs work with __instance

from harmony.

pardeike avatar pardeike commented on August 25, 2024

I made the following test mod in RimWorld (I use it only as a mono execution environment). Here is the only file it has:

using System.Reflection;
using Verse;
using UnityEngine;
using Harmony;

namespace StructTestMod
{
	public class TreeInfo
	{
		public float m_prefabDataIndex;
	}

	public class PrefabCollection<T>
	{
		internal static TreeInfo GetPrefab(uint i)
		{
			Log.Error("GetPrefab(" + i + ")");
			return new TreeInfo();
		}
	}

	public struct TreeInstance
	{
		public uint m_infoIndex;
		public float m_posX;
		public float m_posY;
		public float m_posZ;

		public TreeInfo Info
		{
			get
			{
				Log.Error("TreeInfo get()");
				return PrefabCollection<TreeInfo>.GetPrefab((uint)this.m_infoIndex);
			}
			set
			{
				Log.Error("TreeInfo set()");
				this.m_infoIndex = (ushort)Mathf.Clamp(value.m_prefabDataIndex, 0, 65535);
			}
		}
	}

	[StaticConstructorOnStartup]
	class Main
	{
		static Main()
		{
			HarmonyInstance.DEBUG = true;
			var harmony = HarmonyInstance.Create("net.pardeike.structtestmod");
			harmony.PatchAll(Assembly.GetExecutingAssembly());

			var ti = new TreeInstance();
			var tree = ti.Info;
			Log.Warning("" + tree);
		}
	}

	[HarmonyPatch(typeof(TreeInstance))]
	[HarmonyPatch("get_Info")]
	public class TreeInstanceGetInfoPatch
	{
		public static bool Prefix(ref TreeInstance __instance, ref TreeInfo __result)
		{
			Log.Error("Prefix!");
			var info = PrefabCollection<TreeInfo>.GetPrefab(__instance.m_infoIndex);
			Vector3 pos;
			pos.x = __instance.m_posX;
			pos.y = __instance.m_posY;
			pos.z = __instance.m_posZ;
			__result = info;
			return true;
		}
	}
}

The corresponding debug output from Harmony is this:

PATCHING StructTestMod.TreeInstance StructTestMod.TreeInfo get_Info()
L_0000: Local var #0 StructTestMod.TreeInfo
L_0000: Local var #1 StructTestMod.TreeInfo
L_0000: ldnull
L_0001: stloc 1 (StructTestMod.TreeInfo)
L_0002: ldarga 0
L_0008: ldloca 1 (StructTestMod.TreeInfo)
L_000a: call Boolean Prefix(TreeInstance ByRef, StructTestMod.TreeInfo ByRef)
L_000f: brfalse Label #1
L_0014: nop
L_0015: ldstr "TreeInfo get()"
L_001a: call Void Error(System.String)
L_001f: nop
L_0020: ldarg.0
L_0021: ldfld System.UInt32 m_infoIndex
L_0026: call StructTestMod.TreeInfo GetPrefab(UInt32)
L_002b: stloc.0
L_002c: br Label #2
L_0031: Label #2
L_0031: ldloc.0
L_0032: br Label #0
L_0037: Label #0
L_0037: stloc 1 (StructTestMod.TreeInfo)
L_0038: Label #1
L_0038: ldloc 1 (StructTestMod.TreeInfo)
L_0039: ret
DONE

and the log inside RimWorld shows:

ERR Prefix!
ERR GetPrefab(9890392)
ERR TreeInfo get()
ERR GetPrefix(0)
WARN StructTestMod.TreeInfo

Seems to me that I am missing something here, because all this shows that Harmony works as expected.

from harmony.

pardeike avatar pardeike commented on August 25, 2024

After discussion, we agree on closing this one.

from harmony.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.