Skip to content

Commit

Permalink
为一些主要类添加注释
Browse files Browse the repository at this point in the history
  • Loading branch information
YangSpring114 committed Jul 9, 2024
1 parent 72be0cd commit f36e030
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 25 deletions.
16 changes: 12 additions & 4 deletions MinecraftLaunch/Components/Checker/ResourceChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,26 @@
namespace MinecraftLaunch.Components.Checker;

/// <summary>
/// Minecraft 游戏资源检查器
/// A checker for Minecraft game resources.
/// </summary>
/// <remarks>
/// 包含 Assets Libraries
/// This includes both Assets and Libraries.
/// </remarks>
public sealed class ResourceChecker(GameEntry entry) : IChecker {
private AssetsResolver AssetsResolver => new(entry);

private LibrariesResolver LibraryResolver => new(entry);

/// <summary>
/// Gets the collection of missing resources.
/// </summary>
public IReadOnlyCollection<IDownloadEntry> MissingResources { get; private set; }

/// <summary>
/// Checks the game resources asynchronously.
/// </summary>
/// <returns>
/// A ValueTask that represents the asynchronous operation. The task result contains a boolean value indicating whether all resources are present.
/// </returns>
public async ValueTask<bool> CheckAsync() {
var assetIndex = AssetsResolver.GetAssetIndexJson();
if (!assetIndex.Verify()) {
Expand All @@ -35,4 +43,4 @@ public async ValueTask<bool> CheckAsync() {

return !MissingResources.Any();
}
}
}
41 changes: 35 additions & 6 deletions MinecraftLaunch/Components/Fetcher/CurseForgeFetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,34 @@

namespace MinecraftLaunch.Components.Fetcher;

public sealed class CurseForgeFetcher(string apiKey) : IFetcher<IEnumerable<CurseForgeResourceEntry>> {
private readonly string _key = apiKey;
private readonly string _api = "https://api.curseforge.com/v1/mods";

/// <summary>
/// Fetches resources from CurseForge.
/// </summary>
public sealed class CurseForgeFetcher : IFetcher<IEnumerable<CurseForgeResourceEntry>> {
private const string BASE_API = "https://api.curseforge.com/v1/mods";

private readonly string _key;

/// <summary>
/// Initializes a new instance of the <see cref="CurseForgeFetcher"/> class.
/// </summary>
/// <param name="apiKey">The API key for CurseForge.</param>
public CurseForgeFetcher(string apiKey) {
_key = apiKey;
}

/// <summary>
/// Fetches the CurseForge resources synchronously.
/// </summary>
/// <returns>An enumerable collection of CurseForge resources.</returns>
public IEnumerable<CurseForgeResourceEntry> Fetch() {
return FetchAsync().GetAwaiter().GetResult();
}

/// <summary>
/// Fetches the CurseForge resources asynchronously.
/// </summary>
/// <returns>A ValueTask that represents the asynchronous operation. The task result contains an enumerable collection of CurseForge resources.</returns>
public async ValueTask<IEnumerable<CurseForgeResourceEntry>> FetchAsync() {
var result = new List<CurseForgeResourceEntry>();
var payload = new {
Expand All @@ -26,7 +46,7 @@ public async ValueTask<IEnumerable<CurseForgeResourceEntry>> FetchAsync() {
gameVersionTypeId = null as string
};
try {
using var responseMessage = await $"{_api}/featured"
using var responseMessage = await $"{BASE_API}/featured"
.WithHeader("x-api-key", _key)
.PostJsonAsync(payload);

Expand All @@ -44,13 +64,22 @@ public async ValueTask<IEnumerable<CurseForgeResourceEntry>> FetchAsync() {
return result;
}

/// <summary>
/// Searches for CurseForge resources asynchronously based on the provided search filter and other parameters.
/// </summary>
/// <param name="searchFilter">The search filter.</param>
/// <param name="classId">The class ID. Defaults to 6.</param>
/// <param name="category">The category. Defaults to -1.</param>
/// <param name="gameVersion">The game version. Defaults to null.</param>
/// <param name="modLoaderType">The mod loader type. Defaults to LoaderType.Any.</param>
/// <returns>A ValueTask that represents the asynchronous operation. The task result contains an enumerable collection of CurseForge resources.</returns>
public async ValueTask<IEnumerable<CurseForgeResourceEntry>> SearchResourcesAsync(
string searchFilter,
int classId = 6,
int category = -1,
string gameVersion = null,
LoaderType modLoaderType = LoaderType.Any) {
var stringBuilder = new StringBuilder(_api);
var stringBuilder = new StringBuilder(BASE_API);
stringBuilder.Append("/search?gameId=432");
stringBuilder.Append("&sortField=Featured");
stringBuilder.Append("&sortOrder=desc");
Expand Down
11 changes: 11 additions & 0 deletions MinecraftLaunch/Components/Fetcher/JavaFetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

namespace MinecraftLaunch.Components.Fetcher;

/// <summary>
/// Fetches Java installations on the system.
/// </summary>
public sealed class JavaFetcher : IFetcher<ImmutableArray<JavaEntry>> {
#region Fields
[SupportedOSPlatform(nameof(OSPlatform.OSX))]
Expand Down Expand Up @@ -66,10 +69,18 @@ public sealed class JavaFetcher : IFetcher<ImmutableArray<JavaEntry>> {

#endregion

/// <summary>
/// Fetches the Java installations synchronously.
/// </summary>
/// <returns>An immutable array of Java entries.</returns>
public ImmutableArray<JavaEntry> Fetch() {
return FetchAsync().GetAwaiter().GetResult();
}

/// <summary>
/// Fetches the Java installations asynchronously.
/// </summary>
/// <returns>A ValueTask that represents the asynchronous operation. The task result contains an immutable array of Java entries.</returns>
public async ValueTask<ImmutableArray<JavaEntry>> FetchAsync() {
return EnvironmentUtil.GetPlatformName() switch {
"windows" => FetchWindowJava(),
Expand Down
31 changes: 25 additions & 6 deletions MinecraftLaunch/Components/Fetcher/ModrinthFetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,44 @@
using MinecraftLaunch.Classes.Interfaces;
using MinecraftLaunch.Classes.Models.Download;

namespace MinecraftLaunch.Components.Fetcher;
namespace MinecraftLaunch.Components.Fetcher;

/// <summary>
/// Fetches resources from Modrinth.
/// </summary>
public sealed class ModrinthFetcher : IFetcher<IEnumerable<ModrinthResourceEntry>> {
private readonly string _api = "https://api.modrinth.com/v2/";

private const string BASE_API = "https://api.modrinth.com/v2/";

/// <summary>
/// Fetches the Modrinth resources synchronously.
/// </summary>
/// <returns>An enumerable collection of Modrinth resources.</returns>
public IEnumerable<ModrinthResourceEntry> Fetch() {
return FetchAsync().GetAwaiter().GetResult();
}

/// <summary>
/// Fetches the Modrinth resources asynchronously.
/// </summary>
/// <returns>A ValueTask that represents the asynchronous operation. The task result contains an enumerable collection of Modrinth resources.</returns>
public async ValueTask<IEnumerable<ModrinthResourceEntry>> FetchAsync() {
var jNode = (await $"{_api}search".GetStringAsync()).AsNode();
var jNode = (await $"{BASE_API}search".GetStringAsync()).AsNode();
return jNode?.Select("hits")?.GetEnumerable()
.Deserialize<IEnumerable<ModrinthResourceEntry>>(JsonConverterUtil.DefaultJsonOptions);
}


/// <summary>
/// Searches for Modrinth resources asynchronously based on the provided search filter and other parameters.
/// </summary>
/// <param name="searchFilter">The search filter.</param>
/// <param name="version">The version. Defaults to null.</param>
/// <param name="resourceType">The resource type. Defaults to ModrinthResourceType.Mod.</param>
/// <returns>A ValueTask that represents the asynchronous operation. The task result contains an enumerable collection of Modrinth resources.</returns>
public async ValueTask<IEnumerable<ModrinthResourceEntry>> SearchResourcesAsync(
string searchFilter,
string version = default,
ModrinthResourceType? resourceType = ModrinthResourceType.Mod) {
var stringBuilder = new StringBuilder(_api);
var stringBuilder = new StringBuilder(BASE_API);
stringBuilder.Append($"search?query={searchFilter}");

var facets = new List<string>();
Expand Down
36 changes: 27 additions & 9 deletions MinecraftLaunch/Components/Launcher/Launcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,43 @@
namespace MinecraftLaunch.Components.Launcher;

/// <summary>
/// 标准 Java版 Minecraft 启动器
/// Launcher for the Java version of Minecraft.
/// </summary>
public sealed class Launcher(IGameResolver resolver, LaunchConfig config) : ILauncher {
public sealed class Launcher : ILauncher {
private ArgumentsBuilder _argumentsBuilder;

public LaunchConfig LaunchConfig => config;
/// <summary>
/// Gets the launch configuration.
/// </summary>
public LaunchConfig LaunchConfig { get; }

public IGameResolver GameResolver => resolver;
public IGameResolver GameResolver { get; }

/// <summary>
/// Initializes a new instance of the <see cref="Launcher"/> class.
/// </summary>
/// <param name="resolver">The game resolver.</param>
/// <param name="config">The launch configuration.</param>
public Launcher(IGameResolver resolver, LaunchConfig config) {
GameResolver = resolver;
LaunchConfig = config;
}

/// <summary>
/// Launches the game asynchronously.
/// </summary>
/// <param name="id">The ID of the game to launch.</param>
/// <returns>A ValueTask that represents the asynchronous operation. The task result contains a <see cref="GameProcessWatcher"/>.</returns>
public async ValueTask<IGameProcessWatcher> LaunchAsync(string id) {
var gameEntry = GameResolver.GetGameEntity(id);
var versionPath = gameEntry.OfVersionDirectoryPath(config.IsEnableIndependencyCore);
_argumentsBuilder = new(gameEntry, config);
var gameEntry = GameResolver.GetGameEntity(id);
var versionPath = gameEntry.OfVersionDirectoryPath(LaunchConfig.IsEnableIndependencyCore);
_argumentsBuilder = new(gameEntry, LaunchConfig);

var arguments = _argumentsBuilder.Build();
var process = CreateProcess(arguments, versionPath);

LibrariesResolver librariesResolver = new(gameEntry);
await ExtractNatives(versionPath, librariesResolver);
await Launcher.ExtractNatives(versionPath, librariesResolver);
return new GameProcessWatcher(process, arguments);
}

Expand All @@ -46,7 +64,7 @@ private Process CreateProcess(IEnumerable<string> arguments, string versionPath)
};
}

private async Task ExtractNatives(string versionPath, LibrariesResolver librariesResolver) {
private static async Task ExtractNatives(string versionPath, LibrariesResolver librariesResolver) {
var libraries = librariesResolver.GetLibraries()
.Where(x => ((x as LibraryEntry)?.IsNative) != null)
.Select(x => x.Path)
Expand Down
1 change: 1 addition & 0 deletions MinecraftLaunch/MinecraftLaunch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<LangVersion>latest</LangVersion>
<Title>MinecraftLaunch</Title>
<Nullable>disable</Nullable>
<GenerateDocumentationFile>False</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit f36e030

Please sign in to comment.