Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaBs committed Jun 14, 2021
2 parents 9a98a91 + 863a1b2 commit 1ad4110
Show file tree
Hide file tree
Showing 31 changed files with 518 additions and 394 deletions.
2 changes: 1 addition & 1 deletion CmlLib/CmlLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netcoreapp3.1;net462;net5.0</TargetFrameworks>
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
<Version>3.2.0</Version>
<Version>3.3.0</Version>
<Description>Minecraft Launcher Library for .NET Core and .NET Framework
Support All versions, forge, optifine
see github to learn how to use
Expand Down
81 changes: 28 additions & 53 deletions CmlLib/Core/CMLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,35 +76,7 @@ public async Task<MVersion> GetVersionAsync(string versionname)
.ConfigureAwait(false);
return version;
}

public string CheckJRE()
{
pFileChanged.Report(
new DownloadFileChangedEventArgs(MFile.Runtime, "java", 1, 0));

var mjava = new MJava();
mjava.ProgressChanged += (sender, e) => pProgressChanged.Report(e);
var j = mjava.CheckJava();

pFileChanged.Report(
new DownloadFileChangedEventArgs(MFile.Runtime, "java", 1, 1));
return j;
}

public async Task<string> CheckJREAsync()
{
pFileChanged.Report(
new DownloadFileChangedEventArgs(MFile.Runtime, "java", 1, 0));

var mjava = new MJava();
var j = await mjava.CheckJavaAsync(pProgressChanged)
.ConfigureAwait(false);

pFileChanged.Report(
new DownloadFileChangedEventArgs(MFile.Runtime, "java", 1, 1));
return j;
}


public string CheckForge(string mcversion, string forgeversion, string java)
{
if (Versions == null)
Expand Down Expand Up @@ -193,31 +165,36 @@ public async Task CheckAndDownloadAsync(MVersion version)

public Process CreateProcess(string mcversion, string forgeversion, MLaunchOption option)
{
if (string.IsNullOrEmpty(option.JavaPath))
option.JavaPath = CheckJRE();

CheckAndDownload(GetVersion(mcversion));

var versionName = CheckForge(mcversion, forgeversion, option.JavaPath);

return CreateProcess(versionName, option);
}

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

[MethodTimer.Time]
public Process CreateProcess(string versionname, MLaunchOption option)
public Process CreateProcess(MVersion version, MLaunchOption option)
{
option.StartVersion = GetVersion(versionname);
option.StartVersion = version;

if (this.FileDownloader != null)
CheckAndDownload(option.StartVersion);

return CreateProcess(option);
}

[MethodTimer.Time]
public async Task<Process> CreateProcessAsync(string versionname, MLaunchOption option)
public async Task<Process> CreateProcessAsync(string versionName, MLaunchOption option)
{
var version = await GetVersionAsync(versionName).ConfigureAwait(false);
return await CreateProcessAsync(version, option).ConfigureAwait(false);
}

public async Task<Process> CreateProcessAsync(MVersion version, MLaunchOption option)
{
option.StartVersion = await GetVersionAsync(versionname).ConfigureAwait(false);
option.StartVersion = version;

if (this.FileDownloader != null)
await CheckAndDownloadAsync(option.StartVersion).ConfigureAwait(false);
Expand All @@ -227,41 +204,39 @@ public async Task<Process> CreateProcessAsync(string versionname, MLaunchOption

public Process CreateProcess(MLaunchOption option)
{
if (option.Path == null)
option.Path = MinecraftPath;

if (string.IsNullOrEmpty(option.JavaPath))
option.JavaPath = CheckJRE();

checkLaunchOption(option);
var launch = new MLaunch(option);
return launch.GetProcess();
}

public async Task<Process> CreateProcessAsync(MLaunchOption option)
{
if (option.Path == null)
option.Path = MinecraftPath;

if (string.IsNullOrEmpty(option.JavaPath))
option.JavaPath = await CheckJREAsync().ConfigureAwait(false);

checkLaunchOption(option);
var launch = new MLaunch(option);
return await Task.Run(launch.GetProcess).ConfigureAwait(false);
}

public Process Launch(string versionname, MLaunchOption option)
public Process Launch(string versionName, MLaunchOption option)
{
Process process = CreateProcess(versionname, option);
Process process = CreateProcess(versionName, option);
process.Start();
return process;
}

public async Task<Process> LaunchAsync(string versionname, MLaunchOption option)
public async Task<Process> LaunchAsync(string versionName, MLaunchOption option)
{
Process process = await CreateProcessAsync(versionname, option)
Process process = await CreateProcessAsync(versionName, option)
.ConfigureAwait(false);
process.Start();
return process;
}

private void checkLaunchOption(MLaunchOption option)
{
if (option.Path == null)
option.Path = MinecraftPath;
if (!string.IsNullOrEmpty(option.JavaPath) && option.StartVersion != null)
option.StartVersion.JavaBinaryPath = option.JavaPath;
}
}
}
4 changes: 2 additions & 2 deletions CmlLib/Core/Downloader/AsyncParallelDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public AsyncParallelDownloader(int parallelism)
}

