From 19a339e2e044b0d20ae7c31a21cbfcae30b6e1e9 Mon Sep 17 00:00:00 2001 From: Marrub Date: Tue, 10 Mar 2015 12:06:42 -0400 Subject: [PATCH] Lua scripts can now be run via a list --- dlls/extdll.h | 1 + dlls/gslua.cpp | 83 ++++++++++++++++++++++++++++++++++++++++++-------- dlls/gslua.h | 3 +- 3 files changed, 73 insertions(+), 14 deletions(-) diff --git a/dlls/extdll.h b/dlls/extdll.h index d42571b..d3d056b 100644 --- a/dlls/extdll.h +++ b/dlls/extdll.h @@ -31,6 +31,7 @@ #pragma warning(disable : 4201) // nameless struct/union #pragma warning(disable : 4514) // unreferenced inline function removed #pragma warning(disable : 4100) // unreferenced formal parameter +#define _CRT_SECURE_NO_WARNINGS 1 // [marrub] Stop it, VS2010. #include "archtypes.h" // DAL diff --git a/dlls/gslua.cpp b/dlls/gslua.cpp index 9719448..82fc9d8 100755 --- a/dlls/gslua.cpp +++ b/dlls/gslua.cpp @@ -49,6 +49,8 @@ static gsLuaRegGlobal const gsLib_GLOBALS[] = { // --- Init/quit --- +#define LuaError(...) (ALERT(at_error, __VA_ARGS__), g_bLuaInitialized = FALSE, LuaQuit(), g_L = NULL) + void LuaInit(void) { g_L = luaL_newstate(); @@ -61,7 +63,10 @@ void LuaInit(void) if(luaL_dostring(g_L, "Print_Console(at_console, \"[HLua::Lua] Initialization success!\");") == TRUE) { LuaError("[HLua] Failed initial Lua test. Stopping Lua.\n"); + return; } + + LuaParseScripts(); } void LuaQuit(void) @@ -70,22 +75,74 @@ void LuaQuit(void) ALERT(at_console, "[HLua] Lua shutdown.\n"); } -// --- Error functions --- - -void LuaError(char *szFmt) -{ - char const *szFmtC = (const char *)szFmt; - ALERT(at_error, szFmt); - g_bLuaInitialized = FALSE; - LuaQuit(); - g_L = NULL; -} - void LuaCheckNull(void const *vpToCheck) { if (vpToCheck == NULL) { - LuaError("[HLua] Null pointer exception!"); + LuaError("[HLua] Null pointer exception!\n"); + } +} + +// --- Misc functions --- +void LuaRunScript(char *szFileName) +{ + if(luaL_dofile(g_L, szFileName) == TRUE) + { + LuaError("[HLua] Failed to run script %s\nLua: %s\n", szFileName, lua_tostring(g_L, -1)); + } +} + +void LuaParseScripts(void) +{ + FILE *fScripts; + char szLn[1024]; + char szFilenamePulledOutOfMyAss[MAX_PATH]; + char szAlsoPulledOutOfMyAss[MAX_PATH]; + + GET_GAME_DIR(szAlsoPulledOutOfMyAss); + strcat(szAlsoPulledOutOfMyAss, "/lua"); + CreateDirectory(szAlsoPulledOutOfMyAss, NULL); + strcpy(szFilenamePulledOutOfMyAss, szAlsoPulledOutOfMyAss); + strcat(szFilenamePulledOutOfMyAss, "/scripts.txt"); + + fScripts = fopen(szFilenamePulledOutOfMyAss, "r"); + + if(fScripts == NULL) + { + LuaError("[HLua] Failed to open lua/scripts.txt!\n"); + return; + } + + while(fgets(szLn, 1024, fScripts) != NULL) + { + BOOL bContinue = FALSE; + if(szLn[0] == '\r' || szLn[0] == '\n') continue; + + char ch = szLn[0], pvch = szLn[0]; + for(int i = 1; ch != '\0'; pvch = ch, ch = szLn[i++]) + { + if(i == 1) continue; + + if(pvch == '/' && ch == '/') + { + bContinue = TRUE; + break; + } + + if(ch == '\n' || ch == '\r') + { + szLn[i-1] = '\0'; + break; + } + } + + if(bContinue == TRUE) continue; + + char szSeriouslyIHaveALotOfThingsInMyAss[MAX_PATH]; + strcpy(szSeriouslyIHaveALotOfThingsInMyAss, szAlsoPulledOutOfMyAss); + strcat(szSeriouslyIHaveALotOfThingsInMyAss, szLn); + + LuaRunScript(szSeriouslyIHaveALotOfThingsInMyAss); } } @@ -119,7 +176,7 @@ void LuaRegisterGlobals(void) { 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; + default: LuaError("[HLua] Invalid type in global initializer!\n"); return; } lua_setglobal(g_L, gsLib_GLOBALS[i].name); diff --git a/dlls/gslua.h b/dlls/gslua.h index 192c07e..3807ee1 100755 --- a/dlls/gslua.h +++ b/dlls/gslua.h @@ -5,9 +5,10 @@ void LuaInit(void); void LuaQuit(void); -void LuaError(char *szFmt); void LuaCheckNull(void const *vpToCheck); void LuaRegisterFunctions(void); void LuaRegisterGlobals(void); +void LuaParseScripts(void); +void LuaRunScript(char *szFilename); int LUA_PrintConsole(lua_State *L);