From 232fe569904efa236ec42e95a5e74f4f46d74ff4 Mon Sep 17 00:00:00 2001 From: Cupcak3 Date: Sun, 10 Sep 2023 14:44:34 -0700 Subject: [PATCH] Added inventory page scanning fallback --- InventoryKamera/Properties/AssemblyInfo.cs | 2 +- InventoryKamera/game/Navigation.cs | 24 +++++++++++--- InventoryKamera/scraping/ArtifactScraper.cs | 15 ++++++--- InventoryKamera/scraping/CharacterScraper.cs | 25 ++++++++------ InventoryKamera/scraping/InventoryScraper.cs | 34 ++++++++++++++++++-- InventoryKamera/scraping/MaterialScraper.cs | 2 +- 6 files changed, 80 insertions(+), 22 deletions(-) diff --git a/InventoryKamera/Properties/AssemblyInfo.cs b/InventoryKamera/Properties/AssemblyInfo.cs index fbab762..db87a1c 100644 --- a/InventoryKamera/Properties/AssemblyInfo.cs +++ b/InventoryKamera/Properties/AssemblyInfo.cs @@ -32,6 +32,6 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.3.10.*")] +[assembly: AssemblyVersion("1.3.11.*")] //[assembly: AssemblyFileVersion("1.0.0.0")] [assembly: NeutralResourcesLanguage("en")] \ No newline at end of file diff --git a/InventoryKamera/game/Navigation.cs b/InventoryKamera/game/Navigation.cs index 77d91eb..3f74a49 100644 --- a/InventoryKamera/game/Navigation.cs +++ b/InventoryKamera/game/Navigation.cs @@ -426,6 +426,11 @@ public static bool SetCursor(int X, int Y) return SetCursorPos(GetPosition().Left + X, GetPosition().Top + Y); } + public static bool SetCursor(Point point) + { + return SetCursor(point.X, point.Y); + } + public static void Click() { if (SystemInformation.MouseButtonsSwapped) @@ -434,6 +439,17 @@ public static void Click() sim.Mouse.LeftButtonClick(); } + public static void Click(int x, int y) + { + SetCursor(x, y); + Click(); + } + + public static void Click(Point point) + { + Click(point.X, point.Y); + } + public static void Scroll(Direction direction, int scrolls, int delay = 1) { Action Scroll; @@ -469,11 +485,11 @@ public enum Direction RIGHT = 3, } - #endregion Mouse + #endregion Mouse - #region Delays + #region Delays - public static void SystemWait(Speed speed = Speed.Normal) + public static void SystemWait(Speed speed = Speed.Normal) { double value; switch (speed) @@ -559,7 +575,7 @@ public static double GetDelay() return delay; } - public enum Speed + public enum Speed { Slowest, Slower, diff --git a/InventoryKamera/scraping/ArtifactScraper.cs b/InventoryKamera/scraping/ArtifactScraper.cs index e71e852..eee4f9f 100644 --- a/InventoryKamera/scraping/ArtifactScraper.cs +++ b/InventoryKamera/scraping/ArtifactScraper.cs @@ -24,7 +24,7 @@ public void ScanArtifacts(int count = 0) { // Get Max artifacts from screen int artifactCount = count == 0 ? ScanItemCount() : count; - int page = 0; + int page = 1; var (rectangles, cols, rows) = GetPageOfItems(page); int fullPage = cols * rows; int totalRows = (int)Math.Ceiling(artifactCount / (decimal)cols); @@ -125,13 +125,20 @@ public void ScanArtifacts(int count = 0) } else { - // Scroll back one to keep it from getting too crazy - for (int i = 0; i < 10 * rows - (page % 2 == 0 ? 1 : 2); i++) + + for (int i = 0; i < 10 * rows - 1; i++) { Navigation.sim.Mouse.VerticalScroll(-1); Navigation.Wait(1); } - Navigation.SystemWait(Navigation.Speed.Fast); + // Scroll back one to keep it from getting too crazy + if (page % 3 == 0) + { + Logger.Debug("Scrolled back one"); + Navigation.sim.Mouse.VerticalScroll(1); + Navigation.Wait(1); + } + Navigation.SystemWait(Navigation.Speed.Fast); } ++page; (rectangles, cols, rows) = GetPageOfItems(page, acceptLess: totalRows - rowsQueued <= fullPage); diff --git a/InventoryKamera/scraping/CharacterScraper.cs b/InventoryKamera/scraping/CharacterScraper.cs index 2d9046c..a5b7219 100644 --- a/InventoryKamera/scraping/CharacterScraper.cs +++ b/InventoryKamera/scraping/CharacterScraper.cs @@ -261,16 +261,21 @@ private static void ScanNameAndElement(ref string name, ref string element) if (!GenshinProcesor.CharacterMatchesElement(name, element)) { name = ""; element = ""; } } n.Dispose(); - Logger.Debug("Scanned character name as {0} with element {1}", name, element); if (!string.IsNullOrWhiteSpace(name) && !string.IsNullOrWhiteSpace(element)) { - UserInterface.SetCharacter_NameAndElement(bm, name, element); + Logger.Debug("Scanned character name as {0} with element {1}", name, element); + UserInterface.SetCharacter_NameAndElement(bm, name, element); return; } + else + { + Logger.Debug("Could not parse character name/element (Atempt {0}/{1}). Retrying...", attempts+1, maxAttempts); + bm.Save($"./logging/characters/{bm.GetHashCode()}.png"); + } } attempts++; - Navigation.SystemWait(Navigation.Speed.Normal); + Navigation.SystemWait(Navigation.Speed.Fast); } while ( attempts < maxAttempts ); name = null; element = null; @@ -278,7 +283,7 @@ private static void ScanNameAndElement(ref string name, ref string element) private static int ScanLevel(ref bool ascended) { - int rescans = 0; + int attempt = 0; var xRef = 1280.0; var yRef = 720.0; @@ -320,13 +325,15 @@ private static int ScanLevel(ref bool ascended) Logger.Debug("Parsed character level as {0}", level); return level; } - n.Dispose(); - bm.Dispose(); } Logger.Debug("Failed to parse character level and ascension from {0} (text), retrying", text); - rescans++; - Navigation.SystemWait(Navigation.Speed.Normal); - } while (rescans < 10); + + attempt++; + + n.Dispose(); + bm.Dispose(); + Navigation.SystemWait(Navigation.Speed.Fast); + } while (attempt < 50); return -1; } diff --git a/InventoryKamera/scraping/InventoryScraper.cs b/InventoryKamera/scraping/InventoryScraper.cs index 0473320..fc9d49b 100644 --- a/InventoryKamera/scraping/InventoryScraper.cs +++ b/InventoryKamera/scraping/InventoryScraper.cs @@ -57,6 +57,10 @@ internal class InventoryScraper protected readonly List materialPages; + private List prevRect; + private int prevColumn = 0; + private int prevRow = 0; + public InventoryScraper() { materialPages = new List(); @@ -359,6 +363,15 @@ internal string CurrentSortingMethod() // Sort by row then by column within each row rectangles = rectangles.OrderBy(r => r.Top).ThenBy(r => r.Left).ToList(); + var avgWidth = rectangles.Average(r => r.Width); + var avgHeight = rectangles.Average (r => r.Height); + + rectangles.ForEach(r => + { + r.Width = (int)avgWidth; + r.Height = (int)avgHeight; + }); + return (rectangles, colCoords.Count, rowCoords.Count); } } @@ -417,20 +430,35 @@ internal string CurrentSortingMethod() else { weight -= 0.095; ++counter; } weight = Math.Min(weight, 1); + rectangles = null; } while (itemCount != 40 && weight < 1 && counter < 25); if (Properties.Settings.Default.LogScreenshots) { - SaveInventoryBitmap(screenshot, $"{inventoryPage}Inventory.png)"); + SaveInventoryBitmap(screenshot, $"{inventoryPage}Inventory.png"); using (Graphics g = Graphics.FromImage(screenshot)) rectangles.ForEach(r => g.DrawRectangle(new Pen(Color.Green, 2), r)); SaveInventoryBitmap(screenshot, $"{inventoryPage}Inventory{pageNum}_{cols}x{rows} - weight {weight}.png"); - } processedScreenshot.Dispose(); - return (rectangles, cols, rows); + + if (rectangles == null) + { + Logger.Warn("Could not find 40 items in inventory. Re-using previous item page."); + + return prevRect == null ? + throw new ArgumentNullException("Could not find first page of items!") + : + (prevRect, prevColumn, prevRow); + } + else + { + prevRect = rectangles; prevColumn = cols; prevRow = rows; + return (rectangles, cols, rows); + } + } catch (Exception) { diff --git a/InventoryKamera/scraping/MaterialScraper.cs b/InventoryKamera/scraping/MaterialScraper.cs index 28c2b6e..231198f 100644 --- a/InventoryKamera/scraping/MaterialScraper.cs +++ b/InventoryKamera/scraping/MaterialScraper.cs @@ -67,7 +67,7 @@ public void Scan_Materials(ref Inventory inventory) Material previousMaterial = new Material(null, -1); List rectangles; - int page = 0; + int page = 1; // Keep scanning while not repeating any items names while (true)