File: utils.lua

package info (click to toggle)
spring 106.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 55,260 kB
  • sloc: cpp: 543,946; ansic: 44,800; python: 12,575; java: 12,201; awk: 5,889; sh: 1,796; asm: 1,546; xml: 655; perl: 405; php: 211; objc: 194; makefile: 76; sed: 2
file content (127 lines) | stat: -rw-r--r-- 3,791 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
--  file:    utils.lua
--  brief:   utility routines
--  author:  Dave Rodgers, jK
--
--  Copyright (C) 2007-2011.
--  Licensed under the terms of the GNU GPL, v2 or later.
--
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

if (UtilsGuard) then
	return
end
UtilsGuard = true

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

UTILS_DIRNAMES = {
	LUA_DIRNAME .. 'Utilities/';
	'LuaHandler/Utilities/';
}

local EG = getfenv()

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--  returns:  basename, dirname
--

function Basename(fullpath)
	local _,_,base = fullpath:find("([^\\/:]*)$")
	local _,_,path = fullpath:find("(.*[\\/:])[^\\/:]*$")
	if (path == nil) then path = "" end
	return base, path
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Utilities

local loaded_utils = {}


local function getfilepath(filename)
	for i=1,#UTILS_DIRNAMES do
		if VFS.FileExists(UTILS_DIRNAMES[i] .. filename, VFSMODE or VFS.DEF_MODE) then
			return UTILS_DIRNAMES[i] .. filename
		end
	end
end


function require(filename, _level)
	--// check if it is in the cache
	local utilEnv = loaded_utils[filename]

	--// not in cache -> load it
	if not utilEnv then
		local filepath = getfilepath(filename)
		utilEnv = {}
		setmetatable(utilEnv, {__index = EG})
		local status, err = pcall(VFS.Include, filepath, utilEnv, VFSMODE or VFS.DEF_MODE)
		if status then
			loaded_utils[filename] = utilEnv
		else
			error(("Failed to load util \"%s\": %s."):format(filename, err), 2)
		end
	end


	--// get caller's enviroment
	_level = _level or 2
	local success, _G
	for i=_level,_level+10 do
		success, _G = pcall(getfenv, i+1) --// +1 cause of pcall!
		if success then
			break
		end
	end
	if not success then
		error(_G)
		return
	end

	--// copy to caller's enviroment
	--FIXME use recursive copy?
	for i,v in pairs(utilEnv) do
		_G[i] = v
	end
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Include

function include(filename, envTable, VFSMODE)
	if filename == "colors.h.lua" then
		return require("colors.lua") --// tail call so we don't need to bother with stacklevels
	end
	if filename == "keysym.h.lua" then
		Spring.Log(LUA_NAME, "warning", "Headers files aren't supported anymore use \"require\" instead!")
		return require("keysym.lua") --// tail call so we don't need to bother with stacklevels
	end
	if filename:find(".h.", 1, true) then
		--// give error on old LuaUI syntax (<=0.82)
		error("Headers files aren't supported anymore use \"require\" instead!", 2)
	end

	if (not filename:find("/", 1, true))or(not VFS.FileExists(filename, VFSMODE or VFS.DEF_MODE)) then
		if VFS.FileExists(LUA_DIRNAME .. filename, VFSMODE or VFS.DEF_MODE) then
			filename = LUA_DIRNAME .. filename
		elseif VFS.FileExists("LuaHandler/" .. filename, VFSMODE or VFS.DEF_MODE) then
			filename = "LuaHandler/" .. filename
		end
	end

	return VFS.Include(filename, envTable, VFSMODE or VFS.DEF_MODE)
end

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------