Skip to content

Commit

Permalink
Make KVFile extend KVObject
Browse files Browse the repository at this point in the history
  • Loading branch information
xPaw committed Feb 16, 2024
1 parent ed1f368 commit c98ba96
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ namespace ValveKeyValue.Deserialization
{
interface IVisitingReader : IDisposable
{
void ReadObject();
KVHeader ReadHeader();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public KV1BinaryReader(Stream stream, IVisitationListener listener)
bool disposed;
KV1BinaryNodeType endMarker = KV1BinaryNodeType.End;

public void ReadObject()
public KVHeader ReadHeader()
{
Require.NotDisposed(nameof(KV1TextReader), disposed);

Expand All @@ -51,6 +51,8 @@ public void ReadObject()
{
throw new KeyValueException("Error while parsing binary KeyValues.", ex);
}

return new KVHeader();
}

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public KV1TextReader(TextReader textReader, IParsingVisitationListener listener,
readonly KV1TextReaderStateMachine stateMachine;
bool disposed;

public void ReadObject()
public KVHeader ReadHeader()
{
Require.NotDisposed(nameof(KV1TextReader), disposed);

Expand Down Expand Up @@ -103,6 +103,8 @@ public void ReadObject()
throw new ArgumentOutOfRangeException(nameof(token.TokenType), token.TokenType, "Unhandled token type.");
}
}

return new KVHeader();
}

public void Dispose()
Expand Down Expand Up @@ -224,7 +226,7 @@ void DoIncludeAndMerge(string filePath)

using var stream = OpenFileForInclude(filePath);
using var reader = new KV1TextReader(new StreamReader(stream), mergeListener, options);
reader.ReadObject();
reader.ReadHeader();
}

void DoIncludeAndAppend(string filePath)
Expand All @@ -233,7 +235,7 @@ void DoIncludeAndAppend(string filePath)

using var stream = OpenFileForInclude(filePath);
using var reader = new KV1TextReader(new StreamReader(stream), appendListener, options);
reader.ReadObject();
reader.ReadHeader();
}

Stream OpenFileForInclude(string filePath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public KV3TextReader(TextReader textReader, IParsingVisitationListener listener)
readonly KV3TextReaderStateMachine stateMachine;
bool disposed;

public void ReadObject()
public KVHeader ReadHeader()
{
Require.NotDisposed(nameof(KV3TextReader), disposed);

var file = tokenReader.ReadHeader();
var header = tokenReader.ReadHeader();

while (stateMachine.IsInObject)
{
Expand Down Expand Up @@ -108,6 +108,8 @@ public void ReadObject()
throw new ArgumentOutOfRangeException(nameof(token.TokenType), token.TokenType, "Unhandled token type.");
}
}

return header;
}

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ KVToken ReadBinaryBlob()
return new KVToken(KVTokenType.BinaryBlob, sb.ToString());
}

public KVFile ReadHeader()
public KVHeader ReadHeader()
{
var str = ReadToken();

Expand Down Expand Up @@ -222,7 +222,7 @@ public KVFile ReadHeader()
throw new InvalidDataException($"Unrecognized encoding specifier, expected '{Format.Generic}' but got '{format}'.");
}

return new KVFile
return new KVHeader
{
Encoding = encoding,
Format = format,
Expand Down
11 changes: 7 additions & 4 deletions ValveKeyValue/ValveKeyValue/KVFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace ValveKeyValue
{
public class KVFile
public class KVFile : KVObject
{
public Guid Encoding { get; set; }
public Guid Format { get; set; }
public KVObject Root { get; set; }
public KVHeader Header { get; }

public KVFile(KVHeader header, string name, KVValue value) : base(name, value)
{
Header = header;
}
}
}
10 changes: 10 additions & 0 deletions ValveKeyValue/ValveKeyValue/KVHeader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace ValveKeyValue
{
public class KVHeader
{
public Guid Encoding { get; set; }
public Guid Format { get; set; }
}
}
9 changes: 4 additions & 5 deletions ValveKeyValue/ValveKeyValue/KVSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,12 @@ public KVDocument Deserialize(Stream stream, KVSerializerOptions options = null)
Require.NotNull(stream, nameof(stream));
var builder = new KVObjectBuilder();

using (var reader = MakeReader(stream, builder, options ?? KVSerializerOptions.DefaultOptions))
{
reader.ReadObject();
}
using var reader = MakeReader(stream, builder, options ?? KVSerializerOptions.DefaultOptions);

var header = reader.ReadHeader();
var root = builder.GetObject();
return new KVDocument(root.Name, root.Value);

return new KVDocument(header, root.Name, root.Value); // TODO
}

/// <summary>
Expand Down

0 comments on commit c98ba96

Please sign in to comment.