Skip to content

Commit

Permalink
Fix for change in nwmarketprices data format
Browse files Browse the repository at this point in the history
  • Loading branch information
josdemmers committed Apr 24, 2022
1 parent 8223074 commit 3e6181a
Show file tree
Hide file tree
Showing 22 changed files with 319 additions and 15 deletions.
4 changes: 2 additions & 2 deletions NewWorldCompanion.Entities/NwmarketpriceJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public class NwmarketpriceJson
{
public string item_name { get; set; } = string.Empty;
public string last_checked { get; set; } = string.Empty;
public string recent_lowest_price { get; set; } = string.Empty;
//public double recent_lowest_price { get; set; } = 0.0;
//public string recent_lowest_price { get; set; } = string.Empty;
public double recent_lowest_price { get; set; } = 0.0;
public List<object> avg_graph_data { get; set; } = new List<object>();

[JsonIgnore]
Expand Down
3 changes: 3 additions & 0 deletions NewWorldCompanion.Events/OcrEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ namespace NewWorldCompanion.Events
public class OcrTextReadyEvent : PubSubEvent
{
}
public class OcrTextCountReadyEvent : PubSubEvent
{
}
}
4 changes: 4 additions & 0 deletions NewWorldCompanion.Events/ScreenProcessEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ public class RoiImageReadyEvent : PubSubEvent
public class OcrImageReadyEvent : PubSubEvent
{
}

public class OcrImageCountReadyEvent : PubSubEvent
{
}
}
27 changes: 27 additions & 0 deletions NewWorldCompanion.Helpers/DoubleConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace NewWorldCompanion.Helpers
{
public class DoubleConverter : JsonConverter<double>
{
public override void Write(Utf8JsonWriter writer, double value, JsonSerializerOptions options) =>
writer.WriteNumberValue(value);

public override double Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
reader.TokenType switch
{
JsonTokenType.True => 0.00,
JsonTokenType.False => 0.00,
JsonTokenType.String => double.TryParse(reader.GetString(), out var b) ? b : 0.00,
JsonTokenType.Number => reader.TryGetDouble(out double d) ? d : 0.00,
JsonTokenType.Null => 0.00,
_ => throw new JsonException(),
};
}
}
44 changes: 44 additions & 0 deletions NewWorldCompanion.Helpers/ScreenCapture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,50 @@ public Bitmap GetScreenCapture(IntPtr windowHandle)
return bitmap;
}

public Bitmap? GetScreenCaptureMouseArea(IntPtr windowHandle)
{
Bitmap? bitmap = null;

PInvoke.RECT region;
PInvoke.User32.GetWindowRect(windowHandle, out region);

int width = 100;
int height = 50;

PInvoke.User32.CURSORINFO cursorInfo = new PInvoke.User32.CURSORINFO();
cursorInfo.cbSize = Marshal.SizeOf(cursorInfo);
PInvoke.User32.GetCursorInfo(ref cursorInfo);
int xPos = cursorInfo.ptScreenPos.x;
int yPos = cursorInfo.ptScreenPos.y;

var desktopWindowHandle = PInvoke.User32.GetDesktopWindow();
var windowDCHandle = PInvoke.User32.GetWindowDC(desktopWindowHandle);
var memoryDCHandle = PInvoke.Gdi32.CreateCompatibleDC(windowDCHandle);
var bitmapHandle = PInvoke.Gdi32.CreateCompatibleBitmap(windowDCHandle, width, height);
var bitmapOldHandle = PInvoke.Gdi32.SelectObject(memoryDCHandle, bitmapHandle);

xPos = Math.Min(Math.Max(xPos - width / 2, region.left), region.right - width);
yPos = Math.Min(Math.Max(yPos, region.top), region.bottom - height);
bool status = PInvoke.Gdi32.BitBlt(memoryDCHandle.DangerousGetHandle(), 0, 0, width, height, windowDCHandle.DangerousGetHandle(), xPos, yPos, SRCCOPY | CAPTUREBLT);

try
{
if (status)
{
bitmap = Image.FromHbitmap(bitmapHandle);
}
}
finally
{
PInvoke.Gdi32.SelectObject(memoryDCHandle, bitmapOldHandle);
PInvoke.Gdi32.DeleteObject(bitmapHandle);
PInvoke.Gdi32.DeleteDC(memoryDCHandle);
PInvoke.User32.ReleaseDC(desktopWindowHandle, windowDCHandle.DangerousGetHandle());
}

return bitmap;
}

