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,13 +3,13 @@ using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading;
namespace MfGames.Gallium
namespace MfGames.Gallium;
/// <summary>
/// A low-overhead entity with identification.
/// </summary>
public record Entity
{
/// <summary>
/// A low-overhead entity with identification.
/// </summary>
public record Entity
{
/// <inheritdoc />
public virtual bool Equals(Entity? other)
{
@ -354,7 +354,7 @@ namespace MfGames.Gallium
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
{
Components = this.Components.Add(typeof(T1), component),
Components = this.Components.Add(typeof(T1), component)
};
}
@ -428,7 +428,7 @@ namespace MfGames.Gallium
return this with
{
Components = this.Components.Remove(type),
Components = this.Components.Remove(type)
};
}
@ -456,7 +456,7 @@ namespace MfGames.Gallium
{
return this with
{
Id = Interlocked.Increment(ref nextId),
Id = Interlocked.Increment(ref nextId)
};
}
@ -469,5 +469,4 @@ namespace MfGames.Gallium
{
return this.Components.Keys;
}
}
}

View file

@ -2,10 +2,10 @@ using System;
using System.Collections.Generic;
using System.Linq;
namespace MfGames.Gallium
namespace MfGames.Gallium;
public static class JoinEntityExtensions
{
public static class JoinEntityExtensions
{
/// <summary>
/// Merges two sets of entities using the identifier to determine which
/// entities are the same. The `merge` function takes both of the
@ -25,5 +25,4 @@ namespace MfGames.Gallium
{
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>
/// <typeparam name="T1">The component type being searched.</typeparam>
/// <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)
{

View file

@ -37,7 +37,8 @@ public static class SelectComponentOrDefaultExtensions
/// <param name="entities">The entities to process.</param>
/// <typeparam name="T1">The component type being searched.</typeparam>
/// <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)
{

View file

@ -1,10 +1,10 @@
using System;
using System.Collections.Generic;
namespace MfGames.Gallium
namespace MfGames.Gallium;
public static class SelectEntityExtensions
{
public static class SelectEntityExtensions
{
/// <summary>
/// Selects an entity from the given list, filtering on entities with
/// the given components.
@ -26,7 +26,9 @@ namespace MfGames.Gallium
Func<Entity, T1, Entity?> selectWithComponents,
bool includeEntitiesWithoutComponents = true)
{
return entities.SelectEntity(selectWithComponents, includeEntitiesWithoutComponents ? a => a : a => null);
return entities.SelectEntity(
selectWithComponents,
includeEntitiesWithoutComponents ? a => a : a => null);
}
/// <summary>
@ -51,7 +53,9 @@ namespace MfGames.Gallium
Func<Entity, T1, T2, Entity?> selectWithComponents,
bool includeEntitiesWithoutComponents = true)
{
return entities.SelectEntity(selectWithComponents, includeEntitiesWithoutComponents ? a => a : a => null);
return entities.SelectEntity(
selectWithComponents,
includeEntitiesWithoutComponents ? a => a : a => null);
}
/// <summary>
@ -77,7 +81,9 @@ namespace MfGames.Gallium
Func<Entity, T1, T2, T3, Entity?> selectWithComponents,
bool includeEntitiesWithoutComponents = true)
{
return entities.SelectEntity(selectWithComponents, includeEntitiesWithoutComponents ? a => a : a => null);
return entities.SelectEntity(
selectWithComponents,
includeEntitiesWithoutComponents ? a => a : a => null);
}
/// <summary>
@ -104,7 +110,9 @@ namespace MfGames.Gallium
Func<Entity, T1, T2, T3, T4, Entity?> selectWithComponents,
bool includeEntitiesWithoutComponents = true)
{
return entities.SelectEntity(selectWithComponents, includeEntitiesWithoutComponents ? a => a : a => null);
return entities.SelectEntity(
selectWithComponents,
includeEntitiesWithoutComponents ? a => a : a => null);
}
/// <summary>
@ -167,7 +175,8 @@ namespace MfGames.Gallium
{
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)
: selectWithoutComponents?.Invoke(entity);
@ -205,8 +214,14 @@ namespace MfGames.Gallium
foreach (Entity entity in entities)
{
Entity? result =
entity.TryGet(out T1 value1) && entity.TryGet(out T2 value2) && entity.TryGet(out T3 value3)
? selectWithComponents?.Invoke(entity, value1, value2, value3)
entity.TryGet(out T1 value1)
&& entity.TryGet(out T2 value2)
&& entity.TryGet(out T3 value3)
? selectWithComponents?.Invoke(
entity,
value1,
value2,
value3)
: selectWithoutComponents?.Invoke(entity);
if (result != null)
@ -248,7 +263,12 @@ namespace MfGames.Gallium
&& entity.TryGet(out T2 value2)
&& entity.TryGet(out T3 value3)
&& entity.TryGet(out T4 value4)
? selectWithComponents?.Invoke(entity, value1, value2, value3, value4)
? selectWithComponents?.Invoke(
entity,
value1,
value2,
value3,
value4)
: selectWithoutComponents?.Invoke(entity);
if (result != null)
@ -257,5 +277,4 @@ namespace MfGames.Gallium
}
}
}
}
}

