Use postcard to save worlds instead of ron
This commit is contained in:
parent
9b7691d84e
commit
938eed66eb
3 changed files with 35 additions and 40 deletions
18
Cargo.lock
generated
18
Cargo.lock
generated
|
@ -911,6 +911,12 @@ dependencies = [
|
|||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cobs"
|
||||
version = "0.2.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "67ba02a97a2bd10f4b59b25c7973101c79642302776489e030cd13cdab09ed15"
|
||||
|
||||
[[package]]
|
||||
name = "cocoa"
|
||||
version = "0.24.1"
|
||||
|
@ -2081,8 +2087,8 @@ name = "planet"
|
|||
version = "0.3.0"
|
||||
dependencies = [
|
||||
"bevy",
|
||||
"postcard",
|
||||
"rand",
|
||||
"ron",
|
||||
"serde",
|
||||
]
|
||||
|
||||
|
@ -2098,6 +2104,16 @@ dependencies = [
|
|||
"miniz_oxide 0.3.7",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "postcard"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1c2b180dc0bade59f03fd005cb967d3f1e5f69b13922dad0cd6e047cb8af2363"
|
||||
dependencies = [
|
||||
"cobs",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pp-rs"
|
||||
version = "0.2.1"
|
||||
|
|
|
@ -23,5 +23,7 @@ version = "1.0"
|
|||
default-features = false
|
||||
features = ["derive"]
|
||||
|
||||
[dependencies.ron]
|
||||
version = "0.8.0"
|
||||
[dependencies.postcard]
|
||||
version = "1.0.2"
|
||||
default-features = false
|
||||
features = ["use-std"]
|
|
@ -19,7 +19,7 @@ use {
|
|||
#[derive(Debug)]
|
||||
pub enum LoadError {
|
||||
MissingSave(io::Error),
|
||||
InvalidSave(ron::error::SpannedError),
|
||||
InvalidSave(postcard::Error),
|
||||
}
|
||||
impl Error for LoadError {
|
||||
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
||||
|
@ -49,7 +49,7 @@ impl Display for LoadError {
|
|||
#[derive(Debug)]
|
||||
pub enum SaveError {
|
||||
MissingWorld,
|
||||
SerializationError(ron::Error),
|
||||
SerializationError(postcard::Error),
|
||||
FailedToWrite(io::Error),
|
||||
}
|
||||
impl Error for SaveError {
|
||||
|
@ -94,30 +94,20 @@ impl WorldManager {
|
|||
}
|
||||
|
||||
pub fn save_world<P: AsRef<Path>>(&self, path: P) -> Result<(), SaveError> {
|
||||
let world = match self.get_world() {
|
||||
Some(world) => world,
|
||||
None => {
|
||||
warn!("No world to save");
|
||||
return Err(SaveError::MissingWorld);
|
||||
},
|
||||
let Some(world) = self.get_world() else {
|
||||
warn!("No world to save");
|
||||
return Err(SaveError::MissingWorld);
|
||||
};
|
||||
#[cfg(feature = "logging")]
|
||||
let serialized = match ron::ser::to_string_pretty(world, default()) {
|
||||
|
||||
|
||||
let serialized = match postcard::to_stdvec(world) {
|
||||
Ok(serialized) => serialized,
|
||||
Err(err) => {
|
||||
return Err(SaveError::SerializationError(err));
|
||||
},
|
||||
};
|
||||
|
||||
#[cfg(not(feature = "logging"))]
|
||||
let serialized = match ron::to_string(world) {
|
||||
Ok(serialized) => serialized,
|
||||
Err(err) => {
|
||||
return Err(SaveError::SerializationError(err));
|
||||
},
|
||||
};
|
||||
|
||||
match File::create(path).unwrap().write_all(serialized.as_bytes()) {
|
||||
match File::create(path).unwrap().write_all(serialized.as_slice()) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(err) => Err(SaveError::FailedToWrite(err)),
|
||||
}
|
||||
|
@ -130,14 +120,12 @@ impl WorldManager {
|
|||
return Err(LoadError::MissingSave(err));
|
||||
},
|
||||
};
|
||||
let mut buf = String::new();
|
||||
match file.read_to_string(&mut buf) {
|
||||
Ok(_) => {},
|
||||
Err(err) => {
|
||||
return Err(LoadError::MissingSave(err));
|
||||
},
|
||||
let mut buf = vec![];
|
||||
if let Err(err) = file.read_to_end(&mut buf) {
|
||||
return Err(LoadError::MissingSave(err));
|
||||
};
|
||||
match ron::from_str(buf.as_str()) {
|
||||
|
||||
match postcard::from_bytes(buf.as_slice()) {
|
||||
Ok(world) => {
|
||||
self.world = Some(world);
|
||||
Ok(())
|
||||
|
@ -146,17 +134,6 @@ impl WorldManager {
|
|||
}
|
||||
}
|
||||
|
||||
// #[cfg(feature = "render")]
|
||||
// pub fn toggle_contours(&mut self) {
|
||||
// #[cfg(feature = "logging")]
|
||||
// if self.contours {
|
||||
// debug!("Turning terrain contours off");
|
||||
// } else {
|
||||
// debug!("Turning terrain contours on");
|
||||
// }
|
||||
// self.contours = !self.contours;
|
||||
// }
|
||||
|
||||
#[must_use]
|
||||
pub fn get_world(&self) -> Option<&World> {
|
||||
self.world.as_ref()
|
||||
|
|
Loading…
Reference in a new issue