Skip to content

feat(libSceNpTrophy2,libSceNpUniversalDataSystem): record trophy unlocks and statistics - #7335

Merged
oneandonlydean merged 13 commits into
boykopovar:mainfrom
EagleBearerYS:prep/np-trophies-part2
Oct 18, 2026
Merged

oneandonlydean merged 13 commits into
boykopovar:mainfrom
EagleBearerYS:prep/np-trophies-part2

Conversation

@EagleBearerYS

@EagleBearerYS EagleBearerYS commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

What

Trophies now unlock the way a title defines them. This is the second half of the split asked for on #7262 (merged), which reports the title's trophy data with every trophy locked: this PR records unlocks and statistics and connects those queries to them. It is built on #7262's head 61e12c7, so with #7262 merged its diff shows only its own commits. The title's own sce_sys/uds/udsNN.ucp is read once per service label, on first use, like the trophy data.

  • Unlocks a title posts are recorded. _UnlockTrophy with an int32 _trophy_id unlocks that trophy for the title, user and service label. The unlock goes in a host file next to save data (/_trophy/<user id>_<service label>.txt) and survives restarts.
  • Unlocks through statistics. Every other event a title posts goes through the title's statistics rules (stats_extraction.json in udsNN.ucp). A rule sets a statistic from one of the event's properties. After each event that sets one, every trophy's unlockCondition is checked (ge, gt, le or lt against its target) on the values that event set and the stored values of the others, and reached trophies unlock the same way. Statistics keep their latest value in /_trophy/<user id>_<service label>.stats.txt, so progress also survives restarts.
  • Platinum unlocks once every trophy that names it in platinumTrophyId is unlocked, whichever way they were unlocked.
  • Posted unlocks follow the title's rules. With the title's trophy data loaded, _UnlockTrophy of an id that isn't in it, of the platinum, or of a progressive trophy is refused with one line ([trophy] ignored the posted unlock of trophy 0 for user 268435456: it is the platinum, which unlocks with the last trophy that names it), as KytyPS5 does; those unlock through their statistics and the platinum rule instead. Without trophy data, every posted id of 0 or more is recorded, and no platinum unlocks by itself, since the rule needs the data.
  • The queries report unlocks. A trophy reports whether it is unlocked and when, a progressive trophy its progress, and the game and its groups their unlocked counts and progress_percentage.
  • A new unlock logs [trophy] <title id> unlocked trophy <id> for user <user>. With APS5_TRACE_UDS=1, every post and every statistic it sets is printed too.

Planet of Lana (PPSA19450) earns its trophies this way. Its statistics rules are all of kinds this PR skips (see Not covered):

[trophy] /app0/sce_sys/uds/uds00.ucp: 0 stats, 0 rules for 0 events

