From 0e8c70171873c004af0da518bb206a0f580b7bff Mon Sep 17 00:00:00 2001 From: Louis Poirier Date: Sat, 20 Jan 2024 11:51:02 +0100 Subject: [PATCH 1/2] feat: add ThrowLuaError in LuaVM Allow to throw an error in LuaVM using 'luaL_error'. --- src/scripting/LuaVM.cpp | 7 +++++++ src/scripting/LuaVM.h | 1 + 2 files changed, 8 insertions(+) diff --git a/src/scripting/LuaVM.cpp b/src/scripting/LuaVM.cpp index 7b9265aa..6d600b48 100644 --- a/src/scripting/LuaVM.cpp +++ b/src/scripting/LuaVM.cpp @@ -30,6 +30,13 @@ bool LuaVM::ExecuteLua(const std::string& acCommand) const return m_scripting.ExecuteLua(acCommand); } +int LuaVM::ThrowLuaError(const std::string& acMessage) const +{ + lua_State* state = m_scripting.GetLockedState().Get().lua_state(); + + return luaL_error(state, "%s", acMessage.c_str()); +} + void LuaVM::Update(float aDeltaTime) { if (!m_initialized) diff --git a/src/scripting/LuaVM.h b/src/scripting/LuaVM.h index be8d4291..d6beaf88 100644 --- a/src/scripting/LuaVM.h +++ b/src/scripting/LuaVM.h @@ -33,6 +33,7 @@ struct LuaVM [[nodiscard]] const TiltedPhoques::Map>>& GetAllBinds() const; bool ExecuteLua(const std::string& acCommand) const; + int ThrowLuaError(const std::string& acMessage) const; void Update(float aDeltaTime); void Draw() const; From 4b3c4f62b368174f685ad84b89bceef1c2271e3a Mon Sep 17 00:00:00 2001 From: Louis Poirier Date: Sat, 20 Jan 2024 11:56:47 +0100 Subject: [PATCH 2/2] feat: add overload operators for Vector3 and Vector4. Includes +, -, /, *, < and <= operations. Formats Lua usertypes for easier maintenance. --- src/reverse/BasicTypes.cpp | 70 +++++++++++++++++++++++++++++++++++++ src/reverse/BasicTypes.h | 12 +++++++ src/scripting/Scripting.cpp | 46 +++++++++++++++++++++--- 3 files changed, 123 insertions(+), 5 deletions(-) diff --git a/src/reverse/BasicTypes.cpp b/src/reverse/BasicTypes.cpp index 38effda0..fc6b7ea6 100644 --- a/src/reverse/BasicTypes.cpp +++ b/src/reverse/BasicTypes.cpp @@ -16,6 +16,41 @@ bool Vector3::operator==(const Vector3& acRhs) const noexcept return x == acRhs.x && y == acRhs.y && z == acRhs.z; } +bool Vector3::operator<(const Vector3& acRhs) const noexcept +{ + return x < acRhs.x && y < acRhs.y && z < acRhs.z; +} + +bool Vector3::operator<=(const Vector3& acRhs) const noexcept +{ + return x <= acRhs.x && y <= acRhs.y && z <= acRhs.z; +} + +Vector3 Vector3::operator+(const Vector3& acRhs) const noexcept +{ + return Vector3(x + acRhs.x, y + acRhs.y, z + acRhs.z); +} + +Vector3 Vector3::operator-(const Vector3& acRhs) const noexcept +{ + return Vector3(x - acRhs.x, y - acRhs.y, z - acRhs.z); +} + +Vector3 Vector3::operator*(const Vector3& acRhs) const noexcept +{ + return Vector3(x * acRhs.x, y * acRhs.y, z * acRhs.z); +} + +Vector3 Vector3::operator/(const Vector3& acRhs) const noexcept +{ + if (acRhs.x == 0 || acRhs.y == 0 || acRhs.z == 0) + { + CET::Get().GetVM().ThrowLuaError("Divide by zero exception"); + return acRhs; + } + return Vector3(x / acRhs.x, y / acRhs.y, z / acRhs.z); +} + std::string Vector4::ToString() const noexcept { return fmt::format("ToVector4{{ x = {}, y = {}, z = {}, w = {} }}", x, y, z, w); @@ -26,6 +61,41 @@ bool Vector4::operator==(const Vector4& acRhs) const noexcept return x == acRhs.x && y == acRhs.y && z == acRhs.z && w == acRhs.w; } +bool Vector4::operator<(const Vector4& acRhs) const noexcept +{ + return x < acRhs.x && y < acRhs.y && z < acRhs.z && w < acRhs.w; +} + +bool Vector4::operator<=(const Vector4& acRhs) const noexcept +{ + return x <= acRhs.x && y <= acRhs.y && z <= acRhs.z && w <= acRhs.w; +} + +Vector4 Vector4::operator+(const Vector4& acRhs) const noexcept +{ + return Vector4(x + acRhs.x, y + acRhs.y, z + acRhs.z, w + acRhs.w); +} + +Vector4 Vector4::operator-(const Vector4& acRhs) const noexcept +{ + return Vector4(x - acRhs.x, y - acRhs.y, z - acRhs.z, w - acRhs.w); +} + +Vector4 Vector4::operator*(const Vector4& acRhs) const noexcept +{ + return Vector4(x * acRhs.x, y * acRhs.y, z * acRhs.z, w * acRhs.w); +} + +Vector4 Vector4::operator/(const Vector4& acRhs) const noexcept +{ + if (acRhs.x == 0 || acRhs.y == 0 || acRhs.z == 0 || acRhs.w == 0) + { + CET::Get().GetVM().ThrowLuaError("Divide by zero exception"); + return acRhs; + } + return Vector4(x / acRhs.x, y / acRhs.y, z / acRhs.z, w / acRhs.w); +} + std::string EulerAngles::ToString() const noexcept { return fmt::format("ToEulerAngles{{ roll = {}, pitch = {}, yaw = {} }}", roll, pitch, yaw); diff --git a/src/reverse/BasicTypes.h b/src/reverse/BasicTypes.h index d1f82c85..444ab164 100644 --- a/src/reverse/BasicTypes.h +++ b/src/reverse/BasicTypes.h @@ -16,6 +16,12 @@ struct Vector3 std::string ToString() const noexcept; bool operator==(const Vector3& acRhs) const noexcept; + bool operator<(const Vector3& acRhs) const noexcept; + bool operator<=(const Vector3& acRhs) const noexcept; + Vector3 operator+(const Vector3& acRhs) const noexcept; + Vector3 operator-(const Vector3& acRhs) const noexcept; + Vector3 operator*(const Vector3& acRhs) const noexcept; + Vector3 operator/(const Vector3& acRhs) const noexcept; }; struct Vector4 @@ -36,6 +42,12 @@ struct Vector4 std::string ToString() const noexcept; bool operator==(const Vector4& acRhs) const noexcept; + bool operator<(const Vector4& acRhs) const noexcept; + bool operator<=(const Vector4& acRhs) const noexcept; + Vector4 operator+(const Vector4& acRhs) const noexcept; + Vector4 operator-(const Vector4& acRhs) const noexcept; + Vector4 operator*(const Vector4& acRhs) const noexcept; + Vector4 operator/(const Vector4& acRhs) const noexcept; }; struct EulerAngles diff --git a/src/scripting/Scripting.cpp b/src/scripting/Scripting.cpp index 1a188e2d..bc9eb418 100644 --- a/src/scripting/Scripting.cpp +++ b/src/scripting/Scripting.cpp @@ -231,8 +231,26 @@ void Scripting::PostInitializeScripting() }; luaVm.new_usertype( - "Vector3", sol::constructors(), sol::meta_function::to_string, - &Vector3::ToString, sol::meta_function::equal_to, &Vector3::operator==, "x", &Vector3::x, "y", &Vector3::y, "z", &Vector3::z); + "Vector3", + sol::constructors< + Vector3(float, float, float), + Vector3(float, float), + Vector3(float), + Vector3(const Vector3&), + Vector3() + >(), + sol::meta_function::to_string, &Vector3::ToString, + sol::meta_function::equal_to, &Vector3::operator==, + sol::meta_function::less_than, &Vector3::operator<, + sol::meta_function::less_than_or_equal_to, &Vector3::operator<=, + sol::meta_function::addition, &Vector3::operator+, + sol::meta_function::subtraction, &Vector3::operator-, + sol::meta_function::multiplication, &Vector3::operator*, + sol::meta_function::division, &Vector3::operator/, + "x", &Vector3::x, + "y", &Vector3::y, + "z", &Vector3::z + ); globals["Vector3"] = luaVm["Vector3"]; globals["ToVector3"] = [](sol::table table) -> Vector3 @@ -242,9 +260,27 @@ void Scripting::PostInitializeScripting() luaVm.new_usertype( "Vector4", - sol::constructors(), - sol::meta_function::to_string, &Vector4::ToString, sol::meta_function::equal_to, &Vector4::operator==, "x", &Vector4::x, "y", &Vector4::y, "z", &Vector4::z, "w", - &Vector4::w); + sol::constructors< + Vector4(float, float, float, float), + Vector4(float, float, float), + Vector4(float, float), + Vector4(float), + Vector4(const Vector4&), + Vector4() + >(), + sol::meta_function::to_string, &Vector4::ToString, + sol::meta_function::equal_to, &Vector4::operator==, + sol::meta_function::less_than, &Vector4::operator<, + sol::meta_function::less_than_or_equal_to, &Vector4::operator<=, + sol::meta_function::addition, &Vector4::operator+, + sol::meta_function::subtraction, &Vector4::operator-, + sol::meta_function::multiplication, &Vector4::operator*, + sol::meta_function::division, &Vector4::operator/, + "x", &Vector4::x, + "y", &Vector4::y, + "z", &Vector4::z, + "w", &Vector4::w + ); globals["Vector4"] = luaVm["Vector4"]; globals["ToVector4"] = [](sol::table table) -> Vector4