Beispiel #1
0
        private static Text GenerateCommandSerializer(UnityCommandDetails command, string qualifiedNamespace,
                                                      string componentName)
        {
            Logger.Trace($"Generating {qualifiedNamespace}.{componentName}.{command.CommandName}CommandSerializer class.");

            return(Text.New($@"
private class {command.CommandName}CommandSerializer : ICommandSerializer
{{
    public void Serialize(MessagesToSend messages, SerializedMessagesToSend serializedMessages, CommandMetaData commandMetaData)
    {{
        var storage = ({command.CommandName}CommandsToSendStorage) messages.GetCommandSendStorage(ComponentId, {command.CommandIndex});

        var requests = storage.GetRequests();

        for (int i = 0; i < requests.Count; ++i)
        {{
            ref readonly var request = ref requests[i];
            var context = new CommandContext<{command.FqnRequestType}>(request.SendingEntity, request.Request.Payload, request.Request.Context, request.RequestId);
            commandMetaData.AddRequest<{command.FqnRequestType}>(ComponentId, {command.CommandIndex}, in context);

            var schemaCommandRequest = global::Improbable.Worker.CInterop.SchemaCommandRequest.Create();
            {command.FqnRequestType}.Serialization.Serialize(request.Request.Payload, schemaCommandRequest.GetObject());
            var serializedRequest = new global::Improbable.Worker.CInterop.CommandRequest(ComponentId, {command.CommandIndex}, schemaCommandRequest);

            serializedMessages.AddRequest(serializedRequest, {command.CommandIndex},
                request.Request.TargetEntityId.Id, request.Request.TimeoutMillis, request.RequestId);
        }}

        var responses = storage.GetResponses();
        for (int i = 0; i < responses.Count; ++i)
        {{
            ref readonly var response = ref responses[i];
            if (response.FailureMessage != null)
            {{
                // Send a command failure if the string is non-null.

                serializedMessages.AddFailure(ComponentId, {command.CommandIndex}, response.FailureMessage, (uint) response.RequestId);
                continue;
            }}

            var schemaCommandResponse = global::Improbable.Worker.CInterop.SchemaCommandResponse.Create();
            {command.FqnResponseType}.Serialization.Serialize(response.Payload.Value, schemaCommandResponse.GetObject());

            var serializedResponse = new global::Improbable.Worker.CInterop.CommandResponse(ComponentId, {command.CommandIndex}, schemaCommandResponse);

            serializedMessages.AddResponse(serializedResponse, (uint) response.RequestId);
        }}
    }}
}}
"));
        }
Beispiel #2
0
        private static Text GenerateCommandsToSendStorage(UnityCommandDetails command, string qualifiedNamespace, string componentName)
        {
            Logger.Trace($"Generating {qualifiedNamespace}.{componentName}.{command.CommandName}CommandsToSendStorage class.");

            return(Text.New($@"
private class {command.CommandName}CommandsToSendStorage :
    CommandSendStorage<{command.CommandName}.Request, {command.CommandName}.Response>,
    IComponentCommandSendStorage
{{
    uint IComponentCommandSendStorage.ComponentId => ComponentId;
    uint IComponentCommandSendStorage.CommandId => {command.CommandIndex};
}}
"));
        }
Beispiel #3
0
        private static Text GenerateDiffCommandDeserializer(UnityCommandDetails command, string qualifiedNamespace, string componentName)
        {
            Logger.Trace($"Generating {qualifiedNamespace}.{componentName}.{command.PascalCaseName}DiffCommandDeserializer class.");

            return(Text.New($@"
private class {command.PascalCaseName}DiffCommandDeserializer : ICommandDiffDeserializer
{{
    public void AddRequestToDiff(CommandRequestOp op, ViewDiff diff)
    {{
        var deserializedRequest = {command.FqnRequestType}.Serialization.Deserialize(op.Request.SchemaData.Value.GetObject());

        var request = new {command.PascalCaseName}.ReceivedRequest(
            new EntityId(op.EntityId),
            op.RequestId,
            op.CallerWorkerId,
            op.CallerAttributeSet,
            deserializedRequest);

        diff.AddCommandRequest(request, ComponentId, {command.CommandIndex});
    }}

    public void AddResponseToDiff(CommandResponseOp op, ViewDiff diff, CommandMetaData commandMetaData)
    {{
        {command.FqnResponseType}? rawResponse = null;
        if (op.StatusCode == StatusCode.Success)
        {{
            rawResponse = {command.FqnResponseType}.Serialization.Deserialize(op.Response.SchemaData.Value.GetObject());
        }}

        var internalRequestId = new InternalCommandRequestId(op.RequestId);
        var commandContext = commandMetaData.GetContext<{command.FqnRequestType}>(ComponentId, {command.CommandIndex}, internalRequestId);
        commandMetaData.RemoveRequest(ComponentId, {command.CommandIndex}, internalRequestId);

        var response = new {command.PascalCaseName}.ReceivedResponse(
            commandContext.SendingEntity,
            new EntityId(op.EntityId),
            op.Message,
            op.StatusCode,
            rawResponse,
            commandContext.Request,
            commandContext.Context,
            commandContext.RequestId);

        diff.AddCommandResponse(response, ComponentId, {command.CommandIndex});
    }}
}}
"));
        }
Beispiel #4
0
        private static Text GenerateCommandStorage(UnityCommandDetails command, string qualifiedNamespace, string componentName)
        {
            Logger.Trace($"Generating {qualifiedNamespace}.{componentName}.{command.CommandName}CommandStorage class.");

            var receivedRequestType  = $"{command.CommandName}.ReceivedRequest";
            var receivedResponseType = $"{command.CommandName}.ReceivedResponse";

            return(Text.New($@"
private class Diff{command.CommandName}CommandStorage
    : DiffSpawnCubeCommandStorage<{receivedRequestType}, {receivedResponseType}>
{{
    public override uint ComponentId => {qualifiedNamespace}.{componentName}.ComponentId;
    public override uint CommandId => {command.CommandIndex};
}}
"));
        }
Beispiel #5
0
        private static TypeBlock GenerateCommandMetaclass(string qualifiedNamespace, string componentName, UnityCommandDetails command)
        {
            Logger.Trace($"Generating {qualifiedNamespace}.{componentName}.{command.PascalCaseName}Metaclass class.");

            var rootNamespace = $"global::{qualifiedNamespace}.{componentName}";

            return(Scope.Type($"public class {command.PascalCaseName}Metaclass : ICommandMetaclass",
                              commandMetaclass =>
            {
                commandMetaclass.Line($@"
public uint CommandIndex => {command.CommandIndex};
public string Name => ""{command.PascalCaseName}"";

public Type DiffDeserializer {{ get; }} = typeof({rootNamespace}.{command.PascalCaseName}DiffCommandDeserializer);
public Type Serializer {{ get; }} = typeof({rootNamespace}.{command.PascalCaseName}CommandSerializer);

public Type MetaDataStorage {{ get; }} = typeof({rootNamespace}.{command.PascalCaseName}CommandMetaDataStorage);
public Type SendStorage {{ get; }} = typeof({rootNamespace}.{command.PascalCaseName}CommandsToSendStorage);
public Type DiffStorage {{ get; }} = typeof({rootNamespace}.Diff{command.PascalCaseName}CommandStorage);
");
            }));
        }