Skip to content

Commit

Permalink
Major overhaul to Timing and other fixes
Browse files Browse the repository at this point in the history
- Overhaul to timing methods to increase timing accuracies and fix issues related to pause spamming
- Fixed double splitting in HL2 Survivor
- Added an option to disable pausing timer when starting from game pause
- Code cleanups and refactoring
  • Loading branch information
thisis2838 committed Jan 1, 2022
1 parent b3b617f commit 6f77602
Show file tree
Hide file tree
Showing 52 changed files with 596 additions and 310 deletions.
58 changes: 58 additions & 0 deletions Extensions/Detour.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using LiveSplit.ComponentUtil;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LiveSplit.SourceSplit.Extensions
{
public class ByteReplacement
{
private byte[] _old;
private int _count;
private IntPtr _address;
Process _process;

public ByteReplacement(Process p, IntPtr address, int count)
{
_address = address;
_process = p;
_count = count;
_old = p.ReadBytes(_address, _count);
}

public void Replace(byte[] newBytes)
{
Trace.Assert(newBytes.Length == _count);
_process.WriteBytes(_address, newBytes);
}

public void Restore()
{
_process.WriteBytes(_address, _old);
}
}
public class Detour
{
private ByteReplacement _replaced;

public Detour(Process p, IntPtr from, IntPtr to, int preserve, byte[] newBytes, bool preserveAfter = false)
{
_replaced = new ByteReplacement(p, from, preserve);

p.WriteBytes(to + (preserveAfter ? newBytes.Length : 0), p.ReadBytes(from, preserve));
p.WriteJumpInstruction(from, to);
p.WriteBytes(to + (preserveAfter ? 0 : preserve), newBytes);

p.WriteJumpInstruction(to + preserve + newBytes.Length, from + preserve);
Debug.WriteLine($"Written detour from 0x{from.ToString("X")} to 0x{to.ToString("X")} [{newBytes.Length} bytes]");
}

public void Restore()
{
_replaced.Restore();
}
}
}
410 changes: 265 additions & 145 deletions GameMemory.cs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion GameSpecific/ApertureTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public override GameSupportResult OnUpdate(GameState state)
if (this.IsFirstMap)
{
// first tick player out of shower
if (state.PlayerPosition.Distance(_startPos) < 1.0f)
if (state.PlayerPosition.Current.Distance(_startPos) < 1.0f)
{
Debug.WriteLine("aperture tag start");
_onceFlag = true;
Expand Down
4 changes: 2 additions & 2 deletions GameSpecific/BMSRetail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ public override GameSupportResult OnUpdate(GameState state)
}
else if (_ebEndCommand.BValue && state.CurrentMap.ToLower() == _ebEndMap)
{
if (state.PlayerViewEntityIndex == _ebCamIndex && state.PrevPlayerViewEntityIndex == 1)
if (state.PlayerViewEntityIndex.Current == _ebCamIndex && state.PlayerViewEntityIndex.Old == 1)
return DefaultEnd("bms eb end");
}
else if ((_xenStartCommand.BValue || _xenSplitCommand.BValue) && state.CurrentMap.ToLower() == _xenStartMap)
{
if (state.PlayerViewEntityIndex == 1 && state.PrevPlayerViewEntityIndex == _xenCamIndex)
if (state.PlayerViewEntityIndex.Current == 1 && state.PlayerViewEntityIndex.Old == _xenCamIndex)
{
_onceFlag = true;
Debug.WriteLine("bms xen start");
Expand Down
12 changes: 6 additions & 6 deletions GameSpecific/GameSupport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,24 +145,24 @@ public virtual GameSupportResult OnUpdate(GameState state)
return GameSupportResult.DoNothing;

if (this.AutoStartType == AutoStart.Unfrozen
&& !state.PlayerFlags.HasFlag(FL.FROZEN)
&& state.PrevPlayerFlags.HasFlag(FL.FROZEN))
&& !state.PlayerFlags.Current.HasFlag(FL.FROZEN)
&& state.PlayerFlags.Old.HasFlag(FL.FROZEN))
{
Debug.WriteLine("FL_FROZEN removed from player");
_onceFlag = true;
return GameSupportResult.PlayerGainedControl;
}
else if (this.AutoStartType == AutoStart.ViewEntityChanged
&& state.PrevPlayerViewEntityIndex != GameState.ENT_INDEX_PLAYER
&& state.PlayerViewEntityIndex == GameState.ENT_INDEX_PLAYER)
&& state.PlayerViewEntityIndex.Old != GameState.ENT_INDEX_PLAYER
&& state.PlayerViewEntityIndex.Current == GameState.ENT_INDEX_PLAYER)
{
Debug.WriteLine("view entity changed to player");
_onceFlag = true;
return GameSupportResult.PlayerGainedControl;
}
else if (this.AutoStartType == AutoStart.ParentEntityChanged
&& state.PrevPlayerParentEntityHandle != -1
&& state.PlayerParentEntityHandle == -1)
&& state.PlayerParentEntityHandle.Old != -1
&& state.PlayerParentEntityHandle.Current == -1)
{
Debug.WriteLine("player no longer parented");
_onceFlag = true;
Expand Down
4 changes: 2 additions & 2 deletions GameSpecific/HDTF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ public override GameSupportResult OnUpdate(GameState state)
if (_onceFlag)
return GameSupportResult.DoNothing;

if (state.CurrentMap.ToLower() == "a0c0p1" && state.PlayerPosition.DistanceXY(_startPos) <= 3f)
if (state.CurrentMap.ToLower() == "a0c0p1" && state.PlayerPosition.Current.DistanceXY(_startPos) <= 3f)
{
bool ifIntroNotDeleted = File.Exists(state.GameProcess.ReadString(state.GameOffsets.GameDirPtr, 255) + "/media/a0b0c0s0.bik");
if (_tutResetFlag &&
(ifIntroNotDeleted && _isInCutscene.Current - _isInCutscene.Old == -1) ^
(!ifIntroNotDeleted && !_resetFlag && state.TickCount <= 1 && state.RawTickCount <= 150))
(!ifIntroNotDeleted && !_resetFlag && state.TickCount <= 1 && state.RawTickCount.Current <= 150))
{
Debug.WriteLine("hdtf start");
_onceFlag = true;
Expand Down
2 changes: 1 addition & 1 deletion GameSpecific/HL2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public override GameSupportResult OnUpdate(GameState state)
// "OnTrigger" "point_teleport_destination,Teleport,,0.1,-1"

// first tick player is moveable and on the train
if (state.PlayerPosition.DistanceXY(_startPos) <= 1.0)
if (state.PlayerPosition.Current.DistanceXY(_startPos) <= 1.0)
{
Debug.WriteLine("hl2 start");
_onceFlag = true;
Expand Down
4 changes: 2 additions & 2 deletions GameSpecific/HL2Ep2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public override GameSupportResult OnUpdate(GameState state)
{
// "OnTrigger4" "cvehicle.hangar,EnterVehicle,,0,1"

if (state.PlayerParentEntityHandle != -1
&& state.PrevPlayerParentEntityHandle == -1)
if (state.PlayerParentEntityHandle.Current != -1
&& state.PlayerParentEntityHandle.Old == -1)
{
Debug.WriteLine("ep2 end");
_onceFlag = true;
Expand Down
2 changes: 1 addition & 1 deletion GameSpecific/HL2Mods/HL2Mods_1187Ep1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public override GameSupportResult OnUpdate(GameState state)

if (IsFirstMap)
{
if (state.PrevPlayerViewEntityIndex == _startCamIndex && state.PlayerViewEntityIndex == 1)
if (state.PlayerViewEntityIndex.Old == _startCamIndex && state.PlayerViewEntityIndex.Current == 1)
{
Debug.WriteLine("1187ep1 start");
_onceFlag = true;
Expand Down
2 changes: 1 addition & 1 deletion GameSpecific/HL2Mods/HL2Mods_Crates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public override GameSupportResult OnUpdate(GameState state)
if (this.IsFirstMap)
{
_counterSkin.Update(state.GameProcess);
if (_counterSkin.Current == 10 && state.PlayerViewEntityIndex == _camIndex && state.PrevPlayerViewEntityIndex == 1)
if (_counterSkin.Current == 10 && state.PlayerViewEntityIndex.Current == _camIndex && state.PlayerViewEntityIndex.Old == 1)
{
_onceFlag = true;
Debug.WriteLine("toomanycrates end");
Expand Down
4 changes: 2 additions & 2 deletions GameSpecific/HL2Mods/HL2Mods_DaBaby.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public override GameSupportResult OnUpdate(GameState state)

if (_startCamIndex != -1)
{
if (state.PlayerViewEntityIndex == 1 && state.PrevPlayerViewEntityIndex == _startCamIndex)
if (state.PlayerViewEntityIndex.Current == 1 && state.PlayerViewEntityIndex.Old == _startCamIndex)
{
Debug.WriteLine("da baby start");
return GameSupportResult.PlayerGainedControl;
Expand All @@ -49,7 +49,7 @@ public override GameSupportResult OnUpdate(GameState state)

if (_endingCamIndex != -1)
{
if (state.PlayerViewEntityIndex == _endingCamIndex && state.PrevPlayerViewEntityIndex == 1)
if (state.PlayerViewEntityIndex.Current == _endingCamIndex && state.PlayerViewEntityIndex.Old == 1)
{
Debug.WriteLine("da baby end");
_onceFlag = true;
Expand Down
4 changes: 2 additions & 2 deletions GameSpecific/HL2Mods/HL2Mods_DayHard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public override GameSupportResult OnUpdate(GameState state)

if (this.IsFirstMap && _camIndex != -1)
{
if (state.PlayerViewEntityIndex == 1 &&
state.PrevPlayerViewEntityIndex == _camIndex)
if (state.PlayerViewEntityIndex.Current == 1 &&
state.PlayerViewEntityIndex.Old == _camIndex)
{
Debug.WriteLine("DayHard start");
_onceFlag = true;
Expand Down
2 changes: 1 addition & 1 deletion GameSpecific/HL2Mods/HL2Mods_DeeperDown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public override GameSupportResult OnUpdate(GameState state)

if (this.IsFirstMap)
{
if (state.PlayerViewEntityIndex == 1 && state.PrevPlayerViewEntityIndex == _camIndex)
if (state.PlayerViewEntityIndex.Current == 1 && state.PlayerViewEntityIndex.Old == _camIndex)
{
_onceFlag = true;
Debug.WriteLine("deeper down start");
Expand Down
4 changes: 2 additions & 2 deletions GameSpecific/HL2Mods/HL2Mods_Downfall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public override GameSupportResult OnUpdate(GameState state)

if (this.IsFirstMap)
{
if (state.PrevPlayerViewEntityIndex != GameState.ENT_INDEX_PLAYER
&& state.PlayerViewEntityIndex == GameState.ENT_INDEX_PLAYER)
if (state.PlayerViewEntityIndex.Old != GameState.ENT_INDEX_PLAYER
&& state.PlayerViewEntityIndex.Current == GameState.ENT_INDEX_PLAYER)
{
Debug.WriteLine("downfall start");
_onceFlag = true;
Expand Down
8 changes: 4 additions & 4 deletions GameSpecific/HL2Mods/HL2Mods_Exit2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public override GameSupportResult OnUpdate(GameState state)

if (this.IsFirstMap && this._camIndex != -1)
{
if (state.PlayerViewEntityIndex == 1 &&
state.PrevPlayerViewEntityIndex == _camIndex)
if (state.PlayerViewEntityIndex.Current == 1 &&
state.PlayerViewEntityIndex.Old == _camIndex)
{
Debug.WriteLine("exit2 start");
_onceFlag = true;
Expand All @@ -50,8 +50,8 @@ public override GameSupportResult OnUpdate(GameState state)
}
else if (this.IsLastMap)
{
if (state.PlayerViewEntityIndex == _camIndex &&
state.PrevPlayerViewEntityIndex == 1)
if (state.PlayerViewEntityIndex.Current == _camIndex &&
state.PlayerViewEntityIndex.Old == 1)
{
Debug.WriteLine("exit2 end");
_onceFlag = true;
Expand Down
4 changes: 2 additions & 2 deletions GameSpecific/HL2Mods/HL2Mods_ExperimentalFuel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public override GameSupportResult OnUpdate(GameState state)
var newMote = state.GetEntInfoByIndex(_dustmoteIndex);
var newBrush = state.GetEntInfoByIndex(_blockBrushIndex);

if (state.PlayerPosition.DistanceXY(new Vector3f(7784.5f, 7284f, -15107f)) >= 2
&& state.PrevPlayerPosition.DistanceXY(new Vector3f(7784.5f, 7284f, -15107f)) < 2
if (state.PlayerPosition.Current.DistanceXY(new Vector3f(7784.5f, 7284f, -15107f)) >= 2
&& state.PlayerPosition.Old.DistanceXY(new Vector3f(7784.5f, 7284f, -15107f)) < 2
&& newBrush.EntityPtr == IntPtr.Zero && !_resetFlag)
{
Debug.WriteLine("exp fuel start");
Expand Down
2 changes: 1 addition & 1 deletion GameSpecific/HL2Mods/HL2Mods_Freakman2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public override GameSupportResult OnUpdate(GameState state)

else if (this.IsLastMap && _camIndex != -1)
{
if (state.PrevPlayerViewEntityIndex != _camIndex && state.PlayerViewEntityIndex == _camIndex)
if (state.PlayerViewEntityIndex.Old != _camIndex && state.PlayerViewEntityIndex.Current == _camIndex)
{
_onceFlag = true;
Debug.WriteLine("freakman2 end");
Expand Down
4 changes: 2 additions & 2 deletions GameSpecific/HL2Mods/HL2Mods_GetALife.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public override GameSupportResult OnUpdate(GameState state)

if (this.IsFirstMap)
{
if (state.PlayerViewEntityIndex == 1 && state.PrevPlayerViewEntityIndex == _startCamIndex)
if (state.PlayerViewEntityIndex.Current == 1 && state.PlayerViewEntityIndex.Old == _startCamIndex)
{
_onceFlag = true;
Debug.WriteLine("get a life start");
Expand All @@ -53,7 +53,7 @@ public override GameSupportResult OnUpdate(GameState state)
}
else if (this.IsLastMap)
{
if (state.PrevPlayerViewEntityIndex == 1 && state.PlayerViewEntityIndex == _endCamIndex)
if (state.PlayerViewEntityIndex.Old == 1 && state.PlayerViewEntityIndex.Current == _endCamIndex)
{
_onceFlag = true;
Debug.WriteLine("get a life end");
Expand Down
4 changes: 2 additions & 2 deletions GameSpecific/HL2Mods/HL2Mods_Grey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public override GameSupportResult OnUpdate(GameState state)

if (this.IsFirstMap)
{
if (state.PrevPlayerViewEntityIndex == _startcamIndex && state.PlayerViewEntityIndex == 1)
if (state.PlayerViewEntityIndex.Old == _startcamIndex && state.PlayerViewEntityIndex.Current == 1)
{
_onceFlag = true;
Debug.WriteLine("grey start");
Expand All @@ -54,7 +54,7 @@ public override GameSupportResult OnUpdate(GameState state)
}
else if (this.IsLastMap)
{
if (state.PrevPlayerViewEntityIndex == 1 && state.PlayerViewEntityIndex == _endcamIndex)
if (state.PlayerViewEntityIndex.Old == 1 && state.PlayerViewEntityIndex.Current == _endcamIndex)
{
_onceFlag = true;
Debug.WriteLine("grey end");
Expand Down
4 changes: 2 additions & 2 deletions GameSpecific/HL2Mods/HL2Mods_Hangover.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public override GameSupportResult OnUpdate(GameState state)

if (this.IsFirstMap)
{
if (state.PrevPlayerViewEntityIndex == _startCamIndex &&
state. PlayerViewEntityIndex == 1)
if (state.PlayerViewEntityIndex.Old == _startCamIndex &&
state.PlayerViewEntityIndex.Current == 1)
{
_onceFlag = true;
Debug.WriteLine("hangover start");
Expand Down
2 changes: 1 addition & 1 deletion GameSpecific/HL2Mods/HL2Mods_KillTheMonk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public override GameSupportResult OnUpdate(GameState state)

if (this.IsFirstMap)
{
if (state.PrevPlayerViewEntityIndex == _camIndex && state.PlayerViewEntityIndex == 1)
if (state.PlayerViewEntityIndex.Old == _camIndex && state.PlayerViewEntityIndex.Current == 1)
{
_onceFlag = true;
Debug.WriteLine("kill the monk start");
Expand Down
2 changes: 1 addition & 1 deletion GameSpecific/HL2Mods/HL2Mods_MImp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public override GameSupportResult OnUpdate(GameState state)
}
else if (this.IsLastMap && _camIndex != -1)
{
if (state.PlayerViewEntityIndex == _camIndex && state.PrevPlayerViewEntityIndex != _camIndex)
if (state.PlayerViewEntityIndex.Current == _camIndex && state.PlayerViewEntityIndex.Old != _camIndex)
{
Debug.WriteLine("mimp end");
_onceFlag = true;
Expand Down
10 changes: 5 additions & 5 deletions GameSpecific/HL2Mods/HL2Mods_Others.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class HL2Mods_Misc : GameSupport

public override void OnGenericUpdate(GameState state)
{
if (IsLastMap && state.HostState == HostState.GameShutdown)
if (IsLastMap && state.HostState.Current == HostState.GameShutdown)
OnUpdate(state);
}

Expand Down Expand Up @@ -175,7 +175,7 @@ public override GameSupportResult OnUpdate(GameState state)
return GameSupportResult.DoNothing;

// todo: probably should use the helicopter's position?
if (IsLastMap && state.PlayerPosition.Distance(_endSector) <= 300f)
if (IsLastMap && state.PlayerPosition.Current.Distance(_endSector) <= 300f)
{
float splitTime = state.FindOutputFireTime("game_end", 10);
_splitTime = (splitTime == 0f) ? _splitTime : splitTime;
Expand Down Expand Up @@ -207,7 +207,7 @@ public override GameSupportResult OnUpdate(GameState state)
if (_onceFlag)
return GameSupportResult.DoNothing;

if (IsLastMap && state.PlayerViewEntityIndex != GameState.ENT_INDEX_PLAYER)
if (IsLastMap && state.PlayerViewEntityIndex.Current != GameState.ENT_INDEX_PLAYER)
{
float splitTime = state.FindOutputFireTime("clientcommand", 8);
_splitTime = (splitTime == 0f) ? _splitTime : splitTime;
Expand Down Expand Up @@ -442,8 +442,8 @@ public override GameSupportResult OnUpdate(GameState state)

if (this.IsLastMap && _endCameraIndex != -1)
{
if (state.PrevPlayerViewEntityIndex == 1 &&
state.PlayerViewEntityIndex == _endCameraIndex)
if (state.PlayerViewEntityIndex.Old == 1 &&
state.PlayerViewEntityIndex.Current == _endCameraIndex)
{
_onceFlag = true;
Debug.WriteLine("school_adventures end");
Expand Down
4 changes: 2 additions & 2 deletions GameSpecific/HL2Mods/HL2Mods_Precursor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public override GameSupportResult OnUpdate(GameState state)

if (this.IsFirstMap)
{
if (state.PrevPlayerViewEntityIndex == _startCamIndex && state.PlayerViewEntityIndex == 1)
if (state.PlayerViewEntityIndex.Old == _startCamIndex && state.PlayerViewEntityIndex.Current == 1)
{
_onceFlag = true;
Debug.WriteLine("precursor start");
Expand All @@ -54,7 +54,7 @@ public override GameSupportResult OnUpdate(GameState state)
}
else if (this.IsLastMap)
{
if (state.PrevPlayerViewEntityIndex == 1 && state.PlayerViewEntityIndex == _endCamIndex)
if (state.PlayerViewEntityIndex.Old == 1 && state.PlayerViewEntityIndex.Current == _endCamIndex)
{
_onceFlag = true;
Debug.WriteLine("precursor end");
Expand Down
4 changes: 2 additions & 2 deletions GameSpecific/HL2Mods/HL2Mods_Ptsd1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public override GameSupportResult OnUpdate(GameState state)

if (this.IsFirstMap && _camIndex != -1)
{
if (state.PrevPlayerViewEntityIndex == _camIndex
&& state.PlayerViewEntityIndex == GameState.ENT_INDEX_PLAYER)
if (state.PlayerViewEntityIndex.Old == _camIndex
&& state.PlayerViewEntityIndex.Current == GameState.ENT_INDEX_PLAYER)
{
Debug.WriteLine("ptsd start");
_onceFlag = true;
Expand Down
2 changes: 1 addition & 1 deletion GameSpecific/HL2Mods/HL2Mods_SnipersEp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public override GameSupportResult OnUpdate(GameState state)

if (this.IsFirstMap)
{
if (state.PrevPlayerPosition.BitEqualsXY(_startPos) && !state.PlayerPosition.BitEqualsXY(_startPos) && !_resetFlag)
if (state.PlayerPosition.Old.BitEqualsXY(_startPos) && !state.PlayerPosition.Current.BitEqualsXY(_startPos) && !_resetFlag)
{
_resetFlag = true;
return GameSupportResult.PlayerGainedControl;
Expand Down
Loading

0 comments on commit 6f77602

Please sign in to comment.