Skip to content

Commit 1be5944

Browse files
committed
Mostly Storage
1 parent 1ed7ca6 commit 1be5944

File tree

17 files changed

+204
-332
lines changed

17 files changed

+204
-332
lines changed

GAIL.Networking/Container/Connection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static byte[] CreateID(IPEndPoint ip) {
1919
}
2020

2121
/// <summary>
22-
/// The 6-byte-long ID from the IP and port of the client.
22+
/// The ID from the IP and port of the client.
2323
/// </summary>
2424
public readonly byte[] ID;
2525
/// <summary>
@@ -68,7 +68,7 @@ internal Connection(TcpClient client) {
6868
throw new ArgumentException("Client is not connected", nameof(client));
6969
}
7070
IP = (client.Client.RemoteEndPoint as IPEndPoint)!;
71-
ID = CreateID(IP);
71+
ID = CreateID(IP);
7272
TcpClient = client;
7373
Stream = client.GetStream();
7474
}

GAIL.Networking/Packet/Packet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public virtual void Serialize(Serializer serializer) {
163163
} catch (Exception e) {
164164
throw new InvalidOperationException($"Failed at getting field {field.Property.Name} in packet ({PacketInfo.fullyQualifiedName})", e);
165165
}
166-
if (gainedValue is not IRawSerializable serializable) {
166+
if (gainedValue is not ISerializable serializable) {
167167
throw new InvalidOperationException($"Field {field.Property.Name} in {PacketInfo.fullyQualifiedName} is not a serializable");
168168
}
169169

GAIL.Networking/Streams/NetworkSerializer.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using GAIL.Serializing;
21
using GAIL.Serializing.Formatters;
32
using GAIL.Serializing.Streams;
43

@@ -40,12 +39,4 @@ public void WritePacket(Packet packet, IFormatter? formatter = null) {
4039
}
4140

4241
}
43-
/// <inheritdoc/>
44-
public override void Dispose() {
45-
if (Disposed) { return; }
46-
47-
base.Dispose();
48-
49-
GC.SuppressFinalize(this);
50-
}
5142
}

GAIL.Serializing/Streams/Parser.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,7 @@ public virtual void Dispose() {
163163

164164
Disposed = true;
165165

166-
if (!ShouldCloseStream) { return; }
167-
168-
BaseStream.Close();
166+
if (!ShouldCloseStream) BaseStream.Close();
169167

170168
GC.SuppressFinalize(this);
171169
}

GAIL.Serializing/Streams/Serializer.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,11 @@ public virtual void WriteString(string value, IFormatter? formatter = null) {
112112
/// </remarks>
113113
public virtual void Dispose() {
114114
if (Disposed) { return; }
115-
GC.SuppressFinalize(this);
116115

117116
Disposed = true;
118117

119-
if (!ShouldCloseStream) { return; }
118+
if (ShouldCloseStream) BaseStream.Close();
120119

121-
BaseStream.Close();
120+
GC.SuppressFinalize(this);
122121
}
123122
}

GAIL.Storage/BaseStorage.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using GAIL.Serializing.Formatters;
33
using GAIL.Serializing.Streams;
44
using GAIL.Storage.Hierarchy;
5-
using GAIL.Storage.Streams;
65
using LambdaKit.Logging;
76

