11#include < algorithm>
2+ #include < string>
23
34#include < cubos/bindings/lua/plugin.hpp>
45#include < lua.hpp>
56
67#include < cubos/core/data/fs/file_system.hpp>
78#include < cubos/core/data/fs/standard_archive.hpp>
9+ #include < cubos/core/ecs/system/arguments/plugins.hpp>
810
911#include < cubos/engine/settings/plugin.hpp>
1012
13+ #include " cubos.hpp"
14+ #include " cubos/engine/prelude.hpp"
15+
1116using namespace cubos ::engine;
1217using cubos::core::data::FileSystem;
1318using cubos::core::data::StandardArchive;
19+ using cubos::core::ecs::Plugins;
1420
15- static constexpr std::string_view ScriptsMountPoint = " /scripts/lua" ;
21+ static constexpr std::string ScriptsMountPoint = " /scripts/lua" ;
1622
1723namespace
1824{
@@ -34,7 +40,7 @@ namespace
3440 };
3541} // namespace
3642
37- static void loadScripts (std::string_view path, State& state)
43+ static void loadScripts (std::string_view path, lua_State* state)
3844{
3945 auto directory = FileSystem::find (path);
4046 if (directory != nullptr && directory->directory ())
@@ -50,9 +56,9 @@ static void loadScripts(std::string_view path, State& state)
5056 {
5157 std::string moduleName = std::string{child->path ().substr (
5258 ScriptsMountPoint.length () + 1 , child->path ().length () - 5 - ScriptsMountPoint.length ())};
53- std::replace (moduleName. begin (), moduleName. end () , ' /' , ' .' );
59+ std::ranges:: replace (moduleName, ' /' , ' .' );
5460 std::string execString = std::string{" require('" } + moduleName + " ')" ;
55- luaL_dostring (state. luaState , execString.c_str ());
61+ luaL_dostring (state, execString.c_str ());
5662 }
5763 child = child->sibling ();
5864 }
@@ -63,7 +69,7 @@ static int searchModule(lua_State* state)
6369{
6470 const char * moduleName = luaL_checkstring (state, 1 );
6571 std::string module = moduleName;
66- std::replace (module . begin (), module . end () , ' .' , ' /' );
72+ std::ranges:: replace (module , ' .' , ' /' );
6773 module = module + " .lua" ;
6874
6975 lua_pushstring (state, " scriptsPath" );
@@ -91,7 +97,7 @@ static int searchModule(lua_State* state)
9197static void setupCustomLoader (lua_State* state)
9298{
9399 lua_pushstring (state, " scriptsPath" );
94- lua_pushstring (state, ScriptsMountPoint.data ());
100+ lua_pushstring (state, ScriptsMountPoint.c_str ());
95101 lua_settable (state, LUA_REGISTRYINDEX);
96102
97103 lua_getglobal (state, " package" );
@@ -108,15 +114,15 @@ void cubos::bindings::lua::luaBindingsPlugin(Cubos& cubos)
108114
109115 cubos.uninitResource <State>();
110116
111- cubos.startupSystem (" initialize lua bindings" ).before (settingsTag).call ([](Commands cmds) {
117+ cubos.startupSystem (" initialize lua bindings" ).before (settingsTag).call ([&cubos ](Commands cmds) {
112118 lua_State* state = luaL_newstate ();
113119 luaL_openlibs (state);
114120 setupCustomLoader (state);
115- // TODO: add custom cubos functions here
121+ injectCubos (state, & cubos);
116122 cmds.emplaceResource <State>(state);
117123 });
118124
119- cubos.startupSystem (" load lua scripts" ).after (settingsTag).call ([](Settings& settings, State& state ) {
125+ cubos.startupSystem (" load lua scripts" ).after (settingsTag).call ([](Settings& settings, Plugins plugins ) {
120126 std::string scriptsPath = settings.getString (" scripts.lua.app.osPath" , " " );
121127 if (!scriptsPath.empty ())
122128 {
@@ -129,8 +135,11 @@ void cubos::bindings::lua::luaBindingsPlugin(Cubos& cubos)
129135 {
130136 CUBOS_ERROR (" Couldn't mount scripts directory with path {}" , scriptsPath);
131137 }
132-
133- loadScripts (ScriptsMountPoint, state);
138+ (void )plugins;
139+ plugins.add ([](Cubos& other) {
140+ auto & state = other.world ()->resource <State>();
141+ loadScripts (ScriptsMountPoint, state.luaState );
142+ });
134143 }
135144 });
136145}
0 commit comments