Skip to content

Commit

Permalink
Small tweaks & support for timing stages
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Byass committed Feb 18, 2020
1 parent 1293f77 commit fc4a58e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 10 deletions.
1 change: 1 addition & 0 deletions Source/BetterLoading.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<Compile Include="EnumLoadingStage.cs" />
<Compile Include="LongEventHandlerMirror.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StageTimingData.cs" />
<Compile Include="Stage\InitialLoad\3StageApplyPatches.cs" />
<Compile Include="Stage\InitialLoad\5StageConstructDefs.cs" />
<Compile Include="Stage\InitialLoad\0StageInitMods.cs" />
Expand Down
14 changes: 12 additions & 2 deletions Source/LoadingScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public LoadingScreen()
{
Instance = this;
_currentStage.BecomeActive();
StageTimingData.ExecutedStages.Add(new StageTimingData
{
start = DateTime.Now,
stage = _currentStage
});
}

private void Awake()
Expand Down Expand Up @@ -140,11 +145,16 @@ public void OnGUI()
//Move to next stage
Log.Message("BetterLoading: Finished stage " + _currentStage.GetStageName() + " at " + DateTime.Now.ToLongTimeString());
_currentStage.BecomeInactive();

StageTimingData.ExecutedStages.Last().end = DateTime.Now;

_currentStage = currentList[idx + 1];
_currentStage.BecomeActive();
Log.Message("BetterLoading: Starting stage " + _currentStage.GetStageName());

StageTimingData.ExecutedStages.Add(new StageTimingData
{
start = DateTime.Now,
stage = _currentStage
});
idx++;
}

Expand Down
13 changes: 6 additions & 7 deletions Source/Stage/InitialLoad/3StageApplyPatches.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using Harmony;
using Verse;
Expand All @@ -9,13 +8,13 @@ namespace BetterLoading.Stage.InitialLoad
public class StageApplyPatches : LoadingStage
{
private static List<ModContentPack> _modList;
private static int _currentModNum = 0;

private static ModContentPack? _currentMod;
private static int _currentModNum = -1;

private static bool _loadingPatches;
private static int _numPatches = -1;
private static int _currentPatch;
private static bool _loadingPatches;


public StageApplyPatches(HarmonyInstance instance) : base(instance)
{
_modList = LoadedModManager.RunningMods.ToList();
Expand All @@ -24,7 +23,7 @@ public StageApplyPatches(HarmonyInstance instance) : base(instance)
public override void BecomeInactive()
{
_currentMod = null;
_currentModNum = 0;
_currentModNum = -1;
}

public override string GetStageName()
Expand Down
15 changes: 15 additions & 0 deletions Source/StageTimingData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using BetterLoading.Stage;

namespace BetterLoading
{
public class StageTimingData
{
internal static readonly List<StageTimingData> ExecutedStages = new List<StageTimingData>();

public DateTime start;
public DateTime end;
public LoadingStage stage;
}
}
4 changes: 3 additions & 1 deletion Source/ToExecuteWhenFinishedHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace BetterLoading
{
public class ToExecuteWhenFinishedHandler
{
private const float TARGET_FPS_DURING_LOAD = 30;
private const float MAX_FRAME_DURATION = 1f / TARGET_FPS_DURING_LOAD * 10_000f;
private static long lastEnd;

public static IEnumerator ExecuteToExecuteWhenFinishedTheGoodVersion(List<Action> toExecuteWhenFinished, bool skipLast, Action<Action> actionStartCallback, Action taskFinishedCallback, Action completeCallback)
Expand Down Expand Up @@ -51,7 +53,7 @@ public static IEnumerator ExecuteToExecuteWhenFinishedTheGoodVersion(List<Action
index++;
if(index >= toExecuteWhenFinished.Count)
break;
} while (DateTime.Now.Ticks - lastEnd < 160); //Target 60fps
} while (DateTime.Now.Ticks - lastEnd < MAX_FRAME_DURATION); //Target 60fps

yield return null;
lastEnd = DateTime.Now.Ticks;
Expand Down

0 comments on commit fc4a58e

Please sign in to comment.