Skip to content

Commit

Permalink
Cache loading tips in a file in the user's config directory so they c…
Browse files Browse the repository at this point in the history
…an be shown before loading stage 7 on launches after the first.
  • Loading branch information
Sam Byass committed Nov 26, 2020
1 parent c32e004 commit 60af55e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
Binary file modified 1.1-1.2/Assemblies/BetterLoading.dll
Binary file not shown.
22 changes: 19 additions & 3 deletions Source/LoadingScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using BetterLoading.Stage.InitialLoad;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
using Verse;
Expand All @@ -12,9 +13,11 @@ public sealed class LoadingScreen : MonoBehaviour
{
public static LoadingScreen Instance { get; private set; }

private static string _cachedLoadingTipsPath = Path.Combine(GenFilePaths.ConfigFolderPath, "BetterLoading_Cached_Tips");

private static bool _tipsAvailable;

private static List<string>? _tips;
private static List<string> _tips = File.Exists(_cachedLoadingTipsPath) ? File.ReadAllText(_cachedLoadingTipsPath).Split('\0').ToList() : new List<string>();
private static string? _currentTip;
private static long _timeLastTipShown;
private const int _ticksPerTip = 5 * 10_000_000; //3 seconds
Expand Down Expand Up @@ -114,6 +117,7 @@ internal static T GetStageInstance<T>() where T: LoadingStage
public void Awake()
{
Log.Message("[BetterLoading] Injected into main UI.");
_tipsAvailable = _tips.Count > 0;
}

private void DrawBG()
Expand Down Expand Up @@ -325,8 +329,6 @@ public void OnGUI()
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.
Expand Down Expand Up @@ -363,6 +365,11 @@ public void OnGUI()
}
}

private static List<string> LoadGameplayTips()
{
return DefDatabase<TipSetDef>.AllDefsListForReading.SelectMany(set => set.tips).InRandomOrder().ToList();
}

private void DrawSaveFileLoad()
{
//Draw window
Expand Down Expand Up @@ -645,6 +652,15 @@ private void DrawSaveFileLoad()
public static void MarkTipsNowAvailable()
{
Log.Message("[BetterLoading] Tips should now be available. Showing...");

var tips = LoadGameplayTips();
var cachedTips = File.Exists(_cachedLoadingTipsPath) ? File.ReadAllText(_cachedLoadingTipsPath).Split('\0').ToList() : new List<string>();
if (!tips.SequenceEqual(cachedTips))
{
File.WriteAllText(_cachedLoadingTipsPath, string.Join("\0", tips));
_tips = tips;
}

_tipsAvailable = true;
}
}
Expand Down

0 comments on commit 60af55e

Please sign in to comment.