Skip to content

Commit eeb19e7

Browse files
author
Vyacheslav
committed
Optimize save/load implimentation duplicated code
1 parent b8b8212 commit eeb19e7

File tree

4 files changed

+72
-89
lines changed

4 files changed

+72
-89
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using UnityEngine;
4+
5+
6+
namespace VoxelGame.Voxel
7+
{
8+
public abstract class SaveLoadChunk : ISaveLoadChunk
9+
{
10+
private readonly string pathWorld = "world";
11+
private readonly string pathChunkPrefix = "chunk_";
12+
private readonly string pathEnd = ".dat";
13+
14+
private readonly Queue<SavedChunkData> dataQueue = new Queue<SavedChunkData>();
15+
16+
protected SaveLoadChunk()
17+
{
18+
Directory.CreateDirectory(Path.Combine(Application.persistentDataPath, pathWorld));
19+
}
20+
21+
public void Save(int maxSavedChunks = 1)
22+
{
23+
for (int count = 0; count < maxSavedChunks && dataQueue.Count > 0; count++)
24+
{
25+
SavedChunkData data = dataQueue.Dequeue();
26+
SaveChunk(data);
27+
}
28+
}
29+
30+
public void AddToSaveQueue(SavedChunkData data)
31+
{
32+
dataQueue.Enqueue(data);
33+
}
34+
35+
public abstract SavedChunkData Load(Vector2Int coords);
36+
37+
public void DeleteAll()
38+
{
39+
string path = Path.Combine(Application.persistentDataPath, pathWorld);
40+
if (Directory.Exists(path))
41+
{
42+
Directory.Delete(path, true);
43+
}
44+
}
45+
46+
protected abstract void SaveChunk(SavedChunkData data);
47+
48+
protected string GetDestination(Vector2Int coords)
49+
{
50+
return Path.Combine(Application.persistentDataPath, pathWorld, pathChunkPrefix + coords.ToString() + pathEnd);
51+
}
52+
}
53+
}

VoxelGame_UnityProject/Assets/VoxelGame/Voxel/Scripts/SaveLoad/SaveLoadChunk.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.

VoxelGame_UnityProject/Assets/VoxelGame/Voxel/Scripts/SaveLoad/SaveLoadChunkBinary.cs

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,9 @@
88

99
namespace VoxelGame.Voxel
1010
{
11-
public class SaveLoadChunkBinary : ISaveLoadChunk
11+
public class SaveLoadChunkBinary : SaveLoadChunk
1212
{
13-
private readonly string pathWorld = "world";
14-
private readonly string pathChunkPrefix = "chunk_";
15-
private readonly string pathEnd = ".dat";
16-
17-
private readonly Queue<SavedChunkData> dataQueue = new Queue<SavedChunkData>();
18-
19-
private SaveLoadChunkBinary()
20-
{
21-
Directory.CreateDirectory(Path.Combine(Application.persistentDataPath, pathWorld));
22-
}
23-
24-
public void Save(int maxSavedChunks = 1)
25-
{
26-
for (int count = 0; count < maxSavedChunks && dataQueue.Count > 0; count++)
27-
{
28-
SavedChunkData data = dataQueue.Dequeue();
29-
SaveChunk(data);
30-
}
31-
}
32-
33-
public void AddToSaveQueue(SavedChunkData data)
34-
{
35-
dataQueue.Enqueue(data);
36-
}
37-
38-
public SavedChunkData Load(Vector2Int coords)
13+
public override SavedChunkData Load(Vector2Int coords)
3914
{
4015
string destination = GetDestination(coords);
4116

@@ -49,25 +24,11 @@ public SavedChunkData Load(Vector2Int coords)
4924
return null;
5025
}
5126

52-
private void SaveChunk(SavedChunkData data)
27+
protected override void SaveChunk(SavedChunkData data)
5328
{
5429
string destination = GetDestination(new Vector2Int(data.x, data.z));
5530
byte[] bytes = ByteArrayCompressor.Compress(SerializationUtility.SerializeValue(data, DataFormat.Binary));
5631
File.WriteAllBytes(destination, bytes);
5732
}
58-
59-
private string GetDestination(Vector2Int coords)
60-
{
61-
return Path.Combine(Application.persistentDataPath, pathWorld, pathChunkPrefix + coords.ToString() + pathEnd);
62-
}
63-
64-
public void DeleteAll()
65-
{
66-
string path = Path.Combine(Application.persistentDataPath, pathWorld);
67-
if (Directory.Exists(path))
68-
{
69-
Directory.Delete(path, true);
70-
}
71-
}
7233
}
7334
}
Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,33 @@
1-
using System;
2-
using System.Collections;
3-
using System.Collections.Generic;
41
using System.IO;
52
using UnityEngine;
63
using OdinSerializer;
74
using VoxelGame.Utilities;
85

