diff --git a/src/HUDMerger/Models/Dependencies.cs b/src/HUDMerger/Models/Dependencies.cs index 9eb7669..fe620c1 100644 --- a/src/HUDMerger/Models/Dependencies.cs +++ b/src/HUDMerger/Models/Dependencies.cs @@ -20,6 +20,7 @@ public class Dependencies public HashSet Events { get; init; } = new(StringComparer.OrdinalIgnoreCase); public HashSet LanguageTokens { get; init; } = new(StringComparer.OrdinalIgnoreCase); public FilesHashSet Images { get; init; } = []; + public FilesHashSet PreloadImages { get; init; } = []; public FilesHashSet Audio { get; init; } = []; public FilesHashSet Files { get; init; } = []; @@ -43,6 +44,7 @@ public void UnionWith(Dependencies other) Events.UnionWith(other.Events); LanguageTokens.UnionWith(other.LanguageTokens); Images.UnionWith(other.Images); + PreloadImages.UnionWith(other.PreloadImages); Audio.UnionWith(other.Audio); Files.UnionWith(other.Files); } diff --git a/src/HUDMerger/Models/HUD.cs b/src/HUDMerger/Models/HUD.cs index e8a3d90..0534e9a 100644 --- a/src/HUDMerger/Models/HUD.cs +++ b/src/HUDMerger/Models/HUD.cs @@ -140,6 +140,7 @@ public static void Merge(HUD source, HUD target, HUDPanel[] panels) MergeSourceScheme, MergeLanguageTokens, MergeImages, + MergePreloadImages, MergeAudio, MergeInfoVDF, CopyFiles @@ -1101,11 +1102,13 @@ void RemoveBaseSchemeValues(string folderPath, IEnumerable baseFiles) private static Action? MergeImages(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies) { + IEnumerable images = [..dependencies.PreloadImages, ..dependencies.Images]; + reader.Require([ - ..dependencies.Images.Select((image) => (source, $"{image}.vmt", FileType.VDF) ), + ..images.Select((image) => (source, $"{image}.vmt", FileType.VDF) ), ]); - foreach (string image in dependencies.Images) + foreach (string image in images) { dependencies.Files.Add($"{image}.vmt"); dependencies.Files.Add($"{image}.vtf"); @@ -1126,6 +1129,72 @@ void RemoveBaseSchemeValues(string folderPath, IEnumerable baseFiles) return null; } + private static Action? MergePreloadImages(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies) + { + if (dependencies.PreloadImages.Count == 0) + { + return null; + } + + reader.Require([ + (target, "resource\\ui\\mainmenuoverride.res", FileType.VDF), + (target, "resource\\ui\\mainmenuoverride_preload.res", FileType.VDF) + ]); + + KeyValues mainmenuKeyValues = reader.ReadKeyValues(target, "resource\\ui\\mainmenuoverride.res"); + + KeyValues preloadKeyValues = reader.TryReadKeyValues(target, "resource\\ui\\mainmenuoverride_preload.res") ?? []; + KeyValues preloadKeyValuesHeader = preloadKeyValues.Header("Resource/UI/MainMenuOverride_Preload.res"); + + bool preloadImagesMerged = false; + + foreach (string image in dependencies.PreloadImages) + { + string name = Path.GetFileNameWithoutExtension(image); + KeyValue keyValue = new() + { + Key = name, + Value = new KeyValues([ + new KeyValue { Key = "ControlName", Value = "ImagePanel", Conditional = null }, + new KeyValue { Key = "fieldName", Value = name, Conditional = null }, + new KeyValue { Key = "visible", Value = 0.ToString(), Conditional = null }, + new KeyValue { Key = "enabled", Value = 0.ToString(), Conditional = null }, + new KeyValue { Key = "image", Value = App.PathSeparatorRegex().Replace(Path.GetRelativePath("materials\\vgui", image), "/"), Conditional = null }, + ]), + Conditional = null + }; + + if (!preloadKeyValuesHeader.Any((kv) => kv.Equals(keyValue))) + { + preloadKeyValuesHeader.Add(keyValue); + preloadImagesMerged = true; + } + } + + if (preloadImagesMerged) + { + int index = mainmenuKeyValues.FindLastIndex((kv) => kv.Key.Equals("#base", StringComparison.OrdinalIgnoreCase) && kv.Value is string); + mainmenuKeyValues.Insert( + index != -1 ? (index + 1) : 0, + new KeyValue + { + Key = "#base", + Value = $"mainmenuoverride_preload.res", + Conditional = null + } + ); + } + + return (writer) => + { + if (preloadImagesMerged) + { + writer.Write("resource\\ui\\mainmenuoverride.res", mainmenuKeyValues); + writer.Write("resource\\ui\\mainmenuoverride_preload.res", preloadKeyValues); + } + }; + } + private static Action? MergeAudio(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies) { dependencies.Files.UnionWith(dependencies.Audio); diff --git a/src/HUDMerger/Resources/Panels.json b/src/HUDMerger/Resources/Panels.json index a4be57c..ce1b03a 100644 --- a/src/HUDMerger/Resources/Panels.json +++ b/src/HUDMerger/Resources/Panels.json @@ -57,7 +57,7 @@ "HUDLayout": [ "HudControlPointIcons" ], - "Images": [ + "PreloadImages": [ "materials\\sprites\\obj_icons\\icon_base_blu", "materials\\sprites\\obj_icons\\icon_base_red", "materials\\sprites\\obj_icons\\icon_obj_1_blu_locked", @@ -476,7 +476,7 @@ "HUDLayout": [ "HudObjectiveStatus" ], - "Images": [ + "PreloadImages": [ "materials\\hud\\cart_alert", "materials\\hud\\cart_arrow_left", "materials\\hud\\cart_arrow_right",