Skip to content

Commit

Permalink
Added inventory page scanning fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
Cupcak3 committed Sep 10, 2023
1 parent 9312079 commit 232fe56
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 22 deletions.
2 changes: 1 addition & 1 deletion InventoryKamera/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
24 changes: 20 additions & 4 deletions InventoryKamera/game/Navigation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -559,7 +575,7 @@ public static double GetDelay()
return delay;
}

public enum Speed
public enum Speed
{
Slowest,
Slower,
Expand Down
15 changes: 11 additions & 4 deletions InventoryKamera/scraping/ArtifactScraper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
25 changes: 16 additions & 9 deletions InventoryKamera/scraping/CharacterScraper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,24 +261,29 @@ 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;
}

private static int ScanLevel(ref bool ascended)
{
int rescans = 0;
int attempt = 0;

var xRef = 1280.0;
var yRef = 720.0;
Expand Down Expand Up @@ -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;
}
Expand Down
34 changes: 31 additions & 3 deletions InventoryKamera/scraping/InventoryScraper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ internal class InventoryScraper

protected readonly List<InventoryPage> materialPages;

private List<Rectangle> prevRect;
private int prevColumn = 0;
private int prevRow = 0;

public InventoryScraper()
{
materialPages = new List<InventoryPage>();
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion InventoryKamera/scraping/MaterialScraper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void Scan_Materials(ref Inventory inventory)
Material previousMaterial = new Material(null, -1);

List<Rectangle> rectangles;
int page = 0;
int page = 1;

// Keep scanning while not repeating any items names
while (true)
Expand Down

0 comments on commit 232fe56

Please sign in to comment.