File tree Expand file tree Collapse file tree 3 files changed +54
-6
lines changed Expand file tree Collapse file tree 3 files changed +54
-6
lines changed Original file line number Diff line number Diff line change 7
7
using System . Diagnostics ;
8
8
using System . IO ;
9
9
using System . Runtime . InteropServices ;
10
+ using System . Security . Principal ;
10
11
using System . Threading . Tasks ;
11
12
12
13
namespace OpenLoco . Gui
13
14
{
15
+ // For Unix-like systems (Linux, macOS)
16
+ static class UnixUserChecker
17
+ {
18
+ // Imports the geteuid() function from libc (the standard C library)
19
+ // geteuid() returns the effective user ID of the calling process.
20
+ // A value of 0 typically indicates the root user.
21
+ [ DllImport ( "libc" , EntryPoint = "geteuid" ) ]
22
+ internal static extern uint geteuid ( ) ;
23
+
24
+ public static bool IsRoot ( )
25
+ => geteuid ( ) == 0 ;
26
+ }
27
+
14
28
public static class PlatformSpecific
15
29
{
30
+ public static bool RunningAsAdmin ( )
31
+ {
32
+ if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
33
+ {
34
+ var identity = WindowsIdentity . GetCurrent ( ) ;
35
+ var principal = new WindowsPrincipal ( identity ) ;
36
+ return principal . IsInRole ( WindowsBuiltInRole . Administrator ) ;
37
+ }
38
+ else if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) || RuntimeInformation . IsOSPlatform ( OSPlatform . OSX ) )
39
+ {
40
+ return UnixUserChecker . IsRoot ( ) ;
41
+ }
42
+
43
+ return false ;
44
+ }
45
+
16
46
public static void FolderOpenInDesktop ( string directory , ILogger logger )
17
47
{
18
48
try
Original file line number Diff line number Diff line change @@ -11,8 +11,26 @@ class Program
11
11
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
12
12
// yet and stuff might break.
13
13
[ STAThread ]
14
- public static void Main ( string [ ] args ) => BuildAvaloniaApp ( )
15
- . StartWithClassicDesktopLifetime ( args ) ;
14
+ public static void Main ( string [ ] args )
15
+ {
16
+ PreventRunningAsAdmin ( ) ;
17
+ _ = BuildAvaloniaApp ( )
18
+ . StartWithClassicDesktopLifetime ( args ) ;
19
+ }
20
+
21
+ static void PreventRunningAsAdmin ( )
22
+ {
23
+ if ( PlatformSpecific . RunningAsAdmin ( ) )
24
+ {
25
+ const string errorMessage = "This application should not be run with elevated privileges. Please run it as a regular user." ;
26
+
27
+ // show user a message. must be OS-independent
28
+ Console . Error . WriteLine ( errorMessage ) ;
29
+
30
+ // terminate current program
31
+ throw new UnauthorizedAccessException ( errorMessage ) ;
32
+ }
33
+ }
16
34
17
35
// Avalonia configuration, don't remove; also used by visual designer.
18
36
public static AppBuilder BuildAvaloniaApp ( )
Original file line number Diff line number Diff line change 1
1
<?xml version =" 1.0" encoding =" utf-8" ?>
2
2
<assembly manifestVersion =" 1.0" xmlns =" urn:schemas-microsoft-com:asm.v1" >
3
3
<!-- This manifest is used on Windows only.
4
- Don't remove it as it might cause problems with window transparency and embeded controls.
5
- For more details visit https://learn.microsoft.com/en-us/windows/win32/sbscs/application-manifests -->
4
+ Don't remove it as it might cause problems with window transparency and embedded controls.
5
+ For more details visit https://learn.microsoft.com/en-us/windows/win32/sbscs/application-manifests -->
6
6
<assemblyIdentity version =" 1.0.0.0" name =" OpenLoco.Gui.Desktop" />
7
7
8
8
<compatibility xmlns =" urn:schemas-microsoft-com:compatibility.v1" >
9
9
<application >
10
10
<!-- A list of the Windows versions that this application has been tested on
11
- and is designed to work with. Uncomment the appropriate elements
12
- and Windows will automatically select the most compatible environment. -->
11
+ and is designed to work with. Uncomment the appropriate elements
12
+ and Windows will automatically select the most compatible environment. -->
13
13
14
14
<!-- Windows 10 -->
15
15
<supportedOS Id =" {8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
You can’t perform that action at this time.
0 commit comments