Skip to content

Commit 691a30f

Browse files
committed
Replace "!" in Lua path/cpath when changing it
This way we match Lua's behavior of replacing "!" when opening the package library instead of doing so in every call to searchpath
1 parent 45cbfe8 commit 691a30f

File tree

5 files changed

+21
-15
lines changed

5 files changed

+21
-15
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
- `LuaState.LoadMode` enum for specifying the Lua load mode: text, binary or any
99
- `LuaState.do_buffer` and `LuaState.load_buffer` methods for loading Lua code from possibly binary chunks
1010
- `LuaState.package_path` and `LuaState.package_cpath` properties for accessing the value of Lua's [`package.path`](https://www.lua.org/manual/5.4/manual.html#pdf-package.path) and [`package.cpath`](https://www.lua.org/manual/5.4/manual.html#pdf-package.cpath)
11+
- `LuaState.get_lua_exec_dir` static method to get the executable directory used to replace "!" when setting `package_path` and `package_cpath` properties.
12+
When running in the Godot editor, it returns the globalized version of `res://` path.
13+
Otherwise, it returns the base directory of the executable.
1114
- Advanced project settings for setting the `LuaScriptLanguage` state's `package_path` and `package_cpath` properties
1215

1316
### Changed

src/LuaState.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
#include "utils/module_names.hpp"
2929

3030
#include <godot_cpp/core/binder_common.hpp>
31-
#include <godot_cpp/classes/file_access.hpp>
31+
#include <godot_cpp/classes/engine.hpp>
32+
#include <godot_cpp/classes/os.hpp>
33+
#include <godot_cpp/classes/project_settings.hpp>
3234
#include <luaconf.h>
3335

3436
namespace luagdextension {
@@ -191,6 +193,7 @@ void LuaState::set_package_path(const String& path) {
191193
path.replace(";;", LUA_PATH_SEP LUA_PATH_DEFAULT LUA_PATH_SEP)
192194
.rstrip(LUA_PATH_SEP)
193195
.lstrip(LUA_PATH_SEP)
196+
.replace(LUA_EXEC_DIR, get_lua_exec_dir())
194197
);
195198
}
196199
else {
@@ -204,13 +207,20 @@ void LuaState::set_package_cpath(const String& cpath) {
204207
cpath.replace(";;", LUA_PATH_SEP LUA_CPATH_DEFAULT LUA_PATH_SEP)
205208
.rstrip(LUA_PATH_SEP)
206209
.lstrip(LUA_PATH_SEP)
210+
.replace(LUA_EXEC_DIR, get_lua_exec_dir())
207211
);
208212
}
209213
else {
210214
ERR_FAIL_MSG("LUA_PACKAGE library is not opened");
211215
}
212216
}
213217

218+
String LuaState::get_lua_exec_dir() {
219+
return Engine::get_singleton()->is_editor_hint()
220+
? ProjectSettings::get_singleton()->globalize_path("res://")
221+
: OS::get_singleton()->get_executable_path().get_base_dir();
222+
}
223+
214224
LuaState *LuaState::find_lua_state(lua_State *L) {
215225
L = sol::main_thread(L);
216226
if (LuaState **ptr = valid_states.getptr(L)) {
@@ -268,6 +278,7 @@ void LuaState::_bind_methods() {
268278
ClassDB::bind_method(D_METHOD("get_package_cpath"), &LuaState::get_package_cpath);
269279
ClassDB::bind_method(D_METHOD("set_package_path", "path"), &LuaState::set_package_path);
270280
ClassDB::bind_method(D_METHOD("set_package_cpath", "cpath"), &LuaState::set_package_cpath);
281+
ClassDB::bind_static_method(LuaState::get_class_static(), D_METHOD("get_lua_exec_dir"), &LuaState::get_lua_exec_dir);
271282

272283
// Properties
273284
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "globals", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE, LuaTable::get_class_static()), "", "get_globals");

src/LuaState.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ class LuaState : public RefCounted {
120120

121121
operator String() const;
122122

123+
static String get_lua_exec_dir();
123124
static LuaState *find_lua_state(lua_State *L);
124125

125126
protected:

src/luaopen/local_paths.cpp

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,12 @@
2121
*/
2222

2323
#include "../LuaTable.hpp"
24+
#include "../generated/package_searcher.h"
2425
#include "../utils/convert_godot_lua.hpp"
2526
#include "../utils/load_fileaccess.hpp"
26-
#include "sol/forward.hpp"
27-
#include "sol/load_result.hpp"
2827

29-
#include <godot_cpp/classes/engine.hpp>
3028
#include <godot_cpp/classes/file_access.hpp>
31-
#include <godot_cpp/classes/os.hpp>
32-
#include <godot_cpp/classes/project_settings.hpp>
33-
34-
#include "../generated/package_searcher.h"
29+
#include <luaconf.h>
3530

3631
using namespace luagdextension;
3732

@@ -43,15 +38,11 @@ static int l_searchpath(lua_State *L) {
4338
if (!sep.is_empty()) {
4439
name = name.replace(sep, rep);
4540
}
46-
47-
String execdir_repl = Engine::get_singleton()->is_editor_hint()
48-
? ProjectSettings::get_singleton()->globalize_path("res://")
49-
: OS::get_singleton()->get_executable_path().get_base_dir();
5041

51-
PackedStringArray path_list = path.split(";", false);
42+
PackedStringArray path_list = path.split(LUA_PATH_SEP, false);
5243
PackedStringArray not_found_list;
5344
for (const String& path_template : path_list) {
54-
String filename = path_template.replace("?", name).replace("!", execdir_repl);
45+
String filename = path_template.replace(LUA_PATH_MARK, name);
5546
if (FileAccess::file_exists(filename)) {
5647
sol::stack::push(L, filename);
5748
return 1;

test/gdscript_tests/package_path.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ var initial_package_cpath
77

88
func _init():
99
lua_state = LuaState.new()
10-
lua_state.registry.LUA_NOENV = true
1110
lua_state.open_libraries()
1211
initial_package_path = lua_state.package_path
1312
initial_package_cpath = lua_state.package_cpath
@@ -25,6 +24,7 @@ func test_package_path() -> bool:
2524
func test_package_cpath() -> bool:
2625
var package_cpath = "!/?.so"
2726
lua_state.package_cpath = package_cpath
27+
package_cpath = package_cpath.replace("!", LuaState.get_lua_exec_dir())
2828
assert(lua_state.package_cpath == package_cpath)
2929
assert(lua_state.globals.package.path != package_cpath)
3030
assert(lua_state.globals.package.cpath == package_cpath)

0 commit comments

Comments
 (0)