Skip to content

Commit

Permalink
Improvements to the save file loading screen
Browse files Browse the repository at this point in the history
  • Loading branch information
SamboyCoding committed Mar 19, 2024
1 parent 4a99625 commit 283c647
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 100 deletions.
Binary file modified 1.4/Assemblies/BetterLoading.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion About/Manifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Manifest>
<identifier>BetterLoading</identifier>
<version>3.4.0.0</version>
<version>3.5.0.0</version>
<loadBefore>
<li>Core &gt;= 1.0</li>
<li>Startupimpact</li>
Expand Down
11 changes: 9 additions & 2 deletions Source/LoadingScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ public sealed class LoadingScreen : MonoBehaviour
};

/// <summary>
/// The load list used at game boot.
/// The load list used during loading a save (Game.LoadGame)
///
/// Stage 0 - Load Small Components. Initial load up until beginning of World.ExposeData.
/// Stage 1 - Load World Map. Covers World.ExposeData and the subsequent call to World.FinalizeInit from Game.LoadGame
/// Stage 2 - Load Maps. Covers Map.ExposeData for each map in the save
/// Stage 3 - Finalize Scribe Load. Covers Scribe.loader.FinalizeLoading, which essentially just calls ResolveAllCrossReferences and DoAllPostLoadInits.
/// Stage 4 - Finalize Maps. Covers Map.FinalizeLoading (which is called directly for each map from Game.LoadGame). Ends when FinalizeLoading returns on the last map.
/// Stage 5 - Finalize Game State. Covers everything from the end of FinalizeLoading on the last map, until the long event finishes, which in practice is Game.FinalizeInit and GameComponentUtility.LoadedGame
/// </summary>
internal static List<LoadingStage> LoadSaveFileLoadList = new()
{
Expand All @@ -42,7 +49,7 @@ public sealed class LoadingScreen : MonoBehaviour
new LoadWorldMap(BetterLoadingMain.hInstance),
new LoadMaps(BetterLoadingMain.hInstance),
new FinalizeScribeLoad(BetterLoadingMain.hInstance),
new SpawnAllThings(BetterLoadingMain.hInstance),
new FinalizeMap(BetterLoadingMain.hInstance),
new FinalizeGameState(BetterLoadingMain.hInstance)
};

Expand Down
20 changes: 10 additions & 10 deletions Source/Stage/SaveLoad/2LoadMaps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ public override string GetCurrentStepName()

var text = $"Map {_currMapNum + 1} of {NumMaps}: ";

if (_currMapLoadedCompressed)
return text + "Loading Things";
if (_currMapInitialized)
return text + "Loading Components";

if (_currMapLoadedComponents)
return text + "Loading Compressed Map Data";

if (_currMapInitialized)
return text + "Loading Components";
if (_currMapLoadedCompressed)
return text + "Loading Things";

return text + "Reading Data";
}
Expand All @@ -67,15 +67,15 @@ public override int GetCurrentProgress()
return 0;

var baseValue = _currMapNum * 4;

if (_currMapLoadedCompressed)
return baseValue + 3;

if (_currMapInitialized)
return baseValue + 1;

if (_currMapLoadedComponents)
return baseValue + 2;

if (_currMapInitialized)
return baseValue + 1;
if (_currMapLoadedCompressed)
return baseValue + 3;

return baseValue;
}
Expand Down
187 changes: 187 additions & 0 deletions Source/Stage/SaveLoad/4FinalizeMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
using System;
using HarmonyLib;
using JetBrains.Annotations;
using RimWorld;
using Verse;
using Verse.AI;

