forked from Pathoschild/StardewMods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModEntry.cs
408 lines (360 loc) · 16.6 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
using System;
using System.Collections.Generic;
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.CustomFarmingRedux;
using Pathoschild.Stardew.Common.Integrations.JsonAssets;
using Pathoschild.Stardew.Common.Integrations.ProducerFrameworkMod;
using Pathoschild.Stardew.LookupAnything.Components;
using Pathoschild.Stardew.LookupAnything.Framework;
using Pathoschild.Stardew.LookupAnything.Framework.Constants;
using Pathoschild.Stardew.LookupAnything.Framework.Subjects;
using StardewModdingAPI;
using StardewModdingAPI.Events;
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;
/// <summary>The configured key bindings.</summary>
private ModConfigKeys Keys;
/// <summary>Provides metadata that's not available from the game data directly.</summary>
private Metadata Metadata;
/// <summary>The name of the file containing data for the <see cref="Metadata"/> field.</summary>
private readonly string DatabaseFileName = "data.json";
/****
** Validation
****/
/// <summary>Whether the metadata validation passed.</summary>
private bool IsDataValid;
/****
** State
****/
/// <summary>The previous menus shown before the current lookup UI was opened.</summary>
private readonly Stack<IClickableMenu> PreviousMenus = new Stack<IClickableMenu>();
/// <summary>Provides utility methods for interacting with the game code.</summary>
private GameHelper GameHelper;
/// <summary>Provides subject entries for target values.</summary>
private SubjectFactory SubjectFactory;
/// <summary>Finds and analyzes lookup targets in the world.</summary>
private TargetFactory TargetFactory;
/// <summary>Draws debug information to the screen.</summary>
private DebugInterface DebugInterface;
/*********
** 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();
this.Keys = this.Config.Controls.ParseControls(helper.Input, this.Monitor);
// load translations
L10n.Init(helper.Translation);
// load & validate database
this.LoadMetadata();
this.IsDataValid = this.Metadata.LooksValid();
if (!this.IsDataValid)
{
this.Monitor.Log("The data.json 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.ButtonPressed += this.OnButtonPressed;
if (this.Config.HideOnKeyUp)
helper.Events.Input.ButtonReleased += this.OnButtonReleased;
}
/*********
** Private methods
*********/
/****
** Event handlers
****/
/// <summary>The method invoked on the first update tick, once all mods are initialized.</summary>
/// <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
var customFarming = new CustomFarmingReduxIntegration(this.Helper.ModRegistry, this.Monitor);
var producerFramework = new ProducerFrameworkModIntegration(this.Helper.ModRegistry, this.Monitor);
this.GameHelper = new GameHelper(customFarming, producerFramework, this.Metadata);
this.SubjectFactory = new SubjectFactory(this.Metadata, this.Helper.Translation, this.Helper.Reflection, this.GameHelper, this.Config);
this.TargetFactory = new TargetFactory(this.Helper.Reflection, this.GameHelper, jsonAssets, this.SubjectFactory);
this.DebugInterface = new DebugInterface(this.GameHelper, this.TargetFactory, this.Config, this.Monitor);
}
/// <summary>The method invoked when a new day starts.</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event data.</param>
private void OnDayStarted(object sender, DayStartedEventArgs e)
{
// 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);
}
/// <summary>The method invoked when the player presses a button.</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event data.</param>
private void OnButtonPressed(object sender, ButtonPressedEventArgs e)
{
this.Monitor.InterceptErrors("handling your input", $"handling input '{e.Button}'", () =>
{
ModConfigKeys keys = this.Keys;
if (keys.ToggleSearch.JustPressedUnique())
this.ToggleSearch();
if (keys.ToggleLookup.JustPressedUnique())
this.ToggleLookup();
else if (keys.ScrollUp.JustPressedUnique())
(Game1.activeClickableMenu as LookupMenu)?.ScrollUp();
else if (keys.ScrollDown.JustPressedUnique())
(Game1.activeClickableMenu as LookupMenu)?.ScrollDown();
else if (keys.ToggleDebug.JustPressedUnique() && Context.IsPlayerFree)
this.DebugInterface.Enabled = !this.DebugInterface.Enabled;
});
}
/// <summary>The method invoked when the player releases a button.</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event data.</param>
private void OnButtonReleased(object sender, ButtonReleasedEventArgs e)
{
// perform bound action
this.Monitor.InterceptErrors("handling your input", $"handling input release '{e.Button}'", () =>
{
ModConfigKeys keys = this.Keys;
if (keys.ToggleLookup.JustPressedUnique())
this.HideLookup();
});
}
/// <summary>The method invoked when the player closes a displayed menu.</summary>
/// <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 && this.PreviousMenus.Any())
Game1.activeClickableMenu = this.PreviousMenus.Pop();
});
}
/// <summary>The method invoked when the interface is rendering.</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event data.</param>
private void OnRenderedHud(object sender, RenderedHudEventArgs e)
{
// render debug interface
if (this.DebugInterface.Enabled)
this.DebugInterface.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()
{
// disable lookups if metadata is invalid
if (!this.IsDataValid)
{
this.GameHelper.ShowErrorMessage("The mod doesn't seem to be installed correctly: its data.json file is missing or corrupt.");
return;
}
// show menu
StringBuilder logMessage = new StringBuilder("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(this.GameHelper, subject, this.Monitor, this.Helper.Reflection, this.Config.ScrollAmount, this.Config.ShowDataMiningFields, 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.</summary>
private void ToggleSearch()
{
if (Game1.activeClickableMenu is SearchMenu)
this.HideSearch();
else
this.ShowSearch();
}
/// <summary>Show the search UI.</summary>
private void ShowSearch()
{
this.PushMenu(
new SearchMenu(this.SubjectFactory, this.ShowLookupFor)
);
}
/// <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)
{
// get context
Vector2 cursorPos = this.GameHelper.GetScreenCoordinatesFromCursor();
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, this.Config.EnableTileLookups, 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.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 void LoadMetadata()
{
this.Monitor.InterceptErrors("loading metadata", () =>
{
this.Metadata = this.Helper.Data.ReadJsonFile<Metadata>(this.DatabaseFileName);
});
}
/// <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;
}
}
}