From 1789e5b9a8f90b2ab9eb6c33993f09295056735f Mon Sep 17 00:00:00 2001 From: Xian55 <367101+Xian55@users.noreply.github.com> Date: Sun, 12 Nov 2023 23:51:42 +0100 Subject: [PATCH] Fix Screen was not captured during configuration mode Core: FrameConfigurator: Have to manually enable ScreenCapture! Core: DataFrameConfig: record struct Core: DataFrameMeta: record struct and fixes Core: FrameConfigMeta: Bump version number to 4 since SixLabors.Size uses different serialization. Core: WoWSxreenDXGI: Fixed an error while in configuration mode, attempt to read possible out of bound pixels from non existing AddonTexture --- Core/Configurator/FrameConfigurator.cs | 23 ++++--- Core/DataFrame/DataFrameConfig.cs | 19 +++--- Core/DataFrame/DataFrameMeta.cs | 80 +++++++------------------ Core/DataFrame/FrameConfig.cs | 18 +++--- Core/WoWScreen/WowScreenDXGI.cs | 3 +- Frontend/Pages/FrameConfiguration.razor | 10 ++-- 6 files changed, 63 insertions(+), 90 deletions(-) diff --git a/Core/Configurator/FrameConfigurator.cs b/Core/Configurator/FrameConfigurator.cs index 3c056c85..5f5f45e0 100644 --- a/Core/Configurator/FrameConfigurator.cs +++ b/Core/Configurator/FrameConfigurator.cs @@ -82,6 +82,8 @@ public void Dispose() private void ManualConfigThread() { + screen.Enabled = true; + while (!cts.Token.IsCancellationRequested) { DoConfig(false); @@ -91,6 +93,8 @@ private void ManualConfigThread() wait.Update(); } screenshotThread = null; + + screen.Enabled = false; } private bool DoConfig(bool auto) @@ -173,10 +177,7 @@ private bool DoConfig(bool auto) DataFrameMeta = temp; stage++; - if (auto) - { - logger.LogInformation($"{nameof(DataFrameMeta)}: {DataFrameMeta}"); - } + logger.LogInformation($"{DataFrameMeta}"); } break; case Stage.ValidateMetaSize: @@ -211,14 +212,14 @@ void cropSize(IImageProcessingContext x) ImageBase64 = cropped.ToBase64String(JpegFormat.Instance); } - DataFrames = FrameConfig.TryCreateFrames(DataFrameMeta, cropped); - if (DataFrames.Length == DataFrameMeta.frames) + DataFrames = FrameConfig.CreateFrames(DataFrameMeta, cropped); + if (DataFrames.Length == DataFrameMeta.Count) { stage++; } else { - logger.LogWarning($"DataFrameMeta and FrameConfig dosen't match Frames: ({DataFrames.Length}) != Meta: ({DataFrameMeta.frames})"); + logger.LogWarning($"DataFrameMeta and FrameConfig dosen't match Frames: ({DataFrames.Length}) != Meta: ({DataFrameMeta.Count})"); stage = Stage.Reset; if (auto) @@ -335,8 +336,8 @@ public bool FinishConfig() Version? version = addonConfigurator.GetInstallVersion(); if (version == null || DataFrames.Length == 0 || - DataFrameMeta.frames == 0 || - DataFrames.Length != DataFrameMeta.frames || + DataFrameMeta.Count == 0 || + DataFrames.Length != DataFrameMeta.Count || !TryResolveRaceAndClass(out _, out _, out _)) { logger.LogInformation("Frame configuration was incomplete! Please try again, after resolving the previusly mentioned issues..."); @@ -354,11 +355,15 @@ public bool FinishConfig() public bool StartAutoConfig() { + screen.Enabled = true; + while (DoConfig(true)) { wait.Update(); } + screen.Enabled = false; + return FinishConfig(); } diff --git a/Core/DataFrame/DataFrameConfig.cs b/Core/DataFrame/DataFrameConfig.cs index 9f3c704a..a55b92a9 100644 --- a/Core/DataFrame/DataFrameConfig.cs +++ b/Core/DataFrame/DataFrameConfig.cs @@ -1,22 +1,23 @@ using System; + using SixLabors.ImageSharp; namespace Core; -public readonly struct DataFrameConfig +public readonly record struct DataFrameConfig { public int Version { get; } - public Version addonVersion { get; } - public Rectangle rect { get; } - public DataFrameMeta meta { get; } - public DataFrame[] frames { get; } + public Version AddonVersion { get; } + public Rectangle Rect { get; } + public DataFrameMeta Meta { get; } + public DataFrame[] Frames { get; } public DataFrameConfig(int version, Version addonVersion, Rectangle rect, DataFrameMeta meta, DataFrame[] frames) { Version = version; - this.addonVersion = addonVersion; - this.rect = rect; - this.meta = meta; - this.frames = frames; + this.AddonVersion = addonVersion; + this.Rect = rect; + this.Meta = meta; + this.Frames = frames; } } diff --git a/Core/DataFrame/DataFrameMeta.cs b/Core/DataFrame/DataFrameMeta.cs index 6ed3bee2..1e1c9cc7 100644 --- a/Core/DataFrame/DataFrameMeta.cs +++ b/Core/DataFrame/DataFrameMeta.cs @@ -4,81 +4,47 @@ namespace Core; -public readonly struct DataFrameMeta : IEquatable +public readonly record struct DataFrameMeta { [JsonIgnore] - public static DataFrameMeta Empty { get; } = new(-1, 0, 0, 0, 0); + private static readonly DataFrameMeta empty = new(-1, 0, 0, 0, 0); + [JsonIgnore] + public static ref readonly DataFrameMeta Empty => ref empty; [JsonConstructor] - public DataFrameMeta(int hash, int spacing, int size, int rows, int frames) + public DataFrameMeta(int hash, int spacing, int sizes, int rows, int count) { - this.hash = hash; - this.spacing = spacing; - this.size = size; - this.rows = rows; - this.frames = frames; + this.Hash = hash; + this.Spacing = spacing; + this.Sizes = sizes; + this.Rows = rows; + this.Count = count; } - public int hash { get; } + public int Hash { get; } - public int spacing { get; } + public int Spacing { get; } - public int size { get; } + public int Sizes { get; } - public int rows { get; } + public int Rows { get; } - public int frames { get; } + public int Count { get; } public Size EstimatedSize(Rectangle screenRect) { const int error = 2; - int squareSize = size + error + (spacing != 0 ? spacing + error : 0); - if (squareSize <= 0) - return Size.Empty; - - SizeF estimatedSize = new((float)Math.Ceiling(frames / (float)rows) * squareSize, rows * squareSize); - - if (estimatedSize.Width > screenRect.Width || - estimatedSize.Height > screenRect.Height) - { + int cellSize = Sizes + error + (Spacing != 0 ? Spacing + error : 0); + if (cellSize <= 0) return Size.Empty; - } - - return (Size)estimatedSize; - } - public override int GetHashCode() - { - return hash; - } - - public static bool operator ==(DataFrameMeta left, DataFrameMeta right) - { - return left.Equals(right); - } + SizeF estimated = + new((float)Math.Ceiling(Count / (float)Rows) * cellSize, Rows * cellSize); - public static bool operator !=(DataFrameMeta left, DataFrameMeta right) - { - return !(left == right); - } - - public bool Equals(DataFrameMeta other) - { - return other.hash == hash && - other.spacing == spacing && - other.size == size && - other.rows == rows && - other.frames == frames; - } - - public override bool Equals(object? obj) - { - return obj is DataFrameMeta && Equals((DataFrameMeta)obj); - } - - public override string ToString() - { - return $"hash: {hash} | spacing: {spacing} | size: {size} | rows: {rows} | frames: {frames}"; + return estimated.Width > screenRect.Width || + estimated.Height > screenRect.Height + ? Size.Empty + : (Size)estimated; } } diff --git a/Core/DataFrame/FrameConfig.cs b/Core/DataFrame/FrameConfig.cs index 3cdcce61..735954c7 100644 --- a/Core/DataFrame/FrameConfig.cs +++ b/Core/DataFrame/FrameConfig.cs @@ -10,7 +10,7 @@ namespace Core; public static class FrameConfigMeta { - public const int Version = 3; + public const int Version = 4; public const string DefaultFilename = "frame_config.json"; } @@ -28,9 +28,9 @@ public static bool IsValid(Rectangle rect, Version addonVersion) var config = JsonConvert.DeserializeObject(File.ReadAllText(FrameConfigMeta.DefaultFilename)); bool sameVersion = config.Version == FrameConfigMeta.Version; - bool sameAddonVersion = config.addonVersion == addonVersion; - bool sameRect = config.rect.Width == rect.Width && config.rect.Height == rect.Height; - return sameAddonVersion && sameVersion && sameRect && config.frames.Length > 1; + bool sameAddonVersion = config.AddonVersion == addonVersion; + bool sameRect = config.Rect.Width == rect.Width && config.Rect.Height == rect.Height; + return sameAddonVersion && sameVersion && sameRect && config.Frames.Length > 1; } catch { @@ -44,7 +44,7 @@ public static DataFrame[] LoadFrames() { var config = JsonConvert.DeserializeObject(File.ReadAllText(FrameConfigMeta.DefaultFilename)); if (config.Version == FrameConfigMeta.Version) - return config.frames; + return config.Frames; } return Array.Empty(); @@ -54,7 +54,7 @@ public static DataFrameMeta LoadMeta() { var config = JsonConvert.DeserializeObject(File.ReadAllText(FrameConfigMeta.DefaultFilename)); if (config.Version == FrameConfigMeta.Version) - return config.meta; + return config.Meta; return DataFrameMeta.Empty; } @@ -90,12 +90,12 @@ public static DataFrameMeta GetMeta(Bgra32 color) return new DataFrameMeta(hash, spacing, size, rows, count); } - public static DataFrame[] TryCreateFrames(DataFrameMeta meta, Image bmp) + public static DataFrame[] CreateFrames(DataFrameMeta meta, Image bmp) { - DataFrame[] frames = new DataFrame[meta.frames]; + DataFrame[] frames = new DataFrame[meta.Count]; frames[0] = new(0, 0, 0); - for (int i = 1; i < meta.frames; i++) + for (int i = 1; i < meta.Count; i++) { if (TryGetNextPoint(bmp, i, frames[i].X, out int x, out int y)) { diff --git a/Core/WoWScreen/WowScreenDXGI.cs b/Core/WoWScreen/WowScreenDXGI.cs index 54d0851f..767acdbd 100644 --- a/Core/WoWScreen/WowScreenDXGI.cs +++ b/Core/WoWScreen/WowScreenDXGI.cs @@ -246,7 +246,8 @@ public void Update() ID3D11Texture2D texture = idxgiResource.QueryInterface(); - UpdateAddonImage(texture); + if (frames.Length > 2) + UpdateAddonImage(texture); if (Enabled) UpdateScreenImage(texture); diff --git a/Frontend/Pages/FrameConfiguration.razor b/Frontend/Pages/FrameConfiguration.razor index 4864055c..a39d5493 100644 --- a/Frontend/Pages/FrameConfiguration.razor +++ b/Frontend/Pages/FrameConfiguration.razor @@ -98,7 +98,7 @@
Your screen:
- @if (@frameConfigurator.DataFrames.Length != frameConfigurator.DataFrameMeta.frames) + @if (@frameConfigurator.DataFrames.Length != frameConfigurator.DataFrameMeta.Count) { Red dot } @@ -116,7 +116,7 @@
  • Step 1: Please ensure the addon @addonConfig.Title found in the addon folders is running. You should see the multi-coloured pixels at the top left of the screen. - @if (frameConfigurator.DataFrames.Length != frameConfigurator.DataFrameMeta.frames) + @if (frameConfigurator.DataFrames.Length != frameConfigurator.DataFrameMeta.Count) {
    @@ -124,16 +124,16 @@
  • Step 2: Now we are going to put the addon into configuration mode, this will change the addon colours displayed. In the wow chat window: type /@addonConfig.Command - @if (frameConfigurator.DataFrames.Length != frameConfigurator.DataFrameMeta.frames) + @if (frameConfigurator.DataFrames.Length != frameConfigurator.DataFrameMeta.Count) {
    }
  • - Step 3: Should see @frameConfigurator.DataFrameMeta.frames frames - Now i see @frameConfigurator.DataFrames.Length data frames. + Step 3: Should see @frameConfigurator.DataFrameMeta.Count frames - Now i see @frameConfigurator.DataFrames.Length data frames.
  • - @if (frameConfigurator.DataFrameMeta.frames != 0 && frameConfigurator.DataFrames.Length == frameConfigurator.DataFrameMeta.frames) + @if (frameConfigurator.DataFrameMeta.Count != 0 && frameConfigurator.DataFrames.Length == frameConfigurator.DataFrameMeta.Count) {
  • Step 4: Now return to normal mode, In the wow chat window: type /@addonConfig.Command