Skip to content
Merged
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
11 changes: 6 additions & 5 deletions Sources/OvCore/include/OvCore/Scripting/Common/TScriptEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ namespace OvCore::Scripting
public:
/**
* Constructor of the generic script engine
* @param p_scriptsFolder
* @param p_engineResourcesFolder
* @param p_projectAssetsPath
* @param p_engineAssetsPath
*/
TScriptEngine(
const std::filesystem::path& p_scriptsFolder,
const std::filesystem::path& p_engineResourcesFolder
const std::filesystem::path& p_projectAssetsPath,
const std::filesystem::path& p_engineAssetsPath
);

/**
Expand All @@ -46,9 +46,10 @@ namespace OvCore::Scripting

/**
* Create necessary project files.
* @param p_projectFolder Root folder of the user's project
* @param p_force
*/
bool CreateProjectFiles(bool p_force = false);
bool CreateProjectFiles(const std::filesystem::path& p_projectFolder, bool p_force = false);

/**
* Returns a list of valid extensions for scripts.
Expand Down
12 changes: 6 additions & 6 deletions Sources/OvCore/include/OvCore/Scripting/Lua/LuaScriptEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ namespace OvCore::Scripting
struct LuaScriptEngineContext
{
std::unique_ptr<sol::state> luaState;
std::filesystem::path scriptRootFolder;
std::filesystem::path engineResourcesFolder;
std::filesystem::path projectAssetsPath;
std::filesystem::path engineAssetsPath;
std::vector<std::reference_wrapper<OvCore::ECS::Components::Behaviour>> behaviours;
uint32_t errorCount;
};
Expand All @@ -46,12 +46,12 @@ namespace OvCore::Scripting
public:
/**
* Constructor of the lua script engine
* @param p_scriptsFolder
* @param p_engineResourcesFolder
* @param p_projectAssetsPath
* @param p_engineAssetsPath
*/
LuaScriptEngine(
const std::filesystem::path& p_scriptsFolder,
const std::filesystem::path& p_engineResourcesFolder
const std::filesystem::path& p_projectAssetsPath,
const std::filesystem::path& p_engineAssetsPath
);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* @licence: MIT
*/

#include <filesystem>

#include <sol/sol.hpp>

#include <OvCore/ECS/Actor.h>
Expand Down Expand Up @@ -70,7 +72,29 @@ void BindLuaActor(sol::state& p_luaState)

