Scriptum/Assets/Scripts/SceneManager/SaveChest/SaveChestSystem.cs

106 lines
3.4 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public static class SaveChestSystem
{
private static string map;
public static void SaveChest(ChestPrefarbAsset chestPrefarbAsset, string ElementFolderName)
{
BinaryFormatter formatter = new BinaryFormatter();
// todo: add in scene name folder
string path = SaveSystem.GetSavePath(ElementFolderName);
Debug.Log(path);
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
path += "/" + chestPrefarbAsset.name + ".fun";
FileStream stream = new FileStream(path, FileMode.Create);
ChestPrefarbAssetData chestData = new ChestPrefarbAssetData(chestPrefarbAsset);
formatter.Serialize(stream, chestData);
stream.Close();
}
public static void SaveChestsList(List<ChestPrefarbAsset> chestPrefarbAssetList, string _path, string ElementName)
{
BinaryFormatter formatter = new BinaryFormatter();
// todo: add in scene name folder
string path = SaveSystem.GetSavePath(_path);
Debug.Log("Saved Chest at " + path);
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
path += "/" + ElementName + ".fun";
FileStream stream = new FileStream(path, FileMode.Create);
List<ChestPrefarbAssetData> chestsListData = new List<ChestPrefarbAssetData>();
Debug.Log("---- Save Chest");
foreach(ChestPrefarbAsset chest in chestPrefarbAssetList)
{
Debug.Log(chest.name);
Debug.Log(chest.content.Count);
foreach(KeyValuePair<int, EquippableItemPrefarbAsset> equippableItemPrefarbAsset in chest.content)
{
Debug.Log("item on position " + equippableItemPrefarbAsset.Key + " " + equippableItemPrefarbAsset.Value.equippableItem.name);
}
chestsListData.Add(new ChestPrefarbAssetData(chest));
}
formatter.Serialize(stream, chestsListData);
stream.Close();
}
private static void Save()
{
}
public static ChestPrefarbAssetData LoadChest(string chestName, string ElementFolderName)
{
string path = SaveSystem.GetSavePath(ElementFolderName) + "/" + chestName;// + ".fun";
if(File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
ChestPrefarbAssetData chestData = formatter.Deserialize(stream) as ChestPrefarbAssetData;
stream.Close();
return chestData;
} else {
Debug.Log("Save file not found in " + path);
return null;
}
}
public static List<ChestPrefarbAssetData> LoadChestsList(string _path, string ElementName)
{
string path = SaveSystem.GetSavePath(_path) + "/" + ElementName + ".fun";
if(File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
List<ChestPrefarbAssetData> chestsListData = formatter.Deserialize(stream) as List<ChestPrefarbAssetData>;
stream.Close();
return chestsListData;
} else {
Debug.Log("Save file not found in " + path);
return null;
}
}
}