Sneaking past the chef at the start of Chapter 1 posts the unlock of trophy 12, "Sneaky Sisters" ("Sneak past the chef" in the title's own data):

[uds] context 1 posted _UnlockTrophy {"_trophy_id":12}
[trophy] PPSA19450 unlocked trophy 12 for user 268435456

The unlock is stored as 12 63927864523936811 in _trophy/268435456_0.txt, next to the title's app0/.

Titles that ship no udsNN.ucp, or post no trophy events, behave as with #7262:

  • Every return code is the same.
  • A missing file is silent (a [uds] line with APS5_TRACE_UDS).
  • A malformed file logs one [trophy] <file> is malformed, so ...: <reason> line and turns off only its own feature: without statistics rules, events set no statistic. _UnlockTrophy keeps working either way.
  • One behaviour change: posting a null event pointer, one that CreateEvent didn't return, or one already destroyed now throws, as the library's other invalid arguments do. Before, it was ignored.

How:

  • Events and properties. libSceNpUniversalDataSystem's events and property objects get real contents: int32, uint32, int64, uint64, float, double, bool, string, bytes, nested objects and arrays, with keys kept in insertion order. A registry owns what the Create functions return; Destroy* on anything else, such as an event's own property object, does nothing, because its owner frees it.
  • From event to unlock. A rule picks its $.path property (nested objects give a.b paths). The value is used if it's a non-negative integer that fits the statistic's type and lies within its minValue/maxValue. Otherwise it's ignored with one [trophy] ignored <event> $.<path> for stat <id>: <reason> line. A statistic that was never set reaches nothing, so le and lt trophies don't unlock at a start value of 0. Well-formed entries this doesn't support (for example a sum aggregation) are skipped, each with one log line.
  • The store. One file per user and service label for unlocks and one for statistics, each rewritten through a temporary file and a rename, via RecordWrittenPath_nid_no_patch like other host writes. A statistic is written only when its value changes (StatStore::Set returns early otherwise, TrophyStore.cpp:173), so a statistic that changes with every event writes its file each time. A damaged file throws with its name and line (a debt entry, below).
  • Hook. An unlock listener hook (NpTrophy2AddUnlockListener_nid_no_patch) is there for a later pop-up. Removing a listener waits for an unlock callback that is running, so a removed listener is never called. sceNpTrophy2RegisterUnlockCallback is still not called, because its callback signature isn't public.

Choices the public data leaves open, each listed as debt:

  • A property of any integer width counts toward a statistic if it fits, so a title that sets a counter with SetInt32 still counts. The one public implementation requires the exact width.
  • A progressive trophy reports min(statistic, target), and its target once unlocked.
  • progress_percentage weighs gold 6, silver 2 and bronze 1 and leaves the platinum out, as KytyPS5 does; the console's formula is not known.
  • The platinum rule: whether the console needs exactly the trophies that name it, or every other trophy of the base group. The two agree when every base trophy names it.
  • Damaged records (the last commit): a line in _trophy/<user id>_<service label>.txt or .stats.txt that isn't a new <id> <value> pair (a blank line or one ending in a carriage return included) throws in every trophy query, posted unlock and statistic update that reads that record for its user and service label. The export doesn't return, which stops the title, and the record is read again each time, so it keeps failing until the file is fixed or deleted. On main these calls returned 0.

Sources. Interface facts only, no code, from KytyPS5 (GPL-2.0) at 9e41d8320a:

  • src/common/trophies.cpp: the stats_definition.json and stats_extraction.json keys, and the four comparators;
  • src/libs/libTrophy.cpp: the _UnlockTrophy event and its _trophy_id, CreateEvent's property ownership, and the platinum rule;
  • the percentage weights (gold 6, silver 2, bronze 1, platinum left out) match the PSN point values third parties document (15, 30 and 90).

KytyPS5's error codes aren't adopted, since no other public source confirms them. The return codes are unchanged.

Commits on #7262's head (each builds and passes the tests that exist at it):

  1. feat(libSceNpTrophy2): record trophy unlocks per title and user: the unlock store, the reported unlocks, the listener hook with its dispatch lock, the queries' connection to the store and the platinum, and np_trophy2_store.
  2. feat(libSceNpUniversalDataSystem): keep event properties and record posted trophy unlocks: events and properties with real contents, the _UnlockTrophy path, and np_uds_events, guest_np_trophy_unlocks and guest_np_trophy_data's posted unlocks.
  3. docs: describe trophy unlocks in USAGE.
  4. feat(libSceNpUniversalDataSystem): unlock trophies through the title's statistics: the statistics rules, their store and the unlock check, with guest_np_trophy_stats.
  5. docs: describe trophy statistics in USAGE.
  6. test(libSceNpUniversalDataSystem): check that a stat with a sum aggregation unlocks nothing: it pins the behaviour, so it passes before the next commit.
  7. fix(libSceNpTrophy2): refuse a posted unlock of an unknown, platinum or progressive trophy: refused when the title's trophy data is loaded; the statistics path and the platinum award keep recording.
  8. docs: say which posted trophy unlocks are refused and what stops a title.
  9. docs: say that a malformed trophy or stat record line fails the query: the damaged-records debt entry above.

Overlap (checked against main f1af6c5 and the 38 open PRs that touch the same files; the ones that merge into main merge with this PR too):

Tested

Every test builds its own synthetic .ucp files in a fresh directory; none uses a title's data. This PR's head is #7262's series split from the one reviewed there: with main 5361e69 merged into that series, the trees are the same apart from commit 9's debt entry, so the red runs below, measured on it before the split, are the same code.

  • np_trophy2_package (unit), on top of feat(libSceNpTrophy2): report the title's trophy data from trophyNN.ucp #7262's checks: progress, the platinum after the last trophy that names it and only once, counts and the percentage; the statistics rules kept and skipped, 7 bad input paths, each comparator below, at and above its target, and the statistics store across a restart with 8 damaged files.
  • guest_np_trophy_data, through the built libraries' exports only, on top of feat(libSceNpTrophy2): report the title's trophy data from trophyNN.ucp #7262's checks: direct unlocks giving counts, the percentage and the platinum with the last trophy that names it, in listener order.
  • guest_np_trophy_stats, through the exports only:
    • a statistics file from a previous run shows as progress before any event;
    • each comparator through posted events: ge 9 locked, 10 unlocks; gt 100 locked, 101 unlocks; le 6 locked, 5 unlocks; lt with −1 and a float ignored, 2 unlocks;
    • a nested player.steps progressive trophy at 4, then unlocked at 5, with the platinum after it;
    • the exact statistics file 10 3\n20 101\n30 2\n40 5\n and the unlock file;
    • no files, a malformed trophy file and a truncated uds03.ucp, each turning off only its own feature.
  • np_uds_events, np_trophy2_store and guest_np_trophy_unlocks cover the posted-unlock path: every property setter, ownership, unlock recognition and near-miss names, persistence, damaged records (a carriage return included), and _UnlockTrophy {"_trophy_id":12} through the exports. guest_nptrophy2 still passes unchanged.
  • Listener removal (np_trophy2_store's testListenerRemovalWaitsForItsCallback): removing a listener while its callback runs waits for the callback. Without the dispatch lock it fails 3 of 3 runs (a listener was removed while its callback was still running, so its context could be freed under it); with it, it passes 3 of 3.
  • The reader under sanitizers (ASan and UBSan): each input below is accepted or refused with a std::runtime_error. None reads out of bounds, leaks, or throws anything else: 48 malformed files and 2 valid packages, every truncation of both packages (1,190), 200,000 random byte mutations and 200,000 random JSON texts.
  • Posted unlock rules (np_trophy2_store's testPostedUnlockRules and guest_np_trophy_data, through the exports): with trophy data loaded, an unknown id, the platinum and a progressive trophy posted through UDS stay locked, record nothing, call no listener and log their line, and the counts don't move; the progressive trophy still unlocks through its statistic and the platinum with the last trophy that names it; without trophy data, any id is still recorded. Without the fix commit both fail: an unknown, platinum or progressive trophy posted through UDS was recorded and a progressive, platinum or unknown trophy posted through UDS unlocked, or changed the counts.
  • Other aggregations (guest_np_trophy_stats): a sum statistic logs stat 10 has aggregation "sum"; only "latest" is supported, stores nothing and unlocks nothing.
  • Mutations: gt read as ge, no platinum, statistics not saved, truncation not checked, and progress ignoring the statistic each turn np_trophy2_package red, and four of them guest_np_trophy_stats too.
  • Linux (Ubuntu 24.04, GCC 13.3, CI's configure flags, as an unprivileged user), on main 5361e69 with feat(libSceNpTrophy2): report the title's trophy data from trophyNN.ucp #7262: 1,411 tests, 1,338 passed, 68 skipped, 5 failed. main on the same machine gives 1,405, 1,332, 68 and 5.
    • Per test, the only difference is the 6 tests the two PRs add, all passing.
    • The 5 failures are the IPv6 tests (guest_datagram_* and guest_tcp), which fail on that machine on main too, and guest_sce_net skips there for the same reason.
    • Each of the 9 commits builds and passes the trophy, UDS and system-service tests that exist at it (6, then 8 and 9), and check_test_registration.py and check_conventions.py pass.
  • Windows, the Build workflow's Windows job on my fork, at 8e927be: 1,438 tests, 0 failed (run). The macOS and Ubuntu jobs pass too.
  • Planet of Lana (PPSA19450), Windows 11, NVIDIA GeForce RTX 3070 Laptop GPU (driver 617.42), with APS5_TRACE_UDS=1, played by hand into Chapter 1. It ran on our test tree for the title (not main; it carries the open fixes the title needs) with the whole trophy series before the split, at e06a4f2, before the reviews' last fixes were folded in. Those fixes are off this path: the title registers no unlock listener (the dispatch lock), its trophy JSON is far below the value cap, and it has no statistics rule this PR reads (the statistics fix and the language message). The title unlocked "Sneaky Sisters" as shown under What. The log has no fatal error and no [trophy] or [uds] error or refused post.

Not covered, and why:

  • Statistics rules other than latest: other aggregations (sum, count, max, collection, eachLatest), data types other than uint32 and uint64, and rule conditions beyond the event name. No public source gives their meaning, and KytyPS5 skips them too. Each such rule logs one line naming it, and a trophy that depends on one doesn't unlock. Planet of Lana (PPSA19450) has only such statistics, so this PR reads none of them ([trophy] /app0/sce_sys/uds/uds00.ucp: 0 stats, 0 rules for 0 events). That doesn't stop its trophies: each of its 24 stat-linked trophies is non-progressive and unlocks when the title posts _UnlockTrophy with its _trophy_id, which this PR records directly. Its count statistics count those same posts, and its collection and eachLatest statistics feed activities.
  • _UpdateTrophyProgress: logged once per trophy and not interpreted, because its properties aren't public.
  • An unlock pop-up and sceNpTrophy2RegisterUnlockCallback: the callback's signature isn't public.

Checklist

SystemServiceConsoleLanguage in SystemService.hpp reads ANYPS5_LANGUAGE
with the same default, range and error as sceSystemServiceParamGetInt,
which now calls it. Other libraries can pick the console language
without linking libSceSystemService.
A progress value is {int32 type, 4 reserved bytes, uint64 value}, as
KytyPS5 lays it out, which makes the trophy details 1312 bytes and the
trophy data 32 bytes. Size checks for every query structure keep the
layout fixed, and the technical debt names its single public source.
The title's sce_sys/trophy2/trophyNN.ucp is read on first use with a
bounds-checked .ucp reader and a small JSON reader. tropconf.json gives
each trophy's grade, group, hidden and reward flags, platinum and
unlock condition, and tropmeta_<tag>.json its texts in the console
language, then en-US, then the file's default language. Contexts
remember their user and service label, so the queries find the file.

The trophy, group and game queries report these names, grades, groups
and counts, list every trophy and group, and give progressive trophies
their target. Unlocks are not recorded yet, so every trophy reports as
locked and nothing is written to the host. A missing file is ignored
and a malformed one is logged once; either way the queries keep their
placeholders. A JSON document that nests deeper than 64 levels or holds
more than 1048576 values counts as malformed, which keeps the parsed
values to about 140 MiB.
Unlocks are kept in _trophy/<user id>_<service label>.txt in the title's
run directory, one "<trophy id> <RTC tick>" line per trophy, and each new
unlock logs "[trophy] <title id> unlocked trophy <id> for user <id>".
GetTrophyInfo, GetTrophyInfoArray, GetGroupInfo, GetGroupInfoArray and
GetGameInfo report the recorded unlocks, their times, the unlocked
counts by grade and the progress percentage. An unlock also unlocks the
platinum the trophy names once every trophy naming that platinum is
unlocked. Unlock listeners let a later notification subscribe. Removing a
listener waits for its running callback, and a listener removed during a
dispatch is not called by it.
…osted trophy unlocks

Events and property objects keep their typed properties instead of
dropping them. PostEvent records an _UnlockTrophy event with an int32
_trophy_id for the user and service label of its context, logs why a
trophy event is ignored, and prints every posted event with
APS5_TRACE_UDS.
…s statistics

Events other than the trophy events go through the title's
sce_sys/uds/udsNN.ucp: stats_definition.json gives the statistics that
keep their latest uint32 or uint64 value, and stats_extraction.json the
event property ($.name, dotted for nested objects) that sets each one.
Statistics are kept per user and service label in
_trophy/<user id>_<service label>.stats.txt next to the unlocks.

After each event that sets a statistic, every trophy's unlockCondition
(ge, gt, le or lt against its targetValue) is checked on the values that
event set and the stored values of the other statistics, so a later
event setting the same statistic cannot hide what this one reached.
Reached trophies unlock through the same path as _UnlockTrophy.
Progressive trophies report their statistic as progress, and
APS5_TRACE_UDS prints each statistic an event sets. A missing udsNN.ucp
is ignored and a malformed one is logged once.
…gation unlocks nothing

A title whose uds file defines a stat with an aggregation other than
latest gets one log line naming the stat. Posting an event for it
through the exports stores no stat and unlocks no trophy.
…or progressive trophy

With the title's trophy data loaded, an _UnlockTrophy posted through UDS
no longer records an id the data does not have, the platinum, or a
progressive trophy. Each refusal logs the trophy and the reason, returns
false and calls no listener.

The statistics path and the platinum award now use an internal record
that keeps part 1's behaviour, so a progressive trophy still unlocks
through its statistic and the platinum with the last trophy that names
it. Without trophy data every id of 0 or more is recorded as before.
USAGE.md describes the refused posted unlocks and their log line, and
says that the icon queries and a damaged or unwritable unlock or
statistics file stop the game with an error. The debt entries of
libSceNpTrophy2 and libSceNpUniversalDataSystem no longer say that any
posted id is stored. Both USAGE.md and the libSceNpTrophy2 entry say
that without trophy data the platinum is not unlocked when the other
trophies are.
@github-actions

github-actions Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 6361 docs/dev/debt/libSceNpUniversalDataSystem.md

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 3998 docs/user/USAGE.md
PR 4891 docs/user/USAGE.md
PR 5021 docs/user/USAGE.md
PR 5925 docs/user/USAGE.md
PR 6044 core/libs/prx/libSceNpTrophy2/CMakeLists.txt
PR 6111 docs/user/USAGE.md
PR 6138 core/libs/SceTypes.hpp
PR 6171 docs/user/USAGE.md
PR 6361 core/libs/prx/libSceNpUniversalDataSystem/CMakeLists.txt
docs/dev/debt/libSceNpUniversalDataSystem.md
PR 6788 docs/user/USAGE.md
PR 6924 docs/user/USAGE.md
PR 6946 docs/user/USAGE.md
PR 6947 docs/user/USAGE.md
PR 6977 core/libs/SceTypes.hpp
PR 7332 docs/user/USAGE.md

@oneandonlydean
oneandonlydean merged commit c4fea90 into boykopovar:main Oct 18, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants