Skip to content

Commit

Permalink
Merge pull request #4 from Falki-git/dev
Browse files Browse the repository at this point in the history
Add setting for auto load the last played campaign
  • Loading branch information
Falki-git authored Nov 8, 2023
2 parents c8f0b0f + ec82c7f commit 68510f2
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 11 deletions.
13 changes: 13 additions & 0 deletions .idea/.idea.SkipSplashScreen/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/.idea.SkipSplashScreen/.idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/.idea.SkipSplashScreen/.idea/indexLayout.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/.idea.SkipSplashScreen/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugin_template/swinfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"name": "Skip Splash Screen",
"description": "Skips the splash screen and health warning. To be used only for developing mods (faster iteration).",
"source": "https://github.com/Falki-git/SkipSplashScreen",
"version": "1.1.2",
"version": "1.2.0",
"version_check": "https://raw.githubusercontent.com/Falki-git/SkipSplashScreen/master/plugin_template/swinfo.json",
"ksp2_version": {
"min": "0.1.5",
Expand Down
117 changes: 107 additions & 10 deletions src/SkipSplashScreen/SkipSplashScreenPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,54 +1,151 @@
using BepInEx;
using System.Collections;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using JetBrains.Annotations;
using KSP.Game.Flow;
using KSP.Game;
using SpaceWarp;
using SpaceWarp.API.Mods;
using BepInEx.Logging;
using UnityEngine;

namespace SkipSplashScreen;

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

private static ManualLogSource _logger = BepInEx.Logging.Logger.CreateLogSource("SkipSplashScreen");
private new static readonly ManualLogSource Logger = BepInEx.Logging.Logger.CreateLogSource("SkipSplashScreen");

private CampaignMenu _campaignMenuScript;
private bool _singlePlayerMenuTriggered;
private bool _loadInitiated;
private bool _hasFinished;

private ConfigEntry<bool> _loadLastSavedCampaign;

public void Start()
{
Harmony.CreateAndPatchAll(typeof(SkipSplashScreenPlugin));

_loadLastSavedCampaign = Config.Bind(
MyPluginInfo.PLUGIN_NAME,
"Auto load last played campaign",
false,
"Automatically loads the last save game file after main menu is finished loading.");
}

public void Update()
{
if (_hasFinished)
return;

var gameState = GameManager.Instance?.Game?.GlobalGameState?.GetState();

if (gameState == null)
return;

if (gameState == GameState.MainMenu)
{
_logger.LogDebug($"disappears into oblivion...");
Destroy(this);
}
if (_loadLastSavedCampaign?.Value ?? false)
{
if (!_singlePlayerMenuTriggered)
{
// Trigger the Single Player menu in order for the CampaignEntryTiles to get created
TriggerSinglePlayerMenu();
}

if (!_loadInitiated)
LoadLastSinglePlayerGame();
}
else
{
DestroyPlugin();
}
}
}

[HarmonyPatch(typeof(SequentialFlow), "AddAction"), HarmonyPrefix]
private static bool SequentialFlow_AddAction(FlowAction action)
{
if (action.Name == "Creating Splash Screens Prefab")
{
_logger.LogDebug($"'Creating Splash Screens Prefab' action found. Skipping!");
Logger.LogInfo("'Creating Splash Screens Prefab' action found. Skipping!");
GameManager.Instance.HasPhotosensitivityWarningBeenShown = true;
return false;
}
else
return true;

return true;
}

private void TriggerSinglePlayerMenu()
{
Logger.LogInfo("'Auto load last played campaign' is enabled. To turn it off go into Settings -> Mods -> Skip Splash Screen");

var mainMenu = GameObject.Find(
"GameManager/Default Game Instance(Clone)/UI Manager(Clone)/Main Canvas/MainMenu(Clone)/");
var campaignMenu = mainMenu.GetChild("CampaignMenu");
_campaignMenuScript = campaignMenu.GetComponent<CampaignMenu>();

var campaignSavesList = _campaignMenuScript.Game.SaveLoadManager.GetCampaignSaveFiles(CampaignType.SinglePlayer);
_campaignMenuScript.FillCampaignScrollView(campaignSavesList, _campaignMenuScript._campaignScrollViewContentLastPlayedDate);

_singlePlayerMenuTriggered = true;
}

private void LoadLastSinglePlayerGame()
{
// Wait for the CampaignEntryTiles to get created
if (_campaignMenuScript._campaignEntryTiles.Count == 0)
return;

CampaignTileEntry latestCampaign = null;
DateTime latestPlayed = DateTime.MinValue;

// Determine what campaign was played last.
foreach (var campaign in _campaignMenuScript._campaignEntryTiles)
{
var lastPlayed = DateTime.Parse(campaign.CampaignLastPlayedTime);

if (latestCampaign == null || lastPlayed > latestPlayed)
{
latestCampaign = campaign;
latestPlayed = lastPlayed;
}
}

if (latestCampaign != null)
{
// What campaign tile entry is clicked, last saved game is automatically selected
latestCampaign.OnCampaignClick();
Logger.LogInfo($"Auto loading campaign '{latestCampaign.CampaignName}'.");

StartCoroutine(Load());
_loadInitiated = true;
}
}

private IEnumerator Load()
{
// Wait for the next frame cause save file won't be still selected here
yield return null;
_campaignMenuScript._campaignLoadMenu.LoadSelectedFile();

DestroyPlugin();
}

private void DestroyPlugin()
{
//Logger.LogDebug($"disappears into oblivion...");
//Destroy(this);

// We'll keep the plugin alive because it's needed for config changes
Logger.LogDebug($"{MyPluginInfo.PLUGIN_NAME} workflow completed.");
_hasFinished = true;
}
}
}

0 comments on commit 68510f2

Please sign in to comment.