Skip to content

Commit 9aa33b5

Browse files
authored
Merge pull request #14 from IntoTheDev/messagepack_v3
MessagePack v3
2 parents aa84382 + 65d831b commit 9aa33b5

File tree

8 files changed

+220
-176
lines changed

8 files changed

+220
-176
lines changed

README.md

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
# Save System for Unity
22

3-
### TODO
4-
- [x] Assets References
5-
- [x] AOT support
6-
- [x] Test with Desktop and Mobile builds. Mono and IL2CPP. (Finally managed to get my hands on this branch again. Testing a few things for now. I'll release this branch in 2023 Q1)
7-
8-
93
## Features
104
- Can save pretty much everything (Vector, Quaternion, Array, List, Class, Struct, etc)
115
- Can save assets references
126
- Easy to use
137
- Fast in terms of performance. Even simple int saving way more faster than ```PlayerPrefs.SetInt()```. Performance test at the end of README
14-
- Save files are encrypted
8+
- Automatic save file backup
159
- Extensible
1610

1711
## How to Install
@@ -92,37 +86,7 @@ public class Player : MonoBehaviour
9286

9387
### AOT platforms
9488

95-
[See](https://github.com/neuecc/MessagePack-CSharp#aot-code-generation-support-for-unityxamarin)
96-
97-
```csharp
98-
using MessagePack;
99-
using MessagePack.Resolvers;
100-
using MessagePack.Unity;
101-
using MessagePack.Unity.Extension;
102-
using Serializer;
103-
using ToolBox.Serialization;
104-
using UnityEngine;
105-
106-
public static class MessagePackStartup
107-
{
108-
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
109-
private static void Setup()
110-
{
111-
StaticCompositeResolver.Instance.Register(
112-
GeneratedResolver.Instance,
113-
UnityBlitResolver.Instance,
114-
UnityResolver.Instance,
115-
StandardResolver.Instance,
116-
DataSerializerResolver.Instance
117-
);
118-
119-
var options = ContractlessStandardResolverAllowPrivate.Options.WithResolver(StaticCompositeResolver.Instance);
120-
121-
DataSerializer.Options = options;
122-
MessagePackSerializer.DefaultOptions = options;
123-
}
124-
}
125-
```
89+
You don’t need to do anything for it to work with AOT/IL2CPP, but if any issues come up, check this out: [AOT code generation support for Unity/Xamarin](https://github.com/neuecc/MessagePack-CSharp#aot-code-generation-support-for-unityxamarin)
12690

12791
## Performance test
12892

Runtime/AssetFormatter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ public class AssetFormatter<T> : IMessagePackFormatter<T> where T : Object
88
{
99
public void Serialize(ref MessagePackWriter writer, T value, MessagePackSerializerOptions options)
1010
{
11-
DataSerializer.Container.TryGetId(value, out var id);
11+
DataSerializer.Container.TryResolveId(value, out var id);
1212

1313
writer.WriteUInt16(id);
1414
}
1515

1616
public T Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
1717
{
18-
var resolved = DataSerializer.Container.TryGetObject(reader.ReadUInt16(), out var value);
18+
var resolved = DataSerializer.Container.TryResolveReference(reader.ReadUInt16(), out var value);
1919

20-
return resolved ? (T)value : null;
20+
return resolved ? value as T : null;
2121
}
2222
}
23-
}
23+
}

Runtime/AssetsContainer.cs

Lines changed: 103 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -8,109 +8,109 @@
88

99
namespace ToolBox.Serialization
1010
{
11-
public sealed class AssetsContainer : ScriptableObject
12-
{
13-
[SerializeField] private Object[] _savedAssets;
14-
[SerializeField] private string[] _paths;
15-
16-
public bool TryGetObject(ushort id, out Object entry)
17-
{
18-
entry = null;
19-
20-
if (id == 0 || id >= _savedAssets.Length)
21-
{
22-
return false;
23-
}
24-
25-
entry = _savedAssets[id];
26-
return true;
27-
}
28-
29-
public bool TryGetId(Object value, out ushort id)
30-
{
31-
id = 0;
32-
33-
for (ushort i = 1; i < _savedAssets.Length; i++)
34-
{
35-
if (_savedAssets[i] != value)
36-
{
37-
continue;
38-
}
39-
40-
id = i;
41-
return true;
42-
}
43-
44-
return false;
45-
}
11+
public sealed class AssetsContainer : ScriptableObject
12+
{
13+
[SerializeField] private Object[] _savedAssets;
14+
[SerializeField] private string[] _paths;
4615

4716
#if UNITY_EDITOR
48-
public void LoadAssets()
49-
{
50-
if (_paths == null)
51-
{
52-
return;
53-
}
54-
55-
_paths = _paths.Where(x => !string.IsNullOrEmpty(x) && AssetDatabase.IsValidFolder(x)).ToArray();
56-
57-
if (_paths.Length == 0)
58-
{
59-
return;
60-
}
61-
62-
// ReSharper disable once UseArrayEmptyMethod
63-
_savedAssets ??= new Object[0];
64-
65-
var assets = AssetDatabase
66-
.FindAssets("t:Object", _paths)
67-
.Select(AssetDatabase.GUIDToAssetPath)
68-
.Select(AssetDatabase.LoadAssetAtPath<Object>)
69-
.Where(x =>
70-
{
71-
var fileNamespace = x.GetType().Namespace;
72-
73-
return x != null && (fileNamespace == null || !fileNamespace.Contains("UnityEditor"));
74-
})
75-
.ToList();
76-
77-
var newEntries = new List<Object>();
78-
79-
foreach (var asset in assets)
80-
{
81-
if (!TryGetId(asset, out _))
82-
{
83-
newEntries.Add(asset);
84-
}
85-
86-
var children = AssetDatabase.LoadAllAssetRepresentationsAtPath(AssetDatabase.GetAssetPath(asset));
87-
88-
foreach (var child in children)
89-
{
90-
if (TryGetId(child, out _))
91-
{
92-
continue;
93-
}
94-
95-
newEntries.Add(child);
96-
}
97-
}
98-
99-
ArrayUtility.AddRange(ref _savedAssets, newEntries.ToArray());
100-
101-
if (_savedAssets.Length == 0 || _savedAssets[0] != null)
102-
{
103-
ArrayUtility.Insert(ref _savedAssets, 0, null);
104-
}
105-
106-
EditorUtility.SetDirty(this);
107-
}
108-
109-
public void Clear()
110-
{
111-
_savedAssets = null;
112-
EditorUtility.SetDirty(this);
113-
}
17+
public void LoadAssets()
18+
{
19+
if (_paths == null)
20+
{
21+
return;
22+
}
23+
24+
_paths = _paths.Where(x => !string.IsNullOrEmpty(x) && AssetDatabase.IsValidFolder(x)).ToArray();
25+
26+
if (_paths.Length == 0)
27+
{
28+
return;
29+
}
30+
31+
// ReSharper disable once UseArrayEmptyMethod
32+
_savedAssets ??= new Object[0];
33+
34+
var assets = AssetDatabase
35+
.FindAssets("t:Object", _paths)
36+
.Select(AssetDatabase.GUIDToAssetPath)
37+
.Select(AssetDatabase.LoadAssetAtPath<Object>)
38+
.Where(x =>
39+
{
40+
var fileNamespace = x.GetType().Namespace;
41+
42+
return x != null && (fileNamespace == null || !fileNamespace.Contains("UnityEditor"));
43+
})
44+
.ToList();
45+
46+
var newEntries = new List<Object>();
47+
48+
foreach (var asset in assets)
49+
{
50+
if (!TryResolveId(asset, out _))
51+
{
52+
newEntries.Add(asset);
53+
}
54+
55+
var children = AssetDatabase.LoadAllAssetRepresentationsAtPath(AssetDatabase.GetAssetPath(asset));
56+
57+
foreach (var child in children)
58+
{
59+
if (TryResolveId(child, out _))
60+
{
61+
continue;
62+
}
63+
64+
newEntries.Add(child);
65+
}
66+
}
67+
68+
ArrayUtility.AddRange(ref _savedAssets, newEntries.ToArray());
69+
70+
if (_savedAssets.Length == 0 || _savedAssets[0] != null)
71+
{
72+
ArrayUtility.Insert(ref _savedAssets, 0, null);
73+
}
74+
75+
EditorUtility.SetDirty(this);
76+
}
77+
78+
public void Clear()
79+
{
80+
_savedAssets = null;
81+
EditorUtility.SetDirty(this);
82+
}
11483
#endif
115-
}
116-
}
84+
85+
public bool TryResolveReference(ushort id, out Object entry)
86+
{
87+
entry = null;
88+
89+
if (id == 0 || id >= _savedAssets.Length)
90+
{
91+
return false;
92+
}
93+
94+
entry = _savedAssets[id];
95+
return true;
96+
}
97+
98+
public bool TryResolveId(Object value, out ushort id)
99+
{
100+
id = 0;
101+
102+
for (ushort i = 1; i < _savedAssets.Length; i++)
103+
{
104+
if (_savedAssets[i] != value)
105+
{
106+
continue;
107+
}
108+
109+
id = i;
110+
return true;
111+
}
112+
113+
return false;
114+
}
115+
}
116+
}

0 commit comments

Comments
 (0)