-
Notifications
You must be signed in to change notification settings - Fork 3
/
framework.lua
305 lines (276 loc) · 7.59 KB
/
framework.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
--
-- mexico string extensions
--
string.mexico = {
--
-- Function: string.split (extension function)
--
-- The split function is used to split a string into an array of substrings.
--
-- Parameters:
-- + separator [string] : Specifies the character (or character sequence) to use for splitting the string.
-- + maxSplit [number] : (optional) The maximum number of splits.
-- + isRegEx [bool] : (optional) Specifies if 'parameter' is a regular expression.
--
-- Returns:
-- Returns the new array.
--
split = function (str, separator, maxSplit, isRegEx)
assert(separator and separator ~= '', "The 'separator' parameter SHOULD NOT be null or empty.")
assert(maxSplit == nil or maxSplit >= 1, "The 'maxSplit' parameter MUST be greater or equal 1.")
local result = {}
if string.len(str) > 0 then
local asPlain = not isRegEx
maxSplit = maxSplit or -1
local pos=1 start=1
local first,last = string.find(str, separator, start, asPlain)
while first and maxSplit ~= 0 do
result[pos] = string.sub(str, start, first-1)
pos = pos+1
start = last+1
first,last = string.find(str, separator, start, asPlain)
maxSplit = maxSplit - 1
end
result[pos] = string.sub(str, start)
end
return result
end,
}
-- shortcuts
string.split = string.mexico.split
--
-- mexico table extensions
--
table.mexico = {
--
--
--
indexOf = function(tbl, value)
if not tbl then return false end
for i,v in ipairs(tbl) do
if v == value then return i end
end
return false
end,
--
--
--
removeValue = function(tbl, value)
if not self then return false end
local i = table.mexico.indexOf(tbl, value)
if (i > 0) then
table.remove(tbl, i)
return true
end
return false
end,
--
--
--
clone = function(tbl, deep)
if not tbl then return nil end
local new = {}
local i, v = next(tbl, nil)
while i do
if deep and (type(v) == "table") then
v = table.mexico.clone(v, deep)
end
new[i] = v
i, v = next(tbl, i)
end
return new
end
}
-- shortcuts
table.indexOf = table.mexico.indexOf
table.removeValue = table.mexico.removeValue
table.clone = table.mexico.clone
--
-- init mexico
--
local mexico = {
--
-- Function: fargs
--
-- Returns variable arguments function as <array>, <num>.
--
fargs = function(...)
local args = {...}
local nargs = #args
return args, nargs
end,
--
-- Function: namespace
--
namespace = function(name)
name = name .. "_mx_nsLoader"
if package.loaded[name] then
return package.loaded[name]
end
local ns_mt = {
__index = function(table, key)
local mod_name = name .. "." .. key
local value = require(mod_name)
table[key] = value
return value
end,
}
local ns = {}
setmetatable(ns, ns_mt)
table["ui"] = ns
package.loaded[name] = ns
return ns
end,
--
--
-- Function: class
--
-- Creates a lightweight lua class.
--
-- Parameters:
-- + base [table] : (optional) The base class to inherit from (if not specified, Object will be used).
--
-- Original Version: http://lua-users.org/wiki/SimpleLuaClasses
class = function(base, ...)
local c = {}
-- no base, use Object
if not base then base = package.loaded["Object"] end
-- check if we have to inherit from a base class
if type(base) == 'table' then
-- our new class is a shallow copy of the base class!
for k,v in pairs(base) do
c[k] = v
end
c._base = base -- base class reference
c.init = base.init -- use base init if class does not have one
c.new = base.new -- use base new if class does not have one
end
-- check if we have some mixin objects
c._mixins = {...}
for i,m in ipairs(c._mixins) do
for k,v in pairs(m) do
-- never overload existing functions and never attach init functions
if (type(v) == "function") and (not c[k]) and (k ~= "init") then
c[k] = v
end
end
end
-- the class will be the metatable for all its objects,
-- and they will look up their methods in it.
c.__index = c
c.__tostring = function(c_tbl, ...) if c_tbl then return c_tbl:toString(...) else return nil end end
-- expose a constructor which can be called by <classname>(<args>)
local ctor_metatable = {
__call = function(c_tbl, ...)
local obj
if type(c.new) == "function" then
obj = c.new(...)
assert(obj, "new(...) did not returned a value")
-- setmetatable does not work here so copy all functions
for k,v in pairs(c) do
if type(v) == "function" then -- copy only functions
if obj[k] then -- function already exist
obj["original_" .. k] = obj[k] -- rename original function to "original_<func_name>"
end
obj[k] = v -- attach class function to object instance
end
end
else
obj = {} -- true object creation
setmetatable(obj, c) -- just set class metatable to object -> done
end
if c.init then c.init(obj, ...) end -- call object constructor (or better initializer)
return obj -- return the new object
end,
}
-- set class metatable
setmetatable(c, ctor_metatable)
return c -- return class
end,
-- run method
run = function(filename)
local app require(filename or "app")
package.loaded["mexico"]["app"] = app
return app
end,
}
local mexico_mt = {
--
-- Dynamic load a mexico module
--
__index = function(table, key)
if (key == "ui") then -- ui namespace
local ui_mt = {
__index = function(table, key)
local mod_name = "mexico.ui." .. key
local value = require(mod_name)
table[key] = value
return value
end,
}
local ui = {}
setmetatable(ui, ui_mt)
table["ui"] = ui
return ui
else
if (key == "gc") then -- garbage collector singleton
key = "GarbageCollector"
end
local mod_name = "mexico." .. key
local value = require(mod_name)
table[key] = value
return value
end
end,
}
setmetatable(mexico, mexico_mt)
--
-- Class: Object
--
-- Base class for all objects.
--
local Object = mexico.class(false)
--
-- Constructor
--
function Object:init()
Object.assert(self)
end
--
-- Assert if object is not nil and not already disposed.
--
function Object:assert()
assert(self, "Object is nil.")
assert(not self.disposed, "Object is disposed.")
end
--
-- Function: dispose
--
-- Releases all resources used by the object.
--
-- Remarks:
-- Call this function if you no longer need the object. The garbage collector will thank you!
--
function Object:dispose()
self["disposed"] = true -- mark object as disposed
end
--
-- Function: toString
--
-- Returns a string that represents the current object.
--
-- Returns:
-- The default implementation will return the full type name of the object (like: Object).
--
-- Remarks:
-- Always override this function and not __tostring, since __tostring is already mapped to this function!
--
function Object:toString()
return "Object"
end
-- register mexico shortcut
package.loaded["mexico"] = mexico
-- register Object
package.loaded["mexico.Object"] = Object
-- return package content
return mexico