refactor: cleaning up code to newer C# standards

This commit is contained in:
D. Moonfire 2023-01-14 16:51:06 -06:00
parent 6b9c9cf5e3
commit e4c9e82b99
18 changed files with 1180 additions and 1153 deletions

View file

@ -3,8 +3,8 @@ using System.Collections.Generic;
using System.Collections.Immutable; using System.Collections.Immutable;
using System.Threading; using System.Threading;
namespace MfGames.Gallium namespace MfGames.Gallium;
{
/// <summary> /// <summary>
/// A low-overhead entity with identification. /// A low-overhead entity with identification.
/// </summary> /// </summary>
@ -354,7 +354,7 @@ namespace MfGames.Gallium
return this with return this with
{ {
Components = this.Components.SetItem(typeof(T1), component), Components = this.Components.SetItem(typeof(T1), component)
}; };
} }
@ -393,7 +393,7 @@ namespace MfGames.Gallium
return this with return this with
{ {
Components = this.Components.Add(typeof(T1), component), Components = this.Components.Add(typeof(T1), component)
}; };
} }
@ -428,7 +428,7 @@ namespace MfGames.Gallium
return this with return this with
{ {
Components = this.Components.Remove(type), Components = this.Components.Remove(type)
}; };
} }
@ -456,7 +456,7 @@ namespace MfGames.Gallium
{ {
return this with return this with
{ {
Id = Interlocked.Increment(ref nextId), Id = Interlocked.Increment(ref nextId)
}; };
} }
@ -470,4 +470,3 @@ namespace MfGames.Gallium
return this.Components.Keys; return this.Components.Keys;
} }
} }
}

View file

@ -2,8 +2,8 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
namespace MfGames.Gallium namespace MfGames.Gallium;
{
public static class JoinEntityExtensions public static class JoinEntityExtensions
{ {
/// <summary> /// <summary>
@ -26,4 +26,3 @@ namespace MfGames.Gallium
return input.Join(other, a => a.Id, a => a.Id, merge); return input.Join(other, a => a.Id, a => a.Id, merge);
} }
} }
}

View file

