From 189d2df099e063cc01fcdbf37e1c535325eca020 Mon Sep 17 00:00:00 2001 From: Phantomical Date: Mon, 10 Nov 2025 21:56:33 -0800 Subject: [PATCH 1/8] Allow using features from newer C# versions --- src/HUDReplacer/HUDReplacer.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/src/HUDReplacer/HUDReplacer.csproj b/src/HUDReplacer/HUDReplacer.csproj index 2aac01c..af0c6e9 100644 --- a/src/HUDReplacer/HUDReplacer.csproj +++ b/src/HUDReplacer/HUDReplacer.csproj @@ -5,6 +5,7 @@ 1.3.1 net4.8 + latest GameData\HUDReplacer From 0e2a5d8dd7298fcddec6f84b7cbd84f2ad486cc0 Mon Sep 17 00:00:00 2001 From: Phantomical Date: Mon, 10 Nov 2025 21:57:08 -0800 Subject: [PATCH 2/8] Always select a texture if one is available --- src/HUDReplacer/HUDReplacer.cs | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/HUDReplacer/HUDReplacer.cs b/src/HUDReplacer/HUDReplacer.cs index e151150..d15ecf7 100644 --- a/src/HUDReplacer/HUDReplacer.cs +++ b/src/HUDReplacer/HUDReplacer.cs @@ -38,16 +38,33 @@ class ReplacementInfo public SizedReplacementInfo GetMatchingReplacement(Texture2D tex) { + if (replacements.Count == 0) + return null; + + SizedReplacementInfo replacement = null; + + // Try to find a texture replacement with matching dimensions. foreach (var info in replacements) { - if (info.width == 0 && info.height == 0) - return info; - if (info.width == tex.width && info.height == tex.height) - return info; + { + replacement = info; + break; + } } - return null; + var unsized = replacements[0]; + + // If no matching sized replacements just return the highest + // priority replacement. + if (replacement is null) + return unsized; + + // Prefer the wrong-sized texture if it is higher priority, but + // otherwise use the replacement. + if (unsized.priority > replacement.priority) + return unsized; + return replacement; } } From ea1c78543bad742e8691372c65cb985f143c9e10 Mon Sep 17 00:00:00 2001 From: Phantomical Date: Mon, 10 Nov 2025 21:58:42 -0800 Subject: [PATCH 3/8] Log initial paths relative to ApplicationRoot --- src/HUDReplacer/HUDReplacer.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/HUDReplacer/HUDReplacer.cs b/src/HUDReplacer/HUDReplacer.cs index d15ecf7..4ef085e 100644 --- a/src/HUDReplacer/HUDReplacer.cs +++ b/src/HUDReplacer/HUDReplacer.cs @@ -242,12 +242,17 @@ static void LoadTextures() continue; } + var basePath = KSPUtil.ApplicationRootPath; Debug.Log($"HUDReplacer: path {filePath} - priority: {priority}"); string[] files = Directory.GetFiles(KSPUtil.ApplicationRootPath + filePath, "*.png"); foreach (string filename in files) { - Debug.Log($"HUDReplacer: Found file {filename}"); + var relpath = filename; + if (relpath.StartsWith(basePath)) + relpath = relpath.Substring(basePath.Length); + + Debug.Log($"HUDReplacer: Found file {relpath}"); int width = 0; int height = 0; From 9dcab1ab16bc4271b64c4d73493e482acf9761e9 Mon Sep 17 00:00:00 2001 From: Phantomical Date: Mon, 10 Nov 2025 21:59:05 -0800 Subject: [PATCH 4/8] Log replaced textures if the debug UI is enabled --- src/HUDReplacer/HUDReplacer.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/HUDReplacer/HUDReplacer.cs b/src/HUDReplacer/HUDReplacer.cs index 4ef085e..2ae8a66 100644 --- a/src/HUDReplacer/HUDReplacer.cs +++ b/src/HUDReplacer/HUDReplacer.cs @@ -314,6 +314,7 @@ internal void ReplaceTextures(Texture2D[] tex_array) if (!SceneImages.TryGetValue(HighLogic.LoadedScene, out var sceneImages)) sceneImages = Empty; + var basePath = KSPUtil.ApplicationRootPath; foreach (Texture2D tex in tex_array) { string name = tex.name; @@ -329,6 +330,15 @@ internal void ReplaceTextures(Texture2D[] tex_array) if (replacement is null) continue; + if (SettingsManager.Instance.showDebugToolbar) + { + var path = replacement.path; + if (path.StartsWith(basePath)) + path = path.Substring(basePath.Length); + + Debug.Log($"HUDReplacer: Replacing texture {name} with {path}"); + } + // Special handling for the mouse cursor int cidx = CursorNames.IndexOf(name); if (cidx != -1) From 5a4824496758d3deaf4e326bae2599678df34623 Mon Sep 17 00:00:00 2001 From: Phantomical Date: Mon, 10 Nov 2025 22:14:35 -0800 Subject: [PATCH 5/8] Explicitly decide on an order when loading non-matching textures The new order is now: - choose by priority, then, - pick the texture that matches the size, otherwise, - pick the unsized texture if available, otherwise, - pick the largest texture. --- src/HUDReplacer/HUDReplacer.cs | 54 ++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/src/HUDReplacer/HUDReplacer.cs b/src/HUDReplacer/HUDReplacer.cs index 2ae8a66..6b64ee2 100644 --- a/src/HUDReplacer/HUDReplacer.cs +++ b/src/HUDReplacer/HUDReplacer.cs @@ -5,6 +5,7 @@ using System.Linq; using UnityEngine; using UnityEngine.EventSystems; +using UnityEngine.Rendering; using UnityEngine.SceneManagement; using UnityEngine.UI; @@ -41,30 +42,53 @@ public SizedReplacementInfo GetMatchingReplacement(Texture2D tex) if (replacements.Count == 0) return null; - SizedReplacementInfo replacement = null; + var sized = GetSizedReplacement(tex); + var unsized = GetUnsizedReplacement(tex); - // Try to find a texture replacement with matching dimensions. + if (sized is not null) + { + // If priorities are equal then prefer the sized texture + if (unsized.priority <= sized.priority) + return sized; + } + + return unsized; + } + + SizedReplacementInfo GetSizedReplacement(Texture2D tex) + { foreach (var info in replacements) { if (info.width == tex.width && info.height == tex.height) + return info; + } + + return null; + } + + SizedReplacementInfo GetUnsizedReplacement(Texture2D tex) + { + SizedReplacementInfo found = replacements[0]; + + foreach (var info in replacements) + { + if (info.priority < found.priority) + break; + + // Prefer textures without a specific size if we have one + // available. + if (info.width == 0 && info.height == 0) { - replacement = info; + found = info; break; } - } - - var unsized = replacements[0]; - // If no matching sized replacements just return the highest - // priority replacement. - if (replacement is null) - return unsized; + // Otherwise use the biggest texture we have + if (info.width > found.width && info.height > found.height) + found = info; + } - // Prefer the wrong-sized texture if it is higher priority, but - // otherwise use the replacement. - if (unsized.priority > replacement.priority) - return unsized; - return replacement; + return found; } } From d1600372aee131d3f97c66366d8b723e12b26430 Mon Sep 17 00:00:00 2001 From: Phantomical Date: Mon, 10 Nov 2025 22:28:47 -0800 Subject: [PATCH 6/8] Avoid storing entries that will never be used --- src/HUDReplacer/HUDReplacer.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/HUDReplacer/HUDReplacer.cs b/src/HUDReplacer/HUDReplacer.cs index 6b64ee2..79b6075 100644 --- a/src/HUDReplacer/HUDReplacer.cs +++ b/src/HUDReplacer/HUDReplacer.cs @@ -315,6 +315,15 @@ static void LoadTextures() replacements.Add(basename, replacement); } + // We will never select a replacement with a priority lower + // than the highest priority, so don't bother adding it to + // the list. + if (replacement.replacements.Count != 0) + { + if (info.priority < replacement.replacements[0].priority) + continue; + } + replacement.replacements.Add(info); } } From 36a0034cf3b577a4ca65eacd7c3033d35ba767ac Mon Sep 17 00:00:00 2001 From: Phantomical Date: Mon, 10 Nov 2025 22:29:02 -0800 Subject: [PATCH 7/8] Update CHANGELOG --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a4b50f..dbffbb9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## Unreleased +### Fixed +* Fixed a bug where a replacement with an explicitly specified size would not + be selected if it didn't match the size of the texture it was replacing. + +### Added +* If the debug menu is enabled HUDReplacer will now log exactly what texture + replacements are occurred. ## 1.3.1 ### Changed From f67eb5dcba262187235ed8e557f6174801dd213c Mon Sep 17 00:00:00 2001 From: Phantomical Date: Mon, 10 Nov 2025 22:35:27 -0800 Subject: [PATCH 8/8] Remove unused argument for GetUnsizedReplacement --- src/HUDReplacer/HUDReplacer.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/HUDReplacer/HUDReplacer.cs b/src/HUDReplacer/HUDReplacer.cs index 79b6075..254528a 100644 --- a/src/HUDReplacer/HUDReplacer.cs +++ b/src/HUDReplacer/HUDReplacer.cs @@ -43,7 +43,7 @@ public SizedReplacementInfo GetMatchingReplacement(Texture2D tex) return null; var sized = GetSizedReplacement(tex); - var unsized = GetUnsizedReplacement(tex); + var unsized = GetUnsizedReplacement(); if (sized is not null) { @@ -66,7 +66,7 @@ SizedReplacementInfo GetSizedReplacement(Texture2D tex) return null; } - SizedReplacementInfo GetUnsizedReplacement(Texture2D tex) + SizedReplacementInfo GetUnsizedReplacement() { SizedReplacementInfo found = replacements[0];