Merge branch 'master' into master

This commit is contained in:
Vladimir Sokolov 2025-02-08 18:30:35 +10:00 committed by GitHub
commit e4e38c4bdd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 287 additions and 113 deletions

View File

@ -1,5 +1,6 @@
using MsgPack; using MsgPack;
using Ryujinx.Horizon.Common; using Ryujinx.Horizon.Common;
using Ryujinx.Horizon.Prepo.Types;
using Ryujinx.Memory; using Ryujinx.Memory;
using System; using System;
using System.Threading; using System.Threading;
@ -8,7 +9,7 @@ namespace Ryujinx.Horizon
{ {
public static class HorizonStatic public static class HorizonStatic
{ {
internal static void HandlePlayReport(MessagePackObject report) => internal static void HandlePlayReport(PlayReport report) =>
new Thread(() => PlayReport?.Invoke(report)) new Thread(() => PlayReport?.Invoke(report))
{ {
Name = "HLE.PlayReportEvent", Name = "HLE.PlayReportEvent",
@ -16,7 +17,7 @@ namespace Ryujinx.Horizon
Priority = ThreadPriority.AboveNormal Priority = ThreadPriority.AboveNormal
}.Start(); }.Start();
public static event Action<MessagePackObject> PlayReport; public static event Action<PlayReport> PlayReport;
[field: ThreadStatic] [field: ThreadStatic]
public static HorizonOptions Options { get; private set; } public static HorizonOptions Options { get; private set; }

View File

@ -1,4 +1,3 @@
using Gommon;
using MsgPack; using MsgPack;
using MsgPack.Serialization; using MsgPack.Serialization;
using Ryujinx.Common.Logging; using Ryujinx.Common.Logging;
@ -12,19 +11,12 @@ using Ryujinx.Horizon.Sdk.Sf;
using Ryujinx.Horizon.Sdk.Sf.Hipc; using Ryujinx.Horizon.Sdk.Sf.Hipc;
using System; using System;
using System.Text; using System.Text;
using System.Threading;
using ApplicationId = Ryujinx.Horizon.Sdk.Ncm.ApplicationId; using ApplicationId = Ryujinx.Horizon.Sdk.Ncm.ApplicationId;
namespace Ryujinx.Horizon.Prepo.Ipc namespace Ryujinx.Horizon.Prepo.Ipc
{ {
partial class PrepoService : IPrepoService partial class PrepoService : IPrepoService
{ {
enum PlayReportKind
{
Normal,
System,
}
private readonly ArpApi _arp; private readonly ArpApi _arp;
private readonly PrepoServicePermissionLevel _permissionLevel; private readonly PrepoServicePermissionLevel _permissionLevel;
private ulong _systemSessionId; private ulong _systemSessionId;
@ -196,10 +188,17 @@ namespace Ryujinx.Horizon.Prepo.Ipc
{ {
return PrepoResult.InvalidBufferSize; return PrepoResult.InvalidBufferSize;
} }
StringBuilder builder = new(); StringBuilder builder = new();
MessagePackObject deserializedReport = MessagePackSerializer.UnpackMessagePackObject(reportBuffer.ToArray()); MessagePackObject deserializedReport = MessagePackSerializer.UnpackMessagePackObject(reportBuffer.ToArray());
PlayReport playReport = new()
{
Kind = playReportKind,
Room = gameRoom,
ReportData = deserializedReport
};
builder.AppendLine(); builder.AppendLine();
builder.AppendLine("PlayReport log:"); builder.AppendLine("PlayReport log:");
builder.AppendLine($" Kind: {playReportKind}"); builder.AppendLine($" Kind: {playReportKind}");
@ -209,10 +208,12 @@ namespace Ryujinx.Horizon.Prepo.Ipc
if (pid != 0) if (pid != 0)
{ {
builder.AppendLine($" Pid: {pid}"); builder.AppendLine($" Pid: {pid}");
playReport.Pid = pid;
} }
else else
{ {
builder.AppendLine($" ApplicationId: {applicationId}"); builder.AppendLine($" ApplicationId: {applicationId}");
playReport.AppId = applicationId;
} }
Result result = _arp.GetApplicationInstanceId(out ulong applicationInstanceId, pid); Result result = _arp.GetApplicationInstanceId(out ulong applicationInstanceId, pid);
@ -223,17 +224,20 @@ namespace Ryujinx.Horizon.Prepo.Ipc
_arp.GetApplicationLaunchProperty(out ApplicationLaunchProperty applicationLaunchProperty, applicationInstanceId).AbortOnFailure(); _arp.GetApplicationLaunchProperty(out ApplicationLaunchProperty applicationLaunchProperty, applicationInstanceId).AbortOnFailure();
playReport.Version = applicationLaunchProperty.Version;
builder.AppendLine($" ApplicationVersion: {applicationLaunchProperty.Version}"); builder.AppendLine($" ApplicationVersion: {applicationLaunchProperty.Version}");
if (!userId.IsNull) if (!userId.IsNull)
{ {
builder.AppendLine($" UserId: {userId}"); builder.AppendLine($" UserId: {userId}");
playReport.UserId = userId;
} }
builder.AppendLine($" Room: {gameRoom}"); builder.AppendLine($" Room: {gameRoom}");
builder.AppendLine($" Report: {MessagePackObjectFormatter.Format(deserializedReport)}"); builder.AppendLine($" Report: {MessagePackObjectFormatter.Format(deserializedReport)}");
HorizonStatic.HandlePlayReport(deserializedReport); HorizonStatic.HandlePlayReport(playReport);
Logger.Info?.Print(LogClass.ServicePrepo, builder.ToString()); Logger.Info?.Print(LogClass.ServicePrepo, builder.ToString());

View File

@ -0,0 +1,24 @@
using MsgPack;
using Ryujinx.Horizon.Sdk.Account;
using Ryujinx.Horizon.Sdk.Ncm;
namespace Ryujinx.Horizon.Prepo.Types
{
public struct PlayReport
{
public PlayReportKind Kind { get; init; }
public string Room { get; init; }
public MessagePackObject ReportData { get; init; }
public ApplicationId? AppId;
public ulong? Pid;
public uint Version;
public Uid? UserId;
}
public enum PlayReportKind
{
Normal,
System,
}
}

View File

@ -10,6 +10,7 @@ using Ryujinx.Common.Logging;
using Ryujinx.HLE; using Ryujinx.HLE;
using Ryujinx.HLE.Loaders.Processes; using Ryujinx.HLE.Loaders.Processes;
using Ryujinx.Horizon; using Ryujinx.Horizon;
using Ryujinx.Horizon.Prepo.Types;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -124,7 +125,7 @@ namespace Ryujinx.Ava
_currentApp = null; _currentApp = null;
} }
private static void HandlePlayReport(MessagePackObject playReport) private static void HandlePlayReport(PlayReport playReport)
{ {
if (_discordClient is null) return; if (_discordClient is null) return;
if (!TitleIDs.CurrentApplication.Value.HasValue) return; if (!TitleIDs.CurrentApplication.Value.HasValue) return;

View File

@ -85,7 +85,7 @@ namespace Ryujinx.Ava.Utilities.PlayReport
/// <summary> /// <summary>
/// Runs the configured <see cref="GameSpec.FormatterSpec"/> for the specified game title ID. /// Runs the configured <see cref="FormatterSpec"/> for the specified game title ID.
/// </summary> /// </summary>
/// <param name="runningGameId">The game currently running.</param> /// <param name="runningGameId">The game currently running.</param>
/// <param name="appMeta">The Application metadata information, including localized game name and play time information.</param> /// <param name="appMeta">The Application metadata information, including localized game name and play time information.</param>
@ -94,54 +94,21 @@ namespace Ryujinx.Ava.Utilities.PlayReport
public FormattedValue Format( public FormattedValue Format(
string runningGameId, string runningGameId,
ApplicationMetadata appMeta, ApplicationMetadata appMeta,
MessagePackObject playReport Horizon.Prepo.Types.PlayReport playReport
) )
{ {
if (!playReport.IsDictionary) if (!playReport.ReportData.IsDictionary)
return FormattedValue.Unhandled; return FormattedValue.Unhandled;
if (!_specs.TryGetFirst(s => runningGameId.EqualsAnyIgnoreCase(s.TitleIds), out GameSpec spec)) if (!_specs.TryGetFirst(s => runningGameId.EqualsAnyIgnoreCase(s.TitleIds), out GameSpec spec))
return FormattedValue.Unhandled; return FormattedValue.Unhandled;
foreach (FormatterSpec formatSpec in spec.SimpleValueFormatters.OrderBy(x => x.Priority)) foreach (FormatterSpecBase formatSpec in spec.ValueFormatters.OrderBy(x => x.Priority))
{ {
if (!playReport.AsDictionary().TryGetValue(formatSpec.ReportKey, out MessagePackObject valuePackObject)) if (!formatSpec.Format(appMeta, playReport, out FormattedValue value))
continue; continue;
return formatSpec.Formatter(new Value { Application = appMeta, PackedValue = valuePackObject }); return value;
}
foreach (MultiFormatterSpec formatSpec in spec.MultiValueFormatters.OrderBy(x => x.Priority))
{
List<MessagePackObject> packedObjects = [];
foreach (var reportKey in formatSpec.ReportKeys)
{
if (!playReport.AsDictionary().TryGetValue(reportKey, out MessagePackObject valuePackObject))
continue;
packedObjects.Add(valuePackObject);
}
if (packedObjects.Count != formatSpec.ReportKeys.Length)
return FormattedValue.Unhandled;
return formatSpec.Formatter(packedObjects
.Select(packObject => new Value { Application = appMeta, PackedValue = packObject })
.ToArray());
}
foreach (SparseMultiFormatterSpec formatSpec in spec.SparseMultiValueFormatters.OrderBy(x => x.Priority))
{
Dictionary<string, Value> packedObjects = [];
foreach (var reportKey in formatSpec.ReportKeys)
{
if (!playReport.AsDictionary().TryGetValue(reportKey, out MessagePackObject valuePackObject))
continue;
packedObjects.Add(reportKey, new Value { Application = appMeta, PackedValue = valuePackObject });
}
return formatSpec.Formatter(packedObjects);
} }
return FormattedValue.Unhandled; return FormattedValue.Unhandled;

View File

@ -1,6 +1,4 @@
using System.Collections.Generic; namespace Ryujinx.Ava.Utilities.PlayReport
namespace Ryujinx.Ava.Utilities.PlayReport
{ {
/// <summary> /// <summary>
/// The delegate type that powers single value formatters.<br/> /// The delegate type that powers single value formatters.<br/>
@ -12,7 +10,7 @@ namespace Ryujinx.Ava.Utilities.PlayReport
/// <br/> /// <br/>
/// OR a signal to reset the value that the caller is using the <see cref="Analyzer"/> for. /// OR a signal to reset the value that the caller is using the <see cref="Analyzer"/> for.
/// </summary> /// </summary>
public delegate FormattedValue ValueFormatter(Value value); public delegate FormattedValue SingleValueFormatter(SingleValue value);
/// <summary> /// <summary>
/// The delegate type that powers multiple value formatters.<br/> /// The delegate type that powers multiple value formatters.<br/>
@ -24,7 +22,7 @@ namespace Ryujinx.Ava.Utilities.PlayReport
/// <br/> /// <br/>
/// OR a signal to reset the value that the caller is using the <see cref="Analyzer"/> for. /// OR a signal to reset the value that the caller is using the <see cref="Analyzer"/> for.
/// </summary> /// </summary>
public delegate FormattedValue MultiValueFormatter(Value[] value); public delegate FormattedValue MultiValueFormatter(MultiValue value);
/// <summary> /// <summary>
/// The delegate type that powers multiple value formatters. /// The delegate type that powers multiple value formatters.
@ -38,5 +36,5 @@ namespace Ryujinx.Ava.Utilities.PlayReport
/// <br/> /// <br/>
/// OR a signal to reset the value that the caller is using the <see cref="Analyzer"/> for. /// OR a signal to reset the value that the caller is using the <see cref="Analyzer"/> for.
/// </summary> /// </summary>
public delegate FormattedValue SparseMultiValueFormatter(Dictionary<string, Value> values); public delegate FormattedValue SparseMultiValueFormatter(SparseMultiValue value);
} }

View File

@ -0,0 +1,74 @@
using MsgPack;
using Ryujinx.Ava.Utilities.AppLibrary;
using System.Collections.Generic;
using System.Linq;
namespace Ryujinx.Ava.Utilities.PlayReport
{
public abstract class MatchedValue<T>
{
protected MatchedValue(T matched)
{
Matched = matched;
}
/// <summary>
/// The currently running application's <see cref="ApplicationMetadata"/>.
/// </summary>
public ApplicationMetadata Application { get; init; }
/// <summary>
/// The entire play report.
/// </summary>
public Horizon.Prepo.Types.PlayReport PlayReport { get; init; }
/// <summary>
/// The matched value from the Play Report.
/// </summary>
public T Matched { get; init; }
}
/// <summary>
/// The input data to a <see cref="SingleValueFormatter"/>,
/// containing the currently running application's <see cref="ApplicationMetadata"/>,
/// and the matched <see cref="MessagePackObject"/> from the Play Report.
/// </summary>
public class SingleValue : MatchedValue<Value>
{
public SingleValue(Value matched) : base(matched)
{
}
}
/// <summary>
/// The input data to a <see cref="MultiValueFormatter"/>,
/// containing the currently running application's <see cref="ApplicationMetadata"/>,
/// and the matched <see cref="MessagePackObject"/>s from the Play Report.
/// </summary>
public class MultiValue : MatchedValue<Value[]>
{
public MultiValue(Value[] matched) : base(matched)
{
}
public MultiValue(IEnumerable<MessagePackObject> matched) : base(Value.ConvertPackedObjects(matched))
{
}
}
/// <summary>
/// The input data to a <see cref="SparseMultiValueFormatter"/>,
/// containing the currently running application's <see cref="ApplicationMetadata"/>,
/// and the matched <see cref="MessagePackObject"/>s from the Play Report.
/// </summary>
public class SparseMultiValue : MatchedValue<Dictionary<string, Value>>
{
public SparseMultiValue(Dictionary<string, Value> matched) : base(matched)
{
}
public SparseMultiValue(Dictionary<string, MessagePackObject> matched) : base(Value.ConvertPackedObjectMap(matched))
{
}
}
}

View File

@ -39,28 +39,28 @@
.AddValueFormatter("team_circle", PokemonSVUnionCircle) .AddValueFormatter("team_circle", PokemonSVUnionCircle)
); );
private static FormattedValue BreathOfTheWild_MasterMode(Value value) private static FormattedValue BreathOfTheWild_MasterMode(SingleValue value)
=> value.BoxedValue is 1 ? "Playing Master Mode" : FormattedValue.ForceReset; => value.Matched.BoxedValue is 1 ? "Playing Master Mode" : FormattedValue.ForceReset;
private static FormattedValue TearsOfTheKingdom_CurrentField(Value value) => private static FormattedValue TearsOfTheKingdom_CurrentField(SingleValue value) =>
value.DoubleValue switch value.Matched.DoubleValue switch
{ {
> 800d => "Exploring the Sky Islands", > 800d => "Exploring the Sky Islands",
< -201d => "Exploring the Depths", < -201d => "Exploring the Depths",
_ => "Roaming Hyrule" _ => "Roaming Hyrule"
}; };
private static FormattedValue SuperMarioOdyssey_AssistMode(Value value) private static FormattedValue SuperMarioOdyssey_AssistMode(SingleValue value)
=> value.BoxedValue is 1 ? "Playing in Assist Mode" : "Playing in Regular Mode"; => value.Matched.BoxedValue is 1 ? "Playing in Assist Mode" : "Playing in Regular Mode";
private static FormattedValue SuperMarioOdysseyChina_AssistMode(Value value) private static FormattedValue SuperMarioOdysseyChina_AssistMode(SingleValue value)
=> value.BoxedValue is 1 ? "Playing in 帮助模式" : "Playing in 普通模式"; => value.Matched.BoxedValue is 1 ? "Playing in 帮助模式" : "Playing in 普通模式";
private static FormattedValue SuperMario3DWorldOrBowsersFury(Value value) private static FormattedValue SuperMario3DWorldOrBowsersFury(SingleValue value)
=> value.BoxedValue is 0 ? "Playing Super Mario 3D World" : "Playing Bowser's Fury"; => value.Matched.BoxedValue is 0 ? "Playing Super Mario 3D World" : "Playing Bowser's Fury";
private static FormattedValue MarioKart8Deluxe_Mode(Value value) private static FormattedValue MarioKart8Deluxe_Mode(SingleValue value)
=> value.StringValue switch => value.Matched.StringValue switch
{ {
// Single Player // Single Player
"Single" => "Single Player", "Single" => "Single Player",
@ -87,11 +87,11 @@
_ => FormattedValue.ForceReset _ => FormattedValue.ForceReset
}; };
private static FormattedValue PokemonSVUnionCircle(Value value) private static FormattedValue PokemonSVUnionCircle(SingleValue value)
=> value.BoxedValue is 0 ? "Playing Alone" : "Playing in a group"; => value.Matched.BoxedValue is 0 ? "Playing Alone" : "Playing in a group";
private static FormattedValue PokemonSVArea(Value value) private static FormattedValue PokemonSVArea(SingleValue value)
=> value.StringValue switch => value.Matched.StringValue switch
{ {
// Base Game Locations // Base Game Locations
"a_w01" => "South Area One", "a_w01" => "South Area One",

View File

@ -1,4 +1,7 @@
using FluentAvalonia.Core; using FluentAvalonia.Core;
using MsgPack;
using Ryujinx.Ava.Utilities.AppLibrary;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -11,10 +14,11 @@ namespace Ryujinx.Ava.Utilities.PlayReport
/// </summary> /// </summary>
public class GameSpec public class GameSpec
{ {
private int _lastPriority;
public required string[] TitleIds { get; init; } public required string[] TitleIds { get; init; }
public List<FormatterSpec> SimpleValueFormatters { get; } = [];
public List<MultiFormatterSpec> MultiValueFormatters { get; } = []; public List<FormatterSpecBase> ValueFormatters { get; } = [];
public List<SparseMultiFormatterSpec> SparseMultiValueFormatters { get; } = [];
/// <summary> /// <summary>
@ -24,8 +28,8 @@ namespace Ryujinx.Ava.Utilities.PlayReport
/// <param name="reportKey">The key name to match.</param> /// <param name="reportKey">The key name to match.</param>
/// <param name="valueFormatter">The function which can return a potential formatted value.</param> /// <param name="valueFormatter">The function which can return a potential formatted value.</param>
/// <returns>The current <see cref="GameSpec"/>, for chaining convenience.</returns> /// <returns>The current <see cref="GameSpec"/>, for chaining convenience.</returns>
public GameSpec AddValueFormatter(string reportKey, ValueFormatter valueFormatter) public GameSpec AddValueFormatter(string reportKey, SingleValueFormatter valueFormatter)
=> AddValueFormatter(SimpleValueFormatters.Count, reportKey, valueFormatter); => AddValueFormatter(_lastPriority++, reportKey, valueFormatter);
/// <summary> /// <summary>
/// Add a value formatter at a specific priority to the current <see cref="GameSpec"/> /// Add a value formatter at a specific priority to the current <see cref="GameSpec"/>
@ -36,11 +40,11 @@ namespace Ryujinx.Ava.Utilities.PlayReport
/// <param name="valueFormatter">The function which can return a potential formatted value.</param> /// <param name="valueFormatter">The function which can return a potential formatted value.</param>
/// <returns>The current <see cref="GameSpec"/>, for chaining convenience.</returns> /// <returns>The current <see cref="GameSpec"/>, for chaining convenience.</returns>
public GameSpec AddValueFormatter(int priority, string reportKey, public GameSpec AddValueFormatter(int priority, string reportKey,
ValueFormatter valueFormatter) SingleValueFormatter valueFormatter)
{ {
SimpleValueFormatters.Add(new FormatterSpec ValueFormatters.Add(new FormatterSpec
{ {
Priority = priority, ReportKey = reportKey, Formatter = valueFormatter Priority = priority, ReportKeys = [reportKey], Formatter = valueFormatter
}); });
return this; return this;
} }
@ -53,7 +57,7 @@ namespace Ryujinx.Ava.Utilities.PlayReport
/// <param name="valueFormatter">The function which can format the values.</param> /// <param name="valueFormatter">The function which can format the values.</param>
/// <returns>The current <see cref="GameSpec"/>, for chaining convenience.</returns> /// <returns>The current <see cref="GameSpec"/>, for chaining convenience.</returns>
public GameSpec AddMultiValueFormatter(string[] reportKeys, MultiValueFormatter valueFormatter) public GameSpec AddMultiValueFormatter(string[] reportKeys, MultiValueFormatter valueFormatter)
=> AddMultiValueFormatter(MultiValueFormatters.Count, reportKeys, valueFormatter); => AddMultiValueFormatter(_lastPriority++, reportKeys, valueFormatter);
/// <summary> /// <summary>
/// Add a multi-value formatter at a specific priority to the current <see cref="GameSpec"/> /// Add a multi-value formatter at a specific priority to the current <see cref="GameSpec"/>
@ -66,7 +70,7 @@ namespace Ryujinx.Ava.Utilities.PlayReport
public GameSpec AddMultiValueFormatter(int priority, string[] reportKeys, public GameSpec AddMultiValueFormatter(int priority, string[] reportKeys,
MultiValueFormatter valueFormatter) MultiValueFormatter valueFormatter)
{ {
MultiValueFormatters.Add(new MultiFormatterSpec ValueFormatters.Add(new MultiFormatterSpec
{ {
Priority = priority, ReportKeys = reportKeys, Formatter = valueFormatter Priority = priority, ReportKeys = reportKeys, Formatter = valueFormatter
}); });
@ -84,7 +88,7 @@ namespace Ryujinx.Ava.Utilities.PlayReport
/// <param name="valueFormatter">The function which can format the values.</param> /// <param name="valueFormatter">The function which can format the values.</param>
/// <returns>The current <see cref="GameSpec"/>, for chaining convenience.</returns> /// <returns>The current <see cref="GameSpec"/>, for chaining convenience.</returns>
public GameSpec AddSparseMultiValueFormatter(string[] reportKeys, SparseMultiValueFormatter valueFormatter) public GameSpec AddSparseMultiValueFormatter(string[] reportKeys, SparseMultiValueFormatter valueFormatter)
=> AddSparseMultiValueFormatter(SparseMultiValueFormatters.Count, reportKeys, valueFormatter); => AddSparseMultiValueFormatter(_lastPriority++, reportKeys, valueFormatter);
/// <summary> /// <summary>
/// Add a multi-value formatter at a specific priority to the current <see cref="GameSpec"/> /// Add a multi-value formatter at a specific priority to the current <see cref="GameSpec"/>
@ -100,7 +104,7 @@ namespace Ryujinx.Ava.Utilities.PlayReport
public GameSpec AddSparseMultiValueFormatter(int priority, string[] reportKeys, public GameSpec AddSparseMultiValueFormatter(int priority, string[] reportKeys,
SparseMultiValueFormatter valueFormatter) SparseMultiValueFormatter valueFormatter)
{ {
SparseMultiValueFormatters.Add(new SparseMultiFormatterSpec ValueFormatters.Add(new SparseMultiFormatterSpec
{ {
Priority = priority, ReportKeys = reportKeys, Formatter = valueFormatter Priority = priority, ReportKeys = reportKeys, Formatter = valueFormatter
}); });
@ -111,30 +115,101 @@ namespace Ryujinx.Ava.Utilities.PlayReport
/// <summary> /// <summary>
/// A struct containing the data for a mapping of a key in a Play Report to a formatter for its potential value. /// A struct containing the data for a mapping of a key in a Play Report to a formatter for its potential value.
/// </summary> /// </summary>
public struct FormatterSpec public class FormatterSpec : FormatterSpecBase
{ {
public required int Priority { get; init; } public override bool GetData(Horizon.Prepo.Types.PlayReport playReport, out object result)
public required string ReportKey { get; init; } {
public ValueFormatter Formatter { get; init; } if (!playReport.ReportData.AsDictionary().TryGetValue(ReportKeys[0], out MessagePackObject valuePackObject))
{
result = null;
return false;
}
result = valuePackObject;
return true;
}
} }
/// <summary> /// <summary>
/// A struct containing the data for a mapping of an arbitrary key set in a Play Report to a formatter for their potential values. /// A struct containing the data for a mapping of an arbitrary key set in a Play Report to a formatter for their potential values.
/// </summary> /// </summary>
public struct MultiFormatterSpec public class MultiFormatterSpec : FormatterSpecBase
{ {
public required int Priority { get; init; } public override bool GetData(Horizon.Prepo.Types.PlayReport playReport, out object result)
public required string[] ReportKeys { get; init; } {
public MultiValueFormatter Formatter { get; init; } List<MessagePackObject> packedObjects = [];
foreach (var reportKey in ReportKeys)
{
if (!playReport.ReportData.AsDictionary().TryGetValue(reportKey, out MessagePackObject valuePackObject))
{
result = null;
return false;
}
packedObjects.Add(valuePackObject);
}
result = packedObjects;
return true;
}
} }
/// <summary> /// <summary>
/// A struct containing the data for a mapping of an arbitrary key set in a Play Report to a formatter for their sparsely populated potential values. /// A struct containing the data for a mapping of an arbitrary key set in a Play Report to a formatter for their sparsely populated potential values.
/// </summary> /// </summary>
public struct SparseMultiFormatterSpec public class SparseMultiFormatterSpec : FormatterSpecBase
{ {
public required int Priority { get; init; } public override bool GetData(Horizon.Prepo.Types.PlayReport playReport, out object result)
public required string[] ReportKeys { get; init; } {
public SparseMultiValueFormatter Formatter { get; init; } Dictionary<string, MessagePackObject> packedObjects = [];
foreach (var reportKey in ReportKeys)
{
if (!playReport.ReportData.AsDictionary().TryGetValue(reportKey, out MessagePackObject valuePackObject))
continue;
packedObjects.Add(reportKey, valuePackObject);
}
result = packedObjects;
return true;
}
}
public abstract class FormatterSpecBase
{
public abstract bool GetData(Horizon.Prepo.Types.PlayReport playReport, out object data);
public int Priority { get; init; }
public string[] ReportKeys { get; init; }
public Delegate Formatter { get; init; }
public bool Format(ApplicationMetadata appMeta, Horizon.Prepo.Types.PlayReport playReport, out FormattedValue formattedValue)
{
formattedValue = default;
if (!GetData(playReport, out object data))
return false;
if (data is FormattedValue fv)
{
formattedValue = fv;
return true;
}
switch (Formatter)
{
case SingleValueFormatter svf when data is MessagePackObject mpo:
formattedValue = svf(new SingleValue(mpo) { Application = appMeta, PlayReport = playReport });
return true;
case MultiValueFormatter mvf when data is List<MessagePackObject> messagePackObjects:
formattedValue = mvf(new MultiValue(messagePackObjects) { Application = appMeta, PlayReport = playReport });
return true;
case SparseMultiValueFormatter smvf when
data is Dictionary<string, MessagePackObject> sparseMessagePackObjects:
formattedValue = smvf(new SparseMultiValue(sparseMessagePackObjects) { Application = appMeta, PlayReport = playReport });
return true;
default:
throw new InvalidOperationException("Formatter delegate is not of a known type!");
}
}
} }
} }

View File

@ -1,20 +1,20 @@
using MsgPack; using MsgPack;
using Ryujinx.Ava.Utilities.AppLibrary;
using System; using System;
using System.Collections.Generic;
using System.Linq;
namespace Ryujinx.Ava.Utilities.PlayReport namespace Ryujinx.Ava.Utilities.PlayReport
{ {
/// <summary> /// <summary>
/// The input data to a <see cref="ValueFormatter"/>, /// The base input data to a ValueFormatter delegate,
/// containing the currently running application's <see cref="ApplicationMetadata"/>,
/// and the matched <see cref="MessagePackObject"/> from the Play Report. /// and the matched <see cref="MessagePackObject"/> from the Play Report.
/// </summary> /// </summary>
public class Value public readonly struct Value
{ {
/// <summary> public Value(MessagePackObject packedValue)
/// The currently running application's <see cref="ApplicationMetadata"/>. {
/// </summary> PackedValue = packedValue;
public ApplicationMetadata Application { get; init; } }
/// <summary> /// <summary>
/// The matched value from the Play Report. /// The matched value from the Play Report.
@ -37,6 +37,17 @@ namespace Ryujinx.Ava.Utilities.PlayReport
: boxed.ToString(); : boxed.ToString();
} }
public static implicit operator Value(MessagePackObject matched) => new(matched);
public static Value[] ConvertPackedObjects(IEnumerable<MessagePackObject> packObjects)
=> packObjects.Select(packObject => new Value(packObject)).ToArray();
public static Dictionary<string, Value> ConvertPackedObjectMap(Dictionary<string, MessagePackObject> packObjects)
=> packObjects.ToDictionary(
x => x.Key,
x => new Value(x.Value)
);
#region AsX accessors #region AsX accessors
public bool BooleanValue => PackedValue.AsBoolean(); public bool BooleanValue => PackedValue.AsBoolean();
@ -57,7 +68,7 @@ namespace Ryujinx.Ava.Utilities.PlayReport
} }
/// <summary> /// <summary>
/// A potential formatted value returned by a <see cref="ValueFormatter"/>. /// A potential formatted value returned by a ValueFormatter delegate.
/// </summary> /// </summary>
public readonly struct FormattedValue public readonly struct FormattedValue
{ {
@ -103,28 +114,47 @@ namespace Ryujinx.Ava.Utilities.PlayReport
/// <summary> /// <summary>
/// Return this to tell the caller there is no value to return. /// Return this to tell the caller there is no value to return.
/// </summary> /// </summary>
public static FormattedValue Unhandled => default; public static readonly FormattedValue Unhandled = default;
/// <summary> /// <summary>
/// Return this to suggest the caller reset the value it's using the <see cref="Analyzer"/> for. /// Return this to suggest the caller reset the value it's using the <see cref="Analyzer"/> for.
/// </summary> /// </summary>
public static FormattedValue ForceReset => new() { Handled = true, Reset = true }; public static readonly FormattedValue ForceReset = new() { Handled = true, Reset = true };
/// <summary> /// <summary>
/// A delegate singleton you can use to always return <see cref="ForceReset"/> in a <see cref="ValueFormatter"/>. /// A delegate singleton you can use to always return <see cref="ForceReset"/> in a <see cref="SingleValueFormatter"/>.
/// </summary> /// </summary>
public static readonly ValueFormatter SingleAlwaysResets = _ => ForceReset; public static readonly SingleValueFormatter SingleAlwaysResets = _ => ForceReset;
/// <summary> /// <summary>
/// A delegate singleton you can use to always return <see cref="ForceReset"/> in a <see cref="MultiValueFormatter"/>. /// A delegate singleton you can use to always return <see cref="ForceReset"/> in a <see cref="MultiValueFormatter"/>.
/// </summary> /// </summary>
public static readonly MultiValueFormatter MultiAlwaysResets = _ => ForceReset; public static readonly MultiValueFormatter MultiAlwaysResets = _ => ForceReset;
/// <summary>
/// A delegate singleton you can use to always return <see cref="ForceReset"/> in a <see cref="SparseMultiValueFormatter"/>.
/// </summary>
public static readonly SparseMultiValueFormatter SparseMultiAlwaysResets = _ => ForceReset;
/// <summary> /// <summary>
/// A delegate factory you can use to always return the specified /// A delegate factory you can use to always return the specified
/// <paramref name="formattedValue"/> in a <see cref="ValueFormatter"/>. /// <paramref name="formattedValue"/> in a <see cref="SingleValueFormatter"/>.
/// </summary> /// </summary>
/// <param name="formattedValue">The string to always return for this delegate instance.</param> /// <param name="formattedValue">The string to always return for this delegate instance.</param>
public static ValueFormatter AlwaysReturns(string formattedValue) => _ => formattedValue; public static SingleValueFormatter SingleAlwaysReturns(string formattedValue) => _ => formattedValue;
/// <summary>
/// A delegate factory you can use to always return the specified
/// <paramref name="formattedValue"/> in a <see cref="MultiValueFormatter"/>.
/// </summary>
/// <param name="formattedValue">The string to always return for this delegate instance.</param>
public static MultiValueFormatter MultiAlwaysReturns(string formattedValue) => _ => formattedValue;
/// <summary>
/// A delegate factory you can use to always return the specified
/// <paramref name="formattedValue"/> in a <see cref="SparseMultiValueFormatter"/>.
/// </summary>
/// <param name="formattedValue">The string to always return for this delegate instance.</param>
public static SparseMultiValueFormatter SparseMultiAlwaysReturns(string formattedValue) => _ => formattedValue;
} }
} }