add count_ticks method

This commit is contained in:
Robin Appelman 2022-10-26 22:57:41 +02:00
commit def9f3da63
3 changed files with 27 additions and 1 deletions

View file

@ -12,3 +12,8 @@ export async function edit(bytes: Uint8Array, options: EditOptions): Promise<Uin
let m = await import(/* webpackChunkName: "demos-tf-edit" */ "../pkg/index.js");
return m.edit_js(bytes, options);
}
export async function count_ticks(bytes: Uint8Array): Promise<number> {
let m = await import(/* webpackChunkName: "demos-tf-edit" */ "../pkg/index.js");
return m.count_ticks(bytes);
}

View file

@ -1,7 +1,7 @@
{
"author": "Robin Appelman <robin@icewind.nl>",
"name": "@demostf/edit",
"version": "0.1.2",
"version": "0.1.3",
"scripts": {
"build": "rimraf dist pkg && webpack",
"start": "rimraf dist pkg && webpack-dev-server --open -d",

View file

@ -14,6 +14,7 @@ use bitbuffer::{BitRead, BitWriteStream, LittleEndian};
use tf_demo_parser::demo::message::packetentities::EntityId;
use bitbuffer::BitWrite;
use tf_demo_parser::demo::data::DemoTick;
use crate::clean::clean_demo;
use crate::cond::strip_cond;
@ -50,6 +51,26 @@ pub fn edit(input: &[u8], options: EditOptions) -> Vec<u8> {
}
}
#[wasm_bindgen]
pub fn count_ticks(input: &[u8]) -> u32 {
let demo = Demo::new(&input);
let mut stream = demo.get_stream();
let header = Header::read(&mut stream).unwrap();
let mut tick = DemoTick::default();
let mut packets = RawPacketStream::new(stream);
let mut handler = DemoHandler::default();
handler.handle_header(&header);
while let Some(packet) = packets.next(&handler.state_handler).unwrap() {
tick = packet.tick();
handler.handle_packet(packet).unwrap();
}
tick.into()
}
fn no_cut(input: &[u8], options: EditOptions) -> Vec<u8> {
let mut out_buffer = Vec::with_capacity(input.len());
{