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",
|
"winapi",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cobs"
|
||||||
|
version = "0.2.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "67ba02a97a2bd10f4b59b25c7973101c79642302776489e030cd13cdab09ed15"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cocoa"
|
name = "cocoa"
|
||||||
version = "0.24.1"
|
version = "0.24.1"
|
||||||
|
@ -2081,8 +2087,8 @@ name = "planet"
|
||||||
version = "0.3.0"
|
version = "0.3.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bevy",
|
"bevy",
|
||||||
|
"postcard",
|
||||||
"rand",
|
"rand",
|
||||||
"ron",
|
|
||||||
"serde",
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -2098,6 +2104,16 @@ dependencies = [
|
||||||
"miniz_oxide 0.3.7",
|
"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]]
|
[[package]]
|
||||||
name = "pp-rs"
|
name = "pp-rs"
|
||||||
version = "0.2.1"
|
version = "0.2.1"
|
||||||
|
|
|
@ -23,5 +23,7 @@ version = "1.0"
|
||||||
default-features = false
|
default-features = false
|
||||||
features = ["derive"]
|
features = ["derive"]
|
||||||
|
|
||||||
[dependencies.ron]
|
[dependencies.postcard]
|
||||||
version = "0.8.0"
|
version = "1.0.2"
|
||||||
|
default-features = false
|
||||||
|
features = ["use-std"]
|
|
@ -19,7 +19,7 @@ use {
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum LoadError {
|
pub enum LoadError {
|
||||||
MissingSave(io::Error),
|
MissingSave(io::Error),
|
||||||
InvalidSave(ron::error::SpannedError),
|
InvalidSave(postcard::Error),
|
||||||
}
|
}
|
||||||
impl Error for LoadError {
|
impl Error for LoadError {
|
||||||
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
||||||
|
@ -49,7 +49,7 @@ impl Display for LoadError {
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum SaveError {
|
pub enum SaveError {
|
||||||
MissingWorld,
|
MissingWorld,
|
||||||
SerializationError(ron::Error),
|
SerializationError(postcard::Error),
|
||||||
FailedToWrite(io::Error),
|
FailedToWrite(io::Error),
|
||||||
}
|
}
|
||||||
impl Error for SaveError {
|
impl Error for SaveError {
|
||||||
|
@ -94,30 +94,20 @@ impl WorldManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn save_world<P: AsRef<Path>>(&self, path: P) -> Result<(), SaveError> {
|
pub fn save_world<P: AsRef<Path>>(&self, path: P) -> Result<(), SaveError> {
|
||||||
let world = match self.get_world() {
|
let Some(world) = self.get_world() else {
|
||||||
Some(world) => world,
|
warn!("No world to save");
|
||||||
None => {
|
return Err(SaveError::MissingWorld);
|
||||||
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,
|
Ok(serialized) => serialized,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
return Err(SaveError::SerializationError(err));
|
return Err(SaveError::SerializationError(err));
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(not(feature = "logging"))]
|
match File::create(path).unwrap().write_all(serialized.as_slice()) {
|
||||||
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()) {
|
|
||||||
Ok(_) => Ok(()),
|
Ok(_) => Ok(()),
|
||||||
Err(err) => Err(SaveError::FailedToWrite(err)),
|
Err(err) => Err(SaveError::FailedToWrite(err)),
|
||||||
}
|
}
|
||||||
|
@ -130,14 +120,12 @@ impl WorldManager {
|
||||||
return Err(LoadError::MissingSave(err));
|
return Err(LoadError::MissingSave(err));
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
let mut buf = String::new();
|
let mut buf = vec![];
|
||||||
match file.read_to_string(&mut buf) {
|
if let Err(err) = file.read_to_end(&mut buf) {
|
||||||
Ok(_) => {},
|
return Err(LoadError::MissingSave(err));
|
||||||
Err(err) => {
|
|
||||||
return Err(LoadError::MissingSave(err));
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
match ron::from_str(buf.as_str()) {
|
|
||||||
|
match postcard::from_bytes(buf.as_slice()) {
|
||||||
Ok(world) => {
|
Ok(world) => {
|
||||||
self.world = Some(world);
|
self.world = Some(world);
|
||||||
Ok(())
|
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]
|
#[must_use]
|
||||||
pub fn get_world(&self) -> Option<&World> {
|
pub fn get_world(&self) -> Option<&World> {
|
||||||
self.world.as_ref()
|
self.world.as_ref()
|
||||||
|
|
Loading…
Reference in a new issue