0
0
mirror of https://github.com/sp-tarkov/patcher.git synced 2025-02-13 06:30:45 -05:00
patcher/Patcher/PatchGenerator/MainWindow.xaml.cs

254 lines
8.2 KiB
C#
Raw Normal View History

using System;
using System.Diagnostics;
2021-08-01 00:36:37 -04:00
using System.IO;
using System.Text.RegularExpressions;
2021-08-01 00:36:37 -04:00
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using PatcherUtils;
using PatchGenerator.Extensions;
2021-08-01 00:36:37 -04:00
namespace PatchGenerator
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private string compareFolder = "";
private string targetFolder = "";
private string outputFolderName = "";
public bool AutoZip { get; set; } = true;
2021-08-01 00:36:37 -04:00
private Stopwatch stopwatch = new Stopwatch();
public MainWindow()
{
InitializeComponent();
}
public MainWindow(GenStartupArgs startupArgs = null)
{
InitializeComponent();
if (startupArgs == null) return;
FileNameBox.Text = startupArgs.OutputFolderName;
if (startupArgs.CompareFolderPath != "")
{
compareFolder = startupArgs.CompareFolderPath;
CompareLabel.Content = $"Compare Folder:\n{compareFolder}";
CompareLabel.BorderBrush = Brushes.DarkCyan;
}
if (startupArgs.TargetFolderPath != "")
{
targetFolder = startupArgs.TargetFolderPath;
TargetLabel.Content = $"Target Folder:\n{targetFolder}";
TargetLabel.BorderBrush = Brushes.DarkCyan;
}
AutoZip = startupArgs.AutoZip;
if (startupArgs.ReadyToRun)
{
GenButton_Click(null, null);
}
}
2021-08-01 00:36:37 -04:00
private string GetStopWatchTime()
{
return $"Hours: {stopwatch.Elapsed.Hours} - Mins: {stopwatch.Elapsed.Minutes} - Secs: {stopwatch.Elapsed.Seconds} - MilliSecs: {stopwatch.Elapsed.Milliseconds}";
}
private static bool FileDropCheck(DragEventArgs args, ref string str)
{
if (!args.Data.GetDataPresent(DataFormats.FileDrop))
{
return false;
}
string[] paths = (string[])args.Data.GetData(DataFormats.FileDrop);
if (paths.Length != 1) return false;
if (!Directory.Exists(paths[0]))
{
return false;
}
str = paths[0];
return true;
}
private void CompareLabel_Drop(object sender, DragEventArgs e)
{
if (FileDropCheck(e, ref compareFolder))
{
CompareLabel.Content = $"Compare Folder:\n{compareFolder}";
CompareLabel.BorderBrush = Brushes.DarkCyan;
}
else
{
MessageBox.Show("Dropped File/s could not be used. Make sure you only drop one folder.");
}
}
private void TargetLabel_Drop(object sender, DragEventArgs e)
{
if(FileDropCheck(e, ref targetFolder))
{
TargetLabel.Content = $"Target Folder:\n{targetFolder}";
TargetLabel.BorderBrush = Brushes.DarkCyan;
}
else
{
MessageBox.Show("Dropped File/s could not be used. Make sure you only drop one folder.");
}
}
private void GeneratePatches(string patchBase)
2021-08-01 00:36:37 -04:00
{
//create temp data
GenProgressBar.DispatcherSetIndetermination(true);
GenProgressMessageLabel.DispaatcherSetContent("Extracting temp data ...");
2021-08-01 00:36:37 -04:00
LazyOperations.CleanupTempDir();
2021-08-01 00:36:37 -04:00
LazyOperations.PrepTempDir();
GenProgressBar.DispatcherSetIndetermination(false);
2021-08-01 00:36:37 -04:00
//generate patches
2021-12-17 16:02:31 -05:00
//TODO - fix these weird variable names (why did I do this?)
PatchHelper patcher = new PatchHelper(compareFolder, targetFolder, patchBase);
2021-08-01 00:36:37 -04:00
2021-12-17 16:02:31 -05:00
patcher.ProgressChanged += patcher_ProgressChanged;
2021-08-01 00:36:37 -04:00
2021-12-17 16:02:31 -05:00
if (!patcher.GeneratePatches())
2021-08-01 00:36:37 -04:00
{
2021-12-17 16:02:31 -05:00
MessageBox.Show("One of the provided folder paths doesn't exist.", "Oops :(", MessageBoxButton.OK, MessageBoxImage.Error);
2021-08-01 00:36:37 -04:00
}
//Copy patch client to output folder
File.Copy(LazyOperations.PatcherClientPath, $"{outputFolderName}\\patcher.exe", true);
2021-08-01 00:36:37 -04:00
//compress patch output folder to 7z file
if (AutoZip)
{
LazyOperations.StartZipProcess(outputFolderName, $"{outputFolderName}.7z".FromCwd());
}
2021-08-01 00:36:37 -04:00
GenProgressBar.DispatcherSetValue(100);
GenProgressMessageLabel.DispaatcherSetContent("Done");
2021-08-01 00:36:37 -04:00
}
2021-12-17 16:02:31 -05:00
private void patcher_ProgressChanged(object Sender, int Progress, int Total, int Percent, string Message = "", params LineItem[] AdditionalLineItems)
2021-08-01 00:36:37 -04:00
{
string additionalInfo = "";
foreach (LineItem item in AdditionalLineItems)
2021-08-01 00:36:37 -04:00
{
additionalInfo += $"{item.ItemText}: {item.ItemValue}\n";
}
GenProgressBar.DispatcherSetValue(Percent);
2021-08-01 00:36:37 -04:00
if (!string.IsNullOrWhiteSpace(Message))
{
GenProgressMessageLabel.DispaatcherSetContent(Message);
}
2021-08-01 00:36:37 -04:00
GenProgressInfoLabel.DispaatcherSetContent($"[{Progress}/{Total}]");
AdditionalInfoBlock.DispatcherSetText(additionalInfo);
2021-08-01 00:36:37 -04:00
}
private void GenButton_Click(object sender, RoutedEventArgs e)
{
GenButton.IsEnabled = false;
CompareLabel.IsEnabled = false;
TargetLabel.IsEnabled = false;
FileNameBox.IsEnabled = false;
AutoZip_checkBox.IsEnabled = false;
string InfoNeededMessage = "You must set the following: ";
bool infoNeeded = false;
if(string.IsNullOrWhiteSpace(FileNameBox.Text))
{
InfoNeededMessage += "\n[Output File Name]";
FileNameBox.BorderBrush = Brushes.Red;
infoNeeded = true;
}
if(string.IsNullOrWhiteSpace(compareFolder))
{
InfoNeededMessage += "\n[COMPARE Folder]";
CompareLabel.BorderBrush = Brushes.Red;
infoNeeded = true;
}
if(string.IsNullOrWhiteSpace(targetFolder))
{
InfoNeededMessage += "\n[TARGET Folder]";
TargetLabel.BorderBrush = Brushes.Red;
infoNeeded = true;
}
if (infoNeeded)
{
MessageBox.Show(InfoNeededMessage, "Info Required", MessageBoxButton.OK, MessageBoxImage.Warning);
GenButton.IsEnabled = true;
CompareLabel.IsEnabled = true;
TargetLabel.IsEnabled = true;
FileNameBox.IsEnabled = true;
AutoZip_checkBox.IsEnabled = true;
return;
}
void SetEndingInfo(string info)
{
GenButton.DispatcherSetEnabled(true);
CompareLabel.DispatcherSetEnabled(true);
TargetLabel.DispatcherSetEnabled(true);
FileNameBox.DispatcherSetEnabled(true);
AutoZip_checkBox.DispatcherSetEnabled(true);
GenProgressMessageLabel.DispaatcherSetContent("");
GenProgressInfoLabel.DispaatcherSetContent(info);
}
2021-08-01 00:36:37 -04:00
Task.Run(() =>
{
stopwatch.Reset();
stopwatch.Start();
try
{
GeneratePatches(Path.Combine(outputFolderName.FromCwd(), LazyOperations.PatchFolder));
stopwatch.Stop();
SetEndingInfo($"Patches Generated in: {GetStopWatchTime()}");
2021-08-01 00:36:37 -04:00
}
catch(Exception ex)
2021-08-01 00:36:37 -04:00
{
stopwatch.Stop();
SetEndingInfo(ex.Message);
2021-08-01 00:36:37 -04:00
}
});
}
private void FileNameBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
{
FileNameBox.BorderBrush = Brushes.Gainsboro;
if (outputFolderName == FileNameBox.Text) return;
outputFolderName = Regex.Replace(FileNameBox.Text, "[^A-Za-z0-9.\\-_]", "");
FileNameBox.Text = outputFolderName;
}
2021-08-01 00:36:37 -04:00
}
}