Exemple #1
0
        public static string DealWithFormulaSheetGo(ISheet sheet)
        {
            CodeTemplate.curlang = CodeTemplate.Langue.Go;

            StringBuilder sb = new StringBuilder();
            Dictionary <CellCoord, List <CellCoord> > abouts = new Dictionary <CellCoord, List <CellCoord> >();
            string sheetName = sheet.SheetName.Substring(3);
            string SheetName = sheetName.Substring(0, 1).ToUpper() + sheetName.Substring(1);

            sb.AppendLine("package config");
            sb.AppendLine("type " + SheetName + "FormulaSheet struct {");
            sb.AppendLine("formulaSheet");
            sb.AppendLine("}");

            sb.AppendLine("var " + sheetName + "FormaulaTemplate *formulaSheetTemplate");
            sb.AppendLine("func loadFormula" + SheetName + "() {");
            sb.AppendLine(sheetName + "FormaulaTemplate = new(formulaSheetTemplate)");
            sb.AppendLine(sheetName + "FormaulaTemplate.datas = make(map[int32]float64)");
            sb.AppendLine(sheetName + "FormaulaTemplate.relation = make(map[int32][]int32)");
            sb.AppendLine(sheetName + "FormaulaTemplate.funcs = make(map[int32]func(*formulaSheet) float64)");

            // 数据内容
            for (int rownum = 0; rownum <= sheet.LastRowNum; rownum++)
            {
                IRow row = sheet.GetRow(rownum);
                if (row == null)
                {
                    continue;
                }

                for (int i = 0; i < row.Cells.Count; i++)
                {
                    ICell cell   = row.Cells[i];
                    int   colnum = cell.ColumnIndex;

                    // in、out声明忽略
                    if (colnum == 0 || colnum == 1 || colnum == 3 || colnum == 4)
                    {
                        continue;
                    }

                    if (cell.CellType == CellType.Boolean || cell.CellType == CellType.Numeric)
                    {
                        sb.AppendLine(sheetName + "FormaulaTemplate.datas[" + ((rownum + 1) * 1000 + colnum + 1) +
                                      "] = " + (cell.CellType == CellType.Boolean
                                          ? (cell.BooleanCellValue ? 1 : 0).ToString()
                                          : cell.NumericCellValue.ToString()));
                    }
                    else if (cell.CellType == CellType.Formula)
                    {
                        List <CellCoord> about;
                        sb.AppendLine(sheetName + "FormaulaTemplate.funcs[" + ((rownum + 1) * 1000 + colnum + 1) +
                                      "] = func(ins *formulaSheet) float64 {");
                        sb.AppendLine("return " +
                                      Formula2Code.Translate(sheet, cell.CellFormula, cell.ToString(), out about));
                        sb.AppendLine("}");

                        CellCoord cur = new CellCoord(rownum + 1, colnum + 1);
                        foreach (CellCoord cc in about)
                        {
                            if (!abouts.ContainsKey(cc))
                            {
                                abouts.Add(cc, new List <CellCoord>());
                            }
                            if (!abouts[cc].Contains(cur))
                            {
                                abouts[cc].Add(cur);
                            }
                        }
                    }
                }
            }

            // 数据影响关联递归统计
            bool change;

            do
            {
                change = false;
                foreach (var item in abouts)
                {
                    for (int i = 0; i < item.Value.Count; i++)
                    {
                        if (abouts.ContainsKey(item.Value[i]))
                        {
                            foreach (var c in abouts[item.Value[i]])
                            {
                                if (!item.Value.Contains(c))
                                {
                                    item.Value.Add(c);
                                    change = true;
                                }
                            }
                        }
                    }
                }
            } while (change);

            // 数据影响关联
            foreach (var item in abouts)
            {
                sb.AppendLine(sheetName + "FormaulaTemplate.relation[" + (item.Key.row * 1000 + item.Key.col) +
                              "] = []int32{" +
                              string.Join(",", item.Value.Select(c => { return(c.row * 1000 + c.col); })) + "}");
            }
            sb.AppendLine("}");

            // 创建
            sb.AppendLine("func New" + SheetName + "Formula() *" + SheetName + "FormulaSheet {");
            sb.AppendLine("formula:= new(" + SheetName + "FormulaSheet)");
            sb.AppendLine("formula.template = " + sheetName + "FormaulaTemplate");
            sb.AppendLine("formula.datas = make(map[int32]float64)");
            sb.AppendLine("return formula");
            sb.AppendLine("}");


            // 声明
            AppendGoDeclara(sheet, 0, true, sb);
            AppendGoDeclara(sheet, 3, false, sb);

            // 枚举器
            foreach (var item in FormulaEnumerator.GetList(sheet))
            {
                // 写结构
                sb.AppendLine("type " + item.fullName + " struct {");
                sb.AppendLine("sheet *" + SheetName + "FormulaSheet");
                sb.AppendLine("line int32");
                for (int i = 0; i < item.propertys.Count; i++)
                {
                    sb.AppendLine(item.propertys[i] + " float64 // " + item.notes[i]);
                }
                sb.AppendLine("}");

                // MoveNext
                sb.AppendLine("func (ins *" + item.fullName + ") MoveNext() bool {");
                sb.AppendLine("if ins.line <= 0 {");
                sb.AppendLine("ins.line = " + (item.start + 1) * 1000);
                sb.AppendLine("} else {");
                sb.AppendLine("ins.line = ins.line + " + item.div * 1000);
                sb.AppendLine("}");
                sb.AppendLine("if ins.line >= " + (item.end + 1) * 1000 + " {");
                sb.AppendLine("return false");
                sb.AppendLine("}");
                sb.AppendLine("");
                sb.AppendLine("if ins.sheet.get(ins.line+" + (6 + 1000 * (item.key - 1)) + ") == 0 {");
                sb.AppendLine("return ins.MoveNext()");
                sb.AppendLine("}");
                sb.AppendLine("");
                for (int i = 0; i < item.propertys.Count; i++)
                {
                    sb.AppendLine("ins." + item.propertys[i] + " = ins.sheet.get(ins.line+" + (6 + 1000 * i) + ")");
                }
                sb.AppendLine("return true");
                sb.AppendLine("}");
                sb.AppendLine("");

                // GetEnumerator
                sb.AppendLine("func (ins *" + SheetName + "FormulaSheet) Get" + item.name + "Enumerator() *" +
                              item.fullName + " {");
                sb.AppendLine("enumerator := &" + item.fullName + "{}");
                sb.AppendLine("enumerator.sheet = ins");
                sb.AppendLine("return enumerator");
                sb.AppendLine("}");
            }

            // 结果
            formulaContents.Add(SheetName, sb.ToString());
            return(string.Empty);
        }
