Skip to content

Commit

Permalink
Lua API improvements + direct UEVR integration (#255)
Browse files Browse the repository at this point in the history
* Lua: Add LuaLoader, Lua API improvements

* Lua: Fix initial run not executing on game thread

* Plugins: Fix huge bug with remove_callback

* Lua: Stability improvements on script reset

* Lua: Fix execute_command not working

* Lua: improvement to UI layout

* Lua: Some fixes, basic UObject property getters

* Lua: Add basic auto type-resolving get_property

* Lua: Add support for TArray<UObject*>

* Lua: Add example script

* Lua: Add support for directly indexing properties

* Lua: Add support for setting properties

* Lua: Add basic function calling

* Lua: Fix incorrect return value

* Lua: Throw error on set_property if it doesn't exist

* Lua: Add support for StrProperty parameters, ClassProperty

* Plugins: Fix cases where object finder could return default objects

* Lua: Return class if possible, add conversion functions

* Plugins: Add FStructProperty functions

* Lua: Initial work on Vector types

* Lua: Fix Vector types returned from functions

* Lua: Initial support for passing Vector types to functions

* Lua: Migrate utility functions to separate files

* Lua: Add support for reading/modifying StructProperty properties

* Lua: Add support for passing StructObjects to functions

* Lua: update example script with more tests

* Lua: Add StructObject.new(UStruct*)

* Lua: Pass correct pos/rot structs through stereo callbacks

* Lua: Vector property parity with UE naming

* Plugins: Activate UObjectHook if any functions are called

* Plugins: Add FEnumProperty functions

* Lua: Add support for enum properties and some other primitives

* Lua: Use set_property for call_function args

* Lua: Cleanup TArray return values

* Lua: Fix redundant param resize

* CI: Attempt at including LuaVR.dll in builds

* Lua: Fix undefined behavior when loading LuaVR.dll
  • Loading branch information
praydog authored Jul 5, 2024
1 parent 3d6fb3b commit b05ade4
Show file tree
Hide file tree
Showing 23 changed files with 2,693 additions and 560 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/dev-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ jobs:
- name: Build plugin nullifier
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} --target vr-plugin-nullifier

- name: Build Lua API (shared DLL)
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} --target LuaVR

