-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemoryaccess.lua
More file actions
243 lines (229 loc) · 7.4 KB
/
Copy pathmemoryaccess.lua
File metadata and controls
243 lines (229 loc) · 7.4 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
SHARED_MEMORY = {}
SHARED_MEMORY.AVAILABLE_TYPES =
{
NUMBER = 1,
ARRAY = 2,
STRING = 3,
MAP = 4
}
--Number used to divide and multiply the value for converting it between character and number
local maxByteSize = 512
--Used only inside threads, it will be overridden inside sharedmemory
--It will store SoundData's
local registeredVars = {}
--This function is defined once per thread
__MEMORY_ACCESS_ON_UPDATE = function (varUpdated)
-- update(registeredVars[varUpdated])
end
local mId = nil
local function checkRegistered(varName)
if(not registeredVars[varName]) then
error("SHARED_MEMORY(MEMORY_ACCESS): "..varName.." is not registered")
end
return registeredVars[varName]
end
local function clampDecimal(val, numDecimalPlaces)
local mult = 10^numDecimalPlaces
return math.floor(val*mult + 0.5)/mult
end
local function round(num)
if(num > 0) then return math.floor(num+.5)
else return math.ceil(num-.5)end
end
--Receives the shared variable and set its value, storing in its sample
--Globally available for setting its type inside sharedmemory
function SHARED_MEMORY.setValueAtPos(sharedVar, pos, value)
local mByte = clampDecimal(value / maxByteSize, 7)
sharedVar:setSample(pos, mByte)
end
function SHARED_MEMORY.getValueAtPos(sharedVar, pos)
return round(sharedVar:getSample(pos) * maxByteSize)
end
local setValueAtPos = SHARED_MEMORY.setValueAtPos
local getValueAtPos = SHARED_MEMORY.getValueAtPos
SHARED_MEMORY.dataSetter =
{
--Terminated at -1
[SHARED_MEMORY.AVAILABLE_TYPES.ARRAY] = function (c, value)
setValueAtPos(c, 1, #value)
value = table.concat(value, "&").."&"
for i = 1, #value do
setValueAtPos(c, i, value:byte(i))
end
setValueAtPos(c, #value+1, -1)
end,
--Terminated at -1
[SHARED_MEMORY.AVAILABLE_TYPES.STRING] = function (c, value)
for i = 1, #value do
setValueAtPos(c, i, value:byte(i))
end
setValueAtPos(c, #value+1, -1)
end,
--Represented as a string
[SHARED_MEMORY.AVAILABLE_TYPES.NUMBER] = function (c, value)
value = tostring(value)
for i = 1, #value do
setValueAtPos(c, i, value:byte(i))
end
setValueAtPos(c, #value+1, -1)
end,
--Defined as array with: key&value&key&value
--Terminated at -1
[SHARED_MEMORY.AVAILABLE_TYPES.MAP] = function (c, value)
local count = 1
for k, v in pairs(value) do
for i = 1, #k do
setValueAtPos(c, count, k:byte(i))
count = count + 1
end
setValueAtPos(c, count, string.byte("&"))
count = count+1
for i = 1, #v do
setValueAtPos(c, count, v:byte(i))
count = count + 1
end
setValueAtPos(c, count, string.byte("&"))
count = count+1
end
setValueAtPos(c, count+1, -1)
end
}
SHARED_MEMORY.dataGetter =
{
--Second byte represents its size
--& is reserved! Not gonna update myself
[SHARED_MEMORY.AVAILABLE_TYPES.ARRAY] = function (c)
local ret = {}
local nValue = ""
local value = getValueAtPos(c, 1)
local char
local i = 1
while(value ~= -1) do
char = string.char(value)
local numValue = tonumber(nValue)
if(char == "&") then
table.insert(ret, (numValue and numValue or nValue))
nValue = ""
else
nValue = nValue..char
end
i = i+1
value = getValueAtPos(c, i)
end
return ret
end,
--Terminated at -1
--Value is a string
[SHARED_MEMORY.AVAILABLE_TYPES.STRING] = function (c)
local ret = ""
local i = 1
local value = getValueAtPos(c, 1)
while(value ~= -1) do
ret = ret..string.char(value)
i = i + 1
value = getValueAtPos(c, i)
end
return ret
end,
--Terminated at -1
[SHARED_MEMORY.AVAILABLE_TYPES.NUMBER] = function (c)
local ret = ""
local i = 1
local value = getValueAtPos(c, 1)
-- While in range of 0 to 9 or "-" or "."
while((value >= 48 and value <= 57) or value == 45 or value == 46)do
ret = ret..string.char(value)
i = i + 1
value = getValueAtPos(c, i)
end
return tonumber(ret)
end,
--Defined as array with: key, value, key, value
[SHARED_MEMORY.AVAILABLE_TYPES.MAP] = function (c)
local ret = {}
local keyValue = ""
local isKey = true
local nValue = ""
local value = getValueAtPos(c, 1)
local char
local i = 1
while(value ~= -1) do
char = string.char(value)
if(char == "&") then
local numValue = tonumber(nValue)
if(isKey) then keyValue = (numValue and numValue or nValue)
else ret[keyValue] = (numValue and numValue or nValue) keyValue = "" end
isKey = not isKey
nValue = ""
else
nValue = nValue..char
end
i = i+1
value = getValueAtPos(c, i)
end
return ret
end
}
--Overridden inside sharedmemory for having a main controller
function SHARED_MEMORY.setVarValue(varName, value)
local mvar = checkRegistered(varName)
--Notify the set event
local varType = getValueAtPos(mvar, 0)
if(value ~= nil) then
SHARED_MEMORY.dataSetter[varType](mvar, value)
love.event.push("_SHARED_MEMORY_UPDATE_MEM", varName)
end
end
--In the 0 bit(sample) is stored its type
function SHARED_MEMORY.getVarValue(varName)
local mvar = checkRegistered(varName)
local varType = getValueAtPos(mvar, 0)
return SHARED_MEMORY.dataGetter[varType](mvar)
end
--Can only be called once
--Gets the ID from the params argument, requires SHARED_MEMORY.registerThread to be called
function SHARED_MEMORY.connectThread(...)
if(mId ~= nil) then error("You must not call connectThread twice")end
for i, v in ipairs({...}) do
if(type(v) == "string" and string.match(v, "_SHARED_MEMORY_UPDATE_MEM")) then
print("Connected")
mId = v
return
end
end
end
--May be called any amount of times, but for easier usability, call it just once
--Use the userdata as a trigger to start connecting variables
--Called only inside the thread
--Creates an association between the userdata and string
function SHARED_MEMORY.connectVariables(...)
local isNextName = false
local ref = nil
for i, v in ipairs({...}) do
if(isNextName) then
if(type(v) ~= "string") then
error("You must define connectVariables as userdata, string, userdata, string...")
end
isNextName = false
registeredVars[v] = ref
ref = nil
elseif (type(v) == "userdata") then
isNextName = true
ref = v
end
end
end
local cache = {}
function SHARED_MEMORY._checkUpdates()
local toUpdate = love.thread.getChannel(mId):pop()
while(toUpdate) do
cache[toUpdate] = true
toUpdate = love.thread.getChannel(mId):pop()
end
for name, willUpdate in pairs(cache) do
if(willUpdate) then
__MEMORY_ACCESS_ON_UPDATE(name)
cache[name] = false
end
end
end