From 30fd9c49a25c23aca2198518734ae72415fcf91b Mon Sep 17 00:00:00 2001 From: AlphaBs Date: Tue, 23 Nov 2021 04:50:54 +0900 Subject: [PATCH] null safety, clean code --- CmlLib/Core/Auth/MLogin.cs | 24 +++++++++++++++---- .../Core/Auth/Microsoft/XboxMinecraftLogin.cs | 2 ++ CmlLib/Core/Installer/MForge.cs | 5 +++- CmlLib/Core/Mapper.cs | 4 ++-- CmlLib/Core/Mojang/MojangAPI.cs | 2 ++ .../MojangLauncher/MojangLauncherAccounts.cs | 8 +++---- CmlLib/Core/Version/MVersionCollection.cs | 1 - CmlLib/Core/Version/MVersionSort.cs | 3 --- .../Core/VersionMetadata/MVersionMetadata.cs | 1 - CmlLib/Utils/SemiVersion.cs | 4 +--- CmlLib/Utils/WebDownload.cs | 18 ++++++++------ 11 files changed, 46 insertions(+), 26 deletions(-) diff --git a/CmlLib/Core/Auth/MLogin.cs b/CmlLib/Core/Auth/MLogin.cs index f866114..6c82d88 100644 --- a/CmlLib/Core/Auth/MLogin.cs +++ b/CmlLib/Core/Auth/MLogin.cs @@ -175,7 +175,15 @@ public MLoginResponse Authenticate(string id, string pw, string? clientToken) HttpWebResponse resHeader = mojangRequest("authenticate", req.ToString()); - using StreamReader res = new StreamReader(resHeader.GetResponseStream()); + var stream = resHeader.GetResponseStream(); + if (stream == null) + return new MLoginResponse( + MLoginResult.UnknownError, + null, + "null response stream", + null); + + using StreamReader res = new StreamReader(stream); string rawResponse = res.ReadToEnd(); if (resHeader.StatusCode == HttpStatusCode.OK) // ResultCode == 200 return parseSession(rawResponse, clientToken); @@ -207,7 +215,7 @@ public MLoginResponse TryAutoLogin(MSession session) public MLoginResponse TryAutoLoginFromMojangLauncher() { var mojangAccounts = MojangLauncher.MojangLauncherAccounts.FromDefaultPath(); - var activeAccount = mojangAccounts.GetActiveAccount(); + var activeAccount = mojangAccounts?.GetActiveAccount(); if (activeAccount == null) return new MLoginResponse(MLoginResult.NeedLogin, null, null, null); @@ -218,7 +226,7 @@ public MLoginResponse TryAutoLoginFromMojangLauncher() public MLoginResponse TryAutoLoginFromMojangLauncher(string accountFilePath) { var mojangAccounts = MojangLauncher.MojangLauncherAccounts.FromFile(accountFilePath); - var activeAccount = mojangAccounts.GetActiveAccount(); + var activeAccount = mojangAccounts?.GetActiveAccount(); if (activeAccount == null) return new MLoginResponse(MLoginResult.NeedLogin, null, null, null); @@ -247,7 +255,15 @@ public MLoginResponse Refresh(MSession session) }; HttpWebResponse resHeader = mojangRequest("refresh", req.ToString()); - using StreamReader res = new StreamReader(resHeader.GetResponseStream()); + var stream = resHeader.GetResponseStream(); + if (stream == null) + return new MLoginResponse( + MLoginResult.UnknownError, + null, + "null response stream", + null); + + using StreamReader res = new StreamReader(stream); string rawResponse = res.ReadToEnd(); if ((int)resHeader.StatusCode / 100 == 2) diff --git a/CmlLib/Core/Auth/Microsoft/XboxMinecraftLogin.cs b/CmlLib/Core/Auth/Microsoft/XboxMinecraftLogin.cs index aceb74d..b06c287 100644 --- a/CmlLib/Core/Auth/Microsoft/XboxMinecraftLogin.cs +++ b/CmlLib/Core/Auth/Microsoft/XboxMinecraftLogin.cs @@ -20,6 +20,8 @@ private void writeReq(WebRequest req, string data) private string readRes(WebResponse res) { using var resStream = res.GetResponseStream(); + if (resStream == null) + return ""; using var sr = new StreamReader(resStream); return sr.ReadToEnd(); } diff --git a/CmlLib/Core/Installer/MForge.cs b/CmlLib/Core/Installer/MForge.cs index fc414e6..f92bdc7 100644 --- a/CmlLib/Core/Installer/MForge.cs +++ b/CmlLib/Core/Installer/MForge.cs @@ -73,7 +73,10 @@ public string InstallForge(string mcVersion, string forgeVersion) checkLibraries(installerObj["libraries"] as JArray); // mapping client data - var mapData = mapping(installerObj["data"] as JObject, "client", minecraftJar, installerPath); + var installerData = installerObj["data"] as JObject; + var mapData = (installerData == null) + ? new Dictionary() + : mapping(installerData, "client", minecraftJar, installerPath); // process process(installerObj["processors"] as JArray, mapData); diff --git a/CmlLib/Core/Mapper.cs b/CmlLib/Core/Mapper.cs index d4bc6d2..74d5fa5 100644 --- a/CmlLib/Core/Mapper.cs +++ b/CmlLib/Core/Mapper.cs @@ -51,7 +51,7 @@ public static string[] MapPathString(string[] arg, string prepath) public static string Interpolation(string str, Dictionary dicts) { - str = argBracket.Replace(str, new MatchEvaluator((match => + str = argBracket.Replace(str, (match => { if (match.Groups.Count < 2) return match.Value; @@ -66,7 +66,7 @@ public static string Interpolation(string str, Dictionary dicts } return match.Value; - }))); + })); return str; } diff --git a/CmlLib/Core/Mojang/MojangAPI.cs b/CmlLib/Core/Mojang/MojangAPI.cs index bc7b80a..2e9fcfc 100644 --- a/CmlLib/Core/Mojang/MojangAPI.cs +++ b/CmlLib/Core/Mojang/MojangAPI.cs @@ -14,6 +14,8 @@ public static class MojangAPI private static string readRes(WebResponse res) { using var resStream = res.GetResponseStream(); + if (resStream == null) + return ""; using var sr = new StreamReader(resStream); return sr.ReadToEnd(); } diff --git a/CmlLib/Core/MojangLauncher/MojangLauncherAccounts.cs b/CmlLib/Core/MojangLauncher/MojangLauncherAccounts.cs index 0aeb8eb..ad097e2 100644 --- a/CmlLib/Core/MojangLauncher/MojangLauncherAccounts.cs +++ b/CmlLib/Core/MojangLauncher/MojangLauncherAccounts.cs @@ -37,19 +37,19 @@ public void SaveTo(string path) File.WriteAllText(path, json); } - public static MojangLauncherAccounts FromDefaultPath() + public static MojangLauncherAccounts? FromDefaultPath() { var path = Path.Combine(MinecraftPath.GetOSDefaultPath(), "launcher_accounts.json"); return FromFile(path); } - public static MojangLauncherAccounts FromFile(string path) + public static MojangLauncherAccounts? FromFile(string path) { var content = File.ReadAllText(path); - return FromJSON(content); + return FromJson(content); } - public static MojangLauncherAccounts? FromJSON(string json) + public static MojangLauncherAccounts? FromJson(string json) { return JsonConvert.DeserializeObject(json); } diff --git a/CmlLib/Core/Version/MVersionCollection.cs b/CmlLib/Core/Version/MVersionCollection.cs index 4b37695..127fcdb 100644 --- a/CmlLib/Core/Version/MVersionCollection.cs +++ b/CmlLib/Core/Version/MVersionCollection.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Collections.Specialized; using System.IO; -using System.Linq; using System.Threading.Tasks; using CmlLib.Core.VersionMetadata; diff --git a/CmlLib/Core/Version/MVersionSort.cs b/CmlLib/Core/Version/MVersionSort.cs index 46356da..cc013d2 100644 --- a/CmlLib/Core/Version/MVersionSort.cs +++ b/CmlLib/Core/Version/MVersionSort.cs @@ -1,9 +1,6 @@ using System; using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Diagnostics; using System.Linq; -using System.Text; using CmlLib.Core.VersionMetadata; namespace CmlLib.Core.Version diff --git a/CmlLib/Core/VersionMetadata/MVersionMetadata.cs b/CmlLib/Core/VersionMetadata/MVersionMetadata.cs index aa8481e..592c658 100644 --- a/CmlLib/Core/VersionMetadata/MVersionMetadata.cs +++ b/CmlLib/Core/VersionMetadata/MVersionMetadata.cs @@ -1,5 +1,4 @@ using System; -using System.Data.SqlTypes; using System.Threading.Tasks; using CmlLib.Core.Version; using Newtonsoft.Json; diff --git a/CmlLib/Utils/SemiVersion.cs b/CmlLib/Utils/SemiVersion.cs index 9d8c5a2..a0c4e01 100644 --- a/CmlLib/Utils/SemiVersion.cs +++ b/CmlLib/Utils/SemiVersion.cs @@ -1,6 +1,4 @@ -using System.Linq; - -namespace CmlLib.Utils +namespace CmlLib.Utils { public class SemiVersion { diff --git a/CmlLib/Utils/WebDownload.cs b/CmlLib/Utils/WebDownload.cs index 4cd4c35..7a33978 100644 --- a/CmlLib/Utils/WebDownload.cs +++ b/CmlLib/Utils/WebDownload.cs @@ -15,9 +15,12 @@ internal class WebDownload private class TimeoutWebClient : WebClient { - protected override WebRequest GetWebRequest(Uri uri) + protected override WebRequest? GetWebRequest(Uri uri) { - WebRequest w = base.GetWebRequest(uri); + WebRequest? w = base.GetWebRequest(uri); + if (w == null) + return null; + w.Timeout = DefaultWebRequestTimeout; if (IgnoreProxy) @@ -114,11 +117,12 @@ internal void DownloadFileLimit(string url, string path) req.ContinueTimeout = 5000; var res = req.GetResponse(); - using (var httpStream = res.GetResponseStream()) - using (var fs = File.OpenWrite(path)) - { - httpStream.CopyTo(fs); - } + using var httpStream = res.GetResponseStream(); + if (httpStream == null) + return; + + using var fs = File.OpenWrite(path); + httpStream.CopyTo(fs); } private void progressChanged(long value, long max)