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
This commit is contained in:
Ulhar 2023-01-18 19:34:18 -05:00
parent a45d89457b
commit 2c67f8ddc4

View file

@ -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