public static BitmapSource? ImageSourceFromBitmap(Bitmap? bitmap)
{
if (bitmap != null)
Expand Down
1 change: 1 addition & 0 deletions NewWorldCompanion.Interfaces/IOcrHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
public interface IOcrHandler
{
string OcrText { get; }
string OcrTextCount { get; }
}
}
2 changes: 2 additions & 0 deletions NewWorldCompanion.Interfaces/IScreenCaptureHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ namespace NewWorldCompanion.Interfaces
public interface IScreenCaptureHandler
{
Bitmap? CurrentScreen { get; }
Bitmap? CurrentScreenMouseArea { get; }
bool IsActive { get; set; }
string MouseCoordinates { get; }
string MouseCoordinatesScaled { get; }
int OffsetX { get; }
int OffsetY { get; }

BitmapSource? ImageSourceFromScreenCapture();
BitmapSource? ImageSourceFromScreenCaptureMouseArea();
}
}
1 change: 1 addition & 0 deletions NewWorldCompanion.Interfaces/IScreenProcessHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public interface IScreenProcessHandler
Bitmap? ProcessedImage { get; }
Bitmap? RoiImage { get; }
Bitmap? OcrImage { get; }
Bitmap? OcrImageCount { get; }

int OverlayX { get; }
int OverlayY { get; }
Expand Down
31 changes: 31 additions & 0 deletions NewWorldCompanion.Services/OcrHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class OcrHandler : IOcrHandler

private bool _isBusy = false;
private string _ocrText = string.Empty;
private string _ocrTextCount = string.Empty;

// Start of Constructor region

Expand All @@ -32,6 +33,7 @@ public OcrHandler(IEventAggregator eventAggregator, INewWorldDataStore newWorldD
// Init IEventAggregator
_eventAggregator = eventAggregator;
_eventAggregator.GetEvent<OcrImageReadyEvent>().Subscribe(HandleOcrImageReadyEvent);
_eventAggregator.GetEvent<OcrImageCountReadyEvent>().Subscribe(HandleOcrImageCountReadyEvent);

// Init services
_newWorldDataStore = newWorldDataStore;
Expand All @@ -48,6 +50,7 @@ public OcrHandler(IEventAggregator eventAggregator, INewWorldDataStore newWorldD
#region Properties

public string OcrText { get => _ocrText; set => _ocrText = value; }
public string OcrTextCount { get => _ocrTextCount; set => _ocrTextCount = value; }

#endregion

Expand Down Expand Up @@ -86,6 +89,34 @@ private void HandleOcrImageReadyEvent()
}
}

private void HandleOcrImageCountReadyEvent()
{
if (!_isBusy)
{
_isBusy = true;
if (_screenProcessHandler.OcrImage != null)
{
try
{
Image image = Image.FromFile(@"ocrimages\itemcount.png");
Tesseract tesseract = new Tesseract();
string ocrText = tesseract.Read(image).Trim().Replace('\n', ' ');
OcrTextCount = ocrText;

image.Dispose();
tesseract.Dispose();

_eventAggregator.GetEvent<OcrTextCountReadyEvent>().Publish();
}
catch (Exception)
{
_isBusy = false;
}
}
_isBusy = false;
}
}

#endregion