Exemple #2
0
        /// <summary>
        /// 处理lua表格的formula
        /// </summary>
        public static string DealWithFormulaSheetLua(ISheet sheet)
        {
            CodeTemplate.curlang = CodeTemplate.Langue.Lua;

            StringBuilder sb = new StringBuilder();
            Dictionary <CellCoord, List <CellCoord> > abouts = new Dictionary <CellCoord, List <CellCoord> >();

            // 开头
            sb.AppendLine(CodeTemplate.Get("template_title")
                          .Replace("[SHEET_NAME]", sheet.SheetName.Substring(3))
                          .Replace("[USEFUL_ROW_COUNT]", (sheet.LastRowNum + 1).ToString())
                          );

            // 声明
            sb.AppendLine("\n-- declares");
            sb.AppendLine(CodeTemplate.Get("template_input").Replace("[CONTENT]", GetDeclaras(sheet, 0)));
            sb.AppendLine(CodeTemplate.Get("template_output").Replace("[CONTENT]", GetDeclaras(sheet, 3)));

            // 抓取所有数据
            sb.AppendLine("\n-- all datas");
            for (int rownum = 0; rownum <= sheet.LastRowNum; rownum++)
            {
                IRow row = sheet.GetRow(rownum);
                if (row == null)
                {
                    continue;
                }

                for (int i = 0; i < row.Cells.Count; i++)
                {
                    ICell cell   = row.Cells[i];
                    int   colnum = cell.ColumnIndex;

                    // in、out声明忽略
                    if (colnum == 0 || colnum == 1 || colnum == 3 || colnum == 4)
                    {
                        continue;
                    }

                    if (cell.CellType == CellType.Boolean || cell.CellType == CellType.Numeric)
                    {
                        sb.AppendLine(CodeTemplate.Get("template_value")
                                      .Replace("[ROW]", (rownum + 1).ToString())
                                      .Replace("[COL]", (colnum + 1).ToString())
                                      .Replace("[VALUE]", cell.CellType == CellType.Boolean ? cell.BooleanCellValue.ToString().ToLower() : cell.NumericCellValue.ToString())
                                      );
                    }
                    else if (cell.CellType == CellType.Formula)
                    {
                        List <CellCoord> about;
                        sb.AppendLine(CodeTemplate.Get("template_func")
                                      .Replace("[ROW]", (rownum + 1).ToString())
                                      .Replace("[COL]", (colnum + 1).ToString())
                                      .Replace("[CONTENT]", Formula2Code.Translate(sheet, cell.CellFormula, cell.ToString(), out about))
                                      );

                        CellCoord cur = new CellCoord(rownum + 1, colnum + 1);
                        foreach (CellCoord cc in about)
                        {
                            if (!abouts.ContainsKey(cc))
                            {
                                abouts.Add(cc, new List <CellCoord>());
                            }
                            if (!abouts[cc].Contains(cur))
                            {
                                abouts[cc].Add(cur);
                            }
                        }
                    }
                }
            }

            // 数据影响关联递归统计
            bool change;

            do
            {
                change = false;
                foreach (var item in abouts)
                {
                    for (int i = 0; i < item.Value.Count; i++)
                    {
                        if (abouts.ContainsKey(item.Value[i]))
                        {
                            foreach (var c in abouts[item.Value[i]])
                            {
                                if (!item.Value.Contains(c))
                                {
                                    item.Value.Add(c);
                                    change = true;
                                }
                            }
                        }
                    }
                }
            } while (change);

            // 数据影响关联
            sb.AppendLine("\n-- cell data relation");
            foreach (var item in abouts)
            {
                sb.AppendLine(CodeTemplate.Get("template_about")
                              .Replace("[ROW]", item.Key.row.ToString())
                              .Replace("[COL]", item.Key.col.ToString())
                              .Replace("[CONTENT]", string.Join(",", item.Value.Select(CellCoord.ToString)))
                              );
            }

            // 枚举器
            sb.AppendLine("\n-- enumerator");
            sb.AppendLine("sheet.enumerator = {}");
            foreach (var item in FormulaEnumerator.GetList(sheet))
            {
                sb.AppendLine("sheet.enumerator." + item.name + " = {}");
                sb.AppendLine("sheet.enumerator." + item.name + ".start = " + (item.start + 1));
                sb.AppendLine("sheet.enumerator." + item.name + ".over = " + (item.end + 1));
                sb.AppendLine("sheet.enumerator." + item.name + ".div = " + item.div);
                sb.AppendLine("sheet.enumerator." + item.name + ".key = " + (item.key - 1));
                sb.AppendLine("sheet.enumerator." + item.name + ".propertys = {");
                for (int i = 0; i < item.propertys.Count; i++)
                {
                    sb.AppendLine("\"" + item.propertys[i] + "\", --" + item.notes[i]);
                }
                sb.AppendLine("}");
            }

            // 结果
            formulaContents.Add(sheet.SheetName.Substring(3), sb.ToString());
            return(string.Empty);
        }
