forked from Pathoschild/StardewMods
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModEntry.cs
More file actions
309 lines (274 loc) · 12.9 KB
/
Copy pathModEntry.cs
File metadata and controls
309 lines (274 loc) · 12.9 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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Pathoschild.Stardew.Common;
using Pathoschild.Stardew.Common.Integrations.CustomFarmingRedux;
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
{
/*********
** Properties
*********/
/****
** Configuration
****/
/// <summary>The mod configuration.</summary>
private ModConfig Config;
/// <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>Finds and analyses 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.Helper.ReadConfig<ModConfig>();
// 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
GameEvents.FirstUpdateTick += this.GameEvents_FirstUpdateTick;
}
/*********
** Private methods
*********/
/****
** Event handlers
****/
/// <summary>The method invoked on the first update tick, once all mods are initialised.</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event data.</param>
private void GameEvents_FirstUpdateTick(object sender, EventArgs e)
{
if (!this.IsDataValid)
return;
// initialise functionality
var customFarming = new CustomFarmingReduxIntegration(this.Helper.ModRegistry, this.Monitor);
this.TargetFactory = new TargetFactory(this.Metadata, this.Helper.Translation, this.Helper.Reflection, customFarming);
this.DebugInterface = new DebugInterface(this.TargetFactory, this.Config, this.Monitor);
// hook up events
TimeEvents.AfterDayStarted += this.TimeEvents_AfterDayStarted;
GraphicsEvents.OnPostRenderHudEvent += this.GraphicsEvents_OnPostRenderHudEvent;
MenuEvents.MenuClosed += this.MenuEvents_MenuClosed;
InputEvents.ButtonPressed += this.InputEvents_ButtonPressed;
if (this.Config.HideOnKeyUp)
InputEvents.ButtonReleased += this.InputEvents_ButtonReleased;
}
/// <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 TimeEvents_AfterDayStarted(object sender, EventArgs e)
{
// reset low-level cache once per game day (used for expensive queries that don't change within a day)
GameHelper.ResetCache(this.Metadata, this.Helper.Reflection, this.Helper.Translation, 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 InputEvents_ButtonPressed(object sender, EventArgsInput e)
{
// perform bound action
this.Monitor.InterceptErrors("handling your input", $"handling input '{e.Button}'", () =>
{
var controls = this.Config.Controls;
if (controls.ToggleLookup.Contains(e.Button))
this.ToggleLookup(LookupMode.Cursor);
else if (controls.ToggleLookupInFrontOfPlayer.Contains(e.Button))
this.ToggleLookup(LookupMode.FacingPlayer);
else if (controls.ScrollUp.Contains(e.Button))
(Game1.activeClickableMenu as LookupMenu)?.ScrollUp();
else if (controls.ScrollDown.Contains(e.Button))
(Game1.activeClickableMenu as LookupMenu)?.ScrollDown();
else if (controls.ToggleDebug.Contains(e.Button) && 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 InputEvents_ButtonReleased(object sender, EventArgsInput e)
{
// perform bound action
this.Monitor.InterceptErrors("handling your input", $"handling input release '{e.Button}'", () =>
{
var controls = this.Config.Controls;
if (controls.ToggleLookup.Contains(e.Button) || controls.ToggleLookupInFrontOfPlayer.Contains(e.Button))
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 MenuEvents_MenuClosed(object sender, EventArgsClickableMenuClosed e)
{
// restore the previous menu if it was hidden to show the lookup UI
this.Monitor.InterceptErrors("restoring the previous menu", () =>
{
if (e.PriorMenu 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 GraphicsEvents_OnPostRenderHudEvent(object sender, EventArgs e)
{
// render debug interface
if (this.DebugInterface.Enabled)
this.DebugInterface.Draw(Game1.spriteBatch);
}
/****
** Helpers
****/
/// <summary>Show the lookup UI for the current target.</summary>
/// <param name="lookupMode">The lookup target mode.</param>
private void ToggleLookup(LookupMode lookupMode)
{
if (Game1.activeClickableMenu is LookupMenu)
this.HideLookup();
else
this.ShowLookup(lookupMode);
}
/// <summary>Show the lookup UI for the current target.</summary>
/// <param name="lookupMode">The lookup target mode.</param>
private void ShowLookup(LookupMode lookupMode)
{
// disable lookups if metadata is invalid
if (!this.IsDataValid)
{
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, lookupMode);
if (subject == null)
{
this.Monitor.Log($"{logMessage} no target found.", LogLevel.Trace);
return;
}
// show lookup UI
this.Monitor.Log(logMessage.ToString(), LogLevel.Trace);
this.ShowLookupFor(subject);
}
catch
{
this.Monitor.Log($"{logMessage} an error occurred.", LogLevel.Trace);
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}.", LogLevel.Trace);
if (Game1.activeClickableMenu != null)
{
if (!this.Config.HideOnKeyUp || !(Game1.activeClickableMenu is LookupMenu))
this.PreviousMenus.Push(Game1.activeClickableMenu);
}
Game1.activeClickableMenu = new LookupMenu(subject, this.Metadata, this.Monitor, this.Helper.Reflection, this.Config.ScrollAmount, this.Config.ShowDataMiningFields, this.ShowLookupFor);
});
}
/// <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>
/// <param name="lookupMode">The lookup target mode.</param>
private ISubject GetSubject(StringBuilder logMessage, LookupMode lookupMode)
{
// menu under cursor
if (lookupMode == LookupMode.Cursor)
{
Vector2 cursorPos = GameHelper.GetScreenCoordinatesFromCursor();
// try menu
if (Game1.activeClickableMenu != null)
{
logMessage.Append($" searching the open '{Game1.activeClickableMenu.GetType().Name}' menu...");
return this.TargetFactory.GetSubjectFrom(Game1.activeClickableMenu, cursorPos);
}
// try HUD under cursor
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);
}
}
}
// try world
if (Game1.activeClickableMenu == null)
{
logMessage.Append(" searching the world...");
return this.TargetFactory.GetSubjectFrom(Game1.player, Game1.currentLocation, lookupMode, this.Config.EnableTileLookups);
}
// not found
return null;
}
/// <summary>Show the lookup UI for the current target.</summary>
private void HideLookup()
{
this.Monitor.InterceptErrors("closing the menu", () =>
{
if (Game1.activeClickableMenu is LookupMenu)
{
Game1.playSound("bigDeSelect"); // match default behaviour when closing a menu
Game1.activeClickableMenu = null;
}
});
}
/// <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.ReadJsonFile<Metadata>(this.DatabaseFileName);
});
}
}
}