Lua scripts can now be run via a list

master
Marrub 2015-03-10 12:06:42 -04:00
parent a4f7cd669d
commit 19a339e2e0
3 changed files with 73 additions and 14 deletions

View File

@ -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

View File

@ -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);

View File

@ -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);