Skip to content

Commit

Permalink
Lua: Expose XInput callbacks and datatypes
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Jul 15, 2024
1 parent 93548ce commit b1a1724
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -600,11 +600,14 @@ list(APPEND luavrlib_SOURCES
"lua-api/lib/src/ScriptUtility.cpp"
"lua-api/lib/src/datatypes/StructObject.cpp"
"lua-api/lib/src/datatypes/Vector.cpp"
"lua-api/lib/src/datatypes/XInput.cpp"
"lua-api/lib/include/ScriptContext.hpp"
"lua-api/lib/include/ScriptState.hpp"
"lua-api/lib/include/ScriptUtility.hpp"
"lua-api/lib/include/datatypes/FFrame.hpp"
"lua-api/lib/include/datatypes/StructObject.hpp"
"lua-api/lib/include/datatypes/Vector.hpp"
"lua-api/lib/include/datatypes/XInput.hpp"
)

list(APPEND luavrlib_SOURCES
Expand Down
4 changes: 4 additions & 0 deletions lua-api/lib/include/ScriptContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ class ScriptContext : public std::enable_shared_from_this<ScriptContext> {
std::shared_ptr<sol::state> m_lua_shared{}; // This allows us to keep the state alive (if it was created by ScriptState)
std::recursive_mutex m_mtx{};
UEVR_PluginInitializeParam* m_plugin_initialize_param{nullptr};
std::vector<sol::protected_function> m_on_xinput_get_state_callbacks{};
std::vector<sol::protected_function> m_on_xinput_set_state_callbacks{};
std::vector<sol::protected_function> m_on_pre_engine_tick_callbacks{};
std::vector<sol::protected_function> m_on_post_engine_tick_callbacks{};
std::vector<sol::protected_function> m_on_pre_slate_draw_window_render_thread_callbacks{};
Expand All @@ -129,6 +131,8 @@ class ScriptContext : public std::enable_shared_from_this<ScriptContext> {
static bool global_ufunction_pre_handler(uevr::API::UFunction* fn, uevr::API::UObject* obj, void* params, void* result);
static void global_ufunction_post_handler(uevr::API::UFunction* fn, uevr::API::UObject* obj, void* params, void* result);

static void on_xinput_get_state(uint32_t* retval, uint32_t user_index, void* state);
static void on_xinput_set_state(uint32_t* retval, uint32_t user_index, void* vibration);
static void on_pre_engine_tick(UEVR_UGameEngineHandle engine, float delta_seconds);
static void on_post_engine_tick(UEVR_UGameEngineHandle engine, float delta_seconds);
static void on_pre_slate_draw_window_render_thread(UEVR_FSlateRHIRendererHandle renderer, UEVR_FViewportInfoHandle viewport_info);
Expand Down
2 changes: 1 addition & 1 deletion lua-api/lib/include/datatypes/FFrame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ namespace lua::datatypes {
void* locals;
};

void bind_fframe(sol::state_view& lua);
//void bind_fframe(sol::state_view& lua);
}
7 changes: 7 additions & 0 deletions lua-api/lib/include/datatypes/XInput.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include <sol/sol.hpp>

namespace lua::datatypes {
void bind_xinput(sol::state_view& lua);
}
42 changes: 42 additions & 0 deletions lua-api/lib/src/ScriptContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

#include <windows.h>

#include <Xinput.h>

#include "datatypes/XInput.hpp"
#include "datatypes/Vector.hpp"
#include "datatypes/StructObject.hpp"
#include "datatypes/FFrame.hpp"
Expand Down Expand Up @@ -101,6 +104,8 @@ void ScriptContext::setup_callback_bindings() {

s_callbacks_to_remove.clear();

add_callback(m_plugin_initialize_param->callbacks->on_xinput_get_state, on_xinput_get_state);
add_callback(m_plugin_initialize_param->callbacks->on_xinput_set_state, on_xinput_set_state);
add_callback(cbs->on_pre_engine_tick, on_pre_engine_tick);
add_callback(cbs->on_post_engine_tick, on_post_engine_tick);
add_callback(cbs->on_pre_slate_draw_window_render_thread, on_pre_slate_draw_window_render_thread);
Expand All @@ -112,6 +117,14 @@ void ScriptContext::setup_callback_bindings() {
}

m_lua.new_usertype<UEVR_SDKCallbacks>("UEVR_SDKCallbacks",
"on_xinput_get_state", [this](sol::function fn) {
std::scoped_lock _{ m_mtx };
m_on_xinput_get_state_callbacks.push_back(fn);
},
"on_xinput_set_state", [this](sol::function fn) {
std::scoped_lock _{ m_mtx };
m_on_xinput_set_state_callbacks.push_back(fn);
},
"on_pre_engine_tick", [this](sol::function fn) {
std::scoped_lock _{ m_mtx };
m_on_pre_engine_tick_callbacks.push_back(fn);
Expand Down Expand Up @@ -162,6 +175,7 @@ void ScriptContext::setup_callback_bindings() {
int ScriptContext::setup_bindings() {
m_lua.registry()["uevr_context"] = this;

lua::datatypes::bind_xinput(m_lua);
lua::datatypes::bind_vectors(m_lua);
lua::datatypes::bind_struct_object(m_lua);

Expand Down Expand Up @@ -715,6 +729,34 @@ void ScriptContext::global_ufunction_post_handler(uevr::API::UFunction* fn, uevr
});
}

void ScriptContext::on_xinput_get_state(uint32_t* retval, uint32_t user_index, void* state) {
g_contexts.for_each([=](auto ctx) {
std::scoped_lock _{ ctx->m_mtx };

for (auto& fn : ctx->m_on_xinput_get_state_callbacks) try {
ctx->handle_protected_result(fn(retval, user_index, (XINPUT_STATE*)state));
} catch (const std::exception& e) {
ScriptContext::log("Exception in on_xinput_get_state: " + std::string(e.what()));
} catch (...) {
ScriptContext::log("Unknown exception in on_xinput_get_state");
}
});
}

void ScriptContext::on_xinput_set_state(uint32_t* retval, uint32_t user_index, void* vibration) {
g_contexts.for_each([=](auto ctx) {
std::scoped_lock _{ ctx->m_mtx };

for (auto& fn : ctx->m_on_xinput_set_state_callbacks) try {
ctx->handle_protected_result(fn(retval, user_index, (XINPUT_VIBRATION*)vibration));
} catch (const std::exception& e) {
ScriptContext::log("Exception in on_xinput_set_state: " + std::string(e.what()));
} catch (...) {
ScriptContext::log("Unknown exception in on_xinput_set_state");
}
});
}

void ScriptContext::on_pre_engine_tick(UEVR_UGameEngineHandle engine, float delta_seconds) {
g_contexts.for_each([=](auto ctx) {
std::scoped_lock _{ ctx->m_mtx };
Expand Down
43 changes: 43 additions & 0 deletions lua-api/lib/src/datatypes/XInput.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <windows.h>
#include <Xinput.h>

#include <datatypes/XInput.hpp>

namespace lua::datatypes {
void bind_xinput(sol::state_view& lua) {
lua.new_usertype<XINPUT_GAMEPAD>("XINPUT_GAMEPAD",
"wButtons", &XINPUT_GAMEPAD::wButtons,
"bLeftTrigger", &XINPUT_GAMEPAD::bLeftTrigger,
"bRightTrigger", &XINPUT_GAMEPAD::bRightTrigger,
"sThumbLX", &XINPUT_GAMEPAD::sThumbLX,
"sThumbLY", &XINPUT_GAMEPAD::sThumbLY,
"sThumbRX", &XINPUT_GAMEPAD::sThumbRX,
"sThumbRY", &XINPUT_GAMEPAD::sThumbRY
);

lua.new_usertype<XINPUT_STATE>("XINPUT_STATE",
"Gamepad", &XINPUT_STATE::Gamepad,
"dwPacketNumber", &XINPUT_STATE::dwPacketNumber
);

lua.new_usertype<XINPUT_VIBRATION>("XINPUT_VIBRATION",
"wLeftMotorSpeed", &XINPUT_VIBRATION::wLeftMotorSpeed,
"wRightMotorSpeed", &XINPUT_VIBRATION::wRightMotorSpeed
);

lua["XINPUT_GAMEPAD_DPAD_UP"] = XINPUT_GAMEPAD_DPAD_UP;
lua["XINPUT_GAMEPAD_DPAD_DOWN"] = XINPUT_GAMEPAD_DPAD_DOWN;
lua["XINPUT_GAMEPAD_DPAD_LEFT"] = XINPUT_GAMEPAD_DPAD_LEFT;
lua["XINPUT_GAMEPAD_DPAD_RIGHT"] = XINPUT_GAMEPAD_DPAD_RIGHT;
lua["XINPUT_GAMEPAD_START"] = XINPUT_GAMEPAD_START;
lua["XINPUT_GAMEPAD_BACK"] = XINPUT_GAMEPAD_BACK;
lua["XINPUT_GAMEPAD_LEFT_THUMB"] = XINPUT_GAMEPAD_LEFT_THUMB;
lua["XINPUT_GAMEPAD_RIGHT_THUMB"] = XINPUT_GAMEPAD_RIGHT_THUMB;
lua["XINPUT_GAMEPAD_LEFT_SHOULDER"] = XINPUT_GAMEPAD_LEFT_SHOULDER;
lua["XINPUT_GAMEPAD_RIGHT_SHOULDER"] = XINPUT_GAMEPAD_RIGHT_SHOULDER;
lua["XINPUT_GAMEPAD_A"] = XINPUT_GAMEPAD_A;
lua["XINPUT_GAMEPAD_B"] = XINPUT_GAMEPAD_B;
lua["XINPUT_GAMEPAD_X"] = XINPUT_GAMEPAD_X;
lua["XINPUT_GAMEPAD_Y"] = XINPUT_GAMEPAD_Y;
}
}

0 comments on commit b1a1724

Please sign in to comment.