save level on stop (loading soon)

This commit is contained in:
Zoey 2024-04-23 01:28:32 -07:00
parent d5530a1449
commit fae40178af
No known key found for this signature in database
GPG key ID: 8611B896D1AAFAF2
5 changed files with 40 additions and 3 deletions

26
Cargo.lock generated
View file

@ -56,6 +56,25 @@ dependencies = [
"rustc-demangle", "rustc-demangle",
] ]
[[package]]
name = "bincode"
version = "2.0.0-rc.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f11ea1a0346b94ef188834a65c068a03aec181c94896d481d7a0a40d85b0ce95"
dependencies = [
"bincode_derive",
"serde",
]
[[package]]
name = "bincode_derive"
version = "2.0.0-rc.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e30759b3b99a1b802a7a3aa21c85c3ded5c28e1c83170d82d70f08bbf7f3e4c"
dependencies = [
"virtue",
]
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.3.2" version = "1.3.2"
@ -84,6 +103,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
name = "classics" name = "classics"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"bincode",
"bytes", "bytes",
"flate2", "flate2",
"half", "half",
@ -535,6 +555,12 @@ version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "virtue"
version = "0.0.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9dcc60c0624df774c82a0ef104151231d37da4962957d691c011c852b2473314"
[[package]] [[package]]
name = "wasi" name = "wasi"
version = "0.11.0+wasi-snapshot-preview1" version = "0.11.0+wasi-snapshot-preview1"

View file

@ -4,6 +4,7 @@ name = "classics"
version = "0.1.0" version = "0.1.0"
[dependencies] [dependencies]
bincode = "2.0.0-rc.3"
bytes = "1" bytes = "1"
flate2 = "1" flate2 = "1"
half = "2" half = "2"

BIN
level.clw Normal file

Binary file not shown.

View file

@ -1,5 +1,7 @@
use std::collections::BTreeSet; use std::collections::BTreeSet;
use bincode::{Decode, Encode};
use crate::{packet::server::ServerPacket, util::neighbors}; use crate::{packet::server::ServerPacket, util::neighbors};
use self::block::BLOCK_INFO; use self::block::BLOCK_INFO;
@ -8,7 +10,7 @@ pub mod block;
pub mod generation; pub mod generation;
/// a classic level /// a classic level
#[derive(Debug, Clone)] #[derive(Debug, Clone, Encode, Decode)]
pub struct Level { pub struct Level {
/// the size of the level in the X direction /// the size of the level in the X direction
pub x_size: usize, pub x_size: usize,
@ -91,7 +93,7 @@ impl Level {
} }
/// struct describing a block update for the level to handle /// struct describing a block update for the level to handle
#[derive(Debug, Clone)] #[derive(Debug, Clone, Encode, Decode)]
pub struct BlockUpdate { pub struct BlockUpdate {
/// the index of the block to be updated /// the index of the block to be updated
pub index: usize, pub index: usize,

View file

@ -94,12 +94,20 @@ impl Server {
}); });
} }
}); });
handle_ticks(self.data).await; handle_ticks(self.data.clone()).await;
tokio::time::sleep(std::time::Duration::from_millis(1)).await; tokio::time::sleep(std::time::Duration::from_millis(1)).await;
// TODO: cancel pending tasks/send out "Server is stopping" messages *here* instead of elsewhere // TODO: cancel pending tasks/send out "Server is stopping" messages *here* instead of elsewhere
// rn the message isn't guaranteed to actually go out........ // rn the message isn't guaranteed to actually go out........
let data = self.data.read().await;
let mut encoder = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::best());
bincode::encode_into_std_write(&data.level, &mut encoder, bincode::config::standard())
.unwrap();
tokio::fs::write("level.clw", encoder.finish().unwrap())
.await
.unwrap();
Ok(()) Ok(())
} }
} }