Skip to content

Commit

Permalink
Lua environment attached to engine, small fixes for lua scripting, etc..
Browse files Browse the repository at this point in the history
  • Loading branch information
Mormert committed Jul 25, 2023
1 parent 11d2871 commit c101740
Show file tree
Hide file tree
Showing 25 changed files with 841 additions and 646 deletions.
4 changes: 3 additions & 1 deletion engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ add_library(engine STATIC
"cRigidbody.cpp"
"cLuaScript.cpp"
"jleLuaScript.cpp"
"jleFileChangeNotifier.cpp")
"jleLuaScriptComponent.cpp"
"jleFileChangeNotifier.cpp"
"jleLuaEnvironment.cpp")

if (BUILD_EDITOR)
target_sources(engine PRIVATE
Expand Down
5 changes: 5 additions & 0 deletions engine/EditorResources/scripts/editor.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Main entry point for editor Lua scripts

luaEditor = luaEditor or {}

loadScript("ED:/scripts/pretty_table.lua")
82 changes: 82 additions & 0 deletions engine/EditorResources/scripts/pretty_table.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@

luaEditor = luaEditor or {}

function luaEditor.prettyTable(node)
local cache, stack, output = {}, {}, {}
local depth = 1
local output_str = "{\n"

while true do
local size = 0

for k, v in pairs(node) do
size = size + 1
end

local cur_index = 1
for k, v in pairs(node) do
if (cache[node] == nil) or (cur_index >= cache[node]) then

if (string.find(output_str, "}", output_str:len())) then
output_str = output_str .. ",\n"
elseif not (string.find(output_str, "\n", output_str:len())) then
output_str = output_str .. "\n"
end

-- This is necessary for working with HUGE tables otherwise we run out of memory using concat on huge strings
table.insert(output, output_str)
output_str = ""

local key
if (type(k) == "number" or type(k) == "boolean") then
key = "[" .. tostring(k) .. "]"
else
key = "['" .. tostring(k) .. "']"
end

if (type(v) == "number" or type(v) == "boolean") then
output_str = output_str .. string.rep('\t', depth) .. key .. " = " .. tostring(v)
elseif (type(v) == "table") then
output_str = output_str .. string.rep('\t', depth) .. key .. " = {\n"
table.insert(stack, node)
table.insert(stack, v)
cache[node] = cur_index + 1
break
else
output_str = output_str .. string.rep('\t', depth) .. key .. " = '" .. tostring(v) .. "'"
end

if (cur_index == size) then
output_str = output_str .. "\n" .. string.rep('\t', depth - 1) .. "}"
else
output_str = output_str .. ","
end
else
-- close the table
if (cur_index == size) then
output_str = output_str .. "\n" .. string.rep('\t', depth - 1) .. "}"
end
end

cur_index = cur_index + 1
end

if (size == 0) then
output_str = output_str .. "\n" .. string.rep('\t', depth - 1) .. "}"
end

if (#stack > 0) then
node = stack[#stack]
stack[#stack] = nil
depth = cache[node] == nil and depth + 1 or depth - 1
else
break
end
end

-- This is necessary for working with HUGE tables otherwise we run out of memory using concat on huge strings
table.insert(output, output_str)
output_str = table.concat(output)

return output_str
end
3 changes: 3 additions & 0 deletions engine/EngineResources/scripts/engine.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- Main entry point for engine Lua scripts

luaEngine = luaEngine or {}
30 changes: 17 additions & 13 deletions engine/cLuaScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,30 @@

cLuaScript::cLuaScript(jleObject *owner, jleScene *scene) : jleComponent(owner, scene) {}

cLuaScript::~cLuaScript()
{
// First destroy the 'self' object by making it empty, which is referencing the lua::state,
// which is first needed to avoid a crash. This will not do anything in editor mode, either.
_self = {};
_luaKeepAliveRef.reset();
}

void
cLuaScript::start()
{
if(!_scriptRef)
{
if (!_scriptRef) {
LOGE << "Can't start script since there is a reference issue";
runUpdate = false;
return;
}

_luaKeepAliveRef = _scriptRef->setupLua(_self, _attachedToObject);
_scriptRef->setupLua(_self, _attachedToObject);

if(!_specializationScript.empty())
{
sol::load_result fx = gEngine->luaEnvironment()->getState().load(_specializationScript);
if (!fx.valid()) {
sol::error err = fx;
LOGE << "Failed to load specialization script for " << _attachedToObject->_instanceName << ": " << err.what();
}

fx(_self);
}



_scriptRef->startLua(_self);
}

Expand All @@ -44,8 +49,7 @@ void
cLuaScript::onDestroy()
{
try {
if(_scriptRef)
{
if (_scriptRef) {
_scriptRef->onDestroyLua(_self);
}
} catch (std::exception &e) {
Expand Down
11 changes: 4 additions & 7 deletions engine/cLuaScript.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#pragma once

#include "jleComponent.h"
#include "jleLuaScript.h"
#include "jleLuaScriptComponent.h"
#include "jleResourceRef.h"

class cLuaScript : public jleComponent
Expand All @@ -12,13 +12,11 @@ class cLuaScript : public jleComponent
public:
explicit cLuaScript(jleObject *owner = nullptr, jleScene *scene = nullptr);

virtual ~cLuaScript();

template <class Archive>
void
serialize(Archive &ar)
{
ar( CEREAL_NVP(_scriptRef));
ar( CEREAL_NVP(_scriptRef), CEREAL_NVP(_specializationScript));
}

void start() override;
Expand All @@ -32,9 +30,8 @@ class cLuaScript : public jleComponent
bool runUpdate = true;

private:
jleResourceRef<jleLuaScript> _scriptRef;

std::shared_ptr<sol::state> _luaKeepAliveRef{};
jleResourceRef<jleLuaScriptComponent> _scriptRef;
std::string _specializationScript = "local self = ...;\n";
sol::table _self;

std::function<void(sol::table, float)> _updateLua;
Expand Down
15 changes: 12 additions & 3 deletions engine/editor/jleEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ jleEditor::start()
// AddImGuiWindow(gameController);
// menu->addWindow(gameController);

auto editorSceneObjects = std::make_shared<jleEditorSceneObjectsWindow>("Scene Objects");
addImGuiWindow(editorSceneObjects);
menu->addWindow(editorSceneObjects);
_editorSceneObjects = std::make_shared<jleEditorSceneObjectsWindow>("Scene Objects");
addImGuiWindow(_editorSceneObjects);
menu->addWindow(_editorSceneObjects);

auto contentBrowser = std::make_shared<jleEditorContentBrowser>("Content Browser", _textEditWindow, resourceEditor);
addImGuiWindow(contentBrowser);
Expand All @@ -129,6 +129,9 @@ jleEditor::start()
pointLightLampGizmoMesh = jleResourceRef<jleMesh>{jlePath{"ED:gizmos/models/gizmo_lamp.fbx"}};
directionalLightLampGizmoMesh = jleResourceRef<jleMesh>{jlePath{"ED:gizmos/models/gizmo_sun.fbx"}};

luaEnvironment()->loadScript("ER:/scripts/engine.lua");
luaEnvironment()->loadScript("ED:/scripts/editor.lua");

startRmlUi();

if (_editorSaveState->gameRunning) {
Expand Down Expand Up @@ -454,3 +457,9 @@ jleEditor::editorTextEdit()
{
return *_textEditWindow;
}

jleEditorSceneObjectsWindow &
jleEditor::editorSceneObjects()
{
return *_editorSceneObjects;
}
8 changes: 6 additions & 2 deletions engine/editor/jleEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class jleFramebufferInterface;
class jleSceneEditorWindow;
class jleFileChangeNotifier;
class jleEditorTextEdit;
class jleEditorSceneObjectsWindow;

class jleEditor;
inline jleEditor *gEditor;
Expand Down Expand Up @@ -70,8 +71,9 @@ class jleEditor : public jleGameEngine

std::vector<std::shared_ptr<jleScene>> &getEditorScenes();

jleEditorTextEdit &
editorTextEdit();
jleEditorTextEdit &editorTextEdit();

jleEditorSceneObjectsWindow &editorSceneObjects();

bool
checkSceneIsActiveEditor(const std::string &sceneName)
Expand Down Expand Up @@ -136,6 +138,8 @@ class jleEditor : public jleGameEngine

std::shared_ptr<jleSceneEditorWindow> _sceneWindow;

std::shared_ptr<jleEditorSceneObjectsWindow> _editorSceneObjects;

std::unique_ptr<jleFileChangeNotifier> _fileChangeNotifier;

std::shared_ptr<jleEditorTextEdit> _textEditWindow;
Expand Down
2 changes: 1 addition & 1 deletion engine/editor/jleEditorContentBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ jleEditorContentBrowser::selectedFilePopupObjectTemplate(std::filesystem::path &
objectName.resize(dot);
}

if (auto &&scene = jleEditorSceneObjectsWindow::GetSelectedScene().lock()) {
if (auto &&scene = gEditor->editorSceneObjects().GetSelectedScene().lock()) {
try {
std::shared_ptr<jleObject> object;

Expand Down
16 changes: 16 additions & 0 deletions engine/editor/jleEditorSceneObjectsWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "jleNetScene.h"
#include "jleTypeReflectionUtils.h"

#include <cLuaScript.h>
#include <filesystem>
#include <fstream>

Expand Down Expand Up @@ -206,6 +207,21 @@ jleEditorSceneObjectsWindow::update(jleGameEngine &ge)
} else {
cereal::jleImGuiCerealArchive ar1;
ar1(*selectedObjectSafePtr);

if(!gEngine->isGameKilled())
{
if(auto luaScript = selectedObjectSafePtr->getComponent<cLuaScript>())
{
ImGui::Text("Lua Object:");

sol::protected_function f = gEditor->luaEnvironment()->getState()["luaEditor"]["prettyTable"];
std::string prettyTable = f(luaScript->getSelf());

ImGui::BeginChild("LuaPretty", ImVec2(0, 0), true);
ImGui::TextWrapped("%s", prettyTable.c_str());
ImGui::EndChild();
}
}
}

ImGui::EndTabItem();
Expand Down
11 changes: 6 additions & 5 deletions engine/editor/jleEditorSceneObjectsWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "jleEditorImGuiWindowInterface.h"
#include "jleEditorJsonToImgui.h"
#include <jleLuaScript.h>

#ifdef BUILD_EDITOR

Expand All @@ -13,17 +14,17 @@ class jleEditorSceneObjectsWindow : public iEditorImGuiWindow {

void update(jleGameEngine &ge) override;

static std::weak_ptr<jleObject> &GetSelectedObject();
std::weak_ptr<jleObject> &GetSelectedObject();

static void SetSelectedObject(std::shared_ptr<jleObject> object);
void SetSelectedObject(std::shared_ptr<jleObject> object);

static std::weak_ptr<jleScene> &GetSelectedScene();
std::weak_ptr<jleScene> &GetSelectedScene();

private:

// Using a static weak_ptr here so that it won't impact deletion
static inline std::weak_ptr<jleObject> selectedObject;
static inline std::weak_ptr<jleScene> selectedScene;
std::weak_ptr<jleObject> selectedObject;
std::weak_ptr<jleScene> selectedScene;

void objectTreeRecursive(std::shared_ptr<jleObject> object);
};
Expand Down
4 changes: 2 additions & 2 deletions engine/editor/jleSceneEditorWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ jleSceneEditorWindow::update(jleGameEngine &ge)
_framebuffer->resize(_lastGameWindowWidth, _lastGameWindowHeight);
}

const auto &selectedObject = jleEditorSceneObjectsWindow::GetSelectedObject();
const auto &selectedObject = gEditor->editorSceneObjects().GetSelectedObject();

glBindTexture(GL_TEXTURE_2D, (unsigned int)_framebuffer->texture());
jleStaticOpenGLState::globalActiveTexture = (unsigned int)_framebuffer->texture();
Expand Down Expand Up @@ -162,7 +162,7 @@ jleSceneEditorWindow::update(jleGameEngine &ge)
std::shared_ptr<jleObject> o{};
object->tryFindChildWithInstanceId(pickedID, o);
if (o) {
jleEditorSceneObjectsWindow::SetSelectedObject(o);
gEditor->editorSceneObjects().SetSelectedObject(o);
}
}
}
Expand Down
Loading

0 comments on commit c101740

Please sign in to comment.