// BlockLua (bllua4): Simple Lua interface for TorqueScript // Includes #ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN #endif #include #include #include "BlFuncs.cpp" #include "BlHooks.cpp" #include "lua.hpp" #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_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); #ifndef BLLUA_UNSAFE BLL_LOAD_LUA(gL, bll_fileLuaEnvSafe); #endif // 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); // Load utilities BLL_LOAD_LUA(gL, bll_fileLuaStd); BLL_LOAD_LUA(gL, bll_fileLuaVector); BLL_LOAD_LUA(gL, bll_fileLuaLibts); BlEval(bll_fileTsLibts); BLL_LOAD_LUA(gL, bll_fileLuaLibbl); BLL_LOAD_LUA(gL, bll_fileLuaLibblTypes); BlEval(bll_fileTsLibblSupport); BlEval(bll_fileLoadaddons); BlEval("$_bllua_active = 1;"); BlPrintf(" BlockLua: Done Loading"); return true; } bool deinit() { BlPrintf("BlockLua: Unloading"); BlEval("deactivatePackage(_bllua_main);"); BlEval("$_bllua_active = 0;"); 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; } }