Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
NoobNotFound committed Apr 13, 2023
1 parent f2e86ac commit 7b96836
Show file tree
Hide file tree
Showing 68 changed files with 309 additions and 463 deletions.
16 changes: 7 additions & 9 deletions CmlLib/Core/Auth/MLogin.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Net;
using System.Text;

Expand Down Expand Up @@ -86,7 +84,7 @@ private HttpWebResponse mojangRequest(string endpoint, string postdata)
HttpWebRequest http = WebRequest.CreateHttp(MojangServer.Auth + endpoint);
http.ContentType = "application/json";
http.Method = "POST";

using StreamWriter req = new StreamWriter(http.GetRequestStream());
req.Write(postdata);
req.Flush();
Expand Down Expand Up @@ -178,9 +176,9 @@ public MLoginResponse Authenticate(string id, string pw, string? clientToken)
var stream = resHeader.GetResponseStream();
if (stream == null)
return new MLoginResponse(
MLoginResult.UnknownError,
null,
"null response stream",
MLoginResult.UnknownError,
null,
"null response stream",
null);

using StreamReader res = new StreamReader(stream);
Expand Down Expand Up @@ -219,7 +217,7 @@ public MLoginResponse TryAutoLoginFromMojangLauncher()

if (activeAccount == null)
return new MLoginResponse(MLoginResult.NeedLogin, null, null, null);

return TryAutoLogin(activeAccount.ToSession());
}

Expand All @@ -230,7 +228,7 @@ public MLoginResponse TryAutoLoginFromMojangLauncher(string accountFilePath)

if (activeAccount == null)
return new MLoginResponse(MLoginResult.NeedLogin, null, null, null);

return TryAutoLogin(activeAccount.ToSession());
}

Expand Down Expand Up @@ -262,7 +260,7 @@ public MLoginResponse Refresh(MSession session)
null,
"null response stream",
null);

using StreamReader res = new StreamReader(stream);
string rawResponse = res.ReadToEnd();

Expand Down
6 changes: 3 additions & 3 deletions CmlLib/Core/Auth/MSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public MSession(string? username, string? accessToken, string? uuid)
public string? UUID { get; set; }
[JsonProperty("clientToken")]
public string? ClientToken { get; set; }

public string? UserType { get; set; }

