private async void ShowNewUpdateMessage(Version newVersion = null) { const string releaseDownloadUrl = @"https://github.com/Epix37/Hearthstone-Deck-Tracker/releases"; var settings = new MetroDialogSettings { AffirmativeButtonText = "Download", NegativeButtonText = "Not now" }; var version = newVersion ?? NewVersion; if (version == null) { return; } try { var newVersionString = string.Format("{0}.{1}.{2}", version.Major, version.Minor, version.Build); var result = await this.ShowMessageAsync("New Update available!", "Press \"Download\" to automatically download.", MessageDialogStyle.AffirmativeAndNegative, settings); if (result == MessageDialogResult.Affirmative) { //recheck, in case there was no immediate response to the dialog if ((DateTime.Now - _lastUpdateCheck) > new TimeSpan(0, 10, 0)) { Helper.CheckForUpdates(out version); if (version != null) { newVersionString = string.Format("{0}.{1}.{2}", version.Major, version.Minor, version.Build); } } try { Process.Start("Updater.exe", string.Format("{0} {1}", Process.GetCurrentProcess().Id, newVersionString)); Application.Current.Shutdown(); } catch { Logger.WriteLine("Error starting updater"); Process.Start(releaseDownloadUrl); } } else { _tempUpdateCheckDisabled = true; } } catch (Exception e) { Logger.WriteLine("Error showing new update message\n" + e.Message); } }
public MainWindow() { // Set working directory to path of executable Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory); InitializeComponent(); Trace.Listeners.Add(new TextBoxTraceListener(Options.TextBoxLog)); EnableMenuItems(false); try { if (File.Exists("Updater_new.exe")) { if (File.Exists("Updater.exe")) { File.Delete("Updater.exe"); } File.Move("Updater_new.exe", "Updater.exe"); } } catch { Logger.WriteLine("Error updating updater"); } Helper.MainWindow = this; /*_configPath =*/ Config.Load(); HsLogReader.Create(); var configVersion = string.IsNullOrEmpty(Config.Instance.CreatedByVersion) ? null : new Version(Config.Instance.CreatedByVersion); Version currentVersion; if (Config.Instance.CheckForUpdates) { currentVersion = Helper.CheckForUpdates(out NewVersion); _lastUpdateCheck = DateTime.Now; } else { currentVersion = Helper.GetCurrentVersion(); } var versionString = string.Empty; if (currentVersion != null) { versionString = string.Format("{0}.{1}.{2}", currentVersion.Major, currentVersion.Minor, currentVersion.Build); Help.TxtblockVersion.Text = "Version: " + versionString; // Assign current version to the config instance so that it will be saved when the config // is rewritten to disk, thereby telling us what version of the application created it Config.Instance.CreatedByVersion = currentVersion.ToString(); } ConvertLegacyConfig(currentVersion, configVersion); if (Config.Instance.SelectedTags.Count == 0) { Config.Instance.SelectedTags.Add("All"); } _foundHsDirectory = FindHearthstoneDir(); if (_foundHsDirectory) { _updatedLogConfig = UpdateLogConfigFile(); } //hearthstone, loads db etc - needs to be loaded before playerdecks, since cards are only saved as ids now Game.Reset(); _decksPath = Config.Instance.DataDir + "PlayerDecks.xml"; SetupDeckListFile(); try { DeckList = XmlManager <Decks> .Load(_decksPath); } catch (Exception e) { MessageBox.Show( e.Message + "\n\n" + e.InnerException + "\n\n If you don't know how to fix this, please delete " + _decksPath + " (this will cause you to lose your decks).", "Error loading PlayerDecks.xml"); Application.Current.Shutdown(); } foreach (var deck in DeckList.DecksList) { DeckPickerList.AddDeck(deck); } SetupDefaultDeckStatsFile(); DefaultDeckStats.Load(); SetupDeckStatsFile(); DeckStatsList.Load(); _notifyIcon = new NotifyIcon { Icon = new Icon(@"Images/HearthstoneDeckTracker16.ico"), Visible = true, ContextMenu = new ContextMenu(), Text = "Hearthstone Deck Tracker v" + versionString }; _notifyIcon.ContextMenu.MenuItems.Add("Use no deck", (sender, args) => DeselectDeck()); _notifyIcon.ContextMenu.MenuItems.Add(new MenuItem("Autoselect deck") { MenuItems = { new MenuItem("On", (sender, args) => AutoDeckDetection(true)), new MenuItem("Off", (sender, args) => AutoDeckDetection(false)) } }); _notifyIcon.ContextMenu.MenuItems.Add(new MenuItem("Class cards first") { MenuItems = { new MenuItem("Yes", (sender, args) => SortClassCardsFirst(true)), new MenuItem("No", (sender, args) => SortClassCardsFirst(false)) } }); _notifyIcon.ContextMenu.MenuItems.Add("Show", (sender, args) => ActivateWindow()); _notifyIcon.ContextMenu.MenuItems.Add("Exit", (sender, args) => Close()); _notifyIcon.MouseClick += (sender, args) => { if (args.Button == MouseButtons.Left) { ActivateWindow(); } }; //create overlay Overlay = new OverlayWindow { Topmost = true }; PlayerWindow = new PlayerWindow(Config.Instance, Game.IsUsingPremade ? Game.PlayerDeck : Game.PlayerDrawn); OpponentWindow = new OpponentWindow(Config.Instance, Game.OpponentCards); TimerWindow = new TimerWindow(Config.Instance); StatsWindow = new StatsWindow(); if (Config.Instance.PlayerWindowOnStart) { PlayerWindow.Show(); } if (Config.Instance.OpponentWindowOnStart) { OpponentWindow.Show(); } if (Config.Instance.TimerWindowOnStartup) { TimerWindow.Show(); } if (!DeckList.AllTags.Contains("All")) { DeckList.AllTags.Add("All"); WriteDecks(); } if (!DeckList.AllTags.Contains("Favorite")) { if (DeckList.AllTags.Count > 1) { DeckList.AllTags.Insert(1, "Favorite"); } else { DeckList.AllTags.Add("Favorite"); } WriteDecks(); } if (!DeckList.AllTags.Contains("Arena")) { DeckList.AllTags.Add("Arena"); WriteDecks(); } if (!DeckList.AllTags.Contains("Constructed")) { DeckList.AllTags.Add("Constructed"); WriteDecks(); } if (!DeckList.AllTags.Contains("None")) { DeckList.AllTags.Add("None"); WriteDecks(); } Options.ComboboxAccent.ItemsSource = ThemeManager.Accents; Options.ComboboxTheme.ItemsSource = ThemeManager.AppThemes; Options.ComboboxLanguages.ItemsSource = Helper.LanguageDict.Keys; Options.ComboboxKeyPressGameStart.ItemsSource = EventKeys; Options.ComboboxKeyPressGameEnd.ItemsSource = EventKeys; LoadConfig(); FillElementSorters(); //this has to happen before reader starts var lastDeck = DeckList.DecksList.FirstOrDefault(d => d.Name == Config.Instance.LastDeck); DeckPickerList.SelectDeck(lastDeck); TurnTimer.Create(90); SortFilterDecksFlyout.HideStuffToCreateNewTag(); TagControlEdit.OperationSwitch.Visibility = Visibility.Collapsed; TagControlEdit.PnlSortDecks.Visibility = Visibility.Collapsed; UpdateDbListView(); _doUpdate = _foundHsDirectory; UpdateOverlayAsync(); _initialized = true; Options.MainWindowInitialized(); DeckPickerList.UpdateList(); if (lastDeck != null) { DeckPickerList.SelectDeck(lastDeck); UpdateDeckList(lastDeck); UseDeck(lastDeck); } if (_foundHsDirectory) { HsLogReader.Instance.Start(); } Helper.SortCardCollection(ListViewDeck.Items, Config.Instance.CardSortingClassFirst); DeckPickerList.SortDecks(); // Set Language var culture = Config.Instance.SelectedLanguage.Insert(2, "-"); WPFLocalizeExtension.Engine.LocalizeDictionary.Instance.Culture = System.Globalization.CultureInfo.GetCultureInfo(culture); if (!Directory.Exists(culture)) { var langPath = Path.Combine("Lang", culture); if (Directory.Exists(langPath)) { Helper.CopyFolder(langPath, culture); _showRestartMessage = true; } } }
public MainWindow() { InitializeComponent(); Helper.CheckForUpdates(); //check for log config and create if not existing try { if (!File.Exists(_logConfigPath)) { File.Copy("Files/log.config", _logConfigPath); } else { //update log.config if newer var localFile = new FileInfo(_logConfigPath); var file = new FileInfo("Files/log.config"); if (file.LastWriteTime > localFile.LastWriteTime) { File.Copy("Files/log.config", _logConfigPath, true); } } } catch (UnauthorizedAccessException e) { MessageBox.Show( e.Message + "\n\n" + e.InnerException + "\n\n Please restart the tracker as administrator", "Error writing log.config"); Close(); return; } catch (Exception e) { MessageBox.Show( e.Message + "\n\n" + e.InnerException + "\n\n What happend here? ", "Error writing log.config"); Close(); return; } //load config _config = new Config(); _xmlManagerConfig = new XmlManager <Config> { Type = typeof(Config) }; try { _config = _xmlManagerConfig.Load("config.xml"); } catch (Exception e) { MessageBox.Show( e.Message + "\n\n" + e.InnerException + "\n\n If you don't know how to fix this, please overwrite config with the default one.", "Error loading config.xml"); Close(); return; } //load saved decks if (!File.Exists("PlayerDecks.xml")) { //avoid overwriting decks file with new releases. using (var sr = new StreamWriter("PlayerDecks.xml", false)) { sr.WriteLine("<Decks></Decks>"); } } _xmlManager = new XmlManager <Decks> { Type = typeof(Decks) }; try { _deckList = _xmlManager.Load("PlayerDecks.xml"); } catch (Exception e) { MessageBox.Show( e.Message + "\n\n" + e.InnerException + "\n\n If you don't know how to fix this, please delete PlayerDecks.xml (this will cause you to lose your decks).", "Error loading PlayerDecks.xml"); Close(); return; } ListboxDecks.ItemsSource = _deckList.DecksList; //hearthstone, loads db etc _hearthstone = new Hearthstone(); _newDeck = new Deck(); ListViewNewDeck.ItemsSource = _newDeck.Cards; //create overlay _overlay = new OverlayWindow(_config, _hearthstone) { Topmost = true }; _overlay.Show(); _playerWindow = new PlayerWindow(_config, _hearthstone.PlayerDeck); _opponentWindow = new OpponentWindow(_config, _hearthstone.EnemyCards); LoadConfig(); //find hs directory if (!File.Exists(_config.HearthstoneDirectory + @"\Hearthstone.exe")) { MessageBox.Show("Please specify your Hearthstone directory", "Hearthstone directory not found", MessageBoxButton.OK); var dialog = new OpenFileDialog(); dialog.Title = "Select Hearthstone.exe"; dialog.DefaultExt = "Hearthstone.exe"; dialog.Filter = "Hearthstone.exe|Hearthstone.exe"; var result = dialog.ShowDialog(); if (result != true) { return; } _config.HearthstoneDirectory = Path.GetDirectoryName(dialog.FileName); _xmlManagerConfig.Save("config.xml", _config); } //log reader _logReader = new HsLogReader(_config.HearthstoneDirectory, _config.UpdateDelay); _logReader.CardMovement += LogReaderOnCardMovement; _logReader.GameStateChange += LogReaderOnGameStateChange; _logReader.Analyzing += LogReaderOnAnalyzing; UpdateDbListView(); _updateThread = new Thread(Update); _updateThread.Start(); ListboxDecks.SelectedItem = _deckList.DecksList.FirstOrDefault(d => d.Name != null && d.Name == _config.LastDeck); _initialized = true; UpdateDeckList(ListboxDecks.SelectedItem as Deck); UseDeck(ListboxDecks.SelectedItem as Deck); _logReader.Start(); }
private async void UpdateOverlayAsync() { var hsForegroundChanged = false; while (_doUpdate) { if (User32.GetHearthstoneWindow() != IntPtr.Zero) { Overlay.UpdatePosition(); if (!_tempUpdateCheckDisabled && Config.Instance.CheckForUpdates) { if (!Game.IsRunning && (DateTime.Now - _lastUpdateCheck) > new TimeSpan(0, 10, 0)) { Version newVersion; var currentVersion = Helper.CheckForUpdates(out newVersion); if (currentVersion != null && newVersion != null) { ShowNewUpdateMessage(newVersion); } _lastUpdateCheck = DateTime.Now; } } Game.IsRunning = true; if (!User32.IsHearthstoneInForeground() && !hsForegroundChanged) { if (Config.Instance.WindowsTopmostIfHsForeground && Config.Instance.WindowsTopmost) { PlayerWindow.Topmost = false; OpponentWindow.Topmost = false; TimerWindow.Topmost = false; } hsForegroundChanged = true; } else if (hsForegroundChanged && User32.IsHearthstoneInForeground()) { Overlay.Update(true); if (Config.Instance.WindowsTopmostIfHsForeground && Config.Instance.WindowsTopmost) { //if player topmost is set to true before opponent: //clicking on the playerwindow and back to hs causes the playerwindow to be behind hs. //other way around it works for both windows... what? OpponentWindow.Topmost = true; PlayerWindow.Topmost = true; TimerWindow.Topmost = true; } hsForegroundChanged = false; } } else { Overlay.ShowOverlay(false); if (Game.IsRunning) { //game was closed Logger.WriteLine("Exited game"); HsLogReader.Instance.ClearLog(); Game.Reset(); if (DeckPickerList.SelectedDeck != null) { Game.SetPremadeDeck((Deck)DeckPickerList.SelectedDeck.Clone()); } HsLogReader.Instance.Reset(true); if (Config.Instance.CloseWithHearthstone) { Close(); } } Game.IsRunning = false; } await Task.Delay(Config.Instance.UpdateDelay); } _canShowDown = true; }
public MainWindow() { InitializeComponent(); Helper.CheckForUpdates(); //check for log config and create if not existing try { if (!File.Exists(_logConfigPath)) { File.Copy("Files/log.config", _logConfigPath); } else { //update log.config if newer var localFile = new FileInfo(_logConfigPath); var file = new FileInfo("Files/log.config"); if (file.LastWriteTime > localFile.LastWriteTime) { File.Copy("Files/log.config", _logConfigPath, true); } } } catch (UnauthorizedAccessException ex) { Console.WriteLine("Not authorized write " + _logConfigPath + ". Start as admin(?)"); Console.WriteLine(ex.Message); } catch (IOException ex) { Console.WriteLine(ex.Message); } //load config _config = new Config(); _xmlManagerConfig = new XmlManager <Config> { Type = typeof(Config) }; _config = _xmlManagerConfig.Load("config.xml"); //load saved decks if (!File.Exists("PlayerDecks.xml")) { //avoid overwriting decks file with new releases. using (var sr = new StreamWriter("PlayerDecks.xml", false)) { sr.WriteLine("<Decks></Decks>"); } } _xmlManager = new XmlManager <Decks> { Type = typeof(Decks) }; _deckList = _xmlManager.Load("PlayerDecks.xml"); //add saved decks to gui foreach (var deck in _deckList.DecksList) { ComboBoxDecks.Items.Add(deck.Name); } ComboBoxDecks.SelectedItem = _config.LastDeck; //hearthstone, loads db etc _hearthstone = new Hearthstone(); //create overlay _overlay = new OverlayWindow(_config, _hearthstone) { Topmost = true }; _overlay.Show(); _playerWindow = new PlayerWindow(_config, _hearthstone.PlayerDeck); _opponentWindow = new OpponentWindow(_config, _hearthstone.EnemyCards); LoadConfig(); //find hs directory if (!File.Exists(_config.HearthstoneDirectory + @"\Hearthstone.exe")) { MessageBox.Show("Please specify your Hearthstone directory", "Hearthstone directory not found", MessageBoxButton.OK); var dialog = new OpenFileDialog(); dialog.Title = "Select Hearthstone.exe"; dialog.DefaultExt = "Hearthstone.exe"; dialog.Filter = "Hearthstone.exe|Hearthstone.exe"; var result = dialog.ShowDialog(); if (result != true) { return; } _config.HearthstoneDirectory = Path.GetDirectoryName(dialog.FileName); _xmlManagerConfig.Save("config.xml", _config); } //log reader _logReader = new HsLogReader(_config.HearthstoneDirectory); _logReader.CardMovement += LogReaderOnCardMovement; _logReader.GameStateChange += LogReaderOnGameStateChange; UpdateDbListView(); _options = new OptionsWindow(_config, _overlay, _xmlManagerConfig, _playerWindow, _opponentWindow); _updateThread = new Thread(Update); _updateThread.Start(); _initialized = true; UpdateDeckList(); UseSelectedDeck(); _logReader.Start(); }