diff options
Diffstat (limited to 'ArchipelagoManager.cs')
| -rw-r--r-- | ArchipelagoManager.cs | 133 |
1 files changed, 133 insertions, 0 deletions
| diff --git a/ArchipelagoManager.cs b/ArchipelagoManager.cs new file mode 100644 index 0000000..7affa64 --- /dev/null +++ b/ArchipelagoManager.cs | |||
| @@ -0,0 +1,133 @@ | |||
| 1 | using Archipelago.MultiClient.Net; | ||
| 2 | using Archipelago.MultiClient.Net.Enums; | ||
| 3 | using Archipelago.MultiClient.Net.Models; | ||
| 4 | using Archipelago.MultiClient.Net.Packets; | ||
| 5 | using System; | ||
| 6 | using System.Collections.Generic; | ||
| 7 | using System.Linq; | ||
| 8 | using System.Reflection; | ||
| 9 | using System.Text; | ||
| 10 | using System.Threading.Tasks; | ||
| 11 | using UnityEngine.SceneManagement; | ||
| 12 | using static ManifoldGardenArchipelago.GameData; | ||
| 13 | |||
| 14 | namespace ManifoldGardenArchipelago | ||
| 15 | { | ||
| 16 | public class ArchipelagoManager | ||
| 17 | { | ||
| 18 | private ArchipelagoSession _session; | ||
| 19 | private int _itemIndex = 0; | ||
| 20 | |||
| 21 | public Dictionary<string, Dictionary<int, bool>> chainListenersByScene = new(); | ||
| 22 | |||
| 23 | public ArchipelagoManager() | ||
| 24 | { | ||
| 25 | foreach (var item in GameData.door_by_item) | ||
| 26 | { | ||
| 27 | foreach (var chainListenerReference in item.Value) | ||
| 28 | { | ||
| 29 | if (!chainListenersByScene.TryGetValue(chainListenerReference.scene, out var listenersInScene)) | ||
| 30 | { | ||
| 31 | listenersInScene = []; | ||
| 32 | chainListenersByScene.Add(chainListenerReference.scene, listenersInScene); | ||
| 33 | } | ||
| 34 | |||
| 35 | listenersInScene[chainListenerReference.index] = false; | ||
| 36 | } | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | public async Task<LoginResult> Connect(string url, string slotName, string password) | ||
| 41 | { | ||
| 42 | LoginResult result; | ||
| 43 | try | ||
| 44 | { | ||
| 45 | _session = ArchipelagoSessionFactory.CreateSession(url); | ||
| 46 | |||
| 47 | RoomInfoPacket roomInfoPacket = await _session.ConnectAsync(); | ||
| 48 | |||
| 49 | result = await _session.LoginAsync("Manifold Garden", slotName, ItemsHandlingFlags.AllItems, null, null, null, password == "" ? null : password); | ||
| 50 | } | ||
| 51 | catch (Exception ex) | ||
| 52 | { | ||
| 53 | return new LoginFailure(ex.GetBaseException().Message); | ||
| 54 | } | ||
| 55 | |||
| 56 | return result; | ||
| 57 | } | ||
| 58 | |||
| 59 | ~ArchipelagoManager() | ||
| 60 | { | ||
| 61 | Disconnect(); | ||
| 62 | } | ||
| 63 | |||
| 64 | public void Disconnect() | ||
| 65 | { | ||
| 66 | if (_session == null) | ||
| 67 | { | ||
| 68 | return; | ||
| 69 | } | ||
| 70 | |||
| 71 | _session.Socket.DisconnectAsync(); | ||
| 72 | _session = null; | ||
| 73 | } | ||
| 74 | |||
| 75 | public void Update() | ||
| 76 | { | ||
| 77 | if (_session == null) | ||
| 78 | { | ||
| 79 | return; | ||
| 80 | } | ||
| 81 | |||
| 82 | if (_session.Items.AllItemsReceived.Count > _itemIndex) | ||
| 83 | { | ||
| 84 | for (int i = _itemIndex; i < _session.Items.AllItemsReceived.Count; i++) | ||
| 85 | { | ||
| 86 | ItemInfo item = _session.Items.AllItemsReceived[i]; | ||
| 87 | string itemName = item.ItemName; | ||
| 88 | |||
| 89 | if (GameData.door_by_item.TryGetValue(itemName, out List<GameData.ChainListenerReference> door)) | ||
| 90 | { | ||
| 91 | foreach (GameData.ChainListenerReference chainListenerReference in door) | ||
| 92 | { | ||
| 93 | chainListenersByScene[chainListenerReference.scene][chainListenerReference.index] = true; | ||
| 94 | |||
| 95 | if (SceneManager.GetSceneByName(chainListenerReference.scene) is Scene doorScene && doorScene.isLoaded) | ||
| 96 | { | ||
| 97 | LevelSystems levelSystems = doorScene.GetRootGameObjects().Single((obj) => obj.name == "Level Systems").GetComponent<LevelSystems>(); | ||
| 98 | if (levelSystems.isActiveAndEnabled) | ||
| 99 | { | ||
| 100 | LineDoorController ldc = levelSystems.chainListeners[chainListenerReference.index].GetComponent<LineDoorController>(); | ||
| 101 | ldc.chains.Clear(); | ||
| 102 | |||
| 103 | FieldInfo fieldInfo = typeof(LineDoorController).GetField("doorShouldOpenImmediatelyOnEnable", BindingFlags.Instance | BindingFlags.NonPublic); | ||
| 104 | fieldInfo.SetValue(ldc, true); | ||
| 105 | |||
| 106 | MethodInfo methodInfo = typeof(LineDoorController).GetMethod("SetDoorOpenCloseStateAnimated", BindingFlags.NonPublic | BindingFlags.Instance); | ||
| 107 | methodInfo.Invoke(ldc, [true, false]); | ||
| 108 | } | ||
| 109 | } | ||
| 110 | } | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | _itemIndex = _session.Items.AllItemsReceived.Count; | ||
| 115 | |||
| 116 | /* | ||
| 117 | var hi = new StringBuilder(); | ||
| 118 | foreach (var thing in chainListenersByScene) | ||
| 119 | { | ||
| 120 | hi.AppendLine(thing.Key); | ||
| 121 | |||
| 122 | foreach (var thing2 in thing.Value) | ||
| 123 | { | ||
| 124 | hi.AppendLine($" {thing2.Key}: {thing2.Value}"); | ||
| 125 | } | ||
| 126 | } | ||
| 127 | |||
| 128 | Plugin.Logger.LogInfo( hi.ToString() ); | ||
| 129 | */ | ||
| 130 | } | ||
| 131 | } | ||
| 132 | } | ||
| 133 | } | ||
