Files
ExeLauncher/ExeLauncher.GUI/MainWindow.xaml.cs
2021-12-04 15:35:41 +01:00

172 lines
5.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
namespace ExeLauncher.GUI
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _appName = "Run";
public string ApplicationName
{
get => _appName;
set
{
if (_appName != value)
{
_appName = value;
NotifyPropertyChanged();
}
}
}
private string _applicationIconPath = "";
public string ApplicationIconPath
{
get => _applicationIconPath;
set
{
if (_applicationIconPath != value)
{
_applicationIconPath = value;
NotifyPropertyChanged();
}
}
}
public ObservableCollection<ApplicationModel> Applications { get; set; }
private readonly ExeGenerator _exeGenerator;
public MainWindow()
{
_exeGenerator = new ExeGenerator();
Applications = new ObservableCollection<ApplicationModel>()
{
new ApplicationModel()
};
InitializeComponent();
}
private void Button_Icon(object sender, RoutedEventArgs e)
{
ApplicationIconPath = Program.GetIcon();
}
private void Button_Add(object sender, RoutedEventArgs e)
{
Applications.Add(new ApplicationModel() { Number = Applications.Count + 1 });
}
private void Button_Remove(object sender, RoutedEventArgs e)
{
if (Applications.Count > 1)
{
Applications.RemoveAt(Applications.Count - 1);
}
}
private void Button_Generate(object sender, RoutedEventArgs e)
{
if (!CheckApplications())
{
MessageBox.Show("Path and working directory cannot be empty!", "Invalid path or working directory", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
Generate();
}
private void Generate()
{
var dialog = new SaveFileDialog()
{
AddExtension = true,
DefaultExt = ".exe",
FileName = $"{ApplicationName}.exe",
Filter = "Executable|*.exe",
OverwritePrompt = true
};
if (dialog.ShowDialog() == true)
{
var paths = string.Join("§", Applications.Select(app => app.Path));
var arguments = string.Join("§", Applications.Select(app => app.Arguments));
var workingDirectories = string.Join("§", Applications.Select(app => app.WorkingDirectory));
var errors = _exeGenerator.CreateExecutable(dialog.FileName, paths, arguments, workingDirectories, ApplicationIconPath);
if (errors != null)
{
var message = GenerateErrorMessage(errors);
MessageBox.Show(message, "Compilation error", MessageBoxButton.OK, MessageBoxImage.Error);
}
else
{
MessageBox.Show($"Successfully generated launcher at: {dialog.FileName}", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
}
private string GenerateErrorMessage(System.CodeDom.Compiler.CompilerErrorCollection errors)
{
var builder = new StringBuilder();
builder.AppendLine("An error occured while compiling the executable: ");
foreach (var compilerError in errors)
{
builder.AppendLine($"\t{compilerError}");
builder.AppendLine();
}
return builder.ToString();
}
private bool CheckApplications()
{
foreach (var app in Applications)
{
if (string.IsNullOrWhiteSpace(app.Path) || string.IsNullOrWhiteSpace(app.WorkingDirectory))
{
return false;
}
}
return true;
}
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}