Skip to content

Commit

Permalink
Nullable references for Netkan
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Sep 3, 2024
1 parent ec7aba2 commit 8776ff7
Show file tree
Hide file tree
Showing 92 changed files with 1,149 additions and 1,099 deletions.
6 changes: 5 additions & 1 deletion Netkan/CKAN-netkan.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
<Deterministic>true</Deterministic>
<Configurations>Debug;Release;NoGUI</Configurations>
<Prefer32Bit>false</Prefer32Bit>
<LangVersion>7.3</LangVersion>
<LangVersion>9</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<StartupObject>CKAN.NetKAN.Program</StartupObject>
<ApplicationIcon>..\assets\ckan.ico</ApplicationIcon>
<TargetFrameworks>net48;net7.0</TargetFrameworks>
Expand All @@ -35,6 +37,8 @@
<PackageReference Include="Namotion.Reflection" Version="2.1.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="YamlDotNet" Version="9.1.0" />
<PackageReference Include="Nullable" Version="1.3.1" PrivateAssets="all" />
<PackageReference Include="IndexRange" Version="1.0.3" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net48' ">
<Reference Include="System" />
Expand Down
24 changes: 12 additions & 12 deletions Netkan/CmdLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,25 @@ internal class CmdLineOptions
public bool Debugger { get; set; }

[Option("outputdir", DefaultValue = ".", HelpText = "Output directory")]
public string OutputDir { get; set; }
public string? OutputDir { get; set; }

[Option("cachedir", HelpText = "Cache directory for downloaded mods")]
public string CacheDir { get; set; }
public string? CacheDir { get; set; }

[Option("github-token", HelpText = "GitHub OAuth token for API access")]
public string GitHubToken { get; set; }
public string? GitHubToken { get; set; }

[Option("gitlab-token", HelpText = "GitLab OAuth token for API access")]
public string GitLabToken { get; set; }
public string? GitLabToken { get; set; }

[Option("net-useragent", DefaultValue = null, HelpText = "Set the default User-Agent string for HTTP requests")]
public string NetUserAgent { get; set; }
public string? NetUserAgent { get; set; }

[Option("releases", DefaultValue = "1", HelpText = "Number of releases to inflate, or 'all'")]
public string Releases { get; set; }
public string? Releases { get; set; }

[Option("skip-releases", DefaultValue = "0", HelpText = "Number of releases to skip / index of release to inflate.")]
public string SkipReleases { get; set; }
public string? SkipReleases { get; set; }

[Option("prerelease", HelpText = "Index GitHub prereleases")]
public bool PreRelease { get; set; }
Expand All @@ -44,21 +44,21 @@ internal class CmdLineOptions
public bool OverwriteCache { get; set; }

[Option("queues", HelpText = "Input,Output queue names for Queue Inflator mode")]
public string Queues { get; set; }
public string? Queues { get; set; }

[Option("highest-version", HelpText = "Highest known version for auto-epoching")]
public string HighestVersion { get; set; }
public string? HighestVersion { get; set; }

[Option("validate-ckan", HelpText = "Name of .ckan file to check for errors")]
public string ValidateCkan { get; set; }
public string? ValidateCkan { get; set; }

[Option("version", HelpText = "Display the netkan version number and exit")]
public bool Version { get; set; }

[Option("game", DefaultValue = "KSP", HelpText = "Short name of the game for which to inflate mods")]
public string Game { get; set; }
public string? Game { get; set; }

