Skip to content

Commit

Permalink
Lua: Initial work on Vector types
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Jun 26, 2024
1 parent 4cedaa7 commit 2891e90
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -597,8 +597,10 @@ set(luavrlib_SOURCES "")
list(APPEND luavrlib_SOURCES
"lua-api/lib/src/ScriptContext.cpp"
"lua-api/lib/src/ScriptState.cpp"
"lua-api/lib/src/datatypes/Vector.cpp"
"lua-api/lib/include/ScriptContext.hpp"
"lua-api/lib/include/ScriptState.hpp"
"lua-api/lib/include/datatypes/Vector.hpp"
)

list(APPEND luavrlib_SOURCES
Expand Down Expand Up @@ -633,6 +635,7 @@ target_link_libraries(luavrlib PUBLIC
lua
sol2
kananlib
glm
)

unset(CMKR_TARGET)
Expand Down
3 changes: 2 additions & 1 deletion cmake.toml
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ headers = ["lua-api/lib/**.hpp", "lua-api/lib/**.h"]
link-libraries = [
"lua",
"sol2",
"kananlib"
"kananlib",
"glm"
]

[target.LuaVR]
Expand Down
1 change: 0 additions & 1 deletion lua-api/lib/include/ScriptContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class ScriptContext : public std::enable_shared_from_this<ScriptContext> {
}

static void log(const std::string& message);
static void test_function();

template<typename T1, typename T2>
void add_callback(T1&& adder, T2&& cb) {
Expand Down
18 changes: 18 additions & 0 deletions lua-api/lib/include/datatypes/Vector.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <glm/vec3.hpp>
#include <glm/vec2.hpp>
#include <glm/vec4.hpp>
#include <sol/sol.hpp>
#include <uevr/API.hpp>

namespace lua::datatypes {
using Vector2f = glm::vec2;
using Vector2d = glm::dvec2;
using Vector3f = glm::vec3;
using Vector3d = glm::dvec3;
using Vector4f = glm::vec4;
using Vector4d = glm::dvec4;

void bind_vectors(sol::state_view& lua);
}
64 changes: 59 additions & 5 deletions lua-api/lib/src/ScriptContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include <windows.h>

#include "datatypes/Vector.hpp"

#include "ScriptContext.hpp"

namespace uevr {
Expand Down Expand Up @@ -91,10 +93,6 @@ void ScriptContext::log(const std::string& message) {
API::get()->log_info("[LuaVR] %s", message.c_str());
}

void ScriptContext::test_function() {
log("Test function called!");
}

void ScriptContext::setup_callback_bindings() {
std::scoped_lock _{ m_mtx };

Expand Down Expand Up @@ -159,6 +157,31 @@ void ScriptContext::setup_callback_bindings() {

sol::object call_function(sol::this_state s, uevr::API::UObject* self, uevr::API::UFunction* fn, sol::variadic_args args);

uevr::API::UScriptStruct* get_vector_struct() {
static auto vector_struct = []() {
const auto modern_class = uevr::API::get()->find_uobject<uevr::API::UScriptStruct>(L"ScriptStruct /Script/CoreUObject.Vector");
const auto old_class = modern_class == nullptr ? uevr::API::get()->find_uobject<uevr::API::UScriptStruct>(L"ScriptStruct /Script/CoreUObject.Object.Vector") : nullptr;

return modern_class != nullptr ? modern_class : old_class;
}();

return vector_struct;
}

bool is_ue5() {
static auto cached_result = []() {
const auto c = get_vector_struct();

if (c == nullptr) {
return false;
}

return c->get_struct_size() == sizeof(glm::dvec3);
}();

return cached_result;
}

sol::object prop_to_object(sol::this_state s, void* self, uevr::API::FProperty* desc) {
const auto propc = desc->get_class();

Expand Down Expand Up @@ -198,6 +221,37 @@ sol::object prop_to_object(sol::this_state s, void* self, uevr::API::FProperty*
}

return sol::make_object(s, *(uevr::API::UClass**)((uintptr_t)self + offset));
case L"StructProperty"_fnv:
{
const auto struct_data = (void*)((uintptr_t)self + offset);
const auto struct_desc = ((uevr::API::FStructProperty*)desc)->get_struct();

if (struct_desc == nullptr) {
return sol::make_object(s, sol::lua_nil);
}

/*const auto struct_name_hash = utility::hash(struct_desc->get_fname()->to_string());
switch (struct_name_hash) {
case L"Vector"_fnv:
if (is_ue5()) {
return sol::make_object(s, (lua::datatypes::Vector3f*)struct_data);
}
return sol::make_object(s, (lua::datatypes::Vector3f*)struct_data);
};*/

if (struct_desc == get_vector_struct()) {
if (is_ue5()) {
return sol::make_object(s, (lua::datatypes::Vector3d*)struct_data);
}

return sol::make_object(s, (lua::datatypes::Vector3f*)struct_data);
}

// TODO: Return a reflected struct
return sol::make_object(s, sol::lua_nil);
}
case L"ArrayProperty"_fnv:
{
const auto inner_prop = ((uevr::API::FArrayProperty*)desc)->get_inner();
Expand Down Expand Up @@ -510,7 +564,7 @@ sol::object call_function(sol::this_state s, uevr::API::UObject* self, const std
int ScriptContext::setup_bindings() {
m_lua.registry()["uevr_context"] = this;

m_lua.set_function("test_function", ScriptContext::test_function);
lua::datatypes::bind_vectors(m_lua);

m_lua.new_usertype<UEVR_PluginInitializeParam>("UEVR_PluginInitializeParam",
"uevr_module", &UEVR_PluginInitializeParam::uevr_module,
Expand Down
38 changes: 38 additions & 0 deletions lua-api/lib/src/datatypes/Vector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/euler_angles.hpp>
#include <glm/gtx/vector_angle.hpp>

#include <datatypes/Vector.hpp>

namespace lua::datatypes {
void bind_vectors(sol::state_view& lua) {
#define BIND_VECTOR3_LIKE(name, datatype) \
lua.new_usertype<name>(#name, \
"clone", [](name& v) -> name { return v; }, \
"x", &name::x, \
"y", &name::y, \
"z", &name::z, \
"dot", [](name& v1, name& v2) { return glm::dot(v1, v2); }, \
"cross", [](name& v1, name& v2) { return glm::cross(v1, v2); }, \
"length", [](name& v) { return glm::length(v); }, \
"normalize", [](name& v) { v = glm::normalize(v); }, \
"normalized", [](name& v) { return glm::normalize(v); }, \
"reflect", [](name& v, name& normal) { return glm::reflect(v, normal); }, \
"refract", [](name& v, name& normal, datatype eta) { return glm::refract(v, normal, eta); }, \
"lerp", [](name& v1, name& v2, datatype t) { return glm::lerp(v1, v2, t); }, \
sol::meta_function::addition, [](name& lhs, name& rhs) { return lhs + rhs; }, \
sol::meta_function::subtraction, [](name& lhs, name& rhs) { return lhs - rhs; }, \
sol::meta_function::multiplication, [](name& lhs, datatype scalar) { return lhs * scalar; }

#define BIND_VECTOR3_LIKE_END() \
);

BIND_VECTOR3_LIKE(Vector3f, float),
sol::meta_function::construct, sol::constructors<Vector3f(float, float, float)>()
BIND_VECTOR3_LIKE_END();

BIND_VECTOR3_LIKE(Vector3d, double),
sol::meta_function::construct, sol::constructors<Vector3d(double, double, double)>()
BIND_VECTOR3_LIKE_END();
}
}

0 comments on commit 2891e90

Please sign in to comment.