Теоретически должно работать!!!
This commit is contained in:
@@ -67,6 +67,7 @@ namespace mROA.Codegen
|
||||
// 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>();
|
||||
|
||||
|
||||
@@ -145,7 +146,7 @@ namespace mROA.Codegen
|
||||
}
|
||||
}
|
||||
|
||||
GenerateEventImplementation(classSymbol, invokers, declaredMethods, context);
|
||||
GenerateEventImplementation(classSymbol, invokers, declaredMethods, context, eventBinders);
|
||||
|
||||
var code = $@"// <auto-generated/>
|
||||
|
||||
@@ -222,11 +223,12 @@ namespace mROA.Codegen
|
||||
if (frontendContextRepo.Count != 0)
|
||||
{
|
||||
var fronendRepoCode = @$"// <auto-generated/>
|
||||
using mROA.Implementation;
|
||||
using mROA.Abstract;
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using System;
|
||||
using mROA.Abstract;
|
||||
using mROA.Implementation.Backend;
|
||||
using mROA.Implementation;
|
||||
|
||||
namespace mROA.Codegen
|
||||
{{
|
||||
@@ -235,6 +237,8 @@ namespace mROA.Codegen
|
||||
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)}}};
|
||||
}}
|
||||
}}
|
||||
}}
|
||||
@@ -246,7 +250,7 @@ namespace mROA.Codegen
|
||||
}
|
||||
|
||||
private void GenerateEventImplementation(INamedTypeSymbol classSymbol, List<string> invokers,
|
||||
List<string> declaredMethods, GeneratorExecutionContext context)
|
||||
List<string> declaredMethods, GeneratorExecutionContext context, List<string> binders)
|
||||
{
|
||||
var events = classSymbol.AllInterfaces.Add(classSymbol).SelectMany(i => i.GetMembers())
|
||||
.OfType<IEventSymbol>().ToList();
|
||||
@@ -254,8 +258,7 @@ namespace mROA.Codegen
|
||||
return;
|
||||
|
||||
var additionalSignatures = new List<string>(events.Count);
|
||||
var namespaceName = classSymbol.ContainingNamespace.ToDisplayString();
|
||||
|
||||
var singleEventBinder = new List<string>(events.Count);
|
||||
for (int i = 0; i < events.Count; i++)
|
||||
{
|
||||
var currentEvent = events[i];
|
||||
@@ -264,6 +267,7 @@ namespace mROA.Codegen
|
||||
declaredMethods.Add(additionalMethod);
|
||||
additionalSignatures.Add(signature);
|
||||
GenerateEventCode(currentEvent, invokers, classSymbol);
|
||||
GenerateBinderCode(currentEvent, invokers, classSymbol, singleEventBinder);
|
||||
}
|
||||
|
||||
var partialInterface = $@"
|
||||
@@ -275,6 +279,16 @@ namespace {classSymbol.ContainingNamespace.ToDisplayString()}
|
||||
}}
|
||||
}}
|
||||
";
|
||||
var binder = $@"new EventBinder<{classSymbol.ToDisplayString()}>
|
||||
{{
|
||||
BindAction = (instance, context, representationProducer, index) =>
|
||||
{{
|
||||
var module = representationProducer.Produce(context.OwnerId);
|
||||
|
||||
{string.Join("\r\n", singleEventBinder)}
|
||||
}}
|
||||
}}";
|
||||
binders.Add(binder);
|
||||
#if !DONT_ADD
|
||||
context.AddSource($"{classSymbol.Name}.g.cs", SourceText.From(partialInterface, Encoding.UTF8));
|
||||
#endif
|
||||
@@ -439,6 +453,41 @@ namespace {classSymbol.ContainingNamespace.ToDisplayString()}
|
||||
invokers.Add(backend);
|
||||
}
|
||||
|
||||
private void GenerateBinderCode(IEventSymbol eventSymbol, List<string> invokers, INamedTypeSymbol baseType,
|
||||
List<string> binders)
|
||||
{
|
||||
var index = invokers.Count - 1;
|
||||
var parameters = (eventSymbol.Type as INamedTypeSymbol).TypeArguments.ToList();
|
||||
int parameterIndex = 0;
|
||||
var parametersDeclaration = string.Join(", ",
|
||||
JoinWithComa(Enumerable.Range(0, parameters.Count).Select(i => "p" + i++)));
|
||||
|
||||
var transferParameters =
|
||||
JoinWithComa(parameters.Where(i => ParameterFilterForType(i)).Select(i => "p" + parameters.IndexOf(i)));
|
||||
|
||||
var callFilter = "";
|
||||
|
||||
var requestIndex = parameters.FindIndex(i => i.Name == "RequestContext");
|
||||
if (requestIndex != -1)
|
||||
{
|
||||
callFilter = $"\n\r\t\t\tif(context.OwnerId == p{requestIndex}.OwnerId) return;";
|
||||
}
|
||||
|
||||
var eventBinderCode =
|
||||
$@" (instance as {baseType.ToDisplayString()}).{eventSymbol.Name} += ({parametersDeclaration}) =>
|
||||
{{ {callFilter}
|
||||
var request = new DefaultCallRequest
|
||||
{{
|
||||
CommandId = {index}, ObjectId = index, Parameters = new object[] {{ {transferParameters} }}
|
||||
}};
|
||||
module.PostCallMessageAsync(request.Id, MessageType.EventRequest, request);
|
||||
}};
|
||||
";
|
||||
binders.Add(eventBinderCode);
|
||||
}
|
||||
|
||||
public static string JoinWithComa(IEnumerable<string> parts) => string.Join(", ", parts);
|
||||
|
||||
private void GenerateEventCode(IEventSymbol eventSymbol, List<string> invokers, ITypeSymbol baseInterface)
|
||||
{
|
||||
var level = "\t\t\t";
|
||||
|
||||
Reference in New Issue
Block a user