Skip to content

Commit

Permalink
Allow wesnoth.require to load a whole directory in one call
Browse files Browse the repository at this point in the history
- Use this feature when loading the WML tags from data/lua/wml
  • Loading branch information
CelticMinstrel committed May 3, 2017
1 parent 3eae385 commit 690acf9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
24 changes: 20 additions & 4 deletions data/lua/package.lua
Expand Up @@ -2,6 +2,9 @@
local empty_pkg = {}

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 pkg_name:sub(-4) ~= ".lua" then
if wesnoth.have_file(pkg_name .. ".lua") then
Expand All @@ -28,8 +31,21 @@ function wesnoth.require(pkg_name)
return wesnoth.package[loaded_name]
end

-- Next, load the package with dofile
local pkg = wesnoth.dofile(loaded_name)
wesnoth.package[loaded_name] = pkg or empty_pkg
return pkg
-- Next, if it's a single file, load the package with dofile
if wesnoth.have_file(loaded_name, true) then
local pkg = wesnoth.dofile(loaded_name)
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
11 changes: 1 addition & 10 deletions data/lua/wml-tags.lua
Expand Up @@ -12,16 +12,7 @@ function wesnoth.game_events.on_save()
end

wesnoth.require "wml-flow"
wesnoth.require "wml/objectives"
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"
wesnoth.require "wml"

local helper = wesnoth.require "helper"
local location_set = wesnoth.require "location_set"
Expand Down

0 comments on commit 690acf9

Please sign in to comment.