Ejemplo n.º 1
0
        public static string CreateSerializeCodeForFlatbuffer(ParseStruct structInfo, string varName)
        {
            string            classStr       = "";
            string            stringStr      = "";
            string            argsStr        = "";
            string            spacesStr      = "";
            List <MemberAttr> memberAttrInfo = structInfo.MemberAttrs;
            string            structName     = structInfo.StructName;

            serializeRecursive++;
            for (int j = 0; j <= serializeRecursive; j++)
            {
                spacesStr += "\t";
            }



            for (int i = 0; i < memberAttrInfo.Count; i++)
            {
                string iterName = memberAttrInfo[i].VarName;
                string iterType = memberAttrInfo[i].TypeName;

                if (memberAttrInfo[i].IsArray)
                {
                    if (memberAttrInfo[i].IsClass)
                    {
                        classStr += "\n";
                        classStr += spacesStr + "//class " + iterType + " vector serialize\n";
                        classStr += spacesStr + iterName + "PosArray := make([]flatbuffers.UOffsetT, len(" + varName + "." + iterName + "))\n";
                        classStr += spacesStr + "for i," + iterName + " := range " + varName + "." + iterName + " {\n";
                        classStr += spacesStr + ParseStruct.CreateSerializeCodeForFlatbuffer((ParseStruct)Vars.GetStruct(iterType), iterName);
                        classStr += spacesStr + "\t" + iterName + "PosArray[i] = " + iterName + "Pos\n";
                    }
                    else if (memberAttrInfo[i].IsString)
                    {
                        classStr += "\n";
                        classStr += spacesStr + "//string " + iterName + " vector serialize\n";
                        classStr += spacesStr + iterName + "PosArray := make([]flatbuffers.UOffsetT, len(" + varName + "." + iterName + "))\n";
                        classStr += spacesStr + "for i," + iterName + " := range " + varName + "." + iterName + " {\n";
                        classStr += spacesStr + "\t" + iterName + "PosArray[i] = builder.CreateString(" + iterName + ")\n";
                    }
                    else
                    {
                        string endOffset = varName + StringTo.ToUpper(iterName) + "EndOffset";
                        classStr += "\n";
                        classStr += spacesStr + "//" + iterType + " " + iterName + " vector serialize\n";
                        classStr += spacesStr + structName + "FBStart" + StringTo.ToUpper(iterName) + "Vector(builder, len(" + varName + "." + iterName + "))\n";
                        classStr += spacesStr + "for _," + iterName + " := range " + varName + "." + iterName + " {\n";
                        classStr += spacesStr + "\tbuilder.Prepend" + StringTo.ToUpper(iterType) + "(" + iterName + ")\n";
                        classStr += spacesStr + "}\n";
                        classStr += spacesStr + endOffset + " := builder.EndVector(len(" + varName + "." + iterName + "))\n\n";
                        argsStr  += spacesStr + structName + "FBAdd" + StringTo.ToUpper(iterName) + "(builder," + endOffset + ")\n";
                    }

                    if (memberAttrInfo[i].IsClass || memberAttrInfo[i].IsString)
                    {
                        classStr += spacesStr + "}\n";

                        string endOffset = varName + StringTo.ToUpper(iterName) + "EndOffset";

                        classStr += spacesStr + structName + "FBStart" + StringTo.ToUpper(iterName) + "Vector(builder, len(" + iterName + "PosArray))\n";
                        classStr += spacesStr + "for _," + iterName + "Offset := range " + iterName + "PosArray {\n";
                        classStr += "\t\tbuilder.PrependUOffsetT(" + iterName + "Offset)\n";
                        classStr += "\t}\n";
                        classStr += spacesStr + endOffset + " := builder.EndVector(len(" + iterName + "PosArray))\n\n";
                        argsStr  += spacesStr + structName + "FBAdd" + StringTo.ToUpper(iterName) + "(builder," +
                                    varName + StringTo.ToUpper(iterName) + "EndOffset)\n";
                    }
                }
                else if (memberAttrInfo[i].IsClass)
                {
                    classStr += "\n";
                    classStr += "//class " + iterType + " " + iterName + " serialize\n";
                    classStr += ParseStruct.CreateSerializeCodeForFlatbuffer((ParseStruct)Vars.GetStruct(iterType), iterName);
                    argsStr  += spacesStr + structName + "FBAdd" + StringTo.ToUpper(iterName) + "(builder," + iterName + "Pos" + ")\n";
                }
                else if (memberAttrInfo[i].IsString)
                {
                    string stringPos = varName + StringTo.ToUpper(iterName) + "StringPos";
                    stringStr += "\n";
                    stringStr += spacesStr + stringPos + " := builder.CreateString(" + varName + "." + iterName + ")\n";
                    argsStr   += spacesStr + structName + "FBAdd" + StringTo.ToUpper(iterName) + "(builder," + stringPos + ")\n";
                }
                else
                {
                    argsStr += spacesStr + structName + "FBAdd" + StringTo.ToUpper(iterName) + "(builder," + varName + "." + iterName + ")\n";
                }
            }


            classStr += spacesStr + "//class " + structName + " serialize start\n";
            classStr += stringStr;
            classStr += spacesStr + structName + "FBStart(builder)\n";
            classStr += argsStr;
            classStr += spacesStr + varName + "Pos := " + structName + "FBEnd(builder)\n";
            classStr += spacesStr + "//class " + structName + " serialize end\n\n";
            serializeRecursive--;
            return(classStr);
        }
