1
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
@@ -7,9 +8,25 @@ namespace BITKit.Entities
|
||||
{
|
||||
public class Entity : IEntity, IDisposable
|
||||
{
|
||||
private class EntityServiceProvider : IServiceProvider
|
||||
{
|
||||
public ServiceProvider ServiceProvider;
|
||||
public readonly List<Object> Services = new();
|
||||
public object GetService(Type serviceType)
|
||||
{
|
||||
var value = ServiceProvider.GetService(serviceType);
|
||||
if (value != null)
|
||||
{
|
||||
Services.TryAdd(value);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
public Entity()
|
||||
{
|
||||
RegisterComponent<IEntity>(this);
|
||||
ServiceCollection.AddSingleton<IEntity>(this);
|
||||
}
|
||||
public void WaitForInitializationComplete()
|
||||
{
|
||||
@@ -19,51 +36,26 @@ namespace BITKit.Entities
|
||||
public CancellationToken CancellationToken => _cancellationTokenSource.Token;
|
||||
private readonly CancellationTokenSource _cancellationTokenSource = new();
|
||||
|
||||
public bool TryGetComponent<T>(out T component)
|
||||
public IServiceProvider ServiceProvider
|
||||
{
|
||||
var value = ServiceProvider.GetService<T>();
|
||||
if (value != null)
|
||||
get
|
||||
{
|
||||
component = value;
|
||||
return true;
|
||||
if (_serviceProvider is not null)
|
||||
{
|
||||
return _serviceProvider;
|
||||
}
|
||||
|
||||
var value = new EntityServiceProvider()
|
||||
{
|
||||
ServiceProvider = ServiceCollection.BuildServiceProvider()
|
||||
};
|
||||
_serviceProvider = value;
|
||||
return _serviceProvider;
|
||||
}
|
||||
|
||||
component = default!;
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool TryGetComponent(Type type, out IEntityComponent component)
|
||||
{
|
||||
var value = ServiceProvider.GetService(type);
|
||||
if (value != null)
|
||||
{
|
||||
component = (IEntityComponent)value;
|
||||
return true;
|
||||
}
|
||||
|
||||
component = default!;
|
||||
return false;
|
||||
}
|
||||
|
||||
public IEntityComponent[] Components => ServiceCollection.OfType<IEntityComponent>().ToArray();
|
||||
|
||||
public bool RegisterComponent<T>(T component)
|
||||
{
|
||||
_services.Add(component);
|
||||
ServiceCollection.AddSingleton(typeof(T), component);
|
||||
return true;
|
||||
}
|
||||
|
||||
public IServiceProvider ServiceProvider => _serviceProvider ??= ServiceCollection.BuildServiceProvider();
|
||||
public IServiceCollection ServiceCollection { get; } = new ServiceCollection();
|
||||
private IServiceProvider _serviceProvider;
|
||||
private readonly CacheList<object> _services = new();
|
||||
|
||||
public object[] GetServices()
|
||||
{
|
||||
return _services.ToArray();
|
||||
}
|
||||
|
||||
private EntityServiceProvider _serviceProvider;
|
||||
public object[] GetServices()=> _serviceProvider.Services.ToArray();
|
||||
public void Inject(object obj)
|
||||
{
|
||||
foreach (var fieldInfo in obj.GetType().GetFields(ReflectionHelper.Flags))
|
||||
@@ -78,14 +70,10 @@ namespace BITKit.Entities
|
||||
public void Dispose()
|
||||
{
|
||||
_cancellationTokenSource.Cancel();
|
||||
_cancellationTokenSource.Dispose();
|
||||
|
||||
foreach (var service in _services)
|
||||
_serviceProvider.ServiceProvider.Dispose();
|
||||
foreach (var x in GetServices().OfType<IDisposable>())
|
||||
{
|
||||
if (service is IDisposable disposable)
|
||||
{
|
||||
disposable.Dispose();
|
||||
}
|
||||
x.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user