1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 07:42:57 +08:00

Show a notification if game is run as administrator

This commit is contained in:
Christine Chen 2021-04-26 16:41:26 -04:00
parent bec16436b0
commit 9ad30da729
4 changed files with 72 additions and 0 deletions

View File

@ -7,6 +7,7 @@ using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Versioning;
using System.Security.Principal;
using System.Threading.Tasks;
using Microsoft.Win32;
using osu.Desktop.Overlays;
@ -20,6 +21,7 @@ using osu.Game.Screens.Menu;
using osu.Game.Updater;
using osu.Desktop.Windows;
using osu.Framework.Threading;
using osu.Game.Admin;
using osu.Game.IO;
namespace osu.Desktop
@ -102,6 +104,8 @@ namespace osu.Desktop
}
}
protected override AdminChecker CreateAdminChecker() => new DesktopAdminChecker();
protected override void LoadComplete()
{
base.LoadComplete();
@ -180,5 +184,10 @@ namespace osu.Desktop
Task.Factory.StartNew(() => Import(paths), TaskCreationOptions.LongRunning);
}
}
private class DesktopAdminChecker : AdminChecker
{
protected override bool IsAdmin() => OperatingSystem.IsWindows() ? new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator) : Mono.Unix.Native.Syscall.geteuid() == 0;
}
}
}

View File

@ -25,6 +25,7 @@
</ItemGroup>
<ItemGroup Label="Package References">
<PackageReference Include="Microsoft.NETCore.Targets" Version="5.0.0" />
<PackageReference Include="Mono.Posix.NETStandard" Version="5.20.1-preview" />
<PackageReference Include="System.IO.Packaging" Version="5.0.0" />
<PackageReference Include="ppy.squirrel.windows" Version="1.9.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.2.6" />

View File

@ -0,0 +1,57 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using osu.Framework;
using osu.Framework.Allocation;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Game.Graphics;
using osu.Game.Overlays;
using osu.Game.Overlays.Notifications;
namespace osu.Game.Admin
{
/// <summary>
/// Checks if the game is running with elevated privileges (as admin in Windows, root in Unix) and displays a warning notification if so.
/// </summary>
public class AdminChecker : CompositeDrawable
{
[Resolved]
protected NotificationOverlay Notifications { get; private set; }
protected override void LoadComplete()
{
base.LoadComplete();
if (IsAdmin())
Notifications.Post(new AdminNotification());
}
protected virtual bool IsAdmin() => false;
private class AdminNotification : SimpleNotification
{
public override bool IsImportant => true;
public AdminNotification()
{
bool isUnix = RuntimeInfo.IsUnix;
Text = $"Running osu! as {(isUnix ? "root" : "administrator")} does not improve performance and poses a security risk. Please run the game normally.";
}
[BackgroundDependencyLoader]
private void load(OsuColour colours, NotificationOverlay notificationOverlay)
{
Icon = FontAwesome.Solid.ShieldAlt;
IconBackgound.Colour = colours.YellowDark;
Activated = delegate
{
notificationOverlay.Hide();
return true;
};
}
}
}
}

View File

@ -28,6 +28,7 @@ using osu.Framework.Graphics.Sprites;
using osu.Framework.Input;
using osu.Framework.Input.Bindings;
using osu.Framework.Threading;
using osu.Game.Admin;
using osu.Game.Beatmaps;
using osu.Game.Collections;
using osu.Game.Graphics;
@ -450,6 +451,8 @@ namespace osu.Game
protected virtual UpdateManager CreateUpdateManager() => new UpdateManager();
protected virtual AdminChecker CreateAdminChecker() => new AdminChecker();
protected override Container CreateScalingContainer() => new ScalingContainer(ScalingMode.Everything);
#region Beatmap progression
@ -673,6 +676,8 @@ namespace osu.Game
// dependency on notification overlay, dependent by settings overlay
loadComponentSingleFile(CreateUpdateManager(), Add, true);
loadComponentSingleFile(CreateAdminChecker(), Add, false);
// overlay elements
loadComponentSingleFile(new ManageCollectionsDialog(), overlayContent.Add, true);
loadComponentSingleFile(beatmapListing = new BeatmapListingOverlay(), overlayContent.Add, true);