roguelike-game/src/saveload_system.rs

319 lines
8.5 KiB
Rust

use std::fs::{self, File};
use std::path::Path;
use ::rltk::Point;
use ::specs::error::NoError;
use ::specs::prelude::*;
use ::specs::saveload::{
DeserializeComponents, MarkedBuilder, SerializeComponents, SimpleMarker, SimpleMarkerAllocator,
};
use crate::components::*;
use crate::map::{Map, MasterDungeonMap};
use crate::spatial;
macro_rules! serialize_individually {
($ecs:expr, $ser:expr, $data:expr, $( $type:ty),*,) => {
$(
SerializeComponents::<NoError, SimpleMarker<SerializeMe>>::serialize(
&( $ecs.read_storage::<$type>(), ),
&$data.0,
&$data.1,
&mut $ser,
)
.unwrap();
)*
};
}
#[cfg(target_arch = "wasm32")]
pub fn save_game(_ecs: &mut World) {}
#[cfg(not(target_arch = "wasm32"))]
pub fn save_game(ecs: &mut World) {
// Create helper
let mapcopy = ecs.get_mut::<Map>().unwrap().clone();
let dungeon_master = ecs.get_mut::<MasterDungeonMap>().unwrap().clone();
let savehelper = ecs
.create_entity()
.with(SerializationHelper { map: mapcopy })
.marked::<SimpleMarker<SerializeMe>>()
.build();
let savehelper2 = ecs
.create_entity()
.with(DMSerializationHelper {
map: dungeon_master,
})
.marked::<SimpleMarker<SerializeMe>>()
.build();
// 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,
AlwaysTargetsSelf,
ApplyMove,
ApplyTeleport,
AreaOfEffect,
AttributeBonus,
Attributes,
BlocksTile,
BlocksVisibility,
Chasing,
Confusion,
Consumable,
CursedItem,
DamageOverTime,
Door,
DMSerializationHelper,
Duration,
EntityMoved,
EntryTrigger,
Equippable,
Equipped,
EquipmentChanged,
Faction,
Hidden,
HungerClock,
IdentifiedItem,
InBackpack,
InflictsDamage,
Initiative,
Item,
KnownSpells,
LightSource,
LootTable,
MagicItem,
MagicMapper,
MeleeWeapon,
MoveMode,
MyTurn,
Name,
NaturalAttackDefense,
ObfuscatedName,
OnDeath,
OtherLevelPosition,
ParticleLifetime,
Player,
Pools,
Position,
ProvidesFood,
ProvidesHealing,
ProvidesIdentification,
ProvidesMana,
ProvidesRemoveCurse,
Quips,
Ranged,
Renderable,
SerializationHelper,
SingleActivation,
Skills,
Slow,
SpawnParticleBurst,
SpawnParticleLine,
SpecialAbilities,
SpellTemplate,
StatusEffect,
TeachesSpell,
TeleportTo,
TileSize,
TownPortal,
Vendor,
Viewshed,
WantsToApproach,
WantsToCastSpell,
WantsToDropItem,
WantsToFlee,
WantsToMelee,
WantsToPickupItem,
WantsToRemoveItem,
WantsToUseItem,
Wearable,
);
}
// Clean up
ecs.delete_entity(savehelper)
.expect("Failed to clean up savehelper component");
ecs.delete_entity(savehelper2)
.expect("Failed to clean up savehelper2 component");
}
pub fn does_save_exist() -> bool {
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>(), ),
&$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("Failed to delete entity");
}
}
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 ecs.write_resource::<SimpleMarkerAllocator<SerializeMe>>(),
);
deserialize_individually!(
ecs,
de,
d,
AlwaysTargetsSelf,
ApplyMove,
ApplyTeleport,
AreaOfEffect,
AttributeBonus,
Attributes,
BlocksTile,
BlocksVisibility,
Chasing,
Confusion,
Consumable,
CursedItem,
DamageOverTime,
Door,
DMSerializationHelper,
Duration,
EntityMoved,
EntryTrigger,
Equippable,
Equipped,
EquipmentChanged,
Faction,
Hidden,
HungerClock,
IdentifiedItem,
InBackpack,
InflictsDamage,
Initiative,
Item,
KnownSpells,
LightSource,
LootTable,
MagicItem,
MagicMapper,
MeleeWeapon,
MoveMode,
MyTurn,
Name,
NaturalAttackDefense,
ObfuscatedName,
OnDeath,
OtherLevelPosition,
ParticleLifetime,
Player,
Pools,
Position,
ProvidesFood,
ProvidesHealing,
ProvidesIdentification,
ProvidesMana,
ProvidesRemoveCurse,
Quips,
Ranged,
Renderable,
SerializationHelper,
SingleActivation,
Slow,
Skills,
SpawnParticleBurst,
SpawnParticleLine,
SpecialAbilities,
SpellTemplate,
StatusEffect,
TeachesSpell,
TeleportTo,
TileSize,
TownPortal,
Vendor,
Viewshed,
WantsToApproach,
WantsToCastSpell,
WantsToDropItem,
WantsToFlee,
WantsToMelee,
WantsToPickupItem,
WantsToRemoveItem,
WantsToUseItem,
Wearable,
);
}
let mut deleteme: Option<Entity> = None;
let mut deleteme2: Option<Entity> = None;
{
let entities = ecs.entities();
let helper = ecs.read_storage::<SerializationHelper>();
let helper2 = ecs.read_storage::<DMSerializationHelper>();
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::<Map>();
*worldmap = h.map.clone();
spatial::set_size((worldmap.height * worldmap.width) as usize);
deleteme = Some(e);
}
for (e, h) in (&entities, &helper2).join() {
let mut dungeon_master = ecs.write_resource::<MasterDungeonMap>();
*dungeon_master = h.map.clone();
deleteme2 = Some(e);
}
for (e, _p, pos) in (&entities, &player, &position).join() {
let mut ppos = ecs.write_resource::<Point>();
*ppos = Point::from(*pos);
let mut player_resource = ecs.write_resource::<Entity>();
*player_resource = e;
}
}
ecs.delete_entity(deleteme.unwrap())
.expect("Unable to delete helper entity");
ecs.delete_entity(deleteme2.unwrap())
.expect("Unable to delete helper2 entity");
}
pub fn delete_save() {
if Path::new("./savegame.json").exists() {
std::fs::remove_file("./savegame.json").expect("Failed to delete save file.");
}
}