import"./logger-z_EpIdIa.js";import{A as e,D as t,O as n,_ as r,a as i,b as a,d as o,l as s,n as c,r as l,t as u,v as d,y as f}from"./streamHelpers-AIec78DP.js";import{i as p}from"./scene-BdOVRsxo.js";import{I as m}from"./index-DBtsNu05.js";function h(e){let t=null,n=null,i=null,a=null,o=null,s=null,c=NaN,l=null;for(let u=0;u=3&&d[1]?.endsWith(`Game`)&&(i=d[1]),e[u]!==`readplayerinfo`)continue;let f=e[u+1];if(f){if(f.startsWith(`1 `)){let e=f.split(` `);e[1]&&(c=parseInt(e[1],10)),e[2]&&(s=r(e[2]).trim());continue}if(f.startsWith(`2 `)){let e=f.split(` `);e[1]&&(a=e[1]),e[3]&&(l=e[3]),e[4]&&(t=e[4]);continue}if(f.startsWith(`3 `)){let e=f.split(` `);e[1]&&(o=e[1]),e[2]&&(n=e[2])}}}return{missionDisplayName:t,missionType:n,gameClassName:i,serverDisplayName:a,mod:o,recorderName:s,recorderClientId:Number.isFinite(c)?c:null,recordingDate:l}}function g(e){let t={weaponsHud:null,backpackHud:null,inventoryHud:null,teamScores:[],playerRoster:new Map,chatMessages:[],clockTimeMin:null,gravity:-20};if(!e.length)return t;let n=0,i=()=>{let t=e[n++];return t===``?``:t??``};if(i(),n>=e.length)return t;let a=parseInt(i(),10)||0,o=new Map;for(let e=0;e0&&o.set(s,(o.get(s)??0)+1)}if(n>=e.length||(i(),n>=e.length))return t;{let e=i().split(` `),n=e[0]??``,r=e[1]===`1`||e[1]===`true`,a=e[2]??``,o=e[4]===`1`||e[4]===`true`;r&&n&&(t.backpackHud={packIndex:c(n),active:o,text:a})}if(n>=e.length)return t;let s=i().split(` `),l=parseInt(s[4],10)||0,u=parseInt(s[5],10)||0,d=parseInt(s[6],10);for(let e=0;e=e.length)return t;let p=i().split(` `),m=parseInt(p[4],10)||0,h=parseInt(p[5],10)||0,g=parseInt(p[6],10);for(let e=0;e0&&e.set(n,r)}e.size>0&&(t.inventoryHud={slots:e,activeSlot:isNaN(g)?-1:g})}if(n>=e.length)return t;let _=i().split(` `),v=_[1]??``,y=parseInt(_[2],10)||0,b=[];for(let e=0;e=8)for(let e=0;e<2;e++){let n=e*4,r=e+1;t.teamScores.push({teamId:r,name:b[n]??``,score:parseInt(b[n+1],10)||0,playerCount:o.get(r)??0})}else if(v===`TR2Game`&&y>=4)for(let e=0;e<2;e++){let n=e*2,r=e+1;t.teamScores.push({teamId:r,name:b[n+1]??``,score:parseInt(b[n],10)||0,playerCount:o.get(r)??0})}if(n>=e.length)return t;{let e=i().split(` `),n=parseFloat(e[1]??``);Number.isFinite(n)&&(t.clockTimeMin=n)}for(let r=0;r<10&&!(n>=e.length);r++){let e=i();e&&t.chatMessages.push(e)}if(n=0?t(`Player`,this.initialBlock.controlObjectGhostIndex):void 0;for(let e of this.initialBlock.initialGhosts){if(e.type!==`create`||e.classId==null)continue;let r=this.registry.getGhostParser(e.classId)?.name;if(!r)throw Error(`No ghost parser for classId ${e.classId} (ghost index ${e.index})`);let i=t(r,e.index),a={id:i,ghostIndex:e.index,className:r,spawnTick:0,type:n(r),rotation:[0,0,0,1]};if(this.applyGhostData(a,e.parsedData),e.parsedData){let t=p(r,e.index,e.parsedData);t&&(a.sceneData=t)}this.entities.set(i,a),this.entityIdByGhostIndex.set(e.index,i)}if(this.playerSensorGroup===0&&this.lastControlType===`player`&&this.latestControl.ghostIndex>=0){let e=this.entityIdByGhostIndex.get(this.latestControl.ghostIndex),t=e?this.entities.get(e):void 0;t?.sensorGroup!=null&&t.sensorGroup>0&&(this.playerSensorGroup=t.sensorGroup)}for(let e of this.initialBlock.initialEvents){let t=this.registry.getEventParser(e.classId)?.name;if(t===`SetSensorGroupEvent`&&e.parsedData){let t=e.parsedData.sensorGroup;t!=null&&(this.playerSensorGroup=t)}else if(t===`RemoteCommandEvent`&&e.parsedData){let t=this.resolveNetString(e.parsedData.funcName),n=e.parsedData.args;t===`ServerMessage`&&this.handleServerMessage(n),this.handleHudRemoteCommand(t,n)}}let c=g(this.initialBlock.demoValues);c.weaponsHud&&(this.weaponsHud.slots=c.weaponsHud.slots,this.weaponsHud.activeIndex=c.weaponsHud.activeIndex),c.backpackHud&&(this.backpackHud.packIndex=c.backpackHud.packIndex,this.backpackHud.active=c.backpackHud.active,this.backpackHud.text=c.backpackHud.text),c.inventoryHud&&(this.inventoryHud.slots=c.inventoryHud.slots,this.inventoryHud.activeSlot=c.inventoryHud.activeSlot),this.teamScores=c.teamScores,this.playerRoster=new Map(c.playerRoster),c.clockTimeMin!=null&&(this.clockAnchorStreamSec=0,this.clockDurationMs=c.clockTimeMin*60*1e3);for(let e of c.chatMessages){let t=o(e);if(!t.length)continue;let n=t.map(e=>e.text).join(``);if(!n.trim())continue;let r=t[0].colorCode;if(t.some(e=>e.colorCode===3||e.colorCode===4)&&n.includes(`: `)){let e=n.indexOf(`: `);this.pushChatMessage({timeSec:0,sender:n.slice(0,e),text:n.slice(e+2),kind:`chat`,colorCode:r,segments:t})}else this.pushChatMessage({timeSec:0,sender:``,text:n,kind:`server`,colorCode:r,segments:t})}this.updateCameraAndHud()}getSnapshot(){if(this._cachedSnapshot&&this._cachedSnapshotTick===this.moveTicks)return this._cachedSnapshot;let e=this.buildSnapshot();return this._cachedSnapshot=e,this._cachedSnapshotTick=this.moveTicks,e}getEffectShapes(){let e=new Set,t=t=>{let n=t.dtsFileName;n&&e.add(n);let r=t.subExplosions;if(Array.isArray(r))for(let t of r){if(t==null)continue;let n=this.getDataBlockData(t);n?.dtsFileName&&e.add(n.dtsFileName)}};for(let[,e]of this.initialBlock.dataBlocks){let n=e.data?.explosion;if(n==null)continue;let r=this.getDataBlockData(n);r&&t(r)}return[...e]}stepToTime(e,t=1/0){let n=Number.isFinite(e)?Math.max(0,e):0,r=Math.floor(n*1e3/32),i=!1;r({index:e,ammo:t})),activeIndex:this.weaponsHud.activeIndex},o=n&&n.inventoryHudGen===this._inventoryHudGen?n.inventoryHud:{slots:Array.from(this.inventoryHud.slots.entries()).map(([e,t])=>({slot:e,count:t})),activeSlot:this.inventoryHud.activeSlot},s=n&&n.backpackPackIndex===this.backpackHud.packIndex&&n.backpackActive===this.backpackHud.active&&n.backpackText===this.backpackHud.text?n.backpackHud:this.backpackHud.packIndex>=0?{...this.backpackHud}:null,c,l;if(n&&n.teamScoresGen===this._teamScoresGen&&n.rosterGen===this._rosterGen)c=n.teamScores,l=n.playerRoster;else{c=this.teamScores.map(e=>({...e}));let e=new Map;for(let{teamId:t}of this.playerRoster.values())t>0&&e.set(t,(e.get(t)??0)+1);for(let t of c)t.playerCount=e.get(t.teamId)??0;l=[];for(let[e,t]of this.playerRoster)l.push({clientId:e,...t})}return this._snap={teamScoresGen:this._teamScoresGen,rosterGen:this._rosterGen,teamScores:c,playerRoster:l,weaponsHudGen:this._weaponsHudGen,weaponsHud:a,inventoryHudGen:this._inventoryHudGen,inventoryHud:o,backpackPackIndex:this.backpackHud.packIndex,backpackActive:this.backpackHud.active,backpackText:this.backpackHud.text,backpackHud:s},{timeSec:t,exhausted:this.exhausted,camera:this.camera,entities:e,controlPlayerGhostId:this.controlPlayerGhostId,playerSensorGroup:this.playerSensorGroup,status:this.lastStatus,chatMessages:r,audioEvents:i,weaponsHud:a,backpackHud:s,inventoryHud:o,teamScores:c,playerRoster:l,connectedClientId:this.connectedClientId,matchClockMs:this.computeMatchClockMs(t)}}isPacketData(e){return!!e&&typeof e==`object`&&`gameState`in e&&`events`in e&&`ghosts`in e}isMoveData(e){return!!e&&typeof e==`object`&&`yaw`in e}isInfoData(e){return!!e&&typeof e==`object`&&`value1`in e&&typeof e.value1==`number`&&`value2`in e&&typeof e.value2==`number`}};async function v(t){let n=new e(new Uint8Array(t)),{header:r,initialBlock:i}=await n.load(),a=h(i.demoValues),o=new _(n);return o.missionDisplayName=a.missionDisplayName,o.missionTypeDisplayName=a.missionType,o.gameClassName=a.gameClassName,o.serverDisplayName=a.serverDisplayName,o.connectedPlayerName=a.recorderName,o.connectedClientId=a.recorderClientId,{source:`demo`,duration:r.demoLengthMs/1e3,missionName:i.missionName??null,gameType:a.missionType,serverDisplayName:a.serverDisplayName,recorderName:a.recorderName,recordingDate:a.recordingDate,streamingPlayback:o}}export{v as createDemoStreamingRecording};