Exemple #1
0
                private void UpdateTemplateMeta()
                {
                    uiFeatures      = null;
                    newSystem       = false;
                    templateInfo    = null;
                    templateWarning = null;
                    templateType    = null;
                    valid           = false;
                    sg2             = false;

                    UIFeature.ClearFoldoutStack();

                    if (textAsset != null && !string.IsNullOrEmpty(textAsset.text))
                    {
                        using (var reader = new StringReader(textAsset.text))
                        {
                            string line;
                            while ((line = reader.ReadLine()) != null)
                            {
                                if (line.StartsWith("#INFO="))
                                {
                                    templateInfo = line.Substring(6).TrimEnd().Replace("  ", "\n");
                                }

                                else if (line.StartsWith("#WARNING="))
                                {
                                    templateWarning = line.Substring(9).TrimEnd().Replace("  ", "\n");
                                }

                                else if (line.StartsWith("#CONFIG="))
                                {
                                    templateType = line.Substring(8).TrimEnd().ToLower();
                                }

                                else if (line.StartsWith("#FEATURES"))
                                {
                                    newSystem  = true;
                                    uiFeatures = UIFeature.GetUIFeatures(reader);
                                }

                                else if (line.StartsWith("#SG2"))
                                {
                                    sg2 = true;
                                    return;
                                }

                                //Config meta should appear before the Shader name line
                                else if (line.StartsWith("Shader"))
                                {
                                    valid = uiFeatures != null;
                                    return;
                                }
                            }
                        }
                    }
                }
Exemple #2
0
 private void OnEnable()
 {
     if (!motherUI)
     {
         motherUI = FindObjectOfType <UIFeature>();
         if (!motherUI)
         {
             Debug.LogError("the " + GetType().Name + " apply on " + name + " is missing motherUI (UIFeature) and thus it can't work.");
             workable = false;
         }
     }
 }
