Пример #1
0
        private void SaveAll(PersistedWorldData pwd)
        {
            try
            {
                if (!Directory.Exists("saves"))
                {
                    Directory.CreateDirectory("saves");
                }

                _backUpInterval++;
                bool isBackup = false;
                if (_backUpInterval == config.BackUpInterval && File.Exists($"saves{Path.DirectorySeparatorChar}{config.SaveName}.nitrox"))
                {
                    File.Copy($"saves{Path.DirectorySeparatorChar}{config.SaveName}.nitrox", $"saves{Path.DirectorySeparatorChar}{config.SaveName}-{DateTime.Now.ToString("dd-MM-yyyy_HH-mm-ss")}.nitrox");
                    _backUpInterval = 0;
                    isBackup        = true;
                }

                using (Stream stream = File.OpenWrite($"saves{Path.DirectorySeparatorChar}{config.SaveName}.nitrox"))
                {
                    serializer.Serialize(stream, pwd);
                }

                if (isBackup)
                {
                    RemoveOldBackUps();
                }
            }
            catch (Exception ex)
            {
                Log.Info("Could not save world: " + ex);
            }
        }
Пример #2
0
        public void Save(World world)
        {
            Log.Info("Saving world state.");

            try
            {
                PersistedWorldData persistedData = new PersistedWorldData();
                persistedData.ParsedBatchCells = world.BatchEntitySpawner.SerializableParsedBatches;
                persistedData.ServerStartTime  = world.TimeKeeper.ServerStartTime;
                persistedData.EntityData       = world.EntityData;
                persistedData.BaseData         = world.BaseData;
                persistedData.VehicleData      = world.VehicleData;
                persistedData.InventoryData    = world.InventoryData;
                persistedData.PlayerData       = world.PlayerData;
                persistedData.GameData         = world.GameData;
                persistedData.EscapePodData    = world.EscapePodData;

                using (Stream stream = File.OpenWrite(fileName))
                {
                    serializer.Serialize(stream, persistedData);
                }

                Log.Info("World state saved.");
            }
            catch (Exception ex)
            {
                Log.Info("Could not save world: " + ex);
            }
        }
Пример #3
0
        public bool Save(World world, string saveDir)
        {
            try
            {
                PersistedWorldData persistedData = PersistedWorldData.From(world);

                if (!Directory.Exists(saveDir))
                {
                    Directory.CreateDirectory(saveDir);
                }

                SaveDataSerializer.Serialize(Path.Combine(saveDir, "Version" + fileEnding), new SaveFileVersion());
                SaveDataSerializer.Serialize(Path.Combine(saveDir, "BaseData" + fileEnding), persistedData.BaseData);
                SaveDataSerializer.Serialize(Path.Combine(saveDir, "PlayerData" + fileEnding), persistedData.PlayerData);
                SaveDataSerializer.Serialize(Path.Combine(saveDir, "WorldData" + fileEnding), persistedData.WorldData);
                SaveDataSerializer.Serialize(Path.Combine(saveDir, "EntityData" + fileEnding), persistedData.EntityData);

                Log.Info("World state saved.");
                return(true);
            }
            catch (Exception ex)
            {
                Log.Error("Could not save world: " + ex);
                return(false);
            }
        }
Пример #4
0
        private World CreateFreshWorld()
        {
            PersistedWorldData pWorldData = new PersistedWorldData
            {
                BaseData   = BaseData.From(new List <BasePiece>(), new List <BasePiece>()),
                EntityData = EntityData.From(new List <Entity>()),
                PlayerData = PlayerData.From(new List <Player>()),
                WorldData  = new WorldData()
                {
                    EscapePodData = EscapePodData.From(new List <EscapePodModel>()),
                    GameData      = new GameData
                    {
                        PDAState    = new PDAStateData(),
                        StoryGoals  = new StoryGoalData(),
                        StoryTiming = new StoryTimingData()
                    },
                    InventoryData    = InventoryData.From(new List <ItemData>(), new List <ItemData>(), new List <EquippedItemData>()),
                    VehicleData      = VehicleData.From(new List <VehicleModel>()),
                    ParsedBatchCells = new List <NitroxInt3>(),
                    ServerStartTime  = DateTime.Now
#if DEBUG
                    ,
                    Seed = "TCCBIBZXAB"
#endif
                }
            };

            return(CreateWorld(pWorldData, config.GameMode));
        }
