LuaDLL.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. namespace LuaInterface
  2. {
  3. using System;
  4. using System.Runtime.InteropServices;
  5. using System.Reflection;
  6. using System.Collections;
  7. using System.Text;
  8. using System.Security;
  9. #pragma warning disable 414
  10. public class MonoPInvokeCallbackAttribute : System.Attribute
  11. {
  12. private Type type;
  13. public MonoPInvokeCallbackAttribute( Type t ) { type = t; }
  14. }
  15. #pragma warning restore 414
  16. public enum LuaTypes
  17. {
  18. LUA_TNONE=-1,
  19. LUA_TNIL=0,
  20. LUA_TNUMBER=3,
  21. LUA_TSTRING=4,
  22. LUA_TBOOLEAN=1,
  23. LUA_TTABLE=5,
  24. LUA_TFUNCTION=6,
  25. LUA_TUSERDATA=7,
  26. LUA_TTHREAD=8,
  27. LUA_TLIGHTUSERDATA=2
  28. }
  29. public enum LuaGCOptions
  30. {
  31. LUA_GCSTOP = 0,
  32. LUA_GCRESTART = 1,
  33. LUA_GCCOLLECT = 2,
  34. LUA_GCCOUNT = 3,
  35. LUA_GCCOUNTB = 4,
  36. LUA_GCSTEP = 5,
  37. LUA_GCSETPAUSE = 6,
  38. LUA_GCSETSTEPMUL = 7,
  39. }
  40. public enum LuaThreadStatus
  41. {
  42. LUA_YIELD = 1,
  43. LUA_ERRRUN = 2,
  44. LUA_ERRSYNTAX = 3,
  45. LUA_ERRMEM = 4,
  46. LUA_ERRERR = 5,
  47. }
  48. sealed class LuaIndexes
  49. {
  50. public static int LUA_REGISTRYINDEX=-10000;
  51. public static int LUA_ENVIRONINDEX=-10001;
  52. public static int LUA_GLOBALSINDEX=-10002;
  53. }
  54. [ StructLayout( LayoutKind.Sequential )]
  55. public struct ReaderInfo
  56. {
  57. public String chunkData;
  58. public bool finished;
  59. }
  60. public delegate int LuaCSFunction(IntPtr luaState);
  61. public delegate string LuaChunkReader(IntPtr luaState,ref ReaderInfo data,ref uint size);
  62. public delegate int LuaFunctionCallback(IntPtr luaState);
  63. public class LuaDLL
  64. {
  65. public static int LUA_MULTRET = -1;
  66. #if UNITY_IPHONE && !UNITY_EDITOR
  67. const string LUADLL = "__Internal";
  68. #else
  69. const string LUADLL = "ulua";
  70. #endif
  71. // Thread Funcs
  72. [DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
  73. public static extern int lua_tothread(IntPtr L, int index);
  74. [DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
  75. public static extern int lua_xmove(IntPtr from, IntPtr to, int n);
  76. [DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
  77. public static extern int lua_yield(IntPtr L, int nresults);
  78. [DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
  79. public static extern IntPtr lua_newthread(IntPtr L);
  80. [DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
  81. public static extern int lua_resume(IntPtr L, int narg);
  82. [DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
  83. public static extern int lua_status(IntPtr L);
  84. [DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
  85. public static extern int lua_pushthread(IntPtr L);
  86. public static int luaL_getn(IntPtr luaState, int i)
  87. {
  88. return (int)LuaDLL.lua_objlen(luaState, i);
  89. }
  90. [DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
  91. public static extern int lua_gc(IntPtr luaState, LuaGCOptions what, int data);
  92. [DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
  93. public static extern string lua_typename(IntPtr luaState, LuaTypes type);
  94. public static string luaL_typename(IntPtr luaState, int stackPos)
  95. {
  96. return LuaDLL.lua_typename(luaState, LuaDLL.lua_type(luaState, stackPos));
  97. }
  98. public static int lua_isfunction(IntPtr luaState, int stackPos)
  99. {
  100. return Convert.ToInt32(lua_type(luaState, stackPos) == LuaTypes.LUA_TFUNCTION);
  101. }
  102. public static int lua_islightuserdata(IntPtr luaState, int stackPos)
  103. {
  104. return Convert.ToInt32(lua_type(luaState, stackPos) == LuaTypes.LUA_TLIGHTUSERDATA);
  105. }
  106. public static int lua_istable(IntPtr luaState, int stackPos)
  107. {
  108. return Convert.ToInt32(lua_type(luaState, stackPos) == LuaTypes.LUA_TTABLE);
  109. }
  110. public static int lua_isthread(IntPtr luaState, int stackPos)
  111. {
  112. return Convert.ToInt32(lua_type(luaState, stackPos) == LuaTypes.LUA_TTHREAD);
  113. }
  114. [DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
  115. public static extern void luaL_error(IntPtr luaState, string message);
  116. [DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
  117. public static extern string luaL_gsub(IntPtr luaState, string str, string pattern, string replacement);
  118. [DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
  119. public static extern void lua_getfenv(IntPtr luaState, int stackPos);
  120. [DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
  121. public static extern int lua_isuserdata(IntPtr luaState, int stackPos);
  122. [DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
  123. public static extern int lua_lessthan(IntPtr luaState, int stackPos1, int stackPos2);
  124. [DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
  125. public static extern int lua_rawequal(IntPtr luaState, int stackPos1, int stackPos2);
  126. [DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
  127. public static extern int lua_setfenv(IntPtr luaState, int stackPos);
  128. [DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
  129. public static extern void lua_setfield(IntPtr luaState, int stackPos, string name);
  130. [DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
  131. public static extern int luaL_callmeta(IntPtr luaState, int stackPos, string name);
  132. [DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
  133. public static extern IntPtr luaL_newstate();
  134. /// <summary>DEPRECATED - use luaL_newstate() instead!</summary>
  135. public static IntPtr lua_open()
  136. {
  137. return LuaDLL.luaL_newstate();
  138. }
  139. [DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
  140. public static extern void lua_close(IntPtr luaState);
  141. [DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
  142. public static extern void luaL_openlibs(IntPtr luaState);
  143. [DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
  144. public static extern int lua_objlen(IntPtr luaState, int stackPos);
  145. /// <summary>DEPRECATED - use lua_objlen(IntPtr luaState, int stackPos) instead!</summary>
  146. public static int lua_strlen(IntPtr luaState, int stackPos)
  147. {
  148. return lua_objlen(luaState, stackPos);
  149. }
  150. [DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
  151. public static extern int luaL_loadstring(IntPtr luaState, string chunk);
  152. public static int luaL_dostring(IntPtr luaState, string chunk)
  153. {
  154. int result = LuaDLL.luaL_loadstring(luaState, chunk);
  155. if (result != 0)
  156. return result;
  157. return LuaDLL.lua_pcall(luaState, 0, -1, 0);
  158. }
  159. /// <summary>DEPRECATED - use luaL_dostring(IntPtr luaState, string chunk) instead!</summary>
  160. public static int lua_dostring(IntPtr luaState, string chunk)
  161. {
  162. return LuaDLL.luaL_dostring(luaState, chunk);
  163. }
  164. [DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
  165. public static extern void lua_createtable(IntPtr luaState, int narr, int nrec);
  166. public static void lua_newtable(IntPtr luaState)
  167. {
  168. LuaDLL.lua_createtable(luaState, 0, 0);
  169. }
  170. public static int luaL_dofile(IntPtr luaState, string fileName)
  171. {
  172. int result = LuaDLL.luaL_loadfile(luaState, fileName);
  173. if (result != 0)
  174. return result;
  175. return LuaDLL.lua_pcall(luaState, 0, -1, 0);
  176. }
  177. public static void lua_getglobal(IntPtr luaState, string name)
  178. {
  179. LuaDLL.lua_pushstring(luaState,name);
  180. LuaDLL.lua_gettable(luaState,LuaIndexes.LUA_GLOBALSINDEX);
  181. }
  182. public static void lua_setglobal(IntPtr luaState, string name)
  183. {
  184. LuaDLL.lua_pushstring(luaState,name);
  185. LuaDLL.lua_insert(luaState,-2);
  186. LuaDLL.lua_settable(luaState,LuaIndexes.LUA_GLOBALSINDEX);
  187. }
  188. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  189. public static extern void lua_settop(IntPtr luaState, int newTop);
  190. public static void lua_pop(IntPtr luaState, int amount)
  191. {
  192. LuaDLL.lua_settop(luaState, -(amount) - 1);
  193. }
  194. [DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
  195. public static extern void lua_insert(IntPtr luaState, int newTop);
  196. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  197. public static extern void lua_remove(IntPtr luaState, int index);
  198. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  199. public static extern void lua_gettable(IntPtr luaState, int index);
  200. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  201. public static extern void lua_rawget(IntPtr luaState, int index);
  202. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  203. public static extern void lua_settable(IntPtr luaState, int index);
  204. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  205. public static extern void lua_rawset(IntPtr luaState, int index);
  206. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  207. public static extern void lua_setmetatable(IntPtr luaState, int objIndex);
  208. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  209. public static extern int lua_getmetatable(IntPtr luaState, int objIndex);
  210. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  211. public static extern int lua_equal(IntPtr luaState, int index1, int index2);
  212. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  213. public static extern void lua_pushvalue(IntPtr luaState, int index);
  214. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  215. public static extern void lua_replace(IntPtr luaState, int index);
  216. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  217. public static extern int lua_gettop(IntPtr luaState);
  218. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  219. public static extern LuaTypes lua_type(IntPtr luaState, int index);
  220. public static bool lua_isnil(IntPtr luaState, int index)
  221. {
  222. return (LuaDLL.lua_type(luaState,index)==LuaTypes.LUA_TNIL);
  223. }
  224. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  225. public static extern bool lua_isnumber(IntPtr luaState, int index);
  226. public static bool lua_isboolean(IntPtr luaState, int index)
  227. {
  228. return LuaDLL.lua_type(luaState,index)==LuaTypes.LUA_TBOOLEAN;
  229. }
  230. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  231. public static extern int luaL_ref(IntPtr luaState, int registryIndex);
  232. public static int lua_ref(IntPtr luaState, int lockRef)
  233. {
  234. if(lockRef!=0)
  235. {
  236. return LuaDLL.luaL_ref(luaState,LuaIndexes.LUA_REGISTRYINDEX);
  237. }
  238. else return 0;
  239. }
  240. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  241. public static extern void lua_rawgeti(IntPtr luaState, int tableIndex, int index);
  242. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  243. public static extern void lua_rawseti(IntPtr luaState, int tableIndex, int index);
  244. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  245. public static extern IntPtr lua_newuserdata(IntPtr luaState, int size);
  246. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  247. public static extern IntPtr lua_touserdata(IntPtr luaState, int index);
  248. public static void lua_getref(IntPtr luaState, int reference)
  249. {
  250. LuaDLL.lua_rawgeti(luaState,LuaIndexes.LUA_REGISTRYINDEX,reference);
  251. }
  252. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  253. public static extern void luaL_unref(IntPtr luaState, int registryIndex, int reference);
  254. public static void lua_unref(IntPtr luaState, int reference)
  255. {
  256. LuaDLL.luaL_unref(luaState,LuaIndexes.LUA_REGISTRYINDEX,reference);
  257. }
  258. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  259. public static extern bool lua_isstring(IntPtr luaState, int index);
  260. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  261. public static extern bool lua_iscfunction(IntPtr luaState, int index);
  262. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  263. public static extern void lua_pushnil(IntPtr luaState);
  264. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  265. public static extern void lua_pushstdcallcfunction(IntPtr luaState, IntPtr wrapper);
  266. public static void lua_pushstdcallcfunction(IntPtr luaState, LuaCSFunction function)
  267. {
  268. IntPtr fn = Marshal.GetFunctionPointerForDelegate(function);
  269. lua_pushstdcallcfunction(luaState, fn);
  270. }
  271. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  272. public static extern int lua_call(IntPtr luaState, int nArgs, int nResults);
  273. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  274. public static extern int lua_pcall(IntPtr luaState, int nArgs, int nResults, int errfunc);
  275. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  276. public static extern IntPtr lua_tocfunction(IntPtr luaState, int index);
  277. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  278. public static extern double lua_tonumber(IntPtr luaState, int index);
  279. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  280. public static extern bool lua_toboolean(IntPtr luaState, int index);
  281. [DllImport(LUADLL,CallingConvention = CallingConvention.Cdecl)]
  282. public static extern IntPtr lua_tolstring(IntPtr luaState, int index, out int strLen);
  283. public static string lua_tostring(IntPtr luaState, int index)
  284. {
  285. int strlen;
  286. IntPtr str = lua_tolstring(luaState, index, out strlen);
  287. if (str != IntPtr.Zero)
  288. {
  289. return Marshal.PtrToStringAnsi(str, strlen);
  290. }
  291. else
  292. {
  293. return null;
  294. }
  295. }
  296. [DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
  297. public static extern void lua_atpanic(IntPtr luaState, LuaCSFunction panicf);
  298. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  299. public static extern void lua_pushnumber(IntPtr luaState, double number);
  300. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  301. public static extern void lua_pushboolean(IntPtr luaState, bool value);
  302. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  303. public static extern void lua_pushlstring(IntPtr luaState, string str, int size);
  304. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  305. public static extern void lua_pushstring(IntPtr luaState, string str);
  306. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  307. public static extern int luaL_newmetatable(IntPtr luaState, string meta);
  308. [DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
  309. public static extern void lua_getfield(IntPtr luaState, int stackPos, string meta);
  310. public static void luaL_getmetatable(IntPtr luaState, string meta)
  311. {
  312. LuaDLL.lua_getfield(luaState, LuaIndexes.LUA_REGISTRYINDEX, meta);
  313. }
  314. [DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
  315. public static extern IntPtr luaL_checkudata(IntPtr luaState, int stackPos, string meta);
  316. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  317. public static extern bool luaL_getmetafield(IntPtr luaState, int stackPos, string field);
  318. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  319. public static extern int lua_load(IntPtr luaState, LuaChunkReader chunkReader, ref ReaderInfo data, string chunkName);
  320. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  321. public static extern int luaL_loadbuffer(IntPtr luaState, string buff, int size, string name);
  322. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  323. public static extern int luaL_loadfile(IntPtr luaState, string filename);
  324. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  325. public static extern bool luaL_checkmetatable(IntPtr luaState,int obj);
  326. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  327. public static extern int luanet_tonetobject(IntPtr luaState,int obj);
  328. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  329. public static extern int luanet_newudata(IntPtr luaState,int val);
  330. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  331. public static extern int luanet_rawnetobj(IntPtr luaState,int obj);
  332. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  333. public static extern int luanet_checkudata(IntPtr luaState,int obj,string meta);
  334. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  335. public static extern void lua_error(IntPtr luaState);
  336. [DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
  337. public static extern bool lua_checkstack(IntPtr luaState,int extra);
  338. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  339. public static extern int lua_next(IntPtr luaState,int index);
  340. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  341. public static extern void lua_pushlightuserdata(IntPtr luaState, IntPtr udata);
  342. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  343. public static extern IntPtr luanet_gettag();
  344. [DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
  345. public static extern void luaL_where (IntPtr luaState, int level);
  346. }
  347. }