Skip to content

Commit

Permalink
feat: fully rewrite multiple core components
Browse files Browse the repository at this point in the history
  • Loading branch information
laolarou726 committed Jan 24, 2025
1 parent 82f1467 commit 7dbfeac
Show file tree
Hide file tree
Showing 24 changed files with 591 additions and 498 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,45 @@ static NativeReplaceHelper()
NativeReplaceModel = model;
}

static string GetNativeKey()
static string GetNativeKey(OSPlatform platform, Architecture architecture)
{
var platform = NativeReplaceModel switch
var platformStr = platform switch
{
_ when RuntimeInformation.IsOSPlatform(OSPlatform.Windows) => "windows",
_ when RuntimeInformation.IsOSPlatform(OSPlatform.Linux) => "linux",
_ when RuntimeInformation.IsOSPlatform(OSPlatform.OSX) => "osx",
_ when RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD) => "freebsd",
_ when platform == OSPlatform.Windows => "windows",
_ when platform == OSPlatform.Linux => "linux",
_ when platform == OSPlatform.OSX => "osx",
_ when platform == OSPlatform.FreeBSD => "freebsd",
_ => string.Empty
};

var arch = NativeReplaceModel switch
var archStr = architecture switch
{
_ when RuntimeInformation.OSArchitecture == Architecture.X64 => "x86_64",
_ when RuntimeInformation.OSArchitecture == Architecture.X86 => "x86",
_ when RuntimeInformation.OSArchitecture == Architecture.Arm64 => "arm64",
_ when RuntimeInformation.OSArchitecture == Architecture.Arm => "arm32",
_ when RuntimeInformation.OSArchitecture == Architecture.LoongArch64 => "loongarch64",
Architecture.X64 => "x86_64",
Architecture.X86 => "x86",
Architecture.Arm64 => "arm64",
Architecture.Arm => "arm32",
Architecture.LoongArch64 => "loongarch64",
_ => string.Empty
};

return $"{platform}-{arch}";
return $"{platformStr}-{archStr}";
}

