Scriptum/Assets/Scripts/REFACTORING/Application/Dialogue/Save/MissionDialogue/SaveMissionDialogueManager.cs

100 lines
2.8 KiB
C#
Raw Permalink Normal View History

2023-01-01 17:03:35 +01:00
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
class SaveMissionDialogueManager : SaveModelSystem<MissionDialogue>
{
public override bool SaveModelItem(MissionDialogue model)
{
return base.SaveModelItem(model);
}
public override bool SaveModelList(List<MissionDialogue> list)
{
BinaryFormatter formatter = new BinaryFormatter();
Debug.Log("Saved MissionDialogue list at " + Path);
if (!Directory.Exists(Path)) Directory.CreateDirectory(Path);
/* Main logic of conversion data format */
var data = ConvertObjectsListToListOfDataModels(list);
FileStream stream = new FileStream(
Path + GetFileName(ObjectListName),
FileMode.Create
);
formatter.Serialize(stream, data);
stream.Close();
return true;
}
public override MissionDialogue LoadModelItem()
{
return base.LoadModelItem();
}
public override List<MissionDialogue> LoadModelList()
{
string path = Path + GetFileName(ObjectListName);
if (File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
List<MissionDialogueData> questsList = formatter.Deserialize(stream) as List<MissionDialogueData>;
stream.Close();
return ConvertListOfDataModelsToListOfObject(questsList);
}
else
{
Debug.Log("Save file not found in " + path);
}
return new List<MissionDialogue>();
}
// Support function
// 1. From model to data format
public static MissionDialogueData ConvertObjectToDataModel(MissionDialogue model)
{
2023-01-02 00:05:47 +01:00
return new MissionDialogueData(model);
2023-01-01 17:03:35 +01:00
}
public static List<MissionDialogueData> ConvertObjectsListToListOfDataModels(List<MissionDialogue> modelsList)
{
List<MissionDialogueData> convertedList = new List<MissionDialogueData>();
foreach (MissionDialogue model in modelsList)
{
convertedList.Add(ConvertObjectToDataModel(model));
}
return convertedList;
}
// 2. From data to model format
public static MissionDialogue ConvertDataModelToObject(MissionDialogueData dataModel)
{
return (MissionDialogue)dataModel.MapDataToObject();
}
public static List<MissionDialogue> ConvertListOfDataModelsToListOfObject(List<MissionDialogueData> dataModelsList)
{
List<MissionDialogue> convertedList = new List<MissionDialogue>();
foreach (MissionDialogueData dataModel in dataModelsList)
{
convertedList.Add(ConvertDataModelToObject(dataModel));
}
return convertedList;
}
}