-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorient.lua
204 lines (139 loc) · 3.18 KB
/
orient.lua
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
function script_path()
local str = debug.getinfo(2, "S").source:sub(2)
str = str:gsub('/', '\\')
local dir = str:match("(.*\\)")
if (dir == nil) then
return ''
end
return dir
end
package.cpath = script_path()..'?.dll'..';'..package.cpath
require 'lfs'
function addPackagePath(path)
assert(path, 'no path')
print('package', path)
local luaPath = path..'.lua'
if not package.path:match(luaPath) then
package.path = package.path..';'..luaPath
end
local dllPath = path..'.dll'
if not package.path:match(dllPath) then
package.cpath = package.cpath..';'..dllPath
end
end
function requireDir(path)
assert(path, 'no path')
path = path:gsub('/', '\\')
local dir, name = path:match("(.*\\)(.*)")
if (dir ~= nil) then
local add = dir..'?\\init'
addPackagePath(add)
local add = path..'\\?'
addPackagePath(add)
end
package.loaded[name] = nil
require(name)
end
local isAbsPath = function(path)
assert(path, 'no path')
if path:find(':') then
return true
end
return false
end
io.isAbsPath = function(path)
return isAbsPath(path)
end
local function toFolderPath(path)
assert(path, 'no path')
if type(path)=='number' then
error(debug.traceback())
end
path = path:gsub('/', '\\')
if not path:match('\\$') then
path = path..'\\'
end
return path
end
function getFolder(path)
assert(path, 'no path')
local res = ""
while path:find("\\") do
res = res..path:sub(1, path:find("\\"))
path = path:sub(path:find("\\") + 1)
end
return res
end
function getFileName(path, noExtension)
assert(path, 'no path')
while path:find("\\") do
path = path:sub(path:find("\\") + 1)
end
if noExtension then
if path:lastFind('%.') then
path = path:sub(1, path:lastFind('%.') - 1)
end
end
return path
end
string.reduceFolder = function(s, amount)
if (amount == nil) then
amount = 1
end
if (amount == 0) then
return s
end
return string.reduceFolder(getFolder(s:sub(1, getFolder(s):len() - 1))..getFileName(s), amount - 1)
end
local toAbsPath = function(path, basePath)
assert(path, 'no path')
path = path:gsub('/', '\\')
if isAbsPath(path) then
return path
end
--local scriptDir = getFolder(scriptPath:gsub('/[^/]+$', ''))
if (basePath == nil) then
basePath = io.curDir()
end
local result = toFolderPath(basePath)
while (path:find('..\\') == 1) do
result = result:reduceFolder()
path = path:sub(4)
end
result = result..path
return result
end
io.toAbsPath = function(path, basePath)
return toAbsPath(path, basePath)
end
io.curDir = function()
return toFolderPath(lfs.currentdir())
end
local function getCallStack()
local t = {}
local c = 2
while debug.getinfo(c, 'S') do
local what = debug.getinfo(c, 'S').what
if ((what == 'Lua') or (what == 'main')) then
t[#t + 1] = debug.getinfo(c, 'S')
end
c = c + 1
end
return t
end
io.local_dir = function(level)
if (level == nil) then
level = 0
end
local path = getCallStack()[2 + level].source
path = path:match('^@(.*)$')
while ((path:find('.', 1, true) == 1) or (path:find('\\', 1, true) == 1)) do
path = path:sub(2)
end
path = path:gsub('/', '\\')
path = path:match('(.*\\)') or ''
if not io.isAbsPath(path) then
path = io.curDir()..path
end
return path
end