Example #1
0
        private static string GetComponentIcon(UnityComponentDetails details)
        {
            string componentIcon;

            switch (details.ComponentId)
            {
            case 53:     // Metadata
                componentIcon = "d_FilterByLabel";
                break;

            case 54:     //Position
                componentIcon = "Transform Icon";
                break;

            case 58:     // Interest
                componentIcon = "d_ViewToolOrbit";
                break;

            default:
                componentIcon = "d_TextAsset Icon";
                break;
            }

            if (details.Annotations.TryGetValue("improbable.gdk.editor.ComponentIcon", out var annotations))
            {
                var annotation    = annotations[0].TypeValue;
                var iconNameField = annotation.Fields.First(field => field.Name == "icon_name");
                componentIcon = iconNameField.Value.StringValue;
            }

            return(componentIcon);
        }
Example #2
0
        public CodeWriter Generate(UnityComponentDetails details)
        {
            return(CodeWriter.Populate(cgw =>
            {
                cgw.UsingDirectives(
                    "Unity.Entities",
                    "UnityEngine.UIElements",
                    "UnityEditor.UIElements",
                    "Improbable.Gdk.Debug.WorkerInspector.Codegen"
                    );

                cgw.Namespace(details.Namespace, ns =>
                {
                    ns.Type($"public class {details.Name}Renderer : ComponentVisualElement", type =>
                    {
                        type.Line($"public override ComponentType ComponentType {{ get; }} = ComponentType.ReadOnly<{details.Name}.Component>();");

                        type.TextList(details.FieldDetails.Select(typeGenerator.ToFieldDeclaration));

                        GenerateConstructor(type, details);
                        GenerateUpdateMethod(type, details);
                    });
                });
            }));
        }
