Skip to content

Commit

Permalink
Renames and significant cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsakharov committed Nov 25, 2024
1 parent 43c2039 commit a7d2fc1
Show file tree
Hide file tree
Showing 22 changed files with 389 additions and 405 deletions.
104 changes: 104 additions & 0 deletions Echo/Echo.Compound.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// This file is part of the Prowl Game Engine
// Licensed under the MIT License. See the LICENSE file in the project root for details.

namespace Prowl.Echo;

public sealed partial class Echo
{
public Dictionary<string, Echo> Tags => (Value as Dictionary<string, Echo>)!;

public Echo this[string tagName]
{
get { return Get(tagName); }
set
{
if (TagType != PropertyType.Compound)
throw new InvalidOperationException("Cannot set tag on non-compound tag");
else if (tagName == null)
throw new ArgumentNullException(nameof(tagName));
else if (value == null)
throw new ArgumentNullException(nameof(value));
Tags[tagName] = value;
value.Parent = this;
}
}

/// <summary> Gets a collection containing all tag names in this CompoundTag. </summary>
public IEnumerable<string> GetNames() => Tags.Keys;

/// <summary> Gets a collection containing all tags in this CompoundTag. </summary>
public IEnumerable<Echo> GetAllTags() => Tags.Values;

public Echo? Get(string tagName)
{
if (TagType != PropertyType.Compound)
throw new InvalidOperationException("Cannot get tag from non-compound tag");
else if (tagName == null)
throw new ArgumentNullException(nameof(tagName));
return Tags.TryGetValue(tagName, out var result) ? result : null;
}

public bool TryGet(string tagName, out Echo? result)
{
if (TagType != PropertyType.Compound)
throw new InvalidOperationException("Cannot get tag from non-compound tag");
return tagName != null ? Tags.TryGetValue(tagName, out result) : throw new ArgumentNullException(nameof(tagName));
}

public bool Contains(string tagName)
{
if (TagType != PropertyType.Compound)
throw new InvalidOperationException("Cannot get tag from non-compound tag");
return tagName != null ? Tags.ContainsKey(tagName) : throw new ArgumentNullException(nameof(tagName));
}

public void Add(string name, Echo newTag)
{
if (TagType != PropertyType.Compound)
throw new InvalidOperationException("Cannot get tag from non-compound tag");
if (newTag == null)
throw new ArgumentNullException(nameof(newTag));
else if (newTag == this)
throw new ArgumentException("Cannot add tag to self");
Tags.Add(name, newTag);
newTag.Parent = this;
}

public bool Remove(string name)
{
if (TagType != PropertyType.Compound)
throw new InvalidOperationException("Cannot get tag from non-compound tag");
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentNullException(nameof(name));
return Tags.Remove(name);
}

public bool TryFind(string path, out Echo? tag)
{
tag = Find(path);
return tag != null;
}

public Echo? Find(string path)
{
if (TagType != PropertyType.Compound)
throw new InvalidOperationException("Cannot get tag from non-compound tag");
Echo currentTag = this;
while (true)
{
var i = path.IndexOf('/');
var name = i < 0 ? path : path[..i];
if (!currentTag.TryGet(name, out Echo? tag) || tag == null)
return null;

if (i < 0)
return tag;

if (tag.TagType != PropertyType.Compound)
return null;

currentTag = tag;
path = path[(i + 1)..];
}
}
}
14 changes: 6 additions & 8 deletions Echo/SerializedProperty.List.cs → Echo/Echo.List.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,34 @@
// This file is part of the Prowl Game Engine
// Licensed under the MIT License. See the LICENSE file in the project root for details.

using System.Collections.Generic;

namespace Prowl.Echo;

