示例#1
0
        public async Task GenAsync(RemoteAgent agent, GenJob rpc)
        {
            var res = new GenJobRes()
            {
                ErrCode    = Luban.Common.EErrorCode.OK,
                ErrMsg     = "succ",
                FileGroups = new List <FileGroup>(),
            };

            if (!TryParseArg(rpc.Arg.JobArguments, out GenArgs args, out string errMsg))
            {
                res.ErrCode = Luban.Common.EErrorCode.JOB_ARGUMENT_ERROR;
                res.ErrMsg  = errMsg;
                agent.Session.ReplyRpc <GenJob, GenJobArg, GenJobRes>(rpc, res);
                return;
            }

            var timer = new ProfileTimer();

            timer.StartPhase("= gen_all =");
            try
            {
                string outputCodeDir = args.OutputCodeDir;


                timer.StartPhase("build defines");
                var loader = new ProtoDefLoader(agent);
                await loader.LoadAsync(args.DefineFile);

                timer.EndPhaseAndLog();

                var rawDefines = loader.BuildDefines();

                var ass = new DefAssembly();

                ass.Load(rawDefines, agent);

                var targetService = args.Service;


                List <DefTypeBase> exportTypes = ass.GetExportTypes();

                var tasks        = new List <Task>();
                var genCodeFiles = new ConcurrentBag <FileInfo>();


                var genType = args.GenType;
                switch (genType)
                {
                case "cs":
                {
                    var render = new CsRender();
                    foreach (var c in ass.Types.Values)
                    {
                        tasks.Add(Task.Run(() =>
                            {
                                var content = FileHeaderUtil.ConcatAutoGenerationHeader(render.RenderAny(c), Common.ELanguage.CS);
                                var file    = RenderFileUtil.GetDefTypePath(c.FullName, Common.ELanguage.CS);
                                var md5     = CacheFileUtil.GenMd5AndAddCache(file, content);
                                genCodeFiles.Add(new FileInfo()
                                {
                                    FilePath = file, MD5 = md5
                                });
                            }));
                    }
                    tasks.Add(Task.Run(() =>
                        {
                            var module  = ass.TopModule;
                            var name    = "ProtocolStub";
                            var content = FileHeaderUtil.ConcatAutoGenerationHeader(
                                render.RenderStubs(name, module,
                                                   ass.Types.Values.Where(t => t is DefProto).ToList(),
                                                   ass.Types.Values.Where(t => t is DefRpc).ToList()),
                                Common.ELanguage.CS);
                            var file = RenderFileUtil.GetDefTypePath(name, Common.ELanguage.CS);
                            var md5  = CacheFileUtil.GenMd5AndAddCache(file, content);
                            genCodeFiles.Add(new FileInfo()
                            {
                                FilePath = file, MD5 = md5
                            });
                        }));
                    break;
                }

                case "lua":
                {
                    tasks.Add(Task.Run(() =>
                        {
                            var render  = new LuaRender();
                            var content = FileHeaderUtil.ConcatAutoGenerationHeader(render.RenderTypes(ass.Types.Values.ToList()), Common.ELanguage.LUA);
                            var file    = "Types.lua";
                            var md5     = CacheFileUtil.GenMd5AndAddCache(file, content);
                            genCodeFiles.Add(new FileInfo()
                            {
                                FilePath = file, MD5 = md5
                            });
                        }));
                    break;
                }

                default:
                {
                    throw new NotSupportedException($"not support gen type:{genType}");
                }
                }

                await Task.WhenAll(tasks.ToArray());

                res.FileGroups.Add(new FileGroup()
                {
                    Dir = outputCodeDir, Files = genCodeFiles.ToList()
                });
            }
            catch (Exception e)
            {
                res.ErrCode = Luban.Common.EErrorCode.JOB_EXCEPTION;
                res.ErrMsg  = $"{e.Message} \n {e.StackTrace}";
            }
            timer.EndPhaseAndLog();

            agent.Session.ReplyRpc <GenJob, GenJobArg, GenJobRes>(rpc, res);
        }
        public async Task GenAsync(RemoteAgent agent, GenJob rpc)
        {
            var res = new GenJobRes()
            {
                ErrCode    = Luban.Common.EErrorCode.OK,
                ErrMsg     = "succ",
                FileGroups = new List <FileGroup>(),
            };

            if (!TryParseArg(rpc.Arg.JobArguments, out GenArgs args, out string errMsg))
            {
                res.ErrCode = Luban.Common.EErrorCode.JOB_ARGUMENT_ERROR;
                res.ErrMsg  = errMsg;
                agent.Session.ReplyRpc <GenJob, GenJobArg, GenJobRes>(rpc, res);
                return;
            }

            var timer = new ProfileTimer();

            timer.StartPhase("= gen_all =");
            try
            {
                string outputCodeDir = args.OutputCodeDir;


                timer.StartPhase("build defines");
                var loader = new ProtoDefLoader(agent);
                await loader.LoadAsync(args.DefineFile);

                timer.EndPhaseAndLog();

                var rawDefines = loader.BuildDefines();

                var ass = new DefAssembly();

                ass.Load(rawDefines, agent, args);

                var targetService = args.Service;


                List <DefTypeBase> exportTypes = ass.GetExportTypes();

                var tasks             = new List <Task>();
                var genCodeFiles      = new ConcurrentBag <FileInfo>();
                var genScatteredFiles = new ConcurrentBag <FileInfo>();


                var genType = args.GenType;
                var render  = RenderFactory.CreateRender(genType);
                if (render == null)
                {
                    throw new NotSupportedException($"not support gen type:{genType}");
                }
                ass.CurrentLanguage = RenderFileUtil.GetLanguage(genType);
                render.Render(new GenContext()
                {
                    GenArgs     = args,
                    Assembly    = ass,
                    Lan         = ass.CurrentLanguage,
                    GenType     = genType,
                    Render      = render,
                    Tasks       = tasks,
                    ExportTypes = exportTypes,
                    GenCodeFilesInOutputCodeDir = genCodeFiles,
                    GenScatteredFiles           = genScatteredFiles,
                });


                await Task.WhenAll(tasks.ToArray());

                res.FileGroups.Add(new FileGroup()
                {
                    Dir = outputCodeDir, Files = genCodeFiles.ToList()
                });
                res.ScatteredFiles.AddRange(genScatteredFiles);
            }
            catch (Exception e)
            {
                res.ErrCode    = Luban.Common.EErrorCode.JOB_EXCEPTION;
                res.ErrMsg     = ExceptionUtil.ExtractMessage(e);
                res.StackTrace = e.StackTrace;
            }

            DefAssemblyBase.LocalAssebmly = null;

            timer.EndPhaseAndLog();

            agent.Session.ReplyRpc <GenJob, GenJobArg, GenJobRes>(rpc, res);
        }