Files
BlockLua/src/bllua4.cpp

111 lines
2.8 KiB
C++

// BlockLua (bllua4): Advanced Lua interface for TorqueScript
// Includes
#include <Windows.h>
#include <Psapi.h>
#include "lua.hpp"
#include "BlHooks.cpp"
#include "BlFuncs.cpp"
#include "luainterp.cpp"
#include "lualibts.cpp"
lua_State* gL;
#include "tsliblua.cpp"
// Global variables
// Setup
// Hack to encode the contents of text files as static strings
#define INCLUDE_BIN(varname, filename) \
asm("_" #varname ": .incbin \"" filename "\""); \
asm(".byte 0"); \
extern char varname[];
INCLUDE_BIN(bll_fileLuaEnvSafe, "lua-env-safe.lua");
INCLUDE_BIN(bll_fileLuaEnv , "lua-env.lua");
INCLUDE_BIN(bll_fileTsEnv , "ts-env.cs" );
INCLUDE_BIN(bll_fileLuaStd , "util/std.lua");
INCLUDE_BIN(bll_fileLuaVector , "util/vector.lua");
INCLUDE_BIN(bll_fileLuaMatrix , "util/matrix.lua");
INCLUDE_BIN(bll_fileLuaLibts , "util/libts-lua.lua");
INCLUDE_BIN(bll_fileTsLibts , "util/libts-ts.cs");
INCLUDE_BIN(bll_fileLuaLibbl , "util/libbl.lua" );
INCLUDE_BIN(bll_fileLuaLibblTypes , "util/libbl-types.lua");
INCLUDE_BIN(bll_fileTsLibblSupport, "util/libbl-support.cs");
INCLUDE_BIN(bll_fileLoadaddons , "util/loadaddons.cs");
#define BLL_LOAD_LUA(lstate, vname) \
if(!bll_LuaEval(lstate, vname)) { \
BlPrintf(" Error executing " #vname); \
return false; \
}
bool init() {
BlHooksInit();
BlPrintf("BlockLua: Loading");
BlFuncsInit();
// Initialize Lua environment
gL = lua_open();
luaL_openlibs(gL);
// Expose TS API to Lua
llibbl_init(gL);
// Set up Lua environment
BLL_LOAD_LUA(gL, bll_fileLuaEnv);
#ifdef BLLUA_ALLOWFFI
lua_pushboolean(gL, true);
lua_setglobal(gL, "_bllua_allowffi");
#endif
#ifndef BLLUA_UNSAFE
BLL_LOAD_LUA(gL, bll_fileLuaEnvSafe);
#endif
// Load utilities in Lua
BLL_LOAD_LUA(gL, bll_fileLuaStd);
BLL_LOAD_LUA(gL, bll_fileLuaVector);
BLL_LOAD_LUA(gL, bll_fileLuaMatrix);
BLL_LOAD_LUA(gL, bll_fileLuaLibts);
BLL_LOAD_LUA(gL, bll_fileLuaLibbl);
BLL_LOAD_LUA(gL, bll_fileLuaLibblTypes);
// Expose Lua API to TS
BlAddFunction(NULL, NULL, "_bllua_luacall", bll_ts_luacall, "LuaCall(name, ...) - Call Lua function and return result", 2, 20);
BlEval(bll_fileTsEnv);
BlEval(bll_fileTsLibts);
BlEval(bll_fileTsLibblSupport);
BlEval(bll_fileLoadaddons);
BlEval("$_bllua_active = 1;");
BlPrintf(" BlockLua: Done Loading");
return true;
}
bool deinit() {
BlPrintf("BlockLua: Unloading");
BlEval("$_bllua_active=0;deactivatePackage(_bllua_main);");
bll_LuaEval(gL, "for _,f in pairs(_bllua_on_unload) do f() end");
lua_close(gL);
BlHooksDeinit();
BlFuncsDeinit();
BlPrintf(" BlockLua: Done Unloading");
return true;
}
bool __stdcall DllMain(HINSTANCE hinstance, DWORD reason, void* reserved) {
switch(reason) {
case DLL_PROCESS_ATTACH: return init();
case DLL_PROCESS_DETACH: return deinit();
default : return true;
}
}