Skip to content

Commit 6e6d172

Browse files
author
Benjamin Buß
committed
Added lookup table for strings
1 parent ab8b2ca commit 6e6d172

File tree

8 files changed

+203
-59
lines changed

8 files changed

+203
-59
lines changed

Assets/Kekser/Example/SaveLoadExample.unity

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ Transform:
212212
m_LocalRotation: {x: 0.40821788, y: -0.23456968, z: 0.10938163, w: 0.8754261}
213213
m_LocalPosition: {x: 0, y: 3, z: 0}
214214
m_LocalScale: {x: 1, y: 1, z: 1}
215+
m_ConstrainProportionsScale: 0
215216
m_Children: []
216217
m_Father: {fileID: 0}
217218
m_RootOrder: 1
@@ -245,7 +246,7 @@ MonoBehaviour:
245246
m_Script: {fileID: 11500000, guid: f283094bf0d401644a182287f3f5bd66, type: 3}
246247
m_Name:
247248
m_EditorClassIdentifier:
248-
_savePath: C:/Users/benja/AppData/LocalLow/DefaultCompany/save-system/save.sav
249+
_savePath: C:\save-test\save.sav
249250
--- !u!4 &1006973920
250251
Transform:
251252
m_ObjectHideFlags: 0
@@ -256,6 +257,7 @@ Transform:
256257
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
257258
m_LocalPosition: {x: 0, y: 0, z: 0}
258259
m_LocalScale: {x: 1, y: 1, z: 1}
260+
m_ConstrainProportionsScale: 0
259261
m_Children: []
260262
m_Father: {fileID: 0}
261263
m_RootOrder: 3
@@ -304,6 +306,7 @@ MeshRenderer:
304306
m_CastShadows: 1
305307
m_ReceiveShadows: 1
306308
m_DynamicOccludee: 1
309+
m_StaticShadowCaster: 0
307310
m_MotionVectors: 1
308311
m_LightProbeUsage: 1
309312
m_ReflectionProbeUsage: 1
@@ -352,6 +355,7 @@ Transform:
352355
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
353356
m_LocalPosition: {x: 0, y: 0, z: 0}
354357
m_LocalScale: {x: 10, y: 10, z: 10}
358+
m_ConstrainProportionsScale: 0
355359
m_Children: []
356360
m_Father: {fileID: 0}
357361
m_RootOrder: 2
@@ -435,6 +439,7 @@ Transform:
435439
m_LocalRotation: {x: -0.08300377, y: 0.87206966, z: -0.158969, w: -0.45533922}
436440
m_LocalPosition: {x: 19.377333, y: 9.341052, z: 9.41688}
437441
m_LocalScale: {x: 1, y: 1, z: 1}
442+
m_ConstrainProportionsScale: 0
438443
m_Children: []
439444
m_Father: {fileID: 0}
440445
m_RootOrder: 0
@@ -466,6 +471,7 @@ Transform:
466471
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
467472
m_LocalPosition: {x: 0, y: 0, z: 0}
468473
m_LocalScale: {x: 1, y: 1, z: 1}
474+
m_ConstrainProportionsScale: 0
469475
m_Children: []
470476
m_Father: {fileID: 0}
471477
m_RootOrder: 4

Assets/Kekser/SaveSystem/SaveBuffer.cs

Lines changed: 34 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,30 @@
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.Reflection;
5+
using Kekser.SaveSystem.Utils;
56

