Skip to content

Commit

Permalink
refactor: dotnet-format + some warning handling
Browse files Browse the repository at this point in the history
  • Loading branch information
myarichuk committed Sep 25, 2022
1 parent 5829d41 commit 4a291da
Show file tree
Hide file tree
Showing 28 changed files with 2,528 additions and 2,507 deletions.
30 changes: 15 additions & 15 deletions src/RoguelikeToolkit.Entities/ComponentAttribute.cs
@@ -1,19 +1,19 @@
namespace RoguelikeToolkit.Entities
{
/// <summary>
/// An attribute that marks an entity component
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
public class ComponentAttribute : Attribute
{
/// <summary>
/// Gets or sets a value of component name, this will serve as an id in the entity template text file
/// </summary>
public string? Name { get; set; }
/// <summary>
/// An attribute that marks an entity component
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
public class ComponentAttribute : Attribute
{
/// <summary>
/// Gets or sets a value of component name, this will serve as an id in the entity template text file
/// </summary>
public string? Name { get; set; }

/// <summary>
/// Gets or sets a value indicating whether component's instance will be shared globally
/// </summary>
public bool IsGlobal { get; set; }
}
/// <summary>
/// Gets or sets a value indicating whether component's instance will be shared globally
/// </summary>
public bool IsGlobal { get; set; }
}
}
22 changes: 11 additions & 11 deletions src/RoguelikeToolkit.Entities/Components/IValueComponent.cs
Expand Up @@ -2,15 +2,15 @@

namespace RoguelikeToolkit.Entities.Components
{
/// <summary>
/// An interface that marks the component as having a single value. Should be used if there is a need for an <see cref="Entity"/> to have a primitive or a string as a component.
/// </summary>
/// <typeparam name="TValue">Component's value type.</typeparam>
public interface IValueComponent<TValue>
{
/// <summary>
/// gets or sets the value of the component.
/// </summary>
TValue Value { get; set; }
}
/// <summary>
/// An interface that marks the component as having a single value. Should be used if there is a need for an <see cref="Entity"/> to have a primitive or a string as a component.
/// </summary>
/// <typeparam name="TValue">Component's value type.</typeparam>
public interface IValueComponent<TValue>
{
/// <summary>
/// gets or sets the value of the component.
/// </summary>
TValue Value { get; set; }
}
}
20 changes: 10 additions & 10 deletions src/RoguelikeToolkit.Entities/Components/TagsComponent.cs
Expand Up @@ -2,14 +2,14 @@

namespace RoguelikeToolkit.Entities.Components
{
/// <summary>
/// The component holds tags of a certain <see cref="Entity"/>.
/// </summary>
internal record struct TagsComponent : IValueComponent<HashSet<string>>
{
/// <summary>
/// gets or sets the value of the component.
/// </summary>
public HashSet<string> Value { get; set; }
}
/// <summary>
/// The component holds tags of a certain <see cref="Entity"/>.
/// </summary>
internal record struct TagsComponent : IValueComponent<HashSet<string>>
{
/// <summary>
/// gets or sets the value of the component.
/// </summary>
public HashSet<string> Value { get; set; }
}
}
302 changes: 151 additions & 151 deletions src/RoguelikeToolkit.Entities/EntityTemplate.cs
Expand Up @@ -6,155 +6,155 @@

