Skip to content

Commit

Permalink
Added nullable support. (#1)
Browse files Browse the repository at this point in the history
* Added nullable support.
* .NET version.
  • Loading branch information
dil-rfuszenecker authored Jan 14, 2022
1 parent 4eb1c9b commit 220ba2a
Show file tree
Hide file tree
Showing 12 changed files with 424 additions and 484 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.x
dotnet-version: 6.x
- name: Restore dependencies
run: dotnet restore
- name: Build
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,4 @@ ASALocalRun/

# MFractors (Xamarin productivity tool) working folder
.mfractor/
.ionide/symbolCache.db
19 changes: 7 additions & 12 deletions src/Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static bool AsBool(this Stream stream)
#region Byte Operations
public static Stream FromByte(byte value)
{
MemoryStream result = new MemoryStream();
var result = new MemoryStream();
result.WriteByte(value);
result.Position = 0;
return result;
Expand Down Expand Up @@ -172,24 +172,19 @@ public static ulong AsULong(this Stream stream)

public static Stream FromDecimal(decimal value)
{
MemoryStream stream = new MemoryStream();

BinaryWriter binaryWriter = new BinaryWriter(stream);
var stream = new MemoryStream();
var binaryWriter = new BinaryWriter(stream);
binaryWriter.Write(value);

stream.Position = 0;

return stream;
}

public static decimal AsDecimal(this Stream stream)
{
decimal result;
stream.Position = 0;

BinaryReader binaryReader = new BinaryReader(stream);
var binaryReader = new BinaryReader(stream);
result = binaryReader.ReadDecimal();

stream.Position = 0;
return result;
}
Expand All @@ -198,7 +193,7 @@ public static decimal AsDecimal(this Stream stream)
#region VarInt Operations
public static Stream FromVarInt(long value)
{
MemoryStream result = new MemoryStream();
var result = new MemoryStream();
ulong buffer = (value >= 0) ? (ulong)(value << 1) : (((ulong)(-value) << 1) + 1);
CompactBytes(result, buffer);
return result;
Expand All @@ -214,7 +209,7 @@ public static long AsVarInt(this Stream stream)
#region VarInt Operations
public static Stream FromVarUInt(ulong value)
{
MemoryStream result = new MemoryStream();
var result = new MemoryStream();
CompactBytes(result, value);
return result;
}
Expand Down Expand Up @@ -315,7 +310,7 @@ public static async Task<Stream> FromElementAsync(Element value)
throw new ArgumentNullException(nameof(value));
}

Stream stream = new MemoryStream();
var stream = new MemoryStream();
await value.SaveAsync(stream).ConfigureAwait(false);
return stream;
}
Expand Down
18 changes: 9 additions & 9 deletions src/ElementEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ public class PromptuariumSavedEventArgs : PromptuariumSaveEventArg
public partial class Element
{
#region Events
public static event EventHandler<PromptuariumLoadingEventArgs> OnDataLoading;
public static event EventHandler<PromptuariumLoadedEventArgs> OnDataLoaded;
public event EventHandler<PromptuariumSavingEventArgs> OnDataSaving;
public event EventHandler<PromptuariumSavedEventArgs> OnDataSaved;

public static event EventHandler<PromptuariumLoadingEventArgs> OnMetaDataLoading;
public static event EventHandler<PromptuariumLoadedEventArgs> OnMetaDataLoaded;
public event EventHandler<PromptuariumSavingEventArgs> OnMetaDataSaving;
public event EventHandler<PromptuariumSavedEventArgs> OnMetaDataSaved;
public static event EventHandler<PromptuariumLoadingEventArgs>? OnDataLoading;
public static event EventHandler<PromptuariumLoadedEventArgs>? OnDataLoaded;
public event EventHandler<PromptuariumSavingEventArgs>? OnDataSaving;
public event EventHandler<PromptuariumSavedEventArgs>? OnDataSaved;

public static event EventHandler<PromptuariumLoadingEventArgs>? OnMetaDataLoading;
public static event EventHandler<PromptuariumLoadedEventArgs>? OnMetaDataLoaded;
public event EventHandler<PromptuariumSavingEventArgs>? OnMetaDataSaving;
public event EventHandler<PromptuariumSavedEventArgs>? OnMetaDataSaved;
#endregion
}
#endregion
Expand Down
16 changes: 4 additions & 12 deletions src/ElementIo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,8 @@ public static Task<Element> LoadAsync(Stream stream)
/// <returns>The tree, if no error occured</returns>
public static async Task<Element> LoadAsync(string fileName, CancellationToken cancellationToken)
{
Element result;

using (Stream fileStream = new FileStream(fileName, FileMode.Open))
{
result = await LoadAsync(fileStream, cancellationToken).ConfigureAwait(false);
}

return result;
using var fileStream = new FileStream(fileName, FileMode.Open);
return await LoadAsync(fileStream, cancellationToken).ConfigureAwait(false);
}

public static Task<Element> LoadAsync(string fileName)
Expand Down Expand Up @@ -72,10 +66,8 @@ public Task SaveAsync(Stream stream)
/// <returns>True, if no error occured</returns>
public async Task SaveAsync(string fileName, CancellationToken cancellationToken)
{
using (Stream stream = new FileStream(fileName, FileMode.Create))
{
await SaveAsync(stream, cancellationToken).ConfigureAwait(false);
}
using Stream stream = new FileStream(fileName, FileMode.Create);
await SaveAsync(stream, cancellationToken).ConfigureAwait(false);
}

public Task SaveAsync(string fileName)
Expand Down
68 changes: 35 additions & 33 deletions src/ElementSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ private struct SizeDescriptor

private class SerializationArguments
{
public Element previouslySerialized;
public Element? previouslySerialized;
public int stepUpNodesRequired;
}
#endregion
Expand All @@ -42,7 +42,7 @@ private async Task SerializeAsync(Stream stream, SerializationArguments serializ
{
direction = Directions.New;
}
else if (serializationArguments.previouslySerialized.Parent.Parent == Parent)
else if (serializationArguments.previouslySerialized?.Parent?.Parent == Parent)
{
direction = Directions.Up;
}
Expand All @@ -54,7 +54,7 @@ private async Task SerializeAsync(Stream stream, SerializationArguments serializ
bool appending = false;

// If the node has only children
if (!Contains(Data) && !Contains(MetaData) && children.Count > 0)
if (!Contains(Data) && !Contains(MetaData) && this.Children.Count > 0)
{
stream.WriteByte(ControlByte(direction, DataType.Data, SizeType.Linear, 0));
}
Expand All @@ -66,7 +66,7 @@ private async Task SerializeAsync(Stream stream, SerializationArguments serializ

if (Contains(Data))
{
appending = await SerializeContentAsync(stream, Data, DataType.Data, direction, appending, cancellationToken).ConfigureAwait(false);
appending = await SerializeContentAsync(stream, Data!, DataType.Data, direction, appending, cancellationToken).ConfigureAwait(false);
}

#region Fireing event
Expand All @@ -83,7 +83,7 @@ private async Task SerializeAsync(Stream stream, SerializationArguments serializ

if (Contains(MetaData))
{
appending = await SerializeContentAsync(stream, MetaData, DataType.MetaData, direction, appending, cancellationToken).ConfigureAwait(false);
appending = await SerializeContentAsync(stream, MetaData!, DataType.MetaData, direction, appending, cancellationToken).ConfigureAwait(false);
}

#region Fireing event
Expand All @@ -94,29 +94,29 @@ private async Task SerializeAsync(Stream stream, SerializationArguments serializ

serializationArguments.previouslySerialized = this;

if (children.Count > 0)
if (this.Children.Count > 0)
{
Element child = new Element();
var child = new Element();

for (int childIndex = 0; childIndex < children.Count; childIndex++)
for (int childIndex = 0; childIndex < this.Children.Count; childIndex++)
{
child = children[childIndex];
child = this.Children[childIndex];

if (child != null)
{
await child.SerializeAsync(stream, serializationArguments, cancellationToken).ConfigureAwait(false);
}
}

if (child.children.Count > 0)
if (child?.Children.Count > 0)
{
serializationArguments.stepUpNodesRequired++;
serializationArguments.previouslySerialized = child;
}
}
}

private async Task<bool> SerializeContentAsync(Stream target, Stream source, DataType dataType, Directions direction, bool appending, CancellationToken cancellationToken)
private static async Task<bool> SerializeContentAsync(Stream target, Stream source, DataType dataType, Directions direction, bool appending, CancellationToken cancellationToken)
{
IEnumerable<SizeDescriptor> chunks = GetSizes(source.Length);
source.Position = 0;
Expand All @@ -143,19 +143,19 @@ private async Task<bool> SerializeContentAsync(Stream target, Stream source, Dat
return appending;
}

private byte ControlByte(Directions direction, DataType dataType, SizeType sizeType, byte sizeBits)
private static byte ControlByte(Directions direction, DataType dataType, SizeType sizeType, byte sizeBits)
{
if (sizeBits <= NibbleMaxValue)
{
return (byte)((byte)direction | (byte)dataType | (byte)sizeType | sizeBits);
}

throw new PromptuariumException("Size bits greater than the max value (" + NibbleMaxValue.ToString(CultureInfo.InvariantCulture) + " bits)");
throw new PromptuariumException($"Size bits greater than the max value ({NibbleMaxValue.ToString(CultureInfo.InvariantCulture)} bits)");
}

private IEnumerable<SizeDescriptor> GetSizes(long size)
private static IEnumerable<SizeDescriptor> GetSizes(long size)
{
List<SizeDescriptor> sizes = new List<SizeDescriptor>();
var sizes = new List<SizeDescriptor>();
long word = size;

while (word > 0)
Expand Down Expand Up @@ -204,7 +204,7 @@ private IEnumerable<SizeDescriptor> GetSizes(long size)
return sizes;
}

private bool Contains(Stream stream)
private static bool Contains(Stream? stream)
{
return stream?.Length > 0;
}
Expand All @@ -213,7 +213,7 @@ private bool Contains(Stream stream)
#region Deserialization routines
private static async Task<Element> DeserializeAsync(Stream stream, CancellationToken cancellationToken)
{
Element root = new Element();
var root = new Element();
Element parent = root;
Element node = root;

Expand All @@ -233,8 +233,16 @@ private static async Task<Element> DeserializeAsync(Stream stream, CancellationT
break;

case (byte) Directions.Up:
parent = parent.Parent;
node = AddNewNode(parent);
if (parent.Parent != null)
{
parent = parent.Parent;
node = AddNewNode(parent);
}
else
{
throw new PromptuariumException("Navigating upper the root node during deserialization.");
}

break;

case (byte) Directions.Append:
Expand All @@ -253,7 +261,7 @@ private static async Task<Element> DeserializeAsync(Stream stream, CancellationT

if (IsDataChunk(controlByte))
{
if (node != null && chunkSize > 0)
if (chunkSize > 0)
{
if (node.Data == null)
{
Expand All @@ -273,7 +281,7 @@ private static async Task<Element> DeserializeAsync(Stream stream, CancellationT
}
else
{
if (node != null && chunkSize > 0)
if (chunkSize > 0)
{
if (node.MetaData == null)
{
Expand All @@ -295,9 +303,9 @@ private static async Task<Element> DeserializeAsync(Stream stream, CancellationT

PurgeEmptyNodes(root);

if (root.children.Count > 0)
if (root.Children.Count > 0)
{
return root.children[0];
return root.Children[0];
}
else
{
Expand All @@ -315,14 +323,8 @@ private static int GetChunkSize(byte controlByte) => IsSizeLogarithmic(controlBy

private static Element AddNewNode(Element parent)
{
Element node = null;

if (parent != null)
{
node = new Element();
parent.UnsafeAdd(node);
}

var node = new Element();
parent.UnsafeAdd(node);
return node;
}

Expand All @@ -333,9 +335,9 @@ private static void PurgeEmptyNodes(Element node)
PurgeEmptyNodes(child);
}

if (node.Data == null && node.MetaData == null && node.children.Count == 0 && node.Parent != null)
if (node.Data == null && node.MetaData == null && node.Children.Count == 0)
{
node.Parent.Remove(node);
node.Parent?.Remove(node);
}
}
#endregion
Expand Down
Loading

0 comments on commit 220ba2a

Please sign in to comment.