namespace BetterLoading.Stage.SaveLoad
{
public class FinalizeMap : LoadingStage
{
private enum MapFinalizeState
{
SpawnThings,
SpawnBuildings,
RebuildingPathfindingCache,
FinalizeGeometry,
InitThings,
ListFilth,
CountResources,
CountWealth,
InitComponents,

NumStates
}

private static int _currMapIdx = -1;
private static int _numThingsThisMapSoFar;
private static int _numBuildingsThisMapSoFar;
private static MapFinalizeState _state;

private static bool _finished;

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

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

public override string? GetCurrentStepName()
{
if (_currMapIdx == -1)
return "Waiting...";

var mapString = $"Map {_currMapIdx + 1} of {LoadMaps.NumMaps}:";
return _state switch
{
MapFinalizeState.SpawnThings => $"{mapString} Spawning Things ({_numThingsThisMapSoFar} so far)",
MapFinalizeState.SpawnBuildings => $"{mapString} Spawning Buildings ({_numBuildingsThisMapSoFar} so far)",
MapFinalizeState.RebuildingPathfindingCache => $"{mapString} Calculating Pathfinding Costs",
MapFinalizeState.FinalizeGeometry => $"{mapString} Initializing Map Grid",
MapFinalizeState.InitThings => $"{mapString} Initializing Things",
MapFinalizeState.ListFilth => $"{mapString} Loading Filth",
MapFinalizeState.CountResources => $"{mapString} Counting Resources",
MapFinalizeState.CountWealth => $"{mapString} Counting Wealth",
MapFinalizeState.InitComponents => $"{mapString} Initializing Sub-Components",
_ => throw new ArgumentOutOfRangeException()
};
}

public override int GetCurrentProgress()
{
if (_currMapIdx == -1)
return 0;

var baseMapProgress = _currMapIdx * (int)MapFinalizeState.NumStates;

return baseMapProgress + (int)_state;
}

public override int GetMaximumProgress()
{
return LoadMaps.NumMaps * (int) MapFinalizeState.NumStates;
}

public override bool IsCompleted()
{
return _finished;
}

public override void BecomeInactive()
{
_finished = false;
_currMapIdx = -1;
_numThingsThisMapSoFar = 0;
}

public override void DoPatching(Harmony instance)
{
instance.Patch(AccessTools.Method(typeof(Map), nameof(Map.FinalizeLoading)), new(typeof(FinalizeMap), nameof(OnMapStartFinalizeLoad)), new(typeof(FinalizeMap), nameof(OnMapFinishFinalizeLoad)));

//SpawnThings
instance.Patch(
AccessTools.Method(typeof(GenSpawn), nameof(GenSpawn.Spawn), new[] {typeof(Thing), typeof(IntVec3), typeof(Map), typeof(Rot4), typeof(WipeMode), typeof(bool)}),
new(typeof(FinalizeMap), nameof(OnThingAboutToSpawn))
);

//SpawnThings -> SpawnBuildings
instance.Patch(AccessTools.Method(typeof(GenSpawn), nameof(GenSpawn.SpawnBuildingAsPossible)), new(typeof(FinalizeMap), nameof(OnBuildingAboutToSpawn)));

//SpawnBuildings -> RebuildingPathfindingCache
instance.Patch(AccessTools.Method(typeof(Map), nameof(Map.FinalizeInit)), new(typeof(FinalizeMap), nameof(OnMapStartFinalizeInit)));

//RebuildingPathfindingCache -> FinalizeGeometry2
instance.Patch(AccessTools.Method(typeof(RegionAndRoomUpdater), nameof(RegionAndRoomUpdater.RebuildAllRegionsAndRooms)), new(typeof(FinalizeMap), nameof(OnMapStartRebuildRegions)));

//FinalizeGeometry -> InitThings
instance.Patch(AccessTools.Method(typeof(GasGrid), nameof(GasGrid.RecalculateEverHadGas)), postfix: new(typeof(FinalizeMap), nameof(OnMapFinishFinalizeGeometry)));

//InitThings -> ListFilth
instance.Patch(AccessTools.Method(typeof(ListerFilthInHomeArea), nameof(ListerFilthInHomeArea.RebuildAll)), new(typeof(FinalizeMap), nameof(OnMapStartListFilth)));

//ListFilth -> CountResources
instance.Patch(AccessTools.Method(typeof(ResourceCounter), nameof(ResourceCounter.UpdateResourceCounts)), new(typeof(FinalizeMap), nameof(OnMapStartCountResources)));

//CountResources -> CountWealth
instance.Patch(AccessTools.Method(typeof(WealthWatcher), nameof(WealthWatcher.ForceRecount)), new(typeof(FinalizeMap), nameof(OnMapStartCountWealth)));

//CountWealth -> FinalizeComponents
instance.Patch(AccessTools.Method(typeof(MapComponentUtility), nameof(MapComponentUtility.FinalizeInit)), new(typeof(FinalizeMap), nameof(OnMapStartFinalizeComponents)));

//Transition to next map handled by postfix on FinalizeLoad
}

public static void OnMapStartFinalizeLoad()
{
_currMapIdx++;
_numThingsThisMapSoFar = 0;
_state = MapFinalizeState.SpawnThings;
}

public static void OnMapFinishFinalizeLoad()
{
if (_currMapIdx == LoadMaps.NumMaps - 1)
_finished = true;
}

public static void OnThingAboutToSpawn()
{
_numThingsThisMapSoFar++;
}

public static void OnBuildingAboutToSpawn()
{
_state = MapFinalizeState.SpawnBuildings;
_numBuildingsThisMapSoFar++;
}

public static void OnMapStartFinalizeInit()
{
_state = MapFinalizeState.RebuildingPathfindingCache;
}

public static void OnMapStartRebuildRegions()
{
_state = MapFinalizeState.FinalizeGeometry;
}

public static void OnMapFinishFinalizeGeometry()
{
_state = MapFinalizeState.InitThings;
}

public static void OnMapStartListFilth()
{
_state = MapFinalizeState.ListFilth;
}

public static void OnMapStartCountResources()
{
_state = MapFinalizeState.CountResources;
}

public static void OnMapStartCountWealth()
{
_state = MapFinalizeState.CountWealth;
}

public static void OnMapStartFinalizeComponents()
{
_state = MapFinalizeState.InitComponents;
}
}
}
87 changes: 0 additions & 87 deletions Source/Stage/SaveLoad/4SpawnAllThings.cs

This file was deleted.

0 comments on commit 283c647

Please sign in to comment.