refactor!: Identifiable interface extracted from IEntity
This commit is contained in:
		
							
								
								
									
										39
									
								
								Engine.Core/Serialization/IdentifiableRegistry.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								Engine.Core/Serialization/IdentifiableRegistry.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,39 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
 | 
			
		||||
namespace Engine.Core.Serialization;
 | 
			
		||||
 | 
			
		||||
public class IdentifiableRegistry
 | 
			
		||||
{
 | 
			
		||||
    public Event<IdentifiableRegistry, EntityRegisteredArguments> OnEntityRegistered = null!;
 | 
			
		||||
 | 
			
		||||
    private readonly Dictionary<string, Action<IIdentifiable>?> assignCallbacks = [];
 | 
			
		||||
    private readonly Dictionary<string, IIdentifiable> registeredEntities = [];
 | 
			
		||||
    public IReadOnlyDictionary<string, IIdentifiable> RegisteredEntities => registeredEntities;
 | 
			
		||||
 | 
			
		||||
    public void Add(IIdentifiable identifiable)
 | 
			
		||||
    {
 | 
			
		||||
        if (registeredEntities.TryAdd(identifiable.Id, identifiable))
 | 
			
		||||
            OnEntityRegistered?.Invoke(this, new(identifiable));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void QueueAssign(string id, Action<IIdentifiable> setMethod)
 | 
			
		||||
    {
 | 
			
		||||
        assignCallbacks.TryAdd(id, null);
 | 
			
		||||
        assignCallbacks[id] = assignCallbacks[id] + setMethod;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void AssignAll()
 | 
			
		||||
    {
 | 
			
		||||
        foreach ((string id, Action<IIdentifiable>? action) in assignCallbacks)
 | 
			
		||||
            action?.Invoke(registeredEntities[id]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void Reset()
 | 
			
		||||
    {
 | 
			
		||||
        assignCallbacks.Clear();
 | 
			
		||||
        registeredEntities.Clear();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public readonly record struct EntityRegisteredArguments(IIdentifiable Entity);
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user