diff --git a/mods/README.md b/mods/README.md index bfd7a70..eda5b8a 100644 --- a/mods/README.md +++ b/mods/README.md @@ -20,6 +20,8 @@ mods// skins// bodies and weapons that replace the default ones ``` +**Bartz's moves from the first Dissidia:** `psprecomp/games/dissidia/tools/d1_moves.py --d1 --iso012 <012 ISO>` makes `mods/bartz-dff` from your own two discs: eight moves 012 dropped come back as new abilities. Settings > Mods switches them on and off (next start). Details: [docs/character-mods.md](../psprecomp/games/dissidia/docs/character-mods.md) (*New moves for a retail character*). + A folder with `"kind": "retail"` in its `mod.json` changes one of the game's own characters instead of adding one (the Move Studio's *Save as mod* makes those); its body can be swapped for your own model, and `"skin": "retail"` puts the game's back. A folder whose name starts with `_`, or whose `mod.json` has `"enabled": false`, is never built. `_template/` is the template. ## By hand diff --git a/psprecomp/docs/installer.md b/psprecomp/docs/installer.md index 36fad2b..cd618ed 100644 --- a/psprecomp/docs/installer.md +++ b/psprecomp/docs/installer.md @@ -71,7 +71,7 @@ of the fork. of what the current stage does in the information panel. The step weights live in `setup/install.cpp`. - **Add to Steam** (desktop only; an Options toggle, and a row on the start page of an installed game): `plan_steam` runs `games/dissidia/tools/steam_shortcut.py` (Linux, Windows; the script also serves the Steam Deck setup) to add a non-Steam shortcut to every Steam account on the machine, pointing at `DissidiaRecomp --launch` (check for an update, then play), with the game's icon and the disc's PIC1.PNG as banner. Steam is closed for a moment (`-shutdown`) and started again, because it rewrites its shortcut list on exit. The step is optional: no Steam installed only adds a note to the last page. - **Change extras** (start page of an installed game): the Extras page again, applied without a rebuild (`plan_extras`): downloads / swaps / removes the HD texture pack, adds the Japanese voices, builds the Android version once. - - **Options** (one page in the game's Settings-screen look, without the category sidebar: HD textures on / off, then Texture pack when on, Japanese voices, Android version, Import saves, update check, install folder, Start installing): check for updates; HD textures (on / off) and, when on, Texture pack (LiinkPK's hand-retouched pack or the AI upscaled pack; the help line credits the selected pack); Japanese voices (undub; asks for the undub disc image first); Android + - **Options** (one page in the game's Settings-screen look, without the category sidebar: HD textures on / off, then Texture pack when on, Japanese voices, Android version, Import saves, update check, install folder, Start installing): check for updates; HD textures (on / off) and, when on, Texture pack (LiinkPK's hand-retouched pack or the AI upscaled pack; the help line credits the selected pack); Japanese voices (undub; asks for the undub disc image first); **Bartz: Dissidia 1 moves** (`d1_moves=1`; asks for a Dissidia Final Fantasy (2008) disc image, then `tools/d1_moves.py install --d1 --work ` rebuilds the mod's six files from the shipped patches in `game/d1_moves/` and the player's two discs, checks each against its SHA-256, adds the abilities to main.bin and writes `mods/retail_moves.txt`; also offered by Change extras); Android version; **Import saves** (cycles Off / each PPSSPP or memory-stick SAVEDATA folder found with Dissidia 012 saves, found by `psp_savemgr::detect_sources`; it adds the last install step, `DissidiaRecomp --import-saves FROM --dir WORK`, which copies the saves under the installed game's disc ID, re-signed, and never replaces a save that already exists in the install; verified diff --git a/psprecomp/games/dissidia/d1_moves/manifest.json b/psprecomp/games/dissidia/d1_moves/manifest.json new file mode 100644 index 0000000..9da8860 --- /dev/null +++ b/psprecomp/games/dissidia/d1_moves/manifest.json @@ -0,0 +1,212 @@ +{ + "_info": "Bartz's Dissidia 1 moves: patches that rebuild the mod's files from the player's own Dissidia 012 and Dissidia 1 discs (tools/d1_moves.py install). Made by tools/d1_moves.py patches.", + "folder": "bartz-dff", + "code": "fiv100", + "display": "Bartz: Dissidia (2008) moves", + "files": [ + { + "path": "obj/p_fiv100.objx", + "size": 1983820, + "sha256": "a7a67dd29f5067ac5a9b707b5b428a652a1e53427d52e884f28414f68bdd7a15", + "patch": "p_fiv100.objx.npd", + "sources": [ + "012:obj/p_fiv100.objx", + "d1conv:obj/p_fiv100.objx", + "012:obj/p_for100.objx", + "012:obj/p_six100.objx", + "012:obj/p_thr100.objx", + "012:obj/p_ten100.objx", + "012:obj/p_sev100.objx", + "012:obj/p_one100.objx", + "012:obj/p_two100.objx" + ], + "sources_sha256": [ + "d71567ebd9fab3a50ab725fb5db44253693e1dafc1cde7a890777953ad4e9a83", + "481a7820bcd041e3b95008e271b64e67522f8b769765195101e1cc4597242340", + "e8ed7ea7d07a526ca6ebad4f0ddc43894f8566799158e56bb34e27046ebbae0e", + "8096f57850b01f1e49d37a28655236ad238f9f6bb2784559474d38e098dccde4", + "92048f758e9b739c5ca146c6526aa5a19dcb4d8e4be58ff47f2015a35ed338a3", + "44565392e6beba42a64b27f7c4e0187068672890bdc5ad26cad0b1c4f0180dfc", + "9c296086238b09bac54d7800e24c11013146412417ae01544cc19cf244193ca1", + "3a81bfb308d32d0c3a93a07495ca9ca90d800bfaf633955e0813e974d8d668e1", + "b314530d34aac80f9e4320f0dde57997e2175e51b3db2e27cc61dfa1daef8777" + ] + }, + { + "path": "obj/p_fiv100_1p.gmo", + "size": 2462188, + "sha256": "9a17294ec50482f2f1293700a72d3e75f935df673494d9a95c5b7f66716eeafb", + "patch": "p_fiv100_1p.gmo.npd", + "sources": [ + "012:obj/p_fiv100_1p.gmo", + "d1:obj/p_fiv100_1p.gmo" + ], + "sources_sha256": [ + "1b843a3e44fcc32e5ac6ac86b669cb5a14494f7121528148be3c48d60b5eaab5", + "bb524d5b68740f781df294358e3eaa7c48355747dad9ba38ebd44d851ec51ed1" + ] + }, + { + "path": "obj/p_fiv100_2p.gmo", + "size": 2462188, + "sha256": "c54dbeb66a6ec284aded5bed724260cc0e7aceb51a0744c6ba0232e085093c0f", + "patch": "p_fiv100_2p.gmo.npd", + "sources": [ + "012:obj/p_fiv100_2p.gmo", + "d1:obj/p_fiv100_1p.gmo" + ], + "sources_sha256": [ + "592e67b8c6fcac7300e80ca8a32fa5cbbaa756b729782b9b1fe80ef1c4096532", + "bb524d5b68740f781df294358e3eaa7c48355747dad9ba38ebd44d851ec51ed1" + ] + }, + { + "path": "obj/p_fiv100_3p.gmo", + "size": 2470132, + "sha256": "b86bc46bb92bc4f4a6ad9fa5c4fa583996d4c8db970b5bc8294d14431d49dfc9", + "patch": "p_fiv100_3p.gmo.npd", + "sources": [ + "012:obj/p_fiv100_3p.gmo", + "d1:obj/p_fiv100_1p.gmo" + ], + "sources_sha256": [ + "43da35210e9dd7e4974400cd194eb22c426f7667f1364779aec71d49057d09c7", + "bb524d5b68740f781df294358e3eaa7c48355747dad9ba38ebd44d851ec51ed1" + ] + }, + { + "path": "obj/p_fiv100_zako.gmo", + "size": 2443668, + "sha256": "c0dbcc2c5560b87c0b8272f837ec2d800c0605af071438acd8a35e665c239b5c", + "patch": "p_fiv100_zako.gmo.npd", + "sources": [ + "012:obj/p_fiv100_zako.gmo", + "d1:obj/p_fiv100_1p.gmo" + ], + "sources_sha256": [ + "89422d7152374b0e49c9e5595b1364ebbdf60bbe50d392e9e78a626fbafc8cbf", + "bb524d5b68740f781df294358e3eaa7c48355747dad9ba38ebd44d851ec51ed1" + ] + }, + { + "path": "voice/en/battle/voice_p_fiv100_00.scd", + "size": 357600, + "sha256": "6b3709f8f3f959495e8672ed5608ecb7bd6bf95572d680b6c6c4e84fdba1a8cc", + "patch": "voice_p_fiv100_00.scd.npd", + "sources": [ + "012:voice/en/battle/voice_p_fiv100_00.scd", + "d1crc:dd2e4d13" + ], + "sources_sha256": [ + "9f67e60176757f67a7f867d35082d0a0c464e3373deb2feb1c036c788caa7946", + "06fdeeb7c0acde5bfeb82508fb5e842f7045741a71dd7ff344da282c28ce2eec" + ] + } + ], + "move_names": { + "10": { + "copy": "0C", + "cmd": "0xB0", + "type": "brave", + "phase": "ground", + "learn": 6, + "name": "Ascendant Lance", + "_note": "Dissidia (2008) Bartz move, command 0x198 there (tools/d1_moves.py)", + "cp": 35, + "cp_mastered": 15, + "ap": 120, + "help_d1_cmd": 408 + }, + "11": { + "copy": "0C", + "cmd": "0xB1", + "type": "brave", + "phase": "ground", + "learn": 3, + "name": "Climbarrel", + "_note": "Dissidia (2008) Bartz move, command 0x19A there (tools/d1_moves.py)", + "cp": 35, + "cp_mastered": 20, + "ap": 120, + "help_d1_cmd": 410 + }, + "12": { + "copy": "0D", + "cmd": "0xB2", + "type": "brave", + "phase": "midair", + "learn": 12, + "name": "Storm Shot", + "_note": "Dissidia (2008) Bartz move, command 0x19E there (tools/d1_moves.py)", + "cp": 35, + "cp_mastered": 15, + "ap": 180, + "help_d1_cmd": 414 + }, + "13": { + "copy": "0D", + "cmd": "0xB3", + "type": "brave", + "phase": "midair", + "learn": 28, + "name": "Slidehazzard", + "_note": "Dissidia (2008) Bartz move, command 0x19F there (tools/d1_moves.py)", + "cp": 35, + "cp_mastered": 15, + "ap": 180, + "help_d1_cmd": 415 + }, + "14": { + "copy": "0E", + "cmd": "0xB4", + "type": "hp", + "phase": "ground", + "learn": 24, + "name": "Soul Eater", + "_note": "Dissidia (2008) Bartz move, command 0x19C there (tools/d1_moves.py)", + "cp": 45, + "cp_mastered": 25, + "ap": 180, + "help_d1_cmd": 412 + }, + "15": { + "copy": "0E", + "cmd": "0xB5", + "type": "hp", + "phase": "ground", + "learn": 19, + "name": "Flood", + "_note": "Dissidia (2008) Bartz move, command 0x19D there (tools/d1_moves.py)", + "cp": 45, + "cp_mastered": 25, + "ap": 180, + "help_d1_cmd": 413 + }, + "16": { + "copy": "0F", + "cmd": "0xB6", + "type": "hp", + "phase": "midair", + "learn": 46, + "name": "Wind Shear", + "_note": "Dissidia (2008) Bartz move, command 0x1A1 there (tools/d1_moves.py)", + "cp": 45, + "cp_mastered": 25, + "ap": 300, + "help_d1_cmd": 417 + }, + "17": { + "copy": "0F", + "cmd": "0xB7", + "type": "hp", + "phase": "midair", + "learn": 38, + "name": "Paladin Force", + "_note": "Dissidia (2008) Bartz move, command 0x1A2 there (tools/d1_moves.py)", + "cp": 45, + "cp_mastered": 25, + "ap": 180, + "help_d1_cmd": 418 + } + } +} diff --git a/psprecomp/games/dissidia/d1_moves/p_fiv100.objx.npd b/psprecomp/games/dissidia/d1_moves/p_fiv100.objx.npd new file mode 100644 index 0000000..9461d9c Binary files /dev/null and b/psprecomp/games/dissidia/d1_moves/p_fiv100.objx.npd differ diff --git a/psprecomp/games/dissidia/d1_moves/p_fiv100_1p.gmo.npd b/psprecomp/games/dissidia/d1_moves/p_fiv100_1p.gmo.npd new file mode 100644 index 0000000..067a3a8 Binary files /dev/null and b/psprecomp/games/dissidia/d1_moves/p_fiv100_1p.gmo.npd differ diff --git a/psprecomp/games/dissidia/d1_moves/p_fiv100_2p.gmo.npd b/psprecomp/games/dissidia/d1_moves/p_fiv100_2p.gmo.npd new file mode 100644 index 0000000..067a3a8 Binary files /dev/null and b/psprecomp/games/dissidia/d1_moves/p_fiv100_2p.gmo.npd differ diff --git a/psprecomp/games/dissidia/d1_moves/p_fiv100_3p.gmo.npd b/psprecomp/games/dissidia/d1_moves/p_fiv100_3p.gmo.npd new file mode 100644 index 0000000..2e2e0b6 Binary files /dev/null and b/psprecomp/games/dissidia/d1_moves/p_fiv100_3p.gmo.npd differ diff --git a/psprecomp/games/dissidia/d1_moves/p_fiv100_zako.gmo.npd b/psprecomp/games/dissidia/d1_moves/p_fiv100_zako.gmo.npd new file mode 100644 index 0000000..2d977b5 Binary files /dev/null and b/psprecomp/games/dissidia/d1_moves/p_fiv100_zako.gmo.npd differ diff --git a/psprecomp/games/dissidia/d1_moves/voice_p_fiv100_00.scd.npd b/psprecomp/games/dissidia/d1_moves/voice_p_fiv100_00.scd.npd new file mode 100644 index 0000000..8e09f7e Binary files /dev/null and b/psprecomp/games/dissidia/d1_moves/voice_p_fiv100_00.scd.npd differ diff --git a/psprecomp/games/dissidia/docs/character-mods.md b/psprecomp/games/dissidia/docs/character-mods.md index f6d7709..56f2421 100644 --- a/psprecomp/games/dissidia/docs/character-mods.md +++ b/psprecomp/games/dissidia/docs/character-mods.md @@ -30,6 +30,61 @@ with the add-on 0.32+), Blender (Character mods > Model), or `charmod.py body cl duellum`. It goes on the first costume and its assist; the other costumes keep their own bodies. Weapons are not covered for retail characters yet. +### New moves for a retail character + +A retail folder may also add abilities: `move_names` entries with `"copy"` (as an added character's, see *move_names +fields*) become new 'ablt' records of the retail charId (`roster_mainbin.retail_named`); the retail entries in the same +map are left alone. The build lists each such folder in `/retail_moves.txt`: + +``` +bartz-dff 9 0910,0911,0912,0913,0914,0915,0916,0917 nm=Bartz:_Dissidia_(2008)_moves +``` + +and the runtime (`roster.cpp`, retail moves) reads it at start: +- **on**: the abilities are taught to the character's save record whenever the game fetches it (FUN_088CB164), so a save of + any level has them at once (AP still has to be earned to master them); +- **off** (`#off# ` in front of the line; **Settings > Mods** switches it, at the next start): the records are hidden and + inert the way `"disabled"` makes them (owner 0xFE, command -2), so a save that learnt or equipped them stays valid (an + equipped slot shows empty). The folder's files stay installed: only the new commands use what they add. + +A rebuild keeps a line's `#off#`. A folder deleted or set `"enabled": false` keeps its new abilities as disabled records +(from the previous `extra_abilities.json`), for the same reason. Settings > Mods shows one row per line instead of the +folder's own row. + +**Bartz's Dissidia (2008) moves** (`mods/bartz-dff`, made by `tools/d1_moves.py` from your own two discs): Ascendant +Lance, Climbarrel, Storm Shot, Slidehazzard, Soul Eater, Flood, Wind Shear and Paladin Force, the eight moves 012 took +away, as abilities 0910..0917 next to his 012 kit. + +``` +tools/d1_moves.py --d1 "Dissidia Final Fantasy (USA).iso" --iso012 "Dissidia 012 (USA).iso" # writes mods/bartz-dff +``` + +Releases: the wizard's Options page has **Bartz: Dissidia 1 moves**, which asks for the Dissidia 1 disc image and runs +`d1_moves.py install`. Nothing from either disc ships: `games/dissidia/d1_moves/` holds patches (176 KB, the same +NPD1 format as the online deltas) whose sources are the player's 012 files, their Dissidia 1 files and D1's moveset +run through `d1_objx.py` there; every result is checked against `manifest.json` (SHA-256), D1's help texts are read +from their disc at install. After a change to the mod, regenerate them from a built mods dir: +`d1_moves.py patches --d1 D1.iso --mods work/mods --disc work/disc0` (and `netpack.py record` for online play). Tested 16 +Nov 2026: an install into an empty game folder rebuilt all files byte for byte as the dev build (3.5 s). + +It converts D1's moveset to the 012 layout (`tools/d1_objx.py`: action records 112 -> 116 bytes, the command table, +hits 60 -> 68 bytes with 012's hit-stop bytes filled in, projectile parameters from floats to the float pool, effect +placements 248 -> 260 bytes, and the six event ops whose value count grew, learnt from the moves both games share), then +runs the Move Studio's copy-a-move headless (`tools/d1_moves_studio.js`: Node.js + playwright-core + Chrome) and adds the +names, help texts, CP / AP, learn levels and D1's voice lines for the slots 012's bank left empty (same slot numbers in +both banks). Tested 16 Nov 2026 on the USA discs: every move plays with its animation, hits, effects, banner and damage +(test driver `action 3024..3039`), the Abilities screen lists them, a pressed Soul Eater plays in Quick Battle, and the +switch hides and restores them. + +Online: players who don't have the mod receive it from the player who uses it, verified against a compiled-in list (see +[multiplayer.md](multiplayer.md), *Mods that change a character*). + +Move Studio changes made for it (help every copy): the source's voices and face textures come along when it is the same +character (a mod folder whose `character.based_on` is the target's code); an effect placement bound to the animation it +belongs to (FEPA +0xC8, which the game plays and ends with that animation) is bound to the copied animation instead of +cleared (cleared, a looping hand effect outlived its move), and every placement of the effect id bound to a copied +animation comes along; copied animations get their whole name (`cpSetChunkName`). + ## A character folder (`mods//`) | Path | What | Edit it to | diff --git a/psprecomp/games/dissidia/docs/multiplayer.md b/psprecomp/games/dissidia/docs/multiplayer.md index 04af54b..7126ac6 100644 --- a/psprecomp/games/dissidia/docs/multiplayer.md +++ b/psprecomp/games/dissidia/docs/multiplayer.md @@ -12,7 +12,7 @@ part is the frame-rate rule in `runtime/framerate.cpp`. Open Communications mode in the menu and challenge a player as on a PSP. Nothing else needs turning on: the network layer starts the first time the game uses it. Players need the same game version and character set (do not mix a -modded roster with an unmodded one). +modded roster with an unmodded one). Packs listed below are the exception: they are sent to players who lack them. ### Internet (the default) @@ -128,6 +128,42 @@ Details worth knowing when changing it: - Struct layouts follow PPSSPP: scan info 28 bytes, peer info 152 bytes, PDP stat 20, PTP stat 36 (the last two are unchecked against the game). +## Mods that change a character (verified packs) + +A player who uses a known pack (today: Bartz's Dissidia 1 moves, `mods/bartz-dff`) can play online with players who +never installed it: as soon as both are in the same lobby, the other game fetches what it needs from that player +(about 760 KB: the moveset, the models and the voice bank, as deltas against the retail 012 files every player has), +caches it in `/.net_cache/`, and uses it for that session. A player's own characters are not changed by it. +Only the player who wants the moves needs the Dissidia 1 disc. + +Nothing a peer sends is trusted. The packs a game accepts are compiled into it (`games/dissidia/net_packs.json` -> +`runtime/net_packs.inc`, written by `tools/netpack.py record`): every file with its size and SHA-256, the SHA-256 of the +retail file the delta applies to, and the abilities. The receiving game applies the delta to its own retail file +(`runtime/net_delta.h`: every copy bounds-checked, output capped at the listed size, inflate capped; host test +`tests/run_net_delta_test.sh`, run under AddressSanitizer with corrupted and hostile deltas), keeps the result only if +size and SHA-256 match, stores it under that hash (never a name a peer sent), and builds the ability records itself +from the list. So a peer can make another game use exactly the listed files and abilities, or nothing. + +Who may start a match (`net_packs.cpp` `allow_match`, through the ad-hoc layer's match filter): a player who uses a +pack this game does not know (another version, an edited mod, a build that is not the listed one) or has not been +received and verified yet is refused, and a player who uses a pack only plays with players who confirmed having it. A +refused player is not shown in the lobby. Two players without packs play as before. + +Transport: a side channel of the ad-hoc layer (PDP to the reserved port 0xD1D1, `psp_adhoc_side_*`), so the party +server needs no change. Messages: a manifest every second (packs in use, packs ready), requests and 1 KB pieces of a +delta (24 in flight, resent after 0.4 s, given up after 90 s). A sender serves only the deltas `netpack.py deltas` +wrote (run by `charmod.py build`) for packs whose files it has installed byte for byte (`/.net/.dlt`). + +Tested 16 Nov 2026 with two instances in LAN mode: A with the pack and Ascendant Lance / Soul Eater equipped, B with an +empty mods folder. B fetched and verified all six files in the lobby within seconds, the match started, A picked Bartz +and B Lightning, and both screens showed the same Brave and HP values through Ascendant Lance and Soul Eater. With +one of A's deltas altered (valid format, different bytes), B threw that file away and the two players no longer saw +each other in the lobby. Not done yet: an on-screen note telling a refused player why. + +Adding a pack: build the mod, `netpack.py record --version N` (updates the JSON and the .inc), rebuild +the game. The recorded hashes must be what `d1_moves.py` / the mod build makes on every machine (they are deterministic +for the same discs: rebuilding from scratch gave identical files). + ## The party server `tools/adhoc_relay/` ([README](../../../../tools/adhoc_relay/README.md)) is a small C++ UDP relay. A client registers into a diff --git a/psprecomp/games/dissidia/features.ini b/psprecomp/games/dissidia/features.ini index 267053a..207a06b 100644 --- a/psprecomp/games/dissidia/features.ini +++ b/psprecomp/games/dissidia/features.ini @@ -1,4 +1,5 @@ hd_textures=1 jp_audio=1 +d1_moves=1 android=0 steam=1 diff --git a/psprecomp/games/dissidia/net_packs.json b/psprecomp/games/dissidia/net_packs.json new file mode 100644 index 0000000..cf16503 --- /dev/null +++ b/psprecomp/games/dissidia/net_packs.json @@ -0,0 +1,112 @@ +{ + "_info": "A player who uses such a mod sends the other players what their game needs to show it (the changed moveset, models and\nvoice bank), as deltas against the retail 012 files every player already has. Nothing a peer sends is trusted:", + "packs": [ + { + "name": "bartz-dff", + "version": 1, + "char": 9, + "files": [ + { + "path": "obj/p_fiv100.objx", + "size": 1983820, + "sha256": "a7a67dd29f5067ac5a9b707b5b428a652a1e53427d52e884f28414f68bdd7a15", + "base_size": 1559788, + "base_sha256": "d71567ebd9fab3a50ab725fb5db44253693e1dafc1cde7a890777953ad4e9a83" + }, + { + "path": "obj/p_fiv100_1p.gmo", + "size": 2462188, + "sha256": "9a17294ec50482f2f1293700a72d3e75f935df673494d9a95c5b7f66716eeafb", + "base_size": 2102308, + "base_sha256": "1b843a3e44fcc32e5ac6ac86b669cb5a14494f7121528148be3c48d60b5eaab5" + }, + { + "path": "obj/p_fiv100_2p.gmo", + "size": 2462188, + "sha256": "c54dbeb66a6ec284aded5bed724260cc0e7aceb51a0744c6ba0232e085093c0f", + "base_size": 2102308, + "base_sha256": "592e67b8c6fcac7300e80ca8a32fa5cbbaa756b729782b9b1fe80ef1c4096532" + }, + { + "path": "obj/p_fiv100_3p.gmo", + "size": 2470132, + "sha256": "b86bc46bb92bc4f4a6ad9fa5c4fa583996d4c8db970b5bc8294d14431d49dfc9", + "base_size": 2110252, + "base_sha256": "43da35210e9dd7e4974400cd194eb22c426f7667f1364779aec71d49057d09c7" + }, + { + "path": "obj/p_fiv100_zako.gmo", + "size": 2443668, + "sha256": "c0dbcc2c5560b87c0b8272f837ec2d800c0605af071438acd8a35e665c239b5c", + "base_size": 2083788, + "base_sha256": "89422d7152374b0e49c9e5595b1364ebbdf60bbe50d392e9e78a626fbafc8cbf" + }, + { + "path": "voice/en/battle/voice_p_fiv100_00.scd", + "size": 357600, + "sha256": "6b3709f8f3f959495e8672ed5608ecb7bd6bf95572d680b6c6c4e84fdba1a8cc", + "base_size": 302368, + "base_sha256": "9f67e60176757f67a7f867d35082d0a0c464e3373deb2feb1c036c788caa7946" + } + ], + "abilities": [ + { + "id": "0x0910", + "copy": "0x090C", + "cmd": "0xB0", + "type": 0, + "phase": 0 + }, + { + "id": "0x0911", + "copy": "0x090C", + "cmd": "0xB1", + "type": 0, + "phase": 0 + }, + { + "id": "0x0912", + "copy": "0x090D", + "cmd": "0xB2", + "type": 0, + "phase": 1 + }, + { + "id": "0x0913", + "copy": "0x090D", + "cmd": "0xB3", + "type": 0, + "phase": 1 + }, + { + "id": "0x0914", + "copy": "0x090E", + "cmd": "0xB4", + "type": 1, + "phase": 0 + }, + { + "id": "0x0915", + "copy": "0x090E", + "cmd": "0xB5", + "type": 1, + "phase": 0 + }, + { + "id": "0x0916", + "copy": "0x090F", + "cmd": "0xB6", + "type": 1, + "phase": 1 + }, + { + "id": "0x0917", + "copy": "0x090F", + "cmd": "0xB7", + "type": 1, + "phase": 1 + } + ] + } + ] +} diff --git a/psprecomp/games/dissidia/runtime/hooks_index.cpp b/psprecomp/games/dissidia/runtime/hooks_index.cpp index 5a4720a..355c52a 100644 --- a/psprecomp/games/dissidia/runtime/hooks_index.cpp +++ b/psprecomp/games/dissidia/runtime/hooks_index.cpp @@ -165,6 +165,26 @@ void find_hook(uint8_t* rdram, recomp_context* ctx) { } // namespace +// The game's own copy of the index (retail buffer or the relocated block): rewrite the entry of `crc` in place (the +// lookup's one-entry cache holds a pointer to it, so it sees the change too). False when the index is not loaded yet +// or has no such entry. Game thread only. +bool dissidia_index_set_entry(uint8_t* rdram, uint32_t crc, uint32_t sector, uint32_t size_flags) { + const uint32_t cnt = rd32(rdram, g_base + 8); + if (rd32(rdram, g_base) != dissidia::kIndexMagic || cnt == 0 || cnt > 0x10000u) return false; + int32_t lo = 0, hi = static_cast(cnt) - 1; + while (lo <= hi) { + const int32_t mid = (lo + hi) / 2; + const uint32_t e = g_base + 16u + 12u * static_cast(mid), cur = rd32(rdram, e); + if (cur == crc) { + psp_mem_write(rdram, e + 4, sector); + psp_mem_write(rdram, e + 8, size_flags); + return true; + } + if (cur < crc) lo = mid + 1; else hi = mid - 1; + } + return false; +} + void dissidia_index_install() { const char* off = std::getenv("PSPRECOMP_INDEX_EXTENDED"); if (off && off[0] == '0') { std::fprintf(stderr, "[MODS] package index: extension disabled (PSPRECOMP_INDEX_EXTENDED=0), retail ceiling\n"); return; } diff --git a/psprecomp/games/dissidia/runtime/hooks_io.cpp b/psprecomp/games/dissidia/runtime/hooks_io.cpp index 7841db9..02fc3e4 100644 --- a/psprecomp/games/dissidia/runtime/hooks_io.cpp +++ b/psprecomp/games/dissidia/runtime/hooks_io.cpp @@ -13,6 +13,7 @@ // redirect needs no knowledge of the engine's data structures. #include "package_mods.h" +#include "voice_merge.h" #include "psp_quit.h" #include "psp_platform.h" #include "psp_settings.h" @@ -30,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -54,6 +56,8 @@ enum { TAG_NONE = 0, TAG_INDEX = 1, TAG_PACKAGE = 2, TAG_OVL_BASE = 10 }; // T dissidia::PackageMods g_mods; +std::recursive_mutex g_mods_mu; // g_mods can change while the game runs (net_packs.cpp adds a pack received online) +bool g_net_possible = false; // packs may arrive online: keep PACKAGE.BIN tagged even with no mods (dissidia_mods_net_possible) bool g_built = false; bool g_active = false; int g_trace_left = 0; @@ -267,12 +271,13 @@ void ensure_built(const std::string& host_info_path) { std::string dir = mods_dir(); std::vector extra = mod_roots(); // installed mod folders that are on, then the voice pack for (const std::string& r : extra) std::fprintf(stderr, "[MODS] mod folder on: %s\n", r.c_str()); + std::string jp_dir; // the Japanese voice pack, when on and present if (voice_jp_on()) { const std::string vd = work_parent() + "/voice_jp"; - if (is_dir(vd)) { extra.push_back(vd); std::fprintf(stderr, "[VOICE] Japanese voice pack: %s\n", vd.c_str()); } + if (is_dir(vd)) { extra.push_back(vd); jp_dir = vd; std::fprintf(stderr, "[VOICE] Japanese voice pack: %s\n", vd.c_str()); } else std::fprintf(stderr, "[VOICE] Japanese voice selected but %s does not exist (run tools/jp_audio.py): English voices\n", vd.c_str()); } - if (!is_dir(dir) && extra.empty()) { + if (!is_dir(dir) && extra.empty() && !g_net_possible) { std::fprintf(stderr, "[MODS] no mods directory (%s): loose-file overrides off\n", dir.c_str()); return; } @@ -287,7 +292,37 @@ void ensure_built(const std::string& host_info_path) { std::ifstream f(host_info_path, std::ios::binary); std::vector orig((std::istreambuf_iterator(f)), std::istreambuf_iterator()); std::string log, err; - if (!g_mods.build(orig, (uint64_t)pst.st_size, dir, &log, &err, g_index_capacity, extra)) { + // Japanese voices + a mod that changes a battle voice bank: the Japanese bank with the mod's added clips, served + // ahead of both (voice_merge.h); without this the mod's English-based bank would win over voice_jp/ + std::string first = dir; + std::vector rest = extra; + if (!jp_dir.empty() && is_dir(dir + "/voice")) { + namespace fs = std::filesystem; + const std::string merged = dir + "/.voice_jp_merged"; + int banks = 0; + std::error_code ec; + for (auto it = fs::recursive_directory_iterator(dir + "/voice", ec); !ec && it != fs::recursive_directory_iterator(); it.increment(ec)) { + if (!it->is_regular_file(ec) || it->path().extension() != ".scd") continue; + const std::string rel = fs::relative(it->path(), dir, ec).generic_string(); + std::vector mod, jp, out; + int added = 0; + auto rd = [](const std::string& p, std::vector* b) { + std::ifstream in(p, std::ios::binary); + if (!in) return false; + b->assign((std::istreambuf_iterator(in)), std::istreambuf_iterator()); + return true; + }; + if (!rd(jp_dir + "/" + rel, &jp) || !rd(it->path().string(), &mod)) continue; + if (!dissidia::merge_voice_bank(jp, mod, &out, &added)) continue; + fs::create_directories(fs::path(merged + "/" + rel).parent_path(), ec); + std::ofstream o(merged + "/" + rel, std::ios::binary | std::ios::trunc); + o.write(reinterpret_cast(out.data()), (std::streamsize)out.size()); + banks++; + std::fprintf(stderr, "[VOICE] %s: Japanese bank + %d clip(s) the mod adds (no Japanese recording of them)\n", rel.c_str(), added); + } + if (banks) { first = merged; rest.insert(rest.begin(), dir); } + } + if (!g_mods.build(orig, (uint64_t)pst.st_size, first, &log, &err, g_index_capacity, rest)) { std::fprintf(stderr, "[MODS] cannot parse %s: %s (overrides off)\n", host_info_path.c_str(), err.c_str()); return; } @@ -333,7 +368,7 @@ int classify_open(const char* psp_path) { const size_t slash = host.find_last_of('/'); if (slash != std::string::npos) g_data_dir = host.substr(0, slash); } - if (!g_active) return (pkg && !asset_patches().empty()) ? TAG_PACKAGE : TAG_NONE; + if (!g_active) return (pkg && (!asset_patches().empty() || g_net_possible)) ? TAG_PACKAGE : TAG_NONE; return info ? TAG_INDEX : TAG_PACKAGE; } @@ -358,6 +393,7 @@ bool overlay_read(int tag, int host_fd, int64_t off, uint32_t size, uint8_t* des } return got; }; + std::lock_guard mods_lock(g_mods_mu); if (tag == TAG_PACKAGE && !asset_patches().empty() && !g_active) { // no loose-file mods: plain read plus the built-in edits const int64_t got = base(dest, size, off); *nread = (uint32_t)std::max(got, 0); @@ -389,6 +425,26 @@ bool overlay_read(int tag, int host_fd, int64_t off, uint32_t size, uint8_t* des std::string dissidia_mods_dir() { return mods_dir(); } std::vector dissidia_mod_roots() { return mod_roots(); } +// the Japanese voice pack folder when Japanese voices are on and the pack exists, else "" (net_packs.cpp merges a +// received voice bank into it, as ensure_built does for installed ones) +std::string dissidia_voice_jp_dir() { const std::string vd = work_parent() + "/voice_jp"; return voice_jp_on() && is_dir(vd) ? vd : std::string(); } +// net_packs.cpp, at registration (before the game opens PACKAGE.BIN): packs may arrive online, so PACKAGE.BIN reads go +// through the override layer even when no mod is installed (it serves nothing until a pack is added). +void dissidia_mods_net_possible() { g_net_possible = true; } +// net_packs.cpp, game thread: serve `file` for `asset` from now on (a verified pack received online). The entry moves to +// fresh virtual sectors past every other override, in the patched index and in the game's in-memory copy. +extern bool dissidia_index_set_entry(uint8_t* rdram, uint32_t crc, uint32_t sector, uint32_t size_flags); +bool dissidia_mods_add_override(uint8_t* rdram, const std::string& asset, const std::string& file, uint32_t size) { + std::lock_guard lk(g_mods_mu); + if (!g_built) return false; + dissidia::IndexEntry e; + if (!g_mods.add_override(asset, file, size, &e)) return false; + if (!dissidia_index_set_entry(rdram, e.crc, e.sector, e.size_flags)) return false; + g_active = true; + { std::lock_guard l2(g_lt_mu); g_by_sector.clear(); } + std::fprintf(stderr, "[MODS] online pack: %s now served from %s (%u B, sector %u)\n", asset.c_str(), file.c_str(), size, e.sector); + return true; +} std::string dissidia_package_dir() { return g_data_dir; } void dissidia_add_asset_patch(const std::string& asset, std::function&)> edit) { AssetPatch p; diff --git a/psprecomp/games/dissidia/runtime/hooks_main.cpp b/psprecomp/games/dissidia/runtime/hooks_main.cpp index e47433d..432feeb 100644 --- a/psprecomp/games/dissidia/runtime/hooks_main.cpp +++ b/psprecomp/games/dissidia/runtime/hooks_main.cpp @@ -56,6 +56,7 @@ extern void dissidia_roster_init(uint8_t* rdram); extern void dissidia_roster_boot_patch(uint8_t* rdram); extern void dissidia_roster_grid_install(); extern void dissidia_wpnp_install(); +extern void dissidia_net_packs_install(); // camera.cpp: analogue right-stick battle camera. extern void dissidia_camera_install(uint8_t* rdram); extern void dissidia_menu_settings_install(uint8_t* rdram); @@ -317,6 +318,7 @@ void dissidia_register_hooks(uint8_t* rdram) { dissidia_models_install(); // model staging budget (hooks_models.cpp) dissidia_roster_init(rdram); // extra roster slots (roster.cpp) dissidia_wpnp_install(); // battle.bin wpnp table bigger than its retail buffer (roster.cpp) + dissidia_net_packs_install(); // verified packs from online players (net_packs.cpp) if (std::getenv("PSPRECOMP_TRACE_ATAN")) { g_orig_atan = psp_dispatch_probe_lookup(0x089A457Cu); if (g_orig_atan) psp_dispatch_register(0x089A457Cu, dissidia_atan_trace); } if (std::getenv("PSPRECOMP_TRACE_BONE")) { g_orig_bone = psp_dispatch_probe_lookup(0x089B2974u); if (g_orig_bone) psp_dispatch_register(0x089B2974u, dissidia_bone_trace); } if (std::getenv("PSPRECOMP_TRACE_POOL")) { diff --git a/psprecomp/games/dissidia/runtime/net_delta.h b/psprecomp/games/dissidia/runtime/net_delta.h new file mode 100644 index 0000000..3d55c26 --- /dev/null +++ b/psprecomp/games/dissidia/runtime/net_delta.h @@ -0,0 +1,58 @@ +// games/dissidia/runtime/net_delta.h — the delta decoder of online packs (net_packs.cpp; written by tools/netpack.py). +// It reads bytes another player sent, before anything is verified: every bound is checked, the output never grows past +// `want` bytes and every copy stays inside `src`. Host test: tests/test_net_delta.cpp. +// Format: "NPD1", u32 target size, u32 source size, zlib stream of ops: 0x00 COPY {u32 src off, u32 len}, 0x01 ADD {u32 len, bytes}. +#pragma once +#include + +#include +#include +#include + +namespace dissidia { + +inline bool apply_delta(const std::vector& delta, const std::vector& src, uint32_t want, std::vector* out) { + if (delta.size() < 12 || std::memcmp(delta.data(), "NPD1", 4) != 0) return false; + uint32_t tsize, ssize; + std::memcpy(&tsize, delta.data() + 4, 4); + std::memcpy(&ssize, delta.data() + 8, 4); + if (tsize != want || ssize != src.size()) return false; + // inflate the op stream: at most twice the target plus slack (ADD bytes plus op headers); more = not ours + const uLong cap = (uLong)want * 2 + 65536; + std::vector ops(cap); + z_stream z{}; + if (inflateInit(&z) != Z_OK) return false; + z.next_in = const_cast(delta.data() + 12); + z.avail_in = (uInt)(delta.size() - 12); + z.next_out = ops.data(); + z.avail_out = (uInt)cap; + const int r = inflate(&z, Z_FINISH); + const size_t nops = cap - z.avail_out; + inflateEnd(&z); + if (r != Z_STREAM_END) return false; + out->clear(); + out->reserve(want); + size_t p = 0; + while (p < nops) { + const uint8_t op = ops[p]; + if (op == 0 && p + 9 <= nops) { + uint32_t o, n; + std::memcpy(&o, &ops[p + 1], 4); + std::memcpy(&n, &ops[p + 5], 4); + if ((uint64_t)o + n > src.size() || (uint64_t)out->size() + n > want) return false; + out->insert(out->end(), src.begin() + o, src.begin() + o + n); + p += 9; + } else if (op == 1 && p + 5 <= nops) { + uint32_t n; + std::memcpy(&n, &ops[p + 1], 4); + if ((uint64_t)p + 5 + n > nops || (uint64_t)out->size() + n > want) return false; + out->insert(out->end(), ops.begin() + p + 5, ops.begin() + p + 5 + n); + p += 5 + n; + } else { + return false; + } + } + return out->size() == want; +} + +} // namespace dissidia diff --git a/psprecomp/games/dissidia/runtime/net_packs.cpp b/psprecomp/games/dissidia/runtime/net_packs.cpp new file mode 100644 index 0000000..d3eb12b --- /dev/null +++ b/psprecomp/games/dissidia/runtime/net_packs.cpp @@ -0,0 +1,495 @@ +// games/dissidia/runtime/net_packs.cpp — online play with a player who uses a retail-moves mod (Bartz's Dissidia 1 +// moves) when this player doesn't have it: the other player sends what this game needs, as verified packs. +// +// Trust: none in the peer. The packs that may be received are compiled in (net_packs.inc, from +// games/dissidia/net_packs.json via tools/netpack.py): every file with its size and SHA-256, the SHA-256 of the retail +// file its delta applies to, and the abilities. A peer only ever sends delta bytes. This game +// * applies a delta (bounds-checked: every copy inside its own retail file, the output never past the listed size) +// to its OWN retail file, and keeps the result only when size and SHA-256 are the listed ones; +// * stores it as /.net_cache/.bin, a name made here from the list, never from what the peer sent; +// * makes the ability records itself, from the list (a copy of its own record the list names, the listed command); +// * refuses to start a match with a player who uses a pack it does not know (another version, an edited mod, a pack +// the player cannot share) or has not received and verified yet, and with a player who has not confirmed having +// the packs this player uses (an older build never confirms: no match with it while a pack is in use). +// So a peer can make this game use exactly the listed files and abilities, or nothing. +// +// Wire (the ad-hoc side channel, psp_adhoc_side_*; little endian): +// 'M' 1 nuse {name[24] u16 version u8 state(1 = shareable, 2 = in use, not shareable)}* nready {name[24] u16 version}* +// 'R' sha256[32] u32 offset u16 length request a piece of a delta (at most kChunk bytes) +// 'D' sha256[32] u32 total u32 offset bytes a piece of a delta (total = the delta's size) +// A sender serves only the deltas tools/netpack.py wrote for packs whose files it has installed byte for byte +// (/.net/.dlt). +#include "psp_game_module.h" +#include "hle/psp_hle_adhoc.h" +#include "package_mods.h" +#include "net_delta.h" +#include "voice_merge.h" +#include "recomp.h" + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace psp_cloud { std::string sha256_hex(const std::vector& data); } +std::string dissidia_mods_dir(); +std::string dissidia_package_dir(); +std::string dissidia_voice_jp_dir(); +void dissidia_mods_net_possible(); +bool dissidia_mods_add_override(uint8_t* rdram, const std::string& asset, const std::string& file, uint32_t size); +int dissidia_retail_moves_state(const std::string& folder); +bool dissidia_ablt_ensure(uint8_t* rdram, uint16_t id, uint16_t copy, uint16_t cmd, uint8_t type, uint8_t phase, uint8_t owner); + +namespace { + +struct NetFile { const char* path; uint32_t size; const char* sha; uint32_t base_size; const char* base_sha; }; +struct NetAbil { uint16_t id, copy, cmd; uint8_t type, phase; }; +struct NetPack { const char* name; int version; int charId; std::vector files; std::vector abilities; }; +const std::vector& packs() { + static const std::vector v = { +#include "net_packs.inc" + }; + return v; +} + +using Mac = std::array; +constexpr size_t kName = 24, kChunk = 1024, kWindow = 24; +constexpr uint32_t kMaxDelta = 8u << 20; +constexpr int64_t kResendUs = 400000, kGiveUpUs = 90000000, kPeerGoneUs = 10000000, kManifestUs = 1000000; +const Mac kBcast{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; + +int64_t now_us() { + return std::chrono::duration_cast(std::chrono::steady_clock::now().time_since_epoch()).count(); +} +std::string mac_str(const Mac& m) { + char b[24]; + std::snprintf(b, sizeof b, "%02x:%02x:%02x:%02x:%02x:%02x", m[0], m[1], m[2], m[3], m[4], m[5]); + return b; +} +bool read_file(const std::string& p, std::vector* out, uint64_t max) { + struct stat st; + if (::stat(p.c_str(), &st) != 0 || (uint64_t)st.st_size > max) return false; + std::ifstream f(p, std::ios::binary); + out->assign((std::istreambuf_iterator(f)), std::istreambuf_iterator()); + return f.good() || f.eof(); +} +std::string hex(const uint8_t* p, size_t n) { + std::string s; + char b[3]; + for (size_t i = 0; i < n; i++) { std::snprintf(b, sizeof b, "%02x", p[i]); s += b; } + return s; +} +bool unhex(const char* h, uint8_t out[32]) { + for (int i = 0; i < 32; i++) { + unsigned v = 0; + if (std::sscanf(h + 2 * i, "%2x", &v) != 1) return false; + out[i] = (uint8_t)v; + } + return true; +} + +// ---- state (g_mu; never held while calling into the ad-hoc layer, which calls the handler and filter with its own lock) ---- +struct Peer { + int64_t seen = 0; + bool manifest = false; + std::vector> uses; // (pack index or -1 = unknown to this build, state) + std::set ready; // packs the peer says it has +}; +struct Download { + int pack = -1, file = -1; + Mac from{}; + std::vector data; + uint32_t total = 0; // 0 = not known yet (first piece not received) + std::vector got; // per piece + std::map asked; // piece -> when requested + int64_t started = 0; +}; +std::mutex g_mu; +std::map g_peers; +std::deque>> g_inbox; +std::vector g_file_ok; // per (pack, file) flattened: -1 unknown, 0 no, 1 installed, 2 cached +std::vector g_pack_state; // per pack: 0 not ready, 1 ready, 2 failed (a received file did not verify) +std::vector g_want_active, g_active; // a peer in the group uses it / applied in this session +std::vector g_share; // this player uses it and can send it +std::vector g_use; // this player uses it: 1 shareable, 2 not shareable, 0 no +std::map> g_serve; // sha -> delta bytes this player sends +std::vector g_dl; +std::atomic g_started{false}; +std::atomic g_activate_pending{false}; +Mac g_own{}; + +size_t file_slot(int p, int f) { + size_t n = 0; + for (int i = 0; i < p; i++) n += packs()[i].files.size(); + return n + (size_t)f; +} +std::string cache_dir() { return dissidia_mods_dir() + "/.net_cache"; } +std::string cache_path(const NetFile& f) { return cache_dir() + "/" + f.sha + ".bin"; } +std::string installed_path(const NetFile& f) { return dissidia_mods_dir() + "/" + f.path; } + +// a file is available when this player's mods dir has it byte for byte, or a verified copy is in the cache (hashed +// again on first use: the cache is a plain folder) +int check_file(const NetFile& f) { + std::vector b; + if (read_file(installed_path(f), &b, f.size) && b.size() == f.size && psp_cloud::sha256_hex(b) == f.sha) return 1; + if (read_file(cache_path(f), &b, f.size) && b.size() == f.size && psp_cloud::sha256_hex(b) == f.sha) return 2; + return 0; +} + +void refresh_local() { // worker, once: what this player has and uses + const auto& P = packs(); + size_t nf = 0; + for (const auto& p : P) nf += p.files.size(); + std::vector ok(nf, 0), st(P.size(), 0), use(P.size(), 0); + std::vector share(P.size(), false); + std::map> serve; + for (size_t i = 0; i < P.size(); i++) { + bool all = true, installed = true; + for (size_t f = 0; f < P[i].files.size(); f++) { + const int c = check_file(P[i].files[f]); + ok[file_slot((int)i, (int)f)] = c; + all = all && c != 0; + installed = installed && c == 1; + } + st[i] = all ? 1 : 0; + if (dissidia_retail_moves_state(P[i].name) != 1) continue; // this player does not use it + bool deltas = installed; + for (const auto& f : P[i].files) { + std::vector d; + if (!deltas) break; + if (!read_file(dissidia_mods_dir() + "/.net/" + f.sha + ".dlt", &d, kMaxDelta) || d.size() < 12) { deltas = false; break; } + serve[f.sha] = std::move(d); + } + share[i] = deltas; + use[i] = deltas ? 1 : 2; + std::fprintf(stderr, "[NETPACK] %s v%d: in use here, %s\n", P[i].name, P[i].version, + deltas ? "can be sent to players who don't have it" : "NOT shareable (this build differs from the known one): " + "players without it cannot play this player online while it is on"); + } + std::lock_guard lk(g_mu); + g_file_ok = ok; g_pack_state = st; g_use = use; g_share = share; g_serve = std::move(serve); + g_want_active.assign(P.size(), false); + g_active.assign(P.size(), false); +} + +int pack_by_name(const uint8_t* name, uint16_t ver) { + char n[kName + 1]; + std::memcpy(n, name, kName); + n[kName] = 0; + for (size_t i = 0; i < packs().size(); i++) + if (std::strcmp(packs()[i].name, n) == 0 && packs()[i].version == ver) return (int)i; + return -1; +} +void put_name(std::vector& b, const char* name, int ver) { + uint8_t n[kName] = {0}; + std::strncpy(reinterpret_cast(n), name, kName - 1); + b.insert(b.end(), n, n + kName); + b.push_back(ver & 0xff); b.push_back((ver >> 8) & 0xff); +} +template void put(std::vector& b, T v) { const uint8_t* p = reinterpret_cast(&v); b.insert(b.end(), p, p + sizeof v); } + +std::vector manifest() { // g_mu held + std::vector b{'M', 1}; + std::vector uses; + for (size_t i = 0; i < g_use.size(); i++) if (g_use[i]) uses.push_back((int)i); + b.push_back((uint8_t)uses.size()); + for (int i : uses) { put_name(b, packs()[i].name, packs()[i].version); b.push_back((uint8_t)g_use[i]); } + std::vector ready; + for (size_t i = 0; i < g_pack_state.size(); i++) if (g_pack_state[i] == 1 && (g_active[i] || g_use[i])) ready.push_back((int)i); + b.push_back((uint8_t)ready.size()); + for (int i : ready) put_name(b, packs()[i].name, packs()[i].version); + return b; +} + +// ---- network thread: copy and queue only ---- +void on_side(const uint8_t mac[6], const uint8_t* data, size_t len) { + if (!len || len > PSP_ADHOC_SIDE_MAX) return; + Mac m; + std::memcpy(m.data(), mac, 6); + std::lock_guard lk(g_mu); + if (g_inbox.size() > 4096) return; // a flood: drop + g_inbox.emplace_back(m, std::vector(data, data + len)); +} + +// may a match with this player start? (network thread, called with the ad-hoc lock held: only reads state) +bool allow_match(const uint8_t mac[6]) { + Mac m; + std::memcpy(m.data(), mac, 6); + std::lock_guard lk(g_mu); + bool i_use = false; + for (int u : g_use) i_use = i_use || u != 0; + auto it = g_peers.find(m); + if (it == g_peers.end() || !it->second.manifest) return !i_use; // unknown player: fine only while no pack is in use here + const Peer& p = it->second; + for (auto [idx, state] : p.uses) { + if (idx < 0 || state != 1) return false; // unknown here, or the player's build is not the listed one + if (!g_use[idx] && !(g_pack_state[idx] == 1 && g_active[idx])) return false; // not received / applied yet + } + for (size_t i = 0; i < g_use.size(); i++) + if (g_use[i] && !p.ready.count((int)i)) return false; // the player has not confirmed having mine + return true; +} + +// ---- worker ---- +void handle(const Mac& from, const std::vector& b, std::vector>>& out) { // g_mu held + const int64_t t = now_us(); + if (b[0] == 'M' && b.size() >= 4 && b[1] == 1) { + Peer& p = g_peers[from]; + p.seen = t; + p.manifest = true; + p.uses.clear(); + p.ready.clear(); + size_t o = 2; + const size_t nuse = b[o++]; + for (size_t k = 0; k < nuse; k++) { + if (o + kName + 3 > b.size()) return; + uint16_t ver = b[o + kName] | (b[o + kName + 1] << 8); + p.uses.emplace_back(pack_by_name(&b[o], ver), b[o + kName + 2]); + o += kName + 3; + } + if (o >= b.size()) return; + const size_t nready = b[o++]; + for (size_t k = 0; k < nready; k++) { + if (o + kName + 2 > b.size()) return; + const int idx = pack_by_name(&b[o], (uint16_t)(b[o + kName] | (b[o + kName + 1] << 8))); + if (idx >= 0) p.ready.insert(idx); + o += kName + 2; + } + for (auto [idx, state] : p.uses) { + if (idx < 0) continue; + g_want_active[idx] = true; + if (g_pack_state[idx] == 0 && state == 1) { // fetch from this player what is missing + for (size_t f = 0; f < packs()[idx].files.size(); f++) { + if (g_file_ok[file_slot(idx, (int)f)] != 0) continue; + bool have = false; + for (auto& d : g_dl) have = have || (d.pack == idx && d.file == (int)f); + if (have) continue; + Download d; + d.pack = idx; d.file = (int)f; d.from = from; d.started = t; + g_dl.push_back(d); + std::fprintf(stderr, "[NETPACK] %s: fetching %s from %s\n", packs()[idx].name, packs()[idx].files[f].path, mac_str(from).c_str()); + } + } else if (g_pack_state[idx] == 0 && state != 1 && !g_use[idx]) { + std::fprintf(stderr, "[NETPACK] %s uses %s but cannot send it: no match with that player\n", mac_str(from).c_str(), packs()[idx].name); + } + } + if (g_pack_state.size()) for (auto [idx, state] : p.uses) if (idx < 0) { + std::fprintf(stderr, "[NETPACK] %s uses a pack this build does not know: no match with that player\n", mac_str(from).c_str()); + break; + } + if (g_activate_pending.load() == false) for (size_t i = 0; i < g_want_active.size(); i++) + if (g_want_active[i] && g_pack_state[i] == 1 && !g_active[i] && !g_use[i]) g_activate_pending = true; + return; + } + if (b[0] == 'R' && b.size() == 1 + 32 + 4 + 2) { + const std::string sha = hex(&b[1], 32); + auto it = g_serve.find(sha); + if (it == g_serve.end()) return; // not something this player sends + uint32_t off; uint16_t n; + std::memcpy(&off, &b[33], 4); + std::memcpy(&n, &b[37], 2); + const std::vector& d = it->second; + if (off >= d.size()) return; + n = (uint16_t)std::min({(size_t)n, kChunk, d.size() - off}); + std::vector r{'D'}; + r.insert(r.end(), &b[1], &b[33]); + put(r, (uint32_t)d.size()); + put(r, off); + r.insert(r.end(), d.begin() + off, d.begin() + off + n); + out.emplace_back(from, std::move(r)); + return; + } + if (b[0] == 'D' && b.size() > 1 + 32 + 8) { + uint8_t want[32]; + for (auto& dl : g_dl) { + if (dl.from != from) continue; + if (!unhex(packs()[dl.pack].files[dl.file].sha, want) || std::memcmp(want, &b[1], 32) != 0) continue; + uint32_t total, off; + std::memcpy(&total, &b[33], 4); + std::memcpy(&off, &b[37], 4); + const size_t n = b.size() - 41; + if (total < 12 || total > kMaxDelta || (dl.total && total != dl.total) || off % kChunk || n > kChunk || + (uint64_t)off + n > total || (off + n != total && n != kChunk)) return; // not the shape a sender makes + if (!dl.total) { + dl.total = total; + dl.data.assign(total, 0); + dl.got.assign((total + kChunk - 1) / kChunk, 0); + } + const uint32_t piece = off / kChunk; + std::memcpy(dl.data.data() + off, &b[41], n); + dl.got[piece] = 1; + dl.asked.erase(piece); + return; + } + } +} + +// a finished delta: rebuild the file from this player's own retail file and keep it only if it is the listed one +int finish(Download& dl) { // g_mu NOT held (reads files) + const NetFile& f = packs()[dl.pack].files[dl.file]; + std::vector base, out; + const std::string dir = dissidia_package_dir(); + if (dir.empty() || !dissidia::read_asset(dir, "", f.path, &base)) { + std::fprintf(stderr, "[NETPACK] %s: no retail %s to apply the delta to\n", packs()[dl.pack].name, f.path); + return -1; + } + if (base.size() != f.base_size || psp_cloud::sha256_hex(base) != f.base_sha) { + std::fprintf(stderr, "[NETPACK] %s: this disc's %s is not the known retail file: cannot use the pack\n", packs()[dl.pack].name, f.path); + return -1; + } + if (!dissidia::apply_delta(dl.data, base, f.size, &out) || psp_cloud::sha256_hex(out) != f.sha) { + std::fprintf(stderr, "[NETPACK] %s: %s from %s did NOT verify: thrown away, no match with that player\n", + packs()[dl.pack].name, f.path, mac_str(dl.from).c_str()); + return -1; + } + { + std::error_code ec; // std::filesystem: Windows has no two-argument mkdir + std::filesystem::create_directories(cache_dir(), ec); + } + const std::string tmp = cache_path(f) + ".tmp"; + { + std::ofstream o(tmp, std::ios::binary | std::ios::trunc); + o.write(reinterpret_cast(out.data()), (std::streamsize)out.size()); + if (!o) return -1; + } + if (std::rename(tmp.c_str(), cache_path(f).c_str()) != 0) return -1; + std::fprintf(stderr, "[NETPACK] %s: %s verified (%u B from a %u B delta), cached\n", packs()[dl.pack].name, f.path, f.size, dl.total); + return 2; +} + +void worker() { + refresh_local(); + int64_t next_manifest = 0; + for (;;) { + std::this_thread::sleep_for(std::chrono::milliseconds(30)); + const int64_t t = now_us(); + std::vector>> out; + std::vector done; + uint8_t peers[32][6]; + size_t npeers = 0; + psp_adhoc_group_peers(peers, 32, &npeers, g_own.data()); + { + std::lock_guard lk(g_mu); + while (!g_inbox.empty()) { + auto [m, b] = std::move(g_inbox.front()); + g_inbox.pop_front(); + if (!b.empty()) handle(m, b, out); + } + for (auto it = g_peers.begin(); it != g_peers.end();) + if (t - it->second.seen > kPeerGoneUs) it = g_peers.erase(it); else ++it; + if (npeers && t >= next_manifest) { next_manifest = t + kManifestUs; out.emplace_back(kBcast, manifest()); } + for (auto it = g_dl.begin(); it != g_dl.end();) { + Download& d = *it; + const bool complete = d.total && std::all_of(d.got.begin(), d.got.end(), [](uint8_t g) { return g != 0; }); + if (complete) { done.push_back(std::move(d)); it = g_dl.erase(it); continue; } + if (t - d.started > kGiveUpUs || !g_peers.count(d.from)) { + std::fprintf(stderr, "[NETPACK] %s: gave up on %s (the player left or stopped sending)\n", packs()[d.pack].name, packs()[d.pack].files[d.file].path); + it = g_dl.erase(it); + continue; + } + uint8_t sha[32]; + if (!unhex(packs()[d.pack].files[d.file].sha, sha)) { it = g_dl.erase(it); continue; } + auto ask = [&](uint32_t piece) { + std::vector r{'R'}; + r.insert(r.end(), sha, sha + 32); + put(r, piece * (uint32_t)kChunk); + put(r, (uint16_t)kChunk); + out.emplace_back(d.from, std::move(r)); + d.asked[piece] = t; + }; + if (!d.total) { if (!d.asked.count(0) || t - d.asked[0] > kResendUs) ask(0); } + else { + for (auto& [piece, when] : d.asked) if (t - when > kResendUs) when = 0; // resend lost ones + for (auto& [piece, when] : std::map(d.asked)) if (!when) ask(piece); + for (uint32_t piece = 0; piece < d.got.size() && d.asked.size() < kWindow; piece++) + if (!d.got[piece] && !d.asked.count(piece)) ask(piece); + } + ++it; + } + } + for (auto& [m, b] : out) psp_adhoc_side_send(m.data(), b.data(), b.size()); + for (Download& d : done) { + const int r = finish(d); + std::lock_guard lk(g_mu); + g_file_ok[file_slot(d.pack, d.file)] = r > 0 ? r : 0; + if (r < 0) { g_pack_state[d.pack] = 2; continue; } + bool all = true; + for (size_t f = 0; f < packs()[d.pack].files.size(); f++) all = all && g_file_ok[file_slot(d.pack, (int)f)] != 0; + if (all && g_pack_state[d.pack] == 0) { + g_pack_state[d.pack] = 1; + g_activate_pending = true; + std::fprintf(stderr, "[NETPACK] %s: every file received and verified\n", packs()[d.pack].name); + } + } + } +} + +// ---- game thread (psp_adhoc_pump): apply what a player in the group uses and this player now has ---- +void on_pump(uint8_t* rdram) { + if (!g_started.exchange(true)) std::thread(worker).detach(); + if (!g_activate_pending.exchange(false)) return; + std::vector todo; + { + std::lock_guard lk(g_mu); + for (size_t i = 0; i < g_want_active.size(); i++) + if (g_want_active[i] && g_pack_state[i] == 1 && !g_active[i] && !g_use[i]) todo.push_back((int)i); + } + for (int i : todo) { + const NetPack& p = packs()[i]; + bool ok = true; + for (size_t f = 0; f < p.files.size() && ok; f++) { + int src; + { std::lock_guard lk(g_mu); src = g_file_ok[file_slot(i, (int)f)]; } + if (src == 2) { + std::string file = cache_path(p.files[f]); + uint32_t size = p.files[f].size; + const std::string jp = dissidia_voice_jp_dir(); + std::vector jb, mb, out; + int added = 0; + if (!jp.empty() && std::strncmp(p.files[f].path, "voice/", 6) == 0 && // Japanese voices: their bank + the pack's added clips + read_file(jp + "/" + p.files[f].path, &jb, 64u << 20) && read_file(file, &mb, 64u << 20) && + dissidia::merge_voice_bank(jb, mb, &out, &added)) { + const std::string mf = file + ".jp"; + std::ofstream o(mf, std::ios::binary | std::ios::trunc); + o.write(reinterpret_cast(out.data()), (std::streamsize)out.size()); + if (o) { file = mf; size = (uint32_t)out.size(); } + } + ok = dissidia_mods_add_override(rdram, p.files[f].path, file, size); + } + // src 1: this player's own install of the pack, served since boot + } + for (const NetAbil& a : p.abilities) + ok = ok && dissidia_ablt_ensure(rdram, a.id, a.copy, a.cmd, a.type, a.phase, (uint8_t)p.charId); + std::lock_guard lk(g_mu); + if (ok) g_active[i] = true; else g_pack_state[i] = 2; + std::fprintf(stderr, "[NETPACK] %s v%d %s for this session\n", p.name, p.version, ok ? "applied" : "could NOT be applied (no match with players who use it)"); + } +} + +} // namespace + +// Called from dissidia_register_hooks. +void dissidia_net_packs_install() { + if (packs().empty()) return; + dissidia_mods_net_possible(); + psp_adhoc_side_set_handler(on_side); + psp_adhoc_set_match_filter(allow_match); + psp_adhoc_set_pump_hook(on_pump); + std::fprintf(stderr, "[NETPACK] %zu known pack(s); online players can send them to this game (verified)\n", packs().size()); +} diff --git a/psprecomp/games/dissidia/runtime/net_packs.inc b/psprecomp/games/dissidia/runtime/net_packs.inc new file mode 100644 index 0000000..cb56f2c --- /dev/null +++ b/psprecomp/games/dissidia/runtime/net_packs.inc @@ -0,0 +1,19 @@ +// Generated by tools/netpack.py inc from games/dissidia/net_packs.json. Do not edit. +// The packs a player may receive from another player online (net_packs.cpp): exact sizes and SHA-256. +{"bartz-dff", 1, 9, { + {"obj/p_fiv100.objx", 1983820, "a7a67dd29f5067ac5a9b707b5b428a652a1e53427d52e884f28414f68bdd7a15", 1559788, "d71567ebd9fab3a50ab725fb5db44253693e1dafc1cde7a890777953ad4e9a83"}, + {"obj/p_fiv100_1p.gmo", 2462188, "9a17294ec50482f2f1293700a72d3e75f935df673494d9a95c5b7f66716eeafb", 2102308, "1b843a3e44fcc32e5ac6ac86b669cb5a14494f7121528148be3c48d60b5eaab5"}, + {"obj/p_fiv100_2p.gmo", 2462188, "c54dbeb66a6ec284aded5bed724260cc0e7aceb51a0744c6ba0232e085093c0f", 2102308, "592e67b8c6fcac7300e80ca8a32fa5cbbaa756b729782b9b1fe80ef1c4096532"}, + {"obj/p_fiv100_3p.gmo", 2470132, "b86bc46bb92bc4f4a6ad9fa5c4fa583996d4c8db970b5bc8294d14431d49dfc9", 2110252, "43da35210e9dd7e4974400cd194eb22c426f7667f1364779aec71d49057d09c7"}, + {"obj/p_fiv100_zako.gmo", 2443668, "c0dbcc2c5560b87c0b8272f837ec2d800c0605af071438acd8a35e665c239b5c", 2083788, "89422d7152374b0e49c9e5595b1364ebbdf60bbe50d392e9e78a626fbafc8cbf"}, + {"voice/en/battle/voice_p_fiv100_00.scd", 357600, "6b3709f8f3f959495e8672ed5608ecb7bd6bf95572d680b6c6c4e84fdba1a8cc", 302368, "9f67e60176757f67a7f867d35082d0a0c464e3373deb2feb1c036c788caa7946"}, + }, { + {0x0910, 0x090C, 0xB0, 0, 0}, + {0x0911, 0x090C, 0xB1, 0, 0}, + {0x0912, 0x090D, 0xB2, 0, 1}, + {0x0913, 0x090D, 0xB3, 0, 1}, + {0x0914, 0x090E, 0xB4, 1, 0}, + {0x0915, 0x090E, 0xB5, 1, 0}, + {0x0916, 0x090F, 0xB6, 1, 1}, + {0x0917, 0x090F, 0xB7, 1, 1}, + }}, diff --git a/psprecomp/games/dissidia/runtime/package_mods.cpp b/psprecomp/games/dissidia/runtime/package_mods.cpp index aeae890..5e8e02c 100644 --- a/psprecomp/games/dissidia/runtime/package_mods.cpp +++ b/psprecomp/games/dissidia/runtime/package_mods.cpp @@ -174,6 +174,7 @@ bool PackageMods::build(const std::vector& original_index, uint64_t pac uint32_t index_capacity, const std::vector& extra_dirs) { overrides_.clear(); patched_ = original_index; + package_size_ = package_size; PackageIndex idx; if (!idx.parse(original_index.data(), original_index.size(), err)) return false; auto note = [&](const std::string& s) { if (log) { *log += s; *log += '\n'; } }; @@ -216,7 +217,7 @@ bool PackageMods::build(const std::vector& original_index, uint64_t pac if (!parse_hex8(stem, &crc)) { note("[MODS] skip (bad by_crc name): " + rel); continue; } } else { if (norm.find('/') == std::string::npos && - (norm.compare(0, 6, "readme") == 0 || norm == "roster.txt")) continue; // roster.txt: roster.cpp config + (norm.compare(0, 6, "readme") == 0 || norm == "roster.txt" || norm == "retail_moves.txt")) continue; // roster.cpp config // textures/… is the texture-pack tree (psp_tex_pack.cpp serves it by xxhash id); the game never // requests those paths, and a dump of 1,000+ PNGs would otherwise flood the package index below. if (norm.compare(0, 9, "textures/") == 0) continue; @@ -297,6 +298,33 @@ bool PackageMods::build(const std::vector& original_index, uint64_t pac return true; } +bool PackageMods::add_override(const std::string& asset, const std::string& file, uint32_t size, IndexEntry* out) { + PackageIndex idx; + std::string err; + if (patched_.empty() || !idx.parse(patched_.data(), patched_.size(), &err) || size == 0 || size > kSizeMask) return false; + const std::string norm = normalize_asset_path(asset); + const uint32_t crc = package_crc(norm); + IndexEntry* e = nullptr; + for (auto& x : idx.entries) if (x.crc == crc) e = &x; + if (!e) return false; + overrides_.erase(std::remove_if(overrides_.begin(), overrides_.end(), [crc](const Override& o) { return o.crc == crc; }), + overrides_.end()); + uint64_t next = (package_size_ + kSectorSize - 1) / kSectorSize; + for (const auto& o : overrides_) next = std::max(next, (o.start + o.span + kSectorSize - 1) / kSectorSize); + for (const auto& x : idx.entries) next = std::max(next, (uint64_t)x.sector + x.sectors()); + Override ov; + ov.crc = crc; ov.asset = norm; ov.file = file; ov.size = size; ov.relocated = true; + ov.start = next * kSectorSize; + ov.span = (uint64_t)((size + kSectorSize - 1) / kSectorSize) * kSectorSize; + e->sector = (uint32_t)next; + e->size_flags = (e->flags() << 28) | size; + *out = *e; + overrides_.push_back(ov); + std::sort(overrides_.begin(), overrides_.end(), [](const Override& a, const Override& b) { return a.start < b.start; }); + patched_ = idx.serialize(); + return true; +} + bool PackageMods::read_index(int64_t off, uint32_t size, uint8_t* dest, uint32_t* nread) const { if (off < 0 || (uint64_t)off >= patched_.size()) { *nread = 0; return true; } uint32_t n = (uint32_t)std::min(size, patched_.size() - (uint64_t)off); diff --git a/psprecomp/games/dissidia/runtime/package_mods.h b/psprecomp/games/dissidia/runtime/package_mods.h index c0c2e67..9decda5 100644 --- a/psprecomp/games/dissidia/runtime/package_mods.h +++ b/psprecomp/games/dissidia/runtime/package_mods.h @@ -87,6 +87,11 @@ class PackageMods { uint32_t index_capacity = kIndexBufferBytes, const std::vector& extra_dirs = {}); + /// Add (or replace) the override of an EXISTING index entry after build (a pack received online): the entry is + /// moved to fresh virtual sectors past every other override, so nothing already served moves. `out` gets the + /// entry as it must now read in the game's in-memory index. False if the asset is not in the index. + bool add_override(const std::string& asset, const std::string& file, uint32_t size, IndexEntry* out); + bool empty() const { return overrides_.empty(); } const std::vector& overrides() const { return overrides_; } const std::vector& patched_index() const { return patched_; } @@ -99,6 +104,7 @@ class PackageMods { const BaseReader& base) const; private: + uint64_t package_size_ = 0; std::vector overrides_; // sorted by start, non-overlapping std::vector patched_; }; diff --git a/psprecomp/games/dissidia/runtime/roster.cpp b/psprecomp/games/dissidia/runtime/roster.cpp index 678122a..01e640b 100644 --- a/psprecomp/games/dissidia/runtime/roster.cpp +++ b/psprecomp/games/dissidia/runtime/roster.cpp @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -951,9 +952,15 @@ void learn_all_own(uint8_t* rdram, uint8_t* rec, const Entry& e) { } } } +void retail_moves_teach(uint8_t* rdram, uint32_t rec, int charId); void saverec_hook(uint8_t* rdram, recomp_context* ctx) { const Entry* e = extra_of(static_cast(ctx->r[5])); - if (!e) { g_orig_saverec(rdram, ctx); return; } + if (!e) { + const int charId = static_cast(ctx->r[5]); + g_orig_saverec(rdram, ctx); + retail_moves_teach(rdram, static_cast(ctx->r[2]), charId); + return; + } ctx->r[5] = e->like; g_orig_saverec(rdram, ctx); const uint32_t src = static_cast(ctx->r[2]); @@ -5469,7 +5476,7 @@ void dissidia_roster_grid_install() { g_orig_textcreate = psp_dispatch_probe_lookup(0x0887B3ECu); g_orig_textlit = psp_dispatch_probe_lookup(0x0887D868u); if (g_orig_textcreate && g_orig_textlit && !std::getenv("PSPRECOMP_ROSTER_TRACE")) psp_dispatch_register(0x0887B3ECu, oppinfo_name_hook); - g_orig_saverec = psp_dispatch_probe_lookup(0x088CB164u); + if (!g_orig_saverec) g_orig_saverec = psp_dispatch_probe_lookup(0x088CB164u); // (already hooked for retail moves) if (g_orig_saverec) psp_dispatch_register(0x088CB164u, saverec_hook); { // the extras' own records in a sidecar of each save (savedata import stubs, bound by psp_hle_init before this) const uint32_t init = psp_hle_stub_addr_for_nid(NID_SD_INIT), status = psp_hle_stub_addr_for_nid(NID_SD_STATUS), @@ -5593,6 +5600,88 @@ void objentry_load_hook(uint8_t* rdram, recomp_context* ctx) { g_orig_obj_load(rdram, ctx); } +// ---- Retail moves: new abilities a "kind": "retail" mod gives a retail character (Bartz's Dissidia (2008) moves, +// mods/bartz-dff). tools/charmod.py builds their 'ablt' records into main.bin like an added character's and lists them in +// /retail_moves.txt, one line per mod: "[#off# ] ,,... nm=