using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using System.Text; using System.IO; public class NetPkgCtl { private static List _typeLst = null; //发送接收封包存储列表 private static List _tempNetPkgLst = null;//暂停时缓存的封包列表 public static bool IsStopRec = false; public static bool getNewPackage = false; /// /// 添加封包到封包查看工具 /// /// /// public static void AddNetPkg(string pkgByte, NetPkgType netPkgType, string _packName, string _fields, List _fieldDetails) { #if UNITY_EDITOR if (_typeLst == null) { _typeLst = new List(); } if (_tempNetPkgLst == null) { _tempNetPkgLst = new List(); } getNewPackage = true; NetPkg netPkg = new NetPkg(); netPkg.SendOrGetTime = DateTime.Now.ToString("HH:mm:ss:fff"); netPkg.NetPkgTp = netPkgType; string netPkgStr = pkgByte.Replace(",", " "); netPkg.GameNetPkgStr = netPkgStr; var gameNetPkgName = _packName; string[] byteStr = netPkgStr.Split(' '); if (string.IsNullOrEmpty(gameNetPkgName)) { if (byteStr.Length > 1) { StringBuilder _stringBuilder = new StringBuilder(); _stringBuilder.Append("H"); _stringBuilder.Append(byteStr[0]); _stringBuilder.Append(byteStr[1]); _stringBuilder.Append("(未注册)"); netPkg.GameNetName = _stringBuilder.ToString(); _stringBuilder = null; } } else { netPkg.GameNetName = gameNetPkgName; netPkg.fields = _fields; netPkg.fieldDetails = _fieldDetails; } if (!IsStopRec) { _typeLst.Add(netPkg); } else { _tempNetPkgLst.Add(netPkg); } #endif } public static List GetNetPkg() { return _typeLst; } public static List GetTempNetPkg() { return _tempNetPkgLst; } public static void WriteAllNetLog() { if (_typeLst != null) { var count = 0; var lines = new List(); for (int i = _typeLst.Count - 1; i >= 0; i--) { if (count > 20000) { break; } var package = _typeLst[i]; var line = string.Empty; line = StringUtility.Contact(package.NetPkgTp == NetPkgType.Client ? "【发送】" : "【接收】", package.SendOrGetTime, ":", package.GameNetName, "\r\n"); if (package.fieldDetails != null) { for (int j = 0; j < package.fieldDetails.Count; j++) { line = StringUtility.Contact(line, "\t\t\t", package.fieldDetails[j], "\r\n"); } } lines.Add(line); count++; } File.WriteAllLines(Application.dataPath + "/PackageLogs" + "_" + DateTime.Now.ToString("HH_mm_ss") + ".txt", lines.ToArray()); } } } public class NetPkg { public string SendOrGetTime; public string GameNetPkgStr; public string GameNetName; public NetPkgType NetPkgTp; public string fields; public List fieldDetails; } public enum NetPkgType { Client = 1, Server = 2, All = 3, }