MonsterShop/Assets/Scripts/DataPersistence/DataPersistenceManager.cs

170 lines
4.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;
public class DataPersistenceManager : MonoBehaviour
{
// [Header("Debugging")]
// [SerializeField] private bool initializeDataIfNull = false;
[Header("File Storage Config")]
[SerializeField]
private string fileName;
[SerializeField]
private bool useEncryption;
[Header("Auto Saving Configuration")]
[SerializeField]
private float autoSaveTimeSeconds = 60f;
private GameData gameData = new GameData();
private List<IDataPersistence<GameData>> dataPersistenceObjects;
private FileDataHandler dataHandler;
private Coroutine autoSaveCoroutine;
public static DataPersistenceManager instance { get; private set; }
private void Awake()
{
if (instance != null)
{
Debug.Log(
"Found more than one Data Persistence Manager in the scene. Destroying the newest one."
);
Destroy(this.gameObject);
return;
}
instance = this;
DontDestroyOnLoad(this.gameObject);
this.dataHandler = new FileDataHandler(
Application.persistentDataPath,
fileName,
useEncryption
);
}
private void OnEnable()
{
SceneManager.sceneLoaded += OnSceneLoaded;
Initialize();
}
private void OnDisable()
{
SceneManager.sceneLoaded -= OnSceneLoaded;
}
public void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
// Start();
}
private void Initialize()
{
LoadGame();
// start up the auto saving coroutine
if (autoSaveCoroutine != null)
{
StopCoroutine(autoSaveCoroutine);
}
autoSaveCoroutine = StartCoroutine(AutoSave());
}
public void NewGame()
{
Debug.Log("new game");
this.gameData = new GameData();
}
public void LoadGame()
{
Debug.Log("Loading game");
this.dataPersistenceObjects = FindAllDataPersistenceObjects();
// load any saved data from a file using the data handler
this.gameData = dataHandler.Load();
// start a new game if the data is null and we're configured to initialize data for debugging purposes
if (this.gameData == null)
{
NewGame();
return;
}
// if no data can be loaded, don't continue
// if (this.gameData == null)
// {
// Debug.Log("No data was found. A New Game needs to be started before data can be loaded.");
// return;
// }
// push the loaded data to all other scripts that need it
foreach (IDataPersistence<GameData> dataPersistenceObj in dataPersistenceObjects)
{
dataPersistenceObj.LoadData(gameData);
}
}
public void SaveGame()
{
Debug.Log("Saving game");
this.dataPersistenceObjects = FindAllDataPersistenceObjects();
// if we don't have any data to save, log a warning here
if (this.gameData == null)
{
Debug.LogWarning(
"No data was found. A New Game needs to be started before data can be saved."
);
return;
}
// pass the data to other scripts so they can update it
foreach (IDataPersistence<GameData> dataPersistenceObj in dataPersistenceObjects)
{
dataPersistenceObj.SaveData(gameData);
}
// timestamp the data so we know when it was last saved
gameData.lastUpdated = System.DateTime.Now.ToBinary();
// save that data to a file using the data handler
dataHandler.Save(gameData);
}
private void OnApplicationQuit()
{
SaveGame();
}
private List<IDataPersistence<GameData>> FindAllDataPersistenceObjects()
{
// FindObjectsofType takes in an optional boolean to include inactive gameobjects
IEnumerable<IDataPersistence<GameData>> dataPersistenceObjects =
FindObjectsByType<MonoBehaviour>(FindObjectsSortMode.None)
.OfType<IDataPersistence<GameData>>();
var list = new List<IDataPersistence<GameData>>(dataPersistenceObjects);
return list;
}
private IEnumerator AutoSave()
{
while (true)
{
yield return new WaitForSeconds(autoSaveTimeSeconds);
SaveGame();
Debug.Log("Auto Saved Game");
}
}
}