This repository has been archived by the owner on May 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCharacter.cs
104 lines (88 loc) · 3.04 KB
/
Character.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace PacketAnalyzer
{
public class Character
{
static Dictionary<ulong, string> storage = new Dictionary<ulong, string>();
static FileStream fs = null;
const int packetLength = 2 + 4 + 32;
public static async void Init(string path)
{
fs = File.Open(path, FileMode.OpenOrCreate);
byte[] buffer = new byte[packetLength];
while (await fs.ReadAsync(buffer, 0, packetLength) == packetLength)
{
ushort server = BitConverter.ToUInt16(buffer, 0);
uint id = BitConverter.ToUInt32(buffer, 2);
string name = Encoding.UTF8.GetString(buffer, 6, 32).TrimEnd((char)0);
var key = getKey(server, id);
if (storage.ContainsKey(key))
{
storage[key] = name;
} else
{
storage.Add(key, name);
}
}
}
public static void AddToFile(ushort server, uint id, string name)
{
if (fs == null) return;
// Save only players
if ((id & 0x10000000) != 0x10000000) return;
byte[] buffer = new byte[packetLength + 8];
BitConverter.GetBytes(server).CopyTo(buffer, 0);
BitConverter.GetBytes(id).CopyTo(buffer, 2);
Encoding.UTF8.GetBytes(name).CopyTo(buffer, 6);
fs.Write(buffer, 0, packetLength);
fs.Flush(true);
}
private static ulong getKey(ushort server, uint id)
{
return ((ulong)server << 32) | id;
}
public static void Ensure(Network.FFXIVChinaServer server, uint id, string name)
{
Ensure((ushort)server, id, name);
}
public static void Ensure(ushort server, uint id, string name)
{
ulong key = getKey(server, id);
if (storage.ContainsKey(key))
{
if (storage[key] == name) return;
storage[key] = name;
}
else
{
storage.Add(key, name);
}
AddToFile(server, id, name);
}
public static string Get(ushort server, uint id)
{
ulong key = getKey(server, id);
if (storage.TryGetValue(key, out var name))
{
return name;
}
else
{
return "(unknown)";
}
}
public static string Dump()
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("Count: {0}\r\n", storage.Count);
foreach (var pair in storage) {
var server = Enum.GetName(typeof(Network.FFXIVChinaServer), (ushort)(pair.Key >> 32));
sb.AppendFormat("{1:X8} <{0}> {2}\r\n", server, pair.Key & 0xFFFFFFFF, pair.Value);
}
return sb.ToString();
}
}
}