-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathPathingModule.cs
244 lines (200 loc) · 10.4 KB
/
PathingModule.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
using Blish_HUD;
using Blish_HUD.Modules;
using Blish_HUD.Modules.Managers;
using Blish_HUD.Settings;
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.Threading.Tasks;
using BhModule.Community.Pathing.Entity;
using BhModule.Community.Pathing.Scripting;
using BhModule.Community.Pathing.Scripting.Console;
using BhModule.Community.Pathing.UI.Views;
using Blish_HUD.Controls;
using Blish_HUD.Entities;
using Blish_HUD.Settings.UI.Views;
using Microsoft.Xna.Framework.Input;
using Blish_HUD.Graphics.UI;
using Blish_HUD.GameIntegration;
using Blish_HUD.Content;
namespace BhModule.Community.Pathing {
[Export(typeof(Module))]
public class PathingModule : Module {
private static readonly Logger Logger = Logger.GetLogger<PathingModule>();
#region Service Managers
internal SettingsManager SettingsManager => this.ModuleParameters.SettingsManager;
internal ContentsManager ContentsManager => this.ModuleParameters.ContentsManager;
internal DirectoriesManager DirectoriesManager => this.ModuleParameters.DirectoriesManager;
internal Gw2ApiManager Gw2ApiManager => this.ModuleParameters.Gw2ApiManager;
#endregion
internal static PathingModule Instance { get; private set; }
public ScriptEngine ScriptEngine { get; private set; }
public ModuleSettings Settings { get; private set; }
public TabbedWindow2 SettingsWindow { get; private set; }
public PackInitiator PackInitiator { get; private set; }
public MarkerPackRepo.MarkerPackRepo MarkerPackRepo { get; private set; }
private CornerIcon _pathingIcon;
private bool _packsLoading = false;
public Tab PackSettingsTab { get; private set; }
public Tab MapSettingsTab { get; private set; }
public Tab KeybindSettingsTab { get; private set; }
public Tab ScriptSettingsTab { get; private set; }
public Tab MarkerRepoTab { get; private set; }
private ConsoleWindow _scriptConsoleWindow;
[ImportingConstructor]
public PathingModule([Import("ModuleParameters")] ModuleParameters moduleParameters) : base(moduleParameters) {
Instance = this;
}
protected override void DefineSettings(SettingCollection settings) {
this.Settings = new ModuleSettings(this, settings);
}
private IEnumerable<ContextMenuStripItem> GetPathingMenuItems() {
// Pack initiator gets to inject some menu items, first:
if (this.PackInitiator != null) {
foreach (var menuItem in this.PackInitiator.GetPackMenuItems()) {
menuItem.Enabled = menuItem.Enabled && !_packsLoading;
yield return menuItem;
}
}
// Script Console - we only show if it's enabled or if the user is holding down shift.
if (this.Settings.ScriptsConsoleEnabled.Value || GameService.Input.Keyboard.ActiveModifiers.HasFlag(ModifierKeys.Shift)) {
var scriptConsole = new ContextMenuStripItem() {
Text = "Script Console" // TODO: Localize "Script Console"
};
scriptConsole.Click += (_, _) => ShowScriptWindow();
yield return scriptConsole;
}
// Open Settings
var openSettings = new ContextMenuStripItem() {
Text = "Pathing Module Settings" // TODO: Localize "Pathing Module Settings"
};
openSettings.Click += (_, _) => {
if (SettingsWindow.SelectedTab == MarkerRepoTab) {
SettingsWindow.SelectedTab = PackSettingsTab;
if (SettingsWindow.Visible) return;
}
SettingsWindow.ToggleWindow();
};
yield return openSettings;
}
protected override void Initialize() {
// SOTO Fix
if (DateTime.UtcNow.Date >= new DateTime(2023, 8, 22, 0, 0, 0, DateTimeKind.Utc) && Program.OverlayVersion < new SemVer.Version(1, 1, 0)) {
try {
var tacoActive = typeof(TacOIntegration).GetProperty(nameof(TacOIntegration.TacOIsRunning)).GetSetMethod(true);
tacoActive?.Invoke(GameService.GameIntegration.TacO, new object[] { true });
} catch { /* NOOP */ }
}
_pathingIcon = new CornerIcon() {
IconName = Strings.General_UiName,
Icon = ContentsManager.GetTexture(@"png\pathing-icon.png"),
Priority = "Markers & Trails".GetHashCode()
};
SettingsWindow = new TabbedWindow2(
ContentsManager.GetTexture(@"png\controls\156006.png"),
new Rectangle(35, 36, 900, 640),
new Rectangle(95, 42, 783 + 38, 592)
) {
Title = Strings.General_UiName,
Parent = GameService.Graphics.SpriteScreen,
Location = new Point(100, 100),
Emblem = this.ContentsManager.GetTexture(@"png\controls\1615829.png"),
Id = $"{this.Namespace}_SettingsWindow",
SavesPosition = true,
};
PackSettingsTab = new Tab(ContentsManager.GetTexture(@"png\156740+155150.png"), () => new SettingsView(this.Settings.PackSettings), Strings.Window_MainSettingsTab);
MapSettingsTab = new Tab(ContentsManager.GetTexture(@"png\157123+155150.png"), () => new SettingsView(this.Settings.MapSettings), Strings.Window_MapSettingsTab);
ScriptSettingsTab = new Tab(AsyncTexture2D.FromAssetId(156701), () => new SettingsView(this.Settings.ScriptSettings), "Script Options");
KeybindSettingsTab = new Tab(ContentsManager.GetTexture(@"png\156734+155150.png"), () => new SettingsView(this.Settings.KeyBindSettings), Strings.Window_KeyBindSettingsTab);
MarkerRepoTab = new Tab(AsyncTexture2D.FromAssetId(156909), () => new PackRepoView(this), Strings.Window_DownloadMarkerPacks);
SettingsWindow.Tabs.Add(PackSettingsTab);
SettingsWindow.Tabs.Add(MapSettingsTab);
SettingsWindow.Tabs.Add(ScriptSettingsTab);
SettingsWindow.Tabs.Add(KeybindSettingsTab);
SettingsWindow.Tabs.Add(MarkerRepoTab);
_pathingIcon.Click += delegate {
if (GameService.Input.Keyboard.ActiveModifiers.HasFlag(ModifierKeys.Ctrl)) {
this.Settings.GlobalPathablesEnabled.Value = !this.Settings.GlobalPathablesEnabled.Value;
} else if (_pathingIcon.Enabled) {
TogglePathingContextMenu();
}
};
_pathingIcon.RightMouseButtonPressed += delegate {
if (_pathingIcon.Enabled) {
TogglePathingContextMenu();
}
};
}
private void ShowScriptWindow() {
_scriptConsoleWindow ??= new ConsoleWindow(this);
_scriptConsoleWindow.Show();
_scriptConsoleWindow.BringToFront();
_scriptConsoleWindow.FormClosed += (_,_) => _scriptConsoleWindow = null;
}
private void TogglePathingContextMenu() {
var pathingContextMenuStrip = new ContextMenuStrip();
pathingContextMenuStrip.AddMenuItems(GetPathingMenuItems());
pathingContextMenuStrip.Show(_pathingIcon);
}
private void UpdateModuleLoading(string loadingMessage) {
_pathingIcon.LoadingMessage = loadingMessage;
if (this.RunState == ModuleRunState.Loaded && _pathingIcon != null) {
_pathingIcon.LoadingMessage = loadingMessage;
_packsLoading = !string.IsNullOrWhiteSpace(loadingMessage);
if (!_packsLoading) {
_pathingIcon.BasicTooltipText = Strings.General_UiName;
}
}
}
public IProgress<string> GetModuleProgressHandler() {
// TODO: Consider enforcing a source so that multiple items can be shown in the loading tooltip.
return new Progress<string>(UpdateModuleLoading);
}
protected override async Task LoadAsync() {
var sw = Stopwatch.StartNew();
this.ScriptEngine = new ScriptEngine(this);
this.MarkerPackRepo = new MarkerPackRepo.MarkerPackRepo(this);
this.MarkerPackRepo.Init();
this.PackInitiator = new PackInitiator(DirectoriesManager.GetFullDirectoryPath("markers"), this, GetModuleProgressHandler());
await this.PackInitiator.Init();
sw.Stop();
Logger.Debug($"Took {sw.ElapsedMilliseconds} ms to complete loading Pathing module...");
}
public override IView GetSettingsView() {
return new SettingsHintView((() => {
SettingsWindow.SelectedTab = PackSettingsTab;
SettingsWindow.Show();
},
() => {
SettingsWindow.SelectedTab = MarkerRepoTab;
SettingsWindow.Show();
}, this.PackInitiator));
}
protected override void Update(GameTime gameTime) {
this.ScriptEngine?.Update(gameTime);
this.PackInitiator?.Update(gameTime);
}
private void UnloadPathingElements() {
var entities = GameService.Graphics.World.Entities as IEntity[];
foreach (var entity in entities) {
if (entity is IPathingEntity) {
GameService.Graphics.World.RemoveEntity(entity);
}
}
}
protected override void Unload() {
this.ScriptEngine?.Unload();
this.Settings?.Unload();
this.PackInitiator?.Unload();
_pathingIcon?.Dispose();
SettingsWindow?.Dispose();
_scriptConsoleWindow?.Dispose();
// Help to ensure that all pathing entities
// are removed regardless of our current state.
UnloadPathingElements();
Instance = null;
}
}
}