public sealed partial class SerializedProperty
public sealed partial class Echo
{
public List<SerializedProperty> List => (Value as List<SerializedProperty>)!;
public List<Echo> List => (Value as List<Echo>)!;

public SerializedProperty this[int tagIdx]
public Echo this[int tagIdx]
{
get { return Get(tagIdx); }
set { List[tagIdx] = value; }
}

public SerializedProperty Get(int tagIdx)
public Echo Get(int tagIdx)
{
if (TagType != PropertyType.List)
throw new System.InvalidOperationException("Cannot get tag from non-list tag");
return List[tagIdx];
}

public void ListAdd(SerializedProperty tag)
public void ListAdd(Echo tag)
{
if (TagType != PropertyType.List)
throw new System.InvalidOperationException("Cannot add tag to non-list tag");
List.Add(tag);
tag.Parent = this;
}

public void ListRemove(SerializedProperty tag)
public void ListRemove(Echo tag)
{
if (TagType != PropertyType.List)
throw new System.InvalidOperationException("Cannot remove tag from non-list tag");
Expand Down
72 changes: 35 additions & 37 deletions Echo/SerializedProperty.cs → Echo/Echo.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// This file is part of the Prowl Game Engine
// Licensed under the MIT License. See the LICENSE file in the project root for details.

using System;
using System.Collections.Generic;
using System.Globalization;

namespace Prowl.Echo;
Expand Down Expand Up @@ -30,19 +28,19 @@ public enum PropertyType

public class PropertyChangeEventArgs : EventArgs
{
public SerializedProperty Property { get; }
public Echo Property { get; }
public object? OldValue { get; }
public object? NewValue { get; }

public PropertyChangeEventArgs(SerializedProperty property, object? oldValue, object? newValue)
public PropertyChangeEventArgs(Echo property, object? oldValue, object? newValue)
{
Property = property;
OldValue = oldValue;
NewValue = newValue;
}
}

public sealed partial class SerializedProperty
public sealed partial class Echo
{
public event EventHandler<PropertyChangeEventArgs>? PropertyChanged;

Expand All @@ -51,52 +49,52 @@ public sealed partial class SerializedProperty

public PropertyType TagType { get; private set; }

public SerializedProperty? Parent { get; private set; }

public SerializedProperty() { }
public SerializedProperty(byte i) { _value = i; TagType = PropertyType.Byte; }
public SerializedProperty(sbyte i) { _value = i; TagType = PropertyType.sByte; }
public SerializedProperty(short i) { _value = i; TagType = PropertyType.Short; }
public SerializedProperty(int i) { _value = i; TagType = PropertyType.Int; }
public SerializedProperty(long i) { _value = i; TagType = PropertyType.Long; }
public SerializedProperty(ushort i) { _value = i; TagType = PropertyType.UShort; }
public SerializedProperty(uint i) { _value = i; TagType = PropertyType.UInt; }
public SerializedProperty(ulong i) { _value = i; TagType = PropertyType.ULong; }
public SerializedProperty(float i) { _value = i; TagType = PropertyType.Float; }
public SerializedProperty(double i) { _value = i; TagType = PropertyType.Double; }
public SerializedProperty(decimal i) { _value = i; TagType = PropertyType.Decimal; }
public SerializedProperty(string i) { _value = i; TagType = PropertyType.String; }
public SerializedProperty(byte[] i) { _value = i; TagType = PropertyType.ByteArray; }
public SerializedProperty(bool i) { _value = i; TagType = PropertyType.Bool; }
public SerializedProperty(PropertyType type, object? value)
public Echo? Parent { get; private set; }

public Echo() { }
public Echo(byte i) { _value = i; TagType = PropertyType.Byte; }
public Echo(sbyte i) { _value = i; TagType = PropertyType.sByte; }
public Echo(short i) { _value = i; TagType = PropertyType.Short; }
public Echo(int i) { _value = i; TagType = PropertyType.Int; }
public Echo(long i) { _value = i; TagType = PropertyType.Long; }
public Echo(ushort i) { _value = i; TagType = PropertyType.UShort; }
public Echo(uint i) { _value = i; TagType = PropertyType.UInt; }
public Echo(ulong i) { _value = i; TagType = PropertyType.ULong; }
public Echo(float i) { _value = i; TagType = PropertyType.Float; }
public Echo(double i) { _value = i; TagType = PropertyType.Double; }
public Echo(decimal i) { _value = i; TagType = PropertyType.Decimal; }
public Echo(string i) { _value = i; TagType = PropertyType.String; }
public Echo(byte[] i) { _value = i; TagType = PropertyType.ByteArray; }
public Echo(bool i) { _value = i; TagType = PropertyType.Bool; }
public Echo(PropertyType type, object? value)
{
TagType = type;
if (type == PropertyType.List && value == null)
_value = new List<SerializedProperty>();
_value = new List<Echo>();
else if (type == PropertyType.Compound && value == null)
_value = new Dictionary<string, SerializedProperty>();
_value = new Dictionary<string, Echo>();
else
_value = value;
}
public SerializedProperty(List<SerializedProperty> tags)
public Echo(List<Echo> tags)
{
TagType = PropertyType.List;
_value = tags;
}

public static SerializedProperty NewCompound() => new(PropertyType.Compound, new Dictionary<string, SerializedProperty>());
public static SerializedProperty NewList() => new(PropertyType.List, new List<SerializedProperty>());
public static Echo NewCompound() => new(PropertyType.Compound, new Dictionary<string, Echo>());
public static Echo NewList() => new(PropertyType.List, new List<Echo>());

public void GetAllAssetRefs(ref HashSet<Guid> refs)
{
if (TagType == PropertyType.List)
{
foreach (var tag in (List<SerializedProperty>)Value!)
foreach (var tag in (List<Echo>)Value!)
tag.GetAllAssetRefs(ref refs);
}
else if (TagType == PropertyType.Compound)
{
var dict = (Dictionary<string, SerializedProperty>)Value!;
var dict = (Dictionary<string, Echo>)Value!;
if (TryGet("$type", out var typeName))
{
// This isnt a perfect solution since maybe theres a class named AssetRefCollection or something
Expand All @@ -113,22 +111,22 @@ public void GetAllAssetRefs(ref HashSet<Guid> refs)
}
}

public SerializedProperty Clone()
public Echo Clone()
{
if (TagType == PropertyType.Null) return new(PropertyType.Null, null);
else if (TagType == PropertyType.List)
{
// Value is a List<Tag>
var list = (List<SerializedProperty>)Value!;
var newList = new List<SerializedProperty>(list.Count);
var list = (List<Echo>)Value!;
var newList = new List<Echo>(list.Count);
foreach (var tag in list)
newList.Add(tag.Clone());
}
else if (TagType == PropertyType.Compound)
{
// Value is a Dictionary<string, Tag>
var dict = (Dictionary<string, SerializedProperty>)Value!;
var newDict = new Dictionary<string, SerializedProperty>(dict.Count);
var dict = (Dictionary<string, Echo>)Value!;
var newDict = new Dictionary<string, Echo>(dict.Count);
foreach (var (key, tag) in dict)
newDict.Add(key, tag.Clone());
}
Expand All @@ -151,8 +149,8 @@ public int Count
{
get
{
if (TagType == PropertyType.Compound) return ((Dictionary<string, SerializedProperty>)Value!).Count;
else if (TagType == PropertyType.List) return ((List<SerializedProperty>)Value!).Count;
if (TagType == PropertyType.Compound) return ((Dictionary<string, Echo>)Value!).Count;
else if (TagType == PropertyType.List) return ((List<Echo>)Value!).Count;
else return 0;
}
}
Expand Down
20 changes: 10 additions & 10 deletions Echo/FileFormats/BinaryTagConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ public static class BinaryTagConverter
{

#region Writing
public static void WriteToFile(SerializedProperty tag, FileInfo file)
public static void WriteToFile(Echo tag, FileInfo file)
{
using var stream = file.OpenWrite();
using var writer = new BinaryWriter(stream);
WriteTo(tag, writer);
}

public static void WriteTo(SerializedProperty tag, BinaryWriter writer) => WriteCompound(tag, writer);
public static void WriteTo(Echo tag, BinaryWriter writer) => WriteCompound(tag, writer);

private static void WriteCompound(SerializedProperty tag, BinaryWriter writer)
private static void WriteCompound(Echo tag, BinaryWriter writer)
{
writer.Write(tag.GetAllTags().Count());
foreach (var subTag in tag.Tags)
Expand All @@ -26,7 +26,7 @@ private static void WriteCompound(SerializedProperty tag, BinaryWriter writer)
}
}

private static void WriteTag(SerializedProperty tag, BinaryWriter writer)
private static void WriteTag(Echo tag, BinaryWriter writer)
{
var type = tag.TagType;
writer.Write((byte)type);
Expand Down Expand Up @@ -64,25 +64,25 @@ private static void WriteTag(SerializedProperty tag, BinaryWriter writer)


#region Reading
public static SerializedProperty ReadFromFile(FileInfo file)
public static Echo ReadFromFile(FileInfo file)
{
using var stream = file.OpenRead();
using var reader = new BinaryReader(stream);
return ReadFrom(reader);
}

public static SerializedProperty ReadFrom(BinaryReader reader) => ReadCompound(reader);
public static Echo ReadFrom(BinaryReader reader) => ReadCompound(reader);

private static SerializedProperty ReadCompound(BinaryReader reader)
private static Echo ReadCompound(BinaryReader reader)
{
SerializedProperty tag = SerializedProperty.NewCompound();
Echo tag = Echo.NewCompound();
var tagCount = reader.ReadInt32();
for (int i = 0; i < tagCount; i++)
tag.Add(reader.ReadString(), ReadTag(reader));
return tag;
}

private static SerializedProperty ReadTag(BinaryReader reader)
private static Echo ReadTag(BinaryReader reader)
{
var type = (PropertyType)reader.ReadByte();
if (type == PropertyType.Null) return new(PropertyType.Null, null);
Expand All @@ -102,7 +102,7 @@ private static SerializedProperty ReadTag(BinaryReader reader)
else if (type == PropertyType.Bool) return new(PropertyType.Bool, reader.ReadBoolean());
else if (type == PropertyType.List)
{
var listTag = SerializedProperty.NewList();
var listTag = Echo.NewList();
var tagCount = reader.ReadInt32();
for (int i = 0; i < tagCount; i++)
listTag.ListAdd(ReadTag(reader));
Expand Down
Loading

0 comments on commit a7d2fc1

Please sign in to comment.