////////////////////////////////////////////////////////////////////////// // // 文件: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/"; static string PROTOC = PROTOC_DIR + "protoc.exe"; #else static string PROTOC_DIR = Application.dataPath + "/../../../NLDMisc/proto/bin/mac/"; static string PROTOC = PROTOC_DIR + "protoc"; #endif static string PROTO_DIR = Application.dataPath + "/../../../NLDMisc/proto/"; [MenuItem("Tools/*proto/导出pb到csharp", false, (int)NLDMenuID.PBExportCS)] public static void PBExportCS() { string outputDir = Application.dataPath + "/Code/Scripts/Gameplay/Net/Proto/"; string [] csProto = new string[] { "client.proto", "message_id.proto", "common.proto"}; // VersionControlSystem.Checkout(outputDir); 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; else if (msgID.Contains("_Mg_")) return Gameplay.Net.EP.Manager; else { Debug.Log("ep not handler try again, msgID=" + msgID); return GetEP(msgID); } } [MenuItem("Tools/*proto/导出pb到go", false, (int)NLDMenuID.PBExportGO)] 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[] ids = new List[(int)Gameplay.Net.EP.Max]; for (int i = 0; i < (int)Gameplay.Net.EP.Max; i++) { ids[i] = new List(); } 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; } string codePath = Application.dataPath + $"/../../../NLDServer/message/messages/msg_{curEP.ToString().ToLower()}.go"; DateTime currentDateTime = DateTime.Now; string date = currentDateTime.ToShortDateString(); string content = ftContent.Replace("_MSG_DATE_", date); content += codes; File.WriteAllText(codePath, content); } string codeMessagePool = Application.dataPath + "/../../../NLDServer/message/messages/messages.go"; 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)) { // VersionControlSystem.Checkout(Application.dataPath + "/Code/Scripts/Gameplay/Net/Messages"); 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"; // VersionControlSystem.Checkout(codeMessagePool); EditorUtil.ReplaceContentByTag(codeMessagePool, "MESSAGE_START", "MESSAGE_END", messagePoolStr); } }