Ejemplo n.º 1
0
        private CodeSnippetCompileUnit GenerateValidateMethods()
        {
            var sourceCode = new StringBuilder();

            sourceCode.AppendLine("using UnityEditor;");
            sourceCode.AppendLine("using Yamly.UnityEditor;");
            sourceCode.AppendLine($"namespace {TargetNamespace}");
            sourceCode.AppendLine("{");
            sourceCode.AppendLine("    public static class ValidateYamlyGeneratedUtility");
            sourceCode.AppendLine("{");
            foreach (var group in _groups.Where(CodeGenerationUtility.IsValidGroupName))
            {
                var groupName = CodeGenerationUtility.GetGroupName(group);
                sourceCode.AppendLine($"[MenuItem(\"Yamly/Validate/{group}\")]");
                sourceCode.AppendLine($"public static void Validate{groupName}()");
                sourceCode.AppendLine("{");
                sourceCode.AppendLine($"YamlyAssetPostprocessor.Validate(\"{group}\");");
                sourceCode.AppendLine("}");
            }

            sourceCode.AppendLine("}");
            sourceCode.AppendLine("}");

            return(new CodeSnippetCompileUnit(sourceCode.ToString()));
        }
Ejemplo n.º 2
0
        protected string GetStorageTypeName(Type rootType, AssetDeclarationAttributeBase attribute)
        {
            var groupName = CodeGenerationUtility.GetGroupName(attribute.Group);
            var typeName  = GetShortTypeName(GetTypeName(rootType, false));

            return($"{groupName}{typeName}Storage");
        }
Ejemplo n.º 3
0
        private CodeCompileUnit GenerateEnum()
        {
            var sourceCode = new StringBuilder();

            var groups = _proxyCodeGenerator.RootDefinitions.SelectMany(r => r.Attributes)
                         .Select(a => a.Group)
                         .Distinct()
                         .Where(CodeGenerationUtility.IsValidGroupName)
                         .ToArray();

            sourceCode.AppendLine($"namespace {TargetNamespace}");
            sourceCode.AppendLine("{");
            sourceCode.AppendLine("public enum Assets");
            sourceCode.AppendLine("{");
            for (var i = 0; i < groups.Length; i++)
            {
                sourceCode.Append(CodeGenerationUtility.GetGroupName(groups[i])).AppendLine(",");
            }

            sourceCode.AppendLine("}");

            sourceCode.AppendLine("public static class AssetsExtensions");
            sourceCode.AppendLine("{");
            sourceCode.AppendLine("public static string ToGroupName(this Assets assets)");
            sourceCode.AppendLine("{");
            sourceCode.AppendLine("switch(assets)");
            sourceCode.AppendLine("{");
            foreach (var group in groups)
            {
                var groupName = CodeGenerationUtility.GetGroupName(group);
                sourceCode.AppendLine($"case Assets.{groupName}:");
                sourceCode.AppendLine($"return \"{group}\";");
            }

            sourceCode.AppendLine("default: throw new System.NotSupportedException(assets.ToString());");
            sourceCode.AppendLine("}");
            sourceCode.AppendLine("}");
            sourceCode.AppendLine("}");

            sourceCode.AppendLine("}");

            return(new CodeSnippetCompileUnit(sourceCode.ToString()));
        }
Ejemplo n.º 4
0
        private CodeCompileUnit GenerateEnum()
        {
            var sourceCode = new StringBuilder();

            var groups = Context.Groups;

            sourceCode.AppendLine($"namespace {TargetNamespace}");
            sourceCode.AppendLine("{");
            sourceCode.AppendLine("public enum Assets");
            sourceCode.AppendLine("{");
            for (var i = 0; i < groups.Count; i++)
            {
                sourceCode.Append(CodeGenerationUtility.GetGroupName(groups[i])).AppendLine(",");
            }

            sourceCode.AppendLine("}");

            sourceCode.AppendLine("public static class AssetsExtensions");
            sourceCode.AppendLine("{");
            sourceCode.AppendLine("public static string ToGroupName(this Assets assets)");
            sourceCode.AppendLine("{");
            sourceCode.AppendLine("switch(assets)");
            sourceCode.AppendLine("{");
            foreach (var group in groups)
            {
                var groupName = CodeGenerationUtility.GetGroupName(group);
                sourceCode.AppendLine($"case Assets.{groupName}:");
                sourceCode.AppendLine($"return \"{group}\";");
            }

            sourceCode.AppendLine("default: throw new System.NotSupportedException(assets.ToString());");
            sourceCode.AppendLine("}");
            sourceCode.AppendLine("}");
            sourceCode.AppendLine("}");

            sourceCode.AppendLine("}");

            return(new CodeSnippetCompileUnit(sourceCode.ToString()));
        }
