using System.Collections;
|
using System.Collections.Generic;
|
using UnityEngine;
|
using System;
|
using System.Text;
|
using System.IO;
|
|
public class NetPkgCtl
|
{
|
private static List<NetPkg> _typeLst = null; //发送接收封包存储列表
|
private static List<NetPkg> _tempNetPkgLst = null;//暂停时缓存的封包列表
|
public static bool IsStopRec = false;
|
public static bool getNewPackage = false;
|
|
/// <summary>
|
/// 添加封包到封包查看工具
|
/// </summary>
|
/// <param name="gameNetPkg"></param>
|
/// <param name="typeLst"></param>
|
public static void AddNetPkg(string pkgByte, NetPkgType netPkgType, string _packName, string _fields, List<string> _fieldDetails)
|
{
|
#if UNITY_EDITOR
|
if (_typeLst == null)
|
{
|
_typeLst = new List<NetPkg>();
|
}
|
if (_tempNetPkgLst == null)
|
{
|
_tempNetPkgLst = new List<NetPkg>();
|
}
|
|
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<NetPkg> GetNetPkg()
|
{
|
return _typeLst;
|
}
|
|
public static List<NetPkg> GetTempNetPkg()
|
{
|
return _tempNetPkgLst;
|
}
|
|
public static void WriteAllNetLog(bool @details)
|
{
|
if (_typeLst != null)
|
{
|
var count = 0;
|
var lines = new List<string>();
|
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 ? "【发送】\t" : "【接收】\t", package.SendOrGetTime, ":", package.GameNetName, @details ? "\r\n" : "-->" + package.fields);
|
|
if (@details && 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_" + (@details ? "details" : "abstract") + "_" + 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<string> fieldDetails;
|
|
}
|
|
public enum NetPkgType
|
{
|
Client = 1,
|
Server = 2,
|
All = 3,
|
}
|
|