Add gzip compression to replay files

This commit is contained in:
Sofia 2026-07-27 00:06:21 +03:00
parent 36424543a8
commit 7c0957f7cf
3 changed files with 56 additions and 8 deletions

42
Cargo.lock generated
View File

@ -2,6 +2,12 @@
# It is not intended for manual editing.
version = 4
[[package]]
name = "adler2"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"
[[package]]
name = "cfg-if"
version = "1.0.4"
@ -35,6 +41,15 @@ dependencies = [
"half",
]
[[package]]
name = "crc32fast"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9481c1c90cbf2ac953f07c8d4a58aa3945c425b7185c9154d67a65e4230da511"
dependencies = [
"cfg-if",
]
[[package]]
name = "crunchy"
version = "0.2.4"
@ -47,6 +62,16 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"
[[package]]
name = "flate2"
version = "1.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "843fba2746e448b37e26a819579957415c8cef339bf08564fe8b7ddbd959573c"
dependencies = [
"crc32fast",
"miniz_oxide",
]
[[package]]
name = "gdextension-api"
version = "0.5.1"
@ -172,6 +197,16 @@ version = "0.2.186"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66"
[[package]]
name = "miniz_oxide"
version = "0.8.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316"
dependencies = [
"adler2",
"simd-adler32",
]
[[package]]
name = "nanoserde"
version = "0.2.1"
@ -201,6 +236,7 @@ name = "quakeball"
version = "0.1.0"
dependencies = [
"ciborium",
"flate2",
"godot",
"serde",
"teanet",
@ -255,6 +291,12 @@ dependencies = [
"serde_core",
]
[[package]]
name = "simd-adler32"
version = "0.3.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a219298ac11a56ea9a6d2120044824d6f01aeb034955e7af7bc16858527deea"
[[package]]
name = "syn"
version = "2.0.119"

View File

@ -8,6 +8,7 @@ crate-type = ["cdylib"] # Compile this crate to a dynamic C library.
[dependencies]
ciborium = "0.2.2"
flate2 = "1.1.9"
godot = "0.5.4"
serde = "1.0.228"
teanet = { path = "../teanet-rust" }

View File

@ -1,5 +1,9 @@
use std::{collections::HashMap, io::Cursor};
use std::{
collections::HashMap,
io::{Cursor, Read, Write},
};
use flate2::{Compression, bufread::GzDecoder, write::GzEncoder};
use godot::{
classes::{FileAccess, file_access::ModeFlags},
prelude::*,
@ -15,7 +19,6 @@ use crate::{
DamageSource, IPlayer, NetTransform, ball::Ball, remote_player::RemotePlayer,
weapon::WeaponType,
},
team_resource::TeamResource,
};
pub const STANDALONE_REPLAY_GLOBAL: &str = "StandaloneReplayGlobal";
@ -46,8 +49,10 @@ impl StandaloneReplayManager {
bytes.extend(file.get_buffer(1_000_000).to_vec());
}
let bytes = Cursor::new(&mut bytes);
if let Ok(res) = ciborium::from_reader::<Replay, _>(bytes) {
let cursor = Cursor::new(&mut bytes);
let decoder = GzDecoder::new(cursor);
if let Ok(res) = ciborium::from_reader::<Replay, _>(decoder) {
self.loading_replay = true;
let map_idx = res.map_idx;
self.replay = Some(res);
@ -348,10 +353,10 @@ impl INode for ReplayRecorder {
impl ReplayRecorder {
pub fn save_to(&self, path: &GString) {
if let Some(mut file) = FileAccess::open(path, ModeFlags::WRITE) {
let mut buf = Vec::new();
let cursor = Cursor::new(&mut buf);
ciborium::into_writer(&self.replay, cursor).ok();
file.store_buffer(&PackedArray::from_iter(buf));
let mut bytes = Vec::new();
let encoder = GzEncoder::new(&mut bytes, Compression::default());
ciborium::into_writer(&self.replay, encoder).ok();
file.store_buffer(&PackedArray::from_iter(bytes));
}
}