Exemplo n.º 1
0
 public void SetAsset(MlfAsset asset)
 {
     this.scriptString = asset.mlfObject.MlfInstance.RawScript;
     instanced         = false;
     compiledCode      = null;
     instance          = null;
 }
Exemplo n.º 2
0
 public void SetString(string scriptString)
 {
     this.scriptString = scriptString;
     instanced         = false;
     compiledCode      = null;
     instance          = null;
 }
Exemplo n.º 3
0
        public override void OnMlfFormatPre(MlfInstance instance)
        {
            foreach (MlfBlock block in instance.Blocks)
            {
                if (block.format != "yaml")
                {
                    continue;
                }

                //Detect tabs
                if (block.Content.Contains("\t"))
                {
                    Debug.LogError("YAML Does not support tabs.");
                    return;
                }


                YamlObject yaml = new YamlObject(block.Content);
                block.SetFormatData(formatDataKey, yaml);

                block.OnContentChange += (content) =>
                {
                    yaml.SetContent(content);
                };
            }
        }
Exemplo n.º 4
0
        public override void OnMlfFormatPost(MlfInstance instance)
        {
            List <MlfBlock> blocks = instance.GetBlocks("DefineContext");

            foreach (MlfBlock block in blocks)
            {
                if (block.format != "python")
                {
                    Debug.LogError("Context blocks must be in python format");
                    continue;
                }

                if (block.tags.Count == 0)
                {
                    Debug.LogError("Context block must have at least one tag, defining what context it is defining");
                    continue;
                }

                block.enableFunctionWrapping = false;

                foreach (string tag in block.tags)
                {
                    Snek.SnekScriptEngine.Instance.AddContextDefinition(tag, block.Content);
                }
            }
        }
Exemplo n.º 5
0
        public override void OnMlfInstanceInterpret(MlfInstance instance)
        {
            List <MlfFlag> flags = instance.GetFlags("Context");

            if (flags.Count == 0)
            {
                return;
            }

            foreach (MlfBlock block in instance.Blocks)
            {
                block.contexts = new List <MlfContextReference>();

                foreach (MlfFlag flag in flags)
                {
                    //Bad Context flag
                    if (flag.tags.Count != 1)
                    {
                        continue;
                    }

                    if (flag.arguments.Count != 1)
                    {
                        continue;
                    }

                    block.contexts.Add(new MlfContextReference(flag.tags[0], flag.arguments[0]));
                }
            }
        }
Exemplo n.º 6
0
 public static void OnInstancePostInterpret(MlfInstance instance)
 {
     foreach (MlfProcessor processor in Processors)
     {
         processor.OnMlfFormatPost(instance);
     }
 }
Exemplo n.º 7
0
        public override string DebugInstance(MlfInstance instance)
        {
            List <MlfFlag> flags = instance.GetFlags("Context");

            if (flags.Count == 0)
            {
                return(null);
            }

            string output = "Context Flags:\n";

            foreach (MlfFlag flag in flags)
            {
                //Bad Context flag
                if (flag.tags.Count != 1)
                {
                    output += "(Bad tag)";
                }

                if (flag.arguments.Count != 1)
                {
                    output += "(Bad argument)";
                }
                else
                {
                    output += "ID: " + flag.tags[0] + ", Key: " + flag.arguments[0];
                }
            }

            return(output);
        }
Exemplo n.º 8
0
        public static void CleanCode(MlfInstance instance)
        {
            foreach (MlfBlock block in instance.Blocks)
            {
                //Remove #
                block.Content = Regex.Replace(block.Content, "[#].*", "");

                //Remove double newlines
                block.Content = Regex.Replace(block.Content, @"\n\s*\n", "\n");
            }
        }
Exemplo n.º 9
0
        public static List <string> DebugInstance(MlfInstance instance)
        {
            List <string> instanceDebugs = new List <string>();

            foreach (MlfProcessor processor in Processors)
            {
                instanceDebugs.Add(processor.DebugInstance(instance));
            }

            return(instanceDebugs);
        }
