Skip to content

Commit bde8c29

Browse files
committed
Add setting for automatically loading last played campaign
1 parent 200967c commit bde8c29

File tree

1 file changed

+107
-10
lines changed

1 file changed

+107
-10
lines changed
Lines changed: 107 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,151 @@
1-
using BepInEx;
1+
using System.Collections;
2+
using BepInEx;
3+
using BepInEx.Configuration;
24
using HarmonyLib;
35
using JetBrains.Annotations;
46
using KSP.Game.Flow;
57
using KSP.Game;
68
using SpaceWarp;
79
using SpaceWarp.API.Mods;
810
using BepInEx.Logging;
11+
using UnityEngine;
912

1013
namespace SkipSplashScreen;
1114

1215
[BepInPlugin("com.github.falki.skip_splash_screen", MyPluginInfo.PLUGIN_NAME, MyPluginInfo.PLUGIN_VERSION)]
1316
[BepInDependency(SpaceWarpPlugin.ModGuid, SpaceWarpPlugin.ModVer)]
1417
public class SkipSplashScreenPlugin : BaseSpaceWarpPlugin
1518
{
16-
// These are useful in case some other mod wants to add a dependency to this one
1719
[PublicAPI] public const string ModGuid = MyPluginInfo.PLUGIN_GUID;
1820
[PublicAPI] public const string ModName = MyPluginInfo.PLUGIN_NAME;
1921
[PublicAPI] public const string ModVer = MyPluginInfo.PLUGIN_VERSION;
2022

21-
private static ManualLogSource _logger = BepInEx.Logging.Logger.CreateLogSource("SkipSplashScreen");
23+
private new static readonly ManualLogSource Logger = BepInEx.Logging.Logger.CreateLogSource("SkipSplashScreen");
24+
25+
private CampaignMenu _campaignMenuScript;
26+
private bool _singlePlayerMenuTriggered;
27+
private bool _loadInitiated;
28+
private bool _hasFinished;
29+
30+
private ConfigEntry<bool> _loadLastSavedCampaign;
2231

2332
public void Start()
2433
{
2534
Harmony.CreateAndPatchAll(typeof(SkipSplashScreenPlugin));
35+
36+
_loadLastSavedCampaign = Config.Bind(
37+
MyPluginInfo.PLUGIN_NAME,
38+
"Auto load last played campaign",
39+
false,
40+
"Automatically loads the last save game file after main menu is finished loading.");
2641
}
2742

2843
public void Update()
2944
{
45+
if (_hasFinished)
46+
return;
47+
3048
var gameState = GameManager.Instance?.Game?.GlobalGameState?.GetState();
3149

3250
if (gameState == null)
3351
return;
3452

3553
if (gameState == GameState.MainMenu)
3654
{
37-
_logger.LogDebug($"disappears into oblivion...");
38-
Destroy(this);
39-
}
55+
if (_loadLastSavedCampaign?.Value ?? false)
56+
{
57+
if (!_singlePlayerMenuTriggered)
58+
{
59+
// Trigger the Single Player menu in order for the CampaignEntryTiles to get created
60+
TriggerSinglePlayerMenu();
61+
}
62+
63+
if (!_loadInitiated)
64+
LoadLastSinglePlayerGame();
65+
}
66+
else
67+
{
68+
DestroyPlugin();
69+
}
70+
}
4071
}
4172

4273
[HarmonyPatch(typeof(SequentialFlow), "AddAction"), HarmonyPrefix]
4374
private static bool SequentialFlow_AddAction(FlowAction action)
4475
{
4576
if (action.Name == "Creating Splash Screens Prefab")
4677
{
47-
_logger.LogDebug($"'Creating Splash Screens Prefab' action found. Skipping!");
78+
Logger.LogInfo("'Creating Splash Screens Prefab' action found. Skipping!");
4879
GameManager.Instance.HasPhotosensitivityWarningBeenShown = true;
4980
return false;
5081
}
51-
else
52-
return true;
82+
83+
return true;
84+
}
85+
86+
private void TriggerSinglePlayerMenu()
87+
{
88+
Logger.LogInfo("'Auto load last played campaign' is enabled. To turn it off go into Settings -> Mods -> Skip Splash Screen");
89+
90+
var mainMenu = GameObject.Find(
91+
"GameManager/Default Game Instance(Clone)/UI Manager(Clone)/Main Canvas/MainMenu(Clone)/");
92+
var campaignMenu = mainMenu.GetChild("CampaignMenu");
93+
_campaignMenuScript = campaignMenu.GetComponent<CampaignMenu>();
94+
95+
var campaignSavesList = _campaignMenuScript.Game.SaveLoadManager.GetCampaignSaveFiles(CampaignType.SinglePlayer);
96+
_campaignMenuScript.FillCampaignScrollView(campaignSavesList, _campaignMenuScript._campaignScrollViewContentLastPlayedDate);
97+
98+
_singlePlayerMenuTriggered = true;
99+
}
100+
101+
private void LoadLastSinglePlayerGame()
102+
{
103+
// Wait for the CampaignEntryTiles to get created
104+
if (_campaignMenuScript._campaignEntryTiles.Count == 0)
105+
return;
106+
107+
CampaignTileEntry latestCampaign = null;
108+
DateTime latestPlayed = DateTime.MinValue;
109+
110+
// Determine what campaign was played last.
111+
foreach (var campaign in _campaignMenuScript._campaignEntryTiles)
112+
{
113+
var lastPlayed = DateTime.Parse(campaign.CampaignLastPlayedTime);
114+
115+
if (latestCampaign == null || lastPlayed > latestPlayed)
116+
{
117+
latestCampaign = campaign;
118+
latestPlayed = lastPlayed;
119+
}
120+
}
121+
122+
if (latestCampaign != null)
123+
{
124+
// What campaign tile entry is clicked, last saved game is automatically selected
125+
latestCampaign.OnCampaignClick();
126+
Logger.LogInfo($"Auto loading campaign '{latestCampaign.CampaignName}'.");
127+
128+
StartCoroutine(Load());
129+
_loadInitiated = true;
130+
}
131+
}
132+
133+
private IEnumerator Load()
134+
{
135+
// Wait for the next frame cause save file won't be still selected here
136+
yield return null;
137+
_campaignMenuScript._campaignLoadMenu.LoadSelectedFile();
138+
139+
DestroyPlugin();
140+
}
141+
142+
private void DestroyPlugin()
143+
{
144+
//Logger.LogDebug($"disappears into oblivion...");
145+
//Destroy(this);
146+
147+
// We'll keep the plugin alive because it's needed for config changes
148+
Logger.LogDebug($"{MyPluginInfo.PLUGIN_NAME} workflow completed.");
149+
_hasFinished = true;
53150
}
54-
}
151+
}

0 commit comments

Comments
 (0)