96
namespace VoxelGame.Voxel
107
{
11-
public class SaveLoadChunkJSON : ISaveLoadChunk
8+
public class SaveLoadChunkJSON : SaveLoadChunk
129
{
13-
private readonly string pathWorld = "world1";
14-
private readonly string pathChunkPrefix = "chunk_";
15-
private readonly string pathEnd = ".txt";
16-
17-
private readonly Queue<SavedChunkData> dataQueue = new Queue<SavedChunkData>();
18-
19-
private SaveLoadChunkJSON()
20-
{
21-
Directory.CreateDirectory(Path.Combine(Application.persistentDataPath, pathWorld));
22-
}
23-
24-
public void Save(int maxSavedChunks = 1)
25-
{
26-
for (int count = 0; count < maxSavedChunks && dataQueue.Count > 0; count++)
27-
{
28-
SavedChunkData data = dataQueue.Dequeue();
29-
SaveChunk(data);
30-
}
31-
}
32-
33-
public void AddToSaveQueue(SavedChunkData data)
34-
{
35-
dataQueue.Enqueue(data);
36-
}
37-
38-
public SavedChunkData Load(Vector2Int coords)
10+
public override SavedChunkData Load(Vector2Int coords)
3911
{
4012
string destination = GetDestination(coords);
4113

4214
if (File.Exists(destination))
4315
{
4416
byte[] bytes = File.ReadAllBytes(destination);
45-
SavedChunkData loadedData = OdinSerializer.SerializationUtility.DeserializeValue<SavedChunkData>(bytes, DataFormat.JSON);
17+
SavedChunkData loadedData = SerializationUtility.DeserializeValue<SavedChunkData>(bytes, DataFormat.JSON);
4618
loadedData.map = ByteArrayCompressor.Decompress(loadedData.map);
4719
return loadedData;
4820
}
4921

5022
return null;
5123
}
5224

53-
private void SaveChunk(SavedChunkData data)
25+
protected override void SaveChunk(SavedChunkData data)
5426
{
5527
string destination = GetDestination(new Vector2Int(data.x, data.z));
5628
data.map = ByteArrayCompressor.Compress(data.map);
57-
byte[] bytes = OdinSerializer.SerializationUtility.SerializeValue(data, DataFormat.JSON);
29+
byte[] bytes = SerializationUtility.SerializeValue(data, DataFormat.JSON);
5830
File.WriteAllBytes(destination, bytes);
5931
}
60-
61-
private string GetDestination(Vector2Int coords)
62-
{
63-
return Path.Combine(Application.persistentDataPath, pathWorld, pathChunkPrefix + coords.ToString() + pathEnd);
64-
}
65-
66-
public void DeleteAll()
67-
{
68-
string path = Path.Combine(Application.persistentDataPath, pathWorld);
69-
if (Directory.Exists(path))
70-
{
71-
Directory.Delete(path, true);
72-
}
73-
}
7432
}
7533
}

0 commit comments

Comments
 (0)