Skip to content

Commit

Permalink
Fix issue if last mod in load order pre-loads patches, and show tips …
Browse files Browse the repository at this point in the history
…while loading :)
  • Loading branch information
Sam Byass committed Nov 24, 2020
1 parent cee2abf commit 4077d8e
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
Binary file modified 1.1-1.2/Assemblies/BetterLoading.dll
Binary file not shown.
38 changes: 38 additions & 0 deletions Source/LoadingScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ public sealed class LoadingScreen : MonoBehaviour
{
public static LoadingScreen Instance { get; private set; }

private static bool _tipsAvailable;

private static List<string>? _tips;
private static string? _currentTip;
private static long _timeLastTipShown;
private const int _ticksPerTip = 5 * 10_000_000; //3 seconds

/// <summary>
/// The load list used at game boot.
/// </summary>
Expand Down Expand Up @@ -307,6 +314,31 @@ public void OnGUI()
rect.height += 100; //Allow for wrapping
Text.Anchor = TextAnchor.UpperCenter;
Widgets.Label(rect, "Current: " + currentStageText);

//Draw loading lines
Text.Font = GameFont.Medium;
rect.y += 120;
if (!_tipsAvailable)
{
Widgets.Label(rect, "Gameplay tips will be shown here once the game loads them (after stage 7 completes)");
}
else
{
//Load tips if required
_tips ??= DefDatabase<TipSetDef>.AllDefsListForReading.SelectMany(set => set.tips).InRandomOrder().ToList();

if (_currentTip == null || (DateTime.Now.Ticks - _timeLastTipShown) >= _ticksPerTip)
{
//No tip chosen yet, or time for next tip - pick another and reset timer.
_currentTip = _tips.Pop();
_timeLastTipShown = DateTime.Now.Ticks;
}

//Draw tip.
Widgets.Label(rect, _currentTip);
}
Text.Font = GameFont.Small;

rect.height -= 100; //Remove increased height

//Render global progress bar.
Expand Down Expand Up @@ -609,5 +641,11 @@ private void DrawSaveFileLoad()
barRect = new Rect(rect.x, rect.y + 25, rect.width - 24, 20);
Widgets.FillableBar(barRect, currentStage <= EnumLoadingStage.FinalizeLoad ? 0 : 1);
}

public static void MarkTipsNowAvailable()
{
Log.Message("[BetterLoading] Tips should now be available. Showing...");
_tipsAvailable = true;
}
}
}
20 changes: 20 additions & 0 deletions Source/ModContentPackMirror .cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using JetBrains.Annotations;
using Verse;

namespace BetterLoading
{
public static class ModContentPackMirror
{
private static readonly FieldInfo PatchesField = typeof(ModContentPack).GetField("patches", BindingFlags.NonPublic | BindingFlags.Instance);

public static List<PatchOperation>? GetPatches(ModContentPack instance)
{
return (List<PatchOperation>?) PatchesField.GetValue(instance);
}
}
}
17 changes: 17 additions & 0 deletions Source/Stage/InitialLoad/3StageApplyPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,28 @@ public override bool IsCompleted()

public override void DoPatching(Harmony instance)
{
instance.Patch(AccessTools.Method(typeof(LoadedModManager), nameof(LoadedModManager.ApplyPatches)), new HarmonyMethod(typeof(StageApplyPatches), nameof(PreApplyPatches)));

instance.Patch(AccessTools.Method(typeof(ModContentPack), "LoadPatches"), new HarmonyMethod(typeof(StageApplyPatches), nameof(PreLoadPatches)), new HarmonyMethod(typeof(StageApplyPatches), nameof(PostLoadPatches)));

instance.Patch(AccessTools.Method(typeof(PatchOperation), nameof(PatchOperation.Apply)), new HarmonyMethod(typeof(StageApplyPatches), nameof(PostApplyPatch)));
}

public static void PreApplyPatches()
{
//Reset this in case the patch was triggered early
_currentModNum = 0;
//Some mods load their patches before this point - remove them (e.g. DocWorld)
foreach (var modContentPack in LoadedModManager.RunningMods)
{
if (ModContentPackMirror.GetPatches(modContentPack) != null)
{
Log.Message($"[BetterLoading] Removing {modContentPack.Name} from the list of mods to load patches from - its patches have already been loaded.");
_modList.Remove(modContentPack);
}
}
}

public static void PreLoadPatches(ModContentPack __instance)
{
_loadingPatches = true;
Expand Down
6 changes: 6 additions & 0 deletions Source/Stage/InitialLoad/6StageResolveDefDatabases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ public override void BecomeActive()
inst = LoadingScreen.GetStageInstance<StageResolveDefDatabases>();
}

public override void BecomeInactive()
{
base.BecomeInactive();
LoadingScreen.MarkTipsNowAvailable();
}

public override void DoPatching(Harmony instance)
{
instance.Patch(
Expand Down

0 comments on commit 4077d8e

Please sign in to comment.