@ -16,7 +16,8 @@ public static class SelectComponentExtensions
/// <param name="entities">The entities to process.</param> /// <param name="entities">The entities to process.</param>
/// <typeparam name="T1">The component type being searched.</typeparam> /// <typeparam name="T1">The component type being searched.</typeparam>
/// <returns>A sequence of T1.</returns> /// <returns>A sequence of T1.</returns>
public static IEnumerable<T1> SelectComponent<T1>(this IEnumerable<Entity> entities) public static IEnumerable<T1> SelectComponent<T1>(
this IEnumerable<Entity> entities)
{ {
foreach (Entity entity in entities) foreach (Entity entity in entities)
{ {

View file

@ -37,7 +37,8 @@ public static class SelectComponentOrDefaultExtensions
/// <param name="entities">The entities to process.</param> /// <param name="entities">The entities to process.</param>
/// <typeparam name="T1">The component type being searched.</typeparam> /// <typeparam name="T1">The component type being searched.</typeparam>
/// <returns>A sequence of T1.</returns> /// <returns>A sequence of T1.</returns>
public static IEnumerable<T1?> SelectComponentOrDefault<T1>(this IEnumerable<Entity> entities) public static IEnumerable<T1?> SelectComponentOrDefault<T1>(
this IEnumerable<Entity> entities)
{ {
foreach (Entity entity in entities) foreach (Entity entity in entities)
{ {

View file

@ -1,8 +1,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
namespace MfGames.Gallium namespace MfGames.Gallium;
{
public static class SelectEntityExtensions public static class SelectEntityExtensions
{ {
/// <summary> /// <summary>
@ -26,7 +26,9 @@ namespace MfGames.Gallium
Func<Entity, T1, Entity?> selectWithComponents, Func<Entity, T1, Entity?> selectWithComponents,
bool includeEntitiesWithoutComponents = true) bool includeEntitiesWithoutComponents = true)
{ {
return entities.SelectEntity(selectWithComponents, includeEntitiesWithoutComponents ? a => a : a => null); return entities.SelectEntity(
selectWithComponents,
includeEntitiesWithoutComponents ? a => a : a => null);
} }
/// <summary> /// <summary>
@ -51,7 +53,9 @@ namespace MfGames.Gallium
Func<Entity, T1, T2, Entity?> selectWithComponents, Func<Entity, T1, T2, Entity?> selectWithComponents,
bool includeEntitiesWithoutComponents = true) bool includeEntitiesWithoutComponents = true)
{ {
return entities.SelectEntity(selectWithComponents, includeEntitiesWithoutComponents ? a => a : a => null); return entities.SelectEntity(
selectWithComponents,
includeEntitiesWithoutComponents ? a => a : a => null);
} }
/// <summary> /// <summary>
@ -77,7 +81,9 @@ namespace MfGames.Gallium
Func<Entity, T1, T2, T3, Entity?> selectWithComponents, Func<Entity, T1, T2, T3, Entity?> selectWithComponents,
bool includeEntitiesWithoutComponents = true) bool includeEntitiesWithoutComponents = true)
{ {
return entities.SelectEntity(selectWithComponents, includeEntitiesWithoutComponents ? a => a : a => null); return entities.SelectEntity(
selectWithComponents,
includeEntitiesWithoutComponents ? a => a : a => null);
} }
/// <summary> /// <summary>
@ -104,7 +110,9 @@ namespace MfGames.Gallium
Func<Entity, T1, T2, T3, T4, Entity?> selectWithComponents, Func<Entity, T1, T2, T3, T4, Entity?> selectWithComponents,
bool includeEntitiesWithoutComponents = true) bool includeEntitiesWithoutComponents = true)
{ {
return entities.SelectEntity(selectWithComponents, includeEntitiesWithoutComponents ? a => a : a => null); return entities.SelectEntity(
selectWithComponents,
includeEntitiesWithoutComponents ? a => a : a => null);
} }
/// <summary> /// <summary>
@ -167,7 +175,8 @@ namespace MfGames.Gallium
{ {
foreach (Entity entity in entities) foreach (Entity entity in entities)
{ {
Entity? result = entity.TryGet(out T1 value1) && entity.TryGet(out T2 value2) Entity? result = entity.TryGet(out T1 value1)
&& entity.TryGet(out T2 value2)
? selectWithComponents?.Invoke(entity, value1, value2) ? selectWithComponents?.Invoke(entity, value1, value2)
: selectWithoutComponents?.Invoke(entity); : selectWithoutComponents?.Invoke(entity);
@ -205,8 +214,14 @@ namespace MfGames.Gallium
foreach (Entity entity in entities) foreach (Entity entity in entities)
{ {
Entity? result = Entity? result =
entity.TryGet(out T1 value1) && entity.TryGet(out T2 value2) && entity.TryGet(out T3 value3) entity.TryGet(out T1 value1)
? selectWithComponents?.Invoke(entity, value1, value2, value3) && entity.TryGet(out T2 value2)
&& entity.TryGet(out T3 value3)
? selectWithComponents?.Invoke(
entity,
value1,
value2,
value3)
: selectWithoutComponents?.Invoke(entity); : selectWithoutComponents?.Invoke(entity);
if (result != null) if (result != null)
@ -248,7 +263,12 @@ namespace MfGames.Gallium
&& entity.TryGet(out T2 value2) && entity.TryGet(out T2 value2)
&& entity.TryGet(out T3 value3) && entity.TryGet(out T3 value3)
&& entity.TryGet(out T4 value4) && entity.TryGet(out T4 value4)
? selectWithComponents?.Invoke(entity, value1, value2, value3, value4) ? selectWithComponents?.Invoke(
entity,
value1,
value2,
value3,
value4)
: selectWithoutComponents?.Invoke(entity); : selectWithoutComponents?.Invoke(entity);
if (result != null) if (result != null)
@ -258,4 +278,3 @@ namespace MfGames.Gallium
} }
} }
} }
}

View file

@ -189,7 +189,9 @@ public static class SplitEntityExtensions
Type t1, Type t1,
Func<Entity, object, bool> test) Func<Entity, object, bool> test)
{ {
return SplitEntity(entities, a => a.Has(t1) && test(a, a.Get<object>(t1))); return SplitEntity(
entities,
a => a.Has(t1) && test(a, a.Get<object>(t1)));
} }
/// <summary> /// <summary>

