Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
73 changes: 69 additions & 4 deletions src/HUDReplacer/HUDReplacer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Rendering;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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;
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/HUDReplacer/HUDReplacer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<Version>1.3.1</Version>

<TargetFramework>net4.8</TargetFramework>
<LangVersion>latest</LangVersion>
<!-- <DebugType>portable</DebugType> -->

<BinariesOutputRelativePath>GameData\HUDReplacer</BinariesOutputRelativePath>
Expand Down