Exemple #3
0
        //Parses a #FEATURES text block
        public static UIFeature[] GetUIFeatures(StringReader reader)
        {
            var    uiFeaturesList = new List <UIFeature>();
            string subline;
            var    overflow = 0;

            while ((subline = reader.ReadLine()) != "#END")
            {
                //Just in case template file is badly written
                overflow++;
                if (overflow > 99999)
                {
                    break;
                }

                //Empty line
                if (string.IsNullOrEmpty(subline))
                {
                    continue;
                }

                var data = subline.Split(new[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);

                //Skip empty or comment # lines
                if (data == null || data.Length == 0 || (data.Length > 0 && data[0].StartsWith("#")))
                {
                    continue;
                }

                var kvpList = new List <KeyValuePair <string, string> >();
                for (var i = 1; i < data.Length; i++)
                {
                    var sdata = data[i].Split('=');
                    if (sdata.Length == 2)
                    {
                        kvpList.Add(new KeyValuePair <string, string>(sdata[0], sdata[1]));
                    }
                    else if (sdata.Length == 1)
                    {
                        kvpList.Add(new KeyValuePair <string, string>(sdata[0], null));
                    }
                    else
                    {
                        Debug.LogError("Couldn't parse UI property from Template:\n" + data[i]);
                    }
                }

                UIFeature feature = null;
                switch (data[0])
                {
                case "---": feature = new UIFeature_Separator(); break;

                case "space": feature = new UIFeature_Space(kvpList); break;

                case "flag": feature = new UIFeature_Flag(kvpList); break;

                case "float": feature = new UIFeature_Float(kvpList); break;

                case "subh": feature = new UIFeature_SubHeader(kvpList); break;

                case "header": feature = new UIFeature_Header(kvpList); break;

                case "warning": feature = new UIFeature_Warning(kvpList); break;

                case "sngl": feature = new UIFeature_Single(kvpList); break;

                case "mult": feature = new UIFeature_Multiple(kvpList); break;

                case "keyword": feature = new UIFeature_Keyword(kvpList); break;

                case "mask": feature = new UIFeature_Mask(kvpList); break;

                case "shader_target": feature = new UIFeature_ShaderTarget(); break;

                case "dd_start": feature = new UIFeature_DropDownStart(kvpList); break;

                case "dd_end": feature = new UIFeature_DropDownEnd(); break;
                //case "texture_list": feature = new UIFeature_TextureList(); break;
                //case "tex": feature = new UIFeature_Texture(kvpList); break;

                default: feature = new UIFeature(kvpList); break;
                }

                uiFeaturesList.Add(feature);
            }
            return(uiFeaturesList.ToArray());
        }
Exemple #4
0
            private void UpdateTemplateMeta()
            {
                uiFeatures       = null;
                templateInfo     = null;
                templateWarning  = null;
                templateType     = null;
                templateKeywords = null;
                id = null;

                UIFeature.ClearFoldoutStack();

                if (textAsset != null && !string.IsNullOrEmpty(textAsset.text))
                {
                    //First pass: parse #MODULES and replace related keywords
                    var newTemplateLines = new List <string>();
                    Dictionary <string, Module> modules = new Dictionary <string, Module>();
                    var usedModulesVariables            = new HashSet <Module>();
                    var usedModulesInput = new HashSet <Module>();
                    for (int i = 0; i < originalTextLines.Length; i++)
                    {
                        string line = originalTextLines[i];

                        //Parse #MODULES
                        if (line.StartsWith("#MODULES"))
                        {
                            //Iterate module names and try to find matching TextAssets
                            while (line != "#END" && i < originalTextLines.Length)
                            {
                                line = originalTextLines[i];
                                i++;

                                if (line == "#END")
                                {
                                    break;
                                }

                                if (line.StartsWith("//") || line.StartsWith("#") || string.IsNullOrEmpty(line))
                                {
                                    continue;
                                }

                                try
                                {
                                    var moduleName = line.Trim();
                                    var module     = Module.CreateFromName(moduleName);
                                    if (module != null)
                                    {
                                        modules.Add(moduleName, module);
                                    }
                                }
                                catch (Exception e)
                                {
                                    Debug.LogError(ShaderGenerator2.ErrorMsg(string.Format("Parsing error in <b>#MODULES</b> block:\nLine: '{0}'\n'{1}'\n{2}", line, e.Message, e.StackTrace)));
                                }
                            }
                        }

                        //Replace module keywords
                        if (line.Trim().StartsWith("[[MODULE") && i < originalTextLines.Length)
                        {
                            //extract indentation
                            var indent = "";
                            foreach (var c in line)
                            {
                                if (char.IsWhiteSpace(c))
                                {
                                    indent += c;
                                }
                                else
                                {
                                    break;
                                }
                            }

                            var start = line.IndexOf("[[MODULE:");
                            var end   = line.LastIndexOf("]]");
                            var tag   = line.Substring(start + "[[MODULE:".Length, end - start - "[[MODULE:".Length);

                            var moduleName = "";
                            var key        = "";
                            if (tag.IndexOf(':') > 0)
                            {
                                moduleName = tag.Substring(tag.IndexOf(':') + 1);

                                //remove arguments if any
                                if (moduleName.Contains("("))
                                {
                                    moduleName = moduleName.Substring(0, moduleName.IndexOf("("));
                                }

                                //extract key, if any
                                int keyStart = moduleName.IndexOf(':');
                                if (keyStart > 0)
                                {
                                    key        = moduleName.Substring(keyStart + 1);
                                    moduleName = moduleName.Substring(0, keyStart);
                                }
                            }

                            if (!string.IsNullOrEmpty(moduleName) && !modules.ContainsKey(moduleName))
                            {
                                Debug.LogError(ShaderGenerator2.ErrorMsg(string.Format("Can't find module: '{0}' for '{1}'", moduleName, line.Trim())));
                                continue;
                            }

                            if (tag.StartsWith("INPUT:"))
                            {
                                //Print Input block from specific module
                                foreach (var module in modules.Values)
                                {
                                    if (module.name == moduleName)
                                    {
                                        AddRangeWithIndent(newTemplateLines, module.InputStruct, indent);
                                        usedModulesInput.Add(module);
                                    }
                                }
                            }
                            if (tag == "INPUT")
                            {
                                //Print all Input lines from all modules
                                foreach (var module in modules.Values)
                                {
                                    if (!usedModulesInput.Contains(module))
                                    {
                                        AddRangeWithIndent(newTemplateLines, module.InputStruct, indent);
                                    }
                                }
                            }
                            else if (tag.StartsWith("VARIABLES:"))
                            {
                                //Print Variables line from specific module
                                foreach (var module in modules.Values)
                                {
                                    if (module.name == moduleName)
                                    {
                                        AddRangeWithIndent(newTemplateLines, module.Variables, indent);
                                        usedModulesVariables.Add(module);
                                    }
                                }
                            }
                            else if (tag == "VARIABLES")
                            {
                                //Print all Variables lines from all modules
                                foreach (var module in modules.Values)
                                {
                                    if (!usedModulesVariables.Contains(module))
                                    {
                                        AddRangeWithIndent(newTemplateLines, module.Variables, indent);
                                    }
                                }
                            }
                            else if (tag == "KEYWORDS")
                            {
                                //Print all Keywords lines from all modules
                                foreach (var module in modules.Values)
                                {
                                    AddRangeWithIndent(newTemplateLines, module.Keywords, indent);
                                }
                            }
                            else if (tag.StartsWith("FEATURES:"))
                            {
                                AddRangeWithIndent(newTemplateLines, modules[moduleName].Features, indent);
                            }
                            else if (tag.StartsWith("PROPERTIES_NEW:"))
                            {
                                AddRangeWithIndent(newTemplateLines, modules[moduleName].PropertiesNew, indent);
                            }
                            else if (tag.StartsWith("PROPERTIES_BLOCK:"))
                            {
                                AddRangeWithIndent(newTemplateLines, modules[moduleName].PropertiesBlock, indent);
                            }
                            else if (tag.StartsWith("SHADER_FEATURES_BLOCK"))
                            {
                                AddRangeWithIndent(newTemplateLines, modules[moduleName].ShaderFeaturesBlock, indent);
                            }
                            else if (tag.StartsWith("VERTEX:"))
                            {
                                //Get arguments if any
                                var args     = new List <string>();
                                int argStart = tag.IndexOf("(") + 1;
                                int argEnd   = tag.IndexOf(")");
                                if (argStart > 0 && argEnd > 0)
                                {
                                    string arguments      = tag.Substring(argStart, argEnd - argStart);
                                    var    argumentsSplit = arguments.Split(',');
                                    foreach (var a in argumentsSplit)
                                    {
                                        args.Add(a.Trim());
                                    }
                                }

                                AddRangeWithIndent(newTemplateLines, modules[moduleName].VertexLines(args, key), indent);
                            }
                            else if (tag.StartsWith("FRAGMENT:"))
                            {
                                //Get arguments if any
                                var args     = new List <string>();
                                int argStart = tag.IndexOf("(") + 1;
                                int argEnd   = tag.IndexOf(")");
                                if (argStart > 0 && argEnd > 0)
                                {
                                    string arguments      = tag.Substring(argStart, argEnd - argStart);
                                    var    argumentsSplit = arguments.Split(',');
                                    foreach (var a in argumentsSplit)
                                    {
                                        args.Add(a.Trim());
                                    }
                                }

                                AddRangeWithIndent(newTemplateLines, modules[moduleName].FragmentLines(args, key), indent);
                            }
                        }
                        else
                        {
                            newTemplateLines.Add(line);
                        }
                    }

                    //Apply to textLines
                    this.textLines = newTemplateLines.ToArray();

                    //Second pass: parse other blocks
                    for (int i = 0; i < textLines.Length; i++)
                    {
                        var line = textLines[i];
                        if (line.StartsWith("#INFO="))
                        {
                            templateInfo = line.Substring("#INFO=".Length).TrimEnd().Replace("  ", "\n");
                        }

                        else if (line.StartsWith("#WARNING="))
                        {
                            templateWarning = line.Substring("#WARNING=".Length).TrimEnd().Replace("  ", "\n");
                        }

                        else if (line.StartsWith("#CONFIG="))
                        {
                            templateType = line.Substring("#CONFIG=".Length).TrimEnd().ToLower();
                        }

                        else if (line.StartsWith("#TEMPLATE_KEYWORDS="))
                        {
                            templateKeywords = line.Substring("#TEMPLATE_KEYWORDS=".Length).TrimEnd().Split(',');
                        }

                        else if (line.StartsWith("#ID="))
                        {
                            id = line.Substring("#ID=".Length).TrimEnd();
                        }

                        else if (line.StartsWith("#FEATURES"))
                        {
                            uiFeatures = UIFeature.GetUIFeatures(textLines, ref i, this);
                        }

                        else if (line.StartsWith("#PROPERTIES_NEW"))
                        {
                            shaderProperties = GetShaderProperties(textLines, i);
                            return;
                        }

                        //Config meta should appear before the Shader name line
                        else if (line.StartsWith("Shader"))
                        {
                            return;
                        }
                    }

                    if (id == null)
                    {
                        Debug.LogWarning(ShaderGenerator2.ErrorMsg("Missing ID in template metadata!"));
                    }
                }
            }
Exemple #5
0
            //Parses a #FEATURES text block
            internal static UIFeature[] GetUIFeatures(string[] lines, ref int i, Template template)
            {
                var    uiFeaturesList = new List <UIFeature>();
                string subline;

                do
                {
                    subline = lines[i];
                    i++;

                    //Empty line
                    if (string.IsNullOrEmpty(subline))
                    {
                        continue;
                    }

                    var data = subline.Split(new[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);

                    //Skip empty or comment # lines
                    if (data == null || data.Length == 0 || (data.Length > 0 && data[0].StartsWith("#")))
                    {
                        continue;
                    }

                    var kvpList = new List <KeyValuePair <string, string> >();
                    for (var j = 1; j < data.Length; j++)
                    {
                        var sdata = data[j].Split('=');
                        if (sdata.Length == 2)
                        {
                            kvpList.Add(new KeyValuePair <string, string>(sdata[0], sdata[1]));
                        }
                        else if (sdata.Length == 1)
                        {
                            kvpList.Add(new KeyValuePair <string, string>(sdata[0], null));
                        }
                        else
                        {
                            Debug.LogError("Couldn't parse UI property from Template:\n" + data[j]);
                        }
                    }

                    // Discard the UIFeature if not for this template:
                    var templateCompatibility = kvpList.Find(kvp => kvp.Key == "templates");
                    if (templateCompatibility.Key == "templates")
                    {
                        if (!templateCompatibility.Value.Contains(template.id))
                        {
                            continue;
                        }
                    }

                    UIFeature feature = null;
                    switch (data[0])
                    {
                    case "---": feature = new UIFeature_Separator(); break;

                    case "space": feature = new UIFeature_Space(kvpList); break;

                    case "flag": feature = new UIFeature_Flag(kvpList, false); break;

                    case "nflag": feature = new UIFeature_Flag(kvpList, true); break;

                    case "float": feature = new UIFeature_Float(kvpList); break;

                    case "subh": feature = new UIFeature_SubHeader(kvpList); break;

                    case "header": feature = new UIFeature_Header(kvpList); break;

                    case "warning": feature = new UIFeature_Warning(kvpList); break;

                    case "sngl": feature = new UIFeature_Single(kvpList); break;

                    case "mult": feature = new UIFeature_Multiple(kvpList); break;

                    case "keyword": feature = new UIFeature_Keyword(kvpList); break;

                    case "keyword_str": feature = new UIFeature_KeywordString(kvpList); break;

                    case "dd_start": feature = new UIFeature_DropDownStart(kvpList); break;

                    case "dd_end": feature = new UIFeature_DropDownEnd(); break;

                    case "mult_fs": feature = new UIFeature_MultipleFixedFunction(kvpList); break;

                    default: feature = new UIFeature(kvpList); break;
                    }

                    uiFeaturesList.Add(feature);
                }while(subline != "#END" && i < lines.Length);

                var uiFeaturesArray = uiFeaturesList.ToArray();

                // Build hierarchy from the parsed UIFeatures
                // note: simple hierarchy, where only a top-level element can be parent (one level only)
                UIFeature lastParent = null;

                for (int j = 0; j < uiFeaturesArray.Length; j++)
                {
                    var uiFeature = uiFeaturesArray[j];
                    if (uiFeature.indentLevel == 0 && !(uiFeature is UIFeature_Header) && !(uiFeature is UIFeature_Warning) && !uiFeature.inline)
                    {
                        lastParent = uiFeature;
                    }
                    else if (uiFeature.indentLevel > 0)
                    {
                        uiFeature.parent = lastParent;
                    }
                }

                return(uiFeaturesList.ToArray());
            }