public static void BuildAllFromExcel(string flatcPath, string excelFilePath, string outSchemaPath, string outJsonPath, string outClassPath, string outBinPath) { List <ExcelSheetData> excelSheetDatas = new List <ExcelSheetData>(); ReadExcel(excelFilePath, ref excelSheetDatas); foreach (var sheet in excelSheetDatas) { FbsFile fbsFile = GenFbsFileObject(sheet); string schemaPath = string.Empty; string jsonPath = string.Empty; TableFileGenerater.GenFbsSchemaFile(fbsFile, outSchemaPath, ref schemaPath); TableFileGenerater.GenJsonFile(fbsFile, sheet.fieldValues, outJsonPath, ref jsonPath); if (!Directory.Exists(outClassPath)) { Directory.CreateDirectory(outClassPath); } if (!Directory.Exists(outBinPath)) { Directory.CreateDirectory(outBinPath); } CmdHelper.RunFlatC(flatcPath, schemaPath, jsonPath, outClassPath, outBinPath); } }
public static FbsTable GetFbsRootTableDataTypeObj(FbsFile fbsfile) { FbsTable fbsTable = null; foreach (var table in fbsfile.tables) { if (table.tableName == fbsfile.fileName) { fbsTable = table; break; } } return(fbsTable); }
public static void BuildSheet(ExcelSheetData sheet, string flatcPath, string outSchemaPath, string outJsonPath, string outClassPath, string outBinPath) { FbsFile fbsFile = GenFbsFileObject(sheet); string schemaPath = string.Empty; string jsonPath = string.Empty; TableFileGenerater.GenFbsSchemaFile(fbsFile, outSchemaPath, ref schemaPath); TableFileGenerater.GenJsonFile(fbsFile, sheet.fieldValues, outJsonPath, ref jsonPath); if (!Directory.Exists(outClassPath)) { Directory.CreateDirectory(outClassPath); } if (!Directory.Exists(outBinPath)) { Directory.CreateDirectory(outBinPath); } CmdHelper.RunFlatC(flatcPath, schemaPath, jsonPath, outClassPath, outBinPath); }
public static FbsFile GenFbsFileObject(ExcelSheetData excelSheetData) { //一个sheet 一个table FbsFile fbsFile = new FbsFile { fileName = excelSheetData.sheetName.Replace("=", "") }; FbsTable fbsTable = new FbsTable { tableName = fbsFile.fileName }; //处理table字段 for (int j = 0; j < excelSheetData.fieldNames.Count; j++) { FbsTableField fbsTableField = new FbsTableField(); fbsTableField.fieldName = excelSheetData.fieldNames[j]; string dataTypeStr = excelSheetData.fieldTypes[j]; TableFileGenerater.GetFbsDataTypeByString(dataTypeStr, ref fbsTableField.fieldType, ref fbsTableField.fieldTypeName, ref fbsTableField.dataType, ref fbsTableField.isArray); fbsTable.fields.Add(fbsTableField); } fbsFile.tables.Add(fbsTable); //添加root_type table FbsTable fbsRootTable = new FbsTable(); fbsRootTable.tableName = "Root_" + fbsFile.fileName; FbsTableField dataField = new FbsTableField(); dataField.fieldName = "data"; dataField.fieldType = FbsFieldType.TABLE; dataField.isArray = true; dataField.fieldTypeName = fbsTable.tableName; fbsRootTable.fields.Add(dataField); fbsFile.tables.Add(fbsRootTable); //root_type fbsFile.root_type = fbsRootTable.tableName; fbsFile.namespaceName = "GameDataTables"; return(fbsFile); }
//生成给定类型table的数组组成的json public static void GenJsonFile(FbsFile fbsfile, List <string> fieldValues, string fileOutDir, ref string jsonPath) { //生成json文件 StringBuilder jsonBuilder = StrBuilder; jsonBuilder.Clear(); jsonBuilder.Append("{\n \"data\":[\n"); FbsTable fbsTable = GetFbsRootTableDataTypeObj(fbsfile); if (fbsTable == null) { Debug.LogErrorFormat("can not find table named ", fbsfile.fileName); return; } int fieldCount = fbsTable.fields.Count; int dataCount = Mathf.FloorToInt(fieldValues.Count / fieldCount); for (int k = 0; k < fieldValues.Count; k++) { string value = fieldValues[k]; int fieldIndex = k % fieldCount; int dataIndex = Mathf.FloorToInt(k / fieldCount); FbsTableField tableField = fbsTable.fields[fieldIndex]; string fieldName = tableField.fieldName; if (fieldIndex == 0) { jsonBuilder.Append(" {\n"); } bool isstring = !(tableField.fieldType == FbsFieldType.SCALAR && tableField.dataType != FbsDataType.STRING); string valuecolon = isstring && !tableField.isArray ? "\"" : ""; string valuearrayL = tableField.isArray ? "[" : ""; string valuearrayR = tableField.isArray ? "]" : ""; string jsonValue = value; if (isstring && tableField.isArray) { string[] condition = { "[/]" }; string[] vs = value.Split(condition, StringSplitOptions.RemoveEmptyEntries); jsonValue = ""; for (int m = 0; m < vs.Length; m++) { jsonValue += "\"" + vs[m] + "\","; } jsonValue = jsonValue.Substring(0, jsonValue.Length - 1); } string lastCommon = (fieldIndex == (fieldCount - 1)) ? "" : ","; jsonBuilder.AppendFormat(" \"{0}\":{1}{2}{3}{4}{5}{6}\n", fieldName, valuearrayL, valuecolon, jsonValue, valuecolon, valuearrayR, lastCommon); if (fieldIndex == fieldCount - 1) { if (dataIndex == dataCount - 1) { jsonBuilder.Append(" }\n"); } else { jsonBuilder.Append(" },\n"); } } } jsonBuilder.Append(" ]\n}"); jsonPath = fileOutDir + "/" + fbsTable.tableName + ".json"; if (!Directory.Exists(fileOutDir)) { Directory.CreateDirectory(fileOutDir); } if (File.Exists(jsonPath)) { File.Delete(jsonPath); } FileStream fs = new FileStream(jsonPath, FileMode.Create); StreamWriter sw = new StreamWriter(fs, Encoding.UTF8); sw.Write(jsonBuilder.ToString()); sw.Close(); fs.Close(); jsonBuilder.Clear(); }
//flatbuff schema文件生成 public static void GenFbsSchemaFile(FbsFile fbsFile, string fileOutDir, ref string fileFullpath) { //---------生成FBS文件--------- StringBuilder stringBuilder = StrBuilder; stringBuilder.Clear(); //spacename if (fbsFile.namespaceName != string.Empty) { stringBuilder.AppendFormat("namespace {0};\n\n", fbsFile.namespaceName); } //customAttribute if (fbsFile.customAttributes != null && fbsFile.customAttributes.Count > 0) { for (int i = 0; i < fbsFile.customAttributes.Count; i++) { string field = fbsFile.customAttributes[i]; stringBuilder.AppendFormat("attribute \"{0}\";\n", field); } } //Enums if (fbsFile.enums != null && fbsFile.enums.Count > 0) { for (int i = 0; i < fbsFile.enums.Count; i++) { FbsEnum fbsenum = fbsFile.enums[i]; GenFbsEnum(fbsenum, ref stringBuilder); } } //Structs if (fbsFile.structs != null && fbsFile.structs.Count > 0) { for (int i = 0; i < fbsFile.structs.Count; i++) { FbsStruct fbsStruct = fbsFile.structs[i]; GenFbsStruct(fbsStruct, ref stringBuilder); } } //Unions if (fbsFile.unions != null && fbsFile.unions.Count > 0) { for (int i = 0; i < fbsFile.unions.Count; i++) { FbsUnion fbsUnion = fbsFile.unions[i]; GenFbsUnion(fbsUnion, ref stringBuilder); } } //tables if (fbsFile.tables != null && fbsFile.tables.Count > 0) { for (int i = 0; i < fbsFile.tables.Count; i++) { FbsTable fbstable = fbsFile.tables[i]; GenFbsTable(fbstable, ref stringBuilder); } } //根类型 if (fbsFile.root_type != string.Empty) { stringBuilder.AppendFormat("root_type {0};\n", fbsFile.root_type); } else { Debug.LogError(fbsFile.fileName + " root_type is Empty!!!!"); } // 文件标识 if (fbsFile.file_identifier != string.Empty) { stringBuilder.AppendFormat("file_identifier \"{0}\";\n", fbsFile.file_identifier); } fileFullpath = fileOutDir + "/" + fbsFile.fileName + ".fbs"; if (!Directory.Exists(fileOutDir)) { Directory.CreateDirectory(fileOutDir); } if (File.Exists(fileFullpath)) { File.Delete(fileFullpath); } FileStream fs = new FileStream(fileFullpath, FileMode.Create); StreamWriter sw = new StreamWriter(fs, Encoding.UTF8); sw.Write(stringBuilder.ToString()); sw.Close(); fs.Close(); stringBuilder.Clear(); }