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 b5454bc
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 0 deletions.
11 changes: 11 additions & 0 deletions 3rdParty/Lua/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
include(functions/FetchContent_MakeAvailableExcludeFromAll)

include(FetchContent)
FetchContent_Declare(Lua
URL https://github.com/walterschell/Lua/archive/88246d621abf7b6fba9332f49229d507f020e450.tar.gz
URL_HASH MD5=03b76927cb5341ffc53bea12c37ddcca
)
FetchContent_MakeAvailableExcludeFromAll(Lua)

# 3DS, Switch, Vita, and Xbox do not appear to support 64bit types
target_compile_definitions(lua_static PRIVATE -DLUA_C89_NUMBERS=0)
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
51 changes: 51 additions & 0 deletions Source/utils/lua.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "utils/lua.hpp"

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

#include "plrmsg.h"

namespace devilution {

lua_State *LuaState;

int LuaPlayerMessage(lua_State *state)
{
if (!lua_isstring(LuaState, 1))
return 0;

std::string msg = lua_tostring(state, 1);
EventPlrMsg(msg, UiFlags::ColorRed);

return 1;
}

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()
{
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

0 comments on commit b5454bc

Please sign in to comment.