Comments (3)
I reopen this as a reminder to check if structs work with __instance
from harmony.
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.
After discussion, we agree on closing this one.
from harmony.
Related Issues (20)
- Harmony 3.0: Utility Epic
- Investigate Testing the NuGet Package Directly HOT 3
- Add a Release Pipeline
- Hook exception: System.Exception: Cannot get result from void method System.Void HOT 2
- How to patch method of base class? HOT 1
- When applying a patch fails, the replacement's IL is not printed to the debug log HOT 2
- System.Exception: Parameter "xx" not found in method HOT 10
- SymbolExtensions does not resolve overriden method HOT 3
- can lib work in arm64? HOT 2
- Companion project promotion: Harmony Tools HOT 3
- PatchFunctions.UpdateWrapper NotImplementedException on MacOS HOT 11
- How do i change valuetype HOT 1
- Harmony.Patch() is a bit slow. HOT 2
- 'CORINFO_METHOD_INFO' from assembly 'System.Text.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. HOT 4
- I try to hook DateTime.UtcNow but it throw exception HOT 5
- x86 platform makes patch fail
- return type only support void and bool? why doesn't string work HOT 1
- Could not load type 'CORINFO_METHOD_INFO' from assembly 'System.Text.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. HOT 1
- "ref" keyword not allowed for "__runOriginal" HOT 5
- HarmonyPatchAll do not work with multiple HarmonyPatch HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from harmony.