Skip to content

Commit

Permalink
Add image preloading
Browse files Browse the repository at this point in the history
  • Loading branch information
cooolbros committed Jan 5, 2024
1 parent 7e94876 commit 4a125af
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/HUDMerger/Models/Dependencies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class Dependencies
public HashSet<string> Events { get; init; } = new(StringComparer.OrdinalIgnoreCase);
public HashSet<string> 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; } = [];

Expand All @@ -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);
}
Expand Down
73 changes: 71 additions & 2 deletions src/HUDMerger/Models/HUD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ public static void Merge(HUD source, HUD target, HUDPanel[] panels)
MergeSourceScheme,
MergeLanguageTokens,
MergeImages,
MergePreloadImages,
MergeAudio,
MergeInfoVDF,
CopyFiles
Expand Down Expand Up @@ -1101,11 +1102,13 @@ void RemoveBaseSchemeValues(string folderPath, IEnumerable<string> baseFiles)

private static Action<IHUDFileWriterService>? MergeImages(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies)
{
IEnumerable<string> 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");
Expand All @@ -1126,6 +1129,72 @@ void RemoveBaseSchemeValues(string folderPath, IEnumerable<string> baseFiles)
return null;
}

private static Action<IHUDFileWriterService>? 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<IHUDFileWriterService>? MergeAudio(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies)
{
dependencies.Files.UnionWith(dependencies.Audio);
Expand Down
4 changes: 2 additions & 2 deletions src/HUDMerger/Resources/Panels.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -476,7 +476,7 @@
"HUDLayout": [
"HudObjectiveStatus"
],
"Images": [
"PreloadImages": [
"materials\\hud\\cart_alert",
"materials\\hud\\cart_arrow_left",
"materials\\hud\\cart_arrow_right",
Expand Down

0 comments on commit 4a125af

Please sign in to comment.