Browse Source

Serialization is working.

Herbert Wolverson 5 years ago
parent
commit
f23e39efc3

+ 6 - 0
chapter-11-loadsave/src/components.rs

@@ -123,6 +123,12 @@ pub struct WantsToDropItem {
 
 
 pub struct SerializeMe;
 pub struct SerializeMe;
 
 
+// Special component that exists to help serialize the game data
+#[derive(Component, Serialize, Deserialize, Clone)]
+pub struct SerializationHelper {
+    pub map : super::map::Map
+}
+
 // WantsToMelee wrapper
 // WantsToMelee wrapper
 #[derive(Serialize, Deserialize, Clone)]
 #[derive(Serialize, Deserialize, Clone)]
 pub struct WantsToMeleeData<M>(M);
 pub struct WantsToMeleeData<M>(M);

+ 5 - 6
chapter-11-loadsave/src/main.rs

@@ -150,7 +150,10 @@ impl GameState for State {
                     gui::MainMenuResult::Selected{ selected } => {
                     gui::MainMenuResult::Selected{ selected } => {
                         match selected {
                         match selected {
                             gui::MainMenuSelection::NewGame => newrunstate = RunState::PreRun,
                             gui::MainMenuSelection::NewGame => newrunstate = RunState::PreRun,
-                            gui::MainMenuSelection::LoadGame => newrunstate = RunState::PreRun,
+                            gui::MainMenuSelection::LoadGame => {
+                                saveload_system::load_game(&mut self.ecs);
+                                newrunstate = RunState::AwaitingInput;
+                            }
                             gui::MainMenuSelection::Quit => { ::std::process::exit(0); }
                             gui::MainMenuSelection::Quit => { ::std::process::exit(0); }
                         }
                         }
                     }
                     }
@@ -158,11 +161,6 @@ impl GameState for State {
             }
             }
             RunState::SaveGame => {
             RunState::SaveGame => {
                 saveload_system::save_game(&mut self.ecs);
                 saveload_system::save_game(&mut self.ecs);
-
-                //let data = serde_json::to_string(&*self.ecs.fetch::<Map>()).unwrap();
-                //let mut f = File::create("./savemap.json").expect("Unable to create file");
-                //f.write_all(data.as_bytes()).expect("Unable to write data");
-
                 newrunstate = RunState::MainMenu{ menu_selection : gui::MainMenuSelection::LoadGame };
                 newrunstate = RunState::MainMenu{ menu_selection : gui::MainMenuSelection::LoadGame };
             }
             }
         }
         }
@@ -213,6 +211,7 @@ fn main() {
     gs.ecs.register::<WantsToDropItem>();
     gs.ecs.register::<WantsToDropItem>();
     gs.ecs.register::<Confusion>();
     gs.ecs.register::<Confusion>();
     gs.ecs.register::<SimpleMarker<SerializeMe>>();
     gs.ecs.register::<SimpleMarker<SerializeMe>>();
+    gs.ecs.register::<SerializationHelper>();
 
 
     gs.ecs.insert(SimpleMarkerAllocator::<SerializeMe>::new());
     gs.ecs.insert(SimpleMarkerAllocator::<SerializeMe>::new());
 
 

+ 1 - 1
chapter-11-loadsave/src/map.rs

@@ -15,7 +15,7 @@ pub enum TileType {
     Wall, Floor
     Wall, Floor
 }
 }
 
 
