-
-
Notifications
You must be signed in to change notification settings - Fork 428
Expand file tree
/
Copy pathModEntry.cs
More file actions
242 lines (212 loc) · 9.65 KB
/
ModEntry.cs
File metadata and controls
242 lines (212 loc) · 9.65 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
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Pathoschild.Stardew.Common;
using Pathoschild.Stardew.Common.Integrations.GenericModConfigMenu;
using Pathoschild.Stardew.FastAnimations.Framework;
using Pathoschild.Stardew.FastAnimations.Handlers;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewModdingAPI.Utilities;
using StardewValley;
namespace Pathoschild.Stardew.FastAnimations;
/// <summary>The mod entry point.</summary>
internal class ModEntry : Mod
{
/*********
** Fields
*********/
/// <summary>An arbitrary number which identifies the pause/unpause messages from Fast Animations.</summary>
private const int MessageId = 918718254;
/// <summary>The mod configuration.</summary>
private ModConfig Config = null!; // set in Entry
/// <summary>The animation handlers which skip or accelerate specific animations.</summary>
private IAnimationHandler[] Handlers = null!; // set in Entry
/// <summary>The <see cref="Handlers"/> filtered to those which need to be updated when the object list changes.</summary>
private IAnimationHandlerWithObjectList[] HandlersWithObjectList = null!; // set in Entry
/// <summary>Whether to pause mod features.</summary>
private readonly PerScreen<bool> ModPaused = new();
/*********
** Public methods
*********/
/// <inheritdoc />
public override void Entry(IModHelper helper)
{
I18n.Init(helper.Translation);
CommonHelper.RemoveObsoleteFiles(this, "FastAnimations.pdb"); // removed in 1.11.6
this.Config = helper.ReadConfig<ModConfig>();
this.UpdateConfig();
helper.Events.GameLoop.GameLaunched += this.OnGameLaunched;
helper.Events.GameLoop.SaveLoaded += this.OnSaveLoaded;
helper.Events.GameLoop.UpdateTicked += this.OnUpdateTicked;
helper.Events.Input.ButtonsChanged += this.OnButtonsChanged;
helper.Events.Player.Warped += this.OnWarped;
helper.Events.World.ObjectListChanged += this.OnObjectListChanged;
}
/*********
** Private methods
*********/
/****
** Events
****/
/// <inheritdoc cref="IGameLoopEvents.GameLaunched" />
private void OnGameLaunched(object? sender, GameLaunchedEventArgs e)
{
this.AddGenericModConfigMenu(
new GenericModConfigMenuIntegrationForFastAnimations(
isModPaused: () => this.ModPaused.Value,
setModPaused: this.SetModPausedWithNotification
),
get: () => this.Config,
set: config => this.Config = config,
onSaved: this.UpdateConfig
);
}
/// <inheritdoc cref="IGameLoopEvents.SaveLoaded" />
private void OnSaveLoaded(object? sender, SaveLoadedEventArgs e)
{
// initialize handlers
foreach (IAnimationHandler handler in this.Handlers)
handler.OnNewLocation(Game1.currentLocation);
}
/// <inheritdoc cref="IPlayerEvents.Warped" />
private void OnWarped(object? sender, WarpedEventArgs e)
{
if (!Context.IsWorldReady || Game1.eventUp || !this.Handlers.Any() || !e.IsLocalPlayer)
return;
foreach (IAnimationHandler handler in this.Handlers)
handler.OnNewLocation(e.NewLocation);
}
/// <inheritdoc cref="IWorldEvents.ObjectListChanged" />
private void OnObjectListChanged(object? sender, ObjectListChangedEventArgs e)
{
if (e.IsCurrentLocation)
{
foreach (IAnimationHandlerWithObjectList handler in this.HandlersWithObjectList)
handler.OnObjectListChanged(e);
}
}
/// <inheritdoc cref="IGameLoopEvents.UpdateTicked" />
private void OnUpdateTicked(object? sender, UpdateTickedEventArgs e)
{
if (!this.Handlers.Any() || this.ModPaused.Value)
return;
int playerAnimationId = Game1.player.FarmerSprite.currentSingleAnimation;
foreach (IAnimationHandler handler in this.Handlers)
{
if (handler.TryApply(playerAnimationId))
break;
}
}
/// <inheritdoc cref="IInputEvents.ButtonsChanged" />
private void OnButtonsChanged(object? sender, ButtonsChangedEventArgs e)
{
if (!Context.IsWorldReady)
return;
if (this.Config.PauseModKey.JustPressed())
this.SetModPausedWithNotification(!this.ModPaused.Value);
}
/****
** Methods
****/
/// <summary>Apply the mod configuration if it changed.</summary>
[MemberNotNull(nameof(ModEntry.Handlers))]
private void UpdateConfig()
{
this.Handlers = this.GetHandlers(this.Config).ToArray();
this.HandlersWithObjectList = this.Handlers.OfType<IAnimationHandlerWithObjectList>().ToArray();
GameLocation location = Game1.currentLocation;
if (location != null)
{
foreach (IAnimationHandler handler in this.Handlers)
handler.OnNewLocation(location);
}
}
/// <summary>Set whether the mod features are paused (so animations play at default speeds), and show a HUD notification if it changes.</summary>
/// <param name="paused">Whether the mod features should be paused.</param>
private void SetModPausedWithNotification(bool paused)
{
if (this.ModPaused.Value == paused)
return;
// set value
this.ModPaused.Value = paused;
// show UI message
string keybind = this.Config.PauseModKey.ToString();
string message = paused
? I18n.ModPaused(keybind: keybind)
: I18n.ModUnpaused(keybind: keybind);
Game1.hudMessages.RemoveAll(p => p.number == ModEntry.MessageId);
CommonHelper.ShowInfoMessage(message, duration: 1000, number: ModEntry.MessageId);
}
/// <summary>Get the enabled animation handlers.</summary>
private IEnumerable<IAnimationHandler> GetHandlers(ModConfig config)
{
// player animations
if (config.EatAndDrinkSpeed > 1 || config.DisableEatAndDrinkConfirmation)
yield return new EatingHandler(config.EatAndDrinkSpeed, config.DisableEatAndDrinkConfirmation);
if (config.FishingSpeed > 1)
yield return new FishingHandler(config.FishingSpeed);
if (config.HarvestSpeed > 1)
yield return new HarvestHandler(config.HarvestSpeed);
if (config.HoldUpItemSpeed > 1)
yield return new HoldUpItemHandler(config.HoldUpItemSpeed);
if (config.HorseFluteSpeed > 1)
yield return new HorseFluteHandler(config.HorseFluteSpeed);
if (config.MilkSpeed > 1)
yield return new MilkingHandler(config.MilkSpeed);
if (config.MountOrDismountSpeed > 1)
yield return new MountHorseHandler(config.MountOrDismountSpeed);
if (config.ReadBookSpeed > 1)
yield return new ReadBookHandler(config.ReadBookSpeed);
if (config.ShearSpeed > 1)
yield return new ShearingHandler(config.ShearSpeed);
if (config.ToolSwingSpeed > 1)
yield return new ToolSwingHandler(config.ToolSwingSpeed);
if (config.UseSlingshotSpeed > 1)
yield return new SlingshotHandler(config.UseSlingshotSpeed);
if (config.UseTotemSpeed > 1)
yield return new UseTotemHandler(config.UseTotemSpeed);
if (config.WeaponSwingSpeed > 1)
yield return new WeaponSwingHandler(config.WeaponSwingSpeed);
// world animations
if (config.BreakGeodeSpeed > 1)
yield return new BreakingGeodeHandler(config.BreakGeodeSpeed);
if (config.CasinoSlotsSpeed > 1)
yield return new CasinoSlotsHandler(config.CasinoSlotsSpeed);
if (config.Experimental_EventSpeed > 1)
yield return new EventHandler(config.Experimental_EventSpeed);
if (config.FadeSpeed > 1)
yield return new FadeHandler(config.FadeSpeed);
if (config.FishingTextSpeed > 1)
yield return new FishingTextHandler(config.FishingTextSpeed, this.Helper.Reflection);
if (config.FishingTreasureSpeed > 1)
yield return new FishingTreasureHandler(config.FishingTreasureSpeed);
if (config.ForgeSpeed > 1)
yield return new ForgeHandler(config.ForgeSpeed);
if (config.OpenChestSpeed > 1)
yield return new OpenChestHandler(config.OpenChestSpeed);
if (config.OpenDialogueBoxSpeed > 1)
yield return new OpenDialogueBoxHandler(config.OpenDialogueBoxSpeed);
if (config.PamBusSpeed > 1)
yield return new PamBusHandler(config.PamBusSpeed);
if (config.ParrotExpressSpeed > 1)
yield return new ParrotExpressHandler(config.ParrotExpressSpeed);
if (config.PrizeTicketMachineSpeed > 1)
yield return new PrizeTicketMachineHandler(config.PrizeTicketMachineSpeed, this.Helper.Reflection);
if (config.TailorSpeed > 1)
yield return new TailoringHandler(config.TailorSpeed);
if (config.TreeFallSpeed > 1)
yield return new TreeFallingHandler(config.TreeFallSpeed);
if (config.WheelSpinSpeed > 1)
yield return new WheelSpinHandler(config.WheelSpinSpeed);
// UI animations
if (config.DialogueTypeSpeed > 1)
yield return new DialogueTypingHandler(config.DialogueTypeSpeed);
if (config.ShippingMenuTransitionSpeed > 1)
yield return new ShippingMenuHandler(config.ShippingMenuTransitionSpeed, this.Helper.Reflection);
if (config.TitleMenuTransitionSpeed > 1)
yield return new TitleMenuHandler(config.TitleMenuTransitionSpeed);
if (config.LoadGameBlinkSpeed > 1)
yield return new LoadGameMenuHandler(config.LoadGameBlinkSpeed);
}
}