Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.46 KB | None | 0 0
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Windows.Forms;
  4. using System.Text;
  5. using System.IO;
  6. using System.Collections.Generic;
  7. //using System.Dynamic;
  8.  
  9. namespace GSharp {
  10. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  11. public delegate int LuaFunc(IntPtr L);
  12.  
  13. public static class Debug {
  14. [DllImport("kernel32")]
  15. public static extern bool AllocConsole();
  16.  
  17. public static void Msg(object Msg) {
  18. MessageBox.Show(Msg.ToString(), "G# Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
  19. }
  20.  
  21. public static void Error(object Msg) {
  22. MessageBox.Show(Msg.ToString(), "G# Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  23. }
  24.  
  25. public static void Warning(object Msg) {
  26. MessageBox.Show(Msg.ToString(), "G# Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  27. }
  28. }
  29.  
  30. public static unsafe class Lua {
  31. static string PtrToStr(IntPtr StrPtr) {
  32. return Marshal.PtrToStringAnsi(StrPtr);
  33. }
  34.  
  35. static T StrToPtr<T>(string Str, Func<IntPtr, T> F) {
  36. T R;
  37. IntPtr StrPtr = Marshal.StringToHGlobalAnsi(Str);
  38. R = F(StrPtr);
  39. Marshal.FreeHGlobal(StrPtr);
  40. return R;
  41. }
  42.  
  43. static List<LuaFunc> LuaFuncs = new List<LuaFunc>();
  44. const string LIBNAME = "lua_shared.dll";
  45. const CallingConvention CConv = CallingConvention.Cdecl;
  46. const CharSet CSet = CharSet.Auto;
  47.  
  48. public const int TNONE = -1;
  49. public const int TNIL = 0;
  50. public const int TBOOLEAN = 1;
  51. public const int TLIGHTUSERDATA = 2;
  52. public const int TNUMBER = 3;
  53. public const int TSTRING = 4;
  54. public const int TTABLE = 5;
  55. public const int TFUNCTION = 6;
  56. public const int TUSERDATA = 7;
  57. public const int TTHREAD = 8;
  58.  
  59. public const int REGISTRYINDEX = -10000;
  60. public const int ENVIRONINDEX = -10001;
  61. public const int GLOBALSINDEX = -10002;
  62.  
  63. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "luaL_getmetafield")]
  64. public static extern int GetMetaField(IntPtr State, int I, IntPtr S);
  65.  
  66. public static int GetMetaField(IntPtr State, int I, string S) {
  67. return StrToPtr(S, (Sp) => {
  68. return GetMetaField(State, I, Sp);
  69. });
  70. }
  71.  
  72. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "luaL_callmeta")]
  73. public static extern int CallMeta(IntPtr State, int I, IntPtr S);
  74.  
  75. public static int CallMeta(IntPtr State, int I, string S) {
  76. return StrToPtr<int>(S, (Sp) => {
  77. return CallMeta(State, I, Sp);
  78. });
  79. }
  80.  
  81. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "luaL_newstate")]
  82. public static extern IntPtr NewState();
  83.  
  84. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "luaL_openlibs")]
  85. public static extern void OpenLibs(IntPtr L);
  86.  
  87. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "luaopen_base")]
  88. public static extern int OpenBase(IntPtr L);
  89.  
  90. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "luaopen_bit")]
  91. public static extern int OpenBit(IntPtr L);
  92.  
  93. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "luaopen_debug")]
  94. public static extern int OpenDebug(IntPtr L);
  95.  
  96. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "luaopen_jit")]
  97. public static extern int OpenJit(IntPtr L);
  98.  
  99. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "luaopen_math")]
  100. public static extern int OpenMath(IntPtr L);
  101.  
  102. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "luaopen_os")]
  103. public static extern int OpenOS(IntPtr L);
  104.  
  105. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "luaopen_package")]
  106. public static extern int OpenPackage(IntPtr L);
  107.  
  108. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "luaopen_string")]
  109. public static extern int OpenString(IntPtr L);
  110.  
  111. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "luaopen_table")]
  112. public static extern int OpenTable(IntPtr L);
  113.  
  114. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "luaL_typerror")]
  115. public static extern int TypeError(IntPtr State, int I, IntPtr S);
  116.  
  117. public static int TypeError(IntPtr State, int I, string S) {
  118. return StrToPtr<int>(S, (Sp) => {
  119. return TypeError(State, I, Sp);
  120. });
  121. }
  122.  
  123. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "luaL_argerror")]
  124. public static extern int ArgError(IntPtr State, int I, IntPtr S);
  125.  
  126. public static int ArgError(IntPtr State, int I, string S) {
  127. return StrToPtr<int>(S, (Sp) => {
  128. return ArgError(State, I, Sp);
  129. });
  130. }
  131.  
  132. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "luaL_checklstring")]
  133. public static extern string CheckLString(IntPtr State, int I, int L);
  134.  
  135. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "luaL_optlstring")]
  136. public static extern string OptLString(IntPtr State, int I, string S);
  137.  
  138. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "luaL_checknumber")]
  139. public static extern double CheckNumber(IntPtr State, int I);
  140.  
  141. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "luaL_optnumber")]
  142. public static extern double OptNumber(IntPtr State, int I, double Def);
  143.  
  144. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "luaL_checkinteger")]
  145. public static extern int CheckInt(IntPtr State, int I);
  146.  
  147. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "luaL_optinteger")]
  148. public static extern int OptInt(IntPtr State, int I, int Def);
  149.  
  150. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "luaL_checkstack")]
  151. public static extern void CheckStack(IntPtr State, int I, string S);
  152.  
  153. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "luaL_checktype")]
  154. public static extern void CheckType(IntPtr State, int I, int I2);
  155.  
  156. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "luaL_checkany")]
  157. public static extern void CheckAny(IntPtr State, int I);
  158.  
  159. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "luaL_newmetatable")]
  160. public static extern int NewMetatable(IntPtr State, string S);
  161.  
  162. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "luaL_checkudata")]
  163. public static extern IntPtr CheckUData(IntPtr State, int I, string S);
  164.  
  165. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "luaL_where")]
  166. public static extern void Where(IntPtr State, int I);
  167.  
  168. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "luaL_error")]
  169. public static extern int Error(IntPtr State, IntPtr S);
  170.  
  171. public static int Error(IntPtr State, string S) {
  172. return StrToPtr<int>(S, (Sp) => {
  173. return Error(State, Sp);
  174. });
  175. }
  176.  
  177. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "luaL_ref")]
  178. public static extern int Ref(IntPtr State, int I);
  179.  
  180. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "luaL_unref")]
  181. public static extern void Unref(IntPtr State, int I);
  182.  
  183. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "luaL_loadfile")]
  184. public static extern int LoadFile(IntPtr State, IntPtr S);
  185.  
  186. public static int LoadFile(IntPtr State, string S) {
  187. return StrToPtr<int>(S, (Sp) => {
  188. return LoadFile(State, Sp);
  189. });
  190. }
  191.  
  192. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "luaL_loadbuffer")]
  193. public static extern int LoadBuffer(IntPtr State, string S, int I, string S2);
  194.  
  195. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "luaL_loadbuffer")]
  196. public static extern int LoadBuffer(IntPtr State, char* S, int I, string S2);
  197.  
  198. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "luaL_loadstring")]
  199. public static extern int LoadString(IntPtr State, IntPtr S);
  200.  
  201. public static int LoadString(IntPtr State, string S) {
  202. return StrToPtr<int>(S, (Sp) => {
  203. return LoadString(State, Sp);
  204. });
  205. }
  206.  
  207. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "luaL_gsub")]
  208. public static extern string GSub(IntPtr State, string S, string S2, string S3);
  209.  
  210. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "luaL_findtable")]
  211. public static extern string FindTable(IntPtr State, int I, string S, int I2);
  212.  
  213. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_close")]
  214. public static extern void Close(IntPtr State);
  215.  
  216. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_newthread")]
  217. public static extern IntPtr NewThread(IntPtr State);
  218.  
  219. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_atpanic")]
  220. public static extern LuaFunc AtPanic(IntPtr State, LuaFunc F);
  221.  
  222. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_gettop")]
  223. public static extern int GetTop(IntPtr State);
  224.  
  225. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_settop")]
  226. public static extern void SetTop(IntPtr State, int I);
  227.  
  228. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_pushvalue")]
  229. public static extern void PushValue(IntPtr State, int I);
  230.  
  231. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_remove")]
  232. public static extern void Remove(IntPtr State, int I);
  233.  
  234. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_insert")]
  235. public static extern void Insert(IntPtr State, int I);
  236.  
  237. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_replace")]
  238. public static extern void Replace(IntPtr State, int I);
  239.  
  240. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_xmove")]
  241. public static extern void XMove(IntPtr State, IntPtr L, int I);
  242.  
  243. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_isnumber")]
  244. public static extern bool IsNumber(IntPtr State, int I);
  245.  
  246. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_isstring")]
  247. public static extern bool IsString(IntPtr State, int I);
  248.  
  249. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_iscfunction")]
  250. public static extern bool IsCFunction(IntPtr State, int I);
  251.  
  252. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_isuserdata")]
  253. public static extern bool IsUserdata(IntPtr State, int I);
  254.  
  255. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_type")]
  256. public static extern int Type(IntPtr State, int I);
  257.  
  258. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_typename")]
  259. public static extern IntPtr _TypeName(IntPtr State, int I);
  260.  
  261. public static string TypeName(IntPtr State, int I = -1) {
  262. return PtrToStr(_TypeName(State, I));
  263. }
  264.  
  265. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_equal")]
  266. public static extern int Equal(IntPtr State, int I, int I2);
  267.  
  268. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_rawequal")]
  269. public static extern int RawEqual(IntPtr State, int I, int I2);
  270.  
  271. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_lessthan")]
  272. public static extern int LessThan(IntPtr State, int I, int I2);
  273.  
  274. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_tonumber")]
  275. public static extern double ToNumber(IntPtr State, int I);
  276.  
  277. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_tointeger")]
  278. public static extern int ToInteger(IntPtr State, int I);
  279.  
  280. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_toboolean")]
  281. public static extern bool ToBoolean(IntPtr State, int I);
  282.  
  283. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_tolstring")]
  284. public static extern IntPtr ToLString(IntPtr State, int I, IntPtr L);
  285.  
  286. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_objlen")]
  287. public static extern int ObjLen(IntPtr State, int I);
  288.  
  289. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_tocfunction")]
  290. public static extern LuaFunc ToCFunction(IntPtr State, int I);
  291.  
  292. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_touserdata")]
  293. public static extern IntPtr ToUserdata(IntPtr State, int I);
  294.  
  295. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_tothread")]
  296. public static extern IntPtr ToThread(IntPtr State, int I);
  297.  
  298. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_topointer")]
  299. public static extern IntPtr ToPointer(IntPtr State, int I);
  300.  
  301. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_pushnil")]
  302. public static extern void PushNil(IntPtr State);
  303.  
  304. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_pushnumber")]
  305. public static extern void PushNumber(IntPtr State, double D);
  306.  
  307. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_pushinteger")]
  308. public static extern void PushInteger(IntPtr State, int I);
  309.  
  310. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_pushlstring")]
  311. public static extern void PushLString(IntPtr State, string S, int I);
  312.  
  313. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_pushstring")]
  314. public static extern void PushString(IntPtr State, IntPtr S);
  315.  
  316. public static void PushString(IntPtr State, string S) {
  317. StrToPtr<int>(S, (Sp) => {
  318. PushString(State, Sp);
  319. return 0;
  320. });
  321. }
  322.  
  323. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_pushcclosure")]
  324. public static extern void PushCClosure(IntPtr State, LuaFunc F, int I);
  325.  
  326. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_pushboolean")]
  327. public static extern void PushBoolean(IntPtr State, int I);
  328.  
  329. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_pushlightuserdata")]
  330. public static extern void PushLightUserdata(IntPtr State, IntPtr P);
  331.  
  332. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_pushthread")]
  333. public static extern int PushThread(IntPtr State);
  334.  
  335. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_gettable")]
  336. public static extern void GetTable(IntPtr State, int I);
  337.  
  338. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_getfield")]
  339. public static extern void GetField(IntPtr State, int I, IntPtr S);
  340.  
  341. public static void GetField(IntPtr State, int I, string S) {
  342. StrToPtr<int>(S, (Sp) => {
  343. GetField(State, I, Sp);
  344. return 0;
  345. });
  346. }
  347.  
  348. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_rawget")]
  349. public static extern void RawGet(IntPtr State, int I);
  350.  
  351. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_rawgeti")]
  352. public static extern void RawGetI(IntPtr State, int I, int I2);
  353.  
  354. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_createtable")]
  355. public static extern void CreateTable(IntPtr State, int I, int I2);
  356.  
  357. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_newuserdata")]
  358. public static extern IntPtr NewUserdata(IntPtr State, int I);
  359.  
  360. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_getmetatable")]
  361. public static extern bool GetMetatable(IntPtr State, int I);
  362.  
  363. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_getfenv")]
  364. public static extern void GetFEnv(IntPtr State, int I);
  365.  
  366. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_settable")]
  367. public static extern void SetTable(IntPtr State, int I);
  368.  
  369. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_setfield")]
  370. public static extern void SetField(IntPtr State, int I, IntPtr S);
  371.  
  372. public static void SetField(IntPtr State, int I, string S) {
  373. StrToPtr<int>(S, (Sp) => {
  374. SetField(State, I, Sp);
  375. return 0;
  376. });
  377. }
  378.  
  379. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_rawset")]
  380. public static extern void RawSet(IntPtr State, int I);
  381.  
  382. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_rawseti")]
  383. public static extern void RawSetI(IntPtr State, int I, int I2);
  384.  
  385. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_setmetatable")]
  386. public static extern int SetMetatable(IntPtr State, int I);
  387.  
  388. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_setfenv")]
  389. public static extern int SetFEnv(IntPtr State, int I);
  390.  
  391. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_call")]
  392. public static extern void Call(IntPtr State, int I, int I2);
  393.  
  394. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_pcall")]
  395. public static extern int PCall(IntPtr State, int I, int I2, int I3);
  396.  
  397. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_cpcall")]
  398. public static extern int CPCall(IntPtr State, LuaFunc F, IntPtr P);
  399.  
  400. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_yield")]
  401. public static extern int Yield(IntPtr State, int I);
  402.  
  403. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_resume_real")]
  404. public static extern int Resume(IntPtr State, int I);
  405.  
  406. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_status")]
  407. public static extern int Status(IntPtr State);
  408.  
  409. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_gc")]
  410. public static extern int GC(IntPtr State, int I, int I2);
  411.  
  412. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_error")]
  413. public static extern int Error(IntPtr State);
  414.  
  415. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_next")]
  416. public static extern int Next(IntPtr State, int I);
  417.  
  418. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_concat")]
  419. public static extern void Concat(IntPtr State, int I);
  420.  
  421. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_getupvalue")]
  422. public static extern string GetUpValue(IntPtr State, int I, int I2);
  423.  
  424. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_gethookmask")]
  425. public static extern int GetHookMask(IntPtr State);
  426.  
  427. [DllImport(LIBNAME, CallingConvention = CConv, CharSet = CSet, EntryPoint = "lua_gethookcount")]
  428. public static extern int GetHookCount(IntPtr State);
  429.  
  430. // Custom
  431.  
  432. public static string ToString(IntPtr State, int I) {
  433. return PtrToStr(Lua.ToLString(State, I, IntPtr.Zero));
  434. }
  435.  
  436. public static void Pop(IntPtr State, int I = 1) {
  437. SetTop(State, -(I) - 1);
  438. }
  439.  
  440. public static bool IsTable(IntPtr State, int I) {
  441. return Type(State, I) == TTABLE;
  442. }
  443.  
  444. public static void PushCFunction(IntPtr State, LuaFunc F) {
  445. if (!LuaFuncs.Contains(F))
  446. LuaFuncs.Add(F);
  447. PushCClosure(State, F, 0);
  448. }
  449.  
  450. public static void GetGlobal(IntPtr L, string Key) {
  451. Lua.GetField(L, Lua.GLOBALSINDEX, Key);
  452. }
  453.  
  454. public static int GetGlobal(IntPtr L, params string[] Tables) {
  455. Lua.GetField(L, Lua.GLOBALSINDEX, "_G");
  456. int Pops = 1;
  457.  
  458. for (int i = 0; i < Tables.Length; i++) {
  459. if (Lua.Type(L, -1) != Lua.TTABLE)
  460. throw new Exception(Tables[i - 1] + " is not a table");
  461. Lua.PushString(L, Tables[i]);
  462. Lua.GetTable(L, -2);
  463. Lua.Remove(L, -2);
  464. Pops++;
  465. }
  466.  
  467. return Pops;
  468. }
  469.  
  470. public static void RegisterCFunction(IntPtr State, string TableName, string FuncName, LuaFunc F) {
  471. GetField(State, GLOBALSINDEX, TableName);
  472. if (!IsTable(State, -1)) {
  473. CreateTable(State, 0, 1);
  474. SetField(State, GLOBALSINDEX, TableName);
  475. Pop(State);
  476.  
  477. GetField(State, GLOBALSINDEX, TableName);
  478. }
  479.  
  480. PushString(State, FuncName);
  481. PushCFunction(State, F);
  482. SetTable(State, -3);
  483. Pop(State);
  484. }
  485.  
  486. public static string[] GetStack(IntPtr State) {
  487. int Cnt = Lua.GetTop(State);
  488. List<string> R = new List<string>();
  489.  
  490. for (int i = Cnt; i >= 1; i--)
  491. R.Add(string.Format("{0}, {1} = {2}", -i, Cnt - i + 1, Lua.TypeName(State, Lua.Type(State, -i))));
  492.  
  493. return R.ToArray();
  494. }
  495.  
  496. public static void PrintStack(IntPtr State, string Title = "") {
  497. string[] St = GetStack(State);
  498. Console.WriteLine("{0} {1}", Title, "{");
  499. foreach (var S in St)
  500. Console.WriteLine(" {0}", S);
  501. Console.WriteLine("{0}", "}");
  502. }
  503. }
  504.  
  505. public static unsafe class GMod {
  506. private static IntPtr State;
  507. public static object Lock = new object();
  508.  
  509. public static void Init(IntPtr State) {
  510. GMod.State = State;
  511. }
  512.  
  513. public static void Print(object O) {
  514. lock (Lock) {
  515. Lua.GetField(State, Lua.GLOBALSINDEX, "print");
  516. Lua.PushString(State, O != null ? O.ToString() : "NULL");
  517. Lua.Call(State, 1, 0);
  518. }
  519. }
  520.  
  521. public static void MsgC(int R, int G, int B, string Msg) {
  522. lock (Lock) {
  523. Lua.GetField(State, Lua.GLOBALSINDEX, "MsgC");
  524.  
  525. Lua.CreateTable(State, 0, 3);
  526. Lua.PushString(State, "r");
  527. Lua.PushNumber(State, R);
  528. Lua.SetTable(State, -3);
  529.  
  530. Lua.PushString(State, "g");
  531. Lua.PushNumber(State, G);
  532. Lua.SetTable(State, -3);
  533.  
  534. Lua.PushString(State, "b");
  535. Lua.PushNumber(State, B);
  536. Lua.SetTable(State, -3);
  537.  
  538. Lua.PushString(State, "a");
  539. Lua.PushNumber(State, 255);
  540. Lua.SetTable(State, -3);
  541.  
  542. Lua.PushString(State, Msg);
  543. Lua.Call(State, 2, 0);
  544. }
  545. }
  546. }
  547. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement