Esempio n. 1
0
        /// <summary>
        /// TXT解析为YAML
        /// </summary>
        /// <param name="codeFilePath"></param>
        public string TxtResolveToYaml(string codeFilePath)
        {
            List <ModClass> modClassParent = new List <ModClass>();
            ModClass        modClass       = new ModClass();

            LoopPars(countryEvent, ref modClass, 0);
            modClassParent = modClass.child;

            FileStream   fs = new FileStream(Path.GetDirectoryName(codeFilePath) + "\\" + Path.GetFileNameWithoutExtension(codeFilePath) + ".me", FileMode.Create);
            StreamWriter sw = new StreamWriter(fs, new UTF8Encoding(false));
            //
            var serializer = new SerializerBuilder().Build();
            var yaml       = serializer.Serialize(modClassParent);

            sw.Write(yaml);
            sw.Close();
            fs.Close();

            return(yaml);
        }
Esempio n. 2
0
        /// <summary>
        /// 循环解析
        /// </summary>
        /// <param name="codeParent"></param>
        /// <param name="modClassParent"></param>
        /// <param name="level"></param>
        public void LoopPars(List <string> codeParent, ref ModClass modClassParent, int level)
        {
            int          startLine = -1;
            int          endLine   = -1;
            Stack <char> stack     = new Stack <char>();

            for (int i = 0; i < codeParent.Count; i++)
            {
                List <string> lineClassChild = new List <string>();

                for (int stackIndex = 0; stackIndex < codeParent[i].Length; stackIndex++)
                {
                    switch (codeParent[i][stackIndex])
                    {
                    case '{':
                        stack.Push(codeParent[i][stackIndex]);
                        //找到括号且无开始行,设置开始行
                        if (startLine == -1)
                        {
                            startLine = i;
                        }
                        break;

                    case '}':
                        if (stack.Count > 0 && stack.Pop() == '{')
                        {
                            //完全匹配,且有开始行
                            if (stack.Count == 0 && startLine > -1)
                            {
                                ModClass modClass = new ModClass();
                                string[] strs     = codeParent[startLine].Split('=');
                                modClass.level = level;
                                modClass.id    = i;
                                modClass.type  = Type.function;
                                modClass.name  = strs[0].Trim();
                                int lv = modClass.level + 1;
                                modClassParent.child.Add(modClass);

                                endLine = i;
                                while (startLine <= endLine)
                                {
                                    lineClassChild.Add(codeParent[startLine]);
                                    startLine++;
                                }
                                startLine = -1;
                                endLine   = -1;
                                //删除空字符串数组
                                lineClassChild = lineClassChild.Where(s => !string.IsNullOrEmpty(s.Trim())).ToList();
                                //
                                List <string> tempLine = new List <string>();
                                for (int ii = 0; ii < lineClassChild.Count; ii++)
                                {
                                    string[] ss = lineClassChild[ii].Split('\n');
                                    foreach (var item in ss)
                                    {
                                        tempLine.Add(item);
                                    }
                                }
                                tempLine = tempLine.Where(s => !string.IsNullOrEmpty(s.Trim())).ToList();
                                tempLine.RemoveAt(0);
                                tempLine.RemoveAt(tempLine.Count - 1);

                                LoopPars(tempLine, ref modClass, lv);
                            }
                            break;
                        }
                        else
                        {
                            return;
                        }

                    default:
                        continue;
                    }
                }

                //当前行没有括号
                if (!Regex.IsMatch(codeParent[i], @" *= *{+ *\n*") && !Regex.IsMatch(codeParent[i], @" *\n*}+ *\n*"))
                {
                    //该行没有括号且正在匹配括号为0
                    if (stack.Count == 0)
                    {
                        ModClass modClass = new ModClass();
                        string[] strs     = new string[2];
                        if (Regex.IsMatch(codeParent[i], @">+"))
                        {
                            strs        = codeParent[i].Split('>');
                            modClass.op = ">";
                        }
                        else if (Regex.IsMatch(codeParent[i], @"<+"))
                        {
                            strs        = codeParent[i].Split('<');
                            modClass.op = "<";
                        }
                        else if (Regex.IsMatch(codeParent[i], @"=+"))
                        {
                            strs        = codeParent[i].Split('=');
                            modClass.op = "=";
                        }
                        else
                        {
                            strs = codeParent[i].Split(' ');
                            strs = strs.Where(s => !string.IsNullOrEmpty(s.Trim())).ToArray();
                        }

                        if (string.IsNullOrWhiteSpace(modClass.op))
                        {
                            //空
                            foreach (var item in strs)
                            {
                                modClass.name += item + " ";
                            }
                            modClass.level = level;
                            modClass.id    = i;
                            modClass.type  = Type.variable;
                            modClass.name  = modClass.name.Trim();
                            modClassParent.child.Add(modClass);
                        }
                        else
                        {
                            modClass.level = level;
                            modClass.id    = i;
                            modClass.type  = Type.variable;
                            modClass.name  = strs[0].Trim();
                            modClass.val   = strs[1].Trim();
                            modClassParent.child.Add(modClass);
                        }
                    }
                }
            }
        }