Ejemplo n.º 2
0
        public static string CreateDeserializeCodeForFlatbuffer(ParseStruct structInfo, string varName, string fbName)
        {
            string            strs           = "";
            string            spacesStr      = "";
            List <MemberAttr> memberAttrInfo = structInfo.MemberAttrs;
            string            structName     = structInfo.StructName;

            deserializeRecursive++;
            for (int j = 0; j <= deserializeRecursive; j++)
            {
                spacesStr += "\t";
            }

            for (int i = 0; i < memberAttrInfo.Count; i++)
            {
                string iterName = memberAttrInfo[i].VarName;
                string iterType = memberAttrInfo[i].TypeName;

                //for (int j = 0; j <= deserializeRecursive; j++)
                //     speces += "\t";

                if (memberAttrInfo[i].IsArray)
                {
                    if (memberAttrInfo[i].IsClass)
                    {
                        string tmpName = iterType.ToLower() + "Tmp";
                        strs += spacesStr + "for i := 0;i < " + fbName + "." + StringTo.ToUpper(iterName) + "Length();i++ {\n";
                        strs += spacesStr + "\tvar " + tmpName + " " + iterType + "\n";
                        strs += spacesStr + "\tvar " + iterName + " " + iterType + "FB\n";

                        strs += spacesStr + "\tif ok := " + fbName + "." + StringTo.ToUpper(iterName) + "(&" + iterName + ",i);!ok {\n";
                        strs += spacesStr + "\t\treturn nil, errors.New(\"deserialize " + fbName + "=>" + StringTo.ToUpper(iterName) + " fail\")\n";
                        strs += spacesStr + "\t}\n\n";
                        strs += ParseStruct.CreateDeserializeCodeForFlatbuffer((ParseStruct)Vars.GetStruct(iterType), tmpName, iterName);
                        strs += spacesStr + "\t" + varName + "." + iterName + " = append(" + varName + "." + iterName + "," + tmpName + ")\n";
                    }
                    else
                    {
                        strs += spacesStr + "for i := 0;i < " + fbName + "." + StringTo.ToUpper(iterName) + "Length();i++ {\n";
                        strs += spacesStr + "\t" + varName + "." + iterName + " = append(" + varName + "." + iterName + ",";
                        if (memberAttrInfo[i].IsString)
                        {
                            strs += spacesStr + "string(" + fbName + "." + StringTo.ToUpper(iterName) + "(i)))\n";
                        }
                        else
                        {
                            strs += spacesStr + fbName + "." + StringTo.ToUpper(iterName) + "(i))\n";
                        }
                    }
                    strs += spacesStr + "}\n\n";
                }
                else if (memberAttrInfo[i].IsClass)
                {
                    strs += ParseStruct.CreateDeserializeCodeForFlatbuffer((ParseStruct)Vars.GetStruct(iterType), varName + "." + iterName,
                                                                           fbName + "." + StringTo.ToUpper(iterName) + "()");
                }
                else
                {
                    if (memberAttrInfo[i].IsString)
                    {
                        strs += spacesStr + varName + "." + iterName + " = string(" + fbName + "." + StringTo.ToUpper(iterName) + "())\n";
                    }
                    else
                    {
                        strs += spacesStr + varName + "." + iterName + " = " + fbName + "." + StringTo.ToUpper(iterName) + "()\n";
                    }
                }
            }

            deserializeRecursive--;
            return(strs);
        }
