-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.lua
59 lines (53 loc) · 1.47 KB
/
utils.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
-- Copyright 2013 Arman Darini
local json = require("json")
utils = {
----------------------------------------------------------
printTable = function(t)
for k, v in pairs(t) do
if "table" == type(v) then
local s = ""
for k2, v2 in pairs(v) do
s = s .. ("table" ~= type(v2) and tostring(v2) or "{..}") .. " "
end
print(k, s)
else
print(k, v)
end
end
end,
----------------------------------------------------------
printMemoryUsed = function()
collectgarbage( "collect" )
local memUsage = string.format( "MEMORY = %.3f KB", collectgarbage( "count" ) )
print( memUsage, "TEXTURE = "..(system.getInfo("textureMemoryUsed") / (1024 * 1024) ) )
end,
----------------------------------------------------------
saveTable = function(t, filename)
local path = system.pathForFile(filename, system.DocumentsDirectory)
local file = io.open(path, "w")
if file then
local contents = json.encode(t)
file:write(contents)
io.close( file )
return true
else
return false
end
end,
----------------------------------------------------------
loadTable = function(filename)
local path = system.pathForFile( filename, system.DocumentsDirectory)
local contents = ""
local myTable = {}
local file = io.open( path, "r" )
if file then
-- read all contents of file into a string
local contents = file:read( "*a" )
myTable = json.decode(contents);
io.close( file )
return myTable
end
return nil
end,
}
return utils