100 lines
2.8 KiB
C#
100 lines
2.8 KiB
C#
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)
|
|
{
|
|
return new MissionDialogueData(model);
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|