internal Optional <World> LoadFromFile(string saveDir) { if (!Directory.Exists(saveDir) || !File.Exists(Path.Combine(saveDir, "Version" + fileEnding))) { Log.Warn("No previous save file found - creating a new one."); return(Optional.Empty); } try { PersistedWorldData persistedData = new PersistedWorldData(); SaveFileVersion saveFileVersion = SaveDataSerializer.Deserialize <SaveFileVersion>(Path.Combine(saveDir, "Version" + fileEnding)); if (saveFileVersion == null || saveFileVersion.Version != NitroxEnvironment.Version) { throw new InvalidDataException("Version file is empty or save data files are too old"); } persistedData.BaseData = SaveDataSerializer.Deserialize <BaseData>(Path.Combine(saveDir, "BaseData" + fileEnding)); persistedData.PlayerData = SaveDataSerializer.Deserialize <PlayerData>(Path.Combine(saveDir, "PlayerData" + fileEnding)); persistedData.WorldData = SaveDataSerializer.Deserialize <WorldData>(Path.Combine(saveDir, "WorldData" + fileEnding)); persistedData.EntityData = SaveDataSerializer.Deserialize <EntityData>(Path.Combine(saveDir, "EntityData" + fileEnding)); if (!persistedData.IsValid()) { throw new InvalidDataException("Save files are not valid"); } World world = CreateWorld(persistedData, config.GameMode); return(Optional.Of(world)); } catch (Exception ex) { //Backup world if loading fails using (ZipFile zipFile = new ZipFile()) { string[] nitroxFiles = Directory.GetFiles(saveDir, "*" + fileEnding); zipFile.AddFiles(nitroxFiles); zipFile.Save(Path.Combine(saveDir, "worldBackup.zip")); } #if DEBUG Log.Error($"Could not load world, creating a new one: {ex}"); #else Log.Warn($"Could not load world, creating a new one"); #endif } return(Optional.Empty); }
private Optional <World> LoadFromFile() { try { if (!Directory.Exists(config.SaveName)) { throw new DirectoryNotFoundException(); } PersistedWorldData persistedData = new PersistedWorldData(); using (Stream stream = File.OpenRead(Path.Combine(config.SaveName, "BaseData.nitrox"))) { SaveVersion version = serializer.Deserialize <SaveVersion>(stream); if (version.Version != BaseData.VERSION) { throw new VersionMismatchException("BaseData file is too old"); } persistedData.BaseData = serializer.Deserialize <BaseData>(stream); } using (Stream stream = File.OpenRead(Path.Combine(config.SaveName, "PlayerData.nitrox"))) { SaveVersion version = serializer.Deserialize <SaveVersion>(stream); if (version.Version != PlayerData.VERSION) { throw new VersionMismatchException("PlayerData file is too old"); } persistedData.PlayerData = serializer.Deserialize <PlayerData>(stream); } using (Stream stream = File.OpenRead(Path.Combine(config.SaveName, "WorldData.nitrox"))) { SaveVersion version = serializer.Deserialize <SaveVersion>(stream); if (version.Version != WorldData.VERSION) { throw new VersionMismatchException("WorldData file is too old"); } persistedData.WorldData = serializer.Deserialize <WorldData>(stream); } if (persistedData == null || !persistedData.IsValid()) { throw new InvalidDataException("Persisted state is not valid"); } World world = CreateWorld(persistedData.WorldData.ServerStartTime.Value, persistedData.WorldData.EntityData.Entities, persistedData.BaseData.PartiallyConstructedPieces, persistedData.BaseData.CompletedBasePieceHistory, persistedData.WorldData.VehicleData.Vehicles, persistedData.PlayerData.GetPlayers(), persistedData.WorldData.InventoryData.InventoryItems, persistedData.WorldData.InventoryData.StorageSlotItems, persistedData.WorldData.GameData, persistedData.WorldData.ParsedBatchCells, persistedData.WorldData.EscapePodData.EscapePods, persistedData.WorldData.StoryTimingData, config.GameMode); return(Optional.Of(world)); } catch (DirectoryNotFoundException) { Log.Info("No previous save file found - creating a new one."); } catch (Exception ex) { Log.Info("Could not load world: " + ex + " creating a new one."); } return(Optional.Empty); }
internal Optional <World> LoadFromFile(string saveDir) { if (!Directory.Exists(saveDir) || !File.Exists(Path.Combine(saveDir, "Version" + fileEnding))) { Log.Warn("No previous save file found - creating a new one."); return(Optional.Empty); } try { PersistedWorldData persistedData = new PersistedWorldData(); SaveFileVersions versions = saveDataSerializer.Deserialize <SaveFileVersions>(Path.Combine(saveDir, "Version" + fileEnding)); if (versions == null) { throw new InvalidDataException("Version file is empty or corrupted"); } if (versions.BaseDataVersion != BaseData.VERSION) { throw new VersionMismatchException("BaseData file is too old"); } if (versions.PlayerDataVersion != PlayerData.VERSION) { throw new VersionMismatchException("PlayerData file is too old"); } if (versions.WorldDataVersion != WorldData.VERSION) { throw new VersionMismatchException("WorldData file is too old"); } persistedData.BaseData = saveDataSerializer.Deserialize <BaseData>(Path.Combine(saveDir, "BaseData" + fileEnding)); persistedData.PlayerData = saveDataSerializer.Deserialize <PlayerData>(Path.Combine(saveDir, "PlayerData" + fileEnding)); persistedData.WorldData = saveDataSerializer.Deserialize <WorldData>(Path.Combine(saveDir, "WorldData" + fileEnding)); persistedData.EntityData = saveDataSerializer.Deserialize <EntityData>(Path.Combine(saveDir, "EntityData" + fileEnding)); if (!persistedData.IsValid()) { throw new InvalidDataException("Save files are not valid"); } World world = CreateWorld(persistedData, config.GameMode); return(Optional.Of(world)); } catch (Exception ex) { //Backup world if loading fails using (ZipFile zipFile = new ZipFile()) { zipFile.AddFile(Path.Combine(saveDir, "Version" + fileEnding)); zipFile.AddFile(Path.Combine(saveDir, "BaseData" + fileEnding)); zipFile.AddFile(Path.Combine(saveDir, "PlayerData" + fileEnding)); zipFile.AddFile(Path.Combine(saveDir, "WorldData" + fileEnding)); zipFile.Save(Path.Combine(saveDir, "worldBackup.zip")); } #if DEBUG Log.Error($"Could not load world, creating a new one: {ex}"); #else Log.Warn($"Could not load world, creating a new one"); #endif } return(Optional.Empty); }
private Optional <World> LoadFromFile(string saveDir) { if (!Directory.Exists(saveDir) || !File.Exists(Path.Combine(saveDir, "Version" + fileEnding))) { Log.Warn("No previous save file found - creating a new one."); return(Optional.Empty); } try { PersistedWorldData persistedData = new PersistedWorldData(); SaveFileVersions versions = saveDataSerializer.Deserialize <SaveFileVersions>(Path.Combine(saveDir, "Version" + fileEnding)); if (versions == null) { throw new InvalidDataException("Version file is empty or corrupted"); } if (versions.BaseDataVersion != BaseData.VERSION) { throw new VersionMismatchException("BaseData file is too old"); } if (versions.PlayerDataVersion != PlayerData.VERSION) { throw new VersionMismatchException("PlayerData file is too old"); } if (versions.WorldDataVersion != WorldData.VERSION) { throw new VersionMismatchException("WorldData file is too old"); } persistedData.BaseData = saveDataSerializer.Deserialize <BaseData>(Path.Combine(saveDir, "BaseData" + fileEnding)); persistedData.PlayerData = saveDataSerializer.Deserialize <PlayerData>(Path.Combine(saveDir, "PlayerData" + fileEnding)); persistedData.WorldData = saveDataSerializer.Deserialize <WorldData>(Path.Combine(saveDir, "WorldData" + fileEnding)); if (!persistedData.IsValid()) { throw new InvalidDataException("Save files are not valid"); } World world = CreateWorld(persistedData.WorldData.ServerStartTime.Value, persistedData.WorldData.EntityData.Entities, persistedData.BaseData.PartiallyConstructedPieces, persistedData.BaseData.CompletedBasePieceHistory, persistedData.WorldData.VehicleData.Vehicles, persistedData.PlayerData.GetPlayers(), persistedData.WorldData.InventoryData.InventoryItems, persistedData.WorldData.InventoryData.StorageSlotItems, persistedData.WorldData.GameData, persistedData.WorldData.ParsedBatchCells, persistedData.WorldData.EscapePodData.EscapePods, persistedData.WorldData.StoryTimingData, config.GameModeEnum); return(Optional.Of(world)); } catch (Exception ex) { //Backup world if loading fails using (ZipFile zipFile = new ZipFile()) { zipFile.AddFile(Path.Combine(saveDir, "Version" + fileEnding)); zipFile.AddFile(Path.Combine(saveDir, "BaseData" + fileEnding)); zipFile.AddFile(Path.Combine(saveDir, "PlayerData" + fileEnding)); zipFile.AddFile(Path.Combine(saveDir, "WorldData" + fileEnding)); zipFile.Save(Path.Combine(saveDir, "worldBackup.zip")); } Log.Error($"Could not load world, creating a new one: {ex}"); } return(Optional.Empty); }