"""Opt-in local boss music, prepared off-thread and played in a bounded child.""" import hashlib import importlib.util import json import math import os from pathlib import Path import re import shutil import subprocess import threading import time import uuid import wave _spec=importlib.util.spec_from_file_location('pg_audio_process',Path(__file__).with_name('audio_process.py')) _process=importlib.util.module_from_spec(_spec);_spec.loader.exec_module(_process) Child=_process.Child MAX_SOURCE=512<<20 MAX_WAVE=320<<20 MAX_CACHE=1<<30 MAX_SECONDS=1800 ASSET=re.compile(r'^[a-f0-9]{32}$') UNIT=re.compile(r'^[A-Za-z0-9_]{1,180}$') TOKEN=re.compile(r'^[A-Za-z0-9_:\-]{1,240}$') def atomic(path,text,*,durable=True): tmp=path.with_name(path.name+'.tmp') if path.is_symlink() or tmp.is_symlink():raise ValueError('音频配置路径无效') with tmp.open('w',encoding='utf-8',newline='\n')as f: f.write(text);f.flush() if durable:os.fsync(f.fileno()) os.replace(tmp,path) class LocalAudio: def __init__(self,runtime,data,tools,bosses,game_status,*,child=Child,clock=time.monotonic,wall=time.time): self.runtime=Path(runtime).resolve();self.root=Path(data).resolve()/hashlib.sha256(str(self.runtime).encode()).hexdigest()[:16] self.root.mkdir(parents=True,exist_ok=True) self.tools=Path(tools);self.bosses=dict(bosses);self.game_status=game_status;self.child=child;self.clock=clock;self.wall=wall self.lock=threading.RLock();self.closed=threading.Event();self.client=uuid.uuid4().hex self.save_lock=threading.Lock() self.supported=self.tools.joinpath('ffmpeg.exe').is_file()and self.tools.joinpath('LocalAudioPlayer.exe').is_file() self.mapping={};self.volume=50;self.revision=0;self.message='选择 BOSS 后导入本地音频,切换到本地替换模式后开战播放' self.importing=None;self.convert=None;self.player=None;self.token='';self.source='';self.sent_volume=None self.ack_state='';self.ack_at=0;self.failed=set();self.next_link=0;self.thread=None self.opened_at=0;self.progress_at=0;self.position=-1;self.log_lines=0 self.link_failures=0;self.link_faulted=False self.link=self.runtime/'battle-local-link.tsv';self.settings=self.root/'settings.json' try: if self.settings.exists(): if self.settings.is_symlink()or self.settings.stat().st_size>65536:raise ValueError('配置过大') cfg=json.loads(self.settings.read_text(encoding='utf-8')) if cfg.get('schema')!=1 or type(cfg.get('volume'))is not int or not 0<=cfg['volume']<=100:raise ValueError('配置格式无效') for unit,row in cfg.get('mapping',{}).items(): if unit not in self.bosses or not isinstance(row,dict)or not ASSET.fullmatch(row.get('asset','')):raise ValueError('配置单位无效') if not isinstance(row.get('name'),str)or len(row['name'])>240 or type(row.get('seconds'))not in (float,int)or not math.isfinite(row['seconds'])or not 04096:raise ValueError('BOSS 或文件路径无效') with self.lock: if self.importing:raise ValueError('正在处理一个音频,请等待完成') task={'id':uuid.uuid4().hex,'boss':request['boss'],'cancel':False,'started':self.clock()};self.importing=task self.message='正在后台准备音频,完成前继续使用原配置' threading.Thread(target=self._import,args=(task,request['path']),name='local-audio-import',daemon=True).start() else:raise ValueError('音频请求格式无效') return self.snapshot() def _import(self,task,source): target=self.root/(task['id']+'.wav');guard=None;committed=False try: if not self._import_active(task):return # Remote paths and alternate streams are not opened by the import worker. if source.startswith(('\\\\','//'))or '\x00'in source or (os.name=='nt'and (not re.match(r'^[A-Za-z]:[\\/]',source)or ':'in source[2:])):raise ValueError('请选择本地磁盘上的音频文件') path=Path(source).resolve(strict=True) if not path.is_file()or not 0MAX_CACHE or shutil.disk_usage(self.root).free=MAX_WAVE:raise ValueError('无法解码该音频,已保留原配置') with wave.open(str(target),'rb')as wav: seconds=wav.getnframes()/wav.getframerate() if wav.getnchannels()!=2 or wav.getsampwidth()!=2 or wav.getframerate()!=44100 or not 04096:return data=json.loads(line) if data.get('state')not in ('opening','ready','playing','failed'):return position=data.get('position') if type(position)not in (float,int)or not math.isfinite(position)or not 0<=position<=MAX_SECONDS+1:return with self.lock: if self.player is not player:return changed=self.ack_state!=data['state'] self.ack_state=data['state'];self.ack_at=self.clock() if position!=self.position:self.position=position;self.progress_at=self.clock() if changed:self._log('player state='+data['state']+' position='+str(position)) except (OSError,ValueError):pass def _send(self,text): player=self.player if not player or self.closed.is_set():return player.process.stdin.write((text+'\n').encode('ascii'));player.process.stdin.flush() def tick(self): if self.closed.is_set():return with self.lock: mapping=dict(self.mapping);volume=self.volume;failed=set(self.failed) if not mapping and not self.player:return game=self.game_status();v=game.get('values',{});session=game.get('game_session')or '' available=game.get('ready')and game.get('age_seconds')is not None and 0<=game['age_seconds']<=2.5 and v.get('battle_music_mode')=='LOCAL' unit=v.get('battle_local_unit','');token=v.get('battle_local_token','') selected=available and v.get('battle_local_client')==self.client and unit in mapping and unit not in failed and TOKEN.fullmatch(token) if not selected:self._stop() elif token!=self.token or mapping[unit]['asset']!=getattr(self,'play_asset',None): self._stop();asset=self.root/(mapping[unit]['asset']+'.wav') if asset.is_symlink()or not asset.is_file():self.failed.add(unit);self.message='本地曲目文件已丢失,已跳过';selected=False else: candidate=None try: candidate=self.child([str(self.tools/'LocalAudioPlayer.exe'),str(asset)],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.DEVNULL,memory=256<<20) fresh=self.game_status();fv=fresh.get('values',{}) with self.lock: row=self.mapping.get(unit) current=(not self.closed.is_set()and row and row['asset']==mapping[unit]['asset']and unit not in self.failed and fresh.get('ready')is True and fresh.get('game_session')==session and fresh.get('age_seconds')is not None and 0<=fresh['age_seconds']<=2.5 and fv.get('battle_music_mode')=='LOCAL'and fv.get('battle_local_client')==self.client and fv.get('battle_local_token')==token and fv.get('battle_local_unit')==unit) if current: self.player=candidate;self.token,self.source=token,unit;self.ack_at=self.clock();self.ack_state='opening' self.opened_at=self.clock();self.play_asset=row['asset'];self.position=-1;self.progress_at=self.clock();volume=self.volume published=candidate;candidate=None if current:threading.Thread(target=self._listen,args=(published,),name='local-audio-readback',daemon=True).start() except OSError: self.failed.add(unit);self.message='独立播放器无法启动,已跳过本场曲目';self._stop();selected=False finally: if candidate:candidate.close() if self.player: if self.player.process.poll()is not None or self.ack_state=='failed'or self.clock()-self.ack_at>2 or (self.ack_state=='opening'and self.clock()-self.opened_at>2)or (self.sent_volume is not None and self.clock()-self.progress_at>3): self.failed.add(self.source);self.message='播放器未及时响应,已停止并交还原版';self._stop() else: self._send('PING') if v.get('battle_local_ready')=='true'and self.ack_state in ('ready','playing')and self.sent_volume!=volume: self._send('PLAY '+str(volume));self.sent_volume=volume self.progress_at=self.clock() elif v.get('battle_local_ready')!='true'and self.sent_volume is not None:self._stop() if available and not self.closed.is_set()and not self.link_faulted and self.clock()>=self.next_link: self.next_link=self.clock()+1 with self.lock: ids=','.join(sorted(u for u in self.mapping if u not in self.failed)) prepared=self.token if self.player and self.ack_state in ('ready','playing')else '-' if not TOKEN.fullmatch(session):return try: atomic(self.link,'LocalMusicLinkV1\t'+str(int(self.wall()))+'\t'+session+'\t'+self.client+'\t'+prepared+'\t'+ids+'\n',durable=False) self.link_failures=0 except (OSError,ValueError)as error: self.link_failures=3 if isinstance(error,ValueError)else self.link_failures+1 self._log('heartbeat write failed: '+type(error).__name__+' attempt='+str(self.link_failures)) if self.link_failures>=3: with self.lock:self.failed.update(self.mapping);self.link_faulted=True self._stop();self.message='音乐状态连续未同步,已停止本地播放;可手动重试' else:self.message='音乐状态暂未同步,稍后自动重试;连续失败会停止本地播放' def _loop(self): while not self.closed.wait(0.25): try: with self.lock: if self.importing and self.clock()-self.importing['started']>55: self.importing['cancel']=True;self.message='音频文件读取长时间未响应,已暂停本次导入;其他功能可继续使用' self.tick() except Exception as error: with self.lock:self.failed.update(self.mapping) self.message='本地音频状态异常,已暂停替换' print('[LocalAudio] monitor disabled: '+type(error).__name__,flush=True) try:self._stop() except OSError:pass def close(self): self.closed.set();self._stop() with self.lock: if self.importing:self.importing['cancel']=True converting=self.convert if converting:converting.close() if self.thread:self.thread.join(timeout=1)