Scriptum/Assets/Scripts/REFACTORING/Application/Panel/Inventory/SaveInventoryManager.cs
2022-11-24 03:03:30 +01:00

99 lines
3.6 KiB
C#

using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
public class SaveInventoryManager : SaveModelSystem<IndexValuePair<int, EquippableItemPrefabAsset>>
{
public override bool SaveModelItem(IndexValuePair<int, EquippableItemPrefabAsset> model)
{
return base.SaveModelItem(model);
}
public override bool SaveModelList(List<IndexValuePair<int, EquippableItemPrefabAsset>> list)
{
BinaryFormatter formatter = new BinaryFormatter();
Debug.Log("Saved Inventory 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 IndexValuePair<int, EquippableItemPrefabAsset> LoadModelItem()
{
return base.LoadModelItem();
}
public override List<IndexValuePair<int, EquippableItemPrefabAsset>> LoadModelList()
{
string path = Path + GetFileName(ObjectListName);
if (File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
List<IndexValuePair<int, EquippableItemPrefabAssetData>> questsList = formatter.Deserialize(stream) as List<IndexValuePair<int, EquippableItemPrefabAssetData>>;
stream.Close();
return ConvertListOfDataModelsToListOfObject(questsList);
}
else
{
Debug.Log("Save file not found in " + path);
}
return new List<IndexValuePair<int, EquippableItemPrefabAsset>>();
}
// Support function
// 1. From model to data format
private IndexValuePair<int, EquippableItemPrefabAssetData> ConvertObjectToDataModel(IndexValuePair<int, EquippableItemPrefabAsset> model)
{
return new IndexValuePair<int, EquippableItemPrefabAssetData>(model.Key, new EquippableItemPrefabAssetData(model.Value));
}
private List<IndexValuePair<int, EquippableItemPrefabAssetData>> ConvertObjectsListToListOfDataModels(List<IndexValuePair<int, EquippableItemPrefabAsset>> modelsList)
{
List<IndexValuePair<int, EquippableItemPrefabAssetData>> convertedList = new List<IndexValuePair<int, EquippableItemPrefabAssetData>>();
foreach(IndexValuePair<int, EquippableItemPrefabAsset> model in modelsList)
{
convertedList.Add(ConvertObjectToDataModel(model));
}
return convertedList;
}
// 2. From data to model format
private IndexValuePair<int, EquippableItemPrefabAsset> ConvertDataModelToObject(IndexValuePair<int, EquippableItemPrefabAssetData> dataModel)
{
return new IndexValuePair<int, EquippableItemPrefabAsset>(dataModel.Key, (EquippableItemPrefabAsset)dataModel.Value.MapDataToPrefabAssetModel());
}
private List<IndexValuePair<int, EquippableItemPrefabAsset>> ConvertListOfDataModelsToListOfObject(List<IndexValuePair<int, EquippableItemPrefabAssetData>> dataModelsList)
{
List<IndexValuePair<int, EquippableItemPrefabAsset>> convertedList = new List<IndexValuePair<int, EquippableItemPrefabAsset>>();
foreach (IndexValuePair<int, EquippableItemPrefabAssetData> dataModel in dataModelsList)
{
convertedList.Add(ConvertDataModelToObject(dataModel));
}
return convertedList;
}
}