Skip to content

Commit

Permalink
fix NRE for third party auth
Browse files Browse the repository at this point in the history
  • Loading branch information
laolarou726 committed Jul 16, 2023
1 parent 8190396 commit 45129cc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
38 changes: 25 additions & 13 deletions ProjBobcat/ProjBobcat/Class/Helper/GameResourcesResolveHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static async IAsyncEnumerable<GameModResolvedInfo> ResolveModListAsync(IE
var infoTable = arr.Children.First();

var title = infoTable.HasKey("modId")
? infoTable["modId"]?.AsString
? (infoTable["modId"]?.AsString ?? "-")
: Path.GetFileName(file);
var author = infoTable.HasKey("authors")
? infoTable["authors"]?.AsString
Expand Down Expand Up @@ -128,18 +128,30 @@ public static async IAsyncEnumerable<GameModResolvedInfo> ResolveModListAsync(IE

async Task<GameModResolvedInfo> GetFabricModInfo(IArchiveEntry entry)
{
await using var stream = entry.OpenEntryStream();
var tempModel = await JsonSerializer.DeserializeAsync(stream,
FabricModInfoModelContext.Default.FabricModInfoModel, ct);

var author = tempModel?.Authors?.Any() ?? false
? string.Join(',', tempModel.Authors)
: null;
var modList = tempModel?.Depends?.Select(d => d.Key)?.ToImmutableList();
var titleResult = string.IsNullOrEmpty(tempModel?.Id) ? Path.GetFileName(file) : tempModel.Id;
var versionResult = string.IsNullOrEmpty(tempModel?.Version) ? null : tempModel.Version;

return new GameModResolvedInfo(author, file, modList, titleResult, versionResult, "Fabric", isEnabled);
try
{
await using var stream = entry.OpenEntryStream();
var tempModel = await JsonSerializer.DeserializeAsync(stream,
FabricModInfoModelContext.Default.FabricModInfoModel, ct);

var author = tempModel?.Authors?.Any() ?? false
? string.Join(',', tempModel.Authors)
: null;
var modList = tempModel?.Depends?.Select(d => d.Key)?.ToImmutableList();
var titleResult = string.IsNullOrEmpty(tempModel?.Id) ? Path.GetFileName(file) : tempModel.Id;
var versionResult = string.IsNullOrEmpty(tempModel?.Version) ? null : tempModel.Version;

return new GameModResolvedInfo(author, file, modList, titleResult, versionResult, "Fabric", isEnabled);
}
catch (JsonException e)
{
var errorList = new[]
{
"[!] 数据包 JSON 异常",
e.Message
};
return new GameModResolvedInfo(null, file, errorList.ToImmutableList(), Path.GetFileName(file), null, "Fabric", isEnabled);
}
}

GameModResolvedInfo? result = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class AuthResponseModel

[JsonPropertyName("selectedProfile")] public ProfileInfoModel? SelectedProfile { get; set; }

[JsonPropertyName("user")] public UserInfoModel User { get; set; }
[JsonPropertyName("user")] public UserInfoModel? User { get; set; }
}

[JsonSerializable(typeof(AuthResponseModel))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public async Task<AuthResultBase> AuthTaskAsync(bool userField = false)
Legacy = false,
LocalId = rUuid,
Persistent = true,
RemoteId = result.User.UUID.ToString(),
RemoteId = result.User?.UUID.ToString() ?? new Guid().ToString(),
Type = "Mojang",
UserProperites = result.User?.Properties?.ToAuthProperties(profiles).ToArray() ??
Array.Empty<AuthPropertyModel>(),
Expand Down Expand Up @@ -361,7 +361,7 @@ public async Task<AuthResultBase> AuthRefreshTaskAsync(AuthResponseModel respons
Legacy = false,
LocalId = rUuid,
Persistent = true,
RemoteId = authResponse.User.UUID.ToString(),
RemoteId = authResponse.User?.UUID.ToString() ?? new Guid().ToString(),
Type = "Mojang",
UserProperites = authResponse.User?.Properties?.ToAuthProperties(profiles).ToArray() ??
Array.Empty<AuthPropertyModel>(),
Expand Down

0 comments on commit 45129cc

Please sign in to comment.