Skip to content

Commit

Permalink
Merge pull request #280 from wheremyfoodat/lua
Browse files Browse the repository at this point in the history
Embed LuaJIT part 1
  • Loading branch information
wheremyfoodat authored Sep 17, 2023
2 parents c0e3d6d + b908f3e commit 30cf9df
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@
[submodule "third_party/discord-rpc"]
path = third_party/discord-rpc
url = https://github.com/Panda3DS-emu/discord-rpc
[submodule "third_party/LuaJIT"]
path = third_party/LuaJIT
url = https://github.com/Panda3DS-emu/LuaJIT
22 changes: 20 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ option(ENABLE_LTO "Enable link-time optimization" OFF)
option(ENABLE_USER_BUILD "Make a user-facing build. These builds have various assertions disabled, LTO, and more" OFF)
option(ENABLE_HTTP_SERVER "Enable HTTP server. Used for Discord bot support" OFF)
option(ENABLE_DISCORD_RPC "Compile with Discord RPC support (disabled by default)" ON)
option(ENABLE_LUAJIT "Enable scripting with the Lua programming language" ON)

include_directories(${PROJECT_SOURCE_DIR}/include/)
include_directories(${PROJECT_SOURCE_DIR}/include/kernel)
Expand Down Expand Up @@ -85,6 +86,18 @@ set(CRYPTOPP_BUILD_TESTING OFF)
add_subdirectory(third_party/cryptopp)
add_subdirectory(third_party/glad)

if(ENABLE_LUAJIT)
add_subdirectory(third_party/LuaJIT luajit)
include_directories(third_party/LuaJIT/src ${CMAKE_BINARY_DIR}/luajit)
set_target_properties(luajit PROPERTIES EXCLUDE_FROM_ALL 1)

if(MSVC)
target_compile_definitions(libluajit PRIVATE _CRT_SECURE_NO_WARNINGS)
target_compile_definitions(minilua PRIVATE _CRT_SECURE_NO_WARNINGS)
target_compile_definitions(buildvm PRIVATE _CRT_SECURE_NO_WARNINGS)
endif()
endif()

# Check for x64
if (CMAKE_SYSTEM_PROCESSOR STREQUAL "AMD64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "x86-64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
set(HOST_X64 TRUE)
Expand Down Expand Up @@ -118,7 +131,7 @@ set(SOURCE_FILES src/main.cpp src/emulator.cpp src/io_file.cpp src/config.cpp
src/core/CPU/cpu_dynarmic.cpp src/core/CPU/dynarmic_cycles.cpp
src/core/memory.cpp src/renderer.cpp src/core/renderer_null/renderer_null.cpp
src/http_server.cpp src/stb_image_write.c src/core/cheats.cpp src/core/action_replay.cpp
src/discord_rpc.cpp
src/discord_rpc.cpp src/lua.cpp
)
set(CRYPTO_SOURCE_FILES src/core/crypto/aes_engine.cpp)
set(KERNEL_SOURCE_FILES src/core/kernel/kernel.cpp src/core/kernel/resource_limits.cpp
Expand Down Expand Up @@ -183,7 +196,7 @@ set(HEADER_FILES include/emulator.hpp include/helpers.hpp include/termcolor.hpp
include/applets/applet.hpp include/applets/mii_selector.hpp include/math_util.hpp include/services/soc.hpp
include/services/news_u.hpp include/applets/software_keyboard.hpp include/applets/applet_manager.hpp include/fs/archive_user_save_data.hpp
include/services/amiibo_device.hpp include/services/nfc_types.hpp include/swap.hpp include/services/csnd.hpp include/services/nwm_uds.hpp
include/fs/archive_system_save_data.hpp
include/fs/archive_system_save_data.hpp include/lua.hpp
)

cmrc_add_resource_library(
Expand Down Expand Up @@ -334,6 +347,11 @@ if(ENABLE_DISCORD_RPC AND NOT ANDROID)
target_link_libraries(Alber PRIVATE discord-rpc)
endif()

if(ENABLE_LUAJIT)
target_compile_definitions(Alber PUBLIC "PANDA3DS_ENABLE_LUA=1")
target_link_libraries(Alber PRIVATE libluajit)
endif()

if(ENABLE_OPENGL)
target_compile_definitions(Alber PUBLIC "PANDA3DS_ENABLE_OPENGL=1")
target_link_libraries(Alber PRIVATE resources_renderer_gl)
Expand Down
2 changes: 2 additions & 0 deletions include/emulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "crypto/aes_engine.hpp"
#include "discord_rpc.hpp"
#include "io_file.hpp"
#include "lua.hpp"
#include "memory.hpp"

#ifdef PANDA3DS_ENABLE_HTTP_SERVER
Expand Down Expand Up @@ -77,6 +78,7 @@ class Emulator {
NCSD loadedNCSD;

std::optional<std::filesystem::path> romPath = std::nullopt;
LuaManager lua;

public:
// Decides whether to reload or not reload the ROM when resetting. We use enum class over a plain bool for clarity.
Expand Down
32 changes: 32 additions & 0 deletions include/lua.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once
#include "helpers.hpp"

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

#include "luajit.h"
}

class LuaManager {
lua_State* L = nullptr;
bool initialized = false;

public:
void close();
void initialize();
void loadFile(const char* path);
void reset();
};

#elif // Lua not enabled, Lua manager does nothing
class LuaManager {
public:
void close() {}
void initialize() {}
void loadFile(const char* path) {}
void reset() {}
};
#endif
4 changes: 4 additions & 0 deletions src/emulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,13 @@ Emulator::Emulator()
}
}

lua.initialize();
reset(ReloadOption::NoReload);
}

Emulator::~Emulator() {
config.save(std::filesystem::current_path() / "config.toml");
lua.close();

#ifdef PANDA3DS_ENABLE_DISCORD_RPC
discordRpc.stop();
Expand Down Expand Up @@ -357,6 +359,8 @@ void Emulator::run() {

if (path.extension() == ".amiibo") {
loadAmiibo(path);
} else if (path.extension() == ".lua") {
lua.loadFile(droppedDir);
} else {
loadROM(path);
}
Expand Down
37 changes: 37 additions & 0 deletions src/lua.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifdef PANDA3DS_ENABLE_LUA
#include "lua.hpp"

void LuaManager::initialize() {
L = luaL_newstate(); // Open Lua

if (!L) {
printf("Lua initialization failed, continuing without Lua");
initialized = false;
return;
}

luaL_openlibs(L);
initialized = true;
}

void LuaManager::close() {
if (initialized) {
lua_close(L);
initialized = false;
L = nullptr;
}
}

void LuaManager::loadFile(const char* path) {
int status = luaL_loadfile(L, path); // load Lua script
int ret = lua_pcall(L, 0, 0, 0); // tell Lua to run the script

if (ret != 0) {
fprintf(stderr, "%s\n", lua_tostring(L, -1)); // tell us what mistake we made
}
}

void LuaManager::reset() {
// Reset scripts
}
#endif
1 change: 1 addition & 0 deletions third_party/LuaJIT
Submodule LuaJIT added at 41edf0

0 comments on commit 30cf9df

Please sign in to comment.