Exemple #3
0
        public static List <FormulaEnumerator> GetList(ISheet sheet)
        {
            Dictionary <string, FormulaEnumerator> dict = new Dictionary <string, FormulaEnumerator>();

            for (int i = 0; i <= sheet.LastRowNum; i++)
            {
                try
                {
                    IRow row = sheet.GetRow(i);
                    if (row == null)
                    {
                        continue;
                    }
                    ICell cell = row.GetCell(3, MissingCellPolicy.RETURN_NULL_AND_BLANK);
                    if (cell == null || cell.CellType != CellType.String)
                    {
                        continue;
                    }
                    string[] arr = cell.StringCellValue.Split('_');

                    if (arr[0] == "LS" && arr.Length == 4)
                    {
                        var item = new FormulaEnumerator();
                        if (int.TryParse(arr[1], out item.div) && int.TryParse(arr[2], out item.key))
                        {
                            dict.Add(arr[3], item);
                            item.start    = i + 1;
                            item.name     = arr[3].Substring(0, 1).ToUpper() + arr[3].Substring(1);
                            item.fullName = sheet.SheetName.Substring(3).ToLower() + "_" + arr[3];

                            item.propertys = new List <string>();
                            item.notes     = new List <string>();
                            for (int j = i + 1; j < i + 1 + item.div; j++)
                            {
                                string str = sheet.GetRow(j).GetCell(4).StringCellValue;
                                if (str.StartsWith("_"))
                                {
                                    str = str.Substring(1);
                                }
                                item.propertys.Add(str.Substring(0, 1).ToUpper() + str.Substring(1, str.Length - 2));
                                str = sheet.GetRow(j).GetCell(3).StringCellValue;
                                if (str.EndsWith("1"))
                                {
                                    str = str.Substring(0, str.Length - 1);
                                }
                                item.notes.Add(str);
                            }
                        }
                    }

                    if (arr[0] == "LE" && arr.Length == 2 && dict.ContainsKey(arr[1]))
                    {
                        dict[arr[1]].end = i;
                    }
                }
                catch
                {
                    throw new Exception("公式表获取枚举时出错," + sheet.SheetName + " 第" + i + "行");
                }
            }

            return(new List <FormulaEnumerator>(dict.Values));
        }
