yyl
昨天 5d3366f2e0f687995eb7ad2107c4379fe7acd4e8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Text;
using System.IO;
 
public class NetPkgCtl
{
    private static List<NetPackage> packages = new List<NetPackage>(); //发送接收封包存储列表
    private static List<NetPackage> tempPackages = new List<NetPackage>();//暂停时缓存的封包列表
    public static bool stopRecieve = false;
 
    public static void RecordPackage(ServerType socketType, string bytes, NetPackagetType packType, string packageName, string fields, List<string> fieldDetails)
    {
#if UNITY_EDITOR
        var package = new NetPackage();
        package.socketType = socketType;
        package.time = DateTime.Now.ToString("HH:mm:ss:fff");
        package.type = packType;
 
        if (string.IsNullOrEmpty(packageName))
        {
            package.content = bytes.Replace(",", " ");
            var byteStr = package.content.Split(' ');
            if (byteStr.Length > 1)
            {
                package.name = StringUtility.Contact("H", byteStr[0], byteStr[1], "(未注册)");
            }
        }
        else
        {
            package.content = bytes.Replace(",", " ");
            package.name = packageName;
            package.fields = fields;
            package.fieldDetails = fieldDetails;
        }
 
        if (!stopRecieve)
        {
            packages.Add(package);
        }
        else
        {
            tempPackages.Add(package);
        }
#endif
    }
 
    public static List<NetPackage> GetPackages()
    {
        return packages;
    }
 
    public static List<NetPackage> GetTempPackages()
    {
        return tempPackages;
    }
 
    public static void WriteAllNetLog(bool @details)
    {
        if (packages != null)
        {
            var count = 0;
            var lines = new List<string>();
            for (int i = packages.Count - 1; i >= 0; i--)
            {
                if (count > 20000)
                {
                    break;
                }
 
                var package = packages[i];
                var line = string.Empty;
                line = StringUtility.Contact(package.type == NetPackagetType.Client ? "【发送】\t" : "【接收】\t", package.time, ":", package.name, @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 NetPackage
{
    public ServerType socketType;
    public bool show = false;
    public string time;
    public string serverTime;
    public string content;
    public string name;
    public NetPackagetType type;
    public string fields;
    public List<string> fieldDetails;
}
 
public enum NetPackagetType
{
    Client = 1,
    Server = 2,
    All = 3,
}