Skip to content

Commit

Permalink
Create skeletons for save-load loading screen
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Byass committed Jul 21, 2021
1 parent e196972 commit adfbe71
Show file tree
Hide file tree
Showing 11 changed files with 410 additions and 9 deletions.
Binary file modified 1.3/Assemblies/BetterLoading.dll
Binary file not shown.
31 changes: 23 additions & 8 deletions Source/BetterLoadingMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,13 @@ public BetterLoadingMain(ModContentPack content) : base(content)
}
Log.Message("[BetterLoading] Injecting into main UI.");
LoadingScreen = Object.FindObjectOfType<Root_Entry>().gameObject.AddComponent<LoadingScreen>();
try
{
LoadingScreen.Background = typeof(UI_BackgroundMain).GetField("BGPlanet", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null) as Texture2D;
}
catch (Exception e)
{
Log.Warning($"Failed to find or set background texture:\n{e}");
}
InitLoadingScreenBG();

hInstance.Patch(AccessTools.Method(typeof(LongEventHandler), nameof(LongEventHandler.LongEventsOnGUI)),
new HarmonyMethod(typeof(BetterLoadingMain), nameof(DisableVanillaLoadScreen)));

hInstance.Patch(AccessTools.Method(typeof(Game), nameof(Game.LoadGame)), new HarmonyMethod(typeof(BetterLoadingMain), nameof(OnGameLoadStart)));

BetterLoadingApi.OnGameLoadComplete += CreateTimingReport;
}
else
Expand All @@ -115,6 +110,18 @@ public BetterLoadingMain(ModContentPack content) : base(content)
//Harmony.PatchAll(Assembly.GetExecutingAssembly());
}

private static void InitLoadingScreenBG()
{
try
{
LoadingScreen!.Background = typeof(UI_BackgroundMain).GetField("BGPlanet", BindingFlags.NonPublic | BindingFlags.Static)!.GetValue(null) as Texture2D;
}
catch (Exception e)
{
Log.Warning($"Failed to find or set background texture:\n{e}");
}
}

private void CreateTimingReport()
{
var timeBuildingXml = TimeSpan.FromTicks(GlobalTimingData.TicksFinishedBuildingXmlTree - GlobalTimingData.TicksStarted);
Expand Down Expand Up @@ -284,6 +291,14 @@ public static bool DisableVanillaLoadScreen()
return !LoadingScreen.shouldShow;
}

public static void OnGameLoadStart()
{
LoadingScreen = Object.FindObjectOfType<Root_Play>().gameObject
.AddComponent<LoadingScreen>();
InitLoadingScreenBG();
LoadingScreen!.StartSaveLoad();
}