- name: Checkout frontend
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
with:
Expand All @@ -47,13 +50,15 @@ jobs:
path: |
${{github.workspace}}/build/bin/${{matrix.target}}/*
${{github.workspace}}/build/bin/vr-plugin-nullifier/UEVRPluginNullifier.dll
${{github.workspace}}/build/bin/LuaVR/LuaVR.dll
if-no-files-found: error

- name: Compress artifacts
run: |
echo ${{github.sha}} > ${{github.workspace}}/revision.txt
7z a ${{github.workspace}}/${{matrix.target}}.zip ${{github.workspace}}/build/bin/${{matrix.target}}/*
7z a ${{github.workspace}}/${{matrix.target}}.zip ${{github.workspace}}/build/bin/vr-plugin-nullifier/UEVRPluginNullifier.dll
7z a ${{github.workspace}}/${{matrix.target}}.zip ${{github.workspace}}/build/bin/LuaVR/LuaVR.dll
7z a ${{github.workspace}}/${{matrix.target}}.zip ${{github.workspace}}/revision.txt
- name: Hash zip
Expand Down
63 changes: 58 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -590,14 +590,67 @@ target_include_directories(sol2 INTERFACE
unset(CMKR_TARGET)
unset(CMKR_SOURCES)

# Target luavrlib
set(CMKR_TARGET luavrlib)
set(luavrlib_SOURCES "")

list(APPEND luavrlib_SOURCES
"lua-api/lib/src/ScriptContext.cpp"
"lua-api/lib/src/ScriptState.cpp"
"lua-api/lib/src/ScriptUtility.cpp"
"lua-api/lib/src/datatypes/StructObject.cpp"
"lua-api/lib/src/datatypes/Vector.cpp"
"lua-api/lib/include/ScriptContext.hpp"
"lua-api/lib/include/ScriptState.hpp"
"lua-api/lib/include/ScriptUtility.hpp"
"lua-api/lib/include/datatypes/StructObject.hpp"
"lua-api/lib/include/datatypes/Vector.hpp"
)

list(APPEND luavrlib_SOURCES
cmake.toml
)

set(CMKR_SOURCES ${luavrlib_SOURCES})
add_library(luavrlib STATIC)

if(luavrlib_SOURCES)
target_sources(luavrlib PRIVATE ${luavrlib_SOURCES})
endif()

source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${luavrlib_SOURCES})

target_compile_features(luavrlib PUBLIC
cxx_std_23
)

target_compile_options(luavrlib PUBLIC
"/bigobj"
"/EHa"
"/MP"
)

target_include_directories(luavrlib PUBLIC
"include/"
"lua-api/lib/include"
)

target_link_libraries(luavrlib PUBLIC
lua
sol2
kananlib
glm
)

unset(CMKR_TARGET)
unset(CMKR_SOURCES)

# Target LuaVR
set(CMKR_TARGET LuaVR)
set(LuaVR_SOURCES "")

list(APPEND LuaVR_SOURCES
"lua-api/Main.cpp"
"lua-api/ScriptContext.cpp"
"lua-api/ScriptContext.hpp"
)

list(APPEND LuaVR_SOURCES
Expand Down Expand Up @@ -628,9 +681,7 @@ target_include_directories(LuaVR PUBLIC
)

target_link_libraries(LuaVR PUBLIC
lua
sol2
kananlib
luavrlib
)

set_target_properties(LuaVR PROPERTIES
Expand Down Expand Up @@ -669,6 +720,7 @@ list(APPEND uevr_SOURCES
"src/hooks/XInputHook.cpp"
"src/mods/FrameworkConfig.cpp"
"src/mods/ImGuiThemeHelpers.cpp"
"src/mods/LuaLoader.cpp"
"src/mods/PluginLoader.cpp"
"src/mods/UObjectHook.cpp"
"src/mods/VR.cpp"
Expand Down Expand Up @@ -788,6 +840,7 @@ target_link_libraries(uevr PUBLIC
DirectXTK12
sdkgenny
asmjit
luavrlib
)

target_link_libraries(uevr PUBLIC
Expand Down
24 changes: 18 additions & 6 deletions cmake.toml
Original file line number Diff line number Diff line change
Expand Up @@ -192,17 +192,28 @@ include-directories = ["dependencies/lua/src"]
type = "interface"
include-directories = ["dependencies/sol2/single/single/include"]

[target.luavrlib]
type = "static"
compile-features = ["cxx_std_23"]
compile-options = ["/bigobj", "/EHa", "/MP"]
include-directories = ["include/", "lua-api/lib/include"]
sources = ["lua-api/lib/**.cpp", "lua-api/lib/**.c"]
headers = ["lua-api/lib/**.hpp", "lua-api/lib/**.h"]
link-libraries = [
"lua",
"sol2",
"kananlib",
"glm"
]

[target.LuaVR]
type = "shared"
compile-features = ["cxx_std_23"]
compile-options = ["/bigobj", "/EHa", "/MP"]
include-directories = ["include/"]
sources = ["lua-api/**.cpp", "lua-api/**.c"]
headers = ["lua-api/**.hpp", "lua-api/**.h"]
sources = ["lua-api/Main.cpp"]
link-libraries = [
"lua",
"sol2",
"kananlib"
"luavrlib"
]

[target.LuaVR.properties]
Expand Down Expand Up @@ -241,7 +252,8 @@ link-libraries = [
"DirectXTK",
"DirectXTK12",
"sdkgenny",
"asmjit"
"asmjit",
"luavrlib"
]

[template.ue4template.properties]
Expand Down
15 changes: 15 additions & 0 deletions include/uevr/API.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ DECLARE_UEVR_HANDLE(UEVR_FRHITexture2DHandle);
DECLARE_UEVR_HANDLE(UEVR_UScriptStructHandle);
DECLARE_UEVR_HANDLE(UEVR_FArrayPropertyHandle);
DECLARE_UEVR_HANDLE(UEVR_FBoolPropertyHandle);
DECLARE_UEVR_HANDLE(UEVR_FStructPropertyHandle);
DECLARE_UEVR_HANDLE(UEVR_FEnumPropertyHandle);
DECLARE_UEVR_HANDLE(UEVR_UEnumHandle);
DECLARE_UEVR_HANDLE(UEVR_FNumericPropertyHandle);

/* OpenXR stuff */
DECLARE_UEVR_HANDLE(UEVR_XrInstance);
Expand Down Expand Up @@ -413,6 +417,15 @@ typedef struct {
void (*set_value_in_propbase)(UEVR_FBoolPropertyHandle prop, void* addr, bool value);
} UEVR_FBoolPropertyFunctions;

