From acb5a2cf58d77c4e9d7d969b1b5105f772ede721 Mon Sep 17 00:00:00 2001 From: Mitrofanov Mikhail Date: Fri, 27 Jun 2025 17:51:52 +0300 Subject: [PATCH] Rewrite to direct constructors --- Example.Backend/Program.cs | 2 ++ Example.Shared/Example.Shared.csproj | 4 --- mROA.Codegen/RemoteTypeBinder.cstmpl | 2 +- mROA.Codegen/mROASourceGenerator.cs | 2 +- mROA/Abstract/ICallIndexProvider.cs | 11 +++++++ mROA/Abstract/IInstanceRepository.cs | 2 +- .../Backend/InstanceRepository.cs | 2 +- .../Backend/MultiClientInstanceRepository.cs | 2 +- .../Bootstrap/FullMixBuilder.cs | 3 +- mROA/Implementation/CallRequest.cs | 1 + .../RemoteInstanceRepository.cs | 27 +++++++++------- mROA/Implementation/RemoteObjectFactory.cs | 31 ------------------- mROA/Implementation/SharedObjectShell.cs | 4 +-- 13 files changed, 38 insertions(+), 55 deletions(-) create mode 100644 mROA/Abstract/ICallIndexProvider.cs delete mode 100644 mROA/Implementation/RemoteObjectFactory.cs diff --git a/Example.Backend/Program.cs b/Example.Backend/Program.cs index 15e0604..8778a07 100644 --- a/Example.Backend/Program.cs +++ b/Example.Backend/Program.cs @@ -13,6 +13,8 @@ class Program { public static void Main(string[] args) { + new mROA.Codegen.CoCodegenMethodRepository(); + new CoCodegenMethodRepository(); var builder = new FullMixBuilder(); // builder.UseJsonSerialisation(); builder.Modules.Add(new CborSerializationToolkit()); diff --git a/Example.Shared/Example.Shared.csproj b/Example.Shared/Example.Shared.csproj index db58133..40a196f 100644 --- a/Example.Shared/Example.Shared.csproj +++ b/Example.Shared/Example.Shared.csproj @@ -13,8 +13,4 @@ - - - - diff --git a/mROA.Codegen/RemoteTypeBinder.cstmpl b/mROA.Codegen/RemoteTypeBinder.cstmpl index 69eff9c..f343fc2 100644 --- a/mROA.Codegen/RemoteTypeBinder.cstmpl +++ b/mROA.Codegen/RemoteTypeBinder.cstmpl @@ -11,7 +11,7 @@ namespace mROA.Codegen public sealed class RemoteTypeBinder { static RemoteTypeBinder(){ - RemoteInstanceRepository.RemoteTypes = new Dictionary { + RemoteInstanceRepository.RemoteTypeFactories = new Dictionary> { , }; diff --git a/mROA.Codegen/mROASourceGenerator.cs b/mROA.Codegen/mROASourceGenerator.cs index 9bfe2ba..d1358bb 100644 --- a/mROA.Codegen/mROASourceGenerator.cs +++ b/mROA.Codegen/mROASourceGenerator.cs @@ -175,7 +175,7 @@ namespace mROA.Codegen context.AddSource($"{className}.g.cs", SourceText.From(code, Encoding.UTF8)); #endif _binderTemplate.Insert("remoteTypePair", - $"{{ typeof({classSymbol.ToUnityString()}), typeof({namespaceName}.{className}) }}"); + $"{{ typeof({classSymbol.ToUnityString()}), (i, r, c) => new {namespaceName}.{className}(i, r, c) }}"); } if (totalMethods.Count != 0) diff --git a/mROA/Abstract/ICallIndexProvider.cs b/mROA/Abstract/ICallIndexProvider.cs new file mode 100644 index 0000000..f4f6c7c --- /dev/null +++ b/mROA/Abstract/ICallIndexProvider.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; + + +namespace mROA.Abstract +{ + public interface ICallIndexProvider : IInjectableModule + { + int[] GetIndecies(Type type); + } +} \ No newline at end of file diff --git a/mROA/Abstract/IInstanceRepository.cs b/mROA/Abstract/IInstanceRepository.cs index 53d949d..a0886ba 100644 --- a/mROA/Abstract/IInstanceRepository.cs +++ b/mROA/Abstract/IInstanceRepository.cs @@ -7,7 +7,7 @@ namespace mROA.Abstract { int ResisterObject(object o, IEndPointContext context); void ClearObject(ComplexObjectIdentifier id, IEndPointContext context); - T GetObject(ComplexObjectIdentifier id, IEndPointContext context); + T GetObject(ComplexObjectIdentifier id, IEndPointContext context) where T : class; T GetSingletonObject(IEndPointContext context) where T : class, IShared; object GetSingletonObject(Type type, IEndPointContext context); int GetObjectIndex(object o, IEndPointContext context); diff --git a/mROA/Implementation/Backend/InstanceRepository.cs b/mROA/Implementation/Backend/InstanceRepository.cs index dbece06..377e1cd 100644 --- a/mROA/Implementation/Backend/InstanceRepository.cs +++ b/mROA/Implementation/Backend/InstanceRepository.cs @@ -49,7 +49,7 @@ namespace mROA.Implementation.Backend _storage.Free(id.ContextId); } - public T GetObject(ComplexObjectIdentifier id, IEndPointContext context) + public T GetObject(ComplexObjectIdentifier id, IEndPointContext context) where T : class { var value = _storage.GetValue(id.ContextId); diff --git a/mROA/Implementation/Backend/MultiClientInstanceRepository.cs b/mROA/Implementation/Backend/MultiClientInstanceRepository.cs index 8cfc9d2..6ced8f5 100644 --- a/mROA/Implementation/Backend/MultiClientInstanceRepository.cs +++ b/mROA/Implementation/Backend/MultiClientInstanceRepository.cs @@ -32,7 +32,7 @@ namespace mROA.Implementation.Backend repository.ClearObject(id, context); } - public T GetObject(ComplexObjectIdentifier id, IEndPointContext context) + public T GetObject(ComplexObjectIdentifier id, IEndPointContext context) where T : class { var repository = GetRepositoryByClientId(context.OwnerId); return repository.GetObject(id, context); diff --git a/mROA/Implementation/Bootstrap/FullMixBuilder.cs b/mROA/Implementation/Bootstrap/FullMixBuilder.cs index 3b7a9ae..2129f35 100644 --- a/mROA/Implementation/Bootstrap/FullMixBuilder.cs +++ b/mROA/Implementation/Bootstrap/FullMixBuilder.cs @@ -6,10 +6,11 @@ namespace mROA.Implementation.Bootstrap { public class FullMixBuilder { - public List Modules { get; } = new() { }; + public List Modules { get; } = new(); public void Build() { + foreach (var module in Modules) foreach (var injection in Modules) module.Inject(injection); diff --git a/mROA/Implementation/CallRequest.cs b/mROA/Implementation/CallRequest.cs index 6a909b0..c81ae5f 100644 --- a/mROA/Implementation/CallRequest.cs +++ b/mROA/Implementation/CallRequest.cs @@ -23,6 +23,7 @@ namespace mROA.Implementation public override string ToString() { + return $"Call request {{ Id : {Id}, CommandId : {CommandId}, ObjectId : {ObjectId} }}"; } } diff --git a/mROA/Implementation/RemoteInstanceRepository.cs b/mROA/Implementation/RemoteInstanceRepository.cs index 68ea5f8..8d39993 100644 --- a/mROA/Implementation/RemoteInstanceRepository.cs +++ b/mROA/Implementation/RemoteInstanceRepository.cs @@ -8,7 +8,10 @@ namespace mROA.Implementation public class RemoteInstanceRepository : IInstanceRepository { private List _producedProxys = new(); - public static Dictionary RemoteTypes = new(); + + public static Dictionary> + RemoteTypeFactories = new(); + private IRepresentationModuleProducer? _representationProducer; public int HostId { get; set; } @@ -23,7 +26,7 @@ namespace mROA.Implementation throw new NotSupportedException(); } - public T GetObject(ComplexObjectIdentifier id, IEndPointContext context) + public T GetObject(ComplexObjectIdentifier id, IEndPointContext context) where T : class { var index = _producedProxys.Find(i => i.Identifier.Equals(id)); if (index is not null) @@ -31,20 +34,20 @@ namespace mROA.Implementation if (_representationProducer == null) throw new NullReferenceException("representation producer is not initialized"); - if (!RemoteTypes.TryGetValue(typeof(T), out var remoteType)) throw new NotSupportedException(); + if (!RemoteTypeFactories.TryGetValue(typeof(T), out var remoteType)) throw new NotSupportedException(); var representationModule = _representationProducer.Produce(context.OwnerId); - var remote = (T)Activator.CreateInstance(remoteType, id.ContextId, - representationModule, context)!; + var remote = remoteType(id.ContextId, + representationModule, context); - _producedProxys.Add((remote as RemoteObjectBase)!); + _producedProxys.Add(remote!); - return remote; + return (remote as T)!; } public T GetSingletonObject(IEndPointContext context) where T : class, IShared { - return GetSingletonObject(typeof(T), context) as T; + return (GetSingletonObject(typeof(T), context) as T)!; } public object GetSingletonObject(Type type, IEndPointContext context) @@ -55,10 +58,10 @@ namespace mROA.Implementation var representationModule = _representationProducer.Produce(context.OwnerId); - var instance = Activator.CreateInstance(RemoteTypes[type], -1, representationModule, context); - - var remoteObjectBase = (RemoteObjectBase)instance; - + var instance = RemoteTypeFactories[type](-1, representationModule, context)!; + + var remoteObjectBase = instance; + _producedProxys.Add(remoteObjectBase); return _producedProxys.Last(); diff --git a/mROA/Implementation/RemoteObjectFactory.cs b/mROA/Implementation/RemoteObjectFactory.cs deleted file mode 100644 index 8fa0873..0000000 --- a/mROA/Implementation/RemoteObjectFactory.cs +++ /dev/null @@ -1,31 +0,0 @@ -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, IEndPointContext context) - { - 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(context.OwnerId); - 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 diff --git a/mROA/Implementation/SharedObjectShell.cs b/mROA/Implementation/SharedObjectShell.cs index e6783c6..c2ce05e 100644 --- a/mROA/Implementation/SharedObjectShell.cs +++ b/mROA/Implementation/SharedObjectShell.cs @@ -16,7 +16,7 @@ namespace mROA.Implementation object UniversalValue { get; set; } } - public class SharedObjectShellShell : ISharedObjectShell where T : notnull + public class SharedObjectShellShell : ISharedObjectShell where T : class { private ComplexObjectIdentifier _identifier = ComplexObjectIdentifier.Null; @@ -26,7 +26,7 @@ namespace mROA.Implementation // ReSharper disable once UnusedMember.Global public SharedObjectShellShell() { - _value = default; + _value = null; EndPointContext = new EndPointContext(); }