Skip to content

Commit

Permalink
bug fix and improvements
Browse files Browse the repository at this point in the history
fix platform detect issue
optimize performance
remove unused class
  • Loading branch information
laolarou726 committed Dec 1, 2023
1 parent cb3db59 commit b8e8a16
Show file tree
Hide file tree
Showing 15 changed files with 76 additions and 121 deletions.
1 change: 0 additions & 1 deletion ProjBobcat/ProjBobcat/Class/Helper/FileHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public static FileType GetFileType(string path)
catch (IOException)
{
await Task.Delay(1000, token);
continue;
}
}

Expand Down
52 changes: 0 additions & 52 deletions ProjBobcat/ProjBobcat/Class/Helper/RandomHelper.cs

This file was deleted.

9 changes: 4 additions & 5 deletions ProjBobcat/ProjBobcat/Class/Model/GameConfigurationManager.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

Expand All @@ -22,7 +21,7 @@ public GameConfigurationManager(string path)
_configuration = GetConfigurationDictionary(content);
}

public GameConfigurationManager(IEnumerable<string> list)
public GameConfigurationManager(IReadOnlyCollection<string> list)
{
_configuration = GetConfigurationDictionary(list);
}
Expand Down Expand Up @@ -52,13 +51,13 @@ public async Task SaveAsync(string path)
await File.WriteAllTextAsync(path, sb.ToString());
}

