-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbizhawk-crowd-shuffler.lua
185 lines (135 loc) · 4.35 KB
/
bizhawk-crowd-shuffler.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
local config = {}
config.sessionPath = os.getenv("session") and os.getenv("session") or 'default'
local pathseparator = package.config:sub(1,1)
config.gamePath = "." .. pathseparator .. "sessions" .. pathseparator .. config.sessionPath .. pathseparator .. "CurrentROMs" .. pathseparator
config.savePath = "." .. pathseparator .. "sessions" .. pathseparator .. config.sessionPath .. pathseparator .. "CurrentSaves" .. pathseparator
local frame = 0
local frame_check_mod = 10 -- check every X frames
local function isempty(s)
return s == nil or s == ''
end
local commands = {}
function commands.switchRom(rom)
local currentGame = userdata.get("currentGame")
print("DEBUG: switchRom=" .. rom)
if(currentGame ) then
savestate.save(config.savePath .. currentGame .. ".State")
end
local nextGame = rom
client.openrom(config.gamePath .. nextGame)
savestate.load(config.savePath .. nextGame .. ".State")
userdata.set("currentGame", nextGame)
end
function commands.ping()
-- print("DEBUG: heartbeat received")
comm.socketServerSend("pong");
end
local function parseAndExecuteResponse(response)
for line in string.gmatch(response, "([^\n]+)") do
local t={}
for str in string.gmatch(line, "([^\t]+)") do
table.insert(t, str)
end
local input = {
command = t[1],
args = t[2]
}
-- print("DEBUG: command=" .. input.command)
local command = commands[input.command]
if(command) then
commands[input.command](input.args)
end
end
end
local function main()
-- purge socket data
comm.socketServerSetTimeout(14)
comm.socketServerResponse()
while true do -- The main cycle that causes the emulator to advance and trigger a game switch.
frame = frame + 1
if (frame % frame_check_mod) == 0 then
frame = 0
local response = comm.socketServerResponse()
if isempty(response) == false then
parseAndExecuteResponse(response)
end
end
emu.frameadvance()
end
print("loaded, checking every " .. frame_check_mod .. " frames")
end
if emu then
main()
else
print("Running tests")
-- run tests
comm = {}
local function test_ping()
local called = false
comm.socketServerSend = function()
called = true
end
commands.ping()
assert(called)
end
local function test_parseAndExecuteResponse()
local called = false
commands.test = function()
called = true
end
assert(called == false)
parseAndExecuteResponse("test")
assert(called)
end
local function test_parseAndExecuteResponse_withArgs()
local called = false
local args
commands.test = function(input)
called = true
args = input
end
assert(called == false)
parseAndExecuteResponse("test\tfoo")
assert(called)
assert(args == "foo")
end
local function test_parseAndExecuteResponse_withMultipleCommands()
local called = 0
commands.test = function(input)
called = called + 1
end
assert(called == 0)
parseAndExecuteResponse("test\ntest\n")
assert(called == 2)
end
local function test_switchRom()
userdata = {}
savestate = {}
client = {}
client.openrom = function(args)
client.openrom__args = args
end
savestate.load = function(args)
savestate.load__args = args
end
savestate.save = function(args)
savestate.save__args = args
end
userdata.get = function()
return "foo.nes"
end
userdata.set = function(key, value)
userdata.set__value = value
end
commands.switchRom("bar.nes")
assert(savestate.load__args == config.savePath .. "bar.nes.State")
assert(userdata.set__value == "bar.nes")
assert(savestate.save__args == config.savePath .. "foo.nes.State")
assert(client.openrom__args == config.gamePath .. "bar.nes")
end
test_ping()
test_parseAndExecuteResponse()
test_parseAndExecuteResponse_withArgs()
test_parseAndExecuteResponse_withMultipleCommands()
test_switchRom();
end