View file

@ -2,8 +2,8 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
namespace MfGames.Gallium namespace MfGames.Gallium;
{
public static class WhereEntityExtensions public static class WhereEntityExtensions
{ {
public static IEnumerable<Entity> WhereEntity<T1>( public static IEnumerable<Entity> WhereEntity<T1>(
@ -45,4 +45,3 @@ namespace MfGames.Gallium
x.Get<T4>())); x.Get<T4>()));
} }
} }
}

View file

@ -1,28 +1,31 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
namespace MfGames.Gallium namespace MfGames.Gallium;
{
public static class WhereEntityHasExtensions public static class WhereEntityHasExtensions
{ {
public static IEnumerable<Entity> WhereEntityHas<T1>(this IEnumerable<Entity> entities) public static IEnumerable<Entity> WhereEntityHas<T1>(
this IEnumerable<Entity> entities)
{ {
return entities.Where(x => x.Has<T1>()); return entities.Where(x => x.Has<T1>());
} }
public static IEnumerable<Entity> WhereEntityHasAll<T1, T2>(this IEnumerable<Entity> entities) public static IEnumerable<Entity> WhereEntityHasAll
<T1, T2>(this IEnumerable<Entity> entities)
{ {
return entities.Where(x => x.HasAll<T1, T2>()); return entities.Where(x => x.HasAll<T1, T2>());
} }
public static IEnumerable<Entity> WhereEntityHasAll<T1, T2, T3>(this IEnumerable<Entity> entities) public static IEnumerable<Entity> WhereEntityHasAll
<T1, T2, T3>(this IEnumerable<Entity> entities)
{ {
return entities.Where(x => x.HasAll<T1, T2, T3>()); return entities.Where(x => x.HasAll<T1, T2, T3>());
} }
public static IEnumerable<Entity> WhereEntityHasAll<T1, T2, T3, T4>(this IEnumerable<Entity> entities) public static IEnumerable<Entity> WhereEntityHasAll
<T1, T2, T3, T4>(this IEnumerable<Entity> entities)
{ {
return entities.Where(x => x.HasAll<T1, T2, T3, T4>()); return entities.Where(x => x.HasAll<T1, T2, T3, T4>());
} }
} }
}

View file

@ -1,28 +1,31 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
namespace MfGames.Gallium namespace MfGames.Gallium;
{
public static class WhereEntityNotHasExtensions public static class WhereEntityNotHasExtensions
{ {
public static IEnumerable<Entity> WhereEntityNotHas<T1>(this IEnumerable<Entity> entities) public static IEnumerable<Entity> WhereEntityNotHas<T1>(
this IEnumerable<Entity> entities)
{ {
return entities.Where(x => !x.Has<T1>()); return entities.Where(x => !x.Has<T1>());
} }
public static IEnumerable<Entity> WhereEntityNotHasAll<T1, T2>(this IEnumerable<Entity> entities) public static IEnumerable<Entity> WhereEntityNotHasAll
<T1, T2>(this IEnumerable<Entity> entities)
{ {
return entities.Where(x => !x.HasAll<T1, T2>()); return entities.Where(x => !x.HasAll<T1, T2>());
} }
public static IEnumerable<Entity> WhereEntityNotHasAll<T1, T2, T3>(this IEnumerable<Entity> entities) public static IEnumerable<Entity> WhereEntityNotHasAll
<T1, T2, T3>(this IEnumerable<Entity> entities)
{ {
return entities.Where(x => !x.HasAll<T1, T2, T3>()); return entities.Where(x => !x.HasAll<T1, T2, T3>());
} }
public static IEnumerable<Entity> WhereEntityNotHasAll<T1, T2, T3, T4>(this IEnumerable<Entity> entities) public static IEnumerable<Entity> WhereEntityNotHasAll
<T1, T2, T3, T4>(this IEnumerable<Entity> entities)
{ {
return entities.Where(x => !x.HasAll<T1, T2, T3, T4>()); return entities.Where(x => !x.HasAll<T1, T2, T3, T4>());
} }
} }
}

