-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.lua2p
131 lines (114 loc) · 2.91 KB
/
utils.lua2p
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
local Bitser = require("modules.bitser.bitser")
local Log = require("modules.log.log")
local Utils = {
file = {},
serial = {},
hash = {},
string = {},
table = {},
math = {},
}
function Utils.file.read(filename)
@@assert(type(filename) == "string")
local file = love.filesystem.getInfo(filename)
Log.info(filename, "exists:", file ~= nil)
if file then
local content = love.filesystem.read(filename)
return content, true
end
return false
end
function Utils.file.write(filename, data)
@@assert(type(filename) == "string")
love.filesystem.write(filename, data)
Log.info(filename, "written")
end
function Utils.serial.write(filename, data)
@@assert(type(filename) == "string")
local to_write = Bitser.dumps(data)
Utils.file.write(filename, to_write)
return to_write
end
function Utils.serial.read(filename)
@@assert(type(filename) == "string")
local content, exists = Utils.file.read(filename)
if exists then
local data = Bitser.loads(content)
return data, exists
end
return false
end
function Utils.serial.de(content)
return Bitser.loads(content)
end
function Utils.hash.compare(a, b)
@@assert(type(a) == "string")
@@assert(type(b) == "string")
local hashed_a = love.data.hash("md5", a)
local hashed_b = love.data.hash("md5", b)
return hashed_a == hashed_b
end
function Utils.string.get_substr(str, sep)
@@assert(type(str) == "string")
@@assert(type(sep) == "string")
local n = string.find(str, sep)
return string.sub(str, 1, n - 1)
end
function Utils.string.get_widest(t)
@@assert(type(t) == "table")
@@assert(#t ~= 0)
local widest = 0
for i, str in ipairs(t) do
widest = math.max(widest, #str)
end
return widest
end
function Utils.table.rotate(t, amount)
@@assert(type(t) == "table")
@@assert(type(amount) == "number")
if amount == 0 then return t end
if amount > 0 then
tablex.push(t, tablex.shift(t))
return Utils.table.rotate(t, amount - 1)
elseif amount < 0 then
tablex.unshift(t, tablex.pop(t))
return Utils.table.rotate(t, amount + 1)
end
end
function Utils.table.count_kv(t)
@@assert(type(t) == "table")
if #t ~= 0 then return #t end
local count = 0
for _ in pairs(t) do
count = count + 1
end
return count
end
function Utils.table.pick_random_kv(t, count)
count = count or Utils.table.count_kv(t)
local limit = math.floor(love.math.random() * (count - 1)) + 1
for k, v in pairs(t) do
limit = limit - 1
if limit == 0 then
return k, v
end
end
end
function Utils.math.n_digits(x)
return math.floor(math.log10(x)) + 1
end
function Utils.math.lerp_range(range, t)
@@assert(type(range) == "table" and range.min and range.max)
@@assert(type(t) == "number")
return mathx.lerp(range.min, range.max, t)
end
function Utils.math.calc_e_controller_origin(e)
@@assert(e.__isEntity)
local anim_data = e.animation_data
local transform = e.transform
local pos = e.pos
local x = pos.x + anim_data.frame_width * 0.5 - transform.ox
local y = pos.y
return x, y
end
return Utils