Skip to content

Commit

Permalink
Merge pull request #31 from ewrogers/fix-autosave
Browse files Browse the repository at this point in the history
Fix autosave
  • Loading branch information
ewrogers authored Jun 30, 2023
2 parents fa03d4f + 1ed878f commit eeedd11
Show file tree
Hide file tree
Showing 37 changed files with 694 additions and 925 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ All notable changes to this library will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [4.10.2] - 2023-06-29

### Removed

- Lots of old code and things that were not being used

### Fixed

- Auto-save and load reliability
- Some UI bugs on auto-load
- Lots of performance and under the hood optimizations

## [4.10.1] - 2023-06-27

### Added
Expand Down
70 changes: 70 additions & 0 deletions SleepHunter/Common/UpdatableObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;

namespace SleepHunter.Common
{
public abstract class UpdatableObject : ObservableObject, IDisposable
{
protected bool isDisposed;

public event EventHandler Updated;

public void Update()
{
CheckIfDisposed();

OnUpdate();
RaiseUpdated();
}

public bool TryUpdate()
{
CheckIfDisposed();

try
{
Update();
return true;
}
catch
{
return false;
}
}


~UpdatableObject() => Dispose(false);

public void RaiseUpdated()
{
CheckIfDisposed();
Updated?.Invoke(this, EventArgs.Empty);
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool isDisposing)
{
if (isDisposed)
return;

if (isDisposing)
{

}

isDisposed = true;
}

protected abstract void OnUpdate();

protected void CheckIfDisposed()
{
if (isDisposed)
throw new ObjectDisposedException(GetType().Name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace SleepHunter.Extensions
{
public static class BinaryReaderExtender
public static class BinaryReaderExtensions
{
public static string ReadFixedString(this BinaryReader reader, int length)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

namespace SleepHunter.Extensions
{
public static class CharacterExtender
public static class CharacterExtensions
{
public static bool IsValidHexDigit(this char c, bool allowControl = true)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace SleepHunter.Extensions
{
public static class ControlExtender
public static class ControlExtensions
{
public static T FindItem<T>(this ItemsControl control, Func<T, bool> selector) where T : class
{
Expand Down
13 changes: 13 additions & 0 deletions SleepHunter/Extensions/DispatcherExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Windows.Threading;
using SleepHunter.Threading;

namespace SleepHunter.Extensions
{
public static class DispatcherExtensions
{
public static UIThreadAwaitable SwitchToUIThread(this Dispatcher dispatcher)
{
return new UIThreadAwaitable(dispatcher);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace SleepHunter.Extensions
{
public static class StringExtender
public static class StringExtensions
{
public static string StripNumbers(this string text)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

namespace SleepHunter.Extensions
{
public static class TimeSpanExtender
public static class TimeSpanExtensions
{
static readonly Regex TimeSpanSecondsRegex = new(@"\s*(?<seconds>-?[0-9]*\.?[0-9]{1,})\s*s\s*");
static readonly Regex TimeSpanMinutesRegex = new(@"\s*(?<minutes>-?[0-9]*\.?[0-9]{1,})\s*m\s*");
static readonly Regex TimeSpanHoursRegex = new(@"\s*(?<hours>-?[0-9]*\.?[0-9]{1,})\s*h\s*");
static readonly Regex TimeSpanDaysRegex = new(@"\s*(?<days>-?[0-9]*\.?[0-9]{1,})\s*d\s*");
static readonly Regex TimeSpanSecondsRegex = new(@"\s*(?<seconds>-?[0-9]*\.?[0-9]{1,})\s*s\s*", RegexOptions.Compiled);
static readonly Regex TimeSpanMinutesRegex = new(@"\s*(?<minutes>-?[0-9]*\.?[0-9]{1,})\s*m\s*", RegexOptions.Compiled);
static readonly Regex TimeSpanHoursRegex = new(@"\s*(?<hours>-?[0-9]*\.?[0-9]{1,})\s*h\s*", RegexOptions.Compiled);
static readonly Regex TimeSpanDaysRegex = new(@"\s*(?<days>-?[0-9]*\.?[0-9]{1,})\s*d\s*", RegexOptions.Compiled);

public static string ToFractionalEnglish(this TimeSpan timeSpan, bool useShortNotation = false, string format = null)
=> ToEnglish(timeSpan, useShortNotation, false, true, format);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace SleepHunter.Extensions
{
public static class VersionExtender
public static class VersionExtensions
{
public static bool IsNewerThan(this Version current, Version otherVersion) => current.CompareTo(otherVersion) > 0;

Expand Down
88 changes: 0 additions & 88 deletions SleepHunter/Extensions/WindowExtender.cs

This file was deleted.

43 changes: 43 additions & 0 deletions SleepHunter/Extensions/WindowExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using SleepHunter.Views;
using System.Windows;

namespace SleepHunter.Extensions
{
public static class WindowExtensions
{
public static bool? ShowMessageBox(this Window owner, string windowTitle, string messageText, string subText = null, MessageBoxButton buttons = MessageBoxButton.OK, int width = 420, int height = 280)
{
if (owner == null)
throw new ArgumentNullException(nameof(owner));

var messageBox = new MessageBoxWindow
{
Title = windowTitle ?? string.Empty,
Width = width,
Height = height,
MessageText = messageText ?? string.Empty,
SubText = subText ?? string.Empty
};

if (buttons == MessageBoxButton.OK)
{
messageBox.CancelButtonColumnWidth = new GridLength(1, GridUnitType.Auto);
messageBox.CancelButtonVisibility = Visibility.Collapsed;
}

if (buttons.HasFlag(MessageBoxButton.YesNo))
{
messageBox.OkButtonText = "_Yes";
messageBox.CancelButtonText = "_No";
}

if (!owner.IsLoaded)
owner.Show();

messageBox.Owner = owner;

return messageBox.ShowDialog();
}
}
}
2 changes: 1 addition & 1 deletion SleepHunter/IO/Process/MemoryVariableExtender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static long DereferencePointer(long address, BinaryReader reader)
return reference;
}

public static bool TryDeferenceValue(this MemoryVariable variable, BinaryReader reader, out long address, bool isStringType = false)
public static bool TryDereferenceValue(this MemoryVariable variable, BinaryReader reader, out long address, bool isStringType = false)
{
address = DereferenceValue(variable, reader, isStringType);
return address != 0;
Expand Down
1 change: 1 addition & 0 deletions SleepHunter/IO/Process/ProcessManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text;

using SleepHunter.Win32;
using SleepHunter.Models;

namespace SleepHunter.IO.Process
{
Expand Down
20 changes: 2 additions & 18 deletions SleepHunter/Macro/MacroState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,6 @@ protected virtual void StartMacro(object state = null)
try
{
UpdateClientMacroStatus();

if (!CheckMap())
cancelSource.Cancel();

if (cancelSource.Token.IsCancellationRequested)
break;
Expand All @@ -143,13 +140,13 @@ protected virtual void StartMacro(object state = null)
}
else
{
Thread.Sleep(100);
Thread.Sleep(16);
}
}
finally
{
if (!cancelSource.IsCancellationRequested)
Thread.Sleep(50);
Thread.Sleep(16);

if (cancelSource.IsCancellationRequested)
Stop();
Expand Down Expand Up @@ -183,15 +180,6 @@ protected void UpdateClientMacroStatus()

protected abstract void MacroLoop(object argument);

protected virtual bool CheckMap()
{
if (client == null)
return false;

client.Update(PlayerFieldFlags.Location);
return true;
}

protected virtual bool CancelTask(bool waitForTask = false)
{
if (cancelSource != null)
Expand Down Expand Up @@ -259,8 +247,6 @@ protected virtual void TakeAction(MacroAction action)

protected virtual void SaveKnownState()
{
client.Update(PlayerFieldFlags.Location);

lastKnownMapName = client.Location.MapName;
lastKnownMapNumber = client.Location.MapNumber;
lastKnownXCoordinate = client.Location.X;
Expand All @@ -269,8 +255,6 @@ protected virtual void SaveKnownState()

protected virtual void CheckKnownState(bool saveStateAfterCheck = true)
{
client.Update(PlayerFieldFlags.Location);

if (!string.Equals(client.Location.MapName, lastKnownMapName) ||
client.Location.MapNumber != lastKnownMapNumber)
{
Expand Down
Loading

0 comments on commit eeedd11

Please sign in to comment.