forked from jk3064/new_widgethandler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.lua
More file actions
106 lines (87 loc) · 3.24 KB
/
Copy pathutils.lua
File metadata and controls
106 lines (87 loc) · 3.24 KB
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
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
-- 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_DIRNAME = LUAUI_DIRNAME .. '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 = {}
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 = UTILS_DIRNAME .. 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.Echo("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
return VFS.Include(LUAUI_DIRNAME .. filename, envTable, VFSMODE or VFS.DEF_MODE)
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------