forked from Pathoschild/StardewMods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModEntry.cs
442 lines (386 loc) · 17.7 KB
/
ModEntry.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
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Newtonsoft.Json.Linq;
using Pathoschild.Stardew.Common;
using Pathoschild.Stardew.Common.Integrations.JsonAssets;
using Pathoschild.Stardew.LookupAnything.Components;
using Pathoschild.Stardew.LookupAnything.Framework;
using Pathoschild.Stardew.LookupAnything.Framework.Lookups;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewModdingAPI.Utilities;
using StardewValley;
using StardewValley.Menus;
namespace Pathoschild.Stardew.LookupAnything
{
/// <summary>The mod entry point.</summary>
internal class ModEntry : Mod
{
/*********
** Fields
*********/
/****
** Configuration
****/
/// <summary>The mod configuration.</summary>
private ModConfig Config = null!; // set in Entry
/// <summary>The configured key bindings.</summary>
private ModConfigKeys Keys => this.Config.Controls;
/// <summary>Provides metadata that's not available from the game data directly.</summary>
private Metadata? Metadata;
/// <summary>The relative path to the file containing data for the <see cref="Metadata"/> field.</summary>
private readonly string DatabaseFileName = "assets/data.json";
/****
** Validation
****/
/// <summary>Whether the metadata validation passed.</summary>
[MemberNotNullWhen(true, nameof(ModEntry.Metadata), nameof(ModEntry.GameHelper), nameof(ModEntry.TargetFactory), nameof(ModEntry.DebugInterface))]
private bool IsDataValid { get; set; }
/****
** State
****/
/// <summary>Provides utility methods for interacting with the game code.</summary>
private GameHelper? GameHelper;
/// <summary>Finds and analyzes lookup targets in the world.</summary>
private TargetFactory? TargetFactory;
/// <summary>Draws debug information to the screen.</summary>
private PerScreen<DebugInterface>? DebugInterface;
/// <summary>The previous menus shown before the current lookup UI was opened.</summary>
private readonly PerScreen<Stack<IClickableMenu>> PreviousMenus = new(() => new());
/*********
** Public methods
*********/
/// <summary>The mod entry point, called after the mod is first loaded.</summary>
/// <param name="helper">Provides methods for interacting with the mod directory, such as read/writing a config file or custom JSON files.</param>
public override void Entry(IModHelper helper)
{
// load config
this.Config = this.LoadConfig();
// load translations
I18n.Init(helper.Translation);
// load & validate database
this.Metadata = this.LoadMetadata();
this.IsDataValid = this.Metadata?.LooksValid() == true;
if (!this.IsDataValid)
{
this.Monitor.Log($"The {this.DatabaseFileName} file seems to be missing or corrupt. Lookups will be disabled.", LogLevel.Error);
this.IsDataValid = false;
}
// validate translations
if (!helper.Translation.GetTranslations().Any())
this.Monitor.Log("The translation files in this mod's i18n folder seem to be missing. The mod will still work, but you'll see 'missing translation' messages. Try reinstalling the mod to fix this.", LogLevel.Warn);
// hook up events
helper.Events.GameLoop.GameLaunched += this.OnGameLaunched;
helper.Events.GameLoop.DayStarted += this.OnDayStarted;
helper.Events.Display.RenderedHud += this.OnRenderedHud;
helper.Events.Display.MenuChanged += this.OnMenuChanged;
helper.Events.Input.ButtonsChanged += this.OnButtonsChanged;
}
/*********
** Private methods
*********/
/****
** Event handlers
****/
/// <inheritdoc cref="IGameLoopEvents.GameLaunched"/>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event data.</param>
private void OnGameLaunched(object? sender, GameLaunchedEventArgs e)
{
if (!this.IsDataValid)
return;
// get mod APIs
JsonAssetsIntegration jsonAssets = new JsonAssetsIntegration(this.Helper.ModRegistry, this.Monitor);
// initialize functionality
this.GameHelper = new GameHelper(this.Metadata, this.Monitor, this.Helper.ModRegistry, this.Helper.Reflection);
this.TargetFactory = new TargetFactory(this.Helper.Reflection, this.GameHelper, () => this.Config, jsonAssets, () => this.Config.EnableTileLookups);
this.DebugInterface = new PerScreen<DebugInterface>(() => new DebugInterface(this.GameHelper, this.TargetFactory, () => this.Config, this.Monitor));
// add Generic Mod Config Menu integration
new GenericModConfigMenuIntegrationForLookupAnything(
getConfig: () => this.Config,
reset: () => this.Config = new ModConfig(),
saveAndApply: () => this.Helper.WriteConfig(this.Config),
modRegistry: this.Helper.ModRegistry,
monitor: this.Monitor,
manifest: this.ModManifest
).Register();
}
/// <inheritdoc cref="IGameLoopEvents.DayStarted"/>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event data.</param>
private void OnDayStarted(object? sender, DayStartedEventArgs e)
{
if (!this.IsDataValid)
return;
// reset low-level cache once per game day (used for expensive queries that don't change within a day)
this.GameHelper.ResetCache(this.Helper.Reflection, this.Monitor);
}
/// <inheritdoc cref="IInputEvents.ButtonsChanged"/>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event data.</param>
private void OnButtonsChanged(object? sender, ButtonsChangedEventArgs e)
{
if (!this.IsDataValid)
return;
this.Monitor.InterceptErrors("handling your input", () =>
{
ModConfigKeys keys = this.Keys;
// pressed
if (keys.ToggleSearch.JustPressed())
{
this.Helper.Input.SuppressActiveKeybinds(keys.ToggleSearch);
this.TryToggleSearch();
}
else if (keys.ToggleLookup.JustPressed())
{
this.Helper.Input.SuppressActiveKeybinds(keys.ToggleLookup);
this.ToggleLookup();
}
else if (keys.ScrollUp.JustPressed())
(Game1.activeClickableMenu as IScrollableMenu)?.ScrollUp();
else if (keys.ScrollDown.JustPressed())
(Game1.activeClickableMenu as IScrollableMenu)?.ScrollDown();
else if (keys.PageUp.JustPressed())
(Game1.activeClickableMenu as IScrollableMenu)?.ScrollUp(Game1.activeClickableMenu.height);
else if (keys.PageDown.JustPressed())
(Game1.activeClickableMenu as IScrollableMenu)?.ScrollDown(Game1.activeClickableMenu.height);
else if (keys.ToggleDebug.JustPressed() && Context.IsPlayerFree)
this.DebugInterface.Value.Enabled = !this.DebugInterface.Value.Enabled;
// released
if (this.Config.HideOnKeyUp && keys.ToggleLookup.GetState() == SButtonState.Released)
this.HideLookup();
});
}
/// <inheritdoc cref="IDisplayEvents.MenuChanged"/>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event data.</param>
private void OnMenuChanged(object? sender, MenuChangedEventArgs e)
{
// restore the previous menu if it was hidden to show the lookup UI
this.Monitor.InterceptErrors("restoring the previous menu", () =>
{
if (e.NewMenu == null && (e.OldMenu is LookupMenu or SearchMenu) && this.PreviousMenus.Value.Any())
Game1.activeClickableMenu = this.PreviousMenus.Value.Pop();
});
}
/// <inheritdoc cref="IDisplayEvents.RenderedHud"/>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event data.</param>
private void OnRenderedHud(object? sender, RenderedHudEventArgs e)
{
if (!this.IsDataValid)
return;
// render debug interface
if (this.DebugInterface.Value.Enabled)
this.DebugInterface.Value.Draw(Game1.spriteBatch);
}
/****
** Lookup menu helpers
****/
/// <summary>Show the lookup UI for the current target.</summary>
private void ToggleLookup()
{
if (Game1.activeClickableMenu is LookupMenu)
this.HideLookup();
else
this.ShowLookup();
}
/// <summary>Show the lookup UI for the current target.</summary>
private void ShowLookup()
{
if (!this.IsDataValid)
return;
// disable lookups if metadata is invalid
if (!this.IsDataValid)
{
this.GameHelper.ShowErrorMessage($"The mod doesn't seem to be installed correctly: its {this.DatabaseFileName} file is missing or corrupt.");
return;
}
// show menu
StringBuilder logMessage = new("Received a lookup request...");
this.Monitor.InterceptErrors("looking that up", () =>
{
try
{
// get target
ISubject? subject = this.GetSubject(logMessage);
if (subject == null)
{
this.Monitor.Log($"{logMessage} no target found.");
return;
}
// show lookup UI
this.Monitor.Log(logMessage.ToString());
this.ShowLookupFor(subject);
}
catch
{
this.Monitor.Log($"{logMessage} an error occurred.");
throw;
}
});
}
/// <summary>Show a lookup menu for the given subject.</summary>
/// <param name="subject">The subject to look up.</param>
internal void ShowLookupFor(ISubject subject)
{
this.Monitor.InterceptErrors("looking that up", () =>
{
this.Monitor.Log($"Showing {subject.GetType().Name}::{subject.Type}::{subject.Name}.");
this.PushMenu(
new LookupMenu(
subject: subject,
monitor: this.Monitor,
reflectionHelper: this.Helper.Reflection,
scroll: this.Config.ScrollAmount,
showDebugFields: this.Config.ShowDataMiningFields,
forceFullScreen: this.Config.ForceFullScreen,
showNewPage: this.ShowLookupFor
)
);
});
}
/// <summary>Hide the lookup UI for the current target.</summary>
private void HideLookup()
{
this.Monitor.InterceptErrors("closing the menu", () =>
{
if (Game1.activeClickableMenu is LookupMenu menu)
menu.QueueExit();
});
}
/****
** Search menu helpers
****/
/// <summary>Toggle the search UI if applicable.</summary>
private void TryToggleSearch()
{
if (Game1.activeClickableMenu is SearchMenu)
this.HideSearch();
else if (Context.IsWorldReady && Game1.activeClickableMenu is not LookupMenu)
this.ShowSearch();
}
/// <summary>Show the search UI.</summary>
private void ShowSearch()
{
if (!this.IsDataValid)
return;
this.PushMenu(
new SearchMenu(this.TargetFactory.GetSearchSubjects(), this.ShowLookupFor, this.Monitor, scroll: this.Config.ScrollAmount)
);
}
/// <summary>Hide the search UI.</summary>
private void HideSearch()
{
if (Game1.activeClickableMenu is SearchMenu)
{
Game1.playSound("bigDeSelect"); // match default behaviour when closing a menu
Game1.activeClickableMenu = null;
}
}
/****
** Generic helpers
****/
/// <summary>Read the config file, migrating legacy settings if applicable.</summary>
private ModConfig LoadConfig()
{
// migrate legacy settings
try
{
if (File.Exists(Path.Combine(this.Helper.DirectoryPath, "config.json")))
{
JObject model = this.Helper.ReadConfig<JObject>();
// merge ToggleLookupInFrontOfPlayer bindings into ToggleLookup
JObject? controls = model.Value<JObject?>("Controls");
string? toggleLookup = controls?.Value<string>("ToggleLookup");
string? toggleLookupInFrontOfPlayer = controls?.Value<string>("ToggleLookupInFrontOfPlayer");
if (!string.IsNullOrWhiteSpace(toggleLookupInFrontOfPlayer))
{
controls!.Remove("ToggleLookupInFrontOfPlayer");
controls["ToggleLookup"] = string.Join(", ", (toggleLookup ?? "").Split(',').Concat(toggleLookupInFrontOfPlayer.Split(',')).Select(p => p.Trim()).Where(p => p != "").Distinct());
this.Helper.WriteConfig(model);
}
}
}
catch (Exception ex)
{
this.Monitor.Log("Couldn't migrate legacy settings in config.json; they'll be removed instead.", LogLevel.Warn);
this.Monitor.Log(ex.ToString());
}
// load config
return this.Helper.ReadConfig<ModConfig>();
}
/// <summary>Get the most relevant subject under the player's cursor.</summary>
/// <param name="logMessage">The log message to which to append search details.</param>
private ISubject? GetSubject(StringBuilder logMessage)
{
if (!this.IsDataValid)
return null;
// get context
Vector2 cursorPos = this.GameHelper.GetScreenCoordinatesFromCursor();
if (!Game1.uiMode)
cursorPos = Utility.ModifyCoordinatesForUIScale(cursorPos); // menus use UI coordinates
bool hasCursor = Constants.TargetPlatform != GamePlatform.Android && Game1.wasMouseVisibleThisFrame; // note: only reliable when a menu isn't open
// open menu
if (Game1.activeClickableMenu != null)
{
logMessage.Append($" searching the open '{Game1.activeClickableMenu.GetType().Name}' menu...");
return this.TargetFactory.GetSubjectFrom(Game1.activeClickableMenu, cursorPos);
}
// HUD under cursor
if (hasCursor)
{
foreach (IClickableMenu menu in Game1.onScreenMenus)
{
if (menu.isWithinBounds((int)cursorPos.X, (int)cursorPos.Y))
{
logMessage.Append($" searching the on-screen '{menu.GetType().Name}' menu...");
return this.TargetFactory.GetSubjectFrom(menu, cursorPos);
}
}
}
// world
logMessage.Append(" searching the world...");
return this.TargetFactory.GetSubjectFrom(Game1.player, Game1.currentLocation, hasCursor);
}
/// <summary>Push a new menu onto the display stack, saving the previous menu if needed.</summary>
/// <param name="menu">The menu to show.</param>
private void PushMenu(IClickableMenu menu)
{
if (this.ShouldRestoreMenu(Game1.activeClickableMenu))
{
this.PreviousMenus.Value.Push(Game1.activeClickableMenu);
this.Helper.Reflection.GetField<IClickableMenu>(typeof(Game1), "_activeClickableMenu").SetValue(menu); // bypass Game1.activeClickableMenu, which disposes the previous menu
}
else
Game1.activeClickableMenu = menu;
}
/// <summary>Load the file containing metadata that's not available from the game directly.</summary>
private Metadata? LoadMetadata()
{
Metadata? metadata = null;
this.Monitor.InterceptErrors("loading metadata", () =>
{
metadata = this.Helper.Data.ReadJsonFile<Metadata>(this.DatabaseFileName);
});
return metadata;
}
/// <summary>Get whether a given menu should be restored when the lookup ends.</summary>
/// <param name="menu">The menu to check.</param>
private bool ShouldRestoreMenu(IClickableMenu? menu)
{
// no menu
if (menu == null)
return false;
// if 'hide on key up' is enabled, all lookups should close on key up
if (this.Config.HideOnKeyUp && menu is LookupMenu)
return false;
return true;
}
}
}