Esempio n. 1
0
        /// <summary>
        /// Load the mod save data. Loads the save data from {UserIO.GetSavePath("Saves")}/{SaveData.GetFilename(index)}-modsave-{Metadata.Name}.celeste by default.
        /// </summary>
        public virtual void LoadSaveData(int index)
        {
            if (SaveDataType == null)
            {
                return;
            }

            _SaveData       = (EverestModuleSaveData)SaveDataType.GetConstructor(Everest._EmptyTypeArray).Invoke(Everest._EmptyObjectArray);
            _SaveData.Index = index;

            string path = patch_UserIO.GetSaveFilePath(patch_SaveData.GetFilename(index) + "-modsave-" + Metadata.Name);

            if (!File.Exists(path))
            {
                return;
            }

            try {
                using (Stream stream = File.OpenRead(path)) {
                    if (_SaveData is EverestModuleBinarySaveData)
                    {
                        using (BinaryReader reader = new BinaryReader(stream))
                            ((EverestModuleBinarySaveData)_SaveData).Read(reader);
                    }
                    else
                    {
                        using (StreamReader reader = new StreamReader(stream))
                            YamlHelper.DeserializerUsing(_SaveData).Deserialize(reader, SaveDataType);
                    }
                }
                _SaveData.Index = index;
            } catch {
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Load the mod settings. Loads the settings from {UserIO.GetSavePath("Saves")}/modsettings-{Metadata.Name}.celeste by default.
        /// </summary>
        public virtual void LoadSettings()
        {
            if (SettingsType == null)
            {
                return;
            }

            _Settings = (EverestModuleSettings)SettingsType.GetConstructor(Everest._EmptyTypeArray).Invoke(Everest._EmptyObjectArray);

            string path = patch_UserIO.GetSaveFilePath("modsettings-" + Metadata.Name);

            // Temporary fallback to help migrate settings from their old location.
            if (!File.Exists(path))
            {
                path = Path.Combine(Everest.PathEverest, "ModSettings-OBSOLETE", Metadata.Name + ".yaml");
            }

            if (!File.Exists(path))
            {
                return;
            }

            try {
                using (Stream stream = File.OpenRead(path)) {
                    if (_Settings is EverestModuleBinarySettings)
                    {
                        using (BinaryReader reader = new BinaryReader(stream))
                            ((EverestModuleBinarySettings)_Settings).Read(reader);
                    }
                    else
                    {
                        using (StreamReader reader = new StreamReader(stream))
                            YamlHelper.DeserializerUsing(_Settings).Deserialize(reader, SettingsType);
                    }
                }
            } catch (Exception e) {
                Logger.Log(LogLevel.Warn, "EverestModule", $"Failed to load the settings of {Metadata.Name}!");
                Logger.LogDetailed(e);
            }

            if (_Settings == null)
            {
                _Settings = (EverestModuleSettings)SettingsType.GetConstructor(Everest._EmptyTypeArray).Invoke(Everest._EmptyObjectArray);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Load the mod session. Loads the session from {UserIO.GetSavePath("Saves")}/{SaveData.GetFilename(index)}-modsession-{Metadata.Name}.celeste by default.
        /// </summary>
        public virtual void LoadSession(int index, bool forceNew)
        {
            if (SessionType == null)
            {
                return;
            }

            _Session       = (EverestModuleSession)SessionType.GetConstructor(Everest._EmptyTypeArray).Invoke(Everest._EmptyObjectArray);
            _Session.Index = index;

            if (forceNew)
            {
                return;
            }

            string path = patch_UserIO.GetSaveFilePath(patch_SaveData.GetFilename(index) + "-modsession-" + Metadata.Name);

            if (!File.Exists(path))
            {
                return;
            }

            try {
                using (Stream stream = File.OpenRead(path)) {
                    if (_Session is EverestModuleBinarySession)
                    {
                        using (BinaryReader reader = new BinaryReader(stream))
                            ((EverestModuleBinarySession)_Session).Read(reader);
                    }
                    else
                    {
                        using (StreamReader reader = new StreamReader(stream))
                            YamlHelper.DeserializerUsing(_Session).Deserialize(reader, SessionType);
                    }
                }
                _Session.Index = index;
            } catch (Exception e) {
                Logger.Log(LogLevel.Warn, "EverestModule", $"Failed to load the session of {Metadata.Name}!");
                Logger.LogDetailed(e);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Deserialize the mod session from its raw bytes, fed with data from ReadSession either immediately or async.
        /// </summary>
        public virtual void DeserializeSession(int index, byte[] data)
        {
            if (!SaveDataAsync && !ForceSaveDataAsync)
            {
                throw new Exception($"{Metadata.Name} overrides old methods or otherwise disabled async save data support.");
            }

            if (SessionType == null)
            {
                return;
            }

            _Session       = (EverestModuleSession)SessionType.GetConstructor(Everest._EmptyTypeArray).Invoke(Everest._EmptyObjectArray);
            _Session.Index = index;

            if (data == null)
            {
                return;
            }

            try {
                using (MemoryStream stream = new MemoryStream(data)) {
                    if (_Session is EverestModuleBinarySession bs)
                    {
                        using (BinaryReader reader = new BinaryReader(stream))
                            bs.Read(reader);
                    }
                    else
                    {
                        using (StreamReader reader = new StreamReader(stream))
                            YamlHelper.DeserializerUsing(_Session).Deserialize(reader, SessionType);
                    }
                }
                _Session.Index = index;
            } catch (Exception e) {
                Logger.Log(LogLevel.Warn, "EverestModule", $"Failed to deserialize the session of {Metadata.Name}!");
                Logger.LogDetailed(e);
            }
        }