Repository navigation
Expand file tree
/
Copy pathMapDeviceActivator.MapDeviceSlots.cs
More file actions
159 lines (135 loc) · 5.08 KB
/
Copy pathMapDeviceActivator.MapDeviceSlots.cs
File metadata and controls
159 lines (135 loc) · 5.08 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using ExileCore.PoEMemory;
using ExileCore.PoEMemory.Components;
using ExileCore.PoEMemory.MemoryObjects;
using Microsoft.CSharp.RuntimeBinder;
namespace MapDeviceActivator;
public partial class MapDeviceActivator
{
private static bool MapDeviceHasMap(object mapDeviceWindow)
{
var map = GetMapDeviceMap(mapDeviceWindow);
return map != null && IsMap(map);
}
private bool MapDeviceHasRequiredScarabs()
{
var requiredScarabs = GetRequiredScarabs().ToList();
if (requiredScarabs.Count == 0)
return true;
var scarabs = GetMapDeviceScarabNames().Select(NormalizeItemName).Where(x => !string.IsNullOrWhiteSpace(x)).ToList();
if (scarabs.Count == 0)
return false;
foreach (var requiredScarab in requiredScarabs)
{
var normalizedRequiredScarab = NormalizeItemName(requiredScarab);
var index = scarabs.FindIndex(x => string.Equals(x, normalizedRequiredScarab, StringComparison.OrdinalIgnoreCase));
if (index < 0)
return false;
scarabs.RemoveAt(index);
}
return true;
}
private static Entity GetMapDeviceMap(object mapDeviceWindow)
{
dynamic window = mapDeviceWindow;
dynamic atlas = Instance?.GameController?.IngameState?.IngameUi?.Atlas;
var atlasMapSlot = GetDynamicValue(() => atlas.MapDeviceWindow.MapSlot);
var windowMapSlot = GetDynamicValue(() => window.MapSlot);
return GetVisibleSlotEntity(atlasMapSlot) ?? GetVisibleSlotEntity(windowMapSlot);
}
private static IEnumerable<string> GetMapDeviceScarabNames()
{
dynamic atlas = Instance?.GameController?.IngameState?.IngameUi?.Atlas;
var scarabSlots = GetDynamicValue(() => atlas.MapDeviceWindow.ScarabSlots);
if (scarabSlots is not IEnumerable slots || scarabSlots is string)
yield break;
foreach (var slot in slots)
{
dynamic dynamicSlot = slot;
var visibleItems = GetDynamicValue(() => dynamicSlot.VisibleInventoryItems);
if (visibleItems is not IEnumerable items || visibleItems is string)
continue;
foreach (var visibleItem in items)
{
dynamic dynamicVisibleItem = visibleItem;
var item = GetDynamicValue(() => dynamicVisibleItem.Item) as Entity;
var name = GetBaseComponentName(item);
if (!string.IsNullOrWhiteSpace(name) && IsScarabSlotItem(item, name))
yield return name;
}
}
}
private static string GetBaseComponentName(Entity item)
{
if (item == null || !item.TryGetComponent<Base>(out var baseComponent))
return string.Empty;
return GetDynamicValue(() => ((dynamic)baseComponent).Name) as string ??
GetDynamicValue(() => ((dynamic)baseComponent).BaseName) as string ??
string.Empty;
}
private static bool IsScarabSlotItem(Entity item, string name)
{
dynamic dynamicItem = item;
var metadata = GetDynamicValue(() => dynamicItem.Metadata) as string;
return metadata?.StartsWith("Metadata/Items/Scarabs/", StringComparison.OrdinalIgnoreCase) == true ||
name.Contains("Scarab", StringComparison.OrdinalIgnoreCase);
}
private static string NormalizeItemName(string name)
{
return Regex.Replace(name ?? string.Empty, @"\s+", " ").Trim();
}
private static object GetDynamicValue(Func<object> getter)
{
try
{
return getter();
}
catch (RuntimeBinderException)
{
return null;
}
catch (NullReferenceException)
{
return null;
}
catch (ArgumentOutOfRangeException)
{
return null;
}
}
private static bool GetDynamicBool(Func<object> getter)
{
return GetDynamicValue(getter) is true;
}
private static Entity GetVisibleSlotEntity(object slot)
{
if (slot == null)
return null;
dynamic dynamicSlot = slot;
var visibleItems = GetDynamicValue(() => dynamicSlot.VisibleInventoryItems);
if (visibleItems is not IEnumerable enumerable || visibleItems is string)
return null;
foreach (var visibleItem in enumerable)
{
dynamic dynamicVisibleItem = visibleItem;
var item = GetDynamicValue(() => dynamicVisibleItem.Item) as Entity;
if (item is { IsValid: true })
return item;
}
return null;
}
private static bool IsMap(Entity item)
{
return item.TryGetComponent<MapKey>(out _);
}
private IEnumerable<string> GetRequiredScarabs()
{
return GetScarabSettings()
.Select(x => x.Value)
.Where(x => !string.IsNullOrWhiteSpace(x) && !string.Equals(x, "None", StringComparison.OrdinalIgnoreCase));
}
}