-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPlugin.cs
More file actions
executable file
·888 lines (783 loc) · 36 KB
/
Copy pathPlugin.cs
File metadata and controls
executable file
·888 lines (783 loc) · 36 KB
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
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
#pragma warning disable CS0436 // Type conflicts with imported type
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BazaarGameClient.Domain.Models.Cards;
using BazaarGameShared.Domain.Core.Types;
using BazaarGameShared.Domain.Players;
using BazaarGameShared.Infra.Messages;
using BepInEx;
using HarmonyLib;
using Newtonsoft.Json;
using TheBazaar;
using UnityEngine;
using UnityEngine.InputSystem;
using System.Net.Http;
using System.Text;
using BepInEx.Configuration;
using System.IO;
using System.Threading;
using System.Reflection;
using Newtonsoft.Json.Linq;
using System.Diagnostics;
namespace BazaarPlannerMod;
[BepInPlugin(MyPluginInfo.PLUGIN_GUID, MyPluginInfo.PLUGIN_NAME, MyPluginInfo.PLUGIN_VERSION)]
public class Plugin : BaseUnityPlugin
{
private readonly Harmony _harmony = new Harmony(MyPluginInfo.PLUGIN_GUID);
private static DateTime _lastSentTime = DateTime.MinValue;
private static readonly TimeSpan SendInterval = TimeSpan.FromSeconds(2);
private static string FirebaseApiKey = "AIzaSyCrDTf9_S8PURED8DZBDbbEsJuMA1poduw";
private static string _runId;
private static ConfigEntry<string> UidConfig;
private static ConfigEntry<string> TokenConfig;
private static ConfigEntry<string> RefreshTokenConfig;
private static ConfigEntry<string> TokenExpiryConfig;
private static ConfigFile BPConfig;
private static string _lastBoardState = "";
private static int _encounterId = 0;
private static ConfigEntry<string> DisplayNameConfig;
private static DateTime _lastUpdateTime = DateTime.MinValue;
private static CancellationTokenSource _updateCancellationToken;
private static EVictoryCondition _lastVictoryCondition;
private static string _firebaseUrl = "https://bazaarplanner-default-rtdb.firebaseio.com/";
private static Dictionary<string, List<string>> _baseItemTags;
private const string GithubApiUrl = "https://api.github.com/repos/oceanseth/BazaarPlannerMod/releases/latest";
private static async Task SaveCombat()
{
string uid = UidConfig.Value;
RunInfo runInfo = getRunInfo();
string json = CreateBazaarPlannerJson(runInfo);
string compressed = LZString.CompressToEncodedURIComponent(json);
string runId = runInfo.RunId;
string battleName = $"Day {Data.Run.Day} - {runInfo.OppName}";
string timestamp = ((DateTimeOffset)DateTime.Now).ToUnixTimeMilliseconds().ToString();
var data = new Dictionary<string, object>
{
{ "wins", runInfo.Wins },
{ "losses", runInfo.Losses },
{ "day", runInfo.Day },
{ "t", timestamp },
{ "hero", runInfo.Hero },
{ "lastEncounter", _encounterId.ToString() },
{ $"encounters/{_encounterId}", new
{
name = battleName,
d = compressed,
t = timestamp,
v = _lastVictoryCondition==EVictoryCondition.Win ? 1 : 0
}}
};
// Add ranked field only if PlayMode is true
if (runInfo.PlayMode)
{
data["ranked"] = "1";
}
await SaveToFirebase($"users/{uid}/runs/{runId}", data);
_encounterId++;
// await SaveToFirebase($"users/{uid}/currentRun/id",runId);
}
private static async Task SaveToFirebase(string url, object data)
{
try
{
var token = await GetValidToken();
if (string.IsNullOrEmpty(UidConfig.Value) || string.IsNullOrEmpty(token))
{
Console.WriteLine("Cannot save to BazaarPlanner: Missing UID or token");
return;
}
using (var httpClient = new HttpClient())
{
var jsonData = JsonConvert.SerializeObject(data);
Console.WriteLine($"Attempting to save to {url} with data: {jsonData}");
var request = new HttpRequestMessage
{
Method = new HttpMethod("PATCH"),
RequestUri = new Uri($"{_firebaseUrl}{url}.json?auth={token}"),
Content = new StringContent(jsonData, Encoding.UTF8, "application/json")
};
var response = await httpClient.SendAsync(request);
var responseContent = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode)
{
Console.WriteLine($"{url} saved successfully");
}
else
{
Console.WriteLine($"Failed to save {url}: {response.StatusCode} - {response.ReasonPhrase}");
Console.WriteLine($"Response content: {responseContent}");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error in SaveToFirebase: {ex.Message}");
}
}
private static RunInfo getRunInfo() {
return new RunInfo
{
Wins = Data.Run.Victories,
Losses = Data.Run.Losses,
Hero = Data.Run.Player.Hero.ToString(),
Day = (int)Data.Run.Day,
Gold = Data.Run.Player.GetAttributeValue(EPlayerAttributeType.Gold),
Income = Data.Run.Player.GetAttributeValue(EPlayerAttributeType.Income),
Cards = GetCardInfo(GetItemsAsCards(Data.Run.Player.Hand)),
Stash = GetCardInfo(GetItemsAsCards(Data.Run.Player.Stash)),
Skills = GetSkillInfo(Data.Run.Player.Skills),
OppCards = GetCardInfo(GetItemsAsCards(Data.Run.Opponent?.Hand)),
OppStash = GetCardInfo(GetItemsAsCards(Data.Run.Opponent?.Stash)),
OppSkills = GetSkillInfo(Data.Run.Opponent?.Skills),
Health = Data.Run.Player.GetAttributeValue(EPlayerAttributeType.HealthMax),
Shield = Data.Run.Player.GetAttributeValue(EPlayerAttributeType.Shield),
Regen = Data.Run.Player.GetAttributeValue(EPlayerAttributeType.HealthRegen),
Level = Data.Run.Player.GetAttributeValue(EPlayerAttributeType.Level),
Prestige = Data.Run.Player.GetAttributeValue(EPlayerAttributeType.Prestige),
Name = Data.Profile?.Username,
OppHealth = Data.Run.Opponent?.GetAttributeValue(EPlayerAttributeType.HealthMax),
OppRegen = Data.Run.Opponent?.GetAttributeValue(EPlayerAttributeType.HealthRegen),
OppName = Data.Run.Opponent?.Hero==EHero.Common ? "PvE":Data.SimPvpOpponent?.Name,
OppHero = Data.Run.Opponent?.Hero.ToString(),
OppShield = Data.Run.Opponent?.GetAttributeValue(EPlayerAttributeType.Shield),
OppGold = Data.Run.Opponent?.GetAttributeValue(EPlayerAttributeType.Gold),
OppIncome = Data.Run.Opponent?.GetAttributeValue(EPlayerAttributeType.Income),
OppLevel = Data.Run.Opponent?.GetAttributeValue(EPlayerAttributeType.Level),
OppPrestige = Data.Run.Opponent?.GetAttributeValue(EPlayerAttributeType.Prestige),
RunId = _runId,
PlayMode = Data.SelectedPlayMode==EPlayMode.Ranked
};
}
private static string GetHashedRunId(string runId, string displayName)
{
using (var hmac = new System.Security.Cryptography.HMACSHA256(System.Text.Encoding.UTF8.GetBytes(displayName)))
{
byte[] hashBytes = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(runId));
// Convert to base64 and make URL-safe
return Convert.ToBase64String(hashBytes)
.Replace('+', '-')
.Replace('/', '_')
.Replace("=", "")
.Substring(0, 20); // Truncate to reasonable length
}
}
private static List<Card> GetItemsAsCards(IPlayerInventory container)
{
return container.Container.GetSocketables()
.Cast<Card>() // Cast all items to Card (throws InvalidCastException if any object is incompatible)
.ToList<Card>();
}
static List<RunInfo.SkillInfo> GetSkillInfo(IEnumerable<SkillCard> skills)
{
List<RunInfo.SkillInfo> skillInfos = new List<RunInfo.SkillInfo>();
foreach (var skill in skills)
{
if (skill.Template != null)
skillInfos.Add(new RunInfo.SkillInfo
{
TemplateId = skill.TemplateId,
Tier = skill.Tier,
Name = skill.Template.Localization.Title.Text,
Attributes = skill.Attributes
});
}
return skillInfos;
}
private static string CreateBazaarPlannerJson(RunInfo runInfo)
{
var result = new List<object>();
// Add player and opponent data objects (unchanged)
result.Add(new
{
name = "_b_b",
health = runInfo.Health,
shield = runInfo.Shield,
regen = runInfo.Regen,
playerName = DisplayNameConfig.Value ?? runInfo.Name ?? "Unknown",
hero = runInfo.Hero,
level = runInfo.Level,
prestige = runInfo.Prestige,
income = runInfo.Income,
gold = runInfo.Gold,
skills = runInfo.Skills?.Select(s =>
{
var skillDict = new Dictionary<string, object>
{
["name"] = s.Name,
["tier"] = s.Tier
};
if (s.Attributes.ContainsKey(ECardAttributeType.Custom_0))
{
skillDict["Custom_0"] = s.Attributes[ECardAttributeType.Custom_0];
}
return skillDict;
}).ToList(),
});
if (runInfo.OppCards != null && runInfo.OppCards.Count > 0) {
result.Add(new
{
name = "_b_t",
gold = runInfo.OppGold,
health = runInfo.OppHealth,
regen = runInfo.OppRegen,
shield = runInfo.OppShield,
playerName = runInfo.OppName ?? "Unknown",
hero = runInfo.OppHero,
level = runInfo.OppLevel,
income = runInfo.OppIncome,
prestige = runInfo.OppPrestige,
day = runInfo.Day,
skills = runInfo.OppSkills?.Select(s =>
{
var skillDict = new Dictionary<string, object>
{
["name"] = s.Name,
["tier"] = s.Tier
};
if (s.Attributes.ContainsKey(ECardAttributeType.Custom_0))
{
skillDict["Custom_0"] = s.Attributes[ECardAttributeType.Custom_0];
}
return skillDict;
}).ToList()
});
}
result.Add(new
{
name = "_b_backpack"
});
if(runInfo.OppStash != null && runInfo.OppStash.Count > 0)
{
result.Add(new
{
name = "_b_tb"
});
}
// Helper function to create card object with conditional attributes
object CreateCardObject(RunInfo.CardInfo card, string board)
{
var cardDict = new Dictionary<string, object>
{
["name"] = card.Enchant.Length > 0 ? card.Enchant + " " + card.Name : card.Name,
["startIndex"] = card.Left,
["board"] = board,
["tier"] = card.Tier
};
// Only include tags if they differ from base item
if (card.Tags != null && card.Tags.Count > 0 && HasNewTags(card.Name, card.Tags.Select(t => t.ToString()).ToList()))
{
cardDict["tags"] = card.Tags.Select(t => t.ToString()).ToList();
}
if (card.Attributes?.ContainsKey(ECardAttributeType.SellPrice) == true)
cardDict["valueFinal"] = card.Attributes[ECardAttributeType.SellPrice];
if (card.Attributes?.ContainsKey(ECardAttributeType.HealAmount) == true)
cardDict["healFinal"] = card.Attributes[ECardAttributeType.HealAmount];
if (card.Attributes?.ContainsKey(ECardAttributeType.CooldownMax) == true)
cardDict["cooldownFinal"] = card.Attributes[ECardAttributeType.CooldownMax];
if (card.Attributes?.ContainsKey(ECardAttributeType.CritChance) == true && card.Attributes[ECardAttributeType.CritChance]>0)
cardDict["critFinal"] = card.Attributes[ECardAttributeType.CritChance];
if (card.Attributes?.ContainsKey(ECardAttributeType.BurnApplyAmount) == true)
cardDict["burnFinal"] = card.Attributes[ECardAttributeType.BurnApplyAmount];
if (card.Attributes?.ContainsKey(ECardAttributeType.ShieldApplyAmount) == true)
cardDict["shieldFinal"] = card.Attributes[ECardAttributeType.ShieldApplyAmount];
if (card.Attributes?.ContainsKey(ECardAttributeType.PoisonApplyAmount) == true)
cardDict["poisonFinal"] = card.Attributes[ECardAttributeType.PoisonApplyAmount];
if (card.Attributes?.ContainsKey(ECardAttributeType.DamageAmount) == true)
cardDict["damageFinal"] = card.Attributes[ECardAttributeType.DamageAmount];
if (card.Attributes?.ContainsKey(ECardAttributeType.Lifesteal) == true && card.Attributes[ECardAttributeType.Lifesteal]>0)
cardDict["lifestealFinal"] = card.Attributes[ECardAttributeType.Lifesteal];
if (card.Attributes?.ContainsKey(ECardAttributeType.RegenApplyAmount) == true)
cardDict["regenFinal"] = card.Attributes[ECardAttributeType.RegenApplyAmount];
if (card.Attributes?.ContainsKey(ECardAttributeType.AmmoMax) == true && card.Attributes[ECardAttributeType.AmmoMax]>0)
cardDict["ammoFinal"] = card.Attributes[ECardAttributeType.AmmoMax];
if(card.Attributes?.ContainsKey(ECardAttributeType.SlowAmount) == true)
cardDict["slowFinal"] = card.Attributes[ECardAttributeType.SlowAmount];
if(card.Attributes?.ContainsKey(ECardAttributeType.HasteAmount) == true)
cardDict["hasteFinal"] = card.Attributes[ECardAttributeType.HasteAmount];
if(card.Attributes?.ContainsKey(ECardAttributeType.FreezeAmount) == true)
cardDict["freezeFinal"] = card.Attributes[ECardAttributeType.FreezeAmount];
if(card.Attributes?.ContainsKey(ECardAttributeType.Custom_0) == true)
cardDict["Custom_0"] = card.Attributes[ECardAttributeType.Custom_0];
if(card.Attributes?.ContainsKey(ECardAttributeType.Custom_1) == true)
cardDict["Custom_1"] = card.Attributes[ECardAttributeType.Custom_1];
if(card.Attributes?.ContainsKey(ECardAttributeType.Custom_2) == true)
cardDict["Custom_2"] = card.Attributes[ECardAttributeType.Custom_2];
if(card.Attributes?.ContainsKey(ECardAttributeType.Custom_3) == true)
cardDict["Custom_3"] = card.Attributes[ECardAttributeType.Custom_3];
if(card.Attributes?.ContainsKey(ECardAttributeType.Quest_1) == true)
cardDict["quest_1"] = card.Attributes[ECardAttributeType.Quest_1];
if(card.Attributes?.ContainsKey(ECardAttributeType.Quest_2) == true)
cardDict["quest_2"] = card.Attributes[ECardAttributeType.Quest_2];
if(card.Attributes?.ContainsKey(ECardAttributeType.Quest_3) == true)
cardDict["quest_3"] = card.Attributes[ECardAttributeType.Quest_3];
if(card.Attributes?.ContainsKey(ECardAttributeType.Quest_4) == true)
cardDict["quest_4"] = card.Attributes[ECardAttributeType.Quest_4];
if(card.Attributes?.ContainsKey(ECardAttributeType.Quest_5) == true)
cardDict["quest_5"] = card.Attributes[ECardAttributeType.Quest_5];
return cardDict;
}
// Add player cards
if (runInfo.Cards != null)
{
foreach (var card in runInfo.Cards)
{
result.Add(CreateCardObject(card, "b"));
}
}
// Add opponent cards
if (runInfo.OppCards != null)
{
foreach (var card in runInfo.OppCards)
{
result.Add(CreateCardObject(card, "t"));
}
}
if(runInfo.OppStash != null)
{
foreach (var card in runInfo.OppStash)
{
result.Add(CreateCardObject(card, "tb"));
}
}
if(runInfo.Stash != null)
{
foreach (var card in runInfo.Stash)
{
result.Add(CreateCardObject(card, "backpack"));
}
}
return JsonConvert.SerializeObject(result);
}
static void OpenInBazaarPlanner(string compressedData)
{
try
{
string url = $"https://www.bazaarplanner.com/#{compressedData}";
Application.OpenURL(url);
}
catch (Exception ex)
{
Console.WriteLine($"Error opening BazaarPlanner: {ex.Message}");
}
}
protected virtual void Awake()
{
Logger.LogInfo($"Plugin {MyPluginInfo.PLUGIN_GUID} is loaded!");
_harmony.PatchAll();
// Add version check on startup
CheckForUpdates();
// Load config
BPConfig = new ConfigFile(Path.Combine(Paths.ConfigPath, "BazaarPlanner.cfg"), true);
try
{
Console.WriteLine("Initializing configurations...");
UidConfig = BPConfig.Bind("Authentication", "Uid", "", "Firebase User ID");
TokenExpiryConfig = BPConfig.Bind("Authentication", "TokenExpiry", DateTime.MinValue.ToString(), "Token Expiration Time");
TokenConfig = BPConfig.Bind("Authentication", "Token", "", "Firebase ID Token");
RefreshTokenConfig = BPConfig.Bind("Authentication", "RefreshToken", "", "Firebase Refresh Token");
DisplayNameConfig = BPConfig.Bind("Authentication", "DisplayName", "", "Display Name");
Console.WriteLine("Configurations initialized successfully");
}
catch (Exception ex)
{
Console.WriteLine($"Error initializing configurations: {ex.Message}");
}
// Load base items data on startup
LoadBaseItems();
}
private void LoadBaseItems()
{
try
{
var assembly = Assembly.GetExecutingAssembly();
using (var stream = assembly.GetManifestResourceStream("BazaarPlannerMod.items.js"))
using (var reader = new StreamReader(stream))
{
string content = reader.ReadToEnd();
// Remove the "export const items = " part and any trailing semicolon
content = content.Replace("export const items =", "")
.Trim()
.TrimEnd(';');
// Deserialize to dynamic to easily access the nested structure
var items = JObject.Parse(content);
// Create a simplified dictionary with just the tags
_baseItemTags = items.Properties().ToDictionary(
prop => prop.Name,
prop => prop.Value["tags"].Select(t => t.ToString()).ToList()
);
Logger.LogInfo($"Loaded tags for {_baseItemTags.Count} base items");
}
}
catch (Exception ex)
{
Logger.LogError($"Failed to load base items: {ex.Message}");
_baseItemTags = new Dictionary<string, List<string>>();
}
}
private static bool HasNewTags(string itemName, List<string> currentTags)
{
if (!_baseItemTags.TryGetValue(itemName, out var baseTags))
return true; // If we don't have base data, include tags
if (currentTags == null || currentTags.Count == 0)
return false;
// Check if any current tag is not in base tags
return currentTags.Any(tag => !baseTags.Contains(tag));
}
static List<RunInfo.CardInfo> GetCardInfo(List<Card> cards)
{
List<RunInfo.CardInfo> cardInfos = new List<RunInfo.CardInfo>();
foreach (var card in cards)
{
cardInfos.Add(new RunInfo.CardInfo
{
TemplateId = card.TemplateId,
Tier = card.Tier,
Left = card.LeftSocketId,
Instance = card.GetInstanceId(),
Attributes = card.Attributes,
Tags = card.Tags,
Name = card.Template?.InternalName,
Enchant = card.GetEnchantment().ToString()
});
}
return cardInfos;
}
[HarmonyPatch(typeof(BoardManager), "Update")]
class Update
{
[HarmonyPrefix]
static void Prefix()
{
if (Keyboard.current == null || !Keyboard.current.bKey.wasPressedThisFrame)
{
return;
}
if (DateTime.Now - _lastSentTime < SendInterval)
{
return;
}
_lastSentTime = DateTime.Now;
RunInfo runInfo = getRunInfo();
string json = CreateBazaarPlannerJson(runInfo);
string compressed = LZString.CompressToEncodedURIComponent(json);
Task.Run(() => OpenInBazaarPlanner(compressed));
}
}
private static async Task<T> ReadFromFirebase<T>(string path, bool shallow = false)
{
try
{
var token = await GetValidToken();
using (var client = new HttpClient())
{
var shallowParam = shallow ? "&shallow=true" : "";
var response = await client.GetAsync($"{_firebaseUrl}{path}.json?auth={token}{shallowParam}");
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsStringAsync();
if (!string.IsNullOrEmpty(result) && result != "null")
{
return JsonConvert.DeserializeObject<T>(result);
}
}
else
{
Console.WriteLine($"Failed to fetch from Firebase: {response.ReasonPhrase}");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error reading from Firebase: {ex.Message}");
}
return default(T);
}
[HarmonyPatch(typeof(AppState), "OnRunInitializedMessageReceived")]
public static class OnRunInitializedMessageReceived
{
[HarmonyPrefix]
static async void Prefix(NetMessageRunInitialized obj)
{
_runId = GetHashedRunId(obj.RunId, DisplayNameConfig.Value);
var encounters = await ReadFromFirebase<Dictionary<string, object>>($"users/{UidConfig.Value}/runs/{_runId}/encounters", shallow: true);
_encounterId = encounters?.Count ?? 0;
}
}
private static async Task<string> GetValidToken()
{
try
{
DateTime tokenExpiry;
if (!DateTime.TryParse(TokenExpiryConfig.Value, out tokenExpiry))
{
tokenExpiry = DateTime.MinValue;
}
//Console.WriteLine($"Current token expires: {tokenExpiry}");
if (DateTime.Now < tokenExpiry && !string.IsNullOrEmpty(TokenConfig.Value))
{
// Console.WriteLine("Using existing valid token");
return TokenConfig.Value;
}
if (string.IsNullOrEmpty(RefreshTokenConfig.Value))
{
Console.WriteLine("No refresh token found in config");
return null;
}
//Console.WriteLine("All config values present, attempting to refresh token...");
using (var client = new HttpClient())
{
var content = new FormUrlEncodedContent(new Dictionary<string, string>
{
{ "grant_type", "refresh_token" },
{ "refresh_token", RefreshTokenConfig.Value }
});
// Console.WriteLine("Content: " + content + " and " + FirebaseApiKey);
var response = await client.PostAsync(
$"https://securetoken.googleapis.com/v1/token?key={FirebaseApiKey}",
content
);
// Console.WriteLine($"Token refresh response status: {response.StatusCode}");
if (!response.IsSuccessStatusCode)
{
var errorContent = await response.Content.ReadAsStringAsync();
Console.WriteLine($"Error response content: {errorContent}");
}
if (response.IsSuccessStatusCode)
{
var result = JsonConvert.DeserializeObject<Dictionary<string, string>>(
await response.Content.ReadAsStringAsync()
);
TokenConfig.Value = result["id_token"];
RefreshTokenConfig.Value = result["refresh_token"];
TokenExpiryConfig.Value = DateTime.Now.AddHours(1).ToString();
return TokenConfig.Value;
} else {
Console.WriteLine("Failed to refresh token: " + response.ReasonPhrase);
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error in GetValidToken: {ex.GetType().Name} - {ex.Message}");
return null;
}
return null;
}
private static string _lastMessageId = "";
[HarmonyPatch(typeof(CombatSimHandler), "Simulate")]
class CombatSimHandlerSimulate
{
[HarmonyPrefix]
static void Prefix(NetMessageCombatSim message, CancellationTokenSource cancellationToken)
{
if(_lastMessageId == message.MessageId) return;
_lastMessageId = message.MessageId;
if(UidConfig.Value == null || UidConfig.Value == "") return;
_lastVictoryCondition = message.Data.Winner == ECombatantId.Player ? EVictoryCondition.Win : EVictoryCondition.Lose;
Task.Run(() => SaveCombat());
}
}
/*
[HarmonyPatch(typeof(HeroBannerController), "UpdatePlayer")]
public static class UpdatePlayerPatch
{
[HarmonyPostfix]
static void Postfix(HeroBannerController __instance, string userName, int nameId, string titlePrefix, string titleSuffix, TheBazaar.ProfileData.ISeasonRank currentSeasonRank, int? leaderboardPosition) {
if(UidConfig.Value == null || UidConfig.Value == "") return;
if(userName != Data.Profile?.Username) return;
// Queue the UI update to happen on the next frame
__instance.StartCoroutine(UpdateNameNextFrame(__instance, nameId));
// var getter = typeof(TheBazaar.ProfileData.ProfileContainer).GetProperty("Username");
// getter.SetValue(Data.Profile, DisplayNameConfig.Value);
}
private static IEnumerator UpdateNameNextFrame(HeroBannerController instance, int nameId)
{
yield return null; // Wait for next frame
instance.SetHeroName(DisplayNameConfig.Value, nameId);
}
}
*/
[HarmonyPatch(typeof(HeroBannerController), "UpdatePlayer")]
public static class UpdatePlayerInterceptPatch
{
[HarmonyPrefix]
static bool Prefix(HeroBannerController __instance, ref string userName, ref int nameId,
ref string titlePrefix, TheBazaar.ProfileData.ISeasonRank currentSeasonRank, int? leaderboardPosition)
{
// Check if this is for our player
if(userName != Data.Profile?.Username) return true; // Let original method run unmodified
if(UidConfig.Value == null || UidConfig.Value == "") return true;
// Modify the parameters
userName = DisplayNameConfig.Value;
nameId = 0;
// You can modify other parameters here as needed
// Return true to let the original method run with our modified parameters
// Return false if you want to skip the original method entirely
return true;
}
}
[HarmonyPatch(typeof(HeroBannerController), "SetHeroName")]
public static class SetHeroNamePatch
{
[HarmonyPrefix]
static bool Prefix(ref string newName, ref int usernameId) {
if(newName != Data.Profile?.Username) return true;
if(UidConfig.Value == null || UidConfig.Value == "") return true;
newName = DisplayNameConfig.Value;
usernameId = 0;
return true;
}
}
[HarmonyPatch(typeof(BoardManager), "UpdateBoard")]
public static class UpdateBoardPatch
{
[HarmonyPostfix]
static void Postfix()
{
//Data.Profile.Username = DisplayNameConfig.Value;
if(UidConfig.Value == null || UidConfig.Value == "") return;
RunInfo runInfo = getRunInfo();
string json = CreateBazaarPlannerJson(runInfo);
if(json == _lastBoardState) return;
_lastBoardState = json;
string compressed = LZString.CompressToEncodedURIComponent(json);
var saveData = new {
id = runInfo.RunId,
d = compressed
};
if (_updateCancellationToken == null)
{
// No pending update, do it immediately
Task.Run(() => SaveToFirebase($"users/{UidConfig.Value}/currentrun", saveData));
}
else
{
// Cancel pending update and schedule new one
_updateCancellationToken.Cancel();
_updateCancellationToken = new CancellationTokenSource();
Task.Delay(1000, _updateCancellationToken.Token)
.ContinueWith(t => {
if (!t.IsCanceled) {
Task.Run(() => SaveToFirebase($"users/{UidConfig.Value}/currentrun", compressed));
_updateCancellationToken = null;
}
}, TaskContinuationOptions.OnlyOnRanToCompletion);
}
}
}
private async void CheckForUpdates()
{
Logger.LogInfo("Checking for updates...");
try
{
using (var client = new HttpClient())
{
// Add required headers for GitHub API
client.DefaultRequestHeaders.Add("User-Agent", "BazaarPlannerMod");
var response = await client.GetStringAsync(GithubApiUrl);
Logger.LogInfo("Got reponse from github: " + response);
var releaseInfo = JsonConvert.DeserializeObject<Dictionary<string, object>>(response);
string latestVersion = releaseInfo["tag_name"].ToString().TrimStart('v');
string currentVersion = MyPluginInfo.PLUGIN_VERSION;
if (IsNewerVersion(latestVersion, currentVersion))
{
// Get the installer download URL
var assets = ((JArray)releaseInfo["assets"]);
var installerAsset = assets.FirstOrDefault(a => ((JObject)a)["name"].ToString().Contains("Installer"));
if (installerAsset != null)
{
string downloadUrl = ((JObject)installerAsset)["browser_download_url"].ToString();
await DownloadAndStartInstaller(downloadUrl, latestVersion);
}
} else {
Logger.LogInfo("No updates available, you are on version " + currentVersion + " and latest version is " + latestVersion);
}
}
}
catch (Exception ex)
{
Logger.LogInfo($"Error checking for updates: {ex.Message}");
}
}
private bool IsNewerVersion(string latest, string current)
{
Version latestVersion = Version.Parse(latest);
Version currentVersion = Version.Parse(current);
return latestVersion > currentVersion;
}
private async Task DownloadAndStartInstaller(string downloadUrl, string latestVersion)
{
try
{
// Create batch file first, which will handle download and installation if user agrees
string batchPath = Path.Combine(Path.GetTempPath(), "UpdateBazaarPlanner.bat");
string currentDllPath = Assembly.GetExecutingAssembly().Location;
string tempDir = Path.Combine(Path.GetTempPath(), "BazaarPlannerUpdate");
string batchContent = @$"
@echo off
echo Starting update process... >> %temp%\bp_update.log
echo Temp directory path: {tempDir} >> %temp%\bp_update.log
echo Download URL: {downloadUrl} >> %temp%\bp_update.log
set /p result=<nul
for /f %%i in ('powershell -command ""Add-Type -AssemblyName System.Windows.Forms; $result = [System.Windows.Forms.MessageBox]::Show('New version {latestVersion} of BazaarPlanner available. You are on version {MyPluginInfo.PLUGIN_VERSION}. Update now?', 'BazaarPlanner Update', 'YesNo', 'Question'); $result""') do set result=%%i
echo User clicked: %result% >> %temp%\bp_update.log
if ""%result%""==""No"" (
echo Aborting update >> %temp%\bp_update.log
exit /b 1
)
echo Creating temp directory... >> %temp%\bp_update.log
if exist ""{tempDir}"" (
echo Temp directory already exists >> %temp%\bp_update.log
) else (
mkdir ""{tempDir}"" 2>> %temp%\bp_update.log
if errorlevel 1 (
echo Failed to create temp directory >> %temp%\bp_update.log
exit /b 1
)
)
echo Starting download... >> %temp%\bp_update.log
powershell -command ""$ProgressPreference = 'SilentlyContinue'; (New-Object System.Net.WebClient).DownloadFile('{downloadUrl}', '{tempDir}\installer.zip')"" >> %temp%\bp_update.log 2>&1
if not exist ""{tempDir}\installer.zip"" (
echo Download failed - installer.zip not found >> %temp%\bp_update.log
exit /b 1
)
:wait
echo Waiting for TheBazaar to close... >> %temp%\bp_update.log
taskkill /F /IM TheBazaar.exe >nul 2>&1
if not ERRORLEVEL 1 (
timeout /t 2 /nobreak
goto wait
)
echo Starting extraction... >> %temp%\bp_update.log
powershell -command ""$ProgressPreference = 'SilentlyContinue'; Expand-Archive -Path '{tempDir}\installer.zip' -DestinationPath '{tempDir}' -Force"" >> %temp%\bp_update.log 2>&1
if not exist ""{tempDir}\BazaarPlannerMod.dll"" (
echo Extraction failed - BazaarPlannerMod.dll not found >> %temp%\bp_update.log
exit /b 1
)
echo Cleaning up old files... >> %temp%\bp_update.log
del /F ""{currentDllPath}"" 2>> %temp%\bp_update.log
echo Installing update... >> %temp%\bp_update.log
set ""random_name=%random%%random%.dll""
copy /Y ""{tempDir}\BazaarPlannerMod.dll"" ""{Path.GetDirectoryName(currentDllPath)}\%random_name%"" 2>> %temp%\bp_update.log
echo Cleaning up... >> %temp%\bp_update.log
timeout /t 2 /nobreak
rmdir /S /Q ""{tempDir}"" 2>> %temp%\bp_update.log
powershell -command ""Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.MessageBox]::Show('BazaarPlanner auto-update successful, you are now on version {latestVersion}, please relaunch game', 'BazaarPlanner Update')""
del ""%~f0""
";
File.WriteAllText(batchPath, batchContent);
// Start the batch file and wait for it to complete
var process = Process.Start(batchPath);
await Task.Run(() => {
process.WaitForExit();
return process.ExitCode;
});
}
catch (Exception ex)
{
Logger.LogError($"Error installing update: {ex.Message}");
}
}
}