Fix the launcher so it no longer dies when run from strange folders

This commit is contained in:
Timothy Baldridge 2022-06-24 16:16:40 -06:00
parent bbe75e5061
commit 1408926de9
5 changed files with 40 additions and 16 deletions

View File

@ -17,9 +17,4 @@
<ProjectReference Include="..\Wabbajack.Paths\Wabbajack.Paths.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Xunit.DependencyInjection" Version="8.5.0" />
<PackageReference Include="Xunit.DependencyInjection.Logging" Version="8.0.1" />
</ItemGroup>
</Project>

View File

@ -9,6 +9,7 @@ using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
using MessageBox.Avalonia.DTO;
using ReactiveUI.Fody.Helpers;
using Wabbajack.Compression.Zip;
using Wabbajack.Downloaders.Http;
@ -172,14 +173,38 @@ public class MainWindowViewModel : ViewModelBase
private async Task VerifyCurrentLocation()
{
var entryPoint = KnownFolders.EntryPoint;
if (entryPoint.FileName == "Desktop".ToRelativePath()
|| entryPoint.Depth <= 1
|| entryPoint.FileName == "Downloads".ToRelativePath())
try
{
var entryPoint = KnownFolders.EntryPoint;
if (entryPoint.FileName == "Desktop".ToRelativePath()
|| entryPoint.Depth <= 1
|| entryPoint.FileName == "Downloads".ToRelativePath())
{
var msg = MessageBox.Avalonia.MessageBoxManager
.GetMessageBoxStandardWindow(new MessageBoxStandardParams()
{
Topmost = true,
ShowInCenter = true,
ContentTitle = "Wabbajack Launcher: Bad startup path",
ContentMessage =
"Cannot start in the root, Downloads or Desktop folders.\nPlease move Wabbajack to another folder."
});
var result = await msg.Show();
Environment.Exit(1);
}
}
catch (Exception ex)
{
Status = ex.Message;
var msg = MessageBox.Avalonia.MessageBoxManager
.GetMessageBoxStandardWindow("Bad Download Path",
"Cannot start in the root, Downloads or Desktop folders.");
.GetMessageBoxStandardWindow(new MessageBoxStandardParams()
{
Topmost = true,
ShowInCenter = true,
ContentTitle = "Wabbajack Launcher: Error",
ContentMessage = ex.ToString()
});
var result = await msg.Show();
Environment.Exit(1);
}

View File

@ -11,6 +11,8 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.2-mauipre.1.22054.8" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="xunit" Version="2.4.2-pre.12" />
<PackageReference Include="Xunit.DependencyInjection" Version="8.5.0" />
<PackageReference Include="Xunit.DependencyInjection.Logging" Version="8.0.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>

View File

@ -11,13 +11,15 @@ public static class KnownFolders
{
get
{
var result = Process.GetCurrentProcess().MainModule!.FileName!.ToAbsolutePath().Parent;
if (result.FileName == "dotnet".ToRelativePath() || Assembly.GetEntryAssembly() != null)
var result = Process.GetCurrentProcess().MainModule?.FileName?.ToAbsolutePath() ?? default;
if ((result != default && result.Depth > 1 && result.FileName == "dotnet".ToRelativePath()) || Assembly.GetEntryAssembly() != null)
{
return Assembly.GetExecutingAssembly().Location.ToAbsolutePath().Parent;
result = Assembly.GetEntryAssembly()!.Location.ToAbsolutePath();
}
return result;
return result == default ? Environment.CurrentDirectory.ToAbsolutePath() : result.Parent;
}
}

View File

@ -20,7 +20,7 @@ public struct AbsolutePath : IPath, IComparable<AbsolutePath>, IEquatable<Absolu
internal readonly string[] Parts;
public Extension Extension => Extension.FromPath(Parts[^1]);
public RelativePath FileName => (RelativePath) Parts[^1];
public RelativePath FileName => new(Parts[^1..]);
internal AbsolutePath(string[] parts, PathFormat format)
{