Files
Panda3DS/include/lua_manager.hpp

65 lines
1.3 KiB
C++
Raw Permalink Normal View History

2023-09-17 16:15:54 +03:00
#pragma once
2023-12-16 16:36:03 +02:00
#include <string>
2023-09-17 16:15:54 +03:00
#include "helpers.hpp"
2023-09-18 00:35:29 +03:00
// The kinds of events that can cause a Lua call.
// Frame: Call program on frame end
// TODO: Add more
enum class LuaEvent {
Frame,
};
class Emulator;
2023-09-17 16:15:54 +03:00
#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;
2023-09-18 00:35:29 +03:00
bool haveScript = false;
void signalEventInternal(LuaEvent e);
2023-09-17 16:15:54 +03:00
public:
2023-09-17 19:23:52 +03:00
// For Lua we must have some global pointers to our emulator objects to use them in script code via thunks. See the thunks in lua.cpp as an
// example
static Emulator* g_emulator;
2023-09-17 19:23:52 +03:00
LuaManager(Emulator& emulator) { g_emulator = &emulator; }
2023-09-17 19:23:52 +03:00
2023-09-17 16:15:54 +03:00
void close();
void initialize();
2023-09-17 19:23:52 +03:00
void initializeThunks();
2023-09-17 16:15:54 +03:00
void loadFile(const char* path);
2023-12-16 16:36:03 +02:00
void loadString(const std::string& code);
2023-09-17 16:15:54 +03:00
void reset();
2023-09-18 00:35:29 +03:00
void signalEvent(LuaEvent e) {
if (haveScript) [[unlikely]] {
signalEventInternal(e);
}
}
2023-09-17 16:15:54 +03:00
};
2023-10-17 00:07:48 +03:00
#else // Lua not enabled, Lua manager does nothing
2023-09-17 16:15:54 +03:00
class LuaManager {
public:
LuaManager(Emulator& emulator) {}
2023-09-17 19:23:52 +03:00
2023-09-17 16:15:54 +03:00
void close() {}
void initialize() {}
void loadFile(const char* path) {}
2023-12-16 16:36:03 +02:00
void loadString(const std::string& code) {}
2023-09-17 16:15:54 +03:00
void reset() {}
2023-09-18 00:35:29 +03:00
void signalEvent(LuaEvent e) {}
2023-09-17 16:15:54 +03:00
};
2023-10-17 00:07:48 +03:00
#endif