public static void SaveWorldMap(WorldMapData worldMapData) { FileStream fs = File.Create(WorldMapSaveFile); BinaryFormatter bf = new BinaryFormatter(); SerializedWorldMapData gameData = new SerializedWorldMapData(); gameData.Player = DataSerialization.Serialize(worldMapData.Player); gameData.Seed = worldMapData.Seed; gameData.WorldMapInfos = worldMapData.WorldMapInfos; gameData.DiscoveredMap = worldMapData.DiscoveredMap; gameData.CurrentPosX = (int)worldMapData.CurrentPos.x; gameData.CurrentPosY = (int)worldMapData.CurrentPos.y; gameData.Size = worldMapData.Size; gameData.InventoryItemId = new List <string>(); gameData.InventoryItemQuantity = new List <int>(); foreach (Item item in worldMapData.Inventory) { gameData.InventoryItemId.Add(item.ItemId.ToString()); gameData.InventoryItemQuantity.Add(item.Quantity); } bf.Serialize(fs, gameData); fs.Close(); }
public static WorldMapData LoadWorldMap() { if (File.Exists(WorldMapSaveFile)) { FileStream fs = File.Open(WorldMapSaveFile, FileMode.Open); if (fs == null || fs.Length == 0) { Debug.Log("Failed to load WorldMapSaveFile (null or empty)"); return(null); } BinaryFormatter bf = new BinaryFormatter(); SerializedWorldMapData gameData = (SerializedWorldMapData)bf.Deserialize(fs); fs.Close(); WorldMapData worldMapData = new WorldMapData(); Dictionary <string, string> skillsDb = DataSerialization.ReadDatabasePathById(skillsDatabasePreset); Dictionary <string, string> itemsDb = DataSerialization.ReadDatabasePathById(itemsDatabasePreset); worldMapData.Player = DataSerialization.Deserialize <Player>(gameData.Player, skillsDb, itemsDb); worldMapData.Seed = gameData.Seed; worldMapData.WorldMapInfos = gameData.WorldMapInfos; worldMapData.DiscoveredMap = gameData.DiscoveredMap; worldMapData.CurrentPos = new Vector2(gameData.CurrentPosX, gameData.CurrentPosY); worldMapData.Size = gameData.Size; worldMapData.Inventory = new List <Item>(); for (int i = 0; i < gameData.InventoryItemId.Count; i++) { Item newItem = ScriptableObject.CreateInstance <Item>(); worldMapData.Inventory.Add(DataSerialization.DeserializeItem(newItem, gameData.InventoryItemId[i], itemsDb)); newItem.Quantity = gameData.InventoryItemQuantity[i]; } return(worldMapData); } else { Debug.Log("(Load) WorldMapSaveFile doesn't exist!"); return(null); } }