Beispiel #1
0
        private static void WriteClassDefineCode(string targetNameSpace, FbsStructure fbsStruct,
                                                 string fbsFileNameWithoutExtension)
        {
            formatWriter.Clear();

            //write using
            formatWriter.WriteLine("using System.Collections.Generic;");
            formatWriter.WriteLine("using FlatBuffers;");
            formatWriter.NewLine();

            //write namespace
            if (string.IsNullOrEmpty(fbsStruct.namespaceName))
            {
                formatWriter.WriteLine($"namespace {targetNameSpace}");
            }
            else
            {
                formatWriter.WriteLine($"namespace {targetNameSpace}.{fbsStruct.namespaceName}");
            }

            formatWriter.BeginBlock();
            //write classes
            for (int j = 0; j < fbsStruct.tableStructures.Length; j++)
            {
                WriteClassCode(targetNameSpace, fbsStruct.tableStructures[j], fbsStruct);
                formatWriter.NewLine();
            }

            formatWriter.EndBlock();

            //save generate csharp file
            string csharpFilePath = $"{AppData.CsOutputDirectory}/{fbsFileNameWithoutExtension}.cs";

            File.WriteAllText(csharpFilePath, formatWriter.ToString());
        }
Beispiel #2
0
        private static void WriteFlatBuffersFacilityCode(string targetNameSpace, List <FbsStructure> fbsStructList)
        {
            formatWriter.Clear();

            //write using
            formatWriter.WriteLine("using System.Collections.Generic;");
            formatWriter.WriteLine("using FlatBuffers;");
            formatWriter.WriteLine($"using {targetNameSpace};");
            for (int i = 0; i < fbsStructList.Count; i++)
            {
                if (string.IsNullOrEmpty(fbsStructList[i].namespaceName))
                {
                    continue;
                }
                formatWriter.WriteLine($"using {targetNameSpace}.{fbsStructList[i].namespaceName};");
            }

            formatWriter.NewLine();

            //write namespace
            formatWriter.WriteLine($"namespace {targetNameSpace}");
            formatWriter.BeginBlock();

            //write class
            formatWriter.WriteLine($"public static class {targetNameSpace}ConvertMethods");
            formatWriter.BeginBlock();

            //所有fbs文件生成一个文件
            for (int i = 0; i < fbsStructList.Count; i++)
            {
                FbsStructure fbsStructure = fbsStructList[i];
                for (int j = 0; j < fbsStructure.tableStructures.Length; j++)
                {
                    TableStructure tableStructure = fbsStructure.tableStructures[j];
                    WriteTableEncodeCode(tableStructure);
                    WriteTableDecodeCode(tableStructure);
                    formatWriter.NewLine();
                }
            }

            formatWriter.EndBlock();
            formatWriter.EndBlock();

            string csharpFilePath = $"{AppData.CsOutputDirectory}/{targetNameSpace}ConvertMethods.cs";

            File.WriteAllText(csharpFilePath, formatWriter.ToString());
        }
Beispiel #3
0
        private static void GenerateCustomCSharpFiles(string targetNameSpace, string[] selectFbsFileNames)
        {
            string filesPath = AppData.FbsDirectory;
            List <FbsStructure> allFbsStructureList = new List <FbsStructure>();

            //每一个fbs文件生成一份cs代码
            for (int i = 0; i < selectFbsFileNames.Length; i++)
            {
                string fbsFilePath = $"{filesPath}\\{selectFbsFileNames[i]}";
                string fbsFileNameWithoutExtension = Path.GetFileNameWithoutExtension(fbsFilePath);
//                Debug.WriteLine(filePath);

                if (!File.Exists(fbsFilePath))
                {
                    throw new ParseFileException {
                              errorMessage = $"无法解析文件 {fbsFilePath}. 该文件不存在"
                    };
                }

                string[]     allLines  = File.ReadAllLines(fbsFilePath);
                FbsStructure fbsStruct = fbsParser.ParseFbsFileLines(allLines);
                if (fbsStruct.tableStructures.Length == 0)
                {
                    throw new GenerateCodeException {
                              errorMessage = $"文件 {fbsFilePath} 中没有读取到有效的table"
                    };
                }

                if (fbsStruct.namespaceName == targetNameSpace)
                {
                    throw new GenerateCodeException
                          {
                              errorMessage = $"文件 {fbsFilePath} 设定的namespace名称和工具输出的C#代码namespace相同了,确保命名空间不要冲突。然后删除刚才生成的文件,再试一次。"
                          };
                }

                WriteClassDefineCode(targetNameSpace, fbsStruct, fbsFileNameWithoutExtension);
                allFbsStructureList.Add(fbsStruct);
            }

            WriteFlatBuffersFacilityCode(targetNameSpace, allFbsStructureList);
        }
Beispiel #4
0
        private static void WriteClassCode(string targetNamespace, TableStructure tableStructure, FbsStructure fbsStructure)
        {
            if (generatePoolVersion)
            {
                formatWriter.WriteLine($"public class {tableStructure.tableName} : FlatBuffersFacility.PoolObject");
            }
            else
            {
                formatWriter.WriteLine($"public class {tableStructure.tableName}");
            }

            formatWriter.BeginBlock();

            for (int i = 0; i < tableStructure.fieldInfos.Length; i++)
            {
                TableFieldInfo structFieldInfo = tableStructure.fieldInfos[i];
                WriteFieldCode(structFieldInfo);
            }

            WriteClassEncodeAndDecode(tableStructure, targetNamespace);
            if (generatePoolVersion)
            {
                formatWriter.NewLine();
                WriteClassReleaseCode(tableStructure);
            }

            formatWriter.EndBlock();
        }