nil_core/world/
savedata.rs1use crate::error::{Error, Result};
5use crate::event::Emitter;
6use crate::savedata::Savedata;
7use crate::world::World;
8use std::sync::Arc;
9
10impl World {
11 pub(super) fn consume_pending_save(&mut self) -> Result<()> {
12 if let Some(handle) = self.save_handle.take() {
13 handle.save(self.to_bytes()?);
14 }
15
16 Ok(())
17 }
18
19 pub fn to_bytes(&self) -> Result<Vec<u8>> {
20 let mut buffer = Vec::new();
21 let data = Savedata::from(self);
22 data.write(&mut buffer)?;
23 Ok(buffer)
24 }
25}
26
27impl From<&World> for Savedata {
28 fn from(world: &World) -> Self {
29 Self {
30 round: world.round.clone(),
31 continent: world.continent.clone(),
32 player_manager: world.player_manager.clone(),
33 bot_manager: world.bot_manager.clone(),
34 precursor_manager: world.precursor_manager.clone(),
35 military: world.military.clone(),
36 ranking: world.ranking.clone(),
37 report: world.report.clone(),
38 config: Arc::unwrap_or_clone(world.config()),
39 stats: world.stats.clone(),
40 chat: world.chat.clone(),
41 }
42 }
43}
44
45impl From<Savedata> for World {
46 fn from(savedata: Savedata) -> Self {
47 Self {
48 round: savedata.round,
49 continent: savedata.continent,
50 player_manager: savedata.player_manager,
51 bot_manager: savedata.bot_manager,
52 precursor_manager: savedata.precursor_manager,
53 military: savedata.military,
54 ranking: savedata.ranking,
55 report: savedata.report,
56 config: Arc::new(savedata.config),
57 stats: savedata.stats,
58 chat: savedata.chat,
59
60 emitter: Emitter::default(),
61 save_handle: None,
62 on_next_round: None,
63 }
64 }
65}
66
67impl TryFrom<&World> for Vec<u8> {
68 type Error = Error;
69
70 fn try_from(world: &World) -> Result<Self> {
71 world.to_bytes()
72 }
73}