Example #3
0
 private void GenerateDebugMethod(TypeBlock typeBlock, UnityComponentDetails details)
 {
     typeBlock.Method("protected override void WriteDebugInfo()", mb =>
     {
         mb.Line($"UnityEngine.Debug.Log(\"{details.Name} generated from '{details.SchemaFilePath}' with Component ID '{details.ComponentId}'\");");
     });
 }
        public static CodeWriter Generate(UnityComponentDetails componentDetails)
        {
            return(CodeWriter.Populate(cgw =>
            {
                cgw.UsingDirectives(
                    "Improbable.Gdk.Core",
                    "Improbable.Gdk.Core.Commands"
                    );

                cgw.Namespace(componentDetails.Namespace, ns =>
                {
                    ns.Type($"public partial class {componentDetails.Name}", partial =>
                    {
                        foreach (var command in componentDetails.CommandDetails)
                        {
                            Logger.Trace($"Generating {componentDetails.Namespace}.{componentDetails.Name}.{command.PascalCaseName}CommandMetaDataStorage class.");

                            partial.Line($@"
private class {command.PascalCaseName}CommandMetaDataStorage :
    CommandPayloadStorage<{command.FqnRequestType}>,
    ICommandMetaDataStorage
{{
    public uint CommandId => {command.CommandIndex};
}}
");
                        }
                    });
                });
            }));
        }
        public static string Generate(UnityComponentDetails componentDetails, string qualifiedNamespace)
        {
            return(CodeWriter.Populate(cgw =>
            {
                cgw.UsingDirectives(
                    "System",
                    "System.Collections.Generic",
                    "System.Linq",
                    "Improbable.Gdk.Core"
                    );

                cgw.Namespace(qualifiedNamespace, ns =>
                {
                    ns.Type($"public partial class {componentDetails.ComponentName}", partial =>
                    {
                        partial.Type("internal static class ReferenceTypeProviders", providers =>
                        {
                            foreach (var fieldDetails in componentDetails.FieldDetails.Where(fd => !fd.IsBlittable))
                            {
                                providers.Type(UnityReferenceTypeProviderContent.Generate(fieldDetails, qualifiedNamespace, componentDetails.ComponentName));
                            }
                        });
                    });
                });
            }).Format());
        }
Example #6
0
        public static CodeWriter Generate(UnityComponentDetails componentDetails)
        {
            return(CodeWriter.Populate(cgw =>
            {
                cgw.UsingDirectives(
                    "Improbable.Gdk.Core",
                    "Improbable.Worker.CInterop",
                    "System",
                    "System.Collections.Generic",
                    "System.Diagnostics",
                    "Unity.Entities"
                    );

                cgw.Namespace(componentDetails.Namespace, ns =>
                {
                    ns.Type($"public partial class {componentDetails.Name}", partial =>
                    {
                        partial.Line($"public const uint ComponentId = {componentDetails.ComponentId};");

                        partial.Type(GenerateComponentStruct(componentDetails));
                        partial.Type(GenerateComponentAuthorityStruct(componentDetails));
                        partial.Type(GenerateSnapshotStruct(componentDetails));
                        partial.Type(GenerateSerializationClass(componentDetails));
                        partial.Type(GenerateUpdateStruct(componentDetails));
                        partial.Type(GenerateInternalDynamicClass(componentDetails));
                    });
                });
            }));
        }
Example #7
0
        private static TypeBlock GenerateComponentAuthorityStruct(UnityComponentDetails componentDetails)
        {
            Logger.Trace(
                $"Generating {componentDetails.Namespace}.{componentDetails.Name}.ComponentAuthority struct.");

            return(Scope.Type("public struct HasAuthority : IComponentData", t => { }));
        }
Example #8
0
        private static TypeBlock GenerateInternalDynamicClass(UnityComponentDetails componentDetails, string qualifiedNamespace)
        {
            Logger.Trace($"Generating {qualifiedNamespace}.{componentDetails.ComponentName}.{componentDetails.ComponentName}Dynamic internal class.");

            return(Scope.Type($"internal class {componentDetails.ComponentName}Dynamic : IDynamicInvokable",
                              dynamic =>
            {
                dynamic.Line($"public uint ComponentId => {componentDetails.ComponentName}.ComponentId;");

                dynamic.Line(@"
internal static Dynamic.VTable<Update, Snapshot> VTable = new Dynamic.VTable<Update, Snapshot>
{
    DeserializeSnapshot = DeserializeSnapshot,
    SerializeSnapshot = SerializeSnapshot,
    DeserializeSnapshotRaw = Serialization.DeserializeSnapshot,
    SerializeSnapshotRaw = Serialization.SerializeSnapshot,
    ConvertSnapshotToUpdate = SnapshotToUpdate
};

private static Snapshot DeserializeSnapshot(ComponentData snapshot)
{
    var schemaDataOpt = snapshot.SchemaData;
    if (!schemaDataOpt.HasValue)
    {
        throw new ArgumentException($""Can not deserialize an empty {nameof(ComponentData)}"");
    }

    return Serialization.DeserializeSnapshot(schemaDataOpt.Value.GetFields());
}

private static void SerializeSnapshot(Snapshot snapshot, ComponentData data)
{
    var schemaDataOpt = data.SchemaData;
    if (!schemaDataOpt.HasValue)
    {
        throw new ArgumentException($""Can not serialise an empty {nameof(ComponentData)}"");
    }

    Serialization.SerializeSnapshot(snapshot, data.SchemaData.Value.GetFields());
}
");
                dynamic.Method("private static Update SnapshotToUpdate(in Snapshot snapshot)", m =>
                {
                    m.Initializer("var update = new Update", () =>
                    {
                        return componentDetails.FieldDetails.Select(fd =>
                                                                    $"{fd.PascalCaseName} = snapshot.{fd.PascalCaseName}");
                    });

                    m.Return("update");
                });

                dynamic.Line(@"
public void InvokeHandler(Dynamic.IHandler handler)
{
    handler.Accept<Update, Snapshot>(ComponentId, VTable);
}
");
            }));
        }
        public string Generate(UnityComponentDetails details, string package)
        {
            qualifiedNamespace = package;
            this.details       = details;

            return(TransformText());
        }
        public static string Generate(UnityComponentDetails details, string package)
        {
            var qualifiedNamespace = package;

            var componentDetails   = details;
            var commandDetailsList = details.CommandDetails;
            var rootNamespace      = $"global::{qualifiedNamespace}.{componentDetails.ComponentName}";

            var writer = CodeWriter.Populate(cgw =>
            {
                cgw.UsingDirectives(
                    "System",
                    "Improbable.Gdk.Core",
                    "Improbable.Gdk.Core.Commands"
                    );

                cgw.Namespace(qualifiedNamespace, ns =>
                {
                    ns.Type($"public partial class {componentDetails.ComponentName}", partial =>
                    {
                        Logger.Trace($"Generating {qualifiedNamespace}.{componentDetails.ComponentName}.ComponentMetaclass class.");

                        partial.Type("public class ComponentMetaclass : IComponentMetaclass", componentMetaclass =>
                        {
                            componentMetaclass.Line($@"
public uint ComponentId => {componentDetails.ComponentId};
public string Name => ""{componentDetails.ComponentName}"";

public Type Data {{ get; }} = typeof({rootNamespace}.Component);
public Type Snapshot {{ get; }} = typeof({rootNamespace}.Snapshot);
public Type Update {{ get; }} = typeof({rootNamespace}.Update);

public Type ReplicationHandler {{ get; }} = typeof({rootNamespace}.ComponentReplicator);
public Type Serializer {{ get; }} = typeof({rootNamespace}.ComponentSerializer);
public Type DiffDeserializer {{ get; }} = typeof({rootNamespace}.DiffComponentDeserializer);

public Type DiffStorage {{ get; }} = typeof({rootNamespace}.DiffComponentStorage);
public Type ViewStorage {{ get; }} = typeof({rootNamespace}.{componentDetails.ComponentName}ViewStorage);
public Type EcsViewManager {{ get; }} = typeof({rootNamespace}.EcsViewManager);
public Type DynamicInvokable {{ get; }} = typeof({rootNamespace}.{componentDetails.ComponentName}Dynamic);
");

                            componentMetaclass.Initializer("public ICommandMetaclass[] Commands { get; } = new ICommandMetaclass[]",
                                                           () =>
                            {
                                return(commandDetailsList.Select(command => $"new {command.CommandName}Metaclass()"));
                            });
                        });

                        foreach (var command in commandDetailsList)
                        {
                            partial.Type(GenerateCommandMetaclass(qualifiedNamespace, componentDetails.ComponentName, command));
                        }
                    });
                });
            });

            return(writer.Format());
        }
Example #11
0
        private static TypeBlock GenerateComponentSerializer(UnityComponentDetails componentDetails)
        {
            var eventDetailsList = componentDetails.EventDetails;

            return(Scope.Type("public class ComponentSerializer : IComponentSerializer", serializer =>
            {
                serializer.Line($@"
private ProfilerMarker serializeMarker = new ProfilerMarker(""{componentDetails.Name}.ComponentSerializer.Serialize"");

public uint GetComponentId()
{{
    return ComponentId;
}}
");

                serializer.Method("public void Serialize(MessagesToSend messages, SerializedMessagesToSend serializedMessages)",
                                  m =>
                {
                    m.ProfileScope("serializeMarker", s =>
                    {
                        s.Line(@"
var storage = messages.GetComponentDiffStorage(ComponentId);

var updates = ((IDiffUpdateStorage<Update>) storage).GetUpdates();

for (int i = 0; i < updates.Count; ++i)
{
    ref readonly var update = ref updates[i];
    var schemaUpdate = SchemaComponentUpdate.Create();
    var componentUpdate = new ComponentUpdate(ComponentId, schemaUpdate);
    Serialization.SerializeUpdate(update.Update, schemaUpdate);
    serializedMessages.AddComponentUpdate(componentUpdate, update.EntityId.Id);
}
");

                        foreach (var ev in eventDetailsList)
                        {
                            s.CustomScope(() => new[]
                            {
                                $@"
var events = ((IDiffEventStorage<{ev.PascalCaseName}.Event>) storage).GetEvents();

for (int i = 0; i < events.Count; ++i)
{{
    ref readonly var ev = ref events[i];
    var schemaUpdate = SchemaComponentUpdate.Create();
    var componentUpdate = new ComponentUpdate(ComponentId, schemaUpdate);
    var obj = schemaUpdate.GetEvents().AddObject({ev.EventIndex});
    {ev.FqnPayloadType}.Serialization.Serialize(ev.Event.Payload, obj);
    serializedMessages.AddComponentUpdate(componentUpdate, ev.EntityId.Id);
}}
"
                            });
                        }
                    });
                });
            }));
        }
Example #12
0
        private static TypeBlock GenerateComponentDeserializer(UnityComponentDetails componentDetails)
        {
            var componentNamespace = $"global::{componentDetails.Namespace}.{componentDetails.Name}";
            var eventDetailsList   = componentDetails.EventDetails;

            Logger.Trace($"Generating {componentDetails.Namespace}.{componentDetails.Name}.DiffComponentDeserializer class.");

            return(Scope.Type("public class DiffComponentDeserializer : IComponentDiffDeserializer", deserializer =>
            {
                deserializer.Line(@"
public uint GetComponentId()
{
    return ComponentId;
}
");

                deserializer.Method("public void AddUpdateToDiff(ComponentUpdateOp op, ViewDiff diff, uint updateId)",
                                    m =>
                {
                    m.If("op.Update.SchemaData.Value.GetFields().GetUniqueFieldIdCount() + op.Update.SchemaData.Value.GetClearedFieldCount() > 0",
                         () => new[]
                    {
                        $"var update = {componentNamespace}.Serialization.DeserializeUpdate(op.Update.SchemaData.Value);",
                        "diff.AddComponentUpdate(update, op.EntityId, op.Update.ComponentId, updateId);"
                    });

                    if (eventDetailsList.Count > 0)
                    {
                        m.Line("var eventsObject = op.Update.SchemaData.Value.GetEvents();");

                        foreach (var ev in eventDetailsList)
                        {
                            m.CustomScope(() => new[]
                            {
                                $@"
var eventCount = eventsObject.GetObjectCount({ev.EventIndex});
if (eventCount > 0)
{{
    for (uint i = 0; i < eventCount; i++)
    {{
        var payload = {ev.FqnPayloadType}.Serialization.Deserialize(eventsObject.IndexObject({ev.EventIndex}, i));
        var e = new {ev.PascalCaseName}.Event(payload);
        diff.AddEvent(e, op.EntityId, op.Update.ComponentId, updateId);
    }}
}}
"
                            });
                        }
                    }
                });

                deserializer.Method("public void AddComponentToDiff(AddComponentOp op, ViewDiff diff)", () => new[]
                {
                    "var data = Serialization.DeserializeUpdate(op.Data.SchemaData.Value);",
                    "diff.AddComponent(data, op.EntityId, op.Data.ComponentId);"
                });
            }));
        }
Example #13
0
        private void GenerateUpdateMethod(TypeBlock typeBlock, UnityComponentDetails details)
        {
            typeBlock.Method("public override void Update(EntityManager manager, Entity entity)", mb =>
            {
                mb.Line($"AuthoritativeToggle.value = manager.HasComponent<{details.Name}.HasAuthority>(entity);");
                mb.Line($"var component = manager.GetComponentData<{details.Name}.Component>(entity);");

                mb.TextList(details.FieldDetails.Select(fd => typeGenerator.ToUiFieldUpdate(fd, "component")));
            });
        }
        public static CodeWriter Generate(UnityComponentDetails componentDetails)
        {
            var commandDetailsList = componentDetails.CommandDetails;
            var rootNamespace      = $"global::{componentDetails.Namespace}.{componentDetails.Name}";

            return(CodeWriter.Populate(cgw =>
            {
                cgw.UsingDirectives(
                    "System",
                    "Improbable.Gdk.Core",
                    "Improbable.Gdk.Core.Commands"
                    );

                cgw.Namespace(componentDetails.Namespace, ns =>
                {
                    ns.Type($"public partial class {componentDetails.Name}", partial =>
                    {
                        Logger.Trace($"Generating {componentDetails.Namespace}.{componentDetails.Name}.ComponentMetaclass class.");

                        partial.Type("public class ComponentMetaclass : IComponentMetaclass", componentMetaclass =>
                        {
                            componentMetaclass.Line($@"
public uint ComponentId => {componentDetails.ComponentId};
public string Name => ""{componentDetails.Name}"";

public Type Data {{ get; }} = typeof({rootNamespace}.Component);
public Type Authority {{ get; }} = typeof({rootNamespace}.HasAuthority);
public Type Snapshot {{ get; }} = typeof({rootNamespace}.Snapshot);
public Type Update {{ get; }} = typeof({rootNamespace}.Update);

public Type ReplicationSystem {{ get; }} = typeof({rootNamespace}.ReplicationSystem);
public Type Serializer {{ get; }} = typeof({rootNamespace}.ComponentSerializer);
public Type DiffDeserializer {{ get; }} = typeof({rootNamespace}.DiffComponentDeserializer);

public Type DiffStorage {{ get; }} = typeof({rootNamespace}.DiffComponentStorage);
public Type EcsViewManager {{ get; }} = typeof({rootNamespace}.EcsViewManager);
public Type DynamicInvokable {{ get; }} = typeof({rootNamespace}.{componentDetails.Name}Dynamic);
");

                            componentMetaclass.Initializer("public ICommandMetaclass[] Commands { get; } = new ICommandMetaclass[]",
                                                           () =>
                            {
                                return commandDetailsList.Select(command => $"new {command.PascalCaseName}Metaclass()");
                            });
                        });

                        foreach (var command in commandDetailsList)
                        {
                            partial.Type(GenerateCommandMetaclass(componentDetails.Namespace, componentDetails.Name, command));
                        }
                    });
                });
            }));
        }
Example #15
0
        private static TypeBlock GenerateUpdateStruct(UnityComponentDetails componentDetails)
        {
            Logger.Trace($"Generating {componentDetails.Namespace}.{componentDetails.Name}.Update struct.");

            return(Scope.Type("public struct Update : ISpatialComponentUpdate",
                              update =>
            {
                update.Line(componentDetails.FieldDetails.Select(fd =>
                                                                 $"public Option<{fd.Type}> {fd.PascalCaseName};").ToList());
            }));
        }
        private static TypeBlock GenerateComponentWriter(UnityComponentDetails componentDetails, string qualifiedNamespace)
        {
            Logger.Trace($"Generating {qualifiedNamespace}.{componentDetails.ComponentName}Writer class.");

            return(Scope.Type($"public class {componentDetails.ComponentName}Writer : {componentDetails.ComponentName}Reader",
                              writer =>
            {
                writer.Line($@"
internal {componentDetails.ComponentName}Writer(World world, Entity entity, EntityId entityId)
    : base(world, entity, entityId)
{{
}}
");
                writer.Method($"public void SendUpdate({componentDetails.ComponentName}.Update update)", m =>
                {
                    m.Line($"var component = EntityManager.GetComponentData<{componentDetails.ComponentName}.Component>(Entity);");

                    foreach (var fieldDetails in componentDetails.FieldDetails)
                    {
                        m.Line($@"
if (update.{fieldDetails.PascalCaseName}.HasValue)
{{
    component.{fieldDetails.PascalCaseName} = update.{fieldDetails.PascalCaseName}.Value;
}}
");
                    }

                    m.Line($"EntityManager.SetComponentData(Entity, component);");
                });

                foreach (var eventDetails in componentDetails.EventDetails)
                {
                    var eventType = $"{componentDetails.ComponentName}.{eventDetails.EventName}.Event";
                    writer.Line($@"
public void Send{eventDetails.EventName}Event({eventDetails.FqnPayloadType} {eventDetails.CamelCaseEventName})
{{
    var eventToSend = new {eventType}({eventDetails.CamelCaseEventName});
    ComponentUpdateSystem.SendEvent(eventToSend, EntityId);
}}
");
                }

                writer.Line($@"
public void AcknowledgeAuthorityLoss()
{{
    ComponentUpdateSystem.AcknowledgeAuthorityLoss(EntityId, {componentDetails.ComponentName}.ComponentId);
}}
");
            }));
        }
Example #17
0
        private static TypeBlock GenerateComponentAuthorityStruct(UnityComponentDetails componentDetails,
                                                                  string qualifiedNamespace)
        {
            Logger.Trace(
                $"Generating {qualifiedNamespace}.{componentDetails.ComponentName}.ComponentAuthority struct.");

            return(Scope.Type("public struct ComponentAuthority : ISharedComponentData, IEquatable<ComponentAuthority>",
                              t =>
            {
                t.Line(@"
public bool HasAuthority;

public ComponentAuthority(bool hasAuthority)
{
    HasAuthority = hasAuthority;
}

// todo think about whether any of this is necessary
// Unity does a bitwise equality check so this is just for users reading the struct
public static readonly ComponentAuthority NotAuthoritative = new ComponentAuthority(false);
public static readonly ComponentAuthority Authoritative = new ComponentAuthority(true);

public bool Equals(ComponentAuthority other)
{
    return this == other;
}

public override bool Equals(object obj)
{
    return obj is ComponentAuthority auth && this == auth;
}

public override int GetHashCode()
{
    return HasAuthority.GetHashCode();
}

public static bool operator ==(ComponentAuthority a, ComponentAuthority b)
{
    return a.HasAuthority == b.HasAuthority;
}

public static bool operator !=(ComponentAuthority a, ComponentAuthority b)
{
    return !(a == b);
}
");
            }));
        }
Example #18
0
        private void GenerateConstructor(TypeBlock typeBlock, UnityComponentDetails details)
        {
            typeBlock.Method($"public {details.Name}Renderer() : base()", mb =>
            {
                mb.Line($"ComponentFoldout.text = \"{details.Name}\";");
                mb.Line($"AuthoritativeToggle.SetEnabled(false);");

                foreach (var field in details.FieldDetails)
                {
                    mb.TextList(typeGenerator.ToFieldInitialisation(field, "ComponentFoldout"));
                }

                mb.Line($"InjectComponentIcon(\"{GetComponentIcon(details)}\");");
            });
        }
        public static string Generate(UnityComponentDetails details, string package)
        {
            var qualifiedNamespace = package;
            var componentName      = details.ComponentName;

            return(CodeWriter.Populate(cgw =>
            {
                cgw.UsingDirectives(
                    "System.Collections.Generic",
                    "Improbable.Gdk.Core",
                    "Improbable.Worker",
                    "Unity.Entities"
                    );

                cgw.Namespace(qualifiedNamespace, ns =>
                {
                    ns.Type($"public partial class {componentName}", partial =>
                    {
                        foreach (var eventDetail in details.EventDetails)
                        {
                            var eventName = eventDetail.EventName;
                            var payloadType = eventDetail.FqnPayloadType;

                            Logger.Trace($"Generating {qualifiedNamespace}.{componentName}.{eventName} class.");
                            partial.Line($@"
public static class {eventName}
{{
    public readonly struct Event : IEvent
    {{
        public readonly {payloadType} Payload;

        public Event({payloadType} payload)
        {{
            Payload = payload;
        }}
    }}
}}
");
                        }
                    });
                });
            }).Format());
        }
Example #20
0
        public static CodeWriter Generate(UnityComponentDetails componentDetails)
        {
            return(CodeWriter.Populate(cgw =>
            {
                cgw.UsingDirectives(
                    "Improbable.Gdk.Core",
                    "Improbable.Worker.CInterop"
                    );

                cgw.Namespace(componentDetails.Namespace, ns =>
                {
                    ns.Type($"public partial class {componentDetails.Name}", partial =>
                    {
                        partial.Type(GenerateComponentDeserializer(componentDetails));
                        partial.Type(GenerateComponentSerializer(componentDetails));
                    });
                });
            }));
        }
        private static TypeBlock GenerateComponentWriterSubscriptionManager(UnityComponentDetails componentDetails)
        {
            Logger.Trace($"Generating {componentDetails.Namespace}.{componentDetails.Name}WriterSubscriptionManager class.");
            var componentWriterType = $"{componentDetails.Name}Writer";

            return(Scope.AnnotatedType("AutoRegisterSubscriptionManager",
                                       $"public class {componentDetails.Name}WriterSubscriptionManager : WriterSubscriptionManager<{componentDetails.Name}.Component, {componentWriterType}>",
                                       wsm =>
            {
                wsm.Line($@"
public {componentDetails.Name}WriterSubscriptionManager(World world) : base(world)
{{
}}

protected override {componentWriterType} CreateWriter(Entity entity, EntityId entityId)
{{
    return new {componentWriterType}(World, entity, entityId);
}}
");
            }));
        }
Example #22
0
        public static string Generate(UnityComponentDetails componentDetails, string qualifiedNamespace)
        {
            return(CodeWriter.Populate(cgw =>
            {
                cgw.UsingDirectives(
                    "Improbable.Gdk.Core",
                    "Improbable.Gdk.Core.Commands"
                    );

                cgw.Namespace(qualifiedNamespace, ns =>
                {
                    ns.Type($"public partial class {componentDetails.ComponentName}", partial =>
                    {
                        foreach (var command in componentDetails.CommandDetails)
                        {
                            partial.Text(GenerateCommandStorage(command, qualifiedNamespace, componentDetails.ComponentName));
                            partial.Text(GenerateCommandsToSendStorage(command, qualifiedNamespace, componentDetails.ComponentName));
                        }
                    });
                });
            }).Format());
        }
        private static TypeBlock GenerateCommandReceiverSubscriptionManager(UnityComponentDetails componentDetails, string qualifiedNamespace)
        {
            Logger.Trace($"Generating {qualifiedNamespace}.{componentDetails.ComponentName}CommandReceiverSubscriptionManager class.");

            var commandReceiverType = $"{componentDetails.ComponentName}CommandReceiver";

            return(Scope.AnnotatedType("AutoRegisterSubscriptionManager",
                                       $"public class {componentDetails.ComponentName}CommandReceiverSubscriptionManager : CommandReceiverSubscriptionManagerBase<{commandReceiverType}>",
                                       t =>
            {
                t.Line($@"
public {componentDetails.ComponentName}CommandReceiverSubscriptionManager(World world) : base(world, {componentDetails.ComponentName}.ComponentId)
{{
}}

protected override {commandReceiverType} CreateReceiver(World world, Entity entity, EntityId entityId)
{{
    return new {commandReceiverType}(world, entity, entityId);
}}
");
            }));
        }
Example #24
0
        private static TypeBlock GenerateSnapshotStruct(UnityComponentDetails componentDetails, string qualifiedNamespace)
        {
            Logger.Trace($"Generating {qualifiedNamespace}.{componentDetails.ComponentName}.Snapshot struct.");

            var fieldDetailsList = componentDetails.FieldDetails;

            return(Scope.AnnotatedType("global::System.Serializable",
                                       "public struct Snapshot : ISpatialComponentSnapshot", snapshot =>
            {
                snapshot.Line($"public uint ComponentId => {componentDetails.ComponentId};");

                snapshot.Line(fieldDetailsList.Select(fd => $"public {fd.Type} {fd.PascalCaseName};").ToList());

                if (fieldDetailsList.Count > 0)
                {
                    snapshot.Method($"public Snapshot({GetConstructorArgs(fieldDetailsList)})", () =>
                    {
                        return fieldDetailsList.Select(fd => $"{fd.PascalCaseName} = {fd.CamelCaseName};");
                    });
                }
            }));
        }
        public static CodeWriter Generate(UnityComponentDetails details)
        {
            return(CodeWriter.Populate(cgw =>
            {
                cgw.UsingDirectives(
                    "System",
                    "System.Collections.Generic",
                    "Improbable.Gdk.Core",
                    "Improbable.Gdk.Subscriptions",
                    "Unity.Entities",
                    "Entity = Unity.Entities.Entity"
                    );

                cgw.Namespace(details.Namespace, ns =>
                {
                    ns.Type(GenerateComponentReaderSubscriptionManager(details));
                    ns.Type(GenerateComponentWriterSubscriptionManager(details));
                    ns.Type(GenerateComponentReader(details));
                    ns.Type(GenerateComponentWriter(details));
                });
            }));
        }
        private static TypeBlock GenerateCommandSenderSubscriptionManager(UnityComponentDetails componentDetails)
        {
            Logger.Trace($"Generating {componentDetails.Namespace}.{componentDetails.Name}CommandSenderSubscriptionManager class.");

            var commandSenderType = $"{componentDetails.Name}CommandSender";

            return(Scope.AnnotatedType("AutoRegisterSubscriptionManager",
                                       $"public class {componentDetails.Name}CommandSenderSubscriptionManager : CommandSenderSubscriptionManagerBase<{commandSenderType}>",
                                       t =>
            {
                t.Line($@"
public {componentDetails.Name}CommandSenderSubscriptionManager(World world) : base(world)
{{
}}

protected override {commandSenderType} CreateSender(Entity entity, World world)
{{
    return new {commandSenderType}(entity, world);
}}
");
            }));
        }
Example #27
0
        public static CodeWriter Generate(UnityComponentDetails componentDetails)
        {
            return(CodeWriter.Populate(cgw =>
            {
                cgw.UsingDirectives(
                    "Improbable.Gdk.Core",
                    "Improbable.Gdk.Core.Commands",
                    "Improbable.Worker.CInterop"
                    );

                cgw.Namespace(componentDetails.Namespace, ns =>
                {
                    ns.Type($"public partial class {componentDetails.Name}", partial =>
                    {
                        foreach (var command in componentDetails.CommandDetails)
                        {
                            partial.Text(GenerateDiffCommandDeserializer(command, componentDetails.Namespace, componentDetails.Name));
                            partial.Text(GenerateCommandSerializer(command, componentDetails.Namespace, componentDetails.Name));
                        }
                    });
                });
            }));
        }
        public static string Generate(UnityComponentDetails details, string package)
        {
            return(CodeWriter.Populate(cgw =>
            {
                cgw.UsingDirectives(
                    "System",
                    "System.Collections.Generic",
                    "Improbable.Worker.CInterop",
                    "Improbable.Gdk.Core",
                    "Improbable.Gdk.Subscriptions",
                    "Unity.Entities",
                    "Entity = Unity.Entities.Entity"
                    );

                cgw.Namespace(package, ns =>
                {
                    ns.Type(GenerateComponentReaderSubscriptionManager(details, package));
                    ns.Type(GenerateComponentWriterSubscriptionManager(details, package));
                    ns.Type(GenerateComponentReader(details, package));
                    ns.Type(GenerateComponentWriter(details, package));
                });
            }).Format());
        }
Example #29
0
        public static string Generate(UnityComponentDetails componentDetails, string package)
        {
            var qualifiedNamespace = package;

            return(CodeWriter.Populate(cgw =>
            {
                cgw.UsingDirectives(
                    "Improbable.Gdk.Core",
                    "Improbable.Worker.CInterop"
                    );

                cgw.Namespace(qualifiedNamespace, ns =>
                {
                    ns.Type($"public partial class {componentDetails.ComponentName}", partial =>
                    {
                        foreach (var command in componentDetails.CommandDetails)
                        {
                            partial.Text(GenerateDiffCommandDeserializer(command, qualifiedNamespace, componentDetails.ComponentName));
                            partial.Text(GenerateCommandSerializer(command, qualifiedNamespace, componentDetails.ComponentName));
                        }
                    });
                });
            }).Format());
        }
        public static CodeWriter Generate(UnityComponentDetails details)
        {
            var fullyQualifiedNamespace = $"global::{details.Namespace}.{details.Name}";

            return(CodeWriter.Populate(cgw =>
            {
                cgw.UsingDirectives(
                    "System",
                    "System.Collections.Generic",
                    "Unity.Entities",
                    "Improbable.Gdk.Core",
                    "Improbable.Gdk.Subscriptions",
                    "Entity = Unity.Entities.Entity"
                    );

                cgw.Namespace(details.Namespace, ns =>
                {
                    ns.Type(GenerateCommandSenderSubscriptionManager(details));
                    ns.Type(GenerateCommandReceiverSubscriptionManager(details));
                    ns.Type(GenerateCommandSender(details, fullyQualifiedNamespace));
                    ns.Type(GenerateCommandReceiver(details, fullyQualifiedNamespace));
                });
            }));
        }