This should fully make browser savefile work now

This commit is contained in:
Lncvrt committed 2025-09-22 16:52:57 -07:00
1 parent d61aae848d
commit ad8ced1904
1 file changed
+41 -18
+41 -18
View File
@@ -1,3 +1,4 @@
using System;
using System.IO; using System.IO;
using System.Numerics; using System.Numerics;
using System.Text; using System.Text;
@@ -46,16 +47,35 @@ public class BazookaManager : MonoBehaviour
public void Load() public void Load()
{ {
string path = Path.Join(Application.persistentDataPath, "save.json"); if (Application.platform != RuntimePlatform.WebGLPlayer)
if (!File.Exists(path))
{ {
File.Create(path).Dispose(); string path = Path.Join(Application.persistentDataPath, "save.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";
}
} }
else else
{ {
try try
{ {
var tempSaveFile = JObject.Parse(File.ReadAllText(path)); var tempSaveFile = JObject.Parse(File.ReadAllText(Encoding.UTF8.GetString(Convert.FromBase64String(PlayerPrefs.GetString("saveFile", "e30=")))));
if (tempSaveFile != null) saveFile = tempSaveFile; if (tempSaveFile != null) saveFile = tempSaveFile;
} }
catch catch
@@ -63,24 +83,27 @@ public class BazookaManager : MonoBehaviour
Debug.LogWarning("Failed to load save file"); Debug.LogWarning("Failed to load save file");
} }
} }
if (saveFile["version"] == null || saveFile["version"].ToString() != "0")
{
saveFile["version"] = "0";
}
} }
public void Save() public void Save()
{ {
#if UNITY_EDITOR #if UNITY_EDITOR
return; return;
#else #else
string path = Path.Join(Application.persistentDataPath, "save.json"); if (Application.platform != RuntimePlatform.WebGLPlayer)
var encoded = Encoding.UTF8.GetBytes(saveFile.ToString()); {
using var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None); string path = Path.Join(Application.persistentDataPath, "save.json");
fileStream.Write(encoded, 0, encoded.Length); var encoded = Encoding.UTF8.GetBytes(saveFile.ToString());
fileStream.Flush(true); using var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
#endif fileStream.Write(encoded, 0, encoded.Length);
} fileStream.Flush(true);
}
else
{
PlayerPrefs.SetString("saveFile", Convert.ToBase64String(Encoding.UTF8.GetBytes(saveFile.ToString(Newtonsoft.Json.Formatting.None))));
}
#endif
}
public void ResetSave() public void ResetSave()
{ {