fileProgress?.Report(
new DownloadFileChangedEventArgs(files[0].Type, null, files.Length, 0));
new DownloadFileChangedEventArgs(files[0].Type, true, null, files.Length, 0));
await ForEachAsyncSemaphore(files, MaxThread, doDownload).ConfigureAwait(false);

isRunning = false;
Expand Down Expand Up @@ -127,7 +127,7 @@ private async Task doDownload(DownloadFile file, int retry)

Interlocked.Increment(ref progressedFiles);
pChangeFile?.Report(
new DownloadFileChangedEventArgs(file.Type, file.Name, totalFiles, progressedFiles));
new DownloadFileChangedEventArgs(file.Type, true, file.Name, totalFiles, progressedFiles));
}
catch (Exception ex)
{
Expand Down
4 changes: 3 additions & 1 deletion CmlLib/Core/Downloader/DownloadFileChangedEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ public enum MFile { Runtime, Library, Resource, Minecraft, Others }

public class DownloadFileChangedEventArgs : EventArgs
{
public DownloadFileChangedEventArgs(MFile kind, string? filename, int total, int progressed)
public DownloadFileChangedEventArgs(MFile kind, bool isDownloader, string? filename, int total, int progressed)
{
FileKind = kind;
FileName = filename;
TotalFileCount = total;
ProgressedFileCount = progressed;
this.IsDownloader = isDownloader;
}

public MFile FileKind { get; private set; }
public string? FileName { get; private set; }
public int TotalFileCount { get; private set; }
public int ProgressedFileCount { get; private set; }
public bool IsDownloader { get; private set; }
}
}
2 changes: 1 addition & 1 deletion CmlLib/Core/Downloader/ParallelDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private bool doDownload(DownloadFile file, int failedCount)

private void fireDownloadFileChangedEvent(MFile file, string name, int totalFiles, int progressedFiles)
{
var e = new DownloadFileChangedEventArgs(file, name, totalFiles, progressedFiles);
var e = new DownloadFileChangedEventArgs(file, true, name, totalFiles, progressedFiles);
fireDownloadFileChangedEvent(e);
}

Expand Down
4 changes: 2 additions & 2 deletions CmlLib/Core/Downloader/SequenceDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class SequenceDownloader : IDownloader
downloader.FileDownloadProgressChanged += Downloader_FileDownloadProgressChanged;

fileProgress?.Report(
new DownloadFileChangedEventArgs(files[0].Type, null, files.Length, 0));
new DownloadFileChangedEventArgs(files[0].Type, true, null, files.Length, 0));

for (int i = 0; i < files.Length; i++)
{
Expand All @@ -47,7 +47,7 @@ public class SequenceDownloader : IDownloader
}

