forked from Alvytv/emu-coop
-
Notifications
You must be signed in to change notification settings - Fork 3
/
driver.lua
428 lines (380 loc) · 13.1 KB
/
driver.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
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
-- ACTUAL WORK HAPPENS HERE
function contains(t, e)
for i = 1,#t do
if t[i] == e then return true end
end
return false
end
function memoryRead(addr, size)
if not size or size == 1 then
return memory.readbyte(addr)
elseif size == 2 then
return memory.readword(addr)
elseif size == 4 then
return memory.readdword(addr)
else
error("Invalid size to memoryRead")
end
end
function memoryWrite(addr, value, size)
if not size or size == 1 then
memory.writebyte(addr, value)
elseif size == 2 then
memory.writeword(addr, value)
elseif size == 4 then
memory.writedword(addr, value)
else
error("Invalid size to memoryWrite")
end
end
function recordChanged(record, value, previousValue, receiving,addr)
local allow = true
if type(record.kind) == "function" then
allow, value = record.kind(value, previousValue, receiving)
elseif record.kind == "HealthShare" then
if opts.hpshare then
if record.stype == "uHighsLow" then
allow = previousValue > value
if value >= previousValue then record.cache = value end
elseif record.stype == "uLowsHigh" then
allow = value > previousValue
if previousValue >= value then record.cache = value end
elseif record.stype == "uInstantRefill" then
local state = memory.readbyte(0x7E0010)
if state ~= 0x12 then
local healthRefill = memory.readbyte(0x7EF372)
local maxHealth = memory.readbyte(0x7EF36C)
if healthRefill > 8 then
value = math.min(value + healthRefill - 8, maxHealth)
healthRefill = 8
memory.writebyte(0x7EF372, healthRefill)
memory.writebyte(0x7EF36D, value)
end
value = math.min(value, maxHealth)
allow = value ~= previousValue
record.name = nil
record.verb = nil
if receiving and value == 0x00 and allow then
local currentHealth = memory.readbyte(0x7EF36D)
memory.writebyte(0x7E0373, currentHealth)
record.name = "- Press F to Pay Respects."
record.verb = "died"
end
elseif not receiving then
allow = (value == 0x00) and (value ~= previousValue)
else
allow = false
end
end
else
allow = false
end
elseif record.kind == "MagicShare" then
if opts.magicshare then
if record.stype == "uHighsLow" then
allow = previousValue > value
if value >= previousValue then record.cache = value end
elseif record.stype == "uLowsHigh" then
if addr == 8319859 and previousValue == 127 and value == 128 then -- magic refill from bottle hax
allow = false
if previousValue >= value then record.cache = value end
else
allow = value > previousValue
if previousValue >= value then record.cache = value end
end
elseif record.stype == "uInstantRefill" then
allow = true
local magicRefill = memory.readbyte(0x7EF373)
local maxMagic = 0x80
if magicRefill > 1 then
value = math.min(value + magicRefill - 1, maxMagic)
magicRefill = 1
memory.writebyte(0x7EF373, magicRefill)
memory.writebyte(0x7EF36E, value)
end
allow = value ~= previousValue
end
else
allow = false
end
elseif record.kind == "high" then
local maskedValue = value -- Backup value and previousValue
local maskedPreviousValue = previousValue
if record.mask then -- If necessary, mask both before checking
maskedValue = AND(maskedValue, record.mask)
maskedPreviousValue = AND(maskedPreviousValue, record.mask)
end
allow = maskedValue > maskedPreviousValue
if record.mask then
value = OR(AND(previousValue, XOR(0xFF, record.mask)), maskedValue)
end
elseif record.kind == "low" then
allow = previousValue > value
elseif record.kind == "either" then
allow = value ~= previousValue
elseif record.kind == "bitOr" then
local maskedValue = value -- Backup value and previousValue
local maskedPreviousValue = previousValue
if record.mask then -- If necessary, mask both before checking
maskedValue = AND(maskedValue, record.mask)
maskedPreviousValue = AND(maskedPreviousValue, record.mask)
end
maskedValue = OR(maskedValue, maskedPreviousValue)
allow = maskedValue ~= maskedPreviousValue -- Did operated-on bits change?
value = OR(previousValue, maskedValue) -- Copy operated-on bits back into value
elseif record.kind == "custom" then
--print("got custom v="..value.." pv="..previousValue)
elseif record.kind == "clock" then
if previousValue == 0x58 and value == 0x27 then
allow = true
else
allow = false
end
elseif record.kind == "state" then
if value == 0x19 and previousValue ~= value then
record.cache = value
allow = true
elseif previousValue ~= value then
record.cache = value
allow = false
else
allow = false
end
elseif record.kind == "bottle" then
if value < previousValue then
record.verb = "used"
record.name = record.nameMap[previousValue]
record.cache = value
elseif previousValue < value then
record.verb = "got"
record.name = record.nameMap[value]
record.cache = value
end
allow = value ~= previousValue
elseif record.kind == "key" then
if value < previousValue then
record.verb = "used"
elseif previousValue < value then
record.verb = "got"
end
allow = value ~= previousValue
else
allow = value ~= previousValue
end
-- Checking additional conditions
if allow and record.cond then
allow = performTest(record.cond, value, record.size)
end
-- Cleanup
if allow and record.kind == "state" then
if value == 0x19 and previousValue < value then
record.name = "game"
record.verb = "finished"
end
end
if allow and record.kind == "key" and opts.retromode then
memory.writebyte(0x7EF38B, value)
end
return allow, value
end
function stayAsleep(record, value)
if not record then return false end
if not record.sleep then return false end
return record.sleep(value)
end
function performTest(record, valueOverride, sizeOverride)
if not record then return true end
if record[1] == "test" then
local value = valueOverride or memoryRead(record.addr, sizeOverride or record.size)
if record.gte and record.lte then
return (not record.gte or value >= record.gte) and
(not record.lte or value <= record.lte) and
(value ~= 0x17) and -- 17 save & quit
(value ~= 0x14) -- 14 intro between title and file select (aka history mode)
elseif record.values then
return contains(record.values, value)
else
return false
end
elseif record[1] == "stringtest" then
local cmatch = 0
local match = false
local test = record.value
local len = #test
local addr = record.addr
for token in string.gmatch(test, "([^,]+)") do
cmatch = 0
for i=0,#token-1 do
if string.byte(token, i+1) == memory.readbyte(addr+i) then
cmatch = cmatch+1
end
end
if cmatch == #token then match = true end
end
if match then return true else return false end
elseif record[1] == "optiontest" then
return opts[record.addr] == record.value
else
return false
end
end
class.GameDriver(Driver)
function GameDriver:_init(spec, forceSend)
self.spec = spec
self.sleepQueue = {}
self.forceSend = forceSend
self.didCache = false
end
function GameDriver:checkFirstRunning() -- Do first-frame bootup-- only call if isRunning()
if not self.didCache then
if driverDebug then print("First moment running") end
message("Coop mode: " .. self.spec.guid)
if self.forceSend then message("Syncing...") end
for k,v in pairs(self.spec.sync) do -- Enter all current values into cache so we don't send pointless 0 values later
local value = memoryRead(k, v.size)
if not v.cache then v.cache = value end
if self.forceSend then -- Restoring after a crash send all values regardless of importance
if value ~= 0 then -- FIXME: This is adequate for all current specs but maybe it will not be in future?!
if driverDebug then print("Sending address " .. tostring(k) .. " at startup") end
self:sendTable({addr=k, value=value})
end
end
end
if self.forceSend then
self.forceSend = false
message("Syncing...done!")
end
if self.spec.startup then
self.spec.startup(self.forceSend)
end
self.didCache = true
end
end
function GameDriver:childTick()
if self:isRunning(false) then
self:checkFirstRunning()
if #self.sleepQueue > 0 then
local sQueue = self.sleepQueue
self.sleepQueue = {}
for i, v in ipairs(sQueue) do
self:handleTable(v)
end
end
end
end
function GameDriver:childWake()
self:sendTable({"hello", version=version.release, guid=self.spec.guid, options=opts})
for k,v in pairs(self.spec.sync) do
local syncTable = self.spec.sync -- Assume sync table is not replaced at runtime
local baseAddr = k - (k%2) -- 16-bit aligned equivalent of address
local size = v.size or 1
local function callback(a,b) -- I have no idea what "b" is but snes9x passes it
-- So, this is pretty awful: There is a bug in some versions of snes9x-rr where you if you have registered a registerwrite for an even and odd address,
-- SOMETIMES (not always) writing to the odd address will trigger the even address's callback instead. So when we get a callback we trigger the underlying
-- callback twice, once for each byte in the current word. This does mean caughtWrite() must tolerate spurious extra calls.
for offset=0,1 do
local checkAddr = baseAddr + offset
local record = syncTable[checkAddr]
if record then self:caughtWrite(checkAddr, b, record, size) end
end
end
memory.registerwrite (k, size, callback)
end
end
function GameDriver:isRunning(receiving)
if receiving then
return performTest(self.spec.running) and performTest(self.spec.receiving)
else
return performTest(self.spec.running)
end
end
function GameDriver:caughtWrite(addr, arg2, record, size)
local running = self.spec.running
if self:isRunning(false) then -- TODO: Yes, we got record, but double check
self:checkFirstRunning()
local allow = true
local value = memoryRead(addr, size)
if record.cache then
allow = recordChanged(record, value, record.cache, false, addr)
end
if allow then
record.cache = value -- FIXME: Should this cache EVER be cleared? What about when a new game starts?
self:sendTable({addr=addr, value=value})
end
else
--if driverDebug then print("Ignored memory write because the game is not running") end
end
end
function GameDriver:handleTable(t)
if t[1] == "hello" then
if t.guid ~= self.spec.guid then
self.pipe:abort("Partner has an incompatible .lua file for this game.")
print("Partner's game mode file has guid:\n" .. tostring(t.guid) .. "\nbut yours has:\n" .. tostring(self.spec.guid))
end
if not optionsMatch(opts, t.options) then
self.pipe:abort("Partner disagrees on options chosen.")
print("Partner's options are:\n" .. tostring(t.options) .. "\nbut yours are:\n" .. tostring(opts))
end
return
end
local addr = t.addr
local record = self.spec.sync[addr]
if (self:isRunning(true) and not stayAsleep(record, t.value)) then
self:checkFirstRunning()
if record then
local value = t.value
local allow = true
local previousValue = memoryRead(addr, record.size)
allow, value = recordChanged(record, value, previousValue, true, addr)
if allow then
if record.receiveTrigger then -- Extra setup/cleanup on receive
record.receiveTrigger(value, previousValue)
end
local name = record.name
local names = nil
local msgMask = record.msgMask
if not name and record.nameMap then
name = record.nameMap[value]
end
if name then
names = {name}
elseif record.nameBitmap then
names = {}
for b=0,7 do
if 0 ~= AND(BIT(b), value) and 0 == AND(BIT(b), previousValue) then
if msgMask then
if 0 ~= AND(BIT(b), msgMask) then
table.insert(names, record.nameBitmap[b + 1])
end
else
table.insert(names, record.nameBitmap[b + 1])
end
end
end
if next(names) == nil then
names = nil
end
end
if names then
local verb = record.verb or "got"
for i, v in ipairs(names) do
message("Partner " .. verb .. " " .. v)
end
else
if driverDebug then print("Updated anonymous address " .. tostring(addr) .. " to " .. tostring(value)) end
end
record.cache = value
memoryWrite(addr, value, record.size)
end
else
if driverDebug then print("Unknown memory address was " .. tostring(addr)) end
message("Partner changed unknown memory address...? Uh oh")
end
else
if driverDebug then print("Queueing partner memory write because the game is not running") end
table.insert(self.sleepQueue, t)
end
end
function GameDriver:handleError(s, err)
print("FAILED TABLE LOAD " .. err)
end