private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex > -1)
            {
                if (Functions != null)
                {
                    listBox2.Items.Clear();
                    richTextBox1.Text  = "";
                    richTextBox1.Text += listBox1.SelectedItem.ToString() + "\n";
                    foreach (Compiler.Function f in Functions[listBox1.SelectedItem.ToString()])
                    {
                        foreach (var arg in f.args)
                        {
                            listBox2.Items.Add(arg.name + " : " + arg.GetTypeString());
                        }
                        if (Functions[listBox1.SelectedItem.ToString()].Count > 1)
                        {
                            listBox2.Items.Add("==========================");
                        }
                        richTextBox1.Text += f.desc + "\n==========================\n";
                        richTextBox1.Text += $"lazy: {f.lazy.ToString()} adj: {f?.package}\nattributes: {(f?.attributes.Count > 0 ? f?.attributes.Aggregate((x, y) => (x + ", " + y)):"")}\n\n";
                        foreach (string line in f.file.parsed)
                        {
                            richTextBox1.Text += line + "\n";
                        }
                        richTextBox1.Text += "\n==========================\n";
                    }
                }
                if (Structures != null)
                {
                    listBox2.Items.Clear();
                    Compiler.Structure f = Structures[listBox1.SelectedItem.ToString()];
                    foreach (var arg in f.fields)
                    {
                        listBox2.Items.Add(arg.name + " : " + arg.GetTypeString());
                    }
                    richTextBox1.Text = $"attributes: {(f.attributes.Count > 0 ? f.attributes.Aggregate((x, y) => (x + ", " + y)):"")}";
                }
                if (Variables != null)
                {
                    listBox2.Items.Clear();
                    Compiler.Variable f = Variables[listBox1.SelectedItem.ToString()];
                    richTextBox1.Text = f.gameName + ": " + f.GetTypeString() + " entity:" + f.entity.ToString() + $"\nattributes: {(f.attributes.Count>0?f.attributes.Aggregate((x, y) => (x + ", " + y)):"")}";
                }
                if (Enums != null)
                {
                    listBox2.Items.Clear();
                    List <string> f = Enums[listBox1.SelectedItem.ToString()].Values();

                    foreach (var arg in f)
                    {
                        listBox2.Items.Add(arg);
                    }
                }
                if (Tags != null)
                {
                    listBox2.Items.Clear();
                    List <string> f = Tags[listBox1.SelectedItem.ToString()].values;

                    foreach (var arg in f)
                    {
                        listBox2.Items.Add(arg);
                    }
                }
                if (names != null)
                {
                    listBox2.Items.Clear();
                    listBox2.Items.Add(listBox1.SelectedItem.ToString());
                    richTextBox1.Text = listBox1.SelectedItem.ToString();
                }
                if (Predicates != null)
                {
                    listBox2.Items.Clear();
                    foreach (var v in Predicates[listBox1.SelectedItem.ToString()])
                    {
                        richTextBox1.Text += v.baseFile.content + "\n===================\n";
                    }
                }
            }
        }
