diff --git a/mROA/Abstract/IContextRepository.cs b/mROA/Abstract/IContextRepository.cs index 6307373..0acbb60 100644 --- a/mROA/Abstract/IContextRepository.cs +++ b/mROA/Abstract/IContextRepository.cs @@ -5,11 +5,11 @@ namespace mROA.Abstract { public interface IContextRepository : IInjectableModule { - int ResisterObject(object o, IEndPointContext context); + int ResisterObject(object o, IEndPointContext context); void ClearObject(int id); T GetObjectBySharedObject(SharedObjectShellShell sharedObjectShellShell); T? GetObject(int id); object GetSingleObject(Type type); - int GetObjectIndex(object o, IEndPointContext context); + int GetObjectIndex(object o, IEndPointContext context); } } \ No newline at end of file diff --git a/mROA/Abstract/IEventBinder.cs b/mROA/Abstract/IEventBinder.cs new file mode 100644 index 0000000..3527128 --- /dev/null +++ b/mROA/Abstract/IEventBinder.cs @@ -0,0 +1,7 @@ +namespace mROA.Abstract +{ + public interface IEventBinder + { + public void BindEvents(T source, IEndPointContext context); + } +} \ No newline at end of file diff --git a/mROA/Implementation/Backend/ContextRepository.cs b/mROA/Implementation/Backend/ContextRepository.cs index 4351e96..89bacf5 100644 --- a/mROA/Implementation/Backend/ContextRepository.cs +++ b/mROA/Implementation/Backend/ContextRepository.cs @@ -10,8 +10,11 @@ namespace mROA.Implementation.Backend { public class ContextRepository : IContextRepository { + public static object[] EventBinders = new object[]{}; private int _debugId = -1; + private static int LastDebugId = -1; + // [CanBeNull] private Dictionary _singletons; private object?[] _storage; @@ -39,13 +42,15 @@ namespace mROA.Implementation.Backend Activator.CreateInstance); } - public int ResisterObject(object o, IEndPointContext context) + public int ResisterObject(object o, IEndPointContext context) { if (!_lastIndexFinder.IsCompleted) _lastIndexFinder.Wait(); _storage[_lastIndexFinder.Result] = o; + EventBinders.OfType>().FirstOrDefault()?.BindEvents((T)o, context); + var last = _lastIndexFinder.Result; _lastIndexFinder = Task.Run(FindLastIndex); @@ -71,18 +76,21 @@ namespace mROA.Implementation.Backend public T GetObject(int id) { - return id == -1 || _storage.Length <= id ? throw new NullReferenceException("Cannot find that object. It is null"): (T)_storage[id]!; + return id == -1 || _storage.Length <= id + ? throw new NullReferenceException("Cannot find that object. It is null") + : (T)_storage[id]!; } public object GetSingleObject(Type type) { - return _singletons.GetValueOrDefault(type.GetHashCode()) ?? throw new ArgumentException("Unregistered singleton type"); + return _singletons.GetValueOrDefault(type.GetHashCode()) ?? + throw new ArgumentException("Unregistered singleton type"); } - public int GetObjectIndex(object o, IEndPointContext context) + public int GetObjectIndex(object o, IEndPointContext context) { var index = Array.IndexOf(_storage, o); - return index == -1 ? ResisterObject(o, context) : index; + return index == -1 ? ResisterObject(o, context) : index; } private int FindLastIndex() @@ -98,9 +106,9 @@ namespace mROA.Implementation.Backend _storage = nextStorage; return _storage.Length; } + public void Inject(T dependency) { } - } } \ No newline at end of file diff --git a/mROA/Implementation/Backend/MultiClientContextRepository.cs b/mROA/Implementation/Backend/MultiClientContextRepository.cs index e8dbef3..4b4194d 100644 --- a/mROA/Implementation/Backend/MultiClientContextRepository.cs +++ b/mROA/Implementation/Backend/MultiClientContextRepository.cs @@ -27,10 +27,10 @@ namespace mROA.Implementation.Backend { } - public int ResisterObject(object o, IEndPointContext context) + public int ResisterObject(object o, IEndPointContext context) { var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId()); - return repository.ResisterObject(o, context); + return repository.ResisterObject(o, context); } public void ClearObject(int id) @@ -63,10 +63,10 @@ namespace mROA.Implementation.Backend return repository.GetSingleObject(type); } - public int GetObjectIndex(object o, IEndPointContext context) + public int GetObjectIndex(object o, IEndPointContext context) { var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId()); - return repository.GetObjectIndex(o, context); + return repository.GetObjectIndex(o, context); } public IContextRepository GetRepository(int clientId) diff --git a/mROA/Implementation/RemoteContextRepository.cs b/mROA/Implementation/RemoteContextRepository.cs index 4e0f6b6..e86e74f 100644 --- a/mROA/Implementation/RemoteContextRepository.cs +++ b/mROA/Implementation/RemoteContextRepository.cs @@ -9,7 +9,7 @@ namespace mROA.Implementation private IRepresentationModuleProducer? _representationProducer; public static Dictionary RemoteTypes = new(); - public int ResisterObject(object o, IEndPointContext context) + public int ResisterObject(object o, IEndPointContext context) { throw new NotSupportedException(); } @@ -58,7 +58,7 @@ namespace mROA.Implementation representationModule)!; } - public int GetObjectIndex(object o, IEndPointContext context) + public int GetObjectIndex(object o, IEndPointContext context) { if (o is RemoteObjectBase remote) { diff --git a/mROA/Implementation/SharedObjectShell.cs b/mROA/Implementation/SharedObjectShell.cs index 8a2917d..3fba873 100644 --- a/mROA/Implementation/SharedObjectShell.cs +++ b/mROA/Implementation/SharedObjectShell.cs @@ -49,7 +49,7 @@ namespace mROA.Implementation else { _identifier.OwnerId = EndPointContext.HostId; - _identifier.ContextId = EndPointContext.RealRepository.GetObjectIndex(Value, EndPointContext); + _identifier.ContextId = EndPointContext.RealRepository.GetObjectIndex(Value, EndPointContext); } } }