Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nullable references, net8.0, blend registry alert dot, netkan fixes #4171

Merged
merged 11 commits into from
Sep 3, 2024
Merged
  •  
  •  
  •  
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,18 @@ dotnet_diagnostic.IDE0055.severity = none
# OK to call functions that return values and not use them
dotnet_diagnostic.IDE0058.severity = none

# I like seeing when a using block ends
dotnet_diagnostic.IDE0063.severity = none

# A `using` inside a namespace is useful as a typedef
dotnet_diagnostic.IDE0065.severity = none

# 'switch' expressions are nice, but I don't need to be alerted about them
dotnet_diagnostic.IDE0066.severity = none

# Why do 'new' expressions need to be simple?
dotnet_diagnostic.IDE0090.severity = none

# Allow namespaces to be independent of folder names
dotnet_diagnostic.IDE0130.severity = none

Expand Down
8 changes: 5 additions & 3 deletions AutoUpdate/CKAN-autoupdate.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
<Deterministic>true</Deterministic>
<Configurations>Debug;Release</Configurations>
<Prefer32Bit>false</Prefer32Bit>
<LangVersion>7</LangVersion>
<LangVersion>9</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<ApplicationIcon>..\assets\ckan.ico</ApplicationIcon>
<TargetFrameworks>net48;net7.0-windows</TargetFrameworks>
<UseWindowsForms Condition=" '$(TargetFramework)' == 'net7.0-windows' ">true</UseWindowsForms>
<TargetFrameworks>net48;net8.0-windows</TargetFrameworks>
<UseWindowsForms Condition=" '$(TargetFramework)' == 'net8.0-windows' ">true</UseWindowsForms>
<EnableWindowsTargeting>true</EnableWindowsTargeting>
<FileAlignment>512</FileAlignment>
<ErrorReport>prompt</ErrorReport>
Expand Down
31 changes: 26 additions & 5 deletions AutoUpdate/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
#if NET5_0_OR_GREATER
using System.Runtime.Versioning;
#endif

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* CKAN AUTO-UPDATE TOOL
Expand Down Expand Up @@ -145,7 +148,7 @@ private static void MakeExecutable(string path)
{
UseShellExecute = false
});
permsprocess.WaitForExit();
permsprocess?.WaitForExit();
}
}

Expand All @@ -158,8 +161,17 @@ private static bool IsOnMono
/// <summary>
/// Are we on Windows?
/// </summary>
private static bool IsOnWindows
=> RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
#if NET6_0_OR_GREATER
[SupportedOSPlatformGuard("windows")]
#endif
private static readonly bool IsOnWindows
= RuntimeInformation.IsOSPlatform(OSPlatform.Windows);

#if NET8_0_OR_GREATER
[SupportedOSPlatformGuard("windows6.1")]
private static readonly bool IsOnWindows61
= IsOnWindows && OperatingSystem.IsWindowsVersionAtLeast(6, 1);
#endif