private static void LogMsg(string message)
{
Log.Message($"[{DateTime.Now}] {message}");
Expand Down
28 changes: 27 additions & 1 deletion Source/LoadingScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using BetterLoading.Stage.SaveLoad;
using UnityEngine;
using Verse;

Expand All @@ -28,7 +29,7 @@ public sealed class LoadingScreen : MonoBehaviour
internal static List<LoadingStage> BootLoadList = new List<LoadingStage>
{
//For all of these stages, vanilla just shows "..."
new StageInitMods(BetterLoadingMain.hInstance),
new StageInitMods(BetterLoadingMain.hInstance!),
new StageReadXML(BetterLoadingMain.hInstance),
new StageUnifyXML(BetterLoadingMain.hInstance),
new StageApplyPatches(BetterLoadingMain.hInstance),
Expand All @@ -42,6 +43,22 @@ public sealed class LoadingScreen : MonoBehaviour
new StageRunPostFinalizeCallbacks(BetterLoadingMain.hInstance)
};

/// <summary>
/// The load list used at game boot.
/// </summary>
internal static List<LoadingStage> LoadSaveFileLoadList = new List<LoadingStage>
{
//For all of these stages, vanilla just shows "..."
new LoadSmallComponents(BetterLoadingMain.hInstance!),
new LoadWorldMap(BetterLoadingMain.hInstance),
new FinalizeWorld(BetterLoadingMain.hInstance),
new LoadMaps(BetterLoadingMain.hInstance),
new SetUpCamera(BetterLoadingMain.hInstance),
new ResolveDefCrossRefs(BetterLoadingMain.hInstance),
new SpawnAllThings(BetterLoadingMain.hInstance),
new FinalizeGameState(BetterLoadingMain.hInstance)
};

private static Dictionary<Type, LoadingStage> _loadingStagesByType = new Dictionary<Type, LoadingStage>();

public Texture2D? Background;
Expand Down Expand Up @@ -76,6 +93,13 @@ public sealed class LoadingScreen : MonoBehaviour
private Texture2D? bgSolidColor;
private Texture2D? bgContrastReducer;

public void StartSaveLoad()
{
Log.Message("[BetterLoading] Game Load detected, re-showing for save-load screen.");
_currentStage = LoadSaveFileLoadList[0];
shouldShow = true;
}

public LoadingScreen()
{
Instance = this;
Expand Down Expand Up @@ -197,6 +221,8 @@ public void OnGUI()
List<LoadingStage>? currentList = null;
if (BootLoadList.Contains(_currentStage))
currentList = BootLoadList;
else if (LoadSaveFileLoadList.Contains(_currentStage))
currentList = LoadSaveFileLoadList;

if (currentList == null)
{
Expand Down
55 changes: 55 additions & 0 deletions Source/Stage/SaveLoad/0LoadSmallComponents.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using HarmonyLib;
using JetBrains.Annotations;
using RimWorld.Planet;

namespace BetterLoading.Stage.SaveLoad
{
public class LoadSmallComponents : LoadingStage
{
private static bool _hasFinished;

public LoadSmallComponents([NotNull] Harmony instance) : base(instance)
{
}

public override string GetStageName()
{
return "Loading Small Components";
}

public override string? GetCurrentStepName()
{
return null;
}

public override int GetCurrentProgress()
{
return _hasFinished ? 1 : 0;
}

public override int GetMaximumProgress()
{
return 1;
}

public override bool IsCompleted()
{
return _hasFinished;
}

public override void BecomeInactive()
{
_hasFinished = false;
}

public override void DoPatching(Harmony instance)
{
instance.Patch(AccessTools.Method(typeof(World), nameof(World.ExposeData)), new HarmonyMethod(typeof(LoadSmallComponents), nameof(OnLoadWorldStart)));
}

public static void OnLoadWorldStart()
{
_hasFinished = true;
}
}
}
95 changes: 95 additions & 0 deletions Source/Stage/SaveLoad/1LoadWorldMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System.Linq;
using HarmonyLib;
using JetBrains.Annotations;
using RimWorld.Planet;
using Verse;

namespace BetterLoading.Stage.SaveLoad
{
public class LoadWorldMap : LoadingStage
{
private static bool _hasLoadedWorldFromFile;
private static bool _hasFinalizedWorldInit;
private static bool _loadingGeneratorDataFromFile;

private static WorldGenStep _currentWorldgenStep;
private static int _currentWorldgenStepNum;
private static int _numWorldgenSteps;

public LoadWorldMap([NotNull] Harmony instance) : base(instance)
{
}

public override string GetStageName()
{
return "Initializing World Map";
}

public override string? GetCurrentStepName()
{
if (!_hasLoadedWorldFromFile)
{
if (_numWorldgenSteps == 0)
return "Initializing basic world data";

if (_loadingGeneratorDataFromFile)
return $"Loading {_numWorldgenSteps} world features from save file";

return $"Generating {_numWorldgenSteps} world features";
}

return "Finishing world initialization";
}

public override int GetCurrentProgress()
{
return _hasLoadedWorldFromFile ? 1 : 0;
}

public override int GetMaximumProgress()
{
return 2;
}

public override bool IsCompleted()
{
return _hasFinalizedWorldInit;
}

public override void BecomeInactive()
{
_hasFinalizedWorldInit = false;
_hasLoadedWorldFromFile = false;
}

public override void DoPatching(Harmony instance)
{
instance.Patch(AccessTools.Method(typeof(World), nameof(World.ExposeData)), postfix: new HarmonyMethod(typeof(LoadWorldMap), nameof(OnLoadWorldEnd)));
instance.Patch(AccessTools.Method(typeof(WorldGenerator), nameof(WorldGenerator.GenerateFromScribe)), new HarmonyMethod(typeof(LoadWorldMap), nameof(OnStartLoadGeneratedData)));
instance.Patch(AccessTools.Method(typeof(WorldGenerator), nameof(WorldGenerator.GenerateWithoutWorldData)), new HarmonyMethod(typeof(LoadWorldMap), nameof(OnStartGenerateFreshData)));
instance.Patch(AccessTools.Method(typeof(World), nameof(World.FinalizeInit)), postfix: new HarmonyMethod(typeof(LoadWorldMap), nameof(OnFinalizeWorldInitEnd)));
}

public static void OnStartLoadGeneratedData()
{
_loadingGeneratorDataFromFile = true;
_numWorldgenSteps = WorldGenerator.GenStepsInOrder.Count();
}

public static void OnStartGenerateFreshData()
{
_loadingGeneratorDataFromFile = false;
_numWorldgenSteps = WorldGenerator.GenStepsInOrder.Count();
}

public static void OnLoadWorldEnd()
{
_hasLoadedWorldFromFile = true;
}

public static void OnFinalizeWorldInitEnd()
{
_hasFinalizedWorldInit = true;
}
}
}
35 changes: 35 additions & 0 deletions Source/Stage/SaveLoad/2FinalizeWorld.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using HarmonyLib;
using JetBrains.Annotations;

namespace BetterLoading.Stage.SaveLoad
{
public class FinalizeWorld : LoadingStage {
public FinalizeWorld([NotNull] Harmony instance) : base(instance)
{
}

public override string GetStageName()
{
throw new System.NotImplementedException();
}

public override string? GetCurrentStepName()
{
throw new System.NotImplementedException();
}

public override int GetCurrentProgress()
{
throw new System.NotImplementedException();
}

public override int GetMaximumProgress()
{
throw new System.NotImplementedException();
}

public override void DoPatching(Harmony instance)
{
}
}
}
35 changes: 35 additions & 0 deletions Source/Stage/SaveLoad/3LoadMaps.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using HarmonyLib;
using JetBrains.Annotations;

namespace BetterLoading.Stage.SaveLoad
{
public class LoadMaps : LoadingStage {
public LoadMaps([NotNull] Harmony instance) : base(instance)
{
}

public override string GetStageName()
{
throw new System.NotImplementedException();
}

public override string? GetCurrentStepName()
{
throw new System.NotImplementedException();
}

public override int GetCurrentProgress()
{
throw new System.NotImplementedException();
}

public override int GetMaximumProgress()
{
throw new System.NotImplementedException();
}

public override void DoPatching(Harmony instance)
{
}
}
}
35 changes: 35 additions & 0 deletions Source/Stage/SaveLoad/4SetUpCamera.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using HarmonyLib;
using JetBrains.Annotations;

namespace BetterLoading.Stage.SaveLoad
{
public class SetUpCamera : LoadingStage {
public SetUpCamera([NotNull] Harmony instance) : base(instance)
{
}

public override string GetStageName()
{
throw new System.NotImplementedException();
}

public override string? GetCurrentStepName()
{
throw new System.NotImplementedException();
}

public override int GetCurrentProgress()
{
throw new System.NotImplementedException();
}

public override int GetMaximumProgress()
{
throw new System.NotImplementedException();
}

public override void DoPatching(Harmony instance)
{
}
}
}
Loading

0 comments on commit adfbe71

Please sign in to comment.