Skip to content

Commit

Permalink
Lua: Pass locals onto hooks, fix error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Jul 14, 2024
1 parent 181f519 commit 93548ce
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
20 changes: 20 additions & 0 deletions lua-api/lib/include/datatypes/FFrame.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <uevr/API.hpp>
#include <sol/sol.hpp>

namespace lua::datatypes {
struct FFrame {
void* vtable;

bool bool1;
bool bool2;

void* node;
void* object;
void* code;
void* locals;
};

void bind_fframe(sol::state_view& lua);
}
33 changes: 24 additions & 9 deletions lua-api/lib/src/ScriptContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "datatypes/Vector.hpp"
#include "datatypes/StructObject.hpp"
#include "datatypes/FFrame.hpp"

#include "ScriptUtility.hpp"
#include "ScriptContext.hpp"
Expand Down Expand Up @@ -476,6 +477,8 @@ int ScriptContext::setup_bindings() {

std::unique_lock _{ m_ufunction_hooks_mtx };

fn->hook_ptr(global_ufunction_pre_handler, global_ufunction_post_handler);

if (auto it = m_ufunction_hooks.find(fn); it != m_ufunction_hooks.end()) {
if (pre != sol::nil) {
it->second->pre_hooks.push_back(pre);
Expand All @@ -491,8 +494,6 @@ int ScriptContext::setup_bindings() {
auto& hook = m_ufunction_hooks[fn];
hook = std::make_unique<UFunctionHookState>();

fn->hook_ptr(global_ufunction_pre_handler, global_ufunction_post_handler);

if (pre != sol::nil) {
hook->pre_hooks.push_back(pre);
}
Expand Down Expand Up @@ -662,7 +663,7 @@ int ScriptContext::setup_bindings() {
return out.push(m_lua.lua_state());
}

bool ScriptContext::global_ufunction_pre_handler(uevr::API::UFunction* fn, uevr::API::UObject* obj, void* params, void* out_result) {
bool ScriptContext::global_ufunction_pre_handler(uevr::API::UFunction* fn, uevr::API::UObject* obj, void* frame, void* out_result) {
bool any_false = false;

g_contexts.for_each([=, &any_false](auto ctx) {
Expand All @@ -672,29 +673,43 @@ bool ScriptContext::global_ufunction_pre_handler(uevr::API::UFunction* fn, uevr:
auto it = ctx->m_ufunction_hooks.find(fn);

if (it != ctx->m_ufunction_hooks.end()) {
for (auto& cb : it->second->pre_hooks) {
bool result = cb(fn, obj, params, out_result);
auto fframe = (lua::datatypes::FFrame*)frame;
auto locals = lua::datatypes::StructObject{fframe->locals, fn};
auto locals_obj = sol::make_object(ctx->m_lua.lua_state(), &locals);

if (!result) {
for (auto& cb : it->second->pre_hooks) try {
if (sol::object result = ctx->handle_protected_result(cb(fn, obj, locals_obj, out_result)); !result.is<sol::nil_t>() && result.is<bool>() && result.as<bool>() == false) {
any_false = true;
}
} catch (const std::exception& e) {
ScriptContext::log("Exception in global_ufunction_pre_handler: " + std::string(e.what()));
} catch (...) {
ScriptContext::log("Unknown exception in global_ufunction_pre_handler");
}
}
});

return !any_false;
}

void ScriptContext::global_ufunction_post_handler(uevr::API::UFunction* fn, uevr::API::UObject* obj, void* params, void* result) {
void ScriptContext::global_ufunction_post_handler(uevr::API::UFunction* fn, uevr::API::UObject* obj, void* frame, void* result) {
g_contexts.for_each([=](auto ctx) {
std::scoped_lock _{ ctx->m_mtx };
std::scoped_lock __{ ctx->m_ufunction_hooks_mtx };

auto it = ctx->m_ufunction_hooks.find(fn);

if (it != ctx->m_ufunction_hooks.end()) {
for (auto& cb : it->second->post_hooks) {
cb(fn, obj, params, result);
auto fframe = (lua::datatypes::FFrame*)frame;
auto locals = lua::datatypes::StructObject{fframe->locals, fn};
auto locals_obj = sol::make_object(ctx->m_lua.lua_state(), &locals);

for (auto& cb : it->second->post_hooks) try {
ctx->handle_protected_result(cb(fn, obj, locals_obj, result));
} catch (const std::exception& e) {
ScriptContext::log("Exception in global_ufunction_post_handler: " + std::string(e.what()));
} catch (...) {
ScriptContext::log("Unknown exception in global_ufunction_post_handler");
}
}
});
Expand Down

0 comments on commit 93548ce

Please sign in to comment.