From 56eb8c7eff6ae498f1db798dda74cb0509666de1 Mon Sep 17 00:00:00 2001 From: Phantomical Date: Tue, 11 Nov 2025 00:42:55 -0800 Subject: [PATCH] Use file-scoped namespaces and reformat everything using csharpier --- .chsharpier.yaml | 1 + .config/dotnet-tools.json | 13 + .csharpierignore | 6 + src/HUDReplacer/HUDReplacer.cs | 2208 +++++++++++++++------------- src/HUDReplacer/HarmonyPatches.cs | 1739 +++++++++++----------- src/HUDReplacer/SettingsManager.cs | 62 +- src/HUDReplacer/ToolbarManager.cs | 75 +- src/HUDReplacer/Utility.cs | 66 +- 8 files changed, 2196 insertions(+), 1974 deletions(-) create mode 100644 .chsharpier.yaml create mode 100644 .config/dotnet-tools.json create mode 100644 .csharpierignore diff --git a/.chsharpier.yaml b/.chsharpier.yaml new file mode 100644 index 0000000..e0c7707 --- /dev/null +++ b/.chsharpier.yaml @@ -0,0 +1 @@ +printWidth: 80 diff --git a/.config/dotnet-tools.json b/.config/dotnet-tools.json new file mode 100644 index 0000000..03df232 --- /dev/null +++ b/.config/dotnet-tools.json @@ -0,0 +1,13 @@ +{ + "version": 1, + "isRoot": true, + "tools": { + "csharpier": { + "version": "1.2.1", + "commands": [ + "csharpier" + ], + "rollForward": false + } + } +} \ No newline at end of file diff --git a/.csharpierignore b/.csharpierignore new file mode 100644 index 0000000..42371c4 --- /dev/null +++ b/.csharpierignore @@ -0,0 +1,6 @@ +*.csproj +*.csproj.user +*.props +*.targets +*.config +GameData diff --git a/src/HUDReplacer/HUDReplacer.cs b/src/HUDReplacer/HUDReplacer.cs index 254528a..261a0c3 100644 --- a/src/HUDReplacer/HUDReplacer.cs +++ b/src/HUDReplacer/HUDReplacer.cs @@ -1,1047 +1,1185 @@ -using Cursors; -using System; +using System; using System.Collections.Generic; using System.IO; using System.Linq; +using Cursors; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.Rendering; using UnityEngine.SceneManagement; using UnityEngine.UI; -namespace HUDReplacer +namespace HUDReplacer; + +[KSPAddon(KSPAddon.Startup.MainMenu, false)] +public class HUDReplacerMainMenu : HUDReplacer { } + +[KSPAddon(KSPAddon.Startup.FlightEditorAndKSC, false)] +public class HUDReplacerFEKSC : HUDReplacer { } + +[KSPAddon(KSPAddon.Startup.TrackingStation, false)] +public class HUDReplacerTrackingStation : HUDReplacer { } + +[KSPAddon(KSPAddon.Startup.Settings, false)] +public class HUDReplacerSettings : HUDReplacer { } + +public partial class HUDReplacer : MonoBehaviour { - [KSPAddon(KSPAddon.Startup.MainMenu, false)] - public class HUDReplacerMainMenu : HUDReplacer - { - - } - [KSPAddon(KSPAddon.Startup.FlightEditorAndKSC, false)] - public class HUDReplacerFEKSC : HUDReplacer - { - - } - [KSPAddon(KSPAddon.Startup.TrackingStation, false)] - public class HUDReplacerTrackingStation : HUDReplacer - { - - } - [KSPAddon(KSPAddon.Startup.Settings, false)] - public class HUDReplacerSettings : HUDReplacer - { - - } - public partial class HUDReplacer : MonoBehaviour - { - class ReplacementInfo - { - public List replacements; - - public SizedReplacementInfo GetMatchingReplacement(Texture2D tex) - { - if (replacements.Count == 0) - return null; - - var sized = GetSizedReplacement(tex); - var unsized = GetUnsizedReplacement(); - - 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() - { - 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 - { - public int priority; - public int width; - public int height; - public string path; - public byte[] cachedTextureBytes; - } - - internal static HUDReplacer instance; - internal static bool enableDebug = false; - - private static Dictionary Images; - private static Dictionary> SceneImages; - - // Empty dictionary to be used when there are no images for a given scene. - private static readonly Dictionary Empty = new Dictionary(); - private static readonly string[] CursorNames = new string[] { "basicNeutral", "basicElectricLime", "basicDisabled" }; - - private static string filePathConfig = "HUDReplacer"; - private static string colorPathConfig = "HUDReplacerRecolor"; - private TextureCursor[] cursors; - public void Awake() + class ReplacementInfo + { + public List replacements; + + public SizedReplacementInfo GetMatchingReplacement(Texture2D tex) + { + if (replacements.Count == 0) + return null; + + var sized = GetSizedReplacement(tex); + var unsized = GetUnsizedReplacement(); + + 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() + { + 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 + { + public int priority; + public int width; + public int height; + public string path; + public byte[] cachedTextureBytes; + } + + internal static HUDReplacer instance; + internal static bool enableDebug = false; + + private static Dictionary Images; + private static Dictionary> SceneImages; + + // Empty dictionary to be used when there are no images for a given scene. + private static readonly Dictionary Empty = + new Dictionary(); + private static readonly string[] CursorNames = new string[] + { + "basicNeutral", + "basicElectricLime", + "basicDisabled", + }; + + private static string filePathConfig = "HUDReplacer"; + private static string colorPathConfig = "HUDReplacerRecolor"; + private TextureCursor[] cursors; + + public void Awake() + { + instance = this; + Debug.Log("HUDReplacer: Running scene change. " + HighLogic.LoadedScene); + + if (Images is null) + LoadTextures(); + + if (Images.Count != 0 && SceneImages.Count != 0) + { + Debug.Log("HUDReplacer: Replacing textures..."); + ReplaceTextures(); + Debug.Log("HUDReplacer: Textures have been replaced!"); + } + + LoadHUDColors(); + } + + public void Update() + { + if (enableDebug) + { + if (Input.GetKeyUp(KeyCode.E)) + { + Debug.Log("HUDReplacer: Dumping list of loaded texture2D objects..."); + Texture2D[] tex_array = (Texture2D[]) + (object)Resources.FindObjectsOfTypeAll(typeof(Texture2D)); + foreach (Texture2D tex in tex_array) + { + Debug.Log(tex.name + " - WxH=" + tex.width + "x" + tex.height); + } + Debug.Log("HUDReplacer: Dumping finished."); + } + if (Input.GetKeyUp(KeyCode.Q)) + { + LoadTextures(); + ReplaceTextures(); + LoadHUDColors(); + Debug.Log("HUDReplacer: Refreshed."); + } + if (Input.GetKeyUp(KeyCode.D)) + { + PointerEventData eventDataCurrentPosition = new PointerEventData( + EventSystem.current + ); + eventDataCurrentPosition.position = new Vector2( + Input.mousePosition.x, + Input.mousePosition.y + ); + + List results = new List(); + EventSystem.current.RaycastAll(eventDataCurrentPosition, results); + + Debug.Log("HUDReplacer: [][][][][][][][][][][][][][][][][]"); + foreach (RaycastResult result in results) + { + try + { + Image img = result.gameObject.GetComponent(); + //Debug.Log("HUDReplacer: ------"); + Debug.Log( + "Image.mainTexture.name: " + + img.mainTexture.name + + " - WxH=" + + img.mainTexture.width + + "x" + + img.mainTexture.height + ); + Debug.Log( + "Image.sprite.texture.name: " + + img.sprite.texture.name + + " - WxH=" + + img.sprite.texture.width + + "x" + + img.sprite.texture.height + ); + Debug.Log("HUDReplacer: ------"); + if ( + img.mainTexture.name == "app_divider_pulldown_header" + || img.mainTexture.name == "app_divider_pulldown_header_over" + ) + { + //result.gameObject.GetComponent().SetColor(Color.red); + } + //Texture2D tex = (Texture2D)img.mainTexture; + //DumpTexture(tex); + } + catch (Exception e) + { + Debug.Log(e.ToString()); + } + } + } + /* + It might be possible for a future update to allow modifying the sprite borders. + This would allow you to specify a "9-slice" size, which would fix the issue of improperly scaling the texture and causing blurry/distorted artifacts. + Currently not feasible, as a workaround for the readonly border property needs to be found. + More info: https://docs.unity3d.com/Manual/9SliceSprites.html + + if (Input.GetKeyUp(KeyCode.A)) + { + Texture2D[] tex_array = (Texture2D[])(object)Resources.FindObjectsOfTypeAll(typeof(Texture2D)); + Sprite[] sprite_array = (Sprite[])(object)Resources.FindObjectsOfTypeAll(typeof(Sprite)); + foreach(Sprite sprite in sprite_array) + { + if(sprite.name == "rect_round_color") + { + sprite.border = new Vector4(1,1,1,1); + } + } + Debug.Log("finished"); + } + */ + } + } + + // This gets called by ModuleManager once it has finished applying all + // patches. If MM is not installed then we'll call LoadTextures in Awake + // instead. + public static void ModuleManagerPostLoad() + { + LoadTextures(); + } + + static void LoadTextures() + { + Images = new Dictionary(); + SceneImages = new Dictionary>(); + + UrlDir.UrlConfig[] configs = GameDatabase + .Instance.GetConfigs(filePathConfig) + .OrderByDescending( + (configFile) => + { + int priority = 0; + configFile.config.TryGetValue("priority", ref priority); + return priority; + } + ) + .ToArray(); + + if (configs.Length == 0) + { + Debug.Log("HUDReplacer: No texture configs found."); + return; + } + + foreach (var configFile in configs) + { + var config = configFile.config; + var filePath = config.GetValue("filePath"); + + string onScene = null; + Dictionary replacements = Images; + if (config.TryGetValue("onScene", ref onScene)) + { + if (!Enum.TryParse(onScene, out GameScenes scene)) + { + Debug.LogError( + $"HUDReplacer: Config {configFile.url} contained invalid onScene value {onScene ?? ""}" + ); + continue; + } + + if (!SceneImages.TryGetValue(scene, out replacements)) + { + replacements = new Dictionary(); + SceneImages.Add(scene, replacements); + } + } + + int priority = 0; + if (!config.TryGetValue("priority", ref priority)) + { + Debug.LogError( + $"HUDReplacer: config at {configFile.url} is missing a priority key and will not be loaded" + ); + 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) + { + var relpath = filename; + if (relpath.StartsWith(basePath)) + relpath = relpath.Substring(basePath.Length); + + Debug.Log($"HUDReplacer: Found file {relpath}"); + + int width = 0; + int height = 0; + + string basename = Path.GetFileNameWithoutExtension(filename); + int index = basename.LastIndexOf('#'); + if (index != -1) + { + string size = basename.Substring(index + 1); + basename = basename.Substring(0, index); + + index = size.IndexOf('x'); + if ( + index == -1 + || !int.TryParse(size.Substring(0, index), out width) + || !int.TryParse(size.Substring(index + 1), out height) + ) + { + Debug.LogError( + $"HUDReplacer: filename {filename} was not in the expected format. It needs to be either `name.png` or `name#x.png`" + ); + continue; + } + } + + SizedReplacementInfo info = new SizedReplacementInfo + { + priority = priority, + width = width, + height = height, + path = filename, + }; + + if (!replacements.TryGetValue(basename, out var replacement)) + { + replacement = new ReplacementInfo + { + replacements = new List(1), + }; + 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); + } + } + } + + internal void ReplaceTextures() + { + if (Images.Count == 0 && SceneImages.Count == 0) + return; + + Texture2D[] tex_array = (Texture2D[]) + (object)Resources.FindObjectsOfTypeAll(typeof(Texture2D)); + ReplaceTextures(tex_array); + } + + internal void ReplaceTextures(Texture2D[] tex_array) + { + if (Images.Count == 0 && SceneImages.Count == 0) + return; + + // Get the overloads specific to the current scene but if there are + // then we just use an empty dictionary. + if (!SceneImages.TryGetValue(HighLogic.LoadedScene, out var sceneImages)) + sceneImages = Empty; + + var basePath = KSPUtil.ApplicationRootPath; + foreach (Texture2D tex in tex_array) + { + string name = tex.name; + if (name.Contains("/")) + name = name.Split('/').Last(); + + if (!Images.TryGetValue(name, out var info)) + info = null; + if (!sceneImages.TryGetValue(name, out var sceneInfo)) + sceneInfo = null; + + var replacement = GetMatchingReplacement(info, sceneInfo, tex); + 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) + { + if (cursors is null) + cursors = new TextureCursor[3]; + + cursors[cidx] = CreateCursor(replacement.path); + continue; + } + + // NavBall GaugeGee and GaugeThrottle needs special handling as well + if (name == "GaugeGee") + HarmonyPatches.GaugeGeeFilePath = replacement.path; + else if (name == "GaugeThrottle") + HarmonyPatches.GaugeThrottleFilePath = replacement.path; + else + { + if (replacement.cachedTextureBytes is null) + replacement.cachedTextureBytes = File.ReadAllBytes(replacement.path); + + tex.LoadImage(replacement.cachedTextureBytes); + } + } + + // Need to wait a small amount of time after scene load before you can set the cursor. + this.Invoke(SetCursor, 1f); + } + + private static SizedReplacementInfo GetMatchingReplacement( + ReplacementInfo info, + ReplacementInfo sceneInfo, + Texture2D tex + ) + { + if (info is null && sceneInfo is null) + return null; + + var rep = info?.GetMatchingReplacement(tex); + var sceneRep = sceneInfo?.GetMatchingReplacement(tex); + + if (rep != null && sceneRep != null) + { + if (rep.priority < sceneRep.priority) + return sceneRep; + } + + return rep ?? sceneRep; + } + + internal void LoadHUDColors() + { + UrlDir.UrlConfig[] configs = GameDatabase.Instance.GetConfigs(colorPathConfig); + if (configs.Length <= 0) + { + return; + } + configs = configs + .OrderByDescending(x => int.Parse(x.config.GetValue("priority"))) + .ToArray(); + List colorsSet = new List(); + foreach (UrlDir.UrlConfig configFile in configs) + { + int priority = int.Parse(configFile.config.GetValue("priority")); + + string TumblerColorPositive = "tumblerColorPositive"; + if (configFile.config.HasValue(TumblerColorPositive)) + { + if (!colorsSet.Contains(TumblerColorPositive)) + { + colorsSet.Add(TumblerColorPositive); + Color color = configFile.config.GetValue(TumblerColorPositive).ToRGBA(); + HarmonyPatches.TumblerColorReplacePositive = true; + HarmonyPatches.TumblerColorPositive = color; + } + } + string TumblerColorNegative = "tumblerColorNegative"; + if (configFile.config.HasValue(TumblerColorNegative)) + { + if (!colorsSet.Contains(TumblerColorNegative)) + { + colorsSet.Add(TumblerColorNegative); + Color color = configFile.config.GetValue(TumblerColorNegative).ToRGBA(); + HarmonyPatches.TumblerColorReplaceNegative = true; + HarmonyPatches.TumblerColorNegative = color; + } + } + + string PAWTitleBar = "PAWTitleBar"; + if (configFile.config.HasValue(PAWTitleBar)) + { + if (!colorsSet.Contains(PAWTitleBar)) + { + colorsSet.Add(PAWTitleBar); + HarmonyPatches.PAWTitleBar_color = configFile + .config.GetValue(PAWTitleBar) + .ToRGBA(); + HarmonyPatches.PAWTitleBar_replace = true; + } + } + + string PAWBlueButton = "PAWBlueButton"; + if (configFile.config.HasValue(PAWBlueButton)) + { + if (!colorsSet.Contains(PAWBlueButton)) + { + colorsSet.Add(PAWBlueButton); + HarmonyPatches.PAWBlueButton_replace = true; + HarmonyPatches.PAWBlueButton_color = configFile + .config.GetValue(PAWBlueButton) + .ToRGBA(); + } + } + + string PAWBlueButtonToggle = "PAWBlueButtonToggle"; + if (configFile.config.HasValue(PAWBlueButtonToggle)) + { + if (!colorsSet.Contains(PAWBlueButtonToggle)) + { + colorsSet.Add(PAWBlueButtonToggle); + HarmonyPatches.PAWBlueButtonToggle_replace = true; + HarmonyPatches.PAWBlueButtonToggle_color = configFile + .config.GetValue(PAWBlueButtonToggle) + .ToRGBA(); + } + } + + string PAWVariantSelectorNext = "PAWVariantSelectorNext"; + if (configFile.config.HasValue(PAWVariantSelectorNext)) + { + if (!colorsSet.Contains(PAWVariantSelectorNext)) + { + colorsSet.Add(PAWVariantSelectorNext); + HarmonyPatches.PAWVariantSelectorNext_replace = true; + HarmonyPatches.PAWVariantSelectorNext_color = configFile + .config.GetValue(PAWVariantSelectorNext) + .ToRGBA(); + } + } + + string PAWVariantSelectorPrevious = "PAWVariantSelectorPrevious"; + if (configFile.config.HasValue(PAWVariantSelectorPrevious)) + { + if (!colorsSet.Contains(PAWVariantSelectorPrevious)) + { + colorsSet.Add(PAWVariantSelectorPrevious); + HarmonyPatches.PAWVariantSelectorPrevious_replace = true; + HarmonyPatches.PAWVariantSelectorPrevious_color = configFile + .config.GetValue(PAWVariantSelectorPrevious) + .ToRGBA(); + } + } + + string PAWResourcePriorityIncrease = "PAWResourcePriorityIncrease"; + if (configFile.config.HasValue(PAWResourcePriorityIncrease)) + { + if (!colorsSet.Contains(PAWResourcePriorityIncrease)) + { + colorsSet.Add(PAWResourcePriorityIncrease); + HarmonyPatches.PAWResourcePriorityIncrease_replace = true; + HarmonyPatches.PAWResourcePriorityIncrease_color = configFile + .config.GetValue(PAWResourcePriorityIncrease) + .ToRGBA(); + } + } + + string PAWResourcePriorityDecrease = "PAWResourcePriorityDecrease"; + if (configFile.config.HasValue(PAWResourcePriorityDecrease)) + { + if (!colorsSet.Contains(PAWResourcePriorityDecrease)) + { + colorsSet.Add(PAWResourcePriorityDecrease); + HarmonyPatches.PAWResourcePriorityDecrease_replace = true; + HarmonyPatches.PAWResourcePriorityDecrease_color = configFile + .config.GetValue(PAWResourcePriorityDecrease) + .ToRGBA(); + } + } + + string PAWResourcePriorityReset = "PAWResourcePriorityReset"; + if (configFile.config.HasValue(PAWResourcePriorityReset)) + { + if (!colorsSet.Contains(PAWResourcePriorityReset)) + { + colorsSet.Add(PAWResourcePriorityReset); + HarmonyPatches.PAWResourcePriorityReset_replace = true; + HarmonyPatches.PAWResourcePriorityReset_color = configFile + .config.GetValue(PAWResourcePriorityReset) + .ToRGBA(); + } + } + + string PAWFuelSliderColor = "PAWFuelSliderColor"; + if (configFile.config.HasValue(PAWFuelSliderColor)) + { + if (!colorsSet.Contains(PAWFuelSliderColor)) + { + colorsSet.Add(PAWFuelSliderColor); + HarmonyPatches.PAWFuelSliderColor_replace = true; + HarmonyPatches.PAWFuelSliderColor = configFile + .config.GetValue(PAWFuelSliderColor) + .ToRGBA(); + } + } + + string PAWFuelSliderTextColor = "PAWFuelSliderTextColor"; + if (configFile.config.HasValue(PAWFuelSliderTextColor)) + { + if (!colorsSet.Contains(PAWFuelSliderTextColor)) + { + colorsSet.Add(PAWFuelSliderTextColor); + HarmonyPatches.PAWFuelSliderTextColor_replace = true; + HarmonyPatches.PAWFuelSliderTextColor = configFile + .config.GetValue(PAWFuelSliderTextColor) + .ToRGBA(); + } + } + + string KALTitleBar = "KALTitleBar"; + if (configFile.config.HasValue(KALTitleBar)) + { + if (!colorsSet.Contains(KALTitleBar)) + { + colorsSet.Add(KALTitleBar); + HarmonyPatches.KALTitleBar_color = configFile + .config.GetValue(KALTitleBar) + .ToRGBA(); + HarmonyPatches.KALTitleBar_replace = true; + } + } + + string gaugeNeedleYawPitchRoll = "gaugeNeedleYawPitchRoll"; + if (configFile.config.HasValue(gaugeNeedleYawPitchRoll)) + { + if (!colorsSet.Contains(gaugeNeedleYawPitchRoll)) + { + colorsSet.Add(gaugeNeedleYawPitchRoll); + HarmonyPatches.gaugeNeedleYawPitchRollColor = configFile + .config.GetValue(gaugeNeedleYawPitchRoll) + .ToRGBA(); + HarmonyPatches.gaugeNeedleYawPitchRollColor_replace = true; + } + } + + string gaugeNeedleYawPitchRollPrecision = "gaugeNeedleYawPitchRollPrecision"; + if (configFile.config.HasValue(gaugeNeedleYawPitchRollPrecision)) + { + if (!colorsSet.Contains(gaugeNeedleYawPitchRollPrecision)) + { + colorsSet.Add(gaugeNeedleYawPitchRollPrecision); + HarmonyPatches.gaugeNeedleYawPitchRollPrecisionColor = configFile + .config.GetValue(gaugeNeedleYawPitchRollPrecision) + .ToRGBA(); + HarmonyPatches.gaugeNeedleYawPitchRollPrecisionColor_replace = true; + } + } + + string METDisplayColorRed = "METDisplayColorRed"; + if (configFile.config.HasValue(METDisplayColorRed)) + { + if (!colorsSet.Contains(METDisplayColorRed)) + { + colorsSet.Add(METDisplayColorRed); + HarmonyPatches.METDisplayColorRed = configFile + .config.GetValue(METDisplayColorRed) + .ToRGBA(); + } + } + + string METDisplayColorYellow = "METDisplayColorYellow"; + if (configFile.config.HasValue(METDisplayColorYellow)) + { + if (!colorsSet.Contains(METDisplayColorYellow)) + { + colorsSet.Add(METDisplayColorYellow); + HarmonyPatches.METDisplayColorYellow = configFile + .config.GetValue(METDisplayColorYellow) + .ToRGBA(); + } + } + + string METDisplayColorGreen = "METDisplayColorGreen"; + if (configFile.config.HasValue(METDisplayColorGreen)) + { + if (!colorsSet.Contains(METDisplayColorGreen)) + { + colorsSet.Add(METDisplayColorGreen); + HarmonyPatches.METDisplayColorGreen = configFile + .config.GetValue(METDisplayColorGreen) + .ToRGBA(); + } + } + + string SpeedDisplayColorTextReplace = "speedDisplayColorText"; + if (configFile.config.HasValue(SpeedDisplayColorTextReplace)) + { + if (!colorsSet.Contains(SpeedDisplayColorTextReplace)) + { + colorsSet.Add(SpeedDisplayColorTextReplace); + HarmonyPatches.SpeedDisplayColorTextReplace = true; + HarmonyPatches.SpeedDisplayColorText = configFile + .config.GetValue(SpeedDisplayColorTextReplace) + .ToRGBA(); + } + } + + string SpeedDisplayColorSpeedReplace = "speedDisplayColorSpeed"; + if (configFile.config.HasValue(SpeedDisplayColorSpeedReplace)) + { + if (!colorsSet.Contains(SpeedDisplayColorSpeedReplace)) + { + colorsSet.Add(SpeedDisplayColorSpeedReplace); + HarmonyPatches.SpeedDisplayColorSpeedReplace = true; + HarmonyPatches.SpeedDisplayColorSpeed = configFile + .config.GetValue(SpeedDisplayColorSpeedReplace) + .ToRGBA(); + } + } + + string NavBallHeadingColor = "navBallHeadingColor"; + if (configFile.config.HasValue(NavBallHeadingColor)) + { + if (!colorsSet.Contains(NavBallHeadingColor)) + { + colorsSet.Add(NavBallHeadingColor); + HarmonyPatches.NavBallHeadingColorReplace = true; + HarmonyPatches.NavBallHeadingColor = configFile + .config.GetValue(NavBallHeadingColor) + .ToRGBA(); + } + } + + string StageTotalDeltaVColor = "stageTotalDeltaVColor"; + if (configFile.config.HasValue(StageTotalDeltaVColor)) + { + if (!colorsSet.Contains(StageTotalDeltaVColor)) + { + colorsSet.Add(StageTotalDeltaVColor); + HarmonyPatches.StageTotalDeltaVColorReplace = true; + HarmonyPatches.StageTotalDeltaVColor = configFile + .config.GetValue(StageTotalDeltaVColor) + .ToRGBA(); + } + } + + string StageGroupDeltaVTextColor = "stageGroupDeltaVTextColor"; + if (configFile.config.HasValue(StageGroupDeltaVTextColor)) + { + if (!colorsSet.Contains(StageGroupDeltaVTextColor)) + { + colorsSet.Add(StageGroupDeltaVTextColor); + HarmonyPatches.StageGroupDeltaVTextColorReplace = true; + HarmonyPatches.StageGroupDeltaVTextColor = configFile + .config.GetValue(StageGroupDeltaVTextColor) + .ToRGBA(); + } + } + + string StageGroupDeltaVNumberColor = "stageGroupDeltaVNumberColor"; + if (configFile.config.HasValue(StageGroupDeltaVNumberColor)) + { + if (!colorsSet.Contains(StageGroupDeltaVNumberColor)) + { + colorsSet.Add(StageGroupDeltaVNumberColor); + HarmonyPatches.StageGroupDeltaVNumberColorReplace = true; + HarmonyPatches.StageGroupDeltaVNumberColor = configFile + .config.GetValue(StageGroupDeltaVNumberColor) + .ToRGBA(); + } + } + + string StageGroupDeltaVBackgroundColor = "stageGroupDeltaVBackgroundColor"; + if (configFile.config.HasValue(StageGroupDeltaVBackgroundColor)) + { + if (!colorsSet.Contains(StageGroupDeltaVBackgroundColor)) + { + colorsSet.Add(StageGroupDeltaVBackgroundColor); + HarmonyPatches.StageGroupDeltaVBackgroundColorReplace = true; + HarmonyPatches.StageGroupDeltaVBackgroundColor = configFile + .config.GetValue(StageGroupDeltaVBackgroundColor) + .ToRGBA(); + } + } + + string StageEngineFuelGaugeTextColor = "stageEngineFuelGaugeTextColor"; + if (configFile.config.HasValue(StageEngineFuelGaugeTextColor)) + { + if (!colorsSet.Contains(StageEngineFuelGaugeTextColor)) + { + colorsSet.Add(StageEngineFuelGaugeTextColor); + HarmonyPatches.StageEngineFuelGaugeTextColor_replace = true; + HarmonyPatches.StageEngineFuelGaugeTextColor_color = configFile + .config.GetValue(StageEngineFuelGaugeTextColor) + .ToRGBA(); + } + } + + string StageEngineHeatGaugeTextColor = "stageEngineHeatGaugeTextColor"; + if (configFile.config.HasValue(StageEngineHeatGaugeTextColor)) + { + if (!colorsSet.Contains(StageEngineHeatGaugeTextColor)) + { + colorsSet.Add(StageEngineHeatGaugeTextColor); + HarmonyPatches.StageEngineHeatGaugeTextColor_replace = true; + HarmonyPatches.StageEngineHeatGaugeTextColor_color = configFile + .config.GetValue(StageEngineHeatGaugeTextColor) + .ToRGBA(); + } + } + + string StageEngineFuelGaugeBackgroundColor = "stageEngineFuelGaugeBackgroundColor"; + if (configFile.config.HasValue(StageEngineFuelGaugeBackgroundColor)) + { + if (!colorsSet.Contains(StageEngineFuelGaugeBackgroundColor)) + { + colorsSet.Add(StageEngineFuelGaugeBackgroundColor); + HarmonyPatches.StageEngineFuelGaugeBackgroundColor_replace = true; + HarmonyPatches.StageEngineFuelGaugeBackgroundColor_color = configFile + .config.GetValue(StageEngineFuelGaugeBackgroundColor) + .ToRGBA(); + } + } + + string StageEngineHeatGaugeBackgroundColor = "stageEngineHeatGaugeBackgroundColor"; + if (configFile.config.HasValue(StageEngineHeatGaugeBackgroundColor)) + { + if (!colorsSet.Contains(StageEngineHeatGaugeBackgroundColor)) + { + colorsSet.Add(StageEngineHeatGaugeBackgroundColor); + HarmonyPatches.StageEngineHeatGaugeBackgroundColor_replace = true; + HarmonyPatches.StageEngineHeatGaugeBackgroundColor_color = configFile + .config.GetValue(StageEngineHeatGaugeBackgroundColor) + .ToRGBA(); + } + } + + string StageEngineFuelGaugeFillColor = "stageEngineFuelGaugeFillColor"; + if (configFile.config.HasValue(StageEngineFuelGaugeFillColor)) + { + if (!colorsSet.Contains(StageEngineFuelGaugeFillColor)) + { + colorsSet.Add(StageEngineFuelGaugeFillColor); + HarmonyPatches.StageEngineFuelGaugeFillColor_replace = true; + HarmonyPatches.StageEngineFuelGaugeFillColor_color = configFile + .config.GetValue(StageEngineFuelGaugeFillColor) + .ToRGBA(); + } + } + + string StageEngineHeatGaugeFillColor = "stageEngineHeatGaugeFillColor"; + if (configFile.config.HasValue(StageEngineHeatGaugeFillColor)) + { + if (!colorsSet.Contains(StageEngineHeatGaugeFillColor)) + { + colorsSet.Add(StageEngineHeatGaugeFillColor); + HarmonyPatches.StageEngineHeatGaugeFillColor_replace = true; + HarmonyPatches.StageEngineHeatGaugeFillColor_color = configFile + .config.GetValue(StageEngineHeatGaugeFillColor) + .ToRGBA(); + } + } + + string StageEngineFuelGaugeFillBackgroundColor = + "stageEngineFuelGaugeFillBackgroundColor"; + if (configFile.config.HasValue(StageEngineFuelGaugeFillBackgroundColor)) + { + if (!colorsSet.Contains(StageEngineFuelGaugeFillBackgroundColor)) + { + colorsSet.Add(StageEngineFuelGaugeFillBackgroundColor); + HarmonyPatches.StageEngineFuelGaugeFillBackgroundColor_replace = true; + HarmonyPatches.StageEngineFuelGaugeFillBackgroundColor_color = configFile + .config.GetValue(StageEngineFuelGaugeFillBackgroundColor) + .ToRGBA(); + } + } + + string StageEngineHeatGaugeFillBackgroundColor = + "stageEngineHeatGaugeFillBackgroundColor"; + if (configFile.config.HasValue(StageEngineHeatGaugeFillBackgroundColor)) + { + if (!colorsSet.Contains(StageEngineHeatGaugeFillBackgroundColor)) + { + colorsSet.Add(StageEngineHeatGaugeFillBackgroundColor); + HarmonyPatches.StageEngineHeatGaugeFillBackgroundColor_replace = true; + HarmonyPatches.StageEngineHeatGaugeFillBackgroundColor_color = configFile + .config.GetValue(StageEngineHeatGaugeFillBackgroundColor) + .ToRGBA(); + } + } + + string NavBallCursor = "navballCursor"; + if (configFile.config.HasValue(NavBallCursor)) + { + if (!colorsSet.Contains(NavBallCursor)) + { + colorsSet.Add(NavBallCursor); + GameObject go = GameObject.Find( + "_UIMaster/MainCanvas/Flight/NavballFrame/IVAEVACollapseGroup/NavBallCursor" + ); + if (go != null) + { + Image img = go.GetComponentInChildren(); + if (img != null) + { + img.color = configFile.config.GetValue(NavBallCursor).ToRGBA(); + } + } + } + } + + string VerticalSpeedGaugeNeedle = "verticalSpeedGaugeNeedleColor"; + if (configFile.config.HasValue(VerticalSpeedGaugeNeedle)) + { + if (!colorsSet.Contains(VerticalSpeedGaugeNeedle)) + { + colorsSet.Add(VerticalSpeedGaugeNeedle); + HarmonyPatches.VerticalSpeedGaugeNeedleColorReplace = true; + HarmonyPatches.VerticalSpeedGaugeNeedleColor = configFile + .config.GetValue(VerticalSpeedGaugeNeedle) + .ToRGBA(); + } + } + + string ManeuverNodeEditorTextColor = "maneuverNodeEditorTextColor"; + if (configFile.config.HasValue(ManeuverNodeEditorTextColor)) + { + if (!colorsSet.Contains(ManeuverNodeEditorTextColor)) + { + colorsSet.Add(ManeuverNodeEditorTextColor); + HarmonyPatches.ManeuverNodeEditorTextColor_replace = true; + HarmonyPatches.ManeuverNodeEditorTextColor = configFile + .config.GetValue(ManeuverNodeEditorTextColor) + .ToRGBA(); + } + } + + string SASDisplayOnColor = "SASDisplayOnColor"; + if (configFile.config.HasValue(SASDisplayOnColor)) + { + if (!colorsSet.Contains(SASDisplayOnColor)) + { + colorsSet.Add(SASDisplayOnColor); + HarmonyPatches.SASDisplayColor_SAS_Replace_On = true; + HarmonyPatches.SASDisplayColor_SAS_On_color = configFile + .config.GetValue(SASDisplayOnColor) + .ToRGBA(); + } + } + + string SASDisplayOffColor = "SASDisplayOffColor"; + if (configFile.config.HasValue(SASDisplayOffColor)) + { + if (!colorsSet.Contains(SASDisplayOffColor)) + { + colorsSet.Add(SASDisplayOffColor); + HarmonyPatches.SASDisplayColor_SAS_Replace_Off = true; + HarmonyPatches.SASDisplayColor_SAS_Off_color = configFile + .config.GetValue(SASDisplayOffColor) + .ToRGBA(); + } + } + + string RCSDisplayOnColor = "RCSDisplayOnColor"; + if (configFile.config.HasValue(RCSDisplayOnColor)) + { + if (!colorsSet.Contains(RCSDisplayOnColor)) + { + colorsSet.Add(RCSDisplayOnColor); + HarmonyPatches.RCSDisplayColor_RCS_Replace_On = true; + HarmonyPatches.RCSDisplayColor_RCS_On_color = configFile + .config.GetValue(RCSDisplayOnColor) + .ToRGBA(); + } + } + + string RCSDisplayOffColor = "RCSDisplayOffColor"; + if (configFile.config.HasValue(RCSDisplayOffColor)) + { + if (!colorsSet.Contains(RCSDisplayOffColor)) + { + colorsSet.Add(RCSDisplayOffColor); + HarmonyPatches.RCSDisplayColor_RCS_Replace_Off = true; + HarmonyPatches.RCSDisplayColor_RCS_Off_color = configFile + .config.GetValue(RCSDisplayOffColor) + .ToRGBA(); + } + } + + string EditorCategoryButtonColor = "EditorCategoryButtonColor"; + if (configFile.config.HasValue(EditorCategoryButtonColor)) + { + if (!colorsSet.Contains(EditorCategoryButtonColor)) + { + colorsSet.Add(EditorCategoryButtonColor); + HarmonyPatches.EditorCategoryButtonColor_replace = true; + HarmonyPatches.EditorCategoryButtonColor_color = configFile + .config.GetValue(EditorCategoryButtonColor) + .ToRGBA(); + } + } + + string EditorCategoryButtonColor_Module = "EditorCategoryModuleButtonColor"; + if (configFile.config.HasValue(EditorCategoryButtonColor_Module)) + { + if (!colorsSet.Contains(EditorCategoryButtonColor_Module)) + { + colorsSet.Add(EditorCategoryButtonColor_Module); + HarmonyPatches.EditorCategoryButtonColor_Module_replace = true; + HarmonyPatches.EditorCategoryButtonColor_Module_color = configFile + .config.GetValue(EditorCategoryButtonColor_Module) + .ToRGBA(); + } + } + + string EditorCategoryButtonColor_Resource = "EditorCategoryResourceButtonColor"; + if (configFile.config.HasValue(EditorCategoryButtonColor_Resource)) + { + if (!colorsSet.Contains(EditorCategoryButtonColor_Resource)) + { + colorsSet.Add(EditorCategoryButtonColor_Resource); + HarmonyPatches.EditorCategoryButtonColor_Resource_replace = true; + HarmonyPatches.EditorCategoryButtonColor_Resource_color = configFile + .config.GetValue(EditorCategoryButtonColor_Resource) + .ToRGBA(); + } + } + + string EditorCategoryButtonColor_Manufacturer = "EditorCategoryManufacturerButtonColor"; + if (configFile.config.HasValue(EditorCategoryButtonColor_Manufacturer)) + { + if (!colorsSet.Contains(EditorCategoryButtonColor_Manufacturer)) + { + colorsSet.Add(EditorCategoryButtonColor_Manufacturer); + HarmonyPatches.EditorCategoryButtonColor_Manufacturer_replace = true; + HarmonyPatches.EditorCategoryButtonColor_Manufacturer_color = configFile + .config.GetValue(EditorCategoryButtonColor_Manufacturer) + .ToRGBA(); + } + } + + string EditorCategoryButtonColor_Tech = "EditorCategoryTechButtonColor"; + if (configFile.config.HasValue(EditorCategoryButtonColor_Tech)) + { + if (!colorsSet.Contains(EditorCategoryButtonColor_Tech)) + { + colorsSet.Add(EditorCategoryButtonColor_Tech); + HarmonyPatches.EditorCategoryButtonColor_Tech_replace = true; + HarmonyPatches.EditorCategoryButtonColor_Tech_color = configFile + .config.GetValue(EditorCategoryButtonColor_Tech) + .ToRGBA(); + } + } + + string EditorCategoryButtonColor_Profile = "EditorCategoryProfileButtonColor"; + if (configFile.config.HasValue(EditorCategoryButtonColor_Profile)) + { + if (!colorsSet.Contains(EditorCategoryButtonColor_Profile)) + { + colorsSet.Add(EditorCategoryButtonColor_Profile); + HarmonyPatches.EditorCategoryButtonColor_Profile_replace = true; + HarmonyPatches.EditorCategoryButtonColor_Profile_color = configFile + .config.GetValue(EditorCategoryButtonColor_Profile) + .ToRGBA(); + } + } + + string EditorCategoryButtonColor_Subassembly = "EditorCategorySubassemblyButtonColor"; + if (configFile.config.HasValue(EditorCategoryButtonColor_Subassembly)) + { + if (!colorsSet.Contains(EditorCategoryButtonColor_Subassembly)) + { + colorsSet.Add(EditorCategoryButtonColor_Subassembly); + HarmonyPatches.EditorCategoryButtonColor_Subassembly_replace = true; + HarmonyPatches.EditorCategoryButtonColor_Subassembly_color = configFile + .config.GetValue(EditorCategoryButtonColor_Subassembly) + .ToRGBA(); + } + } + + string EditorCategoryButtonColor_Variants = "EditorCategoryVariantsButtonColor"; + if (configFile.config.HasValue(EditorCategoryButtonColor_Variants)) + { + if (!colorsSet.Contains(EditorCategoryButtonColor_Variants)) + { + colorsSet.Add(EditorCategoryButtonColor_Variants); + HarmonyPatches.EditorCategoryButtonColor_Variants_replace = true; + HarmonyPatches.EditorCategoryButtonColor_Variants_color = configFile + .config.GetValue(EditorCategoryButtonColor_Variants) + .ToRGBA(); + } + } + + string EditorCategoryButtonColor_Custom = "EditorCategoryCustomButtonColor"; + if (configFile.config.HasValue(EditorCategoryButtonColor_Custom)) + { + if (!colorsSet.Contains(EditorCategoryButtonColor_Custom)) + { + colorsSet.Add(EditorCategoryButtonColor_Custom); + HarmonyPatches.EditorCategoryButtonColor_Custom_replace = true; + HarmonyPatches.EditorCategoryButtonColor_Custom_color = configFile + .config.GetValue(EditorCategoryButtonColor_Custom) + .ToRGBA(); + } + } + } + } + + /* + internal static void LoadTumblerColors() + { + UrlDir.UrlConfig[] configs = GameDatabase.Instance.GetConfigs(colorPathConfig); + if (configs.Length <= 0) + { + return; + } + configs = configs.OrderByDescending(x => int.Parse(x.config.GetValue("priority"))).ToArray(); + List colorsSet = new List(); + foreach (UrlDir.UrlConfig configFile in configs) + { + int priority = int.Parse(configFile.config.GetValue("priority")); + + + string TumblerColorPositive = "tumblerColorPositive"; + if (configFile.config.HasValue(TumblerColorPositive)) + { + if (!colorsSet.Contains(TumblerColorPositive)) + { + colorsSet.Add(TumblerColorPositive); + Color color = configFile.config.GetValue(TumblerColorPositive).ToRGBA(); + HarmonyPatches.TumblerColorReplacePositive = true; + HarmonyPatches.TumblerColorPositive = color; + } + } + string TumblerColorNegative = "tumblerColorNegative"; + if (configFile.config.HasValue(TumblerColorNegative)) + { + if (!colorsSet.Contains(TumblerColorNegative)) + { + colorsSet.Add(TumblerColorNegative); + Color color = configFile.config.GetValue(TumblerColorNegative).ToRGBA(); + HarmonyPatches.TumblerColorReplaceNegative = true; + HarmonyPatches.TumblerColorNegative = color; + } + } + } + HarmonyPatches.TumblerColorsLoaded = true; + } + */ + private void SetCursor() + { + if (cursors != null && cursors[0] != null) { - instance = this; - Debug.Log("HUDReplacer: Running scene change. " + HighLogic.LoadedScene); - - if (Images is null) - LoadTextures(); - - if (Images.Count != 0 && SceneImages.Count != 0) - { - Debug.Log("HUDReplacer: Replacing textures..."); - ReplaceTextures(); - Debug.Log("HUDReplacer: Textures have been replaced!"); - } - - LoadHUDColors(); - } - - public void Update() - { - if(enableDebug) - { - if (Input.GetKeyUp(KeyCode.E)) - { - Debug.Log("HUDReplacer: Dumping list of loaded texture2D objects..."); - Texture2D[] tex_array = (Texture2D[])(object)Resources.FindObjectsOfTypeAll(typeof(Texture2D)); - foreach (Texture2D tex in tex_array) - { - Debug.Log(tex.name + " - WxH=" + tex.width + "x" + tex.height); - } - Debug.Log("HUDReplacer: Dumping finished."); - } - if (Input.GetKeyUp(KeyCode.Q)) - { - LoadTextures(); - ReplaceTextures(); - LoadHUDColors(); - Debug.Log("HUDReplacer: Refreshed."); - } - if (Input.GetKeyUp(KeyCode.D)) - { - - PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current); - eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y); - - List results = new List(); - EventSystem.current.RaycastAll(eventDataCurrentPosition, results); - - Debug.Log("HUDReplacer: [][][][][][][][][][][][][][][][][]"); - foreach (RaycastResult result in results) - { - try - { - Image img = result.gameObject.GetComponent(); - //Debug.Log("HUDReplacer: ------"); - Debug.Log("Image.mainTexture.name: " + img.mainTexture.name + " - WxH=" + img.mainTexture.width + "x" + img.mainTexture.height); - Debug.Log("Image.sprite.texture.name: " + img.sprite.texture.name + " - WxH=" + img.sprite.texture.width + "x" + img.sprite.texture.height); - Debug.Log("HUDReplacer: ------"); - if(img.mainTexture.name == "app_divider_pulldown_header" || img.mainTexture.name == "app_divider_pulldown_header_over") - { - //result.gameObject.GetComponent().SetColor(Color.red); - } - //Texture2D tex = (Texture2D)img.mainTexture; - //DumpTexture(tex); - } - catch (Exception e) - { - Debug.Log(e.ToString()); - } - } - } - /* - It might be possible for a future update to allow modifying the sprite borders. - This would allow you to specify a "9-slice" size, which would fix the issue of improperly scaling the texture and causing blurry/distorted artifacts. - Currently not feasible, as a workaround for the readonly border property needs to be found. - More info: https://docs.unity3d.com/Manual/9SliceSprites.html - - if (Input.GetKeyUp(KeyCode.A)) - { - Texture2D[] tex_array = (Texture2D[])(object)Resources.FindObjectsOfTypeAll(typeof(Texture2D)); - Sprite[] sprite_array = (Sprite[])(object)Resources.FindObjectsOfTypeAll(typeof(Sprite)); - foreach(Sprite sprite in sprite_array) - { - if(sprite.name == "rect_round_color") - { - sprite.border = new Vector4(1,1,1,1); - } - } - Debug.Log("finished"); - } - */ - } - - } - - // This gets called by ModuleManager once it has finished applying all - // patches. If MM is not installed then we'll call LoadTextures in Awake - // instead. - public static void ModuleManagerPostLoad() - { - LoadTextures(); - } - - static void LoadTextures() - { - Images = new Dictionary(); - SceneImages = new Dictionary>(); - - UrlDir.UrlConfig[] configs = GameDatabase.Instance.GetConfigs(filePathConfig) - .OrderByDescending((configFile) => - { - int priority = 0; - configFile.config.TryGetValue("priority", ref priority); - return priority; - }) - .ToArray(); - - if (configs.Length == 0) - { - Debug.Log("HUDReplacer: No texture configs found."); - return; - } - - foreach (var configFile in configs) - { - var config = configFile.config; - var filePath = config.GetValue("filePath"); - - string onScene = null; - Dictionary replacements = Images; - if (config.TryGetValue("onScene", ref onScene)) - { - if (!Enum.TryParse(onScene, out GameScenes scene)) - { - Debug.LogError($"HUDReplacer: Config {configFile.url} contained invalid onScene value {onScene ?? ""}"); - continue; - } - - if (!SceneImages.TryGetValue(scene, out replacements)) - { - replacements = new Dictionary(); - SceneImages.Add(scene, replacements); - } - } - - int priority = 0; - if (!config.TryGetValue("priority", ref priority)) - { - Debug.LogError($"HUDReplacer: config at {configFile.url} is missing a priority key and will not be loaded"); - 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) - { - var relpath = filename; - if (relpath.StartsWith(basePath)) - relpath = relpath.Substring(basePath.Length); - - Debug.Log($"HUDReplacer: Found file {relpath}"); - - int width = 0; - int height = 0; - - string basename = Path.GetFileNameWithoutExtension(filename); - int index = basename.LastIndexOf('#'); - if (index != -1) - { - string size = basename.Substring(index + 1); - basename = basename.Substring(0, index); - - index = size.IndexOf('x'); - if (index == -1 - || !int.TryParse(size.Substring(0, index), out width) - || !int.TryParse(size.Substring(index + 1), out height)) - { - Debug.LogError($"HUDReplacer: filename {filename} was not in the expected format. It needs to be either `name.png` or `name#x.png`"); - continue; - } - } - - SizedReplacementInfo info = new SizedReplacementInfo - { - priority = priority, - width = width, - height = height, - path = filename - }; - - if (!replacements.TryGetValue(basename, out var replacement)) - { - replacement = new ReplacementInfo - { - replacements = new List(1) - }; - 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); - } - } - } - - internal void ReplaceTextures() - { - if (Images.Count == 0 && SceneImages.Count == 0) - return; - - Texture2D[] tex_array = (Texture2D[])(object)Resources.FindObjectsOfTypeAll(typeof(Texture2D)); - ReplaceTextures(tex_array); - } - internal void ReplaceTextures(Texture2D[] tex_array) - { - if (Images.Count == 0 && SceneImages.Count == 0) - return; - - // Get the overloads specific to the current scene but if there are - // then we just use an empty dictionary. - if (!SceneImages.TryGetValue(HighLogic.LoadedScene, out var sceneImages)) - sceneImages = Empty; - - var basePath = KSPUtil.ApplicationRootPath; - foreach (Texture2D tex in tex_array) - { - string name = tex.name; - if (name.Contains("/")) - name = name.Split('/').Last(); - - if (!Images.TryGetValue(name, out var info)) - info = null; - if (!sceneImages.TryGetValue(name, out var sceneInfo)) - sceneInfo = null; - - var replacement = GetMatchingReplacement(info, sceneInfo, tex); - 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) - { - if (cursors is null) - cursors = new TextureCursor[3]; - - cursors[cidx] = CreateCursor(replacement.path); - continue; - } - - // NavBall GaugeGee and GaugeThrottle needs special handling as well - if (name == "GaugeGee") - HarmonyPatches.GaugeGeeFilePath = replacement.path; - else if (name == "GaugeThrottle") - HarmonyPatches.GaugeThrottleFilePath = replacement.path; - else - { - if (replacement.cachedTextureBytes is null) - replacement.cachedTextureBytes = File.ReadAllBytes(replacement.path); - - tex.LoadImage(replacement.cachedTextureBytes); - } - } - - // Need to wait a small amount of time after scene load before you can set the cursor. - this.Invoke(SetCursor, 1f); - } - - private static SizedReplacementInfo GetMatchingReplacement( - ReplacementInfo info, - ReplacementInfo sceneInfo, - Texture2D tex - ) - { - if (info is null && sceneInfo is null) - return null; - - var rep = info?.GetMatchingReplacement(tex); - var sceneRep = sceneInfo?.GetMatchingReplacement(tex); - - if (rep != null && sceneRep != null) - { - if (rep.priority < sceneRep.priority) - return sceneRep; - } - - return rep ?? sceneRep; - } - - internal void LoadHUDColors() - { - UrlDir.UrlConfig[] configs = GameDatabase.Instance.GetConfigs(colorPathConfig); - if (configs.Length <= 0) - { - return; - } - configs = configs.OrderByDescending(x => int.Parse(x.config.GetValue("priority"))).ToArray(); - List colorsSet = new List(); - foreach (UrlDir.UrlConfig configFile in configs) - { - int priority = int.Parse(configFile.config.GetValue("priority")); - - string TumblerColorPositive = "tumblerColorPositive"; - if (configFile.config.HasValue(TumblerColorPositive)) - { - if (!colorsSet.Contains(TumblerColorPositive)) - { - colorsSet.Add(TumblerColorPositive); - Color color = configFile.config.GetValue(TumblerColorPositive).ToRGBA(); - HarmonyPatches.TumblerColorReplacePositive = true; - HarmonyPatches.TumblerColorPositive = color; - } - } - string TumblerColorNegative = "tumblerColorNegative"; - if (configFile.config.HasValue(TumblerColorNegative)) - { - if (!colorsSet.Contains(TumblerColorNegative)) - { - colorsSet.Add(TumblerColorNegative); - Color color = configFile.config.GetValue(TumblerColorNegative).ToRGBA(); - HarmonyPatches.TumblerColorReplaceNegative = true; - HarmonyPatches.TumblerColorNegative = color; - } - } - - string PAWTitleBar = "PAWTitleBar"; - if (configFile.config.HasValue(PAWTitleBar)) - { - if (!colorsSet.Contains(PAWTitleBar)) - { - colorsSet.Add(PAWTitleBar); - HarmonyPatches.PAWTitleBar_color = configFile.config.GetValue(PAWTitleBar).ToRGBA(); - HarmonyPatches.PAWTitleBar_replace = true; - } - } - - string PAWBlueButton = "PAWBlueButton"; - if (configFile.config.HasValue(PAWBlueButton)) - { - if (!colorsSet.Contains(PAWBlueButton)) - { - colorsSet.Add(PAWBlueButton); - HarmonyPatches.PAWBlueButton_replace = true; - HarmonyPatches.PAWBlueButton_color = configFile.config.GetValue(PAWBlueButton).ToRGBA(); - } - } - - string PAWBlueButtonToggle = "PAWBlueButtonToggle"; - if (configFile.config.HasValue(PAWBlueButtonToggle)) - { - if (!colorsSet.Contains(PAWBlueButtonToggle)) - { - colorsSet.Add(PAWBlueButtonToggle); - HarmonyPatches.PAWBlueButtonToggle_replace = true; - HarmonyPatches.PAWBlueButtonToggle_color = configFile.config.GetValue(PAWBlueButtonToggle).ToRGBA(); - } - } - - string PAWVariantSelectorNext = "PAWVariantSelectorNext"; - if (configFile.config.HasValue(PAWVariantSelectorNext)) - { - if (!colorsSet.Contains(PAWVariantSelectorNext)) - { - colorsSet.Add(PAWVariantSelectorNext); - HarmonyPatches.PAWVariantSelectorNext_replace = true; - HarmonyPatches.PAWVariantSelectorNext_color = configFile.config.GetValue(PAWVariantSelectorNext).ToRGBA(); - } - } - - string PAWVariantSelectorPrevious = "PAWVariantSelectorPrevious"; - if (configFile.config.HasValue(PAWVariantSelectorPrevious)) - { - if (!colorsSet.Contains(PAWVariantSelectorPrevious)) - { - colorsSet.Add(PAWVariantSelectorPrevious); - HarmonyPatches.PAWVariantSelectorPrevious_replace = true; - HarmonyPatches.PAWVariantSelectorPrevious_color = configFile.config.GetValue(PAWVariantSelectorPrevious).ToRGBA(); - } - } - - string PAWResourcePriorityIncrease = "PAWResourcePriorityIncrease"; - if (configFile.config.HasValue(PAWResourcePriorityIncrease)) - { - if (!colorsSet.Contains(PAWResourcePriorityIncrease)) - { - colorsSet.Add(PAWResourcePriorityIncrease); - HarmonyPatches.PAWResourcePriorityIncrease_replace = true; - HarmonyPatches.PAWResourcePriorityIncrease_color = configFile.config.GetValue(PAWResourcePriorityIncrease).ToRGBA(); - } - } - - string PAWResourcePriorityDecrease = "PAWResourcePriorityDecrease"; - if (configFile.config.HasValue(PAWResourcePriorityDecrease)) - { - if (!colorsSet.Contains(PAWResourcePriorityDecrease)) - { - colorsSet.Add(PAWResourcePriorityDecrease); - HarmonyPatches.PAWResourcePriorityDecrease_replace = true; - HarmonyPatches.PAWResourcePriorityDecrease_color = configFile.config.GetValue(PAWResourcePriorityDecrease).ToRGBA(); - } - } - - string PAWResourcePriorityReset = "PAWResourcePriorityReset"; - if (configFile.config.HasValue(PAWResourcePriorityReset)) - { - if (!colorsSet.Contains(PAWResourcePriorityReset)) - { - colorsSet.Add(PAWResourcePriorityReset); - HarmonyPatches.PAWResourcePriorityReset_replace = true; - HarmonyPatches.PAWResourcePriorityReset_color = configFile.config.GetValue(PAWResourcePriorityReset).ToRGBA(); - } - } - - string PAWFuelSliderColor = "PAWFuelSliderColor"; - if (configFile.config.HasValue(PAWFuelSliderColor)) - { - if (!colorsSet.Contains(PAWFuelSliderColor)) - { - colorsSet.Add(PAWFuelSliderColor); - HarmonyPatches.PAWFuelSliderColor_replace = true; - HarmonyPatches.PAWFuelSliderColor = configFile.config.GetValue(PAWFuelSliderColor).ToRGBA(); - } - } - - string PAWFuelSliderTextColor = "PAWFuelSliderTextColor"; - if (configFile.config.HasValue(PAWFuelSliderTextColor)) - { - if (!colorsSet.Contains(PAWFuelSliderTextColor)) - { - colorsSet.Add(PAWFuelSliderTextColor); - HarmonyPatches.PAWFuelSliderTextColor_replace = true; - HarmonyPatches.PAWFuelSliderTextColor = configFile.config.GetValue(PAWFuelSliderTextColor).ToRGBA(); - } - } - - string KALTitleBar = "KALTitleBar"; - if (configFile.config.HasValue(KALTitleBar)) - { - if (!colorsSet.Contains(KALTitleBar)) - { - colorsSet.Add(KALTitleBar); - HarmonyPatches.KALTitleBar_color = configFile.config.GetValue(KALTitleBar).ToRGBA(); - HarmonyPatches.KALTitleBar_replace = true; - } - } - - string gaugeNeedleYawPitchRoll = "gaugeNeedleYawPitchRoll"; - if (configFile.config.HasValue(gaugeNeedleYawPitchRoll)) - { - if (!colorsSet.Contains(gaugeNeedleYawPitchRoll)) - { - colorsSet.Add(gaugeNeedleYawPitchRoll); - HarmonyPatches.gaugeNeedleYawPitchRollColor = configFile.config.GetValue(gaugeNeedleYawPitchRoll).ToRGBA(); - HarmonyPatches.gaugeNeedleYawPitchRollColor_replace = true; - } - } - - string gaugeNeedleYawPitchRollPrecision = "gaugeNeedleYawPitchRollPrecision"; - if (configFile.config.HasValue(gaugeNeedleYawPitchRollPrecision)) - { - if (!colorsSet.Contains(gaugeNeedleYawPitchRollPrecision)) - { - colorsSet.Add(gaugeNeedleYawPitchRollPrecision); - HarmonyPatches.gaugeNeedleYawPitchRollPrecisionColor = configFile.config.GetValue(gaugeNeedleYawPitchRollPrecision).ToRGBA(); - HarmonyPatches.gaugeNeedleYawPitchRollPrecisionColor_replace = true; - } - } - - string METDisplayColorRed = "METDisplayColorRed"; - if (configFile.config.HasValue(METDisplayColorRed)) - { - if (!colorsSet.Contains(METDisplayColorRed)) - { - colorsSet.Add(METDisplayColorRed); - HarmonyPatches.METDisplayColorRed = configFile.config.GetValue(METDisplayColorRed).ToRGBA(); - } - } - - string METDisplayColorYellow = "METDisplayColorYellow"; - if (configFile.config.HasValue(METDisplayColorYellow)) - { - if (!colorsSet.Contains(METDisplayColorYellow)) - { - colorsSet.Add(METDisplayColorYellow); - HarmonyPatches.METDisplayColorYellow = configFile.config.GetValue(METDisplayColorYellow).ToRGBA(); - } - } - - string METDisplayColorGreen = "METDisplayColorGreen"; - if (configFile.config.HasValue(METDisplayColorGreen)) - { - if (!colorsSet.Contains(METDisplayColorGreen)) - { - colorsSet.Add(METDisplayColorGreen); - HarmonyPatches.METDisplayColorGreen = configFile.config.GetValue(METDisplayColorGreen).ToRGBA(); - } - } - - string SpeedDisplayColorTextReplace = "speedDisplayColorText"; - if (configFile.config.HasValue(SpeedDisplayColorTextReplace)) - { - if (!colorsSet.Contains(SpeedDisplayColorTextReplace)) - { - colorsSet.Add(SpeedDisplayColorTextReplace); - HarmonyPatches.SpeedDisplayColorTextReplace = true; - HarmonyPatches.SpeedDisplayColorText = configFile.config.GetValue(SpeedDisplayColorTextReplace).ToRGBA(); - } - } - - string SpeedDisplayColorSpeedReplace = "speedDisplayColorSpeed"; - if (configFile.config.HasValue(SpeedDisplayColorSpeedReplace)) - { - if (!colorsSet.Contains(SpeedDisplayColorSpeedReplace)) - { - colorsSet.Add(SpeedDisplayColorSpeedReplace); - HarmonyPatches.SpeedDisplayColorSpeedReplace = true; - HarmonyPatches.SpeedDisplayColorSpeed = configFile.config.GetValue(SpeedDisplayColorSpeedReplace).ToRGBA(); - } - } - - string NavBallHeadingColor = "navBallHeadingColor"; - if (configFile.config.HasValue(NavBallHeadingColor)) - { - if (!colorsSet.Contains(NavBallHeadingColor)) - { - colorsSet.Add(NavBallHeadingColor); - HarmonyPatches.NavBallHeadingColorReplace = true; - HarmonyPatches.NavBallHeadingColor = configFile.config.GetValue(NavBallHeadingColor).ToRGBA(); - } - } - - string StageTotalDeltaVColor = "stageTotalDeltaVColor"; - if (configFile.config.HasValue(StageTotalDeltaVColor)) - { - if (!colorsSet.Contains(StageTotalDeltaVColor)) - { - colorsSet.Add(StageTotalDeltaVColor); - HarmonyPatches.StageTotalDeltaVColorReplace = true; - HarmonyPatches.StageTotalDeltaVColor = configFile.config.GetValue(StageTotalDeltaVColor).ToRGBA(); - } - } - - string StageGroupDeltaVTextColor = "stageGroupDeltaVTextColor"; - if (configFile.config.HasValue(StageGroupDeltaVTextColor)) - { - if (!colorsSet.Contains(StageGroupDeltaVTextColor)) - { - colorsSet.Add(StageGroupDeltaVTextColor); - HarmonyPatches.StageGroupDeltaVTextColorReplace = true; - HarmonyPatches.StageGroupDeltaVTextColor = configFile.config.GetValue(StageGroupDeltaVTextColor).ToRGBA(); - } - } - - string StageGroupDeltaVNumberColor = "stageGroupDeltaVNumberColor"; - if (configFile.config.HasValue(StageGroupDeltaVNumberColor)) - { - if (!colorsSet.Contains(StageGroupDeltaVNumberColor)) - { - colorsSet.Add(StageGroupDeltaVNumberColor); - HarmonyPatches.StageGroupDeltaVNumberColorReplace = true; - HarmonyPatches.StageGroupDeltaVNumberColor = configFile.config.GetValue(StageGroupDeltaVNumberColor).ToRGBA(); - } - } - - string StageGroupDeltaVBackgroundColor = "stageGroupDeltaVBackgroundColor"; - if (configFile.config.HasValue(StageGroupDeltaVBackgroundColor)) - { - if (!colorsSet.Contains(StageGroupDeltaVBackgroundColor)) - { - colorsSet.Add(StageGroupDeltaVBackgroundColor); - HarmonyPatches.StageGroupDeltaVBackgroundColorReplace = true; - HarmonyPatches.StageGroupDeltaVBackgroundColor = configFile.config.GetValue(StageGroupDeltaVBackgroundColor).ToRGBA(); - } - } - - string StageEngineFuelGaugeTextColor = "stageEngineFuelGaugeTextColor"; - if (configFile.config.HasValue(StageEngineFuelGaugeTextColor)) - { - if (!colorsSet.Contains(StageEngineFuelGaugeTextColor)) - { - colorsSet.Add(StageEngineFuelGaugeTextColor); - HarmonyPatches.StageEngineFuelGaugeTextColor_replace = true; - HarmonyPatches.StageEngineFuelGaugeTextColor_color = configFile.config.GetValue(StageEngineFuelGaugeTextColor).ToRGBA(); - } - } - - string StageEngineHeatGaugeTextColor = "stageEngineHeatGaugeTextColor"; - if (configFile.config.HasValue(StageEngineHeatGaugeTextColor)) - { - if (!colorsSet.Contains(StageEngineHeatGaugeTextColor)) - { - colorsSet.Add(StageEngineHeatGaugeTextColor); - HarmonyPatches.StageEngineHeatGaugeTextColor_replace = true; - HarmonyPatches.StageEngineHeatGaugeTextColor_color = configFile.config.GetValue(StageEngineHeatGaugeTextColor).ToRGBA(); - } - } - - string StageEngineFuelGaugeBackgroundColor = "stageEngineFuelGaugeBackgroundColor"; - if (configFile.config.HasValue(StageEngineFuelGaugeBackgroundColor)) - { - if (!colorsSet.Contains(StageEngineFuelGaugeBackgroundColor)) - { - colorsSet.Add(StageEngineFuelGaugeBackgroundColor); - HarmonyPatches.StageEngineFuelGaugeBackgroundColor_replace = true; - HarmonyPatches.StageEngineFuelGaugeBackgroundColor_color = configFile.config.GetValue(StageEngineFuelGaugeBackgroundColor).ToRGBA(); - } - } - - string StageEngineHeatGaugeBackgroundColor = "stageEngineHeatGaugeBackgroundColor"; - if (configFile.config.HasValue(StageEngineHeatGaugeBackgroundColor)) - { - if (!colorsSet.Contains(StageEngineHeatGaugeBackgroundColor)) - { - colorsSet.Add(StageEngineHeatGaugeBackgroundColor); - HarmonyPatches.StageEngineHeatGaugeBackgroundColor_replace = true; - HarmonyPatches.StageEngineHeatGaugeBackgroundColor_color = configFile.config.GetValue(StageEngineHeatGaugeBackgroundColor).ToRGBA(); - } - } - - string StageEngineFuelGaugeFillColor = "stageEngineFuelGaugeFillColor"; - if (configFile.config.HasValue(StageEngineFuelGaugeFillColor)) - { - if (!colorsSet.Contains(StageEngineFuelGaugeFillColor)) - { - colorsSet.Add(StageEngineFuelGaugeFillColor); - HarmonyPatches.StageEngineFuelGaugeFillColor_replace = true; - HarmonyPatches.StageEngineFuelGaugeFillColor_color = configFile.config.GetValue(StageEngineFuelGaugeFillColor).ToRGBA(); - } - } - - string StageEngineHeatGaugeFillColor = "stageEngineHeatGaugeFillColor"; - if (configFile.config.HasValue(StageEngineHeatGaugeFillColor)) - { - if (!colorsSet.Contains(StageEngineHeatGaugeFillColor)) - { - colorsSet.Add(StageEngineHeatGaugeFillColor); - HarmonyPatches.StageEngineHeatGaugeFillColor_replace = true; - HarmonyPatches.StageEngineHeatGaugeFillColor_color = configFile.config.GetValue(StageEngineHeatGaugeFillColor).ToRGBA(); - } - } - - string StageEngineFuelGaugeFillBackgroundColor = "stageEngineFuelGaugeFillBackgroundColor"; - if (configFile.config.HasValue(StageEngineFuelGaugeFillBackgroundColor)) - { - if (!colorsSet.Contains(StageEngineFuelGaugeFillBackgroundColor)) - { - colorsSet.Add(StageEngineFuelGaugeFillBackgroundColor); - HarmonyPatches.StageEngineFuelGaugeFillBackgroundColor_replace = true; - HarmonyPatches.StageEngineFuelGaugeFillBackgroundColor_color = configFile.config.GetValue(StageEngineFuelGaugeFillBackgroundColor).ToRGBA(); - } - } - - string StageEngineHeatGaugeFillBackgroundColor = "stageEngineHeatGaugeFillBackgroundColor"; - if (configFile.config.HasValue(StageEngineHeatGaugeFillBackgroundColor)) - { - if (!colorsSet.Contains(StageEngineHeatGaugeFillBackgroundColor)) - { - colorsSet.Add(StageEngineHeatGaugeFillBackgroundColor); - HarmonyPatches.StageEngineHeatGaugeFillBackgroundColor_replace = true; - HarmonyPatches.StageEngineHeatGaugeFillBackgroundColor_color = configFile.config.GetValue(StageEngineHeatGaugeFillBackgroundColor).ToRGBA(); - } - } - - string NavBallCursor = "navballCursor"; - if (configFile.config.HasValue(NavBallCursor)) - { - if (!colorsSet.Contains(NavBallCursor)) - { - colorsSet.Add(NavBallCursor); - GameObject go = GameObject.Find("_UIMaster/MainCanvas/Flight/NavballFrame/IVAEVACollapseGroup/NavBallCursor"); - if (go != null) - { - Image img = go.GetComponentInChildren(); - if (img != null) - { - img.color = configFile.config.GetValue(NavBallCursor).ToRGBA(); - } - } - } - } - - string VerticalSpeedGaugeNeedle = "verticalSpeedGaugeNeedleColor"; - if (configFile.config.HasValue(VerticalSpeedGaugeNeedle)) - { - if (!colorsSet.Contains(VerticalSpeedGaugeNeedle)) - { - colorsSet.Add(VerticalSpeedGaugeNeedle); - HarmonyPatches.VerticalSpeedGaugeNeedleColorReplace = true; - HarmonyPatches.VerticalSpeedGaugeNeedleColor = configFile.config.GetValue(VerticalSpeedGaugeNeedle).ToRGBA(); - } - } - - string ManeuverNodeEditorTextColor = "maneuverNodeEditorTextColor"; - if (configFile.config.HasValue(ManeuverNodeEditorTextColor)) - { - if (!colorsSet.Contains(ManeuverNodeEditorTextColor)) - { - colorsSet.Add(ManeuverNodeEditorTextColor); - HarmonyPatches.ManeuverNodeEditorTextColor_replace = true; - HarmonyPatches.ManeuverNodeEditorTextColor = configFile.config.GetValue(ManeuverNodeEditorTextColor).ToRGBA(); - } - } - - string SASDisplayOnColor = "SASDisplayOnColor"; - if (configFile.config.HasValue(SASDisplayOnColor)) - { - if (!colorsSet.Contains(SASDisplayOnColor)) - { - colorsSet.Add(SASDisplayOnColor); - HarmonyPatches.SASDisplayColor_SAS_Replace_On = true; - HarmonyPatches.SASDisplayColor_SAS_On_color = configFile.config.GetValue(SASDisplayOnColor).ToRGBA(); - } - } - - string SASDisplayOffColor = "SASDisplayOffColor"; - if (configFile.config.HasValue(SASDisplayOffColor)) - { - if (!colorsSet.Contains(SASDisplayOffColor)) - { - colorsSet.Add(SASDisplayOffColor); - HarmonyPatches.SASDisplayColor_SAS_Replace_Off = true; - HarmonyPatches.SASDisplayColor_SAS_Off_color = configFile.config.GetValue(SASDisplayOffColor).ToRGBA(); - } - } - - string RCSDisplayOnColor = "RCSDisplayOnColor"; - if (configFile.config.HasValue(RCSDisplayOnColor)) - { - if (!colorsSet.Contains(RCSDisplayOnColor)) - { - colorsSet.Add(RCSDisplayOnColor); - HarmonyPatches.RCSDisplayColor_RCS_Replace_On = true; - HarmonyPatches.RCSDisplayColor_RCS_On_color = configFile.config.GetValue(RCSDisplayOnColor).ToRGBA(); - } - } - - string RCSDisplayOffColor = "RCSDisplayOffColor"; - if (configFile.config.HasValue(RCSDisplayOffColor)) - { - if (!colorsSet.Contains(RCSDisplayOffColor)) - { - colorsSet.Add(RCSDisplayOffColor); - HarmonyPatches.RCSDisplayColor_RCS_Replace_Off = true; - HarmonyPatches.RCSDisplayColor_RCS_Off_color = configFile.config.GetValue(RCSDisplayOffColor).ToRGBA(); - } - } - - string EditorCategoryButtonColor = "EditorCategoryButtonColor"; - if (configFile.config.HasValue(EditorCategoryButtonColor)) - { - if (!colorsSet.Contains(EditorCategoryButtonColor)) - { - colorsSet.Add(EditorCategoryButtonColor); - HarmonyPatches.EditorCategoryButtonColor_replace = true; - HarmonyPatches.EditorCategoryButtonColor_color = configFile.config.GetValue(EditorCategoryButtonColor).ToRGBA(); - } - } - - string EditorCategoryButtonColor_Module = "EditorCategoryModuleButtonColor"; - if (configFile.config.HasValue(EditorCategoryButtonColor_Module)) - { - if (!colorsSet.Contains(EditorCategoryButtonColor_Module)) - { - colorsSet.Add(EditorCategoryButtonColor_Module); - HarmonyPatches.EditorCategoryButtonColor_Module_replace = true; - HarmonyPatches.EditorCategoryButtonColor_Module_color = configFile.config.GetValue(EditorCategoryButtonColor_Module).ToRGBA(); - } - } - - string EditorCategoryButtonColor_Resource = "EditorCategoryResourceButtonColor"; - if (configFile.config.HasValue(EditorCategoryButtonColor_Resource)) - { - if (!colorsSet.Contains(EditorCategoryButtonColor_Resource)) - { - colorsSet.Add(EditorCategoryButtonColor_Resource); - HarmonyPatches.EditorCategoryButtonColor_Resource_replace = true; - HarmonyPatches.EditorCategoryButtonColor_Resource_color = configFile.config.GetValue(EditorCategoryButtonColor_Resource).ToRGBA(); - } - } - - string EditorCategoryButtonColor_Manufacturer = "EditorCategoryManufacturerButtonColor"; - if (configFile.config.HasValue(EditorCategoryButtonColor_Manufacturer)) - { - if (!colorsSet.Contains(EditorCategoryButtonColor_Manufacturer)) - { - colorsSet.Add(EditorCategoryButtonColor_Manufacturer); - HarmonyPatches.EditorCategoryButtonColor_Manufacturer_replace = true; - HarmonyPatches.EditorCategoryButtonColor_Manufacturer_color = configFile.config.GetValue(EditorCategoryButtonColor_Manufacturer).ToRGBA(); - } - } - - string EditorCategoryButtonColor_Tech = "EditorCategoryTechButtonColor"; - if (configFile.config.HasValue(EditorCategoryButtonColor_Tech)) - { - if (!colorsSet.Contains(EditorCategoryButtonColor_Tech)) - { - colorsSet.Add(EditorCategoryButtonColor_Tech); - HarmonyPatches.EditorCategoryButtonColor_Tech_replace = true; - HarmonyPatches.EditorCategoryButtonColor_Tech_color = configFile.config.GetValue(EditorCategoryButtonColor_Tech).ToRGBA(); - } - } - - string EditorCategoryButtonColor_Profile = "EditorCategoryProfileButtonColor"; - if (configFile.config.HasValue(EditorCategoryButtonColor_Profile)) - { - if (!colorsSet.Contains(EditorCategoryButtonColor_Profile)) - { - colorsSet.Add(EditorCategoryButtonColor_Profile); - HarmonyPatches.EditorCategoryButtonColor_Profile_replace = true; - HarmonyPatches.EditorCategoryButtonColor_Profile_color = configFile.config.GetValue(EditorCategoryButtonColor_Profile).ToRGBA(); - } - } - - string EditorCategoryButtonColor_Subassembly = "EditorCategorySubassemblyButtonColor"; - if (configFile.config.HasValue(EditorCategoryButtonColor_Subassembly)) - { - if (!colorsSet.Contains(EditorCategoryButtonColor_Subassembly)) - { - colorsSet.Add(EditorCategoryButtonColor_Subassembly); - HarmonyPatches.EditorCategoryButtonColor_Subassembly_replace = true; - HarmonyPatches.EditorCategoryButtonColor_Subassembly_color = configFile.config.GetValue(EditorCategoryButtonColor_Subassembly).ToRGBA(); - } - } - - string EditorCategoryButtonColor_Variants = "EditorCategoryVariantsButtonColor"; - if (configFile.config.HasValue(EditorCategoryButtonColor_Variants)) - { - if (!colorsSet.Contains(EditorCategoryButtonColor_Variants)) - { - colorsSet.Add(EditorCategoryButtonColor_Variants); - HarmonyPatches.EditorCategoryButtonColor_Variants_replace = true; - HarmonyPatches.EditorCategoryButtonColor_Variants_color = configFile.config.GetValue(EditorCategoryButtonColor_Variants).ToRGBA(); - } - } - - string EditorCategoryButtonColor_Custom = "EditorCategoryCustomButtonColor"; - if (configFile.config.HasValue(EditorCategoryButtonColor_Custom)) - { - if (!colorsSet.Contains(EditorCategoryButtonColor_Custom)) - { - colorsSet.Add(EditorCategoryButtonColor_Custom); - HarmonyPatches.EditorCategoryButtonColor_Custom_replace = true; - HarmonyPatches.EditorCategoryButtonColor_Custom_color = configFile.config.GetValue(EditorCategoryButtonColor_Custom).ToRGBA(); - } - } - } - } - /* - internal static void LoadTumblerColors() - { - UrlDir.UrlConfig[] configs = GameDatabase.Instance.GetConfigs(colorPathConfig); - if (configs.Length <= 0) - { - return; - } - configs = configs.OrderByDescending(x => int.Parse(x.config.GetValue("priority"))).ToArray(); - List colorsSet = new List(); - foreach (UrlDir.UrlConfig configFile in configs) - { - int priority = int.Parse(configFile.config.GetValue("priority")); - - - string TumblerColorPositive = "tumblerColorPositive"; - if (configFile.config.HasValue(TumblerColorPositive)) - { - if (!colorsSet.Contains(TumblerColorPositive)) - { - colorsSet.Add(TumblerColorPositive); - Color color = configFile.config.GetValue(TumblerColorPositive).ToRGBA(); - HarmonyPatches.TumblerColorReplacePositive = true; - HarmonyPatches.TumblerColorPositive = color; - } - } - string TumblerColorNegative = "tumblerColorNegative"; - if (configFile.config.HasValue(TumblerColorNegative)) - { - if (!colorsSet.Contains(TumblerColorNegative)) - { - colorsSet.Add(TumblerColorNegative); - Color color = configFile.config.GetValue(TumblerColorNegative).ToRGBA(); - HarmonyPatches.TumblerColorReplaceNegative = true; - HarmonyPatches.TumblerColorNegative = color; - } - } - } - HarmonyPatches.TumblerColorsLoaded = true; - } - */ - private void SetCursor() - { - if (cursors != null && cursors[0] != null) - { - if (cursors[1] == null) cursors[1] = cursors[0]; - if (cursors[2] == null) cursors[2] = cursors[0]; - CursorController.Instance.AddCursor("HUDReplacerCursor", cursors[0], cursors[1], cursors[2]); - CursorController.Instance.ChangeCursor("HUDReplacerCursor"); - Debug.Log("HUDReplacer: Changed Cursor!"); - } - } - - private TextureCursor CreateCursor(string value) - { - Texture2D cursor = new Texture2D(2, 2); - cursor.LoadImage(File.ReadAllBytes(value)); - //Cursor.SetCursor(cursor, new Vector2(6,0), CursorMode.ForceSoftware); - TextureCursor tc = new TextureCursor(); - tc.texture = cursor; - tc.hotspot = new Vector2(6, 0); - return tc; - } - } + if (cursors[1] == null) + cursors[1] = cursors[0]; + if (cursors[2] == null) + cursors[2] = cursors[0]; + CursorController.Instance.AddCursor( + "HUDReplacerCursor", + cursors[0], + cursors[1], + cursors[2] + ); + CursorController.Instance.ChangeCursor("HUDReplacerCursor"); + Debug.Log("HUDReplacer: Changed Cursor!"); + } + } + + private TextureCursor CreateCursor(string value) + { + Texture2D cursor = new Texture2D(2, 2); + cursor.LoadImage(File.ReadAllBytes(value)); + //Cursor.SetCursor(cursor, new Vector2(6,0), CursorMode.ForceSoftware); + TextureCursor tc = new TextureCursor(); + tc.texture = cursor; + tc.hotspot = new Vector2(6, 0); + return tc; + } } diff --git a/src/HUDReplacer/HarmonyPatches.cs b/src/HUDReplacer/HarmonyPatches.cs index 33eab44..c6675db 100644 --- a/src/HUDReplacer/HarmonyPatches.cs +++ b/src/HUDReplacer/HarmonyPatches.cs @@ -1,858 +1,903 @@ -using Expansions.Serenity; +using System; +using System.Collections.Generic; +using System.IO; +using System.Reflection; +using Expansions.Serenity; using HarmonyLib; using KSP.UI; using KSP.UI.Screens; using KSP.UI.Screens.Flight; -using System; -using System.Collections.Generic; -using System.IO; -using System.Reflection; using TMPro; using UnityEngine; using UnityEngine.UI; using static Kerbal; -namespace HUDReplacer -{ - [KSPAddon(KSPAddon.Startup.Instantly, true)] - public class HarmonyPatches : MonoBehaviour - { - public void Awake() - { - // NOTE: A Harmony patcher should be placed in a run once Startup addon. The patch is kept between scene changes. - var harmony = new Harmony("UltraJohn.Mods.HUDReplacer"); - harmony.PatchAll(Assembly.GetExecutingAssembly()); - } - - - // Tumbler colors - internal static bool TumblerColorReplacePositive = false; - internal static bool TumblerColorReplaceNegative = false; - internal static Color TumblerColorPositive = Color.black; - internal static Color TumblerColorNegative = new Color(0.6f, 0f, 0f); - - [HarmonyPatch(typeof(KSP.UI.Screens.Tumbler), "Awake")] - class Patch1 - { - static void Prefix(ref Color ___positiveColor, ref Color ___negativeColor) - { - if (TumblerColorReplacePositive) - { - ___positiveColor = TumblerColorPositive; - } - if (TumblerColorReplaceNegative) - { - ___negativeColor = TumblerColorNegative; - } - } - } - - [HarmonyPatch(typeof(StageTumbler), "Awake")] - class Patch1_2 - { - static void Postfix(StageTumbler __instance) - { - if (TumblerColorReplacePositive) - { - MeshRenderer[] meshes = __instance.gameObject.GetComponentsInChildren(); - foreach(MeshRenderer mesh in meshes) - { - mesh.material.color = TumblerColorPositive; - } - } - } - } - - // PAW Title bar patch - internal static bool PAWTitleBar_replace = false; - internal static Color PAWTitleBar_color; - - [HarmonyPatch(typeof(UIPartActionController), "CreatePartUI")] - class Patch2 - { - static void Postfix(ref UIPartActionWindow __result) - { - if (!PAWTitleBar_replace) return; - try - { - - Image[] images = __result.gameObject.GetComponentsInChildren(); - foreach (Image img in images) - { - if (img.mainTexture.name == "app_divider_pulldown_header_over") - { - img.color = PAWTitleBar_color; - } - } - } - catch (Exception e) - { - Debug.Log(e.ToString()); - } - } - } - - // PAW Blue button - internal static bool PAWBlueButton_replace = false; - internal static Color PAWBlueButton_color; - - [HarmonyPatch(typeof(UIPartActionButton), "Awake")] - class Patch2_1 - { - static void Postfix(ref UIPartActionButton __instance) - { - if (!PAWBlueButton_replace) return; - __instance.button.GetComponent().color = PAWBlueButton_color; - } - } - - // PAW Blue button Toggle & Fuel Flow Buttons - internal static bool PAWBlueButtonToggle_replace = false; - internal static Color PAWBlueButtonToggle_color; - - [HarmonyPatch(typeof(UIButtonToggle), "Awake")] - class Patch2_2 - { - static void Postfix(ref UIButtonToggle __instance) - { - if (PAWBlueButtonToggle_replace) - { - if (__instance.toggleImage.mainTexture.name.Contains("Blue_Btn")) - { - __instance.toggleImage.color = PAWBlueButtonToggle_color; - } - } - } - } - - // PAW Variant Selector Previous/Next Button - internal static bool PAWVariantSelectorNext_replace = false; - internal static Color PAWVariantSelectorNext_color; - internal static bool PAWVariantSelectorPrevious_replace = false; - internal static Color PAWVariantSelectorPrevious_color; - - [HarmonyPatch(typeof(UIPartActionVariantSelector), nameof(UIPartActionVariantSelector.Setup))] - class Patch2_3 - { - static void Postfix(ref UIPartActionVariantSelector __instance) - { - if (PAWVariantSelectorNext_replace) - { - __instance.buttonNext.GetComponent().color = PAWVariantSelectorNext_color; - } - if (PAWVariantSelectorPrevious_replace) - { - __instance.buttonPrevious.GetComponent().color = PAWVariantSelectorPrevious_color; - } - - } - } - - // PAW Resource Priority Increase/Decrease/Reset Button - internal static bool PAWResourcePriorityIncrease_replace = false; - internal static Color PAWResourcePriorityIncrease_color; - internal static bool PAWResourcePriorityDecrease_replace = false; - internal static Color PAWResourcePriorityDecrease_color; - internal static bool PAWResourcePriorityReset_replace = false; - internal static Color PAWResourcePriorityReset_color; - - [HarmonyPatch(typeof(UIPartActionResourcePriority), "Awake")] - class Patch2_4 - { - static void Postfix(ref Button ___btnInc, ref Button ___btnDec, ref Button ___btnReset) - { - if (PAWResourcePriorityIncrease_replace) - { - ___btnInc.GetComponent().color = PAWResourcePriorityIncrease_color; - } - if (PAWResourcePriorityDecrease_replace) - { - ___btnDec.GetComponent().color = PAWResourcePriorityDecrease_color; - } - if (PAWResourcePriorityReset_replace) - { - ___btnReset.GetComponent().color = PAWResourcePriorityReset_color; - } - } - } - - // PAW Fuel Slider - internal static bool PAWFuelSliderColor_replace = false; - internal static Color PAWFuelSliderColor; - internal static bool PAWFuelSliderTextColor_replace = false; - internal static Color PAWFuelSliderTextColor; - - [HarmonyPatch(typeof(UIPartActionResourceEditor), "Setup")] - class Patch2_5 - { - static void Postfix(ref UIPartActionResourceEditor __instance) - { - if (PAWFuelSliderColor_replace) - { - __instance.slider.image.color = PAWFuelSliderColor; - } - if (PAWFuelSliderTextColor_replace) - { - __instance.resourceName.color = PAWFuelSliderTextColor; - __instance.resourceAmnt.color = PAWFuelSliderTextColor; - __instance.resourceMax.color = PAWFuelSliderTextColor; - foreach (Transform child in __instance.sliderContainer.transform) - { - if (child.name == "Slash") - { - child.GetComponent().color = PAWFuelSliderTextColor; - } - /* re-enable if background can't be colored through the texture itself - if (child.name == "Background") - { - child.GetComponent().color = Color.yellow; - break; - } - */ - } - } - - - } - } - - // KAL-1000 Editor patch - internal static bool KALTitleBar_replace = false; - internal static Color KALTitleBar_color; - - [HarmonyPatch(typeof(RoboticControllerWindow), nameof(RoboticControllerWindow.Spawn))] - class Patch3 - { - static void Postfix(ref RoboticControllerWindow __result) - { - if (!__result) return; - Image[] image_array = __result.gameObject.GetComponentsInChildren(); - List textures = new List(); - foreach (Image img in image_array) - { - textures.Add((Texture2D)img.mainTexture); - if(img.mainTexture.name == "app_divider_pulldown_header_over" && KALTitleBar_replace) - { - img.color = KALTitleBar_color; - } - } - Texture2D[] tex_array = textures.ToArray(); - if (tex_array.Length > 0) HUDReplacer.instance.ReplaceTextures(tex_array); - } - } - - // Yaw/Pitch/Roll + Precision mode - // gaugeNeedleYawPitchRoll - internal static bool gaugeNeedleYawPitchRollColor_replace = false; - internal static bool gaugeNeedleYawPitchRollPrecisionColor_replace = false; - internal static Color gaugeNeedleYawPitchRollColor; - internal static Color gaugeNeedleYawPitchRollPrecisionColor; - - [HarmonyPatch(typeof(LinearControlGauges), nameof(LinearControlGauges.Start))] - class Patch4 - { - static void Postfix(ref LinearControlGauges __instance, ref List ___inputGaugeImages) - { - if (!__instance) return; - if (!gaugeNeedleYawPitchRollColor_replace) return; - try - { - foreach (Image img in ___inputGaugeImages) - { - img.color = gaugeNeedleYawPitchRollColor; - } - } - catch (Exception e) - { - Debug.Log(e.ToString()); - } - } - } - - [HarmonyPatch(typeof(LinearControlGauges), "onPrecisionModeToggle")] - class Patch5 - { - static void Postfix(ref LinearControlGauges __instance, ref bool precisionMode, ref List ___inputGaugeImages) - { - if (!__instance) return; - if (gaugeNeedleYawPitchRollColor_replace == false && gaugeNeedleYawPitchRollPrecisionColor_replace == false) return; - try - { - foreach(Image img in ___inputGaugeImages) - { - if (!precisionMode) - { - if(gaugeNeedleYawPitchRollColor_replace) - { - img.color = gaugeNeedleYawPitchRollColor; - } - - } - else - { - if(gaugeNeedleYawPitchRollPrecisionColor_replace) - { - img.color = gaugeNeedleYawPitchRollPrecisionColor; - } - - } - } - } - catch (Exception e) - { - Debug.Log(e.ToString()); - } - } - } - - // Top left clock widget - - internal static Color METDisplayColorRed = Color.red; - internal static Color METDisplayColorYellow = Color.yellow; - internal static Color METDisplayColorGreen = Color.green; - - internal static Color METDisplayRedColorOverride() - { - return METDisplayColorRed; - } - internal static Color METDisplayYellowColorOverride() - { - return METDisplayColorYellow; - } - internal static Color METDisplayGreenColorOverride() - { - return METDisplayColorGreen; - } - - [HarmonyPatch(typeof(METDisplay), "LateUpdate")] - class Patch6 - { - static IEnumerable Transpiler(IEnumerable instructions) - { - var getColorRed = AccessTools.Method(typeof(Color), "get_red"); - var getColorYellow = AccessTools.Method(typeof(Color), "get_yellow"); - var getColorGreen = AccessTools.Method(typeof(Color), "get_green"); - foreach (var instruction in instructions) - { - if (instruction.Calls(getColorRed)) - { - instruction.operand = AccessTools.Method(typeof(HarmonyPatches), nameof(HarmonyPatches.METDisplayRedColorOverride)); - } - else if (instruction.Calls(getColorYellow)) - { - instruction.operand = AccessTools.Method(typeof(HarmonyPatches), nameof(HarmonyPatches.METDisplayYellowColorOverride)); - } - else if (instruction.Calls(getColorGreen)) - { - instruction.operand = AccessTools.Method(typeof(HarmonyPatches), nameof(HarmonyPatches.METDisplayGreenColorOverride)); - } - yield return instruction; - } - } - } - - // Top left clock widget - - [HarmonyPatch(typeof(UIPlanetariumDateTime), "Start")] - class Patch7 - { - static void Postfix(ref UIPlanetariumDateTime __instance) - { - __instance.textDate.color = METDisplayColorGreen; - } - } - - // Top left clock widget - - [HarmonyPatch(typeof(UIPlanetariumDateTime), "onGameUnPause")] - class Patch8 - { - static void Postfix(ref UIPlanetariumDateTime __instance) - { - __instance.textDate.color = METDisplayColorGreen; - } - } - - // NavBall speed display unit - - internal static bool SpeedDisplayColorTextReplace = false; - internal static bool SpeedDisplayColorSpeedReplace = false; - internal static Color SpeedDisplayColorText = Color.green; - internal static Color SpeedDisplayColorSpeed = Color.green; - - [HarmonyPatch(typeof(SpeedDisplay), "Start")] - class Patch9 - { - static void Postfix(ref SpeedDisplay __instance) - { - if (SpeedDisplayColorTextReplace) - { - __instance.textTitle.color = SpeedDisplayColorText; - } - if (SpeedDisplayColorSpeedReplace) - { - __instance.textSpeed.color = SpeedDisplayColorSpeed; - } - } - } - - // NavBall heading unit - - internal static bool NavBallHeadingColorReplace = false; - internal static Color NavBallHeadingColor = Color.green; - - [HarmonyPatch(typeof(NavBall), "Start")] - class Patch10 - { - static void Postfix(ref NavBall __instance) - { - if (NavBallHeadingColorReplace) - { - __instance.headingText.color = NavBallHeadingColor; - } - } - } - - // Stage Total deltaV - internal static bool StageTotalDeltaVColorReplace = false; - internal static Color StageTotalDeltaVColor = Color.white; - - [HarmonyPatch(typeof(StageManager), "Awake")] - class Patch11 - { - static void Postfix(ref StageManager __instance) - { - if (__instance && StageTotalDeltaVColorReplace) - { - __instance.deltaVTotalText.color = StageTotalDeltaVColor; - } - } - } - - // Stage Group deltaV - internal static bool StageGroupDeltaVTextColorReplace = false; - internal static bool StageGroupDeltaVNumberColorReplace = false; - internal static bool StageGroupDeltaVBackgroundColorReplace = false; - internal static Color StageGroupDeltaVTextColor = Color.white; - internal static Color StageGroupDeltaVNumberColor = Color.white; - internal static Color StageGroupDeltaVBackgroundColor = Color.white; - - [HarmonyPatch(typeof(StageGroup), "Awake")] - class Patch12 - { - static void Postfix(ref StageGroup __instance, ref TextMeshProUGUI ___DeltaVHeadingText, ref TextMeshProUGUI ___uiStageIndex) - { - if (__instance) - { - if (StageGroupDeltaVTextColorReplace) - { - ___DeltaVHeadingText.color = StageGroupDeltaVTextColor; - } - if (StageGroupDeltaVNumberColorReplace) - { - ___uiStageIndex.color = StageGroupDeltaVNumberColor; - } - if(StageGroupDeltaVBackgroundColorReplace) - { - Image[] images = __instance.GetComponentsInChildren(); - foreach (Image img in images) - { - if(img.mainTexture.name == "StageDV") - { - img.color = StageGroupDeltaVBackgroundColor; - } - } - } - } - } - } - - // Stage Engine Fuel & Heat gauge (Text color) - internal static bool StageEngineFuelGaugeTextColor_replace = false; - internal static Color StageEngineFuelGaugeTextColor_color; - internal static bool StageEngineHeatGaugeTextColor_replace = false; - internal static Color StageEngineHeatGaugeTextColor_color; - - [HarmonyPatch(typeof(ProtoStageIconInfo), nameof(ProtoStageIconInfo.SetMsgTextColor))] - class Patch12_1 - { - static bool Prefix(ref Color c) - { - if (StageEngineFuelGaugeTextColor_replace) - { - // Engine & RCS gauge - if (c == XKCDColors.ElectricLime.A(0.6f)) - { - c = StageEngineFuelGaugeTextColor_color; - return true; - } - } - if (StageEngineHeatGaugeTextColor_replace) - { - // Heat gauge - if (c == XKCDColors.OrangeYellow.A(0.6f)) - { - c = StageEngineHeatGaugeTextColor_color; - return true; - } - } - - return true; - } - } - - // Stage Engine Fuel & Heat gauge (Background color) - internal static bool StageEngineFuelGaugeBackgroundColor_replace = false; - internal static Color StageEngineFuelGaugeBackgroundColor_color; - internal static bool StageEngineHeatGaugeBackgroundColor_replace = false; - internal static Color StageEngineHeatGaugeBackgroundColor_color; - - [HarmonyPatch(typeof(ProtoStageIconInfo), nameof(ProtoStageIconInfo.SetMsgBgColor))] - class Patch12_2 - { - static bool Prefix(ref Color c) - { - if (StageEngineFuelGaugeBackgroundColor_replace) - { - // Engine & RCS gauge - if (c == XKCDColors.DarkLime.A(0.6f)) - { - c = StageEngineFuelGaugeBackgroundColor_color; - return true; - } - } - if (StageEngineHeatGaugeBackgroundColor_replace) - { - // Heat gauge - if (c == XKCDColors.DarkRed.A(0.6f)) - { - c = StageEngineHeatGaugeBackgroundColor_color; - return true; - } - } - return true; - } - } - - // Stage Engine Fuel & Heat gauge (Fill color) - internal static bool StageEngineFuelGaugeFillColor_replace = false; - internal static Color StageEngineFuelGaugeFillColor_color; - internal static bool StageEngineHeatGaugeFillColor_replace = false; - internal static Color StageEngineHeatGaugeFillColor_color; - - [HarmonyPatch(typeof(ProtoStageIconInfo), nameof(ProtoStageIconInfo.SetProgressBarColor))] - class Patch12_3 - { - static bool Prefix(ref Color c) - { - if (StageEngineFuelGaugeFillColor_replace) - { - // Engine & RCS gauge - if (c == XKCDColors.Yellow.A(0.6f)) - { - c = StageEngineFuelGaugeFillColor_color; - return true; - } - } - if (StageEngineHeatGaugeFillColor_replace) - { - // Heat gauge - if (c == XKCDColors.OrangeYellow.A(0.6f)) - { - c = StageEngineHeatGaugeFillColor_color; - return true; - } - } - return true; - } - } - - // Stage Engine Fuel & Heat gauge (Fill background color) - internal static bool StageEngineFuelGaugeFillBackgroundColor_replace = false; - internal static Color StageEngineFuelGaugeFillBackgroundColor_color; - internal static bool StageEngineHeatGaugeFillBackgroundColor_replace = false; - internal static Color StageEngineHeatGaugeFillBackgroundColor_color; - - [HarmonyPatch(typeof(ProtoStageIconInfo), nameof(ProtoStageIconInfo.SetProgressBarBgColor))] - class Patch12_4 - { - static bool Prefix(ref Color c) - { - if (StageEngineFuelGaugeFillBackgroundColor_replace) - { - // Engine & RCS gauge - if (c == XKCDColors.DarkLime.A(0.6f)) - { - c = StageEngineFuelGaugeFillBackgroundColor_color; - return true; - } - } - if (StageEngineHeatGaugeFillBackgroundColor_replace) - { - // Heat gauge - if (c == XKCDColors.DarkRed.A(0.6f)) - { - c = StageEngineHeatGaugeFillBackgroundColor_color; - return true; - } - } - return true; - } - } - - internal static bool VerticalSpeedGaugeNeedleColorReplace = false; - internal static Color VerticalSpeedGaugeNeedleColor; - - [HarmonyPatch(typeof(VerticalSpeedGauge), nameof(VerticalSpeedGauge.Start))] - class Patch13 - { - static void Postfix(ref VerticalSpeedGauge __instance) - { - if (VerticalSpeedGaugeNeedleColorReplace) - { - __instance.gauge.pointer.gameObject.GetComponentInChildren().color = VerticalSpeedGaugeNeedleColor; - } - } - } - - internal static bool ManeuverNodeEditorTextColor_replace = false; - internal static Color ManeuverNodeEditorTextColor; - - [HarmonyPatch(typeof(ManeuverNodeEditorTabOrbitBasic), "Start")] - class Patch14 - { - static void Postfix(ref TextMeshProUGUI ___apoapsisAltitude, ref TextMeshProUGUI ___apoapsisTime, ref TextMeshProUGUI ___periapsisAltitude, ref TextMeshProUGUI ___periapsisTime, ref TextMeshProUGUI ___orbitPeriod) - { - if (ManeuverNodeEditorTextColor_replace) - { - ___apoapsisAltitude.color = ManeuverNodeEditorTextColor; - ___apoapsisTime.color = ManeuverNodeEditorTextColor; - ___periapsisAltitude.color = ManeuverNodeEditorTextColor; - ___periapsisTime.color = ManeuverNodeEditorTextColor; - ___orbitPeriod.color = ManeuverNodeEditorTextColor; - } - } - } - - [HarmonyPatch(typeof(ManeuverNodeEditorTabOrbitAdv), "Start")] - class Patch14_1 - { - static void Postfix(ref TextMeshProUGUI ___orbitArgumentOfPeriapsis, ref TextMeshProUGUI ___orbitLongitudeOfAscendingNode, ref TextMeshProUGUI ___ejectionAngle, ref TextMeshProUGUI ___orbitEccentricity, ref TextMeshProUGUI ___orbitInclination) - { - if (ManeuverNodeEditorTextColor_replace) - { - ___orbitArgumentOfPeriapsis.color = ManeuverNodeEditorTextColor; - ___orbitLongitudeOfAscendingNode.color = ManeuverNodeEditorTextColor; - ___ejectionAngle.color = ManeuverNodeEditorTextColor; - ___orbitEccentricity.color = ManeuverNodeEditorTextColor; - ___orbitInclination.color = ManeuverNodeEditorTextColor; - } - } - } - - [HarmonyPatch(typeof(ManeuverNodeEditorTabVectorInput), "Start")] - class Patch14_2 - { - static void Postfix(ref TMP_InputField ___proRetrogradeField, ref TMP_InputField ___normalField, ref TMP_InputField ___radialField, ref TMP_InputField ___timeField) - { - if (ManeuverNodeEditorTextColor_replace) - { - ___proRetrogradeField.textComponent.color = ManeuverNodeEditorTextColor; - ___normalField.textComponent.color = ManeuverNodeEditorTextColor; - ___radialField.textComponent.color = ManeuverNodeEditorTextColor; - ___timeField.textComponent.color = ManeuverNodeEditorTextColor; - } - } - } - - [HarmonyPatch(typeof(ManeuverNodeEditorTabVectorHandles), "Start")] - class Patch14_3 - { - static void Postfix(ref TextMeshProUGUI ___sliderTimeDVString) - { - if (ManeuverNodeEditorTextColor_replace) - { - ___sliderTimeDVString.color = ManeuverNodeEditorTextColor; - } - } - } - - internal static string GaugeGeeFilePath = ""; - internal static string GaugeThrottleFilePath = ""; - - [HarmonyPatch(typeof(NavBall), "Start")] - class Patch15 - { - static void Postfix(ref NavBall __instance) - { - if(GaugeGeeFilePath != "") - { - Texture2D tex = (Texture2D)__instance.sideGaugeGee.mainTexture; - ImageConversion.LoadImage(tex, File.ReadAllBytes(GaugeGeeFilePath)); - } - if(GaugeThrottleFilePath != "") - { - Texture2D tex = (Texture2D)__instance.sideGaugeThrottle.mainTexture; - ImageConversion.LoadImage(tex, File.ReadAllBytes(GaugeThrottleFilePath)); - } - } - } - - internal static bool SASDisplayColor_SAS_Replace_On = false; - internal static bool SASDisplayColor_SAS_Replace_Off = false; - internal static bool RCSDisplayColor_RCS_Replace_On = false; - internal static bool RCSDisplayColor_RCS_Replace_Off = false; - internal static Color SASDisplayColor_SAS_On_color; - internal static Color SASDisplayColor_SAS_Off_color; - internal static Color RCSDisplayColor_RCS_On_color; - internal static Color RCSDisplayColor_RCS_Off_color; - - [HarmonyPatch(typeof(SASDisplay), "Start")] - class Patch16 - { - static void Postfix(ref SASDisplay __instance) - { - UIStateText.TextState[] states = __instance.stateText.states; - if(states == null) - { - Debug.LogError("HUDReplacer: no states found for SASDisplay.stateText.states"); - return; - } - int num = states.Length; - while (num-- > 0) - { - if (states[num].name == "On") - { - if (SASDisplayColor_SAS_Replace_On) - { - states[num].textColor = SASDisplayColor_SAS_On_color; - } - } - if (states[num].name == "Off") - { - if (SASDisplayColor_SAS_Replace_Off) - { - states[num].textColor = SASDisplayColor_SAS_Off_color; - } - } - } - } - } - [HarmonyPatch(typeof(RCSDisplay), "Start")] - class Patch16_2 - { - static void Postfix(ref RCSDisplay __instance) - { - UIStateText.TextState[] states = __instance.stateText.states; - if (states == null) - { - Debug.LogError("HUDReplacer: no states found for RCSDisplay.stateText.states"); - return; - } - int num = states.Length; - while (num-- > 0) - { - if (states[num].name == "On") - { - if (RCSDisplayColor_RCS_Replace_On) - { - states[num].textColor = RCSDisplayColor_RCS_On_color; - } - } - if (states[num].name == "Off") - { - if (RCSDisplayColor_RCS_Replace_Off) - { - states[num].textColor = RCSDisplayColor_RCS_Off_color; - } - } - } - } - } - - internal static bool EditorCategoryButtonColor_replace = false; - internal static bool EditorCategoryButtonColor_Module_replace = false; - internal static bool EditorCategoryButtonColor_Resource_replace = false; - internal static bool EditorCategoryButtonColor_Manufacturer_replace = false; - internal static bool EditorCategoryButtonColor_Tech_replace = false; - internal static bool EditorCategoryButtonColor_Profile_replace = false; - internal static bool EditorCategoryButtonColor_Subassembly_replace = false; - internal static bool EditorCategoryButtonColor_Variants_replace = false; - internal static bool EditorCategoryButtonColor_Custom_replace = false; - internal static Color EditorCategoryButtonColor_color; - internal static Color EditorCategoryButtonColor_Module_color; - internal static Color EditorCategoryButtonColor_Resource_color; - internal static Color EditorCategoryButtonColor_Manufacturer_color; - internal static Color EditorCategoryButtonColor_Tech_color; - internal static Color EditorCategoryButtonColor_Profile_color; - internal static Color EditorCategoryButtonColor_Subassembly_color; - internal static Color EditorCategoryButtonColor_Variants_color; - internal static Color EditorCategoryButtonColor_Custom_color; - - [HarmonyPatch(typeof(PartCategorizer), "Setup")] - class Patch17 { - static void Prefix(ref PartCategorizer __instance) - { - if (EditorCategoryButtonColor_replace) - { - __instance.colorFilterFunction = EditorCategoryButtonColor_color; - } - if (EditorCategoryButtonColor_Module_replace) - { - __instance.colorFilterModule = EditorCategoryButtonColor_Module_color; - } - if (EditorCategoryButtonColor_Resource_replace) - { - __instance.colorFilterResource = EditorCategoryButtonColor_Resource_color; - } - if (EditorCategoryButtonColor_Manufacturer_replace) - { - __instance.colorFilterManufacturer = EditorCategoryButtonColor_Manufacturer_color; - } - if (EditorCategoryButtonColor_Tech_replace) - { - __instance.colorFilterTech = EditorCategoryButtonColor_Tech_color; - } - if (EditorCategoryButtonColor_Profile_replace) - { - __instance.colorFilterProfile = EditorCategoryButtonColor_Profile_color; - } - if (EditorCategoryButtonColor_Subassembly_replace) - { - __instance.colorSubassembly = EditorCategoryButtonColor_Subassembly_color; - } - if (EditorCategoryButtonColor_Variants_replace) - { - __instance.colorVariants = EditorCategoryButtonColor_Variants_color; - } - if (EditorCategoryButtonColor_Custom_replace) - { - __instance.colorCategory = EditorCategoryButtonColor_Custom_color; - } - } - } - /* - Perhaps at some point we might tackle orbital lines - - [HarmonyPatch(typeof(OrbitRenderer), "GetNodeColour")] - class Patch15 - { - static void Postfix() - { - Debug.Log("test"); - } - } - - [HarmonyPatch(typeof(OrbitRenderer), "GetOrbitColour")] - class Patch15_1 - { - static void Postfix(ref OrbitRenderer __instance) - { - Debug.Log("test"); - } - } - */ - } - +namespace HUDReplacer; +[KSPAddon(KSPAddon.Startup.Instantly, true)] +public class HarmonyPatches : MonoBehaviour +{ + public void Awake() + { + // NOTE: A Harmony patcher should be placed in a run once Startup addon. The patch is kept between scene changes. + var harmony = new Harmony("UltraJohn.Mods.HUDReplacer"); + harmony.PatchAll(Assembly.GetExecutingAssembly()); + } + + // Tumbler colors + internal static bool TumblerColorReplacePositive = false; + internal static bool TumblerColorReplaceNegative = false; + internal static Color TumblerColorPositive = Color.black; + internal static Color TumblerColorNegative = new Color(0.6f, 0f, 0f); + + [HarmonyPatch(typeof(KSP.UI.Screens.Tumbler), "Awake")] + class Patch1 + { + static void Prefix(ref Color ___positiveColor, ref Color ___negativeColor) + { + if (TumblerColorReplacePositive) + { + ___positiveColor = TumblerColorPositive; + } + if (TumblerColorReplaceNegative) + { + ___negativeColor = TumblerColorNegative; + } + } + } + + [HarmonyPatch(typeof(StageTumbler), "Awake")] + class Patch1_2 + { + static void Postfix(StageTumbler __instance) + { + if (TumblerColorReplacePositive) + { + MeshRenderer[] meshes = + __instance.gameObject.GetComponentsInChildren(); + foreach (MeshRenderer mesh in meshes) + { + mesh.material.color = TumblerColorPositive; + } + } + } + } + + // PAW Title bar patch + internal static bool PAWTitleBar_replace = false; + internal static Color PAWTitleBar_color; + + [HarmonyPatch(typeof(UIPartActionController), "CreatePartUI")] + class Patch2 + { + static void Postfix(ref UIPartActionWindow __result) + { + if (!PAWTitleBar_replace) + return; + try + { + Image[] images = __result.gameObject.GetComponentsInChildren(); + foreach (Image img in images) + { + if (img.mainTexture.name == "app_divider_pulldown_header_over") + { + img.color = PAWTitleBar_color; + } + } + } + catch (Exception e) + { + Debug.Log(e.ToString()); + } + } + } + + // PAW Blue button + internal static bool PAWBlueButton_replace = false; + internal static Color PAWBlueButton_color; + + [HarmonyPatch(typeof(UIPartActionButton), "Awake")] + class Patch2_1 + { + static void Postfix(ref UIPartActionButton __instance) + { + if (!PAWBlueButton_replace) + return; + __instance.button.GetComponent().color = PAWBlueButton_color; + } + } + + // PAW Blue button Toggle & Fuel Flow Buttons + internal static bool PAWBlueButtonToggle_replace = false; + internal static Color PAWBlueButtonToggle_color; + + [HarmonyPatch(typeof(UIButtonToggle), "Awake")] + class Patch2_2 + { + static void Postfix(ref UIButtonToggle __instance) + { + if (PAWBlueButtonToggle_replace) + { + if (__instance.toggleImage.mainTexture.name.Contains("Blue_Btn")) + { + __instance.toggleImage.color = PAWBlueButtonToggle_color; + } + } + } + } + + // PAW Variant Selector Previous/Next Button + internal static bool PAWVariantSelectorNext_replace = false; + internal static Color PAWVariantSelectorNext_color; + internal static bool PAWVariantSelectorPrevious_replace = false; + internal static Color PAWVariantSelectorPrevious_color; + + [HarmonyPatch(typeof(UIPartActionVariantSelector), nameof(UIPartActionVariantSelector.Setup))] + class Patch2_3 + { + static void Postfix(ref UIPartActionVariantSelector __instance) + { + if (PAWVariantSelectorNext_replace) + { + __instance.buttonNext.GetComponent().color = PAWVariantSelectorNext_color; + } + if (PAWVariantSelectorPrevious_replace) + { + __instance.buttonPrevious.GetComponent().color = + PAWVariantSelectorPrevious_color; + } + } + } + + // PAW Resource Priority Increase/Decrease/Reset Button + internal static bool PAWResourcePriorityIncrease_replace = false; + internal static Color PAWResourcePriorityIncrease_color; + internal static bool PAWResourcePriorityDecrease_replace = false; + internal static Color PAWResourcePriorityDecrease_color; + internal static bool PAWResourcePriorityReset_replace = false; + internal static Color PAWResourcePriorityReset_color; + + [HarmonyPatch(typeof(UIPartActionResourcePriority), "Awake")] + class Patch2_4 + { + static void Postfix(ref Button ___btnInc, ref Button ___btnDec, ref Button ___btnReset) + { + if (PAWResourcePriorityIncrease_replace) + { + ___btnInc.GetComponent().color = PAWResourcePriorityIncrease_color; + } + if (PAWResourcePriorityDecrease_replace) + { + ___btnDec.GetComponent().color = PAWResourcePriorityDecrease_color; + } + if (PAWResourcePriorityReset_replace) + { + ___btnReset.GetComponent().color = PAWResourcePriorityReset_color; + } + } + } + + // PAW Fuel Slider + internal static bool PAWFuelSliderColor_replace = false; + internal static Color PAWFuelSliderColor; + internal static bool PAWFuelSliderTextColor_replace = false; + internal static Color PAWFuelSliderTextColor; + + [HarmonyPatch(typeof(UIPartActionResourceEditor), "Setup")] + class Patch2_5 + { + static void Postfix(ref UIPartActionResourceEditor __instance) + { + if (PAWFuelSliderColor_replace) + { + __instance.slider.image.color = PAWFuelSliderColor; + } + if (PAWFuelSliderTextColor_replace) + { + __instance.resourceName.color = PAWFuelSliderTextColor; + __instance.resourceAmnt.color = PAWFuelSliderTextColor; + __instance.resourceMax.color = PAWFuelSliderTextColor; + foreach (Transform child in __instance.sliderContainer.transform) + { + if (child.name == "Slash") + { + child.GetComponent().color = PAWFuelSliderTextColor; + } + /* re-enable if background can't be colored through the texture itself + if (child.name == "Background") + { + child.GetComponent().color = Color.yellow; + break; + } + */ + } + } + } + } + + // KAL-1000 Editor patch + internal static bool KALTitleBar_replace = false; + internal static Color KALTitleBar_color; + + [HarmonyPatch(typeof(RoboticControllerWindow), nameof(RoboticControllerWindow.Spawn))] + class Patch3 + { + static void Postfix(ref RoboticControllerWindow __result) + { + if (!__result) + return; + Image[] image_array = __result.gameObject.GetComponentsInChildren(); + List textures = new List(); + foreach (Image img in image_array) + { + textures.Add((Texture2D)img.mainTexture); + if ( + img.mainTexture.name == "app_divider_pulldown_header_over" + && KALTitleBar_replace + ) + { + img.color = KALTitleBar_color; + } + } + Texture2D[] tex_array = textures.ToArray(); + if (tex_array.Length > 0) + HUDReplacer.instance.ReplaceTextures(tex_array); + } + } + + // Yaw/Pitch/Roll + Precision mode + // gaugeNeedleYawPitchRoll + internal static bool gaugeNeedleYawPitchRollColor_replace = false; + internal static bool gaugeNeedleYawPitchRollPrecisionColor_replace = false; + internal static Color gaugeNeedleYawPitchRollColor; + internal static Color gaugeNeedleYawPitchRollPrecisionColor; + + [HarmonyPatch(typeof(LinearControlGauges), nameof(LinearControlGauges.Start))] + class Patch4 + { + static void Postfix(ref LinearControlGauges __instance, ref List ___inputGaugeImages) + { + if (!__instance) + return; + if (!gaugeNeedleYawPitchRollColor_replace) + return; + try + { + foreach (Image img in ___inputGaugeImages) + { + img.color = gaugeNeedleYawPitchRollColor; + } + } + catch (Exception e) + { + Debug.Log(e.ToString()); + } + } + } + + [HarmonyPatch(typeof(LinearControlGauges), "onPrecisionModeToggle")] + class Patch5 + { + static void Postfix( + ref LinearControlGauges __instance, + ref bool precisionMode, + ref List ___inputGaugeImages + ) + { + if (!__instance) + return; + if ( + gaugeNeedleYawPitchRollColor_replace == false + && gaugeNeedleYawPitchRollPrecisionColor_replace == false + ) + return; + try + { + foreach (Image img in ___inputGaugeImages) + { + if (!precisionMode) + { + if (gaugeNeedleYawPitchRollColor_replace) + { + img.color = gaugeNeedleYawPitchRollColor; + } + } + else + { + if (gaugeNeedleYawPitchRollPrecisionColor_replace) + { + img.color = gaugeNeedleYawPitchRollPrecisionColor; + } + } + } + } + catch (Exception e) + { + Debug.Log(e.ToString()); + } + } + } + + // Top left clock widget + + internal static Color METDisplayColorRed = Color.red; + internal static Color METDisplayColorYellow = Color.yellow; + internal static Color METDisplayColorGreen = Color.green; + + internal static Color METDisplayRedColorOverride() + { + return METDisplayColorRed; + } + + internal static Color METDisplayYellowColorOverride() + { + return METDisplayColorYellow; + } + + internal static Color METDisplayGreenColorOverride() + { + return METDisplayColorGreen; + } + + [HarmonyPatch(typeof(METDisplay), "LateUpdate")] + class Patch6 + { + static IEnumerable Transpiler(IEnumerable instructions) + { + var getColorRed = AccessTools.Method(typeof(Color), "get_red"); + var getColorYellow = AccessTools.Method(typeof(Color), "get_yellow"); + var getColorGreen = AccessTools.Method(typeof(Color), "get_green"); + foreach (var instruction in instructions) + { + if (instruction.Calls(getColorRed)) + { + instruction.operand = AccessTools.Method( + typeof(HarmonyPatches), + nameof(HarmonyPatches.METDisplayRedColorOverride) + ); + } + else if (instruction.Calls(getColorYellow)) + { + instruction.operand = AccessTools.Method( + typeof(HarmonyPatches), + nameof(HarmonyPatches.METDisplayYellowColorOverride) + ); + } + else if (instruction.Calls(getColorGreen)) + { + instruction.operand = AccessTools.Method( + typeof(HarmonyPatches), + nameof(HarmonyPatches.METDisplayGreenColorOverride) + ); + } + yield return instruction; + } + } + } + + // Top left clock widget + + [HarmonyPatch(typeof(UIPlanetariumDateTime), "Start")] + class Patch7 + { + static void Postfix(ref UIPlanetariumDateTime __instance) + { + __instance.textDate.color = METDisplayColorGreen; + } + } + + // Top left clock widget + + [HarmonyPatch(typeof(UIPlanetariumDateTime), "onGameUnPause")] + class Patch8 + { + static void Postfix(ref UIPlanetariumDateTime __instance) + { + __instance.textDate.color = METDisplayColorGreen; + } + } + + // NavBall speed display unit + + internal static bool SpeedDisplayColorTextReplace = false; + internal static bool SpeedDisplayColorSpeedReplace = false; + internal static Color SpeedDisplayColorText = Color.green; + internal static Color SpeedDisplayColorSpeed = Color.green; + + [HarmonyPatch(typeof(SpeedDisplay), "Start")] + class Patch9 + { + static void Postfix(ref SpeedDisplay __instance) + { + if (SpeedDisplayColorTextReplace) + { + __instance.textTitle.color = SpeedDisplayColorText; + } + if (SpeedDisplayColorSpeedReplace) + { + __instance.textSpeed.color = SpeedDisplayColorSpeed; + } + } + } + + // NavBall heading unit + + internal static bool NavBallHeadingColorReplace = false; + internal static Color NavBallHeadingColor = Color.green; + + [HarmonyPatch(typeof(NavBall), "Start")] + class Patch10 + { + static void Postfix(ref NavBall __instance) + { + if (NavBallHeadingColorReplace) + { + __instance.headingText.color = NavBallHeadingColor; + } + } + } + + // Stage Total deltaV + internal static bool StageTotalDeltaVColorReplace = false; + internal static Color StageTotalDeltaVColor = Color.white; + + [HarmonyPatch(typeof(StageManager), "Awake")] + class Patch11 + { + static void Postfix(ref StageManager __instance) + { + if (__instance && StageTotalDeltaVColorReplace) + { + __instance.deltaVTotalText.color = StageTotalDeltaVColor; + } + } + } + + // Stage Group deltaV + internal static bool StageGroupDeltaVTextColorReplace = false; + internal static bool StageGroupDeltaVNumberColorReplace = false; + internal static bool StageGroupDeltaVBackgroundColorReplace = false; + internal static Color StageGroupDeltaVTextColor = Color.white; + internal static Color StageGroupDeltaVNumberColor = Color.white; + internal static Color StageGroupDeltaVBackgroundColor = Color.white; + + [HarmonyPatch(typeof(StageGroup), "Awake")] + class Patch12 + { + static void Postfix( + ref StageGroup __instance, + ref TextMeshProUGUI ___DeltaVHeadingText, + ref TextMeshProUGUI ___uiStageIndex + ) + { + if (__instance) + { + if (StageGroupDeltaVTextColorReplace) + { + ___DeltaVHeadingText.color = StageGroupDeltaVTextColor; + } + if (StageGroupDeltaVNumberColorReplace) + { + ___uiStageIndex.color = StageGroupDeltaVNumberColor; + } + if (StageGroupDeltaVBackgroundColorReplace) + { + Image[] images = __instance.GetComponentsInChildren(); + foreach (Image img in images) + { + if (img.mainTexture.name == "StageDV") + { + img.color = StageGroupDeltaVBackgroundColor; + } + } + } + } + } + } + + // Stage Engine Fuel & Heat gauge (Text color) + internal static bool StageEngineFuelGaugeTextColor_replace = false; + internal static Color StageEngineFuelGaugeTextColor_color; + internal static bool StageEngineHeatGaugeTextColor_replace = false; + internal static Color StageEngineHeatGaugeTextColor_color; + + [HarmonyPatch(typeof(ProtoStageIconInfo), nameof(ProtoStageIconInfo.SetMsgTextColor))] + class Patch12_1 + { + static bool Prefix(ref Color c) + { + if (StageEngineFuelGaugeTextColor_replace) + { + // Engine & RCS gauge + if (c == XKCDColors.ElectricLime.A(0.6f)) + { + c = StageEngineFuelGaugeTextColor_color; + return true; + } + } + if (StageEngineHeatGaugeTextColor_replace) + { + // Heat gauge + if (c == XKCDColors.OrangeYellow.A(0.6f)) + { + c = StageEngineHeatGaugeTextColor_color; + return true; + } + } + + return true; + } + } + + // Stage Engine Fuel & Heat gauge (Background color) + internal static bool StageEngineFuelGaugeBackgroundColor_replace = false; + internal static Color StageEngineFuelGaugeBackgroundColor_color; + internal static bool StageEngineHeatGaugeBackgroundColor_replace = false; + internal static Color StageEngineHeatGaugeBackgroundColor_color; + + [HarmonyPatch(typeof(ProtoStageIconInfo), nameof(ProtoStageIconInfo.SetMsgBgColor))] + class Patch12_2 + { + static bool Prefix(ref Color c) + { + if (StageEngineFuelGaugeBackgroundColor_replace) + { + // Engine & RCS gauge + if (c == XKCDColors.DarkLime.A(0.6f)) + { + c = StageEngineFuelGaugeBackgroundColor_color; + return true; + } + } + if (StageEngineHeatGaugeBackgroundColor_replace) + { + // Heat gauge + if (c == XKCDColors.DarkRed.A(0.6f)) + { + c = StageEngineHeatGaugeBackgroundColor_color; + return true; + } + } + return true; + } + } + + // Stage Engine Fuel & Heat gauge (Fill color) + internal static bool StageEngineFuelGaugeFillColor_replace = false; + internal static Color StageEngineFuelGaugeFillColor_color; + internal static bool StageEngineHeatGaugeFillColor_replace = false; + internal static Color StageEngineHeatGaugeFillColor_color; + + [HarmonyPatch(typeof(ProtoStageIconInfo), nameof(ProtoStageIconInfo.SetProgressBarColor))] + class Patch12_3 + { + static bool Prefix(ref Color c) + { + if (StageEngineFuelGaugeFillColor_replace) + { + // Engine & RCS gauge + if (c == XKCDColors.Yellow.A(0.6f)) + { + c = StageEngineFuelGaugeFillColor_color; + return true; + } + } + if (StageEngineHeatGaugeFillColor_replace) + { + // Heat gauge + if (c == XKCDColors.OrangeYellow.A(0.6f)) + { + c = StageEngineHeatGaugeFillColor_color; + return true; + } + } + return true; + } + } + + // Stage Engine Fuel & Heat gauge (Fill background color) + internal static bool StageEngineFuelGaugeFillBackgroundColor_replace = false; + internal static Color StageEngineFuelGaugeFillBackgroundColor_color; + internal static bool StageEngineHeatGaugeFillBackgroundColor_replace = false; + internal static Color StageEngineHeatGaugeFillBackgroundColor_color; + + [HarmonyPatch(typeof(ProtoStageIconInfo), nameof(ProtoStageIconInfo.SetProgressBarBgColor))] + class Patch12_4 + { + static bool Prefix(ref Color c) + { + if (StageEngineFuelGaugeFillBackgroundColor_replace) + { + // Engine & RCS gauge + if (c == XKCDColors.DarkLime.A(0.6f)) + { + c = StageEngineFuelGaugeFillBackgroundColor_color; + return true; + } + } + if (StageEngineHeatGaugeFillBackgroundColor_replace) + { + // Heat gauge + if (c == XKCDColors.DarkRed.A(0.6f)) + { + c = StageEngineHeatGaugeFillBackgroundColor_color; + return true; + } + } + return true; + } + } + + internal static bool VerticalSpeedGaugeNeedleColorReplace = false; + internal static Color VerticalSpeedGaugeNeedleColor; + + [HarmonyPatch(typeof(VerticalSpeedGauge), nameof(VerticalSpeedGauge.Start))] + class Patch13 + { + static void Postfix(ref VerticalSpeedGauge __instance) + { + if (VerticalSpeedGaugeNeedleColorReplace) + { + __instance.gauge.pointer.gameObject.GetComponentInChildren().color = + VerticalSpeedGaugeNeedleColor; + } + } + } + + internal static bool ManeuverNodeEditorTextColor_replace = false; + internal static Color ManeuverNodeEditorTextColor; + + [HarmonyPatch(typeof(ManeuverNodeEditorTabOrbitBasic), "Start")] + class Patch14 + { + static void Postfix( + ref TextMeshProUGUI ___apoapsisAltitude, + ref TextMeshProUGUI ___apoapsisTime, + ref TextMeshProUGUI ___periapsisAltitude, + ref TextMeshProUGUI ___periapsisTime, + ref TextMeshProUGUI ___orbitPeriod + ) + { + if (ManeuverNodeEditorTextColor_replace) + { + ___apoapsisAltitude.color = ManeuverNodeEditorTextColor; + ___apoapsisTime.color = ManeuverNodeEditorTextColor; + ___periapsisAltitude.color = ManeuverNodeEditorTextColor; + ___periapsisTime.color = ManeuverNodeEditorTextColor; + ___orbitPeriod.color = ManeuverNodeEditorTextColor; + } + } + } + + [HarmonyPatch(typeof(ManeuverNodeEditorTabOrbitAdv), "Start")] + class Patch14_1 + { + static void Postfix( + ref TextMeshProUGUI ___orbitArgumentOfPeriapsis, + ref TextMeshProUGUI ___orbitLongitudeOfAscendingNode, + ref TextMeshProUGUI ___ejectionAngle, + ref TextMeshProUGUI ___orbitEccentricity, + ref TextMeshProUGUI ___orbitInclination + ) + { + if (ManeuverNodeEditorTextColor_replace) + { + ___orbitArgumentOfPeriapsis.color = ManeuverNodeEditorTextColor; + ___orbitLongitudeOfAscendingNode.color = ManeuverNodeEditorTextColor; + ___ejectionAngle.color = ManeuverNodeEditorTextColor; + ___orbitEccentricity.color = ManeuverNodeEditorTextColor; + ___orbitInclination.color = ManeuverNodeEditorTextColor; + } + } + } + + [HarmonyPatch(typeof(ManeuverNodeEditorTabVectorInput), "Start")] + class Patch14_2 + { + static void Postfix( + ref TMP_InputField ___proRetrogradeField, + ref TMP_InputField ___normalField, + ref TMP_InputField ___radialField, + ref TMP_InputField ___timeField + ) + { + if (ManeuverNodeEditorTextColor_replace) + { + ___proRetrogradeField.textComponent.color = ManeuverNodeEditorTextColor; + ___normalField.textComponent.color = ManeuverNodeEditorTextColor; + ___radialField.textComponent.color = ManeuverNodeEditorTextColor; + ___timeField.textComponent.color = ManeuverNodeEditorTextColor; + } + } + } + + [HarmonyPatch(typeof(ManeuverNodeEditorTabVectorHandles), "Start")] + class Patch14_3 + { + static void Postfix(ref TextMeshProUGUI ___sliderTimeDVString) + { + if (ManeuverNodeEditorTextColor_replace) + { + ___sliderTimeDVString.color = ManeuverNodeEditorTextColor; + } + } + } + + internal static string GaugeGeeFilePath = ""; + internal static string GaugeThrottleFilePath = ""; + + [HarmonyPatch(typeof(NavBall), "Start")] + class Patch15 + { + static void Postfix(ref NavBall __instance) + { + if (GaugeGeeFilePath != "") + { + Texture2D tex = (Texture2D)__instance.sideGaugeGee.mainTexture; + ImageConversion.LoadImage(tex, File.ReadAllBytes(GaugeGeeFilePath)); + } + if (GaugeThrottleFilePath != "") + { + Texture2D tex = (Texture2D)__instance.sideGaugeThrottle.mainTexture; + ImageConversion.LoadImage(tex, File.ReadAllBytes(GaugeThrottleFilePath)); + } + } + } + + internal static bool SASDisplayColor_SAS_Replace_On = false; + internal static bool SASDisplayColor_SAS_Replace_Off = false; + internal static bool RCSDisplayColor_RCS_Replace_On = false; + internal static bool RCSDisplayColor_RCS_Replace_Off = false; + internal static Color SASDisplayColor_SAS_On_color; + internal static Color SASDisplayColor_SAS_Off_color; + internal static Color RCSDisplayColor_RCS_On_color; + internal static Color RCSDisplayColor_RCS_Off_color; + + [HarmonyPatch(typeof(SASDisplay), "Start")] + class Patch16 + { + static void Postfix(ref SASDisplay __instance) + { + UIStateText.TextState[] states = __instance.stateText.states; + if (states == null) + { + Debug.LogError("HUDReplacer: no states found for SASDisplay.stateText.states"); + return; + } + int num = states.Length; + while (num-- > 0) + { + if (states[num].name == "On") + { + if (SASDisplayColor_SAS_Replace_On) + { + states[num].textColor = SASDisplayColor_SAS_On_color; + } + } + if (states[num].name == "Off") + { + if (SASDisplayColor_SAS_Replace_Off) + { + states[num].textColor = SASDisplayColor_SAS_Off_color; + } + } + } + } + } + + [HarmonyPatch(typeof(RCSDisplay), "Start")] + class Patch16_2 + { + static void Postfix(ref RCSDisplay __instance) + { + UIStateText.TextState[] states = __instance.stateText.states; + if (states == null) + { + Debug.LogError("HUDReplacer: no states found for RCSDisplay.stateText.states"); + return; + } + int num = states.Length; + while (num-- > 0) + { + if (states[num].name == "On") + { + if (RCSDisplayColor_RCS_Replace_On) + { + states[num].textColor = RCSDisplayColor_RCS_On_color; + } + } + if (states[num].name == "Off") + { + if (RCSDisplayColor_RCS_Replace_Off) + { + states[num].textColor = RCSDisplayColor_RCS_Off_color; + } + } + } + } + } + + internal static bool EditorCategoryButtonColor_replace = false; + internal static bool EditorCategoryButtonColor_Module_replace = false; + internal static bool EditorCategoryButtonColor_Resource_replace = false; + internal static bool EditorCategoryButtonColor_Manufacturer_replace = false; + internal static bool EditorCategoryButtonColor_Tech_replace = false; + internal static bool EditorCategoryButtonColor_Profile_replace = false; + internal static bool EditorCategoryButtonColor_Subassembly_replace = false; + internal static bool EditorCategoryButtonColor_Variants_replace = false; + internal static bool EditorCategoryButtonColor_Custom_replace = false; + internal static Color EditorCategoryButtonColor_color; + internal static Color EditorCategoryButtonColor_Module_color; + internal static Color EditorCategoryButtonColor_Resource_color; + internal static Color EditorCategoryButtonColor_Manufacturer_color; + internal static Color EditorCategoryButtonColor_Tech_color; + internal static Color EditorCategoryButtonColor_Profile_color; + internal static Color EditorCategoryButtonColor_Subassembly_color; + internal static Color EditorCategoryButtonColor_Variants_color; + internal static Color EditorCategoryButtonColor_Custom_color; + + [HarmonyPatch(typeof(PartCategorizer), "Setup")] + class Patch17 + { + static void Prefix(ref PartCategorizer __instance) + { + if (EditorCategoryButtonColor_replace) + { + __instance.colorFilterFunction = EditorCategoryButtonColor_color; + } + if (EditorCategoryButtonColor_Module_replace) + { + __instance.colorFilterModule = EditorCategoryButtonColor_Module_color; + } + if (EditorCategoryButtonColor_Resource_replace) + { + __instance.colorFilterResource = EditorCategoryButtonColor_Resource_color; + } + if (EditorCategoryButtonColor_Manufacturer_replace) + { + __instance.colorFilterManufacturer = EditorCategoryButtonColor_Manufacturer_color; + } + if (EditorCategoryButtonColor_Tech_replace) + { + __instance.colorFilterTech = EditorCategoryButtonColor_Tech_color; + } + if (EditorCategoryButtonColor_Profile_replace) + { + __instance.colorFilterProfile = EditorCategoryButtonColor_Profile_color; + } + if (EditorCategoryButtonColor_Subassembly_replace) + { + __instance.colorSubassembly = EditorCategoryButtonColor_Subassembly_color; + } + if (EditorCategoryButtonColor_Variants_replace) + { + __instance.colorVariants = EditorCategoryButtonColor_Variants_color; + } + if (EditorCategoryButtonColor_Custom_replace) + { + __instance.colorCategory = EditorCategoryButtonColor_Custom_color; + } + } + } + /* + Perhaps at some point we might tackle orbital lines + + [HarmonyPatch(typeof(OrbitRenderer), "GetNodeColour")] + class Patch15 + { + static void Postfix() + { + Debug.Log("test"); + } + } + + [HarmonyPatch(typeof(OrbitRenderer), "GetOrbitColour")] + class Patch15_1 + { + static void Postfix(ref OrbitRenderer __instance) + { + Debug.Log("test"); + } + } + */ } diff --git a/src/HUDReplacer/SettingsManager.cs b/src/HUDReplacer/SettingsManager.cs index 8790146..613b0bc 100644 --- a/src/HUDReplacer/SettingsManager.cs +++ b/src/HUDReplacer/SettingsManager.cs @@ -2,32 +2,38 @@ namespace HUDReplacer { - internal class SettingsManager - { - static SettingsManager instance; - public bool showDebugToolbar; - public static SettingsManager Instance - { - get { - if (instance == null) - { - instance = new SettingsManager(); - } - return instance; - } - } - SettingsManager() - { - ConfigNode node = GameDatabase.Instance.GetConfigNode("HUDReplacer/HUDReplacerSettings"); - if(node == null) - { - Debug.LogError("HUDReplacer: Could not load settings. Settings.cfg may be missing."); - return; - } - if (node.HasValue("showDebugToolbar")) - { - bool.TryParse(node.GetValue("showDebugToolbar"), out showDebugToolbar); - } - } - } + internal class SettingsManager + { + static SettingsManager instance; + public bool showDebugToolbar; + public static SettingsManager Instance + { + get + { + if (instance == null) + { + instance = new SettingsManager(); + } + return instance; + } + } + + SettingsManager() + { + ConfigNode node = GameDatabase.Instance.GetConfigNode( + "HUDReplacer/HUDReplacerSettings" + ); + if (node == null) + { + Debug.LogError( + "HUDReplacer: Could not load settings. Settings.cfg may be missing." + ); + return; + } + if (node.HasValue("showDebugToolbar")) + { + bool.TryParse(node.GetValue("showDebugToolbar"), out showDebugToolbar); + } + } + } } diff --git a/src/HUDReplacer/ToolbarManager.cs b/src/HUDReplacer/ToolbarManager.cs index e63559c..7e5a757 100644 --- a/src/HUDReplacer/ToolbarManager.cs +++ b/src/HUDReplacer/ToolbarManager.cs @@ -1,40 +1,49 @@ using KSP.UI.Screens; using UnityEngine; -namespace HUDReplacer +namespace HUDReplacer; + +[KSPAddon(KSPAddon.Startup.SpaceCentre, true)] +internal class ToolbarManager : MonoBehaviour { - [KSPAddon(KSPAddon.Startup.SpaceCentre, true)] - internal class ToolbarManager : MonoBehaviour - { - private ApplicationLauncherButton ToolbarButton; - private Texture OnTexture; - private Texture OffTexture; - private void Awake() - { - if (!SettingsManager.Instance.showDebugToolbar) - { - return; - } - OnTexture = GameDatabase.Instance.GetTexture("HUDReplacer/Assets/ToolbarOn", false); - OffTexture = GameDatabase.Instance.GetTexture("HUDReplacer/Assets/ToolbarOff", false); - if (!ToolbarButton) - { - ToolbarButton = ApplicationLauncher.Instance.AddModApplication(onToolbarToggleOn, onToolbarToggleOff, null, null, null, null, ApplicationLauncher.AppScenes.ALWAYS, OffTexture); - } - } + private ApplicationLauncherButton ToolbarButton; + private Texture OnTexture; + private Texture OffTexture; + + private void Awake() + { + if (!SettingsManager.Instance.showDebugToolbar) + { + return; + } + OnTexture = GameDatabase.Instance.GetTexture("HUDReplacer/Assets/ToolbarOn", false); + OffTexture = GameDatabase.Instance.GetTexture("HUDReplacer/Assets/ToolbarOff", false); + if (!ToolbarButton) + { + ToolbarButton = ApplicationLauncher.Instance.AddModApplication( + onToolbarToggleOn, + onToolbarToggleOff, + null, + null, + null, + null, + ApplicationLauncher.AppScenes.ALWAYS, + OffTexture + ); + } + } - private void onToolbarToggleOn() - { - HUDReplacer.enableDebug = true; - ToolbarButton.SetTexture(OnTexture); - Debug.Log("HUDReplacer: Enabled debug mode."); - } + private void onToolbarToggleOn() + { + HUDReplacer.enableDebug = true; + ToolbarButton.SetTexture(OnTexture); + Debug.Log("HUDReplacer: Enabled debug mode."); + } - private void onToolbarToggleOff() - { - HUDReplacer.enableDebug = false; - ToolbarButton.SetTexture(OffTexture); - Debug.Log("HUDReplacer: Disabled debug mode."); - } - } + private void onToolbarToggleOff() + { + HUDReplacer.enableDebug = false; + ToolbarButton.SetTexture(OffTexture); + Debug.Log("HUDReplacer: Disabled debug mode."); + } } diff --git a/src/HUDReplacer/Utility.cs b/src/HUDReplacer/Utility.cs index b584022..f5475b4 100644 --- a/src/HUDReplacer/Utility.cs +++ b/src/HUDReplacer/Utility.cs @@ -3,39 +3,43 @@ using System.IO; using UnityEngine; -namespace HUDReplacer +namespace HUDReplacer; + +internal static class Utility { - internal static class Utility - { - internal static void Invoke(this MonoBehaviour mb, Action f, float delay) - { - mb.StartCoroutine(InvokeRoutine(f, delay)); - } + internal static void Invoke(this MonoBehaviour mb, Action f, float delay) + { + mb.StartCoroutine(InvokeRoutine(f, delay)); + } - private static IEnumerator InvokeRoutine(Action f, float delay) - { - yield return new WaitForSeconds(delay); - f(); - } + private static IEnumerator InvokeRoutine(Action f, float delay) + { + yield return new WaitForSeconds(delay); + f(); + } - internal static Color ToRGBA(this string color) - { - string[] values = color.Split(','); - return new Color(float.Parse(values[0]), float.Parse(values[1]), float.Parse(values[2]), float.Parse(values[3])); - } + internal static Color ToRGBA(this string color) + { + string[] values = color.Split(','); + return new Color( + float.Parse(values[0]), + float.Parse(values[1]), + float.Parse(values[2]), + float.Parse(values[3]) + ); + } - // Might be useful in the future, if it's possible to read textures from the game's data. - // It's currently blocked by the ImportFormat IsReadable flag. - // As it stands right now this is the problem: - // "Texture is not readable. The texture memory cannot be accessed from scripts." - private static void DumpTexture(Texture2D tex) - { - string path = KSPUtil.ApplicationRootPath + "GameData/HUDReplacer/PluginData/Dump"; - if (!Directory.Exists(path)) - { - Directory.CreateDirectory(path); - } - File.WriteAllBytes(path, tex.EncodeToPNG()); - } - } + // Might be useful in the future, if it's possible to read textures from the game's data. + // It's currently blocked by the ImportFormat IsReadable flag. + // As it stands right now this is the problem: + // "Texture is not readable. The texture memory cannot be accessed from scripts." + private static void DumpTexture(Texture2D tex) + { + string path = KSPUtil.ApplicationRootPath + "GameData/HUDReplacer/PluginData/Dump"; + if (!Directory.Exists(path)) + { + Directory.CreateDirectory(path); + } + File.WriteAllBytes(path, tex.EncodeToPNG()); + } }