-
Notifications
You must be signed in to change notification settings - Fork 2
/
Character.cs
765 lines (617 loc) · 26.3 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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using WindowsTimer = System.Windows.Forms.Timer;
using System.Globalization;
using Decal.Adapter;
using Decal.Adapter.Wrappers;
namespace TreeStats
{
public struct AllegianceInfoRecord
{
public String name;
public int rank;
public int race;
public int gender;
public AllegianceInfoRecord(String _name, int _rank, int _race, int _gender)
{
name = _name;
rank = _rank;
race = _race;
gender = _gender;
}
}
public static class Character
{
public static Uri endpoint;
public static CoreManager MyCore { get; set; }
public static PluginHost MyHost { get; set; }
// Updates
public static DateTime lastSend; // Throttle sending
public static int minimumSendInterval = 5; // Seconds
public static WindowsTimer updateTimer; // Automatically send updates every `updateTimerInterval`
public static int updateTimerInterval = 1000 * 60 * 60; // One hour
public static bool sentServerPopulation; // Only send server pop the first time (after login)
// Store latest message
public static string lastMessage = null;
// Make an area to store information
// These are stored for later because we get some of
// this information before we login and are ready to
// send the character update.
public static string character;
public static string server;
public static int currentTitle;
public static List<Int32> titlesList;
public static Int64 luminance_earned;
public static Int64 luminance_total;
// Allegiance information
public static string allegianceName;
public static int allegianceSize;
public static int followers;
public static AllegianceInfoRecord monarch;
public static AllegianceInfoRecord patron;
public static List<AllegianceInfoRecord> vassals;
// Store character properties from GameEvent/Login Character message
public static Dictionary<Int32, Int32> characterProperties;
// DWORD Blacklist
// We're going to grab all the Character Property DWORD values
// becuase we haven't figured all of the ones we want.
// But there are some we know we never want and we'll blacklist those.
public static List<Int32> dwordBlacklist;
// Resources
// http://www.immortalbob.com/phpBB3/viewtopic.php?f=24&t=100&start=10
// http://pastebin.com/X05rYnYU
// http://www.virindi.net/repos/virindi_public/trunk/VirindiTankLootPlugins/VTClassic%20Shared/Constants.cs
internal static void Init(CoreManager core, PluginHost host)
{
try
{
endpoint = new Uri(PluginCore.urlBase);
MyCore = core;
MyHost = host;
// General character info
currentTitle = -1;
titlesList = new List<Int32>();
allegianceName = null;
luminance_earned = -1;
luminance_total = -1;
vassals = new List<AllegianceInfoRecord>();
// Store all returned character properties from the Login Player event
characterProperties = new Dictionary<Int32, Int32>();
// A list of dwords we know we don't want to save
dwordBlacklist = new List<Int32>()
{
2,5,7,10,17,19,20,24,25,26,28,30,33,35,36,38,43,45,86,87,88,89,90,91,
92,98,105,106,107,108,109,110,111,113,114,115,117,125,129,131,134,158,
159,160,166,170,171,172,174,175,176,177,178,179,188,193,270,271,272,293
};
lastSend = DateTime.MinValue;
// Set up timed updates
updateTimer = new WindowsTimer();
updateTimer.Interval = updateTimerInterval;
updateTimer.Tick += new EventHandler(updateTimer_Tick);
updateTimer.Start();
sentServerPopulation = false;
}
catch (Exception ex)
{
Logging.LogError(ex);
}
}
internal static void Destroy()
{
try
{
endpoint = null;
lastMessage = null;
characterProperties.Clear();
characterProperties = null;
if (vassals != null)
{
vassals.Clear();
}
vassals = null;
dwordBlacklist.Clear();
dwordBlacklist = null;
lastSend = DateTime.MinValue;
if (updateTimer != null)
{
updateTimer.Stop();
updateTimer.Tick -= updateTimer_Tick;
updateTimer.Dispose();
updateTimer = null;
}
}
catch (Exception ex)
{
Logging.LogError(ex);
}
}
internal static void updateTimer_Tick(object sender, EventArgs e)
{
Logging.LogMessage("updateTimer_Tick()");
TryUpdate(false);
}
/* DoUpdate()
*
* Sends an update without checking for the minimum send interval
*/
internal static void DoUpdate()
{
try
{
Logging.LogMessage("DoUpdate()");
GetCharacterInfo();
SendCharacterInfo(lastMessage);
}
catch (Exception ex)
{
Logging.LogError(ex);
}
}
/* TryUpdate()
*
* Sends an update via DoUpdate() after checking for the minimum time interval
*/
internal static void TryUpdate(bool isManual)
{
TryUpdate(isManual, false);
}
internal static void TryUpdate(bool isManual, bool isQuiet)
{
Logging.LogMessage("TryUpdate()");
// Automatic
if (!isManual)
{
if (Settings.ShouldSendCharacter(Character.server + "-" + Character.character))
{
DoUpdate();
}
// Manual update
}
else
{
if (lastSend != DateTime.MinValue) // Null check: Can't do null checks on DateTimes, so we do this
{
TimeSpan diff = DateTime.Now - lastSend;
if (diff.Seconds < minimumSendInterval)
{
if (!isQuiet)
{
Util.WriteToChat("Failed to send character: Please wait " + (minimumSendInterval - diff.Seconds).ToString() + "s before sending again.");
}
return;
}
}
DoUpdate();
}
}
/* GetCharacterInfo()
*
* Gets player information
*
* This method builds a JSON request string which is later encrypted
* It would be really nice to use a proper data structure for
* this and using Json.NET to serialize that data structure.
*
* Some of the stuff here is stored beforehand from other messages
* Most of it is taken from CharacterFilter once login is completed
*
* Saves the concatenated JSON string to class variable lastMessage
*/
internal static void GetCharacterInfo()
{
try
{
// Declare the fileservice for later use
Decal.Adapter.Wrappers.CharacterFilter cf = MyCore.CharacterFilter;
Decal.Filters.FileService fs = CoreManager.Current.FileService as Decal.Filters.FileService;
// Save character and server for later since we're going to use this alot
server = cf.Server;
// Prepare the en-US culture for string creation
// I use this because I want my dates to all be formatted as if
// the client using an en-US locale
CultureInfo culture_en_us = new CultureInfo("en-US");
// One long string stores the entire POST request body
// And the string is generated with StringBuilder
StringBuilder req = new StringBuilder();
req.Append("{");
// Add the TreeStats Account if it's valid (logged in)
Logging.LogMessage("Checking whether player is logged in...");
Logging.LogMessage(" Settings::isLoggedIn() : " + Settings.isLoggedIn.ToString());
Logging.LogMessage(" Settings::useAccount() : " + Settings.useAccount.ToString());
Logging.LogMessage(" Settings::accountName() : " + Settings.accountName);
if (Settings.isLoggedIn && Settings.useAccount && Settings.accountName.Length > 0 && Settings.accountPassword.Length > 0)
{
Logging.LogMessage("Appending account name " + Settings.accountName + " to this upload.");
req.AppendFormat("\"account_name\":\"{0}\",", Settings.accountName);
}
// General attributes
req.AppendFormat("\"version\":\"{0}\",", 2);
req.AppendFormat("\"name\":\"{0}\",", character);
req.AppendFormat("\"race\":\"{0}\",", cf.Race);
req.AppendFormat("\"gender\":\"{0}\",", cf.Gender);
req.AppendFormat("\"level\":{0},", cf.Level);
// Add allegiance name if we've gotten it in a message
if (allegianceName != null)
{
req.AppendFormat("\"allegiance_name\":\"{0}\",", allegianceName);
}
req.AppendFormat("\"rank\":{0},", cf.Rank);
req.AppendFormat("\"followers\":{0},", followers);
req.AppendFormat("\"server\":\"{0}\",", cf.Server);
/* Only append server population if it hasn't been sent yet (we just logged in).
/ Character Filter only receives this value from the server and login, instead
/ of continuously. If we sent this each time we'd be reporting inaccurate server
* populations.
*/
if (!sentServerPopulation)
{
req.AppendFormat("\"server_population\":{0},", cf.ServerPopulation);
sentServerPopulation = true;
}
req.AppendFormat("\"deaths\":{0},", cf.Deaths);
req.AppendFormat("\"birth\":\"{0}\",", cf.Birth.ToString(culture_en_us));
req.AppendFormat("\"total_xp\":{0},", cf.TotalXP);
req.AppendFormat("\"unassigned_xp\":{0},", cf.UnassignedXP);
req.AppendFormat("\"skill_credits\":{0},", cf.SkillPoints);
if (Settings.sendLocation)
{
req.AppendFormat("\"location\":\"{0}\",", new Location(MyHost.Actions.Landcell, MyHost.Actions.LocationX, MyHost.Actions.LocationY).ToString());
}
//req.AppendFormat("\"age\":{0},", cf.Age);
// Luminance XP
if (luminance_earned != -1)
{
req.AppendFormat("\"luminance_earned\":{0},", luminance_earned);
}
if (luminance_total != -1)
{
req.AppendFormat("\"luminance_total\":{0},", luminance_total);
}
// Attributes
req.Append("\"attribs\":{");
string attribs_format = "\"{0}\":{{\"name\":\"{1}\",\"base\":{2},\"creation\":{3}}},";
foreach (var attr in MyCore.CharacterFilter.Attributes)
{
req.AppendFormat(attribs_format, attr.Name.ToLower(), attr.Name, attr.Base, attr.Creation);
}
req.Remove(req.Length - 1, 1);
req.Append("},");
// Vitals
req.Append("\"vitals\":{");
string vitals_format = "\"{0}\":{{\"name\":\"{1}\",\"base\":{2}}},";
foreach (var vital in MyCore.CharacterFilter.Vitals)
{
req.AppendFormat(vitals_format, vital.Name.ToLower(), vital.Name, vital.Base);
}
req.Remove(req.Length - 1, 1);
req.Append("},");
// Skills
Decal.Interop.Filters.SkillInfo skillinfo = null;
req.Append("\"skills\":{");
string skill_format = "\"{0}\":{{\"name\":\"{1}\",\"base\":{2},\"training\":\"{3}\"}},";
string name;
string training;
for (int i = 0; i < fs.SkillTable.Length; ++i)
{
try
{
skillinfo = MyCore.CharacterFilter.Underlying.get_Skill((Decal.Interop.Filters.eSkillID)fs.SkillTable[i].Id);
name = skillinfo.Name.ToLower().Replace(" ", "_");
training = skillinfo.Training.ToString().Substring(6);
req.AppendFormat(skill_format, name, name, skillinfo.Base, training);
}
finally
{
if (skillinfo != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(skillinfo);
skillinfo = null;
}
name = null;
training = null;
}
}
req.Remove(req.Length - 1, 1);
req.Append("},");
// Monarch & Patron Information
// We wrap in try/catch because AllegianceInfoWrapper behaves oddly (Is not null when it should be? Not sure on this.)
try
{
if (monarch.name != null)
{
req.AppendFormat("\"monarch\":{{\"name\":\"{0}\",\"race\":{1},\"rank\":{2},\"gender\":{3},\"followers\":{4}}},",
monarch.name, monarch.race, monarch.rank, monarch.gender, allegianceSize);
}
}
catch (Exception ex)
{
Logging.LogError(ex);
}
try
{
if (patron.name != null)
{
req.AppendFormat("\"patron\":{{\"name\":\"{0}\",\"race\":{1},\"rank\":{2},\"gender\":{3}}},",
patron.name, patron.race, patron.rank, patron.gender);
}
}
catch (Exception ex)
{
Logging.LogError(ex);
}
// Vassals
try
{
if (vassals.Count > 0)
{
req.Append("\"vassals\":[");
string vassal_format = "{{\"name\":\"{0}\",\"race\":{1},\"rank\":{2},\"gender\":{3}}},";
foreach (AllegianceInfoRecord vassal in vassals)
{
req.AppendFormat(vassal_format, vassal.name, vassal.race, vassal.rank, vassal.gender);
}
req.Remove(req.Length - 1, 1);
req.Append("],");
}
}
catch (Exception ex)
{
Logging.LogError(ex);
}
// Titles
// Add titles to message if we have them
if (currentTitle != -1)
{
req.AppendFormat("\"current_title\":{0},", currentTitle);
req.Append("\"titles\":[");
//foreach(int titleId in titlesList)
for (int i = 0; i < titlesList.Count; i++)
{
req.AppendFormat("{0},", titlesList[i]);
}
// Remove final trailing comma
req.Remove(req.Length - 1, 1);
req.Append("],");
}
// Character Properties
if (characterProperties.Count > 0)
{
req.Append("\"properties\":{");
string property_format = "\"{0}\":{1},";
foreach (var kvp in characterProperties)
{
req.AppendFormat(property_format, kvp.Key, kvp.Value);
}
req.Remove(req.Length - 1, 1);
req.Append("},");
}
req.Remove(req.Length - 1, 1); // Remove trailing comma
req.Append("}");
// Encrypt POST request
lastMessage = Encryption.encrypt(req.ToString());
Logging.LogMessage(req.ToString());
}
catch (Exception ex)
{
Logging.LogError(ex);
}
}
internal static void SendCharacterInfo(string message)
{
Uri sendUrl;
try
{
lastSend = DateTime.Now;
if (message == null || message.Length < 1)
{
return;
}
// Do the sending
using (var client = new WebClient())
{
client.UploadStringCompleted += (s, e) =>
{
if (e.Error != null)
{
Util.WriteToChat("Update failed: " + e.Error.Message);
}
else
{
Util.WriteToChat(e.Result);
}
};
// Decide whether to use the default URL or a custom one
if (Settings.useCustomURL) {
sendUrl = new Uri(Settings.customURL);
Logging.LogMessage("Using custom send URL of " + sendUrl.ToString());
} else {
sendUrl = endpoint;
}
client.Headers.Add("User-Agent", "TreeStats v" + typeof(PluginCore).Assembly.GetName().Version.ToString());
client.UploadStringAsync(sendUrl, "POST", message);
}
}
catch (Exception ex)
{
Logging.LogError(ex);
}
}
internal static void ProcessTitlesMessage(NetworkMessageEventArgs e)
{
try
{
// Save current title
currentTitle = e.Message.Value<Int32>("current");
MessageStruct titles = e.Message.Struct("titles");
for (int i = 0; i < titles.Count; i++)
{
// Add title to list
// Check if exists first so multiple firings of the event don't make
// duplicate titles
Int32 titleId = titles.Struct(i).Value<Int32>("title");
if (!titlesList.Contains(titleId))
{
titlesList.Add(titleId);
}
}
}
catch (Exception ex)
{
Logging.LogError(ex);
}
}
internal static void ProcessCharacterPropertyData(NetworkMessageEventArgs e)
{
try
{
MessageStruct props = e.Message.Struct("properties");
MessageStruct dwords = props.Struct("dwords");
MessageStruct qwords = props.Struct("qwords");
MessageStruct strings = props.Struct("strings");
MessageStruct tmpStruct;
Int32 tmpKey;
Int32 tmpValue;
// Process strings to extract character name
// This is a workaround for a bug in GDLE
character = strings.Struct(0).Value<string>("value");
// Process DWORDS
for (int i = 0; i < dwords.Count; i++)
{
tmpStruct = dwords.Struct(i);
tmpKey = tmpStruct.Value<Int32>("key");
tmpValue = tmpStruct.Value<Int32>("value");
if (!dwordBlacklist.Contains(tmpKey))
{
characterProperties.Add(tmpKey, tmpValue);
}
}
// Process QWORDS
Int64 qwordKey;
Int64 qwordValue;
for (int i = 0; i < qwords.Count; i++)
{
tmpStruct = qwords.Struct(i);
qwordKey = tmpStruct.Value<Int64>("key");
qwordValue = tmpStruct.Value<Int64>("value");
if (qwordKey == 6)
{
luminance_earned = qwordValue;
}
else if (qwordKey == 7)
{
luminance_total = qwordValue;
}
}
}
catch (Exception ex)
{
Logging.LogError(ex);
}
}
internal static void ProcessAllegianceInfoMessage(NetworkMessageEventArgs e)
{
monarch = new AllegianceInfoRecord();
patron = new AllegianceInfoRecord();
vassals = new List<AllegianceInfoRecord>();
Dictionary<int, AllegianceInfoRecord> recs = new Dictionary<int, AllegianceInfoRecord>();
Dictionary<int, int> parents = new Dictionary<int, int>();
try
{
// Take down general info
allegianceName = e.Message.Value<string>("allegianceName");
allegianceSize = e.Message.Value<Int32>("allegianceSize");
followers = e.Message.Value<Int32>("followers");
// Process the records struct, which has all the members of the
// allegiance
MessageStruct records = e.Message.Struct("records");
MessageStruct record;
/* Determine monarch, patron, and vassals from the records struct
*
* I'm not sure if all of this is strictly necessary but it works. The main
* thing adding complexity is the logic for finding the patron. I could
* make this simpler if I knew that the records vector was in tree order.
*/
int currentId = MyCore.CharacterFilter.Id;
for (int i = 0; i < records.Count; i++)
{
record = records.Struct(i);
Logging.LogMessage("Setting treeparent in parents dict.");
// Build up the parents dict as we go
parents[record.Value<int>("character")] = record.Value<int>("treeParent");
// Save the record for later
recs.Add(
record.Value<int>("character"),
new AllegianceInfoRecord(record.Value<string>("name"),
record.Value<int>("rank"),
record.Value<int>("race"),
record.Value<int>("gender")));
// Add to vassals if appropriate
if (record.Value<int>("treeParent") == currentId)
{
vassals.Add(
new AllegianceInfoRecord(
record.Value<string>("name"),
record.Value<int>("rank"),
record.Value<int>("race"),
record.Value<int>("gender")));
}
// Set as monarch if appropriate
else if (record.Value<int>("treeParent") <= 1)
{
monarch = new AllegianceInfoRecord(
record.Value<string>("name"),
record.Value<int>("rank"),
record.Value<int>("race"),
record.Value<int>("gender"));
}
}
// Should stop now if records is empty, because we aren't in an allegiance
Logging.LogMessage("Printing records");
foreach (KeyValuePair<int, AllegianceInfoRecord> rec in recs)
{
Logging.LogMessage(rec.Key.ToString() + " : " + rec.Value.name);
}
Logging.LogMessage("Printing parents");
foreach (KeyValuePair<int, int> rec in parents)
{
Logging.LogMessage(rec.Key.ToString() + ": " + rec.Value.ToString());
}
// Set patron
if (parents.Count > 0 && recs.ContainsKey(parents[currentId]))
{
patron = recs[parents[currentId]];
}
}
catch (Exception ex)
{
Logging.LogError(ex);
}
finally
{
recs.Clear();
recs = null;
parents.Clear();
parents = null;
}
}
internal static void ProcessSetTitleMessage(NetworkMessageEventArgs e)
{
try
{
Int32 title = e.Message.Value<Int32>("title");
bool active = e.Message.Value<bool>("active");
if (active)
{
currentTitle = title;
}
}
catch (Exception ex)
{
Logging.LogError(ex);
}
}
}
}