Exemple #4
0
        public static string DealWithFormulaSheetCS(ISheet sheet)
        {
            CodeTemplate.curlang = CodeTemplate.Langue.CS;

            StringBuilder sb = new StringBuilder();
            Dictionary <CellCoord, List <CellCoord> > abouts = new Dictionary <CellCoord, List <CellCoord> >();
            string sheetName = sheet.SheetName.Substring(3);
            string SheetName = sheetName.Substring(0, 1).ToUpper() + sheetName.Substring(1);

            string className = SheetName + "FormulaSheet";

            sb.Append("using System;\r\n");
            sb.Append("using UnityEngine;\r\n");
            sb.Append("using System.Collections;\r\n");
            sb.Append("using System.Collections.Generic;\r\n");
            sb.Append("\r\n");

            // 扩展Config类统一获取某个表的实例对象
            sb.Append("public partial class Config {\r\n");

            sb.Append("\tpublic static " + className + " New" + className + "(){\r\n");
            sb.Append("\t\tvar formula = new " + className + "();\r\n");
            sb.Append("\t\tformula.Init();\r\n");
            sb.Append("\t\treturn formula;\r\n");
            sb.Append("\t}\r\n");
            sb.Append("}\r\n");

            //------

            // 开始生成这个配置表的算法类
            sb.Append("public class " + className + " : FormulaSheet { //定义数据表类开始\r\n");
            sb.Append("public void Init(){\r\n");

            // 数据内容
            for (int rownum = 0; rownum <= sheet.LastRowNum; rownum++)
            {
                IRow row = sheet.GetRow(rownum);
                if (row == null)
                {
                    continue;
                }

                for (int i = 0; i < row.Cells.Count; i++)
                {
                    ICell cell   = row.Cells[i];
                    int   colnum = cell.ColumnIndex;

                    // in、out声明忽略
                    if (colnum == 0 || colnum == 1 || colnum == 3 || colnum == 4)
                    {
                        continue;
                    }

                    if (cell.CellType == CellType.Boolean || cell.CellType == CellType.Numeric)
                    {
                        sb.Append("this.datas[" + ((rownum + 1) * 1000 + colnum + 1) + "] = " + (cell.CellType == CellType.Boolean ? (cell.BooleanCellValue ? 1 : 0).ToString() : cell.NumericCellValue.ToString()) + "f;\r\n");
                    }
                    else if (cell.CellType == CellType.Formula)
                    {
                        List <CellCoord> about;
                        sb.Append("this.funcs[" + ((rownum + 1) * 1000 + colnum + 1) + "] = ins => {\r\n");

                        string content = Formula2Code.Translate(sheet, cell.CellFormula, cell.ToString(), out about);
                        if (CodeTemplate.curlang == CodeTemplate.Langue.CS)
                        {
                            content = FixFloat(content);
                        }

                        sb.Append("\treturn (float)" + content + ";\r\n");
                        sb.Append("};\r\n");

                        CellCoord cur = new CellCoord(rownum + 1, colnum + 1);
                        foreach (CellCoord cc in about)
                        {
                            if (!abouts.ContainsKey(cc))
                            {
                                abouts.Add(cc, new List <CellCoord>());
                            }
                            if (!abouts[cc].Contains(cur))
                            {
                                abouts[cc].Add(cur);
                            }
                        }
                    }
                }
            }
            // 数据影响关联递归统计
            bool change;

            do
            {
                change = false;
                foreach (var item in abouts)
                {
                    for (int i = 0; i < item.Value.Count; i++)
                    {
                        if (abouts.ContainsKey(item.Value[i]))
                        {
                            foreach (var c in abouts[item.Value[i]])
                            {
                                if (!item.Value.Contains(c))
                                {
                                    item.Value.Add(c);
                                    change = true;
                                }
                            }
                        }
                    }
                }
            } while (change);

            // 数据影响关联
            foreach (var item in abouts)
            {
                sb.Append("this.relation[" + (item.Key.row * 1000 + item.Key.col) + "] = new int[]{" + string.Join(",", item.Value.Select(c => { return(c.row * 1000 + c.col); })) + "};\r\n");
            }
            sb.Append("} // 初始化数据结束\r\n");


            // 声明
            AppendCSDeclara(sheet, 0, true, sb);
            AppendCSDeclara(sheet, 3, false, sb);

            // 枚举器
            foreach (var item in FormulaEnumerator.GetList(sheet))
            {
                // 写结构
                sb.Append("public struct " + item.fullName + " {\r\n");

                // 属性
                sb.Append("\tpublic " + className + " sheet;\r\n");
                sb.Append("\t int line;\r\n");
                for (int i = 0; i < item.propertys.Count; i++)
                {
                    sb.Append("\tfloat " + item.propertys[i] + "; // " + item.notes[i] + "\r\n");
                }

                // 枚举方法
                sb.Append("public bool MoveNext() {\r\n");
                // MoveNext
                sb.Append("\tif (line <= 0) {\r\n");
                sb.Append("\t\tline = " + (item.start + 1) * 1000 + ";\r\n");
                sb.Append("\t} else {\r\n");
                sb.Append("\t\tline = line + " + item.div * 1000 + ";\r\n");
                sb.Append("}\r\n");
                sb.Append("\tif (line >= " + (item.end + 1) * 1000 + ") {\r\n");
                sb.Append("\t\treturn false;\r\n");
                sb.Append("}\r\n");
                sb.Append("\tif (sheet.get(line+" + (6 + 1000 * (item.key - 1)) + ") == 0 ) {\r\n");
                sb.Append("\t\treturn MoveNext();\r\n");
                sb.Append("}\r\n");
                for (int i = 0; i < item.propertys.Count; i++)
                {
                    sb.Append("" + item.propertys[i] + " = sheet.get(line+" + (6 + 1000 * i) + ");\r\n");
                }
                sb.Append("\treturn true;\r\n");
                sb.Append("} // 枚举方法next结束\r\n");
                sb.Append("} // 枚举struct定义结束\r\n");

                // GetEnumerator
                sb.Append("public " + item.fullName + " Get" + item.name + "Enumerator(){\r\n");
                sb.Append("\tvar enumerator = new " + item.fullName + "();\r\n");
                sb.Append("\t\tenumerator.sheet = this;\r\n");
                sb.Append("\t\treturn enumerator;\r\n");
                sb.Append("}\r\n");
            }

            sb.Append("}\r\n");

            // 结果
            formulaContents.Add(SheetName, sb.ToString());
            return(string.Empty);
        }