-
-
Notifications
You must be signed in to change notification settings - Fork 386
/
Copy pathModEntry.cs
264 lines (223 loc) · 8.58 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
using System;
using Microsoft.Xna.Framework;
using Pathoschild.Stardew.Common;
using Pathoschild.Stardew.Common.Integrations.GenericModConfigMenu;
using Pathoschild.Stardew.SkipIntro.Framework;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewValley;
using StardewValley.Menus;
using StardewValley.SDKs;
namespace Pathoschild.Stardew.SkipIntro;
/// <summary>The mod entry point.</summary>
internal class ModEntry : Mod
{
/*********
** Fields
*********/
/// <summary>The mod configuration.</summary>
private ModConfig Config = null!; // set in Entry
/// <summary>Whether the game has launched.</summary>
private bool IsLaunched;
/// <summary>The current step in the mod logic.</summary>
private Stage CurrentStage = Stage.None;
/*********
** Public methods
*********/
/// <inheritdoc />
public override void Entry(IModHelper helper)
{
I18n.Init(helper.Translation);
CommonHelper.RemoveObsoleteFiles(this, "SkipIntro.pdb"); // removed in 1.9.13
this.Config = this.LoadConfig();
helper.Events.GameLoop.GameLaunched += this.OnGameLaunched;
helper.Events.Display.MenuChanged += this.OnMenuChanged;
helper.Events.GameLoop.UpdateTicked += this.OnUpdateTicked;
}
/*********
** Private methods
*********/
/****
** Event handlers
****/
/// <inheritdoc cref="IGameLoopEvents.GameLaunched" />
private void OnGameLaunched(object? sender, GameLaunchedEventArgs e)
{
this.AddGenericModConfigMenu(
new GenericModConfigMenuIntegrationForSkipIntro(),
get: () => this.Config,
set: config => this.Config = config
);
}
/// <inheritdoc cref="IDisplayEvents.MenuChanged" />
private void OnMenuChanged(object? sender, MenuChangedEventArgs e)
{
if (Constants.TargetPlatform == GamePlatform.Android)
return; // return to title doesn't replay intro on Android
if (e.NewMenu is TitleMenu)
this.CurrentStage = Stage.SkipIntro;
}
/// <inheritdoc cref="IGameLoopEvents.UpdateTicked" />
private void OnUpdateTicked(object? sender, UpdateTickedEventArgs e)
{
try
{
// wait until game window opens
if (Game1.ticks <= 1)
return;
// start intro skip on game launch
if (!this.IsLaunched)
{
if (Game1.activeClickableMenu is not TitleMenu)
return;
this.IsLaunched = true;
this.CurrentStage = Stage.SkipIntro;
}
// apply skip logic
if (this.CurrentStage != Stage.None)
{
this.CurrentStage = Game1.activeClickableMenu is TitleMenu menu
? this.Skip(menu, this.CurrentStage)
: Stage.None;
}
}
catch (Exception ex)
{
this.Monitor.InterceptError(ex, "skipping the intro");
this.Helper.Events.GameLoop.UpdateTicked -= this.OnUpdateTicked;
}
}
/****
** Methods
****/
/// <summary>Load the mod configuration.</summary>
private ModConfig LoadConfig()
{
var config = this.Helper.ReadConfig<ModConfig>();
if (Constants.TargetPlatform == GamePlatform.Android)
{
if (config.SkipTo is Screen.HostCoop or Screen.JoinCoop)
config.SkipTo = Screen.Title; // no co-op on Android
}
return config;
}
/// <summary>Skip the intro if the game is ready.</summary>
/// <param name="menu">The title menu whose intro to skip.</param>
/// <param name="currentStage">The current step in the mod logic.</param>
/// <returns>Returns the next step in the skip logic.</returns>
private Stage Skip(TitleMenu menu, Stage currentStage)
{
// wait until the game is ready
if (Game1.currentGameTime == null)
return currentStage;
// do nothing if a confirmation box is on-screen (e.g. multiplayer disconnect error)
if (TitleMenu.subMenu is ConfirmationDialog)
return Stage.None;
// apply skip step
return currentStage switch
{
Stage.SkipIntro => this.SkipToTitle(menu),
Stage.TransitionToLoad => this.TransitionToLoad(menu),
Stage.StartTransitionToCoop => this.StartTransitionToCoop(menu),
Stage.TransitionToCoop => this.TransitionToCoop(menu),
Stage.TransitionToCoopHost => this.TransitionToCoopHost(),
_ => Stage.None
};
}
/// <summary>Skip to the title screen.</summary>
/// <param name="menu">The title menu.</param>
/// <returns>Returns the next step in the skip logic.</returns>
private Stage SkipToTitle(TitleMenu menu)
{
// skip to title screen
menu.skipToTitleButtons();
// avoid game crash since Game1.currentSong isn't set yet
Game1.currentSong ??= Game1.soundBank.GetCue("MainTheme");
// skip button transition
menu.buttonsToShow = TitleMenu.numberOfButtons;
if (Constants.TargetPlatform == GamePlatform.Android)
{
while (menu.isTransitioningButtons)
menu.update(Game1.currentGameTime);
}
else
{
while (menu.buttonsToShow < TitleMenu.numberOfButtons)
menu.update(Game1.currentGameTime);
}
// set next step
switch (this.Config.SkipTo)
{
case Screen.Title:
return Stage.None;
case Screen.Load:
return Stage.TransitionToLoad;
case Screen.JoinCoop:
case Screen.HostCoop:
return Stage.StartTransitionToCoop;
default:
this.Monitor.Log($"Unrecognized skip option {this.Config.SkipTo}.", LogLevel.Warn);
return Stage.None;
}
}
/// <summary>Skip from the title screen to the load menu.</summary>
/// <param name="menu">The title menu.</param>
/// <returns>Returns the next step in the skip logic.</returns>
private Stage TransitionToLoad(TitleMenu menu)
{
// start transition
menu.performButtonAction("Load");
// skip animation
while (TitleMenu.subMenu == null)
menu.update(Game1.currentGameTime);
return Stage.None;
}
/// <summary>Start transitioning from the title screen to the co-op section.</summary>
/// <param name="menu">The title menu.</param>
/// <returns>Returns the next step in the skip logic.</returns>
private Stage StartTransitionToCoop(TitleMenu menu)
{
// wait until the game client SDK is ready, which is needed to load the co-op menus
SDKHelper sdk = this.Helper.Reflection.GetProperty<SDKHelper>(typeof(Program), "sdk").GetValue();
if (!sdk.ConnectionFinished)
return Stage.StartTransitionToCoop;
// start transition
menu.performButtonAction("Co-op");
// need a full game update before the next step to avoid crashes
return Stage.TransitionToCoop;
}
/// <summary>Finish transitioning from the title screen to the co-op section.</summary>
/// <param name="menu">The title menu.</param>
/// <returns>Returns the next step in the skip logic.</returns>
private Stage TransitionToCoop(TitleMenu menu)
{
// skip animation
while (TitleMenu.subMenu == null)
menu.update(Game1.currentGameTime);
// prevent a crash in CoopMenu.gameWindowSizeChanged if it's called before connectionFinished
if (TitleMenu.subMenu is CoopMenu { joinTab: null } coop)
{
coop.joinTab = new ClickableComponent(Rectangle.Empty, "");
coop.hostTab = new ClickableComponent(Rectangle.Empty, "");
coop.refreshButton = new ClickableComponent(Rectangle.Empty, "");
}
// set next step
return this.Config.SkipTo == Screen.HostCoop
? Stage.TransitionToCoopHost
: Stage.None;
}
/// <summary>Skip from the co-op section to the host screen.</summary>
/// <returns>Returns the next step in the skip logic.</returns>
private Stage TransitionToCoopHost()
{
// not applicable
if (TitleMenu.subMenu is not CoopMenu submenu)
return Stage.None;
// not connected yet
if (submenu.hostTab == null)
return Stage.TransitionToCoopHost;
// select host tab
submenu.receiveLeftClick(submenu.hostTab.bounds.X, submenu.hostTab.bounds.Y, playSound: false);
return Stage.None;
}
}