Skip to content

Commit

Permalink
refactor: refactor ComponentFactory for code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
myarichuk committed Sep 15, 2022
1 parent 2d2f3d4 commit 5829d41
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 21 deletions.
5 changes: 3 additions & 2 deletions Library.sln
Expand Up @@ -2,12 +2,13 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.3.32825.248
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RoguelikeToolkit.Entities", "src\RoguelikeToolkit.Entities\RoguelikeToolkit.Entities.csproj", "{AA6E5513-F4A3-4642-AC51-0327A0F9E7C4}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RoguelikeToolkit.Entities", "src\RoguelikeToolkit.Entities\RoguelikeToolkit.Entities.csproj", "{AA6E5513-F4A3-4642-AC51-0327A0F9E7C4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RoguelikeToolkit.Entities.Tests", "tests\RoguelikeToolkit.Entities.Tests\RoguelikeToolkit.Entities.Tests.csproj", "{98A9E87D-8A55-4BC0-AEA3-DD9F20E6A66F}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RoguelikeToolkit.Entities.Tests", "tests\RoguelikeToolkit.Entities.Tests\RoguelikeToolkit.Entities.Tests.csproj", "{98A9E87D-8A55-4BC0-AEA3-DD9F20E6A66F}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{222FFFD2-D7D9-4951-BCF2-BD1684B01EBC}"
ProjectSection(SolutionItems) = preProject
.pre-commit-config.yaml = .pre-commit-config.yaml
global.json = global.json
EndProjectSection
EndProject
Expand Down
8 changes: 0 additions & 8 deletions src/RoguelikeToolkit.Entities/Factory/ComponentFactory.cs
Expand Up @@ -17,13 +17,7 @@ namespace RoguelikeToolkit.Entities.Factory
/// </summary>
internal class ComponentFactory
{
private static readonly MethodInfo? CreateInstanceMethodNonGeneric =
typeof(ComponentFactory).Methods()
.FirstOrDefault(m => m.Name == nameof(TryCreateInstance));

private readonly ConcurrentDictionary<Type, IList<PropertyInfo>> _typePropertyCache = new();
private readonly ConcurrentDictionary<Type, Type[]> _createInstanceGenericsCache = new();
private readonly ConcurrentDictionary<Type, MethodInfo> _createInstanceMethodCache = new();

private readonly TypeConversionProvider _typeConversionProvider = new(Options.Create(new TypeConversionProviderOptions
{
Expand Down Expand Up @@ -129,8 +123,6 @@ public bool TryCreateInstance(Type componentType, IReadOnlyDictionary<object, ob
/// <param name="objectData">Property data, typically received from YamlDotNet deserialization</param>
/// <param name="instance">resulting instance of the component</param>
/// <returns>true if instance creation succeeded, false otherwise</returns>
/// <exception cref="ArgumentNullException"><paramref name="objectData"/> is <see langword="null"/></exception>
/// <exception cref="ArgumentException">The type implements <see cref="IValueComponent{TValue}"/>, use the other overload for correct functionality.</exception>
public bool TryCreateInstance<TComponent>(IReadOnlyDictionary<object, object> objectData, out TComponent instance)
{
var success = TryCreateInstance(typeof(TComponent), objectData, out var instanceAsObject);
Expand Down
17 changes: 6 additions & 11 deletions src/RoguelikeToolkit.Entities/Factory/EntityFactory.cs
Expand Up @@ -176,8 +176,7 @@ private void ConstructEntity(EntityTemplate template, out Entity entity)
}

var rawComponentType = componentRawData.Value.GetType();
object? componentInstance = null;
componentInstance = CreateComponentInstance(rawComponentType, componentType, componentRawData);
var componentInstance = CreateComponentInstance(rawComponentType, componentType, componentRawData);

if (IsGlobalComponent(componentType))
{
Expand Down Expand Up @@ -206,14 +205,16 @@ private void SetRegularComponentInEntity(in Entity entity, Type componentType, o
private void SetGlobalComponentInEntity(in Entity entity, Type componentType, object componentInstance)
{
var genericWorldHasMethod =
WorldHasMethodCache.GetOrAdd(componentType, type => (WorldHasMethod ?? throw new InvalidOperationException($"failed to create method delegate ({nameof(WorldHasMethod)}")).MakeGenericMethod(type));
WorldHasMethodCache.GetOrAdd(
componentType,
type => (WorldHasMethod ?? throw new InvalidOperationException($"failed to create method delegate ({nameof(WorldHasMethod)}")).MakeGenericMethod(type));

var hasSuchComponent = (bool)genericWorldHasMethod.Call(_world);
if (!hasSuchComponent)
{
var genericWorldSetMethod = WorldSetMethodCache.GetOrAdd(
componentType,
type => WorldSetMethod.MakeGenericMethod(type));
type => (WorldSetMethod ?? throw new InvalidOperationException($"failed to create method delegate ({nameof(WorldSetMethod)})")).MakeGenericMethod(type));

genericWorldSetMethod.Call(
_world,
Expand All @@ -222,7 +223,7 @@ private void SetGlobalComponentInEntity(in Entity entity, Type componentType, ob

var genericSetSameAsWorldMethod = EntitySetSameAsWorldMethodCache.GetOrAdd(
componentType,
type => (EntitySetSameAsWorldMethod ?? throw new InvalidOperationException($"failed to create method delegate ({nameof(EntitySetSameAsWorldMethod)}")).MakeGenericMethod(type));
type => (EntitySetSameAsWorldMethod ?? throw new InvalidOperationException($"failed to create method delegate ({nameof(EntitySetSameAsWorldMethod)})")).MakeGenericMethod(type));

genericSetSameAsWorldMethod.Call(entity.WrapIfValueType());
}
Expand All @@ -232,12 +233,6 @@ private void SetGlobalComponentInEntity(in Entity entity, Type componentType, ob
object? componentInstance;
if (componentRawType.IsValueType || componentRawType.Name == nameof(String))
{
if (!componentType.IsValueComponentType())
{
throw new InvalidOperationException(
"Cannot set value type component with incompatible type. The component type must inherit from IValueComponent<TValue>");
}

// TODO: refactor for better error handling
if (!_componentFactory.TryCreateInstance(componentType, componentRawData.Value, out componentInstance))
{
Expand Down

0 comments on commit 5829d41

Please sign in to comment.