Skip to content

Commit 357d247

Browse files
committedFeb 8, 2025·
[增加]1. 增加消息的命名规范约束
1 parent a24a980 commit 357d247

File tree

2 files changed

+55
-15
lines changed

2 files changed

+55
-15
lines changed
 

‎ProtoExport/MessageHelper.cs

+39-15
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,15 @@ public static MessageInfoList Parse(string proto, string fileName, string filePa
5252
throw new Exception("Module not found==>example: option module = 100");
5353
}
5454

55-
Console.WriteLine($"Package: {packageMatch.Groups[1].Value} => Module: {moduleMatch.Groups[1].Value}");
55+
var packageName = packageMatch.Groups[1].Value;
56+
Console.WriteLine($"Package: {packageName} => Module: {moduleMatch.Groups[1].Value}");
5657
// 使用正则表达式提取枚举类型
57-
ParseEnum(proto, messageInfo.Infos);
58+
ParseEnum(proto, packageName, messageInfo.Infos);
5859

5960
// 使用正则表达式提取消息类型
60-
ParseMessage(proto, messageInfo.Infos, isGenerateErrorCode);
61+
ParseMessage(proto, packageName, messageInfo.Infos, isGenerateErrorCode);
6162

62-
ParseComment(proto, messageInfo.Infos);
63+
ParseComment(proto, packageName, messageInfo.Infos);
6364

6465
// 消息码排序配对
6566
MessageIdHandler(messageInfo.Infos, 10);
@@ -92,7 +93,7 @@ private static void MessageIdHandler(List<MessageInfo> operationCodeInfos, int s
9293
}
9394
}
9495

95-
private static void ParseComment(string proto, List<MessageInfo> operationCodeInfos)
96+
private static void ParseComment(string proto, string packageName, List<MessageInfo> operationCodeInfos)
9697
{
9798
MatchCollection enumMatches = Regex.Matches(proto, CommentPattern, RegexOptions.Singleline);
9899
foreach (Match match in enumMatches)
@@ -113,14 +114,19 @@ private static void ParseComment(string proto, List<MessageInfo> operationCodeIn
113114
}
114115
}
115116

116-
private static void ParseEnum(string proto, List<MessageInfo> codes)
117+
private static void ParseEnum(string proto, string packageName, List<MessageInfo> codes)
117118
{
118119
MatchCollection enumMatches = Regex.Matches(proto, EnumPattern, RegexOptions.Singleline);
119120
foreach (Match match in enumMatches)
120121
{
121122
MessageInfo info = new MessageInfo(true);
122123
codes.Add(info);
123124
string blockName = match.Groups[1].Value;
125+
if (!Utility.IsCamelCase(blockName))
126+
{
127+
throw new Exception($"[{packageName}] 包的 [{blockName}] 枚举名称必须遵守 [Upper Camel Case 命名规则]\n");
128+
}
129+
124130
info.Name = blockName;
125131
// Console.WriteLine("Enum Name: " + match.Groups[1].Value);
126132
// Console.WriteLine("Contents: " + match.Groups[2].Value);
@@ -143,25 +149,34 @@ private static void ParseEnum(string proto, List<MessageInfo> codes)
143149
var fieldSplit = fieldType.Split('=', StringSplitOptions.RemoveEmptyEntries);
144150
if (fieldSplit.Length > 1)
145151
{
146-
field.Type = fieldSplit[0].Trim();
152+
var name = fieldSplit[0].Trim();
153+
if (!Utility.IsCamelCase(name))
154+
{
155+
throw new Exception($"[{packageName}] 包的 {name} 枚举字段名称必须遵守 [Upper Camel Case 命名规则]\n");
156+
}
157+
158+
field.Type = name;
147159
field.Members = int.Parse(fieldSplit[1].Replace(";", "").Trim());
148160
}
149161
}
150162
}
151163
}
152164
}
153165

154-
private static void ParseMessage(string proto, List<MessageInfo> codes, bool isGenerateErrorCode = false)
166+
private static void ParseMessage(string proto, string packageName, List<MessageInfo> codes, bool isGenerateErrorCode = false)
155167
{
156168
MatchCollection messageMatches = Regex.Matches(proto, MessagePattern, RegexOptions.Singleline);
157169
foreach (Match match in messageMatches)
158170
{
159171
string messageName = match.Groups[1].Value;
160-
// Console.WriteLine("Message Name: " + match.Groups[1].Value);
161-
// Console.WriteLine("Contents: " + match.Groups[2].Value);
162172
var blockContent = match.Groups[2].Value.Trim();
163173
MessageInfo info = new MessageInfo();
164174
codes.Add(info);
175+
if (!Utility.IsCamelCase(messageName))
176+
{
177+
throw new Exception($"[{packageName}] 包的 [{messageName}] 消息名称必须遵守 [Upper Camel Case 命名规则]\n");
178+
}
179+
165180
info.Name = messageName;
166181
foreach (var line in blockContent.Split(new string[] { "\r", "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries))
167182
{
@@ -204,13 +219,25 @@ private static void ParseMessage(string proto, List<MessageInfo> codes, bool isG
204219
}
205220
}
206221

222+
var name = fieldSplitStrings[2].Trim();
223+
224+
if (!Utility.IsCamelCase(name))
225+
{
226+
throw new Exception($"[{packageName}] 包的 [{messageName}] 消息的 [{name}] 字段名称必须遵守 [Upper Camel Case 命名规则]\n");
227+
}
207228

208-
field.Name = fieldSplitStrings[2].Trim();
229+
field.Name = name;
209230
}
210231
else if (fieldSplitStrings.Length > 1)
211232
{
212233
field.Type = Utility.ConvertType(fieldSplitStrings[0].Trim());
213-
field.Name = fieldSplitStrings[1].Trim();
234+
var name = fieldSplitStrings[1].Trim();
235+
if (!Utility.IsCamelCase(name))
236+
{
237+
throw new Exception($"[{packageName}] 包的 [{messageName}] 消息的 [{name}] 字段名称必须遵守 [Upper Camel Case 命名规则]\n");
238+
}
239+
240+
field.Name = name;
214241
}
215242
}
216243
}
@@ -227,7 +254,4 @@ private static void ParseMessage(string proto, List<MessageInfo> codes, bool isG
227254
}
228255
}
229256
}
230-
231-
[GeneratedRegex(ModulePattern, RegexOptions.Singleline)]
232-
private static partial Regex MyRegex();
233257
}

‎ProtoExport/Utility.cs

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Text.RegularExpressions;
2+
13
namespace GameFrameX.ProtoExport
24
{
35
internal static class Utility
@@ -6,6 +8,20 @@ internal static class Utility
68

79
public static readonly string[] splitNotesChars = { "//" };
810

11+
12+
/// <summary>
13+
///
14+
/// </summary>
15+
/// <param name="str"></param>
16+
/// <returns></returns>
17+
public static bool IsCamelCase(string str)
18+
{
19+
// 定义一个正则表达式来匹配 Upper Camel Case 命名规则
20+
string pattern = @"^[A-Z][a-zA-Z]*$";
21+
22+
return Regex.IsMatch(str, pattern);
23+
}
24+
925
public static string ConvertType(string type)
1026
{
1127
string typeCs = "";

0 commit comments

Comments
 (0)
Please sign in to comment.