public static Dictionary<string, string> GetConfigurationDictionary(IEnumerable<string>? lines)
public static Dictionary<string, string> GetConfigurationDictionary(IReadOnlyCollection<string>? lines)
{
var result = new Dictionary<string, string>();

if (!(lines?.Any() ?? false)) return result;
if ((lines?.Count ?? 0) == 0) return result;

foreach (var line in lines)
foreach (var line in lines!)
{
if (string.IsNullOrWhiteSpace(line)) continue;
if (!line.Contains(':')) continue;
Expand Down
2 changes: 1 addition & 1 deletion ProjBobcat/ProjBobcat/Class/Model/Version/Item/ListItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public int CompareTo(object? obj)

public bool IsNull()
{
return !this.Any();
return Count == 0;
}

public void Normalize()
Expand Down
2 changes: 1 addition & 1 deletion ProjBobcat/ProjBobcat/Class/Model/VersionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class VersionInfo
public required List<FileInfo> Libraries { get; set; }
public required List<NativeFileInfo> Natives { get; set; }
public Logging? Logging { get; init; }
public IEnumerable<string>? JvmArguments { get; set; }
public IReadOnlyList<string>? JvmArguments { get; set; }
public required IEnumerable<string> GameArguments { get; set; }
public IReadOnlyDictionary<string, string>? AvailableGameArguments { get; set; }

Expand Down
6 changes: 3 additions & 3 deletions ProjBobcat/ProjBobcat/Class/VersionLocatorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ public abstract class VersionLocatorBase(string rootPath) : LauncherParserBase(r

public abstract IEnumerable<VersionInfo> GetAllGames();

public abstract IEnumerable<string> ParseJvmArguments(IEnumerable<JsonElement> arguments);
public abstract IEnumerable<string> ParseJvmArguments(JsonElement[] arguments);
private protected abstract VersionInfo? ToVersion(string id);

public abstract (List<NativeFileInfo>, List<FileInfo>) GetNatives(IEnumerable<Library> libraries);
public abstract (List<NativeFileInfo>, List<FileInfo>) GetNatives(Library[] libraries);

private protected abstract (IEnumerable<string>, Dictionary<string, string>) ParseGameArguments(
(string, IEnumerable<JsonElement>) arguments);
(string, JsonElement[]) arguments);

public abstract RawVersionModel? ParseRawVersion(string id);
}
36 changes: 18 additions & 18 deletions ProjBobcat/ProjBobcat/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,30 @@ public static class Constants
{
public const string FallBackVersion = "0.0.0";

public static string WhereCommand => OperatingSystem.IsWindows() ? Windows.WhereCommand : Linux.WhereCommand;
public static string JavaExecutable => OperatingSystem.IsWindows() ? Windows.JavaExecutable : Linux.JavaExecutable;
public static string WhereCommand => OperatingSystem.IsWindows() ? Windows.WhereCommand : UnixKind.WhereCommand;
public static string JavaExecutable => OperatingSystem.IsWindows() ? Windows.JavaExecutable : UnixKind.JavaExecutable;

public static string JavaExecutableExtension => OperatingSystem.IsWindows()
? Windows.JavaExecutableExtension
: Linux.JavaExecutableExtension;
: UnixKind.JavaExecutableExtension;

public static string JavaExecutablePath => RuntimeInformation.RuntimeIdentifier switch
{
"windows" => Windows.JavaExecutablePath,
"linux" => Linux.JavaExecutablePath,
"osx" => Osx.JavaExecutablePath,
_ => throw new PlatformNotSupportedException("Unknown operating system.")
_ when RuntimeInformation.IsOSPlatform(OSPlatform.Windows) => Windows.JavaExecutablePath,
_ when RuntimeInformation.IsOSPlatform(OSPlatform.Linux) => UnixKind.JavaExecutablePath,
_ when RuntimeInformation.IsOSPlatform(OSPlatform.OSX) => UnixKind.MacOs.JavaExecutablePath,
var id => throw new PlatformNotSupportedException($"Unknown operating system: {id}")
};

public static string OsSymbol => RuntimeInformation.RuntimeIdentifier switch
{
"windows" => Windows.OsSymbol,
"linux" => Linux.OsSymbol,
"osx" => Osx.OsSymbol,
_ => throw new PlatformNotSupportedException("Unknown operating system.")
_ when RuntimeInformation.IsOSPlatform(OSPlatform.Windows) => Windows.OsSymbol,
_ when RuntimeInformation.IsOSPlatform(OSPlatform.Linux) => UnixKind.OsSymbol,
_ when RuntimeInformation.IsOSPlatform(OSPlatform.OSX) => UnixKind.MacOs.OsSymbol,
var id => throw new PlatformNotSupportedException($"Unknown operating system: {id}")
};

class Windows
static class Windows
{
public const string WhereCommand = "where";
public const string JavaExecutable = "javaw.exe";
Expand All @@ -39,18 +39,18 @@ class Windows
public const string OsSymbol = "windows";
}

class Linux
static class UnixKind
{
public const string WhereCommand = "whereis";
public const string JavaExecutable = "java";
public const string JavaExecutablePath = $"bin/{JavaExecutable}";
public const string JavaExecutableExtension = "*";
public const string OsSymbol = "linux";
}

class Osx : Linux
{
public new const string JavaExecutablePath = $"Contents/Home/bin/{JavaExecutable}";
public new const string OsSymbol = "osx";
public static class MacOs
{
public const string JavaExecutablePath = $"Contents/Home/bin/{JavaExecutable}";
public const string OsSymbol = "osx";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class DefaultResourceCompleter : IResourceCompleter
public int MaxDegreeOfParallelism { get; set; } = 1;
public int TotalRetry { get; set; } = 2;
public bool CheckFile { get; set; } = true;
public IEnumerable<IResourceInfoResolver>? ResourceInfoResolvers { get; set; }
public IReadOnlyList<IResourceInfoResolver>? ResourceInfoResolvers { get; set; }

public event EventHandler<GameResourceInfoResolveEventArgs> GameResourceInfoResolveStatus
{
Expand All @@ -65,7 +65,7 @@ public event EventHandler<DownloadFileCompletedEventArgs> DownloadFileCompletedE

public async Task<TaskResult<ResourceCompleterCheckResult?>> CheckAndDownloadTaskAsync()
{
if (!(ResourceInfoResolvers?.Any() ?? false))
if ((ResourceInfoResolvers?.Count ?? 0) == 0)
return new TaskResult<ResourceCompleterCheckResult?>(TaskResultStatus.Success, value: null);

_needToDownload = 0;
Expand Down Expand Up @@ -146,7 +146,10 @@ async Task ReceiveGameResourceTask(IAsyncEnumerable<IGameResource> asyncEnumerab
Interlocked.Add(ref _needToDownload, count);
}

var chunks = ResourceInfoResolvers.Chunk(MaxDegreeOfParallelism).ToImmutableArray();
var chunks =
ResourceInfoResolvers!
.Chunk(MaxDegreeOfParallelism)
.ToImmutableArray();
foreach (var chunk in chunks)
{
var tasks = new Task[chunk.Length];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,8 @@ public async Task<ForgeInstallResult> InstallForgeTaskAsync()

var outputs = new Dictionary<string, string>();

if (proc.Outputs?.Any() ?? false)
foreach (var (k, v) in proc.Outputs)
if ((proc.Outputs?.Count ?? 0) > 0)
foreach (var (k, v) in proc.Outputs!)
{
var resolvedKey = ResolveVariableRegex(k);
var resolvedValue = ResolveVariableRegex(v);
Expand Down Expand Up @@ -395,10 +395,7 @@ public async Task<ForgeInstallResult> InstallForgeTaskAsync()

_failedFiles.Clear();

var libs = ipModel.Libraries.ToList();
libs.AddRange(versionJsonModel.Libraries);

var resolvedLibs = VersionLocator.GetNatives(libs).Item2;
var resolvedLibs = VersionLocator.GetNatives([.. ipModel.Libraries, .. versionJsonModel.Libraries]).Item2;
var libDownloadInfo = new List<DownloadFile>();

foreach (var lib in resolvedLibs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public async Task InstallTaskAsync()
{
CheckSum = checkSum,
DownloadPath = downloadDir,
DownloadUri = file.Downloads.RandomSample()!,
DownloadUri = Random.Shared.GetItems(file.Downloads, 1)[0],
FileName = fileName,
FileSize = file.Size
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ public IEnumerable<string> ParseJvmHeadArguments()

var gameArgs = LaunchSettings.GameArguments ?? LaunchSettings.FallBackGameArguments;

if (gameArgs!.AdditionalJvmArguments?.Any() ?? false)
foreach (var jvmArg in gameArgs.AdditionalJvmArguments)
if ((gameArgs!.AdditionalJvmArguments?.Count ?? 0) > 0)
foreach (var jvmArg in gameArgs.AdditionalJvmArguments!)
yield return jvmArg;

if (string.IsNullOrEmpty(GameProfile?.JavaArgs))
Expand Down
Loading

0 comments on commit b8e8a16

Please sign in to comment.