Exemplo n.º 10
0
        public override void OnMlfFormatPost(MlfInstance instance)
        {
            List <MlfBlock> blocks = instance.GetBlocks("DefineLocalScope");

            foreach (MlfBlock block in blocks)
            {
                if (block.format != "python")
                {
                    Debug.LogError("Scope blocks must be in python format");
                    continue;
                }
            }
        }
Exemplo n.º 11
0
        public override void OnMlfFormatPre(MlfInstance instance)
        {
            foreach (MlfBlock block in instance.Blocks)
            {
                if (block.format != "python")
                {
                    continue;
                }

                SnekScriptSource source = new SnekScriptSource(block);

                block.SetFormatData("scriptSource", source);
            }
        }
Exemplo n.º 12
0
        public override void OnMlfInstanceInterpret(MlfInstance instance)
        {
            MlfFlag flag = instance.GetFlag("DefaultImport");

            if (flag != null)
            {
                foreach (MlfBlock block in instance.Blocks)
                {
                    if (block.format == "python")
                    {
                        block.AddPrefixText("import UnityEngine as u");
                    }
                }
            }
        }
Exemplo n.º 13
0
 public MlfObject(string rawText, string path)
 {
     this.mlfInstance = new MlfInstance(rawText, path);
 }
Exemplo n.º 14
0
 public MlfObject(MlfInstance mlfInstance)
 {
     this.mlfInstance = mlfInstance;
 }
Exemplo n.º 15
0
 public void SetMlfInstance(MlfInstance instance)
 {
     parentAsset = null;
     mlfInstance = instance;
 }
Exemplo n.º 16
0
        public void OnGUI()
        {
            scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);


            //Regex:
            regexTesterDropdown = EditorGUILayout.Foldout(regexTesterDropdown, "Regex tester", true);

            if (regexTesterDropdown)
            {
                EditorGUI.indentLevel++;

                regex_pattern = EditorGUILayout.TextField("Pattern", regex_pattern);
                EditorGUILayout.LabelField("Text:");
                regex_text = EditorGUILayout.TextArea(regex_text, GUILayout.MinHeight(400));

                EditorGUILayout.BeginHorizontal();

                if (GUILayout.Button("Match", GUILayout.ExpandWidth(false)))
                {
                    MatchCollection matches = Regex.Matches(regex_text, regex_pattern);
                    regex_output  = "(Match)\n";
                    regex_output += "count: " + matches.Count + "\n";

                    for (int i = 0; i < matches.Count; i++)
                    {
                        regex_output += i + ": " + matches[i].Value + "\n";
                    }
                }

                if (GUILayout.Button("Split", GUILayout.ExpandWidth(false)))
                {
                    string[] splits = Regex.Split(regex_text, regex_pattern);
                    regex_output  = "(Split)\n";
                    regex_output += "count: " + splits.Length + "\n";

                    for (int i = 0; i < splits.Length; i++)
                    {
                        regex_output += i + ": " + splits[i] + "\n";
                    }
                }


                EditorGUILayout.EndHorizontal();

                EditorGUILayout.LabelField("Output:");
                EditorGUILayout.SelectableLabel(regex_output, EditorStyles.helpBox, GUILayout.MinHeight(400));
            }


            inputScriptDopdown = EditorGUILayout.Foldout(inputScriptDopdown, "Input Script", true);

            if (inputScriptDopdown)
            {
                EditorGUI.indentLevel++;

                //Asset Selection
                EditorGUILayout.BeginHorizontal();

                scriptAsset = (MlfAsset)EditorGUILayout.ObjectField(scriptAsset, typeof(MlfAsset), false);

                if (GUILayout.Button("Use Asset", GUILayout.ExpandWidth(false)) && scriptAsset != null)
                {
                    SetAsset(scriptAsset);
                }

                EditorGUILayout.EndHorizontal();

                //Text sekection
                EditorGUILayout.BeginHorizontal();

                textInputText = EditorGUILayout.TextArea(textInputText);

                if (GUILayout.Button("Use Text", GUILayout.ExpandWidth(false)) && textInputText.Replace(" ", "") != "")
                {
                    SetString(textInputText);
                }
                EditorGUILayout.EndHorizontal();

                EditorGUI.indentLevel--;
            }

            if (scriptString == null)
            {
                EditorGUILayout.EndScrollView();
                return;
            }

            clean = EditorGUILayout.Toggle("Clean", clean);

            if (instance == null && instanced == false)
            {
                instance = new MlfInstance(scriptString);
                instance.Interpret(clean);
                instanced = true;
            }

            if (instance != null && instance.Blocks != null)
            {
                DisplayScriptInfo();
            }
            EditorGUILayout.EndScrollView();
        }