/* Behaviours relatives */
"GetBehaviour", [](Actor& p_this, const std::string& p_name) -> sol::table {
if (auto behaviour = p_this.GetBehaviour(p_name))
// First try matching by script name (stem without path or extension)
OvCore::ECS::Components::Behaviour* behaviour = nullptr;
for (auto& [key, b] : p_this.GetBehaviours())
{
if (std::filesystem::path(b.name).stem().string() == p_name)
{
behaviour = &b;
break;
}
}

// Fall back to path-based match: try as-is, then with .lua appended if no extension given
if (!behaviour)
{
behaviour = p_this.GetBehaviour(p_name);
}

if (!behaviour && std::filesystem::path(p_name).extension().empty())
{
behaviour = p_this.GetBehaviour(p_name + ".lua");
}

if (behaviour)
Comment on lines +75 to +97
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not exactly thrilled about this, but this is necessary to keep all the GetBehaviour("{name}") from breaking after this update. Another option would be to move this code to GetBehaviour itself, but having lua-specific code in there wouldn't make much sense. Could definitely be improved in the future tho.

{
if (auto script = behaviour->GetScript())
{
Expand Down
44 changes: 27 additions & 17 deletions Sources/OvCore/src/OvCore/Scripting/Lua/LuaScriptEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,23 +129,23 @@ namespace

template<>
OvCore::Scripting::LuaScriptEngineBase::TScriptEngine(
const std::filesystem::path& p_scriptRootFolder,
const std::filesystem::path& p_engineResourcesFolder
const std::filesystem::path& p_projectAssetsPath,
const std::filesystem::path& p_engineAssetsPath
)
{
m_context.scriptRootFolder = p_scriptRootFolder;
m_context.engineResourcesFolder = p_engineResourcesFolder;
m_context.projectAssetsPath = p_projectAssetsPath;
m_context.engineAssetsPath = p_engineAssetsPath;
}

template<>
OvCore::Scripting::LuaScriptEngineBase::~TScriptEngine() {}

template<>
bool OvCore::Scripting::LuaScriptEngineBase::CreateProjectFiles(bool p_force)
bool OvCore::Scripting::LuaScriptEngineBase::CreateProjectFiles(const std::filesystem::path& p_projectFolder, bool p_force)
{
// Create a .luarc.json file inside the project's script folder.
// Create a .luarc.json file at the root of the user's project.
// This file will allow Lua LSPs to properly discover Lua symbols exposed by Overload.
const std::filesystem::path luarcPath = m_context.scriptRootFolder / ".luarc.json";
const std::filesystem::path luarcPath = p_projectFolder / ".luarc.json";

// Prevent the .luarc.json from being overrided UNLESS p_force is used
if (!p_force && std::filesystem::exists(luarcPath))
Expand All @@ -154,7 +154,7 @@ bool OvCore::Scripting::LuaScriptEngineBase::CreateProjectFiles(bool p_force)
}

std::ofstream luarc(luarcPath);
luarc << GetLuarcFileContent(m_context.engineResourcesFolder);
luarc << GetLuarcFileContent(m_context.engineAssetsPath);
return true;
}

Expand All @@ -173,7 +173,19 @@ std::vector<std::string> OvCore::Scripting::LuaScriptEngineBase::GetValidExtensi
template<>
std::string OvCore::Scripting::LuaScriptEngineBase::GetDefaultScriptContent(const std::string& p_name)
{
return "---@class " + p_name + " : Behaviour\nlocal " + p_name + " =\n{\n}\n\nfunction " + p_name + ":OnStart()\nend\n\nfunction " + p_name + ":OnUpdate(deltaTime)\nend\n\nreturn " + p_name;
return
"---@class " + p_name + " : Behaviour\n"
"local " + p_name + " =\n"
"{\n"
"}\n"
"\n"
"function " + p_name + ":OnStart()\n"
"end\n"
"\n"
"function " + p_name + ":OnUpdate(deltaTime)\n"
"end\n"
"\n"
"return " + p_name;
}

template<>
Expand All @@ -183,8 +195,7 @@ void OvCore::Scripting::LuaScriptEngineBase::AddBehaviour(OvCore::ECS::Component

m_context.behaviours.push_back(std::ref(p_toAdd));

const auto scriptFileName = p_toAdd.name + GetDefaultExtension();
const auto scriptPath = m_context.scriptRootFolder / scriptFileName;
const auto scriptPath = m_context.projectAssetsPath / p_toAdd.name;

if (!RegisterBehaviour(*m_context.luaState, p_toAdd, scriptPath.string()))
{
Expand Down Expand Up @@ -311,11 +322,11 @@ void OvCore::Scripting::LuaScriptEngineBase::OnTriggerExit(OvCore::ECS::Componen
}

OvCore::Scripting::LuaScriptEngine::LuaScriptEngine(
const std::filesystem::path& p_scriptsFolder,
const std::filesystem::path& p_engineResourcesFolder
const std::filesystem::path& p_projectAssetsPath,
const std::filesystem::path& p_engineAssetsPath
) : OvCore::Scripting::LuaScriptEngineBase(
p_scriptsFolder,
p_engineResourcesFolder
p_projectAssetsPath,
p_engineAssetsPath
)
{
CreateContext();
Expand All @@ -342,8 +353,7 @@ void OvCore::Scripting::LuaScriptEngine::CreateContext()

std::for_each(m_context.behaviours.begin(), m_context.behaviours.end(),
[this](std::reference_wrapper<OvCore::ECS::Components::Behaviour> behaviour) {
const auto scriptFileName = behaviour.get().name + GetDefaultExtension();
const auto scriptPath = m_context.scriptRootFolder / scriptFileName;
const auto scriptPath = m_context.projectAssetsPath / behaviour.get().name;
if (!RegisterBehaviour(*m_context.luaState, behaviour.get(), scriptPath.string()))
{
++m_context.errorCount;
Expand Down
6 changes: 3 additions & 3 deletions Sources/OvCore/src/OvCore/Scripting/Null/NullScriptEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@

template<>
OvCore::Scripting::NullScriptEngineBase::TScriptEngine(
const std::filesystem::path& p_scriptRootFolder,
const std::filesystem::path& p_engineResourcesFolder
const std::filesystem::path& p_projectAssetsPath,
const std::filesystem::path& p_engineAssetsPath
) {}

template<>
OvCore::Scripting::NullScriptEngineBase::~TScriptEngine() {}

template<>
bool OvCore::Scripting::NullScriptEngineBase::CreateProjectFiles(bool p_force) { return true; }
bool OvCore::Scripting::NullScriptEngineBase::CreateProjectFiles(const std::filesystem::path& p_projectFolder, bool p_force) { return true; }

template<>
std::string OvCore::Scripting::NullScriptEngineBase::GetDefaultExtension()
Expand Down
1 change: 0 additions & 1 deletion Sources/OvEditor/include/OvEditor/Core/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ namespace OvEditor::Core
const std::filesystem::path projectFile;
const std::filesystem::path engineAssetsPath;
const std::filesystem::path projectAssetsPath;
const std::filesystem::path projectScriptsPath;
const std::filesystem::path editorAssetsPath;

std::unique_ptr<OvWindowing::Context::Device> device;
Expand Down
16 changes: 8 additions & 8 deletions Sources/OvEditor/include/OvEditor/Core/EditorActions.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,18 @@ namespace OvEditor::Core
std::string GetResourcePath(const std::string& p_path, bool p_isFromEngine = false);

/**
* Returns the script path of a file
* Returns the script path of a file (relative to projectAssetsPath, forward-slash separated)
* @param p_path
*/
std::string GetScriptPath(const std::string& p_path);

/**
* Migrates scripts from a legacy Scripts/ folder into Assets/Scripts/.
* If a Scripts/ folder is found in the project root, prompts the user and
* moves it into Assets/, updating all scene files accordingly.
*/
void MigrateScripts();

/**
* Propagate the folder rename everywhere (Resource manager, scenes, materials...)
* @param p_previousName
Expand All @@ -313,13 +320,6 @@ namespace OvEditor::Core
*/
void PropagateFolderDestruction(std::string p_folderPath);

/**
* Propagate the script rename in scenes and inspector
* @param p_previousName
* @param p_newName
*/
void PropagateScriptRename(std::string p_previousName, std::string p_newName);

/**
* Propagate the file rename everywhere it is used
* @param p_previousName
Expand Down
4 changes: 2 additions & 2 deletions Sources/OvEditor/include/OvEditor/Panels/AssetBrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ namespace OvEditor::Panels
void Refresh();

private:
void ParseFolder(OvUI::Widgets::Layout::TreeNode& p_root, const std::filesystem::directory_entry& p_directory, bool p_isEngineItem, bool p_scriptFolder = false);
void ConsiderItem(OvUI::Widgets::Layout::TreeNode* p_root, const std::filesystem::directory_entry& p_entry, bool p_isEngineItem, bool p_autoOpen = false, bool p_scriptFolder = false);
void ParseFolder(OvUI::Widgets::Layout::TreeNode& p_root, const std::filesystem::directory_entry& p_directory, bool p_isEngineItem);
void ConsiderItem(OvUI::Widgets::Layout::TreeNode* p_root, const std::filesystem::directory_entry& p_entry, bool p_isEngineItem, bool p_autoOpen = false);

private:
OvUI::Widgets::Layout::Group* m_assetList;
Expand Down
4 changes: 2 additions & 2 deletions Sources/OvEditor/src/OvEditor/Core/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ OvEditor::Core::Context::Context(const std::filesystem::path& p_projectFolder) :
projectFile(Utils::ProjectManagement::GetProjectFile(p_projectFolder)),
engineAssetsPath(std::filesystem::current_path() / "Data" / "Engine"),
projectAssetsPath(projectFolder / "Assets"),
projectScriptsPath(projectFolder / "Scripts"),
editorAssetsPath(std::filesystem::current_path() / "Data" / "Editor"),
sceneManager(projectAssetsPath.string()),
projectSettings(projectFile.string())
Expand Down Expand Up @@ -149,14 +148,15 @@ OvEditor::Core::Context::Context(const std::filesystem::path& p_projectFolder) :

/* Scripting */
scriptEngine = std::make_unique<OvCore::Scripting::ScriptEngine>(
projectScriptsPath,
projectAssetsPath,
engineAssetsPath
);

// Ensures lua project files are up-to-date. This is necessary for Lua's LSP to function properly.
// If Overload's installation directory changes, references to engine symbols would be lost,
// hence this invocation.
scriptEngine->CreateProjectFiles(
projectFolder,
Settings::EditorSettings::RegenerateScriptingProjectFilesOnStartup
);

Expand Down
Loading