Skip to content

Commit

Permalink
Pak configuration support
Browse files Browse the repository at this point in the history
  • Loading branch information
TekkaGB committed Mar 6, 2022
1 parent 8a5fc57 commit 8558ea1
Show file tree
Hide file tree
Showing 11 changed files with 208 additions and 10 deletions.
32 changes: 32 additions & 0 deletions Unverum/FileNameConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.IO;
using System.Windows.Data;
using System.Windows.Media;

namespace Unverum.UI
{
public class FileNameConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return Path.GetFileNameWithoutExtension((string)value);
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}
public class PathNameConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return ((string)value).Replace($@"{Global.assemblyLocation}{Global.s}Mods{Global.s}{Global.config.CurrentGame}", "...");
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}
}
14 changes: 8 additions & 6 deletions Unverum/ModLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,14 @@ private static void RestoreDirectory(string path)
if (File.Exists($"{file}.bak"))
File.Move($"{file}.bak", file, true);
}
private static int CopyFolder(string sourcePath, string targetPath, string defaultSig)
private static int CopyFolder(Dictionary<string, bool> paks, string sourcePath, string targetPath, string defaultSig)
{
var counter = 0;
//Copy all the files & Replaces any files with the same name
foreach (var path in Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories))
{
if (Path.GetExtension(path).Equals(".pak", StringComparison.InvariantCultureIgnoreCase))
if (Path.GetExtension(path).Equals(".pak", StringComparison.InvariantCultureIgnoreCase)
&& paks.ContainsKey(path) && paks[path])
{
var newPath = path.Replace(sourcePath, targetPath).Replace(".pak", "_9_P.pak");
Directory.CreateDirectory(Path.GetDirectoryName(newPath));
Expand Down Expand Up @@ -150,7 +151,7 @@ private static void PakFiles(string path, string output, string sig)
Global.logger.WriteLine($"Failed to create pak!", LoggerType.Error);
}
// Copy over mod files in order of ModList
public static void Build(string path, List<string> mods, bool? patched, string movies, string splash, string sound)
public static void Build(string path, List<Mod> mods, bool? patched, string movies, string splash, string sound)
{
var missing = false;
Dictionary<string, Entry> entries = null;
Expand All @@ -167,9 +168,10 @@ public static void Build(string path, List<string> mods, bool? patched, string m
foreach (var tilde in Enumerable.Range(0, tildes))
priorityName += "~";
priorityName += folderLetter;
var folder = $"{path}{Global.s}{priorityName}{Global.s}{Path.GetFileName(mod)}";
var folder = $"{path}{Global.s}{priorityName}{Global.s}{mod.name}";
var modPath = $@"{Global.assemblyLocation}{Global.s}Mods{Global.s}{Global.config.CurrentGame}{Global.s}{mod.name}";
// Copy over .paks and .sigs to ~mods folder in order
if (CopyFolder(mod, folder, sig) > 0)
if (CopyFolder(mod.paks, modPath, folder, sig) > 0)
{
Global.logger.WriteLine($"Copied paks and sigs from {mod} over to {folder}", LoggerType.Info);
folderLetter++;
Expand All @@ -180,7 +182,7 @@ public static void Build(string path, List<string> mods, bool? patched, string m
}
}
// Copy over mp4s and bmps to the appropriate folders while storing backups
foreach (var file in Directory.GetFiles(mod, "*", SearchOption.AllDirectories))
foreach (var file in Directory.GetFiles(modPath, "*", SearchOption.AllDirectories))
{
var ext = Path.GetExtension(file).ToLowerInvariant();
switch (ext)
Expand Down
5 changes: 5 additions & 0 deletions Unverum/StringConverters.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand All @@ -8,6 +9,10 @@ namespace Unverum
{
public static class StringConverters
{
public static string FormatFileName(string filename)
{
return Path.GetFileName(filename);
}
// Load all suffixes in an array
static readonly string[] suffixes =
{ " Bytes", " KB", " MB", " GB", " TB", " PB" };
Expand Down
1 change: 1 addition & 0 deletions Unverum/Structures/ModStructures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class Mod
{
public string name { get; set; }
public bool enabled { get; set; }
public Dictionary<string, bool> paks { get; set; }
}
public class Metadata
{
Expand Down
78 changes: 78 additions & 0 deletions Unverum/UI/ConfigurePaksWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<Window x:Class="Unverum.UI.ConfigurePaksWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Unverum.UI"
xmlns:fa5="http://schemas.fontawesome.com/icons/"
mc:Ignorable="d"
Title="Unverum" WindowStartupLocation="CenterScreen"
ResizeMode="NoResize" MinWidth="400" MaxWidth="750"
Background="#202020" ShowActivated="True" SizeToContent="WidthAndHeight">
<Window.Resources>
<local:FileNameConverter x:Key="FileNameConverter"/>
<local:PathNameConverter x:Key="PathNameConverter"/>
<Style x:Key="ToggleButtonStyle" TargetType="{x:Type ToggleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border x:Name="border" Background="#353535">
<ContentPresenter/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="true">
<Setter Property="Background" TargetName="border" Value="#006ac1"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<DataTemplate x:Key="PakTemplate">
<ToggleButton Click="ToggleButton_Click" IsChecked="{Binding Value, Mode=OneWay}" Margin="5" HorizontalContentAlignment="Stretch" Background="#494949" Style="{StaticResource ToggleButtonStyle}">
<ToggleButton.Resources>
<Style TargetType="{x:Type Border}">
<Setter Property="CornerRadius" Value="2"/>
</Style>
</ToggleButton.Resources>
<Grid VerticalAlignment="Center" x:Name="GridItem" Margin="3">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Name="PakText" Grid.Row="0" TextTrimming="CharacterEllipsis" FontSize="18" FontWeight="SemiBold" Foreground="#f2f2f2" Text="{Binding Key, Mode=OneWay, Converter={StaticResource FileNameConverter}}"/>
<TextBlock Name="PathText" Grid.Row="1" TextTrimming="CharacterEllipsis" FontSize="13" TextWrapping="WrapWithOverflow" Foreground="#a2a2a2" Text="{Binding Key, Mode=OneWay, Converter={StaticResource PathNameConverter}}"/>
</Grid>
</ToggleButton>
</DataTemplate>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ListView MaxHeight="500" Style="{StaticResource ListviewStyle}" Name="PakList" Grid.Row="0" Background="Transparent" BorderThickness="0" ItemTemplate="{StaticResource PakTemplate}">
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="Focusable" Value="false"/>
<Setter Property="Background" Value="Transparent" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<ContentPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
</ListView>
<Button Grid.Row="1" HorizontalContentAlignment="Stretch" Margin="5" Name="CloseButton" Content=" Close" FontSize="18" FontWeight="SemiBold" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Height="40" Background="#494949" Foreground="#f2f2f2" Click="CloseButton_Click">
<Button.Resources>
<Style TargetType="{x:Type Border}">
<Setter Property="CornerRadius" Value="2"/>
</Style>
</Button.Resources>
</Button>
</Grid>
</Window>
43 changes: 43 additions & 0 deletions Unverum/UI/ConfigurePaksWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.IO;
using System.Reflection;
using System.Windows.Input;
using System.Windows.Controls.Primitives;
using System.Collections.ObjectModel;

namespace Unverum.UI
{
/// <summary>
/// Interaction logic for ConfigurePaksWindow.xaml
/// </summary>
public partial class ConfigurePaksWindow : Window
{
public Mod _mod;
public ConfigurePaksWindow(Mod mod)
{
InitializeComponent();
if (mod != null)
{
_mod = mod;
PakList.ItemsSource = new ObservableCollection<KeyValuePair<string, bool>>(_mod.paks);
Title = $"Configure Paks for {_mod.name}";
}
}

private void CloseButton_Click(object sender, RoutedEventArgs e)
{
Close();
}
private void ToggleButton_Click(object sender, RoutedEventArgs e)
{
ToggleButton button = sender as ToggleButton;
var item = button.DataContext as KeyValuePair<string, bool>?;
_mod.paks[item.Value.Key] = (bool)button.IsChecked;
}
}
}
2 changes: 1 addition & 1 deletion Unverum/UI/EditWindow.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Window x:Class="Unverum.EditWindow"
<Window x:Class="Unverum.UI.EditWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
Expand Down
2 changes: 1 addition & 1 deletion Unverum/UI/EditWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using System.Reflection;
using System.Windows.Input;

namespace Unverum
namespace Unverum.UI
{
/// <summary>
/// Interaction logic for EditWindow.xaml
Expand Down
1 change: 1 addition & 0 deletions Unverum/UI/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@
<ContextMenu>
<MenuItem Header="Fetch Metadata" IsCheckable="False" Click="FetchItem_Click"/>
<MenuItem Header="Rename Mod" IsCheckable="False" Click="EditItem_Click"/>
<MenuItem Header="Configure Paks" IsCheckable="False" Click="ConfigurePaksItem_Click"/>
<MenuItem Header="Open Mod Folder" IsCheckable="False" Click="OpenItem_Click"/>
<MenuItem Header="Delete Mod" IsCheckable="False" Click="DeleteItem_Click"/>
</ContextMenu>
Expand Down
36 changes: 34 additions & 2 deletions Unverum/UI/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,22 @@ private async void Refresh()
Global.ModList.Remove(mod);
});
Global.logger.WriteLine($"Deleted {mod.name}", LoggerType.Info);
continue;
}
// Update all paks found
if (mod.paks == null)
mod.paks = new();
foreach (var file in Directory.GetFiles($"{currentModDirectory}{Global.s}{mod.name}", "*.*", SearchOption.AllDirectories))
{
if (Path.GetExtension(file).Equals(".pak", StringComparison.InvariantCultureIgnoreCase)
&& !mod.paks.ContainsKey(file))
mod.paks.Add(file, true); // Enable all paks when first added
}
// Remove all paks that no longer exist
foreach (var pak in mod.paks.Keys)
{
if (!File.Exists(pak))
mod.paks.Remove(pak);
}
}

Expand Down Expand Up @@ -642,7 +658,7 @@ private async Task<bool> Build(string path)
CostumePatched = Setup.CheckCostumePatch(Global.config.Configs[Global.config.CurrentGame].Launcher);
if (!ModLoader.Restart(path, MoviesFolder, SplashFolder, SoundsFolder))
return false;
List<string> mods = Global.config.Configs[Global.config.CurrentGame].ModList.Where(x => x.enabled).Select(y => $@"{Global.assemblyLocation}{Global.s}Mods{Global.s}{Global.config.CurrentGame}{Global.s}{y.name}").ToList();
var mods = Global.config.Configs[Global.config.CurrentGame].ModList.Where(x => x.enabled).ToList();
mods.Reverse();
// Rename HeroGame back since its no longer needed to be renamed
Expand Down Expand Up @@ -719,6 +735,22 @@ private void EditItem_Click(object sender, RoutedEventArgs e)
ew.ShowDialog();
}
}
private void ConfigurePaksItem_Click(object sender, RoutedEventArgs e)
{
var selectedMods = ModGrid.SelectedItems;
var temp = new Mod[selectedMods.Count];
selectedMods.CopyTo(temp, 0);
bool edited = false;
foreach (var row in temp)
if (row != null)
{
ConfigurePaksWindow cpw = new ConfigurePaksWindow(row);
cpw.ShowDialog();
var index = Global.ModList.IndexOf(row);
Global.ModList[index] = cpw._mod;
Global.UpdateConfig();
}
}
private void FetchItem_Click(object sender, RoutedEventArgs e)
{
var selectedMods = ModGrid.SelectedItems;
Expand Down Expand Up @@ -1866,7 +1898,7 @@ private async void SortAlphabeticallyAndGroupEnabled_Click(object sender, Routed
ModGrid.ItemsSource = Global.ModList;
});
});
Global.config.Configs[Global.config.CurrentGame].ModList = Global.ModList;
Global.UpdateConfig();
}
e.Handled = true;
}
Expand Down
4 changes: 4 additions & 0 deletions Unverum/Unverum.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,10 @@
<XamlRuntime>$(DefaultXamlRuntime)</XamlRuntime>
<SubType>Designer</SubType>
</Page>
<Page Update="UI\ConfigurePaksWindow.xaml">
<XamlRuntime>$(DefaultXamlRuntime)</XamlRuntime>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Update="UI\FetchWindow.xaml">
<XamlRuntime>$(DefaultXamlRuntime)</XamlRuntime>
<SubType>Designer</SubType>
Expand Down

0 comments on commit 8558ea1

Please sign in to comment.