View file

@ -1,10 +1,12 @@
using System; using System;
using MfGames.Gallium;
using Xunit; using Xunit;
using Xunit.Abstractions; using Xunit.Abstractions;
namespace MfGames.Gallium.Tests namespace Gallium.Tests;
{
public class EntityTests : GalliumTestsBase public class EntityTests : GalliumTestsBase
{ {
public EntityTests(ITestOutputHelper output) public EntityTests(ITestOutputHelper output)
@ -213,4 +215,3 @@ namespace MfGames.Gallium.Tests
Assert.True(entity1 == entity2); Assert.True(entity1 == entity2);
} }
} }
}

View file

@ -1,9 +1,11 @@
using System.Linq; using System.Linq;
using MfGames.Gallium;
using Xunit; using Xunit;
namespace MfGames.Gallium.Tests namespace Gallium.Tests;
{
public class EnumerableEntityTests public class EnumerableEntityTests
{ {
[Fact] [Fact]
@ -16,7 +18,7 @@ namespace MfGames.Gallium.Tests
new Entity().Add("2") new Entity().Add("2")
.Add(new TestComponent2()), .Add(new TestComponent2()),
new Entity().Add("3") new Entity().Add("3")
.Add(new TestComponent1()), .Add(new TestComponent1())
}; };
Assert.Equal( Assert.Equal(
@ -40,7 +42,7 @@ namespace MfGames.Gallium.Tests
.Add(new TestComponent2()), .Add(new TestComponent2()),
new Entity().Add("3") new Entity().Add("3")
.Add(new TestComponent1()) .Add(new TestComponent1())
.Add(new TestComponent2()), .Add(new TestComponent2())
}; };
Assert.Equal( Assert.Equal(
@ -67,12 +69,13 @@ namespace MfGames.Gallium.Tests
new Entity().Add("3") new Entity().Add("3")
.Add<ITestComponent3>(new TestComponent3a()) .Add<ITestComponent3>(new TestComponent3a())
.Add(new TestComponent1()) .Add(new TestComponent1())
.Add(new TestComponent2()), .Add(new TestComponent2())
}; };
Assert.Equal( Assert.Equal(
new[] { "1", "2", "3-TestComponent3a" }, new[] { "1", "2", "3-TestComponent3a" },
entities.SelectEntity<TestComponent1, TestComponent2, ITestComponent3>( entities
.SelectEntity<TestComponent1, TestComponent2, ITestComponent3>(
( (
e, e,
_, _,
@ -96,7 +99,7 @@ namespace MfGames.Gallium.Tests
new Entity().Add("2") new Entity().Add("2")
.Add(new TestComponent2()), .Add(new TestComponent2()),
new Entity().Add("3") new Entity().Add("3")
.Add(new TestComponent1()), .Add(new TestComponent1())
}; };
Assert.Equal( Assert.Equal(
@ -117,7 +120,7 @@ namespace MfGames.Gallium.Tests
.Add(new TestComponent2()) .Add(new TestComponent2())
.Add(new TestComponent1()), .Add(new TestComponent1()),
new Entity().Add("3") new Entity().Add("3")
.Add(new TestComponent1()), .Add(new TestComponent1())
}; };
Assert.Equal( Assert.Equal(
@ -140,12 +143,14 @@ namespace MfGames.Gallium.Tests
.Add(new TestComponent2()) .Add(new TestComponent2())
.Add(new TestComponent1()), .Add(new TestComponent1()),
new Entity().Add("3") new Entity().Add("3")
.Add<ITestComponent3>(new TestComponent3a()), .Add<ITestComponent3>(new TestComponent3a())
}; };
Assert.Equal( Assert.Equal(
new[] { "1" }, new[] { "1" },
entities.WhereEntityHasAll<TestComponent1, TestComponent2, ITestComponent3>() entities
.WhereEntityHasAll<TestComponent1, TestComponent2,
ITestComponent3>()
.Select(x => x.Get<string>()) .Select(x => x.Get<string>())
.ToArray()); .ToArray());
} }
@ -160,7 +165,7 @@ namespace MfGames.Gallium.Tests
new Entity().Add("2") new Entity().Add("2")
.Add(new TestComponent2()), .Add(new TestComponent2()),
new Entity().Add("3") new Entity().Add("3")
.Add(new TestComponent1()), .Add(new TestComponent1())
}; };
Assert.Equal( Assert.Equal(
@ -180,7 +185,7 @@ namespace MfGames.Gallium.Tests
new Entity().Add("2") new Entity().Add("2")
.Add(new TestComponent2()) .Add(new TestComponent2())
.Add(new TestComponent1()), .Add(new TestComponent1()),
new Entity().Add("3"), new Entity().Add("3")
}; };
Assert.Equal( Assert.Equal(
@ -202,14 +207,15 @@ namespace MfGames.Gallium.Tests
.Add(new TestComponent2()) .Add(new TestComponent2())
.Add<ITestComponent3>(new TestComponent3b()), .Add<ITestComponent3>(new TestComponent3b()),
new Entity().Add("3") new Entity().Add("3")
.Add<ITestComponent3>(new TestComponent3a()), .Add<ITestComponent3>(new TestComponent3a())
}; };
Assert.Equal( Assert.Equal(
new string[] { "1", "3" }, new[] { "1", "3" },
entities.WhereEntityNotHasAll<TestComponent1, TestComponent2, ITestComponent3>() entities
.WhereEntityNotHasAll<TestComponent1, TestComponent2,
ITestComponent3>()
.Select(x => x.Get<string>()) .Select(x => x.Get<string>())
.ToArray()); .ToArray());
} }
} }
}

