From 2c67f8ddc461c37921a4a062898beb39c1023e4f Mon Sep 17 00:00:00 2001 From: Ulhar Date: Wed, 18 Jan 2023 19:34:18 -0500 Subject: [PATCH] make require() paths more consistent in Utils.loadNamespace This only has an impact if passing a subdirectory to the function i.e. "ecs/systems" or whathaveyou require() will store its result in package.loaded, but different IDs will be stored under different keys in that table, even if they point to the same file i.e. "ecs/systems.collision" vs "ecs.systems.collision" Because one would not have the same identity as the other, require()ing the latter would return a different systemClass than the former, therefore could not be used by world:getSystem This change serves to make it easier to use require() to fetch a given systemClass to find within a world, by making loadNamespace convert the former ID to the latter --- concord/utils.lua | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/concord/utils.lua b/concord/utils.lua index 92e19aa..dced381 100644 --- a/concord/utils.lua +++ b/concord/utils.lua @@ -31,6 +31,10 @@ function Utils.loadNamespace(pathOrFiles, namespace) error("bad argument #1 to 'loadNamespace' (path '"..pathOrFiles.."' not found)", 2) end + -- normalizes the path to use dots instead of slashes + -- this is more friendly to require + local friendlyPath = pathOrFiles:gsub("%/", ".") + local files = love.filesystem.getDirectoryItems(pathOrFiles) for _, file in ipairs(files) do @@ -38,12 +42,12 @@ function Utils.loadNamespace(pathOrFiles, namespace) if isFile then local name = file:sub(1, #file - 4) - local path = pathOrFiles.."."..name + local path = friendlyPath.."."..name local value = require(path) if namespace then namespace[name] = value end else - local value = require(pathOrFiles.."."..file) + local value = require(friendlyPath.."."..file) if namespace then namespace[file] = value end end end