Exemplo n.º 17
0
        public override void OnMlfInstanceInterpret(MlfInstance instance)
        {
            MlfBlock propertyBlock = instance.GetBlock("Properties");

            if (propertyBlock == null)
            {
                instance.defaultProperties = null;
                return;
            }

            //Only support yaml format
            if (propertyBlock.format != "yaml")
            {
                Debug.LogError("[MLF] Properties block only supports the 'yaml' format \n" + instance.path);
                return;
            }

            Dictionary <string, MlfProperty> properties = new Dictionary <string, MlfProperty>();

            Yaml.YamlObject yaml = (Yaml.YamlObject)propertyBlock.GetFormatData(YamlFormat.formatDataKey);

            //Root
            foreach (YamlNode node in ((YamlSequenceNode)yaml.RootNode).Children)
            {
                //Should only be a single child for this entry
                foreach (var entry in ((YamlMappingNode)node).Children)
                {
                    MlfProperty property = new MlfProperty();

                    //Scalar -> value = Type
                    if (entry.Value.NodeType == YamlNodeType.Scalar)
                    {
                        System.Type type = null;

                        if (!TypeFinder.TryFindType(((YamlScalarNode)entry.Value).Value, out type))
                        {
                            continue;
                        }

                        property.type = type;
                    }

                    //Mapping -> read children values
                    else if (entry.Value.NodeType == YamlNodeType.Mapping)
                    {
                        IDictionary <YamlNode, YamlNode> entries = ((YamlMappingNode)entry.Value).Children;

                        if (entries.ContainsKey("specialtype"))
                        {
                            property.specialType = ((YamlScalarNode)entries["specialtype"]).Value;
                        }

                        if (entries.ContainsKey("type"))
                        {
                            System.Type type = null;

                            if (!TypeFinder.TryFindType(((YamlScalarNode)entries["type"]).Value, out type))
                            {
                                continue;
                            }

                            property.type = type;
                        }

                        //Default value
                        if (entries.ContainsKey("default"))
                        {
                            property.Value = ((YamlScalarNode)entries["default"]).Value;
                        }

                        //Value value, equivilant to default
                        if (entries.ContainsKey("value"))
                        {
                            property.Value = ((YamlScalarNode)entries["value"]).Value;
                        }

                        if (entries.ContainsKey("tooltip"))
                        {
                            property.tooltip = ((YamlScalarNode)entries["tooltip"]).Value;
                        }

                        if (entries.ContainsKey("static"))
                        {
                            //TODO: Use actual resolver instead of crappy check
                            string staticVar = ((YamlScalarNode)entries["static"]).Value.ToLower().Trim();

                            if (staticVar == "yes" || staticVar == "true")
                            {
                                property.staticVar = true;
                            }
                            else
                            {
                                property.staticVar = false;
                            }
                        }
                    }

                    properties[((YamlScalarNode)entry.Key).Value.TrimStart('$')] = property;
                }
            }

            Dictionary <string, MlfProperty> oldProperties = instance.defaultProperties;

            instance.defaultProperties = properties;

            //Replace old values if they exist
            if (oldProperties != null)
            {
                foreach (KeyValuePair <string, MlfProperty> pair in oldProperties)
                {
                    if (instance.defaultProperties.ContainsKey(pair.Key))
                    {
                        if (instance.defaultProperties[pair.Key].CanAssignValue(pair.Value.Value))
                        {
                            instance.defaultProperties[pair.Key].Value = pair.Value.Value;
                        }
                    }
                }
            }

            if (instance.defaultProperties.Count == 0)
            {
                return;
            }

            //Replace properties in contents
            //TODO: Make individual formats handle this in a "post post processor", as well as
            //fix issues with parts of words being replaced!
            foreach (MlfBlock block in instance.Blocks)
            {
                foreach (KeyValuePair <string, MlfProperty> pair in instance.defaultProperties)
                {
                    block.Content = Regex.Replace(block.Content, @"\s*\$" + pair.Key + @"\s*", Snek.SnekScriptEngine.propertyPrefix + pair.Key);
                }
            }
        }
