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
168 lines (151 loc) · 6.8 KB
/
ModEntry.cs
File metadata and controls
168 lines (151 loc) · 6.8 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
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Pathoschild.Stardew.FastAnimations.Framework;
using Pathoschild.Stardew.FastAnimations.Handlers;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewValley;
namespace Pathoschild.Stardew.FastAnimations
{
/// <summary>The mod entry point.</summary>
internal class ModEntry : Mod
{
/*********
** Fields
*********/
/// <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
/*********
** 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)
{
I18n.Init(helper.Translation);
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.Player.Warped += this.OnWarped;
}
/*********
** Private methods
*********/
/****
** Events
****/
/// <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)
{
// add Generic Mod Config Menu integration
new GenericModConfigMenuIntegrationForFastAnimations(
getConfig: () => this.Config,
reset: () =>
{
this.Config = new ModConfig();
this.Helper.WriteConfig(this.Config);
this.UpdateConfig();
},
saveAndApply: () =>
{
this.Helper.WriteConfig(this.Config);
this.UpdateConfig();
},
modRegistry: this.Helper.ModRegistry,
monitor: this.Monitor,
manifest: this.ModManifest
).Register();
}
/// <inheritdoc cref="IGameLoopEvents.SaveLoaded"/>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event data.</param>
private void OnSaveLoaded(object? sender, SaveLoadedEventArgs e)
{
// initialize handlers
foreach (IAnimationHandler handler in this.Handlers)
handler.OnNewLocation(Game1.currentLocation);
}
/// <inheritdoc cref="IPlayerEvents.Warped"/>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event data.</param>
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="IGameLoopEvents.UpdateTicked"/>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event data.</param>
private void OnUpdateTicked(object? sender, UpdateTickedEventArgs e)
{
if (Game1.eventUp || !this.Handlers.Any())
return;
int playerAnimationID = this.Helper.Reflection.GetField<int>(Game1.player.FarmerSprite, "currentSingleAnimation").GetValue();
foreach (IAnimationHandler handler in this.Handlers)
{
if (handler.IsEnabled(playerAnimationID))
{
handler.Update(playerAnimationID);
break;
}
}
}
/****
** Methods
****/
/// <summary>Apply the mod configuration if it changed.</summary>
[MemberNotNull(nameof(ModEntry.Handlers))]
private void UpdateConfig()
{
this.Handlers = this.GetHandlers(this.Config).ToArray();
}
/// <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.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.ShearSpeed > 1)
yield return new ShearingHandler(config.ShearSpeed);
if (config.UseSlingshotSpeed > 1)
yield return new SlingshotHandler(config.UseSlingshotSpeed);
if (config.ToolSwingSpeed > 1)
yield return new ToolSwingHandler(config.ToolSwingSpeed);
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, this.Helper.Reflection);
if (config.PamBusSpeed > 1)
yield return new PamBusHandler(config.PamBusSpeed);
if (config.TreeFallSpeed > 1)
yield return new TreeFallingHandler(config.TreeFallSpeed, this.Helper.Reflection);
// UI animations
if (config.TitleMenuTransitionSpeed > 1)
yield return new TitleMenuHandler(config.TitleMenuTransitionSpeed, this.Helper.Reflection);
if (config.LoadGameBlinkSpeed > 1)
yield return new LoadGameMenuHandler(config.LoadGameBlinkSpeed, this.Helper.Reflection);
}
}
}