Пример #1
0
        private void ProcessTableAndFieldNamespace(FbsStructure fbsStructure)
        {
            string targetNamespace = AppData.TargetNamespace;
            string namespaceName   = fbsStructure.namespaceName;

            for (int i = 0; i < fbsStructure.tableStructures.Length; i++)
            {
                TableStructure tableStructure = fbsStructure.tableStructures[i];
                if (string.IsNullOrEmpty(namespaceName))
                {
                    tableStructure.tableNameWithNamespace       = $"global::{tableStructure.tableName}";
                    tableStructure.tableNameWithCSharpNamespace = $"{targetNamespace}.{tableStructure.tableName}";
                }
                else
                {
                    tableStructure.tableNameWithNamespace       = $"global::{namespaceName}.{tableStructure.tableName}";
                    tableStructure.tableNameWithCSharpNamespace = $"{targetNamespace}.{namespaceName}.{tableStructure.tableName}";
                }
                for (int j = 0; j < tableStructure.fieldInfos.Length; j++)
                {
                    TableFieldInfo fieldInfo = tableStructure.fieldInfos[j];
                    fieldInfo.fieldTypeNameWithNameSpace =
                        ConvertToCSharpTypeNameWithNamespaceName(fieldInfo.fieldTypeName, namespaceName);
                }
            }
        }
Пример #2
0
        private void TraverseParseObject(string[] lines, ref int currentLineNum, ref TableStructure tableStructure)
        {
            List <TableFieldInfo> fieldInfoList = new List <TableFieldInfo>();

            while (currentLineNum < lines.Length)
            {
                string line = lines[currentLineNum];
                if (string.IsNullOrWhiteSpace(line))
                {
                    currentLineNum++;
                    continue;
                }

                bool isEndOfObject = line.Contains("}");

                //这里使用正则表达式寻找,因为fbs允许多个field写在一行
                //find pattern: fieldname:typename; or fieldname:[typename]; ;
                const string    fieldPattern              = @"[a-zA-Z0-9_]+ *: *([a-zA-Z0-9_]+\.)*[a-zA-Z0-9_]+;";
                const string    arrayFieldPattern         = @"[a-zA-Z0-9_]+ *: *\[([a-zA-Z0-9_]+\.)*[a-zA-Z0-9_]+ *\] *;";
                MatchCollection fieldMatchCollection      = Regex.Matches(line, fieldPattern);
                MatchCollection arrayFieldMatchCollection = Regex.Matches(line, arrayFieldPattern);
                int             fieldCount      = fieldMatchCollection.Count;
                int             arrayFieldCount = arrayFieldMatchCollection.Count;
                if (fieldCount == 0 && arrayFieldCount == 0)
                {
                    if (isEndOfObject)
                    {
                        currentLineNum++;
                        tableStructure.fieldInfos = fieldInfoList.ToArray();
                        return;
                    }

                    currentLineNum++;
                    continue;
                }

                for (int i = 0; i < fieldCount; i++)
                {
                    string matchString = fieldMatchCollection[i].Value;
                    matchString = matchString.Replace(" ", "");
                    //remove ;
                    matchString = matchString.Slice(0, -1);

                    string[] spliteStrings = matchString.Split(':');
                    string   fieldName     = spliteStrings[0];
                    string   typeName      = spliteStrings[1];

                    TableFieldInfo newFieldInfo = new TableFieldInfo
                    {
                        fieldName               = fieldName,
                        fieldTypeName           = typeName,
                        isArray                 = false,
                        isScalarType            = CheckFlatbuffersTypeIsScalarType(typeName),
                        upperCamelCaseFieldName = ConvertToUpperCamelCase(fieldName),
                        fieldCSharpTypeName     = ConvertToCSharpTypeName(typeName),
                    };
                    fieldInfoList.Add(newFieldInfo);
                }

                for (int i = 0; i < arrayFieldCount; i++)
                {
                    string matchString = arrayFieldMatchCollection[i].Value;
                    //remove ;
                    matchString = matchString.Replace(" ", "");
                    matchString = matchString.Slice(0, -1);

                    string[] spliteStrings = matchString.Split(':');
                    string   fieldName     = spliteStrings[0];
                    string   typeName      = spliteStrings[1];
                    //remove []
                    typeName = typeName.Slice(1, -1);

                    TableFieldInfo newFieldInfo = new TableFieldInfo
                    {
                        fieldName     = fieldName,
                        fieldTypeName = typeName,
                        isArray       = true,
                        //数组都不是scalar类型
                        isScalarType            = false,
                        upperCamelCaseFieldName = ConvertToUpperCamelCase(fieldName),
                        fieldCSharpTypeName     = ConvertToCSharpTypeName(typeName),
                        arrayTypeIsScalarType   = CheckFlatbuffersTypeIsScalarType(typeName)
                    };
                    fieldInfoList.Add(newFieldInfo);
                }

                if (isEndOfObject)
                {
                    currentLineNum++;
                    tableStructure.fieldInfos = fieldInfoList.ToArray();
                    return;
                }

                currentLineNum++;
            }

            throw new ParseFileException {
                      errorMessage = "解析文件出错,格式不正确"
            };
        }