typedef struct {
UEVR_UScriptStructHandle (*get_struct)(UEVR_FStructPropertyHandle prop);
} UEVR_FStructPropertyFunctions;

typedef struct {
UEVR_FNumericPropertyHandle (*get_underlying_prop)(UEVR_FEnumPropertyHandle prop);
UEVR_UEnumHandle (*get_enum)(UEVR_FEnumPropertyHandle prop);
} UEVR_FEnumPropertyFunctions;

typedef struct {
const UEVR_SDKFunctions* functions;
const UEVR_SDKCallbacks* callbacks;
Expand All @@ -434,6 +447,8 @@ typedef struct {
const UEVR_UScriptStructFunctions* uscriptstruct;
const UEVR_FArrayPropertyFunctions* farrayproperty;
const UEVR_FBoolPropertyFunctions* fboolproperty;
const UEVR_FStructPropertyFunctions* fstructproperty;
const UEVR_FEnumPropertyFunctions* fenumproperty;
} UEVR_SDKData;

DECLARE_UEVR_HANDLE(UEVR_IVRSystem);
Expand Down
53 changes: 53 additions & 0 deletions include/uevr/API.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,59 @@ class API {
}
};

struct FStructProperty : public FProperty {
inline UEVR_FStructPropertyHandle to_handle() { return (UEVR_FStructPropertyHandle)this; }
inline UEVR_FStructPropertyHandle to_handle() const { return (UEVR_FStructPropertyHandle)this; }

UScriptStruct* get_struct() const {
static const auto fn = initialize()->get_struct;
return (UScriptStruct*)fn(to_handle());
}

private:
static inline const UEVR_FStructPropertyFunctions* s_functions{nullptr};
static inline const UEVR_FStructPropertyFunctions* initialize() {
if (s_functions == nullptr) {
s_functions = API::get()->sdk()->fstructproperty;
}

return s_functions;
}
};

struct UEnum : public UObject {

};

struct FNumericProperty : public FProperty {

};

struct FEnumProperty : public FProperty {
inline UEVR_FEnumPropertyHandle to_handle() { return (UEVR_FEnumPropertyHandle)this; }
inline UEVR_FEnumPropertyHandle to_handle() const { return (UEVR_FEnumPropertyHandle)this; }

FNumericProperty* get_underlying_prop() const {
static const auto fn = initialize()->get_underlying_prop;
return (FNumericProperty*)fn(to_handle());
}

UEnum* get_enum() const {
static const auto fn = initialize()->get_enum;
return (UEnum*)fn(to_handle());
}

private:
static inline const UEVR_FEnumPropertyFunctions* s_functions{nullptr};
static inline const UEVR_FEnumPropertyFunctions* initialize() {
if (s_functions == nullptr) {
s_functions = API::get()->sdk()->fenumproperty;
}

return s_functions;
}
};

struct FFieldClass {
inline UEVR_FFieldClassHandle to_handle() { return (UEVR_FFieldClassHandle)this; }
inline UEVR_FFieldClassHandle to_handle() const { return (UEVR_FFieldClassHandle)this; }
Expand Down
16 changes: 8 additions & 8 deletions lua-api/Main.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
// Lua API to expose UEVR functionality to Lua scripts via UE4SS

#include <windows.h>
#include "ScriptContext.hpp"
#include "lib/include/ScriptContext.hpp"

std::shared_ptr<uevr::ScriptContext> g_script_context{};

// Main exported function that takes in the lua_State*
extern "C" __declspec(dllexport) int luaopen_LuaVR(lua_State* L) {
luaL_checkversion(L);

ScriptContext::log("Initializing LuaVR...");

auto script_context = ScriptContext::reinitialize(L);
g_script_context = uevr::ScriptContext::create(L);

if (!script_context->valid()) {
ScriptContext::log("LuaVR failed to initialize! Make sure to inject VR first!");
if (!g_script_context->valid()) {
uevr::ScriptContext::log("LuaVR failed to initialize! Make sure to inject VR first!");
return 0;
}

ScriptContext::log("LuaVR initialized!");
uevr::ScriptContext::log("LuaVR initialized!");

return script_context->setup_bindings();
return g_script_context->setup_bindings();
}

BOOL APIENTRY DllMain(HMODULE module, DWORD ul_reason_for_call, LPVOID reserved) {
Expand Down
Loading

0 comments on commit b05ade4

Please # to comment.