Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ jobs:
run: |
CONFIG_LOWER=$(echo "${{ matrix.configuration }}" | tr '[:upper:]' '[:lower:]')
make -j$(nproc) config=${CONFIG_LOWER}_x64

- name: Run Lua API tests
working-directory: ${{env.GITHUB_WORKSPACE}}
run: ./Bin/${{ matrix.configuration }}/OvLuaApiTests/OvLuaApiTests
4 changes: 4 additions & 0 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ jobs:
- name: Build
working-directory: ${{env.GITHUB_WORKSPACE}}
run: msbuild /m /p:Configuration=${{ matrix.configuration }} ${{env.SOLUTION_FILE_PATH}} /p:Platform=x64

- name: Run Lua API tests
working-directory: ${{env.GITHUB_WORKSPACE}}
run: .\Bin\${{ matrix.configuration }}\OvLuaApiTests\OvLuaApiTests.exe
51 changes: 51 additions & 0 deletions Sources/OvCore/include/OvCore/Scripting/Lua/LuaBindings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* @project: Overload
* @author: Overload Tech.
* @licence: MIT
*/

#pragma once

namespace sol
{
class state;
}

namespace OvCore::Scripting::Lua
{
/**
* Registers the standard Lua libraries and every symbol exposed by Overload.
* @param p_luaState
*/
void BindLuaApi(sol::state& p_luaState);

/**
* Registers actor bindings exposed to Lua scripts.
* @param p_luaState
*/
void BindLuaActor(sol::state& p_luaState);

/**
* Registers component bindings exposed to Lua scripts.
* @param p_luaState
*/
void BindLuaComponents(sol::state& p_luaState);

/**
* Registers global engine bindings exposed to Lua scripts.
* @param p_luaState
*/
void BindLuaGlobal(sol::state& p_luaState);

/**
* Registers math bindings exposed to Lua scripts.
* @param p_luaState
*/
void BindLuaMath(sol::state& p_luaState);

/**
* Registers profiler bindings exposed to Lua scripts.
* @param p_luaState
*/
void BindLuaProfiler(sol::state& p_luaState);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
#include <OvCore/ECS/Components/CReflectionProbe.h>
#include <OvCore/ECS/Components/CSkinnedMeshRenderer.h>
#include <OvCore/ECS/Components/CSpotLight.h>
#include <OvCore/Scripting/Lua/LuaBindings.h>
#include <OvCore/Scripting/Lua/LuaScriptEngine.h>

void BindLuaActor(sol::state& p_luaState)
void OvCore::Scripting::Lua::BindLuaActor(sol::state& p_luaState)
{
using namespace OvCore::ECS;
using namespace OvCore::ECS::Components;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
#include <OvCore/ECS/Components/CSkinnedMeshRenderer.h>
#include <OvCore/ECS/Components/CSpotLight.h>
#include <OvCore/ECS/Components/CTransform.h>
#include <OvCore/Scripting/Lua/LuaBindings.h>

void BindLuaComponents(sol::state& p_luaState)
void OvCore::Scripting::Lua::BindLuaComponents(sol::state& p_luaState)
{
using namespace OvMaths;
using namespace OvCore::ECS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@
#include "OvCore/ResourceManagement/MaterialManager.h"
#include "OvCore/ResourceManagement/SoundManager.h"
#include "OvCore/Scripting/Common/ScriptPropertyValue.h"
#include <OvCore/Scripting/Lua/LuaBindings.h>

#include <OvPhysics/Entities/PhysicalObject.h>

#include <OvWindowing/Inputs/InputManager.h>

#include <sol/sol.hpp>

void BindLuaGlobal(sol::state& p_luaState)
void OvCore::Scripting::Lua::BindLuaGlobal(sol::state& p_luaState)
{
using namespace OvWindowing;
using namespace OvWindowing::Inputs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
#include <OvMaths/FMatrix3.h>
#include <OvMaths/FMatrix4.h>
#include <OvMaths/FQuaternion.h>
#include <OvCore/Scripting/Lua/LuaBindings.h>

#include <sol/sol.hpp>

void BindLuaMath(sol::state& p_luaState)
void OvCore::Scripting::Lua::BindLuaMath(sol::state& p_luaState)
{
using namespace OvMaths;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#include <sol/sol.hpp>
#include <tracy/TracyLua.hpp>

void BindLuaProfiler(sol::state& p_luaState)
#include <OvCore/Scripting/Lua/LuaBindings.h>

void OvCore::Scripting::Lua::BindLuaProfiler(sol::state& p_luaState)
{
tracy::LuaRegister(p_luaState.lua_state());
}
20 changes: 20 additions & 0 deletions Sources/OvCore/src/OvCore/Scripting/Lua/LuaBindings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @project: Overload
* @author: Overload Tech.
* @licence: MIT
*/

#include <OvCore/Scripting/Lua/LuaBindings.h>

#include <sol/sol.hpp>

void OvCore::Scripting::Lua::BindLuaApi(sol::state& p_luaState)
{
p_luaState.open_libraries(sol::lib::base, sol::lib::math);

BindLuaActor(p_luaState);
BindLuaComponents(p_luaState);
BindLuaGlobal(p_luaState);
BindLuaMath(p_luaState);
BindLuaProfiler(p_luaState);
}
15 changes: 2 additions & 13 deletions Sources/OvCore/src/OvCore/Scripting/Lua/LuaScriptEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,9 @@
#include <OvCore/Scripting/ScriptEngine.h>
#include <OvCore/ECS/Components/Behaviour.h>
#include <OvCore/ECS/Actor.h>
#include <OvCore/Scripting/Lua/LuaBindings.h>
#include <OvTools/Utils/String.h>

void BindLuaActor(sol::state& p_state);
void BindLuaComponents(sol::state& p_state);
void BindLuaGlobal(sol::state& p_state);
void BindLuaMath(sol::state& p_state);
void BindLuaProfiler(sol::state& p_state);

namespace
{
template<typename... Args>
Expand Down Expand Up @@ -345,13 +340,7 @@ void OvCore::Scripting::LuaScriptEngine::CreateContext()
OVASSERT(m_context.luaState == nullptr, "A Lua context already exists!");

m_context.luaState = std::make_unique<sol::state>();
m_context.luaState->open_libraries(sol::lib::base, sol::lib::math);

BindLuaActor(*m_context.luaState);
BindLuaComponents(*m_context.luaState);
BindLuaGlobal(*m_context.luaState);
BindLuaMath(*m_context.luaState);
BindLuaProfiler(*m_context.luaState);
OvCore::Scripting::Lua::BindLuaApi(*m_context.luaState);

m_context.errorCount = 0;

Expand Down
38 changes: 38 additions & 0 deletions Tests/OvLuaApiTests/Lua/Globals/ApiSurface.test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
AssertEquals("table", type(Debug), "Debug")
AssertCallable(Debug.Log, "Debug.Log")
AssertCallable(Debug.LogInfo, "Debug.LogInfo")
AssertCallable(Debug.LogWarning, "Debug.LogWarning")
AssertCallable(Debug.LogError, "Debug.LogError")

AssertEquals("table", type(Inputs), "Inputs")
AssertCallable(Inputs.GetKeyDown, "Inputs.GetKeyDown")
AssertCallable(Inputs.GetMousePos, "Inputs.GetMousePos")

AssertEquals("table", type(Scenes), "Scenes")
AssertCallable(Scenes.GetCurrentScene, "Scenes.GetCurrentScene")
AssertCallable(Scenes.Load, "Scenes.Load")

AssertEquals("table", type(Resources), "Resources")
AssertCallable(Resources.GetModel, "Resources.GetModel")
AssertCallable(Resources.GetTexture, "Resources.GetTexture")
AssertCallable(Resources.GetSound, "Resources.GetSound")

AssertEquals("table", type(Math), "Math")
AssertCallable(Math.Lerp, "Math.Lerp")
AssertNear(4, Math.Lerp(2, 6, 0.5), 0.0001, "Math.Lerp")

AssertEquals("table", type(Physics), "Physics")
AssertCallable(Physics.Raycast, "Physics.Raycast")

AssertEquals("table", type(Vector2), "Vector2")
AssertCallable(Vector2.new, "Vector2.new")
AssertEquals("table", type(Vector3), "Vector3")
AssertCallable(Vector3.new, "Vector3.new")
AssertEquals("table", type(Vector4), "Vector4")
AssertCallable(Vector4.new, "Vector4.new")
AssertEquals("table", type(Matrix3), "Matrix3")
AssertCallable(Matrix3.new, "Matrix3.new")
AssertEquals("table", type(Matrix4), "Matrix4")
AssertCallable(Matrix4.new, "Matrix4.new")
AssertEquals("table", type(Quaternion), "Quaternion")
AssertCallable(Quaternion.new, "Quaternion.new")
30 changes: 30 additions & 0 deletions Tests/OvLuaApiTests/Lua/Globals/Enums.test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
AssertTrue(Key.UNKNOWN ~= nil, "Key.UNKNOWN is registered")
AssertTrue(Key.SPACE ~= nil, "Key.SPACE is registered")
AssertTrue(Key.A == Key.A, "Key equality is stable")
AssertTrue(Key.A ~= Key.B, "Key inequality is stable")

AssertTrue(MouseButton.BUTTON_1 ~= nil, "MouseButton.BUTTON_1 is registered")
AssertTrue(MouseButton.BUTTON_LEFT == MouseButton.BUTTON_1, "left mouse button alias")
AssertTrue(MouseButton.BUTTON_RIGHT == MouseButton.BUTTON_2, "right mouse button alias")
AssertTrue(MouseButton.BUTTON_MIDDLE == MouseButton.BUTTON_3, "middle mouse button alias")

AssertTrue(CollisionDetectionMode.DISCRETE ~= nil, "CollisionDetectionMode.DISCRETE is registered")
AssertTrue(CollisionDetectionMode.DISCRETE ~= CollisionDetectionMode.CONTINUOUS, "collision modes are distinct")

AssertTrue(ProjectionMode.ORTHOGRAPHIC ~= nil, "ProjectionMode.ORTHOGRAPHIC is registered")
AssertTrue(ProjectionMode.ORTHOGRAPHIC ~= ProjectionMode.PERSPECTIVE, "projection modes are distinct")

AssertTrue(FrustumBehaviour.DISABLED ~= nil, "FrustumBehaviour.DISABLED is registered")
AssertTrue(FrustumBehaviour.DISABLED ~= FrustumBehaviour.MESH_BOUNDS, "frustum behaviours are distinct")

AssertTrue(TonemappingMode.NEUTRAL ~= nil, "TonemappingMode.NEUTRAL is registered")
AssertTrue(TonemappingMode.NEUTRAL ~= TonemappingMode.ACES, "tonemapping modes are distinct")

AssertTrue(ReflectionProbeRefreshMode.REALTIME ~= nil, "ReflectionProbeRefreshMode.REALTIME is registered")
AssertTrue(ReflectionProbeRefreshMode.REALTIME ~= ReflectionProbeRefreshMode.ONCE, "reflection refresh modes are distinct")

AssertTrue(ReflectionProbeCaptureSpeed.ONE_FACE ~= nil, "ReflectionProbeCaptureSpeed.ONE_FACE is registered")
AssertTrue(ReflectionProbeCaptureSpeed.ONE_FACE ~= ReflectionProbeCaptureSpeed.SIX_FACES, "reflection capture speeds are distinct")

AssertTrue(ReflectionProbeInfluencePolicy.GLOBAL ~= nil, "ReflectionProbeInfluencePolicy.GLOBAL is registered")
AssertTrue(ReflectionProbeInfluencePolicy.GLOBAL ~= ReflectionProbeInfluencePolicy.LOCAL, "reflection influence policies are distinct")
31 changes: 31 additions & 0 deletions Tests/OvLuaApiTests/Lua/Globals/Factories.test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
local model = Model()
AssertEquals("userdata", type(model), "Model factory")
AssertEquals("", model.path, "Model default path")
model.path = "Assets/Models/Ship.fbx"
AssertEquals("Assets/Models/Ship.fbx", model.path, "Model path mutation")

local texture = Texture()
AssertEquals("userdata", type(texture), "Texture factory")
AssertEquals("", texture.path, "Texture default path")

local shader = Shader()
AssertEquals("userdata", type(shader), "Shader factory")
AssertEquals("", shader.path, "Shader default path")

local material = Material()
AssertEquals("userdata", type(material), "Material factory")
AssertEquals("", material.path, "Material default path")

local sound = Sound()
AssertEquals("userdata", type(sound), "Sound factory")
AssertEquals("", sound.path, "Sound default path")

local prefab = Prefab()
AssertEquals("userdata", type(prefab), "Prefab factory")
AssertEquals("", prefab.path, "Prefab default path")

local actor = Actor()
AssertEquals("userdata", type(actor), "Actor factory")
AssertEquals(0, actor.guid, "Actor default guid")
actor.guid = 42
AssertEquals(42, actor.guid, "Actor guid mutation")
29 changes: 29 additions & 0 deletions Tests/OvLuaApiTests/Lua/Math/Vector2.test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
local epsilon = 0.0001

local defaultVector = Vector2.new()
AssertVector2Equals(0, 0, defaultVector, epsilon, "default constructor")

local a = Vector2.new(3, 4)
local b = Vector2.new(1, 2)

AssertVector2Equals(3, 4, a, epsilon, "constructor")
AssertVector2Equals(1, 1, Vector2.One(), epsilon, "Vector2.One")
AssertVector2Equals(0, 0, Vector2.Zero(), epsilon, "Vector2.Zero")

AssertVector2Equals(4, 6, a + b, epsilon, "addition")
AssertVector2Equals(2, 2, a - b, epsilon, "subtraction")
AssertVector2Equals(-3, -4, -a, epsilon, "unary minus")
AssertVector2Equals(6, 8, a * 2, epsilon, "scalar multiplication")
AssertVector2Equals(1.5, 2, a / 2, epsilon, "scalar division")

AssertNear(5, a:Length(), epsilon, "length")
AssertNear(11, a:Dot(b), epsilon, "dot")
AssertVector2Equals(0.6, 0.8, a:Normalize(), epsilon, "normalize")
AssertVector2Equals(0, 0, Vector2.Zero():Normalize(), epsilon, "zero normalize")
AssertVector2Equals(2, 3, Vector2.Lerp(b, a, 0.5), epsilon, "lerp")

local ok = pcall(function()
return a / 0
end)

AssertFalse(ok, "division by zero fails")
37 changes: 37 additions & 0 deletions Tests/OvLuaApiTests/Lua/Math/Vector3.test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
local epsilon = 0.0001

local defaultVector = Vector3.new()
AssertVector3Equals(0, 0, 0, defaultVector, epsilon, "default constructor")

local a = Vector3.new(1, 2, 3)
local b = Vector3.new(4, 5, 6)

AssertVector3Equals(1, 2, 3, a, epsilon, "constructor")
AssertVector3Equals(1, 1, 1, Vector3.One(), epsilon, "Vector3.One")
AssertVector3Equals(0, 0, 0, Vector3.Zero(), epsilon, "Vector3.Zero")
AssertVector3Equals(0, 0, 1, Vector3.Forward(), epsilon, "Vector3.Forward")
AssertVector3Equals(0, 0, -1, Vector3.Backward(), epsilon, "Vector3.Backward")
AssertVector3Equals(0, 1, 0, Vector3.Up(), epsilon, "Vector3.Up")
AssertVector3Equals(0, -1, 0, Vector3.Down(), epsilon, "Vector3.Down")
AssertVector3Equals(1, 0, 0, Vector3.Right(), epsilon, "Vector3.Right")
AssertVector3Equals(-1, 0, 0, Vector3.Left(), epsilon, "Vector3.Left")

AssertVector3Equals(5, 7, 9, a + b, epsilon, "addition")
AssertVector3Equals(-3, -3, -3, a - b, epsilon, "subtraction")
AssertVector3Equals(-1, -2, -3, -a, epsilon, "unary minus")
AssertVector3Equals(2, 4, 6, a * 2, epsilon, "scalar multiplication")
AssertVector3Equals(4, 10, 18, a * b, epsilon, "component multiplication")
AssertVector3Equals(0.5, 1, 1.5, a / 2, epsilon, "scalar division")

AssertNear(math.sqrt(14), a:Length(), epsilon, "length")
AssertNear(32, a:Dot(b), epsilon, "dot")
AssertVector3Equals(-3, 6, -3, a:Cross(b), epsilon, "cross")
AssertVector3Equals(0, 0, 0, Vector3.Zero():Normalize(), epsilon, "zero normalize")
AssertVector3Equals(2.5, 3.5, 4.5, Vector3.Lerp(a, b, 0.5), epsilon, "lerp")
AssertNear(math.sqrt(27), Vector3.Distance(a, b), epsilon, "distance")

local ok = pcall(function()
return a / 0
end)

AssertFalse(ok, "division by zero fails")
Loading
Loading