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 diff --git a/src/HUDReplacer/HUDReplacer.cs b/src/HUDReplacer/HUDReplacer.cs index e151150..254528a 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; @@ -38,17 +39,57 @@ class ReplacementInfo public SizedReplacementInfo GetMatchingReplacement(Texture2D tex) { - foreach (var info in replacements) + if (replacements.Count == 0) + return null; + + var sized = GetSizedReplacement(tex); + var unsized = GetUnsizedReplacement(); + + if (sized is not null) { - if (info.width == 0 && info.height == 0) - return info; + // 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() + { + 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) + { + found = info; + break; + } + + // Otherwise use the biggest texture we have + if (info.width > found.width && info.height > found.height) + found = info; + } + + return found; + } } class SizedReplacementInfo @@ -225,12 +266,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; @@ -269,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); } } @@ -292,6 +347,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; @@ -307,6 +363,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) 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