Skip to content

Commit

Permalink
Allow shorter module name specification in wesnoth.require
Browse files Browse the repository at this point in the history
Basically, you can now omit the "lua/" prefix and the ".lua" suffix.
If necessary, they will automatically be applied for you.
  • Loading branch information
CelticMinstrel committed May 3, 2017
1 parent 072ddc4 commit 6b74f91
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions data/lua/package.lua
@@ -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

0 comments on commit 6b74f91

Please sign in to comment.