Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
cooolbros committed Nov 13, 2024
2 parents 7bd763c + 53cb2c7 commit 6a285f5
Show file tree
Hide file tree
Showing 60 changed files with 599 additions and 315 deletions.
10 changes: 10 additions & 0 deletions src/HUDMerger.Core/App.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Text.RegularExpressions;

namespace HUDMerger.Core;

public partial class App
{
[GeneratedRegex(@"[/\\]+", RegexOptions.Compiled)]
public static partial Regex PathSeparatorRegex();
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Windows.Input;

namespace HUDMerger.Commands;
namespace HUDMerger.Core.Commands;

public abstract class CommandBase : ICommand, IDisposable
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,25 @@
using System.IO;
using System.Linq;
using System.Windows;
using HUDMerger.Models;
using HUDMerger.ViewModels;
using HUDMerger.Core.Models;
using HUDMerger.Core.Services;
using HUDMerger.Core.ViewModels;

namespace HUDMerger.Commands;
namespace HUDMerger.Core.Commands;

public class MergeCommand : CommandBase
{
private bool _disposed;
private readonly MainWindowViewModel _mainWindowViewModel;
private readonly ISettingsService SettingsService;
private readonly IMessageBoxService MessageBoxService;

public MergeCommand(MainWindowViewModel mainWindowViewModel)
public MergeCommand(MainWindowViewModel mainWindowViewModel, ISettingsService settingsService, IMessageBoxService messageBoxService)
{
_mainWindowViewModel = mainWindowViewModel;
_mainWindowViewModel.PropertyChanged += _mainWindowViewModel_PropertyChanged;
SettingsService = settingsService;
MessageBoxService = messageBoxService;
}

private void _mainWindowViewModel_PropertyChanged(object? sender, PropertyChangedEventArgs e)
Expand All @@ -44,7 +49,7 @@ static bool PathContainsPath(string parentDir, string subDir)
return !relativeDirectory.StartsWith("..") && !Path.IsPathRooted(relativeDirectory);
}

bool teamFortress2FolderContainsTarget = PathContainsPath(Path.Join(((App)Application.Current).Settings.Value.TeamFortress2Folder, "tf\\custom"), _mainWindowViewModel.TargetHUD!.FolderPath);
bool teamFortress2FolderContainsTarget = PathContainsPath(Path.Join(SettingsService.Settings.TeamFortress2Folder, "tf\\custom"), _mainWindowViewModel.TargetHUD!.FolderPath);

Process[] processes;

Expand All @@ -63,7 +68,7 @@ static bool PathContainsPath(string parentDir, string subDir)

if (teamFortress2FolderContainsTarget && processes.Length != 0)
{
MessageBox.Show("TF2 process open, cannot merge!", "TF2 Open Error", MessageBoxButton.OK, MessageBoxImage.Error);
MessageBoxService.ShowException(new Exception("TF2 process open, cannot merge!"), "TF2 Open Error");
return;
}
#endif
Expand All @@ -74,14 +79,15 @@ static bool PathContainsPath(string parentDir, string subDir)
_mainWindowViewModel.HUDPanelViewModels
.Where((hudPanelViewModel) => hudPanelViewModel.Selected)
.Select((hudPanelViewModel) => hudPanelViewModel.HUDPanel)
.ToArray()
.ToArray(),
SettingsService
);

MessageBox.Show("Done!");
MessageBoxService.Show("Done!");
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
MessageBoxService.ShowException(e, "Error");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using HUDMerger.Models;
using HUDMerger.Core.Models;
using VDF.Exceptions;

namespace HUDMerger.Exceptions;
namespace HUDMerger.Core.Exceptions;

public class FileException(HUD hud, string relativePath, VDFSyntaxException innerException) : Exception($"{hud.Name}: {App.PathSeparatorRegex().Replace(relativePath, "/")}: {innerException.Message}")
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.IO;
using System.Text;

namespace HUDMerger.Extensions;
namespace HUDMerger.Core.Extensions;

public static class BinaryReaderExtensions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using VDF.Models;

namespace HUDMerger.Extensions;
namespace HUDMerger.Core.Extensions;

public static class HashSetExtensions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using VDF;
using VDF.Models;

namespace HUDMerger.Extensions;
namespace HUDMerger.Core.Extensions;

