Files
WoW-Hybrid-gateway/ops/octo_radio_stations.py

76 lines
4.1 KiB
Python

#!/usr/bin/env python3
"""Radio stations from the current Octo client (owner 2026-11-27: client data, not the server).
Station names: the generated STATIONS block of Octo's radio UI (patch-5.mpq,
Interface/FrameXML/OctoRadioPlaceholder/OctoRadioPlaceholder.lua).
Stream URLs: Octo SoundEntries.dbc rows named radio_octo_<station slug> (patch-O.mpq), the rows Octo's
server answers ".radio <n>" with. Writes the C++ table the Backend player accepts (it never streams an URL
sent by Lua) and the Lua list the OctoRadio addon shows.
Usage: octo_radio_stations.py [--octo-data DIR]
"""
import argparse, ctypes as C, re, struct, sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent))
from octo_client_data import read_member
ROOT = Path(__file__).resolve().parents[1]
LIB = Path('/mnt/Games/@project/gateway/build/octo-data/libmpq-readonly.so')
LUA = 'Interface\\FrameXML\\OctoRadioPlaceholder\\OctoRadioPlaceholder.lua'
DBC = 'DBFilesClient\\SoundEntries.dbc'
def api():
a = C.CDLL(str(LIB))
sig = {'archive_open': [C.POINTER(C.c_void_p), C.c_char_p, C.c_int64], 'archive_close': [C.c_void_p],
'file_number': [C.c_void_p, C.c_char_p, C.POINTER(C.c_uint32)],
'file_size_unpacked': [C.c_void_p, C.c_uint32, C.POINTER(C.c_int64)],
'file_read': [C.c_void_p, C.c_uint32, C.c_void_p, C.c_int64, C.POINTER(C.c_int64)]}
for n, args in sig.items():
f = getattr(a, 'libmpq__' + n); f.argtypes = args; f.restype = C.c_int32
return a
def member(a, archive, name):
h = C.c_void_p()
if a.libmpq__archive_open(C.byref(h), str(archive).encode(), -1): raise SystemExit(f'cannot open {archive}')
try: return read_member(a, h, name)
finally: a.libmpq__archive_close(h)
def slug(name):
return re.sub(r'[^a-z0-9]+', '_', name.lower()).strip('_')
def main():
p = argparse.ArgumentParser(); p.add_argument('--octo-data', type=Path, default=Path.home() / 'Games/octo/drive_c/Octowow/Data')
args = p.parse_args(); a = api()
lua = member(a, args.octo_data / 'patch-5.mpq', LUA)
if not lua: raise SystemExit('radio UI not in patch-5')
block = re.search(rb'--@OCTORADIO_STATIONS@(.*?)--@END_OCTORADIO_STATIONS@', lua, re.S).group(1).decode()
stations = [(n, int(c)) for n, c in re.findall(r'name\s*=\s*"([^"]+)"\s*,\s*cmd\s*=\s*(\d+)', block)]
dbc = member(a, args.octo_data / 'patch-O.mpq', DBC)
if not dbc: raise SystemExit('SoundEntries not in patch-O (patch-O missing/empty?)')
n, f, rs, _ = struct.unpack_from('<4I', dbc, 4); st = 20 + n * rs
text = lambda o: dbc[st + o:dbc.index(b'\0', st + o)].decode('latin1')
urls = {}
for i in range(n):
r = struct.unpack_from('<%dI' % f, dbc, 20 + i * rs)
if text(r[2]).startswith('radio_octo_') and text(r[3]).startswith('http'): urls[text(r[2])[11:]] = (r[0], text(r[3]))
rows = []
for name, cmd in stations:
if slug(name) not in urls: raise SystemExit(f'no SoundEntries stream for station {name!r}')
sound, url = urls[slug(name)]
if not re.fullmatch(r'http://[A-Za-z0-9.-]+(:\d+)?/[A-Za-z0-9_./-]+', url): raise SystemExit(f'unexpected URL {url!r}')
rows.append((cmd, name, sound, url))
hpp = ['// Generated by ops/octo_radio_stations.py from the Octo client (patch-5 radio UI + patch-O SoundEntries).',
'#pragma once', '#include <array>', '#include <string_view>', 'namespace gateway {',
'struct RadioStation { unsigned cmd; std::string_view name; unsigned sound; std::string_view url; };',
f'inline constexpr std::array<RadioStation,{len(rows)}> RadioStations{{{{']
hpp += [f' {{{c},"{nm}",{s},"{u}"}},' for c, nm, s, u in rows]
hpp += ['}};', '}']
(ROOT / 'include/gateway/radio_stations.hpp').write_text('\n'.join(hpp) + '\n')
lua_out = ['-- Generated by ops/octo_radio_stations.py from the Octo client; do not edit.', 'OctoRadioStations = {']
lua_out += [f' {{ name = "{nm}", cmd = {c} }},' for c, nm, s, u in rows]
lua_out += ['}']
(ROOT / 'client_addons/OctoRadio/Stations.lua').write_text('\n'.join(lua_out) + '\n')
for r in rows: print(*r)
if __name__ == '__main__':
main()