/// <summary>
/// Display unexpected exceptions to user
Expand All @@ -179,11 +191,20 @@ private static void ReportError(string message, params object[] args)
{
string err = string.Format(message, args);
Console.Error.WriteLine(err);
if (fromGui)
#if NETFRAMEWORK || WINDOWS
if (
#if NET8_0_OR_GREATER
IsOnWindows61 &&
#endif
fromGui)
{
// Show a popup in case the console isn't open
MessageBox.Show(err, Properties.Resources.FatalErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show(err,
Properties.Resources.FatalErrorTitle,
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
#endif
}

private const int maxRetries = 8;
Expand Down
14 changes: 5 additions & 9 deletions AutoUpdate/SingleAssemblyResourceManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.IO;
using System.Globalization;
using System.Resources;
using System.Reflection;
Expand All @@ -14,16 +13,13 @@ public SingleAssemblyResourceManager(string basename, Assembly assembly) : base(
{
}

protected override ResourceSet InternalGetResourceSet(CultureInfo culture,
protected override ResourceSet? InternalGetResourceSet(CultureInfo culture,
bool createIfNotExists, bool tryParents)
{
if (!myResourceSets.TryGetValue(culture, out ResourceSet rs) && createIfNotExists)
if (!myResourceSets.TryGetValue(culture, out ResourceSet? rs) && createIfNotExists && MainAssembly != null)
{
// Lazy-load default language (without caring about duplicate assignment in race conditions, no harm done)
if (neutralResourcesCulture == null)
{
neutralResourcesCulture = GetNeutralResourcesLanguage(MainAssembly);
}
neutralResourcesCulture ??= GetNeutralResourcesLanguage(MainAssembly);

// If we're asking for the default language, then ask for the
// invariant (non-specific) resources.
Expand All @@ -33,7 +29,7 @@ protected override ResourceSet InternalGetResourceSet(CultureInfo culture,
}
string resourceFileName = GetResourceFileName(culture);

Stream store = MainAssembly.GetManifestResourceStream(resourceFileName);
var store = MainAssembly.GetManifestResourceStream(resourceFileName);

// If we found the appropriate resources in the local assembly
if (store != null)
Expand All @@ -50,7 +46,7 @@ protected override ResourceSet InternalGetResourceSet(CultureInfo culture,
return rs;
}

private CultureInfo neutralResourcesCulture;
private CultureInfo? neutralResourcesCulture;
private readonly Dictionary<CultureInfo, ResourceSet> myResourceSets = new Dictionary<CultureInfo, ResourceSet>();
}
}
62 changes: 33 additions & 29 deletions Cmdline/Action/AuthToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public AuthToken() { }
/// <returns>
/// Exit code
/// </returns>
public int RunSubCommand(GameInstanceManager manager, CommonOptions opts, SubCommandOptions unparsed)
public int RunSubCommand(GameInstanceManager? manager,
CommonOptions? opts,
SubCommandOptions unparsed)
{
string[] args = unparsed.options.ToArray();
int exitCode = Exit.OK;
Expand All @@ -37,12 +39,9 @@ public int RunSubCommand(GameInstanceManager manager, CommonOptions opts, SubCom
{
CommonOptions options = (CommonOptions)suboptions;
options.Merge(opts);
user = new ConsoleUser(options.Headless);
if (manager == null)
{
manager = new GameInstanceManager(user);
}
exitCode = options.Handle(manager, user);
user = new ConsoleUser(options.Headless);
manager ??= new GameInstanceManager(user);
exitCode = options.Handle(manager, user);
if (exitCode == Exit.OK)
{
switch (option)
Expand Down Expand Up @@ -75,24 +74,23 @@ private int listAuthTokens()
foreach (string host in hosts)
{
longestHostLen = Math.Max(longestHostLen, host.Length);
if (ServiceLocator.Container.Resolve<IConfiguration>().TryGetAuthToken(host, out string token))
if (ServiceLocator.Container.Resolve<IConfiguration>().TryGetAuthToken(host, out string? token))
{
longestTokenLen = Math.Max(longestTokenLen, token.Length);
}
}
// Create format string: {0,-longestHostLen} {1,-longestTokenLen}
string fmt = string.Format("{0}0,-{2}{1} {0}1,-{3}{1}",
"{", "}", longestHostLen, longestTokenLen);
user.RaiseMessage(fmt, hostHeader, tokenHeader);
user.RaiseMessage(fmt,
new string('-', longestHostLen),
new string('-', longestTokenLen)
);
user?.RaiseMessage(fmt, hostHeader, tokenHeader);
user?.RaiseMessage(fmt,
new string('-', longestHostLen),
new string('-', longestTokenLen));
foreach (string host in hosts)
{
if (ServiceLocator.Container.Resolve<IConfiguration>().TryGetAuthToken(host, out string token))
if (ServiceLocator.Container.Resolve<IConfiguration>().TryGetAuthToken(host, out string? token))
{
user.RaiseMessage(fmt, host, token);
user?.RaiseMessage(fmt, host, token);
}
}
}
Expand All @@ -101,36 +99,42 @@ private int listAuthTokens()

private int addAuthToken(AddAuthTokenOptions opts)
{
if (Uri.CheckHostName(opts.host) != UriHostNameType.Unknown)
{
ServiceLocator.Container.Resolve<IConfiguration>().SetAuthToken(opts.host, opts.token);
}
else
if (opts.host is string h)
{
user.RaiseError(Properties.Resources.AuthTokenInvalidHostName, opts.host);
if (Uri.CheckHostName(h) != UriHostNameType.Unknown)
{
ServiceLocator.Container.Resolve<IConfiguration>().SetAuthToken(h, opts.token);
}
else
{
user?.RaiseError(Properties.Resources.AuthTokenInvalidHostName, h);
}
}
return Exit.OK;
}

private int removeAuthToken(RemoveAuthTokenOptions opts)
{
ServiceLocator.Container.Resolve<IConfiguration>().SetAuthToken(opts.host, null);
if (opts.host is string h)
{
ServiceLocator.Container.Resolve<IConfiguration>().SetAuthToken(h, null);
}
return Exit.OK;
}

private IUser user;
private IUser? user;
}

internal class AuthTokenSubOptions : VerbCommandOptions
{
[VerbOption("list", HelpText = "List auth tokens")]
public CommonOptions ListOptions { get; set; }
public CommonOptions? ListOptions { get; set; }

[VerbOption("add", HelpText = "Add an auth token")]
public AddAuthTokenOptions AddOptions { get; set; }
public AddAuthTokenOptions? AddOptions { get; set; }

[VerbOption("remove", HelpText = "Delete an auth token")]
public RemoveAuthTokenOptions RemoveOptions { get; set; }
public RemoveAuthTokenOptions? RemoveOptions { get; set; }

[HelpVerbOption]
public string GetUsage(string verb)
Expand Down Expand Up @@ -173,13 +177,13 @@ public static IEnumerable<string> GetHelp(string verb)

internal class AddAuthTokenOptions : CommonOptions
{
[ValueOption(0)] public string host { get; set; }
[ValueOption(1)] public string token { get; set; }
[ValueOption(0)] public string? host { get; set; }
[ValueOption(1)] public string? token { get; set; }
}

internal class RemoveAuthTokenOptions : CommonOptions
{
[ValueOption(0)] public string host { get; set; }
[ValueOption(0)] public string? host { get; set; }
}

}
Loading