Make icon cache save to disk
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Numerics;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
@@ -9,7 +13,7 @@ public class BazookaManager : MonoBehaviour
|
||||
private bool firstLoadDone = false;
|
||||
public JObject saveFile = new()
|
||||
{
|
||||
["version"] = "0"
|
||||
["version"] = "1"
|
||||
};
|
||||
public List<MarketplaceIcon> iconCache = new();
|
||||
|
||||
@@ -22,7 +26,42 @@ public class BazookaManager : MonoBehaviour
|
||||
if (!firstLoadDone)
|
||||
{
|
||||
firstLoadDone = true;
|
||||
Load();
|
||||
var save = LoadObject("savefile");
|
||||
var cache = LoadArray("cache");
|
||||
if (save != null)
|
||||
{
|
||||
saveFile = save;
|
||||
if (saveFile["version"] == null)
|
||||
{
|
||||
saveFile["version"] = "0";
|
||||
}
|
||||
if (saveFile["version"].ToString() == "0")
|
||||
{
|
||||
if (saveFile["bird"]["customIcon"]["data"] != null)
|
||||
{
|
||||
var data = saveFile["bird"]["customIcon"]["data"];
|
||||
var customData = GetCustomBirdIconData();
|
||||
saveFile["bird"]["customIcon"]["data"].Parent.Remove();
|
||||
foreach (var icon in data)
|
||||
if (icon["uuid"].ToString() != null) customData.Purchased.Add(icon["uuid"].ToString());
|
||||
SetCustomBirdIconData(customData);
|
||||
}
|
||||
saveFile["version"] = "1";
|
||||
Save("savefile", saveFile);
|
||||
}
|
||||
}
|
||||
if (cache != null)
|
||||
{
|
||||
var tempIconCache = cache.ToObject<List<MarketplaceIcon>>() ?? new List<MarketplaceIcon>();
|
||||
foreach (var icon in tempIconCache)
|
||||
{
|
||||
iconCache.Add(new MarketplaceIcon
|
||||
{
|
||||
Data = icon.Data.Substring(0, icon.Data.Length - 128),
|
||||
ID = icon.ID
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -33,65 +72,106 @@ public class BazookaManager : MonoBehaviour
|
||||
|
||||
void OnApplicationQuit()
|
||||
{
|
||||
Save();
|
||||
Save("savefile", saveFile);
|
||||
|
||||
var cacheIcons = new JArray();
|
||||
foreach (var icon in iconCache)
|
||||
cacheIcons.Add(new JObject
|
||||
{
|
||||
["data"] = icon.Data + Tools.Sha512Sum(Convert.FromBase64String(icon.Data)),
|
||||
["id"] = icon.ID
|
||||
});
|
||||
Save("cache", cacheIcons);
|
||||
}
|
||||
|
||||
void OnApplicationPause(bool pause)
|
||||
{
|
||||
if (pause)
|
||||
{
|
||||
Save();
|
||||
Save("savefile", saveFile);
|
||||
|
||||
var cacheIcons = new JArray();
|
||||
foreach (var icon in iconCache)
|
||||
cacheIcons.Add(new JObject
|
||||
{
|
||||
["data"] = icon.Data + Tools.Sha512Sum(Convert.FromBase64String(icon.Data)),
|
||||
["id"] = icon.ID
|
||||
});
|
||||
Save("cache", cacheIcons);
|
||||
}
|
||||
}
|
||||
|
||||
public void Load()
|
||||
public JObject LoadObject(string saveName)
|
||||
{
|
||||
if (SensitiveInfo.BAZOOKA_MANAGER_KEY.Trim().Length == 0 || SensitiveInfo.BAZOOKA_MANAGER_FILE_KEY.Trim().Length == 0) return;
|
||||
string path = Path.Join(Application.persistentDataPath, SensitiveInfo.BAZOOKA_MANAGER_FILE_KEY + ".dat");
|
||||
string path = Path.Join(Application.persistentDataPath, saveName + ".json");
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
File.Create(path).Dispose();
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
var tempSaveFile = JObject.Parse(SensitiveInfo.DecryptRaw(File.ReadAllBytes(path), SensitiveInfo.BAZOOKA_MANAGER_KEY));
|
||||
if (tempSaveFile != null) saveFile = tempSaveFile;
|
||||
var tempSaveFile = JObject.Parse(File.ReadAllText(path));
|
||||
if (tempSaveFile != null) return tempSaveFile;
|
||||
}
|
||||
catch
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogWarning("Failed to load save file");
|
||||
Debug.LogWarning("Failed to load save file: " + e);
|
||||
}
|
||||
}
|
||||
if (saveFile["version"] == null || saveFile["version"].ToString() != "0")
|
||||
{
|
||||
saveFile["version"] = "0";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void Save()
|
||||
public JArray LoadArray(string saveName)
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
return;
|
||||
#else
|
||||
if (SensitiveInfo.BAZOOKA_MANAGER_KEY.Trim().Length == 0 || SensitiveInfo.BAZOOKA_MANAGER_FILE_KEY.Trim().Length == 0) return;
|
||||
string path = Path.Join(Application.persistentDataPath, SensitiveInfo.BAZOOKA_MANAGER_FILE_KEY + ".dat");
|
||||
var encoded = SensitiveInfo.EncryptRaw(saveFile.ToString(Newtonsoft.Json.Formatting.None), SensitiveInfo.BAZOOKA_MANAGER_KEY);
|
||||
string path = Path.Join(Application.persistentDataPath, saveName + ".json");
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
var tempSaveFile = JArray.Parse(File.ReadAllText(path));
|
||||
if (tempSaveFile != null) return tempSaveFile;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogWarning("Failed to load save file: " + e);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void Save(string saveName, JObject save)
|
||||
{
|
||||
string path = Path.Join(Application.persistentDataPath, saveName + ".json");
|
||||
var encoded = Encoding.UTF8.GetBytes(save.ToString(Formatting.Indented));
|
||||
if (encoded == null) return;
|
||||
using var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
|
||||
fileStream.Write(encoded, 0, encoded.Length);
|
||||
fileStream.Flush(true);
|
||||
}
|
||||
|
||||
public void Save(string saveName, JArray save)
|
||||
{
|
||||
string path = Path.Join(Application.persistentDataPath, saveName + ".json");
|
||||
var encoded = Encoding.UTF8.GetBytes(save.ToString(Formatting.Indented));
|
||||
if (encoded == null) return;
|
||||
using var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
|
||||
fileStream.Write(encoded, 0, encoded.Length);
|
||||
fileStream.Flush(true);
|
||||
#endif
|
||||
}
|
||||
|
||||
public void ResetSave()
|
||||
{
|
||||
saveFile = new JObject
|
||||
{
|
||||
["version"] = "0"
|
||||
["version"] = "1"
|
||||
};
|
||||
Save();
|
||||
Save("savefile", saveFile);
|
||||
}
|
||||
|
||||
//Random stuff
|
||||
@@ -135,7 +215,6 @@ public class BazookaManager : MonoBehaviour
|
||||
saveFile["bird"]["overlay"] = value;
|
||||
}
|
||||
|
||||
|
||||
public void UnsetBirdOverlay()
|
||||
{
|
||||
if (saveFile["bird"] == null) return;
|
||||
|
||||
Reference in New Issue
Block a user