Ejemplo n.º 3
0
        bool parse(string data)
        {
            data = Regex.Replace(data, @"\/\/[^\n]*", "");
            data = Regex.Replace(data, @"[\n\r\t]", "");
            data = Regex.Replace(data, @"\s{2,}", "");

            string[] classes = Regex.Split(data, @"[@\}]");

            if (classes.Length == 0)
            {
                throw new System.Exception("parse classes is failed, no class struct!!");
            }

            //

            foreach (string c in classes)
            {
                string[] symbolFlag = c.Split('{');
                if (symbolFlag.Length != 2)
                {
                    continue;
                }

                string[] symbolAttr = symbolFlag[0].Split(":");
                if (symbolAttr.Length != 2)
                {
                    throw new Exception("parse symbol  attr is failed,  symbol missing :, " + symbolFlag[0]);
                }

                IBParse idlParse;
                switch (symbolAttr[0])
                {
                case Symbol.Struct:
                    idlParse = new ParseStruct();
                    break;

                case Symbol.Interface:
                    idlParse = new ParseInterface();
                    break;

                case Symbol.Namespace:
                    idlParse = new ParseNamespace();
                    break;

                default:
                    throw new Exception("parse symbol attr is error,  symbol: " + symbolAttr[0]);
                }

                if (idlParse.Parse(m_fileName, symbolAttr[1].Trim(), symbolFlag[1].Trim()))
                {
                    switch (symbolAttr[0])
                    {
                    case Symbol.Struct:
                        Vars.RegisterStruct(idlParse.GetName(), idlParse);
                        break;

                    case Symbol.Interface:
                        Vars.RegisterInterface(idlParse.GetName(), idlParse);
                        break;

                    case Symbol.Namespace:
                        Vars.RegisterNamespace(idlParse);
                        break;
                    }
                }
            }

            //TODDO:解释代码修
            createCode();
            return(true);
        }
