Compare commits

...

287 Commits

Author SHA1 Message Date
James Osborne cab3de3ff4 upstream-master-20260614
CMake / build (macos-latest) (push) Has been cancelled
CMake / build (ubuntu-latest) (push) Has been cancelled
upstream master 20260614
2026-06-14 19:12:29 -04:00
Your Name 9150e85442 Merge remote-tracking branch 'upstream/master' into feature/upstream-master-20260614
# Conflicts:
#	src/Client.cc
#	src/IPStackSimulator.cc
#	src/ReceiveCommands.cc
#	src/ReceiveSubcommands.cc
#	src/SendCommands.cc
#	src/ServerState.cc
#	src/ServerState.hh
2026-06-14 18:30:38 -04:00
Martin Michelsen 3cdb5e91a8 name Root structures appropriately 2026-06-14 10:03:55 -07:00
Martin Michelsen 629e2bb4cd make replay tests run in parallel and share immutable data 2026-06-14 09:27:55 -07:00
James Osborne ce39dfc66a Update README.md 2026-06-14 12:14:22 -04:00
James Osborne 448d212aea Update README.md 2026-06-14 12:13:31 -04:00
James Osborne 0208bf26d9 Send account lock session-end on logout
Send account lock session-end on logout
2026-06-14 03:40:52 -04:00
Your Name 1951506dd6 Send account lock session-end on logout 2026-06-14 03:32:25 -04:00
James Osborne d5e429b86a Gate BB login lock before character select
Gate BB login lock before character select
2026-06-14 00:24:33 -04:00
Your Name 6dacf2c152 Gate BB login lock before character select 2026-06-14 00:15:23 -04:00
Martin Michelsen 1737d8abc8 add more options in IntegralExpression 2026-06-13 20:07:23 -07:00
Martin Michelsen 554fc5d208 make check-quests parallel 2026-06-13 17:16:04 -07:00
Martin Michelsen 0dbb34b9f9 don't share iconv_t objects between threads 2026-06-13 17:16:04 -07:00
Your Name 4219eb84c7 Revert "Ignore local source artifacts"
This reverts commit 46c365fda0.
2026-06-13 19:59:01 -04:00
Your Name 46c365fda0 Ignore local source artifacts 2026-06-13 19:57:53 -04:00
Martin Michelsen 5739f99912 port 60 FPS code to all GC versions 2026-06-13 15:55:02 -07:00
Martin Michelsen 9647fe4d63 make $item command use game item ID space 2026-06-13 10:25:25 -07:00
Martin Michelsen 7c007d1b1e fix payment item handling in 6xDA 2026-06-13 10:25:25 -07:00
Martin Michelsen 45b33a3c3a add timing in check-quests 2026-06-13 10:25:25 -07:00
James Osborne a611462655 team sync team chat relay fix
team sync team chat relay fix
2026-06-12 22:06:32 -04:00
Your Name 4e8253c38f Log TeamSync chat exchange flow 2026-06-12 21:50:54 -04:00
Your Name 15266e0ef9 Log TeamSync team chat relay attempts 2026-06-12 21:36:16 -04:00
Your Name 133041f09b Relay TeamSync team chat events 2026-06-12 20:40:15 -04:00
James Osborne 989eabe3a0 Merge pull request #36 from incentivebeats/feature/team-sync-phase3-team-management
Add TeamSync team management events
2026-06-12 18:02:28 -04:00
Your Name f883367eaa Add TeamSync team management events 2026-06-12 16:39:46 -04:00
James Osborne 91c4711c48 Merge pull request #35 from incentivebeats/feature/team-sync-phase2-points-names
Add TeamSync member name and points updates
2026-06-12 16:08:22 -04:00
Your Name f4af1a73f1 Add TeamSync member name and points updates 2026-06-12 15:46:36 -04:00
James Osborne b450a04be4 team sync chat relay
Feature/team sync chat relay
2026-06-12 15:32:48 -04:00
Your Name 23015614ed Harden TeamSync canonical state application 2026-06-12 14:39:07 -04:00
Your Name cf380e93d2 Guard unsupported TeamSync team mutations 2026-06-12 13:02:06 -04:00
incentive 21bceac1e3 Route TeamSync member mutations through coordinator 2026-06-12 01:26:18 -04:00
incentive b578c1cbbe Apply TeamSync team membership to account records 2026-06-12 01:23:59 -04:00
incentive 44650179f0 Route TeamSync team creation through coordinator 2026-06-12 01:07:12 -04:00
incentive 7526176bb3 Add TeamSync outbound team create queue 2026-06-12 01:02:13 -04:00
incentive 4d893607c2 Add TeamSync canonical team state apply scaffold 2026-06-12 00:57:17 -04:00
incentive 6995e5b7f4 Improve TeamSync coordinator error logging 2026-06-11 22:34:47 -04:00
incentive e9187609ae Add TeamSync exchange task scaffold 2026-06-11 22:28:35 -04:00
incentive bf3c9c08e6 Harden TeamSync config parsing 2026-06-11 22:00:23 -04:00
incentive c4fb18b3b4 Add TeamSync configuration scaffold 2026-06-11 21:59:00 -04:00
James Osborne fe412ebd84 merge upstream master 20260611
Feature/merge upstream master 20260611
2026-06-11 20:02:10 -04:00
incentive ee5ee49d22 Use EmulatorBase DisassembleResult for DOL semantic diff 2026-06-11 19:18:07 -04:00
incentive 12481996b8 Merge upstream master
# Conflicts:
#	README.md
2026-06-11 18:31:23 -04:00
James Osborne aae7e64018 Add BB license update admin endpoint
Add BB license update admin endpoint
2026-06-11 18:28:17 -04:00
incentive ed47fdc5d1 Add BB license update admin endpoint 2026-06-11 13:13:27 -04:00
James Osborne 021cb9b176 account login locks
Feature/account login locks
2026-06-11 12:19:25 -04:00
incentive 3d37aacc06 Add login lock coordinator heartbeat 2026-06-11 02:06:07 -04:00
incentive 56084c736f Add coordinator login lock acquire request 2026-06-11 01:28:23 -04:00
incentive 6a4789b248 Add login lock session plumbing 2026-06-11 01:22:32 -04:00
James Osborne f18d5a468c Allow account concurrency across client sources
Allow account concurrency across client sources
2026-06-10 15:35:53 -04:00
incentive 79fa456365 Allow account concurrency across client sources 2026-06-10 15:27:17 -04:00
James Osborne 98e338046c Add account admin mutation endpoints
Add account admin mutation endpoints
2026-06-09 23:01:51 -04:00
incentive 3c9240e7d8 Add account admin mutation endpoints 2026-06-09 20:52:47 -04:00
James Osborne 40689c0690 Skip BB auth gate for PC patch login
Skip BB auth gate for PC patch login
2026-06-09 19:58:21 -04:00
incentive e433b0c663 Skip BB auth gate for PC patch login 2026-06-09 16:29:47 -04:00
Martin Michelsen bb70390fd8 fix TODO comments 2026-06-09 07:18:29 -07:00
Martin Michelsen c98f88f5c0 add basic semantic diff for DOL files 2026-06-09 07:02:50 -07:00
James Osborne e3c223f979 account sync hooks
Feature/account sync hooks
2026-06-08 12:48:08 -04:00
incentive 127288c349 Add explicit account sync source identity 2026-06-08 06:06:20 -04:00
incentive cfbe1fda27 Spool account sync events to disk 2026-06-08 05:59:55 -04:00
incentive 8f80005cb1 Move account sync implementation out of header 2026-06-08 05:55:11 -04:00
incentive 70dd22ee8c Make account sync hooks config-aware 2026-06-08 04:43:21 -04:00
incentive 94250d21eb Add log-only BB account sync logout hook 2026-06-08 04:02:11 -04:00
incentive fe97a0dda4 Add log-only BB account sync login hook 2026-06-08 03:59:27 -04:00
incentive f8d50b3ab7 Add log-only account sync save hooks 2026-06-08 03:56:50 -04:00
James Osborne 1d162bb723 brutal peeps labeling in room list
brutal peeps labeling in room list
2026-06-08 03:33:27 -04:00
incentive e802752836 Clarify Brutal Peeps room join warnings 2026-06-07 18:10:45 -04:00
incentive 2e38c4b12f Split BB and PC Brutal Peeps patch timing 2026-06-07 17:31:48 -04:00
incentive 75653f155c Show Brutal Peeps tier in BB game list 2026-06-07 16:56:44 -04:00
incentive b9cd17d9dc Do not use BB color tokens in game list names 2026-06-07 16:53:41 -04:00
incentive 672a6575a7 Restrict Brutal Peeps rooms to matching client versions 2026-06-07 15:54:21 -04:00
incentive 7d609b6a40 Restrict PC Brutal Peeps rooms to PC clients 2026-06-07 15:48:38 -04:00
incentive 9183c1e362 Mark Brutal Peeps rooms in game list 2026-06-07 15:39:33 -04:00
Martin Michelsen c329418f30 reconcile 6xE0 prize list with old records and Ephinea's list 2026-06-07 12:02:33 -07:00
James Osborne 2dba843cb2 writecodeblocks noop clean
Feature/pc writecodeblocks noop clean
2026-06-07 13:00:51 -04:00
incentive 1f9ef0c3b6 Fix Dragon Visual Fix source encoding 2026-06-07 12:24:05 -04:00
incentive 0bf07a882c Log Dragon client function loading 2026-06-07 12:18:15 -04:00
incentive 52087e50a3 Fix PC client function probe version type 2026-06-07 12:13:26 -04:00
incentive 2c66407e8b Probe Dragon client function indexing 2026-06-07 11:58:44 -04:00
incentive eb7457a436 Log PC patch menu function filtering 2026-06-07 11:49:46 -04:00
incentive 943bb20cec Shorten Dragon Visual Fix menu label 2026-06-07 06:54:41 -04:00
incentive ea2d87cacb Rename Dragon Visual Fix patch 2026-06-07 06:52:43 -04:00
incentive 3e527bf979 Encode PC Dragon Visual Fix bytes directly 2026-06-07 06:47:55 -04:00
incentive e8b80a3ede Add PC Dragon Visual Fix patch 2026-06-07 06:33:21 -04:00
incentive c1a5063ba8 Use VirtualProtect for PC WriteCodeBlocks 2026-06-07 06:18:51 -04:00
incentive b9a621e7cc Use VirtualProtect for PC WriteCodeBlocks 2026-06-07 06:12:32 -04:00
incentive 41f05b1fe5 Test PC WriteCodeBlocks against writable data 2026-06-07 06:01:21 -04:00
incentive 5d58c2467c Add PC WriteCodeBlocks no-op test 2026-06-07 05:56:52 -04:00
James Osborne 621691b369 Brutal Peeps PC client memory patch
Add Brutal Peeps PC client memory patch
2026-06-07 04:56:13 -04:00
incentive 3c779b9e1f Track PC Brutal Peeps BattleParam patch tier 2026-06-07 04:30:47 -04:00
incentive 892b12535c Keep retrying PC Brutal Peeps patch after room load 2026-06-07 04:12:44 -04:00
incentive fc1fe53b63 Only patch PC Brutal Peeps after combat floor event 2026-06-07 04:00:35 -04:00
incentive b80cf85f48 Patch PC Brutal Peeps after entering combat floor 2026-06-07 03:54:55 -04:00
incentive f0bc3639c9 Force-send PC Brutal Peeps dynamic patch 2026-06-07 03:43:25 -04:00
incentive 17ddfe4945 Retry PC Brutal Peeps memory patch after load 2026-06-07 03:37:45 -04:00
incentive e3c7f77440 Compact PC Brutal Peeps memory patch payload 2026-06-07 03:32:00 -04:00
incentive 1ef2a7e1e2 Defer PC Brutal Peeps memory patch until room load 2026-06-07 03:24:53 -04:00
incentive 58efb41957 Allow PC to create Brutal Peeps rooms 2026-06-07 03:18:24 -04:00
incentive a1c3beafac Allow Brutal Peeps menu for PC 2026-06-07 03:06:09 -04:00
incentive 30e645fdeb Add Brutal Peeps PC client memory patch 2026-06-07 02:57:26 -04:00
Martin Michelsen 77d31cd3b5 add more logs to 6xE0 handler 2026-06-06 21:51:15 -07:00
Martin Michelsen 5f7032f920 add symlinks for DC V1 battle mode city maps; closes #772 2026-06-06 21:51:15 -07:00
Martin Michelsen 0c9cd57329 fix 6xDA attribute computation; closes #775 2026-06-06 21:51:15 -07:00
Martin Michelsen b301df96f2 add TreeWindowIndex 2026-06-06 21:51:15 -07:00
Martin Michelsen 708d2a9fb0 convert shop random sets to JSON 2026-06-06 21:51:15 -07:00
Martin Michelsen efe7401d7b convert TekkerAdjustmentSet to JSON 2026-06-06 21:51:15 -07:00
Martin Michelsen 3f33b94e8f more work on ItemMagEdit 2026-06-06 21:51:15 -07:00
James Osborne a5307ccb1a brutal peeps bb atp scaling
Feature/brutal peeps bb atp scaling
2026-06-07 00:33:22 -04:00
incentive 9dbb19972f Retune Brutal Peeps ATP scaling 2026-06-07 00:21:14 -04:00
incentive 61ff0de929 Use gentler Brutal Peeps ATP tier table 2026-06-07 00:13:46 -04:00
James Osborne af8e1ccb91 brutal peeps bb episode hp tables
Feature/brutal peeps bb episode hp tables
2026-06-06 23:42:36 -04:00
incentive 8dd966a5d7 Patch all BB online BattleParam tables before Brutal Peeps rooms 2026-06-06 23:33:11 -04:00
incentive 98dd6b8913 Select BB Brutal Peeps HP table by episode 2026-06-06 23:24:39 -04:00
James Osborne 6492dbf879 brutal peeps hp scaling
Feature/brutal peeps hp scaling
2026-06-06 22:54:24 -04:00
incentive 5feffea722 Use raw BattleParam HP offsets for Brutal Peeps patch 2026-06-06 22:18:52 -04:00
incentive 672be0b6b8 Patch Brutal Peeps HP before BB room load 2026-06-06 22:11:31 -04:00
incentive c7fb0cf5f6 Use raw HP diffs with simple BattleParam scanner 2026-06-06 22:06:06 -04:00
incentive eba565c381 Use table-offset signature for Brutal Peeps HP patch 2026-06-06 21:59:05 -04:00
incentive 942dbbc5b9 Patch matching BB battle param tables with raw HP diffs 2026-06-06 21:52:42 -04:00
incentive b4a7374fae Apply Brutal Peeps HP patch after BB area load 2026-06-06 21:35:22 -04:00
incentive 976cb132a2 Pass Brutal Peeps HP patch config in suffix 2026-06-06 21:24:29 -04:00
incentive f424d6ed3c Avoid coroutine ICE in Brutal Peeps HP patch 2026-06-06 21:15:13 -04:00
incentive e44dad8344 Add Brutal Peeps HP client patch 2026-06-06 21:07:52 -04:00
Corrine 989fc8f0ec Fix ClearUnreleasedItemList.s for 50YJ 2026-06-06 17:12:27 -07:00
James Osborne 46ddcb64a9 Feature/brutal peeps
Feature/brutal peeps live
2026-06-05 20:24:09 -04:00
incentive b27e0b8351 Retune Brutal Peeps EXP and HP scaling 2026-06-05 20:09:32 -04:00
incentive 563f84d87a Remove Brutal Peeps global unlock config 2026-06-05 19:27:07 -04:00
incentive 507a0ef9f0 Use Brutal Peeps tier table for rewards and enemy HP 2026-06-05 19:25:14 -04:00
incentive 2493173052 Gate Brutal Peeps tiers by character level 2026-06-05 19:23:49 -04:00
incentive 9d4636f386 Add Brutal Peeps tier table 2026-06-05 19:21:14 -04:00
incentive 08c897cbea Rename Blueballz mode to Brutal Peeps 2026-06-05 19:19:48 -04:00
James Osborne 108eee0154 Upstream/2026 05 31
Upstream/2026 05 31
2026-05-31 18:10:09 -04:00
incentive bde24db224 Merge upstream changes from 2026-05-31 2026-05-31 11:23:38 -04:00
James Osborne e7c3eec58f Fix BB EP4 solo quest unlocks
Fix BB EP4 solo quest unlocks and ship-state marking
2026-05-31 10:45:49 -04:00
incentive 55744dfc9d Fix BB EP4 solo access and ship-state marking 2026-05-31 10:02:53 -04:00
incentive 6d9d7cfb95 Fix BB EP4 solo access and ship-state marking 2026-05-31 09:54:02 -04:00
incentive 1cf5b210ca Unlock BB EP4 solo areas 2026-05-31 09:08:41 -04:00
incentive a030983b61 Fix BB EP4 solo quest unlocks and ship-state marking 2026-05-31 08:42:39 -04:00
Martin Michelsen 93bad47c03 more quest flag notes 2026-05-30 21:56:40 -07:00
Martin Michelsen f9ff902d35 update solo quest unlock conditions 2026-05-30 13:44:38 -07:00
Martin Michelsen 1dee20713b explain quest counters more clearly 2026-05-30 09:52:19 -07:00
Martin Michelsen 9187a3ceb0 update PlayerVisualConfigV4 struct to match client implementation 2026-05-30 09:47:52 -07:00
Martin Michelsen e4054d95d9 rewrite BB EXP generation to handle non-player kills properly 2026-05-29 07:43:05 -07:00
Martin Michelsen c09ee2da85 handle missing block indexes in qst decoder 2026-05-29 06:56:56 -07:00
Martin Michelsen bb0ead8650 fix Dal Ra Lie Ultimate name 2026-05-28 08:29:49 -07:00
Martin Michelsen 3aa58e24b4 add minor notes from Challenge RE 2026-05-28 08:29:37 -07:00
Martin Michelsen c3aacc2352 add Challenge warning chime code 2026-05-28 08:29:20 -07:00
Martin Michelsen 077a4b91d0 update to-do list 2026-05-28 08:29:12 -07:00
James Osborne 7099045ea1 Feature/room crossplay opt in
Feature/room crossplay opt in
2026-05-25 20:45:15 -04:00
Martin Michelsen e9c2ac34a3 eliminate using namespace 2026-05-25 16:44:37 -07:00
Martin Michelsen 4503d09c77 fix DC NTE map file 2026-05-25 16:44:37 -07:00
Martin Michelsen e5b3abd49f remove unnecessary config var 2026-05-25 16:44:37 -07:00
Martin Michelsen b59dde53b2 delete unnecessary file caches 2026-05-25 16:44:36 -07:00
Martin Michelsen b8efd730f9 fix incorrect game duration in info window 2026-05-25 16:44:36 -07:00
Martin Michelsen f13c4df946 style updates 2026-05-25 16:44:36 -07:00
Martin Michelsen 62a9da9ed3 update game join procedure implementation 2026-05-25 16:44:36 -07:00
incentive 356abb6698 Treat tab-padded BB room names as crossplay opt-in 2026-05-25 18:57:00 -04:00
incentive d40dbec9f0 Log room name bytes for crossplay opt-in 2026-05-25 18:49:45 -04:00
incentive 5ec3028316 Handle padded BB room names for crossplay opt-in 2026-05-25 18:45:40 -04:00
incentive bf52bfb291 Recognize BB x-prefixed crossplay rooms 2026-05-25 18:37:30 -04:00
incentive fa543b842e Log room crossplay opt-in decisions 2026-05-25 18:23:42 -04:00
incentive 7e9c6c185a Add opt-in room crossplay compatibility 2026-05-25 18:13:50 -04:00
James Osborne 0789f04d6a Make DC V2 EXP boost config-driven
Make DC V2 EXP boost config-driven
2026-05-24 12:34:41 -04:00
incentive 3e9cb883f4 Disable GC V3 XP patch menu for now 2026-05-24 12:33:43 -04:00
incentive e3ad3c505f Limit GC V3 XP patch to USA Plus 2026-05-24 12:13:08 -04:00
incentive a292956151 Match GC EP1 EXP patch row coverage to disc 2026-05-24 12:10:32 -04:00
incentive 5e94f2eac0 Expand GC V3 EP1 EXP denylist 2026-05-24 11:28:58 -04:00
incentive 114fe642fc Skip problematic GC V3 EP1 EXP rows 2026-05-24 11:20:12 -04:00
incentive c6ee7aa08b Fix GC V3 EXP runtime byte order 2026-05-24 10:36:11 -04:00
incentive 48d32214c0 Merge master into DC/GC XP boost branch 2026-05-24 10:30:09 -04:00
incentive 0b3464b4fc Make GC V3 EXP boost config-driven 2026-05-24 01:19:02 -04:00
incentive f8ebc67c90 Make GC V3 EXP boost config-driven 2026-05-24 00:38:10 -04:00
incentive e6b7ed7e24 Make DC V2 EXP boost config-driven 2026-05-23 23:50:10 -04:00
Corrine fffd2c3e62 Added notes on 50YJ 2026-05-23 19:27:33 -07:00
Corrine ea74b4ac07 Port all 59NL/59NJ patches to 50YJ (US 1.24.3) 2026-05-23 19:27:33 -07:00
James Osborne 059011ddda exp dispatch rate aware / bb char locks
Fix dcv2/gc exp dispatch rate aware
2026-05-23 04:57:11 -04:00
incentive 6910c90fe6 Lock BB test characters to test ship 2026-05-22 22:20:48 -04:00
incentive ab245d1b70 Lock BB test characters to test ship and hide ship transfers 2026-05-22 22:11:38 -04:00
incentive 57f3e1e5f2 Lock BB test characters to test ship 2026-05-22 21:49:38 -04:00
Martin Michelsen 6f9c442e7a fix missing login in send_client_init_bb 2026-05-21 06:42:24 -07:00
Martin Michelsen a21b09d7b9 minor notes updates 2026-05-21 06:41:52 -07:00
incentive 6bb5bb8496 Resolve GC EXP patches to US-only addresses 2026-05-21 04:50:27 -04:00
incentive 243098c98c Restore exact May 16 GC EXP patches 2026-05-21 04:40:23 -04:00
incentive 9ff1934b2f Fix GC EXP patch metadata descriptions 2026-05-21 04:11:17 -04:00
incentive 6b3669dfd6 Restore GC EXP direct menu patches 2026-05-21 03:43:48 -04:00
incentive 7c7ecf0383 Use awaited path for GC EXP dispatch 2026-05-21 03:26:40 -04:00
incentive 2615ce46eb Dispatch GC EXP before game loading 2026-05-21 03:08:41 -04:00
incentive 9e746a63d9 Fix GC EXP enable shim assembly 2026-05-21 02:47:09 -04:00
incentive 1849d9d13d Make GC EXP enable shim inert and dedupe dispatch 2026-05-21 02:32:12 -04:00
incentive b9c9b877d2 Add GC V3 EXP dispatcher 2026-05-21 01:50:06 -04:00
incentive 07d04a761e Restore GC EXP patches without XP client flag 2026-05-21 01:40:31 -04:00
incentive 2b81f4d1d3 Add PC V2 EXP patch assets 2026-05-20 23:04:38 -04:00
incentive da8466c432 Make DC V2 EXP dispatcher rate-aware 2026-05-20 09:46:25 -04:00
James Osborne 7b0bdbd1ce Upstream sync 2026 05 19
Upstream sync 2026 05 19
2026-05-19 18:23:04 -04:00
incentive 47e9fe5f16 Merge upstream newserv master 2026-05-19 16:56:33 -04:00
James Osborne d7862426ac align proxy boost gates with upstream cleanup
align proxy boost gates with upstream cleanup
2026-05-19 16:13:01 -04:00
incentive 4cf5974c7d Fix BB live ship menu reconnect port 2026-05-19 16:11:05 -04:00
incentive e7ea471ec5 PSO Peeps: align proxy boost gates with upstream cleanup 2026-05-19 15:50:37 -04:00
Martin Michelsen 86227c0026 fix GC NoRareSelling patch; closes #763 2026-05-17 19:42:39 -07:00
Martin Michelsen 7b9b44c191 add option to prevent concurrent logins; closes #511 2026-05-17 15:00:32 -07:00
James Osborne f73dbf5a96 Skip auto patches for checksum-only clients
Skip auto patches for checksum-only clients
2026-05-17 13:21:43 -04:00
Martin Michelsen cb69dc9c14 more notes on PlayerInventoryItem 2026-05-17 07:59:41 -07:00
Martin Michelsen d31fb5b084 fix battle level up on BB 2026-05-17 07:59:32 -07:00
incentive f7fff5c82b Skip auto patches for checksum-only clients 2026-05-17 09:35:32 -04:00
James Osborne 6b99ce49e8 bb lobby defaults 2026-05-17 02:28:25 -04:00
incentive e7cdce9a3d Force BB clients to repatch default lobby assets 2026-05-17 02:21:41 -04:00
incentive e78da0e19e Restore default BB city02 lobby assets 2026-05-17 02:10:39 -04:00
incentive 0c50c664af Restore default BB city02 map assets 2026-05-17 02:02:02 -04:00
James Osborne a5bfb25854 Add BB stream diagnostics and event guard | Restore DC V2 EXP dispatcher
Devnet/v0.2.1 bb stream diag 20260517 t041446 z
2026-05-17 01:10:47 -04:00
incentive 7bd2fba177 Restore DC V2 EXP dispatcher 2026-05-17 01:04:21 -04:00
Martin Michelsen ef3a6575ab demote ignore_def message from warning 2026-05-16 22:02:57 -07:00
Martin Michelsen 57de5a71a3 fix mismatched enemy type order 2026-05-16 22:02:41 -07:00
Martin Michelsen 53bc0641a9 delete compatibility table 2026-05-16 22:02:11 -07:00
incentive 21884bf109 Add BB stream diagnostics and event guard 2026-05-17 00:15:50 -04:00
Martin Michelsen 0d5cfc6ccc censor user credentials in logs by default 2026-05-16 17:20:09 -07:00
James Osborne 4f7e353daa Add Dreamcast V2 EXP dispatcher
Dc xp fix
2026-05-16 05:49:50 -04:00
incentive ca6a07a151 Add Dreamcast V2 EXP dispatcher 2026-05-16 05:24:26 -04:00
incentive e384477594 Add Dreamcast V2 EXP dispatcher 2026-05-16 05:19:20 -04:00
incentive 50332d2f1e Add internal Dreamcast V2 EXP tables 2026-05-16 04:50:30 -04:00
incentive 48c25159bc Restore working Dreamcast V2 EXP patch state 2026-05-16 04:03:57 -04:00
incentive 28a113657b Fix Dreamcast V2 EXP factor load 2026-05-16 03:45:07 -04:00
incentive f3681d79f9 Fix Dreamcast V2 EXP resend behavior 2026-05-16 03:23:36 -04:00
incentive e29231356b Resend Dreamcast V2 patches after floor changes 2026-05-16 03:13:58 -04:00
incentive 781800a36e Make Dreamcast V2 EXP patches difficulty-aware 2026-05-16 02:57:56 -04:00
incentive e3fd155e0e Temporarily restore fixed Dreamcast V2 EXP patches 2026-05-16 02:48:23 -04:00
incentive c043bbc909 Fix Dreamcast V2 EXP patch sentinel load 2026-05-16 02:43:00 -04:00
incentive e4a758284c Make Dreamcast V2 EXP patches difficulty-aware 2026-05-16 02:38:10 -04:00
Martin Michelsen ecc61b7d1f document usability flags in ItemParameterTable 2026-05-15 21:22:09 -07:00
James Osborne 447fef0574 Dc xp fix
Dc xp fix
2026-05-15 23:52:35 -04:00
incentive 0063384144 Resend selected patch-menu functions after loading 2026-05-15 23:43:47 -04:00
incentive fdbad0a044 Write Dreamcast V2 EXP patches to third table copy 2026-05-15 23:28:28 -04:00
incentive b29360f066 Write Dreamcast V2 EXP patches to both table copies 2026-05-15 23:20:18 -04:00
incentive b238d7f26a Fix Dreamcast V2 EXP patch addresses 2026-05-15 23:14:04 -04:00
James Osborne 8ef0623605 Sync from and adapt to upstream 20260514
Sync/upstream 20260514
2026-05-15 22:19:52 -04:00
incentive d5df5eec5a Add Dreamcast V2 EXP client patches 2026-05-15 19:39:21 -04:00
incentive 9dfcf57bbf Split EP2 GC EXP patches by version 2026-05-15 18:19:13 -04:00
incentive 566b36d611 Add 3OJ5 support for GC client functions 2026-05-15 17:24:20 -04:00
incentive e8323989d3 Remove tentative 3OP0 GC patch mappings 2026-05-15 16:18:34 -04:00
incentive 08bb9b8a10 Shorten PSO Peeps GC patch menu names 2026-05-15 15:35:13 -04:00
incentive 19294a50a5 Add tentative 3OP0 mappings for PSO Peeps GC patches 2026-05-15 15:24:03 -04:00
incentive 6de7db4765 Add IP stack port remapping 2026-05-15 15:06:32 -04:00
incentive b2b8fd2cec Retarget PSO Peeps GC patches to US Plus 2026-05-15 13:27:11 -04:00
incentive 096831ac1b Convert PSO Peeps client functions to new format 2026-05-15 11:52:45 -04:00
Martin Michelsen 645590d03f fix S_14 definition 2026-05-14 19:20:40 -07:00
James Osborne b873a62772 Restore upstream savechar and loadchar proxy restrictions
Restore upstream savechar and loadchar proxy restrictions
2026-05-14 15:03:16 -04:00
incentive 78878ad276 Merge upstream newserv master 2026-05-14 14:23:00 -04:00
incentive 58d7f23ace Restore upstream savechar and loadchar proxy restrictions 2026-05-14 14:02:30 -04:00
Martin Michelsen b4f83c32de don't suppress 1D in proxy sessions; BB server may have customized it 2026-05-13 21:33:56 -07:00
Martin Michelsen 800b55cb04 add GC rare mag feed test 2026-05-13 21:28:20 -07:00
Martin Michelsen a6c25568ba support multiple replays in the same session 2026-05-13 21:25:55 -07:00
Martin Michelsen 2e667bbe50 support multiple aliases for quest opcodes 2026-05-13 20:32:44 -07:00
Martin Michelsen b1edf00efc update readme 2026-05-13 10:37:15 -07:00
Martin Michelsen 9d0abbce7f fix patch menu visibility 2026-05-12 17:58:37 -07:00
Martin Michelsen 563e601441 update comments in WriteMemory.s 2026-05-12 07:37:50 -07:00
Martin Michelsen c315b828ad sort DOL files by name 2026-05-12 07:37:36 -07:00
Martin Michelsen 21eae36c8f update readme for new client function compiler 2026-05-12 07:18:56 -07:00
Martin Michelsen e78e2ba887 rewrite client function compiler 2026-05-11 21:33:35 -07:00
incentive 74bfe9683d Merge remote-tracking branch 'origin/sync/upstream-20260507'
CMake / build (macos-latest) (push) Has been cancelled
CMake / build (ubuntu-latest) (push) Has been cancelled
2026-05-11 09:10:04 -04:00
Martin Michelsen 2f2a0bcf2b update logo 2026-05-10 13:38:11 -07:00
Martin Michelsen 6da72c7323 make unsealable table ordered 2026-05-10 11:20:36 -07:00
Martin Michelsen fc70919c94 reorganize game tables 2026-05-10 11:09:35 -07:00
Martin Michelsen 554bef0de4 more details on MagEvolutionTable 2026-05-09 17:05:23 -07:00
Martin Michelsen 7ce3ce5b65 add level table JSON format 2026-05-09 17:05:23 -07:00
Martin Michelsen 9915422ae6 clean up rel helpers 2026-05-08 22:22:41 -07:00
incentive e88ed98318 Merge upstream newserv master 2026-05-09 01:02:34 -04:00
Martin Michelsen e342915505 fix invalid dereference 2026-05-08 20:09:19 -07:00
Martin Michelsen e617385425 move all_start_offsets to CommonFileFormats 2026-05-08 19:50:53 -07:00
Martin Michelsen 211d08710b add default item parameter tables 2026-05-08 18:25:57 -07:00
Martin Michelsen ee40425393 add ItemParameterTable binary serialization; make JSON the default format 2026-05-08 09:23:53 -07:00
Corrine 12f9a045ca Same treatment to AR Codes. 2026-05-07 19:35:21 -07:00
Corrine 8134243fb1 Prevent confusion. 2026-05-07 19:35:21 -07:00
Corrine c869ed27f1 Fixed a mistake with NoRareSelling 2026-05-07 19:35:21 -07:00
Corrine 321ba64016 Finished. 2026-05-07 19:35:21 -07:00
Corrine a8606d26a8 Added MoreSaveSlots.59NJ 2026-05-07 19:35:21 -07:00
Corrine a57cca6c12 Port 59NL server patches to 59NJ (pt. 1) 2026-05-07 19:35:21 -07:00
incentive 86a46df442 Treat proxy A0 lobby exit as intentional
CMake / build (macos-latest) (push) Has been cancelled
CMake / build (ubuntu-latest) (push) Has been cancelled
2026-05-06 02:03:51 -04:00
incentive cdb397f5ea Add GC EP1 and EP2 EXP client patches 2026-05-05 22:34:44 -04:00
incentive 2b73d58033 Add GC Normal Dragon HP client patch 2026-05-05 22:21:33 -04:00
incentive 5abd47ff72 Align BB Normal Dragon HP with V2 2026-05-05 19:39:05 -04:00
incentive 261cb5c76f Log Dragon boss action state 2026-05-05 19:12:20 -04:00
incentive ea1044c271 Merge boss flags when blocking damage rollback 2026-05-05 18:51:10 -04:00
incentive a1c358e13a Preserve boss flags when blocking damage rollback 2026-05-05 18:28:46 -04:00
incentive 1f00bf1d9b Clamp boss damage rollback from 6x0A 2026-05-05 18:12:36 -04:00
incentive 8bc602012e Allow DC V2 Falz death-drop pickup recovery 2026-05-05 16:52:25 -04:00
Martin Michelsen b8e7d81a22 add check for invalid defensive item type 2026-05-04 10:32:08 -07:00
Martin Michelsen d384cf4f11 fix mag evolution table reference 2026-05-04 09:51:57 -07:00
Martin Michelsen 681ce135f8 add JSON encoding for ItemPMT 2026-05-03 22:55:53 -07:00
Martin Michelsen e19c0b8bc9 add 11/2000 mag table 2026-05-02 10:50:55 -07:00
480 changed files with 212848 additions and 18026 deletions
+19 -17
View File
@@ -50,6 +50,8 @@ add_custom_target(
set(SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/src/Revision.cc
src/Account.cc
src/AccountSync.cc
src/TeamSync.cc
src/AddressTranslator.cc
src/AFSArchive.cc
src/AsyncHTTPServer.cc
@@ -60,10 +62,14 @@ set(SOURCES
src/ChatCommands.cc
src/ChoiceSearch.cc
src/Client.cc
src/ClientFunctionIndex.cc
src/CommandCensorData.cc
src/CommonItemSet.cc
src/Compression.cc
src/DCSerialNumbers.cc
src/DNSServer.cc
src/DOLFileIndex.cc
src/DataIndex.cc
src/DownloadSession.cc
src/EnemyType.cc
src/Episode3/AssistServer.cc
@@ -78,8 +84,6 @@ set(SOURCES
src/Episode3/RulerServer.cc
src/Episode3/Server.cc
src/Episode3/Tournament.cc
src/FileContentsCache.cc
src/FunctionCompiler.cc
src/GameServer.cc
src/GSLArchive.cc
src/HTTPServer.cc
@@ -97,7 +101,7 @@ set(SOURCES
src/LevelTable.cc
src/Lobby.cc
src/Loggers.cc
src/MagEvolutionTable.cc
src/MagMetadataTable.cc
src/Main.cc
src/Map.cc
src/Menu.cc
@@ -124,9 +128,11 @@ set(SOURCES
src/ServerShell.cc
src/ServerState.cc
src/ShellCommands.cc
src/ShopRandomSets.cc
src/SignalWatcher.cc
src/StaticGameData.cc
src/TeamIndex.cc
src/TekkerAdjustmentSet.cc
src/Text.cc
src/TextIndex.cc
src/Version.cc
@@ -153,23 +159,19 @@ add_dependencies(newserv newserv-Revision-cc)
enable_testing()
file(GLOB LogTestCases ${CMAKE_SOURCE_DIR}/tests/*.test.txt)
file(GLOB LogRDTestCases ${CMAKE_SOURCE_DIR}/tests/*.rdtest.txt)
file(GLOB LOG_TEST_CASES ${CMAKE_SOURCE_DIR}/tests/*.test.txt)
list(TRANSFORM LOG_TEST_CASES PREPEND "--replay-log=" OUTPUT_VARIABLE LOG_REPLAY_ARGS)
add_test(
NAME "log-replays"
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMAND ${CMAKE_BINARY_DIR}/newserv --parallel --config=${CMAKE_SOURCE_DIR}/tests/config.json ${LOG_REPLAY_ARGS})
foreach(LogTestCase IN ITEMS ${LogTestCases})
file(GLOB SCRIPT_TEST_CASES ${CMAKE_SOURCE_DIR}/tests/*.test.sh)
foreach(SCRIPT_TEST_CASE IN ITEMS ${SCRIPT_TEST_CASES})
add_test(
NAME ${LogTestCase}
NAME ${SCRIPT_TEST_CASE}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMAND ${CMAKE_BINARY_DIR}/newserv --replay-log=${LogTestCase} --config=${CMAKE_SOURCE_DIR}/tests/config.json)
endforeach()
file(GLOB ScriptTestCases ${CMAKE_SOURCE_DIR}/tests/*.test.sh)
foreach(ScriptTestCase IN ITEMS ${ScriptTestCases})
add_test(
NAME ${ScriptTestCase}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMAND ${ScriptTestCase} ${CMAKE_BINARY_DIR}/newserv)
COMMAND ${SCRIPT_TEST_CASE} ${CMAKE_BINARY_DIR}/newserv)
endforeach()
+3 -6
View File
@@ -1,5 +1,6 @@
# PSO Peeps newserv
# psopeeps-newserv
PSO Peeps is a private multi-platform Phantasy Star Online server supporting DC V2, PC V2, GC V3, and Blue Burst. Our ships feature XP boosts, optional experimental crossplay between all versions, increased difficulty tiers, and a hardcore mode.
This is the PSO Peeps maintained version of [newserv](https://github.com/fuzziqersoftware/newserv), a game server, proxy, and reverse-engineering tool for Phantasy Star Online.
The original project was created by fuzziqersoftware and contains years of reverse-engineering, documentation, and implementation work for PSO. This repository keeps that work as the foundation while carrying local changes used by PSO Peeps.
@@ -22,10 +23,6 @@ A copy of the upstream README is preserved here:
That document contains the original newserv history, setup notes, compatibility information, connection instructions, feature documentation, and technical reference material.
## Building
Build instructions are currently the same as upstream unless noted otherwise. See the original README for dependency and build details.
## License and attribution
This project remains based on newserv by fuzziqersoftware.
+1 -6
View File
@@ -1,13 +1,11 @@
## General
- Make a server patch version of story flag fixer quest
- Fix enemy flag mapping in v2/v3 crossplay and test
- Handle items in crossplay - use the replacement table
- Make proxy server handle all login commands on non-BB, including sending 9C when needed
- Add $switchit command (activates switch flag(s) for nearest object, e.g. laser fence, door, fog collision)
- Add a way to persist flags across connections, at least on v3, because of Meet User + B2 enable quest interactions - maybe update the quest to patch one of the login commands so the server can tell it's enabled
- Handle MeetUserExtensions properly in 41 and C4 commands on the proxy (rewrite the embedded 19 command and put some metadata in the persistent config, perhaps)
- Clean up ItemParameterTable implementation (see comment at the top of the class definition)
- Make UI strings localizable (e.g. entries in menus, welcome message, etc.)
## PSO DC
@@ -30,8 +28,5 @@
## PSOBB
- Figure out why Pouilly Slime EXP doesn't work
- Make server-specified rare enemies work with maps loaded by the proxy
- Implement serialization for various table types (ItemPMT, ItemPT, etc.)
- Record some BB tests
- Add all necessary Guild Card number rewrites in BB commands on the proxy
- Implement BB replay tests properly and record some
+99
View File
@@ -0,0 +1,99 @@
Subject: [PATCH] DC V2 EXP: server-side per-difficulty dispatcher hooked into set-events
Adds a `dispatch_dc_v2_exp_patch` helper that:
- no-ops unless the client is DC V2, supports send_function_call, has
`PsoPeepsV2EXP_enabled` in `auto_patches_enabled`, and is in an actual game
- reads the lobby's current difficulty
- looks up `PsoPeepsV2EXP_internal_{10|5}x_{normal|hard|vh|ult}` (10x preferred
if both deployed; falls back to 5x)
- sends it via the existing send_function_call coroutine
Hooks the dispatcher at the end of `on_trigger_set_event`, which fires on every
6x67 the client emits (i.e. every area transition that triggers map events).
This survives all difficulty/area cycling because the patch is re-applied on
every trigger.
The menu-visible shim `PsoPeepsV2EXP_enabled` uses a fixed key across both 5x
week and 10x weekend deploys, so a player's selection survives the systemd
file swap.
---
src/ReceiveSubcommands.cc | 41 +++++++++++++++++++++++++++++++++++++++++
1 file changed, 41 insertions(+)
diff --git a/src/ReceiveSubcommands.cc b/src/ReceiveSubcommands.cc
--- a/src/ReceiveSubcommands.cc
+++ b/src/ReceiveSubcommands.cc
@@ -- (around line 3637 — directly before the existing `on_trigger_set_event`) --
+// Dispatch the right per-difficulty DC V2 EXP table when the player has the
+// universal EXP shim enabled. The shim's body covers Normal; this corrects to
+// the actual loaded difficulty on every set-events trigger. No-op for non-DC-V2
+// clients, clients without the shim toggled on, or when the right internal
+// patch isn't currently deployed.
+static asio::awaitable<void> dispatch_dc_v2_exp_patch(std::shared_ptr<Client> c) {
+ if (c->version() != Version::DC_V2) {
+ co_return;
+ }
+ if (!c->check_flag(Client::Flag::HAS_SEND_FUNCTION_CALL)) {
+ co_return;
+ }
+ if (!c->login || !c->login->account) {
+ co_return;
+ }
+ if (!c->login->account->auto_patches_enabled.contains("PsoPeepsV2EXP_enabled")) {
+ co_return;
+ }
+
+ auto l = c->require_lobby();
+ if (!l->is_game()) {
+ co_return;
+ }
+
+ const char* diff_str;
+ switch (l->difficulty) {
+ case Difficulty::NORMAL: diff_str = "normal"; break;
+ case Difficulty::HARD: diff_str = "hard"; break;
+ case Difficulty::VERY_HARD: diff_str = "vh"; break;
+ case Difficulty::ULTIMATE: diff_str = "ult"; break;
+ default: co_return;
+ }
+
+ auto s = c->require_server_state();
+ // Try 10x first; fall back to 5x. The active multiplier is whichever set is
+ // deployed by the current week's systemd timer state.
+ for (int mult : {10, 5}) {
+ std::string key = std::format("PsoPeepsV2EXP_internal_{}x_{}", mult, diff_str);
+ std::shared_ptr<Function> fn;
+ try {
+ fn = s->client_functions->get(key, c->specific_version);
+ } catch (...) {
+ continue;
+ }
+ if (fn) {
+ co_await send_function_call(c, fn);
+ co_return;
+ }
+ }
+}
+
static asio::awaitable<void> on_trigger_set_event(shared_ptr<Client> c, SubcommandMessage& msg) {
auto l = c->require_lobby();
if (!l->is_game()) {
co_return;
}
const auto& cmd = msg.check_size_t<G_TriggerSetEvent_6x67>();
auto event_sts = l->map_state->event_states_for_id(c->version(), cmd.floor, cmd.event_id);
l->log.info_f("Client triggered set events with floor {:02X} and ID {:X} ({} events)",
cmd.floor, cmd.event_id, event_sts.size());
for (auto ev_st : event_sts) {
ev_st->flags |= 0x04;
if (c->check_flag(Client::Flag::DEBUG_ENABLED)) {
send_text_message_fmt(c, "$C5W-{:03X} START", ev_st->w_id);
}
}
forward_subcommand(c, msg);
+
+ co_await dispatch_dc_v2_exp_patch(c);
}
+54 -67
View File
@@ -26,7 +26,8 @@ See TODO.md for a list of known issues and future work I've curated, or go to th
* [Cross-version play](#cross-version-play)
* [Server-side saves](#server-side-saves)
* [Episode 3 features](#episode-3-features)
* [Memory patches, client functions, and DOL files](#memory-patches-and-client-functions)
* [Memory patches and client functions](#memory-patches-and-client-functions)
* [DOL loader](#dol-loader)
* [Using newserv as a proxy](#using-newserv-as-a-proxy)
* [Chat commands](#chat-commands)
* [REST API](#rest-api)
@@ -77,18 +78,21 @@ If you want to use parts of newserv in your project, there are two easy ways to
* If you're only using a few files from newserv, you can copy and paste the contents of the LICENSE file into a comment at the beginning of each copied file.
Some of the more likely useful files are:
* **src/BattleParamsIndex.hh**: Format of BattleParamEntry files
* **src/CommandFormats.hh**: Complete listing of all network commands used in all known versions of the game, and their formats
* **src/CommonItemSet.hh/cc**: Format of ItemPT files, shop definition files, and tekker adjustment tables
* **src/Compression.hh/cc**: PRS and BC0 compression and decompression algorithms
* **src/DCSerialNumbers.hh/cc**: PSO DC serial number validation algorithm and serial number generator
* **src/ItemData.hh**: Item format reference
* **src/ItemCreator.hh/cc**: Reverse-engineered item generator from Episodes 1&2 (used for all versions)
* **src/ItemParameterTable.hh**: Format of many structures in ItemPMT.prs
* **src/ItemParameterTable.cc**: Format of many structures in ItemPMT.prs (see BinaryItemParameterTableT)
* **src/Map.hh/cc**: Map file (.dat/.evt) structure, listing of object/enemy types and parameters, and reverse-engineered Challenge Mode random enemy generation algorithm
* **src/MagEvolutionTable.cc**: Format of ItemMagEdit.prs
* **src/QuestScript.cc**: Complete listing of all quest opcodes on all versions, along with their arguments and behavior
* **src/RareItemSet.hh/cc**: Format of ItemRT files (rare item drop tables)
* **src/SaveFileFormats.hh**: Definitions of save file structures for all versions
* **src/Episode3/DataIndexes.hh**: Episode 3 file structures, including card definition format and map/quest format
* **system/item-tables/names-v4.json**: Names of all items, indexed by the first 3 bytes of data1
* **system/tables/names-v4.json**: Names of all items, indexed by the first 3 bytes of data1
## Contributing to newserv
@@ -96,37 +100,18 @@ The goals of this project are:
* Build stable, extensible PSO server software that includes all vanilla functionality as well as optional modern conveniences, features, and cheats.
* Document the internals of PSO's network protocol, file formats, and game mechanics. This is mainly done through comments in the code.
This is a personal project; there is no official development team, official website, or official instance of newserv. Issues and pull requests are certainly welcome, but please only add content (e.g. quests or patches) that you've created, is already public, or you have permission to release publicly.
This is a personal project; there is no official development team, official website, or official instance of newserv. Issues and pull requests are certainly welcome, but please only add content (e.g. quests, patches, client functions) that you've created, is already public, or you have permission to release publicly.
No AI agents have been used in building, documenting, testing, or debugging this project, and any PRs authored by AI agents will be rejected.
# Compatibility
newserv supports all known versions of PSO, including various development prototypes. This table lists all versions that newserv supports. (NTE stands for Network Trial Edition; the GameCube beta versions were called Trial Edition instead, but we use the NTE abbreviation anyway for consistency.)
newserv is compatible with all versions of PSO, including all known development prototypes. For a full list of versions, see the [memory patches and client functions](#memory-patches-and-client-functions) section.
| Version | Lobbies | Games | Proxy |
|-----------------|----------|----------|----------|
| DC NTE | Yes | Yes | Yes |
| DC 11/2000 | Yes | Yes | Yes |
| DC 12/2000 | Yes | Yes | Yes |
| DC 01/2001 | Yes | Yes | Yes |
| DC V1 | Yes | Yes | Yes |
| DC 08/2001 | Yes | Yes | Yes |
| DC V2 | Yes | Yes | Yes |
| PC NTE | Yes (1) | Yes | Yes |
| PC | Yes | Yes | Yes |
| GC Ep1&2 NTE | Yes | Yes | Yes |
| GC Ep1&2 | Yes | Yes | Yes |
| GC Ep1&2 Plus | Yes | Yes | Yes |
| GC Ep3 NTE | Yes | Yes (2) | Yes |
| GC Ep3 | Yes | Yes | Yes |
| Xbox Ep1&2 Beta | Yes (3) | Yes (3) | Yes (3) |
| Xbox Ep1&2 | Yes (3) | Yes (3) | Yes (3) |
| BB (vanilla) | Yes | Yes | Yes |
| BB (Tethealla) | Yes | Yes | Yes |
*Notes:*
1. *This is the only version of PSO that doesn't have any way to identify the player's account - there is no serial number or username. For this reason, AllowUnregisteredUsers must be enabled in config.json to support PC NTE, and PC NTE players receive a random Guild Card number every time they connect. To prevent abuse, PC NTE support can be disabled in config.json.*
2. *Episode 3 NTE battles are not well-tested; some things may not work. See notes/ep3-nte-differences.txt for a list of known differences between NTE and the final version. NTE and non-NTE players cannot battle each other.*
3. *PSO Xbox connects through Xbox Live, so you can't easily host a private server for this version of the game. See the [How to connect](#pso-xbox) section.*
There are a few version-specific quirks to be aware of:
* PC NTE is the only version of PSO that doesn't have any way to identify the player's account - there is no serial number or username. For this reason, AllowUnregisteredUsers must be enabled in config.json to support PC NTE, and PC NTE players receive a random Guild Card number every time they connect. To prevent abuse, PC NTE support can be disabled in config.json.
* Episode 3 Trial Edition battles are not well-tested; some things may not work. See notes/ep3-nte-differences.txt for a list of known differences between Trial Edition and the final version. Trial Edition and non-Trial-Edition players cannot battle each other.
* PSO Xbox connects through Xbox Live, so you can't easily host a private server for the Xbox version of the game. See the [how to connect](#pso-xbox) section.
# Setup
@@ -376,7 +361,7 @@ In the `private` and `duplicate` modes, there is no incentive to pick up items b
The drop mode can be changed at any time during a game with the `$dropmode` chat command. If the mode is changed after some items have already been dropped, the existing items retain their visibility (that is, items dropped in private mode still can't be picked up by other players since they were dropped before the mode was changed). You can configure which drop modes are used by default, and which modes players are allowed to choose, in config.json. See the comments above the AllowedDropModes and DefaultDropMode keys.
In the server drop modes, the item tables used to generate common items are in the `system/item-tables/ItemPT-*` files. (The V2 files are used for V1 as well.) The rare item tables are in the `rare-table-*.json` files. Unlike the original formats, it's possible to make each enemy drop multiple different rare items at different rates, though the default tables never do this.
In the server drop modes, the item tables used to generate common items are in the `system/tables/common-table-*` files. The rare item tables are in the `rare-table-*.json` files. Unlike the original formats, it's possible to make each enemy drop multiple different rare items at different rates, though the default tables never do this.
## Cross-version play
@@ -461,9 +446,9 @@ Like quests, Episode 3 card definitions, maps, and quests are cached in memory.
## Memory patches and client functions
You can put assembly files in the system/client-functions directory with filenames like PatchName.VERS.patch.s and they will appear in the Patches menu for clients that support client functions. Client functions are written in SH-4, PowerPC, or x86 assembly and are compiled when newserv is started. The assembly system's features are documented in the comments in system/client-functions/System/WriteMemoryGC.ppc.s.
newserv supports sending compiled functions to run on the client, for most PSO versions. These functions are written in SH-4, PowerPC, or x86 assembly and compiled during server startup. This is generally used for applying code patches to the client, but can also be used to implement new functionality, since the functions may be run at any time. There are many options that control client function behavior (including whether they appear in the Patches menu or can be run via the `$patch` chat command); see system/client-functions/System/WriteMemory.s for full documentation.
The VERS token in client function filenames refers to the specific version of the game that the client function applies to. Some versions do not support receiving client functions at all. *Note: newserv uses the shorter GameCube versioning convention, where discs labeled DOL-XXXX-0-0Y are version 1.Y. The PSO community seems to use the convention 1.0Y in some places instead, but these are the same version. For example, the version that newserv calls v1.4 is the same as v1.04, and is labeled DOL-GPOJ-0-04 on the underside of the disc.*
In these files, you'll see `.versions` lines specifying which specific versions of the game the client function is compatible with. Some versions do not support receiving client functions at all. *Note: newserv uses the shorter GameCube versioning convention, where discs labeled DOL-XXXX-0-0Y are version 1.Y. The PSO community seems to use the convention 1.0Y in some places instead, but these are the same version. For example, the version that newserv calls v1.4 is the same as v1.04, and is labeled DOL-GPOJ-0-04 on the underside of the disc.*
The specific versions are:
@@ -511,7 +496,7 @@ The specific versions are:
*Notes:*
1. *Client functions are only supported on these versions if EnableSendFunctionCallQuestNumbers is set in config.json. See the comments there for more information.*
newserv comes with a set of patches for many of the above versions. These are organized in subdirectories within system/client-functions/.
newserv comes with a set of patches for many of the above versions, in system/client-functions/.
### DOL loader
@@ -541,11 +526,11 @@ There are many options available when starting a proxy session. All options are
* **Switch assist**: unlocks doors that require two or four players in a one-player game, when you step on one of the switches.
* **Infinite Meseta** (Episode 3 only): gives you 1,000,000 Meseta, regardless of the value sent by the remote server.
* **Block events**: disables holiday events sent by the remote server.
* **Block patches**: prevents any B2 (patch) commands from reaching the client.
* **Block patches**: prevents any B2 (client function / patch) commands from reaching the client.
* **Save files**: saves copies of several kinds of files when they're sent by the remote server. The files are written to the current directory (which is usually the directory containing the system/ directory). Saved files can then be used with newserv by just moving the file into the appropriate place in the system/ directory and renaming it appropriately. These kinds of files can be saved:
* Online quests and download quests (saved as .bin/.dat files)
* GBA games (saved as .gba files)
* Patches (saved as .bin files and disassembled as .txt files)
* Client functions / patches (saved as .bin files and disassembled as .txt files)
* Player, system, and Guild Card data from BB sessions (saved as .psochar, .psosys, .psosysteam, and .psocard files)
* Stream file data from BB sessions (saved as ItemPMT, BattleParamEntry, ItemMagEdit, and PlyLevelTbl files)
* Episode 3 online quests and maps (saved as .mnmd files)
@@ -591,6 +576,7 @@ Some commands only work for clients not in proxy sessions. The chat commands are
* You'll see in-game messages from the server when you take some actions, like killing enemies, opening boxes, or flipping switches.
* You'll see the rare seed value and floor variations when you join a game.
* You'll be placed into the last available slot in lobbies and games instead of the first, unless you're joining a BB solo-mode game.
* You'll be able to run any client function with `$patch`, not only those that are marked visible.
* You'll be able to join games with any PSO version, not only those for which cross-version play is normally enabled. See the "Cross-version play" section above for details on this.
* `$readmem <address>`: Read 4 bytes from the given address and show you the values.
* `$writemem <address> <data>`: Write data to the given address. Data is not required to be any specific size.
@@ -619,7 +605,7 @@ Some commands only work for clients not in proxy sessions. The chat commands are
* `$ln [name-or-type]`: Set the lobby number. Visible only to you. This command exists because some non-lobby maps can be loaded as lobbies with invalid lobby numbers. See the "GC lobby types" and "Ep3 lobby types" entries in the information menu for acceptable values here. Note that non-lobby maps do not have a lobby counter, so there's no way to exit the lobby without using either `$ln` again or `$exit`. On the game server, `$ln` reloads the lobby immediately; on the proxy, it doesn't take effect until you load another lobby yourself (which means you'll like have to use `$exit` to escape). Run this command with no argument to return to the default lobby.
* `$swa`: Enable or disable switch assist. When enabled, the server will unlock two-player and four-player doors in non-quest games when you step on any of the required switches.
* `$exit`: If you're in a lobby, send you to the main menu (which ends your proxy session, if you're in one). If you're in a game or spectator team, send you to the lobby (but does not end your proxy session if you're in one). Does nothing if you're in a non-Episode 3 game and no quest is in progress.
* `$patch <name>`: Run a patch on your client. `<name>` must exactly match the name of a patch on the server.
* `$patch <name>`: Run a client function. `<name>` must exactly match the name of a client function on the server.
* Character data commands (non-proxy only)
* `$switchchar <slot>` (BB only): Switch to a different character from your account without logging out.
@@ -777,36 +763,37 @@ newserv has many CLI options, which can be used to access functionality other th
The data formats that newserv can convert to/from are:
| Format | Encode/compress action | Decode/extract action |
|-------------------------------------|---------------------------|------------------------------|
| PRS compression | `compress-prs` | `decompress-prs` |
| PR2/PRC compression | `compress-pr2` | `decompress-pr2` |
| BC0 compression | `compress-bc0` | `decompress-bc0` |
| Raw encrypted data | `encrypt-data` | `decrypt-data` |
| Episode 3 command mask | `encrypt-trivial-data` | `decrypt-trivial-data` |
| Challenge Mode rank text | `encrypt-challenge-data` | `decrypt-challenge-data` |
| PSO DC quest file (.vms) | None | `decode-vms` |
| PSO GC quest file (.gci) | None | `decode-gci` |
| Download quest file (.dlq) | None | `decode-dlq` |
| Server quest file (.qst) | `encode-qst` | `decode-qst` |
| PSO DC save file (.vms) | `encrypt-vms-save` | `decrypt-vms-save` |
| PSO PC save file | `encrypt-pc-save` | `decrypt-pc-save` |
| PSO GC save file (.gci) | `encrypt-gci-save` | `decrypt-gci-save` |
| PSO Xbox save file | None | `decrypt-xbox-save` |
| PSO GC snapshot file | None | `decode-gci-snapshot` |
| Quest script (.bin) | `assemble-quest-script` | `disassemble-quest-script` |
| Quest map (.dat) | None | `disassemble-quest-map` |
| AFS archive (.afs) | None | `extract-afs` |
| BML archive (.bml) | None | `extract-bml` |
| PPK archive (.ppk) | None | `extract-ppk` |
| GSL archive (.gsl) | `generate-gsl` | `extract-gsl` |
| GVM texture (.gvm) | `encode-gvm` | None |
| Bitmap font (.fon) | `encode-bitmap-font` | `decode-bitmap-font` |
| Text archive | `encode-text-archive` | `decode-text-archive` |
| Unicode text set | `encode-unicode-text-set` | `decode-unicode-text-set` |
| Word Select data set | None | `decode-word-select-set` |
| Set data table | None | `disassemble-set-data-table` |
| Rare item table (AFS/GSL/JSON/HTML) | `convert-rare-item-set` | `convert-rare-item-set` |
| Format | Encode/compress action | Decode/extract action |
|-------------------------------------|-------------------------------|-------------------------------|
| PRS compression | `compress-prs` | `decompress-prs` |
| PR2/PRC compression | `compress-pr2` | `decompress-pr2` |
| BC0 compression | `compress-bc0` | `decompress-bc0` |
| Raw encrypted data | `encrypt-data` | `decrypt-data` |
| Episode 3 command mask | `encrypt-trivial-data` | `decrypt-trivial-data` |
| Challenge Mode rank text | `encrypt-challenge-data` | `decrypt-challenge-data` |
| PSO DC quest file (.vms) | None | `decode-vms` |
| PSO GC quest file (.gci) | None | `decode-gci` |
| Download quest file (.dlq) | None | `decode-dlq` |
| Server quest file (.qst) | `encode-qst` | `decode-qst` |
| PSO DC save file (.vms) | `encrypt-vms-save` | `decrypt-vms-save` |
| PSO PC save file | `encrypt-pc-save` | `decrypt-pc-save` |
| PSO GC save file (.gci) | `encrypt-gci-save` | `decrypt-gci-save` |
| PSO Xbox save file | None | `decrypt-xbox-save` |
| PSO GC snapshot file | None | `decode-gci-snapshot` |
| Quest script (.bin) | `assemble-quest-script` | `disassemble-quest-script` |
| Quest map (.dat) | None | `disassemble-quest-map` |
| AFS archive (.afs) | None | `extract-afs` |
| BML archive (.bml) | None | `extract-bml` |
| PPK archive (.ppk) | None | `extract-ppk` |
| GSL archive (.gsl) | `generate-gsl` | `extract-gsl` |
| GVM texture (.gvm) | `encode-gvm` | None (use resource_dasm) |
| Bitmap font (.fon) | `encode-bitmap-font` | `decode-bitmap-font` |
| Text archive | `encode-text-archive` | `decode-text-archive` |
| Unicode text set | `encode-unicode-text-set` | `decode-unicode-text-set` |
| Word Select data set | None | `decode-word-select-set` |
| Set data table | None | `disassemble-set-data-table` |
| Rare item table (AFS/GSL/JSON/HTML) | `convert-rare-item-set` | `convert-rare-item-set` |
| Item definitions (ItemPMT) | `encode-item-parameter-table` | `decode-item-parameter-table` |
There are several actions that don't fit well into the table above, which let you do other things:
+40 -2
View File
@@ -41,6 +41,7 @@ Version codes (from README.md):
4OEU: PSO Xbox US TU
4OPD: PSO Xbox EU Disc
4OPU: PSO Xbox EU TU
50YJ: PSO BB US 1.24.3
59NJ: PSO BB JP 1.25.11
59NL: PSO BB JP 1.25.13 (including the Tethealla client)
@@ -81,12 +82,16 @@ Disable item equip restrictions ("God of equip")
3OJ5 => 041050D4 38000005
3OJT => 0415BF50 38000005
3OP0 => 041052D4 38000005
5OYJ => 005C8C8F E9A7000000
59NJ => 005C9F35 E9A7000000
59NL => 005C9F31 E9A7000000
All items visible in Pioneer 2
3OE1 => 04102D88 38600000
Mags visible in Pioneer 2
5OYJ => 005D7053 EB04
59NJ => 005D8F27 EB04
59NL => 005D8F4B EB04
Disable pause menu background + offset
@@ -94,6 +99,12 @@ Disable pause menu background + offset
0428735C 4800000C
3OE2 => 0424CED8 48000370
042887D8 4800000C
5OYJ => 00713758 9090
0072D417 9090
0072D27E 90E9
59NJ => 00719C58 9090
00733C57 9090
00733ABE 90E9
59NL => 00719B54 9090
00733BA7 9090
00733A0E 90E9
@@ -637,6 +648,10 @@ Fast tekker (skips wind-up jingle)
0023EF77 jmp +0x0A
4OPU => 0023F14C mov dword [ebp + 0x14C], 1
0023F167 jmp +0x0A
5OYJ => 006D3F7B mov dword [edi + 0x14C], 1
006D3F98 jmp +0x0B
59NJ => 006DA14B mov dword [edi + 0x14C], 1
006DA168 jmp +0x0B
59NL => 006DA113 mov dword [edi + 0x14C], 1
006DA130 jmp +0x0B
@@ -667,8 +682,20 @@ Allow loading corrupted save files
041156D4 4E800020
60 frames per second
This does not adjust any logic or animations; everything just runs faster
3OE1 => 045CDEF8 00000001
This doesn't adjust any logic or animations; everything just runs faster
3OJT => 043F5AC0 38800001
3OJ2 => 043D8550 38800001
3OJ3 => 043DAF58 38800001
3OJ4 => 043DCDF8 38800001
3OJ5 => 043DCBA8 38800001
3OE0 => 043D9820 38800001
3OE1 => 043D9878 38800001
3OE2 => 043DCF78 38800001
3OP0 => 043DBA68 38800001
3SJT => 043567AC 38800001
3SE0 => 0438A804 38800001
3SJ0 => 043897B4 38800001
3SP0 => 0438B6D4 38800001
Show extended item info when targeting a dropped item
(Compiled from the ExtendedItemInfo patch, also written by me)
@@ -973,6 +1000,10 @@ Override Challenge mode random enemy location tables limit
4OEU => 002E742C XXXXXXXX (count as little-endian dword)
4OPD => 002E720C XXXXXXXX (count as little-endian dword)
4OPU => 002E745C XXXXXXXX (count as little-endian dword)
5OYJ => 008075C3 XXXXXXXX (count * 4 as little-endian dword)
008075DC XXXXXXXX (count as little-endian dword)
59NJ => 0080FA3F XXXXXXXX (count * 4 as little-endian dword)
0080FA58 XXXXXXXX (count as little-endian dword)
59NL => 0080ECB7 XXXXXXXX (count * 4 as little-endian dword)
0080ECD0 XXXXXXXX (count as little-endian dword)
@@ -1108,3 +1139,10 @@ Rappy size modifier
3OE1 => 040C1E24 48000020 // Disable flag check in render
045D0718 40800000 // X/Z scale as float (here, 4.0)
045D071C 40800000 // Y scale as float (here, 4.0)
Disable HP reduction warning sound in Challenge mode
3OE1 => 04076A28 4E800020
Mag invincibility effect sparkliness modifier
(Default 003C; smaller values are more sparkly)
3OE1 => 801131C4 3860XXXX
+1 -2
View File
@@ -4,7 +4,6 @@ import subprocess
import sys
from dataclasses import dataclass
version_tokens = ("3OJ2", "3OJ3", "3OJ4", "3OJ5", "3OE0", "3OE1", "3OE2", "3OP0")
@@ -62,7 +61,7 @@ def write_patches_for_code(
f.write("reloc0:\n")
f.write(" .offsetof start\n")
f.write("start:\n")
f.write(" .include WriteCodeBlocksGC\n")
f.write(" .include WriteCodeBlocks\n")
for region in write_regions:
f.write(
f" # region @ {region.address:08X} ({len(region.data) * 4} bytes)\n"
+1 -1
View File
@@ -127,7 +127,7 @@ MOVEMENT DATA 1E (YOWIE_DESERT)
fparam1 = TODO: 59NL:005AEBC5; looks like an angle in degrees (range [0, 359])
fparam2 = TODO: 59NL:005AEBEE
MOVEMENT DATA 0D (DARK_BRINGER)
MOVEMENT DATA 0D (CHAOS_BRINGER)
fparam1 = TODO: 3OE1:FUN_80097F98; NNF: charge speed
fparam2 = TODO: 3OE1:FUN_800983F8; NNF: movement speed
fparam6 = TODO: 3OE1:80097F3C; NNF: Regular attack cooldown. Delay between going red and shooting.
+73
View File
@@ -0,0 +1,73 @@
########################################################################
DOWNLOAD
########################################################################
The official installer for this client is seemingly lost to time.
However, we do still have access to a download link to a directory of
the game client. Located at the bottom of this post:
https://github.com/fuzziqersoftware/newserv/discussions/734
The correct client exe to use, would be PsoBB.pat inside the
"3. PSOBB Executable" directory. While the file extension is .pat, it
can be renamed and changed to .exe .
However, PsoBB.exe in its current state will not work on its own.
As it is packed with a version of ASProtect. Which will impede you from
removing GameGuard, as well as modifying the client to connect to a desired
IP address.
There are two ways around this.
1. Use a code injection dll
2. Unpack the exe
As far as I know, There is currently not any code injection dll projects
available for use with this client. So our main option is going to be
unpacking the client.
There are several ways to unpack a client. For the sake of simplicity, we
will use a automated program.
Something like:
https://github.com/Hendi48/ASpirin
Originally found in this issue:
https://github.com/fuzziqersoftware/newserv/issues/748
You will know the process was successful if the new resulting exe file
has a much larger file size than the original.
########################################################################
REMOVE GAMEGUARD
########################################################################
The first step in being able to use this client, is removing GameGuard.
In order to do this, we will prevent GameGuard from initializing by
forcing the responsible function to return.
00844A9C - ret (or C3 in hex)
This will effectively stop GameGuard from ever starting.
However, the client has checks on startup to see if GameGuard is running,
and will close the game if it detects otherwise.
008444BB - jmp 008444DD
Now there is nothing in the way from starting up the game.
Find and edit the client's IP addresses, and have fun.
########################################################################
NOTES
########################################################################
Despite being a US client primarily using english, the client seems to
still have a reliance on having Japanese-IME enabled.
You can get around any kind of issue with this by patching out the need
for IME.
008582CC - call dword ptr ds:[0x008E0228]
Alternatively, in a hex editor, you can search for:
"EB 1A 6A 00 FF 15 9C C3"
Once found, replace with:
"EB 1A 6A 00 FF 15 28 02"
+1 -1
View File
@@ -3,7 +3,7 @@ PSOBB SUPPORT FILES, NOTES & RESOURCES
--------------------------------------------------------------------------------
CLIENT LOCALIZATION
By default PSOBB loads everything in Japanese so it requires some extra files
By default PSOBB JP clients load everything in Japanese so it requires some extra files
to properly implement the English localization from SOA, these files are offered
here inside the usbb-resources folder for your convenience they are the same ones
from the old official USBB client
+340 -187
View File
@@ -1,187 +1,340 @@
0007 = Set by rico capsule in caves
000B = P2 Tyrell Start
000C = P2 Irene Start
000D = P2 Scientist 1 Start
000E = P2 Scientist 2 Start
000F = P2 More Scientist stuff.
0010 = P2 Irene after talking to Tyrell
0011 = Read a rico capsule (any)
0012 = P2 Scientist after talking to Irene.
0013 = P2 Menu 6, quest counter / Tekker talked to
0014 = Entered Forest 1
0015 = Entered Forest 2
0016 = Entered Dragon Area
0017 = Dragon defeated
0018 = Caves unlocked
0018 = P2 Principle after defeating dragon
0019 = P2 Scientist after defeating dragon
001E = Entered Caves 1 (Gov 2-1)
001F = Entered De Rol Le in 2-4
0020 = De Rol Le defeated
0021 = Mines unlocked (P2 Tyrell after defeating De Rol Le)
0028 = Entered Mines 1
0029 = Entered Vol Opt Area
002A = Defeated Vol Opt
002B = Set by rico capsule about the 3 seals (after vol opt).
002C = Activated Forest monument
002D = Activated Caves monument (Gov 2-2)
002E = Activated Mines monument
002F = Activated all monuments
0030 = Entered Ruins 1
0032 = Entered Falz 1
0035 = Hard mode unlocked
0036 = Entered Falz 3 // Very Hard mode unlocked (?)
0037 = Ultimate unlocked
0046 = One CCA door lock unlocked
0047 = One CCA door lock unlocked
0048 = One CCA door lock unlocked
0049 = Entered Laboratory
004A = Lab Assistant Start
004B = Entered Temple Beta
004C = Defeated Barba Ray
004D = Lab Assistant after defeating barba ray
004E = Entered Spaceship Beta
004F = Defeated Gol Dragon
0051 = Entered CCA
0052 = Defeated Gal Gyrphon // Defeated Gol dragon in seat of heart (?)
0054 = Entered Seabed Upper
0057 = Defeated Olga Flow
005B = Lab Natasha Start
005C = Lab Natasha after VR temple
005D = Lab Natasha after VR Spaceship
005E = Lab Assistant after defeating Gal gryphon
005F = After reading the last capsule from flowen
0060 = Lab Natasha after CCA
0065 = Cleared Magnitude of Metal
0067 = Cleared Claiming a Stake
0069 = Cleared Value of Money
006B = Cleared Battle Training
006D = Cleared Journalistic Pursuit
006F = Cleared The Fake in Yellow
0071 = Cleared Native Research
0073 = Cleared Forest of Sorrow
0075 = Cleared Gran Squall
0077 = Cleared Addicting Food
0079 = Cleared The Lost Bride
007B = Cleared Waterfall Tears
007D = Cleared Black Paper
007F = Cleared Secret Delivery
0081 = Cleared Soul of a Blacksmith
0083 = Cleared Letter from Lionel
0085 = Cleared The Grave's Butler
0087 = Cleared Knowing One's Heart
0089 = Cleared The Retired Hunter
008B = Cleared Dr. Osto's Research
008D = Cleared Unsealed Door
008F = Cleared Soul of Steel
0091 = Cleared Doc's Secret Plan (able to make enemy part weapons)
0093 = Cleared Seek my Master
0095 = Cleared From the Depths
0096 = Unknown (set in the fake in yellow)
0097 = Seat of heart unknown
009B = Cleared Central Dome Fire Swirl
00A1 = Cleared Seat of the Heart
00C9 = Got an enemy weapon converted
00CA = unknown Fake In Yellow
00CE = unknown Fake In Yellow
00D3 = Dr.Osto's research black paper subplot. Told Sue your name
00D4 = Dr.Osto's research black paper subplot. Didn't tell Sue your name from before.
00D5 = Dr.Osto's research black paper subplot. Did tell Sue your name from before.
00D6 = Unsealed door. black paper subplot Talked to Sue. Refused to tell her your name
00D7 = Unsealed door. black paper subplot. bernie tells you Sue is part of black paper.
00D8 = Black paper subplot in waterfall of tears talking to Sue
00D9 = Black paper subplot in Black paper talking to Sue (used option 2)
00DB = Black paper subplot in Black paper talking to Sue (used any option)
00DE = Black paper subplot in Black paper talked to Sue at the end of quest?
00DF = Knowing ones heart talked to Bernie?
00E0 = Seek my master. Zoke ,Donoph subplot?
00E2 = Bernie Gran Squall
00E7 = Defeated Kireek in waterfall of tears
00E8 = Black paper subplot in black paper. defeated Kireek...
00EB = Black paper subplot in from the depths. Defeated Kireek and got soul eater!
00F1 = Secret delivery. Started the Weapons subplot //is cleared if quest is left
00F3 = Weapon badge approval for claiming the snake //is cleared if quest is left
00F4 = Weapon badge approval for the lost bride //is cleared if quest is left
00F5 = Weapon badge approval for gran squall //is cleared if quest is left
00F6 = Secret delivery. Got AKIKO's FRYING PAN!
00FB = Got Orochi-agito
00FD = Unknown addicting food
0105 = Central dome fire swirl. Got Glory of the past!
0106 = Central dome fire swirl. Got Mark3.
0107 = Central dome fire swirl. got Sonic knuckles
0108 = Central dome fire swirl. got mail from BOGARDE
0109 = Central dome fire swirl. got mail from ANNA
010A = Central dome fire swirl. got mail from NADJA
010B = Central dome fire swirl. got mail from Lionel
010C = Soul of the blacksmith. Got one of the 3 special weapons!
010D = Donoph Baz dies The Retired Hunter
010E = Seat of heart unknown
010F = Seat of heart unknown
0110 = Seat of heart unknown
0111 = Seat of heart unknown
0112 = Seat of heart unknown
0113 = Seat of heart unknown
0187 = Soul of steel. Got Marina's bag! //dreamcast
0188 = Soul of steel. Unknown.
0191 = Capsule Elly VR
0197 = Cleared VR Temple
01AD = Capsule elly CCA
01AE = Capsule elly CCA
01B3 = After reading a capsule from flowen
01D6 = Set after unlocking vr spaceship
01F5 = Episode1: Cleared government 1-1
01F7 = Episode1: Cleared government 1-2
01F9 = Episode1: Cleared government 1-3
01FB = Episode1: Cleared government 2-1
01FD = Episode1: Cleared government 2-2
01FF = Episode1: Cleared government 2-3
0201 = Episode1: Cleared government 2-4
0203 = Episode1: Cleared government 3-1
0205 = Episode1: Cleared government 3-2
0207 = Episode1: Cleared government 3-3
0209 = Episode1: Cleared government 4-1
020B = Episode1: Cleared government 4-2
020D = Episode1: Cleared government 4-3
020F = Episode1: Cleared government 4-4
0211 = Episode1: Cleared government 4-5
0213 = Episode2: Cleared government 5-1 // Talked to Tekker (?)
0214 = Entered Forest 1
0215 = Episode2: Cleared government 5-2
0217 = Episode2: Cleared government 5-3 // Defeated Dragon (?)
0219 = Episode2: Cleared government 5-4
021B = Episode2: Cleared government 5-5
021D = Episode2: Cleared government 6-1
021F = Episode2: Cleared government 6-2
0220 = Defeated De Rol Le
0221 = Episode2: Cleared government 6-3
0223 = Episode2: Cleared government 6-4
0225 = Episode2: Cleared government 6-5
0227 = Episode2: Cleared government 7-1
0229 = Episode2: Cleared government 7-2
022A = Defeated Vol Opt (002A and 022A together on hard mode)
022B = Episode2: Cleared government 7-3 // Rico capsule after Vol Opt, at Ruins door (?)
022D = Episode2: Cleared government 7-4 // Entered Caves 2 (?)
022F = Episode2: Cleared government 7-5
0230 = Entered Ruins 1
0231 = Episode2: Cleared government 8-1
0233 = Episode2: Cleared government 8-2
0234 = Entered Falz 2
0235 = Episode2: Cleared government 8-3
0246 = Activated Jungle East big door switch
0248 = Activated Seaside big door switch
024F = Defeated Gol Dragon
0252 = Defeated Gal Gryphon
02BD = Episode4: Cleared government 9-1
02BE = Episode4: Cleared government 9-2
02BF = Episode4: Cleared government 9-3
02C0 = Episode4: Cleared government 9-4
02C1 = Episode4: Cleared government 9-5
02C2 = Episode4: Cleared government 9-6
02C3 = Episode4: Cleared government 9-7
02C4 = Episode4: Cleared government 9-8
0314 = Entered Forest 1
0330 = Entered Ruins 1
03FA = P2 Menu 7, G-Counter // Talked to Momoka
03FB = Nol start
03FC = Cleared Ep2 government on ultimate
03FE = Cleared Ep2 government on normal-vh
* = verified from client or quest disassembly
0007 = Set by rico capsule in caves
000B * = Has seen first single-mode Tyrell conversation (Episode 1)
000C = P2 Irene Start
000D = P2 Scientist 1 Start
000E = P2 Scientist 2 Start
000F = P2 More Scientist stuff.
0010 = P2 Irene after talking to Tyrell
0011 = Read a rico capsule (any)
0012 = P2 Scientist after talking to Irene.
0013 = P2 Menu 6, quest counter / Tekker talked to
0014 * = Has been to Forest 1
0015 = Entered Forest 2
0016 * = TBoss1Dragon encountered
0017 * = Dragon defeated (Caves unlocked in online/multi mode (pre-V4); sets command 10 quest tier to 1)
0018 * = Talked with Tyrell after defeating Dragon (Caves unlocked in offline single mode (pre-V4))
0019 = P2 Scientist after defeating dragon
001E * = Has been to Cave 1
001F = Entered De Rol Le in 2-4
0020 * = De Rol Le defeated (Mines unlocked in online/multi mode (pre-V4); sets command 10 quest tier to 2)
0021 * = Talked with Tyrell after defeating De Rol Le (Mines unlocked in offline single mode (pre-V4))
0028 * = Has been to Mine 1
0029 * = TBoss3Volopt encountered
002A * = Vol Opt defeated (Ruins unlocked in online/multi mode (pre-V4); sets command 10 quest tier to 3)
002B = Set by rico capsule about the 3 seals (after vol opt).
002C * = Activated Forest monument (checked by TODoorVoShip_update and TODoorVoShip_init)
002D * = Activated Caves monument (checked by TODoorVoShip_update and TODoorVoShip_init)
002E * = Activated Mines monument (checked by TODoorVoShip_update and TODoorVoShip_init)
002F * = Activated all monuments (checked by TODoorVoShip_update and TODoorVoShip_init; causes Vol Opt to construct different objects upon defeat; see 3OE1:8003CB50, 3OE1:80040E68)
0030 * = Has been to Ruins 1 (Ruins unlocked in offline single mode (pre-V4))
0032 * = TBoss4Type1 encountered
0033 * = TBoss4Type1 defeated; sets command 10 quest tier to 4
0034 * = TBoss4Type2 encountered
0035 * = TBoss4Type2 defeated; unlocks Hard if set in Normal
0036 * = TBoss4Type3 encountered
0037 * = TBoss4Type3 defeated; unlocks Very Hard or Ultimate if set in Hard or Very Hard respectively
0046 * = First CCA door lock unlocked (TODO: Cleared by 59NL:006585E4; is this triggered anywhere?)
0047 * = Second CCA door lock unlocked (TODO: Cleared by 59NL:006585E4; is this triggered anywhere?)
0048 * = Third CCA door lock unlocked (TODO: Cleared by 59NL:006585E4; is this triggered anywhere?)
0049 = Entered Laboratory
004A = Lab Assistant Start
004B * = Has been to VR Temple Beta
004C * = Barba Ray defeated (VR Spaceship unlocked in online/multi mode (pre-V4))
004D * = Talked with Lab Assistant after defeating Barba Ray (VR Spaceship unlocked in offline single mode (pre-V4))
004E * = Has been to VR Spaceship Beta
004F * = TBoss8Dragon defeated (CCA unlocked in online/multi mode (pre-V4))
0050 * = CCA unlocked in offline single mode (pre-V4)
0051 * = Has been to CCA
0052 * = Gal Gryphon defeated (Seabed unlocked in online/multi mode (pre-V4))
0053 * = Seabed unlocked in offline single mode (pre-V4)
0054 * = Has been to Seabed Upper
0055 * = Has been to Seabed Lower
0056 * = TBoss6PlotFalz encountered
0057 * = TBoss6PlotFalz defeated (unlocks the next difficulty level)
005B * = Has seen first single-mode Natasha conversation (Episode 1)
005C = Lab Natasha after VR temple
005D = Lab Natasha after VR Spaceship
005E = Lab Assistant after defeating Gal gryphon
005F = After reading the last capsule from flowen
0060 = Lab Natasha after CCA
0064 * = Magnitude of Metal unlocked
0065 * = Magnitude of Metal completed
0066 * = Claiming A Stake unlocked
0067 * = Claiming A Stake completed
0068 * = The Value of Money unlocked
0069 * = The Value of Money completed
006A * = Battle Training unlocked
006B * = Battle Training completed
006C * = Journalistic Pursuit unlocked
006D * = Journalistic Pursuit completed
006E * = The Fake in yellow unlocked
006F * = The Fake in yellow completed
0070 * = Native Research unlocked
0071 * = Native Research completed
0072 * = Forest of Sorrow unlocked
0073 * = Forest of Sorrow completed
0074 * = Gran Squall unlocked
0075 * = Gran Squall completed
0076 * = Addicting Food unlocked
0077 * = Addicting Food completed
0078 * = The Lost Bride unlocked
0079 * = The Lost Bride completed
007A * = Waterfall tears unlocked
007B * = Waterfall tears completed
007C * = Black Paper unlocked
007D * = Black Paper completed
007E * = Secret Delivery unlocked
007F * = Secret Delivery completed
0080 * = Soul of a Blacksmith unlocked
0081 * = Soul of a Blacksmith completed
0082 * = Letter from Lionel unlocked
0083 * = Letter from Lionel completed
0084 * = The Grave's Butler unlocked
0085 * = The Grave's Butler completed
0086 * = Knowing One's Heart unlocked
0087 * = Knowing One's Heart completed
0088 * = Retired Hunter unlocked
0089 * = Retired Hunter completed
008A * = Dr. Osto's Research unlocked
008B * = Dr. Osto's Research completed
008C * = The Unsealed Door unlocked
008D * = The Unsealed Door completed
008E * = Soul of Steel unlocked
008F * = Soul of Steel completed
0090 * = Doc's Secret Plan unlocked
0091 * = Doc's Secret Plan completed
0092 * = Seek My Master unlocked
0093 * = Seek My Master completed
0094 * = From the Depths unlocked
0095 * = From the Depths completed
0096 = Unknown (set in the fake in yellow)
0097 * = TBoss4Type3 defeated while any player was using Dark Flow, Dark Meteor, or Dark Bridge
0098 * = TBoss6PlotFalz defeated while any player was using a Read weapon (e.g. Red Saber, Reg Handgun, etc.) or any of Rico's armors (Red Ring, Rico's Glasses, Rico's Earring)
0099 * = TBoss4Thanks destroyed
009A * = TBoss6Thanks destroyed
009B = Cleared Central Dome Fire Swirl
009F * = Central Dome Fire Swirl unlocked
00A0 * = Central Dome Fire Swirl completed
00A1 * = Seat of the Heart completed
00A2 * = The East Tower completed
00A3 * = The East Tower completed
00A4 * = The West Tower completed
00A5 * = The West Tower completed
00C9 = Got an enemy weapon converted
00CA = unknown Fake In Yellow
00CE = unknown Fake In Yellow
00D3 = Dr.Osto's research black paper subplot. Told Sue your name
00D4 = Dr.Osto's research black paper subplot. Didn't tell Sue your name from before.
00D5 = Dr.Osto's research black paper subplot. Did tell Sue your name from before.
00D6 = Unsealed door. black paper subplot Talked to Sue. Refused to tell her your name
00D7 = Unsealed door. black paper subplot. bernie tells you Sue is part of black paper.
00D8 = Black paper subplot in waterfall of tears talking to Sue
00D9 = Black paper subplot in Black paper talking to Sue (used option 2)
00DB = Black paper subplot in Black paper talking to Sue (used any option)
00DE = Black paper subplot in Black paper talked to Sue at the end of quest?
00DF = Knowing ones heart talked to Bernie?
00E0 = Seek my master. Zoke ,Donoph subplot?
00E2 = Bernie Gran Squall
00E7 = Defeated Kireek in waterfall of tears
00E8 = Black paper subplot in black paper. defeated Kireek...
00EB = Black paper subplot in from the depths. Defeated Kireek and got soul eater!
00F1 = Secret delivery. Started the Weapons subplot //is cleared if quest is left
00F3 = Weapon badge approval for claiming the snake //is cleared if quest is left
00F4 = Weapon badge approval for the lost bride //is cleared if quest is left
00F5 = Weapon badge approval for gran squall //is cleared if quest is left
00F6 = Secret delivery. Got AKIKO's FRYING PAN!
00FB = Got Orochi-agito
00FD = Unknown addicting food
0105 = Central dome fire swirl. Got Glory of the past!
0106 = Central dome fire swirl. Got Mark3.
0107 = Central dome fire swirl. got Sonic knuckles
0108 = Central dome fire swirl. got mail from BOGARDE
0109 = Central dome fire swirl. got mail from ANNA
010A = Central dome fire swirl. got mail from NADJA
010B = Central dome fire swirl. got mail from Lionel
010C = Soul of the blacksmith. Got one of the 3 special weapons!
010D = Donoph Baz dies The Retired Hunter
010E = Seat of heart unknown
010F = Seat of heart unknown
0110 = Seat of heart unknown
0111 = Seat of heart unknown
0112 = Seat of heart unknown
0113 = Seat of heart unknown
0187 = Soul of steel. Got Marina's bag! //dreamcast
0188 = Soul of steel. Unknown.
0191 = Capsule Elly VR
0197 = Cleared VR Temple
01AD = Capsule elly CCA
01AE = Capsule elly CCA
01B3 = After reading a capsule from flowen
01D6 = Set after unlocking vr spaceship
01F5 * = Episode1: Cleared government 1-1
01F7 * = Episode1: Cleared government 1-2
01F9 * = Episode1: Cleared government 1-3; Caves unlocked (V4)
01FB * = Episode1: Cleared government 2-1
01FD * = Episode1: Cleared government 2-2
01FF * = Episode1: Cleared government 2-3
0201 * = Episode1: Cleared government 2-4; Mines unlocked (V4)
0203 * = Episode1: Cleared government 3-1
0205 * = Episode1: Cleared government 3-2
0207 * = Episode1: Cleared government 3-3; Ruins unlocked (V4)
0209 * = Episode1: Cleared government 4-1
020B * = Episode1: Cleared government 4-2
020D * = Episode1: Cleared government 4-3
020F * = Episode1: Cleared government 4-4
0211 * = Episode1: Cleared government 4-5
0213 * = Episode2: Cleared government 5-1
0215 * = Episode2: Cleared government 5-2
0217 * = Episode2: Cleared government 5-3
0219 * = Episode2: Cleared government 5-4
021B * = Episode2: Cleared government 5-5; VR Spaceship unlocked (V4)
021D * = Episode2: Cleared government 6-1
021F * = Episode2: Cleared government 6-2
0221 * = Episode2: Cleared government 6-3
0223 * = Episode2: Cleared government 6-4
0225 * = Episode2: Cleared government 6-5; CCA unlocked (V4)
0227 * = Episode2: Cleared government 7-1
0229 * = Episode2: Cleared government 7-2
022B * = Episode2: Cleared government 7-3
022D * = Episode2: Cleared government 7-4
022F * = Episode2: Cleared government 7-5; Seabed unlocked (V4)
0231 * = Episode2: Cleared government 8-1
0233 * = Episode2: Cleared government 8-2
0235 * = Episode2: Cleared government 8-3
0246 = Activated Jungle East big door switch
0248 = Activated Seaside big door switch
024F = Defeated Gol Dragon
0252 = Defeated Gal Gryphon
02BD * = Episode4: Cleared government 9-1; Crater West unlocked
02BE * = Episode4: Cleared government 9-2; Crater South unlocked
02BF * = Episode4: Cleared government 9-3; Crater North unlocked
02C0 * = Episode4: Cleared government 9-4; Crater Interior unlocked
02C1 * = Episode4: Cleared government 9-5; Desert unlocked
02C2 * = Episode4: Cleared government 9-6
02C3 * = Episode4: Cleared government 9-7
02C4 * = Episode4: Cleared government 9-8
0314 = Entered Forest 1
0330 = Entered Ruins 1
03FA = P2 Menu 7, G-Counter // Talked to Momoka
03FB = Nol start
03FC = Cleared Ep2 government on ultimate
03FE * = TODO (3OE1:8019BA34, 3OE1:80218F74, 59NL:0066A287)
Quest unlock conditions:
QUESTNUM NAMEIDX SDESCIDX LDESCIDX ENFL CPFL FLAGS FILE
00000001 00000000 00000001 00000002 006A 006B 00000000 quest04
00000002 00000003 00000004 00000005 0066 0067 00000000 quest02
00000003 00000006 00000007 00000008 0064 0065 00000000 quest01
00000004 00000009 0000000A 0000000B 0068 0069 00000000 quest03
00000005 0000000C 0000000D 0000000E 006C 006D 00000000 quest05
00000006 0000000F 00000010 00000011 006E 006F 00000000 quest06
00000007 00000012 00000013 00000014 0070 0071 00000000 quest07
00000008 00000015 00000016 00000017 0072 0073 00000000 quest08
00000009 00000018 00000019 0000001A 0074 0075 00000000 quest09
0000000A 0000001B 0000001C 0000001D 0076 0077 00000000 quest10
0000000B 0000001E 0000001F 00000020 0078 0079 00000000 quest11
0000000C 00000021 00000022 00000023 007A 007B 00000000 quest12
0000000D 00000024 00000025 00000026 007C 007D 00000000 quest13
0000000E 00000027 00000028 00000029 007E 007F 00000000 quest14
0000000F 0000002A 0000002B 0000002C 0080 0081 00000000 quest15
00000010 0000002D 0000002E 0000002F 0082 0083 00000000 quest16
00000011 00000030 00000031 00000032 0084 0085 00000000 quest17
00000012 00000033 00000034 00000035 0086 0087 00000000 quest18
00000013 00000036 00000037 00000038 0088 0089 00000000 quest19
00000014 00000039 0000003A 0000003B 008A 008B 00000000 quest20
00000015 0000003C 0000003D 0000003E 008C 008D 00000000 quest21
00000016 0000003F 00000040 00000041 008E 008F 00000000 quest22
00000017 00000042 00000043 00000044 0090 0091 00000000 quest23
00000018 00000045 00000046 00000047 0092 0093 00000000 quest24
00000019 00000048 00000049 0000004A 0094 0095 00000000 quest25
0000001A 0000004B 0000004C 0000004D 009F 00A0 00000000 quest26 (plus only; CDFS)
0000001B 0000004E 0000004F 00000050 FFFF 00A1 00000000 quest27 (plus only; ep2; Seat of the Heart)
0000001C 00000051 00000052 00000053 00A2 00A3 00000000 quest28 (plus only; ep2; The East Tower)
0000001D 00000054 00000055 00000056 00A4 00A5 00000000 quest29 (plus only; ep2; THe West Tower)
// Pre-BB
/* quest01 */ F_0064 = true;
/* quest02 */ F_0066 = true;
/* quest03 */ F_0068 = F_0065 && F_0067 && F_006B && F_0018;
/* quest04 */ F_006A = true;
/* quest05 */ F_006C = F_0065 && F_0067 && F_006B;
/* quest06 */ F_006E = F_0065 && F_0067 && F_006B;
/* quest07 */ F_0070 = F_0065 && F_0067 && F_006B;
/* quest08 */ F_0072 = F_0071;
/* quest09 */ F_0074 = F_0065 && F_0067 && F_006B;
/* quest10 */ F_0076 = F_0065 && F_0067 && F_006B && F_0018;
/* quest11 */ F_0078 = F_0065 && F_0067 && F_006B && F_0018;
/* quest12 */ F_007A = F_0077 && F_0079 && F_007F && F_0085;
/* quest13 */ F_007C = F_007B;
/* quest14 */ F_007E = F_0065 && F_0067 && F_006B && F_0018;
/* quest15 */ F_0080 = F_0077 && F_0079 && F_007F && F_0085;
/* quest16 */ F_0082 = F_0065 && F_0067 && F_006B && F_0021;
/* quest17 */ F_0084 = F_0065 && F_0067 && F_006B && F_0018;
/* quest18 */ F_0086 = F_0065 && F_0067 && F_006B && F_0021;
/* quest19 */ F_0088 = F_0065 && F_0067 && F_006B && F_0030;
/* quest20 */ F_008A = F_0065 && F_0067 && F_006B && F_0021;
/* quest21 */ F_008C = F_008B && F_007F && F_0021;
/* quest22 */ F_008E = F_008D && F_0030 && F_0091;
/* quest23 */ F_0090 = F_007F && F_0030;
/* quest24 */ F_0092 = F_0065 && F_0067 && F_006B && F_0030;
/* quest25 */ F_0094 = F_008D && F_0030 && F_0091;
/* quest26 (Plus only) */ F_009F = F_0095;
/* quest28 (Plus only) */ F_00A2 = F_00A1;
/* quest29 (Plus only) */ F_00A4 = F_00A3;
// BB
/* quest01 */ F_0064 = true;
/* quest02 */ F_0066 = true;
/* quest03 */ F_0068 = F_0065 && F_0067 && F_006B && F_01F9;
/* quest04 */ F_006A = true;
/* quest05 */ F_006C = F_0065 && F_0067 && F_006B;
/* quest06 */ F_006E = F_0065 && F_0067 && F_006B;
/* quest07 */ F_0070 = F_0065 && F_0067 && F_006B;
/* quest08 */ F_0072 = F_0071;
/* quest09 */ F_0074 = F_0065 && F_0067 && F_006B;
/* quest10 */ F_0076 = F_0065 && F_0067 && F_006B && F_01F9;
/* quest11 */ F_0078 = F_0065 && F_0067 && F_006B && F_01F9;
/* quest12 */ F_007A = F_0077 && F_0079 && F_007F && F_0085;
/* quest13 */ F_007C = F_007B;
/* quest14 */ F_007E = F_0065 && F_0067 && F_006B && F_01F9;
/* quest15 */ F_0080 = F_0077 && F_0079 && F_007F && F_0085;
/* quest16 */ F_0082 = F_0065 && F_0067 && F_006B && F_0201;
/* quest17 */ F_0084 = F_0065 && F_0067 && F_006B && F_01F9;
/* quest18 */ F_0086 = F_0065 && F_0067 && F_006B && F_0201;
/* quest19 */ F_0088 = F_0065 && F_0067 && F_006B && F_0207;
/* quest20 */ F_008A = F_0065 && F_0067 && F_006B && F_0201;
/* quest21 */ F_008C = F_008B && F_007F && F_0201;
/* quest22 */ F_008E = F_008D && F_0091 && F_0207;
/* quest23 */ F_0090 = F_007F && F_0207;
/* quest24 */ F_0092 = F_0065 && F_0067 && F_006B && F_0207;
/* quest25 */ F_0094 = F_008D && F_0091 && F_0207;
### T FLAG NAME REQUIREMENTS AVAILABLE_IF ENABLED_IF
001 1 0065 Magnitude of Metal !F_0065 || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
002 1 0067 Claiming A Stake !F_0067 || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
003 3 0069 The Value of Money T1, Caves F_0065 && F_0067 && F_006B && F_01F9 !F_0069 || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
004 1 006B Battle Training !F_006B || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
005 2 006D Journalistic Pursuit T1 F_0065 && F_0067 && F_006B !F_006D || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
006 2 006F The Fake in yellow T1 F_0065 && F_0067 && F_006B !F_006F || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
007 2 0071 Native Research T1 F_0065 && F_0067 && F_006B !F_0071 || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
008 2 0073 Forest of Sorrow 007 F_0071 !F_0073 || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
009 2 0075 Gran Squall T1 F_0065 && F_0067 && F_006B !F_0075 || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
010 3 0077 Addicting Food T1 F_0065 && F_0067 && F_006B && F_01F9 !F_0077 || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
011 3 0079 The Lost Bride T1 F_0065 && F_0067 && F_006B && F_01F9 !F_0079 || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
012 3 007B Waterfall tears 010, 011, 014, 017 F_0077 && F_0079 && F_007F && F_0085 !F_007B || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
013 3 007D Black Paper 012 F_007B !F_007D || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
014 3 007F Secret Delivery T1 F_0065 && F_0067 && F_006B && F_01F9 !F_007F || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
015 3 0081 Soul of a Blacksmith 010, 011, 014, 017 F_0077 && F_0079 && F_007F && F_0085 !F_0081 || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
016 3 0083 Letter from Lionel T1, Mines F_0065 && F_0067 && F_006B && F_0201 !F_0083 || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
017 3 0085 The Grave's Butler T1 F_0065 && F_0067 && F_006B && F_01F9 !F_0085 || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
018 4 0087 Knowing One's Heart T1, Mines F_0065 && F_0067 && F_006B && F_0201 !F_0087 || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
019 5 0089 Retired Hunter T1, Ruins F_0065 && F_0067 && F_006B && F_0207 !F_0089 || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
020 4 008B Dr. Osto's Research T1, Mines F_0065 && F_0067 && F_006B && F_0201 !F_008B || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
021 4 008D The Unsealed Door 020, 014 F_008B && F_007F && F_0201 !F_008D || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
022 5 008F Soul of Steel 023 F_008D && F_0091 && F_0207 !F_008F || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
023 5 0091 Doc's Secret Plan 014, Ruins F_007F && F_0207 !F_0091 || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
024 5 0093 Seek My Master T1, Ruins F_0065 && F_0067 && F_006B && F_0207 !F_0093 || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
025 5 0095 From the Depths 023 F_008D && F_0091 && F_0207 !F_0095 || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
026 2 009B Central Dome Fire Swirl 008 F_0073
-27
View File
@@ -1,27 +0,0 @@
### T FLAG NAME REQUIREMENTS AVAILABLE_IF ENABLED_IF
001 1 0065 Magnitude of Metal !F_0065 || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
002 1 0067 Claiming A Stake !F_0067 || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
003 3 0069 The Value of Money T1, Caves F_0065 && F_0067 && F_006B && F_01F9 !F_0069 || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
004 1 006B Battle Training !F_006B || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
005 2 006D Journalistic Pursuit T1 F_0065 && F_0067 && F_006B !F_006D || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
006 2 006F The Fake in yellow T1 F_0065 && F_0067 && F_006B !F_006F || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
007 2 0071 Native Research T1 F_0065 && F_0067 && F_006B !F_0071 || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
008 2 0073 Forest of Sorrow 007 F_0071 !F_0073 || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
009 2 0075 Gran Squall T1 F_0065 && F_0067 && F_006B !F_0075 || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
010 3 0077 Addicting Food T1 F_0065 && F_0067 && F_006B !F_0077 || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
011 3 0079 The Lost Bride T1 F_0065 && F_0067 && F_006B !F_0079 || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
012 3 007B Waterfall tears 010, 011, 014, 017 F_0077 && F_0079 && F_007F && F_0085 !F_007B || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
013 3 007D Black Paper 012 F_007B !F_007D || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
014 3 007F Secret Delivery T1 F_0065 && F_0067 && F_006B !F_007F || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
015 3 0081 Soul of a Blacksmith 010, 011, 014, 017 F_0077 && F_0079 && F_007F && F_0085 !F_0081 || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
016 3 0083 Letter from Lionel T1, Mines F_0065 && F_0067 && F_006B && F_0201 !F_0083 || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
017 3 0085 The Grave's Butler T1 F_0065 && F_0067 && F_006B !F_0085 || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
018 4 0087 Knowing One's Heart T1, Mines F_0065 && F_0067 && F_006B && F_0201 !F_0087 || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
019 5 0089 Retired Hunter T1, Ruins F_0065 && F_0067 && F_006B && F_0207 !F_0089 || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
020 4 008B Dr. Osto's Research T1, Mines F_0065 && F_0067 && F_006B && F_0201 !F_008B || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
021 4 008D The Unsealed Door 020, 014 F_008B && F_007F !F_008D || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
022 5 008F Soul of Steel 023 F_0091 !F_008F || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
023 5 0091 Doc's Secret Plan 014, Ruins F_007F && F_0207 !F_0091 || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
024 5 0093 Seek My Master T1, Ruins F_0065 && F_0067 && F_006B && F_0207 !F_0093 || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
025 5 0095 From the Depths 023 F_0091 !F_0095 || (F_0065 && F_0067 && F_0069 && F_006B && F_006D && F_006F && F_0071 && F_0073 && F_0075 && F_0077 && F_0079 && F_007B && F_007D && F_007F && F_0081 && F_0083 && F_0085 && F_0087 && F_0089 && F_008B && F_008D && F_008F && F_0091 && F_0093 && F_0095)
026 2 009B Central Dome Fire Swirl 008 F_0073
+10 -13
View File
@@ -9,10 +9,7 @@
#include "Text.hh"
using namespace std;
AFSArchive::AFSArchive(shared_ptr<const string> data)
: data(data) {
AFSArchive::AFSArchive(std::shared_ptr<const std::string> data) : data(data) {
struct FileHeader {
be_uint32_t magic;
le_uint32_t num_files;
@@ -26,7 +23,7 @@ AFSArchive::AFSArchive(shared_ptr<const string> data)
phosg::StringReader r(*this->data);
const auto& header = r.get<FileHeader>();
if (header.magic != 0x41465300) { // 'AFS\0'
throw runtime_error("file is not an AFS archive");
throw std::runtime_error("file is not an AFS archive");
}
while (this->entries.size() < header.num_files) {
@@ -35,21 +32,21 @@ AFSArchive::AFSArchive(shared_ptr<const string> data)
}
}
pair<const void*, size_t> AFSArchive::get(size_t index) const {
std::pair<const void*, size_t> AFSArchive::get(size_t index) const {
const auto& entry = this->entries.at(index);
if (entry.offset > this->data->size()) {
throw out_of_range("entry begins beyond end of archive");
throw std::out_of_range("entry begins beyond end of archive");
}
if (entry.offset + entry.size > this->data->size()) {
throw out_of_range("entry extends beyond end of archive");
throw std::out_of_range("entry extends beyond end of archive");
}
return make_pair(this->data->data() + entry.offset, entry.size);
return std::make_pair(this->data->data() + entry.offset, entry.size);
}
string AFSArchive::get_copy(size_t index) const {
std::string AFSArchive::get_copy(size_t index) const {
auto ret = this->get(index);
return string(reinterpret_cast<const char*>(ret.first), ret.second);
return std::string(reinterpret_cast<const char*>(ret.first), ret.second);
}
phosg::StringReader AFSArchive::get_reader(size_t index) const {
@@ -57,12 +54,12 @@ phosg::StringReader AFSArchive::get_reader(size_t index) const {
return phosg::StringReader(ret.first, ret.second);
}
string AFSArchive::generate(const vector<string>& files, bool big_endian) {
std::string AFSArchive::generate(const std::vector<std::string>& files, bool big_endian) {
return big_endian ? AFSArchive::generate_t<true>(files) : AFSArchive::generate_t<false>(files);
}
template <bool BE>
string AFSArchive::generate_t(const vector<string>& files) {
std::string AFSArchive::generate_t(const std::vector<std::string>& files) {
phosg::StringWriter w;
w.put_u32b(0x41465300); // 'AFS\0'
w.put<U32T<BE>>(files.size());
+192 -190
View File
@@ -9,24 +9,23 @@
#include <phosg/Time.hh>
#include "Account.hh"
#include "AccountSync.hh"
using namespace std;
shared_ptr<DCNTELicense> DCNTELicense::from_json(const phosg::JSON& json) {
auto ret = make_shared<DCNTELicense>();
std::shared_ptr<DCNTELicense> DCNTELicense::from_json(const phosg::JSON& json) {
auto ret = std::make_shared<DCNTELicense>();
ret->serial_number = json.get_string("SerialNumber");
ret->access_key = json.get_string("AccessKey");
if (ret->serial_number.size() > 16) {
throw runtime_error("serial number is too long");
throw std::runtime_error("serial number is too long");
}
if (ret->serial_number.empty()) {
throw runtime_error("serial number is too short");
throw std::runtime_error("serial number is too short");
}
if (ret->access_key.size() > 16) {
throw runtime_error("access key is too long");
throw std::runtime_error("access key is too long");
}
if (ret->access_key.empty()) {
throw runtime_error("access key is too short");
throw std::runtime_error("access key is too short");
}
return ret;
}
@@ -35,15 +34,15 @@ phosg::JSON DCNTELicense::json() const {
return phosg::JSON::dict({{"SerialNumber", this->serial_number}, {"AccessKey", this->access_key}});
}
shared_ptr<V1V2License> V1V2License::from_json(const phosg::JSON& json) {
auto ret = make_shared<V1V2License>();
std::shared_ptr<V1V2License> V1V2License::from_json(const phosg::JSON& json) {
auto ret = std::make_shared<V1V2License>();
ret->serial_number = json.get_int("SerialNumber");
ret->access_key = json.get_string("AccessKey");
if (ret->serial_number == 0) {
throw runtime_error("serial number is zero");
throw std::runtime_error("serial number is zero");
}
if (ret->access_key.size() != 8) {
throw runtime_error("access key length is incorrect");
throw std::runtime_error("access key length is incorrect");
}
return ret;
}
@@ -52,19 +51,19 @@ phosg::JSON V1V2License::json() const {
return phosg::JSON::dict({{"SerialNumber", this->serial_number}, {"AccessKey", this->access_key}});
}
shared_ptr<GCLicense> GCLicense::from_json(const phosg::JSON& json) {
auto ret = make_shared<GCLicense>();
std::shared_ptr<GCLicense> GCLicense::from_json(const phosg::JSON& json) {
auto ret = std::make_shared<GCLicense>();
ret->serial_number = json.get_int("SerialNumber");
ret->access_key = json.get_string("AccessKey");
ret->password = json.get_string("Password");
if (ret->serial_number == 0) {
throw runtime_error("serial number is zero");
throw std::runtime_error("serial number is zero");
}
if (ret->access_key.size() != 12) {
throw runtime_error("access key length is incorrect");
throw std::runtime_error("access key length is incorrect");
}
if (ret->password.empty()) {
throw runtime_error("password is too short");
throw std::runtime_error("password is too short");
}
return ret;
}
@@ -77,19 +76,19 @@ phosg::JSON GCLicense::json() const {
});
}
shared_ptr<XBLicense> XBLicense::from_json(const phosg::JSON& json) {
auto ret = make_shared<XBLicense>();
std::shared_ptr<XBLicense> XBLicense::from_json(const phosg::JSON& json) {
auto ret = std::make_shared<XBLicense>();
ret->gamertag = json.get_string("GamerTag");
ret->user_id = json.get_int("UserID");
ret->account_id = json.get_int("AccountID");
if (ret->gamertag.empty()) {
throw runtime_error("gamertag is too short");
throw std::runtime_error("gamertag is too short");
}
if (ret->user_id == 0) {
throw runtime_error("user ID is zero");
throw std::runtime_error("user ID is zero");
}
if (ret->account_id == 0) {
throw runtime_error("account ID is zero");
throw std::runtime_error("account ID is zero");
}
return ret;
}
@@ -98,21 +97,21 @@ phosg::JSON XBLicense::json() const {
return phosg::JSON::dict({{"GamerTag", this->gamertag}, {"UserID", this->user_id}, {"AccountID", this->account_id}});
}
shared_ptr<BBLicense> BBLicense::from_json(const phosg::JSON& json) {
auto ret = make_shared<BBLicense>();
std::shared_ptr<BBLicense> BBLicense::from_json(const phosg::JSON& json) {
auto ret = std::make_shared<BBLicense>();
ret->username = json.get_string("UserName");
ret->password = json.get_string("Password");
if (ret->username.size() > 16) {
throw runtime_error("username is too long");
throw std::runtime_error("username is too long");
}
if (ret->username.empty()) {
throw runtime_error("username is too short");
throw std::runtime_error("username is too short");
}
if (ret->password.size() > 16) {
throw runtime_error("password is too long");
throw std::runtime_error("password is too long");
}
if (ret->password.empty()) {
throw runtime_error("password is too short");
throw std::runtime_error("password is too short");
}
return ret;
}
@@ -132,51 +131,51 @@ Account::Account(const phosg::JSON& json)
uint64_t format_version = 0;
try {
format_version = json.get_int("FormatVersion");
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
}
if (format_version == 0) {
// Original format - no account ID
this->account_id = json.get_int("SerialNumber");
string access_key = json.get_string("AccessKey", "");
string dc_nte_serial_number = json.get_string("DCNTESerialNumber", "");
string dc_nte_access_key = json.get_string("DCNTEAccessKey", "");
string gc_password = json.get_string("GCPassword", "");
string xb_gamertag = json.get_string("XBGamerTag", "");
std::string access_key = json.get_string("AccessKey", "");
std::string dc_nte_serial_number = json.get_string("DCNTESerialNumber", "");
std::string dc_nte_access_key = json.get_string("DCNTEAccessKey", "");
std::string gc_password = json.get_string("GCPassword", "");
std::string xb_gamertag = json.get_string("XBGamerTag", "");
uint64_t xb_user_id = json.get_int("XBUserID", 0);
uint64_t xb_account_id = json.get_int("XBAccountID", 0);
string bb_username = json.get_string("BBUsername", "");
string bb_password = json.get_string("BBPassword", "");
std::string bb_username = json.get_string("BBUsername", "");
std::string bb_password = json.get_string("BBPassword", "");
if (access_key.size() == 12) {
if (!gc_password.empty()) {
auto lic = make_shared<GCLicense>();
auto lic = std::make_shared<GCLicense>();
lic->serial_number = this->account_id;
lic->access_key = access_key;
lic->password = gc_password;
this->gc_licenses.emplace(lic->serial_number, lic);
}
} else if (access_key.size() >= 8) {
auto lic = make_shared<V1V2License>();
auto lic = std::make_shared<V1V2License>();
lic->serial_number = this->account_id;
lic->access_key = access_key.substr(0, 8);
this->dc_licenses.emplace(lic->serial_number, lic);
this->pc_licenses.emplace(lic->serial_number, lic);
}
if (!dc_nte_serial_number.empty() && !dc_nte_access_key.empty()) {
auto lic = make_shared<DCNTELicense>();
auto lic = std::make_shared<DCNTELicense>();
lic->serial_number = dc_nte_serial_number;
lic->access_key = dc_nte_access_key;
this->dc_nte_licenses.emplace(lic->serial_number, lic);
}
if (!xb_gamertag.empty() && xb_user_id && xb_account_id) {
auto lic = make_shared<XBLicense>();
auto lic = std::make_shared<XBLicense>();
lic->gamertag = xb_gamertag;
lic->user_id = xb_user_id;
lic->account_id = xb_account_id;
this->xb_licenses.emplace(lic->user_id, lic);
}
if (!bb_username.empty() && !bb_password.empty()) {
auto lic = make_shared<BBLicense>();
auto lic = std::make_shared<BBLicense>();
lic->username = bb_username;
lic->password = bb_password;
this->bb_licenses.emplace(lic->username, lic);
@@ -223,7 +222,7 @@ Account::Account(const phosg::JSON& json)
for (const auto& it : json.get_list("AutoPatchesEnabled")) {
this->auto_patches_enabled.emplace(it->as_string());
}
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
}
}
@@ -279,11 +278,11 @@ phosg::JSON Account::json() const {
});
}
string Account::str() const {
std::string Account::str() const {
std::string ret = std::format("Account: {:010}/{:08X}\n", this->account_id, this->account_id);
if (this->flags) {
string flags_str = "";
std::string flags_str = "";
if (this->flags == static_cast<uint32_t>(Flag::ROOT)) {
flags_str = "ROOT";
} else if (this->flags == static_cast<uint32_t>(Flag::ADMINISTRATOR)) {
@@ -334,7 +333,7 @@ string Account::str() const {
}
if (this->user_flags) {
string user_flags_str = "";
std::string user_flags_str = "";
if (this->check_user_flag(UserFlag::DISABLE_DROP_NOTIFICATION_BROADCAST)) {
user_flags_str += "DISABLE_DROP_NOTIFICATION_BROADCAST,";
}
@@ -347,8 +346,7 @@ string Account::str() const {
}
if (this->ban_end_time) {
string time_str = phosg::format_time(this->ban_end_time);
ret += std::format(" Banned until: {} ({})\n", this->ban_end_time, time_str);
ret += std::format(" Banned until: {} ({})\n", this->ban_end_time, phosg::format_time(this->ban_end_time));
}
if (this->ep3_current_meseta || this->ep3_total_meseta_earned) {
ret += std::format(" Episode 3 meseta: {} (total earned: {})\n",
@@ -399,20 +397,21 @@ string Account::str() const {
void Account::save() const {
if (!this->is_temporary) {
auto json = this->json();
string json_data = json.serialize(
std::string json_data = json.serialize(
phosg::JSON::SerializeOption::FORMAT | phosg::JSON::SerializeOption::HEX_INTEGERS);
string filename = std::format("system/licenses/{:010}.json", this->account_id);
std::string filename = std::format("system/licenses/{:010}.json", this->account_id);
phosg::save_file(filename, json_data);
AccountSync::notify_account_saved(this->account_id, filename);
}
}
void Account::delete_file() const {
string filename = std::format("system/licenses/{:010}.json", this->account_id);
remove(filename.c_str());
std::filesystem::remove(std::format("system/licenses/{:010}.json", this->account_id));
}
string Login::str() const {
string ret = std::format("Account:{:08X}", this->account->account_id);
std::string Login::str() const {
std::string ret = std::format("Account:{:08X}", this->account->account_id);
if (this->account_was_created) {
ret += " (new)";
}
@@ -435,21 +434,21 @@ string Login::str() const {
}
size_t AccountIndex::count() const {
shared_lock g(this->lock);
std::shared_lock g(this->lock);
return this->by_account_id.size();
}
shared_ptr<Account> AccountIndex::from_account_id(uint32_t account_id) const {
std::shared_ptr<Account> AccountIndex::from_account_id(uint32_t account_id) const {
try {
shared_lock g(this->lock);
std::shared_lock g(this->lock);
return this->by_account_id.at(account_id);
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
throw missing_account();
}
}
shared_ptr<Login> AccountIndex::from_dc_nte_credentials_locked(const string& serial_number, const string& access_key) {
auto login = make_shared<Login>();
std::shared_ptr<Login> AccountIndex::from_dc_nte_credentials_locked(const std::string& serial_number, const std::string& access_key) {
auto login = std::make_shared<Login>();
login->account = this->by_dc_nte_serial_number.at(serial_number);
login->dc_nte_license = login->account->dc_nte_licenses.at(serial_number);
if (login->dc_nte_license->access_key != access_key) {
@@ -461,30 +460,30 @@ shared_ptr<Login> AccountIndex::from_dc_nte_credentials_locked(const string& ser
return login;
}
shared_ptr<Login> AccountIndex::from_dc_nte_credentials(
const string& serial_number, const string& access_key, bool allow_create) {
std::shared_ptr<Login> AccountIndex::from_dc_nte_credentials(
const std::string& serial_number, const std::string& access_key, bool allow_create) {
if (serial_number.empty()) {
throw no_username();
}
try {
shared_lock g(this->lock);
std::shared_lock g(this->lock);
return this->from_dc_nte_credentials_locked(serial_number, access_key);
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
}
unique_lock g(this->lock);
std::unique_lock g(this->lock);
try {
return this->from_dc_nte_credentials_locked(serial_number, access_key);
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
}
if (allow_create) {
auto login = make_shared<Login>();
auto login = std::make_shared<Login>();
login->account_was_created = true;
login->account = make_shared<Account>();
login->account = std::make_shared<Account>();
login->account->account_id = phosg::fnv1a32(serial_number) & 0x7FFFFFFF;
auto lic = make_shared<DCNTELicense>();
auto lic = std::make_shared<DCNTELicense>();
lic->serial_number = serial_number;
lic->access_key = access_key;
login->account->dc_nte_licenses.emplace(lic->serial_number, lic);
@@ -496,9 +495,9 @@ shared_ptr<Login> AccountIndex::from_dc_nte_credentials(
}
}
shared_ptr<Login> AccountIndex::from_dc_credentials_locked(
uint32_t serial_number, const string& access_key, const string& character_name) {
auto login = make_shared<Login>();
std::shared_ptr<Login> AccountIndex::from_dc_credentials_locked(
uint32_t serial_number, const std::string& access_key, const std::string& character_name) {
auto login = std::make_shared<Login>();
login->account = this->by_dc_serial_number.at(serial_number);
login->dc_license = login->account->dc_licenses.at(serial_number);
bool is_shared = login->account->check_flag(Account::Flag::IS_SHARED_ACCOUNT);
@@ -514,30 +513,30 @@ shared_ptr<Login> AccountIndex::from_dc_credentials_locked(
return login;
}
shared_ptr<Login> AccountIndex::from_dc_credentials(
uint32_t serial_number, const string& access_key, const string& character_name, bool allow_create) {
std::shared_ptr<Login> AccountIndex::from_dc_credentials(
uint32_t serial_number, const std::string& access_key, const std::string& character_name, bool allow_create) {
if (serial_number == 0) {
throw no_username();
}
try {
shared_lock g(this->lock);
std::shared_lock g(this->lock);
return this->from_dc_credentials_locked(serial_number, access_key, character_name);
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
}
unique_lock g(this->lock);
std::unique_lock g(this->lock);
try {
return this->from_dc_credentials_locked(serial_number, access_key, character_name);
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
}
if (allow_create) {
auto login = make_shared<Login>();
auto login = std::make_shared<Login>();
login->account_was_created = true;
login->account = make_shared<Account>();
login->account = std::make_shared<Account>();
login->account->account_id = serial_number;
auto lic = make_shared<V1V2License>();
auto lic = std::make_shared<V1V2License>();
lic->serial_number = serial_number;
lic->access_key = access_key;
login->account->dc_licenses.emplace(lic->serial_number, lic);
@@ -549,19 +548,19 @@ shared_ptr<Login> AccountIndex::from_dc_credentials(
}
}
shared_ptr<Login> AccountIndex::from_pc_nte_credentials(uint32_t guild_card_number, bool allow_create) {
std::shared_ptr<Login> AccountIndex::from_pc_nte_credentials(uint32_t guild_card_number, bool allow_create) {
if (!allow_create) {
throw missing_account();
}
if (guild_card_number == 0xFFFFFFFF) {
guild_card_number = phosg::random_object<uint32_t>() & 0x7FFFFFFF;
}
auto login = make_shared<Login>();
auto login = std::make_shared<Login>();
login->account_was_created = true;
login->account = make_shared<Account>();
login->account = std::make_shared<Account>();
login->account->account_id = guild_card_number;
login->account->is_temporary = true;
auto lic = make_shared<V1V2License>();
auto lic = std::make_shared<V1V2License>();
lic->serial_number = guild_card_number;
login->account->pc_licenses.emplace(lic->serial_number, lic);
login->pc_license = lic;
@@ -569,9 +568,9 @@ shared_ptr<Login> AccountIndex::from_pc_nte_credentials(uint32_t guild_card_numb
return login;
}
shared_ptr<Login> AccountIndex::from_pc_credentials_locked(
uint32_t serial_number, const string& access_key, const string& character_name) {
auto login = make_shared<Login>();
std::shared_ptr<Login> AccountIndex::from_pc_credentials_locked(
uint32_t serial_number, const std::string& access_key, const std::string& character_name) {
auto login = std::make_shared<Login>();
login->account = this->by_pc_serial_number.at(serial_number);
login->pc_license = login->account->pc_licenses.at(serial_number);
bool is_shared = login->account->check_flag(Account::Flag::IS_SHARED_ACCOUNT);
@@ -587,30 +586,30 @@ shared_ptr<Login> AccountIndex::from_pc_credentials_locked(
return login;
}
shared_ptr<Login> AccountIndex::from_pc_credentials(
uint32_t serial_number, const string& access_key, const string& character_name, bool allow_create) {
std::shared_ptr<Login> AccountIndex::from_pc_credentials(
uint32_t serial_number, const std::string& access_key, const std::string& character_name, bool allow_create) {
if (serial_number == 0) {
throw no_username();
}
try {
shared_lock g(this->lock);
std::shared_lock g(this->lock);
return this->from_pc_credentials_locked(serial_number, access_key, character_name);
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
}
unique_lock g(this->lock);
std::unique_lock g(this->lock);
try {
return this->from_pc_credentials_locked(serial_number, access_key, character_name);
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
}
if (allow_create) {
auto login = make_shared<Login>();
auto login = std::make_shared<Login>();
login->account_was_created = true;
login->account = make_shared<Account>();
login->account = std::make_shared<Account>();
login->account->account_id = serial_number;
auto lic = make_shared<V1V2License>();
auto lic = std::make_shared<V1V2License>();
lic->serial_number = serial_number;
lic->access_key = access_key;
login->account->pc_licenses.emplace(lic->serial_number, lic);
@@ -622,9 +621,12 @@ shared_ptr<Login> AccountIndex::from_pc_credentials(
}
}
shared_ptr<Login> AccountIndex::from_gc_credentials_locked(
uint32_t serial_number, const string& access_key, const string* password, const string& character_name) {
auto login = make_shared<Login>();
std::shared_ptr<Login> AccountIndex::from_gc_credentials_locked(
uint32_t serial_number,
const std::string& access_key,
const std::string* password,
const std::string& character_name) {
auto login = std::make_shared<Login>();
login->account = this->by_gc_serial_number.at(serial_number);
login->gc_license = login->account->gc_licenses.at(serial_number);
bool is_shared = login->account->check_flag(Account::Flag::IS_SHARED_ACCOUNT);
@@ -643,34 +645,34 @@ shared_ptr<Login> AccountIndex::from_gc_credentials_locked(
return login;
}
shared_ptr<Login> AccountIndex::from_gc_credentials(
std::shared_ptr<Login> AccountIndex::from_gc_credentials(
uint32_t serial_number,
const string& access_key,
const string* password,
const string& character_name,
const std::string& access_key,
const std::string* password,
const std::string& character_name,
bool allow_create) {
if (serial_number == 0) {
throw no_username();
}
try {
shared_lock g(this->lock);
std::shared_lock g(this->lock);
return this->from_gc_credentials_locked(serial_number, access_key, password, character_name);
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
}
unique_lock g(this->lock);
std::unique_lock g(this->lock);
try {
return this->from_gc_credentials_locked(serial_number, access_key, password, character_name);
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
}
if (allow_create && password) {
auto login = make_shared<Login>();
auto login = std::make_shared<Login>();
login->account_was_created = true;
login->account = make_shared<Account>();
login->account = std::make_shared<Account>();
login->account->account_id = serial_number;
auto lic = make_shared<GCLicense>();
auto lic = std::make_shared<GCLicense>();
lic->serial_number = serial_number;
lic->access_key = access_key;
lic->password = *password;
@@ -683,8 +685,8 @@ shared_ptr<Login> AccountIndex::from_gc_credentials(
}
}
shared_ptr<Login> AccountIndex::from_xb_credentials_locked(uint64_t user_id) {
auto login = make_shared<Login>();
std::shared_ptr<Login> AccountIndex::from_xb_credentials_locked(uint64_t user_id) {
auto login = std::make_shared<Login>();
login->account = this->by_xb_user_id.at(user_id);
login->xb_license = login->account->xb_licenses.at(user_id);
if (login->account->ban_end_time && (login->account->ban_end_time >= phosg::now())) {
@@ -693,30 +695,30 @@ shared_ptr<Login> AccountIndex::from_xb_credentials_locked(uint64_t user_id) {
return login;
}
shared_ptr<Login> AccountIndex::from_xb_credentials(
const string& gamertag, uint64_t user_id, uint64_t account_id, bool allow_create) {
std::shared_ptr<Login> AccountIndex::from_xb_credentials(
const std::string& gamertag, uint64_t user_id, uint64_t account_id, bool allow_create) {
if (user_id == 0 || account_id == 0) {
throw incorrect_access_key();
}
try {
shared_lock g(this->lock);
std::shared_lock g(this->lock);
return this->from_xb_credentials_locked(user_id);
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
}
unique_lock g(this->lock);
std::unique_lock g(this->lock);
try {
return this->from_xb_credentials_locked(user_id);
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
}
if (allow_create) {
auto login = make_shared<Login>();
auto login = std::make_shared<Login>();
login->account_was_created = true;
login->account = make_shared<Account>();
login->account = std::make_shared<Account>();
login->account->account_id = phosg::fnv1a32(gamertag) & 0x7FFFFFFF;
auto lic = make_shared<XBLicense>();
auto lic = std::make_shared<XBLicense>();
lic->gamertag = gamertag;
lic->user_id = user_id;
lic->account_id = account_id;
@@ -729,8 +731,9 @@ shared_ptr<Login> AccountIndex::from_xb_credentials(
}
}
shared_ptr<Login> AccountIndex::from_bb_credentials_locked(const string& username, const string* password) {
auto login = make_shared<Login>();
std::shared_ptr<Login> AccountIndex::from_bb_credentials_locked(
const std::string& username, const std::string* password) {
auto login = std::make_shared<Login>();
login->account = this->by_bb_username.at(username);
login->bb_license = login->account->bb_licenses.at(username);
if (password && (login->bb_license->password != *password)) {
@@ -742,30 +745,30 @@ shared_ptr<Login> AccountIndex::from_bb_credentials_locked(const string& usernam
return login;
}
shared_ptr<Login> AccountIndex::from_bb_credentials(
const string& username, const string* password, bool allow_create) {
std::shared_ptr<Login> AccountIndex::from_bb_credentials(
const std::string& username, const std::string* password, bool allow_create) {
if (username.empty() || (password && password->empty())) {
throw no_username();
}
try {
shared_lock g(this->lock);
std::shared_lock g(this->lock);
return this->from_bb_credentials_locked(username, password);
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
}
unique_lock g(this->lock);
std::unique_lock g(this->lock);
try {
return this->from_bb_credentials_locked(username, password);
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
}
if (allow_create && password) {
auto login = make_shared<Login>();
auto login = std::make_shared<Login>();
login->account_was_created = true;
login->account = make_shared<Account>();
login->account = std::make_shared<Account>();
login->account->account_id = phosg::fnv1a32(username) & 0x7FFFFFFF;
auto lic = make_shared<BBLicense>();
auto lic = std::make_shared<BBLicense>();
lic->username = username;
lic->password = *password;
login->account->bb_licenses.emplace(lic->username, lic);
@@ -777,9 +780,9 @@ shared_ptr<Login> AccountIndex::from_bb_credentials(
}
}
vector<shared_ptr<Account>> AccountIndex::all() const {
shared_lock g(this->lock);
vector<shared_ptr<Account>> ret;
std::vector<std::shared_ptr<Account>> AccountIndex::all() const {
std::shared_lock g(this->lock);
std::vector<std::shared_ptr<Account>> ret;
ret.reserve(this->by_account_id.size());
for (const auto& it : this->by_account_id) {
ret.emplace_back(it.second);
@@ -787,44 +790,44 @@ vector<shared_ptr<Account>> AccountIndex::all() const {
return ret;
}
void AccountIndex::add(shared_ptr<Account> a) {
unique_lock g(this->lock);
void AccountIndex::add(std::shared_ptr<Account> a) {
std::unique_lock g(this->lock);
this->add_locked(a);
}
void AccountIndex::add_locked(shared_ptr<Account> a) {
void AccountIndex::add_locked(std::shared_ptr<Account> a) {
if (this->force_all_temporary) {
a->is_temporary = true;
}
for (const auto& it : a->dc_nte_licenses) {
if (this->by_dc_nte_serial_number.count(it.second->serial_number)) {
throw runtime_error("account already exists with this DC NTE serial number");
throw std::runtime_error("account already exists with this DC NTE serial number");
}
}
for (const auto& it : a->dc_licenses) {
if (this->by_dc_serial_number.count(it.second->serial_number)) {
throw runtime_error("account already exists with this DC serial number");
throw std::runtime_error("account already exists with this DC serial number");
}
}
for (const auto& it : a->pc_licenses) {
if (this->by_pc_serial_number.count(it.second->serial_number)) {
throw runtime_error("account already exists with this PC NTE serial number");
throw std::runtime_error("account already exists with this PC NTE serial number");
}
}
for (const auto& it : a->gc_licenses) {
if (this->by_gc_serial_number.count(it.second->serial_number)) {
throw runtime_error("account already exists with this GC serial number");
throw std::runtime_error("account already exists with this GC serial number");
}
}
for (const auto& it : a->xb_licenses) {
if (this->by_xb_user_id.count(it.second->user_id)) {
throw runtime_error("account already exists with this XB user ID");
throw std::runtime_error("account already exists with this XB user ID");
}
}
for (const auto& it : a->bb_licenses) {
if (this->by_bb_username.count(it.second->username)) {
throw runtime_error("account already exists with this BB username");
throw std::runtime_error("account already exists with this BB username");
}
}
@@ -854,10 +857,10 @@ void AccountIndex::add_locked(shared_ptr<Account> a) {
}
void AccountIndex::remove(uint32_t account_id) {
unique_lock g(this->lock);
std::unique_lock g(this->lock);
auto acc_it = this->by_account_id.find(account_id);
if (acc_it == this->by_account_id.end()) {
throw out_of_range("account does not exist");
throw std::out_of_range("account does not exist");
}
auto a = std::move(acc_it->second);
this->by_account_id.erase(acc_it);
@@ -882,154 +885,153 @@ void AccountIndex::remove(uint32_t account_id) {
}
}
void AccountIndex::add_dc_nte_license(shared_ptr<Account> account, shared_ptr<DCNTELicense> license) {
void AccountIndex::add_dc_nte_license(std::shared_ptr<Account> account, std::shared_ptr<DCNTELicense> license) {
if (!this->by_dc_nte_serial_number.emplace(license->serial_number, account).second) {
throw runtime_error("serial number already registered");
throw std::runtime_error("serial number already registered");
}
if (!account->dc_nte_licenses.emplace(license->serial_number, license).second) {
this->by_dc_nte_serial_number.erase(license->serial_number);
throw logic_error("serial number registered in account but not in account index");
throw std::logic_error("serial number registered in account but not in account index");
}
}
void AccountIndex::add_dc_license(shared_ptr<Account> account, shared_ptr<V1V2License> license) {
void AccountIndex::add_dc_license(std::shared_ptr<Account> account, std::shared_ptr<V1V2License> license) {
if (!this->by_dc_serial_number.emplace(license->serial_number, account).second) {
throw runtime_error("serial number already registered");
throw std::runtime_error("serial number already registered");
}
if (!account->dc_licenses.emplace(license->serial_number, license).second) {
this->by_dc_serial_number.erase(license->serial_number);
throw logic_error("serial number registered in account but not in account index");
throw std::logic_error("serial number registered in account but not in account index");
}
}
void AccountIndex::add_pc_license(shared_ptr<Account> account, shared_ptr<V1V2License> license) {
void AccountIndex::add_pc_license(std::shared_ptr<Account> account, std::shared_ptr<V1V2License> license) {
if (!this->by_pc_serial_number.emplace(license->serial_number, account).second) {
throw runtime_error("serial number already registered");
throw std::runtime_error("serial number already registered");
}
if (!account->pc_licenses.emplace(license->serial_number, license).second) {
this->by_pc_serial_number.erase(license->serial_number);
throw logic_error("serial number registered in account but not in account index");
throw std::logic_error("serial number registered in account but not in account index");
}
}
void AccountIndex::add_gc_license(shared_ptr<Account> account, shared_ptr<GCLicense> license) {
void AccountIndex::add_gc_license(std::shared_ptr<Account> account, std::shared_ptr<GCLicense> license) {
if (!this->by_gc_serial_number.emplace(license->serial_number, account).second) {
throw runtime_error("serial number already registered");
throw std::runtime_error("serial number already registered");
}
if (!account->gc_licenses.emplace(license->serial_number, license).second) {
this->by_gc_serial_number.erase(license->serial_number);
throw logic_error("serial number registered in account but not in account index");
throw std::logic_error("serial number registered in account but not in account index");
}
}
void AccountIndex::add_xb_license(shared_ptr<Account> account, shared_ptr<XBLicense> license) {
void AccountIndex::add_xb_license(std::shared_ptr<Account> account, std::shared_ptr<XBLicense> license) {
if (!this->by_xb_user_id.emplace(license->user_id, account).second) {
throw runtime_error("user ID already registered");
throw std::runtime_error("user ID already registered");
}
if (!account->xb_licenses.emplace(license->user_id, license).second) {
this->by_xb_user_id.erase(license->user_id);
throw logic_error("user ID registered in account but not in account index");
throw std::logic_error("user ID registered in account but not in account index");
}
}
void AccountIndex::add_bb_license(shared_ptr<Account> account, shared_ptr<BBLicense> license) {
void AccountIndex::add_bb_license(std::shared_ptr<Account> account, std::shared_ptr<BBLicense> license) {
if (!this->by_bb_username.emplace(license->username, account).second) {
throw runtime_error("username already registered");
throw std::runtime_error("username already registered");
}
if (!account->bb_licenses.emplace(license->username, license).second) {
this->by_bb_username.erase(license->username);
throw logic_error("username registered in account but not in account index");
throw std::logic_error("username registered in account but not in account index");
}
}
void AccountIndex::remove_dc_nte_license(shared_ptr<Account> account, const string& serial_number) {
void AccountIndex::remove_dc_nte_license(std::shared_ptr<Account> account, const std::string& serial_number) {
auto it = account->dc_nte_licenses.find(serial_number);
if (it == account->dc_nte_licenses.end()) {
throw runtime_error("license not registered to account");
throw std::runtime_error("license not registered to account");
}
if (!this->by_dc_nte_serial_number.erase(it->second->serial_number)) {
throw runtime_error("license registered in account but not in account index");
throw std::runtime_error("license registered in account but not in account index");
}
account->dc_nte_licenses.erase(it);
}
void AccountIndex::remove_dc_license(shared_ptr<Account> account, uint32_t serial_number) {
void AccountIndex::remove_dc_license(std::shared_ptr<Account> account, uint32_t serial_number) {
auto it = account->dc_licenses.find(serial_number);
if (it == account->dc_licenses.end()) {
throw runtime_error("license not registered to account");
throw std::runtime_error("license not registered to account");
}
if (!this->by_dc_serial_number.erase(it->second->serial_number)) {
throw runtime_error("license registered in account but not in account index");
throw std::runtime_error("license registered in account but not in account index");
}
account->dc_licenses.erase(it);
}
void AccountIndex::remove_pc_license(shared_ptr<Account> account, uint32_t serial_number) {
void AccountIndex::remove_pc_license(std::shared_ptr<Account> account, uint32_t serial_number) {
auto it = account->pc_licenses.find(serial_number);
if (it == account->pc_licenses.end()) {
throw runtime_error("license not registered to account");
throw std::runtime_error("license not registered to account");
}
if (!this->by_pc_serial_number.erase(it->second->serial_number)) {
throw runtime_error("license registered in account but not in account index");
throw std::runtime_error("license registered in account but not in account index");
}
account->pc_licenses.erase(it);
}
void AccountIndex::remove_gc_license(shared_ptr<Account> account, uint32_t serial_number) {
void AccountIndex::remove_gc_license(std::shared_ptr<Account> account, uint32_t serial_number) {
auto it = account->gc_licenses.find(serial_number);
if (it == account->gc_licenses.end()) {
throw runtime_error("license not registered to account");
throw std::runtime_error("license not registered to account");
}
if (!this->by_gc_serial_number.erase(it->second->serial_number)) {
throw runtime_error("license registered in account but not in account index");
throw std::runtime_error("license registered in account but not in account index");
}
account->gc_licenses.erase(it);
}
void AccountIndex::remove_xb_license(shared_ptr<Account> account, uint64_t user_id) {
void AccountIndex::remove_xb_license(std::shared_ptr<Account> account, uint64_t user_id) {
auto it = account->xb_licenses.find(user_id);
if (it == account->xb_licenses.end()) {
throw runtime_error("license not registered to account");
throw std::runtime_error("license not registered to account");
}
if (!this->by_xb_user_id.erase(it->second->user_id)) {
throw runtime_error("license registered in account but not in account index");
throw std::runtime_error("license registered in account but not in account index");
}
account->xb_licenses.erase(it);
}
void AccountIndex::remove_bb_license(shared_ptr<Account> account, const string& username) {
void AccountIndex::remove_bb_license(std::shared_ptr<Account> account, const std::string& username) {
auto it = account->bb_licenses.find(username);
if (it == account->bb_licenses.end()) {
throw runtime_error("license not registered to account");
throw std::runtime_error("license not registered to account");
}
if (!this->by_bb_username.erase(it->second->username)) {
throw runtime_error("license registered in account but not in account index");
throw std::runtime_error("license registered in account but not in account index");
}
account->bb_licenses.erase(it);
}
shared_ptr<Account> AccountIndex::create_temporary_account_for_shared_account(
shared_ptr<const Account> src_a, const string& variation_data) const {
auto ret = make_shared<Account>(*src_a);
std::shared_ptr<Account> AccountIndex::create_temporary_account_for_shared_account(
std::shared_ptr<const Account> src_a, const std::string& variation_data) const {
auto ret = std::make_shared<Account>(*src_a);
ret->is_temporary = true;
ret->account_id = phosg::fnv1a32(&src_a->account_id, sizeof(src_a->account_id));
ret->account_id = phosg::fnv1a32(variation_data, ret->account_id);
return ret;
}
AccountIndex::AccountIndex(bool force_all_temporary)
: force_all_temporary(force_all_temporary) {
AccountIndex::AccountIndex(bool force_all_temporary) : force_all_temporary(force_all_temporary) {
if (!this->force_all_temporary) {
if (!std::filesystem::is_directory("system/licenses")) {
std::filesystem::create_directories("system/licenses");
} else {
for (const auto& item : std::filesystem::directory_iterator("system/licenses")) {
string filename = item.path().filename().string();
std::string filename = item.path().filename().string();
if (filename.ends_with(".json")) {
try {
phosg::JSON json = phosg::JSON::parse(phosg::load_file("system/licenses/" + filename));
this->add(make_shared<Account>(json));
} catch (const exception& e) {
this->add(std::make_shared<Account>(json));
} catch (const std::exception& e) {
phosg::log_error_f("Failed to index account {}", filename);
throw;
}
+834
View File
@@ -0,0 +1,834 @@
#include "AccountSync.hh"
#include "AsyncUtils.hh"
#include <algorithm>
#include <atomic>
#include <chrono>
#include <cstdio>
#include <inttypes.h>
#include <filesystem>
#include <memory>
#include <format>
#include <fstream>
#include <mutex>
#include <stdexcept>
#include <string>
namespace AccountSync {
static std::mutex config_mutex;
static std::mutex spool_mutex;
static Config current_config;
static std::atomic<bool> heartbeat_task_started(false);
static asio::io_context* login_lock_io_context = nullptr;
static uint64_t now_usecs() {
using namespace std::chrono;
return duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
}
static Config get_config() {
std::lock_guard<std::mutex> g(config_mutex);
return current_config;
}
struct ParsedHTTPURL {
std::string host;
uint16_t port = 80;
std::string path = "/";
};
static ParsedHTTPURL parse_http_url(const std::string& url) {
static const std::string prefix = "http://";
if (!url.starts_with(prefix)) {
throw std::runtime_error("only http:// coordinator URLs are supported");
}
size_t host_start = prefix.size();
size_t path_start = url.find('/', host_start);
std::string host_port = (path_start == std::string::npos)
? url.substr(host_start)
: url.substr(host_start, path_start - host_start);
ParsedHTTPURL ret;
ret.path = (path_start == std::string::npos) ? "/" : url.substr(path_start);
if (host_port.empty()) {
throw std::runtime_error("coordinator URL has empty host");
}
size_t colon_offset = host_port.rfind(':');
if (colon_offset == std::string::npos) {
ret.host = host_port;
} else {
ret.host = host_port.substr(0, colon_offset);
std::string port_s = host_port.substr(colon_offset + 1);
if (ret.host.empty() || port_s.empty()) {
throw std::runtime_error("coordinator URL has invalid host/port");
}
size_t end_offset = 0;
uint64_t port = std::stoull(port_s, &end_offset, 10);
if ((end_offset != port_s.size()) || (port == 0) || (port > 0xFFFF)) {
throw std::runtime_error("coordinator URL has invalid port");
}
ret.port = port;
}
return ret;
}
static std::string join_url_path(std::string base_path, const std::string& suffix) {
while ((base_path.size() > 1) && (base_path.back() == '/')) {
base_path.pop_back();
}
if (base_path.empty() || (base_path == "/")) {
return suffix;
}
return base_path + suffix;
}
static std::string lowercase(std::string s) {
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char ch) -> char {
return static_cast<char>(std::tolower(ch));
});
return s;
}
static void trim_ascii_inplace(std::string& s) {
while (!s.empty() && ((s.back() == ' ') || (s.back() == '\t') || (s.back() == '\r') || (s.back() == '\n'))) {
s.pop_back();
}
size_t start = 0;
while ((start < s.size()) && ((s[start] == ' ') || (s[start] == '\t') || (s[start] == '\r') || (s[start] == '\n'))) {
start++;
}
if (start) {
s = s.substr(start);
}
}
static asio::awaitable<std::string> read_http_line(
asio::ip::tcp::socket& sock,
std::string& pending_data,
size_t max_length) {
static const char* delimiter = "\r\n";
static const size_t delimiter_size = 2;
size_t delimiter_pos = pending_data.find(delimiter);
while ((delimiter_pos == std::string::npos) && (pending_data.size() < max_length)) {
size_t pre_size = pending_data.size();
pending_data.resize(std::min(max_length, pending_data.size() + 0x400));
size_t bytes_read = co_await sock.async_read_some(
asio::buffer(pending_data.data() + pre_size, pending_data.size() - pre_size),
asio::use_awaitable);
pending_data.resize(pre_size + bytes_read);
delimiter_pos = pending_data.find(delimiter, (pre_size >= 1) ? (pre_size - 1) : 0);
}
if (delimiter_pos == std::string::npos) {
throw std::runtime_error("HTTP response line exceeds maximum length");
}
std::string ret = pending_data.substr(0, delimiter_pos);
pending_data = pending_data.substr(delimiter_pos + delimiter_size);
co_return ret;
}
static asio::awaitable<std::string> read_http_data(
asio::ip::tcp::socket& sock,
std::string& pending_data,
size_t size) {
std::string ret;
if (pending_data.size() == size) {
pending_data.swap(ret);
} else if (pending_data.size() > size) {
ret = pending_data.substr(0, size);
pending_data = pending_data.substr(size);
} else {
size_t bytes_to_read = size - pending_data.size();
pending_data.swap(ret);
ret.resize(size);
co_await asio::async_read(
sock,
asio::buffer(ret.data() + size - bytes_to_read, bytes_to_read),
asio::use_awaitable);
}
co_return ret;
}
static asio::awaitable<phosg::JSON> post_json_with_timeout(
const Config& cfg,
const std::string& path_suffix,
const std::string& body) {
ParsedHTTPURL url = parse_http_url(cfg.coordinator_url);
std::string path = join_url_path(url.path, path_suffix);
auto executor = co_await asio::this_coro::executor;
auto resolver = std::make_shared<asio::ip::tcp::resolver>(executor);
auto sock = std::make_shared<asio::ip::tcp::socket>(executor);
auto timer = std::make_shared<asio::steady_timer>(executor);
auto timed_out = std::make_shared<bool>(false);
timer->expires_after(std::chrono::microseconds(cfg.request_timeout_usecs));
timer->async_wait([resolver, sock, timed_out](std::error_code ec) -> void {
if (!ec) {
*timed_out = true;
resolver->cancel();
if (sock->is_open()) {
sock->close();
}
}
});
try {
auto endpoints = co_await resolver->async_resolve(url.host, std::format("{}", url.port), asio::use_awaitable);
co_await asio::async_connect(*sock, endpoints, asio::use_awaitable);
std::string host_header = url.host;
if (url.port != 80) {
host_header += std::format(":{}", url.port);
}
std::string request = std::format(
"POST {} HTTP/1.1\r\n"
"Host: {}\r\n"
"User-Agent: psopeeps-newserv\r\n"
"Content-Type: application/json\r\n"
"Accept: application/json\r\n"
"Connection: close\r\n"
"X-Psopeeps-Admin-Secret: {}\r\n"
"Content-Length: {}\r\n"
"\r\n"
"{}",
path,
host_header,
cfg.shared_secret,
body.size(),
body);
co_await asio::async_write(*sock, asio::buffer(request), asio::use_awaitable);
std::string pending_data;
std::string status_line = co_await read_http_line(*sock, pending_data, 0x1000);
if (!status_line.starts_with("HTTP/1.")) {
throw std::runtime_error("invalid HTTP response from coordinator");
}
size_t first_space = status_line.find(' ');
if (first_space == std::string::npos) {
throw std::runtime_error("invalid HTTP status line from coordinator");
}
size_t second_space = status_line.find(' ', first_space + 1);
std::string code_s = status_line.substr(
first_space + 1,
(second_space == std::string::npos) ? std::string::npos : (second_space - first_space - 1));
int response_code = std::stoi(code_s);
size_t content_length = 0;
for (;;) {
std::string line = co_await read_http_line(*sock, pending_data, 0x10000);
if (line.empty()) {
break;
}
size_t colon_offset = line.find(':');
if (colon_offset == std::string::npos) {
continue;
}
std::string name = lowercase(line.substr(0, colon_offset));
std::string value = line.substr(colon_offset + 1);
trim_ascii_inplace(value);
if (name == "content-length") {
size_t end_offset = 0;
content_length = std::stoull(value, &end_offset, 10);
if (end_offset != value.size()) {
throw std::runtime_error("invalid Content-Length from coordinator");
}
}
}
if (response_code != 200) {
throw std::runtime_error(std::format("coordinator returned HTTP {}", response_code));
}
if (content_length > 0x100000) {
throw std::runtime_error("coordinator response is too large");
}
std::string response_body = co_await read_http_data(*sock, pending_data, content_length);
timer->cancel();
co_return phosg::JSON::parse(response_body);
} catch (...) {
timer->cancel();
if (*timed_out) {
throw std::runtime_error("coordinator request timed out");
}
throw;
}
}
static std::string source_label(const Config& cfg) {
if (!cfg.source.empty()) {
return cfg.source;
}
if (!cfg.source_region.empty() && !cfg.source_ship.empty()) {
return cfg.source_region + "-" + cfg.source_ship;
}
if (!cfg.source_region.empty()) {
return cfg.source_region;
}
if (!cfg.source_ship.empty()) {
return cfg.source_ship;
}
return "unknown";
}
static std::string json_escape(const std::string& s) {
std::string ret;
ret.reserve(s.size() + 8);
for (unsigned char ch : s) {
switch (ch) {
case '\\':
ret += "\\\\";
break;
case '"':
ret += "\\\"";
break;
case '\b':
ret += "\\b";
break;
case '\f':
ret += "\\f";
break;
case '\n':
ret += "\\n";
break;
case '\r':
ret += "\\r";
break;
case '\t':
ret += "\\t";
break;
default:
if (ch < 0x20) {
char buf[8];
std::snprintf(buf, sizeof(buf), "\\u%04X", ch);
ret += buf;
} else {
ret += static_cast<char>(ch);
}
}
}
return ret;
}
static void append_spool_line(const Config& cfg, const std::string& line) {
if (cfg.spool_directory.empty()) {
return;
}
try {
std::lock_guard<std::mutex> g(spool_mutex);
std::filesystem::create_directories(cfg.spool_directory);
std::filesystem::path path = std::filesystem::path(cfg.spool_directory) / "events.jsonl";
std::ofstream f(path, std::ios::app);
if (!f) {
throw std::runtime_error("failed to open spool file");
}
f << line << '\n';
} catch (const std::exception& e) {
std::fprintf(stderr,
"[AccountSync] warning failed_to_write_spool directory=%s error=%s\n",
cfg.spool_directory.c_str(),
e.what());
}
}
static std::string base_event_json(const Config& cfg, const char* event, uint32_t account_id) {
return std::format(
"{{\"timestamp_usecs\":{},\"producer\":\"newserv\",\"source\":\"{}\",\"source_region\":\"{}\",\"source_ship\":\"{}\",\"account_store\":\"{}\",\"event\":\"{}\",\"account_id\":{},\"account_id_str\":\"{:010}\"",
now_usecs(),
json_escape(source_label(cfg)),
json_escape(cfg.source_region),
json_escape(cfg.source_ship),
json_escape(cfg.account_store),
json_escape(event),
static_cast<unsigned int>(account_id),
static_cast<unsigned int>(account_id));
}
void configure(const Config& cfg) {
{
std::lock_guard<std::mutex> g(config_mutex);
current_config = cfg;
}
if (cfg.enabled) {
std::fprintf(stderr,
"[AccountSync] config enabled source=%s source_region=%s source_ship=%s account_store=%s coordinator_url=%s notify_bb_sessions=%s spool_directory=%s login_locks=%s\n",
source_label(cfg).c_str(),
cfg.source_region.c_str(),
cfg.source_ship.c_str(),
cfg.account_store.c_str(),
cfg.coordinator_url.c_str(),
cfg.notify_bb_sessions ? "true" : "false",
cfg.spool_directory.c_str(),
cfg.enable_login_locks ? "true" : "false");
}
}
void configure_from_json(const phosg::JSON& json) {
Config cfg;
cfg.enabled = json.get_bool("Enabled", false);
const std::string legacy_region = json.get_string("Region", "");
cfg.source = json.get_string("Source", legacy_region);
cfg.source_region = json.get_string("SourceRegion", "");
cfg.source_ship = json.get_string("SourceShip", "");
cfg.account_store = json.get_string("AccountStore", "shared");
if (cfg.source_region.empty() && cfg.source_ship.empty() && !legacy_region.empty()) {
size_t dash_offset = legacy_region.find('-');
if (dash_offset != std::string::npos) {
cfg.source_region = legacy_region.substr(0, dash_offset);
cfg.source_ship = legacy_region.substr(dash_offset + 1);
} else {
cfg.source_region = legacy_region;
}
}
cfg.coordinator_url = json.get_string("CoordinatorURL", "");
cfg.shared_secret = json.get_string("SharedSecret", "");
cfg.request_timeout_usecs = json.get_int("RequestTimeoutUsecs", 3000000);
cfg.fail_open = json.get_bool("FailOpen", false);
cfg.notify_account_saves = json.get_bool("NotifyAccountSaves", true);
cfg.notify_player_saves = json.get_bool("NotifyPlayerSaves", true);
cfg.notify_backup_saves = json.get_bool("NotifyBackupSaves", true);
cfg.enable_login_locks = json.get_bool("EnableLoginLocks", false);
cfg.login_lock_heartbeat_interval_usecs = json.get_int("LoginLockHeartbeatIntervalUsecs", 60000000);
cfg.notify_bb_sessions = json.get_bool("NotifyBBSessions", cfg.enable_login_locks);
cfg.spool_directory = json.get_string("SpoolDirectory", "system/account-sync-spool");
configure(cfg);
}
static asio::awaitable<void> send_login_lock_heartbeat() {
auto cfg = get_config();
if (!cfg.enabled || !cfg.enable_login_locks) {
co_return;
}
if (cfg.coordinator_url.empty()) {
std::fprintf(stderr,
"[AccountSync] warning login_lock_heartbeat_skipped reason=coordinator_url_not_configured source=%s\n",
source_label(cfg).c_str());
co_return;
}
std::string body = std::format(
"{{\"source\":\"{}\",\"source_region\":\"{}\",\"source_ship\":\"{}\",\"account_store\":\"{}\"}}",
json_escape(source_label(cfg)),
json_escape(cfg.source_region),
json_escape(cfg.source_ship),
json_escape(cfg.account_store));
try {
phosg::JSON response = co_await post_json_with_timeout(cfg, "/account-locks/heartbeat", body);
bool ok = response.get_bool("ok", response.get_bool("OK", false));
if (!ok) {
std::fprintf(stderr,
"[AccountSync] warning login_lock_heartbeat_rejected source=%s response=%s\n",
source_label(cfg).c_str(),
response.serialize().c_str());
co_return;
}
int64_t refreshed = response.get_int("refreshed", 0);
std::fprintf(stderr,
"[AccountSync] login_lock_heartbeat_ok source=%s refreshed=%" PRId64 "\n",
source_label(cfg).c_str(),
refreshed);
} catch (const std::exception& e) {
std::fprintf(stderr,
"[AccountSync] warning login_lock_heartbeat_failed source=%s error=%s\n",
source_label(cfg).c_str(),
e.what());
}
}
static asio::awaitable<void> login_lock_heartbeat_task() {
for (;;) {
auto cfg = get_config();
uint64_t interval_usecs = cfg.login_lock_heartbeat_interval_usecs;
if (interval_usecs < 5000000) {
interval_usecs = 5000000;
}
co_await async_sleep(std::chrono::microseconds(interval_usecs));
co_await send_login_lock_heartbeat();
}
}
void start_login_lock_heartbeat_task(asio::io_context& io_context) {
login_lock_io_context = &io_context;
bool expected = false;
if (!heartbeat_task_started.compare_exchange_strong(expected, true)) {
return;
}
asio::co_spawn(io_context, login_lock_heartbeat_task(), asio::detached);
std::fprintf(stderr, "[AccountSync] login lock heartbeat task started\n");
}
asio::awaitable<LoginLockAcquireResult> acquire_login_lock(
uint32_t account_id,
const std::string& version_name,
const std::string& existing_session_nonce) {
auto cfg = get_config();
LoginLockAcquireResult ret;
if (!cfg.enabled || !cfg.enable_login_locks) {
co_return ret;
}
if (!existing_session_nonce.empty()) {
ret.session_nonce = existing_session_nonce;
co_return ret;
}
std::string proposed_session_nonce = std::format("{}-{}-{}", source_label(cfg), account_id, now_usecs());
if (cfg.coordinator_url.empty()) {
std::string message = "account lock coordinator URL is not configured";
if (cfg.fail_open) {
ret.allowed = true;
ret.fail_open_used = true;
ret.session_nonce = proposed_session_nonce;
ret.message = message;
std::fprintf(stderr,
"[AccountSync] warning login_lock_fail_open reason=%s account_id=%010u source=%s version=%s nonce=%s\n",
message.c_str(),
static_cast<unsigned int>(account_id),
source_label(cfg).c_str(),
version_name.c_str(),
ret.session_nonce.c_str());
co_return ret;
}
ret.allowed = false;
ret.message = "$C6Account lock server\nis unavailable.";
std::fprintf(stderr,
"[AccountSync] login_lock_denied reason=%s account_id=%010u source=%s version=%s\n",
message.c_str(),
static_cast<unsigned int>(account_id),
source_label(cfg).c_str(),
version_name.c_str());
co_return ret;
}
std::string body = std::format(
"{{\"account_id\":{},\"account_id_str\":\"{:010}\",\"source\":\"{}\",\"source_region\":\"{}\",\"source_ship\":\"{}\",\"account_store\":\"{}\",\"version\":\"{}\",\"session_nonce\":\"{}\"}}",
static_cast<unsigned int>(account_id),
static_cast<unsigned int>(account_id),
json_escape(source_label(cfg)),
json_escape(cfg.source_region),
json_escape(cfg.source_ship),
json_escape(cfg.account_store),
json_escape(version_name),
json_escape(proposed_session_nonce));
try {
phosg::JSON response = co_await post_json_with_timeout(cfg, "/account-locks/acquire", body);
ret.allowed = response.get_bool("ok", response.get_bool("OK", false));
ret.session_nonce = response.get_string("session_nonce", proposed_session_nonce);
ret.message = response.get_string("message", "");
ret.holder_source = response.get_string("holder_source", "");
if (ret.allowed) {
if (ret.session_nonce.empty()) {
ret.session_nonce = proposed_session_nonce;
}
std::fprintf(stderr,
"[AccountSync] login_lock_acquired account_id=%010u source=%s version=%s nonce=%s\n",
static_cast<unsigned int>(account_id),
source_label(cfg).c_str(),
version_name.c_str(),
ret.session_nonce.c_str());
} else {
if (ret.message.empty()) {
ret.message = "$C6Account is already active\non another ship.";
}
std::fprintf(stderr,
"[AccountSync] login_lock_denied account_id=%010u source=%s version=%s holder_source=%s message=%s\n",
static_cast<unsigned int>(account_id),
source_label(cfg).c_str(),
version_name.c_str(),
ret.holder_source.c_str(),
ret.message.c_str());
}
co_return ret;
} catch (const std::exception& e) {
if (cfg.fail_open) {
ret.allowed = true;
ret.fail_open_used = true;
ret.session_nonce = proposed_session_nonce;
ret.message = e.what();
std::fprintf(stderr,
"[AccountSync] warning login_lock_fail_open reason=%s account_id=%010u source=%s version=%s nonce=%s\n",
e.what(),
static_cast<unsigned int>(account_id),
source_label(cfg).c_str(),
version_name.c_str(),
ret.session_nonce.c_str());
co_return ret;
}
ret.allowed = false;
ret.message = "$C6Account lock server\nis unavailable.";
std::fprintf(stderr,
"[AccountSync] login_lock_denied reason=%s account_id=%010u source=%s version=%s\n",
e.what(),
static_cast<unsigned int>(account_id),
source_label(cfg).c_str(),
version_name.c_str());
co_return ret;
}
}
static asio::awaitable<void> send_login_lock_session_end(
uint32_t account_id,
std::string session_nonce,
std::string version_name) {
auto cfg = get_config();
if (!cfg.enabled || !cfg.enable_login_locks) {
co_return;
}
if (cfg.coordinator_url.empty()) {
std::fprintf(stderr,
"[AccountSync] warning login_lock_session_end_skipped reason=coordinator_url_not_configured account_id=%010u source=%s nonce=%s\n",
static_cast<unsigned int>(account_id),
source_label(cfg).c_str(),
session_nonce.c_str());
co_return;
}
if (session_nonce.empty()) {
std::fprintf(stderr,
"[AccountSync] warning login_lock_session_end_skipped reason=empty_session_nonce account_id=%010u source=%s version=%s\n",
static_cast<unsigned int>(account_id),
source_label(cfg).c_str(),
version_name.c_str());
co_return;
}
std::string body = std::format(
"{{\"account_id\":{},\"account_id_str\":\"{:010}\",\"source\":\"{}\",\"source_region\":\"{}\",\"source_ship\":\"{}\",\"account_store\":\"{}\",\"version\":\"{}\",\"session_nonce\":\"{}\"}}",
static_cast<unsigned int>(account_id),
static_cast<unsigned int>(account_id),
json_escape(source_label(cfg)),
json_escape(cfg.source_region),
json_escape(cfg.source_ship),
json_escape(cfg.account_store),
json_escape(version_name),
json_escape(session_nonce));
try {
phosg::JSON response = co_await post_json_with_timeout(cfg, "/account-locks/session-end", body);
bool ok = response.get_bool("ok", response.get_bool("OK", false));
if (!ok) {
std::fprintf(stderr,
"[AccountSync] warning login_lock_session_end_rejected account_id=%010u source=%s nonce=%s response=%s\n",
static_cast<unsigned int>(account_id),
source_label(cfg).c_str(),
session_nonce.c_str(),
response.serialize().c_str());
co_return;
}
std::fprintf(stderr,
"[AccountSync] login_lock_session_end_ok account_id=%010u source=%s nonce=%s\n",
static_cast<unsigned int>(account_id),
source_label(cfg).c_str(),
session_nonce.c_str());
} catch (const std::exception& e) {
std::fprintf(stderr,
"[AccountSync] warning login_lock_session_end_failed account_id=%010u source=%s nonce=%s error=%s\n",
static_cast<unsigned int>(account_id),
source_label(cfg).c_str(),
session_nonce.c_str(),
e.what());
}
}
void notify_login_session_end(
uint32_t account_id,
const std::string& session_nonce,
const std::string& version_name) {
auto cfg = get_config();
if (!cfg.enabled || !cfg.enable_login_locks) {
return;
}
std::fprintf(stderr,
"[AccountSync] event=login_session_end source=%s source_region=%s source_ship=%s account_store=%s account_id=%010u session_nonce=%s version=%s\n",
source_label(cfg).c_str(),
cfg.source_region.c_str(),
cfg.source_ship.c_str(),
cfg.account_store.c_str(),
static_cast<unsigned int>(account_id),
session_nonce.c_str(),
version_name.c_str());
if (login_lock_io_context) {
asio::co_spawn(
*login_lock_io_context,
send_login_lock_session_end(account_id, session_nonce, version_name),
asio::detached);
} else {
std::fprintf(stderr,
"[AccountSync] warning login_lock_session_end_not_spawned reason=io_context_not_available account_id=%010u source=%s nonce=%s\n",
static_cast<unsigned int>(account_id),
source_label(cfg).c_str(),
session_nonce.c_str());
}
auto line = base_event_json(cfg, "login_session_end", account_id) +
std::format(",\"session_nonce\":\"{}\",\"version\":\"{}\"}}",
json_escape(session_nonce),
json_escape(version_name));
append_spool_line(cfg, line);
}
void notify_account_saved(uint32_t account_id, const std::string& filename) {
auto cfg = get_config();
if (!cfg.enabled || !cfg.notify_account_saves) {
return;
}
std::fprintf(stderr,
"[AccountSync] event=account_saved source=%s source_region=%s source_ship=%s account_store=%s account_id=%010u filename=%s\n",
source_label(cfg).c_str(),
cfg.source_region.c_str(),
cfg.source_ship.c_str(),
cfg.account_store.c_str(),
static_cast<unsigned int>(account_id),
filename.c_str());
auto line = base_event_json(cfg, "account_saved", account_id) +
std::format(",\"filename\":\"{}\"}}", json_escape(filename));
append_spool_line(cfg, line);
}
void notify_backup_saved(uint32_t account_id, size_t slot, const std::string& filename) {
auto cfg = get_config();
if (!cfg.enabled || !cfg.notify_backup_saves) {
return;
}
std::fprintf(stderr,
"[AccountSync] event=backup_saved source=%s source_region=%s source_ship=%s account_store=%s account_id=%010u slot=%zu filename=%s\n",
source_label(cfg).c_str(),
cfg.source_region.c_str(),
cfg.source_ship.c_str(),
cfg.account_store.c_str(),
static_cast<unsigned int>(account_id),
slot,
filename.c_str());
auto line = base_event_json(cfg, "backup_saved", account_id) +
std::format(",\"slot\":{},\"filename\":\"{}\"}}", slot, json_escape(filename));
append_spool_line(cfg, line);
}
void notify_player_state_saved(
const char* reason,
uint32_t account_id,
const std::string& bb_username,
const std::string& filename) {
auto cfg = get_config();
if (!cfg.enabled || !cfg.notify_player_saves) {
return;
}
std::fprintf(stderr,
"[AccountSync] event=player_state_saved source=%s source_region=%s source_ship=%s account_store=%s reason=%s account_id=%010u bb_username=%s filename=%s\n",
source_label(cfg).c_str(),
cfg.source_region.c_str(),
cfg.source_ship.c_str(),
cfg.account_store.c_str(),
reason,
static_cast<unsigned int>(account_id),
bb_username.c_str(),
filename.c_str());
auto line = base_event_json(cfg, "player_state_saved", account_id) +
std::format(",\"reason\":\"{}\",\"bb_username\":\"{}\",\"filename\":\"{}\"}}",
json_escape(reason),
json_escape(bb_username),
json_escape(filename));
append_spool_line(cfg, line);
}
void notify_bb_login_start(
uint32_t account_id,
const std::string& bb_username,
int64_t character_slot,
uint8_t connection_phase) {
auto cfg = get_config();
if (!cfg.enabled || !cfg.notify_bb_sessions) {
return;
}
std::fprintf(stderr,
"[AccountSync] event=bb_login_start source=%s source_region=%s source_ship=%s account_store=%s account_id=%010u bb_username=%s character_slot=%lld connection_phase=%u\n",
source_label(cfg).c_str(),
cfg.source_region.c_str(),
cfg.source_ship.c_str(),
cfg.account_store.c_str(),
static_cast<unsigned int>(account_id),
bb_username.c_str(),
static_cast<long long>(character_slot),
static_cast<unsigned int>(connection_phase));
auto line = base_event_json(cfg, "bb_login_start", account_id) +
std::format(",\"bb_username\":\"{}\",\"character_slot\":{},\"connection_phase\":{}}}",
json_escape(bb_username),
static_cast<long long>(character_slot),
static_cast<unsigned int>(connection_phase));
append_spool_line(cfg, line);
}
void notify_bb_login_end(
uint32_t account_id,
const std::string& bb_username,
int64_t character_slot) {
auto cfg = get_config();
if (!cfg.enabled || !cfg.notify_bb_sessions) {
return;
}
std::fprintf(stderr,
"[AccountSync] event=bb_login_end source=%s source_region=%s source_ship=%s account_store=%s account_id=%010u bb_username=%s character_slot=%lld\n",
source_label(cfg).c_str(),
cfg.source_region.c_str(),
cfg.source_ship.c_str(),
cfg.account_store.c_str(),
static_cast<unsigned int>(account_id),
bb_username.c_str(),
static_cast<long long>(character_slot));
auto line = base_event_json(cfg, "bb_login_end", account_id) +
std::format(",\"bb_username\":\"{}\",\"character_slot\":{}}}",
json_escape(bb_username),
static_cast<long long>(character_slot));
append_spool_line(cfg, line);
}
} // namespace AccountSync
+79
View File
@@ -0,0 +1,79 @@
#pragma once
#include <cstddef>
#include <cstdint>
#include <asio.hpp>
#include <string>
#include <phosg/JSON.hh>
namespace AccountSync {
struct Config {
bool enabled = false;
// Source identity. Region is no longer enough because account JSON is shared
// across live/test/hardcore in a region.
std::string source;
std::string source_region;
std::string source_ship;
std::string account_store = "shared";
std::string coordinator_url;
std::string shared_secret;
uint64_t request_timeout_usecs = 3000000;
bool fail_open = false;
bool notify_account_saves = true;
bool notify_player_saves = true;
bool notify_backup_saves = true;
bool notify_bb_sessions = false;
bool enable_login_locks = false; // Reserved for future blocking lock behavior
uint64_t login_lock_heartbeat_interval_usecs = 60000000;
std::string spool_directory = "system/account-sync-spool";
};
void configure(const Config& cfg);
void configure_from_json(const phosg::JSON& json);
struct LoginLockAcquireResult {
bool allowed = true;
bool fail_open_used = false;
std::string session_nonce;
std::string message;
std::string holder_source;
};
void start_login_lock_heartbeat_task(asio::io_context& io_context);
asio::awaitable<LoginLockAcquireResult> acquire_login_lock(
uint32_t account_id,
const std::string& version_name,
const std::string& existing_session_nonce);
void notify_login_session_end(
uint32_t account_id,
const std::string& session_nonce,
const std::string& version_name);
void notify_account_saved(uint32_t account_id, const std::string& filename);
void notify_backup_saved(uint32_t account_id, size_t slot, const std::string& filename);
void notify_player_state_saved(
const char* reason,
uint32_t account_id,
const std::string& bb_username,
const std::string& filename);
void notify_bb_login_start(
uint32_t account_id,
const std::string& bb_username,
int64_t character_slot,
uint8_t connection_phase);
void notify_bb_login_end(
uint32_t account_id,
const std::string& bb_username,
int64_t character_slot);
} // namespace AccountSync
+296 -106
View File
@@ -5,6 +5,7 @@
#include <future>
#include <phosg/Filesystem.hh>
#include <phosg/Strings.hh>
#include <resource_file/Emulators/PPC32Emulator.hh>
#include <resource_file/Emulators/X86Emulator.hh>
#include <resource_file/ExecutableFormats/DOLFile.hh>
#include <resource_file/ExecutableFormats/PEFile.hh>
@@ -14,8 +15,6 @@
#include "Text.hh"
#include "Types.hh"
using namespace std;
class AddressTranslator {
public:
enum class ExpandMethod {
@@ -63,7 +62,7 @@ public:
case ExpandMethod::RAW_BOTH:
return "RAW_BOTH";
default:
throw logic_error("invalid expand method");
throw std::logic_error("invalid expand method");
}
}
@@ -85,7 +84,7 @@ public:
case ExpandMethod::RAW_BOTH:
return false;
default:
throw logic_error("invalid expand method");
throw std::logic_error("invalid expand method");
}
}
@@ -107,46 +106,46 @@ public:
case ExpandMethod::RAW_BOTH:
return false;
default:
throw logic_error("invalid expand method");
throw std::logic_error("invalid expand method");
}
}
AddressTranslator(const string& directory)
AddressTranslator(const std::string& directory)
: log("[addr-trans] "),
directory(directory) {
while (this->directory.ends_with("/")) {
this->directory.pop_back();
}
for (const auto& item : std::filesystem::directory_iterator(this->directory)) {
string filename = item.path().filename().string();
std::string filename = item.path().filename().string();
if (filename.size() < 4) {
continue;
}
string name = filename.substr(0, filename.size() - 4);
string path = directory + "/" + filename;
std::string name = filename.substr(0, filename.size() - 4);
std::string path = directory + "/" + filename;
if (filename.ends_with(".dol")) {
ResourceDASM::DOLFile dol(path.c_str());
auto mem = make_shared<ResourceDASM::MemoryContext>();
auto mem = std::make_shared<ResourceDASM::MemoryContext>();
dol.load_into(mem);
this->mems.emplace(name, mem);
this->ppc_mems.emplace(mem);
this->log.info_f("Loaded {}", name);
} else if (filename.ends_with(".xbe")) {
ResourceDASM::XBEFile xbe(path.c_str());
auto mem = make_shared<ResourceDASM::MemoryContext>();
auto mem = std::make_shared<ResourceDASM::MemoryContext>();
xbe.load_into(mem);
this->mems.emplace(name, mem);
this->log.info_f("Loaded {}", name);
} else if (filename.ends_with(".exe")) {
ResourceDASM::PEFile pe(path.c_str());
auto mem = make_shared<ResourceDASM::MemoryContext>();
auto mem = std::make_shared<ResourceDASM::MemoryContext>();
pe.load_into(mem);
this->mems.emplace(name, mem);
this->log.info_f("Loaded {}", name);
} else if (filename.ends_with(".bin")) {
string data = phosg::load_file(path);
auto mem = make_shared<ResourceDASM::MemoryContext>();
std::string data = phosg::load_file(path);
auto mem = std::make_shared<ResourceDASM::MemoryContext>();
mem->allocate_at(0x8C010000, data.size());
mem->memcpy(0x8C010000, data.data(), data.size());
this->mems.emplace(name, mem);
@@ -156,10 +155,10 @@ public:
}
~AddressTranslator() = default;
const string& get_source_filename() const {
const std::string& get_source_filename() const {
return this->src_filename;
}
void set_source_file(const string& filename) {
void set_source_file(const std::string& filename) {
this->src_filename = filename;
this->src_mem = this->mems.at(this->src_filename);
}
@@ -178,25 +177,25 @@ public:
uint32_t opcode = r.get_u32b();
if ((opcode & 0xFFFF0000) == 0x3DA00000) {
if (r13_high_found) {
throw runtime_error("multiple values for r13_high");
throw std::runtime_error("multiple values for r13_high");
}
r13_high_found = true;
r13 |= (opcode << 16);
} else if ((opcode & 0xFFFF0000) == 0x3C400000) {
if (r2_high_found) {
throw runtime_error("multiple values for r2_high");
throw std::runtime_error("multiple values for r2_high");
}
r2_high_found = true;
r2 |= (opcode << 16);
} else if ((opcode & 0xFFFF0000) == 0x61AD0000) {
if (r13_low_found) {
throw runtime_error("multiple values for r13_low");
throw std::runtime_error("multiple values for r13_low");
}
r13_low_found = true;
r13 |= (opcode & 0xFFFF);
} else if ((opcode & 0xFFFF0000) == 0x60420000) {
if (r2_low_found) {
throw runtime_error("multiple values for r2_low");
throw std::runtime_error("multiple values for r2_low");
}
r2_low_found = true;
r2 |= (opcode & 0xFFFF);
@@ -217,11 +216,11 @@ public:
}
struct ParseDATConstructorTableSpec {
string src_name;
std::string src_name;
uint32_t index_addr;
size_t num_areas;
bool has_names;
vector<uint32_t> x86_constructor_calls;
std::vector<uint32_t> x86_constructor_calls;
ParseDATConstructorTableSpec(const phosg::JSON& json) {
this->src_name = json.at("SourceName").as_string();
@@ -233,8 +232,8 @@ public:
}
}
static vector<ParseDATConstructorTableSpec> from_json_list(const phosg::JSON& json) {
vector<ParseDATConstructorTableSpec> ret;
static std::vector<ParseDATConstructorTableSpec> from_json_list(const phosg::JSON& json) {
std::vector<ParseDATConstructorTableSpec> ret;
for (const auto& z : json.as_list()) {
ret.emplace_back(*z);
}
@@ -267,25 +266,25 @@ public:
// Returns {type: {constructor_addr: [(start_area, end_area), ...]}}
template <typename EntryT>
map<uint32_t, map<uint32_t, vector<pair<size_t, size_t>>>> parse_dat_constructor_table_t(
shared_ptr<const ResourceDASM::MemoryContext>& mem, const ParseDATConstructorTableSpec& spec) {
std::map<uint32_t, std::map<uint32_t, std::vector<std::pair<size_t, size_t>>>> parse_dat_constructor_table_t(
std::shared_ptr<const ResourceDASM::MemoryContext>& mem, const ParseDATConstructorTableSpec& spec) {
if (!mem) {
throw runtime_error("no file selected");
throw std::runtime_error("no file selected");
}
// On some of the x86 builds of the game (PCv2 and Xbox), the constructor tables aren't entirely static in the data
// sections - some parts are written during static initialization instead. To handle this, we make a copy of the
// immutable MemoryContext and run the static initialization functions using resource_dasm's emulator before
// parsing the constructor table.
shared_ptr<const ResourceDASM::MemoryContext> effective_mem = mem;
std::shared_ptr<const ResourceDASM::MemoryContext> effective_mem = mem;
if (!spec.x86_constructor_calls.empty()) {
auto constructed_mem = make_shared<ResourceDASM::MemoryContext>(mem->duplicate());
auto constructed_mem = std::make_shared<ResourceDASM::MemoryContext>(mem->duplicate());
uint32_t esp = constructed_mem->allocate(0x1000) + 0x1000;
for (uint32_t constructor_addr : spec.x86_constructor_calls) {
ResourceDASM::X86Emulator emu(constructed_mem);
// Uncomment for debugging
// auto debugger = make_shared<ResourceDASM::EmulatorDebugger<ResourceDASM::X86Emulator>>();
// auto debugger = std::make_shared<ResourceDASM::EmulatorDebugger<ResourceDASM::X86Emulator>>();
// debugger->bind(emu);
// debugger->state.mode = ResourceDASM::DebuggerMode::TRACE;
@@ -295,7 +294,7 @@ public:
constructed_mem->write_u32l(esp - 4, 0xFFFFFFFF); // Return addr
try {
emu.execute();
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
if (regs.eip != 0xFFFFFFFF) {
throw;
}
@@ -304,7 +303,7 @@ public:
effective_mem = constructed_mem;
}
map<uint32_t, map<uint32_t, vector<pair<size_t, size_t>>>> table;
std::map<uint32_t, std::map<uint32_t, std::vector<std::pair<size_t, size_t>>>> table;
auto index_r = effective_mem->reader(spec.index_addr, spec.num_areas * sizeof(uint32_t));
for (size_t area = 0; area < spec.num_areas; area++) {
@@ -322,18 +321,18 @@ public:
if (!group.empty() && (group.back().second == (area - 1))) {
group.back().second = area;
} else {
group.emplace_back(make_pair(area, area));
group.emplace_back(std::make_pair(area, area));
}
}
if (entries_r.eof()) {
throw runtime_error("did not find end-of-entries marker");
throw std::runtime_error("did not find end-of-entries marker");
}
}
return table;
}
static uint64_t area_mask_for_ranges(const vector<pair<size_t, size_t>>& ranges) {
static uint64_t area_mask_for_ranges(const std::vector<std::pair<size_t, size_t>>& ranges) {
uint64_t ret = 0;
for (const auto& [start, end] : ranges) {
for (size_t z = start; z <= end; z++) {
@@ -344,7 +343,7 @@ public:
}
void parse_dat_constructor_table(const ParseDATConstructorTableSpec& spec) {
map<uint32_t, map<uint32_t, vector<pair<size_t, size_t>>>> table;
std::map<uint32_t, std::map<uint32_t, std::vector<std::pair<size_t, size_t>>>> table;
auto spec_mem = this->mems.at(spec.src_name);
if (this->ppc_mems.count(spec_mem)) {
table = this->parse_dat_constructor_table_t<DATConstructorTableEntry<true>>(spec_mem, spec);
@@ -374,10 +373,10 @@ public:
}
void parse_dat_constructor_table_multi(
const vector<ParseDATConstructorTableSpec>& specs, bool is_enemies, bool print_area_masks) {
map<string, map<uint32_t, map<uint32_t, vector<pair<size_t, size_t>>>>> all_tables;
const std::vector<ParseDATConstructorTableSpec>& specs, bool is_enemies, bool print_area_masks) {
std::map<std::string, std::map<uint32_t, std::map<uint32_t, std::vector<std::pair<size_t, size_t>>>>> all_tables;
for (const auto& spec : specs) {
map<uint32_t, map<uint32_t, vector<pair<size_t, size_t>>>> table;
std::map<uint32_t, std::map<uint32_t, std::vector<std::pair<size_t, size_t>>>> table;
auto spec_mem = this->mems.at(spec.src_name);
if (this->ppc_mems.count(spec_mem)) {
table = this->parse_dat_constructor_table_t<DATConstructorTableEntry<true>>(spec_mem, spec);
@@ -389,14 +388,14 @@ public:
all_tables.emplace(spec.src_name, std::move(table));
}
map<string, size_t> version_widths;
map<uint32_t, map<string, string>> formatted_cells_for_type;
std::map<std::string, size_t> version_widths;
std::map<uint32_t, std::map<std::string, std::string>> formatted_cells_for_type;
for (const auto& spec : specs) {
const auto& table = all_tables.at(spec.src_name);
size_t max_width = 0;
for (const auto& [type, constructor_to_area_ranges] : table) {
string cell_data;
std::string cell_data;
for (const auto& [constructor, area_ranges] : constructor_to_area_ranges) {
if (!cell_data.empty()) {
cell_data.push_back(' ');
@@ -417,14 +416,14 @@ public:
}
}
}
max_width = max<size_t>(max_width, cell_data.size());
max_width = std::max<size_t>(max_width, cell_data.size());
formatted_cells_for_type[type][spec.src_name] = std::move(cell_data);
}
version_widths[spec.src_name] = max_width;
}
vector<string> formatted_lines;
string header_line = "TYPE =>";
std::vector<std::string> formatted_lines;
std::string header_line = "TYPE =>";
for (const auto& spec : specs) {
size_t width = version_widths.at(spec.src_name);
header_line.push_back(' ');
@@ -436,7 +435,7 @@ public:
header_line += " NAME";
for (const auto& [type, formatted_cells] : formatted_cells_for_type) {
string line = std::format("{:04X} =>", type);
std::string line = std::format("{:04X} =>", type);
for (const auto& spec : specs) {
size_t width = version_widths.at(spec.src_name);
try {
@@ -446,7 +445,7 @@ public:
if (width > cell_data.size()) {
line.resize(line.size() + (width - cell_data.size()), ' ');
}
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
line.resize(line.size() + (width + 1), ' ');
}
}
@@ -466,21 +465,21 @@ public:
}
uint32_t find_match(
shared_ptr<const ResourceDASM::MemoryContext> dest_mem,
std::shared_ptr<const ResourceDASM::MemoryContext> dest_mem,
uint32_t src_addr,
uint32_t src_size,
ExpandMethod expand_method) const {
bool is_ppc = this->is_ppc_expand_method(expand_method);
bool is_ppc_data = this->is_ppc_data_expand_method(expand_method);
if (!this->src_mem) {
throw runtime_error("no source file selected");
throw std::runtime_error("no source file selected");
}
if (src_size == 0) {
src_size = is_ppc ? 4 : 1;
}
pair<uint32_t, uint32_t> src_section = make_pair(0, 0);
std::pair<uint32_t, uint32_t> src_section = std::make_pair(0, 0);
for (const auto& sec : this->src_mem->allocated_blocks()) {
if (src_addr >= sec.first && src_addr + src_size <= sec.first + sec.second) {
src_section = sec;
@@ -488,7 +487,7 @@ public:
}
}
if (!src_section.second) {
throw runtime_error("source address not within any section");
throw std::runtime_error("source address not within any section");
}
const char* method_token = this->name_for_expand_method(expand_method);
@@ -570,7 +569,7 @@ public:
if (num_matches == 1) {
return last_match_address;
} else if (num_matches == 0) {
throw runtime_error("did not find exactly one match");
throw std::runtime_error("did not find exactly one match");
}
bool can_expand_backward = false;
bool can_expand_forward = false;
@@ -614,10 +613,10 @@ public:
can_expand_forward = (src_bytes_available_after > match_bytes_after);
break;
default:
throw logic_error("invalid expand method");
throw std::logic_error("invalid expand method");
}
if (!can_expand_backward && !can_expand_forward) {
throw runtime_error("no further expansion is allowed");
throw std::runtime_error("no further expansion is allowed");
}
if (can_expand_backward) {
match_bytes_before += (is_ppc ? 4 : 1);
@@ -626,7 +625,7 @@ public:
match_bytes_after += (is_ppc ? 4 : 1);
}
}
throw runtime_error("scan field too long; too many matches");
throw std::runtime_error("scan field too long; too many matches");
}
enum class MatchType {
@@ -637,18 +636,18 @@ public:
void find_all_matches(uint32_t src_addr, uint32_t src_size, MatchType type) const {
if (!this->src_mem) {
throw runtime_error("no source file selected");
throw std::runtime_error("no source file selected");
}
map<string, uint32_t> results;
std::map<std::string, uint32_t> results;
for (const auto& it : this->mems) {
if (it.second == this->src_mem) {
log.info_f("({}) {:08X} (from source)", it.first, src_addr);
results.emplace(it.first, src_addr);
} else {
vector<future<uint32_t>> futures;
static const vector<ExpandMethod> ppc_methods = {
std::vector<std::future<uint32_t>> futures;
static const std::vector<ExpandMethod> ppc_methods = {
ExpandMethod::PPC_TEXT_FORWARD,
ExpandMethod::PPC_TEXT_FORWARD_WITH_BARRIER,
ExpandMethod::PPC_TEXT_BACKWARD,
@@ -660,7 +659,7 @@ public:
ExpandMethod::PPC_DATA_BACKWARD,
ExpandMethod::PPC_DATA_BOTH,
};
static const vector<ExpandMethod> ppc_text_methods = {
static const std::vector<ExpandMethod> ppc_text_methods = {
ExpandMethod::PPC_TEXT_FORWARD,
ExpandMethod::PPC_TEXT_FORWARD_WITH_BARRIER,
ExpandMethod::PPC_TEXT_BACKWARD,
@@ -669,18 +668,18 @@ public:
ExpandMethod::PPC_TEXT_BOTH_WITH_BARRIER,
ExpandMethod::PPC_TEXT_BOTH_IGNORE_ORIGIN,
};
static const vector<ExpandMethod> ppc_data_methods = {
static const std::vector<ExpandMethod> ppc_data_methods = {
ExpandMethod::PPC_DATA_FORWARD,
ExpandMethod::PPC_DATA_BACKWARD,
ExpandMethod::PPC_DATA_BOTH,
};
static const vector<ExpandMethod> raw_methods = {
static const std::vector<ExpandMethod> raw_methods = {
ExpandMethod::RAW_FORWARD,
ExpandMethod::RAW_BACKWARD,
ExpandMethod::RAW_BOTH,
};
const vector<ExpandMethod>* methods;
const std::vector<ExpandMethod>* methods;
if (this->ppc_mems.count(it.second)) {
if (type == MatchType::ANY) {
methods = &ppc_methods;
@@ -689,7 +688,7 @@ public:
} else if (type == MatchType::DATA) {
methods = &ppc_data_methods;
} else {
throw logic_error("invalid match type");
throw std::logic_error("invalid match type");
}
} else {
methods = &raw_methods;
@@ -699,14 +698,14 @@ public:
futures.emplace_back(async(&AddressTranslator::find_match, this, it.second, src_addr, src_size, methods->at(z)));
}
unordered_set<uint32_t> match_addrs;
std::unordered_set<uint32_t> match_addrs;
for (size_t z = 0; z < futures.size(); z++) {
const char* method_name = this->name_for_expand_method(methods->at(z));
try {
uint32_t ret = futures[z].get();
log.info_f("({}) ({}) {:08X}", it.first, method_name, ret);
match_addrs.emplace(ret);
} catch (const exception& e) {
} catch (const std::exception& e) {
log.error_f("({}) ({}) failed: {}", it.first, method_name, e.what());
}
}
@@ -726,12 +725,12 @@ public:
}
uint32_t find_be_to_le_data_match(
shared_ptr<const ResourceDASM::MemoryContext> dest_mem, uint32_t src_addr, uint32_t src_size) const {
std::shared_ptr<const ResourceDASM::MemoryContext> dest_mem, uint32_t src_addr, uint32_t src_size) const {
if (src_size == 0) {
src_size = 4;
}
pair<uint32_t, uint32_t> src_section = make_pair(0, 0);
std::pair<uint32_t, uint32_t> src_section = std::make_pair(0, 0);
for (const auto& sec : this->src_mem->allocated_blocks()) {
if (src_addr >= sec.first && src_addr + src_size <= sec.first + sec.second) {
src_section = sec;
@@ -739,7 +738,7 @@ public:
}
}
if (!src_section.second) {
throw runtime_error("source address not within any section");
throw std::runtime_error("source address not within any section");
}
size_t src_offset = src_addr - src_section.first;
@@ -782,12 +781,12 @@ public:
if (num_matches == 1) {
return last_match_address;
} else if (num_matches == 0) {
throw runtime_error("did not find exactly one match");
throw std::runtime_error("did not find exactly one match");
}
bool can_expand_backward = (src_bytes_available_before >= match_bytes_before + 4);
bool can_expand_forward = (src_bytes_available_after >= match_bytes_after + 4);
if (!can_expand_backward && !can_expand_forward) {
throw runtime_error("no further expansion is allowed");
throw std::runtime_error("no further expansion is allowed");
}
if (can_expand_backward) {
match_bytes_before += 4;
@@ -796,15 +795,15 @@ public:
match_bytes_after += 4;
}
}
throw runtime_error("scan field too long; too many matches");
throw std::runtime_error("scan field too long; too many matches");
}
void find_all_be_to_le_data_matches(uint32_t src_addr, uint32_t src_size) const {
if (!this->src_mem) {
throw runtime_error("no source file selected");
throw std::runtime_error("no source file selected");
}
map<string, uint32_t> results;
std::map<std::string, uint32_t> results;
for (const auto& it : this->mems) {
if (it.second == this->src_mem) {
log.info_f("({}) {:08X} (from source)", it.first, src_addr);
@@ -815,7 +814,7 @@ public:
try {
ret = this->find_be_to_le_data_match(it.second, src_addr, src_size);
log.info_f("({}) {:08X}", it.first, ret);
} catch (const exception& e) {
} catch (const std::exception& e) {
log.error_f("({}) failed: {}", it.first, e.what());
}
@@ -831,7 +830,7 @@ public:
}
}
void find_data(const string& data) const {
void find_data(const std::string& data) const {
for (const auto& [name, mem] : this->mems) {
for (const auto& [sec_addr, sec_size] : mem->allocated_blocks()) {
uint32_t last_addr = sec_addr + sec_size - data.size();
@@ -844,10 +843,10 @@ public:
}
}
void handle_command(const string& command) {
void handle_command(const std::string& command) {
auto tokens = phosg::split(command, ' ');
if (tokens.empty()) {
throw runtime_error("no command given");
throw std::runtime_error("no command given");
}
phosg::strip_trailing_whitespace(tokens[tokens.size() - 1]);
@@ -856,7 +855,7 @@ public:
} else if (tokens[0] == "find") {
this->find_data(phosg::parse_data_string(tokens.at(1)));
} else if (tokens[0] == "only") {
unordered_set<string> to_keep{tokens.begin() + 1, tokens.end()};
std::unordered_set<std::string> to_keep{tokens.begin() + 1, tokens.end()};
for (auto it = this->mems.begin(); it != this->mems.end();) {
if (to_keep.count(it->first)) {
it++;
@@ -891,7 +890,7 @@ public:
auto specs = ParseDATConstructorTableSpec::from_json_list(phosg::JSON::parse(phosg::load_file(tokens.at(1))));
this->parse_dat_constructor_table_multi(specs, is_enemies, true);
} else if (!tokens[0].empty()) {
throw runtime_error("unknown command");
throw std::runtime_error("unknown command");
}
}
@@ -904,10 +903,10 @@ public:
}
fflush(stdout);
string command = phosg::fgets(stdin);
std::string command = phosg::fgets(stdin);
try {
this->handle_command(command);
} catch (const exception& e) {
} catch (const std::exception& e) {
this->log.error_f("Failed: {}", e.what());
}
}
@@ -916,14 +915,14 @@ public:
private:
phosg::PrefixedLogger log;
string directory;
unordered_map<string, shared_ptr<const ResourceDASM::MemoryContext>> mems;
unordered_set<shared_ptr<const ResourceDASM::MemoryContext>> ppc_mems;
string src_filename;
shared_ptr<const ResourceDASM::MemoryContext> src_mem;
std::string directory;
std::unordered_map<std::string, std::shared_ptr<const ResourceDASM::MemoryContext>> mems;
std::unordered_set<std::shared_ptr<const ResourceDASM::MemoryContext>> ppc_mems;
std::string src_filename;
std::shared_ptr<const ResourceDASM::MemoryContext> src_mem;
};
void run_address_translator(const string& directory, const string& use_filename, const string& command) {
void run_address_translator(const std::string& directory, const std::string& use_filename, const std::string& command) {
AddressTranslator trans(directory);
if (!use_filename.empty()) {
trans.set_source_file(use_filename);
@@ -936,32 +935,32 @@ void run_address_translator(const string& directory, const string& use_filename,
}
}
vector<DiffEntry> diff_dol_files(const string& a_filename, const string& b_filename) {
std::vector<DiffEntry> diff_dol_files(const std::string& a_filename, const std::string& b_filename) {
ResourceDASM::DOLFile a(a_filename.c_str());
ResourceDASM::DOLFile b(b_filename.c_str());
auto a_mem = make_shared<ResourceDASM::MemoryContext>();
auto b_mem = make_shared<ResourceDASM::MemoryContext>();
auto a_mem = std::make_shared<ResourceDASM::MemoryContext>();
auto b_mem = std::make_shared<ResourceDASM::MemoryContext>();
a.load_into(a_mem);
b.load_into(b_mem);
uint32_t min_addr = 0xFFFFFFFF;
uint32_t max_addr = 0x00000000;
for (const auto& sec : a.sections) {
min_addr = min<uint32_t>(min_addr, sec.address);
max_addr = max<uint32_t>(max_addr, sec.address + sec.data.size());
min_addr = std::min<uint32_t>(min_addr, sec.address);
max_addr = std::max<uint32_t>(max_addr, sec.address + sec.data.size());
}
for (const auto& sec : b.sections) {
min_addr = min<uint32_t>(min_addr, sec.address);
max_addr = max<uint32_t>(max_addr, sec.address + sec.data.size());
min_addr = std::min<uint32_t>(min_addr, sec.address);
max_addr = std::max<uint32_t>(max_addr, sec.address + sec.data.size());
}
vector<DiffEntry> ret;
std::vector<DiffEntry> ret;
for (uint32_t addr = min_addr; addr < max_addr; addr += 4) {
bool a_exists = a_mem->exists(addr, 4);
bool b_exists = b_mem->exists(addr, 4);
if (a_exists && b_exists) {
string a_value = a_mem->read(addr, 4);
string b_value = b_mem->read(addr, 4);
std::string a_value = a_mem->read(addr, 4);
std::string b_value = b_mem->read(addr, 4);
if (a_value != b_value) {
if (!ret.empty() && (ret.back().address + ret.back().b_data.size() == addr)) {
ret.back().a_data += a_value;
@@ -975,26 +974,217 @@ vector<DiffEntry> diff_dol_files(const string& a_filename, const string& b_filen
return ret;
}
vector<DiffEntry> diff_xbe_files(const string& a_filename, const string& b_filename) {
void diff_dol_files_semantic(
FILE* stream,
const std::string& a_filename,
const std::string& b_filename,
const std::unordered_set<uint32_t>& a_ignore_functions,
const std::unordered_set<uint32_t>& b_ignore_functions) {
ResourceDASM::DOLFile a(a_filename.c_str());
ResourceDASM::DOLFile b(b_filename.c_str());
// There must be the same number of sections
if (a.sections.size() != b.sections.size()) {
throw std::runtime_error("DOL files do not have the same section count");
}
for (size_t section_index = 0; section_index < a.sections.size(); section_index++) {
const auto& a_sec = a.sections[section_index];
const auto& b_sec = b.sections[section_index];
if (!a_sec.is_text || !b_sec.is_text) {
phosg::fwrite_fmt(stderr, "SECTION {} DATA\n", section_index);
// TODO: Diff the contents as binary data
} else {
phosg::fwrite_fmt(stderr, "SECTION {} TEXT\n", section_index);
struct FileAnalysis {
struct Function {
const ResourceDASM::EmulatorBase::DisassembleResult::Label* label;
size_t size;
std::vector<std::pair<uint32_t, uint32_t>> code; // [(opcode, mask)]
};
std::vector<Function> functions;
ResourceDASM::EmulatorBase::DisassembleResult dasm;
};
auto disassemble_section = [&](const ResourceDASM::DOLFile& file, const ResourceDASM::DOLFile::Section& sec) -> FileAnalysis {
std::multimap<uint32_t, std::string> labels;
if ((file.entrypoint >= sec.address) && (file.entrypoint < (sec.address + sec.data.size()))) {
labels.emplace(file.entrypoint, "entry");
}
FileAnalysis ret;
ret.dasm = ResourceDASM::PPC32Emulator::disassemble_structured(
sec.data.data(), sec.data.size(), sec.address, &labels);
FileAnalysis::Function* prev_fn = nullptr;
for (const auto& [addr, label] : ret.dasm.labels) {
if (label.refs.call_addrs.empty() ||
(label.address < sec.address) ||
(label.address >= (sec.address + sec.data.size()))) {
continue;
}
if (prev_fn) {
prev_fn->size = addr - prev_fn->label->address;
}
auto& fn = ret.functions.emplace_back();
fn.label = &label;
prev_fn = &fn;
}
if (prev_fn) {
prev_fn->size = sec.data.size() - (prev_fn->label->address - sec.address);
}
for (auto& fn : ret.functions) {
const be_uint32_t* code = reinterpret_cast<const be_uint32_t*>(
sec.data.data() + (fn.label->address - sec.address));
for (size_t z = 0; z < fn.size >> 2; z++) {
uint32_t opcode = code[z];
if ((opcode & 0xFC000000) == 0x34000000) {
fn.code.emplace_back(opcode, 0xFFFF0000); // subic.
} else if ((opcode & 0xFC000000) == 0x38000000) {
fn.code.emplace_back(opcode, 0xFFFF0000); // addi
} else if ((opcode & 0xFC1F0000) == 0x3C000000) {
fn.code.emplace_back(opcode, 0xFFFF0000); // lis
} else if ((opcode & 0xFC000000) == 0x48000000) {
fn.code.emplace_back(opcode, 0xFC000000); // b[la]
} else if ((opcode & 0xF8000000) == 0x60000000) {
fn.code.emplace_back(opcode, 0xFFFF0000); // ori
} else if ((opcode & 0xF8000000) == 0x80000000) {
fn.code.emplace_back(opcode, 0xFFFF0000); // lwz, lwzu
} else if ((opcode & 0xFC000000) == 0x88000000) {
fn.code.emplace_back(opcode, 0xFFFF0000); // lbz
} else if ((opcode & 0xF8000000) == 0x90000000) {
fn.code.emplace_back(opcode, 0xFFFF0000); // stw, stwu
} else if ((opcode & 0xF8000000) == 0x98000000) {
fn.code.emplace_back(opcode, 0xFFFF0000); // stb, stbu
} else if ((opcode & 0xF8000000) == 0xA0000000) {
fn.code.emplace_back(opcode, 0xFFFF0000); // lhz, lhzu
} else if ((opcode & 0xFC000000) == 0xAC000000) {
fn.code.emplace_back(opcode, 0xFFFF0000); // lhau
} else if ((opcode & 0xF8000000) == 0xB0000000) {
fn.code.emplace_back(opcode, 0xFFFF0000); // sth, sthu
} else if ((opcode & 0xF8000000) == 0xC0000000) {
fn.code.emplace_back(opcode, 0xFFFF0000); // lfs, lfsu
} else if ((opcode & 0xFC000000) == 0xC8000000) {
fn.code.emplace_back(opcode, 0xFFFF0000); // lfd
} else if ((opcode & 0xF8000000) == 0xD0000000) {
fn.code.emplace_back(opcode, 0xFFFF0000); // stfs, stfsu
} else {
fn.code.emplace_back(opcode, 0xFFFFFFFF);
}
}
}
return ret;
};
auto a_ana = disassemble_section(a, a_sec);
auto b_ana = disassemble_section(b, b_sec);
bool use_color = isatty(fileno(stream));
auto a_fn_it = a_ana.functions.cbegin();
auto b_fn_it = b_ana.functions.cbegin();
while ((a_fn_it != a_ana.functions.end()) || (b_fn_it != b_ana.functions.end())) {
if ((a_fn_it != a_ana.functions.end()) && (b_fn_it != b_ana.functions.end())) {
phosg::fwrite_fmt(stream, "FUNCTION: A:{:08X} B:{:08X}\n", a_fn_it->label->address, b_fn_it->label->address);
bool functions_identical = true;
for (size_t z = 0; z < std::max<size_t>(a_fn_it->code.size(), b_fn_it->code.size()); z++) {
uint32_t a_op = (z < a_fn_it->code.size()) ? a_fn_it->code[z].first : 0xFFFFFFFF;
uint32_t b_op = (z < b_fn_it->code.size()) ? b_fn_it->code[z].first : 0xFFFFFFFF;
uint32_t a_mask = (z < a_fn_it->code.size()) ? a_fn_it->code[z].second : 0xFFFFFFFF;
uint32_t b_mask = (z < b_fn_it->code.size()) ? b_fn_it->code[z].second : 0xFFFFFFFF;
uint32_t mask = a_mask | b_mask;
if ((a_op & mask) != (b_op & mask)) {
functions_identical = false;
}
}
if (!functions_identical) {
for (size_t z = 0; z < std::max<size_t>(a_fn_it->code.size(), b_fn_it->code.size()); z++) {
uint32_t a_op = (z < a_fn_it->code.size()) ? a_fn_it->code[z].first : 0xFFFFFFFF;
uint32_t b_op = (z < b_fn_it->code.size()) ? b_fn_it->code[z].first : 0xFFFFFFFF;
uint32_t a_mask = (z < a_fn_it->code.size()) ? a_fn_it->code[z].second : 0xFFFFFFFF;
uint32_t b_mask = (z < b_fn_it->code.size()) ? b_fn_it->code[z].second : 0xFFFFFFFF;
uint32_t mask = a_mask | b_mask;
if ((a_op & mask) == (b_op & mask)) {
phosg::fwrite_fmt(stream, " {:08X}->{:08X} {:08X} {}\n",
a_fn_it->label->address + z * 4, b_fn_it->label->address + z * 4,
a_op, ResourceDASM::PPC32Emulator::disassemble_one(a_fn_it->label->address + z * 4, a_op));
} else {
if (use_color) {
phosg::print_color_escape(stream, phosg::TerminalFormat::BOLD, phosg::TerminalFormat::FG_RED, phosg::TerminalFormat::END);
}
phosg::fwrite_fmt(stream, "- {:08X}->{:08X} {:08X} {}\n",
a_fn_it->label->address + z * 4, b_fn_it->label->address + z * 4,
a_op, ResourceDASM::PPC32Emulator::disassemble_one(a_fn_it->label->address + z * 4, a_op));
if (use_color) {
phosg::print_color_escape(stream, phosg::TerminalFormat::BOLD, phosg::TerminalFormat::FG_GREEN, phosg::TerminalFormat::END);
}
phosg::fwrite_fmt(stream, "+ {:08X}->{:08X} {:08X} {}\n",
a_fn_it->label->address + z * 4, b_fn_it->label->address + z * 4,
b_op, ResourceDASM::PPC32Emulator::disassemble_one(b_fn_it->label->address + z * 4, b_op));
if (use_color) {
phosg::print_color_escape(stream, phosg::TerminalFormat::NORMAL, phosg::TerminalFormat::END);
}
}
}
}
do {
a_fn_it++;
} while (a_fn_it != a_ana.functions.end() && a_ignore_functions.count(a_fn_it->label->address));
do {
b_fn_it++;
} while (b_fn_it != b_ana.functions.end() && b_ignore_functions.count(b_fn_it->label->address));
} else if (a_fn_it != a_ana.functions.end()) {
phosg::fwrite_fmt(stream, "FUNCTION: A:{:08X} B:(missing)\n", a_fn_it->label->address);
for (size_t z = 0; z < a_fn_it->code.size(); z++) {
phosg::fwrite_fmt(stream, " {:08X} {:08X} {}\n",
a_fn_it->label->address + z * 4, a_fn_it->code[z].first,
ResourceDASM::PPC32Emulator::disassemble_one(a_fn_it->label->address + z * 4, a_fn_it->code[z].first));
}
do {
a_fn_it++;
} while (a_fn_it != a_ana.functions.end() && a_ignore_functions.count(a_fn_it->label->address));
} else {
phosg::fwrite_fmt(stream, "FUNCTION: A:(missing) B:{:08X}\n", b_fn_it->label->address);
for (size_t z = 0; z < b_fn_it->code.size(); z++) {
phosg::fwrite_fmt(stream, " {:08X} {:08X} {}\n",
b_fn_it->label->address + z * 4, b_fn_it->code[z].first,
ResourceDASM::PPC32Emulator::disassemble_one(b_fn_it->label->address + z * 4, b_fn_it->code[z].first));
}
do {
b_fn_it++;
} while (b_fn_it != b_ana.functions.end() && b_ignore_functions.count(b_fn_it->label->address));
}
}
}
}
}
std::vector<DiffEntry> diff_xbe_files(const std::string& a_filename, const std::string& b_filename) {
ResourceDASM::XBEFile a(a_filename.c_str());
ResourceDASM::XBEFile b(b_filename.c_str());
auto a_mem = make_shared<ResourceDASM::MemoryContext>();
auto b_mem = make_shared<ResourceDASM::MemoryContext>();
auto a_mem = std::make_shared<ResourceDASM::MemoryContext>();
auto b_mem = std::make_shared<ResourceDASM::MemoryContext>();
a.load_into(a_mem);
b.load_into(b_mem);
uint32_t min_addr = 0xFFFFFFFF;
uint32_t max_addr = 0x00000000;
for (const auto& sec : a.sections) {
min_addr = min<uint32_t>(min_addr, sec.addr);
max_addr = max<uint32_t>(max_addr, sec.addr + sec.size);
min_addr = std::min<uint32_t>(min_addr, sec.addr);
max_addr = std::max<uint32_t>(max_addr, sec.addr + sec.size);
}
for (const auto& sec : b.sections) {
min_addr = min<uint32_t>(min_addr, sec.addr);
max_addr = max<uint32_t>(max_addr, sec.addr + sec.size);
min_addr = std::min<uint32_t>(min_addr, sec.addr);
max_addr = std::max<uint32_t>(max_addr, sec.addr + sec.size);
}
vector<DiffEntry> ret;
std::vector<DiffEntry> ret;
for (uint32_t addr = min_addr; addr < max_addr; addr++) {
bool a_exists = a_mem->exists(addr, 1);
bool b_exists = b_mem->exists(addr, 1);
+9
View File
@@ -3,6 +3,7 @@
#include <stdint.h>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>
@@ -13,5 +14,13 @@ struct DiffEntry {
};
void run_address_translator(const std::string& directory, const std::string& use_filename, const std::string& command);
std::vector<DiffEntry> diff_dol_files(const std::string& a_filename, const std::string& b_filename);
std::vector<DiffEntry> diff_xbe_files(const std::string& a_filename, const std::string& b_filename);
void diff_dol_files_semantic(
FILE* stream,
const std::string& a_filename,
const std::string& b_filename,
const std::unordered_set<uint32_t>& a_ignore_functions,
const std::unordered_set<uint32_t>& b_ignore_functions);
+19 -21
View File
@@ -14,9 +14,7 @@
#include "Revision.hh"
#include "Server.hh"
using namespace std;
static const unordered_map<int, const char*> explanation_for_response_code{
static const std::unordered_map<int, const char*> explanation_for_response_code{
{100, "Continue"},
{101, "Switching Protocols"},
{102, "Processing"},
@@ -92,7 +90,7 @@ const std::string* HTTPRequest::get_header(const std::string& name) const {
if (its.first == its.second) {
return nullptr;
}
const string* ret = &its.first->second;
const std::string* ret = &its.first->second;
its.first++;
if (its.first != its.second) {
throw std::out_of_range("Header appears multiple times: " + name);
@@ -105,7 +103,7 @@ const std::string* HTTPRequest::get_query_param(const std::string& name) const {
if (its.first == its.second) {
return nullptr;
}
const string* ret = &its.first->second;
const std::string* ret = &its.first->second;
its.first++;
if (its.first != its.second) {
throw std::out_of_range("Query parameter appears multiple times: " + name);
@@ -113,7 +111,7 @@ const std::string* HTTPRequest::get_query_param(const std::string& name) const {
return ret;
}
static void url_decode_inplace(string& s) {
static void url_decode_inplace(std::string& s) {
size_t write_offset = 0, read_offset = 0;
for (; read_offset < s.size(); write_offset++) {
if ((s[read_offset] == '%') && (read_offset < s.size() - 2)) {
@@ -139,7 +137,7 @@ asio::awaitable<HTTPRequest> HTTPClient::recv_http_request(size_t max_line_size,
std::string request_line = co_await this->r.read_line("\r\n", max_line_size);
auto line_tokens = phosg::split(request_line, ' ');
if (line_tokens.size() != 3) {
throw runtime_error("invalid HTTP request line");
throw std::runtime_error("invalid HTTP request line");
}
const auto& method_token = line_tokens[0];
if (method_token == "GET") {
@@ -169,14 +167,14 @@ asio::awaitable<HTTPRequest> HTTPClient::recv_http_request(size_t max_line_size,
req.http_version = std::move(line_tokens[2]);
size_t fragment_start_offset = line_tokens[1].find('#');
if (fragment_start_offset != string::npos) {
if (fragment_start_offset != std::string::npos) {
req.fragment = line_tokens[1].substr(fragment_start_offset + 1);
line_tokens[1].resize(fragment_start_offset);
}
size_t query_start_offset = line_tokens[1].find('?');
string query;
if (query_start_offset != string::npos) {
std::string query;
if (query_start_offset != std::string::npos) {
query = line_tokens[1].substr(query_start_offset + 1);
line_tokens[1].resize(query_start_offset);
}
@@ -189,12 +187,12 @@ asio::awaitable<HTTPRequest> HTTPClient::recv_http_request(size_t max_line_size,
auto query_tokens = phosg::split(query, '&');
for (auto& token : query_tokens) {
size_t equals_pos = token.find('=');
if (equals_pos == string::npos) {
if (equals_pos == std::string::npos) {
url_decode_inplace(token);
req.query_params.emplace(std::move(token), "");
} else {
string key = token.substr(0, equals_pos);
string value = token.substr(equals_pos + 1);
std::string key = token.substr(0, equals_pos);
std::string value = token.substr(equals_pos + 1);
url_decode_inplace(key);
url_decode_inplace(value);
req.query_params.emplace(std::move(key), std::move(value));
@@ -217,11 +215,11 @@ asio::awaitable<HTTPRequest> HTTPClient::recv_http_request(size_t max_line_size,
}
} else {
size_t colon_pos = line.find(':');
if (colon_pos == string::npos) {
throw runtime_error("malformed header line");
if (colon_pos == std::string::npos) {
throw std::runtime_error("malformed header line");
}
string key = line.substr(0, colon_pos);
string value = line.substr(colon_pos + 1);
std::string key = line.substr(0, colon_pos);
std::string value = line.substr(colon_pos + 1);
phosg::strip_whitespace(key);
phosg::strip_whitespace(value);
prev_header_it = req.headers.emplace(phosg::tolower(key), std::move(value));
@@ -230,7 +228,7 @@ asio::awaitable<HTTPRequest> HTTPClient::recv_http_request(size_t max_line_size,
auto transfer_encoding_header = req.get_header("transfer-encoding");
if (transfer_encoding_header && phosg::tolower(*transfer_encoding_header) == "chunked") {
deque<string> chunks;
std::deque<std::string> chunks;
size_t total_data_bytes = 0;
for (;;) {
auto line = co_await this->r.read_line("\r\n", 0x20);
@@ -306,7 +304,7 @@ asio::awaitable<WebSocketMessage> HTTPClient::recv_websocket_message(size_t max_
}
if (payload_size > max_data_size) {
throw runtime_error("Incoming WebSocket message exceeds size limit");
throw std::runtime_error("Incoming WebSocket message exceeds size limit");
}
// Read the masking key if present
@@ -380,7 +378,7 @@ asio::awaitable<WebSocketMessage> HTTPClient::recv_websocket_message(size_t max_
}
}
throw logic_error("failed to receive websocket message");
throw std::logic_error("failed to receive websocket message");
}
asio::awaitable<void> HTTPClient::send_websocket_message(const void* data, size_t size, uint8_t opcode) {
@@ -396,7 +394,7 @@ asio::awaitable<void> HTTPClient::send_websocket_message(const void* data, size_
w.put_u8(size);
}
array<asio::const_buffer, 2> bufs = {asio::const_buffer(w.data(), w.size()), asio::const_buffer(data, size)};
std::array<asio::const_buffer, 2> bufs = {asio::const_buffer(w.data(), w.size()), asio::const_buffer(data, size)};
co_await asio::async_write(this->r.get_socket(), bufs, asio::use_awaitable);
}
+19 -21
View File
@@ -7,15 +7,12 @@
#include <phosg/Strings.hh>
#include <string>
using namespace std;
AsyncEvent::AsyncEvent(asio::any_io_executor ex)
: executor(ex), is_set(false) {}
AsyncEvent::AsyncEvent(asio::any_io_executor ex) : executor(ex), is_set(false) {}
void AsyncEvent::set() {
std::vector<std::unique_ptr<asio::detail::awaitable_handler<asio::any_io_executor>>> waiters_to_resume;
{
lock_guard g(this->lock);
std::lock_guard g(this->lock);
this->is_set = true;
this->waiters.swap(waiters_to_resume);
}
@@ -28,7 +25,7 @@ void AsyncEvent::set() {
}
void AsyncEvent::clear() {
lock_guard g(this->lock);
std::lock_guard g(this->lock);
this->is_set = false;
}
@@ -36,11 +33,12 @@ asio::awaitable<void> AsyncEvent::wait() {
auto token = asio::use_awaitable_t<>{};
co_await asio::async_initiate<asio::use_awaitable_t<>, void()>(
[this](auto&& handler) -> void {
lock_guard g(this->lock);
std::lock_guard g(this->lock);
if (this->is_set) {
handler();
} else {
this->waiters.emplace_back(make_unique<asio::detail::awaitable_handler<asio::any_io_executor>>(std::move(handler)));
this->waiters.emplace_back(
std::make_unique<asio::detail::awaitable_handler<asio::any_io_executor>>(std::move(handler)));
}
},
token);
@@ -49,17 +47,17 @@ asio::awaitable<void> AsyncEvent::wait() {
AsyncSocketReader::AsyncSocketReader(asio::ip::tcp::socket&& sock)
: sock(std::move(sock)) {}
asio::awaitable<string> AsyncSocketReader::read_line(const char* delimiter, size_t max_length) {
asio::awaitable<std::string> AsyncSocketReader::read_line(const char* delimiter, size_t max_length) {
size_t delimiter_size = strlen(delimiter);
if (delimiter_size == 0) {
throw logic_error("delimiter is empty");
throw std::logic_error("delimiter is empty");
}
size_t delimiter_backup_bytes = delimiter_size - 1;
size_t delimiter_pos = this->pending_data.find(delimiter);
while ((delimiter_pos == string::npos) && (!max_length || (this->pending_data.size() < max_length))) {
while ((delimiter_pos == std::string::npos) && (!max_length || (this->pending_data.size() < max_length))) {
size_t pre_size = this->pending_data.size();
this->pending_data.resize(min(max_length, this->pending_data.size() + 0x400));
this->pending_data.resize(std::min(max_length, this->pending_data.size() + 0x400));
auto buf = asio::buffer(this->pending_data.data() + pre_size, this->pending_data.size() - pre_size);
size_t bytes_read = co_await this->sock.async_read_some(buf, asio::use_awaitable);
@@ -69,18 +67,18 @@ asio::awaitable<string> AsyncSocketReader::read_line(const char* delimiter, size
(delimiter_backup_bytes > pre_size) ? 0 : (pre_size - delimiter_backup_bytes));
}
if (delimiter_pos == string::npos) {
throw runtime_error("line exceeds max length");
if (delimiter_pos == std::string::npos) {
throw std::runtime_error("line exceeds max length");
}
// TODO: It's not great that we copy the data here. There's probably a more idiomatic and efficient way to do this.
string ret = this->pending_data.substr(0, delimiter_pos);
std::string ret = this->pending_data.substr(0, delimiter_pos);
this->pending_data = this->pending_data.substr(delimiter_pos + delimiter_size);
co_return ret;
}
asio::awaitable<string> AsyncSocketReader::read_data(size_t size) {
string ret;
asio::awaitable<std::string> AsyncSocketReader::read_data(size_t size) {
std::string ret;
if (this->pending_data.size() == size) {
this->pending_data.swap(ret);
} else if (this->pending_data.size() > size) {
@@ -111,7 +109,7 @@ asio::awaitable<void> AsyncSocketReader::read_data_into(void* data, size_t size)
}
}
void AsyncWriteCollector::add(string&& data) {
void AsyncWriteCollector::add(std::string&& data) {
const auto& item = this->owned_data.emplace_back(std::move(data));
bufs.emplace_back(asio::buffer(item.data(), item.size()));
}
@@ -121,14 +119,14 @@ void AsyncWriteCollector::add_reference(const void* data, size_t size) {
}
asio::awaitable<void> AsyncWriteCollector::write(asio::ip::tcp::socket& sock) {
deque<string> local_owned_data;
std::deque<std::string> local_owned_data;
local_owned_data.swap(this->owned_data);
vector<asio::const_buffer> local_bufs;
std::vector<asio::const_buffer> local_bufs;
local_bufs.swap(this->bufs);
co_await asio::async_write(sock, local_bufs, asio::use_awaitable);
}
asio::awaitable<void> async_sleep(chrono::steady_clock::duration duration) {
asio::awaitable<void> async_sleep(std::chrono::steady_clock::duration duration) {
asio::steady_timer timer(co_await asio::this_coro::executor, duration);
co_await timer.async_wait(asio::use_awaitable);
}
+1 -1
View File
@@ -259,7 +259,7 @@ asio::awaitable<std::invoke_result_t<FnT, ArgTs...>> call_on_thread_pool(asio::t
using ReturnT = std::invoke_result_t<FnT, ArgTs...>;
auto bound = std::bind(std::forward<FnT>(f), std::forward<ArgTs>(args)...);
// We have to use a shared_ptr here in case call_on_thread_pool is canceled (in that case, the posted callback will
// We have to use a std::shared_ptr here in case call_on_thread_pool is canceled (in that case, the posted callback will
// try to use promise after the call_on_thread_pool coroutine has been destroyed)
auto promise = std::make_shared<AsyncPromise<ReturnT>>();
asio::post(pool, [bound = std::move(bound), promise]() mutable {
+18 -21
View File
@@ -7,8 +7,6 @@
#include "Text.hh"
#include "Types.hh"
using namespace std;
template <bool BE>
struct BMLHeaderT {
parray<uint8_t, 0x04> unknown_a1;
@@ -42,13 +40,13 @@ void BMLArchive::load_t() {
const auto& entry = r.get<BMLHeaderEntryT<BE>>();
if (offset + entry.compressed_size > this->data->size()) {
throw runtime_error("BML data entry extends beyond end of data");
throw std::runtime_error("BML data entry extends beyond end of data");
}
size_t data_offset = offset;
offset = (offset + entry.compressed_size + 0x1F) & (~0x1F);
if (offset + entry.compressed_gvm_size > this->data->size()) {
throw runtime_error("BML GVM entry extends beyond end of data");
throw std::runtime_error("BML GVM entry extends beyond end of data");
}
size_t gvm_offset = offset;
offset = (offset + entry.compressed_gvm_size + 0x1F) & (~0x1F);
@@ -57,8 +55,7 @@ void BMLArchive::load_t() {
}
}
BMLArchive::BMLArchive(shared_ptr<const string> data, bool big_endian)
: data(data) {
BMLArchive::BMLArchive(std::shared_ptr<const std::string> data, bool big_endian) : data(data) {
if (big_endian) {
this->load_t<true>();
} else {
@@ -66,42 +63,42 @@ BMLArchive::BMLArchive(shared_ptr<const string> data, bool big_endian)
}
}
const unordered_map<string, BMLArchive::Entry> BMLArchive::all_entries() const {
const std::unordered_map<std::string, BMLArchive::Entry> BMLArchive::all_entries() const {
return this->entries;
}
pair<const void*, size_t> BMLArchive::get(const std::string& name) const {
std::pair<const void*, size_t> BMLArchive::get(const std::string& name) const {
try {
const auto& entry = this->entries.at(name);
return make_pair(this->data->data() + entry.offset, entry.size);
} catch (const out_of_range&) {
throw out_of_range("BML does not contain file: " + name);
return std::make_pair(this->data->data() + entry.offset, entry.size);
} catch (const std::out_of_range&) {
throw std::out_of_range("BML does not contain file: " + name);
}
}
pair<const void*, size_t> BMLArchive::get_gvm(const std::string& name) const {
std::pair<const void*, size_t> BMLArchive::get_gvm(const std::string& name) const {
try {
const auto& entry = this->entries.at(name);
return make_pair(this->data->data() + entry.gvm_offset, entry.gvm_size);
} catch (const out_of_range&) {
throw out_of_range("BML does not contain file: " + name);
return std::make_pair(this->data->data() + entry.gvm_offset, entry.gvm_size);
} catch (const std::out_of_range&) {
throw std::out_of_range("BML does not contain file: " + name);
}
}
string BMLArchive::get_copy(const string& name) const {
std::string BMLArchive::get_copy(const std::string& name) const {
try {
const auto& entry = this->entries.at(name);
return this->data->substr(entry.offset, entry.size);
} catch (const out_of_range&) {
throw out_of_range("BML does not contain file: " + name);
} catch (const std::out_of_range&) {
throw std::out_of_range("BML does not contain file: " + name);
}
}
phosg::StringReader BMLArchive::get_reader(const string& name) const {
phosg::StringReader BMLArchive::get_reader(const std::string& name) const {
try {
const auto& entry = this->entries.at(name);
return phosg::StringReader(this->data->data() + entry.offset, entry.size);
} catch (const out_of_range&) {
throw out_of_range("BML does not contain file: " + name);
} catch (const std::out_of_range&) {
throw std::out_of_range("BML does not contain file: " + name);
}
}
+232 -22
View File
@@ -7,7 +7,190 @@
#include "PSOEncryption.hh"
#include "StaticGameData.hh"
using namespace std;
BattleParamsIndex::AttackData BattleParamsIndex::AttackData::from_json(const phosg::JSON& json) {
return AttackData{
json.get_int("MinATP"),
json.get_int("MaxATP"),
json.get_int("MinATA"),
json.get_int("MaxATA"),
json.get_float("DistanceX"),
json.get_int("Angle"),
json.get_float("DistanceY"),
json.get_int("UnknownA8"),
json.get_int("UnknownA9"),
json.get_int("UnknownA10"),
json.get_int("UnknownA11"),
json.get_int("UnknownA12"),
json.get_int("UnknownA13"),
json.get_int("UnknownA14"),
json.get_int("UnknownA15"),
json.get_int("UnknownA16"),
};
}
phosg::JSON BattleParamsIndex::AttackData::json() const {
return phosg::JSON::dict({
{"MinATP", this->min_atp.load()},
{"MaxATP", this->max_atp.load()},
{"MinATA", this->min_ata.load()},
{"MaxATA", this->max_ata.load()},
{"DistanceX", this->distance_x.load()},
{"Angle", this->angle.load()},
{"DistanceY", this->distance_y.load()},
{"UnknownA8", this->unknown_a8.load()},
{"UnknownA9", this->unknown_a9.load()},
{"UnknownA10", this->unknown_a10.load()},
{"UnknownA11", this->unknown_a11.load()},
{"UnknownA12", this->unknown_a12.load()},
{"UnknownA13", this->unknown_a13.load()},
{"UnknownA14", this->unknown_a14.load()},
{"UnknownA15", this->unknown_a15.load()},
{"UnknownA16", this->unknown_a16.load()},
});
}
BattleParamsIndex::ResistData BattleParamsIndex::ResistData::from_json(const phosg::JSON& json) {
return BattleParamsIndex::ResistData{
json.get_int("EVPBonus"),
json.get_int("EFR"),
json.get_int("EIC"),
json.get_int("ETH"),
json.get_int("ELT"),
json.get_int("EDK"),
json.get_int("UnknownA6"),
json.get_int("UnknownA7"),
json.get_int("UnknownA8"),
json.get_int("UnknownA9"),
json.get_int("DFPBonus"),
};
}
phosg::JSON BattleParamsIndex::ResistData::json() const {
return phosg::JSON::dict({
{"EVPBonus", this->evp_bonus.load()},
{"EFR", this->efr.load()},
{"EIC", this->eic.load()},
{"ETH", this->eth.load()},
{"ELT", this->elt.load()},
{"EDK", this->edk.load()},
{"UnknownA6", this->unknown_a6.load()},
{"UnknownA7", this->unknown_a7.load()},
{"UnknownA8", this->unknown_a8.load()},
{"UnknownA9", this->unknown_a9.load()},
{"DFPBonus", this->dfp_bonus.load()},
});
}
BattleParamsIndex::MovementData BattleParamsIndex::MovementData::from_json(const phosg::JSON& json) {
const auto& fparams_json = json.at("FParams").as_list();
const auto& iparams_json = json.at("IParams").as_list();
return BattleParamsIndex::MovementData{
fparams_json.at(0)->as_float(),
fparams_json.at(1)->as_float(),
fparams_json.at(2)->as_float(),
fparams_json.at(3)->as_float(),
fparams_json.at(4)->as_float(),
fparams_json.at(5)->as_float(),
iparams_json.at(0)->as_float(),
iparams_json.at(1)->as_float(),
iparams_json.at(2)->as_float(),
iparams_json.at(3)->as_float(),
iparams_json.at(4)->as_float(),
iparams_json.at(5)->as_float(),
};
}
phosg::JSON BattleParamsIndex::MovementData::json() const {
auto fparams_list = phosg::JSON::list({
this->fparam1.load(),
this->fparam2.load(),
this->fparam3.load(),
this->fparam4.load(),
this->fparam5.load(),
this->fparam6.load(),
});
auto iparams_list = phosg::JSON::list({
this->iparam1.load(),
this->iparam2.load(),
this->iparam3.load(),
this->iparam4.load(),
this->iparam5.load(),
this->iparam6.load(),
});
return phosg::JSON::dict({{"FParams", std::move(fparams_list)}, {"IParams", std::move(iparams_list)}});
}
BattleParamsIndex::Table BattleParamsIndex::Table::from_json(const phosg::JSON& json) {
BattleParamsIndex::Table ret;
for (Difficulty difficulty : ALL_DIFFICULTIES_V234) {
const auto& diff_json = json.at(name_for_difficulty(difficulty));
for (size_t z = 0; z < 0x60; z++) {
const auto& entry_json = diff_json.at(z);
ret.stats[static_cast<size_t>(difficulty)][z] = PlayerStats::from_json(entry_json.at("Stats"));
ret.attack_data[static_cast<size_t>(difficulty)][z] = AttackData::from_json(entry_json.at("AttackData"));
ret.resist_data[static_cast<size_t>(difficulty)][z] = ResistData::from_json(entry_json.at("ResistData"));
ret.movement_data[static_cast<size_t>(difficulty)][z] = MovementData::from_json(entry_json.at("MovementData"));
}
}
return ret;
}
phosg::JSON BattleParamsIndex::Table::json() const {
auto ret = phosg::JSON::dict();
for (Difficulty difficulty : ALL_DIFFICULTIES_V234) {
auto diff_ret = phosg::JSON::list();
for (size_t z = 0; z < 0x60; z++) {
auto stats_json = this->stats_for_index(difficulty, z).json();
auto attack_data_json = this->attack_data_for_index(difficulty, z).json();
auto resist_data_json = this->resist_data_for_index(difficulty, z).json();
auto movement_data_json = this->movement_data_for_index(difficulty, z).json();
std::set<EnemyType> stats_names;
std::set<EnemyType> attack_data_names;
std::set<EnemyType> resist_data_names;
std::set<EnemyType> movement_data_names;
for (Episode episode : ALL_EPISODES_V4) {
for (const auto& enemy_type : enemy_types_for_battle_param_stats_index(episode, z)) {
stats_names.emplace(enemy_type);
}
for (const auto& enemy_type : enemy_types_for_battle_param_attack_data_index(episode, z)) {
attack_data_names.emplace(enemy_type);
}
for (const auto& enemy_type : enemy_types_for_battle_param_resist_data_index(episode, z)) {
resist_data_names.emplace(enemy_type);
}
for (const auto& enemy_type : enemy_types_for_battle_param_movement_data_index(episode, z)) {
movement_data_names.emplace(enemy_type);
}
}
auto stats_names_json = phosg::JSON::list();
for (EnemyType enemy_type : stats_names) {
stats_names_json.emplace_back(phosg::name_for_enum(enemy_type));
}
auto attack_data_names_json = phosg::JSON::list();
for (EnemyType enemy_type : attack_data_names) {
attack_data_names_json.emplace_back(phosg::name_for_enum(enemy_type));
}
auto resist_data_names_json = phosg::JSON::list();
for (EnemyType enemy_type : resist_data_names) {
resist_data_names_json.emplace_back(phosg::name_for_enum(enemy_type));
}
auto movement_data_names_json = phosg::JSON::list();
for (EnemyType enemy_type : movement_data_names) {
movement_data_names_json.emplace_back(phosg::name_for_enum(enemy_type));
}
stats_json.emplace("Enemies", std::move(stats_names_json));
attack_data_json.emplace("Enemies", std::move(attack_data_names_json));
resist_data_json.emplace("Enemies", std::move(resist_data_names_json));
movement_data_json.emplace("Enemies", std::move(movement_data_names_json));
diff_ret.emplace_back(phosg::JSON::dict({
{"BPIndex", z},
{"Stats", std::move(stats_json)},
{"AttackData", std::move(attack_data_json)},
{"ResistData", std::move(resist_data_json)},
{"MovementData", std::move(movement_data_json)},
}));
}
ret.emplace(name_for_difficulty(difficulty), std::move(diff_ret));
}
return ret;
}
void BattleParamsIndex::Table::print(FILE* stream, Episode episode) const {
phosg::fwrite_fmt(stream, "========== STATS\n");
@@ -17,7 +200,7 @@ void BattleParamsIndex::Table::print(FILE* stream, Episode episode) const {
for (size_t z = 0; z < 0x60; z++) {
const auto& e = this->stats[static_cast<size_t>(difficulty)][z];
phosg::fwrite_fmt(stream, " {:02X} ", z);
string names_str;
std::string names_str;
for (auto type : enemy_types_for_battle_param_stats_index(episode, z)) {
if (!names_str.empty()) {
names_str += ", ";
@@ -27,7 +210,7 @@ void BattleParamsIndex::Table::print(FILE* stream, Episode episode) const {
phosg::fwrite_fmt(stream,
"{:5} {:5} {:5} {:5} {:5} {:5} {:5} {:5} {:5} {:5} {}",
e.char_stats.atp, e.char_stats.mst, e.char_stats.evp, e.char_stats.hp, e.char_stats.dfp, e.char_stats.ata,
e.char_stats.lck, e.esp, e.experience, e.meseta, names_str);
e.char_stats.lck, e.esp, e.exp, e.meseta, names_str);
fputc('\n', stream);
}
}
@@ -76,13 +259,46 @@ void BattleParamsIndex::Table::print(FILE* stream, Episode episode) const {
}
}
BattleParamsIndex::BattleParamsIndex(
shared_ptr<const string> data_on_ep1,
shared_ptr<const string> data_on_ep2,
shared_ptr<const string> data_on_ep4,
shared_ptr<const string> data_off_ep1,
shared_ptr<const string> data_off_ep2,
shared_ptr<const string> data_off_ep4) {
phosg::JSON BattleParamsIndex::json() const {
return phosg::JSON::dict({
{"Episode1-Online", this->get_table(false, Episode::EP1).json()},
{"Episode2-Online", this->get_table(false, Episode::EP2).json()},
{"Episode4-Online", this->get_table(false, Episode::EP4).json()},
{"Episode1-Solo", this->get_table(true, Episode::EP1).json()},
{"Episode2-Solo", this->get_table(true, Episode::EP2).json()},
{"Episode4-Solo", this->get_table(true, Episode::EP4).json()},
});
}
JSONBattleParamsIndex::JSONBattleParamsIndex(const phosg::JSON& json) {
this->tables[0][0] = Table::from_json(json.at("Episode1-Online"));
this->tables[0][1] = Table::from_json(json.at("Episode2-Online"));
this->tables[0][2] = Table::from_json(json.at("Episode4-Online"));
this->tables[1][0] = Table::from_json(json.at("Episode1-Solo"));
this->tables[1][1] = Table::from_json(json.at("Episode2-Solo"));
this->tables[1][2] = Table::from_json(json.at("Episode4-Solo"));
}
const BattleParamsIndex::Table& JSONBattleParamsIndex::get_table(bool solo, Episode episode) const {
switch (episode) {
case Episode::EP1:
return this->tables[!!solo][0];
case Episode::EP2:
return this->tables[!!solo][1];
case Episode::EP4:
return this->tables[!!solo][2];
default:
throw std::invalid_argument("invalid episode");
}
}
BinaryBattleParamsIndex::BinaryBattleParamsIndex(
std::shared_ptr<const std::string> data_on_ep1,
std::shared_ptr<const std::string> data_on_ep2,
std::shared_ptr<const std::string> data_on_ep4,
std::shared_ptr<const std::string> data_off_ep1,
std::shared_ptr<const std::string> data_off_ep2,
std::shared_ptr<const std::string> data_off_ep4) {
this->files[0][0].data = data_on_ep1;
this->files[0][1].data = data_on_ep2;
this->files[0][2].data = data_on_ep4;
@@ -94,7 +310,7 @@ BattleParamsIndex::BattleParamsIndex(
for (uint8_t episode = 0; episode < 3; episode++) {
auto& file = this->files[is_solo][episode];
if (file.data->size() < sizeof(Table)) {
throw runtime_error(std::format(
throw std::runtime_error(std::format(
"battle params table size is incorrect (expected {:X} bytes, have {:X} bytes; is_solo={}, episode={})",
sizeof(Table), file.data->size(), is_solo, episode));
}
@@ -103,21 +319,15 @@ BattleParamsIndex::BattleParamsIndex(
}
}
const BattleParamsIndex::Table& BattleParamsIndex::get_table(bool solo, Episode episode) const {
uint8_t ep_index;
const BattleParamsIndex::Table& BinaryBattleParamsIndex::get_table(bool solo, Episode episode) const {
switch (episode) {
case Episode::EP1:
ep_index = 0;
break;
return *this->files[!!solo][0].table;
case Episode::EP2:
ep_index = 1;
break;
return *this->files[!!solo][1].table;
case Episode::EP4:
ep_index = 2;
break;
return *this->files[!!solo][2].table;
default:
throw invalid_argument("invalid episode");
throw std::invalid_argument("invalid episode");
}
return *this->files[!!solo][ep_index].table;
}
+38 -10
View File
@@ -35,7 +35,8 @@ public:
/* 24 */ le_uint32_t unknown_a14;
/* 28 */ le_uint32_t unknown_a15;
/* 2C */ le_uint32_t unknown_a16;
/* 30 */
static AttackData from_json(const phosg::JSON& json);
phosg::JSON json() const;
} __packed_ws__(AttackData, 0x30);
struct ResistData {
@@ -50,7 +51,8 @@ public:
/* 14 */ le_uint32_t unknown_a8;
/* 18 */ le_uint32_t unknown_a9;
/* 1C */ le_int32_t dfp_bonus;
/* 20 */
static ResistData from_json(const phosg::JSON& json);
phosg::JSON json() const;
} __packed_ws__(ResistData, 0x20);
struct MovementData {
@@ -66,7 +68,8 @@ public:
/* 24 */ le_uint32_t iparam4;
/* 28 */ le_uint32_t iparam5;
/* 2C */ le_uint32_t iparam6;
/* 30 */
static MovementData from_json(const phosg::JSON& json);
phosg::JSON json() const;
} __packed_ws__(MovementData, 0x30);
struct Table {
@@ -76,23 +79,48 @@ public:
/* AE00 */ parray<parray<MovementData, 0x60>, 4> movement_data; // [difficulty][bp_index]
/* F600 */
const PlayerStats& stats_for_index(Difficulty difficulty, uint8_t index) const {
inline const PlayerStats& stats_for_index(Difficulty difficulty, uint8_t index) const {
return this->stats.at(static_cast<size_t>(difficulty)).at(index);
}
const AttackData& attack_data_for_index(Difficulty difficulty, uint8_t index) const {
inline const AttackData& attack_data_for_index(Difficulty difficulty, uint8_t index) const {
return this->attack_data.at(static_cast<size_t>(difficulty)).at(index);
}
const ResistData& resist_data_for_index(Difficulty difficulty, uint8_t index) const {
inline const ResistData& resist_data_for_index(Difficulty difficulty, uint8_t index) const {
return this->resist_data.at(static_cast<size_t>(difficulty)).at(index);
}
const MovementData& movement_data_for_index(Difficulty difficulty, uint8_t index) const {
inline const MovementData& movement_data_for_index(Difficulty difficulty, uint8_t index) const {
return this->movement_data.at(static_cast<size_t>(difficulty)).at(index);
}
static Table from_json(const phosg::JSON& json);
phosg::JSON json() const;
void print(FILE* stream, Episode episode) const;
} __packed_ws__(Table, 0xF600);
BattleParamsIndex(
virtual ~BattleParamsIndex() = default;
virtual const Table& get_table(bool solo, Episode episode) const = 0;
phosg::JSON json() const;
protected:
BattleParamsIndex() = default;
};
class JSONBattleParamsIndex : public BattleParamsIndex {
public:
explicit JSONBattleParamsIndex(const phosg::JSON& json);
virtual const Table& get_table(bool solo, Episode episode) const;
protected:
// Indexed as [online/offline][episode]
std::array<std::array<Table, 3>, 2> tables;
};
class BinaryBattleParamsIndex : public BattleParamsIndex {
public:
BinaryBattleParamsIndex(
std::shared_ptr<const std::string> data_on_ep1, // BattleParamEntry_on.dat
std::shared_ptr<const std::string> data_on_ep2, // BattleParamEntry_lab_on.dat
std::shared_ptr<const std::string> data_on_ep4, // BattleParamEntry_ep4_on.dat
@@ -100,9 +128,9 @@ public:
std::shared_ptr<const std::string> data_off_ep2, // BattleParamEntry_lab.dat
std::shared_ptr<const std::string> data_off_ep4); // BattleParamEntry_ep4.dat
const Table& get_table(bool solo, Episode episode) const;
virtual const Table& get_table(bool solo, Episode episode) const;
private:
protected:
struct File {
std::shared_ptr<const std::string> data;
const Table* table = nullptr;
+46
View File
@@ -0,0 +1,46 @@
#pragma once
#include <array>
#include <cstdint>
struct BrutalPeepsTierDefinition {
int8_t tier;
uint32_t required_level;
float exp_multiplier;
float enemy_hp_multiplier;
double rare_drop_multiplier;
};
static constexpr std::array<BrutalPeepsTierDefinition, 11> BRUTAL_PEEPS_TIERS = {{
{1, 100, 1.10f, 1.10f, 1.001},
{2, 110, 1.15f, 1.15f, 1.002},
{3, 120, 1.20f, 1.20f, 1.005},
{4, 130, 1.30f, 1.30f, 1.006},
{5, 140, 1.40f, 1.40f, 1.008},
{6, 150, 1.50f, 1.50f, 1.009},
{7, 160, 1.75f, 1.75f, 1.010},
{8, 170, 2.00f, 2.00f, 1.020},
{9, 180, 2.50f, 2.50f, 1.030},
{10, 190, 3.00f, 3.00f, 1.040},
{11, 200, 1.00f, 4.00f, 1.050},
}};
static inline const BrutalPeepsTierDefinition* brutal_peeps_tier_definition(int64_t tier) {
for (const auto& def : BRUTAL_PEEPS_TIERS) {
if (def.tier == tier) {
return &def;
}
}
return nullptr;
}
static inline int8_t max_brutal_peeps_tier_for_level(uint32_t level) {
int8_t ret = -1;
for (const auto& def : BRUTAL_PEEPS_TIERS) {
if (level >= def.required_level) {
ret = def.tier;
}
}
return ret;
}
+77 -36
View File
@@ -7,25 +7,28 @@
#include <phosg/Network.hh>
#include <phosg/Time.hh>
#include "CommandCensorData.hh"
#include "Loggers.hh"
#include "StaticGameData.hh"
#include "Version.hh"
using namespace std;
extern bool use_terminal_colors;
Channel::Channel(
Version version,
Language language,
const string& name,
const std::string& name,
phosg::TerminalFormat terminal_send_color,
phosg::TerminalFormat terminal_recv_color)
phosg::TerminalFormat terminal_recv_color,
bool censor_received_credentials,
bool censor_sent_credentials)
: version(version),
language(language),
name(name),
terminal_send_color(terminal_send_color),
terminal_recv_color(terminal_recv_color) {
terminal_recv_color(terminal_recv_color),
censor_received_credentials(censor_received_credentials),
censor_sent_credentials(censor_sent_credentials) {
}
void Channel::send(uint16_t cmd, uint32_t flag, bool silent) {
@@ -44,7 +47,7 @@ void Channel::send(
size += b.second;
}
string send_data;
std::string send_data;
size_t logical_size;
size_t send_data_size = 0;
switch (this->version) {
@@ -108,12 +111,12 @@ void Channel::send(
}
default:
throw logic_error("unimplemented game version in send_command");
throw std::logic_error("unimplemented game version in send_command");
}
// All versions of PSO I've seen (so far) have a receive buffer 0x7C00 bytes in size
if (send_data_size > 0x7C00) {
throw runtime_error("outbound command too large");
throw std::runtime_error("outbound command too large");
}
send_data.reserve(send_data_size);
@@ -132,7 +135,20 @@ void Channel::send(
command_data_log.info_f("Sending to {} (version={} command={:02X} flag={:02X})",
this->name, phosg::name_for_enum(version), cmd, flag);
}
phosg::print_data(stderr, send_data.data(), logical_size, 0, nullptr, phosg::PrintDataFlags::PRINT_ASCII | phosg::PrintDataFlags::DISABLE_COLOR | phosg::PrintDataFlags::OFFSET_16_BITS);
struct iovec iov{.iov_base = send_data.data(), .iov_len = send_data.size()};
if (this->censor_sent_credentials) {
auto [censor_data, censor_size] = censor_data_for_client_command(this->version, cmd);
struct iovec censor_iovs[2] = {
// const_casts are OK here because print_data does not modify the buffers
{.iov_base = const_cast<char*>("\0\0\0\0\0\0\0\0"), .iov_len = static_cast<size_t>(is_v4(this->version) ? 8 : 4)},
{.iov_base = const_cast<void*>(censor_data), .iov_len = censor_size}};
phosg::print_data(stderr, &iov, 1, 0, nullptr, 0, censor_iovs, 2, phosg::FormatDataFlags::PRINT_ASCII | phosg::FormatDataFlags::OFFSET_16_BITS);
} else {
phosg::print_data(stderr, &iov, 1, 0, phosg::FormatDataFlags::PRINT_ASCII | phosg::FormatDataFlags::OFFSET_16_BITS);
}
if (use_terminal_colors && this->terminal_send_color != phosg::TerminalFormat::NORMAL) {
print_color_escape(stderr, phosg::TerminalFormat::NORMAL, phosg::TerminalFormat::END);
}
@@ -146,10 +162,10 @@ void Channel::send(
}
void Channel::send(uint16_t cmd, uint32_t flag, const void* data, size_t size, bool silent) {
this->send(cmd, flag, {make_pair(data, size)}, silent);
this->send(cmd, flag, {std::make_pair(data, size)}, silent);
}
void Channel::send(uint16_t cmd, uint32_t flag, const string& data, bool silent) {
void Channel::send(uint16_t cmd, uint32_t flag, const std::string& data, bool silent) {
this->send(cmd, flag, data.data(), data.size(), silent);
}
@@ -164,7 +180,7 @@ void Channel::send(const void* data, size_t size, bool silent) {
silent);
}
void Channel::send(const string& data, bool silent) {
void Channel::send(const std::string& data, bool silent) {
this->send(data.data(), data.size(), silent);
}
@@ -178,7 +194,7 @@ asio::awaitable<Channel::Message> Channel::recv() {
size_t command_logical_size = header.size(version);
if (command_logical_size < header_size) {
throw runtime_error("header size field is smaller than header");
throw std::runtime_error("header size field is smaller than header");
}
// If encryption is enabled, BB pads commands to 8-byte boundaries, and this is not reflected in the size field. This
@@ -187,7 +203,7 @@ asio::awaitable<Channel::Message> Channel::recv() {
? ((command_logical_size + 7) & ~7)
: command_logical_size;
string command_data(command_physical_size - header_size, '\0');
std::string command_data(command_physical_size - header_size, '\0');
co_await this->recv_raw(command_data.data(), command_data.size());
if (this->crypt_in.get()) {
@@ -201,6 +217,7 @@ asio::awaitable<Channel::Message> Channel::recv() {
}
command_data.resize(command_logical_size - header_size);
uint16_t command = header.command(this->version);
if (command_data_log.should_log(phosg::LogLevel::L_INFO) && (this->terminal_recv_color != phosg::TerminalFormat::END)) {
if (use_terminal_colors && this->terminal_recv_color != phosg::TerminalFormat::NORMAL) {
print_color_escape(stderr, this->terminal_recv_color, phosg::TerminalFormat::BOLD, phosg::TerminalFormat::END);
@@ -221,10 +238,20 @@ asio::awaitable<Channel::Message> Channel::recv() {
header.flag(this->version));
}
vector<struct iovec> iovs;
iovs.emplace_back(iovec{.iov_base = &header, .iov_len = header_size});
iovs.emplace_back(iovec{.iov_base = command_data.data(), .iov_len = command_data.size()});
phosg::print_data(stderr, iovs, 0, nullptr, phosg::PrintDataFlags::PRINT_ASCII | phosg::PrintDataFlags::DISABLE_COLOR | phosg::PrintDataFlags::OFFSET_16_BITS);
struct iovec iovs[2] = {
{.iov_base = &header, .iov_len = header_size},
{.iov_base = command_data.data(), .iov_len = command_data.size()}};
if (this->censor_received_credentials) {
auto [censor_data, censor_size] = censor_data_for_client_command(this->version, command);
struct iovec censor_iovs[2] = {
// const_casts are OK here because print_data does not modify the buffers
{.iov_base = const_cast<char*>("\0\0\0\0\0\0\0\0"), .iov_len = header_size},
{.iov_base = const_cast<void*>(censor_data), .iov_len = censor_size}};
phosg::print_data(stderr, iovs, 2, 0, nullptr, 0, censor_iovs, 2, phosg::FormatDataFlags::PRINT_ASCII | phosg::FormatDataFlags::OFFSET_16_BITS);
} else {
phosg::print_data(stderr, iovs, 2, 0, phosg::FormatDataFlags::PRINT_ASCII | phosg::FormatDataFlags::OFFSET_16_BITS);
}
if (use_terminal_colors && this->terminal_recv_color != phosg::TerminalFormat::NORMAL) {
phosg::print_color_escape(stderr, phosg::TerminalFormat::NORMAL, phosg::TerminalFormat::END);
@@ -232,22 +259,32 @@ asio::awaitable<Channel::Message> Channel::recv() {
}
co_return Message{
.command = header.command(this->version),
.command = command,
.flag = header.flag(this->version),
.data = std::move(command_data),
};
}
shared_ptr<SocketChannel> SocketChannel::create(
std::shared_ptr<SocketChannel> SocketChannel::create(
std::shared_ptr<asio::io_context> io_context,
std::unique_ptr<asio::ip::tcp::socket>&& sock,
Version version,
Language language,
const string& name,
const std::string& name,
phosg::TerminalFormat terminal_send_color,
phosg::TerminalFormat terminal_recv_color) {
shared_ptr<SocketChannel> ret(new SocketChannel(
io_context, std::move(sock), version, language, name, terminal_send_color, terminal_recv_color));
phosg::TerminalFormat terminal_recv_color,
bool censor_received_credentials,
bool censor_sent_credentials) {
std::shared_ptr<SocketChannel> ret(new SocketChannel(
io_context,
std::move(sock),
version,
language,
name,
terminal_send_color,
terminal_recv_color,
censor_received_credentials,
censor_sent_credentials));
asio::co_spawn(*io_context, ret->send_task(), asio::detached);
return ret;
}
@@ -257,10 +294,12 @@ SocketChannel::SocketChannel(
std::unique_ptr<asio::ip::tcp::socket>&& sock,
Version version,
Language language,
const string& name,
const std::string& name,
phosg::TerminalFormat terminal_send_color,
phosg::TerminalFormat terminal_recv_color)
: Channel(version, language, name, terminal_send_color, terminal_recv_color),
phosg::TerminalFormat terminal_recv_color,
bool censor_received_credentials,
bool censor_sent_credentials)
: Channel(version, language, name, terminal_send_color, terminal_recv_color, censor_received_credentials, censor_sent_credentials),
sock(std::move(sock)),
local_addr(this->sock->local_endpoint()),
remote_addr(this->sock->remote_endpoint()),
@@ -279,7 +318,7 @@ void SocketChannel::disconnect() {
this->send_buffer_nonempty_signal.set();
}
void SocketChannel::send_raw(string&& data) {
void SocketChannel::send_raw(std::string&& data) {
if (this->sock && !this->should_disconnect) {
this->outbound_data.emplace_back(std::move(data));
this->send_buffer_nonempty_signal.set();
@@ -288,7 +327,7 @@ void SocketChannel::send_raw(string&& data) {
asio::awaitable<void> SocketChannel::recv_raw(void* data, size_t size) {
if (!this->sock || this->should_disconnect) {
throw runtime_error("Cannot receive on closed channel");
throw std::runtime_error("Cannot receive on closed channel");
}
co_await asio::async_read(*this->sock, asio::buffer(data, size), asio::use_awaitable);
}
@@ -298,11 +337,11 @@ asio::awaitable<void> SocketChannel::send_task() {
auto this_sh = this->shared_from_this();
while (this->sock->is_open()) {
deque<string> to_send;
std::deque<std::string> to_send;
to_send.swap(this->outbound_data);
if (!to_send.empty()) {
vector<asio::const_buffer> bufs;
std::vector<asio::const_buffer> bufs;
bufs.reserve(to_send.size());
for (const auto& it : to_send) {
bufs.emplace_back(asio::buffer(it.data(), it.size()));
@@ -327,13 +366,15 @@ PeerChannel::PeerChannel(
Language language,
const std::string& name,
phosg::TerminalFormat terminal_send_color,
phosg::TerminalFormat terminal_recv_color)
: Channel(version, language, name, terminal_send_color, terminal_recv_color),
phosg::TerminalFormat terminal_recv_color,
bool censor_received_credentials,
bool censor_sent_credentials)
: Channel(version, language, name, terminal_send_color, terminal_recv_color, censor_received_credentials, censor_sent_credentials),
send_buffer_nonempty_signal(io_context->get_executor()) {}
void PeerChannel::link_peers(std::shared_ptr<PeerChannel> peer1, std::shared_ptr<PeerChannel> peer2) {
if (peer1->connected() || peer2->connected()) {
throw logic_error("Cannot link already-connected peer channels");
throw std::logic_error("Cannot link already-connected peer channels");
}
peer1->peer = peer2;
peer2->peer = peer1;
@@ -357,7 +398,7 @@ void PeerChannel::disconnect() {
this->send_buffer_nonempty_signal.set();
}
void PeerChannel::send_raw(string&& data) {
void PeerChannel::send_raw(std::string&& data) {
auto peer = this->peer.lock();
if (peer) {
peer->inbound_data.emplace_back(std::move(data));
@@ -385,7 +426,7 @@ asio::awaitable<void> PeerChannel::recv_raw(void* data, size_t size) {
this->inbound_data.pop_front();
}
} else if (!this->peer.lock()) {
throw runtime_error("Channel peer has disconnected");
throw std::runtime_error("Channel peer has disconnected");
}
}
}
+19 -9
View File
@@ -19,6 +19,8 @@ public:
std::string name;
phosg::TerminalFormat terminal_send_color;
phosg::TerminalFormat terminal_recv_color;
bool censor_received_credentials;
bool censor_sent_credentials;
struct Message {
uint16_t command;
@@ -87,8 +89,10 @@ protected:
Version version,
Language language,
const std::string& name,
phosg::TerminalFormat terminal_send_color = phosg::TerminalFormat::END,
phosg::TerminalFormat terminal_recv_color = phosg::TerminalFormat::END);
phosg::TerminalFormat terminal_send_color,
phosg::TerminalFormat terminal_recv_color,
bool censor_received_credentials,
bool censor_sent_credentials);
Channel(const Channel& other) = delete;
Channel(Channel&& other) = delete;
Channel& operator=(const Channel& other) = delete;
@@ -114,9 +118,11 @@ public:
std::unique_ptr<asio::ip::tcp::socket>&& sock,
Version version,
Language language,
const std::string& name = "",
phosg::TerminalFormat terminal_send_color = phosg::TerminalFormat::END,
phosg::TerminalFormat terminal_recv_color = phosg::TerminalFormat::END);
const std::string& name,
phosg::TerminalFormat terminal_send_color,
phosg::TerminalFormat terminal_recv_color,
bool censor_received_credentials,
bool censor_sent_credentials);
virtual std::string default_name() const;
@@ -134,7 +140,9 @@ private:
Language language,
const std::string& name,
phosg::TerminalFormat terminal_send_color,
phosg::TerminalFormat terminal_recv_color);
phosg::TerminalFormat terminal_recv_color,
bool censor_received_credentials,
bool censor_sent_credentials);
std::deque<std::string> outbound_data;
bool should_disconnect = false;
@@ -152,9 +160,11 @@ public:
std::shared_ptr<asio::io_context> io_context,
Version version,
Language language,
const std::string& name = "",
phosg::TerminalFormat terminal_send_color = phosg::TerminalFormat::END,
phosg::TerminalFormat terminal_recv_color = phosg::TerminalFormat::END);
const std::string& name,
phosg::TerminalFormat terminal_send_color,
phosg::TerminalFormat terminal_recv_color,
bool censor_received_credentials,
bool censor_sent_credentials);
static void link_peers(std::shared_ptr<PeerChannel> peer1, std::shared_ptr<PeerChannel> peer2);
+267 -292
View File
File diff suppressed because it is too large Load Diff
+10 -12
View File
@@ -5,9 +5,7 @@
#include "Client.hh"
using namespace std;
const vector<ChoiceSearchCategory> CHOICE_SEARCH_CATEGORIES({
const std::vector<ChoiceSearchCategory> CHOICE_SEARCH_CATEGORIES{
ChoiceSearchCategory{
.id = 0x0001,
.name = "Level",
@@ -24,7 +22,7 @@ const vector<ChoiceSearchCategory> CHOICE_SEARCH_CATEGORIES({
{0x0009, "Level 121-160"},
{0x000A, "Level 161-200"},
},
.client_matches = +[](shared_ptr<Client> searcher_c, shared_ptr<Client> target_c, uint16_t choice_id) -> bool {
.client_matches = +[](std::shared_ptr<Client> searcher_c, std::shared_ptr<Client> target_c, uint16_t choice_id) -> bool {
if (choice_id == 0x0000) {
return true;
}
@@ -75,18 +73,18 @@ const vector<ChoiceSearchCategory> CHOICE_SEARCH_CATEGORIES({
{0x0008, "FOnewm"},
{0x0009, "FOnewearl"},
},
.client_matches = +[](shared_ptr<Client>, shared_ptr<Client> target_c, uint16_t choice_id) -> bool {
.client_matches = +[](std::shared_ptr<Client>, std::shared_ptr<Client> target_c, uint16_t choice_id) -> bool {
switch (choice_id) {
case 0x0000:
return true;
case 0x0010:
return target_c->character_file()->disp.visual.class_flags & 0x20;
return target_c->character_file()->disp.visual.sh.class_flags & 0x20;
case 0x0011:
return target_c->character_file()->disp.visual.class_flags & 0x40;
return target_c->character_file()->disp.visual.sh.class_flags & 0x40;
case 0x0012:
return target_c->character_file()->disp.visual.class_flags & 0x80;
return target_c->character_file()->disp.visual.sh.class_flags & 0x80;
default:
return ((choice_id - 1) == target_c->character_file()->disp.visual.char_class);
return ((choice_id - 1) == target_c->character_file()->disp.visual.sh.char_class);
}
},
},
@@ -102,7 +100,7 @@ const vector<ChoiceSearchCategory> CHOICE_SEARCH_CATEGORIES({
{0x0005, "GC Episode 3"},
{0x0006, "BB"},
},
.client_matches = +[](shared_ptr<Client>, shared_ptr<Client> target_c, uint16_t choice_id) -> bool {
.client_matches = +[](std::shared_ptr<Client>, std::shared_ptr<Client> target_c, uint16_t choice_id) -> bool {
if (choice_id == 0x0000) {
return true;
}
@@ -142,9 +140,9 @@ const vector<ChoiceSearchCategory> CHOICE_SEARCH_CATEGORIES({
{0x0005, "Battle"},
{0x0006, "Challenge"},
},
.client_matches = +[](shared_ptr<Client>, shared_ptr<Client> target_c, uint16_t choice_id) -> bool {
.client_matches = +[](std::shared_ptr<Client>, std::shared_ptr<Client> target_c, uint16_t choice_id) -> bool {
uint16_t target_choice_id = target_c->character_file()->choice_search_config.get_setting(0x0204);
return (choice_id == 0) || (target_choice_id == 0) || (choice_id == target_choice_id);
},
},
});
};
+275 -161
View File
@@ -1,4 +1,5 @@
#include "Client.hh"
#include "TeamSync.hh"
#include <errno.h>
#include <string.h>
@@ -15,12 +16,48 @@
#include "SendCommands.hh"
#include "Server.hh"
#include "Version.hh"
using namespace std;
#include "AccountSync.hh"
const uint64_t CLIENT_CONFIG_MAGIC = 0x8399AC32;
static atomic<uint64_t> next_id(1);
static std::atomic<uint64_t> next_client_id(1);
static uint8_t account_client_source_for_version(Version v) {
switch (v) {
case Version::DC_NTE:
case Version::DC_11_2000:
case Version::DC_V1:
case Version::DC_V2:
return 1;
case Version::PC_PATCH:
case Version::PC_NTE:
case Version::PC_V2:
return 2;
case Version::GC_NTE:
case Version::GC_V3:
case Version::GC_EP3_NTE:
case Version::GC_EP3:
return 3;
case Version::XB_V3:
return 4;
case Version::BB_PATCH:
case Version::BB_V4:
return 5;
default:
throw std::logic_error("invalid game version for account client source");
}
}
static uint64_t account_client_source_key(uint32_t account_id, Version v) {
return (static_cast<uint64_t>(account_id) << 8) | account_client_source_for_version(v);
}
void Client::set_flags_for_version(Version version, int64_t sub_version) {
this->set_flag(Flag::PROXY_CHAT_COMMANDS_ENABLED);
@@ -82,7 +119,7 @@ void Client::set_flags_for_version(Version version, int64_t sub_version) {
this->set_flag(Flag::SEND_FUNCTION_CALL_NO_CACHE_PATCH);
break;
default:
throw logic_error("invalid game version");
throw std::logic_error("invalid game version");
}
break;
@@ -151,7 +188,7 @@ void Client::set_flags_for_version(Version version, int64_t sub_version) {
this->set_flag(Flag::CAN_RECEIVE_ENABLE_B2_QUEST);
break;
default:
throw runtime_error(std::format("unknown sub_version {:X}", sub_version));
throw std::runtime_error(std::format("unknown sub_version {:X}", sub_version));
}
}
@@ -176,11 +213,11 @@ void Client::set_drop_notification_mode(ItemDropNotificationMode new_mode) {
}
Client::Client(
shared_ptr<GameServer> server,
shared_ptr<Channel> channel,
std::shared_ptr<GameServer> server,
std::shared_ptr<Channel> channel,
ServerBehavior server_behavior)
: server(server),
id(next_id++),
id(next_client_id++),
log(std::format("[C-{:X}] ", this->id), client_log.min_level),
channel(channel),
server_behavior(server_behavior),
@@ -193,7 +230,7 @@ Client::Client(
// Don't print data sent to patch clients to the logs. The patch server protocol is fully understood and data logs
// for patch clients are generally more annoying than helpful at this point.
auto s = server->get_state();
if (is_patch(this->version()) && s->hide_download_commands) {
if (is_patch(this->version()) && s->data->hide_download_commands) {
this->channel->terminal_recv_color = phosg::TerminalFormat::END;
this->channel->terminal_send_color = phosg::TerminalFormat::END;
} else {
@@ -202,7 +239,7 @@ Client::Client(
}
this->set_flags_for_version(this->version(), -1);
if (is_v1_or_v2(this->version()) ? s->default_rare_notifs_enabled_v1_v2 : s->default_rare_notifs_enabled_v3_v4) {
if (is_v1_or_v2(this->version()) ? s->data->default_rare_notifs_enabled_v1_v2 : s->data->default_rare_notifs_enabled_v3_v4) {
this->set_drop_notification_mode(ItemDropNotificationMode::RARES_ONLY);
}
this->specific_version = default_specific_version_for_version(this->version(), -1);
@@ -212,7 +249,7 @@ Client::Client(
// Don't print data sent to patch clients to the logs. The patch server protocol is fully understood and data logs
// for patch clients are generally more annoying than helpful at this point.
if ((s->hide_download_commands) &&
if ((s->data->hide_download_commands) &&
((this->version() == Version::PC_PATCH) || (this->version() == Version::BB_PATCH))) {
this->channel->terminal_recv_color = phosg::TerminalFormat::END;
this->channel->terminal_send_color = phosg::TerminalFormat::END;
@@ -234,18 +271,33 @@ Client::~Client() {
if ((this->version() == Version::BB_V4) && (this->character_data.get())) {
this->save_all();
if (this->login && this->login->account && this->login->bb_license) {
AccountSync::notify_bb_login_end(
this->login->account->account_id,
this->login->bb_license->username,
this->bb_character_index);
}
}
if (this->account_sync_lock_acquired && this->login && this->login->account) {
AccountSync::notify_login_session_end(
this->login->account->account_id,
this->account_sync_session_nonce,
phosg::name_for_enum(this->version()));
this->account_sync_lock_acquired = false;
}
this->log.info_f("Deleted");
}
void Client::update_channel_name() {
string default_name = this->channel->default_name();
std::string default_name = this->channel->default_name();
auto player = this->character_file(false, false);
if (player) {
string name_str = player->disp.name.decode(this->language());
size_t level = player->disp.stats.level + 1;
this->channel->name = std::format("C-{:X} ({} Lv.{}) @ {}", this->id, name_str, level, default_name);
this->channel->name = std::format("C-{:X} ({} Lv.{}) @ {}",
this->id, player->disp.visual.name.decode(this->language()), player->disp.stats.level + 1, default_name);
} else {
this->channel->name = std::format("C-{:X} @ {}", this->id, default_name);
}
@@ -270,7 +322,7 @@ void Client::reschedule_save_game_data_timer() {
void Client::reschedule_ping_and_timeout_timers() {
auto s = this->require_server_state();
if (!is_patch(this->version())) {
this->send_ping_timer.expires_after(std::chrono::microseconds(s->client_ping_interval_usecs));
this->send_ping_timer.expires_after(std::chrono::microseconds(s->data->client_ping_interval_usecs));
this->send_ping_timer.async_wait([this](std::error_code ec) {
if (!ec) {
this->log.info_f("Sending ping command");
@@ -278,14 +330,14 @@ void Client::reschedule_ping_and_timeout_timers() {
// The game doesn't use this timestamp; we only use it for debugging purposes
be_uint64_t timestamp = phosg::now();
this->channel->send(0x1D, 0x00, &timestamp, sizeof(be_uint64_t));
} catch (const exception& e) {
} catch (const std::exception& e) {
this->log.warning_f("Failed to send ping: {}", e.what());
}
}
});
}
this->idle_timeout_timer.expires_after(std::chrono::microseconds(s->client_idle_timeout_usecs));
this->idle_timeout_timer.expires_after(std::chrono::microseconds(s->data->client_idle_timeout_usecs));
this->idle_timeout_timer.async_wait([this](std::error_code ec) {
if (!ec) {
this->log.info_f("Idle timeout expired");
@@ -298,7 +350,7 @@ void Client::convert_account_to_temporary_if_nte() {
// If the session is a prototype version and the account was created and we should use a temporary account instead,
// delete the permanent account and replace it with a temporary account.
auto s = this->require_server_state();
if (s->use_temp_accounts_for_prototypes && this->login->account_was_created && is_any_nte(this->version())) {
if (s->data->use_temp_accounts_for_prototypes && this->login->account_was_created && is_any_nte(this->version())) {
this->log.info_f("Client is a prototype version and the account was created during this session; converting permanent account to temporary account");
this->login->account->is_temporary = true;
this->login->account->delete_file();
@@ -306,25 +358,25 @@ void Client::convert_account_to_temporary_if_nte() {
}
}
shared_ptr<ServerState> Client::require_server_state() const {
std::shared_ptr<ServerState> Client::require_server_state() const {
auto server = this->server.lock();
if (!server) {
throw logic_error("server is deleted");
throw std::logic_error("server is deleted");
}
return server->get_state();
}
shared_ptr<Lobby> Client::require_lobby() const {
std::shared_ptr<Lobby> Client::require_lobby() const {
auto l = this->lobby.lock();
if (!l) {
throw runtime_error("client not in any lobby");
throw std::runtime_error("client not in any lobby");
}
return l;
}
shared_ptr<const TeamIndex::Team> Client::team() const {
std::shared_ptr<const TeamIndex::Team> Client::team() const {
if (!this->login) {
throw logic_error("Client::team called on client with no account");
throw std::logic_error("Client::team called on client with no account");
}
if (this->login->account->bb_team_id == 0) {
@@ -352,9 +404,10 @@ shared_ptr<const TeamIndex::Team> Client::team() const {
// The team membership is valid, but the player name may be different; update the team membership if needed
if (p) {
auto& m = member_it->second;
string name = p->disp.name.decode(this->language());
std::string name = p->disp.visual.name.decode(this->language());
if (m.name != name) {
this->log.info_f("Updating player name in team config");
TeamSync::enqueue_team_member_update(this->login->account->account_id, name, 0);
s->team_index->update_member_name(this->login->account->account_id, name);
}
}
@@ -363,8 +416,8 @@ shared_ptr<const TeamIndex::Team> Client::team() const {
}
bool Client::evaluate_quest_availability_expression(
shared_ptr<const IntegralExpression> expr,
shared_ptr<const Lobby> game,
std::shared_ptr<const IntegralExpression> expr,
std::shared_ptr<const Lobby> game,
uint8_t event,
Difficulty difficulty,
size_t num_players,
@@ -376,11 +429,13 @@ bool Client::evaluate_quest_availability_expression(
return true;
}
if (game && !game->quest_flag_values) {
throw logic_error("quest flags are missing from game");
throw std::logic_error("quest flags are missing from game");
}
auto p = this->character_file();
IntegralExpression::Env env = {
.flags = &p->quest_flags.data.at(static_cast<size_t>(difficulty)),
.section_id = p->disp.visual.sh.section_id,
.difficulty = difficulty,
.flags = &p->quest_flags,
.challenge_records = &p->challenge_records,
.team = this->team(),
.num_players = num_players,
@@ -389,15 +444,14 @@ bool Client::evaluate_quest_availability_expression(
};
int64_t ret = expr->evaluate(env);
if (this->log.should_log(phosg::LogLevel::L_INFO)) {
string expr_str = expr->str();
this->log.info_f("Evaluated integral expression {} => {}", expr_str, ret ? "TRUE" : "FALSE");
this->log.info_f("Evaluated integral expression {} => {}", expr->str(), ret ? "TRUE" : "FALSE");
}
return ret;
}
bool Client::can_see_quest(
shared_ptr<const Quest> q,
shared_ptr<const Lobby> game,
std::shared_ptr<const Quest> q,
std::shared_ptr<const Lobby> game,
uint8_t event,
Difficulty difficulty,
size_t num_players,
@@ -405,13 +459,20 @@ bool Client::can_see_quest(
if (!q->has_version_any_language(this->version())) {
return false;
}
if ((this->version() == Version::BB_V4) &&
game &&
(game->mode == GameMode::SOLO) &&
(game->episode == Episode::EP4) &&
(q->meta.episode == Episode::EP4)) {
return true;
}
return this->evaluate_quest_availability_expression(
q->meta.available_expression, game, event, difficulty, num_players, v1_present);
}
bool Client::can_play_quest(
shared_ptr<const Quest> q,
shared_ptr<const Lobby> game,
std::shared_ptr<const Quest> q,
std::shared_ptr<const Lobby> game,
uint8_t event,
Difficulty difficulty,
size_t num_players,
@@ -422,6 +483,13 @@ bool Client::can_play_quest(
if ((q->meta.max_players > 0) && (num_players > q->meta.max_players)) {
return false;
}
if ((this->version() == Version::BB_V4) &&
game &&
(game->mode == GameMode::SOLO) &&
(game->episode == Episode::EP4) &&
(q->meta.episode == Episode::EP4)) {
return true;
}
return this->evaluate_quest_availability_expression(
q->meta.enabled_expression, game, event, difficulty, num_players, v1_present);
}
@@ -433,129 +501,162 @@ bool Client::can_use_chat_commands() const {
if (this->login->account->check_flag(Account::Flag::ALWAYS_ENABLE_CHAT_COMMANDS)) {
return true;
}
return this->require_server_state()->enable_chat_commands;
return this->require_server_state()->data->enable_chat_commands;
}
void Client::set_login(shared_ptr<Login> login) {
void Client::set_login(std::shared_ptr<Login> login) {
this->login = login;
if (this->log.should_log(phosg::LogLevel::L_INFO)) {
string login_str = this->login->str();
this->log.info_f("Login: {}", login_str);
auto s = this->require_server_state();
if (!s->data->allow_same_account_concurrent_logins) {
if (s->data->allow_same_account_concurrent_logins_across_client_sources) {
uint64_t source_key = account_client_source_key(login->account->account_id, this->version());
auto it = s->client_for_account_source.find(source_key);
if ((it != s->client_for_account_source.end()) && (it->second.get() != this)) {
if (it->second->channel) {
it->second->channel->disconnect();
}
s->client_for_account_source.erase(it);
}
s->client_for_account_source.emplace(source_key, this->shared_from_this());
} else {
auto it = s->client_for_account.find(login->account->account_id);
if ((it != s->client_for_account.end()) && (it->second.get() != this)) {
if (it->second->channel) {
it->second->channel->disconnect();
}
s->client_for_account.erase(it);
}
s->client_for_account.emplace(this->login->account->account_id, this->shared_from_this());
}
}
}
// System file
string Client::system_filename(const string& bb_username) {
std::string Client::system_filename(const std::string& bb_username) {
return std::format("system/players/system_{}.psosys", bb_username);
}
string Client::system_filename() const {
std::string Client::system_filename() const {
if (this->version() != Version::BB_V4) {
throw logic_error("non-BB players do not have system data");
throw std::logic_error("non-BB players do not have system data");
}
if (!this->login || !this->login->bb_license) {
throw logic_error("client is not logged in");
throw std::logic_error("client is not logged in");
}
return this->system_filename(this->login->bb_license->username);
}
shared_ptr<PSOBBBaseSystemFile> Client::system_file(bool allow_load) {
std::shared_ptr<PSOBBBaseSystemFile> Client::system_file(bool allow_load) {
if (!this->system_data && allow_load) {
this->load_all_files();
}
return this->system_data;
}
shared_ptr<const PSOBBBaseSystemFile> Client::system_file(bool throw_if_missing) const {
std::shared_ptr<const PSOBBBaseSystemFile> Client::system_file(bool throw_if_missing) const {
if (!this->system_data.get() && throw_if_missing) {
throw runtime_error("system file is not loaded");
throw std::runtime_error("system file is not loaded");
}
return this->system_data;
}
void Client::save_system_file() const {
if (!this->system_data) {
throw logic_error("no system file loaded");
throw std::logic_error("no system file loaded");
}
string filename = this->system_filename();
std::string filename = this->system_filename();
phosg::save_object_file(filename, *this->system_data);
this->log.info_f("Saved system file {}", filename);
if (this->login && this->login->account && this->login->bb_license) {
AccountSync::notify_player_state_saved(
"system_saved",
this->login->account->account_id,
this->login->bb_license->username,
filename);
}
}
// Guild Card file
string Client::guild_card_filename(const string& bb_username) {
std::string Client::guild_card_filename(const std::string& bb_username) {
return std::format("system/players/guild_cards_{}.psocard", bb_username);
}
string Client::guild_card_filename() const {
std::string Client::guild_card_filename() const {
if (this->version() != Version::BB_V4) {
throw logic_error("non-BB players do not have saved Guild Card files");
throw std::logic_error("non-BB players do not have saved Guild Card files");
}
if (!this->login || !this->login->bb_license) {
throw logic_error("client is not logged in");
throw std::logic_error("client is not logged in");
}
return this->guild_card_filename(this->login->bb_license->username);
}
shared_ptr<PSOBBGuildCardFile> Client::guild_card_file(bool allow_load) {
std::shared_ptr<PSOBBGuildCardFile> Client::guild_card_file(bool allow_load) {
if (!this->guild_card_data && allow_load) {
this->load_all_files();
}
return this->guild_card_data;
}
shared_ptr<const PSOBBGuildCardFile> Client::guild_card_file(bool allow_load) const {
std::shared_ptr<const PSOBBGuildCardFile> Client::guild_card_file(bool allow_load) const {
if (!this->guild_card_data && allow_load) {
throw runtime_error("account data is not loaded");
throw std::runtime_error("account data is not loaded");
}
return this->guild_card_data;
}
void Client::save_guild_card_file() const {
if (!this->guild_card_data.get()) {
throw logic_error("no Guild Card file loaded");
throw std::logic_error("no Guild Card file loaded");
}
string filename = this->guild_card_filename();
std::string filename = this->guild_card_filename();
phosg::save_object_file(filename, *this->guild_card_data);
this->log.info_f("Saved Guild Card file {}", filename);
if (this->login && this->login->account && this->login->bb_license) {
AccountSync::notify_player_state_saved(
"guild_card_saved",
this->login->account->account_id,
this->login->bb_license->username,
filename);
}
}
// Character file
string Client::character_filename(const std::string& bb_username, ssize_t index) {
std::string Client::character_filename(const std::string& bb_username, ssize_t index) {
if (bb_username.empty()) {
throw logic_error("non-BB players do not have saved character filenames");
throw std::logic_error("non-BB players do not have saved character filenames");
}
if (index < 0) {
throw logic_error("character index is not set");
throw std::logic_error("character index is not set");
}
return std::format("system/players/player_{}_{}.psochar", bb_username, index);
}
string Client::backup_character_filename(uint32_t account_id, size_t index, bool is_ep3) {
std::string Client::backup_character_filename(uint32_t account_id, size_t index, bool is_ep3) {
return std::format("system/players/backup_player_{}_{}.{}",
account_id, index, is_ep3 ? "pso3char" : "psochar");
}
string Client::character_filename() const {
std::string Client::character_filename() const {
if (this->version() != Version::BB_V4) {
throw logic_error("non-BB players do not have saved character filenames");
throw std::logic_error("non-BB players do not have saved character filenames");
}
if (!this->login || !this->login->bb_license) {
throw logic_error("client is not logged in");
throw std::logic_error("client is not logged in");
}
return this->character_filename(this->login->bb_license->username, this->bb_character_index);
}
shared_ptr<PSOBBCharacterFile> Client::character_file(bool allow_load, bool allow_overlay) {
std::shared_ptr<PSOBBCharacterFile> Client::character_file(bool allow_load, bool allow_overlay) {
if (this->overlay_character_data && allow_overlay) {
return this->overlay_character_data;
}
if (!this->character_data && allow_load) {
if ((this->version() == Version::BB_V4) && (this->bb_character_index < 0)) {
throw runtime_error("character index not specified");
throw std::runtime_error("character index not specified");
}
this->load_all_files();
if (!this->character_data) {
@@ -565,35 +666,35 @@ shared_ptr<PSOBBCharacterFile> Client::character_file(bool allow_load, bool allo
return this->character_data;
}
shared_ptr<const PSOBBCharacterFile> Client::character_file(bool throw_if_missing, bool allow_overlay) const {
std::shared_ptr<const PSOBBCharacterFile> Client::character_file(bool throw_if_missing, bool allow_overlay) const {
if (allow_overlay && this->overlay_character_data) {
return this->overlay_character_data;
}
if (!this->character_data && throw_if_missing) {
throw runtime_error("character data is not loaded");
throw std::runtime_error("character data is not loaded");
}
return this->character_data;
}
void Client::save_character_file(
const string& filename,
shared_ptr<const PSOBBBaseSystemFile> system,
shared_ptr<const PSOBBCharacterFile> character) {
const std::string& filename,
std::shared_ptr<const PSOBBBaseSystemFile> system,
std::shared_ptr<const PSOBBCharacterFile> character) {
PSOCHARFile::save(filename, system, character);
}
void Client::save_ep3_character_file(
const string& filename,
const std::string& filename,
const PSOGCEp3CharacterFile::Character& character) {
phosg::save_file(filename, &character, sizeof(character));
}
void Client::save_character_file() {
if (!this->system_data.get()) {
throw logic_error("no system file loaded");
throw std::logic_error("no system file loaded");
}
if (!this->character_data.get()) {
throw logic_error("no character file loaded");
throw std::logic_error("no character file loaded");
}
if (this->should_update_play_time) {
// This is slightly inaccurate, since fractions of a second are truncated off each time we save. I'm lazy, so
@@ -612,23 +713,30 @@ void Client::save_character_file() {
auto filename = this->character_filename();
this->save_character_file(filename, this->system_data, this->character_data);
this->log.info_f("Saved character file {}", filename);
if (this->login && this->login->account && this->login->bb_license) {
AccountSync::notify_player_state_saved(
"character_saved",
this->login->account->account_id,
this->login->bb_license->username,
filename);
}
}
void Client::create_character_file(
uint32_t guild_card_number,
Language language,
const PlayerDispDataBBPreview& preview,
shared_ptr<const LevelTable> level_table) {
const PlayerVisualConfigV4& visual,
std::shared_ptr<const LevelTable> level_table) {
this->log.info_f("Creating new character file");
this->character_data = PSOBBCharacterFile::create_from_preview(guild_card_number, language, preview, level_table);
this->character_data = PSOBBCharacterFile::create_from_config(guild_card_number, language, visual, level_table);
this->save_character_file();
this->log.info_f("Deleting bank file");
this->bank_data.reset();
std::filesystem::remove(this->bank_filename());
}
void Client::create_battle_overlay(shared_ptr<const BattleRules> rules, shared_ptr<const LevelTable> level_table) {
this->overlay_character_data = make_shared<PSOBBCharacterFile>(*this->character_file(true, false));
void Client::create_battle_overlay(std::shared_ptr<const BattleRules> rules, std::shared_ptr<const LevelTable> level_table) {
this->overlay_character_data = std::make_shared<PSOBBCharacterFile>(*this->character_file(true, false));
if (rules->weapon_and_armor_mode != BattleRules::WeaponAndArmorMode::ALLOW) {
this->overlay_character_data->inventory.remove_all_items_of_type(0);
@@ -645,8 +753,8 @@ void Client::create_battle_overlay(shared_ptr<const BattleRules> rules, shared_p
this->overlay_character_data->inventory.hp_from_materials = 0;
this->overlay_character_data->inventory.tp_from_materials = 0;
uint32_t target_level = clamp<uint32_t>(rules->char_level, 0, 199);
uint8_t char_class = this->overlay_character_data->disp.visual.char_class;
uint32_t target_level = std::clamp<uint32_t>(rules->char_level, 0, 199);
uint8_t char_class = this->overlay_character_data->disp.visual.sh.char_class;
auto& stats = this->overlay_character_data->disp.stats;
level_table->reset_to_base(stats, char_class);
@@ -677,16 +785,16 @@ void Client::create_battle_overlay(shared_ptr<const BattleRules> rules, shared_p
}
void Client::create_challenge_overlay(
Version version, size_t template_index, shared_ptr<const LevelTable> level_table) {
Version version, size_t template_index, std::shared_ptr<const LevelTable> level_table) {
auto p = this->character_file(true, false);
const auto& tpl = get_challenge_template_definition(version, p->disp.visual.class_flags, template_index);
const auto& tpl = get_challenge_template_definition(version, p->disp.visual.sh.class_flags, template_index);
this->overlay_character_data = make_shared<PSOBBCharacterFile>(*p);
this->overlay_character_data = std::make_shared<PSOBBCharacterFile>(*p);
auto overlay = this->overlay_character_data;
for (size_t z = 0; z < overlay->inventory.items.size(); z++) {
auto& i = overlay->inventory.items[z];
i.present = 0;
i.state = 0;
i.unknown_a1 = 0;
i.extension_data1 = 0;
i.extension_data2 = 0;
@@ -696,14 +804,14 @@ void Client::create_challenge_overlay(
overlay->inventory.items[13].extension_data2 = 1;
level_table->reset_to_base(overlay->disp.stats, overlay->disp.visual.char_class);
level_table->advance_to_level(overlay->disp.stats, tpl.level, overlay->disp.visual.char_class);
level_table->reset_to_base(overlay->disp.stats, overlay->disp.visual.sh.char_class);
level_table->advance_to_level(overlay->disp.stats, tpl.level, overlay->disp.visual.sh.char_class);
const auto& stats_delta = level_table->stats_delta_for_level(
overlay->disp.visual.char_class, overlay->disp.stats.level);
overlay->disp.visual.sh.char_class, overlay->disp.stats.level);
overlay->disp.stats.esp = 40;
overlay->disp.stats.attack_range = 10.0;
overlay->disp.stats.experience = stats_delta.experience;
overlay->disp.stats.exp = stats_delta.exp;
overlay->disp.stats.meseta = 0;
overlay->clear_all_material_usage();
for (size_t z = 0; z < 0x13; z++) {
@@ -712,7 +820,7 @@ void Client::create_challenge_overlay(
for (size_t z = 0; z < tpl.items.size(); z++) {
auto& inv_item = overlay->inventory.items[z];
inv_item.present = tpl.items[z].present;
inv_item.state = tpl.items[z].state;
inv_item.unknown_a1 = tpl.items[z].unknown_a1;
inv_item.flags = tpl.items[z].flags;
inv_item.data = tpl.items[z].data;
@@ -726,9 +834,9 @@ void Client::create_challenge_overlay(
// Bank file
string Client::bank_filename(const std::string& bb_username, ssize_t index) {
std::string Client::bank_filename(const std::string& bb_username, ssize_t index) {
if (bb_username.empty()) {
throw logic_error("non-BB players do not have saved bank files");
throw std::logic_error("non-BB players do not have saved bank files");
}
if (index < 0) {
return std::format("system/players/shared_bank_{}.psobank", bb_username);
@@ -737,19 +845,19 @@ string Client::bank_filename(const std::string& bb_username, ssize_t index) {
}
}
string Client::bank_filename() const {
std::string Client::bank_filename() const {
if (this->version() != Version::BB_V4) {
throw logic_error("non-BB players do not have saved bank filenames");
throw std::logic_error("non-BB players do not have saved bank filenames");
}
if (!this->login || !this->login->bb_license) {
throw logic_error("client is not logged in");
throw std::logic_error("client is not logged in");
}
return this->bank_filename(this->login->bb_license->username, this->bb_bank_character_index);
}
std::shared_ptr<PlayerBank> Client::bank_file(bool allow_load) {
if (this->version() != Version::BB_V4) {
throw logic_error("non-BB players do not have saved bank files");
throw std::logic_error("non-BB players do not have saved bank files");
}
if (this->has_overlay()) {
throw std::runtime_error("bank is inaccessible when overlay is present");
@@ -759,7 +867,7 @@ std::shared_ptr<PlayerBank> Client::bank_file(bool allow_load) {
// If there's a psobank file, load it and ignore the character file bank
auto filename = this->bank_filename();
auto f = phosg::fopen_unique(filename, "rb");
this->bank_data = make_shared<PlayerBank>();
this->bank_data = std::make_shared<PlayerBank>();
this->bank_data->load(f.get());
this->log.info_f("Loaded bank data from {}", filename);
} catch (const phosg::cannot_open_file&) {
@@ -771,22 +879,23 @@ std::shared_ptr<PlayerBank> Client::bank_file(bool allow_load) {
this->log.info_f("Using bank data from loaded character");
} else if (this->bb_bank_character_index >= 0) {
if (!this->login || !this->login->bb_license) {
throw logic_error("client is not logged in");
throw std::logic_error("client is not logged in");
}
string filename = this->character_filename(this->login->bb_license->username, this->bb_bank_character_index);
std::string filename = this->character_filename(
this->login->bb_license->username, this->bb_bank_character_index);
auto character = PSOCHARFile::load_shared(filename, false).character_file;
this->bank_data = std::make_shared<PlayerBank>(character->bank);
this->log.info_f("Using bank data from {}", filename);
} else {
// The shared bank doesn't exist; make a new one
this->bank_data = make_shared<PlayerBank>();
this->bank_data = std::make_shared<PlayerBank>();
this->log.info_f("Created new shared bank");
}
}
auto s = this->require_server_state();
this->bank_data->max_items = s->bb_max_bank_items;
this->bank_data->max_meseta = s->bb_max_bank_meseta;
this->bank_data->max_items = s->data->bb_max_bank_items;
this->bank_data->max_meseta = s->data->bb_max_bank_meseta;
this->update_bank_data_after_load(this->bank_data);
}
return this->bank_data;
@@ -799,18 +908,25 @@ std::shared_ptr<const PlayerBank> Client::bank_file(bool throw_if_missing) const
return this->bank_data;
}
void Client::save_bank_file(const string& filename, const PlayerBank& bank) {
void Client::save_bank_file(const std::string& filename, const PlayerBank& bank) {
auto f = phosg::fopen_unique(filename, "wb");
bank.save(f.get());
}
void Client::save_bank_file() const {
if (!this->bank_data) {
throw logic_error("no bank file loaded");
throw std::logic_error("no bank file loaded");
}
auto filename = this->bank_filename();
this->save_bank_file(filename, *this->bank_data);
this->log.info_f("Saved bank file {}", filename);
if (this->login && this->login->account && this->login->bb_license) {
AccountSync::notify_player_state_saved(
"bank_saved",
this->login->account->account_id,
this->login->bb_license->username,
filename);
}
}
void Client::change_bank(ssize_t index) {
@@ -828,30 +944,28 @@ void Client::change_bank(ssize_t index) {
// Legacy files
string Client::legacy_account_filename() const {
std::string Client::legacy_account_filename() const {
if (this->version() != Version::BB_V4) {
throw logic_error("non-BB players do not have saved account data");
throw std::logic_error("non-BB players do not have saved account data");
}
if (!this->login || !this->login->bb_license) {
throw logic_error("client is not logged in");
throw std::logic_error("client is not logged in");
}
return std::format("system/players/account_{}.nsa", this->login->bb_license->username);
}
string Client::legacy_player_filename() const {
std::string Client::legacy_player_filename() const {
if (this->version() != Version::BB_V4) {
throw logic_error("non-BB players do not have saved player files");
throw std::logic_error("non-BB players do not have saved player files");
}
if (!this->login || !this->login->bb_license) {
throw logic_error("client is not logged in");
throw std::logic_error("client is not logged in");
}
if (this->bb_character_index < 0) {
throw logic_error("character index is not set");
throw std::logic_error("character index is not set");
}
return std::format(
"system/players/player_{}_{}.nsc",
this->login->bb_license->username,
static_cast<ssize_t>(this->bb_character_index + 1));
return std::format("system/players/player_{}_{}.nsc",
this->login->bb_license->username, static_cast<ssize_t>(this->bb_character_index + 1));
}
void Client::import_blocked_senders(const parray<le_uint32_t, 30>& blocked_senders) {
@@ -865,20 +979,20 @@ void Client::import_blocked_senders(const parray<le_uint32_t, 30>& blocked_sende
void Client::load_all_files() {
if (this->version() != Version::BB_V4) {
this->system_data = make_shared<PSOBBBaseSystemFile>();
this->character_data = make_shared<PSOBBCharacterFile>();
this->guild_card_data = make_shared<PSOBBGuildCardFile>();
this->bank_data = make_shared<PlayerBank>();
this->system_data = std::make_shared<PSOBBBaseSystemFile>();
this->character_data = std::make_shared<PSOBBCharacterFile>();
this->guild_card_data = std::make_shared<PSOBBGuildCardFile>();
this->bank_data = std::make_shared<PlayerBank>();
return;
}
if (!this->login || !this->login->bb_license) {
throw logic_error("cannot load BB player data until client is logged in");
throw std::logic_error("cannot load BB player data until client is logged in");
}
if (!this->system_data) {
string sys_filename = this->system_filename();
std::string sys_filename = this->system_filename();
if (std::filesystem::is_regular_file(sys_filename)) {
this->system_data = make_shared<PSOBBBaseSystemFile>(phosg::load_object_file<PSOBBBaseSystemFile>(sys_filename, true));
this->system_data = std::make_shared<PSOBBBaseSystemFile>(phosg::load_object_file<PSOBBBaseSystemFile>(sys_filename, true));
this->log.info_f("Loaded system data from {}", sys_filename);
} else {
this->log.info_f("System file is missing: {}", sys_filename);
@@ -886,7 +1000,7 @@ void Client::load_all_files() {
}
if (!this->character_data && (this->bb_character_index >= 0)) {
string char_filename = this->character_filename();
std::string char_filename = this->character_filename();
if (std::filesystem::is_regular_file(char_filename)) {
auto psochar = PSOCHARFile::load_shared(char_filename, !this->system_data);
this->character_data = psochar.character_file;
@@ -895,7 +1009,7 @@ void Client::load_all_files() {
// If there was no .psosys file, use the system file from the .psochar file instead
if (!this->system_data) {
if (!psochar.system_file) {
throw logic_error("account system data not present, and also not loaded from psochar file");
throw std::logic_error("account system data not present, and also not loaded from psochar file");
}
this->system_data = psochar.system_file;
this->log.info_f("Loaded system data from {}", char_filename);
@@ -910,9 +1024,9 @@ void Client::load_all_files() {
}
if (!this->guild_card_data) {
string card_filename = this->guild_card_filename();
std::string card_filename = this->guild_card_filename();
if (std::filesystem::is_regular_file(card_filename)) {
this->guild_card_data = make_shared<PSOBBGuildCardFile>(phosg::load_object_file<PSOBBGuildCardFile>(card_filename));
this->guild_card_data = std::make_shared<PSOBBGuildCardFile>(phosg::load_object_file<PSOBBGuildCardFile>(card_filename));
this->guild_card_data->delete_duplicates();
this->log.info_f("Loaded Guild Card data from {}", card_filename);
} else {
@@ -922,25 +1036,25 @@ void Client::load_all_files() {
// If any of the above files are still missing, try to load from .nsa/.nsc files instead
if (!this->system_data || (!this->character_data && (this->bb_character_index >= 0)) || !this->guild_card_data) {
string nsa_filename = this->legacy_account_filename();
shared_ptr<LegacySavedAccountDataBB> nsa_data;
std::string nsa_filename = this->legacy_account_filename();
std::shared_ptr<LegacySavedAccountDataBB> nsa_data;
if (std::filesystem::is_regular_file(nsa_filename)) {
nsa_data = make_shared<LegacySavedAccountDataBB>(phosg::load_object_file<LegacySavedAccountDataBB>(nsa_filename));
nsa_data = std::make_shared<LegacySavedAccountDataBB>(phosg::load_object_file<LegacySavedAccountDataBB>(nsa_filename));
if (!nsa_data->signature.eq(LegacySavedAccountDataBB::SIGNATURE)) {
throw runtime_error("account data header is incorrect");
throw std::runtime_error("account data header is incorrect");
}
if (!this->system_data) {
this->system_data = make_shared<PSOBBBaseSystemFile>(nsa_data->system_file);
this->system_data = std::make_shared<PSOBBBaseSystemFile>(nsa_data->system_file);
this->log.info_f("Loaded legacy system data from {}", nsa_filename);
}
if (!this->guild_card_data) {
this->guild_card_data = make_shared<PSOBBGuildCardFile>(nsa_data->guild_card_file);
this->guild_card_data = std::make_shared<PSOBBGuildCardFile>(nsa_data->guild_card_file);
this->log.info_f("Loaded legacy Guild Card data from {}", nsa_filename);
}
}
if (!this->character_data && (this->bb_character_index >= 0)) {
string nsc_filename = this->legacy_player_filename();
std::string nsc_filename = this->legacy_player_filename();
if (std::filesystem::is_regular_file(nsc_filename)) {
auto nsc_data = phosg::load_object_file<LegacySavedPlayerDataBB>(nsc_filename);
if (nsc_data.signature == LegacySavedPlayerDataBB::SIGNATURE_V0) {
@@ -950,10 +1064,10 @@ void Client::load_all_files() {
nsc_data.battle_records.disconnect_count = 0;
nsc_data.battle_records.unknown_a1.clear(0);
} else if (nsc_data.signature != LegacySavedPlayerDataBB::SIGNATURE_V1) {
throw runtime_error("legacy player data has incorrect signature");
throw std::runtime_error("legacy player data has incorrect signature");
}
this->character_data = make_shared<PSOBBCharacterFile>();
this->character_data = std::make_shared<PSOBBCharacterFile>();
this->character_data->inventory = nsc_data.inventory;
this->character_data->disp = nsc_data.disp;
this->character_data->play_time_seconds = 0;
@@ -961,12 +1075,12 @@ void Client::load_all_files() {
this->character_data->death_count = nsc_data.death_count;
this->character_data->bank = nsc_data.bank;
this->character_data->guild_card.guild_card_number = this->login->account->account_id;
this->character_data->guild_card.name = nsc_data.disp.name;
this->character_data->guild_card.name = nsc_data.disp.visual.name;
this->character_data->guild_card.description = nsc_data.guild_card_description;
this->character_data->guild_card.present = 1;
this->character_data->guild_card.language = nsc_data.inventory.language;
this->character_data->guild_card.section_id = nsc_data.disp.visual.section_id;
this->character_data->guild_card.char_class = nsc_data.disp.visual.char_class;
this->character_data->guild_card.section_id = nsc_data.disp.visual.sh.section_id;
this->character_data->guild_card.char_class = nsc_data.disp.visual.sh.char_class;
this->character_data->auto_reply = nsc_data.auto_reply;
this->character_data->info_board = nsc_data.info_board;
this->character_data->battle_records = nsc_data.battle_records;
@@ -989,23 +1103,23 @@ void Client::load_all_files() {
// The system and Guild Card files can be auto-created if they can't be loaded. After this, system_data and
// guild_card_data are always non-null, but character_data may still be null
if (!this->system_data) {
this->system_data = make_shared<PSOBBBaseSystemFile>();
this->system_data = std::make_shared<PSOBBBaseSystemFile>();
auto s = this->require_server_state();
if (s->bb_default_keyboard_config) {
this->system_data->key_config = *s->bb_default_keyboard_config;
if (s->data->bb_default_keyboard_config) {
this->system_data->key_config = *s->data->bb_default_keyboard_config;
}
if (s->bb_default_joystick_config) {
this->system_data->joystick_config = *s->bb_default_joystick_config;
if (s->data->bb_default_joystick_config) {
this->system_data->joystick_config = *s->data->bb_default_joystick_config;
}
this->log.info_f("Created new system data");
}
if (!this->guild_card_data) {
this->guild_card_data = make_shared<PSOBBGuildCardFile>();
this->guild_card_data = std::make_shared<PSOBBGuildCardFile>();
this->log.info_f("Created new Guild Card data");
}
auto s = this->require_server_state();
auto stack_limits = s->item_stack_limits(this->version());
auto stack_limits = s->data->item_stack_limits(this->version());
this->blocked_senders.clear();
for (size_t z = 0; z < this->guild_card_data->blocked_senders.size(); z++) {
@@ -1016,7 +1130,7 @@ void Client::load_all_files() {
if (this->character_data) {
// Clear legacy play_time field
this->character_data->disp.name.clear_after_bytes(0x18);
this->character_data->disp.visual.name.clear_after_bytes(0x18);
this->character_data->inventory.enforce_stack_limits(stack_limits);
this->login->account->auto_reply_message = this->character_data->auto_reply.decode();
this->login->account->save();
@@ -1029,8 +1143,8 @@ void Client::load_all_files() {
}
}
void Client::update_character_data_after_load(shared_ptr<PSOBBCharacterFile> charfile) {
charfile->import_tethealla_material_usage(this->require_server_state()->level_table(this->version()));
void Client::update_character_data_after_load(std::shared_ptr<PSOBBCharacterFile> charfile) {
charfile->import_tethealla_material_usage(this->require_server_state()->data->level_table(this->version()));
Language lang = this->language();
this->log.info_f("Overriding language fields in save files with {}", name_for_language(lang));
@@ -1038,9 +1152,9 @@ void Client::update_character_data_after_load(shared_ptr<PSOBBCharacterFile> cha
charfile->guild_card.language = lang;
}
void Client::update_bank_data_after_load(shared_ptr<PlayerBank> bank) {
void Client::update_bank_data_after_load(std::shared_ptr<PlayerBank> bank) {
auto s = this->require_server_state();
auto limits = s->item_stack_limits(this->version());
auto limits = s->data->item_stack_limits(this->version());
for (auto& item : bank->items) {
if (item.data.is_stackable(*limits)) {
if (item.data.data1[5] != item.amount) {
@@ -1071,17 +1185,17 @@ void Client::save_all() {
}
void Client::load_backup_character(uint32_t account_id, size_t index) {
string filename = this->backup_character_filename(account_id, index, false);
std::string filename = this->backup_character_filename(account_id, index, false);
this->character_data = PSOCHARFile::load_shared(filename, false).character_file;
this->update_character_data_after_load(this->character_data);
this->v1_v2_last_reported_disp.reset();
}
shared_ptr<PSOGCEp3CharacterFile::Character> Client::load_ep3_backup_character(uint32_t account_id, size_t index) {
string filename = this->backup_character_filename(account_id, index, true);
auto ch = make_shared<PSOGCEp3CharacterFile::Character>(phosg::load_object_file<PSOGCEp3CharacterFile::Character>(filename));
std::shared_ptr<PSOGCEp3CharacterFile::Character> Client::load_ep3_backup_character(uint32_t account_id, size_t index) {
std::string filename = this->backup_character_filename(account_id, index, true);
auto ch = std::make_shared<PSOGCEp3CharacterFile::Character>(phosg::load_object_file<PSOGCEp3CharacterFile::Character>(filename));
this->character_data = PSOBBCharacterFile::create_from_file(*ch);
this->ep3_config = make_shared<Episode3::PlayerConfig>(ch->ep3_config);
this->ep3_config = std::make_shared<Episode3::PlayerConfig>(ch->ep3_config);
this->update_character_data_after_load(this->character_data);
this->v1_v2_last_reported_disp.reset();
return ch;
@@ -1112,7 +1226,7 @@ void Client::print_inventory() const {
for (size_t x = 0; x < p->inventory.num_items; x++) {
const auto& item = p->inventory.items[x];
auto hex = item.data.hex();
auto name = s->describe_item(this->version(), item.data);
auto name = s->data->describe_item(this->version(), item.data);
this->log.info_f("[PlayerInventory] {:2}: [+{:08X}] {} ({})", x, item.flags, hex, name);
}
}
@@ -1126,7 +1240,7 @@ void Client::print_bank() const {
const auto& item = this->bank_data->items[x];
const char* present_token = item.present ? "" : " (missing present flag)";
auto hex = item.data.hex();
auto name = s->describe_item(this->version(), item.data);
auto name = s->data->describe_item(this->version(), item.data);
this->log.info_f("[PlayerBank] {:3}: {} ({}) (x{}){}", x, hex, name, item.amount, present_token);
}
} else {
+11 -6
View File
@@ -6,11 +6,10 @@
#include "Account.hh"
#include "AsyncUtils.hh"
#include "Channel.hh"
#include "ClientFunctionIndex.hh"
#include "CommandFormats.hh"
#include "Episode3/BattleRecord.hh"
#include "Episode3/Tournament.hh"
#include "FileContentsCache.hh"
#include "FunctionCompiler.hh"
#include "PSOEncryption.hh"
#include "PSOProtocol.hh"
#include "PatchFileIndex.hh"
@@ -19,6 +18,7 @@
#include "QuestScript.hh"
#include "TeamIndex.hh"
#include "Text.hh"
#include <string>
extern const uint64_t CLIENT_CONFIG_MAGIC;
@@ -27,7 +27,7 @@ struct Lobby;
class Parsed6x70Data;
struct GetPlayerInfoResult {
// Exactly one of the following two shared_ptrs is not null
// Exactly one of the following two std::shared_ptrs is not null
std::shared_ptr<PSOBBCharacterFile> character;
std::shared_ptr<PSOGCEp3CharacterFile::Character> ep3_character;
bool is_full_info; // True if the client sent 30; false if it was 61 or 98
@@ -132,6 +132,9 @@ public:
uint64_t xb_user_id = 0;
uint32_t xb_unknown_a1b = 0;
std::shared_ptr<Login> login;
bool account_sync_lock_acquired = false;
uint32_t account_sync_lock_account_id = 0;
std::string account_sync_session_nonce;
std::shared_ptr<ProxySession> proxy_session;
// Patch server state (only used for PC_PATCH and BB_PATCH versions)
@@ -152,7 +155,8 @@ public:
uint8_t override_lobby_event = 0xFF; // FF = no override
uint8_t override_lobby_number = 0x80; // 80 = no override
int64_t override_random_seed = -1;
int8_t selected_blueballz_tier = -1; // -1 = normal lobby/game; 0..10 = requested Blueballz tier
int8_t selected_brutal_peeps_tier = -1; // -1 = normal lobby/game; 1..11 = requested Brutal Peeps tier
int8_t brutal_peeps_pc_battleparam_patch_tier = -1; // -1 = vanilla; 1..11 = currently applied PC BattleParam BP tier
std::unique_ptr<Variations> override_variations;
VectorXYZF pos;
uint32_t floor = 0x0F;
@@ -191,8 +195,9 @@ public:
};
bool should_update_play_time;
std::unordered_set<uint32_t> blocked_senders;
std::unique_ptr<PlayerDispDataDCPCV3> v1_v2_last_reported_disp;
std::unique_ptr<PlayerDispDataV123> v1_v2_last_reported_disp;
std::shared_ptr<Parsed6x70Data> last_reported_6x70;
std::unordered_set<uint16_t> expected_game_state_sync_commands; // (command_num << 8) | target_client_id
// These are null unless the client is within the trade sequence (D0-D4 or EE commands)
std::unique_ptr<PendingItemTrade> pending_item_trade;
std::unique_ptr<PendingCardTrade> pending_card_trade;
@@ -315,7 +320,7 @@ public:
void create_character_file(
uint32_t guild_card_number,
Language language,
const PlayerDispDataBBPreview& preview,
const PlayerVisualConfigV4& visual,
std::shared_ptr<const LevelTable> level_table);
void create_battle_overlay(std::shared_ptr<const BattleRules> rules, std::shared_ptr<const LevelTable> level_table);
void create_challenge_overlay(Version version, size_t template_index, std::shared_ptr<const LevelTable> level_table);
+633
View File
@@ -0,0 +1,633 @@
#include "ClientFunctionIndex.hh"
#include <stdio.h>
#include <string.h>
#include <filesystem>
#include <phosg/Filesystem.hh>
#include <phosg/Hash.hh>
#include <phosg/Time.hh>
#include <stdexcept>
#include <resource_file/Emulators/PPC32Emulator.hh>
#include <resource_file/Emulators/SH4Emulator.hh>
#include <resource_file/Emulators/X86Emulator.hh>
#include "CommandFormats.hh"
#include "CommonFileFormats.hh"
#include "Compression.hh"
#include "Loggers.hh"
using Arch = ClientFunctionIndex::Function::Architecture;
const char* name_for_architecture(Arch arch) {
switch (arch) {
case Arch::SH4:
return "SH-4";
case Arch::POWERPC:
return "PowerPC";
case Arch::X86:
return "x86";
default:
throw std::logic_error("invalid architecture");
}
}
uint32_t specific_version_for_architecture(Arch arch) {
switch (arch) {
case Arch::SH4:
return SPECIFIC_VERSION_SH4_INDETERMINATE;
case Arch::POWERPC:
return SPECIFIC_VERSION_PPC_INDETERMINATE;
case Arch::X86:
return SPECIFIC_VERSION_X86_INDETERMINATE;
default:
throw std::logic_error("invalid architecture");
}
}
Arch architecture_for_specific_version(uint32_t specific_version) {
if (specific_version == SPECIFIC_VERSION_SH4_INDETERMINATE) {
return Arch::SH4;
} else if (specific_version == SPECIFIC_VERSION_PPC_INDETERMINATE) {
return Arch::POWERPC;
} else if (specific_version == SPECIFIC_VERSION_X86_INDETERMINATE) {
return Arch::X86;
} else if (specific_version_is_dc(specific_version)) {
return Arch::SH4;
} else if (specific_version_is_gc(specific_version)) {
return Arch::POWERPC;
} else {
return Arch::X86;
}
}
static inline std::string cache_key(const std::string& name, uint32_t specific_version) {
return std::format("{}-{:08X}", name, specific_version);
}
template <typename T>
const T& get_with_sv_fallback(
const std::unordered_map<std::string, T>& index, const std::string& name, uint32_t specific_version) {
try {
return index.at(cache_key(name, specific_version));
} catch (const std::out_of_range&) {
}
uint32_t arch_specific_version = specific_version_for_architecture(architecture_for_specific_version(
specific_version));
if (arch_specific_version != specific_version) {
try {
return index.at(cache_key(name, arch_specific_version));
} catch (const std::out_of_range&) {
}
}
return index.at(name);
}
template <bool BE>
std::string ClientFunctionIndex::Function::generate_client_command_t(
const std::unordered_map<std::string, uint32_t>& label_writes,
const void* suffix_data,
size_t suffix_size,
uint32_t override_relocations_offset) const {
using FooterT = RELFileFooterT<BE>;
FooterT footer;
footer.num_relocations = this->relocation_deltas.size();
footer.unused1.clear(0);
footer.root_offset = this->entrypoint_offset_offset;
footer.unused2.clear(0);
phosg::StringWriter w;
if (!label_writes.empty()) {
std::string modified_code = this->code;
for (const auto& it : label_writes) {
size_t offset = this->label_offsets.at(it.first);
if (offset > modified_code.size() - 4) {
throw std::runtime_error("label out of range");
}
*reinterpret_cast<U32T<FooterT::IsBE>*>(modified_code.data() + offset) = it.second;
}
w.write(modified_code);
} else {
w.write(this->code);
}
if (suffix_size) {
w.write(suffix_data, suffix_size);
}
while (w.size() & 3) {
w.put_u8(0);
}
footer.relocations_offset = w.size();
// Always write at least 4 bytes even if there are no relocations
if (this->relocation_deltas.empty()) {
w.put_u32(0);
}
if (override_relocations_offset) {
footer.relocations_offset = override_relocations_offset;
} else {
for (uint16_t delta : this->relocation_deltas) {
w.put<U16T<FooterT::IsBE>>(delta);
}
if (this->relocation_deltas.size() & 1) {
w.put_u16(0);
}
}
w.put(footer);
return std::move(w.str());
}
std::string ClientFunctionIndex::Function::generate_client_command(
const std::unordered_map<std::string, uint32_t>& label_writes,
const void* suffix_data,
size_t suffix_size,
uint32_t override_relocations_offset) const {
if (this->is_big_endian()) {
return this->generate_client_command_t<true>(label_writes, suffix_data, suffix_size, override_relocations_offset);
} else if ((this->arch == Architecture::X86) || (this->arch == Architecture::SH4)) {
return this->generate_client_command_t<false>(label_writes, suffix_data, suffix_size, override_relocations_offset);
} else {
throw std::logic_error("invalid architecture");
}
}
static std::unordered_map<uint32_t, std::string> preprocess_function_code(const std::string& text) {
std::unordered_set<uint32_t> all_specific_versions;
struct Line {
std::string text;
std::unordered_map<uint32_t, size_t> new_specific_versions; // Nonempty iff line is a .versions directive
bool enable_all_versions = false;
};
std::vector<Line> lines;
for (auto& line_text : phosg::split(text, '\n')) {
auto& line = lines.emplace_back();
line.text = std::move(line_text);
std::string stripped_line = line.text;
phosg::strip_whitespace(stripped_line);
if (stripped_line == ".all_versions") {
line.enable_all_versions = true;
} else if (stripped_line.starts_with(".versions ")) {
for (auto& vers_token : phosg::split(stripped_line.substr(10), ' ')) {
phosg::strip_whitespace(vers_token);
if (!vers_token.empty()) {
uint32_t specific_version = specific_version_for_str(vers_token);
size_t version_index = line.new_specific_versions.size();
all_specific_versions.emplace(specific_version);
line.new_specific_versions.emplace(std::move(specific_version), version_index);
}
}
}
}
static const std::string empty_str = "";
std::unordered_map<uint32_t, std::string> ret;
for (uint32_t specific_version : all_specific_versions) {
std::deque<std::string> version_lines;
bool include_current_line = true;
size_t current_vers_index = all_specific_versions.size();
for (size_t line_znum = 0; line_znum < lines.size(); line_znum++) {
const auto& line = lines[line_znum];
if (line.enable_all_versions) {
include_current_line = true;
current_vers_index = all_specific_versions.size();
version_lines.emplace_back(empty_str);
} else if (!line.new_specific_versions.empty()) {
auto it = line.new_specific_versions.find(specific_version);
if (it == line.new_specific_versions.end()) {
include_current_line = false;
current_vers_index = all_specific_versions.size();
} else {
include_current_line = true;
current_vers_index = it->second;
}
version_lines.emplace_back(empty_str);
} else if (!include_current_line) {
version_lines.emplace_back(empty_str);
} else {
std::string line_text = line.text;
size_t vers_offset = line_text.find("<VERS ");
while (vers_offset != std::string::npos) {
size_t end_offset = line_text.find('>', vers_offset + 6);
if (end_offset == std::string::npos) {
throw std::runtime_error(std::format("(version {}) (line {}) unterminated <VERS> replacement",
str_for_specific_version(specific_version), line_znum + 1));
}
auto tokens = phosg::split(line_text.substr(vers_offset + 6, end_offset - vers_offset - 6), ' ');
if (current_vers_index >= tokens.size()) {
throw std::runtime_error(std::format("(version {}) (line {}) invalid <VERS> replacement",
str_for_specific_version(specific_version), line_znum + 1));
}
line_text = line_text.substr(0, vers_offset) + tokens[current_vers_index] + line_text.substr(end_offset + 1);
vers_offset = line_text.find("<VERS ");
}
version_lines.emplace_back(std::move(line_text));
}
}
ret.emplace(specific_version, phosg::join(version_lines, "\n"));
}
return ret;
}
ClientFunctionIndex::ClientFunctionIndex(const std::string& root_dir, bool raise_on_any_failure) {
std::map<std::string, std::string> source_files;
std::function<void(const std::string&)> add_directory = [&](const std::string& dir) -> void {
for (const auto& item : std::filesystem::directory_iterator(dir)) {
std::string item_name = item.path().filename().string();
std::string item_path = dir.ends_with("/") ? (dir + item_name) : (dir + "/" + item_name);
if (std::filesystem::is_directory(item_path)) {
add_directory(item_path);
} else if (item_path.ends_with(".s") && std::filesystem::is_regular_file(item_path)) {
client_functions_log.debug_f("Adding {} from {}", item_name, item_path);
if (item_name.find("Dragon") != std::string::npos) {
client_functions_log.warning_f("Dragon source load debug: adding {} from {}", item_name, item_path);
}
if (!source_files.emplace(item_name, phosg::load_file(item_path)).second) {
throw std::runtime_error(std::format("Duplicate source filename: {}", item_name));
}
} else if (item_path.ends_with(".bin") && std::filesystem::is_regular_file(item_path)) {
client_functions_log.debug_f("Adding {} from {}", item_name, item_path);
if (!source_files.emplace(item_name, phosg::load_file(item_path)).second) {
throw std::runtime_error(std::format("Duplicate binary filename: {}", item_name));
}
} else {
client_functions_log.debug_f("Ignoring {}", item_path);
}
}
};
add_directory(root_dir);
std::unordered_map<std::string, std::string> include_cache;
uint32_t last_menu_item_id = 0;
for (const auto& [source_filename, source] : source_files) {
if (!source_filename.ends_with(".s")) {
client_functions_log.debug_f("Skipping root compile for {} because it is not a .s file", source_filename);
continue;
}
if (source_filename.ends_with(".inc.s")) {
client_functions_log.debug_f("Skipping root compile for {} because it is an include", source_filename);
continue;
}
std::unordered_map<uint32_t, std::string> preprocessed;
try {
preprocessed = preprocess_function_code(source);
} catch (const std::exception& e) {
throw std::runtime_error(std::format("({} preprocessing) {}", source_filename, e.what()));
}
if (source_filename.find("Dragon") != std::string::npos) {
client_functions_log.warning_f(
"Dragon preprocess debug: source={} produced {} version chunk(s)",
source_filename,
preprocessed.size());
for (const auto& [debug_sv, debug_source] : preprocessed) {
client_functions_log.warning_f(
"Dragon preprocess debug: source={} sv={} chunk_size={}",
source_filename,
str_for_specific_version(debug_sv),
debug_source.size());
}
}
for (const auto& [specific_version, source] : preprocessed) {
std::shared_ptr<Function> fn = std::make_shared<Function>();
fn->short_name = source_filename.substr(0, source_filename.size() - 2);
fn->specific_version = specific_version;
fn->menu_item_id = ++last_menu_item_id;
fn->arch = architecture_for_specific_version(fn->specific_version);
try {
std::unordered_set<std::string> get_include_stack;
std::function<std::string(const std::string&, uint32_t)> get_include_for_sv = [&include_cache, &source_files, &get_include_stack, &get_include_for_sv](const std::string& name, uint32_t specific_version) -> std::string {
try {
return get_with_sv_fallback(include_cache, name, specific_version);
} catch (const std::out_of_range&) {
}
if (client_functions_log.should_log(phosg::LogLevel::L_DEBUG)) {
client_functions_log.debug_f("({}) Include {}-{} needs to be compiled",
get_include_stack.size(), name, str_for_specific_version(specific_version));
}
auto it = source_files.find(name + ".inc.s");
if (it != source_files.end()) {
if (!get_include_stack.emplace(name).second) {
throw std::runtime_error("Mutual recursion between includes: " + name);
}
for (const auto& [include_specific_version, include_source] : preprocess_function_code(it->second)) {
ResourceDASM::EmulatorBase::AssembleResult ret;
auto get_include = std::bind(get_include_for_sv, std::placeholders::_1, include_specific_version);
switch (architecture_for_specific_version(include_specific_version)) {
case Arch::POWERPC:
ret = ResourceDASM::PPC32Emulator::assemble(include_source, get_include);
break;
case Arch::X86:
ret = ResourceDASM::X86Emulator::assemble(include_source, get_include);
break;
case Arch::SH4:
ret = ResourceDASM::SH4Emulator::assemble(include_source, get_include);
break;
default:
throw std::runtime_error("unknown architecture");
}
if (client_functions_log.should_log(phosg::LogLevel::L_DEBUG)) {
client_functions_log.debug_f("({}) Compiled include {}-{}",
get_include_stack.size(), name, str_for_specific_version(include_specific_version));
}
include_cache.emplace(cache_key(name, include_specific_version), std::move(ret.code));
}
get_include_stack.erase(name);
} else {
it = source_files.find(name + ".inc.bin");
if (it != source_files.end()) {
include_cache.emplace(name, it->second).first->second;
client_functions_log.debug_f("({}) Cached binary include {}", get_include_stack.size(), name);
}
}
try {
return get_with_sv_fallback(include_cache, name, specific_version);
} catch (const std::out_of_range&) {
}
throw std::runtime_error(std::format(
"Data not found for include {} ({})", name, str_for_specific_version(specific_version)));
};
try {
ResourceDASM::EmulatorBase::AssembleResult assembled;
auto get_include = std::bind(get_include_for_sv, std::placeholders::_1, specific_version);
switch (fn->arch) {
case Arch::POWERPC:
assembled = ResourceDASM::PPC32Emulator::assemble(source, get_include);
break;
case Arch::X86:
assembled = ResourceDASM::X86Emulator::assemble(source, get_include);
break;
case Arch::SH4:
assembled = ResourceDASM::SH4Emulator::assemble(source, get_include);
break;
default:
throw std::runtime_error("invalid architecture");
}
fn->code = std::move(assembled.code);
fn->label_offsets = std::move(assembled.label_offsets);
for (const auto& [key, value] : assembled.metadata_keys) {
if (key == "visibility") {
if (value == "hidden") {
fn->visibility = Function::Visibility::DEBUG_ONLY;
} else if (value == "cheat") {
fn->visibility = Function::Visibility::CHAT_COMMAND_ONLY_WITH_CHEAT_MODE;
} else if (value == "chat") {
fn->visibility = Function::Visibility::CHAT_COMMAND_ONLY;
} else if (value == "menu") {
fn->visibility = Function::Visibility::PATCHES_MENU_ONLY;
} else if (value == "all") {
fn->visibility = Function::Visibility::PATCHES_MENU_AND_CHAT_COMMAND;
} else {
throw std::runtime_error("Invalid visibility value");
}
} else if (key == "key") {
fn->short_name = value;
} else if (key == "name") {
fn->long_name = value;
} else if (key == "description") {
fn->description = value;
} else if (key == "client_flag") {
fn->client_flag = stoull(value, nullptr, 0);
} else if (key == "show_return_value") {
fn->show_return_value = true;
} else {
throw std::runtime_error("unknown metadata key: " + key);
}
}
try {
fn->entrypoint_offset_offset = fn->label_offsets.at("entry_ptr");
} catch (const std::out_of_range&) {
throw std::runtime_error("code does not contain entry_ptr label");
}
std::set<uint32_t> reloc_indexes;
for (const auto& it : fn->label_offsets) {
if (it.first.starts_with("reloc")) {
reloc_indexes.emplace(it.second / 4);
}
}
uint32_t prev_index = 0;
for (const auto& it : reloc_indexes) {
uint32_t delta = it - prev_index;
if (delta > 0xFFFF) {
throw std::runtime_error("relocation delta too far away");
}
fn->relocation_deltas.emplace_back(delta);
prev_index = it;
}
} catch (const std::exception& e) {
if (raise_on_any_failure) {
throw;
}
client_functions_log.warning_f("Failed to compile function {} ({}): {}",
fn->short_name, str_for_specific_version(specific_version), e.what());
}
auto key = cache_key(fn->short_name, specific_version);
if (!this->all_functions.emplace(key, fn).second) {
throw std::runtime_error("Duplicate function key: " + key);
}
this->functions_by_specific_version[specific_version].emplace(key, fn);
this->functions_by_menu_item_id.emplace(fn->menu_item_id, fn);
client_functions_log.debug_f("Compiled function {} ({}; {}; {})",
fn->short_name, str_for_specific_version(fn->specific_version), name_for_architecture(fn->arch),
phosg::name_for_enum(fn->visibility));
} catch (const std::exception& e) {
throw std::runtime_error(std::format(
"({}-{}) {}", fn->short_name, str_for_specific_version(specific_version), e.what()));
}
}
}
for (const char* probe_name : {"DragonVisualFix", "PsoPeepsDragonVisualFixPC", "RaresInQuests"}) {
for (uint32_t probe_sv : {0x324F4A57u, SPECIFIC_VERSION_X86_INDETERMINATE}) {
std::string key = cache_key(probe_name, probe_sv);
auto all_it = this->all_functions.find(key);
auto map_it = this->functions_by_specific_version.find(probe_sv);
bool in_version_map = false;
if (map_it != this->functions_by_specific_version.end()) {
in_version_map = map_it->second.count(key);
}
client_functions_log.warning_f(
"Client function probe: name={} sv={} key={} all_functions={} version_map={} map_size={}",
probe_name,
str_for_specific_version(probe_sv),
key,
all_it != this->all_functions.end(),
in_version_map,
map_it == this->functions_by_specific_version.end() ? 0 : map_it->second.size());
if (all_it != this->all_functions.end()) {
const auto& fn = all_it->second;
client_functions_log.warning_f(
"Client function probe detail: short={} long={} visibility={} specific_version={} arch={} menu_item_id={:08X}",
fn->short_name,
fn->long_name,
phosg::name_for_enum(fn->visibility),
str_for_specific_version(fn->specific_version),
name_for_architecture(fn->arch),
static_cast<uint32_t>(fn->menu_item_id));
}
}
}
}
std::shared_ptr<const Menu> ClientFunctionIndex::patch_switches_menu(
uint32_t specific_version,
const std::unordered_set<std::string>& server_auto_patches_enabled,
const std::unordered_set<std::string>& client_auto_patches_enabled) const {
auto ret = std::make_shared<Menu>(MenuID::PATCH_SWITCHES, "Patches");
ret->items.emplace_back(PatchesMenuItemID::GO_BACK, "Go back", "Return to the\nmain menu", 0);
auto map_it = this->functions_by_specific_version.find(specific_version);
if (map_it != this->functions_by_specific_version.end()) {
client_functions_log.warning_f(
"Patch menu debug: building menu for specific_version={} with {} function entries",
str_for_specific_version(specific_version),
map_it->second.size());
for (auto [name, fn] : map_it->second) {
bool appears = fn->appears_in_patches_menu();
bool server_auto = server_auto_patches_enabled.count(fn->short_name);
bool client_enabled = client_auto_patches_enabled.count(fn->short_name);
bool dragon_debug =
(fn->short_name.find("Dragon") != std::string::npos) ||
(fn->long_name.find("Dragon") != std::string::npos);
if (dragon_debug || appears) {
client_functions_log.warning_f(
"Patch menu debug: key={} short={} long={} visibility={} appears={} server_auto={} client_enabled={} menu_item_id={:08X}",
name,
fn->short_name,
fn->long_name,
phosg::name_for_enum(fn->visibility),
appears,
server_auto,
client_enabled,
static_cast<uint32_t>(fn->menu_item_id));
}
if (appears && !server_auto) {
std::string item_text;
item_text.push_back(client_enabled ? '*' : '-');
item_text += fn->long_name.empty() ? fn->short_name : fn->long_name;
ret->items.emplace_back(
fn->menu_item_id, item_text, fn->description, MenuItem::Flag::REQUIRES_SEND_FUNCTION_CALL_RUNS_CODE);
}
}
} else {
client_functions_log.warning_f(
"Patch menu debug: no functions for specific_version={}",
str_for_specific_version(specific_version));
}
return ret;
}
bool ClientFunctionIndex::patch_menu_empty(uint32_t specific_version) const {
uint32_t mask = specific_version_is_indeterminate(specific_version) ? 0xFF000000 : 0xFFFFFFFF;
auto it = this->functions_by_specific_version.lower_bound(specific_version & mask);
return ((it == this->functions_by_specific_version.end()) || ((it->first & mask) != (specific_version & mask)));
}
std::shared_ptr<const ClientFunctionIndex::Function> ClientFunctionIndex::get(
const std::string& name, uint32_t specific_version) const {
return get_with_sv_fallback(this->all_functions, name, specific_version);
}
std::shared_ptr<const ClientFunctionIndex::Function> ClientFunctionIndex::get(
const std::string& name, Arch arch) const {
return get_with_sv_fallback(this->all_functions, name, specific_version_for_architecture(arch));
}
std::shared_ptr<const ClientFunctionIndex::Function> ClientFunctionIndex::get(const std::string& name) const {
return this->all_functions.at(name);
}
std::shared_ptr<const ClientFunctionIndex::Function> ClientFunctionIndex::get_by_menu_item_id(
uint32_t menu_item_id) const {
return this->functions_by_menu_item_id.at(menu_item_id);
}
uint32_t specific_version_for_gc_header_checksum(uint32_t header_checksum) {
static std::unordered_map<uint32_t, uint32_t> checksum_to_specific_version;
if (checksum_to_specific_version.empty()) {
struct {
char system_code = 'G';
char game_code1 = 'P';
char game_code2;
char region_code;
char developer_code1 = '8';
char developer_code2 = 'P';
uint8_t disc_number = 0;
uint8_t version_code;
} __attribute__((packed)) data;
for (const char* game_code2 = "OS"; *game_code2; game_code2++) {
data.game_code2 = *game_code2;
for (const char* region_code = "JEP"; *region_code; region_code++) {
data.region_code = *region_code;
for (uint8_t version_code = 0; version_code < 8; version_code++) {
data.version_code = version_code;
uint32_t checksum = phosg::crc32(&data, sizeof(data));
uint32_t specific_version = 0x33000030 | (*game_code2 << 16) | (*region_code << 8) | version_code;
if (!checksum_to_specific_version.emplace(checksum, specific_version).second) {
throw std::logic_error("multiple specific_versions have same header checksum");
}
}
}
{
// Generate entries for Trial Editions
data.region_code = 'J';
data.system_code = 'D';
data.version_code = 0;
uint32_t checksum = phosg::crc32(&data, sizeof(data));
uint32_t specific_version = 0x33004A54 | (*game_code2 << 16);
if (!checksum_to_specific_version.emplace(checksum, specific_version).second) {
throw std::logic_error("multiple specific_versions have same header checksum");
}
data.system_code = 'G';
}
}
}
return checksum_to_specific_version.at(header_checksum);
}
template <>
const char* phosg::name_for_enum<ClientFunctionIndex::Function::Visibility>(
ClientFunctionIndex::Function::Visibility vis) {
switch (vis) {
case ClientFunctionIndex::Function::Visibility::DEBUG_ONLY:
return "DEBUG_ONLY";
case ClientFunctionIndex::Function::Visibility::CHAT_COMMAND_ONLY_WITH_CHEAT_MODE:
return "CHAT_COMMAND_ONLY_WITH_CHEAT_MODE";
case ClientFunctionIndex::Function::Visibility::CHAT_COMMAND_ONLY:
return "CHAT_COMMAND_ONLY";
case ClientFunctionIndex::Function::Visibility::PATCHES_MENU_ONLY:
return "PATCHES_MENU_ONLY";
case ClientFunctionIndex::Function::Visibility::PATCHES_MENU_AND_CHAT_COMMAND:
return "PATCHES_MENU_AND_CHAT_COMMAND";
default:
throw std::logic_error("Invalid client function visibility");
}
}
+96
View File
@@ -0,0 +1,96 @@
#pragma once
#include <inttypes.h>
#include <map>
#include <memory>
#include <phosg/Strings.hh>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include "Menu.hh"
class ClientFunctionIndex {
public:
struct Function {
enum class Architecture {
UNKNOWN = 0,
POWERPC, // GC
X86, // PC, XB, BB
SH4, // Dreamcast
};
Architecture arch = Architecture::UNKNOWN;
std::string code;
std::vector<uint16_t> relocation_deltas;
std::unordered_map<std::string, uint32_t> label_offsets;
uint32_t entrypoint_offset_offset = 0;
std::string short_name; // Based on filename
std::string long_name; // From .meta name directive
std::string description; // From .meta description directive
uint64_t client_flag = 0; // From .meta client_flag directive
uint32_t menu_item_id = 0;
enum class Visibility {
DEBUG_ONLY = 0,
CHAT_COMMAND_ONLY_WITH_CHEAT_MODE,
CHAT_COMMAND_ONLY,
PATCHES_MENU_ONLY,
PATCHES_MENU_AND_CHAT_COMMAND,
};
Visibility visibility;
bool show_return_value = false;
uint32_t specific_version;
inline bool appears_in_patches_menu() const {
return (this->visibility == Visibility::PATCHES_MENU_ONLY) ||
(this->visibility == Visibility::PATCHES_MENU_AND_CHAT_COMMAND);
}
inline bool allowed_via_chat_command(bool cheat_mode_enabled) const {
return (cheat_mode_enabled && (this->visibility == Visibility::CHAT_COMMAND_ONLY_WITH_CHEAT_MODE)) ||
(this->visibility == Visibility::CHAT_COMMAND_ONLY) ||
(this->visibility == Visibility::PATCHES_MENU_AND_CHAT_COMMAND);
}
inline bool is_big_endian() const {
return (this->arch == Architecture::POWERPC);
}
template <bool BE>
std::string generate_client_command_t(
const std::unordered_map<std::string, uint32_t>& label_writes,
const void* suffix_data = nullptr,
size_t suffix_size = 0,
uint32_t override_relocations_offset = 0) const;
std::string generate_client_command(
const std::unordered_map<std::string, uint32_t>& label_writes = {},
const void* suffix_data = nullptr,
size_t suffix_size = 0,
uint32_t override_relocations_offset = 0) const;
};
ClientFunctionIndex() = default;
ClientFunctionIndex(const std::string& directory, bool raise_on_any_failure);
std::unordered_map<std::string, std::shared_ptr<Function>> all_functions; // Key is "PatchName-SpecificVersion"
std::map<uint32_t, std::map<std::string, std::shared_ptr<Function>>> functions_by_specific_version;
std::map<uint32_t, std::shared_ptr<Function>> functions_by_menu_item_id;
std::shared_ptr<const Menu> patch_switches_menu(
uint32_t specific_version,
const std::unordered_set<std::string>& server_auto_patches_enabled,
const std::unordered_set<std::string>& client_auto_patches_enabled) const;
bool patch_menu_empty(uint32_t specific_version) const;
std::shared_ptr<const Function> get(const std::string& name, uint32_t specific_version) const;
std::shared_ptr<const Function> get(const std::string& name, Function::Architecture arch) const;
std::shared_ptr<const Function> get(const std::string& name) const;
std::shared_ptr<const Function> get_by_menu_item_id(uint32_t menu_item_id) const;
};
const char* name_for_architecture(ClientFunctionIndex::Function::Architecture arch);
uint32_t specific_version_for_gc_header_checksum(uint32_t header_checksum);
template <>
const char* phosg::name_for_enum<ClientFunctionIndex::Function::Visibility>(
ClientFunctionIndex::Function::Visibility vis);
+241
View File
@@ -0,0 +1,241 @@
#include "CommandCensorData.hh"
#include "CommandFormats.hh"
std::pair<const void*, size_t> censor_data_for_client_command(Version version, uint16_t command) {
switch (command) {
case 0x03: {
static const C_LegacyLogin_PC_V3_03 ret{
.hardware_id = 0,
.sub_version = 0,
.is_extended = 0,
.language = Language::JAPANESE,
.unused = 0,
.serial_number2 = 1,
.access_key2 = 1,
};
return std::make_pair(&ret, sizeof(ret));
}
case 0x04:
if (is_patch(version)) {
static const C_Login_Patch_04 ret{.unused{0}, .username{1}, .password{1}, .email_address{1}};
return std::make_pair(&ret, sizeof(ret));
} else if (!is_v4(version)) {
static const C_LegacyLogin_PC_V3_04 ret{
.hardware_id = 0,
.sub_version = 0,
.is_extended = 0,
.language = Language::JAPANESE,
.unused = 0,
.serial_number = 1,
.access_key = 1,
};
return std::make_pair(&ret, sizeof(ret));
} else {
static const C_LegacyLogin_BB_04 ret{
.sub_version = 0,
.is_extended = 0,
.language = Language::JAPANESE,
.unused = 0,
.username = 1,
.password = 1};
return std::make_pair(&ret, sizeof(ret));
}
case 0x88:
if (version == Version::DC_NTE) {
static const C_Login_DCNTE_88 ret{.serial_number{1}, .access_key{1}};
return std::make_pair(&ret, sizeof(ret));
} else {
return std::make_pair(nullptr, 0);
}
case 0x8A:
if (version == Version::DC_NTE) {
static const C_ConnectionInfo_DCNTE_8A ret{
.hardware_id = 0, .sub_version = 0, .unused = 0, .username = 1, .password = 1, .email_address = 1};
return std::make_pair(&ret, sizeof(ret));
} else {
return std::make_pair(nullptr, 0);
}
case 0x8B:
if (version == Version::DC_NTE) {
static const C_Login_DCNTE_8B ret{
.player_tag = 0,
.guild_card_number = 0,
.hardware_id = 0,
.sub_version = 0,
.is_extended = 0,
.language = Language::JAPANESE,
.unused1 = 0,
.serial_number = 1,
.access_key = 1,
.username = 1,
.password = 1,
.login_character_name = 0,
.unused = 0,
};
return std::make_pair(&ret, sizeof(ret));
} else {
return std::make_pair(nullptr, 0);
}
case 0x90: {
static const C_LoginV1_DC_PC_V3_90 ret{.serial_number = 1, .access_key = 1};
return std::make_pair(&ret, sizeof(ret));
}
case 0x92: {
static const C_RegisterV1_DC_92 ret{
.hardware_id = 0,
.sub_version = 0,
.unused1 = 0,
.language = Language::JAPANESE,
.unused2 = 0,
.serial_number2 = 1,
.access_key2 = 1,
.email_address = 1,
};
return std::make_pair(&ret, sizeof(ret));
}
case 0x93:
if (!is_v4(version)) {
static const C_LoginV1_DC_93 ret{
.player_tag = 0,
.guild_card_number = 0,
.hardware_id = 0,
.sub_version = 0,
.is_extended = 0,
.language = Language::JAPANESE,
.unused1 = 0,
.serial_number = 1,
.access_key = 1,
.serial_number2 = 1,
.access_key2 = 1,
.login_character_name = 0,
.unused2 = 0,
};
return std::make_pair(&ret, sizeof(ret));
} else {
static const C_LoginBase_BB_93 ret{
.player_tag = 0,
.guild_card_number = 0,
.sub_version = 0,
.language = Language::JAPANESE,
.character_slot = 0,
.connection_phase = 0,
.client_code = 0,
.security_token = 0,
.username = 1,
.password = 1,
.menu_id = 0,
.preferred_lobby_id = 0,
};
return std::make_pair(&ret, sizeof(ret));
}
case 0x9A: {
static const C_Login_DC_PC_V3_9A ret{
.v1_serial_number = 1,
.v1_access_key = 1,
.serial_number = 1,
.access_key = 1,
.player_tag = 0,
.guild_card_number = 0,
.sub_version = 0,
.serial_number2 = 1,
.access_key2 = 1,
.email_address = 1,
};
return std::make_pair(&ret, sizeof(ret));
}
case 0x9C:
if (!is_v4(version)) {
static const C_Register_DC_PC_V3_9C ret{
.hardware_id = 0,
.sub_version = 0,
.unused1 = 0,
.language = Language::JAPANESE,
.unused2 = 0,
.serial_number = 1,
.access_key = 1,
.password = 1,
};
return std::make_pair(&ret, sizeof(ret));
} else {
static const C_Register_BB_9C ret{
.sub_version = 0,
.unused1 = 0,
.language = Language::JAPANESE,
.unused2 = 0,
.username = 1,
.password = 1,
.game_tag = 0,
};
return std::make_pair(&ret, sizeof(ret));
}
case 0x9D:
case 0x9E:
if (!is_v4(version)) {
static const C_Login_DC_PC_GC_9D ret{
.player_tag = 0,
.guild_card_number = 0,
.hardware_id = 0,
.sub_version = 0,
.is_extended = 0,
.language = Language::JAPANESE,
.unused3 = 0,
.v1_serial_number = 1,
.v1_access_key = 1,
.serial_number = 1,
.access_key = 1,
.serial_number2 = 1,
.access_key2 = 1,
.login_character_name = 0,
};
return std::make_pair(&ret, sizeof(ret));
} else {
static const C_LoginExtended_BB_9E ret{
.player_tag = 0,
.guild_card_number = 0,
.sub_version = 0,
.language32 = 0,
.unknown_a2 = 0,
.v1_serial_number = 1,
.v1_access_key = 1,
.serial_number = 1,
.access_key = 1,
.username = 1,
.password = 1,
.guild_card_number_str = 0,
.client_config = 0,
.extension{},
};
return std::make_pair(&ret, sizeof(ret));
}
case 0xDB:
if (!is_v4(version)) {
static const C_VerifyAccount_V3_DB ret{
.v1_serial_number = 1,
.v1_access_key = 1,
.serial_number = 1,
.access_key = 1,
.hardware_id = 0,
.sub_version = 0,
.serial_number2 = 1,
.access_key2 = 1,
.password = 1,
};
return std::make_pair(&ret, sizeof(ret));
} else {
static const C_VerifyAccount_BB_DB ret{
.v1_serial_number = 1,
.v1_access_key = 1,
.serial_number = 1,
.access_key = 1,
.sub_version = 0,
.username = 1,
.password = 1,
.game_tag = 0,
};
return std::make_pair(&ret, sizeof(ret));
}
default:
return std::make_pair(nullptr, 0);
}
}
+9
View File
@@ -0,0 +1,9 @@
#pragma once
#include <stdint.h>
#include <utility>
#include "Version.hh"
std::pair<const void*, size_t> censor_data_for_client_command(Version version, uint16_t command);
+79 -55
View File
@@ -257,7 +257,7 @@ struct S_ReconnectT {
U16T<BE> port = 0;
le_uint16_t unused = 0;
} __packed_ws_be__(S_ReconnectT, 0x08);
using S_Reconnect_Patch_14 = S_ReconnectT<false>;
using S_Reconnect_Patch_14 = S_ReconnectT<true>;
// 15 (S->C): Login failure
// No arguments. The client shows a message like "Incorrect game ID or password" and disconnects.
@@ -318,7 +318,7 @@ struct S_ServerInitWithAfterMessageT_DC_PC_V3_02_17_91_9B {
// Internal name: SndRegist
struct C_LegacyLogin_PC_V3_03 {
/* 00 */ be_uint64_t hardware_id;
/* 00 */ be_uint64_t hardware_id = 0;
/* 08 */ le_uint32_t sub_version = 0;
/* 0C */ uint8_t is_extended = 0;
/* 0D */ Language language = Language::JAPANESE;
@@ -364,7 +364,7 @@ struct S_ServerInitWithAfterMessageT_BB_03_9B {
// likely a relic of an older, now-unused sequence. Like 03, this command isn't used by any known PSO version.
struct C_LegacyLogin_PC_V3_04 {
/* 00 */ be_uint64_t hardware_id;
/* 00 */ be_uint64_t hardware_id = 0;
/* 08 */ le_uint32_t sub_version = 0;
/* 0C */ uint8_t is_extended = 0;
/* 0D */ Language language = Language::JAPANESE;
@@ -412,18 +412,18 @@ struct S_UpdateClientConfig_DC_PC_04 {
le_uint32_t guild_card_number = 0;
} __packed_ws__(S_UpdateClientConfig_DC_PC_04, 8);
struct S_UpdateClientConfig_V3_04 {
template <size_t ClientConfigBytes>
struct S_UpdateClientConfigT_V3_BB_04 {
le_uint32_t player_tag = 0x00010000;
le_uint32_t guild_card_number = 0;
// This field is opaque to the client; it will send back the contents verbatim in subsequent 9E or 9F commands.
parray<uint8_t, 0x20> client_config;
} __packed_ws__(S_UpdateClientConfig_V3_04, 0x28);
parray<uint8_t, ClientConfigBytes> client_config;
} __attribute__((packed));
struct S_UpdateClientConfig_BB_04 {
le_uint32_t player_tag = 0x00010000;
le_uint32_t guild_card_number = 0;
parray<uint8_t, 0x28> client_config;
} __packed_ws__(S_UpdateClientConfig_BB_04, 0x30);
using S_UpdateClientConfig_V3_04 = S_UpdateClientConfigT_V3_BB_04<0x20>;
using S_UpdateClientConfig_BB_04 = S_UpdateClientConfigT_V3_BB_04<0x28>;
check_struct_size(S_UpdateClientConfig_V3_04, 0x28);
check_struct_size(S_UpdateClientConfig_BB_04, 0x30);
// 05: Disconnect
// Internal name: SndLogout
@@ -1053,13 +1053,13 @@ struct PlayerRecordsEntry_BB {
struct C_CharacterData_DCv1_61_98 {
/* 0000 */ PlayerInventory inventory;
/* 034C */ PlayerDispDataDCPCV3 disp;
/* 034C */ PlayerDispDataV123 disp;
/* 041C */
} __packed_ws__(C_CharacterData_DCv1_61_98, 0x041C);
struct C_CharacterData_DCv2_61_98 {
/* 0000 */ PlayerInventory inventory;
/* 034C */ PlayerDispDataDCPCV3 disp;
/* 034C */ PlayerDispDataV123 disp;
/* 041C */ PlayerRecordsEntry_DC records;
/* 04D8 */ ChoiceSearchConfig choice_search_config;
/* 04F0 */
@@ -1067,7 +1067,7 @@ struct C_CharacterData_DCv2_61_98 {
struct C_CharacterData_PC_61_98 {
/* 0000 */ PlayerInventory inventory;
/* 034C */ PlayerDispDataDCPCV3 disp;
/* 034C */ PlayerDispDataV123 disp;
/* 041C */ PlayerRecordsEntry_PC records;
/* 0510 */ ChoiceSearchConfig choice_search_config;
/* 0528 */ parray<le_uint32_t, 0x1E> blocked_senders;
@@ -1079,7 +1079,7 @@ struct C_CharacterData_PC_61_98 {
struct C_CharacterData_GCNTE_61_98 {
/* 0000 */ PlayerInventory inventory;
/* 034C */ PlayerDispDataDCPCV3 disp;
/* 034C */ PlayerDispDataV123 disp;
/* 041C */ PlayerRecordsEntry_DC records;
/* 04D8 */ ChoiceSearchConfig choice_search_config;
/* 04F0 */ parray<le_uint32_t, 0x1E> blocked_senders;
@@ -1091,7 +1091,7 @@ struct C_CharacterData_GCNTE_61_98 {
struct C_CharacterData_V3_61_98 {
/* 0000 */ PlayerInventory inventory;
/* 034C */ PlayerDispDataDCPCV3 disp;
/* 034C */ PlayerDispDataV123 disp;
/* 041C */ PlayerRecordsEntry_V3 records;
/* 0538 */ ChoiceSearchConfig choice_search_config;
/* 0550 */ pstring<TextEncoding::MARKED, 0xAC> info_board;
@@ -1104,7 +1104,7 @@ struct C_CharacterData_V3_61_98 {
struct C_CharacterData_Ep3_61_98 {
/* 0000 */ PlayerInventory inventory;
/* 034C */ PlayerDispDataDCPCV3 disp;
/* 034C */ PlayerDispDataV123 disp;
/* 041C */ PlayerRecordsEntry_V3 records;
/* 0538 */ ChoiceSearchConfig choice_search_config;
/* 0550 */ pstring<TextEncoding::MARKED, 0xAC> info_board;
@@ -1117,7 +1117,7 @@ struct C_CharacterData_Ep3_61_98 {
struct C_CharacterData_BB_61_98 {
/* 0000 */ PlayerInventory inventory;
/* 034C */ PlayerDispDataBB disp;
/* 034C */ PlayerDispDataV4 disp;
/* 04DC */ PlayerRecordsEntry_BB records;
/* 0638 */ ChoiceSearchConfig choice_search_config;
/* 0650 */ pstring<TextEncoding::UTF16, 0xAC> info_board;
@@ -1139,17 +1139,37 @@ struct C_CharacterData_BB_61_98 {
// 64 (S->C): Join game
// Internal name: RcvStartGame3
// This is sent to the joining player; the other players get a 65 instead. Note that (except on Episode 3) this command
// does not include the player's disp or inventory data. The clients in the game are responsible for sending that data
// to each other during the join process with 60/62/6C/6D commands.
// After receiving a 64 command, the client starts the game loading procedure, during which it will completely ignore
// other 64 or 65 commands, and will delay processing of all other commands except 1D until loading is done. If more
// than 0x10000 bytes of commands are sent during loading, any commands that don't fit in the buffer are lost.
// Curiously, this command is named RcvStartGame3 internally, while 0E is named RcvStartGame. The string RcvStartGame2
// appears in the DC versions, but it seems the relevant code was deleted - there are no references to the string.
// The game joining procedure goes as follows:
// 1. The server sends 64 to the joining player, and 65 to all other players in the game. This pauses the game and
// brings up the "please wait" message box for all players. The joining player unloads the lobby assets and begins
// loading Pioneer 2 assets. On v2 and later, the joining player sends 8A near the beginning of this procedure.
// During this time, the joining player will completely ignore other 64 or 65 commands, and will delay processing of
// all other commands except 1D until loading is done. If more than 0x10000 bytes of commands are sent during
// loading, any commands that don't fit in the buffer are lost.
// 2. If the joining player is not the only player in the game:
// a. If the leader is DC v1 or later, the leader sends 6x6D (item state).
// b. The leader sends 6x6B (enemy state).
// c. The leader sends 6x6C (object state).
// d. If the leader is DC NTE or DC 11/2000, the leader sends 6x6D (item state).
// e. The leader sends 6x6E (set flag state).
// f. If the leader is DC v1 or later, the leader sends 6x6F (quest flag state).
// g. If the leader is DC v1 or later, the leader sends 6x71 (construct player).
// h. All players except the joining player send 6x70 to the joining player. (This is not synchronized; non-leader
// players do not wait for any of the above things to happen, so their 6x70 commands may be interleaved with the
// preceding commands from the leader, or may arrive after the following 6x72.) Character data is sent in a
// different format here (6x70 instead of 65) because it contains ephemeral fields that the server doesn't know
// about - things like current HP, state, game flags, player flags, etc. which are not present in 65.
// i. If the leader is not BB, the leader sends 6x72 to all players, which resumes the game. If the leader is BB,
// the server is responsible for sending 6x72, and should do so here. (There is no sequence-breaking risk here,
// since the 6x72 sent to other players will always be ordered after the 65 from the server, so they will always
// send a 6x70 containing their player state at the time the game was paused.)
// 3. Once the joining player has fully loaded, the player processes all the commands sent during loading, which sets
// up the game state and constructs all the other players. Once the local player is constructed, the joining player
// sends 6F, which notifies the server that it can unlock the game and allow more players to join.
// Header flag = entry count
template <typename LobbyDataT>
struct S_JoinGameT_DC_PC {
@@ -1195,7 +1215,7 @@ struct S_JoinGame_Ep3_64 : S_JoinGame_GC_64 {
// four of these are always present and they are filled in in slot positions.
struct Ep3PlayerEntry {
PlayerInventory inventory;
PlayerDispDataDCPCV3 disp;
PlayerDispDataV123 disp;
} __packed_ws__(Ep3PlayerEntry, 0x41C);
parray<Ep3PlayerEntry, 4> players_ep3;
} __packed_ws__(S_JoinGame_Ep3_64, 0x1180);
@@ -1272,10 +1292,10 @@ struct S_JoinLobbyT {
return offsetof(S_JoinLobbyT, entries) + used_entries * sizeof(Entry);
}
} __attribute__((packed));
using S_JoinLobby_DCNTE_65_67_68 = S_JoinLobbyT<LobbyFlagsDCNTE, PlayerLobbyDataDCGC, PlayerDispDataDCPCV3>;
using S_JoinLobby_PC_65_67_68 = S_JoinLobbyT<LobbyFlags, PlayerLobbyDataPC, PlayerDispDataDCPCV3>;
using S_JoinLobby_DC_GC_65_67_68_Ep3_EB = S_JoinLobbyT<LobbyFlags, PlayerLobbyDataDCGC, PlayerDispDataDCPCV3>;
using S_JoinLobby_BB_65_67_68 = S_JoinLobbyT<LobbyFlags, PlayerLobbyDataBB, PlayerDispDataBB>;
using S_JoinLobby_DCNTE_65_67_68 = S_JoinLobbyT<LobbyFlagsDCNTE, PlayerLobbyDataDCGC, PlayerDispDataV123>;
using S_JoinLobby_PC_65_67_68 = S_JoinLobbyT<LobbyFlags, PlayerLobbyDataPC, PlayerDispDataV123>;
using S_JoinLobby_DC_GC_65_67_68_Ep3_EB = S_JoinLobbyT<LobbyFlags, PlayerLobbyDataDCGC, PlayerDispDataV123>;
using S_JoinLobby_BB_65_67_68 = S_JoinLobbyT<LobbyFlags, PlayerLobbyDataBB, PlayerDispDataV4>;
check_struct_size(S_JoinLobby_DCNTE_65_67_68, 0x32D4);
check_struct_size(S_JoinLobby_PC_65_67_68, 0x339C);
check_struct_size(S_JoinLobby_DC_GC_65_67_68_Ep3_EB, 0x32DC);
@@ -1287,7 +1307,7 @@ struct S_JoinLobby_XB_65_67_68 {
struct Entry {
PlayerLobbyDataXB lobby_data;
PlayerInventory inventory;
PlayerDispDataDCPCV3 disp;
PlayerDispDataV123 disp;
} __packed_ws__(Entry, 0x468);
// Note: not all of these will be filled in and sent if the lobby isn't full (the command size will be shorter than
// this struct's size)
@@ -1509,7 +1529,7 @@ struct S_ArrowUpdateEntry_88 {
// The server should respond with an 8A command.
struct C_ConnectionInfo_DCNTE_8A {
be_uint64_t hardware_id;
be_uint64_t hardware_id = 0;
le_uint32_t sub_version = 0x20;
le_uint32_t unused = 0;
pstring<TextEncoding::ASCII, 0x30> username;
@@ -1521,10 +1541,10 @@ struct C_ConnectionInfo_DCNTE_8A {
// header.flag is a success flag. If it's zero, the client shows an error message and disconnects. Otherwise, the
// client responds with an 8B command.
// 8A (C->S): Request lobby/game name (except DC NTE)
// 8A (C->S): Request lobby/game name (DCv2 and later)
// No arguments.
// 8A (S->C): Lobby/game name (except DC NTE)
// 8A (S->C): Lobby/game name (DCv2 and later)
// Contents is a string containing the lobby or game name. All versions after DCv1 send an 8A command to request the
// team name after joining a game. The response is used to handle the team_name token in quest strings, and appears in
// some Challenge Mode information windows.
@@ -1536,7 +1556,7 @@ struct C_ConnectionInfo_DCNTE_8A {
struct C_Login_DCNTE_8B {
le_uint32_t player_tag = 0x00010000;
le_uint32_t guild_card_number = 0;
be_uint64_t hardware_id;
be_uint64_t hardware_id = 0;
le_uint32_t sub_version = 0x20;
uint8_t is_extended = 0;
Language language = Language::JAPANESE;
@@ -1589,7 +1609,7 @@ struct C_LoginV1_DC_PC_V3_90 {
// 92 (C->S): Register (DC)
struct C_RegisterV1_DC_92 {
be_uint64_t hardware_id;
be_uint64_t hardware_id = 0;
le_uint32_t sub_version;
uint8_t unused1 = 0;
Language language = Language::JAPANESE;
@@ -1608,7 +1628,7 @@ struct C_RegisterV1_DC_92 {
struct C_LoginV1_DC_93 {
/* 00 */ le_uint32_t player_tag = 0x00010000;
/* 04 */ le_uint32_t guild_card_number = 0;
/* 08 */ be_uint64_t hardware_id;
/* 08 */ be_uint64_t hardware_id = 0;
/* 10 */ le_uint32_t sub_version = 0;
/* 14 */ uint8_t is_extended = 0;
/* 15 */ Language language = Language::JAPANESE;
@@ -1662,7 +1682,7 @@ struct C_LoginWithoutHardwareInfo_BB_93 : C_LoginBase_BB_93 {
struct C_LoginWithHardwareInfo_BB_93 : C_LoginBase_BB_93 {
// See the comment in the above structure. This format is used on newer client versions.
/* 7C */ be_uint64_t hardware_id;
/* 7C */ be_uint64_t hardware_id = 0;
/* 84 */ parray<uint8_t, 0x28> client_config;
/* AC */
} __packed_ws__(C_LoginWithHardwareInfo_BB_93, 0xAC);
@@ -1773,7 +1793,7 @@ struct C_Login_DC_PC_V3_9A {
// It appears PSO GC sends uninitialized data in the header.flag field here.
struct C_Register_DC_PC_V3_9C {
/* 00 */ be_uint64_t hardware_id;
/* 00 */ be_uint64_t hardware_id = 0;
/* 08 */ le_uint32_t sub_version = 0;
/* 0C */ uint8_t unused1 = 0;
/* 0D */ Language language = Language::JAPANESE;
@@ -1819,7 +1839,7 @@ struct C_Login_DC_PC_GC_9D {
// other bytes are all zeroes.
// - V3: the hardware ID is all zeroes.
// On the client, this is actually an array of 8 bytes, but we treat it as a single integer for simplicity.
/* 08 */ be_uint64_t hardware_id;
/* 08 */ be_uint64_t hardware_id = 0;
/* 10 */ le_uint32_t sub_version = 0;
/* 14 */ uint8_t is_extended = 0; // If 1, structure has extended format
/* 15 */ Language language = Language::JAPANESE;
@@ -2623,7 +2643,7 @@ struct C_GuildCardDataRequest_BB_03DC {
// DD (S->C): Send quest state to joining player (BB)
// When a player joins a game with a quest already in progress, the server should send this command to the leader.
// No arguments except header.flag, which is the client ID that the leader should send quest state to. The leader will
// then send a series of target commands (62/6D) that the server can forward to the joining player.
// then send 6x6D, 6x6B, 6x6C, and 6x6E, in that order, targeted at the client specified in header.flag.
// DE (S->C): Rare monster list (BB)
@@ -2928,7 +2948,7 @@ struct S_CardBattleTableConfirmation_Ep3_E5 {
struct SC_PlayerPreview_CreateCharacter_BB_00E5 {
le_int32_t character_index = 0;
PlayerDispDataBBPreview preview;
PlayerDispDataV4Preview preview;
} __packed_ws__(SC_PlayerPreview_CreateCharacter_BB_00E5, 0x80);
// E6 (C->S): Spectator team control (Episode 3)
@@ -3014,7 +3034,7 @@ struct S_JoinSpectatorTeam_Ep3_E8 {
struct PlayerEntry {
/* 0000 */ PlayerLobbyDataDCGC lobby_data;
/* 0020 */ PlayerInventory inventory;
/* 036C */ PlayerDispDataDCPCV3 disp;
/* 036C */ PlayerDispDataV123 disp;
/* 043C */
} __packed_ws__(PlayerEntry, 0x43C);
/* 0080 */ parray<PlayerEntry, 4> players;
@@ -4269,7 +4289,9 @@ struct G_Attack_6x43_6x44_6x45 {
le_uint16_t unknown_a2 = 0;
} __packed_ws__(G_Attack_6x43_6x44_6x45, 8);
// 6x46: Attack finished (sent after each of 43, 44, and 45) (protected on GC NTE/V3/V4)
// 6x46: Set attack strike targets (sent after each of 43, 44, and 45) (protected on GC NTE/V3/V4)
// This command sets the targets of each strike of an attack (e.g. each pair of mechgun bullets, or each swing of a
// pair of daggers). For multi-strike attacks, this is sent multiple times.
// The number of targets is not bounds-checked during byteswapping on GC clients. The client only expects up to 10
// entries here, so if the number of targets is too large, the client will byteswap the function's return address on
// the stack, and it will crash.
@@ -4739,7 +4761,7 @@ struct G_SyncPlayerDispAndInventory_DCNTE_6x70 {
/* 0054 */ PlayerHoldState_DCProtos hold_state;
/* 0064 */ le_uint32_t area = 0;
/* 0068 */ le_uint32_t player_flags = 0;
/* 006C */ PlayerVisualConfig visual;
/* 006C */ PlayerVisualConfigV123 visual;
/* 00BC */ PlayerStats stats;
/* 00E0 */ le_uint32_t num_items = 0;
/* 00E4 */ parray<PlayerInventoryItem, 0x1E> items;
@@ -4757,7 +4779,7 @@ struct G_SyncPlayerDispAndInventory_DC112000_6x70 {
/* 0060 */ PlayerHoldState_DCProtos hold_state;
/* 0070 */ le_uint32_t area = 0;
/* 0074 */ le_uint32_t player_flags = 0;
/* 0078 */ PlayerVisualConfig visual;
/* 0078 */ PlayerVisualConfigV123 visual;
/* 00C8 */ PlayerStats stats;
/* 00EC */ le_uint32_t num_items = 0;
/* 00F0 */ parray<PlayerInventoryItem, 0x1E> items;
@@ -4788,13 +4810,13 @@ struct G_6x70_Base_V1 {
/* 00AC */ le_uint32_t area = 0;
/* 00B0 */ le_uint32_t player_flags = 0;
/* 00B4 */ parray<uint8_t, 0x14> technique_levels_v1 = 0xFF; // Last byte is uninitialized
/* 00C8 */ PlayerVisualConfig visual;
/* 0118 */
} __packed_ws__(G_6x70_Base_V1, 0x118);
/* 00C8 */
} __packed_ws__(G_6x70_Base_V1, 0xC8);
struct G_SyncPlayerDispAndInventory_DC_PC_6x70 {
/* 0000 */ G_ExtendedHeaderT<G_ClientIDHeader> header = {{0x70, 0x00, 0x0000}, sizeof(G_SyncPlayerDispAndInventory_DC_PC_6x70)};
/* 0008 */ G_6x70_Base_V1 base;
/* 00D0 */ PlayerVisualConfigV123 visual;
/* 0120 */ PlayerStats stats;
/* 0144 */ le_uint32_t num_items = 0;
/* 0148 */ parray<PlayerInventoryItem, 0x1E> items;
@@ -4805,6 +4827,7 @@ struct G_SyncPlayerDispAndInventory_DC_PC_6x70 {
struct G_SyncPlayerDispAndInventory_GC_6x70 {
/* 0000 */ G_ExtendedHeaderT<G_ClientIDHeader> header = {{0x70, 0x00, 0x0000}, sizeof(G_SyncPlayerDispAndInventory_GC_6x70)};
/* 0008 */ G_6x70_Base_V1 base;
/* 00D0 */ PlayerVisualConfigV123 visual;
/* 0120 */ PlayerStats stats;
/* 0144 */ le_uint32_t num_items = 0;
/* 0148 */ parray<PlayerInventoryItem, 0x1E> items;
@@ -4815,6 +4838,7 @@ struct G_SyncPlayerDispAndInventory_GC_6x70 {
struct G_SyncPlayerDispAndInventory_XB_6x70 {
/* 0000 */ G_ExtendedHeaderT<G_ClientIDHeader> header = {{0x70, 0x00, 0x0000}, sizeof(G_SyncPlayerDispAndInventory_XB_6x70)};
/* 0008 */ G_6x70_Base_V1 base;
/* 00D0 */ PlayerVisualConfigV123 visual;
/* 0120 */ PlayerStats stats;
/* 0144 */ le_uint32_t num_items = 0;
/* 0148 */ parray<PlayerInventoryItem, 0x1E> items;
@@ -4828,7 +4852,7 @@ struct G_SyncPlayerDispAndInventory_XB_6x70 {
struct G_SyncPlayerDispAndInventory_BB_6x70 {
/* 0000 */ G_ExtendedHeaderT<G_ClientIDHeader> header = {{0x70, 0x00, 0x0000}, sizeof(G_SyncPlayerDispAndInventory_BB_6x70)};
/* 0008 */ G_6x70_Base_V1 base;
/* 0120 */ pstring<TextEncoding::UTF16_ALWAYS_MARKED, 0x10> name;
/* 00D0 */ PlayerVisualConfigV4 visual;
/* 0140 */ PlayerStats stats;
/* 0164 */ le_uint32_t num_items = 0;
/* 0168 */ parray<PlayerInventoryItem, 0x1E> items;
@@ -5198,10 +5222,10 @@ struct G_SetChallengeTime_6x95 {
struct G_SelectChallengeModeFailureOption_6x97 {
G_UnusedHeader header;
le_uint32_t unused1 = 0;
le_uint32_t client_id = 0;
le_uint32_t is_retry = 0;
le_uint32_t unused1 = 0;
le_uint32_t unused2 = 0;
le_uint32_t unused3 = 0;
} __packed_ws__(G_SelectChallengeModeFailureOption_6x97, 0x14);
// 6x98: Unknown
@@ -5229,7 +5253,7 @@ struct G_UpdateEntityStat_6x9A {
// Used in battle mode if the rules specify that techniques should level up upon character death.
struct G_LevelUpAllTechniques_6x9B {
G_UnusedHeader header;
G_ClientIDHeader header;
uint8_t num_levels = 0;
parray<uint8_t, 3> unused;
} __packed_ws__(G_LevelUpAllTechniques_6x9B, 8);
@@ -5745,7 +5769,7 @@ struct G_ChangeLobbyMusic_Ep3_6xBF {
// 6xBF: Give EXP (BB) (server->client only)
// newserv implements an extension that causes this command to show the purple EXP numbers which are normally generated
// by the client instead. This requires the server to also send the enemy ID that generated the EXP, hence the
// extension struct here. See ServerEXPDisplay.59NL.patch.s for details.
// extension struct here. See ServerEXPDisplay.s for details.
struct G_GiveExperience_BB_6xBF {
G_ClientIDHeader header;
+113
View File
@@ -1,7 +1,9 @@
#pragma once
#include <phosg/Encoding.hh>
#include <phosg/Strings.hh>
#include <phosg/Vector.hh>
#include <set>
#include "Text.hh"
@@ -160,3 +162,114 @@ struct RELFileFooterT {
} __packed_ws_be__(RELFileFooterT, 0x20);
using RELFileFooter = RELFileFooterT<false>;
using RELFileFooterBE = RELFileFooterT<true>;
template <bool BE>
std::set<uint32_t> all_relocation_offsets_for_rel_file(const void* data, size_t size) {
phosg::StringReader r(data, size);
std::set<uint32_t> ret;
ret.emplace(r.size() - 0x20); // REL footer
ret.emplace(r.pget<U32T<BE>>(r.size() - 0x10)); // root
ret.emplace(r.pget<U32T<BE>>(r.size() - 0x20)); // relocations
const auto& footer = r.pget<RELFileFooterT<BE>>(r.size() - sizeof(RELFileFooterT<BE>));
auto sub_r = r.sub(footer.relocations_offset, footer.num_relocations * sizeof(U16T<BE>));
uint32_t offset = 0;
while (!sub_r.eof()) {
offset += sub_r.template get<U16T<BE>>() * 4;
ret.emplace(r.pget<U32T<BE>>(offset));
}
return ret;
}
template <typename T>
size_t get_rel_array_count(const std::set<uint32_t>& offsets, size_t start_offset) {
auto it = offsets.lower_bound(start_offset);
if (it == offsets.end()) {
throw std::out_of_range("start offset out of range");
}
if (*it == start_offset) {
it++;
}
if (it == offsets.end()) {
throw std::out_of_range("no further offset beyond start offset");
}
return (*it - start_offset) / sizeof(T);
}
template <bool BE>
class RELFileWriter {
public:
RELFileWriter() = default;
~RELFileWriter() = default;
template <typename T>
uint32_t put(const T& obj) {
uint32_t ret = this->w.size();
this->w.put<T>(obj);
return ret;
}
uint32_t write(const void* data, size_t size) {
uint32_t ret = this->w.size();
this->w.write(data, size);
return ret;
}
uint32_t write(const std::string& data) {
uint32_t ret = this->w.size();
this->w.write(data);
return ret;
}
uint32_t write_offset(uint32_t value) {
uint32_t ret = this->w.size();
this->relocations.emplace(ret);
this->w.put<U32T<BE>>(value);
return ret;
}
uint32_t write_ref(const ArrayRefT<BE>& ref) {
uint32_t ret = this->w.size();
this->w.put<ArrayRefT<BE>>(ref);
this->relocations.emplace(ret + offsetof(ArrayRefT<BE>, offset));
return ret;
}
void align(size_t alignment) {
while (this->w.size() & (alignment - 1)) {
this->w.put_u8(0);
}
}
std::string finalize(uint32_t root_offset) {
RELFileFooterT<BE> footer;
footer.root_offset = root_offset;
this->align(0x20);
footer.relocations_offset = this->w.size();
footer.num_relocations = this->relocations.size();
footer.unused1[0] = 1;
uint32_t last_offset = 0;
for (uint32_t reloc_offset : this->relocations) {
if (reloc_offset & 3) {
throw std::logic_error("Relocation is not 4-byte aligned");
}
size_t reloc_value = (reloc_offset - last_offset) >> 2;
if (reloc_value > 0xFFFF) {
throw std::runtime_error("Relocation offset is too far away from previous");
}
this->w.put<U16T<BE>>(reloc_value);
last_offset = reloc_offset;
}
align(0x20);
this->w.put<RELFileFooterT<BE>>(footer);
return std::move(this->w.str());
}
phosg::StringWriter w;
std::set<uint32_t> relocations;
};
+37 -207
View File
@@ -6,8 +6,6 @@
#include "StaticGameData.hh"
#include "Types.hh"
using namespace std;
template <typename IntT, size_t Count>
phosg::JSON to_json(const parray<IntT, Count>& v) {
auto ret = phosg::JSON::list();
@@ -20,7 +18,7 @@ phosg::JSON to_json(const parray<IntT, Count>& v) {
template <typename IntT, size_t Count>
void from_json_into(const phosg::JSON& json, parray<IntT, Count>& ret) {
if (json.size() != Count) {
throw runtime_error("incorrect array length");
throw std::runtime_error("incorrect array length");
}
for (size_t z = 0; z < Count; z++) {
ret[z] = json.at(z).as_int();
@@ -39,7 +37,7 @@ phosg::JSON to_json(const parray<CommonItemSet::Table::Range<IntT>, Count>& v) {
template <typename IntT, size_t Count>
void from_json_into(const phosg::JSON& json, parray<CommonItemSet::Table::Range<IntT>, Count>& ret) {
if (json.size() != Count) {
throw runtime_error("incorrect array length");
throw std::runtime_error("incorrect array length");
}
for (size_t z = 0; z < Count; z++) {
from_json_into(json.at(z), ret[z]);
@@ -60,7 +58,7 @@ void from_json_into(const phosg::JSON& json, CommonItemSet::Table::Range<IntT>&
} else {
const auto& l = json.as_list();
if (l.size() != 2) {
throw runtime_error("incorrect range list length");
throw std::runtime_error("incorrect range list length");
}
ret.min = l.at(0)->as_int();
ret.max = l.at(1)->as_int();
@@ -79,7 +77,7 @@ phosg::JSON to_json(const parray<parray<IntT, Count2>, Count1>& v) {
template <typename IntT, size_t Count1, size_t Count2>
void from_json_into(const phosg::JSON& json, parray<parray<IntT, Count2>, Count1>& ret) {
if (json.size() != Count1) {
throw runtime_error("incorrect array length");
throw std::runtime_error("incorrect array length");
}
for (size_t z = 0; z < Count1; z++) {
from_json_into(json.at(z), ret[z]);
@@ -89,7 +87,7 @@ void from_json_into(const phosg::JSON& json, parray<parray<IntT, Count2>, Count1
template <typename IntT, size_t Count1, size_t Count2>
void from_json_into(const phosg::JSON& json, parray<parray<CommonItemSet::Table::Range<IntT>, Count2>, Count1>& ret) {
if (json.size() != Count1) {
throw runtime_error("incorrect array length");
throw std::runtime_error("incorrect array length");
}
for (size_t z = 0; z < Count1; z++) {
from_json_into(json.at(z), ret[z]);
@@ -139,7 +137,7 @@ CommonItemSet::Table::Table(std::shared_ptr<const Table> prev_table, const phosg
for (auto enemy_type : phosg::EnumRange<EnemyType>()) {
try {
from_json_into(*dict.at(phosg::name_for_enum(enemy_type)), this->enemy_type_meseta_ranges[enemy_type]);
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
}
}
} else {
@@ -151,7 +149,7 @@ CommonItemSet::Table::Table(std::shared_ptr<const Table> prev_table, const phosg
for (auto enemy_type : phosg::EnumRange<EnemyType>()) {
try {
this->enemy_type_drop_probs[enemy_type] = dict.at(phosg::name_for_enum(enemy_type))->as_int();
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
}
}
} else {
@@ -163,7 +161,7 @@ CommonItemSet::Table::Table(std::shared_ptr<const Table> prev_table, const phosg
for (auto enemy_type : phosg::EnumRange<EnemyType>()) {
try {
this->enemy_type_item_classes[enemy_type] = dict.at(phosg::name_for_enum(enemy_type))->as_int();
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
}
}
} else {
@@ -208,13 +206,13 @@ void CommonItemSet::Table::print(FILE* stream) const {
def.rt_index, phosg::name_for_enum(enemy_type),
meseta_range.min, meseta_range.max, drop_prob, item_class,
name_for_common_item_class(item_class));
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
phosg::fwrite_fmt(stream, " {:02X}:{:<23} ----- ----- ---- --:-------\n",
def.rt_index, phosg::name_for_enum(enemy_type));
}
}
static const array<const char*, 12> base_weapon_type_names = {
static const std::array<const char*, 12> base_weapon_type_names = {
"SABER", "SWORD", "DAGGER", "PARTISAN", "SLICER", "HANDGUN", "RIFLE", "MECHGUN", "SHOT", "CANE", "ROD", "WAND"};
phosg::fwrite_fmt(stream, "Base weapon config:\n");
phosg::fwrite_fmt(stream, " TYPE PROB [SB AL] FLOORS\n");
@@ -295,7 +293,7 @@ void CommonItemSet::Table::print(FILE* stream) const {
fputc('\n', stream);
}
static const array<const char*, 19> technique_names = {
static const std::array<const char*, 19> technique_names = {
"FOIE ",
"GIFOIE ",
"RAFOIE ",
@@ -392,7 +390,7 @@ void CommonItemSet::Table::print_diff(FILE* stream, const Table& other) const {
}
auto format_enemy_range_table = [&](const std::unordered_map<EnemyType, Range<uint16_t>>& table) -> std::string {
string ret = "";
std::string ret = "";
for (auto enemy_type : phosg::EnumRange<EnemyType>()) {
try {
const auto& range = table.at(enemy_type);
@@ -400,13 +398,13 @@ void CommonItemSet::Table::print_diff(FILE* stream, const Table& other) const {
ret += ",";
}
ret += std::format("{}=[{},{}]", phosg::name_for_enum(enemy_type), range.min, range.max);
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
}
}
return ret;
};
auto format_enemy_u8_table = [&](const std::unordered_map<EnemyType, uint8_t>& table) -> std::string {
string ret = "";
std::string ret = "";
for (auto enemy_type : phosg::EnumRange<EnemyType>()) {
try {
uint8_t value = table.at(enemy_type);
@@ -414,7 +412,7 @@ void CommonItemSet::Table::print_diff(FILE* stream, const Table& other) const {
ret += ",";
}
ret += std::format("{}={}", phosg::name_for_enum(enemy_type), value);
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
}
}
return ret;
@@ -620,7 +618,7 @@ phosg::JSON CommonItemSet::json() const {
auto prev_table = this->get_prev_table(episode, mode, difficulty, section_id);
auto table = this->get_table(episode, mode, difficulty, section_id);
ret.emplace(json_key, table->json(prev_table));
} catch (const runtime_error&) {
} catch (const std::runtime_error&) {
}
}
}
@@ -642,7 +640,7 @@ void CommonItemSet::print(FILE* stream) const {
name_for_difficulty(difficulty),
name_for_section_id(section_id));
table->print(stream);
} catch (const runtime_error&) {
} catch (const std::runtime_error&) {
}
}
}
@@ -655,15 +653,15 @@ void CommonItemSet::print_diff(FILE* stream, const CommonItemSet& other) const {
for (const auto& mode : ALL_GAME_MODES_V4) {
for (const auto& difficulty : ALL_DIFFICULTIES_V234) {
for (uint8_t section_id = 0; section_id < 10; section_id++) {
shared_ptr<const Table> this_table;
shared_ptr<const Table> other_table;
std::shared_ptr<const Table> this_table;
std::shared_ptr<const Table> other_table;
try {
this_table = this->get_table(episode, mode, difficulty, section_id);
} catch (const runtime_error&) {
} catch (const std::runtime_error&) {
}
try {
other_table = other.get_table(episode, mode, difficulty, section_id);
} catch (const runtime_error&) {
} catch (const std::runtime_error&) {
}
if (!this_table && !other_table) {
@@ -705,7 +703,7 @@ CommonItemSet::Table::Table(const phosg::StringReader& r, bool is_big_endian, bo
template <bool BE>
void CommonItemSet::Table::parse_itempt_t(const phosg::StringReader& r, bool is_v3) {
const auto& offsets = r.pget<OffsetsT<BE>>(r.pget<U32T<BE>>(r.size() - 0x10));
const auto& offsets = r.pget<RootT<BE>>(r.pget<U32T<BE>>(r.size() - 0x10));
this->base_weapon_type_prob_table = r.pget<parray<uint8_t, 0x0C>>(offsets.base_weapon_type_prob_table_offset);
this->subtype_base_table = r.pget<parray<int8_t, 0x0C>>(offsets.subtype_base_table_offset);
@@ -789,17 +787,17 @@ std::string CommonItemSet::json_key_for_table(
token_name_for_difficulty(difficulty), name_for_section_id(section_id));
}
shared_ptr<const CommonItemSet::Table> CommonItemSet::get_table(
std::shared_ptr<const CommonItemSet::Table> CommonItemSet::get_table(
Episode episode, GameMode mode, Difficulty difficulty, uint8_t section_id) const {
try {
return this->tables.at(this->key_for_table(episode, mode, difficulty, section_id));
} catch (const out_of_range&) {
throw runtime_error(std::format("common item table not available for episode={}, mode={}, difficulty={}, secid={}",
} catch (const std::out_of_range&) {
throw std::runtime_error(std::format("common item table not available for episode={}, mode={}, difficulty={}, secid={}",
name_for_episode(episode), name_for_mode(mode), name_for_difficulty(difficulty), section_id));
}
}
shared_ptr<const CommonItemSet::Table> CommonItemSet::get_prev_table(
std::shared_ptr<const CommonItemSet::Table> CommonItemSet::get_prev_table(
Episode episode, GameMode mode, Difficulty difficulty, uint8_t section_id) const {
if (section_id != 0) {
// All section IDs are based on the previous, except Viridia
@@ -837,7 +835,7 @@ AFSV2CommonItemSet::AFSV2CommonItemSet(
for (size_t section_id = 0; section_id < 10; section_id++) {
auto entry = pt_afs.get(static_cast<size_t>(difficulty) * 10 + section_id);
phosg::StringReader r(entry.first, entry.second);
auto table = make_shared<Table>(r, false, false, Episode::EP1);
auto table = std::make_shared<Table>(r, false, false, Episode::EP1);
this->tables.emplace(this->key_for_table(Episode::EP1, GameMode::NORMAL, difficulty, section_id), table);
this->tables.emplace(this->key_for_table(Episode::EP1, GameMode::BATTLE, difficulty, section_id), table);
this->tables.emplace(this->key_for_table(Episode::EP1, GameMode::SOLO, difficulty, section_id), table);
@@ -861,7 +859,7 @@ AFSV2CommonItemSet::AFSV2CommonItemSet(
continue;
}
auto r = ct_afs.get_reader(static_cast<size_t>(difficulty) * 10);
auto table = make_shared<Table>(r, false, false, Episode::EP1);
auto table = std::make_shared<Table>(r, false, false, Episode::EP1);
for (size_t section_id = 0; section_id < 10; section_id++) {
this->tables.emplace(this->key_for_table(Episode::EP1, GameMode::CHALLENGE, difficulty, section_id), table);
}
@@ -872,7 +870,7 @@ AFSV2CommonItemSet::AFSV2CommonItemSet(
GSLV3V4CommonItemSet::GSLV3V4CommonItemSet(std::shared_ptr<const std::string> gsl_data, bool is_big_endian) {
GSLArchive gsl(gsl_data, is_big_endian);
auto filename_for_table = +[](Episode episode, Difficulty difficulty, uint8_t section_id, bool is_challenge) -> string {
auto filename_for_table = +[](Episode episode, Difficulty difficulty, uint8_t section_id, bool is_challenge) -> std::string {
const char* episode_token = "";
switch (episode) {
case Episode::EP1:
@@ -885,7 +883,7 @@ GSLV3V4CommonItemSet::GSLV3V4CommonItemSet(std::shared_ptr<const std::string> gs
episode_token = "s";
break;
default:
throw runtime_error("invalid episode");
throw std::runtime_error("invalid episode");
}
return std::format(
"ItemPT{}{}{}{}.rel",
@@ -901,7 +899,7 @@ GSLV3V4CommonItemSet::GSLV3V4CommonItemSet(std::shared_ptr<const std::string> gs
phosg::StringReader r;
try {
r = gsl.get_reader(filename_for_table(episode, difficulty, section_id, false));
} catch (const exception&) {
} catch (const std::exception&) {
// Fall back to Episode 1 if Episode 4 data is missing
if (episode == Episode::EP4) {
auto ep1_table = this->tables.at(this->key_for_table(Episode::EP1, GameMode::NORMAL, difficulty, section_id));
@@ -913,7 +911,7 @@ GSLV3V4CommonItemSet::GSLV3V4CommonItemSet(std::shared_ptr<const std::string> gs
throw;
}
}
auto table = make_shared<Table>(r, is_big_endian, true, episode);
auto table = std::make_shared<Table>(r, is_big_endian, true, episode);
this->tables.emplace(this->key_for_table(episode, GameMode::NORMAL, difficulty, section_id), table);
this->tables.emplace(this->key_for_table(episode, GameMode::BATTLE, difficulty, section_id), table);
this->tables.emplace(this->key_for_table(episode, GameMode::SOLO, difficulty, section_id), table);
@@ -923,11 +921,11 @@ GSLV3V4CommonItemSet::GSLV3V4CommonItemSet(std::shared_ptr<const std::string> gs
for (Difficulty difficulty : ALL_DIFFICULTIES_V234) {
try {
auto r = gsl.get_reader(filename_for_table(episode, difficulty, 0, true));
auto table = make_shared<Table>(r, is_big_endian, true, episode);
auto table = std::make_shared<Table>(r, is_big_endian, true, episode);
for (size_t section_id = 0; section_id < 10; section_id++) {
this->tables.emplace(this->key_for_table(episode, GameMode::CHALLENGE, difficulty, section_id), table);
}
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
// GC NTE doesn't have Ep2 challenge; just skip adding the table
}
}
@@ -943,180 +941,12 @@ JSONCommonItemSet::JSONCommonItemSet(const phosg::JSON& json) {
auto prev_table = this->get_prev_table(episode, mode, difficulty, section_id);
auto json_key = this->json_key_for_table(episode, mode, difficulty, section_id);
auto key = this->key_for_table(episode, mode, difficulty, section_id);
this->tables.emplace(key, make_shared<Table>(prev_table, json.at(json_key), episode));
} catch (const runtime_error&) {
} catch (const out_of_range&) {
this->tables.emplace(key, std::make_shared<Table>(prev_table, json.at(json_key), episode));
} catch (const std::runtime_error&) {
} catch (const std::out_of_range&) {
}
}
}
}
}
}
RELFileSet::RELFileSet(std::shared_ptr<const std::string> data) : data(data), r(*this->data) {}
ArmorRandomSet::ArmorRandomSet(std::shared_ptr<const std::string> data) : RELFileSet(data) {
// For some reason the footer tables are doubly indirect in this file
uint32_t specs_offset_offset = this->r.pget_u32b(data->size() - 0x10);
uint32_t specs_offset = this->r.pget_u32b(specs_offset_offset);
this->tables = &this->r.pget<parray<TableSpec, 3>>(specs_offset);
}
std::pair<const ArmorRandomSet::WeightTableEntry8*, size_t>
ArmorRandomSet::get_armor_table(size_t index) const {
return this->get_table<WeightTableEntry8>(this->tables->at(0), index);
}
std::pair<const ArmorRandomSet::WeightTableEntry8*, size_t>
ArmorRandomSet::get_shield_table(size_t index) const {
return this->get_table<WeightTableEntry8>(this->tables->at(1), index);
}
std::pair<const ArmorRandomSet::WeightTableEntry8*, size_t>
ArmorRandomSet::get_unit_table(size_t index) const {
return this->get_table<WeightTableEntry8>(this->tables->at(2), index);
}
ToolRandomSet::ToolRandomSet(std::shared_ptr<const std::string> data) : RELFileSet(data) {
uint32_t specs_offset = r.pget_u32b(data->size() - 0x10);
this->common_recovery_table_spec = &r.pget<TableSpec>(r.pget_u32b(specs_offset));
this->rare_recovery_table_spec = &r.pget<TableSpec>(r.pget_u32b(specs_offset + sizeof(uint32_t)), 2 * sizeof(TableSpec));
this->tech_disk_table_spec = this->rare_recovery_table_spec + 1;
this->tech_disk_level_table_spec = &r.pget<TableSpec>(r.pget_u32b(specs_offset + 2 * sizeof(uint32_t)));
}
pair<const uint8_t*, size_t> ToolRandomSet::get_common_recovery_table(size_t index) const {
return this->get_table<uint8_t>(*this->common_recovery_table_spec, index);
}
pair<const ToolRandomSet::WeightTableEntry8*, size_t>
ToolRandomSet::get_rare_recovery_table(size_t index) const {
return this->get_table<WeightTableEntry8>(*this->rare_recovery_table_spec, index);
}
pair<const ToolRandomSet::WeightTableEntry8*, size_t>
ToolRandomSet::get_tech_disk_table(size_t index) const {
return this->get_table<WeightTableEntry8>(*this->tech_disk_table_spec, index);
}
pair<const ToolRandomSet::TechDiskLevelEntry*, size_t>
ToolRandomSet::get_tech_disk_level_table(size_t index) const {
return this->get_table<TechDiskLevelEntry>(*this->tech_disk_level_table_spec, index);
}
WeaponRandomSet::WeaponRandomSet(std::shared_ptr<const std::string> data) : RELFileSet(data) {
uint32_t offsets_offset = this->r.pget_u32b(data->size() - 0x10);
this->offsets = &this->r.pget<Offsets>(offsets_offset);
}
std::pair<const WeaponRandomSet::WeightTableEntry8*, size_t>
WeaponRandomSet::get_weapon_type_table(size_t index) const {
const auto& spec = this->r.pget<TableSpec>(this->offsets->weapon_type_table + index * sizeof(TableSpec));
const auto* data = &this->r.pget<WeightTableEntry8>(spec.offset, spec.entries_per_table * sizeof(WeightTableEntry8));
return make_pair(data, spec.entries_per_table);
}
const parray<WeaponRandomSet::WeightTableEntry32, 6>*
WeaponRandomSet::get_bonus_type_table(size_t which, size_t index) const {
uint32_t base_offset = which ? this->offsets->bonus_type_table2 : this->offsets->bonus_type_table1;
return &this->r.pget<parray<WeightTableEntry32, 6>>(base_offset + sizeof(parray<WeightTableEntry32, 6>) * index);
}
const WeaponRandomSet::RangeTableEntry*
WeaponRandomSet::get_bonus_range(size_t which, size_t index) const {
uint32_t base_offset = which ? this->offsets->bonus_range_table2 : this->offsets->bonus_range_table1;
return &this->r.pget<RangeTableEntry>(base_offset + sizeof(RangeTableEntry) * index);
}
const parray<WeaponRandomSet::WeightTableEntry32, 3>*
WeaponRandomSet::get_special_mode_table(size_t index) const {
return &this->r.pget<parray<WeightTableEntry32, 3>>(
this->offsets->special_mode_table + sizeof(parray<WeightTableEntry32, 3>) * index);
}
const WeaponRandomSet::RangeTableEntry*
WeaponRandomSet::get_standard_grind_range(size_t index) const {
return &this->r.pget<RangeTableEntry>(this->offsets->standard_grind_range_table + sizeof(RangeTableEntry) * index);
}
const WeaponRandomSet::RangeTableEntry*
WeaponRandomSet::get_favored_grind_range(size_t index) const {
return &this->r.pget<RangeTableEntry>(this->offsets->favored_grind_range_table + sizeof(RangeTableEntry) * index);
}
TekkerAdjustmentSet::TekkerAdjustmentSet(std::shared_ptr<const std::string> data) : data(data), r(*data) {
this->offsets = &this->r.pget<Offsets>(this->r.pget_u32b(this->r.size() - 0x10));
}
const ProbabilityTable<uint8_t, 100>& TekkerAdjustmentSet::get_table(
std::array<ProbabilityTable<uint8_t, 100>, 10>& tables_default,
std::array<ProbabilityTable<uint8_t, 100>, 10>& tables_favored,
uint32_t offset_and_count_offset,
bool favored,
uint8_t section_id) const {
if (section_id >= 10) {
throw runtime_error("invalid section ID");
}
ProbabilityTable<uint8_t, 100>& table = favored ? tables_favored[section_id] : tables_default[section_id];
if (table.count == 0) {
uint32_t offset = r.pget_u32b(offset_and_count_offset);
uint32_t count_per_section_id = r.pget_u32b(offset_and_count_offset + 4);
auto* entries = &r.pget<DeltaProbabilityEntry>(offset, sizeof(DeltaProbabilityEntry) * count_per_section_id * 10);
for (size_t z = count_per_section_id * section_id; z < count_per_section_id * (section_id + 1); z++) {
size_t count = favored ? entries[z].count_favored : entries[z].count_default;
for (size_t w = 0; w < count; w++) {
table.push(entries[z].delta_index);
}
}
}
return table;
}
const ProbabilityTable<uint8_t, 100>& TekkerAdjustmentSet::get_special_upgrade_prob_table(uint8_t section_id, bool favored) const {
return this->get_table(
this->special_upgrade_prob_tables_default,
this->special_upgrade_prob_tables_favored,
this->offsets->special_upgrade_prob_table_offset,
favored, section_id);
}
const ProbabilityTable<uint8_t, 100>& TekkerAdjustmentSet::get_grind_delta_prob_table(uint8_t section_id, bool favored) const {
return this->get_table(
this->grind_delta_prob_tables_default,
this->grind_delta_prob_tables_favored,
this->offsets->grind_delta_prob_table_offset,
favored, section_id);
}
const ProbabilityTable<uint8_t, 100>& TekkerAdjustmentSet::get_bonus_delta_prob_table(uint8_t section_id, bool favored) const {
return this->get_table(
this->bonus_delta_prob_tables_default,
this->bonus_delta_prob_tables_favored,
this->offsets->bonus_delta_prob_table_offset,
favored, section_id);
}
int8_t TekkerAdjustmentSet::get_luck(uint32_t start_offset, uint8_t delta_index) const {
phosg::StringReader sub_r = r.sub(start_offset);
while (!sub_r.eof()) {
const auto& entry = sub_r.get<LuckTableEntry>();
if (entry.delta_index == 0xFF) {
return 0;
} else if (entry.delta_index == delta_index) {
return entry.luck;
}
}
return 0;
}
int8_t TekkerAdjustmentSet::get_luck_for_special_upgrade(uint8_t delta_index) const {
return this->get_luck(this->offsets->special_upgrade_luck_table_offset, delta_index);
}
int8_t TekkerAdjustmentSet::get_luck_for_grind_delta(uint8_t delta_index) const {
return this->get_luck(this->offsets->grind_delta_luck_table_offset, delta_index);
}
int8_t TekkerAdjustmentSet::get_luck_for_bonus_delta(uint8_t delta_index) const {
return this->get_luck(this->offsets->bonus_delta_luck_offset, delta_index);
}
+4 -296
View File
@@ -69,7 +69,7 @@ public:
void parse_itempt_t(const phosg::StringReader& r, bool is_v3);
template <bool BE>
struct OffsetsT {
struct RootT {
// This data structure uses index probability tables in multiple places. An index probability table is a table
// where each entry holds the probability that that entry's index is used. For example, if the armor slot count
// probability table contains [77, 17, 5, 1, 0], this means there is a 77% chance of no slots, 17% chance of 1
@@ -244,9 +244,9 @@ public:
/* 50 */ U32T<BE> box_item_class_prob_table_offset;
// There are several unused fields here.
} __packed_ws_be__(OffsetsT, 0x54);
using Offsets = OffsetsT<false>;
using OffsetsBE = OffsetsT<true>;
} __packed_ws_be__(RootT, 0x54);
using Root = RootT<false>;
using RootBE = RootT<true>;
};
bool operator==(const CommonItemSet& other) const = default;
@@ -284,295 +284,3 @@ class JSONCommonItemSet : public CommonItemSet {
public:
explicit JSONCommonItemSet(const phosg::JSON& json);
};
// Note: There are clearly better ways of doing this, but this implementation closely follows what the original code in
// the client does.
template <typename ItemT, size_t MaxCount>
struct ProbabilityTable {
ItemT items[MaxCount];
size_t count;
ProbabilityTable() : count(0) {}
void push(ItemT item) {
if (this->count == MaxCount) {
throw std::runtime_error("push to full probability table");
}
this->items[this->count++] = item;
}
ItemT pop() {
if (this->count == 0) {
throw std::runtime_error("pop from empty probability table");
}
return this->items[--this->count];
}
void shuffle(std::shared_ptr<RandomGenerator> rand_crypt) {
for (size_t z = 1; z < this->count; z++) {
size_t other_z = rand_crypt->next() % (z + 1);
ItemT t = this->items[z];
this->items[z] = this->items[other_z];
this->items[other_z] = t;
}
}
ItemT sample(std::shared_ptr<RandomGenerator> rand_crypt) const {
if (this->count == 0) {
throw std::runtime_error("sample from empty probability table");
} else if (this->count == 1) {
return this->items[0];
} else {
return this->items[rand_crypt->next() % this->count];
}
}
};
class RELFileSet {
public:
template <typename ValueT, typename WeightT = ValueT>
struct WeightTableEntry {
ValueT value;
WeightT weight;
} __attribute__((packed));
using WeightTableEntry8 = WeightTableEntry<uint8_t>;
using WeightTableEntry32 = WeightTableEntry<be_uint32_t>;
check_struct_size(WeightTableEntry8, 2);
check_struct_size(WeightTableEntry32, 8);
protected:
std::shared_ptr<const std::string> data;
phosg::StringReader r;
struct TableSpec {
be_uint32_t offset;
uint8_t entries_per_table;
parray<uint8_t, 3> unused;
} __packed_ws__(TableSpec, 8);
RELFileSet(std::shared_ptr<const std::string> data);
template <typename T>
std::pair<const T*, size_t> get_table(const TableSpec& spec, size_t index) const {
const T* entries = &r.pget<T>(
spec.offset + index * spec.entries_per_table * sizeof(T), spec.entries_per_table * sizeof(T));
return std::make_pair(entries, spec.entries_per_table);
}
};
class ArmorRandomSet : public RELFileSet {
public:
// This class parses and accesses data from ArmorRandom.rel
ArmorRandomSet(std::shared_ptr<const std::string> data);
std::pair<const WeightTableEntry8*, size_t> get_armor_table(size_t index) const;
std::pair<const WeightTableEntry8*, size_t> get_shield_table(size_t index) const;
std::pair<const WeightTableEntry8*, size_t> get_unit_table(size_t index) const;
private:
const parray<TableSpec, 3>* tables;
};
class ToolRandomSet : public RELFileSet {
public:
// This class parses and accesses data from ToolRandom.rel
ToolRandomSet(std::shared_ptr<const std::string> data);
struct TechDiskLevelEntry {
enum class Mode : uint8_t {
LEVEL_1 = 0,
PLAYER_LEVEL_DIVISOR = 1,
RANDOM_IN_RANGE = 2,
};
Mode mode;
uint8_t player_level_divisor_or_min_level;
uint8_t max_level;
} __packed_ws__(TechDiskLevelEntry, 3);
std::pair<const uint8_t*, size_t> get_common_recovery_table(size_t index) const;
std::pair<const WeightTableEntry8*, size_t> get_rare_recovery_table(size_t index) const;
std::pair<const WeightTableEntry8*, size_t> get_tech_disk_table(size_t index) const;
std::pair<const TechDiskLevelEntry*, size_t> get_tech_disk_level_table(size_t index) const;
private:
const TableSpec* common_recovery_table_spec;
const TableSpec* rare_recovery_table_spec;
const TableSpec* tech_disk_table_spec;
const TableSpec* tech_disk_level_table_spec;
};
class WeaponRandomSet : public RELFileSet {
public:
// This class parses and accesses data from WeaponRandom*.rel
WeaponRandomSet(std::shared_ptr<const std::string> data);
struct RangeTableEntry {
be_uint32_t min;
be_uint32_t max;
} __packed_ws__(RangeTableEntry, 8);
std::pair<const WeightTableEntry8*, size_t> get_weapon_type_table(size_t index) const;
const parray<WeightTableEntry32, 6>* get_bonus_type_table(size_t which, size_t index) const;
const RangeTableEntry* get_bonus_range(size_t which, size_t index) const;
const parray<WeightTableEntry32, 3>* get_special_mode_table(size_t index) const;
const RangeTableEntry* get_standard_grind_range(size_t index) const;
const RangeTableEntry* get_favored_grind_range(size_t index) const;
private:
struct Offsets {
be_uint32_t weapon_type_table; // [{c, o -> (table)}](10)
be_uint32_t bonus_type_table1; // [[{u32 value, u32 weight}](6)](9)
be_uint32_t bonus_type_table2; // [[{u32 value, u32 weight}](6)](9)
be_uint32_t bonus_range_table1; // [{u32 min_index, u32 max_index}](9)
be_uint32_t bonus_range_table2; // [{u32 min_index, u32 max_index}](9)
be_uint32_t special_mode_table; // [[{u32 value, u32 weight}](3)](8)
be_uint32_t standard_grind_range_table; // [{u32 min, u32 max}](6)
be_uint32_t favored_grind_range_table; // [{u32 min, u32 max}](6)
} __packed_ws__(Offsets, 0x20);
const Offsets* offsets;
};
class TekkerAdjustmentSet {
public:
// This class parses and accesses data from JudgeItem.rel
TekkerAdjustmentSet(std::shared_ptr<const std::string> data);
const ProbabilityTable<uint8_t, 100>& get_special_upgrade_prob_table(uint8_t section_id, bool favored) const;
const ProbabilityTable<uint8_t, 100>& get_grind_delta_prob_table(uint8_t section_id, bool favored) const;
const ProbabilityTable<uint8_t, 100>& get_bonus_delta_prob_table(uint8_t section_id, bool favored) const;
int8_t get_luck_for_special_upgrade(uint8_t delta_index) const;
int8_t get_luck_for_grind_delta(uint8_t delta_index) const;
int8_t get_luck_for_bonus_delta(uint8_t delta_index) const;
private:
const ProbabilityTable<uint8_t, 100>& get_table(
std::array<ProbabilityTable<uint8_t, 100>, 10>& tables_default,
std::array<ProbabilityTable<uint8_t, 100>, 10>& tables_favored,
uint32_t offset_and_count_offset,
bool favored,
uint8_t section_id) const;
int8_t get_luck(uint32_t start_offset, uint8_t delta_index) const;
std::shared_ptr<const std::string> data;
phosg::StringReader r;
struct DeltaProbabilityEntry {
uint8_t delta_index;
uint8_t count_default;
uint8_t count_favored;
} __packed_ws__(DeltaProbabilityEntry, 3);
struct LuckTableEntry {
uint8_t delta_index;
int8_t luck;
} __packed_ws__(LuckTableEntry, 2);
struct Offsets {
// Each section ID's favored weapon class has different probabilities than those used for all other weapons. The
// tables are labeled with (D) for the default values and (F) for the favored-class values.
// Note that the favored bonuses for Redria are all zero; these values are unused because Redria does not have a
// favored weapon type. Curiously, Yellowboze also does not have a favored weapon type, but the values for
// Yellowboze are not all zero.
// This table specifies how likely a special is to be upgraded or downgraded by one level.
// In PSO V3, the special upgrade table is:
// Viridia => (D) +1=10%, 0=60%, -1=30%
// Viridia => (F) +1=25%, 0=50%, -1=25%
// Greennill => (D) +1=25%, 0=65%, -1=10%
// Greennill => (F) +1=40%, 0=55%, -1=5%
// Skyly => (D) +1=15%, 0=70%, -1=15%
// Skyly => (F) +1=30%, 0=60%, -1=10%
// Bluefull => (D) +1=10%, 0=60%, -1=30%
// Bluefull => (F) +1=25%, 0=50%, -1=25%
// Purplenum => (D) +1=25%, 0=65%, -1=10%
// Purplenum => (F) +1=40%, 0=55%, -1=5%
// Pinkal => (D) +1=15%, 0=70%, -1=15%
// Pinkal => (F) +1=30%, 0=60%, -1=10%
// Redria => (D) +1=20%, 0=60%, -1=20%
// Redria => (F) +1=0%, 0=0%, -1=0%
// Oran => (D) +1=15%, 0=70%, -1=15%
// Oran => (F) +1=30%, 0=60%, -1=10%
// Yellowboze => (D) +1=25%, 0=65%, -1=10%
// Yellowboze => (F) +1=40%, 0=55%, -1=5%
// Whitill => (D) +1=10%, 0=60%, -1=30%
// Whitill => (F) +1=25%, 0=50%, -1=25%
be_uint32_t special_upgrade_prob_table_offset; // [{c, o -> (DeltaProbabilityEntry)[10][c]})
// This table specifies how likely a weapon's grind is to be upgraded or downgraded, and by how much. The final
// grind value is clamped to the range between 0 and the weapon's maximum grind from ItemPMT, inclusive.
// In PSO V3, the grind delta table is:
// Viridia => (D) +3=3%, +2=7%, +1=13%, 0=60%, -1=10%, -2=7%, -3=0%
// Viridia => (F) +3=5%, +2=13%, +1=25%, 0=50%, -1=7%, -2=0%, -3=0%
// Greennill => (D) +3=0%, +2=5%, +1=10%, 0=70%, -1=10%, -2=5%, -3=0%
// Greennill => (F) +3=3%, +2=7%, +1=20%, 0=60%, -1=10%, -2=0%, -3=0%
// Skyly => (D) +3=0%, +2=7%, +1=10%, 0=60%, -1=13%, -2=7%, -3=3%
// Skyly => (F) +3=3%, +2=12%, +1=20%, 0=50%, -1=10%, -2=5%, -3=0%
// Bluefull => (D) +3=3%, +2=7%, +1=13%, 0=60%, -1=10%, -2=7%, -3=0%
// Bluefull => (F) +3=5%, +2=13%, +1=25%, 0=50%, -1=7%, -2=0%, -3=0%
// Purplenum => (D) +3=0%, +2=5%, +1=10%, 0=70%, -1=10%, -2=5%, -3=0%
// Purplenum => (F) +3=3%, +2=7%, +1=20%, 0=60%, -1=10%, -2=0%, -3=0%
// Pinkal => (D) +3=0%, +2=7%, +1=10%, 0=60%, -1=13%, -2=7%, -3=3%
// Pinkal => (F) +3=3%, +2=12%, +1=20%, 0=50%, -1=10%, -2=5%, -3=0%
// Redria => (D) +3=0%, +2=7%, +1=10%, 0=60%, -1=13%, -2=7%, -3=3%
// Redria => (F) +3=0%, +2=0%, +1=0%, 0=0%, -1=0%, -2=0%, -3=0%
// Oran => (D) +3=0%, +2=7%, +1=10%, 0=60%, -1=13%, -2=7%, -3=3%
// Oran => (F) +3=3%, +2=12%, +1=20%, 0=50%, -1=10%, -2=5%, -3=0%
// Yellowboze => (D) +3=0%, +2=5%, +1=10%, 0=70%, -1=10%, -2=5%, -3=0%
// Yellowboze => (F) +3=3%, +2=7%, +1=20%, 0=60%, -1=10%, -2=0%, -3=0%
// Whitill => (D) +3=3%, +2=7%, +1=13%, 0=60%, -1=10%, -2=7%, -3=0%
// Whitill => (F) +3=5%, +2=13%, +1=25%, 0=50%, -1=7%, -2=0%, -3=0%
be_uint32_t grind_delta_prob_table_offset; // [{c, o -> (DeltaProbabilityEntry)[10][c]})
// This table specifies how likely a weapon's bonuses are to be upgraded or downgraded, and by how much. The final
// bonuses are capped above at 100, but there is no lower limit (so negative results are possible).
// In PSO V3, the bonus delta table is:
// Viridia => (D) +10=5%, +5=15%, 0=60%, -5=15%, -10=5%
// Viridia => (F) +10=8%, +5=20%, 0=60%, -5=10%, -10=2%
// Greennill => (D) +10=5%, +5=10%, 0=50%, -5=25%, -10=10%
// Greennill => (F) +10=8%, +5=15%, 0=50%, -5=20%, -10=7%
// Skyly => (D) +10=10%, +5=25%, 0=50%, -5=10%, -10=5%
// Skyly => (F) +10=13%, +5=30%, 0=50%, -5=5%, -10=2%
// Bluefull => (D) +10=5%, +5=15%, 0=60%, -5=15%, -10=5%
// Bluefull => (F) +10=8%, +5=20%, 0=60%, -5=10%, -10=2%
// Purplenum => (D) +10=5%, +5=10%, 0=50%, -5=25%, -10=10%
// Purplenum => (F) +10=8%, +5=15%, 0=50%, -5=20%, -10=7%
// Pinkal => (D) +10=10%, +5=25%, 0=50%, -5=10%, -10=5%
// Pinkal => (F) +10=13%, +5=30%, 0=50%, -5=5%, -10=2%
// Redria => (D) +10=10%, +5=25%, 0=50%, -5=10%, -10=5%
// Redria => (F) +10=0%, +5=0%, 0=0%, -5=0%, -10=0%
// Oran => (D) +10=10%, +5=25%, 0=50%, -5=10%, -10=5%
// Oran => (F) +10=13%, +5=30%, 0=50%, -5=5%, -10=2%
// Yellowboze => (D) +10=5%, +5=10%, 0=50%, -5=25%, -10=10%
// Yellowboze => (F) +10=8%, +5=15%, 0=50%, -5=20%, -10=7%
// Whitill => (D) +10=5%, +5=15%, 0=60%, -5=15%, -10=5%
// Whitill => (F) +10=8%, +5=20%, 0=60%, -5=10%, -10=2%
be_uint32_t bonus_delta_prob_table_offset; // [{c, o -> (DeltaProbabilityEntry)[10][c]})
// There is a secondary computation done during weapon adjustment that appears to determine how "good" the
// resulting weapon is compared to its original state. If the result of this computation is positive, the game
// plays a jingle when the tekker result is accepted. These tables describe how much each delta affects this value,
// which we call luck.
// In PSO V3, the special upgrade luck table is:
// +1 => +20, 0 => 0, -1 => -20
be_uint32_t special_upgrade_luck_table_offset; // LuckTableEntry[...]; ending with FF FF
// In PSO V3, the grind delta luck table is:
// +3 => +10, +2 => +5, +1 => +3, 0 => 0, -1 => -3, -2 => -5, -3 => -10
be_uint32_t grind_delta_luck_table_offset; // LuckTableEntry[...]; ending with FF FF
// In PSO V3, the bonus delta luck table is:
// +10 => +15, +5 => +8, 0 => 0, -5 => -8, -10 => -15
be_uint32_t bonus_delta_luck_offset; // LuckTableEntry[...]; ending with FF FF
} __packed_ws__(Offsets, 0x18);
const Offsets* offsets;
mutable std::array<ProbabilityTable<uint8_t, 100>, 10> special_upgrade_prob_tables_default;
mutable std::array<ProbabilityTable<uint8_t, 100>, 10> special_upgrade_prob_tables_favored;
mutable std::array<ProbabilityTable<uint8_t, 100>, 10> grind_delta_prob_tables_default;
mutable std::array<ProbabilityTable<uint8_t, 100>, 10> grind_delta_prob_tables_favored;
mutable std::array<ProbabilityTable<uint8_t, 100>, 10> bonus_delta_prob_tables_default;
mutable std::array<ProbabilityTable<uint8_t, 100>, 10> bonus_delta_prob_tables_favored;
};
+170 -97
View File
@@ -11,8 +11,6 @@
#include "Text.hh"
using namespace std;
template <>
const char* phosg::name_for_enum<CompressPhase>(CompressPhase v) {
switch (v) {
@@ -30,29 +28,29 @@ const char* phosg::name_for_enum<CompressPhase>(CompressPhase v) {
}
template <size_t WindowLength, size_t MaxMatchLength>
struct WindowIndex {
struct MapWindowIndex {
const uint8_t* data;
size_t size;
size_t offset;
set<size_t, function<bool(size_t, size_t)>> index;
size_t current_offset;
std::set<size_t, std::function<bool(size_t, size_t)>> index;
WindowIndex(const void* data, size_t size)
MapWindowIndex(const void* data, size_t size)
: data(reinterpret_cast<const uint8_t*>(data)),
size(size),
offset(0),
index(bind(&WindowIndex::set_comparator, this, placeholders::_1, placeholders::_2)) {}
current_offset(0),
index(std::bind(&MapWindowIndex::set_comparator, this, std::placeholders::_1, std::placeholders::_2)) {}
void advance() {
if (this->offset >= WindowLength) {
this->index.erase(this->offset - WindowLength);
if (this->current_offset >= WindowLength) {
this->index.erase(this->current_offset - WindowLength);
}
this->index.emplace(this->offset);
this->offset++;
this->index.emplace(this->current_offset);
this->current_offset++;
}
size_t get_match_length(size_t match_offset) const {
size_t match_iter = match_offset;
size_t offset_iter = this->offset;
size_t offset_iter = this->current_offset;
while ((match_iter < match_offset + MaxMatchLength) &&
(match_iter < this->size) &&
(offset_iter < this->size) &&
@@ -69,7 +67,7 @@ struct WindowIndex {
// strings far too much. We can solve this by instead storing the offset of each string as keys in a set and using a
// custom comparator to treat them as references to binary strings within the data.
bool set_comparator(size_t a, size_t b) const {
size_t max_length = min<size_t>(MaxMatchLength, this->size - max<size_t>(a, b));
size_t max_length = std::min<size_t>(MaxMatchLength, this->size - std::max<size_t>(a, b));
size_t end_a = a + max_length;
for (; a < end_a; a++, b++) {
uint8_t data_a = static_cast<uint8_t>(this->data[a]);
@@ -83,14 +81,14 @@ struct WindowIndex {
return a < b; // Maximum-length match; order them by offset
};
pair<size_t, size_t> get_best_match() const {
std::pair<size_t, size_t> match() const {
// Find the best match from the index. It's unlikely that we'll get an exact match, so check the entry before the
// upper_bound result too. Note: We use upper_bound rather than lower_bound because in PRS, a backreference can be
// encoded with fewer bits if it's close to the decompression offset, and this makes us pick the latest match by
// default.
size_t match_offset = 0;
size_t match_size = 0;
auto it = this->index.upper_bound(this->offset);
auto it = this->index.upper_bound(this->current_offset);
if (it != this->index.end()) {
size_t new_match_offset = *it;
size_t new_match_size = this->get_match_length(new_match_offset);
@@ -108,10 +106,86 @@ struct WindowIndex {
match_size = new_match_size;
}
}
return make_pair(match_offset, match_size);
return std::make_pair(match_offset, match_size);
}
};
template <size_t WindowLength, size_t MaxMatchLength>
struct TreeWindowIndex {
// This class is the result of an experiment to see if a prefix tree is faster for optimal compression. It turns out
// it's not, even if the std::map in Node is replaced with a std::unordered_map or std::array. This structure is
// easier to understand than MapWindowIndex, but is about 6-7x slower on average.
const uint8_t* data;
size_t size;
size_t current_offset;
struct Node {
size_t start_offset = 0;
std::map<uint8_t, Node> children;
};
Node root;
TreeWindowIndex(const void* data, size_t size)
: data(reinterpret_cast<const uint8_t*>(data)), size(size), current_offset(0) {}
void advance() {
// Delete the oldest string, if the current offset has passed the initial window
if (this->current_offset >= WindowLength) {
size_t start_offset = this->current_offset - WindowLength;
size_t end_offset = std::min<size_t>(this->size, start_offset + MaxMatchLength);
Node* current = &this->root;
for (size_t offset = start_offset; offset < end_offset; offset++) {
auto child_it = current->children.find(this->data[offset]);
// The child should always be in the set - we should have added all of its nodes in a previous advance() call
if (child_it == current->children.end()) {
throw std::logic_error(std::format("Attempted to delete string at offset {:X} which was not in the set",
start_offset));
}
if (child_it->second.start_offset == start_offset) {
// If the child's start offset matches start_offset, then the rest of the nodes below it must also match
// start_offset (they were never again visited by the second part of this function after the first time they
// were added) so the entire subtree can be deleted. This means there are no other strings in the window that
// share the first (start_offset + 1) bytes with this string.
current->children.erase(child_it);
break;
} else {
// If the start offset does not match start_offset, then this node belongs to a later string; we need to
// check its subtree to see if anything should be deleted
current = &child_it->second;
}
}
}
// Create all nodes for the current string, or update their start_offsets if they already exist
size_t end_offset = std::min<size_t>(size, this->current_offset + MaxMatchLength);
Node* current = &this->root;
for (size_t offset = this->current_offset; offset < end_offset; offset++) {
current = &current->children[this->data[offset]];
current->start_offset = this->current_offset;
}
this->current_offset++;
}
std::pair<size_t, size_t> match() const {
size_t end_offset = std::min<size_t>(size, this->current_offset + MaxMatchLength);
const Node* current = &this->root;
size_t offset;
for (offset = this->current_offset; offset < end_offset; offset++) {
auto it = current->children.find(this->data[offset]);
if (it == current->children.end()) {
break;
} else {
current = &it->second;
}
}
return std::make_pair(current->start_offset, offset - this->current_offset);
}
};
template <size_t WindowSize, size_t MaxMatchLength>
using WindowIndex = MapWindowIndex<WindowSize, MaxMatchLength>;
struct LZSSInterleavedWriter {
phosg::StringWriter w;
size_t buf_offset;
@@ -140,7 +214,7 @@ struct LZSSInterleavedWriter {
void write_control(bool v) {
if (this->next_control_bit == 0) {
throw logic_error("write_control called with no space to write");
throw std::logic_error("write_control called with no space to write");
}
if (v) {
this->buf[0] |= this->next_control_bit;
@@ -208,28 +282,28 @@ struct PRSPathNode {
size_t to_offset = 0;
};
string prs_compress_optimal(const void* in_data_v, size_t in_size, ProgressCallback progress_fn) {
std::string prs_compress_optimal(const void* in_data_v, size_t in_size, ProgressCallback progress_fn) {
const uint8_t* in_data = reinterpret_cast<const uint8_t*>(in_data_v);
vector<PRSPathNode> nodes;
std::vector<PRSPathNode> nodes;
nodes.resize(in_size + 1);
nodes[0].bits_used = 18; // Stop command: 2 control bits and 2 data bytes
size_t copy_progress_max = 3 * in_size;
atomic<size_t> copy_progress = 0;
std::atomic<size_t> copy_progress = 0;
// Populate all possible short copies
std::thread short_window_thread([&]() -> void {
WindowIndex<0x100, 5> window(in_data_v, in_size);
while (window.offset < in_size) {
if (window.offset && (window.offset & 0xFFF) == 0 && progress_fn) {
while (window.current_offset < in_size) {
if (window.current_offset && (window.current_offset & 0xFFF) == 0 && progress_fn) {
size_t progress = copy_progress.fetch_add(0x1000) + 0x1000;
progress_fn(CompressPhase::INDEX, progress, copy_progress_max, 0);
}
auto& node = nodes[window.offset];
auto match = window.get_best_match();
auto& node = nodes[window.current_offset];
auto match = window.match();
if (match.second >= 2) {
node.short_copy_offset = match.first - window.offset;
node.short_copy_offset = match.first - window.current_offset;
node.max_short_copy_size = match.second;
}
window.advance();
@@ -239,15 +313,15 @@ string prs_compress_optimal(const void* in_data_v, size_t in_size, ProgressCallb
// Populate all possible long copies
std::thread long_window_thread([&]() -> void {
WindowIndex<0x1FFF, 9> window(in_data_v, in_size);
while (window.offset < in_size) {
if (window.offset && (window.offset & 0xFFF) == 0 && progress_fn) {
while (window.current_offset < in_size) {
if (window.current_offset && (window.current_offset & 0xFFF) == 0 && progress_fn) {
size_t progress = copy_progress.fetch_add(0x1000) + 0x1000;
progress_fn(CompressPhase::INDEX, progress, copy_progress_max, 0);
}
auto& node = nodes[window.offset];
auto match = window.get_best_match();
auto& node = nodes[window.current_offset];
auto match = window.match();
if (match.second >= 3) {
node.long_copy_offset = match.first - window.offset;
node.long_copy_offset = match.first - window.current_offset;
node.max_long_copy_size = match.second;
}
window.advance();
@@ -257,15 +331,15 @@ string prs_compress_optimal(const void* in_data_v, size_t in_size, ProgressCallb
// Populate all possible extended copies
std::thread extended_window_thread([&]() -> void {
WindowIndex<0x1FFF, 0x100> window(in_data_v, in_size);
while (window.offset < in_size) {
if (window.offset && (window.offset & 0xFFF) == 0 && progress_fn) {
while (window.current_offset < in_size) {
if (window.current_offset && (window.current_offset & 0xFFF) == 0 && progress_fn) {
size_t progress = copy_progress.fetch_add(0x1000) + 0x1000;
progress_fn(CompressPhase::INDEX, progress, copy_progress_max, 0);
}
auto& node = nodes[window.offset];
auto match = window.get_best_match();
auto& node = nodes[window.current_offset];
auto match = window.match();
if (match.second >= 1) {
node.extended_copy_offset = match.first - window.offset;
node.extended_copy_offset = match.first - window.current_offset;
node.max_extended_copy_size = match.second;
}
window.advance();
@@ -361,14 +435,14 @@ string prs_compress_optimal(const void* in_data_v, size_t in_size, ProgressCallb
switch (next_node.from_command_type) {
case PRSPathNode::CommandType::LITERAL:
if (copy_size != 1) {
throw logic_error("incorrect size for LITERAL copy type");
throw std::logic_error("incorrect size for LITERAL copy type");
}
w.write_control(true);
w.write_data(in_data[offset]);
break;
case PRSPathNode::CommandType::SHORT_COPY: {
if (copy_size < 2 || copy_size > 5) {
throw logic_error("incorrect size for SHORT_COPY copy type");
throw std::logic_error("incorrect size for SHORT_COPY copy type");
}
uint8_t encoded_size = copy_size - 2;
w.write_control(false);
@@ -383,7 +457,7 @@ string prs_compress_optimal(const void* in_data_v, size_t in_size, ProgressCallb
}
case PRSPathNode::CommandType::LONG_COPY: {
if (copy_size < 2 || copy_size > 9) {
throw logic_error("incorrect size for LONG_COPY copy type");
throw std::logic_error("incorrect size for LONG_COPY copy type");
}
w.write_control(false);
w.flush_if_ready();
@@ -395,7 +469,7 @@ string prs_compress_optimal(const void* in_data_v, size_t in_size, ProgressCallb
}
case PRSPathNode::CommandType::EXTENDED_COPY: {
if (copy_size < 1 || copy_size > 0x100) {
throw logic_error("incorrect size for EXTENDED_COPY copy type");
throw std::logic_error("incorrect size for EXTENDED_COPY copy type");
}
w.write_control(false);
w.flush_if_ready();
@@ -407,7 +481,7 @@ string prs_compress_optimal(const void* in_data_v, size_t in_size, ProgressCallb
break;
}
default:
throw logic_error("invalid copy type in shortest path");
throw std::logic_error("invalid copy type in shortest path");
}
w.flush_if_ready();
@@ -424,11 +498,11 @@ string prs_compress_optimal(const void* in_data_v, size_t in_size, ProgressCallb
return std::move(w.close());
}
string prs_compress_optimal(const string& data, ProgressCallback progress_fn) {
std::string prs_compress_optimal(const std::string& data, ProgressCallback progress_fn) {
return prs_compress_optimal(data.data(), data.size(), progress_fn);
}
string prs_compress_pessimal(const void* vdata, size_t size) {
std::string prs_compress_pessimal(const void* vdata, size_t size) {
const uint8_t* in_data = reinterpret_cast<const uint8_t*>(vdata);
// The worst possible encoding we can do is a literal byte when no byte with the same value is within the window, or
@@ -436,10 +510,10 @@ string prs_compress_pessimal(const void* vdata, size_t size) {
WindowIndex<0x1FFF, 1> window(in_data, size);
LZSSInterleavedWriter w;
for (size_t z = 0; z < size; z++) {
auto match = window.get_best_match();
auto match = window.match();
if (match.second >= 1) {
// Write extended copy
int16_t offset = match.first - window.offset;
int16_t offset = match.first - window.current_offset;
w.write_control(false);
w.flush_if_ready();
w.write_control(true);
@@ -479,7 +553,7 @@ PRSCompressor::PRSCompressor(
void PRSCompressor::add(const void* data, size_t size) {
if (this->closed) {
throw logic_error("compressor is closed");
throw std::logic_error("compressor is closed");
}
phosg::StringReader r(data, size);
@@ -488,7 +562,7 @@ void PRSCompressor::add(const void* data, size_t size) {
}
}
void PRSCompressor::add(const string& data) {
void PRSCompressor::add(const std::string& data) {
this->add(data.data(), data.size());
}
@@ -573,7 +647,7 @@ void PRSCompressor::advance() {
this->advance_extended_copy(backreference_offset, best_match_size);
} else {
throw logic_error("invalid best match");
throw std::logic_error("invalid best match");
}
}
@@ -621,7 +695,7 @@ void PRSCompressor::advance_extended_copy(ssize_t offset, size_t size) {
this->move_forward_data_to_reverse_log(size);
}
string& PRSCompressor::close() {
std::string& PRSCompressor::close() {
if (!this->closed) {
// Advance until all input is consumed
while (this->reverse_log.end_offset() < this->input_bytes) {
@@ -658,23 +732,23 @@ void PRSCompressor::flush_control() {
this->output.pput_u8(this->control_byte_offset, this->pending_control_bits & 0xFF);
} else {
if (this->control_byte_offset != this->output.size() - 1) {
throw logic_error("data written without control bits");
throw std::logic_error("data written without control bits");
}
this->output.str().resize(this->output.str().size() - 1);
}
}
string prs_compress(const void* vdata, size_t size, ssize_t compression_level, ProgressCallback progress_fn) {
std::string prs_compress(const void* vdata, size_t size, ssize_t compression_level, ProgressCallback progress_fn) {
PRSCompressor prs(compression_level, progress_fn);
prs.add(vdata, size);
return std::move(prs.close());
}
string prs_compress(const string& data, ssize_t compression_level, ProgressCallback progress_fn) {
std::string prs_compress(const std::string& data, ssize_t compression_level, ProgressCallback progress_fn) {
return prs_compress(data.data(), data.size(), compression_level, progress_fn);
}
string prs_compress_indexed(const void* in_data_v, size_t in_size, ProgressCallback progress_fn) {
std::string prs_compress_indexed(const void* in_data_v, size_t in_size, ProgressCallback progress_fn) {
const uint8_t* in_data = reinterpret_cast<const uint8_t*>(in_data_v);
LZSSInterleavedWriter w;
@@ -683,15 +757,15 @@ string prs_compress_indexed(const void* in_data_v, size_t in_size, ProgressCallb
WindowIndex<0x1FFF, 0x100> w_extended(in_data_v, in_size);
size_t last_progress_fn_call_offset = 0;
while (w_short.offset < in_size) {
if (progress_fn && ((last_progress_fn_call_offset & ~0xFFF) != (w_short.offset & ~0xFFF))) {
last_progress_fn_call_offset = w_short.offset;
progress_fn(CompressPhase::GENERATE_RESULT, w_short.offset, in_size, w.size());
while (w_short.current_offset < in_size) {
if (progress_fn && ((last_progress_fn_call_offset & ~0xFFF) != (w_short.current_offset & ~0xFFF))) {
last_progress_fn_call_offset = w_short.current_offset;
progress_fn(CompressPhase::GENERATE_RESULT, w_short.current_offset, in_size, w.size());
}
auto m_short = w_short.get_best_match();
auto m_long = w_long.get_best_match();
auto m_extended = w_extended.get_best_match();
auto m_short = w_short.match();
auto m_long = w_long.match();
auto m_extended = w_extended.match();
// Write the match that achieves the best ratio of output bytes to compressed bits used. To do this without
// floating-point math, we multiply the output byte count for each type of command by 468 / (command_bits), since
@@ -737,11 +811,11 @@ string prs_compress_indexed(const void* in_data_v, size_t in_size, ProgressCallb
switch (command_type) {
case PRSPathNode::CommandType::LITERAL:
w.write_control(true);
w.write_data(in_data[w_short.offset]);
w.write_data(in_data[w_short.current_offset]);
bytes_consumed = 1;
break;
case PRSPathNode::CommandType::SHORT_COPY: {
ssize_t backreference_offset = m_short.first - w_short.offset;
ssize_t backreference_offset = m_short.first - w_short.current_offset;
uint8_t encoded_size = m_short.second - 2;
w.write_control(false);
w.flush_if_ready();
@@ -755,7 +829,7 @@ string prs_compress_indexed(const void* in_data_v, size_t in_size, ProgressCallb
break;
}
case PRSPathNode::CommandType::LONG_COPY: {
ssize_t backreference_offset = m_long.first - w_long.offset;
ssize_t backreference_offset = m_long.first - w_long.current_offset;
w.write_control(false);
w.flush_if_ready();
w.write_control(true);
@@ -766,7 +840,7 @@ string prs_compress_indexed(const void* in_data_v, size_t in_size, ProgressCallb
break;
}
case PRSPathNode::CommandType::EXTENDED_COPY: {
ssize_t backreference_offset = m_extended.first - w_extended.offset;
ssize_t backreference_offset = m_extended.first - w_extended.current_offset;
w.write_control(false);
w.flush_if_ready();
w.write_control(true);
@@ -779,12 +853,12 @@ string prs_compress_indexed(const void* in_data_v, size_t in_size, ProgressCallb
}
case PRSPathNode::CommandType::NONE:
default:
throw logic_error("invalid command type");
throw std::logic_error("invalid command type");
}
w.flush_if_ready();
if (bytes_consumed == 0) {
throw logic_error("no input data was consumed");
throw std::logic_error("no input data was consumed");
}
for (size_t z = 0; z < bytes_consumed; z++) {
@@ -804,7 +878,7 @@ string prs_compress_indexed(const void* in_data_v, size_t in_size, ProgressCallb
return std::move(w.close());
}
string prs_compress_indexed(const string& data, ProgressCallback progress_fn) {
std::string prs_compress_indexed(const std::string& data, ProgressCallback progress_fn) {
return prs_compress_indexed(data.data(), data.size(), progress_fn);
}
@@ -846,7 +920,7 @@ PRSDecompressResult prs_decompress_with_meta(
if (allow_unterminated) {
return {std::move(w.str()), r.where()};
} else {
throw runtime_error("maximum output size exceeded");
throw std::runtime_error("maximum output size exceeded");
}
}
w.put_u8(r.get_u8());
@@ -882,14 +956,14 @@ PRSDecompressResult prs_decompress_with_meta(
// support ranges that cover the current end of the output.
size_t read_offset = w.size() + offset;
if (read_offset >= w.size()) {
throw runtime_error("backreference offset beyond beginning of output");
throw std::runtime_error("backreference offset beyond beginning of output");
}
for (size_t z = 0; z < count; z++) {
if (max_output_size && w.size() == max_output_size) {
if (allow_unterminated) {
return {std::move(w.str()), r.where()};
} else {
throw out_of_range("maximum output size exceeded");
throw std::out_of_range("maximum output size exceeded");
}
}
w.put_u8(w.str()[read_offset + z]);
@@ -900,16 +974,16 @@ PRSDecompressResult prs_decompress_with_meta(
return {std::move(w.str()), r.where()};
}
PRSDecompressResult prs_decompress_with_meta(const string& data, size_t max_output_size, bool allow_unterminated) {
PRSDecompressResult prs_decompress_with_meta(const std::string& data, size_t max_output_size, bool allow_unterminated) {
return prs_decompress_with_meta(data.data(), data.size(), max_output_size, allow_unterminated);
}
string prs_decompress(const void* data, size_t size, size_t max_output_size, bool allow_unterminated) {
std::string prs_decompress(const void* data, size_t size, size_t max_output_size, bool allow_unterminated) {
auto ret = prs_decompress_with_meta(data, size, max_output_size, allow_unterminated);
return std::move(ret.data);
}
string prs_decompress(const string& data, size_t max_output_size, bool allow_unterminated) {
std::string prs_decompress(const std::string& data, size_t max_output_size, bool allow_unterminated) {
auto ret = prs_decompress_with_meta(data.data(), data.size(), max_output_size, allow_unterminated);
return std::move(ret.data);
}
@@ -945,7 +1019,7 @@ size_t prs_decompress_size(const void* data, size_t size, size_t max_output_size
size_t read_offset = ret + offset;
if (read_offset >= ret) {
throw runtime_error("backreference offset beyond beginning of output");
throw std::runtime_error("backreference offset beyond beginning of output");
}
ret += count;
}
@@ -954,7 +1028,7 @@ size_t prs_decompress_size(const void* data, size_t size, size_t max_output_size
if (allow_unterminated) {
return max_output_size;
} else {
throw out_of_range("maximum output size exceeded");
throw std::out_of_range("maximum output size exceeded");
}
}
}
@@ -962,7 +1036,7 @@ size_t prs_decompress_size(const void* data, size_t size, size_t max_output_size
return ret;
}
size_t prs_decompress_size(const string& data, size_t max_output_size, bool allow_unterminated) {
size_t prs_decompress_size(const std::string& data, size_t max_output_size, bool allow_unterminated) {
return prs_decompress_size(data.data(), data.size(), max_output_size, allow_unterminated);
}
@@ -1015,7 +1089,7 @@ void prs_disassemble(FILE* stream, const void* data, size_t size) {
}
if (read_offset >= output_bytes) {
throw runtime_error("backreference offset beyond beginning of output");
throw std::runtime_error("backreference offset beyond beginning of output");
}
output_bytes += count;
}
@@ -1043,23 +1117,22 @@ struct BC0PathNode {
size_t to_offset = 0;
};
string bc0_compress_optimal(
const void* in_data_v, size_t in_size, ProgressCallback progress_fn) {
std::string bc0_compress_optimal(const void* in_data_v, size_t in_size, ProgressCallback progress_fn) {
const uint8_t* in_data = reinterpret_cast<const uint8_t*>(in_data_v);
vector<BC0PathNode> nodes;
std::vector<BC0PathNode> nodes;
nodes.resize(in_size + 1);
nodes[0].bits_used = 0;
// Populate all possible backreferences
{
WindowIndex<0x1000, 0x12> window(in_data_v, in_size);
while (window.offset < in_size) {
if ((window.offset & 0xFFF) == 0 && progress_fn) {
progress_fn(CompressPhase::INDEX, window.offset, in_size, 0);
while (window.current_offset < in_size) {
if ((window.current_offset & 0xFFF) == 0 && progress_fn) {
progress_fn(CompressPhase::INDEX, window.current_offset, in_size, 0);
}
auto& node = nodes[window.offset];
auto match = window.get_best_match();
auto& node = nodes[window.current_offset];
auto match = window.match();
if (match.second >= 3) {
node.memo_offset = (match.first - 0x12) & 0xFFF;
node.max_copy_size = match.second;
@@ -1140,24 +1213,24 @@ string bc0_compress_optimal(
return std::move(w.close());
}
string bc0_compress(const string& data, ProgressCallback progress_fn) {
std::string bc0_compress(const std::string& data, ProgressCallback progress_fn) {
return bc0_compress(data.data(), data.size(), progress_fn);
}
string bc0_compress(const void* in_data_v, size_t in_size, ProgressCallback progress_fn) {
std::string bc0_compress(const void* in_data_v, size_t in_size, ProgressCallback progress_fn) {
const uint8_t* in_data = reinterpret_cast<const uint8_t*>(in_data_v);
LZSSInterleavedWriter w;
WindowIndex<0x1000, 0x12> window(in_data_v, in_size);
size_t last_progress_fn_call_offset = 0;
while (window.offset < in_size) {
if (progress_fn && ((last_progress_fn_call_offset & ~0xFFF) != (window.offset & ~0xFFF))) {
last_progress_fn_call_offset = window.offset;
progress_fn(CompressPhase::GENERATE_RESULT, window.offset, in_size, w.size());
while (window.current_offset < in_size) {
if (progress_fn && ((last_progress_fn_call_offset & ~0xFFF) != (window.current_offset & ~0xFFF))) {
last_progress_fn_call_offset = window.current_offset;
progress_fn(CompressPhase::GENERATE_RESULT, window.current_offset, in_size, w.size());
}
auto match = window.get_best_match();
auto match = window.match();
// Write a backreference if a match was found; otherwise, write a literal
if (match.second >= 3) {
@@ -1167,7 +1240,7 @@ string bc0_compress(const void* in_data_v, size_t in_size, ProgressCallback prog
w.write_data(((memo_offset >> 4) & 0xF0) | (match.second - 3));
} else {
w.write_control(true);
w.write_data(in_data[window.offset]);
w.write_data(in_data[window.current_offset]);
match.second = 1;
}
w.flush_if_ready();
@@ -1180,7 +1253,7 @@ string bc0_compress(const void* in_data_v, size_t in_size, ProgressCallback prog
return std::move(w.close());
}
string bc0_encode(const void* in_data_v, size_t in_size) {
std::string bc0_encode(const void* in_data_v, size_t in_size) {
const uint8_t* in_data = reinterpret_cast<const uint8_t*>(in_data_v);
LZSSInterleavedWriter w;
@@ -1197,11 +1270,11 @@ string bc0_encode(const void* in_data_v, size_t in_size) {
// the output buffer. It is unlikely that this can be usefully exploited (e.g. for RCE) because the output pointer is
// loaded from memory before every byte is written, so we cannot change the output pointer to any arbitrary address.
string bc0_decompress(const string& data) {
std::string bc0_decompress(const std::string& data) {
return bc0_decompress(data.data(), data.size());
}
string bc0_decompress(const void* data, size_t size) {
std::string bc0_decompress(const void* data, size_t size) {
phosg::StringReader r(data, size);
phosg::StringWriter w;
@@ -1265,7 +1338,7 @@ string bc0_decompress(const void* data, size_t size) {
return std::move(w.str());
}
void bc0_disassemble(FILE* stream, const string& data) {
void bc0_disassemble(FILE* stream, const std::string& data) {
bc0_disassemble(stream, data.data(), data.size());
}
+27 -28
View File
@@ -9,8 +9,6 @@
#include "PSOEncryption.hh"
using namespace std;
static const uint32_t primes1[] = {
0x65, 0x67, 0x6B, 0x6D, 0x71, 0x7F, 0x83, 0x89, 0x8B, 0x95, 0x97, 0x9D, 0xA3, 0xA7, 0xAD, 0xB3, 0xB5, 0xBF, 0xC1,
0xC5, 0xC7, 0xD3, 0xDF, 0xE3, 0xE5, 0xE9, 0xEF, 0xF1, 0xFB, 0x101, 0x107, 0x10D, 0x10F, 0x115, 0x119, 0x11B, 0x125,
@@ -735,10 +733,10 @@ static const uint32_t primes3[] = {
static constexpr size_t num_primes3 = sizeof(primes3) / sizeof(primes3[0]);
static bool check_prime3(uint64_t prime3) {
static vector<bool> primes3_set;
static mutex primes3_init_mutex;
static std::vector<bool> primes3_set;
static std::mutex primes3_init_mutex;
if (primes3_set.empty()) {
lock_guard g(primes3_init_mutex);
std::lock_guard g(primes3_init_mutex);
if (primes3_set.empty()) {
size_t primes3_set_size = primes3[num_primes3 - 1] - primes3[0] + 1;
primes3_set.resize(primes3_set_size, false);
@@ -790,7 +788,7 @@ static char replace_char_reverse(char ch) {
static constexpr uint64_t INVALID_PRODUCT = 0xFFFFFFFFFFFFFFFF;
static uint64_t decode_dc_serial_number_str(const string& s) {
static uint64_t decode_dc_serial_number_str(const std::string& s) {
if (s.size() != 8) {
return INVALID_PRODUCT;
}
@@ -828,20 +826,20 @@ static uint32_t encode_dc_serial_number_int(uint32_t v) {
(replace_nybble_reverse(v));
}
static pair<size_t, size_t> compute_offset1_and_limit1(uint8_t domain, uint8_t subdomain) {
static std::pair<size_t, size_t> compute_offset1_and_limit1(uint8_t domain, uint8_t subdomain) {
if (domain > 2) {
return make_pair(0, 0);
return std::make_pair(0, 0);
}
size_t domain_base = domain * 30;
if (subdomain != 0xFF) {
size_t subdomain_base = domain_base + (subdomain % 3);
return make_pair(subdomain_base, subdomain_base + 1);
return std::make_pair(subdomain_base, subdomain_base + 1);
} else {
return make_pair(domain_base, domain_base + 3);
return std::make_pair(domain_base, domain_base + 3);
}
}
bool dc_serial_number_is_valid_slow(const string& s, uint8_t domain, uint8_t subdomain) {
bool dc_serial_number_is_valid_slow(const std::string& s, uint8_t domain, uint8_t subdomain) {
uint64_t serial_number = decode_dc_serial_number_str(s);
if (serial_number == INVALID_PRODUCT) {
return false;
@@ -890,7 +888,7 @@ bool decoded_dc_serial_number_is_valid_fast(uint32_t serial_number, uint8_t doma
return false;
}
bool dc_serial_number_is_valid_fast(const string& s, uint8_t domain, uint8_t subdomain) {
bool dc_serial_number_is_valid_fast(const std::string& s, uint8_t domain, uint8_t subdomain) {
uint64_t serial_number = decode_dc_serial_number_str(s);
if (serial_number == INVALID_PRODUCT) {
return false;
@@ -902,7 +900,7 @@ bool dc_serial_number_is_valid_fast(uint32_t serial_number, uint8_t domain, uint
return decoded_dc_serial_number_is_valid_fast(decode_dc_serial_number_int(serial_number), domain, subdomain);
}
string generate_dc_serial_number(uint8_t domain, uint8_t subdomain) {
std::string generate_dc_serial_number(uint8_t domain, uint8_t subdomain) {
size_t offset1, limit1;
if (domain == 0) {
offset1 = 0x00;
@@ -914,7 +912,7 @@ string generate_dc_serial_number(uint8_t domain, uint8_t subdomain) {
offset1 = 0x3C;
limit1 = 0x3F;
} else {
throw runtime_error("invalid domain");
throw std::runtime_error("invalid domain");
}
size_t det1 = (subdomain == 0xFF) ? phosg::random_object<uint32_t>() : subdomain;
@@ -922,23 +920,23 @@ string generate_dc_serial_number(uint8_t domain, uint8_t subdomain) {
size_t index2 = phosg::random_object<uint32_t>() % num_primes2;
size_t index3 = phosg::random_object<uint32_t>() % num_primes3;
uint32_t value = primes1[index1] * primes2[index2] * primes3[index3];
string s = std::format("{:08X}", value);
std::string s = std::format("{:08X}", value);
string ret;
std::string ret;
for (char ch : s) {
ret.push_back(replace_char_reverse(ch));
}
return ret;
}
unordered_map<uint32_t, string> generate_all_dc_serial_numbers(uint8_t domain, uint8_t subdomain) {
std::unordered_map<uint32_t, std::string> generate_all_dc_serial_numbers(uint8_t domain, uint8_t subdomain) {
DCSerialNumberIterator iter;
if (domain < 3) {
iter.domain = domain;
iter.end_domain = domain + 1;
} else if (domain != 0xFF) {
throw runtime_error("invalid domain");
throw std::runtime_error("invalid domain");
}
if (subdomain < 3) {
@@ -946,11 +944,11 @@ unordered_map<uint32_t, string> generate_all_dc_serial_numbers(uint8_t domain, u
iter.start_subdomain = subdomain;
iter.end_subdomain = subdomain + 1;
} else if (subdomain != 0xFF) {
throw runtime_error("invalid subdomain");
throw std::runtime_error("invalid subdomain");
}
uint32_t serial_number;
unordered_map<uint32_t, string> ret;
std::unordered_map<uint32_t, std::string> ret;
while ((serial_number = iter.next()) != 0) {
ret[serial_number].push_back(((iter.domain << 2) & 3) | (iter.subdomain & 3));
if (iter.index3 == 0) {
@@ -1015,7 +1013,7 @@ void dc_serial_number_speed_test(uint64_t seed) {
size_t num_disagreements = 0;
static constexpr size_t count = 0x1000;
for (size_t z = 0; z < count; z++) {
string s = std::format("{:08X}", crypt.next());
std::string s = std::format("{:08X}", crypt.next());
uint64_t start = phosg::now();
bool is_valid_fast = dc_serial_number_is_valid_fast(s, 1, 0xFF);
@@ -1041,7 +1039,8 @@ void dc_serial_number_speed_test(uint64_t seed) {
phosg::fwrite_fmt(stderr, "Disagreements: {}\n", num_disagreements);
}
string decrypt_dp_address_jpn(const string& executable, const string& values, const string& indexes) {
std::string decrypt_dp_address_jpn(
const std::string& executable, const std::string& values, const std::string& indexes) {
phosg::StringReader values_r(values);
phosg::StringReader indexes_r(indexes);
@@ -1056,7 +1055,7 @@ string decrypt_dp_address_jpn(const string& executable, const string& values, co
fixup_offset += (fixup_steps_r.get_u8() << 2);
fixup_steps_r.skip(1);
if (fixup_offset + 4 > decrypted.compressed_data.size()) {
throw runtime_error("fixup beyond end of compressed data");
throw std::runtime_error("fixup beyond end of compressed data");
}
*reinterpret_cast<le_uint32_t*>(decrypted.compressed_data.data() + fixup_offset) = fixup_values_r.get_u32l();
}
@@ -1064,10 +1063,10 @@ string decrypt_dp_address_jpn(const string& executable, const string& values, co
return prs_decompress(decrypted.compressed_data);
}
EncryptedDCv2Executables encrypt_dp_address_jpn(const string& executable, const string& indexes) {
EncryptedDCv2Executables encrypt_dp_address_jpn(const std::string& executable, const std::string& indexes) {
EncryptedDCv2Executables ret;
string compressed = prs_compress(executable);
std::string compressed = prs_compress(executable);
ret.executable = encrypt_pr2_data<false>(compressed, executable.size(), phosg::random_object<uint32_t>() & 0x7FFFFF7F);
phosg::StringReader indexes_r(indexes);
@@ -1079,12 +1078,12 @@ EncryptedDCv2Executables encrypt_dp_address_jpn(const string& executable, const
std::string crypt_dp_address_jpn_simple(const std::string& data, int64_t mask_key) {
if (data.size() & 3) {
throw runtime_error("size is not a multiple of 4");
throw std::runtime_error("size is not a multiple of 4");
}
phosg::StringReader r(data);
if (mask_key < 0) {
unordered_map<uint32_t, size_t> key_freq;
std::unordered_map<uint32_t, size_t> key_freq;
while (!r.eof()) {
key_freq[r.get_u32l()] += 1;
}
@@ -1096,7 +1095,7 @@ std::string crypt_dp_address_jpn_simple(const std::string& data, int64_t mask_ke
}
}
if (mask_key < 0) {
throw runtime_error("cannot determine mask key");
throw std::runtime_error("cannot determine mask key");
}
phosg::log_info_f("Determined {:08X} to be the most likely mask key", mask_key);
r.go(0);
+11 -14
View File
@@ -14,10 +14,7 @@
#include "NetworkAddresses.hh"
#include "ServerState.hh"
using namespace std;
DNSServer::DNSServer(shared_ptr<ServerState> state)
: state(state) {}
DNSServer::DNSServer(std::shared_ptr<ServerState> state) : state(state) {}
void DNSServer::listen(const std::string& addr, int port) {
if (port == 0) {
@@ -25,15 +22,15 @@ void DNSServer::listen(const std::string& addr, int port) {
}
asio::ip::address asio_addr = addr.empty() ? asio::ip::address_v4::any() : asio::ip::make_address(addr);
asio::ip::udp::endpoint endpoint(asio_addr, port);
auto sock = make_shared<asio::ip::udp::socket>(*this->state->io_context, endpoint);
auto sock = std::make_shared<asio::ip::udp::socket>(*this->state->io_context, endpoint);
this->sockets.emplace(sock);
asio::co_spawn(*this->state->io_context, this->dns_server_task(sock), asio::detached);
}
string DNSServer::response_for_query(const void* vdata, size_t size, uint32_t resolved_address) {
std::string DNSServer::response_for_query(const void* vdata, size_t size, uint32_t resolved_address) {
if (size < 0x0C) {
throw invalid_argument("query too small");
throw std::invalid_argument("query too small");
}
const char* data = reinterpret_cast<const char*>(vdata);
@@ -41,7 +38,7 @@ string DNSServer::response_for_query(const void* vdata, size_t size, uint32_t re
phosg::be_uint32_t be_resolved_address = resolved_address;
string response;
std::string response;
response.append(data, 2);
response.append("\x81\x80\x00\x01\x00\x01\x00\x00\x00\x00", 10);
response.append(&data[12], name_len);
@@ -50,13 +47,13 @@ string DNSServer::response_for_query(const void* vdata, size_t size, uint32_t re
return response;
}
string DNSServer::response_for_query(const string& query, uint32_t resolved_address) {
std::string DNSServer::response_for_query(const std::string& query, uint32_t resolved_address) {
return DNSServer::response_for_query(query.data(), query.size(), resolved_address);
}
asio::awaitable<void> DNSServer::dns_server_task(std::shared_ptr<asio::ip::udp::socket> sock) {
for (;;) {
string input(2048, 0);
std::string input(2048, 0);
asio::ip::udp::endpoint sender_ep;
size_t bytes = co_await sock->async_receive_from(asio::buffer(input), sender_ep, asio::use_awaitable);
uint32_t sender_addr = ipv4_addr_for_asio_addr(sender_ep.address());
@@ -64,12 +61,12 @@ asio::awaitable<void> DNSServer::dns_server_task(std::shared_ptr<asio::ip::udp::
if (bytes < 0x0C) {
dns_server_log.warning_f("input query too small");
phosg::print_data(stderr, input.data(), bytes);
} else if (!this->state->banned_ipv4_ranges->check(sender_addr)) {
} else if (!this->state->data->banned_ipv4_ranges->check(sender_addr)) {
input.resize(bytes);
uint32_t connect_address = is_local_address(sender_addr)
? this->state->local_address
: this->state->external_address;
string response = this->response_for_query(input, connect_address);
? this->state->data->local_address
: this->state->data->external_address;
std::string response = this->response_for_query(input, connect_address);
co_await sock->async_send_to(asio::buffer(response.data(), response.size()), sender_ep, asio::use_awaitable);
}
}
+1 -1
View File
@@ -8,7 +8,7 @@
#include "IPV4RangeSet.hh"
struct ServerState;
class ServerState;
class DNSServer {
public:
+95
View File
@@ -0,0 +1,95 @@
#include "DOLFileIndex.hh"
#include <stdio.h>
#include <string.h>
#include <filesystem>
#include <phosg/Filesystem.hh>
#include <phosg/Hash.hh>
#include <phosg/Time.hh>
#include <stdexcept>
#include <resource_file/Emulators/PPC32Emulator.hh>
#include <resource_file/Emulators/SH4Emulator.hh>
#include <resource_file/Emulators/X86Emulator.hh>
#include "CommandFormats.hh"
#include "CommonFileFormats.hh"
#include "Compression.hh"
#include "Loggers.hh"
DOLFileIndex::DOLFileIndex(const std::string& directory) {
if (!std::filesystem::is_directory(directory)) {
client_functions_log.info_f("DOL file directory is missing");
return;
}
auto menu = std::make_shared<Menu>(MenuID::PROGRAMS, "Programs");
this->menu = menu;
menu->items.emplace_back(ProgramsMenuItemID::GO_BACK, "Go back", "Return to the\nmain menu", 0);
std::vector<std::string> filenames;
for (const auto& item : std::filesystem::directory_iterator(directory)) {
filenames.emplace_back(item.path().filename().string());
}
std::sort(filenames.begin(), filenames.end());
uint32_t next_menu_item_id = 0;
for (const auto& filename : filenames) {
bool is_dol = filename.ends_with(".dol");
bool is_compressed_dol = filename.ends_with(".dol.prs");
if (!is_dol && !is_compressed_dol) {
continue;
}
std::string name = filename.substr(0, filename.size() - (is_compressed_dol ? 8 : 4));
try {
auto dol = std::make_shared<File>();
dol->menu_item_id = next_menu_item_id++;
dol->name = name;
std::string path = directory + "/" + filename;
std::string file_data = phosg::load_file(path);
std::string description;
if (is_compressed_dol) {
size_t decompressed_size = prs_decompress_size(file_data);
phosg::StringWriter w;
w.put_u32b(file_data.size());
w.put_u32b(decompressed_size);
w.write(file_data);
while (w.size() & 3) {
w.put_u8(0);
}
dol->data = std::move(w.str());
std::string compressed_size_str = phosg::format_size(file_data.size());
std::string decompressed_size_str = phosg::format_size(decompressed_size);
client_functions_log.debug_f("Loaded compressed DOL file {} ({} -> {})",
dol->name, compressed_size_str, decompressed_size_str);
description = std::format("$C6{}$C7\n{}\n{} (orig)", dol->name, compressed_size_str, decompressed_size_str);
} else {
phosg::StringWriter w;
w.put_u32b(0);
w.put_u32b(file_data.size());
w.write(file_data);
while (w.size() & 3) {
w.put_u8(0);
}
dol->data = std::move(w.str());
std::string size_str = phosg::format_size(dol->data.size());
client_functions_log.debug_f("Loaded DOL file {} ({})", filename, size_str);
description = std::format("$C6{}$C7\n{}", dol->name, size_str);
}
this->item_id_to_file.emplace_back(dol);
menu->items.emplace_back(dol->menu_item_id, dol->name, description, MenuItem::Flag::REQUIRES_SEND_FUNCTION_CALL_RUNS_CODE);
} catch (const std::exception& e) {
client_functions_log.warning_f("Failed to load DOL file {}: {}", filename, e.what());
}
}
}
+31
View File
@@ -0,0 +1,31 @@
#pragma once
#include <inttypes.h>
#include <map>
#include <memory>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include "Menu.hh"
struct DOLFileIndex {
struct File {
uint32_t menu_item_id;
std::string name;
std::string data;
bool is_compressed;
};
std::vector<std::shared_ptr<File>> item_id_to_file;
std::shared_ptr<const Menu> menu;
DOLFileIndex() = default;
explicit DOLFileIndex(const std::string& directory);
inline bool empty() const {
return this->item_id_to_file.empty();
}
};
+2063
View File
File diff suppressed because it is too large Load Diff
+421
View File
@@ -0,0 +1,421 @@
#pragma once
#include <atomic>
#include <map>
#include <memory>
#include <phosg/JSON.hh>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#include "Account.hh"
#include "Client.hh"
#include "ClientFunctionIndex.hh"
#include "CommonItemSet.hh"
#include "DNSServer.hh"
#include "DOLFileIndex.hh"
#include "Episode3/DataIndexes.hh"
#include "Episode3/Tournament.hh"
#include "GSLArchive.hh"
#include "IPV4RangeSet.hh"
#include "ItemNameIndex.hh"
#include "ItemParameterTable.hh"
#include "ItemTranslationTable.hh"
#include "LevelTable.hh"
#include "Lobby.hh"
#include "MagMetadataTable.hh"
#include "Menu.hh"
#include "Quest.hh"
#include "ShopRandomSets.hh"
#include "TeamIndex.hh"
#include "TekkerAdjustmentSet.hh"
#include "WordSelectTable.hh"
struct DataIndex {
// This structure contains everything which is essentially immutable during the server's uptime - e.g. configuration,
// tables, item definitions, etc. which are only changed by reloading them from disk. Mutable structures, like
// AccountIndex and TeamIndex, are on ServerState instead.
struct PortConfiguration {
std::string name;
std::string addr; // Blank = listen on all interfaces (default)
uint16_t port;
Version version;
ServerBehavior behavior;
};
struct CheatFlags {
// This structure describes which behaviors are considered cheating (that is, require cheat mode to be enabled or the
// user to have the CHEAT_ANYWHERE account flag). A false value here means that that particular behavior is NOT
// cheating, so cheat mode is NOT required.
bool create_items = true;
bool edit_section_id = true;
bool edit_stats = true;
bool ep3_replace_assist = true;
bool ep3_unset_field_character = true;
bool infinite_hp_tp = true;
bool fast_kills = true;
bool insufficient_minimum_level = true;
bool override_random_seed = true;
bool override_section_id = true;
bool override_variations = true;
bool proxy_override_drops = true;
bool reset_materials = false;
bool warp = true;
CheatFlags() = default;
explicit CheatFlags(const phosg::JSON& json);
};
struct BBStreamFile {
struct Entry {
uint32_t offset;
uint32_t size;
uint32_t checksum; // crc32
std::string filename;
};
std::vector<Entry> entries;
std::string data;
};
enum class RunShellBehavior {
DEFAULT = 0,
ALWAYS,
NEVER,
};
enum class BehaviorSwitch {
OFF = 0,
OFF_BY_DEFAULT,
ON_BY_DEFAULT,
ON,
};
static inline bool behavior_enabled(BehaviorSwitch b) {
return (b == BehaviorSwitch::ON_BY_DEFAULT) || (b == BehaviorSwitch::ON);
}
static inline bool behavior_can_be_overridden(BehaviorSwitch b) {
return (b == BehaviorSwitch::OFF_BY_DEFAULT) || (b == BehaviorSwitch::ON_BY_DEFAULT);
}
uint64_t creation_time;
std::string config_filename;
std::shared_ptr<const phosg::JSON> config_json;
bool one_time_config_loaded = false;
size_t num_worker_threads = 0;
std::string name;
std::unordered_map<std::string, PortConfiguration> name_to_port_config;
std::unordered_map<uint16_t, PortConfiguration> number_to_port_config;
std::unordered_map<uint16_t, uint16_t> ip_stack_port_remap;
std::string username;
std::string dns_server_addr;
uint16_t dns_server_port = 0;
std::vector<std::string> ip_stack_addresses;
std::vector<std::string> ppp_stack_addresses;
std::vector<std::string> ppp_raw_addresses;
std::vector<std::string> http_addresses;
uint64_t client_ping_interval_usecs = 30000000;
uint64_t client_idle_timeout_usecs = 60000000;
uint64_t patch_client_idle_timeout_usecs = 300000000;
uint64_t psopeeps_dcv2_exp_multiplier = 5;
bool is_debug = false;
bool ip_stack_debug = false;
bool allow_unregistered_users = false;
bool allow_pc_nte = false;
bool use_temp_accounts_for_prototypes = true;
bool allow_same_account_concurrent_logins = true;
bool allow_same_account_concurrent_logins_across_client_sources = false;
std::array<uint16_t, NUM_VERSIONS> compatibility_groups = {};
bool enable_chat_commands = true;
char chat_command_sentinel = '\0'; // 0 = default (@ on 11/2000; $ on all other versions)
size_t num_backup_character_slots = 16;
std::unique_ptr<std::array<uint32_t, NUM_NON_PATCH_VERSIONS>> version_name_colors;
uint32_t client_customization_name_color = 0x00000000;
uint8_t allowed_drop_modes_v1_v2_normal = 0x1F;
uint8_t allowed_drop_modes_v1_v2_battle = 0x07;
uint8_t allowed_drop_modes_v1_v2_challenge = 0x07;
uint8_t allowed_drop_modes_v3_normal = 0x1F;
uint8_t allowed_drop_modes_v3_battle = 0x07;
uint8_t allowed_drop_modes_v3_challenge = 0x07;
uint8_t allowed_drop_modes_v4_normal = 0x1D; // CLIENT not allowed
uint8_t allowed_drop_modes_v4_battle = 0x05;
uint8_t allowed_drop_modes_v4_challenge = 0x05;
ServerDropMode default_drop_mode_v1_v2_normal = ServerDropMode::CLIENT;
ServerDropMode default_drop_mode_v1_v2_battle = ServerDropMode::CLIENT;
ServerDropMode default_drop_mode_v1_v2_challenge = ServerDropMode::CLIENT;
ServerDropMode default_drop_mode_v3_normal = ServerDropMode::CLIENT;
ServerDropMode default_drop_mode_v3_battle = ServerDropMode::CLIENT;
ServerDropMode default_drop_mode_v3_challenge = ServerDropMode::CLIENT;
ServerDropMode default_drop_mode_v4_normal = ServerDropMode::SERVER_SHARED;
ServerDropMode default_drop_mode_v4_battle = ServerDropMode::SERVER_SHARED;
ServerDropMode default_drop_mode_v4_challenge = ServerDropMode::SERVER_SHARED;
std::unordered_map<uint16_t, IntegralExpression> quest_flag_rewrites_v1_v2;
std::unordered_map<uint16_t, IntegralExpression> quest_flag_rewrites_v3;
std::unordered_map<uint16_t, IntegralExpression> quest_flag_rewrites_v4;
std::unordered_map<std::string, std::pair<uint8_t, uint32_t>> quest_counter_fields; // For $qfread command
uint64_t persistent_game_idle_timeout_usecs = 0;
std::unordered_map<uint32_t, int64_t> enable_send_function_call_quest_numbers;
bool enable_v3_v4_protected_subcommands = false;
bool ep3_infinite_meseta = false;
std::vector<uint32_t> ep3_defeat_player_meseta_rewards = {400, 500, 600, 700, 800};
std::vector<uint32_t> ep3_defeat_com_meseta_rewards = {100, 200, 300, 400, 500};
uint32_t ep3_final_round_meseta_bonus = 300;
bool ep3_jukebox_is_free = false;
uint32_t ep3_behavior_flags = 0;
bool hide_download_commands = true;
bool censor_credentials = true;
RunShellBehavior run_shell_behavior = RunShellBehavior::DEFAULT;
BehaviorSwitch cheat_mode_behavior = BehaviorSwitch::OFF_BY_DEFAULT;
bool default_switch_assist_enabled = false;
bool use_game_creator_section_id = false;
bool enable_bb_ship_selection_menu = false;
bool enable_brutal_peeps_mode = false;
bool enable_hardcore_mode = false;
bool enable_test_mode = false;
bool rare_notifs_enabled_for_client_drops = false;
bool default_rare_notifs_enabled_v1_v2 = false;
bool default_rare_notifs_enabled_v3_v4 = false;
std::unordered_set<uint32_t> notify_game_for_item_primary_identifiers_v1_v2;
std::unordered_set<uint32_t> notify_game_for_item_primary_identifiers_v3;
std::unordered_set<uint32_t> notify_game_for_item_primary_identifiers_v4;
std::unordered_set<uint32_t> notify_server_for_item_primary_identifiers_v1_v2;
std::unordered_set<uint32_t> notify_server_for_item_primary_identifiers_v3;
std::unordered_set<uint32_t> notify_server_for_item_primary_identifiers_v4;
bool notify_server_for_max_level_achieved = false;
std::vector<std::shared_ptr<const PSOBBEncryption::KeyFile>> bb_private_keys;
std::shared_ptr<const parray<uint8_t, 0x16C>> bb_default_keyboard_config;
std::shared_ptr<const parray<uint8_t, 0x38>> bb_default_joystick_config;
std::shared_ptr<const ClientFunctionIndex> client_functions;
std::shared_ptr<const PatchFileIndex> pc_patch_file_index;
std::shared_ptr<const PatchFileIndex> bb_patch_file_index;
std::unordered_map<uint64_t, std::shared_ptr<const MapFile>> map_file_for_source_hash;
std::map<uint32_t, std::array<std::shared_ptr<const MapFile>, NUM_VERSIONS>> map_files_for_free_play_key;
std::unordered_map<uint64_t, std::shared_ptr<const SuperMap>> supermap_for_source_hash_sum;
std::unordered_map<uint32_t, std::shared_ptr<const SuperMap>> supermap_for_free_play_key;
std::shared_ptr<const RoomLayoutIndex> room_layout_index;
std::shared_ptr<const BBStreamFile> bb_stream_file;
std::shared_ptr<const DOLFileIndex> dol_file_index;
std::shared_ptr<const Episode3::CardIndex> ep3_card_index;
std::shared_ptr<const Episode3::CardIndex> ep3_card_index_trial;
std::shared_ptr<const Episode3::MapIndex> ep3_map_index;
std::shared_ptr<const Episode3::COMDeckIndex> ep3_com_deck_index;
std::shared_ptr<const G_SetEXResultValues_Ep3_6xB4x4B> ep3_default_ex_values;
std::shared_ptr<const G_SetEXResultValues_Ep3_6xB4x4B> ep3_tournament_ex_values;
std::shared_ptr<const G_SetEXResultValues_Ep3_6xB4x4B> ep3_tournament_final_round_ex_values;
std::shared_ptr<const QuestCategoryIndex> quest_category_index;
std::shared_ptr<const QuestIndex> quest_index;
std::shared_ptr<const LevelTable> level_table_v1_v2;
std::shared_ptr<const LevelTable> level_table_v3;
std::shared_ptr<const LevelTable> level_table_v4;
std::shared_ptr<const BattleParamsIndex> battle_params;
std::shared_ptr<const GSLArchive> bb_data_gsl;
std::unordered_map<std::string, std::shared_ptr<const CommonItemSet>> common_item_sets;
std::unordered_map<std::string, std::shared_ptr<const RareItemSet>> rare_item_sets;
std::shared_ptr<const ArmorShopRandomSet> armor_random_set;
std::shared_ptr<const ToolShopRandomSet> tool_random_set;
std::array<std::shared_ptr<const WeaponShopRandomSet>, 4> weapon_random_sets; // Keyed on difficulty
std::shared_ptr<const TekkerAdjustmentSet> tekker_adjustment_set;
std::array<std::shared_ptr<const ItemParameterTable>, NUM_VERSIONS> item_parameter_tables;
std::shared_ptr<const ItemTranslationTable> item_translation_table;
std::array<std::shared_ptr<const ItemData::StackLimits>, NUM_VERSIONS> item_stack_limits_tables;
size_t bb_max_bank_items = 200;
size_t bb_max_bank_meseta = 999999;
std::shared_ptr<const MagMetadataTable> mag_metadata_table_dc_nte;
std::shared_ptr<const MagMetadataTable> mag_metadata_table_dc_11_2000;
std::shared_ptr<const MagMetadataTable> mag_metadata_table_v1;
std::shared_ptr<const MagMetadataTable> mag_metadata_table_v2;
std::shared_ptr<const MagMetadataTable> mag_metadata_table_v3;
std::shared_ptr<const MagMetadataTable> mag_metadata_table_v4;
std::shared_ptr<const TextIndex> text_index;
std::array<std::shared_ptr<const ItemNameIndex>, NUM_VERSIONS> item_name_indexes;
std::shared_ptr<const WordSelectTable> word_select_table;
std::array<std::shared_ptr<const SetDataTableBase>, NUM_VERSIONS> set_data_tables;
std::array<std::shared_ptr<const SetDataTableBase>, NUM_VERSIONS> set_data_tables_ep1_ult;
std::shared_ptr<const SetDataTableBase> bb_solo_set_data_table;
std::shared_ptr<const SetDataTableBase> bb_solo_set_data_table_ep1_ult;
std::array<std::shared_ptr<const MapState::RareEnemyRates>, 4> rare_enemy_rates_by_difficulty;
std::shared_ptr<const MapState::RareEnemyRates> rare_enemy_rates_challenge;
std::array<std::array<size_t, 4>, 3> min_levels_v1_v2; // Indexed as [episode][difficulty]
std::array<std::array<size_t, 4>, 3> min_levels_v3; // Indexed as [episode][difficulty]
std::array<std::array<size_t, 4>, 3> min_levels_v4; // Indexed as [episode][difficulty]
std::unordered_set<std::string> bb_required_patches;
std::unordered_set<std::string> auto_patches;
CheatFlags cheat_flags;
struct QuestF960Result {
uint32_t meseta_cost = 0;
uint32_t base_probability = 0;
uint32_t probability_upgrade = 0;
std::array<std::vector<ItemData>, 7> results;
QuestF960Result() = default;
QuestF960Result(
const phosg::JSON& json, std::shared_ptr<const ItemNameIndex> name_index, const ItemData::StackLimits& limits);
};
// Indexed as [type][difficulty][random_choice]
std::vector<std::vector<std::vector<ItemData>>> quest_F95E_results;
std::vector<std::pair<size_t, ItemData>> quest_F95F_results; // [(num_photon_tickets, item)]
std::vector<QuestF960Result> quest_F960_success_results;
QuestF960Result quest_F960_failure_results;
float bb_global_exp_multiplier = 1.0f;
int64_t dc_v2_exp_multiplier = 1;
int64_t gc_v3_exp_multiplier = 1;
float exp_share_multiplier = 0.5f;
float server_global_drop_rate_multiplier = 1.0f;
uint16_t ep3_card_auction_points = 0;
uint16_t ep3_card_auction_min_size = 0;
uint16_t ep3_card_auction_max_size = 0;
struct CardAuctionPoolEntry {
uint64_t probability;
uint16_t card_id;
uint16_t min_price;
};
std::vector<CardAuctionPoolEntry> ep3_card_auction_pool;
std::array<std::vector<uint16_t>, 5> ep3_trap_card_ids;
struct Ep3LobbyBannerEntry {
uint32_t type = 1;
uint32_t which; // See B9 documentation in CommandFormats.hh
std::string data;
};
std::vector<Ep3LobbyBannerEntry> ep3_lobby_banners;
std::shared_ptr<const IPV4RangeSet> banned_ipv4_ranges;
phosg::JSON team_reward_defs_json;
std::shared_ptr<const Menu> information_menu_v2;
std::shared_ptr<const Menu> information_menu_v3;
std::shared_ptr<const std::vector<std::string>> information_contents_v2;
std::shared_ptr<const std::vector<std::string>> information_contents_v3;
std::shared_ptr<const Menu> proxy_destinations_menu_dc;
std::shared_ptr<const Menu> proxy_destinations_menu_pc;
std::shared_ptr<const Menu> proxy_destinations_menu_gc;
std::shared_ptr<const Menu> proxy_destinations_menu_xb;
std::vector<std::pair<std::string, uint16_t>> proxy_destinations_dc;
std::vector<std::pair<std::string, uint16_t>> proxy_destinations_pc;
std::vector<std::pair<std::string, uint16_t>> proxy_destinations_gc;
std::vector<std::pair<std::string, uint16_t>> proxy_destinations_xb;
std::optional<std::pair<std::string, uint16_t>> proxy_destination_patch;
std::optional<std::pair<std::string, uint16_t>> proxy_destination_bb;
std::string welcome_message;
std::string pc_patch_server_message;
std::string bb_patch_server_message;
std::array<std::vector<uint32_t>, NUM_VERSIONS> public_lobby_search_orders;
std::vector<uint32_t> client_customization_public_lobby_search_order;
uint8_t pre_lobby_event = 0;
std::vector<uint8_t> per_lobby_events;
int32_t ep3_menu_song = -1;
std::map<std::string, uint32_t> all_addresses;
uint32_t local_address = 0;
uint32_t external_address = 0;
bool proxy_allow_save_files = true;
explicit DataIndex(const std::string& config_filename = "");
uint32_t connect_address_for_client(std::shared_ptr<Client> c) const;
uint16_t game_server_port_for_version(Version v) const;
std::shared_ptr<const Menu> information_menu(Version version) const;
std::shared_ptr<const Menu> proxy_destinations_menu(Version version) const;
const std::vector<std::pair<std::string, uint16_t>>& proxy_destinations(Version version) const;
std::shared_ptr<const SetDataTableBase> set_data_table(
Version version, Episode episode, GameMode mode, Difficulty difficulty) const;
inline std::shared_ptr<const WeaponShopRandomSet> weapon_random_set(Difficulty difficulty) const {
return this->weapon_random_sets.at(static_cast<size_t>(difficulty));
}
inline std::shared_ptr<const MapState::RareEnemyRates> rare_enemy_rates(Difficulty difficulty) const {
return this->rare_enemy_rates_by_difficulty.at(static_cast<size_t>(difficulty));
}
std::shared_ptr<const LevelTable> level_table(Version version) const;
std::shared_ptr<const ItemParameterTable> item_parameter_table(Version version) const;
std::shared_ptr<const ItemParameterTable> item_parameter_table_for_encode(Version version) const;
std::shared_ptr<const MagMetadataTable> mag_metadata_table(Version version) const;
std::shared_ptr<const ItemData::StackLimits> item_stack_limits(Version version) const;
std::shared_ptr<const ItemNameIndex> item_name_index_opt(Version version) const; // Returns null if missing
std::shared_ptr<const ItemNameIndex> item_name_index(Version version) const; // Throws if missing
std::string describe_item(Version version, const ItemData& item, uint8_t flags = 0) const;
ItemData parse_item_description(Version version, const std::string& description) const;
std::shared_ptr<const CommonItemSet> common_item_set(Version logic_version, std::shared_ptr<const Quest> q) const;
std::shared_ptr<const RareItemSet> rare_item_set(Version logic_version, std::shared_ptr<const Quest> q) const;
const std::vector<uint32_t>& public_lobby_search_order(Version version, bool is_client_customization) const;
inline const std::vector<uint32_t>& public_lobby_search_order(std::shared_ptr<const Client> c) const {
return this->public_lobby_search_order(c->version(), c->check_flag(Client::Flag::IS_CLIENT_CUSTOMIZATION));
}
inline uint32_t name_color_for_client(Version v, bool is_client_customization) const {
if (is_client_customization && this->client_customization_name_color) {
return this->client_customization_name_color;
}
return this->version_name_colors ? this->version_name_colors->at(static_cast<size_t>(v) - NUM_PATCH_VERSIONS) : 0;
}
inline uint32_t name_color_for_client(std::shared_ptr<const Client> c) const {
return this->name_color_for_client(c->version(), c->check_flag(Client::Flag::IS_CLIENT_CUSTOMIZATION));
}
std::shared_ptr<const std::vector<std::string>> information_contents_for_client(std::shared_ptr<const Client> c) const;
size_t default_min_level_for_game(Version version, Episode episode, Difficulty difficulty) const;
void set_port_configuration(const std::vector<PortConfiguration>& port_configs);
std::shared_ptr<const std::string> load_bb_file(const std::string& patch_index_filename) const;
std::shared_ptr<const std::string> load_map_file(Version version, const std::string& filename) const;
std::pair<std::string, uint16_t> parse_port_spec(const phosg::JSON& json) const;
std::vector<PortConfiguration> parse_port_configuration(const phosg::JSON& json) const;
static constexpr uint32_t free_play_key(
Episode episode, GameMode mode, Difficulty difficulty, uint8_t floor, uint32_t layout, uint32_t entities) {
return (static_cast<uint32_t>(episode) << 28) |
(static_cast<uint32_t>(mode) << 26) |
(static_cast<uint32_t>(difficulty) << 24) |
(static_cast<uint32_t>(floor) << 16) |
(static_cast<uint32_t>(layout) << 8) |
(static_cast<uint32_t>(entities) << 0);
}
std::shared_ptr<const SuperMap> get_free_play_supermap(
Episode episode, GameMode mode, Difficulty difficulty, uint8_t floor, uint32_t layout, uint32_t entities);
std::vector<std::shared_ptr<const SuperMap>> supermaps_for_variations(
Episode episode, GameMode mode, Difficulty difficulty, const Variations& variations);
void collect_network_addresses();
void load_config_early();
void load_config_late();
void load_bb_private_keys();
void load_bb_system_defaults();
void load_patch_indexes();
void load_maps();
void load_battle_params();
void load_level_tables();
void load_text_index();
std::shared_ptr<ItemNameIndex> create_item_name_index_for_version(
std::shared_ptr<const ItemParameterTable> pmt,
std::shared_ptr<const ItemData::StackLimits> limits,
std::shared_ptr<const TextIndex> text_index) const;
void load_item_name_indexes();
void load_drop_tables();
void load_item_definitions();
void load_set_data_tables();
void load_word_select_table();
void load_ep3_cards();
void load_ep3_maps(bool raise_on_any_failure = false);
void load_quest_index(bool raise_on_any_failure = false);
void compile_functions(bool raise_on_any_failure = false);
void load_dol_files();
void generate_bb_stream_file();
void load_all();
};
+58 -56
View File
@@ -22,12 +22,10 @@
#include "ReceiveSubcommands.hh"
#include "SendCommands.hh"
using namespace std;
static string random_name() {
string ret;
static std::string random_name() {
std::string ret;
size_t length = (phosg::random_object<size_t>() % 12) + 4;
static const string alphabet = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890-+<>:\"\',.";
static const std::string alphabet = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890-+<>:\"\',.";
while (ret.size() < length) {
ret.push_back(alphabet[phosg::random_object<size_t>() % alphabet.size()]);
}
@@ -87,40 +85,40 @@ DownloadSession::DownloadSession(
case Version::DC_V1:
case Version::DC_V2:
if (this->serial_number2 == 0 || this->serial_number == 0 || this->access_key.empty()) {
throw runtime_error("missing credentials");
throw std::runtime_error("missing credentials");
}
break;
case Version::PC_V2:
if (this->serial_number == 0 || this->access_key.empty()) {
throw runtime_error("missing credentials");
throw std::runtime_error("missing credentials");
}
break;
case Version::GC_V3:
if (this->serial_number == 0 || this->access_key.empty() || this->password.empty()) {
throw runtime_error("missing credentials");
throw std::runtime_error("missing credentials");
}
break;
case Version::XB_V3:
if (this->xb_gamertag.empty() || this->xb_user_id == 0 || this->xb_account_id == 0) {
throw runtime_error("missing credentials");
throw std::runtime_error("missing credentials");
}
break;
case Version::BB_V4:
if (this->username.empty() || this->password.empty()) {
throw runtime_error("missing credentials");
throw std::runtime_error("missing credentials");
}
break;
default:
throw runtime_error("unsupported version");
throw std::runtime_error("unsupported version");
}
this->character->inventory.language = language;
}
asio::awaitable<void> DownloadSession::run() {
string netloc_str = std::format("{}:{}", this->remote_host, this->remote_port);
std::string netloc_str = std::format("{}:{}", this->remote_host, this->remote_port);
this->log.info_f("Connecting to {}", netloc_str);
auto sock = make_unique<asio::ip::tcp::socket>(co_await async_connect_tcp(this->remote_host, this->remote_port));
auto sock = std::make_unique<asio::ip::tcp::socket>(co_await async_connect_tcp(this->remote_host, this->remote_port));
this->channel = SocketChannel::create(
this->io_context,
std::move(sock),
@@ -128,7 +126,9 @@ asio::awaitable<void> DownloadSession::run() {
this->language,
netloc_str,
this->show_command_data ? phosg::TerminalFormat::FG_GREEN : phosg::TerminalFormat::END,
this->show_command_data ? phosg::TerminalFormat::FG_YELLOW : phosg::TerminalFormat::END);
this->show_command_data ? phosg::TerminalFormat::FG_YELLOW : phosg::TerminalFormat::END,
false,
false);
this->log.info_f("Server channel connected");
while (this->channel->connected()) {
@@ -149,7 +149,7 @@ void DownloadSession::send_93_9D_9E(bool extended) {
ret.serial_number.encode(std::format("{:08X}", this->serial_number));
ret.access_key.encode(this->access_key);
ret.serial_number2.encode(std::format("{:08X}", this->serial_number2));
ret.login_character_name.encode(this->character->disp.name.decode());
ret.login_character_name.encode(this->character->disp.visual.name.decode());
this->channel->send(0x93, 0x01, &ret, extended ? sizeof(ret) : sizeof(C_LoginV1_DC_93));
} else if (is_v2(this->version)) {
@@ -164,7 +164,7 @@ void DownloadSession::send_93_9D_9E(bool extended) {
ret.access_key.encode(this->access_key);
ret.serial_number2 = ret.serial_number;
ret.access_key2 = ret.access_key;
ret.login_character_name.encode(this->character->disp.name.decode());
ret.login_character_name.encode(this->character->disp.visual.name.decode());
size_t data_size = extended
? ((this->version == Version::PC_V2) ? sizeof(ret) : sizeof(C_LoginExtended_DC_GC_9D))
: sizeof(C_Login_DC_PC_GC_9D);
@@ -182,7 +182,7 @@ void DownloadSession::send_93_9D_9E(bool extended) {
ret.access_key.encode(this->access_key);
ret.serial_number2 = ret.serial_number;
ret.access_key2 = ret.access_key;
ret.login_character_name.encode(this->character->disp.name.decode());
ret.login_character_name.encode(this->character->disp.visual.name.decode());
ret.client_config = this->client_config;
this->channel->send(0x9E, 0x01, &ret, extended ? sizeof(ret) : sizeof(C_Login_PC_GC_9E));
@@ -198,7 +198,7 @@ void DownloadSession::send_93_9D_9E(bool extended) {
ret.access_key.encode(std::format("{:016X}", this->xb_user_id));
ret.serial_number2 = ret.serial_number;
ret.access_key2 = ret.access_key;
ret.login_character_name.encode(this->character->disp.name.decode());
ret.login_character_name.encode(this->character->disp.visual.name.decode());
ret.xb_netloc.internal_ipv4_address = phosg::random_object<uint32_t>();
ret.xb_netloc.external_ipv4_address = phosg::random_object<uint32_t>();
ret.xb_netloc.port = 9500;
@@ -212,7 +212,7 @@ void DownloadSession::send_93_9D_9E(bool extended) {
this->channel->send(0x9E, 0x01, &ret, extended ? sizeof(ret) : sizeof(C_Login_DC_PC_GC_9D));
} else {
throw runtime_error("unsupported version");
throw std::runtime_error("unsupported version");
}
}
@@ -222,13 +222,13 @@ void DownloadSession::send_61_98(bool is_98) {
if (is_v1(this->version)) {
C_CharacterData_DCv1_61_98 ret;
ret.inventory = this->character->inventory;
ret.disp = convert_player_disp_data<PlayerDispDataDCPCV3, PlayerDispDataBB>(this->character->disp, Language::ENGLISH, Language::ENGLISH);
ret.disp = convert_player_disp_data<PlayerDispDataV123, PlayerDispDataV4>(this->character->disp, Language::ENGLISH, Language::ENGLISH);
this->channel->send(command, 0x01, ret);
} else if (this->version == Version::DC_V2) {
C_CharacterData_DCv2_61_98 ret;
ret.inventory = this->character->inventory;
ret.disp = convert_player_disp_data<PlayerDispDataDCPCV3, PlayerDispDataBB>(this->character->disp, Language::ENGLISH, Language::ENGLISH);
ret.disp = convert_player_disp_data<PlayerDispDataV123, PlayerDispDataV4>(this->character->disp, Language::ENGLISH, Language::ENGLISH);
ret.records.challenge = this->character->challenge_records;
ret.records.battle = this->character->battle_records;
ret.choice_search_config = this->character->choice_search_config;
@@ -237,7 +237,7 @@ void DownloadSession::send_61_98(bool is_98) {
} else if (this->version == Version::PC_V2) {
C_CharacterData_PC_61_98 ret;
ret.inventory = this->character->inventory;
ret.disp = convert_player_disp_data<PlayerDispDataDCPCV3, PlayerDispDataBB>(this->character->disp, Language::ENGLISH, Language::ENGLISH);
ret.disp = convert_player_disp_data<PlayerDispDataV123, PlayerDispDataV4>(this->character->disp, Language::ENGLISH, Language::ENGLISH);
ret.records.challenge = this->character->challenge_records;
ret.records.battle = this->character->battle_records;
ret.choice_search_config = this->character->choice_search_config;
@@ -246,7 +246,7 @@ void DownloadSession::send_61_98(bool is_98) {
} else if (is_v3(this->version)) {
C_CharacterData_V3_61_98 ret;
ret.inventory = this->character->inventory;
ret.disp = convert_player_disp_data<PlayerDispDataDCPCV3, PlayerDispDataBB>(this->character->disp, Language::ENGLISH, Language::ENGLISH);
ret.disp = convert_player_disp_data<PlayerDispDataV123, PlayerDispDataV4>(this->character->disp, Language::ENGLISH, Language::ENGLISH);
ret.records.challenge = this->character->challenge_records;
ret.records.battle = this->character->battle_records;
ret.choice_search_config = this->character->choice_search_config;
@@ -264,7 +264,7 @@ void DownloadSession::send_61_98(bool is_98) {
this->channel->send(command, 0x04, ret);
} else {
throw runtime_error("unsupported version");
throw std::runtime_error("unsupported version");
}
}
@@ -276,16 +276,16 @@ asio::awaitable<void> DownloadSession::on_message(Channel::Message& msg) {
switch (msg.command) {
case 0x03: {
if (this->version != Version::BB_V4) {
throw runtime_error("BB server sent non-BB encryption command");
throw std::runtime_error("BB server sent non-BB encryption command");
}
if (!this->bb_key_file) {
throw runtime_error("BB encryption requires a key file");
throw std::runtime_error("BB encryption requires a key file");
}
const auto& cmd = msg.check_size_t<S_ServerInitDefault_BB_03_9B>(0xFFFF);
this->channel->crypt_in = make_shared<PSOBBEncryption>(*this->bb_key_file, &cmd.server_key[0], sizeof(cmd.server_key));
this->channel->crypt_out = make_shared<PSOBBEncryption>(*this->bb_key_file, &cmd.client_key[0], sizeof(cmd.client_key));
this->channel->crypt_in = std::make_shared<PSOBBEncryption>(*this->bb_key_file, &cmd.server_key[0], sizeof(cmd.server_key));
this->channel->crypt_out = std::make_shared<PSOBBEncryption>(*this->bb_key_file, &cmd.client_key[0], sizeof(cmd.client_key));
this->log.info_f("Enabled BB encryption");
throw runtime_error("not yet implemented"); // Send 93
throw std::runtime_error("not yet implemented"); // Send 93
break;
}
@@ -295,17 +295,17 @@ asio::awaitable<void> DownloadSession::on_message(Channel::Message& msg) {
case 0x9B: {
const auto& cmd = msg.check_size_t<S_ServerInitDefault_DC_PC_V3_02_17_91_9B>(0xFFFF);
if (uses_v3_encryption(this->version)) {
this->channel->crypt_in = make_shared<PSOV3Encryption>(cmd.server_key);
this->channel->crypt_out = make_shared<PSOV3Encryption>(cmd.client_key);
this->channel->crypt_in = std::make_shared<PSOV3Encryption>(cmd.server_key);
this->channel->crypt_out = std::make_shared<PSOV3Encryption>(cmd.client_key);
this->log.info_f("Enabled V3 encryption (server key {:08X}, client key {:08X})",
cmd.server_key, cmd.client_key);
} else if (!uses_v4_encryption(this->version)) {
this->channel->crypt_in = make_shared<PSOV2Encryption>(cmd.server_key);
this->channel->crypt_out = make_shared<PSOV2Encryption>(cmd.client_key);
this->channel->crypt_in = std::make_shared<PSOV2Encryption>(cmd.server_key);
this->channel->crypt_out = std::make_shared<PSOV2Encryption>(cmd.client_key);
this->log.info_f("Enabled V2 encryption (server key {:08X}, client key {:08X})",
cmd.server_key, cmd.client_key);
} else {
throw runtime_error("BB server sent non-BB encryption command");
throw std::runtime_error("BB server sent non-BB encryption command");
}
if (msg.command == 0x02) {
@@ -344,7 +344,7 @@ asio::awaitable<void> DownloadSession::on_message(Channel::Message& msg) {
this->send_93_9D_9E(true);
} else {
throw runtime_error("unsupported version");
throw std::runtime_error("unsupported version");
}
}
@@ -379,14 +379,14 @@ asio::awaitable<void> DownloadSession::on_message(Channel::Message& msg) {
this->channel->send(0x9C, 0x00, ret);
} else {
throw runtime_error("unsupported version");
throw std::runtime_error("unsupported version");
}
} else if (msg.flag == 0 || msg.flag == 2) {
this->send_93_9D_9E(true);
} else {
throw runtime_error("login failed");
throw std::runtime_error("login failed");
}
break;
}
@@ -394,14 +394,14 @@ asio::awaitable<void> DownloadSession::on_message(Channel::Message& msg) {
case 0x92:
case 0x9C:
if (msg.flag == 0) {
throw runtime_error("server rejected login credentials");
throw std::runtime_error("server rejected login credentials");
}
this->send_93_9D_9E(true);
break;
case 0x9F: {
if (is_v1_or_v2(this->version)) {
throw runtime_error("invalid command");
throw std::runtime_error("invalid command");
}
this->channel->send(0x9F, 0x00, this->client_config);
break;
@@ -475,11 +475,11 @@ asio::awaitable<void> DownloadSession::on_message(Channel::Message& msg) {
if (this->interactive) {
while (item_index == 0 || item_index > msg.flag) {
this->log.info_f("Choose response index:");
string input = phosg::fgets(stdin);
item_index = stoul(input, nullptr, 0);
std::string input = phosg::fgets(stdin);
item_index = std::stoul(input, nullptr, 0);
}
} else {
throw runtime_error("unhandled menu selection");
throw std::runtime_error("unhandled menu selection");
}
}
ret.menu_id = items[item_index].menu_id;
@@ -519,9 +519,9 @@ asio::awaitable<void> DownloadSession::on_message(Channel::Message& msg) {
const auto& cmd = msg.check_size_t<S_Reconnect_19>(sizeof(S_Reconnect_19), 0xFFFF);
auto new_ep = make_endpoint_ipv4(cmd.address, cmd.port);
string netloc_str = str_for_endpoint(new_ep);
std::string netloc_str = str_for_endpoint(new_ep);
this->log.info_f("Connecting to {}", netloc_str);
auto sock = make_unique<asio::ip::tcp::socket>(co_await async_connect_tcp(new_ep));
auto sock = std::make_unique<asio::ip::tcp::socket>(co_await async_connect_tcp(new_ep));
this->channel = SocketChannel::create(
this->io_context,
std::move(sock),
@@ -529,7 +529,9 @@ asio::awaitable<void> DownloadSession::on_message(Channel::Message& msg) {
this->language,
netloc_str,
this->show_command_data ? phosg::TerminalFormat::FG_GREEN : phosg::TerminalFormat::END,
this->show_command_data ? phosg::TerminalFormat::FG_YELLOW : phosg::TerminalFormat::END);
this->show_command_data ? phosg::TerminalFormat::FG_YELLOW : phosg::TerminalFormat::END,
false,
false);
this->log.info_f("Server channel connected");
break;
}
@@ -645,12 +647,12 @@ asio::awaitable<void> DownloadSession::on_message(Channel::Message& msg) {
case 0xA6: {
auto handle_command = [&]<typename CmdT>() {
const auto& cmd = msg.check_size_t<CmdT>(0xFFFF);
string internal_name = cmd.filename.decode();
string filtered_name;
std::string internal_name = cmd.filename.decode();
std::string filtered_name;
for (char ch : internal_name) {
filtered_name.push_back((isalnum(ch) || (ch == '-') || (ch == '.') || (ch == '_')) ? ch : '_');
}
string local_filename = std::format(
std::string local_filename = std::format(
"{}/{:016X}_{}_{}_{:c}_{}",
this->output_dir,
this->current_request,
@@ -673,7 +675,7 @@ asio::awaitable<void> DownloadSession::on_message(Channel::Message& msg) {
case 0x13:
case 0xA7: {
const auto& cmd = msg.check_size_t<S_WriteFile_13_A7>();
string internal_filename = cmd.filename.decode();
std::string internal_filename = cmd.filename.decode();
if (!is_v1_or_v2(this->version)) {
C_WriteFileConfirmation_V3_BB_13_A7 ret;
@@ -688,8 +690,8 @@ asio::awaitable<void> DownloadSession::on_message(Channel::Message& msg) {
}
auto& f = this->open_files.at(cmd.filename.decode());
size_t block_offset = msg.flag * 0x400;
size_t allowed_block_size = (block_offset < f.total_size) ? min<size_t>(f.total_size - block_offset, 0x400) : 0;
size_t data_size = min<size_t>(cmd.data_size, allowed_block_size);
size_t allowed_block_size = (block_offset < f.total_size) ? std::min<size_t>(f.total_size - block_offset, 0x400) : 0;
size_t data_size = std::min<size_t>(cmd.data_size, allowed_block_size);
size_t block_end_offset = block_offset + data_size;
if (block_end_offset > f.data.size()) {
f.data.resize(block_end_offset);
@@ -716,7 +718,7 @@ asio::awaitable<void> DownloadSession::on_message(Channel::Message& msg) {
}
case 0xAC: {
if (is_v1_or_v2(this->version)) {
throw runtime_error("unsupported version");
throw std::runtime_error("unsupported version");
}
this->on_request_complete();
break;
@@ -741,7 +743,7 @@ void DownloadSession::send_next_request() {
this->log.info_f("{:016X}: {}", it.first, it.second);
}
this->log.info_f("Choose item to expand by ID (q to quit; s to skip to next config):");
string input = phosg::fgets(stdin);
std::string input = phosg::fgets(stdin);
if (input.empty() || (input == "q\n")) {
this->channel->disconnect();
return;
@@ -749,7 +751,7 @@ void DownloadSession::send_next_request() {
this->pending_requests.clear();
this->on_request_complete();
} else {
this->current_request = stoull(input, nullptr, 16);
this->current_request = std::stoull(input, nullptr, 16);
this->done_requests.emplace(this->current_request);
this->pending_requests.erase(this->current_request);
}
@@ -809,7 +811,7 @@ void DownloadSession::on_request_complete() {
}
}
const std::vector<DownloadSession::GameConfig> DownloadSession::game_configs({
const std::vector<DownloadSession::GameConfig> DownloadSession::game_configs{
{.mode = GameMode::NORMAL, .episode = Episode::EP1, .v1 = true, .v2 = true, .v3 = true},
{.mode = GameMode::NORMAL, .episode = Episode::EP2, .v1 = false, .v2 = false, .v3 = true},
{.mode = GameMode::NORMAL, .episode = Episode::EP4, .v1 = false, .v2 = false, .v3 = false},
@@ -819,4 +821,4 @@ const std::vector<DownloadSession::GameConfig> DownloadSession::game_configs({
{.mode = GameMode::SOLO, .episode = Episode::EP1, .v1 = false, .v2 = false, .v3 = false},
{.mode = GameMode::SOLO, .episode = Episode::EP2, .v1 = false, .v2 = false, .v3 = false},
{.mode = GameMode::SOLO, .episode = Episode::EP4, .v1 = false, .v2 = false, .v3 = false},
});
};
+215 -157
View File
@@ -2,13 +2,12 @@
#include <phosg/Filesystem.hh>
#include <phosg/Strings.hh>
#include <set>
#include "Loggers.hh"
#include "PSOEncryption.hh"
#include "StaticGameData.hh"
using namespace std;
static constexpr uint8_t EP1 = EnemyTypeDefinition::Flag::VALID_EP1;
static constexpr uint8_t EP2 = EnemyTypeDefinition::Flag::VALID_EP2;
static constexpr uint8_t EP4 = EnemyTypeDefinition::Flag::VALID_EP4;
@@ -16,142 +15,142 @@ static constexpr uint8_t RARE = EnemyTypeDefinition::Flag::IS_RARE;
static constexpr uint8_t BOSS = EnemyTypeDefinition::Flag::IS_BOSS;
static constexpr uint8_t NONE = 0xFF;
static const vector<EnemyTypeDefinition> type_defs{
static const std::vector<EnemyTypeDefinition> type_defs{
// clang-format off
// TYPE FLAGS RT OLDRT BP-STATS BP-ATTACK BP-RESIST ENUM NAME IN-GAME NAME ULTIMATE NAME
{EnemyType::UNKNOWN, 0, NONE, NONE, {}, {}, {}, "UNKNOWN", "__UNKNOWN__", nullptr},
{EnemyType::NONE, 0, NONE, NONE, {}, {}, {}, "NONE", "__NONE__", nullptr},
{EnemyType::NON_ENEMY_NPC, EP1 | EP2 | EP4, NONE, NONE, {}, {}, {}, "NON_ENEMY_NPC", "__NPC__", nullptr},
{EnemyType::AL_RAPPY, EP1 | RARE, 0x06, 0x06, {0x19}, {0x19}, {0x19}, "AL_RAPPY", "Al Rappy", "Pal Rappy"},
{EnemyType::ASTARK, EP4, 0x58, 0x41, {0x09}, {0x0B, 0x0A, 0x0C}, {0x09}, "ASTARK", "Astark", nullptr},
{EnemyType::BA_BOOTA, EP4, 0x62, 0x4F, {0x03}, {0x03, 0x02, 0x04}, {0x03}, "BA_BOOTA", "Ba Boota", nullptr},
{EnemyType::BARBA_RAY, EP2 | BOSS, 0x49, 0x49, {0x0F}, {0x0F}, {0x0F}, "BARBA_RAY", "Barba Ray", nullptr},
{EnemyType::BARBA_RAY_JOINT, EP2 | BOSS, 0x49, 0x49, {0x10}, {0x0F}, {0x10}, "BARBA_RAY_JOINT", "Barba Ray (joint)", nullptr},
{EnemyType::BARBAROUS_WOLF, EP1 | EP2, 0x08, 0x08, {0x03}, {0x03}, {0x03}, "BARBAROUS_WOLF", "Barbarous Wolf", "Gulgus-gue"},
{EnemyType::BEE_L, EP1 | EP2, NONE, NONE, {0x0C}, {0x0C}, {0x0C}, "BEE_L", "Bee L", "Gee L"},
{EnemyType::BEE_R, EP1 | EP2, NONE, NONE, {0x0B}, {0x0B}, {0x0B}, "BEE_R", "Bee R", "Gee R"},
{EnemyType::BOOMA, EP1, 0x09, 0x09, {0x4B}, {0x4E}, {0x4A}, "BOOMA", "Booma", "Bartle"},
{EnemyType::BOOTA, EP4, 0x60, 0x4D, {0x00}, {0x00, 0x02, 0x04}, {0x00}, "BOOTA", "Boota", nullptr},
{EnemyType::BULCLAW, EP1, 0x28, 0x28, {0x1F}, {0x1F}, {0x1F}, "BULCLAW", "Bulclaw", nullptr},
{EnemyType::BULK, EP1, 0x27, 0x27, {0x1F}, {0x1F}, {0x1F}, "BULK", "Bulk", nullptr},
{EnemyType::CANADINE, EP1, 0x1C, 0x1C, {0x07}, {0x07}, {0x07}, "CANADINE", "Canadine", "Canabin"},
{EnemyType::CANADINE_GROUP, EP1, 0x1C, 0x1C, {0x08}, {0x08}, {0x08}, "CANADINE_GROUP", "Canadine (group)", "Canabin (group)"},
{EnemyType::CANANE, EP1, 0x1D, 0x1D, {0x09}, {0x09}, {0x09}, "CANANE", "Canane", "Canune"},
{EnemyType::CHAOS_BRINGER, EP1, 0x24, 0x24, {0x0D}, {0x0D}, {0x0D}, "CHAOS_BRINGER", "Chaos Bringer", "Dark Bringer"},
{EnemyType::CHAOS_SORCERER, EP1 | EP2, 0x1F, 0x1F, {0x0A}, {0x0A}, {0x0A}, "CHAOS_SORCERER", "Chaos Sorceror", "Gran Sorceror"},
{EnemyType::CLAW, EP1, 0x26, 0x26, {0x20}, {0x20}, {0x20}, "CLAW", "Claw", nullptr},
{EnemyType::DARK_BELRA, EP1 | EP2, 0x25, 0x25, {0x0E}, {0x0E, 0x13}, {0x0E}, "DARK_BELRA", "Dark Belra", "Indi Belra"},
{EnemyType::DARK_FALZ_1, EP1 | BOSS, NONE, NONE, {0x36}, {0x36}, {0x36}, "DARK_FALZ_1", "Dark Falz (phase 1)", nullptr},
{EnemyType::DARK_FALZ_2, EP1 | BOSS, 0x2F, 0x2F, {0x37}, {0x37}, {0x37}, "DARK_FALZ_2", "Dark Falz (phase 2)", nullptr},
{EnemyType::DARK_FALZ_3, EP1 | BOSS, 0x2F, 0x2F, {0x38}, {0x38}, {0x38}, "DARK_FALZ_3", "Dark Falz (phase 3)", nullptr},
{EnemyType::DARK_GUNNER, EP1, 0x22, 0x22, {0x1E}, {0x1E}, {0x1E}, "DARK_GUNNER", "Dark Gunner", nullptr},
{EnemyType::DARK_GUNNER_CONTROL, EP1, NONE, NONE, {}, {}, {}, "DARK_GUNNER_CONTROL", "Dark Gunner (control)", nullptr},
{EnemyType::DARVANT, EP1, NONE, NONE, {0x35}, {0x35}, {0x35}, "DARVANT", "Darvant", nullptr},
{EnemyType::DE_ROL_LE, EP1 | BOSS, 0x2D, 0x2D, {0x0F}, {0x0F}, {0x0F}, "DE_ROL_LE", "De Rol Le", "Dal Ral Lie"},
{EnemyType::DE_ROL_LE_BODY, EP1 | BOSS, NONE, NONE, {0x10}, {0x0F}, {0x10}, "DE_ROL_LE_BODY", "De Rol Le (body)", "Dal Ral Lie (body)"},
{EnemyType::DE_ROL_LE_MINE, EP1 | BOSS, NONE, NONE, {0x11}, {0x0F}, {0x11}, "DE_ROL_LE_MINE", "De Rol Le (mine)", "Dal Ral Lie (mine)"},
{EnemyType::DEATH_GUNNER, EP1, 0x23, 0x23, {0x1E}, {0x1E}, {0x1E}, "DEATH_GUNNER", "Death Gunner", nullptr},
{EnemyType::DEL_LILY, EP2, 0x53, 0x53, {0x25}, {0x25}, {0x25}, "DEL_LILY", "Del Lily", nullptr},
{EnemyType::DEL_RAPPY_CRATER, EP4, 0x69, 0x57, {0x06}, {0x06}, {0x06}, "DEL_RAPPY_CRATER", "Del Rappy (crater)", nullptr},
{EnemyType::DEL_RAPPY_DESERT, EP4, 0x69, 0x58, {0x18}, {0x18}, {0x18}, "DEL_RAPPY_DESERT", "Del Rappy (desert)", nullptr},
{EnemyType::DELBITER, EP2, 0x48, 0x48, {0x0D}, {0x0D}, {0x0D}, "DELBITER", "Delbiter", nullptr},
{EnemyType::DELDEPTH, EP2, 0x47, 0x47, {0x30}, {0x30}, {0x30}, "DELDEPTH", "Deldepth", nullptr},
{EnemyType::DELSABER, EP1 | EP2, 0x1E, 0x1E, {0x52}, {0x57, 0x58, 0x59}, {0x51}, "DELSABER", "Delsaber", nullptr},
{EnemyType::DIMENIAN, EP1 | EP2, 0x29, 0x29, {0x53}, {0x5A}, {0x52}, "DIMENIAN", "Dimenian", "Arlan"},
{EnemyType::DOLMDARL, EP2, 0x41, 0x41, {0x50}, {0x55}, {0x4F}, "DOLMDARL", "Dolmdarl", nullptr},
{EnemyType::DOLMOLM, EP2, 0x40, 0x40, {0x4F}, {0x54}, {0x4E}, "DOLMOLM", "Dolmolm", nullptr},
{EnemyType::DORPHON, EP4, 0x63, 0x50, {0x0F}, {0x0F}, {0x0F}, "DORPHON", "Dorphon", nullptr},
{EnemyType::DORPHON_ECLAIR, EP4 | RARE, 0x64, 0x51, {0x10}, {0x10}, {0x10}, "DORPHON_ECLAIR", "Dorphon Eclair", nullptr},
{EnemyType::DRAGON, EP1 | BOSS, 0x2C, 0x2C, {0x12}, {0x12, 0x14, 0x15, 0x16, 0x17}, {0x12}, "DRAGON", "Dragon", "Sil Dragon"},
{EnemyType::DUBCHIC, EP1 | EP2, 0x18, 0x18, {0x1B}, {0x1B}, {0x1B}, "DUBCHIC", "Dubchic", "Dubchich"},
{EnemyType::DUBWITCH, EP1 | EP2, NONE, NONE, {}, {}, {}, "DUBWITCH", "Dubwitch", "Duvuik"},
{EnemyType::EGG_RAPPY, EP2, 0x51, 0x51, {0x19}, {0x19}, {0x19}, "EGG_RAPPY", "Egg Rappy", nullptr},
{EnemyType::EPSIGARD, EP2, NONE, NONE, {0x24}, {0x24}, {0x24}, "EPSIGARD", "Episgard", nullptr},
{EnemyType::EPSILON, EP2, 0x54, 0x54, {0x23}, {0x23}, {0x23}, "EPSILON", "Epsilon", nullptr},
{EnemyType::EVIL_SHARK, EP1, 0x10, 0x10, {0x4F}, {0x54}, {0x4E}, "EVIL_SHARK", "Evil Shark", "Vulmer"},
{EnemyType::GAEL_OR_GIEL, EP2, NONE, NONE, {0x2E}, {0x2E}, {0x2E}, "GAEL_OR_GIEL", "Gael/Giel", nullptr},
{EnemyType::GAL_GRYPHON, EP2 | BOSS, 0x4D, 0x4D, {0x1E}, {0x1E, 0x1F, 0x20, 0x21, 0x22}, {0x1E}, "GAL_GRYPHON", "Gal Gryphon", nullptr},
{EnemyType::GARANZ, EP1 | EP2, 0x19, 0x19, {0x1D}, {0x1D}, {0x1D}, "GARANZ", "Garanz", "Baranz"},
{EnemyType::GEE, EP2, 0x36, 0x36, {0x07}, {0x07}, {0x07}, "GEE", "Gee", nullptr},
{EnemyType::GI_GUE, EP2, 0x37, 0x37, {0x1A}, {0x1A}, {0x1A}, "GI_GUE", "Gi Gue", nullptr},
{EnemyType::GIBBLES, EP2, 0x3D, 0x3D, {0x3D}, {0x3D, 0x3E, 0x3F}, {0x3D}, "GIBBLES", "Gibbles", nullptr},
{EnemyType::GIGOBOOMA, EP1, 0x0B, 0x0B, {0x4D}, {0x50}, {0x4C}, "GIGOBOOMA", "Gigobooma", "Tollaw"},
{EnemyType::GILLCHIC, EP1 | EP2, 0x32, 0x32, {0x1C}, {0x1C}, {0x1C}, "GILLCHIC", "Gillchic", "Gillchich"},
{EnemyType::GIRTABLULU, EP4, 0x5D, 0x48, {0x1F}, {0x1F}, {0x1F}, "GIRTABLULU", "Girtablulu", nullptr},
{EnemyType::GOBOOMA, EP1, 0x0A, 0x0A, {0x4C}, {0x4F}, {0x4B}, "GOBOOMA", "Gobooma", "Barble"},
{EnemyType::GOL_DRAGON, EP2 | BOSS, 0x4C, 0x4C, {0x12}, {0x12, 0x14, 0x15, 0x16, 0x17}, {0x12}, "GOL_DRAGON", "Gol Dragon", nullptr},
{EnemyType::GORAN, EP4, 0x65, 0x52, {0x11}, {0x11, 0x14}, {0x11}, "GORAN", "Goran", nullptr},
{EnemyType::GORAN_DETONATOR, EP4, 0x66, 0x53, {0x13}, {0x13, 0x16}, {0x13}, "GORAN_DETONATOR", "Goran Detonator", nullptr},
{EnemyType::GRASS_ASSASSIN, EP1 | EP2, 0x0C, 0x0C, {0x4E}, {0x51, 0x52, 0x53}, {0x4D}, "GRASS_ASSASSIN", "Grass Assassin", "Crimson Assassin"},
{EnemyType::GUIL_SHARK, EP1, 0x12, 0x12, {0x51}, {0x56}, {0x50}, "GUIL_SHARK", "Guil Shark", "Melqueek"},
{EnemyType::HALLO_RAPPY, EP2, 0x50, 0x50, {0x19}, {0x19}, {0x19}, "HALLO_RAPPY", "Hallo Rappy", nullptr},
{EnemyType::HIDOOM, EP1 | EP2, 0x17, 0x17, {0x32}, {0x32}, {0x32}, "HIDOOM", "Hidoom", nullptr},
{EnemyType::HILDEBEAR, EP1 | EP2, 0x01, 0x01, {0x49}, {0x48, 0x49, 0x4A}, {0x48}, "HILDEBEAR", "Hildebear", "Hildelt"},
{EnemyType::HILDEBLUE, EP1 | EP2 | RARE, 0x02, 0x02, {0x4A}, {0x4B, 0x4C, 0x4D}, {0x49}, "HILDEBLUE", "Hildeblue", "Hildetorr"},
{EnemyType::ILL_GILL, EP2, 0x52, 0x52, {0x26}, {0x26, 0x27, 0x28, 0x29}, {0x26}, "ILL_GILL", "Ill Gill", nullptr},
{EnemyType::KONDRIEU, EP4 | RARE | BOSS, 0x6C, 0x5B, {0x28, 0x2A}, {0x28, 0x2A}, {0x28, 0x2A}, "KONDRIEU", "Kondrieu", nullptr},
{EnemyType::KONDRIEU_SPINNER, EP4 | RARE | BOSS, 0x6C, 0x5B, {0x29, 0x2B}, {0x29, 0x2B}, {0x29, 0x2B}, "KONDRIEU_SPINNER", "Kondrieu (spinner)", nullptr},
{EnemyType::LA_DIMENIAN, EP1 | EP2, 0x2A, 0x2A, {0x54}, {0x5B}, {0x53}, "LA_DIMENIAN", "La Dimenian", "Merlan"},
{EnemyType::LOVE_RAPPY, EP2, 0x33, 0x33, {0x19}, {0x19}, {0x19}, "LOVE_RAPPY", "Love Rappy", nullptr},
{EnemyType::MERICARAND, EP2, 0x38, 0x38, {0x3A}, {0x3A}, {0x3A}, "MERICARAND", "Mericarand", nullptr},
{EnemyType::MERICAROL, EP2, 0x38, 0x38, {0x3A}, {0x3A}, {0x3A}, "MERICAROL", "Mericarol", nullptr},
{EnemyType::MERICUS, EP2 | RARE, 0x3A, 0x3A, {0x46}, {0x46}, {0x46}, "MERICUS", "Mericus", nullptr},
{EnemyType::MERIKLE, EP2 | RARE, 0x39, 0x39, {0x45}, {0x45}, {0x45}, "MERIKLE", "Merikle", nullptr},
{EnemyType::MERILLIA, EP2, 0x34, 0x34, {0x4B}, {0x4E}, {0x4A}, "MERILLIA", "Merillia", nullptr},
{EnemyType::MERILTAS, EP2, 0x35, 0x35, {0x4C}, {0x4F}, {0x4B}, "MERILTAS", "Meriltas", nullptr},
{EnemyType::MERISSA_A, EP4, 0x5B, 0x46, {0x19}, {0x19}, {0x19}, "MERISSA_A", "Merissa A", nullptr},
{EnemyType::MERISSA_AA, EP4 | RARE, 0x5C, 0x47, {0x1A}, {0x1A}, {0x1A}, "MERISSA_AA", "Merissa AA", nullptr},
{EnemyType::MIGIUM, EP1 | EP2, 0x16, 0x16, {0x33}, {0x33}, {0x33}, "MIGIUM", "Migium", nullptr},
{EnemyType::MONEST, EP1 | EP2, 0x04, 0x04, {0x01}, {0x01}, {0x01}, "MONEST", "Monest", "Mothvist"},
{EnemyType::MORFOS, EP2, 0x42, 0x42, {0x40}, {0x40, 0x50}, {0x40}, "MORFOS", "Morfos", nullptr},
{EnemyType::MOTHMANT, EP1 | EP2, 0x03, 0x03, {0x00}, {0x00}, {0x00}, "MOTHMANT", "Mothmant", "Mothvert"},
{EnemyType::NANO_DRAGON, EP1, 0x0F, 0x0F, {0x1A}, {0x1A}, {0x1A}, "NANO_DRAGON", "Nano Dragon", nullptr},
{EnemyType::NAR_LILY, EP1 | EP2 | RARE, 0x0E, 0x0E, {0x05}, {0x05}, {0x05}, "NAR_LILY", "Nar Lily", "Mil Lily"},
{EnemyType::OLGA_FLOW_1, EP2 | BOSS, NONE, NONE, {0x2B}, {0x2B}, {0x2B}, "OLGA_FLOW_1", "Olga Flow (phase 1)", nullptr},
{EnemyType::OLGA_FLOW_2, EP2 | BOSS, 0x4E, 0x4E, {0x2C}, {0x2C}, {0x2C}, "OLGA_FLOW_2", "Olga Flow (phase 2)", nullptr},
{EnemyType::PAL_SHARK, EP1, 0x11, 0x11, {0x50}, {0x55}, {0x4F}, "PAL_SHARK", "Pal Shark", "Govulmer"},
{EnemyType::PAN_ARMS, EP1 | EP2, 0x15, 0x15, {0x31}, {0x31}, {0x31}, "PAN_ARMS", "Pan Arms", nullptr},
{EnemyType::PAZUZU_CRATER, EP4 | RARE, 0x5F, 0x4B, {0x08}, {0x08}, {0x08}, "PAZUZU_CRATER", "Pazuzu (crater)", nullptr},
{EnemyType::PAZUZU_DESERT, EP4 | RARE, 0x5F, 0x4C, {0x1C}, {0x1C}, {0x1C}, "PAZUZU_DESERT", "Pazuzu (desert)", nullptr},
{EnemyType::PIG_RAY, EP2, 0x4A, NONE, {0x08}, {0x08}, {0x08}, "PIG_RAY", "Pig Ray", nullptr},
{EnemyType::POFUILLY_SLIME, EP1, 0x13, 0x13, {0x30}, {0x30}, {0x30}, "POFUILLY_SLIME", "Pofuilly Slime", nullptr},
{EnemyType::POUILLY_SLIME, EP1 | RARE, 0x14, 0x14, {0x34}, {0x34}, {0x34}, "POUILLY_SLIME", "Pouilly Slime", nullptr},
{EnemyType::POISON_LILY, EP1 | EP2, 0x0D, 0x0D, {0x04}, {0x04}, {0x04}, "POISON_LILY", "Poison Lily", "Ob Lily"},
{EnemyType::PYRO_GORAN, EP4, 0x67, 0x54, {0x12}, {0x12, 0x15}, {0x12}, "PYRO_GORAN", "Pyro Goran", nullptr},
{EnemyType::RAG_RAPPY, EP1 | EP2, 0x05, 0x05, {0x18}, {0x18}, {0x18}, "RAG_RAPPY", "Rag Rappy", "El Rappy"},
{EnemyType::RECOBOX, EP2, 0x43, 0x43, {0x41}, {0x41}, {0x41}, "RECOBOX", "Recobox", nullptr},
{EnemyType::RECON, EP2, 0x44, 0x44, {0x42}, {0x42}, {0x42}, "RECON", "Recon", nullptr},
{EnemyType::SAINT_MILION, EP4 | BOSS, 0x6A, 0x59, {0x20, 0x22}, {0x20, 0x22}, {0x20, 0x22}, "SAINT_MILION", "Saint-Milion", nullptr},
{EnemyType::SAINT_MILION_SPINNER, EP4 | BOSS, 0x6A, 0x59, {0x21, 0x23}, {0x21, 0x23}, {0x21, 0x23}, "SAINT_MILION_SPINNER", "Saint-Milion (spinner)", nullptr},
{EnemyType::SAINT_RAPPY, EP2, 0x4F, 0x4F, {0x19}, {0x19}, {0x19}, "SAINT_RAPPY", "Saint Rappy", nullptr},
{EnemyType::SAND_RAPPY_CRATER, EP4, 0x68, 0x55, {0x05}, {0x05}, {0x05}, "SAND_RAPPY_CRATER", "Sand Rappy (crater)", nullptr},
{EnemyType::SAND_RAPPY_DESERT, EP4, 0x68, 0x56, {0x17}, {0x17}, {0x17}, "SAND_RAPPY_DESERT", "Sand Rappy (desert)", nullptr},
{EnemyType::SATELLITE_LIZARD_CRATER, EP4, 0x5A, 0x44, {0x0D}, {0x0D}, {0x0D}, "SATELLITE_LIZARD_CRATER", "Satellite Lizard (crater)", nullptr},
{EnemyType::SATELLITE_LIZARD_DESERT, EP4, 0x5A, 0x45, {0x1D}, {0x1D}, {0x1D}, "SATELLITE_LIZARD_DESERT", "Satellite Lizard (desert)", nullptr},
{EnemyType::SAVAGE_WOLF, EP1 | EP2, 0x07, 0x07, {0x02}, {0x02}, {0x02}, "SAVAGE_WOLF", "Savage Wolf", "Gulgus"},
{EnemyType::SHAMBERTIN, EP4 | BOSS, 0x6B, 0x5A, {0x24, 0x26}, {0x24, 0x26}, {0x24, 0x26}, "SHAMBERTIN", "Shambertin", nullptr},
{EnemyType::SHAMBERTIN_SPINNER, EP4 | BOSS, 0x6B, 0x5A, {0x25, 0x27}, {0x25, 0x27}, {0x25, 0x27}, "SHAMBERTIN_SPINNER", "Shambertin (spinner)", nullptr},
{EnemyType::SINOW_BEAT, EP1, 0x1A, 0x1A, {0x06}, {0x06}, {0x06}, "SINOW_BEAT", "Sinow Beat", "Sinow Blue"},
{EnemyType::SINOW_BERILL, EP2, 0x3E, 0x3E, {0x06}, {0x06}, {0x06}, "SINOW_BERILL", "Sinow Berill", nullptr},
{EnemyType::SINOW_GOLD, EP1, 0x1B, 0x1B, {0x13}, {0x47}, {0x13}, "SINOW_GOLD", "Sinow Gold", "Sinow Red"},
{EnemyType::SINOW_SPIGELL, EP2, 0x3F, 0x3F, {0x13}, {0x47}, {0x13}, "SINOW_SPIGELL", "Sinow Spigell", nullptr},
{EnemyType::SINOW_ZELE, EP2, 0x46, 0x46, {0x44}, {0x44}, {0x44}, "SINOW_ZELE", "Sinow Zele", nullptr},
{EnemyType::SINOW_ZOA, EP2, 0x45, 0x45, {0x43}, {0x43}, {0x43}, "SINOW_ZOA", "Sinow Zoa", nullptr},
{EnemyType::SO_DIMENIAN, EP1 | EP2, 0x2B, 0x2B, {0x55}, {0x5C}, {0x54}, "SO_DIMENIAN", "So Dimenian", "Del-D"},
{EnemyType::UL_GIBBON, EP2, 0x3B, 0x3B, {0x3B}, {0x3B}, {0x3B}, "UL_GIBBON", "Ul Gibbon", nullptr},
{EnemyType::UL_RAY, EP2, 0x4B, NONE, {0x09}, {0x09}, {0x09}, "UL_RAY", "Ul Ray", nullptr},
{EnemyType::VOL_OPT_1, EP1 | BOSS, NONE, NONE, {0x21}, {0x21}, {0x21}, "VOL_OPT_1", "Vol Opt (phase 1)", "Vol Opt ver.2 (phase 1)"},
{EnemyType::VOL_OPT_2, EP1 | BOSS, 0x2E, 0x2E, {0x25}, {0x25}, {0x25}, "VOL_OPT_2", "Vol Opt (phase 2)", "Vol Opt ver.2 (phase 2)"},
{EnemyType::VOL_OPT_AMP, EP1 | BOSS, NONE, NONE, {0x24}, {0x24}, {0x24}, "VOL_OPT_AMP", "Vol Opt (amp)", "Vol Opt ver.2 (amp)"},
{EnemyType::VOL_OPT_CORE, EP1 | BOSS, NONE, NONE, {0x26}, {0x26}, {0x26}, "VOL_OPT_CORE", "Vol Opt (core)", "Vol Opt ver.2 (core)"},
{EnemyType::VOL_OPT_MONITOR, EP1 | BOSS, NONE, NONE, {0x23}, {0x23}, {0x23}, "VOL_OPT_MONITOR", "Vol Opt (monitor)", "Vol Opt ver.2 (monitor)"},
{EnemyType::VOL_OPT_PILLAR, EP1 | BOSS, NONE, NONE, {0x22}, {0x22}, {0x22}, "VOL_OPT_PILLAR", "Vol Opt (pillar)", "Vol Opt ver.2 (pillar)"},
{EnemyType::YOWIE_CRATER, EP4, 0x59, 0x42, {0x0E}, {0x0E}, {0x0E}, "YOWIE_CRATER", "Yowie (crater)", nullptr},
{EnemyType::YOWIE_DESERT, EP4, 0x59, 0x43, {0x1E}, {0x1E}, {0x1E}, "YOWIE_DESERT", "Yowie (desert)", nullptr},
{EnemyType::ZE_BOOTA, EP4, 0x61, 0x4E, {0x01}, {0x01, 0x02, 0x04}, {0x01}, "ZE_BOOTA", "Ze Boota", nullptr},
{EnemyType::ZOL_GIBBON, EP2, 0x3C, 0x3C, {0x3C}, {0x3C}, {0x3C}, "ZOL_GIBBON", "Zol Gibbon", nullptr},
{EnemyType::ZU_CRATER, EP4, 0x5E, 0x49, {0x07}, {0x07}, {0x07}, "ZU_CRATER", "Zu (crater)", nullptr},
{EnemyType::ZU_DESERT, EP4, 0x5E, 0x4A, {0x1B}, {0x1B}, {0x1B}, "ZU_DESERT", "Zu (desert)", nullptr},
// TYPE FLAGS RT OLDRT BP-STATS BP-ATTACK BP-RESIST BP-MOVEMENT ENUM NAME IN-GAME NAME ULTIMATE NAME
{EnemyType::UNKNOWN, 0, NONE, NONE, {}, {}, {}, {}, "UNKNOWN", "__UNKNOWN__", nullptr},
{EnemyType::NONE, 0, NONE, NONE, {}, {}, {}, {}, "NONE", "__NONE__", nullptr},
{EnemyType::NON_ENEMY_NPC, EP1 | EP2 | EP4, NONE, NONE, {}, {}, {}, {}, "NON_ENEMY_NPC", "__NPC__", nullptr},
{EnemyType::AL_RAPPY, EP1 | RARE, 0x06, 0x06, {0x19}, {0x19}, {0x19}, {0x19}, "AL_RAPPY", "Al Rappy", "Pal Rappy"},
{EnemyType::ASTARK, EP4, 0x58, 0x41, {0x09}, {0x0B, 0x0A, 0x0C}, {0x09}, {0x09}, "ASTARK", "Astark", nullptr},
{EnemyType::BA_BOOTA, EP4, 0x62, 0x4F, {0x03}, {0x03, 0x02, 0x04}, {0x03}, {0x03}, "BA_BOOTA", "Ba Boota", nullptr},
{EnemyType::BARBA_RAY_JOINT, EP2 | BOSS, 0x49, 0x49, {0x10}, {0x0F}, {0x10}, {}, "BARBA_RAY_JOINT", "Barba Ray (joint)", nullptr},
{EnemyType::BARBA_RAY, EP2 | BOSS, 0x49, 0x49, {0x0F}, {0x0F}, {0x0F}, {0x0F}, "BARBA_RAY", "Barba Ray", nullptr},
{EnemyType::BARBAROUS_WOLF, EP1 | EP2, 0x08, 0x08, {0x03}, {0x03}, {0x03}, {0x03}, "BARBAROUS_WOLF", "Barbarous Wolf", "Gulgus-gue"},
{EnemyType::BEE_L, EP1 | EP2, NONE, NONE, {0x0C}, {0x0C}, {0x0C}, {0x0C}, "BEE_L", "Bee L", "Gee L"},
{EnemyType::BEE_R, EP1 | EP2, NONE, NONE, {0x0B}, {0x0B}, {0x0B}, {0x0B}, "BEE_R", "Bee R", "Gee R"},
{EnemyType::BOOMA, EP1, 0x09, 0x09, {0x4B}, {0x4E}, {0x4A}, {0x4A}, "BOOMA", "Booma", "Bartle"},
{EnemyType::BOOTA, EP4, 0x60, 0x4D, {0x00}, {0x00, 0x02, 0x04}, {0x00}, {0x00}, "BOOTA", "Boota", nullptr},
{EnemyType::BULCLAW, EP1, 0x28, 0x28, {0x1F}, {0x1F}, {0x1F}, {0x1F, 0x20}, "BULCLAW", "Bulclaw", nullptr},
{EnemyType::BULK, EP1, 0x27, 0x27, {0x1F}, {0x1F}, {0x1F}, {0x1F, 0x20}, "BULK", "Bulk", nullptr},
{EnemyType::CANADINE_GROUP, EP1, 0x1C, 0x1C, {0x08}, {0x08}, {0x08}, {0x08}, "CANADINE_GROUP", "Canadine (group)", "Canabin (group)"},
{EnemyType::CANADINE, EP1, 0x1C, 0x1C, {0x07}, {0x07}, {0x07}, {0x07}, "CANADINE", "Canadine", "Canabin"},
{EnemyType::CANANE, EP1, 0x1D, 0x1D, {0x09}, {0x09}, {0x09}, {0x09}, "CANANE", "Canane", "Canune"},
{EnemyType::CHAOS_BRINGER, EP1, 0x24, 0x24, {0x0D}, {0x0D}, {0x0D}, {0x0A, 0x0D}, "CHAOS_BRINGER", "Chaos Bringer", "Dark Bringer"},
{EnemyType::CHAOS_SORCERER, EP1 | EP2, 0x1F, 0x1F, {0x0A}, {0x0A}, {0x0A}, {}, "CHAOS_SORCERER", "Chaos Sorceror", "Gran Sorceror"},
{EnemyType::CLAW, EP1, 0x26, 0x26, {0x20}, {0x20}, {0x20}, {}, "CLAW", "Claw", nullptr},
{EnemyType::DARK_BELRA, EP1 | EP2, 0x25, 0x25, {0x0E}, {0x0E, 0x13}, {0x0E}, {0x0E}, "DARK_BELRA", "Dark Belra", "Indi Belra"},
{EnemyType::DARK_FALZ_1, EP1 | BOSS, NONE, NONE, {0x36}, {0x36}, {0x36}, {0x36, 0x39}, "DARK_FALZ_1", "Dark Falz (phase 1)", nullptr},
{EnemyType::DARK_FALZ_2, EP1 | BOSS, 0x2F, 0x2F, {0x37}, {0x37}, {0x37}, {0x37}, "DARK_FALZ_2", "Dark Falz (phase 2)", nullptr},
{EnemyType::DARK_FALZ_3, EP1 | BOSS, 0x2F, 0x2F, {0x38}, {0x38}, {0x38}, {0x38}, "DARK_FALZ_3", "Dark Falz (phase 3)", nullptr},
{EnemyType::DARK_GUNNER_CONTROL, EP1, NONE, NONE, {}, {}, {}, {}, "DARK_GUNNER_CONTROL", "Dark Gunner (control)", nullptr},
{EnemyType::DARK_GUNNER, EP1, 0x22, 0x22, {0x1E}, {0x1E}, {0x1E}, {0x1E}, "DARK_GUNNER", "Dark Gunner", nullptr},
{EnemyType::DARVANT, EP1, NONE, NONE, {0x35}, {0x35}, {0x35}, {0x35, 0x39}, "DARVANT", "Darvant", nullptr},
{EnemyType::DE_ROL_LE_BODY, EP1 | BOSS, NONE, NONE, {0x10}, {0x0F}, {0x10}, {0x0F}, "DE_ROL_LE_BODY", "De Rol Le (body)", "Dal Ra Lie (body)"},
{EnemyType::DE_ROL_LE_MINE, EP1 | BOSS, NONE, NONE, {0x11}, {0x0F}, {0x11}, {0x0F}, "DE_ROL_LE_MINE", "De Rol Le (mine)", "Dal Ra Lie (mine)"},
{EnemyType::DE_ROL_LE, EP1 | BOSS, 0x2D, 0x2D, {0x0F}, {0x0F}, {0x0F}, {0x0F}, "DE_ROL_LE", "De Rol Le", "Dal Ra Lie"},
{EnemyType::DEATH_GUNNER, EP1, 0x23, 0x23, {0x1E}, {0x1E}, {0x1E}, {0x1E}, "DEATH_GUNNER", "Death Gunner", nullptr},
{EnemyType::DEL_LILY, EP2, 0x53, 0x53, {0x25}, {0x25}, {0x25}, {0x25}, "DEL_LILY", "Del Lily", nullptr},
{EnemyType::DEL_RAPPY_CRATER, EP4, 0x69, 0x57, {0x06}, {0x06}, {0x06}, {0x06}, "DEL_RAPPY_CRATER", "Del Rappy (crater)", nullptr},
{EnemyType::DEL_RAPPY_DESERT, EP4, 0x69, 0x58, {0x18}, {0x18}, {0x18}, {0x18}, "DEL_RAPPY_DESERT", "Del Rappy (desert)", nullptr},
{EnemyType::DELBITER, EP2, 0x48, 0x48, {0x0D}, {0x0D}, {0x0D}, {0x0D}, "DELBITER", "Delbiter", nullptr},
{EnemyType::DELDEPTH, EP2, 0x47, 0x47, {0x30}, {0x30}, {0x30}, {0x30}, "DELDEPTH", "Deldepth", nullptr},
{EnemyType::DELSABER, EP1 | EP2, 0x1E, 0x1E, {0x52}, {0x57, 0x58, 0x59}, {0x51}, {0x51}, "DELSABER", "Delsaber", nullptr},
{EnemyType::DIMENIAN, EP1 | EP2, 0x29, 0x29, {0x53}, {0x5A}, {0x52}, {0x52}, "DIMENIAN", "Dimenian", "Arlan"},
{EnemyType::DOLMDARL, EP2, 0x41, 0x41, {0x50}, {0x55}, {0x4F}, {0x4F}, "DOLMDARL", "Dolmdarl", nullptr},
{EnemyType::DOLMOLM, EP2, 0x40, 0x40, {0x4F}, {0x54}, {0x4E}, {0x4E}, "DOLMOLM", "Dolmolm", nullptr},
{EnemyType::DORPHON_ECLAIR, EP4 | RARE, 0x64, 0x51, {0x10}, {0x10}, {0x10}, {0x10}, "DORPHON_ECLAIR", "Dorphon Eclair", nullptr},
{EnemyType::DORPHON, EP4, 0x63, 0x50, {0x0F}, {0x0F}, {0x0F}, {0x0F}, "DORPHON", "Dorphon", nullptr},
{EnemyType::DRAGON, EP1 | BOSS, 0x2C, 0x2C, {0x12}, {0x12, 0x14, 0x15, 0x16, 0x17}, {0x12}, {0x11}, "DRAGON", "Dragon", "Sil Dragon"},
{EnemyType::DUBCHIC, EP1 | EP2, 0x18, 0x18, {0x1B}, {0x1B}, {0x1B}, {0x1B}, "DUBCHIC", "Dubchic", "Dubchich"},
{EnemyType::DUBWITCH, EP1 | EP2, NONE, NONE, {}, {}, {}, {}, "DUBWITCH", "Dubwitch", "Duvuik"},
{EnemyType::EGG_RAPPY, EP2, 0x51, 0x51, {0x19}, {0x19}, {0x19}, {0x19}, "EGG_RAPPY", "Egg Rappy", nullptr},
{EnemyType::EPSIGARD, EP2, NONE, NONE, {0x24}, {0x24}, {0x24}, {0x24}, "EPSIGARD", "Episgard", nullptr},
{EnemyType::EPSILON, EP2, 0x54, 0x54, {0x23}, {0x23}, {0x23}, {0x23}, "EPSILON", "Epsilon", nullptr},
{EnemyType::EVIL_SHARK, EP1, 0x10, 0x10, {0x4F}, {0x54}, {0x4E}, {0x4E}, "EVIL_SHARK", "Evil Shark", "Vulmer"},
{EnemyType::GAEL_OR_GIEL, EP2, NONE, NONE, {0x2E}, {0x2E}, {0x2E}, {}, "GAEL_OR_GIEL", "Gael/Giel", nullptr},
{EnemyType::GAL_GRYPHON, EP2 | BOSS, 0x4D, 0x4D, {0x1E}, {0x1E, 0x1F, 0x20, 0x21, 0x22}, {0x1E}, {0x1E, 0x1F, 0x20}, "GAL_GRYPHON", "Gal Gryphon", nullptr},
{EnemyType::GARANZ, EP1 | EP2, 0x19, 0x19, {0x1D}, {0x1D}, {0x1D}, {0x1D}, "GARANZ", "Garanz", "Baranz"},
{EnemyType::GEE, EP2, 0x36, 0x36, {0x07}, {0x07}, {0x07}, {0x07}, "GEE", "Gee", nullptr},
{EnemyType::GI_GUE, EP2, 0x37, 0x37, {0x1A}, {0x1A}, {0x1A}, {0x1A}, "GI_GUE", "Gi Gue", nullptr},
{EnemyType::GIBBLES, EP2, 0x3D, 0x3D, {0x3D}, {0x3D, 0x3E, 0x3F}, {0x3D}, {0x3D}, "GIBBLES", "Gibbles", nullptr},
{EnemyType::GIGOBOOMA, EP1, 0x0B, 0x0B, {0x4D}, {0x50}, {0x4C}, {0x4C}, "GIGOBOOMA", "Gigobooma", "Tollaw"},
{EnemyType::GILLCHIC, EP1 | EP2, 0x32, 0x32, {0x1C}, {0x1C}, {0x1C}, {0x1C}, "GILLCHIC", "Gillchic", "Gillchich"},
{EnemyType::GIRTABLULU, EP4, 0x5D, 0x48, {0x1F}, {0x1F}, {0x1F}, {0x1F}, "GIRTABLULU", "Girtablulu", nullptr},
{EnemyType::GOBOOMA, EP1, 0x0A, 0x0A, {0x4C}, {0x4F}, {0x4B}, {0x4B}, "GOBOOMA", "Gobooma", "Barble"},
{EnemyType::GOL_DRAGON, EP2 | BOSS, 0x4C, 0x4C, {0x12}, {0x12, 0x14, 0x15, 0x16, 0x17}, {0x12}, {0x11, 0x12, 0x13}, "GOL_DRAGON", "Gol Dragon", nullptr},
{EnemyType::GORAN_DETONATOR, EP4, 0x66, 0x53, {0x13}, {0x13, 0x16}, {0x13}, {0x13}, "GORAN_DETONATOR", "Goran Detonator", nullptr},
{EnemyType::GORAN, EP4, 0x65, 0x52, {0x11}, {0x11, 0x14}, {0x11}, {0x11}, "GORAN", "Goran", nullptr},
{EnemyType::GRASS_ASSASSIN, EP1 | EP2, 0x0C, 0x0C, {0x4E}, {0x51, 0x52, 0x53}, {0x4D}, {0x4D}, "GRASS_ASSASSIN", "Grass Assassin", "Crimson Assassin"},
{EnemyType::GUIL_SHARK, EP1, 0x12, 0x12, {0x51}, {0x56}, {0x50}, {0x50}, "GUIL_SHARK", "Guil Shark", "Melqueek"},
{EnemyType::HALLO_RAPPY, EP2, 0x50, 0x50, {0x19}, {0x19}, {0x19}, {0x19}, "HALLO_RAPPY", "Hallo Rappy", nullptr},
{EnemyType::HIDOOM, EP1 | EP2, 0x17, 0x17, {0x32}, {0x32}, {0x32}, {0x32}, "HIDOOM", "Hidoom", nullptr},
{EnemyType::HILDEBEAR, EP1 | EP2, 0x01, 0x01, {0x49}, {0x48, 0x49, 0x4A}, {0x48}, {0x48}, "HILDEBEAR", "Hildebear", "Hildelt"},
{EnemyType::HILDEBLUE, EP1 | EP2 | RARE, 0x02, 0x02, {0x4A}, {0x4B, 0x4C, 0x4D}, {0x49}, {0x49}, "HILDEBLUE", "Hildeblue", "Hildetorr"},
{EnemyType::ILL_GILL, EP2, 0x52, 0x52, {0x26}, {0x26, 0x27, 0x28, 0x29}, {0x26}, {0x26}, "ILL_GILL", "Ill Gill", nullptr},
{EnemyType::KONDRIEU_SPINNER, EP4 | RARE | BOSS, 0x6C, 0x5B, {0x29, 0x2B}, {0x29, 0x2B}, {0x29, 0x2B}, {0x29, 0x2B}, "KONDRIEU_SPINNER", "Kondrieu (spinner)", nullptr},
{EnemyType::KONDRIEU, EP4 | RARE | BOSS, 0x6C, 0x5B, {0x28, 0x2A}, {0x28, 0x2A}, {0x28, 0x2A}, {0x28, 0x2A}, "KONDRIEU", "Kondrieu", nullptr},
{EnemyType::LA_DIMENIAN, EP1 | EP2, 0x2A, 0x2A, {0x54}, {0x5B}, {0x53}, {0x53}, "LA_DIMENIAN", "La Dimenian", "Merlan"},
{EnemyType::LOVE_RAPPY, EP2, 0x33, 0x33, {0x19}, {0x19}, {0x19}, {0x19}, "LOVE_RAPPY", "Love Rappy", nullptr},
{EnemyType::MERICARAND, EP2, 0x38, 0x38, {0x3A}, {0x3A}, {0x3A}, {0x3A}, "MERICARAND", "Mericarand", nullptr},
{EnemyType::MERICAROL, EP2, 0x38, 0x38, {0x3A}, {0x3A}, {0x3A}, {0x3A}, "MERICAROL", "Mericarol", nullptr},
{EnemyType::MERICUS, EP2 | RARE, 0x3A, 0x3A, {0x46}, {0x46}, {0x46}, {0x46}, "MERICUS", "Mericus", nullptr},
{EnemyType::MERIKLE, EP2 | RARE, 0x39, 0x39, {0x45}, {0x45}, {0x45}, {0x45}, "MERIKLE", "Merikle", nullptr},
{EnemyType::MERILLIA, EP2, 0x34, 0x34, {0x4B}, {0x4E}, {0x4A}, {0x4A}, "MERILLIA", "Merillia", nullptr},
{EnemyType::MERILTAS, EP2, 0x35, 0x35, {0x4C}, {0x4F}, {0x4B}, {0x4B}, "MERILTAS", "Meriltas", nullptr},
{EnemyType::MERISSA_A, EP4, 0x5B, 0x46, {0x19}, {0x19}, {0x19}, {0x19}, "MERISSA_A", "Merissa A", nullptr},
{EnemyType::MERISSA_AA, EP4 | RARE, 0x5C, 0x47, {0x1A}, {0x1A}, {0x1A}, {0x1A}, "MERISSA_AA", "Merissa AA", nullptr},
{EnemyType::MIGIUM, EP1 | EP2, 0x16, 0x16, {0x33}, {0x33}, {0x33}, {0x33}, "MIGIUM", "Migium", nullptr},
{EnemyType::MONEST, EP1 | EP2, 0x04, 0x04, {0x01}, {0x01}, {0x01}, {0x01}, "MONEST", "Monest", "Mothvist"},
{EnemyType::MORFOS, EP2, 0x42, 0x42, {0x40}, {0x40, 0x50}, {0x40}, {0x40}, "MORFOS", "Morfos", nullptr},
{EnemyType::MOTHMANT, EP1 | EP2, 0x03, 0x03, {0x00}, {0x00}, {0x00}, {0x00}, "MOTHMANT", "Mothmant", "Mothvert"},
{EnemyType::NANO_DRAGON, EP1, 0x0F, 0x0F, {0x1A}, {0x1A}, {0x1A}, {0x1A}, "NANO_DRAGON", "Nano Dragon", nullptr},
{EnemyType::NAR_LILY, EP1 | EP2 | RARE, 0x0E, 0x0E, {0x05}, {0x05}, {0x05}, {0x05}, "NAR_LILY", "Nar Lily", "Mil Lily"},
{EnemyType::OLGA_FLOW_1, EP2 | BOSS, NONE, NONE, {0x2B}, {0x2B}, {0x2B}, {0x2B, 0x2D, 0x2F}, "OLGA_FLOW_1", "Olga Flow (phase 1)", nullptr},
{EnemyType::OLGA_FLOW_2, EP2 | BOSS, 0x4E, 0x4E, {0x2C}, {0x2C}, {0x2C}, {0x2C, 0x2D, 0x3E, 0x2F}, "OLGA_FLOW_2", "Olga Flow (phase 2)", nullptr},
{EnemyType::PAL_SHARK, EP1, 0x11, 0x11, {0x50}, {0x55}, {0x4F}, {0x4F}, "PAL_SHARK", "Pal Shark", "Govulmer"},
{EnemyType::PAN_ARMS, EP1 | EP2, 0x15, 0x15, {0x31}, {0x31}, {0x31}, {0x31}, "PAN_ARMS", "Pan Arms", nullptr},
{EnemyType::PAZUZU_CRATER, EP4 | RARE, 0x5F, 0x4B, {0x08}, {0x08}, {0x08}, {0x08}, "PAZUZU_CRATER", "Pazuzu (crater)", nullptr},
{EnemyType::PAZUZU_DESERT, EP4 | RARE, 0x5F, 0x4C, {0x1C}, {0x1C}, {0x1C}, {0x1C}, "PAZUZU_DESERT", "Pazuzu (desert)", nullptr},
{EnemyType::PIG_RAY, EP2, 0x4A, NONE, {0x08}, {0x08}, {0x08}, {0x08}, "PIG_RAY", "Pig Ray", nullptr},
{EnemyType::POFUILLY_SLIME, EP1, 0x13, 0x13, {0x30}, {0x30}, {0x30}, {0x30}, "POFUILLY_SLIME", "Pofuilly Slime", nullptr},
{EnemyType::POISON_LILY, EP1 | EP2, 0x0D, 0x0D, {0x04}, {0x04}, {0x04}, {0x04}, "POISON_LILY", "Poison Lily", "Ob Lily"},
{EnemyType::POUILLY_SLIME, EP1 | RARE, 0x14, 0x14, {0x34}, {0x34}, {0x34}, {0x34}, "POUILLY_SLIME", "Pouilly Slime", nullptr},
{EnemyType::PYRO_GORAN, EP4, 0x67, 0x54, {0x12}, {0x12, 0x15}, {0x12}, {0x12}, "PYRO_GORAN", "Pyro Goran", nullptr},
{EnemyType::RAG_RAPPY, EP1 | EP2, 0x05, 0x05, {0x18}, {0x18}, {0x18}, {0x18}, "RAG_RAPPY", "Rag Rappy", "El Rappy"},
{EnemyType::RECOBOX, EP2, 0x43, 0x43, {0x41}, {0x41}, {0x41}, {0x41}, "RECOBOX", "Recobox", nullptr},
{EnemyType::RECON, EP2, 0x44, 0x44, {0x42}, {0x42}, {0x42}, {0x42}, "RECON", "Recon", nullptr},
{EnemyType::SAINT_MILION_SPINNER, EP4 | BOSS, 0x6A, 0x59, {0x21, 0x23}, {0x21, 0x23}, {0x21, 0x23}, {0x21, 0x23}, "SAINT_MILION_SPINNER", "Saint-Milion (spinner)", nullptr},
{EnemyType::SAINT_MILION, EP4 | BOSS, 0x6A, 0x59, {0x20, 0x22}, {0x20, 0x22}, {0x20, 0x22}, {0x20, 0x22}, "SAINT_MILION", "Saint-Milion", nullptr},
{EnemyType::SAINT_RAPPY, EP2, 0x4F, 0x4F, {0x19}, {0x19}, {0x19}, {0x19}, "SAINT_RAPPY", "Saint Rappy", nullptr},
{EnemyType::SAND_RAPPY_CRATER, EP4, 0x68, 0x55, {0x05}, {0x05}, {0x05}, {0x05}, "SAND_RAPPY_CRATER", "Sand Rappy (crater)", nullptr},
{EnemyType::SAND_RAPPY_DESERT, EP4, 0x68, 0x56, {0x17}, {0x17}, {0x17}, {0x17}, "SAND_RAPPY_DESERT", "Sand Rappy (desert)", nullptr},
{EnemyType::SATELLITE_LIZARD_CRATER, EP4, 0x5A, 0x44, {0x0D}, {0x0D}, {0x0D}, {0x0D}, "SATELLITE_LIZARD_CRATER", "Satellite Lizard (crater)", nullptr},
{EnemyType::SATELLITE_LIZARD_DESERT, EP4, 0x5A, 0x45, {0x1D}, {0x1D}, {0x1D}, {0x1D}, "SATELLITE_LIZARD_DESERT", "Satellite Lizard (desert)", nullptr},
{EnemyType::SAVAGE_WOLF, EP1 | EP2, 0x07, 0x07, {0x02}, {0x02}, {0x02}, {0x02}, "SAVAGE_WOLF", "Savage Wolf", "Gulgus"},
{EnemyType::SHAMBERTIN_SPINNER, EP4 | BOSS, 0x6B, 0x5A, {0x25, 0x27}, {0x25, 0x27}, {0x25, 0x27}, {0x25, 0x27}, "SHAMBERTIN_SPINNER", "Shambertin (spinner)", nullptr},
{EnemyType::SHAMBERTIN, EP4 | BOSS, 0x6B, 0x5A, {0x24, 0x26}, {0x24, 0x26}, {0x24, 0x26}, {0x24, 0x26}, "SHAMBERTIN", "Shambertin", nullptr},
{EnemyType::SINOW_BEAT, EP1, 0x1A, 0x1A, {0x06}, {0x06}, {0x06}, {0x06}, "SINOW_BEAT", "Sinow Beat", "Sinow Blue"},
{EnemyType::SINOW_BERILL, EP2, 0x3E, 0x3E, {0x06}, {0x06}, {0x06}, {0x06}, "SINOW_BERILL", "Sinow Berill", nullptr},
{EnemyType::SINOW_GOLD, EP1, 0x1B, 0x1B, {0x13}, {0x47}, {0x13}, {0x10}, "SINOW_GOLD", "Sinow Gold", "Sinow Red"},
{EnemyType::SINOW_SPIGELL, EP2, 0x3F, 0x3F, {0x13}, {0x47}, {0x13}, {0x10}, "SINOW_SPIGELL", "Sinow Spigell", nullptr},
{EnemyType::SINOW_ZELE, EP2, 0x46, 0x46, {0x44}, {0x44}, {0x44}, {0x44}, "SINOW_ZELE", "Sinow Zele", nullptr},
{EnemyType::SINOW_ZOA, EP2, 0x45, 0x45, {0x43}, {0x43}, {0x43}, {0x43}, "SINOW_ZOA", "Sinow Zoa", nullptr},
{EnemyType::SO_DIMENIAN, EP1 | EP2, 0x2B, 0x2B, {0x55}, {0x5C}, {0x54}, {0x54}, "SO_DIMENIAN", "So Dimenian", "Del-D"},
{EnemyType::UL_GIBBON, EP2, 0x3B, 0x3B, {0x3B}, {0x3B}, {0x3B}, {0x3B}, "UL_GIBBON", "Ul Gibbon", nullptr},
{EnemyType::UL_RAY, EP2, 0x4B, NONE, {0x09}, {0x09}, {0x09}, {0x09}, "UL_RAY", "Ul Ray", nullptr},
{EnemyType::VOL_OPT_1, EP1 | BOSS, NONE, NONE, {0x21}, {0x21}, {0x21}, {0x21, 0x22, 0x23}, "VOL_OPT_1", "Vol Opt (phase 1)", "Vol Opt ver.2 (phase 1)"},
{EnemyType::VOL_OPT_2, EP1 | BOSS, 0x2E, 0x2E, {0x25}, {0x25}, {0x25}, {0x25, 0x26, 0x28, 0x29, 0x2A}, "VOL_OPT_2", "Vol Opt (phase 2)", "Vol Opt ver.2 (phase 2)"},
{EnemyType::VOL_OPT_AMP, EP1 | BOSS, NONE, NONE, {0x24}, {0x24}, {0x24}, {}, "VOL_OPT_AMP", "Vol Opt (amp)", "Vol Opt ver.2 (amp)"},
{EnemyType::VOL_OPT_CORE, EP1 | BOSS, NONE, NONE, {0x26}, {0x26}, {0x26}, {}, "VOL_OPT_CORE", "Vol Opt (core)", "Vol Opt ver.2 (core)"},
{EnemyType::VOL_OPT_MONITOR, EP1 | BOSS, NONE, NONE, {0x23}, {0x23}, {0x23}, {}, "VOL_OPT_MONITOR", "Vol Opt (monitor)", "Vol Opt ver.2 (monitor)"},
{EnemyType::VOL_OPT_PILLAR, EP1 | BOSS, NONE, NONE, {0x22}, {0x22}, {0x22}, {}, "VOL_OPT_PILLAR", "Vol Opt (pillar)", "Vol Opt ver.2 (pillar)"},
{EnemyType::YOWIE_CRATER, EP4, 0x59, 0x42, {0x0E}, {0x0E}, {0x0E}, {0x0E}, "YOWIE_CRATER", "Yowie (crater)", nullptr},
{EnemyType::YOWIE_DESERT, EP4, 0x59, 0x43, {0x1E}, {0x1E}, {0x1E}, {0x1E}, "YOWIE_DESERT", "Yowie (desert)", nullptr},
{EnemyType::ZE_BOOTA, EP4, 0x61, 0x4E, {0x01}, {0x01, 0x02, 0x04}, {0x01}, {0x01}, "ZE_BOOTA", "Ze Boota", nullptr},
{EnemyType::ZOL_GIBBON, EP2, 0x3C, 0x3C, {0x3C}, {0x3C}, {0x3C}, {0x3C}, "ZOL_GIBBON", "Zol Gibbon", nullptr},
{EnemyType::ZU_CRATER, EP4, 0x5E, 0x49, {0x07}, {0x07}, {0x07}, {0x07}, "ZU_CRATER", "Zu (crater)", nullptr},
{EnemyType::ZU_DESERT, EP4, 0x5E, 0x4A, {0x1B}, {0x1B}, {0x1B}, {0x1B}, "ZU_DESERT", "Zu (desert)", nullptr},
// clang-format on
};
@@ -166,19 +165,19 @@ const char* phosg::name_for_enum<EnemyType>(EnemyType type) {
template <>
EnemyType phosg::enum_for_name<EnemyType>(const char* name) {
static unordered_map<string, EnemyType> index;
static std::unordered_map<std::string, EnemyType> index;
if (index.empty()) {
for (const auto& def : type_defs) {
if (!index.emplace(def.enum_name, def.type).second) {
throw logic_error(std::format("duplicate enemy enum name: {}", def.enum_name));
throw std::logic_error(std::format("duplicate enemy enum name: {}", def.enum_name));
}
}
}
return index.at(name);
}
const vector<EnemyType>& enemy_types_for_rare_table_index(Episode episode, uint8_t rt_index) {
static array<vector<vector<EnemyType>>, 5> data;
const std::vector<EnemyType>& enemy_types_for_rare_table_index(Episode episode, uint8_t rt_index) {
static std::array<std::vector<std::vector<EnemyType>>, 5> data;
auto& ret = data.at(static_cast<size_t>(episode));
if (ret.empty()) {
for (const auto& def : type_defs) {
@@ -195,32 +194,91 @@ const vector<EnemyType>& enemy_types_for_rare_table_index(Episode episode, uint8
}
try {
return ret.at(rt_index);
} catch (const out_of_range&) {
static const vector<EnemyType> empty_vec;
} catch (const std::out_of_range&) {
static const std::vector<EnemyType> empty_vec;
return empty_vec;
}
}
const vector<EnemyType>& enemy_types_for_battle_param_stats_index(Episode episode, uint8_t bp_index) {
static array<vector<vector<EnemyType>>, 5> data;
auto& ret = data.at(static_cast<size_t>(episode));
if (ret.empty()) {
struct BPIndexCacheEntry {
std::set<EnemyType> stats;
std::set<EnemyType> attack_data;
std::set<EnemyType> resist_data;
std::set<EnemyType> movement_data;
};
static const BPIndexCacheEntry& get_bp_index_cache_entry(Episode episode, uint8_t bp_index) {
static bool cache_populated = false;
static std::array<std::vector<BPIndexCacheEntry>, 5> data;
if (!cache_populated) {
cache_populated = true;
for (const auto& def : type_defs) {
if (def.valid_in_episode(episode) && !def.bp_stats_indexes.empty()) {
// Some enemies (e.g. Ep4 bosses) have multiple stats structures; we use the last one, since that's the only
// one used when giving EXP
size_t bp_index = def.bp_stats_indexes.back();
if (bp_index >= ret.size()) {
ret.resize(bp_index + 1);
for (const auto& episode : ALL_EPISODES_V4) {
if (!def.valid_in_episode(episode)) {
continue;
}
auto& ep_index = data[static_cast<size_t>(episode)];
for (const auto& bp_index : def.bp_stats_indexes) {
if (bp_index >= ep_index.size()) {
ep_index.resize(bp_index + 1);
}
ep_index[bp_index].stats.emplace(def.type);
}
for (const auto& bp_index : def.bp_attack_data_indexes) {
if (bp_index >= ep_index.size()) {
ep_index.resize(bp_index + 1);
}
ep_index[bp_index].attack_data.emplace(def.type);
}
for (const auto& bp_index : def.bp_resist_data_indexes) {
if (bp_index >= ep_index.size()) {
ep_index.resize(bp_index + 1);
}
ep_index[bp_index].resist_data.emplace(def.type);
}
for (const auto& bp_index : def.bp_movement_data_indexes) {
if (bp_index >= ep_index.size()) {
ep_index.resize(bp_index + 1);
}
ep_index[bp_index].movement_data.emplace(def.type);
}
ret[bp_index].emplace_back(def.type);
}
}
}
auto& ep_index = data.at(static_cast<size_t>(episode));
return ep_index.at(bp_index);
}
static const std::set<EnemyType> empty_vec;
const std::set<EnemyType>& enemy_types_for_battle_param_stats_index(Episode episode, uint8_t bp_index) {
try {
return ret.at(bp_index);
} catch (const out_of_range&) {
static const vector<EnemyType> empty_vec;
return get_bp_index_cache_entry(episode, bp_index).stats;
} catch (const std::out_of_range&) {
return empty_vec;
}
}
const std::set<EnemyType>& enemy_types_for_battle_param_attack_data_index(Episode episode, uint8_t bp_index) {
try {
return get_bp_index_cache_entry(episode, bp_index).attack_data;
} catch (const std::out_of_range&) {
return empty_vec;
}
}
const std::set<EnemyType>& enemy_types_for_battle_param_resist_data_index(Episode episode, uint8_t bp_index) {
try {
return get_bp_index_cache_entry(episode, bp_index).resist_data;
} catch (const std::out_of_range&) {
return empty_vec;
}
}
const std::set<EnemyType>& enemy_types_for_battle_param_movement_data_index(Episode episode, uint8_t bp_index) {
try {
return get_bp_index_cache_entry(episode, bp_index).movement_data;
} catch (const std::out_of_range&) {
return empty_vec;
}
}
+16 -11
View File
@@ -3,6 +3,7 @@
#include <inttypes.h>
#include <phosg/Tools.hh>
#include <set>
#include "StaticGameData.hh"
#include "Types.hh"
@@ -20,8 +21,8 @@ enum class EnemyType : uint8_t {
AL_RAPPY,
ASTARK,
BA_BOOTA,
BARBA_RAY,
BARBA_RAY_JOINT,
BARBA_RAY,
BARBAROUS_WOLF,
BEE_L,
BEE_R,
@@ -29,8 +30,8 @@ enum class EnemyType : uint8_t {
BOOTA,
BULCLAW,
BULK,
CANADINE,
CANADINE_GROUP,
CANADINE,
CANANE,
CHAOS_BRINGER,
CHAOS_SORCERER,
@@ -39,12 +40,12 @@ enum class EnemyType : uint8_t {
DARK_FALZ_1,
DARK_FALZ_2,
DARK_FALZ_3,
DARK_GUNNER,
DARK_GUNNER_CONTROL,
DARK_GUNNER,
DARVANT,
DE_ROL_LE,
DE_ROL_LE_BODY,
DE_ROL_LE_MINE,
DE_ROL_LE,
DEATH_GUNNER,
DEL_LILY,
DEL_RAPPY_CRATER,
@@ -55,8 +56,8 @@ enum class EnemyType : uint8_t {
DIMENIAN,
DOLMDARL,
DOLMOLM,
DORPHON,
DORPHON_ECLAIR,
DORPHON,
DRAGON,
DUBCHIC,
DUBWITCH, // Has no entry in battle params
@@ -75,8 +76,8 @@ enum class EnemyType : uint8_t {
GIRTABLULU,
GOBOOMA,
GOL_DRAGON,
GORAN,
GORAN_DETONATOR,
GORAN,
GRASS_ASSASSIN,
GUIL_SHARK,
HALLO_RAPPY,
@@ -84,8 +85,8 @@ enum class EnemyType : uint8_t {
HILDEBEAR,
HILDEBLUE,
ILL_GILL,
KONDRIEU,
KONDRIEU_SPINNER,
KONDRIEU,
LA_DIMENIAN,
LOVE_RAPPY,
MERICARAND,
@@ -110,22 +111,22 @@ enum class EnemyType : uint8_t {
PAZUZU_DESERT,
PIG_RAY,
POFUILLY_SLIME,
POUILLY_SLIME,
POISON_LILY,
POUILLY_SLIME,
PYRO_GORAN,
RAG_RAPPY,
RECOBOX,
RECON,
SAINT_MILION,
SAINT_MILION_SPINNER,
SAINT_MILION,
SAINT_RAPPY,
SAND_RAPPY_CRATER,
SAND_RAPPY_DESERT,
SATELLITE_LIZARD_CRATER,
SATELLITE_LIZARD_DESERT,
SAVAGE_WOLF,
SHAMBERTIN,
SHAMBERTIN_SPINNER,
SHAMBERTIN,
SINOW_BEAT,
SINOW_BERILL,
SINOW_GOLD,
@@ -165,6 +166,7 @@ struct EnemyTypeDefinition {
std::vector<uint8_t> bp_stats_indexes;
std::vector<uint8_t> bp_attack_data_indexes;
std::vector<uint8_t> bp_resist_data_indexes;
std::vector<uint8_t> bp_movement_data_indexes;
// Note: movement data isn't bound as strongly to the enemy types; some enemies use many entries and some use none at
// all, so we don't list them here. See notes/movement-data.txt for a listing of which enemies use which entries.
const char* enum_name;
@@ -200,4 +202,7 @@ template <>
EnemyType phosg::enum_for_name<EnemyType>(const char* name);
const std::vector<EnemyType>& enemy_types_for_rare_table_index(Episode episode, uint8_t rt_index);
const std::vector<EnemyType>& enemy_types_for_battle_param_stats_index(Episode episode, uint8_t bp_index);
const std::set<EnemyType>& enemy_types_for_battle_param_stats_index(Episode episode, uint8_t bp_index);
const std::set<EnemyType>& enemy_types_for_battle_param_attack_data_index(Episode episode, uint8_t bp_index);
const std::set<EnemyType>& enemy_types_for_battle_param_resist_data_index(Episode episode, uint8_t bp_index);
const std::set<EnemyType>& enemy_types_for_battle_param_movement_data_index(Episode episode, uint8_t bp_index);
+13 -15
View File
@@ -2,22 +2,20 @@
#include "Server.hh"
using namespace std;
namespace Episode3 {
const vector<uint16_t>& all_assist_card_ids(bool is_nte) {
const std::vector<uint16_t>& all_assist_card_ids(bool is_nte) {
// Note: This order matches the order that the cards are defined in the original
// code. This is relevant for consistency of results when choosing a random card
// (for God Whim).
static const vector<uint16_t> ALL_ASSIST_CARD_IDS_TRIAL = {
static const std::vector<uint16_t> ALL_ASSIST_CARD_IDS_TRIAL = {
0x00F5, 0x00F6, 0x00F7, 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x00FD, 0x00FE, 0x00FF, 0x0100, 0x0101, 0x0102,
0x0103, 0x0104, 0x0105, 0x0106, 0x0107, 0x0108, 0x0109, 0x010A, 0x010B, 0x010C, 0x010D, 0x010E, 0x010F, 0x0121,
0x0125, 0x0126, 0x0127, 0x0128, 0x0129, 0x012A, 0x012B, 0x012C, 0x012D, 0x012E, 0x012F, 0x0130, 0x0131, 0x0132,
0x0133, 0x0134, 0x0135, 0x0136, 0x0137, 0x0138, 0x0139, 0x013A, 0x013B, 0x013C, 0x013D, 0x013E, 0x013F, 0x0140,
0x0141, 0x0142, 0x0143, 0x0144, 0x0145, 0x0146, 0x0148, 0x014A, 0x014B, 0x014C, 0x014D, 0x014E, 0x023F, 0x0240,
0x0241, 0x0242};
static const vector<uint16_t> ALL_ASSIST_CARD_IDS_FINAL = {
static const std::vector<uint16_t> ALL_ASSIST_CARD_IDS_FINAL = {
0x0018, 0x0019, 0x001A, 0x00F5, 0x00F6, 0x00F7, 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x00FD, 0x00FE, 0x00FF,
0x0100, 0x0101, 0x0102, 0x0103, 0x0104, 0x0105, 0x0106, 0x0107, 0x0108, 0x0109, 0x010A, 0x010B, 0x010C, 0x010D,
0x010E, 0x010F, 0x0121, 0x0125, 0x0126, 0x0127, 0x0128, 0x0129, 0x012A, 0x012B, 0x012C, 0x012D, 0x012E, 0x012F,
@@ -28,12 +26,12 @@ const vector<uint16_t>& all_assist_card_ids(bool is_nte) {
}
AssistEffect assist_effect_number_for_card_id(uint16_t card_id, bool is_nte) {
static const unordered_map<uint16_t, AssistEffect> card_id_to_effect_final_only({
static const std::unordered_map<uint16_t, AssistEffect> card_id_to_effect_final_only({
{0x0018, /* 0x0049 */ AssistEffect::DICE_FEVER_PLUS},
{0x0019, /* 0x004A */ AssistEffect::RICH_PLUS},
{0x001A, /* 0x004B */ AssistEffect::CHARITY_PLUS},
});
static const unordered_map<uint16_t, AssistEffect> card_id_to_effect({
static const std::unordered_map<uint16_t, AssistEffect> card_id_to_effect({
{0x00F5, /* 0x0001 */ AssistEffect::DICE_HALF},
{0x00F6, /* 0x0002 */ AssistEffect::DICE_PLUS_1},
{0x00F7, /* 0x0003 */ AssistEffect::DICE_FEVER},
@@ -109,18 +107,18 @@ AssistEffect assist_effect_number_for_card_id(uint16_t card_id, bool is_nte) {
});
try {
return card_id_to_effect.at(card_id);
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
}
if (!is_nte) {
try {
return card_id_to_effect_final_only.at(card_id);
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
}
}
return AssistEffect::NONE;
}
AssistServer::AssistServer(shared_ptr<Server> server)
AssistServer::AssistServer(std::shared_ptr<Server> server)
: w_server(server),
assist_effects(AssistEffect::NONE),
num_assist_cards_set(0),
@@ -128,18 +126,18 @@ AssistServer::AssistServer(shared_ptr<Server> server)
active_assist_effects(AssistEffect::NONE),
num_active_assists(0) {}
shared_ptr<Server> AssistServer::server() {
std::shared_ptr<Server> AssistServer::server() {
auto s = this->w_server.lock();
if (!s) {
throw runtime_error("server is deleted");
throw std::runtime_error("server is deleted");
}
return s;
}
shared_ptr<const Server> AssistServer::server() const {
std::shared_ptr<const Server> AssistServer::server() const {
auto s = this->w_server.lock();
if (!s) {
throw runtime_error("server is deleted");
throw std::runtime_error("server is deleted");
}
return s;
}
@@ -148,7 +146,7 @@ uint16_t AssistServer::card_id_for_card_ref(uint16_t card_ref) const {
return this->server()->card_id_for_card_ref(card_ref);
}
shared_ptr<const CardIndex::CardEntry> AssistServer::definition_for_card_id(
std::shared_ptr<const CardIndex::CardEntry> AssistServer::definition_for_card_id(
uint16_t card_id) const {
return this->server()->definition_for_card_id(card_id);
}
+39 -48
View File
@@ -5,14 +5,11 @@
#include "../CommandFormats.hh"
#include "../SendCommands.hh"
using namespace std;
namespace Episode3 {
void BattleRecord::PlayerEntry::print(FILE* stream) const {
// TODO: Format this nicely somehow. Maybe factor out the functions in
// QuestScript that format some of these structures
phosg::print_data(stream, this, sizeof(*this), 0, nullptr, phosg::PrintDataFlags::PRINT_ASCII | phosg::PrintDataFlags::DISABLE_COLOR | phosg::PrintDataFlags::OFFSET_16_BITS);
// TODO: Format this nicely somehow. Maybe factor out the functions in QuestScript that format some of these structs
phosg::print_data(stream, this, sizeof(*this), 0, phosg::FormatDataFlags::PRINT_ASCII | phosg::FormatDataFlags::OFFSET_16_BITS);
}
BattleRecord::Event::Event(phosg::StringReader& r) {
@@ -42,7 +39,7 @@ BattleRecord::Event::Event(phosg::StringReader& r) {
this->data = r.read(r.get_u16l());
break;
default:
throw logic_error("unknown event type");
throw std::logic_error("unknown event type");
}
}
@@ -52,7 +49,7 @@ void BattleRecord::Event::serialize(phosg::StringWriter& w) const {
switch (this->type) {
case Event::Type::PLAYER_JOIN:
if (this->players.size() != 1) {
throw logic_error("player join event does not contain 1 player entry");
throw std::logic_error("player join event does not contain 1 player entry");
}
w.put(this->players[0]);
break;
@@ -76,13 +73,12 @@ void BattleRecord::Event::serialize(phosg::StringWriter& w) const {
w.write(this->data);
break;
default:
throw logic_error("unknown event type");
throw std::logic_error("unknown event type");
}
}
void BattleRecord::Event::print(FILE* stream) const {
string time_str = phosg::format_time(this->timestamp);
phosg::fwrite_fmt(stream, "Event @{:016X} ({}) ", this->timestamp, time_str);
phosg::fwrite_fmt(stream, "Event @{:016X} ({}) ", this->timestamp, phosg::format_time(this->timestamp));
switch (this->type) {
case Type::PLAYER_JOIN:
phosg::fwrite_fmt(stream, "PLAYER_JOIN {:02X}\n", this->players[0].lobby_data.client_id);
@@ -103,26 +99,26 @@ void BattleRecord::Event::print(FILE* stream) const {
break;
case Type::BATTLE_COMMAND:
phosg::fwrite_fmt(stream, "BATTLE_COMMAND\n");
phosg::print_data(stream, this->data, 0, nullptr, phosg::PrintDataFlags::PRINT_ASCII | phosg::PrintDataFlags::DISABLE_COLOR | phosg::PrintDataFlags::OFFSET_16_BITS);
phosg::print_data(stream, this->data, 0, phosg::FormatDataFlags::PRINT_ASCII | phosg::FormatDataFlags::OFFSET_16_BITS);
break;
case Type::GAME_COMMAND:
phosg::fwrite_fmt(stream, "GAME_COMMAND\n");
phosg::print_data(stream, this->data, 0, nullptr, phosg::PrintDataFlags::PRINT_ASCII | phosg::PrintDataFlags::DISABLE_COLOR | phosg::PrintDataFlags::OFFSET_16_BITS);
phosg::print_data(stream, this->data, 0, phosg::FormatDataFlags::PRINT_ASCII | phosg::FormatDataFlags::OFFSET_16_BITS);
break;
case Type::EP3_GAME_COMMAND:
phosg::fwrite_fmt(stream, "EP3_GAME_COMMAND\n");
phosg::print_data(stream, this->data, 0, nullptr, phosg::PrintDataFlags::PRINT_ASCII | phosg::PrintDataFlags::DISABLE_COLOR | phosg::PrintDataFlags::OFFSET_16_BITS);
phosg::print_data(stream, this->data, 0, phosg::FormatDataFlags::PRINT_ASCII | phosg::FormatDataFlags::OFFSET_16_BITS);
break;
case Type::CHAT_MESSAGE:
phosg::fwrite_fmt(stream, "CHAT_MESSAGE {:08X}\n", this->guild_card_number);
phosg::print_data(stream, this->data, 0, nullptr, phosg::PrintDataFlags::PRINT_ASCII | phosg::PrintDataFlags::DISABLE_COLOR | phosg::PrintDataFlags::OFFSET_16_BITS);
phosg::print_data(stream, this->data, 0, phosg::FormatDataFlags::PRINT_ASCII | phosg::FormatDataFlags::OFFSET_16_BITS);
break;
case Type::SERVER_DATA_COMMAND:
phosg::fwrite_fmt(stream, "SERVER_DATA_COMMAND\n");
phosg::print_data(stream, this->data, 0, nullptr, phosg::PrintDataFlags::PRINT_ASCII | phosg::PrintDataFlags::DISABLE_COLOR | phosg::PrintDataFlags::OFFSET_16_BITS);
phosg::print_data(stream, this->data, 0, phosg::FormatDataFlags::PRINT_ASCII | phosg::FormatDataFlags::OFFSET_16_BITS);
break;
default:
throw runtime_error("unknown event type in battle record");
throw std::runtime_error("unknown event type in battle record");
}
}
@@ -132,11 +128,8 @@ BattleRecord::BattleRecord(uint32_t behavior_flags)
battle_start_timestamp(0),
battle_end_timestamp(0) {}
BattleRecord::BattleRecord(const string& data)
: is_writable(false),
behavior_flags(0),
battle_start_timestamp(0),
battle_end_timestamp(0) {
BattleRecord::BattleRecord(const std::string& data)
: is_writable(false), behavior_flags(0), battle_start_timestamp(0), battle_end_timestamp(0) {
phosg::StringReader r(data);
uint64_t signature = r.get_u64l();
@@ -146,7 +139,7 @@ BattleRecord::BattleRecord(const string& data)
} else if (signature == this->SIGNATURE_V2) {
has_random_stream = true;
} else {
throw runtime_error("incorrect battle record signature");
throw std::runtime_error("incorrect battle record signature");
}
this->battle_start_timestamp = r.get_u64l();
@@ -160,7 +153,7 @@ BattleRecord::BattleRecord(const string& data)
}
}
string BattleRecord::serialize() const {
std::string BattleRecord::serialize() const {
phosg::StringWriter w;
w.put_u64l(this->SIGNATURE_V2);
w.put_u64l(this->battle_start_timestamp);
@@ -177,13 +170,13 @@ string BattleRecord::serialize() const {
void BattleRecord::add_player(
const PlayerLobbyDataDCGC& lobby_data,
const PlayerInventory& inventory,
const PlayerDispDataDCPCV3& disp,
const PlayerDispDataV123& disp,
uint32_t level) {
if (!this->is_writable) {
throw logic_error("cannot write to battle record");
throw std::logic_error("cannot write to battle record");
}
if (this->battle_start_timestamp != 0) {
throw runtime_error("cannot add player during battle");
throw std::runtime_error("cannot add player during battle");
}
Event& ev = this->events.emplace_back();
ev.type = Event::Type::PLAYER_JOIN;
@@ -197,7 +190,7 @@ void BattleRecord::add_player(
void BattleRecord::delete_player(uint8_t client_id) {
if (!this->is_writable) {
throw logic_error("cannot write to battle record");
throw std::logic_error("cannot write to battle record");
}
Event& ev = this->events.emplace_back();
ev.type = Event::Type::PLAYER_LEAVE;
@@ -207,7 +200,7 @@ void BattleRecord::delete_player(uint8_t client_id) {
void BattleRecord::add_command(Event::Type type, const void* data, size_t size) {
if (!this->is_writable) {
throw logic_error("cannot write to battle record");
throw std::logic_error("cannot write to battle record");
}
Event& ev = this->events.emplace_back();
ev.type = type;
@@ -215,9 +208,9 @@ void BattleRecord::add_command(Event::Type type, const void* data, size_t size)
ev.data.assign(reinterpret_cast<const char*>(data), size);
}
void BattleRecord::add_command(Event::Type type, string&& data) {
void BattleRecord::add_command(Event::Type type, std::string&& data) {
if (!this->is_writable) {
throw logic_error("cannot write to battle record");
throw std::logic_error("cannot write to battle record");
}
Event& ev = this->events.emplace_back();
ev.type = type;
@@ -225,10 +218,9 @@ void BattleRecord::add_command(Event::Type type, string&& data) {
ev.data = std::move(data);
}
void BattleRecord::add_chat_message(
uint32_t guild_card_number, string&& data) {
void BattleRecord::add_chat_message(uint32_t guild_card_number, std::string&& data) {
if (!this->is_writable) {
throw logic_error("cannot write to battle record");
throw std::logic_error("cannot write to battle record");
}
Event& ev = this->events.emplace_back();
ev.type = Event::Type::CHAT_MESSAGE;
@@ -241,7 +233,7 @@ void BattleRecord::add_random_data(const void* data, size_t size) {
this->random_stream.append(reinterpret_cast<const char*>(data), size);
}
const string& BattleRecord::get_random_stream() const {
const std::string& BattleRecord::get_random_stream() const {
return this->random_stream;
}
@@ -260,7 +252,7 @@ bool BattleRecord::is_map_definition_event(const Event& ev) {
void BattleRecord::set_battle_start_timestamp() {
if (this->battle_start_timestamp != 0) {
throw logic_error("battle start timestamp is already set");
throw std::logic_error("battle start timestamp is already set");
}
this->battle_start_timestamp = phosg::now();
@@ -272,30 +264,30 @@ void BattleRecord::set_battle_start_timestamp() {
for (auto& ev : this->events) {
if (ev.type == Event::Type::PLAYER_JOIN) {
if (ev.players.size() != 1) {
throw logic_error("player join event does not contain 1 player entry");
throw std::logic_error("player join event does not contain 1 player entry");
}
auto& player = ev.players[0];
if (player.lobby_data.client_id >= 4) {
throw runtime_error("invalid client ID");
throw std::runtime_error("invalid client ID");
}
players[player.lobby_data.client_id] = player;
players_present[player.lobby_data.client_id] = true;
} else if (ev.type == Event::Type::PLAYER_LEAVE) {
if (ev.leaving_client_id >= 4) {
throw logic_error("invalid client ID");
throw std::logic_error("invalid client ID");
}
players_present[ev.leaving_client_id] = false;
} else if (ev.type == Event::Type::SET_INITIAL_PLAYERS) {
throw logic_error("BattleRecord::set_battle_start_timestamp called twice");
throw std::logic_error("BattleRecord::set_battle_start_timestamp called twice");
} else if (this->is_map_definition_event(ev)) {
num_map_events++;
}
}
deque<Event> new_events;
std::deque<Event> new_events;
// Generate the initial players event
Event initial_ev;
@@ -336,32 +328,31 @@ void BattleRecord::set_battle_end_timestamp() {
}
void BattleRecord::print(FILE* stream) const {
string start_str = phosg::format_time(this->battle_start_timestamp);
string end_str = phosg::format_time(this->battle_end_timestamp);
phosg::fwrite_fmt(stream, "BattleRecord {} behavior_flags={:08X} start={:016X} ({}) end={:016X} ({}); {} events\n",
this->is_writable ? "writable" : "read-only",
this->behavior_flags,
this->battle_start_timestamp,
start_str,
phosg::format_time(this->battle_start_timestamp),
this->battle_end_timestamp,
end_str, this->events.size());
phosg::format_time(this->battle_end_timestamp),
this->events.size());
for (const auto& event : this->events) {
event.print(stream);
}
}
BattleRecordPlayer::BattleRecordPlayer(std::shared_ptr<asio::io_context> io_context, shared_ptr<const BattleRecord> rec)
BattleRecordPlayer::BattleRecordPlayer(std::shared_ptr<asio::io_context> io_context, std::shared_ptr<const BattleRecord> rec)
: io_context(io_context),
record(rec),
event_it(this->record->events.begin()),
play_start_timestamp(0),
next_command_timer(*this->io_context) {}
shared_ptr<const BattleRecord> BattleRecordPlayer::get_record() const {
std::shared_ptr<const BattleRecord> BattleRecordPlayer::get_record() const {
return this->record;
}
void BattleRecordPlayer::set_lobby(shared_ptr<Lobby> l) {
void BattleRecordPlayer::set_lobby(std::shared_ptr<Lobby> l) {
this->lobby = l;
}
@@ -407,7 +398,7 @@ asio::awaitable<void> BattleRecordPlayer::play_task() {
switch (ev.type) {
case BattleRecord::Event::Type::PLAYER_JOIN:
// Technically we can support this, but it should never happen
throw runtime_error("player join event during battle replay");
throw std::runtime_error("player join event during battle replay");
case BattleRecord::Event::Type::PLAYER_LEAVE:
send_player_leave_notification(l, ev.leaving_client_id);
break;
+2 -2
View File
@@ -22,7 +22,7 @@ public:
struct PlayerEntry {
PlayerLobbyDataDCGC lobby_data;
PlayerInventory inventory;
PlayerDispDataDCPCV3 disp;
PlayerDispDataV123 disp;
le_uint32_t level;
void print(FILE* stream) const;
@@ -85,7 +85,7 @@ public:
void add_player(
const PlayerLobbyDataDCGC& lobby_data,
const PlayerInventory& inventory,
const PlayerDispDataDCPCV3& disp,
const PlayerDispDataV123& disp,
uint32_t level);
void delete_player(uint8_t client_id);
void add_command(Event::Type type, const void* data, size_t size);
+52 -59
View File
@@ -3,11 +3,9 @@
#include "../CommandFormats.hh"
#include "Server.hh"
using namespace std;
namespace Episode3 {
Card::Card(uint16_t card_id, uint16_t card_ref, uint16_t client_id, shared_ptr<Server> server)
Card::Card(uint16_t card_id, uint16_t card_ref, uint16_t client_id, std::shared_ptr<Server> server)
: w_server(server),
w_player_state(server->get_player_state(client_id)),
client_id(client_id),
@@ -39,7 +37,7 @@ void Card::init() {
// Arkz-side. This could break things later on in the battle, and even if it
// doesn't, it certainly isn't behavior that the player would expect, so we
// prevent it instead.
throw runtime_error("card definition is missing");
throw std::runtime_error("card definition is missing");
}
this->sc_card_ref = ps->get_sc_card_ref();
this->sc_def_entry = s->definition_for_card_id(ps->get_sc_card_id());
@@ -56,7 +54,7 @@ void Card::init() {
} else {
int16_t rules_char_hp = s->map_and_rules->rules.char_hp;
int16_t base_char_hp = (rules_char_hp == 0) ? 15 : rules_char_hp;
int16_t hp = clamp<int16_t>(base_char_hp + this->def_entry->def.hp.stat, 1, 99);
int16_t hp = std::clamp<int16_t>(base_char_hp + this->def_entry->def.hp.stat, 1, 99);
this->max_hp = hp;
this->current_hp = hp;
}
@@ -78,34 +76,34 @@ void Card::init() {
}
}
shared_ptr<Server> Card::server() {
std::shared_ptr<Server> Card::server() {
auto s = this->w_server.lock();
if (!s) {
throw runtime_error("server is deleted");
throw std::runtime_error("server is deleted");
}
return s;
}
shared_ptr<const Server> Card::server() const {
std::shared_ptr<const Server> Card::server() const {
auto s = this->w_server.lock();
if (!s) {
throw runtime_error("server is deleted");
throw std::runtime_error("server is deleted");
}
return s;
}
shared_ptr<PlayerState> Card::player_state() {
std::shared_ptr<PlayerState> Card::player_state() {
auto s = this->w_player_state.lock();
if (!s) {
throw runtime_error("server is deleted");
throw std::runtime_error("server is deleted");
}
return s;
}
shared_ptr<const PlayerState> Card::player_state() const {
std::shared_ptr<const PlayerState> Card::player_state() const {
auto s = this->w_player_state.lock();
if (!s) {
throw runtime_error("server is deleted");
throw std::runtime_error("server is deleted");
}
return s;
}
@@ -158,7 +156,7 @@ ssize_t Card::apply_abnormal_condition(
int16_t existing_cond_value = 0;
auto& cond = this->action_chain.conditions[cond_index];
if ((eff.type == ConditionType::MV_BONUS) && (cond.type == ConditionType::MV_BONUS)) {
existing_cond_value = clamp<int16_t>(cond.value, -99, 99);
existing_cond_value = std::clamp<int16_t>(cond.value, -99, 99);
log.debug_f("MV_BONUS combines => existing_cond_value = {}", existing_cond_value);
}
@@ -176,7 +174,7 @@ ssize_t Card::apply_abnormal_condition(
switch (eff.arg1.at(0)) {
case 'a': {
string s = eff.arg1.decode();
std::string s = eff.arg1.decode();
cond.a_arg_value = atoi(s.c_str() + 1);
break;
}
@@ -190,13 +188,12 @@ ssize_t Card::apply_abnormal_condition(
cond.remaining_turns = 102;
break;
case 't': {
string s = eff.arg1.decode();
std::string s = eff.arg1.decode();
cond.remaining_turns = atoi(s.c_str() + 1);
}
}
string cond_str = cond.str(s);
log.debug_f("wrote condition {} => {}", cond_index, cond_str);
log.debug_f("wrote condition {} => {}", cond_index, cond.str(s));
if (!is_nte) {
s->card_special->update_condition_orders(this->shared_from_this());
@@ -204,8 +201,7 @@ ssize_t Card::apply_abnormal_condition(
if (this->action_chain.conditions[z].type == ConditionType::NONE) {
continue;
}
string cond_str = cond.str(s);
log.debug_f("sorted conditions: [{}] => {}", z, cond_str);
log.debug_f("sorted conditions: [{}] => {}", z, cond.str(s));
}
}
@@ -213,7 +209,7 @@ ssize_t Card::apply_abnormal_condition(
}
void Card::apply_ap_and_tp_adjust_assists_to_attack(
shared_ptr<const Card> attacker_card,
std::shared_ptr<const Card> attacker_card,
int16_t* inout_attacker_ap,
int16_t* in_defense_power,
int16_t* inout_attacker_tp) const {
@@ -226,12 +222,12 @@ void Card::apply_ap_and_tp_adjust_assists_to_attack(
switch (s->assist_server->get_active_assist_by_index(z)) {
case AssistEffect::POWERLESS_RAIN:
if (is_nte) {
*inout_attacker_ap = max<int16_t>(*inout_attacker_ap - 2, 0);
*inout_attacker_ap = std::max<int16_t>(*inout_attacker_ap - 2, 0);
}
break;
case AssistEffect::BRAVE_WIND:
if (is_nte) {
*inout_attacker_ap = max<int16_t>(*inout_attacker_ap + 2, 0);
*inout_attacker_ap = std::max<int16_t>(*inout_attacker_ap + 2, 0);
}
break;
case AssistEffect::SILENT_COLOSSEUM:
@@ -284,7 +280,7 @@ bool Card::check_card_flag(uint32_t flags) const {
void Card::commit_attack(
int16_t damage,
shared_ptr<Card> attacker_card,
std::shared_ptr<Card> attacker_card,
G_ApplyConditionEffect_Ep3_6xB4x06* cmd,
size_t strike_number,
int16_t* out_effective_damage) {
@@ -302,7 +298,7 @@ void Card::commit_attack(
auto eff = s->assist_server->get_active_assist_by_index(z);
if ((eff == AssistEffect::RANSOM) && (attacker_card->action_chain.chain.attack_medium == AttackMedium::PHYSICAL)) {
uint8_t team_id = this->player_state()->get_team_id();
int16_t exp_amount = clamp<int16_t>(s->team_exp[team_id], 0, effective_damage);
int16_t exp_amount = std::clamp<int16_t>(s->team_exp[team_id], 0, effective_damage);
s->team_exp[team_id] -= exp_amount;
effective_damage -= exp_amount;
if (!is_nte) {
@@ -326,7 +322,7 @@ void Card::commit_attack(
this->player_state()->stats.damage_taken += effective_damage;
log.debug_f("updated stats");
this->current_hp = clamp<int16_t>(this->current_hp - effective_damage, 0, this->max_hp);
this->current_hp = std::clamp<int16_t>(this->current_hp - effective_damage, 0, this->max_hp);
log.debug_f("hp set to {}", this->current_hp);
if ((effective_damage > 0) && (attacker_ps->stats.max_attack_damage < effective_damage)) {
@@ -366,7 +362,7 @@ void Card::commit_attack(
}
}
int16_t Card::compute_defense_power_for_attacker_card(shared_ptr<const Card> attacker_card) {
int16_t Card::compute_defense_power_for_attacker_card(std::shared_ptr<const Card> attacker_card) {
if (!attacker_card) {
return 0;
}
@@ -392,7 +388,7 @@ int16_t Card::compute_defense_power_for_attacker_card(shared_ptr<const Card> att
return this->action_metadata.defense_power + this->action_metadata.defense_bonus;
}
void Card::destroy_set_card(shared_ptr<Card> attacker_card) {
void Card::destroy_set_card(std::shared_ptr<Card> attacker_card) {
auto s = this->server();
auto ps = this->player_state();
@@ -490,7 +486,7 @@ int32_t Card::error_code_for_move_to_location(const Location& loc) const {
return 0;
}
void Card::execute_attack(shared_ptr<Card> attacker_card) {
void Card::execute_attack(std::shared_ptr<Card> attacker_card) {
if (!attacker_card) {
return;
}
@@ -518,7 +514,7 @@ void Card::execute_attack(shared_ptr<Card> attacker_card) {
if (attacker_card->action_chain.chain.attack_medium == AttackMedium::UNKNOWN_03) {
// Probably Resta
for (size_t strike_num = 0; strike_num < attacker_card->action_chain.chain.strike_count; strike_num++) {
this->current_hp = min<int16_t>(this->current_hp + attacker_card->action_chain.chain.effective_tp, this->max_hp);
this->current_hp = std::min<int16_t>(this->current_hp + attacker_card->action_chain.chain.effective_tp, this->max_hp);
}
this->propagate_shared_hp_if_needed();
cmd.effect.tp = attacker_card->action_chain.chain.effective_tp;
@@ -545,7 +541,7 @@ void Card::execute_attack(shared_ptr<Card> attacker_card) {
log.debug_f("assist adjusts ap={}, defense={}", attack_ap, defense_power);
int16_t raw_damage = attack_ap - defense_power;
int16_t preliminary_damage = max<int16_t>(raw_damage, 0) - attack_tp;
int16_t preliminary_damage = std::max<int16_t>(raw_damage, 0) - attack_tp;
this->last_attack_preliminary_damage = preliminary_damage;
log.debug_f("raw_damage={}, preliminary_damange={}", raw_damage, preliminary_damage);
@@ -571,8 +567,8 @@ void Card::execute_attack(shared_ptr<Card> attacker_card) {
}
}
cmd.effect.current_hp = is_nte ? attack_ap : min<int16_t>(attack_ap, 99);
cmd.effect.ap = is_nte ? defense_power : min<int16_t>(defense_power, 99);
cmd.effect.current_hp = is_nte ? attack_ap : std::min<int16_t>(attack_ap, 99);
cmd.effect.ap = is_nte ? defense_power : std::min<int16_t>(defense_power, 99);
cmd.effect.tp = attack_tp;
auto ps = this->player_state();
@@ -583,7 +579,7 @@ void Card::execute_attack(shared_ptr<Card> attacker_card) {
for (size_t strike_num = 0; strike_num < attacker_card->action_chain.chain.strike_count; strike_num++) {
int16_t final_effective_damage = 0;
target->commit_attack(preliminary_damage, attacker_card, &cmd, strike_num, &final_effective_damage);
ps->stats.action_card_negated_damage += max<int16_t>(0, this->current_defense_power - final_effective_damage);
ps->stats.action_card_negated_damage += std::max<int16_t>(0, this->current_defense_power - final_effective_damage);
}
} else {
log.debug_f("flag 2 set; committing zero-damage attack");
@@ -618,7 +614,7 @@ const Condition* Card::find_condition(ConditionType cond_type) const {
return const_cast<Card*>(this)->find_condition(cond_type);
}
shared_ptr<const CardIndex::CardEntry> Card::get_definition() const {
std::shared_ptr<const CardIndex::CardEntry> Card::get_definition() const {
return this->def_entry;
}
@@ -839,10 +835,10 @@ void Card::set_current_and_max_hp(int16_t hp) {
void Card::set_current_hp(
uint32_t new_hp, bool propagate_shared_hp, bool enforce_max_hp) {
if (!enforce_max_hp) {
new_hp = max<int16_t>(new_hp, 0);
this->max_hp = max<int16_t>(this->max_hp, new_hp);
new_hp = std::max<int16_t>(new_hp, 0);
this->max_hp = std::max<int16_t>(this->max_hp, new_hp);
} else {
new_hp = clamp<int16_t>(new_hp, 0, this->max_hp);
new_hp = std::clamp<int16_t>(new_hp, 0, this->max_hp);
}
this->current_hp = new_hp;
@@ -955,11 +951,11 @@ void Card::compute_action_chain_results(bool apply_action_conditions, bool ignor
}
if (!this->action_chain.check_flag(0x10)) {
this->action_chain.chain.effective_ap = is_nte ? effective_ap : min<int16_t>(effective_ap, 99);
this->action_chain.chain.effective_ap = is_nte ? effective_ap : std::min<int16_t>(effective_ap, 99);
log.debug_f("set chain effective_ap = {}", this->action_chain.chain.effective_ap);
}
if (!this->action_chain.check_flag(0x20)) {
this->action_chain.chain.effective_tp = is_nte ? effective_tp : min<int16_t>(effective_tp, 99);
this->action_chain.chain.effective_tp = is_nte ? effective_tp : std::min<int16_t>(effective_tp, 99);
log.debug_f("set chain effective_tp = {}", this->action_chain.chain.effective_tp);
}
@@ -1099,7 +1095,7 @@ void Card::compute_action_chain_results(bool apply_action_conditions, bool ignor
this->action_chain.chain.damage = is_nte
? (damage * this->action_chain.chain.damage_multiplier)
: min<int16_t>(damage * this->action_chain.chain.damage_multiplier, 99);
: std::min<int16_t>(damage * this->action_chain.chain.damage_multiplier, 99);
log.debug_f("overall chain damage = {} (base) * {} (mult) = {}",
damage, this->action_chain.chain.damage_multiplier, this->action_chain.chain.damage);
@@ -1108,7 +1104,7 @@ void Card::compute_action_chain_results(bool apply_action_conditions, bool ignor
s->card_special->apply_action_conditions(EffectWhen::BEFORE_ANY_CARD_ATTACK, this_sh, this_sh, 2, nullptr);
log.debug_f("applied action conditions (2)");
if (!is_nte && this->action_chain.check_flag(0x100)) {
this->action_chain.chain.damage = min<int16_t>(this->action_chain.chain.damage + 5, 99);
this->action_chain.chain.damage = std::min<int16_t>(this->action_chain.chain.damage + 5, 99);
log.debug_f("(has flag 0x100) chain damage = {}", this->action_chain.chain.damage);
}
} else {
@@ -1141,8 +1137,7 @@ void Card::compute_action_chain_results(bool apply_action_conditions, bool ignor
}
if (log.should_log(phosg::LogLevel::L_DEBUG)) {
string chain_str = this->action_chain.str(s);
log.debug_f("result computed as {}", chain_str);
log.debug_f("result computed as {}", this->action_chain.str(s));
}
}
@@ -1208,20 +1203,19 @@ void Card::move_phase_before() {
this->server()->card_special->move_phase_before_for_card(this->shared_from_this());
}
void Card::unknown_80236374(shared_ptr<Card> other_card, const ActionState* as) {
void Card::unknown_80236374(std::shared_ptr<Card> other_card, const ActionState* as) {
auto s = this->server();
auto log = s->log_stack(std::format("unknown_80236374(@{:04X} #{:04X}, @{:04X} #{:04X}): ", this->get_card_ref(), this->get_card_id(), other_card->get_card_ref(), other_card->get_card_id()));
if (log.should_log(phosg::LogLevel::L_DEBUG)) {
if (as) {
string as_str = as->str(s);
log.debug_f("as = {}", as_str);
log.debug_f("as = {}", as->str(s));
} else {
log.debug_f("as = null");
}
}
auto check_card = [&](shared_ptr<Card> card) -> void {
auto check_card = [&](std::shared_ptr<Card> card) -> void {
if (card) {
if (!card->unknown_80236554(other_card, as)) {
log.debug_f("check_card @{:04X} #{:04X} => false", card->get_card_ref(), card->get_card_id());
@@ -1263,7 +1257,7 @@ void Card::unknown_802379DC(const ActionState& pa) {
pa.defense_card_ref, this->shared_from_this(), pa.original_attacker_card_ref);
this->server()->card_special->unknown_8024A9D8(pa, 0xFFFF);
for (size_t z = 0; z < this->action_metadata.target_card_ref_count; z++) {
shared_ptr<Card> card = this->server()->card_for_set_card_ref(this->action_metadata.target_card_refs[z]);
std::shared_ptr<Card> card = this->server()->card_for_set_card_ref(this->action_metadata.target_card_refs[z]);
if (card) {
card->action_chain.set_action_subphase_from_card(this->shared_from_this());
card->send_6xB4x4E_4C_4D_if_needed();
@@ -1302,7 +1296,7 @@ void Card::unknown_80237A90(const ActionState& pa, uint16_t action_card_ref) {
if (this->action_chain.chain.target_card_ref_count == 0) {
for (size_t z = 0; (z < 4 * 9) && (pa.target_card_refs[z] != 0xFFFF); z++) {
this->action_chain.add_target_card_ref(pa.target_card_refs[z]);
shared_ptr<Card> sc_card = s->card_for_set_card_ref(pa.target_card_refs[z]);
std::shared_ptr<Card> sc_card = s->card_for_set_card_ref(pa.target_card_refs[z]);
if (sc_card) {
sc_card->action_metadata.add_target_card_ref(this->card_ref);
sc_card->card_flags |= 8;
@@ -1357,7 +1351,7 @@ bool Card::is_guard_item() const {
return (this->def_entry->def.card_class() == CardClass::GUARD_ITEM);
}
bool Card::unknown_80236554(shared_ptr<Card> other_card, const ActionState* as) {
bool Card::unknown_80236554(std::shared_ptr<Card> other_card, const ActionState* as) {
auto s = this->server();
auto log = s->log_stack(other_card
? std::format("unknown_80236554(@{:04X} #{:04X}, @{:04X} #{:04X}): ", this->get_card_ref(), this->get_card_id(), other_card->get_card_ref(), other_card->get_card_id())
@@ -1395,7 +1389,7 @@ bool Card::unknown_80236554(shared_ptr<Card> other_card, const ActionState* as)
}
}
this->action_metadata.attack_bonus = max<int16_t>(attack_bonus, 0);
this->action_metadata.attack_bonus = std::max<int16_t>(attack_bonus, 0);
log.debug_f("attack_bonus = {}", this->action_metadata.attack_bonus);
this->last_attack_preliminary_damage = 0;
this->last_attack_final_damage = 0;
@@ -1421,17 +1415,17 @@ bool Card::unknown_80236554(shared_ptr<Card> other_card, const ActionState* as)
return ret;
}
void Card::execute_attack_on_all_valid_targets(shared_ptr<Card> attacker_card) {
void Card::execute_attack_on_all_valid_targets(std::shared_ptr<Card> attacker_card) {
auto s = this->server();
for (size_t client_id = 0; client_id < 4; client_id++) {
auto ps = s->player_states[client_id];
if (ps) {
shared_ptr<Card> card = ps->get_sc_card();
std::shared_ptr<Card> card = ps->get_sc_card();
if (card) {
card->execute_attack(attacker_card);
}
for (size_t set_index = 0; set_index < 8; set_index++) {
shared_ptr<Card> card = ps->get_set_card(set_index);
std::shared_ptr<Card> card = ps->get_set_card(set_index);
if (card) {
card->execute_attack(attacker_card);
}
@@ -1476,7 +1470,7 @@ void Card::apply_attack_result() {
temp_chain.chain.target_card_ref_count++;
} else if ((target_card->get_definition()->def.type == CardType::ITEM) && !this->action_chain.check_flag(0x02)) {
auto target_ps = target_card->player_state();
shared_ptr<Card> candidate_card;
std::shared_ptr<Card> candidate_card;
for (size_t set_index = 0; set_index < 8; set_index++) {
auto set_card = target_ps->get_set_card(set_index);
if (set_card && (set_card != target_card) && !(set_card->card_flags & 2) && set_card->is_guard_item()) {
@@ -1555,12 +1549,11 @@ void Card::apply_attack_result() {
}
if (log.should_log(phosg::LogLevel::L_DEBUG)) {
string as_str = as.str(s);
log.debug_f("as constructed as {}", as_str);
log.debug_f("as constructed as {}", as.str(s));
}
for (size_t z = 0; z < this->action_chain.chain.target_card_ref_count; z++) {
shared_ptr<Card> card = s->card_for_set_card_ref(this->action_chain.chain.target_card_refs[z]);
std::shared_ptr<Card> card = s->card_for_set_card_ref(this->action_chain.chain.target_card_refs[z]);
if (card) {
card->current_defense_power = card->action_metadata.attack_bonus;
if (!this->action_chain.check_flag(0x40)) {
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -905,7 +905,7 @@ struct PlayerConfig {
/* 2270:211C */ be_uint32_t unknown_t3;
// This visual config is copied to the player's main visual config when the player's name or proportions have
// changed, or when certain buttons on the controller (L, R, X, Y) are held at game start time.
/* 2274:2120 */ PlayerVisualConfig backup_visual;
/* 2274:2120 */ PlayerVisualConfigV123 backup_visual;
/* 22C4:2170 */ parray<uint8_t, 0x8C> unknown_a14;
/* 2350:21FC */
+6 -8
View File
@@ -2,8 +2,6 @@
#include "Server.hh"
using namespace std;
namespace Episode3 {
NameEntry::NameEntry() {
@@ -198,7 +196,7 @@ void DeckState::redraw_initial_hand(bool is_nte) {
auto s = this->server.lock();
if (!s) {
throw runtime_error("server is missing");
throw std::runtime_error("server is missing");
}
// Shuffle the deck, except the first 5 cards (which are about to be drawn).
@@ -260,7 +258,7 @@ void DeckState::shuffle() {
if (this->shuffle_enabled) {
auto s = this->server.lock();
if (!s) {
throw runtime_error("server is missing");
throw std::runtime_error("server is missing");
}
size_t max = this->num_drawable_cards();
@@ -305,17 +303,17 @@ void DeckState::print(FILE* stream, std::shared_ptr<const CardIndex> card_index)
this->loop_enabled ? "true" : "false");
for (size_t z = 0; z < 31; z++) {
const auto& e = this->entries[z];
shared_ptr<const CardIndex::CardEntry> ce;
std::shared_ptr<const CardIndex::CardEntry> ce;
if (card_index) {
try {
ce = card_index->definition_for_id(e.card_id);
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
}
}
if (ce) {
string name = ce->def.en_name.decode(Language::ENGLISH);
phosg::fwrite_fmt(stream, " ({:02}) index={:02X} ref=@{:04X} card_id=#{:04X} \"{}\" {}\n",
z, e.deck_index, this->card_refs[z], e.card_id, name, name_for_card_state(e.state));
z, e.deck_index, this->card_refs[z], e.card_id, ce->def.en_name.decode(Language::ENGLISH),
name_for_card_state(e.state));
} else {
phosg::fwrite_fmt(stream, " ({:02}) index={:02X} ref=@{:04X} card_id=#{:04X} {}\n",
z, e.deck_index, this->card_refs[z], e.card_id, name_for_card_state(e.state));
-2
View File
@@ -1,7 +1,5 @@
#include "MapState.hh"
using namespace std;
namespace Episode3 {
MapState::MapState() {
+48 -54
View File
@@ -2,11 +2,9 @@
#include "Server.hh"
using namespace std;
namespace Episode3 {
PlayerState::PlayerState(uint8_t client_id, shared_ptr<Server> server)
PlayerState::PlayerState(uint8_t client_id, std::shared_ptr<Server> server)
: w_server(server),
client_id(client_id),
num_hand_redraws_allowed(1),
@@ -46,10 +44,10 @@ void PlayerState::init() {
if (s->player_states.at(this->client_id).get() != this) {
// Note: The original code handles this, but we don't. This appears not to
// ever happen, so we didn't bother implementing it.
throw logic_error("replacing a player state object is not permitted");
throw std::logic_error("replacing a player state object is not permitted");
}
this->deck_state = make_shared<DeckState>(this->client_id, s->deck_entries[client_id]->card_ids, s);
this->deck_state = std::make_shared<DeckState>(this->client_id, s->deck_entries[client_id]->card_ids, s);
if (s->map_and_rules->rules.disable_deck_shuffle) {
this->deck_state->disable_shuffle();
}
@@ -62,7 +60,7 @@ void PlayerState::init() {
this->team_id = s->deck_entries[this->client_id]->team_id;
auto sc_ce = s->definition_for_card_ref(this->sc_card_ref);
if (!sc_ce) {
throw runtime_error("SC card definition is missing");
throw std::runtime_error("SC card definition is missing");
}
if (sc_ce->def.type == CardType::HUNTERS_SC) {
this->sc_card_type = CardType::HUNTERS_SC;
@@ -72,13 +70,13 @@ void PlayerState::init() {
// In the original code, sc_card_type gets left as 0xFFFFFFFF (yes, it's a
// uint32_t). This probably breaks some things later on, so we instead
// prevent it upfront.
throw runtime_error("SC card is not a Hunters or Arkz SC");
throw std::runtime_error("SC card is not a Hunters or Arkz SC");
}
this->hand_and_equip = make_shared<HandAndEquipState>();
this->card_short_statuses = make_shared<parray<CardShortStatus, 0x10>>();
this->set_card_action_chains = make_shared<parray<ActionChainWithConds, 9>>();
this->set_card_action_metadatas = make_shared<parray<ActionMetadata, 9>>();
this->hand_and_equip = std::make_shared<HandAndEquipState>();
this->card_short_statuses = std::make_shared<parray<CardShortStatus, 0x10>>();
this->set_card_action_chains = std::make_shared<parray<ActionChainWithConds, 9>>();
this->set_card_action_metadatas = std::make_shared<parray<ActionMetadata, 9>>();
this->hand_and_equip->clear_FF();
for (size_t z = 0; z < 0x10; z++) {
@@ -89,7 +87,7 @@ void PlayerState::init() {
this->set_card_action_metadatas->at(z).clear_FF();
}
this->sc_card = make_shared<Card>(this->deck_state->sc_card_id(), this->sc_card_ref, this->client_id, s);
this->sc_card = std::make_shared<Card>(this->deck_state->sc_card_id(), this->sc_card_ref, this->client_id, s);
this->sc_card->init();
this->draw_initial_hand();
if (s->options.is_nte()) {
@@ -114,18 +112,18 @@ void PlayerState::init() {
this->god_whim_can_use_hidden_cards = (s->deck_entries[this->client_id]->god_whim_flag != 3);
}
shared_ptr<Server> PlayerState::server() {
std::shared_ptr<Server> PlayerState::server() {
auto s = this->w_server.lock();
if (!s) {
throw runtime_error("server is deleted");
throw std::runtime_error("server is deleted");
}
return s;
}
shared_ptr<const Server> PlayerState::server() const {
std::shared_ptr<const Server> PlayerState::server() const {
auto s = this->w_server.lock();
if (!s) {
throw runtime_error("server is deleted");
throw std::runtime_error("server is deleted");
}
return s;
}
@@ -151,7 +149,7 @@ bool PlayerState::draw_cards_allowed() const {
return true;
}
void PlayerState::apply_assist_card_effect_on_set(shared_ptr<PlayerState> setter_ps) {
void PlayerState::apply_assist_card_effect_on_set(std::shared_ptr<PlayerState> setter_ps) {
auto s = this->server();
uint16_t assist_card_id = this->set_assist_card_id;
@@ -244,7 +242,7 @@ void PlayerState::apply_assist_card_effect_on_set(shared_ptr<PlayerState> setter
case AssistEffect::LEGACY: {
uint16_t total_cost = 0;
for (ssize_t z = 7; z >= 0; z--) {
shared_ptr<const Card> card = this->set_cards[z];
std::shared_ptr<const Card> card = this->set_cards[z];
if (card) {
auto ce = card->get_definition();
uint8_t card_cost = ce->def.self_cost;
@@ -258,7 +256,7 @@ void PlayerState::apply_assist_card_effect_on_set(shared_ptr<PlayerState> setter
if (!is_nte) {
this->on_cards_destroyed();
}
this->atk_points = min<uint8_t>(9, this->atk_points + (total_cost >> 1));
this->atk_points = std::min<uint8_t>(9, this->atk_points + (total_cost >> 1));
this->update_hand_and_equip_state_and_send_6xB4x02_if_needed();
if (!is_nte) {
s->send_6xB4x05();
@@ -323,7 +321,7 @@ void PlayerState::apply_assist_card_effect_on_set(shared_ptr<PlayerState> setter
if (!s->options.is_nte()) {
s->team_num_cards_destroyed[this->team_id] = 0;
for (size_t client_id = 0; client_id < 4; client_id++) {
const auto other_ps = s->get_player_state(client_id);
auto other_ps = s->get_player_state(client_id);
if (other_ps && (this->team_id == other_ps->get_team_id())) {
auto card = other_ps->get_sc_card();
if (card) {
@@ -353,7 +351,7 @@ void PlayerState::apply_assist_card_effect_on_set(shared_ptr<PlayerState> setter
if (is_nte
? (other_ps->assist_remaining_turns != 90 && other_ps->assist_remaining_turns != 99)
: (other_ps->assist_remaining_turns < 10)) {
other_ps->assist_remaining_turns = min<uint8_t>(9, other_ps->assist_remaining_turns << 1);
other_ps->assist_remaining_turns = std::min<uint8_t>(9, other_ps->assist_remaining_turns << 1);
}
for (ssize_t set_index = is_nte ? 0 : -1; set_index < 8; set_index++) {
@@ -369,7 +367,7 @@ void PlayerState::apply_assist_card_effect_on_set(shared_ptr<PlayerState> setter
cond.remaining_turns <<= 1;
}
} else if (cond.remaining_turns < 10) {
cond.remaining_turns = min<uint8_t>(9, cond.remaining_turns << 1);
cond.remaining_turns = std::min<uint8_t>(9, cond.remaining_turns << 1);
}
}
}
@@ -480,7 +478,7 @@ void PlayerState::apply_dice_effects() {
}
for (size_t die_index = 0; die_index < 2; die_index++) {
this->dice_results[die_index] = min<uint8_t>(this->dice_results[die_index], 9);
this->dice_results[die_index] = std::min<uint8_t>(this->dice_results[die_index], 9);
}
}
@@ -818,15 +816,14 @@ int32_t PlayerState::error_code_for_client_setting_card(
}
}
vector<uint16_t> PlayerState::get_all_cards_within_range(
std::vector<uint16_t> PlayerState::get_all_cards_within_range(
const parray<uint8_t, 9 * 9>& range, const Location& loc, uint8_t target_team_id) const {
auto s = this->server();
auto log = s->log_stack("get_all_cards_within_range: ");
string loc_str = loc.str();
log.debug_f("loc={}, target_team_id={:02X}", loc_str, target_team_id);
log.debug_f("loc={}, target_team_id={:02X}", loc.str(), target_team_id);
vector<uint16_t> ret;
std::vector<uint16_t> ret;
for (size_t client_id = 0; client_id < 4; client_id++) {
auto other_ps = s->player_states[client_id];
if (other_ps && ((target_team_id == 0xFF) || (target_team_id == other_ps->get_team_id()))) {
@@ -845,7 +842,7 @@ void PlayerState::get_short_status_for_card_index_in_hand(size_t hand_index, Car
stat->card_ref = this->card_refs[hand_index - 1];
}
shared_ptr<DeckState> PlayerState::get_deck() {
std::shared_ptr<DeckState> PlayerState::get_deck() {
return this->deck_state;
}
@@ -871,11 +868,11 @@ uint16_t PlayerState::get_sc_card_id() const {
return this->sc_card_id;
}
shared_ptr<Card> PlayerState::get_sc_card() {
std::shared_ptr<Card> PlayerState::get_sc_card() {
return this->sc_card;
}
shared_ptr<const Card> PlayerState::get_sc_card() const {
std::shared_ptr<const Card> PlayerState::get_sc_card() const {
return this->sc_card;
}
@@ -887,11 +884,11 @@ CardType PlayerState::get_sc_card_type() const {
return this->sc_card_type;
}
shared_ptr<Card> PlayerState::get_set_card(size_t set_index) {
std::shared_ptr<Card> PlayerState::get_set_card(size_t set_index) {
return (set_index < 8) ? this->set_cards[set_index] : nullptr;
}
shared_ptr<const Card> PlayerState::get_set_card(size_t set_index) const {
std::shared_ptr<const Card> PlayerState::get_set_card(size_t set_index) const {
return (set_index < 8) ? this->set_cards[set_index] : nullptr;
}
@@ -964,7 +961,7 @@ uint16_t PlayerState::pop_from_discard_log(uint16_t) {
bool PlayerState::move_card_to_location_by_card_index(size_t card_index, const Location& new_loc) {
auto s = this->server();
shared_ptr<Card> card;
std::shared_ptr<Card> card;
if (card_index == 0) {
card = this->sc_card;
} else {
@@ -1010,7 +1007,7 @@ void PlayerState::move_null_hand_refs_to_end() {
void PlayerState::on_cards_destroyed() {
auto s = this->server();
unordered_multimap<uint16_t, bool> card_refs_map; // {card_ref: should_return_to_hand}
std::unordered_multimap<uint16_t, bool> card_refs_map; // {card_ref: should_return_to_hand}
for (size_t z = 0; z < 8; z++) {
auto card = this->set_cards[z];
if (!card || !(card->card_flags & 2)) {
@@ -1333,7 +1330,7 @@ bool PlayerState::set_card_from_hand(
}
this->card_refs[card_index + 1] = card_ref;
// Note: NTE doesn't call the destructor on the existing card, if there is one. Is that a bug?
this->set_cards[card_index - 7] = make_shared<Card>(s->card_id_for_card_ref(card_ref), card_ref, this->client_id, s);
this->set_cards[card_index - 7] = std::make_shared<Card>(s->card_id_for_card_ref(card_ref), card_ref, this->client_id, s);
auto new_card = this->set_cards[card_index - 7];
new_card->init();
@@ -1433,7 +1430,7 @@ void PlayerState::set_initial_location() {
static const uint8_t start_tile_defs_offset_for_team_size[4] = {0, 0, 1, 3};
if (num_team_players >= 4) {
throw logic_error("too many players on team");
throw std::logic_error("too many players on team");
}
size_t start_tile_def_index = start_tile_defs_offset_for_team_size[num_team_players] + player_index_within_team;
uint8_t player_start_tile = mr->map.start_tile_definitions[this->team_id][start_tile_def_index];
@@ -1455,11 +1452,11 @@ void PlayerState::set_initial_location() {
}
}
if (!start_tile_found) {
throw runtime_error("player start location not set");
throw std::runtime_error("player start location not set");
}
}
void PlayerState::set_map_occupied_bit_for_card_on_warp_tile(shared_ptr<const Card> card) {
void PlayerState::set_map_occupied_bit_for_card_on_warp_tile(std::shared_ptr<const Card> card) {
if (!card) {
return;
}
@@ -1524,7 +1521,7 @@ bool PlayerState::subtract_or_check_atk_or_def_points_for_action(const ActionSta
void PlayerState::subtract_atk_points(uint8_t cost) {
this->atk_points -= cost;
this->atk_points2 = min<uint8_t>(this->atk_points, this->atk_points2_max);
this->atk_points2 = std::min<uint8_t>(this->atk_points, this->atk_points2_max);
}
G_UpdateHand_Ep3_6xB4x02 PlayerState::prepare_6xB4x02() const {
@@ -1571,7 +1568,7 @@ void PlayerState::set_random_assist_card_from_hand_for_free() {
auto s = this->server();
bool is_nte = s->options.is_nte();
vector<uint16_t> candidate_card_refs;
std::vector<uint16_t> candidate_card_refs;
for (size_t hand_index = 0; hand_index < 6; hand_index++) {
uint16_t card_ref = this->card_refs[hand_index];
auto ce = s->definition_for_card_ref(card_ref);
@@ -1636,11 +1633,11 @@ void PlayerState::send_6xB4x04_if_needed(bool always_send) {
}
}
vector<uint16_t> PlayerState::get_card_refs_within_range_from_all_players(
std::vector<uint16_t> PlayerState::get_card_refs_within_range_from_all_players(
const parray<uint8_t, 9 * 9>& range, const Location& loc, CardType type) const {
auto s = this->server();
vector<uint16_t> ret;
std::vector<uint16_t> ret;
for (size_t client_id = 0; client_id < 4; client_id++) {
auto other_ps = s->player_states[client_id];
if (other_ps && ((other_ps->get_sc_card_type() == type) || (type == CardType::ITEM))) {
@@ -1697,7 +1694,7 @@ void PlayerState::handle_before_turn_assist_effects() {
break;
case AssistEffect::ATK_DICE_2:
// Note: This behavior doesn't match the card description. Is it supposed to add 2 or multiply by 2?
this->atk_points = min<int16_t>(this->atk_points + 2, 9);
this->atk_points = std::min<int16_t>(this->atk_points + 2, 9);
this->update_hand_and_equip_state_and_send_6xB4x02_if_needed();
break;
case AssistEffect::SKIP_TURN:
@@ -1756,8 +1753,7 @@ bool PlayerState::set_action_cards_for_action_state(const ActionState& pa) {
size_t z = 0;
do {
if (log.should_log(phosg::LogLevel::L_DEBUG)) {
string ref_str = s->debug_str_for_card_ref(pa.action_card_refs[z]);
log.debug_f("on action card ref {}", ref_str);
log.debug_f("on action card ref {}", s->debug_str_for_card_ref(pa.action_card_refs[z]));
}
card->unknown_80237A90(pa, pa.action_card_refs[z]);
card->unknown_802379BC(pa.action_card_refs[z]);
@@ -1793,8 +1789,7 @@ bool PlayerState::set_action_cards_for_action_state(const ActionState& pa) {
auto target_card = s->card_for_set_card_ref(pa.target_card_refs[z]);
if (target_card) {
if (log.should_log(phosg::LogLevel::L_DEBUG)) {
string ref_str = s->debug_str_for_card_ref(pa.target_card_refs[z]);
log.debug_f("on target card ref {}", ref_str);
log.debug_f("on target card ref {}", s->debug_str_for_card_ref(pa.target_card_refs[z]));
}
target_card->unknown_802379DC(pa);
if (!is_nte) {
@@ -1823,8 +1818,7 @@ bool PlayerState::set_action_cards_for_action_state(const ActionState& pa) {
}
for (size_t z = 0; (z < pa.action_card_refs.size()) && (pa.action_card_refs[z] != 0xFFFF); z++) {
if (log.should_log(phosg::LogLevel::L_DEBUG)) {
string ref_str = s->debug_str_for_card_ref(pa.action_card_refs[z]);
log.debug_f("discarding {} from hand", ref_str);
log.debug_f("discarding {} from hand", s->debug_str_for_card_ref(pa.action_card_refs[z]));
}
this->discard_ref_from_hand(pa.action_card_refs[z]);
}
@@ -1866,7 +1860,7 @@ void PlayerState::dice_phase_before() {
this->send_set_card_updates();
}
void PlayerState::handle_homesick_assist_effect_from_bomb(shared_ptr<Card> card) {
void PlayerState::handle_homesick_assist_effect_from_bomb(std::shared_ptr<Card> card) {
if (!card) {
return;
}
@@ -1998,13 +1992,13 @@ void PlayerState::roll_main_dice_or_apply_after_effects() {
}
this->atk_points += s->team_dice_bonus[this->team_id];
this->def_points += s->team_dice_bonus[this->team_id];
this->atk_points = clamp<uint8_t>(this->atk_points, 1, 9);
this->def_points = clamp<uint8_t>(this->def_points, 1, 9);
this->atk_points = std::clamp<uint8_t>(this->atk_points, 1, 9);
this->def_points = std::clamp<uint8_t>(this->def_points, 1, 9);
if (!s->options.is_nte()) {
this->atk_bonuses = this->atk_points - atk_before_bonuses;
this->def_bonuses = this->def_points - def_before_bonuses;
}
this->atk_points2 = min<uint8_t>(this->atk_points2_max, this->atk_points);
this->atk_points2 = std::min<uint8_t>(this->atk_points2_max, this->atk_points);
this->update_hand_and_equip_state_and_send_6xB4x02_if_needed();
}
@@ -2036,7 +2030,7 @@ void PlayerState::compute_team_dice_bonus_after_draw_phase() {
uint8_t current_team_turn = s->get_current_team_turn();
uint8_t dice_boost = s->get_team_exp(current_team_turn) / (s->team_client_count[current_team_turn] * 12);
s->card_special->adjust_dice_boost_if_team_has_condition_52(current_team_turn, &dice_boost, 0);
s->team_dice_bonus[current_team_turn] = clamp<int16_t>(dice_boost, 0, 8);
s->team_dice_bonus[current_team_turn] = std::clamp<int16_t>(dice_boost, 0, 8);
this->update_hand_and_equip_state_and_send_6xB4x02_if_needed();
}
+16 -18
View File
@@ -2,8 +2,6 @@
#include "Server.hh"
using namespace std;
namespace Episode3 {
Condition::Condition() {
@@ -61,7 +59,7 @@ void Condition::clear_FF() {
this->unknown_a8 = 0xFF;
}
std::string Condition::str(shared_ptr<const Server> s) const {
std::string Condition::str(std::shared_ptr<const Server> s) const {
return std::format(
"Condition[type={}, turns={}, a_arg={}, dice={}, flags={:02X}, "
"def_eff_index={}, ref={}, value={}, giver_ref={} "
@@ -98,7 +96,7 @@ void EffectResult::clear() {
this->dice_roll_value = 0;
}
std::string EffectResult::str(shared_ptr<const Server> s) const {
std::string EffectResult::str(std::shared_ptr<const Server> s) const {
return std::format(
"EffectResult[att_ref={}, target_ref={}, value={}, cur_hp={}, ap={}, tp={}, flags={:02X}, op={}, cond_index={}, dice={}]",
s->debug_str_for_card_ref(this->attacker_card_ref),
@@ -130,7 +128,7 @@ bool CardShortStatus::operator!=(const CardShortStatus& other) const {
return !this->operator==(other);
}
std::string CardShortStatus::str(shared_ptr<const Server> s) const {
std::string CardShortStatus::str(std::shared_ptr<const Server> s) const {
return std::format(
"CardShortStatus[ref={}, cur_hp={}, flags={:08X}, loc={}, u1={:04X}, max_hp={}, u2={}]",
s->debug_str_for_card_ref(this->card_ref),
@@ -178,7 +176,7 @@ void ActionState::clear() {
this->unused2 = 0xFFFF;
}
std::string ActionState::str(shared_ptr<const Server> s) const {
std::string ActionState::str(std::shared_ptr<const Server> s) const {
return std::format(
"ActionState[client={:X}, u={}, facing={}, attacker_ref={}, def_ref={}, target_refs={}, action_refs={}, orig_attacker_ref={}]",
this->client_id,
@@ -222,7 +220,7 @@ bool ActionChain::operator!=(const ActionChain& other) const {
return !this->operator==(other);
}
std::string ActionChain::str(shared_ptr<const Server> s) const {
std::string ActionChain::str(std::shared_ptr<const Server> s) const {
return std::format(
"ActionChain[eff_ap={}, eff_tp={}, ap_bonus={}, damage={}, acting_ref={}, unknown_ref_a3={}, attack_action_refs={}, "
"attack_action_ref_count={}, medium={}, target_ref_count={}, subphase={}, strikes={}, damage_mult={}, attack_num={}, "
@@ -309,8 +307,8 @@ bool ActionChainWithConds::operator!=(const ActionChainWithConds& other) const {
return !this->operator==(other);
}
std::string ActionChainWithConds::str(shared_ptr<const Server> s) const {
string ret = "ActionChainWithConds[chain=";
std::string ActionChainWithConds::str(std::shared_ptr<const Server> s) const {
std::string ret = "ActionChainWithConds[chain=";
ret += this->chain.str(s);
ret += ", conds=[";
for (size_t z = 0; z < this->conditions.size(); z++) {
@@ -383,7 +381,7 @@ void ActionChainWithConds::set_flags(uint32_t flags) {
this->chain.flags |= flags;
}
void ActionChainWithConds::add_attack_action_card_ref(uint16_t card_ref, shared_ptr<Server> server) {
void ActionChainWithConds::add_attack_action_card_ref(uint16_t card_ref, std::shared_ptr<Server> server) {
if (card_ref != 0xFFFF) {
this->chain.attack_action_card_refs[this->chain.attack_action_card_ref_count++] = card_ref;
}
@@ -397,7 +395,7 @@ void ActionChainWithConds::add_target_card_ref(uint16_t card_ref) {
}
}
void ActionChainWithConds::compute_attack_medium(shared_ptr<Server> server) {
void ActionChainWithConds::compute_attack_medium(std::shared_ptr<Server> server) {
this->chain.attack_medium = AttackMedium::PHYSICAL;
for (size_t z = 0; z < this->chain.attack_action_card_ref_count; z++) {
uint16_t card_ref = this->chain.attack_action_card_refs[z];
@@ -437,7 +435,7 @@ bool ActionChainWithConds::get_condition_value(
return any_found;
}
void ActionChainWithConds::set_action_subphase_from_card(shared_ptr<const Card> card) {
void ActionChainWithConds::set_action_subphase_from_card(std::shared_ptr<const Card> card) {
this->chain.action_subphase = card->server()->get_current_action_subphase();
}
@@ -545,7 +543,7 @@ bool ActionMetadata::operator!=(const ActionMetadata& other) const {
return !this->operator==(other);
}
std::string ActionMetadata::str(shared_ptr<const Server> s) const {
std::string ActionMetadata::str(std::shared_ptr<const Server> s) const {
return std::format(
"ActionMetadata[ref={}, target_ref_count={}, def_ref_count={}, subphase={}, def_power={}, def_bonus={}, "
"att_bonus={}, flags={:08X}, target_refs={}, defense_refs={}, original_attacker_refs={}]",
@@ -621,7 +619,7 @@ void ActionMetadata::add_target_card_ref(uint16_t card_ref) {
}
void ActionMetadata::add_defense_card_ref(
uint16_t defense_card_ref, shared_ptr<Card> card, uint16_t original_attacker_card_ref) {
uint16_t defense_card_ref, std::shared_ptr<Card> card, uint16_t original_attacker_card_ref) {
if ((defense_card_ref != 0xFFFF) && (this->defense_card_ref_count < 8)) {
this->defense_card_refs[this->defense_card_ref_count] = defense_card_ref;
this->original_attacker_card_refs[this->defense_card_ref_count] = original_attacker_card_ref;
@@ -634,7 +632,7 @@ HandAndEquipState::HandAndEquipState() {
this->clear();
}
std::string HandAndEquipState::str(shared_ptr<const Server> s) const {
std::string HandAndEquipState::str(std::shared_ptr<const Server> s) const {
return std::format(
"HandAndEquipState[dice=[{}, {}], atk={}, def={}, atk2={}, a1={}, total_set_cost={}, is_cpu={}, assist_flags={:08X}, "
"hand_refs={}, assist_ref={}, set_refs={}, sc_ref={}, hand_refs2={}, set_refs2={}, assist_ref2={}, assist_set_num={}, assist_card_id={}, "
@@ -777,7 +775,7 @@ uint8_t PlayerBattleStats::rank_for_score(float score) {
const char* PlayerBattleStats::name_for_rank(uint8_t rank) {
if (rank >= RANK_THRESHOLD_COUNT + 1) {
throw invalid_argument("invalid rank");
throw std::invalid_argument("invalid rank");
}
return RANK_NAMES[rank];
}
@@ -842,12 +840,12 @@ static bool is_card_within_range(
return ret;
}
vector<uint16_t> get_card_refs_within_range(
std::vector<uint16_t> get_card_refs_within_range(
const parray<uint8_t, 9 * 9>& range,
const Location& loc,
const parray<CardShortStatus, 0x10>& short_statuses,
phosg::PrefixedLogger* log) {
vector<uint16_t> ret;
std::vector<uint16_t> ret;
if (is_card_within_range(range, loc, short_statuses[0], log)) {
if (log) {
log->debug_f("get_card_refs_within_range: sc card @{:04X} within range", short_statuses[0].card_ref);
+57 -64
View File
@@ -5,20 +5,17 @@
#include "DataIndexes.hh"
#include "Server.hh"
using namespace std;
namespace Episode3 {
void compute_effective_range(
parray<uint8_t, 9 * 9>& ret,
shared_ptr<const CardIndex> card_index,
std::shared_ptr<const CardIndex> card_index,
uint16_t card_id,
const Location& loc,
shared_ptr<const MapAndRulesState> map_and_rules,
std::shared_ptr<const MapAndRulesState> map_and_rules,
phosg::PrefixedLogger* log) {
if (log && log->should_log(phosg::LogLevel::L_DEBUG)) {
string loc_str = loc.str();
log->debug_f("compute_effective_range: card_id=#{:04X}, loc={}", card_id, loc_str);
log->debug_f("compute_effective_range: card_id=#{:04X}, loc={}", card_id, loc.str());
log->debug_f("compute_effective_range: map_and_rules->map:");
map_and_rules->map.print(stderr);
}
@@ -28,10 +25,10 @@ void compute_effective_range(
if (card_id == 0xFFFE) { // Heavy Fog: one tile directly in front
range_def[3] = 0x00000100;
} else {
shared_ptr<const CardIndex::CardEntry> ce;
std::shared_ptr<const CardIndex::CardEntry> ce;
try {
ce = card_index->definition_for_id(card_id);
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
return;
}
for (size_t z = 0; z < 6; z++) {
@@ -95,7 +92,7 @@ void compute_effective_range(
up_y = 9 - y - 1;
break;
default:
throw logic_error("invalid direction");
throw std::logic_error("invalid direction");
}
ret[y * 9 + x] = decoded_range[up_y * 9 + up_x];
if (log) {
@@ -117,9 +114,9 @@ void compute_effective_range(
}
bool card_linkage_is_valid(
shared_ptr<const CardIndex::CardEntry> right_ce,
shared_ptr<const CardIndex::CardEntry> left_ce,
shared_ptr<const CardIndex::CardEntry> sc_ce,
std::shared_ptr<const CardIndex::CardEntry> right_ce,
std::shared_ptr<const CardIndex::CardEntry> left_ce,
std::shared_ptr<const CardIndex::CardEntry> sc_ce,
bool has_permission_effect) {
if (!right_ce) {
return false;
@@ -173,31 +170,27 @@ bool card_linkage_is_valid(
return false;
}
RulerServer::RulerServer(shared_ptr<Server> server)
: w_server(server),
team_id_for_client_id(0xFF),
error_code1(0),
error_code2(0),
error_code3(0) {}
RulerServer::RulerServer(std::shared_ptr<Server> server)
: w_server(server), team_id_for_client_id(0xFF), error_code1(0), error_code2(0), error_code3(0) {}
shared_ptr<Server> RulerServer::server() {
std::shared_ptr<Server> RulerServer::server() {
auto s = this->w_server.lock();
if (!s) {
throw runtime_error("server is deleted");
throw std::runtime_error("server is deleted");
}
return s;
}
shared_ptr<const Server> RulerServer::server() const {
std::shared_ptr<const Server> RulerServer::server() const {
auto s = this->w_server.lock();
if (!s) {
throw runtime_error("server is deleted");
throw std::runtime_error("server is deleted");
}
return s;
}
ActionChainWithConds* RulerServer::action_chain_with_conds_for_card_ref(uint16_t card_ref) {
return const_cast<ActionChainWithConds*>(as_const(*this).action_chain_with_conds_for_card_ref(card_ref));
return const_cast<ActionChainWithConds*>(std::as_const(*this).action_chain_with_conds_for_card_ref(card_ref));
}
const ActionChainWithConds* RulerServer::action_chain_with_conds_for_card_ref(uint16_t card_ref) const {
@@ -398,7 +391,7 @@ bool RulerServer::attack_action_has_pierce_and_not_rampage(const ActionState& pa
last_action_card_index = z;
}
auto check_chain = [&]() -> optional<bool> {
auto check_chain = [&]() -> std::optional<bool> {
const auto* chain = this->action_chain_with_conds_for_card_ref(pa.attacker_card_ref);
if (chain) {
for (ssize_t cond_index = 8; cond_index >= 0; cond_index--) {
@@ -415,7 +408,7 @@ bool RulerServer::attack_action_has_pierce_and_not_rampage(const ActionState& pa
}
}
}
return nullopt;
return std::nullopt;
};
if (is_nte) {
@@ -767,7 +760,7 @@ bool RulerServer::check_move_path_and_get_cost(
if (max_dist < 1) {
return false;
}
max_dist = min<uint8_t>(max_dist, 9);
max_dist = std::min<uint8_t>(max_dist, 9);
const auto* short_status = this->short_status_for_card_ref(card_ref);
if (!short_status) {
@@ -1030,7 +1023,7 @@ bool RulerServer::check_usability_or_condition_apply(
}
break;
case CriterionCode::HUNTER_NON_ANDROID_SC: {
static const unordered_set<uint16_t> card_ids = {
static const std::unordered_set<uint16_t> card_ids = {
0x0001, // Orland
0x0002, // Kranz
0x0003, // Ino'lis
@@ -1059,7 +1052,7 @@ bool RulerServer::check_usability_or_condition_apply(
return ret && card_ids.count(card_id2);
}
case CriterionCode::HUNTER_HU_CLASS_MALE_SC: {
static const unordered_set<uint16_t> card_ids = {
static const std::unordered_set<uint16_t> card_ids = {
0x0001, // Orland
0x0113, // Teifu
0x02AA, // H-HUmar
@@ -1070,7 +1063,7 @@ bool RulerServer::check_usability_or_condition_apply(
return ret && card_ids.count(card_id2);
}
case CriterionCode::HUNTER_FEMALE_SC: {
static const unordered_set<uint16_t> card_ids = {
static const std::unordered_set<uint16_t> card_ids = {
0x0003, // Ino'lis
0x0004, // Sil'fer
0x0006, // Kylria
@@ -1094,7 +1087,7 @@ bool RulerServer::check_usability_or_condition_apply(
return ret && card_ids.count(card_id2);
}
case CriterionCode::HUNTER_NON_RA_CLASS_HUMAN_SC: {
static const unordered_set<uint16_t> card_ids = {
static const std::unordered_set<uint16_t> card_ids = {
0x0001, // Orland
0x0003, // Ino'lis
0x0004, // Sil'fer
@@ -1117,7 +1110,7 @@ bool RulerServer::check_usability_or_condition_apply(
return ret && card_ids.count(card_id2);
}
case CriterionCode::HUNTER_HU_CLASS_ANDROID_SC: {
static const unordered_set<uint16_t> card_ids = {
static const std::unordered_set<uint16_t> card_ids = {
0x0110, // Saligun
0x0113, // Teifu
0x02AC, // H-HUcast
@@ -1128,7 +1121,7 @@ bool RulerServer::check_usability_or_condition_apply(
return ret && card_ids.count(card_id2);
}
case CriterionCode::HUNTER_NON_RA_CLASS_NON_NEWMAN_SC: {
static const unordered_set<uint16_t> card_ids = {
static const std::unordered_set<uint16_t> card_ids = {
0x0001, // Orland
0x0003, // Ino'lis
0x0110, // Saligun
@@ -1148,7 +1141,7 @@ bool RulerServer::check_usability_or_condition_apply(
return ret && card_ids.count(card_id2);
}
case CriterionCode::HUNTER_NON_NEWMAN_NON_FORCE_MALE_SC: {
static const unordered_set<uint16_t> card_ids = {
static const std::unordered_set<uint16_t> card_ids = {
0x0001, // Orland
0x0002, // Kranz
0x0005, // Guykild
@@ -1166,7 +1159,7 @@ bool RulerServer::check_usability_or_condition_apply(
return ret && card_ids.count(card_id2);
}
case CriterionCode::HUNTER_HUNEWEARL_CLASS_SC: {
static const unordered_set<uint16_t> card_ids = {
static const std::unordered_set<uint16_t> card_ids = {
0x0004, // Sil'fer
0x02AB, // H-HUnewearl
0x02CF, // H-HUnewearl
@@ -1174,7 +1167,7 @@ bool RulerServer::check_usability_or_condition_apply(
return ret && card_ids.count(card_id2);
}
case CriterionCode::HUNTER_RA_CLASS_MALE_SC: {
static const unordered_set<uint16_t> card_ids = {
static const std::unordered_set<uint16_t> card_ids = {
0x0002, // Kranz
0x0005, // Guykild
0x02AE, // H-RAmar
@@ -1185,7 +1178,7 @@ bool RulerServer::check_usability_or_condition_apply(
return ret && card_ids.count(card_id2);
}
case CriterionCode::HUNTER_RA_CLASS_FEMALE_SC: {
static const unordered_set<uint16_t> card_ids = {
static const std::unordered_set<uint16_t> card_ids = {
0x0006, // Kylria
0x0114, // Stella
0x02AF, // H-RAmarl
@@ -1196,7 +1189,7 @@ bool RulerServer::check_usability_or_condition_apply(
return ret && card_ids.count(card_id2);
}
case CriterionCode::HUNTER_RA_OR_FO_CLASS_FEMALE_SC: {
static const unordered_set<uint16_t> card_ids = {
static const std::unordered_set<uint16_t> card_ids = {
0x0003, // Ino'lis
0x0006, // Kylria
0x0112, // Viviana
@@ -1213,7 +1206,7 @@ bool RulerServer::check_usability_or_condition_apply(
return ret && card_ids.count(card_id2);
}
case CriterionCode::HUNTER_HU_OR_RA_CLASS_HUMAN_SC: {
static const unordered_set<uint16_t> card_ids = {
static const std::unordered_set<uint16_t> card_ids = {
0x0001, // Orland
0x0002, // Kranz
0x0004, // Sil'fer
@@ -1230,7 +1223,7 @@ bool RulerServer::check_usability_or_condition_apply(
return ret && card_ids.count(card_id2);
}
case CriterionCode::HUNTER_RA_CLASS_ANDROID_SC: {
static const unordered_set<uint16_t> card_ids = {
static const std::unordered_set<uint16_t> card_ids = {
0x0005, // Guykild
0x0114, // Stella
0x02B0, // H-RAcast
@@ -1241,7 +1234,7 @@ bool RulerServer::check_usability_or_condition_apply(
return ret && card_ids.count(card_id2);
}
case CriterionCode::HUNTER_FO_CLASS_FEMALE_SC: {
static const unordered_set<uint16_t> card_ids = {
static const std::unordered_set<uint16_t> card_ids = {
0x0003, // Ino'lis
0x0112, // Viviana
0x02B3, // H-FOmarl
@@ -1252,7 +1245,7 @@ bool RulerServer::check_usability_or_condition_apply(
return ret && card_ids.count(card_id2);
}
case CriterionCode::HUNTER_HUMAN_FEMALE_SC: {
static const unordered_set<uint16_t> card_ids = {
static const std::unordered_set<uint16_t> card_ids = {
0x0003, // Ino'lis
0x0004, // Sil'fer
0x0006, // Kylria
@@ -1269,7 +1262,7 @@ bool RulerServer::check_usability_or_condition_apply(
return ret && card_ids.count(card_id2);
}
case CriterionCode::HUNTER_ANDROID_SC: {
static const unordered_set<uint16_t> card_ids = {
static const std::unordered_set<uint16_t> card_ids = {
0x0005, // Guykild
0x0110, // Saligun
0x0113, // Teifu
@@ -1433,14 +1426,14 @@ uint16_t RulerServer::compute_attack_or_defense_costs(
final_cost = 0;
}
} else {
final_cost = max<int16_t>(final_cost, this->hand_and_equip_states[pa.client_id]->atk_points);
final_cost = std::max<int16_t>(final_cost, this->hand_and_equip_states[pa.client_id]->atk_points);
}
}
if (out_ally_cost) {
*out_ally_cost = total_ally_cost;
}
return max<int16_t>(final_cost, total_cost + assist_cost_bias);
return std::max<int16_t>(final_cost, total_cost + assist_cost_bias);
}
bool RulerServer::compute_effective_range_and_target_mode_for_attack(
@@ -1627,7 +1620,7 @@ bool RulerServer::defense_card_can_apply_to_attack(
bool RulerServer::defense_card_matches_any_attack_card_top_color(const ActionState& pa) const {
auto ce = this->definition_for_card_ref(pa.action_card_refs[0]);
if (!ce) {
throw runtime_error("defense card definition is missing");
throw std::runtime_error("defense card definition is missing");
}
const auto* chain = this->action_chain_with_conds_for_card_ref(pa.original_attacker_card_ref);
if (chain->chain.attack_action_card_ref_count < 1) {
@@ -1646,7 +1639,7 @@ bool RulerServer::defense_card_matches_any_attack_card_top_color(const ActionSta
return false;
}
shared_ptr<const CardIndex::CardEntry> RulerServer::definition_for_card_ref(uint16_t card_ref) const {
std::shared_ptr<const CardIndex::CardEntry> RulerServer::definition_for_card_ref(uint16_t card_ref) const {
uint16_t card_id = this->card_id_for_card_ref(card_ref);
if (card_id == 0xFFFF) {
return nullptr;
@@ -1841,7 +1834,7 @@ int32_t RulerServer::error_code_for_client_setting_card(
return -0x7E;
}
} else {
int16_t diff = max<int16_t>(summon_area_size - summon_cost, 0);
int16_t diff = std::max<int16_t>(summon_area_size - summon_cost, 0);
if (x_offset > 0) {
if (loc->x < summon_area_loc.x) {
return -0x7E;
@@ -1860,7 +1853,7 @@ int32_t RulerServer::error_code_for_client_setting_card(
return -0x7E;
}
} else {
int16_t diff = max<int16_t>(summon_area_size - summon_cost, 0);
int16_t diff = std::max<int16_t>(summon_area_size - summon_cost, 0);
if (y_offset > 0) {
if (loc->y < summon_area_loc.y) {
return -0x7E;
@@ -1974,7 +1967,7 @@ bool RulerServer::flood_fill_move_path(
Direction dirs[3] = {direction, turn_left(direction), turn_right(direction)};
for (size_t dir_index = 0; dir_index < 3; dir_index++) {
if (static_cast<uint8_t>(dirs[dir_index]) > 3) {
throw logic_error("invalid direction");
throw std::logic_error("invalid direction");
}
ret |= this->flood_fill_move_path(
chain,
@@ -2017,7 +2010,7 @@ uint16_t RulerServer::get_ally_sc_card_ref(uint16_t card_ref) const {
return 0xFFFF;
}
shared_ptr<const CardIndex::CardEntry> RulerServer::definition_for_card_id(uint32_t card_id) const {
std::shared_ptr<const CardIndex::CardEntry> RulerServer::definition_for_card_id(uint32_t card_id) const {
return this->server()->definition_for_card_id(card_id);
}
@@ -2130,11 +2123,11 @@ bool RulerServer::get_creature_summon_area(uint8_t client_id, Location* out_loc,
return true;
}
shared_ptr<HandAndEquipState> RulerServer::get_hand_and_equip_state_for_client_id(uint8_t client_id) {
std::shared_ptr<HandAndEquipState> RulerServer::get_hand_and_equip_state_for_client_id(uint8_t client_id) {
return (client_id < 4) ? this->hand_and_equip_states[client_id] : nullptr;
}
shared_ptr<const HandAndEquipState> RulerServer::get_hand_and_equip_state_for_client_id(uint8_t client_id) const {
std::shared_ptr<const HandAndEquipState> RulerServer::get_hand_and_equip_state_for_client_id(uint8_t client_id) const {
return (client_id < 4) ? this->hand_and_equip_states[client_id] : nullptr;
}
@@ -2171,7 +2164,7 @@ ssize_t RulerServer::get_path_cost(
path_length *= cond.value;
}
}
return clamp<ssize_t>(path_length + cost_penalty, 0, 99);
return std::clamp<ssize_t>(path_length + cost_penalty, 0, 99);
}
ActionType RulerServer::get_pending_action_type(const ActionState& pa) const {
@@ -2440,9 +2433,9 @@ bool RulerServer::is_defense_valid(const ActionState& pa) {
}
void RulerServer::link_objects(
shared_ptr<MapAndRulesState> map_and_rules,
shared_ptr<StateFlags> state_flags,
shared_ptr<AssistServer> assist_server) {
std::shared_ptr<MapAndRulesState> map_and_rules,
std::shared_ptr<StateFlags> state_flags,
std::shared_ptr<AssistServer> assist_server) {
this->map_and_rules = map_and_rules;
this->state_flags = state_flags;
this->assist_server = assist_server;
@@ -2491,7 +2484,7 @@ size_t RulerServer::max_move_distance_for_card_ref(uint32_t card_ref) const {
if (this->find_condition_on_card_ref(card_ref, ConditionType::SET_MV, &cond, nullptr, true)) {
ret = cond.value;
}
ret = max<ssize_t>(0, ret);
ret = std::max<ssize_t>(0, ret);
size_t num_assists = this->assist_server->compute_num_assist_effects_for_client(client_id);
bool has_stamina_effect = false;
@@ -2505,7 +2498,7 @@ size_t RulerServer::max_move_distance_for_card_ref(uint32_t card_ref) const {
}
}
return has_stamina_effect ? 9 : min<ssize_t>(9, ret);
return has_stamina_effect ? 9 : std::min<ssize_t>(9, ret);
}
}
@@ -2566,11 +2559,11 @@ void RulerServer::offsets_for_direction(
void RulerServer::register_player(
uint8_t client_id,
shared_ptr<HandAndEquipState> hes,
shared_ptr<parray<CardShortStatus, 0x10>> short_statuses,
shared_ptr<DeckEntry> deck_entry,
shared_ptr<parray<ActionChainWithConds, 9>> set_card_action_chains,
shared_ptr<parray<ActionMetadata, 9>> set_card_action_metadatas) {
std::shared_ptr<HandAndEquipState> hes,
std::shared_ptr<parray<CardShortStatus, 0x10>> short_statuses,
std::shared_ptr<DeckEntry> deck_entry,
std::shared_ptr<parray<ActionChainWithConds, 9>> set_card_action_chains,
std::shared_ptr<parray<ActionMetadata, 9>> set_card_action_metadatas) {
this->hand_and_equip_states[client_id] = hes;
this->short_statuses[client_id] = short_statuses;
this->deck_entries[client_id] = deck_entry;
@@ -2656,7 +2649,7 @@ int32_t RulerServer::set_cost_for_card(uint8_t client_id, uint16_t card_ref) con
// In NTE, Land Price is apparently 2x rather than 1.5x
ret = is_nte ? (ret << 1) : (ret + (ret >> 1));
} else if (eff == AssistEffect::DEFLATION) {
ret = max<int32_t>(0, ret - 1);
ret = std::max<int32_t>(0, ret - 1);
} else if (eff == AssistEffect::INFLATION) {
ret++;
}
+119 -121
View File
@@ -7,11 +7,9 @@
#include "../Revision.hh"
#include "../SendCommands.hh"
using namespace std;
namespace Episode3 {
// This is (obviously) not the original string. The original string is:
// This is (obviously) not the original std::string. The original std::string is:
// NTE: "03/05/29 18:00 by K.Toya"
// Final: "[V1][FINAL2.0] 03/09/13 15:30 by K.Toya"
static const char* VERSION_SIGNATURE = "newserv Ep3 based on [V1][FINAL2.0] 03/09/13 15:30 by K.Toya";
@@ -27,7 +25,7 @@ void Server::PresenceEntry::clear() {
this->is_cpu_player = 0;
}
Server::Server(shared_ptr<Lobby> lobby, Options&& options)
Server::Server(std::shared_ptr<Lobby> lobby, Options&& options)
: lobby(lobby),
battle_record(lobby ? lobby->battle_record : nullptr),
has_lobby(lobby != nullptr),
@@ -81,7 +79,7 @@ Server::Server(shared_ptr<Lobby> lobby, Options&& options)
Server::~Server() noexcept(false) {
if (this->logger_stack.size() != 1) {
throw logic_error(std::format("incorrect logger stack size: expected 1, received {}", this->logger_stack.size()));
throw std::logic_error(std::format("incorrect logger stack size: expected 1, received {}", this->logger_stack.size()));
}
delete this->logger_stack.back();
}
@@ -89,31 +87,31 @@ Server::~Server() noexcept(false) {
void Server::init() {
this->log().info_f("Creating server with random seed {:08X}", this->options.rand_crypt->seed());
this->map_and_rules = make_shared<MapAndRulesState>();
this->map_and_rules = std::make_shared<MapAndRulesState>();
this->num_clients_present = 0;
this->overlay_state.clear();
for (size_t z = 0; z < 4; z++) {
this->presence_entries[z].clear();
this->deck_entries[z] = make_shared<DeckEntry>();
this->deck_entries[z] = std::make_shared<DeckEntry>();
this->name_entries[z].clear();
this->name_entries_valid[z] = false;
}
this->card_special = make_shared<CardSpecial>(this->shared_from_this());
this->card_special = std::make_shared<CardSpecial>(this->shared_from_this());
// Note: The original implementation calls the default PSOV2Encryption constructor for random_crypt, which just uses
// 0 as the seed. It then re-seeds the generator later. We instead expect the caller to provide a seeded generator,
// and we don't re-seed it at all.
// this->random_crypt = make_shared<PSOV2Encryption>(0);
// this->random_crypt = std::make_shared<PSOV2Encryption>(0);
this->state_flags = make_shared<StateFlags>();
this->state_flags = std::make_shared<StateFlags>();
this->clear_player_flags_after_dice_phase();
this->update_battle_state_flags_and_send_6xB4x03_if_needed();
this->assist_server = make_shared<AssistServer>(this->shared_from_this());
this->ruler_server = make_shared<RulerServer>(this->shared_from_this());
this->assist_server = std::make_shared<AssistServer>(this->shared_from_this());
this->ruler_server = std::make_shared<RulerServer>(this->shared_from_this());
this->ruler_server->link_objects(this->map_and_rules, this->state_flags, this->assist_server);
this->send_6xB4x46();
@@ -135,7 +133,7 @@ Server::StackLogger::StackLogger(StackLogger&& other)
: PrefixedLogger(std::move(other)),
server(other.server) {
if (this->server->logger_stack.back() != &other) {
throw logic_error("cannot move StackLogger unless it is the last one");
throw std::logic_error("cannot move StackLogger unless it is the last one");
}
this->server->logger_stack.back() = this;
}
@@ -144,7 +142,7 @@ Server::StackLogger& Server::StackLogger::operator=(StackLogger&& other) {
this->PrefixedLogger::operator=(std::move(other));
this->server = other.server;
if (this->server->logger_stack.back() != &other) {
throw logic_error("cannot move StackLogger unless it is the last one");
throw std::logic_error("cannot move StackLogger unless it is the last one");
}
this->server->logger_stack.back() = this;
return *this;
@@ -152,7 +150,7 @@ Server::StackLogger& Server::StackLogger::operator=(StackLogger&& other) {
Server::StackLogger::~StackLogger() noexcept(false) {
if (this->server->logger_stack.back() != this) {
throw logic_error("incorrect logger stack unwind order");
throw std::logic_error("incorrect logger stack unwind order");
}
this->server->logger_stack.pop_back();
}
@@ -171,8 +169,7 @@ std::string Server::debug_str_for_card_ref(uint16_t card_ref) const {
}
auto ce = this->definition_for_card_ref(card_ref);
if (ce) {
string name = ce->def.en_name.decode();
return std::format("@{:04X} (#{:04X} {})", card_ref, ce->def.card_id, name);
return std::format("@{:04X} (#{:04X} {})", card_ref, ce->def.card_id, ce->def.en_name.decode());
} else {
return std::format("@{:04X} (missing)", card_ref);
}
@@ -184,8 +181,7 @@ std::string Server::debug_str_for_card_id(uint16_t card_id) const {
}
auto ce = this->definition_for_card_id(card_id);
if (ce) {
string name = ce->def.en_name.decode();
return std::format("#{:04X} ({})", card_id, name);
return std::format("#{:04X} ({})", card_id, ce->def.en_name.decode());
} else {
return std::format("#{:04X} (missing)", card_id);
}
@@ -209,17 +205,17 @@ int8_t Server::get_winner_team_id() const {
}
if (!team_player_counts[0] || !team_player_counts[1]) {
throw logic_error("at least one team has no players");
throw std::logic_error("at least one team has no players");
}
if (team_win_flag_counts[0] && team_win_flag_counts[1]) {
throw logic_error("both teams have winning players");
throw std::logic_error("both teams have winning players");
}
for (int8_t z = 0; z < 2; z++) {
if (!team_win_flag_counts[z]) {
continue;
}
if (team_win_flag_counts[z] != team_player_counts[z]) {
throw logic_error("only some players on team have won");
throw std::logic_error("only some players on team have won");
}
return z;
}
@@ -236,10 +232,10 @@ void Server::send(const void* data, size_t size, uint8_t command, bool enable_ma
if (this->has_lobby) {
auto l = this->lobby.lock();
if (!l) {
throw runtime_error("lobby is deleted");
throw std::runtime_error("lobby is deleted");
}
string masked_data;
std::string masked_data;
if (enable_masking &&
!this->options.is_nte() &&
!(this->options.behavior_flags & BehaviorFlag::DISABLE_MASKING) &&
@@ -261,13 +257,13 @@ void Server::send(const void* data, size_t size, uint8_t command, bool enable_ma
} else if ((this->options.behavior_flags & BehaviorFlag::LOG_COMMANDS_IF_LOBBY_MISSING) &&
this->log().info_f("Generated command")) {
phosg::print_data(stderr, data, size, 0, nullptr, phosg::PrintDataFlags::PRINT_ASCII | phosg::PrintDataFlags::DISABLE_COLOR | phosg::PrintDataFlags::OFFSET_16_BITS);
phosg::print_data(stderr, data, size, 0, phosg::FormatDataFlags::PRINT_ASCII | phosg::FormatDataFlags::OFFSET_16_BITS);
}
}
void Server::send_6xB4x46() const {
// Note: This function is not part of the original implementation; it was factored out from its callsites in this
// file and the strings were changed.
// file and the std::strings were changed.
// NTE doesn't have the date_str2 field, but we send it anyway to make debugging easier.
G_ServerVersionStrings_Ep3_6xB4x46 cmd;
@@ -275,12 +271,13 @@ void Server::send_6xB4x46() const {
cmd.date_str1.encode(
std::format("Card definitions: {:016X}", this->options.card_index->definitions_hash()),
Language::ENGLISH);
string build_date = phosg::format_time(BUILD_TIMESTAMP);
std::string build_date = phosg::format_time(BUILD_TIMESTAMP);
cmd.date_str2.encode(std::format("newserv {} compiled at {}", GIT_REVISION_HASH, build_date), Language::ENGLISH);
this->send(cmd);
}
string Server::prepare_6xB6x41_map_definition(shared_ptr<const MapIndex::Map> map, Language language, bool is_nte) {
std::string Server::prepare_6xB6x41_map_definition(
std::shared_ptr<const MapIndex::Map> map, Language language, bool is_nte) {
auto vm = map->version(language);
const auto& compressed = vm->compressed(is_nte);
@@ -295,7 +292,8 @@ string Server::prepare_6xB6x41_map_definition(shared_ptr<const MapIndex::Map> ma
void Server::send_commands_for_joining_spectator(std::shared_ptr<Channel> ch) const {
if (this->last_chosen_map) {
string data = this->prepare_6xB6x41_map_definition(this->last_chosen_map, ch->language, this->options.is_nte());
std::string data = this->prepare_6xB6x41_map_definition(
this->last_chosen_map, ch->language, this->options.is_nte());
this->log().info_f(
"Sending {} version of map {:08X}", name_for_language(ch->language), this->last_chosen_map->map_number);
ch->send(0x6C, 0x00, data);
@@ -366,11 +364,12 @@ void Server::add_team_exp(uint8_t team_id, int32_t exp) {
}
}
this->team_exp[team_id] = clamp<int16_t>(this->team_exp[team_id] + exp, 0, this->team_client_count[team_id] * 96);
this->team_exp[team_id] = std::clamp<int16_t>(
this->team_exp[team_id] + exp, 0, this->team_client_count[team_id] * 96);
uint8_t dice_boost = this->team_exp[team_id] / (this->team_client_count[team_id] * 12);
this->card_special->adjust_dice_boost_if_team_has_condition_52(team_id, &dice_boost, 0);
this->team_dice_bonus[team_id] = min<uint8_t>(dice_boost, 8);
this->team_dice_bonus[team_id] = std::min<uint8_t>(dice_boost, 8);
}
bool Server::advance_battle_phase() {
@@ -396,7 +395,7 @@ bool Server::advance_battle_phase() {
this->dice_phase_before();
break;
default:
throw logic_error("invalid battle phase");
throw std::logic_error("invalid battle phase");
}
return this->check_for_battle_end();
}
@@ -414,15 +413,15 @@ void Server::draw_phase_before() {
}
}
shared_ptr<const CardIndex::CardEntry> Server::definition_for_card_ref(uint16_t card_ref) const {
std::shared_ptr<const CardIndex::CardEntry> Server::definition_for_card_ref(uint16_t card_ref) const {
try {
return this->options.card_index->definition_for_id(this->card_id_for_card_ref(card_ref));
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
return nullptr;
}
}
shared_ptr<Card> Server::card_for_set_card_ref(uint16_t card_ref) {
std::shared_ptr<Card> Server::card_for_set_card_ref(uint16_t card_ref) {
if (card_ref == 0xFFFF) {
return nullptr;
}
@@ -447,7 +446,7 @@ shared_ptr<Card> Server::card_for_set_card_ref(uint16_t card_ref) {
return nullptr;
}
shared_ptr<const Card> Server::card_for_set_card_ref(uint16_t card_ref) const {
std::shared_ptr<const Card> Server::card_for_set_card_ref(uint16_t card_ref) const {
return const_cast<Server*>(this)->card_for_set_card_ref(card_ref);
}
@@ -584,7 +583,7 @@ bool Server::check_for_battle_end() {
void Server::force_replace_assist_card(uint8_t client_id, uint16_t card_id) {
auto ps = this->player_states.at(client_id);
if (!ps) {
throw runtime_error("player does not exist");
throw std::runtime_error("player does not exist");
}
if (card_id == 0xFFFF) {
ps->discard_set_assist_card();
@@ -599,12 +598,12 @@ void Server::force_replace_assist_card(uint8_t client_id, uint16_t card_id) {
void Server::force_destroy_field_character(uint8_t client_id, size_t visible_index) {
auto ps = this->player_states.at(client_id);
if (!ps) {
throw runtime_error("player does not exist");
throw std::runtime_error("player does not exist");
}
// TODO: Is it possible for there to be gaps in the set cards array? If not, we could just do a direct array lookup
// here instead of this loop
shared_ptr<Card> set_card = nullptr;
std::shared_ptr<Card> set_card = nullptr;
for (size_t set_index = 0; set_index < 8; set_index++) {
if (!ps->set_cards[set_index]) {
continue;
@@ -681,7 +680,7 @@ void Server::compute_all_map_occupied_bits() {
}
void Server::compute_team_dice_bonus(uint8_t team_id) {
this->team_dice_bonus[team_id] = clamp<int16_t>(
this->team_dice_bonus[team_id] = std::clamp<int16_t>(
this->team_exp[team_id] / (this->team_client_count[team_id] * 12), 0, 8);
}
@@ -700,10 +699,10 @@ void Server::copy_player_states_to_prev_states() {
}
}
shared_ptr<const CardIndex::CardEntry> Server::definition_for_card_id(uint16_t card_id) const {
std::shared_ptr<const CardIndex::CardEntry> Server::definition_for_card_id(uint16_t card_id) const {
try {
return this->options.card_index->definition_for_id(card_id);
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
return nullptr;
}
}
@@ -731,7 +730,7 @@ void Server::determine_first_team_turn() {
this->team_client_count[0] = this->map_and_rules->num_team0_players;
this->team_client_count[1] = this->map_and_rules->num_players - this->team_client_count[0];
if (this->team_client_count[0] == 0 || this->team_client_count[1] == 0) {
throw runtime_error("one or both teams have no players");
throw std::runtime_error("one or both teams have no players");
}
this->first_team_turn = 0xFF;
while (this->first_team_turn == 0xFF) {
@@ -927,7 +926,7 @@ void Server::end_action_phase() {
bool Server::enqueue_attack_or_defense(uint8_t client_id, ActionState* pa) {
auto log = this->log_stack("enqueue_attack_or_defense: ");
if (log.should_log(phosg::LogLevel::L_DEBUG)) {
string s = pa->str(this->shared_from_this());
std::string s = pa->str(this->shared_from_this());
log.debug_f("input: {}", s);
}
@@ -977,8 +976,8 @@ bool Server::enqueue_attack_or_defense(uint8_t client_id, ActionState* pa) {
size_t attack_index = this->num_pending_attacks++;
this->pending_attacks[attack_index] = *pa;
if (log.should_log(phosg::LogLevel::L_DEBUG)) {
string pa_str = this->pending_attacks[attack_index].str(this->shared_from_this());
log.debug_f("set pending attack {}: {}", attack_index, pa_str);
log.debug_f("set pending attack {}: {}",
attack_index, this->pending_attacks[attack_index].str(this->shared_from_this()));
}
ps->set_action_cards_for_action_state(*pa);
log.debug_f("set action cards");
@@ -1013,14 +1012,14 @@ uint8_t Server::get_current_team_turn() const {
return this->current_team_turn1;
}
shared_ptr<PlayerState> Server::get_player_state(uint8_t client_id) {
std::shared_ptr<PlayerState> Server::get_player_state(uint8_t client_id) {
if (client_id >= 4) {
return nullptr;
}
return this->player_states[client_id];
}
shared_ptr<const PlayerState> Server::get_player_state(uint8_t client_id) const {
std::shared_ptr<const PlayerState> Server::get_player_state(uint8_t client_id) const {
if (client_id >= 4) {
return nullptr;
}
@@ -1094,19 +1093,19 @@ void Server::move_phase_after() {
continue;
}
static const array<vector<uint16_t>, 5> DEFAULT_TRAP_CARD_IDS = {
static const std::array<std::vector<uint16_t>, 5> DEFAULT_TRAP_CARD_IDS = {
// Red: Dice Fever, Heavy Fog, Muscular, Immortality, Snail Pace
vector<uint16_t>{0x00F7, 0x010F, 0x012E, 0x013B, 0x013C},
std::vector<uint16_t>{0x00F7, 0x010F, 0x012E, 0x013B, 0x013C},
// Blue: Gold Rush, Charity, Requiem
vector<uint16_t>{0x0131, 0x012B, 0x0133},
std::vector<uint16_t>{0x0131, 0x012B, 0x0133},
// Purple: Powerless Rain, Trash 1, Empty Hand, Skip Draw
vector<uint16_t>{0x00FA, 0x0125, 0x0126, 0x0137},
std::vector<uint16_t>{0x00FA, 0x0125, 0x0126, 0x0137},
// Green: Brave Wind, Homesick, Fly
vector<uint16_t>{0x00FB, 0x014E, 0x0107},
std::vector<uint16_t>{0x00FB, 0x014E, 0x0107},
// Yellow: Dice+1, Battle Royale, Reverse Card, Giant Garden, Fix
vector<uint16_t>{0x00F6, 0x0242, 0x014B, 0x0145, 0x012D}};
std::vector<uint16_t>{0x00F6, 0x0242, 0x014B, 0x0145, 0x012D}};
const vector<uint16_t>* trap_card_ids = &this->options.trap_card_ids.at(trap_type);
const std::vector<uint16_t>* trap_card_ids = &this->options.trap_card_ids.at(trap_type);
if (trap_card_ids->empty()) {
trap_card_ids = &DEFAULT_TRAP_CARD_IDS.at(trap_type);
}
@@ -1201,7 +1200,7 @@ int8_t Server::send_6xB4x33_remove_ally_atk_if_needed(const ActionState& pa) {
bool has_ally_cost = false;
uint8_t ally_cost = 0;
uint8_t setter_client_id = 0xFF;
shared_ptr<PlayerState> setter_ps = nullptr;
std::shared_ptr<PlayerState> setter_ps = nullptr;
cmd.card_ref = 0xFFFF;
for (size_t z = 0; (z < 8) && (pa.action_card_refs[z] != 0xFFFF); z++) {
auto ce = this->definition_for_card_ref(pa.action_card_refs[z]);
@@ -1509,7 +1508,7 @@ void Server::setup_and_start_battle() {
this->name_entries[z].clear();
}
} else {
this->player_states[z] = make_shared<PlayerState>(z, this->shared_from_this());
this->player_states[z] = std::make_shared<PlayerState>(z, this->shared_from_this());
this->player_states[z]->init();
}
}
@@ -1523,7 +1522,7 @@ void Server::setup_and_start_battle() {
}
auto card = ps->get_sc_card();
if (card) {
team_hp[ps->get_team_id()] = min<int16_t>(team_hp[ps->get_team_id()], card->get_current_hp());
team_hp[ps->get_team_id()] = std::min<int16_t>(team_hp[ps->get_team_id()], card->get_current_hp());
}
}
@@ -1739,7 +1738,7 @@ bool Server::update_registration_phase() {
return true;
}
const unordered_map<uint8_t, Server::handler_t> Server::subcommand_handlers({
const std::unordered_map<uint8_t, Server::handler_t> Server::subcommand_handlers{
{0x0B, &Server::handle_CAx0B_redraw_initial_hand},
{0x0C, &Server::handle_CAx0C_end_redraw_initial_hand_phase},
{0x0D, &Server::handle_CAx0D_end_non_action_phase},
@@ -1763,25 +1762,25 @@ const unordered_map<uint8_t, Server::handler_t> Server::subcommand_handlers({
{0x41, &Server::handle_CAx41_map_request},
{0x48, &Server::handle_CAx48_end_turn},
{0x49, &Server::handle_CAx49_card_counts},
});
};
void Server::on_server_data_input(shared_ptr<Client> sender_c, const string& data) {
void Server::on_server_data_input(std::shared_ptr<Client> sender_c, const std::string& data) {
auto header = check_size_t<G_CardBattleCommandHeader>(data, 0xFFFF);
size_t expected_size = header.size * 4;
if (expected_size < data.size()) {
phosg::print_data(stderr, data);
throw runtime_error(std::format(
throw std::runtime_error(std::format(
"command is incomplete: expected {:X} bytes, received {:X} bytes", expected_size, data.size()));
}
if (header.subcommand != 0xB3) {
throw runtime_error("server data command is not 6xB3");
throw std::runtime_error("server data command is not 6xB3");
}
handler_t handler = nullptr;
try {
handler = this->subcommand_handlers.at(header.subsubcommand);
} catch (const out_of_range&) {
throw runtime_error("unknown CAx subsubcommand");
} catch (const std::out_of_range&) {
throw std::runtime_error("unknown CAx subsubcommand");
}
if (this->battle_record && this->battle_record->writable()) {
@@ -1791,17 +1790,17 @@ void Server::on_server_data_input(shared_ptr<Client> sender_c, const string& dat
if ((sender_c && (sender_c->version() == Version::GC_EP3_NTE)) || !header.mask_key) {
(this->*handler)(sender_c, data);
} else {
string unmasked_data = data;
std::string unmasked_data = data;
set_mask_for_ep3_game_command(unmasked_data.data(), unmasked_data.size(), 0);
(this->*handler)(sender_c, unmasked_data);
}
}
void Server::handle_CAx0B_redraw_initial_hand(shared_ptr<Client>, const string& data) {
void Server::handle_CAx0B_redraw_initial_hand(std::shared_ptr<Client>, const std::string& data) {
const auto& in_cmd = check_size_t<G_RedrawInitialHand_Ep3_CAx0B>(data);
this->send_debug_command_received_message(in_cmd.client_id, in_cmd.header.subsubcommand, "REDRAW");
if (in_cmd.client_id >= 4) {
throw runtime_error("invalid client ID");
throw std::runtime_error("invalid client ID");
}
int32_t error_code = 0;
@@ -1829,11 +1828,11 @@ void Server::handle_CAx0B_redraw_initial_hand(shared_ptr<Client>, const string&
this->send_debug_message_if_error_code_nonzero(in_cmd.client_id, error_code);
}
void Server::handle_CAx0C_end_redraw_initial_hand_phase(shared_ptr<Client>, const string& data) {
void Server::handle_CAx0C_end_redraw_initial_hand_phase(std::shared_ptr<Client>, const std::string& data) {
const auto& in_cmd = check_size_t<G_EndInitialRedrawPhase_Ep3_CAx0C>(data);
this->send_debug_command_received_message(in_cmd.client_id, in_cmd.header.subsubcommand, "HAND READY");
if (in_cmd.client_id >= 4) {
throw runtime_error("invalid client ID");
throw std::runtime_error("invalid client ID");
}
int32_t error_code = 0;
@@ -1887,11 +1886,11 @@ void Server::handle_CAx0C_end_redraw_initial_hand_phase(shared_ptr<Client>, cons
this->send_debug_message_if_error_code_nonzero(in_cmd.client_id, error_code);
}
void Server::handle_CAx0D_end_non_action_phase(shared_ptr<Client>, const string& data) {
void Server::handle_CAx0D_end_non_action_phase(std::shared_ptr<Client>, const std::string& data) {
const auto& in_cmd = check_size_t<G_EndNonAttackPhase_Ep3_CAx0D>(data);
this->send_debug_command_received_message(in_cmd.client_id, in_cmd.header.subsubcommand, "END PHASE");
if (in_cmd.client_id >= 4) {
throw runtime_error("invalid client ID");
throw std::runtime_error("invalid client ID");
}
if (!this->options.is_nte()) {
@@ -1909,11 +1908,11 @@ void Server::handle_CAx0D_end_non_action_phase(shared_ptr<Client>, const string&
this->send(out_cmd_fin);
}
void Server::handle_CAx0E_discard_card_from_hand(shared_ptr<Client>, const string& data) {
void Server::handle_CAx0E_discard_card_from_hand(std::shared_ptr<Client>, const std::string& data) {
const auto& in_cmd = check_size_t<G_DiscardCardFromHand_Ep3_CAx0E>(data);
this->send_debug_command_received_message(in_cmd.client_id, in_cmd.header.subsubcommand, "DISCARD");
if (in_cmd.client_id >= 4) {
throw runtime_error("invalid client ID");
throw std::runtime_error("invalid client ID");
}
int32_t error_code = 0;
@@ -1948,11 +1947,11 @@ void Server::handle_CAx0E_discard_card_from_hand(shared_ptr<Client>, const strin
this->send_debug_message_if_error_code_nonzero(in_cmd.client_id, error_code);
}
void Server::handle_CAx0F_set_card_from_hand(shared_ptr<Client>, const string& data) {
void Server::handle_CAx0F_set_card_from_hand(std::shared_ptr<Client>, const std::string& data) {
const auto& in_cmd = check_size_t<G_SetCardFromHand_Ep3_CAx0F>(data);
this->send_debug_command_received_message(in_cmd.client_id, in_cmd.header.subsubcommand, "SET FC");
if (in_cmd.client_id >= 4) {
throw runtime_error("invalid client ID");
throw std::runtime_error("invalid client ID");
}
int32_t error_code = 0;
@@ -1994,11 +1993,11 @@ void Server::handle_CAx0F_set_card_from_hand(shared_ptr<Client>, const string& d
this->send_debug_message_if_error_code_nonzero(in_cmd.client_id, this->ruler_server->error_code1);
}
void Server::handle_CAx10_move_fc_to_location(shared_ptr<Client>, const string& data) {
void Server::handle_CAx10_move_fc_to_location(std::shared_ptr<Client>, const std::string& data) {
const auto& in_cmd = check_size_t<G_MoveFieldCharacter_Ep3_CAx10>(data);
this->send_debug_command_received_message(in_cmd.client_id, in_cmd.header.subsubcommand, "MOVE");
if (in_cmd.client_id >= 4) {
throw runtime_error("invalid client ID");
throw std::runtime_error("invalid client ID");
}
int32_t error_code = 0;
@@ -2034,11 +2033,11 @@ void Server::handle_CAx10_move_fc_to_location(shared_ptr<Client>, const string&
this->send_debug_message_if_error_code_nonzero(in_cmd.client_id, this->ruler_server->error_code2);
}
void Server::handle_CAx11_enqueue_attack_or_defense(shared_ptr<Client>, const string& data) {
void Server::handle_CAx11_enqueue_attack_or_defense(std::shared_ptr<Client>, const std::string& data) {
const auto& in_cmd = check_size_t<G_EnqueueAttackOrDefense_Ep3_CAx11>(data);
this->send_debug_command_received_message(in_cmd.client_id, in_cmd.header.subsubcommand, "ENQUEUE ACT");
if (in_cmd.client_id >= 4) {
throw runtime_error("invalid client ID");
throw std::runtime_error("invalid client ID");
}
int32_t error_code = 0;
@@ -2072,11 +2071,11 @@ void Server::handle_CAx11_enqueue_attack_or_defense(shared_ptr<Client>, const st
this->send_debug_message_if_error_code_nonzero(in_cmd.client_id, this->ruler_server->error_code3);
}
void Server::handle_CAx12_end_attack_list(shared_ptr<Client>, const string& data) {
void Server::handle_CAx12_end_attack_list(std::shared_ptr<Client>, const std::string& data) {
const auto& in_cmd = check_size_t<G_EndAttackList_Ep3_CAx12>(data);
this->send_debug_command_received_message(in_cmd.client_id, in_cmd.header.subsubcommand, "END ATK LIST");
if (in_cmd.client_id >= 4) {
throw runtime_error("invalid client ID");
throw std::runtime_error("invalid client ID");
}
int32_t error_code = 0;
@@ -2097,7 +2096,7 @@ void Server::handle_CAx12_end_attack_list(shared_ptr<Client>, const string& data
}
template <typename CmdT>
void Server::handle_CAx13_update_map_during_setup_t(shared_ptr<Client> c, const string& data) {
void Server::handle_CAx13_update_map_during_setup_t(std::shared_ptr<Client> c, const std::string& data) {
const auto& in_cmd = check_size_t<CmdT>(data);
this->send_debug_command_received_message(in_cmd.header.subsubcommand, "UPDATE MAP");
@@ -2157,7 +2156,7 @@ void Server::handle_CAx13_update_map_during_setup_t(shared_ptr<Client> c, const
}
}
void Server::handle_CAx13_update_map_during_setup(shared_ptr<Client> c, const string& data) {
void Server::handle_CAx13_update_map_during_setup(std::shared_ptr<Client> c, const std::string& data) {
if (this->options.is_nte()) {
this->handle_CAx13_update_map_during_setup_t<G_SetMapState_Ep3NTE_CAx13>(c, data);
} else {
@@ -2165,7 +2164,7 @@ void Server::handle_CAx13_update_map_during_setup(shared_ptr<Client> c, const st
}
}
void Server::handle_CAx14_update_deck_during_setup(shared_ptr<Client>, const string& data) {
void Server::handle_CAx14_update_deck_during_setup(std::shared_ptr<Client>, const std::string& data) {
const auto& in_cmd = check_size_t<G_SetPlayerDeck_Ep3_CAx14>(data);
this->send_debug_command_received_message(in_cmd.client_id, in_cmd.header.subsubcommand, "UPDATE DECK");
@@ -2188,7 +2187,7 @@ void Server::handle_CAx14_update_deck_during_setup(shared_ptr<Client>, const str
}
}
if (verify_error) {
throw runtime_error(std::format("invalid deck: -0x{:X}", verify_error));
throw std::runtime_error(std::format("invalid deck: -0x{:X}", verify_error));
}
if (!this->options.is_nte() && !(this->options.behavior_flags & BehaviorFlag::SKIP_D1_D2_REPLACE)) {
this->ruler_server->replace_D1_D2_rank_cards_with_Attack(entry.card_ids);
@@ -2211,7 +2210,7 @@ void Server::handle_CAx14_update_deck_during_setup(shared_ptr<Client>, const str
}
}
void Server::handle_CAx15_unused_hard_reset_server_state(shared_ptr<Client>, const string& data) {
void Server::handle_CAx15_unused_hard_reset_server_state(std::shared_ptr<Client>, const std::string& data) {
const auto& in_cmd = check_size_t<G_HardResetServerState_Ep3_CAx15>(data);
this->send_debug_command_received_message(in_cmd.header.subsubcommand, "HARD RESET");
@@ -2224,10 +2223,10 @@ void Server::handle_CAx15_unused_hard_reset_server_state(shared_ptr<Client>, con
// this->send_all_state_updates();
// this->update_registration_phase();
// this->setup_and_start_battle();
throw runtime_error("hard reset command received");
throw std::runtime_error("hard reset command received");
}
void Server::handle_CAx1B_update_player_name(shared_ptr<Client>, const string& data) {
void Server::handle_CAx1B_update_player_name(std::shared_ptr<Client>, const std::string& data) {
const auto& in_cmd = check_size_t<G_SetPlayerName_Ep3_CAx1B>(data);
this->send_debug_command_received_message(in_cmd.entry.client_id, in_cmd.header.subsubcommand, "UPDATE NAME");
@@ -2258,7 +2257,7 @@ void Server::handle_CAx1B_update_player_name(shared_ptr<Client>, const string& d
this->send(out_cmd);
}
void Server::handle_CAx1D_start_battle(shared_ptr<Client>, const string& data) {
void Server::handle_CAx1D_start_battle(std::shared_ptr<Client>, const std::string& data) {
const auto& in_cmd = check_size_t<G_StartBattle_Ep3_CAx1D>(data);
this->send_debug_command_received_message(in_cmd.header.subsubcommand, "START BATTLE");
@@ -2304,7 +2303,7 @@ void Server::handle_CAx1D_start_battle(shared_ptr<Client>, const string& data) {
}
}
void Server::handle_CAx21_end_battle(shared_ptr<Client>, const string& data) {
void Server::handle_CAx21_end_battle(std::shared_ptr<Client>, const std::string& data) {
const auto& in_cmd = check_size_t<G_EndBattle_Ep3_CAx21>(data);
this->send_debug_command_received_message(in_cmd.header.subsubcommand, "END BATTLE");
if (this->setup_phase == SetupPhase::BATTLE_ENDED) {
@@ -2318,11 +2317,11 @@ void Server::handle_CAx21_end_battle(shared_ptr<Client>, const string& data) {
}
}
void Server::handle_CAx28_end_defense_list(shared_ptr<Client>, const string& data) {
void Server::handle_CAx28_end_defense_list(std::shared_ptr<Client>, const std::string& data) {
const auto& in_cmd = check_size_t<G_EndDefenseList_Ep3_CAx28>(data);
this->send_debug_command_received_message(in_cmd.client_id, in_cmd.header.subsubcommand, "END DEF LIST");
if (in_cmd.client_id >= 4) {
throw runtime_error("invalid client ID");
throw std::runtime_error("invalid client ID");
}
G_ActionResult_Ep3_6xB4x1E out_cmd_ack;
@@ -2369,13 +2368,13 @@ void Server::handle_CAx28_end_defense_list(shared_ptr<Client>, const string& dat
this->send(out_cmd_fin);
}
void Server::handle_CAx2B_legacy_set_card(shared_ptr<Client>, const string& data) {
void Server::handle_CAx2B_legacy_set_card(std::shared_ptr<Client>, const std::string& data) {
const auto& in_cmd = check_size_t<G_ExecLegacyCard_Ep3_CAx2B>(data);
this->send_debug_command_received_message(in_cmd.header.subsubcommand, "EXEC LEGACY");
// Sega's original implementation does nothing here, so we do nothing as well.
}
void Server::handle_CAx34_subtract_ally_atk_points(shared_ptr<Client>, const string& data) {
void Server::handle_CAx34_subtract_ally_atk_points(std::shared_ptr<Client>, const std::string& data) {
const auto& in_cmd = check_size_t<G_PhotonBlastRequest_Ep3_CAx34>(data);
uint8_t card_ref_client_id = client_id_for_card_ref(in_cmd.card_ref);
@@ -2450,11 +2449,11 @@ void Server::handle_CAx34_subtract_ally_atk_points(shared_ptr<Client>, const str
}
}
void Server::handle_CAx37_client_ready_to_advance_from_starter_roll_phase(shared_ptr<Client>, const string& data) {
void Server::handle_CAx37_client_ready_to_advance_from_starter_roll_phase(std::shared_ptr<Client>, const std::string& data) {
const auto& in_cmd = check_size_t<G_AdvanceFromStartingRollsPhase_Ep3_CAx37>(data);
this->send_debug_command_received_message(in_cmd.client_id, in_cmd.header.subsubcommand, "CHOOSE ORDER");
if (in_cmd.client_id >= 4) {
throw runtime_error("invalid client ID");
throw std::runtime_error("invalid client ID");
}
auto ps = this->player_states[in_cmd.client_id];
@@ -2479,19 +2478,19 @@ void Server::handle_CAx37_client_ready_to_advance_from_starter_roll_phase(shared
}
}
void Server::handle_CAx3A_time_limit_expired(shared_ptr<Client>, const string& data) {
void Server::handle_CAx3A_time_limit_expired(std::shared_ptr<Client>, const std::string& data) {
const auto& in_cmd = check_size_t<G_OverallTimeLimitExpired_Ep3_CAx3A>(data);
this->send_debug_command_received_message(in_cmd.header.subsubcommand, "TIME EXPIRED");
// We don't need to do anything here because the overall time limit is tracked server-side instead.
}
void Server::handle_CAx40_map_list_request(shared_ptr<Client> sender_c, const string& data) {
void Server::handle_CAx40_map_list_request(std::shared_ptr<Client> sender_c, const std::string& data) {
const auto& in_cmd = check_size_t<G_MapListRequest_Ep3_CAx40>(data);
this->send_debug_command_received_message(in_cmd.header.subsubcommand, "MAP LIST");
auto l = this->lobby.lock();
if (!l) {
throw runtime_error("lobby is deleted");
throw std::runtime_error("lobby is deleted");
}
size_t num_players = l ? l->count_clients() : 1;
@@ -2512,13 +2511,13 @@ void Server::handle_CAx40_map_list_request(shared_ptr<Client> sender_c, const st
void Server::send_6xB6x41_to_all_clients() const {
if (!this->last_chosen_map) {
throw logic_error("cannot send 6xB4x41 without a map chosen");
throw std::logic_error("cannot send 6xB4x41 without a map chosen");
}
auto l = this->lobby.lock();
if (l) {
vector<string> map_commands_by_language;
auto send_to_client = [&](shared_ptr<Client> c) -> void {
std::vector<std::string> map_commands_by_language;
auto send_to_client = [&](std::shared_ptr<Client> c) -> void {
if (!c) {
return;
}
@@ -2545,7 +2544,7 @@ void Server::send_6xB6x41_to_all_clients() const {
if (this->battle_record && this->battle_record->writable()) {
// TODO: It's not great that we just pick the first one; ideally we'd put all of them in the recording and send
// the appropriate one to the client in the playback lobby
for (string& data : map_commands_by_language) {
for (std::string& data : map_commands_by_language) {
if (!data.empty()) {
this->battle_record->add_command(BattleRecord::Event::Type::BATTLE_COMMAND, std::move(data));
break;
@@ -2559,7 +2558,7 @@ void Server::send_6xB6x41_to_all_clients() const {
}
}
void Server::handle_CAx41_map_request(shared_ptr<Client>, const string& data) {
void Server::handle_CAx41_map_request(std::shared_ptr<Client>, const std::string& data) {
const auto& cmd = check_size_t<G_MapDataRequest_Ep3_CAx41>(data);
this->send_debug_command_received_message(cmd.header.subsubcommand, "MAP DATA");
if (!this->options.tournament || (this->options.tournament->get_map()->map_number == cmd.map_number)) {
@@ -2568,11 +2567,11 @@ void Server::handle_CAx41_map_request(shared_ptr<Client>, const string& data) {
}
}
void Server::handle_CAx48_end_turn(shared_ptr<Client>, const string& data) {
void Server::handle_CAx48_end_turn(std::shared_ptr<Client>, const std::string& data) {
const auto& in_cmd = check_size_t<G_EndTurn_Ep3_CAx48>(data);
this->send_debug_command_received_message(in_cmd.client_id, in_cmd.header.subsubcommand, "END TURN");
if (in_cmd.client_id >= 4) {
throw runtime_error("invalid client ID");
throw std::runtime_error("invalid client ID");
}
auto ps = this->get_player_state(in_cmd.client_id);
@@ -2585,7 +2584,7 @@ void Server::handle_CAx48_end_turn(shared_ptr<Client>, const string& data) {
this->send(out_cmd);
}
void Server::handle_CAx49_card_counts(shared_ptr<Client>, const string& data) {
void Server::handle_CAx49_card_counts(std::shared_ptr<Client>, const std::string& data) {
const auto& in_cmd = check_size_t<G_CardCounts_Ep3_CAx49>(data);
this->send_debug_command_received_message(in_cmd.header.sender_client_id, in_cmd.header.subsubcommand, "CARD COUNTS");
@@ -2612,7 +2611,7 @@ void Server::compute_losing_team_id_and_add_winner_flags(uint32_t flags) {
uint32_t winner_flags = flags | AssistFlag::HAS_WON_BATTLE | AssistFlag::WINNER_DECIDED_BY_DEFEAT;
int8_t losing_team_id = -1;
array<uint32_t, 2> team_counts = {0, 0};
std::array<uint32_t, 2> team_counts{0, 0};
if (!is_nte) {
// First, check which team has more dead SCs
@@ -2747,8 +2746,7 @@ void Server::unknown_8023EEF4() {
ActionState as = this->pending_attacks_with_cards[this->unknown_a14];
if (log.should_log(phosg::LogLevel::L_DEBUG)) {
log.debug_f("card @{:04X} #{:04X} can attack", card->get_card_ref(), card->get_card_id());
string as_str = as.str(this->shared_from_this());
log.debug_f("as: {}", as_str);
log.debug_f("as: {}", as.str(this->shared_from_this()));
}
if (is_nte) {
this->replace_targets_due_to_destruction_nte(&as);
@@ -2756,8 +2754,7 @@ void Server::unknown_8023EEF4() {
this->replace_targets_due_to_destruction_or_conditions(&as);
}
if (log.should_log(phosg::LogLevel::L_DEBUG)) {
string as_str = as.str(this->shared_from_this());
log.debug_f("as after target replacement: {}", as_str);
log.debug_f("as after target replacement: {}", as.str(this->shared_from_this()));
}
if (this->any_target_exists_for_attack(as)) {
log.debug_f("as is valid");
@@ -2837,8 +2834,8 @@ void Server::execute_bomb_assist_effect() {
for (size_t set_index = 0; set_index < 8; set_index++) {
auto card = ps->get_set_card(set_index);
if (card && !(card->card_flags & 2)) {
max_hp = max<int16_t>(max_hp, card->get_current_hp());
min_hp = min<int16_t>(min_hp, card->get_current_hp());
max_hp = std::max<int16_t>(max_hp, card->get_current_hp());
min_hp = std::min<int16_t>(min_hp, card->get_current_hp());
}
}
}
@@ -2875,7 +2872,7 @@ void Server::replace_targets_due_to_destruction_nte(ActionState* as) {
continue;
}
auto ps = target_card->player_state();
shared_ptr<Card> found_guard_item;
std::shared_ptr<Card> found_guard_item;
for (size_t z = 0; z < 8; z++) {
auto set_card = ps->get_set_card(z);
if (set_card && (set_card != target_card) && !(set_card->card_flags & 2) && set_card->is_guard_item()) {
@@ -2915,7 +2912,7 @@ void Server::replace_targets_due_to_destruction_or_conditions(ActionState* as) {
return;
}
vector<uint16_t> phase1_replaced_card_refs;
std::vector<uint16_t> phase1_replaced_card_refs;
for (size_t client_id = 0; client_id < 4; client_id++) {
auto ps = this->get_player_state(client_id);
if (!attacker_card->action_chain.check_flag(0x200 << client_id)) {
@@ -3005,7 +3002,7 @@ void Server::replace_targets_due_to_destruction_or_conditions(ActionState* as) {
}
// as->target_card_refs[phase1_replaced_card_refs.size()] = 0xFFFF;
vector<uint16_t> phase2_replaced_card_refs;
std::vector<uint16_t> phase2_replaced_card_refs;
for (size_t z = 0; (z < 4 * 9) && (as->target_card_refs[z] != 0xFFFF); z++) {
uint16_t target_card_ref = this->send_6xB4x06_if_card_ref_invalid(as->target_card_refs[z], 7);
auto target_card = this->card_for_set_card_ref(target_card_ref);
@@ -3105,13 +3102,14 @@ void Server::unknown_802402F4() {
}
}
vector<shared_ptr<Card>> Server::const_cast_set_cards_v(const vector<shared_ptr<const Card>>& cards) {
std::vector<std::shared_ptr<Card>> Server::const_cast_set_cards_v(
const std::vector<std::shared_ptr<const Card>>& cards) {
// TODO: This is dumb. Figure out a not-dumb way to do this.
vector<shared_ptr<Card>> ret;
std::vector<std::shared_ptr<Card>> ret;
for (auto const_card : cards) {
auto mutable_card = this->card_for_set_card_ref(const_card->get_card_ref());
if (mutable_card.get() != const_card.get()) {
throw logic_error("inconsistent set cards index");
throw std::logic_error("inconsistent set cards index");
}
ret.emplace_back(mutable_card);
}
+1 -1
View File
@@ -37,7 +37,7 @@ namespace Episode3 {
// RulerServer.hh/cc
// Server.hh/cc
// Class ownership levels (classes may contain weak_ptrs but not shared_ptrs to classes at the same or higher level):
// Class ownership levels (classes may contain weak_ptrs but not std::shared_ptrs to classes at the same or higher level):
// - Server
// - - RulerServer
// - - - AssistServer
+100 -116
View File
@@ -7,23 +7,18 @@
#include "../SendCommands.hh"
#include "../ServerState.hh"
using namespace std;
namespace Episode3 {
Tournament::PlayerEntry::PlayerEntry(uint32_t account_id, const string& player_name)
: account_id(account_id),
player_name(player_name) {}
Tournament::PlayerEntry::PlayerEntry(uint32_t account_id, const std::string& player_name)
: account_id(account_id), player_name(player_name) {}
Tournament::PlayerEntry::PlayerEntry(shared_ptr<Client> c)
Tournament::PlayerEntry::PlayerEntry(std::shared_ptr<Client> c)
: account_id(c->login->account->account_id),
client(c),
player_name(c->character_file()->disp.name.decode(c->language())) {}
player_name(c->character_file()->disp.visual.name.decode(c->language())) {}
Tournament::PlayerEntry::PlayerEntry(
shared_ptr<const COMDeckDefinition> com_deck)
: account_id(0),
com_deck(com_deck) {}
Tournament::PlayerEntry::PlayerEntry(std::shared_ptr<const COMDeckDefinition> com_deck)
: account_id(0), com_deck(com_deck) {}
bool Tournament::PlayerEntry::is_com() const {
return (this->com_deck != nullptr);
@@ -33,8 +28,7 @@ bool Tournament::PlayerEntry::is_human() const {
return (this->account_id != 0);
}
Tournament::Team::Team(
shared_ptr<Tournament> tournament, size_t index, size_t max_players)
Tournament::Team::Team(std::shared_ptr<Tournament> tournament, size_t index, size_t max_players)
: tournament(tournament),
index(index),
max_players(max_players),
@@ -43,7 +37,7 @@ Tournament::Team::Team(
num_rounds_cleared(0),
is_active(true) {}
string Tournament::Team::str() const {
std::string Tournament::Team::str() const {
size_t num_human_players = 0;
size_t num_com_players = 0;
for (const auto& player : this->players) {
@@ -51,7 +45,7 @@ string Tournament::Team::str() const {
num_com_players += player.is_com();
}
string ret = std::format("[Team/{} {} {}H/{}C/{}P name={} pass={} rounds={}",
std::string ret = std::format("[Team/{} {} {}H/{}C/{}P name={} pass={} rounds={}",
this->index, this->is_active ? "active" : "inactive",
num_human_players, num_com_players, this->max_players, this->name,
this->password, this->num_rounds_cleared);
@@ -67,26 +61,27 @@ string Tournament::Team::str() const {
return ret + "]";
}
void Tournament::Team::register_player(shared_ptr<Client> c, const string& team_name, const string& password) {
void Tournament::Team::register_player(
std::shared_ptr<Client> c, const std::string& team_name, const std::string& password) {
if (this->players.size() >= this->max_players) {
throw runtime_error("team is full");
throw std::runtime_error("team is full");
}
if (!this->name.empty() && (password != this->password)) {
throw runtime_error("incorrect password");
throw std::runtime_error("incorrect password");
}
auto tournament = this->tournament.lock();
if (!tournament) {
throw runtime_error("tournament has been deleted");
throw std::runtime_error("tournament has been deleted");
}
if (!tournament->all_player_account_ids.emplace(c->login->account->account_id).second) {
throw runtime_error("player already registered in same tournament");
throw std::runtime_error("player already registered in same tournament");
}
for (const auto& player : this->players) {
if (player.is_human() && (player.account_id == c->login->account->account_id)) {
throw logic_error("player already registered in team but not in tournament");
throw std::logic_error("player already registered in team but not in tournament");
}
}
@@ -125,7 +120,7 @@ bool Tournament::Team::unregister_player(uint32_t account_id) {
// Look through the pending matches to see if this team is involved in any of them
for (auto match : tournament->pending_matches) {
if (!match->preceding_a || !match->preceding_b) {
throw logic_error("zero-round match is pending after tournament registration phase");
throw std::logic_error("zero-round match is pending after tournament registration phase");
}
if (match->preceding_a->winner_team.get() == this) {
match->set_winner_team(match->preceding_b->winner_team);
@@ -139,7 +134,7 @@ bool Tournament::Team::unregister_player(uint32_t account_id) {
} else {
// If the tournament has not started yet, just remove the player from the team
if (!tournament->all_player_account_ids.erase(account_id)) {
throw logic_error("player removed from team but not from tournament");
throw std::logic_error("player removed from team but not from tournament");
}
}
@@ -176,27 +171,19 @@ size_t Tournament::Team::num_com_players() const {
}
Tournament::Match::Match(
shared_ptr<Tournament> tournament, shared_ptr<Match> preceding_a, shared_ptr<Match> preceding_b)
: tournament(tournament),
preceding_a(preceding_a),
preceding_b(preceding_b),
winner_team(nullptr),
round_num(0) {
std::shared_ptr<Tournament> tournament, std::shared_ptr<Match> preceding_a, std::shared_ptr<Match> preceding_b)
: tournament(tournament), preceding_a(preceding_a), preceding_b(preceding_b), winner_team(nullptr), round_num(0) {
if (this->preceding_a->round_num != this->preceding_b->round_num) {
throw logic_error("preceding matches have different round numbers");
throw std::logic_error("preceding matches have different round numbers");
}
this->round_num = this->preceding_a->round_num + 1;
}
Tournament::Match::Match(shared_ptr<Tournament> tournament, shared_ptr<Team> winner_team)
: tournament(tournament),
preceding_a(nullptr),
preceding_b(nullptr),
winner_team(winner_team),
round_num(0) {}
Tournament::Match::Match(std::shared_ptr<Tournament> tournament, std::shared_ptr<Team> winner_team)
: tournament(tournament), preceding_a(nullptr), preceding_b(nullptr), winner_team(winner_team), round_num(0) {}
string Tournament::Match::str() const {
string winner_str = this->winner_team ? this->winner_team->str() : "(none)";
std::string Tournament::Match::str() const {
std::string winner_str = this->winner_team ? this->winner_team->str() : "(none)";
return std::format("[Match round={} winner={}]", this->round_num, winner_str);
}
@@ -262,12 +249,12 @@ void Tournament::Match::on_winner_team_set() {
}
}
void Tournament::Match::set_winner_team_without_triggers(shared_ptr<Team> team) {
void Tournament::Match::set_winner_team_without_triggers(std::shared_ptr<Team> team) {
if (!this->preceding_a || !this->preceding_b) {
throw logic_error("set_winner_team called on zero-round match");
throw std::logic_error("set_winner_team called on zero-round match");
}
if ((team != this->preceding_a->winner_team) && (team != this->preceding_b->winner_team)) {
throw logic_error("winner team did not participate in match");
throw std::logic_error("winner team did not participate in match");
}
this->winner_team = team;
@@ -280,29 +267,29 @@ void Tournament::Match::set_winner_team_without_triggers(shared_ptr<Team> team)
}
}
void Tournament::Match::set_winner_team(shared_ptr<Team> team) {
void Tournament::Match::set_winner_team(std::shared_ptr<Team> team) {
this->set_winner_team_without_triggers(team);
this->on_winner_team_set();
}
shared_ptr<Tournament::Team> Tournament::Match::opponent_team_for_team(shared_ptr<Team> team) const {
std::shared_ptr<Tournament::Team> Tournament::Match::opponent_team_for_team(std::shared_ptr<Team> team) const {
if (!this->preceding_a || !this->preceding_b) {
throw logic_error("zero-round matches do not have opponents");
throw std::logic_error("zero-round matches do not have opponents");
}
if (team == this->preceding_a->winner_team) {
return this->preceding_b->winner_team;
} else if (team == this->preceding_b->winner_team) {
return this->preceding_a->winner_team;
} else {
throw logic_error("team is not registered for this match");
throw std::logic_error("team is not registered for this match");
}
}
Tournament::Tournament(
shared_ptr<const MapIndex> map_index,
shared_ptr<const COMDeckIndex> com_deck_index,
const string& name,
shared_ptr<const MapIndex::Map> map,
std::shared_ptr<const MapIndex> map_index,
std::shared_ptr<const COMDeckIndex> com_deck_index,
const std::string& name,
std::shared_ptr<const MapIndex::Map> map,
const Rules& rules,
size_t num_teams,
uint8_t flags)
@@ -317,18 +304,20 @@ Tournament::Tournament(
current_state(State::REGISTRATION),
menu_item_id(0xFFFFFFFF) {
if (this->num_teams < 4) {
throw invalid_argument("team count must be 4 or more");
throw std::invalid_argument("team count must be 4 or more");
}
if (this->num_teams > 32) {
throw invalid_argument("team count must be 32 or fewer");
throw std::invalid_argument("team count must be 32 or fewer");
}
if (this->num_teams & (this->num_teams - 1)) {
throw invalid_argument("team count must be a power of 2");
throw std::invalid_argument("team count must be a power of 2");
}
}
Tournament::Tournament(
shared_ptr<const MapIndex> map_index, shared_ptr<const COMDeckIndex> com_deck_index, const phosg::JSON& json)
std::shared_ptr<const MapIndex> map_index,
std::shared_ptr<const COMDeckIndex> com_deck_index,
const phosg::JSON& json)
: log(std::format("[Tournament:{}] ", json.get_string("name"))),
map_index(map_index),
com_deck_index(com_deck_index),
@@ -336,7 +325,7 @@ Tournament::Tournament(
current_state(State::REGISTRATION) {}
void Tournament::init() {
vector<size_t> team_index_to_rounds_cleared;
std::vector<size_t> team_index_to_rounds_cleared;
bool is_registration_complete;
if (!this->source_json.is_null()) {
@@ -350,7 +339,7 @@ void Tournament::init() {
is_registration_complete = this->source_json.get_bool("is_registration_complete");
for (const auto& team_json : this->source_json.get_list("teams")) {
auto& team = this->teams.emplace_back(make_shared<Team>(
auto& team = this->teams.emplace_back(std::make_shared<Team>(
this->shared_from_this(), this->teams.size(), team_json->get_int("max_players")));
team->name = team_json->get_string("name");
team->password = team_json->get_string("password");
@@ -367,7 +356,7 @@ void Tournament::init() {
} else if (player_json->is_string()) {
team->players.emplace_back(this->com_deck_index->deck_for_name(player_json->as_string()));
} else {
throw runtime_error("invalid player spec");
throw std::runtime_error("invalid player spec");
}
}
}
@@ -378,7 +367,7 @@ void Tournament::init() {
} else {
// Create empty teams
while (this->teams.size() < this->num_teams) {
auto t = make_shared<Team>(this->shared_from_this(), this->teams.size(), (this->flags & Flag::IS_2V2) ? 2 : 1);
auto t = std::make_shared<Team>(this->shared_from_this(), this->teams.size(), (this->flags & Flag::IS_2V2) ? 2 : 1);
this->teams.emplace_back(t);
}
is_registration_complete = false;
@@ -390,12 +379,12 @@ void Tournament::init() {
this->create_bracket_matches();
// Start with all zero-round matches in the match queue
unordered_set<shared_ptr<Match>> match_queue;
std::unordered_set<std::shared_ptr<Match>> match_queue;
for (auto match : this->zero_round_matches) {
match_queue.emplace(match->following.lock());
}
if (match_queue.count(nullptr)) {
throw logic_error("null match in match queue");
throw std::logic_error("null match in match queue");
}
// For each match in the queue, either resolve it from the previous state or
@@ -406,12 +395,12 @@ void Tournament::init() {
match_queue.erase(match_it);
if (!match->preceding_a->winner_team || !match->preceding_b->winner_team) {
throw logic_error("preceding matches are not resolved");
throw std::logic_error("preceding matches are not resolved");
}
size_t& a_rounds_cleared = team_index_to_rounds_cleared[match->preceding_a->winner_team->index];
size_t& b_rounds_cleared = team_index_to_rounds_cleared[match->preceding_b->winner_team->index];
if (a_rounds_cleared && b_rounds_cleared) {
throw runtime_error("both teams won the same match");
throw std::runtime_error("both teams won the same match");
}
if (!a_rounds_cleared && !b_rounds_cleared) {
this->pending_matches.emplace(match); // Neither team has won yet
@@ -434,7 +423,7 @@ void Tournament::init() {
}
if (!this->final_match->winner_team == this->pending_matches.empty()) {
throw logic_error("there must be pending matches if and only if the final match is not resolved");
throw std::logic_error("there must be pending matches if and only if the final match is not resolved");
}
// If all matches are resolved, then the tournament is complete
@@ -449,19 +438,19 @@ void Tournament::init() {
void Tournament::create_bracket_matches() {
if (this->teams.size() < 4) {
throw logic_error("tournaments must have at least 4 teams");
throw std::logic_error("tournaments must have at least 4 teams");
}
if (this->teams.size() > 32) {
throw logic_error("tournaments must have at most 32 teams");
throw std::logic_error("tournaments must have at most 32 teams");
}
if (this->teams.size() & (this->teams.size() - 1)) {
throw logic_error("tournaments team count is not a power of 2");
throw std::logic_error("tournaments team count is not a power of 2");
}
// Create the zero-round matches, and make them all pending if registration is still open
this->zero_round_matches.clear();
for (const auto& team : this->teams) {
auto m = make_shared<Match>(this->shared_from_this(), team);
auto m = std::make_shared<Match>(this->shared_from_this(), team);
this->zero_round_matches.emplace_back(m);
if (this->current_state == State::REGISTRATION) {
this->pending_matches.emplace(m);
@@ -469,11 +458,11 @@ void Tournament::create_bracket_matches() {
}
// Create the bracket matches
vector<shared_ptr<Match>> current_round_matches = this->zero_round_matches;
std::vector<std::shared_ptr<Match>> current_round_matches = this->zero_round_matches;
while (current_round_matches.size() > 1) {
vector<shared_ptr<Match>> next_round_matches;
std::vector<std::shared_ptr<Match>> next_round_matches;
for (size_t z = 0; z < current_round_matches.size(); z += 2) {
auto m = make_shared<Match>(this->shared_from_this(), current_round_matches[z], current_round_matches[z + 1]);
auto m = std::make_shared<Match>(this->shared_from_this(), current_round_matches[z], current_round_matches[z + 1]);
current_round_matches[z]->following = m;
current_round_matches[z + 1]->following = m;
next_round_matches.emplace_back(std::move(m));
@@ -516,26 +505,26 @@ phosg::JSON Tournament::json() const {
});
}
shared_ptr<Tournament::Team> Tournament::get_winner_team() const {
std::shared_ptr<Tournament::Team> Tournament::get_winner_team() const {
if (this->current_state != State::COMPLETE) {
return nullptr;
}
if (!this->final_match) {
throw logic_error("tournament is complete but final match is missing");
throw std::logic_error("tournament is complete but final match is missing");
}
if (!this->final_match->winner_team) {
throw logic_error("tournament is complete but winner is not set");
throw std::logic_error("tournament is complete but winner is not set");
}
return this->final_match->winner_team;
}
shared_ptr<Tournament::Match> Tournament::next_match_for_team(shared_ptr<Team> team) const {
std::shared_ptr<Tournament::Match> Tournament::next_match_for_team(std::shared_ptr<Team> team) const {
if (this->current_state == Tournament::State::REGISTRATION) {
return nullptr;
}
for (auto match : this->pending_matches) {
if (!match->preceding_a || !match->preceding_b) {
throw logic_error("zero-round match is pending after tournament registration phase");
throw std::logic_error("zero-round match is pending after tournament registration phase");
}
if ((team == match->preceding_a->winner_team) || (team == match->preceding_b->winner_team)) {
return match;
@@ -544,11 +533,11 @@ shared_ptr<Tournament::Match> Tournament::next_match_for_team(shared_ptr<Team> t
return nullptr;
}
shared_ptr<Tournament::Match> Tournament::get_final_match() const {
std::shared_ptr<Tournament::Match> Tournament::get_final_match() const {
return this->final_match;
}
shared_ptr<Tournament::Team> Tournament::team_for_account_id(uint32_t account_id) const {
std::shared_ptr<Tournament::Team> Tournament::team_for_account_id(uint32_t account_id) const {
if (!this->all_player_account_ids.count(account_id)) {
return nullptr;
}
@@ -561,16 +550,16 @@ shared_ptr<Tournament::Team> Tournament::team_for_account_id(uint32_t account_id
}
}
throw logic_error("account ID registered in tournament but not in any team");
throw std::logic_error("account ID registered in tournament but not in any team");
}
const set<uint32_t>& Tournament::get_all_player_account_ids() const {
const std::set<uint32_t>& Tournament::get_all_player_account_ids() const {
return this->all_player_account_ids;
}
void Tournament::start() {
if (this->current_state != State::REGISTRATION) {
throw runtime_error("tournament has already started");
throw std::runtime_error("tournament has already started");
}
bool has_com_teams = (this->flags & Flag::HAS_COM_TEAMS);
@@ -584,7 +573,7 @@ void Tournament::start() {
}
}
if (num_human_teams < (has_com_teams ? 1 : 2)) {
throw runtime_error("not enough registrants to start tournament");
throw std::runtime_error("not enough registrants to start tournament");
}
if ((this->flags & Flag::SHUFFLE_ENTRIES) && (this->flags & Flag::RESIZE_ON_START)) {
@@ -642,11 +631,11 @@ void Tournament::start() {
}
for (const auto& player : t->players) {
if (player.is_com()) {
throw logic_error("non-human player on team before tournament start");
throw std::logic_error("non-human player on team before tournament start");
}
}
if (this->com_deck_index->num_decks() < t->max_players - t->players.size()) {
throw runtime_error("not enough COM decks to complete team");
throw std::runtime_error("not enough COM decks to complete team");
}
// If we allow all-COM teams, or this is a 2v2 tournament and the team has only one human on it, add a COM
if (has_com_teams || !t->players.empty()) {
@@ -687,10 +676,10 @@ void Tournament::send_all_state_updates_on_deletion() const {
}
}
string Tournament::bracket_str() const {
string ret = std::format("Tournament \"{}\"\n", this->name);
std::string Tournament::bracket_str() const {
std::string ret = std::format("Tournament \"{}\"\n", this->name);
function<void(shared_ptr<Match>, size_t)> add_match = [&](shared_ptr<Match> m, size_t indent_level) -> void {
std::function<void(std::shared_ptr<Match>, size_t)> add_match = [&](std::shared_ptr<Match> m, size_t indent_level) -> void {
ret.append(2 * indent_level, ' ');
ret += m->str();
if (this->pending_matches.count(m)) {
@@ -707,13 +696,12 @@ string Tournament::bracket_str() const {
auto en_vm = this->map->version(Language::ENGLISH);
if (en_vm) {
string map_name = en_vm->map->name.decode(en_vm->language);
std::string map_name = en_vm->map->name.decode(en_vm->language);
ret += std::format(" Map: {:08X} ({})\n", this->map->map_number, map_name);
} else {
ret += std::format(" Map: {:08X}\n", this->map->map_number);
}
string rules_str = this->rules.str();
ret += std::format(" Rules: {}\n", rules_str);
ret += std::format(" Rules: {}\n", this->rules.str());
ret += std::format(" Structure: {}, {} entries\n", (this->flags & Flag::IS_2V2) ? "2v2" : "1v1", this->num_teams);
ret += std::format(" COM teams: {}\n", (this->flags & Flag::HAS_COM_TEAMS) ? "allowed" : "forbidden");
ret += std::format(" Shuffle entries: {}\n", (this->flags & Flag::SHUFFLE_ENTRIES) ? "yes" : "no");
@@ -739,14 +727,12 @@ string Tournament::bracket_str() const {
if (this->current_state == State::REGISTRATION) {
ret += " Teams:\n";
for (const auto& team : this->teams) {
string team_str = team->str();
ret += std::format(" {}\n", team_str);
ret += std::format(" {}\n", team->str());
}
} else {
ret += " Pending matches:\n";
for (const auto& match : this->pending_matches) {
string match_str = match->str();
ret += std::format(" {}\n", match_str);
ret += std::format(" {}\n", match->str());
}
}
@@ -755,13 +741,11 @@ string Tournament::bracket_str() const {
}
TournamentIndex::TournamentIndex(
shared_ptr<const MapIndex> map_index,
shared_ptr<const COMDeckIndex> com_deck_index,
const string& state_filename,
std::shared_ptr<const MapIndex> map_index,
std::shared_ptr<const COMDeckIndex> com_deck_index,
const std::string& state_filename,
bool skip_load_state)
: map_index(map_index),
com_deck_index(com_deck_index),
state_filename(state_filename) {
: map_index(map_index), com_deck_index(com_deck_index), state_filename(state_filename) {
if (this->state_filename.empty() || skip_load_state) {
return;
}
@@ -775,14 +759,14 @@ TournamentIndex::TournamentIndex(
if (json.is_list()) {
if (json.size() > 0x20) {
throw runtime_error("tournament phosg::JSON list length is incorrect");
throw std::runtime_error("tournament phosg::JSON list length is incorrect");
}
for (size_t z = 0; z < min<size_t>(json.size(), 0x20); z++) {
for (size_t z = 0; z < std::min<size_t>(json.size(), 0x20); z++) {
if (!json.at(z).is_null()) {
auto tourn = make_shared<Tournament>(this->map_index, this->com_deck_index, json.at(z));
auto tourn = std::make_shared<Tournament>(this->map_index, this->com_deck_index, json.at(z));
tourn->init();
if (!this->name_to_tournament.emplace(tourn->get_name(), tourn).second) {
throw runtime_error("multiple tournaments have the same name: " + tourn->get_name());
throw std::runtime_error("multiple tournaments have the same name: " + tourn->get_name());
}
tourn->set_menu_item_id(this->menu_item_id_to_tournament.size());
this->menu_item_id_to_tournament.emplace_back(tourn);
@@ -790,20 +774,20 @@ TournamentIndex::TournamentIndex(
}
} else if (json.is_dict()) {
if (json.size() > 0x20) {
throw runtime_error("tournament phosg::JSON dict length is incorrect");
throw std::runtime_error("tournament phosg::JSON dict length is incorrect");
}
for (const auto& it : json.as_dict()) {
auto tourn = make_shared<Tournament>(this->map_index, this->com_deck_index, *it.second);
auto tourn = std::make_shared<Tournament>(this->map_index, this->com_deck_index, *it.second);
tourn->init();
if (!this->name_to_tournament.emplace(tourn->get_name(), tourn).second) {
// This is logic_error instead of runtime_error because phosg::JSON dicts already have unique keys
throw logic_error("multiple tournaments have the same name: " + tourn->get_name());
throw std::logic_error("multiple tournaments have the same name: " + tourn->get_name());
}
tourn->set_menu_item_id(this->menu_item_id_to_tournament.size());
this->menu_item_id_to_tournament.emplace_back(tourn);
}
} else {
throw runtime_error("tournament state root phosg::JSON is not a list or dict");
throw std::runtime_error("tournament state root phosg::JSON is not a list or dict");
}
}
@@ -819,20 +803,20 @@ void TournamentIndex::save() const {
phosg::save_file(this->state_filename, json.serialize(phosg::JSON::SerializeOption::FORMAT | phosg::JSON::SerializeOption::HEX_INTEGERS | phosg::JSON::SerializeOption::ESCAPE_CONTROLS_ONLY));
}
shared_ptr<Tournament> TournamentIndex::create_tournament(
const string& name,
shared_ptr<const MapIndex::Map> map,
std::shared_ptr<Tournament> TournamentIndex::create_tournament(
const std::string& name,
std::shared_ptr<const MapIndex::Map> map,
const Rules& rules,
size_t num_teams,
uint8_t flags) {
if (this->name_to_tournament.size() >= 0x20) {
throw runtime_error("there can be at most 32 tournaments at a time");
throw std::runtime_error("there can be at most 32 tournaments at a time");
}
auto t = make_shared<Tournament>(this->map_index, this->com_deck_index, name, map, rules, num_teams, flags);
auto t = std::make_shared<Tournament>(this->map_index, this->com_deck_index, name, map, rules, num_teams, flags);
t->init();
if (!this->name_to_tournament.emplace(t->get_name(), t).second) {
throw runtime_error("a tournament with the same name already exists");
throw std::runtime_error("a tournament with the same name already exists");
}
size_t z;
@@ -852,7 +836,7 @@ shared_ptr<Tournament> TournamentIndex::create_tournament(
return t;
}
bool TournamentIndex::delete_tournament(const string& name) {
bool TournamentIndex::delete_tournament(const std::string& name) {
auto it = this->name_to_tournament.find(name);
if (it == this->name_to_tournament.end()) {
return false;
@@ -869,7 +853,7 @@ bool TournamentIndex::delete_tournament(const string& name) {
return true;
}
shared_ptr<Tournament::Team> TournamentIndex::team_for_account_id(uint32_t account_id) const {
std::shared_ptr<Tournament::Team> TournamentIndex::team_for_account_id(uint32_t account_id) const {
for (const auto& it : this->name_to_tournament) {
const auto& tourn = it.second;
auto team = tourn->team_for_account_id(account_id);
@@ -880,7 +864,7 @@ shared_ptr<Tournament::Team> TournamentIndex::team_for_account_id(uint32_t accou
return nullptr;
}
void TournamentIndex::link_client(shared_ptr<Client> c) {
void TournamentIndex::link_client(std::shared_ptr<Client> c) {
if (!is_ep3(c->version())) {
return;
}
@@ -898,7 +882,7 @@ void TournamentIndex::link_client(shared_ptr<Client> c) {
return;
}
}
throw logic_error("tournament team found for player, but player not found on team");
throw std::logic_error("tournament team found for player, but player not found on team");
} else {
c->ep3_tournament_team.reset();
if (c->version() == Version::GC_EP3) {
+1 -1
View File
@@ -13,7 +13,7 @@
struct Lobby;
class Client;
struct ServerState;
class ServerState;
namespace Episode3 {
-89
View File
@@ -1,89 +0,0 @@
#include "FileContentsCache.hh"
#include <unistd.h>
#include <phosg/Filesystem.hh>
#include <phosg/Time.hh>
using namespace std;
FileContentsCache::FileContentsCache(uint64_t ttl_usecs) : ttl_usecs(ttl_usecs) {}
FileContentsCache::File::File(
const string& name,
string&& data,
uint64_t load_time)
: name(name),
data(make_shared<string>(std::move(data))),
load_time(load_time) {}
shared_ptr<const FileContentsCache::File> FileContentsCache::replace(
const string& name, string&& data, uint64_t t) {
if (t == 0) {
t = phosg::now();
}
auto new_file = make_shared<File>(name, std::move(data), t);
auto emplace_ret = this->name_to_file.emplace(name, new_file);
if (!emplace_ret.second) {
emplace_ret.first->second = new_file;
}
return new_file;
}
shared_ptr<const FileContentsCache::File> FileContentsCache::replace(
const string& name, const void* data, size_t size, uint64_t t) {
string s(reinterpret_cast<const char*>(data), size);
return this->replace(name, std::move(s), t);
}
FileContentsCache::GetResult FileContentsCache::get_or_load(const std::string& name) {
return this->get(name, phosg::load_file);
}
FileContentsCache::GetResult FileContentsCache::get_or_load(const char* name) {
return this->get_or_load(string(name));
}
shared_ptr<const FileContentsCache::File> FileContentsCache::get_or_throw(const std::string& name) {
auto throw_fn = +[](const std::string&) -> string {
throw out_of_range("file missing from cache");
};
return this->get(name, throw_fn).file;
}
shared_ptr<const FileContentsCache::File> FileContentsCache::get_or_throw(const char* name) {
return this->get_or_throw(string(name));
}
FileContentsCache::GetResult FileContentsCache::get(const std::string& name,
std::function<std::string(const std::string&)> generate) {
uint64_t t = phosg::now();
try {
auto& entry = this->name_to_file.at(name);
if (this->ttl_usecs && (t - entry->load_time < this->ttl_usecs)) {
return {entry, false};
}
} catch (const out_of_range& e) {
}
return {this->replace(name, generate(name)), true};
}
FileContentsCache::GetResult FileContentsCache::get(const char* name,
std::function<std::string(const std::string&)> generate) {
return this->get(string(name), generate);
}
shared_ptr<const string> ThreadSafeFileCache::get(
const string& name, std::function<shared_ptr<const string>(const std::string&)> generate) {
try {
shared_lock g(this->lock);
return this->name_to_file.at(name);
} catch (const out_of_range&) {
unique_lock g(this->lock);
auto it = this->name_to_file.find(name);
if (it == this->name_to_file.end()) {
it = this->name_to_file.emplace(name, generate(name)).first;
}
return it->second;
}
}
-124
View File
@@ -1,124 +0,0 @@
#pragma once
#include <functional>
#include <memory>
#include <mutex>
#include <shared_mutex>
#include <string>
#include <unordered_map>
#include <phosg/Time.hh>
class FileContentsCache {
public:
struct File {
std::string name;
std::shared_ptr<const std::string> data;
uint64_t load_time;
File() = delete;
File(const std::string& name, std::string&& contents, uint64_t load_time);
File(const File&) = delete;
File(File&&) = delete;
File& operator=(const File&) = delete;
File& operator=(File&&) = delete;
~File() = default;
};
explicit FileContentsCache(uint64_t ttl_usecs);
FileContentsCache(const FileContentsCache&) = delete;
FileContentsCache(FileContentsCache&&) = delete;
FileContentsCache& operator=(const FileContentsCache&) = delete;
FileContentsCache& operator=(FileContentsCache&&) = delete;
~FileContentsCache() = default;
template <typename NameT>
bool delete_key(NameT key) {
return this->name_to_file.erase(key);
}
std::shared_ptr<const File> replace(const std::string& name, std::string&& data, uint64_t t = 0);
std::shared_ptr<const File> replace(const std::string& name, const void* data, size_t size, uint64_t t = 0);
struct GetResult {
std::shared_ptr<const File> file;
bool generate_called;
};
GetResult get_or_load(const std::string& name);
GetResult get_or_load(const char* name);
std::shared_ptr<const File> get_or_throw(const std::string& name);
std::shared_ptr<const File> get_or_throw(const char* name);
GetResult get(const std::string& name, std::function<std::string(const std::string&)> generate);
GetResult get(const char* name, std::function<std::string(const std::string&)> generate);
template <typename T>
struct GetObjResult {
const T& obj;
std::shared_ptr<const File> data;
bool generate_called;
};
template <typename T, typename NameT>
GetObjResult<T> get_obj_or_load(NameT name) {
auto res = this->get_or_load(name);
if (res.file->data->size() != sizeof(T)) {
throw std::runtime_error("cached string size is incorrect");
}
return {*reinterpret_cast<const T*>(res.file->data->data()), res.file, res.generate_called};
}
template <typename T, typename NameT>
GetObjResult<T> get_obj_or_throw(NameT name) {
auto res = this->get_or_throw(name);
if (res.file->data->size() != sizeof(T)) {
throw std::runtime_error("cached string size is incorrect");
}
return {*reinterpret_cast<const T*>(res.file->data->data()), res.file, res.generate_called};
}
template <typename T, typename NameT>
GetObjResult<T> get_obj(NameT name, std::function<T(const std::string&)> generate) {
uint64_t t = phosg::now();
try {
auto& f = this->name_to_file.at(name);
if (f->data->size() != sizeof(T)) {
throw std::runtime_error("cached string size is incorrect");
}
if (this->ttl_usecs && (t - f->load_time < this->ttl_usecs)) {
return {*reinterpret_cast<const T*>(f->data->data()), f, false};
}
} catch (const std::out_of_range& e) {
}
T value = generate(name);
auto ret = this->replace_obj(name, value);
ret.generate_called = true;
return ret;
}
template <typename T, typename NameT>
GetObjResult<T> replace_obj(NameT name, const T& value) {
auto cached_value = this->replace(name, &value, sizeof(value));
return {*reinterpret_cast<const T*>(cached_value->data->data()), cached_value, false};
}
private:
std::unordered_map<std::string, std::shared_ptr<File>> name_to_file;
uint64_t ttl_usecs;
};
class ThreadSafeFileCache {
public:
explicit ThreadSafeFileCache() = default;
ThreadSafeFileCache(const ThreadSafeFileCache&) = delete;
ThreadSafeFileCache(ThreadSafeFileCache&&) = delete;
ThreadSafeFileCache& operator=(const ThreadSafeFileCache&) = delete;
ThreadSafeFileCache& operator=(ThreadSafeFileCache&&) = delete;
~ThreadSafeFileCache() = default;
// generate() is called while the lock is held for writing, so it will block other threads.
std::shared_ptr<const std::string> get(
const std::string& name, std::function<std::shared_ptr<const std::string>(const std::string&)> generate);
private:
std::shared_mutex lock;
std::unordered_map<std::string, std::shared_ptr<const std::string>> name_to_file;
};
-612
View File
@@ -1,612 +0,0 @@
#include "FunctionCompiler.hh"
#include <stdio.h>
#include <string.h>
#include <filesystem>
#include <phosg/Filesystem.hh>
#include <phosg/Hash.hh>
#include <phosg/Time.hh>
#include <stdexcept>
#include <resource_file/Emulators/PPC32Emulator.hh>
#include <resource_file/Emulators/SH4Emulator.hh>
#include <resource_file/Emulators/X86Emulator.hh>
#include "CommandFormats.hh"
#include "CommonFileFormats.hh"
#include "Compression.hh"
#include "Loggers.hh"
using namespace std;
const char* name_for_architecture(CompiledFunctionCode::Architecture arch) {
switch (arch) {
case CompiledFunctionCode::Architecture::POWERPC:
return "PowerPC";
case CompiledFunctionCode::Architecture::X86:
return "x86";
case CompiledFunctionCode::Architecture::SH4:
return "SH-4";
default:
throw logic_error("invalid architecture");
}
}
template <bool BE>
string CompiledFunctionCode::generate_client_command_t(
const unordered_map<string, uint32_t>& label_writes,
const void* suffix_data,
size_t suffix_size,
uint32_t override_relocations_offset) const {
using FooterT = RELFileFooterT<BE>;
FooterT footer;
footer.num_relocations = this->relocation_deltas.size();
footer.unused1.clear(0);
footer.root_offset = this->entrypoint_offset_offset;
footer.unused2.clear(0);
phosg::StringWriter w;
if (!label_writes.empty()) {
string modified_code = this->code;
for (const auto& it : label_writes) {
size_t offset = this->label_offsets.at(it.first);
if (offset > modified_code.size() - 4) {
throw runtime_error("label out of range");
}
*reinterpret_cast<U32T<FooterT::IsBE>*>(modified_code.data() + offset) = it.second;
}
w.write(modified_code);
} else {
w.write(this->code);
}
if (suffix_size) {
w.write(suffix_data, suffix_size);
}
while (w.size() & 3) {
w.put_u8(0);
}
footer.relocations_offset = w.size();
// Always write at least 4 bytes even if there are no relocations
if (this->relocation_deltas.empty()) {
w.put_u32(0);
}
if (override_relocations_offset) {
footer.relocations_offset = override_relocations_offset;
} else {
for (uint16_t delta : this->relocation_deltas) {
w.put<U16T<FooterT::IsBE>>(delta);
}
if (this->relocation_deltas.size() & 1) {
w.put_u16(0);
}
}
w.put(footer);
return std::move(w.str());
}
string CompiledFunctionCode::generate_client_command(
const unordered_map<string, uint32_t>& label_writes,
const void* suffix_data,
size_t suffix_size,
uint32_t override_relocations_offset) const {
if (this->arch == Architecture::POWERPC) {
return this->generate_client_command_t<true>(label_writes, suffix_data, suffix_size, override_relocations_offset);
} else if ((this->arch == Architecture::X86) || (this->arch == Architecture::SH4)) {
return this->generate_client_command_t<false>(label_writes, suffix_data, suffix_size, override_relocations_offset);
} else {
throw logic_error("invalid architecture");
}
}
bool CompiledFunctionCode::is_big_endian() const {
return (this->arch == Architecture::POWERPC);
}
static unordered_map<uint32_t, std::string> preprocess_function_code(const std::string& text) {
auto parse_specific_version_list = +[](std::string&& text) -> vector<uint32_t> {
phosg::strip_whitespace(text);
vector<uint32_t> ret;
for (auto& vers_token : phosg::split(text, ' ')) {
phosg::strip_whitespace(vers_token);
if (vers_token.empty()) {
continue;
}
if (vers_token.size() != 4) {
throw std::runtime_error("invalid specific_version: " + vers_token);
}
ret.emplace_back(*reinterpret_cast<const be_uint32_t*>(vers_token.data()));
}
return ret;
};
// Find a .versions directive and populate specific_versions
vector<uint32_t> specific_versions;
auto lines = phosg::split(text, '\n');
for (auto& line : lines) {
if (line.starts_with(".versions ")) {
if (!specific_versions.empty()) {
throw std::runtime_error("multiple .versions directives in file");
}
specific_versions = parse_specific_version_list(line.substr(10));
if (specific_versions.empty()) {
throw std::runtime_error(".versions directive does not specify any versions");
}
line.clear();
}
}
// If there's no .versions directive, just return the text as-is
if (specific_versions.empty()) {
return {{0, std::move(text)}};
}
vector<deque<string>> version_lines;
version_lines.resize(specific_versions.size());
size_t line_num = 1;
vector<uint32_t> current_only_versions;
unordered_set<uint32_t> current_only_versions_set;
auto add_blank_line = [&]() -> void {
for (size_t vers_index = 0; vers_index < specific_versions.size(); vers_index++) {
version_lines[vers_index].emplace_back("");
}
};
for (auto& line : lines) {
phosg::strip_whitespace(line);
if (line.starts_with(".only_versions ")) {
current_only_versions = parse_specific_version_list(line.substr(15));
current_only_versions_set.clear();
for (uint32_t specific_version : current_only_versions) {
current_only_versions_set.emplace(specific_version);
}
add_blank_line();
} else if (line == ".all_versions") {
current_only_versions.clear();
current_only_versions_set.clear();
add_blank_line();
} else {
size_t vers_offset = line.find("<VERS ");
if (vers_offset == string::npos) {
for (size_t vers_index = 0; vers_index < specific_versions.size(); vers_index++) {
if (current_only_versions.empty() || current_only_versions_set.count(specific_versions[vers_index])) {
version_lines[vers_index].emplace_back(line);
} else {
version_lines[vers_index].emplace_back("");
}
}
} else {
size_t token_index = 0;
for (size_t vers_index = 0; vers_index < specific_versions.size(); vers_index++) {
if (current_only_versions.empty() || current_only_versions_set.count(specific_versions[vers_index])) {
string version_line = line;
size_t vers_offset = line.find("<VERS ");
while (vers_offset != string::npos) {
size_t end_offset = version_line.find('>', vers_offset + 6);
if (end_offset == string::npos) {
throw runtime_error(std::format("(line {}) unterminated <VERS> replacement", line_num));
}
auto tokens = phosg::split(version_line.substr(vers_offset + 6, end_offset - vers_offset - 6), ' ');
if (tokens.size() <= token_index) {
throw runtime_error(std::format("(line {}) invalid <VERS> replacement", line_num));
}
version_line = version_line.substr(0, vers_offset) + tokens.at(token_index) + version_line.substr(end_offset + 1);
vers_offset = version_line.find("<VERS ");
}
version_lines[vers_index].emplace_back(version_line);
token_index++;
} else {
version_lines[vers_index].emplace_back("");
}
}
}
}
line_num++;
}
unordered_map<uint32_t, string> ret;
for (size_t z = 0; z < specific_versions.size(); z++) {
ret.emplace(specific_versions[z], phosg::join(version_lines.at(z), "\n"));
}
return ret;
}
static vector<shared_ptr<CompiledFunctionCode>> compile_function_code(
CompiledFunctionCode::Architecture arch,
const string& function_directory,
const string& system_directory,
const string& name,
const string& text,
bool raise_on_any_failure) {
unordered_set<string> get_include_stack;
function<string(const string&)> get_include = [&](const string& name) -> string {
const char* arch_name_token;
switch (arch) {
case CompiledFunctionCode::Architecture::POWERPC:
arch_name_token = "ppc";
break;
case CompiledFunctionCode::Architecture::X86:
arch_name_token = "x86";
break;
case CompiledFunctionCode::Architecture::SH4:
arch_name_token = "sh4";
break;
default:
throw runtime_error("unknown architecture");
}
// Look in the function directory first, then the system directory
string asm_filename = std::format("{}/{}.{}.inc.s", function_directory, name, arch_name_token);
if (!std::filesystem::is_regular_file(asm_filename)) {
asm_filename = std::format("{}/{}.{}.inc.s", system_directory, name, arch_name_token);
}
if (std::filesystem::is_regular_file(asm_filename)) {
if (!get_include_stack.emplace(name).second) {
throw runtime_error("mutual recursion between includes: " + name);
}
ResourceDASM::EmulatorBase::AssembleResult ret;
switch (arch) {
case CompiledFunctionCode::Architecture::POWERPC:
ret = ResourceDASM::PPC32Emulator::assemble(phosg::load_file(asm_filename), get_include);
break;
case CompiledFunctionCode::Architecture::X86:
ret = ResourceDASM::X86Emulator::assemble(phosg::load_file(asm_filename), get_include);
break;
case CompiledFunctionCode::Architecture::SH4:
ret = ResourceDASM::SH4Emulator::assemble(phosg::load_file(asm_filename), get_include);
break;
default:
throw runtime_error("unknown architecture");
}
get_include_stack.erase(name);
return ret.code;
}
string bin_filename = function_directory + "/" + name + ".inc.bin";
if (std::filesystem::is_regular_file(bin_filename)) {
return phosg::load_file(bin_filename);
}
bin_filename = system_directory + "/" + name + ".inc.bin";
if (std::filesystem::is_regular_file(bin_filename)) {
return phosg::load_file(bin_filename);
}
throw runtime_error("data not found for include: " + name + " (from " + asm_filename + " or " + bin_filename + ")");
};
auto version_texts = preprocess_function_code(text);
vector<shared_ptr<CompiledFunctionCode>> ret;
for (const auto& [specific_version, version_text] : version_texts) {
try {
ResourceDASM::EmulatorBase::AssembleResult assembled;
if (arch == CompiledFunctionCode::Architecture::POWERPC) {
assembled = ResourceDASM::PPC32Emulator::assemble(version_text, get_include);
} else if (arch == CompiledFunctionCode::Architecture::X86) {
assembled = ResourceDASM::X86Emulator::assemble(version_text, get_include);
} else if (arch == CompiledFunctionCode::Architecture::SH4) {
assembled = ResourceDASM::SH4Emulator::assemble(version_text, get_include);
} else {
throw runtime_error("invalid architecture");
}
auto compiled = ret.emplace_back(make_shared<CompiledFunctionCode>());
compiled->arch = arch;
compiled->short_name = name;
compiled->specific_version = specific_version;
compiled->code = std::move(assembled.code);
compiled->label_offsets = std::move(assembled.label_offsets);
for (const auto& it : assembled.metadata_keys) {
if (it.first == "hide_from_patches_menu") {
compiled->hide_from_patches_menu = true;
} else if (it.first == "name") {
compiled->long_name = it.second;
} else if (it.first == "description") {
compiled->description = it.second;
} else if (it.first == "client_flag") {
compiled->client_flag = stoull(it.second, nullptr, 0);
} else if (it.first == "show_return_value") {
compiled->show_return_value = true;
} else {
throw runtime_error("unknown metadata key: " + it.first);
}
}
set<uint32_t> reloc_indexes;
for (const auto& it : compiled->label_offsets) {
if (it.first.starts_with("reloc")) {
reloc_indexes.emplace(it.second / 4);
}
}
try {
compiled->entrypoint_offset_offset = compiled->label_offsets.at("entry_ptr");
} catch (const out_of_range&) {
throw runtime_error("code does not contain entry_ptr label");
}
uint32_t prev_index = 0;
for (const auto& it : reloc_indexes) {
uint32_t delta = it - prev_index;
if (delta > 0xFFFF) {
throw runtime_error("relocation delta too far away");
}
compiled->relocation_deltas.emplace_back(delta);
prev_index = it;
}
} catch (const exception& e) {
string version_str = specific_version ? (" (" + str_for_specific_version(specific_version) + ")") : "";
if (raise_on_any_failure) {
throw;
}
function_compiler_log.warning_f("Failed to compile function {}{}: {}", name, version_str, e.what());
}
}
return ret;
}
FunctionCodeIndex::FunctionCodeIndex(const string& directory, bool raise_on_any_failure) {
string system_dir_path = directory.ends_with("/") ? (directory + "System") : (directory + "/System");
uint32_t next_menu_item_id = 1;
for (const auto& item : std::filesystem::directory_iterator(directory)) {
string subdir_name = item.path().filename().string();
string subdir_path = directory.ends_with("/") ? (directory + subdir_name) : (directory + "/" + subdir_name);
auto add_file = [&](string filename) -> void {
try {
if (!filename.ends_with(".s")) {
return;
}
string name = filename.substr(0, filename.size() - 2);
if (name.ends_with(".inc")) {
return;
}
bool is_patch = name.ends_with(".patch");
if (is_patch) {
name.resize(name.size() - 6);
}
// Figure out the version or specific_version
CompiledFunctionCode::Architecture arch = CompiledFunctionCode::Architecture::UNKNOWN;
uint32_t specific_version = 0;
string short_name = name;
if (name.ends_with(".ppc")) {
arch = CompiledFunctionCode::Architecture::POWERPC;
name.resize(name.size() - 4);
short_name = name;
} else if (name.ends_with(".x86")) {
arch = CompiledFunctionCode::Architecture::X86;
name.resize(name.size() - 4);
short_name = name;
} else if (name.ends_with(".sh4")) {
arch = CompiledFunctionCode::Architecture::SH4;
name.resize(name.size() - 4);
short_name = name;
} else if (is_patch && (name.size() >= 5) && (name[name.size() - 5] == '.')) {
specific_version = (name[name.size() - 4] << 24) | (name[name.size() - 3] << 16) | (name[name.size() - 2] << 8) | name[name.size() - 1];
if (specific_version_is_dc(specific_version)) {
arch = CompiledFunctionCode::Architecture::SH4;
} else if (specific_version_is_gc(specific_version)) {
arch = CompiledFunctionCode::Architecture::POWERPC;
} else if (specific_version_is_pc_v2(specific_version) ||
specific_version_is_xb(specific_version) ||
specific_version_is_bb(specific_version)) {
arch = CompiledFunctionCode::Architecture::X86;
} else {
throw runtime_error("unable to determine architecture from specific_version");
}
short_name = name.substr(0, name.size() - 5);
}
if (arch == CompiledFunctionCode::Architecture::UNKNOWN) {
throw runtime_error("unable to determine architecture");
}
string path = subdir_path + "/" + filename;
string text = phosg::load_file(path);
for (auto code : compile_function_code(arch, subdir_path, system_dir_path, name, text, raise_on_any_failure)) {
if (code->specific_version == 0) {
code->specific_version = specific_version;
}
code->source_path = path;
code->short_name = short_name;
this->name_to_function.emplace(name, code);
if (is_patch) {
code->menu_item_id = next_menu_item_id++;
this->menu_item_id_and_specific_version_to_patch_function.emplace(
static_cast<uint64_t>(code->menu_item_id) << 32 | code->specific_version, code);
this->name_and_specific_version_to_patch_function.emplace(
std::format("{}-{:08X}", code->short_name, code->specific_version), code);
}
string patch_prefix = is_patch ? std::format("[{:08X}] ", code->menu_item_id) : "";
function_compiler_log.debug_f("Compiled function {}{} ({}; {})",
patch_prefix, name, str_for_specific_version(code->specific_version), name_for_architecture(code->arch));
}
} catch (const exception& e) {
if (raise_on_any_failure) {
throw runtime_error(format("({}) {}", filename, e.what()));
}
function_compiler_log.warning_f("Failed to compile function {}: {}", filename, e.what());
}
};
if (std::filesystem::is_regular_file(subdir_path)) {
add_file(subdir_path);
} else if (std::filesystem::is_directory(subdir_path)) {
for (const auto& item : std::filesystem::directory_iterator(subdir_path)) {
string filename = item.path().filename().string();
add_file(filename);
}
} else {
function_compiler_log.warning_f("Skipping {} (unknown file type)", subdir_name);
continue;
}
}
}
shared_ptr<const Menu> FunctionCodeIndex::patch_switches_menu(
uint32_t specific_version,
const std::unordered_set<std::string>& server_auto_patches_enabled,
const std::unordered_set<std::string>& client_auto_patches_enabled) const {
auto suffix = std::format("-{:08X}", specific_version);
auto ret = make_shared<Menu>(MenuID::PATCH_SWITCHES, "Patches");
ret->items.emplace_back(PatchesMenuItemID::GO_BACK, "Go back", "Return to the\nmain menu", 0);
for (const auto& it : this->name_and_specific_version_to_patch_function) {
const auto& fn = it.second;
if (fn->hide_from_patches_menu || !it.first.ends_with(suffix) || server_auto_patches_enabled.count(fn->short_name)) {
continue;
}
string name;
name.push_back(client_auto_patches_enabled.count(fn->short_name) ? '*' : '-');
name += fn->long_name.empty() ? fn->short_name : fn->long_name;
ret->items.emplace_back(fn->menu_item_id, name, fn->description, MenuItem::Flag::REQUIRES_SEND_FUNCTION_CALL_RUNS_CODE);
}
return ret;
}
bool FunctionCodeIndex::patch_menu_empty(uint32_t specific_version) const {
uint32_t mask = specific_version_is_indeterminate(specific_version) ? 0xFF000000 : 0xFFFFFFFF;
for (const auto& it : this->menu_item_id_and_specific_version_to_patch_function) {
if ((it.first & mask) == (specific_version & mask)) {
return false;
}
}
return true;
}
std::shared_ptr<const CompiledFunctionCode> FunctionCodeIndex::get_patch(
const std::string& name, uint32_t specific_version) const {
return this->name_and_specific_version_to_patch_function.at(std::format("{}-{:08X}", name, specific_version));
}
DOLFileIndex::DOLFileIndex(const string& directory) {
if (!std::filesystem::is_directory(directory)) {
function_compiler_log.info_f("DOL file directory is missing");
return;
}
auto menu = make_shared<Menu>(MenuID::PROGRAMS, "Programs");
this->menu = menu;
menu->items.emplace_back(ProgramsMenuItemID::GO_BACK, "Go back", "Return to the\nmain menu", 0);
uint32_t next_menu_item_id = 0;
for (const auto& item : std::filesystem::directory_iterator(directory)) {
string filename = item.path().filename().string();
bool is_dol = filename.ends_with(".dol");
bool is_compressed_dol = filename.ends_with(".dol.prs");
if (!is_dol && !is_compressed_dol) {
continue;
}
string name = filename.substr(0, filename.size() - (is_compressed_dol ? 8 : 4));
try {
auto dol = make_shared<File>();
dol->menu_item_id = next_menu_item_id++;
dol->name = name;
string path = directory + "/" + filename;
string file_data = phosg::load_file(path);
string description;
if (is_compressed_dol) {
size_t decompressed_size = prs_decompress_size(file_data);
phosg::StringWriter w;
w.put_u32b(file_data.size());
w.put_u32b(decompressed_size);
w.write(file_data);
while (w.size() & 3) {
w.put_u8(0);
}
dol->data = std::move(w.str());
string compressed_size_str = phosg::format_size(file_data.size());
string decompressed_size_str = phosg::format_size(decompressed_size);
function_compiler_log.debug_f("Loaded compressed DOL file {} ({} -> {})",
dol->name, compressed_size_str, decompressed_size_str);
description = std::format("$C6{}$C7\n{}\n{} (orig)", dol->name, compressed_size_str, decompressed_size_str);
} else {
phosg::StringWriter w;
w.put_u32b(0);
w.put_u32b(file_data.size());
w.write(file_data);
while (w.size() & 3) {
w.put_u8(0);
}
dol->data = std::move(w.str());
string size_str = phosg::format_size(dol->data.size());
function_compiler_log.debug_f("Loaded DOL file {} ({})", filename, size_str);
description = std::format("$C6{}$C7\n{}", dol->name, size_str);
}
this->name_to_file.emplace(dol->name, dol);
this->item_id_to_file.emplace_back(dol);
menu->items.emplace_back(dol->menu_item_id, dol->name, description, MenuItem::Flag::REQUIRES_SEND_FUNCTION_CALL_RUNS_CODE);
} catch (const exception& e) {
function_compiler_log.warning_f("Failed to load DOL file {}: {}", filename, e.what());
}
}
}
uint32_t specific_version_for_gc_header_checksum(uint32_t header_checksum) {
static unordered_map<uint32_t, uint32_t> checksum_to_specific_version;
if (checksum_to_specific_version.empty()) {
struct {
char system_code = 'G';
char game_code1 = 'P';
char game_code2;
char region_code;
char developer_code1 = '8';
char developer_code2 = 'P';
uint8_t disc_number = 0;
uint8_t version_code;
} __attribute__((packed)) data;
for (const char* game_code2 = "OS"; *game_code2; game_code2++) {
data.game_code2 = *game_code2;
for (const char* region_code = "JEP"; *region_code; region_code++) {
data.region_code = *region_code;
for (uint8_t version_code = 0; version_code < 8; version_code++) {
data.version_code = version_code;
uint32_t checksum = phosg::crc32(&data, sizeof(data));
uint32_t specific_version = 0x33000030 | (*game_code2 << 16) | (*region_code << 8) | version_code;
if (!checksum_to_specific_version.emplace(checksum, specific_version).second) {
throw logic_error("multiple specific_versions have same header checksum");
}
}
}
{
// Generate entries for Trial Editions
data.region_code = 'J';
data.system_code = 'D';
data.version_code = 0;
uint32_t checksum = phosg::crc32(&data, sizeof(data));
uint32_t specific_version = 0x33004A54 | (*game_code2 << 16);
if (!checksum_to_specific_version.emplace(checksum, specific_version).second) {
throw logic_error("multiple specific_versions have same header checksum");
}
data.system_code = 'G';
}
}
}
return checksum_to_specific_version.at(header_checksum);
}
-95
View File
@@ -1,95 +0,0 @@
#pragma once
#include <inttypes.h>
#include <map>
#include <memory>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include "Menu.hh"
// TODO: Support x86 and SH4 function calls in the future. Currently we only
// support PPC32 because I haven't written an appropriate x86 assembler yet.
struct CompiledFunctionCode {
enum class Architecture {
UNKNOWN = 0,
POWERPC, // GC
X86, // PC, XB, BB
SH4, // Dreamcast
};
Architecture arch;
std::string code;
std::vector<uint16_t> relocation_deltas;
std::unordered_map<std::string, uint32_t> label_offsets;
uint32_t entrypoint_offset_offset = 0;
std::string source_path; // Path to source file from newserv root
std::string short_name; // Based on filename
std::string long_name; // From .meta name directive
std::string description; // From .meta description directive
uint64_t client_flag = 0; // From .meta client_flag directive
uint32_t menu_item_id = 0;
bool hide_from_patches_menu = false;
bool show_return_value = false;
uint32_t specific_version = 0; // 0 = not a client-selectable patch
bool is_big_endian() const;
template <bool BE>
std::string generate_client_command_t(
const std::unordered_map<std::string, uint32_t>& label_writes,
const void* suffix_data = nullptr,
size_t suffix_size = 0,
uint32_t override_relocations_offset = 0) const;
std::string generate_client_command(
const std::unordered_map<std::string, uint32_t>& label_writes = {},
const void* suffix_data = nullptr,
size_t suffix_size = 0,
uint32_t override_relocations_offset = 0) const;
};
const char* name_for_architecture(CompiledFunctionCode::Architecture arch);
struct FunctionCodeIndex {
FunctionCodeIndex() = default;
FunctionCodeIndex(const std::string& directory, bool raise_on_any_failure);
std::unordered_map<std::string, std::shared_ptr<CompiledFunctionCode>> name_to_function;
std::unordered_map<uint8_t, std::shared_ptr<CompiledFunctionCode>> index_to_function;
std::unordered_map<uint64_t, std::shared_ptr<CompiledFunctionCode>> menu_item_id_and_specific_version_to_patch_function;
// Key here is e.g. "PATCHNAME-SPECIFICVERSION", with the latter in hex
std::map<std::string, std::shared_ptr<CompiledFunctionCode>> name_and_specific_version_to_patch_function;
std::shared_ptr<const Menu> patch_switches_menu(
uint32_t specific_version,
const std::unordered_set<std::string>& server_auto_patches_enabled,
const std::unordered_set<std::string>& client_auto_patches_enabled) const;
bool patch_menu_empty(uint32_t specific_version) const;
std::shared_ptr<const CompiledFunctionCode> get_patch(const std::string& name, uint32_t specific_version) const;
};
struct DOLFileIndex {
struct File {
uint32_t menu_item_id;
std::string name;
std::string data;
bool is_compressed;
};
std::vector<std::shared_ptr<File>> item_id_to_file;
std::unordered_map<std::string, std::shared_ptr<File>> name_to_file;
std::shared_ptr<const Menu> menu;
DOLFileIndex() = default;
explicit DOLFileIndex(const std::string& directory);
inline bool empty() const {
return this->name_to_file.empty() && this->item_id_to_file.empty();
}
};
uint32_t specific_version_for_gc_header_checksum(uint32_t header_checksum);
+15 -17
View File
@@ -7,8 +7,6 @@
#include "Text.hh"
#include "Types.hh"
using namespace std;
template <bool BE>
struct GSLHeaderEntryT {
pstring<TextEncoding::ASCII, 0x20> filename;
@@ -30,13 +28,13 @@ void GSLArchive::load_t() {
}
uint64_t offset = static_cast<uint64_t>(entry.offset) * 0x800;
if (offset + entry.size > this->data->size()) {
throw runtime_error("GSL entry extends beyond end of data");
throw std::runtime_error("GSL entry extends beyond end of data");
}
this->entries.emplace(entry.filename.decode(), Entry{offset, entry.size});
}
}
GSLArchive::GSLArchive(shared_ptr<const string> data, bool big_endian) : data(data) {
GSLArchive::GSLArchive(std::shared_ptr<const std::string> data, bool big_endian) : data(data) {
if (big_endian) {
this->load_t<true>();
} else {
@@ -44,43 +42,43 @@ GSLArchive::GSLArchive(shared_ptr<const string> data, bool big_endian) : data(da
}
}
const unordered_map<string, GSLArchive::Entry> GSLArchive::all_entries() const {
const std::unordered_map<std::string, GSLArchive::Entry> GSLArchive::all_entries() const {
return this->entries;
}
pair<const void*, size_t> GSLArchive::get(const std::string& name) const {
std::pair<const void*, size_t> GSLArchive::get(const std::string& name) const {
try {
const auto& entry = this->entries.at(name);
return make_pair(this->data->data() + entry.offset, entry.size);
} catch (const out_of_range&) {
throw out_of_range("GSL does not contain file: " + name);
return std::make_pair(this->data->data() + entry.offset, entry.size);
} catch (const std::out_of_range&) {
throw std::out_of_range("GSL does not contain file: " + name);
}
}
string GSLArchive::get_copy(const string& name) const {
std::string GSLArchive::get_copy(const std::string& name) const {
try {
const auto& entry = this->entries.at(name);
return this->data->substr(entry.offset, entry.size);
} catch (const out_of_range&) {
throw out_of_range("GSL does not contain file: " + name);
} catch (const std::out_of_range&) {
throw std::out_of_range("GSL does not contain file: " + name);
}
}
phosg::StringReader GSLArchive::get_reader(const string& name) const {
phosg::StringReader GSLArchive::get_reader(const std::string& name) const {
try {
const auto& entry = this->entries.at(name);
return phosg::StringReader(this->data->data() + entry.offset, entry.size);
} catch (const out_of_range&) {
throw out_of_range("GSL does not contain file: " + name);
} catch (const std::out_of_range&) {
throw std::out_of_range("GSL does not contain file: " + name);
}
}
string GSLArchive::generate(const unordered_map<string, string>& files, bool big_endian) {
std::string GSLArchive::generate(const std::unordered_map<std::string, std::string>& files, bool big_endian) {
return big_endian ? GSLArchive::generate_t<true>(files) : GSLArchive::generate_t<false>(files);
}
template <bool BE>
string GSLArchive::generate_t(const unordered_map<string, string>& files) {
std::string GSLArchive::generate_t(const std::unordered_map<std::string, std::string>& files) {
phosg::StringWriter w;
// Make sure there's enough space for a blank header entry before any file's data pages begin
+80 -29
View File
@@ -1,4 +1,42 @@
#include "GameServer.hh"
#include <stdexcept>
static uint8_t account_client_source_for_version(Version v) {
switch (v) {
case Version::DC_NTE:
case Version::DC_11_2000:
case Version::DC_V1:
case Version::DC_V2:
return 1;
case Version::PC_PATCH:
case Version::PC_NTE:
case Version::PC_V2:
return 2;
case Version::GC_NTE:
case Version::GC_V3:
case Version::GC_EP3_NTE:
case Version::GC_EP3:
return 3;
case Version::XB_V3:
return 4;
case Version::BB_PATCH:
case Version::BB_V4:
return 5;
default:
throw std::logic_error("invalid game version for account client source");
}
}
static uint64_t account_client_source_key(uint32_t account_id, Version v) {
return (static_cast<uint64_t>(account_id) << 8) | account_client_source_for_version(v);
}
#include <ctype.h>
#include <errno.h>
@@ -18,14 +56,11 @@
#include "PSOProtocol.hh"
#include "ReceiveCommands.hh"
using namespace std;
using namespace std::placeholders;
GameServer::GameServer(shared_ptr<ServerState> state) : Server(state->io_context, "[GameServer] "), state(state) {}
GameServer::GameServer(std::shared_ptr<ServerState> state) : Server(state->io_context, "[GameServer] "), state(state) {}
void GameServer::listen(
const std::string& name,
const string& addr,
const std::string& addr,
uint16_t port,
Version version,
ServerBehavior behavior) {
@@ -34,7 +69,7 @@ void GameServer::listen(
}
asio::ip::address asio_addr = addr.empty() ? asio::ip::address_v4::any() : asio::ip::make_address(addr);
auto sock = make_shared<GameServerSocket>();
auto sock = std::make_shared<GameServerSocket>();
sock->name = name;
sock->endpoint = asio::ip::tcp::endpoint(asio_addr, port);
sock->version = version;
@@ -42,8 +77,8 @@ void GameServer::listen(
this->add_socket(std::move(sock));
}
shared_ptr<Client> GameServer::connect_channel(shared_ptr<Channel> ch, uint16_t port, ServerBehavior initial_state) {
auto c = make_shared<Client>(this->shared_from_this(), ch, initial_state);
std::shared_ptr<Client> GameServer::connect_channel(std::shared_ptr<Channel> ch, uint16_t port, ServerBehavior initial_state) {
auto c = std::make_shared<Client>(this->shared_from_this(), ch, initial_state);
c->listener_port = port;
this->log.info_f("Client connected: C-{:X} via TSI-{}-{}-{}",
@@ -53,31 +88,31 @@ shared_ptr<Client> GameServer::connect_channel(shared_ptr<Channel> ch, uint16_t
return c;
}
shared_ptr<Client> GameServer::get_client() const {
std::shared_ptr<Client> GameServer::get_client() const {
if (this->clients.empty()) {
throw runtime_error("no clients on game server");
throw std::runtime_error("no clients on game server");
}
if (this->clients.size() > 1) {
throw runtime_error("multiple clients on game server");
throw std::runtime_error("multiple clients on game server");
}
return *this->clients.begin();
}
vector<shared_ptr<Client>> GameServer::get_clients_by_identifier(const string& ident) const {
std::vector<std::shared_ptr<Client>> GameServer::get_clients_by_identifier(const std::string& ident) const {
int64_t account_id_hex = -1;
int64_t account_id_dec = -1;
try {
account_id_dec = stoul(ident, nullptr, 10);
} catch (const invalid_argument&) {
} catch (const std::invalid_argument&) {
}
try {
account_id_hex = stoul(ident, nullptr, 16);
} catch (const invalid_argument&) {
} catch (const std::invalid_argument&) {
}
// TODO: It's kind of not great that we do a linear search here, but this is only used in the shell, so it should be
// pretty rare.
vector<shared_ptr<Client>> results;
std::vector<std::shared_ptr<Client>> results;
for (const auto& c : this->clients) {
if (c->login && c->login->account->account_id == account_id_hex) {
results.emplace_back(c);
@@ -97,7 +132,7 @@ vector<shared_ptr<Client>> GameServer::get_clients_by_identifier(const string& i
}
auto p = c->character_file(false, false);
if (p && p->disp.name.eq(ident, p->inventory.language)) {
if (p && p->disp.visual.name.eq(ident, p->inventory.language)) {
results.emplace_back(c);
continue;
}
@@ -115,10 +150,10 @@ vector<shared_ptr<Client>> GameServer::get_clients_by_identifier(const string& i
return results;
}
shared_ptr<Client> GameServer::create_client(
shared_ptr<GameServerSocket> listen_sock, asio::ip::tcp::socket&& client_sock) {
std::shared_ptr<Client> GameServer::create_client(
std::shared_ptr<GameServerSocket> listen_sock, asio::ip::tcp::socket&& client_sock) {
uint32_t addr = ipv4_addr_for_asio_addr(client_sock.remote_endpoint().address());
if (this->state->banned_ipv4_ranges->check(addr)) {
if (this->state->data->banned_ipv4_ranges->check(addr)) {
if (client_sock.is_open()) {
client_sock.close();
}
@@ -127,34 +162,37 @@ shared_ptr<Client> GameServer::create_client(
auto channel = SocketChannel::create(
this->io_context,
make_unique<asio::ip::tcp::socket>(std::move(client_sock)),
std::make_unique<asio::ip::tcp::socket>(std::move(client_sock)),
listen_sock->version,
Language::ENGLISH,
"",
phosg::TerminalFormat::FG_YELLOW,
phosg::TerminalFormat::FG_GREEN);
auto c = make_shared<Client>(this->shared_from_this(), channel, listen_sock->behavior);
phosg::TerminalFormat::FG_GREEN,
this->state->data->censor_credentials,
false);
auto c = std::make_shared<Client>(this->shared_from_this(), channel, listen_sock->behavior);
c->listener_port = listen_sock->endpoint.port();
this->log.info_f("Client connected: C-{:X} via {}", c->id, listen_sock->name);
return c;
}
asio::awaitable<void> GameServer::handle_client_command(shared_ptr<Client> c, unique_ptr<Channel::Message> msg) {
asio::awaitable<void> GameServer::handle_client_command(
std::shared_ptr<Client> c, std::unique_ptr<Channel::Message> msg) {
try {
co_await on_command(c, std::move(msg));
} catch (const exception& e) {
} catch (const std::exception& e) {
this->log.warning_f("Error processing client command: {}", e.what());
c->channel->disconnect();
}
}
asio::awaitable<void> GameServer::handle_client(shared_ptr<Client> c) {
asio::awaitable<void> GameServer::handle_client(std::shared_ptr<Client> c) {
auto g = phosg::on_close_scope(std::bind(&Client::cancel_pending_promises, c.get()));
try {
co_await on_connect(c);
} catch (const exception& e) {
} catch (const std::exception& e) {
this->log.warning_f("Error in client initialization: {}", e.what());
c->channel->disconnect();
}
@@ -181,18 +219,31 @@ asio::awaitable<void> GameServer::destroy_client(std::shared_ptr<Client> c) {
try {
co_await on_disconnect(c);
} catch (const exception& e) {
} catch (const std::exception& e) {
this->log.warning_f("Error during client disconnect cleanup: {}", e.what());
}
// Note: It's important to move the disconnect hooks out of the client here because the hooks could modify
// c->disconnect_hooks while it's being iterated here, which would invalidate these iterators.
unordered_map<string, function<void()>> hooks = std::move(c->disconnect_hooks);
std::unordered_map<std::string, std::function<void()>> hooks = std::move(c->disconnect_hooks);
for (auto h_it : hooks) {
try {
h_it.second();
} catch (const exception& e) {
} catch (const std::exception& e) {
c->log.warning_f("Disconnect hook {} failed: {}", h_it.first, e.what());
}
}
if (c->login) {
auto it = this->state->client_for_account.find(c->login->account->account_id);
if ((it != this->state->client_for_account.end()) && (it->second == c)) {
this->state->client_for_account.erase(it);
}
uint64_t source_key = account_client_source_key(c->login->account->account_id, c->version());
auto source_it = this->state->client_for_account_source.find(source_key);
if ((source_it != this->state->client_for_account_source.end()) && (source_it->second == c)) {
this->state->client_for_account_source.erase(source_it);
}
}
}
+1 -3
View File
@@ -15,9 +15,7 @@ struct GameServerSocket : ServerSocket {
ServerBehavior behavior;
};
class GameServer
: public Server<Client, GameServerSocket>,
public std::enable_shared_from_this<GameServer> {
class GameServer : public Server<Client, GameServerSocket>, public std::enable_shared_from_this<GameServer> {
public:
GameServer() = delete;
GameServer(const GameServer&) = delete;
+435 -108
View File
@@ -1,7 +1,9 @@
#include "HTTPServer.hh"
#include <ctype.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include <phosg/Network.hh>
#include <string>
@@ -15,9 +17,7 @@
#include "Server.hh"
#include "ShellCommands.hh"
using namespace std;
HTTPServer::HTTPServer(shared_ptr<ServerState> state)
HTTPServer::HTTPServer(std::shared_ptr<ServerState> state)
: AsyncHTTPServer(state->io_context, "[HTTPServer] "), state(state) {
using RouterRetT = std::variant<RawResponse, std::shared_ptr<const phosg::JSON>>;
using RetT = asio::awaitable<RouterRetT>;
@@ -32,16 +32,104 @@ HTTPServer::HTTPServer(shared_ptr<ServerState> state)
});
};
auto parse_u32_string = [](const std::string& value_str, int base, const char* field_name) -> uint32_t {
if (value_str.empty()) {
throw HTTPError(400, std::format("{} is required", field_name));
}
size_t conversion_end = 0;
uint64_t value = 0;
try {
value = std::stoull(value_str, &conversion_end, base);
} catch (const std::exception&) {
throw HTTPError(400, std::format("Invalid {}", field_name));
}
if (conversion_end != value_str.size()) {
throw HTTPError(400, std::format("Invalid {}", field_name));
}
if (value > 0xFFFFFFFFULL) {
throw HTTPError(400, std::format("{} out of range", field_name));
}
return value;
};
auto parse_u32_decimal_or_0x = [parse_u32_string](const std::string& value_str, const char* field_name) -> uint32_t {
int base = 10;
if ((value_str.size() >= 2) &&
(value_str[0] == '0') &&
((value_str[1] == 'x') || (value_str[1] == 'X'))) {
base = 16;
}
return parse_u32_string(value_str, base, field_name);
};
auto parse_account_id = [parse_u32_decimal_or_0x](const std::string& account_id_str) -> uint32_t {
return parse_u32_decimal_or_0x(account_id_str, "account ID");
};
auto require_nonzero_u32 = [](uint32_t value, const char* field_name) -> uint32_t {
if (value == 0) {
throw HTTPError(400, std::format("{} is zero", field_name));
}
return value;
};
auto require_string_length = [](const std::string& value, const char* field_name, size_t min_size, size_t max_size) -> void {
if (value.size() < min_size) {
throw HTTPError(400, std::format("{} is too short", field_name));
}
if (value.size() > max_size) {
throw HTTPError(400, std::format("{} is too long", field_name));
}
};
auto require_exact_string_length = [](const std::string& value, const char* field_name, size_t expected_size) -> void {
if (value.size() != expected_size) {
throw HTTPError(400, std::format("{} length is incorrect", field_name));
}
};
auto normalize_license_type = [](std::string type_str) -> std::string {
for (char& ch : type_str) {
ch = static_cast<char>(toupper(static_cast<unsigned char>(ch)));
}
return type_str;
};
auto require_account_admin_secret = [this](const HTTPRequest& req) -> void {
const auto& account_sync_config = this->state->data->config_json->get("AccountSync", phosg::JSON::dict());
std::string shared_secret = account_sync_config.get_string("SharedSecret", "");
if (shared_secret.empty()) {
throw HTTPError(403, "Account admin mutations are disabled");
}
const std::string* header_secret = req.get_header("x-psopeeps-admin-secret");
if (!header_secret || (*header_secret != shared_secret)) {
throw HTTPError(403, "Forbidden");
}
};
auto account_mutation_response = [](const char* action, std::shared_ptr<Account> account) -> std::shared_ptr<phosg::JSON> {
return std::make_shared<phosg::JSON>(phosg::JSON::dict({
{"OK", true},
{"Action", action},
{"AccountID", account->account_id},
{"AccountIDDecimal", std::format("{:010}", account->account_id)},
{"AccountIDHex", std::format("{:08X}", account->account_id)},
}));
};
this->router.add(HTTPRequest::Method::GET, "/", [generate_server_version_json](ArgsT&&) -> RetT {
co_return make_shared<phosg::JSON>(generate_server_version_json());
co_return std::make_shared<phosg::JSON>(generate_server_version_json());
});
this->router.add(HTTPRequest::Method::POST, "/y/shell-exec", [this](ArgsT&& args) -> RetT {
auto command = args.post_data.get_string("command");
try {
auto dispatch_res = co_await ShellCommand::dispatch_str(this->state, command);
co_return make_shared<phosg::JSON>(phosg::JSON::dict({{"result", phosg::join(dispatch_res, "\n")}}));
} catch (const exception& e) {
co_return std::make_shared<phosg::JSON>(phosg::JSON::dict({{"result", phosg::join(dispatch_res, "\n")}}));
} catch (const std::exception& e) {
throw HTTPError(400, e.what());
}
});
@@ -70,8 +158,8 @@ HTTPServer::HTTPServer(shared_ptr<ServerState> state)
}
};
auto escape_label = +[](const string& in) -> string {
string out;
auto escape_label = +[](const std::string& in) -> std::string {
std::string out;
for (char ch : in) {
if (ch == '\\') {
out += "\\\\";
@@ -86,14 +174,14 @@ HTTPServer::HTTPServer(shared_ptr<ServerState> state)
return out;
};
auto add_metric = [](string& out, const string& name, uint64_t value) -> void {
auto add_metric = [](std::string& out, const std::string& name, uint64_t value) -> void {
out += name;
out += " ";
out += std::to_string(value);
out += "\n";
};
auto add_metric_1label = [](string& out, const string& name, const string& label_name, const string& label_value, uint64_t value) -> void {
auto add_metric_1label = [](std::string& out, const std::string& name, const std::string& label_name, const std::string& label_value, uint64_t value) -> void {
out += name;
out += "{";
out += label_name;
@@ -104,9 +192,9 @@ HTTPServer::HTTPServer(shared_ptr<ServerState> state)
out += "\n";
};
map<string, uint64_t> connected_by_version;
map<string, uint64_t> lobby_players_by_version;
map<string, uint64_t> game_players_by_version;
std::map<std::string, uint64_t> connected_by_version;
std::map<std::string, uint64_t> lobby_players_by_version;
std::map<std::string, uint64_t> game_players_by_version;
uint64_t connected_total = 0;
uint64_t lobbies_total = 0;
@@ -143,10 +231,10 @@ HTTPServer::HTTPServer(shared_ptr<ServerState> state)
}
}
string server_name = escape_label(this->state->name);
string revision = escape_label(GIT_REVISION_HASH);
std::string server_name = escape_label(this->state->data->name);
std::string revision = escape_label(GIT_REVISION_HASH);
string out;
std::string out;
out += "# HELP pso_newserv_up Whether this newserv HTTP metrics endpoint is reachable\n";
out += "# TYPE pso_newserv_up gauge\n";
add_metric(out, "pso_newserv_up", 1);
@@ -206,9 +294,9 @@ HTTPServer::HTTPServer(shared_ptr<ServerState> state)
});
this->router.add(HTTPRequest::Method::GET, "/y/clients", [this](ArgsT&&) -> RetT {
auto res = make_shared<phosg::JSON>(phosg::JSON::list());
auto res = std::make_shared<phosg::JSON>(phosg::JSON::list());
for (const auto& c : this->state->game_server->all_clients()) {
auto item_name_index = this->state->item_name_index_opt(c->version());
auto item_name_index = this->state->data->item_name_index_opt(c->version());
const char* drop_notifications_mode = "unknown";
switch (c->get_drop_notification_mode()) {
@@ -303,7 +391,7 @@ HTTPServer::HTTPServer(shared_ptr<ServerState> state)
client_json.emplace("DFP", p->disp.stats.char_stats.dfp.load());
client_json.emplace("ATA", p->disp.stats.char_stats.ata.load());
client_json.emplace("LCK", p->disp.stats.char_stats.lck.load());
client_json.emplace("EXP", p->disp.stats.experience.load());
client_json.emplace("EXP", p->disp.stats.exp.load());
client_json.emplace("Meseta", p->disp.stats.meseta.load());
auto tech_levels_json = phosg::JSON::dict();
for (size_t z = 0; z < 0x13; z++) {
@@ -313,22 +401,22 @@ HTTPServer::HTTPServer(shared_ptr<ServerState> state)
client_json.emplace("TechniqueLevels", std::move(tech_levels_json));
}
client_json.emplace("Level", p->disp.stats.level.load() + 1);
client_json.emplace("NameColor", p->disp.visual.name_color.load());
client_json.emplace("ExtraModel", (p->disp.visual.validation_flags & 2) ? p->disp.visual.extra_model : phosg::JSON(nullptr));
client_json.emplace("SectionID", name_for_section_id(p->disp.visual.section_id));
client_json.emplace("CharClass", name_for_char_class(p->disp.visual.char_class));
client_json.emplace("Costume", p->disp.visual.costume.load());
client_json.emplace("Skin", p->disp.visual.skin.load());
client_json.emplace("Face", p->disp.visual.face.load());
client_json.emplace("Head", p->disp.visual.head.load());
client_json.emplace("Hair", p->disp.visual.hair.load());
client_json.emplace("HairR", p->disp.visual.hair_r.load());
client_json.emplace("HairG", p->disp.visual.hair_g.load());
client_json.emplace("HairB", p->disp.visual.hair_b.load());
client_json.emplace("ProportionX", p->disp.visual.proportion_x.load());
client_json.emplace("ProportionY", p->disp.visual.proportion_y.load());
client_json.emplace("NameColor", p->disp.visual.sh.name_color.load());
client_json.emplace("ExtraModel", (p->disp.visual.sh.validation_flags & 2) ? p->disp.visual.sh.extra_model : phosg::JSON(nullptr));
client_json.emplace("SectionID", name_for_section_id(p->disp.visual.sh.section_id));
client_json.emplace("CharClass", name_for_char_class(p->disp.visual.sh.char_class));
client_json.emplace("Costume", p->disp.visual.sh.costume.load());
client_json.emplace("Skin", p->disp.visual.sh.skin.load());
client_json.emplace("Face", p->disp.visual.sh.face.load());
client_json.emplace("Head", p->disp.visual.sh.head.load());
client_json.emplace("Hair", p->disp.visual.sh.hair.load());
client_json.emplace("HairR", p->disp.visual.sh.hair_r.load());
client_json.emplace("HairG", p->disp.visual.sh.hair_g.load());
client_json.emplace("HairB", p->disp.visual.sh.hair_b.load());
client_json.emplace("ProportionX", p->disp.visual.sh.proportion_x.load());
client_json.emplace("ProportionY", p->disp.visual.sh.proportion_y.load());
client_json.emplace("Name", p->disp.name.decode(c->language()));
client_json.emplace("Name", p->disp.visual.name.decode(c->language()));
client_json.emplace("PlayTimeSeconds", p->play_time_seconds.load());
client_json.emplace("AutoReply", p->auto_reply.decode(c->language()));
@@ -366,7 +454,7 @@ HTTPServer::HTTPServer(shared_ptr<ServerState> state)
uint8_t minute = p->challenge_records.grave_time & 0xFF;
client_json.emplace("ChallengeGraveTime", std::format("{:04}-{:02}-{:02} {:02}:{:02}:00", year, month, day, hour, minute));
}
string grave_enemy_types;
std::string grave_enemy_types;
if (p->challenge_records.grave_defeated_by_enemy_rt_index) {
for (EnemyType type : enemy_types_for_rare_table_index(
p->challenge_records.grave_is_ep2 ? Episode::EP2 : Episode::EP1,
@@ -448,11 +536,11 @@ HTTPServer::HTTPServer(shared_ptr<ServerState> state)
});
this->router.add(HTTPRequest::Method::GET, "/y/lobbies", [this](ArgsT&&) -> RetT {
auto res = make_shared<phosg::JSON>(phosg::JSON::list());
auto res = std::make_shared<phosg::JSON>(phosg::JSON::list());
for (const auto& [_, l] : this->state->id_to_lobby) {
auto leader = l->clients[l->leader_id];
Version v = leader ? leader->version() : Version::BB_V4;
auto item_name_index = this->state->item_name_index_opt(v);
auto item_name_index = this->state->data->item_name_index_opt(v);
auto client_ids_json = phosg::JSON::list();
for (size_t z = 0; z < l->max_clients; z++) {
@@ -581,7 +669,7 @@ HTTPServer::HTTPServer(shared_ptr<ServerState> state)
name = ce->def.jp_short_name.decode();
}
cards_json.emplace_back(name);
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
cards_json.emplace_back(deck_entry->card_ids[w].load());
}
}
@@ -656,24 +744,257 @@ HTTPServer::HTTPServer(shared_ptr<ServerState> state)
});
this->router.add(HTTPRequest::Method::GET, "/y/accounts", [this](ArgsT&&) -> RetT {
auto res = make_shared<phosg::JSON>(phosg::JSON::list());
auto res = std::make_shared<phosg::JSON>(phosg::JSON::list());
for (const auto& it : this->state->account_index->all()) {
res->emplace_back(it->json());
}
co_return res;
});
this->router.add(HTTPRequest::Method::GET, "/y/account/:account_id", [this](ArgsT&& args) -> RetT {
uint32_t account_id = args.get_param<uint32_t>("account_id");
this->router.add(HTTPRequest::Method::GET, "/y/account/:account_id", [this, parse_account_id](ArgsT&& args) -> RetT {
uint32_t account_id = parse_account_id(args.params.at("account_id"));
try {
co_return make_shared<phosg::JSON>(this->state->account_index->from_account_id(account_id)->json());
co_return std::make_shared<phosg::JSON>(this->state->account_index->from_account_id(account_id)->json());
} catch (const AccountIndex::missing_account&) {
throw HTTPError(404, "Account does not exist");
}
});
this->router.add(HTTPRequest::Method::POST, "/y/account/:account_id/ensure",
[this, parse_account_id, parse_u32_decimal_or_0x, require_string_length, require_account_admin_secret, account_mutation_response](ArgsT&& args) -> RetT {
require_account_admin_secret(args.req);
uint32_t account_id = parse_account_id(args.params.at("account_id"));
try {
std::shared_ptr<Account> account;
try {
account = this->state->account_index->from_account_id(account_id);
} catch (const AccountIndex::missing_account&) {
account = std::make_shared<Account>();
account->account_id = account_id;
account->flags = parse_u32_decimal_or_0x(args.post_data.get_string("flags", "0"), "flags");
account->user_flags = parse_u32_decimal_or_0x(args.post_data.get_string("user_flags", "0"), "user_flags");
this->state->account_index->add(account);
}
std::string bb_username = args.post_data.get_string("bb_username", "");
if (!bb_username.empty()) {
auto license = std::make_shared<BBLicense>();
license->username = std::move(bb_username);
license->password = args.post_data.get_string("bb_password", "");
require_string_length(license->username, "bb_username", 1, 16);
require_string_length(license->password, "bb_password", 1, 16);
auto existing_it = account->bb_licenses.find(license->username);
if (existing_it != account->bb_licenses.end()) {
if (existing_it->second->password != license->password) {
throw HTTPError(409, "BB license already exists on account with different password");
}
} else {
this->state->account_index->add_bb_license(account, license);
}
}
account->save();
co_return account_mutation_response("ensure-account", account);
} catch (const HTTPError&) {
throw;
} catch (const std::runtime_error& e) {
if (!strcmp(e.what(), "username already registered")) {
throw HTTPError(409, e.what());
}
throw HTTPError(400, e.what());
} catch (const std::exception& e) {
throw HTTPError(400, e.what());
}
});
this->router.add(HTTPRequest::Method::POST, "/y/account/:account_id/add-license",
[this, parse_account_id, parse_u32_string, require_nonzero_u32, require_string_length, require_exact_string_length, normalize_license_type, require_account_admin_secret, account_mutation_response](ArgsT&& args) -> RetT {
require_account_admin_secret(args.req);
uint32_t account_id = parse_account_id(args.params.at("account_id"));
try {
auto account = this->state->account_index->from_account_id(account_id);
std::string type_str = normalize_license_type(args.post_data.get_string("type"));
if (type_str == "DC") {
auto license = std::make_shared<V1V2License>();
license->serial_number = require_nonzero_u32(parse_u32_string(args.post_data.get_string("serial_number"), 16, "serial_number"), "serial_number");
license->access_key = args.post_data.get_string("access_key");
require_exact_string_length(license->access_key, "access_key", 8);
auto existing_it = account->dc_licenses.find(license->serial_number);
if (existing_it != account->dc_licenses.end()) {
if (existing_it->second->access_key != license->access_key) {
throw HTTPError(409, "DC license already exists on account with different access key");
}
co_return account_mutation_response("add-license", account);
}
this->state->account_index->add_dc_license(account, license);
} else if (type_str == "PC") {
auto license = std::make_shared<V1V2License>();
license->serial_number = require_nonzero_u32(parse_u32_string(args.post_data.get_string("serial_number"), 16, "serial_number"), "serial_number");
license->access_key = args.post_data.get_string("access_key");
require_exact_string_length(license->access_key, "access_key", 8);
auto existing_it = account->pc_licenses.find(license->serial_number);
if (existing_it != account->pc_licenses.end()) {
if (existing_it->second->access_key != license->access_key) {
throw HTTPError(409, "PC license already exists on account with different access key");
}
co_return account_mutation_response("add-license", account);
}
this->state->account_index->add_pc_license(account, license);
} else if (type_str == "GC") {
auto license = std::make_shared<GCLicense>();
license->serial_number = require_nonzero_u32(parse_u32_string(args.post_data.get_string("serial_number"), 10, "serial_number"), "serial_number");
license->access_key = args.post_data.get_string("access_key");
license->password = args.post_data.get_string("password");
require_exact_string_length(license->access_key, "access_key", 12);
require_string_length(license->password, "password", 1, 8);
auto existing_it = account->gc_licenses.find(license->serial_number);
if (existing_it != account->gc_licenses.end()) {
if ((existing_it->second->access_key != license->access_key) ||
(existing_it->second->password != license->password)) {
throw HTTPError(409, "GC license already exists on account with different credentials");
}
co_return account_mutation_response("add-license", account);
}
this->state->account_index->add_gc_license(account, license);
} else if (type_str == "BB") {
auto license = std::make_shared<BBLicense>();
license->username = args.post_data.get_string("username");
license->password = args.post_data.get_string("password", "");
require_string_length(license->username, "username", 1, 16);
require_string_length(license->password, "password", 1, 16);
auto existing_it = account->bb_licenses.find(license->username);
if (existing_it != account->bb_licenses.end()) {
if (existing_it->second->password != license->password) {
throw HTTPError(409, "BB license already exists on account with different password");
}
co_return account_mutation_response("add-license", account);
}
this->state->account_index->add_bb_license(account, license);
} else {
throw HTTPError(400, "Invalid license type");
}
account->save();
co_return account_mutation_response("add-license", account);
} catch (const AccountIndex::missing_account&) {
throw HTTPError(404, "Account does not exist");
} catch (const HTTPError&) {
throw;
} catch (const std::runtime_error& e) {
if (!strcmp(e.what(), "serial number already registered") ||
!strcmp(e.what(), "username already registered")) {
throw HTTPError(409, e.what());
}
throw HTTPError(400, e.what());
} catch (const std::exception& e) {
throw HTTPError(400, e.what());
}
});
this->router.add(HTTPRequest::Method::POST, "/y/account/:account_id/set-bb-license",
[this, parse_account_id, require_string_length, require_account_admin_secret, account_mutation_response](ArgsT&& args) -> RetT {
require_account_admin_secret(args.req);
uint32_t account_id = parse_account_id(args.params.at("account_id"));
try {
auto account = this->state->account_index->from_account_id(account_id);
auto license = std::make_shared<BBLicense>();
license->username = args.post_data.get_string("username");
license->password = args.post_data.get_string("password", "");
require_string_length(license->username, "username", 1, 16);
require_string_length(license->password, "password", 1, 16);
auto existing_it = account->bb_licenses.find(license->username);
if (existing_it != account->bb_licenses.end()) {
existing_it->second->password = license->password;
} else {
this->state->account_index->add_bb_license(account, license);
}
account->save();
co_return account_mutation_response("set-bb-license", account);
} catch (const AccountIndex::missing_account&) {
throw HTTPError(404, "Account does not exist");
} catch (const HTTPError&) {
throw;
} catch (const std::runtime_error& e) {
if (!strcmp(e.what(), "username already registered")) {
throw HTTPError(409, e.what());
}
throw HTTPError(400, e.what());
} catch (const std::exception& e) {
throw HTTPError(400, e.what());
}
});
this->router.add(HTTPRequest::Method::POST, "/y/account/:account_id/delete-license",
[this, parse_account_id, parse_u32_string, normalize_license_type, require_account_admin_secret, account_mutation_response](ArgsT&& args) -> RetT {
require_account_admin_secret(args.req);
uint32_t account_id = parse_account_id(args.params.at("account_id"));
try {
auto account = this->state->account_index->from_account_id(account_id);
std::string type_str = normalize_license_type(args.post_data.get_string("type"));
if (type_str == "DC") {
uint32_t serial_number = parse_u32_string(args.post_data.get_string("serial_number"), 16, "serial_number");
if (account->dc_licenses.find(serial_number) == account->dc_licenses.end()) {
co_return account_mutation_response("delete-license", account);
}
this->state->account_index->remove_dc_license(account, serial_number);
} else if (type_str == "PC") {
uint32_t serial_number = parse_u32_string(args.post_data.get_string("serial_number"), 16, "serial_number");
if (account->pc_licenses.find(serial_number) == account->pc_licenses.end()) {
co_return account_mutation_response("delete-license", account);
}
this->state->account_index->remove_pc_license(account, serial_number);
} else if (type_str == "GC") {
uint32_t serial_number = parse_u32_string(args.post_data.get_string("serial_number"), 10, "serial_number");
if (account->gc_licenses.find(serial_number) == account->gc_licenses.end()) {
co_return account_mutation_response("delete-license", account);
}
this->state->account_index->remove_gc_license(account, serial_number);
} else if (type_str == "BB") {
std::string username = args.post_data.get_string("username");
if (account->bb_licenses.find(username) == account->bb_licenses.end()) {
co_return account_mutation_response("delete-license", account);
}
this->state->account_index->remove_bb_license(account, username);
} else {
throw HTTPError(400, "Invalid license type");
}
account->save();
co_return account_mutation_response("delete-license", account);
} catch (const AccountIndex::missing_account&) {
throw HTTPError(404, "Account does not exist");
} catch (const HTTPError&) {
throw;
} catch (const std::exception& e) {
throw HTTPError(400, e.what());
}
});
this->router.add(HTTPRequest::Method::GET, "/y/teams", [this](ArgsT&&) -> RetT {
auto res = make_shared<phosg::JSON>(phosg::JSON::dict());
auto res = std::make_shared<phosg::JSON>(phosg::JSON::dict());
for (const auto& it : this->state->team_index->all()) {
res->emplace(std::format("{}", it->team_id), it->json());
}
@@ -686,7 +1007,7 @@ HTTPServer::HTTPServer(shared_ptr<ServerState> state)
if (!team) {
throw HTTPError(404, "Team does not exist");
}
co_return make_shared<phosg::JSON>(team->json());
co_return std::make_shared<phosg::JSON>(team->json());
});
this->router.add(HTTPRequest::Method::GET, "/y/team/:team_id/flag", [this](ArgsT&& args) -> RetT {
@@ -712,26 +1033,26 @@ HTTPServer::HTTPServer(shared_ptr<ServerState> state)
lobby_count++;
}
}
uint64_t uptime_usecs = phosg::now() - this->state->creation_time;
uint64_t uptime_usecs = phosg::now() - this->state->data->creation_time;
return phosg::JSON::dict({
{"StartTimeUsecs", this->state->creation_time},
{"StartTime", phosg::format_time(this->state->creation_time)},
{"StartTimeUsecs", this->state->data->creation_time},
{"StartTime", phosg::format_time(this->state->data->creation_time)},
{"UptimeUsecs", uptime_usecs},
{"Uptime", phosg::format_duration(uptime_usecs)},
{"LobbyCount", lobby_count},
{"GameCount", game_count},
{"ClientCount", this->state->game_server->all_clients().size() - ProxySession::num_proxy_sessions},
{"ProxySessionCount", ProxySession::num_proxy_sessions},
{"ServerName", this->state->name},
{"ServerName", this->state->data->name},
});
};
this->router.add(HTTPRequest::Method::GET, "/y/server", [generate_server_info_json](ArgsT&&) -> RetT {
co_return make_shared<phosg::JSON>(generate_server_info_json());
co_return std::make_shared<phosg::JSON>(generate_server_info_json());
});
this->router.add(HTTPRequest::Method::GET, "/y/config", [this](ArgsT&&) -> RetT {
co_return this->state->config_json;
co_return this->state->data->config_json;
});
this->router.add(HTTPRequest::Method::GET, "/y/summary", [this, generate_server_info_json](ArgsT&&) -> RetT {
@@ -742,12 +1063,12 @@ HTTPServer::HTTPServer(shared_ptr<ServerState> state)
clients_json.emplace_back(phosg::JSON::dict({
{"ID", c->id},
{"AccountID", c->login ? c->login->account->account_id : phosg::JSON(nullptr)},
{"Name", p ? p->disp.name.decode(c->language()) : phosg::JSON(nullptr)},
{"Name", p ? p->disp.visual.name.decode(c->language()) : phosg::JSON(nullptr)},
{"Version", phosg::name_for_enum(c->version())},
{"Language", name_for_language(c->language())},
{"Level", p ? (p->disp.stats.level + 1) : phosg::JSON(nullptr)},
{"Class", p ? name_for_char_class(p->disp.visual.char_class) : phosg::JSON(nullptr)},
{"SectionID", p ? name_for_section_id(p->disp.visual.section_id) : phosg::JSON(nullptr)},
{"Class", p ? name_for_char_class(p->disp.visual.sh.char_class) : phosg::JSON(nullptr)},
{"SectionID", p ? name_for_section_id(p->disp.visual.sh.section_id) : phosg::JSON(nullptr)},
{"LobbyID", l ? l->lobby_id : phosg::JSON(nullptr)},
{"IsOnProxy", c->proxy_session ? true : false},
}));
@@ -789,7 +1110,7 @@ HTTPServer::HTTPServer(shared_ptr<ServerState> state)
}
}
co_return make_shared<phosg::JSON>(phosg::JSON::dict({
co_return std::make_shared<phosg::JSON>(phosg::JSON::dict({
{"Clients", std::move(clients_json)},
{"Games", std::move(games_json)},
{"Server", generate_server_info_json()},
@@ -797,26 +1118,30 @@ HTTPServer::HTTPServer(shared_ptr<ServerState> state)
});
this->router.add(HTTPRequest::Method::GET, "/y/data/ep3/cards", [this](ArgsT&& args) -> RetT {
auto& index = args.req.query_params.count("trial") ? this->state->ep3_card_index_trial : this->state->ep3_card_index;
co_return co_await call_on_thread_pool(*this->state->thread_pool, [&]() -> shared_ptr<phosg::JSON> {
return make_shared<phosg::JSON>(index->definitions_json());
auto& index = args.req.query_params.count("trial")
? this->state->data->ep3_card_index_trial
: this->state->data->ep3_card_index;
co_return co_await call_on_thread_pool(*this->state->thread_pool, [&]() -> std::shared_ptr<phosg::JSON> {
return std::make_shared<phosg::JSON>(index->definitions_json());
});
});
this->router.add(HTTPRequest::Method::GET, "/y/data/ep3/card/:card_id", [this](ArgsT&& args) -> RetT {
auto& index = args.req.query_params.count("trial") ? this->state->ep3_card_index_trial : this->state->ep3_card_index;
auto& index = args.req.query_params.count("trial")
? this->state->data->ep3_card_index_trial
: this->state->data->ep3_card_index;
uint32_t card_id = args.get_param<uint32_t>("card_id");
try {
co_return make_shared<phosg::JSON>(index->definition_for_id(card_id)->def.json());
co_return std::make_shared<phosg::JSON>(index->definition_for_id(card_id)->def.json());
} catch (const std::out_of_range&) {
throw HTTPError(404, "Card definition does not exist");
}
});
this->router.add(HTTPRequest::Method::GET, "/y/data/ep3/maps", [this](ArgsT&&) -> RetT {
co_return co_await call_on_thread_pool(*this->state->thread_pool, [&]() -> shared_ptr<phosg::JSON> {
auto ret = make_shared<phosg::JSON>(phosg::JSON::dict());
for (const auto& [map_number, map] : this->state->ep3_map_index->all_maps()) {
co_return co_await call_on_thread_pool(*this->state->thread_pool, [&]() -> std::shared_ptr<phosg::JSON> {
auto ret = std::make_shared<phosg::JSON>(phosg::JSON::dict());
for (const auto& [map_number, map] : this->state->data->ep3_map_index->all_maps()) {
auto languages_json = phosg::JSON::list();
for (const auto& vm : map->all_versions()) {
if (vm) {
@@ -835,11 +1160,11 @@ HTTPServer::HTTPServer(shared_ptr<ServerState> state)
});
this->router.add(HTTPRequest::Method::GET, "/y/data/ep3/map/:map_number/:language", [this](ArgsT&& args) -> RetT {
co_return co_await call_on_thread_pool(*this->state->thread_pool, [&]() -> shared_ptr<phosg::JSON> {
co_return co_await call_on_thread_pool(*this->state->thread_pool, [&]() -> std::shared_ptr<phosg::JSON> {
try {
auto map = this->state->ep3_map_index->map_for_id(args.get_param<uint32_t>("map_number", true));
auto map = this->state->data->ep3_map_index->map_for_id(args.get_param<uint32_t>("map_number", true));
auto vm = map->version(language_for_name(args.params.at("language")));
return make_shared<phosg::JSON>(vm->map->json(vm->language));
return std::make_shared<phosg::JSON>(vm->map->json(vm->language));
} catch (const std::out_of_range&) {
throw HTTPError(404, "Map version does not exist");
}
@@ -849,9 +1174,9 @@ HTTPServer::HTTPServer(shared_ptr<ServerState> state)
this->router.add(HTTPRequest::Method::GET, "/y/data/ep3/map/:map_number/:language/raw", [this](ArgsT&& args) -> RetT {
co_return co_await call_on_thread_pool(*this->state->thread_pool, [&]() -> RawResponse {
try {
auto map = this->state->ep3_map_index->map_for_id(args.get_param<uint32_t>("map_number"));
auto map = this->state->data->ep3_map_index->map_for_id(args.get_param<uint32_t>("map_number"));
auto vm = map->version(language_for_name(args.params.at("language")));
string data(reinterpret_cast<const char*>(vm->map.get()), sizeof(Episode3::MapDefinition));
std::string data(reinterpret_cast<const char*>(vm->map.get()), sizeof(Episode3::MapDefinition));
return RawResponse{.content_type = "application/octet-stream", .data = std::move(data)};
} catch (const std::out_of_range&) {
throw HTTPError(404, "Map version does not exist");
@@ -860,8 +1185,8 @@ HTTPServer::HTTPServer(shared_ptr<ServerState> state)
});
this->router.add(HTTPRequest::Method::GET, "/y/data/common-tables", [this](ArgsT&&) -> RetT {
auto ret = make_shared<phosg::JSON>(phosg::JSON::list());
for (const auto& it : this->state->common_item_sets) {
auto ret = std::make_shared<phosg::JSON>(phosg::JSON::list());
for (const auto& it : this->state->data->common_item_sets) {
ret->emplace_back(it.first);
}
co_return ret;
@@ -869,18 +1194,18 @@ HTTPServer::HTTPServer(shared_ptr<ServerState> state)
this->router.add(HTTPRequest::Method::GET, "/y/data/common-table/:table_name", [this](ArgsT&& args) -> RetT {
try {
const auto& table = this->state->common_item_sets.at(args.params.at("table_name"));
co_return co_await call_on_thread_pool(*this->state->thread_pool, [&]() -> shared_ptr<phosg::JSON> {
return make_shared<phosg::JSON>(table->json());
const auto& table = this->state->data->common_item_sets.at(args.params.at("table_name"));
co_return co_await call_on_thread_pool(*this->state->thread_pool, [&]() -> std::shared_ptr<phosg::JSON> {
return std::make_shared<phosg::JSON>(table->json());
});
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
throw HTTPError(404, "Table does not exist");
}
});
this->router.add(HTTPRequest::Method::GET, "/y/data/rare-tables", [this](ArgsT&&) -> RetT {
auto ret = make_shared<phosg::JSON>(phosg::JSON::list());
for (const auto& it : this->state->rare_item_sets) {
auto ret = std::make_shared<phosg::JSON>(phosg::JSON::list());
for (const auto& it : this->state->data->rare_item_sets) {
ret->emplace_back(it.first);
}
co_return ret;
@@ -889,52 +1214,52 @@ HTTPServer::HTTPServer(shared_ptr<ServerState> state)
this->router.add(HTTPRequest::Method::GET, "/y/data/rare-table/:table_name", [this](ArgsT&& args) -> RetT {
try {
const auto& table_name = args.params.at("table_name");
const auto& table = this->state->rare_item_sets.at(table_name);
shared_ptr<const ItemNameIndex> name_index;
const auto& table = this->state->data->rare_item_sets.at(table_name);
std::shared_ptr<const ItemNameIndex> name_index;
if (table_name.ends_with("-v1")) {
name_index = this->state->item_name_index_opt(Version::DC_V1);
name_index = this->state->data->item_name_index_opt(Version::DC_V1);
} else if (table_name.ends_with("-v2")) {
name_index = this->state->item_name_index_opt(Version::PC_V2);
name_index = this->state->data->item_name_index_opt(Version::PC_V2);
} else if (table_name.ends_with("-v3")) {
name_index = this->state->item_name_index_opt(Version::GC_V3);
name_index = this->state->data->item_name_index_opt(Version::GC_V3);
} else if (table_name.ends_with("-v4")) {
name_index = this->state->item_name_index_opt(Version::BB_V4);
name_index = this->state->data->item_name_index_opt(Version::BB_V4);
}
co_return co_await call_on_thread_pool(*this->state->thread_pool, [&]() -> shared_ptr<phosg::JSON> {
return make_shared<phosg::JSON>(table->json(name_index));
co_return co_await call_on_thread_pool(*this->state->thread_pool, [&]() -> std::shared_ptr<phosg::JSON> {
return std::make_shared<phosg::JSON>(table->json(name_index));
});
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
throw HTTPError(404, "Table does not exist");
}
});
this->router.add(HTTPRequest::Method::GET, "/y/data/quests", [this](ArgsT&&) -> RetT {
co_return co_await call_on_thread_pool(*this->state->thread_pool, [&]() -> shared_ptr<phosg::JSON> {
return make_shared<phosg::JSON>(this->state->quest_index->json());
co_return co_await call_on_thread_pool(*this->state->thread_pool, [&]() -> std::shared_ptr<phosg::JSON> {
return std::make_shared<phosg::JSON>(this->state->data->quest_index->json());
});
});
this->router.add(HTTPRequest::Method::GET, "/y/data/quest/:quest_num", [this](ArgsT&& args) -> RetT {
uint32_t quest_num = args.get_param<uint32_t>("quest_num");
auto q = this->state->quest_index->get(quest_num);
auto q = this->state->data->quest_index->get(quest_num);
if (!q) {
throw HTTPError(404, "Quest does not exist");
}
co_return make_shared<phosg::JSON>(q->json());
co_return std::make_shared<phosg::JSON>(q->json());
});
}
asio::awaitable<void> HTTPServer::send_rare_drop_notification(shared_ptr<const phosg::JSON> message) {
asio::awaitable<void> HTTPServer::send_rare_drop_notification(std::shared_ptr<const phosg::JSON> message) {
if (!this->rare_drop_subscribers.empty()) {
string data = message->serialize();
std::string data = message->serialize();
// Make a copy of the rare drop subscribers set, so we can guarantee that the client objects are all valid until
// this coroutine returns
unordered_set<shared_ptr<HTTPClient>> subscribers = this->rare_drop_subscribers;
std::unordered_set<std::shared_ptr<HTTPClient>> subscribers = this->rare_drop_subscribers;
size_t expected_results = subscribers.size();
AsyncPromise<void> complete_promise;
auto fn = [this, &data, &expected_results, &complete_promise](shared_ptr<HTTPClient> c) -> asio::awaitable<void> {
auto fn = [this, &data, &expected_results, &complete_promise](std::shared_ptr<HTTPClient> c) -> asio::awaitable<void> {
try {
co_await c->send_websocket_message(data);
} catch (const std::exception& e) {
@@ -954,14 +1279,14 @@ asio::awaitable<void> HTTPServer::send_rare_drop_notification(shared_ptr<const p
co_return;
}
asio::awaitable<std::unique_ptr<HTTPResponse>> HTTPServer::handle_request(shared_ptr<HTTPClient> c, HTTPRequest&& req) {
variant<RawResponse, shared_ptr<const phosg::JSON>> ret;
asio::awaitable<std::unique_ptr<HTTPResponse>> HTTPServer::handle_request(std::shared_ptr<HTTPClient> c, HTTPRequest&& req) {
std::variant<RawResponse, std::shared_ptr<const phosg::JSON>> ret;
uint32_t serialize_options = phosg::JSON::SerializeOption::ESCAPE_CONTROLS_ONLY;
uint64_t start_time = phosg::now();
this->log.info_f("{} ...", req.path);
auto resp = make_unique<HTTPResponse>();
auto resp = std::make_unique<HTTPResponse>();
resp->http_version = req.http_version;
resp->response_code = 200;
resp->headers.emplace("Server", "newserv");
@@ -981,39 +1306,41 @@ asio::awaitable<std::unique_ptr<HTTPResponse>> HTTPServer::handle_request(shared
ret = co_await this->router.call_handler(c, req);
} catch (const HTTPError& e) {
ret = make_shared<phosg::JSON>(phosg::JSON::dict({{"Error", true}, {"Message", e.what()}}));
ret = std::make_shared<phosg::JSON>(phosg::JSON::dict({{"Error", true}, {"Message", e.what()}}));
resp->response_code = e.code;
} catch (const exception& e) {
ret = make_shared<phosg::JSON>(phosg::JSON::dict({{"Error", true}, {"Message", e.what()}}));
} catch (const std::exception& e) {
ret = std::make_shared<phosg::JSON>(phosg::JSON::dict({{"Error", true}, {"Message", e.what()}}));
resp->response_code = 500;
}
uint64_t handler_end = phosg::now();
if (holds_alternative<shared_ptr<const phosg::JSON>>(ret)) {
if (holds_alternative<std::shared_ptr<const phosg::JSON>>(ret)) {
// If the handler returns nullptr (not JSON null), assume it called enable_websockets and send no response
auto& json = get<shared_ptr<const phosg::JSON>>(ret);
auto& json = get<std::shared_ptr<const phosg::JSON>>(ret);
if (!json) {
co_return nullptr;
}
resp->headers.emplace("Content-Type", "application/json");
resp->data = co_await call_on_thread_pool(*this->state->thread_pool, [&]() -> string {
resp->data = co_await call_on_thread_pool(*this->state->thread_pool, [&]() -> std::string {
return json->serialize(serialize_options, 0);
});
uint64_t serialize_end = phosg::now();
string handler_time = phosg::format_duration(handler_end - start_time);
string serialize_time = phosg::format_duration(serialize_end - handler_end);
string size_str = phosg::format_size(resp->data.size());
this->log.info_f("{} in [handler: {}, serialize: {}, size: {}]", req.path, handler_time, serialize_time, size_str);
this->log.info_f("{} in [handler: {}, serialize: {}, size: {}]",
req.path,
phosg::format_duration(handler_end - start_time),
phosg::format_duration(serialize_end - handler_end),
phosg::format_size(resp->data.size()));
} else {
auto& raw_resp = get<RawResponse>(ret);
resp->headers.emplace("Content-Type", std::move(raw_resp.content_type));
resp->data = std::move(raw_resp.data);
string handler_time = phosg::format_duration(handler_end - start_time);
string size_str = phosg::format_size(resp->data.size());
this->log.info_f("{} in [handler: {}, size: {}]", req.path, handler_time, size_str);
this->log.info_f("{} in [handler: {}, size: {}]",
req.path,
phosg::format_duration(handler_end - start_time),
phosg::format_size(resp->data.size()));
}
co_return resp;
+14 -16
View File
@@ -4,8 +4,6 @@
#include <phosg/Strings.hh>
using namespace std;
static inline uint16_t collapse_checksum(uint32_t sum) {
// It's impossible for this to be necessary more than twice: the first addition can carry out at most a single bit.
sum = (sum & 0xFFFF) + (sum >> 16);
@@ -65,13 +63,13 @@ FrameInfo::FrameInfo(LinkType link_type, const void* header_start, size_t size)
break;
default:
throw logic_error("invalid link type");
throw std::logic_error("invalid link type");
}
// Parse inner protocol headers
switch (proto) {
case Protocol::NONE:
throw runtime_error("unknown protocol");
throw std::runtime_error("unknown protocol");
case Protocol::LCP:
this->payload_size -= sizeof(LCPHeader);
this->lcp = &r.get<LCPHeader>();
@@ -87,7 +85,7 @@ FrameInfo::FrameInfo(LinkType link_type, const void* header_start, size_t size)
case Protocol::IPV4:
this->ipv4 = &r.get<IPv4Header>();
if (this->payload_size < this->ipv4->size) {
throw invalid_argument("ipv4 header specifies size larger than frame");
throw std::invalid_argument("ipv4 header specifies size larger than frame");
}
this->payload_size = this->ipv4->size - sizeof(IPv4Header);
@@ -95,7 +93,7 @@ FrameInfo::FrameInfo(LinkType link_type, const void* header_start, size_t size)
this->tcp = &r.get<TCPHeader>();
size_t tcp_header_size = (this->tcp->flags >> 12) * 4;
if (tcp_header_size < sizeof(TCPHeader) || tcp_header_size > this->payload_size) {
throw invalid_argument("frame is too small for tcp4 header with options");
throw std::invalid_argument("frame is too small for tcp4 header with options");
}
this->tcp_options_size = tcp_header_size - sizeof(TCPHeader);
this->payload_size -= tcp_header_size;
@@ -115,12 +113,12 @@ FrameInfo::FrameInfo(LinkType link_type, const void* header_start, size_t size)
this->payload = r.getv(this->payload_size);
}
string FrameInfo::header_str() const {
std::string FrameInfo::header_str() const {
if (!this->ether && !this->hdlc) {
return "<invalid-frame-info>";
}
string ret;
std::string ret;
if (this->ether) {
ret = std::format(
"ETHER:{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}->{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}",
@@ -187,10 +185,10 @@ string FrameInfo::header_str() const {
void FrameInfo::truncate(size_t new_total_size) {
if (new_total_size > this->total_size) {
throw logic_error("truncate call expands frame size");
throw std::logic_error("truncate call expands frame size");
}
if (new_total_size < this->payload_size) {
throw logic_error("truncate call destroys part of header");
throw std::logic_error("truncate call destroys part of header");
}
size_t delta_bytes = this->total_size - new_total_size;
this->total_size -= delta_bytes;
@@ -222,7 +220,7 @@ uint16_t FrameInfo::computed_ipv4_header_checksum(const IPv4Header& ipv4) {
uint16_t FrameInfo::computed_ipv4_header_checksum() const {
if (!this->ipv4) {
throw logic_error("cannot compute ipv4 header checksum for non-ipv4 frame");
throw std::logic_error("cannot compute ipv4 header checksum for non-ipv4 frame");
}
return this->computed_ipv4_header_checksum(*this->ipv4);
}
@@ -252,10 +250,10 @@ uint16_t FrameInfo::computed_udp4_checksum(
uint16_t FrameInfo::computed_udp4_checksum() const {
if (!this->ipv4) {
throw logic_error("cannot compute udp header checksum for non-ipv4 frame");
throw std::logic_error("cannot compute udp header checksum for non-ipv4 frame");
}
if (!this->udp) {
throw logic_error("cannot compute udp header checksum for non-udp frame");
throw std::logic_error("cannot compute udp header checksum for non-udp frame");
}
return this->computed_udp4_checksum(
*this->ipv4, *this->udp, this->payload, this->payload_size);
@@ -293,10 +291,10 @@ uint16_t FrameInfo::computed_tcp4_checksum(
uint16_t FrameInfo::computed_tcp4_checksum() const {
if (!this->ipv4) {
throw logic_error("cannot compute tcp header checksum for non-ipv4 frame");
throw std::logic_error("cannot compute tcp header checksum for non-ipv4 frame");
}
if (!this->tcp) {
throw logic_error("cannot compute tcp header checksum for non-tcp frame");
throw std::logic_error("cannot compute tcp header checksum for non-tcp frame");
}
return this->computed_tcp4_checksum(
*this->ipv4, *this->tcp, this->tcp + 1,
@@ -317,7 +315,7 @@ uint16_t FrameInfo::computed_hdlc_checksum(const void* vdata, size_t size) {
uint16_t FrameInfo::computed_hdlc_checksum() const {
if (!this->hdlc) {
throw logic_error("cannot compute HDLC checksum for non-HDLC frame");
throw std::logic_error("cannot compute HDLC checksum for non-HDLC frame");
}
return this->computed_hdlc_checksum(&this->hdlc->address, this->total_size - 4);
}
+167 -140
View File
@@ -13,18 +13,16 @@
#include "IPFrameInfo.hh"
#include "Loggers.hh"
using namespace std;
static size_t unescape_hdlc_frame_inplace(void* vdata, size_t size) {
uint8_t* data = reinterpret_cast<uint8_t*>(vdata);
if (size < 2) {
throw runtime_error("escaped HDLC frame is too small");
throw std::runtime_error("escaped HDLC frame is too small");
}
if (data[0] != 0x7E) {
throw runtime_error("HDLC frame does not begin with 7E");
throw std::runtime_error("HDLC frame does not begin with 7E");
}
if (data[size - 1] != 0x7E) {
throw runtime_error("HDLC frame does not end with 7E");
throw std::runtime_error("HDLC frame does not end with 7E");
}
size_t read_offset = 1;
@@ -33,33 +31,34 @@ static size_t unescape_hdlc_frame_inplace(void* vdata, size_t size) {
uint8_t ch = data[read_offset++];
if (ch == 0x7D) {
if (read_offset >= size - 1) {
throw runtime_error("abort sequence received");
throw std::runtime_error("abort sequence received");
}
ch = data[read_offset++] ^ 0x20;
}
data[write_offset++] = ch;
}
if (write_offset > size - 1) {
throw logic_error("unescaping HDLC frame resulted in longer data string");
throw std::logic_error("unescaping HDLC frame resulted in longer data string");
}
data[write_offset++] = 0x7E;
return write_offset;
}
static string escape_hdlc_frame(const void* data, size_t size, uint32_t escape_control_character_flags = 0xFFFFFFFF) {
static std::string escape_hdlc_frame(
const void* data, size_t size, uint32_t escape_control_character_flags = 0xFFFFFFFF) {
if (size < 2) {
throw runtime_error("HDLC frame too small for start and end sentinels");
throw std::runtime_error("HDLC frame too small for start and end sentinels");
}
phosg::StringReader r(data, size);
if (r.pget_u8(size - 1) != 0x7E) {
throw runtime_error("HDLC frame does not end with 7E");
throw std::runtime_error("HDLC frame does not end with 7E");
}
r.truncate(size - 1);
if (r.get_u8() != 0x7E) {
throw runtime_error("HDLC frame does not begin with 7E");
throw std::runtime_error("HDLC frame does not begin with 7E");
}
string ret("\x7E", 1);
std::string ret("\x7E", 1);
while (!r.eof()) {
uint8_t ch = r.get_u8();
@@ -74,7 +73,7 @@ static string escape_hdlc_frame(const void* data, size_t size, uint32_t escape_c
return ret;
}
static string escape_hdlc_frame(const string& data, uint32_t escape_control_character_flags = 0xFFFFFFFF) {
static std::string escape_hdlc_frame(const std::string& data, uint32_t escape_control_character_flags = 0xFFFFFFFF) {
return escape_hdlc_frame(data.data(), data.size(), escape_control_character_flags);
}
@@ -114,7 +113,7 @@ void IPSSClient::TCPConnection::drain_outbound_data(size_t size) {
}
}
if (size > 0) {
throw logic_error("attempted to drain more outbound data than was present");
throw std::logic_error("attempted to drain more outbound data than was present");
}
}
@@ -128,7 +127,7 @@ void IPSSClient::TCPConnection::linearize_outbound_data(size_t size) {
}
IPSSClient::IPSSClient(
shared_ptr<IPStackSimulator> sim, uint64_t network_id, VirtualNetworkProtocol protocol, asio::ip::tcp::socket&& sock)
std::shared_ptr<IPStackSimulator> sim, uint64_t network_id, VirtualNetworkProtocol protocol, asio::ip::tcp::socket&& sock)
: io_context(sim->get_io_context()),
sim(sim),
network_id(network_id),
@@ -143,10 +142,10 @@ IPSSClient::IPSSClient(
void IPSSClient::reschedule_idle_timeout() {
auto sim = this->sim.lock();
if (!sim) {
throw runtime_error("cannot reschedule idle timeout when simulator is missing");
throw std::runtime_error("cannot reschedule idle timeout when simulator is missing");
}
this->idle_timeout_timer.cancel();
this->idle_timeout_timer.expires_after(std::chrono::microseconds(sim->get_state()->client_idle_timeout_usecs));
this->idle_timeout_timer.expires_after(std::chrono::microseconds(sim->get_state()->data->client_idle_timeout_usecs));
this->idle_timeout_timer.async_wait([this, sim](std::error_code ec) {
if (!ec) {
sim->log.info_f("Idle timeout expired on N-{:X}", this->network_id);
@@ -165,8 +164,10 @@ IPSSChannel::IPSSChannel(
Language language,
const std::string& name,
phosg::TerminalFormat terminal_send_color,
phosg::TerminalFormat terminal_recv_color)
: Channel(version, language, name, terminal_send_color, terminal_recv_color),
phosg::TerminalFormat terminal_recv_color,
bool censor_received_credentials,
bool censor_sent_credentials)
: Channel(version, language, name, terminal_send_color, terminal_recv_color, censor_received_credentials, censor_sent_credentials),
sim(sim),
ipss_client(ipss_client),
tcp_conn(tcp_conn),
@@ -202,7 +203,7 @@ void IPSSChannel::add_inbound_data(const void* data, size_t size) {
// If recv_buf is not null, there is a coroutine waiting to receive data, and inbound_data must be empty. Copy the
// data directly to the waiting coroutine's buffer, and put the rest in this->inbound_data if needed.
if (this->recv_buf) {
size_t direct_size = min<size_t>(this->recv_buf_size, size);
size_t direct_size = std::min<size_t>(this->recv_buf_size, size);
memcpy(this->recv_buf, data, direct_size);
data = reinterpret_cast<const uint8_t*>(data) + direct_size;
size -= direct_size;
@@ -219,7 +220,7 @@ void IPSSChannel::add_inbound_data(const void* data, size_t size) {
this->data_available_signal.set();
}
void IPSSChannel::send_raw(string&& data) {
void IPSSChannel::send_raw(std::string&& data) {
auto c = this->ipss_client.lock();
if (!c) {
return;
@@ -247,7 +248,7 @@ void IPSSChannel::send_raw(string&& data) {
asio::awaitable<void> IPSSChannel::recv_raw(void* data, size_t size) {
if (this->recv_buf) {
throw logic_error("recv_raw called again when it was already pending");
throw std::logic_error("recv_raw called again when it was already pending");
}
// Receive as much data as possible from the pending inbound data buffer
@@ -272,7 +273,7 @@ asio::awaitable<void> IPSSChannel::recv_raw(void* data, size_t size) {
this->recv_buf_size = size;
while (this->recv_buf) {
if (!this->connected()) {
throw runtime_error("IPSS channel closed");
throw std::runtime_error("IPSS channel closed");
}
this->data_available_signal.clear();
co_await this->data_available_signal.wait();
@@ -280,18 +281,18 @@ asio::awaitable<void> IPSSChannel::recv_raw(void* data, size_t size) {
}
}
IPStackSimulator::IPStackSimulator(shared_ptr<ServerState> state)
IPStackSimulator::IPStackSimulator(std::shared_ptr<ServerState> state)
: Server(state->io_context, "[IPStackSimulator] "), state(state) {
this->host_mac_address_bytes.clear(0x90);
this->broadcast_mac_address_bytes.clear(0xFF);
}
void IPStackSimulator::listen(const std::string& name, const string& addr, int port, VirtualNetworkProtocol protocol) {
void IPStackSimulator::listen(const std::string& name, const std::string& addr, int port, VirtualNetworkProtocol protocol) {
if (port == 0) {
throw std::runtime_error("Listening port cannot be zero");
}
asio::ip::address asio_addr = addr.empty() ? asio::ip::address_v4::any() : asio::ip::make_address(addr);
auto sock = make_shared<IPSSSocket>();
auto sock = std::make_shared<IPSSSocket>();
sock->name = name;
sock->endpoint = asio::ip::tcp::endpoint(asio_addr, port);
sock->protocol = protocol;
@@ -318,12 +319,12 @@ uint64_t IPStackSimulator::tcp_conn_key_for_client_frame(const IPv4Header& ipv4,
uint64_t IPStackSimulator::tcp_conn_key_for_client_frame(const FrameInfo& fi) {
if (!fi.ipv4 || !fi.tcp) {
throw logic_error("tcp_conn_key_for_frame called on non-TCP frame");
throw std::logic_error("tcp_conn_key_for_frame called on non-TCP frame");
}
return IPStackSimulator::tcp_conn_key_for_client_frame(*fi.ipv4, *fi.tcp);
}
string IPStackSimulator::str_for_ipv4_netloc(uint32_t addr, uint16_t port) {
std::string IPStackSimulator::str_for_ipv4_netloc(uint32_t addr, uint16_t port) {
be_uint32_t be_addr = addr;
char addr_str[INET_ADDRSTRLEN];
memset(addr_str, 0, sizeof(addr_str));
@@ -334,16 +335,16 @@ string IPStackSimulator::str_for_ipv4_netloc(uint32_t addr, uint16_t port) {
}
}
string IPStackSimulator::str_for_tcp_connection(
shared_ptr<const IPSSClient> c, std::shared_ptr<const IPSSClient::TCPConnection> conn) {
std::string IPStackSimulator::str_for_tcp_connection(
std::shared_ptr<const IPSSClient> c, std::shared_ptr<const IPSSClient::TCPConnection> conn) {
uint64_t key = IPStackSimulator::tcp_conn_key_for_connection(conn);
string server_netloc_str = str_for_ipv4_netloc(conn->server_addr, conn->server_port);
string client_netloc_str = str_for_ipv4_netloc(c->ipv4_addr, conn->client_port);
std::string server_netloc_str = str_for_ipv4_netloc(conn->server_addr, conn->server_port);
std::string client_netloc_str = str_for_ipv4_netloc(c->ipv4_addr, conn->client_port);
return std::format("{:016X} ({} -> {})", key, client_netloc_str, server_netloc_str);
}
asio::awaitable<void> IPStackSimulator::send_ethernet_tapserver_frame(
shared_ptr<IPSSClient> c, FrameInfo::Protocol proto, const void* data, size_t size) const {
std::shared_ptr<IPSSClient> c, FrameInfo::Protocol proto, const void* data, size_t size) const {
struct TapServerEthernetHeader {
phosg::le_uint16_t frame_size;
@@ -356,9 +357,9 @@ asio::awaitable<void> IPStackSimulator::send_ethernet_tapserver_frame(
header.ether.src_mac = this->host_mac_address_bytes;
switch (proto) {
case FrameInfo::Protocol::NONE:
throw logic_error("layer 3 protocol not specified");
throw std::logic_error("layer 3 protocol not specified");
case FrameInfo::Protocol::LCP:
throw logic_error("cannot send LCP frame over Ethernet");
throw std::logic_error("cannot send LCP frame over Ethernet");
case FrameInfo::Protocol::IPV4:
header.ether.protocol = 0x0800;
break;
@@ -366,16 +367,17 @@ asio::awaitable<void> IPStackSimulator::send_ethernet_tapserver_frame(
header.ether.protocol = 0x0806;
break;
default:
throw logic_error("unknown layer 3 protocol");
throw std::logic_error("unknown layer 3 protocol");
}
header.frame_size = size + sizeof(EthernetHeader);
array<asio::const_buffer, 2> bufs{asio::buffer(static_cast<const void*>(&header), sizeof(header)), asio::buffer(data, size)};
std::array<asio::const_buffer, 2> bufs{
asio::buffer(static_cast<const void*>(&header), sizeof(header)), asio::buffer(data, size)};
co_await asio::async_write(c->sock, bufs, asio::use_awaitable);
}
asio::awaitable<void> IPStackSimulator::send_hdlc_frame(
shared_ptr<IPSSClient> c, FrameInfo::Protocol proto, const void* data, size_t size, bool is_raw) const {
std::shared_ptr<IPSSClient> c, FrameInfo::Protocol proto, const void* data, size_t size, bool is_raw) const {
HDLCHeader hdlc;
hdlc.start_sentinel1 = 0x7E;
@@ -383,7 +385,7 @@ asio::awaitable<void> IPStackSimulator::send_hdlc_frame(
hdlc.control = 0x03;
switch (proto) {
case FrameInfo::Protocol::NONE:
throw logic_error("layer 3 protocol not specified");
throw std::logic_error("layer 3 protocol not specified");
case FrameInfo::Protocol::LCP:
hdlc.protocol = 0xC021;
break;
@@ -397,9 +399,9 @@ asio::awaitable<void> IPStackSimulator::send_hdlc_frame(
hdlc.protocol = 0x0021;
break;
case FrameInfo::Protocol::ARP:
throw runtime_error("cannot send ARP packets over HDLC");
throw std::runtime_error("cannot send ARP packets over HDLC");
default:
throw logic_error("unknown layer 3 protocol");
throw std::logic_error("unknown layer 3 protocol");
}
phosg::StringWriter w;
@@ -408,14 +410,14 @@ asio::awaitable<void> IPStackSimulator::send_hdlc_frame(
w.put_u16l(FrameInfo::computed_hdlc_checksum(w.str().data() + 1, w.size() - 1));
w.put_u8(0x7E);
string escaped = escape_hdlc_frame(w.str(), c->hdlc_escape_control_character_flags);
std::string escaped = escape_hdlc_frame(w.str(), c->hdlc_escape_control_character_flags);
if (this->log.debug_f("Sending HDLC frame to virtual network (escaped to {:X} bytes)", escaped.size())) {
phosg::print_data(stderr, w.str());
}
if (!is_raw) {
phosg::le_uint16_t frame_size = escaped.size();
array<asio::const_buffer, 2> bufs{
std::array<asio::const_buffer, 2> bufs{
asio::buffer(static_cast<const void*>(&frame_size), sizeof(frame_size)),
asio::buffer(escaped.data(), escaped.size())};
co_await asio::async_write(c->sock, bufs, asio::use_awaitable);
@@ -425,7 +427,7 @@ asio::awaitable<void> IPStackSimulator::send_hdlc_frame(
}
asio::awaitable<void> IPStackSimulator::send_layer3_frame(
shared_ptr<IPSSClient> c, FrameInfo::Protocol proto, const void* data, size_t size) const {
std::shared_ptr<IPSSClient> c, FrameInfo::Protocol proto, const void* data, size_t size) const {
switch (c->protocol) {
case VirtualNetworkProtocol::ETHERNET_TAPSERVER:
co_await this->send_ethernet_tapserver_frame(c, proto, data, size);
@@ -437,11 +439,11 @@ asio::awaitable<void> IPStackSimulator::send_layer3_frame(
co_await this->send_hdlc_frame(c, proto, data, size, true);
break;
default:
throw logic_error("unknown link type");
throw std::logic_error("unknown link type");
}
}
asio::awaitable<void> IPStackSimulator::on_client_frame(shared_ptr<IPSSClient> c, const void* data, size_t size) {
asio::awaitable<void> IPStackSimulator::on_client_frame(std::shared_ptr<IPSSClient> c, const void* data, size_t size) {
FrameInfo::LinkType link_type = (c->protocol == VirtualNetworkProtocol::ETHERNET_TAPSERVER)
? FrameInfo::LinkType::ETHERNET
: FrameInfo::LinkType::HDLC;
@@ -459,17 +461,17 @@ asio::awaitable<void> IPStackSimulator::on_client_frame(shared_ptr<IPSSClient> c
if (c->mac_addr.is_filled_with(0)) {
c->mac_addr = fi.ether->src_mac;
} else if ((fi.ether->src_mac != c->mac_addr) && (fi.ether->src_mac != this->broadcast_mac_address_bytes)) {
throw runtime_error("client sent IPv4 packet from different MAC address");
throw std::runtime_error("client sent IPv4 packet from different MAC address");
}
} else if (fi.hdlc) {
uint16_t expected_checksum = fi.computed_hdlc_checksum();
uint16_t stored_checksum = fi.stored_hdlc_checksum();
if (expected_checksum != stored_checksum) {
throw runtime_error(std::format(
throw std::runtime_error(std::format(
"HDLC checksum is incorrect ({:04X} expected, {:04X} received)", expected_checksum, stored_checksum));
}
} else {
throw runtime_error("frame is not Ethernet or HDLC");
throw std::runtime_error("frame is not Ethernet or HDLC");
}
if (fi.lcp) {
@@ -487,18 +489,18 @@ asio::awaitable<void> IPStackSimulator::on_client_frame(shared_ptr<IPSSClient> c
} else if (fi.ipv4) {
uint16_t expected_ipv4_checksum = fi.computed_ipv4_header_checksum();
if (fi.ipv4->checksum != expected_ipv4_checksum) {
throw runtime_error(std::format(
throw std::runtime_error(std::format(
"IPv4 header checksum is incorrect ({:04X} expected, {:04X} received)", expected_ipv4_checksum, fi.ipv4->checksum));
}
if ((fi.ipv4->src_addr != c->ipv4_addr) && (fi.ipv4->src_addr != 0)) {
throw runtime_error("client sent IPv4 packet from different IPv4 address");
throw std::runtime_error("client sent IPv4 packet from different IPv4 address");
}
if (fi.udp) {
uint16_t expected_udp_checksum = fi.computed_udp4_checksum();
if (fi.udp->checksum != expected_udp_checksum) {
throw runtime_error(std::format(
throw std::runtime_error(std::format(
"UDP checksum is incorrect ({:04X} expected, {:04X} received)", expected_udp_checksum, fi.udp->checksum));
}
co_await this->on_client_udp_frame(c, fi);
@@ -506,27 +508,27 @@ asio::awaitable<void> IPStackSimulator::on_client_frame(shared_ptr<IPSSClient> c
} else if (fi.tcp) {
uint16_t expected_tcp_checksum = fi.computed_tcp4_checksum();
if (fi.tcp->checksum != expected_tcp_checksum) {
throw runtime_error(std::format(
throw std::runtime_error(std::format(
"TCP checksum is incorrect ({:04X} expected, {:04X} received)", expected_tcp_checksum, fi.tcp->checksum));
}
co_await this->on_client_tcp_frame(c, fi);
} else {
throw runtime_error("frame uses unsupported IPv4 protocol");
throw std::runtime_error("frame uses unsupported IPv4 protocol");
}
} else {
throw runtime_error("frame is not IPv4");
throw std::runtime_error("frame is not IPv4");
}
}
asio::awaitable<void> IPStackSimulator::on_client_lcp_frame(shared_ptr<IPSSClient> c, const FrameInfo& fi) {
asio::awaitable<void> IPStackSimulator::on_client_lcp_frame(std::shared_ptr<IPSSClient> c, const FrameInfo& fi) {
switch (fi.lcp->command) {
case 0x01: { // Configure-Request
auto opts_r = fi.read_payload();
while (!opts_r.eof()) {
uint8_t opt = opts_r.get_u8();
string opt_data = opts_r.read(opts_r.get_u8() - 2);
std::string opt_data = opts_r.read(opts_r.get_u8() - 2);
phosg::StringReader opt_data_r(opt_data);
switch (opt) {
case 0x01: // Maximum receive unit
@@ -544,9 +546,9 @@ asio::awaitable<void> IPStackSimulator::on_client_lcp_frame(shared_ptr<IPSSClien
case 0x04: // Quality protocol
case 0x07: // Protocol field compression
case 0x08: // Address and control field compression
throw runtime_error(std::format("unimplemented LCP option {:02X} ({} bytes)", opt, opt_data.size()));
throw std::runtime_error(std::format("unimplemented LCP option {:02X} ({} bytes)", opt, opt_data.size()));
default:
throw runtime_error("unknown LCP option");
throw std::runtime_error("unknown LCP option");
}
}
// Technically, we should implement the LCP state machine, but I'm too lazy to do this right now. In our
@@ -589,14 +591,14 @@ asio::awaitable<void> IPStackSimulator::on_client_lcp_frame(shared_ptr<IPSSClien
case 0x05: { // Terminate-Request
c->ipv4_addr = 0;
c->tcp_connections.clear();
string response(reinterpret_cast<const char*>(fi.payload), fi.payload_size);
std::string response(reinterpret_cast<const char*>(fi.payload), fi.payload_size);
response.at(0) = 0x06; // Terminate-Ack
co_await this->send_layer3_frame(c, FrameInfo::Protocol::LCP, response);
break;
}
case 0x09: { // Echo-Request
string response(reinterpret_cast<const char*>(fi.payload), fi.payload_size);
std::string response(reinterpret_cast<const char*>(fi.payload), fi.payload_size);
response.at(0) = 0x0A; // Echo-Reply
co_await this->send_layer3_frame(c, FrameInfo::Protocol::LCP, response);
break;
@@ -612,23 +614,23 @@ asio::awaitable<void> IPStackSimulator::on_client_lcp_frame(shared_ptr<IPSSClien
case 0x07: // Code-Reject
case 0x08: // Protocol-Reject
case 0x0A: // Echo-Reply
throw runtime_error("unimplemented LCP command");
throw std::runtime_error("unimplemented LCP command");
default:
throw runtime_error("unknown LCP command");
throw std::runtime_error("unknown LCP command");
}
}
asio::awaitable<void> IPStackSimulator::on_client_pap_frame(shared_ptr<IPSSClient> c, const FrameInfo& fi) {
asio::awaitable<void> IPStackSimulator::on_client_pap_frame(std::shared_ptr<IPSSClient> c, const FrameInfo& fi) {
if (fi.pap->command != 0x01) { // Authenticate-Request
throw runtime_error("client sent incorrect PAP command");
throw std::runtime_error("client sent incorrect PAP command");
}
auto r = fi.read_payload();
string username = r.read(r.get_u8());
string password = r.read(r.get_u8());
std::string username = r.read(r.get_u8());
std::string password = r.read(r.get_u8());
this->log.info_f("Client logged in with username \"{}\" and password", username);
static const string login_message = "newserv PPP simulator";
static const std::string login_message = "newserv PPP simulator";
phosg::StringWriter w;
w.put<PAPHeader>(PAPHeader{
.command = 0x02, // Authenticate-Ack
@@ -640,7 +642,7 @@ asio::awaitable<void> IPStackSimulator::on_client_pap_frame(shared_ptr<IPSSClien
co_await this->send_layer3_frame(c, FrameInfo::Protocol::PAP, w.str());
}
asio::awaitable<void> IPStackSimulator::on_client_ipcp_frame(shared_ptr<IPSSClient> c, const FrameInfo& fi) {
asio::awaitable<void> IPStackSimulator::on_client_ipcp_frame(std::shared_ptr<IPSSClient> c, const FrameInfo& fi) {
switch (fi.ipcp->command) {
case 0x01: { // Configure-Request
auto opts_r = fi.read_payload();
@@ -651,11 +653,11 @@ asio::awaitable<void> IPStackSimulator::on_client_ipcp_frame(shared_ptr<IPSSClie
phosg::StringWriter rejected_opts_w;
while (!opts_r.eof()) {
uint8_t opt = opts_r.get_u8();
string opt_data = opts_r.read(opts_r.get_u8() - 2);
std::string opt_data = opts_r.read(opts_r.get_u8() - 2);
phosg::StringReader opt_data_r(opt_data);
switch (opt) {
case 0x01: // IP addresses (deprecated as of 1992; we don't support it at all)
throw runtime_error("IPCP client sent IP-Addresses option");
throw std::runtime_error("IPCP client sent IP-Addresses option");
case 0x02: // IP compression protocol
rejected_opts_w.put_u8(0x02);
rejected_opts_w.put_u8(opt_data_r.size() + 2);
@@ -673,9 +675,9 @@ asio::awaitable<void> IPStackSimulator::on_client_ipcp_frame(shared_ptr<IPSSClie
case 0x82: // Primary NBNS server address
case 0x84: // Secondary NBNS server address
case 0x04: // Mobile IP address
throw runtime_error(std::format("unimplemented IPCP option {:02X} ({} bytes)", opt, opt_data.size()));
throw std::runtime_error(std::format("unimplemented IPCP option {:02X} ({} bytes)", opt, opt_data.size()));
default:
throw runtime_error("unknown IPCP option");
throw std::runtime_error("unknown IPCP option");
}
}
@@ -753,7 +755,7 @@ asio::awaitable<void> IPStackSimulator::on_client_ipcp_frame(shared_ptr<IPSSClie
case 0x05: { // Terminate-Request
c->ipv4_addr = 0;
c->tcp_connections.clear();
string response(reinterpret_cast<const char*>(fi.payload), fi.payload_size);
std::string response(reinterpret_cast<const char*>(fi.payload), fi.payload_size);
response.at(0) = 0x06; // Terminate-Ack
co_await this->send_layer3_frame(c, FrameInfo::Protocol::LCP, response);
break;
@@ -766,21 +768,21 @@ asio::awaitable<void> IPStackSimulator::on_client_ipcp_frame(shared_ptr<IPSSClie
case 0x04: // Configure-Reject
case 0x06: // Terminate-Ack
case 0x07: // Code-Reject
throw runtime_error("unimplemented IPCP command");
throw std::runtime_error("unimplemented IPCP command");
default:
throw runtime_error("unknown LCP command");
throw std::runtime_error("unknown LCP command");
}
}
asio::awaitable<void> IPStackSimulator::on_client_arp_frame(shared_ptr<IPSSClient> c, const FrameInfo& fi) {
asio::awaitable<void> IPStackSimulator::on_client_arp_frame(std::shared_ptr<IPSSClient> c, const FrameInfo& fi) {
if (fi.arp->hwaddr_len != 6 ||
fi.arp->paddr_len != 4 ||
fi.arp->hardware_type != 0x0001 ||
fi.arp->protocol_type != 0x0800) {
throw runtime_error("unsupported ARP parameters");
throw std::runtime_error("unsupported ARP parameters");
}
if (fi.payload_size < 20) {
throw runtime_error("ARP payload too small");
throw std::runtime_error("ARP payload too small");
}
if (c->ipv4_addr == 0) {
@@ -814,7 +816,7 @@ asio::awaitable<void> IPStackSimulator::on_client_arp_frame(shared_ptr<IPSSClien
co_await this->send_layer3_frame(c, FrameInfo::Protocol::ARP, w.str());
}
asio::awaitable<void> IPStackSimulator::on_client_udp_frame(shared_ptr<IPSSClient> c, const FrameInfo& fi) {
asio::awaitable<void> IPStackSimulator::on_client_udp_frame(std::shared_ptr<IPSSClient> c, const FrameInfo& fi) {
// We only implement DHCP and newserv's DNS server here.
// Every received UDP packet will elicit exactly one UDP response from newserv, so we prepare the headers in advance
@@ -836,24 +838,24 @@ asio::awaitable<void> IPStackSimulator::on_client_udp_frame(shared_ptr<IPSSClien
// r_udp.size filled in later
// r_udp.checksum filled in later
string r_data;
std::string r_data;
if (fi.udp->dest_port == 67) { // DHCP
auto r = fi.read_payload();
const auto& dhcp = r.get<DHCPHeader>();
if (dhcp.hardware_type != 1) {
throw runtime_error("unknown DHCP hardware type");
throw std::runtime_error("unknown DHCP hardware type");
}
if (dhcp.hardware_address_length != 6) {
throw runtime_error("unknown DHCP hardware address length");
throw std::runtime_error("unknown DHCP hardware address length");
}
if (dhcp.magic != 0x63825363) {
throw runtime_error("incorrect DHCP magic cookie");
throw std::runtime_error("incorrect DHCP magic cookie");
}
if (dhcp.opcode != 1) { // Request
throw runtime_error("DHCP packet is not a request");
throw std::runtime_error("DHCP packet is not a request");
}
unordered_map<uint8_t, string> option_data;
std::unordered_map<uint8_t, std::string> option_data;
for (;;) {
uint8_t option = r.get_u8();
if (option == 0xFF) {
@@ -866,8 +868,8 @@ asio::awaitable<void> IPStackSimulator::on_client_udp_frame(shared_ptr<IPSSClien
uint8_t command = 0;
try {
command = option_data.at(53).at(0);
} catch (const out_of_range&) {
throw runtime_error("client did not send a DHCP command option");
} catch (const std::out_of_range&) {
throw std::runtime_error("client did not send a DHCP command option");
}
if (command == 7) {
@@ -883,7 +885,7 @@ asio::awaitable<void> IPStackSimulator::on_client_udp_frame(shared_ptr<IPSSClien
r_ipv4.dest_addr = c->ipv4_addr;
if ((command != 1) && (command != 3)) {
throw runtime_error("client sent unknown DHCP command option");
throw std::runtime_error("client sent unknown DHCP command option");
}
phosg::StringWriter w;
@@ -949,19 +951,19 @@ asio::awaitable<void> IPStackSimulator::on_client_udp_frame(shared_ptr<IPSSClien
r_data = std::move(w.str());
} else {
throw runtime_error("client sent unknown DHCP command");
throw std::runtime_error("client sent unknown DHCP command");
}
} else if (fi.udp->dest_port == 53) { // DNS
if (fi.payload_size < 0x0C) {
throw runtime_error("DNS payload too small");
throw std::runtime_error("DNS payload too small");
}
uint32_t resolved_address = this->connect_address_for_remote_address(c->ipv4_addr);
r_data = DNSServer::response_for_query(fi.payload, fi.payload_size, resolved_address);
} else { // Not DHCP or DNS
throw runtime_error("UDP packet is not DHCP or DNS");
throw std::runtime_error("UDP packet is not DHCP or DNS");
}
if (!r_data.empty()) {
@@ -971,7 +973,7 @@ asio::awaitable<void> IPStackSimulator::on_client_udp_frame(shared_ptr<IPSSClien
r_udp.checksum = FrameInfo::computed_udp4_checksum(r_ipv4, r_udp, r_data.data(), r_data.size());
if (this->log.should_log(phosg::LogLevel::L_DEBUG)) {
string remote_str = this->str_for_ipv4_netloc(fi.ipv4->src_addr, fi.udp->src_port);
std::string remote_str = this->str_for_ipv4_netloc(fi.ipv4->src_addr, fi.udp->src_port);
this->log.debug_f("Sending UDP response to {}", remote_str);
phosg::print_data(stderr, r_data);
}
@@ -985,19 +987,19 @@ asio::awaitable<void> IPStackSimulator::on_client_udp_frame(shared_ptr<IPSSClien
}
}
asio::awaitable<void> IPStackSimulator::on_client_tcp_frame(shared_ptr<IPSSClient> c, const FrameInfo& fi) {
asio::awaitable<void> IPStackSimulator::on_client_tcp_frame(std::shared_ptr<IPSSClient> c, const FrameInfo& fi) {
this->log.debug_f("Virtual network sent TCP frame (seq={:08X}, ack={:08X})",
fi.tcp->seq_num, fi.tcp->ack_num);
if (fi.tcp->flags & (TCPHeader::Flag::NS | TCPHeader::Flag::CWR | TCPHeader::Flag::ECE | TCPHeader::Flag::URG)) {
throw runtime_error("unsupported flag in TCP packet");
throw std::runtime_error("unsupported flag in TCP packet");
}
if (fi.tcp->flags & TCPHeader::Flag::SYN) {
// We never make connections back to the client, so we should never receive a SYN+ACK. Essentially, no other flags
// should be set in any received SYN.
if ((fi.tcp->flags & 0x0FFF) != TCPHeader::Flag::SYN) {
throw runtime_error("TCP SYN contains extra flags");
throw std::runtime_error("TCP SYN contains extra flags");
}
phosg::StringReader options_r(fi.tcp + 1, fi.tcp_options_size);
@@ -1013,19 +1015,19 @@ asio::awaitable<void> IPStackSimulator::on_client_tcp_frame(shared_ptr<IPSSClien
break;
case 2: // Max segment size
if (option_size != 4) {
throw runtime_error("incorrect size for TCP max frame size option");
throw std::runtime_error("incorrect size for TCP max frame size option");
}
max_frame_size = options_r.get_u16b();
break;
case 3: // Window scale (ignored)
if (option_size != 3) {
throw runtime_error("incorrect size for TCP window scale option");
throw std::runtime_error("incorrect size for TCP window scale option");
}
options_r.skip(option_size);
break;
case 4: // Selective ACK supported (ignored)
if (option_size != 2) {
throw runtime_error("incorrect size for TCP selective ACK supported option");
throw std::runtime_error("incorrect size for TCP selective ACK supported option");
}
break;
case 5: // Selective ACK (ignored)
@@ -1033,21 +1035,21 @@ asio::awaitable<void> IPStackSimulator::on_client_tcp_frame(shared_ptr<IPSSClien
break;
case 8: // Timestamps (ignored)
if (option_size != 10) {
throw runtime_error("incorrect size for TCP timestamps option");
throw std::runtime_error("incorrect size for TCP timestamps option");
}
options_r.skip(8);
break;
default:
throw runtime_error("invalid TCP option");
throw std::runtime_error("invalid TCP option");
}
}
shared_ptr<IPSSClient::TCPConnection> conn;
string conn_str;
std::shared_ptr<IPSSClient::TCPConnection> conn;
std::string conn_str;
uint64_t key = this->tcp_conn_key_for_client_frame(fi);
auto conn_it = c->tcp_connections.find(key);
if (conn_it == c->tcp_connections.end()) {
conn = make_shared<IPSSClient::TCPConnection>(c);
conn = std::make_shared<IPSSClient::TCPConnection>(c);
c->tcp_connections.emplace(key, conn);
conn->server_addr = fi.ipv4->dest_addr;
conn->server_port = fi.tcp->dest_port;
@@ -1068,7 +1070,7 @@ asio::awaitable<void> IPStackSimulator::on_client_tcp_frame(shared_ptr<IPSSClien
// Connection is NOT new; this is probably a resend of an earlier SYN
if (!conn->awaiting_first_ack) {
throw logic_error("SYN received on already-open connection after initial phase");
throw std::logic_error("SYN received on already-open connection after initial phase");
}
// TODO: We should check the syn/ack numbers here instead of just assuming they're correct
conn_str = this->str_for_tcp_connection(c, conn);
@@ -1098,7 +1100,7 @@ asio::awaitable<void> IPStackSimulator::on_client_tcp_frame(shared_ptr<IPSSClien
this->log.debug_f("Client sent ACK {:08X}", fi.tcp->ack_num);
if (conn->awaiting_first_ack) {
if (fi.tcp->ack_num != conn->acked_server_seq + 1) {
throw runtime_error("first ack_num was not acked_server_seq + 1");
throw std::runtime_error("first ack_num was not acked_server_seq + 1");
}
conn->acked_server_seq++;
conn->awaiting_first_ack = false;
@@ -1109,7 +1111,7 @@ asio::awaitable<void> IPStackSimulator::on_client_tcp_frame(shared_ptr<IPSSClien
this->log.debug_f("Advancing acked_server_seq from {:08X}", conn->acked_server_seq);
uint32_t ack_delta = fi.tcp->ack_num - conn->acked_server_seq;
if (conn->outbound_data_bytes < ack_delta) {
throw runtime_error("client acknowledged beyond end of sent data");
throw std::runtime_error("client acknowledged beyond end of sent data");
}
conn->drain_outbound_data(ack_delta);
@@ -1123,7 +1125,7 @@ asio::awaitable<void> IPStackSimulator::on_client_tcp_frame(shared_ptr<IPSSClien
ack_delta, conn->acked_server_seq);
} else if (seq_num_less(fi.tcp->ack_num, conn->acked_server_seq)) {
throw runtime_error("client sent lower ack num than previous frame");
throw std::runtime_error("client sent lower ack num than previous frame");
}
}
@@ -1135,10 +1137,10 @@ asio::awaitable<void> IPStackSimulator::on_client_tcp_frame(shared_ptr<IPSSClien
if (fi.tcp->flags & (TCPHeader::Flag::RST | TCPHeader::Flag::FIN)) {
bool is_rst = (fi.tcp->flags & TCPHeader::Flag::RST);
if (is_rst && (fi.tcp->flags & TCPHeader::Flag::FIN)) {
throw runtime_error("client sent TCP FIN+RST");
throw std::runtime_error("client sent TCP FIN+RST");
}
string conn_str = this->str_for_tcp_connection(c, conn);
std::string conn_str = this->str_for_tcp_connection(c, conn);
this->log.info_f("Client closed TCP connection {}", conn_str);
if (conn->server_channel) {
conn->server_channel->disconnect();
@@ -1160,7 +1162,7 @@ asio::awaitable<void> IPStackSimulator::on_client_tcp_frame(shared_ptr<IPSSClien
// newserv can handle incomplete commands, so we just ignore the PSH flag and forward any data to the server
// immediately (hence the lack of a flag check in the above condition).
string conn_str = this->log.should_log(phosg::LogLevel::L_WARNING)
std::string conn_str = this->log.should_log(phosg::LogLevel::L_WARNING)
? this->str_for_tcp_connection(c, conn)
: "";
@@ -1188,7 +1190,7 @@ asio::awaitable<void> IPStackSimulator::on_client_tcp_frame(shared_ptr<IPSSClien
}
if (payload_skip_bytes > fi.payload_size) {
throw logic_error("payload skip bytes too large");
throw std::logic_error("payload skip bytes too large");
}
if (payload_skip_bytes < fi.payload_size) {
@@ -1237,9 +1239,9 @@ asio::awaitable<void> IPStackSimulator::on_client_tcp_frame(shared_ptr<IPSSClien
}
}
void IPStackSimulator::schedule_send_pending_push_frame(shared_ptr<IPSSClient::TCPConnection> conn, uint64_t delay_usecs) {
void IPStackSimulator::schedule_send_pending_push_frame(std::shared_ptr<IPSSClient::TCPConnection> conn, uint64_t delay_usecs) {
conn->resend_push_timer.expires_after(std::chrono::microseconds(delay_usecs));
conn->resend_push_timer.async_wait([wconn = weak_ptr<IPSSClient::TCPConnection>(conn)](std::error_code ec) {
conn->resend_push_timer.async_wait([wconn = std::weak_ptr<IPSSClient::TCPConnection>(conn)](std::error_code ec) {
if (ec) {
return;
}
@@ -1260,7 +1262,7 @@ void IPStackSimulator::schedule_send_pending_push_frame(shared_ptr<IPSSClient::T
}
asio::awaitable<void> IPStackSimulator::send_pending_push_frame(
shared_ptr<IPSSClient> c, shared_ptr<IPSSClient::TCPConnection> conn) {
std::shared_ptr<IPSSClient> c, std::shared_ptr<IPSSClient::TCPConnection> conn) {
if (!conn->outbound_data_bytes) {
if (!conn->server_channel || !conn->server_channel->connected()) {
co_await this->close_tcp_connection(c, conn);
@@ -1268,11 +1270,11 @@ asio::awaitable<void> IPStackSimulator::send_pending_push_frame(
co_return;
}
size_t bytes_to_send = min<size_t>(conn->outbound_data_bytes, conn->next_push_max_frame_size);
size_t bytes_to_send = std::min<size_t>(conn->outbound_data_bytes, conn->next_push_max_frame_size);
if (c->protocol == VirtualNetworkProtocol::HDLC_TAPSERVER) {
// There is a bug in Dolphin's modem implementation (which I wrote, so it's my fault) that causes commands to be
// dropped when too much data is sent. To work around this, we only send up to 200 bytes in each push frame.
bytes_to_send = min<size_t>(bytes_to_send, 200);
bytes_to_send = std::min<size_t>(bytes_to_send, 200);
}
this->log.debug_f("Sending PSH frame with seq_num {:08X}, 0x{:X}/0x{:X} data bytes",
@@ -1282,7 +1284,7 @@ asio::awaitable<void> IPStackSimulator::send_pending_push_frame(
if (conn->outbound_data.empty() || conn->outbound_data.front().size() < bytes_to_send) {
// This should never happen because bytes_to_send should always be less than or equal to conn->outbound_data_bytes,
// which itself should be equal to the number of bytes that can be linearized
throw logic_error("failed to linearize enough bytes before sending TCP PSH");
throw std::logic_error("failed to linearize enough bytes before sending TCP PSH");
}
co_await this->send_tcp_frame(c, conn, TCPHeader::Flag::PSH, conn->outbound_data.front().data(), bytes_to_send);
conn->awaiting_ack = true;
@@ -1298,17 +1300,17 @@ asio::awaitable<void> IPStackSimulator::send_pending_push_frame(
if (conn->resend_push_usecs > 5000000) {
conn->resend_push_usecs = 5000000;
}
conn->next_push_max_frame_size = max<size_t>(0x100, conn->next_push_max_frame_size - 0x100);
conn->next_push_max_frame_size = std::max<size_t>(0x100, conn->next_push_max_frame_size - 0x100);
}
asio::awaitable<void> IPStackSimulator::send_tcp_frame(
shared_ptr<IPSSClient> c,
shared_ptr<IPSSClient::TCPConnection> conn,
std::shared_ptr<IPSSClient> c,
std::shared_ptr<IPSSClient::TCPConnection> conn,
uint16_t flags,
const void* payload_data,
size_t payload_size) {
if (!payload_data != !(flags & TCPHeader::Flag::PSH)) {
throw logic_error("data should be given if and only if PSH is given");
throw std::logic_error("data should be given if and only if PSH is given");
}
IPv4Header ipv4;
@@ -1348,42 +1350,67 @@ asio::awaitable<void> IPStackSimulator::send_tcp_frame(
}
asio::awaitable<void> IPStackSimulator::open_server_connection(
shared_ptr<IPSSClient> c, shared_ptr<IPSSClient::TCPConnection> conn) {
std::shared_ptr<IPSSClient> c, std::shared_ptr<IPSSClient::TCPConnection> conn) {
if (conn->server_channel) {
throw logic_error("server connection is already open");
throw std::logic_error("server connection is already open");
}
string conn_str = this->str_for_tcp_connection(c, conn);
std::string conn_str = this->str_for_tcp_connection(c, conn);
// Figure out which logical port the connection should go to
auto port_config_it = this->state->number_to_port_config.find(conn->server_port);
if (port_config_it == this->state->number_to_port_config.end()) {
this->log.error_f("TCP connection {} is to undefined port {}", conn_str, conn->server_port);
uint16_t effective_server_port = conn->server_port;
auto remap_it = this->state->data->ip_stack_port_remap.find(effective_server_port);
if (remap_it != this->state->data->ip_stack_port_remap.end()) {
this->log.info_f(
"Remapping IP stack TCP destination port {} to {} for connection {}",
effective_server_port,
remap_it->second,
conn_str);
effective_server_port = remap_it->second;
}
auto port_config_it = this->state->data->number_to_port_config.find(effective_server_port);
if (port_config_it == this->state->data->number_to_port_config.end()) {
this->log.error_f(
"TCP connection {} is to undefined port {} after remap from {}",
conn_str,
effective_server_port,
conn->server_port);
co_await this->close_tcp_connection(c, conn);
co_return;
}
const auto& port_config = port_config_it->second;
conn->server_channel = make_shared<IPSSChannel>(this->shared_from_this(), c, conn, port_config->version, Language::ENGLISH);
conn->server_channel = std::make_shared<IPSSChannel>(
this->shared_from_this(),
c,
conn,
port_config.version,
Language::ENGLISH,
"",
phosg::TerminalFormat::END,
phosg::TerminalFormat::END,
false,
this->state->data->censor_credentials);
if (!this->state->game_server.get()) {
this->log.error_f("No server available for TCP connection {}", conn_str);
co_await this->close_tcp_connection(c, conn);
co_return;
} else {
this->state->game_server->connect_channel(conn->server_channel, conn->server_port, port_config->behavior);
this->state->game_server->connect_channel(conn->server_channel, effective_server_port, port_config.behavior);
this->log.info_f("Connected TCP connection {} to game server", conn_str);
}
}
asio::awaitable<void> IPStackSimulator::close_tcp_connection(
shared_ptr<IPSSClient> c, shared_ptr<IPSSClient::TCPConnection> conn) {
std::shared_ptr<IPSSClient> c, std::shared_ptr<IPSSClient::TCPConnection> conn) {
// Send an RST to the client. This is kind of rude (we really should use FIN) but the PSO network stack always sends
// an RST to us when disconnecting, so whatever
co_await this->send_tcp_frame(c, conn, TCPHeader::Flag::RST);
// Delete the connection object
string conn_str = this->str_for_tcp_connection(c, conn);
std::string conn_str = this->str_for_tcp_connection(c, conn);
this->log.info_f("Server closed TCP connection {}", conn_str);
c->tcp_connections.erase(this->tcp_conn_key_for_connection(conn));
}
@@ -1391,7 +1418,7 @@ asio::awaitable<void> IPStackSimulator::close_tcp_connection(
std::shared_ptr<IPSSClient> IPStackSimulator::create_client(
std::shared_ptr<IPSSSocket> listen_sock, asio::ip::tcp::socket&& client_sock) {
uint32_t addr = ipv4_addr_for_asio_addr(client_sock.remote_endpoint().address());
if (this->state->banned_ipv4_ranges->check(addr)) {
if (this->state->data->banned_ipv4_ranges->check(addr)) {
if (client_sock.is_open()) {
client_sock.close();
}
@@ -1400,14 +1427,14 @@ std::shared_ptr<IPSSClient> IPStackSimulator::create_client(
uint64_t network_id = this->next_network_id++;
this->log.info_f("Virtual network N-{:X} connected via {}", network_id, listen_sock->name);
return make_shared<IPSSClient>(this->shared_from_this(), network_id, listen_sock->protocol, std::move(client_sock));
return std::make_shared<IPSSClient>(this->shared_from_this(), network_id, listen_sock->protocol, std::move(client_sock));
}
asio::awaitable<void> IPStackSimulator::handle_tapserver_client(std::shared_ptr<IPSSClient> c) {
for (;;) {
le_uint16_t frame_size;
co_await asio::async_read(c->sock, asio::buffer(&frame_size, sizeof(frame_size)), asio::use_awaitable);
string frame(frame_size, '\0');
std::string frame(frame_size, '\0');
co_await asio::async_read(c->sock, asio::buffer(frame.data(), frame.size()), asio::use_awaitable);
if (c->protocol == VirtualNetworkProtocol::HDLC_TAPSERVER) {
@@ -1416,7 +1443,7 @@ asio::awaitable<void> IPStackSimulator::handle_tapserver_client(std::shared_ptr<
try {
co_await this->on_client_frame(c, frame.data(), frame.size());
} catch (const exception& e) {
} catch (const std::exception& e) {
if (this->log.warning_f("Failed to process frame: {}", e.what())) {
phosg::print_data(stderr, frame);
}
@@ -1442,10 +1469,10 @@ asio::awaitable<void> IPStackSimulator::handle_hdlc_raw_client(std::shared_ptr<I
size_t frame_start_offset = 0;
while (buffer.size() > frame_start_offset) {
if (buffer[frame_start_offset] != 0x7E) {
throw runtime_error("HDLC frame does not begin with 7E");
throw std::runtime_error("HDLC frame does not begin with 7E");
}
size_t frame_end_offset = buffer.find(0x7E, frame_start_offset + 1);
if (frame_end_offset == string::npos) {
if (frame_end_offset == std::string::npos) {
break;
}
frame_end_offset++;
@@ -1456,7 +1483,7 @@ asio::awaitable<void> IPStackSimulator::handle_hdlc_raw_client(std::shared_ptr<I
try {
co_await this->on_client_frame(c, frame_data, unescaped_size);
} catch (const exception& e) {
} catch (const std::exception& e) {
if (this->log.warning_f("Failed to process frame: {}", e.what())) {
phosg::print_data(stderr, frame_data, unescaped_size);
}
@@ -1467,7 +1494,7 @@ asio::awaitable<void> IPStackSimulator::handle_hdlc_raw_client(std::shared_ptr<I
// Delete the processed packets from the beginning of the buffer
if (frame_start_offset > buffer_bytes) {
throw logic_error("frame start offset is beyond buffer bounds");
throw std::logic_error("frame start offset is beyond buffer bounds");
} else if (frame_start_offset == buffer_bytes) {
buffer_bytes = 0;
} else if (frame_start_offset > 0) {
+10 -13
View File
@@ -94,10 +94,8 @@ struct IPSSClient : std::enable_shared_from_this<IPSSClient> {
void reschedule_idle_timeout();
};
// IPSSChannel provides an "unwrapped" connection to the rest of the server. It
// implements the Channel interface and can be used in place of an
// SocketChannel, so the rest of the server doesn't have to know about
// IPStackSimulator.
// IPSSChannel provides an "unwrapped" connection to the rest of the server. It implements the Channel interface and
// can be used in place of an SocketChannel, so the rest of the server doesn't have to know about IPStackSimulator.
class IPSSChannel : public Channel {
public:
std::shared_ptr<IPStackSimulator> sim;
@@ -110,18 +108,19 @@ public:
std::weak_ptr<IPSSClient::TCPConnection> tcp_conn,
Version version,
Language language,
const std::string& name = "",
phosg::TerminalFormat terminal_send_color = phosg::TerminalFormat::END,
phosg::TerminalFormat terminal_recv_color = phosg::TerminalFormat::END);
const std::string& name,
phosg::TerminalFormat terminal_send_color,
phosg::TerminalFormat terminal_recv_color,
bool censor_received_credentials,
bool censor_sent_credentials);
virtual std::string default_name() const;
virtual bool connected() const;
virtual void disconnect();
// Adds inbound data, which will then be available via recv_raw(). This
// function is called by IPStackSimulator to forward "unwrapped" data to
// the game/proxy servers.
// Adds inbound data, which will then be available via recv_raw(). This function is called by IPStackSimulator to
// forward "unwrapped" data to the game/proxy servers.
void add_inbound_data(const void* data, size_t size);
virtual void send_raw(std::string&& data);
@@ -134,9 +133,7 @@ private:
size_t recv_buf_size = 0;
};
class IPStackSimulator
: public Server<IPSSClient, IPSSSocket>,
public std::enable_shared_from_this<IPStackSimulator> {
class IPStackSimulator : public Server<IPSSClient, IPSSSocket>, public std::enable_shared_from_this<IPStackSimulator> {
public:
IPStackSimulator(std::shared_ptr<ServerState> state);
~IPStackSimulator() = default;
+4 -6
View File
@@ -1,7 +1,5 @@
#include "IPV4RangeSet.hh"
using namespace std;
IPV4RangeSet::IPV4RangeSet(const phosg::JSON& json) {
for (const auto& it : json.as_list()) {
// String should be of the form a.b.c.d or a.b.c.d/e
@@ -13,22 +11,22 @@ IPV4RangeSet::IPV4RangeSet(const phosg::JSON& json) {
} else if (tokens.size() == 2) {
mask_bits = stoul(tokens[1], nullptr, 10);
if (mask_bits > 32) {
throw runtime_error("invalid IPv4 address range");
throw std::runtime_error("invalid IPv4 address range");
}
} else {
throw runtime_error("invalid IPv4 address range");
throw std::runtime_error("invalid IPv4 address range");
}
auto addr_tokens = phosg::split(tokens[0], '.');
if (addr_tokens.size() != 4) {
throw runtime_error("invalid IPv4 address");
throw std::runtime_error("invalid IPv4 address");
}
uint32_t addr = 0;
for (size_t z = 0; z < 4; z++) {
size_t end_pos = 0;
size_t new_byte = stoul(addr_tokens[z], &end_pos, 10);
if (end_pos != addr_tokens[z].size() || new_byte > 0xFF) {
throw runtime_error("invalid IPv4 address");
throw std::runtime_error("invalid IPv4 address");
}
addr = (addr << 8) | new_byte;
}
+13 -11
View File
@@ -7,8 +7,6 @@
#include "Text.hh"
using namespace std;
struct GVMFileEntry {
be_uint16_t file_num;
pstring<TextEncoding::ASCII, 0x1C> name;
@@ -35,21 +33,25 @@ struct GVRHeader {
be_uint16_t height;
} __packed_ws__(GVRHeader, 0x10);
string encode_gvm(const phosg::ImageRGBA8888N& img, GVRDataFormat data_format, const string& internal_name, uint32_t global_index) {
std::string encode_gvm(
const phosg::ImageRGBA8888N& img,
GVRDataFormat data_format,
const std::string& internal_name,
uint32_t global_index) {
int8_t dimensions_field = -2;
{
size_t h = img.get_height();
size_t w = img.get_width();
if ((h != w) || (w & (w - 1)) || (h & (h - 1))) {
throw runtime_error("image must be square and dimensions must be powers of 2");
throw std::runtime_error("image must be square and dimensions must be powers of 2");
}
for (w >>= 1; w; w >>= 1, dimensions_field++) {
}
if (dimensions_field < 1) {
throw runtime_error("image is too small");
throw std::runtime_error("image is too small");
}
if (dimensions_field > 0xF) {
throw runtime_error("image is too large");
throw std::runtime_error("image is too large");
}
}
@@ -64,7 +66,7 @@ string encode_gvm(const phosg::ImageRGBA8888N& img, GVRDataFormat data_format, c
pixel_bytes = pixel_count * 2;
break;
default:
throw invalid_argument("cannot encode pixel format");
throw std::invalid_argument("cannot encode pixel format");
}
phosg::StringWriter w;
@@ -102,7 +104,7 @@ string encode_gvm(const phosg::ImageRGBA8888N& img, GVRDataFormat data_format, c
w.put_u32b(phosg::argb8888_for_rgba8888(c));
break;
default:
throw logic_error("cannot encode pixel format");
throw std::logic_error("cannot encode pixel format");
}
}
}
@@ -112,9 +114,9 @@ string encode_gvm(const phosg::ImageRGBA8888N& img, GVRDataFormat data_format, c
return std::move(w.str());
}
static const array<uint32_t, 4> fon_colors = {0x000000FF, 0x555555FF, 0xAAAAAAFF, 0xFFFFFFFF};
static const std::array<uint32_t, 4> fon_colors = {0x000000FF, 0x555555FF, 0xAAAAAAFF, 0xFFFFFFFF};
phosg::ImageRGB888 decode_fon(const string& data, size_t width) {
phosg::ImageRGB888 decode_fon(const std::string& data, size_t width) {
size_t num_pixels = data.size() * 4;
size_t height = num_pixels / width;
phosg::ImageRGB888 ret(width, height);
@@ -132,7 +134,7 @@ constexpr size_t uabs(size_t a, size_t b) {
return (a > b) ? (a - b) : (b - a);
}
string encode_fon(const phosg::ImageRGB888& img) {
std::string encode_fon(const phosg::ImageRGB888& img) {
phosg::BitWriter w;
for (size_t y = 0; y < img.get_height(); y++) {
for (size_t x = 0; x < img.get_width(); x++) {
+94 -71
View File
@@ -19,22 +19,17 @@
#include "SaveFileFormats.hh"
#include "Text.hh"
using namespace std;
IntegralExpression::IntegralExpression(const string& text)
: root(this->parse_expr(text)) {}
IntegralExpression::IntegralExpression(const std::string& text) : root(this->parse_expr(text)) {}
IntegralExpression::BinaryOperatorNode::BinaryOperatorNode(
Type type, unique_ptr<const Node>&& left, unique_ptr<const Node>&& right)
: type(type),
left(std::move(left)),
right(std::move(right)) {}
Type type, std::unique_ptr<const Node>&& left, std::unique_ptr<const Node>&& right)
: type(type), left(std::move(left)), right(std::move(right)) {}
bool IntegralExpression::BinaryOperatorNode::operator==(const Node& other) const {
try {
const BinaryOperatorNode& other_bin = dynamic_cast<const BinaryOperatorNode&>(other);
return other_bin.type == this->type && *other_bin.left == *this->left && *other_bin.right == *this->right;
} catch (const bad_cast&) {
} catch (const std::bad_cast&) {
return false;
}
}
@@ -78,11 +73,11 @@ int64_t IntegralExpression::BinaryOperatorNode::evaluate(const Env& env) const {
case Type::MODULUS:
return this->left->evaluate(env) % this->right->evaluate(env);
default:
throw logic_error("invalid binary operator type");
throw std::logic_error("invalid binary operator type");
}
}
string IntegralExpression::BinaryOperatorNode::str() const {
std::string IntegralExpression::BinaryOperatorNode::str() const {
switch (this->type) {
case Type::LOGICAL_OR:
return "(" + this->left->str() + ") || (" + this->right->str() + ")";
@@ -121,19 +116,18 @@ string IntegralExpression::BinaryOperatorNode::str() const {
case Type::MODULUS:
return "(" + this->left->str() + ") % (" + this->right->str() + ")";
default:
throw logic_error("invalid binary operator type");
throw std::logic_error("invalid binary operator type");
}
}
IntegralExpression::UnaryOperatorNode::UnaryOperatorNode(Type type, unique_ptr<const Node>&& sub)
: type(type),
sub(std::move(sub)) {}
IntegralExpression::UnaryOperatorNode::UnaryOperatorNode(Type type, std::unique_ptr<const Node>&& sub)
: type(type), sub(std::move(sub)) {}
bool IntegralExpression::UnaryOperatorNode::operator==(const Node& other) const {
try {
const UnaryOperatorNode& other_un = dynamic_cast<const UnaryOperatorNode&>(other);
return other_un.type == this->type && *other_un.sub == *this->sub;
} catch (const bad_cast&) {
} catch (const std::bad_cast&) {
return false;
}
}
@@ -147,11 +141,11 @@ int64_t IntegralExpression::UnaryOperatorNode::evaluate(const Env& env) const {
case Type::NEGATIVE:
return -this->sub->evaluate(env);
default:
throw logic_error("invalid unary operator type");
throw std::logic_error("invalid unary operator type");
}
}
string IntegralExpression::UnaryOperatorNode::str() const {
std::string IntegralExpression::UnaryOperatorNode::str() const {
switch (this->type) {
case Type::LOGICAL_NOT:
return "!(" + this->sub->str() + ")";
@@ -160,30 +154,46 @@ string IntegralExpression::UnaryOperatorNode::str() const {
case Type::NEGATIVE:
return "-(" + this->sub->str() + ")";
default:
throw logic_error("invalid unary operator type");
throw std::logic_error("invalid unary operator type");
}
}
IntegralExpression::FlagLookupNode::FlagLookupNode(uint16_t flag_index) : flag_index(flag_index) {}
bool IntegralExpression::SectionIDLookupNode::operator==(const Node& other) const {
return (dynamic_cast<const SectionIDLookupNode*>(&other) != nullptr);
}
bool IntegralExpression::FlagLookupNode::operator==(const Node& other) const {
int64_t IntegralExpression::SectionIDLookupNode::evaluate(const Env& env) const {
return env.section_id;
}
std::string IntegralExpression::SectionIDLookupNode::str() const {
return "P_SID";
}
IntegralExpression::QuestFlagLookupNode::QuestFlagLookupNode(Difficulty difficulty, uint16_t flag_index)
: difficulty(difficulty), flag_index(flag_index) {}
bool IntegralExpression::QuestFlagLookupNode::operator==(const Node& other) const {
try {
const FlagLookupNode& other_flag = dynamic_cast<const FlagLookupNode&>(other);
return other_flag.flag_index == this->flag_index;
} catch (const bad_cast&) {
const QuestFlagLookupNode& other_flag = dynamic_cast<const QuestFlagLookupNode&>(other);
return ((other_flag.difficulty == this->difficulty) && (other_flag.flag_index == this->flag_index));
} catch (const std::bad_cast&) {
return false;
}
}
int64_t IntegralExpression::FlagLookupNode::evaluate(const Env& env) const {
int64_t IntegralExpression::QuestFlagLookupNode::evaluate(const Env& env) const {
if (!env.flags) {
throw runtime_error("quest flags not available");
throw std::runtime_error("quest flags not available");
}
return env.flags->get(this->flag_index) ? 1 : 0;
Difficulty effective_difficulty = (this->difficulty == Difficulty::UNKNOWN) ? env.difficulty : this->difficulty;
return env.flags->get(effective_difficulty, this->flag_index) ? 1 : 0;
}
string IntegralExpression::FlagLookupNode::str() const {
return std::format("F_{:04X}", this->flag_index);
std::string IntegralExpression::QuestFlagLookupNode::str() const {
return (this->difficulty == Difficulty::UNKNOWN)
? std::format("F_{:04X}", this->flag_index)
: std::format("F_{:c}_{:04X}", abbreviation_for_difficulty(this->difficulty), this->flag_index);
}
IntegralExpression::ChallengeCompletionLookupNode::ChallengeCompletionLookupNode(Episode episode, uint8_t stage_index)
@@ -193,14 +203,14 @@ bool IntegralExpression::ChallengeCompletionLookupNode::operator==(const Node& o
try {
const ChallengeCompletionLookupNode& other_cc = dynamic_cast<const ChallengeCompletionLookupNode&>(other);
return other_cc.episode == this->episode && other_cc.stage_index == this->stage_index;
} catch (const bad_cast&) {
} catch (const std::bad_cast&) {
return false;
}
}
int64_t IntegralExpression::ChallengeCompletionLookupNode::evaluate(const Env& env) const {
if (!env.challenge_records) {
throw runtime_error("challenge records not available");
throw std::runtime_error("challenge records not available");
}
if (this->episode == Episode::EP1) {
return env.challenge_records->times_ep1_online.at(this->stage_index).has_value();
@@ -210,17 +220,17 @@ int64_t IntegralExpression::ChallengeCompletionLookupNode::evaluate(const Env& e
return false;
}
string IntegralExpression::ChallengeCompletionLookupNode::str() const {
std::string IntegralExpression::ChallengeCompletionLookupNode::str() const {
return std::format("CC_{}_{}", abbreviation_for_episode(this->episode), static_cast<uint8_t>(this->stage_index + 1));
}
IntegralExpression::TeamRewardLookupNode::TeamRewardLookupNode(const string& reward_name) : reward_name(reward_name) {}
IntegralExpression::TeamRewardLookupNode::TeamRewardLookupNode(const std::string& reward_name) : reward_name(reward_name) {}
bool IntegralExpression::TeamRewardLookupNode::operator==(const Node& other) const {
try {
const TeamRewardLookupNode& other_team_reward = dynamic_cast<const TeamRewardLookupNode&>(other);
return other_team_reward.reward_name == this->reward_name;
} catch (const bad_cast&) {
} catch (const std::bad_cast&) {
return false;
}
}
@@ -229,7 +239,7 @@ int64_t IntegralExpression::TeamRewardLookupNode::evaluate(const Env& env) const
return (env.team && env.team->has_reward(this->reward_name)) ? 1 : 0;
}
string IntegralExpression::TeamRewardLookupNode::str() const {
std::string IntegralExpression::TeamRewardLookupNode::str() const {
return "T_" + this->reward_name;
}
@@ -243,7 +253,7 @@ int64_t IntegralExpression::NumPlayersLookupNode::evaluate(const Env& env) const
return env.num_players;
}
string IntegralExpression::NumPlayersLookupNode::str() const {
std::string IntegralExpression::NumPlayersLookupNode::str() const {
return "V_NumPlayers";
}
@@ -257,7 +267,7 @@ int64_t IntegralExpression::EventLookupNode::evaluate(const Env& env) const {
return env.event;
}
string IntegralExpression::EventLookupNode::str() const {
std::string IntegralExpression::EventLookupNode::str() const {
return "V_Event";
}
@@ -271,7 +281,7 @@ int64_t IntegralExpression::V1PresenceLookupNode::evaluate(const Env& env) const
return env.v1_present ? 1 : 0;
}
string IntegralExpression::V1PresenceLookupNode::str() const {
std::string IntegralExpression::V1PresenceLookupNode::str() const {
return "V_V1Present";
}
@@ -282,7 +292,7 @@ bool IntegralExpression::ConstantNode::operator==(const Node& other) const {
try {
const ConstantNode& other_const = dynamic_cast<const ConstantNode&>(other);
return other_const.value == this->value;
} catch (const bad_cast&) {
} catch (const std::bad_cast&) {
return false;
}
}
@@ -291,11 +301,11 @@ int64_t IntegralExpression::ConstantNode::evaluate(const Env&) const {
return this->value;
}
string IntegralExpression::ConstantNode::str() const {
std::string IntegralExpression::ConstantNode::str() const {
return std::format("{}", this->value);
}
unique_ptr<const IntegralExpression::Node> IntegralExpression::parse_expr(string_view text) {
std::unique_ptr<const IntegralExpression::Node> IntegralExpression::parse_expr(std::string_view text) {
// Strip off spaces and fully-enclosing parentheses
for (;;) {
size_t starting_size = text.size();
@@ -329,22 +339,22 @@ unique_ptr<const IntegralExpression::Node> IntegralExpression::parse_expr(string
}
}
if (text.empty()) {
throw runtime_error("invalid expression");
throw std::runtime_error("invalid expression");
}
// Check for binary operators at the root level
using BinType = BinaryOperatorNode::Type;
static const vector<vector<pair<std::string, BinaryOperatorNode::Type>>> binary_operator_levels = {
{{make_pair("||", BinType::LOGICAL_OR)}},
{{make_pair("&&", BinType::LOGICAL_AND)}},
{{make_pair("|", BinType::BITWISE_OR)}},
{{make_pair("^", BinType::BITWISE_XOR)}},
{{make_pair("&", BinType::BITWISE_AND)}},
{{make_pair("==", BinType::EQUAL)}, {make_pair("!=", BinType::NOT_EQUAL)}},
{{make_pair("<=", BinType::LESS_OR_EQUAL)}, {make_pair(">=", BinType::GREATER_OR_EQUAL)}, {make_pair("<", BinType::LESS_THAN)}, {make_pair(">", BinType::GREATER_THAN)}},
{{make_pair("<<", BinType::LEFT_SHIFT)}, {make_pair(">>", BinType::RIGHT_SHIFT)}},
{{make_pair("+", BinType::ADD)}, {make_pair("-", BinType::SUBTRACT)}},
{{make_pair("*", BinType::MULTIPLY)}, {make_pair("/", BinType::DIVIDE)}, {make_pair("%", BinType::MODULUS)}},
static const std::vector<std::vector<std::pair<std::string, BinaryOperatorNode::Type>>> binary_operator_levels = {
{{std::make_pair("||", BinType::LOGICAL_OR)}},
{{std::make_pair("&&", BinType::LOGICAL_AND)}},
{{std::make_pair("|", BinType::BITWISE_OR)}},
{{std::make_pair("^", BinType::BITWISE_XOR)}},
{{std::make_pair("&", BinType::BITWISE_AND)}},
{{std::make_pair("==", BinType::EQUAL)}, {std::make_pair("!=", BinType::NOT_EQUAL)}},
{{std::make_pair("<=", BinType::LESS_OR_EQUAL)}, {std::make_pair(">=", BinType::GREATER_OR_EQUAL)}, {std::make_pair("<", BinType::LESS_THAN)}, {std::make_pair(">", BinType::GREATER_THAN)}},
{{std::make_pair("<<", BinType::LEFT_SHIFT)}, {std::make_pair(">>", BinType::RIGHT_SHIFT)}},
{{std::make_pair("+", BinType::ADD)}, {std::make_pair("-", BinType::SUBTRACT)}},
{{std::make_pair("*", BinType::MULTIPLY)}, {std::make_pair("/", BinType::DIVIDE)}, {std::make_pair("%", BinType::MODULUS)}},
};
for (const auto& operators : binary_operator_levels) {
size_t paren_level = 0;
@@ -386,16 +396,29 @@ unique_ptr<const IntegralExpression::Node> IntegralExpression::parse_expr(string
}
// Check for env lookups
if (text == "P_SID") {
return std::make_unique<SectionIDLookupNode>();
}
if (text.starts_with("F_")) {
Difficulty difficulty = Difficulty::UNKNOWN;
if (text.starts_with("F_N_")) {
difficulty = Difficulty::NORMAL;
} else if (text.starts_with("F_H_")) {
difficulty = Difficulty::HARD;
} else if (text.starts_with("F_V_")) {
difficulty = Difficulty::VERY_HARD;
} else if (text.starts_with("F_U_")) {
difficulty = Difficulty::ULTIMATE;
}
char* endptr = nullptr;
uint64_t flag = strtoul(text.data() + 2, &endptr, 16);
uint64_t flag = strtoul(text.data() + ((difficulty == Difficulty::UNKNOWN) ? 2 : 4), &endptr, 16);
if (endptr != text.data() + text.size()) {
throw runtime_error("invalid flag lookup token");
throw std::runtime_error("invalid flag lookup token");
}
if (flag >= 0x400) {
throw runtime_error("invalid flag index");
throw std::runtime_error("invalid flag index");
}
return make_unique<FlagLookupNode>(flag);
return std::make_unique<QuestFlagLookupNode>(difficulty, flag);
}
if (text.starts_with("CC_")) {
Episode episode;
@@ -404,45 +427,45 @@ unique_ptr<const IntegralExpression::Node> IntegralExpression::parse_expr(string
} else if (text.starts_with("CC_Ep2_")) {
episode = Episode::EP2;
} else {
throw runtime_error("invalid challenge episode");
throw std::runtime_error("invalid challenge episode");
}
char* endptr = nullptr;
uint64_t stage_index = strtoul(text.data() + 7, &endptr, 0) - 1;
if (endptr != text.data() + text.size()) {
throw runtime_error("invalid challenge completion lookup token");
throw std::runtime_error("invalid challenge completion lookup token");
}
if ((episode == Episode::EP1 && stage_index > 8) || (episode == Episode::EP2 && stage_index > 4)) {
throw runtime_error("invalid challenge stage index");
throw std::runtime_error("invalid challenge stage index");
}
return make_unique<ChallengeCompletionLookupNode>(episode, stage_index);
return std::make_unique<ChallengeCompletionLookupNode>(episode, stage_index);
}
if (text.starts_with("T_")) {
return make_unique<TeamRewardLookupNode>(string(text.substr(2)));
return std::make_unique<TeamRewardLookupNode>(std::string(text.substr(2)));
}
if (text == "V_NumPlayers") {
return make_unique<NumPlayersLookupNode>();
return std::make_unique<NumPlayersLookupNode>();
}
if (text == "V_Event") {
return make_unique<EventLookupNode>();
return std::make_unique<EventLookupNode>();
}
if (text == "V_V1Present") {
return make_unique<V1PresenceLookupNode>();
return std::make_unique<V1PresenceLookupNode>();
}
// Check for constants
if (text == "true") {
return make_unique<ConstantNode>(1);
return std::make_unique<ConstantNode>(1);
}
if (text == "false") {
return make_unique<ConstantNode>(0);
return std::make_unique<ConstantNode>(0);
}
try {
size_t endpos;
int64_t v = stoll(string(text), &endpos, 0);
int64_t v = std::stoll(std::string(text), &endpos, 0);
if (endpos == text.size()) {
return make_unique<ConstantNode>(v);
return std::make_unique<ConstantNode>(v);
}
} catch (const exception&) {
} catch (const std::exception&) {
}
throw runtime_error("unparseable expression");
throw std::runtime_error("unparseable expression");
}
+16 -4
View File
@@ -15,7 +15,9 @@
class IntegralExpression {
public:
struct Env {
const QuestFlagsForDifficulty* flags;
uint8_t section_id;
Difficulty difficulty;
const QuestFlags* flags;
const PlayerRecordsChallengeBB* challenge_records;
std::shared_ptr<const TeamIndex::Team> team;
size_t num_players;
@@ -105,15 +107,25 @@ protected:
std::unique_ptr<const Node> sub;
};
class FlagLookupNode : public Node {
class SectionIDLookupNode : public Node {
public:
FlagLookupNode(uint16_t flag_index);
virtual ~FlagLookupNode() = default;
SectionIDLookupNode() = default;
virtual ~SectionIDLookupNode() = default;
virtual bool operator==(const Node& other) const;
virtual int64_t evaluate(const Env& env) const;
virtual std::string str() const;
};
class QuestFlagLookupNode : public Node {
public:
QuestFlagLookupNode(Difficulty difficulty, uint16_t flag_index);
virtual ~QuestFlagLookupNode() = default;
virtual bool operator==(const Node& other) const;
virtual int64_t evaluate(const Env& env) const;
virtual std::string str() const;
protected:
Difficulty difficulty;
uint16_t flag_index;
};
+191 -302
View File
@@ -6,36 +6,80 @@
#include "EnemyType.hh"
#include "Loggers.hh"
using namespace std;
// Note: There are clearly better ways of doing this, but this implementation closely follows what the original code in
// the client does.
template <typename ItemT, size_t MaxCount>
struct ProbabilityTable {
ItemT items[MaxCount];
size_t count;
// The favored weapon type table is hardcoded in the game client. The table is:
// Viridia shots
// Greennill rifles
// Skyly swords
// Bluefull partisans
// Purplenum mechguns
// Pinkal canes
// Redria (none)
// Oran daggers
// Yellowboze (none)
// Whitill slicers
static const array<uint8_t, 10> favored_weapon_by_section_id = {
0x09, 0x07, 0x02, 0x04, 0x08, 0x0A, 0xFF, 0x03, 0xFF, 0x05};
ProbabilityTable() : count(0) {}
ProbabilityTable(const std::vector<ShopRandomSetBase::IntPairT<ItemT>>& table) : ProbabilityTable() {
for (const auto& entry : table) {
for (size_t y = 0; y < entry.weight; y++) {
this->push(entry.value);
}
}
}
template <size_t Count>
ProbabilityTable(const std::array<ShopRandomSetBase::IntPairT<ItemT>, Count>& table) : ProbabilityTable() {
for (const auto& entry : table) {
for (size_t y = 0; y < entry.weight; y++) {
this->push(entry.value);
}
}
}
void push(ItemT item) {
if (this->count == MaxCount) {
throw std::runtime_error("push to full probability table");
}
this->items[this->count++] = item;
}
ItemT pop() {
if (this->count == 0) {
throw std::runtime_error("pop from empty probability table");
}
return this->items[--this->count];
}
void shuffle(std::shared_ptr<RandomGenerator> rand_crypt) {
for (size_t z = 1; z < this->count; z++) {
size_t other_z = rand_crypt->next() % (z + 1);
ItemT t = this->items[z];
this->items[z] = this->items[other_z];
this->items[other_z] = t;
}
}
ItemT sample(std::shared_ptr<RandomGenerator> rand_crypt) const {
if (this->count == 0) {
throw std::runtime_error("sample from empty probability table");
} else if (this->count == 1) {
return this->items[0];
} else {
return this->items[rand_crypt->next() % this->count];
}
}
};
ItemCreator::ItemCreator(
shared_ptr<const CommonItemSet> common_item_set,
shared_ptr<const RareItemSet> rare_item_set,
shared_ptr<const ArmorRandomSet> armor_random_set,
shared_ptr<const ToolRandomSet> tool_random_set,
shared_ptr<const WeaponRandomSet> weapon_random_set,
shared_ptr<const TekkerAdjustmentSet> tekker_adjustment_set,
shared_ptr<const ItemParameterTable> item_parameter_table,
std::shared_ptr<const CommonItemSet> common_item_set,
std::shared_ptr<const RareItemSet> rare_item_set,
std::shared_ptr<const ArmorShopRandomSet> armor_random_set,
std::shared_ptr<const ToolShopRandomSet> tool_random_set,
std::shared_ptr<const WeaponShopRandomSet> weapon_random_set,
std::shared_ptr<const TekkerAdjustmentSet> tekker_adjustment_set,
std::shared_ptr<const ItemParameterTable> item_parameter_table,
std::shared_ptr<const ItemData::StackLimits> stack_limits,
GameMode mode,
Difficulty difficulty,
uint8_t section_id,
std::shared_ptr<RandomGenerator> rand_crypt,
shared_ptr<const BattleRules> restrictions)
std::shared_ptr<const BattleRules> restrictions)
: log(std::format("[ItemCreator:{}/{}/{}/{}] ", phosg::name_for_enum(stack_limits->version), abbreviation_for_mode(mode), abbreviation_for_difficulty(difficulty), section_id), lobby_log.min_level),
logic_version(stack_limits->version),
is_legacy_replay(false),
@@ -175,7 +219,7 @@ ItemCreator::DropResult ItemCreator::on_box_item_drop(uint8_t area, bool force_r
case 6: // Nothing
break;
default:
throw logic_error("this should be impossible");
throw std::logic_error("this should be impossible");
}
if (item_class < 6) {
this->generate_common_item_variances(res.item, area);
@@ -183,7 +227,7 @@ ItemCreator::DropResult ItemCreator::on_box_item_drop(uint8_t area, bool force_r
}
return res;
} catch (const exception& e) {
} catch (const std::exception& e) {
this->log.error_f("Exception in item creation: {}", e.what());
return DropResult();
}
@@ -233,13 +277,13 @@ ItemCreator::DropResult ItemCreator::on_monster_item_drop(EnemyType enemy_type,
case 2:
try {
item_class = pt->enemy_type_item_classes.at(enemy_type);
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
this->log.info_f("Item class is not set for this enemy type");
item_class = 0xFF;
}
break;
default:
throw logic_error("invalid item class determinant");
throw std::logic_error("invalid item class determinant");
}
this->log.info_f(
@@ -265,7 +309,7 @@ ItemCreator::DropResult ItemCreator::on_monster_item_drop(EnemyType enemy_type,
res.item.data1[0] = 0x04;
try {
res.item.data2d = this->choose_meseta_amount(pt->enemy_type_meseta_ranges.at(enemy_type)) & 0xFFFF;
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
this->log.info_f("Meseta range is not set for this enemy type");
return DropResult();
}
@@ -281,7 +325,7 @@ ItemCreator::DropResult ItemCreator::on_monster_item_drop(EnemyType enemy_type,
return res;
} catch (const exception& e) {
} catch (const std::exception& e) {
this->log.error_f("Exception in item creation: {}", e.what());
return DropResult();
}
@@ -408,7 +452,7 @@ ItemData ItemCreator::create_rare_item(const ItemData& drop_item, uint8_t area)
case 4:
break;
default:
throw logic_error("invalid item class");
throw std::logic_error("invalid item class");
}
this->set_item_kill_count_if_unsealable(item);
}
@@ -424,7 +468,7 @@ void ItemCreator::generate_rare_weapon_bonuses(ItemData& item, Episode episode,
auto pt = this->pt(episode);
if (!pt->has_rare_bonus_value_prob_table) {
throw logic_error("generate_rare_weapon_bonuses called for common item table without rare bonus value probability table");
throw std::logic_error("generate_rare_weapon_bonuses called for common item table without rare bonus value probability table");
}
for (size_t z = 0; z < 6; z += 2) {
@@ -557,7 +601,7 @@ void ItemCreator::clear_item_if_restricted(ItemData& item) const {
item.clear();
break;
default:
throw logic_error("invalid weapon and armor mode");
throw std::logic_error("invalid weapon and armor mode");
}
break;
case 2:
@@ -588,7 +632,7 @@ void ItemCreator::clear_item_if_restricted(ItemData& item) const {
}
break;
default:
throw logic_error("invalid tech disk mode");
throw std::logic_error("invalid tech disk mode");
}
} else if ((item.data1[1] == 9) && this->restrictions->forbid_scape_dolls) {
this->log.info_f("Restricted: scape dolls not allowed");
@@ -602,7 +646,7 @@ void ItemCreator::clear_item_if_restricted(ItemData& item) const {
}
break;
default:
throw logic_error("invalid item");
throw std::logic_error("invalid item");
}
}
}
@@ -642,7 +686,7 @@ void ItemCreator::generate_common_item_variances(ItemData& item, uint8_t area) {
// Note: The original code does the following here:
// item.clear();
// item.data1[0] = 0x05;
throw logic_error("invalid item class");
throw std::logic_error("invalid item class");
}
this->clear_item_if_restricted(item);
@@ -706,7 +750,7 @@ void ItemCreator::generate_common_tool_variances(ItemData& item, uint8_t area) {
item.data1[0] = 0x03;
item.data1[1] = data.first;
item.data1[2] = data.second;
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
this->log.info_f("Tool class is missing; skipping item generation");
return;
}
@@ -795,7 +839,7 @@ void ItemCreator::generate_common_weapon_variances(ItemData& item, uint8_t area)
void ItemCreator::generate_common_weapon_grind(ItemData& item, uint8_t area, uint8_t offset_within_subtype_range) {
if (item.data1[0] == 0) {
uint8_t offset = clamp<uint8_t>(offset_within_subtype_range, 0, 3);
uint8_t offset = std::clamp<uint8_t>(offset_within_subtype_range, 0, 3);
item.data1[3] = this->get_rand_from_weighted_tables_2d_vertical(this->pt(area)->grind_prob_table, offset);
this->log.info_f("Generated grind {:02X} from offset within subtype range {:02X}", item.data1[3], offset_within_subtype_range);
}
@@ -860,7 +904,7 @@ void ItemCreator::generate_unit_stars_tables() {
case Version::BB_PATCH:
case Version::GC_EP3_NTE:
case Version::GC_EP3:
throw logic_error("ItemCreator cannot be created for Episode 3 games");
throw std::logic_error("ItemCreator cannot be created for Episode 3 games");
case Version::DC_NTE:
star_base_index = 0x124;
num_units = 0x43;
@@ -890,7 +934,7 @@ void ItemCreator::generate_unit_stars_tables() {
num_units = 0x64;
break;
default:
throw logic_error("invalid game version");
throw std::logic_error("invalid game version");
}
for (auto& vec : this->unit_results_by_star_count) {
@@ -944,7 +988,7 @@ IntT ItemCreator::get_rand_from_weighted_tables(const IntT* tables, size_t offse
rand_max += tables[x * stride + offset];
}
if (rand_max == 0) {
throw runtime_error("weighted table is empty");
throw std::runtime_error("weighted table is empty");
}
uint32_t x = this->rand_int(rand_max);
@@ -955,7 +999,7 @@ IntT ItemCreator::get_rand_from_weighted_tables(const IntT* tables, size_t offse
}
x -= table_value;
}
throw logic_error("selector was not less than rand_max");
throw std::logic_error("selector was not less than rand_max");
}
template <typename IntT, size_t X>
@@ -968,8 +1012,8 @@ IntT ItemCreator::get_rand_from_weighted_tables_2d_vertical(const parray<parray<
return ItemCreator::get_rand_from_weighted_tables<IntT>(tables[0].data(), offset, Y, X);
}
vector<ItemData> ItemCreator::generate_armor_shop_contents(Episode episode, size_t player_level) {
vector<ItemData> shop;
std::vector<ItemData> ItemCreator::generate_armor_shop_contents(Episode episode, size_t player_level) {
std::vector<ItemData> shop;
this->generate_armor_shop_armors(shop, episode, player_level);
this->generate_armor_shop_shields(shop, player_level);
this->generate_armor_shop_units(shop, player_level);
@@ -992,7 +1036,7 @@ size_t ItemCreator::get_table_index_for_armor_shop(
}
bool ItemCreator::shop_does_not_contain_duplicate_armor(
const vector<ItemData>& shop, const ItemData& item) {
const std::vector<ItemData>& shop, const ItemData& item) {
for (const auto& shop_item : shop) {
if ((shop_item.data1[0] == item.data1[0]) &&
(shop_item.data1[1] == item.data1[1]) &&
@@ -1005,7 +1049,7 @@ bool ItemCreator::shop_does_not_contain_duplicate_armor(
}
bool ItemCreator::shop_does_not_contain_duplicate_tech_disk(
const vector<ItemData>& shop, const ItemData& item) {
const std::vector<ItemData>& shop, const ItemData& item) {
for (const auto& shop_item : shop) {
if ((shop_item.data1[0] == item.data1[0]) &&
(shop_item.data1[1] == item.data1[1]) &&
@@ -1018,7 +1062,7 @@ bool ItemCreator::shop_does_not_contain_duplicate_tech_disk(
}
bool ItemCreator::shop_does_not_contain_duplicate_or_too_many_similar_weapons(
const vector<ItemData>& shop, const ItemData& item) {
const std::vector<ItemData>& shop, const ItemData& item) {
size_t similar_items = 0;
for (const auto& shop_item : shop) {
// Disallow exact matches
@@ -1037,7 +1081,7 @@ bool ItemCreator::shop_does_not_contain_duplicate_or_too_many_similar_weapons(
}
bool ItemCreator::shop_does_not_contain_duplicate_item_by_data1_0_1_2(
const vector<ItemData>& shop, const ItemData& item) {
const std::vector<ItemData>& shop, const ItemData& item) {
for (const auto& shop_item : shop) {
if ((shop_item.data1[0] == item.data1[0]) &&
(shop_item.data1[1] == item.data1[1]) &&
@@ -1048,7 +1092,7 @@ bool ItemCreator::shop_does_not_contain_duplicate_item_by_data1_0_1_2(
return true;
}
void ItemCreator::generate_armor_shop_armors(vector<ItemData>& shop, Episode episode, size_t player_level) {
void ItemCreator::generate_armor_shop_armors(std::vector<ItemData>& shop, Episode episode, size_t player_level) {
size_t num_items;
if (player_level < 11) {
num_items = 4;
@@ -1061,13 +1105,7 @@ void ItemCreator::generate_armor_shop_armors(vector<ItemData>& shop, Episode epi
}
size_t table_index = this->get_table_index_for_armor_shop(player_level);
ProbabilityTable<uint8_t, 100> pt;
auto src_table = this->armor_random_set->get_armor_table(table_index);
for (size_t z = 0; z < src_table.second; z++) {
for (size_t y = 0; y < src_table.first[z].weight; y++) {
pt.push(src_table.first[z].value);
}
}
ProbabilityTable<uint8_t, 100> pt{this->armor_random_set->armor_table.at(table_index)};
pt.shuffle(this->rand_crypt);
for (size_t items_generated = 0; items_generated < num_items;) {
@@ -1092,7 +1130,7 @@ void ItemCreator::generate_armor_shop_armors(vector<ItemData>& shop, Episode epi
}
}
void ItemCreator::generate_armor_shop_shields(vector<ItemData>& shop, size_t player_level) {
void ItemCreator::generate_armor_shop_shields(std::vector<ItemData>& shop, size_t player_level) {
size_t num_items;
if (player_level < 11) {
num_items = 4;
@@ -1105,13 +1143,7 @@ void ItemCreator::generate_armor_shop_shields(vector<ItemData>& shop, size_t pla
}
size_t table_index = this->get_table_index_for_armor_shop(player_level);
ProbabilityTable<uint8_t, 100> pt;
auto src_table = this->armor_random_set->get_shield_table(table_index);
for (size_t z = 0; z < src_table.second; z++) {
for (size_t y = 0; y < src_table.first[z].weight; y++) {
pt.push(src_table.first[z].value);
}
}
ProbabilityTable<uint8_t, 100> pt{this->armor_random_set->shield_table.at(table_index)};
pt.shuffle(this->rand_crypt);
for (size_t items_generated = 0; items_generated < num_items;) {
@@ -1135,7 +1167,7 @@ void ItemCreator::generate_armor_shop_shields(vector<ItemData>& shop, size_t pla
}
}
void ItemCreator::generate_armor_shop_units(vector<ItemData>& shop, size_t player_level) {
void ItemCreator::generate_armor_shop_units(std::vector<ItemData>& shop, size_t player_level) {
size_t num_items;
if (player_level < 11) {
return; // num_items = 0
@@ -1148,13 +1180,7 @@ void ItemCreator::generate_armor_shop_units(vector<ItemData>& shop, size_t playe
}
size_t table_index = this->get_table_index_for_armor_shop(player_level);
ProbabilityTable<uint8_t, 100> pt;
auto src_table = this->armor_random_set->get_unit_table(table_index);
for (size_t z = 0; z < src_table.second; z++) {
for (size_t y = 0; y < src_table.first[z].weight; y++) {
pt.push(src_table.first[z].value);
}
}
ProbabilityTable<uint8_t, 100> pt{this->armor_random_set->unit_table.at(table_index)};
pt.shuffle(this->rand_crypt);
for (size_t items_generated = 0; items_generated < num_items;) {
@@ -1169,8 +1195,8 @@ void ItemCreator::generate_armor_shop_units(vector<ItemData>& shop, size_t playe
}
}
vector<ItemData> ItemCreator::generate_tool_shop_contents(size_t player_level) {
vector<ItemData> shop;
std::vector<ItemData> ItemCreator::generate_tool_shop_contents(size_t player_level) {
std::vector<ItemData> shop;
this->generate_common_tool_shop_recovery_items(shop, player_level);
this->generate_rare_tool_shop_recovery_items(shop, player_level);
this->generate_tool_shop_tech_disks(shop, player_level);
@@ -1192,11 +1218,7 @@ size_t ItemCreator::get_table_index_for_tool_shop(size_t player_level) {
}
}
static const vector<pair<uint8_t, uint8_t>> tool_item_defs{
{0x00, 0x00}, {0x00, 0x01}, {0x00, 0x02}, {0x01, 0x00}, {0x01, 0x01}, {0x01, 0x02}, {0x06, 0x00}, {0x06, 0x01},
{0x03, 0x00}, {0x04, 0x00}, {0x05, 0x00}, {0x07, 0x00}, {0x08, 0x00}, {0x09, 0x00}, {0x0A, 0x00}, {0xFF, 0xFF}};
void ItemCreator::generate_common_tool_shop_recovery_items(vector<ItemData>& shop, size_t player_level) {
void ItemCreator::generate_common_tool_shop_recovery_items(std::vector<ItemData>& shop, size_t player_level) {
size_t table_index;
if (player_level < 11) {
table_index = 0;
@@ -1212,35 +1234,26 @@ void ItemCreator::generate_common_tool_shop_recovery_items(vector<ItemData>& sho
table_index = 5;
}
auto table = this->tool_random_set->get_common_recovery_table(table_index);
for (size_t z = 0; z < table.second; z++) {
uint8_t type = table.first[z];
if (type == 0x0F) {
for (const auto& entry : this->tool_random_set->common_recovery_table.at(table_index)) {
if (entry == 0x0F) {
continue;
}
auto& item = shop.emplace_back();
item.data1[0] = 3;
item.data1[1] = tool_item_defs[type].first;
item.data1[2] = tool_item_defs[type].second;
item.data1[1] = ToolShopRandomSet::item_defs[entry].first;
item.data1[2] = ToolShopRandomSet::item_defs[entry].second;
}
}
void ItemCreator::generate_rare_tool_shop_recovery_items(vector<ItemData>& shop, size_t player_level) {
void ItemCreator::generate_rare_tool_shop_recovery_items(std::vector<ItemData>& shop, size_t player_level) {
if (player_level < 11) {
return;
}
static constexpr size_t num_items = 2;
ProbabilityTable<uint8_t, 100> pt;
size_t table_index = this->get_table_index_for_tool_shop(player_level);
auto table = this->tool_random_set->get_rare_recovery_table(table_index);
for (size_t z = 0; z < table.second; z++) {
const auto& e = table.first[z];
for (size_t y = 0; y < e.weight; y++) {
pt.push(e.value);
}
}
ProbabilityTable<uint8_t, 100> pt{this->tool_random_set->rare_recovery_table.at(table_index)};
pt.shuffle(this->rand_crypt);
size_t effective_num_items = num_items;
@@ -1254,8 +1267,8 @@ void ItemCreator::generate_rare_tool_shop_recovery_items(vector<ItemData>& shop,
} else {
ItemData item;
item.data1[0] = 3;
item.data1[1] = tool_item_defs[type].first;
item.data1[2] = tool_item_defs[type].second;
item.data1[1] = ToolShopRandomSet::item_defs[type].first;
item.data1[2] = ToolShopRandomSet::item_defs[type].second;
if (this->shop_does_not_contain_duplicate_item_by_data1_0_1_2(shop, item)) {
shop.emplace_back(std::move(item));
items_generated++;
@@ -1264,7 +1277,7 @@ void ItemCreator::generate_rare_tool_shop_recovery_items(vector<ItemData>& shop,
}
}
void ItemCreator::generate_tool_shop_tech_disks(vector<ItemData>& shop, size_t player_level) {
void ItemCreator::generate_tool_shop_tech_disks(std::vector<ItemData>& shop, size_t player_level) {
size_t num_items;
if (player_level < 11) {
num_items = 4;
@@ -1275,28 +1288,16 @@ void ItemCreator::generate_tool_shop_tech_disks(vector<ItemData>& shop, size_t p
}
size_t table_index = this->get_table_index_for_tool_shop(player_level);
auto table = this->tool_random_set->get_tech_disk_table(table_index);
ProbabilityTable<uint8_t, 100> pt;
for (size_t z = 0; z < table.second; z++) {
const auto& e = table.first[z];
for (size_t y = 0; y < e.weight; y++) {
pt.push(e.value);
}
}
ProbabilityTable<uint8_t, 100> pt{this->tool_random_set->tech_disk_table.at(table_index)};
pt.shuffle(this->rand_crypt);
static const array<uint8_t, 0x13> tech_num_map = {
0x00, 0x03, 0x06, 0x0F, 0x10, 0x0D, 0x0A, 0x0B, 0x0C, 0x01, 0x04, 0x07,
0x0E, 0x11, 0x02, 0x05, 0x08, 0x09, 0x12};
size_t items_generated = 0;
while (items_generated < num_items) {
uint8_t tech_num_index = pt.pop();
ItemData item;
item.data1[0] = 3;
item.data1[1] = 2;
item.data1[4] = tech_num_map.at(tech_num_index);
item.data1[4] = ToolShopRandomSet::tech_num_map.at(tech_num_index);
this->choose_tech_disk_level_for_tool_shop(item, player_level, tech_num_index);
if (this->shop_does_not_contain_duplicate_tech_disk(shop, item)) {
shop.emplace_back(std::move(item));
@@ -1307,34 +1308,34 @@ void ItemCreator::generate_tool_shop_tech_disks(vector<ItemData>& shop, size_t p
void ItemCreator::choose_tech_disk_level_for_tool_shop(ItemData& item, size_t player_level, uint8_t tech_num_index) {
size_t table_index = this->get_table_index_for_tool_shop(player_level);
auto table = this->tool_random_set->get_tech_disk_level_table(table_index);
if (tech_num_index >= table.second) {
throw runtime_error("technique number out of range");
auto table = this->tool_random_set->tech_disk_level_table.at(table_index);
if (tech_num_index >= table.size()) {
throw std::runtime_error("technique number out of range");
}
const auto& e = table.first[tech_num_index];
const auto& e = table[tech_num_index];
switch (e.mode) {
case ToolRandomSet::TechDiskLevelEntry::Mode::LEVEL_1:
case ToolShopRandomSet::TechDiskLevelEntry::Mode::LEVEL_1:
item.data1[2] = 0;
break;
case ToolRandomSet::TechDiskLevelEntry::Mode::PLAYER_LEVEL_DIVISOR:
item.data1[2] = clamp<ssize_t>(
(min<size_t>(player_level, 99) / e.player_level_divisor_or_min_level) - 1, 0, 14);
case ToolShopRandomSet::TechDiskLevelEntry::Mode::PLAYER_LEVEL_DIVISOR:
item.data1[2] = std::clamp<ssize_t>(
(std::min<size_t>(player_level, 99) / e.player_level_divisor_or_min_level) - 1, 0, 14);
break;
case ToolRandomSet::TechDiskLevelEntry::Mode::RANDOM_IN_RANGE: {
// Note: This logic does not give a uniform distribution - if the minimumlevel is not zero (level 1), then the
case ToolShopRandomSet::TechDiskLevelEntry::Mode::RANDOM_IN_RANGE: {
// Note: This logic does not give a uniform distribution - if the minimum level is not zero (level 1), then the
// minimum level is more likely than all the other levels. This behavior matches the client's logic, though it's
// unclear if this nonuniformity was intentional.
int16_t min_level = max<int16_t>(e.player_level_divisor_or_min_level - 1, 0);
item.data1[2] = clamp<int16_t>(this->rand_int(e.max_level), min_level, 14);
int16_t min_level = std::max<int16_t>(e.player_level_divisor_or_min_level - 1, 0);
item.data1[2] = std::clamp<int16_t>(this->rand_int(e.max_level), min_level, 14);
break;
}
default:
throw logic_error("invalid tech disk level mode");
throw std::logic_error("invalid tech disk level mode");
}
}
vector<ItemData> ItemCreator::generate_weapon_shop_contents(size_t player_level) {
std::vector<ItemData> ItemCreator::generate_weapon_shop_contents(size_t player_level) {
size_t num_items;
if (player_level < 11) {
num_items = 10;
@@ -1375,119 +1376,25 @@ vector<ItemData> ItemCreator::generate_weapon_shop_contents(size_t player_level)
}
}
ProbabilityTable<uint8_t, 100> pt;
auto table = this->weapon_random_set->get_weapon_type_table(table_index);
for (size_t z = 0; z < table.second; z++) {
const auto& e = table.first[z];
for (size_t y = 0; y < e.weight; y++) {
pt.push(e.value);
}
}
ProbabilityTable<uint8_t, 100> pt{this->weapon_random_set->weapon_type_weight_tables.at(table_index).at(section_id)};
pt.shuffle(this->rand_crypt);
vector<ItemData> shop;
std::vector<ItemData> shop;
while (shop.size() < num_items) {
ItemData item;
const std::pair<uint8_t, uint8_t>* def;
uint8_t which = pt.pop();
if (which == 0x39) {
static const vector<pair<uint8_t, uint8_t>> defs{
{0x28, 0x00}, {0x2A, 0x00}, {0x2B, 0x00}, {0x35, 0x00}, {0x52, 0x00}, {0x48, 0x00}, {0x64, 0x00},
{0x59, 0x00}, {0x8A, 0x00}, {0x99, 0x00}};
const auto& def = defs.at(this->section_id);
item.data1[0] = 0;
item.data1[1] = def.first;
item.data1[2] = def.second;
def = &WeaponShopRandomSet::type_defs_39.at(this->section_id);
} else if (which == 0x3A) {
static const vector<pair<uint8_t, uint8_t>> defs{
{0x99, 0x00}, {0x64, 0x00}, {0x8A, 0x00}, {0x28, 0x00}, {0x59, 0x00}, {0x2B, 0x00}, {0x52, 0x00},
{0x2A, 0x00}, {0x48, 0x00}, {0x35, 0x00}};
const auto& def = defs.at(this->section_id);
item.data1[0] = 0;
item.data1[1] = def.first;
item.data1[2] = def.second;
def = &WeaponShopRandomSet::type_defs_3A.at(this->section_id);
} else {
static const vector<pair<uint8_t, uint8_t>> defs({
/* 00 */ {0x01, 0x00},
/* 01 */ {0x01, 0x01},
/* 02 */ {0x01, 0x02},
/* 03 */ {0x01, 0x03},
/* 04 */ {0x01, 0x04},
/* 05 */ {0x03, 0x00},
/* 06 */ {0x03, 0x01},
/* 07 */ {0x03, 0x02},
/* 08 */ {0x03, 0x03},
/* 09 */ {0x03, 0x04},
/* 0A */ {0x02, 0x00},
/* 0B */ {0x02, 0x01},
/* 0C */ {0x02, 0x02},
/* 0D */ {0x02, 0x03},
/* 0E */ {0x02, 0x04},
/* 0F */ {0x05, 0x00},
/* 10 */ {0x05, 0x01},
/* 11 */ {0x05, 0x02},
/* 12 */ {0x05, 0x03},
/* 13 */ {0x05, 0x04},
/* 14 */ {0x04, 0x00},
/* 15 */ {0x04, 0x01},
/* 16 */ {0x04, 0x02},
/* 17 */ {0x04, 0x03},
/* 18 */ {0x04, 0x04},
/* 19 */ {0x06, 0x00},
/* 1A */ {0x06, 0x01},
/* 1B */ {0x06, 0x02},
/* 1C */ {0x06, 0x03},
/* 1D */ {0x06, 0x04},
/* 1E */ {0x07, 0x00},
/* 1F */ {0x07, 0x01},
/* 20 */ {0x07, 0x02},
/* 21 */ {0x07, 0x03},
/* 22 */ {0x07, 0x04},
/* 23 */ {0x08, 0x00},
/* 24 */ {0x08, 0x01},
/* 25 */ {0x08, 0x02},
/* 26 */ {0x08, 0x03},
/* 27 */ {0x08, 0x04},
/* 28 */ {0x09, 0x00},
/* 29 */ {0x09, 0x01},
/* 2A */ {0x09, 0x02},
/* 2B */ {0x09, 0x03},
/* 2C */ {0x09, 0x04},
/* 2D */ {0x0A, 0x00},
/* 2E */ {0x0A, 0x01},
/* 2F */ {0x0A, 0x02},
/* 30 */ {0x0A, 0x03},
/* 31 */ {0x0B, 0x00},
/* 32 */ {0x0B, 0x01},
/* 33 */ {0x0B, 0x02},
/* 34 */ {0x0B, 0x03},
/* 35 */ {0x0C, 0x00},
/* 36 */ {0x0C, 0x01},
/* 37 */ {0x0C, 0x02},
/* 38 */ {0x0C, 0x03},
/* 39 */ {0xFF, 0xFF}, // Special-cased above
/* 3A */ {0xFF, 0xFF}, // Special-cased above
/* 3B */ {0x01, 0x05},
/* 3C */ {0x02, 0x05},
/* 3D */ {0x06, 0x05},
/* 3E */ {0x08, 0x05},
/* 3F */ {0x0A, 0x04},
/* 40 */ {0x0C, 0x04},
/* 41 */ {0x0B, 0x04},
/* 42 */ {0x01, 0x06},
/* 43 */ {0x03, 0x05},
/* 44 */ {0x07, 0x05},
/* 45 */ {0x0A, 0x05},
/* 46 */ {0x0C, 0x05},
/* 47 */ {0x0B, 0x05},
});
const auto& def = defs.at(which);
item.data1[0] = 0;
item.data1[1] = def.first;
item.data1[2] = def.second;
def = &WeaponShopRandomSet::type_defs.at(which);
}
item.data1[0] = 0;
item.data1[1] = def->first;
item.data1[2] = def->second;
this->generate_weapon_shop_item_grind(item, player_level);
this->generate_weapon_shop_item_special(item, player_level);
@@ -1521,18 +1428,17 @@ void ItemCreator::generate_weapon_shop_item_grind(ItemData& item, size_t player_
table_index = 5;
}
uint8_t favored_weapon = favored_weapon_by_section_id.at(this->section_id);
uint8_t favored_weapon = TekkerAdjustmentSet::favored_weapon_type_for_section_id(this->section_id);
bool is_favored = (favored_weapon != 0xFF) && (item.data1[1] == favored_weapon);
const auto* range = is_favored
? this->weapon_random_set->get_favored_grind_range(table_index)
: this->weapon_random_set->get_standard_grind_range(table_index);
const auto& range = is_favored
? this->weapon_random_set->favored_grind_range_table.at(table_index)
: this->weapon_random_set->default_grind_range_table.at(table_index);
const auto& weapon_def = this->item_parameter_table->get_weapon(item.data1[1], item.data1[2]);
item.data1[3] = clamp<uint8_t>(this->rand_int(range->max + 1), range->min, weapon_def.max_grind);
item.data1[3] = std::clamp<uint8_t>(this->rand_int(range.max + 1), range.min, weapon_def.max_grind);
}
void ItemCreator::generate_weapon_shop_item_special(ItemData& item, size_t player_level) {
ProbabilityTable<uint8_t, 100> pt;
size_t table_index;
if (player_level < 11) {
@@ -1553,13 +1459,8 @@ void ItemCreator::generate_weapon_shop_item_special(ItemData& item, size_t playe
table_index = 7;
}
const auto* table = this->weapon_random_set->get_special_mode_table(table_index);
for (size_t z = 0; z < table->size(); z++) {
const auto& e = table->at(z);
for (size_t y = 0; y < e.weight; y++) {
pt.push(e.value);
}
}
ProbabilityTable<uint32_t, 100> pt{this->weapon_random_set->special_mode_table.at(table_index)};
pt.shuffle(this->rand_crypt);
// Note: The original code shuffles pt and then pops a single value from it. For simplicity, we just sample a single
// value instead.
@@ -1574,13 +1475,10 @@ void ItemCreator::generate_weapon_shop_item_special(ItemData& item, size_t playe
item.data1[4] = this->choose_weapon_special(1);
break;
default:
throw runtime_error("invalid special mode");
throw std::runtime_error("invalid special mode");
}
}
static const array<int8_t, 20> bonus_values = {
-50, -45, -40, -35, -30, -25, -20, -15, -10, -5, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50};
void ItemCreator::generate_weapon_shop_item_bonus1(ItemData& item, size_t player_level) {
size_t table_index;
if (player_level < 4) {
@@ -1603,14 +1501,8 @@ void ItemCreator::generate_weapon_shop_item_bonus1(ItemData& item, size_t player
table_index = 8;
}
const auto* type_table = this->weapon_random_set->get_bonus_type_table(0, table_index);
ProbabilityTable<uint8_t, 100> pt;
for (size_t z = 0; z < type_table->size(); z++) {
const auto& e = type_table->at(z);
for (size_t y = 0; y < e.weight; y++) {
pt.push(e.value);
}
}
ProbabilityTable<uint32_t, 100> pt{this->weapon_random_set->bonus_type_table1.at(table_index)};
pt.shuffle(this->rand_crypt);
// Note: The original code shuffles pt and then pops a single value from it. For simplicity, we just sample a single
// value instead.
@@ -1618,8 +1510,8 @@ void ItemCreator::generate_weapon_shop_item_bonus1(ItemData& item, size_t player
if (item.data1[6] == 0) {
item.data1[7] = 0;
} else {
const auto* range = this->weapon_random_set->get_bonus_range(0, table_index);
item.data1[7] = bonus_values.at(max<size_t>(this->rand_int(range->max + 1), range->min));
const auto& range = this->weapon_random_set->bonus_range_table1.at(table_index);
item.data1[7] = WeaponShopRandomSet::bonus_values.at(std::max<size_t>(this->rand_int(range.max + 1), range.min));
}
}
@@ -1645,14 +1537,7 @@ void ItemCreator::generate_weapon_shop_item_bonus2(ItemData& item, size_t player
table_index = 8;
}
const auto* type_table = this->weapon_random_set->get_bonus_type_table(1, table_index);
ProbabilityTable<uint8_t, 100> pt;
for (size_t z = 0; z < type_table->size(); z++) {
const auto& e = type_table->at(z);
for (size_t y = 0; y < e.weight; y++) {
pt.push(e.value);
}
}
ProbabilityTable<uint32_t, 100> pt{this->weapon_random_set->bonus_type_table2.at(table_index)};
pt.shuffle(this->rand_crypt);
do {
@@ -1662,8 +1547,8 @@ void ItemCreator::generate_weapon_shop_item_bonus2(ItemData& item, size_t player
if (item.data1[8] == 0) {
item.data1[9] = 0;
} else {
const auto* range = this->weapon_random_set->get_bonus_range(1, table_index);
item.data1[9] = bonus_values.at(max<size_t>(this->rand_int(range->max + 1), range->min));
const auto& range = this->weapon_random_set->bonus_range_table2.at(table_index);
item.data1[9] = WeaponShopRandomSet::bonus_values.at(std::max<size_t>(this->rand_int(range.max + 1), range.min));
}
}
@@ -1715,7 +1600,7 @@ ItemData ItemCreator::base_item_for_specialized_box(uint32_t param4, uint32_t pa
item.data2d = ((param5 >> 0x10) & 0xFFFF) * 10;
break;
default:
throw runtime_error("invalid item class");
throw std::runtime_error("invalid item class");
}
return item;
@@ -1723,59 +1608,64 @@ ItemData ItemCreator::base_item_for_specialized_box(uint32_t param4, uint32_t pa
ssize_t ItemCreator::apply_tekker_deltas(ItemData& item, uint8_t section_id) {
if (item.data1[0] != 0) {
throw runtime_error("tekker deltas can only be applied to weapons");
throw std::runtime_error("tekker deltas can only be applied to weapons");
}
static const array<int8_t, 11> delta_table = {-10, -5, -3, -2, -1, 0, 1, 2, 3, 5, 10};
bool favored = item.data1[1] == favored_weapon_by_section_id[section_id];
bool favored = (item.data1[1] == TekkerAdjustmentSet::favored_weapon_type_for_section_id(section_id));
ssize_t luck = 0;
this->log.info_f("Applying tekker deltas for {} weapon", favored ? "favored" : "non-favored");
auto sample_prob_table = [this](const TekkerAdjustmentSet::Table& table) -> int8_t {
size_t sample = this->rand_crypt->next() % table.total;
for (const auto& [k, v] : table.probs) {
if (sample < v) {
return k;
}
sample -= v;
}
throw std::logic_error("Table total is incorrect");
};
// Adjust the weapon's special
{
const auto& prob_table = this->tekker_adjustment_set->get_special_upgrade_prob_table(section_id, favored);
uint8_t delta_index = prob_table.sample(this->rand_crypt);
int8_t delta = delta_table.at(delta_index);
this->log.info_f("(Special) Delta index {}, delta {}", delta_index, delta);
// Note: The original code checks specifically for -1 and +1 here, but the data files only include delta_indexes 4,
// 5, and 6 (which correspond to -1, 0, and 1) anyway, so we just check for positive and negative numbers instead.
// When using the original JudgeItem.rel file, the behavior should be the same, but this feels more correct.
try {
uint8_t new_special;
if (delta < 0) {
new_special = item.data1[4] - 1;
} else if (delta > 0) {
new_special = item.data1[4] + 1;
} else {
new_special = item.data1[4];
}
if (new_special != item.data1[4]) {
int8_t delta = sample_prob_table(favored
? this->tekker_adjustment_set->favored_special_delta_table[section_id]
: this->tekker_adjustment_set->default_special_delta_table[section_id]);
this->log.info_f("(Special) Delta {} chosen", delta);
for (; delta != 0; delta += (delta < 0) - (0 < delta)) {
try {
// Note: The original code checks specifically for -1 and +1 here and only increments or decrements the special
// by 1, and the data files only include delta_indexes 4, 5, and 6 (which correspond to -1, 0, and 1). But we
// want to support other levels of delta indexes, so we simply add delta instead. When using the original
// JudgeItem.rel file, the behavior should be the same, but this logic feels more correct.
uint8_t new_special = item.data1[4] + delta;
if (this->item_parameter_table->get_special(item.data1[4]).type ==
this->item_parameter_table->get_special(new_special).type) {
item.data1[4] = new_special;
this->log.info_f("(Special) Delta {} applied", delta);
break;
} else {
this->log.info_f("(Special) Delta canceled because it would change special category");
this->log.info_f("(Special) Delta {} canceled because it would change special category", delta);
}
} catch (const std::out_of_range&) {
// Invalid special number passed to get_special; treat it as if delta == 0
}
} catch (const out_of_range&) {
// Invalid special number passed to get_special; just ignore it
}
luck += this->tekker_adjustment_set->get_luck_for_special_upgrade(delta_index);
luck += this->tekker_adjustment_set->special_luck_table.at(delta);
this->log.info_f("(Special) Luck is now {}", luck);
}
// Adjust the weapon's grind if it's not rare
if (!this->item_parameter_table->is_item_rare(item)) {
const auto& weapon_def = this->item_parameter_table->get_weapon(item.data1[1], item.data1[2]);
const auto& prob_table = this->tekker_adjustment_set->get_grind_delta_prob_table(section_id, favored);
uint8_t delta_index = prob_table.sample(this->rand_crypt);
int8_t delta = delta_table.at(delta_index);
this->log.info_f("(Grind) Delta index {}, delta {}", delta_index, delta);
int8_t delta = sample_prob_table(favored
? this->tekker_adjustment_set->favored_grind_delta_table[section_id]
: this->tekker_adjustment_set->default_grind_delta_table[section_id]);
this->log.info_f("(Grind) Delta {} chosen", delta);
int16_t new_grind = static_cast<int16_t>(item.data1[3]) + static_cast<int16_t>(delta);
item.data1[3] = clamp<int16_t>(new_grind, 0, weapon_def.max_grind);
luck += this->tekker_adjustment_set->get_luck_for_grind_delta(delta_index);
item.data1[3] = std::clamp<int16_t>(new_grind, 0, weapon_def.max_grind);
luck += this->tekker_adjustment_set->grind_luck_table.at(delta);
this->log.info_f("(Grind) Luck is now {}", luck);
} else {
this->log.info_f("(Grind) Item is rare; skipping grind adjustment");
@@ -1783,20 +1673,19 @@ ssize_t ItemCreator::apply_tekker_deltas(ItemData& item, uint8_t section_id) {
// Adjust the weapon's bonuses
{
const auto& prob_table = this->tekker_adjustment_set->get_bonus_delta_prob_table(section_id, favored);
// Note: The original code really does use the same delta for all three bonuses.
uint8_t delta_index = prob_table.sample(this->rand_crypt);
int8_t delta = delta_table.at(delta_index);
this->log.info_f("(Bonuses) Delta index {}, delta {}", delta_index, delta);
int8_t delta = sample_prob_table(favored
? this->tekker_adjustment_set->favored_bonus_delta_table[section_id]
: this->tekker_adjustment_set->default_bonus_delta_table[section_id]);
this->log.info_f("(Bonuses) Delta {} chosen", delta);
// Note: The original code doesn't check if there's actually a bonus in each slot before incrementing the values.
// Presumably there's a check later that will clear any invalid bonuses, but we don't have such a check, so we need
// to check here if each bonus is actually present.
for (size_t z = 6; z <= 10; z += 2) {
if (item.data1[z] >= 1 && item.data1[z] <= 5) {
item.data1[z + 1] = min<int8_t>(item.data1[z + 1] + delta, 100);
item.data1[z + 1] = std::min<int8_t>(item.data1[z + 1] + delta, 100);
}
}
luck += this->tekker_adjustment_set->get_luck_for_bonus_delta(delta_index);
luck += this->tekker_adjustment_set->bonus_luck_table.at(delta);
this->log.info_f("(Bonuses) Luck is now {}", luck);
}
+8 -6
View File
@@ -7,7 +7,9 @@
#include "PSOEncryption.hh"
#include "PlayerSubordinates.hh"
#include "RareItemSet.hh"
#include "ShopRandomSets.hh"
#include "StaticGameData.hh"
#include "TekkerAdjustmentSet.hh"
// This file and ItemCreator.cc are essentially a direct reverse-engineering of the item creation algorithm in PSO GC.
// Only minor changes have been made to support BB (as described in the comments in the implementation) and to support
@@ -19,9 +21,9 @@ public:
ItemCreator(
std::shared_ptr<const CommonItemSet> common_item_set,
std::shared_ptr<const RareItemSet> rare_item_set,
std::shared_ptr<const ArmorRandomSet> armor_random_set,
std::shared_ptr<const ToolRandomSet> tool_random_set,
std::shared_ptr<const WeaponRandomSet> weapon_random_set,
std::shared_ptr<const ArmorShopRandomSet> armor_random_set,
std::shared_ptr<const ToolShopRandomSet> tool_random_set,
std::shared_ptr<const WeaponShopRandomSet> weapon_random_set,
std::shared_ptr<const TekkerAdjustmentSet> tekker_adjustment_set,
std::shared_ptr<const ItemParameterTable> item_parameter_table,
std::shared_ptr<const ItemData::StackLimits> stack_limits,
@@ -84,9 +86,9 @@ private:
Difficulty difficulty;
uint8_t section_id;
std::shared_ptr<const RareItemSet> rare_item_set;
std::shared_ptr<const ArmorRandomSet> armor_random_set;
std::shared_ptr<const ToolRandomSet> tool_random_set;
std::shared_ptr<const WeaponRandomSet> weapon_random_set;
std::shared_ptr<const ArmorShopRandomSet> armor_random_set;
std::shared_ptr<const ToolShopRandomSet> tool_random_set;
std::shared_ptr<const WeaponShopRandomSet> weapon_random_set;
std::shared_ptr<const TekkerAdjustmentSet> tekker_adjustment_set;
std::shared_ptr<const ItemParameterTable> item_parameter_table;
std::shared_ptr<const CommonItemSet> common_item_set;
+32 -34
View File
@@ -6,12 +6,10 @@
#include "ItemParameterTable.hh"
#include "StaticGameData.hh"
using namespace std;
const vector<uint8_t> ItemData::StackLimits::DEFAULT_TOOL_LIMITS_DC_NTE({10});
const vector<uint8_t> ItemData::StackLimits::DEFAULT_TOOL_LIMITS_V1_V2({10, 10, 1, 10, 10, 10, 10, 10, 10, 1});
const vector<uint8_t> ItemData::StackLimits::DEFAULT_TOOL_LIMITS_V3_V4(
{10, 10, 1, 10, 10, 10, 10, 10, 10, 1, 1, 1, 1, 1, 1, 1, 99, 1});
const std::vector<uint8_t> ItemData::StackLimits::DEFAULT_TOOL_LIMITS_DC_NTE{10};
const std::vector<uint8_t> ItemData::StackLimits::DEFAULT_TOOL_LIMITS_V1_V2{10, 10, 1, 10, 10, 10, 10, 10, 10, 1};
const std::vector<uint8_t> ItemData::StackLimits::DEFAULT_TOOL_LIMITS_V3_V4{
10, 10, 1, 10, 10, 10, 10, 10, 10, 1, 1, 1, 1, 1, 1, 1, 99, 1};
const ItemData::StackLimits ItemData::StackLimits::DEFAULT_STACK_LIMITS_DC_NTE(
Version::DC_NTE, ItemData::StackLimits::DEFAULT_TOOL_LIMITS_DC_NTE, 999999);
@@ -21,7 +19,7 @@ const ItemData::StackLimits ItemData::StackLimits::DEFAULT_STACK_LIMITS_V3_V4(
Version::GC_V3, ItemData::StackLimits::DEFAULT_TOOL_LIMITS_V3_V4, 999999);
ItemData::StackLimits::StackLimits(
Version version, const vector<uint8_t>& max_tool_stack_sizes_by_data1_1, uint32_t max_meseta_stack_size)
Version version, const std::vector<uint8_t>& max_tool_stack_sizes_by_data1_1, uint32_t max_meseta_stack_size)
: version(version),
max_tool_stack_sizes_by_data1_1(max_tool_stack_sizes_by_data1_1),
max_meseta_stack_size(max_meseta_stack_size) {}
@@ -40,7 +38,7 @@ uint8_t ItemData::StackLimits::get(uint8_t data1_0, uint8_t data1_1) const {
}
if (data1_0 == 3) {
const auto& vec = this->max_tool_stack_sizes_by_data1_1;
return vec.at(min<size_t>(data1_1, vec.size() - 1));
return vec.at(std::min<size_t>(data1_1, vec.size() - 1));
}
return 1;
}
@@ -181,7 +179,7 @@ bool ItemData::is_wrapped(const StackLimits& limits) const {
case 4:
return false;
default:
throw runtime_error("invalid item data");
throw std::runtime_error("invalid item data");
}
}
@@ -206,7 +204,7 @@ void ItemData::wrap(const StackLimits& limits, uint8_t present_color) {
case 4:
break;
default:
throw runtime_error("invalid item data");
throw std::runtime_error("invalid item data");
}
}
@@ -227,7 +225,7 @@ void ItemData::unwrap(const StackLimits& limits) {
case 4:
break;
default:
throw runtime_error("invalid item data");
throw std::runtime_error("invalid item data");
}
}
@@ -309,7 +307,7 @@ uint16_t ItemData::compute_mag_strength_flags() const {
ret |= 0x020;
}
uint16_t highest = max<uint16_t>(dex, max<uint16_t>(pow, mind));
uint16_t highest = std::max<uint16_t>(dex, std::max<uint16_t>(pow, mind));
if ((pow == highest) + (dex == highest) + (mind == highest) > 1) {
ret |= 0x100;
}
@@ -343,10 +341,10 @@ uint8_t ItemData::mag_photon_blast_for_slot(uint8_t slot) const {
left_pb_num--;
}
}
throw logic_error("failed to find unused photon blast number");
throw std::logic_error("failed to find unused photon blast number");
} else {
throw logic_error("invalid slot index");
throw std::logic_error("invalid slot index");
}
}
@@ -387,7 +385,7 @@ void ItemData::add_mag_photon_blast(uint8_t pb_num) {
pb_num--;
}
if (pb_num >= 4) {
throw runtime_error("left photon blast number is too high");
throw std::runtime_error("left photon blast number is too high");
}
pb_nums |= (pb_num << 6);
flags |= 4;
@@ -471,11 +469,11 @@ void ItemData::decode_for_version(Version from_version) {
break;
default:
throw runtime_error("invalid item class");
throw std::runtime_error("invalid item class");
}
}
void ItemData::encode_for_version(Version to_version, shared_ptr<const ItemParameterTable> item_parameter_table) {
void ItemData::encode_for_version(Version to_version, std::shared_ptr<const ItemParameterTable> item_parameter_table) {
bool should_encode_v2_data = item_parameter_table &&
(is_v1(to_version) || is_v2(to_version)) &&
(to_version != Version::GC_NTE) &&
@@ -486,7 +484,7 @@ void ItemData::encode_for_version(Version to_version, shared_ptr<const ItemParam
if (should_encode_v2_data && (this->data1[1] > 0x26)) {
if (this->data1[1] < 0x89) {
this->data1[5] = this->data1[1];
this->data1[1] = item_parameter_table->get_weapon_class(this->data1[1]);
this->data1[1] = item_parameter_table->get_weapon_kind(this->data1[1]);
if (this->data1[1] == 0x00) {
this->data1[1] = 0x0F;
}
@@ -501,7 +499,7 @@ void ItemData::encode_for_version(Version to_version, shared_ptr<const ItemParam
break;
case 0x01: {
static const array<uint8_t, 4> armor_limits = {0x00, 0x29, 0x27, 0x44};
static const std::array<uint8_t, 4> armor_limits = {0x00, 0x29, 0x27, 0x44};
if (should_encode_v2_data && (this->data1[2] >= armor_limits[this->data1[1]])) {
this->data1[3] = this->data1[2];
this->data1[2] = 0x00;
@@ -554,7 +552,7 @@ void ItemData::encode_for_version(Version to_version, shared_ptr<const ItemParam
break;
default:
throw runtime_error("invalid item class");
throw std::runtime_error("invalid item class");
}
}
@@ -658,7 +656,7 @@ bool ItemData::has_bonuses() const {
case 3:
return (this->get_unit_bonus() > 0);
default:
throw runtime_error("invalid item");
throw std::runtime_error("invalid item");
}
case 2:
if (this->data1[1] < 0x23) {
@@ -670,7 +668,7 @@ bool ItemData::has_bonuses() const {
case 4:
return false;
default:
throw runtime_error("invalid item");
throw std::runtime_error("invalid item");
}
}
@@ -703,7 +701,7 @@ EquipSlot ItemData::default_equip_slot() const {
case 0x02:
return EquipSlot::MAG;
}
throw runtime_error("item cannot be equipped");
throw std::runtime_error("item cannot be equipped");
}
bool ItemData::can_be_equipped_in_slot(EquipSlot slot) const {
@@ -722,7 +720,7 @@ bool ItemData::can_be_equipped_in_slot(EquipSlot slot) const {
case EquipSlot::WEAPON:
return (this->data1[0] == 0x00);
default:
throw runtime_error("invalid equip slot");
throw std::runtime_error("invalid equip slot");
}
}
@@ -748,23 +746,23 @@ bool ItemData::compare_for_sort(const ItemData& a, const ItemData& b) {
return false;
}
ItemData ItemData::from_data(const string& data) {
ItemData ItemData::from_data(const std::string& data) {
if (data.size() < 2) {
throw runtime_error("data is too short");
throw std::runtime_error("data is too short");
}
if (data.size() > 0x10) {
throw runtime_error("data is too long");
throw std::runtime_error("data is too long");
}
ItemData ret;
for (size_t z = 0; z < min<size_t>(data.size(), 12); z++) {
for (size_t z = 0; z < std::min<size_t>(data.size(), 12); z++) {
ret.data1[z] = data[z];
}
for (size_t z = 12; z < min<size_t>(data.size(), 16); z++) {
for (size_t z = 12; z < std::min<size_t>(data.size(), 16); z++) {
ret.data2[z - 12] = data[z];
}
if (ret.data1[0] > 4) {
throw runtime_error("invalid item class");
throw std::runtime_error("invalid item class");
}
return ret;
}
@@ -772,7 +770,7 @@ ItemData ItemData::from_data(const string& data) {
ItemData ItemData::from_primary_identifier(const StackLimits& limits, uint32_t primary_identifier) {
ItemData ret;
if (primary_identifier > 0x04000000) {
throw runtime_error("invalid item class");
throw std::runtime_error("invalid item class");
}
ret.data1[0] = (primary_identifier >> 24) & 0xFF;
ret.data1[1] = (primary_identifier >> 16) & 0xFF;
@@ -786,16 +784,16 @@ ItemData ItemData::from_primary_identifier(const StackLimits& limits, uint32_t p
return ret;
}
string ItemData::hex() const {
std::string ItemData::hex() const {
return std::format("{:08X} {:08X} {:08X} ({:08X}) {:08X}",
this->data1db[0], this->data1db[1], this->data1db[2], this->id, this->data2db);
}
string ItemData::short_hex() const {
std::string ItemData::short_hex() const {
auto ret = std::format("{:08X}{:08X}{:08X}{:08X}",
this->data1db[0], this->data1db[1], this->data1db[2], this->data2db);
size_t offset = ret.find_last_not_of('0');
if (offset != string::npos) {
if (offset != std::string::npos) {
offset += (offset & 1) ? 1 : 2;
offset = std::max<size_t>(offset, 6);
if (offset < ret.size()) {
+251 -70
View File
@@ -1,8 +1,8 @@
#include "ItemNameIndex.hh"
#include "StaticGameData.hh"
#include <algorithm>
using namespace std;
#include "StaticGameData.hh"
ItemNameIndex::ItemNameIndex(
std::shared_ptr<const ItemParameterTable> item_parameter_table,
@@ -17,17 +17,17 @@ ItemNameIndex::ItemNameIndex(
continue;
}
const string* name = nullptr;
const std::string* name = nullptr;
bool is_es_weapon = false;
try {
ItemData item = ItemData::from_primary_identifier(*this->limits, primary_identifier);
is_es_weapon = item.is_s_rank_weapon();
name = &name_coll.at(item_parameter_table->get_item_id(item));
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
}
if (name) {
auto meta = make_shared<ItemMetadata>();
auto meta = std::make_shared<ItemMetadata>();
meta->primary_identifier = primary_identifier;
meta->name = *name;
this->primary_identifier_index.emplace(meta->primary_identifier, meta);
@@ -42,7 +42,7 @@ ItemNameIndex::ItemNameIndex(
static std::string s_rank_name_characters("\0ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_", 0x20);
// clang-format off
static const array<const char*, 0x29> name_for_weapon_special = {
static const std::array<const char*, 0x29> name_for_weapon_special = {
nullptr,
"Draw", // Type: 0001, amount: 0005
"Drain", // Type: 0001, amount: 0009
@@ -87,7 +87,7 @@ static const array<const char*, 0x29> name_for_weapon_special = {
};
// clang-format on
const array<const char*, 0x11> name_for_s_rank_special = {
const std::array<const char*, 0x11> name_for_s_rank_special = {
nullptr,
"Jellen",
"Zalure",
@@ -115,7 +115,7 @@ std::string ItemNameIndex::describe_item(const ItemData& item, uint8_t flags) co
return std::format("{}{} Meseta", include_color_escapes ? "$C7" : "", item.data2d);
}
vector<string> ret_tokens;
std::vector<std::string> ret_tokens;
// For weapons, specials appear before the weapon name
bool is_unidentified = false;
@@ -133,7 +133,7 @@ std::string ItemNameIndex::describe_item(const ItemData& item, uint8_t flags) co
if (special_id) {
try {
ret_tokens.emplace_back(name_for_weapon_special.at(special_id));
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
ret_tokens.emplace_back(std::format("!SP:{:02X}", special_id));
}
}
@@ -141,7 +141,7 @@ std::string ItemNameIndex::describe_item(const ItemData& item, uint8_t flags) co
if (!name_only && (item.data1[0] == 0x00) && (item.data1[2] != 0x00) && item.is_s_rank_weapon()) {
try {
ret_tokens.emplace_back(name_for_s_rank_special.at(item.data1[2]));
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
ret_tokens.emplace_back(std::format("!SSP:{:02X}", item.data1[2]));
}
}
@@ -158,11 +158,11 @@ std::string ItemNameIndex::describe_item(const ItemData& item, uint8_t flags) co
// Add the item name
uint32_t primary_identifier = item.primary_identifier();
if ((primary_identifier & 0xFFFF0000) == 0x03020000) {
string technique_name;
std::string technique_name;
try {
technique_name = tech_id_to_name.at(item.data1[4]);
technique_name[0] = toupper(technique_name[0]);
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
technique_name = std::format("!TD:{:02X}", item.data1[4]);
}
// Hide the level for Reverser and Ryuker, unless the level isn't 1
@@ -175,7 +175,7 @@ std::string ItemNameIndex::describe_item(const ItemData& item, uint8_t flags) co
try {
auto meta = this->primary_identifier_index.at(primary_identifier);
ret_tokens.emplace_back(meta->name);
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
ret_tokens.emplace_back(std::format("!ID:{:08X}", primary_identifier));
}
}
@@ -206,7 +206,7 @@ std::string ItemNameIndex::describe_item(const ItemData& item, uint8_t flags) co
static_cast<uint8_t>(be_data1w5 & 0x1F),
};
string name;
std::string name;
for (size_t x = 0; x < 8; x++) {
char ch = s_rank_name_characters.at(char_indexes[x]);
if (ch == 0) {
@@ -304,7 +304,7 @@ std::string ItemNameIndex::describe_item(const ItemData& item, uint8_t flags) co
uint16_t pow = item.data1w[3];
uint16_t dex = item.data1w[4];
uint16_t mind = item.data1w[5];
auto format_stat = +[](uint16_t stat) -> string {
auto format_stat = +[](uint16_t stat) -> std::string {
uint16_t level = stat / 100;
uint8_t partial = stat % 100;
if (partial == 0) {
@@ -321,7 +321,7 @@ std::string ItemNameIndex::describe_item(const ItemData& item, uint8_t flags) co
uint8_t flags = item.data2[2];
if (flags & 7) {
static const vector<const char*> pb_shortnames = {"F", "E", "G", "P", "L", "M&Y", "MG", "GR"};
static const std::vector<const char*> pb_shortnames = {"F", "E", "G", "P", "L", "M&Y", "MG", "GR"};
const char* pb_names[3] = {nullptr, nullptr, nullptr};
uint8_t left_pb = item.mag_photon_blast_for_slot(2);
@@ -337,7 +337,7 @@ std::string ItemNameIndex::describe_item(const ItemData& item, uint8_t flags) co
pb_names[2] = pb_shortnames[right_pb];
}
string token = "PB:";
std::string token = "PB:";
for (size_t x = 0; x < 3; x++) {
if (pb_names[x] == nullptr) {
continue;
@@ -352,7 +352,7 @@ std::string ItemNameIndex::describe_item(const ItemData& item, uint8_t flags) co
try {
ret_tokens.emplace_back(std::format("({})", name_for_mag_color.at(item.data2[3])));
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
ret_tokens.emplace_back(std::format("(!CL:{:02X})", item.data2[3]));
}
@@ -363,7 +363,7 @@ std::string ItemNameIndex::describe_item(const ItemData& item, uint8_t flags) co
}
}
string ret = phosg::join(ret_tokens, " ");
std::string ret = phosg::join(ret_tokens, " ");
if (include_color_escapes) {
if (is_unidentified) {
return "$C3" + ret;
@@ -385,18 +385,18 @@ ItemData ItemNameIndex::parse_item_description(const std::string& desc) const {
ItemData ret;
try {
ret = this->parse_item_description_phase(desc, false);
} catch (const exception& e1) {
} catch (const std::exception& e1) {
try {
ret = this->parse_item_description_phase(desc, true);
} catch (const exception& e2) {
} catch (const std::exception& e2) {
try {
ret = ItemData::from_data(phosg::parse_data_string(desc));
} catch (const exception& ed) {
} catch (const std::exception& ed) {
if (strcmp(e1.what(), e2.what())) {
throw runtime_error(std::format("cannot parse item description \"{}\" (as text 1: {}) (as text 2: {}) (as data: {})",
throw std::runtime_error(std::format("cannot parse item description \"{}\" (as text 1: {}) (as text 2: {}) (as data: {})",
desc, e1.what(), e2.what(), ed.what()));
} else {
throw runtime_error(std::format("cannot parse item description \"{}\" (as text: {}) (as data: {})",
throw std::runtime_error(std::format("cannot parse item description \"{}\" (as text: {}) (as data: {})",
desc, e1.what(), ed.what()));
}
}
@@ -412,17 +412,17 @@ ItemData ItemNameIndex::parse_item_description_phase(const std::string& descript
ret.id = 0xFFFFFFFF;
ret.data2d = 0;
string desc = phosg::tolower(description);
std::string desc = phosg::tolower(description);
if (desc.ends_with(" meseta")) {
ret.data1[0] = 0x04;
ret.data2d = stol(desc, nullptr, 10);
ret.data2d = std::stol(desc, nullptr, 10);
return ret;
}
if (desc.starts_with("es ")) {
auto parse_name = [&](const std::string& token) -> void {
if (token.size() > 8) {
throw runtime_error("s-rank name too long");
throw std::runtime_error("s-rank name too long");
}
uint8_t char_indexes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
@@ -430,7 +430,7 @@ ItemData ItemNameIndex::parse_item_description_phase(const std::string& descript
char ch = toupper(token[z]);
size_t pos = s_rank_name_characters.find(ch);
if (pos == std::string::npos) {
throw runtime_error(std::format("s-rank name contains invalid character {:02X} ({})", ch, ch));
throw std::runtime_error(std::format("s-rank name contains invalid character {:02X} ({})", ch, ch));
}
char_indexes[z] = pos;
}
@@ -536,10 +536,10 @@ ItemData ItemNameIndex::parse_item_description_phase(const std::string& descript
ret.data1[4] = tech;
} else {
if (tokens.size() != 2) {
throw runtime_error("invalid tech disk format");
throw std::runtime_error("invalid tech disk format");
}
if (!tokens[1].starts_with("lv.")) {
throw runtime_error("invalid tech disk level");
throw std::runtime_error("invalid tech disk level");
}
uint8_t tech = technique_for_name(tokens[0]);
uint8_t level = stoul(tokens[1].substr(3), nullptr, 10) - 1;
@@ -572,7 +572,7 @@ ItemData ItemNameIndex::parse_item_description_phase(const std::string& descript
if (!name_for_weapon_special[z]) {
continue;
}
string prefix = phosg::tolower(name_for_weapon_special[z]);
std::string prefix = phosg::tolower(name_for_weapon_special[z]);
prefix += ' ';
if (desc.starts_with(prefix)) {
weapon_special = z;
@@ -590,14 +590,14 @@ ItemData ItemNameIndex::parse_item_description_phase(const std::string& descript
if (name_it != this->name_index.end() && desc.starts_with(name_it->first)) {
break;
} else if (name_it == this->name_index.begin()) {
throw runtime_error("no such item");
throw std::runtime_error("no such item");
} else {
name_it--;
lookback++;
}
}
if (lookback >= 4) {
throw runtime_error("item not found: " + desc);
throw std::runtime_error("item not found: " + desc);
}
desc = desc.substr(name_it->first.size());
@@ -635,7 +635,7 @@ ItemData ItemNameIndex::parse_item_description_phase(const std::string& descript
} else {
auto p_tokens = phosg::split(token, '/');
if (p_tokens.size() > 5) {
throw runtime_error("invalid bonuses token");
throw std::runtime_error("invalid bonuses token");
}
uint8_t max_bonuses = this->item_parameter_table->is_unsealable_item(ret) ? 2 : 3;
uint8_t bonus_index = 0;
@@ -645,7 +645,7 @@ ItemData ItemNameIndex::parse_item_description_phase(const std::string& descript
continue;
}
if (bonus_index >= max_bonuses) {
throw runtime_error("weapon has too many bonuses");
throw std::runtime_error("weapon has too many bonuses");
}
ret.data1[6 + (2 * bonus_index)] = z + 1;
ret.data1[7 + (2 * bonus_index)] = static_cast<uint8_t>(bonus_value);
@@ -660,7 +660,7 @@ ItemData ItemNameIndex::parse_item_description_phase(const std::string& descript
} else if (ret.data1[0] == 0x01) {
if (ret.data1[1] == 0x03) { // Unit
static const unordered_map<string, uint16_t> modifiers({
static const std::unordered_map<std::string, uint16_t> modifiers({
{"--", 0xFFFC},
{"-", 0xFFFE},
{"", 0x0000},
@@ -696,7 +696,7 @@ ItemData ItemNameIndex::parse_item_description_phase(const std::string& descript
if (token.empty()) {
continue;
} else if (!token.starts_with("+")) {
throw runtime_error("invalid armor/shield modifier");
throw std::runtime_error("invalid armor/shield modifier");
}
if (token.ends_with("def")) {
ret.data1w[3] = static_cast<uint16_t>(stol(token.substr(1, token.size() - 4), nullptr, 10));
@@ -719,9 +719,9 @@ ItemData ItemNameIndex::parse_item_description_phase(const std::string& descript
} else if (token.starts_with("pb:")) { // Photon blasts
auto pb_tokens = phosg::split(token.substr(3), ',');
if (pb_tokens.size() > 3) {
throw runtime_error("too many photon blasts specified");
throw std::runtime_error("too many photon blasts specified");
}
static const unordered_map<string, uint8_t> name_to_pb_num(
static const std::unordered_map<std::string, uint8_t> name_to_pb_num(
{{"f", 0}, {"e", 1}, {"g", 2}, {"p", 3}, {"l", 4}, {"m", 5}, {"my", 5}, {"m&y", 5}});
for (const auto& pb_token : pb_tokens) {
ret.add_mag_photon_blast(name_to_pb_num.at(pb_token));
@@ -733,12 +733,12 @@ ItemData ItemNameIndex::parse_item_description_phase(const std::string& descript
} else if (!token.empty() && isdigit(token[0])) { // Stats
auto s_tokens = phosg::split(token, '/');
if (s_tokens.size() != 4) {
throw runtime_error("incorrect stat count");
throw std::runtime_error("incorrect stat count");
}
for (size_t z = 0; z < 4; z++) {
auto n_tokens = phosg::split(s_tokens[z], '.');
if (n_tokens.size() == 0 || n_tokens.size() > 2) {
throw logic_error("incorrect stats argument format");
throw std::logic_error("incorrect stats argument format");
} else if ((n_tokens.size() == 1) || (n_tokens[1].size() == 0)) {
ret.data1w[z + 2] = stoul(n_tokens[0], nullptr, 10) * 100;
} else if (n_tokens[1].size() == 1) {
@@ -746,7 +746,7 @@ ItemData ItemNameIndex::parse_item_description_phase(const std::string& descript
} else if (n_tokens[1].size() == 2) {
ret.data1w[z + 2] = stoul(n_tokens[0], nullptr, 10) * 100 + stoul(n_tokens[1], nullptr, 10);
} else {
throw runtime_error("incorrect stat format");
throw std::runtime_error("incorrect stat format");
}
}
ret.data1[2] = ret.compute_mag_level();
@@ -766,18 +766,18 @@ ItemData ItemNameIndex::parse_item_description_phase(const std::string& descript
ret.data1[5] = 1;
}
} else if (!desc.empty()) {
throw runtime_error("item cannot be stacked");
throw std::runtime_error("item cannot be stacked");
}
if (is_wrapped) {
if (ret.is_stackable(*this->limits)) {
throw runtime_error("stackable items cannot be wrapped");
throw std::runtime_error("stackable items cannot be wrapped");
} else {
ret.data1[3] |= 0x40;
}
}
} else {
throw logic_error("invalid item class");
throw std::logic_error("invalid item class");
}
return ret;
@@ -787,11 +787,11 @@ void ItemNameIndex::print_table(FILE* stream) const {
auto pmt = this->item_parameter_table;
phosg::fwrite_fmt(stream, "WEAPONS\n");
phosg::fwrite_fmt(stream, " CODE => ---ID--- TYPE SKIN POINTS FLAG ATPLO ATPHI ATPRQ MSTRQ ATARQ -MST- GND PH SP ATA SB(S1:AMT1,S2:AMT2) PJ 1X 1Y 2X 2Y CR --A1-- A4 A5 TB BF CL ST* USL ---DIVISOR--- NAME\n");
phosg::fwrite_fmt(stream, " CODE => ---ID--- TYPE SKIN POINTS FLAG ATPLO ATPHI ATPRQ MSTRQ ATARQ -MST- GND PH SP ATA SB(S1:AMT1,S2:AMT2) PJ 1X 1Y 2X 2Y CR --A1-- A4 A5 TB(TN:FL:AMOUNT, ... ) BF CL ST* USL ---DIVISOR--- NAME\n");
for (size_t data1_1 = 0; data1_1 < pmt->num_weapon_classes(); data1_1++) {
uint8_t weapon_class = pmt->get_weapon_class(data1_1);
uint8_t weapon_class = pmt->get_weapon_kind(data1_1);
float sale_divisor = pmt->get_sale_divisor(0x00, data1_1);
string divisor_str = std::format("{:g}", sale_divisor);
std::string divisor_str = std::format("{:g}", sale_divisor);
divisor_str.resize(13, ' ');
size_t data1_2_limit = pmt->num_weapons_in_class(data1_1);
@@ -804,17 +804,33 @@ void ItemNameIndex::print_table(FILE* stream) const {
item.data1[0] = 0x00;
item.data1[1] = data1_1;
item.data1[2] = data1_2;
string name = this->describe_item(item);
std::string name = this->describe_item(item);
auto& stat_boost = pmt->get_stat_boost(w.stat_boost_entry_index);
phosg::fwrite_fmt(stream, " 00{:02X}{:02X} => {:08X} {:04X} {:04X} {:6} {:04X} {:5} {:5} {:5} {:5} {:5} {:5} {:3} {:02X} {:02X} {:3} {:02X}({:02X}:{:04X},{:02X}:{:04X}) {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X}{:02X}{:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:2}* {} {} {}\n",
const auto& stat_boost = pmt->get_stat_boost(w.stat_boost_entry_index);
std::string tech_boost_str;
if (w.tech_boost_entry_index < pmt->num_tech_boosts()) {
const auto& tech_boost = pmt->get_tech_boost(w.tech_boost_entry_index);
tech_boost_str = std::format("({:02X}:{:02X}:{:g},{:02X}:{:02X}:{:g},{:02X}:{:02X}:{:g})",
tech_boost.tech_num1, tech_boost.flags1, tech_boost.amount1, tech_boost.tech_num2, tech_boost.flags2,
tech_boost.amount2, tech_boost.tech_num3, tech_boost.flags3, tech_boost.amount3);
}
tech_boost_str.resize(40, ' ');
phosg::fwrite_fmt(stream,
// CODE => ID TYPE SKIN PTS FLAG ATP- ATP+ ATPR MSTR ATAR MST GND PH SP ATA
" 00{:02X}{:02X} => {:08X} {:04X} {:04X} {:6} {:04X} {:5} {:5} {:5} {:5} {:5} {:5} {:3} {:02X} {:02X} {:3} "
// SB( S1:AMT1 , S2:AMT2 ) PJ 1X 1Y 2X 2Y CR --------A1-------- A4
"{:02X}({:02X}:{:04X},{:02X}:{:04X}) {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X}{:02X}{:02X} {:02X} "
// A5 TB( TN: FL: AMT, TN: FL: AMT, TN: FL: AMT) BF CL ST* US DV NAME\n"
"{:02X} {:02X}{} {:02X} {:02X} {:2}* {} {} {}\n",
data1_1,
data1_2,
w.id,
w.type,
w.skin,
w.team_points,
w.class_flags,
w.usability_flags,
w.atp_min,
w.atp_max,
w.atp_required,
@@ -841,7 +857,8 @@ void ItemNameIndex::print_table(FILE* stream) const {
w.unknown_a1[2],
w.unknown_a4,
w.unknown_a5,
w.tech_boost,
w.tech_boost_entry_index,
tech_boost_str,
w.behavior_flags,
weapon_class,
stars,
@@ -852,10 +869,10 @@ void ItemNameIndex::print_table(FILE* stream) const {
}
phosg::fwrite_fmt(stream, "ARMORS\n");
phosg::fwrite_fmt(stream, " CODE => ---ID--- TYPE SKIN POINTS -DFP- -EVP- BP BE FLAG LVL EFR ETH EIC EDK ELT DFR EVR SB(S1:AMT1,S2:AMT2) TB FT A4 ST* ---DIVISOR--- NAME\n");
phosg::fwrite_fmt(stream, " CODE => ---ID--- TYPE SKIN POINTS -DFP- -EVP- BP BE FLAG LVL EFR ETH EIC EDK ELT DFR EVR SB(S1:AMT1,S2:AMT2) TB(TN:FL:AMOUNT, ... ) FT A4 ST* ---DIVISOR--- NAME\n");
for (size_t data1_1 = 1; data1_1 < 3; data1_1++) {
float sale_divisor = pmt->get_sale_divisor(0x01, data1_1);
string divisor_str = std::format("{:g}", sale_divisor);
std::string divisor_str = std::format("{:g}", sale_divisor);
divisor_str.resize(13, ' ');
size_t data1_2_limit = pmt->num_armors_or_shields_in_class(data1_1);
@@ -867,10 +884,20 @@ void ItemNameIndex::print_table(FILE* stream) const {
item.data1[0] = 0x01;
item.data1[1] = data1_1;
item.data1[2] = data1_2;
string name = this->describe_item(item);
std::string name = this->describe_item(item);
auto& stat_boost = pmt->get_stat_boost(a.stat_boost_entry_index);
phosg::fwrite_fmt(stream, " 01{:02X}{:02X} => {:08X} {:04X} {:04X} {:6} {:5} {:5} {:02X} {:02X} {:04X} {:3} {:3} {:3} {:3} {:3} {:3} {:3} {:3} {:02X}({:02X}:{:04X},{:02X}:{:04X}) {:02X} {:02X} {:02X} {:2}* {} {}\n",
std::string tech_boost_str;
if (a.tech_boost_entry_index < pmt->num_tech_boosts()) {
const auto& tech_boost = pmt->get_tech_boost(a.tech_boost_entry_index);
tech_boost_str = std::format("({:02X}:{:02X}:{:g},{:02X}:{:02X}:{:g},{:02X}:{:02X}:{:g})",
tech_boost.tech_num1, tech_boost.flags1, tech_boost.amount1, tech_boost.tech_num2, tech_boost.flags2,
tech_boost.amount2, tech_boost.tech_num3, tech_boost.flags3, tech_boost.amount3);
}
tech_boost_str.resize(40, ' ');
phosg::fwrite_fmt(stream, " 01{:02X}{:02X} => {:08X} {:04X} {:04X} {:6} {:5} {:5} {:02X} {:02X} {:04X} {:3} {:3} {:3} {:3} {:3} {:3} {:3} {:3} {:02X}({:02X}:{:04X},{:02X}:{:04X}) {:02X}{} {:02X} {:02X} {:2}* {} {}\n",
data1_1,
data1_2,
a.id,
@@ -881,7 +908,7 @@ void ItemNameIndex::print_table(FILE* stream) const {
a.evp,
a.block_particle,
a.block_effect,
a.class_flags,
a.usability_flags,
static_cast<uint8_t>(a.required_level + 1),
a.efr,
a.eth,
@@ -895,7 +922,8 @@ void ItemNameIndex::print_table(FILE* stream) const {
stat_boost.amount1,
stat_boost.stat2,
stat_boost.amount2,
a.tech_boost,
a.tech_boost_entry_index,
tech_boost_str,
a.flags_type,
a.unknown_a4,
stars,
@@ -908,7 +936,7 @@ void ItemNameIndex::print_table(FILE* stream) const {
phosg::fwrite_fmt(stream, " CODE => ---ID--- TYPE SKIN POINTS STAT COUNT ST-MOD ST* ---DIVISOR--- NAME\n");
{
float sale_divisor = pmt->get_sale_divisor(0x01, 0x03);
string divisor_str = std::format("{:g}", sale_divisor);
std::string divisor_str = std::format("{:g}", sale_divisor);
divisor_str.resize(13, ' ');
size_t data1_2_limit = pmt->num_units();
@@ -920,7 +948,7 @@ void ItemNameIndex::print_table(FILE* stream) const {
item.data1[0] = 0x01;
item.data1[1] = 0x03;
item.data1[2] = data1_2;
string name = this->describe_item(item);
std::string name = this->describe_item(item);
phosg::fwrite_fmt(stream, " 0103{:02X} => {:08X} {:04X} {:04X} {:6} {:04X} {:5} {:6} {:2}* {} {}\n",
data1_2,
@@ -945,14 +973,14 @@ void ItemNameIndex::print_table(FILE* stream) const {
const auto& m = pmt->get_mag(data1_1);
float sale_divisor = pmt->get_sale_divisor(0x02, data1_1);
string divisor_str = std::format("{:g}", sale_divisor);
std::string divisor_str = std::format("{:g}", sale_divisor);
divisor_str.resize(13, ' ');
ItemData item;
item.data1[0] = 0x02;
item.data1[1] = data1_1;
item.data1[2] = 0x00;
string name = this->describe_item(item);
std::string name = this->describe_item(item);
phosg::fwrite_fmt(stream, " 02{:02X}00 => {:08X} {:04X} {:04X} {:6} {:04X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:04X} {} {}\n",
data1_1,
@@ -971,7 +999,7 @@ void ItemNameIndex::print_table(FILE* stream) const {
m.on_low_hp_flag,
m.on_death_flag,
m.on_boss_flag,
m.class_flags,
m.usability_flags,
divisor_str,
name);
}
@@ -981,7 +1009,7 @@ void ItemNameIndex::print_table(FILE* stream) const {
phosg::fwrite_fmt(stream, " CODE => ---ID--- TYPE SKIN POINTS COUNT TECH -COST- ITEMFLAG ---DIVISOR--- NAME\n");
for (size_t data1_1 = 0; data1_1 < pmt->num_tool_classes(); data1_1++) {
float sale_divisor = pmt->get_sale_divisor(0x03, data1_1);
string divisor_str = std::format("{:g}", sale_divisor);
std::string divisor_str = std::format("{:g}", sale_divisor);
divisor_str.resize(13, ' ');
size_t data1_2_limit = pmt->num_tools_in_class(data1_1);
@@ -993,7 +1021,7 @@ void ItemNameIndex::print_table(FILE* stream) const {
item.data1[1] = data1_1;
item.data1[(data1_1 == 0x02) ? 4 : 2] = data1_2;
item.set_tool_item_amount(*this->limits, 1);
string name = this->describe_item(item);
std::string name = this->describe_item(item);
phosg::fwrite_fmt(stream, " 03{:02X}{:02X} => {:08X} {:04X} {:04X} {:6} {:5} {:04X} {:6} {:08X} {} {}\n",
data1_1,
@@ -1040,7 +1068,7 @@ void ItemNameIndex::print_table(FILE* stream) const {
}
phosg::fwrite_fmt(stream, "SPECIAL DEFINITIONS\n");
phosg::fwrite_fmt(stream, " SPECIAL => TYPE COUNT ST* NAME\n");
phosg::fwrite_fmt(stream, " SP => TYPE COUNT ST* NAME\n");
for (size_t index = 0; index < pmt->num_specials(); index++) {
const auto& sp = pmt->get_special(index);
uint8_t stars = pmt->get_special_stars(index);
@@ -1048,15 +1076,15 @@ void ItemNameIndex::print_table(FILE* stream) const {
if (index) {
try {
name = name_for_weapon_special.at(index);
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
}
}
phosg::fwrite_fmt(stream, " {:02X} => {:04X} {:5} {:2}* {}\n", index, sp.type, sp.amount, stars, name);
phosg::fwrite_fmt(stream, " {:02X} => {:04X} {:5} {:2}* {}\n", index, sp.type, sp.amount, stars, name);
}
phosg::fwrite_fmt(stream, "ITEM COMBINATIONS\n");
phosg::fwrite_fmt(stream, " ---USE + -EQUIP => RESULT MLV GND LVL CLS\n");
for (const auto& combo_list_it : pmt->get_all_item_combinations()) {
for (const auto& combo_list_it : pmt->item_combinations_index()) {
for (const auto& combo : combo_list_it.second) {
phosg::fwrite_fmt(stream, " {:02X}{:02X}{:02X} + {:02X}{:02X}{:02X} => {:02X}{:02X}{:02X}",
combo.used_item[0], combo.used_item[1], combo.used_item[2],
@@ -1096,4 +1124,157 @@ void ItemNameIndex::print_table(FILE* stream) const {
event_item.item[0], event_item.item[1], event_item.item[2], event_item.probability);
}
}
phosg::fwrite_fmt(stream, "PHOTON COLORS\n");
phosg::fwrite_fmt(stream, " ## => ---A1--- (A2) (A3)\n");
for (size_t z = 0; z < pmt->num_photon_colors(); z++) {
const auto& pc = pmt->get_photon_color(z);
phosg::fwrite_fmt(stream, " {:02X} => {:08X} ({:g}, {:g}, {:g}, {:g}) ({:g}, {:g}, {:g}, {:g})\n",
z, pc.unknown_a1, pc.unknown_a2.x, pc.unknown_a2.y, pc.unknown_a2.z, pc.unknown_a2.t,
pc.unknown_a3.x, pc.unknown_a3.y, pc.unknown_a3.z, pc.unknown_a3.t);
}
phosg::fwrite_fmt(stream, "WEAPON RANGES\n");
phosg::fwrite_fmt(stream, " ## => ---A3--- ---A4--- ---A5--- (A1) (A2)\n");
for (size_t z = 0; z < pmt->num_weapon_ranges(); z++) {
const auto& wr = pmt->get_weapon_range(z);
phosg::fwrite_fmt(stream, " {:02X} => {:08X} {:08X} {:08X} ({:g}) ({:g})\n",
z, wr.unknown_a3, wr.unknown_a4, wr.unknown_a5, wr.unknown_a1, wr.unknown_a2);
}
phosg::fwrite_fmt(stream, "SALE DIVISORS\n");
phosg::fwrite_fmt(stream, " ARMOR = {:g}\n", pmt->get_sale_divisor(1, 1));
phosg::fwrite_fmt(stream, " SHIELD = {:g}\n", pmt->get_sale_divisor(1, 2));
phosg::fwrite_fmt(stream, " UNIT = {:g}\n", pmt->get_sale_divisor(1, 3));
phosg::fwrite_fmt(stream, " MAG = {:g}\n", pmt->get_sale_divisor(2, 0));
auto write_data_string = [&](const std::string& data, size_t addr = 0) -> void {
if (data.empty()) {
phosg::fwrite_fmt(stream, " (no data)\n");
} else {
auto data_str = phosg::format_data(data, addr);
phosg::strip_trailing_whitespace(data_str);
phosg::fwrite_fmt(stream, " {}\n", phosg::str_replace_all(data_str, "\n", "\n "));
}
};
phosg::fwrite_fmt(stream, "STAR VALUES\n");
write_data_string(pmt->get_star_value_table(), pmt->get_star_value_index_range().first);
phosg::fwrite_fmt(stream, "UNKNOWN_A1\n");
write_data_string(pmt->get_unknown_a1());
phosg::fwrite_fmt(stream, "WEAPON EFFECTS\n");
phosg::fwrite_fmt(stream, " ## => -SOUND1- -VALUE1- -SOUND2- -VALUE2- ----------------A5---------------\n");
for (size_t z = 0; z < pmt->num_weapon_effects(); z++) {
const auto& we = pmt->get_weapon_effect(z);
auto a5_str = phosg::format_data_string(we.unknown_a5.data(), we.unknown_a5.size());
phosg::fwrite_fmt(stream, " {:02X} => {:08X} {:08X} {:08X} {:08X} {}\n",
z, we.sound_id1, we.eff_value1, we.sound_id2, we.eff_value2, a5_str);
}
phosg::fwrite_fmt(stream, "WEAPON STAT BOOST INDEX TABLE\n");
write_data_string(pmt->get_weapon_stat_boost_index_table());
phosg::fwrite_fmt(stream, "ARMOR STAT BOOST INDEX TABLE\n");
write_data_string(pmt->get_armor_stat_boost_index_table());
phosg::fwrite_fmt(stream, "SHIELD STAT BOOST INDEX TABLE\n");
write_data_string(pmt->get_shield_stat_boost_index_table());
phosg::fwrite_fmt(stream, "STAT BOOSTS\n");
phosg::fwrite_fmt(stream, " ## => BOOSTS\n");
for (size_t z = 0; z < pmt->num_stat_boosts(); z++) {
const auto& sb = pmt->get_stat_boost(z);
static constexpr std::array<const char*, 0x10> stat_names{
"ATP+", "ATA+", "EVP+", "DFP+", "MST+", "HP+", "LCK+", "ALL+",
"ATP-", "ATA-", "EVP-", "DFP-", "MST-", "HP-", "LCK-", "ALL-"};
std::string s;
if (sb.stat1 > 0x10) {
s = std::format("[{:02X}:{:04X}]", sb.stat1, sb.amount1);
} else if (sb.stat1 > 0) {
s = std::format("{}{}", stat_names[sb.stat1 - 1], sb.amount1);
}
if (sb.stat2) {
if (!s.empty()) {
s += ", ";
}
if (sb.stat2 > 0x10) {
s += std::format("[{:02X}:{:04X}]", sb.stat2, sb.amount2);
} else if (sb.stat2 > 0) {
s += std::format("{}{}", stat_names[sb.stat2 - 1], sb.amount2);
}
}
if (s.empty()) {
s = "(none)";
}
phosg::fwrite_fmt(stream, " {:02X} => {}\n", z, s);
}
phosg::fwrite_fmt(stream, "SHIELD EFFECTS\n");
phosg::fwrite_fmt(stream, " ## => -SOUND1- ---A1---\n");
for (size_t z = 0; z < pmt->num_shield_effects(); z++) {
const auto& se = pmt->get_shield_effect(z);
phosg::fwrite_fmt(stream, " {:02X} => {:08X} {:08X}\n", z, se.sound_id, se.unknown_a1);
}
phosg::fwrite_fmt(stream, "SOUND REMAPS\n");
phosg::fwrite_fmt(stream, " -SOUND1- => RT:[...] CC:[...]\n");
for (const auto& remap : pmt->get_all_sound_remaps()) {
std::string rt_str;
for (uint32_t rt_sound_id : remap.by_rt_index) {
if (!rt_str.empty()) {
rt_str += ",";
}
rt_str += std::format("{:08X}", rt_sound_id);
}
std::string cc_str;
for (uint32_t cc_sound_id : remap.by_char_class) {
if (!cc_str.empty()) {
cc_str += ",";
}
cc_str += std::format("{:08X}", cc_sound_id);
}
phosg::fwrite_fmt(stream, " {:08X} => RT:[{}] CC:[{}]\n", remap.sound_id, rt_str, cc_str);
}
phosg::fwrite_fmt(stream, "TECH BOOSTS\n");
phosg::fwrite_fmt(stream, " ## => BOOSTS\n");
for (size_t z = 0; z < pmt->num_tech_boosts(); z++) {
const auto& tb = pmt->get_tech_boost(z);
std::string s;
if (tb.amount1) {
s += std::format("{:02X}:{:02X}:{:g}", tb.tech_num1, tb.flags1, tb.amount1);
}
if (tb.amount2) {
s += std::format("{:02X}:{:02X}:{:g}", tb.tech_num2, tb.flags2, tb.amount2);
}
if (tb.amount3) {
s += std::format("{:02X}:{:02X}:{:g}", tb.tech_num3, tb.flags3, tb.amount3);
}
phosg::fwrite_fmt(stream, " {:02X} => {}\n", z, s);
}
phosg::fwrite_fmt(stream, "UNSEALABLE ITEMS\n");
phosg::fwrite_fmt(stream, " -ITEM- NAME\n");
std::vector<uint32_t> unsealable_items;
for (uint32_t item_code : pmt->all_unsealable_items()) {
unsealable_items.emplace_back(item_code);
}
std::sort(unsealable_items.begin(), unsealable_items.end());
for (uint32_t item_code : unsealable_items) {
ItemData item;
item.data1[0] = item_code >> 16;
item.data1[1] = item_code >> 8;
item.data1[2] = item_code;
phosg::fwrite_fmt(stream, " {:06X} {}\n", item_code, this->describe_item(item));
}
phosg::fwrite_fmt(stream, "RANGED SPECIALS\n");
phosg::fwrite_fmt(stream, " ## => 11 12 WR A1\n");
for (size_t z = 0; z < pmt->num_ranged_specials(); z++) {
const auto& rs = pmt->get_ranged_special(z);
phosg::fwrite_fmt(stream, " {:02X} => {:02X} {:02X} {:02X} {:02X}\n",
z, rs.data1_1, rs.data1_2, rs.weapon_range_index, rs.unknown_a1);
}
}
+2299 -328
View File
File diff suppressed because it is too large Load Diff
+239 -70
View File
@@ -51,9 +51,21 @@ public:
uint16_t type = 0; // "Model" in Soly's ItemPMT editor
uint16_t skin = 0; // "Texture" in Soly's ItemPMT editor
uint32_t team_points = 0;
void parse_base_from_json(const phosg::JSON& json);
phosg::JSON json() const;
};
struct Weapon : ItemBase {
uint16_t class_flags = 0;
// Bits in usability_flags (to be usable, all bits corresponding to the character's attributes must be set):
// 01 = hunter
// 02 = ranger
// 04 = force
// 08 = human
// 10 = android
// 20 = newman
// 40 = male
// 80 = female
uint16_t usability_flags = 0;
uint16_t atp_min = 0;
uint16_t atp_max = 0;
uint16_t atp_required = 0;
@@ -64,7 +76,8 @@ public:
uint8_t photon = 0;
uint8_t special = 0;
uint8_t ata = 0;
uint8_t stat_boost_entry_index = 0; // TODO: This could be larger (16 or 32 bits)
uint8_t stat_boost_entry_index = 0;
parray<uint8_t, 3> v2_unknown_a9;
uint8_t projectile = 0;
int8_t trail1_x = 0;
int8_t trail1_y = 0;
@@ -74,13 +87,16 @@ public:
parray<uint8_t, 3> unknown_a1 = 0;
uint8_t unknown_a4 = 0;
uint8_t unknown_a5 = 0;
uint8_t tech_boost = 0;
uint8_t tech_boost_entry_index = 0;
// Bits in behavior_flags:
// 01 = disable combos (weapon can only be used once in a row)
// 02 = TODO (sets TItemWeapon flag 40000; used in TItemWeapon_v1E)
// 04 = TODO (sets TItemWeapon flag 80000; used in TItemWeapon_v1E)
// 08 = weapon cannot have attributes (they are ignored if present)
uint8_t behavior_flags = 0;
static Weapon from_json(const phosg::JSON& json);
phosg::JSON json() const;
};
struct ArmorOrShield : ItemBase {
@@ -88,7 +104,7 @@ public:
uint16_t evp = 0;
uint8_t block_particle = 0;
uint8_t block_effect = 0;
uint16_t class_flags = 0x00FF;
uint16_t usability_flags = 0x00FF; // See Weapon::usability_flags for details
uint8_t required_level = 0;
uint8_t efr = 0;
uint8_t eth = 0;
@@ -98,7 +114,7 @@ public:
uint8_t dfp_range = 0;
uint8_t evp_range = 0;
uint8_t stat_boost_entry_index = 0;
uint8_t tech_boost = 0;
uint8_t tech_boost_entry_index = 0;
// TODO: Figure out what this does. Only a few values appear to do anything:
// Armors:
// 01 sets item->flags |= 1 (used in TItemProArmor_v10)
@@ -108,18 +124,44 @@ public:
// 03 sets item->flags |= 8 (used in TItemProShield_v1A)
uint8_t flags_type = 0;
uint8_t unknown_a4 = 0;
static ArmorOrShield from_json(const phosg::JSON& json);
phosg::JSON json() const;
};
struct Unit : ItemBase {
uint16_t stat = 0;
uint16_t stat_amount = 0;
int16_t modifier_amount = 0;
static Unit from_json(const phosg::JSON& json);
phosg::JSON json() const;
};
struct Mag : ItemBase {
uint16_t feed_table = 0;
uint8_t photon_blast = 0;
uint8_t activation = 0;
// Internally, when a mag effect is about to activate, the game computes a byte with the following flags:
// 01 = HP low (causes effect in mag's on_low_hp if eligible, priority 2)
// 02 = died (causes effect in mag's on_death if eligible, priority 3)
// 04 = synchro < 30 (causes effect 5, priority 7)
// 08 = synchro > 100 (causes effect 4, priority 8)
// 10 = TODO (3OE1:80112B24) (causes effect 3, priority 6)
// 20 = player leveled up (causes effect 2, priority 5)
// 40 = PB meter filled (causes effect in mag's on_pb_full if eligible, priority 1)
// 80 = entered boss arena (causes effect in mag's on_boss if eligible, priority 4)
// Values for on_* trigger fields:
// 0 = no effect
// 1 = TODO (used internally; not synced via 6x61; possibly just cancel previous effect?)
// 2 = TODO (used internally; seems to be effect 1, but delayed by 45 frames?)
// 3 = TODO (used internally; seems to be effect 1, but delayed by 90 frames?)
// 4 = Shifta + Deband (level = (IQ / 40) + 1; delayed by 90 frames)
// 5 = TODO (used internally; seems to be effect 1, but delayed by 90 frames?)
// 6 = Resta (not synced via 6x61; level = (IQ / 40) + 1; delayed by 90 frames)
// 7 = Reverser (not synced via 6x61; delayed by 90 frames)
// 8 = invincibility (duration = (((IQ + synchro) / 3) + 40) * 30 frames, but this is capped at 30 seconds, so in
// practice it's always just 30 seconds regardless of IQ and synchro)
uint8_t on_pb_full = 0;
uint8_t on_low_hp = 0;
uint8_t on_death = 0;
@@ -141,7 +183,10 @@ public:
uint8_t on_low_hp_flag = 0;
uint8_t on_death_flag = 0;
uint8_t on_boss_flag = 0;
uint16_t class_flags = 0x00FF;
uint16_t usability_flags = 0x00FF; // See Weapon::usability_flags for details
static Mag from_json(const phosg::JSON& json);
phosg::JSON json() const;
};
struct Tool : ItemBase {
@@ -157,7 +202,10 @@ public:
// 00000020 - usable in boss arenas
// 00000040 - usable in Challenge mode
// 00000080 - is rare (renders as red box; V3+ only)
/* 0C */ uint32_t item_flags = 0;
uint32_t item_flags = 0;
static Tool from_json(const phosg::JSON& json);
phosg::JSON json() const;
};
struct MagFeedResult {
@@ -168,11 +216,17 @@ public:
int8_t iq = 0;
int8_t synchro = 0;
parray<uint8_t, 2> unused;
static MagFeedResult from_json(const phosg::JSON& json);
phosg::JSON json() const;
} __packed_ws__(MagFeedResult, 8);
struct Special {
uint16_t type = 0xFFFF;
uint16_t amount = 0;
static Special from_json(const phosg::JSON& json);
phosg::JSON json() const;
};
struct StatBoost {
@@ -201,6 +255,9 @@ public:
uint16_t amount1 = 0;
uint8_t stat2 = 0;
uint16_t amount2 = 0;
static StatBoost from_json(const phosg::JSON& json);
phosg::JSON json() const;
} __attribute__((packed));
// Indexed as [tech_num][char_class]
@@ -215,23 +272,41 @@ public:
uint8_t level = 0;
uint8_t char_class = 0;
parray<uint8_t, 3> unused;
static ItemCombination from_json(const phosg::JSON& json);
phosg::JSON json() const;
} __packed_ws__(ItemCombination, 0x10);
struct TechniqueBoost {
uint8_t tech_num = 0;
// It appears that only one bit in the flags field is used: 01 = enable piercing (for Megid)
uint8_t flags = 0;
float amount = 0.0f;
struct TechBoost {
// It appears that only one bit in the flags fields is used: 01 = enable piercing (for Megid)
uint8_t tech_num1 = 0;
uint8_t flags1 = 0;
float amount1 = 0.0f;
uint8_t tech_num2 = 0;
uint8_t flags2 = 0;
float amount2 = 0.0f;
uint8_t tech_num3 = 0;
uint8_t flags3 = 0;
float amount3 = 0.0f;
static TechBoost from_json(const phosg::JSON& json);
phosg::JSON json() const;
};
struct EventItem {
parray<uint8_t, 3> item;
uint8_t probability = 0;
static EventItem from_json(const phosg::JSON& json);
phosg::JSON json() const;
} __packed_ws__(EventItem, 4);
struct UnsealableItem {
parray<uint8_t, 3> item;
uint8_t unused = 0;
static UnsealableItem from_json(const phosg::JSON& json);
phosg::JSON json() const;
} __packed_ws__(UnsealableItem, 4);
struct NonWeaponSaleDivisors {
@@ -239,119 +314,213 @@ public:
float shield_divisor = 0.0f;
float unit_divisor = 0.0f;
float mag_divisor = 0.0f;
static NonWeaponSaleDivisors from_json(const phosg::JSON& json);
phosg::JSON json() const;
};
struct ShieldEffect {
uint32_t sound_id;
uint32_t unknown_a1;
uint32_t sound_id = 0;
uint32_t unknown_a1 = 0;
static ShieldEffect from_json(const phosg::JSON& json);
phosg::JSON json() const;
};
struct PhotonColorEntry {
uint32_t unknown_a1;
uint32_t unknown_a1 = 0;
VectorXYZTF unknown_a2;
VectorXYZTF unknown_a3;
static PhotonColorEntry from_json(const phosg::JSON& json);
phosg::JSON json() const;
};
struct UnknownA1 {
uint16_t unknown_a1;
uint16_t unknown_a2;
};
uint16_t unknown_a1 = 0;
uint16_t unknown_a2 = 0;
struct UnknownA5 {
uint32_t target_param; // For players, char_class; for enemies, rt_index; for objects, 0x30
uint32_t unknown_a2;
uint32_t unknown_a3;
static UnknownA1 from_json(const phosg::JSON& json);
phosg::JSON json() const;
};
struct WeaponEffect {
uint32_t sound_id1;
uint32_t eff_value1;
uint32_t sound_id2;
uint32_t eff_value2;
uint32_t sound_id1 = 0;
uint32_t eff_value1 = 0;
uint32_t sound_id2 = 0;
uint32_t eff_value2 = 0;
parray<uint8_t, 0x10> unknown_a5;
static WeaponEffect from_json(const phosg::JSON& json);
phosg::JSON json() const;
};
struct WeaponRange {
float unknown_a1;
float unknown_a2;
uint32_t unknown_a3; // Angle
uint32_t unknown_a4; // Angle
uint32_t unknown_a5;
float unknown_a1 = 0;
float unknown_a2 = 0;
uint32_t unknown_a3 = 0; // Angle
uint32_t unknown_a4 = 0; // Angle
uint32_t unknown_a5 = 0;
static WeaponRange from_json(const phosg::JSON& json);
phosg::JSON json() const;
};
struct RangedSpecial {
uint8_t data1_1;
uint8_t data1_2;
uint8_t weapon_range_index;
uint8_t unknown_a1;
uint8_t data1_1 = 0;
uint8_t data1_2 = 0;
uint8_t weapon_range_index = 0;
uint8_t unknown_a1 = 0;
static RangedSpecial from_json(const phosg::JSON& json);
phosg::JSON json() const;
} __packed_ws__(RangedSpecial, 4);
ItemParameterTable() = delete;
struct SoundRemaps {
uint32_t sound_id = 0;
std::vector<uint32_t> by_rt_index;
std::vector<uint32_t> by_char_class;
static SoundRemaps from_json(const phosg::JSON& json);
phosg::JSON json() const;
};
virtual ~ItemParameterTable() = default;
static std::shared_ptr<ItemParameterTable> create(std::shared_ptr<const std::string> data, Version version);
static std::shared_ptr<ItemParameterTable> from_binary(std::shared_ptr<const std::string> data, Version version);
static std::shared_ptr<ItemParameterTable> from_json(const phosg::JSON& json);
phosg::JSON json() const;
std::string serialize_binary(Version version) const;
std::set<uint32_t> compute_all_valid_primary_identifiers() const;
// weapon_table accessors
virtual size_t num_weapon_classes() const = 0;
virtual size_t num_weapons_in_class(uint8_t data1_1) const = 0;
virtual const Weapon& get_weapon(uint8_t data1_1, uint8_t data1_2) const = 0;
// armor_table accessors
virtual size_t num_armors_or_shields_in_class(uint8_t data1_1) const = 0;
virtual const ArmorOrShield& get_armor_or_shield(uint8_t data1_1, uint8_t data1_2) const = 0;
// unit_table accessors
virtual size_t num_units() const = 0;
virtual const Unit& get_unit(uint8_t data1_2) const = 0;
virtual size_t num_mags() const = 0;
virtual const Mag& get_mag(uint8_t data1_1) const = 0;
// tool_table accessors
virtual size_t num_tool_classes() const = 0;
virtual size_t num_tools_in_class(uint8_t data1_1) const = 0;
virtual const Tool& get_tool(uint8_t data1_1, uint8_t data1_2) const = 0;
virtual std::pair<uint8_t, uint8_t> find_tool_by_id(uint32_t id) const = 0;
std::variant<const Weapon*, const ArmorOrShield*, const Unit*, const Mag*, const Tool*>
definition_for_primary_identifier(uint32_t primary_identifier) const;
// mag_table accessors
virtual size_t num_mags() const = 0;
virtual const Mag& get_mag(uint8_t data1_1) const = 0;
// weapon_kind_table accessors (data1_1 in [0, num_weapon_classes()])
virtual size_t num_weapon_kinds() const = 0;
virtual uint8_t get_weapon_kind(uint8_t data1_1) const = 0;
// photon_color_table accessors
virtual size_t num_photon_colors() const = 0;
virtual const PhotonColorEntry& get_photon_color(size_t index) const = 0;
// weapon_range_table accessors
virtual size_t num_weapon_ranges() const = 0;
virtual const WeaponRange& get_weapon_range(size_t index) const = 0;
// weapon_sale_divisor_table and non_weapon_sale_divisor_table accessors (data1_0 in [0, 1, 2]; data1_1 in [0,
// num_weapon_classes()] for weapons or ignored otherwise)
virtual size_t num_weapon_sale_divisors() const = 0;
virtual float get_sale_divisor(uint8_t data1_0, uint8_t data1_1) const = 0;
virtual const MagFeedResult& get_mag_feed_result(uint8_t table_index, uint8_t which) const = 0;
// mag_feed_table accessors (table_index in [0, 7], item_index in [0, 10])
virtual const MagFeedResult& get_mag_feed_result(uint8_t table_index, uint8_t item_index) const = 0;
// star_value_table accessors
virtual std::pair<uint32_t, uint32_t> get_star_value_index_range() const = 0;
virtual uint32_t get_special_stars_base_index() const = 0;
virtual uint8_t get_item_stars(uint32_t id) const = 0;
virtual uint8_t get_special_stars(uint8_t special) const = 0;
std::string get_star_value_table() const;
// unknown_a1 accessors
virtual std::string get_unknown_a1() const = 0;
// special_table accessors
virtual size_t num_specials() const = 0;
virtual const Special& get_special(uint8_t special) const = 0;
virtual const StatBoost& get_stat_boost(uint8_t entry_index) const = 0;
virtual uint8_t get_max_tech_level(uint8_t char_class, uint8_t tech_num) const = 0;
virtual uint8_t get_weapon_class(uint8_t data1_1) const = 0;
// weapon_effect_table accessors
virtual size_t num_weapon_effects() const = 0;
virtual const WeaponEffect& get_weapon_effect(size_t index) const = 0;
// weapon_stat_boost_index_table accessors
virtual size_t num_weapon_stat_boost_indexes() const = 0;
virtual uint8_t get_weapon_stat_boost_index(size_t index) const = 0;
std::string get_weapon_stat_boost_index_table() const;
// armor_stat_boost_index_table accessors
virtual size_t num_armor_stat_boost_indexes() const = 0;
virtual uint8_t get_armor_stat_boost_index(size_t index) const = 0;
std::string get_armor_stat_boost_index_table() const;
// shield_stat_boost_index_table accessors
virtual size_t num_shield_stat_boost_indexes() const = 0;
virtual uint8_t get_shield_stat_boost_index(size_t index) const = 0;
std::string get_shield_stat_boost_index_table() const;
// stat_boost_table accessors
virtual size_t num_stat_boosts() const = 0;
virtual const StatBoost& get_stat_boost(size_t index) const = 0;
// shield_effect_table accessors
virtual size_t num_shield_effects() const = 0;
virtual const ShieldEffect& get_shield_effect(size_t index) const = 0;
// max_tech_level_table accessors
virtual uint8_t get_max_tech_level(uint8_t char_class, uint8_t tech_num) const = 0;
// combination_table accessors
virtual size_t num_item_combinations() const = 0;
virtual const ItemCombination& get_item_combination(size_t index) const = 0;
const std::map<uint32_t, std::vector<ItemCombination>>& item_combinations_index() const;
const std::vector<ItemCombination>& all_combinations_for_used_item(const ItemData& used_item) const;
const ItemCombination& get_item_combination(const ItemData& used_item, const ItemData& equipped_item) const;
// sound_remap_table accessors
virtual const std::vector<SoundRemaps>& get_all_sound_remaps() const = 0;
// tech_boost_table accessors
virtual size_t num_tech_boosts() const = 0;
virtual const TechBoost& get_tech_boost(size_t index) const = 0;
// unwrap_table accessors
virtual size_t num_events() const = 0;
virtual std::pair<const EventItem*, size_t> get_event_items(uint8_t event_number) const = 0;
// unsealable_table accessors
virtual const std::set<uint32_t>& all_unsealable_items() const = 0;
bool is_unsealable_item(uint8_t data1_0, uint8_t data1_1, uint8_t data1_2) const;
bool is_unsealable_item(const ItemData& item) const;
// ranged_special_table accessors
virtual size_t num_ranged_specials() const = 0;
virtual const RangedSpecial& get_ranged_special(size_t index) const = 0;
// Composite accessors
std::variant<const Weapon*, const ArmorOrShield*, const Unit*, const Mag*, const Tool*>
definition_for_primary_identifier(uint32_t primary_identifier) const;
uint32_t get_item_id(const ItemData& item) const;
uint32_t get_item_team_points(const ItemData& item) const;
uint8_t get_item_base_stars(const ItemData& item) const;
uint8_t get_item_adjusted_stars(const ItemData& item, bool ignore_unidentified = false) const;
bool is_item_rare(const ItemData& item) const;
virtual bool is_unsealable_item(uint8_t data1_0, uint8_t data1_1, uint8_t data1_2) const = 0;
bool is_unsealable_item(const ItemData& item) const;
const ItemCombination& get_item_combination(const ItemData& used_item, const ItemData& equipped_item) const;
const std::vector<ItemCombination>& get_all_combinations_for_used_item(const ItemData& used_item) const;
virtual const std::map<uint32_t, std::vector<ItemCombination>>& get_all_item_combinations() const = 0;
virtual size_t num_events() const = 0;
virtual std::pair<const EventItem*, size_t> get_event_items(uint8_t event_number) const = 0;
size_t price_for_item(const ItemData& item) const;
protected:
std::shared_ptr<const std::string> data;
phosg::StringReader r;
ItemParameterTable() = default;
mutable std::unordered_map<uint16_t, Weapon> weapons;
mutable std::vector<ArmorOrShield> armors;
mutable std::vector<ArmorOrShield> shields;
mutable std::vector<Unit> units;
mutable std::vector<Mag> mags;
mutable std::unordered_map<uint16_t, Tool> tools;
mutable std::vector<Special> specials;
mutable std::vector<StatBoost> stat_boosts;
// Key is used_item. We can't index on (used_item, equipped_item) because equipped_item may contain wildcards, and
// the matching order matters.
mutable std::map<uint32_t, std::vector<ItemCombination>> item_combination_index;
explicit ItemParameterTable(std::shared_ptr<const std::string> data);
mutable std::optional<std::map<uint32_t, std::vector<ItemCombination>>> item_combination_index;
};
+10 -12
View File
@@ -1,7 +1,5 @@
#include "ItemTranslationTable.hh"
using namespace std;
static constexpr bool is_canonical(uint32_t id) {
return !(id & 0x80000000);
}
@@ -23,12 +21,12 @@ ItemTranslationTable::ItemTranslationTable(
if (is_canonical(id)) {
has_any_canonical_id = true;
if (!this->entry_index_for_version[v_s].emplace(id, z).second) {
throw runtime_error(std::format("(row {}) duplicate canonical ID {:08X}", z, id));
throw std::runtime_error(std::format("(row {}) duplicate canonical ID {:08X}", z, id));
}
}
}
if (!has_any_canonical_id) {
throw runtime_error(std::format("(row {}) no canonical ID present in row", z));
throw std::runtime_error(std::format("(row {}) no canonical ID present in row", z));
}
}
@@ -46,27 +44,27 @@ ItemTranslationTable::ItemTranslationTable(
uint32_t e_id = this->entries[z].id_for_version[v_s];
if (is_canonical(e_id)) {
if (!entry_index.count(e_id)) {
throw logic_error(std::format("(row {} version {}) canonical ID {:X} is missing from the index", z, phosg::name_for_enum(v), e_id));
throw std::logic_error(std::format("(row {} version {}) canonical ID {:08X} is missing from the index", z, phosg::name_for_enum(v), e_id));
}
try {
item_parameter_table->definition_for_primary_identifier(e_id);
} catch (const out_of_range&) {
throw runtime_error(std::format("(row {} version {}) ID {:X} not defined in item parameter table", z, phosg::name_for_enum(v), e_id));
} catch (const std::out_of_range&) {
throw std::runtime_error(std::format("(row {} version {}) ID {:08X} not defined in item parameter table", z, phosg::name_for_enum(v), e_id));
}
if (!remaining_identifiers.erase(e_id)) {
throw runtime_error(std::format("(row {} version {}) ID {:X} not in item parameter table's primary identifier list", z, phosg::name_for_enum(v), e_id));
throw std::runtime_error(std::format("(row {} version {}) ID {:08X} not in item parameter table's primary identifier list", z, phosg::name_for_enum(v), e_id));
}
} else if (!entry_index.count(make_canonical(e_id))) {
throw runtime_error(std::format("(row {} version {}) ID {:X} refers to nonexistent canonical ID", z, phosg::name_for_enum(v), e_id));
throw std::runtime_error(std::format("(row {} version {}) ID {:08X} refers to nonexistent canonical ID", z, phosg::name_for_enum(v), e_id));
}
}
if (!remaining_identifiers.empty()) {
string missing_str = std::format("(version {}) not all identifiers in the item parameter table are defined in the translation table; missing:", phosg::name_for_enum(v));
std::string missing_str = std::format("(version {}) not all identifiers in the item parameter table are defined in the translation table; missing:", phosg::name_for_enum(v));
for (uint32_t id : remaining_identifiers) {
missing_str += std::format(" {:08X}", id);
}
throw runtime_error(missing_str);
throw std::runtime_error(missing_str);
}
}
}
@@ -92,7 +90,7 @@ uint32_t ItemTranslationTable::translate(uint32_t primary_identifier, Version fr
ItemTranslationTable::Entry::Entry(const phosg::JSON& json) {
const auto& l = json.as_list();
if (l.size() != NUM_NON_PATCH_VERSIONS + 1) {
throw runtime_error("list length is incorrect");
throw std::runtime_error("list length is incorrect");
}
for (size_t z = 0; z < NUM_NON_PATCH_VERSIONS; z++) {
this->id_for_version[z] = l[z]->as_int();
+45 -47
View File
@@ -4,9 +4,7 @@
#include "SendCommands.hh"
using namespace std;
void player_use_item(shared_ptr<Client> c, size_t item_index, shared_ptr<RandomGenerator> rand_crypt) {
void player_use_item(std::shared_ptr<Client> c, size_t item_index, std::shared_ptr<RandomGenerator> rand_crypt) {
auto s = c->require_server_state();
// On PC (and presumably DC), the client sends a 6x29 after this to delete the used item. On GC and later versions,
@@ -23,26 +21,26 @@ void player_use_item(shared_ptr<Client> c, size_t item_index, shared_ptr<RandomG
// Nothing to do (it should be deleted)
} else if ((primary_identifier & 0xFFFF0000) == 0x03020000) { // Technique disk
auto item_parameter_table = s->item_parameter_table(c->version());
uint8_t max_level = item_parameter_table->get_max_tech_level(player->disp.visual.char_class, item.data.data1[4]);
auto item_parameter_table = s->data->item_parameter_table(c->version());
uint8_t max_level = item_parameter_table->get_max_tech_level(player->disp.visual.sh.char_class, item.data.data1[4]);
if (item.data.data1[2] > max_level) {
throw runtime_error("technique level too high");
throw std::runtime_error("technique level too high");
}
player->set_technique_level(item.data.data1[4], item.data.data1[2]);
} else if ((primary_identifier & 0xFFFF0000) == 0x030A0000) { // Grinder
if (item.data.data1[2] > 2) {
throw runtime_error("incorrect grinder value");
throw std::runtime_error("incorrect grinder value");
}
auto& weapon = player->inventory.items[player->inventory.find_equipped_item(EquipSlot::WEAPON)];
// Only enforce grind limits on BB, since the server doesn't have direct control over inventories on other versions
auto item_parameter_table = s->item_parameter_table(c->version());
auto item_parameter_table = s->data->item_parameter_table(c->version());
auto weapon_def = item_parameter_table->get_weapon(weapon.data.data1[1], weapon.data.data1[2]);
if (is_v4 && (weapon.data.data1[3] >= weapon_def.max_grind)) {
throw runtime_error("weapon already at maximum grind");
throw std::runtime_error("weapon already at maximum grind");
}
weapon.data.data1[3] = min<uint8_t>(weapon.data.data1[3] + item.data.data1[2] + 1, weapon_def.max_grind);
weapon.data.data1[3] = std::min<uint8_t>(weapon.data.data1[3] + item.data.data1[2] + 1, weapon_def.max_grind);
} else if ((primary_identifier & 0xFFFF0000) == 0x030B0000) { // Material
auto p = c->character_file();
@@ -85,11 +83,11 @@ void player_use_item(shared_ptr<Client> c, size_t item_index, shared_ptr<RandomG
if (!is_v3_or_later && (c->version() != Version::GC_NTE)) {
p->disp.stats.char_stats.lck += 2;
} else {
throw runtime_error("unknown material used");
throw std::runtime_error("unknown material used");
}
break;
default:
throw runtime_error("unknown material used");
throw std::runtime_error("unknown material used");
}
if (is_v3_or_later || (type == Type::HP) || (type == Type::TP)) {
p->set_material_usage(type, p->get_material_usage(type) + 1);
@@ -98,13 +96,13 @@ void player_use_item(shared_ptr<Client> c, size_t item_index, shared_ptr<RandomG
} else if ((primary_identifier & 0xFFFF0000) == 0x030F0000) { // AddSlot
auto& armor = player->inventory.items[player->inventory.find_equipped_item(EquipSlot::ARMOR)];
if (armor.data.data1[5] >= 4) {
throw runtime_error("armor already at maximum slot count");
throw std::runtime_error("armor already at maximum slot count");
}
armor.data.data1[5]++;
} else if (item.data.is_wrapped(*s->item_stack_limits(c->version()))) {
} else if (item.data.is_wrapped(*s->data->item_stack_limits(c->version()))) {
// Unwrap present
item.data.unwrap(*s->item_stack_limits(c->version()));
item.data.unwrap(*s->data->item_stack_limits(c->version()));
should_delete_item = false;
} else if (primary_identifier == 0x00330000) {
@@ -133,14 +131,14 @@ void player_use_item(shared_ptr<Client> c, size_t item_index, shared_ptr<RandomG
} else if ((primary_identifier & 0xFFFF0000) == 0x030C0000) { // Non-combo mag cells
auto& mag = player->inventory.items[player->inventory.find_equipped_item(EquipSlot::MAG)];
uint8_t evolution_number = s->mag_evolution_table(c->version())->get_evolution_number(mag.data.data1[1]);
uint8_t evolution_number = s->data->mag_metadata_table(c->version())->get_evolution_number(mag.data.data1[1]);
if (evolution_number < 4) {
switch (item.data.data1[2]) {
case 0x00: // Cell of MAG 502
mag.data.data1[1] = (player->disp.visual.section_id & 1) ? 0x1D : 0x21;
mag.data.data1[1] = (player->disp.visual.sh.section_id & 1) ? 0x1D : 0x21;
break;
case 0x01: // Cell of MAG 213
mag.data.data1[1] = (player->disp.visual.section_id & 1) ? 0x27 : 0x22;
mag.data.data1[1] = (player->disp.visual.sh.section_id & 1) ? 0x27 : 0x22;
break;
case 0x02: // Parts of RoboChao
mag.data.data1[1] = 0x28;
@@ -155,20 +153,20 @@ void player_use_item(shared_ptr<Client> c, size_t item_index, shared_ptr<RandomG
mag.data.data1[1] = 0x2B;
break;
default:
throw runtime_error("invalid mag cell used");
throw std::runtime_error("invalid mag cell used");
}
}
} else if ((primary_identifier & 0xFFFF0000) == 0x03150000) {
// Christmas Present, etc. - use unwrap_table + probabilities therein
auto item_parameter_table = s->item_parameter_table(c->version());
auto item_parameter_table = s->data->item_parameter_table(c->version());
auto table = item_parameter_table->get_event_items(item.data.data1[2]);
size_t sum = 0;
for (size_t z = 0; z < table.second; z++) {
sum += table.first[z].probability;
}
if (sum == 0) {
throw runtime_error("no unwrap results available for event");
throw std::runtime_error("no unwrap results available for event");
}
// TODO: It seems that on non-BB, clients don't synchronize this at all, so they could end up thinking the
// unwrapped item is something completely different. (They don't even use a fixed random seed, like for rares; they
@@ -200,7 +198,7 @@ void player_use_item(shared_ptr<Client> c, size_t item_index, shared_ptr<RandomG
// The unopened Hunters Report's rank is stored in the kill count field; using the unopened report copies the rank
// to data1[2] and replaces the inventory item with a new item with the same ID. The game also moves the item to
// the end of the inventory, so we do the same.
const auto& stack_limits = *s->item_stack_limits(c->version());
const auto& stack_limits = *s->data->item_stack_limits(c->version());
auto report_item = player->remove_item(item.data.id, 1, stack_limits);
report_item.data1[2] = report_item.get_kill_count();
player->add_item(report_item, stack_limits);
@@ -215,33 +213,33 @@ void player_use_item(shared_ptr<Client> c, size_t item_index, shared_ptr<RandomG
continue;
}
try {
auto item_parameter_table = s->item_parameter_table(c->version());
auto item_parameter_table = s->data->item_parameter_table(c->version());
const auto& combo = item_parameter_table->get_item_combination(item.data, inv_item.data);
if (combo.char_class != 0xFF && combo.char_class != player->disp.visual.char_class) {
throw runtime_error("item combination requires specific char_class");
if (combo.char_class != 0xFF && combo.char_class != player->disp.visual.sh.char_class) {
throw std::runtime_error("item combination requires specific char_class");
}
if (combo.mag_level != 0xFF) {
if (inv_item.data.data1[0] != 2) {
throw runtime_error("item combination applies with mag level requirement, but equipped item is not a mag");
throw std::runtime_error("item combination applies with mag level requirement, but equipped item is not a mag");
}
if (inv_item.data.compute_mag_level() < combo.mag_level) {
throw runtime_error("item combination applies with mag level requirement, but equipped mag level is too low");
throw std::runtime_error("item combination applies with mag level requirement, but equipped mag level is too low");
}
}
if (combo.grind != 0xFF) {
if (inv_item.data.data1[0] != 0) {
throw runtime_error("item combination applies with grind requirement, but equipped item is not a weapon");
throw std::runtime_error("item combination applies with grind requirement, but equipped item is not a weapon");
}
if (inv_item.data.data1[3] < combo.grind) {
throw runtime_error("item combination applies with grind requirement, but equipped weapon grind is too low");
throw std::runtime_error("item combination applies with grind requirement, but equipped weapon grind is too low");
}
}
if (combo.level != 0xFF && player->disp.stats.level + 1 < combo.level) {
throw runtime_error("item combination applies with level requirement, but player level is too low");
throw std::runtime_error("item combination applies with level requirement, but player level is too low");
}
// If we get here, then the combo applies
if (combo_applied) {
throw runtime_error("multiple combinations apply");
throw std::runtime_error("multiple combinations apply");
}
combo_applied = true;
@@ -254,7 +252,7 @@ void player_use_item(shared_ptr<Client> c, size_t item_index, shared_ptr<RandomG
inv_item.data.data1[4] = 0; // Flags + special
}
inv_item.flags &= (~8); // Unequip it
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
}
}
}
@@ -262,20 +260,20 @@ void player_use_item(shared_ptr<Client> c, size_t item_index, shared_ptr<RandomG
if (should_delete_item) {
// Allow overdrafting meseta if the client is not BB, since the server isn't informed when meseta is added or
// removed from the bank.
player->remove_item(item.data.id, 1, *s->item_stack_limits(c->version()));
player->remove_item(item.data.id, 1, *s->data->item_stack_limits(c->version()));
}
}
void apply_mag_feed_result(
ItemData& mag_item,
const ItemData& fed_item,
shared_ptr<const ItemParameterTable> item_parameter_table,
shared_ptr<const MagEvolutionTable> mag_evolution_table,
std::shared_ptr<const ItemParameterTable> item_parameter_table,
std::shared_ptr<const MagMetadataTable> mag_metadata_table,
uint8_t char_class,
uint8_t section_id,
bool version_has_rare_mags) {
static const unordered_map<uint32_t, size_t> result_index_for_fed_item({
static const std::unordered_map<uint32_t, size_t> result_index_for_fed_item({
{0x03000000, 0}, // Monomate
{0x03000100, 1}, // Dimate
{0x03000200, 2}, // Trimate
@@ -298,7 +296,7 @@ void apply_mag_feed_result(
if ((delta > 0) || ((delta < 0) && (-delta < existing_stat))) {
uint16_t level = data.compute_mag_level();
if (level > 200) {
throw runtime_error("mag level is too high");
throw std::runtime_error("mag level is too high");
}
if ((level == 200) && ((99 - existing_stat) < delta)) {
delta = 99 - existing_stat;
@@ -311,12 +309,12 @@ void apply_mag_feed_result(
update_stat(mag_item, 3, feed_result.pow);
update_stat(mag_item, 4, feed_result.dex);
update_stat(mag_item, 5, feed_result.mind);
mag_item.data2[0] = clamp<ssize_t>(static_cast<ssize_t>(mag_item.data2[0]) + feed_result.synchro, 0, 120);
mag_item.data2[1] = clamp<ssize_t>(static_cast<ssize_t>(mag_item.data2[1]) + feed_result.iq, 0, 200);
mag_item.data2[0] = std::clamp<ssize_t>(static_cast<ssize_t>(mag_item.data2[0]) + feed_result.synchro, 0, 120);
mag_item.data2[1] = std::clamp<ssize_t>(static_cast<ssize_t>(mag_item.data2[1]) + feed_result.iq, 0, 200);
uint8_t mag_level = mag_item.compute_mag_level();
mag_item.data1[2] = mag_level;
uint8_t evolution_number = mag_evolution_table->get_evolution_number(mag_item.data1[1]);
uint8_t evolution_number = mag_metadata_table->get_evolution_number(mag_item.data1[1]);
uint8_t mag_number = mag_item.data1[1];
// Note: Sega really did just hardcode all these rules into the client. There is no data file describing these
@@ -347,7 +345,7 @@ void apply_mag_feed_result(
mag_item.data1[1] = 0x19; // Vritra
break;
default:
throw runtime_error("invalid character class");
throw std::runtime_error("invalid character class");
}
}
@@ -401,7 +399,7 @@ void apply_mag_feed_result(
} else if (is_ranger) {
table_index += 6;
} else if (!is_hunter) {
throw logic_error("char class is not any of the top-level classes");
throw std::logic_error("char class is not any of the top-level classes");
}
// Note: The original code checks the class (hunter/ranger/force) again here, and goes into 3 branches that
@@ -433,7 +431,7 @@ void apply_mag_feed_result(
bool is_ranger = char_class_is_ranger(char_class);
bool is_force = char_class_is_force(char_class);
if (is_hunter + is_ranger + is_force != 1) {
throw logic_error("char class is not exactly one of the top-level classes");
throw std::logic_error("char class is not exactly one of the top-level classes");
}
if (is_hunter) {
@@ -490,9 +488,9 @@ void player_feed_mag(std::shared_ptr<Client> c, size_t mag_item_index, size_t fe
apply_mag_feed_result(
player->inventory.items[mag_item_index].data,
player->inventory.items[fed_item_index].data,
s->item_parameter_table(c->version()),
s->mag_evolution_table(c->version()),
player->disp.visual.char_class,
player->disp.visual.section_id,
s->data->item_parameter_table(c->version()),
s->data->mag_metadata_table(c->version()),
player->disp.visual.sh.char_class,
player->disp.visual.sh.section_id,
!is_v1_or_v2(c->version()));
}
+1 -1
View File
@@ -19,7 +19,7 @@ void apply_mag_feed_result(
ItemData& mag_item,
const ItemData& fed_item,
std::shared_ptr<const ItemParameterTable> item_parameter_table,
std::shared_ptr<const MagEvolutionTable> mag_evolution_table,
std::shared_ptr<const MagMetadataTable> mag_metadata_table,
uint8_t char_class,
uint8_t section_id,
bool version_has_rare_mags);
+184 -96
View File
@@ -5,14 +5,11 @@
#include <phosg/Filesystem.hh>
#include "CommonFileFormats.hh"
#include "Compression.hh"
#include "PSOEncryption.hh"
using namespace std;
#include "StaticGameData.hh"
void LevelTable::reset_to_base(PlayerStats& stats, uint8_t char_class) const {
stats.level = 0;
stats.experience = 0;
stats.exp = 0;
stats.char_stats = this->base_stats_for_class(char_class);
}
@@ -28,43 +25,111 @@ void LevelTable::advance_to_level(PlayerStats& stats, uint32_t level, uint8_t ch
stats.char_stats.dfp += level_stats.dfp;
stats.char_stats.ata += level_stats.ata;
// Note: It is not a bug that lck is ignored here; the original code ignores it too.
stats.experience = level_stats.experience;
stats.exp = level_stats.exp;
}
}
LevelTableV2::LevelTableV2(const string& data, bool compressed) {
struct Offsets {
// TODO: The overall format of this file on V2 has much more data than we actually use. What's known of the
// structure so far:
le_uint32_t level_deltas; // (5468) -> u32[9] -> LevelStatsDelta[200]
le_uint32_t unknown_a1; // (548C) -> float[6]
le_uint32_t max_stats; // (54A4) -> PlayerStats[9]
le_uint32_t level_100_stats; // (55E8) -> PlayerStats[9]
le_uint32_t base_stats; // (57AC) -> u32[9] -> CharacterStats
le_uint32_t unknown_a2; // (57D0) -> (0x120 zero bytes)
le_uint32_t attack_data; // (58F0) -> AttackData[9]
le_uint32_t unknown_a4; // (5AA0) -> parray<parray<float, 5>, 9>
le_uint32_t unknown_a5; // (5B54) -> float[9]
le_uint32_t unknown_a6; // (5B78) -> (0x30 bytes)
le_uint32_t unknown_a7; // (5BA8) -> (0x2D bytes)
le_uint32_t unknown_a8; // (5E00) -> u32[3] -> float[0x2D]
le_uint32_t unknown_a9; // (5DF4) -> (0x90 bytes)
le_uint32_t unknown_a10; // (60D0) -> u32[3] -> (0x10-byte struct)[0x0C]
le_uint32_t unknown_a11; // (616C) -> u32[3] -> (0x30-bytes)
le_uint32_t unknown_a12; // (64FC) -> u32[3] -> (0x14-byte struct)[0x0F]
} __packed_ws__(Offsets, 0x40);
phosg::StringReader r;
string decompressed_data;
if (compressed) {
decompressed_data = prs_decompress(data);
r = phosg::StringReader(decompressed_data);
} else {
r = phosg::StringReader(data);
phosg::JSON LevelTable::json() const {
auto base_stats_json = phosg::JSON::list();
auto max_stats_json = phosg::JSON::list();
auto level_deltas_json = phosg::JSON::list();
for (size_t char_class = 0; char_class < this->num_char_classes(); char_class++) {
base_stats_json.emplace_back(this->base_stats_for_class(char_class).json());
max_stats_json.emplace_back(this->max_stats_for_class(char_class).json());
auto this_class_level_deltas_json = phosg::JSON::list();
for (size_t level = 0; level < 200; level++) {
this_class_level_deltas_json.emplace_back(this->stats_delta_for_level(char_class, level).json());
}
level_deltas_json.emplace_back(std::move(this_class_level_deltas_json));
}
return phosg::JSON::dict({
{"BaseStats", std::move(base_stats_json)},
{"MaxStats", std::move(max_stats_json)},
{"LevelDeltas", std::move(level_deltas_json)},
});
}
JSONLevelTable::JSONLevelTable(const phosg::JSON& json) {
const auto& base_stats_json = json.at("BaseStats").as_list();
const auto& max_stats_json = json.at("MaxStats").as_list();
const auto& level_deltas_json = json.at("LevelDeltas").as_list();
for (size_t char_class = 0; char_class < base_stats_json.size(); char_class++) {
this->base_stats.emplace_back(CharacterStats::from_json(*base_stats_json.at(char_class)));
this->max_stats.emplace_back(PlayerStats::from_json(*max_stats_json.at(char_class)));
const auto& this_class_level_deltas_json = level_deltas_json.at(char_class)->as_list();
auto& parsed_deltas = this->level_deltas.emplace_back();
for (size_t level = 0; level < 200; level++) {
parsed_deltas[level] = LevelStatsDelta::from_json(*this_class_level_deltas_json.at(level));
}
}
}
size_t JSONLevelTable::num_char_classes() const {
return this->base_stats.size();
}
const CharacterStats& JSONLevelTable::base_stats_for_class(uint8_t char_class) const {
return this->base_stats.at(char_class);
}
const PlayerStats& JSONLevelTable::max_stats_for_class(uint8_t char_class) const {
return this->max_stats.at(char_class);
}
const LevelStatsDelta& JSONLevelTable::stats_delta_for_level(uint8_t char_class, uint8_t level) const {
return this->level_deltas.at(char_class).at(level);
}
LevelTableV2::LevelTableV2(const std::string& data) {
struct Root {
// The overall format of this file on V2 has much more data than we actually use. This table is sorted by the
// offset in the PlayerTable.prs file; note that the offset fields in this structure do not match that order.
// ## OFFS WHAT -> TARGET
// 0008 level_deltas[0] -> LevelStatsDelta[200] (by level) (the rest follow immediately)
// 00 5468 level_deltas -> u32[9] (by char_class)
// 04 548C hp_tp_factors -> HPTPFactors[3] (by char_class_class; [0] = hunter, [1] = ranger, [2] = force)
// 08 54A4 max_stats -> PlayerStats[9] (by char_class)
// 0C 55E8 level_100_stats -> PlayerStats[9] (by char_class)
// 572C base_stats[0] -> CharacterStats
// 10 57AC base_stats -> u32[9] (by char_class)
// 14 57D0 resist_data -> ResistData[9] (by char_class)
// 18 58F0 attack_data -> AttackData[9] (by char_class)
// 1C 5AA0 unknown_a4 -> float[15][3] (by [???][attack_number])
// 20 5B54 unknown_a5 -> float[3][3] (by [strike_number][attack_number])
// 24 5B78 unknown_a6 -> float[3][3] (by [strike_number][attack_number]) (may be [4][3] in original code; there are 0xC zero bytes after)
// 28 5BA8 unknown_a7 -> uint8_t[15][3] (same indexes as unknown_a4)
// 5BD8 unknown_a9[0] -> UnknownA9[15] (index unknown; appears animation-related)
// 30 5DF4 unknown_a9 -> u32[3] (by char_class_class)
// 2C 5E00 area_sound_configs -> AreaSoundConfig[0x12] (by area)
// 5E90 unknown_a10[0] -> (0x10-byte struct)[0x0C] (the rest follow immediately)
// 34 60D0 unknown_a10 -> u32[3] (by char_class_class)
// 60DC unknown_a11[0] -> WeaponReference[12] (the rest follow immediately)
// 38 616C unknown_a11 -> u32[3] (by char_class_class)
// 6178 unknown_a12[0] -> UnknownA12[15] (the rest follow immediately)
// 3C 64FC unknown_a12 -> u32[3] (by char_class_class)
/* 00 / 5468 * */ le_uint32_t level_deltas;
/* 04 / 548C * */ le_uint32_t hp_tp_factors;
/* 08 / 54A4 * */ le_uint32_t max_stats;
/* 0C / 55E8 * */ le_uint32_t level_100_stats;
/* 10 / 57AC * */ le_uint32_t base_stats;
/* 14 / 57D0 * */ le_uint32_t resist_data;
/* 18 / 58F0 * */ le_uint32_t attack_data;
/* 1C / 5AA0 * */ le_uint32_t unknown_a4;
/* 20 / 5B54 * */ le_uint32_t unknown_a5;
/* 24 / 5B78 * */ le_uint32_t unknown_a6;
/* 28 / 5BA8 * */ le_uint32_t unknown_a7;
/* 2C / 5E00 * */ le_uint32_t area_sound_configs;
/* 30 / 5DF4 * */ le_uint32_t unknown_a9;
/* 34 / 60D0 * */ le_uint32_t unknown_a10;
/* 38 / 616C * */ le_uint32_t unknown_a11;
/* 3C / 64FC * */ le_uint32_t unknown_a12;
} __packed_ws__(Root, 0x40);
phosg::StringReader r(data);
const auto& footer = r.pget<RELFileFooter>(r.size() - sizeof(RELFileFooter));
const auto& offsets = r.pget<Offsets>(footer.root_offset);
const auto& offsets = r.pget<Root>(footer.root_offset);
const auto& level_deltas_offsets = r.pget<parray<le_uint32_t, 9>>(offsets.level_deltas);
const auto& base_stats_offsets = r.pget<parray<le_uint32_t, 9>>(offsets.base_stats);
for (size_t char_class = 0; char_class < 9; char_class++) {
@@ -73,17 +138,16 @@ LevelTableV2::LevelTableV2(const string& data, bool compressed) {
this->level_deltas[char_class][level] = src_level_deltas[level];
}
this->max_stats[char_class] = r.pget<PlayerStats>(offsets.max_stats + char_class * sizeof(PlayerStats));
this->level_100_stats[char_class] = r.pget<PlayerStats>(offsets.level_100_stats + char_class * sizeof(PlayerStats));
this->base_stats[char_class] = r.pget<CharacterStats>(base_stats_offsets[char_class]);
}
}
const CharacterStats& LevelTableV2::base_stats_for_class(uint8_t char_class) const {
return this->base_stats.at(char_class);
size_t LevelTableV2::num_char_classes() const {
return 9;
}
const PlayerStats& LevelTableV2::level_100_stats_for_class(uint8_t char_class) const {
return this->level_100_stats.at(char_class);
const CharacterStats& LevelTableV2::base_stats_for_class(uint8_t char_class) const {
return this->base_stats.at(char_class);
}
const PlayerStats& LevelTableV2::max_stats_for_class(uint8_t char_class) const {
@@ -94,47 +158,12 @@ const LevelStatsDelta& LevelTableV2::stats_delta_for_level(uint8_t char_class, u
return this->level_deltas.at(char_class).at(level);
}
LevelTableV3BE::LevelTableV3BE(const string& data, bool encrypted) {
phosg::StringReader r;
string decompressed_data;
if (encrypted) {
auto decrypted = decrypt_pr2_data<true>(data);
decompressed_data = prs_decompress(decrypted.compressed_data);
if (decompressed_data.size() != decrypted.decompressed_size) {
throw runtime_error("decompressed data size does not match expected size");
}
r = phosg::StringReader(decompressed_data);
} else {
r = phosg::StringReader(data);
}
// The GC format is very simple (but everything is big-endian):
// root:
// u32 offset:
// u32[12] offsets:
// LevelStatsDeltaBE[200] level_deltas
const auto& footer = r.pget<RELFileFooterBE>(r.size() - sizeof(RELFileFooterBE));
const auto& offsets = r.pget<parray<be_uint32_t, 12>>(r.pget_u32b(footer.root_offset));
for (size_t char_class = 0; char_class < 12; char_class++) {
const auto& src_deltas = r.pget<parray<LevelStatsDeltaBE, 200>>(offsets[char_class]);
for (size_t level = 0; level < 200; level++) {
const auto& src_delta = src_deltas[level];
auto& dest_delta = this->level_deltas[char_class][level];
dest_delta.atp = src_delta.atp;
dest_delta.mst = src_delta.mst;
dest_delta.evp = src_delta.evp;
dest_delta.hp = src_delta.hp;
dest_delta.dfp = src_delta.dfp;
dest_delta.ata = src_delta.ata;
dest_delta.lck = src_delta.lck;
dest_delta.tp = src_delta.tp;
dest_delta.experience = src_delta.experience;
}
}
size_t LevelTableV3::num_char_classes() const {
return 12;
}
const CharacterStats& LevelTableV3BE::base_stats_for_class(uint8_t char_class) const {
static const array<CharacterStats, 12> data = {
const CharacterStats& LevelTableV3::base_stats_for_class(uint8_t char_class) const {
static const std::array<CharacterStats, 12> data = {
// ATP MST EVP HP DFP ATA LCK
CharacterStats{0x0023, 0x001D, 0x002D, 0x0014, 0x0011, 0x001E, 0x000A},
CharacterStats{0x001E, 0x0028, 0x003C, 0x0013, 0x0016, 0x0019, 0x000A},
@@ -152,7 +181,7 @@ const CharacterStats& LevelTableV3BE::base_stats_for_class(uint8_t char_class) c
return data.at(char_class);
}
static const array<PlayerStats, 12> max_stats_v3_v4 = {
static const std::array<PlayerStats, 12> max_stats_v3_v4 = {
// ATP MST EVP HP DFP ATA LCK ESP PRX PRY L E M
PlayerStats{{0x056B, 0x02DC, 0x02F4, 0x0265, 0x0243, 0x054B, 0x0064}, 0x0064, 0.0f, 0.0f, 0, 0, 0},
PlayerStats{{0x04CB, 0x0499, 0x032B, 0x0254, 0x024D, 0x056C, 0x0064}, 0x0064, 0.0f, 0.0f, 0, 0, 0},
@@ -168,31 +197,86 @@ static const array<PlayerStats, 12> max_stats_v3_v4 = {
PlayerStats{{0x0474, 0x0407, 0x0384, 0x02CF, 0x0241, 0x06C2, 0x0064}, 0x0064, 0.0f, 0.0f, 0, 0, 0},
};
const PlayerStats& LevelTableV3BE::max_stats_for_class(uint8_t char_class) const {
const PlayerStats& LevelTableV3::max_stats_for_class(uint8_t char_class) const {
return max_stats_v3_v4.at(char_class);
}
const LevelStatsDelta& LevelTableV3BE::stats_delta_for_level(uint8_t char_class, uint8_t level) const {
const LevelStatsDelta& LevelTableV3::stats_delta_for_level(uint8_t char_class, uint8_t level) const {
return this->level_deltas.at(char_class).at(level);
}
LevelTableV4::LevelTableV4(const string& data, bool compressed) {
struct Offsets {
le_uint32_t base_stats; // -> u32[12] -> CharacterStats
le_uint32_t level_deltas; // -> u32[12] -> LevelStatsDelta[200]
} __packed_ws__(Offsets, 8);
template <bool BE>
void parse_level_deltas_t(std::array<std::array<LevelStatsDelta, 200>, 12>& deltas, const std::string& data) {
// The V3 format is very simple:
// root:
// u32 offset:
// u32[12] offsets:
// LevelStatsDeltaBE[200] level_deltas
phosg::StringReader r(data);
const auto& footer = r.pget<RELFileFooterT<BE>>(r.size() - sizeof(RELFileFooterT<BE>));
const auto& offsets = r.pget<parray<U32T<BE>, 12>>(r.pget<U32T<BE>>(footer.root_offset));
for (size_t char_class = 0; char_class < 12; char_class++) {
const auto& src_deltas = r.pget<parray<LevelStatsDeltaT<BE>, 200>>(offsets[char_class]);
for (size_t level = 0; level < 200; level++) {
deltas[char_class][level] = src_deltas[level];
}
}
}
phosg::StringReader r;
string decompressed_data;
if (compressed) {
decompressed_data = prs_decompress(data);
r = phosg::StringReader(decompressed_data);
} else {
r = phosg::StringReader(data);
LevelTableGC::LevelTableGC(const std::string& data) {
parse_level_deltas_t<true>(this->level_deltas, data);
}
LevelTableXB::LevelTableXB(const std::string& data) {
parse_level_deltas_t<false>(this->level_deltas, data);
}
struct RootV4 {
le_uint32_t base_stats; // -> u32[12] -> CharacterStats
le_uint32_t level_deltas; // -> u32[12] -> LevelStatsDelta[200]
} __packed_ws__(RootV4, 8);
std::string LevelTable::serialize_binary_v4() const {
RELFileWriter<false> rel;
RootV4 root;
{
std::vector<uint32_t> offsets;
for (size_t char_class = 0; char_class < this->num_char_classes(); char_class++) {
offsets.emplace_back(rel.put<CharacterStats>(this->base_stats_for_class(char_class)));
}
root.base_stats = rel.w.size();
for (uint32_t offset : offsets) {
rel.write_offset(offset);
}
}
{
std::vector<uint32_t> offsets;
for (size_t char_class = 0; char_class < this->num_char_classes(); char_class++) {
offsets.emplace_back(rel.w.size());
for (size_t level = 0; level < 200; level++) {
rel.put<LevelStatsDelta>(this->stats_delta_for_level(char_class, level));
}
}
root.level_deltas = rel.w.size();
for (uint32_t offset : offsets) {
rel.write_offset(offset);
}
}
size_t root_offset = rel.put<RootV4>(root);
rel.relocations.emplace(root_offset);
rel.relocations.emplace(root_offset + 4);
return rel.finalize(root_offset);
}
LevelTableV4::LevelTableV4(const std::string& data) {
phosg::StringReader r(data);
const auto& footer = r.pget<RELFileFooter>(r.size() - sizeof(RELFileFooter));
const auto& offsets = r.pget<Offsets>(footer.root_offset);
const auto& offsets = r.pget<RootV4>(footer.root_offset);
const auto& level_deltas_offsets = r.pget<parray<le_uint32_t, 12>>(offsets.level_deltas);
const auto& base_stats_offsets = r.pget<parray<le_uint32_t, 12>>(offsets.base_stats);
for (size_t char_class = 0; char_class < 12; char_class++) {
@@ -204,6 +288,10 @@ LevelTableV4::LevelTableV4(const string& data, bool compressed) {
}
}
size_t LevelTableV4::num_char_classes() const {
return 12;
}
const CharacterStats& LevelTableV4::base_stats_for_class(uint8_t char_class) const {
return this->base_stats.at(char_class);
}
+169 -42
View File
@@ -21,17 +21,34 @@ struct CharacterStatsT {
/* 0A */ U16T<BE> ata = 0;
/* 0C */ U16T<BE> lck = 0;
/* 0E */
static CharacterStatsT<BE> from_json(const phosg::JSON& json) {
return CharacterStatsT<BE>{
json.at("ATP").as_int(),
json.at("MST").as_int(),
json.at("EVP").as_int(),
json.at("HP").as_int(),
json.at("DFP").as_int(),
json.at("ATA").as_int(),
json.at("LCK").as_int()};
}
phosg::JSON json() const {
return phosg::JSON::dict({{"ATP", this->atp.load()},
{"MST", this->mst.load()},
{"EVP", this->evp.load()},
{"HP", this->hp.load()},
{"DFP", this->dfp.load()},
{"ATA", this->ata.load()},
{"LCK", this->lck.load()}});
}
operator CharacterStatsT<!BE>() const {
CharacterStatsT<!BE> ret;
ret.atp = this->atp;
ret.mst = this->mst;
ret.evp = this->evp;
ret.hp = this->hp;
ret.dfp = this->dfp;
ret.ata = this->ata;
ret.lck = this->lck;
return ret;
return CharacterStatsT<!BE>{
this->atp.load(),
this->mst.load(),
this->evp.load(),
this->hp.load(),
this->dfp.load(),
this->ata.load(),
this->lck.load()};
}
} __packed_ws_be__(CharacterStatsT, 0x0E);
using CharacterStats = CharacterStatsT<false>;
@@ -44,37 +61,82 @@ struct PlayerStatsT {
/* 10 */ F32T<BE> attack_range = 0.0;
/* 14 */ F32T<BE> knockback_range = 0.0;
/* 18 */ U32T<BE> level = 0; // Qedit specifies this as tech level when used for enemies
/* 1C */ U32T<BE> experience = 0;
/* 1C */ U32T<BE> exp = 0;
/* 20 */ U32T<BE> meseta = 0; // Qedit specifies this as TP when used for enemies
/* 24 */
operator PlayerStatsT<!BE>() const {
PlayerStatsT<!BE> ret;
ret.char_stats = this->char_stats;
ret.esp = this->esp;
ret.attack_range = this->attack_range;
ret.knockback_range = this->knockback_range;
ret.level = this->level;
ret.experience = this->experience;
ret.meseta = this->meseta;
static PlayerStatsT<BE> from_json(const phosg::JSON& json) {
return PlayerStatsT<BE>{
CharacterStatsT<BE>::from_json(json),
json.at("ESP").as_int(),
json.at("AttackRange").as_float(),
json.at("KnockbackRange").as_float(),
json.at("Level").as_int(),
json.at("EXP").as_int(),
json.at("Meseta").as_int()};
}
phosg::JSON json() const {
auto ret = this->char_stats.json();
ret.emplace("ESP", this->esp.load());
ret.emplace("AttackRange", this->attack_range.load());
ret.emplace("KnockbackRange", this->knockback_range.load());
ret.emplace("Level", this->level.load());
ret.emplace("EXP", this->exp.load());
ret.emplace("Meseta", this->meseta.load());
return ret;
}
operator PlayerStatsT<!BE>() const {
return PlayerStatsT<!BE>{
this->char_stats,
this->esp.load(),
this->attack_range.load(),
this->knockback_range.load(),
this->level.load(),
this->exp.load(),
this->meseta.load()};
}
} __packed_ws_be__(PlayerStatsT, 0x24);
using PlayerStats = PlayerStatsT<false>;
using PlayerStatsBE = PlayerStatsT<true>;
template <bool BE>
struct LevelStatsDeltaT {
/* 00 */ uint8_t atp;
/* 01 */ uint8_t mst;
/* 02 */ uint8_t evp;
/* 03 */ uint8_t hp;
/* 04 */ uint8_t dfp;
/* 05 */ uint8_t ata;
/* 06 */ uint8_t lck;
/* 07 */ uint8_t tp;
/* 08 */ U32T<BE> experience;
/* 00 */ uint8_t atp = 0;
/* 01 */ uint8_t mst = 0;
/* 02 */ uint8_t evp = 0;
/* 03 */ uint8_t hp = 0;
/* 04 */ uint8_t dfp = 0;
/* 05 */ uint8_t ata = 0;
/* 06 */ uint8_t lck = 0;
/* 07 */ uint8_t tp = 0;
/* 08 */ U32T<BE> exp = 0;
/* 0C */
static LevelStatsDeltaT<BE> from_json(const phosg::JSON& json) {
return LevelStatsDeltaT<BE>{
static_cast<uint8_t>(json.at("ATP").as_int()),
static_cast<uint8_t>(json.at("MST").as_int()),
static_cast<uint8_t>(json.at("EVP").as_int()),
static_cast<uint8_t>(json.at("HP").as_int()),
static_cast<uint8_t>(json.at("DFP").as_int()),
static_cast<uint8_t>(json.at("ATA").as_int()),
static_cast<uint8_t>(json.at("LCK").as_int()),
static_cast<uint8_t>(json.at("TP").as_int()),
static_cast<uint32_t>(json.at("EXP").as_int())};
}
phosg::JSON json() const {
return phosg::JSON::dict({{"ATP", this->atp},
{"MST", this->mst},
{"EVP", this->evp},
{"HP", this->hp},
{"DFP", this->dfp},
{"ATA", this->ata},
{"LCK", this->lck},
{"TP", this->tp},
{"EXP", this->exp.load()}});
}
operator LevelStatsDeltaT<!BE>() const {
return LevelStatsDeltaT<!BE>{
this->atp, this->mst, this->evp, this->hp, this->dfp, this->ata, this->lck, this->tp, this->exp.load()};
}
void apply(CharacterStats& ps) const {
ps.ata += this->ata;
@@ -92,9 +154,11 @@ using LevelStatsDeltaBE = LevelStatsDeltaT<true>;
class LevelTable {
// This is the base class for all the LevelTable implementations. The public interface here only defines functions
// that the server needs to handle requests, but some subclasses implement more functionality. See the comments and
// Offsets structures inside the subclasses' constructor implementations for more details on the file formats.
// Root structures inside the subclasses' constructor implementations for more details on the file formats.
public:
virtual ~LevelTable() = default;
virtual size_t num_char_classes() const = 0;
virtual const CharacterStats& base_stats_for_class(uint8_t char_class) const = 0;
virtual const PlayerStats& max_stats_for_class(uint8_t char_class) const = 0;
virtual const LevelStatsDelta& stats_delta_for_level(uint8_t char_class, uint8_t level) const = 0;
@@ -102,50 +166,113 @@ public:
void reset_to_base(PlayerStats& stats, uint8_t char_class) const;
void advance_to_level(PlayerStats& stats, uint32_t level, uint8_t char_class) const;
std::string serialize_binary_v4() const;
phosg::JSON json() const;
protected:
LevelTable() = default;
};
class LevelTableV2 : public LevelTable { // from PlayerTable.prs (PC)
class JSONLevelTable : public LevelTable {
public:
LevelTableV2(const std::string& data, bool compressed);
virtual ~LevelTableV2() = default;
JSONLevelTable(const phosg::JSON& json);
virtual ~JSONLevelTable() = default;
virtual size_t num_char_classes() const;
virtual const CharacterStats& base_stats_for_class(uint8_t char_class) const;
const PlayerStats& level_100_stats_for_class(uint8_t char_class) const;
virtual const PlayerStats& max_stats_for_class(uint8_t char_class) const;
virtual const LevelStatsDelta& stats_delta_for_level(uint8_t char_class, uint8_t level) const;
private:
std::vector<CharacterStats> base_stats;
std::vector<PlayerStats> max_stats;
std::vector<std::array<LevelStatsDelta, 200>> level_deltas;
};
class LevelTableV2 : public LevelTable { // from PlayerTable.prs (PC)
public:
struct HPTPFactors {
le_float hp_factor;
le_float tp_factor;
} __packed_ws__(HPTPFactors, 8);
struct UnknownA9 {
le_float unknown_a1;
le_float unknown_a2;
le_float unknown_a3;
} __packed_ws__(UnknownA9, 0x0C);
struct AreaSoundConfig {
le_uint16_t step_sound;
le_uint16_t grass_step_sound;
le_uint16_t water_step_sound;
parray<uint8_t, 2> unused;
} __packed_ws__(AreaSoundConfig, 8);
struct WeaponReference {
le_uint16_t data1_1;
le_uint16_t data1_2;
} __packed_ws__(WeaponReference, 4);
struct UnknownA12 {
le_float unknown_a1;
le_float unknown_a2;
le_float unknown_a3;
le_float unknown_a4;
le_uint32_t unknown_a5;
} __packed_ws__(UnknownA12, 0x14);
explicit LevelTableV2(const std::string& data);
virtual ~LevelTableV2() = default;
virtual size_t num_char_classes() const;
virtual const CharacterStats& base_stats_for_class(uint8_t char_class) const;
virtual const PlayerStats& max_stats_for_class(uint8_t char_class) const;
virtual const LevelStatsDelta& stats_delta_for_level(uint8_t char_class, uint8_t level) const;
protected:
std::array<CharacterStats, 9> base_stats;
std::array<PlayerStats, 9> level_100_stats;
std::array<PlayerStats, 9> max_stats;
std::array<std::array<LevelStatsDelta, 200>, 9> level_deltas;
};
class LevelTableV3BE : public LevelTable { // from PlyLevelTbl.cpt (GC)
class LevelTableV3 : public LevelTable { // from PlyLevelTbl.cpt (GC/XB)
public:
LevelTableV3BE(const std::string& data, bool encrypted);
virtual ~LevelTableV3BE() = default;
virtual ~LevelTableV3() = default;
virtual size_t num_char_classes() const;
virtual const CharacterStats& base_stats_for_class(uint8_t char_class) const;
virtual const PlayerStats& max_stats_for_class(uint8_t char_class) const;
virtual const LevelStatsDelta& stats_delta_for_level(uint8_t char_class, uint8_t level) const;
private:
protected:
LevelTableV3() = default;
std::array<std::array<LevelStatsDelta, 200>, 12> level_deltas;
};
class LevelTableGC : public LevelTableV3 {
public:
explicit LevelTableGC(const std::string& data);
virtual ~LevelTableGC() = default;
};
class LevelTableXB : public LevelTableV3 {
public:
explicit LevelTableXB(const std::string& data);
virtual ~LevelTableXB() = default;
};
class LevelTableV4 : public LevelTable { // from PlyLevelTbl.prs (BB)
public:
LevelTableV4(const std::string& data, bool compressed);
explicit LevelTableV4(const std::string& data);
virtual ~LevelTableV4() = default;
virtual size_t num_char_classes() const;
virtual const CharacterStats& base_stats_for_class(uint8_t char_class) const;
virtual const PlayerStats& max_stats_for_class(uint8_t char_class) const;
virtual const LevelStatsDelta& stats_delta_for_level(uint8_t char_class, uint8_t level) const;
private:
protected:
std::array<CharacterStats, 12> base_stats;
std::array<std::array<LevelStatsDelta, 200>, 12> level_deltas;
};
+84 -73
View File
@@ -9,8 +9,7 @@
#include "SendCommands.hh"
#include "ServerState.hh"
#include "Text.hh"
using namespace std;
#include "BrutalPeeps.hh"
bool Lobby::FloorItem::visible_to_client(uint8_t client_id) const {
return this->flags & (1 << client_id);
@@ -24,17 +23,17 @@ bool Lobby::FloorItemManager::exists(uint32_t item_id) const {
return this->items.count(item_id);
}
shared_ptr<Lobby::FloorItem> Lobby::FloorItemManager::find(uint32_t item_id) const {
std::shared_ptr<Lobby::FloorItem> Lobby::FloorItemManager::find(uint32_t item_id) const {
return this->items.at(item_id);
}
void Lobby::FloorItemManager::add(
const ItemData& item,
const VectorXZF& pos,
shared_ptr<const MapState::ObjectState> from_obj,
shared_ptr<const MapState::EnemyState> from_ene,
std::shared_ptr<const MapState::ObjectState> from_obj,
std::shared_ptr<const MapState::EnemyState> from_ene,
uint16_t flags) {
auto fi = make_shared<FloorItem>();
auto fi = std::make_shared<FloorItem>();
fi->data = item;
fi->pos = pos;
fi->drop_number = this->next_drop_number++;
@@ -44,14 +43,14 @@ void Lobby::FloorItemManager::add(
this->add(fi);
}
void Lobby::FloorItemManager::add(shared_ptr<Lobby::FloorItem> fi) {
void Lobby::FloorItemManager::add(std::shared_ptr<Lobby::FloorItem> fi) {
if (fi->flags == 0) {
throw logic_error("floor item is not visible to any player");
throw std::logic_error("floor item is not visible to any player");
}
auto emplace_ret = this->items.emplace(fi->data.id, fi);
if (!emplace_ret.second) {
throw runtime_error("floor item already exists with the same ID");
throw std::runtime_error("floor item already exists with the same ID");
}
for (size_t z = 0; z < 12; z++) {
if (fi->visible_to_client(z)) {
@@ -65,15 +64,15 @@ void Lobby::FloorItemManager::add(shared_ptr<Lobby::FloorItem> fi) {
std::shared_ptr<Lobby::FloorItem> Lobby::FloorItemManager::remove(uint32_t item_id, uint8_t client_id) {
auto item_it = this->items.find(item_id);
if (item_it == this->items.end()) {
throw out_of_range("item not present");
throw std::out_of_range("item not present");
}
auto fi = item_it->second;
if ((client_id != 0xFF) && !fi->visible_to_client(client_id)) {
throw runtime_error("client does not have access to item");
throw std::runtime_error("client does not have access to item");
}
for (size_t z = 0; z < 12; z++) {
if (fi->visible_to_client(z) && !this->queue_for_client[z].erase(fi->drop_number)) {
throw logic_error("item queue for client is inconsistent");
throw std::logic_error("item queue for client is inconsistent");
}
}
this->items.erase(item_it);
@@ -83,7 +82,7 @@ std::shared_ptr<Lobby::FloorItem> Lobby::FloorItemManager::remove(uint32_t item_
}
std::unordered_set<std::shared_ptr<Lobby::FloorItem>> Lobby::FloorItemManager::evict() {
unordered_set<shared_ptr<FloorItem>> ret;
std::unordered_set<std::shared_ptr<FloorItem>> ret;
for (size_t z = 0; z < 12; z++) {
while (this->queue_for_client[z].size() > 48) {
ret.emplace(this->remove(this->queue_for_client[z].begin()->second->data.id, 0xFF));
@@ -94,7 +93,7 @@ std::unordered_set<std::shared_ptr<Lobby::FloorItem>> Lobby::FloorItemManager::e
}
void Lobby::FloorItemManager::clear_inaccessible(uint16_t remaining_clients_mask) {
unordered_set<uint32_t> item_ids_to_delete;
std::unordered_set<uint32_t> item_ids_to_delete;
for (const auto& it : this->items) {
if ((it.second->flags & remaining_clients_mask) == 0) {
item_ids_to_delete.emplace(it.first);
@@ -107,7 +106,7 @@ void Lobby::FloorItemManager::clear_inaccessible(uint16_t remaining_clients_mask
}
void Lobby::FloorItemManager::clear_private() {
unordered_set<uint32_t> item_ids_to_delete;
std::unordered_set<uint32_t> item_ids_to_delete;
for (const auto& it : this->items) {
if ((it.second->flags & 0x00F) != 0x00F) {
item_ids_to_delete.emplace(it.first);
@@ -130,7 +129,7 @@ void Lobby::FloorItemManager::clear() {
}
uint32_t Lobby::FloorItemManager::reassign_all_item_ids(uint32_t next_item_id) {
::map<uint32_t, shared_ptr<FloorItem>> old_items;
std::map<uint32_t, std::shared_ptr<FloorItem>> old_items;
old_items.swap(this->items);
for (auto& queue : this->queue_for_client) {
queue.clear();
@@ -142,13 +141,13 @@ uint32_t Lobby::FloorItemManager::reassign_all_item_ids(uint32_t next_item_id) {
return next_item_id;
}
Lobby::Lobby(shared_ptr<ServerState> s, uint32_t id, bool is_game)
Lobby::Lobby(std::shared_ptr<ServerState> s, uint32_t id, bool is_game)
: server_state(s),
log(std::format("[{}:{:X}] ", is_game ? "Game" : "Lobby", id), lobby_log.min_level),
creation_time(phosg::now()),
lobby_id(id),
random_seed(phosg::random_object<uint32_t>()),
rand_crypt(make_shared<DisabledRandomGenerator>()),
rand_crypt(std::make_shared<DisabledRandomGenerator>()),
drop_mode(ServerDropMode::CLIENT),
idle_timeout_timer(*s->io_context) {
this->log.info_f("Created");
@@ -174,21 +173,21 @@ uint8_t Lobby::area_for_floor(Version version, uint8_t floor) const {
if (this->quest) {
return this->quest->meta.floor_assignments.at(floor).area;
}
auto sdt = this->require_server_state()->set_data_table(version, this->episode, this->mode, this->difficulty);
auto sdt = this->require_server_state()->data->set_data_table(version, this->episode, this->mode, this->difficulty);
return sdt->default_floor_to_area(this->episode).at(floor);
}
shared_ptr<ServerState> Lobby::require_server_state() const {
std::shared_ptr<ServerState> Lobby::require_server_state() const {
auto s = this->server_state.lock();
if (!s) {
throw logic_error("server is deleted");
throw std::logic_error("server is deleted");
}
return s;
}
shared_ptr<Lobby::ChallengeParameters> Lobby::require_challenge_params() const {
std::shared_ptr<Lobby::ChallengeParameters> Lobby::require_challenge_params() const {
if (!this->challenge_params) {
throw runtime_error("challenge params are missing");
throw std::runtime_error("challenge params are missing");
}
return this->challenge_params;
}
@@ -206,34 +205,38 @@ void Lobby::create_item_creator(Version logic_version) {
logic_version = leader_c ? leader_c->version() : Version::BB_V4;
}
shared_ptr<RandomGenerator> rand_crypt;
std::shared_ptr<RandomGenerator> rand_crypt;
if (s->use_psov2_rand_crypt) {
rand_crypt = make_shared<PSOV2Encryption>(this->rand_crypt->seed());
rand_crypt = std::make_shared<PSOV2Encryption>(this->rand_crypt->seed());
} else {
rand_crypt = make_shared<MT19937Generator>(this->rand_crypt->seed());
rand_crypt = std::make_shared<MT19937Generator>(this->rand_crypt->seed());
}
uint8_t effective_section_id = this->effective_section_id();
if (effective_section_id >= 10) {
effective_section_id = 0x00;
}
this->item_creator = make_shared<ItemCreator>(
s->common_item_set(logic_version, this->quest),
s->rare_item_set(logic_version, this->quest),
s->armor_random_set,
s->tool_random_set,
s->weapon_random_set(this->difficulty),
s->tekker_adjustment_set,
s->item_parameter_table(logic_version),
s->item_stack_limits(logic_version),
this->item_creator = std::make_shared<ItemCreator>(
s->data->common_item_set(logic_version, this->quest),
s->data->rare_item_set(logic_version, this->quest),
s->data->armor_random_set,
s->data->tool_random_set,
s->data->weapon_random_set(this->difficulty),
s->data->tekker_adjustment_set,
s->data->item_parameter_table(logic_version),
s->data->item_stack_limits(logic_version),
(this->mode == GameMode::SOLO) ? GameMode::NORMAL : this->mode,
this->difficulty,
effective_section_id,
rand_crypt,
this->quest ? this->quest->meta.battle_rules : nullptr);
if (this->blueballz_tier >= 0) {
double rare_mult = 1.25 + (static_cast<double>(this->blueballz_tier) * 0.25);
this->item_creator->set_rare_drop_rate_multiplier(rare_mult);
this->log.info_f("Blueballz +{} rare drop rate multiplier set to {:g}x", this->blueballz_tier, rare_mult);
if (this->brutal_peeps_tier >= 0) {
const auto* brutal_peeps_def = brutal_peeps_tier_definition(this->brutal_peeps_tier);
if (brutal_peeps_def) {
this->item_creator->set_rare_drop_rate_multiplier(brutal_peeps_def->rare_drop_multiplier);
this->log.info_f("Brutal Peeps +{} rare drop rate multiplier set to {:g}x",
this->brutal_peeps_tier,
brutal_peeps_def->rare_drop_multiplier);
}
}
if (s->use_legacy_item_random_behavior) {
this->item_creator->set_legacy_replay();
@@ -249,7 +252,7 @@ uint8_t Lobby::effective_section_id() const {
}
auto leader = this->clients.at(this->leader_id);
if (leader) {
return leader->character_file()->disp.visual.section_id;
return leader->character_file()->disp.visual.sh.section_id;
}
return 0xFF;
}
@@ -279,13 +282,13 @@ void Lobby::load_maps() {
if (this->quest) {
this->log.info_f("Loading quest supermap");
auto supermap = this->quest->get_supermap(this->random_seed);
this->map_state = make_shared<MapState>(
this->map_state = std::make_shared<MapState>(
this->lobby_id, this->difficulty, this->event, this->random_seed, this->rare_enemy_rates, this->rand_crypt, supermap);
} else {
this->log.info_f("Loading free play supermaps");
auto s = this->require_server_state();
auto supermaps = s->supermaps_for_variations(this->episode, this->mode, this->difficulty, this->variations);
this->map_state = make_shared<MapState>(
auto supermaps = s->data->supermaps_for_variations(this->episode, this->mode, this->difficulty, this->variations);
this->map_state = std::make_shared<MapState>(
this->lobby_id, this->difficulty, this->event, this->random_seed, this->rare_enemy_rates, this->rand_crypt, supermaps);
}
@@ -314,13 +317,13 @@ void Lobby::create_ep3_server() {
bool is_nte = this->is_ep3_nte();
Episode3::Server::Options options = {
.card_index = is_nte ? s->ep3_card_index_trial : s->ep3_card_index,
.map_index = s->ep3_map_index,
.behavior_flags = s->ep3_behavior_flags,
.card_index = is_nte ? s->data->ep3_card_index_trial : s->data->ep3_card_index,
.map_index = s->data->ep3_map_index,
.behavior_flags = s->data->ep3_behavior_flags,
.opt_rand_stream = nullptr,
.rand_crypt = this->rand_crypt,
.tournament = tourn,
.trap_card_ids = s->ep3_trap_card_ids,
.trap_card_ids = s->data->ep3_trap_card_ids,
.output_queue = nullptr,
};
if (is_nte) {
@@ -328,7 +331,7 @@ void Lobby::create_ep3_server() {
} else {
options.behavior_flags &= (~Episode3::BehaviorFlag::IS_TRIAL_EDITION);
}
this->ep3_server = make_shared<Episode3::Server>(this->shared_from_this(), std::move(options));
this->ep3_server = std::make_shared<Episode3::Server>(this->shared_from_this(), std::move(options));
this->ep3_server->init();
}
@@ -386,9 +389,9 @@ bool Lobby::any_v1_clients_present() const {
return false;
}
void Lobby::add_client(shared_ptr<Client> c, ssize_t required_client_id) {
void Lobby::add_client(std::shared_ptr<Client> c, ssize_t required_client_id) {
if (!c->login) {
throw runtime_error("client is not logged in");
throw std::runtime_error("client is not logged in");
}
ssize_t index;
@@ -396,7 +399,7 @@ void Lobby::add_client(shared_ptr<Client> c, ssize_t required_client_id) {
if (required_client_id >= 0) {
if (this->clients.at(required_client_id).get()) {
throw out_of_range("required slot is in use");
throw std::out_of_range("required slot is in use");
}
this->clients[required_client_id] = c;
index = required_client_id;
@@ -409,7 +412,7 @@ void Lobby::add_client(shared_ptr<Client> c, ssize_t required_client_id) {
}
}
if (index < min_client_id) {
throw out_of_range("no space left in lobby");
throw std::out_of_range("no space left in lobby");
}
} else {
for (index = min_client_id; index < this->max_clients; index++) {
@@ -419,7 +422,7 @@ void Lobby::add_client(shared_ptr<Client> c, ssize_t required_client_id) {
}
}
if (index >= this->max_clients) {
throw out_of_range("no space left in lobby");
throw std::out_of_range("no space left in lobby");
}
}
@@ -466,11 +469,11 @@ void Lobby::add_client(shared_ptr<Client> c, ssize_t required_client_id) {
PlayerLobbyDataDCGC lobby_data;
lobby_data.player_tag = 0x00010000;
lobby_data.guild_card_number = c->login->account->account_id;
lobby_data.name.encode(p->disp.name.decode(c->language()), c->language());
lobby_data.name.encode(p->disp.visual.name.decode(c->language()), c->language());
this->battle_record->add_player(
lobby_data,
p->inventory,
p->disp.to_dcpcv3<false>(c->language(), c->language()),
p->disp.to_v123<false>(c->language(), c->language()),
c->ep3_config ? (c->ep3_config->online_clv_exp / 100) : 0);
}
@@ -492,10 +495,10 @@ void Lobby::add_client(shared_ptr<Client> c, ssize_t required_client_id) {
}
}
void Lobby::remove_client(shared_ptr<Client> c) {
void Lobby::remove_client(std::shared_ptr<Client> c) {
if (this->clients.at(c->lobby_client_id) != c) {
auto other_c = this->clients[c->lobby_client_id].get();
throw logic_error(std::format(
throw std::logic_error(std::format(
"client\'s lobby client id ({}) does not match client list ({})",
c->lobby_client_id,
static_cast<uint8_t>(other_c ? other_c->lobby_client_id : 0xFF)));
@@ -568,20 +571,20 @@ void Lobby::remove_client(shared_ptr<Client> c) {
}
}
void Lobby::move_client_to_lobby(shared_ptr<Lobby> dest_lobby, shared_ptr<Client> c, ssize_t required_client_id) {
void Lobby::move_client_to_lobby(std::shared_ptr<Lobby> dest_lobby, std::shared_ptr<Client> c, ssize_t required_client_id) {
if (dest_lobby.get() == this) {
return;
}
if (required_client_id >= 0) {
if (dest_lobby->clients.at(required_client_id)) {
throw out_of_range("required slot is in use");
throw std::out_of_range("required slot is in use");
}
} else {
ssize_t min_client_id = this->check_flag(Lobby::Flag::IS_SPECTATOR_TEAM) ? 4 : 0;
size_t available_slots = dest_lobby->max_clients - min_client_id;
if (dest_lobby->count_clients() >= available_slots) {
throw out_of_range("no space left in lobby");
throw std::out_of_range("no space left in lobby");
}
}
@@ -589,7 +592,7 @@ void Lobby::move_client_to_lobby(shared_ptr<Lobby> dest_lobby, shared_ptr<Client
dest_lobby->add_client(c, required_client_id);
}
shared_ptr<Client> Lobby::find_client(const string* identifier, uint64_t account_id) {
std::shared_ptr<Client> Lobby::find_client(const std::string* identifier, uint64_t account_id) {
for (size_t x = 0; x < this->max_clients; x++) {
auto lc = this->clients[x];
if (!lc) {
@@ -598,12 +601,12 @@ shared_ptr<Client> Lobby::find_client(const string* identifier, uint64_t account
if (account_id && lc->login && (lc->login->account->account_id == account_id)) {
return lc;
}
if (identifier && (lc->character_file()->disp.name.eq(*identifier, lc->language()))) {
if (identifier && (lc->character_file()->disp.visual.name.eq(*identifier, lc->language()))) {
return lc;
}
}
throw out_of_range("client not found");
throw std::out_of_range("client not found");
}
Lobby::JoinError Lobby::join_error_for_client(std::shared_ptr<Client> c, const std::string* password) const {
@@ -615,6 +618,14 @@ Lobby::JoinError Lobby::join_error_for_client(std::shared_ptr<Client> c, const s
return JoinError::VERSION_CONFLICT;
}
if (this->is_game()) {
// Brutal Peeps rooms rely on version-specific BattleParam patching.
// BB Brutal rooms are BB-only; PC Brutal rooms are PC V2-only.
if ((this->brutal_peeps_tier >= 1) &&
((this->version_is_allowed(Version::BB_V4) && (c->version() != Version::BB_V4)) ||
(this->version_is_allowed(Version::PC_V2) && (c->version() != Version::PC_V2)))) {
return JoinError::VERSION_CONFLICT;
}
if (this->check_flag(Flag::QUEST_SELECTION_IN_PROGRESS)) {
return JoinError::QUEST_SELECTION_IN_PROGRESS;
}
@@ -667,7 +678,7 @@ bool Lobby::item_exists(uint8_t floor, uint32_t item_id) const {
return this->floor_item_managers.at(floor).exists(item_id);
}
shared_ptr<Lobby::FloorItem> Lobby::find_item(uint8_t floor, uint32_t item_id) const {
std::shared_ptr<Lobby::FloorItem> Lobby::find_item(uint8_t floor, uint32_t item_id) const {
return this->floor_item_managers.at(floor).find(item_id);
}
@@ -683,7 +694,7 @@ void Lobby::add_item(
this->evict_items_from_floor(floor);
}
void Lobby::add_item(uint8_t floor, shared_ptr<FloorItem> fi) {
void Lobby::add_item(uint8_t floor, std::shared_ptr<FloorItem> fi) {
auto& m = this->floor_item_managers.at(floor);
m.add(fi);
this->evict_items_from_floor(floor);
@@ -705,7 +716,7 @@ void Lobby::evict_items_from_floor(uint8_t floor) {
}
}
shared_ptr<Lobby::FloorItem> Lobby::remove_item(uint8_t floor, uint32_t item_id, uint8_t requesting_client_id) {
std::shared_ptr<Lobby::FloorItem> Lobby::remove_item(uint8_t floor, uint32_t item_id, uint8_t requesting_client_id) {
return this->floor_item_managers.at(floor).remove(item_id, requesting_client_id);
}
@@ -726,7 +737,7 @@ void Lobby::on_item_id_generated_externally(uint32_t item_id) {
}
}
void Lobby::assign_inventory_and_bank_item_ids(shared_ptr<Client> c, bool consume_ids) {
void Lobby::assign_inventory_and_bank_item_ids(std::shared_ptr<Client> c, bool consume_ids) {
auto p = c->character_file();
uint32_t orig_next_item_id = this->next_item_id_for_client.at(c->lobby_client_id);
for (size_t z = 0; z < p->inventory.num_items; z++) {
@@ -751,8 +762,8 @@ void Lobby::assign_inventory_and_bank_item_ids(shared_ptr<Client> c, bool consum
}
}
unordered_map<uint32_t, shared_ptr<Client>> Lobby::clients_by_account_id() const {
unordered_map<uint32_t, shared_ptr<Client>> ret;
std::unordered_map<uint32_t, std::shared_ptr<Client>> Lobby::clients_by_account_id() const {
std::unordered_map<uint32_t, std::shared_ptr<Client>> ret;
for (auto c : this->clients) {
if (c && c->login) {
ret.emplace(c->login->account->account_id, c);
@@ -764,7 +775,7 @@ unordered_map<uint32_t, shared_ptr<Client>> Lobby::clients_by_account_id() const
QuestIndex::IncludeCondition Lobby::quest_include_condition() const {
size_t num_players = this->count_clients();
bool v1_present = this->any_v1_clients_present();
return [this, num_players, v1_present](shared_ptr<const Quest> q) -> QuestIndex::IncludeState {
return [this, num_players, v1_present](std::shared_ptr<const Quest> q) -> QuestIndex::IncludeState {
bool is_enabled = true;
for (const auto& lc : this->clients) {
auto this_sh = this->shared_from_this();
@@ -779,7 +790,7 @@ QuestIndex::IncludeCondition Lobby::quest_include_condition() const {
};
}
bool Lobby::compare_shared(const shared_ptr<const Lobby>& a, const shared_ptr<const Lobby>& b) {
bool Lobby::compare_shared(const std::shared_ptr<const Lobby>& a, const std::shared_ptr<const Lobby>& b) {
// Sort keys:
// 1. Priority class: has free space < empty (persistent) < full < non-joinable (in quest/battle)
// 2. Password: public < locked
@@ -787,7 +798,7 @@ bool Lobby::compare_shared(const shared_ptr<const Lobby>& a, const shared_ptr<co
// 4. Episode: 1 < 2 < 4
// 5. Difficulty: Normal < Hard < Very Hard < Ultimate
// 6. Game name
static auto get_priority = +[](const shared_ptr<const Lobby>& l) -> size_t {
static auto get_priority = +[](const std::shared_ptr<const Lobby>& l) -> size_t {
if (l->check_flag(Lobby::Flag::QUEST_SELECTION_IN_PROGRESS) ||
l->check_flag(Lobby::Flag::QUEST_IN_PROGRESS) ||
l->check_flag(Lobby::Flag::BATTLE_IN_PROGRESS)) {
@@ -869,6 +880,6 @@ const char* phosg::name_for_enum<Lobby::JoinError>(Lobby::JoinError value) {
case Lobby::JoinError::NO_ACCESS_TO_QUEST:
return "NO_ACCESS_TO_QUEST";
default:
throw runtime_error("invalid drop mode");
throw std::runtime_error("invalid drop mode");
}
}
+4 -4
View File
@@ -20,7 +20,7 @@
#include "StaticGameData.hh"
#include "Text.hh"
struct ServerState;
class ServerState;
struct Lobby : public std::enable_shared_from_this<Lobby> {
struct FloorItem {
@@ -41,7 +41,7 @@ struct Lobby : public std::enable_shared_from_this<Lobby> {
struct FloorItemManager {
phosg::PrefixedLogger log;
uint64_t next_drop_number;
// It's important that this is a map and not an unordered_map. See the comment in send_game_item_state for details.
// It's important that this is a map and not an std::unordered_map. See the comment in send_game_item_state for details.
std::map<uint32_t, std::shared_ptr<FloorItem>> items; // Keyed on item_id
std::array<std::map<uint64_t, std::shared_ptr<FloorItem>>, 12> queue_for_client;
@@ -81,7 +81,7 @@ struct Lobby : public std::enable_shared_from_this<Lobby> {
START_BATTLE_PLAYER_IMMEDIATELY = 0x00010000,
CANNOT_CHANGE_CHEAT_MODE = 0x00020000,
USE_CREATOR_SECTION_ID = 0x00040000,
BLUEBALLZ_PLUS0 = 0x00080000,
BRUTAL_PEEPS_MODE = 0x00080000,
// Flags used only for lobbies
PUBLIC = 0x01000000,
DEFAULT = 0x02000000,
@@ -119,7 +119,7 @@ struct Lobby : public std::enable_shared_from_this<Lobby> {
Episode episode = Episode::NONE;
GameMode mode = GameMode::NORMAL;
Difficulty difficulty = Difficulty::NORMAL;
int8_t blueballz_tier = -1; // -1 = disabled; 0..10 = Blueballz +0..+10
int8_t brutal_peeps_tier = -1; // -1 = disabled; 1..11 = Brutal Peeps +1..+11
float base_exp_multiplier = 1.0f;
float exp_share_multiplier = 0.5f;
float challenge_exp_multiplier = 1.0f;
+5 -7
View File
@@ -2,14 +2,12 @@
#include <phosg/Strings.hh>
using namespace std;
phosg::PrefixedLogger channel_exceptions_log("[Channel] ", phosg::LogLevel::L_USE_DEFAULT);
phosg::PrefixedLogger client_functions_log("[ClientFunctionIndex] ", phosg::LogLevel::L_USE_DEFAULT);
phosg::PrefixedLogger client_log("", phosg::LogLevel::L_USE_DEFAULT);
phosg::PrefixedLogger command_data_log("[Commands] ", phosg::LogLevel::L_USE_DEFAULT);
phosg::PrefixedLogger config_log("[Config] ", phosg::LogLevel::L_USE_DEFAULT);
phosg::PrefixedLogger dns_server_log("[DNSServer] ", phosg::LogLevel::L_USE_DEFAULT);
phosg::PrefixedLogger function_compiler_log("[FunctionCompiler] ", phosg::LogLevel::L_USE_DEFAULT);
phosg::PrefixedLogger ip_stack_simulator_log("[IPStackSimulator] ", phosg::LogLevel::L_USE_DEFAULT);
phosg::PrefixedLogger lobby_log("", phosg::LogLevel::L_USE_DEFAULT);
phosg::PrefixedLogger patch_index_log("[PatchFileIndex] ", phosg::LogLevel::L_USE_DEFAULT);
@@ -22,19 +20,19 @@ phosg::PrefixedLogger static_game_data_log("[StaticGameData] ", phosg::LogLevel:
static void set_log_level_from_json(
phosg::PrefixedLogger& log, const phosg::JSON& d, const char* json_key) {
try {
string name = phosg::toupper(d.at(json_key).as_string());
std::string name = phosg::toupper(d.at(json_key).as_string());
log.min_level = phosg::enum_for_name<phosg::LogLevel>(name);
} catch (const out_of_range&) {
} catch (const std::out_of_range&) {
}
}
void set_all_log_levels(phosg::LogLevel level) {
channel_exceptions_log.min_level = level;
client_functions_log.min_level = level;
client_log.min_level = level;
command_data_log.min_level = level;
config_log.min_level = level;
dns_server_log.min_level = level;
function_compiler_log.min_level = level;
ip_stack_simulator_log.min_level = level;
lobby_log.min_level = level;
patch_index_log.min_level = level;
@@ -47,11 +45,11 @@ void set_all_log_levels(phosg::LogLevel level) {
void set_log_levels_from_json(const phosg::JSON& json) {
set_log_level_from_json(channel_exceptions_log, json, "ChannelExceptions");
set_log_level_from_json(client_functions_log, json, "ClientFunctionIndex");
set_log_level_from_json(client_log, json, "Clients");
set_log_level_from_json(command_data_log, json, "CommandData");
set_log_level_from_json(config_log, json, "Config");
set_log_level_from_json(dns_server_log, json, "DNSServer");
set_log_level_from_json(function_compiler_log, json, "FunctionCompiler");
set_log_level_from_json(ip_stack_simulator_log, json, "IPStackSimulator");
set_log_level_from_json(lobby_log, json, "Lobbies");
set_log_level_from_json(patch_index_log, json, "PatchFileIndex");
+1 -1
View File
@@ -4,11 +4,11 @@
#include <phosg/Strings.hh>
extern phosg::PrefixedLogger channel_exceptions_log;
extern phosg::PrefixedLogger client_functions_log;
extern phosg::PrefixedLogger client_log;
extern phosg::PrefixedLogger command_data_log;
extern phosg::PrefixedLogger config_log;
extern phosg::PrefixedLogger dns_server_log;
extern phosg::PrefixedLogger function_compiler_log;
extern phosg::PrefixedLogger ip_stack_simulator_log;
extern phosg::PrefixedLogger lobby_log;
extern phosg::PrefixedLogger patch_index_log;
-170
View File
@@ -1,170 +0,0 @@
#include "MagEvolutionTable.hh"
#include "CommonFileFormats.hh"
using namespace std;
struct MotionReference {
struct Side {
// This specifies which entry in ItemMagMotion.dat is used. The file is just a list of 0x64-byte structures.
// 0xFF = no TItemMagSub is created
uint8_t motion_table_entry = 0xFF;
parray<uint8_t, 5> unknown_a1 = 0;
} __packed_ws__(Side, 0x06);
parray<Side, 2> sides; // [0] = right side, [1] = left side
} __packed_ws__(MotionReference, 0x0C);
template <bool BE>
struct MotionReferenceTables {
// It seems that there are two definition tables, but only the first is used on any version of PSO. On v3 and later,
// the two offsets point to the same table, but on v2 they don't and the second table contains different data.
// TODO: Figure out what the deal is with the different v2 tables.
U32T<BE> ref_table; // -> MotionReference[num_mags]
U32T<BE> unused_ref_table; // -> MotionReference[num_mags]
} __packed_ws_be__(MotionReferenceTables, 0x08);
template <bool BE>
struct ColorEntry {
// Colors are specified as 4 floats, each in the range [0, 1], for each color channel. The default colors are:
// alpha red green blue color (see StaticGameData.cc)
// 1.0 1.0 0.2 0.1 red
// 1.0 0.2 0.2 1.0 blue
// 1.0 1.0 0.9 0.1 yellow
// 1.0 0.1 1.0 0.1 green
// 1.0 0.8 0.1 1.0 purple
// 1.0 0.1 0.1 0.2 black
// 1.0 0.9 1.0 1.0 white
// 1.0 0.1 0.9 1.0 cyan
// 1.0 0.5 0.3 0.2 brown
// 1.0 1.0 0.4 0.0 orange (v3+)
// 1.0 0.502 0.545 0.977 light-blue (v3+)
// 1.0 0.502 0.502 0.0 olive (v3+)
// 1.0 0.0 0.941 0.714 turquoise (v3+)
// 1.0 0.8 0.098 0.392 fuchsia (v3+)
// 1.0 0.498 0.498 0.498 grey (v3+)
// 1.0 0.996 0.996 0.832 cream (v3+)
// 1.0 0.996 0.498 0.784 pink (v3+)
// 1.0 0.0 0.498 0.322 dark-green (v3+)
F32T<BE> alpha;
F32T<BE> red;
F32T<BE> green;
F32T<BE> blue;
} __packed_ws_be__(ColorEntry, 0x10);
template <bool BE>
struct UnknownA3Entry {
uint8_t flags;
uint8_t unknown_a2;
U16T<BE> unknown_a3;
U16T<BE> unknown_a4;
U16T<BE> unknown_a5;
} __packed_ws_be__(UnknownA3Entry, 0x08);
template <bool BE>
struct RootV2V3V4 {
/* -- / 112K / V1 / V2 / V3 / BB */
/* 00 / 0438 / 0438 / 05BC / 0340 / 0400 */ U32T<BE> motion_tables; // -> MotionReferenceTables
/* 04 / 0440 / 0440 / 0594 / 0348 / 0408 */ U32T<BE> unknown_a2; // -> (uint8_t[2])[NumMags] (references into unknown_a3)
/* 08 / 0498 / 0498 / 0608 / 03CE / 04AE */ U32T<BE> unknown_a3; // -> UnknownA3Entry[max(unknown_a2) + 1]
/* 0C / 0510 / 0520 / 06B0 / 0476 / 0556 */ U32T<BE> unknown_a4; // -> uint8_t[NumMags]
/* 10 / 053C / 054C / 06EC / 04BC / 05AC */ U32T<BE> color_table; // -> ColorEntry[NumColors]
/* 14 / / / 077C / 05DC / 06CC */ U32T<BE> evolution_number_table; // -> uint8_t[NumMags]
} __packed_ws_be__(RootV2V3V4, 0x18);
struct RootV1 {
le_uint32_t motion_tables;
le_uint32_t unknown_a2;
le_uint32_t unknown_a3;
le_uint32_t unknown_a4;
le_uint32_t color_table;
} __packed_ws__(RootV1, 0x14);
static uint8_t get_v1_mag_evolution_number(uint8_t data1_1) {
static const std::array<uint8_t, 0x2C> v1_evolution_number_table{
/* 00 */ 0, 1, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 2, 2,
/* 10 */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 2, 2, 3, 4, 3, 3,
/* 20 */ 3, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 4};
if (data1_1 >= v1_evolution_number_table.size()) {
throw runtime_error("invalid mag number");
}
return v1_evolution_number_table[data1_1];
}
template <typename RootT, size_t NumMags, size_t NumColors, bool BE>
class MagEvolutionTableT : public MagEvolutionTable {
public:
explicit MagEvolutionTableT(std::shared_ptr<const std::string> data)
: data(data), r(*data), root(&r.pget<RootT>(this->r.pget_u32l(this->data->size() - 0x10))) {}
virtual ~MagEvolutionTableT() = default;
virtual VectorXYZTF get_color_rgba(size_t index) const {
if (index >= NumColors) {
throw runtime_error("invalid mag color index");
}
const auto& color = this->r.pget<ColorEntry<BE>>(this->root->color_table + sizeof(ColorEntry<BE>) * index);
return {color.red.load(), color.green.load(), color.blue.load(), color.alpha.load()};
}
virtual uint8_t get_evolution_number(uint8_t data1_1) const {
if constexpr (requires { this->root->evolution_number_table; }) {
return this->r.pget_u8(this->root->evolution_number_table + data1_1);
} else {
return get_v1_mag_evolution_number(data1_1);
}
}
protected:
std::shared_ptr<const std::string> data;
phosg::StringReader r;
const RootT* root;
};
class MagEvolutionTableDCNTE : public MagEvolutionTable {
public:
MagEvolutionTableDCNTE() = default;
virtual ~MagEvolutionTableDCNTE() = default;
virtual VectorXYZTF get_color_rgba(size_t) const {
throw runtime_error("mag colors not available on DC NTE");
}
virtual uint8_t get_evolution_number(uint8_t data1_1) const {
return get_v1_mag_evolution_number(data1_1);
}
};
using MagEvolutionTableDC112000 = MagEvolutionTableT<RootV2V3V4<false>, 0x28, 0x09, false>;
using MagEvolutionTableV1 = MagEvolutionTableT<RootV2V3V4<false>, 0x28, 0x09, false>;
using MagEvolutionTableV2 = MagEvolutionTableT<RootV2V3V4<false>, 0x3A, 0x09, false>;
using MagEvolutionTableGCNTE = MagEvolutionTableT<RootV2V3V4<true>, 0x3A, 0x09, true>;
using MagEvolutionTableGC = MagEvolutionTableT<RootV2V3V4<true>, 0x43, 0x12, true>;
using MagEvolutionTableXB = MagEvolutionTableT<RootV2V3V4<false>, 0x43, 0x12, false>;
using MagEvolutionTableV4 = MagEvolutionTableT<RootV2V3V4<false>, 0x53, 0x12, false>;
std::shared_ptr<MagEvolutionTable> MagEvolutionTable::create(
std::shared_ptr<const std::string> data, Version version) {
switch (version) {
case Version::DC_NTE:
return std::make_shared<MagEvolutionTableDCNTE>();
case Version::DC_11_2000:
return std::make_shared<MagEvolutionTableDC112000>(data);
case Version::DC_V1:
return std::make_shared<MagEvolutionTableV1>(data);
case Version::DC_V2:
case Version::PC_NTE:
case Version::PC_V2:
return std::make_shared<MagEvolutionTableV2>(data);
case Version::GC_NTE:
return std::make_shared<MagEvolutionTableGCNTE>(data);
case Version::GC_V3:
case Version::GC_EP3:
case Version::GC_EP3_NTE:
return std::make_shared<MagEvolutionTableGC>(data);
case Version::XB_V3:
return std::make_shared<MagEvolutionTableXB>(data);
case Version::BB_V4:
return std::make_shared<MagEvolutionTableV4>(data);
default:
throw std::logic_error("Cannot create mag evolution table for this version");
}
}
-26
View File
@@ -1,26 +0,0 @@
#pragma once
#include "WindowsPlatform.hh"
#include <stdint.h>
#include <memory>
#include <string>
#include "CommonFileFormats.hh"
#include "Text.hh"
#include "Types.hh"
#include "Version.hh"
class MagEvolutionTable {
public:
virtual ~MagEvolutionTable() = default;
static std::shared_ptr<MagEvolutionTable> create(std::shared_ptr<const std::string> data, Version version);
virtual VectorXYZTF get_color_rgba(size_t index) const = 0;
virtual uint8_t get_evolution_number(uint8_t data1_1) const = 0;
protected:
MagEvolutionTable() = default;
};
+580
View File
@@ -0,0 +1,580 @@
#include "MagMetadataTable.hh"
#include "CommonFileFormats.hh"
MagMetadataTable::MotionReferences MagMetadataTable::MotionReferences::from_json(const phosg::JSON& json) {
auto parse_side = [](Side& side, const phosg::JSON& side_json) -> void {
side.eff_1 = side_json.at("Eff1").as_int();
side.eff_2 = side_json.at("Eff2").as_int();
side.eff_3 = side_json.at("Eff3").as_int();
side.eff_4_8 = side_json.at("Eff48").as_int();
side.eff_5 = side_json.at("Eff5").as_int();
side.eff_6_7 = side_json.at("Eff67").as_int();
};
MagMetadataTable::MotionReferences ret;
parse_side(ret.sides[0], json.at("Left"));
parse_side(ret.sides[1], json.at("Right"));
return ret;
}
phosg::JSON MagMetadataTable::MotionReferences::json() const {
auto serialize_side = [](const Side& side) -> phosg::JSON {
return phosg::JSON::dict({
{"Eff1", side.eff_1},
{"Eff2", side.eff_2},
{"Eff3", side.eff_3},
{"Eff48", side.eff_4_8},
{"Eff5", side.eff_5},
{"Eff67", side.eff_6_7},
});
};
return phosg::JSON::dict({{"Left", serialize_side(this->sides[0])}, {"Right", serialize_side(this->sides[1])}});
}
MagMetadataTable::UnknownA3Entry MagMetadataTable::UnknownA3Entry::from_json(const phosg::JSON& json) {
return UnknownA3Entry{
.flags = static_cast<uint8_t>(json.get_int("Flags")),
.unknown_a2 = static_cast<uint8_t>(json.get_int("UnknownA2")),
.unknown_a3 = static_cast<int16_t>(json.get_int("UnknownA3")),
.unknown_a4 = static_cast<int16_t>(json.get_int("UnknownA4")),
.unknown_a5 = static_cast<int16_t>(json.get_int("UnknownA5")),
};
}
phosg::JSON MagMetadataTable::UnknownA3Entry::json() const {
return phosg::JSON::dict({
{"Flags", this->flags},
{"UnknownA2", this->unknown_a2},
{"UnknownA3", this->unknown_a3},
{"UnknownA4", this->unknown_a4},
{"UnknownA5", this->unknown_a5},
});
}
static VectorXYZTF color_for_json(const phosg::JSON& json) {
return VectorXYZTF{
.x = json.get_float(0),
.y = json.get_float(1),
.z = json.get_float(2),
.t = json.get_float(3),
};
}
static phosg::JSON json_for_color(const VectorXYZTF& color) {
return phosg::JSON::list({color.x.load(), color.y.load(), color.z.load(), color.t.load()});
}
template <bool BE>
struct MotionReferenceTables {
// It seems that there are two definition tables, but only the first is used on any version of PSO. On v3 and later,
// the two offsets point to the same table, but on v2 they don't and the second table contains different data.
// TODO: Figure out what the deal is with the different v2 tables.
U32T<BE> first_ref_table;
U32T<BE> second_ref_table;
} __packed_ws_be__(MotionReferenceTables, 0x08);
template <bool BE>
struct ColorEntry {
// Colors are specified as 4 floats, each in the range [0, 1], for each color channel. The default colors are:
// alpha red green blue color (see StaticGameData.cc)
// 00 => 1.0 1.0 0.2 0.1 red
// 01 => 1.0 0.2 0.2 1.0 blue
// 02 => 1.0 1.0 0.9 0.1 yellow
// 03 => 1.0 0.1 1.0 0.1 green
// 04 => 1.0 0.8 0.1 1.0 purple
// 05 => 1.0 0.1 0.1 0.2 black
// 06 => 1.0 0.9 1.0 1.0 white
// 07 => 1.0 0.1 0.9 1.0 cyan
// 08 => 1.0 0.5 0.3 0.2 brown
// 09 => 1.0 1.0 0.4 0.0 orange (v3+)
// 0A => 1.0 0.502 0.545 0.977 light-blue (v3+)
// 0B => 1.0 0.502 0.502 0.0 olive (v3+)
// 0C => 1.0 0.0 0.941 0.714 turquoise (v3+)
// 0D => 1.0 0.8 0.098 0.392 fuchsia (v3+)
// 0E => 1.0 0.498 0.498 0.498 grey (v3+)
// 0F => 1.0 0.996 0.996 0.832 cream (v3+)
// 10 => 1.0 0.996 0.498 0.784 pink (v3+)
// 11 => 1.0 0.0 0.498 0.322 dark-green (v3+)
// If a mag's color index is invalid (>= 0x12), it is reassigned at equip time using the following logic:
// - Set base_index to player->visual.sh.skin if player is an android, or player->visual.costume otherwise
// - If (base_index % 9) < 7 (that is, if their costume or body color is one of the colored slots on the character
// creation screen), then set the mag color to either (base_index % 9) or (base_index % 9) + 9, with equal
// probability.
// - If (base_index % 9) >= 7 (that is, if their costume or body color is one of the last two blank-colored slots
// on the character creation screen), then set the mag color to any of the available colors, chosen at random.
F32T<BE> alpha;
F32T<BE> red;
F32T<BE> green;
F32T<BE> blue;
ColorEntry(const VectorXYZTF& c) : alpha(c.t), red(c.x), green(c.y), blue(c.z) {}
operator VectorXYZTF() const {
return VectorXYZTF{this->red.load(), this->green.load(), this->blue.load(), this->alpha.load()};
}
} __packed_ws_be__(ColorEntry, 0x10);
template <bool BE>
struct UnknownA3EntryT {
uint8_t flags;
uint8_t unknown_a2;
S16T<BE> unknown_a3;
S16T<BE> unknown_a4;
S16T<BE> unknown_a5;
UnknownA3EntryT(const MagMetadataTable::UnknownA3Entry& e)
: flags(e.flags),
unknown_a2(e.unknown_a2),
unknown_a3(e.unknown_a3),
unknown_a4(e.unknown_a4),
unknown_a5(e.unknown_a5) {}
operator MagMetadataTable::UnknownA3Entry() const {
return MagMetadataTable::UnknownA3Entry{
this->flags, this->unknown_a2, this->unknown_a3, this->unknown_a4, this->unknown_a5};
}
} __packed_ws_be__(UnknownA3EntryT, 0x08);
class JSONMagMetadataTable : public MagMetadataTable {
public:
explicit JSONMagMetadataTable(const phosg::JSON& json) {
for (const auto& mag_json : json.at("Mags").as_list()) {
const auto& unknown_a2_json = mag_json->at("UnknownA2").as_list();
auto& mag = this->mags.emplace_back();
mag.first_motion_refs = MotionReferences::from_json(mag_json->at("MotionRefs1"));
mag.second_motion_refs = MotionReferences::from_json(mag_json->at("MotionRefs2"));
mag.unknown_a2 = std::make_pair(unknown_a2_json.at(0)->as_int(), unknown_a2_json.at(1)->as_int());
mag.render_flags = mag_json->at("RenderFlags").as_int();
mag.evolution_number = mag_json->at("EvolutionNumber").as_int();
}
for (const auto& a3_json : json.at("UnknownA3").as_list()) {
this->unknown_a3.emplace_back(UnknownA3Entry::from_json(*a3_json));
}
for (const auto& color_json : json.at("Colors").as_list()) {
this->colors.emplace_back(color_for_json(*color_json));
}
}
virtual ~JSONMagMetadataTable() = default;
virtual size_t num_mags() const {
return this->mags.size();
}
virtual size_t num_motion_entries(bool) const {
return this->mags.size();
}
virtual const MotionReferences& get_motion_references(bool use_second_table, size_t data1_1) const {
const auto& mag = this->mags.at(data1_1);
return use_second_table ? mag.second_motion_refs : mag.first_motion_refs;
}
virtual std::pair<uint8_t, uint8_t> get_unknown_a2(size_t data1_1) const {
return this->mags.at(data1_1).unknown_a2;
}
virtual size_t num_unknown_a3_entries() const {
return this->unknown_a3.size();
}
virtual const UnknownA3Entry& get_unknown_a3(size_t index) const {
return this->unknown_a3.at(index);
}
virtual uint8_t get_render_flags(size_t data1_1) const {
return this->mags.at(data1_1).render_flags;
}
virtual size_t num_colors() const {
return this->colors.size();
}
virtual const VectorXYZTF& get_color_rgba(size_t index) const {
return this->colors.at(index);
}
virtual uint8_t get_evolution_number(uint8_t data1_1) const {
return this->mags.at(data1_1).evolution_number;
}
protected:
struct Mag {
MotionReferences first_motion_refs;
MotionReferences second_motion_refs;
std::pair<uint8_t, uint8_t> unknown_a2 = std::make_pair(0, 0);
uint8_t render_flags = 0;
uint8_t evolution_number = 0;
};
std::vector<Mag> mags;
std::vector<UnknownA3Entry> unknown_a3;
std::vector<VectorXYZTF> colors;
};
struct HeaderV1 {
parray<uint8_t, 4> unknown_a1 = {0x0F, 0xF0, 0x00, 0x00};
le_uint32_t unknown_a2 = 0x00000003;
le_uint16_t unknown_a3 = 0x00C8;
le_uint16_t unknown_a4 = 0x0078;
// unknown_a5 added in V2
le_float unknown_a6 = 0.25; // 3E800000
le_float unknown_a7 = 0.099999994; // 3DCCCCCC
le_uint32_t unknown_a8 = 0x00000C00;
} __packed_ws__(HeaderV1, 0x18);
template <bool BE>
struct HeaderV2V3V4 {
parray<uint8_t, 4> unknown_a1 = {0x0F, 0xF0, 0x00, 0x00};
U32T<BE> unknown_a2 = 0x00000003;
U16T<BE> unknown_a3 = 0x00C8;
U16T<BE> unknown_a4 = 0x0078;
parray<uint8_t, 4> unknown_a5 = {0xC8, 0x00, 0x00, 0x00};
F32T<BE> unknown_a6 = 0.25; // 3E800000
F32T<BE> unknown_a7 = 0.099999994; // 3DCCCCCC
U32T<BE> unknown_a8 = 0x00000C00;
} __packed_ws_be__(HeaderV2V3V4, 0x1C);
// Fields:
// 112K / V1 / V2 / V3 / BB R
// 0018 / 0018 / 001C / 001C / 001C motion_tables.first_ref_table // -> MotionReferences[NumMags]
// 0228 / 0228 / 02D4 / 001C / 001C motion_tables.second_ref_table // -> MotionReferences[NumMags]
// 0438 / 0438 / 05BC / 0340 / 0400 * motion_tables; // -> MotionReferenceTables
// 0440 / 0440 / 0594 / 0348 / 0408 * unknown_a2; // -> (uint8_t[2])[NumMags] (references into unknown_a3)
// 0498 / 0498 / 0608 / 03CE / 04AE * unknown_a3; // -> UnknownA3Entry[max(unknown_a2) + 1]
// 0510 / 0520 / 06B0 / 0476 / 0556 * render_flags; // -> uint8_t[NumMags]
// 053C / 054C / 06EC / 04BC / 05AC * color_table; // -> ColorEntry[NumColors]
// ---- / ---- / 077C / 05DC / 06CC * evolution_number_table; // -> uint8_t[NumMags]
template <bool BE>
struct RootV1 {
U32T<BE> motion_tables;
U32T<BE> unknown_a2;
U32T<BE> unknown_a3;
U32T<BE> render_flags;
U32T<BE> color_table;
} __packed_ws_be__(RootV1, 0x14);
template <bool BE>
struct RootV2V3V4 : RootV1<BE> {
U32T<BE> evolution_number_table;
} __packed_ws_be__(RootV2V3V4, 0x18);
static uint8_t get_v1_mag_evolution_number(uint8_t data1_1) {
static const std::array<uint8_t, 0x2C> v1_evolution_number_table{
/* 00 */ 0, 1, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 2, 2,
/* 10 */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 2, 2, 3, 4, 3, 3,
/* 20 */ 3, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 4};
if (data1_1 >= v1_evolution_number_table.size()) {
throw std::runtime_error("invalid mag number");
}
return v1_evolution_number_table[data1_1];
}
template <typename HeaderT, typename RootT, size_t NumMags, size_t NumColors, bool BE>
class BinaryMagMetadataTableT : public MagMetadataTable {
public:
explicit BinaryMagMetadataTableT(std::shared_ptr<const std::string> data)
: data(data), r(*data), root(&r.pget<RootT>(this->r.pget<U32T<BE>>(this->data->size() - 0x10))) {}
virtual ~BinaryMagMetadataTableT() = default;
template <typename RawT, typename ParsedT>
const ParsedT& add_to_vector_cache(std::vector<ParsedT>& cache, size_t base_offset, size_t index) const {
while (cache.size() <= index) {
cache.emplace_back(this->r.pget<RawT>(base_offset + sizeof(RawT) * cache.size()));
}
return cache[index];
}
virtual size_t num_mags() const {
return NumMags;
}
virtual size_t num_motion_entries(bool use_second_table) const {
const auto& tables = this->r.pget<MotionReferenceTables<BE>>(this->root->motion_tables);
return get_rel_array_count<MotionReferences>(
this->all_start_offsets(), use_second_table ? tables.second_ref_table : tables.first_ref_table);
}
virtual const MotionReferences& get_motion_references(bool use_second_table, size_t index) const {
if (index >= this->num_motion_entries(use_second_table)) {
throw std::logic_error("Invalid motion reference index");
}
const auto& tables = this->r.pget<MotionReferenceTables<BE>>(this->root->motion_tables);
uint32_t array_offset = use_second_table ? tables.second_ref_table : tables.first_ref_table;
return this->r.pget<MotionReferences>(array_offset + sizeof(MotionReferences) * index);
}
virtual std::pair<uint8_t, uint8_t> get_unknown_a2(size_t index) const {
if (index >= this->num_mags()) {
throw std::logic_error("Invalid unknown_a2 index");
}
uint32_t base_offset = this->root->unknown_a2 + (index * 2);
return std::make_pair(this->r.pget_u8(base_offset), this->r.pget_u8(base_offset + 1));
}
virtual size_t num_unknown_a3_entries() const {
return get_rel_array_count<UnknownA3EntryT<BE>>(this->all_start_offsets(), this->root->unknown_a3);
}
virtual const UnknownA3Entry& get_unknown_a3(size_t index) const {
if (index >= this->num_unknown_a3_entries()) {
throw std::logic_error("Invalid unknown_a2 index");
}
return this->add_to_vector_cache<UnknownA3EntryT<BE>>(this->unknown_a3_entries, this->root->unknown_a3, index);
}
virtual uint8_t get_render_flags(size_t index) const {
if (index >= this->num_mags()) {
throw std::logic_error("Invalid render_flags index");
}
return this->r.pget_u8(this->root->render_flags + index);
}
virtual size_t num_colors() const {
return NumColors;
}
virtual const VectorXYZTF& get_color_rgba(size_t index) const {
if (index >= NumColors) {
throw std::runtime_error("invalid mag color index");
}
return this->add_to_vector_cache<ColorEntry<BE>>(this->colors, this->root->color_table, index);
}
virtual uint8_t get_evolution_number(uint8_t data1_1) const {
if (data1_1 >= this->num_mags()) {
throw std::logic_error("Invalid evolution_number index");
}
if constexpr (requires { this->root->evolution_number_table; }) {
return this->r.pget_u8(this->root->evolution_number_table + data1_1);
} else {
return get_v1_mag_evolution_number(data1_1);
}
}
const std::set<uint32_t>& all_start_offsets() const {
if (this->start_offsets.empty()) {
this->start_offsets = all_relocation_offsets_for_rel_file<BE>(r.pgetv(0, r.size()), r.size());
}
return this->start_offsets;
}
static std::string serialize(const MagMetadataTable& table) {
RELFileWriter<BE> rel;
RootT root;
rel.template put<HeaderT>(HeaderT());
MotionReferenceTables<BE> motion_ref_tables;
motion_ref_tables.first_ref_table = rel.w.size();
bool alias_motion_ref_tables = true;
for (size_t z = 0; z < table.num_motion_entries(false); z++) {
const auto& refs = table.get_motion_references(false, z);
rel.template put<MotionReferences>(refs);
if (refs != table.get_motion_references(true, z)) {
alias_motion_ref_tables = false;
}
}
if (alias_motion_ref_tables) {
motion_ref_tables.second_ref_table = motion_ref_tables.first_ref_table;
} else {
motion_ref_tables.second_ref_table = rel.w.size();
for (size_t z = 0; z < table.num_motion_entries(true); z++) {
rel.template put<MotionReferences>(table.get_motion_references(true, z));
}
}
root.motion_tables = rel.w.size();
rel.template put<MotionReferenceTables<BE>>(motion_ref_tables);
rel.relocations.emplace(root.motion_tables);
rel.relocations.emplace(root.motion_tables + 4);
root.unknown_a2 = rel.w.size();
for (size_t z = 0; z < table.num_mags(); z++) {
auto [left, right] = table.get_unknown_a2(z);
rel.template put<uint8_t>(left);
rel.template put<uint8_t>(right);
}
root.unknown_a3 = rel.w.size();
for (size_t z = 0; z < table.num_unknown_a3_entries(); z++) {
rel.template put<UnknownA3EntryT<BE>>(table.get_unknown_a3(z));
}
root.render_flags = rel.w.size();
for (size_t z = 0; z < table.num_mags(); z++) {
rel.template put<uint8_t>(table.get_render_flags(z));
}
rel.align(4);
root.color_table = rel.w.size();
for (size_t z = 0; z < table.num_colors(); z++) {
rel.template put<ColorEntry<BE>>(table.get_color_rgba(z));
}
if constexpr (requires { root.evolution_number_table; }) {
root.evolution_number_table = rel.w.size();
for (size_t z = 0; z < table.num_mags(); z++) {
rel.template put<uint8_t>(table.get_evolution_number(z));
}
}
rel.align(4);
uint32_t root_offset = rel.template put<RootT>(root);
for (size_t z = 1; z <= sizeof(RootT) / 4; z++) {
rel.relocations.emplace(rel.w.size() - (z * 4));
}
return rel.finalize(root_offset);
}
protected:
std::shared_ptr<const std::string> data;
phosg::StringReader r;
const RootT* root;
mutable std::set<uint32_t> start_offsets;
mutable std::vector<UnknownA3Entry> unknown_a3_entries;
mutable std::vector<VectorXYZTF> colors;
};
class MagMetadataTableDCNTE : public MagMetadataTable {
public:
MagMetadataTableDCNTE() = default;
virtual ~MagMetadataTableDCNTE() = default;
virtual size_t num_mags() const {
return 0x2C;
}
virtual size_t num_motion_entries(bool) const {
return 0;
}
virtual const MotionReferences& get_motion_references(bool, size_t) const {
throw std::runtime_error("Mag tables not available on DC NTE");
}
virtual std::pair<uint8_t, uint8_t> get_unknown_a2(size_t) const {
throw std::runtime_error("Mag tables not available on DC NTE");
}
virtual size_t num_unknown_a3_entries() const {
return 0;
}
virtual const UnknownA3Entry& get_unknown_a3(size_t) const {
throw std::runtime_error("Mag tables not available on DC NTE");
}
virtual uint8_t get_render_flags(size_t) const {
throw std::runtime_error("Mag tables not available on DC NTE");
}
virtual size_t num_colors() const {
return 0;
}
virtual const VectorXYZTF& get_color_rgba(size_t) const {
throw std::runtime_error("Mag tables not available on DC NTE");
}
virtual uint8_t get_evolution_number(uint8_t data1_1) const {
return get_v1_mag_evolution_number(data1_1);
}
};
using MagMetadataTableDC112000 = BinaryMagMetadataTableT<HeaderV1, RootV1<false>, 0x2C, 0x09, false>;
using MagMetadataTableV1 = BinaryMagMetadataTableT<HeaderV1, RootV1<false>, 0x2C, 0x09, false>;
using MagMetadataTableV2 = BinaryMagMetadataTableT<HeaderV2V3V4<false>, RootV2V3V4<false>, 0x3A, 0x09, false>;
using MagMetadataTableGCNTE = BinaryMagMetadataTableT<HeaderV2V3V4<true>, RootV2V3V4<true>, 0x3A, 0x09, true>;
using MagMetadataTableGC = BinaryMagMetadataTableT<HeaderV2V3V4<true>, RootV2V3V4<true>, 0x43, 0x12, true>;
using MagMetadataTableXB = BinaryMagMetadataTableT<HeaderV2V3V4<false>, RootV2V3V4<false>, 0x43, 0x12, false>;
using MagMetadataTableV4 = BinaryMagMetadataTableT<HeaderV2V3V4<false>, RootV2V3V4<false>, 0x53, 0x12, false>;
std::shared_ptr<MagMetadataTable> MagMetadataTable::from_binary(
std::shared_ptr<const std::string> data, Version version) {
switch (version) {
case Version::DC_NTE:
return std::make_shared<MagMetadataTableDCNTE>();
case Version::DC_11_2000:
return std::make_shared<MagMetadataTableDC112000>(data);
case Version::DC_V1:
return std::make_shared<MagMetadataTableV1>(data);
case Version::DC_V2:
case Version::PC_NTE:
case Version::PC_V2:
return std::make_shared<MagMetadataTableV2>(data);
case Version::GC_NTE:
return std::make_shared<MagMetadataTableGCNTE>(data);
case Version::GC_V3:
case Version::GC_EP3:
case Version::GC_EP3_NTE:
return std::make_shared<MagMetadataTableGC>(data);
case Version::XB_V3:
return std::make_shared<MagMetadataTableXB>(data);
case Version::BB_V4:
return std::make_shared<MagMetadataTableV4>(data);
default:
throw std::logic_error("Cannot create mag metadata table for this version");
}
}
std::shared_ptr<MagMetadataTable> MagMetadataTable::from_json(const phosg::JSON& json) {
return std::make_shared<JSONMagMetadataTable>(json);
}
phosg::JSON MagMetadataTable::json() const {
if (this->num_motion_entries(true) != this->num_motion_entries(false)) {
throw std::runtime_error("Motion entry counts differ across tables");
}
if (this->num_motion_entries(false) != this->num_mags()) {
throw std::runtime_error(std::format("Motion entry count {} does not match mag count {}",
this->num_motion_entries(false), this->num_mags()));
}
auto mags_json = phosg::JSON::list();
for (size_t z = 0; z < this->num_mags(); z++) {
auto mag_json = phosg::JSON::dict();
mag_json.emplace("MotionRefs1", this->get_motion_references(false, z).json());
mag_json.emplace("MotionRefs2", this->get_motion_references(true, z).json());
auto unknown_a2 = this->get_unknown_a2(z);
mag_json.emplace("UnknownA2", phosg::JSON::list({unknown_a2.first, unknown_a2.second}));
mag_json.emplace("RenderFlags", this->get_render_flags(z));
mag_json.emplace("EvolutionNumber", this->get_evolution_number(z));
mags_json.emplace_back(std::move(mag_json));
}
auto unknown_a3_json = phosg::JSON::list();
for (size_t z = 0; z < this->num_unknown_a3_entries(); z++) {
unknown_a3_json.emplace_back(this->get_unknown_a3(z).json());
}
auto colors_json = phosg::JSON::list();
for (size_t z = 0; z < this->num_colors(); z++) {
colors_json.emplace_back(json_for_color(this->get_color_rgba(z)));
}
return phosg::JSON::dict({
{"Mags", std::move(mags_json)},
{"UnknownA3", std::move(unknown_a3_json)},
{"Colors", std::move(colors_json)},
});
}
std::string MagMetadataTable::serialize_binary(Version version) const {
switch (version) {
case Version::DC_NTE:
throw std::runtime_error("DC NTE does not have a an ItemMagEdit format");
case Version::DC_11_2000:
return MagMetadataTableDC112000::serialize(*this);
case Version::DC_V1:
return MagMetadataTableV1::serialize(*this);
case Version::DC_V2:
case Version::PC_NTE:
case Version::PC_V2:
return MagMetadataTableV2::serialize(*this);
case Version::GC_NTE:
return MagMetadataTableGCNTE::serialize(*this);
case Version::GC_V3:
case Version::GC_EP3:
case Version::GC_EP3_NTE:
return MagMetadataTableGC::serialize(*this);
case Version::XB_V3:
return MagMetadataTableXB::serialize(*this);
case Version::BB_V4:
return MagMetadataTableV4::serialize(*this);
default:
throw std::logic_error("Cannot create item parameter table for this version");
}
}
+78
View File
@@ -0,0 +1,78 @@
#pragma once
#include "WindowsPlatform.hh"
#include <stdint.h>
#include <memory>
#include <string>
#include "CommonFileFormats.hh"
#include "Text.hh"
#include "Types.hh"
#include "Version.hh"
class MagMetadataTable {
public:
struct MotionReferences {
// These entries specify which entry in ItemMagMotion.dat is used. The file is just a list of 0x64-byte structures.
// 0xFF = no TItemMagSub is created
struct Side {
uint8_t eff_1;
uint8_t eff_2;
uint8_t eff_3;
uint8_t eff_4_8;
uint8_t eff_5;
uint8_t eff_6_7;
bool operator==(const Side&) const = default;
bool operator!=(const Side&) const = default;
} __packed_ws__(Side, 6);
parray<Side, 2> sides; // [0] = right side, [1] = left side
bool operator==(const MotionReferences&) const = default;
bool operator!=(const MotionReferences&) const = default;
static MotionReferences from_json(const phosg::JSON& json);
phosg::JSON json() const;
} __packed_ws__(MotionReferences, 0x0C);
struct UnknownA3Entry {
uint8_t flags;
uint8_t unknown_a2;
int16_t unknown_a3;
int16_t unknown_a4;
int16_t unknown_a5;
static UnknownA3Entry from_json(const phosg::JSON& json);
phosg::JSON json() const;
};
virtual ~MagMetadataTable() = default;
virtual size_t num_mags() const = 0;
virtual size_t num_motion_entries(bool use_second_table) const = 0;
virtual const MotionReferences& get_motion_references(bool use_second_table, size_t index) const = 0;
virtual std::pair<uint8_t, uint8_t> get_unknown_a2(size_t index) const = 0;
virtual size_t num_unknown_a3_entries() const = 0;
virtual const UnknownA3Entry& get_unknown_a3(size_t index) const = 0;
virtual uint8_t get_render_flags(size_t index) const = 0;
virtual size_t num_colors() const = 0;
virtual const VectorXYZTF& get_color_rgba(size_t index) const = 0;
virtual uint8_t get_evolution_number(uint8_t data1_1) const = 0;
static std::shared_ptr<MagMetadataTable> from_binary(std::shared_ptr<const std::string> data, Version version);
static std::shared_ptr<MagMetadataTable> from_json(const phosg::JSON& json);
phosg::JSON json() const;
std::string serialize_binary(Version version) const;
protected:
MagMetadataTable() = default;
};

Some files were not shown because too many files have changed in this diff Show More