forked from sruon/ffxivlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Entry.cs
321 lines (302 loc) · 12.7 KB
/
Entry.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
namespace ffxivlib
{
public partial class Chatlog
{
public class Entry
{
#region Properties
public byte[] Raw { get; set; }
public DateTime Timestamp { get; set; }
public string Code { get; set; }
public string Text { get; set; }
public string RawString { get; set; }
public byte[] RawModified { get; set; }
#endregion
#region Constructor
/// <summary>
/// Builds a chatlog entry out of a byte array
/// The implementation is sketchy at best but it should be reliable enough
/// </summary>
/// <param name="raw">The array to process</param>
public Entry(byte[] raw)
{
Raw = raw;
RawString = Encoding.UTF8.GetString(raw);
ProcessEntry(raw);
}
#endregion
#region Private methods
/// <summary>
/// Main processing function
/// This extracts the timestamp, code and process the text to clean it.
/// </summary>
/// <param name="raw">The array to process</param>
private void ProcessEntry(byte[] raw)
{
List<byte> workingCopy = raw.ToList();
if (raw.Length < Constants.TIMESTAMP_SIZE + Constants.CHATCODE_SIZE)
return;
try
{
Timestamp = GetTimeStamp(int.Parse(
Encoding.UTF8.GetString(
workingCopy.ToArray()
).Substring(0, Constants.TIMESTAMP_SIZE),
NumberStyles.HexNumber));
}
catch
{
return;
}
workingCopy.RemoveRange(0, 8);
Code = Encoding.UTF8.GetString(workingCopy.ToArray(), 0, Constants.CHATCODE_SIZE);
workingCopy.RemoveRange(0, 4);
int sep = workingCopy[1] == ':' ? 2 : 1; // Removes :: separators
workingCopy.RemoveRange(0, sep);
workingCopy = ReplaceAutotranslate(workingCopy);
workingCopy = CleanFormat(workingCopy);
workingCopy = CleanName(workingCopy);
workingCopy = CleanMob(workingCopy);
workingCopy = CleanHQ(workingCopy);
workingCopy = CleanEmote(workingCopy);
RawModified = workingCopy.ToArray();
Text = CleanString(Encoding.UTF8.GetString(workingCopy.ToArray()));
}
/// <summary>
/// Replaces auto-translate.
/// TODO: Use a fucking regex.
/// </summary>
/// <param name="workingCopy"></param>
/// <returns></returns>
private static List<byte> ReplaceAutotranslate(List<byte> workingCopy)
{
//I have plans [02 2E] OPEN [05] COUNT [05] CATEGORY [F2 01 F7] ID [03] CLOSE
var openATPattern = new List<byte>
{
0x02,
0x2E
};
var closeAT = 0x03;
int[] idx = workingCopy.Select((b, i) => b == openATPattern[0] ? i : -1).Where(i => i != -1).ToArray();
foreach (int i in idx)
{
int cpy = i;
if (workingCopy[++cpy] == openATPattern[1])
{
List<byte> extracted = new List<byte>();
byte count = workingCopy[++cpy];
while (workingCopy[++cpy] != closeAT || extracted.Count == 0)
{
extracted.Add(workingCopy[cpy]);
}
byte category = extracted[0];
extracted.RemoveAt(0);
if (extracted[0] == 0xF0 || extracted[0] == 0xF2 || extracted[0] == 0xF4 || extracted[0] == 0xF6)
{
extracted.RemoveAt(0);
}
int atId = 0;
foreach (byte b in extracted)
{
atId <<= 8;
atId |= b;
}
string replacement;
// Let's not raise if user doesn't have resource files
try
{
replacement = string.Format("{{{0}}}", ResourceParser.GetAutotranslate(category, atId));
}
catch (Exception)
{
replacement = string.Format("{{[{0} {1}]}}", category, atId);
}
workingCopy.RemoveRange(i, count + 3);
workingCopy.InsertRange(i, Encoding.UTF8.GetBytes(replacement));
return ReplaceAutotranslate(workingCopy);
}
}
return workingCopy;
}
/// <summary>
/// Removes any invalid character left
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
private static string CleanString(string input)
{
return new string(input.Where(value =>
(value >= 0x0020 && value <= 0xD7FF) ||
(value >= 0xE000 && value <= 0xFFFD) ||
value == 0x0009 ||
value == 0x000A ||
value == 0x000D).ToArray());
}
/// <summary>
/// Removes junk around emotes that's only useful to the client.
/// </summary>
/// <param name="workingCopy"></param>
/// <returns></returns>
private static List<byte> CleanEmote(List<byte> workingCopy)
{
var openEmotePattern = new List<byte>
{
0x02,
0x27,
0x12,
0x01,
0x01,
0x01,
0x01,
0xFF
};
var closeEmotePattern = new List<byte>
{
0x02,
0x27,
0x07,
0xCF,
0x01,
0x01,
0x01,
0xFF,
0x01,
0x03
};
int emoteIndex;
while ((emoteIndex = Library.ByteSearch(workingCopy.ToArray(), openEmotePattern.ToArray())) != -1)
{
int i = workingCopy.FindIndex(item => item == 0x03);
if (i != -1)
{
int limit = (i - emoteIndex);
if (limit < 0) return workingCopy;
workingCopy.RemoveRange(emoteIndex, limit);
if ((emoteIndex = Library.ByteSearch(workingCopy.ToArray(), closeEmotePattern.ToArray())) != -1)
{
workingCopy.RemoveRange(emoteIndex, closeEmotePattern.Count);
}
}
}
return workingCopy;
}
/// <summary>
/// Removes junk around NPC names that's only useful to the client.
/// </summary>
/// <param name="workingCopy"></param>
/// <returns></returns>
private static List<byte> CleanMob(List<byte> workingCopy)
{
var pattern = new List<byte>
{
0x20,
0x20,
0xEE,
0x81,
0xAF,
0x20
};
if (workingCopy.Count <= 0)
return workingCopy;
if (pattern.Where((t, i) => workingCopy[i] != t).Any())
return workingCopy;
workingCopy.RemoveRange(0, pattern.Count);
return workingCopy;
}
/// <summary>
/// This replaces the HQ icon 0xEE 0x80 0xBC by a simple HQ 0x48 0x51
/// This unfortunately isn't very reliable as it might replaces other icons used by SE.
/// </summary>
/// <param name="workingCopy"></param>
/// <returns></returns>
private static List<byte> CleanHQ(List<byte> workingCopy)
{
var pattern = new List<byte>
{
0xEE,
0x80,
0xBC
};
var hqRep = new List<byte>
{
0x48,
0x51
};
int i = workingCopy.FindIndex(item => item == pattern[0]);
if (i == -1)
return workingCopy;
for (; i < pattern.Count; i++)
{
if (workingCopy[i] != pattern[i])
return workingCopy;
}
workingCopy.RemoveRange(i, pattern.Count);
workingCopy.InsertRange(i, hqRep);
return workingCopy;
}
/// <summary>
/// Removes tags used for formatting
/// 0x02 0xXX 0xXX 0x03
/// 0x02 0xXX 0xXX 0xXX 0xXX 0xXX 0xXX 0x03
/// </summary>
/// <param name="workingCopy"></param>
/// <returns></returns>
private static List<byte> CleanFormat(List<byte> workingCopy)
{
int[] idx = workingCopy.Select((b, i) => b == 0x02 ? i : -1).Where(i => i != -1).ToArray();
bool changed = false;
foreach (int i in idx)
{
if (workingCopy.Count > i + 8 && workingCopy[i + 8] == 0x03)
{
workingCopy.RemoveRange(i, 9);
changed = true;
}
if (workingCopy.Count > i + 4 && workingCopy[i + 4] == 0x03)
{
workingCopy.RemoveRange(i, 5);
changed = true;
}
if (changed)
workingCopy = CleanFormat(workingCopy);
}
return workingCopy;
}
/// <summary>
/// Removes junk around PC names that's only useful to the client.
/// </summary>
/// <param name="workingCopy"></param>
/// <returns></returns>
private static List<byte> CleanName(List<byte> workingCopy)
{
if (workingCopy.Count(f => f == 0x3) == 1)
return workingCopy;
int name = workingCopy.FindIndex(0, item => item == 0x3);
if (name != -1)
{
workingCopy.RemoveRange(0, name + 1);
name = workingCopy.FindIndex(0, item => item == 0x3);
if (name != -1)
workingCopy.RemoveRange(name - 9, 10);
}
return workingCopy;
}
/// <summary>
/// Creates a DateTime object out of our timestamp
/// </summary>
/// <param name="value">Timestamp to convert</param>
/// <returns>DateTime object corresponding to the timestamp</returns>
private static DateTime GetTimeStamp(double value)
{
var dt = new DateTime(1970, 1, 1, 0, 0, 0, 0);
return dt.AddSeconds(value).ToLocalTime();
}
#endregion
};
}
}