Пример #5
0
        public World CreateWorld(PersistedWorldData pWorldData, ServerGameMode gameMode)
        {
            string seed = pWorldData.WorldData.Seed;

            if (string.IsNullOrWhiteSpace(seed))
            {
                seed = StringHelper.GenerateRandomString(10);
            }

            Log.Info($"Loading world with seed {seed}");

            World world = new()
            {
                TimeKeeper = new TimeKeeper {
                    ServerStartTime = pWorldData.WorldData.ServerStartTime
                },

                SimulationOwnershipData = new SimulationOwnershipData(),
                PlayerManager           = new PlayerManager(pWorldData.PlayerData.GetPlayers(), config),

                BaseManager = new BaseManager(pWorldData.BaseData.PartiallyConstructedPieces, pWorldData.BaseData.CompletedBasePieceHistory),

                InventoryManager = new InventoryManager(pWorldData.WorldData.InventoryData.InventoryItems, pWorldData.WorldData.InventoryData.StorageSlotItems, pWorldData.WorldData.InventoryData.Modules),

                EscapePodManager = new EscapePodManager(pWorldData.WorldData.EscapePodData.EscapePods, randomStart, seed),

                GameData = pWorldData.WorldData.GameData,
                GameMode = gameMode,
                Seed     = seed
            };

            world.EventTriggerer = new EventTriggerer(world.PlayerManager, pWorldData.WorldData.GameData.StoryTiming.ElapsedTime, pWorldData.WorldData.GameData.StoryTiming.AuroraExplosionTime);
            world.VehicleManager = new VehicleManager(pWorldData.WorldData.VehicleData.Vehicles, world.InventoryManager);

            world.BatchEntitySpawner = new BatchEntitySpawner(
                NitroxServiceLocator.LocateService <EntitySpawnPointFactory>(),
                NitroxServiceLocator.LocateService <UweWorldEntityFactory>(),
                NitroxServiceLocator.LocateService <UwePrefabFactory>(),
                pWorldData.WorldData.ParsedBatchCells,
                protoBufSerializer,
                NitroxServiceLocator.LocateService <Dictionary <NitroxTechType, IEntityBootstrapper> >(),
                NitroxServiceLocator.LocateService <Dictionary <string, PrefabPlaceholdersGroupAsset> >(),
                world.Seed
                );

            world.EntityManager = new EntityManager(pWorldData.EntityData.Entities, world.BatchEntitySpawner);

            HashSet <NitroxTechType> serverSpawnedSimulationWhiteList = NitroxServiceLocator.LocateService <HashSet <NitroxTechType> >();

            world.EntitySimulation = new EntitySimulation(world.EntityManager, world.SimulationOwnershipData, world.PlayerManager, serverSpawnedSimulationWhiteList);

            return(world);
        }
    }
Пример #6
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();
                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);
        }
Пример #7
0
        public void Save(World world)
        {
            try
            {
                PersistedWorldData persistedData = new PersistedWorldData();
                persistedData.WorldData.ParsedBatchCells = world.BatchEntitySpawner.SerializableParsedBatches;
                persistedData.WorldData.ServerStartTime  = world.TimeKeeper.ServerStartTime;
                persistedData.WorldData.EntityData       = EntityData.From(world.EntityManager.GetAllEntities());
                persistedData.BaseData = BaseData.From(world.BaseManager.GetPartiallyConstructedPieces(), world.BaseManager.GetCompletedBasePieceHistory());
                persistedData.WorldData.VehicleData   = VehicleData.From(world.VehicleManager.GetVehicles());
                persistedData.WorldData.InventoryData = InventoryData.From(world.InventoryManager.GetAllInventoryItems(), world.InventoryManager.GetAllStorageSlotItems());
                persistedData.PlayerData                = PlayerData.From(world.PlayerManager.GetAllPlayers());
                persistedData.WorldData.GameData        = world.GameData;
                persistedData.WorldData.StoryTimingData = StoryTimingData.From(world.EventTriggerer);
                persistedData.WorldData.EscapePodData   = EscapePodData.From(world.EscapePodManager.GetEscapePods());

                if (!Directory.Exists(config.SaveName))
                {
                    Directory.CreateDirectory(config.SaveName);
                }

                using (Stream stream = File.OpenWrite(Path.Combine(config.SaveName, "BaseData.nitrox")))
                {
                    serializer.Serialize(stream, new SaveVersion(BaseData.VERSION));
                    serializer.Serialize(stream, persistedData.BaseData);
                }

                using (Stream stream = File.OpenWrite(Path.Combine(config.SaveName, "PlayerData.nitrox")))
                {
                    serializer.Serialize(stream, new SaveVersion(PlayerData.VERSION));
                    serializer.Serialize(stream, persistedData.PlayerData);
                }

                using (Stream stream = File.OpenWrite(Path.Combine(config.SaveName, "WorldData.nitrox")))
                {
                    serializer.Serialize(stream, new SaveVersion(WorldData.VERSION));
                    serializer.Serialize(stream, persistedData.WorldData);
                }

                Log.Info("World state saved.");
            }
            catch (Exception ex)
            {
                Log.Info("Could not save world: " + ex);
            }
        }
