From 6b74f91b2feb4bca38f8675d141f41a455525e17 Mon Sep 17 00:00:00 2001 From: Celtic Minstrel Date: Wed, 3 May 2017 01:43:18 -0400 Subject: [PATCH] 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. --- data/lua/package.lua | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/data/lua/package.lua b/data/lua/package.lua index d4b6f728d23..0c4a7bd589f 100644 --- a/data/lua/package.lua +++ b/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