NLDClient-yudde/ProjectNLD/Assets/Editor/Tools/Proto/ProtoTools.cs

266 lines
9.0 KiB
C#
Raw Normal View History

2023-08-22 19:08:45 +08:00
//////////////////////////////////////////////////////////////////////////
//
// 文件Assets\Editor\Proto\ProtoTools.cs
// 作者Xoen Xie
// 时间2023/06/19
// 描述pb tools
// 说明:
//
//////////////////////////////////////////////////////////////////////////
using UnityEditor;
using UnityEngine;
using System.IO;
using System;
using System.Collections.Generic;
public static class ProtoTools
{
#if UNITY_EDITOR_WIN
static string PROTOC_DIR = Application.dataPath + "/../../../NLDMisc/proto/bin/win64/";
2023-08-22 19:08:45 +08:00
static string PROTOC = PROTOC_DIR + "protoc.exe";
#else
static string PROTOC_DIR = Application.dataPath + "/../../../NLDMisc/proto/bin/mac/";
2023-08-22 19:08:45 +08:00
static string PROTOC = PROTOC_DIR + "protoc";
#endif
static string PROTO_DIR = Application.dataPath + "/../../../NLDMisc/proto/";
2023-08-22 19:08:45 +08:00
2025-02-25 15:03:08 +08:00
[MenuItem("Tools/*proto/导出pb到csharp", false, (int)NLDMenuID.PBExportCS)]
2023-08-22 19:08:45 +08:00
public static void PBExportCS()
{
string outputDir = Application.dataPath + "/Code/Scripts/Gameplay/Net/Proto/";
string [] csProto = new string[] { "client.proto", "message_id.proto", "common.proto"};
2023-10-23 16:54:35 +08:00
// VersionControlSystem.Checkout(outputDir);
2023-08-22 19:08:45 +08:00
for (int i = 0; i < csProto.Length; i ++)
{
EditorUtil.StartProcess(PROTOC,
$"--csharp_out={outputDir} --proto_path=../../ {csProto[i]}", PROTOC_DIR);
}
Debug.Log("Export to csharp done...");
CSCodeGen();
Debug.Log("csharp code gen done...");
}
private static Gameplay.Net.EP GetEP(string msgID) {
if (msgID.Contains("Clt_"))
return Gameplay.Net.EP.Client;
else if (msgID.Contains("G_"))
return Gameplay.Net.EP.Game;
else if (msgID.Contains("L_"))
return Gameplay.Net.EP.Login;
else if (msgID.Contains("Gt_"))
return Gameplay.Net.EP.Gate;
else if (msgID.Contains("C_"))
return Gameplay.Net.EP.Center;
else if (msgID.Contains("Mg_"))
return Gameplay.Net.EP.Manager;
else
Debug.LogError("ep not handler, msgID=" + msgID);
return Gameplay.Net.EP.None;
}
private static Gameplay.Net.EP GetDestEP(string msgID)
{
if (msgID.Contains("_G_"))
return Gameplay.Net.EP.Game;
else if (msgID.Contains("_L_"))
return Gameplay.Net.EP.Login;
else if (msgID.Contains("_Gt_"))
return Gameplay.Net.EP.Gate;
2023-12-27 13:41:45 +08:00
else if (msgID.Contains("_Mg_"))
return Gameplay.Net.EP.Manager;
2023-08-22 19:08:45 +08:00
else
{
Debug.Log("ep not handler try again, msgID=" + msgID);
return GetEP(msgID);
}
}
2025-02-25 15:03:08 +08:00
[MenuItem("Tools/*proto/导出pb到go", false, (int)NLDMenuID.PBExportGO)]
2023-08-22 19:08:45 +08:00
public static void PBExportGo()
{
string[] protos = new string[] { "message_id.proto", "server/s_message_id.proto" };
string codeTemplate = Application.dataPath + "/Editor/Tools/Proto/MSGGoCode.txt";
string fileTemplate = Application.dataPath + "/Editor/Tools/Proto/MSGTemplateGO.txt";
string ftContent = File.ReadAllText(fileTemplate);
string cdContent = File.ReadAllText(codeTemplate);
string messagePoolStr = "";
List<string>[] ids = new List<string>[(int)Gameplay.Net.EP.Max];
for (int i = 0; i < (int)Gameplay.Net.EP.Max; i++) {
ids[i] = new List<string>();
}
for (int i = 0; i < protos.Length; i++)
{
string protoName = protos[i];
string protoPath = PROTO_DIR + "/" + protoName;
string[] lines = File.ReadAllLines(protoPath);
bool bGenCode = false;
for (int J = 0; J < lines.Length; J++)
{
if (bGenCode)
{
if (lines[J].Contains("MESSAGE_ID_END"))
{
break;
}
string rawMsgID = lines[J].Trim();
int index = rawMsgID.IndexOf("=");
if (index > 0)
{
string msgID = rawMsgID.Substring(0, index - 1);
msgID = msgID.Trim();
Debug.Log("Find Message ID:" + msgID);
var ep = GetEP(msgID);
ids[(int)ep].Add(msgID);
messagePoolStr += $"\txsf_net.SetMessage(new(MSG_{msgID}))\n";
}
}
else
{
if (lines[J].Contains("MESSAGE_ID_START"))
{
bGenCode = true;
}
}
}
}
for (int i = 0; i < (int)Gameplay.Net.EP.Max; i++)
{
var list = ids[i];
if (list.Count <= 0)
continue;
Gameplay.Net.EP curEP = (Gameplay.Net.EP)i;
string codes = "";
for (int J = 0; J < list.Count; ++J)
{
string msgID = list[J];
string goID = $"SMSGID_{msgID}";
string goEP = curEP.ToString();
if (curEP == Gameplay.Net.EP.Client)
{
goID = $"MSGID_{msgID}";
goEP = GetDestEP(msgID).ToString();
}
string c = cdContent.Replace("_MSG_NAME_", msgID);
c = c.Replace("_MSG_ID_NAME_", goID);
c = c.Replace("_MSG_EP_", goEP);
codes += c;
}
2024-01-30 12:44:07 +08:00
string codePath = Application.dataPath + $"/../../../NLDServer/message/messages/msg_{curEP.ToString().ToLower()}.go";
2023-08-22 19:08:45 +08:00
DateTime currentDateTime = DateTime.Now;
string date = currentDateTime.ToShortDateString();
string content = ftContent.Replace("_MSG_DATE_", date);
content += codes;
File.WriteAllText(codePath, content);
}
2024-01-30 12:44:07 +08:00
string codeMessagePool = Application.dataPath + "/../../../NLDServer/message/messages/messages.go";
2023-08-22 19:08:45 +08:00
EditorUtil.ReplaceContentByTag(codeMessagePool, "MESSAGE_START", "MESSAGE_END", messagePoolStr);
Debug.Log("Export to go done...");
}
private static void CSCodeGen()
{
string protoPath = PROTO_DIR + "/message_id.proto";
string codeTemplate = Application.dataPath + "/Editor/Tools/Proto/MSGTemplateCS.txt";
string TmpContent = File.ReadAllText(codeTemplate);
string [] lines = File.ReadAllLines(protoPath);
bool bGenCode = false;
string messagePoolStr = "";
for(int i = 0; i < lines.Length; i ++)
{
if(bGenCode)
{
if(lines[i].Contains("MESSAGE_ID_END"))
{
break;
}
string rawMsgID = lines[i].Trim();
int index = rawMsgID.IndexOf("=");
if(index > 0)
{
string msgID = rawMsgID.Substring(0, index-1);
msgID = msgID.Trim();
Debug.Log("Find Message ID:" + msgID);
string[] idParts = msgID.Split("_");
string idName = "";
for(int J = 0; J < idParts.Length; J++)
{
idName += EditorUtil.FirstLetterToUpper(idParts[J]);
}
string Desc = "";
int descIndex = rawMsgID.IndexOf("//");
if(descIndex > 0)
{
Desc = rawMsgID.Substring(descIndex+2);
}
string codePath = Application.dataPath + $"/Code/Scripts/Gameplay/Net/Messages/MSG_{msgID}.cs";
if(!File.Exists(codePath))
{
2023-10-23 16:54:35 +08:00
// VersionControlSystem.Checkout(Application.dataPath + "/Code/Scripts/Gameplay/Net/Messages");
2023-08-22 19:08:45 +08:00
string content = TmpContent.Replace("_MSG_NAME_", msgID);
DateTime currentDateTime = DateTime.Now;
string date = currentDateTime.ToShortDateString();
content = content.Replace("_MSG_DATE_", date);
content = content.Replace("_MSG_DESC_", Desc.Trim());
content = content.Replace("_MSG_ID_NAME_", idName);
File.WriteAllText(codePath, content);
}
messagePoolStr += $"\t\t\tm_MessagePool[(int)NLDMID.MSGID.{idName}] = new MSG_{msgID}();\n";
}
}
else
{
if(lines[i].Contains("MESSAGE_ID_START"))
{
bGenCode = true;
}
}
}
string codeMessagePool = Application.dataPath + "/Code/Scripts/Gameplay/Net/Client/NetManager.cs";
2023-10-23 16:54:35 +08:00
// VersionControlSystem.Checkout(codeMessagePool);
2023-08-22 19:08:45 +08:00
EditorUtil.ReplaceContentByTag(codeMessagePool, "MESSAGE_START", "MESSAGE_END", messagePoolStr);
}
}