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
219 lines (193 loc) · 10.1 KB
/
ModEntry.cs
File metadata and controls
219 lines (193 loc) · 10.1 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
using System.Collections.Generic;
using System.Linq;
using Pathoschild.Stardew.Common;
using Pathoschild.Stardew.Common.Input;
using Pathoschild.Stardew.DataLayers.Framework;
using Pathoschild.Stardew.DataLayers.Layers;
using Pathoschild.Stardew.DataLayers.Layers.Coverage;
using Pathoschild.Stardew.DataLayers.Layers.Crops;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewModdingAPI.Utilities;
using StardewValley;
using StardewValley.Menus;
namespace Pathoschild.Stardew.DataLayers
{
/// <summary>The mod entry point.</summary>
internal class ModEntry : Mod
{
/*********
** Fields
*********/
/// <summary>The mod configuration.</summary>
private ModConfig Config;
/// <summary>The configured key bindings.</summary>
private ModConfigKeys Keys;
/// <summary>The available data layers.</summary>
private ILayer[] Layers;
/// <summary>Maps key bindings to the layers they should activate.</summary>
private readonly IDictionary<KeyBinding, ILayer> ShortcutMap = new Dictionary<KeyBinding, ILayer>();
/// <summary>Handles access to the supported mod integrations.</summary>
private ModIntegrations Mods;
/// <summary>The current overlay being displayed, if any.</summary>
private readonly PerScreen<DataLayerOverlay> CurrentOverlay = new PerScreen<DataLayerOverlay>();
/*********
** 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)
{
// read config
this.Config = helper.ReadConfig<ModConfig>();
this.Keys = this.Config.Controls.ParseControls(helper.Input, this.Monitor);
// init
I18n.Init(helper.Translation);
// hook up events
helper.Events.GameLoop.GameLaunched += this.OnGameLaunched;
helper.Events.GameLoop.SaveLoaded += this.OnSaveLoaded;
helper.Events.GameLoop.ReturnedToTitle += this.OnReturnedToTitle;
helper.Events.GameLoop.UpdateTicked += this.OnUpdateTicked;
helper.Events.Input.ButtonPressed += this.OnButtonPressed;
// hook up commands
var commandHandler = new CommandHandler(this.Monitor, () => this.CurrentOverlay.Value?.CurrentLayer);
helper.ConsoleCommands.Add(commandHandler.CommandName, $"Starts a Data Layers command. Type '{commandHandler.CommandName} help' for details.", (name, args) => commandHandler.Handle(args));
}
/*********
** Private methods
*********/
/// <summary>The method invoked on the first game update tick.</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event arguments.</param>
private void OnGameLaunched(object sender, GameLaunchedEventArgs e)
{
// init mod integrations
this.Mods = new ModIntegrations(this.Monitor, this.Helper.ModRegistry, this.Helper.Reflection);
}
/// <summary>The method invoked when the save is loaded.</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event arguments.</param>
private void OnSaveLoaded(object sender, SaveLoadedEventArgs e)
{
// init layers
// need to do this after the save is loaded so translations use the selected language
this.Layers = this.GetLayers(this.Config, this.Helper.Input, this.Mods).ToArray();
foreach (ILayer layer in this.Layers)
{
if (layer.ShortcutKey.ToString() != SButton.None.ToString())
this.ShortcutMap[layer.ShortcutKey] = layer;
}
}
/// <summary>Get the enabled data layers.</summary>
/// <param name="config">The mod configuration.</param>
/// <param name="input">The API for checking input state.</param>
/// <param name="mods">Handles access to the supported mod integrations.</param>
private IEnumerable<ILayer> GetLayers(ModConfig config, IInputHelper input, ModIntegrations mods)
{
ModConfig.LayerConfigs layers = config.Layers;
if (layers.Accessible.IsEnabled())
yield return new AccessibleLayer(layers.Accessible, input, this.Monitor);
if (layers.Buildable.IsEnabled())
yield return new BuildableLayer(layers.Buildable, input, this.Monitor);
if (layers.CoverageForBeeHouses.IsEnabled())
yield return new BeeHouseLayer(layers.CoverageForBeeHouses, input, this.Monitor);
if (layers.CoverageForScarecrows.IsEnabled())
yield return new ScarecrowLayer(layers.CoverageForScarecrows, mods, input, this.Monitor);
if (layers.CoverageForSprinklers.IsEnabled())
yield return new SprinklerLayer(layers.CoverageForSprinklers, mods, input, this.Monitor);
if (layers.CoverageForJunimoHuts.IsEnabled())
yield return new JunimoHutLayer(layers.CoverageForJunimoHuts, mods, input, this.Monitor);
if (layers.CropWater.IsEnabled())
yield return new CropWaterLayer(layers.CropWater, input, this.Monitor);
if (layers.CropPaddyWater.IsEnabled())
yield return new CropPaddyWaterLayer(layers.CropPaddyWater, input, this.Monitor);
if (layers.CropFertilizer.IsEnabled())
yield return new CropFertilizerLayer(layers.CropFertilizer, input, this.Monitor);
if (layers.CropHarvest.IsEnabled())
yield return new CropHarvestLayer(layers.CropHarvest, input, this.Monitor);
if (layers.Machines.IsEnabled() && mods.Automate.IsLoaded)
yield return new MachineLayer(layers.Machines, mods, input, this.Monitor);
if (layers.Tillable.IsEnabled())
yield return new TillableLayer(layers.Tillable, input, this.Monitor);
// add separate grid layer if grid isn't enabled for all layers
if (!config.ShowGrid && layers.TileGrid.IsEnabled())
yield return new GridLayer(layers.TileGrid, input, this.Monitor);
}
/// <summary>The method invoked when the player returns to the title screen.</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event arguments.</param>
private void OnReturnedToTitle(object sender, ReturnedToTitleEventArgs e)
{
this.CurrentOverlay.Value?.Dispose();
this.CurrentOverlay.Value = null;
this.Layers = null;
}
/// <summary>The method invoked when the player presses an input button.</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event arguments.</param>
private void OnButtonPressed(object sender, ButtonPressedEventArgs e)
{
if (this.Layers == null)
return;
// perform bound action
this.Monitor.InterceptErrors("handling your input", $"handling input '{e.Button}'", () =>
{
// check context
if (!this.CanOverlayNow())
return;
bool overlayVisible = this.CurrentOverlay.Value != null;
ModConfigKeys keys = this.Keys;
// toggle overlay
if (keys.ToggleLayer.JustPressedUnique())
{
if (overlayVisible)
{
this.CurrentOverlay.Value.Dispose();
this.CurrentOverlay.Value = null;
}
else
this.CurrentOverlay.Value = new DataLayerOverlay(this.Helper.Events, this.Helper.Input, this.Helper.Reflection, this.Layers, this.CanOverlayNow, this.Config.CombineOverlappingBorders, this.Config.ShowGrid);
this.Helper.Input.Suppress(e.Button);
}
// cycle layers
else if (overlayVisible && keys.NextLayer.JustPressedUnique())
{
this.CurrentOverlay.Value.NextLayer();
this.Helper.Input.Suppress(e.Button);
}
else if (overlayVisible && keys.PrevLayer.JustPressedUnique())
{
this.CurrentOverlay.Value.PrevLayer();
this.Helper.Input.Suppress(e.Button);
}
// shortcut to layer
else if (overlayVisible)
{
ILayer layer = this.ShortcutMap.Where(p => p.Key.JustPressedUnique()).Select(p => p.Value).FirstOrDefault();
if (layer != null && layer != this.CurrentOverlay.Value.CurrentLayer)
{
this.CurrentOverlay.Value.SetLayer(layer);
this.Helper.Input.Suppress(e.Button);
}
}
});
}
/// <summary>Receive an update tick.</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event arguments.</param>
private void OnUpdateTicked(object sender, UpdateTickedEventArgs e)
{
this.CurrentOverlay.Value?.Update();
}
/// <summary>Whether overlays are allowed in the current game context.</summary>
private bool CanOverlayNow()
{
if (!Context.IsWorldReady)
return false;
return
Context.IsPlayerFree // player is free to roam
|| (Game1.activeClickableMenu is CarpenterMenu && this.Helper.Reflection.GetField<bool>(Game1.activeClickableMenu, "onFarm").GetValue()) // on Robin's or Wizard's build screen
|| (this.Mods.PelicanFiber.IsLoaded && this.Mods.PelicanFiber.IsBuildMenuOpen() && this.Helper.Reflection.GetField<bool>(Game1.activeClickableMenu, "onFarm").GetValue()); // on Pelican Fiber's build screen
}
}
}