Skip to content

Commit

Permalink
Clean Code
Browse files Browse the repository at this point in the history
  • Loading branch information
NoobNotFound committed May 9, 2023
1 parent 652c845 commit c59e3db
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 66 deletions.
2 changes: 1 addition & 1 deletion CmlLib/Core/Auth/MSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class MSession
{
public MSession() { }

public MSession(string? username, string? accessToken, string? uuid,string? clientToken = null)
public MSession(string? username, string? accessToken, string? uuid, string? clientToken = null)
{
Username = username;
AccessToken = accessToken;
Expand Down
6 changes: 2 additions & 4 deletions CmlLib/Core/Files/AssetChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@ private void checkIndex(MinecraftPath path, MVersion version)
if (!string.IsNullOrEmpty(directoryName))
Directory.CreateDirectory(directoryName);

using (var wc = new WebClient())
{
wc.DownloadFile(version.AssetUrl, index);
}
using var wc = new WebClient();
wc.DownloadFile(version.AssetUrl, index);
}
}

Expand Down
18 changes: 7 additions & 11 deletions CmlLib/Core/Installer/MJava.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,17 @@ public async Task<string> CheckJavaAsync(IProgress<ProgressChangedEventArgs>? pr

public string GetJavaUrl()
{
using (var wc = new WebClient())
{
string json = wc.DownloadString(MojangServer.LauncherMeta);
return parseLauncherMetadata(json);
}
using var wc = new WebClient();
string json = wc.DownloadString(MojangServer.LauncherMeta);
return parseLauncherMetadata(json);
}

public async Task<string> GetJavaUrlAsync()
{
using (var wc = new WebClient())
{
string json = await wc.DownloadStringTaskAsync(MojangServer.LauncherMeta)
.ConfigureAwait(false);
return parseLauncherMetadata(json);
}
using var wc = new WebClient();
string json = await wc.DownloadStringTaskAsync(MojangServer.LauncherMeta)
.ConfigureAwait(false);
return parseLauncherMetadata(json);
}

private string parseLauncherMetadata(string json)
Expand Down
18 changes: 8 additions & 10 deletions CmlLib/Utils/GameOptionsFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,18 +195,16 @@ public void Save(Encoding encoding)

public void Save(string path, Encoding encoding)
{
using (var fs = File.OpenWrite(path))
using (var writer = new StreamWriter(fs, encoding))
using var fs = File.OpenWrite(path);
using var writer = new StreamWriter(fs, encoding);
foreach (KeyValuePair<string, string?> keyvalue in options)
{
foreach (KeyValuePair<string, string?> keyvalue in options)
if (keyvalue.Value == null)
writer.WriteLine(keyvalue.Key);
else
{
if (keyvalue.Value == null)
writer.WriteLine(keyvalue.Key);
else
{
var line = ToKeyValueString(keyvalue.Key, keyvalue.Value);
writer.WriteLine(line);
}
var line = ToKeyValueString(keyvalue.Key, keyvalue.Value);
writer.WriteLine(line);
}
}
}
Expand Down
36 changes: 16 additions & 20 deletions CmlLib/Utils/SharpZip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,27 @@ public SharpZip(string path)

public void Unzip(string path)
{
using (var fs = File.OpenRead(ZipPath))
using (var s = new ZipInputStream(fs))
using var fs = File.OpenRead(ZipPath);
using var s = new ZipInputStream(fs);
long length = fs.Length;
ZipEntry e;
while ((e = s.GetNextEntry()) != null)
{
long length = fs.Length;
ZipEntry e;
while ((e = s.GetNextEntry()) != null)
{
var zfile = Path.Combine(path, e.Name);

var dirName = Path.GetDirectoryName(zfile);
var fileName = Path.GetFileName(zfile);
var zfile = Path.Combine(path, e.Name);

if (!string.IsNullOrWhiteSpace(dirName))
Directory.CreateDirectory(dirName);
var dirName = Path.GetDirectoryName(zfile);
var fileName = Path.GetFileName(zfile);

if (!string.IsNullOrWhiteSpace(fileName))
{
using (var zFileStream = File.OpenWrite(zfile))
{
s.CopyTo(zFileStream);
}
}
if (!string.IsNullOrWhiteSpace(dirName))
Directory.CreateDirectory(dirName);

ev(s.Position, length);
if (!string.IsNullOrWhiteSpace(fileName))
{
using var zFileStream = File.OpenWrite(zfile);
s.CopyTo(zFileStream);
}

ev(s.Position, length);
}
}

Expand Down
38 changes: 18 additions & 20 deletions CmlLib/Utils/WebDownload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,28 +78,26 @@ internal async Task DownloadFileAsync(DownloadFile file)
if (!string.IsNullOrEmpty(directoryName))
Directory.CreateDirectory(directoryName);

using (var wc = new TimeoutWebClient())
{
long lastBytes = 0;
using var wc = new TimeoutWebClient();
long lastBytes = 0;

wc.DownloadProgressChanged += (s, e) =>
wc.DownloadProgressChanged += (s, e) =>
{
lock (locker)
{
lock (locker)
{
var progressedBytes = e.BytesReceived - lastBytes;
if (progressedBytes < 0)
return;

lastBytes = e.BytesReceived;

var progress = new DownloadFileProgress(
file, e.TotalBytesToReceive, progressedBytes, e.BytesReceived, e.ProgressPercentage);
FileDownloadProgressChanged?.Invoke(this, progress);
}
};
await wc.DownloadFileTaskAsync(file.Url, file.Path)
.ConfigureAwait(false);
}
var progressedBytes = e.BytesReceived - lastBytes;
if (progressedBytes < 0)
return;

lastBytes = e.BytesReceived;

var progress = new DownloadFileProgress(
file, e.TotalBytesToReceive, progressedBytes, e.BytesReceived, e.ProgressPercentage);
FileDownloadProgressChanged?.Invoke(this, progress);
}
};
await wc.DownloadFileTaskAsync(file.Url, file.Path)
.ConfigureAwait(false);
}

internal void DownloadFileLimit(string url, string path)
Expand Down

0 comments on commit c59e3db

Please sign in to comment.