Skip to content

Commit

Permalink
null safety, clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaBs committed Nov 22, 2021
1 parent d35f684 commit 30fd9c4
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 26 deletions.
24 changes: 20 additions & 4 deletions CmlLib/Core/Auth/MLogin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions CmlLib/Core/Auth/Microsoft/XboxMinecraftLogin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
5 changes: 4 additions & 1 deletion CmlLib/Core/Installer/MForge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string?>()
: mapping(installerData, "client", minecraftJar, installerPath);

// process
process(installerObj["processors"] as JArray, mapData);
Expand Down
4 changes: 2 additions & 2 deletions CmlLib/Core/Mapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static string[] MapPathString(string[] arg, string prepath)

public static string Interpolation(string str, Dictionary<string, string?> dicts)
{
str = argBracket.Replace(str, new MatchEvaluator((match =>
str = argBracket.Replace(str, (match =>
{
if (match.Groups.Count < 2)
return match.Value;
Expand All @@ -66,7 +66,7 @@ public static string Interpolation(string str, Dictionary<string, string?> dicts
}

return match.Value;
})));
}));

return str;
}
Expand Down
2 changes: 2 additions & 0 deletions CmlLib/Core/Mojang/MojangAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
8 changes: 4 additions & 4 deletions CmlLib/Core/MojangLauncher/MojangLauncherAccounts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MojangLauncherAccounts>(json);
}
Expand Down
1 change: 0 additions & 1 deletion CmlLib/Core/Version/MVersionCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 0 additions & 3 deletions CmlLib/Core/Version/MVersionSort.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 0 additions & 1 deletion CmlLib/Core/VersionMetadata/MVersionMetadata.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Data.SqlTypes;
using System.Threading.Tasks;
using CmlLib.Core.Version;
using Newtonsoft.Json;
Expand Down
4 changes: 1 addition & 3 deletions CmlLib/Utils/SemiVersion.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Linq;

namespace CmlLib.Utils
namespace CmlLib.Utils
{
public class SemiVersion
{
Expand Down
18 changes: 11 additions & 7 deletions CmlLib/Utils/WebDownload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 30fd9c4

Please sign in to comment.