namespace RoguelikeToolkit.Entities
{
/// <summary>
/// A class record that holds the structure of the <see cref="Entity"/> to be constructed
/// </summary>
public record EntityTemplate
{
/// <summary>
/// Cached property names of the <see cref="EntityTemplate"/> class, used in entity construction logic
/// </summary>
internal static readonly HashSet<string> PropertyNames =
new(
typeof(EntityTemplate).Properties(Flags.InstancePublic)
.Select(p => p.Name)
.Where(propertyName => propertyName != nameof(EmbeddedTemplates)),
StringComparer.InvariantCultureIgnoreCase);

private readonly Dictionary<string, object> _components = new(StringComparer.InvariantCultureIgnoreCase);

private readonly HashSet<string> _inherits = new(StringComparer.InvariantCultureIgnoreCase);
private readonly HashSet<string> _tags = new(StringComparer.InvariantCultureIgnoreCase);

private readonly HashSet<EntityTemplate> _embeddedTemplates = new(EqualityComparer);

/// <summary>
/// Initializes a new instance of the <see cref="EntityTemplate"/> class.
/// </summary>
public EntityTemplate()
{
}

// note: copy constructor needed for "shallow copy" of records

/// <summary>
/// Initializes a new instance of the <see cref="EntityTemplate"/> class (copy constructor)
/// </summary>
/// <param name="other">Instance of <see cref="EntityTemplate"/> to copy values from</param>
/// <exception cref="ArgumentNullException"><paramref name="other"/> is <see langword="null"/></exception>
protected EntityTemplate(EntityTemplate other)
{
// just in case
if (other == null)
{
throw new ArgumentNullException(nameof(other));
}

_components = new Dictionary<string, object>(other.Components, StringComparer.InvariantCultureIgnoreCase);
_inherits = new HashSet<string>(other.Inherits, StringComparer.InvariantCultureIgnoreCase);
_tags = new HashSet<string>(other.Tags, StringComparer.InvariantCultureIgnoreCase);
_embeddedTemplates = new HashSet<EntityTemplate>(other.EmbeddedTemplates);
}

/// <summary>
/// Gets the <see cref="IEqualityComparer{T}"/> implementation for <see cref="EntityTemplate"/>, compares two instances by comparing values of the <see cref="Name"/> properties
/// </summary>
public static IEqualityComparer<EntityTemplate> EqualityComparer { get; } = new NameEqualityComparer();

/// <summary>
/// Gets or sets the name of the entity template. Effectively, this is an entity Id and it should be unique
/// </summary>
public string? Name { get; set; }

/// <summary>
/// Gets the collection of entity components.
/// </summary>
public IReadOnlyDictionary<string, object> Components => _components;

/// <summary>
/// Gets a collection of entity template names, from which entity templates this template inherits from
/// </summary>
public IReadOnlySet<string> Inherits => _inherits;

/// <summary>
/// Gets a collection of tags attached to this entity
/// </summary>
public IReadOnlySet<string> Tags => _tags;

/// <summary>
/// Gets the collection of embedded templates contained in this one
/// </summary>
[YamlIgnore]
public HashSet<EntityTemplate> EmbeddedTemplates => _embeddedTemplates;

/// <summary>
/// Merge this template data with other template. Does not override existing values
/// </summary>
/// <param name="other">entity template to copy values from</param>
internal void MergeWith(EntityTemplate other)
{
MergeComponents(other.Components);
MergeInherits(other.Inherits);
MergeTags(other.Tags);
MergeEmbeddedTemplates(other.EmbeddedTemplates);
}

/// <summary>
/// Merge this template components data with other template. Does not override existing values
/// </summary>
/// <param name="otherComponents">components data to copy values from</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void MergeComponents(IReadOnlyDictionary<string, object> otherComponents) =>
_components.MergeWith(otherComponents);

/// <summary>
/// Merge this template inheritance data with other template. Does not override existing values
/// </summary>
/// <param name="otherInherits">inheritance data to copy values from</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void MergeInherits(IReadOnlySet<string> otherInherits) =>
_inherits.UnionWith(otherInherits);

/// <summary>
/// Merge this template tags data with other template. Does not override existing values
/// </summary>
/// <param name="otherTags">tags data to copy values from</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void MergeTags(IReadOnlySet<string> otherTags) =>
_tags.UnionWith(otherTags);

/// <summary>
/// Merge this template embedded template data with other template. Does not override existing values
/// </summary>
/// <param name="otherEmbeddedTemplates">embedded template data to copy values from</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void MergeEmbeddedTemplates(IReadOnlySet<EntityTemplate> otherEmbeddedTemplates) =>
_embeddedTemplates.UnionWith(otherEmbeddedTemplates);

private sealed class NameEqualityComparer : IEqualityComparer<EntityTemplate>
{
public bool Equals(EntityTemplate? x, EntityTemplate? y)
{
if (ReferenceEquals(x, y))
{
return true;
}

if (ReferenceEquals(x, null))
{
return false;
}

if (ReferenceEquals(y, null))
{
return false;
}

return x.GetType() == y.GetType() && string.Equals(x.Name, y.Name, StringComparison.InvariantCultureIgnoreCase);
}

public int GetHashCode(EntityTemplate obj) =>
obj.Name != null ? StringComparer.InvariantCultureIgnoreCase.GetHashCode(obj.Name) : 0;
}
}
/// <summary>
/// A class record that holds the structure of the <see cref="Entity"/> to be constructed
/// </summary>
public record EntityTemplate
{
/// <summary>
/// Cached property names of the <see cref="EntityTemplate"/> class, used in entity construction logic
/// </summary>
internal static readonly HashSet<string> PropertyNames =
new(
typeof(EntityTemplate).Properties(Flags.InstancePublic)
.Select(p => p.Name)
.Where(propertyName => propertyName != nameof(EmbeddedTemplates)),
StringComparer.InvariantCultureIgnoreCase);

private readonly Dictionary<string, object> _components = new(StringComparer.InvariantCultureIgnoreCase);

private readonly HashSet<string> _inherits = new(StringComparer.InvariantCultureIgnoreCase);
private readonly HashSet<string> _tags = new(StringComparer.InvariantCultureIgnoreCase);

private readonly HashSet<EntityTemplate> _embeddedTemplates = new(EqualityComparer);

/// <summary>
/// Initializes a new instance of the <see cref="EntityTemplate"/> class.
/// </summary>
public EntityTemplate()
{
}

// note: copy constructor needed for "shallow copy" of records

/// <summary>
/// Initializes a new instance of the <see cref="EntityTemplate"/> class (copy constructor)
/// </summary>
/// <param name="other">Instance of <see cref="EntityTemplate"/> to copy values from</param>
/// <exception cref="ArgumentNullException"><paramref name="other"/> is <see langword="null"/></exception>
protected EntityTemplate(EntityTemplate other)
{
// just in case
if (other == null)
{
throw new ArgumentNullException(nameof(other));
}

_components = new Dictionary<string, object>(other.Components, StringComparer.InvariantCultureIgnoreCase);
_inherits = new HashSet<string>(other.Inherits, StringComparer.InvariantCultureIgnoreCase);
_tags = new HashSet<string>(other.Tags, StringComparer.InvariantCultureIgnoreCase);
_embeddedTemplates = new HashSet<EntityTemplate>(other.EmbeddedTemplates);
}

/// <summary>
/// Gets the <see cref="IEqualityComparer{T}"/> implementation for <see cref="EntityTemplate"/>, compares two instances by comparing values of the <see cref="Name"/> properties
/// </summary>
public static IEqualityComparer<EntityTemplate> EqualityComparer { get; } = new NameEqualityComparer();

/// <summary>
/// Gets or sets the name of the entity template. Effectively, this is an entity Id and it should be unique
/// </summary>
public string? Name { get; set; }

/// <summary>
/// Gets the collection of entity components.
/// </summary>
public IReadOnlyDictionary<string, object> Components => _components;

/// <summary>
/// Gets a collection of entity template names, from which entity templates this template inherits from
/// </summary>
public IReadOnlySet<string> Inherits => _inherits;

/// <summary>
/// Gets a collection of tags attached to this entity
/// </summary>
public IReadOnlySet<string> Tags => _tags;

/// <summary>
/// Gets the collection of embedded templates contained in this one
/// </summary>
[YamlIgnore]
public HashSet<EntityTemplate> EmbeddedTemplates => _embeddedTemplates;

/// <summary>
/// Merge this template data with other template. Does not override existing values
/// </summary>
/// <param name="other">entity template to copy values from</param>
internal void MergeWith(EntityTemplate other)
{
MergeComponents(other.Components);
MergeInherits(other.Inherits);
MergeTags(other.Tags);
MergeEmbeddedTemplates(other.EmbeddedTemplates);
}

/// <summary>
/// Merge this template components data with other template. Does not override existing values
/// </summary>
/// <param name="otherComponents">components data to copy values from</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void MergeComponents(IReadOnlyDictionary<string, object> otherComponents) =>
_components.MergeWith(otherComponents);

/// <summary>
/// Merge this template inheritance data with other template. Does not override existing values
/// </summary>
/// <param name="otherInherits">inheritance data to copy values from</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void MergeInherits(IReadOnlySet<string> otherInherits) =>
_inherits.UnionWith(otherInherits);

/// <summary>
/// Merge this template tags data with other template. Does not override existing values
/// </summary>
/// <param name="otherTags">tags data to copy values from</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void MergeTags(IReadOnlySet<string> otherTags) =>
_tags.UnionWith(otherTags);

/// <summary>
/// Merge this template embedded template data with other template. Does not override existing values
/// </summary>
/// <param name="otherEmbeddedTemplates">embedded template data to copy values from</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void MergeEmbeddedTemplates(IReadOnlySet<EntityTemplate> otherEmbeddedTemplates) =>
_embeddedTemplates.UnionWith(otherEmbeddedTemplates);

private sealed class NameEqualityComparer : IEqualityComparer<EntityTemplate>
{
public bool Equals(EntityTemplate? x, EntityTemplate? y)
{
if (ReferenceEquals(x, y))
{
return true;
}

if (ReferenceEquals(x, null))
{
return false;
}

if (ReferenceEquals(y, null))
{
return false;
}

return x.GetType() == y.GetType() && string.Equals(x.Name, y.Name, StringComparison.InvariantCultureIgnoreCase);
}

public int GetHashCode(EntityTemplate obj) =>
obj.Name != null ? StringComparer.InvariantCultureIgnoreCase.GetHashCode(obj.Name) : 0;
}
}
}

0 comments on commit 4a291da

Please sign in to comment.