Skip to content

Commit

Permalink
Plugins/Lua: Add dispatch_lua_event, on_lua_event
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Oct 12, 2024
1 parent fcceb47 commit 1883ddc
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 5 deletions.
3 changes: 2 additions & 1 deletion include/uevr/API.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ SOFTWARE.
#define UEVR_OUT

#define UEVR_PLUGIN_VERSION_MAJOR 2
#define UEVR_PLUGIN_VERSION_MINOR 32
#define UEVR_PLUGIN_VERSION_MINOR 33
#define UEVR_PLUGIN_VERSION_PATCH 0

#define UEVR_RENDERER_D3D11 0
Expand Down Expand Up @@ -217,6 +217,7 @@ typedef struct {
unsigned int (*get_persistent_dir)(wchar_t* buffer, unsigned int buffer_size);
int (*register_inline_hook)(void* target, void* dst, void** original);
void (*unregister_inline_hook)(int hook_id);
void (*dispatch_lua_event)(const char* event_name, const char* event_data);
} UEVR_PluginFunctions;

typedef struct {
Expand Down
5 changes: 5 additions & 0 deletions include/uevr/API.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ class API {
return result;
}

void dispatch_lua_event(std::string_view event_name, std::string_view event_data) {
static const auto fn = param()->functions->dispatch_lua_event;
fn(event_name.data(), event_data.data());
}

template <typename... Args> void log_error(const char* format, Args... args) { m_param->functions->log_error(format, args...); }
template <typename... Args> void log_warn(const char* format, Args... args) { m_param->functions->log_warn(format, args...); }
template <typename... Args> void log_info(const char* format, Args... args) { m_param->functions->log_info(format, args...); }
Expand Down
32 changes: 29 additions & 3 deletions lua-api/lib/include/ScriptContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,48 @@ class ScriptContext : public std::enable_shared_from_this<ScriptContext> {
void script_reset() {
std::scoped_lock _{m_mtx};

for (auto& cb : m_on_script_reset_callbacks) {
for (auto& cb : m_on_script_reset_callbacks) try {
handle_protected_result(cb());
} catch (const std::exception& e) {
log("Exception in on_script_reset: " + std::string(e.what()));
} catch (...) {
log("Unknown exception in on_script_reset");
}
}

void frame() {
std::scoped_lock _{m_mtx};

for (auto& cb : m_on_frame_callbacks) {
for (auto& cb : m_on_frame_callbacks) try {
handle_protected_result(cb());
} catch (const std::exception& e) {
log("Exception in on_frame: " + std::string(e.what()));
} catch (...) {
log("Unknown exception in on_frame");
}
}

void draw_ui() {
std::scoped_lock _{m_mtx};

for (auto& cb : m_on_draw_ui_callbacks) {
for (auto& cb : m_on_draw_ui_callbacks) try {
handle_protected_result(cb());
} catch (const std::exception& e) {
log("Exception in on_draw_ui: " + std::string(e.what()));
} catch (...) {
log("Unknown exception in on_draw_ui");
}
}

void dispatch_event(std::string_view event_name, std::string_view event_data) {
std::scoped_lock _{m_mtx};

for (auto& cb : m_on_lua_event_callbacks) try {
handle_protected_result(cb(event_name, event_data));
} catch (const std::exception& e) {
log("Exception in on_lua_event: " + std::string(e.what()));
} catch (...) {
log("Unknown exception in on_lua_event");
}
}

Expand All @@ -116,6 +140,7 @@ class ScriptContext : public std::enable_shared_from_this<ScriptContext> {
std::vector<sol::protected_function> m_on_post_calculate_stereo_view_offset_callbacks{};
std::vector<sol::protected_function> m_on_pre_viewport_client_draw_callbacks{};
std::vector<sol::protected_function> m_on_post_viewport_client_draw_callbacks{};
std::vector<sol::protected_function> m_on_lua_event_callbacks{};

// Custom UEVR callbacks
std::vector<sol::protected_function> m_on_frame_callbacks{};
Expand Down Expand Up @@ -146,5 +171,6 @@ class ScriptContext : public std::enable_shared_from_this<ScriptContext> {
static void on_frame();
static void on_draw_ui();
static void on_script_reset();
static void on_lua_event(std::string_view event_name, std::string_view event_data);
};
}
1 change: 1 addition & 0 deletions lua-api/lib/include/ScriptState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class ScriptState {
void on_frame();
void on_draw_ui();
void on_script_reset();
void dispatch_event(std::string_view event_name, std::string_view event_data);

auto& context() {
return m_context;
Expand Down
21 changes: 21 additions & 0 deletions lua-api/lib/src/ScriptContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ void ScriptContext::setup_callback_bindings() {
"on_script_reset", [this](sol::function fn) {
std::scoped_lock _{ m_mtx };
m_on_script_reset_callbacks.push_back(fn);
},
"on_lua_event", [this](sol::function fn) {
std::scoped_lock _{ m_mtx };
m_on_lua_event_callbacks.push_back(fn);
}
);
}
Expand Down Expand Up @@ -1102,4 +1106,21 @@ void ScriptContext::on_script_reset() {
}
});
}

void ScriptContext::on_lua_event(std::string_view event_name, std::string_view event_data) {
g_contexts.for_each([=](auto ctx) {
std::scoped_lock _{ ctx->m_mtx };

const char* event_name_data = event_name.data();
const char* event_data_data = event_data.data();

for (auto& fn : ctx->m_on_lua_event_callbacks) try {
ctx->handle_protected_result(fn(event_name_data, event_data_data));
} catch (const std::exception& e) {
ScriptContext::log("Exception in on_lua_event: " + std::string(e.what()));
} catch (...) {
ScriptContext::log("Unknown exception in on_lua_event");
}
});
}
}
6 changes: 6 additions & 0 deletions lua-api/lib/src/ScriptState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,10 @@ void ScriptState::on_draw_ui() {
m_context->draw_ui();
}
}

void ScriptState::dispatch_event(std::string_view event_name, std::string_view event_data) {
if (m_context != nullptr) {
m_context->dispatch_event(event_name, event_data);
}
}
}
10 changes: 10 additions & 0 deletions src/mods/LuaLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,4 +281,14 @@ void LuaLoader::reset_scripts() {

std::sort(m_known_scripts.begin(), m_known_scripts.end());
std::sort(m_loaded_scripts.begin(), m_loaded_scripts.end());
}

