using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.Serialization.Formatters.Binary; using UnityEngine; public class SaveEquipmentManager : SaveModelSystem> { public override bool SaveModelItem(IndexValuePair model) { return base.SaveModelItem(model); } public override bool SaveModelList(List> list) { BinaryFormatter formatter = new BinaryFormatter(); Debug.Log("Saved Equipment 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 LoadModelItem() { return base.LoadModelItem(); } public override List> LoadModelList() { string path = Path + GetFileName(ObjectListName); if (File.Exists(path)) { BinaryFormatter formatter = new BinaryFormatter(); FileStream stream = new FileStream(path, FileMode.Open); List> euipmentList = formatter.Deserialize(stream) as List>; stream.Close(); return ConvertListOfDataModelsToListOfObject(euipmentList); } else { Debug.Log("Save file not found in " + path); } return new List>(); } // Support function // 1. From model to data format private IndexValuePair ConvertObjectToDataModel(IndexValuePair model) { if (model.Value != null) return new IndexValuePair(model.Key, new EquippableItemPrefabAssetData(model.Value)); else return new IndexValuePair(model.Key, null); } private List> ConvertObjectsListToListOfDataModels(List> modelsList) { // 1. prepare list with all slot possibiliteies List> convertedList = GetEmptyListOfEquipmentSlotsPrefabData(); // 2. Assign values foreach (IndexValuePair model in modelsList) { convertedList.RemoveAll(slot => slot.Key == model.Key); convertedList.Add(ConvertObjectToDataModel(model)); } return convertedList; } // 2. From data to model format private IndexValuePair ConvertDataModelToObject(IndexValuePair dataModel) { if(dataModel.Value != null) return new IndexValuePair(dataModel.Key, (EquippableItemPrefabAsset)dataModel.Value.MapDataToPrefabAssetModel()); else return new IndexValuePair(dataModel.Key, null); } private List> ConvertListOfDataModelsToListOfObject(List> dataModelsList) { // 1. prepare list with all slot possibiliteies List> convertedList = GetEmptyListOfEquipmentSlotsPrefab(); // 2. Assign values foreach (IndexValuePair dataModel in dataModelsList) { convertedList.RemoveAll(slot => slot.Key == (EquipmentPanelSlotsTypeEnum)dataModel.Key); convertedList.Add(ConvertDataModelToObject(dataModel)); } return convertedList; } // Empty list factories functions private List> GetEmptyListOfEquipmentSlotsPrefab() { List> convertedList = new List>(); foreach (EquipmentPanelSlotsTypeEnum emptyElement in Enum.GetValues(typeof(EquipmentPanelSlotsTypeEnum))) { convertedList.Add(new IndexValuePair(emptyElement, null)); } return convertedList; } private List> GetEmptyListOfEquipmentSlotsPrefabData() { List> convertedList = new List>(); foreach (EquipmentPanelSlotsTypeEnum emptyElement in Enum.GetValues(typeof(EquipmentPanelSlotsTypeEnum))) { convertedList.Add(new IndexValuePair(emptyElement, null)); } return convertedList; } }