Частично переписан RemoteTypeBinder

This commit is contained in:
2025-03-25 21:40:12 +03:00
parent bf636c0f9c
commit a48c77e48f
5 changed files with 39 additions and 33 deletions
+4 -2
View File
@@ -7,9 +7,11 @@ using mROA.Abstract;
namespace <!L namespaceName>
{
partial class <!L className> : RemoteObjectBase, <!L originalName>
partial class <!L className>
: RemoteObjectBase, <!L originalName>
{
public <!L className>(int id, IRepresentationModule representationModule) : base(id, representationModule)
public <!L className>(int id, IRepresentationModule representationModule)
: base(id, representationModule)
{
}
+22
View File
@@ -0,0 +1,22 @@
// <auto-generated/>
using System.Collections.Generic;
using System.Reflection;
using System;
using mROA.Abstract;
using mROA.Implementation.Backend;
using mROA.Implementation;
namespace mROA.Codegen
{
public sealed class RemoteTypeBinder
{
static RemoteTypeBinder(){
RemoteContextRepository.RemoteTypes = new Dictionary<Type, Type> {
<!I remoteTypePair r sep typesSep><!D typesSep>,
<!D>};
ContextRepository.EventBinders = new object[] {
<!I eventBinder r sep typesSep>
};
}
}
}
+1 -1
View File
@@ -9,7 +9,7 @@ namespace mROA.CodegenTools
{
public List<ITemplateSection> Parts { get; set; } = new List<ITemplateSection>();
public object AdditionalContext { get; set; }
public ITagged this[string tag] => Parts.OfType<ITagged>().FirstOrDefault(i => i.Tag == tag);
public ITagged? this[string tag] => Parts.OfType<ITagged>().FirstOrDefault(i => i.Tag == tag);
public void Insert(string tag, object value)
{
+1
View File
@@ -35,6 +35,7 @@
<ItemGroup>
<EmbeddedResource Include="RemoteEndpoint.cstmpl" />
<EmbeddedResource Include="RemoteTypeBinder.cstmpl" />
<None Remove="MethodRepo.cstmpl"/>
<EmbeddedResource Include="MethodRepo.cstmpl"/>
</ItemGroup>
+11 -30
View File
@@ -23,6 +23,7 @@ namespace mROA.Codegen
private TemplateDocument MethodRepoTemplate;
private TemplateDocument ClassTemplateOriginal;
private TemplateDocument ClassTemplate;
private TemplateDocument BinderTemplate;
private static Predicate<IParameterSymbol> ParameterFilter =
i => i.Type.Name is "CancellationToken" or "RequestContext";
@@ -34,6 +35,7 @@ namespace mROA.Codegen
{
MethodRepoTemplate = TemplateReader.FromEmbeddedResource("MethodRepo.cstmpl");
ClassTemplateOriginal = TemplateReader.FromEmbeddedResource("RemoteEndpoint.cstmpl");
BinderTemplate = TemplateReader.FromEmbeddedResource("RemoteTypeBinder.cstmpl");
}
public void Execute(GeneratorExecutionContext context)
@@ -66,7 +68,7 @@ namespace mROA.Codegen
GenerateCode(context, context.Compilation, interfaces.ToImmutableArray());
}
catch (Exception)
catch (Exception e)
{
Console.WriteLine("ERROR: Unable to load method repository");
}
@@ -81,8 +83,7 @@ namespace mROA.Codegen
// var test = asm.GetManifestResourceStream("mROA.Codegen.test.tpt");
// var reader = new StreamReader(test);
// var allText = reader.ReadToEnd();
var frontendContextRepo = new List<string>();
var eventBinders = new List<string>();
List<IMethodSymbol> totalMethods = new List<IMethodSymbol>();
@@ -162,7 +163,7 @@ namespace mROA.Codegen
}
}
GenerateEventImplementation(classSymbol, invokers, declaredMethods, context, eventBinders);
GenerateEventImplementation(classSymbol, invokers, context);
ClassTemplate.AddDefine("className", className);
ClassTemplate.AddDefine("originalName", originalName);
@@ -175,7 +176,7 @@ namespace mROA.Codegen
#if !DONT_ADD
context.AddSource($"{className}.g.cs", SourceText.From(code, Encoding.UTF8));
#endif
frontendContextRepo.Add(
BinderTemplate.Insert("remoteTypePair",
$"{{ typeof({classSymbol.ToDisplayString()}), typeof({namespaceName}.{className}) }}");
}
@@ -193,37 +194,16 @@ namespace mROA.Codegen
#endif
}
if (frontendContextRepo.Count != 0)
if (BinderTemplate["remoteTypePair+"] != null)
{
var fronendRepoCode = @$"// <auto-generated/>
using System.Collections.Generic;
using System.Reflection;
using System;
using mROA.Abstract;
using mROA.Implementation.Backend;
using mROA.Implementation;
namespace mROA.Codegen
{{
public sealed class RemoteTypeBinder
{{
static RemoteTypeBinder(){{
RemoteContextRepository.RemoteTypes = new Dictionary<Type, Type> {{
{string.Join(", \r\n\t\t\t", frontendContextRepo)}}};
ContextRepository.EventBinders = new object[] {{
{string.Join(",\r\n\t\t\t", eventBinders)}}};
}}
}}
}}
";
var fronendRepoCode = BinderTemplate.Compile();
#if !DONT_ADD
context.AddSource("RemoteTypeBinder.g.cs", SourceText.From(fronendRepoCode, Encoding.UTF8));
#endif
}
}
private void GenerateEventImplementation(INamedTypeSymbol classSymbol, List<string> invokers,
List<string> declaredMethods, GeneratorExecutionContext context, List<string> binders)
private void GenerateEventImplementation(INamedTypeSymbol classSymbol, List<string> invokers, GeneratorExecutionContext context)
{
var events = classSymbol.AllInterfaces.Add(classSymbol).SelectMany(i => i.GetMembers())
.OfType<IEventSymbol>().ToList();
@@ -263,7 +243,8 @@ namespace {classSymbol.ContainingNamespace.ToDisplayString()}
{string.Join("\r\n", singleEventBinder)}
}}
}}";
binders.Add(binder);
BinderTemplate.Insert("eventBinder", binder);
#if !DONT_ADD
context.AddSource($"{classSymbol.Name}.g.cs", SourceText.From(partialInterface, Encoding.UTF8));
#endif