Skip to content

Commit 9c87368

Browse files
committed
implement error dialog when dumper is ran by admin
1 parent 68fed73 commit 9c87368

File tree

10 files changed

+183
-34
lines changed

10 files changed

+183
-34
lines changed

Ps3DiscDumper/Dumper.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public class Dumper: IDisposable
2828
{
2929
public const string Version = "4.0.0-beta1";
3030

31+
static Dumper() => Log.Info("PS3 Disc Dumper v" + Dumper.Version);
32+
3133
private static readonly Regex VersionParts = new(@"(?<ver>\d+(\.\d+){0,2})[ \-]*(?<pre>.*)", RegexOptions.Singleline | RegexOptions.ExplicitCapture);
3234
private static readonly Regex ScsiInfoParts = new(@"Host: .+$\s*Vendor: (?<vendor>.+?)\s* Model: (?<model>.+?)\s* Rev: (?<revision>.+)$\s*Type: \s*(?<type>.+?)\s* ANSI ?SCSI revision: (?<scsi_rev>.+?)\s*$",
3335
RegexOptions.Multiline | RegexOptions.ExplicitCapture);

Tests/Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
1111
<PackageReference Include="nunit" Version="3.13.3" />
1212
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0">
1313
<PrivateAssets>all</PrivateAssets>

UI.Avalonia/App.axaml.cs

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Avalonia.Platform;
88
using Avalonia.Styling;
99
using Ps3DiscDumper;
10+
using Ps3DiscDumper.Utils;
1011
using UI.Avalonia.Utils.ColorPalette;
1112
using UI.Avalonia.ViewModels;
1213
using UI.Avalonia.Views;
@@ -33,26 +34,33 @@ public override void OnFrameworkInitializationCompleted()
3334
{
3435
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
3536
{
36-
var vm = new MainWindowViewModel();
37-
38-
var systemFonts = FontManager.Current.SystemFonts;
39-
if (systemFonts.TryGetGlyphTypeface("Segoe Fluent Icons", FontStyle.Normal, FontWeight.Normal, FontStretch.Normal, out _))
40-
vm.CurrentPage.SymbolFontFamily = new("Segoe Fluent Icons");
41-
if (systemFonts.TryGetGlyphTypeface("Segoe UI Variable Small", FontStyle.Normal, FontWeight.Normal, FontStretch.Normal, out _))
42-
vm.CurrentPage.SmallFontFamily = new("Segoe UI Variable Small");
43-
if (systemFonts.TryGetGlyphTypeface("Segoe UI Variable Display", FontStyle.Normal, FontWeight.Normal, FontStretch.Normal, out _))
44-
vm.CurrentPage.LargeFontFamily = new("Segoe UI Variable Display");
45-
46-
var w = new MainWindow { DataContext = vm, };
37+
var safeToRun = SecurityEx.IsSafe(desktop.Args);
38+
Window w;
39+
ViewModelBase vm;
40+
if (safeToRun)
41+
{
42+
var mainViewModel = new MainWindowViewModel();
43+
vm = mainViewModel.CurrentPage;
44+
SetSymbolFont(vm);
45+
w = new MainWindow { DataContext = mainViewModel };
46+
}
47+
else
48+
{
49+
vm = new ErrorStubViewModel();
50+
SetSymbolFont(vm);
51+
w = new ErrorStub { DataContext = vm };
52+
}
4753
desktop.MainWindow = w;
4854
desktop.MainWindow.Activated += OnActivated;
4955
desktop.MainWindow.Deactivated += OnDeactivated;
5056
desktop.MainWindow.ActualThemeVariantChanged += OnThemeChanged;
5157
if (w.PlatformSettings is { } ps)
5258
ps.ColorValuesChanged += OnPlatformColorsChanged;
5359

54-
vm.CurrentPage.MicaEnabled = isMicaCapable.Value;
55-
vm.CurrentPage.AcrylicEnabled = w.ActualTransparencyLevel == WindowTransparencyLevel.AcrylicBlur;
60+
vm.MicaEnabled = isMicaCapable.Value;
61+
vm.AcrylicEnabled = w.ActualTransparencyLevel == WindowTransparencyLevel.AcrylicBlur;
62+
63+
var systemFonts = FontManager.Current.SystemFonts;
5664
if (systemFonts.TryGetGlyphTypeface("Segoe UI Variable Text", FontStyle.Normal, FontWeight.Normal, FontStretch.Normal, out _))
5765
w.FontFamily = new("Segoe UI Variable Text");
5866
else if (systemFonts.TryGetGlyphTypeface("Segoe UI", FontStyle.Normal, FontWeight.Normal, FontStretch.Normal, out _))
@@ -61,6 +69,17 @@ public override void OnFrameworkInitializationCompleted()
6169
base.OnFrameworkInitializationCompleted();
6270
}
6371

72+
private static void SetSymbolFont(ViewModelBase vm)
73+
{
74+
var systemFonts = FontManager.Current.SystemFonts;
75+
if (systemFonts.TryGetGlyphTypeface("Segoe Fluent Icons", FontStyle.Normal, FontWeight.Normal, FontStretch.Normal, out _))
76+
vm.SymbolFontFamily = new("Segoe Fluent Icons");
77+
if (systemFonts.TryGetGlyphTypeface("Segoe UI Variable Small", FontStyle.Normal, FontWeight.Normal, FontStretch.Normal, out _))
78+
vm.SmallFontFamily = new("Segoe UI Variable Small");
79+
if (systemFonts.TryGetGlyphTypeface("Segoe UI Variable Display", FontStyle.Normal, FontWeight.Normal, FontStretch.Normal, out _))
80+
vm.LargeFontFamily = new("Segoe UI Variable Display");
81+
}
82+
6483
private void OnActivated(object? sender, EventArgs e)
6584
{
6685
if (sender is not Window w)
@@ -85,10 +104,16 @@ private void OnDeactivated(object? sender, EventArgs e)
85104

86105
internal static void OnThemeChanged(object? sender, EventArgs e)
87106
{
88-
if (sender is not Window { DataContext: MainWindowViewModel {CurrentPage: ViewModelBase vm} } window)
107+
Window w;
108+
ViewModelBase vm;
109+
if (sender is Window { DataContext: MainWindowViewModel { CurrentPage: {} vm1 } } w1)
110+
(w, vm) = (w1, vm1);
111+
else if (sender is Window { DataContext: ViewModelBase vm2 } w2)
112+
(w, vm) = (w2, vm2);
113+
else
89114
return;
90115

91-
if (window.ActualThemeVariant == ThemeVariant.Light)
116+
if (w.ActualThemeVariant == ThemeVariant.Light)
92117
{
93118
vm.TintColor = ThemeConsts.LightThemeTintColor;
94119
vm.TintOpacity = ThemeConsts.LightThemeTintOpacity;
@@ -97,7 +122,7 @@ internal static void OnThemeChanged(object? sender, EventArgs e)
97122
vm.Layer2GroundedColor = ThemeConsts.LightThemeLayer2Grounded;
98123
vm.ColorPalette = ThemeConsts.Light;
99124
}
100-
else if (window.ActualThemeVariant == ThemeVariant.Dark)
125+
else if (w.ActualThemeVariant == ThemeVariant.Dark)
101126
{
102127
vm.TintColor = ThemeConsts.DarkThemeTintColor;
103128
vm.TintOpacity = ThemeConsts.DarkThemeTintOpacity;
@@ -107,8 +132,8 @@ internal static void OnThemeChanged(object? sender, EventArgs e)
107132
vm.ColorPalette = ThemeConsts.Dark;
108133
}
109134
}
110-
111-
135+
136+
112137
internal static void OnPlatformColorsChanged(object? sender, PlatformColorValues e)
113138
{
114139
if (Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace UI.Avalonia.ViewModels;
2+
3+
public partial class ErrorStubViewModel: ViewModelBase
4+
{
5+
}

UI.Avalonia/ViewModels/MainViewModel.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,7 @@ public partial class MainViewModel : ViewModelBase, IDisposable
1818

1919
public MainViewModel(): this(new()){}
2020

21-
public MainViewModel(SettingsViewModel settings)
22-
{
23-
this.settings = settings;
24-
pageTitle = "PS3 Disc Dumper v" + Dumper.Version;
25-
Log.Info(pageTitle);
26-
}
21+
public MainViewModel(SettingsViewModel settings) => this.settings = settings;
2722

2823
[ObservableProperty] private string stepTitle = "Please insert a PS3 game disc";
2924
[ObservableProperty] private string stepSubtitle = "";

UI.Avalonia/ViewModels/ViewModelBase.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
using System;
22
using System.Diagnostics;
3-
using Avalonia;
4-
using Avalonia.Controls;
5-
using Avalonia.Controls.ApplicationLifetimes;
63
using Avalonia.Media;
74
using CommunityToolkit.Mvvm.ComponentModel;
85
using CommunityToolkit.Mvvm.Input;
@@ -48,8 +45,8 @@ public partial class ViewModelBase: ObservableObject
4845
public string PinSymbol => UseSegoeIcons ? "\ue718" : "\uf08d";
4946
public string ValidationErrorSymbol => UseSegoeIcons ? "\ue783" : "\uf06a";
5047
public string ValidationWarningSymbol => UseSegoeIcons ? "\ue7ba" : "\uf071";
51-
52-
[ObservableProperty] protected string pageTitle = "PS3 Disc Dumper";
48+
49+
[ObservableProperty] protected string pageTitle = "PS3 Disc Dumper v" + Dumper.Version;
5350
[ObservableProperty] private bool canEditSettings = true;
5451

5552
[RelayCommand]

UI.Avalonia/Views/ErrorStub.axaml

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<Window xmlns="https://github.com/avaloniaui"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
4+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5+
xmlns:vm="clr-namespace:UI.Avalonia.ViewModels"
6+
x:Class="UI.Avalonia.Views.ErrorStub"
7+
x:DataType="vm:ErrorStubViewModel"
8+
mc:Ignorable="d" d:DesignWidth="600" d:DesignHeight="400"
9+
Width="600" Height="400"
10+
11+
WindowStartupLocation="CenterScreen"
12+
CanResize="False"
13+
ExtendClientAreaToDecorationsHint="True"
14+
ExtendClientAreaChromeHints="PreferSystemChrome"
15+
SystemDecorations="Full"
16+
Topmost="{Binding StayOnTop}"
17+
18+
Background="Transparent"
19+
TransparencyLevelHint="Mica, AcrylicBlur, None"
20+
RenderOptions.TextRenderingMode="Antialias"
21+
22+
Icon="/Assets/icon.ico"
23+
Title="{Binding PageTitle}"
24+
>
25+
26+
<Design.DataContext>
27+
<!-- This only sets the DataContext for the previewer in an IDE,
28+
to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) -->
29+
<vm:ErrorStubViewModel/>
30+
</Design.DataContext>
31+
32+
<Panel>
33+
<Border IsHitTestVisible="False" Background="{Binding TintColor}">
34+
<Border.IsVisible>
35+
<MultiBinding Converter="{x:Static BoolConverters.Or}">
36+
<Binding Path="!EnableTransparency"/>
37+
<Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=Window}" Path="!IsActive"/>
38+
<MultiBinding Converter="{x:Static BoolConverters.And}">
39+
<Binding Path="!MicaEnabled"/>
40+
<Binding Path="!AcrylicEnabled"/>
41+
</MultiBinding>
42+
</MultiBinding>
43+
</Border.IsVisible>
44+
</Border>
45+
<ExperimentalAcrylicBorder IsHitTestVisible="False">
46+
<ExperimentalAcrylicBorder.IsVisible>
47+
<MultiBinding Converter="{x:Static BoolConverters.And}">
48+
<Binding Path="EnableTransparency"/>
49+
<Binding Path="!MicaEnabled"/>
50+
<Binding Path="AcrylicEnabled"/>
51+
</MultiBinding>
52+
</ExperimentalAcrylicBorder.IsVisible>
53+
<ExperimentalAcrylicBorder.Material>
54+
<ExperimentalAcrylicMaterial
55+
BackgroundSource="Digger"
56+
TintColor="{Binding TintColor, Converter={StaticResource ColorConverter}}"
57+
TintOpacity="{Binding TintOpacity}"
58+
MaterialOpacity="0.5"
59+
/>
60+
</ExperimentalAcrylicBorder.Material>
61+
</ExperimentalAcrylicBorder>
62+
63+
<!-- title -->
64+
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" Margin="11 11 0 0"
65+
FontSize="12"
66+
IsVisible="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}, Path=IsExtendedIntoWindowDecorations}"
67+
Text="{Binding PageTitle}"/>
68+
69+
<!-- message -->
70+
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center"
71+
Margin="16 64 16 16">
72+
<StackPanel Orientation="Horizontal" MaxWidth="568">
73+
<TextBlock FontFamily="{Binding SymbolFontFamily}" FontSize="36"
74+
Margin="0 0 16 0" Padding="0 4 0 0"
75+
Foreground="{Binding ColorPalette.StatusDangerForeground1, Converter={StaticResource BrushConverter}}"
76+
Text="{Binding ValidationErrorSymbol}"/>
77+
<TextBlock FontSize="28" TextWrapping="Wrap" ClipToBounds="True" MaxWidth="500">
78+
Please do not run software as Administrator unless application was designed to properly handle it, and it is explicitly required.
79+
</TextBlock>
80+
</StackPanel>
81+
82+
<Button Margin="0 44 0 0" Padding="32 8" FontSize="24"
83+
HorizontalAlignment="Center"
84+
HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
85+
Click="Exit">
86+
Exit
87+
</Button>
88+
</StackPanel>
89+
90+
</Panel>
91+
92+
</Window>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using Avalonia;
3+
using Avalonia.Controls;
4+
using Avalonia.Interactivity;
5+
using Avalonia.Markup.Xaml;
6+
using Avalonia.Media;
7+
using Avalonia.Platform;
8+
using UI.Avalonia.Utils.ColorPalette;
9+
10+
namespace UI.Avalonia.Views;
11+
12+
public partial class ErrorStub : Window
13+
{
14+
public ErrorStub()
15+
{
16+
InitializeComponent();
17+
}
18+
19+
public override void Show()
20+
{
21+
22+
base.Show();
23+
App.OnThemeChanged(this, EventArgs.Empty);
24+
App.OnPlatformColorsChanged(this, PlatformSettings?.GetColorValues() ?? new PlatformColorValues
25+
{
26+
AccentColor1 = Color.Parse(ThemeConsts.AccentColor),
27+
AccentColor2 = Color.Parse(ThemeConsts.AccentColor),
28+
AccentColor3 = Color.Parse(ThemeConsts.AccentColor),
29+
});
30+
}
31+
32+
private void Exit(object? sender, RoutedEventArgs e) => Close();
33+
}

UI.Avalonia/Views/MainWindow.axaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
55
xmlns:vm="using:UI.Avalonia.ViewModels"
6-
mc:Ignorable="d" d:DesignWidth="600" d:DesignHeight="400"
7-
Width="600" Height="400"
86
x:Class="UI.Avalonia.Views.MainWindow"
97
x:DataType="vm:MainWindowViewModel"
8+
mc:Ignorable="d" d:DesignWidth="600" d:DesignHeight="400"
9+
Width="600" Height="400"
1010

1111
WindowStartupLocation="CenterScreen"
1212
CanResize="False"
@@ -77,7 +77,7 @@
7777
</Button>
7878
<!-- window title -->
7979
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" Margin="11 11 0 0"
80-
FontFamily="{Binding CurrentPage.SmallFontFamily}" FontSize="12"
80+
FontSize="12"
8181
IsVisible="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}, Path=IsExtendedIntoWindowDecorations}"
8282
Text="{Binding CurrentPage.PageTitle}"/>
8383
</StackPanel>

UI.Avalonia/Views/MainWindow.axaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public override void Show()
2929

3030
base.Show();
3131
App.OnThemeChanged(this, EventArgs.Empty);
32-
App.OnPlatformColorsChanged(this, PlatformSettings?.GetColorValues() ?? new PlatformColorValues()
32+
App.OnPlatformColorsChanged(this, PlatformSettings?.GetColorValues() ?? new PlatformColorValues
3333
{
3434
AccentColor1 = Color.Parse(ThemeConsts.AccentColor),
3535
AccentColor2 = Color.Parse(ThemeConsts.AccentColor),

0 commit comments

Comments
 (0)