Skip to content

Commit 2845105

Browse files
committed
Major bug fixes
- Fixed assets resolver that causes file corruption - Fixed bug on Andoid and IOS
1 parent 1bda874 commit 2845105

File tree

3 files changed

+70
-8
lines changed

3 files changed

+70
-8
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using UnityEngine;
3+
4+
namespace ToolBox.Serialization
5+
{
6+
internal sealed class ApplicationStateObserver : MonoBehaviour
7+
{
8+
public event Action OnQuit = null;
9+
10+
#if !UNITY_IOS && !UNITY_ANDROID
11+
private void OnApplicationQuit() =>
12+
OnQuit?.Invoke();
13+
#else
14+
private void OnApplicationPause(bool pause)
15+
{
16+
if (pause)
17+
OnQuit?.Invoke();
18+
}
19+
20+
private void OnApplicationFocus(bool focus)
21+
{
22+
if (!focus)
23+
OnQuit?.Invoke();
24+
}
25+
}
26+
#endif
27+
}

DataSerializer/ApplicationStateObserver.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

DataSerializer/DataSerializer.cs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ public static void Save<T>(string key, T dataToSave)
2525
if (_data.TryGetValue(key, out var data))
2626
{
2727
var item = data;
28-
item.Value = SerializationUtility.SerializeValue(dataToSave, DATA_FORMAT, _serializationContext);
28+
item.Value = Serialize(dataToSave);
2929
}
3030
else
3131
{
32-
var saveItem = new Item { Value = SerializationUtility.SerializeValue(dataToSave, DATA_FORMAT, _serializationContext) };
32+
var saveItem = new Item { Value = Serialize(dataToSave) };
3333
_data.Add(key, saveItem);
3434
}
3535
}
@@ -39,7 +39,7 @@ public static T Load<T>(string key)
3939
_data.TryGetValue(key, out var value);
4040
var loadItem = value;
4141

42-
return SerializationUtility.DeserializeValue<T>(loadItem.Value, DATA_FORMAT, _deserializationContext);
42+
return Deserialize<T>(loadItem.Value);
4343
}
4444

4545
public static bool TryLoad<T>(string key, out T data)
@@ -49,7 +49,7 @@ public static bool TryLoad<T>(string key, out T data)
4949
if (_data.TryGetValue(key, out var value))
5050
{
5151
var loadItem = value;
52-
data = SerializationUtility.DeserializeValue<T>(loadItem.Value, DATA_FORMAT, _deserializationContext);
52+
data = Deserialize<T>(loadItem.Value);
5353
hasKey = true;
5454
}
5555
else
@@ -94,14 +94,22 @@ private static void Setup()
9494
GeneratePath();
9595

9696
LoadFile();
97-
Application.quitting += SaveFile;
97+
}
98+
99+
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
100+
private static void CreateObserver()
101+
{
102+
var obj = new GameObject("ApplicationStateObserver");
103+
var observer = obj.AddComponent<ApplicationStateObserver>();
104+
UnityEngine.Object.DontDestroyOnLoad(obj);
105+
observer.OnQuit += SaveFile;
98106
}
99107

100108
private static void SaveFile()
101109
{
102110
FileSaving?.Invoke();
103111

104-
var bytes = SerializationUtility.SerializeValue(_data, DATA_FORMAT, _serializationContext);
112+
var bytes = Serialize(_data);
105113
File.WriteAllBytes(_savePath, bytes);
106114
}
107115

@@ -113,15 +121,31 @@ private static void LoadFile()
113121
fileStream?.Close();
114122
}
115123

116-
var loadBytes = File.ReadAllBytes(_savePath);
117-
_data = SerializationUtility.DeserializeValue<Dictionary<string, Item>>(loadBytes, DATA_FORMAT, _deserializationContext);
124+
var bytes = File.ReadAllBytes(_savePath);
125+
_data = Deserialize<Dictionary<string, Item>>(bytes);
118126

119127
if (_data == null)
120128
_data = new Dictionary<string, Item>(INITIAL_SIZE);
121129
}
122130

123131
private static void GeneratePath() =>
124132
_savePath = Path.Combine(Application.persistentDataPath, $"{FILE_NAME}_{_currentProfileIndex}.data");
133+
134+
private static byte[] Serialize<T>(T data)
135+
{
136+
var bytes = SerializationUtility.SerializeValue(data, DATA_FORMAT, _serializationContext);
137+
_serializationContext.ResetToDefault();
138+
139+
return bytes;
140+
}
141+
142+
private static T Deserialize<T>(byte[] bytes)
143+
{
144+
var data = SerializationUtility.DeserializeValue<T>(bytes, DATA_FORMAT, _deserializationContext);
145+
_deserializationContext.Reset();
146+
147+
return data;
148+
}
125149
}
126150
}
127151

0 commit comments

Comments
 (0)