67
namespace Kekser.SaveSystem
78
{
9+
public class LookUpSaveBuffer : SaveBuffer
10+
{
11+
public LookUpSaveBuffer() : base()
12+
{
13+
}
14+
15+
public LookUpSaveBuffer(byte[] data) : base(data)
16+
{
17+
}
18+
19+
public override byte[] Data
20+
{
21+
get => _lookUpTable.PrependHeader(base.Data);
22+
set => base.Data = _lookUpTable.RemoveHeader(value);
23+
}
24+
}
25+
826
public class SaveBuffer
927
{
10-
//TODO: Add a type lookup table to save space
28+
protected static LookUpTable _lookUpTable = new LookUpTable();
1129

1230
public SaveBuffer()
1331
{
@@ -18,48 +36,17 @@ public SaveBuffer(byte[] data)
1836
Data = data;
1937
}
2038

21-
private int _length = 0;
2239
private int _offset = 0;
2340

24-
private byte[] _data = new byte[1024];
41+
private DynamicArray _data = new DynamicArray();
2542

26-
public byte[] Data
43+
public virtual byte[] Data
2744
{
28-
get
29-
{
30-
byte[] result = new byte[_length];
31-
Buffer.BlockCopy(_data, 0, result, 0, _length);
32-
return result;
33-
}
45+
get => _data.Data;
3446
set
3547
{
36-
if (value == null || value.Length == 0)
37-
{
38-
_data = new byte[1024];
39-
_length = 0;
40-
_offset = 0;
41-
}
42-
else
43-
{
44-
EnsureCapacity(value.Length);
45-
Buffer.BlockCopy(value, 0, _data, 0, value.Length);
46-
_length = value.Length;
47-
_offset = 0;
48-
}
49-
}
50-
}
51-
52-
private void EnsureCapacity(int requiredLength)
53-
{
54-
if (requiredLength > _data.Length)
55-
{
56-
int newLength = _data.Length;
57-
while (newLength < requiredLength)
58-
newLength <<= 1;
59-
60-
byte[] newData = new byte[newLength];
61-
Buffer.BlockCopy(_data, 0, newData, 0, _length);
62-
_data = newData;
48+
_data.Data = value;
49+
_offset = 0;
6350
}
6451
}
6552

@@ -72,31 +59,23 @@ public void SaveBytes(byte[] value)
7259
}
7360

7461
SaveInt(value.Length);
75-
EnsureCapacity(_length + value.Length);
76-
Buffer.BlockCopy(value, 0, _data, _length, value.Length);
77-
_length += value.Length;
62+
_data.AddBytes(value);
7863
}
7964
public void SaveInt(int value)
8065
{
81-
EnsureCapacity(_length + sizeof(int));
82-
Buffer.BlockCopy(BitConverter.GetBytes(value), 0, _data, _length, sizeof(int));
83-
_length += sizeof(int);
66+
_data.AddBytes(BitConverter.GetBytes(value));
8467
}
8568
public void SaveFloat(float value)
8669
{
87-
EnsureCapacity(_length + sizeof(float));
88-
Buffer.BlockCopy(BitConverter.GetBytes(value), 0, _data, _length, sizeof(float));
89-
_length += sizeof(float);
70+
_data.AddBytes(BitConverter.GetBytes(value));
9071
}
9172
public void SaveBool(bool value)
9273
{
93-
EnsureCapacity(_length + sizeof(bool));
94-
Buffer.BlockCopy(BitConverter.GetBytes(value), 0, _data, _length, sizeof(bool));
95-
_length += sizeof(bool);
74+
_data.AddBytes(BitConverter.GetBytes(value));
9675
}
9776
public void SaveString(string value)
9877
{
99-
SaveBytes(System.Text.Encoding.UTF8.GetBytes(value));
78+
SaveInt(_lookUpTable.Add(value));
10079
}
10180
public void SaveVector2(UnityEngine.Vector2 value)
10281
{
@@ -252,32 +231,31 @@ public void Save(object obj)
252231
public byte[] LoadBytes()
253232
{
254233
int length = LoadInt();
255-
byte[] value = new byte[length];
256-
Buffer.BlockCopy(_data, _offset, value, 0, length);
234+
byte[] value = _data.GetBytes(_offset, length);
257235
_offset += length;
258236
return value;
259237
}
260238
public int LoadInt()
261239
{
262-
int value = BitConverter.ToInt32(_data, _offset);
240+
int value = BitConverter.ToInt32(_data.RawData, _offset);
263241
_offset += sizeof(int);
264242
return value;
265243
}
266244
public float LoadFloat()
267245
{
268-
float value = BitConverter.ToSingle(_data, _offset);
246+
float value = BitConverter.ToSingle(_data.RawData, _offset);
269247
_offset += sizeof(float);
270248
return value;
271249
}
272250
public bool LoadBool()
273251
{
274-
bool value = BitConverter.ToBoolean(_data, _offset);
252+
bool value = BitConverter.ToBoolean(_data.RawData, _offset);
275253
_offset += sizeof(bool);
276254
return value;
277255
}
278256
public string LoadString()
279257
{
280-
return System.Text.Encoding.UTF8.GetString(LoadBytes());
258+
return _lookUpTable.Get(LoadInt());
281259
}
282260
public UnityEngine.Vector2 LoadVector2()
283261
{

Assets/Kekser/SaveSystem/SaveLoadManager.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ namespace Kekser.SaveSystem
1111
{
1212
public class SaveLoadManager : MonoBehaviour
1313
{
14+
private static void EnsureFolderExists(string path)
15+
{
16+
string directory = Path.GetDirectoryName(path);
17+
if (string.IsNullOrEmpty(directory))
18+
return;
19+
if (Directory.Exists(directory))
20+
return;
21+
Directory.CreateDirectory(directory);
22+
}
23+
1424
public static async Task<bool> Save(string file)
1525
{
1626
try
@@ -19,8 +29,10 @@ public static async Task<bool> Save(string file)
1929
dataObject.Add("Scene", new DataElement(SceneManager.GetActiveScene().name));
2030
SaveAttributeManager.Save(dataObject);
2131

22-
SaveBuffer saveData = new SaveBuffer();
32+
LookUpSaveBuffer saveData = new LookUpSaveBuffer();
2333
dataObject.DataSerialize(saveData);
34+
35+
EnsureFolderExists(file);
2436
File.WriteAllBytes(file, Compress(saveData.Data));
2537
return true;
2638
}
@@ -38,7 +50,7 @@ public static async Task<bool> Load(string file)
3850
try
3951
{
4052
byte[] data = Decompress(File.ReadAllBytes(file));
41-
SaveBuffer saveData = new SaveBuffer(data);
53+
LookUpSaveBuffer saveData = new LookUpSaveBuffer(data);
4254

4355
DataObject dataObject = new DataObject();
4456
dataObject.DataDeserialize(saveData);

Assets/Kekser/SaveSystem/Utils.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System;
2+
3+
namespace Kekser.SaveSystem.Utils
4+
{
5+
public class DynamicArray
6+
{
7+
private int _capacity = 0;
8+
9+
private int _length = 0;
10+
11+
private byte[] _data;
12+
13+
public DynamicArray()
14+
{
15+
_capacity = 1024;
16+
_data = new byte[1024];
17+
}
18+
19+
public DynamicArray(int capacity)
20+
{
21+
_capacity = capacity;
22+
_data = new byte[capacity];
23+
}
24+
25+
private void EnsureCapacity(int requiredLength)
26+
{
27+
if (requiredLength > _data.Length)
28+
{
29+
int newLength = _data.Length;
30+
while (newLength < requiredLength)
31+
newLength <<= 1;
32+
33+
byte[] newData = new byte[newLength];
34+
Buffer.BlockCopy(_data, 0, newData, 0, _length);
35+
_data = newData;
36+
}
37+
}
38+
39+
public byte[] RawData => _data;
40+
41+
public byte[] Data
42+
{
43+
get
44+
{
45+
byte[] result = new byte[_length];
46+
Buffer.BlockCopy(_data, 0, result, 0, _length);
47+
return result;
48+
}
49+
set
50+
{
51+
if (value == null || value.Length == 0)
52+
{
53+
_data = new byte[_capacity];
54+
_length = 0;
55+
}
56+
else
57+
{
58+
EnsureCapacity(value.Length);
59+
Buffer.BlockCopy(value, 0, _data, 0, value.Length);
60+
_length = value.Length;
61+
}
62+
}
63+
}
64+
65+
public void AddBytes(byte[] value)
66+
{
67+
EnsureCapacity(_length + value.Length);
68+
Buffer.BlockCopy(value, 0, _data, _length, value.Length);
69+
_length += value.Length;
70+
}
71+
72+
public byte[] GetBytes(int index, int length)
73+
{
74+
byte[] value = new byte[length];
75+
Buffer.BlockCopy(_data, index, value, 0, length);
76+
return value;
77+
}
78+
}
79+
}

Assets/Kekser/SaveSystem/Utils/DynamicArray.cs.meta

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

0 commit comments

Comments
 (0)