public static class KeyValuesExtensions
{
Expand Down
24 changes: 24 additions & 0 deletions src/HUDMerger.Core/HUDMerger.Core.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>HUDMerger.Core</RootNamespace>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Toolkit.Mvvm" Version="7.1.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\HUDAnimations\HUDAnimations.csproj" />
<ProjectReference Include="..\VDF\VDF.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="Resources\**">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
using System.IO;
using System.Linq;
using System.Text.Json;
using HUDMerger.Extensions;
using HUDMerger.Services;
using HUDMerger.Core.Extensions;
using HUDMerger.Core.Services;
using VDF;
using VDF.Models;

namespace HUDMerger.Models;
namespace HUDMerger.Core.Models;

public class Dependencies
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;

namespace HUDMerger.Models;
namespace HUDMerger.Core.Models;

public class FilesHashSet : HashSet<string>
{
Expand Down
44 changes: 22 additions & 22 deletions src/HUDMerger/Models/HUD.cs → src/HUDMerger.Core/Models/HUD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
using System.Windows;
using HUDAnimations.Models;
using HUDAnimations.Models.Animations;
using HUDMerger.Exceptions;
using HUDMerger.Extensions;
using HUDMerger.Models.Scheme;
using HUDMerger.Services;
using HUDMerger.Core.Exceptions;
using HUDMerger.Core.Extensions;
using HUDMerger.Core.Models.Scheme;
using HUDMerger.Core.Services;
using VDF;
using VDF.Models;

namespace HUDMerger.Models;
namespace HUDMerger.Core.Models;

/// <summary>
/// Represents a custom HUD
Expand Down Expand Up @@ -118,7 +118,7 @@ bool TestKeyValues(KeyValues keyValues)
/// <param name="source">HUD to merge panels from</param>
/// <param name="target">HUD to merge panels to</param>
/// <param name="panels">Panels</param>
public static void Merge(HUD source, HUD target, HUDPanel[] panels)
public static void Merge(HUD source, HUD target, HUDPanel[] panels, ISettingsService settingsService)
{
Dependencies dependencies = new(panels.Select((panel) => panel.Dependencies).OfType<Dependencies>());
dependencies.Files.UnionWith(panels.Select((panel) => panel.Main).Where((main) => !string.IsNullOrEmpty(main)));
Expand All @@ -129,9 +129,9 @@ public static void Merge(HUD source, HUD target, HUDPanel[] panels)
.SelectMany((files) => files)
);

IHUDFileReaderService reader = new HUDFileReaderService();
IHUDFileReaderService reader = new HUDFileReaderService(settingsService);

Func<IHUDFileReaderService, HUD, HUD, Dependencies, Action<IHUDFileWriterService>?>[] actions =
Func<IHUDFileReaderService, HUD, HUD, Dependencies, ISettingsService, Action<IHUDFileWriterService>?>[] actions =
[
AddDependencies,
MergeHUDLayout,
Expand All @@ -149,11 +149,11 @@ public static void Merge(HUD source, HUD target, HUDPanel[] panels)
List<Action<IHUDFileWriterService>> commitActions = [];
List<Exception> exceptions = [];

foreach (Func<IHUDFileReaderService, HUD, HUD, Dependencies, Action<IHUDFileWriterService>?> action in actions)
foreach (Func<IHUDFileReaderService, HUD, HUD, Dependencies, ISettingsService, Action<IHUDFileWriterService>?> action in actions)
{
try
{
Action<IHUDFileWriterService>? result = action(reader, source, target, dependencies);
Action<IHUDFileWriterService>? result = action(reader, source, target, dependencies, settingsService);
if (result != null)
{
commitActions.Add(result);
Expand Down Expand Up @@ -186,13 +186,13 @@ public static void Merge(HUD source, HUD target, HUDPanel[] panels)
}
}

private static Action<IHUDFileWriterService>? AddDependencies(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies)
private static Action<IHUDFileWriterService>? AddDependencies(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies, ISettingsService settingsService)
{
dependencies.Add(reader, source);
return null;
}

private static Action<IHUDFileWriterService>? MergeHUDLayout(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies)
private static Action<IHUDFileWriterService>? MergeHUDLayout(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies, ISettingsService settingsService)
{
if (dependencies.HUDLayout.Count == 0)
{
Expand Down Expand Up @@ -377,7 +377,7 @@ void RemoveBaseHUDLayoutEntries(string folderPath, IEnumerable<string> baseFiles
};
}

private static Action<IHUDFileWriterService>? MergeHUDAnimations(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies)
private static Action<IHUDFileWriterService>? MergeHUDAnimations(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies, ISettingsService settingsService)
{
if (dependencies.Events.Count == 0)
{
Expand Down Expand Up @@ -503,7 +503,7 @@ bool SourceHUDAnimationsFileExists(KeyValue kv)
};
}

private static Action<IHUDFileWriterService>? MergeClientScheme(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies)
private static Action<IHUDFileWriterService>? MergeClientScheme(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies, ISettingsService settingsService)
{
if (!dependencies.ClientScheme.Any())
{
Expand Down Expand Up @@ -732,7 +732,7 @@ If the value is a string select the string value
};
}

private static Action<IHUDFileWriterService>? MergeSourceScheme(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies)
private static Action<IHUDFileWriterService>? MergeSourceScheme(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies, ISettingsService settingsService)
{
if (!dependencies.SourceScheme.Any() || dependencies.Files.Contains("resource\\sourcescheme.res"))
{
Expand Down Expand Up @@ -966,7 +966,7 @@ void RemoveBaseSchemeValues(string folderPath, IEnumerable<string> baseFiles)
};
}

private static Action<IHUDFileWriterService>? MergeLanguageTokens(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies)
private static Action<IHUDFileWriterService>? MergeLanguageTokens(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies, ISettingsService settingsService)
{
if (dependencies.LanguageTokens.Count == 0)
{
Expand All @@ -980,7 +980,7 @@ void RemoveBaseSchemeValues(string folderPath, IEnumerable<string> baseFiles)

List<string> languages = ["english"];

string settingsLanguage = ((App)Application.Current).Settings.Value.Language;
string settingsLanguage = settingsService.Settings.Language;

if (settingsLanguage != "english")
{
Expand Down Expand Up @@ -1103,7 +1103,7 @@ void RemoveBaseSchemeValues(string folderPath, IEnumerable<string> baseFiles)
};
}

private static Action<IHUDFileWriterService>? MergeImages(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies)
private static Action<IHUDFileWriterService>? MergeImages(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies, ISettingsService settingsService)
{
IEnumerable<string> images = dependencies.Images.Concat(dependencies.PreloadImages);

Expand Down Expand Up @@ -1132,7 +1132,7 @@ void RemoveBaseSchemeValues(string folderPath, IEnumerable<string> baseFiles)
return null;
}

private static Action<IHUDFileWriterService>? MergePreloadImages(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies)
private static Action<IHUDFileWriterService>? MergePreloadImages(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies, ISettingsService settingsService)
{
if (dependencies.PreloadImages.Count == 0)
{
Expand Down Expand Up @@ -1198,13 +1198,13 @@ void RemoveBaseSchemeValues(string folderPath, IEnumerable<string> baseFiles)
};
}

private static Action<IHUDFileWriterService>? MergeAudio(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies)
private static Action<IHUDFileWriterService>? MergeAudio(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies, ISettingsService settingsService)
{
dependencies.Files.UnionWith(dependencies.Audio);
return null;
}

private static Action<IHUDFileWriterService>? MergeInfoVDF(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies)
private static Action<IHUDFileWriterService>? MergeInfoVDF(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies, ISettingsService settingsService)
{
reader.Require([
(target, "info.vdf", FileType.VDF)
Expand Down Expand Up @@ -1233,7 +1233,7 @@ void RemoveBaseSchemeValues(string folderPath, IEnumerable<string> baseFiles)
};
}

private static Action<IHUDFileWriterService>? CopyFiles(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies)
private static Action<IHUDFileWriterService>? CopyFiles(IHUDFileReaderService reader, HUD source, HUD target, Dependencies dependencies, ISettingsService settingsService)
{
return (writer) =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using HUDMerger.Extensions;
using HUDMerger.Services;
using HUDMerger.Core.Extensions;
using HUDMerger.Core.Services;
using VDF;
using VDF.Models;

namespace HUDMerger.Models;
namespace HUDMerger.Core.Models;

public class HUDLayout
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;

namespace HUDMerger.Models;
namespace HUDMerger.Core.Models;

public record class HUDPanel
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace HUDMerger.Models;
namespace HUDMerger.Core.Models;

/// <summary>
/// Stores a file and key path to a Key/Value
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using HUDMerger.Services;
using HUDMerger.Core.Services;
using VDF.Models;

namespace HUDMerger.Models.Scheme;
namespace HUDMerger.Core.Models.Scheme;

public class ClientScheme : SchemeBase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using VDF.Models;

namespace HUDMerger.Models.Scheme;
namespace HUDMerger.Core.Models.Scheme;

public interface IScheme
{
Expand Down
Loading

0 comments on commit 6a285f5

Please sign in to comment.