void LuaLoader::dispatch_event(std::string_view event_name, std::string_view event_data) {
std::scoped_lock _{m_access_mutex};

if (m_main_state == nullptr) {
return;
}

m_main_state->dispatch_event(event_name, event_data);
}
1 change: 1 addition & 0 deletions src/mods/LuaLoader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class LuaLoader : public Mod {

// Resets the ScriptState and runs autorun scripts again.
void reset_scripts();
void dispatch_event(std::string_view event_name, std::string_view event_data);

private:
ScriptState::GarbageCollectionData make_gc_data() const {
Expand Down
7 changes: 6 additions & 1 deletion src/mods/PluginLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ int register_inline_hook(void* target, void* dst, void** original) {
void unregister_inline_hook(int id) {
PluginLoader::get()->remove_inline_hook(id);
}

void dispatch_lua_event(const char* event_name, const char* event_data) {
LuaLoader::get()->dispatch_event(event_name, event_data);
}
}

namespace uevr {
Expand Down Expand Up @@ -188,7 +192,8 @@ UEVR_PluginFunctions g_plugin_functions {
uevr::remove_callback,
uevr::get_persistent_dir,
uevr::register_inline_hook,
uevr::unregister_inline_hook
uevr::unregister_inline_hook,
uevr::dispatch_lua_event
};

#define GET_ENGINE_WORLD_RETNULL() \
Expand Down

0 comments on commit 1883ddc

Please sign in to comment.