-
Notifications
You must be signed in to change notification settings - Fork 814
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
187 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
include(functions/FetchContent_MakeAvailableExcludeFromAll) | ||
|
||
set(LUA_ENABLE_TESTING OFF) | ||
set(LUA_BUILD_COMPILER OFF) | ||
|
||
include(FetchContent) | ||
FetchContent_Declare(Lua | ||
URL https://github.com/walterschell/Lua/archive/88246d621abf7b6fba9332f49229d507f020e450.tar.gz | ||
URL_HASH MD5=03b76927cb5341ffc53bea12c37ddcca | ||
) | ||
FetchContent_MakeAvailableExcludeFromAll(Lua) | ||
|
||
if(ANDROID AND ("${ANDROID_ABI}" STREQUAL "armeabi-v7a" OR "${ANDROID_ABI}" STREQUAL "x86")) | ||
target_compile_definitions(lua_internal INTERFACE -DLUA_USE_C89) | ||
elseif(NINTENDO_3DS OR VITA OR NINTENDO_SWITCH OR NXDK) | ||
target_compile_definitions(lua_static PUBLIC -DLUA_USE_C89) | ||
elseif(IOS) | ||
target_compile_definitions(lua_static PUBLIC -DLUA_USE_IOS) | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
#include "utils/lua.hpp" | ||
|
||
#include <string_view> | ||
|
||
extern "C" { | ||
#include <lauxlib.h> | ||
#include <lua.h> | ||
#include <lualib.h> | ||
} | ||
|
||
#include "engine/assets.hpp" | ||
#include "plrmsg.h" | ||
#include "utils/console.h" | ||
|
||
namespace devilution { | ||
|
||
namespace { | ||
|
||
lua_State *LuaState; | ||
|
||
int LuaErrorHandler(lua_State *state) | ||
{ | ||
const char *msg = lua_tostring(state, -1); | ||
SDL_Log("[Lua Error] %s", msg); | ||
return 1; | ||
} | ||
|
||
// Helper function for protected Lua calls with error handler | ||
int SafeLuaCall(int nargs, int nresults) | ||
{ | ||
// Push the error handler function onto the stack | ||
lua_pushcfunction(LuaState, LuaErrorHandler); | ||
lua_insert(LuaState, -2 - nargs); | ||
int status = lua_pcall(LuaState, nargs, nresults, -2 - nargs); | ||
lua_remove(LuaState, -1); // remove error handler | ||
return status; | ||
} | ||
|
||
int LuaPrint(lua_State *state) | ||
{ | ||
int nargs = lua_gettop(state); | ||
if (nargs >= 1 && lua_isstring(state, 1)) { | ||
std::string msg = lua_tostring(state, 1); | ||
msg += "\n"; | ||
printInConsole(msg); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
int LuaPlayerMessage(lua_State *state) | ||
{ | ||
int nargs = lua_gettop(state); | ||
if (nargs >= 1 && lua_isstring(state, 1)) { | ||
std::string_view msg = lua_tostring(state, 1); | ||
EventPlrMsg(msg, UiFlags::ColorRed); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
} // namespace | ||
|
||
void LuaInitialize() | ||
{ | ||
LuaState = luaL_newstate(); | ||
|
||
// Load liberies | ||
luaL_requiref(LuaState, LUA_GNAME, luaopen_base, 1); | ||
lua_pop(LuaState, 1); | ||
luaL_requiref(LuaState, LUA_LOADLIBNAME, luaopen_package, 1); | ||
lua_pop(LuaState, 1); | ||
luaL_requiref(LuaState, LUA_COLIBNAME, luaopen_coroutine, 1); | ||
lua_pop(LuaState, 1); | ||
luaL_requiref(LuaState, LUA_TABLIBNAME, luaopen_table, 1); | ||
lua_pop(LuaState, 1); | ||
luaL_requiref(LuaState, LUA_STRLIBNAME, luaopen_string, 1); | ||
lua_pop(LuaState, 1); | ||
luaL_requiref(LuaState, LUA_MATHLIBNAME, luaopen_math, 1); | ||
lua_pop(LuaState, 1); | ||
luaL_requiref(LuaState, LUA_UTF8LIBNAME, luaopen_utf8, 1); | ||
lua_pop(LuaState, 1); | ||
|
||
#ifdef _DEBUG | ||
luaL_requiref(LuaState, LUA_DBLIBNAME, luaopen_debug, 1); | ||
lua_pop(LuaState, 1); | ||
#endif | ||
|
||
// Registering globals | ||
lua_register(LuaState, "print", LuaPrint); | ||
lua_pushstring(LuaState, LUA_VERSION); | ||
lua_setglobal(LuaState, "_VERSION"); | ||
|
||
// Registering devilutionx object table | ||
lua_newtable(LuaState); | ||
lua_pushcfunction(LuaState, LuaPlayerMessage); | ||
lua_setfield(LuaState, -2, "message"); | ||
lua_setglobal(LuaState, "devilutionx"); | ||
|
||
// Run base script | ||
AssetRef ref = FindAsset("init.lua"); | ||
if (!ref.ok()) | ||
return; | ||
|
||
const size_t size = ref.size(); | ||
std::unique_ptr<char[]> luaScript { new char[size + 1] }; | ||
|
||
AssetHandle handle = OpenAsset(std::move(ref)); | ||
if (!handle.ok()) | ||
return; | ||
|
||
if (size > 0 && !handle.read(luaScript.get(), size)) | ||
return; | ||
|
||
luaScript[size] = '\0'; // Terminate string | ||
|
||
int status = luaL_loadstring(LuaState, luaScript.get()); | ||
if (status == LUA_OK) | ||
status = SafeLuaCall(0, 0); // protected call with error handler | ||
if (status != LUA_OK) | ||
SDL_Log("%s", lua_tostring(LuaState, -1)); | ||
} | ||
|
||
void LuaShutdown() | ||
{ | ||
if (LuaState == nullptr) | ||
return; | ||
|
||
lua_close(LuaState); | ||
} | ||
|
||
void LuaEvent(std::string name) | ||
{ | ||
lua_getglobal(LuaState, name.c_str()); | ||
if (!lua_isfunction(LuaState, -1)) | ||
return; | ||
int status = SafeLuaCall(0, 0); | ||
if (status != LUA_OK) | ||
SDL_Log("%s", lua_tostring(LuaState, -1)); | ||
} | ||
|
||
} // namespace devilution |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
namespace devilution { | ||
|
||
void LuaInitialize(); | ||
void LuaShutdown(); | ||
void LuaEvent(std::string name); | ||
|
||
} // namespace devilution |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters