From a4f7cd669d0426b485aeeaa68215d12bc5005e64 Mon Sep 17 00:00:00 2001 From: Marrub Date: Tue, 10 Mar 2015 10:55:52 -0400 Subject: [PATCH] Varied changes, global system, better Print_Console --- dlls/gslua.cpp | 71 +++++++++++++++++++++++++++++++++++++++++++++----- dlls/gslua.h | 2 ++ 2 files changed, 66 insertions(+), 7 deletions(-) diff --git a/dlls/gslua.cpp b/dlls/gslua.cpp index f862822..9719448 100755 --- a/dlls/gslua.cpp +++ b/dlls/gslua.cpp @@ -8,15 +8,45 @@ #include "lua/lua.hpp" #define MAX_LUA_LIB_FUNCS 200 +#define MAX_LUA_LIB_GLOBALS 200 extern lua_State *g_L; extern BOOL g_bLuaInitialized; static const luaL_Reg gsLib_METHODS[] = { - { "_Print_Console", LUA_PrintConsole }, + { "Print_Console", LUA_PrintConsole }, { NULL, NULL } }; +typedef enum +{ + LTYPE_INT, + LTYPE_STR +} gsLuaType; + +typedef union +{ + int i; + const char *str; +} gsLuaGlobal; + +typedef struct gsLuaRegGlobal +{ + const char *name; + gsLuaGlobal value; + gsLuaType type; +} gsLuaRegGlobal; + +static gsLuaRegGlobal const gsLib_GLOBALS[] = { + { "at_notice", at_notice, LTYPE_INT }, + { "at_console", at_console, LTYPE_INT }, + { "at_aiconsole", at_aiconsole, LTYPE_INT }, + { "at_warning", at_warning, LTYPE_INT }, + { "at_error", at_error, LTYPE_INT }, + { "at_logged", at_logged, LTYPE_INT }, + { NULL } +}; + // --- Init/quit --- void LuaInit(void) @@ -26,8 +56,9 @@ void LuaInit(void) ALERT(at_console, "[HLua] Lua initialized.\n"); LuaRegisterFunctions(); + LuaRegisterGlobals(); - if(luaL_dostring(g_L, "_Print_Console(\"[HLua::Lua] Initialization success!\");") == TRUE) + if(luaL_dostring(g_L, "Print_Console(at_console, \"[HLua::Lua] Initialization success!\");") == TRUE) { LuaError("[HLua] Failed initial Lua test. Stopping Lua.\n"); } @@ -43,7 +74,7 @@ void LuaQuit(void) void LuaError(char *szFmt) { - char const * szFmtC = (const char *) szFmt; + char const *szFmtC = (const char *)szFmt; ALERT(at_error, szFmt); g_bLuaInitialized = FALSE; LuaQuit(); @@ -54,28 +85,54 @@ void LuaCheckNull(void const *vpToCheck) { if (vpToCheck == NULL) { - LuaError ("[HLua] Null pointer exception!"); + LuaError("[HLua] Null pointer exception!"); } } -// --- Binded lua functions --- +// --- Binded lua functions and globals --- void LuaRegisterFunctions(void) { int i; ALERT(at_console, "[HLua] Registering functions.\n"); + for(i = 0; i < MAX_LUA_LIB_FUNCS; i++) { if(gsLib_METHODS[i].name == NULL && gsLib_METHODS[i].func == NULL) break; + lua_register(g_L, gsLib_METHODS[i].name, gsLib_METHODS[i].func); } + ALERT(at_console, "[HLua] Register success, got %d functions.\n", i); } +void LuaRegisterGlobals(void) +{ + int i; + ALERT(at_console, "[HLua] Registering globals.\n"); + + for(i = 0; i < MAX_LUA_LIB_GLOBALS; i++) + { + if(gsLib_GLOBALS[i].name == NULL) break; + + switch(gsLib_GLOBALS[i].type) + { + case LTYPE_INT: lua_pushnumber(g_L, gsLib_GLOBALS[i].value.i); break; + case LTYPE_STR: lua_pushstring(g_L, gsLib_GLOBALS[i].value.str); break; + default: LuaError("[HLua] Invalid type in global initializer!"); return; + } + + lua_setglobal(g_L, gsLib_GLOBALS[i].name); + } + + ALERT(at_console, "[HLua] Global registry success, put out %d globals.\n", i); +} + int LUA_PrintConsole(lua_State *L) { - const char *szToWrite = luaL_checkstring(L, 1); + const char *szToWrite = luaL_checkstring(L, 2); + ALERT_TYPE eType = (ALERT_TYPE)luaL_checkinteger(L, 1); LuaCheckNull(szToWrite); - ALERT(at_console, "%s\n", szToWrite); + ALERT(eType, "%s\n", szToWrite); return 0; } diff --git a/dlls/gslua.h b/dlls/gslua.h index e3e8c5b..192c07e 100755 --- a/dlls/gslua.h +++ b/dlls/gslua.h @@ -8,4 +8,6 @@ void LuaQuit(void); void LuaError(char *szFmt); void LuaCheckNull(void const *vpToCheck); void LuaRegisterFunctions(void); +void LuaRegisterGlobals(void); int LUA_PrintConsole(lua_State *L); +