diff --git a/src/Lumper.UI/App.axaml.cs b/src/Lumper.UI/App.axaml.cs index 22bf68a3..e007fa9d 100644 --- a/src/Lumper.UI/App.axaml.cs +++ b/src/Lumper.UI/App.axaml.cs @@ -1,27 +1,34 @@ -using Avalonia; -using Avalonia.Controls.ApplicationLifetimes; -using Avalonia.Markup.Xaml; -using Lumper.UI.ViewModels; -using Lumper.UI.Views; - -namespace Lumper.UI; - -public class App : Application -{ - public override void Initialize() - { - AvaloniaXamlLoader.Load(this); - } - - public override void OnFrameworkInitializationCompleted() - { - if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime - desktop) - desktop.MainWindow = new MainWindow - { - DataContext = new MainWindowViewModel() - }; - - base.OnFrameworkInitializationCompleted(); - } -} +using Avalonia; +using Avalonia.Controls.ApplicationLifetimes; +using Avalonia.Markup.Xaml; +using Lumper.UI.ViewModels; +using Lumper.UI.Views; + +namespace Lumper.UI; + +public class App : Application +{ + public override void Initialize() + { + AvaloniaXamlLoader.Load(this); + } + + public override void OnFrameworkInitializationCompleted() + { + if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime + desktop) + { + desktop.MainWindow = new MainWindow + { + DataContext = new MainWindowViewModel() + }; + + if (desktop.Args?.Length > 0) + { + ((MainWindowViewModel)desktop.MainWindow.DataContext!).LoadBsp(desktop.Args[0]); + } + } + + base.OnFrameworkInitializationCompleted(); + } +} diff --git a/src/Lumper.UI/Views/MainWindow.axaml.cs b/src/Lumper.UI/Views/MainWindow.axaml.cs index f299b21d..db2cc3a7 100644 --- a/src/Lumper.UI/Views/MainWindow.axaml.cs +++ b/src/Lumper.UI/Views/MainWindow.axaml.cs @@ -1,51 +1,74 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using Avalonia.Controls; -using Avalonia.Input; -using Lumper.UI.ViewModels; - -namespace Lumper.UI.Views; - -public partial class MainWindow : Window -{ - public MainWindow() - { - InitializeComponent(); - - AddHandler(DragDrop.DragOverEvent, DragOver); - AddHandler(DragDrop.DropEvent, Drop); - } - - private async void Window_OnClosing(object? sender, CancelEventArgs e) - { - if (DataContext is not MainWindowViewModel model) - throw new ArgumentOutOfRangeException(); - await model.OnClose(e); - } - - private void DragOver(object? sender, DragEventArgs e) - { - e.DragEffects = DragDropEffects.Link; - - var names = e.Data.GetFileNames() ?? new List(); - - if (!e.Data.Contains(DataFormats.FileNames) || !names.FirstOrDefault("").ToLower().EndsWith(".bsp")) - e.DragEffects = DragDropEffects.None; - } - - private void Drop(object? sender, DragEventArgs e) - { - if (e.Data.Contains(DataFormats.FileNames)) - { - var names = e.Data.GetFileNames() ?? new List(); - var target = names.FirstOrDefault(""); - - if (!target.ToLower().EndsWith(".bsp")) - return; - - ((MainWindowViewModel)DataContext!).LoadBsp(target); - } - } -} +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Diagnostics; +using System.Linq; +using Avalonia; +using Avalonia.Controls; +using Avalonia.Input; +using Lumper.UI.ViewModels; + +namespace Lumper.UI.Views; + +public partial class MainWindow : Window +{ + public MainWindow() + { + InitializeComponent(); + + AddHandler(DragDrop.DragOverEvent, DragOver); + AddHandler(DragDrop.DropEvent, Drop); + } + + private async void Window_OnClosing(object? sender, CancelEventArgs e) + { + if (DataContext is not MainWindowViewModel model) + throw new ArgumentOutOfRangeException(); + await model.OnClose(e); + } + + private void DragOver(object? sender, DragEventArgs e) + { + e.DragEffects = DragDropEffects.Link; + + var names = e.Data.GetFileNames() ?? new List(); + + if (!e.Data.Contains(DataFormats.FileNames) || !names.FirstOrDefault("").ToLower().EndsWith(".bsp")) + e.DragEffects = DragDropEffects.None; + } + + private void Drop(object? sender, DragEventArgs e) + { + if (e.Data.Contains(DataFormats.FileNames)) + { + var names = e.Data.GetFileNames() ?? new List(); + foreach (string target in names) + { + if (!target.ToLower().EndsWith(".bsp")) + continue; + + if (((MainWindowViewModel)DataContext!).BspModel == null) + { + // if nothing is open, open it. + ((MainWindowViewModel)DataContext!).LoadBsp(target); + } + else + { + // Otherwise, open a brand new Lumper instance with it. + string? mainModuleFileName = Process.GetCurrentProcess().MainModule?.FileName; + if (mainModuleFileName == null) + return; + + string? executablePath = mainModuleFileName; + ProcessStartInfo startInfo = new ProcessStartInfo() + { + ArgumentList = { $"\"{target}\"" }, + FileName = executablePath, + }; + + Process.Start(startInfo); + } + } + } + } +}