89 lines
2.0 KiB
C#
89 lines
2.0 KiB
C#
using System.IO;
|
|
using System.Text;
|
|
using Newtonsoft.Json.Linq;
|
|
using UnityEngine;
|
|
|
|
public class BazookaManager : MonoBehaviour
|
|
{
|
|
public static BazookaManager Instance;
|
|
private bool firstLoadDone = false;
|
|
public JObject saveFile = new()
|
|
{
|
|
["version"] = "0"
|
|
};
|
|
|
|
void Awake()
|
|
{
|
|
if (Instance == null)
|
|
{
|
|
Instance = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
if (!firstLoadDone)
|
|
{
|
|
firstLoadDone = true;
|
|
Load();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
|
|
void OnApplicationQuit()
|
|
{
|
|
Save();
|
|
}
|
|
|
|
void OnApplicationPause(bool pause)
|
|
{
|
|
if (pause)
|
|
{
|
|
Save();
|
|
}
|
|
}
|
|
|
|
public void Load()
|
|
{
|
|
string path = Path.Join(Application.persistentDataPath, "savefile.json");
|
|
if (!File.Exists(path))
|
|
{
|
|
File.Create(path).Dispose();
|
|
}
|
|
else
|
|
{
|
|
try
|
|
{
|
|
var tempSaveFile = JObject.Parse(File.ReadAllText(path));
|
|
if (tempSaveFile != null) saveFile = tempSaveFile;
|
|
}
|
|
catch
|
|
{
|
|
Debug.LogWarning("Failed to load save file");
|
|
}
|
|
}
|
|
if (saveFile["version"] == null || saveFile["version"].ToString() != "0")
|
|
{
|
|
saveFile["version"] = "0";
|
|
}
|
|
}
|
|
|
|
public void Save()
|
|
{
|
|
string path = Path.Join(Application.persistentDataPath, "savefile.json");
|
|
using var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
|
|
var encoded = Encoding.UTF8.GetBytes(saveFile.ToString(Newtonsoft.Json.Formatting.Indented));
|
|
fileStream.Write(encoded, 0, encoded.Length);
|
|
fileStream.Flush(true);
|
|
}
|
|
|
|
public void ResetSave()
|
|
{
|
|
saveFile = new JObject
|
|
{
|
|
["version"] = "0"
|
|
};
|
|
Save();
|
|
}
|
|
}
|