// Start of Methods region
Expand Down
17 changes: 13 additions & 4 deletions NewWorldCompanion.Services/OverlayHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Prism.Events;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -128,6 +129,7 @@ private void DrawGraphics(object? sender, DrawGraphicsEventArgs e)
private void DrawGraphicsItem(DrawGraphicsEventArgs e, string itemName)
{
NwmarketpriceJson nwmarketpriceJson = _priceManager.GetPriceData(itemName);
NumberStyles style = NumberStyles.AllowDecimalPoint;

string infoItemName = itemName;
string infoPrice = "Loading...";
Expand All @@ -137,9 +139,12 @@ private void DrawGraphicsItem(DrawGraphicsEventArgs e, string itemName)
{
string recentLowestPriceAvgList = nwmarketpriceJson.RecentLowestPriceAvg;

infoPrice = nwmarketpriceJson.recent_lowest_price.Equals(nwmarketpriceJson.last_checked) ?
nwmarketpriceJson.recent_lowest_price :
$"{nwmarketpriceJson.recent_lowest_price} lowest ({nwmarketpriceJson.last_checked})";
//infoPrice = nwmarketpriceJson.recent_lowest_price.Equals(nwmarketpriceJson.last_checked) ?
// nwmarketpriceJson.recent_lowest_price :
// $"{nwmarketpriceJson.recent_lowest_price} lowest ({nwmarketpriceJson.last_checked})";
infoPrice = nwmarketpriceJson.recent_lowest_price.Equals(nwmarketpriceJson.last_checked) ?
decimal.Parse(nwmarketpriceJson.recent_lowest_price.ToString(), style, CultureInfo.InvariantCulture).ToString("F2"):
$"{nwmarketpriceJson.recent_lowest_price} lowest ({nwmarketpriceJson.last_checked})";
infoPriceAvg = string.IsNullOrWhiteSpace(recentLowestPriceAvgList) ?
infoPriceAvg :
$"{recentLowestPriceAvgList} lowest avg ({nwmarketpriceJson.last_checked})";
Expand All @@ -164,6 +169,7 @@ private void DrawGraphicsItem(DrawGraphicsEventArgs e, string itemName)
private void DrawGraphicsRecipe(DrawGraphicsEventArgs e, CraftingRecipe craftingRecipe)
{
NwmarketpriceJson nwmarketpriceJson = _priceManager.GetPriceData(craftingRecipe.Localisation);
NumberStyles style = NumberStyles.AllowDecimalPoint;

bool learnedStatus = craftingRecipe.Learned;
string infoItemName = craftingRecipe.Localisation;
Expand All @@ -175,8 +181,11 @@ private void DrawGraphicsRecipe(DrawGraphicsEventArgs e, CraftingRecipe crafting
{
string recentLowestPriceAvgList = nwmarketpriceJson.RecentLowestPriceAvg;

//infoPrice = nwmarketpriceJson.recent_lowest_price.Equals(nwmarketpriceJson.last_checked) ?
// nwmarketpriceJson.recent_lowest_price :
// $"{nwmarketpriceJson.recent_lowest_price} lowest ({nwmarketpriceJson.last_checked})";
infoPrice = nwmarketpriceJson.recent_lowest_price.Equals(nwmarketpriceJson.last_checked) ?
nwmarketpriceJson.recent_lowest_price :
decimal.Parse(nwmarketpriceJson.recent_lowest_price.ToString(), style, CultureInfo.InvariantCulture).ToString("F2") :
$"{nwmarketpriceJson.recent_lowest_price} lowest ({nwmarketpriceJson.last_checked})";
infoPriceAvg = string.IsNullOrWhiteSpace(recentLowestPriceAvgList) ?
infoPriceAvg :
Expand Down
15 changes: 13 additions & 2 deletions NewWorldCompanion.Services/PriceManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using NewWorldCompanion.Entities;
using NewWorldCompanion.Events;
using NewWorldCompanion.Helpers;
using NewWorldCompanion.Interfaces;
using Prism.Events;
using System;
Expand Down Expand Up @@ -127,13 +128,23 @@ public void UpdatePriceData(string itemName)

if (!string.IsNullOrWhiteSpace(json))
{
var nwmarketpriceJson = JsonSerializer.Deserialize<NwmarketpriceJson>(json);
// create the options
var options = new JsonSerializerOptions()
{
WriteIndented = true
};
// register the converter
options.Converters.Add(new DoubleConverter());

var nwmarketpriceJson = JsonSerializer.Deserialize<NwmarketpriceJson>(json, options);
if (nwmarketpriceJson != null)
{
Debug.WriteLine($"item_name: {nwmarketpriceJson.item_name}");
Debug.WriteLine($"recent_lowest_price: {nwmarketpriceJson.recent_lowest_price}");
Debug.WriteLine($"last_checked: {nwmarketpriceJson.last_checked}");

nwmarketpriceJson.item_name = string.IsNullOrEmpty(nwmarketpriceJson.item_name) ? itemName : nwmarketpriceJson.item_name;

_priceCache[itemName] = nwmarketpriceJson;
_eventAggregator.GetEvent<PriceCacheUpdatedEvent>().Publish();
}
Expand All @@ -143,7 +154,7 @@ public void UpdatePriceData(string itemName)
_priceCache[itemName] = new NwmarketpriceJson
{
item_name = itemName,
recent_lowest_price = "no data",
recent_lowest_price = 0.00,
last_checked = "no data"
};
}
Expand Down
15 changes: 15 additions & 0 deletions NewWorldCompanion.Services/ScreenCaptureHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class ScreenCaptureHandler : IScreenCaptureHandler
private DispatcherTimer _captureTimer = new();
private DispatcherTimer _coordinatesTimer = new();
private Bitmap? _currentScreen = null;
private Bitmap? _currentScreenMouseArea = null;
private int _delay = 100;
private int _delayCoordinates = 100;
private bool _isActive = true;
Expand Down Expand Up @@ -59,6 +60,7 @@ public ScreenCaptureHandler(IEventAggregator eventAggregator)
#region Properties

public Bitmap? CurrentScreen { get => _currentScreen; set => _currentScreen = value; }
public Bitmap? CurrentScreenMouseArea { get => _currentScreenMouseArea; set => _currentScreenMouseArea = value; }
public int Delay { get => _delay; set => _delay = value; }
public int DelayCoordinates { get => _delayCoordinates; set => _delayCoordinates = value; }
public bool IsActive { get => _isActive; set => _isActive = value; }
Expand Down Expand Up @@ -118,6 +120,7 @@ private void ScreenCapture()
if (windowHandle.ToInt64() > 0)
{
_currentScreen = _screenCapture.GetScreenCaptureMouse(windowHandle, ref _offsetX, ref _offsetY) ?? _currentScreen;
_currentScreenMouseArea = _screenCapture.GetScreenCaptureMouseArea(windowHandle) ?? _currentScreenMouseArea;
_eventAggregator.GetEvent<ScreenCaptureReadyEvent>().Publish();
valid = true;

Expand Down Expand Up @@ -178,6 +181,18 @@ private void UpdateCoordinates()
}
}

public BitmapSource? ImageSourceFromScreenCaptureMouseArea()
{
if (CurrentScreenMouseArea != null)
{
return Helpers.ScreenCapture.ImageSourceFromBitmap(CurrentScreenMouseArea);
}
else
{
return null;
}
}

#endregion

}
Expand Down
Loading

0 comments on commit 3e6181a

Please sign in to comment.