@@ -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>
89123public 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.
0 commit comments