Skip to content

Commit

Permalink
Nullable references for Core
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Sep 2, 2024
1 parent 9ce588f commit 8dcead3
Show file tree
Hide file tree
Showing 123 changed files with 2,326 additions and 2,164 deletions.
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
6 changes: 3 additions & 3 deletions ConsoleUI/RepoScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ protected RepoScreen(IGame game, SortedDictionary<string, Repository> reps, stri
});

// mainMenu = list of default options
if (defaultRepos.repositories != null && defaultRepos.repositories.Length > 0) {
if (defaultRepos?.repositories != null && defaultRepos?.repositories.Length > 0) {
List<ConsoleMenuOption> opts = new List<ConsoleMenuOption>();
foreach (Repository r in defaultRepos.repositories) {
foreach (Repository r in defaultRepos?.repositories) {
// This variable will be remembered correctly in our lambdas later
Repository repo = r;
opts.Add(new ConsoleMenuOption(
Expand Down Expand Up @@ -153,7 +153,7 @@ protected bool urlValid()
/// </summary>
protected SortedDictionary<string, Repository> editList;

private RepositoryList defaultRepos;
private RepositoryList? defaultRepos;

private int labelWidth => Math.Max(8, Math.Max(
Properties.Resources.RepoNameLabel.Length,
Expand Down
14 changes: 7 additions & 7 deletions Core/AutoUpdate/AutoUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public AutoUpdate()

public CkanUpdate GetUpdate(bool devBuild)
{
if (updates.TryGetValue(devBuild, out CkanUpdate update))
if (updates.TryGetValue(devBuild, out CkanUpdate? update))
{
return update;
}
Expand All @@ -30,6 +30,9 @@ public CkanUpdate GetUpdate(bool devBuild)

private readonly Dictionary<bool, CkanUpdate> updates = new Dictionary<bool, CkanUpdate>();

// This is null when running tests, seemingly.
private static readonly string exePath = Assembly.GetEntryAssembly()?.Location ?? "";

/// <summary>
/// Report whether it's possible to run the auto-updater.
/// Checks whether we can overwrite the running ckan.exe.
Expand All @@ -43,7 +46,7 @@ public CkanUpdate GetUpdate(bool devBuild)
/// and then launches the helper allowing us to upgrade.
/// </summary>
/// <param name="launchCKANAfterUpdate">If set to <c>true</c> launch CKAN after update.</param>
public void StartUpdateProcess(bool launchCKANAfterUpdate, bool devBuild, IUser user = null)
public void StartUpdateProcess(bool launchCKANAfterUpdate, bool devBuild, IUser? user = null)
{
var pid = Process.GetCurrentProcess().Id;

Expand Down Expand Up @@ -84,8 +87,8 @@ public static void SetExecutable(string fileName)
{
UseShellExecute = false
};
Process permsprocess = Process.Start(permsinfo);
permsprocess.WaitForExit();
var permsprocess = Process.Start(permsinfo);
permsprocess?.WaitForExit();
}
}

Expand All @@ -103,8 +106,5 @@ private static bool CanWrite(string path)
return false;
}
}

// This is null when running tests, seemingly.
private static readonly string exePath = Assembly.GetEntryAssembly()?.Location ?? "";
}
}
8 changes: 2 additions & 6 deletions Core/AutoUpdate/CkanUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@ namespace CKAN
/// </summary>
public abstract class CkanUpdate
{
public CkanModuleVersion Version { get; protected set; }
public Uri ReleaseDownload { get; protected set; }
public long ReleaseSize { get; protected set; }
public Uri UpdaterDownload { get; protected set; }
public long UpdaterSize { get; protected set; }
public string ReleaseNotes { get; protected set; }
public CkanModuleVersion? Version { get; protected set; }
public string? ReleaseNotes { get; protected set; }

public string updaterFilename = $"{Path.GetTempPath()}{Guid.NewGuid()}.exe";
public string ckanFilename = $"{Path.GetTempPath()}{Guid.NewGuid()}.exe";
Expand Down
55 changes: 35 additions & 20 deletions Core/AutoUpdate/GithubReleaseCkanUpdate.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Collections.Generic;

using Autofac;
Expand All @@ -18,38 +19,47 @@ public class GitHubReleaseCkanUpdate : CkanUpdate
/// Initialize the Object
/// </summary>
/// <param name="json">JSON representation of release</param>
public GitHubReleaseCkanUpdate(GitHubReleaseInfo releaseJson = null)
public GitHubReleaseCkanUpdate(GitHubReleaseInfo? releaseJson = null)
{
if (releaseJson == null)
{
var coreConfig = ServiceLocator.Container.Resolve<IConfiguration>();
var token = coreConfig.TryGetAuthToken(latestCKANReleaseApiUrl.Host, out string t)
var token = coreConfig.TryGetAuthToken(latestCKANReleaseApiUrl.Host, out string? t)
? t : null;
releaseJson = JsonConvert.DeserializeObject<GitHubReleaseInfo>(
Net.DownloadText(latestCKANReleaseApiUrl, token));
if (releaseJson == null)
{
throw new Kraken(Properties.Resources.AutoUpdateNotFetched);
}
releaseJson = Net.DownloadText(latestCKANReleaseApiUrl, token) is string content
? JsonConvert.DeserializeObject<GitHubReleaseInfo>(content)
: null;
}
if (releaseJson is null
|| releaseJson.tag_name is null
|| releaseJson.name is null
|| releaseJson.body is null
|| releaseJson.assets is null)
{
throw new Kraken(Properties.Resources.AutoUpdateNotFetched);
}

Version = new CkanModuleVersion(releaseJson.tag_name.ToString(),
releaseJson.name.ToString());
ReleaseNotes = ExtractReleaseNotes(releaseJson.body.ToString());
foreach (var asset in releaseJson.assets)

var releaseAsset = releaseJson.assets.First(asset => asset.browser_download_url
?.ToString()
.EndsWith("ckan.exe")
?? false);
var updaterAsset = releaseJson.assets.First(asset => asset.browser_download_url
?.ToString()
.EndsWith("AutoUpdater.exe")
?? false);
if (releaseAsset.browser_download_url is null
|| updaterAsset.browser_download_url is null)
{
string url = asset.browser_download_url.ToString();
if (url.EndsWith("ckan.exe"))
{
ReleaseDownload = asset.browser_download_url;
ReleaseSize = asset.size;
}
else if (url.EndsWith("AutoUpdater.exe"))
{
UpdaterDownload = asset.browser_download_url;
UpdaterSize = asset.size;
}
throw new Kraken(Properties.Resources.AutoUpdateNotFetched);
}
ReleaseDownload = releaseAsset.browser_download_url;
ReleaseSize = releaseAsset.size;
UpdaterDownload = updaterAsset.browser_download_url;
UpdaterSize = updaterAsset.size;
}

public override IList<NetAsyncDownloader.DownloadTarget> Targets => new[]
Expand All @@ -60,6 +70,11 @@ public GitHubReleaseCkanUpdate(GitHubReleaseInfo releaseJson = null)
ReleaseDownload, ckanFilename, ReleaseSize),
};

private Uri ReleaseDownload { get; set; }
private long ReleaseSize { get; set; }
private Uri UpdaterDownload { get; set; }
private long UpdaterSize { get; set; }

/// <summary>
/// Extracts release notes from the body of text provided by the github API.
/// By default this is everything after the first three dashes on a line by
Expand Down
12 changes: 6 additions & 6 deletions Core/AutoUpdate/GithubReleaseInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ namespace CKAN
{
public class GitHubReleaseInfo
{
public string tag_name;
public string name;
public string body;
public List<Asset> assets;
public string? tag_name;
public string? name;
public string? body;
public List<Asset>? assets;

public sealed class Asset
{
public Uri browser_download_url;
public long size;
public Uri? browser_download_url;
public long size;
}
}
}
24 changes: 11 additions & 13 deletions Core/AutoUpdate/S3BuildCkanUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,15 @@ namespace CKAN
{
public class S3BuildCkanUpdate : CkanUpdate
{
public S3BuildCkanUpdate(S3BuildVersionInfo versionJson = null)
public S3BuildCkanUpdate(S3BuildVersionInfo? versionJson = null)
{
if (versionJson == null)
versionJson ??= Net.DownloadText(new Uri(S3BaseUrl, VersionJsonUrlPiece)) is string content
? JsonConvert.DeserializeObject<S3BuildVersionInfo>(content)
: null;
if (versionJson is null || versionJson.version is null)
{
versionJson = JsonConvert.DeserializeObject<S3BuildVersionInfo>(
Net.DownloadText(new Uri(S3BaseUrl, VersionJsonUrlPiece)));
if (versionJson == null)
{
throw new Kraken(Properties.Resources.AutoUpdateNotFetched);
}
throw new Kraken(Properties.Resources.AutoUpdateNotFetched);
}

Version = new CkanModuleVersion(versionJson.version.ToString(), "dev");
ReleaseNotes = versionJson.changelog;
UpdaterDownload = new Uri(S3BaseUrl, AutoUpdaterUrlPiece);
Expand All @@ -29,12 +26,13 @@ public S3BuildCkanUpdate(S3BuildVersionInfo versionJson = null)

public override IList<NetAsyncDownloader.DownloadTarget> Targets => new[]
{
new NetAsyncDownloader.DownloadTargetFile(
UpdaterDownload, updaterFilename),
new NetAsyncDownloader.DownloadTargetFile(
ReleaseDownload, ckanFilename),
new NetAsyncDownloader.DownloadTargetFile(UpdaterDownload, updaterFilename),
new NetAsyncDownloader.DownloadTargetFile(ReleaseDownload, ckanFilename),
};

private Uri ReleaseDownload { get; set; }
private Uri UpdaterDownload { get; set; }

private static readonly Uri S3BaseUrl =
new Uri("https://ksp-ckan.s3-us-west-2.amazonaws.com/");
private const string VersionJsonUrlPiece = "version.json";
Expand Down
4 changes: 2 additions & 2 deletions Core/AutoUpdate/S3BuildVersionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace CKAN
{
public class S3BuildVersionInfo
{
public ModuleVersion version;
public string changelog;
public ModuleVersion? version;
public string? changelog;
}
}
7 changes: 6 additions & 1 deletion Core/CKAN-core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
<Deterministic>true</Deterministic>
<Configurations>Debug;Release;NoGUI</Configurations>
<Prefer32Bit>false</Prefer32Bit>
<LangVersion>7.3</LangVersion>
<LangVersion>9</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarningsNotAsErrors>SYSLIB0050,IDE0090</WarningsNotAsErrors>
<NoWarn>IDE1006</NoWarn>
<TargetFrameworks>netstandard2.0;net48;net7.0</TargetFrameworks>
<CoreCompileDependsOn>PrepareResources;$(CompileDependsOn)</CoreCompileDependsOn>
Expand Down Expand Up @@ -54,6 +57,8 @@
<PackageReference Include="NJsonSchema" Version="10.9.0" />
<PackageReference Include="ValveKeyValue" Version="0.3.1.152" />
<PackageReference Include="Mono.Cecil" Version="0.11.5" />
<PackageReference Include="Nullable" Version="1.3.1" PrivateAssets="all" />
<PackageReference Include="IndexRange" Version="1.0.3" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\_build\meta\GlobalAssemblyVersionInfo.cs">
Expand Down
15 changes: 2 additions & 13 deletions Core/CKANPathUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ public static class CKANPathUtils
/// <param name="path">The path to normalize</param>
/// <returns>The normalized path</returns>
public static string NormalizePath(string path)
=> path == null ? null
: path.Length < 2 ? path.Replace('\\', '/')
: path.Replace('\\', '/').TrimEnd('/');
=> path.Length < 2 ? path.Replace('\\', '/')
: path.Replace('\\', '/').TrimEnd('/');

/// <summary>
/// Gets the last path element. Ex: /a/b/c returns c
Expand Down Expand Up @@ -61,11 +60,6 @@ public static string GetLeadingPathElements(string path)
/// </summary>
public static string ToRelative(string path, string root)
{
if (path == null || root == null)
{
throw new PathErrorKraken(null, "Null path provided");
}

// We have to normalise before we check for rootedness,
// otherwise backslash separators fail on Linux.

Expand Down Expand Up @@ -96,11 +90,6 @@ public static string ToRelative(string path, string root)
/// </summary>
public static string ToAbsolute(string path, string root)
{
if (path == null || root == null)
{
throw new PathErrorKraken(null, "Null path provided");
}

path = NormalizePath(path);
root = NormalizePath(root);

Expand Down
4 changes: 2 additions & 2 deletions Core/CkanTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ private static void SetMaxTimeout(TimeSpan timeout)
}
}

private static void SetField(Type T, string fieldName, object value)
private static void SetField(Type T, string fieldName, object? value)
{
try
{
T.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Static)
.SetValue(null, value);
?.SetValue(null, value);
}
catch
{
Expand Down
2 changes: 1 addition & 1 deletion Core/CompatibleGameVersions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace CKAN
[JsonConverter(typeof(CompatibleGameVersionsConverter))]
public class CompatibleGameVersions
{
public string GameVersionWhenWritten { get; set; }
public string? GameVersionWhenWritten { get; set; }

public List<string> Versions { get; set; } = new List<string>();
}
Expand Down
Loading

0 comments on commit 8dcead3

Please sign in to comment.