[ValueOption(0)]
public string File { get; set; }
public string? File { get; set; }
}
}
9 changes: 5 additions & 4 deletions Netkan/ConsoleUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public int RaiseSelectionDialog(string message, params object[] args)
while (!valid)
{
// Wait for input from the command line.
string input = Console.In.ReadLine();
var input = Console.In.ReadLine();

if (input == null)
{
Expand All @@ -190,7 +190,7 @@ public int RaiseSelectionDialog(string message, params object[] args)
}

// Check for cancellation characters.
if (input == "c" || input == "n")
if (input is "c" or "n")
{
RaiseMessage("Selection cancelled.");

Expand Down Expand Up @@ -250,8 +250,9 @@ public void RaiseError(string message, params object[] args)
{
log.ErrorFormat(
message.Replace("\r\n", "%0A"),
args.Select(a => a.ToString().Replace("\r\n", "%0A")).ToArray()
);
args.OfType<string>()
.Select(a => a.ToString().Replace("\r\n", "%0A"))
.ToArray());
}
else
{
Expand Down
25 changes: 21 additions & 4 deletions Netkan/Extensions/JObjectExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;

using log4net;
using Newtonsoft.Json.Linq;

Expand All @@ -15,7 +17,7 @@ internal static class JObjectExtensions
/// <param name="jobject">The <see cref="JObject"/> to write to.</param>
/// <param name="propertyName">The name of the property to write.</param>
/// <param name="token">The value of the property to write if it does not exist.</param>
public static void SafeAdd(this JObject jobject, string propertyName, JToken token)
public static void SafeAdd(this JObject jobject, string propertyName, JToken? token)
{
if (string.IsNullOrWhiteSpace(propertyName))
{
Expand All @@ -36,7 +38,7 @@ public static void SafeAdd(this JObject jobject, string propertyName, JToken tok
/// <param name="jobject">The <see cref="JObject"/> to write to</param>
/// <param name="propertyName">The name of the property to write</param>
/// <param name="tokenCallback">Function to generate value of the property to write if it does not exist</param>
public static void SafeAdd(this JObject jobject, string propertyName, Func<JToken> tokenCallback)
public static void SafeAdd(this JObject jobject, string propertyName, Func<JToken?> tokenCallback)
{
if (string.IsNullOrWhiteSpace(propertyName))
{
Expand All @@ -59,9 +61,9 @@ public static void SafeAdd(this JObject jobject, string propertyName, Func<JToke
/// <returns>
/// Returns
/// </returns>
public static void SafeMerge(this JObject jobject, string propertyName, JToken token)
public static void SafeMerge(this JObject jobject, string propertyName, JToken? token)
{
JObject srcObj = token as JObject;
var srcObj = token as JObject;
// No need to do anything if source object is null or empty
if (srcObj?.Properties().Any() ?? false)
{
Expand All @@ -79,5 +81,20 @@ public static void SafeMerge(this JObject jobject, string propertyName, JToken t
}
}

public static JToken? FromMessyList<T>(T? first, IEnumerable<T?>? rest) where T: notnull
{
var items = Enumerable.Repeat(first, 1)
.Concat(rest ?? Enumerable.Empty<T?>())
.OfType<T>()
.Distinct()
.ToList();
return items.Count switch
{
0 => null,
1 => new JValue(items.First()),
_ => new JArray(items),
};
}

}
}
38 changes: 21 additions & 17 deletions Netkan/Extensions/YamlExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static YamlMappingNode[] Parse(TextReader input)
var stream = new YamlStream();
stream.Load(input);
return stream.Documents.Select(doc => doc?.RootNode as YamlMappingNode)
.OfType<YamlMappingNode>()
.ToArray();
}

Expand All @@ -34,17 +35,20 @@ public static JObject ToJObject(this YamlMappingNode yaml)
var jobj = new JObject();
foreach (var kvp in yaml)
{
switch (kvp.Value.NodeType)
if ((string?)kvp.Key is string k)
{
case YamlNodeType.Mapping:
jobj.Add((string)kvp.Key, (kvp.Value as YamlMappingNode).ToJObject());
break;
case YamlNodeType.Sequence:
jobj.Add((string)kvp.Key, (kvp.Value as YamlSequenceNode).ToJarray());
break;
case YamlNodeType.Scalar:
jobj.Add((string)kvp.Key, (kvp.Value as YamlScalarNode).ToJValue());
break;
switch (kvp.Value)
{
case YamlMappingNode obj:
jobj.Add(k, obj.ToJObject());
break;
case YamlSequenceNode array:
jobj.Add(k, array.ToJarray());
break;
case YamlScalarNode scalar:
jobj.Add(k, scalar.ToJValue());
break;
}
}
}
return jobj;
Expand All @@ -55,16 +59,16 @@ private static JArray ToJarray(this YamlSequenceNode yaml)
var jarr = new JArray();
foreach (var elt in yaml)
{
switch (elt.NodeType)
switch (elt)
{
case YamlNodeType.Mapping:
jarr.Add((elt as YamlMappingNode).ToJObject());
case YamlMappingNode obj:
jarr.Add(obj.ToJObject());
break;
case YamlNodeType.Sequence:
jarr.Add((elt as YamlSequenceNode).ToJarray());
case YamlSequenceNode array:
jarr.Add(array.ToJarray());
break;
case YamlNodeType.Scalar:
jarr.Add((elt as YamlScalarNode).ToJValue());
case YamlScalarNode scalar:
jarr.Add(scalar.ToJValue());
break;
}
}
Expand Down
Loading

0 comments on commit 8776ff7

Please sign in to comment.