//public Guid ActiveDeckId { get; set; } public static void Load() { SetupDeckListFile(); var file = Config.Instance.DataDir + "PlayerDecks.xml"; if (!File.Exists(file)) { return; } try { _instance = XmlManager <DeckList> .Load(file); } catch (Exception) { //failed loading deckstats var corruptedFile = Helper.GetValidFilePath(Config.Instance.DataDir, "PlayerDecks_corrupted", "xml"); try { File.Move(file, corruptedFile); } catch (Exception) { throw new Exception( "Can not load or move PlayerDecks.xml file. Please manually delete the file in \"%appdata\\HearthstoneDeckTracker\"."); } //get latest backup file var backup = new DirectoryInfo(Config.Instance.DataDir).GetFiles("PlayerDecks_backup*").OrderByDescending(x => x.CreationTime).FirstOrDefault(); if (backup != null) { try { File.Copy(backup.FullName, file); _instance = XmlManager <DeckList> .Load(file); } catch (Exception ex) { throw new Exception( "Error restoring PlayerDecks backup. Please manually rename \"PlayerDecks_backup.xml\" to \"PlayerDecks.xml\" in \"%appdata\\HearthstoneDeckTracker\".", ex); } } else { throw new Exception("PlayerDecks.xml is corrupted."); } } var save = false; if (!Instance.AllTags.Contains("All")) { Instance.AllTags.Add("All"); save = true; } if (!Instance.AllTags.Contains("Favorite")) { if (Instance.AllTags.Count > 1) { Instance.AllTags.Insert(1, "Favorite"); } else { Instance.AllTags.Add("Favorite"); } save = true; } if (!Instance.AllTags.Contains("None")) { Instance.AllTags.Add("None"); save = true; } if (save) { Save(); } Instance.LoadActiveDeck(); }
public async Task GetCardCounts(Deck deck) { var hsHandle = User32.GetHearthstoneWindow(); if(!User32.IsHearthstoneInForeground()) { //restore window and bring to foreground User32.ShowWindow(hsHandle, User32.SwRestore); User32.SetForegroundWindow(hsHandle); //wait it to actually be in foreground, else the rect might be wrong await Task.Delay(500); } if(!User32.IsHearthstoneInForeground()) { MessageBox.Show("Can't find Hearthstone window."); Logger.WriteLine("Can't find Hearthstone window.", "ArenaImport"); return; } await Task.Delay(1000); Overlay.ForceHidden = true; Overlay.UpdatePosition(); const double xScale = 0.013; const double yScale = 0.017; const int targetHue = 53; const int hueMargin = 3; const int numVisibleCards = 21; var hsRect = User32.GetHearthstoneRect(false); var ratio = (4.0 / 3.0) / ((double)hsRect.Width / hsRect.Height); var posX = (int)DeckExporter.GetXPos(0.92, hsRect.Width, ratio); var startY = 71.0/768.0 * hsRect.Height; var strideY = 29.0/768.0 * hsRect.Height; int width = (int)Math.Round(hsRect.Width * xScale); int height = (int)Math.Round(hsRect.Height * yScale); for(var i = 0; i < Math.Min(numVisibleCards, deck.Cards.Count); i++) { var posY = (int)(startY + strideY * i); var capture = Helper.CaptureHearthstone(new System.Drawing.Point(posX, posY), width, height, hsHandle); if(capture != null) { var yellowPixels = 0; for(int x = 0; x < width; x++) { for(int y = 0; y < height; y++) { var pixel = capture.GetPixel(x, y); if(Math.Abs(pixel.GetHue() - targetHue) < hueMargin) yellowPixels++; } } //Console.WriteLine(yellowPixels + " of " + width * height + " - " + yellowPixels / (double)(width * height)); //capture.Save("arenadeckimages/" + i + ".png"); var yellowPixelRatio = yellowPixels / (double)(width * height); if(yellowPixelRatio > 0.25 && yellowPixelRatio < 50) deck.Cards[i].Count = 2; } } if(deck.Cards.Count > numVisibleCards) { const int scrollClicksPerCard = 4; const int scrollDistance = 120; var clientPoint = new System.Drawing.Point(posX, (int)startY); var previousPos = System.Windows.Forms.Cursor.Position; User32.ClientToScreen(hsHandle, ref clientPoint); System.Windows.Forms.Cursor.Position = new System.Drawing.Point(clientPoint.X, clientPoint.Y); for(int j = 0; j < scrollClicksPerCard * (deck.Cards.Count - numVisibleCards); j++) { User32.mouse_event((uint)User32.MouseEventFlags.Wheel, 0, 0, -scrollDistance, UIntPtr.Zero); await Task.Delay(30); } System.Windows.Forms.Cursor.Position = previousPos; await Task.Delay(100); var remainingCards = deck.Cards.Count - numVisibleCards; startY = 76.0 / 768.0 * hsRect.Height + (numVisibleCards - remainingCards) * strideY; for(int i = 0; i < remainingCards ; i++) { var posY = (int)(startY + strideY * i); var capture = Helper.CaptureHearthstone(new System.Drawing.Point(posX, posY), width, height, hsHandle); if(capture != null) { var yellowPixels = 0; for(int x = 0; x < width; x++) { for(int y = 0; y < height; y++) { var pixel = capture.GetPixel(x, y); if(Math.Abs(pixel.GetHue() - targetHue) < hueMargin) yellowPixels++; } } //Console.WriteLine(yellowPixels + " of " + width * height + " - " + yellowPixels / (double)(width * height)); //capture.Save("arenadeckimages/" + i + 21 + ".png"); var yellowPixelRatio = yellowPixels / (double)(width * height); if(yellowPixelRatio > 0.25 && yellowPixelRatio < 50) deck.Cards[numVisibleCards + i].Count = 2; } } System.Windows.Forms.Cursor.Position = new System.Drawing.Point(clientPoint.X, clientPoint.Y); for(int j = 0; j < scrollClicksPerCard * (deck.Cards.Count - 21); j++) { User32.mouse_event((uint)User32.MouseEventFlags.Wheel, 0, 0, scrollDistance, UIntPtr.Zero); await Task.Delay(30); } System.Windows.Forms.Cursor.Position = previousPos; } Overlay.ForceHidden = false; Overlay.UpdatePosition(); ActivateWindow(); }
private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) { if (e.Exception is MissingMethodException || e.Exception is TypeLoadException) { var plugin = PluginManager.Instance.Plugins.FirstOrDefault(p => new FileInfo(p.FileName).Name.Replace(".dll", "") == e.Exception.Source); if (plugin != null) { plugin.IsEnabled = false; var header = $"{plugin.NameAndVersion} is not compatible with HDT {Helper.GetCurrentVersion().ToVersionString()}."; ErrorManager.AddError(header, "Make sure you are using the latest version of the Plugin and HDT.\n\n" + e.Exception); e.Handled = true; return; } } var stackTrace = e.Exception.StackTrace.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); Analytics.Analytics.TrackEvent(e.Exception.GetType().ToString().Split('.').Last(), stackTrace.Length > 0 ? stackTrace[0] : "", stackTrace.Length > 1 ? stackTrace[1] : ""); #if (!DEBUG) var date = DateTime.Now; var fileName = "Crash Reports\\" + $"Crash report {date.Day}{date.Month}{date.Year}-{date.Hour}{date.Minute}"; if (!Directory.Exists("Crash Reports")) { Directory.CreateDirectory("Crash Reports"); } using (var sr = new StreamWriter(fileName + ".txt", true)) { sr.WriteLine("########## " + DateTime.Now + " ##########"); sr.WriteLine(e.Exception); sr.WriteLine(Core.MainWindow.Options.OptionsTrackerLogging.TextBoxLog.Text); } MessageBox.Show(e.Exception.Message + "\n\n" + "A crash report file was created at:\n\"" + Environment.CurrentDirectory + "\\" + fileName + ".txt\"\n\nPlease \na) create an issue on github (https://github.com/Epix37/Hearthstone-Deck-Tracker) \nor \nb) send an email to [email protected].\n\nPlease include the generated crash report(s) and a short explanation of what lead to the crash.", "Oops! Something went wrong.", MessageBoxButton.OK, MessageBoxImage.Error); e.Handled = true; Shutdown(); #endif }