diff --git a/src/HUDMerger/Models/Dependencies.cs b/src/HUDMerger/Models/Dependencies.cs index a11e2b0..284cdbe 100644 --- a/src/HUDMerger/Models/Dependencies.cs +++ b/src/HUDMerger/Models/Dependencies.cs @@ -47,7 +47,7 @@ public void UnionWith(Dependencies other) Files.UnionWith(other.Files); } - public void Add(HUDFileReaderService reader, HUD hud) + public void Add(IHUDFileReaderService reader, HUD hud) { reader.Require(Files.Select((file) => (hud, file, FileType.VDF))); @@ -57,7 +57,7 @@ public void Add(HUDFileReaderService reader, HUD hud) } } - public void Add(HUDFileReaderService reader, HUD hud, string relativePath) + public void Add(IHUDFileReaderService reader, HUD hud, string relativePath) { try { diff --git a/src/HUDMerger/Models/HUD.cs b/src/HUDMerger/Models/HUD.cs index 671ba59..5f914cc 100644 --- a/src/HUDMerger/Models/HUD.cs +++ b/src/HUDMerger/Models/HUD.cs @@ -122,9 +122,9 @@ public static void Merge(HUD source, HUD target, HUDPanel[] panels) Dependencies dependencies = new(panels.Select((panel) => panel.Dependencies).OfType()); dependencies.Files.UnionWith(panels.Select((panel) => panel.Main).Where((main) => !string.IsNullOrEmpty(main))); - HUDFileReaderService reader = new(); + IHUDFileReaderService reader = new HUDFileReaderService(); - Func?>[] actions = + Func?>[] actions = [ AddDependencies, MergeHUDLayout, @@ -138,14 +138,14 @@ public static void Merge(HUD source, HUD target, HUDPanel[] panels) CopyFiles ]; - List> commitActions = []; + List> commitActions = []; List exceptions = []; - foreach (Func?> action in actions) + foreach (Func?> action in actions) { try { - Action? result = action(reader, source, target, dependencies); + Action? result = action(reader, source, target, dependencies); if (result != null) { commitActions.Add(result); @@ -170,21 +170,21 @@ public static void Merge(HUD source, HUD target, HUDPanel[] panels) ])); } - HUDFileWriterService writer = new(target.FolderPath); + IHUDFileWriterService writer = new HUDFileWriterService(target.FolderPath); - foreach (Action action in commitActions) + foreach (Action action in commitActions) { action(writer); } } - private static Action? AddDependencies(HUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies) + private static Action? AddDependencies(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies) { dependencies.Add(reader, source); return null; } - private static Action? MergeHUDLayout(HUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies) + private static Action? MergeHUDLayout(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies) { if (dependencies.HUDLayout.Count == 0) { @@ -369,7 +369,7 @@ void RemoveBaseHUDLayoutEntries(string folderPath, IEnumerable baseFiles }; } - private static Action? MergeHUDAnimations(HUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies) + private static Action? MergeHUDAnimations(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies) { if (dependencies.Events.Count == 0) { @@ -492,7 +492,7 @@ bool SourceHUDAnimationsFileExists(KeyValue kv) }; } - private static Action? MergeClientScheme(HUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies) + private static Action? MergeClientScheme(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies) { if (!dependencies.ClientScheme.Any()) { @@ -721,7 +721,7 @@ If the value is a string select the string value }; } - private static Action? MergeSourceScheme(HUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies) + private static Action? MergeSourceScheme(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies) { if (!dependencies.SourceScheme.Any()) { @@ -955,7 +955,7 @@ void RemoveBaseSchemeValues(string folderPath, IEnumerable baseFiles) }; } - private static Action? MergeLanguageTokens(HUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies) + private static Action? MergeLanguageTokens(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies) { if (dependencies.LanguageTokens.Count == 0) { @@ -982,7 +982,7 @@ void RemoveBaseSchemeValues(string folderPath, IEnumerable baseFiles) reader.Require(required); - static (KeyValues Root, KeyValues Tokens) LoadLanguageFile(HUDFileReaderService reader, HUD hud, string language) + static (KeyValues Root, KeyValues Tokens) LoadLanguageFile(IHUDFileReaderService reader, HUD hud, string language) { KeyValues keyValues = reader.ReadKeyValues(hud, $"resource\\chat_{language}.txt"); KeyValues languageFileHeader = keyValues.Header("lang"); @@ -1078,7 +1078,7 @@ void RemoveBaseSchemeValues(string folderPath, IEnumerable baseFiles) }; } - private static Action? MergeImages(HUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies) + private static Action? MergeImages(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies) { reader.Require([ ..dependencies.Images.Select((image) => (source, $"{image}.vmt", FileType.VDF) ), @@ -1105,13 +1105,13 @@ void RemoveBaseSchemeValues(string folderPath, IEnumerable baseFiles) return null; } - private static Action? MergeAudio(HUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies) + private static Action? MergeAudio(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies) { dependencies.Files.UnionWith(dependencies.Audio); return null; } - private static Action? MergeInfoVDF(HUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies) + private static Action? MergeInfoVDF(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies) { reader.Require([ (target, "info.vdf", FileType.VDF) @@ -1140,7 +1140,7 @@ void RemoveBaseSchemeValues(string folderPath, IEnumerable baseFiles) }; } - private static Action? CopyFiles(HUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies) + private static Action? CopyFiles(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies) { return (writer) => { diff --git a/src/HUDMerger/Models/HUDLayout.cs b/src/HUDMerger/Models/HUDLayout.cs index 7d46589..36bbe5d 100644 --- a/src/HUDMerger/Models/HUDLayout.cs +++ b/src/HUDMerger/Models/HUDLayout.cs @@ -17,9 +17,9 @@ public HUDLayout() { } - public HUDLayout(HUDFileReaderService reader, HUD hud) + public HUDLayout(IHUDFileReaderService reader, HUD hud) { - static Dictionary>? ReadBaseFile(HUDFileReaderService reader, HUD hud, string relativePath) + static Dictionary>? ReadBaseFile(IHUDFileReaderService reader, HUD hud, string relativePath) { KeyValues? keyValues = reader.TryReadKeyValues(hud, relativePath); if (keyValues == null) return null; diff --git a/src/HUDMerger/Models/Scheme/ClientScheme.cs b/src/HUDMerger/Models/Scheme/ClientScheme.cs index 48d8acc..775dbc7 100644 --- a/src/HUDMerger/Models/Scheme/ClientScheme.cs +++ b/src/HUDMerger/Models/Scheme/ClientScheme.cs @@ -10,11 +10,11 @@ public ClientScheme() : base() { } - public ClientScheme(HUDFileReaderService reader, HUD hud, string relativePath) : base(reader, hud, relativePath) + public ClientScheme(IHUDFileReaderService reader, HUD hud, string relativePath) : base(reader, hud, relativePath) { } - public ClientScheme(HUDFileReaderService reader, HUD hud, string relativePath, KeyValues keyValues) : base(reader, hud, relativePath, keyValues) + public ClientScheme(IHUDFileReaderService reader, HUD hud, string relativePath, KeyValues keyValues) : base(reader, hud, relativePath, keyValues) { } } diff --git a/src/HUDMerger/Models/Scheme/SchemeBase.cs b/src/HUDMerger/Models/Scheme/SchemeBase.cs index 81353f9..c0b13d3 100644 --- a/src/HUDMerger/Models/Scheme/SchemeBase.cs +++ b/src/HUDMerger/Models/Scheme/SchemeBase.cs @@ -29,13 +29,13 @@ public SchemeBase() { } - public SchemeBase(HUDFileReaderService reader, HUD hud, string relativePath) : this(reader, hud, relativePath, reader.ReadKeyValues(hud, relativePath)) + public SchemeBase(IHUDFileReaderService reader, HUD hud, string relativePath) : this(reader, hud, relativePath, reader.ReadKeyValues(hud, relativePath)) { } - public SchemeBase(HUDFileReaderService reader, HUD hud, string relativePath, KeyValues keyValues) + public SchemeBase(IHUDFileReaderService reader, HUD hud, string relativePath, KeyValues keyValues) { - static SchemeFile? ReadBaseFile(HUDFileReaderService reader, HUD hud, string relativePath) + static SchemeFile? ReadBaseFile(IHUDFileReaderService reader, HUD hud, string relativePath) { KeyValues? keyValues = reader.TryReadKeyValues(hud, relativePath); if (keyValues == null) return null; diff --git a/src/HUDMerger/Models/Scheme/SourceScheme.cs b/src/HUDMerger/Models/Scheme/SourceScheme.cs index 764876e..9cd2ebb 100644 --- a/src/HUDMerger/Models/Scheme/SourceScheme.cs +++ b/src/HUDMerger/Models/Scheme/SourceScheme.cs @@ -10,11 +10,11 @@ public SourceScheme() : base() { } - public SourceScheme(HUDFileReaderService reader, HUD hud, string relativePath) : base(reader, hud, relativePath) + public SourceScheme(IHUDFileReaderService reader, HUD hud, string relativePath) : base(reader, hud, relativePath) { } - public SourceScheme(HUDFileReaderService reader, HUD hud, string relativePath, KeyValues keyValues) : base(reader, hud, relativePath, keyValues) + public SourceScheme(IHUDFileReaderService reader, HUD hud, string relativePath, KeyValues keyValues) : base(reader, hud, relativePath, keyValues) { } } diff --git a/src/HUDMerger/Services/HUDFileReaderService.cs b/src/HUDMerger/Services/HUDFileReaderService.cs index b220905..4af7ee8 100644 --- a/src/HUDMerger/Services/HUDFileReaderService.cs +++ b/src/HUDMerger/Services/HUDFileReaderService.cs @@ -21,7 +21,7 @@ public enum FileType : byte HUDAnimations = 1 } -public partial class HUDFileReaderService +public partial class HUDFileReaderService : IHUDFileReaderService { private readonly Lazy TF2MiscDirVPK = new(() => new(Path.Join(((App)Application.Current).Settings.Value.TeamFortress2Folder, "tf\\tf2_misc_dir.vpk"))); private readonly Lazy PlatformMiscDirVPK = new(() => new(Path.Join(((App)Application.Current).Settings.Value.TeamFortress2Folder, "platform\\platform_misc_dir.vpk"))); diff --git a/src/HUDMerger/Services/HUDFileWriterService.cs b/src/HUDMerger/Services/HUDFileWriterService.cs index a4626f7..35c3913 100644 --- a/src/HUDMerger/Services/HUDFileWriterService.cs +++ b/src/HUDMerger/Services/HUDFileWriterService.cs @@ -9,7 +9,7 @@ namespace HUDMerger.Services; -public class HUDFileWriterService(string folderPath) +public class HUDFileWriterService(string folderPath) : IHUDFileWriterService { private void Write(string relativePath, string text, Encoding? encoding) { diff --git a/src/HUDMerger/Services/IHUDFileReaderService.cs b/src/HUDMerger/Services/IHUDFileReaderService.cs new file mode 100644 index 0000000..f8a47c5 --- /dev/null +++ b/src/HUDMerger/Services/IHUDFileReaderService.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using HUDAnimations.Models; +using HUDMerger.Models; +using VDF.Models; + +namespace HUDMerger.Services; + +public interface IHUDFileReaderService +{ + public void Require(IEnumerable<(HUD hud, string relativePath, FileType type)> filePaths); + + public KeyValues ReadKeyValues(HUD hud, string relativePath); + public KeyValues? TryReadKeyValues(HUD hud, string relativePath); + + public HUDAnimationsFile ReadHUDAnimations(HUD hud, string relativePath); + public HUDAnimationsFile? TryReadHUDAnimations(HUD hud, string relativePath); +} diff --git a/src/HUDMerger/Services/IHUDFileWriterService.cs b/src/HUDMerger/Services/IHUDFileWriterService.cs new file mode 100644 index 0000000..a405daf --- /dev/null +++ b/src/HUDMerger/Services/IHUDFileWriterService.cs @@ -0,0 +1,14 @@ +using System; +using System.Text; +using HUDAnimations.Models; +using HUDMerger.Models; +using VDF.Models; + +namespace HUDMerger.Services; + +public interface IHUDFileWriterService +{ + public void Write(string relativePath, KeyValues keyValues, Encoding? encoding = default); + public void Write(string relativePath, HUDAnimationsFile keyValues, Encoding? encoding = default); + public void Copy(HUD source, string relativePath); +}