87
namespace GAIL.Storage;
@@ -47,7 +46,7 @@ public static Logger Logger { get {
4746
/// <returns>True if it succeeded.</returns>
4847
public virtual bool Load(Stream stream, bool shouldCloseStream = true) {
4948
try {
50-
using StorageParser parser = new(stream, shouldCloseStream);
49+
using Serializer parser = new(stream, shouldCloseStream);
5150
Parse(parser, Formatter);
5251
} catch (Exception e) {
5352
Logger.LogError("Failed to load storage file:");
@@ -78,7 +77,7 @@ public virtual bool Load(string filePath) {
7877
/// <returns>True if it succeeded.</returns>
7978
public virtual bool Save(Stream stream, bool shouldCloseStream = true) {
8079
try {
81-
using StorageSerializer serializer = new(stream, shouldCloseStream);
80+
using Serializer serializer = new(stream, shouldCloseStream);
8281
Serialize(serializer, Formatter);
8382
} catch (Exception e) {
8483
Logger.LogError("Failed to save storage file:");
@@ -103,9 +102,9 @@ public virtual bool Save(string filePath) {
103102
return Save(fs);
104103
}
105104

106-
/// <inheritdoc/>
107-
public abstract void Serialize(Serializer serializer, IFormatter? formatter = null);
105+
/// <inheritdoc/>
106+
public abstract void Serialize(Serializer serializer, IFormatter? formatter = null);
108107

109-
/// <inheritdoc/>
110-
public abstract void Parse(Parser parser, IFormatter? formatter = null);
108+
/// <inheritdoc/>
109+
public abstract void Parse(Parser parser, IFormatter? formatter = null);
111110
}

GAIL.Storage/LookupStorage.cs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,44 @@
1+
using GAIL.Serializing;
12
using GAIL.Serializing.Formatters;
23
using GAIL.Serializing.Streams;
4+
using GAIL.Storage.Hierarchy;
35

46
namespace GAIL.Storage;
57

68
/// <summary>
79
/// A file containing data and a lookup table.
810
/// </summary>
911
public class LookupStorage : BaseStorage {
10-
public LookupTable lookupTable;
11-
/// <inheritdoc/>
12-
public override void Parse(Parser parser, IFormatter? formatter = null) {
13-
lookupTable = parser.ReadSerializable(LookupTable.Info);
12+
public LookupTable LookupTable { get; private set; }
13+
/// <inheritdoc/>
14+
public override void Parse(Parser parser, IFormatter? formatter = null) {
15+
if (formatter != null) {
16+
parser.Decode((p) => {
17+
Parse(p, null);
18+
}, formatter);
19+
}
20+
LookupTable = parser.ReadSerializable(LookupTable.Info);
1421
}
1522

16-
/// <inheritdoc/>
17-
public override void Serialize(Serializer serializer, IFormatter? formatter = null) {
23+
private void WriteChild(Serializer serializer, IChildNode child) {
24+
if (child is IParentNode parent) {
25+
foreach (IChildNode c in parent.Children.Values) {
26+
WriteChild(serializer, c);
27+
}
28+
} else if (child is ISerializable serializable) {
29+
serializer.WriteSerializable(serializable);
30+
}
31+
}
32+
/// <inheritdoc/>
33+
public override void Serialize(Serializer serializer, IFormatter? formatter = null) {
34+
if (formatter != null) {
35+
serializer.Encode((s) => {
36+
Serialize(s, null);
37+
}, formatter);
38+
}
39+
serializer.WriteSerializable(LookupTable);
40+
foreach (IChildNode child in children.Values) {
41+
42+
}
1843
}
1944
}

GAIL.Storage/LookupTable.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,26 @@ private static Dictionary<string, uint> GenerateLookupTable(LookupStorage storag
1515

1616
}
1717

18-
public Dictionary<string, uint> lookup;
18+
public readonly Dictionary<string, uint> lookup;
1919

2020
public LookupTable(Dictionary<string, uint> lookupDictionary) {
2121
lookup = lookupDictionary;
2222
}
2323
public LookupTable() : this([]) { }
2424
public LookupTable(LookupStorage storage) : this(GenerateLookupTable(storage)) { }
2525

26+
private static KeyValuePair<string, uint> ReadPair(Parser p) {
27+
return new(p.ReadString(), p.ReadUInt());
28+
}
2629
/// <inheritdoc/>
2730
public void Parse(Parser parser, IFormatter? formatter = null) {
28-
throw new NotImplementedException();
31+
if (formatter != null) {
32+
parser.Decode((p) => {
33+
ReadPair(p);
34+
}, formatter);
35+
} else {
36+
ReadPair(p);
37+
}
2938
}
3039

3140
/// <inheritdoc/>

GAIL.Storage/Members/Container.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
using GAIL.Serializing.Formatters;
2+
using GAIL.Serializing.Streams;
13
using GAIL.Storage.Hierarchy;
24

35
namespace GAIL.Storage.Members;
46

57
/// <summary>
68
/// A container can contain more members with keys.
79
/// </summary>
8-
public sealed class Container : Node {
10+
public sealed class Container : Node, IField {
911
/// <summary>
1012
/// Creates a new container.
1113
/// </summary>
@@ -24,4 +26,14 @@ public Container(string key, Dictionary<string, IChildNode>? members = null) : b
2426
public Container(string key, IParentNode parent, Dictionary<string, IChildNode>? members = null) : base(key, parent) {
2527
children = members??[];
2628
}
29+
30+
public void Parse(Parser parser, IFormatter? formatter = null)
31+
{
32+
throw new NotImplementedException();
33+
}
34+
35+
public void Serialize(Serializer serializer, IFormatter? formatter = null)
36+
{
37+
throw new NotImplementedException();
38+
}
2739
}

GAIL.Storage/Members/Field.cs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Diagnostics.Contracts;
12
using GAIL.Serializing;
23
using GAIL.Serializing.Formatters;
34
using GAIL.Serializing.Streams;
@@ -7,23 +8,14 @@ namespace GAIL.Storage.Members;
78

89

910
/// <summary>
10-
/// Represents a node in a storage file with actual data.
11+
/// Represents a node in a storage file with data.
1112
/// </summary>
1213
public interface IField : IChildNode, ISerializable {
1314
/// <summary>
14-
/// Creates this class from bytes.
15-
/// </summary>
16-
/// <param name="parser">The parser to read from.</param>
17-
/// <param name="readKey">Whether the key of the field should be read.</param>
18-
/// <param name="formatter">The formatter to use.</param>
19-
public void Parse(Parser parser, bool readKey = true, IFormatter? formatter = null);
20-
/// <summary>
21-
/// Turns this class into bytes.
22-
/// </summary>
23-
/// <param name="serializer">The serializer to write to.</param>
24-
/// <param name="writeKey">Whether the key of the field should be written.</param>
25-
/// <param name="formatter">The formatter to use.</param>
26-
public void Serialize(Serializer serializer, bool writeKey = true, IFormatter? formatter = null);
15+
/// The type of this field.
16+
/// </summary>
17+
[Pure]
18+
public MemberType Type { get; }
2719
}
2820
/// <summary>
2921
/// Represents a node in a storage file with an actual value.

0 commit comments

Comments
 (0)