Skip to content

Commit

Permalink
add import from zip option
Browse files Browse the repository at this point in the history
  • Loading branch information
katycat5e committed Jul 25, 2023
1 parent ded476c commit 3d48156
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 14 deletions.
12 changes: 8 additions & 4 deletions SkinConfigurator/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
xmlns:local="clr-namespace:SkinConfigurator"
mc:Ignorable="d"
Title="Skin Configurator" Height="450" Width="800"
d:DataContext="{d:DesignInstance {x:Type local:SkinPackModel}}">
d:DataContext="{d:DesignInstance {x:Type local:SkinPackModel}}"
Closing="Window_Closing">
<Window.Resources>
<local:NullToVisibilityConverter x:Key="nullVisibilityConverter"/>
<local:NullToBoolConverter x:Key="nullToBoolConverter"/>
Expand All @@ -28,7 +29,10 @@
<DockPanel LastChildFill="False">
<Menu DockPanel.Dock="Top">
<MenuItem x:Name="CreatePackButton" Header="New Pack" Click="CreatePackButton_Click" ToolTip="Clear all fields and start anew"/>
<MenuItem x:Name="ImportPackButton" Header="Import Pack..." Click="ImportPackButton_Click" ToolTip="Import multiple skins from a folder"/>
<MenuItem Header="Import...">
<MenuItem x:Name="ImportPackButton" Header="Import Folder..." Click="ImportPackButton_Click" ToolTip="Import multiple skins from a folder"/>
<MenuItem x:Name="ImportZipButton" Header="Import Old Zip..." Click="ImportZipButton_Click" ToolTip="Import skins from an old UMM or BepInEx pack"/>
</MenuItem>
</Menu>

<Grid DataContext="{Binding ModInfoModel}" DockPanel.Dock="Top"
Expand Down Expand Up @@ -58,8 +62,8 @@
IsEnabled="{Binding IsValid}"
Background="#FF94E2A4" Padding="0,10"/>

<Button x:Name="TestPackButton" DockPanel.Dock="Bottom" Content="Save for Testing..." Click="TestPackButton_Click"
ToolTip="Save the pack directly to a folder (bypass UMM)"
<Button x:Name="TestPackButton" DockPanel.Dock="Bottom" Content="Install Unzipped..." Click="TestPackButton_Click"
ToolTip="Save the pack directly to a folder or mods directory (bypass UMM)"
IsEnabled="{Binding IsValid}"
Background="#FFFFFF80" Padding="0,10"/>
</DockPanel>
Expand Down
42 changes: 39 additions & 3 deletions SkinConfigurator/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ private void ImportPackButton_Click(object sender, RoutedEventArgs e)
{
Description = "Select root folder of skin",
UseDescriptionForTitle = true,
SelectedPath = Settings.DefaultSkinWorkFolder,
};

var result = folderDialog.ShowDialog();
Expand All @@ -55,6 +56,30 @@ private void ImportPackButton_Click(object sender, RoutedEventArgs e)
}
}

private void ImportZipButton_Click(object sender, RoutedEventArgs e)
{
var dialog = new OpenFileDialog()
{
Title = "Select Zipped Skin Pack",
DefaultExt = ".zip",
Filter = "Zip Archives (.zip)|*.zip",
InitialDirectory = Settings.DefaultSkinWorkFolder,
};

bool? result = dialog.ShowDialog(this);

if (result == true)
{
if (!File.Exists(dialog.FileName))
{
MessageBox.Show(this, "Selected path is not a valid Zip Archive", "Invalid Path", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}

Model = PackImporter.ImportFromArchive(dialog.FileName);
}
}

private void AddSkinButton_Click(object sender, RoutedEventArgs e)
{
using var folderDialog = new System.Windows.Forms.FolderBrowserDialog()
Expand Down Expand Up @@ -164,13 +189,11 @@ private void PackageButton_Click(object sender, RoutedEventArgs e)

private void TestPackButton_Click(object sender, RoutedEventArgs e)
{
string defaultPath = Path.Combine(Settings.DerailValleyDirectory, Model.ModInfoModel.Id!);

var dialog = new System.Windows.Forms.FolderBrowserDialog()
{
Description = "Select destination folder for skins",
UseDescriptionForTitle = true,
SelectedPath = defaultPath,
InitialDirectory = Settings.DerailValleyDirectory,
ShowNewFolderButton = true,
};

Expand Down Expand Up @@ -212,5 +235,18 @@ private void SettingsButton_Click(object sender, RoutedEventArgs e)
ConfiguratorSettings.SaveConfig(Settings);
}
}

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
try
{
string tempFolder = Path.Combine(Environment.CurrentDirectory, "Temp");
if (Directory.Exists(tempFolder))
{
Directory.Delete(tempFolder, true);
}
}
catch { }
}
}
}
49 changes: 42 additions & 7 deletions SkinConfigurator/PackImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace SkinConfigurator
Expand All @@ -13,21 +15,31 @@ internal static class PackImporter
public static SkinPackModel ImportFromFolder(string path)
{
var model = new SkinPackModel();
model.ModInfoModel.BindingName = Path.GetFileName(path);
model.ModInfoModel.BindingName = GetCleanSkinName(path);

foreach (string skinFolder in EnumerateSkinFolders(path))
{
var skin = new SkinConfigModel(skinFolder)
{
BindingCarId = TryExtractCarType(skinFolder)
};

model.SkinConfigModels.Add(skin);
model.AddSkinConfig(skinFolder, TryExtractCarType(skinFolder));
}

return model;
}

private static readonly Regex _nexusNameRegex = new(@"^([\w ]+)(?:[-\d]+)$");

private static string GetCleanSkinName(string folderPath)
{
string folderName = Path.GetFileName(folderPath);

var nexusNameMatch = _nexusNameRegex.Match(folderName);
if (nexusNameMatch.Success)
{
folderName = nexusNameMatch.Groups[1].Value;
}

return folderName.Replace('_', ' ');
}

private static IEnumerable<string> EnumerateSkinFolders(string path)
{
if (Directory.EnumerateFiles(path).Any(f => Constants.IsSupportedExtension(Path.GetExtension(f))))
Expand Down Expand Up @@ -61,5 +73,28 @@ private static IEnumerable<string> EnumerateSkinFolders(string path)

return null;
}

public static SkinPackModel ImportFromArchive(string archivePath)
{
using var stream = File.OpenRead(archivePath);
using var archive = new ZipArchive(stream, ZipArchiveMode.Read);

string tempFolder = ExtractArchiveToTemp(archive, Path.GetFileNameWithoutExtension(archivePath));
return ImportFromFolder(tempFolder);
}

private static string ExtractArchiveToTemp(ZipArchive archive, string archiveName)
{
string destFolder = Path.Combine(Environment.CurrentDirectory, "Temp", archiveName);

foreach (var entry in archive.Entries.Where(e => !string.IsNullOrWhiteSpace(e.Name)))
{
string destFile = Path.Combine(destFolder, entry.FullName);
Directory.CreateDirectory(Path.GetDirectoryName(destFile)!);
entry.ExtractToFile(destFile, true);
}

return destFolder;
}
}
}

0 comments on commit 3d48156

Please sign in to comment.