//写入table的field public static void GenFbsSchemaField(FbsTableField field, ref StringBuilder stringBuilder) { string typestring = field.fieldType == FbsFieldType.SCALAR ? GetFbsDataTypeString(field.dataType, field.isArray) : field.isArray ? string.Format("[{0}]", field.fieldTypeName) : field.fieldTypeName; string defaultvalue = string.Empty; if (field.defaultValue != string.Empty && !field.isArray && field.fieldType != FbsFieldType.UNION) { defaultvalue = " = " + field.defaultValue; } string attributeString = string.Empty; GenTableFieldAttributes(field, ref attributeString); stringBuilder.AppendFormat(" {0}:{1}{2}{3};\n", field.fieldName, typestring, defaultvalue, attributeString); }
//生成table 字段属性文本 public static void GenTableFieldAttributes(FbsTableField field, ref string attributeString) { if (field.attributes != null && field.attributes.Count > 0) { attributeString = "("; for (int i = 0; i < field.attributes.Count; i++) { FbsAttribute fieldAttribute = field.attributes[i]; if (fieldAttribute.attributeType == FbsAttributeType.ID && fieldAttribute.attributeValue != string.Empty) { attributeString += string.Format("id:{0},", fieldAttribute.attributeValue); } else if (fieldAttribute.attributeType == FbsAttributeType.DEPRECATED) { attributeString += "deprecated,"; } else if (fieldAttribute.attributeType == FbsAttributeType.REQUIRED && field.fieldType != FbsFieldType.SCALAR) { attributeString += "required,"; } else if (fieldAttribute.attributeType == FbsAttributeType.NESTED_FLATBUFFER && fieldAttribute.attributeValue != string.Empty) { attributeString += string.Format("nested_flatbuffer: \"{0}\",", fieldAttribute.attributeValue); } else if (fieldAttribute.attributeType == FbsAttributeType.FLEXBUFFER && field.dataType == FbsDataType.UBYTE && field.isArray) { attributeString += "flexbuffer,"; } else if (fieldAttribute.attributeType == FbsAttributeType.KEY) { attributeString += "key,"; } else if (fieldAttribute.attributeType == FbsAttributeType.HASH && (field.dataType == FbsDataType.UINT || field.dataType == FbsDataType.ULONG)) { attributeString += "hash,"; } else if (fieldAttribute.attributeType == FbsAttributeType.CUSTOM) { attributeString += fieldAttribute.attributeValue == string.Empty ? string.Format("{0},", fieldAttribute.customName) : string.Format("{0}:{1},", fieldAttribute.customName, fieldAttribute.attributeValue); } } attributeString = attributeString.Substring(0, attributeString.Length - 1); attributeString += ")"; } }
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); }
public static void GenFbsTable(FbsTable fbsTable, ref StringBuilder stringBuilder) { //表结构 if (fbsTable.isOriginalOrder) { stringBuilder.AppendFormat("table {0} (original_order) {1}\n", fbsTable.tableName, "{"); } else { stringBuilder.AppendFormat("table {0} {1}\n", fbsTable.tableName, "{"); } if (fbsTable.fields != null && fbsTable.fields.Count > 0) { int count = fbsTable.fields.Count; for (int i = 0; i < count; i++) { FbsTableField field = fbsTable.fields[i]; GenFbsSchemaField(field, ref stringBuilder); } } stringBuilder.Append("}\n\n"); }
//生成给定类型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(); }
public static string GetTableFieldTypeString(FbsTableField field) { return(field.fieldType == FbsFieldType.SCALAR ? GetFbsDataTypeString(field.dataType, field.isArray) : field.isArray?string.Format("[{0}]", field.fieldTypeName) : field.fieldTypeName); }