// Copyright (c) Pixel Crushers. All rights reserved.
using UnityEngine;
using System;
using System.Collections.Generic;
using System.Linq;
namespace PixelCrushers
{
///
/// Holds the data for a saved game.
///
[Serializable]
public class SavedGameData : ISerializationCallbackReceiver
{
///
/// Holds the data returned by a Saver along with the Saver's key
/// and the index of the scene that the Saver was in.
///
[Serializable]
public class SaveRecord
{
public string key;
public int sceneIndex;
public string data;
public SaveRecord() { }
public SaveRecord(string key, int sceneIndex, string data)
{
this.key = key;
this.sceneIndex = sceneIndex;
this.data = data;
}
}
[SerializeField]
private int m_version = 0;
[SerializeField]
private string m_sceneName;
private Dictionary m_dict = new Dictionary();
[SerializeField]
private List m_list = new List();
///
/// The save file format version. This is an arbitrary value that you
/// can assign by setting SaveSystem.version.
///
public int version
{
get { return m_version; }
set { m_version = value; }
}
///
/// The scene in which the game was saved.
///
public string sceneName
{
get { return m_sceneName; }
set { m_sceneName = value; }
}
///
/// Provides direct access to the dictionary of save records.
/// Use with caution.
///
public Dictionary Dict { get { return m_dict; } }
public void OnBeforeSerialize()
{
m_list.Clear();
foreach (var kvp in m_dict)
{
m_list.Add(kvp.Value);
}
}
public void OnAfterDeserialize()
{
m_dict = new Dictionary();
for (int i = 0; i < m_list.Count; i++)
{
if (m_list[i] == null) continue;
m_dict.Add(m_list[i].key, m_list[i]);
}
}
///
/// Retrieves info about the previously-saved data for a Saver.
///
/// Saver's unique key.
/// Returns the Save Record stored under the Saver's key, or null if no data is stored.
public SaveRecord GetDataInfo(string key)
{
return m_dict.ContainsKey(key) ? m_dict[key] : null;
}
///
/// Retrieves the previously-saved data for a Saver.
///
/// Saver's unique key.
/// Returns the data stored under the Saver's key, or null if no data is stored.
public string GetData(string key)
{
return m_dict.ContainsKey(key) ? m_dict[key].data : null;
}
///
/// Stores a Saver's data.
///
/// Saver's unique key.
/// Scene in which Saver exists.
/// Data to set.
public void SetData(string key, int sceneIndex, string data)
{
if (m_dict.ContainsKey(key))
{
m_dict[key].sceneIndex = sceneIndex;
m_dict[key].data = data;
}
else
{
m_dict.Add(key, new SaveRecord(key, sceneIndex, data));
}
}
///
/// Removes a Saver's data.
///
/// Saver's unique key.
public void DeleteData(string key)
{
if (m_dict.ContainsKey(key))
{
m_dict.Remove(key);
}
}
///
/// Removes all save records except those in the current scene and those that are
/// configured to remember across scene changes.
///
/// Don't clear out Savers in this scene.
public void DeleteObsoleteSaveData(int currentSceneIndex)
{
m_dict = m_dict.Where(element => element.Value.sceneIndex == currentSceneIndex || element.Value.sceneIndex == SaveSystem.NoSceneIndex)
.ToDictionary(element => element.Key, element => element.Value);
}
}
}