mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
DotNet支持自定义协议目录
DotNet支持自定义协议目录
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
#if TENGINE_NET
|
#if TENGINE_NET
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using TEngine.Core.Network;
|
using TEngine.Core.Network;
|
||||||
|
|
||||||
#pragma warning disable CS8604
|
#pragma warning disable CS8604
|
||||||
#pragma warning disable CS8602
|
#pragma warning disable CS8602
|
||||||
#pragma warning disable CS8600
|
#pragma warning disable CS8600
|
||||||
@@ -49,6 +50,21 @@ public sealed class ProtoBufExporter
|
|||||||
Directory.CreateDirectory(Define.ProtoBufClientDirectory);
|
Directory.CreateDirectory(Define.ProtoBufClientDirectory);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!Directory.Exists($"{Define.ProtoBufDirectory}Inner"))
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory($"{Define.ProtoBufDirectory}Inner");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Directory.Exists($"{Define.ProtoBufDirectory}Outer"))
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory($"{Define.ProtoBufDirectory}Outer");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Directory.Exists($"{Define.ProtoBufDirectory}Bson"))
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory($"{Define.ProtoBufDirectory}Bson");
|
||||||
|
}
|
||||||
|
|
||||||
var tasks = new Task[2];
|
var tasks = new Task[2];
|
||||||
tasks[0] = Task.Run(RouteType);
|
tasks[0] = Task.Run(RouteType);
|
||||||
tasks[1] = Task.Run(async () =>
|
tasks[1] = Task.Run(async () =>
|
||||||
@@ -63,6 +79,7 @@ public sealed class ProtoBufExporter
|
|||||||
|
|
||||||
private async Task Start(ProtoBufOpCodeType opCodeType)
|
private async Task Start(ProtoBufOpCodeType opCodeType)
|
||||||
{
|
{
|
||||||
|
List<string> files = new List<string>();
|
||||||
var protoFile = "";
|
var protoFile = "";
|
||||||
var opCodeName = "";
|
var opCodeName = "";
|
||||||
var parameter = "";
|
var parameter = "";
|
||||||
@@ -73,7 +90,7 @@ public sealed class ProtoBufExporter
|
|||||||
string customRouteType = null;
|
string customRouteType = null;
|
||||||
_opcodes.Clear();
|
_opcodes.Clear();
|
||||||
var file = new StringBuilder();
|
var file = new StringBuilder();
|
||||||
var saveDirectory = new Dictionary<string,string>();
|
var saveDirectory = new Dictionary<string, string>();
|
||||||
|
|
||||||
switch (opCodeType)
|
switch (opCodeType)
|
||||||
{
|
{
|
||||||
@@ -89,6 +106,8 @@ public sealed class ProtoBufExporter
|
|||||||
protoFile = $"{Define.ProtoBufDirectory}OuterMessage.proto";
|
protoFile = $"{Define.ProtoBufDirectory}OuterMessage.proto";
|
||||||
saveDirectory.Add(Define.ProtoBufServerDirectory, _serverTemplate);
|
saveDirectory.Add(Define.ProtoBufServerDirectory, _serverTemplate);
|
||||||
saveDirectory.Add(Define.ProtoBufClientDirectory, _clientTemplate);
|
saveDirectory.Add(Define.ProtoBufClientDirectory, _clientTemplate);
|
||||||
|
files.Add(protoFile);
|
||||||
|
files.AddRange(Directory.GetFiles($"{Define.ProtoBufDirectory}Outer").ToList());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case ProtoBufOpCodeType.Inner:
|
case ProtoBufOpCodeType.Inner:
|
||||||
@@ -103,6 +122,8 @@ public sealed class ProtoBufExporter
|
|||||||
opCodeName = "InnerOpcode";
|
opCodeName = "InnerOpcode";
|
||||||
protoFile = $"{Define.ProtoBufDirectory}InnerMessage.proto";
|
protoFile = $"{Define.ProtoBufDirectory}InnerMessage.proto";
|
||||||
saveDirectory.Add(Define.ProtoBufServerDirectory, _serverTemplate);
|
saveDirectory.Add(Define.ProtoBufServerDirectory, _serverTemplate);
|
||||||
|
files.Add(protoFile);
|
||||||
|
files.AddRange(Directory.GetFiles($"{Define.ProtoBufDirectory}Inner").ToList());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case ProtoBufOpCodeType.InnerBson:
|
case ProtoBufOpCodeType.InnerBson:
|
||||||
@@ -117,11 +138,17 @@ public sealed class ProtoBufExporter
|
|||||||
opCodeName = "InnerBsonOpcode";
|
opCodeName = "InnerBsonOpcode";
|
||||||
protoFile = $"{Define.ProtoBufDirectory}InnerBsonMessage.proto";
|
protoFile = $"{Define.ProtoBufDirectory}InnerBsonMessage.proto";
|
||||||
saveDirectory.Add(Define.ProtoBufServerDirectory, _serverTemplate);
|
saveDirectory.Add(Define.ProtoBufServerDirectory, _serverTemplate);
|
||||||
|
files.Add(protoFile);
|
||||||
|
files.AddRange(Directory.GetFiles($"{Define.ProtoBufDirectory}Bson").ToList());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var protoFileText = await File.ReadAllTextAsync(protoFile);
|
|
||||||
|
|
||||||
|
foreach (var filePath in files)
|
||||||
|
{
|
||||||
|
var protoFileText = await File.ReadAllTextAsync(filePath);
|
||||||
|
|
||||||
foreach (var line in protoFileText.Split('\n'))
|
foreach (var line in protoFileText.Split('\n'))
|
||||||
{
|
{
|
||||||
@@ -134,7 +161,7 @@ public sealed class ProtoBufExporter
|
|||||||
|
|
||||||
if (currentLine.StartsWith("///"))
|
if (currentLine.StartsWith("///"))
|
||||||
{
|
{
|
||||||
file.AppendFormat(" /// <summary>\r\n" + " /// {0}\r\n" + " /// </summary>\r\n", currentLine.TrimStart(new[] {'/', '/', '/'}));
|
file.AppendFormat(" /// <summary>\r\n" + " /// {0}\r\n" + " /// </summary>\r\n", currentLine.TrimStart(new[] { '/', '/', '/' }));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -144,11 +171,11 @@ public sealed class ProtoBufExporter
|
|||||||
opcodeInfo = new OpcodeInfo();
|
opcodeInfo = new OpcodeInfo();
|
||||||
file.AppendLine("\t[ProtoContract]");
|
file.AppendLine("\t[ProtoContract]");
|
||||||
className = currentLine.Split(Define.SplitChars, StringSplitOptions.RemoveEmptyEntries)[1];
|
className = currentLine.Split(Define.SplitChars, StringSplitOptions.RemoveEmptyEntries)[1];
|
||||||
var splits = currentLine.Split(new[] {"//"}, StringSplitOptions.RemoveEmptyEntries);
|
var splits = currentLine.Split(new[] { "//" }, StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
|
||||||
if (splits.Length > 1)
|
if (splits.Length > 1)
|
||||||
{
|
{
|
||||||
var parameterArray = currentLine.Split(new[] {"//"}, StringSplitOptions.RemoveEmptyEntries)[1].Trim().Split(',');
|
var parameterArray = currentLine.Split(new[] { "//" }, StringSplitOptions.RemoveEmptyEntries)[1].Trim().Split(',');
|
||||||
parameter = parameterArray[0].Trim();
|
parameter = parameterArray[0].Trim();
|
||||||
|
|
||||||
switch (parameterArray.Length)
|
switch (parameterArray.Length)
|
||||||
@@ -302,7 +329,7 @@ public sealed class ProtoBufExporter
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var csName = $"{Path.GetFileNameWithoutExtension(protoFile)}.cs";
|
var csName = $"{Path.GetFileNameWithoutExtension(filePath)}.cs";
|
||||||
|
|
||||||
foreach (var (directory, template) in saveDirectory)
|
foreach (var (directory, template) in saveDirectory)
|
||||||
{
|
{
|
||||||
@@ -312,6 +339,9 @@ public sealed class ProtoBufExporter
|
|||||||
}
|
}
|
||||||
|
|
||||||
file.Clear();
|
file.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
#region GenerateOpCode
|
||||||
|
|
||||||
file.AppendLine("namespace TEngine");
|
file.AppendLine("namespace TEngine");
|
||||||
file.AppendLine("{");
|
file.AppendLine("{");
|
||||||
@@ -333,6 +363,8 @@ public sealed class ProtoBufExporter
|
|||||||
var csFile = Path.Combine(directory, $"{opCodeName}.cs");
|
var csFile = Path.Combine(directory, $"{opCodeName}.cs");
|
||||||
await File.WriteAllTextAsync(csFile, file.ToString());
|
await File.WriteAllTextAsync(csFile, file.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task RouteType()
|
private async Task RouteType()
|
||||||
@@ -353,7 +385,7 @@ public sealed class ProtoBufExporter
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
var splits = currentLine.Split(new[] {"//"}, StringSplitOptions.RemoveEmptyEntries);
|
var splits = currentLine.Split(new[] { "//" }, StringSplitOptions.RemoveEmptyEntries);
|
||||||
var routeTypeStr = splits[0].Split("=", StringSplitOptions.RemoveEmptyEntries);
|
var routeTypeStr = splits[0].Split("=", StringSplitOptions.RemoveEmptyEntries);
|
||||||
routeTypeFileSb.Append($"\t\t{routeTypeStr[0].Trim()} = {routeTypeStr[1].Trim()},");
|
routeTypeFileSb.Append($"\t\t{routeTypeStr[0].Trim()} = {routeTypeStr[1].Trim()},");
|
||||||
|
|
||||||
@@ -468,12 +500,12 @@ public sealed class ProtoBufExporter
|
|||||||
flag = 1;
|
flag = 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
else if(trim.StartsWith("#else"))
|
else if (trim.StartsWith("#else"))
|
||||||
{
|
{
|
||||||
flag = 2;
|
flag = 2;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
else if(trim.StartsWith($"#endif"))
|
else if (trim.StartsWith($"#endif"))
|
||||||
{
|
{
|
||||||
flag = 0;
|
flag = 0;
|
||||||
continue;
|
continue;
|
||||||
|
Reference in New Issue
Block a user