Ejemplo n.º 1
0
        private void UpgradeSave(string saveDir)
        {
            SaveFileVersion saveFileVersion = Serializer.Deserialize <SaveFileVersion>(Path.Combine(saveDir, $"Version{FileEnding}"));

            if (saveFileVersion.Version == NitroxEnvironment.Version)
            {
                return;
            }

            if (config.SerializerMode == ServerSerializerMode.PROTOBUF)
            {
                Log.Info("Can't upgrade while using ProtoBuf as serializer");
            }
            else
            {
                try
                {
                    foreach (SaveDataUpgrade upgrade in upgrades)
                    {
                        if (upgrade.TargetVersion > saveFileVersion.Version)
                        {
                            upgrade.UpgradeSaveFiles(saveDir, FileEnding);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex, "Error while upgrading save file.");
                    return;
                }

                Serializer.Serialize(Path.Combine(saveDir, $"Version{FileEnding}"), new SaveFileVersion());
                Log.Info($"Save file was upgraded to {NitroxEnvironment.Version}");
            }
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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();
                SaveFileVersion    saveFileVersion = Serializer.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   = Serializer.Deserialize <BaseData>(Path.Combine(saveDir, $"BaseData{FileEnding}"));
                persistedData.PlayerData = Serializer.Deserialize <PlayerData>(Path.Combine(saveDir, $"PlayerData{FileEnding}"));
                persistedData.WorldData  = Serializer.Deserialize <WorldData>(Path.Combine(saveDir, $"WorldData{FileEnding}"));
                persistedData.EntityData = Serializer.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)
            {
                Log.Error($"Could not load world, creating a new one : {ex.GetType()} {ex.Message}");

                //Backup world if loading fails
                string outZip = Path.Combine(saveDir, "worldBackup.zip");
                Log.WarnSensitive("Creating a backup at {path}", Path.GetFullPath(outZip));
                FileSystem.Instance.ZipFilesInDirectory(saveDir, outZip, $"*{FileEnding}", true);
            }

            return(Optional.Empty);
        }
Ejemplo n.º 4
0
        private void UpgradeSave(string saveDir)
        {
            SaveFileVersion saveFileVersion = Serializer.Deserialize <SaveFileVersion>(Path.Combine(saveDir, $"Version{FileEnding}"));

            // SaveFileVersion structure was updated in V1.5.0.0
            // This can be removed with V1.6.0.0 or later
            if (File.ReadAllText(Path.Combine(saveDir, $"Version{FileEnding}")).Contains("BaseDataVersion"))
            {
                saveFileVersion = new SaveFileVersion(new Version(1, 4, 0, 0));
            }

            if (saveFileVersion.Version == NitroxEnvironment.Version)
            {
                return;
            }

            if (config.SerializerMode == ServerSerializerMode.PROTOBUF)
            {
                Log.Info("Can't upgrade while using ProtoBuf as serializer");
            }
            else
            {
                try
                {
                    foreach (SaveDataUpgrade upgrade in upgrades)
                    {
                        if (upgrade.TargetVersion > saveFileVersion.Version)
                        {
                            upgrade.UpgradeSaveFiles(saveDir, FileEnding);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex, "Error while upgrading save file.");
                    return;
                }

                Serializer.Serialize(Path.Combine(saveDir, $"Version{FileEnding}"), new SaveFileVersion());
                Log.Info($"Save file was upgraded to {NitroxEnvironment.Version}");
            }
        }