public bool CheckIsValid()
Expand All @@ -35,8 +35,8 @@ public static MSession GetOfflineSession(string username)
{
return new MSession
{
Username = username,
AccessToken = "access_token",
Username = username,
AccessToken = "access_token",
UUID = "user_uuid",
UserType = "Mojang",
ClientToken = null
Expand Down
1 change: 0 additions & 1 deletion CmlLib/Core/Auth/Microsoft/AuthenticationResponse.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Newtonsoft.Json;
using System;

namespace CmlLib.Core.Auth.Microsoft
{
Expand Down
2 changes: 0 additions & 2 deletions CmlLib/Core/Auth/Microsoft/XboxMinecraftLogin.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using CmlLib.Core.Mojang;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Net;

namespace CmlLib.Core.Auth.Microsoft
Expand Down
56 changes: 26 additions & 30 deletions CmlLib/Core/CMLauncher.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
using CmlLib.Core.Downloader;
using CmlLib.Core.Files;
using CmlLib.Core.Installer;
using CmlLib.Core.Java;
using CmlLib.Core.Version;
using CmlLib.Core.VersionLoader;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using CmlLib.Core.Java;

namespace CmlLib.Core
{
Expand All @@ -21,7 +17,7 @@ public CMLauncher(string path) : this(new MinecraftPath(path))

public CMLauncher(MinecraftPath mc)
{
this.MinecraftPath = mc;
MinecraftPath = mc;

GameFileCheckers = new FileCheckerCollection();
FileDownloader = new AsyncParallelDownloader();
Expand All @@ -38,17 +34,17 @@ public CMLauncher(MinecraftPath mc)
public event DownloadFileChangedHandler? FileChanged;
public event ProgressChangedEventHandler? ProgressChanged;
public event EventHandler<string>? LogOutput;

private readonly IProgress<DownloadFileChangedEventArgs> pFileChanged;
private readonly IProgress<ProgressChangedEventArgs> pProgressChanged;

public MinecraftPath MinecraftPath { get; private set; }
public MVersionCollection? Versions { get; private set; }
public IVersionLoader VersionLoader { get; set; }

public FileCheckerCollection GameFileCheckers { get; private set; }
public IDownloader? FileDownloader { get; set; }

public IJavaPathResolver JavaPathResolver { get; set; }

public MVersionCollection GetAllVersions()
Expand Down Expand Up @@ -81,7 +77,7 @@ public async Task<MVersion> GetVersionAsync(string versionName)
.ConfigureAwait(false);
return version;
}

public string CheckForge(string mcversion, string forgeversion, string java)
{
if (Versions == null)
Expand Down Expand Up @@ -124,7 +120,7 @@ public string CheckForge(string mcversion, string forgeversion, string java)
public DownloadFile[] CheckLostGameFiles(MVersion version)
{
var lostFiles = new List<DownloadFile>();
foreach (IFileChecker checker in this.GameFileCheckers)
foreach (IFileChecker checker in GameFileCheckers)
{
DownloadFile[]? files = checker.CheckFiles(MinecraftPath, version, pFileChanged);
if (files != null)
Expand All @@ -137,7 +133,7 @@ public DownloadFile[] CheckLostGameFiles(MVersion version)
public async Task<DownloadFile[]> CheckLostGameFilesTaskAsync(MVersion version)
{
var lostFiles = new List<DownloadFile>();
foreach (IFileChecker checker in this.GameFileCheckers)
foreach (IFileChecker checker in GameFileCheckers)
{
DownloadFile[]? files = await checker.CheckFilesTaskAsync(MinecraftPath, version, pFileChanged)
.ConfigureAwait(false);
Expand All @@ -150,7 +146,7 @@ public async Task<DownloadFile[]> CheckLostGameFilesTaskAsync(MVersion version)

public async Task DownloadGameFiles(DownloadFile[] files)
{
if (this.FileDownloader == null)
if (FileDownloader == null)
return;

await FileDownloader.DownloadFiles(files, pFileChanged, pProgressChanged)
Expand All @@ -159,7 +155,7 @@ await FileDownloader.DownloadFiles(files, pFileChanged, pProgressChanged)

public void CheckAndDownload(MVersion version)
{
foreach (var checker in this.GameFileCheckers)
foreach (var checker in GameFileCheckers)
{
DownloadFile[]? files = checker.CheckFiles(MinecraftPath, version, pFileChanged);

Expand All @@ -172,7 +168,7 @@ public void CheckAndDownload(MVersion version)

public async Task CheckAndDownloadAsync(MVersion version)
{
foreach (var checker in this.GameFileCheckers)
foreach (var checker in GameFileCheckers)
{
DownloadFile[]? files = await checker.CheckFilesTaskAsync(MinecraftPath, version, pFileChanged)
.ConfigureAwait(false);
Expand All @@ -195,46 +191,46 @@ public Process CreateProcess(string mcversion, string forgeversion, MLaunchOptio

return CreateProcess(versionName, option);
}
public Process CreateProcess(string versionName, MLaunchOption option, bool checkAndDownload=true)

public Process CreateProcess(string versionName, MLaunchOption option, bool checkAndDownload = true)
=> CreateProcess(GetVersion(versionName), option, checkAndDownload);

[MethodTimer.Time]
public Process CreateProcess(MVersion version, MLaunchOption option, bool checkAndDownload=true)
public Process CreateProcess(MVersion version, MLaunchOption option, bool checkAndDownload = true)
{
option.StartVersion = version;

if (checkAndDownload)
CheckAndDownload(option.StartVersion);

return CreateProcess(option);
}

public async Task<Process> CreateProcessAsync(string versionName, MLaunchOption option,
bool checkAndDownload=true)
public async Task<Process> CreateProcessAsync(string versionName, MLaunchOption option,
bool checkAndDownload = true)
{
var version = await GetVersionAsync(versionName).ConfigureAwait(false);
return await CreateProcessAsync(version, option, checkAndDownload).ConfigureAwait(false);
}

public async Task<Process> CreateProcessAsync(MVersion version, MLaunchOption option,
bool checkAndDownload=true)
public async Task<Process> CreateProcessAsync(MVersion version, MLaunchOption option,
bool checkAndDownload = true)
{
option.StartVersion = version;

if (checkAndDownload)
await CheckAndDownloadAsync(option.StartVersion).ConfigureAwait(false);

return await CreateProcessAsync(option).ConfigureAwait(false);
}

public Process CreateProcess(MLaunchOption option)
{
checkLaunchOption(option);
var launch = new MLaunch(option);
return launch.GetProcess();
}

public async Task<Process> CreateProcessAsync(MLaunchOption option)
{
checkLaunchOption(option);
Expand Down Expand Up @@ -268,7 +264,7 @@ private void checkLaunchOption(MLaunchOption option)
else if (!string.IsNullOrEmpty(option.JavaVersion))
option.StartVersion.JavaVersion = option.JavaVersion;
else if (string.IsNullOrEmpty(option.StartVersion.JavaBinaryPath))
option.StartVersion.JavaBinaryPath =
option.StartVersion.JavaBinaryPath =
GetJavaPath(option.StartVersion) ?? GetDefaultJavaPath();
}
}
Expand All @@ -277,7 +273,7 @@ private void checkLaunchOption(MLaunchOption option)
{
if (string.IsNullOrEmpty(version.JavaVersion))
return null;

return JavaPathResolver.GetJavaBinaryPath(version.JavaVersion, MRule.OSName);
}

Expand Down
14 changes: 5 additions & 9 deletions CmlLib/Core/Downloader/AsyncParallelDownloader.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using CmlLib.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

namespace CmlLib.Core.Downloader
{
Expand Down Expand Up @@ -36,21 +32,21 @@ public AsyncParallelDownloader(int parallelism)
MaxThread = parallelism;
}

public async Task DownloadFiles(DownloadFile[] files,
public async Task DownloadFiles(DownloadFile[] files,
IProgress<DownloadFileChangedEventArgs>? fileProgress,
IProgress<ProgressChangedEventArgs>? downloadProgress)
{
if (files.Length == 0)
return;

if (isRunning)
throw new InvalidOperationException("already downloading");

isRunning = true;

pChangeFile = fileProgress;
pChangeProgress = downloadProgress;

totalFiles = files.Length;
progressedFiles = 0;

Expand All @@ -66,7 +62,7 @@ public async Task DownloadFiles(DownloadFile[] files,
fileProgress?.Report(
new DownloadFileChangedEventArgs(files[0].Type, this, null, files.Length, 0));
await ForEachAsyncSemaphore(files, MaxThread, doDownload).ConfigureAwait(false);

isRunning = false;
}

Expand All @@ -90,7 +86,7 @@ async Task work(T item)
throttler.Release();
}
}

tasks.Add(work(element));
}
await Task.WhenAll(tasks).ConfigureAwait(false);
Expand Down
11 changes: 4 additions & 7 deletions CmlLib/Core/Downloader/DownloadFile.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
using System;
using System.Threading.Tasks;

namespace CmlLib.Core.Downloader
namespace CmlLib.Core.Downloader
{
public class DownloadFile : IEquatable<DownloadFile>
{
public DownloadFile(string path, string url)
{
this.Path = path;
this.Url = url;
Path = path;
Url = url;
}

public MFile Type { get; set; }
public string? Name { get; set; }
public string Path { get; private set; }
Expand Down
4 changes: 1 addition & 3 deletions CmlLib/Core/Downloader/DownloadFileChangedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;

namespace CmlLib.Core.Downloader
namespace CmlLib.Core.Downloader
{
public delegate void DownloadFileChangedHandler(DownloadFileChangedEventArgs e);

Expand Down
10 changes: 5 additions & 5 deletions CmlLib/Core/Downloader/DownloadFileProgress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ public class DownloadFileProgress
{
public DownloadFileProgress(DownloadFile file, long total, long progressed, long received, int percent)
{
this.File = file;
this.TotalBytes = total;
this.ProgressedBytes = progressed;
this.ReceivedBytes = received;
this.ProgressPercentage = percent;
File = file;
TotalBytes = total;
ProgressedBytes = progressed;
ReceivedBytes = received;
ProgressPercentage = percent;
}

public DownloadFile File { get; private set; }
Expand Down
4 changes: 2 additions & 2 deletions CmlLib/Core/Downloader/FileProgressChangedEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ public class FileProgressChangedEventArgs : ProgressChangedEventArgs
{
public FileProgressChangedEventArgs(long total, long received, int percent) : base(percent, null)
{
this.TotalBytes = total;
this.ReceivedBytes = received;
TotalBytes = total;
ReceivedBytes = received;
}

public long TotalBytes { get; private set; }
Expand Down
8 changes: 3 additions & 5 deletions CmlLib/Core/Downloader/IDownloader.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using System;
using System.ComponentModel;
using System.Threading.Tasks;
using System.ComponentModel;

namespace CmlLib.Core.Downloader
{
public interface IDownloader
{
Task DownloadFiles(DownloadFile[] files,
IProgress<DownloadFileChangedEventArgs>? fileProgress,
Task DownloadFiles(DownloadFile[] files,
IProgress<DownloadFileChangedEventArgs>? fileProgress,
IProgress<ProgressChangedEventArgs>? downloadProgress);
}
}
Loading

0 comments on commit 7b96836

Please sign in to comment.