Skip to content

Commit 1e143c2

Browse files
committed
update
1 parent 1be5944 commit 1e143c2

File tree

11 files changed

+139
-81
lines changed

11 files changed

+139
-81
lines changed

GAIL.Storage/BaseStorage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static Logger Logger { get {
4646
/// <returns>True if it succeeded.</returns>
4747
public virtual bool Load(Stream stream, bool shouldCloseStream = true) {
4848
try {
49-
using Serializer parser = new(stream, shouldCloseStream);
49+
using Parser parser = new(stream, shouldCloseStream);
5050
Parse(parser, Formatter);
5151
} catch (Exception e) {
5252
Logger.LogError("Failed to load storage file:");

GAIL.Storage/Hierarchy/ChildNode.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ public abstract class ChildNode : IChildNode {
77
/// <summary>
88
/// Create a node with a key, value and a parent.
99
/// </summary>
10-
/// <param name="key">The key</param>
11-
/// <param name="parent"></param>
12-
/// <returns></returns>
10+
/// <param name="key">The key.</param>
11+
/// <param name="parent">The parent of this child.</param>
1312
public ChildNode(string key, IParentNode parent) {
1413
Key = key;
1514
SetParent(parent);
@@ -40,7 +39,6 @@ public virtual void ClearParent() {
4039
if (previousParent?.Children.ContainsKey(Key) ?? false) {
4140
previousParent.RemoveChild(this);
4241
}
43-
4442
}
4543

4644
/// <inheritdoc/>

GAIL.Storage/LookupStorage.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using GAIL.Serializing.Formatters;
33
using GAIL.Serializing.Streams;
44
using GAIL.Storage.Hierarchy;
5+
using GAIL.Storage.Members;
6+
using GAIL.Storage.Streams;
57

68
namespace GAIL.Storage;
79

@@ -16,17 +18,18 @@ public override void Parse(Parser parser, IFormatter? formatter = null) {
1618
parser.Decode((p) => {
1719
Parse(p, null);
1820
}, formatter);
21+
} else {
22+
LookupTable = parser.ReadSerializable(LookupTable.Info);
1923
}
20-
LookupTable = parser.ReadSerializable(LookupTable.Info);
24+
// NOTE: Not parsing the following, because of the lookup table.
2125
}
22-
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);
26+
private void WriteParent(Serializer serializer, IParentNode parent) {
27+
foreach (IChildNode c in parent.Children.Values) {
28+
if (c is IParentNode p) {
29+
WriteParent(serializer, p);
30+
} else if (c is IField field) {
31+
serializer.WriteMember(field, false);
2732
}
28-
} else if (child is ISerializable serializable) {
29-
serializer.WriteSerializable(serializable);
3033
}
3134
}
3235
/// <inheritdoc/>
@@ -35,10 +38,9 @@ public override void Serialize(Serializer serializer, IFormatter? formatter = nu
3538
serializer.Encode((s) => {
3639
Serialize(s, null);
3740
}, formatter);
38-
}
39-
serializer.WriteSerializable(LookupTable);
40-
foreach (IChildNode child in children.Values) {
41-
41+
} else {
42+
serializer.WriteSerializable(LookupTable);
43+
WriteParent(serializer, this);
4244
}
4345
}
4446
}

GAIL.Storage/LookupTable.cs

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,49 @@
11
using GAIL.Serializing;
22
using GAIL.Serializing.Formatters;
33
using GAIL.Serializing.Streams;
4+
using GAIL.Storage.Hierarchy;
5+
using GAIL.Storage.Members;
6+
using GAIL.Storage.Streams;
47

58
namespace GAIL.Storage;
69

7-
public class LookupTable : ISerializable {
8-
private static ISerializable.Info<LookupTable>? info;
9-
public static ISerializable.Info<LookupTable> Info { get {
10-
info ??= ISerializable.CreateInfo<LookupTable>();
11-
return info;
12-
} }
10+
public class LookupTable {
1311

14-
private static Dictionary<string, uint> GenerateLookupTable(LookupStorage storage) {
12+
public Dictionary<string, IChildNode> lookup;
1513

16-
}
17-
18-
public readonly Dictionary<string, uint> lookup;
19-
20-
public LookupTable(Dictionary<string, uint> lookupDictionary) {
21-
lookup = lookupDictionary;
22-
}
23-
public LookupTable() : this([]) { }
24-
public LookupTable(LookupStorage storage) : this(GenerateLookupTable(storage)) { }
14+
public LookupTable() {
15+
lookup = [];
16+
}
2517

26-
private static KeyValuePair<string, uint> ReadPair(Parser p) {
18+
private static KeyValuePair<string, uint> ReadPair(Parser p) {
2719
return new(p.ReadString(), p.ReadUInt());
2820
}
29-
/// <inheritdoc/>
30-
public void Parse(Parser parser, IFormatter? formatter = null) {
31-
if (formatter != null) {
21+
public void Parse(Parser parser, IFormatter? formatter = null) {
22+
if (formatter != null) {
3223
parser.Decode((p) => {
33-
ReadPair(p);
24+
Parse(p, null);
3425
}, formatter);
3526
} else {
36-
ReadPair(p);
37-
}
38-
}
39-
40-
/// <inheritdoc/>
41-
public void Serialize(Serializer serializer, IFormatter? formatter = null) {
42-
throw new NotImplementedException();
43-
}
27+
Lookup = [];
28+
MemberType type;
29+
do {
30+
type = parser.ReadType();
31+
if (type == MemberType.LookupTable) {
32+
((IDictionary<string, uint>)Lookup).Add(ReadPair(parser));
33+
} else if (type != MemberType.End) {
34+
throw new InvalidDataException("A lookup table cannot contain any other type than LookupTable and End");
35+
}
36+
} while (type!=MemberType.End);
37+
}
38+
}
39+
public readonly KeyValuePair<string[], uint>[] lookup;
40+
public LookupStorage() {
41+
lookup = [];
42+
}
43+
public void WriteEntry(Serializer serializer, string[] id, uint location) {
44+
serializer.WriteSerializable()
45+
}
46+
public void WriteEnd(Serializer serializer) {
47+
serializer.WriteType(MemberType.End);
48+
}
4449
}

GAIL.Storage/Members/Container.cs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
using GAIL.Serializing;
12
using GAIL.Serializing.Formatters;
23
using GAIL.Serializing.Streams;
34
using GAIL.Storage.Hierarchy;
5+
using GAIL.Storage.Streams;
46

57
namespace GAIL.Storage.Members;
68

@@ -27,13 +29,32 @@ public Container(string key, IParentNode parent, Dictionary<string, IChildNode>?
2729
children = members??[];
2830
}
2931

30-
public void Parse(Parser parser, IFormatter? formatter = null)
31-
{
32-
throw new NotImplementedException();
32+
/// <inheritdoc/>
33+
public MemberType Type => MemberType.Container;
34+
35+
/// <inheritdoc/>
36+
public void Parse(Parser parser, bool hasKey, IFormatter? formatter = null) {
37+
if (formatter != null) {
38+
parser.Decode(p => Parse(p, hasKey), formatter);
39+
} else {
40+
if (hasKey) key = parser.ReadString();
41+
children = parser.ReadChildren(hasKey).ToDictionary(static x => x.Key, static x => x);
42+
}
43+
}
44+
/// <inheritdoc/>
45+
public void Serialize(Serializer serializer, bool hasKey, IFormatter? formatter = null) {
46+
if (formatter != null) {
47+
serializer.Encode(s => Serialize(s, hasKey), formatter);
48+
} else {
49+
if (hasKey) serializer.WriteString(key);
50+
serializer.WriteChildren([.. children.Values], hasKey);
51+
}
3352
}
3453

35-
public void Serialize(Serializer serializer, IFormatter? formatter = null)
36-
{
37-
throw new NotImplementedException();
54+
void ISerializable.Parse(Parser parser, IFormatter? formatter) {
55+
Parse(parser, true, formatter);
56+
}
57+
void ISerializable.Serialize(Serializer serializer, IFormatter? formatter) {
58+
Serialize(serializer, true, formatter);
3859
}
3960
}

GAIL.Storage/Members/Field.cs

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,42 @@ public interface IField : IChildNode, ISerializable {
1616
/// </summary>
1717
[Pure]
1818
public MemberType Type { get; }
19+
20+
/// <summary>
21+
/// Turns this class into bytes.
22+
/// </summary>
23+
/// <param name="serializer">The serializer to write to.</param>
24+
/// <param name="hasKey">Whether the key should be written.</param>
25+
/// <param name="formatter">The formatter to use.</param>
26+
public void Serialize(Serializer serializer, bool hasKey, IFormatter? formatter = null);
27+
/// <summary>
28+
/// Creates this class from bytes.
29+
/// </summary>
30+
/// <param name="parser">The parser to read from.</param>
31+
/// <param name="hasKey">Whether the key should be read.</param>
32+
/// <param name="formatter">The formatter to use.</param>
33+
public void Parse(Parser parser, bool hasKey, IFormatter? formatter = null);
34+
}
35+
/// <summary>
36+
/// Implements some common methods of IField.
37+
/// </summary>
38+
/// <inheritdoc/>
39+
public abstract class Field(string key, IParentNode parent) : ChildNode(key, parent), IField {
40+
/// <inheritdoc/>
41+
public abstract MemberType Type { get; }
42+
/// <inheritdoc/>
43+
public abstract void Parse(Parser parser, bool hasKey, IFormatter? formatter = null);
44+
/// <inheritdoc/>
45+
public abstract void Serialize(Serializer serializer, bool hasKey, IFormatter? formatter = null);
46+
47+
void ISerializable.Parse(Parser parser, IFormatter? formatter) {
48+
Parse(parser, true, formatter);
49+
}
50+
void ISerializable.Serialize(Serializer serializer, IFormatter? formatter) {
51+
Serialize(serializer, true, formatter);
52+
}
1953
}
54+
2055
/// <summary>
2156
/// Represents a node in a storage file with an actual value.
2257
/// </summary>
@@ -49,36 +84,35 @@ public SerializableField(string key, ISerializable baseSerializable, IParentNode
4984
serializable = baseSerializable;
5085
}
5186

52-
/// <inheritdoc/>
53-
public void Parse(Parser parser, IFormatter? formatter = null) {
54-
Parse(parser, readKey:true, formatter);
87+
/// <inheritdoc/>
88+
public virtual MemberType Type => MemberType.Custom;
89+
90+
/// <inheritdoc/>
91+
public void Parse(Parser parser, IFormatter? formatter = null) {
92+
Parse(parser, true, formatter);
5593
}
5694

5795
/// <inheritdoc/>
58-
public void Parse(Parser parser, bool readKey = true, IFormatter? formatter = null) {
96+
public void Parse(Parser parser, bool hasKey, IFormatter? formatter = null) {
5997
if (formatter != null) {
60-
parser.Decode((p) => {
61-
Parse(p, readKey, null);
62-
}, formatter);
98+
parser.Decode(p => Parse(p, hasKey, null), formatter);
6399
} else {
64-
if (readKey) key = parser.ReadString();
100+
if (hasKey) key = parser.ReadString();
65101
serializable.Parse(parser, null);
66102
}
67103
}
68104

69105
/// <inheritdoc/>
70106
public void Serialize(Serializer serializer, IFormatter? formatter = null) {
71-
Serialize(serializer, writeKey:true, formatter);
107+
Serialize(serializer, true, formatter);
72108
}
73109

74110
/// <inheritdoc/>
75-
public void Serialize(Serializer serializer, bool writeKey = true, IFormatter? formatter = null) {
111+
public void Serialize(Serializer serializer, bool hasKey, IFormatter? formatter = null) {
76112
if (formatter != null) {
77-
serializer.Encode((s) => {
78-
Serialize(s, writeKey, null);
79-
}, formatter);
113+
serializer.Encode(s => Serialize(s, hasKey, null), formatter);
80114
} else {
81-
if (writeKey) serializer.WriteString(key);
115+
if (hasKey) serializer.WriteString(key);
82116
serializable.Serialize(serializer, null);
83117
}
84118
}
@@ -87,13 +121,11 @@ public void Serialize(Serializer serializer, bool writeKey = true, IFormatter? f
87121
/// Represents a basic field implementation using a serializable with a value.
88122
/// </summary>
89123
public class SerializableField<T> : SerializableField, IField<T> {
90-
private ISerializable<T>? castedSerializable;
91124
/// <summary>
92125
/// The underlying serializable.
93126
/// </summary>
94127
public ISerializable<T> Serializable { get {
95-
castedSerializable ??= (ISerializable<T>)serializable;
96-
return castedSerializable;
128+
return (ISerializable<T>)serializable;
97129
} }
98130
/// <summary>
99131
/// Creates a new serializable field.

GAIL.Storage/Members/ListField.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections;
2+
using System.Security.Cryptography.X509Certificates;
23
using GAIL.Serializing;
34
using GAIL.Storage.Hierarchy;
45

@@ -35,6 +36,9 @@ public static ISerializable.Info<ListField<T>> CreateInfo(ISerializable.Info typ
3536
/// <param name="info">The info of the values to use.</param>
3637
/// <param name="parent"></param>
3738
public ListField(string key, List<T> values, ISerializable.Info info, IParentNode parent) : base(key, new ListSerializable<T>(values, info), parent) { }
39+
40+
/// <inheritdoc/>
41+
public override MemberType Type => MemberType.List;
3842
/// <inheritdoc/>
3943
public T this[int index] { get => Serializable.Value[index]; set => Serializable.Value[index] = value; }
4044
/// <inheritdoc/>

GAIL.Storage/Members/MemberType.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ public enum MemberType : byte {
44
End,
55
Container,
66
List,
7-
Serializable
7+
LookupTable,
8+
Custom
89
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// TODO: Make some ??

GAIL.Storage/Streams/ParserExtensions.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,12 @@ public static MemberType ReadType(this Parser parser) {
2121
/// </summary>
2222
/// <param name="parser">The parser to read from.</param>
2323
/// <param name="hasKey">True if there is a key to read.</param>
24-
/// <returns>The new parsed member, null if it is an end.</returns>
24+
/// <returns>The new parsed member.</returns>
2525
public static IChildNode? ReadMember(this Parser parser, bool hasKey = true) {
2626
MemberType type = ReadType(parser);
27-
string key = "";
28-
if (hasKey) {
29-
key = parser.ReadString();
30-
}
27+
// TODO: Create member (MemberInfo?).
3128

32-
return parser.ReadSerializable(info) as IChildNode;
29+
return member;
3330
}
3431
/// <summary>
3532
/// Reads multiple members.

0 commit comments

Comments
 (0)