Scriptum/Assets/Scripts/REFACTORING/Application/Panel/Inventory/SaveInventoryManager.cs

99 lines
3.6 KiB
C#
Raw Normal View History

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