Varied changes, global system, better Print_Console

master
Marrub 2015-03-10 10:55:52 -04:00
parent faa0d3747c
commit a4f7cd669d
2 changed files with 66 additions and 7 deletions

View File

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

View File

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