forked from Pathoschild/StardewMods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModEntry.cs
228 lines (195 loc) · 8.54 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
using System;
using Microsoft.Xna.Framework.Input;
using Pathoschild.Stardew.Common;
using Pathoschild.Stardew.SkipIntro.Framework;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewValley;
using StardewValley.Menus;
namespace Pathoschild.Stardew.SkipIntro
{
/// <summary>The mod entry point.</summary>
internal class ModEntry : Mod
{
/*********
** Fields
*********/
/// <summary>The mod configuration.</summary>
private ModConfig Config;
/// <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
*********/
/// <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 = this.LoadConfig();
helper.Events.GameLoop.GameLaunched += this.OnGameLaunched;
helper.Events.Display.MenuChanged += this.OnMenuChanged;
helper.Events.GameLoop.UpdateTicked += this.OnUpdateTicked;
}
/*********
** Private methods
*********/
/****
** Event handlers
****/
/// <summary>The event called after the first game update, once all mods are loaded.</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event arguments.</param>
private void OnGameLaunched(object sender, GameLaunchedEventArgs e)
{
// add Generic Mod Config Menu integration
new GenericModConfigMenuIntegrationForSkipIntro(
getConfig: () => this.Config,
reset: () =>
{
this.Config = new ModConfig();
this.Helper.WriteConfig(this.Config);
},
saveAndApply: () => this.Helper.WriteConfig(this.Config),
modRegistry: this.Helper.ModRegistry,
monitor: this.Monitor,
manifest: this.ModManifest
).Register();
}
/// <summary>The method called 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 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;
}
/// <summary>Receives 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)
{
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;
switch (currentStage)
{
// skip to title screen
case Stage.SkipIntro:
if (Constants.TargetPlatform == GamePlatform.Android)
{
// skip to title screen
menu.skipToTitleButtons();
// skip button transition
while (this.Helper.Reflection.GetField<bool>(menu, "isTransitioningButtons").GetValue())
menu.update(Game1.currentGameTime);
}
else
{
// skip to title screen
menu.receiveKeyPress(Keys.Escape);
menu.update(Game1.currentGameTime);
// skip button transition
while (this.Helper.Reflection.GetField<int>(menu, "buttonsToShow").GetValue() < TitleMenu.numberOfButtons)
menu.update(Game1.currentGameTime);
}
// skip to next screen
switch (this.Config.SkipTo)
{
case Screen.Title:
return Stage.None;
case Screen.Load:
// skip to load screen
menu.performButtonAction("Load");
while (TitleMenu.subMenu == null)
menu.update(Game1.currentGameTime);
return Stage.None;
case Screen.JoinCoop:
case Screen.HostCoop:
// skip to co-op screen
menu.performButtonAction("Co-op");
return Stage.TransitioningToCoop; // need a full game update before the next step to avoid crashes
default:
this.Monitor.Log($"Unrecognized skip option {this.Config.SkipTo}.", LogLevel.Warn);
return Stage.None;
}
// skip to co-op screen
case Stage.TransitioningToCoop:
while (TitleMenu.subMenu == null)
menu.update(Game1.currentGameTime);
return this.Config.SkipTo == Screen.JoinCoop
? Stage.None
: Stage.WaitingForConnection;
// skip to host tab after connection is established
case Stage.WaitingForConnection:
{
// not applicable
if (this.Config.SkipTo != Screen.HostCoop || TitleMenu.subMenu is not CoopMenu submenu)
return Stage.None;
// not connected yet
if (submenu.hostTab == null)
return currentStage;
// select host tab
submenu.receiveLeftClick(submenu.hostTab.bounds.X, submenu.hostTab.bounds.Y, playSound: false);
return Stage.None;
}
default:
return Stage.None;
}
}
}
}