From cae023a2197c96984d6b874eb2ed38217350f90e Mon Sep 17 00:00:00 2001 From: Neloreck Date: Wed, 14 Feb 2024 02:33:32 +0200 Subject: [PATCH] Add gamedata as lua modules root. --- src/xrScriptEngine/script_engine.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/xrScriptEngine/script_engine.cpp b/src/xrScriptEngine/script_engine.cpp index 0f859179cf1..f2a6c10c8a6 100644 --- a/src/xrScriptEngine/script_engine.cpp +++ b/src/xrScriptEngine/script_engine.cpp @@ -997,6 +997,21 @@ void CScriptEngine::init(ExporterFunc exporterFunc, bool loadGlobalNamespace) luaL_dostring(lua(), mathRandom); } + // Adds gamedata folder as module root for lua `require` and allows usage of built-in lua module system. + // Notes: + // - Does not resolve files inside archived game files + // Example: + // `local example = require("scripts.folder.file")` tries to import `gamedata\scripts\folder\file.script` + { + string_path gamedataPath; + string_path packagePath; + + FS.update_path(gamedataPath, "$game_data$", "?.script;"); + xr_sprintf(packagePath, "package.path = package.path .. [[%s]]", gamedataPath); + + luaL_dostring(lua(), packagePath); + } + // XXX nitrocaster: with vanilla scripts, '-nojit' option requires script profiler to be disabled. The reason // is that lua hooks somehow make 'super' global unavailable (is's used all over the vanilla scripts). // You can disable script profiler by commenting out the following lines in the beginning of _g.script: