139 lines
2.8 KiB
C++
Executable File
139 lines
2.8 KiB
C++
Executable File
/* Copyright (C) 2015 Graham Sanderson, All Rights Reserved */
|
|
#include "extdll.h"
|
|
#include "eiface.h"
|
|
#include "util.h"
|
|
#include "game.h"
|
|
#include "gslua.h"
|
|
|
|
#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 },
|
|
{ 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)
|
|
{
|
|
g_L = luaL_newstate();
|
|
g_bLuaInitialized = TRUE;
|
|
ALERT(at_console, "[HLua] Lua initialized.\n");
|
|
|
|
LuaRegisterFunctions();
|
|
LuaRegisterGlobals();
|
|
|
|
if(luaL_dostring(g_L, "Print_Console(at_console, \"[HLua::Lua] Initialization success!\");") == TRUE)
|
|
{
|
|
LuaError("[HLua] Failed initial Lua test. Stopping Lua.\n");
|
|
}
|
|
}
|
|
|
|
void LuaQuit(void)
|
|
{
|
|
lua_close(g_L);
|
|
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!");
|
|
}
|
|
}
|
|
|
|
// --- 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, 2);
|
|
ALERT_TYPE eType = (ALERT_TYPE)luaL_checkinteger(L, 1);
|
|
LuaCheckNull(szToWrite);
|
|
ALERT(eType, "%s\n", szToWrite);
|
|
return 0;
|
|
}
|