mirror of
https://github.com/par274/sharpemu.git
synced 2026-07-25 04:08:40 +08:00
Harden param.json metadata parsing (#134)
Co-authored-by: Dafenx <196083014+Dafenxz0@users.noreply.github.com>
This commit is contained in:
@@ -45,7 +45,13 @@ public static class Ps5ParamJsonReader
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using var doc = JsonDocument.Parse(data);
|
ReadOnlyMemory<byte> json = data;
|
||||||
|
if (json.Span.StartsWith("\uFEFF"u8))
|
||||||
|
{
|
||||||
|
json = json[3..];
|
||||||
|
}
|
||||||
|
|
||||||
|
using var doc = JsonDocument.Parse(json);
|
||||||
return TryReadPs5Param(doc.RootElement);
|
return TryReadPs5Param(doc.RootElement);
|
||||||
}
|
}
|
||||||
catch (JsonException)
|
catch (JsonException)
|
||||||
@@ -56,12 +62,15 @@ public static class Ps5ParamJsonReader
|
|||||||
|
|
||||||
private static (string? Title, string? TitleId, string? Version) TryReadPs5Param(JsonElement root)
|
private static (string? Title, string? TitleId, string? Version) TryReadPs5Param(JsonElement root)
|
||||||
{
|
{
|
||||||
string? titleId = root.TryGetProperty("titleId", out var eTid) ? eTid.GetString() : null;
|
if (root.ValueKind != JsonValueKind.Object)
|
||||||
|
return (null, null, null);
|
||||||
|
|
||||||
|
var titleId = GetString(root, "titleId");
|
||||||
|
|
||||||
string? ver =
|
string? ver =
|
||||||
(root.TryGetProperty("contentVersion", out var cv) ? cv.GetString() : null)
|
GetString(root, "contentVersion")
|
||||||
?? (root.TryGetProperty("masterVersion", out var mv) ? mv.GetString() : null)
|
?? GetString(root, "masterVersion")
|
||||||
?? (root.TryGetProperty("targetContentVersion", out var tv) ? tv.GetString() : null);
|
?? GetString(root, "targetContentVersion");
|
||||||
|
|
||||||
string? title = ExtractTitleName(root);
|
string? title = ExtractTitleName(root);
|
||||||
|
|
||||||
@@ -70,34 +79,49 @@ public static class Ps5ParamJsonReader
|
|||||||
|
|
||||||
private static string? ExtractTitleName(JsonElement root)
|
private static string? ExtractTitleName(JsonElement root)
|
||||||
{
|
{
|
||||||
if (!root.TryGetProperty("localizedParameters", out var lp))
|
if ((!root.TryGetProperty("localizedParameters", out var lp) || lp.ValueKind != JsonValueKind.Object) &&
|
||||||
|
root.TryGetProperty("disc", out var disc) && disc.ValueKind == JsonValueKind.Object)
|
||||||
{
|
{
|
||||||
if (root.TryGetProperty("disc", out var disc) && disc.ValueKind == JsonValueKind.Object)
|
disc.TryGetProperty("localizedParameters", out lp);
|
||||||
{
|
|
||||||
disc.TryGetProperty("localizedParameters", out lp);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lp.ValueKind != JsonValueKind.Object)
|
if (lp.ValueKind != JsonValueKind.Object)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
string? defLang = lp.TryGetProperty("defaultLanguage", out var dl) ? dl.GetString() : null;
|
var defLang = GetString(lp, "defaultLanguage");
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(defLang))
|
if (!string.IsNullOrEmpty(defLang))
|
||||||
{
|
{
|
||||||
if (lp.TryGetProperty(defLang, out var langObj) && langObj.ValueKind == JsonValueKind.Object)
|
if (lp.TryGetProperty(defLang, out var langObj) && langObj.ValueKind == JsonValueKind.Object)
|
||||||
{
|
{
|
||||||
if (langObj.TryGetProperty("titleName", out var tn))
|
var title = GetString(langObj, "titleName");
|
||||||
return tn.GetString();
|
if (!string.IsNullOrWhiteSpace(title))
|
||||||
|
return title;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lp.TryGetProperty("en-US", out var en) && en.ValueKind == JsonValueKind.Object)
|
if (lp.TryGetProperty("en-US", out var en) && en.ValueKind == JsonValueKind.Object)
|
||||||
{
|
{
|
||||||
if (en.TryGetProperty("titleName", out var tn2))
|
var title = GetString(en, "titleName");
|
||||||
return tn2.GetString();
|
if (!string.IsNullOrWhiteSpace(title))
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var property in lp.EnumerateObject())
|
||||||
|
{
|
||||||
|
if (property.Value.ValueKind == JsonValueKind.Object)
|
||||||
|
{
|
||||||
|
var title = GetString(property.Value, "titleName");
|
||||||
|
if (!string.IsNullOrWhiteSpace(title))
|
||||||
|
return title;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static string? GetString(JsonElement parent, string propertyName) =>
|
||||||
|
parent.TryGetProperty(propertyName, out var value) && value.ValueKind == JsonValueKind.String
|
||||||
|
? value.GetString()
|
||||||
|
: null;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user