Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow shorter module name specification in wesnoth.require
Basically, you can now omit the "lua/" prefix and the ".lua" suffix. If necessary, they will automatically be applied for you.
- Loading branch information
1 parent
072ddc4
commit 6b74f91
Showing
1 changed file
with
25 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,35 @@ | ||
|
||
local empty_pkg = {} | ||
|
||
local function resolve_package(pkg_name) | ||
if wesnoth.have_file(pkg_name) then return pkg_name end | ||
if pkg_name:sub(-4) ~= ".lua" then | ||
if wesnoth.have_file(pkg_name .. ".lua") then | ||
return pkg_name .. ".lua" | ||
end | ||
if pkg_name:sub(1, 4) ~= "lua/" then | ||
if wesnoth.have_file("lua/" .. pkg_name .. ".lua") then | ||
return "lua/" .. pkg_name .. ".lua" | ||
end | ||
end | ||
end | ||
if pkg_name:sub(1, 4) ~= "lua/" then | ||
if wesnoth.have_file("lua/" .. pkg_name) then | ||
return "lua/" .. pkg_name | ||
end | ||
end | ||
return nil | ||
end | ||
|
||
function wesnoth.require(pkg_name) | ||
-- First, check if the package is already loaded | ||
if wesnoth.package[pkg_name] then | ||
return wesnoth.package[pkg_name] | ||
local loaded_name = resolve_package(pkg_name) | ||
if loaded_name and wesnoth.package[loaded_name] then | ||
return wesnoth.package[loaded_name] | ||
end | ||
|
||
-- Next, load the package with dofile | ||
local pkg = wesnoth.dofile(pkg_name) | ||
wesnoth.package[pkg_name] = pkg or empty_pkg | ||
local pkg = wesnoth.dofile(loaded_name) | ||
wesnoth.package[loaded_name] = pkg or empty_pkg | ||
return pkg | ||
end |