Allow wesnoth.require to load a whole directory in one call
- Use this feature when loading the WML tags from data/lua/wml
This commit is contained in:
parent
3eae385f3f
commit
690acf9b79
2 changed files with 21 additions and 14 deletions
|
@ -2,6 +2,9 @@
|
||||||
local empty_pkg = {}
|
local empty_pkg = {}
|
||||||
|
|
||||||
local function resolve_package(pkg_name)
|
local function resolve_package(pkg_name)
|
||||||
|
if pkg_name[#pkg_name] == '/' then
|
||||||
|
pkg_name = pkg_name:sub(1, -2)
|
||||||
|
end
|
||||||
if wesnoth.have_file(pkg_name) then return pkg_name end
|
if wesnoth.have_file(pkg_name) then return pkg_name end
|
||||||
if pkg_name:sub(-4) ~= ".lua" then
|
if pkg_name:sub(-4) ~= ".lua" then
|
||||||
if wesnoth.have_file(pkg_name .. ".lua") then
|
if wesnoth.have_file(pkg_name .. ".lua") then
|
||||||
|
@ -28,8 +31,21 @@ function wesnoth.require(pkg_name)
|
||||||
return wesnoth.package[loaded_name]
|
return wesnoth.package[loaded_name]
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Next, load the package with dofile
|
-- Next, if it's a single file, load the package with dofile
|
||||||
local pkg = wesnoth.dofile(loaded_name)
|
if wesnoth.have_file(loaded_name, true) then
|
||||||
wesnoth.package[loaded_name] = pkg or empty_pkg
|
local pkg = wesnoth.dofile(loaded_name)
|
||||||
return pkg
|
wesnoth.package[loaded_name] = pkg or empty_pkg
|
||||||
|
return pkg
|
||||||
|
else -- If it's a directory, load all the files therein
|
||||||
|
local files = wesnoth.read_file(loaded_name)
|
||||||
|
local pkg = {}
|
||||||
|
for i = files.ndirs + 1, #files do
|
||||||
|
if files[i]:sub(-4) == ".lua" then
|
||||||
|
local subpkg_name = files[i]:sub(1, -5)
|
||||||
|
pkg[subpkg_name] = wesnoth.require(loaded_name .. '/' .. files[i])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
wesnoth.package[loaded_name] = pkg
|
||||||
|
return pkg
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -12,16 +12,7 @@ function wesnoth.game_events.on_save()
|
||||||
end
|
end
|
||||||
|
|
||||||
wesnoth.require "wml-flow"
|
wesnoth.require "wml-flow"
|
||||||
wesnoth.require "wml/objectives"
|
wesnoth.require "wml"
|
||||||
wesnoth.require "wml/animate_unit"
|
|
||||||
wesnoth.require "wml/items"
|
|
||||||
wesnoth.require "wml/message"
|
|
||||||
wesnoth.require "wml/object"
|
|
||||||
wesnoth.require "wml/modify_unit"
|
|
||||||
wesnoth.require "wml/harm_unit"
|
|
||||||
wesnoth.require "wml/find_path"
|
|
||||||
wesnoth.require "wml/endlevel"
|
|
||||||
wesnoth.require "wml/random_placement"
|
|
||||||
|
|
||||||
local helper = wesnoth.require "helper"
|
local helper = wesnoth.require "helper"
|
||||||
local location_set = wesnoth.require "location_set"
|
local location_set = wesnoth.require "location_set"
|
||||||
|
|
Loading…
Add table
Reference in a new issue