Esempio n. 1
0
 public static string Generate(UnityEnumDetails details, string package)
 {
     return(CodeWriter.Populate(cgw =>
     {
         cgw.Namespace(package, ns =>
         {
             ns.Line(UnityEnumContent.Generate(details, package).Format());
         });
     }).Format());
 }
        public static CodeWriter Generate(UnityEnumDetails details)
        {
            Logger.Trace($"Generating code for {details.QualifiedName}.");

            return(CodeWriter.Populate(cgw =>
            {
                cgw.Namespace(details.Namespace, ns =>
                {
                    ns.Enum(UnityEnumContent.Generate(details, details.Namespace));
                });
            }));
        }
Esempio n. 3
0
        public static TypeBlock Generate(UnityTypeDetails details, string typeNamespace)
        {
            var nestedTypes = details.ChildTypes;
            var nestedEnums = details.ChildEnums;

            var typeDetails      = details;
            var fieldDetailsList = details.FieldDetails;
            var hasPartial       = PartialDatabase.TryGetPartial(typeDetails.GetPartialResourceTypeName(), out var partial);

            Logger.Trace($"Generating {typeNamespace}.{typeDetails.CapitalisedName} struct.");

            return(Scope.AnnotatedType("global::System.Serializable",
                                       $"public struct {typeDetails.CapitalisedName}", s =>
            {
                if (fieldDetailsList.Count > 0)
                {
                    s.Line(fieldDetailsList
                           .Select(fd => $"public {fd.Type} {fd.PascalCaseName};")
                           .ToList());

                    var constructorArgs = GetConstructorArgs(fieldDetailsList);

                    s.Method($"public {typeDetails.CapitalisedName}({constructorArgs})", m =>
                    {
                        m.Line(sb =>
                        {
                            foreach (var fd in fieldDetailsList)
                            {
                                sb.AppendLine($"{fd.PascalCaseName} = {fd.CamelCaseName};");
                            }
                        });
                    });
                }

                if (hasPartial)
                {
                    s.Line(partial);
                }

                s.Type(GenerateSerializationClass(typeDetails));

                foreach (var nestedType in nestedTypes)
                {
                    s.Type(Generate(nestedType, $"{typeNamespace}.{typeDetails.CapitalisedName}"));
                }

                foreach (var nestedEnum in nestedEnums)
                {
                    s.Enum(UnityEnumContent.Generate(nestedEnum, $"{typeNamespace}.{typeDetails.CapitalisedName}"));
                }
            }));
        }
        public virtual string TransformText()
        {
            this.GenerationEnvironment = null;

            #line 3 "Templates\UnityTypeContent.tt"

            var typeDetails      = GetTypeDetails();
            var fieldDetailsList = GetFieldDetailsList();


            #line default
            #line hidden

            #line 7 "Templates\UnityTypeContent.tt"
            this.Write("\r\npublic struct ");

            #line default
            #line hidden

            #line 8 "Templates\UnityTypeContent.tt"
            this.Write(this.ToStringHelper.ToStringWithCulture(typeDetails.CapitalisedName));

            #line default
            #line hidden

            #line 8 "Templates\UnityTypeContent.tt"
            this.Write("\r\n{\r\n");

            #line default
            #line hidden

            #line 10 "Templates\UnityTypeContent.tt"
            foreach (var fieldDetails in fieldDetailsList)
            {
            #line default
            #line hidden

            #line 11 "Templates\UnityTypeContent.tt"
                this.Write("    public ");

            #line default
            #line hidden

            #line 11 "Templates\UnityTypeContent.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(fieldDetails.Type));

            #line default
            #line hidden

            #line 11 "Templates\UnityTypeContent.tt"
                this.Write(" ");

            #line default
            #line hidden

            #line 11 "Templates\UnityTypeContent.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(fieldDetails.PascalCaseName));

            #line default
            #line hidden

            #line 11 "Templates\UnityTypeContent.tt"
                this.Write(";\r\n");

            #line default
            #line hidden

            #line 12 "Templates\UnityTypeContent.tt"
            }

            #line default
            #line hidden

            #line 13 "Templates\UnityTypeContent.tt"

            if (fieldDetailsList.Count > 0)
            {
                var constructorArgs = GetConstructorArgs();


            #line default
            #line hidden

            #line 18 "Templates\UnityTypeContent.tt"
                this.Write("\r\n    public ");

            #line default
            #line hidden

            #line 19 "Templates\UnityTypeContent.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(typeDetails.CapitalisedName));

            #line default
            #line hidden

            #line 19 "Templates\UnityTypeContent.tt"
                this.Write("(");

            #line default
            #line hidden

            #line 19 "Templates\UnityTypeContent.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(constructorArgs));

            #line default
            #line hidden

            #line 19 "Templates\UnityTypeContent.tt"
                this.Write(")\r\n    {\r\n");

            #line default
            #line hidden

            #line 21 "Templates\UnityTypeContent.tt"
                foreach (var fieldDetails in fieldDetailsList)
                {
            #line default
            #line hidden

            #line 22 "Templates\UnityTypeContent.tt"
                    this.Write("        ");

            #line default
            #line hidden

            #line 22 "Templates\UnityTypeContent.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(fieldDetails.PascalCaseName));

            #line default
            #line hidden

            #line 22 "Templates\UnityTypeContent.tt"
                    this.Write(" = ");

            #line default
            #line hidden

            #line 22 "Templates\UnityTypeContent.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(fieldDetails.CamelCaseName));

            #line default
            #line hidden

            #line 22 "Templates\UnityTypeContent.tt"
                    this.Write(";\r\n");

            #line default
            #line hidden

            #line 23 "Templates\UnityTypeContent.tt"
                }

            #line default
            #line hidden

            #line 24 "Templates\UnityTypeContent.tt"
                this.Write("    }\r\n");

            #line default
            #line hidden

            #line 25 "Templates\UnityTypeContent.tt"
            }

            #line default
            #line hidden

            #line 26 "Templates\UnityTypeContent.tt"
            this.Write("\r\n    public static class Serialization\r\n    {\r\n        public static void Serial" +
                       "ize(");

            #line default
            #line hidden

            #line 29 "Templates\UnityTypeContent.tt"
            this.Write(this.ToStringHelper.ToStringWithCulture(typeDetails.CapitalisedName));

            #line default
            #line hidden

            #line 29 "Templates\UnityTypeContent.tt"
            this.Write(" instance, global::Improbable.Worker.Core.SchemaObject obj)\r\n        {\r\n");

            #line default
            #line hidden

            #line 31 "Templates\UnityTypeContent.tt"
            foreach (var fieldDetails in fieldDetailsList)
            {
            #line default
            #line hidden

            #line 32 "Templates\UnityTypeContent.tt"
                this.Write("            {\r\n                ");

            #line default
            #line hidden

            #line 33 "Templates\UnityTypeContent.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(fieldDetails.GetSerializationString("instance." + fieldDetails.PascalCaseName, "obj", 4)));

            #line default
            #line hidden

            #line 33 "Templates\UnityTypeContent.tt"
                this.Write("\r\n            }\r\n");

            #line default
            #line hidden

            #line 35 "Templates\UnityTypeContent.tt"
            }

            #line default
            #line hidden

            #line 36 "Templates\UnityTypeContent.tt"
            this.Write("        }\r\n\r\n        public static ");

            #line default
            #line hidden

            #line 38 "Templates\UnityTypeContent.tt"
            this.Write(this.ToStringHelper.ToStringWithCulture(typeDetails.CapitalisedName));

            #line default
            #line hidden

            #line 38 "Templates\UnityTypeContent.tt"
            this.Write(" Deserialize(global::Improbable.Worker.Core.SchemaObject obj)\r\n        {\r\n       " +
                       "     var instance = new ");

            #line default
            #line hidden

            #line 40 "Templates\UnityTypeContent.tt"
            this.Write(this.ToStringHelper.ToStringWithCulture(typeDetails.CapitalisedName));

            #line default
            #line hidden

            #line 40 "Templates\UnityTypeContent.tt"
            this.Write("();\r\n");

            #line default
            #line hidden

            #line 41 "Templates\UnityTypeContent.tt"
            foreach (var fieldDetails in fieldDetailsList)
            {
            #line default
            #line hidden

            #line 42 "Templates\UnityTypeContent.tt"
                this.Write("            {\r\n                ");

            #line default
            #line hidden

            #line 43 "Templates\UnityTypeContent.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(fieldDetails.GetDeserializeString("instance." + fieldDetails.PascalCaseName, "obj", 4)));

            #line default
            #line hidden

            #line 43 "Templates\UnityTypeContent.tt"
                this.Write("\r\n            }\r\n");

            #line default
            #line hidden

            #line 45 "Templates\UnityTypeContent.tt"
            }

            #line default
            #line hidden

            #line 46 "Templates\UnityTypeContent.tt"
            this.Write("            return instance;\r\n        }\r\n    }\r\n");

            #line default
            #line hidden

            #line 49 "Templates\UnityTypeContent.tt"

            var typeGenerator = new UnityTypeContent();
            foreach (var nestedType in nestedTypes)
            {
            #line default
            #line hidden

            #line 54 "Templates\UnityTypeContent.tt"
                this.Write("\r\n    ");

            #line default
            #line hidden

            #line 55 "Templates\UnityTypeContent.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(CommonGeneratorUtils.IndentEveryNewline(typeGenerator.Generate(nestedType, enumSet))));

            #line default
            #line hidden

            #line 55 "Templates\UnityTypeContent.tt"
                this.Write("\r\n");

            #line default
            #line hidden

            #line 56 "Templates\UnityTypeContent.tt"
            }

            #line default
            #line hidden

            #line 57 "Templates\UnityTypeContent.tt"

            var enumGenerator = new UnityEnumContent();
            foreach (var nestedEnum in nestedEnums)
            {
            #line default
            #line hidden

            #line 62 "Templates\UnityTypeContent.tt"
                this.Write("\r\n    ");

            #line default
            #line hidden

            #line 63 "Templates\UnityTypeContent.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(CommonGeneratorUtils.IndentEveryNewline(enumGenerator.Generate(nestedEnum))));

            #line default
            #line hidden

            #line 63 "Templates\UnityTypeContent.tt"
                this.Write("\r\n");

            #line default
            #line hidden

            #line 64 "Templates\UnityTypeContent.tt"
            }

            #line default
            #line hidden

            #line 65 "Templates\UnityTypeContent.tt"
            this.Write("}\r\n");

            #line default
            #line hidden
            return(this.GenerationEnvironment.ToString());
        }