public static List<Library> Replace(
List<RawVersionModel> versions,
List<Library> libs,
NativeReplacementPolicy policy,
OSPlatform? javaPlatform,
Architecture? javaArch,
bool useSystemGlfwOnLinux,
bool useSystemOpenAlOnLinux)
{
if (policy == NativeReplacementPolicy.Disabled) return libs;

var replaceKey = GetNativeKey();
javaPlatform ??= SystemInfoHelper.GetOsPlatform();
javaArch ??= RuntimeInformation.OSArchitecture;

var replaceKey = GetNativeKey(javaPlatform.Value, javaArch.Value);
var replaceDic = replaceKey switch
{
"windows-x86_64" => NativeReplaceModel.WindowsX64,
Expand Down Expand Up @@ -100,7 +105,7 @@ public static List<Library> Replace(

var osCheckFlag = OperatingSystem.IsWindows() || OperatingSystem.IsMacOS() || OperatingSystem.IsLinux();

if (RuntimeInformation.ProcessArchitecture == Architecture.X86 && osCheckFlag)
if (javaArch.Value == Architecture.X86 && osCheckFlag)
return libs;

var isNotLinux = OperatingSystem.IsWindows() || OperatingSystem.IsMacOS();
Expand All @@ -113,7 +118,7 @@ public static List<Library> Replace(

if (versionsArr is { Length: >= 2 }) minor = int.TryParse(versionsArr[1], out var outMinor) ? outMinor : -1;

if (RuntimeInformation.ProcessArchitecture == Architecture.Arm64 &&
if (javaArch.Value == Architecture.Arm64 &&
isNotLinux &&
minor is -1 or >= 19)
return libs;
Expand Down
9 changes: 9 additions & 0 deletions ProjBobcat/ProjBobcat/Class/Helper/SystemInfoHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ namespace ProjBobcat.Class.Helper;
/// </summary>
public static class SystemInfoHelper
{
public static OSPlatform GetOsPlatform()
{
if (OperatingSystem.IsWindows()) return OSPlatform.Windows;
if (OperatingSystem.IsMacOS()) return OSPlatform.OSX;
if (OperatingSystem.IsLinux()) return OSPlatform.Linux;

throw new PlatformNotSupportedException();
}

public static string GetSystemArch()
{
return RuntimeInformation.OSArchitecture switch
Expand Down
52 changes: 2 additions & 50 deletions ProjBobcat/ProjBobcat/Class/LaunchArgumentParserBase.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System.IO;
using ProjBobcat.Class.Helper;
using ProjBobcat.Class.Model;
using ProjBobcat.Class.Model.Auth;
using ProjBobcat.Class.Model.LauncherProfile;
using ProjBobcat.Interface;
using ProjBobcat.Interface;

namespace ProjBobcat.Class;

Expand All @@ -12,17 +7,10 @@ namespace ProjBobcat.Class;
/// </summary>
public abstract class LaunchArgumentParserBase(
string rootPath,
LaunchSettings launchSettings,
ILauncherProfileParser launcherProfileParser,
IVersionLocator versionLocator,
AuthResultBase authResult)
IVersionLocator versionLocator)
: LauncherParserBase(rootPath)
{
/// <summary>
/// 启动设置
/// </summary>
protected LaunchSettings LaunchSettings { get; init; } = launchSettings;

/// <summary>
/// launcher_profile 解析器
/// </summary>
Expand All @@ -32,40 +20,4 @@ public abstract class LaunchArgumentParserBase(
/// 版本定位器
/// </summary>
protected IVersionLocator VersionLocator { get; init; } = versionLocator;

/// <summary>
/// 账户验证结果
/// </summary>
protected AuthResultBase AuthResult { get; init; } = authResult;

/// <summary>
/// 游戏档案
/// </summary>
protected GameProfileModel? GameProfile { get; init; }

/// <summary>
/// Native 根目录
/// </summary>
public virtual string NativeRoot =>
Path.Combine(this.RootPath, GamePathHelper.GetNativeRoot(this.LaunchSettings.Version));

/// <summary>
/// Asset 根目录
/// </summary>
public virtual string AssetRoot => Path.Combine(this.RootPath, GamePathHelper.GetAssetsRoot());

/// <summary>
/// Class 路径
/// </summary>
protected abstract string ClassPath { get; init; }

/// <summary>
/// 版本信息
/// </summary>
protected abstract VersionInfo VersionInfo { get; init; }

/// <summary>
/// 上一次的验证结果
/// </summary>
protected AuthResultBase? LastAuthResult { get; init; }
}
5 changes: 3 additions & 2 deletions ProjBobcat/ProjBobcat/Class/LaunchWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ public void Do()
if (this.Process == null) return;
if (this.Process.ProcessName != "Minecraft.Windows")
{
this.Process.BeginOutputReadLine();
this.Process.OutputDataReceived += this.ProcessOnOutputDataReceived;
this.Process.BeginErrorReadLine();
this.Process.ErrorDataReceived += this.ProcessOnErrorDataReceived;

this.Process.BeginOutputReadLine();
this.Process.BeginErrorReadLine();
}

this.Process.Exited += this.ProcessOnExited;
Expand Down
7 changes: 7 additions & 0 deletions ProjBobcat/ProjBobcat/Class/Model/GameBrokenReason.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace ProjBobcat.Class.Model;

public enum GameBrokenReason
{
ParentVersionNotFound,
GameJsonCorrupted
}
10 changes: 10 additions & 0 deletions ProjBobcat/ProjBobcat/Class/Model/JavaRuntimeInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Runtime.InteropServices;

namespace ProjBobcat.Class.Model;

public record JavaRuntimeInfo(
string JavaPath,
OSPlatform JavaPlatform,
Architecture JavaArch,
bool UseSystemGlfwOnLinux,
bool UseSystemOpenAlOnLinux);
11 changes: 6 additions & 5 deletions ProjBobcat/ProjBobcat/Class/Model/LaunchSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ namespace ProjBobcat.Class.Model;

public class GameArguments
{
/// <summary>
/// Java executable file
/// </summary>
public required string JavaExecutable { get; set; }

public required JavaRuntimeInfo? Java { get; set; }
public uint MinMemory { get; set; }
public required uint MaxMemory { get; set; }
public ResolutionModel? Resolution { get; set; }
Expand Down Expand Up @@ -47,6 +43,10 @@ public class LaunchSettings
/// </summary>
public required string Version { get; init; }

public bool EnableXmlLoggingOutput { get; init; }

public required NativeReplacementPolicy NativeReplacementPolicy { get; init; }

/// <summary>
/// 游戏窗口标题
/// </summary>
Expand All @@ -55,6 +55,7 @@ public class LaunchSettings
public required IVersionLocator VersionLocator { get; init; }

public required IAuthenticator Authenticator { get; init; }

public ProfileInfoModel? SelectedProfile { get; init; }

public bool VersionInsulation { get; init; }
Expand Down
43 changes: 31 additions & 12 deletions ProjBobcat/ProjBobcat/Class/Model/VersionInfo.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
using System.Collections.Generic;
using ProjBobcat.Interface;
using System.Collections.Generic;

namespace ProjBobcat.Class.Model;

public class VersionInfo
public class BrokenVersionInfo(string id) : IVersionInfo
{
public string Name { get; init; } = id;

public string DirName { get; init; } = id;

public required GameBrokenReason BrokenReason { get; init; }
}

public class VersionInfo : IVersionInfo
{
/// <summary>
/// 为启动器引用准备的带有tag的名称。
Expand All @@ -14,7 +24,7 @@ public class VersionInfo
/// 该版本的真实id,例如1.14-forge-xxx
/// The real id of this version, like 1.14-forge-xxx
/// </summary>
public required string Id { get; set; }
public required string Id { get; init; }

public required string DirName { get; init; }

Expand All @@ -24,19 +34,28 @@ public class VersionInfo

public JavaVersionModel? JavaVersion { get; set; }

public required string MainClass { get; set; }
public string? Assets { get; init; }
public Asset? AssetInfo { get; set; }
public required List<FileInfo> Libraries { get; set; }
public required List<NativeFileInfo> Natives { get; set; }
public Logging? Logging { get; init; }
public IReadOnlyList<string>? JvmArguments { get; set; }
public required IEnumerable<string> GameArguments { get; set; }
public IReadOnlyDictionary<string, string>? AvailableGameArguments { get; set; }

public RawVersionModel? RawVersion { get; init; }

public IReadOnlyList<RawVersionModel>? InheritsVersions { get; init; }

/// <summary>
/// 在递归式继承中最古老的版本(递归终点)。
/// The oldest version inherited (recursive).
/// </summary>
public string? RootVersion { get; set; }
}
}

public record ResolvedGameVersion(
string? RootVersion,
string DirName,
string MainClass,
string? Assets,
Asset? AssetInfo,
Logging? Logging,
IReadOnlyList<FileInfo> Libraries,
IReadOnlyList<NativeFileInfo> Natives,
IReadOnlyList<string>? JvmArguments,
IReadOnlyList<string>? GameArguments,
IReadOnlyDictionary<string, string>? AvailableGameArguments);
15 changes: 8 additions & 7 deletions ProjBobcat/ProjBobcat/Class/VersionLocatorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@

namespace ProjBobcat.Class;

public abstract class VersionLocatorBase(string rootPath) : LauncherParserBase(rootPath), IVersionLocator
public abstract class VersionLocatorBase : IVersionLocator
{
public ILauncherProfileParser? LauncherProfileParser { get; init; }
public ILauncherAccountParser? LauncherAccountParser { get; init; }

public abstract VersionInfo? GetGame(string id);
public abstract IVersionInfo GetGame(string id);

public abstract IEnumerable<VersionInfo> GetAllGames();
public abstract ResolvedGameVersion? ResolveGame(
IVersionInfo rawVersionInfo,
NativeReplacementPolicy nativeReplacementPolicy,
JavaRuntimeInfo? javaRuntimeInfo);

public abstract IEnumerable<IVersionInfo> GetAllGames();

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

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

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

public abstract RawVersionModel? ParseRawVersion(string id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ namespace ProjBobcat.DefaultComponent.Authenticator;

#region Temp Models

record McReqModel(string identityToken);
public record CacheTokenProviderResult(
bool IsCredentialValid,
bool IsAuthResultValid,
AuthResultBase? AuthResult,
GraphAuthResultModel? CacheAuthResult);
record McReqModel([property: JsonPropertyName("identityToken")] string IdentityToken);

[JsonSerializable(typeof(McReqModel))]
partial class McReqModelContext : JsonSerializerContext;
Expand Down Expand Up @@ -65,7 +70,7 @@ public MicrosoftAuthenticator()
/// </summary>
public Guid? ProfileId { get; set; }

public Func<Task<(bool, GraphAuthResultModel?)>>? CacheTokenProvider { get; init; }
public Func<MicrosoftAuthenticator, Task<CacheTokenProviderResult>>? CacheTokenProvider { get; init; }
public required ILauncherAccountParser LauncherAccountParser { get; init; }

public AuthResultBase Auth(bool userField = false)
Expand All @@ -87,9 +92,12 @@ public async Task<AuthResultBase> AuthTaskAsync(bool userField = false)
}
};

var (isCredentialValid, cacheAuthResult) = await this.CacheTokenProvider();
var cacheTokenResult = await this.CacheTokenProvider(this);

if (!isCredentialValid || cacheAuthResult == null)
if (cacheTokenResult is { IsAuthResultValid: true, AuthResult: { Error: null, AuthStatus: AuthStatus.Succeeded } })
return cacheTokenResult.AuthResult;

if (!cacheTokenResult.IsCredentialValid || cacheTokenResult.CacheAuthResult == null)
return new MicrosoftAuthResult
{
AuthStatus = AuthStatus.Failed,
Expand All @@ -101,10 +109,10 @@ public async Task<AuthResultBase> AuthTaskAsync(bool userField = false)
}
};

var accessToken = cacheAuthResult.AccessToken;
var refreshToken = cacheAuthResult.RefreshToken;
var idToken = cacheAuthResult.IdToken;
var expiresIn = cacheAuthResult.ExpiresIn;
var accessToken = cacheTokenResult.CacheAuthResult.AccessToken;
var refreshToken = cacheTokenResult.CacheAuthResult.RefreshToken;
var idToken = cacheTokenResult.CacheAuthResult.IdToken;
var expiresIn = cacheTokenResult.CacheAuthResult.ExpiresIn;

#region STAGE 1

Expand Down Expand Up @@ -368,8 +376,8 @@ await profileResRes.Content.ReadFromJsonAsync(MojangErrorResponseModelContext.De
(x.Value.MinecraftProfile?.Id.Equals(ProfileId?.ToString("N"), StringComparison.OrdinalIgnoreCase) ?? false) &&
x.Value.Type.Equals("XBox", StringComparison.OrdinalIgnoreCase));

if (value == default)
return default;
if (value == null)
return null;

var sP = new ProfileInfoModel
{
Expand Down
Loading

0 comments on commit 7dbfeac

Please sign in to comment.