Example #1
0
        public bool Proceduree_GenerateFbs(ExcelData excelData)
        {
            List <string> dataList0 = excelData.GetRowData(0);   //字段
            List <string> dataList1 = excelData.GetRowData(1);   //类型
            List <string> dataList2 = excelData.GetRowData(2);   //说明

            if (dataList0 == null)
            {
                Debug.LogError("获取" + excelData.FileName + "第0行失败!");
                return(false);
            }
            if (dataList1 == null)
            {
                Debug.LogError("获取" + excelData.FileName + "第1行失败!");
                return(false);
            }
            if (dataList2 == null)
            {
                Debug.LogError("获取" + excelData.FileName + "第2行失败!");
                return(false);
            }

            int    count              = dataList0.Count;
            string generateScript     = "";
            string lastArrayWorldName = "";

            for (int i = 0; i < count; i++)
            {
                string cell_world = dataList0[i];
                string cell_type  = dataList1[i];
                string cell_des   = "该字段无备注";
                if (i < dataList2.Count)
                {
                    cell_des = dataList2[i];
                }

                if (i == 0)
                {
                    if (cell_world.StartsWith("#"))
                    {
                        cell_world = cell_world.Replace("#", "");
                    }
                    if (cell_type.StartsWith("#"))
                    {
                        cell_type = cell_type.Replace("#", "");
                    }
                    if (cell_des.StartsWith("#"))
                    {
                        cell_des = cell_des.Replace("#", "");
                    }
                }

                bool needAdd = true;
                if (cell_world.Contains(":"))
                {
                    string[] temp = cell_world.Split(':');
                    if (temp.Length > 0)
                    {
                        needAdd = string.IsNullOrEmpty(lastArrayWorldName) || temp[0] != lastArrayWorldName;
                        if (needAdd)
                        {
                            lastArrayWorldName = temp[0];
                            cell_type          = string.Format("[{0}]", cell_type);
                            cell_world         = lastArrayWorldName;
                        }
                    }
                }

                if (needAdd)
                {
                    generateScript = generateScript + string.Format(Template.Template_FbsLine, cell_des, cell_world, cell_type.ToLower());
                }
            }

            string fbs = string.Format(Template.Template_Fbs, excelData.Name, excelData.Name, excelData.Name, generateScript, excelData.Name);

            if (Tools.GenerateFile(fbs, excelData.FbsPath))
            {
                Debug.Log("[Generate]=> " + excelData.FbsPath + " Sucess!");

                AddDelayRun(() =>
                {
                    Procedure_GenerateFlatBufferCode(excelData);
                });
            }

            return(true);
        }
        public override bool DealRow(ExcelData excelData, int rowIndex)
        {
            if (rowIndex > 2)
            {
                string        JsonLine                 = "{"; //一行的json数据
                List <string> rowList                  = excelData.GetRowData(rowIndex);
                int           rowListCount             = rowList.Count;
                int           arrayMark                = 0;
                List <TableArrayElementInfo> arrayList = new List <TableArrayElementInfo>();
                for (int i = 0; i < rowListCount; i++)
                {
                    string wordName = excelData.GetWordName(i);
                    string wordType = excelData.GetTypeName(i);
                    string cellStr  = rowList[i];

                    //string wordArrayName = null;
                    if (wordName.Contains(":"))
                    {
                        wordName = wordName.Split(':')[0];
                        if (arrayMark == 0)
                        {
                            arrayMark = 1;
                            arrayList.Clear();
                        }
                    }

                    if (i + 1 < rowListCount)                                   //预判下一个值(如果不是最后一个)
                    {
                        string nextCellWordName = excelData.GetWordName(i + 1); //字段名称
                        if (nextCellWordName.Contains(":"))
                        {
                            string nextWordArrayName = nextCellWordName.Split(':')[0];
                            if (!nextWordArrayName.Equals(wordName)) //如果下一个单元格和当前单元格不是一个数组里的
                            {
                                if (arrayMark == 1)
                                {
                                    arrayMark = 2;
                                }
                            }
                        }
                        else
                        {
                            if (arrayMark == 1)
                            {
                                arrayMark = 2;
                            }
                        }
                    }
                    else
                    {
                        //如果已经是最后一个了
                        if (arrayMark == 1)
                        {
                            arrayMark = 2;
                        }
                    }

                    if (arrayMark == 0)  //普通单元格
                    {
                        cellStr = GetSingleCellString(wordType, cellStr);
                        string jsonCellStr = string.Format(Template_Server_Java_Json.Template_JsonCell, wordName, cellStr);
                        JsonLine += jsonCellStr;
                        if (i + 1 < rowListCount)
                        {
                            JsonLine += ",";
                        }
                    }
                    else if (arrayMark == 1) //同一数组添加
                    {
                        arrayList.Add(new TableArrayElementInfo(wordName, wordType, cellStr, excelData.Name));
                    }
                    else if (arrayMark == 2) //整个数组解析成代码
                    {
                        arrayList.Add(new TableArrayElementInfo(wordName, wordType, cellStr, excelData.Name));
                        string cellParam   = GetCellArrayString(arrayList);
                        string jsonCellStr = string.Format(Template_Server_Java_Json.Template_JsonCell, wordName, cellParam);

                        JsonLine = JsonLine + jsonCellStr;
                        if (i + 1 < rowListCount)
                        {
                            JsonLine += ",";
                        }
                        arrayMark = 0;
                    }
                }


                //Console.WriteLine(excelData.GetAllRowCount().ToString() + "   " + (rowIndex + 1).ToString());
                if (excelData.GetAllRowCount() == rowIndex + 1)
                {
                    JsonLine       += "}";
                    jsonContentStr += JsonLine;
                }
                else
                {
                    JsonLine       += "},";
                    jsonContentStr += JsonLine + "\n";
                }
            }
            return(true);
        }
        public override void Execute(ExcelData excelData, System.Action <ExcelData, bool> onFinished)
        {
            string        singleCode1  = "";
            string        singleCode2  = "";
            List <string> rowList      = excelData.GetRowData(0);
            List <string> desList      = excelData.GetRowData(2);
            string        lastWordName = "";

            for (int i = 0; i < rowList.Count; i++)
            {
                string typeName = excelData.GetTypeName(i);
                string wordName = excelData.GetWordName(i);
                string desStr   = desList[i];
                if (string.IsNullOrEmpty(desStr))
                {
                    desStr = "该字段未添加注释";
                }

                bool isArray = false;
                if (wordName.Contains(":"))
                {
                    wordName = wordName.Split(':')[0];
                    isArray  = true;
                }
                if (!lastWordName.Equals(wordName))
                {
                    if (typeName == "string")
                    {
                        typeName = "String";
                    }
                    if (typeName == "bool")
                    {
                        typeName = "boolean";
                    }
                    if (isArray)
                    {
                        if (typeName == "int")
                        {
                            typeName = "List<Integer>";
                        }
                        else
                        {
                            typeName = string.Format(@"List<{0}>", typeName);
                        }
                    }
                    singleCode1 += string.Format(Template_Server_Java_Json.Template_SingleTableCodeWord, wordName, typeName, wordName);
                    singleCode2 += string.Format(Template_Server_Java_Json.Template_SingleTableCodeWordGet, desStr, typeName, wordName, wordName);
                    lastWordName = wordName;
                }
            }
            string codeFileContent = string.Format(Template_Server_Java_Json.Template_SingleTableCode, excelData.Name, singleCode1, singleCode2);
            string JsonFilePath    = System.IO.Path.Combine(Setting.GenJavaServerJsonCodePath, "Table" + excelData.Name + ".java");

            if (Tools.GenerateFile(codeFileContent, JsonFilePath))
            {
                Debug.Log("[Generate Sucess]:" + JsonFilePath);
                if (onFinished != null)
                {
                    onFinished(excelData, true);
                }
            }

            if (onFinished != null)
            {
                onFinished(excelData, true);
            }
        }