View file

@ -3,8 +3,8 @@ using Serilog.Core;
using Xunit.Abstractions; using Xunit.Abstractions;
namespace MfGames.Gallium.Tests namespace Gallium.Tests;
{
/// <summary> /// <summary>
/// Common initialization logic for Gallium-based tests including setting /// Common initialization logic for Gallium-based tests including setting
/// up containers, logging, and Serilog. /// up containers, logging, and Serilog.
@ -38,4 +38,3 @@ namespace MfGames.Gallium.Tests
/// </summary> /// </summary>
protected Logger Logger { get; } protected Logger Logger { get; }
} }
}

View file

@ -1,6 +1,5 @@
namespace MfGames.Gallium.Tests namespace Gallium.Tests;
{
public interface ITestComponent3 public interface ITestComponent3
{ {
} }
}

View file

@ -1,6 +1,5 @@
namespace MfGames.Gallium.Tests namespace Gallium.Tests;
{
public class TestComponent1 public class TestComponent1
{ {
} }
}

View file

@ -1,6 +1,5 @@
namespace MfGames.Gallium.Tests namespace Gallium.Tests;
{
public class TestComponent2 public class TestComponent2
{ {
} }
}

View file

@ -1,6 +1,5 @@
namespace MfGames.Gallium.Tests namespace Gallium.Tests;
{
public class TestComponent3a : ITestComponent3 public class TestComponent3a : ITestComponent3
{ {
} }
}

View file

@ -1,6 +1,5 @@
namespace MfGames.Gallium.Tests namespace Gallium.Tests;
{
public class TestComponent3b : ITestComponent3 public class TestComponent3b : ITestComponent3
{ {
} }
}