-#[derive(Default, Serialize, Deserialize)]
+#[derive(Default, Serialize, Deserialize, Clone)]
 pub struct Map {
 pub struct Map {
     pub tiles : Vec<TileType>,
     pub tiles : Vec<TileType>,
     pub rooms : Vec<Rect>,
     pub rooms : Vec<Rect>,

+ 90 - 12
chapter-11-loadsave/src/saveload_system.rs

@@ -1,10 +1,10 @@
 use specs::prelude::*;
 use specs::prelude::*;
-use specs::saveload::{SimpleMarker, SerializeComponents};
+use specs::saveload::{SimpleMarker, SimpleMarkerAllocator, SerializeComponents, DeserializeComponents, MarkedBuilder};
 use specs::error::NoError;
 use specs::error::NoError;
 use super::components::*;
 use super::components::*;
 use std::fs::File;
 use std::fs::File;
-use std::io::Write;
 use std::path::Path;
 use std::path::Path;
+use std::fs;
 
 
 macro_rules! serialize_individually {
 macro_rules! serialize_individually {
     ($ecs:expr, $ser:expr, $data:expr, $( $type:ty),*) => {
     ($ecs:expr, $ser:expr, $data:expr, $( $type:ty),*) => {
@@ -20,18 +20,96 @@ macro_rules! serialize_individually {
     };
     };
 }
 }
 
 
-pub fn save_game(ecs : &World) {
-    let data = ( ecs.entities(), ecs.read_storage::<SimpleMarker<SerializeMe>>() );
+pub fn save_game(ecs : &mut World) {
+    // Create helper
+    let mapcopy = ecs.get_mut::<super::map::Map>().unwrap().clone();
+    let savehelper = ecs
+        .create_entity()
+        .with(SerializationHelper{ map : mapcopy })
+        .marked::<SimpleMarker<SerializeMe>>()
+        .build();
 
 
-    let writer = File::create("./savegame.json").unwrap();
-    let mut serializer = serde_json::Serializer::new(writer);
-    serialize_individually!(ecs, serializer, data, Position, Renderable, Player, Viewshed, Monster, 
-        Name, BlocksTile, CombatStats, SufferDamage, WantsToMelee, Item, Consumable, Ranged, InflictsDamage, 
-        AreaOfEffect, Confusion, ProvidesHealing, InBackpack, WantsToPickupItem, WantsToUseItem,
-        WantsToDropItem
-    );
+    // Actually serialize
+    {
+        let data = ( ecs.entities(), ecs.read_storage::<SimpleMarker<SerializeMe>>() );
+
+        let writer = File::create("./savegame.json").unwrap();
+        let mut serializer = serde_json::Serializer::new(writer);
+        serialize_individually!(ecs, serializer, data, Position, Renderable, Player, Viewshed, Monster, 
+            Name, BlocksTile, CombatStats, SufferDamage, WantsToMelee, Item, Consumable, Ranged, InflictsDamage, 
+            AreaOfEffect, Confusion, ProvidesHealing, InBackpack, WantsToPickupItem, WantsToUseItem,
+            WantsToDropItem, SerializationHelper
+        );
+    }
+
+    // Clean up
+    ecs.delete_entity(savehelper).expect("Crash on cleanup");
 }
 }
 
 
 pub fn does_save_exist() -> bool {
 pub fn does_save_exist() -> bool {
     Path::new("./savegame.json").exists()
     Path::new("./savegame.json").exists()
-}
+}
+
+macro_rules! deserialize_individually {
+    ($ecs:expr, $de:expr, $data:expr, $( $type:ty),*) => {
+        $(
+        DeserializeComponents::<NoError, _>::deserialize(
+            &mut ( &mut $ecs.write_storage::<$type>(), ),
+            &mut $data.0, // entities
+            &mut $data.1, // marker
+            &mut $data.2, // allocater
+            &mut $de,
+        )
+        .unwrap();
+        )*
+    };
+}
+
+pub fn load_game(ecs: &mut World) {
+    {
+        // Delete everything
+        let mut to_delete = Vec::new();
+        for e in ecs.entities().join() {
+            to_delete.push(e);
+        }
+        for del in to_delete.iter() {
+            ecs.delete_entity(*del).expect("Deletion failed");
+        }
+    }
+
+    let data = fs::read_to_string("./savegame.json").unwrap();
+    let mut de = serde_json::Deserializer::from_str(&data);
+
+    {
+        let mut d = (&mut ecs.entities(), &mut ecs.write_storage::<SimpleMarker<SerializeMe>>(), &mut SimpleMarkerAllocator::<SerializeMe>::new());
+
+        deserialize_individually!(ecs, de, d, Position, Renderable, Player, Viewshed, Monster, 
+            Name, BlocksTile, CombatStats, SufferDamage, WantsToMelee, Item, Consumable, Ranged, InflictsDamage, 
+            AreaOfEffect, Confusion, ProvidesHealing, InBackpack, WantsToPickupItem, WantsToUseItem,
+            WantsToDropItem, SerializationHelper
+        );
+    }
+
+    let mut deleteme : Option<Entity> = None;
+    {
+        let entities = ecs.entities();
+        let helper = ecs.read_storage::<SerializationHelper>();
+        let player = ecs.read_storage::<Player>();
+        let position = ecs.read_storage::<Position>();
+        for (e,h) in (&entities, &helper).join() {
+            let mut worldmap = ecs.write_resource::<super::map::Map>();
+            *worldmap = h.map.clone();
+            worldmap.tile_content = vec![Vec::new(); super::map::MAPCOUNT];
+            deleteme = Some(e);
+        }
+        for (e,_p,pos) in (&entities, &player, &position).join() {
+            let mut ppos = ecs.write_resource::<rltk::Point>();
+            *ppos = rltk::Point::new(pos.x, pos.y);
+            let mut player_resource = ecs.write_resource::<Entity>();
+            *player_resource = e;
+        }
+    }
+    ecs.delete_entity(deleteme.unwrap()).expect("Unable to delete helper");
+
+
+}