Ejemplo n.º 1
0
        public static void test3()
        {
            FileStream file = new FileStream("all.proto.dat", FileMode.Open);

            Console.WriteLine(file.Length);
            excel_proto proto = new excel_proto();

            proto.MergeFrom(file);
            Console.WriteLine(proto.ToString());
            foreach (var it in proto.AllProto)
            {
                Console.WriteLine(it.Key);
                Console.WriteLine(it.Value.ToStringUtf8());
            }

            var         dataFile = new FileStream("test.dat", FileMode.Open);
            excel_table table    = new excel_table();

            table.MergeFrom(dataFile);
            Console.WriteLine(table.ProtoMsgName);
            foreach (var it in table.Table)
            {
                foreach (var cell in it.Value.CellData)
                {
                    Console.WriteLine("int=" + cell.Int32Value + ",float=" + cell.FloatValue);
                }
            }
            Console.ReadKey();
        }
Ejemplo n.º 2
0
        excel_proto ReadProto()
        {
            excel_proto proto_data    = new excel_proto();
            string      protoDataPath = config.protoDataPath + PROTO_DATA_FILE;
            FileStream  file;

            try
            {
                file = new FileStream(protoDataPath, FileMode.OpenOrCreate);
            }
            catch (Exception e)
            {
                Console.WriteLine("打开文件 " + protoDataPath + " 失败:error=" + e.Message);
                return(null);
            }

            if (file.Length > 0)
            {
                try
                {
                    proto_data.MergeFrom(file);
                }
                catch (Exception e)
                {
                    Console.WriteLine("解析文件 " + protoDataPath + " 失败:error=" + e.Message);
                    file.Close();
                    return(null);
                }
            }
            file.Close();
            return(proto_data);
        }
Ejemplo n.º 3
0
        static excel_proto ReadProto(string path)
        {
            excel_proto proto_data = new excel_proto();
            FileStream  file;

            try
            {
                file = new FileStream(path, FileMode.Open);
            }
            catch (Exception e)
            {
                Console.WriteLine("打开文件 " + path + " 失败:error=" + e.Message);
                return(null);
            }

            if (file.Length > 0)
            {
                try
                {
                    proto_data.MergeFrom(file);
                }
                catch (Exception e)
                {
                    Console.WriteLine("解析文件 " + path + " 失败:error=" + e.Message);
                    file.Close();
                    return(null);
                }
            }
            file.Close();
            return(proto_data);
        }
Ejemplo n.º 4
0
 public static void GenerateConfigType(excel_proto proto, out StringBuilder dotnetCode, out StringBuilder javaCode)
 {
     dotnetCode = new StringBuilder();
     javaCode   = new StringBuilder();
     foreach (var it in proto.AllProto)
     {
         dotnetCode.Append(it.Key.ToUpper()).Append(",\r\n");
     }
 }
Ejemplo n.º 5
0
        bool WriteProto(string proto)
        {
            if (proto.Length <= 0)
            {
                ConsoleError(msgName, -1, "生成的proto为空");
                return(false);
            }
            FileStream file;

            try
            {
                file = new FileStream(PROTO_DATA_FILE, FileMode.OpenOrCreate);
            } catch (Exception e)
            {
                ConsoleError(msgName, -1, "打开文件" + PROTO_DATA_FILE + "失败:error=" + e.ToString());
                return(false);
            }

            excel_proto proto_data = new excel_proto();

            Console.WriteLine("read proto size=" + file.Length);
            if (file.Length > 0)
            {
                try
                {
                    proto_data.MergeFrom(file);
                }
                catch (Exception e)
                {
                    ConsoleError(msgName, -1, "解析文件" + PROTO_DATA_FILE + "失败:error=" + e.Message);
                    file.Close();
                    return(false);
                }
            }
            file.Close();

            file = new FileStream(PROTO_DATA_FILE, FileMode.Truncate);

            if (proto_data.AllProto.TryGetValue(msgName, out ByteString tempValue))
            {
                proto_data.AllProto[msgName] = ByteString.CopyFromUtf8(proto);
            }
            else
            {
                proto_data.AllProto.Add(msgName, ByteString.CopyFromUtf8(proto));
            }

            var data = proto_data.ToByteArray();

            file.Write(data, 0, data.Length);
            file.Close();
            return(true);
        }
Ejemplo n.º 6
0
        public static bool GenerateCode()
        {
            string configPath = "generate_code_config.json";
            var    config     = ReadJsonConfig(configPath);

            if (null == config)
            {
                Console.WriteLine("读取配置失败,config=" + configPath);
                return(false);
            }
            FileStream file;

            try
            {
                file = new FileStream(config.protoDataPath, FileMode.Open);
            }
            catch (Exception e)
            {
                Console.WriteLine("打开文件 " + config.protoDataPath + " 失败:error=" + e.Message);
                return(false);
            }
            excel_proto proto = new excel_proto();

            proto.MergeFrom(file);

            var dotnetTemplateStr = ReadCodeTemplate(config.dotnetCodeTemplatePath);

            if (null == dotnetTemplateStr)
            {
                Console.WriteLine("读取模板配置失败,config=" + config.dotnetCodeTemplatePath);
                return(false);
            }
            GenerateConfigType(proto, out StringBuilder dotnetCode, out StringBuilder javaCode);
            dotnetTemplateStr = dotnetTemplateStr.Replace("CONFIG_TYPE_REPLACE_STRING,", dotnetCode.ToString());
            dotnetTemplateStr = dotnetTemplateStr.Replace("MD5_REPLACE_STRING", proto.Md5);
            if (!WriteGenerateCode(config.dotnetCodePath, dotnetTemplateStr))
            {
                return(false);
            }
            return(true);
        }