Exemplo n.º 18
0
 public MlfBlock(MlfInstance mlfInstance)
 {
     this.owningInstance = mlfInstance;
     this.formatData     = new Dictionary <string, object>();
 }
Exemplo n.º 19
0
 public MlfObject(string rawText)
 {
     this.mlfInstance = new MlfInstance(rawText);
 }
Exemplo n.º 20
0
        public static List <MlfBlock> FindBlocks(MlfInstance instance, string script, string path)
        {
            List <MlfBlock> codeblocks = new List <MlfBlock>();

            //Block header pattern
            string pattern = @"(?:@.+\n|\r)?\[\[.+\]\]";

            MatchCollection matches = Regex.Matches(script, pattern);

            string[] headers = new string[matches.Count];

            for (int i = 0; i < matches.Count; i++)
            {
                headers[i] = matches[i].Value;
            }

            string[] contents   = Regex.Split(script, pattern);
            int[]    lineCounts = new int[contents.Length];

            //Build line counts
            for (int i = 0; i < lineCounts.Length; i++)
            {
                lineCounts[i] = Regex.Matches(contents[i], "\n|\r\n?").Count - 1;
            }

            //Build blocks
            for (int i = 0; i < headers.Length; i++)
            {
                Match m = Regex.Match(headers[i], @"(|@.+\n|\r)\[\[(.+?)\s*(|"".+"")\s*(|\(.+\))\]\]");

                int format_group   = 1;
                int id_group       = 2;
                int tag_group      = 3;
                int argument_group = 4;

                MlfBlock block = new MlfBlock(instance);
                block.line = lineCounts[i];
                block.path = path;

                //ID Required
                if (!m.Groups[id_group].Success)
                {
                    continue;
                }

                //ID
                block.id = m.Groups[id_group].Value;

                //Format
                if (m.Groups[format_group].Success)
                {
                    block.format = m.Groups[format_group].Value.TrimStart('@').Trim();
                }

                //Tags
                if (m.Groups[tag_group].Success && m.Groups[tag_group].Value.Length != 0)
                {
                    block.tags = new List <string>(m.Groups[tag_group].Value.Trim('"').Split(','));
                }
                else
                {
                    block.tags = new List <string>();
                }

                //Arguments
                if (m.Groups[argument_group].Success && m.Groups[argument_group].Value.Length != 0)
                {
                    block.arguments = new List <string>(m.Groups[argument_group].Value.Trim('(', ')').Split(','));
                }
                else
                {
                    block.arguments = new List <string>();
                }

                //Expression
                if (contents.Length > i + 1)
                {
                    block.Content = contents[i + 1];
                }

                block.path = path;
                codeblocks.Add(block);
            }

            //Find "root code" (Code without a block) and implement it into all blocks.
            if (codeblocks.Count > 1)
            {
                foreach (MlfBlock block in codeblocks)
                {
                    block.AddPrefixText(contents[0]);
                }
            }

            return(codeblocks);
        }