diff --git a/mROA/Abstract/IRemoteObjectFactory.cs b/mROA/Abstract/IRemoteObjectFactory.cs new file mode 100644 index 0000000..e68fb0f --- /dev/null +++ b/mROA/Abstract/IRemoteObjectFactory.cs @@ -0,0 +1,9 @@ +using mROA.Implementation; + +namespace mROA.Abstract +{ + public interface IRemoteObjectFactory : IInjectableModule + { + T Produce(ComplexObjectIdentifier id); + } +} \ No newline at end of file diff --git a/mROA/Implementation/ComplexObjectIdentifier.cs b/mROA/Implementation/ComplexObjectIdentifier.cs index 35652ee..15ede59 100644 --- a/mROA/Implementation/ComplexObjectIdentifier.cs +++ b/mROA/Implementation/ComplexObjectIdentifier.cs @@ -12,9 +12,10 @@ namespace mROA.Implementation { ContextId = contextId; OwnerId = ownerId; - } + public static ComplexObjectIdentifier Singleton(int ownerId) => new() { ContextId = -1, OwnerId = ownerId }; + public static ComplexObjectIdentifier Null = new ComplexObjectIdentifier { ContextId = -2, OwnerId = -1 }; public static ComplexObjectIdentifier FromFlat(ulong flat) => new() { Flat = flat }; diff --git a/mROA/Implementation/RemoteObjectFactory.cs b/mROA/Implementation/RemoteObjectFactory.cs new file mode 100644 index 0000000..fad21e3 --- /dev/null +++ b/mROA/Implementation/RemoteObjectFactory.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using mROA.Abstract; + +namespace mROA.Implementation +{ + public class RemoteObjectFactory : IRemoteObjectFactory + { + public static Dictionary RemoteTypes = new(); + private IRepresentationModuleProducer? _representationProducer; + + public T Produce(ComplexObjectIdentifier id) + { + if (_representationProducer == null) + throw new NullReferenceException("representation producer is not initialized"); + + if (!RemoteTypes.TryGetValue(typeof(T), out var remoteType)) throw new NotSupportedException(); + var representationModule = + _representationProducer.Produce(TransmissionConfig.OwnershipRepository.GetOwnershipId()); + var remote = (T)Activator.CreateInstance(remoteType, id.ContextId, + representationModule)!; + return remote; + } + + public void Inject(T dependency) + { + if (dependency is IRepresentationModuleProducer serialisationModule) + _representationProducer = serialisationModule; + } + } +} \ No newline at end of file