mcpiroman / UnityNativeTool

Allows to unload native plugins in Unity3d editor
MIT License
183 stars 18 forks source link

MarshalAs with LPArray #3

Closed getinthedamnbox closed 5 years ago

getinthedamnbox commented 5 years ago

Hello,

Thank you very much for developing this tool. I'm assisting a professor at my university with some programming tasks, and this tool seems like it solves one of the problems I'm working on. I have a question that I'm hoping you might be able to help me with.

In your documentation, I see this in the "Limitations" section:

[MarshalAs] attribute's properties MarshalCookie, MarshalType, MarshalTypeRef and SafeArrayUserDefinedSubType are not supported (due to Mono bug).

As expected given this issue, there are a couple of DLL imports I can't mock without Unity crashing:

[DllImport("Behavior")]
public static extern void transferActorData([MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)] actorParameter[] agents, int agentcount, int behavior);

[DllImport("Behavior")]
public static extern void transferObstacleData([MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)] obstacleParameter[] obstacles, int obstaclecount);

I'm assuming this is because they use MarshalAs with the UnmanagedType parameter.

Admittedly, I'm not familiar enough with imports of this kind to know how to work around this. (This is my first time attempting to use custom plugins in Unity.) I wanted to ask if you know of a different way to pass these arrays, which could be compatible with your tool. Is there such a thing, to your knowledge?

Thanks again for sharing your work on this.

Matt

mcpiroman commented 5 years ago

So to clarify, according to readme your signatures should work, as you don't use mentioned properties from [MarshalAs] (so UnmanagedType and SizeParamIndex should be fine).

However, I'm able to repro crash with these properties on array parameter. I'll investigate this (it smells like yet another bug in Mono).

In your case, it seems to me that SizeParamIndex is not required if the array is only passed into unmanaged code (not backwords) and UnmanagedType is not required as LPArray should be the default in this case. So then you could remove [MarshalAs] altogether from these imports which should avoid the crash. Please note however that I'm not expert in the interop and marshaling arrays is peculiarly complicated and apparently poorly documented thing.

getinthedamnbox commented 5 years ago

Thank you for the clarification and the advice about removing [MarshalAs]. Just as you said, I was able to remove it entirely and avoid the crash. The tool is now working perfectly. Much appreciated!

mcpiroman commented 5 years ago

It turned out to be another bug in Mono, so I can't do much about it. I'll simply add it to limitations, which is quite unfortune, because unlike previous ones this is actually being used. Thanks for report.