-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtechno.lua
More file actions
272 lines (219 loc) · 5.86 KB
/
techno.lua
File metadata and controls
272 lines (219 loc) · 5.86 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
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
-- Library of my custom tweaks
function Trand(tb)
math.randomseed( os.time() )
return tb[math.random(#tb)]
end
function Sleep(n) -- seconds
local clock = os.clock
local t0 = clock()
while clock() - t0 <= n do end
end
function FileExists(name)
if type(name)~="string" then return false end
return os.rename(name,name) and true or false
end
function IsFile(name)
if type(name)~="string" then return false end
if not FileExists(name) then return false end
local f = io.open(name)
if f then
f:close()
return true
end
return false
end
function IsDir(name)
return (FileExists(name) and not IsFile(name))
end
function table.len(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
function table.has_value(tab, val)
for _, value in ipairs(tab) do if value == val then return true end end
return false
end
function table.has_key(tab, val)
for key, _ in pairs(tab) do if key == val then return true end end
return false
end
function table.contains(table, element)
for _, value in pairs(table) do
if value == element then
return true
end
end
return false
end
function table.val_to_str ( v )
if "string" == type( v ) then
v = string.gsub( v, "\n", "\\n" )
if string.match( string.gsub(v,"[^'\"]",""), '^"+$' ) then
return "'" .. v .. "'"
end
return '"' .. string.gsub(v,'"', '\\"' ) .. '"'
else
return "table" == type( v ) and table.toStr( v ) or
tostring( v )
end
end
function table.key_to_str ( k )
if "string" == type( k ) and string.match( k, "^[_%a][_%a%d]*$" ) then
return k
else
return "[" .. table.val_to_str( k ) .. "]"
end
end
function table.toStr( tbl )
local result, done = {}, {}
for k, v in ipairs( tbl ) do
table.insert( result, table.val_to_str( v ) )
done[ k ] = true
end
for k, v in pairs( tbl ) do
if not done[ k ] then
table.insert( result,
table.key_to_str( k ) .. "=" .. table.val_to_str( v ) )
end
end
return "{" .. table.concat( result, "," ) .. "}"
end
function table.slice(tbl, first, last, step)
local sliced = {}
for i = first or 1, last or #tbl, step or 1 do
sliced[#sliced+1] = tbl[i]
end
return sliced
end
local meta = getmetatable("") -- get the string metatable
meta.__add = function(a,b) -- the + operator
return a..b
end
meta.__sub = function(a,b) -- the - operator
return a:gsub(b,"")
end
meta.__mul = function(a,b) -- the * operator
return a:rep(b)
end
-- if you have string.explode (check out the String exploding snippet) you can also add this:
meta.__div = function(a,b) -- the / operator
return a:SplitStr(b)
end
meta.__index = function(a,b) -- if you attempt to do string[id]
if type(b) ~= "number" then
return string[b]
end
return a:sub(b,b)
end
function string.contains(s, v)
if s:find(v, 1, true) then return true else return false end
end
function string.starts_with(str, start)
return str:sub(1, #start) == start
end
function string.ends_with(str, ending)
return ending == "" or str:sub(-#ending) == ending
end
function string.capfirst(inStr)
local out = inStr:gsub("^%l",string.upper)
return out
end
function string.split(s, delimiter)
local result = {};
for match in (s..delimiter):gmatch("(.-)"..delimiter) do
table.insert(result, match);
end
return result;
end
function string.strip(str)
return str:gsub("^%s*(.-)%s*$", "%1")
end
function string.getind(s, ind) return s:sub(ind,ind) end
function string.repind(s, ind, news)
local l = s:sub(1, ind-1)
local r = s:sub(ind+1, #s)
return l..news..r
end
function string.insert(s, ind, news)
local l = s:sub(1, ind)
local r = s:sub(ind+1, #s)
return l..news..r
end
function string.getFileExt( path )
return path:match( "%.([^%.]+)$" )
end
function string.stripFileExt( path )
local i = path:match( ".+()%.%w+$" )
if ( i ) then return path:sub( 1, i - 1 ) end
return path
end
function string.parseFilePath( path )
return path:match( "^(.*[/\\])[^/\\]-$" ) or ""
end
function string.parseFileName( path )
if not (path:find( "\\" ) and path:find( "/" ) ) then return path end
return path:match( "[\\/]([^/\\]+)$" ) or ""
end
function string.Plural( str, quantity )
return str .. ( ( quantity ~= 1 ) and "s" or "" )
end
function string.Left( str, num ) return string.sub( str, 1, num ) end
function string.Right( str, num ) return string.sub( str, -num ) end
function string.Replace( str, tofind, toreplace )
local tbl = string.split( tofind, str )
if ( tbl[ 1 ] ) then return table.concat( tbl, toreplace ) end
return str
end
-- Note: These use Lua index numbering, not what you'd expect
-- ie they start from 1, not 0.
function string.SetChar( s, k, v )
local start = s:sub( 0, k - 1 )
local send = s:sub( k + 1 )
return start .. v .. send
end
function string.GetChar( s, k )
return s:sub( k, k )
end
function math.Rand( low, high )
return low + ( high - low ) * math.random()
end
function math.round(float)
return float % 1 >= 0.5 and math.ceil(float) or math.floor(float)
end
function ShallowCopy(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in pairs(orig) do
copy[orig_key] = orig_value
end
else -- number, string, boolean, etc
copy = orig
end
return copy
end
function DeepCopy(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in next, orig, nil do
copy[DeepCopy(orig_key)] = DeepCopy(orig_value)
end
setmetatable(copy, DeepCopy(getmetatable(orig)))
else -- number, string, boolean, etc
copy = orig
end
return copy
end
function MergeObj(first, second)
for k, v in pairs(second) do
first[k] = v
end
return first
end
function Implement(orig)
return DeepCopy(orig)
end