Skip to content

Commit df4eba1

Browse files
ZunaweFlySniper
authored andcommitted
BizHawkClient: Add BizHawkClient (ArchipelagoMW#1978)
Adds a generic client that can communicate with BizHawk. Similar to SNIClient, but for arbitrary systems and doesn't have an intermediary application like SNI.
1 parent 214dc0f commit df4eba1

File tree

8 files changed

+1300
-0
lines changed

8 files changed

+1300
-0
lines changed

BizHawkClient.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from __future__ import annotations
2+
3+
import ModuleUpdate
4+
ModuleUpdate.update()
5+
6+
from worlds._bizhawk.context import launch
7+
8+
if __name__ == "__main__":
9+
launch()

data/lua/base64.lua

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
-- This file originates from this repository: https://github.com/iskolbin/lbase64
2+
-- It was modified to translate between base64 strings and lists of bytes instead of base64 strings and strings.
3+
4+
local base64 = {}
5+
6+
local extract = _G.bit32 and _G.bit32.extract -- Lua 5.2/Lua 5.3 in compatibility mode
7+
if not extract then
8+
if _G._VERSION == "Lua 5.4" then
9+
extract = load[[return function( v, from, width )
10+
return ( v >> from ) & ((1 << width) - 1)
11+
end]]()
12+
elseif _G.bit then -- LuaJIT
13+
local shl, shr, band = _G.bit.lshift, _G.bit.rshift, _G.bit.band
14+
extract = function( v, from, width )
15+
return band( shr( v, from ), shl( 1, width ) - 1 )
16+
end
17+
elseif _G._VERSION == "Lua 5.1" then
18+
extract = function( v, from, width )
19+
local w = 0
20+
local flag = 2^from
21+
for i = 0, width-1 do
22+
local flag2 = flag + flag
23+
if v % flag2 >= flag then
24+
w = w + 2^i
25+
end
26+
flag = flag2
27+
end
28+
return w
29+
end
30+
end
31+
end
32+
33+
34+
function base64.makeencoder( s62, s63, spad )
35+
local encoder = {}
36+
for b64code, char in pairs{[0]='A','B','C','D','E','F','G','H','I','J',
37+
'K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y',
38+
'Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n',
39+
'o','p','q','r','s','t','u','v','w','x','y','z','0','1','2',
40+
'3','4','5','6','7','8','9',s62 or '+',s63 or'/',spad or'='} do
41+
encoder[b64code] = char:byte()
42+
end
43+
return encoder
44+
end
45+
46+
function base64.makedecoder( s62, s63, spad )
47+
local decoder = {}
48+
for b64code, charcode in pairs( base64.makeencoder( s62, s63, spad )) do
49+
decoder[charcode] = b64code
50+
end
51+
return decoder
52+
end
53+
54+
local DEFAULT_ENCODER = base64.makeencoder()
55+
local DEFAULT_DECODER = base64.makedecoder()
56+
57+
local char, concat = string.char, table.concat
58+
59+
function base64.encode( arr, encoder )
60+
encoder = encoder or DEFAULT_ENCODER
61+
local t, k, n = {}, 1, #arr
62+
local lastn = n % 3
63+
for i = 1, n-lastn, 3 do
64+
local a, b, c = arr[i], arr[i + 1], arr[i + 2]
65+
local v = a*0x10000 + b*0x100 + c
66+
local s
67+
s = char(encoder[extract(v,18,6)], encoder[extract(v,12,6)], encoder[extract(v,6,6)], encoder[extract(v,0,6)])
68+
t[k] = s
69+
k = k + 1
70+
end
71+
if lastn == 2 then
72+
local a, b = arr[n-1], arr[n]
73+
local v = a*0x10000 + b*0x100
74+
t[k] = char(encoder[extract(v,18,6)], encoder[extract(v,12,6)], encoder[extract(v,6,6)], encoder[64])
75+
elseif lastn == 1 then
76+
local v = arr[n]*0x10000
77+
t[k] = char(encoder[extract(v,18,6)], encoder[extract(v,12,6)], encoder[64], encoder[64])
78+
end
79+
return concat( t )
80+
end
81+
82+
function base64.decode( b64, decoder )
83+
decoder = decoder or DEFAULT_DECODER
84+
local pattern = '[^%w%+%/%=]'
85+
if decoder then
86+
local s62, s63
87+
for charcode, b64code in pairs( decoder ) do
88+
if b64code == 62 then s62 = charcode
89+
elseif b64code == 63 then s63 = charcode
90+
end
91+
end
92+
pattern = ('[^%%w%%%s%%%s%%=]'):format( char(s62), char(s63) )
93+
end
94+
b64 = b64:gsub( pattern, '' )
95+
local t, k = {}, 1
96+
local n = #b64
97+
local padding = b64:sub(-2) == '==' and 2 or b64:sub(-1) == '=' and 1 or 0
98+
for i = 1, padding > 0 and n-4 or n, 4 do
99+
local a, b, c, d = b64:byte( i, i+3 )
100+
local s
101+
local v = decoder[a]*0x40000 + decoder[b]*0x1000 + decoder[c]*0x40 + decoder[d]
102+
table.insert(t,extract(v,16,8))
103+
table.insert(t,extract(v,8,8))
104+
table.insert(t,extract(v,0,8))
105+
end
106+
if padding == 1 then
107+
local a, b, c = b64:byte( n-3, n-1 )
108+
local v = decoder[a]*0x40000 + decoder[b]*0x1000 + decoder[c]*0x40
109+
table.insert(t,extract(v,16,8))
110+
table.insert(t,extract(v,8,8))
111+
elseif padding == 2 then
112+
local a, b = b64:byte( n-3, n-2 )
113+
local v = decoder[a]*0x40000 + decoder[b]*0x1000
114+
table.insert(t,extract(v,16,8))
115+
end
116+
return t
117+
end
118+
119+
return base64

0 commit comments

Comments
 (0)