fileProgress?.Report(
new DownloadFileChangedEventArgs(file.Type, file.Name, files.Length, i));
new DownloadFileChangedEventArgs(file.Type, true, file.Name, files.Length, i));
}
catch (Exception ex)
{
Expand Down
2 changes: 1 addition & 1 deletion CmlLib/Core/Files/AssetChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private void checkIndex(MinecraftPath path, MVersion version)

if (progressed % 50 == 0) // prevent ui freezing
progress?.Report(
new DownloadFileChangedEventArgs(MFile.Resource, "", total, progressed));
new DownloadFileChangedEventArgs(MFile.Resource, false, "", total, progressed));
}

return downloadRequiredFiles.Distinct().ToArray(); // 10ms
Expand Down
8 changes: 4 additions & 4 deletions CmlLib/Core/Files/ClientChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public sealed class ClientChecker : IFileChecker
public DownloadFile[]? CheckFiles(MinecraftPath path, MVersion version,
IProgress<DownloadFileChangedEventArgs>? progress)
{
progress?.Report(new DownloadFileChangedEventArgs(MFile.Minecraft, version.Jar, 1, 0));
progress?.Report(new DownloadFileChangedEventArgs(MFile.Minecraft, false, version.Jar, 1, 0));
DownloadFile? result = checkClientFile(path, version);
progress?.Report(new DownloadFileChangedEventArgs(MFile.Minecraft, version.Jar, 1, 1));
progress?.Report(new DownloadFileChangedEventArgs(MFile.Minecraft, false, version.Jar, 1, 1));

if (result == null)
return null;
Expand All @@ -26,10 +26,10 @@ public sealed class ClientChecker : IFileChecker
public async Task<DownloadFile[]?> CheckFilesTaskAsync(MinecraftPath path, MVersion version,
IProgress<DownloadFileChangedEventArgs>? progress)
{
progress?.Report(new DownloadFileChangedEventArgs(MFile.Minecraft, version.Jar, 1, 0));
progress?.Report(new DownloadFileChangedEventArgs(MFile.Minecraft, false, version.Jar, 1, 0));
DownloadFile? result = await Task.Run(() => checkClientFile(path, version))
.ConfigureAwait(false);
progress?.Report(new DownloadFileChangedEventArgs(MFile.Minecraft, version.Jar, 1, 1));
progress?.Report(new DownloadFileChangedEventArgs(MFile.Minecraft, false, version.Jar, 1, 1));

if (result == null)
return null;
Expand Down
31 changes: 26 additions & 5 deletions CmlLib/Core/Files/FileCheckerCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,35 @@ public class FileCheckerCollection : IEnumerable<IFileChecker>
}
}

private JavaChecker? java;

public JavaChecker? JavaFileChecker
{
get => java;
set
{
if (java != null)
checkers.Remove(java);

java = value;

if (java != null)
checkers.Add(java);
}
}

public FileCheckerCollection()
{
checkers = new List<IFileChecker>(3);

library = new LibraryChecker();
asset = new AssetChecker();
client = new ClientChecker();

java = new JavaChecker();

checkers.AddRange(new IFileChecker[]
{
library, asset, client
library, asset, client, java
});
}

Expand All @@ -78,11 +96,12 @@ public void Add(IFileChecker item)
checkers.Add(item);
}

public void AddRange(IEnumerable<IFileChecker> items)
public void AddRange(IEnumerable<IFileChecker?> items)
{
foreach (IFileChecker item in items)
foreach (IFileChecker? item in items)
{
Add(item);
if (item != null)
Add(item);
}
}

Expand Down Expand Up @@ -115,6 +134,8 @@ private void CheckArgument(IFileChecker item)
throw new ArgumentException($"Set {nameof(AssetFileChecker)} property.");
if (item is ClientChecker)
throw new ArgumentException($"Set {nameof(ClientFileChecker)} property.");
if (item is JavaChecker)
throw new ArgumentException($"Set {nameof(JavaFileChecker)} property.");
}

public IEnumerator<IFileChecker> GetEnumerator()
Expand Down
Loading

0 comments on commit 1ad4110

Please sign in to comment.