Scriptum/Assets/Scripts/REFACTORING/Application/Mission/Save/SaveMissionManager.cs

104 lines
2.7 KiB
C#
Raw 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;
2023-01-03 22:44:24 +01:00
class SaveMissionManager : SaveModelSystem<MissionData>
2023-01-01 17:03:35 +01:00
{
2023-01-03 22:44:24 +01:00
public override bool SaveModelItem(MissionData model)
2023-01-01 17:03:35 +01:00
{
return base.SaveModelItem(model);
}
2023-01-03 22:44:24 +01:00
public override bool SaveModelList(List<MissionData> list)
2023-01-01 17:03:35 +01:00
{
BinaryFormatter formatter = new BinaryFormatter();
Debug.Log("Saved Mision list at " + Path);
if (!Directory.Exists(Path)) Directory.CreateDirectory(Path);
/* Main logic of conversion data format */
2023-01-03 22:44:24 +01:00
var data = list; // ConvertObjectsListToListOfDataModels(list);
2023-01-01 17:03:35 +01:00
FileStream stream = new FileStream(
Path + GetFileName(ObjectListName),
FileMode.Create
);
formatter.Serialize(stream, data);
stream.Close();
return true;
}
2023-01-03 22:44:24 +01:00
public override MissionData LoadModelItem()
2023-01-01 17:03:35 +01:00
{
return base.LoadModelItem();
}
2023-01-03 22:44:24 +01:00
public override List<MissionData> LoadModelList()
2023-01-01 17:03:35 +01:00
{
string path = Path + GetFileName(ObjectListName);
if (File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
2023-01-03 22:44:24 +01:00
List<MissionData> missionsList = formatter.Deserialize(stream) as List<MissionData>;
2023-01-01 17:03:35 +01:00
stream.Close();
2023-01-03 22:44:24 +01:00
return missionsList; // ConvertListOfDataModelsToListOfObject(questsList);
2023-01-01 17:03:35 +01:00
}
else
{
Debug.Log("Save file not found in " + path);
}
2023-01-03 22:44:24 +01:00
return new List<MissionData>();
2023-01-01 17:03:35 +01:00
}
2023-01-03 22:44:24 +01:00
// ----------------- USELLES IN THIS CLASS -----------------
#region unused
2023-01-01 17:03:35 +01:00
// Support function
// 1. From model to data format
public static MissionData ConvertObjectToDataModel(Mission model)
{
2023-01-03 22:44:24 +01:00
return new MissionData(model);
2023-01-01 17:03:35 +01:00
}
public static List<MissionData> ConvertObjectsListToListOfDataModels(List<Mission> modelsList)
{
List<MissionData> convertedList = new List<MissionData>();
foreach (Mission model in modelsList)
{
convertedList.Add(ConvertObjectToDataModel(model));
}
return convertedList;
}
// 2. From data to model format
public static Mission ConvertDataModelToObject(MissionData dataModel)
{
return (Mission)dataModel.MapDataToObject();
}
public static List<Mission> ConvertListOfDataModelsToListOfObject(List<MissionData> dataModelsList)
{
List<Mission> convertedList = new List<Mission>();
foreach (MissionData dataModel in dataModelsList)
{
convertedList.Add(ConvertDataModelToObject(dataModel));
}
return convertedList;
}
2023-01-03 22:44:24 +01:00
#endregion
2023-01-01 17:03:35 +01:00
}