/// <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 { IDeserializer deserializer = new DeserializerBuilder() .IgnoreUnmatchedProperties() .WithObjectFactory(t => _Session) .Build(); using (StreamReader reader = new StreamReader(path)) _Session = (EverestModuleSession)deserializer.Deserialize(reader, SessionType); } } _Session.Index = index; } catch { } }
/// <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); } }
/// <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); } }