Skip to content

Commit

Permalink
[修改]1. 修改TS的解析导出
Browse files Browse the repository at this point in the history
  • Loading branch information
AlianBlank committed Aug 22, 2024
1 parent f43b8b8 commit 616eb51
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 15 deletions.
13 changes: 13 additions & 0 deletions ProtoExport/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ private static int Main(string[] args)
}

var files = Directory.GetFiles(launcherOptions.InputPath, "*.proto", SearchOption.AllDirectories);

var messageInfoLists = new List<MessageInfoList>(files.Length);
foreach (var file in files)
{
var operationCodeInfo = MessageHelper.Parse(File.ReadAllText(file), Path.GetFileNameWithoutExtension(file), launcherOptions.OutputPath, launcherOptions.IsGenerateErrorCode);
messageInfoLists.Add(operationCodeInfo);
switch (modeType)
{
case ModeType.Server:
Expand All @@ -56,6 +59,16 @@ private static int Main(string[] args)
}
}

switch (modeType)
{
case ModeType.TypeScript:
(ProtoGenerateHelper as ProtoBuffTypeScriptHelper)?.Post(messageInfoLists, launcherOptions.OutputPath);
break;
default:
throw new ArgumentOutOfRangeException();
}


Console.WriteLine("导出成功,按任意键退出");
Console.ReadKey();
return 0;
Expand Down
77 changes: 62 additions & 15 deletions ProtoExport/ProtoBuffTypeScriptHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ public void Run(MessageInfoList messageInfoList, string outputPath, string names
StringBuilder sb = new StringBuilder();
sb.Append("import IRequestMessage from \"../network/IRequestMessage\";\n");
sb.Append("import IResponseMessage from \"../network/IResponseMessage\";\n");
sb.Append("import INotifyMessage from \"../network/INotifyMessage\";\n");
sb.Append("import IHeartBeatMessage from \"../network/IHeartBeatMessage\";\n");
sb.Append("import MessageObject from \"../network/MessageObject\";\n");
sb.Append("import RegisterRequestMessageClass from \"../network/RegisterRequestMessageClass\";\n");
sb.Append("import RegisterResponseMessageClass from \"../network/RegisterResponseMessageClass\";\n");
// sb.Append($"namespace {namespaceName} {'{'}\n");
sb.Append("import ProtoMessageHelper from \"../network/ProtoMessageHelper\";\n");
sb.AppendLine();
sb.Append($"export namespace {messageInfoList.ModuleName} {'{'}\n");

foreach (var operationCodeInfo in messageInfoList.Infos)
{
Expand All @@ -42,14 +44,14 @@ public void Run(MessageInfoList messageInfoList, string outputPath, string names
sb.Append($"\t/// <summary>\n");
sb.Append($"\t/// {operationCodeInfo.Description}\n");
sb.Append($"\t/// </summary>\n");
if (operationCodeInfo.IsRequest)
{
sb.Append($"\t@RegisterRequestMessageClass({operationCodeInfo.Opcode})\n");
}
else
{
sb.Append($"\t@RegisterResponseMessageClass({operationCodeInfo.Opcode})\n");
}
// if (operationCodeInfo.IsRequest)
// {
// sb.Append($"\t@RegisterRequestMessageClass({operationCodeInfo.Opcode})\n");
// }
// else
// {
// sb.Append($"\t@RegisterResponseMessageClass({operationCodeInfo.Opcode})\n");
// }

if (string.IsNullOrEmpty(operationCodeInfo.ParentClass))
{
Expand All @@ -59,6 +61,26 @@ public void Run(MessageInfoList messageInfoList, string outputPath, string names
{
// sb.AppendLine($"\t[MessageTypeHandler({operationCodeInfo.Opcode})]");
sb.AppendLine($"\texport class {operationCodeInfo.Name} extends MessageObject implements {operationCodeInfo.ParentClass} {'{'}");
sb.AppendLine();

sb.Append($"\t\tpublic static register(): void{'{'}\n");
if (operationCodeInfo.IsRequest)
{
sb.Append($"\t\t\tProtoMessageHelper.registerReqMessage('{messageInfoList.ModuleName}.{operationCodeInfo.Name}', {(messageInfoList.Module << 16) + operationCodeInfo.Opcode});\n");
}
else
{
sb.Append($"\t\t\tProtoMessageHelper.registerRespMessage('{messageInfoList.ModuleName}.{operationCodeInfo.Name}', {(messageInfoList.Module << 16) + operationCodeInfo.Opcode});\n");
}

sb.Append($"\t\t{'}'}\n");


sb.Append($"\t\tpublic get PackageName(): string{'{'}\n");
sb.Append($"\t\t\treturn '{messageInfoList.ModuleName}.{operationCodeInfo.Name}';\n");
sb.Append($"\t\t{'}'}\n");

sb.AppendLine();
}

foreach (var operationField in operationCodeInfo.Fields)
Expand All @@ -84,15 +106,12 @@ public void Run(MessageInfoList messageInfoList, string outputPath, string names
}
}

sb.Append($"\t\tpublic static get PackageName(): string{'{'}\n");
sb.Append($"\t\t\treturn '{namespaceName}.{operationCodeInfo.Name}';\n");
sb.Append($"\t\t{'}'}\n");

sb.AppendLine("\t}\n");
}
}

// sb.Append("}\n");
sb.Append("}\n");

File.WriteAllText(messageInfoList.OutputPath + ".ts", sb.ToString(), Encoding.UTF8);
}
Expand Down Expand Up @@ -139,5 +158,33 @@ static string ConvertType(string type)

return typeCs;
}


public void Post(List<MessageInfoList> operationCodeInfo, string launcherOptionsOutputPath)
{
StringBuilder stringBuilder = new StringBuilder();

foreach (var messageInfoList in operationCodeInfo)
{
stringBuilder.AppendLine($"import {{ {messageInfoList.ModuleName} }} from \"./{messageInfoList.ModuleName}_{messageInfoList.Module}\";");
}

stringBuilder.AppendLine();
stringBuilder.AppendLine("export default class ProtoMessageRegister {\n public static register(): void{");

foreach (var messageInfoList in operationCodeInfo)
{
foreach (var messageInfo in messageInfoList.Infos)
{
if (!string.IsNullOrEmpty(messageInfo.ParentClass))
{
stringBuilder.AppendLine($"\t\t{messageInfoList.ModuleName}.{messageInfo.Name}.register();");
}
}
}

stringBuilder.AppendLine(" }\n}");
File.WriteAllText(launcherOptionsOutputPath + "/ProtoMessageRegister.ts", stringBuilder.ToString(), Encoding.UTF8);
}
}
}

0 comments on commit 616eb51

Please sign in to comment.