commit f3de9e31d8113926fa7fca985222af6c67556cac Author: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Sun May 14 00:00:20 2017 +0300 Initial commit diff --git a/Release/CL.read.1.tlog b/Release/CL.read.1.tlog new file mode 100644 index 0000000..3c05c30 Binary files /dev/null and b/Release/CL.read.1.tlog differ diff --git a/Release/CL.write.1.tlog b/Release/CL.write.1.tlog new file mode 100644 index 0000000..1499bce Binary files /dev/null and b/Release/CL.write.1.tlog differ diff --git a/Release/cl.command.1.tlog b/Release/cl.command.1.tlog new file mode 100644 index 0000000..d1c5a90 Binary files /dev/null and b/Release/cl.command.1.tlog differ diff --git a/Release/gmcl_jack_win32.dll.intermediate.manifest b/Release/gmcl_jack_win32.dll.intermediate.manifest new file mode 100644 index 0000000..ecea6f7 --- /dev/null +++ b/Release/gmcl_jack_win32.dll.intermediate.manifest @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/Release/gmcl_jack_win32.lastbuildstate b/Release/gmcl_jack_win32.lastbuildstate new file mode 100644 index 0000000..67b41d3 --- /dev/null +++ b/Release/gmcl_jack_win32.lastbuildstate @@ -0,0 +1,2 @@ +#v4.0:v100:false +Release|Win32|D:\GMHax\| diff --git a/Release/gmcl_jack_win32.log b/Release/gmcl_jack_win32.log new file mode 100644 index 0000000..f153b35 --- /dev/null +++ b/Release/gmcl_jack_win32.log @@ -0,0 +1,29 @@ +Build started 09.05.2017 1:38:12. + 1>Project "D:\GMHax\gmcl_jack_win32\gmcl_jack_win32.vcxproj" on node 2 (build target(s)). + 1>InitializeBuildStatus: + Creating "Release\gmcl_jack_win32.unsuccessfulbuild" because "AlwaysCreate" was specified. + ClCompile: + C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\CL.exe /c /Zi /nologo /W3 /WX- /O2 /Oi /Oy- /GL /D _WINDLL /D _MBCS /Gm- /EHsc /MD /GS /Gy /fp:precise /Zc:wchar_t /Zc:forScope /Fo"Release\\" /Fd"Release\vc100.pdb" /Gd /TP /analyze- /errorReport:prompt state.cpp + state.cpp + 1>state.cpp(116): warning C4996: 'vsnprintf': This function or variable may be unsafe. Consider using vsnprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. + C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\stdio.h(354): см. объявление "vsnprintf" + 1>state.cpp(140): warning C4800: int: принудительно задано логическое значение "true" или "false" (предупреждение о производительности) + 1>state.cpp(325): warning C4996: '_snprintf': This function or variable may be unsafe. Consider using _snprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. + C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\stdio.h(363): см. объявление "_snprintf" + Link: + C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\link.exe /ERRORREPORT:PROMPT /OUT:"D:\GMHax\Release\gmcl_jack_win32.dll" /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /ManifestFile:"Release\gmcl_jack_win32.dll.intermediate.manifest" /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"D:\GMHax\Release\gmcl_jack_win32.pdb" /OPT:REF /OPT:ICF /LTCG /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"D:\GMHax\Release\gmcl_jack_win32.lib" /MACHINE:X86 /DLL Release\main.obj + Release\state.obj + Создается библиотека D:\GMHax\Release\gmcl_jack_win32.lib и объект D:\GMHax\Release\gmcl_jack_win32.exp + Создание кода + Создание кода завершено + gmcl_jack_win32.vcxproj -> D:\GMHax\Release\gmcl_jack_win32.dll + Manifest: + C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin\mt.exe /nologo /verbose /outputresource:"D:\GMHax\Release\gmcl_jack_win32.dll;#2" /manifest Release\gmcl_jack_win32.dll.intermediate.manifest + FinalizeBuildStatus: + Deleting file "Release\gmcl_jack_win32.unsuccessfulbuild". + Touching "Release\gmcl_jack_win32.lastbuildstate". + 1>Done Building Project "D:\GMHax\gmcl_jack_win32\gmcl_jack_win32.vcxproj" (build target(s)). + +Построение успешно завершено. + +Time Elapsed 00:00:02.79 diff --git a/Release/gmcl_jack_win32.vcxprojResolveAssemblyReference.cache b/Release/gmcl_jack_win32.vcxprojResolveAssemblyReference.cache new file mode 100644 index 0000000..3f01681 Binary files /dev/null and b/Release/gmcl_jack_win32.vcxprojResolveAssemblyReference.cache differ diff --git a/Release/gmcl_jack_win32.write.1.tlog b/Release/gmcl_jack_win32.write.1.tlog new file mode 100644 index 0000000..ffde6a9 --- /dev/null +++ b/Release/gmcl_jack_win32.write.1.tlog @@ -0,0 +1,135 @@ +^D:\GMHax\gmcl_jack_win32\gmcl_jack_win32.vcxproj +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.exp +D:\GMHax\Release\gmcl_jack_win32.exp +^D:\GMHax\gmcl_jack_win32\gmcl_jack_win32.vcxproj +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.exp +D:\GMHax\Release\gmcl_jack_win32.exp +^D:\GMHax\gmcl_jack_win32\gmcl_jack_win32.vcxproj +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.exp +D:\GMHax\Release\gmcl_jack_win32.exp +^D:\GMHax\gmcl_jack_win32\gmcl_jack_win32.vcxproj +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.exp +D:\GMHax\Release\gmcl_jack_win32.exp +^D:\GMHax\gmcl_jack_win32\gmcl_jack_win32.vcxproj +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.exp +D:\GMHax\Release\gmcl_jack_win32.exp +^D:\GMHax\gmcl_jack_win32\gmcl_jack_win32.vcxproj +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.exp +D:\GMHax\Release\gmcl_jack_win32.exp +^D:\GMHax\gmcl_jack_win32\gmcl_jack_win32.vcxproj +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.exp +D:\GMHax\Release\gmcl_jack_win32.exp +^D:\GMHax\gmcl_jack_win32\gmcl_jack_win32.vcxproj +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.exp +D:\GMHax\Release\gmcl_jack_win32.exp +^D:\GMHax\gmcl_jack_win32\gmcl_jack_win32.vcxproj +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.exp +D:\GMHax\Release\gmcl_jack_win32.exp +^D:\GMHax\gmcl_jack_win32\gmcl_jack_win32.vcxproj +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.exp +D:\GMHax\Release\gmcl_jack_win32.exp +^D:\GMHax\gmcl_jack_win32\gmcl_jack_win32.vcxproj +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.exp +D:\GMHax\Release\gmcl_jack_win32.exp +^D:\GMHax\gmcl_jack_win32\gmcl_jack_win32.vcxproj +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.exp +D:\GMHax\Release\gmcl_jack_win32.exp +^D:\GMHax\gmcl_jack_win32\gmcl_jack_win32.vcxproj +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.exp +D:\GMHax\Release\gmcl_jack_win32.exp +^D:\GMHax\gmcl_jack_win32\gmcl_jack_win32.vcxproj +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.exp +D:\GMHax\Release\gmcl_jack_win32.exp +^D:\GMHax\gmcl_jack_win32\gmcl_jack_win32.vcxproj +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.exp +D:\GMHax\Release\gmcl_jack_win32.exp +^D:\GMHax\gmcl_jack_win32\gmcl_jack_win32.vcxproj +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.exp +D:\GMHax\Release\gmcl_jack_win32.exp +^D:\GMHax\gmcl_jack_win32\gmcl_jack_win32.vcxproj +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.exp +D:\GMHax\Release\gmcl_jack_win32.exp +^D:\GMHax\gmcl_jack_win32\gmcl_jack_win32.vcxproj +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.exp +D:\GMHax\Release\gmcl_jack_win32.exp +^D:\GMHax\gmcl_jack_win32\gmcl_jack_win32.vcxproj +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.exp +D:\GMHax\Release\gmcl_jack_win32.exp +^D:\GMHax\gmcl_jack_win32\gmcl_jack_win32.vcxproj +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.exp +D:\GMHax\Release\gmcl_jack_win32.exp +^D:\GMHax\gmcl_jack_win32\gmcl_jack_win32.vcxproj +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.exp +D:\GMHax\Release\gmcl_jack_win32.exp +^D:\GMHax\gmcl_jack_win32\gmcl_jack_win32.vcxproj +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.exp +D:\GMHax\Release\gmcl_jack_win32.exp +^D:\GMHax\gmcl_jack_win32\gmcl_jack_win32.vcxproj +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.exp +D:\GMHax\Release\gmcl_jack_win32.exp +^D:\GMHax\gmcl_jack_win32\gmcl_jack_win32.vcxproj +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.exp +D:\GMHax\Release\gmcl_jack_win32.exp +^D:\GMHax\gmcl_jack_win32\gmcl_jack_win32.vcxproj +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.exp +D:\GMHax\Release\gmcl_jack_win32.exp +^D:\GMHax\gmcl_jack_win32\gmcl_jack_win32.vcxproj +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.exp +D:\GMHax\Release\gmcl_jack_win32.exp +^D:\GMHax\gmcl_jack_win32\gmcl_jack_win32.vcxproj +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.lib +D:\GMHax\Release\gmcl_jack_win32.exp +D:\GMHax\Release\gmcl_jack_win32.exp diff --git a/Release/link.command.1.tlog b/Release/link.command.1.tlog new file mode 100644 index 0000000..e832dac Binary files /dev/null and b/Release/link.command.1.tlog differ diff --git a/Release/link.read.1.tlog b/Release/link.read.1.tlog new file mode 100644 index 0000000..869c4e4 Binary files /dev/null and b/Release/link.read.1.tlog differ diff --git a/Release/link.write.1.tlog b/Release/link.write.1.tlog new file mode 100644 index 0000000..1659a7b Binary files /dev/null and b/Release/link.write.1.tlog differ diff --git a/Release/main.obj b/Release/main.obj new file mode 100644 index 0000000..211465d Binary files /dev/null and b/Release/main.obj differ diff --git a/Release/mt.command.1.tlog b/Release/mt.command.1.tlog new file mode 100644 index 0000000..1f120ad Binary files /dev/null and b/Release/mt.command.1.tlog differ diff --git a/Release/mt.read.1.tlog b/Release/mt.read.1.tlog new file mode 100644 index 0000000..85f30f2 Binary files /dev/null and b/Release/mt.read.1.tlog differ diff --git a/Release/mt.write.1.tlog b/Release/mt.write.1.tlog new file mode 100644 index 0000000..b3aff2a Binary files /dev/null and b/Release/mt.write.1.tlog differ diff --git a/Release/state.obj b/Release/state.obj new file mode 100644 index 0000000..15c7eb1 Binary files /dev/null and b/Release/state.obj differ diff --git a/Release/vc100.pdb b/Release/vc100.pdb new file mode 100644 index 0000000..89a445a Binary files /dev/null and b/Release/vc100.pdb differ diff --git a/gmcl_jack_win32.vcxproj b/gmcl_jack_win32.vcxproj new file mode 100644 index 0000000..709ed28 --- /dev/null +++ b/gmcl_jack_win32.vcxproj @@ -0,0 +1,74 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {14BE7A2F-5B95-442B-9650-391727398D18} + gmcl_jack_win32 + + + + Application + true + MultiByte + + + DynamicLibrary + false + true + MultiByte + + + + + + + + + + + + + + + Level3 + Disabled + + + true + + + + + Level3 + MaxSpeed + true + true + + + true + true + true + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/gmcl_jack_win32.vcxproj.filters b/gmcl_jack_win32.vcxproj.filters new file mode 100644 index 0000000..bccb874 --- /dev/null +++ b/gmcl_jack_win32.vcxproj.filters @@ -0,0 +1,36 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Файлы исходного кода + + + Файлы исходного кода + + + + + Заголовочные файлы + + + Заголовочные файлы + + + Заголовочные файлы + + + \ No newline at end of file diff --git a/gmcl_jack_win32.vcxproj.user b/gmcl_jack_win32.vcxproj.user new file mode 100644 index 0000000..ace9a86 --- /dev/null +++ b/gmcl_jack_win32.vcxproj.user @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/interface.h b/interface.h new file mode 100644 index 0000000..9b1b656 --- /dev/null +++ b/interface.h @@ -0,0 +1,37 @@ +#ifndef __INTERFACE_H +#define __INTERFACE_H + +#include "lua.h" +#include "state.h" + +class CLuaInterface; + +class CLuaShared +{ +public: + void** vtable; + char tmp[88]; + CLuaInterface* m_pClient; + CLuaInterface* m_pServer; + CLuaInterface* m_pMenu; +}; + +class CLuaInterface +{ +public: + void** vtable; + CLuaState* m_pState; +}; + +#ifndef DLL_EXPORT +#if defined(__GCC__) +#define DLL_EXPORT __attribute__((dllexport)) +#elif defined (_MSC_VER) +#define DLL_EXPORT __declspec(dllexport) +#endif +#endif + +#define GMOD_MODULE_OPEN() extern "C" DLL_EXPORT int gmod13_open(CLuaState* L) +#define GMOD_MODULE_CLOSE() extern "C" DLL_EXPORT int gmod13_close(CLuaState* L) + +#endif \ No newline at end of file diff --git a/lua.h b/lua.h new file mode 100644 index 0000000..fafb160 --- /dev/null +++ b/lua.h @@ -0,0 +1,87 @@ +#ifndef __LUA_H +#define __LUA_H + +#include +#include +#include + +#define LUA_REGISTRYINDEX (-10000) +#define LUA_ENVIRONINDEX (-10001) +#define LUA_GLOBALSINDEX (-10002) + +class CLuaState; +typedef int (*CLuaFunction)(CLuaState*); + +typedef int (*lua_type) (CLuaState *L, int idx); +typedef const char *(*lua_typename) (CLuaState *L, int tp); + +typedef int (*lua_gettop) (CLuaState *L); +typedef void (*lua_settop) (CLuaState *L, int idx); +typedef void (*lua_pushvalue) (CLuaState *L, int idx); + +typedef double (*lua_tonumber) (CLuaState *L, int idx); +typedef int (*lua_toboolean) (CLuaState *L, int idx); +typedef const char *(*lua_tolstring) (CLuaState *L, int idx, size_t *len); +typedef CLuaFunction (*lua_tocfunction) (CLuaState *L, int idx); +typedef void *(*lua_touserdata) (CLuaState *L, int idx); +typedef CLuaState *(*lua_tothread) (CLuaState *L, int idx); +typedef const void *(*lua_topointer) (CLuaState *L, int idx); + +typedef void (*lua_pushnil) (CLuaState *L); +typedef void (*lua_pushnumber) (CLuaState *L, double n); +typedef void (*lua_pushlstring) (CLuaState *L, const char *s, size_t l); +typedef void (*lua_pushcclosure) (CLuaState *L, CLuaFunction fn, int n); +typedef void (*lua_pushboolean) (CLuaState *L, int b); +typedef void (*lua_pushlightuserdata) (CLuaState *L, void *p); +typedef int (*lua_pushthread) (CLuaState *L); + +typedef void (*lua_gettable) (CLuaState *L, int idx); +typedef void (*lua_getfield) (CLuaState *L, int idx, const char *k); +typedef void (*lua_rawget) (CLuaState *L, int idx); +typedef void (*lua_rawgeti) (CLuaState *L, int idx, int n); +typedef void (*lua_createtable) (CLuaState *L, int narr, int nrec); +typedef void *(*lua_newuserdata) (CLuaState *L, size_t sz); +typedef int (*lua_getmetatable) (CLuaState *L, int objindex); +typedef void (*lua_getfenv) (CLuaState *L, int idx); + +typedef void (*lua_settable) (CLuaState *L, int idx); +typedef void (*lua_setfield) (CLuaState *L, int idx, const char *k); +typedef void (*lua_rawset) (CLuaState *L, int idx); +typedef void (*lua_rawseti) (CLuaState *L, int idx, int n); +typedef int (*lua_setmetatable) (CLuaState *L, int objindex); +typedef int (*lua_setfenv) (CLuaState *L, int idx); + +typedef void (*lua_call) (CLuaState *L, int nargs, int nresults); +typedef int (*lua_pcall) (CLuaState *L, int nargs, int nresults, int errfunc); + +typedef int (*luaL_loadbufferx)(CLuaState *L, const char *buff, + size_t sz,const char *name,const char* mode); +typedef int (*luaL_newmetatable) (CLuaState *L, const char *tname); +typedef void (*luaL_checktype) (CLuaState *L, int narg, int t); +typedef size_t (*lua_objlen) (CLuaState *L, int idx); +typedef void (*lua_pushinteger)(CLuaState *L, int n); +typedef int (*lua_tointeger)(CLuaState* L,int idx); + +typedef enum { + OK = 0, + YIELD, + ERRRUN, + ERRSYNTAX, + ERRMEM, + ERRERR +} ret_t; + +typedef enum { + NONE = -1, + NIL, + TBOOLEAN, + LIGHTUSERDATA, + NUMBER, + STRING, + TABLE, + FUNCTION, + USERDATA, + THREAD, +} luatype_t; + +#endif \ No newline at end of file diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..154b6c7 --- /dev/null +++ b/main.cpp @@ -0,0 +1,156 @@ +#include +#include "interface.h" + +#define BALL_NAME "luaL_Ball" +#define BALL_TYPE 195 + +typedef struct { + int mass; + int speed; +} ball_t; + +int refId; + +int TestFunc(CLuaState* L) +{ + L->PushString("Hello World!"); + return 1; +} + +int CallBack(CLuaState* L) +{ + L->CheckType(1,FUNCTION); + L->CheckType(2,NUMBER); + int n = L->GetNumber(2); + + L->Push(1); + L->Call(0,n); + L->Pop(n); + return 0; +} + +int CreateBall(CLuaState* L) +{ + L->CheckType(1,NUMBER); + L->CheckType(2,NUMBER); + ball_t* pBall = new ball_t; + + pBall->mass = L->GetNumber(1); + pBall->speed = L->GetNumber(2); + + userdata_t* pData = L->NewUserData(); + pData->data = pBall; + pData->type = BALL_TYPE; + + L->GetMetaFromRegistry(BALL_NAME); + L->SetMetaTable(-2); + + return 1; +} + +ball_t* CheckBall(CLuaState* L,int idx) +{ + userdata_t* pData = L->GetUserData(idx); + if(pData->type == BALL_TYPE) + return (ball_t*)pData->data; + else + return NULL; +} + +int ball_tostring(CLuaState* L) +{ + ball_t* pBall = CheckBall(L,1); + if(!pBall) + L->PushString("pBall = NULL"); + else + L->PushFString("[Ball with mass %d and speed %d]",pBall->mass,pBall->speed); + return 1; +} + +int ball_eq(CLuaState *L) +{ + ball_t* pBall1 = CheckBall(L,1),*pBall2 = CheckBall(L,2); + L->PushBool(((pBall1->mass == pBall2->mass) && (pBall1->speed == pBall2->speed))); + return 1; +} + +int ball_add(CLuaState *L) +{ + ball_t* pBall1 = CheckBall(L,1),*pBall2 = CheckBall(L,2); + + L->PushCFunction(CreateBall); + L->PushNumber(pBall1->mass + pBall2->mass); + L->PushNumber(pBall1->speed + pBall2->speed); + L->Call(2,1); + + return 1; +} + +int ball_gc(CLuaState* L) +{ + ball_t* pBall = CheckBall(L,1); + delete pBall; + return 0; +} + +int ball_increment(CLuaState* L) +{ + ball_t* pBall = CheckBall(L,1); + pBall->mass++; + pBall->speed++; + return 0; +} + +GMOD_MODULE_OPEN() +{ + L->CreateMetaTable(BALL_NAME); + + L->PushCFunction(ball_tostring); + L->SetField(-2,"__tostring"); + + L->PushCFunction(ball_eq); + L->SetField(-2,"__eq"); + + L->PushCFunction(ball_gc); + L->SetField(-2,"__gc"); + + L->PushCFunction(ball_add); + L->SetField(-2,"__add"); + + L->PushCFunction(ball_increment); + L->SetField(-2,"Increment"); + + L->Push(-1); + L->SetField(-2,"__index"); + + L->CreateTable(); + + L->PushCFunction(TestFunc); + L->SetField(-2,"TestFunc"); + + L->PushCFunction(CallBack); + L->SetField(-2,"CallBack"); + + L->PushCFunction(CreateBall); + L->SetField(-2,"CreateBall"); + + L->SetGlobal("jack"); + + L->PushString("Hello World!"); + refId = L->ReferenceCreate(); + L->Print("Reference ID: %d",refId); + + L->GetGlobal("print"); + L->ReferencePush(refId); + L->Call(1,0); + return 0; +} + +GMOD_MODULE_CLOSE() +{ + L->ReferenceFree(refId); + + L->PushNil(); + L->SetGlobal("jack"); + return 0; +} \ No newline at end of file diff --git a/state.cpp b/state.cpp new file mode 100644 index 0000000..651dad8 --- /dev/null +++ b/state.cpp @@ -0,0 +1,343 @@ +#include "state.h" + +func_t lua_api[] = { + {"lua_gettop",NULL}, //0 + {"lua_settop",NULL}, //1 + {"lua_pushvalue",NULL}, //2 + {"lua_tonumber",NULL}, //3 + {"lua_toboolean",NULL}, //4 + {"lua_tolstring",NULL}, //5 + {"lua_tocfunction",NULL}, //6 + {"lua_touserdata",NULL}, //7 + {"lua_tothread",NULL}, //8 + {"lua_topointer",NULL}, //9 + {"lua_pushnil",NULL}, //10 + {"lua_pushnumber",NULL}, //11 + {"lua_pushlstring",NULL}, //12 + {"lua_pushcclosure",NULL}, //13 + {"lua_pushboolean",NULL}, //14 + {"lua_pushlightuserdata",NULL}, //15 + {"lua_pushthread",NULL}, //16 + {"lua_gettable",NULL}, //17 + {"lua_getfield",NULL}, //18 + {"lua_rawget",NULL}, //19 + {"lua_rawgeti",NULL}, //20 + {"lua_createtable",NULL}, //21 + {"lua_getmetatable",NULL}, //22 + {"lua_newuserdata",NULL}, //23 + {"lua_getfenv",NULL}, //24 + {"lua_settable",NULL}, //25 + {"lua_setfield",NULL}, //26 + {"lua_rawset",NULL}, //27 + {"lua_rawseti",NULL}, //28 + {"lua_setmetatable",NULL}, //29 + {"lua_setfenv",NULL}, //30 + {"lua_call",NULL}, //31 + {"lua_pcall",NULL}, //32 + {"luaL_loadbufferx",NULL}, //33 + {"luaL_newmetatable",NULL}, //34 + {"lua_type",NULL}, //35 + {"lua_typename",NULL}, //36 + {"luaL_checktype",NULL}, //37 + {"lua_objlen",NULL}, //38 + {"lua_pushinteger",NULL}, //39 + {"lua_tointeger",NULL}, //40 + {NULL,NULL} +}; + +CLuaState::CLuaState() +{ +} + +CLuaState::~CLuaState() +{ +} + +int CLuaState::GetTop() +{ + return (((lua_gettop)lua_api[0].ptr))(this); +} + +void CLuaState::SetTop(int n) +{ + (((lua_settop)lua_api[1].ptr))(this,n); +} + +void CLuaState::SetField(int idx,const char* name) +{ + (((lua_setfield)lua_api[26].ptr))(this,idx,name); +} + +void CLuaState::GetField(int idx,const char* name) +{ + (((lua_getfield)lua_api[18].ptr))(this,idx,name); +} + +void CLuaState::Push(int idx) +{ + (((lua_pushvalue)lua_api[2].ptr))(this,idx); +} + +void CLuaState::PushNil() +{ + (((lua_pushnil)lua_api[10].ptr))(this); +} + +void CLuaState::PushBool(bool val) +{ + (((lua_pushboolean)lua_api[14].ptr))(this,val); +} + +void CLuaState::PushNumber(double num) +{ + (((lua_pushnumber)lua_api[11].ptr))(this,num); +} + +void CLuaState::PushInteger(int n) +{ + (((lua_pushinteger)lua_api[39].ptr))(this,n); +} + +void CLuaState::PushCClosure(CLuaFunction fn,int n) +{ + (((lua_pushcclosure)lua_api[13].ptr))(this,fn,n); +} + +void CLuaState::PushString(const char* str,size_t len) +{ + if(!len) + len = strlen(str); + (((lua_pushlstring)lua_api[12].ptr))(this,str,len); +} + +void CLuaState::PushVString(const char* fmt,va_list ap) +{ + char buf[512]; + vsnprintf(buf,512,fmt,ap); + PushString(buf); +} + +void CLuaState::PushFString(const char* fmt,...) +{ + va_list ap; + va_start(ap,fmt); + PushVString(fmt,ap); + va_end(ap); +} + +void CLuaState::PushLightUserData(void* p) +{ + (((lua_pushlightuserdata)lua_api[15].ptr))(this,p); +} + +void CLuaState::PushThread(CLuaState* L) +{ + (((lua_pushthread)lua_api[16].ptr))(L); +} + +bool CLuaState::GetBool(int idx) +{ + return (((lua_toboolean)lua_api[4].ptr))(this,idx); +} + +double CLuaState::GetNumber(int idx) +{ + return (((lua_tonumber)lua_api[3].ptr))(this,idx); +} + +int CLuaState::GetInteger(int idx) +{ + return (((lua_tointeger)lua_api[40].ptr))(this,idx); +} + +const char* CLuaState::GetString(int idx,size_t* len) +{ + return (((lua_tolstring)lua_api[5].ptr))(this,idx,len); +} + +userdata_t* CLuaState::GetUserData(int idx) +{ + return (userdata_t*)(((lua_touserdata)lua_api[7].ptr))(this,idx); +} + +CLuaFunction CLuaState::GetCFunction(int idx) +{ + return (((lua_tocfunction)lua_api[6].ptr))(this,idx); +} + +CLuaState* CLuaState::GetThread(int idx) +{ + return (((lua_tothread)lua_api[8].ptr))(this,idx); +} + +const void* CLuaState::GetPointer(int idx) +{ + return (((lua_topointer)lua_api[9].ptr))(this,idx); +} + +void CLuaState::CreateTable() +{ + (((lua_createtable)lua_api[21].ptr))(this,0,0); +} + +void CLuaState::GetTable(int idx) +{ + (((lua_gettable)lua_api[17].ptr))(this,idx); +} + +void CLuaState::SetTable(int idx) +{ + (((lua_settable)lua_api[25].ptr))(this,idx); +} + +void CLuaState::RawGet(int idx) +{ + (((lua_rawget)lua_api[19].ptr))(this,idx); +} + +void CLuaState::RawGeti(int idx,int n) +{ + (((lua_rawgeti)lua_api[20].ptr))(this,idx,n); +} + +void CLuaState::RawSet(int idx) +{ + (((lua_rawset)lua_api[27].ptr))(this,idx); +} + +void CLuaState::RawSeti(int idx,int n) +{ + (((lua_rawseti)lua_api[28].ptr))(this,idx,n); +} + +void CLuaState::GetEnvironment(int idx) +{ + (((lua_getfenv)lua_api[24].ptr))(this,idx); +} + +void CLuaState::SetEnvironment(int idx) +{ + (((lua_setfenv)lua_api[30].ptr))(this,idx); +} + +userdata_t* CLuaState::NewUserData() +{ + return (userdata_t*)(((lua_newuserdata)lua_api[23].ptr))(this,sizeof(userdata_t)); +} + +void CLuaState::CreateMetaTable(const char* name) +{ + (((luaL_newmetatable)lua_api[34].ptr))(this,name); +} + +int CLuaState::GetMetaTable(int idx) +{ + return (((lua_getmetatable)lua_api[22].ptr))(this,idx); +} + +int CLuaState::SetMetaTable(int idx) +{ + return (((lua_setmetatable)lua_api[29].ptr))(this,idx); +} + +luatype_t CLuaState::Type(int idx) +{ + return (luatype_t)(((lua_type)lua_api[35].ptr))(this,idx); +} + +const char* CLuaState::TypeName(int idx) +{ + return (((lua_typename)lua_api[36].ptr))(this,Type(idx)); +} + +void CLuaState::CheckType(int idx,luatype_t tp) +{ + (((luaL_checktype)lua_api[37].ptr))(this,idx,tp); +} + +void CLuaState::Call(int nargs,int nret) +{ + (((lua_call)lua_api[31].ptr))(this,nargs,nret); +} + +int CLuaState::PCall(int nargs,int nret,int errfunc) +{ + return (((lua_pcall)lua_api[32].ptr))(this,nargs,nret,errfunc); +} + +void CLuaState::Print(const char* msg,...) +{ + GetGlobal("print"); + va_list ap; + va_start(ap,msg); + PushVString(msg,ap); + va_end(ap); + Call(1,0); +} + +int CLuaState::ObjLen(int idx) +{ + return (((lua_objlen)lua_api[38].ptr))(this,idx); +} + +int CLuaState::ReferenceCreate() +{ + int len = ObjLen(-1); + len++; + PushInteger(len); + Push(-2); + SetTable(LUA_REGISTRYINDEX); + Pop(); + return len; +} + +void CLuaState::ReferencePush(int refId) +{ + PushInteger(refId); + GetTable(LUA_REGISTRYINDEX); +} + +void CLuaState::ReferenceFree(int refId) +{ + PushInteger(refId); + PushNil(); + SetTable(LUA_REGISTRYINDEX); +} + +const char* CLuaState::RunString(const char* name,const char* buf,size_t len) +{ + const char* err = NULL; + if( (((luaL_loadbufferx)lua_api[33].ptr))(this,buf,len,name,NULL) != OK) + { + err = GetString(-1); + Pop(); + } + else + PCall(0,0,0); + return err; +} + +BOOL WINAPI ConnectLuaShared() +{ + HMODULE hLuaShared = NULL; + char szDebugInfo[128]; + if(!(hLuaShared = GetModuleHandle("lua_shared.dll"))) + return FALSE; + for(func_t* i = lua_api; i->name; i++) + { + if(!(i->ptr = (void*)GetProcAddress(hLuaShared,i->name))) + { + _snprintf(szDebugInfo,128,"GetProcAddress failed at %s",i->name); + MessageBoxA(NULL,szDebugInfo,"ConnectLuaShared",MB_ICONHAND); + return FALSE; + } + } + return TRUE; +} + +BOOL APIENTRY DllMain(HINSTANCE hDll,DWORD fdwReason,LPVOID lpReserved) +{ + if(fdwReason == DLL_PROCESS_ATTACH) + return ConnectLuaShared(); + return TRUE; +} \ No newline at end of file diff --git a/state.h b/state.h new file mode 100644 index 0000000..ffbc529 --- /dev/null +++ b/state.h @@ -0,0 +1,102 @@ +#ifndef __STATE_H +#define __STATE_H + +#include "lua.h" +#include +#include +#include + +typedef struct { + const char* name; + void* ptr; +} func_t; + +typedef struct { + void* data; + unsigned char type; +} userdata_t; + +class CLuaState +{ +public: + CLuaState(); + ~CLuaState(); + + int GetTop(); + void SetTop(int n); + + void SetField(int idx,const char* name); + void GetField(int idx,const char* name); + + inline void GetGlobal(const char* name){GetField(LUA_GLOBALSINDEX,name);} + inline void SetGlobal(const char* name){SetField(LUA_GLOBALSINDEX,name);} + inline void GetMetaFromRegistry(const char* name){GetField(LUA_REGISTRYINDEX,name);} + + void Push(int idx); + void Pop(int amt = 1){SetTop(-(amt)-1);} + + void PushNil(); + void PushBool(bool val); + void PushNumber(double num); + void PushInteger(int n); + void PushCClosure(CLuaFunction fn,int n); + inline void PushCFunction(CLuaFunction fn){PushCClosure(fn,0);} + void PushString(const char* str,size_t len = 0); + void PushVString(const char* fmt,va_list ap); + void PushFString(const char* fmt,...); + void PushLightUserData(void* p); + void PushThread(CLuaState* L); + + bool GetBool(int idx); + double GetNumber(int idx); + int GetInteger(int idx); + const char* GetString(int idx,size_t* len = NULL); + userdata_t* GetUserData(int idx); + CLuaFunction GetCFunction(int idx); + CLuaState* GetThread(int idx); + const void* GetPointer(int idx); + + void CreateTable(); + void GetTable(int idx); + void SetTable(int idx); + + void RawGet(int idx); + void RawGeti(int idx,int n); + void RawSet(int idx); + void RawSeti(int idx,int n); + + void GetEnvironment(int idx); + void SetEnvironment(int idx); + + userdata_t* NewUserData(); + void CreateMetaTable(const char* name); + int GetMetaTable(int idx); + int SetMetaTable(int idx); + + luatype_t Type(int idx); + const char* TypeName(int idx); + void CheckType(int idx,luatype_t tp); + + void Call(int nargs,int nret); + int PCall(int nargs,int nret,int errfunc); + + int ObjLen(int idx); + int ReferenceCreate(); + void ReferencePush(int refId); + void ReferenceFree(int refId); + + void Print(const char*,...); + const char* RunString(const char* name,const char* buf,size_t len); + + inline bool IsNil(int idx){return (Type(idx) == NIL);} + inline bool IsBool(int idx){return (Type(idx) == TBOOLEAN);} + inline bool IsLightUserData(int idx){return (Type(idx) == LIGHTUSERDATA);} + inline bool IsNumber(int idx){return (Type(idx) == NUMBER);} + inline bool IsString(int idx){return (Type(idx) == STRING);} + inline bool IsTable(int idx){return (Type(idx) == TABLE);} + inline bool IsFunction(int idx){return (Type(idx) == FUNCTION);} + inline bool IsUserData(int idx){return (Type(idx) == USERDATA);} + inline bool IsThread(int idx){return (Type(idx) == THREAD);} +}; + +#endif \ No newline at end of file