Drake53 / War3Api

Warcraft III's Common and Blizzard API's in C#.
MIT License
23 stars 4 forks source link

Expose a typesafe way to get global variables #2

Open jamesrom opened 4 years ago

jamesrom commented 4 years ago

Using CSharp.lua's method of inserting Lua code, we can leverage that to allow access to global variables from C#.

Drake53 commented 4 years ago

It's easier to write using CSharpLua.Template:

/// @CSharpLua.Template = "rawget(System.global, {0})"
public static extern T GetGlobal<T>(string key);
Drake53 commented 4 years ago

I just tested this method, it works when added directly to the source, but not when added as a package (.dll/.xml). This is due to a bug in my fork of CSharpLua, which is unable to parse the documentation .xml file properly: it cannot find the CSharpLua.Template attribute, because you added documentation xml nodes to it (summary, etc). I'll have to fix this bug before I can merge your PR.

Drake53 commented 4 years ago

Did some more testing, and it turns out I incorrectly assumed it was caused by the documentation. The real cause is the generic type argument (in combination with it being passed to the transpiler as a .dll instead of .cs).

Related: https://github.com/yanghuan/CSharp.lua/wiki/FAQ_en#9how-to-change-some-of-the-generated-behavior