Skip to content

Commit 9ec220b

Browse files
authored
Fix some cases where texture replacements were not being applied (#10)
* Allow using features from newer C# versions * Always select a texture if one is available * Log initial paths relative to ApplicationRoot * Log replaced textures if the debug UI is enabled * 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. * Avoid storing entries that will never be used * Update CHANGELOG * Remove unused argument for GetUnsizedReplacement
1 parent 8caf227 commit 9ec220b

File tree

3 files changed

+77
-4
lines changed

3 files changed

+77
-4
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## Unreleased
9+
### Fixed
10+
* Fixed a bug where a replacement with an explicitly specified size would not
11+
be selected if it didn't match the size of the texture it was replacing.
12+
13+
### Added
14+
* If the debug menu is enabled HUDReplacer will now log exactly what texture
15+
replacements are occurred.
916

1017
## 1.3.1
1118
### Changed

src/HUDReplacer/HUDReplacer.cs

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Linq;
66
using UnityEngine;
77
using UnityEngine.EventSystems;
8+
using UnityEngine.Rendering;
89
using UnityEngine.SceneManagement;
910
using UnityEngine.UI;
1011

@@ -38,17 +39,57 @@ class ReplacementInfo
3839

3940
public SizedReplacementInfo GetMatchingReplacement(Texture2D tex)
4041
{
41-
foreach (var info in replacements)
42+
if (replacements.Count == 0)
43+
return null;
44+
45+
var sized = GetSizedReplacement(tex);
46+
var unsized = GetUnsizedReplacement();
47+
48+
if (sized is not null)
4249
{
43-
if (info.width == 0 && info.height == 0)
44-
return info;
50+
// If priorities are equal then prefer the sized texture
51+
if (unsized.priority <= sized.priority)
52+
return sized;
53+
}
54+
55+
return unsized;
56+
}
4557

58+
SizedReplacementInfo GetSizedReplacement(Texture2D tex)
59+
{
60+
foreach (var info in replacements)
61+
{
4662
if (info.width == tex.width && info.height == tex.height)
4763
return info;
4864
}
4965

5066
return null;
5167
}
68+
69+
SizedReplacementInfo GetUnsizedReplacement()
70+
{
71+
SizedReplacementInfo found = replacements[0];
72+
73+
foreach (var info in replacements)
74+
{
75+
if (info.priority < found.priority)
76+
break;
77+
78+
// Prefer textures without a specific size if we have one
79+
// available.
80+
if (info.width == 0 && info.height == 0)
81+
{
82+
found = info;
83+
break;
84+
}
85+
86+
// Otherwise use the biggest texture we have
87+
if (info.width > found.width && info.height > found.height)
88+
found = info;
89+
}
90+
91+
return found;
92+
}
5293
}
5394

5495
class SizedReplacementInfo
@@ -225,12 +266,17 @@ static void LoadTextures()
225266
continue;
226267
}
227268

269+
var basePath = KSPUtil.ApplicationRootPath;
228270
Debug.Log($"HUDReplacer: path {filePath} - priority: {priority}");
229271
string[] files = Directory.GetFiles(KSPUtil.ApplicationRootPath + filePath, "*.png");
230272

231273
foreach (string filename in files)
232274
{
233-
Debug.Log($"HUDReplacer: Found file {filename}");
275+
var relpath = filename;
276+
if (relpath.StartsWith(basePath))
277+
relpath = relpath.Substring(basePath.Length);
278+
279+
Debug.Log($"HUDReplacer: Found file {relpath}");
234280

235281
int width = 0;
236282
int height = 0;
@@ -269,6 +315,15 @@ static void LoadTextures()
269315
replacements.Add(basename, replacement);
270316
}
271317

318+
// We will never select a replacement with a priority lower
319+
// than the highest priority, so don't bother adding it to
320+
// the list.
321+
if (replacement.replacements.Count != 0)
322+
{
323+
if (info.priority < replacement.replacements[0].priority)
324+
continue;
325+
}
326+
272327
replacement.replacements.Add(info);
273328
}
274329
}
@@ -292,6 +347,7 @@ internal void ReplaceTextures(Texture2D[] tex_array)
292347
if (!SceneImages.TryGetValue(HighLogic.LoadedScene, out var sceneImages))
293348
sceneImages = Empty;
294349

350+
var basePath = KSPUtil.ApplicationRootPath;
295351
foreach (Texture2D tex in tex_array)
296352
{
297353
string name = tex.name;
@@ -307,6 +363,15 @@ internal void ReplaceTextures(Texture2D[] tex_array)
307363
if (replacement is null)
308364
continue;
309365

366+
if (SettingsManager.Instance.showDebugToolbar)
367+
{
368+
var path = replacement.path;
369+
if (path.StartsWith(basePath))
370+
path = path.Substring(basePath.Length);
371+
372+
Debug.Log($"HUDReplacer: Replacing texture {name} with {path}");
373+
}
374+
310375
// Special handling for the mouse cursor
311376
int cidx = CursorNames.IndexOf(name);
312377
if (cidx != -1)

src/HUDReplacer/HUDReplacer.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<Version>1.3.1</Version>
66

77
<TargetFramework>net4.8</TargetFramework>
8+
<LangVersion>latest</LangVersion>
89
<!-- <DebugType>portable</DebugType> -->
910

1011
<BinariesOutputRelativePath>GameData\HUDReplacer</BinariesOutputRelativePath>

0 commit comments

Comments
 (0)