mirror of
https://codeberg.org/demostf/edit.git
synced 2026-11-30 18:22:20 +01:00
anonymize cli option
This commit is contained in:
parent
bd5a990336
commit
90afd1d100
4 changed files with 138 additions and 6 deletions
115
wasm/src/anonymize.rs
Normal file
115
wasm/src/anonymize.rs
Normal file
|
|
@ -0,0 +1,115 @@
|
||||||
|
use std::cell::RefCell;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::sync::atomic::AtomicUsize;
|
||||||
|
use std::sync::atomic::Ordering;
|
||||||
|
|
||||||
|
use crate::mutate::MessageMutator;
|
||||||
|
use crate::mutate::MutatorList;
|
||||||
|
use crate::mutate::PacketMutator;
|
||||||
|
use tf_demo_parser::demo::data::MaybeUtf8String;
|
||||||
|
use tf_demo_parser::demo::data::UserInfo;
|
||||||
|
use tf_demo_parser::demo::message::Message;
|
||||||
|
use tf_demo_parser::demo::message::UserMessage;
|
||||||
|
use tf_demo_parser::demo::packet::stringtable::StringTableEntry;
|
||||||
|
use tf_demo_parser::demo::packet::Packet;
|
||||||
|
use tf_demo_parser::ParserState;
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct Anonymize {
|
||||||
|
user_info_table: AtomicUsize,
|
||||||
|
names: RefCell<HashMap<String, u16>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Anonymize {
|
||||||
|
fn anonymize_users_table_entry(&self, index: u16, entry: &mut StringTableEntry) {
|
||||||
|
if let Ok(Some(mut user_info)) = UserInfo::parse_from_string_table(
|
||||||
|
index,
|
||||||
|
entry.text.as_ref().map(|s| s.as_ref()),
|
||||||
|
entry.extra_data.as_ref().map(|data| data.data.clone()),
|
||||||
|
) {
|
||||||
|
self.names
|
||||||
|
.borrow_mut()
|
||||||
|
.insert(user_info.player_info.name.clone(), index);
|
||||||
|
|
||||||
|
user_info.player_info.friends_id = 0;
|
||||||
|
user_info.player_info.friends_name_bytes = Default::default();
|
||||||
|
user_info.player_info.name = format!("Redacted ({index})");
|
||||||
|
user_info.player_info.steam_id = format!("[U:10:{:09}]", index);
|
||||||
|
|
||||||
|
if let Ok(encoded) = user_info.encode_to_string_table() {
|
||||||
|
*entry = encoded;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn anonymize_text(&self, text: &str) -> String {
|
||||||
|
let mut text = text.to_string();
|
||||||
|
for (name, index) in self.names.borrow().iter() {
|
||||||
|
text = text.replace(name, &format!("Redacted ({index})"));
|
||||||
|
}
|
||||||
|
text
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PacketMutator for Anonymize {
|
||||||
|
fn mutate_packet(&mut self, packet: &mut Packet, state: &ParserState) {
|
||||||
|
match packet {
|
||||||
|
Packet::StringTables(table_message) => {
|
||||||
|
for (index, table) in table_message.tables.iter_mut().enumerate() {
|
||||||
|
if table.name == "userinfo" {
|
||||||
|
self.user_info_table.store(index, Ordering::SeqCst);
|
||||||
|
for (index, entry) in table.entries.iter_mut() {
|
||||||
|
self.anonymize_users_table_entry(*index, entry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Packet::Message(msg_packet) => {
|
||||||
|
for message in msg_packet.messages.iter_mut() {
|
||||||
|
self.mutate_message(message, state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MessageMutator for Anonymize {
|
||||||
|
fn mutate_message(&self, message: &mut Message, _state: &ParserState) {
|
||||||
|
match message {
|
||||||
|
Message::CreateStringTable(table_message) => {
|
||||||
|
if table_message.table.name == "userinfo" {
|
||||||
|
for (index, entry) in table_message.table.entries.iter_mut() {
|
||||||
|
self.anonymize_users_table_entry(*index, entry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Message::UpdateStringTable(table_message)
|
||||||
|
if table_message.table_id as usize
|
||||||
|
== self.user_info_table.load(Ordering::SeqCst) =>
|
||||||
|
{
|
||||||
|
for (index, entry) in table_message.entries.iter_mut() {
|
||||||
|
self.anonymize_users_table_entry(*index, entry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Message::UserMessage(user_message) => match user_message {
|
||||||
|
UserMessage::SayText2(text) => {
|
||||||
|
if let Some(from) = text.from.as_mut() {
|
||||||
|
*from = MaybeUtf8String::Valid(self.anonymize_text(from.as_ref()));
|
||||||
|
}
|
||||||
|
text.text = MaybeUtf8String::Valid(self.anonymize_text(text.text.as_ref()));
|
||||||
|
}
|
||||||
|
UserMessage::Text(text) => {
|
||||||
|
text.text = MaybeUtf8String::Valid(self.anonymize_text(text.text.as_ref()));
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
},
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn anonymize(mutators: &mut MutatorList) {
|
||||||
|
let mutator = Anonymize::default();
|
||||||
|
mutators.push_packet_mutator(mutator);
|
||||||
|
}
|
||||||
|
|
@ -7,6 +7,8 @@ use std::fs;
|
||||||
struct Args {
|
struct Args {
|
||||||
/// Path to the source demo
|
/// Path to the source demo
|
||||||
path: String,
|
path: String,
|
||||||
|
/// Path to the target demo, defaults to `out.dem`
|
||||||
|
output_path: Option<String>,
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
unlock_pov: bool,
|
unlock_pov: bool,
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
|
|
@ -17,6 +19,8 @@ struct Args {
|
||||||
remove_invalid_deletes: bool,
|
remove_invalid_deletes: bool,
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
fix_player_count: bool,
|
fix_player_count: bool,
|
||||||
|
#[arg(long)]
|
||||||
|
anonymize: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Args {
|
impl Args {
|
||||||
|
|
@ -24,12 +28,15 @@ impl Args {
|
||||||
let builder = EditOptions::builder()
|
let builder = EditOptions::builder()
|
||||||
.unlock_pov(self.unlock_pov)
|
.unlock_pov(self.unlock_pov)
|
||||||
.remove_invalid_deletes(self.remove_invalid_deletes)
|
.remove_invalid_deletes(self.remove_invalid_deletes)
|
||||||
.fix_player_count(self.fix_player_count);
|
.fix_player_count(self.fix_player_count)
|
||||||
|
.anonymize(self.anonymize);
|
||||||
if let (Some(from), Some(to)) = (self.from, self.to) {
|
if let (Some(from), Some(to)) = (self.from, self.to) {
|
||||||
builder.cut(TickRange {
|
builder
|
||||||
|
.cut(TickRange {
|
||||||
from: from.into(),
|
from: from.into(),
|
||||||
to: to.into(),
|
to: to.into(),
|
||||||
}).build()
|
})
|
||||||
|
.build()
|
||||||
} else {
|
} else {
|
||||||
builder.build()
|
builder.build()
|
||||||
}
|
}
|
||||||
|
|
@ -42,5 +49,6 @@ fn main() {
|
||||||
let options = args.get_options();
|
let options = args.get_options();
|
||||||
let file = fs::read(&args.path).unwrap();
|
let file = fs::read(&args.path).unwrap();
|
||||||
let output = edit(&file, options);
|
let output = edit(&file, options);
|
||||||
fs::write("out.dem", output).unwrap();
|
let output_path = args.output_path.as_deref().unwrap_or("out.dem");
|
||||||
|
fs::write(output_path, output).unwrap();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
mod anonymize;
|
||||||
mod clean;
|
mod clean;
|
||||||
mod cond;
|
mod cond;
|
||||||
mod cut;
|
mod cut;
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
use crate::anonymize::anonymize;
|
||||||
use crate::missing_preserve::removed_invalid_deletes;
|
use crate::missing_preserve::removed_invalid_deletes;
|
||||||
use crate::player_count::fix_player_count;
|
use crate::player_count::fix_player_count;
|
||||||
use crate::{clean_demo, strip_cond, unlock_pov, MutatorList};
|
use crate::{clean_demo, strip_cond, unlock_pov, MutatorList};
|
||||||
|
|
@ -26,6 +27,9 @@ pub struct EditOptions {
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
#[cfg_attr(feature = "cli", builder(default))]
|
#[cfg_attr(feature = "cli", builder(default))]
|
||||||
pub fix_player_count: bool,
|
pub fix_player_count: bool,
|
||||||
|
#[serde(default)]
|
||||||
|
#[cfg_attr(feature = "cli", builder(default))]
|
||||||
|
pub anonymize: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EditOptions {
|
impl EditOptions {
|
||||||
|
|
@ -66,6 +70,10 @@ impl EditOptions {
|
||||||
fix_player_count(&mut mutators, header, stream)?;
|
fix_player_count(&mut mutators, header, stream)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if self.anonymize {
|
||||||
|
anonymize(&mut mutators);
|
||||||
|
}
|
||||||
|
|
||||||
Ok(mutators)
|
Ok(mutators)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue