From 8e37eda0aef8882b5b05f796f217b257248dac46 Mon Sep 17 00:00:00 2001 From: Mihail Mitrovanov Date: Thu, 15 May 2025 08:20:32 +0300 Subject: [PATCH] Client useful method in instance repository added --- Example.Frontend/Program.cs | 6 +- mROA.Codegen/mROA.Codegen.csproj | 2 +- mROA.Codegen/mROASourceGenerator.cs | 19 ++--- mROA/Abstract/IInstanceRepository.cs | 3 +- .../Backend/BasicExecutionModule.cs | 2 +- .../Backend/InstanceRepository.cs | 8 ++- .../Backend/MultiClientInstanceRepository.cs | 10 ++- .../ComplexInstanceRepository.cs | 70 ------------------- .../RemoteInstanceRepository.cs | 7 +- mROA/mROA.csproj | 2 +- 10 files changed, 40 insertions(+), 89 deletions(-) delete mode 100644 mROA/Implementation/ComplexInstanceRepository.cs diff --git a/Example.Frontend/Program.cs b/Example.Frontend/Program.cs index 28957ce..5e1b7a7 100644 --- a/Example.Frontend/Program.cs +++ b/Example.Frontend/Program.cs @@ -46,8 +46,8 @@ class Program var context = builder.GetModule(); var factory = - context.GetSingleObject(typeof(IPrinterFactory), - builder.GetModule()) as IPrinterFactory; + context.GetSingletonObject( + builder.GetModule()); using (var disposingPrinter = factory.Create("Test")) { @@ -115,7 +115,7 @@ class Program DemoCheck.Dispose = true; - var loadSingleton = context.GetSingleObject(typeof(ILoadTest), builder.GetModule()) as ILoadTest; + var loadSingleton = context.GetSingletonObject(builder.GetModule()); var cts = new CancellationTokenSource(); diff --git a/mROA.Codegen/mROA.Codegen.csproj b/mROA.Codegen/mROA.Codegen.csproj index 765f637..0b91fce 100644 --- a/mROA.Codegen/mROA.Codegen.csproj +++ b/mROA.Codegen/mROA.Codegen.csproj @@ -17,7 +17,7 @@ https://github.com/YaslePoy/mROA git True - 2.0.1 + 2.0.3 diff --git a/mROA.Codegen/mROASourceGenerator.cs b/mROA.Codegen/mROASourceGenerator.cs index b2b9f26..8e3fafc 100644 --- a/mROA.Codegen/mROASourceGenerator.cs +++ b/mROA.Codegen/mROASourceGenerator.cs @@ -411,9 +411,11 @@ namespace mROA.Codegen var parametersDeclaration = string.Join(", ", JoinWithComa(Enumerable.Range(0, parameters.Count).Select(i => "p" + i))); + + int pi = 0; var transferParameters = - JoinWithComa(parameters.Where(i => !ParameterFilterForType(i)) - .Select(i => "p" + parameters.IndexOf(i))); + JoinWithComa(parameters.Select(i => (i, pi++)).Where(i => !ParameterFilterForType(i.i)) + .Select(i => "p" + i.Item2)); var requestIndex = parameters.FindIndex(i => i.Name == "RequestContext"); if (requestIndex != -1) @@ -440,15 +442,16 @@ namespace mROA.Codegen { var level = "\t\t\t"; - var parameters = ((INamedTypeSymbol)eventSymbol.Type).TypeArguments; - var parsingParameters = parameters.RemoveAll(ParameterFilterForType).ToList(); + int pi = 0; + var parameters = ((INamedTypeSymbol)eventSymbol.Type).TypeArguments.Select(i => (i, pi++)).ToImmutableArray(); + var parsingParameters = parameters.RemoveAll(i => ParameterFilterForType(i.i)).ToList(); var parameterTypes = string.Join(", ", - $"{string.Join(", ", parsingParameters.Select(p => $"typeof({p.ToUnityString()})"))}"); + $"{string.Join(", ", parsingParameters.Select(p => $"typeof({p.i.ToUnityString()})"))}"); var parametersInsertList = new List(); foreach (var parameter in parameters) - switch (parameter.Name) + switch (parameter.i.Name) { case "CancellationToken": parametersInsertList.Add("(CancellationToken)special[1]"); @@ -457,8 +460,8 @@ namespace mROA.Codegen parametersInsertList.Add("special[0] as RequestContext"); break; default: - parametersInsertList.Add(Caster(parameter, - $"parameters[{parameters.IndexOf(parameter)}]")); + parametersInsertList.Add(Caster(parameter.i, + $"parameters[{parameter.Item2}]")); break; } diff --git a/mROA/Abstract/IInstanceRepository.cs b/mROA/Abstract/IInstanceRepository.cs index 7086178..53d949d 100644 --- a/mROA/Abstract/IInstanceRepository.cs +++ b/mROA/Abstract/IInstanceRepository.cs @@ -8,7 +8,8 @@ namespace mROA.Abstract int ResisterObject(object o, IEndPointContext context); void ClearObject(ComplexObjectIdentifier id, IEndPointContext context); T GetObject(ComplexObjectIdentifier id, IEndPointContext context); - object GetSingleObject(Type type, IEndPointContext context); + T GetSingletonObject(IEndPointContext context) where T : class, IShared; + object GetSingletonObject(Type type, IEndPointContext context); int GetObjectIndex(object o, IEndPointContext context); } } \ No newline at end of file diff --git a/mROA/Implementation/Backend/BasicExecutionModule.cs b/mROA/Implementation/Backend/BasicExecutionModule.cs index 69ceee4..0387bf1 100644 --- a/mROA/Implementation/Backend/BasicExecutionModule.cs +++ b/mROA/Implementation/Backend/BasicExecutionModule.cs @@ -91,7 +91,7 @@ namespace mROA.Implementation.Backend { var context = command.ObjectId.ContextId != -1 ? instanceRepository.GetObject(command.ObjectId, endPointContext) - : instanceRepository.GetSingleObject(invoker.SuitableType, endPointContext); + : instanceRepository.GetSingletonObject(invoker.SuitableType, endPointContext); return context; } diff --git a/mROA/Implementation/Backend/InstanceRepository.cs b/mROA/Implementation/Backend/InstanceRepository.cs index 4c7c9e5..dbece06 100644 --- a/mROA/Implementation/Backend/InstanceRepository.cs +++ b/mROA/Implementation/Backend/InstanceRepository.cs @@ -61,7 +61,13 @@ namespace mROA.Implementation.Backend return (T)value; } - public object GetSingleObject(Type type, IEndPointContext context) + public T GetSingletonObject(IEndPointContext context) where T : class, IShared + { + return GetSingletonObject(typeof(T), context) as T ?? + throw new ArgumentException("Unregistered singleton type"); + } + + public object GetSingletonObject(Type type, IEndPointContext context) { return _singletons.GetValueOrDefault(type.GetHashCode()) ?? throw new ArgumentException("Unregistered singleton type"); diff --git a/mROA/Implementation/Backend/MultiClientInstanceRepository.cs b/mROA/Implementation/Backend/MultiClientInstanceRepository.cs index 520e4cb..8cfc9d2 100644 --- a/mROA/Implementation/Backend/MultiClientInstanceRepository.cs +++ b/mROA/Implementation/Backend/MultiClientInstanceRepository.cs @@ -38,10 +38,16 @@ namespace mROA.Implementation.Backend return repository.GetObject(id, context); } - public object GetSingleObject(Type type, IEndPointContext context) + public T GetSingletonObject(IEndPointContext context) where T : class, IShared { var repository = GetRepositoryByClientId(context.OwnerId); - return repository.GetSingleObject(type, context); + return repository.GetSingletonObject(context); + } + + public object GetSingletonObject(Type type, IEndPointContext context) + { + var repository = GetRepositoryByClientId(context.OwnerId); + return repository.GetSingletonObject(type, context); } public int GetObjectIndex(object o, IEndPointContext context) diff --git a/mROA/Implementation/ComplexInstanceRepository.cs b/mROA/Implementation/ComplexInstanceRepository.cs deleted file mode 100644 index 09be507..0000000 --- a/mROA/Implementation/ComplexInstanceRepository.cs +++ /dev/null @@ -1,70 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using mROA.Abstract; - -namespace mROA.Implementation -{ - public class ComplexInstanceRepository : IInstanceRepository - { - private List>> _storages = new(); - public static object[] EventBinders = { }; - - private IRemoteObjectFactory? _remoteObjectFactory; - private IRepresentationModuleProducer? _representationModuleProducer; - - public void Inject(T dependency) - { - if (dependency is IRemoteObjectFactory remoteObjectFactory) - { - _remoteObjectFactory = remoteObjectFactory; - } - - if (dependency is IRepresentationModuleProducer moduleProducer) - { - _representationModuleProducer = moduleProducer; - } - } - - public int HostId { get; set; } - - public int ResisterObject(object o, IEndPointContext context) - { - var storageIndex = _storages.FindIndex(i => i.Key == context.OwnerId); - if (storageIndex == -1) - { - _storages.Add( - new KeyValuePair>(context.OwnerId, new ExtensibleStorage())); - storageIndex = _storages.Count - 1; - } - - var storage = _storages[storageIndex].Value; - - var placedIndex = storage.Place(o); - EventBinders.OfType>().FirstOrDefault() - ?.BindEvents((T)o, context, _representationModuleProducer!, placedIndex); - - return placedIndex; - } - - public void ClearObject(ComplexObjectIdentifier id, IEndPointContext context) - { - _storages.Find(i => i.Key == id.OwnerId).Value.Free(id.ContextId); - } - - public T GetObject(ComplexObjectIdentifier id, IEndPointContext context) - { - throw new NotImplementedException(); - } - - public object GetSingleObject(Type type, IEndPointContext context) - { - throw new NotImplementedException(); - } - - public int GetObjectIndex(object o, IEndPointContext context) - { - throw new NotImplementedException(); - } - } -} \ No newline at end of file diff --git a/mROA/Implementation/RemoteInstanceRepository.cs b/mROA/Implementation/RemoteInstanceRepository.cs index 1d00f42..39ae9ef 100644 --- a/mROA/Implementation/RemoteInstanceRepository.cs +++ b/mROA/Implementation/RemoteInstanceRepository.cs @@ -42,7 +42,12 @@ namespace mROA.Implementation return remote; } - public object GetSingleObject(Type type, IEndPointContext context) + public T GetSingletonObject(IEndPointContext context) where T : class, IShared + { + return GetSingletonObject(typeof(T), context) as T; + } + + public object GetSingletonObject(Type type, IEndPointContext context) { if (_representationProducer == null) throw new NullReferenceException("representation producer is not initialized"); diff --git a/mROA/mROA.csproj b/mROA/mROA.csproj index 3c172de..6c53c78 100644 --- a/mROA/mROA.csproj +++ b/mROA/mROA.csproj @@ -4,7 +4,7 @@ netstandard2.1 enable mROA - 2.0.0 + 2.0.1 YaslePoy Fast and easy RPC with contex https://github.com/YaslePoy/mROA