View file

@ -189,7 +189,9 @@ public static class SplitEntityExtensions
Type t1,
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>

View file

@ -2,10 +2,10 @@ using System;
using System.Collections.Generic;
using System.Linq;
namespace MfGames.Gallium
namespace MfGames.Gallium;
public static class WhereEntityExtensions
{
public static class WhereEntityExtensions
{
public static IEnumerable<Entity> WhereEntity<T1>(
this IEnumerable<Entity> entities,
Func<Entity, T1, bool> include)
@ -44,5 +44,4 @@ namespace MfGames.Gallium
x.Get<T3>(),
x.Get<T4>()));
}
}
}

View file

@ -1,28 +1,31 @@
using System.Collections.Generic;
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>());
}
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>());
}
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>());
}
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>());
}
}
}

View file

@ -1,28 +1,31 @@
using System.Collections.Generic;
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>());
}
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>());
}
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>());
}
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>());
}
}
}

View file

@ -1,12 +1,14 @@
using System;
using MfGames.Gallium;
using Xunit;
using Xunit.Abstractions;
namespace MfGames.Gallium.Tests
namespace Gallium.Tests;
public class EntityTests : GalliumTestsBase
{
public class EntityTests : GalliumTestsBase
{
public EntityTests(ITestOutputHelper output)
: base(output)
{
@ -212,5 +214,4 @@ namespace MfGames.Gallium.Tests
Assert.Equal(entity1, entity2);
Assert.True(entity1 == entity2);
}
}
}

View file

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

View file

@ -3,14 +3,14 @@ using Serilog.Core;
using Xunit.Abstractions;
namespace MfGames.Gallium.Tests
namespace Gallium.Tests;
/// <summary>
/// Common initialization logic for Gallium-based tests including setting
/// up containers, logging, and Serilog.
/// </summary>
public abstract class GalliumTestsBase
{
/// <summary>
/// Common initialization logic for Gallium-based tests including setting
/// up containers, logging, and Serilog.
/// </summary>
public abstract class GalliumTestsBase
{
protected GalliumTestsBase(ITestOutputHelper output)
{
this.Output = output;
@ -37,5 +37,4 @@ namespace MfGames.Gallium.Tests
/// Gets the logger used to report messages about the test.
/// </summary>
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

@ -10,10 +10,10 @@
<Version>1.4.0</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.1" />
<PackageReference Include="Serilog.Sinks.XUnit" Version="3.0.3" />
<PackageReference Include="JunitXml.TestLogger" Version="3.0.114" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.1"/>
<PackageReference Include="Serilog.Sinks.XUnit" Version="3.0.3"/>
<PackageReference Include="JunitXml.TestLogger" Version="3.0.114"/>
<PackageReference Include="xunit" Version="2.4.2"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
@ -24,6 +24,6 @@
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\MfGames.Gallium\MfGames.Gallium.csproj" />
<ProjectReference Include="..\..\src\MfGames.Gallium\MfGames.Gallium.csproj"/>
</ItemGroup>
</Project>

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
{
}
}