Skip to content

Commit

Permalink
improve compatibility with other launcher
Browse files Browse the repository at this point in the history
  • Loading branch information
laolarou726 committed Mar 5, 2024
1 parent 94e847e commit 6d8b8e7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 40 deletions.
77 changes: 42 additions & 35 deletions ProjBobcat/ProjBobcat/Class/Helper/GameResourcesResolveHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,52 +56,59 @@ public static class GameResourcesResolveHelper
bool isEnabled,
CancellationToken ct)
{
List<GameModInfoModel>? model = null;
try
{
List<GameModInfoModel>? model = null;

await using var stream = entry.OpenEntryStream();
var doc = await JsonDocument.ParseAsync(stream, cancellationToken: ct);
await using var stream = entry.OpenEntryStream();
var doc = await JsonDocument.ParseAsync(stream, cancellationToken: ct);

switch (doc.RootElement.ValueKind)
{
case JsonValueKind.Object:
var val = doc.RootElement.Deserialize(GameModInfoModelContext.Default.GameModInfoModel);
switch (doc.RootElement.ValueKind)
{
case JsonValueKind.Object:
var val = doc.RootElement.Deserialize(GameModInfoModelContext.Default.GameModInfoModel);

if (val != null) model = [val];
if (val != null) model = [val];

break;
case JsonValueKind.Array:
model = doc.RootElement.Deserialize(GameModInfoModelContext.Default.ListGameModInfoModel) ?? [];
break;
}
break;
case JsonValueKind.Array:
model = doc.RootElement.Deserialize(GameModInfoModelContext.Default.ListGameModInfoModel) ?? [];
break;
}

if (model == null || model.Count == 0)
return null;
if (model == null || model.Count == 0)
return null;

var authors = new HashSet<string>();
foreach (var author in model.Where(m => m.AuthorList != null).SelectMany(m => m.AuthorList!))
authors.Add(author);
var authors = new HashSet<string>();
foreach (var author in model.Where(m => m.AuthorList != null).SelectMany(m => m.AuthorList!))
authors.Add(author);

var baseMod = model.FirstOrDefault(m => string.IsNullOrEmpty(m.Parent));
var baseMod = model.FirstOrDefault(m => string.IsNullOrEmpty(m.Parent));

if (baseMod == null)
{
baseMod = model.First();
model.RemoveAt(0);
}
else
{
model.Remove(baseMod);
}
if (baseMod == null)
{
baseMod = model.First();
model.RemoveAt(0);
}
else
{
model.Remove(baseMod);
}

var authorStr = string.Join(',', authors);
var authorResult = string.IsNullOrEmpty(authorStr) ? null : authorStr;
var modList = model.Where(m => !string.IsNullOrEmpty(m.Name)).Select(m => m.Name!).ToImmutableList();
var titleResult = string.IsNullOrEmpty(baseMod.Name) ? Path.GetFileName(file) : baseMod.Name;
var authorStr = string.Join(',', authors);
var authorResult = string.IsNullOrEmpty(authorStr) ? null : authorStr;
var modList = model.Where(m => !string.IsNullOrEmpty(m.Name)).Select(m => m.Name!).ToImmutableList();
var titleResult = string.IsNullOrEmpty(baseMod.Name) ? Path.GetFileName(file) : baseMod.Name;

var displayModel = new GameModResolvedInfo(authorResult, file, modList, titleResult, baseMod.Version,
"Forge *", isEnabled);
var displayModel = new GameModResolvedInfo(authorResult, file, modList, titleResult, baseMod.Version,
"Forge *", isEnabled);

return displayModel;
return displayModel;
}
catch (Exception)
{
return null;
}
}

static async Task<GameModResolvedInfo> GetFabricModInfo(
Expand Down
16 changes: 11 additions & 5 deletions ProjBobcat/ProjBobcat/Class/Helper/GameVersionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static partial class GameVersionHelper
TryGetMcVersionByFabric(version) ??
TryGetMcVersionByOptifine(version) ??
TryGetMcVersionByInheritFrom(version) ??
TryGetMcVersionByClientVersion(version) ??
TryGetMcVersionById(version);

if (string.IsNullOrEmpty(mcVersion)) continue;
Expand All @@ -31,17 +32,22 @@ public static partial class GameVersionHelper
return null;
}

private static string TryGetMcVersionById(RawVersionModel version)
static string TryGetMcVersionById(RawVersionModel version)
{
return version.Id;
}

private static string? TryGetMcVersionByInheritFrom(RawVersionModel version)
static string? TryGetMcVersionByClientVersion(RawVersionModel version)
{
return version.ClientVersion;
}

static string? TryGetMcVersionByInheritFrom(RawVersionModel version)
{
return version.InheritsFrom;
}

private static string? TryGetMcVersionByForgeGameArgs(RawVersionModel version)
static string? TryGetMcVersionByForgeGameArgs(RawVersionModel version)
{
var gameArgs = version.Arguments?.Game?
.Where(arg => arg.ValueKind == JsonValueKind.String)
Expand All @@ -61,7 +67,7 @@ private static string TryGetMcVersionById(RawVersionModel version)
return mcVersion;
}

private static string? TryGetMcVersionByFabric(RawVersionModel version)
static string? TryGetMcVersionByFabric(RawVersionModel version)
{
const string fabricLibPrefix = "net.fabricmc:intermediary:";

Expand All @@ -73,7 +79,7 @@ private static string TryGetMcVersionById(RawVersionModel version)
return mcVersion;
}

private static string? TryGetMcVersionByOptifine(RawVersionModel version)
static string? TryGetMcVersionByOptifine(RawVersionModel version)
{
const string optifineLibPrefix = "optifine:OptiFine:";

Expand Down
6 changes: 6 additions & 0 deletions ProjBobcat/ProjBobcat/Class/Model/RawVersionModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ public class RawVersionModel
[JsonPropertyName("id")]
public required string Id { get; set; }

/// <summary>
/// ClientVersion(Reserved field for other third-party launchers)
/// </summary>
[JsonPropertyName("clientVersion")]
public string? ClientVersion { get; set; }

[JsonPropertyName("javaVersion")] public JavaVersionModel? JavaVersion { get; set; }

/// <summary>
Expand Down

0 comments on commit 6d8b8e7

Please sign in to comment.