forked from FAForever/fa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.lua
More file actions
770 lines (703 loc) · 24.7 KB
/
utils.lua
File metadata and controls
770 lines (703 loc) · 24.7 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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
-- ==========================================================================================
-- * File : lua/system/utils.lua
-- * Authors : Gas Powered Games, FAF Community, HUSSAR
-- * Summary : Contains global functions for working with tables and strings
-- ==========================================================================================
--- RandomIter(table) returns a function that when called, returns a pseudo-random element of the supplied table.
--- Each element of the table will be returned once. This is essentially for "shuffling" sets.
function RandomIter(someSet)
local keyList = {}
for key, val in someSet do
table.insert(keyList, key)
end
return function()
local size = table.getn(keyList)
if size > 0 then
local key = table.remove(keyList, Random(1, size))
return key, someSet[key]
else
return
end
end
end
--- safecall(msg, fn, ...) calls the given function with the given args, and
--- catches any error and logs a warning including the given message.
--- Returns nil if the function failed, otherwise returns the function's result.
function safecall(msg, fn, ...)
local ok, result = pcall(fn, unpack(arg))
if ok then
return result
else
WARN("Problem " .. tostring(msg) .. ":\n" .. result)
return
end
end
--- table.copy(t) returns a shallow copy of t.
function table.copy(t)
if not t then return end -- prevents looping over nil table
local r = {}
for k,v in t do
r[k] = v
end
return r
end
--- table.contains(t,val) returns the key for val if it is in t table.
--- Otherwise, return nil
function table.find(t,val)
if not t then return end -- prevents looping over nil table
for k,v in t do
if v == val then
return k
end
end
-- return nil by falling off the end
end
--- table.subset(t1,t2) returns true iff every key/value pair in t1 is also in t2
function table.subset(t1,t2)
if not t1 and not t2 then return true end -- nothing is in nothing
if not t1 then return true end -- nothing is in something
if not t2 then return false end -- something is not in nothing
for k,v in t1 do
if t2[k] ~= v then return false end
end
return true
end
--- table.equal(t1,t2) returns true iff t1 and t2 contain the same key/value pairs.
function table.equal(t1,t2)
return table.subset(t1,t2) and table.subset(t2,t1)
end
--- table.removeByValue(t,val) remove a field by value instead of by index
function table.removeByValue(t,val)
if not t then return end -- prevent looping over nil table
for k,v in t do
if v == val then
table.remove(t,k)
return
end
end
end
--- table.deepcopy(t) returns a copy of t with all sub-tables also copied.
function table.deepcopy(t,backrefs)
if type(t)=='table' then
if backrefs==nil then backrefs = {} end
local b = backrefs[t]
if b then
return b
end
local r = {}
backrefs[t] = r
for k,v in t do
r[k] = table.deepcopy(v,backrefs)
end
return r
else
return t
end
end
--- table.merged(t1,t2) returns a table in which fields from t2 overwrite
--- fields from t1. Neither t1 nor t2 is modified. The returned table may
--- share structure with either t1 or t2, so it is not safe to modify.
-- e.g. t1 = { x=1, y=2, sub1={z=3}, sub2={w=4} }
-- t2 = { y=5, sub1={a=6}, sub2="Fred" }
-- merged(t1,t2) -> { x=1, y=5, sub1={a=6,z=3}, sub2="Fred" }
-- merged(t2,t1) -> { x=1, y=2, sub1={a=6,z=3}, sub2={w=4} }
function table.merged(t1, t2)
if t1==t2 then
return t1
end
if type(t1)~='table' or type(t2)~='table' then
return t2
end
local copied = nil
for k,v in t2 do
if type(v)=='table' then
v = table.merged(t1[k], v)
end
if t1[k] ~= v then
copied = copied or table.copy(t1)
t1 = copied
t1[k] = v
end
end
return t1
end
--- Write all undefined keys from t2 into t1.
function table.assimilate(t1, t2)
if not t2 then return t1 end -- prevent looping over nil table
for k, v in t2 do
if t1[k] == nil then
t1[k] = v
end
end
return t1
end
--- Remove all keys in t2 from t1.
function table.subtract(t1, t2)
if not t2 then return t1 end -- prevent looping over nil table
for k, v in t2 do
t1[k] = nil
end
return t1
end
--- table.cat(t1, t2) performs a shallow "merge" of t1 and t2, where t1 and t2
--- are expected to be numerically keyed (existing keys are discarded).
--- e.g. table.cat({1, 2, 3}, {'A', 'House', 3.14}) -> {1, 2, 3, 'A', 'House', 3.14}
function table.cat(t1, t2)
-- handling nil tables before lopping
if not t1 then return table.copy(t2) end
if not t2 then return table.copy(t1) end
local r = {}
for i,v in t1 do
table.insert(r, v)
end
for i,v in t2 do
table.insert(r, v)
end
return r
end
--- Concatenate arbitrarily-many tables (equivalent to table.cat, but varargs.
--- Slightly more overhead, but can constructively concat *all* the things)
function table.concatenate(...)
local ret = {}
for index = 1, table.getn(arg) do
if arg[index] then
for k, v in arg[index] do
table.insert(ret, v)
end
end
end
return ret
end
--- Destructively concatenate two tables. (numerical keys only)
--- Appends the keys of t2 onto t1, returning it. The original t1 is destroyed,
--- but this avoids the need to copy the values in t1, saving some time.
function table.destructiveCat(t1, t2)
for k, v in t2 do
table.insert(t1, v)
end
end
--- table.sorted(t, [comp]) is the same as table.sort(t, comp) except it returns
--- a sorted copy of t, leaving the original unchanged.
--- [comp] is an optional comparison function, defaulting to less-than.
function table.sorted(t, comp)
local r = table.copy(t)
table.sort(r, comp)
return r
end
--- sort_by(field) provides a handy comparison function for sorting
--- a list of tables by some field.
---
--- For example,
--- my_list={ {name="Fred", ...}, {name="Wilma", ...}, {name="Betty", ...} ... }
---
--- table.sort(my_list, sort_by 'name')
--- to get names in increasing order
---
--- table.sort(my_list, sort_down_by 'name')
--- to get names in decreasing order
function sort_by(field)
return function(t1,t2)
return t1[field] < t2[field]
end
end
function sort_down_by(field)
return function(t1,t2)
return t2[field] < t1[field]
end
end
--- table.keys(t, [comp]) returns a list of the keys of t, sorted.
--- [comp] is an optional comparison function, defaulting to less-than, e.g.
-- table.keys(t) -- returns keys in increasing order (low performance with large tables)
-- table.keys(t, function(a, b) return a > b end) -- returns keys in decreasing order (low performance with large tables)
-- table.keys(t, false) -- returns keys without comparing/sorting (better performance with large tables)
function table.keys(t, comp)
local r = {}
if not t then return r end -- prevents looping over nil table
local n = 1
for k,v in t do
r[n] = k -- faster than table.insert(r,k)
n = n + 1
end
if comp ~= false then table.sort(r, comp) end
return r
end
--- table.values(t) Return a list of the values of t, in unspecified order.
function table.values(t)
local r = {}
if not t then return r end -- prevents looping over nil table
local n = 1
for k,v in t do
r[n] = v -- faster than table.insert(r,v)
n = n + 1
end
return r
end
--- Concatenate keys of a table into a string and separates them by optional string.
function table.concatkeys(t, sep)
sep = sep or ", "
local tt = table.keys(t)
return table.concat(tt,sep)
end
--- Iterates over a table in key-sorted order:
--- for k,v in sortedpairs(t) do
--- print(k,v)
--- end
--- @param comp is an optional comparison function, defaulting to less-than.
function sortedpairs(t, comp)
local keys = table.keys(t, comp)
local i=1
return function()
local k = keys[i]
if k~=nil then
i=i+1
return k,t[k]
end
end
end
--- Returns actual size of a table, including string keys
function table.getsize(t)
-- handling nil table like empty tables so that no need to check
-- for nil table and then size of table:
-- if t and table.getsize(t) > 0 then
-- do some thing
-- end
if type(t) ~= 'table' then return 0 end
local size = 0
for k, v in t do
size = size + 1
end
return size
end
--- Returns a table with keys and values from t reversed.
--- e.g. table.inverse {'one','two','three'} => {one=1, two=2, three=3}
--- table.inverse {foo=17, bar=100} => {[17]=foo, [100]=bar}
--- If t contains duplicate values, it is unspecified which one will be returned.
--- e.g. table.inverse {foo='x', bar='x'} => possibly {x='bar'} or {x='foo'}
function table.inverse(t)
if not t then return {} end -- prevents looping over nil table
local r = {}
for k,v in t do
r[v] = k
end
return r
end
--- Reverses order of values in a table using their index
--- table.reverse {'one','two','three'} => {'three', 'two', 'one'}
function table.reverse(t)
if not t then return {} end -- prevents looping over nil table
local r = {}
local items = table.indexize(t) -- convert from hash table
local itemsCount = table.getsize(t)
for k, v in ipairs(items) do
r[itemsCount + 1 - k] = v
end
return r
end
--- Converts hash table to a new table with keys from 1 to size of table and the same values
--- it is useful for preparing hash table before sorting its values
--- table.indexize { [a] = 'one', [b] = 'two', [c] = 'three' } =>
--- { [1] = 'one', [2] = 'two', [3] = 'three' }
function table.indexize(t)
if not t then return {} end -- prevents looping over nil table
local r = {}
local n = 1
for k, v in t do
r[n] = v -- faster than table.insert(r, v)
n = n + 1
end
return r
end
--- Converts a table to a new table with values as keys and values equal to true, duplicated table values are discarded
--- it is useful for quickly looking up values in tables instead of looping over them
--- table.hash { [1] = 'A', [2] = 'B', [3] = 'C', [4] = 'C' } =>
--- { [A] = true, [B] = true, [C] = true }
function table.hash(t)
if not t then return {} end -- prevents looping over nil table
local r = {}
for k, v in t do
if type(v) ~= "string" and type(v) ~= 'number' then
r[tostring(v)] = true
else
r[v] = true
end
end
return r
end
--- Converts a table to a new table with values as keys only if their values are true
--- it is reverse logic of table.hash(t)
--- table.unhash { [A] = true, [B] = true, [C] = false } =>
-- { [1] = 'A', [2] = 'B', }
function table.unhash(t)
if not t then return {} end -- prevents looping over nil table
local r = {}
local n = 1
for k, v in t do
if v then
r[n] = k -- faster than table.insert(r, k)
n = n + 1
end
end
return r
end
--- Gets keys of hash table if their values equal to specified boolean value, defaults to true
--- it is useful to check which keys are present or not in a hash table
--- t = { [A] = true, [B] = true, [C] = false }
--- table.hashkeys(t, true) => { 'A', 'B' }
--- table.hashkeys(t, false) => { 'C' }
function table.hashkeys(t, value)
if value == nil then value = true end -- defaulting to true
local r = table.filter(t, function(v) return v == value end)
return table.keys(r)
end
--- table.map(fn,t) returns a table with the same keys as t but with
--- fn function applied to each value.
function table.map(fn, t)
if not t then return {} end -- prevents looping over nil table
local r = {}
for k,v in t do
r[k] = fn(v)
end
return r
end
--- table.empty(t) returns true iff t has no keys/values.
function table.empty(t)
return table.getsize(t) == 0
end
--- table.shuffle(t) returns a shuffled table
function table.shuffle(t)
local r = {}
for key, val in RandomIter(t) do
if type(key) == 'number' then
table.insert(r, val)
else
r[key] = val
end
end
return r
end
-- table.binsert(t, value, cmp) binary insert value into table using cmp-func
function table.binsert(t, value, cmp)
local cmp = cmp or (function(a,b) return a < b end)
local start, stop, mid, state = 1, table.getsize(t), 1, 0
while start <= stop do
mid = math.floor((start + stop) / 2)
if cmp(value, t[mid]) then
stop, state = mid - 1, 0
else
start, state = mid + 1, 1
end
end
table.insert(t, mid + state, value)
return mid + state
end
-- Pretty-print a table. Depressingly large amount of wheel-reinvention were required, thanks to
-- SC's LUA being a bit weird and the existing solutions to this problem being aggressively optimized
function printField(k, v, tblName, printer)
if not printer then printer = WARN end
if not tblName then tblName = "" end
if "table" == type(k) then
table.print(k, tblName .. " ", printer)
else
tblName = tblName .. '' .. tostring(k)
end
if "string" == type(v) then
printer(tblName .. " = " .. "\"" .. v .. "\"")
elseif "table" == type(v) then
--printer(tblName .. k .. " = ")
table.print(v, tblName .. " ", printer)
else
printer(tblName .. " = " .. tostring(v))
end
end
--- Prints keys and values of a table and sub-tables if present
--- @param tbl specifies a table to print
--- @param tblPrefix specifies optional table prefix/name
--- @param printer specifies optional message printer: LOG, WARN, error, etc.
--- e.g. table.print(categories)
--- table.print(categories, 'categories')
--- table.print(categories, 'categories', 'WARN')
function table.print(tbl, tblPrefix, printer)
if not printer then printer = LOG end
if not tblPrefix then tblPrefix = "" end
if not tbl then
printer(tblPrefix .." table is nil")
return
end
if table.getsize(tbl) == 0 then
printer(tblPrefix .." { }")
return
end
printer(tblPrefix.." {")
for k, v in pairs(tbl) do
printField(k, v, tblPrefix .. " ", printer)
end
printer(tblPrefix.." }")
end
--- Return filtered table containing every mapping from table for which fn function returns true when passed the value.
--- @param t - is a table to filter
--- @param fn - is decision function to use to filter the table, defaults checking if a value is true or exists in table
function table.filter(t, fn)
local r = {}
if not fn then fn = function(v) return v end end
for k, v in t do
if fn(v) then
r[k] = v
end
end
return r
end
--- Returns total count of values that match fn function or if values exist in table
--- @param fn is optional filtering function that is applied to each value of the table
function table.count(t, fn)
if not t then return 0 end -- prevents looping over nil table
if not fn then fn = function(v) return v end end
local r = table.filter(t, fn)
return table.getsize(r)
end
--- Returns a new table with unique values stored using numeric keys and it does not preserve keys of the original table
function table.unique(t)
if not t then return end -- prevents looping over nil table
local unique = {}
local ins = {}
local n = 0
for k, v in t do
if not ins[v] then
n = n + 1
unique[n] = v -- faster than table.insert(unique, v)
ins[v] = true
end
end
return unique
end
--- Returns items as a single string, separated by the delimiter
function StringJoin(items, delimiter)
local str = "";
for k,v in items do
str = str .. v .. delimiter
end
return str
end
--- "explode" a string into a series of tokens, using a separator character `sep`
function StringSplit(str, sep)
local sep, fields = sep or ":", {}
local pattern = string.format("([^%s]+)", sep)
str:gsub(pattern, function(c) fields[table.getn(fields)+1] = c end)
return fields
end
--- Returns true if the string starts with the specified value
function StringStartsWith(stringToMatch, valueToSeek)
return string.sub(stringToMatch, 1, valueToSeek:len()) == valueToSeek
end
--- Extracts a string between two specified strings
--- e.g. StringExtract('/path/name_end.lua', '/', '_end', true) --> name
function StringExtract(str, str1, str2, fromEnd)
local pattern = str1 .. '(.*)' .. str2
if fromEnd then pattern = '.*' .. pattern end
local i, ii, m = string.find(str, pattern)
return m
end
--- Adds comma as thousands separator in specified value
--- e.g. StringComma(10000) --> 10,000
function StringComma(value)
local str = value or 0
while true do
str, k = string.gsub(str, "^(-?%d+)(%d%d%d)", '%1,%2')
if k == 0 then
break
end
end
return str
end
--- Prepends a string with specified symbol or one space
function StringPrepend(str, symbol)
if not symbol then symbol = ' ' end
return symbol .. str
end
--- Splits a string with camel cast to a string with separate words
--- e.g. StringSplitCamel('SupportCommanderUnit') -> 'Support Commander Unit'
function StringSplitCamel(str)
return (str:gsub("[A-Z]", StringPrepend):gsub("^.", string.upper))
end
--- Reverses order of letters for specified string
--- e.g. StringCapitalize('abc123') --> 321cba
function StringReverse(str)
local tbl = {}
str:gsub(".",function(c) table.insert(tbl,c) end)
tbl = table.reverse(tbl)
return table.concat(tbl)
end
--- Capitalizes each word in specified string
--- e.g. StringCapitalize('hello supreme commander') --> Hello Supreme Commander
function StringCapitalize(str)
return string.gsub(" "..str, "%W%l", string.upper):sub(2)
end
--- Check if a given string starts with specified string
function StringStarts(str, startString)
return string.sub(str, 1, string.len(startString)) == startString
end
--- Check if a given string ends with specified string
function StringEnds(str, endString)
return endString == '' or string.sub(str, -string.len(endString)) == endString
end
--- Sorts two variables based on their numeric value or alpha order (strings)
function Sort(itemA, itemB)
if not itemA or not itemB then return 0 end
if type(itemA) == "string" or
type(itemB) == "string" then
if string.lower(itemA) == string.lower(itemB) then
return 0
else
-- sort string using alpha order
return string.lower(itemA) < string.lower(itemB)
end
else
if math.abs(itemA - itemB) < 0.0001 then
return 0
else
-- sort numbers in decreasing order
return itemA > itemB
end
end
end
-- Rounds a number to specified double precision
function math.round(num, idp)
if not idp then
return math.floor(num+.5)
else
return tonumber(string.format("%." .. (idp or 0) .. "f", num))
end
end
--- Clamps numeric value to specified Min and Max range
function math.clamp(v, min, max)
return math.max(min, math.min(max, v))
end
--- Creates timer for profiling task(s) and calculating time delta between consecutive function calls, e.g.
--- local timer = CreateTimer()
--- timer:Start() -- then execute some LUA code
--- timer:Stop()
--- or
--- timer:Start('task1') -- then execute task #1
--- timer:Stop('task1')
--- timer:Start('task2') -- then execute task #2
--- timer:Stop('task2')
function CreateTimer()
return {
tasks = {},
Reset = function(self)
self.tasks = {}
end,
-- starts profiling timer for optional task name
Start = function(self, name, useLogging)
name = self:Verify(name)
-- capture start time
self.tasks[name].stop = nil
self.tasks[name].start = CurrentTime()
self.tasks[name].calls = self.tasks[name].calls + 1
if useLogging then
LOG('Timing task: ' .. name .. ' started')
end
end,
-- stops profiling timer and calculates stats for optional task name
Stop = function(self, name, useLogging)
name = self:Verify(name)
-- capture stop time
self.tasks[name].stop = CurrentTime()
self.tasks[name].time = self.tasks[name].stop - self.tasks[name].start
self.tasks[name].total = self.tasks[name].total + self.tasks[name].time
-- track improvements between consecutive profiling of the same task
if self.tasks[name].last then
self.tasks[name].delta = self.tasks[name].last - self.tasks[name].time
end
-- save current time for comparing with the next task profiling
self.tasks[name].last = self.tasks[name].time
if useLogging then
LOG('Timing task: ' .. name ..' completed in ' .. self:ToString(name))
end
return self:ToString(name)
end,
-- verifies if profiling timer has stats for optional task name
Verify = function(self, name)
if not name then name = 'default-task' end
if not self.tasks[name] then
self.tasks[name] = {}
self.tasks[name].name = name
self.tasks[name].start = nil
self.tasks[name].stop = nil
self.tasks[name].delta = nil
self.tasks[name].last = nil
self.tasks[name].calls = 0
self.tasks[name].total = 0
self.tasks[name].time = 0
end
return name
end,
-- gets stats for optional task name
GetStats = function(self, name)
name = self:Verify(name)
return self.tasks[name]
end,
-- gets time for optional task name
GetTime = function(self, name)
name = self:Verify(name)
local ret = ''
if not self.tasks[name].start then
WARN('Timer cannot get time duration for not started task: ' .. tostring(name))
elseif not self.tasks[name].stop then
WARN('Timer cannot get time duration for not stopped task: ' .. tostring(name))
else
ret = string.format("%0.3f seconds", self.tasks[name].time)
end
return ret
end,
-- gets time delta between latest and previous profiling of named tasks
GetDelta = function(self, name)
name = self:Verify(name)
local ret = ''
if not self.tasks[name].delta then
WARN('Timer cannot get time delta after just one profiling of task: ' .. tostring(name))
else
ret = string.format("%0.3f seconds", self.tasks[name].delta)
if self.tasks[name].delta > 0 then
ret = '+' .. ret
end
end
return ret
end,
-- gets time total of all profiling calls of named tasks
GetTotal = function(self, name)
name = self:Verify(name)
local ret = ''
if not self.tasks[name].start then
WARN('Timer cannot get time total for not started task: ' .. tostring(name))
else
ret = string.format("%0.3f seconds", self.tasks[name].total)
end
return ret
end,
-- converts profiling stats for optional named task to string
ToString = function(self, name)
name = self:Verify(name)
local ret = self:GetTime(name)
if self.tasks[name].delta then
ret = ret .. ', delta: ' .. self:GetDelta(name)
end
if self.tasks[name].calls > 1 then
ret = ret .. ', calls: ' .. tostring(self.tasks[name].calls)
ret = ret .. ', total: ' .. self:GetTotal(name)
end
return ret
end,
-- prints profiling stats of all tasks in increasing order of tasks
-- @param key is optional sorting argument of tasks, e.g. 'stop', 'time', 'start'
Print = function(self, key)
key = key or 'stop'
local sorted = table.indexize(self.tasks)
sorted = table.sorted(sorted, sort_by(key))
for _, task in sorted do
if task.stop then
LOG('Timing task: ' .. task.name ..' completed in ' .. self:ToString(task.name))
end
end
end
}
end