Ejemplo n.º 5
0
        private CodeCompileUnit GenerateStorageExtensionMethods()
        {
            var sourceCode = new StringBuilder();

            var groups = _proxyCodeGenerator.RootDefinitions.SelectMany(r => r.Attributes)
                         .Select(a => a.Group)
                         .Distinct()
                         .Where(CodeGenerationUtility.IsValidGroupName)
                         .ToArray();

            var g = new Dictionary <string, Type>();

            foreach (var definition in _proxyCodeGenerator.RootDefinitions)
            {
                foreach (var attribute in definition.Attributes)
                {
                    switch (attribute.GetDeclarationType())
                    {
                    case DeclarationType.Single:
                        g[attribute.Group] = definition.Root;
                        break;

                    case DeclarationType.List:
                        var elementType = definition.Root;
                        g[attribute.Group] = typeof(List <>).MakeGenericType(elementType);
                        break;

                    case DeclarationType.Dictionary:
                        var dictionaryAttribute = (AssetDictionaryAttribute)attribute;
                        if (dictionaryAttribute.IsSingleFile)
                        {
                            g[attribute.Group] = dictionaryAttribute.KeyType;
                        }

                        var keySourceMethodInfo = definition.Root.GetKeySourceMethodInfo(dictionaryAttribute);
                        if (keySourceMethodInfo == null)
                        {
                            continue;
                        }

                        var keyType   = keySourceMethodInfo.ReturnType;
                        var valueType = definition.Root;
                        g[attribute.Group] = typeof(Dictionary <,>).MakeGenericType(keyType, valueType);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }
            }

            sourceCode.AppendLine($"namespace {TargetNamespace}");
            sourceCode.AppendLine("{");
            sourceCode.AppendLine("using System.Collections.Generic;");
            sourceCode.AppendLine("public static class StorageDefinitionExtensions");
            sourceCode.AppendLine("{");

            sourceCode.AppendLine($"public static T Get<T>(this {nameof(Storage)} s, {TargetNamespace}.Assets asset)");
            sourceCode.AppendLine("{");
            sourceCode.AppendLine("return s.Get<T>(asset.ToGroupName());");
            sourceCode.AppendLine("}");

            sourceCode.AppendLine($"public static bool Contains(this {nameof(Storage)} s, {TargetNamespace}.Assets asset)");
            sourceCode.AppendLine("{");
            sourceCode.AppendLine("return s.Contains(asset.ToGroupName());");
            sourceCode.AppendLine("}");

            sourceCode.AppendLine($"public static bool Contains<T>(this {nameof(Storage)} s, {TargetNamespace}.Assets asset)");
            sourceCode.AppendLine("{");
            sourceCode.AppendLine("return s.Contains<T>(asset.ToGroupName());");
            sourceCode.AppendLine("}");

            for (var i = 0; i < groups.Length; i++)
            {
                var group     = groups[i];
                var groupName = CodeGenerationUtility.GetGroupName(group);
                var typeName  = _proxyCodeGenerator.GetTypeName(g[group]);
                sourceCode.AppendLine($"public static {typeName} Get{groupName}(this Storage s)");
                sourceCode.AppendLine("{");
                sourceCode.AppendLine($"return s.Get<{typeName}>(\"{group}\");");
                sourceCode.AppendLine("}");
            }

            sourceCode.AppendLine("}");
            sourceCode.AppendLine("}");

            return(new CodeSnippetCompileUnit(sourceCode.ToString()));
        }