Ejemplo n.º 4
0
        public static string CreateFlatbufferCode(ParseStruct structInterface)
        {
            m_flatbufferVariable["bool"]    = "bool";
            m_flatbufferVariable["int8"]    = "byte";
            m_flatbufferVariable["uint8"]   = "ubyte";
            m_flatbufferVariable["int16"]   = "short";
            m_flatbufferVariable["uint16"]  = "ushort";
            m_flatbufferVariable["int"]     = "int";
            m_flatbufferVariable["uint"]    = "uint";
            m_flatbufferVariable["int32"]   = "long";
            m_flatbufferVariable["uint32"]  = "ulong";
            m_flatbufferVariable["float32"] = "float";
            m_flatbufferVariable["float64"] = "double";
            m_flatbufferVariable["string"]  = "string";

            string strs = "table " + structInterface.StructName + "FB {\n";

            for (int i = 0; i < structInterface.MemberAttrs.Count; i++)
            {
                MemberAttr v        = structInterface.MemberAttrs[i];
                string     typeName = getFlatbufferVariable(v.TypeName);
                if (typeName == null)
                {
                    ParseStruct pstruct = (ParseStruct)Vars.GetStruct(v.TypeName);
                    if (pstruct != null)
                    {
                        typeName = pstruct.GetName() + "FB";
                    }
                }

                if (typeName != null)
                {
                    if (v.IsArray)
                    {
                        if (v.IsClass)
                        {
                            strs += "\t" + v.VarName + ":[" + v.TypeName + "FB];\n";
                        }
                        else
                        {
                            strs += "\t" + v.VarName + ":[" + v.TypeName + "];\n";
                        }
                    }
                    else
                    {
                        if (v.IsClass)
                        {
                            strs += "\t" + v.VarName + ":" + v.TypeName + "FB;\n";
                        }
                        else
                        {
                            strs += "\t" + v.VarName + ":" + v.TypeName + ";\n";
                        }
                    }
                }
                else
                {
                    throw new System.Exception("create flatbuffer file is faild, message name: " +
                                               structInterface.StructName + ", type:" + v.TypeName + " is not exits!");
                }
            }
            strs += "}\n\n";

            return(strs);
        }
