88ab6bcee7
Add Shop panel & saving module
100 lines
2.5 KiB
C#
100 lines
2.5 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Runtime.Serialization.Formatters.Binary;
|
|
using UnityEngine;
|
|
|
|
public class SaveShopManager : SaveModelSystem<Shop>
|
|
{
|
|
public override bool SaveModelItem(Shop model)
|
|
{
|
|
return base.SaveModelItem(model);
|
|
}
|
|
|
|
public override bool SaveModelList(List<Shop> list)
|
|
{
|
|
BinaryFormatter formatter = new BinaryFormatter();
|
|
|
|
Debug.Log("Saved Shops 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 Shop LoadModelItem()
|
|
{
|
|
return base.LoadModelItem();
|
|
}
|
|
|
|
public override List<Shop> LoadModelList()
|
|
{
|
|
string path = Path + GetFileName(ObjectListName);
|
|
|
|
if (File.Exists(path))
|
|
{
|
|
BinaryFormatter formatter = new BinaryFormatter();
|
|
FileStream stream = new FileStream(path, FileMode.Open);
|
|
|
|
List<ShopData> questsList = formatter.Deserialize(stream) as List<ShopData>;
|
|
stream.Close();
|
|
|
|
return ConvertListOfDataModelsToListOfObject(questsList);
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("Save file not found in " + path);
|
|
}
|
|
|
|
return new List<Shop>();
|
|
}
|
|
|
|
// Support function
|
|
|
|
// 1. From model to data format
|
|
public static ShopData ConvertObjectToDataModel(Shop model)
|
|
{
|
|
return new ShopData(model);
|
|
}
|
|
|
|
public static List<ShopData> ConvertObjectsListToListOfDataModels(List<Shop> modelsList)
|
|
{
|
|
List<ShopData> convertedList = new List<ShopData>();
|
|
|
|
foreach (Shop model in modelsList)
|
|
{
|
|
convertedList.Add(ConvertObjectToDataModel(model));
|
|
}
|
|
|
|
return convertedList;
|
|
}
|
|
|
|
// 2. From data to model format
|
|
public static Shop ConvertDataModelToObject(ShopData dataModel)
|
|
{
|
|
return (Shop)dataModel.MapDataToObject();
|
|
}
|
|
|
|
public static List<Shop> ConvertListOfDataModelsToListOfObject(List<ShopData> dataModelsList)
|
|
{
|
|
List<Shop> convertedList = new List<Shop>();
|
|
|
|
foreach (ShopData dataModel in dataModelsList)
|
|
{
|
|
convertedList.Add(ConvertDataModelToObject(dataModel));
|
|
}
|
|
|
|
return convertedList;
|
|
}
|
|
} |