Esempio n. 2
0
        public string[] FormatJsonInternal(string[] args, Compiler.Context context, int start = 0)
        {
            string           output        = "[";
            string           output2       = "";
            string           output3       = "";
            int              strTag        = 0;
            HashSet <string> unpackedFloat = new HashSet <string>();

            for (int i = start; i < args.Length; i++)
            {
                string   arg          = Compiler.smartEmpty(args[i]).StartsWith("(") ? Compiler.smartEmpty(args[i].Substring(args[i].IndexOf('(') + 1, args[i].LastIndexOf(')') - args[i].IndexOf('(') - 1)) : args[i];
                string[] subargs      = Compiler.smartSplit(arg, ',');
                bool     ignoreFormat = false;
                if (subargs[0].Contains("\""))
                {
                    string ext = Compiler.smartExtract(subargs[0]);
                    if (ext.StartsWith("\"") && ext.EndsWith("\""))
                    {
                        output += ",{\"text\":" + subargs[0];
                    }
                    else if (ext.StartsWith("f\"") && ext.EndsWith("\""))
                    {
                        string[] part = Compiler.extractString(ext.Substring(1, ext.Length - 1)).Replace("{", "}").Split('}');

                        var a2     = Enumerable.Range(0, part.Length);
                        var recArg = (subargs.Length > 1) ? subargs.Skip(1).Aggregate((x, y) => x + "," + y) : "";
                        var part2  = part.Zip(a2, (first, second) => second % 2 == 0 ? "\"" + first + "\"" : first)
                                     .Where(x => x != "\"\"")
                                     .Select(x => "(" + x + "," + recArg + ")")
                                     .Aggregate((x, y) => x + "," + y);


                        string[] outs = FormatJsonInternal(Compiler.smartSplit(part2, ','), context);
                        output  += "," + outs[0].Substring(1, outs[0].Length - 2);
                        output2 += outs[1];
                        output3 += outs[2];


                        ignoreFormat = true;
                    }
                    else
                    {
                        throw new Exception("JSON Syntaxe Error");
                    }
                }
                else if (subargs[0].Contains("@"))
                {
                    output += ",{\"selector\":\"" + subargs[0] + "\"";
                }
                else if (float.TryParse(subargs[0], out float _))
                {
                    output += ",{\"text\":\"" + subargs[0] + "\"";
                }
                else if (Compiler.isStringVar(subargs[0]))
                {
                    string tmp = Compiler.getString(subargs[0]);
                    tmp    += "tag @e[tag=__str__,tag=!__str__tag__] add __str_" + strTag.ToString() + "\n";
                    tmp    += "tag @e[tag=__str__,tag=!__str__tag__] add __str__tag__" + "\n";
                    output += ",{\"selector\":\"" + "@e[tag=__str_" + strTag.ToString() + "]" + "\"";
                    strTag++;
                    output2 += tmp;
                }
                else if (context.GetVarType(subargs[0]) == Compiler.Type.ENTITY)
                {
                    output += ",{\"selector\":\"" + context.GetEntitySelector(subargs[0]) + "\"";
                }
                else if (context.GetVarType(subargs[0]) == Compiler.Type.ARRAY)
                {
                    int nb = Compiler.GetVariable(context.GetVariable(subargs[0])).arraySize;
                    ignoreFormat = true;
                    for (int j = 0; j < Compiler.GetVariable(context.GetVariable(subargs[0])).arraySize; j++)
                    {
                        var va = Compiler.GetVariable(context.GetVariable(subargs[0] + "." + j.ToString()));
                        if (va.type == Compiler.Type.STRING)
                        {
                            string tmp = Compiler.getString(subargs[0]);
                            tmp    += "tag @e[tag=__str__,tag=!__str__tag__] add __str_" + strTag.ToString() + "\n";
                            tmp    += "tag @e[tag=__str__,tag=!__str__tag__] add __str__tag__" + "\n";
                            output += ",{\"selector\":\"" + "@e[tag=__str_" + strTag.ToString() + "]" + "\"";
                            output += jsonSubArg(subargs, context);
                            output += "}";
                            strTag++;
                            output2 += tmp;
                        }
                        else
                        {
                            if (j == 0)
                            {
                                output += ",{\"text\":" + "\"[\"";
                                output += jsonSubArg(subargs, context);
                                output += "}";
                            }
                            string[] v = Compiler.GetVariableByName(subargs[0] + "." + j.ToString()).scoreboard().Split(' ');
                            output += ",{ \"score\":{ \"name\":\"" + v[0] + "\",\"objective\":\"" + v[1] + "\"}";
                            output += jsonSubArg(subargs, context);
                            output += "}";
                            output += ",{\"text\":" + "\", \"";
                            output += jsonSubArg(subargs, context);
                            output += "}";
                            if (j == nb - 1)
                            {
                                output += ",{\"text\":" + "\"]\"";
                                output += jsonSubArg(subargs, context);
                            }
                        }
                    }
                }
                else if (context.GetVarType(subargs[0]) == Compiler.Type.STRUCT)
                {
                    Compiler.Structure s = Compiler.structs[Compiler.GetVariableByName(subargs[0]).enums];
                    output += ",{\"text\":\"" + s.name + "(" + "\"";
                    output += jsonSubArg(subargs, context);
                    output += "}";
                    foreach (var v in s.fields)
                    {
                        output += ",{\"text\":\"" + v.name + " = " + "\"";
                        output += jsonSubArg(subargs, context);
                        output += "}";

                        string tmp = FormatJsonInternal(new string[] { subargs[0] + "." + v.name }, context, 0)[0];
                        output += "," + tmp.Substring(1, tmp.Length - 2);

                        output += ",{\"text\":\"" + ", " + "\"";
                        output += jsonSubArg(subargs, context);
                        output += "}";
                    }
                    output += ",{\"text\":\"" + ")" + "\"";
                }
                else if (context.GetVarType(subargs[0]) == Compiler.Type.FLOAT)
                {
                    var v = Compiler.GetVariableByName(subargs[0]);
                    if (!unpackedFloat.Contains(v.gameName))
                    {
                        output2 += Compiler.parseLine($"int {v.gameName}.u #= {v.gameName}") + "\n";
                        output2 += Compiler.parseLine($"{v.gameName}.u /={Compiler.compilerSetting.FloatPrecision}") + "\n";
                        output2 += Compiler.parseLine($"int {v.gameName}.l #= {v.gameName}") + "\n";
                        output2 += Compiler.parseLine($"{v.gameName}.l %={Compiler.compilerSetting.FloatPrecision}") + "\n";

                        unpackedFloat.Add(v.gameName);
                    }
                    var vu = Compiler.GetVariableByName($"{v.gameName}.u").scoreboard().Split(' ');
                    var vl = Compiler.GetVariableByName($"{v.gameName}.l").scoreboard().Split(' ');
                    output += ",{ \"score\":{ \"name\":\"" + vu[0] + "\",\"objective\":\"" + vu[1] + "\"}";
                    output += jsonSubArg(subargs, context) + "}";
                    output += ",{\"text\":\".\"";
                    output += jsonSubArg(subargs, context) + "}";
                    output += ",{ \"score\":{ \"name\":\"" + vl[0] + "\",\"objective\":\"" + vl[1] + "\"}";
                }
                else
                {
                    string[] v = Compiler.GetVariableByName(subargs[0]).scoreboard().Split(' ');
                    output += ",{ \"score\":{ \"name\":\"" + v[0] + "\",\"objective\":\"" + v[1] + "\"}";
                }
                if (!ignoreFormat)
                {
                    output += jsonSubArg(subargs, context);
                }
                if (!ignoreFormat)
                {
                    output += "}";
                }
            }

            output += "]";

            if (strTag > 0)
            {
                output3 += "\nkill @e[tag=__str__]";
            }
            output = "[" + output.Substring(2, output.Length - 2);
            return(new string[] { output, output2, output3 });
        }