Scriptum/Assets/Scripts/REFACTORING/Application/Mission/Save/Condition/SaveMissionConditionManager.cs
2023-01-03 22:44:24 +01:00

125 lines
4.4 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
class SaveMissionConditionManager : SaveModelSystem<IndexValuePair<string, List<MissionConditionData>>> // TODO change MissionConditionData
{
public override bool SaveModelItem(IndexValuePair<string, List<MissionConditionData>> model)
{
return base.SaveModelItem(model);
}
public override bool SaveModelList(List<IndexValuePair<string, List<MissionConditionData>>> list)
{
BinaryFormatter formatter = new BinaryFormatter();
Debug.Log("Saved Mision Condition list at " + Path);
if (!Directory.Exists(Path)) Directory.CreateDirectory(Path);
/* Main logic of conversion data format */
var data = list; // ConvertObjectsListToListOfDataModels(list);
FileStream stream = new FileStream(
Path + GetFileName(ObjectListName),
FileMode.Create
);
formatter.Serialize(stream, data);
stream.Close();
return true;
}
public override IndexValuePair<string, List<MissionConditionData>> LoadModelItem()
{
return base.LoadModelItem();
}
public override List<IndexValuePair<string, List<MissionConditionData>>> LoadModelList()
{
string path = Path + GetFileName(ObjectListName);
if (File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
List<IndexValuePair<string, List<MissionConditionData>>> missionsList = formatter.Deserialize(stream) as List<IndexValuePair<string, List<MissionConditionData>>>;
stream.Close();
return missionsList; // ConvertListOfDataModelsToListOfObject(questsList);
}
else
{
Debug.Log("Save file not found in " + path);
}
return new List<IndexValuePair<string, List<MissionConditionData>>>();
}
// ----------------- USELLES IN THIS CLASS -----------------
#region unused
// Support function
// 1. From model to data format
public static MissionConditionData ConvertObjectToDataModel(MissionCondition model)
{
return MissionConditionData.Build(model);
}
public static List<IndexValuePair<string, List<MissionConditionData>>> ConvertObjectsListToListOfDataModels(List<IndexValuePair<string, List<MissionCondition>>> modelsList)
{
List<IndexValuePair<string, List<MissionConditionData>>> convertedList = new List<IndexValuePair<string, List<MissionConditionData>>>();
foreach (IndexValuePair<string, List<MissionCondition>> dataModel in modelsList)
{
IndexValuePair<string, List<MissionConditionData>> conditionsForMapList = new IndexValuePair<string, List<MissionConditionData>>(dataModel.Key, new List<MissionConditionData>());
foreach (MissionCondition condition in dataModel.Value)
{
conditionsForMapList.Value.Add(
ConvertObjectToDataModel(condition)
);
}
convertedList.Add(conditionsForMapList);
}
return convertedList;
}
// 2. From data to model format
public static MissionCondition ConvertDataModelToObject(MissionConditionData dataModel)
{
return dataModel.MapDataToObject();
}
public static List<IndexValuePair<string, List<MissionCondition>>> ConvertListOfDataModelsToListOfObject(List<IndexValuePair<string, List<MissionConditionData>>> dataModelsList)
{
List<IndexValuePair<string, List<MissionCondition>>> conditioncConvertedList = new List<IndexValuePair<string, List<MissionCondition>>>();
foreach (IndexValuePair<string, List<MissionConditionData>> dataModel in dataModelsList)
{
IndexValuePair<string, List<MissionCondition>> conditionsForMapList = new IndexValuePair<string, List<MissionCondition>>(dataModel.Key, new List<MissionCondition>());
foreach (MissionConditionData condition in dataModel.Value)
{
conditionsForMapList.Value.Add(
ConvertDataModelToObject(condition)
);
}
conditioncConvertedList.Add(conditionsForMapList);
}
return conditioncConvertedList;
}
#endregion
}