Skip to content

Commit

Permalink
fix LogReader resetting
Browse files Browse the repository at this point in the history
- other things would be reset before all logreaders stopped reading, causing them to be updated with mid-game data.
- this caused the new hero detection to crash on resets because hero lines were read after entites were cleared.
  • Loading branch information
Alexander Zeier committed Dec 9, 2015
1 parent 39df2c3 commit 24a99eb
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 30 deletions.
22 changes: 17 additions & 5 deletions Hearthstone Deck Tracker/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,7 @@ private static async void UpdateOverlayAsync()
Logger.WriteLine("Exited game", "UpdateOverlayLoop");
Game.CurrentRegion = Region.UNKNOWN;
Logger.WriteLine("Reset region", "UpdateOverlayLoop");
//HsLogReaderV2.Instance.ClearLog();
Game.Reset();
if (DeckList.Instance.ActiveDeck != null)
Game.SetPremadeDeck((Deck) DeckList.Instance.ActiveDeck.Clone());
await LogReaderManager.Restart();
await Reset();

MainWindow.BtnStartHearthstone.Visibility = Visibility.Visible;
TrayIcon.NotifyIcon.ContextMenu.MenuItems[useNoDeckMenuItem].Visible = true;
Expand All @@ -220,6 +216,22 @@ private static async void UpdateOverlayAsync()
CanShutdown = true;
}

public static async Task Reset()
{
var stoppedReader = await LogReaderManager.Stop();
Game.Reset();
if(DeckList.Instance.ActiveDeck != null)
{
Game.SetPremadeDeck((Deck)DeckList.Instance.ActiveDeck.Clone());
MainWindow.UpdateMenuItemVisibility();
}
if(stoppedReader)
LogReaderManager.Restart();
Overlay.Update(false);
Overlay.UpdatePlayerCards();
Windows.PlayerWindow.UpdatePlayerCards();
}


public static class Windows
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,7 @@ private async void CheckboxRemoveCards_Checked(object sender, RoutedEventArgs e)
return;
Config.Instance.RemoveCardsFromDeck = true;
SaveConfig(false);
_game.Reset();
if(DeckList.Instance.ActiveDeck != null)
_game.SetPremadeDeck((Deck)DeckList.Instance.ActiveDeck.Clone());
await LogReaderManager.Restart();
await Core.Reset();
Core.Overlay.Update(true);
}

Expand All @@ -119,10 +116,7 @@ private async void CheckboxRemoveCards_Unchecked(object sender, RoutedEventArgs
return;
Config.Instance.RemoveCardsFromDeck = false;
SaveConfig(false);
_game.Reset();
if(DeckList.Instance.ActiveDeck != null)
_game.SetPremadeDeck((Deck)DeckList.Instance.ActiveDeck.Clone());
await LogReaderManager.Restart();
await Core.Reset();
Core.Overlay.Update(true);
}

Expand Down
1 change: 1 addition & 0 deletions Hearthstone Deck Tracker/Hearthstone/GameV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public void Reset(bool resetStats = true)
{
Helper.UpdatePlayerCards();
Helper.UpdateOpponentCards();
Core.MainWindow.NeedToIncorrectDeckMessage = false;
}
}

Expand Down
1 change: 1 addition & 0 deletions Hearthstone Deck Tracker/LogReader/HsGameState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public void Reset()
JoustReveals = 0;
KnownCardIds.Clear();
LastGameStart = DateTime.Now;
WaitForController = null;
}

public void ProposeKeyPoint(KeyPointType type, int id, ActivePlayer player)
Expand Down
19 changes: 14 additions & 5 deletions Hearthstone Deck Tracker/LogReader/LogReaderManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,30 @@ public static int GetTurnNumber()
return _gameState.GetTurnNumber();
}

public static async Task Stop()
public static async Task<bool> Stop()
{
if(!_running)
return;
{
Logger.WriteLine("LogReaders could not be stopped, stop already in progress.", "LogReaderManager");
return false;
}
_stop = true;
while(_running)
await Task.Delay(50);
await Task.WhenAll(LogReaders.Select(x => x.Stop()));
Logger.WriteLine("Stopped LogReaders.", "LogReaderManager");
return true;
}

public static async Task Restart()
/// <summary>
/// LogReaderManager.Stop needs to be called first!
/// These can not happen in one call because other things need to be reset between stopping and restarting.
/// </summary>
public static void Restart()
{
if(!_running)
if(_running)
return;
await Stop();
Logger.WriteLine("Restarting LogReaders.", "LogReaderManager");
_startingPoint = GetStartingPoint();
_gameState.Reset();
_game.GameTime.TimedTasks.Clear();
Expand Down
28 changes: 16 additions & 12 deletions Hearthstone Deck Tracker/Windows/MainWindow/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,12 @@ public partial class MainWindow : INotifyPropertyChanged

public async void UseDeck(Deck selected)
{
Core.Game.Reset();

if(selected != null)
{
DeckList.Instance.ActiveDeck = selected;
Core.Game.SetPremadeDeck((Deck)selected.Clone());
UpdateMenuItemVisibility();
}
//needs to be true for automatic deck detection to work
await LogReaderManager.Restart();
Core.Overlay.Update(false);
Core.Overlay.UpdatePlayerCards();
Core.Windows.PlayerWindow.UpdatePlayerCards();
await Core.Reset();
}

private void UpdateMenuItemVisibility()
internal void UpdateMenuItemVisibility()
{
var deck = DeckPickerList.SelectedDecks.FirstOrDefault();
if(deck == null)
Expand Down Expand Up @@ -812,6 +802,12 @@ public async void ShowIncorrectDeckMessage()
}
await Task.Delay(1000);

if(!NeedToIncorrectDeckMessage)
{
IsShowingIncorrectDeckMessage = false;
return;
}

var decks =
DeckList.Instance.Decks.Where(
d =>
Expand All @@ -822,6 +818,14 @@ public async void ShowIncorrectDeckMessage()

Logger.WriteLine(decks.Count + " possible decks found.", "IncorrectDeckMessage");
Core.Game.NoMatchingDeck = decks.Count == 0;

if(decks.Any(x => x == DeckList.Instance.ActiveDeck))
{
Logger.WriteLine("Correct deck already selected.", "IncorrectDeckMessage");
IsShowingIncorrectDeckMessage = false;
NeedToIncorrectDeckMessage = false;
return;
}

if(decks.Count == 1 && Config.Instance.AutoSelectDetectedDeck)
{
Expand Down

0 comments on commit 24a99eb

Please sign in to comment.