Ejemplo n.º 5
0
        void createCode()
        {
            if (Vars.GetNamespace() == null)
            {
                throw new System.Exception("undefine namespace");
            }

            string serverCodeInterface, serverCodeService;
            string structCode;
            string flatbufferCode;

            serverCodeService  = "//magic RPC Automatic generation service:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "\n\n";
            serverCodeService += Vars.GetNamespace().CreateCodeForLanguage(m_language);

            serverCodeInterface  = "//magic RPC Automatic generation interface:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "\n\n";
            serverCodeInterface += Vars.GetNamespace().CreateCodeForLanguage(m_language);

            serverCodeInterface += "import (\n";
            serverCodeInterface += "\trpcs \"github.com/yamakiller/magicRPC/service\"\n";
            serverCodeInterface += "\trpcc \"github.com/yamakiller/magicRPC/rpc\"\n";
            serverCodeInterface += "\trpcb \"github.com/yamakiller/magicLibs/boxs\"\n";
            serverCodeInterface += "\tflatbuffers \"github.com/google/flatbuffers/go\"\n";
            serverCodeInterface += ")\n\n\n";


            serverCodeService += "import (\n";
            serverCodeService += "\trpcb \"github.com/yamakiller/magicLibs/boxs\"\n";
            serverCodeService += ")\n\n\n";

            string clientCodeInterface, clientCodeService;

            clientCodeInterface  = "//magic RPC Client Automatic generation interface:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "\n\n";
            clientCodeInterface += Vars.GetNamespace().CreateCodeForLanguage(m_language);
            clientCodeInterface += "import (\n";
            clientCodeInterface += "\t\"errors\"\n";
            clientCodeInterface += "\trpclient \"github.com/yamakiller/magicRPC/client\"\n";
            clientCodeInterface += "\trpcc \"github.com/yamakiller/magicRPC/rpc\"\n";
            clientCodeInterface += ")\n\n\n";

            clientCodeService  = "//magic RPC Client Automatic generation service:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "\n\n";
            clientCodeService += Vars.GetNamespace().CreateCodeForLanguage(m_language);
            clientCodeService += "import (\n";
            clientCodeService += "\trpclient \"github.com/yamakiller/magicRPC/client\"\n";
            clientCodeService += "\trpcc \"github.com/yamakiller/magicRPC/rpc\"\n";
            clientCodeService += ")\n\n\n";

            /*foreach(k, v; idlInerfaceList)
             * {
             *  serverCodeInterface ~= v.createServerCodeForInterface(CODE_LANGUAGE.CL_DLANG);
             *  serverCodeService ~= v.createServerCodeForService(CODE_LANGUAGE.CL_DLANG);
             *
             *  clientCodeInterface ~= v.createClientCodeForInterface(CODE_LANGUAGE.CL_DLANG);
             *  clientCodeService ~= v.createClientCodeForService(CODE_LANGUAGE.CL_DLANG);
             * }*/
            //Interface Code make
            foreach (KeyValuePair <string, IBParse> pair in Vars.GetInterfaces())
            {
                serverCodeInterface += pair.Value.CreateServerInterfaceForLanguage(m_language);
                serverCodeService   += pair.Value.CreateServerServiceForLanguage(m_language);

                clientCodeInterface += pair.Value.CreateClientInterfaceForLanguage(m_language);
                clientCodeService   += pair.Value.CreateClientServiceForLanguage(m_language);
            }

            //Struct Code make
            structCode  = "//magic RPC Automatic generation message:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "\n\n";
            structCode += Vars.GetNamespace().CreateCodeForLanguage(m_language);
            foreach (KeyValuePair <string, IBParse> pair in Vars.GetStructs())
            {
                structCode += pair.Value.CreateCodeForLanguage(m_language);
            }

            //flatbufferCode = "//magic RPC Automatic generation fbs:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "\n\n";
            flatbufferCode  = "namespace " + Vars.GetNamespace().GetName() + ";\n\n";
            flatbufferCode += "attribute \"priority\";\n\n";
            foreach (KeyValuePair <string, IBParse> pair in Vars.GetStructs())
            {
                flatbufferCode += FlatbufferCode.CreateFlatbufferCode((ParseStruct)pair.Value);
            }

            if (!Directory.Exists(m_outFilePath))
            {
                Directory.CreateDirectory(m_outFilePath);
            }

            if (!Directory.Exists(m_outFilePath + "/Server"))
            {
                Directory.CreateDirectory(m_outFilePath + "/Server");
            }

            if (!Directory.Exists(m_outFilePath + "/Server/" + Vars.GetNamespace().GetName()))
            {
                Directory.CreateDirectory(m_outFilePath + "/Server/" + Vars.GetNamespace().GetName());
            }

            if (!Directory.Exists(m_outFilePath + "/Client"))
            {
                Directory.CreateDirectory(m_outFilePath + "/Client");
            }

            if (!Directory.Exists(m_outFilePath + "/Client/" + Vars.GetNamespace().GetName()))
            {
                Directory.CreateDirectory(m_outFilePath + "/Client/" + Vars.GetNamespace().GetName());
            }


            if (!Directory.Exists(m_outFilePath + "/Message"))
            {
                Directory.CreateDirectory(m_outFilePath + "/Message");
            }

            if (!Directory.Exists(m_outFilePath + "/Message/" + Vars.GetNamespace().GetName()))
            {
                Directory.CreateDirectory(m_outFilePath + "/Message/" + Vars.GetNamespace().GetName());
            }


            FileSave.Save(m_outFilePath + "/Server/" + Vars.GetNamespace().GetName() + "/" + m_fileName + "Interface" + extension(), serverCodeInterface);
            FileSave.Save(m_outFilePath + "/Server/" + Vars.GetNamespace().GetName() + "/" + m_fileName + "Service" + extension(), serverCodeService);
            FileSave.Save(m_outFilePath + "/Message/" + Vars.GetNamespace().GetName() + "/" + m_fileName + "Message" + extension(), structCode);
            FileSave.Save(m_outFilePath + "/Client/" + Vars.GetNamespace().GetName() + "/" + m_fileName + "Interface" + extension(), clientCodeInterface);
            FileSave.Save(m_outFilePath + "/Client/" + Vars.GetNamespace().GetName() + "/" + m_fileName + "Service" + extension(), clientCodeService);

            FileSave.Save(m_outFilePath + "/" + m_fileName + ".fbs", flatbufferCode);

            Proc.RunExe("flatc.exe", "--go -o  " + m_outFilePath + "/Message/ " + m_outFilePath + "/" + m_fileName + ".fbs");
        }