From 2d0d71f9ec81e9be0d49b7dc4d79e977b62b2ed5 Mon Sep 17 00:00:00 2001 From: MinusKelvin Date: Sun, 17 Nov 2019 00:12:28 +1100 Subject: [PATCH] significantly reduce replay file size --- .gitignore | 2 +- gui/Cargo.toml | 3 ++- gui/src/local.rs | 11 +++++++---- gui/src/replay.rs | 7 ++++--- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 592255c..54230ec 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,4 @@ playout* Cargo.lock options.yaml -replay.json \ No newline at end of file +replay.dat \ No newline at end of file diff --git a/gui/Cargo.toml b/gui/Cargo.toml index 1070001..2314e1c 100644 --- a/gui/Cargo.toml +++ b/gui/Cargo.toml @@ -14,7 +14,8 @@ libtetris = { path = "../libtetris" } battle = { path = "../battle" } bot = { path = "../bot" } serde = "1" -serde_json = "1" +bincode = "1" +libflate = "0.1" serde_yaml = "0.8" rand = "0.7.0" rand_pcg = "0.2.0" diff --git a/gui/src/local.rs b/gui/src/local.rs index 8ab2cc0..6cab0ce 100644 --- a/gui/src/local.rs +++ b/gui/src/local.rs @@ -11,6 +11,7 @@ use rand::prelude::*; use crate::input::InputSource; use crate::replay::InfoReplay; use std::collections::VecDeque; +use libflate::deflate; type InputFactory = dyn Fn(Board) -> (Box, String); @@ -74,16 +75,18 @@ impl EventHandler for LocalGame<'_> { while timer::check_update_time(ctx, 60) { let do_update = match self.state { State::GameOver(0) => { - serde_json::to_writer( - std::io::BufWriter::new( - std::fs::File::create("replay.json" - ).unwrap()), + let mut encoder = deflate::Encoder::new( + std::fs::File::create("replay.dat" + ).unwrap()); + bincode::serialize_into( + &mut encoder, &InfoReplay { replay: self.battle.replay.clone(), p1_info_updates: self.p1_info_updates.clone(), p2_info_updates: self.p2_info_updates.clone() } ).unwrap(); + encoder.finish().unwrap(); // Don't catch up after pause due to replay saving while timer::check_update_time(ctx, 60) {} diff --git a/gui/src/replay.rs b/gui/src/replay.rs index 1138c6f..b764af2 100644 --- a/gui/src/replay.rs +++ b/gui/src/replay.rs @@ -7,6 +7,7 @@ use crate::Resources; use battle::{ Battle, Controller, Replay }; use std::collections::VecDeque; use serde::{ Serialize, Deserialize }; +use libflate::deflate; pub struct ReplayGame<'a, P> { gui: Gui, @@ -23,8 +24,8 @@ impl<'a, P: AsRef + Clone> ReplayGame<'a, P> { pub fn new(resources: &'a mut Resources, file: P) -> Self { let InfoReplay { replay, p1_info_updates, p2_info_updates - } = serde_json::from_reader( - std::io::BufReader::new(std::fs::File::open(file.clone()).unwrap()) + } = bincode::deserialize_from( + deflate::Decoder::new(std::fs::File::open(file.clone()).unwrap()) ).unwrap(); let battle = Battle::new( replay.config, replay.p1_seed, replay.p2_seed, replay.garbage_seed @@ -61,7 +62,7 @@ impl + Clone> EventHandler for ReplayGame<'_, P> { loop { match std::fs::File::open(self.file.clone()) { Ok(f) => { - match serde_json::from_reader(std::io::BufReader::new(f)) { + match bincode::deserialize_from(deflate::Decoder::new(f)) { Ok(r) => { replay = r; break