Skip to content

Commit

Permalink
Add voice back (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
LaihoE authored Mar 15, 2024
1 parent 03b3691 commit 05f6d3d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 7 deletions.
30 changes: 30 additions & 0 deletions src/node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use parser::second_pass::parser_settings::create_huffman_lookup_table;
use parser::second_pass::variants::soa_to_aos;
use parser::second_pass::variants::BytesVariant;
use parser::second_pass::variants::OutputSerdeHelperStruct;
use parser::second_pass::voice_data::convert_voice_data_to_wav;
use serde_json::Value;
use std::collections::HashMap;
use std::fs::File;
Expand All @@ -31,6 +32,35 @@ fn parse_demo(bytes: BytesVariant, parser: &mut Parser) -> Result<DemoOutput, Er
},
}
}
#[napi]
pub fn parse_voice(path_or_buf: Either<String, Buffer>) -> napi::Result<HashMap<String, Vec<u8>>> {
let bytes = resolve_byte_type(path_or_buf).unwrap();
let settings = ParserInputs {
wanted_players: vec![],
wanted_player_props: vec![],
wanted_other_props: vec![],
wanted_events: vec![],
wanted_ticks: vec![],
real_name_to_og_name: AHashMap::default(),
parse_ents: false,
parse_projectiles: false,
only_header: false,
count_props: false,
only_convars: false,
huffman_lookup_table: &vec![],
};
let mut parser = Parser::new(settings, false);
let output = parse_demo(bytes, &mut parser)?;
let out = match convert_voice_data_to_wav(output.voice_data) {
Ok(out) => out,
Err(e) => return Err(Error::new(Status::InvalidArg, format!("{}", e).to_owned())),
};
let mut out_hm = HashMap::default();
for (steamid, bytes) in out {
out_hm.insert(steamid, bytes);
}
Ok(out_hm)
}

#[napi]
pub fn list_game_events(path_or_buf: Either<String, Buffer>) -> napi::Result<Value> {
Expand Down
53 changes: 46 additions & 7 deletions src/python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use parser::second_pass::game_events::GameEvent;
use parser::second_pass::parser_settings::create_huffman_lookup_table;
use parser::second_pass::variants::VarVec;
use parser::second_pass::variants::Variant;
use parser::second_pass::voice_data::convert_voice_data_to_wav;
use polars::prelude::ArrowField;
use polars::prelude::NamedFrom;
use polars::series::Series;
Expand All @@ -21,11 +22,11 @@ use pyo3::exceptions::PyValueError;
use pyo3::ffi::Py_uintptr_t;
use pyo3::prelude::*;
use pyo3::types::IntoPyDict;
use pyo3::types::PyBytes;
use pyo3::types::PyDict;
use pyo3::types::PyList;
use pyo3::Python;
use pyo3::{PyAny, PyObject, PyResult};
use std::collections::HashMap;
use std::sync::Arc;

use pyo3::create_exception;
Expand Down Expand Up @@ -676,6 +677,44 @@ impl DemoParser {
};
Ok(event_series)
}
pub fn parse_voice(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
let mmap = match create_mmap(self.path.clone()) {
Ok(mmap) => mmap,
Err(e) => {
return Err(Exception::new_err(format!(
"{}. File name: {}",
e,
self.path.clone()
)))
}
};
let settings = ParserInputs {
wanted_players: vec![],
wanted_player_props: vec![],
wanted_other_props: vec![],
wanted_events: vec![],
wanted_ticks: vec![],
real_name_to_og_name: AHashMap::default(),
parse_ents: false,
parse_projectiles: false,
only_header: false,
count_props: false,
only_convars: false,
huffman_lookup_table: &vec![],
};
let mut parser = Parser::new(settings, false);
let output = match parser.parse_demo(&mmap) {
Ok(output) => output,
Err(e) => return Err(PyValueError::new_err(format!("{}", e))),
};
let out = convert_voice_data_to_wav(output.voice_data).unwrap();
let mut out_hm = AHashMap::default();
for (steamid, bytes) in out {
let py_bytes = PyBytes::new(py, &bytes);
out_hm.insert(steamid, py_bytes);
}
Ok(out_hm.to_object(py))
}

#[args(py_kwargs = "**")]
pub fn parse_ticks(
Expand Down Expand Up @@ -1151,7 +1190,7 @@ fn to_bool_series(pairs: &Vec<&EventField>, name: &String) -> DataFrameColumn {
}
DataFrameColumn::Series(Series::new(name, v))
}
fn to_py_sticker_col(pairs: &Vec<&EventField>, name: &String, py: Python) -> DataFrameColumn {
fn to_py_sticker_col(pairs: &Vec<&EventField>, _name: &String, py: Python) -> DataFrameColumn {
let mut v: Vec<Vec<_>> = vec![];
for pair in pairs {
match &pair.data {
Expand All @@ -1160,11 +1199,11 @@ fn to_py_sticker_col(pairs: &Vec<&EventField>, name: &String, py: Python) -> Dat
let mut vv = vec![];
for sticker in weapon {
let dict = PyDict::new(py);
dict.set_item("id", sticker.id.to_object(py));
dict.set_item("name", sticker.name.to_object(py));
dict.set_item("wear", sticker.wear.to_object(py));
dict.set_item("x", sticker.x.to_object(py));
dict.set_item("y", sticker.y.to_object(py));
let _ = dict.set_item("id", sticker.id.to_object(py));
let _ = dict.set_item("name", sticker.name.to_object(py));
let _ = dict.set_item("wear", sticker.wear.to_object(py));
let _ = dict.set_item("x", sticker.x.to_object(py));
let _ = dict.set_item("y", sticker.y.to_object(py));
vv.push(dict);
}
v.push(vv)
Expand Down

0 comments on commit 05f6d3d

Please sign in to comment.