public static void GenFbsUnion(FbsUnion fbsUnion, ref StringBuilder stringBuilder) { stringBuilder.AppendFormat("union {0} {1}\n", fbsUnion.unionName, "{"); if (fbsUnion.unionTables != null && (fbsUnion.unionTables.Count > 0)) { int Count = fbsUnion.unionTables.Count; for (int i = 0; i < Count; i++) { string field = fbsUnion.unionTables[i]; stringBuilder.AppendFormat(" {0},\n", field); } } // stringBuilder.Remove(stringBuilder.Length - 1, 1); stringBuilder.Append("}\n\n"); }
//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(); }