Пример #8
0
        public void Save(World world)
        {
            try
            {
                PersistedWorldData persistedData = new PersistedWorldData();
                persistedData.WorldData.ParsedBatchCells = world.BatchEntitySpawner.SerializableParsedBatches;
                persistedData.WorldData.ServerStartTime  = world.TimeKeeper.ServerStartTime;
                persistedData.WorldData.EntityData       = world.EntityData;
                persistedData.BaseData = world.BaseData;
                persistedData.WorldData.VehicleData   = world.VehicleData;
                persistedData.WorldData.InventoryData = world.InventoryData;
                persistedData.PlayerData              = world.PlayerData;
                persistedData.WorldData.GameData      = world.GameData;
                persistedData.WorldData.EscapePodData = world.EscapePodData;

                if (!Directory.Exists(config.SaveName))
                {
                    Directory.CreateDirectory(config.SaveName);
                }

                using (Stream stream = File.OpenWrite(Path.Combine(config.SaveName, "BaseData.nitrox")))
                {
                    serializer.Serialize(stream, new SaveVersion(BaseData.VERSION));
                    serializer.Serialize(stream, persistedData.BaseData);
                }

                using (Stream stream = File.OpenWrite(Path.Combine(config.SaveName, "PlayerData.nitrox")))
                {
                    serializer.Serialize(stream, new SaveVersion(PlayerData.VERSION));
                    serializer.Serialize(stream, persistedData.PlayerData);
                }

                using (Stream stream = File.OpenWrite(Path.Combine(config.SaveName, "WorldData.nitrox")))
                {
                    serializer.Serialize(stream, new SaveVersion(WorldData.VERSION));
                    serializer.Serialize(stream, persistedData.WorldData);
                }

                Log.Info("World state saved.");
            }
            catch (Exception ex)
            {
                Log.Info("Could not save world: " + ex);
            }
        }
Пример #9
0
        public void Save(World world, string saveDir)
        {
            try
            {
                PersistedWorldData persistedData = new PersistedWorldData
                {
                    BaseData   = BaseData.From(world.BaseManager.GetPartiallyConstructedPieces(), world.BaseManager.GetCompletedBasePieceHistory()),
                    PlayerData = PlayerData.From(world.PlayerManager.GetAllPlayers()),
                    EntityData = EntityData.From(world.EntityManager.GetAllEntities()),
                    WorldData  =
                    {
                        ParsedBatchCells = world.BatchEntitySpawner.SerializableParsedBatches,
                        ServerStartTime  = world.TimeKeeper.ServerStartTime,
                        VehicleData      = VehicleData.From(world.VehicleManager.GetVehicles()),
                        InventoryData    = InventoryData.From(world.InventoryManager.GetAllInventoryItems(), world.InventoryManager.GetAllStorageSlotItems()),
                        GameData         = world.GameData,
                        StoryTimingData  = StoryTimingData.From(world.EventTriggerer),
                        EscapePodData    = EscapePodData.From(world.EscapePodManager.GetEscapePods())
                    }
                };

                if (!Directory.Exists(saveDir))
                {
                    Directory.CreateDirectory(saveDir);
                }

                saveDataSerializer.Serialize(Path.Combine(saveDir, "Version" + fileEnding), new SaveFileVersions());
                saveDataSerializer.Serialize(Path.Combine(saveDir, "BaseData" + fileEnding), persistedData.BaseData);
                saveDataSerializer.Serialize(Path.Combine(saveDir, "PlayerData" + fileEnding), persistedData.PlayerData);
                saveDataSerializer.Serialize(Path.Combine(saveDir, "WorldData" + fileEnding), persistedData.WorldData);
                saveDataSerializer.Serialize(Path.Combine(saveDir, "EntityData" + fileEnding), persistedData.EntityData);

                Log.Info("World state saved.");
            }
            catch (Exception ex)
            {
                Log.Info("Could not save world: " + ex);
            }
        }
Пример #10
0
        public void Save(World world)
        {
            try
            {
                PersistedWorldData persistedData = new PersistedWorldData();
                persistedData.ParsedBatchCells = world.BatchEntitySpawner.SerializableParsedBatches;
                persistedData.ServerStartTime  = world.TimeKeeper.ServerStartTime;
                persistedData.EntityData       = world.EntityData;
                persistedData.BaseData         = world.BaseData;
                persistedData.VehicleData      = world.VehicleData;
                persistedData.InventoryData    = world.InventoryData;
                persistedData.PlayerData       = world.PlayerData;
                persistedData.GameData         = world.GameData;
                persistedData.EscapePodData    = world.EscapePodData;

                Task t = new Task(() => SaveAll(persistedData));
                t.RunSynchronously();
                //Log.Info("World state saved.");
            }
            catch (Exception ex)
            {
                Log.Info("Could not save world: " + ex);
            }
        }
Пример #11
0
        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);
        }
Пример #12
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);
        }
Пример #13
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);
        }