Skip to content

Commit

Permalink
Add Lua support
Browse files Browse the repository at this point in the history
  • Loading branch information
AJenbo committed Oct 7, 2023
1 parent e18b37a commit f191e6c
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 0 deletions.
19 changes: 19 additions & 0 deletions 3rdParty/Lua/CMakeLists.txt
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)
target_compile_definitions(lua_static PUBLIC -DLUA_USE_C89)
elseif(IOS)
target_compile_definitions(lua_static PUBLIC -DLUA_USE_IOS)
endif()
2 changes: 2 additions & 0 deletions CMake/Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ if(SUPPORTS_MPQ)
endif()
endif()

add_subdirectory(3rdParty/Lua)

if(SCREEN_READER_INTEGRATION)
if(WIN32)
add_subdirectory(3rdParty/tolk)
Expand Down
3 changes: 3 additions & 0 deletions Source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ set(libdevilutionx_SRCS
utils/format_int.cpp
utils/language.cpp
utils/logged_fstream.cpp
utils/lua.cpp
utils/paths.cpp
utils/parse_int.cpp
utils/pcx_to_clx.cpp
Expand Down Expand Up @@ -287,6 +288,8 @@ if(DISCORD_INTEGRATION)
target_link_libraries(libdevilutionx PRIVATE discord discord_game_sdk)
endif()

target_link_libraries(libdevilutionx PRIVATE lua_static)

if(SCREEN_READER_INTEGRATION AND WIN32)
target_compile_definitions(libdevilutionx PRIVATE Tolk)
endif()
Expand Down
4 changes: 4 additions & 0 deletions Source/diablo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
#include "utils/console.h"
#include "utils/display.h"
#include "utils/language.h"
#include "utils/lua.hpp"
#include "utils/parse_int.hpp"
#include "utils/paths.h"
#include "utils/screen_reader.hpp"
Expand Down Expand Up @@ -822,6 +823,7 @@ void RunGameLoop(interface_mode uMsg)
nthread_ignore_mutex(false);

discord_manager::StartGame();
LuaEvent("GameStartedEvent");
#ifdef GPERF_HEAP_FIRST_GAME_ITERATION
unsigned run_game_iteration = 0;
#endif
Expand Down Expand Up @@ -1230,6 +1232,7 @@ void DiabloDeinit()
{
FreeItemGFX();

LuaShutdown();
ShutDownScreenReader();

if (gbSndInited)
Expand Down Expand Up @@ -2449,6 +2452,7 @@ int DiabloMain(int argc, char **argv)
LoadLanguageArchive();

ApplicationInit();
LuaInitialize();
SaveOptions();

// Finally load game data
Expand Down
57 changes: 57 additions & 0 deletions Source/utils/lua.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "utils/lua.hpp"

extern "C" {
#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>
}

#include "plrmsg.h"

namespace devilution {

namespace {

lua_State *LuaState;

int LuaPlayerMessage(lua_State *state)
{
if (lua_isstring(LuaState, 1)) {
std::string msg = lua_tostring(state, 1);
EventPlrMsg(msg, UiFlags::ColorRed);
}

return 0;
}

} // namespace

void LuaInitialize()
{
LuaState = luaL_newstate();
lua_register(LuaState, "PlayerMessage", LuaPlayerMessage);

int status = luaL_dofile(LuaState, "init.lua");
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 = lua_pcall(LuaState, 0, 0, 0);
if (status != LUA_OK)
SDL_Log("%s", lua_tostring(LuaState, -1));
}

} // namespace devilution
11 changes: 11 additions & 0 deletions Source/utils/lua.hpp
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
2 changes: 2 additions & 0 deletions test/timedemo_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "pfile.h"
#include "playerdat.hpp"
#include "utils/display.h"
#include "utils/lua.hpp"
#include "utils/paths.h"

using namespace devilution;
Expand All @@ -33,6 +34,7 @@ void RunTimedemo(std::string timedemoFolderName)

InitKeymapActions();
LoadOptions();
LuaInitialize();

const int demoNumber = 0;

Expand Down

0 comments on commit f191e6c

Please sign in to comment.