Example #1
0
        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);
        }
Example #2
0
        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);
        }