-
Notifications
You must be signed in to change notification settings - Fork 4
/
ArchipelagoClient.cs
570 lines (525 loc) · 21.2 KB
/
ArchipelagoClient.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
using System;
using System.Collections.Generic;
using System.Linq;
using Archipelago.MultiClient.Net;
using Archipelago.MultiClient.Net.Enums;
using Archipelago.MultiClient.Net.BounceFeatures.DeathLink;
using BepInEx.Logging;
using LunacidAP.Data;
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
using System.Threading.Tasks;
using static LunacidAP.Data.LunacidLocations;
using Archipelago.MultiClient.Net.Models;
using Archipelago.Gifting.Net.Service;
using Archipelago.Gifting.Net.Gifts.Versions.Current;
using static LunacidAP.Data.LunacidGifts;
using Archipelago.Gifting.Net.Utilities.CloseTraitParser;
namespace LunacidAP
{
public class ArchipelagoClient : MonoBehaviour
{
public const string GAME_NAME = "Lunacid";
public const string GIFT_COLOR = "#9DAE11";
private static ManualLogSource _log;
public static ArchipelagoClient AP;
private static GameObject Obj;
public System.Random RandomStatic;
public int SlotID;
private int cheatedCount;
private int Stack;
private CONTROL Control;
private POP_text_scr Popup;
public ArchipelagoSession Session;
public SlotData SlotData { get; private set; }
public GiftingService Gifting { get; private set; }
private GiftHelper giftHelper { get; set; }
public const long LOCATION_INIT_ID = 771111110;
private bool _connected;
public bool Authenticated
{
get { return _connected; }
set
{
_connected = value;
}
}
private bool _connecting;
public bool IsConnecting
{
get { return _connecting; }
set
{
_connecting = value;
}
}
private DeathLinkService _deathLinkService;
public bool IsCurrentlyDeathLinked = false;
public string[] CurrentDLData = new string[2] { "", "" };
public static bool IsInGame = false;
public static bool HasReceivedItems = false;
public static readonly List<string> ScenesNotInGame = new() { "MainMenu", "CHAR_CREATE", "Gameover" };
public readonly SortedDictionary<long, ArchipelagoItem> LocationTable = new() { };
public static void Setup(ManualLogSource log)
{
_log = log;
Obj = new();
Obj.name = "ArchipelagoClient";
DontDestroyOnLoad(Obj);
AP = Obj.AddComponent<ArchipelagoClient>();
}
public IEnumerator Connect(string slotName, string hostName, int port, string password, bool reconnect = false, int slotID = -1)
{
_log.LogInfo($"Connecting: {slotName}, {hostName}, {port}");
IsConnecting = true;
Authenticated = false;
var minimumVersion = new Version(0, 5, 0);
Session = ArchipelagoSessionFactory.CreateSession(hostName, port);
var connectTask = Task.Run(Session.ConnectAsync);
yield return new WaitUntil(() => connectTask.IsCompleted);
if (!connectTask.Status.HasFlag(TaskStatus.RanToCompletion))
{
_log.LogError($"Failed to connect to Archipelago server at {hostName}:{port}.");
yield break;
}
var loginTask = Task.Run(async () => await Session.LoginAsync(GAME_NAME, slotName, ItemsHandlingFlags.AllItems, minimumVersion, password: password));
yield return new WaitUntil(() => loginTask.IsCompleted);
if (loginTask.IsFaulted)
{
_log.LogError(loginTask.Exception.GetBaseException().Message);
IsConnecting = false;
yield break;
}
LoginResult result = loginTask.Result;
if (!result.Successful)
{
LoginFailure failure = (LoginFailure)result;
_log.LogError(string.Join("\n", failure.Errors));
IsConnecting = false;
yield break;
}
LoginSuccessful login = (LoginSuccessful)result;
SlotID = login.Slot;
SlotData = new SlotData(login.SlotData, _log);
int seed = SlotData.GetSlotSetting("seed", 0);
if (ConnectionData.Seed != 0 && ConnectionData.Seed != seed)
{
_log.LogError("The server's seed does not match the save file's seed. Make sure you're connecting with the right save file");
Disconnect();
IsConnecting = false;
yield break;
}
var gameVersion = PluginInfo.PLUGIN_VERSION.Split('.');
var archipelagoVersion = SlotData.ClientVersion.Split('.');
if (int.Parse(gameVersion[0]) > int.Parse(archipelagoVersion[0]) || int.Parse(gameVersion[1]) > int.Parse(archipelagoVersion[1]))
{
_log.LogError($"The server's game was made for {SlotData.ClientVersion}, but this game is {PluginInfo.PLUGIN_VERSION}.");
IsConnecting = false;
yield break;
}
else if (int.Parse(gameVersion[2]) > int.Parse(archipelagoVersion[2]))
{
_log.LogWarning($"The server's game was made for {SlotData.ClientVersion} but the game is newer. Should be fine though.");
}
ConnectionData.Seed = seed;
RandomStatic = new System.Random(seed);
if (!reconnect && ConnectionData.ScoutedLocations.Count() == 0)
{
BuildLocationTable();
// Scout unchecked locations
var uncheckedLocationIDs = from locationID in LocationTable.Keys select locationID;
Task<Dictionary<long, ScoutedItemInfo>> scoutedInfoTask = Task.Run(async () => await Session.Locations.ScoutLocationsAsync(false, uncheckedLocationIDs.ToArray()));
//Task<LocationInfoPacket> locationInfoTask = Task.Run(async () => await Session.Locations.ScoutLocationsAsync(false, uncheckedLocationIDs.ToArray()));
yield return new WaitUntil(() => scoutedInfoTask.IsCompleted);
if (scoutedInfoTask.IsFaulted)
{
_log.LogError(scoutedInfoTask.Exception.GetBaseException().Message);
yield break;
}
var scoutedInfo = scoutedInfoTask.Result;
foreach (var item in scoutedInfo.Values)
{
int locationID = (int)item.LocationId;
LocationTable[locationID] = new ArchipelagoItem(item, false);
}
ConnectionData.ScoutedLocations = LocationTable;
}
CommunionHint.DetermineHints(SlotData.Seed);
// Sync checked locations
var checkedLocationIDs = from locationID in ConnectionData.ScoutedLocations.Keys where IsLocationChecked(locationID) select locationID;
var locationCheckTask = Task.Run(async () => await Session.Locations.CompleteLocationChecksAsync(checkedLocationIDs.ToArray()));
yield return new WaitUntil(() => locationCheckTask.IsCompleted);
if (locationCheckTask.IsFaulted)
{
_log.LogError("Locating Syncing has failed.");
_log.LogError(locationCheckTask.Exception.GetBaseException().Message);
IsConnecting = false;
yield break;
}
// Sync collected locations
foreach (long locationID in Session.Locations.AllLocationsChecked)
{
if (!ConnectionData.CompletedLocations.Contains(locationID))
{
ConnectionData.CompletedLocations.Add(locationID);
}
}
// Connection successful
ConnectionData.WriteConnectionData(hostName, port, slotName, password);
Authenticated = true;
if (slotID > -1)
{
SaveHandler.SaveData(slotID); // to ensure the updated information isn't lost at load.
}
Session.Socket.ErrorReceived += Session_ErrorReceived;
Session.Socket.SocketClosed += Session_SocketClosed;
if (login.SlotData["death_link"].ToString() == "1")
{
ConnectionData.DeathLink = true;
InitializeDeathLink();
}
SetUpGifting();
cheatedCount = 0;
IsConnecting = false;
_log.LogInfo("Successfully connected to server!");
}
public void SetUpGifting()
{
Gifting = new GiftingService(Session);
BKTreeCloseTraitParser<string> closeTraitParser = new ();
giftHelper = new GiftHelper(_log, closeTraitParser);
Gifting.OpenGiftBox();
Gifting.SubscribeToNewGifts(Gifting_WhenGiftWasReceived);
Gifting.CheckGiftBox();
foreach (var lunacidGiftItem in LunacidTraits.LunacidItemTraits)
{
closeTraitParser.RegisterAvailableGift(lunacidGiftItem.Key, lunacidGiftItem.Value);
}
}
public void AttemptConnectFromDeath(int currentSave)
{
StartCoroutine(Connect(ConnectionData.SlotName, ConnectionData.HostName, ConnectionData.Port, ConnectionData.Password, false, currentSave));
}
public void Gifting_WhenGiftWasReceived(Dictionary<string, Gift> incomingGifts)
{
Control = GameObject.Find("CONTROL").GetComponent<CONTROL>();
Popup = Control.PAPPY;
StartCoroutine(giftHelper.HandleIncomingGifts());
}
public void PrepareGift(GiftVector giftVector, string slotName)
{
StartCoroutine(SendGift(giftVector, slotName));
}
private IEnumerator SendGift(GiftVector giftVector, string slotName)
{
var sendGiftTask = Task.Run(async () => await Gifting.SendGiftAsync(giftVector.GiftItem, giftVector.GiftTraits, slotName, 0));
yield return new WaitUntil(() => sendGiftTask.IsCompleted);
if (sendGiftTask.IsFaulted)
{
_log.LogError(sendGiftTask.Exception.GetBaseException().Message);
yield break;
}
var wasSent = sendGiftTask.Result;
if (wasSent)
{
Control = GameObject.Find("CONTROL").GetComponent<CONTROL>();
Popup = Control.PAPPY;
if (Popup is null)
{
_log.LogWarning("We tried to tell you about sending an item but we got null output!");
yield break;
}
Popup.POP($"Gifted <color={GIFT_COLOR}>{giftVector.GiftItem.Name}</color> to {slotName}", 1f, 0);
}
else
{
_log.LogWarning($"Player {slotName} isn't accepting gifts.");
}
}
private void Session_SocketClosed(string _)
{
if (Authenticated)
{
Authenticated = false;
StartCoroutine(TryReconnect());
}
}
private IEnumerator TryReconnect()
{
while (!Authenticated && IsInGame)
{
yield return new WaitForSeconds(60);
yield return Connect(ConnectionData.SlotName, ConnectionData.HostName, ConnectionData.Port, ConnectionData.Password, true);
}
}
public void Session_ErrorReceived(Exception e, string message)
{
_log.LogError(message);
if (e != null) _log.LogError(e.ToString());
//Disconnect();
}
public void Disconnect()
{
if (Session != null && Session.Socket != null)
{
Session.Socket.DisconnectAsync();
_log.LogInfo($"Disconnecting.");
}
Session = null;
Authenticated = false;
HasReceivedItems = false;
}
private void Update()
{
Control = GameObject.Find("CONTROL").GetComponent<CONTROL>();
if (Session is not null && IsInNormalGameState())
{
if (Session.Items.Any())
{
HandleReceivedItems();
}
}
}
private void HandleReceivedItems()
{
var item = Session.Items.DequeueItem();
if (item.LocationId >= 0 && !ConnectionData.ReceivedItems.Any(x => x.PlayerId == item.Player && x.LocationId == item.LocationId && item.ItemId == x.ItemId))
{
var receivedItem = new ReceivedItem(item);
ConnectionData.ReceivedItems.Add(receivedItem);
StartCoroutine(ReceiveItem(receivedItem));
}
else if (item.LocationId < 0)
{
_log.LogInfo($"Cheated Count {cheatedCount} before increment vs {ConnectionData.CheatedCount}");
cheatedCount++;
if (cheatedCount > ConnectionData.CheatedCount)
{
ConnectionData.CheatedCount = cheatedCount;
var receivedItem = new ReceivedItem(item);
ConnectionData.ReceivedItems.Add(receivedItem);
StartCoroutine(ReceiveItem(receivedItem));
}
}
}
private void AppendItemToReceived(ReceivedItem receivedItem)
{
foreach (var item in ConnectionData.ReceivedItems.ToList())
{
if (item.ItemId == receivedItem.ItemId && item.LocationId == item.LocationId && item.PlayerId == receivedItem.PlayerId)
{
return;
}
ConnectionData.ReceivedItems.Add(receivedItem);
}
}
public void InitializeDeathLink()
{
if (_deathLinkService == null)
{
_deathLinkService = Session.CreateDeathLinkService();
_deathLinkService.OnDeathLinkReceived += DeathLinkReceieved;
}
if (ConnectionData.DeathLink)
{
_deathLinkService.EnableDeathLink();
}
else
{
_deathLinkService.DisableDeathLink();
}
}
private void DeathLinkReceieved(DeathLink deathLink)
{
IsCurrentlyDeathLinked = true;
CurrentDLData[0] = deathLink.Source;
CurrentDLData[1] = deathLink.Cause;
}
public void SendDeathLink()
{
if (IsCurrentlyDeathLinked)
{
IsCurrentlyDeathLinked = false;
return;
}
var deathLink = new DeathLink(ConnectionData.SlotName, "perished in the Great Well");
_deathLinkService.SendDeathLink(deathLink);
}
public IEnumerator ReceiveDeathLink(string source, string reason)
{
yield return new WaitForSeconds(2f);
var you = GameObject.Find("PLAYER").GetComponent<Player_Control_scr>();
Control = GameObject.Find("CONTROL").GetComponent<CONTROL>();
Control.PAPPY.POP($"The death of {source} by {reason} causes you to perish.", 1f, 1);
you.Die();
IsCurrentlyDeathLinked = false;
}
private void BuildLocationTable()
{
List<int> locations = new();
foreach (var region in LunacidLocations.APLocationData)
{
foreach (var location in region.Value)
{
var allowedList = new List<string>() { "TOWER", "ARENA2" };
if (location.IgnoreLocationHandler == true && !allowedList.Contains(region.Key))
{
continue;
}
if (SlotData.ExcludeCoinLocations && LunacidLocations.CoinLocations.Contains(location.APLocationName))
{
continue;
}
if (SlotData.ExcludeTower && LunacidLocations.TowerLocations.Contains(location.APLocationName))
{
continue;
}
if (SlotData.StartingClass == 5 && location.APLocationName == "WR: Demi's Introduction Gift")
{
continue;
}
locations.Add((int)location.APLocationID);
}
}
if (SlotData.Shopsanity)
{
foreach (var location in LunacidLocations.ShopLocations)
{
if (location.IgnoreLocationHandler == true)
{
continue;
}
locations.Add((int)location.APLocationID);
}
}
if (SlotData.Dropsanity != Dropsanity.Off)
{
foreach (var location in LunacidLocations.UniqueDropLocations)
{
if (location.IgnoreLocationHandler == true)
{
continue;
}
locations.Add((int)location.APLocationID);
}
}
if (SlotData.Dropsanity == Dropsanity.Randomized)
{
foreach (var location in LunacidLocations.OtherDropLocations)
{
if (location.IgnoreLocationHandler == true)
{
continue;
}
locations.Add((int)location.APLocationID);
}
}
foreach (var id in locations)
{
LocationTable[id] = null;
}
}
private IEnumerator ReceiveItem(ReceivedItem item)
{
var instanceStack = Stack + 1;
Stack++;
while (instanceStack < Stack)
{
yield return new WaitForSeconds(1f);
}
var self = false;
if (item.PlayerName == ConnectionData.SlotName) self = true;
ItemHandler.GiveLunacidItem(item, self);
Stack--;
}
public static readonly Dictionary<ItemFlags, string> ProgressionFlagToString = new()
{
{ItemFlags.Advancement, "Progression"},
{ItemFlags.NeverExclude, "Useful"},
{ItemFlags.None, "Filler"},
{ItemFlags.Trap, "Trap"}
};
public static string FlagColor(ItemFlags itemFlag)
{
if (itemFlag.HasFlag(ItemFlags.Advancement))
{
return "#AF99EF";
}
else if (itemFlag.HasFlag(ItemFlags.NeverExclude))
{
return "#6D8BE8";
}
else if (itemFlag.HasFlag(ItemFlags.Trap))
{
return "#FA8072";
}
else if (itemFlag.HasFlag(ItemFlags.None))
{
return "#00EEEE";
}
return "#FFFFFF";
}
public ArchipelagoItem ScoutLocation(long locationId)
{
return ConnectionData.ScoutedLocations[locationId];
}
public bool IsLocationChecked(long location)
{
return ConnectionData.CompletedLocations.Contains(location);
}
public bool IsLocationChecked(string location)
{
var locationID = GetLocationIDFromName(location);
return ConnectionData.CompletedLocations.Contains(locationID);
}
public bool IsLocationChecked(LocationData location)
{
return ConnectionData.CompletedLocations.Contains(location.APLocationID);
}
public long GetLocationIDFromName(string locationName)
{
return Session.Locations.GetLocationIdFromName(GAME_NAME, locationName);
}
public string GetLocationNameFromID(long location)
{
return Session.Locations.GetLocationNameFromId(location, "Lunacid");
}
public string GetPlayerNameFromSlot(int slot)
{
return Session.Players.GetPlayerName(slot);
}
public bool WasItemReceived(string itemName)
{
foreach (var receivedItem in ConnectionData.ReceivedItems)
{
if (receivedItem.ItemName == itemName)
{
return true;
}
}
return false;
}
public bool WasItemCountReceived(string itemName, int count)
{
var foundCount = 0;
foreach (var receivedItem in ConnectionData.ReceivedItems)
{
if (receivedItem.ItemName == itemName)
{
foundCount += 1;
}
}
return count <= foundCount;
}
public bool HasGoal(Goal goal)
{
return SlotData.Ending.HasFlag(goal);
}
public bool IsInNormalGameState()
{
var isInEnding = new List<string>() { "WhatWillBeAtTheEnd", "END_A", "END_B", "END_E" }.Contains(SceneManager.GetActiveScene().name);
return Control.LOADED && IsInGame && !isInEnding && GameObject.Find("PLAYER") is not null && Control.Current_Gameplay_State == 0;
}
}
}