Ejemplo n.º 7
0
        bool WriteProto(excel_proto proto_data)
        {
            string     protoDataPath = config.protoDataPath + PROTO_DATA_FILE;
            FileStream file;

            try
            {
                file = new FileStream(protoDataPath, FileMode.Create);
            }
            catch (Exception e)
            {
                Console.WriteLine("打开文件 " + protoDataPath + " 失败:error=" + e.Message);
                return(false);
            }

            var data = proto_data.ToByteArray();

            file.Write(data, 0, data.Length);
            file.Close();
            return(true);
        }
Ejemplo n.º 8
0
        //生成proto文件
        public bool GenerateProto()
        {
            string     path = config.protoDataPath + PROTO_DATA_FILE;
            FileStream file;

            try
            {
                file = new FileStream(path, FileMode.Open);
            }
            catch (Exception e)
            {
                Console.WriteLine("GenerateProto failed:打开文件" + path + "失败,error=" + e.Message);
                return(false);
            }
            if (file.Length <= 0)
            {
                Console.WriteLine("GenerateProto failed:文件" + path + "为空");
                file.Close();
                return(false);
            }

            excel_proto proto = new excel_proto();

            var data = new byte[file.Length];

            file.Read(data, 0, data.Length);
            file.Close();

            try
            {
                proto.MergeFrom(data);
            }
            catch (Exception e)
            {
                Console.WriteLine("GenerateProto failed:解析文件" + path + "失败,error=", e.Message);
                return(false);
            }

            StringBuilder ss = new StringBuilder();

            ss.Append("syntax = \"proto3\";").Append(LINE_SIGN)
            .Append("package bestan.config;").Append(LINE_SIGN);
            foreach (var it in proto.AllProto)
            {
                ss.Append(it.Value.ToStringUtf8());

                ss.Append(LINE_SIGN).Append("message " + it.Key + "_all").Append(LINE_SIGN)
                .Append("{").Append(LINE_SIGN)
                .Append("\tmap<int32, ").Append(it.Key).Append("> configs = 1;").Append(LINE_SIGN)
                .Append("}").Append(LINE_SIGN);
            }

            var proto_data = ReadProto();

            if (proto_data == null)
            {
                return(false);
            }
            proto_data.Md5 = GetMD5(ss.ToString());
            ss.Append(LINE_SIGN).Append("//MD5=").Append(proto_data.Md5).Append(LINE_SIGN);
            if (!WriteProto(proto_data))
            {
                return(false);
            }
            string protoFile = config.protoPath + PROTO_FILE;

            try
            {
                file = new FileStream(protoFile, FileMode.Create);
            }
            catch (Exception e)
            {
                Console.WriteLine("GenerateProto failed:打开文件" + protoFile + "失败,error=" + e.Message);
                return(false);
            }
            var outData = System.Text.Encoding.Default.GetBytes(ss.ToString());

            file.Write(outData, 0, outData.Length);
            file.Close();

            if (!RunProcess())
            {
                Console.WriteLine("使用protoc生成python用的pb文件失败:proto=" + config.protoPath + PROTO_FILE);
                return(false);
            }
            if (!InitPython())
            {
                Console.WriteLine("InitPython 执行失败");
                return(false);
            }
            return(true);
        }
Ejemplo n.º 9
0
        //生成proto文件
        public static bool GenerateProto(string dir)
        {
            string     path = dir + PROTO_DATA_FILE;
            FileStream file;

            try
            {
                file = new FileStream(path, FileMode.Open);
            }
            catch (Exception e)
            {
                Console.WriteLine("GenerateProto failed:打开文件" + path + "失败,error=" + e.Message);
                return(false);
            }
            if (file.Length <= 0)
            {
                Console.WriteLine("GenerateProto failed:文件" + path + "为空");
                file.Close();
                return(false);
            }

            excel_proto proto = new excel_proto();

            var data = new byte[file.Length];

            file.Read(data, 0, data.Length);
            file.Close();

            try
            {
                proto.MergeFrom(data);
            }
            catch (Exception e)
            {
                Console.WriteLine("GenerateProto failed:解析文件" + path + "失败,error=", e.Message);
                return(false);
            }

            StringBuilder ss = new StringBuilder();

            ss.Append("syntax = \"proto3\";").Append(LINE_SIGN)
            .Append("package bestan.common.config;").Append(LINE_SIGN);
            foreach (var it in proto.AllProto)
            {
                ss.Append(it.Value.ToStringUtf8());
            }
            string protoFile = dir + "all.proto";

            try
            {
                file = new FileStream(protoFile, FileMode.Create);
            }
            catch (Exception e)
            {
                Console.WriteLine("GenerateProto failed:打开文件" + protoFile + "失败,error=" + e.Message);
                return(false);
            }
            var outData = System.Text.Encoding.Default.GetBytes(ss.ToString());

            file.Write(outData, 0, outData.Length);
            file.Close();
            return(true);
        }