Skip to content

Commit 5ae50fc

Browse files
committed
Do not use Tmds.DBus in trimmed apps
1 parent 8fa7895 commit 5ae50fc

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

src/Common/Guards.cs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright Bastian Eicher
2+
// Licensed under the MIT License
3+
4+
namespace NanoByte.Common;
5+
6+
/// <summary>
7+
/// Guards that can be statically evaluated by a linker.
8+
/// </summary>
9+
internal class Guards
10+
{
11+
#if NET9_0_OR_GREATER
12+
// This feature is disabled by default in trimmed apps:
13+
// https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/trimming-options#framework-features-disabled-when-trimming
14+
private const string NotTrimmedSwitch = "System.Resources.ResourceManager.AllowCustomResourceTypes";
15+
16+
/// <summary>
17+
/// The current app is likely to be not trimmed and therefore supports full reflection.
18+
/// </summary>
19+
[FeatureGuard(typeof(RequiresUnreferencedCodeAttribute))]
20+
[FeatureSwitchDefinition(NotTrimmedSwitch)]
21+
public static bool NotTrimmed { get; } = AppContext.TryGetSwitch(NotTrimmedSwitch, out bool value) ? value : true;
22+
#else
23+
/// <summary>
24+
/// Always returns true.
25+
/// </summary>
26+
public static bool NotTrimmed => true;
27+
#endif
28+
}

src/Common/Native/OSUtils.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public static string ExpandVariables(string value, StringDictionary variables)
107107
if (WindowsUtils.IsWindowsNT) return KeepAwakeWindows(NativeMethods.ExecutionState.SystemRequired);
108108

109109
#if NET
110-
if (UnixUtils.IsLinux)
110+
if (UnixUtils.IsLinux && Guards.NotTrimmed)
111111
{
112112
var login = Connection.System.CreateProxy<IManager>("org.freedesktop.login1", "/org/freedesktop/login1");
113113
return login.InhibitAsync("idle", AppInfo.Current.Name ?? "App", reason, "block").Result;
@@ -137,7 +137,7 @@ public static string ExpandVariables(string value, StringDictionary variables)
137137
if (WindowsUtils.IsWindowsNT) return KeepAwakeWindows(NativeMethods.ExecutionState.DisplayRequired);
138138

139139
#if NET
140-
if (UnixUtils.IsLinux)
140+
if (UnixUtils.IsLinux && Guards.NotTrimmed)
141141
{
142142
var screenSaver = Connection.Session.CreateProxy<IScreenSaver>("org.freedesktop.ScreenSaver", "/org/freedesktop/ScreenSaver");
143143
uint cookie = screenSaver.InhibitAsync(AppInfo.Current.Name ?? "App", reason).Result;

src/Common/Net/NetUtils.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public static Connectivity GetInternetConnectivity()
145145
return SafeNativeMethods.InternetGetConnectedState(out _, 0) ? Connectivity.Normal : Connectivity.None;
146146

147147
#if NET
148-
if (UnixUtils.IsLinux)
148+
if (UnixUtils.IsLinux && Guards.NotTrimmed)
149149
{
150150
var networkManager = Connection.System.CreateProxy<INetworkManager>("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager");
151151
if (networkManager.GetAsync<uint>("Connectivity").Result is INetworkManager.ConnectivityNone or INetworkManager.ConnectivityPortal)

0 commit comments

Comments
 (0)