Skip to content

Commit 616eb51

Browse files
committed
[修改]1. 修改TS的解析导出
1 parent f43b8b8 commit 616eb51

File tree

2 files changed

+75
-15
lines changed

2 files changed

+75
-15
lines changed

ProtoExport/Program.cs

+13
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,12 @@ private static int Main(string[] args)
3939
}
4040

4141
var files = Directory.GetFiles(launcherOptions.InputPath, "*.proto", SearchOption.AllDirectories);
42+
43+
var messageInfoLists = new List<MessageInfoList>(files.Length);
4244
foreach (var file in files)
4345
{
4446
var operationCodeInfo = MessageHelper.Parse(File.ReadAllText(file), Path.GetFileNameWithoutExtension(file), launcherOptions.OutputPath, launcherOptions.IsGenerateErrorCode);
47+
messageInfoLists.Add(operationCodeInfo);
4548
switch (modeType)
4649
{
4750
case ModeType.Server:
@@ -56,6 +59,16 @@ private static int Main(string[] args)
5659
}
5760
}
5861

62+
switch (modeType)
63+
{
64+
case ModeType.TypeScript:
65+
(ProtoGenerateHelper as ProtoBuffTypeScriptHelper)?.Post(messageInfoLists, launcherOptions.OutputPath);
66+
break;
67+
default:
68+
throw new ArgumentOutOfRangeException();
69+
}
70+
71+
5972
Console.WriteLine("导出成功,按任意键退出");
6073
Console.ReadKey();
6174
return 0;

ProtoExport/ProtoBuffTypeScriptHelper.cs

+62-15
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ public void Run(MessageInfoList messageInfoList, string outputPath, string names
1313
StringBuilder sb = new StringBuilder();
1414
sb.Append("import IRequestMessage from \"../network/IRequestMessage\";\n");
1515
sb.Append("import IResponseMessage from \"../network/IResponseMessage\";\n");
16+
sb.Append("import INotifyMessage from \"../network/INotifyMessage\";\n");
17+
sb.Append("import IHeartBeatMessage from \"../network/IHeartBeatMessage\";\n");
1618
sb.Append("import MessageObject from \"../network/MessageObject\";\n");
17-
sb.Append("import RegisterRequestMessageClass from \"../network/RegisterRequestMessageClass\";\n");
18-
sb.Append("import RegisterResponseMessageClass from \"../network/RegisterResponseMessageClass\";\n");
19-
// sb.Append($"namespace {namespaceName} {'{'}\n");
19+
sb.Append("import ProtoMessageHelper from \"../network/ProtoMessageHelper\";\n");
20+
sb.AppendLine();
21+
sb.Append($"export namespace {messageInfoList.ModuleName} {'{'}\n");
2022

2123
foreach (var operationCodeInfo in messageInfoList.Infos)
2224
{
@@ -42,14 +44,14 @@ public void Run(MessageInfoList messageInfoList, string outputPath, string names
4244
sb.Append($"\t/// <summary>\n");
4345
sb.Append($"\t/// {operationCodeInfo.Description}\n");
4446
sb.Append($"\t/// </summary>\n");
45-
if (operationCodeInfo.IsRequest)
46-
{
47-
sb.Append($"\t@RegisterRequestMessageClass({operationCodeInfo.Opcode})\n");
48-
}
49-
else
50-
{
51-
sb.Append($"\t@RegisterResponseMessageClass({operationCodeInfo.Opcode})\n");
52-
}
47+
// if (operationCodeInfo.IsRequest)
48+
// {
49+
// sb.Append($"\t@RegisterRequestMessageClass({operationCodeInfo.Opcode})\n");
50+
// }
51+
// else
52+
// {
53+
// sb.Append($"\t@RegisterResponseMessageClass({operationCodeInfo.Opcode})\n");
54+
// }
5355

5456
if (string.IsNullOrEmpty(operationCodeInfo.ParentClass))
5557
{
@@ -59,6 +61,26 @@ public void Run(MessageInfoList messageInfoList, string outputPath, string names
5961
{
6062
// sb.AppendLine($"\t[MessageTypeHandler({operationCodeInfo.Opcode})]");
6163
sb.AppendLine($"\texport class {operationCodeInfo.Name} extends MessageObject implements {operationCodeInfo.ParentClass} {'{'}");
64+
sb.AppendLine();
65+
66+
sb.Append($"\t\tpublic static register(): void{'{'}\n");
67+
if (operationCodeInfo.IsRequest)
68+
{
69+
sb.Append($"\t\t\tProtoMessageHelper.registerReqMessage('{messageInfoList.ModuleName}.{operationCodeInfo.Name}', {(messageInfoList.Module << 16) + operationCodeInfo.Opcode});\n");
70+
}
71+
else
72+
{
73+
sb.Append($"\t\t\tProtoMessageHelper.registerRespMessage('{messageInfoList.ModuleName}.{operationCodeInfo.Name}', {(messageInfoList.Module << 16) + operationCodeInfo.Opcode});\n");
74+
}
75+
76+
sb.Append($"\t\t{'}'}\n");
77+
78+
79+
sb.Append($"\t\tpublic get PackageName(): string{'{'}\n");
80+
sb.Append($"\t\t\treturn '{messageInfoList.ModuleName}.{operationCodeInfo.Name}';\n");
81+
sb.Append($"\t\t{'}'}\n");
82+
83+
sb.AppendLine();
6284
}
6385

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

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

91110
sb.AppendLine("\t}\n");
92111
}
93112
}
94113

95-
// sb.Append("}\n");
114+
sb.Append("}\n");
96115

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

140159
return typeCs;
141160
}
161+
162+
163+
public void Post(List<MessageInfoList> operationCodeInfo, string launcherOptionsOutputPath)
164+
{
165+
StringBuilder stringBuilder = new StringBuilder();
166+
167+
foreach (var messageInfoList in operationCodeInfo)
168+
{
169+
stringBuilder.AppendLine($"import {{ {messageInfoList.ModuleName} }} from \"./{messageInfoList.ModuleName}_{messageInfoList.Module}\";");
170+
}
171+
172+
stringBuilder.AppendLine();
173+
stringBuilder.AppendLine("export default class ProtoMessageRegister {\n public static register(): void{");
174+
175+
foreach (var messageInfoList in operationCodeInfo)
176+
{
177+
foreach (var messageInfo in messageInfoList.Infos)
178+
{
179+
if (!string.IsNullOrEmpty(messageInfo.ParentClass))
180+
{
181+
stringBuilder.AppendLine($"\t\t{messageInfoList.ModuleName}.{messageInfo.Name}.register();");
182+
}
183+
}
184+
}
185+
186+
stringBuilder.AppendLine(" }\n}");
187+
File.WriteAllText(launcherOptionsOutputPath + "/ProtoMessageRegister.ts", stringBuilder.ToString(), Encoding.UTF8);
188+
}
142189
}
143190
}

0 commit comments

Comments
 (0)