Compare commits

..

435 Commits

Author SHA1 Message Date
incentive f05e68492d PSO Peeps Start
Docker / Build (push) Has been cancelled
2026-05-01 23:14:17 -04:00
Martin Michelsen 7f68d41bac fix port name in game server lookup 2026-04-25 22:21:46 -07:00
Martin Michelsen 75e7232096 handle BB not sending C6 after 08E8 2026-04-25 12:06:43 -07:00
Martin Michelsen 7a29b39771 allow 6xCB in free-play 2026-04-24 20:36:45 -07:00
Martin Michelsen cfcb56b13f update command notes 2026-04-24 20:17:48 -07:00
Martin Michelsen 9e6740b778 update 6x30 notes 2026-04-20 20:00:11 -07:00
Martin Michelsen 590f937959 add release script 2026-04-19 13:32:26 -07:00
Martin Michelsen 31abc24e81 don't allow players to pick up items if they are too far away 2026-04-19 09:35:20 -07:00
Martin Michelsen 507fbf0451 add another useless AR code 2026-04-19 08:57:49 -07:00
Martin Michelsen 1fa660129d add last-hit tracking for target subcommands 2026-04-11 08:28:40 -07:00
Martin Michelsen 67082f7b6b update static 2026-04-11 08:14:12 -07:00
Martin Michelsen b34c9a7c88 improve error message for missing quest common/rare item sets 2026-04-05 21:50:53 -07:00
Martin Michelsen 87e85932a4 switch rare drops to stacked space logic 2026-04-03 19:52:42 -07:00
Martin Michelsen b704d827ed add support for direct Xbox connections 2026-04-01 21:47:21 -07:00
Martin Michelsen 598ecf88e3 revise death_flags notes 2026-04-01 08:48:19 -07:00
Martin Michelsen a05971017d explain a few of the unknown player_flags bits 2026-03-30 19:38:15 -07:00
Martin Michelsen b7819413b0 handle missing DAR entries in HTML generator 2026-03-27 07:55:44 -07:00
Martin Michelsen 80e4b0e6fe clean up formatting on Ep3 drop rates comment 2026-03-23 20:39:44 -07:00
Martin Michelsen daee47b722 use new phosg parallel functions 2026-03-22 21:37:52 -07:00
Martin Michelsen 5724fb9a12 add $allrare debug command; closes #739 2026-03-22 21:37:43 -07:00
Martin Michelsen 983753f840 add FogDebug patch 2026-03-21 09:30:59 -07:00
Martin Michelsen 53d2318873 more details on player_flags 2026-03-20 22:21:21 -07:00
Martin Michelsen 83291d5501 more details on player_flags 2026-03-19 23:01:30 -07:00
Martin Michelsen 55be92a56f add game duration to info window 2026-03-19 10:42:01 -07:00
Martin Michelsen 6a23e5da0a make some sense out of game_flags and player_flags 2026-03-18 22:44:02 -07:00
Martin Michelsen 4571cf7fdc fix attribute count check in 6xDA 2026-03-11 07:19:53 -07:00
Martin Michelsen 4e3549ba6b use EnemyType in ItemCreator; fix incorrect drop tables 2026-03-08 20:40:15 -07:00
Martin Michelsen 3cbf64dda2 update quest collision opcode docs 2026-03-08 12:55:57 -07:00
Martin Michelsen 382bc6b7ce don't allow error cases for bb_exchange_pd_percent to destroy items 2026-03-04 21:37:54 -08:00
Martin Michelsen e05991ffb3 add test for trade window sequence 2026-03-03 20:43:46 -08:00
Martin Michelsen ffda97222d document --language option in disassemble-quest-script 2026-03-02 08:46:38 -08:00
Martin Michelsen 8f21604367 clean up CMakeLists a bit 2026-03-02 08:46:25 -08:00
Martin Michelsen 4045504b61 also send combatant info board entries in spectator teams 2026-02-28 20:49:36 -08:00
Martin Michelsen 4aad1514c2 port a few more bug fix patches to Xbox 2026-02-28 20:49:19 -08:00
Martin Michelsen a649a4a146 add WIP Xbox BugFixes patch
Docker / Build (push) Has been cancelled
2026-02-26 21:47:53 -08:00
Martin Michelsen 7e21d8a9a1 enable color codes in info-board proxy shell command 2026-02-26 21:47:31 -08:00
Martin Michelsen c0fc3014cf move C9 comment 2026-02-22 20:33:02 -08:00
Martin Michelsen 08dff98948 fix Influence assist effect 2026-02-22 07:36:11 -08:00
Martin Michelsen d1c1228308 track hits from TargetEntry commands instead of 6x0A 2026-02-16 20:44:21 -08:00
Martin Michelsen b5fd58722b delete bank file when creating new character 2026-02-16 20:39:20 -08:00
Martin Michelsen f0e8e35e2b fix typos in error messages 2026-02-16 20:20:11 -08:00
Martin Michelsen 68b495b4b4 make --restrict-room floor-specific 2026-02-15 10:50:16 -08:00
Martin Michelsen 1e459edfc4 add --restrict-room in random enemy optimizer 2026-02-15 08:32:27 -08:00
Martin Michelsen c6d7025f43 add more debug info to EXP computation 2026-02-15 08:31:59 -08:00
Martin Michelsen ccf4b723f5 add --pessimize option in optimize-materialized-map 2026-02-14 19:58:54 -08:00
Martin Michelsen 8717f00106 add param filtering in materialize optimizer 2026-02-12 21:37:33 -08:00
Martin Michelsen 99630c999d add optimize-materialized-map 2026-02-12 21:12:57 -08:00
Martin Michelsen e8c262223b simplify Ep3 spectator join logic 2026-02-09 21:20:22 -08:00
Martin Michelsen 3d7215d591 add only command in address translator 2026-02-09 21:20:11 -08:00
Martin Michelsen ba48236200 add 2OJ4 2026-02-09 21:19:44 -08:00
Martin Michelsen 8065300fae replace item names with IDs in config.json 2026-02-07 22:34:45 -08:00
Martin Michelsen e9dfa5d1de fail on unknown quest directives 2026-02-05 19:58:57 -08:00
Martin Michelsen d38be2f360 add default4 BB key 2026-02-05 19:58:57 -08:00
Martin Michelsen 2429c4d341 add decoder/encoder for AdEnding.rel 2026-02-01 17:32:04 -08:00
Martin Michelsen ef2d9fae03 add qdefault codes in ar-codes.txt 2026-02-01 16:38:41 -08:00
Martin Michelsen 7016d65313 make gsl commands easier to use 2026-01-27 20:01:50 -08:00
Martin Michelsen bdd066edb2 fix notes for npc_param regsA[2] 2026-01-18 22:53:39 -08:00
Martin Michelsen 1bd305d4e7 add client function for debugging movement data 2026-01-14 22:06:06 -08:00
Martin Michelsen 890014b223 add findings from first pass on movement data 2026-01-12 22:03:37 -08:00
Martin Michelsen e4ef96fcc5 add inventory debugging AR code 2026-01-12 22:03:37 -08:00
Martin Michelsen 641b3a7bef fix formatting in proxy handler table 2026-01-12 22:03:37 -08:00
Martin Michelsen 6f9f684cc9 add AR code to disable CCA dust 2026-01-05 10:06:20 -08:00
Martin Michelsen 2602196279 make downloading a quest not end the proxy session 2026-01-05 00:12:37 -08:00
Martin Michelsen ec16cb0ae3 rename fields in battle param structs 2026-01-04 21:08:38 -08:00
Martin Michelsen 6da7b26c9f add comment about common table JSON formats 2026-01-04 10:51:18 -08:00
Martin Michelsen 8663e6682a slightly more fanciness in AccurateKillCount patch 2026-01-04 10:07:06 -08:00
Martin Michelsen 9b14e5d400 don't let SuperMap's edit-distance allow subtypes of the same base_type to merge 2026-01-04 10:07:06 -08:00
Martin Michelsen a1e067cc52 add a few learnings from enemy RE 2026-01-04 10:07:06 -08:00
Martin Michelsen a469b4355e add option to change chat command character 2026-01-04 00:59:39 -08:00
Martin Michelsen 4aa206bd4b add all BP indexes and fix incorrect RT indexes 2026-01-04 00:59:39 -08:00
Martin Michelsen d9540ba414 add comment about game section ID changes 2026-01-01 11:14:27 -08:00
Martin Michelsen cb7c45ef27 reformat ItemCreator.cc 2026-01-01 11:14:17 -08:00
Martin Michelsen f98db20618 implement BB system, guild card, and stream files in proxy save files option 2026-01-01 10:58:47 -08:00
Martin Michelsen 8fbf2246e6 fix change_event applying when it shouldn't on BB 2026-01-01 10:58:47 -08:00
Martin Michelsen 6b1726c1b5 add $savefiles command 2026-01-01 10:58:47 -08:00
Martin Michelsen cac61e6763 add ability to delay $item until next drop on proxy 2026-01-01 10:58:47 -08:00
Martin Michelsen 227e88f906 add warning about ItemPT/RT not reloading at quest start 2025-12-30 21:17:38 -08:00
Martin Michelsen 7ab3175f80 make quest item exchange implementations more complete 2025-12-26 19:54:22 -08:00
Martin Michelsen cd0d13e98c update notes on 0122 command 2025-12-26 09:44:44 -08:00
Martin Michelsen 8eeb487bc7 make item data stack count consistent with bank item stack count at load time 2025-12-25 23:21:32 -08:00
Martin Michelsen d79d551c68 fix Momoka item exchange via menu object 2025-12-25 22:19:54 -08:00
Martin Michelsen 4b43333ce9 fix data race between save timer and client disconnect 2025-12-25 22:19:35 -08:00
Martin Michelsen b228ea847f show enemy type on proxy if debug and fast kills are both on 2025-12-25 14:58:47 -08:00
Martin Michelsen 4d97bdec7f fix type in compute_all_valid_primary_identifiers 2025-12-25 14:57:41 -08:00
Martin Michelsen 8133b20598 add battle param reload bugfix 2025-12-23 09:09:49 -08:00
Martin Michelsen 73ced9d229 print bank when team reward is given 2025-12-23 09:09:38 -08:00
Martin Michelsen 6e765fe1ed clarify comment in PSOV2Encryption::single 2025-12-23 09:09:20 -08:00
Martin Michelsen 26f9b90ef8 fix line numbers around .only_versions 2025-12-23 09:08:51 -08:00
Martin Michelsen 668c687d68 remove unused argument 2025-12-22 00:46:56 -08:00
Martin Michelsen 87b048dc15 implement PSOV2Encryption::single 2025-12-22 00:14:22 -08:00
Martin Michelsen ea23f18aa2 use amount in 6xCC handler 2025-12-21 21:22:28 -08:00
Martin Michelsen a0a7231d67 reformat remaining files 2025-12-21 21:15:49 -08:00
Martin Michelsen e5a03b7e9b move weights sum out of loops in materialize_random_sections 2025-12-21 14:58:10 -08:00
Martin Michelsen a013b8c9d3 reformat more files 2025-12-21 14:57:53 -08:00
Martin Michelsen 894ac6b8ff reformat more files; add Ep3 map endpoint in HTTP server 2025-12-21 10:35:41 -08:00
Martin Michelsen a462a774f5 reformat more files 2025-12-20 21:55:32 -08:00
Martin Michelsen a9fa138213 add MapFile::serialize 2025-12-20 18:52:45 -08:00
Martin Michelsen 0a4c9a0a61 document sorted restriction on random enemy room entries 2025-12-19 21:44:55 -08:00
Martin Michelsen f99bba67d0 update TObjAreaWarpQuest notes 2025-12-19 01:14:14 -08:00
Martin Michelsen 849cca37c8 add explanation in expr field in cards.html 2025-12-19 00:11:09 -08:00
Martin Michelsen 9ebaaacd46 reformat DCSerialNumbers 2025-12-19 00:10:36 -08:00
Martin Michelsen c1968dad27 document TObjRoomId 2025-12-17 23:49:09 -08:00
Martin Michelsen 2732f9c9f8 document materialize-map command 2025-12-17 23:48:00 -08:00
Martin Michelsen 1bd2e6cf62 add challenge location limit override patch 2025-12-12 14:51:43 -08:00
Martin Michelsen 1ab7a851be simulate arithmetic opcodes in quest script analysis 2025-12-11 00:20:55 -08:00
Martin Michelsen 342b4df8c4 add action for debugging random enemy sections 2025-12-11 00:20:39 -08:00
Martin Michelsen 8953ffc2b5 add slow gibbles fix code 2025-12-08 20:10:15 -08:00
Martin Michelsen af796a418a add another AR code 2025-12-08 20:10:15 -08:00
Martin Michelsen 60203bdfba fix formatting in EnemyType.cc 2025-12-07 18:07:23 -08:00
Martin Michelsen 6677908354 reformat Map.cc/hh 2025-12-07 16:44:08 -08:00
Martin Michelsen 96079700f7 fix SetDataTable formatting 2025-12-06 22:05:35 -08:00
Martin Michelsen 976a281e93 update formatting in src/Episode3 2025-12-06 00:18:53 -08:00
Martin Michelsen 6291e42ba9 make 04E8 handler match the client's logic 2025-12-05 19:53:09 -08:00
Martin Michelsen a89423e9f5 reformat CommandFormats and DataIndexes 2025-12-05 19:52:49 -08:00
Martin Michelsen 81169ba9d3 add lobby arrow AR codes 2025-12-05 19:51:58 -08:00
Martin Michelsen e715a8461a fix comment in EnableRandomEnemies include 2025-12-05 15:39:34 -08:00
Martin Michelsen 1ee6b21398 add warning if random enemy location count is too large 2025-12-05 15:39:25 -08:00
Martin Michelsen 9524020aaa improve disassembly for random enemy sections 2025-12-03 21:49:44 -08:00
Martin Michelsen 194bb5b393 actually enforce NOCOMMIT tags finally 2025-12-02 21:46:58 -08:00
Martin Michelsen 779ec9df3b add EnableRandomEnemies include 2025-12-02 21:07:47 -08:00
Martin Michelsen 82ed175a5c add param5 note for TObjCityMapWarp 2025-12-02 00:03:57 -08:00
Martin Michelsen 68f96129fe remove stray debug print 2025-12-02 00:03:31 -08:00
Martin Michelsen c482324a97 use area instead of floor during map construction 2025-11-30 23:12:55 -08:00
Martin Michelsen 800c70c401 skip non-BB clients in 6xC8 handler 2025-11-30 17:31:13 -08:00
Martin Michelsen f26c543977 handle extra data after quest label table 2025-11-30 11:56:20 -08:00
Martin Michelsen 23e31749e9 add transcode-text action 2025-11-30 11:56:20 -08:00
Martin Michelsen ad91b6f6b7 update some boss command notes 2025-11-30 08:50:11 -08:00
Martin Michelsen 2c333b51d2 add $fastkill command 2025-11-29 12:12:34 -08:00
Martin Michelsen 80f8ee1b09 fix flags in Account::str 2025-11-29 09:47:36 -08:00
Martin Michelsen 1498a6e68d fix meta.quest_number check 2025-11-28 15:02:28 -08:00
Martin Michelsen 1fc313505a use test config for quest compiler test 2025-11-28 14:47:51 -08:00
Martin Michelsen 435ac82c18 define most of the remining fields in BB extended quest header 2025-11-28 14:36:13 -08:00
Martin Michelsen 7ec267a7c0 fix solo quest unlock flags 2025-11-28 14:22:16 -08:00
Martin Michelsen 81293255b5 fix more quest metadata 2025-11-28 14:22:07 -08:00
Martin Michelsen 4fe225a302 fix multiple bugs in quest assembler 2025-11-28 12:41:42 -08:00
Martin Michelsen 3ef91b0159 allow whitespace in create item masks 2025-11-28 12:41:42 -08:00
Martin Michelsen e02a006b60 add support for cross-episode quests 2025-11-28 12:40:14 -08:00
Martin Michelsen 23eb6b29a5 fix metadata on more quests 2025-11-28 12:33:28 -08:00
Martin Michelsen afe48e7034 ignore .dec files 2025-11-27 21:06:00 -08:00
Martin Michelsen bd1cdfdb97 further improve quest assembler/disassembler matching 2025-11-26 23:06:04 -08:00
Martin Michelsen a783177420 rewrite quest disassembler for better consistency with assembler 2025-11-25 23:41:46 -08:00
Martin Michelsen 9d42f849c5 fix metadata in solo quest headers 2025-11-25 23:26:24 -08:00
Martin Michelsen 566de06fd1 rewrite quest disassembler 2025-11-24 01:03:24 -08:00
Martin Michelsen 474ad99396 document how wave events work 2025-11-23 17:35:52 -08:00
Martin Michelsen b53847d1b5 update .gitignore 2025-11-23 17:35:38 -08:00
Martin Michelsen d827c1bf5d fix random enemy definitions count check; closes #723 2025-11-20 08:31:43 -08:00
Martin Michelsen 886daa5880 add .evt to patch instructions in readme 2025-11-20 08:29:50 -08:00
Martin Michelsen cc72092b05 write 59NJ version of MomokaItemExchangeFix 2025-11-19 22:27:26 -08:00
Martin Michelsen c6f74e74c4 hide other players' EXP values in ServerEXPDisplay
Docker / Build (push) Has been cancelled
2025-11-18 17:29:22 -08:00
Martin Michelsen 328980628a fix $edit level 2025-11-18 17:28:48 -08:00
Martin Michelsen 886e9b9f4f fix 5% payment type in 6xDA 2025-11-18 10:27:31 -08:00
Martin Michelsen 26d2ae416e delete unused arguments 2025-11-16 22:37:13 -08:00
Martin Michelsen 62c4c82fcc rewrite HTTP interface 2025-11-16 15:09:28 -08:00
Martin Michelsen 11cc19fe3e update last player name when sending E7; closes #706 2025-11-16 11:01:06 -08:00
Martin Michelsen d1d045a70e fix rare enemy rate inheritance; closes #719 2025-11-16 10:56:39 -08:00
Martin Michelsen 54c790a63c fix notes on get_slot_meseta 2025-11-16 10:48:02 -08:00
Martin Michelsen f1f5c1036a fix invalid range check 2025-11-16 00:05:47 -08:00
Martin Michelsen 77d5436b15 implement quest item creation masks 2025-11-15 23:54:49 -08:00
Martin Michelsen 678c60dd14 update some notes; fix quest assembler bugs 2025-11-15 22:36:18 -08:00
Martin Michelsen d40d231584 add some new AR codes 2025-11-14 19:13:15 -08:00
Martin Michelsen 00ddff7e46 update notes on loading into games/lobbies 2025-11-14 19:13:15 -08:00
Blst34 5725af0f6b Add files via upload 2025-11-12 19:55:11 -08:00
Martin Michelsen 87248e7e67 fix enemy alias lookup logic 2025-11-11 00:04:55 -08:00
Martin Michelsen 712cfc9ac4 fix JSON common table parser 2025-11-10 22:56:23 -08:00
Martin Michelsen 1d8befde8e add fix for TJS rapid-switch crash on GC 2025-11-09 18:01:57 -08:00
Martin Michelsen fb036cda37 fix null pointer dereference in episode 4 free play; closes #717 2025-11-09 16:01:10 -08:00
Martin Michelsen 136e2730de rename Ep4 test door 2025-11-09 16:00:41 -08:00
Martin Michelsen ae47d92016 update notes on delayed_switch_episode 2025-11-08 10:30:39 -08:00
Martin Michelsen b80ed0021b add method to override enemy EXP in quests 2025-11-07 22:53:36 -08:00
Martin Michelsen 1d11879142 demote IPSS unhandled frames to debug logs; closes #713 2025-11-07 21:10:40 -08:00
Martin Michelsen a122b27b1f don't use client's floor for 6x0A and 6x0B 2025-11-07 21:02:08 -08:00
Martin Michelsen cbba724ba1 add pause menu UI code 2025-11-07 20:25:42 -08:00
Martin Michelsen 2c51571ea4 add some misc codes 2025-11-07 14:28:21 -08:00
Martin Michelsen e1d774ce49 fix quest name in HTTP API; closes #714 2025-11-07 11:01:43 -08:00
Martin Michelsen b9e3973c76 document specialized item box format 2025-11-06 22:47:07 -08:00
Martin Michelsen c878093c5f ignore map_designate, etc. if floor number isn't valid 2025-11-06 21:18:42 -08:00
Martin Michelsen 7210441878 allow 6x17 for enemies and objects 2025-11-05 23:06:17 -08:00
Martin Michelsen 36eeee5641 clean up character load function 2025-11-05 22:29:43 -08:00
Martin Michelsen 8d2ffba3e1 add unit specific modifiers 2025-11-05 22:29:18 -08:00
Martin Michelsen 766d4e0c7a fix many edge cases in item name parsing 2025-11-05 21:45:15 -08:00
Martin Michelsen a99f552e7c fix synchro description in mag creation 2025-11-05 19:14:37 -08:00
Martin Michelsen 540a41a583 add Ep3 battle replay test 2025-11-05 09:02:22 -08:00
Martin Michelsen 8cb7d2b2fe fix $playrec behavior 2025-11-04 09:12:48 -08:00
Martin Michelsen 293f25d579 add print-free-supermap 2025-11-04 09:12:40 -08:00
Martin Michelsen 64763e76af fix floor tracking on $exit 2025-11-04 09:12:27 -08:00
Martin Michelsen 69b7e7f998 more object notes 2025-11-02 22:38:02 -08:00
Martin Michelsen 5579bce5d9 delete proxy_session_id 2025-11-02 20:40:30 -08:00
Martin Michelsen 0dd5e2ac10 use bit_cast now that resource_dasm is required 2025-11-02 18:19:06 -08:00
Martin Michelsen 155ed6bcf9 add $makeobj; update some object notes 2025-11-02 17:14:38 -08:00
Martin Michelsen 4e2f62bc73 update notes on TObjDoor 2025-10-30 10:07:56 -07:00
Martin Michelsen bf36a185a2 document TContainerAncient01 2025-10-29 21:27:15 -07:00
Martin Michelsen 4c4c54c536 document TOSparkMachine01 2025-10-29 19:50:09 -07:00
Martin Michelsen e79e6944df update more object notes 2025-10-29 10:27:48 -07:00
Martin Michelsen f6079e3078 update notes for TOSensorAncient01 2025-10-29 10:11:01 -07:00
Martin Michelsen 31b49a71fb add fast tekker patch 2025-10-28 22:35:38 -07:00
Martin Michelsen 83260d5037 fix $sound in lobby 2025-10-28 22:24:38 -07:00
Martin Michelsen 648da83aa1 add new patch file 2025-10-28 10:00:43 -07:00
Martin Michelsen adf1db92c7 fix Ep3 quest download test 2025-10-28 09:50:07 -07:00
Martin Michelsen 662ee48a64 add patch to show EXP gains from the server 2025-10-28 09:50:07 -07:00
Martin Michelsen 446b521898 fix player levels on HTTP server 2025-10-27 22:20:22 -07:00
Martin Michelsen d6db731149 fix uninitialized memory in IPStackSimulator 2025-10-27 22:20:14 -07:00
Martin Michelsen 9106a11be8 add test for Ep3 download quests and map loader 2025-10-27 22:19:58 -07:00
Martin Michelsen 7bc58a757e reimplement Episode 3 map categories 2025-10-26 23:07:47 -07:00
Martin Michelsen 27b5556e4b fix EnemyDamageSync crash on Xbox at connect time 2025-10-26 21:13:20 -07:00
Martin Michelsen b39b4197ed add 59NJ version of CallProtectedHandler 2025-10-25 22:15:06 -07:00
Martin Michelsen a99647d4c7 fix two off-parity offsets in BankSize patch 2025-10-25 21:57:03 -07:00
Repflez 10a6bafb2f Add 59NJ version of BankSize function 2025-10-25 21:57:03 -07:00
Martin Michelsen b4f7688b82 add some new AR codes 2025-10-22 23:41:41 -07:00
Martin Michelsen 08e6b882f3 fix incorrect game metadata logic in proxy
update
2025-10-22 23:30:26 -07:00
Martin Michelsen 4adc174674 merge Ep3 tables in handler-tables 2025-10-22 23:30:26 -07:00
Martin Michelsen 01b1f42bac add some Ep3 command notes 2025-10-22 19:47:23 -07:00
Martin Michelsen be4c7f80cb add tests for quest indexes and function compiler 2025-10-21 22:54:48 -07:00
Martin Michelsen 790363adb5 clean up some patches 2025-10-20 23:11:18 -07:00
Martin Michelsen 09b96a4a86 add BB-DR in handler-tables 2025-10-18 01:03:00 -07:00
Martin Michelsen 6ffa656ad4 implement Hunters Report item behavior 2025-10-18 01:03:00 -07:00
Martin Michelsen 3f2df68ac5 fix flags check on Xbox EnemyDamageSync 2025-10-18 01:03:00 -07:00
Martin Michelsen a7f2ecefe5 don't use under-stack space in EnemyDamageSync 2025-10-18 01:03:00 -07:00
Martin Michelsen 46c2260d0f use enums for difficulty and language; fix enemy state aliases; closes #694 2025-10-18 01:03:00 -07:00
Martin Michelsen 052dcf8c6e update 6xB6 notes 2025-10-18 01:03:00 -07:00
Martin Michelsen cd5863fcde fix $edit for names with spaces 2025-10-18 01:03:00 -07:00
Martin Michelsen 90de571457 document contents of BugFixes patch 2025-10-18 01:03:00 -07:00
Martin Michelsen d9d33c2d65 add patch downloader 2025-10-18 01:03:00 -07:00
Repflez 09962696b7 Assemble the fleti instruction properly 2025-10-17 08:47:04 -07:00
Martin Michelsen d143cbb461 document GC RareItemNotifications patch 2025-10-12 09:48:09 -07:00
Martin Michelsen db7f7abfc4 update HTML drop table notes in command info 2025-10-12 09:48:09 -07:00
Martin Michelsen 6ba92d3a7a skip EXP computation for Level 200 characters 2025-10-12 09:48:09 -07:00
Martin Michelsen 36a1e0dfae fix common tables on GC NTE 2025-10-12 09:48:09 -07:00
Martin Michelsen 47f7e71ae9 display quest names in client's native language in game info window 2025-10-12 09:48:09 -07:00
Martin Michelsen c2008f1f9c handle Ep1&2 NTE protected commands properly 2025-10-12 09:48:09 -07:00
Martin Michelsen 3c32a66064 hide section ID for empty persistent games 2025-10-12 09:48:09 -07:00
Martin Michelsen 41026fbd93 add ep3 auction code 2025-10-12 09:48:09 -07:00
nolrinale d49750aa02 Added missing Coren map files 2025-10-10 21:26:57 -07:00
Martin Michelsen 54f309030e fix exact rate hint in drop tables 2025-10-08 21:37:30 -07:00
Martin Michelsen 093c25fce4 include DAR in generated drop tables 2025-10-08 21:29:55 -07:00
Martin Michelsen a777dc8236 make AsyncEvent resumption faster 2025-10-08 21:29:36 -07:00
Martin Michelsen 4044e4e5a6 fix battle table + $exit edge case 2025-10-05 20:38:44 -07:00
Martin Michelsen 036b4e9456 assign a specific_version for PC NTE 2025-10-05 11:27:59 -07:00
Martin Michelsen 4074530a71 disable EXP share during battle and challenge quests 2025-10-05 11:02:56 -07:00
Martin Michelsen 31eedd7e7e work around 6xD9 client bug 2025-10-05 10:49:07 -07:00
Martin Michelsen df2dfd21e3 fix 88 command during loading on proxy 2025-10-05 10:49:07 -07:00
Martin Michelsen 00b0f71bf4 update some notes 2025-10-05 10:47:20 -07:00
Martin Michelsen 1450a5acd3 allow 6x25 to overwrite slots on all versions 2025-10-04 09:55:00 -07:00
Martin Michelsen 2a138ea0b6 update some command notes 2025-10-04 09:54:37 -07:00
Martin Michelsen 2534ff37de fix potential race in socket closure 2025-10-04 09:54:21 -07:00
Martin Michelsen d61cb1106d allow $unset to remove assist cards too 2025-10-04 09:53:26 -07:00
Martin Michelsen d5f0c6aceb fix shared bank creation 2025-10-03 08:41:45 -07:00
Martin Michelsen 2bab3f2f8f fix episode 4 boss drops
Docker / Build (push) Has been cancelled
2025-09-30 23:19:44 -07:00
Martin Michelsen fdd0bfea08 rewrite quest metadata indexing
- split ep3 download quests from quest index
- fix Ep3 NTE download quests
- automatically detect battle/challenge params and area remaps
2025-09-28 23:26:14 -07:00
Martin Michelsen 48c225366f rewrite trade sequence 2025-09-26 21:45:24 -07:00
Martin Michelsen 0d88253334 add deadzone hint to font bitmap decoder 2025-09-26 21:45:04 -07:00
Martin Michelsen d7b17aa383 update some notes 2025-09-26 21:44:44 -07:00
Martin Michelsen ba131ab94a handle 6xE2 full inventory case 2025-09-25 21:20:48 -07:00
Martin Michelsen 648d9c5164 remove leader check on 6x17 2025-09-25 09:06:53 -07:00
Martin Michelsen 60487daf6f fix 6x17 checks for Vol Opt arena 2025-09-24 21:02:05 -07:00
Martin Michelsen e0c43836b3 add English AR code for Ep1&2 Trial 2025-09-22 18:05:42 -07:00
Martin Michelsen 719a403b1d show dmc patch in patches menu 2025-09-22 18:05:42 -07:00
Martin Michelsen 6f88c3d31a fix size field in 6xDD 2025-09-22 09:20:45 -07:00
Martin Michelsen 7114798e69 fix size check on 6xDD extension 2025-09-21 17:18:42 -07:00
Martin Michelsen 65384435a3 add extension for fractional EXP multipliers on BB 2025-09-21 13:16:28 -07:00
Martin Michelsen 4236ff62b1 add ep1 boss rush test 2025-09-19 09:16:28 -07:00
Martin Michelsen 277be9bcd6 obscure security updates 2025-09-18 23:48:14 -07:00
Martin Michelsen 9493e2d3e7 add some ar codes 2025-09-18 21:51:55 -07:00
Martin Michelsen 16b15162d5 add decrypt_pr1_data 2025-09-16 08:39:19 -07:00
Martin Michelsen 9854b93d02 support AFS tables in convert-common-item-set 2025-09-16 08:39:12 -07:00
Martin Michelsen d02ab1e7a5 add node about D5 non-repeatability on BB 2025-09-16 08:38:49 -07:00
Martin Michelsen e0c8ca677f add Windows build outline 2025-09-14 21:03:42 -07:00
Martin Michelsen 2cea44f790 add Ep3 JP subcommands in handler-tables 2025-09-14 13:37:39 -07:00
Martin Michelsen fb783034bc handle incorrect flags in 10 command 2025-09-14 13:04:42 -07:00
Martin Michelsen 40a6f49b29 fix crossplay challenge restart logic 2025-09-13 22:38:32 -07:00
Martin Michelsen dea0ac99c3 update some command notes 2025-09-13 22:38:27 -07:00
Martin Michelsen 24cf8e73c6 fix incorrect symlink on q080-gcn 2025-09-12 23:50:47 -07:00
Martin Michelsen c301a921e6 assume all GC NTE quests are Episode 1 2025-09-12 23:50:47 -07:00
Martin Michelsen 22d7825ba3 handle devil's/demon's in EnemyDamageSync 2025-09-12 23:45:51 -07:00
Martin Michelsen 526bfb64e5 fix memcpy call that gcc is unhappy with 2025-09-11 16:17:38 -07:00
Martin Michelsen 55cbf6e20b fix out-of-bounds access in 6x46, etc. 2025-09-11 10:14:39 -07:00
Martin Michelsen 0b86ffb227 fix use-after-free in AsyncPromise 2025-09-11 10:14:39 -07:00
Matt Swift e28596c825 Add Aberrant Grove custom quest 2025-09-11 09:31:14 -07:00
Matt Swift 716676b87d Add GC NTE quest symlinks 2025-09-11 09:31:14 -07:00
Martin Michelsen 5ca0265c37 remove unused argument 2025-09-10 22:10:47 -07:00
Martin Michelsen c7a0873ca8 fix cross-floor commands in EnemyDamageSync 2025-09-10 21:15:22 -07:00
Martin Michelsen b1d51cdbbe fix visibility for some patches 2025-09-09 23:18:09 -07:00
Martin Michelsen 5a7151bc63 minor proxy bugfixes 2025-09-09 23:18:01 -07:00
Martin Michelsen 49d861919f update some notes 2025-09-06 22:53:59 -07:00
Martin Michelsen 3f20c4239f remove cmake from explicit-install list in GH Actions script 2025-09-02 21:37:58 -07:00
Martin Michelsen 038f306661 update notes on some 6xB5 subcommands 2025-09-02 21:34:39 -07:00
Martin Michelsen 0575f3c9cf fix windows build 2025-09-02 21:34:19 -07:00
Martin Michelsen e37307acb3 fix bank load function when index not set 2025-08-29 18:49:32 -07:00
Martin Michelsen 4b32b41183 add note in readme about xbox connectivity 2025-08-29 10:33:51 -07:00
Martin Michelsen c8f8a6f65b clean up legacy format notes 2025-08-26 23:54:56 -07:00
Martin Michelsen 0c93275e88 describe some esoteric NTE and 11/2000 commands 2025-08-24 22:47:33 -07:00
Martin Michelsen c44ab27c7e update some command notes 2025-08-24 18:17:39 -07:00
Martin Michelsen 3f09a7b57b add version checks around bank access 2025-08-24 17:28:26 -07:00
Martin Michelsen 0b4d5b2f89 add BB BankSize patch 2025-08-22 22:39:32 -07:00
Martin Michelsen 45824b46fe support per-quest common and rare tables 2025-08-22 14:09:41 -07:00
Martin Michelsen e78f3142e3 update comment on send_lobby_list 2025-08-21 10:37:35 -07:00
Martin Michelsen 4166149841 add player check in HungryMagSound 2025-08-19 23:18:03 -07:00
Martin Michelsen 45131dabc0 fix dice range parsing in create-tournament 2025-08-19 20:22:41 -07:00
Martin Michelsen b235644575 expand leaf containers in text set serialization 2025-08-15 12:54:13 -07:00
Martin Michelsen 377d8beac3 implement $switchchar command 2025-08-14 23:44:16 -07:00
Martin Michelsen 16bff52575 update comments in expand_rate 2025-08-13 11:51:35 -07:00
Martin Michelsen 49fb7eba60 fix $bank when used with MoreSaveSlots 2025-08-13 11:42:20 -07:00
Martin Michelsen 00b46d7161 update game_flags notes 2025-08-13 11:42:07 -07:00
Martin Michelsen 5bea9d3a2b add warning about crossplay + stack limits 2025-08-07 00:00:25 -07:00
Martin Michelsen a9dcd4b87e enforce stack limits when loading BB character data
Docker / Build (push) Has been cancelled
2025-08-06 21:23:30 -07:00
Martin Michelsen 5c84581978 add names in show-battle-params 2025-08-06 21:03:20 -07:00
Martin Michelsen ab38a58e39 mention address config in readme 2025-08-06 21:02:30 -07:00
Martin Michelsen d430112a94 support chat shell command for non-proxy clients 2025-07-27 14:18:48 -07:00
Martin Michelsen 0cf59f874d use remote_addr for SocketChannel in send_reconnect 2025-07-26 16:54:13 -07:00
Martin Michelsen bf028ed0f6 fix data2 handling in 30 command from GetExtendedPlayerInfo 2025-07-24 21:37:36 -07:00
Martin Michelsen 1ecc41dea9 format show-item-tables output more cleanly 2025-07-24 18:38:14 -07:00
Justin Schwartz 648e15a016 document the original unit stars random state 2025-07-22 23:18:53 -07:00
Martin Michelsen 1729edc1d2 add dynamic switching in EnemyDamageSync 2025-07-22 00:27:21 -07:00
Martin Michelsen bbcc03f832 improve CommonItemSet JSON parser/serializer 2025-07-20 22:30:04 -07:00
Martin Michelsen 6827229c83 refine 6x79 a bit 2025-07-20 22:30:01 -07:00
Martin Michelsen 60291993b6 add configurable min levels for non-BB; closes #666 2025-07-11 17:57:39 -07:00
Martin Michelsen 118512ebb2 fix websocket timeout 2025-07-10 09:38:31 -07:00
Martin Michelsen ae9eaccd29 fix disconnect for websocket clients 2025-07-08 20:09:20 -07:00
Martin Michelsen 3025420aea fix headers in show-item-tables 2025-07-08 20:09:04 -07:00
Martin Michelsen 3c4ad43e71 add belra arm bug fix 2025-07-06 23:25:03 -07:00
Martin Michelsen 9e02b6c666 add $sound command 2025-07-06 21:41:31 -07:00
Martin Michelsen fe435c13d3 fix local address detection 2025-07-06 20:48:44 -07:00
Martin Michelsen 3b5145880c fix $loadchar description in readme 2025-07-06 15:35:56 -07:00
Martin Michelsen d965ff5031 add stat boosts to ItemPMT formatting 2025-07-06 13:57:31 -07:00
Martin Michelsen 22a89deb8b fix save game data timer 2025-07-05 20:27:24 -07:00
Martin Michelsen c9ba61a4b0 fix NAME_ONLY for units with kill counts 2025-07-05 19:54:30 -07:00
Martin Michelsen 0cdf2784cc fix text alignment in MoreSaveSlots 2025-07-05 19:49:20 -07:00
Martin Michelsen 76a948a45d fix unused variable 2025-07-03 00:27:38 -07:00
Martin Michelsen fd39a89957 fix BB proxy bugs 2025-07-02 21:14:32 -07:00
Martin Michelsen 0a5065707c use new phosg::Image class 2025-07-01 09:56:42 -07:00
Martin Michelsen 072e647c7b update readme 2025-06-29 11:22:40 -07:00
Martin Michelsen 148db03a9a fix copy-paste error in MoreSaveSlots patch 2025-06-24 20:53:33 -07:00
Martin Michelsen cff5ad23fc fix scroll bar setup in MoreSaveSlots 2025-06-24 20:12:49 -07:00
Martin Michelsen 3e174b7397 add notes on TObjSinBoard 2025-06-24 20:12:33 -07:00
Martin Michelsen e9bf51f3f7 save all fields when applying npc skins 2025-06-24 20:12:24 -07:00
Martin Michelsen 28ab1bea9c add IPv6 support in proxy 2025-06-17 01:19:26 -07:00
Martin Michelsen 923cc4ebb0 add missing xbox includes 2025-06-16 19:22:38 -07:00
Martin Michelsen e24a0e3c40 decrypt Ep3 player config at load time 2025-06-16 00:30:53 -07:00
Martin Michelsen a857cc9d03 update some notes 2025-06-16 00:10:50 -07:00
Martin Michelsen 8746b544b6 describe the PCv2-exclusive quest opcodes 2025-06-14 20:40:53 -07:00
Martin Michelsen ccd5baedf1 add notes from BB trial edition 2025-06-14 12:00:36 -07:00
Martin Michelsen 9621e89cd7 add notes and support for final PCv2 version 2025-06-14 00:35:56 -07:00
Martin Michelsen 3844c9881c add AccurateKillCount patch 2025-06-12 18:49:38 -07:00
Martin Michelsen 6999694f89 rewrite 6xE4 logic 2025-06-12 01:27:54 -07:00
Martin Michelsen 54acd931da use .label/.address in xbox client functions 2025-06-09 10:00:38 -07:00
Martin Michelsen 9bc9e219b5 add patch for disabling Xbox save signature validation 2025-06-07 19:32:21 -07:00
Martin Michelsen e8b2765a71 add xbox disk file formats 2025-06-07 19:26:34 -07:00
Martin Michelsen d4bc880018 make $killcount work for units too 2025-06-07 09:53:56 -07:00
Martin Michelsen c1a2742617 update readme 2025-06-07 09:53:35 -07:00
Martin Michelsen ebaeb2f70a update docs for find_inventory_item quest opcode 2025-06-05 21:33:51 -07:00
Martin Michelsen 0366e36edb add Xbox-US1 quest handlers 2025-06-05 20:59:41 -07:00
Martin Michelsen a0f52f01bb use 6x2F for infinite HP 2025-06-04 00:18:57 -07:00
Martin Michelsen bee4c55446 make client functions parameterizable by version 2025-06-04 00:16:43 -07:00
Martin Michelsen 1a6b26e56b add text-only matching in AddressTranslator 2025-06-03 09:59:19 -07:00
Martin Michelsen 1047d089d5 fix 6x0B error message 2025-05-31 23:15:23 -07:00
Martin Michelsen 2d6096cfda fix $savechar on BB 2025-05-31 23:15:00 -07:00
Martin Michelsen 7cbd9402d0 fix CallNativeFunctionGC
Docker / Build (push) Has been cancelled
2025-05-31 15:15:03 -07:00
Martin Michelsen 0396337994 fix inventory/bank debug messages 2025-05-31 15:14:04 -07:00
Martin Michelsen 6fbc0829ae add patch to replace Pinz shop cards 2025-05-31 10:56:01 -07:00
Martin Michelsen 4f41cbc9ce fix description generated in $item command 2025-05-31 10:07:11 -07:00
Martin Michelsen d1e6d75d70 fix TethVer detection hack 2025-05-31 10:04:09 -07:00
Martin Michelsen 067f2439ca make redirect wait apply to SocketChannels as well 2025-05-31 09:34:09 -07:00
Martin Michelsen 2d2edbd7be fix ping exception handler 2025-05-31 09:29:01 -07:00
Vargur f5f457aa6f Fix HTTP endpoint logic: remove incorrect negation in rare-tables path check
The !req.path.starts_with( was causing every subsequent command to be processed as a rare-tables substring command.
2025-05-30 19:29:52 -07:00
Martin Michelsen aabbafb749 fix game flag translation across v2/v3 boundary 2025-05-28 22:01:54 -07:00
Martin Michelsen e72e37f713 implement extended $infhp features on proxy server; closes #501 2025-05-27 19:34:47 -07:00
Martin Michelsen f884893b18 reprioritize to-do list 2025-05-27 19:34:25 -07:00
Martin Michelsen c74c0e2250 fix conditions 2025-05-26 23:52:43 -07:00
Martin Michelsen 5f4d2ec891 complete implementation of $checkchar and make slot count configurable; closes #645 2025-05-26 21:55:19 -07:00
Martin Michelsen 33b0ab3ed3 improve BB proxy functionality 2025-05-26 18:56:23 -07:00
Martin Michelsen 2e158a1df8 fix Programs menu item in tests
Docker / Build (push) Has been cancelled
2025-05-26 15:08:26 -07:00
Martin Michelsen 6a89f18580 make logging less verbose 2025-05-26 14:51:43 -07:00
Martin Michelsen b3e757dcdc add Windows platform wrapper 2025-05-26 14:20:20 -07:00
Martin Michelsen 9c675a14ab fix CI build steps 2025-05-26 14:17:47 -07:00
Martin Michelsen cc99050964 switch to coroutine execution model 2025-05-26 14:11:38 -07:00
Martin Michelsen f65b1f1c14 make login faster with MoreSaveSlots 2025-04-25 08:56:19 -07:00
Martin Michelsen 1ad2c47444 make $exit work without a quest loaded on most versions 2025-04-24 18:58:20 -07:00
Martin Michelsen ebef2f2bd1 add aliases for $arrow command 2025-04-21 19:51:00 -07:00
Martin Michelsen afa23f03c7 describe how TObjNpcEnemy works 2025-04-19 11:01:31 -07:00
Martin Michelsen 9d7b6c6341 update some notes 2025-04-19 11:00:33 -07:00
Martin Michelsen 4199f7bb23 update comments on MoreSaveSlots patch 2025-04-13 19:11:12 -07:00
Martin Michelsen 140d488239 support more BB save slots; add client patch 2025-04-12 23:35:00 -07:00
Martin Michelsen 22e9314e18 fix some notes 2025-04-07 23:49:08 -07:00
anzz1 c8a3b3ba31 add 59NL version of Palette client patch
Enables the alternate action palette for number keys
Credits to Soly from Blue Burst Patch Project
2025-04-05 21:42:09 -07:00
Martin Michelsen 8b7e4014ae fix quest max players check; closes #636 2025-04-05 14:11:21 -07:00
Martin Michelsen 13b94e7ba1 minor cleanup in map entity notes 2025-04-05 11:38:04 -07:00
Martin Michelsen ab2a8d5fa9 document item/level table format commands 2025-04-05 11:38:04 -07:00
Martin Michelsen a01d8206e1 add outline of ep4 enemy args 2025-04-04 14:23:43 -07:00
Martin Michelsen 61570a2563 add version/area flags to object/enemy defs 2025-04-04 00:39:57 -07:00
Martin Michelsen 822c0e0670 more ep2 enemy notes 2025-04-03 10:39:40 -07:00
Martin Michelsen c5b5ab3815 fixes after compiler upgrade 2025-04-03 10:38:55 -07:00
anzz1 b28e9a5d54 [BB] unitxt_shop_e typo fix
"This item will be delete. OK?" -> "This item will be deleted. OK?"
2025-03-31 20:19:30 -07:00
anzz1 e5e61d189c [BB] Correct unitxt_shop_e.prs, add WS files for reference
Corrected unitxt_shop_e.prs: Fixed typo 'Mestea' -> 'Meseta', added missing Present Counter text lines.
Added WS files to notes for reference (from gsl)
2025-03-31 20:19:30 -07:00
anzz1 8b35d07fc9 59NL DrawDistance client function (beta)
Currently beta quality, map objects that fade like boxes, and Pioneer's
background billboards and elevators still have regular draw distance.
TODO: 90% of stuff is included, bring home the last 10%.
2025-03-31 20:18:39 -07:00
anzz1 2f462d391e Update HungryMagSound.59NL.patch.s
Change the hungry mag sound effect from "message received" to "mag feed"
2025-03-31 20:15:38 -07:00
Martin Michelsen 09d3b90169 describe some Ep2 enemies 2025-03-30 16:21:02 -07:00
Martin Michelsen a329db3036 use new phosg hash interface 2025-03-30 12:57:55 -07:00
Martin Michelsen 711fa742be describe ep1/ep2 bosses 2025-03-29 21:51:05 -07:00
Martin Michelsen d9c549bef5 add $whatene command 2025-03-29 21:50:11 -07:00
Martin Michelsen e0d1db0363 handle DARK_GUNNER_CONTROL properly 2025-03-29 16:32:53 -07:00
Martin Michelsen 1723a4152c describe Ruins enemies 2025-03-29 16:19:35 -07:00
Martin Michelsen c212b2987c describe Mines enemies 2025-03-28 22:36:19 -07:00
Martin Michelsen 488a5b201e more enemy type docs 2025-03-27 23:38:46 -07:00
Martin Michelsen 4770297cd0 document some things in ItemPMT 2025-03-27 23:38:37 -07:00
Martin Michelsen 3297df580a port the menu code to all versions 2025-03-26 23:22:03 -07:00
Martin Michelsen 936b914cbc start describing enemy types 2025-03-26 23:07:39 -07:00
Martin Michelsen ad51dcf16f describe remaining object types 2025-03-25 17:37:03 -07:00
Martin Michelsen c8f330e2c8 describe some ep4 objects 2025-03-25 11:30:11 -07:00
Martin Michelsen 6467693df9 describe a few Ep4 objects 2025-03-24 23:45:22 -07:00
Martin Michelsen 07716fd301 describe Ep3 map objects 2025-03-24 18:33:59 -07:00
Martin Michelsen b30cd3bb8e load Ep3 Morgue map 2025-03-24 18:29:09 -07:00
Martin Michelsen a4a8389add describe remaining Ep2 objects 2025-03-24 15:24:19 -07:00
Martin Michelsen 7f2fca3a79 add notes for lobby, temple, and spaceship objects 2025-03-23 21:58:37 -07:00
Martin Michelsen cfea8a2712 more object notes 2025-03-22 12:52:31 -07:00
Martin Michelsen 3e59f9a91e brace-init in vector math 2025-03-22 00:05:50 -07:00
Martin Michelsen 69edba036e add $whatobj command 2025-03-21 23:58:49 -07:00
Martin Michelsen ca1dc6ad7d more object notes 2025-03-21 23:58:49 -07:00
Martin Michelsen dcd8d3b650 more object notes 2025-03-18 23:45:38 -07:00
Martin Michelsen 1bc668f72f disable non-resource-file CI build 2025-03-18 20:58:44 -07:00
Martin Michelsen 52d019a321 make BB proxy's Save Files option generate .psochar files 2025-03-18 20:55:04 -07:00
Martin Michelsen 02c3d35d78 more object comments 2025-03-18 20:55:04 -07:00
Martin Michelsen 6328453d38 make resource_file required 2025-03-18 18:59:16 -07:00
Martin Michelsen 595675df20 refine object comments 2025-03-18 00:49:55 -07:00
Martin Michelsen 4489bca037 document more map object types 2025-03-17 22:57:24 -07:00
Martin Michelsen 333b62b884 rewrite dat constructor tables 2025-03-16 23:09:49 -07:00
Martin Michelsen f06b07a7c4 add note on F829 2025-03-16 12:20:13 -07:00
Martin Michelsen b52a2e4a5b refine some ItemPMT structures 2025-03-16 12:20:13 -07:00
Martin Michelsen 26c3a87a73 distinguish hidden-name ES weapons 2025-03-16 12:20:13 -07:00
Martin Michelsen 73eef4815b update 6x9A description 2025-03-16 12:20:13 -07:00
Martin Michelsen d85737b1a7 update release instructions; closes #621 2025-03-14 23:36:03 -07:00
Martin Michelsen ed05bbe2e3 write gc/xbox versions of NoRareSelling 2025-03-14 23:23:39 -07:00
Martin Michelsen f0c492abea remove patches menu in favor of patch switches; closes #623 2025-03-14 23:20:09 -07:00
Martin Michelsen 2cff04943f add player_count in 83 command struct 2025-03-14 23:20:09 -07:00
anzz1 1df7b821e8 cleanup 59NL NoSellRare client patch 2025-03-14 21:17:54 -07:00
anzz1 5fb842761d add 59NL version of NoSellRare client patch
Prevents you from accidentally selling rares and untekked weapons to vendor
Credits to Soly from Blue Burst Patch Project
2025-03-14 21:17:54 -07:00
Martin Michelsen 3cddb99c20 use IP stack sim address in HTTP responses if client is on tapserver 2025-03-09 23:27:07 -07:00
1931 changed files with 406232 additions and 268091 deletions
+2 -4
View File
@@ -16,19 +16,18 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
with_resource_file: ["true", "false"]
steps:
- uses: actions/checkout@v4
- name: Install libraries (Linux)
if: ${{ matrix.os == 'ubuntu-latest' }}
run: sudo apt-get install -y libevent-dev
run: sudo apt-get install -y cmake libasio-dev
- name: Install libraries (macOS)
if: ${{ matrix.os == 'macos-latest' }}
run: |
brew install libevent
brew install asio libiconv
cat << EOF > nproc
#!/bin/sh
@@ -47,7 +46,6 @@ jobs:
sudo make install
- name: Install resource_file
if: ${{ matrix.with_resource_file == 'true' }}
run: |
git clone https://github.com/fuzziqersoftware/resource_dasm.git
cd resource_dasm
+26 -11
View File
@@ -2,43 +2,58 @@
.DS_Store
# Build products
src/Revision.cc
newserv
newserv.exe
src/Revision.cc
# CMake files
build
cmake_install.cmake
CMakeCache.txt
CMakeFiles
CTestTestFile.cmake
CTestTestfile.cmake
CTestTestFile.cmake
install_manifest.txt
Makefile
Testing
# Files modified by the user and/or server that don't have defaults
system/config.json
system/ep3/battle-records/*.mzr
system/ep3/battle-records/*.mzrd
system/ep3/tournament-state.json
system/licenses.nsi
system/licenses/*.json
system/patch-bb/.metadata-cache.json
system/patch-pc/.metadata-cache.json
system/players/*.nsa
system/players/*.nsc
system/players/*.psobank
system/players/*.psocard
system/players/*.psochar
system/players/*.psosys
system/players/*.psocard
system/players/*.nsc
system/players/*.nsa
system/teams/*.json
system/teams/*.bmp
system/patch-pc/.metadata-cache.json
system/patch-bb/.metadata-cache.json
system/teams/*.json
# Files fuzziqersoftware uses that don't make sense to be committed to the main
# repository
*.dec
*.WIP-s
files
make_release.py
notes-private
old-khyller
old-newserv
release
release.zip
system/patch-bb/data
system/patch-bb/psobb.pat
all-quests
system/dol
system/patch-bb/data
system/client-functions/Debug-Private
system/config.2.json
system/ep3/banners
system/ep3/cardtex
system/ep3/cardtex-trial
system/players
system/quests/includes
system/quests/private
.vscode
+25 -42
View File
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.22)
set(CMAKE_POLICY_DEFAULT_CMP0110 NEW)
@@ -19,19 +19,15 @@ endif()
# Library search
find_path (LIBEVENT_INCLUDE_DIR NAMES event.h)
find_library (LIBEVENT_LIBRARY NAMES event)
find_library (LIBEVENT_CORE NAMES event_core)
find_library (LIBEVENT_PTHREADS NAMES event_pthreads)
set (LIBEVENT_INCLUDE_DIRS ${LIBEVENT_INCLUDE_DIR})
set (LIBEVENT_LIBRARIES
${LIBEVENT_LIBRARY}
${LIBEVENT_CORE}
${LIBEVENT_PTHREADS})
find_path(ASIO_INCLUDE_DIR NAMES asio.hpp HINTS "${WINDOWS_ENV}/include" REQUIRED)
if(WIN32)
find_path(Iconv_INCLUDE_DIRS NAMES iconv.h HINTS "${WINDOWS_ENV}/include" REQUIRED)
find_library(Iconv_LIBRARIES NAMES iconv HINTS "${WINDOWS_ENV}/lib" REQUIRED)
else()
find_package(Iconv REQUIRED)
endif()
find_package(phosg REQUIRED)
find_package(Iconv REQUIRED)
find_package(resource_file QUIET)
find_package(resource_file REQUIRED)
@@ -54,10 +50,12 @@ add_custom_target(
set(SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/src/Revision.cc
src/Account.cc
src/AddressTranslator.cc
src/AFSArchive.cc
src/AsyncHTTPServer.cc
src/AsyncUtils.cc
src/BattleParamsIndex.cc
src/BMLArchive.cc
src/CatSession.cc
src/Channel.cc
src/ChatCommands.cc
src/ChoiceSearch.cc
@@ -80,9 +78,9 @@ set(SOURCES
src/Episode3/RulerServer.cc
src/Episode3/Server.cc
src/Episode3/Tournament.cc
src/EventUtils.cc
src/FileContentsCache.cc
src/FunctionCompiler.cc
src/GameServer.cc
src/GSLArchive.cc
src/HTTPServer.cc
src/ImageEncoder.cc
@@ -94,8 +92,8 @@ set(SOURCES
src/ItemData.cc
src/ItemNameIndex.cc
src/ItemParameterTable.cc
src/ItemTranslationTable.cc
src/Items.cc
src/ItemTranslationTable.cc
src/LevelTable.cc
src/Lobby.cc
src/Loggers.cc
@@ -103,26 +101,25 @@ set(SOURCES
src/Map.cc
src/Menu.cc
src/NetworkAddresses.cc
src/PatchDownloadSession.cc
src/PatchFileIndex.cc
src/PatchServer.cc
src/PlayerFilesManager.cc
src/PlayerInventory.cc
src/PlayerSubordinates.cc
src/PPKArchive.cc
src/ProxyCommands.cc
src/ProxyServer.cc
src/ProxySession.cc
src/PSOEncryption.cc
src/PSOGCObjectGraph.cc
src/PSOProtocol.cc
src/Quest.cc
src/QuestMetadata.cc
src/QuestScript.cc
src/RareItemSet.cc
src/ReceiveCommands.cc
src/ReceiveSubcommands.cc
src/ReplaySession.cc
src/Revision.cc
src/SaveFileFormats.cc
src/SendCommands.cc
src/Server.cc
src/ServerShell.cc
src/ServerState.cc
src/ShellCommands.cc
@@ -135,19 +132,14 @@ set(SOURCES
src/WordSelectTable.cc
)
if(resource_file_FOUND)
set(SOURCES ${SOURCES} src/AddressTranslator.cc)
endif()
add_executable(newserv ${SOURCES})
target_include_directories(newserv PUBLIC ${LIBEVENT_INCLUDE_DIR} ${Iconv_INCLUDE_DIRS})
target_link_libraries(newserv phosg::phosg ${LIBEVENT_LIBRARIES} ${Iconv_LIBRARIES} pthread)
if(resource_file_FOUND)
target_compile_definitions(newserv PUBLIC HAVE_RESOURCE_FILE)
target_link_libraries(newserv resource_file::resource_file)
message(STATUS "resource_file found; enabling patch support")
else()
message(WARNING "resource_file not found; disabling patch support")
target_include_directories(newserv PUBLIC ${ASIO_INCLUDE_DIR} ${Iconv_INCLUDE_DIRS})
target_link_libraries(newserv phosg::phosg ${Iconv_LIBRARIES} resource_file::resource_file)
if (WIN32)
target_compile_definitions(newserv PUBLIC WINVER=0x0A00 _WIN32_WINNT=0x0A00)
target_compile_options(newserv PRIVATE -Wa,-mbig-obj -Wno-mismatched-new-delete)
target_link_options(newserv PRIVATE -static -static-libgcc -static-libstdc++)
target_link_libraries(newserv ws2_32 mswsock bcrypt iphlpapi)
endif()
add_dependencies(newserv newserv-Revision-cc)
@@ -170,15 +162,6 @@ foreach(LogTestCase IN ITEMS ${LogTestCases})
COMMAND ${CMAKE_BINARY_DIR}/newserv --replay-log=${LogTestCase} --config=${CMAKE_SOURCE_DIR}/tests/config.json)
endforeach()
if(resource_file_FOUND)
foreach(LogRDTestCase IN ITEMS ${LogRDTestCases})
add_test(
NAME ${LogRDTestCase}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMAND ${CMAKE_BINARY_DIR}/newserv --replay-log=${LogRDTestCase} --config=${CMAKE_SOURCE_DIR}/tests/config.json)
endforeach()
endif()
file(GLOB ScriptTestCases ${CMAKE_SOURCE_DIR}/tests/*.test.sh)
foreach(ScriptTestCase IN ITEMS ${ScriptTestCases})
+8 -8
View File
@@ -11,7 +11,7 @@ RUN apt update && apt install -y --no-install-recommends \
make \
cmake \
g++ \
libevent-dev \
libasio-dev \
zlib1g-dev
# ---
@@ -29,13 +29,13 @@ RUN git clone --depth 1 -b ${PHOSG_TARGET} https://github.com/fuzziqersoftware/p
sudo make install
RUN \
if [ "$BUILD_RESOURCE_DASM" = "true" ] ; then \
if [ "$BUILD_RESOURCE_DASM" = "true" ] ; then \
git clone --depth 1 -b ${RESOURCE_DASM_TARGET} https://github.com/fuzziqersoftware/resource_dasm.git && \
cd resource_dasm && \
cmake . && \
make -j$(nproc) && \
sudo make install \
; fi
; fi
# ---
@@ -53,10 +53,10 @@ RUN cmake -B $PWD/build -DCMAKE_BUILD_TYPE=${BUILD_TYPE} && \
sudo make -C build install
RUN \
if [ "$BUILD_STRIP" = "true" ] ; then \
strip /usr/local/lib/*.a && \
strip /usr/local/bin/* \
; fi
if [ "$BUILD_STRIP" = "true" ] ; then \
strip /usr/local/lib/*.a && \
strip /usr/local/bin/* \
; fi
# ---
@@ -72,7 +72,7 @@ RUN cp -f system/config.example.json system/config.json && \
FROM ${BASE_IMAGE} AS final
RUN apt update && apt install -y --no-install-recommends \
libevent-dev \
libasio-dev \
&& rm -rf /var/lib/apt/lists/* /var/cache/apt/*
WORKDIR /newserv
+190 -133
View File
@@ -12,8 +12,8 @@ See TODO.md for a list of known issues and future work I've curated, or go to th
* Background
* [History](#history)
* [Other server projects](#other-server-projects)
* [Developer information](#developer-information)
* [Using newserv in other projects](#using-newserv-in-other-projects)
* [Contributing to newserv](#contributing-to-newserv)
* [Compatibility](#compatibility)
* Setup
* [Server setup](#server-setup)
@@ -26,7 +26,7 @@ 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-client-functions-and-dol-files)
* [Memory patches, client functions, and DOL files](#memory-patches-and-client-functions)
* [Using newserv as a proxy](#using-newserv-as-a-proxy)
* [Chat commands](#chat-commands)
* [REST API](#rest-api)
@@ -46,7 +46,7 @@ For a while it was essentially necessary to use a proxy to go online at all, so
<img align="left" src="static/s-aeon.png" /> Sometime in 2006 or 2007, I abandoned Khyller and rebuilt the entire thing from scratch, resulting in Aeon. Aeon was substantially cleaner in code than Khyller but still fairly hard to work with, and it lacked a few of the more arcane features I had originally written (for example, the ability to convert any quest into a download quest). In addition, the code still had some stability problems... it turns out that Aeon's concurrency primitives were simply incorrect. I had derived the concept of a mutex myself, before taking any real computer engineering classes, but had implemented it incorrectly. I made the race window as small as possible, but Aeon would still randomly crash after running seemingly fine for a few days.
At the time of its inception, Aeon was also called newserv, and you may find some beta releases floating around the Internet with filenames like `newserv-b3.zip`. I had released betas 1, 2, and 3 before I released the entire source of beta 5 and stopped working on the project when I went to college. This was around the time when I switched from writing software primarily on Windows to primarily on macOS and Linux, so Aeon beta 5 was the last server I wrote that specifically targeted Windows. (newserv, which you're looking at now, is a bit tedious to compile on Windows but does work.)
At the time of its inception, Aeon was also called newserv, and you may find some beta releases floating around the Internet with filenames like `newserv-b3.zip`. I had released betas 1, 2, and 3 before I released the entire source of beta 5 and stopped working on the project when I went to college. This was around the time when I switched from writing software primarily on Windows to primarily on macOS and Linux, so Aeon beta 5 was the last server I wrote that specifically targeted Windows. (newserv, which you're looking at now, is difficult to compile on Windows but does work.)
<img align="left" src="static/s-newserv.png" /> After a long hiatus from PSO and much professional and personal development in my technical abilities, I was reminiscing sometime in October 2018 by reading my old code archives. Somehow inspired when I came across Aeon, I spent a weekend and a couple more evenings rewriting the entire project again, cleaning up ancient patterns I had used eleven years ago, replacing entire modules with simple STL containers, and eliminating even more support files in favor of configuration autodetection. The code is now suitably modern and stable, and I'm not embarrassed by its existence, as I am by Aeon beta 5's source code and my archive of Khyller (which, thankfully, no one else ever saw).
@@ -54,42 +54,49 @@ At the time of its inception, Aeon was also called newserv, and you may find som
Independently of this project, there are many other PSO servers out there. Those that I know of that are (or were) public are listed here in approximate chronological order:
* (Early 2000s) **[Schtserv](https://schtserv.com/)**: The first public-access PSO server; written in Delphi by Schthack. Still active and popular as of early 2025. Schtserv is also the only other unofficial server to support all versions of PSO, including Episode 3. (Their implementation of Episode 3 is based on newserv's, which is itself based on Sega's.)
* (Early 2000s) **[Schtserv](https://schtserv.com/)**: The first public-access PSO server, written in Delphi by Schthack. Schtserv is the only other unofficial server to support Episode 3, their implementation of which is based on newserv's (which is based on Sega's).
* (2005) **Khyller**: An early attempt of mine to support PSO PC, GC, and BB. See above for more details.
* (2006) **Aeon**: My second attempt. Better than Khyller, but still unreliable.
* (2008) **Tethealla**: A fairly extensive implementation of PSOBB, written in C by Sodaboy. The public version of Tethealla has been [officially disowned](https://www.pioneer2.net/community/threads/tethealla-server-forums-removal.26365/) (as it is now more than 15 years old), but closed-source development continues. [Ephinea](https://ephinea.pioneer2.net/), currently the most popular PSOBB server, is the continuation of this project. Several other modern PSOBB servers are forks of the initial public version of Tethealla as well.
* (2008) **[Sylverant](https://sylverant.net/)** [(source)](https://sourceforge.net/projects/sylverant/): The second public-access PSO server; written in C by BlueCrab. Still active and popular as of early 2025.
* (2008) **Tethealla**: A fairly extensive implementation of PSOBB, written in C by Sodaboy. The public version of Tethealla has been [officially disowned](https://www.pioneer2.net/community/threads/tethealla-server-forums-removal.26365/) as it is now more than 15 years old, but closed-source development continues. [Ephinea](https://ephinea.pioneer2.net/) is the continuation of this project. Several other modern PSOBB servers are forks of the initial public version of Tethealla as well.
* (2008) **[Sylverant](https://sylverant.net/)** [(source)](https://sourceforge.net/projects/sylverant/): The second public-access PSO server, written in C by BlueCrab.
* (2015) **[Archon](https://github.com/dcrodman/archon)**: A PSOBB server written in Go by Drew Rodman.
* (2015) **[Idola](https://github.com/HybridEidolon/idolapsoserv)**: A PSOBB server written in Rust by HybridEidolon. Functionality status unknown; the project has been archived.
* (2017) **[Aselia](https://github.com/Solybum/Aselia)**: A PSOBB server written written in C# by Soly. It seems this was planned to be open-source at some point, but that has not (yet) happened.
* (2017) **[Aselia](https://github.com/Solybum/Aselia)**: A PSOBB server written in C# by Soly. It seems this was planned to be open-source at some point, but that has not (yet) happened.
* (2018) **newserv**: This project right here.
* (2019) **[Mechonis](https://gitlab.com/sora3087/mechonis)**: A PSOBB server with a microservice architecture written in TypeScript by TrueVision.
* (2020) **[Booma.Server](https://github.com/HelloKitty/Booma.Server)**: A PSOBB server written in C# by Glader, with Soly's help.
* (2021) **[Phantasmal World](https://github.com/DaanVandenBosch/phantasmal-world)**: A set of PSO tools, including a web-based model viewer and quest builder, and a PSO server, written by Daan Vanden Bosch.
* (2021) **[Elseware](http://git.sharnoth.com/jake/elseware)**: A PSOBB server written in Rust by Jake.
## Developer information
## Using newserv in other projects
There is a lot of code in this project that could be useful as a reference. Some of the more notable files are:
You are free to use code from newserv in your own open-source projects; the only condition is that the contents of the LICENSE file must be included in your project if you use code from newserv. Your project does not also have to use the MIT license; you can use any license you want.
If you want to use parts of newserv in your project, there are two easy ways to do so with proper licensing:
* If you're using a lot of code from newserv, you can put a copy of newserv's LICENSE file in your repository alongside your own license file, or include the contents of newserv's license in your own license file.
* 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/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/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/Map.hh/cc**: Map file (.dat) structure and reverse-engineered Challenge Mode random enemy generation algorithm
* **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/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
## Using newserv in other projects
## Contributing to newserv
There is a fair amount of code in this project that could potentially be useful to other projects. You are free to use code from newserv in your own open-source projects; the only condition is that the contents of the LICENSE file must be included in your project if you use code from newserv. Your project does not also have to use the MIT license; you can use any license you want.
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.
If you want to use parts of newserv in your project, there are two easy ways to do so with proper licensing:
* If you're using a lot of code from newserv, you can put a copy of newserv's LICENSE file in your repository alongside your own license file, or include the contents of newserv's license in your own license file.
* 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.
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.
# Compatibility
@@ -97,28 +104,29 @@ newserv supports all known versions of PSO, including various development protot
| Version | Lobbies | Games | Proxy |
|-----------------|----------|----------|----------|
| DC NTE | Yes | Yes | No |
| DC 11/2000 | Yes | Yes | No |
| 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 | No |
| 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 | Yes | Yes |
| Xbox Ep1&2 | 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.*
# Setup
@@ -128,27 +136,27 @@ Currently newserv works on macOS, Windows, and Ubuntu Linux. It will likely work
### Windows/macOS
1. Download the latest release-windows-amd64.zip or release-macos-arm64.zip file from the [releases page](https://github.com/fuzziqersoftware/newserv/releases).
1. Download the latest release.zip file from the [releases page](https://github.com/fuzziqersoftware/newserv/releases).
2. Extract the contents of the archive to some location on your computer.
3. (Optional) If you want to change any config options, go into the system/ folder, open config.json in a text editor, and edit it to your liking. There are comments in the file that describe what all the options do.
3. Go into the system/ folder, open config.json in a text editor, and edit it to your liking. There are comments in the file that describe what all the options do. Most of the options can be left alone if you want default behavior, but on Windows, you must change LocalAddress and ExternalAddress.
4. (Optional) If you plan to play Blue Burst on newserv, set up the patch directory. See [client patch directories](#client-patch-directories) for details.
5. Run the newserv executable.
### Linux
There are currently no precompiled releases for Linux. To run newserv on Linux, you'll have to build it from source - see the "Building from source" section below.
There are currently no precompiled releases for Linux. To run newserv on Linux, you'll have to build it from source - see the section below.
### Building from source
### Building from source (macOS/Linux)
1. Install the packages newserv depends on.
* If you're on Windows, install [Cygwin](https://www.cygwin.com/). While doing so, install the `cmake`, `gcc-core`, `gcc-g++`, `git`, `libevent2.1_7`, `libevent-devel`, `make`, `libiconv-devel`, and `zlib` packages. Do the rest of these steps inside a Cygwin shell (not a Windows cmd shell or PowerShell).
* If you're on macOS, run `brew install cmake libevent libiconv`.
* If you're on Linux, run `sudo apt-get install cmake libevent-dev` (or use your Linux distribution's package manager).
3. Build and install [phosg](https://github.com/fuzziqersoftware/phosg).
4. Optionally, install [resource_dasm](https://github.com/fuzziqersoftware/resource_dasm). This will enable newserv to send memory patches and load DOL files on PSO GC clients. PSO GC clients can play PSO normally on newserv without this.
5. Run `cmake . && make` in the newserv directory.
To build on macOS or Linux:
After building newserv, edit system/config.example.json as needed **and rename it to system/config.json** (note that this step is not necessary for the precompiled releases!), set up [client patch directories](#client-patch-directories) if you're planning to play Blue Burst, then run `./newserv` in newserv's directory.
1. Install the dependencies needed for your platform:
* macOS: `brew install cmake asio libiconv`
* Linux: `sudo apt-get install cmake libasio-dev` (or use your Linux distribution's package manager)
2. Build and install [phosg](https://github.com/fuzziqersoftware/phosg) and [resource_dasm](https://github.com/fuzziqersoftware/resource_dasm).
3. Run `cmake . && make` in the newserv directory.
After building newserv, edit system/config.example.json as needed **and rename it to system/config.json** (note that this step is not necessary for the precompiled releases), set up [client patch directories](#client-patch-directories) if you're planning to play Blue Burst, then run `./newserv` in newserv's directory.
The server has an interactive shell which can be used to make changes, such as managing user accounts, updating the server's configuration, managing Episode 3 tournaments, and more. Type `help` and press Enter to see all the commands.
@@ -156,6 +164,16 @@ On Linux and macOS, the server also responds to SIGUSR1 and SIGUSR2. SIGUSR1 doe
To use newserv in other ways (e.g. for translating data), see the end of this document.
### Building from source (Windows)
The current version of newserv is cross-compiled using mingw-w64 on a macOS build machine, with the necessary libraries manually installed. Setting up such a build environment is tedious and not recommended; it's recommended to just use a release version instead.
Here is a rough outline of the Windows build process. You should only attempt this yourself if you're familiar with setting up build environments and can deal with issues you may encounter along the way.
1. Install recent versions of MinGW and CMake.
2. Build and install zlib, libiconv, asio, phosg, and resource_dasm into your MinGW environment.
3. Clone the newserv repository with symlinks enabled: `git clone -c core.symlinks=true https://github.com/fuzziqersoftware/newserv.git`
4. Build newserv via CMake.
## Client patch directories
newserv implements a patch server for PSO PC and PSO BB game data. Any file or directory you put in the system/patch-bb or system/patch-pc directories will be synced to clients when they connect to the patch server.
@@ -163,7 +181,7 @@ newserv implements a patch server for PSO PC and PSO BB game data. Any file or d
For Blue Burst set up, the below is mandatory for a smooth experience:
1. Browse to your chosen client's data directory.
2. Copy all the `map_*.dat` files, `unitxt_*` files and the `data.gsl` file and place them in `system/patch-bb/data`.
2. Copy all the `map_*.dat` files, `map_*.evt`, `unitxt_*` files, and the `data.gsl` file and place them in `system/patch-bb/data`.
3. If you're using game files from the Tethealla client, make a copy of `unitxt_j.prs` inside system/patch-bb/data and name it `unitxt_e.prs`. (If `unitxt_e.prs` already exists, replace it with the copied file.)
If you don't have a BB client, or if you're using a Tethealla client from another source, Tethealla clients that are compatible with newserv can be found here: [English](https://web.archive.org/web/20240402011115/https://ragol.org/files/bb/TethVer12513_English.zip) / [Japanese](https://web.archive.org/web/20240402013127/https://ragol.org/files/bb/TethVer12513_Japanese.zip). These clients connect to 127.0.0.1 (localhost) automatically.
@@ -245,6 +263,10 @@ If you're using the tapserver BBA or modem type, you can make it connect to a ne
3. In PSO's network settings, enable DHCP ("Automatically obtain an IP address"), set DNS server address to "Automatic", and leave DHCP Hostname as "Not set". Leave the proxy server settings blank.
4. Start an online game.
### PSO Xbox
Unfortunately, you can't easily host a private server for PSO Xbox because the Xbox version of the game tunnels its connections through Xbox Live. There is a modern replacement for Xbox Live named [Insignia](https://insignia.live/), which supports the three main PSO Xbox servers, but as of now does not support other private PSO servers.
### PSO BB
The PSO BB client has been modified and distributed in many different forms. newserv supports most, but not all, of the common distributions. Unlike other versions, it's common for various BB clients to have different map files. It's important that the client and server have the same map files, so make sure to set up the patch directory based on the client you'll be using with newserv. (See the [client patch directories](#client-patch-directories) section for instructions on setting this up.)
@@ -275,7 +297,8 @@ A license is a set of credentials that a player can use to log in. There are six
* *GC licenses* consist of a 10-digit decimal serial number, a 12-character access key, and a password of up to 8 characters.
* *XB licenses* consist of a gamertag of up to 16 characters, a 16-character hex user ID, and a 16-character hex account ID.
* *BB licenses* consist of a username of up to 16 characters and a password of up to 16 characters.
Each account may have multiple licenses. To add a license to an account, use `add-license` in the shell.
Each account may have multiple licenses. To add a license to an existing account, use `add-license` in the shell.
On BB, character data is scoped to the license, but system and Guild Card data is scoped to the account. That is, an account with multiple BB licenses can have more than 4 characters (up to 4 per license), but they will all share the same team membership and Guild Card lists.
@@ -295,7 +318,10 @@ For .dat files, the `LANGUAGE` token may be omitted. If it's present, then that
For example, the GameCube version of Lost HEAT SWORD is in two files named `q058-gc-e.bin` and `q058-gc.dat`. newserv knows these files are quests because they're in the system/quests/ directory, it knows they're for PSO GC because the filenames contain `-gc`, it knows this is the English version of the quest because the .bin filename ends with `-e` (even though the .dat filename does not), and it puts them in the Retrieval category because the files are within the retrieval/ directory within system/quests/.
Some quests (mostly battle and challenge mode quests) have additional JSON metadata files that describe how the server should handle them. These files include flags that can be used to hide the quest unless a preceding quest has been cleared, or to hide the quest unless purchased as a BB team reward. These metadata files are generally named similarly to their .bin and .dat counterparts, except the `VERSION` token may also be omitted if the metadata applies to all languages of the quest on all PSO versions. See system/quests/battle/b88001.json for documentation on the exact format of the JSON file.
Some quests have additional JSON metadata files that describe how the server should handle them. These metadata files are generally named similarly to their .bin and .dat counterparts, except the `VERSION` token may also be omitted if the metadata applies to all languages of the quest on all PSO versions. See the comments in system/quests/retrieval/q058.json for all of the available options and how to use them. Some of the options are:
- Disable or hide the quest if certain preceding quests aren't cleared or other conditions aren't met
- Enable the quest to be joined while in progress
- Override the common and/or rare item tables and set the allowed drop modes
Some quests may also include a .pvr file, which contains an image used in the quest. These files are named similarly to their .bin and .dat counterparts.
@@ -329,7 +355,7 @@ There are multiple PSO quest formats out there; newserv supports all of them. It
4. *Episode 3 quests don't go in the system/quests directory. See the [Episode 3 section](#episode-3-features) section below.*
5. *Quest source can be assembled into a .bin or .bind file with `newserv assemble-quest-script FILENAME.txt`. See system/quests/retrieval/q058-gc-e.bin.txt for an annotated example; this is the English GameCube version of Lost HEAT SWORD.*
Episode 3 download quests consist only of a .bin file - there is no corresponding .dat file. Episode 3 download quest files may be named with the .mnm extension instead of .bin, since the format is the same as the standard map files (in system/ep3/). These files can be encoded in any of the formats described above, except .qst.
Episode 3 download quests consist only of a .bin file - there is no corresponding .dat file. Episode 3 download quest files may be named with the .mnm extension instead of .bin, since the format is the same as the standard map files (in system/ep3/maps/). These files can be encoded in any of the formats described above, except .qst.
When newserv indexes the quests during startup, it will warn (but not fail) if any quests are corrupt or in unrecognized formats.
@@ -354,7 +380,7 @@ In the server drop modes, the item tables used to generate common items are in t
## Cross-version play
All versions of PSO can see and interact with each other in the lobby. By default, newserv allows V1 and V2 players to play together, and allows GC and Xbox players to play together. You can change these rules to allow all versions to play together, or to prevent versions from playing together, with the CompatibilityGroups setting in config.json.
All versions of PSO can see and interact with each other in the lobby. By default, newserv allows V1 and V2 players to play in games together, and allows GC and Xbox players to play in games together. You can change these rules to allow all versions to play in games together, or to prevent versions from playing in games together, with the CompatibilityGroups setting in config.json.
There are several cross-version restrictions that always apply regardless of the compatibility groups setting:
* DC V1 players cannot join DC V2 games if the game creator didn't choose to allow them.
@@ -373,7 +399,9 @@ newserv has the ability to save character data on the server side. For PSO BB, t
Each account has 4 BB character slots and 16 non-BB character file slots. The non-BB slots are independent of the BB slots, and can be accessed with the `$savechar <slot>` and `$loadchar <slot>` commands (slots are numbered 1 through 16). `$savechar` copies the character you're currently playing as and saves the data on the server, and `$loadchar` does the reverse, overwriting your current character with the data saved on the server. Note that you can load a character that was saved from a different version of PSO, which allows you to easily transfer characters between games. On v1 and v2, changes done by `$loadchar` will be undone if you join a game; to permanently save your changes, disconnect from the lobby after using the command.
There is a third command, `$bbchar <username> <password> <slot>`, which behaves similarly to `$savechar` but writes the character data to a BB character slot in a different account instead (slots are numbered 1 through 4). This can be used to "upgrade" a character to BB from an earlier version.
You can see basic information about a character saved on the server (without affecting your current character) by using `$checkchar <slot>`. You can delete a previously-saved character with `$deletechar <slot>`.
There is also the command `$bbchar <username> <password> <slot>`, which behaves similarly to `$savechar` but writes the character data to a BB character slot in a different account instead (slots are numbered 1 through 4). This can be used to "upgrade" a character to BB from an earlier version.
Exactly which data is saved and loaded depends on the game version:
@@ -424,18 +452,14 @@ Episode 3 state and game data is stored in the system/ep3 directory. The files i
* card-text.mnr: Compressed card text archive. Generally only used for debugging.
* card-text.mnrd: Decompressed card text archive; same format as TextCardE.bin. Generally only used for debugging.
* com-decks.json: COM decks used in tournaments. The default decks in this file come from logs from Sega's servers, so the file doesn't include every COM deck Sega ever made - the rest are probably lost to time.
* maps/: Online free battle and quest maps (.mnm/.bin/.mnmd/.bind files). newserv comes with the default online maps, as well as some fan-made variations and quests to help new players get up to speed.
* maps-download/: Download maps and quests (.mnm/.bin/.mnmd/.bind files). There are two subcategories by default (download maps and Trial Edition download maps), but you can add more by editing QuestCategories in config.json. Categories that have flag 0x40 (Ep3 download) set are indexed from this directory; all others are indexed from system/quests/. Files in maps-download/ subdirectories have the same format as those in the maps/ directory, but should be named like `e###-gc3-LANGUAGE.EXT` (similar to how non-Episode 3 quests are named in the system/quests/ directory). If you want a map to be available for online play and for downloading, the file must exist in both maps/ and in a maps-download/ subdirectory (a symbolic link is acceptable).
* maps-offline/: Offline map files. These are all the offline quests and free battle maps from the client, including some debugging/test maps that were inaccessible during normal play. To make them playable online, put the files in the maps/ directory.
* maps/: Online free battle and quest maps (.mnm/.bin/.mnmd/.bind files). newserv comes with the default online maps, as well as some fan-made variations and quests to help new players get up to speed. Within the maps/ directory, each subdirectory is treated as a separate category and may be optionally downloadable or available at the battle setup counter. The category.json file in each subdirectory specifies the category's behavior; see system/ep3/maps/online/category.json for a documented example.
* tournament-state.json: State of all active tournaments. This file is automatically written when any tournament changes state for any reason (e.g. a tournament is created/started/deleted or a match is resolved).
There is no public editor for Episode 3 maps and quests, but the format is described fairly thoroughly in src/Episode3/DataIndexes.hh (see the MapDefinition structure). You'll need to use `newserv decompress-prs ...` to decompress a .bin or .mnm file before editing it, but you don't need to compress it again to use it - just put the .bind or .mnmd file in the maps directory and newserv will make it available.
Like quests, Episode 3 card definitions, maps, and quests are cached in memory. If you've changed any of these files, you can run `reload ep3-cards` or `reload ep3-maps` in the interactive shell to make the changes take effect without restarting the server.
## Memory patches, client functions, and DOL files
*Everything in this section requires resource_dasm to be installed, so newserv can use the assemblers and disassemblers from its libresource_file library. If resource_dasm is not installed, newserv will still build and run, but these features will not be available.*
## 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.
@@ -443,50 +467,55 @@ The VERS token in client function filenames refers to the specific version of th
The specific versions are:
| Game | VERS | Architecture |
|------------------------------|------|---------------|
| PSO DC Network Trial Edition | 1OJ1 | Not supported |
| PSO DC 11/2000 prototype | 1OJ2 | Not supported |
| PSO DC 12/2000 prototype | 1OJ3 | Not supported |
| PSO DC 01/2001 prototype | 1OJ4 | Not supported |
| PSO DC v1 JP | 1OJF | Not supported |
| PSO DC v1 US | 1OEF | Not supported |
| PSO DC v1 EU | 1OPF | Not supported |
| PSO DC 08/2001 prototype | 2OJ5 | SH-4 |
| PSO DC v2 JP | 2OJF | SH-4 |
| PSO DC v2 US | 2OEF | SH-4 |
| PSO DC v2 EU | 2OPF | SH-4 |
| PSO PC (v2) | 2OJW | Not supported |
| PSO GC Trial Edition | 3OJT | PowerPC |
| PSO GC v1.2 JP | 3OJ2 | PowerPC |
| PSO GC v1.3 JP | 3OJ3 | PowerPC |
| PSO GC v1.4 (Plus) JP | 3OJ4 | PowerPC |
| PSO GC v1.5 (Plus) JP | 3OJ5 | PowerPC (1) |
| PSO GC v1.0 US | 3OE0 | PowerPC |
| PSO GC v1.1 US | 3OE1 | PowerPC |
| PSO GC v1.2 (Plus) US | 3OE2 | PowerPC (1) |
| PSO GC v1.0 EU | 3OP0 | PowerPC |
| PSO GC Ep3 Trial Edition | 3SJT | PowerPC |
| PSO GC Ep3 JP | 3SJ0 | PowerPC |
| PSO GC Ep3 US | 3SE0 | PowerPC (1) |
| PSO GC Ep3 EU | 3SP0 | PowerPC (1) |
| PSO Xbox Beta | 4OJB | x86 |
| PSO Xbox JP Disc | 4OJD | x86 |
| PSO Xbox JP TU | 4OJU | x86 |
| PSO Xbox US Disc | 4OED | x86 |
| PSO Xbox US TU | 4OEU | x86 |
| PSO Xbox EU Disc | 4OPD | x86 |
| PSO Xbox EU TU | 4OPU | x86 |
| PSO BB JP 1.25.11 | 59NJ | x86 |
| PSO BB JP 1.25.13 | 59NL | x86 |
| PSO BB Tethealla | 59NL | x86 |
| Game | VERS | CPU architecture |
|------------------------------|------|--------------------------------|
| PSO DC Network Trial Edition | 1OJ1 | Client functions not supported |
| PSO DC 11/2000 prototype | 1OJ2 | Client functions not supported |
| PSO DC 12/2000 prototype | 1OJ3 | Client functions not supported |
| PSO DC 01/2001 prototype | 1OJ4 | Client functions not supported |
| PSO DC v1 JP | 1OJF | Client functions not supported |
| PSO DC v1 US | 1OEF | Client functions not supported |
| PSO DC v1 EU | 1OPF | Client functions not supported |
| PSO DC 08/06/2001 prototype | 2OJ4 | SH-4 |
| PSO DC 08/22/2001 prototype | 2OJ5 | SH-4 |
| PSO DC v2 JP | 2OJF | SH-4 |
| PSO DC v2 US | 2OEF | SH-4 |
| PSO DC v2 EU | 2OPF | SH-4 |
| PSO PC (v2) Trial Edition | 2OJT | Client functions not supported |
| PSO PC (v2) 04/2002 | 2OJW | Client functions not supported |
| PSO PC (v2) 02/2003 | 2OJZ | Client functions not supported |
| PSO GC Trial Edition | 3OJT | PowerPC |
| PSO GC v1.2 JP | 3OJ2 | PowerPC |
| PSO GC v1.3 JP | 3OJ3 | PowerPC |
| PSO GC v1.4 (Plus) JP | 3OJ4 | PowerPC |
| PSO GC v1.5 (Plus) JP | 3OJ5 | PowerPC (1) |
| PSO GC v1.0 US | 3OE0 | PowerPC |
| PSO GC v1.1 US | 3OE1 | PowerPC |
| PSO GC v1.2 (Plus) US | 3OE2 | PowerPC (1) |
| PSO GC v1.0 EU | 3OP0 | PowerPC |
| PSO GC Ep3 Trial Edition | 3SJT | PowerPC |
| PSO GC Ep3 JP | 3SJ0 | PowerPC |
| PSO GC Ep3 US | 3SE0 | PowerPC (1) |
| PSO GC Ep3 EU | 3SP0 | PowerPC (1) |
| PSO Xbox Beta | 4OJB | x86 |
| PSO Xbox JP Disc | 4OJD | x86 |
| PSO Xbox JP TU | 4OJU | x86 |
| PSO Xbox US Disc | 4OED | x86 |
| PSO Xbox US TU | 4OEU | x86 |
| PSO Xbox EU Disc | 4OPD | x86 |
| PSO Xbox EU TU | 4OPU | x86 |
| PSO BB JP 1.25.11 | 59NJ | x86 |
| PSO BB JP 1.25.13 | 59NL | x86 |
| PSO BB Tethealla | 59NL | x86 |
*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, based on AR codes originally made by Ralf at GC-Forever and Aleron Ives. Many of them were originally posted in [this thread](https://www.gc-forever.com/forums/viewtopic.php?f=38&t=2050).
newserv comes with a set of patches for many of the above versions. These are organized in subdirectories within system/client-functions/.
You can also put DOL files in the system/dol directory, and they will appear in the Programs menu for GC clients. Selecting a DOL file there will load the file into the GameCube's memory and run it, just like the old homebrew loaders (PSUL and PSOload) did. For this to work, ReadMemoryWordGC.ppc.s, WriteMemoryGC.ppc.s, and RunDOL.ppc.s must be present in the system/client-functions/System directory. This has been tested on Dolphin but not on a real GameCube, so results may vary.
### DOL loader
You can put DOL files in the system/dol directory, and they will appear in the Programs menu for GC clients. Selecting a DOL file there will load the file into the GameCube's memory and run it, just like the old homebrew loaders (PSUL and PSOload) did. For this to work, ReadMemoryWordGC.ppc.s, WriteMemoryGC.ppc.s, and RunDOL.ppc.s must be present in the system/client-functions/System directory. This has been tested on Dolphin but not on a real GameCube, so results may vary.
Like other kinds of data, functions and DOL files are cached in memory. If you've changed any of these files, you can run `reload functions` or `reload dol-files` in the interactive shell to make the changes take effect without restarting the server.
@@ -498,9 +527,9 @@ If you want to play online on remote servers rather than running your own server
To use the proxy for PSO DC, PC, or GC, add an entry to the corresponding ProxyDestinations dictionary in config.json, then run newserv and connect to it as normal (see below). You'll see a "Proxy server" option in the main menu, and you can pick which remote server to connect to.
To use the proxy for PSO BB, set the ProxyDestination-BB entry in config.json. If this option is set, it essentially disables the game server for all PSO BB clients - all clients will be proxied to the specified destination instead. Unfortunately, because PSO BB uses a different set of handlers for the data server phase and character selection, there's no in-game way to present the player with a list of options, like there is on PSO PC and PSO GC.
To use the proxy for PSO BB, set the ProxyDestination-BB entry in config.json. If this option is set, it essentially disables the game server for all BB clients - all BB clients will be proxied to the specified destination instead. Unfortunately, because PSO BB uses a different set of handlers for the data server phase and character selection, there's no in-game way to present the player with a list of options, like there is on PSO PC and PSO GC.
When you're on PSO DC, PC, or GC and are connected to a remote server through newserv's proxy, choosing the Change Ship or Change Block action from the lobby counter will send you back to newserv's main menu instead of the remote server's ship or block select menu. You can go back to the server you were just on by choosing it from the proxy server menu again.
When you're on PSO DC, PC, GC, or Xbox and are connected to a remote server through newserv's proxy, choosing the Change Ship or Change Block action from the lobby counter will send you back to newserv's main menu instead of the remote server's ship or block select menu. You can go back to the server you were just on by choosing it from the proxy server menu again.
There are many options available when starting a proxy session. All options are off by default unless otherwise noted. The options are:
* **Chat commands**: enables chat commands in the proxy session (on by default).
@@ -513,120 +542,146 @@ There are many options available when starting a proxy session. All options are
* **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.
* **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). These kinds of files can be saved:
* **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 to text files if newserv is built with patch support)
* Player data from BB sessions (saved as .bin files, which are not the same format as .nsc files)
* 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)
* Episode 3 download quests (saved as .mnm files)
* Episode 3 card definitions (saved as .mnr files)
* Episode 3 media updates (saved as .gvm, .bml, or .bin files)
The remote server will probably try to assign you a Guild Card number that doesn't match the one you have on newserv. On PSO DC, PC and GC, the proxy server rewrites the commands in transit to make it look like the remote server assigned you the same Guild Card number as you have on newserv, but if the remote server has some external integrations (e.g. forum or Discord bots), they will use the Guild Card number that the remote server believes it has assigned to you. The number assigned by the remote server is shown to you when you first connect to the remote server, and you can retrieve it in lobbies or during games with the `$li` command.
The remote server will probably try to assign you a Guild Card number that doesn't match the one you have on newserv. The proxy rewrites the commands in transit to make it look like the remote server assigned you the same Guild Card number as you have on newserv, but if the remote server has some external integrations (e.g. forum or Discord bots), they will use the Guild Card number that the remote server believes it has assigned to you. The number assigned by the remote server is shown to you when you first connect to the remote server, and you can retrieve it in lobbies or during games with the `$li` command.
Some chat commands (see below) have the same basic function on the proxy server but have different effects or conditions. In addition, there are some server shell commands that affect clients on the proxy (run `help` in the shell to see what they are). If there's only one proxy session open, the shell's proxy commands will affect that session. Otherwise, you'll have to specify which session to affect with the `on` prefix - to send a chat message in LinkedSession:17205AE4, for example, you would run `on 17205AE4 chat ...`.
Some chat commands (see below) have the same basic function on the proxy but have different effects or conditions. In addition, there are some server shell commands that affect clients on the proxy (run `help` in the shell to see what they are). If there's only one proxy session open, the shell's proxy commands will affect that session. Otherwise, you'll have to specify which session to affect with the `on` prefix - to send a chat message in C-17's session, for example, you would run `on C-17 chat ...`.
## Chat commands
newserv supports a variety of commands players can use by chatting in-game. Any chat message that begins with `$` is treated as a chat command. (If you actually want to send a chat message starting with `$`, type `$$` instead.) On the DC 11/2000 prototype, `@` is used instead of `$` for all chat commands, since `$` does not appear on the English virtual keyboard.
Some commands only work on the game server and not on the proxy server. The chat commands are:
Some commands only work for clients not in proxy sessions. The chat commands are:
* Information commands
* `$li`: Show basic information about the lobby or game you're in. If you're on the proxy server, show information about your connection instead (remote Guild Card number, client ID, etc.).
* `$si` (game server only): Show basic information about the server.
* `$ping`: Show round-trip ping time from the server to you. On the proxy server, show the ping time from you to the proxy and from the proxy to the server.
* `$matcount` (game server only): Show how many of each type of material you've used.
* `$killcount` (game server only): Show the kill count on your currently-equipped weapon. If you're in a game and not on BB, the value is only accurate at the time the item enters the game.
* `$li`: Show basic information about the lobby or game you're in. If you're on the proxy, show information about your connection instead (remote Guild Card number, client ID, etc.).
* `$si`: Show basic information about the server.
* `$ping`: Show round-trip ping time from the server to you. On the proxy, show the ping time from you to the proxy and from the proxy to the server.
* `$matcount` (non-proxy only): Show how many of each type of material you've used.
* `$killcount` (non-proxy only): Show the kill count on your currently-equipped weapon. If you're in a game and not on BB, the value is only accurate at the time the item enters the game.
* `$itemnotifs <mode>`: Enable item drop notification messages. If the game has private drops enabled, you will only see a notification if the dropped item is visible to you; you won't be notified of other players' drops. The modes are:
* `off`: No notifications are shown.
* `rare`: You are notified when a rare item drops.
* `on`: You are notified when any item drops, except Meseta.
* `every`: You are notified when any item drops, including Meseta.
* `$announcerares`: Enable or disable announcements for your rare item finds. This determines whether rare items you find will be announced to the game and server, not whether you will see announcements for others finding rare items.
* `$what` (game server only): Show the type, name, and stats of the nearest item on the ground.
* `$where` (game server only): Show your current floor number and coordinates. Mainly useful for debugging.
* `$qfread <field-name>` (game server only): Show the value of a quest counter in your player data. The field names are defined in config.json.
* `$what` (non-proxy only): Show the type, name, and stats of the nearest item on the ground.
* `$where`: Show your current floor number and coordinates. Mainly useful for debugging.
* `$qfread <field-name>` (non-proxy only): Show the value of a quest counter in your player data. The field names are defined in config.json.
* Debugging commands
* `$debug` (game server only): Enable or disable debug. You need the DEBUG flag in your user account to use this command. Enabling debug does several things:
* Basic debugging commands (special permissions not required)
* `$whatobj` and `$whatene` (non-proxy only): Tells you what the closest object or enemy spawn point is to your position, along with its coordinates and object or enemy ID. The full definition is also printed to the server's log.
* `$qcheck <flag-num>` (non-proxy only): Show the value of a quest flag. If you're in a game, show the value of the flag in that game; if you're in the lobby, show the saved value of that quest flag for your character (BB only).
* `$qgread <flag-num>` (non-proxy only): Show the value of a quest counter ("global flag").
* `$sound <sound-id>`: Play the given sound (GC only).
* Restricted debugging commands (`$debug` permission required)
* `$debug`: Enable debug mode. You need the DEBUG flag in your user account to use this command. Enabling debug does several things:
* You'll be able to use the rest of the commands in this section.
* 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 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.
* The rest of the commands in this section are enabled on the game server. (They are always enabled on the proxy server.)
* `$readmem <address>` (game server only): Read 4 bytes from the given address and show you the values.
* `$writemem <address> <data>` (game server only): Write data to the given address. Data is not required to be any specific size.
* `$nativecall <address> [arg1 ...]` (game server only, GC only): Call a native function on your client. Only arguments passed in registers are supported; calling functions that take many arguments is not supported.
* `$quest <number>` (game server only): Load a quest by quest number. Can be used to load battle or challenge quests with only one player present. Debug is not required to be enabled if the specified quest has the AllowStartFromChatCommand field set in its metadata file.
* `$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.
* `$nativecall <address> [arg1 ...]` (GC only): Call a native function on your client. Only arguments passed in registers are supported; calling functions that take many arguments is not supported.
* `$quest <number>` (non-proxy only): Load a quest by quest number. Can be used to load battle or challenge quests with only one player present. `$debug` is not required for this command if the specified quest has the AllowStartFromChatCommand field set in its metadata file.
* `$qcall <function-id>`: Call a quest function on your client.
* `$qcheck <flag-num>` (game server only): Show the value of a quest flag. This command can be used without debug mode enabled. If you're in a game, show the value of the flag in that game; if you're in the lobby, show the saved value of that quest flag for your character (BB only).
* `$qset <flag-num>` or `$qclear <flag-num>`: Set or clear a quest flag for everyone in the game. If you're in the lobby and on BB, set or clear the saved value of a quest flag in your character file.
* `$qgread <flag-num>` (game server only): Show the value of a quest counter ("global flag"). This command can be used without debug mode enabled.
* `$qgwrite <flag-num> <value>` (game server only): Set the value of a quest counter ("global flag") for yourself.
* `$qgwrite <flag-num> <value>` (non-proxy only): Set the value of a quest counter ("global flag") for yourself.
* `$qsync <reg-num> <value>`: Set a quest register's value for yourself only. `<reg-num>` should be either rXX (e.g. r60) or fXX (e.g. f60); if the latter, `<value>` is parsed as a floating-point value instead of as an integer.
* `$qsyncall <reg-num> <value>`: Set a quest register's value for everyone in the game. `<reg-num>` should be either rXX (e.g. r60) or fXX (e.g. f60); if the latter, `<value>` is parsed as a floating-point value instead of as an integer.
* `$swset [floor] <flag-num>` and `$swclear [floor] <flag-num>`: Set or clear a switch flag. If floor is not given, sets or clears the flag on your current floor.
* `$swsetall`: Set all switch flags on your current floor. This unlocks all doors, disables all laser fences, triggers all light/poison switches, etc.
* `$gc` (game server only): Send your own Guild Card to yourself.
* `$allrare`: Make all enemies and boxes drop their rare items every time.
* `$gc` (non-proxy only): Send your own Guild Card to yourself.
* `$sc <data>`: Send a command to yourself.
* `$scp <data>`: Send a protected command to yourself.
* `$ss <data>`: Send a command to the remote server (if in a proxy session) or to the game server.
* `$sb <data>`: Send a command to yourself, and to the remote server or game server.
* `$auction` (Episode 3 only): Bring up the CARD Auction menu, regardless of how many players are in the game or if you have a VIP card.
* `$auction` (Episode 3 only): Bring up the CARD Auction menu, even if there are fewer than 4 players are in the game or you don't have a VIP card.
* `$makeobj <type> [coords...] [angles...] [params...]`: Create a map object. This is only implemented for a few specific client versions. The type is an integer like `273` or `0x0107`. Coordinates are specified as e.g. `x:30 y:0 z:-25.5`; if coordinates are not specified, the object is created at the player's coordinates. Angles are specified as e.g. `r:0 p:0x1000 w:-0x400` (for roll, pitch, and yaw, respectively). Parameters are specified as e.g. `1:2.0 2:0.0 5:0x4000`; any unspecified parameters are set to zero. The object is only created for the calling player and is not added to the server's map state; if the object ever sends update commands (e.g. 6x0B), it will likely result in a disconnection.
* Personal state commands
* `$arrow <color-id>`: Change your lobby arrow color.
* `$secid <section-id>`: Set your override section ID. After running this command, any games you create will use your override section ID for rare drops instead of your character's actual section ID. If you're in a game and you are the leader of the game, this also immediately changes the item tables used by the server when creating items. To revert to your actual section id, run `$secid` with no name after it. On the proxy server, this will not work if the remote server controls item drops. If the server does not allow cheat mode anywhere (that is, "CheatModeBehavior" is "Off" in config.json), this command does nothing.
* `$rand <seed>`: Set your override random seed (specified as a 32-bit hex value). This will make any games you create use the given seed for rare enemies. This also makes item drops deterministic in Blue Burst games hosted by newserv. On the proxy server, this command can cause desyncs with other players in the same game, since they will not see the overridden random seed. To remove the override, run `$rand` with no arguments. If the server does not allow cheat mode anywhere (that is, "CheatModeBehavior" is "Off" in config.json), this command does nothing.
* `$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 server, 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.
* `$arrow <color-id>`: Change your lobby arrow color. The color may be specified by number (0-12) or by name (red, blue, green, yellow, purple, cyan, orange, pink, white, white2, white3, or black).
* `$secid <section-id>`: Set your override section ID. After running this command, any games you create will use your override section ID for rare drops instead of your character's actual section ID. If you're in a game and you are the leader of the game, this also immediately changes the item tables used by the server when creating items. To revert to your actual section id, run `$secid` with no name after it. On the proxy, this will not work if the remote server controls item drops. If the server does not allow cheat mode anywhere (that is, "CheatModeBehavior" is "Off" in config.json), this command does nothing.
* `$rand <seed>`: Set your override random seed (specified as a 32-bit hex value). This will make any games you create use the given seed for rare enemies and item drops. On the proxy, this command can cause desyncs with other players in the same game, since they will not see the overridden random seed. To remove the override, run `$rand` with no arguments. If the server does not allow cheat mode anywhere (that is, "CheatModeBehavior" is "Off" in config.json), this command does nothing.
* `$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.
* Character data commands (game server only)
* Character data commands (non-proxy only)
* `$switchchar <slot>` (BB only): Switch to a different character from your account without logging out.
* `$savechar <slot>`: Save your current character data on the server in the specified slot. See the [server-side saves section](#server-side-saves) for more details.
* `$loadchar <slot>`: Save your current character data on the server in the specified slot. See the [server-side saves section](#server-side-saves) for more details.
* `$loadchar <slot>`: Load character data from the specified slot on the server, and replace your current character with it. See the [server-side saves section](#server-side-saves) for more details.
* `$bbchar <username> <password> <slot>`: Save your current character data on the server in a different account's BB character slots. See the [server-side saves section](#server-side-saves) for more details.
* `$checkchar [slot]`: Tells you basic information about a server-side character previously saved using `$savechar`. If `slot` is not given, tells you which slots are used and which are free.
* `$deletechar <slot>`: Deletes a server-side character previously saved using `$savechar`.
* `$edit <stat> <value>`: Modify your character data. See the [using $edit](#using-edit) section for details.
* Blue Burst player commands (game server only)
* Blue Burst player commands (non-proxy only)
* `$bank [number]`: Switch your current bank, so you can access your other character's banks (if `number` is 1-4) or your shared account bank (if `number` is 0). If `number` is not given, switch back to your current character's bank.
* `$save`: Save your character, system, and Guild Card data immediately. (By default, your character is saved every 60 seconds while online, and your account and Guild Card data are saved whenever they change.)
* Game state commands (game server only)
* Game state commands (non-proxy only)
* `$maxlevel <level>`: Set the maximum level for players to join the current game. (This only applies when joining; if a player joins and then levels up past this level during the game, they are not kicked out, but won't be able to rejoin if they leave.)
* `$minlevel <level>`: Set the minimum level for players to join the current game.
* `$password <password>`: Set the game's join password. To unlock the game, run `$password` with nothing after it.
* `$dropmode [mode]`: Change the way item drops behave in the current game. `mode` can be `none`, `client`, `shared`, `private`, or `duplicate`. If `mode` is not given, tells you the current drop mode without changing it. See the [item tables and drop modes section](#item-tables-and-drop-modes) for more information.
* `$persist`: Enable or disable persistence for the current game. When persistence is on, the game will not be deleted when the last player leaves. The states of enemies, objects, and switches will be saved, and items left on the floor will not be deleted. (But if you're in the private or duplicate drop mode, items dropped by enemies are deleted - to make sure a certain item won't be deleted, you can pick it up and drop it again.) If the game is empty for too long (15 minutes by default), it is then deleted.
* Episode 3 commands (game server only)
* Episode 3 commands (non-proxy only)
* `$spec`: Toggle the allow spectators flag for Episode 3 games. If any players are spectating when this flag is disabled, they are sent back to the lobby.
* `$inftime`: Toggle infinite-time mode. Must be used before starting a battle. If infinite-time mode is on, the overall and per-phase time limits will be disabled regardless of the values chosen during battle rules setup. After completing a battle, infinite-time mode is reset to the server's default value (which can be set in Episode3BehaviorFlags in config.json).
* `$dicerange [d:L-H] [1:L-H] [a1:L-H] [d1:L-H]`: Set override dice ranges for the next battle. The min and max dice values from the rules setup menu always apply to the ATK dice, but you can specify a different range for the DEF dice with `d:2-4` (for example). The `1:` override applies to the 1-player team in a 2v1 game (so you would set the 2-player team's desired dice range in the rules menu). You can also specify the 1-player team's ATK and DEF ranges separately with the `a1:` and `d1:` overrides. Note that these ranges will only be used if the chosen map or quest does not override them.
* `$stat <what>`: Show a statistic about your player or team in the current battle. `<what>` can be `duration`, `fcs-destroyed`, `cards-destroyed`, `damage-given`, `damage-taken`, `opp-cards-destroyed`, `own-cards-destroyed`, `move-distance`, `cards-set`, `fcs-set`, `attack-actions-set`, `techs-set`, `assists-set`, `defenses-self`, `defenses-ally`, `cards-drawn`, `max-attack-damage`, `max-combo`, `attacks-given`, `attacks-taken`, `sc-damage`, `damage-defended`, or `rank`.
* `$surrender`: Cause your team to immediately lose the current battle. If your story character is already defeated, you can't surrender - only your teammate can.
* `$saverec <name>`: Save the recording of the last battle.
* `$playrec <name>`: Play a battle recording. This command creates a spectator team immediately but the replay does not start automatically, to give other players a chance to join. To start the battle replay within the spectator team, run `$playrec` again (with no name). There is a bug in Dolphin that makes this command unstable in emulation (see the "Battle records" section above).
* `$playrec <name>`: Play a battle recording. This command creates a spectator team and plays the specified recording as if it were happening in real time. By default, playback will start immediately when the spectator team is ready; you can delay this to allow others to join by prepending a `!` to the recording name. In that case, using `$playrec` again (with no argument) within the spectator team will start playback.
* Cheat mode commands
* `$cheat` (game server only): Enable or disable cheat mode for the current game. All other cheat mode commands do nothing if cheat mode is disabled. By default, cheat mode is off in new games but can be enabled; there is an option in config.json that allows you to disable cheat mode entirely, or set it to on by default in new games. Cheat mode is always enabled on the proxy server, unless cheat mode is disabled on the entire server.
* `$infhp`: Enable or disable infinite HP mode. Applies to only you; does not affect other players. When enabled, one-hit KO attacks will still kill you, but on most versions of the game (not DCv1, GC US 1.2, or GC JP 1.5), the server will automatically revive you if you die. On all versions except GC US 1.2 and GC JP 1.5, infinite HP also automatically cures status ailments.
* `$inftp`: Enable or disable infinite TP mode. Applies to only you; does not affect other players.
* `$cheat` (non-proxy only): Enable or disable cheat mode for the current game. All other cheat mode commands do nothing if cheat mode is disabled. By default, cheat mode is off in new games but can be enabled; there is an option in config.json that allows you to disable cheat mode entirely, or set it to on by default in new games. Cheat mode is always enabled on the proxy, unless cheat mode is disabled on the entire server.
* `$infhp`: Enable or disable infinite HP mode. Applies to only you; does not affect other players. When enabled, one-hit KO attacks will still kill you, but on most versions of the game, the server will automatically revive you if you die. Infinite HP also automatically cures status ailments.
* `$inftp`: Enable or disable infinite TP mode. Applies to only you; does not affect other players. Does not work on DCv1 or earlier versions.
* `$fastkill`: Enable or disable fast kills. Applies to only you; does not affect other players. When enabled, the server will kill any enemy after you hit it once. Bosses are not affected by fast kills.
* `$warpme <floor-id>` (or `$warp <floor-id>`): Warp yourself to the given floor.
* `$warpall <floor-id>`: Warp everyone in the game to the given floor. You must be the leader to use this command, unless you're on the proxy server.
* `$warpall <floor-id>`: Warp everyone in the game to the given floor. You must be the leader to use this command, unless you're on the proxy.
* `$next`: Warp yourself to the next floor.
* `$item <desc>` (or `$i <desc>`): Create an item. `desc` may be a description of the item (e.g. "Hell Saber +5 0/10/25/0/10") or a string of hex data specifying the item code. Item codes are 16 hex bytes; at least 2 bytes must be specified, and all unspecified bytes are zeroes. If you are on the proxy server, you must not be using Blue Burst for this command to work. On the game server, this command works for all versions.
* `$unset <index>` (game server only): In an Episode 3 battle, removes one of your set cards from the field. `<index>` is the index of the set card as it appears on your screen - 1 is the card next to your SC's icon, 2 is the card to the right of 1, etc. This does not cause a Hunters-side SC to lose HP, as they normally do when their items are destroyed.
* `$dropmode [mode]` (proxy server): Change the way item drops behave in the current game, if you are not on BB. Unlike the game server version of this command, using this on the proxy server requires cheats to be enabled. This works by intercepting the drop requests sent to and from the leader. (So, if you are the leader and not using server drop mode on the remote server, it affects the entire game; otherwise, it affects only items generated by your actions.) `mode` can be `none` (no drops), `default` (normal drops), or `proxy` (use newserv's drop tables instead of the remote server's). If `mode` is not given, tells you the current drop mode without changing it.
* `$item <desc>` (or `$i <desc>`): Create an item. `desc` may be a description of the item or a string of hex data specifying the item code. Item codes are 16 hex bytes; at least 2 bytes must be specified, and all unspecified bytes are zeroes. If you are on the proxy, you must not be using Blue Burst for this command to work. On the game server, this command works for all versions. Here are some examples to illustrate the syntax (nothing is case-sensitive, and everything except the item name itself is optional):
* `$item Saber +5 0/10/25/0/10` (weapon with special, grind and attributes)
* `$item ???? Draw Autogun` (untekked weapon with special; can have grind/attributes too, as above)
* `$item SEALED J-SWORD K:2000` (weapon with kill count)
* `$item ES APHEX ZALURE TWIN +200` (ES weapon must be prefixed with "ES"; name comes before special)
* `$item DF FIELD +10DEF +20EVP +4` (armor with DFP bonus, EVP bonus, and slot count)
* `$item RED MERGE +10DFP +20EVP` (shield; same as armor except without slot count)
* `$item Knight/Power +9` (unit with specific modifier)
* `$item Knight/Power++` (unit with normal modifier; ++/-- are +4/-4 and +/- are +2/-2)
* `$item LIMITER K:1000` (sealed unit with kill count)
* `$item Tapas PB:F,G,M&Y 120% 200IQ 5/195/0/0 green` (mag with PBs, synchro, IO, stats, and color)
* `$item Trimate x10` (tool with stack size)
* `$item Disk:Reverser` (technique disk without level)
* `$item Disk:Razonde Lv.30` (technique disk with level)
* `$item 1000 Meseta`
* `$unset <index>` (non-proxy only): In an Episode 3 battle, removes one of your set cards from the field. `<index>` is the index of the set card as it appears on your screen - 1 is the card next to your SC's icon, 2 is the card to the right of 1, etc. This does not cause a Hunters-side SC to lose HP, as they normally do when their items are destroyed. You can also destroy the assist card set on yourself with `$unset 0`.
* `$dropmode [mode]` (proxy only): Change the way item drops behave in the current game, if you are not on BB. Unlike the game server version of this command, using this on the proxy requires cheats to be enabled. This works by intercepting the drop requests sent to and from the leader. (So, if you are the leader and not using server drop mode on the remote server, it affects the entire game; otherwise, it affects only items generated by your actions.) `mode` can be `none` (no drops), `default` (normal drops), or `proxy` (use newserv's drop tables instead of the remote server's). If `mode` is not given, tells you the current drop mode without changing it.
* Aesthetic commands
* `$event <event>`: Set the current holiday event in the current lobby. Holiday events are documented in the "Using $event" item in the information menu. If you're on the proxy server, this applies to all lobbies and games you join, but only you will see the new event - other players will not.
* `$allevent <event>` (game server only): Set the current holiday event in all lobbies.
* `$event <event>`: Set the current holiday event in the current lobby. Holiday events are documented in the "Using $event" item in the information menu. If you're on the proxy, this applies to all lobbies and games you join, but only you will see the new event - other players will not.
* `$allevent <event>` (non-proxy only): Set the current holiday event in all lobbies.
* `$song <song-id>` (Episode 3 only): Play a specific song in the current lobby.
* Administration commands (game server only)
* Administration commands (non-proxy only)
* `$ann <message>`: Send an announcement message. The message is sent as temporary on-screen text to all players in all games and lobbies. On BB, the message appears in the scrolling top bar.
* `$ann!`, `$ann?`, `$ann?!`: Same as `$ann`, but with `?`, omits the sender's name, and with `!`, sends the message as a Simple Mail message instead of on-screen text.
* `$silence <identifier>`: Silence a player (remove their ability to chat) or unsilence a player. The identifier may be the player's name or Guild Card number.
@@ -690,7 +745,7 @@ The HTTP server has the following endpoints:
* `GET /y/data/config`: Returns the server's configuration file.
* `GET /y/accounts`: Returns information about all registered accounts.
* `GET /y/clients`: Returns information about all connected clients on the game server.
* `GET /y/proxy-clients`: Returns information about all connected clients on the proxy server.
* `GET /y/proxy-clients`: Returns information about all connected clients on the proxy.
* `GET /y/lobbies`: Returns information about all lobbies and games.
* `GET /y/server`: Returns information about the server.
* `GET /y/summary`: Returns a summary of the server's state, connected clients, active games, and proxy sessions.
@@ -718,7 +773,7 @@ Upon connecting, you'll get the message `{"ServerType": "newserv"}`. After that,
# Non-server features
newserv has many CLI options, which can be used to access functionality other than the game and proxy server. Run `newserv help` to see a full list of the options and how to use each one.
newserv has many CLI options, which can be used to access functionality other than the game server and proxy. Run `newserv help` to see a full list of the options and how to use each one.
The data formats that newserv can convert to/from are:
@@ -737,6 +792,7 @@ The data formats that newserv can convert to/from are:
| 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` |
@@ -760,6 +816,7 @@ There are several actions that don't fit well into the table above, which let yo
* Format Episode 3 game data in a human-readable manner (`show-ep3-maps`, `show-ep3-cards`, `generate-ep3-cards-html`)
* Format Blue Burst battle parameter files in a human-readable manner (`show-battle-params`)
* Convert item data to a human-readable description, or vice versa (`describe-item`)
* Show the server's item and level tables (`show-item-tables`, `show-level-tables`)
* Connect to another PSO server and pretend to be a client (`cat-client`)
* Generate or describe DC serial numbers (`generate-dc-serial-number`, `inspect-dc-serial-number`)
+11 -3
View File
@@ -1,13 +1,19 @@
## General
- Make UI strings localizable (e.g. entries in menus, welcome message, etc.)
- Add an idle connection timeout for proxy sessions
- 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)
- Handle MeetUserExtensions properly in 41 and C4 commands on the proxy (rewrite the embedded 19 command and store a map of received destinations)
- Make UI strings localizable (e.g. entries in menus, welcome message, etc.)
## PSO DC
- Investigate if https://crates.io/crates/blaze-ssl-async can be used to implement the HL check server
- v2 challenge data in $savechar/$loadchar doesn't work properly
## Episode 3
@@ -27,3 +33,5 @@
- 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
+67
View File
@@ -0,0 +1,67 @@
#!/usr/bin/env python3
import os
import shutil
import subprocess
import sys
from typing import Callable
def filter_directory(dir: str, predicate: Callable[[str], bool]):
for filename in os.listdir(dir):
if not predicate(filename):
path = os.path.join(dir, filename)
if os.path.isfile(path):
os.remove(path)
else:
shutil.rmtree(path)
def main():
print("Deleting existing release directory")
if os.path.exists("release"):
shutil.rmtree("release")
if os.path.exists("release.zip"):
os.remove("release.zip")
os.mkdir("release")
print("Adding executables")
shutil.copy("newserv", "release/newserv-macos")
shutil.copy("newserv.exe", "release/newserv-windows.exe")
shutil.copy("README.md", "release/README.md")
print("Adding system directory")
shutil.copytree("system", "release/system")
print("Removing instance-based and temporary files")
filter_directory(
"release/system",
lambda filename: (not filename.endswith(".json"))
or filename == "config.example.json",
)
filter_directory(
"release/system/ep3", lambda filename: not filename.startswith("cardtex")
)
filter_directory(
"release/system/client-functions",
lambda filename: filename not in ("Debug-Private", "FastLoading", "notes.txt"),
)
filter_directory("release/system/dol", lambda filename: False)
filter_directory("release/system/ep3/banners", lambda filename: False)
filter_directory("release/system/ep3/battle-records", lambda filename: False)
filter_directory("release/system/licenses", lambda filename: False)
filter_directory("release/system/players", lambda filename: False)
filter_directory(
"release/system/quests",
lambda filename: filename not in ("private", "includes"),
)
filter_directory("release/system/teams", lambda filename: filename == "base.json")
subprocess.check_call(["find", "release", "-name", ".DS_Store", "-delete"])
subprocess.check_call(["find", "release", "-name", "*.WIP-s", "-delete"])
print("Setting up configuration")
os.rename("release/system/config.example.json", "release/system/config.json")
if __name__ == "__main__":
sys.exit(main())
+409 -4
View File
@@ -13,11 +13,14 @@ Version codes (from README.md):
1OJF: PSO DC v1 JP
1OEF: PSO DC v1 US
1OPF: PSO DC v1 EU
2OJ4: PSO DC 08/2001 prototype
2OJ5: PSO DC 08/2001 prototype
2OJF: PSO DC v2 JP
2OEF: PSO DC v2 US
2OPF: PSO DC v2 EU
2OJW: PSO PC (v2)
2OJT: PSO PC Trial Edition
2OJW: PSO PC (v2) 04/2002
2OJZ: PSO PC (v2) 02/2003
3OJT: PSO GC Trial Edition
3OJ2: PSO GC v1.2 JP
3OJ3: PSO GC v1.3 JP
@@ -39,28 +42,95 @@ Version codes (from README.md):
4OPD: PSO Xbox EU Disc
4OPU: PSO Xbox EU TU
59NJ: PSO BB JP 1.25.11
59NL: PSO BB JP 1.25.13
59NL: PSO BB Tethealla
59NL: PSO BB JP 1.25.13 (including the Tethealla client)
The menu code
This code makes all disabled items in menus selectable, which allows you to e.g. use items you can't normally use
3OJ2 => 04263B80 48000028
042AC548 48000020
3OJ3 => 04264758 48000028
042AD3F0 48000020
3OJ4 => 042657B4 48000028
042AE51C 48000020
3OJ5 => 04265554 48000028
042AE2D0 48000020
3OE0 => 04264458 48000028
042ACF04 48000020
3OE1 => 04264458 48000028
042ACF48 48000020
3OE2 => 04265818 48000028
042AE484 48000020
3OP0 => 04265060 48000028
042ADC18 48000020
3SJT => 0417ADD0 48000028
3SJ0 => 0416B5A4 48000028
3SE0 => 0416B458 48000028
3SP0 => 0416B904 48000028
Disable serial number validation (untested)
2OEF => 8C1E743E 01E0
8C2670B6 01E0
Disable item equip restrictions ("God of equip")
3OE0 => 0410521C 38000005
3OE1 => 0410521C 38000005
3OE2 => 041050E4 38000005
3OJ2 => 04104F78 38000005
3OJ3 => 04105154 38000005
3OJ4 => 04105240 38000005
3OJ5 => 041050D4 38000005
3OJT => 0415BF50 38000005
3OP0 => 041052D4 38000005
59NL => 005C9F31 E9A7000000
All items visible in Pioneer 2
3OE1 => 04102D88 38600000
Mags visible in Pioneer 2
59NL => 005D8F4B EB04
Disable pause menu background + offset
3OE1 => 0424BD5C 48000370
0428735C 4800000C
3OE2 => 0424CED8 48000370
042887D8 4800000C
59NL => 00719B54 9090
00733BA7 9090
00733A0E 90E9
All rareable enemies are rare
3OE0 => 040AC944 60000000 // Hildeblue
040C1B70 60000000 // Rappies
040C3FC8 60000000 // Nar Lily
040EB050 48000010 // Pouilly Slime
3OE1 => 040AC944 60000000 // Hildeblue
040C1B70 60000000 // Rappies
040C3FC8 60000000 // Nar Lily
040EB050 48000010 // Pouilly Slime
3OE2 => 040ACAFC 60000000 // Hildeblue
040C1D08 60000000 // Rappies
040C4160 60000000 // Nar Lily
040EB1E8 48000010 // Pouilly Slime
3OJ2 => 040AC6B8 60000000 // Hildeblue
040C18CC 60000000 // Rappies
040C3D24 60000000 // Nar Lily
040EADAC 48000010 // Pouilly Slime
3OJ3 => 040AC9C4 60000000 // Hildeblue
040C1BD0 60000000 // Rappies
040C4028 60000000 // Nar Lily
040EB0B0 48000010 // Pouilly Slime
3OJ4 => 040ACB3C 60000000 // Hildeblue
040C1E04 60000000 // Rappies
040C41A0 60000000 // Nar Lily
040EB374 48000010 // Pouilly Slime
3OJ5 => 040ACAEC 60000000 // Hildeblue
040C1CF8 60000000 // Rappies
040C4150 60000000 // Nar Lily
040EB1D8 48000010 // Pouilly Slime
3OP0 => 040ACAC4 60000000 // Hildeblue
040C1CD0 60000000 // Rappies
040C4128 60000000 // Nar Lily
040EB1B0 48000010 // Pouilly Slime
Unlock all songs in BGM test
Note: sadly, there are no secret/unused ones
@@ -143,6 +213,9 @@ Auto-press A as fast as possible during loading screens
3SJT => 040C2C48 60000000
3SJ0 => 042F8B74 60000000
CARD lobby battle tables react immediately
3SE0 => 042C04D4 60000000
Change type of all loading screens
Values for X: 0 = lobby/game join, 1 = quest load, 3 = pipe up, 4 = pipe down, anything else = silent black screen
3OE1 => 0401CA04 3BE0000X
@@ -197,12 +270,47 @@ Enable Change Marker option in all lobbies
3OE2 => 041385C8 4800004C
3OP0 => 04138848 4800004C
Lobby arrows rotation speed modifier
3OE1 => 041C6B64 3804XXXX (default 0800)
Change lobby arrow colors
Note: All values as floats in [0, 1]
3OE1 => 04443780 AAAAAAAA (slot 0)
04443784 RRRRRRRR (slot 0)
04443788 GGGGGGGG (slot 0)
0444378C BBBBBBBB (slot 0)
04443790 AAAAAAAA (slot 1)
04443794 RRRRRRRR (slot 1)
04443798 GGGGGGGG (slot 1)
0444379C BBBBBBBB (slot 1)
...
Change HUD color mask
3SE0 => 0438CA8C 3C00RRGG
0438CA90 6000BBAA
Disable lobby event music (but keep the visuals)
3OJT => 040B2394 38000000
3SE0 => 040B705C 38000000
3SJ0 => 040B7078 38000000
3SP0 => 040B74A0 38000000
Disable rate limit for lobby chair movement
3OJ2 => 041C73B0 60000000
3OJ3 => 041C786C 60000000
3OJ4 => 041C7DA8 60000000
3OJ5 => 041C7938 60000000
3OE0 => 041C77CC 60000000
3OE1 => 041C77CC 60000000
3OE2 => 041C799C 60000000
3OP0 => 041C7E58 60000000
3SJT => 040E290C 60000000
3SJ0 => 040DE6C4 60000000
3SE0 => 040DE6A8 60000000
3SP0 => 040DEAEC 60000000
Make lobby chairs fast (client-side only)
3SE0 => 0457E618 40000000
Enable Pinz's Shop Super Card Capsule Machine as a fourth option
3SE0 => 043101C0 38800004
@@ -250,8 +358,14 @@ Unlock all offline free battle maps
This unlocks ALL maps, including a bunch of maps with garbage names that crash if you try to play them
3SJT => 042BE538 38600001
3SJ0 => 042C9C2C 38600001
3SP0 => 042CB50C 38600001
3SE0 => 042CAA00 38600001
3SP0 => 042CB50C 38600001
Card auctions accessible with fewer than 4 players
3SJT => 042DD618 38600004
3SJ0 => 042F4F20 38600004
3SE0 => 042F5D88 38600004
3SP0 => 042F698C 38600004
Talk to auction counter offline to get all cards
3SE0 => 042F5D18 4BD160E8
@@ -417,6 +531,11 @@ Note: Without a TextEnglish.pr2/pr3 patch, the menu items for these sounds will
0442B6E0 802C0000
Use English language files
3OJT => 04189FE8 38000001
0418A010 38000001
0418A0A0 38000001
0418A0C8 38000001
04189EC4 3BC00001
3SJT => 0408E414 38600001
0408E448 38000001
0408E44C 900DA62C
@@ -459,6 +578,68 @@ Heaven Punisher's special always works
3OE2 => 0412AD84 38800001
3OP0 => 0412AF5C 38800001
Fast tekker (skips wind-up jingle)
1OJ1 => 8C15B0CA mov r1, 1
8C15B0E6 nop
1OJ2 => 8C162302 mov r1, 1
8C16231E nop
1OJ3 => 8C175E66 mov r1, 1
8C175E82 nop
1OJ4 => 8C1780AE mov r1, 1
8C1780CA nop
1OJF => 8C17600E mov r1, 1
8C17602A nop
1OEF => 8C17863E mov r1, 1
8C17865A nop
1OPF => 8C1783FA mov r1, 1
8C178416 nop
2OJ5 => 8C19BD4A mov r1, 1
8C19BD66 nop
2OJF => 8C19ADB6 mov r1, 1
8C19ADD2 nop
2OEF => 8C19BD4A mov r1, 1
8C19BD66 nop
2OPF => 8C19B7E2 mov r1, 1
8C19B7FE nop
2OJW => 005B14A3 mov dword [ebx + 0x150], 1
005B14BF jmp +0x0D
2OJZ => 005B0193 mov dword [ebx + 0x150], 1
005B01AF jmp +0x0D
3OJT => 0426FAE8 38000001
0426FB10 60000000
3OJ2 => 0421F8CC 38000001
0421F8F4 60000000
3OJ3 => 04220250 38000001
04220278 60000000
3OJ4 => 04221154 38000001
0422117C 60000000
3OJ5 => 04220EF0 38000001
04220F18 60000000
3OE0 => 04220170 38000001
04220198 60000000
3OE1 => 04220170 38000001
04220198 60000000
3OE2 => 04221224 38000001
0422124C 60000000
3OP0 => 04220ABC 38000001
04220AE4 60000000
4OED => 0023EF3C mov dword [ebp + 0x14C], 1
0023EF57 jmp +0x0A
4OEU => 0023F0BC mov dword [ebp + 0x14C], 1
0023F0D7 jmp +0x0A
4OJB => 0023EC5C mov dword [ebp + 0x14C], 1
0023EC77 jmp +0x0A
4OJD => 0023EEAC mov dword [ebp + 0x14C], 1
0023EEC7 jmp +0x0A
4OJU => 0023F21C mov dword [ebp + 0x14C], 1
0023F237 jmp +0x0A
4OPD => 0023EF5C mov dword [ebp + 0x14C], 1
0023EF77 jmp +0x0A
4OPU => 0023F14C mov dword [ebp + 0x14C], 1
0023F167 jmp +0x0A
59NL => 006DA113 mov dword [edi + 0x14C], 1
006DA130 jmp +0x0B
Allow loading corrupted save files
3OJ2 => 041FC784 38600007
041FC788 4E800020
@@ -703,3 +884,227 @@ Show extended item info when targeting a dropped item
04005188 38210020
0400518C 7C0803A6
04005190 4E800020
All weapons can do 3-hit combos
3OE0 => 041D3248 38000001
3OE1 => 041D3248 38000001
3OE2 => 041D3448 38000001
3OJ2 => 041D2DEC 38000001
3OJ3 => 041D3318 38000001
3OJ4 => 041D3144 38000001
3OJ5 => 041D33E4 38000001
3OP0 => 041D3904 38000001
Disable save file signature validation (for moving Xbox saves across consoles)
4OJB => 002F01CB 9090
4OJD => 002F0CDB 9090
4OJU => 002F22DB 9090
4OED => 002F212B 9090
4OEU => 002F22DB 9090
4OPD => 002F215B 9090
4OPU => 002F234B 9090
Enable UDP test mode online
3OE1 => 041A3D60 38600001
Main warp door opens in Challenge mode
3OE1 => 041820A4 38600001
041820A8 4E800020
Allow arbitrary tech disk levels
3OE1 => 0410EBE8 60000000
04100D18 60000000
041D6C0C 60000000
041D6C5C 60000000
0422CB50 60000000
042CD74C 4E800020
Change particle colors in quest loading screen
3OE1 => 04472C20 AARRGGBB // Default color
04472C24 AARRGGBB // Color after 1 A press
04472C28 AARRGGBB // Color after 2 A presses
04472C2C AARRGGBB // Color after 3 A presses
04472C30 AARRGGBB // Color after 4 A presses
04472C34 AARRGGBB // Color after 5 A presses
Floor warp loading screen speed modifier
// XXXX = speed; default is 01B4; 0800 = very fast/wobbly; 0020 = very slow
3OE1 => 0434A350 3863XXXX
Slow Gibbles fix
3OJ2 => 042D6A48 C022FD98
042D6A6C C022FD98
3OJ3 => 042D7A00 C022FDA0
042D7A24 C022FDA0
3OJ4 => 042D8B34 C022FDA0
042D8B58 C022FDA0
3OJ5 => 042D88E0 C022FDA0
042D8904 C022FDA0
3OE0 => 042D7428 C022FDA8
042D744C C022FDA8
3OE1 => 042D746C C022FDA8
042D7490 C022FDA8
3OE2 => 042D8A94 C022FDA8
042D8AB8 C022FDA8
3OP0 => 042D8228 C022FDA8
042D824C C022FDA8
Override Challenge mode random enemy location tables limit
2OJ5 => 8C2501B2 XXE5 (count as byte)
2OJF => 8C24E98E XXE5 (count as byte)
2OEF => 8C2501A2 XXE5 (count as byte)
2OPF => 8C244C7E XXE5 (count as byte)
2OJW => 005AA2FE XXXXXXXX (count * 4 as little-endian dword)
005AA30C XXXXXXXX (count as little-endian dword)
2OJZ => 005A908E XXXXXXXX (count * 4 as little-endian dword)
005A909D XXXXXXXX (count as little-endian dword)
3OE0 => 04209448 3880XXXX (count as big-endian word)
3OE1 => 04209448 3880XXXX (count as big-endian word)
3OE2 => 0420A330 3880XXXX (count as big-endian word)
3OJ2 => 04208C4C 3880XXXX (count as big-endian word)
3OJ3 => 042094C0 3880XXXX (count as big-endian word)
3OJ4 => 0420A5A8 3880XXXX (count as big-endian word)
3OJ5 => 04209FFC 3880XXXX (count as big-endian word)
3OP0 => 04209D2C 3880XXXX (count as big-endian word)
4OJB => 002E527C XXXXXXXX (count as little-endian dword)
4OJD => 002E5DFC XXXXXXXX (count as little-endian dword)
4OJU => 002E740C XXXXXXXX (count as little-endian dword)
4OED => 002E71DC XXXXXXXX (count as little-endian dword)
4OEU => 002E742C XXXXXXXX (count as little-endian dword)
4OPD => 002E720C XXXXXXXX (count as little-endian dword)
4OPU => 002E745C XXXXXXXX (count as little-endian dword)
59NL => 0080ECB7 XXXXXXXX (count * 4 as little-endian dword)
0080ECD0 XXXXXXXX (count as little-endian dword)
Disable dust effect in CCA
3OJT => 042F4EE8 48000010
3OJ2 => 04297ECC 48000010
3OJ3 => 04298C94 48000010
3OJ4 => 04299DAC 48000010
3OJ5 => 04299B60 48000010
3OE0 => 042987EC 48000010
3OE1 => 04298830 48000010
3OE2 => 04299D14 48000010
3OP0 => 042994BC 48000010
Inventory debugging code
(makes a copy of player 1's inventory at 8000A04C, updated every frame)
3OE2 => 0400A000 9421FFE0 // stwu [r1 - 0x20], r1
0400A004 7C0802A6 // mflr r0
0400A008 90010024 // stw [r1 + 0x24], r0
0400A00C 3C608051 // lis r3, 0x8051
0400A010 8063EA10 // lwz r3, [r3 - 0x15F0] // r3 = TObjPlayer_objs[0]
0400A014 3C808000 // lis r4, 0x8000
0400A018 6084A050 // ori r4, r4, 0xA050
0400A01C 9064FFFC // stw [r4 - 4], r3 // 8000A04C = 0 (in case player is null)
0400A020 28030000 // cmplwi r3, 0
0400A024 41820014 // beq +0x10
0400A028 481AE2E9 // bl TObjPlayer_export_inventory // (TObjPlayer_objs[0], 0x8000A050)
0400A02C 3C808000 // lis r4, 0x8000
0400A030 6084A04C // ori r4, r4, 0xA04C
0400A034 90640000 // stw [r4], r3 // 8000A04C = inventory item count
0400A038 80010024 // lwz r0, [r1 + 0x24]
0400A03C 7C0803A6 // mtlr r0
0400A040 38210020 // addi r1, r1, 0x20
0400A044 4E800020 // blr
041A39B8 4BE66648 // b 8000A000 // main_phase_0E_exec_frame return - chain to hook at 8000A000
Load qdefault.bin quest script from disk in offline free play
(Don't use this on a disc image where qdefault.bin doesn't exist; there is a bug in the quest script environment constructor that will leave the current directory set incorrectly if the file doesn't exist, and the game will softlock)
3OE1 => 041A3A30 4BE6656D
041A3088 4BE66F1D
04009F9C 38600002
04009FA0 48000008
04009FA4 38600000
04009FA8 7C0802A6
04009FAC 9421FFE0
04009FB0 90010024
04009FB4 90610008
04009FB8 386001A4
04009FBC 4821F581
04009FC0 28030000
04009FC4 41820018
04009FC8 808DBD20
04009FCC 3CA08000
04009FD0 60A59FF0
04009FD4 38C00000
04009FD8 481EC171
04009FDC 80610008
04009FE0 80010024
04009FE4 38210020
04009FE8 7C0803A6
04009FEC 4E800020
04009FF0 71646566
04009FF4 61756C74
04009FF8 2E62696E
04009FFC 00000000
3OE2 => 041A3B5C 4BE66441
041A31B0 4BE66DF5
04009F9C 38600002
04009FA0 48000008
04009FA4 38600000
04009FA8 7C0802A6
04009FAC 9421FFE0
04009FB0 90010024
04009FB4 90610008
04009FB8 386001A4
04009FBC 48220635
04009FC0 28030000
04009FC4 41820018
04009FC8 808DBD40
04009FCC 3CA08000
04009FD0 60A59FF0
04009FD4 38C00000
04009FD8 481EC309
04009FDC 80610008
04009FE0 80010024
04009FE4 38210020
04009FE8 7C0803A6
04009FEC 4E800020
04009FF0 71646566
04009FF4 61756C74
04009FF8 2E62696E
04009FFC 00000000
Enable quest board menu in free play (for use with the above code)
3OE0 => 04262B44 38600001
3OE1 => 04262B44 38600001
3OE2 => 04263F04 38600001
3OJ2 => 0426226C 38600001
3OJ3 => 04262E44 38600001
3OJ4 => 04263EB8 38600001
3OP0 => 0426374C 38600001
All classes' footsteps sound like RAcast's
(Change the 2 in 38600002 to 0 for human/Newman, 1 for lighter androids, or 3 if you want to be annoyed)
3OE0 => 041B3ED0 38600002
041B3ED4 4E800020
3OE1 => 041B3ED0 38600002
041B3ED4 4E800020
3OE2 => 041B4068 38600002
041B406C 4E800020
3OJ2 => 041B3AE4 38600002
041B3AE8 4E800020
3OJ3 => 041B3F38 38600002
041B3F3C 4E800020
3OJ4 => 041B552C 38600002
041B5530 4E800020
3OJ5 => 041B4004 38600002
041B4008 4E800020
3OJT => 0420A120 38600002
0420A124 4E800020
3OP0 => 041B4524 38600002
041B4528 4E800020
3SE0 => 040D0378 38600002
040D037C 4E800020
3SJ0 => 040D0394 38600002
040D0398 4E800020
3SJT => 040D431C 38600002
040D4320 4E800020
3SP0 => 040D07BC 38600002
040D07C0 4E800020
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)
File diff suppressed because one or more lines are too long
+6 -5
View File
@@ -1,5 +1,6 @@
DC NTE: pso02.dricas.ne.jp
Nov 2000 proto: test1.st-pso.games.sega.net
Dec 2000 proto: sg107634.csrd.sega.co.jp OR master.pso.dream-key.com
Jan 2001 proto: master.pso.dream-key.com
Aug 2001 proto (v2): game01.st-pso.games.sega.net
1OJ1 (DC NTE): pso02.dricas.ne.jp
1OJ2 (11/2000): test1.st-pso.games.sega.net
1OJ3 (12/2000): sg107634.csrd.sega.co.jp OR master.pso.dream-key.com
1OJ4 (01/2001): master.pso.dream-key.com
2OJ4 (08/06/2001; v2): game01.st-pso.games.sega.net
2OJ5 (08/22/2001; v2): game01.st-pso.games.sega.net
+2
View File
@@ -1,4 +1,6 @@
List of differences in Ep3 NTE compared to Final:
- COMs can play more than one defense card per turn
- The battle setup menu allows 1v2 battles
- Assist cards
- - Dice Fever sets dice to 6, not 5, and there is no Dice Fever +
- - Rich + and Charity + also don't exist
+979 -979
View File
File diff suppressed because it is too large Load Diff
+3 -3
View File
@@ -1,7 +1,7 @@
patch required in TethVer12513 to get this to work: 0048210D EB
patch required in 59NL to get this to work: 0048210D EB
is_hangame callsites:
0040457C - ??? (something in TDataProtocol?)
is_hangame callsites in 59NL:
0040457C - don't save password on disconnect
004820F4 - client version check (use patch above to bypass)
00708318 - patch server domain name
00708348 - patch server port
+586
View File
@@ -0,0 +1,586 @@
NOTE: The NNF descriptions are from Kayak's movement data notes: https://qedit.info/index.php?title=Get_movement_data
MOVEMENT DATA 00 (BOOTA)
MOVEMENT DATA 01 (ZE_BOOTA)
MOVEMENT DATA 03 (BA_BOOTA)
MOVEMENT DATA 11 (GORAN)
MOVEMENT DATA 12 (PYRO_GORAN)
MOVEMENT DATA 13 (GORAN_DETONATOR)
MOVEMENT DATA 4A (BOOMA, MERILLIA)
MOVEMENT DATA 4B (GOBOOMA, MERILTAS)
MOVEMENT DATA 4C (GIGOBOOMA)
MOVEMENT DATA 4E (EVIL_SHARK, DOLMOLM)
MOVEMENT DATA 4F (PAL_SHARK, DOLMDARL)
MOVEMENT DATA 50 (GUIL_SHARK)
MOVEMENT DATA 52 (DIMENIAN)
MOVEMENT DATA 53 (LA_DIMENIAN)
MOVEMENT DATA 54 (SO_DIMENIAN)
fparam1 = idle move speed (when returning to initial position)
fparam2 = idle walking animation speed
fparam3 = engaged move speed (when approaching a player)
fparam4 = engaged animation speed
fparam5 = MERILLIA, MERILTAS; TODO: 3OE1:800D5750; poison cloud damage
fparam5 = DOLMOLM, DOLMDARL; TODO: 3OE1:802FFA70; max distance to notice player?; NNF: Possibly frames before getting trapped
fparam5 = BOOTA, ZE_BOOTA, BA_BOOTA; TODO: 59NL:005A56B5, 59NL:005A5361
fparam5 = GORAN, PYRO_GORAN, GORAN_DETONATOR; TODO: 59NL:005ACDD2
fparam6 = MERILLIA, MERILTAS; TODO: 3OE1:800D7074; NNF: run away speed
fparam6 = GORAN, PYRO_GORAN, GORAN_DETONATOR; TODO: 59NL:005AD31E, 59NL:005ACC23, 59NL:005ACB4A; length of a vector (speed?)
iparam1 = MERILLIA, MERILTAS; low HP threshold percentage (0-100); controls how often it runs away
iparam1 = DOLMOLM, DOLMDARL; TODO: 3OE1:802FFFBC; NNF: Angle to use Trap (Cannot exceed Attack data angle)
iparam1 = BOOTA, ZE_BOOTA, BA_BOOTA; TODO: 59NL:005A53D7, 59NL:005A531C, 59NL:005A5092 (special case for 01 only apparently?)
iparam1 = GORAN, PYRO_GORAN, GORAN_DETONATOR; TODO: 59NL:005AD0CF, 59NL:005ACE95; looks like an angle in degrees (range [0, 359])
iparam2 = DOLMOLM, DOLMDARL; TODO: 3OE1:80300154; NNF: Length of time in seconds Dolmolm trap lasts
iparam2 = BOOTA, ZE_BOOTA, BA_BOOTA; TODO: 59NL:005A5580
iparam2 = GORAN, PYRO_GORAN, GORAN_DETONATOR; TODO: 59NL:005ACD0F
MOVEMENT DATA 00 (MOTHMANT)
fparam1 = speed when low to the ground (chase mode)
iparam2 = delay before attack (applies when in chase mode and reached target, or between attacks when near target)
MOVEMENT DATA 01 (MONEST)
(loaded with assets but not used)
MOVEMENT DATA 02 (SAVAGE_WOLF)
MOVEMENT DATA 03 (BARBAROUS_WOLF)
(loaded with assets but not used)
MOVEMENT DATA 04 (POISON_LILY)
MOVEMENT DATA 05 (NAR_LILY)
MOVEMENT DATA 25 (DEL_LILY)
fparam1 = DEL_LILY; TODO: 3OE1:800C25C8; damage for some kind of attack
iparam1 = POISON_LILY, NAR_LILY; Megid level, only used in Ultimate
MOVEMENT DATA 05 (SAND_RAPPY_CRATER)
MOVEMENT DATA 06 (DEL_RAPPY_CRATER)
MOVEMENT DATA 17 (SAND_RAPPY_DESERT)
MOVEMENT DATA 18 (RAG_RAPPY, DEL_RAPPY_DESERT)
MOVEMENT DATA 19 (AL_RAPPY, LOVE_RAPPY, SAINT_RAPPY, EGG_RAPPY, HALLO_RAPPY)
fparam1 = hitbox radius
fparam2 = TODO: 3OE1:TObjEneLappy_set_params_from_movement_data, 59NL:FUN_00526A7C; NNF: Flinch time (higher number is less flinch time)
fparam3 = TODO: 3OE1:TObjEneLappy_set_params_from_movement_data, 59NL:FUN_00526A7C; NNF: Animation speed at which their legs move after getting hit once they fake die (negative values make them run underground, 0 makes it so their legs don't move along and they just slide across the ground lol.)
MOVEMENT DATA 06 (SINOW_BEAT, SINOW_BERILL)
MOVEMENT DATA 10 (SINOW_GOLD, SINOW_SPIGELL)
fparam1 = TODO: 3OE1:800E63F0, 3OE1:800F38D8; NNF: Movement speed
fparam2 = SINOW_BEAT, SINOW_GOLD; TODO: 3OE1:800E6410; NNF: Clone movement speed (invisible flag must be set to 0 to get clones)
fparam3 = TODO: 3OE1:800E5D0C, 3OE1:800F3234; NNF: The speed (and amount) it moves forward right when it is about to attack you
fparam4 = TODO: 3OE1:800E7BA0, 3OE1:800E7DA8, 3OE1:800F53E4
fparam5 = TODO: 3OE1:800E7BA8, 3OE1:800F53EC
fparam6 = SINOW_BERILL, SINOW_SPIGELL; TODO: 3OE1:800F21D4, 3OE1:800F2E84; probability of something (0-1)
iparam1 = Shifta/Deband/Resta level
iparam2 = TODO: 3OE1:800E5200, 3OE1:800F2B8C
iparam3 = TODO: 3OE1:800E5D78, 3OE1:800F32A0; NNF: Amount of time (in frames) that the sinow pauses after it attacks
iparam4 = SINOW_BERILL, SINOW_SPIGELL; attack tech level (Rafoie in Ultimate, Gifoie otherwise)
MOVEMENT DATA 07 (CANADINE)
MOVEMENT DATA 08 (CANADINE_RING)
MOVEMENT DATA 09 (CANANE)
fparam1 = TODO: 3OE1:8009CEF4; NNF: Movement speed of animation perfomed just before melee attack
fparam2 = electrical attack damage
fparam3 = explosion damage
iparam1 = TODO: 3OE1:8009D4AC; NNF: Zonde attack charge time (higher is longer)
iparam2 = TODO: 3OE1:8009D508; NNF: Delay after laser targetting ends before shooting Zonde
iparam3 = TODO: 3OE1:8009D59C; NNF: Delay after casting Zonde
iparam4 = TODO: 3OE1:8009D5B8; NNF: Number of times Zonde is cast before they go to the next cycle
iparam5 = TODO: 3OE1:8009C5C8; NNF: stun frames after being hit
iparam6 = CANANE; TODO: 3OE1:8009B148; number of out-fighters (see CANADINE description in Map.cc); NNF: How many of the 8 ring Canadines will cast Zonde (numbers greater than 8 are treated as 8). The remaining number out of 8 will perform melee attacks instead. Value of 0 causes FSOD.
MOVEMENT DATA 07 (GEE)
iparam5 = TODO: 3OE1:800C9778; probably same as CANADINE (movement data 07 iparam5)
MOVEMENT DATA 07 (ZU_CRATER)
MOVEMENT DATA 08 (PAZUZU_CRATER)
MOVEMENT DATA 1B (ZU_CRATER)
MOVEMENT DATA 1C (PAZUZU_CRATER)
fparam1 = TODO: 59NL:005B4A3C
MOVEMENT DATA 08 (PIG_RAY)
MOVEMENT DATA 09 (UL_RAY)
fparam1 = TODO: 3OE1:803072B0, 3OE1:80307354; speed?
iparam1 = TODO: 3OE1:803075FC; frame count for something
MOVEMENT DATA 09 (ASTARK)
fparam1 = TODO: 59NL:005A2B8F
fparam2 = TODO: 59NL:005A2E1E
fparam3 = TODO: 59NL:005A31D1
fparam4 = TODO: 59NL:005A3124
fparam5 = TODO: 59NL:005A4992
fparam6 = TODO: 59NL:005A2B79
iparam1 = TODO: 59NL:005A4947
iparam2 = TODO: 59NL:005A499D
MOVEMENT DATA 0A (CHAOS_SORCERER)
iparam1 = attack tech 1 level (Grants in Ultimate, Gizonde in non-Ultimate Ep2, Rafoie in non-Ultimate Ep1)
iparam2 = attack teck 2 level (Megid in Ultimate, Gibarta otherwise)
iparam3 = Resta level
MOVEMENT DATA 0B (BEE_R)
MOVEMENT DATA 0C (BEE_L)
(loaded with assets but not used)
MOVEMENT DATA 0D (SATELLITE_LIZARD_CRATER)
MOVEMENT DATA 0E (YOWIE_CRATER)
MOVEMENT DATA 1D (SATELLITE_LIZARD_DESERT)
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)
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.
iparam2 = TODO: 3OE1:FUN_80097F98; NNF: cooldown time after shooting
iparam3 = damage for charge attack; 3OE1:80099128
iparam4 = TODO: 3OE1:80097A30; NNF: laser attack damage
iparam5 = TODO: 3OE1:FUN_800983F8; NNF: swing attack radius
iparam6 = TODO: 3OE1:FUN_800983F8; NNF: charge attack radius (if player is outside this range)
MOVEMENT DATA 0D (DELBITER)
fparam1 = TODO: 3OE1:80302D1C, 3OE1:80302B38, 3OE1:803033C8, 3OE1:8030344C; NNF: Charge speed
fparam2 = TODO: 3OE1:80303124; NNF: Walking speed
fparam3 = TODO: 3OE1:80304F00
fparam4 = TODO: 3OE1:80304F10
fparam5 = TODO: 3OE1:80302E34
fparam6 = TODO: 3OE1:80302FD8; NNF: Charge radius (how far away you have to be before it charges).
iparam1 = TODO: 3OE1:80302A6C
iparam2 = TODO: 3OE1:803042F8
iparam3 = TODO: 3OE1:80304368; NNF: Charge damage.
iparam4 = TODO: 3OE1:80302414; related to TP absorption; NNF: Laser damage.
iparam5 = TODO: 3OE1:803030A4; NNF: Radius at which Delbiter attempts foot stomp attack (the range at which that attack can hit you, however, is not modified).
iparam6 = TODO: 3OE1:8030267C
MOVEMENT DATA 0E (DARK_BELRA)
(loaded with assets but not used)
MOVEMENT DATA 0F (DE_ROL_LE, BARBA_RAY)
fparam1 = DE_ROL_LE; TODO: damage amount; 3OE1:800304A4
fparam1 = BARBA_RAY; TODO: 3OE1:802E7980; damage for some attack; NNF: laser damage
fparam2 = DE_ROL_LE; TODO: TObjectV8047ec78 which has no constructor, so this is unused?; 3OE1:80038FD8
fparam2 = BARBA_RAY; TODO: 3OE1:802EDA38; TBoss7PhotonBullet_update; NNF: missile damage
fparam3 = DE_ROL_LE; TODO: TBoss2Mine, appears to be mine explosion damage; 3OE1:800385E4; NNF: Missile damage
fparam4 = DE_ROL_LE; TODO: multiplied by a random number in range [-1, 1] and added to pos.x; only happens if param5 passes
fparam5 = DE_ROL_LE; TODO: probability of some kind (range [0, 1]); 3OE1:80030C80
iparam1 = total HP
iparam2 = HP until armor on joints falls off
iparam3 = HP until mask falls off
iparam4 = DE_ROL_LE; TODO: only used in Ultimate, in other difficulties 180 is used instead
iparam5 = DE_ROL_LE; TODO: only used in Ultimate, in other difficulties 120 is used instead
MOVEMENT DATA 0F (DORPHON)
MOVEMENT DATA 10 (DORPHON_ECLAIR)
fparam1 = TODO: 59NL:005A832F, 59NL:005A8364, 59NL:005A8388, 59NL:005A8A9A, 59NL:005A9643, 59NL:005A96E5
fparam2 = TODO: 59NL:005A8EC2, 59NL:005A903D
fparam3 = TODO: 59NL:FUN_005A9ADC; minimum 0.1
fparam4 = TODO: 59NL:FUN_005A9ADC; minimum 0.1
fparam5 = TODO: 59NL:005A85AB
fparam6 = TODO: 59NL:005A8F2D
iparam1 = TODO: 59NL:005A8082
iparam2 = TODO: 59NL:005A89C6 and many others
iparam3 = TODO: 59NL:005A8477 and many others
iparam4 = TODO: 59NL:005A79E6; looks like same as for DELBITER
iparam5 = TODO: 59NL:005A8E4D
iparam6 = TODO: 59NL:005A71DA; multiplied by 30
MOVEMENT DATA 11 (DRAGON, GOL_DRAGON)
fparam1 = DRAGON; TODO: TBoss1DragonEffBreath
fparam1 = GOL_DRAGON; TODO: 3OE1:802F98EC; damage for some attack
fparam2 = DRAGON; TODO: TObjBoss1Crater_update, multiplied by 0.666 internally; TBoss1Dragon @ 3OE1:800276E0
fparam2 = GOL_DRAGON; TODO: 3OE1:802F987C; damage for some attack
fparam3 = DRAGON; TODO: 3OE1:8002787C
fparam3 = GOL_DRAGON; TODO: 3OE1:802F9810; damage for some attack
fparam4 = DRAGON; TODO: hitbox radius for something
fparam4 = GOL_DRAGON; TODO: 3OE1:802F9DBC; range for some attack
fparam5 = DRAGON; TODO: only used in Ultimate, in other difficulties 0.8 is used instead
fparam5 = GOL_DRAGON; TODO: 3OE1:802F2FDC, 3OE1:802F38A8, 3OE1:802F3AFC, 3OE1:802F8800
fparam6 = DRAGON; TODO: only used in Ultimate, in other difficulties 2.0 is used instead
fparam6 = GOL_DRAGON; TODO: 3OE1:802F7BBC, 3OE1:802F7C34
iparam1 = TODO: 3OE1:TBoss8Dragon_v58; damage amount for 1 hitbox
iparam2 = TODO: 3OE1:TBoss8Dragon_v58; damage amount for 2 hitboxes
iparam3 = TODO: 3OE1:TBoss8Dragon_v58; damage amount for 4 hitboxes
iparam4 = GOL_DRAGON; clone HP
iparam5 = GOL_DRAGON; TODO: 3OE1:802F32C8; which clone to create? (should be in range [0, 5])
MOVEMENT DATA 12 (GOL_DRAGON)
fparam1 = TODO: 3OE1:FUN_802FC22C
fparam2 = TODO: 3OE1:FUN_802FC22C
fparam3 = TODO: 3OE1:FUN_802FC22C
fparam4 = TODO: 3OE1:FUN_802FC22C
fparam5 = TODO: 3OE1:FUN_802FC22C; same function as fparam1 but used when no clones exist?
fparam6 = TODO: 3OE1:FUN_802FC22C; same function as fparam2 but used when no clones exist?
MOVEMENT DATA 13 (GOL_DRAGON)
fparam1 = TODO: 3OE1:FUN_802FC22C; same function as movement data 12 fparam3 but used when no clones exist?
fparam2 = TODO: 3OE1:FUN_802FC22C; same function as movement data 12 fparam4 but used when no clones exist?
fparam3 = TODO: 3OE1:802FBDBC; HP for phase 2 to begin?
fparam4 = TODO: 3OE1:802F6F24; scaling factor for a vector (speed/range?)
MOVEMENT DATA 19 (MERISSA_A)
MOVEMENT DATA 1A (MERISSA_AA)
fparam1 = TODO: 59NL:005B70AC
fparam2 = TODO: 59NL:005B70AC
fparam3 = TODO: 59NL:005B70AC
fparam4 = TODO: 59NL:005B5750, 59NL:005B6101
iparam1 = TODO: 59NL:005B56F8, 59NL:005B61DE; looks like an angle in degrees (range [0, 359])
iparam2 = TODO: 59NL:005B5824; looks like an angle in degrees (range [0, 359])
MOVEMENT DATA 1A (NANO_DRAGON)
fparam1 = horizontal flight speed
fparam2 = straight laser speed
fparam3 = homing laser speed (if set too low, it will go backwards)
fparam4 = TODO: 3OE1:800D9C70; NNF: Homing laser projectile count (projectile number = number given).
fparam5 = TODO: 3OE1:800D9C70; NNF: Homing laser arc.
iparam1 = straight laser damage
iparam2 = homing laser damage
MOVEMENT DATA 1A (GI_GUE)
fparam1 = TODO: 3OE1:802CA8F4, 3OE1:802CAA04; looks like a scape factor; NNF: Speed when flying away.
fparam2 = TODO: 3OE1:TObjEneMe1GiGue_FUN_802C98FC; NNF: missile speed
fparam3 = TODO: 3OE1:TObjEneMe1GiGue_FUN_802C98FC; NNF: confuse projectile speed
fparam4 = TODO: 3OE1:802CCA18
fparam5 = TODO: 3OE1:802CC640
fparam6 = TODO: 3OE1:802CA274; probability in range [0, 1]
iparam1 = TODO: 3OE1:TObjEneMe1GiGue_FUN_802C98FC; minimum value 40; NNF: Rafoie bomb attack damage
iparam2 = TODO: 3OE1:TObjEneMe1GiGue_FUN_802C98FC; NNF: Confusion projectile damage (affected by EFR).
iparam3 = Jellen/Zalure level
iparam4 = TODO: 3OE1:TObjEneMe1GiGue_FUN_802C98FC
iparam5 = TODO: 3OE1:TObjEneMe1GiGue_FUN_802C98FC; minimum value 20 in one scenario, 40 in another
MOVEMENT DATA 1B (DUBCHIC)
MOVEMENT DATA 1C (GILLCHIC)
fparam1 = TODO: 3OE1:FUN_800A89D4; NNF: Punch speed. Higher values means faster punches.
fparam2 = punch attack range when not damaged
fparam3 = TODO: 3OE1:800A8B64, 3OE1:800A9E98; only used when damaged, values when not damaged are 0.37037036 for DUBCHIC, 0.57037038 for GILLCHIC (unused since GILLCHIC dies instead of being damaged)
fparam4 = TODO: 3OE1:FUN_800A89D4
fparam5 = TODO: 3OE1:FUN_800A89D4; NNF: Punch speed and movement speed when damaged
fparam6 = punch attack range when damaged
iparam1 = number of frames after kill before revive sequence starts (Dubchic only)
iparam2 = TODO: 3OE1:800A8F9C; NNF: Laser charge time
iparam3 = TODO: 3OE1:800A9B40; NNF: Number of invicibility frames after knockdown
iparam4 = laser damage
MOVEMENT DATA 1D (GARANZ)
fparam1 = TODO: 3OE1:800D320C; NNF: Distance travelled every movement phase. Speed is unaffected, so it can take a long time before it stops to shoot.
fparam2 = TODO: 3OE1:TObjEneGyaranzo_set_movement_params; NNF: Movement speed. This not only makes the Garanz faster, but ends the movement phase sooner, so it gets around to shooting missiles faster too. Doesn't work well without a value in fparam1.
fparam3 = TODO: 3OE1:TObjEneGyaranzo_set_movement_params; NNF: TODO
fparam4 = missile speed
fparam5 = TODO: 3OE1:TObjEneGyaranzo_set_movement_params; NNF: Missile launch arc. Defines how tight the downward curve of the missile (once launched) towards the player is. Set to 0, missiles travel straight into the ceiling and cannot hit the player.
iparam1 = TODO: 3OE1:800D2C4C; NNF: Number of frames waited after shooting before commencing movement again. Garanz does have a lower limit and will not wait 0 frames before starting again.
iparam2 = TODO: 3OE1:800D2254; NNF: Missile launch cooldown
iparam3 = TODO: 3OE1:800D46A8; missile damage
iparam4 = TODO: 3OE1:800D40FC; NNF: Mine Damage
MOVEMENT DATA 1E (DARK_GUNNER)
fparam1 = TODO: 3OE1:800A0F44, 3OE1:800A11D0
fparam2 = TODO: 3OE1:800A24F8
fparam3 = TODO: 3OE1:800A1C4C; seems to be a distance limit / radius of some sort
fparam4 = TODO: 3OE1:800A1104; NNF: laser speed
iparam1 = charge time after windup sound and before laser shot
iparam2 = TODO: 3OE1:800A12A4; NNF: Length of time vulnerability remains after being damaged (lower=shorter)
iparam3 = TODO: 3OE1:800A3190; NNF: Duration of invincibility (close to 0 will be no invincibility).
iparam4 = laser shot damage
MOVEMENT DATA 1E (GAL_GRYPHON)
fparam1 = TODO: 3OE1:80065DEC; NNF: Y Value Camera adjustment when Gal lands
fparam2 = TODO: 3OE1:80065DEC; NNF: X Value Camera adjustment when Gal lands
fparam3 = TODO: 3OE1:80065DEC; NNF: Y Value Camera adjustment when Gal lands (cam location)
fparam4 = TODO: 3OE1:80065DEC; NNF: Adjusts Camera near or far to player
fparam5 = TODO: 3OE1:80065DEC; same as fparam1 but for a different situation (A); NNF: Lowers/Raises the Camera when Gal is flying
fparam6 = TODO: 3OE1:80065DEC; same as fparam2 but for a different situation (A)
MOVEMENT DATA 1F (BULCLAW)
iparam1 = TODO: 3OE1:8008F8C8; percentage (0-100) of max HP; NNF: % chance it does it's suicide attack once split into a Bulk, you need to attack it once
MOVEMENT DATA 1F (GAL_GRYPHON)
fparam1 = TODO: 3OE1:80065DEC; same as data 1E fparam3 but for a different situation (A); NNF: (BULCLAW) Aggro Range?
fparam2 = TODO: 3OE1:80065DEC; same as data 1E fparam4 but for a different situation (A)
fparam3 = TODO: 3OE1:80065DEC; same as data 1E fparam1 but for a different situation (B)
fparam4 = TODO: 3OE1:80065DEC; same as data 1E fparam2 but for a different situation (B)
fparam5 = TODO: 3OE1:80065DEC; same as data 1E fparam3 but for a different situation (B)
fparam6 = TODO: 3OE1:80065DEC; same as data 1E fparam4 but for a different situation (B)
MOVEMENT DATA 1F (GIRTABLULU)
fparam1 = TODO: 59NL:005ABDBD
fparam2 = TODO: 59NL:005ABDB1
fparam4 = TODO: 59NL:005ABD3C
fparam5 = TODO: 59NL:005ABD45
fparam6 = TODO: 59NL:005ABD08; looks like an angle in degrees (range [0, 359])
iparam1 = TODO: 59NL:005AAB66, 59NL:005AAD18
iparam3 = TODO: 59NL:005AA9FA
iparam4 = TODO: 59NL:005AA85B
iparam5 = TODO: 59NL:005AAF20; length of time in frames?
iparam6 = TODO: 59NL:005AA5FD
MOVEMENT DATA 20 (BULCLAW)
(loaded with assets but not used)
MOVEMENT DATA 20 (GAL_GRYPHON)
fparam1 = TODO: 3OE1:FUN_80064064; damage scaling factor for some attack (TBoss5GryphonSnarl)
fparam2 = TODO: 3OE1:80064130; damage amount for shock wave attack (TBoss5GryphonShockWave)
fparam3 = TODO: 3OE1:80064130; damage amount for tornado attack (TBoss5GryphonTornado)
fparam4 = TODO: 3OE1:80064044; damage amount for some attack
fparam5 = TODO: 3OE1:8006475C; hitbox radius for some attack?
iparam1 = TODO: 3OE1:TBoss5Gryphon_V58; damage amount for 1 hitbox
iparam2 = TODO: 3OE1:TBoss5Gryphon_V58; damage amount for 4 hitboxes
iparam3 = TODO: 3OE1:TBoss5Gryphon_V58; damage amount for 1 hitbox
iparam4 = TODO: 3OE1:TBoss5Gryphon_V58; damage amount for 4 hitboxes
iparam5 = TODO: 3OE1:TBoss5Gryphon_FUN_8005F0F0, 3OE1:800609D0
MOVEMENT DATA 20 (SAINT_MILLION_1)
MOVEMENT DATA 22 (SAINT_MILLION_2)
MOVEMENT DATA 24 (SHAMBERTIN_1)
MOVEMENT DATA 26 (SHAMBERTIN_2)
MOVEMENT DATA 28 (KONDRIEU_1)
MOVEMENT DATA 2A (KONDRIEU_2)
iparam1 = TODO: 59NL:00768990, 59NL:00768A84
iparam2 = TODO: 59NL:00768990, 59NL:00768A84
iparam3 = TODO: 59NL:00768990, 59NL:00768A84
iparam4 = TODO: 59NL:00768990, 59NL:00768A84
iparam5 = TODO: 59NL:00768990, 59NL:00768A84
MOVEMENT DATA 21 (SAINT_MILION_SPINNER, 0/4/8/12)
MOVEMENT DATA 23 (SAINT_MILION_SPINNER, other indexes)
MOVEMENT DATA 25 (SHAMBERTIN_SPINNER, 0/4/8/12)
MOVEMENT DATA 27 (SHAMBERTIN_SPINNER, other indexes)
MOVEMENT DATA 29 (KONDRIEU_SPINNER, 0/4/8/12)
MOVEMENT DATA 2B (KONDRIEU_SPINNER, other indexes)
iparam1 = TODO: 59NL:0076D40D
MOVEMENT DATA 21 (VOL_OPT_1)
iparam1 = speed of moving around in the screens
MOVEMENT DATA 22 (VOL_OPT_1)
iparam1 = damage for electrical attack
MOVEMENT DATA 23 (VOL_OPT_1)
iparam1 = large monitors' HP
iparam2 = small monitors' HP
MOVEMENT DATA 23 (EPSILON)
fparam2 = TODO: 3OE1:8035FDB4; scale factor for vector; NNF: Laser tracking speed.
fparam3 = TODO: 3OE1:8035FF08; NNF: Rafoie damage (based on MST).
iparam1 = TODO: 3OE1:8035FD60; NNF: Controls how long the laser tracks players before casting Rafoie (number of Rafoies shot is tied to this - shorter tracking time means more Rafoies).
iparam2 = TODO: 3OE1:8035FE40; NNF: Delay between when Rafoie stops and next laser begins.
iparam3 = TODO: 3OE1:8035E44C, 3OE1:803608C0; NNF: Cooldown on Epsigard tech activation.
iparam4 = TODO: 3OE1:8035F850; NNF: Epsigard attack radius.
MOVEMENT DATA 24 (VOL_OPT)
(loaded with assets but not used)
MOVEMENT DATA 24 (EPSIGARD)
fparam1 = TODO: 3OE1:8035CB58, 3OE1:8035CD1C, 3OE1:8035D3B4; NNF: Epsigard circle radius.
fparam2 = TODO: 3OE1:8035CD20; NNF: Speed at which Epsigards eject from Epsilon. Epsigards always eject for a second, so fast eject speeds will project them far. They will then spin come back in to fparam1 radius.
fparam3 = TODO: 3OE1:8035CB50, 3OE1:8035CBFC; NNF: Epsigard rotation speed.
fparam4 = TODO: 3OE1:8035D0CC; NNF: Damage dealt per Epsigard hit.
iparam1 = TODO: 3OE1:8035CF28; NNF: Seems to affect Epsigard damage radius. At 120, can't get hit from the front, can only gt hit from a specific position from the back and to the side.
MOVEMENT DATA 25 (VOL_OPT_2)
fparam1 = TODO: specifies length of a vector; NNF: missile speed; 3OE1:80049CB0
fparam2 = TODO: specifies length of a vector; 3OE1:80049C94
fparam3 = TODO: NNF: knockback distance when hit by pillar; player gets rotated in a random direction, and then moved backwards from that direction
fparam4 = TODO: 3OE1:80049FE0; NNF: Homing pillar stomp: Affects cooldown of third pillar ('fast' pillar variants only).
fparam5 = TODO: add param for random generation for pillar stomp; NNF: Homing pillar stomp: Affects cooldown of second pillar ('fast' pillar variants only).
fparam6 = TODO: mult param for random generation for pillar stomp; final value is (random(0, 1) * fparam5) + fparam4; 3 values generated in total; NNF: Homing pillar stomp: Affects cooldown of first pillar ('fast' pillar variants only).
iparam1 = TODO: NNF: missile damage
iparam2 = TODO: NNF: pillar damage
iparam3 = TODO: NNF: trap laser damage
iparam4 = HP recovery amount * 5 (so e.g. 2500 here means 500HP)
iparam5 = TODO: NNF: Charge time of trap laser attack; value used is max(10, iparam5 + 120); but used in multiple places! which is which? 3OE1:800490D0 3OE1:8004661C
iparam6 = TODO: related to TObjVoloptPillar; 3OE1:80044ACC, 3OE1:80047110, 3OE1:8004A24C; NNF: Homing pillar stomp: Cooldown for each pillar drop. Longer is higher.
MOVEMENT DATA 26 (VOL_OPT_2)
fparam1 = TODO: specifies length of a vector; 3OE1:80049778, 3OE1:800499E8; NNF: Ball speed for laser floor trap
fparam2 = TODO: specifies length of a vector; 3OE1:8004975C
iparam1 = TODO: looks like lifetime in frames for a subordinate; 3OE1:80049A14; NNF: Ball chase duration
iparam2 = TODO: 3OE1:8004490C; looks like an angular velocity?; NNF: Amount of wait time taken for rotating pillars to first attack
MOVEMENT DATA 26 (ILL_GILL)
fparam1 = TODO: 3OE1:803642E8, 3OE1:80363DBC, 3OE1:80363FCC; NNF: Affects charge speed and cooldown time.
fparam2 = TODO: 3OE1:80364F3C; NNF: Scythe attack speed.
iparam1 = TODO: 3OE1:80365324, 3OE1:803652CC; weapon special amount; NNF: Seems to affect how much damage the lightning scythe attack does, and how effective the megid scythe attack is (lower is less effective)
iparam2 = TODO: 3OE1:8036537C; weapon special amount; NNF: Seems to affect how much damage the lightning scythe attack does, and how effective the megid scythe attack is (lower is less effective)
MOVEMENT DATA 27 (VOL_OPT; used in Vol Opt phase 1?)
MOVEMENT DATA 28 (VOL_OPT; used when no player is caught by the Vol Opt cage)
MOVEMENT DATA 29 (VOL_OPT; used when any player is caught by the Vol Opt cage)
fparam1 = TODO: param to some camera logic
fparam2 = TODO: param to some camera logic
fparam3 = TODO: param to some camera logic
fparam4 = TODO: param to some camera logic
iparam1 = TODO: entire movement data is unused if this is zero; 3OE1:TBoss3Volopt_FUN_8003EB6C
MOVEMENT DATA 2A (VOL_OPT_2)
iparam1 = TODO: only has effect if nonzero; 3OE1:80048074
MOVEMENT DATA 2B (OLGA_FLOW_1)
fparam1 = TODO: 3OE1:802B6190, 3OE1:803547F4; must be >0, default 20; NNF: sword damage
fparam2 = TODO: 3OE1:80320F84; NNF: Olga Flow 1 shot (ball) damage
fparam3 = TODO: 3OE1:802B5DD0; must be >0, default 20; NNF: tail swipe damage
fparam4 = TODO: 3OE1:802B5980; must be >0, default 20; NNF: shot (beam) damage
fparam5 = TODO: 3OE1:802B5668; must be >0, default 20; NNF: gravity trap attack damage
fparam6 = TODO: 3OE1:802B2620; must be >0, default 7; NNF: delay between attacks (lower is faster)
iparam1 = TODO: 3OE1:802B4970; looks like damage threshold; must be >0, default 200; NNF: Docile Mode HP Threshold
iparam2 = TODO: 3OE1:802B4A50; looks like damage threshold; must be >0, default 200; NNF: Sky/Floor Sword HP to trigger
iparam3 = TODO: 3OE1:802B49C0; must be >0, default 200; NNF: Sky/Floor Sword HP to cancel
iparam4 = TODO: 3OE1:802B4924; must be >0, default 200; NNF: Gravity Trap Attack HP Threshold
iparam5 = TODO: 3OE1:802B694C; seems to not be read - missing label?; must be >0, default 90; NNF: Shot charge-up duration (lower is shorter)
iparam6 = TODO: 3OE1:TBoss6Type1_FUN_802B1CA8; must be >0, default 180; NNF: Movement speed and duration during charge-up shot (lower is faster/shorter)
MOVEMENT DATA 2C (OLGA_FLOW_2)
fparam1 = TODO: 3OE1:80354FBC; NNF: Olga Flow 2 sword damage (lower is less)
fparam2 = TODO: 3OE1:802BB218; must be >0, default is 20; NNF: Foot damage
fparam5 = TODO: 3OE1:802BB218; must be >0, default is 20; NNF: Wrong attribute damage dealt during soul steal (physical - lower is less)
fparam3 = TODO: 3OE1:80354FEC, 3OE1:8035BF80; NNF: Olga Flow 2 Divine Punishment damage (lower is less)
fparam4 = rock damage; must be > 0; default 20
fparam6 = TODO: 3OE1:802BB218; must be >0, default is 60; NNF: Rock fall duration during soul steal (lower is less)
iparam1 = TODO: 3OE1:802BB218; must be >0, default is 200; NNF: Amount of damage to trigger Divine Punishment
iparam2 = TODO: 3OE1:802BB218; must be >0, default is 200; NNF: Amount of damage it takes to go into soul steal state
iparam3 = TODO: 3OE1:802BB218; must be >0, default is 200; NNF: Amount of damage to knock Olga Flow out of soul steal state
iparam4 = TODO: 3OE1:802BB218; must be >0, default is 60; NNF: Delay between attacks (lower is faster)
iparam5 = TODO: 3OE1:802BB218; must be in range [0, 100] with iparam5 + iparam6 <= 100, default is 25, used as a probability along with iparam6; NNF: Form 1's Total HP% Trigger to halve Attack delays
iparam6 = TODO: 3OE1:802BB218; must be in range [0, 100] with iparam5 + iparam6 <= 100, default is 10, used as a probability along with iparam5
MOVEMENT DATA 2D (OLGA_FLOW_1, OLGA_FLOW_2)
fparam1 = OLGA_FLOW_1; TODO: 3OE1:80323128; TBoss6Mine; default 20; NNF: Trap damage Form 1
fparam2 = OLGA_FLOW_2; TODO: 3OE1:8036773C; TBoss6MagMine; must be >0, default 20; NNF: Trap damage Form 2
fparam3 = OLGA_FLOW_2; TODO: 3OE1:8036778C; TBoss6MagMine; must be in range [0, 100], default 0
MOVEMENT DATA 2E (OLGA_FLOW_2)
fparam1 = TODO: 3OE1:8032EE24; TBoss6Mag; NNF: Amount of time Gael/Giel stays dead (lower is shorter)
fparam2 = TODO: 3OE1:8032EE74; TBoss6Mag; NNF: Gael/Giel Chase speed during Divine Punishment
fparam3 = TODO: 3OE1:802BB218; must be >0, default 1; used instead of movement data 2F fparam1 if a certain flag is set; NNF: Olga Flow's normal movement speed after some threshold
fparam4 = TODO: 3OE1:802BB218; must be >0, default 1; used instead of movement data 2F fparam2 if a certain flag is set; NNF: Olga Flow's movement speed during soul steal after some threshold
MOVEMENT DATA 2F (OLGA_FLOW_1, OLGA_FLOW_2)
fparam1 = OLGA_FLOW_2; TODO: 3OE1:802BB218; must be >0, default 1
fparam2 = OLGA_FLOW_2; TODO: 3OE1:802BB218; must be >0, default 1
fparam3 = OLGA_FLOW_2; TODO: 3OE1:802BB218, 3OE1:8035BFB0; must be >0, default 1; damage reduction for movement data 2C fparam2?, only applies if a certain flag is set
fparam4 = OLGA_FLOW_2; TODO: 3OE1:802BB218; must be >0, default 120; looks like duration for something
fparam5 = OLGA_FLOW_1; TODO: 3OE1:80320FB4; also related to shot/ball attack
fparam6 = OLGA_FLOW_1; TODO: 3OE1:802B694C; must be >0, default 7; same as movement data 2B fparam6 but used when a certain flag is enabled
MOVEMENT DATA 30 (POFUILLY_SLIME)
MOVEMENT DATA 34 (POUILLY_SLIME)
fparam1 = spit attack damage * 5 (so e.g. 1000 here means 200 damange)
MOVEMENT DATA 30 (DELDEPTH)
fparam1 = TODO: 3OE1:80312E04; NNF: Movement speed (Disk form).
fparam2 = TODO: 3OE1:80312E1C; NNF: Distance travelled per movement (Disk form).
iparam1 = attack tech level (Megid in Ultimate; Barta in other difficulties); also bomb power? (TODO: 3OE1:80312490)
iparam2 = TODO: 3OE1:80312AE0; NNF: Rotation speed (Unfolded form) - lower is slower.
MOVEMENT DATA 31 (PAN_ARMS)
fparam1 = TODO: 3OE1:800DF31C
fparam2 = TODO: 3OE1:800E36DC; NNF: Blue laser damage
fparam3 = TODO: 3OE1:800E36DC; NNF: Red laser damage
iparam1 = TODO: 3OE1:800DF32C; value is max(iparam1, 5); NNF: spawn radius
iparam2 = TODO: 3OE1:800DF350; value is max(iparam2, 0); NNF: spawn speed in frames
MOVEMENT DATA 32 (HIDOOM)
MOVEMENT DATA 33 (MIGIUM)
fparam1 = TODO: 3OE1:800E2640
fparam2 = TODO: 3OE1:800E2650; NNF: stab damage
fparam3 = MIGIUM; TODO: 3OE1:800E26AC
iparam1 = MIGIUM; Resta level, must be in range [0, 14]; NNF: Jellen level
iparam2 = MIGIUM; Jellen level, must be in range [0, 14]; NNF: Zalure level
iparam3 = MIGIUM; Zalure level, must be in range [0, 14]; NNF: Resta level
MOVEMENT DATA 35 (DARVANT)
fparam1 = TODO: must be in range [0.33333334, 5.833333]; 3OE1:8005D5E0; NNF: Attack speed
iparam1 = number of Darvants that must be killed before phase ends (actual value is player count * iparam1); must be in range [1, 19]
MOVEMENT DATA 36 (DARK_FALZ_1)
fparam1 = NNF: movement speed; must be in range [1, 60]; used as reciprocal (see 3OE1:80052F60) so lower is faster
iparam1 = Rafoie level, expected to be in range [0, 14]
iparam2 = Rabarta level, expected to be in range [0, 14]
iparam3 = TODO: 3OE1:FUN_80054DE0; NNF: Dark Falz 1 Divine Punishment strength (Also based on MST)
MOVEMENT DATA 37 (DARK_FALZ_2)
fparam1 = TODO: 3OE1:80057BE4; value used is clamp(floor(fparam1), 1, 25) * 75 - 7; appears angle-related; 3OE1:8005653C; NNF: Movement speed (backwards, lower is faster).
iparam1 = TODO: must be in range [1, 4], chooses between 4 different actions in a certain situation, named MD_STOP1 through MD_STOP4; 3OE1:80056358
iparam2 = TODO: Resta level; 3OE1:80056994
MOVEMENT DATA 38 (DARK_FALZ_3)
fparam1 = TODO: 3OE1:80050CA4
iparam1 = Grants level
iparam2 = Megid level
iparam3 = number of pairs of homing attacks (TObjDFHorming) to launch at once; must be in range [1, 8]
iparam4 = TODO: 3OE1:8005B14C; NNF: HP threshold to soul steal
iparam5 = TODO: 3OE1:80050C94; NNF: Ball attack damage. (with 3000/10 MST, does 700 Damage)
MOVEMENT DATA 39 (DARVANT, DARK_FALZ_1)
fparam1 = DARVANT; TODO: must be in range [0.33333334, 10.208332]; 3OE1:8005D618; NNF: Attack speed
iparam1 = DARK_FALZ_1; TODO: number of Darvants to spawn at a time?; clamped to [1, 6]; 3OE1:80054CF0
MOVEMENT DATA 3A (MERICAROL)
MOVEMENT DATA 45 (MERIKLE)
MOVEMENT DATA 46 (MERICUS)
fparam1 = TODO: 3OE1:802CE110; NNF: rush damage
fparam2 = poison cloud damage
fparam3 = TODO: 3OE1:802CEAA8; NNF: Spit 'attack capability'. Set to 1, attack does nothing and does not register as a hit.
fparam4 = TODO: 3OE1:802CEAB0; NNF: Projectile speed; also affects the cooldown time between each shot.
fparam5 = poison cloud radius
fparam6 = TODO: 3OE1:802CD890; probability in range [0, 1]; NNF: Level of 'Megidness'. Value of 1 treats the attack as megid, despite fparam3.
iparam3 = TODO: 3OE1:802CED14; NNF: Projectile fire rate.
iparam4 = TODO: 3OE1:802CD7FC; NNF: Charge up time for poison cloud attack.
iparam5 = TODO: 3OE1:802CE850; NNF: Melee attack cooldown time.
iparam6 = TODO: 3OE1:802CEA30
MOVEMENT DATA 3B (UL_GIBBON)
MOVEMENT DATA 3C (ZOL_GIBBON)
(loaded with assets but not used)
MOVEMENT DATA 3D (GIBBLES)
fparam2 = TODO: 3OE1:802D7F5C; NNF: Triple-punch attack cooldown.
fparam3 = TODO: 3OE1:802D8BC0; NNF: Movement speed.
fparam4 = TODO: 3OE1:802D7490; NNF: Jump cooldown time (Higher value = less waiting time).
iparam1 = TODO: 3OE1:802D7484
MOVEMENT DATA 40 (MORFOS)
fparam1 = laser speed; hitbox radius is fparam1 * 1.5
fparam2 = laser damage
iparam1 = TODO: 3OE1:80332298, 3OE1:803321C4; NNF: Firing rate of regular laser attack. Laser attack when aggressive (charging) is unaffected.
iparam2 = TODO: 3OE1:8033161C, 3OE1:8033192C, 3OE1:80331B4C, 3OE1:80331D00, 3OE1:80331FA0; NNF: Speed at which Morphos spins after firing laser.
iparam3 = TODO: 3OE1:80331F04; NNF: Interval in frames of attacks
iparam4 = TODO: 3OE1:803318EC; NNF: Charge frames before attacking without hitstun.
iparam5 = TODO: 3OE1:803318CC; NNF: Affects charge laser tracking. Too high and doesnt lock-on. Need Research
MOVEMENT DATA 41 (RECOBOX)
(loaded with assets but not used)
MOVEMENT DATA 42 (RECON)
fparam1 = TODO: 3OE1:8031C31C; NNF: Chase speed for buzzsaw attack
fparam2 = bomb explosion radius
fparam3 = bomb damage
fparam4 = TODO: 3OE1:8031A144; NNF: bomb throw distance
iparam1 = TODO: 3OE1:80319DCC; bomb frames until explosion?; NNF: Speed recon comes out of the recobox. As it always takes the same amount of 'time' to come out, higher values make it go high up as well as fast.
iparam2 = TODO: 3OE1:8031B68C; NNF: Frame delay from when Recon gets in position to when it activates buzzsaw.
MOVEMENT DATA 43 (SINOW_ZOA)
MOVEMENT DATA 44 (SINOW_ZELE)
fparam1 = TODO: 3OE1:80317B7C; NNF: Movement speed
fparam3 = TODO: 3OE1:803173F4; NNF: Speed at which Sinow Zoa/Zele reappears after warping.
fparam4 = TODO: 3OE1:80319960; NNF: Attack speed
fparam5 = TODO: 3OE1:80319968
fparam6 = TODO: 3OE1:80316F84
iparam1 = Resta/Shifta/Deband/Jellen/Zalure level
iparam2 = TODO: 3OE1:80316BE8
iparam3 = TODO: 3OE1:80317458; NNF: Cooldown time for all attacks.
iparam4 = attack tech level (Rabarta in Ultimate, Gibarta otherwise)
MOVEMENT DATA 48 (HILDEBEAR)
MOVEMENT DATA 49 (HILDEBLUE)
fparam1 = punch attack speed
fparam2 = TODO: 3OE1:800ADBE0; NNF: tech range
fparam3 = movement speed (does not affect animation speed)
fparam4 = walking animation speed
MOVEMENT DATA 4D (GRASS_ASSASSIN)
(loaded with assets but not used)
MOVEMENT DATA 51 (DELSABER)
fparam1 = TODO: 3OE1:800A5454
fparam2 = TODO: 3OE1:800A5708
fparam3 = TODO: 3OE1:800A5CA4
fparam4 = TODO: 3OE1:800A5D04
+1 -1
View File
@@ -557,7 +557,7 @@ BugFixes
8000C6E8 807F0024 8000C6E8 807F0024 8000C6E8 807F0024 8000C6E8 807F0024 8000C6E8 807F0024 8000C6E8 807F0024 8000C6E8 807F0024 8000C6E8 807F0024 lwz r3, [r31 + 0x0024]
8000C6EC 48165AA0 8000C6EC 482147D4 8000C6EC 482156C0 8000C6EC 48215474 8000C6EC 482146F4 8000C6EC 482146F4 8000C6EC 482157A8 8000C6EC 48215040 b +0x002146F4 /* 80220DE0 */
8021D098 4BDEF638 8021D9FC 4BDEECD4 8021E8E8 4BDEDDE8 8021E69C 4BDEE034 8021D91C 4BDEEDB4 8021D91C 4BDEEDB4 8021E9D0 4BDEDD00 8021E268 4BDEE468 b -0x0021124C /* 8000C6D0 */
80172188 4BE9A558 80220EBC 4BDEB824 80221DA8 4BDEA938 80221B5C 4BDEAB84 80220DDC 4BDEB904 80220DDC 4BDEB904 80221E90 4BDEA850 80221728 4BDEAFB8 b -0x002146FC /* 8000C6E0 */
80220528 4BE9A558 80220EBC 4BDEB824 80221DA8 4BDEA938 80221B5C 4BDEAB84 80220DDC 4BDEB904 80220DDC 4BDEB904 80221E90 4BDEA850 80221728 4BDEAFB8 b -0x002146FC /* 8000C6E0 */
Dropped Mag Colour Bug Fix
BugFixes
Binary file not shown.
Binary file not shown.
View File
View File
View File
Binary file not shown.
Binary file not shown.
+1 -2
View File
@@ -17,7 +17,7 @@
0019 = P2 Scientist after defeating dragon
001E = Entered Caves 1 (Gov 2-1)
001F = Entered De Rol Le in 2-4
0020 = De Ro lee defeated
0020 = De Rol Le defeated
0021 = Mines unlocked (P2 Tyrell after defeating De Rol Le)
0028 = Entered Mines 1
0029 = Entered Vol Opt Area
@@ -105,7 +105,6 @@
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
00FB = Received OROCHI-AGITO!
00FD = Unknown addicting food
0105 = Central dome fire swirl. Got Glory of the past!
0106 = Central dome fire swirl. Got Mark3.
+2 -2
View File
@@ -67,8 +67,8 @@ string AFSArchive::generate_t(const vector<string>& files) {
w.put_u32b(0x41465300); // 'AFS\0'
w.put<U32T<BE>>(files.size());
// It seems entries are aligned to 0x800-byte boundaries, and the file's
// header is always 0x80000 (!) bytes, most of which is unused
// It seems entries are aligned to 0x800-byte boundaries, and the file's header is always 0x80000 (!) bytes, most of
// which is unused
uint32_t data_offset = 0x80000;
for (const auto& file : files) {
w.put<U32T<BE>>(data_offset);
+4
View File
@@ -23,6 +23,10 @@ public:
return this->entries;
}
inline size_t num_entries() const {
return this->entries.size();
}
std::pair<const void*, size_t> get(size_t index) const;
std::string get_copy(size_t index) const;
phosg::StringReader get_reader(size_t index) const;
+57 -79
View File
@@ -2,6 +2,7 @@
#include <stdio.h>
#include <string.h>
#include <filesystem>
#include <phosg/Filesystem.hh>
#include <phosg/Hash.hh>
#include <phosg/Random.hh>
@@ -31,10 +32,7 @@ shared_ptr<DCNTELicense> DCNTELicense::from_json(const phosg::JSON& json) {
}
phosg::JSON DCNTELicense::json() const {
return phosg::JSON::dict({
{"SerialNumber", this->serial_number},
{"AccessKey", this->access_key},
});
return phosg::JSON::dict({{"SerialNumber", this->serial_number}, {"AccessKey", this->access_key}});
}
shared_ptr<V1V2License> V1V2License::from_json(const phosg::JSON& json) {
@@ -51,10 +49,7 @@ shared_ptr<V1V2License> V1V2License::from_json(const phosg::JSON& json) {
}
phosg::JSON V1V2License::json() const {
return phosg::JSON::dict({
{"SerialNumber", this->serial_number},
{"AccessKey", this->access_key},
});
return phosg::JSON::dict({{"SerialNumber", this->serial_number}, {"AccessKey", this->access_key}});
}
shared_ptr<GCLicense> GCLicense::from_json(const phosg::JSON& json) {
@@ -100,11 +95,7 @@ shared_ptr<XBLicense> XBLicense::from_json(const phosg::JSON& json) {
}
phosg::JSON XBLicense::json() const {
return phosg::JSON::dict({
{"GamerTag", this->gamertag},
{"UserID", this->user_id},
{"AccountID", this->account_id},
});
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) {
@@ -127,10 +118,7 @@ shared_ptr<BBLicense> BBLicense::from_json(const phosg::JSON& json) {
}
phosg::JSON BBLicense::json() const {
return phosg::JSON::dict({
{"UserName", this->username},
{"Password", this->password},
});
return phosg::JSON::dict({{"UserName", this->username}, {"Password", this->password}});
}
Account::Account(const phosg::JSON& json)
@@ -292,7 +280,7 @@ phosg::JSON Account::json() const {
}
string Account::str() const {
std::string ret = phosg::string_printf("Account: %010" PRIu32 "/%08" PRIX32 "\n", this->account_id, this->account_id);
std::string ret = std::format("Account: {:010}/{:08X}\n", this->account_id, this->account_id);
if (this->flags) {
string flags_str = "";
@@ -328,6 +316,9 @@ string Account::str() const {
flags_str += "CHEAT_ANYWHERE,";
}
if (this->check_flag(Flag::DISABLE_QUEST_REQUIREMENTS)) {
flags_str += "DISABLE_QUEST_REQUIREMENTS,";
}
if (this->check_flag(Flag::ALWAYS_ENABLE_CHAT_COMMANDS)) {
flags_str += "ALWAYS_ENABLE_CHAT_COMMANDS,";
}
if (this->check_flag(Flag::IS_SHARED_ACCOUNT)) {
@@ -336,10 +327,10 @@ string Account::str() const {
}
if (flags_str.empty()) {
flags_str = "none";
} else if (phosg::ends_with(flags_str, ",")) {
} else if (flags_str.ends_with(",")) {
flags_str.pop_back();
}
ret += phosg::string_printf(" Flags: %08" PRIX32 " (%s)\n", this->flags, flags_str.c_str());
ret += std::format(" Flags: {:08X} ({})\n", this->flags, flags_str);
}
if (this->user_flags) {
@@ -349,56 +340,56 @@ string Account::str() const {
}
if (user_flags_str.empty()) {
user_flags_str = "none";
} else if (phosg::ends_with(user_flags_str, ",")) {
} else if (user_flags_str.ends_with(",")) {
user_flags_str.pop_back();
}
ret += phosg::string_printf(" User flags: %08" PRIX32 " (%s)\n", this->user_flags, user_flags_str.c_str());
ret += std::format(" User flags: {:08X} ({})\n", this->user_flags, user_flags_str);
}
if (this->ban_end_time) {
string time_str = phosg::format_time(this->ban_end_time);
ret += phosg::string_printf(" Banned until: %" PRIu64 " (%s)\n", this->ban_end_time, time_str.c_str());
ret += std::format(" Banned until: {} ({})\n", this->ban_end_time, time_str);
}
if (this->ep3_current_meseta || this->ep3_total_meseta_earned) {
ret += phosg::string_printf(" Episode 3 meseta: %" PRIu32 " (total earned: %" PRIu32 ")\n",
ret += std::format(" Episode 3 meseta: {} (total earned: {})\n",
this->ep3_current_meseta, this->ep3_total_meseta_earned);
}
if (!this->last_player_name.empty()) {
ret += phosg::string_printf(" Last player name: \"%s\"\n", this->last_player_name.c_str());
ret += std::format(" Last player name: \"{}\"\n", this->last_player_name);
}
if (!this->auto_reply_message.empty()) {
ret += phosg::string_printf(" Auto reply message: \"%s\"\n", this->auto_reply_message.c_str());
ret += std::format(" Auto reply message: \"{}\"\n", this->auto_reply_message);
}
if (this->bb_team_id) {
ret += phosg::string_printf(" BB team ID: %08" PRIX32 "\n", this->bb_team_id);
ret += std::format(" BB team ID: {:08X}\n", this->bb_team_id);
}
if (this->is_temporary) {
ret += phosg::string_printf(" Is temporary license: true\n");
ret += std::format(" Is temporary license: true\n");
}
for (const auto& it : this->dc_nte_licenses) {
ret += phosg::string_printf(" DC NTE license: serial_number=%s access_key=%s\n",
it.second->serial_number.c_str(), it.second->access_key.c_str());
ret += std::format(" DC NTE license: serial_number={} access_key={}\n",
it.second->serial_number, it.second->access_key);
}
for (const auto& it : this->dc_licenses) {
ret += phosg::string_printf(" DC license: serial_number=%" PRIX32 " access_key=%s\n",
it.second->serial_number, it.second->access_key.c_str());
ret += std::format(" DC license: serial_number={:X} access_key={}\n",
it.second->serial_number, it.second->access_key);
}
for (const auto& it : this->pc_licenses) {
ret += phosg::string_printf(" PC license: serial_number=%" PRIX32 " access_key=%s\n",
it.second->serial_number, it.second->access_key.c_str());
ret += std::format(" PC license: serial_number={:X} access_key={}\n",
it.second->serial_number, it.second->access_key);
}
for (const auto& it : this->gc_licenses) {
ret += phosg::string_printf(" GC license: serial_number=%010" PRIu32 " access_key=%s password=%s\n",
it.second->serial_number, it.second->access_key.c_str(), it.second->password.c_str());
ret += std::format(" GC license: serial_number={:010} access_key={} password={}\n",
it.second->serial_number, it.second->access_key, it.second->password);
}
for (const auto& it : this->xb_licenses) {
ret += phosg::string_printf(" XB license: gamertag=%s user_id=%016" PRIX64 " account_id=%016" PRIX64 "\n",
it.second->gamertag.c_str(), it.second->user_id, it.second->account_id);
ret += std::format(" XB license: gamertag={} user_id={:016X} account_id={:016X}\n",
it.second->gamertag, it.second->user_id, it.second->account_id);
}
for (const auto& it : this->bb_licenses) {
ret += phosg::string_printf(" BB license: username=%s password=%s\n",
it.second->username.c_str(), it.second->password.c_str());
ret += std::format(" BB license: username={} password={}\n",
it.second->username, it.second->password);
}
phosg::strip_trailing_whitespace(ret);
@@ -408,56 +399,37 @@ string Account::str() const {
void Account::save() const {
if (!this->is_temporary) {
auto json = this->json();
string json_data = json.serialize(phosg::JSON::SerializeOption::FORMAT | phosg::JSON::SerializeOption::HEX_INTEGERS);
string filename = phosg::string_printf("system/licenses/%010" PRIu32 ".json", this->account_id);
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);
phosg::save_file(filename, json_data);
}
}
void Account::delete_file() const {
string filename = phosg::string_printf("system/licenses/%010" PRIu32 ".json", this->account_id);
string filename = std::format("system/licenses/{:010}.json", this->account_id);
remove(filename.c_str());
}
uint64_t Login::proxy_session_id() const {
uint64_t low_part = 0;
if (this->dc_nte_license) {
low_part = this->dc_nte_license->proxy_session_id_part();
} else if (this->dc_license) {
low_part = this->dc_license->proxy_session_id_part();
} else if (this->pc_license) {
low_part = this->pc_license->proxy_session_id_part();
} else if (this->gc_license) {
low_part = this->gc_license->proxy_session_id_part();
} else if (this->xb_license) {
low_part = this->xb_license->proxy_session_id_part();
} else if (this->bb_license) {
low_part = this->bb_license->proxy_session_id_part();
} else {
throw logic_error("none of the licenses in a Login were present");
}
return (static_cast<uint64_t>(this->account->account_id) << 32) | low_part;
}
string Login::str() const {
string ret = phosg::string_printf("Account:%08" PRIX32, this->account->account_id);
string ret = std::format("Account:{:08X}", this->account->account_id);
if (this->account_was_created) {
ret += " (new)";
}
if (this->dc_nte_license) {
ret += phosg::string_printf(" via DC NTE serial number %s", this->dc_nte_license->serial_number.c_str());
ret += std::format(" via DC NTE serial number {}", this->dc_nte_license->serial_number);
} else if (this->dc_license) {
ret += phosg::string_printf(" via DC serial number %08" PRIX32, this->dc_license->serial_number);
ret += std::format(" via DC serial number {:08X}", this->dc_license->serial_number);
} else if (this->pc_license) {
ret += phosg::string_printf(" via PC serial number %08" PRIX32, this->pc_license->serial_number);
ret += std::format(" via PC serial number {:08X}", this->pc_license->serial_number);
} else if (this->gc_license) {
ret += phosg::string_printf(" via GC serial number %010" PRIu32, this->gc_license->serial_number);
ret += std::format(" via GC serial number {:010}", this->gc_license->serial_number);
} else if (this->xb_license) {
ret += phosg::string_printf(" via XB user ID %016" PRIX64, this->xb_license->user_id);
ret += std::format(" via XB user ID {:016X}", this->xb_license->user_id);
} else if (this->bb_license) {
ret += phosg::string_printf(" via BB username %s", this->bb_license->username.c_str());
ret += std::format(" via BB username {}", this->bb_license->username);
} else {
ret += phosg::string_printf(" artificially");
ret += std::format(" artificially");
}
return ret;
}
@@ -672,7 +644,11 @@ shared_ptr<Login> AccountIndex::from_gc_credentials_locked(
}
shared_ptr<Login> AccountIndex::from_gc_credentials(
uint32_t serial_number, const string& access_key, const string* password, const string& character_name, bool allow_create) {
uint32_t serial_number,
const string& access_key,
const string* password,
const string& character_name,
bool allow_create) {
if (serial_number == 0) {
throw no_username();
}
@@ -766,7 +742,8 @@ 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) {
shared_ptr<Login> AccountIndex::from_bb_credentials(
const string& username, const string* password, bool allow_create) {
if (username.empty() || (password && password->empty())) {
throw no_username();
}
@@ -1043,16 +1020,17 @@ shared_ptr<Account> AccountIndex::create_temporary_account_for_shared_account(
AccountIndex::AccountIndex(bool force_all_temporary)
: force_all_temporary(force_all_temporary) {
if (!this->force_all_temporary) {
if (!phosg::isdir("system/licenses")) {
mkdir("system/licenses", 0755);
if (!std::filesystem::is_directory("system/licenses")) {
std::filesystem::create_directories("system/licenses");
} else {
for (const auto& item : phosg::list_directory("system/licenses")) {
if (phosg::ends_with(item, ".json")) {
for (const auto& item : std::filesystem::directory_iterator("system/licenses")) {
string filename = item.path().filename().string();
if (filename.ends_with(".json")) {
try {
phosg::JSON json = phosg::JSON::parse(phosg::load_file("system/licenses/" + item));
phosg::JSON json = phosg::JSON::parse(phosg::load_file("system/licenses/" + filename));
this->add(make_shared<Account>(json));
} catch (const exception& e) {
phosg::log_error("Failed to index account %s", item.c_str());
phosg::log_error_f("Failed to index account {}", filename);
throw;
}
}
+12 -60
View File
@@ -17,10 +17,6 @@ struct DCNTELicense {
std::string serial_number;
std::string access_key;
inline uint64_t proxy_session_id_part() const {
return phosg::fnv1a32(this->serial_number);
}
static std::shared_ptr<DCNTELicense> from_json(const phosg::JSON& json);
phosg::JSON json() const;
};
@@ -29,10 +25,6 @@ struct V1V2License {
uint32_t serial_number = 0;
std::string access_key;
inline uint64_t proxy_session_id_part() const {
return this->serial_number;
}
static std::shared_ptr<V1V2License> from_json(const phosg::JSON& json);
phosg::JSON json() const;
};
@@ -42,10 +34,6 @@ struct GCLicense {
std::string access_key;
std::string password;
inline uint64_t proxy_session_id_part() const {
return this->serial_number;
}
static std::shared_ptr<GCLicense> from_json(const phosg::JSON& json);
phosg::JSON json() const;
};
@@ -55,10 +43,6 @@ struct XBLicense {
uint64_t user_id = 0;
uint64_t account_id = 0;
inline uint64_t proxy_session_id_part() const {
return phosg::fnv1a32(this->gamertag);
}
static std::shared_ptr<XBLicense> from_json(const phosg::JSON& json);
phosg::JSON json() const;
};
@@ -67,10 +51,6 @@ struct BBLicense {
std::string username;
std::string password;
inline uint64_t proxy_session_id_part() const {
return phosg::fnv1a32(this->username);
}
static std::shared_ptr<BBLicense> from_json(const phosg::JSON& json);
phosg::JSON json() const;
};
@@ -92,8 +72,7 @@ struct Account {
ADMINISTRATOR = 0x000000FF,
ROOT = 0x7FFFFFFF,
IS_SHARED_ACCOUNT = 0x80000000,
// NOTE: When adding or changing license flags, don't forget to change the
// documentation in the shell's help text.
// NOTE: When adding or changing license flags, don't forget to change the documentation in the shell's help text.
UNUSED_BITS = 0x70FFFF00,
// clang-format on
};
@@ -169,8 +148,7 @@ struct Login {
bool account_was_created = false;
// This field will never be null
std::shared_ptr<Account> account;
// Exactly one of the following will be non-null, representing the license
// that the client logged in with
// Exactly one of the following will be non-null, representing the license that the client logged in with
std::shared_ptr<DCNTELicense> dc_nte_license;
std::shared_ptr<V1V2License> dc_license;
std::shared_ptr<V1V2License> pc_license;
@@ -178,8 +156,6 @@ struct Login {
std::shared_ptr<XBLicense> xb_license;
std::shared_ptr<BBLicense> bb_license;
uint64_t proxy_session_id() const;
std::string str() const;
};
@@ -232,22 +208,12 @@ public:
std::shared_ptr<Account> from_account_id(uint32_t account_id) const;
std::shared_ptr<Login> from_dc_nte_credentials(
const std::string& serial_number,
const std::string& access_key,
bool allow_create);
const std::string& serial_number, const std::string& access_key, bool allow_create);
std::shared_ptr<Login> from_dc_credentials(
uint32_t serial_number,
const std::string& access_key,
const std::string& character_name,
bool allow_create);
std::shared_ptr<Login> from_pc_nte_credentials(
uint32_t guild_card_number,
bool allow_create);
uint32_t serial_number, const std::string& access_key, const std::string& character_name, bool allow_create);
std::shared_ptr<Login> from_pc_nte_credentials(uint32_t guild_card_number, bool allow_create);
std::shared_ptr<Login> from_pc_credentials(
uint32_t serial_number,
const std::string& access_key,
const std::string& character_name,
bool allow_create);
uint32_t serial_number, const std::string& access_key, const std::string& character_name, bool allow_create);
std::shared_ptr<Login> from_gc_credentials(
uint32_t serial_number,
const std::string& access_key,
@@ -255,14 +221,9 @@ public:
const std::string& character_name,
bool allow_create);
std::shared_ptr<Login> from_xb_credentials(
const std::string& gamertag,
uint64_t user_id,
uint64_t account_id,
bool allow_create);
const std::string& gamertag, uint64_t user_id, uint64_t account_id, bool allow_create);
std::shared_ptr<Login> from_bb_credentials(
const std::string& username,
const std::string* password,
bool allow_create);
const std::string& username, const std::string* password, bool allow_create);
std::shared_ptr<Account> create_temporary_account_for_shared_account(
std::shared_ptr<const Account> src_a, const std::string& variation_data) const;
@@ -270,8 +231,6 @@ public:
protected:
bool force_all_temporary;
// This class must be thread-safe because it's used by both the patch server
// and game server threads
mutable std::shared_mutex lock;
std::unordered_map<uint32_t, std::shared_ptr<Account>> by_account_id;
std::unordered_map<std::string, std::shared_ptr<Account>> by_dc_nte_serial_number;
@@ -284,23 +243,16 @@ protected:
void add_locked(std::shared_ptr<Account> a);
std::shared_ptr<Login> from_dc_nte_credentials_locked(
const std::string& serial_number,
const std::string& access_key);
const std::string& serial_number, const std::string& access_key);
std::shared_ptr<Login> from_dc_credentials_locked(
uint32_t serial_number,
const std::string& access_key,
const std::string& character_name);
uint32_t serial_number, const std::string& access_key, const std::string& character_name);
std::shared_ptr<Login> from_pc_credentials_locked(
uint32_t serial_number,
const std::string& access_key,
const std::string& character_name);
uint32_t serial_number, const std::string& access_key, const std::string& character_name);
std::shared_ptr<Login> from_gc_credentials_locked(
uint32_t serial_number,
const std::string& access_key,
const std::string* password,
const std::string& character_name);
std::shared_ptr<Login> from_xb_credentials_locked(uint64_t user_id);
std::shared_ptr<Login> from_bb_credentials_locked(
const std::string& username,
const std::string* password);
std::shared_ptr<Login> from_bb_credentials_locked(const std::string& username, const std::string* password);
};
-24
View File
@@ -1,24 +0,0 @@
#pragma once
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
struct DiffEntry {
uint32_t address;
std::string a_data;
std::string b_data;
};
inline void run_address_translator(const std::string&, const std::string&, const std::string&) {
throw std::runtime_error("resource_file is not available; install it and rebuild newserv");
}
inline std::vector<DiffEntry> diff_dol_files(const std::string&, const std::string&) {
throw std::runtime_error("resource_file is not available; install it and rebuild newserv");
}
inline std::vector<DiffEntry> diff_xbe_files(const std::string&, const std::string&) {
throw std::runtime_error("resource_file is not available; install it and rebuild newserv");
}
+360 -47
View File
@@ -1,13 +1,19 @@
#include "AddressTranslator.hh"
#include <array>
#include <filesystem>
#include <future>
#include <phosg/Filesystem.hh>
#include <phosg/Strings.hh>
#include <resource_file/Emulators/X86Emulator.hh>
#include <resource_file/ExecutableFormats/DOLFile.hh>
#include <resource_file/ExecutableFormats/PEFile.hh>
#include <resource_file/ExecutableFormats/XBEFile.hh>
#include "Map.hh"
#include "Text.hh"
#include "Types.hh"
using namespace std;
class AddressTranslator {
@@ -107,44 +113,44 @@ public:
AddressTranslator(const string& directory)
: log("[addr-trans] "),
directory(directory),
enable_ppc(false) {
while (phosg::ends_with(this->directory, "/")) {
directory(directory) {
while (this->directory.ends_with("/")) {
this->directory.pop_back();
}
for (const auto& filename : phosg::list_directory(this->directory)) {
for (const auto& item : std::filesystem::directory_iterator(this->directory)) {
string filename = item.path().filename().string();
if (filename.size() < 4) {
continue;
}
string name = filename.substr(0, filename.size() - 4);
string path = directory + "/" + filename;
if (phosg::ends_with(filename, ".dol")) {
if (filename.ends_with(".dol")) {
ResourceDASM::DOLFile dol(path.c_str());
auto mem = make_shared<ResourceDASM::MemoryContext>();
dol.load_into(mem);
this->mems.emplace(name, mem);
this->enable_ppc = true;
this->log.info("Loaded %s", name.c_str());
} else if (phosg::ends_with(filename, ".xbe")) {
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>();
xbe.load_into(mem);
this->mems.emplace(name, mem);
this->log.info("Loaded %s", name.c_str());
} else if (phosg::ends_with(filename, ".exe")) {
this->log.info_f("Loaded {}", name);
} else if (filename.ends_with(".exe")) {
ResourceDASM::PEFile pe(path.c_str());
auto mem = make_shared<ResourceDASM::MemoryContext>();
pe.load_into(mem);
this->mems.emplace(name, mem);
this->log.info("Loaded %s", name.c_str());
} else if (phosg::ends_with(filename, ".bin")) {
this->log.info_f("Loaded {}", name);
} else if (filename.ends_with(".bin")) {
string data = phosg::load_file(path);
auto mem = make_shared<ResourceDASM::MemoryContext>();
mem->allocate_at(0x8C010000, data.size());
mem->memcpy(0x8C010000, data.data(), data.size());
this->mems.emplace(name, mem);
this->log.info("Loaded %s", name.c_str());
this->log.info_f("Loaded {}", name);
}
}
}
@@ -198,18 +204,267 @@ public:
}
}
if (r2_low_found && r2_high_found) {
fprintf(stderr, "(%s) r2 = %08" PRIX32 "\n", it.first.c_str(), r2);
phosg::fwrite_fmt(stderr, "({}) r2 = {:08X}\n", it.first, r2);
} else {
fprintf(stderr, "(%s) r2 = __MISSING__\n", it.first.c_str());
phosg::fwrite_fmt(stderr, "({}) r2 = __MISSING__\n", it.first);
}
if (r13_low_found && r13_high_found) {
fprintf(stderr, "(%s) r13 = %08" PRIX32 "\n", it.first.c_str(), r13);
phosg::fwrite_fmt(stderr, "({}) r13 = {:08X}\n", it.first, r13);
} else {
fprintf(stderr, "(%s) r13 = __MISSING__\n", it.first.c_str());
phosg::fwrite_fmt(stderr, "({}) r13 = __MISSING__\n", it.first);
}
}
}
struct ParseDATConstructorTableSpec {
string src_name;
uint32_t index_addr;
size_t num_areas;
bool has_names;
vector<uint32_t> x86_constructor_calls;
ParseDATConstructorTableSpec(const phosg::JSON& json) {
this->src_name = json.at("SourceName").as_string();
this->index_addr = json.at("IndexAddress").as_int();
this->num_areas = json.at("AreaCount").as_int();
this->has_names = json.at("HasNames").as_bool();
for (const auto& z : json.at("X86ConstructorCalls").as_list()) {
this->x86_constructor_calls.emplace_back(z->as_int());
}
}
static vector<ParseDATConstructorTableSpec> from_json_list(const phosg::JSON& json) {
vector<ParseDATConstructorTableSpec> ret;
for (const auto& z : json.as_list()) {
ret.emplace_back(*z);
}
return ret;
}
};
template <bool BE>
struct DATConstructorTableEntry {
static constexpr bool IsBE = BE;
U16T<BE> type;
U16T<BE> unused;
U32T<BE> constructor_addr;
F32T<BE> max_dist2; // Only applies for objects
U32T<BE> default_num_children;
} __attribute__((packed));
template <bool BE>
struct DATConstructorTableEntryWithName {
static constexpr bool IsBE = BE;
pstring<TextEncoding::ASCII, 0x10> debug_name;
U16T<BE> type;
U16T<BE> unused;
U32T<BE> constructor_addr;
F32T<BE> max_dist2; // Only applies for objects
U32T<BE> default_num_children;
} __attribute__((packed));
// 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) {
if (!mem) {
throw 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;
if (!spec.x86_constructor_calls.empty()) {
auto constructed_mem = 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>>();
// debugger->bind(emu);
// debugger->state.mode = ResourceDASM::DebuggerMode::TRACE;
auto& regs = emu.registers();
regs.eip = constructor_addr;
regs.esp().u = esp - 4;
constructed_mem->write_u32l(esp - 4, 0xFFFFFFFF); // Return addr
try {
emu.execute();
} catch (const out_of_range&) {
if (regs.eip != 0xFFFFFFFF) {
throw;
}
}
}
effective_mem = constructed_mem;
}
map<uint32_t, map<uint32_t, vector<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++) {
uint32_t entries_addr = EntryT::IsBE ? index_r.get_u32b() : index_r.get_u32l();
if (!entries_addr) {
continue;
}
auto entries_r = effective_mem->reader(entries_addr, 0x4000); // 0x4000 is probably enough
while (!entries_r.eof()) {
const auto& entry = entries_r.get<EntryT>();
if (entry.type == 0xFFFF) {
break;
}
auto& group = table[entry.type][entry.constructor_addr];
if (!group.empty() && (group.back().second == (area - 1))) {
group.back().second = area;
} else {
group.emplace_back(make_pair(area, area));
}
}
if (entries_r.eof()) {
throw 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) {
uint64_t ret = 0;
for (const auto& [start, end] : ranges) {
for (size_t z = start; z <= end; z++) {
ret |= static_cast<uint64_t>(1ULL << z);
}
}
return ret;
}
void parse_dat_constructor_table(const ParseDATConstructorTableSpec& spec) {
map<uint32_t, map<uint32_t, vector<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);
} else if (!spec.has_names) {
table = this->parse_dat_constructor_table_t<DATConstructorTableEntry<false>>(spec_mem, spec);
} else {
table = this->parse_dat_constructor_table_t<DATConstructorTableEntryWithName<false>>(spec_mem, spec);
}
for (const auto& [type, constructor_to_area_ranges] : table) {
phosg::fwrite_fmt(stdout, "{:04X} =>", type);
for (const auto& [constructor, area_ranges] : constructor_to_area_ranges) {
phosg::fwrite_fmt(stdout, " {:08X}", constructor);
bool is_first = true;
for (const auto& [start, end] : area_ranges) {
fputc(is_first ? ':' : ',', stdout);
if (start == end) {
phosg::fwrite_fmt(stdout, "{:02X}", start);
} else {
phosg::fwrite_fmt(stdout, "{:02X}-{:02X}", start, end);
}
is_first = false;
}
}
fputc('\n', stdout);
}
}
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;
for (const auto& spec : specs) {
map<uint32_t, map<uint32_t, vector<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);
} else if (!spec.has_names) {
table = this->parse_dat_constructor_table_t<DATConstructorTableEntry<false>>(spec_mem, spec);
} else {
table = this->parse_dat_constructor_table_t<DATConstructorTableEntryWithName<false>>(spec_mem, spec);
}
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;
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;
for (const auto& [constructor, area_ranges] : constructor_to_area_ranges) {
if (!cell_data.empty()) {
cell_data.push_back(' ');
}
cell_data += std::format("{:08X}", constructor);
if (print_area_masks) {
cell_data += std::format(":{:016X}", this->area_mask_for_ranges(area_ranges));
} else {
bool is_first = true;
for (const auto& [start, end] : area_ranges) {
cell_data.push_back(is_first ? ':' : ',');
if (start == end) {
cell_data += std::format("{:02X}", start);
} else {
cell_data += std::format("{:02X}-{:02X}", start, end);
}
is_first = false;
}
}
}
max_width = 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 =>";
for (const auto& spec : specs) {
size_t width = version_widths.at(spec.src_name);
header_line.push_back(' ');
header_line += spec.src_name;
if (width > spec.src_name.size()) {
header_line.resize(header_line.size() + (width - spec.src_name.size()), '-');
}
}
header_line += " NAME";
for (const auto& [type, formatted_cells] : formatted_cells_for_type) {
string line = std::format("{:04X} =>", type);
for (const auto& spec : specs) {
size_t width = version_widths.at(spec.src_name);
try {
const auto& cell_data = formatted_cells.at(spec.src_name);
line.push_back(' ');
line += cell_data;
if (width > cell_data.size()) {
line.resize(line.size() + (width - cell_data.size()), ' ');
}
} catch (const out_of_range&) {
line.resize(line.size() + (width + 1), ' ');
}
}
line.push_back(' ');
line += is_enemies ? MapFile::name_for_enemy_type(type) : MapFile::name_for_object_type(type);
if ((formatted_lines.size() % 40) == 0) {
formatted_lines.emplace_back(header_line);
}
formatted_lines.emplace_back(std::move(line));
}
for (auto& line : formatted_lines) {
phosg::strip_trailing_whitespace(line);
phosg::fwrite_fmt(stdout, "{}\n", line);
}
}
uint32_t find_match(
shared_ptr<const ResourceDASM::MemoryContext> dest_mem,
uint32_t src_addr,
@@ -241,7 +496,7 @@ public:
size_t src_offset = src_addr - src_section.first;
size_t src_bytes_available_before = src_offset;
size_t src_bytes_available_after = src_section.second - src_offset - 4;
this->log.info("(find_match/%s) Source offset = %08zX with %zX/%zX bytes available before/after",
this->log.info_f("(find_match/{}) Source offset = {:08X} with {:X}/{:X} bytes available before/after",
method_token, src_offset, src_bytes_available_before, src_bytes_available_after);
size_t match_bytes_before = 0;
@@ -311,7 +566,7 @@ public:
}
}
}
this->log.info("(find_match/%s) For match length %zX, %zu matches found", method_token, match_length, num_matches);
this->log.info_f("(find_match/{}) For match length {:X}, {} matches found", method_token, match_length, num_matches);
if (num_matches == 1) {
return last_match_address;
} else if (num_matches == 0) {
@@ -374,7 +629,13 @@ public:
throw runtime_error("scan field too long; too many matches");
}
void find_all_matches(uint32_t src_addr, uint32_t src_size) const {
enum class MatchType {
ANY = 0,
TEXT,
DATA,
};
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");
}
@@ -382,7 +643,7 @@ public:
map<string, uint32_t> results;
for (const auto& it : this->mems) {
if (it.second == this->src_mem) {
log.info("(%s) %08" PRIX32 " (from source)", it.first.c_str(), src_addr);
log.info_f("({}) {:08X} (from source)", it.first, src_addr);
results.emplace(it.first, src_addr);
} else {
@@ -399,46 +660,73 @@ public:
ExpandMethod::PPC_DATA_BACKWARD,
ExpandMethod::PPC_DATA_BOTH,
};
static const vector<ExpandMethod> ppc_text_methods = {
ExpandMethod::PPC_TEXT_FORWARD,
ExpandMethod::PPC_TEXT_FORWARD_WITH_BARRIER,
ExpandMethod::PPC_TEXT_BACKWARD,
ExpandMethod::PPC_TEXT_BACKWARD_WITH_BARRIER,
ExpandMethod::PPC_TEXT_BOTH,
ExpandMethod::PPC_TEXT_BOTH_WITH_BARRIER,
ExpandMethod::PPC_TEXT_BOTH_IGNORE_ORIGIN,
};
static const vector<ExpandMethod> ppc_data_methods = {
ExpandMethod::PPC_DATA_FORWARD,
ExpandMethod::PPC_DATA_BACKWARD,
ExpandMethod::PPC_DATA_BOTH,
};
static const vector<ExpandMethod> raw_methods = {
ExpandMethod::RAW_FORWARD,
ExpandMethod::RAW_BACKWARD,
ExpandMethod::RAW_BOTH,
};
const auto& methods = this->enable_ppc ? ppc_methods : raw_methods;
for (size_t z = 0; z < methods.size(); z++) {
futures.emplace_back(async(&AddressTranslator::find_match, this, it.second, src_addr, src_size, methods[z]));
const vector<ExpandMethod>* methods;
if (this->ppc_mems.count(it.second)) {
if (type == MatchType::ANY) {
methods = &ppc_methods;
} else if (type == MatchType::TEXT) {
methods = &ppc_text_methods;
} else if (type == MatchType::DATA) {
methods = &ppc_data_methods;
} else {
throw logic_error("invalid match type");
}
} else {
methods = &raw_methods;
}
for (size_t z = 0; z < methods->size(); z++) {
futures.emplace_back(async(&AddressTranslator::find_match, this, it.second, src_addr, src_size, methods->at(z)));
}
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[z]);
const char* method_name = this->name_for_expand_method(methods->at(z));
try {
uint32_t ret = futures[z].get();
log.info("(%s) (%s) %08" PRIX32, it.first.c_str(), method_name, ret);
log.info_f("({}) ({}) {:08X}", it.first, method_name, ret);
match_addrs.emplace(ret);
} catch (const exception& e) {
log.error("(%s) (%s) failed: %s", it.first.c_str(), method_name, e.what());
log.error_f("({}) ({}) failed: {}", it.first, method_name, e.what());
}
}
if (match_addrs.empty()) {
log.error("(%s) no match found", it.first.c_str());
log.error_f("({}) no match found", it.first);
} else if (match_addrs.size() > 1) {
log.error("(%s) different matches found by different methods", it.first.c_str());
log.error_f("({}) different matches found by different methods", it.first);
} else {
results.emplace(it.first, *match_addrs.begin());
}
}
}
for (const auto& it : results) {
fprintf(stdout, "%s => %08" PRIX32 "\n", it.first.c_str(), it.second);
phosg::fwrite_fmt(stdout, "{} => {:08X}\n", it.first, it.second);
}
}
uint32_t find_be_to_le_data_match(
shared_ptr<const ResourceDASM::MemoryContext> dest_mem,
uint32_t src_addr,
uint32_t src_size) const {
shared_ptr<const ResourceDASM::MemoryContext> dest_mem, uint32_t src_addr, uint32_t src_size) const {
if (src_size == 0) {
src_size = 4;
}
@@ -490,7 +778,7 @@ public:
}
}
}
this->log.info("... For match length %zX, %zu matches found", match_length, num_matches);
this->log.info_f("... For match length {:X}, {} matches found", match_length, num_matches);
if (num_matches == 1) {
return last_match_address;
} else if (num_matches == 0) {
@@ -519,37 +807,37 @@ public:
map<string, uint32_t> results;
for (const auto& it : this->mems) {
if (it.second == this->src_mem) {
log.info("(%s) %08" PRIX32 " (from source)", it.first.c_str(), src_addr);
log.info_f("({}) {:08X} (from source)", it.first, src_addr);
results.emplace(it.first, src_addr);
} else {
uint32_t ret = 0;
try {
ret = this->find_be_to_le_data_match(it.second, src_addr, src_size);
log.info("(%s) %08" PRIX32, it.first.c_str(), ret);
log.info_f("({}) {:08X}", it.first, ret);
} catch (const exception& e) {
log.error("(%s) failed: %s", it.first.c_str(), e.what());
log.error_f("({}) failed: {}", it.first, e.what());
}
if (ret == 0) {
log.error("(%s) no match found", it.first.c_str());
log.error_f("({}) no match found", it.first);
} else {
results.emplace(it.first, ret);
}
}
}
for (const auto& it : results) {
fprintf(stdout, "%s => %08" PRIX32 "\n", it.first.c_str(), it.second);
phosg::fwrite_fmt(stdout, "{} => {:08X}\n", it.first, it.second);
}
}
void find_data(const std::string& data) const {
void find_data(const 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();
for (uint32_t addr = sec_addr; addr < last_addr; addr++) {
if (!mem->memcmp(addr, data.data(), data.size())) {
fprintf(stderr, "%s => %08" PRIX32 "\n", name.c_str(), addr);
phosg::fwrite_fmt(stderr, "{} => {:08X}\n", name, addr);
}
}
}
@@ -567,16 +855,41 @@ public:
this->set_source_file(tokens.at(1));
} 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()};
for (auto it = this->mems.begin(); it != this->mems.end();) {
if (to_keep.count(it->first)) {
it++;
} else {
it = this->mems.erase(it);
}
}
} else if (tokens[0] == "match") {
this->find_all_matches(
stoul(tokens.at(1), nullptr, 16),
tokens.size() >= 3 ? stoul(tokens[2], nullptr, 16) : 0);
tokens.size() >= 3 ? stoul(tokens[2], nullptr, 16) : 0,
MatchType::ANY);
} else if (tokens[0] == "match-text") {
this->find_all_matches(
stoul(tokens.at(1), nullptr, 16),
tokens.size() >= 3 ? stoul(tokens[2], nullptr, 16) : 0,
MatchType::TEXT);
} else if (tokens[0] == "match-data") {
this->find_all_matches(
stoul(tokens.at(1), nullptr, 16),
tokens.size() >= 3 ? stoul(tokens[2], nullptr, 16) : 0,
MatchType::DATA);
} else if (tokens[0] == "match-be-le") {
this->find_all_be_to_le_data_matches(
stoul(tokens.at(1), nullptr, 16),
tokens.size() >= 3 ? stoul(tokens[2], nullptr, 16) : 0);
} else if (tokens[0] == "find-ppc-globals") {
this->find_ppc_rtoc_global_regs();
} else if ((tokens[0] == "parse-dat-object-constructor-tables") ||
(tokens[0] == "parse-dat-enemy-constructor-tables")) {
bool is_enemies = (tokens[0] == "parse-dat-enemy-constructor-tables");
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");
}
@@ -585,9 +898,9 @@ public:
void run_shell() {
while (!feof(stdin)) {
if (!this->src_filename.empty()) {
fprintf(stdout, "addr-trans:%s/%s> ", this->directory.c_str(), this->src_filename.c_str());
phosg::fwrite_fmt(stdout, "addr-trans:{}/{}> ", this->directory, this->src_filename);
} else {
fprintf(stdout, "addr-trans:%s> ", this->directory.c_str());
phosg::fwrite_fmt(stdout, "addr-trans:{}> ", this->directory);
}
fflush(stdout);
@@ -595,7 +908,7 @@ public:
try {
this->handle_command(command);
} catch (const exception& e) {
this->log.error("Failed: %s", e.what());
this->log.error_f("Failed: {}", e.what());
}
}
fputc('\n', stdout);
@@ -605,12 +918,12 @@ 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;
bool enable_ppc;
};
void run_address_translator(const std::string& directory, const std::string& use_filename, const std::string& command) {
void run_address_translator(const string& directory, const string& use_filename, const string& command) {
AddressTranslator trans(directory);
if (!use_filename.empty()) {
trans.set_source_file(use_filename);
+407
View File
@@ -0,0 +1,407 @@
#include "AsyncHTTPServer.hh"
#include <inttypes.h>
#include <stdlib.h>
#include <phosg/Encoding.hh>
#include <phosg/Network.hh>
#include <phosg/Time.hh>
#include <string>
#include <vector>
#include "AsyncUtils.hh"
#include "Loggers.hh"
#include "Revision.hh"
#include "Server.hh"
using namespace std;
static const unordered_map<int, const char*> explanation_for_response_code{
{100, "Continue"},
{101, "Switching Protocols"},
{102, "Processing"},
{200, "OK"},
{201, "Created"},
{202, "Accepted"},
{203, "Non-Authoritative Information"},
{204, "No Content"},
{205, "Reset Content"},
{206, "Partial Content"},
{207, "Multi-Status"},
{208, "Already Reported"},
{226, "IM Used"},
{300, "Multiple Choices"},
{301, "Moved Permanently"},
{302, "Found"},
{303, "See Other"},
{304, "Not Modified"},
{305, "Use Proxy"},
{307, "Temporary Redirect"},
{308, "Permanent Redirect"},
{400, "Bad Request"},
{401, "Unathorized"},
{402, "Payment Required"},
{403, "Forbidden"},
{404, "Not Found"},
{405, "Method Not Allowed"},
{406, "Not Acceptable"},
{407, "Proxy Authentication Required"},
{408, "Request Timeout"},
{409, "Conflict"},
{410, "Gone"},
{411, "Length Required"},
{412, "Precondition Failed"},
{413, "Request Entity Too Large"},
{414, "Request-URI Too Long"},
{415, "Unsupported Media Type"},
{416, "Requested Range Not Satisfiable"},
{417, "Expectation Failed"},
{418, "I\'m a Teapot"},
{420, "Enhance Your Calm"},
{422, "Unprocessable Entity"},
{423, "Locked"},
{424, "Failed Dependency"},
{426, "Upgrade Required"},
{428, "Precondition Required"},
{429, "Too Many Requests"},
{431, "Request Header Fields Too Large"},
{444, "No Response"},
{449, "Retry With"},
{451, "Unavailable For Legal Reasons"},
{500, "Internal Server Error"},
{501, "Not Implemented"},
{502, "Bad Gateway"},
{503, "Service Unavailable"},
{504, "Gateway Timeout"},
{505, "HTTP Version Not Supported"},
{506, "Variant Also Negotiates"},
{507, "Insufficient Storage"},
{508, "Loop Detected"},
{509, "Bandwidth Limit Exceeded"},
{510, "Not Extended"},
{511, "Network Authentication Required"},
{598, "Network Read Timeout Error"},
{599, "Network Connect Timeout Error"},
};
HTTPError::HTTPError(int code, const std::string& what)
: std::runtime_error(what), code(code) {}
const std::string* HTTPRequest::get_header(const std::string& name) const {
auto its = this->headers.equal_range(name);
if (its.first == its.second) {
return nullptr;
}
const string* ret = &its.first->second;
its.first++;
if (its.first != its.second) {
throw std::out_of_range("Header appears multiple times: " + name);
}
return ret;
}
const std::string* HTTPRequest::get_query_param(const std::string& name) const {
auto its = this->query_params.equal_range(name);
if (its.first == its.second) {
return nullptr;
}
const string* ret = &its.first->second;
its.first++;
if (its.first != its.second) {
throw std::out_of_range("Query parameter appears multiple times: " + name);
}
return ret;
}
static void url_decode_inplace(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)) {
s[write_offset] =
static_cast<char>(phosg::value_for_hex_char(s[read_offset + 1]) << 4) |
static_cast<char>(phosg::value_for_hex_char(s[read_offset + 2]));
read_offset += 3;
} else if (s[write_offset] == '+') {
s[write_offset] = ' ';
read_offset++;
} else {
s[write_offset] = s[read_offset];
read_offset++;
}
}
s.resize(write_offset);
}
HTTPClient::HTTPClient(asio::ip::tcp::socket&& sock) : r(std::move(sock)) {}
asio::awaitable<HTTPRequest> HTTPClient::recv_http_request(size_t max_line_size, size_t max_body_size) {
HTTPRequest req;
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");
}
const auto& method_token = line_tokens[0];
if (method_token == "GET") {
req.method = HTTPRequest::Method::GET;
} else if (method_token == "POST") {
req.method = HTTPRequest::Method::POST;
} else if (method_token == "DELETE") {
req.method = HTTPRequest::Method::DELETE;
} else if (method_token == "HEAD") {
req.method = HTTPRequest::Method::HEAD;
} else if (method_token == "PATCH") {
req.method = HTTPRequest::Method::PATCH;
} else if (method_token == "PUT") {
req.method = HTTPRequest::Method::PUT;
} else if (method_token == "UPDATE") {
req.method = HTTPRequest::Method::UPDATE;
} else if (method_token == "OPTIONS") {
req.method = HTTPRequest::Method::OPTIONS;
} else if (method_token == "CONNECT") {
req.method = HTTPRequest::Method::CONNECT;
} else if (method_token == "TRACE") {
req.method = HTTPRequest::Method::TRACE;
} else {
throw HTTPError(400, "Unknown request method");
}
req.http_version = std::move(line_tokens[2]);
size_t fragment_start_offset = line_tokens[1].find('#');
if (fragment_start_offset != 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) {
query = line_tokens[1].substr(query_start_offset + 1);
line_tokens[1].resize(query_start_offset);
}
req.path = std::move(line_tokens[1]);
if (req.path.empty()) {
throw std::runtime_error("request path is missing");
}
auto query_tokens = phosg::split(query, '&');
for (auto& token : query_tokens) {
size_t equals_pos = token.find('=');
if (equals_pos == 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);
url_decode_inplace(key);
url_decode_inplace(value);
req.query_params.emplace(std::move(key), std::move(value));
}
}
auto prev_header_it = req.headers.end();
for (;;) {
std::string line = co_await this->r.read_line("\r\n", max_line_size);
if (line.empty()) {
break;
}
if (line[0] == ' ' || line[0] == '\t') {
if (prev_header_it == req.headers.end()) {
throw std::runtime_error("received header continuation line before any header");
} else {
phosg::strip_whitespace(line);
prev_header_it->second.append(1, ' ');
prev_header_it->second += line;
}
} else {
size_t colon_pos = line.find(':');
if (colon_pos == string::npos) {
throw runtime_error("malformed header line");
}
string key = line.substr(0, colon_pos);
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));
}
}
auto transfer_encoding_header = req.get_header("transfer-encoding");
if (transfer_encoding_header && phosg::tolower(*transfer_encoding_header) == "chunked") {
deque<string> chunks;
size_t total_data_bytes = 0;
for (;;) {
auto line = co_await this->r.read_line("\r\n", 0x20);
size_t parse_offset = 0;
size_t chunk_size = stoull(line, &parse_offset, 16);
if (parse_offset != line.size()) {
throw HTTPError(400, "Invalid chunk header during chunked encoding");
}
if (chunk_size == 0) {
break;
}
total_data_bytes += chunk_size;
if (total_data_bytes > max_body_size) {
throw HTTPError(400, "Request data size too large");
}
chunks.emplace_back(co_await this->r.read_data(chunk_size));
auto after_chunk_data = co_await this->r.read_line("\r\n", 0x20);
if (!after_chunk_data.empty()) {
throw HTTPError(400, "Incorrect trailing sequence after chunk data");
}
}
} else {
auto content_length_header = req.get_header("content-length");
size_t content_length = content_length_header ? stoull(*content_length_header) : 0;
if (content_length > max_body_size) {
throw HTTPError(400, "Request data size too large");
} else if (content_length > 0) {
req.data = co_await this->r.read_data(content_length);
}
}
co_return req;
}
asio::awaitable<void> HTTPClient::send_http_response(const HTTPResponse& resp) {
AsyncWriteCollector w;
w.add(std::format("{} {} {}\r\n",
resp.http_version, resp.response_code, explanation_for_response_code.at(resp.response_code)));
for (const auto& it : resp.headers) {
w.add(it.first + ": " + it.second + "\r\n");
}
if (!resp.data.empty()) {
w.add(std::format("Content-Length: {}\r\n", resp.data.size()));
}
w.add("\r\n");
if (!resp.data.empty()) {
w.add_reference(resp.data.data(), resp.data.size());
}
co_await w.write(this->r.get_socket());
}
asio::awaitable<WebSocketMessage> HTTPClient::recv_websocket_message(size_t max_data_size) {
WebSocketMessage prev_msg;
bool prev_msg_present = false;
while (this->r.get_socket().is_open()) {
WebSocketMessage msg;
// We need at most 10 bytes to determine if there's a valid frame, or as little as 2
co_await this->r.read_data_into(msg.header, 2);
// Get the payload size
bool has_mask = msg.header[1] & 0x80;
size_t payload_size = msg.header[1] & 0x7F;
if (payload_size == 0x7F) {
phosg::be_uint64_t wire_size;
co_await this->r.read_data_into(&wire_size, sizeof(wire_size));
payload_size = wire_size;
} else if (payload_size == 0x7E) {
phosg::be_uint16_t wire_size;
co_await this->r.read_data_into(&wire_size, sizeof(wire_size));
payload_size = wire_size;
}
if (payload_size > max_data_size) {
throw runtime_error("Incoming WebSocket message exceeds size limit");
}
// Read the masking key if present
if (has_mask) {
co_await this->r.read_data_into(msg.mask_key, sizeof(msg.mask_key));
}
// Read and unmask message data
msg.data = co_await this->r.read_data(payload_size);
if (has_mask) {
for (size_t x = 0; x < msg.data.size(); x++) {
msg.data[x] ^= msg.mask_key[x & 3];
}
}
this->last_communication_time = phosg::now();
// If the current message is a control message, respond appropriately (these can be sent in the middle of
// fragmented messages)
uint8_t opcode = msg.header[0] & 0x0F;
if (opcode & 0x08) {
if (opcode == 0x0A) {
// Ping response; ignore it
} else if (opcode == 0x08) {
// Close message
co_await this->send_websocket_message(msg.data, msg.opcode);
this->r.close();
} else if (opcode == 0x09) {
// Ping message
co_await this->send_websocket_message(msg.data, 0x0A);
} else {
// Unknown control message type
this->r.close();
}
continue;
}
// If there's an existing fragment, the current message's opcode should be zero; if there's no pending message, it
// must not be zero
if (prev_msg_present == (opcode != 0)) {
this->r.close();
continue;
}
// Save the message opcode, if present, and append the frame data
if (!prev_msg_present) {
prev_msg = std::move(msg);
} else {
prev_msg.header[0] = msg.header[0];
prev_msg.header[1] = msg.header[1];
if (opcode) {
prev_msg.opcode = msg.opcode;
}
if (has_mask) {
prev_msg.mask_key[0] = msg.mask_key[0];
prev_msg.mask_key[1] = msg.mask_key[1];
prev_msg.mask_key[2] = msg.mask_key[2];
prev_msg.mask_key[3] = msg.mask_key[3];
}
prev_msg.data += msg.data;
}
// If the FIN bit is set, then the frame is complete - append the payload to any pending payloads and call the
// message handler. If the FIN bit isn't set, we need to receive at least one continuation frame to complete the
// message.
if (prev_msg.header[0] & 0x80) {
co_return prev_msg;
}
}
throw logic_error("failed to receive websocket message");
}
asio::awaitable<void> HTTPClient::send_websocket_message(const void* data, size_t size, uint8_t opcode) {
phosg::StringWriter w;
w.put_u8(0x80 | (opcode & 0x0F));
if (size > 0xFFFF) {
w.put_u8(0x7F);
w.put_u64b(size);
} else if (size > 0x7D) {
w.put_u8(0x7E);
w.put_u16b(size);
} else {
w.put_u8(size);
}
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);
}
asio::awaitable<void> HTTPClient::send_websocket_message(const std::string& data, uint8_t opcode) {
return this->send_websocket_message(data.data(), data.size(), opcode);
}
const HTTPServerLimits DEFAULT_HTTP_LIMITS;
+348
View File
@@ -0,0 +1,348 @@
#pragma once
#include "WindowsPlatform.hh"
#include <stdlib.h>
#include <asio.hpp>
#include <exception>
#include <functional>
#include <memory>
#include <optional>
#include <phosg/Encoding.hh>
#include <phosg/Hash.hh>
#include <phosg/Time.hh>
#include <string>
#include "AsyncUtils.hh"
#include "Server.hh"
struct HTTPRequest {
enum class Method {
GET = 0,
POST,
DELETE,
HEAD,
PATCH,
PUT,
UPDATE,
OPTIONS,
CONNECT,
TRACE,
};
std::string http_version;
Method method;
std::string path;
std::string fragment;
std::unordered_multimap<std::string, std::string> headers; // Header names converted to all lowercase
std::unordered_multimap<std::string, std::string> query_params;
std::string data;
// Header name should be entirely lowercase for this function. Returns nullptr if the header doesn't exist; throws
// http_error(400) if multiple instances of it exist.
const std::string* get_header(const std::string& name) const;
const std::string* get_query_param(const std::string& name) const;
};
struct HTTPResponse {
std::string http_version;
int response_code = 200;
// Content-Length should NOT be specified in headers; it is automatically added in async_write() if data isn't blank.
std::unordered_multimap<std::string, std::string> headers;
std::string data;
};
struct WebSocketMessage {
uint8_t header[2] = {0, 0};
uint8_t opcode = 0x01;
uint8_t mask_key[4] = {0, 0, 0, 0};
std::string data;
};
class HTTPError : public std::runtime_error {
public:
HTTPError(int code, const std::string& what);
int code;
};
struct HTTPClient {
AsyncSocketReader r;
uint64_t last_communication_time = 0;
bool is_websocket = false;
HTTPClient(asio::ip::tcp::socket&& sock);
asio::awaitable<HTTPRequest> recv_http_request(size_t max_line_size, size_t max_body_size);
asio::awaitable<void> send_http_response(const HTTPResponse& resp);
asio::awaitable<WebSocketMessage> recv_websocket_message(size_t max_data_size);
asio::awaitable<void> send_websocket_message(const void* data, size_t size, uint8_t opcode = 0x01);
asio::awaitable<void> send_websocket_message(const std::string& data, uint8_t opcode = 0x01);
};
template <typename RetT>
class HTTPRouter {
public:
struct Args {
std::shared_ptr<HTTPClient> client;
const HTTPRequest& req;
std::unordered_map<std::string, std::string> params;
phosg::JSON post_data;
template <typename T>
requires(std::is_integral_v<T>)
T get_param(const char* name, bool hex = false) const {
const auto& value_str = this->params.at(name);
size_t conversion_end;
int64_t v = std::stoull(value_str, &conversion_end, hex ? 16 : 0);
if (conversion_end != value_str.size()) {
throw HTTPError(400, "Invalid integer value");
}
uint64_t uv = static_cast<uint64_t>(v);
if constexpr (std::is_unsigned_v<T>) {
if (uv & (~phosg::mask_for_type<T>)) {
throw HTTPError(400, "Unsigned value out of range");
}
return uv;
} else {
if (((uv & (~(phosg::mask_for_type<T> >> 1))) != 0) && ((uv & (~(phosg::mask_for_type<T> >> 1))) != (~(phosg::mask_for_type<T> >> 1)))) {
throw HTTPError(400, "Signed value out of range");
}
return v;
}
}
};
using Handler = std::function<asio::awaitable<RetT>(Args&&)>;
static std::vector<std::string> split_and_normalize_path(const std::string& path) {
auto path_tokens = phosg::split(path, '/');
while (!path_tokens.empty() && path_tokens.back().empty()) {
path_tokens.pop_back();
}
return path_tokens;
}
void add(HTTPRequest::Method method, const std::string& path_pattern, Handler handler) {
this->routes.emplace_back(Route{
.method = method, .path_tokens = this->split_and_normalize_path(path_pattern), .handler = handler});
}
asio::awaitable<RetT> call_handler(std::shared_ptr<HTTPClient> c, const HTTPRequest& req) {
Args args = {.client = c, .req = req, .params = {}, .post_data = phosg::JSON()};
auto tokens = this->split_and_normalize_path(req.path);
for (const auto& route : this->routes) {
if (route.path_tokens.size() != tokens.size()) {
continue;
}
bool matched = true;
args.params.clear();
for (size_t z = 0; z < tokens.size(); z++) {
if (route.path_tokens[z].starts_with(':')) {
args.params.emplace(route.path_tokens[z].substr(1), tokens[z]);
} else if (route.path_tokens[z] != tokens[z]) {
matched = false;
break;
}
}
if (matched) {
if (req.method != route.method) {
throw HTTPError(405, "Incorrect HTTP method");
}
if (req.method == HTTPRequest::Method::POST) {
auto* content_type = req.get_header("content-type");
if (!content_type || (*content_type != "application/json")) {
throw HTTPError(400, "POST requests must use the application/json content type");
}
try {
args.post_data = phosg::JSON::parse(req.data);
} catch (const std::exception& e) {
throw HTTPError(400, std::format("Invalid JSON: {}", e.what()));
}
}
co_return co_await route.handler(std::move(args));
}
}
throw HTTPError(404, "Request path did not match any route");
}
private:
struct Route {
HTTPRequest::Method method;
std::vector<std::string> path_tokens;
Handler handler;
};
std::vector<Route> routes;
};
struct HTTPServerLimits {
size_t max_http_request_line_size = 0x1000; // 4KB
size_t max_http_data_size = 0x200000; // 2MB
size_t max_http_keepalive_idle_usecs = 300 * 1000 * 1000; // 5 minutes (0 = no limit)
size_t max_websocket_message_size = 0x200000; // 2MB
size_t max_websocket_idle_usecs = 0; // No limit by default
};
extern const HTTPServerLimits DEFAULT_HTTP_LIMITS;
template <typename ClientT = HTTPClient>
class AsyncHTTPServer : public Server<ClientT, ServerSocket> {
public:
explicit AsyncHTTPServer(
std::shared_ptr<asio::io_context> io_context,
const std::string& log_prefix = "[AsyncHTTPServer] ",
const HTTPServerLimits& limits = DEFAULT_HTTP_LIMITS)
: Server<ClientT, ServerSocket>(io_context, log_prefix), limits(limits) {}
AsyncHTTPServer(const AsyncHTTPServer&) = delete;
AsyncHTTPServer(AsyncHTTPServer&&) = delete;
AsyncHTTPServer& operator=(const AsyncHTTPServer&) = delete;
AsyncHTTPServer& operator=(AsyncHTTPServer&&) = delete;
virtual ~AsyncHTTPServer() = default;
void listen(const std::string& addr, int port) {
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 = std::make_shared<ServerSocket>();
sock->name = std::format("http:{}:{}", addr, port);
sock->endpoint = asio::ip::tcp::endpoint(asio_addr, port);
this->add_socket(std::move(sock));
}
protected:
HTTPServerLimits limits;
void require_GET(const HTTPRequest& req) {
if (req.method != HTTPRequest::Method::GET) {
throw HTTPError(405, "GET method required for this endpoint");
}
}
phosg::JSON require_JSON_POST(const HTTPRequest& req) {
if (req.method != HTTPRequest::Method::POST) {
throw HTTPError(405, "POST method required for this endpoint");
}
auto* content_type = req.get_header("content-type");
if (!content_type || (*content_type != "application/json")) {
throw HTTPError(400, "POST requests must use the application/json content type");
}
try {
return phosg::JSON::parse(req.data);
} catch (const std::exception& e) {
throw HTTPError(400, std::format("Invalid JSON: {}", e.what()));
}
}
// Attempts to switch the client to WebSockets. Returns true if this is done successfully (and the caller should then
// receive/send WebSocket messages), or false if this failed (and the caller should send an HTTP response).
asio::awaitable<bool> enable_websockets(std::shared_ptr<ClientT> c, const HTTPRequest& req) {
if (req.method != HTTPRequest::Method::GET) {
co_return false;
}
auto connection_header = req.get_header("connection");
if (!connection_header || phosg::tolower(*connection_header) != "upgrade") {
co_return false;
}
auto upgrade_header = req.get_header("upgrade");
if (!upgrade_header || phosg::tolower(*upgrade_header) != "websocket") {
co_return false;
}
auto sec_websocket_key_header = req.get_header("sec-websocket-key");
if (!sec_websocket_key_header) {
co_return false;
}
std::string sec_websocket_accept_data = *sec_websocket_key_header + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
std::string sec_websocket_accept = phosg::base64_encode(phosg::SHA1(sec_websocket_accept_data).bin());
HTTPResponse resp;
resp.http_version = req.http_version;
resp.response_code = 101;
resp.headers.emplace("Upgrade", "websocket");
resp.headers.emplace("Connection", "upgrade");
resp.headers.emplace("Sec-WebSocket-Accept", std::move(sec_websocket_accept));
co_await c->send_http_response(resp);
c->is_websocket = true;
co_return true;
}
[[nodiscard]] virtual std::shared_ptr<ClientT> create_client(
std::shared_ptr<ServerSocket>, asio::ip::tcp::socket&& client_sock) {
return std::make_shared<HTTPClient>(std::move(client_sock));
}
// handle_request must do one of the following three things:
// 1. Return an HTTP response.
// 2. Call enable_websockets, and if it returns true, return nullptr. After this point, handle_request will not be
// called again for this client; handle_websocket_message will be called instead when any WebSocket messages are
// received. If enable_websockets returns false, handle_request must still return an HTTP response.
// 3. Throw an exception. In this case, the client receives an HTTP 500 response.
virtual asio::awaitable<std::unique_ptr<HTTPResponse>> handle_request(std::shared_ptr<ClientT> c, HTTPRequest&& req) = 0;
virtual asio::awaitable<void> handle_websocket_message(std::shared_ptr<ClientT>, WebSocketMessage&&) {
co_return;
}
virtual asio::awaitable<void> handle_client(std::shared_ptr<ClientT> c) {
asio::steady_timer idle_timer(*this->io_context);
while (c->r.get_socket().is_open()) {
if (c->is_websocket) {
WebSocketMessage msg = co_await c->recv_websocket_message(this->limits.max_websocket_message_size);
idle_timer.cancel();
try {
co_await this->handle_websocket_message(c, std::move(msg));
} catch (const std::exception& e) {
c->r.close();
}
} else {
HTTPRequest req = co_await c->recv_http_request(
this->limits.max_http_request_line_size, this->limits.max_http_data_size);
idle_timer.cancel();
std::unique_ptr<HTTPResponse> resp;
try {
resp = co_await this->handle_request(c, std::move(req));
} catch (const std::exception& e) {
resp = std::make_unique<HTTPResponse>();
resp->http_version = req.http_version;
resp->response_code = 500;
resp->headers.emplace("Content-Type", "text/plain");
resp->data = "Internal server error:\n";
resp->data += e.what();
}
if (resp) {
co_await c->send_http_response(*resp);
}
if (!c->is_websocket) {
auto* conn_header = req.get_header("connection");
if (!conn_header || (*conn_header != "keep-alive")) {
c->r.close();
}
}
}
size_t idle_usecs_limit = c->is_websocket
? this->limits.max_websocket_idle_usecs
: this->limits.max_http_keepalive_idle_usecs;
if (idle_usecs_limit && c->r.get_socket().is_open()) {
idle_timer.expires_after(std::chrono::microseconds(idle_usecs_limit));
idle_timer.async_wait([c](std::error_code ec) {
if (!ec) {
c->r.close();
}
});
}
}
idle_timer.cancel();
}
};
+162
View File
@@ -0,0 +1,162 @@
#include "AsyncUtils.hh"
#include <asio.hpp>
#include <exception>
#include <functional>
#include <optional>
#include <phosg/Strings.hh>
#include <string>
using namespace std;
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);
this->is_set = true;
this->waiters.swap(waiters_to_resume);
}
for (auto& waiter : waiters_to_resume) {
asio::post(this->executor,
[handler = std::move(waiter)]() mutable {
(*handler)();
});
}
}
void AsyncEvent::clear() {
lock_guard g(this->lock);
this->is_set = false;
}
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);
if (this->is_set) {
handler();
} else {
this->waiters.emplace_back(make_unique<asio::detail::awaitable_handler<asio::any_io_executor>>(std::move(handler)));
}
},
token);
}
AsyncSocketReader::AsyncSocketReader(asio::ip::tcp::socket&& sock)
: sock(std::move(sock)) {}
asio::awaitable<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");
}
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))) {
size_t pre_size = this->pending_data.size();
this->pending_data.resize(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);
this->pending_data.resize(pre_size + bytes_read);
delimiter_pos = this->pending_data.find(
delimiter,
(delimiter_backup_bytes > pre_size) ? 0 : (pre_size - delimiter_backup_bytes));
}
if (delimiter_pos == string::npos) {
throw 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);
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;
if (this->pending_data.size() == size) {
this->pending_data.swap(ret);
} else if (this->pending_data.size() > size) {
ret = this->pending_data.substr(0, size);
this->pending_data = this->pending_data.substr(size);
} else {
size_t bytes_to_read = size - this->pending_data.size();
this->pending_data.swap(ret);
ret.resize(size);
co_await asio::async_read(this->sock, asio::buffer(ret.data() + size - bytes_to_read, bytes_to_read), asio::use_awaitable);
}
co_return ret;
}
asio::awaitable<void> AsyncSocketReader::read_data_into(void* data, size_t size) {
if (this->pending_data.size() == size) {
memcpy(data, this->pending_data.data(), size);
this->pending_data.clear();
} else if (this->pending_data.size() > size) {
memcpy(data, this->pending_data.data(), size);
this->pending_data = this->pending_data.substr(size);
} else {
memcpy(data, this->pending_data.data(), this->pending_data.size());
size_t bytes_to_read = size - this->pending_data.size();
this->pending_data.clear();
void* read_buf = reinterpret_cast<uint8_t*>(data) + size - bytes_to_read;
co_await asio::async_read(this->sock, asio::buffer(read_buf, bytes_to_read), asio::use_awaitable);
}
}
void AsyncWriteCollector::add(string&& data) {
const auto& item = this->owned_data.emplace_back(std::move(data));
bufs.emplace_back(asio::buffer(item.data(), item.size()));
}
void AsyncWriteCollector::add_reference(const void* data, size_t size) {
bufs.emplace_back(asio::buffer(data, size));
}
asio::awaitable<void> AsyncWriteCollector::write(asio::ip::tcp::socket& sock) {
deque<string> local_owned_data;
local_owned_data.swap(this->owned_data);
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::steady_timer timer(co_await asio::this_coro::executor, duration);
co_await timer.async_wait(asio::use_awaitable);
}
asio::awaitable<asio::ip::tcp::socket> async_connect_tcp(uint32_t ipv4_addr, uint16_t port) {
uint8_t octets[4] = {
static_cast<uint8_t>(ipv4_addr >> 24),
static_cast<uint8_t>(ipv4_addr >> 16),
static_cast<uint8_t>(ipv4_addr >> 8),
static_cast<uint8_t>(ipv4_addr)};
return async_connect_tcp(std::format("{}.{}.{}.{}", octets[0], octets[1], octets[2], octets[3]), port);
}
asio::awaitable<asio::ip::tcp::socket> async_connect_tcp(const std::string& host, uint16_t port) {
auto executor = co_await asio::this_coro::executor;
asio::ip::tcp::resolver resolver(executor);
auto endpoints = co_await resolver.async_resolve(host, std::format("{}", port), asio::use_awaitable);
asio::ip::tcp::socket sock(executor);
co_await asio::async_connect(sock, endpoints, asio::use_awaitable);
co_return sock;
}
asio::awaitable<asio::ip::tcp::socket> async_connect_tcp(const asio::ip::tcp::endpoint& ep) {
auto executor = co_await asio::this_coro::executor;
asio::ip::tcp::socket sock(executor);
co_await sock.async_connect(ep, asio::use_awaitable);
co_return sock;
}
+273
View File
@@ -0,0 +1,273 @@
#pragma once
#include <asio.hpp>
#include <asio/experimental/parallel_group.hpp>
#include <asio/experimental/promise.hpp>
#include <deque>
#include <exception>
#include <functional>
#include <optional>
#include <phosg/Strings.hh>
template <typename T>
class AsyncPromise {
public:
AsyncPromise() = default;
asio::awaitable<T> get() {
if (!this->exc && !this->val.has_value()) {
auto executor = co_await asio::this_coro::executor;
co_await asio::async_initiate<decltype(asio::use_awaitable), void(std::error_code)>(
[this, &executor](auto&& new_handler) {
this->resolver_ref.emplace(ResolverRef{.resolve = std::move(new_handler), .executor = &executor});
},
asio::use_awaitable);
}
if (this->exc) {
std::rethrow_exception(this->exc);
} else if (this->val.has_value()) {
co_return *this->val;
} else {
throw std::logic_error("AsyncPromise await resolved but did not have a value or exception");
}
}
void set_value(T&& result) {
if (this->done()) {
throw std::logic_error("attempted to set value on completed promise");
}
this->val = result;
this->resolve();
}
void set_exception(std::exception_ptr ex) {
if (this->done()) {
throw std::logic_error("attempted to set value on completed promise");
}
this->exc = ex;
this->resolve();
}
void cancel() {
this->set_exception(std::make_exception_ptr(std::runtime_error("AsyncPromise cancelled")));
}
bool done() const {
return this->exc || this->val.has_value();
}
private:
struct ResolverRef {
asio::detail::awaitable_handler<asio::any_io_executor, std::error_code> resolve;
asio::any_io_executor* executor;
};
std::optional<T> val;
std::exception_ptr exc;
std::optional<ResolverRef> resolver_ref;
void resolve() {
if (this->resolver_ref) {
auto* executor = this->resolver_ref->executor;
ResolverRef ref = std::move(*this->resolver_ref);
this->resolver_ref.reset();
asio::post(*executor, [ref = std::move(ref)]() mutable -> void {
ref.resolve(std::error_code{});
});
}
}
};
template <>
class AsyncPromise<void> {
public:
AsyncPromise() = default;
asio::awaitable<void> get() {
if (!this->exc && !this->returned) {
auto executor = co_await asio::this_coro::executor;
co_await asio::async_initiate<decltype(asio::use_awaitable), void(std::error_code)>(
[this, &executor](auto&& new_handler) {
this->resolver_ref.emplace(ResolverRef{.resolve = std::move(new_handler), .executor = &executor});
},
asio::use_awaitable);
}
if (this->exc) {
std::rethrow_exception(this->exc);
} else if (this->returned) {
co_return;
} else {
throw std::logic_error("AsyncPromise await resolved but did not have a value or exception");
}
}
void set_value() {
if (this->done()) {
throw std::logic_error("attempted to set value on completed promise");
}
this->returned = true;
this->resolve();
}
void set_exception(std::exception_ptr ex) {
if (this->done()) {
throw std::logic_error("attempted to set value on completed promise");
}
this->exc = ex;
this->resolve();
}
void cancel() {
this->set_exception(std::make_exception_ptr(std::runtime_error("AsyncPromise cancelled")));
}
bool done() const {
return this->exc || this->returned;
}
private:
struct ResolverRef {
asio::detail::awaitable_handler<asio::any_io_executor, std::error_code> resolve;
asio::any_io_executor* executor;
};
bool returned = false;
std::exception_ptr exc;
std::optional<ResolverRef> resolver_ref;
void resolve() {
if (this->resolver_ref) {
auto* executor = this->resolver_ref->executor;
ResolverRef ref = std::move(*this->resolver_ref);
this->resolver_ref.reset();
asio::post(*executor, [ref = std::move(ref)]() mutable -> void {
ref.resolve(std::error_code{});
});
}
}
};
class AsyncEvent {
public:
AsyncEvent(asio::any_io_executor ex);
AsyncEvent(const AsyncEvent&) = delete;
AsyncEvent(AsyncEvent&&) = delete;
AsyncEvent& operator=(const AsyncEvent&) = delete;
AsyncEvent& operator=(AsyncEvent&&) = delete;
void set();
void clear();
asio::awaitable<void> wait();
private:
asio::any_io_executor executor;
bool is_set;
std::mutex lock;
std::vector<std::unique_ptr<asio::detail::awaitable_handler<asio::any_io_executor>>> waiters;
};
class AsyncSocketReader {
public:
explicit AsyncSocketReader(asio::ip::tcp::socket&& sock);
AsyncSocketReader(const AsyncSocketReader&) = delete;
AsyncSocketReader(AsyncSocketReader&&) = delete;
AsyncSocketReader& operator=(const AsyncSocketReader&) = delete;
AsyncSocketReader& operator=(AsyncSocketReader&&) = delete;
~AsyncSocketReader() = default;
// Reads one line from the socket, buffering any extra data read. The delimiter is not included in the returned line.
// max_length = 0 means no maximum length is enforced.
asio::awaitable<std::string> read_line(
const char* delimiter = "\n", size_t max_length = 0);
asio::awaitable<std::string> read_data(size_t size);
asio::awaitable<void> read_data_into(void* data, size_t size);
// The caller cannot know what the socket's read state is, so this should only be used when the caller intends to
// write to the socket, not read
inline asio::ip::tcp::socket& get_socket() {
return this->sock;
}
inline bool is_open() const {
return this->sock.is_open();
}
inline void close() {
if (this->sock.is_open()) {
this->sock.close();
}
}
private:
std::string pending_data; // Data read but not yet returned to the caller
asio::ip::tcp::socket sock;
};
class AsyncWriteCollector {
public:
AsyncWriteCollector() = default;
AsyncWriteCollector(const AsyncWriteCollector&) = delete;
AsyncWriteCollector(AsyncWriteCollector&&) = delete;
AsyncWriteCollector& operator=(const AsyncWriteCollector&) = delete;
AsyncWriteCollector& operator=(AsyncWriteCollector&&) = delete;
~AsyncWriteCollector() = default;
void add(std::string&& data);
// When using add_reference, it is the caller's responsibility to ensure that the buffer is valid until *this is
// destroyed or write() returns.
void add_reference(const void* data, size_t size);
asio::awaitable<void> write(asio::ip::tcp::socket& sock);
private:
std::deque<std::string> owned_data;
std::vector<asio::const_buffer> bufs;
};
asio::awaitable<void> async_sleep(std::chrono::steady_clock::duration duration);
inline asio::ip::tcp::endpoint make_endpoint_ipv4(uint32_t addr, uint16_t port) {
return asio::ip::tcp::endpoint(asio::ip::address_v4(addr), port);
}
inline asio::ip::tcp::endpoint make_endpoint_ipv6(const void* addr, uint16_t port) {
std::array<uint8_t, 0x10> bytes;
for (size_t z = 0; z < 0x10; z++) {
bytes[z] = reinterpret_cast<const uint8_t*>(addr)[z];
}
return asio::ip::tcp::endpoint(asio::ip::address_v6(bytes), port);
}
inline std::string str_for_endpoint(const asio::ip::tcp::endpoint& ep) {
return ep.address().to_string() + std::format(":{}", ep.port());
}
inline uint32_t ipv4_addr_for_asio_addr(const asio::ip::address& addr) {
if (!addr.is_v4()) {
throw std::runtime_error("Address is not IPv4");
}
return addr.to_v4().to_uint();
}
asio::awaitable<asio::ip::tcp::socket> async_connect_tcp(uint32_t ipv4_addr, uint16_t port);
asio::awaitable<asio::ip::tcp::socket> async_connect_tcp(const std::string& host, uint16_t port);
asio::awaitable<asio::ip::tcp::socket> async_connect_tcp(const asio::ip::tcp::endpoint& ep);
template <typename FnT, typename... ArgTs>
asio::awaitable<std::invoke_result_t<FnT, ArgTs...>> call_on_thread_pool(asio::thread_pool& pool, FnT&& f, ArgTs&&... args) {
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
// 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 {
try {
promise->set_value(bound());
} catch (...) {
promise->set_exception(std::current_exception());
}
});
co_return co_await promise->get();
}
+2 -4
View File
@@ -14,8 +14,7 @@ struct BMLHeaderT {
parray<uint8_t, 0x04> unknown_a1;
U32T<BE> num_entries;
parray<uint8_t, 0x38> unknown_a2;
} __packed__;
} __attribute__((packed));
using BMLHeader = BMLHeaderT<false>;
using BMLHeaderBE = BMLHeaderT<true>;
check_struct_size(BMLHeader, 0x40);
@@ -30,8 +29,7 @@ struct BMLHeaderEntryT {
U32T<BE> compressed_gvm_size;
U32T<BE> decompressed_gvm_size;
parray<uint8_t, 0x0C> unknown_a2;
} __packed__;
} __attribute__((packed));
using BMLHeaderEntry = BMLHeaderEntryT<false>;
using BMLHeaderEntryBE = BMLHeaderEntryT<true>;
check_struct_size(BMLHeaderEntry, 0x40);
+63 -23
View File
@@ -9,28 +9,68 @@
using namespace std;
void BattleParamsIndex::Table::print(FILE* stream) const {
auto print_entry = +[](FILE* stream, const PlayerStats& e) {
fprintf(stream,
"%5hu %5hu %5hu %5hu %5hu %5hu %5hu %5hu %5" PRIu32 " %5" PRIu32,
e.char_stats.atp.load(),
e.char_stats.mst.load(),
e.char_stats.evp.load(),
e.char_stats.hp.load(),
e.char_stats.dfp.load(),
e.char_stats.ata.load(),
e.char_stats.lck.load(),
e.esp.load(),
e.experience.load(),
e.meseta.load());
};
for (size_t diff = 0; diff < 4; diff++) {
fprintf(stream, "%c ZZ ATP PSV EVP HP DFP ATA LCK ESP EXP DIFF\n",
abbreviation_for_difficulty(diff));
void BattleParamsIndex::Table::print(FILE* stream, Episode episode) const {
phosg::fwrite_fmt(stream, "========== STATS\n");
for (Difficulty difficulty : ALL_DIFFICULTIES_V234) {
phosg::fwrite_fmt(stream, "{} ZZ ATP PSV EVP HP DFP ATA LCK ESP EXP DIFF NAMES\n",
abbreviation_for_difficulty(difficulty));
for (size_t z = 0; z < 0x60; z++) {
fprintf(stream, " %02zX ", z);
print_entry(stream, this->stats[diff][z]);
const auto& e = this->stats[static_cast<size_t>(difficulty)][z];
phosg::fwrite_fmt(stream, " {:02X} ", z);
string names_str;
for (auto type : enemy_types_for_battle_param_stats_index(episode, z)) {
if (!names_str.empty()) {
names_str += ", ";
}
names_str += phosg::name_for_enum(type);
}
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);
fputc('\n', stream);
}
}
phosg::fwrite_fmt(stream, "========== ATTACK DATA\n");
for (Difficulty difficulty : ALL_DIFFICULTIES_V234) {
phosg::fwrite_fmt(stream, "{} ZZ ATP- ATP+ ATA- ATA+ -DIST-X- -ANGLE-- -DIST-Y- -A8- -A9- A10- A11- --A12--- --A13--- --A14--- --A15--- --A16---\n",
abbreviation_for_difficulty(difficulty));
for (size_t z = 0; z < 0x60; z++) {
const auto& e = this->attack_data[static_cast<size_t>(difficulty)][z];
phosg::fwrite_fmt(stream,
" {:02X} {:04X} {:04X} {:04X} {:04X} {:8.3f} {:08X} {:8.3f} {:04X} {:04X} {:04X} {:04X} {:08X} {:08X} {:08X} {:08X} {:08X}",
z, e.min_atp, e.max_atp, e.min_ata, e.max_ata, e.distance_x, e.angle, e.distance_y, e.unknown_a8,
e.unknown_a9, e.unknown_a10, e.unknown_a11, e.unknown_a12, e.unknown_a13, e.unknown_a14, e.unknown_a15,
e.unknown_a16);
fputc('\n', stream);
}
}
phosg::fwrite_fmt(stream, "========== RESIST DATA\n");
for (Difficulty difficulty : ALL_DIFFICULTIES_V234) {
phosg::fwrite_fmt(stream, "{} ZZ EVP- EFR- EIC- ETH- ELT- EDK- ---A6--- ---A7--- ---A8--- ---A9--- --DFP---\n",
abbreviation_for_difficulty(difficulty));
for (size_t z = 0; z < 0x60; z++) {
const auto& e = this->resist_data[static_cast<size_t>(difficulty)][z];
phosg::fwrite_fmt(stream,
" {:02X} {:04X} {:04X} {:04X} {:04X} {:04X} {:04X} {:08X} {:08X} {:08X} {:08X} {:08X}",
z, e.evp_bonus, e.efr, e.eic, e.eth, e.elt, e.edk, e.unknown_a6, e.unknown_a7, e.unknown_a8, e.unknown_a9,
e.dfp_bonus);
fputc('\n', stream);
}
}
phosg::fwrite_fmt(stream, "========== MOVEMENT DATA\n");
for (Difficulty difficulty : ALL_DIFFICULTIES_V234) {
phosg::fwrite_fmt(stream, "{} ZZ FPARAM-1 FPARAM-2 FPARAM-3 FPARAM-4 FPARAM-5 FPARAM-6 IPARAM-1 IPARAM-2 IPARAM-3 IPARAM-4 IPARAM-5 IPARAM-6\n",
abbreviation_for_difficulty(difficulty));
for (size_t z = 0; z < 0x60; z++) {
const auto& e = this->movement_data[static_cast<size_t>(difficulty)][z];
phosg::fwrite_fmt(stream,
" {:02X} {:8.3f} {:8.3f} {:8.3f} {:8.3f} {:8.3f} {:8.3f} {:08X} {:08X} {:08X} {:08X} {:08X} {:08X}",
z, e.fparam1, e.fparam2, e.fparam3, e.fparam4, e.fparam5, e.fparam6,
e.iparam1, e.iparam2, e.iparam3, e.iparam4, e.iparam5, e.iparam6);
fputc('\n', stream);
}
}
@@ -54,8 +94,8 @@ 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(phosg::string_printf(
"battle params table size is incorrect (expected %zX bytes, have %zX bytes; is_solo=%hhu, episode=%hhu)",
throw 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));
}
file.table = reinterpret_cast<const Table*>(file.data->data());
+35 -22
View File
@@ -19,12 +19,12 @@ public:
// These files are little-endian, even on PSO GC.
struct AttackData {
/* 00 */ le_int16_t unknown_a1;
/* 02 */ le_int16_t atp;
/* 04 */ le_int16_t ata_bonus;
/* 06 */ le_uint16_t unknown_a4;
/* 00 */ le_int16_t min_atp;
/* 02 */ le_int16_t max_atp;
/* 04 */ le_int16_t min_ata;
/* 06 */ le_int16_t max_ata;
/* 08 */ le_float distance_x;
/* 0C */ le_uint32_t angle_x; // Out of 0x10000 (high 16 bits are unused)
/* 0C */ le_uint32_t angle; // Out of 0x10000 (high 16 bits are unused)
/* 10 */ le_float distance_y;
/* 14 */ le_uint16_t unknown_a8;
/* 16 */ le_uint16_t unknown_a9;
@@ -54,29 +54,42 @@ public:
} __packed_ws__(ResistData, 0x20);
struct MovementData {
/* 00 */ le_float idle_move_speed;
/* 04 */ le_float idle_animation_speed;
/* 08 */ le_float move_speed;
/* 0C */ le_float animation_speed;
/* 10 */ le_float unknown_a1;
/* 14 */ le_float unknown_a2;
/* 18 */ le_uint32_t unknown_a3;
/* 1C */ le_uint32_t unknown_a4;
/* 20 */ le_uint32_t unknown_a5;
/* 24 */ le_uint32_t unknown_a6;
/* 28 */ le_uint32_t unknown_a7;
/* 2C */ le_uint32_t unknown_a8;
/* 00 */ le_float fparam1;
/* 04 */ le_float fparam2;
/* 03 */ le_float fparam3;
/* 0C */ le_float fparam4;
/* 10 */ le_float fparam5;
/* 14 */ le_float fparam6;
/* 18 */ le_uint32_t iparam1;
/* 1C */ le_uint32_t iparam2;
/* 20 */ le_uint32_t iparam3;
/* 24 */ le_uint32_t iparam4;
/* 28 */ le_uint32_t iparam5;
/* 2C */ le_uint32_t iparam6;
/* 30 */
} __packed_ws__(MovementData, 0x30);
struct Table {
/* 0000 */ parray<parray<PlayerStats, 0x60>, 4> stats;
/* 3600 */ parray<parray<AttackData, 0x60>, 4> attack_data;
/* 7E00 */ parray<parray<ResistData, 0x60>, 4> resist_data;
/* AE00 */ parray<parray<MovementData, 0x60>, 4> movement_data;
/* 0000 */ parray<parray<PlayerStats, 0x60>, 4> stats; // [difficulty][bp_index]
/* 3600 */ parray<parray<AttackData, 0x60>, 4> attack_data; // [difficulty][bp_index]
/* 7E00 */ parray<parray<ResistData, 0x60>, 4> resist_data; // [difficulty][bp_index]
/* AE00 */ parray<parray<MovementData, 0x60>, 4> movement_data; // [difficulty][bp_index]
/* F600 */
void print(FILE* stream) const;
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 {
return this->attack_data.at(static_cast<size_t>(difficulty)).at(index);
}
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 {
return this->movement_data.at(static_cast<size_t>(difficulty)).at(index);
}
void print(FILE* stream, Episode episode) const;
} __packed_ws__(Table, 0xF600);
BattleParamsIndex(
-187
View File
@@ -1,187 +0,0 @@
#include "CatSession.hh"
#include <arpa/inet.h>
#include <ctype.h>
#include <errno.h>
#include <event2/buffer.h>
#include <event2/bufferevent.h>
#include <event2/event.h>
#include <event2/listener.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <algorithm>
#include <iostream>
#include <phosg/Encoding.hh>
#include <phosg/Filesystem.hh>
#include <phosg/Network.hh>
#include <phosg/Random.hh>
#include <phosg/Strings.hh>
#include <phosg/Time.hh>
#include "Loggers.hh"
#include "PSOProtocol.hh"
#include "ProxyCommands.hh"
#include "ReceiveCommands.hh"
#include "ReceiveSubcommands.hh"
#include "SendCommands.hh"
using namespace std;
CatSession::exit_shell::exit_shell() : runtime_error("shell exited") {}
CatSession::CatSession(
shared_ptr<struct event_base> base,
const struct sockaddr_storage& remote,
Version version,
shared_ptr<const PSOBBEncryption::KeyFile> bb_key_file)
: log(phosg::string_printf("[CatSession:%s] ", phosg::name_for_enum(version)), proxy_server_log.min_level),
base(base),
read_event(event_new(this->base.get(), 0, EV_READ | EV_PERSIST, CatSession::dispatch_read_stdin, this), event_free),
channel(version, 1, CatSession::dispatch_on_channel_input, CatSession::dispatch_on_channel_error, this, "CatSession"),
bb_key_file(bb_key_file) {
if (remote.ss_family != AF_INET) {
throw runtime_error("remote is not AF_INET");
}
string netloc_str = phosg::render_sockaddr_storage(remote);
this->log.info("Connecting to %s", netloc_str.c_str());
struct bufferevent* bev = bufferevent_socket_new(
this->base.get(), -1, BEV_OPT_CLOSE_ON_FREE | BEV_OPT_DEFER_CALLBACKS);
if (!bev) {
throw runtime_error(phosg::string_printf("failed to open socket (%d)", EVUTIL_SOCKET_ERROR()));
}
this->channel.set_bufferevent(bev, 0);
if (bufferevent_socket_connect(this->channel.bev.get(),
reinterpret_cast<const sockaddr*>(&remote), sizeof(struct sockaddr_in)) != 0) {
throw runtime_error(phosg::string_printf("failed to connect (%d)", EVUTIL_SOCKET_ERROR()));
}
event_add(this->read_event.get(), nullptr);
this->poll.add(0, POLLIN);
}
void CatSession::execute_command(const std::string& command) {
string full_cmd = phosg::parse_data_string(command, nullptr, phosg::ParseDataFlags::ALLOW_FILES);
send_command_with_header(this->channel, full_cmd.data(), full_cmd.size());
}
void CatSession::dispatch_on_channel_input(
Channel& ch, uint16_t command, uint32_t flag, std::string& data) {
auto* session = reinterpret_cast<CatSession*>(ch.context_obj);
session->on_channel_input(command, flag, data);
}
void CatSession::on_channel_input(
uint16_t command, uint32_t flag, std::string& data) {
if (!uses_v4_encryption(this->channel.version)) {
if (command == 0x02 || command == 0x17 || command == 0x91 || command == 0x9B) {
const auto& cmd = check_size_t<S_ServerInitDefault_DC_PC_V3_02_17_91_9B>(data, 0xFFFF);
if (uses_v3_encryption(this->channel.version)) {
this->channel.crypt_in = make_shared<PSOV3Encryption>(cmd.server_key);
this->channel.crypt_out = make_shared<PSOV3Encryption>(cmd.client_key);
this->log.info("Enabled V3 encryption (server key %08" PRIX32 ", client key %08" PRIX32 ")",
cmd.server_key.load(), cmd.client_key.load());
} else { // PC, DC, or patch server
this->channel.crypt_in = make_shared<PSOV2Encryption>(cmd.server_key);
this->channel.crypt_out = make_shared<PSOV2Encryption>(cmd.client_key);
this->log.info("Enabled V2 encryption (server key %08" PRIX32 ", client key %08" PRIX32 ")",
cmd.server_key.load(), cmd.client_key.load());
}
}
} else { // BB
if (command == 0x03 || command == 0x9B) {
if (!this->bb_key_file) {
throw runtime_error("BB encryption requires a key file");
}
const auto& cmd = check_size_t<S_ServerInitDefault_BB_03_9B>(data, 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->log.info("Enabled BB encryption");
}
}
// TODO: Use the iovec form of print_data here instead of
// prepend_command_header (which copies the string)
string full_cmd = prepend_command_header(this->channel.version, this->channel.crypt_in.get(), command, flag, data);
phosg::print_data(stdout, full_cmd, 0, nullptr, phosg::PrintDataFlags::PRINT_ASCII | phosg::PrintDataFlags::OFFSET_16_BITS);
}
void CatSession::dispatch_on_channel_error(Channel& ch, short events) {
auto* session = reinterpret_cast<CatSession*>(ch.context_obj);
session->on_channel_error(events);
}
void CatSession::on_channel_error(short events) {
if (events & BEV_EVENT_CONNECTED) {
this->log.info("Channel connected");
}
if (events & BEV_EVENT_ERROR) {
int err = EVUTIL_SOCKET_ERROR();
this->log.warning("Error %d (%s) in unlinked client stream", err,
evutil_socket_error_to_string(err));
}
if (events & (BEV_EVENT_ERROR | BEV_EVENT_EOF)) {
this->log.info("Session endpoint has disconnected");
this->channel.disconnect();
event_base_loopexit(this->base.get(), nullptr);
}
}
void CatSession::dispatch_read_stdin(evutil_socket_t, short, void* ctx) {
reinterpret_cast<CatSession*>(ctx)->read_stdin();
}
void CatSession::read_stdin() {
bool any_command_read = false;
for (;;) {
auto poll_result = this->poll.poll();
short fd_events = 0;
try {
fd_events = poll_result.at(0);
} catch (const out_of_range&) {
}
if (!(fd_events & POLLIN)) {
break;
}
string command(2048, '\0');
if (!fgets(command.data(), command.size(), stdin)) {
if (!any_command_read) {
// ctrl+d probably; we should exit
fputc('\n', stderr);
event_base_loopexit(this->base.get(), nullptr);
return;
} else {
break; // probably not EOF; just no more commands for now
}
}
// trim the extra data off the string
size_t len = strlen(command.c_str());
if (len == 0) {
break;
}
if (command[len - 1] == '\n') {
len--;
}
command.resize(len);
any_command_read = true;
try {
execute_command(command);
} catch (const exit_shell&) {
event_base_loopexit(this->base.get(), nullptr);
return;
} catch (const exception& e) {
fprintf(stderr, "FAILED: %s\n", e.what());
}
}
}
-54
View File
@@ -1,54 +0,0 @@
#pragma once
#include <event2/event.h>
#include <functional>
#include <map>
#include <memory>
#include <phosg/Filesystem.hh>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include "PSOEncryption.hh"
#include "PSOProtocol.hh"
#include "ServerState.hh"
class CatSession {
public:
CatSession(
std::shared_ptr<struct event_base> base,
const struct sockaddr_storage& remote,
Version version,
std::shared_ptr<const PSOBBEncryption::KeyFile> bb_key_file);
CatSession(const CatSession&) = delete;
CatSession(CatSession&&) = delete;
CatSession& operator=(const CatSession&) = delete;
CatSession& operator=(CatSession&&) = delete;
virtual ~CatSession() = default;
protected:
phosg::PrefixedLogger log;
std::shared_ptr<struct event_base> base;
std::unique_ptr<struct event, void (*)(struct event*)> read_event;
phosg::Poll poll;
Channel channel;
std::shared_ptr<const PSOBBEncryption::KeyFile> bb_key_file;
class exit_shell : public std::runtime_error {
public:
exit_shell();
~exit_shell() = default;
};
virtual void execute_command(const std::string& command);
static void dispatch_read_stdin(evutil_socket_t fd, short events, void* ctx);
static void dispatch_on_channel_input(Channel& ch, uint16_t command, uint32_t flag, std::string& msg);
static void dispatch_on_channel_error(Channel& ch, short events);
void on_channel_input(uint16_t command, uint32_t flag, std::string& msg);
void on_channel_error(short events);
void read_stdin();
};
+234 -264
View File
@@ -1,9 +1,6 @@
#include "Channel.hh"
#include <errno.h>
#include <event2/buffer.h>
#include <event2/bufferevent.h>
#include <event2/event.h>
#include <string.h>
#include <unistd.h>
@@ -11,245 +8,34 @@
#include <phosg/Time.hh>
#include "Loggers.hh"
#include "StaticGameData.hh"
#include "Version.hh"
using namespace std;
extern bool use_terminal_colors;
static void flush_and_free_bufferevent(struct bufferevent* bev) {
bufferevent_flush(bev, EV_READ | EV_WRITE, BEV_FINISHED);
bufferevent_free(bev);
}
Channel::Channel(
Version version,
uint8_t language,
on_command_received_t on_command_received,
on_error_t on_error,
void* context_obj,
Language language,
const string& name,
phosg::TerminalFormat terminal_send_color,
phosg::TerminalFormat terminal_recv_color)
: bev(nullptr, flush_and_free_bufferevent),
virtual_network_id(0),
version(version),
: version(version),
language(language),
name(name),
terminal_send_color(terminal_send_color),
terminal_recv_color(terminal_recv_color),
on_command_received(on_command_received),
on_error(on_error),
context_obj(context_obj) {
}
Channel::Channel(
struct bufferevent* bev,
uint64_t virtual_network_id,
Version version,
uint8_t language,
on_command_received_t on_command_received,
on_error_t on_error,
void* context_obj,
const string& name,
phosg::TerminalFormat terminal_send_color,
phosg::TerminalFormat terminal_recv_color)
: bev(nullptr, flush_and_free_bufferevent),
version(version),
language(language),
name(name),
terminal_send_color(terminal_send_color),
terminal_recv_color(terminal_recv_color),
on_command_received(on_command_received),
on_error(on_error),
context_obj(context_obj) {
this->set_bufferevent(bev, virtual_network_id);
}
void Channel::replace_with(
Channel&& other,
on_command_received_t on_command_received,
on_error_t on_error,
void* context_obj,
const std::string& name) {
this->set_bufferevent(other.bev.release(), other.virtual_network_id);
this->local_addr = other.local_addr;
this->remote_addr = other.remote_addr;
this->version = other.version;
this->language = other.language;
this->crypt_in = other.crypt_in;
this->crypt_out = other.crypt_out;
this->name = name;
this->terminal_send_color = other.terminal_send_color;
this->terminal_recv_color = other.terminal_recv_color;
this->on_command_received = on_command_received;
this->on_error = on_error;
this->context_obj = context_obj;
other.disconnect(); // Clears crypts, addrs, etc.
}
void Channel::set_bufferevent(struct bufferevent* bev, uint64_t virtual_network_id) {
this->bev.reset(bev);
this->virtual_network_id = virtual_network_id;
if (this->bev.get()) {
int fd = bufferevent_getfd(this->bev.get());
if (fd < 0) {
memset(&this->local_addr, 0, sizeof(this->local_addr));
memset(&this->remote_addr, 0, sizeof(this->remote_addr));
} else {
phosg::get_socket_addresses(fd, &this->local_addr, &this->remote_addr);
}
bufferevent_setcb(this->bev.get(), &Channel::dispatch_on_input, nullptr, &Channel::dispatch_on_error, this);
bufferevent_enable(this->bev.get(), EV_READ | EV_WRITE);
} else {
memset(&this->local_addr, 0, sizeof(this->local_addr));
memset(&this->remote_addr, 0, sizeof(this->remote_addr));
}
}
void Channel::disconnect() {
if (this->bev.get()) {
// If the output buffer is not empty, move the bufferevent into the draining
// pool instead of disconnecting it, to make sure all the data gets sent.
struct evbuffer* out_buffer = bufferevent_get_output(this->bev.get());
if (evbuffer_get_length(out_buffer) == 0) {
this->bev.reset(); // Destructor flushes and frees the bufferevent
} else {
// The callbacks will free it when all the data is sent or the client
// disconnects
auto on_output = +[](struct bufferevent* bev, void*) -> void {
flush_and_free_bufferevent(bev);
};
auto on_error = +[](struct bufferevent* bev, short events, void*) -> void {
if (events & BEV_EVENT_ERROR) {
int err = EVUTIL_SOCKET_ERROR();
channel_exceptions_log.warning(
"Disconnecting channel caused error %d (%s)", err,
evutil_socket_error_to_string(err));
}
if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) {
bufferevent_flush(bev, EV_WRITE, BEV_FINISHED);
bufferevent_free(bev);
}
};
struct bufferevent* bev = this->bev.release();
bufferevent_setcb(bev, nullptr, on_output, on_error, bev);
bufferevent_disable(bev, EV_READ);
}
}
memset(&this->local_addr, 0, sizeof(this->local_addr));
memset(&this->remote_addr, 0, sizeof(this->remote_addr));
this->virtual_network_id = false;
this->crypt_in.reset();
this->crypt_out.reset();
}
Channel::Message Channel::recv() {
struct evbuffer* buf = bufferevent_get_input(this->bev.get());
size_t header_size = (this->version == Version::BB_V4) ? 8 : 4;
PSOCommandHeader header;
if (evbuffer_copyout(buf, &header, header_size) < static_cast<ssize_t>(header_size)) {
throw out_of_range("no command available");
}
if (this->crypt_in.get()) {
this->crypt_in->decrypt(&header, header_size, false);
}
size_t command_logical_size = header.size(version);
// If encryption is enabled, BB pads commands to 8-byte boundaries, and this
// is not reflected in the size field. This logic does not occur if encryption
// is not yet enabled.
size_t command_physical_size = (this->crypt_in.get() && (version == Version::BB_V4))
? ((command_logical_size + 7) & ~7)
: command_logical_size;
if (evbuffer_get_length(buf) < command_physical_size) {
throw out_of_range("no command available");
}
// If we get here, then there is a full command in the buffer. Some encryption
// algorithms' advancement depends on the decrypted data, so we have to
// actually decrypt the header again (with advance=true) to keep them in a
// consistent state.
string header_data(header_size, '\0');
if (evbuffer_remove(buf, header_data.data(), header_data.size()) < static_cast<ssize_t>(header_data.size())) {
throw logic_error("enough bytes available, but could not remove them");
}
if (this->crypt_in.get()) {
this->crypt_in->decrypt(header_data.data(), header_data.size());
}
string command_data(command_physical_size - header_size, '\0');
if (evbuffer_remove(buf, command_data.data(), command_data.size()) < static_cast<ssize_t>(command_data.size())) {
throw logic_error("enough bytes available, but could not remove them");
}
if (this->crypt_in.get()) {
// Some versions of PSO DC can send commands whose sizes are not a multiple
// of 4, but the server is expected to always use a multiple of 4 bytes when
// decrypting (the extra cipher bytes are lost). To emulate this behavior,
// we have to round up the size for DC commands here.
size_t orig_size = command_data.size();
command_data.resize((orig_size + 3) & (~3), 0);
this->crypt_in->decrypt(command_data.data(), command_data.size());
command_data.resize(orig_size);
}
command_data.resize(command_logical_size - header_size);
if (command_data_log.should_log(phosg::LogLevel::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);
}
if (version == Version::BB_V4) {
command_data_log.info(
"Received from %s (version=BB command=%04hX flag=%08" PRIX32 ")",
this->name.c_str(),
header.command(this->version),
header.flag(this->version));
} else {
command_data_log.info(
"Received from %s (version=%s command=%02hX flag=%02" PRIX32 ")",
this->name.c_str(),
phosg::name_for_enum(this->version),
header.command(this->version),
header.flag(this->version));
}
vector<struct iovec> iovs;
iovs.emplace_back(iovec{.iov_base = header_data.data(), .iov_len = header_data.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);
if (use_terminal_colors && this->terminal_recv_color != phosg::TerminalFormat::NORMAL) {
phosg::print_color_escape(stderr, phosg::TerminalFormat::NORMAL, phosg::TerminalFormat::END);
}
}
return {
.command = header.command(this->version),
.flag = header.flag(this->version),
.data = std::move(command_data),
};
terminal_recv_color(terminal_recv_color) {
}
void Channel::send(uint16_t cmd, uint32_t flag, bool silent) {
this->send(cmd, flag, nullptr, 0, silent);
}
void Channel::send(uint16_t cmd, uint32_t flag, const std::vector<std::pair<const void*, size_t>> blocks, bool silent) {
void Channel::send(
uint16_t cmd, uint32_t flag, const std::vector<std::pair<const void*, size_t>> blocks, bool silent) {
if (!this->connected()) {
channel_exceptions_log.warning("Attempted to send command on closed channel; dropping data");
channel_exceptions_log.warning_f("Attempted to send command on closed channel; dropping data");
return;
}
@@ -272,10 +58,7 @@ void Channel::send(uint16_t cmd, uint32_t flag, const std::vector<std::pair<cons
case Version::GC_EP3:
case Version::XB_V3: {
PSOCommandHeaderDCV3 header;
if (this->crypt_out.get() &&
(this->version != Version::DC_NTE) &&
(this->version != Version::DC_11_2000) &&
(this->version != Version::DC_V1)) {
if (this->crypt_out.get() && !is_v1(this->version)) {
send_data_size = (sizeof(header) + size + 3) & ~3;
} else {
send_data_size = (sizeof(header) + size);
@@ -305,13 +88,11 @@ void Channel::send(uint16_t cmd, uint32_t flag, const std::vector<std::pair<cons
break;
}
case Version::BB_V4: {
// BB has an annoying behavior here: command lengths must be multiples of
// 4, but the actual data length must be a multiple of 8. If the size
// field is not divisible by 8, 4 extra bytes are sent anyway. This
// behavior only applies when encryption is enabled - any commands sent
// before encryption is enabled have no size restrictions (except they
// must include a full header and must fit in the client's receive
// buffer), and no implicit extra bytes are sent.
// BB has an annoying behavior here: command lengths must be multiples of 4, but the actual data length must be a
// multiple of 8. If the size field is not divisible by 8, 4 extra bytes are sent anyway. This behavior only
// applies when encryption is enabled - any commands sent before encryption is enabled have no size restrictions
// (except they must include a full header and must fit in the client's receive buffer), and no implicit extra
// bytes are sent.
PSOCommandHeaderBB header;
if (this->crypt_out.get()) {
send_data_size = (sizeof(header) + size + 7) & ~7;
@@ -330,8 +111,7 @@ void Channel::send(uint16_t cmd, uint32_t flag, const std::vector<std::pair<cons
throw 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
// 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");
}
@@ -342,16 +122,15 @@ void Channel::send(uint16_t cmd, uint32_t flag, const std::vector<std::pair<cons
}
send_data.resize(send_data_size, '\0');
if (!silent && (command_data_log.should_log(phosg::LogLevel::INFO)) && (this->terminal_send_color != phosg::TerminalFormat::END)) {
if (!silent && (command_data_log.should_log(phosg::LogLevel::L_INFO)) && (this->terminal_send_color != phosg::TerminalFormat::END)) {
if (use_terminal_colors && this->terminal_send_color != phosg::TerminalFormat::NORMAL) {
print_color_escape(stderr, phosg::TerminalFormat::FG_YELLOW, phosg::TerminalFormat::BOLD, phosg::TerminalFormat::END);
}
if (version == Version::BB_V4) {
command_data_log.info("Sending to %s (version=BB command=%04hX flag=%08" PRIX32 ")",
this->name.c_str(), cmd, flag);
command_data_log.info_f("Sending to {} (version=BB command={:04X} flag={:08X})", this->name, cmd, flag);
} else {
command_data_log.info("Sending to %s (version=%s command=%02hX flag=%02" PRIX32 ")",
this->name.c_str(), phosg::name_for_enum(version), cmd, flag);
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);
if (use_terminal_colors && this->terminal_send_color != phosg::TerminalFormat::NORMAL) {
@@ -363,8 +142,7 @@ void Channel::send(uint16_t cmd, uint32_t flag, const std::vector<std::pair<cons
this->crypt_out->encrypt(send_data.data(), send_data.size());
}
struct evbuffer* buf = bufferevent_get_output(this->bev.get());
evbuffer_add(buf, send_data.data(), send_data.size());
this->send_raw(std::move(send_data));
}
void Channel::send(uint16_t cmd, uint32_t flag, const void* data, size_t size, bool silent) {
@@ -387,35 +165,227 @@ void Channel::send(const void* data, size_t size, bool silent) {
}
void Channel::send(const string& data, bool silent) {
return this->send(data.data(), data.size(), silent);
this->send(data.data(), data.size(), silent);
}
void Channel::dispatch_on_input(struct bufferevent*, void* ctx) {
Channel* ch = reinterpret_cast<Channel*>(ctx);
// The client can be disconnected during on_command_received, so we have to
// make sure ch->bev is valid every time before calling recv()
while (ch->bev.get()) {
Message msg;
try {
msg = ch->recv();
} catch (const out_of_range&) {
break;
} catch (const exception& e) {
channel_exceptions_log.warning("Error receiving on channel: %s", e.what());
ch->on_error(*ch, BEV_EVENT_ERROR);
break;
asio::awaitable<Channel::Message> Channel::recv() {
size_t header_size = (this->version == Version::BB_V4) ? 8 : 4;
PSOCommandHeader header;
co_await this->recv_raw(&header, header_size);
if (this->crypt_in.get()) {
this->crypt_in->decrypt(&header, header_size);
}
size_t command_logical_size = header.size(version);
if (command_logical_size < header_size) {
throw 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
// logic does not occur if encryption is not yet enabled.
size_t command_physical_size = (this->crypt_in.get() && (version == Version::BB_V4))
? ((command_logical_size + 7) & ~7)
: command_logical_size;
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()) {
// Some versions of PSO DC can send commands whose sizes are not a multiple of 4, but the server is expected to
// always use a multiple of 4 bytes when decrypting (the extra cipher bytes are lost). To emulate this behavior, we
// have to round up the size for DC commands here.
size_t orig_size = command_data.size();
command_data.resize((orig_size + 3) & (~3), 0);
this->crypt_in->decrypt(command_data.data(), command_data.size());
command_data.resize(orig_size);
}
command_data.resize(command_logical_size - header_size);
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);
}
if (ch->on_command_received) {
ch->on_command_received(*ch, msg.command, msg.flag, msg.data);
if (version == Version::BB_V4) {
command_data_log.info_f(
"Received from {} (version=BB command={:04X} flag={:08X})",
this->name,
header.command(this->version),
header.flag(this->version));
} else {
command_data_log.info_f(
"Received from {} (version={} command={:02X} flag={:02X})",
this->name,
phosg::name_for_enum(this->version),
header.command(this->version),
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);
if (use_terminal_colors && this->terminal_recv_color != phosg::TerminalFormat::NORMAL) {
phosg::print_color_escape(stderr, phosg::TerminalFormat::NORMAL, phosg::TerminalFormat::END);
}
}
co_return Message{
.command = header.command(this->version),
.flag = header.flag(this->version),
.data = std::move(command_data),
};
}
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,
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));
asio::co_spawn(*io_context, ret->send_task(), asio::detached);
return ret;
}
SocketChannel::SocketChannel(
std::shared_ptr<asio::io_context> io_context,
std::unique_ptr<asio::ip::tcp::socket>&& sock,
Version version,
Language language,
const string& name,
phosg::TerminalFormat terminal_send_color,
phosg::TerminalFormat terminal_recv_color)
: Channel(version, language, name, terminal_send_color, terminal_recv_color),
sock(std::move(sock)),
local_addr(this->sock->local_endpoint()),
remote_addr(this->sock->remote_endpoint()),
send_buffer_nonempty_signal(io_context->get_executor()) {}
std::string SocketChannel::default_name() const {
return "ip:" + str_for_endpoint(this->remote_addr);
}
bool SocketChannel::connected() const {
return !this->should_disconnect && this->sock && this->sock->is_open();
}
void SocketChannel::disconnect() {
this->should_disconnect = true;
this->send_buffer_nonempty_signal.set();
}
void SocketChannel::send_raw(string&& data) {
if (this->sock && !this->should_disconnect) {
this->outbound_data.emplace_back(std::move(data));
this->send_buffer_nonempty_signal.set();
}
}
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");
}
co_await asio::async_read(*this->sock, asio::buffer(data, size), asio::use_awaitable);
}
asio::awaitable<void> SocketChannel::send_task() {
// Ensure *this doesn't get deleted while the socket is open
auto this_sh = this->shared_from_this();
while (this->sock->is_open()) {
deque<string> to_send;
to_send.swap(this->outbound_data);
if (!to_send.empty()) {
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()));
}
co_await asio::async_write(*this->sock, bufs, asio::use_awaitable);
}
if (this->outbound_data.empty()) {
if (this->should_disconnect) {
this->sock->close();
} else {
this->send_buffer_nonempty_signal.clear();
co_await this->send_buffer_nonempty_signal.wait();
}
}
}
}
void Channel::dispatch_on_error(struct bufferevent*, short events, void* ctx) {
Channel* ch = reinterpret_cast<Channel*>(ctx);
if (ch->on_error) {
ch->on_error(*ch, events);
} else {
ch->disconnect();
PeerChannel::PeerChannel(
std::shared_ptr<asio::io_context> io_context,
Version version,
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),
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");
}
peer1->peer = peer2;
peer2->peer = peer1;
}
std::string PeerChannel::default_name() const {
return std::format("peer:{}->{}", reinterpret_cast<const void*>(this), reinterpret_cast<const void*>(this->peer.lock().get()));
}
bool PeerChannel::connected() const {
return (!this->inbound_data.empty()) || (this->peer.lock() != nullptr);
}
void PeerChannel::disconnect() {
auto peer = this->peer.lock();
if (peer) {
peer->peer.reset();
peer->send_buffer_nonempty_signal.set();
}
this->peer.reset();
this->send_buffer_nonempty_signal.set();
}
void PeerChannel::send_raw(string&& data) {
auto peer = this->peer.lock();
if (peer) {
peer->inbound_data.emplace_back(std::move(data));
peer->send_buffer_nonempty_signal.set();
}
}
asio::awaitable<void> PeerChannel::recv_raw(void* data, size_t size) {
while (size > 0) {
while (this->inbound_data.empty() && this->peer.lock()) {
this->send_buffer_nonempty_signal.clear();
co_await this->send_buffer_nonempty_signal.wait();
}
if (!this->inbound_data.empty()) {
auto& front_block = this->inbound_data.front();
if (size < front_block.size()) {
memcpy(data, front_block.data(), size);
front_block = front_block.substr(size);
size = 0;
} else {
memcpy(data, front_block.data(), front_block.size());
size -= front_block.size();
data = reinterpret_cast<uint8_t*>(data) + front_block.size();
this->inbound_data.pop_front();
}
} else if (!this->peer.lock()) {
throw runtime_error("Channel peer has disconnected");
}
}
}
+130 -61
View File
@@ -1,22 +1,18 @@
#pragma once
#include <netinet/in.h>
#include <asio.hpp>
#include <memory>
#include <string>
#include "AsyncUtils.hh"
#include "PSOEncryption.hh"
#include "PSOProtocol.hh"
#include "Version.hh"
struct Channel {
std::unique_ptr<struct bufferevent, void (*)(struct bufferevent*)> bev;
struct sockaddr_storage local_addr;
struct sockaddr_storage remote_addr;
uint64_t virtual_network_id; // 0 = normal TCP connection
class Channel {
public:
Version version;
uint8_t language;
Language language;
std::shared_ptr<PSOEncryption> crypt_in;
std::shared_ptr<PSOEncryption> crypt_out;
@@ -28,58 +24,45 @@ struct Channel {
uint16_t command;
uint32_t flag;
std::string data;
template <typename T>
const T& check_size_t(size_t min_size, size_t max_size) const {
return ::check_size_t<const T>(this->data.data(), this->data.size(), min_size, max_size);
}
template <typename T>
T& check_size_t(size_t min_size, size_t max_size) {
return ::check_size_t<T>(this->data.data(), this->data.size(), min_size, max_size);
}
template <typename T>
const T& check_size_t(size_t max_size) const {
return ::check_size_t<const T>(this->data.data(), this->data.size(), sizeof(T), max_size);
}
template <typename T>
T& check_size_t(size_t max_size) {
return ::check_size_t<T>(this->data.data(), this->data.size(), sizeof(T), max_size);
}
template <typename T>
const T& check_size_t() const {
return ::check_size_t<const T>(this->data.data(), this->data.size(), sizeof(T), sizeof(T));
}
template <typename T>
T& check_size_t() {
return ::check_size_t<T>(this->data.data(), this->data.size(), sizeof(T), sizeof(T));
}
};
typedef void (*on_command_received_t)(Channel&, uint16_t, uint32_t, std::string&);
typedef void (*on_error_t)(Channel&, short);
virtual ~Channel() = default;
on_command_received_t on_command_received;
on_error_t on_error;
void* context_obj;
virtual std::string default_name() const = 0;
// Creates an unconnected channel
Channel(
Version version,
uint8_t language,
on_command_received_t on_command_received,
on_error_t on_error,
void* context_obj,
const std::string& name,
phosg::TerminalFormat terminal_send_color = phosg::TerminalFormat::END,
phosg::TerminalFormat terminal_recv_color = phosg::TerminalFormat::END);
// Creates a connected channel
Channel(
struct bufferevent* bev,
uint64_t virtual_network_id,
Version version,
uint8_t language,
on_command_received_t on_command_received,
on_error_t on_error,
void* context_obj,
const std::string& name = "",
phosg::TerminalFormat terminal_send_color = phosg::TerminalFormat::END,
phosg::TerminalFormat terminal_recv_color = phosg::TerminalFormat::END);
Channel(const Channel& other) = delete;
Channel(Channel&& other) = delete;
Channel& operator=(const Channel& other) = delete;
Channel& operator=(Channel&& other) = delete;
// Returns whether the channel is connected or not.
virtual bool connected() const = 0;
void replace_with(
Channel&& other,
on_command_received_t on_command_received,
on_error_t on_error,
void* context_obj,
const std::string& name = "");
void set_bufferevent(struct bufferevent* bev, uint64_t virtual_network_id);
inline bool connected() const {
return this->bev.get() != nullptr;
}
void disconnect();
// Receives a message. Throws std::out_of_range if no messages are available.
Message recv();
// Disconnects the channel. Any pending data will still be sent before the underlying transport (e.g. socket) is
// closed, but further send calls will do nothing.
virtual void disconnect() = 0;
// Sends a message with an automatically-constructed header.
void send(uint16_t cmd, uint32_t flag = 0, bool silent = false);
@@ -92,12 +75,98 @@ struct Channel {
this->send(cmd, flag, &data, sizeof(data), silent);
}
// Sends a message with a pre-existing header (as the first few bytes in the
// data)
// Sends a message with a pre-existing header (as the first few bytes in the data)
void send(const void* data, size_t size, bool silent = false);
void send(const std::string& data, bool silent = false);
private:
static void dispatch_on_input(struct bufferevent*, void* ctx);
static void dispatch_on_error(struct bufferevent*, short events, void* ctx);
// Receives a message. Throws std::out_of_range if no messages are available.
asio::awaitable<Message> recv();
protected:
Channel(
Version version,
Language language,
const std::string& name,
phosg::TerminalFormat terminal_send_color = phosg::TerminalFormat::END,
phosg::TerminalFormat terminal_recv_color = phosg::TerminalFormat::END);
Channel(const Channel& other) = delete;
Channel(Channel&& other) = delete;
Channel& operator=(const Channel& other) = delete;
Channel& operator=(Channel&& other) = delete;
// Sends raw data on the underlying transport. If the channel is already disconnected, silently drops the data.
virtual void send_raw(std::string&& data) = 0;
// Receives raw data on the underlying transport. Raises when the channel is disconnected.
virtual asio::awaitable<void> recv_raw(void* data, size_t size) = 0;
};
// Standard channel type, used for most PSO clients. Represents an open TCP socket.
class SocketChannel : public Channel, public std::enable_shared_from_this<SocketChannel> {
public:
std::unique_ptr<asio::ip::tcp::socket> sock;
asio::ip::tcp::endpoint local_addr;
asio::ip::tcp::endpoint remote_addr;
// SocketChannel has a static constructor because it has an internal task, which is necessary to support flushing
// before disconnection (for example) and also to make send_raw not a coroutine, which keeps the rest of the code
// cleaner.
static std::shared_ptr<SocketChannel> create(std::shared_ptr<asio::io_context> io_context,
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);
virtual std::string default_name() const;
virtual bool connected() const;
virtual void disconnect();
virtual void send_raw(std::string&& data);
virtual asio::awaitable<void> recv_raw(void* data, size_t size);
private:
SocketChannel(
std::shared_ptr<asio::io_context> io_context,
std::unique_ptr<asio::ip::tcp::socket>&& sock,
Version version,
Language language,
const std::string& name,
phosg::TerminalFormat terminal_send_color,
phosg::TerminalFormat terminal_recv_color);
std::deque<std::string> outbound_data;
bool should_disconnect = false;
AsyncEvent send_buffer_nonempty_signal;
asio::awaitable<void> send_task();
};
// In-process peer channel, used for replay testing.
class PeerChannel : public Channel {
public:
std::weak_ptr<PeerChannel> peer;
PeerChannel(
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);
static void link_peers(std::shared_ptr<PeerChannel> peer1, std::shared_ptr<PeerChannel> peer2);
virtual std::string default_name() const;
virtual bool connected() const;
virtual void disconnect();
virtual void send_raw(std::string&& data);
virtual asio::awaitable<void> recv_raw(void* data, size_t size);
private:
AsyncEvent send_buffer_nonempty_signal;
std::deque<std::string> inbound_data;
};
+1745 -1456
View File
File diff suppressed because it is too large Load Diff
+3 -3
View File
@@ -2,13 +2,13 @@
#include <stdint.h>
#include <asio.hpp>
#include <memory>
#include <string>
#include "Client.hh"
#include "Lobby.hh"
#include "ProxyServer.hh"
#include "ProxySession.hh"
#include "ServerState.hh"
void on_chat_command(std::shared_ptr<Client> c, const std::string& text, bool check_permissions);
void on_chat_command(std::shared_ptr<ProxyServer::LinkedSession> ses, const std::string& text, bool check_permissions);
asio::awaitable<void> on_chat_command(std::shared_ptr<Client> c, const std::string& text, bool check_permissions);
+7 -7
View File
@@ -28,10 +28,10 @@ const vector<ChoiceSearchCategory> CHOICE_SEARCH_CATEGORIES({
if (choice_id == 0x0000) {
return true;
}
uint32_t target_level = target_c->character()->disp.stats.level + 1;
uint32_t target_level = target_c->character_file()->disp.stats.level + 1;
switch (choice_id) {
case 0x0001:
return (labs(static_cast<int32_t>(target_level - searcher_c->character()->disp.stats.level)) <= 5);
return (labs(static_cast<int32_t>(target_level - searcher_c->character_file()->disp.stats.level)) <= 5);
case 0x0002:
return (target_level <= 10);
case 0x0003:
@@ -80,13 +80,13 @@ const vector<ChoiceSearchCategory> CHOICE_SEARCH_CATEGORIES({
case 0x0000:
return true;
case 0x0010:
return target_c->character()->disp.visual.class_flags & 0x20;
return target_c->character_file()->disp.visual.class_flags & 0x20;
case 0x0011:
return target_c->character()->disp.visual.class_flags & 0x40;
return target_c->character_file()->disp.visual.class_flags & 0x40;
case 0x0012:
return target_c->character()->disp.visual.class_flags & 0x80;
return target_c->character_file()->disp.visual.class_flags & 0x80;
default:
return ((choice_id - 1) == target_c->character()->disp.visual.char_class);
return ((choice_id - 1) == target_c->character_file()->disp.visual.char_class);
}
},
},
@@ -143,7 +143,7 @@ const vector<ChoiceSearchCategory> CHOICE_SEARCH_CATEGORIES({
{0x0006, "Challenge"},
},
.client_matches = +[](shared_ptr<Client>, shared_ptr<Client> target_c, uint16_t choice_id) -> bool {
uint16_t target_choice_id = target_c->character()->choice_search_config.get_setting(0x0204);
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);
},
},
+4 -5
View File
@@ -31,17 +31,16 @@ struct ChoiceSearchConfigT {
operator ChoiceSearchConfigT<!BE>() const {
ChoiceSearchConfigT<!BE> ret;
ret.disabled = this->disabled.load();
ret.disabled = this->disabled;
for (size_t z = 0; z < this->entries.size(); z++) {
auto& ret_e = ret.entries[z];
const auto& this_e = this->entries[z];
ret_e.parent_choice_id = this_e.parent_choice_id.load();
ret_e.choice_id = this_e.choice_id.load();
ret_e.parent_choice_id = this_e.parent_choice_id;
ret_e.choice_id = this_e.choice_id;
}
return ret;
}
} __packed__;
} __attribute__((packed));
using ChoiceSearchConfig = ChoiceSearchConfigT<false>;
using ChoiceSearchConfigBE = ChoiceSearchConfigT<true>;
check_struct_size(ChoiceSearchConfig, 0x18);
+600 -584
View File
File diff suppressed because it is too large Load Diff
+196 -250
View File
@@ -1,11 +1,10 @@
#pragma once
#include <netinet/in.h>
#include <memory>
#include <stdexcept>
#include "Account.hh"
#include "AsyncUtils.hh"
#include "Channel.hh"
#include "CommandFormats.hh"
#include "Episode3/BattleRecord.hh"
@@ -15,6 +14,7 @@
#include "PSOEncryption.hh"
#include "PSOProtocol.hh"
#include "PatchFileIndex.hh"
#include "ProxySession.hh"
#include "Quest.hh"
#include "QuestScript.hh"
#include "TeamIndex.hh"
@@ -22,80 +22,76 @@
extern const uint64_t CLIENT_CONFIG_MAGIC;
class Server;
class GameServer;
struct Lobby;
class Parsed6x70Data;
struct GetPlayerInfoResult {
// Exactly one of the following two 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
};
class Client : public std::enable_shared_from_this<Client> {
public:
enum class Flag : uint64_t {
// clang-format off
// This mask specifies which flags are sent to the client
// TODO: It'd be nice to use a pattern here (e.g. all server-side flags are
// in the high bits) but that would require re-recording or manually
// rewriting all the tests
CLIENT_SIDE_MASK = 0xEF3CFFFF7C0BFFFB,
// Version-related flags
CHECKED_FOR_DC_V1_PROTOTYPE = 0x0000000000000002,
NO_D6_AFTER_LOBBY = 0x0000000000000100,
NO_D6 = 0x0000000000000200,
FORCE_ENGLISH_LANGUAGE_BB = 0x0000000000000400,
CHECKED_FOR_DC_V1_PROTOTYPE = 0x0000000000000001,
NO_D6_AFTER_LOBBY = 0x0000000000000002,
NO_D6 = 0x0000000000000004,
FORCE_ENGLISH_LANGUAGE_BB = 0x0000000000000008,
// Flags describing the behavior for send_function_call
HAS_SEND_FUNCTION_CALL = 0x0000000000001000,
ENCRYPTED_SEND_FUNCTION_CALL = 0x0000000000002000,
SEND_FUNCTION_CALL_ACTUALLY_RUNS_CODE = 0x0000000000004000,
SEND_FUNCTION_CALL_NO_CACHE_PATCH = 0x0000000000008000,
CAN_RECEIVE_ENABLE_B2_QUEST = 0x0000000000020000,
AWAITING_ENABLE_B2_QUEST = 0x0000000000040000, // Server-side only
HAS_SEND_FUNCTION_CALL = 0x0000000000000010,
ENCRYPTED_SEND_FUNCTION_CALL = 0x0000000000000020,
SEND_FUNCTION_CALL_ACTUALLY_RUNS_CODE = 0x0000000000000040,
SEND_FUNCTION_CALL_NO_CACHE_PATCH = 0x0000000000000080,
CAN_RECEIVE_ENABLE_B2_QUEST = 0x0000000000000100,
AWAITING_ENABLE_B2_QUEST = 0x0000000000000200,
// State flags
LOADING = 0x0000000000100000, // Server-side only
LOADING_QUEST = 0x0000000000200000, // Server-side only
LOADING_RUNNING_JOINABLE_QUEST = 0x0000000000400000, // Server-side only
LOADING_TOURNAMENT = 0x0000000000800000, // Server-side only
IN_INFORMATION_MENU = 0x0000000001000000, // Server-side only
AT_WELCOME_MESSAGE = 0x0000000002000000, // Server-side only
SAVE_ENABLED = 0x0000000004000000,
HAS_EP3_CARD_DEFS = 0x0000000008000000,
HAS_EP3_MEDIA_UPDATES = 0x0000000010000000,
USE_OVERRIDE_RANDOM_SEED = 0x0000000020000000,
HAS_GUILD_CARD_NUMBER = 0x0000000040000000,
HAS_AUTO_PATCHES = 0x0000004000000000,
AT_BANK_COUNTER = 0x0000000080000000, // Server-side only
SHOULD_SEND_ARTIFICIAL_ITEM_STATE = 0x0001000000000000, // Server-side only
SHOULD_SEND_ARTIFICIAL_ENEMY_AND_SET_STATE = 0x0040000000000000, // Server-side only
SHOULD_SEND_ARTIFICIAL_OBJECT_STATE = 0x0080000000000000, // Server-side only
SHOULD_SEND_ARTIFICIAL_FLAG_STATE = 0x0002000000000000, // Server-side only
SHOULD_SEND_ARTIFICIAL_PLAYER_STATES = 0x0200000000000000, // Server-side only
SHOULD_SEND_ENABLE_SAVE = 0x0004000000000000,
SWITCH_ASSIST_ENABLED = 0x0000000100000000,
IS_CLIENT_CUSTOMIZATION = 0x0100000000000000,
EP3_ALLOW_6xBC = 0x1000000000000000, // Server-side only
LOADING = 0x0000000000000400,
LOADING_QUEST = 0x0000000000000800,
LOADING_RUNNING_JOINABLE_QUEST = 0x0000000000001000,
LOADING_TOURNAMENT = 0x0000000000002000,
IN_INFORMATION_MENU = 0x0000000000004000,
AT_WELCOME_MESSAGE = 0x0000000000008000,
SAVE_ENABLED = 0x0000000000010000,
HAS_EP3_CARD_DEFS = 0x0000000000020000,
HAS_EP3_MEDIA_UPDATES = 0x0000000000040000,
HAS_AUTO_PATCHES = 0x0000000000080000,
AT_BANK_COUNTER = 0x0000000000100000,
SHOULD_SEND_ARTIFICIAL_ITEM_STATE = 0x0000000000200000,
SHOULD_SEND_ARTIFICIAL_ENEMY_AND_SET_STATE = 0x0000000000400000,
SHOULD_SEND_ARTIFICIAL_OBJECT_STATE = 0x0000000000800000,
SHOULD_SEND_ARTIFICIAL_FLAG_STATE = 0x0000000001000000,
SHOULD_SEND_ARTIFICIAL_PLAYER_STATES = 0x0000000002000000,
SHOULD_SEND_ENABLE_SAVE = 0x0000000004000000,
SWITCH_ASSIST_ENABLED = 0x0000000008000000,
IS_CLIENT_CUSTOMIZATION = 0x0000000010000000,
EP3_ALLOW_6xBC = 0x0000000020000000,
// Cheat mode and option flags
INFINITE_HP_ENABLED = 0x0000000200000000,
INFINITE_TP_ENABLED = 0x0000000400000000,
DEBUG_ENABLED = 0x0000000800000000,
ITEM_DROP_NOTIFICATIONS_1 = 0x0010000000000000,
ITEM_DROP_NOTIFICATIONS_2 = 0x0020000000000000,
INFINITE_HP_ENABLED = 0x0000000040000000,
INFINITE_TP_ENABLED = 0x0000000080000000,
FAST_KILLS_ENABLED = 0x0000000100000000,
ALL_RARES_ENABLED = 0x0000100000000000,
DEBUG_ENABLED = 0x0000000200000000,
ITEM_DROP_NOTIFICATIONS_1 = 0x0000000400000000,
ITEM_DROP_NOTIFICATIONS_2 = 0x0000000800000000,
HAS_ENEMY_DAMAGE_SYNC_PATCH = 0x0000001000000000, // Must be same as in EnemyDamageSync*.s
// Proxy option flags
PROXY_SAVE_FILES = 0x0000001000000000,
PROXY_CHAT_COMMANDS_ENABLED = 0x0000002000000000,
PROXY_SAVE_FILES = 0x0000002000000000,
PROXY_CHAT_COMMANDS_ENABLED = 0x0000004000000000,
PROXY_PLAYER_NOTIFICATIONS_ENABLED = 0x0000008000000000,
PROXY_SUPPRESS_CLIENT_PINGS = 0x0000010000000000,
PROXY_SUPPRESS_REMOTE_LOGIN = 0x0000020000000000,
PROXY_ZERO_REMOTE_GUILD_CARD = 0x0000040000000000,
PROXY_EP3_INFINITE_MESETA_ENABLED = 0x0000080000000000,
PROXY_EP3_INFINITE_TIME_ENABLED = 0x0000100000000000,
PROXY_RED_NAME_ENABLED = 0x0000200000000000,
PROXY_BLANK_NAME_ENABLED = 0x0000400000000000,
PROXY_BLOCK_FUNCTION_CALLS = 0x0000800000000000,
PROXY_EP3_UNMASK_WHISPERS = 0x0008000000000000,
PROXY_VIRTUAL_CLIENT = 0x0400000000000000,
PROXY_EP3_INFINITE_MESETA_ENABLED = 0x0000010000000000,
PROXY_EP3_INFINITE_TIME_ENABLED = 0x0000020000000000,
PROXY_BLOCK_FUNCTION_CALLS = 0x0000040000000000,
PROXY_EP3_UNMASK_WHISPERS = 0x0000080000000000,
// clang-format on
};
enum class ItemDropNotificationMode {
@@ -107,123 +103,73 @@ public:
static constexpr uint64_t DEFAULT_FLAGS = static_cast<uint64_t>(Flag::PROXY_CHAT_COMMANDS_ENABLED);
struct Config {
uint64_t enabled_flags = DEFAULT_FLAGS; // Client::Flag enum
uint32_t specific_version = 0;
int32_t override_random_seed = 0;
uint8_t override_section_id = 0xFF; // FF = no override
uint8_t override_lobby_event = 0xFF; // FF = no override
uint8_t override_lobby_number = 0x80; // 80 = no override
uint32_t proxy_destination_address = 0;
uint16_t proxy_destination_port = 0;
Config() = default;
bool operator==(const Config& other) const = default;
bool operator!=(const Config& other) const = default;
bool should_update_vs(const Config& other) const;
[[nodiscard]] static inline bool check_flag(uint64_t enabled_flags, Flag flag) {
return !!(enabled_flags & static_cast<uint64_t>(flag));
}
[[nodiscard]] inline bool check_flag(Flag flag) const {
return this->check_flag(this->enabled_flags, flag);
}
inline void set_flag(Flag flag) {
this->enabled_flags |= static_cast<uint64_t>(flag);
}
inline void clear_flag(Flag flag) {
this->enabled_flags &= (~static_cast<uint64_t>(flag));
}
inline void toggle_flag(Flag flag) {
this->enabled_flags ^= static_cast<uint64_t>(flag);
}
void set_flags_for_version(Version version, int64_t sub_version);
ItemDropNotificationMode get_drop_notification_mode() const;
void set_drop_notification_mode(ItemDropNotificationMode new_mode);
template <size_t Bytes>
void parse_from(const parray<uint8_t, Bytes>& data) {
phosg::StringReader r(data.data(), data.size());
if (r.get_u32l() != CLIENT_CONFIG_MAGIC) {
throw std::invalid_argument("config signature is incorrect");
}
this->specific_version = r.get_u32l();
this->enabled_flags = r.get_u64l();
this->override_random_seed = r.get_u32l();
this->proxy_destination_address = r.get_u32b();
this->proxy_destination_port = r.get_u16l();
this->override_section_id = r.get_u8();
this->override_lobby_event = r.get_u8();
this->override_lobby_number = r.get_u8();
}
template <size_t Bytes>
void serialize_into(parray<uint8_t, Bytes>& data) const {
phosg::StringWriter w;
w.put_u32l(CLIENT_CONFIG_MAGIC);
w.put_u32l(this->specific_version);
w.put_u64l(this->enabled_flags & static_cast<uint64_t>(Flag::CLIENT_SIDE_MASK));
w.put_u32l(this->override_random_seed);
w.put_u32b(this->proxy_destination_address);
w.put_u16l(this->proxy_destination_port);
w.put_u8(this->override_section_id);
w.put_u8(this->override_lobby_event);
w.put_u8(this->override_lobby_number);
const auto& s = w.str();
for (size_t z = 0; z < s.size(); z++) {
data[z] = s[z];
}
data.clear_after(s.size(), 0xFF);
}
};
std::weak_ptr<Server> server;
std::weak_ptr<GameServer> server;
uint64_t id;
phosg::PrefixedLogger log;
// Account information (not all of these are used; depends on game version)
std::string username;
std::string password;
std::string email_address;
uint64_t hardware_id = 0;
int32_t sub_version = 0;
uint8_t bb_client_code = 0;
uint8_t bb_connection_phase = 0xFF;
ssize_t bb_character_index = -1; // -1 = not set
ssize_t bb_bank_character_index = -1; // -1 = shared bank
uint32_t bb_security_token = 0;
parray<uint8_t, 0x28> bb_client_config;
std::string login_character_name;
std::string serial_number;
std::string access_key;
std::string serial_number2;
std::string access_key2;
std::string v1_serial_number;
std::string v1_access_key;
XBNetworkLocation xb_netloc;
parray<le_uint32_t, 3> xb_unknown_a1a;
uint64_t xb_user_id = 0;
uint32_t xb_unknown_a1b = 0;
std::shared_ptr<Login> login;
std::shared_ptr<ProxySession> proxy_session;
// Patch server state (only used for PC_PATCH and BB_PATCH versions)
std::vector<PatchFileChecksumRequest> patch_file_checksum_requests;
// Network
Channel channel;
struct sockaddr_storage next_connection_addr;
std::shared_ptr<Channel> channel;
std::shared_ptr<PSOBBMultiKeyDetectorEncryption> bb_detector_crypt;
ServerBehavior server_behavior;
bool should_disconnect;
bool should_send_to_lobby_server;
bool should_send_to_proxy_server;
uint16_t listener_port = 0;
std::unordered_map<std::string, std::function<void()>> disconnect_hooks;
std::shared_ptr<XBNetworkLocation> xb_netloc;
parray<le_uint32_t, 3> xb_9E_unknown_a1a;
uint8_t bb_connection_phase;
uint64_t ping_start_time;
uint64_t ping_start_time = 0;
// Lobby/positioning
Config config;
Config synced_config;
// Basic state
uint64_t enabled_flags = DEFAULT_FLAGS; // Client::Flag enum
uint32_t specific_version = 0;
uint8_t override_section_id = 0xFF; // FF = no override
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
std::unique_ptr<Variations> override_variations;
int32_t sub_version;
VectorXZF pos;
uint32_t floor;
VectorXYZF pos;
uint32_t floor = 0x0F;
std::weak_ptr<Lobby> lobby;
uint8_t lobby_client_id;
uint8_t lobby_arrow_color;
int64_t preferred_lobby_id; // <0 = no preference
uint8_t lobby_client_id = 0;
uint8_t lobby_arrow_color = 0;
int64_t preferred_lobby_id = -1; // <0 = no preference
std::unique_ptr<struct event, void (*)(struct event*)> save_game_data_event;
std::unique_ptr<struct event, void (*)(struct event*)> send_ping_event;
std::unique_ptr<struct event, void (*)(struct event*)> idle_timeout_event;
int16_t card_battle_table_number;
uint16_t card_battle_table_seat_number;
uint16_t card_battle_table_seat_state;
asio::steady_timer save_game_data_timer;
asio::steady_timer send_ping_timer;
asio::steady_timer idle_timeout_timer;
int16_t card_battle_table_number = -1;
uint16_t card_battle_table_seat_number = 0;
uint16_t card_battle_table_seat_state = 0;
std::weak_ptr<Episode3::Tournament::Team> ep3_tournament_team;
std::shared_ptr<const Episode3::BattleRecord> ep3_prev_battle_record;
std::shared_ptr<const Menu> last_menu_sent;
uint32_t last_game_info_requested;
uint32_t last_game_info_requested = 0;
struct JoinCommand {
uint16_t command;
uint32_t flag;
@@ -249,53 +195,66 @@ public:
// 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;
uint32_t telepipe_lobby_id;
uint32_t telepipe_lobby_id = 0;
TelepipeState telepipe_state;
std::shared_ptr<Episode3::PlayerConfig> ep3_config; // Null for non-Ep3
int8_t bb_character_index;
ItemData bb_identify_result;
std::array<std::vector<ItemData>, 3> bb_shop_contents;
// Miscellaneous (used by chat commands)
uint32_t next_exp_value; // next EXP value to give
bool can_chat;
struct PendingCharacterExport {
std::shared_ptr<const Account> dest_account;
ssize_t character_index = -1;
std::shared_ptr<const BBLicense> dest_bb_license; // Only used for $bbchar; null for $savechar
};
std::unique_ptr<PendingCharacterExport> pending_character_export;
std::deque<std::function<void(uint32_t, uint32_t)>> function_call_response_queue;
// Miscellaneous (used by chat commands / quest opcodes)
uint8_t schtserv_response_register = 0;
uint32_t next_exp_value = 0;
bool can_chat = true;
// NOTE: If you add any new optional promises here, make sure to also add them to cancel_pending_promises.
// NOTE: Entries in this queue can be nullptr; that represents a B2 command sent by the remote server during a proxy
// session. We can't just omit those from the queue entirely, because if we did, we could end up sending the wrong B3
// response back.
std::deque<std::shared_ptr<AsyncPromise<C_ExecuteCodeResult_B3>>> function_call_response_queue;
std::shared_ptr<AsyncPromise<GetPlayerInfoResult>> character_data_ready_promise;
std::shared_ptr<AsyncPromise<void>> enable_save_promise;
// File loading state
uint32_t dol_base_addr;
std::shared_ptr<DOLFileIndex::File> loading_dol_file;
std::unordered_map<std::string, std::shared_ptr<const std::string>> sending_files;
Client(
std::shared_ptr<Server> server,
struct bufferevent* bev,
uint64_t virtual_network_id,
Version version,
ServerBehavior server_behavior);
Client(std::shared_ptr<GameServer> server, std::shared_ptr<Channel> channel, ServerBehavior server_behavior);
~Client();
void update_channel_name();
void reschedule_save_game_data_event();
void reschedule_ping_and_timeout_events();
void reschedule_save_game_data_timer();
void reschedule_ping_and_timeout_timers();
inline Version version() const {
return this->channel.version;
return this->channel->version;
}
inline uint8_t language() const {
return this->channel.language;
inline Language language() const {
return this->channel->language;
}
[[nodiscard]] static inline bool check_flag(uint64_t enabled_flags, Flag flag) {
return !!(enabled_flags & static_cast<uint64_t>(flag));
}
[[nodiscard]] inline bool check_flag(Flag flag) const {
return this->check_flag(this->enabled_flags, flag);
}
inline void set_flag(Flag flag) {
this->enabled_flags |= static_cast<uint64_t>(flag);
}
inline void clear_flag(Flag flag) {
this->enabled_flags &= (~static_cast<uint64_t>(flag));
}
inline void toggle_flag(Flag flag) {
this->enabled_flags ^= static_cast<uint64_t>(flag);
}
void set_flags_for_version(Version version, int64_t sub_version);
ItemDropNotificationMode get_drop_notification_mode() const;
void set_drop_notification_mode(ItemDropNotificationMode new_mode);
void convert_account_to_temporary_if_nte();
void sync_config();
std::shared_ptr<ServerState> require_server_state() const;
std::shared_ptr<Lobby> require_lobby() const;
@@ -305,37 +264,58 @@ public:
std::shared_ptr<const IntegralExpression> expr,
std::shared_ptr<const Lobby> game,
uint8_t event,
uint8_t difficulty,
Difficulty difficulty,
size_t num_players,
bool v1_present) const;
bool can_see_quest(
std::shared_ptr<const Quest> q,
std::shared_ptr<const Lobby> game,
uint8_t event,
uint8_t difficulty,
Difficulty difficulty,
size_t num_players,
bool v1_present) const;
bool can_play_quest(
std::shared_ptr<const Quest> q,
std::shared_ptr<const Lobby> game,
uint8_t event,
uint8_t difficulty,
Difficulty difficulty,
size_t num_players,
bool v1_present) const;
bool can_use_chat_commands() const;
static void dispatch_save_game_data(evutil_socket_t, short, void* ctx);
void save_game_data();
static void dispatch_send_ping(evutil_socket_t, short, void* ctx);
void send_ping();
static void dispatch_idle_timeout(evutil_socket_t, short, void* ctx);
void idle_timeout();
void suspend_timeouts();
void set_login(std::shared_ptr<Login> login);
void import_blocked_senders(const parray<le_uint32_t, 30>& blocked_senders);
static std::string system_filename(const std::string& bb_username);
std::string system_filename() const;
std::shared_ptr<PSOBBBaseSystemFile> system_file(bool allow_load = true);
std::shared_ptr<const PSOBBBaseSystemFile> system_file(bool throw_if_missing = true) const;
void save_system_file() const;
static std::string guild_card_filename(const std::string& bb_username);
std::string guild_card_filename() const;
std::shared_ptr<PSOBBGuildCardFile> guild_card_file(bool allow_load = true);
std::shared_ptr<const PSOBBGuildCardFile> guild_card_file(bool allow_load = true) const;
void save_guild_card_file() const;
static std::string character_filename(const std::string& bb_username, ssize_t index);
static std::string backup_character_filename(uint32_t account_id, size_t index, bool is_ep3);
std::string character_filename() const;
std::shared_ptr<PSOBBCharacterFile> character_file(bool allow_load = true, bool allow_overlay = true);
std::shared_ptr<const PSOBBCharacterFile> character_file(bool throw_if_missing = true, bool allow_overlay = true) const;
static void save_character_file(
const std::string& filename,
std::shared_ptr<const PSOBBBaseSystemFile> sys,
std::shared_ptr<const PSOBBCharacterFile> character);
static void save_ep3_character_file(const std::string& filename, const PSOGCEp3CharacterFile::Character& character);
void save_character_file();
void create_character_file(
uint32_t guild_card_number,
Language language,
const PlayerDispDataBBPreview& preview,
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);
inline void delete_overlay() {
@@ -345,73 +325,39 @@ public:
return this->overlay_character_data.get() != nullptr;
}
void import_blocked_senders(const parray<le_uint32_t, 30>& blocked_senders);
static std::string bank_filename(const std::string& bb_username, ssize_t index);
std::string bank_filename() const;
std::shared_ptr<PlayerBank> bank_file(bool allow_load = true);
std::shared_ptr<const PlayerBank> bank_file(bool throw_if_missing = true) const;
static void save_bank_file(const std::string& filename, const PlayerBank& bank);
void save_bank_file() const;
void change_bank(ssize_t bb_character_index); // -1 = use shared bank
std::shared_ptr<PSOBBBaseSystemFile> system_file(bool allow_load = true);
std::shared_ptr<PSOBBCharacterFile> character(bool allow_load = true, bool allow_overlay = true);
std::shared_ptr<PSOBBGuildCardFile> guild_card_file(bool allow_load = true);
std::shared_ptr<const PSOBBBaseSystemFile> system_file(bool allow_load = true) const;
std::shared_ptr<const PSOBBCharacterFile> character(bool allow_load = true, bool allow_overlay = true) const;
std::shared_ptr<const PSOBBGuildCardFile> guild_card_file(bool allow_load = true) const;
void create_character_file(
uint32_t guild_card_number,
uint8_t language,
const PlayerDispDataBBPreview& preview,
std::shared_ptr<const LevelTable> level_table);
std::string system_filename() const;
static std::string character_filename(const std::string& bb_username, int8_t index);
static std::string backup_character_filename(uint32_t account_id, size_t index, bool is_ep3);
std::string character_filename(int8_t index = -1) const;
std::string guild_card_filename() const;
std::string shared_bank_filename() const;
std::string legacy_player_filename() const;
std::string legacy_account_filename() const;
std::string legacy_player_filename() const;
void save_all();
void save_system_file() const;
static void save_character_file(
const std::string& filename,
std::shared_ptr<const PSOBBBaseSystemFile> sys,
std::shared_ptr<const PSOBBCharacterFile> character);
static void save_ep3_character_file(
const std::string& filename,
const PSOGCEp3CharacterFile::Character& character);
// Note: This function is not const because it updates the player's play time.
void save_character_file();
void save_guild_card_file() const;
void load_backup_character(uint32_t account_id, size_t index);
std::shared_ptr<PSOGCEp3CharacterFile::Character> load_ep3_backup_character(uint32_t account_id, size_t index);
void save_and_unload_character();
void unload_character(bool save);
PlayerBank200& current_bank();
const PlayerBank200& current_bank() const;
std::shared_ptr<PSOBBCharacterFile> current_bank_character();
bool use_shared_bank(); // Returns true if the bank exists; false if it was created
void use_character_bank(int8_t bb_character_index);
void use_default_bank();
void print_inventory() const;
void print_bank() const;
void print_inventory(FILE* stream) const;
void print_bank(FILE* stream) const;
void cancel_pending_promises();
private:
// The overlay character data is used in battle and challenge modes, when
// character data is temporarily replaced in-game. In other play modes and in
// lobbies, overlay_character_data is null.
// The overlay character data is used in battle and challenge modes, when character data is temporarily replaced
// in-game. In other play modes and in lobbies, overlay_character_data is null.
std::shared_ptr<PSOBBBaseSystemFile> system_data;
std::shared_ptr<PSOBBCharacterFile> overlay_character_data;
std::shared_ptr<PSOBBCharacterFile> character_data;
std::shared_ptr<PSOBBGuildCardFile> guild_card_data;
std::shared_ptr<PlayerBank200> external_bank;
std::shared_ptr<PSOBBCharacterFile> external_bank_character;
int8_t external_bank_character_index;
uint64_t last_play_time_update;
void save_and_clear_external_bank();
std::shared_ptr<PlayerBank> bank_data;
uint64_t last_play_time_update = 0;
void load_all_files();
void update_character_data_after_load(std::shared_ptr<PSOBBCharacterFile> character_data);
void update_bank_data_after_load(std::shared_ptr<PlayerBank> bank_data);
};
+1983 -2253
View File
File diff suppressed because it is too large Load Diff
+48 -18
View File
@@ -5,6 +5,10 @@
#include "Text.hh"
constexpr double radians_for_fixed_point_angle(uint16_t angle) {
return static_cast<double>(angle * 2 * M_PI) / 0x10000;
}
struct VectorXZF {
le_float x = 0.0;
le_float z = 0.0;
@@ -33,9 +37,23 @@ struct VectorXZF {
inline double norm2() const {
return ((this->x * this->x) + (this->z * this->z));
}
inline double dist(const VectorXZF& other) const {
return sqrt(this->dist2(other));
}
inline double dist2(const VectorXZF& other) const {
double x = this->x - other.x;
double z = this->z - other.z;
return ((x * x) + (z * z));
}
inline VectorXZF rotate_y(double angle) const {
double s = sin(angle);
double c = cos(angle);
return VectorXZF{this->x * c - this->z * s, this->x * s + this->z * c};
}
inline std::string str() const {
return phosg::string_printf("[VectorXZF x=%g z=%g]", this->x.load(), this->z.load());
return std::format("[VectorXZF x={:g} z={:g}]", this->x, this->z);
}
} __packed_ws__(VectorXZF, 0x08);
@@ -73,8 +91,24 @@ struct VectorXYZF {
return ((this->x * this->x) + (this->y * this->y) + (this->z * this->z));
}
inline VectorXYZF rotate_x(double angle) const {
double s = sin(angle);
double c = cos(angle);
return VectorXYZF{this->x, this->y * c - this->z * s, this->y * s + this->z * c};
}
inline VectorXYZF rotate_y(double angle) const {
double s = sin(angle);
double c = cos(angle);
return VectorXYZF{this->x * c + this->z * s, this->y, -this->x * s + this->z * c};
}
inline VectorXYZF rotate_z(double angle) const {
double s = sin(angle);
double c = cos(angle);
return VectorXYZF{this->x * c - this->y * s, this->x * s + this->y * c, this->z};
}
inline std::string str() const {
return phosg::string_printf("[VectorXYZF x=%g y=%g z=%g]", this->x.load(), this->y.load(), this->z.load());
return std::format("[VectorXYZF x={:g} y={:g} z={:g}]", this->x, this->y, this->z);
}
} __packed_ws__(VectorXYZF, 0x0C);
@@ -97,7 +131,7 @@ struct ArrayRefT {
/* 00 */ U32T<BE> count;
/* 04 */ U32T<BE> offset;
/* 08 */
} __packed__;
} __attribute__((packed));
using ArrayRef = ArrayRefT<false>;
using ArrayRefBE = ArrayRefT<true>;
check_struct_size(ArrayRef, 8);
@@ -106,24 +140,20 @@ check_struct_size(ArrayRefBE, 8);
template <bool BE>
struct RELFileFooterT {
static constexpr bool IsBE = BE;
// Relocations is a list of words (le_uint16_t on DC/PC/XB/BB, be_uint16_t on
// GC) containing the number of doublewords (uint32_t) to skip for each
// relocation. The relocation pointer starts immediately after the
// checksum_size field in the header, and advances by the value of one
// relocation word (times 4) before each relocation. At each relocated
// doubleword, the address of the first byte of the code (after checksum_size)
// is added to the existing value.
// For example, if the code segment contains the following data (where R
// specifies doublewords to relocate):
// Relocations is a list of words (le_uint16_t on DC/PC/XB/BB, be_uint16_t on GC) containing the number of
// doublewords (uint32_t) to skip for each relocation. The relocation pointer starts at the beginning of the file
// data, and advances by the value of one relocation word (times 4) before each relocation. At each relocated
// doubleword, the address of the first byte of the file is added to the existing value.
//
// For example, if the file data contains the following data (where R specifies doublewords to relocate):
// RR RR RR RR ?? ?? ?? ?? ?? ?? ?? ?? RR RR RR RR
// RR RR RR RR ?? ?? ?? ?? RR RR RR RR
// then the relocation words should be 0000, 0003, 0001, and 0002.
// If there is a small number of relocations, they may be placed in the unused
// fields of this structure to save space and/or confuse reverse engineers.
// The game never accesses the last 12 bytes of this structure unless
// relocations_offset points there, so those 12 bytes may also be omitted
// entirely in situations (e.g. in the B2 command, without changing code_size,
// so code_size would technically extend beyond the end of the B2 command).
//
// If there is a small number of relocations, they may be placed in the unused fields of this structure to save space
// and/or confuse reverse engineers. The game never accesses the last 12 bytes of this structure unless
// relocations_offset points there, so those 12 bytes may also be omitted entirely in some situations (e.g. in the B2
// command, without changing code_size, so code_size would technically extend beyond the end of the B2 command).
U32T<BE> relocations_offset = 0;
U32T<BE> num_relocations = 0;
parray<U32T<BE>, 2> unused1;
+568 -260
View File
File diff suppressed because it is too large Load Diff
+141 -150
View File
@@ -4,6 +4,7 @@
#include <phosg/Encoding.hh>
#include <phosg/JSON.hh>
#include "EnemyType.hh"
#include "GSLArchive.hh"
#include "PSOEncryption.hh"
#include "StaticGameData.hh"
@@ -15,14 +16,24 @@ public:
class Table {
public:
Table() = delete;
Table(const phosg::JSON& json, Episode episode);
Table(std::shared_ptr<const Table> prev_table, const phosg::JSON& json, Episode episode);
Table(const phosg::StringReader& r, bool big_endian, bool is_v3, Episode episode);
bool operator==(const Table& other) const = default;
bool operator!=(const Table& other) const = default;
template <typename IntT>
struct Range {
IntT min;
IntT max;
} __packed__;
IntT min = 0;
IntT max = 0;
bool operator==(const Range& other) const = default;
bool operator!=(const Range& other) const = default;
inline bool empty() const {
return ((this->min | this->max) == 0);
}
} __attribute__((packed));
Episode episode;
parray<uint8_t, 0x0C> base_weapon_type_prob_table;
@@ -31,9 +42,10 @@ public:
parray<parray<uint8_t, 4>, 9> grind_prob_table;
parray<uint8_t, 0x05> armor_shield_type_index_prob_table;
parray<uint8_t, 0x05> armor_slot_count_prob_table;
parray<Range<uint16_t>, 0x64> enemy_meseta_ranges;
parray<uint8_t, 0x64> enemy_type_drop_probs;
parray<uint8_t, 0x64> enemy_item_classes;
// Note: PSO originally uses arrays indexed by rt_index here, but we index enemies by the EnemyType enum instead
std::unordered_map<EnemyType, Range<uint16_t>> enemy_type_meseta_ranges;
std::unordered_map<EnemyType, uint8_t> enemy_type_drop_probs;
std::unordered_map<EnemyType, uint8_t> enemy_type_item_classes;
parray<Range<uint16_t>, 0x0A> box_meseta_ranges;
bool has_rare_bonus_value_prob_table;
parray<parray<uint16_t, 6>, 0x17> bonus_value_prob_table;
@@ -48,8 +60,9 @@ public:
parray<uint8_t, 0x0A> unit_max_stars_table;
parray<parray<uint8_t, 10>, 7> box_item_class_prob_table;
phosg::JSON json() const;
phosg::JSON json(std::shared_ptr<const Table> prev_table) const;
void print(FILE* stream) const;
void print_diff(FILE* stream, const Table& other) const;
private:
template <bool BE>
@@ -57,54 +70,43 @@ public:
template <bool BE>
struct OffsetsT {
// 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 slot, 5% chance of 2 slots, 1% chance
// of 3 slots, and no chance of 4 slots. The values in index probability
// tables do not have to add up to 100; the game sums all of them and
// chooses a random number less than that maximum.
// 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
// slot, 5% chance of 2 slots, 1% chance of 3 slots, and no chance of 4 slots. The values in index probability
// tables do not have to add up to 100; the game sums all of them and chooses a random number less than that
// maximum.
// The area (floor) number is used in many places as well. Unlike the normal
// area numbers, which start with Pioneer 2, the area numbers in this
// structure start with Forest 1, and boss areas are treated as the first
// area of the next section (so De Rol Le has Mines 1 drops, for example).
// Final boss areas are treated as the last non-boss area (so Dark Falz
// boxes are like Ruins 3 boxes). We refer to these adjusted area numbers as
// (area - 1).
// The area (floor) number is used in many places as well. Unlike the normal area numbers, which start with
// Pioneer 2, the area numbers in this structure start with Forest 1, and boss areas are treated as the first
// area of the next section (so De Rol Le has Mines 1 drops, for example). Final boss areas are treated as the
// last non-boss area (so Dark Falz boxes are like Ruins 3 boxes). We refer to these adjusted area numbers as
// (area - 1), or area_norm.
// This index probability table determines the types of non-rare weapons.
// The indexes in this table correspond to the non-rare weapon types 01
// through 0C (Saber through Wand).
// This index probability table determines the types of non-rare weapons. The indexes in this table correspond to
// the non-rare weapon types 01 through 0C (Saber through Wand).
// V2/V3: -> parray<uint8_t, 0x0C>
/* 00 */ U32T<BE> base_weapon_type_prob_table_offset;
// This table specifies the base subtype for each weapon type. Negative
// values here mean that the weapon cannot be found in the first N areas (so
// -2, for example, means that the weapon never appears in Forest 1 or 2 at
// all). Nonnegative values here mean the subtype can be found in all areas,
// and specify the base subtype (usually in the range [0, 4]). The subtype
// of weapon that actually appears depends on this value and a value from
// the following table.
// This table specifies the base subtype for each weapon type. Negative values here mean that the weapon cannot
// be found in the first N areas (so -2, for example, means that the weapon never appears in Forest 1 or 2 at
// all). Nonnegative values here mean the subtype can be found in all areas, and specify the base subtype
// (usually in the range [0, 4]). The subtype of weapon that actually appears depends on this value and a value
// from the following table.
// V2/V3: -> parray<int8_t, 0x0C>
/* 04 */ U32T<BE> subtype_base_table_offset;
// This table specifies how many areas each weapon subtype appears in. For
// example, if Sword (subtype 02, which is index 1 in this table and the
// table above) has a subtype base of -2 and a subtype area length of 4,
// then Sword items can be found when area - 1 is 2, 3, 4, or 5 (Cave 1
// through Mine 1), and Gigush (the next sword subtype) can be found in Mine
// 1 through Ruins 3.
// This table specifies how many areas each weapon subtype appears in. For example, if Sword (subtype 02, which
// is index 1 in this table and the table above) has a subtype base of -2 and a subtype area length of 4, then
// Sword items can be found when area - 1 is 2, 3, 4, or 5 (Cave 1 through Mine 1), and Gigush (the next sword
// subtype) can be found in Mine 1 through Ruins 3.
// V2/V3: -> parray<uint8_t, 0x0C>
/* 08 */ U32T<BE> subtype_area_length_table_offset;
// This index probability table specifies how likely each possible grind
// value is. The table is indexed as [grind][subtype_area_index], where the
// subtype area index is how many areas the player is beyond the first area
// in which the subtype can first be found (clamped to [0, 3]). To continue
// the example above, in Cave 3, subtype_area_index would be 2, since Swords
// can first be found two areas earlier in Cave 1.
// This index probability table specifies how likely each possible grind value is. The table is indexed as
// [grind][subtype_area_index], where the subtype area index is how many areas the player is beyond the first
// area in which the subtype can first be found (clamped to [0, 3]). To continue the example above, in Cave 3,
// subtype_area_index would be 2, since Swords can first be found two areas earlier in Cave 1.
// For example, this table could look like this:
// [64 1E 19 14] // Chance of getting a grind +0
// [00 1E 17 0F] // Chance of getting a grind +1
@@ -114,74 +116,66 @@ public:
// V2/V3: -> parray<parray<uint8_t, 4>, 9>
/* 0C */ U32T<BE> grind_prob_table_offset;
// TODO: Figure out exactly how this table is used. Anchor: 80106D34
// This index probability table specifies how likely each type of armor or shield is. The general formula is:
// data1[2] = max((area_norm + (result from this table) + armor_or_shield_type_bias - 3), 0)
// In this way, (armor_or_shield_type_bias + area_norm - 3) can be thought of as the "base" value for each area,
// and this table specifies how likely the armor/shield is to be "upgraded" from that value.
// V2/V3: -> parray<uint8_t, 0x05>
/* 10 */ U32T<BE> armor_shield_type_index_prob_table_offset;
// This index probability table specifies how common each possible slot
// count is for armor drops.
// This index probability table specifies how common each possible slot count is for armor drops.
// V2/V3: -> parray<uint8_t, 0x05>
/* 14 */ U32T<BE> armor_slot_count_prob_table_offset;
// This array (indexed by enemy_type) specifies the range of meseta values
// that each enemy can drop.
// V2/V3: -> parray<Range<U16T>, 0x64>
/* 18 */ U32T<BE> enemy_meseta_ranges_offset;
// This array (indexed by rt_index) specifies the range of meseta values that each enemy can drop.
// V2/V3: -> parray<Range<U16T>, NUM_RT_INDEXES_V3>
/* 18 */ U32T<BE> enemy_rt_index_meseta_ranges_offset;
// Each byte in this table (indexed by enemy_type) represents the percent
// chance that the enemy drops anything at all. (This check is done before
// the rare drop check, so the chance of getting a rare item from an enemy
// is essentially this probability multiplied by the rare drop rate.)
// V2/V3: -> parray<uint8_t, 0x64>
/* 1C */ U32T<BE> enemy_type_drop_probs_offset;
// Each byte in this table (indexed by rt_index) represents the percent chance that the enemy drops anything at
// all. (This check is done before the rare drop check, so the chance of getting a rare item from an enemy is
// essentially this probability multiplied by the rare drop rate.)
// V2/V3: -> parray<uint8_t, NUM_RT_INDEXES_V3>
/* 1C */ U32T<BE> enemy_rt_index_drop_probs_offset;
// Each byte in this table (indexed by enemy_type) represents the class of
// item that the enemy can drop. The values are:
// 00 = weapon
// 01 = armor
// 02 = shield
// 03 = unit
// 04 = tool
// 05 = meseta
// Anything else = no item
// V2/V3: -> parray<uint8_t, 0x64>
/* 20 */ U32T<BE> enemy_item_classes_offset;
// Each byte in this table (indexed by rt_index) represents the class of item that can drop. The values are:
// 00 = weapon
// 01 = armor
// 02 = shield
// 03 = unit
// 04 = tool
// 05 = meseta
// Anything else = no item
// V2/V3: -> parray<uint8_t, NUM_RT_INDEXES_V3>
/* 20 */ U32T<BE> enemy_rt_index_item_classes_offset;
// This table (indexed by area - 1) specifies the ranges of meseta values
// that can drop from boxes.
// This table (indexed by area - 1) specifies the ranges of meseta values that can drop from boxes.
// V2/V3: -> parray<Range<U16T>, 0x0A>
/* 24 */ U32T<BE> box_meseta_ranges_offset;
// This array specifies the chance that a rare weapon will have each
// possible bonus value. This is indexed as [(bonus_value - 10 / 5)][spec],
// so the first row refers the probability of getting a -10% bonus, the next
// row is the chance of getting -5%, etc., all the way up to +100%. For
// non-rare items, spec is determined randomly based on the following field;
// for rare items, spec is always 5.
// This array specifies the chance that a rare weapon will have each possible bonus value. This is indexed as
// [(bonus_value - 10 / 5)][spec], so the first row refers the probability of getting a -10% bonus, the next row
// is the chance of getting -5%, etc., all the way up to +100%. For non-rare items (or all items on v1/v2), spec
// is determined randomly based on the following field; for rare items on v3+, spec is always 5.
// V2: -> parray<parray<uint8_t, 5>, 0x17>
// V3: -> parray<parray<U16T, 6>, 0x17>
/* 28 */ U32T<BE> bonus_value_prob_table_offset;
// This array specifies the value of spec to be used in the above lookup for
// non-rare items. This is NOT an index probability table; this is a direct
// lookup with indexes [bonus_index][area - 1]. A value of 0xFF in any byte
// of this array prevents any weapon from having a bonus in that slot.
// For example, the array might look like this:
// This array specifies the value of spec to be used in the above lookup for non-rare items. This is NOT an index
// probability table; this is a direct lookup with indexes [bonus_index][area - 1]. A value of 0xFF in any byte
// of this array prevents any weapon from having a bonus in that slot. An example table might look like this:
// [00 00 00 01 01 01 01 02 02 02]
// [FF FF FF 00 00 00 01 01 01 01]
// [FF FF FF FF FF FF FF FF FF 00]
// F1 F2 C1 C2 C3 M1 M2 R1 R2 R3 // (Episode 1 areas, for reference)
// In this example, spec is 0, 1, or 2 in all cases where a weapon can have
// a bonus. In Forest 1 and 2 and Cave 1, weapons may have at most one
// bonus; in all other areas except Ruins 3, they can have at most two
// bonuses, and in Ruins 3, they can have up to three bonuses.
// In this example, spec is 0, 1, or 2 in all cases where a weapon can have a bonus. In Forest 1 and 2 and Cave
// 1, weapons may have at most one bonus; in all other areas except Ruins 3, they can have at most two bonuses,
// and in Ruins 3, they can have up to three bonuses.
// V2/V3: // -> parray<parray<uint8_t, 10>, 3>
/* 2C */ U32T<BE> nonrare_bonus_prob_spec_offset;
// This array specifies the chance that a weapon will have each bonus type.
// The table is indexed as [bonus_type][area - 1] for non-rare items; for
// rare items, a random value in the range [0, 9] is used instead of
// (area - 1).
// This array specifies the chance that a weapon will have each bonus type. The table is indexed as
// [bonus_type][area - 1] for non-rare items; for rare items, a random value in the range [0, 9] is used instead
// of (area - 1).
// For example, the table might look like this:
// [46 46 3F 3E 3E 3D 3C 3C 3A 3A] // Chance of getting no bonus
// [14 14 0A 0A 09 02 02 04 05 05] // Chance of getting Native bonus
@@ -193,54 +187,50 @@ public:
// V2/V3: -> parray<parray<uint8_t, 10>, 6>
/* 30 */ U32T<BE> bonus_type_prob_table_offset;
// This array (indexed by area - 1) specifies a multiplier of used in
// special ability determination. It seems this uses the star values from
// ItemPMT, but not yet clear exactly in what way.
// TODO: Figure out exactly what this does. Anchor: 80106FEC
// This array (indexed by area - 1) specifies a parameter used in weapon special generation. If the sampled value
// from this table is 0, no special is generated. Otherwise, a random floating-point value W in the range [0,
// special_mult] is generated and truncated to an integer. If this value is greater than 3, no special is
// generated; otherwise, a random special worth (W + 1) stars is chosen. It seems Sega only intended special_mult
// to be in the range [0, 4], but values greater than 4 will work, and will simply increase the probability of
// getting no special.
// V2/V3: -> parray<uint8_t, 0x0A>
/* 34 */ U32T<BE> special_mult_offset;
// This array (indexed by area - 1) specifies the probability that any
// non-rare weapon will have a special ability.
// This array (indexed by area - 1) specifies the probability that a non-rare weapon will have a special ability.
// V2/V3: -> parray<uint8_t, 0x0A>
/* 38 */ U32T<BE> special_percent_offset;
// This index probability table is indexed by [tool_class][area - 1]. The
// tool class refers to an entry in ItemPMT, which links it to the actual
// item code.
// This index probability table is indexed by [tool_class][area - 1]. The tool class refers to an entry in
// ItemPMT, which links it to the actual item code.
// V2/V3: -> parray<parray<U16T, 0x0A>, 0x1C>
/* 3C */ U32T<BE> tool_class_prob_table_offset;
// This index probability table determines how likely each technique is to
// appear. The table is indexed as [technique_num][area - 1].
// This index probability table determines how likely each technique is to appear. The table is indexed as
// [technique_num][area - 1].
// V2/V3: -> parray<parray<uint8_t, 0x0A>, 0x13>
/* 40 */ U32T<BE> technique_index_prob_table_offset;
// This table specifies the ranges for technique disk levels. The table is
// indexed as [technique_num][area - 1]. If either min or max in the range
// is 0xFF, or if max < min, technique disks are not dropped for that
// technique and area pair.
// This table specifies the ranges for technique disk levels. The table is indexed as [technique_num][area - 1].
// If either min or max in the range is 0xFF, or if max < min, technique disks are not dropped for that technique
// and area pair.
// V2/V3: -> parray<parray<Range<uint8_t>, 0x0A>, 0x13>
/* 44 */ U32T<BE> technique_level_ranges_offset;
// See comments on armor_shield_type_index_prob_table_offset for how this is used.
/* 48 */ uint8_t armor_or_shield_type_bias;
/* 49 */ parray<uint8_t, 3> unused1;
// These values specify the maximum number of stars any generated unit can
// have in each area. The values here are not inclusive; that is, a value
// of 7 means that only units with 1-6 stars can drop in that area. The
// game uniformly chooses a random number of stars in the acceptable
// range, then uniformly chooses a random unit with that many stars.
// These values specify the maximum number of stars any generated unit can have in each area. The values here are
// not inclusive; that is, a value of 7 means that only units with 1-6 stars can drop in that area. The game
// uniformly chooses a random number of stars in the acceptable range, then uniformly chooses a random unit with
// that many stars.
// V2/V3: -> parray<uint8_t, 0x0A>
/* 4C */ U32T<BE> unit_max_stars_offset;
// This index probability table determines which type of items drop from
// boxes. The table is indexed as [item_class][area - 1], with item_class
// as the result value (that is, in the example below, the game looks at a
// single column and sums the values going down, then the chosen item
// class is one of the row indexes based on the weight values in the
// column.) The resulting item_class value has the same meaning as in
// enemy_item_classes above.
// This index probability table determines which type of items drop from boxes. The table is indexed as
// [item_class][area - 1], with item_class as the result value (that is, in the example below, the game looks at
// a single column and sums the values going down, then the chosen item class is one of the row indexes based on
// the weight values in the column.) The resulting value has the same meaning as in enemy_rt_index_item_classes.
// For example, this array might look like the following:
// [07 07 08 08 06 07 08 09 09 0A] // Chances per area of a weapon drop
// [02 02 02 02 03 02 02 02 03 03] // Chances per area of an armor drop
@@ -254,21 +244,30 @@ public:
/* 50 */ U32T<BE> box_item_class_prob_table_offset;
// There are several unused fields here.
} __packed__;
} __attribute__((packed));
using Offsets = OffsetsT<false>;
using OffsetsBE = OffsetsT<true>;
check_struct_size(Offsets, 0x54);
check_struct_size(OffsetsBE, 0x54);
};
std::shared_ptr<const Table> get_table(Episode episode, GameMode mode, uint8_t difficulty, uint8_t secid) const;
bool operator==(const CommonItemSet& other) const = default;
bool operator!=(const CommonItemSet& other) const = default;
std::shared_ptr<const Table> get_table(
Episode episode, GameMode mode, Difficulty difficulty, uint8_t section_id) const;
std::shared_ptr<const Table> get_prev_table(
Episode episode, GameMode mode, Difficulty difficulty, uint8_t section_id) const;
phosg::JSON json() const;
void print(FILE* stream) const;
void print_diff(FILE* stream, const CommonItemSet& other) const;
protected:
CommonItemSet() = default;
static uint16_t key_for_table(Episode episode, GameMode mode, uint8_t difficulty, uint8_t secid);
static uint16_t key_for_table(Episode episode, GameMode mode, Difficulty difficulty, uint8_t section_id);
static std::string json_key_for_table(Episode episode, GameMode mode, Difficulty difficulty, uint8_t section_id);
std::unordered_map<uint16_t, std::shared_ptr<Table>> tables;
};
@@ -288,8 +287,8 @@ 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.
// 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];
@@ -311,22 +310,22 @@ struct ProbabilityTable {
return this->items[--this->count];
}
void shuffle(std::shared_ptr<PSOLFGEncryption> opt_rand_crypt) {
void shuffle(std::shared_ptr<RandomGenerator> rand_crypt) {
for (size_t z = 1; z < this->count; z++) {
size_t other_z = random_from_optional_crypt(opt_rand_crypt) % (z + 1);
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<PSOLFGEncryption> opt_rand_crypt) const {
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[random_from_optional_crypt(opt_rand_crypt) % this->count];
return this->items[rand_crypt->next() % this->count];
}
}
};
@@ -337,7 +336,7 @@ public:
struct WeightTableEntry {
ValueT value;
WeightT weight;
} __packed__;
} __attribute__((packed));
using WeightTableEntry8 = WeightTableEntry<uint8_t>;
using WeightTableEntry32 = WeightTableEntry<be_uint32_t>;
@@ -357,11 +356,9 @@ protected:
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 {
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));
spec.offset + index * spec.entries_per_table * sizeof(T), spec.entries_per_table * sizeof(T));
return std::make_pair(entries, spec.entries_per_table);
}
};
@@ -474,17 +471,14 @@ private:
} __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.
// 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
// 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.
// 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%
@@ -508,9 +502,8 @@ private:
// 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.
// 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%
@@ -534,9 +527,8 @@ private:
// 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).
// 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%
@@ -560,11 +552,10 @@ private:
// 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.
// 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
+96 -148
View File
@@ -63,14 +63,11 @@ struct WindowIndex {
return match_iter - match_offset;
};
// The data structure we want is a binary-searchable set of all strings
// starting at all possible offsets within the sliding window, and we need
// to be able to search lexicographically but insert and delete by offset.
// A std::map<std::string, size_t> would accomplish this, but would be
// horrendously inefficient: we'd have to copy 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.
// The data structure we want is a binary-searchable set of all strings starting at all possible offsets within the
// sliding window, and we need to be able to search lexicographically but insert and delete by offset. A
// std::map<std::string, size_t> would accomplish this, but would be horrendously inefficient: we'd have to copy
// 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 end_a = a + max_length;
@@ -87,11 +84,9 @@ struct WindowIndex {
};
pair<size_t, size_t> get_best_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
// 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;
@@ -123,9 +118,7 @@ struct LZSSInterleavedWriter {
uint8_t next_control_bit;
uint8_t buf[0x19];
LZSSInterleavedWriter()
: buf_offset(1),
next_control_bit(1) {
LZSSInterleavedWriter() : buf_offset(1), next_control_bit(1) {
this->buf[0] = 0;
}
@@ -166,9 +159,7 @@ struct LZSSInterleavedWriter {
class ControlStreamReader {
public:
ControlStreamReader(phosg::StringReader& r)
: r(r),
bits(0x0000) {}
ControlStreamReader(phosg::StringReader& r) : r(r), bits(0x0000) {}
bool read() {
if (!(this->bits & 0x0100)) {
@@ -285,8 +276,7 @@ string prs_compress_optimal(const void* in_data_v, size_t in_size, ProgressCallb
long_window_thread.join();
extended_window_thread.join();
// For each node, populate the literal value, and the best ways to get to the
// following nodes
// For each node, populate the literal value, and the best ways to get to the following nodes
for (size_t z = 0; z < in_size; z++) {
if ((z & 0xFFF) == 0 && progress_fn) {
progress_fn(CompressPhase::CONSTRUCT_PATHS, z, in_size, 0);
@@ -441,9 +431,8 @@ string prs_compress_optimal(const string& data, ProgressCallback progress_fn) {
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 an extended copy if there is a byte
// with the same value in the window.
// The worst possible encoding we can do is a literal byte when no byte with the same value is within the window, or
// an extended copy if there is a byte with the same value in the window.
WindowIndex<0x1FFF, 1> window(in_data, size);
LZSSInterleavedWriter w;
for (size_t z = 0; z < size; z++) {
@@ -539,9 +528,8 @@ void PRSCompressor::advance() {
match_size++;
}
// If there are multiple matches of the longest length, use the latest one,
// since it's more likely that it can be expressed as a short copy instead
// of a long copy.
// If there are multiple matches of the longest length, use the latest one, since it's more likely that it can be
// expressed as a short copy instead of a long copy.
if (match_size >= (best_match_size + best_match_literals)) {
best_match_offset = match_offset;
best_match_size = match_size;
@@ -558,15 +546,13 @@ void PRSCompressor::advance() {
this->advance_literal();
}
// If there is a suitable match, write a backreference; otherwise, write a
// literal. The backreference should be encoded:
// If there is a match, write a backreference; otherwise, write a literal. The backreference should be encoded:
// - As a short copy if offset in [-0x100, -1] and size in [2, 5]
// - As a long copy if offset in [-0x1FFF, -1] and size in [3, 9]
// - As an extended copy if offset in [-0x1FFF, -1] and size in [10, 0x100]
// Technically an extended copy can be used for sizes 1-9 as well, but if
// size is 1 or 2, writing literals is better (since it uses fewer data
// bytes and control bits), and a long copy can cover sizes 3-9 (and also
// uses fewer data bytes and control bits).
// Technically an extended copy can be used for sizes 1-9 as well, but if size is 1 or 2, writing literals is better
// (since it uses fewer data bytes and control bits), and a long copy can cover sizes 3-9 (and also uses fewer data
// bytes and control bits).
ssize_t backreference_offset = best_match_offset - this->reverse_log.end_offset();
if (best_match_size < 2) {
// The match is too small; a literal would use fewer bits
@@ -576,8 +562,8 @@ void PRSCompressor::advance() {
this->advance_short_copy(backreference_offset, best_match_size);
} else if (best_match_size < 3) {
// We can't use a long copy for size 2, and it's not worth it to use an
// extended copy for this either (as noted above), so write a literal
// We can't use a long copy for size 2, and it's not worth it to use an extended copy for this either (as noted
// above), so write a literal
this->advance_literal();
} else if ((backreference_offset >= -0x1FFF) && (best_match_size <= 9)) {
@@ -655,14 +641,12 @@ string& PRSCompressor::close() {
void PRSCompressor::write_control(bool z) {
if (this->pending_control_bits & 0x0100) {
this->output.pput_u8(
this->control_byte_offset, this->pending_control_bits & 0xFF);
this->output.pput_u8(this->control_byte_offset, this->pending_control_bits & 0xFF);
this->control_byte_offset = this->output.size();
this->output.put_u8(0);
this->pending_control_bits = z ? 0x8080 : 0x8000;
} else {
this->pending_control_bits =
(this->pending_control_bits >> 1) | (z ? 0x8080 : 0x8000);
this->pending_control_bits = (this->pending_control_bits >> 1) | (z ? 0x8080 : 0x8000);
}
}
@@ -671,8 +655,7 @@ void PRSCompressor::flush_control() {
while (!(this->pending_control_bits & 0x0100)) {
this->pending_control_bits >>= 1;
}
this->output.pput_u8(
this->control_byte_offset, this->pending_control_bits & 0xFF);
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");
@@ -681,25 +664,17 @@ void PRSCompressor::flush_control() {
}
}
string prs_compress(
const void* vdata,
size_t size,
ssize_t compression_level,
ProgressCallback progress_fn) {
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) {
string prs_compress(const 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) {
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;
@@ -718,14 +693,11 @@ string prs_compress_indexed(
auto m_long = w_long.get_best_match();
auto m_extended = w_extended.get_best_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 468 is the least common multiple of the number of bits for each
// command type. The command type with the highest score is the one we'll
// use, breaking ties by choosing the shorter command type. Note that the
// size of any copy type can be zero if no match was found; if no matches
// were found at all, then we can always write a literal.
// 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
// 468 is the least common multiple of the number of bits for each command type. The command type with the highest
// score is the one we'll use, breaking ties by choosing the shorter command type. Note that the size of any copy
// type can be zero if no match was found; if no matches were found at all, then we can always write a literal.
size_t score_literal = 52;
size_t score_short = m_short.second * 39;
size_t score_long = m_long.second * 26;
@@ -838,41 +810,30 @@ string prs_compress_indexed(const string& data, ProgressCallback progress_fn) {
PRSDecompressResult prs_decompress_with_meta(
const void* data, size_t size, size_t max_output_size, bool allow_unterminated) {
// PRS is an LZ77-based compression algorithm. Compressed data is split into
// two streams: a control stream and a data stream. The control stream is read
// one bit at a time, and the data stream is read one byte at a time. The
// streams are interleaved such that the decompressor never has to move
// backward in the input stream - when the decompressor needs a control bit
// and there are no unused bits from the previous byte of the control stream,
// it reads a byte from the input and treats it as the next 8 control bits.
// PRS is an LZ77-based compression algorithm. Compressed data is split into two streams: a control stream and a data
// stream. The control stream is read one bit at a time, and the data stream is read one byte at a time. The streams
// are interleaved such that the decompressor never has to move backward in the input stream - when the decompressor
// needs a control bit and there are no unused bits from the previous byte of the control stream, it reads a byte
// from the input and treats it as the next 8 control bits.
// There are 3 distinct commands in PRS, labeled here with their control bits:
// 1 - Literal byte. The decompressor copies one byte from the input data
// stream to the output.
// 00 - Short backreference. The decompressor reads two control bits and adds
// 2 to this value to determine the number of bytes to copy, then reads
// one byte from the data stream to determine how far back in the output
// to copy from. This byte is treated as an 8-bit negative number - so
// 0xF7, for example, means to start copying data from 9 bytes before the
// end of the output. The range must start before the end of the output,
// but the end of the range may be beyond the end of the output. In this
// case, the bytes between the beginning of the range and original end of
// the output are simply repeated.
// 01 - Long backreference. The decompressor reads two bytes from the data and
// byteswaps the resulting 16-bit value (that is, the low byte is read
// first). The start offset (again, as a negative number) is the top 13
// bits of this value; the size is the low 3 bits of this value, plus 2.
// If the size bits are all zero, an additional byte is read from the
// data stream and 1 is added to it to determine the backreference size
// (we call this an extended backreference). Therefore, the maximum
// backreference size is 256 bytes.
// Decompression ends when either there are no more input bytes to read, or
// when a long backreference is read with all zeroes in its offset field. The
// original implementation stops decompression successfully when any attempt
// to read from the input encounters the end of the stream, but newserv's
// implementation only allows this at the end of an opcode - if end-of-stream
// is encountered partway through an opcode, we throw instead, because it's
// likely the input has been truncated or is malformed in some way.
// 1 - Literal byte. The decompressor copies one byte from the input data stream to the output.
// 00 - Short backreference. The decompressor reads two control bits and adds 2 to this value to determine the number
// of bytes to copy, then reads one byte from the data stream to determine how far back in the output to copy
// from. This byte is treated as an 8-bit negative number - so 0xF7, for example, means to start copying data
// from 9 bytes before the end of the output. The range must start before the end of the output, but the end of
// the range may be beyond the end of the output. In this case, the bytes between the beginning of the range and
// original end of the output are simply repeated.
// 01 - Long backreference. The decompressor reads two bytes from the data and byteswaps the resulting 16-bit value
// (that is, the low byte is read first). The start offset (again, as a negative number) is the top 13 bits of
// this value; the size is the low 3 bits of this value, plus 2. If the size bits are all zero, an additional
// byte is read from the data stream and 1 is added to it to determine the backreference size (we call this an
// extended backreference). Therefore, the maximum backreference size is 256 bytes.
// Decompression ends when either there are no more input bytes to read, or when a long backreference is read with
// all zeroes in its offset field. The original implementation stops decompression successfully when any attempt to
// read from the input encounters the end of the stream, but newserv's implementation only allows this at the end of
// an opcode - if end-of-stream is encountered partway through an opcode, we throw instead, because it's likely the
// input has been truncated or is malformed in some way.
phosg::StringWriter w;
phosg::StringReader r(data, size);
@@ -894,10 +855,9 @@ PRSDecompressResult prs_decompress_with_meta(
ssize_t offset;
size_t count;
// Control 01 = long backreference
if (cr.read()) {
// The bits stored in the data stream are AAAAABBBCCCCCCCC, which we
// rearrange into offset = CCCCCCCCAAAAA and size = BBB.
// Control 01 = long backreference
// The bits from the data stream are AAAAABBBCCCCCCCC, which we rearrange as offset=CCCCCCCCAAAAA and size=BBB.
uint16_t a = r.get_u8();
a |= (r.get_u8() << 8);
offset = (a >> 3) | (~0x1FFF);
@@ -905,24 +865,21 @@ PRSDecompressResult prs_decompress_with_meta(
if (offset == ~0x1FFF) {
break;
}
// If the size field is zero, it's an extended backreference (size comes
// from another byte in the data stream)
// If the size field is zero, it's an extended backreference (size comes from another byte in the data stream)
count = (a & 7) ? ((a & 7) + 2) : (r.get_u8() + 1);
// Control 00 = short backreference
} else {
// Count comes from 2 bits in the control stream instead of from the
// data stream (and 2 is added). Importantly, the control stream bits
// are read first - this may involve reading another control stream
// byte, which happens before the offset is read from the data stream.
// Control 00 = short backreference
// Count comes from 2 bits in the control stream instead of from the data stream (and 2 is added). Importantly,
// the control stream bits are read first - this may involve reading another control stream byte, which happens
// before the offset is read from the data stream.
count = cr.read() << 1;
count = (count | cr.read()) + 2;
offset = r.get_u8() | (~0xFF);
}
// Copy bytes from the referenced location in the output. Importantly,
// copy only one byte at a time, in order to support ranges that cover the
// current end of the output.
// Copy bytes from the referenced location in the output. Importantly, copy only one byte at a time, in order to
// 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");
@@ -1018,7 +975,7 @@ void prs_disassemble(FILE* stream, const void* data, size_t size) {
uint8_t buffered_bits = cr.buffered_bits();
if (cr.read()) {
uint8_t literal_value = r.get_u8();
fprintf(stream, "[%zX] %hhu> 1 %02hhX literal %02hhX\n",
phosg::fwrite_fmt(stream, "[{:X}] {}> 1 {:02X} literal {:02X}\n",
output_bytes, buffered_bits, literal_value, literal_value);
output_bytes++;
@@ -1030,19 +987,19 @@ void prs_disassemble(FILE* stream, const void* data, size_t size) {
uint16_t a = (a_high << 8) | a_low;
ssize_t offset = (a >> 3) | (~0x1FFF);
if (offset == ~0x1FFF) {
fprintf(stream, "[%zX] end\n", output_bytes);
phosg::fwrite_fmt(stream, "[{:X}] end\n", output_bytes);
break;
}
if (a & 7) {
count = (a & 7) + 2;
read_offset = output_bytes + offset;
fprintf(stream, "[%zX] %hhu> 01 %02hhX %02hhX long copy from %zd (offset=%zX) size=%zX\n",
phosg::fwrite_fmt(stream, "[{:X}] {}> 01 {:02X} {:02X} long copy from {} (offset={:X}) size={:X}\n",
output_bytes, buffered_bits, a_low, a_high, offset, read_offset, count);
} else {
uint8_t count_u8 = r.get_u8();
count = count_u8 + 1;
read_offset = output_bytes + offset;
fprintf(stream, "[%zX] %hhu> 01 %02hhX %02hhX %02hhX extended copy from %zd (offset=%zX) size=%zX\n",
phosg::fwrite_fmt(stream, "[{:X}] {}> 01 {:02X} {:02X} {:02X} extended copy from {} (offset={:X}) size={:X}\n",
output_bytes, buffered_bits, a_low, a_high, count_u8, offset, read_offset, count);
}
@@ -1053,7 +1010,7 @@ void prs_disassemble(FILE* stream, const void* data, size_t size) {
count = ((first_bit ? 2 : 0) | (second_bit ? 1 : 0)) + 2;
ssize_t offset = offset_u8 | (~0xFF);
read_offset = output_bytes + offset;
fprintf(stream, "[%zX] %hhu> 00%c%c %02hhX short copy from %zd (offset=%zX) size=%zX\n",
phosg::fwrite_fmt(stream, "[{:X}] {}> 00{}{} {:02X} short copy from {} (offset={:X}) size={:X}\n",
output_bytes, buffered_bits, first_bit ? '1' : '0', second_bit ? '1' : '0', offset_u8, offset, read_offset, count);
}
@@ -1069,11 +1026,10 @@ void prs_disassemble(FILE* stream, const std::string& data) {
return prs_disassemble(stream, data.data(), data.size());
}
// BC0 is a compression algorithm fairly similar to PRS, but with a simpler set
// of commands. Like PRS, there is a control stream, indicating when to copy a
// literal byte from the input and when to copy from a backreference; unlike
// PRS, there is only one type of backreference. Also, there is no stop opcode;
// the decompressor simply stops when there are no more input bytes to read.
// BC0 is a compression algorithm fairly similar to PRS, but with a simpler set of commands. Like PRS, there is a
// control stream, indicating when to copy a literal byte from the input and when to copy from a backreference; unlike
// PRS, there is only one type of backreference. Also, there is no stop opcode; the decompressor simply stops when
// there are no more input bytes to read.
struct BC0PathNode {
uint16_t memo_offset = 0;
@@ -1112,8 +1068,7 @@ string bc0_compress_optimal(
}
}
// For each node, populate the literal value, and the best ways to get to the
// following nodes
// For each node, populate the literal value, and the best ways to get to the following nodes
for (size_t z = 0; z < in_size; z++) {
if ((z & 0xFFF) == 0 && progress_fn) {
progress_fn(CompressPhase::CONSTRUCT_PATHS, z, in_size, 0);
@@ -1238,11 +1193,9 @@ string bc0_encode(const void* in_data_v, size_t in_size) {
return std::move(w.close());
}
// The BC0 decompression implementation in PSO GC is vulnerable to overflow
// attacks - there is no bounds checking on 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.
// The BC0 decompression implementation in PSO GC is vulnerable to overflow attacks - there is no bounds checking on
// 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) {
return bc0_decompress(data.data(), data.size());
@@ -1252,22 +1205,18 @@ string bc0_decompress(const void* data, size_t size) {
phosg::StringReader r(data, size);
phosg::StringWriter w;
// Unlike PRS, BC0 uses a memo which "rolls over" every 0x1000 bytes. The
// boundaries of these "memo pages" are offset by -0x12 bytes for some reason,
// so the first output byte corresponds to position 0xFEE on the first memo
// page. Backreferences refer to offsets based on the start of memo pages; for
// example, if the current output offset is 0x1234, a backreference with
// offset 0x123 refers to the byte that was written at offset 0x1111 (because
// that byte is at offset 0x111 in the memo, because the memo rolls over every
// 0x1000 bytes and the first memo byte was 0x12 bytes before the beginning of
// the next page). The memo is initially zeroed from 0 to 0xFEE; it seems PSO
// GC doesn't initialize the last 0x12 bytes of the first memo page.
// Unlike PRS, BC0 uses a memo which "rolls over" every 0x1000 bytes. The boundaries of these "memo pages" are offset
// by -0x12 bytes for some reason, so the first output byte corresponds to position 0xFEE on the first memo page.
// Backreferences refer to offsets based on the start of memo pages; for example, if the current output offset is
// 0x1234, a backreference with offset 0x123 refers to the byte that was written at offset 0x1111 (because that byte
// is at offset 0x111 in the memo, because the memo rolls over every 0x1000 bytes and the first memo byte was 0x12
// bytes before the beginning of the next page). The memo is initially zeroed from 0 to 0xFEE; it seems PSO GC
// doesn't initialize the last 0x12 bytes of the first memo page.
parray<uint8_t, 0x1000> memo;
uint16_t memo_offset = 0x0FEE;
// The low byte of this value contains the control stream data; the high bits
// specify which low bits are valid. When the last 1 is shifted out of the
// high byte, we need to read a new control stream byte to get the next set of
// The low byte of this value contains the control stream data; the high bits specify which low bits are valid. When
// the last 1 is shifted out of the high byte, we need to read a new control stream byte to get the next set of
// control bits.
uint16_t control_stream_bits = 0x0000;
@@ -1282,14 +1231,13 @@ string bc0_decompress(const void* data, size_t size) {
}
if ((control_stream_bits & 1) == 0) {
// Control bit 0 means to perform a backreference copy. The offset and
// size are stored in two bytes in the input stream, laid out as follows:
// a1 = 0bBBBBBBBB
// a2 = 0bAAAACCCC
// The offset is the concatenation of bits AAAABBBBBBBB, which refers to
// a position in the memo; the number of bytes to copy is (CCCC + 3). The
// decompressor copies that many bytes from that offset in the memo, and
// writes them to the output and to the current position in the memo.
// Control bit 0 means to perform a backreference copy. The offset and size are stored in two bytes in the input
// stream, laid out as follows:
// a1 = 0bBBBBBBBB
// a2 = 0bAAAACCCC
// The offset is the concatenation of bits AAAABBBBBBBB, which refers to a position in the memo; the number of
// bytes to copy is (CCCC + 3). The decompressor copies that many bytes from that offset in the memo, and writes
// them to the output and to the current position in the memo.
uint8_t a1 = r.get_u8();
if (r.eof()) {
break;
@@ -1305,8 +1253,8 @@ string bc0_decompress(const void* data, size_t size) {
}
} else {
// Control bit 1 means to write a byte directly from the input to the
// output. As above, the byte is also written to the memo.
// Control bit 1 means to write a byte directly from the input to the output. As above, the byte is also written
// to the memo.
uint8_t v = r.get_u8();
w.put_u8(v);
memo[memo_offset] = v;
@@ -1346,11 +1294,11 @@ void bc0_disassemble(FILE* stream, const void* data, size_t size) {
uint8_t a2 = r.get_u8();
size_t count = (a2 & 0x0F) + 3;
// size_t backreference_offset = a1 | ((a2 << 4) & 0xF00);
fprintf(stream, "[%zX] backreference %02zX\n", output_bytes, count);
phosg::fwrite_fmt(stream, "[{:X}] backreference {:02X}\n", output_bytes, count);
output_bytes += count;
} else {
fprintf(stream, "[%zX] literal %02hhX\n", output_bytes, r.get_u8());
phosg::fwrite_fmt(stream, "[{:X}] literal {:02X}\n", output_bytes, r.get_u8());
output_bytes++;
}
}
+35 -58
View File
@@ -22,39 +22,32 @@ const char* phosg::name_for_enum<CompressPhase>(CompressPhase v);
typedef std::function<void(CompressPhase phase, size_t input_progress, size_t input_size, size_t output_size)> ProgressCallback;
////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PRS compression
////////////////////////////////////////////////////////////////////////////////
// Use this class if you need to compress from multiple input buffers, or need
// to compress multiple chunks and don't want to copy their contents
// unnecessarily. (For most common use cases, use prs_compress, below, instead.)
// To use this class, instantiate it, then call .add() one or more times, then
// call .close() and use the returned string as the compressed result.
// Use this class if you need to compress from multiple input buffers, or need to compress multiple chunks and don't
// want to copy their contents unnecessarily. (For most common use cases, use prs_compress, below, instead.) To use
// this class, instantiate it, then call .add() one or more times, then call .close() and use the returned string as
// the compressed result.
class PRSCompressor {
public:
// compression_level specifies how aggressively to search for alternate paths:
// -1: Don't perform any compression at all, but produce output that can be
// understood by prs_decompress. The output will be about 9/8 the size
// of the input.
// 0: Greedily search for the longest backreference at every point. Don't
// consider any alternate paths. Generally offers a good balance between
// speed and output size.
// 1: Consider two paths at each point when a backreference is found: using
// the backreference or ignoring it.
// 2+: Consider further chains of paths at each point. Using values 2 or
// greater for compression_level generally yields diminishing returns.
// -1: Don't perform any compression at all, but produce output that can be understood by prs_decompress. The
// output will be about 9/8 the size of the input.
// 0: Greedily search for the longest backreference at every point. Don't consider any alternate paths. Generally
// offers a good balance between speed and output size.
// 1: Consider two paths at each point when a backreference is found: using the backreference or ignoring it.
// 2+: Consider further chains of paths at each point. Using values 2 or greater for compression_level generally
// yields diminishing returns.
explicit PRSCompressor(ssize_t compression_level = 0, ProgressCallback progress_fn = nullptr);
~PRSCompressor() = default;
// Adds more input data to be compressed, which logically comes after all
// previous data provided via add() calls. Cannot be called after close() is
// called.
// Adds more input data to be compressed, which logically comes after all previous data provided via add() calls.
// Cannot be called after close() is called.
void add(const void* data, size_t size);
void add(const std::string& data);
// Ends compression and returns the complete compressed result. It's OK to
// std::move() from the returned string reference.
// Ends compression and returns the complete compressed result. It's OK to std::move() from the returned reference.
std::string& close();
// Returns the total number of bytes passed to add() calls so far.
@@ -149,36 +142,24 @@ private:
phosg::StringWriter output;
};
// These functions use PRSCompressor to compress a buffer of data. This is
// essentially a shortcut for constructing a PRSCompressor, calling .add() on
// it once, then calling .close().
// These functions use PRSCompressor to compress a buffer of data. This is essentially a shortcut for constructing a
// PRSCompressor, calling .add() on it once, then calling .close().
std::string prs_compress(
const void* vdata,
size_t size,
ssize_t compression_level = 0,
ProgressCallback progress_fn = nullptr);
const void* vdata, size_t size, ssize_t compression_level = 0, ProgressCallback progress_fn = nullptr);
std::string prs_compress(
const std::string& data,
ssize_t compression_level = 0,
ProgressCallback progress_fn = nullptr);
const std::string& data, ssize_t compression_level = 0, ProgressCallback progress_fn = nullptr);
// A faster form of prs_compress that doesn't have a tunable compression level.
std::string prs_compress_indexed(
const void* vdata,
size_t size,
ProgressCallback progress_fn = nullptr);
std::string prs_compress_indexed(
const std::string& data,
ProgressCallback progress_fn = nullptr);
std::string prs_compress_indexed(const void* vdata, size_t size, ProgressCallback progress_fn = nullptr);
std::string prs_compress_indexed(const std::string& data, ProgressCallback progress_fn = nullptr);
// Compresses data using PRS to the smallest possible output size. This function
// is slow, but produces results significantly smaller than even Sega's original
// compressor.
// Compresses data using PRS to the smallest possible output size. This function is slow, but produces results
// significantly smaller than even Sega's original compressor.
std::string prs_compress_optimal(const void* vdata, size_t size, ProgressCallback progress_fn = nullptr);
std::string prs_compress_optimal(const std::string& data, ProgressCallback progress_fn = nullptr);
// Compresses data using PRS to the LARGEST possible output size. There is no
// practical use for this function except for amusement.
// Compresses data using PRS to the LARGEST possible output size. There is no practical use for this function except
// for amusement.
std::string prs_compress_pessimal(const void* vdata, size_t size);
// Decompresses PRS-compressed data.
@@ -186,13 +167,14 @@ struct PRSDecompressResult {
std::string data;
size_t input_bytes_used;
};
PRSDecompressResult prs_decompress_with_meta(const void* data, size_t size, size_t max_output_size = 0, bool allow_unterminated = false);
PRSDecompressResult prs_decompress_with_meta(const std::string& data, size_t max_output_size = 0, bool allow_unterminated = false);
PRSDecompressResult prs_decompress_with_meta(
const void* data, size_t size, size_t max_output_size = 0, bool allow_unterminated = false);
PRSDecompressResult prs_decompress_with_meta(
const std::string& data, size_t max_output_size = 0, bool allow_unterminated = false);
std::string prs_decompress(const void* data, size_t size, size_t max_output_size = 0, bool allow_unterminated = false);
std::string prs_decompress(const std::string& data, size_t max_output_size = 0, bool allow_unterminated = false);
// Returns the decompressed size of PRS-compressed data, without actually
// decompressing it.
// Returns the decompressed size of PRS-compressed data, without actually decompressing it.
size_t prs_decompress_size(const void* data, size_t size, size_t max_output_size = 0, bool allow_unterminated = false);
size_t prs_decompress_size(const std::string& data, size_t max_output_size = 0, bool allow_unterminated = false);
@@ -200,21 +182,16 @@ size_t prs_decompress_size(const std::string& data, size_t max_output_size = 0,
void prs_disassemble(FILE* stream, const void* data, size_t size);
void prs_disassemble(FILE* stream, const std::string& data);
////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// BC0 compression
////////////////////////////////////////////////////////////////////////////////
// Compresses data using the BC0 algorithm. Like with PRS, the optimal variant
// is slow, but produces the smallest possible output.
std::string bc0_compress_optimal(
const void* in_data_v,
size_t in_size,
ProgressCallback progress_fn = nullptr);
// Compresses data using the BC0 algorithm. Like with PRS, the optimal variant is slow, but produces the smallest
// possible output.
std::string bc0_compress(const std::string& data, ProgressCallback progress_fn = nullptr);
std::string bc0_compress(const void* in_data_v, size_t in_size, ProgressCallback progress_fn = nullptr);
std::string bc0_compress_optimal(const void* in_data_v, size_t in_size, ProgressCallback progress_fn = nullptr);
// Encodes data in a BC0-compatible format without compression (similar to using
// compression_level=-1 with prs_compress).
// Encodes data in a BC0-compatible format without compression (similar to compression_level=-1 in prs_compress).
std::string bc0_encode(const void* in_data_v, size_t in_size);
// Decompresses BC0-compressed data.
+733 -1115
View File
File diff suppressed because it is too large Load Diff
+8 -12
View File
@@ -5,20 +5,16 @@
#include <string>
#include <unordered_map>
// dc_serial_number_is_valid_slow is Sega's implementation;
// dc_serial_number_is_valid_fast produces identical results but is between 3000
// and 7500 times faster, depending on the compiler's optimization level.
bool dc_serial_number_is_valid_slow(
const std::string& s, uint8_t domain, uint8_t subdomain = 0xFF);
bool dc_serial_number_is_valid_fast(
const std::string& s, uint8_t domain, uint8_t subdomain = 0xFF);
bool dc_serial_number_is_valid_fast(
uint32_t serial_number, uint8_t domain, uint8_t subdomain = 0xFF);
bool decoded_dc_serial_number_is_valid_fast(
uint32_t serial_number, uint8_t domain, uint8_t subdomain = 0xFF);
// dc_serial_number_is_valid_slow is Sega's implementation; dc_serial_number_is_valid_fast produces identical results
// but is between 3000 and 7500 times faster, depending on the compiler's optimization level.
bool dc_serial_number_is_valid_slow(const std::string& s, uint8_t domain, uint8_t subdomain = 0xFF);
bool dc_serial_number_is_valid_fast(const std::string& s, uint8_t domain, uint8_t subdomain = 0xFF);
bool dc_serial_number_is_valid_fast(uint32_t serial_number, uint8_t domain, uint8_t subdomain = 0xFF);
bool decoded_dc_serial_number_is_valid_fast(uint32_t serial_number, uint8_t domain, uint8_t subdomain = 0xFF);
std::string generate_dc_serial_number(uint8_t domain, uint8_t subdomain = 0xFF);
std::unordered_map<uint32_t, std::string> generate_all_dc_serial_numbers(uint8_t domain = 0xFF, uint8_t subdomain = 0xFF);
std::unordered_map<uint32_t, std::string> generate_all_dc_serial_numbers(
uint8_t domain = 0xFF, uint8_t subdomain = 0xFF);
struct DCSerialNumberIterator {
bool started = false;
+23 -68
View File
@@ -1,7 +1,5 @@
#include "DNSServer.hh"
#include <netinet/in.h>
#include <poll.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
@@ -14,48 +12,23 @@
#include "Loggers.hh"
#include "NetworkAddresses.hh"
#include "ServerState.hh"
using namespace std;
DNSServer::DNSServer(
shared_ptr<struct event_base> base,
uint32_t local_connect_address,
uint32_t external_connect_address,
shared_ptr<const IPV4RangeSet> banned_ipv4_ranges)
: base(base),
local_connect_address(local_connect_address),
external_connect_address(external_connect_address),
banned_ipv4_ranges(banned_ipv4_ranges) {}
DNSServer::~DNSServer() {
for (const auto& it : this->fd_to_receive_event) {
close(it.first);
}
}
void DNSServer::listen(const std::string& socket_path) {
this->add_socket(phosg::listen(socket_path, 0, 0));
}
DNSServer::DNSServer(shared_ptr<ServerState> state)
: state(state) {}
void DNSServer::listen(const std::string& addr, int port) {
this->add_socket(phosg::listen(addr, port, 0));
}
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);
asio::ip::udp::endpoint endpoint(asio_addr, port);
auto sock = make_shared<asio::ip::udp::socket>(*this->state->io_context, endpoint);
this->sockets.emplace(sock);
void DNSServer::listen(int port) {
this->add_socket(phosg::listen("", port, 0));
}
void DNSServer::add_socket(int fd) {
unique_ptr<struct event, void (*)(struct event*)> e(
event_new(this->base.get(), fd, EV_READ | EV_PERSIST, &DNSServer::dispatch_on_receive_message, this),
event_free);
event_add(e.get(), nullptr);
this->fd_to_receive_event.emplace(fd, std::move(e));
}
void DNSServer::dispatch_on_receive_message(evutil_socket_t fd,
short events, void* ctx) {
reinterpret_cast<DNSServer*>(ctx)->on_receive_message(fd, events);
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) {
@@ -77,45 +50,27 @@ 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) {
string DNSServer::response_for_query(const string& query, uint32_t resolved_address) {
return DNSServer::response_for_query(query.data(), query.size(), resolved_address);
}
void DNSServer::on_receive_message(int fd, short) {
asio::awaitable<void> DNSServer::dns_server_task(std::shared_ptr<asio::ip::udp::socket> sock) {
for (;;) {
struct sockaddr_storage remote;
socklen_t remote_size = sizeof(sockaddr_in);
memset(&remote, 0, remote_size);
string input(2048, 0);
ssize_t bytes = recvfrom(fd, const_cast<char*>(input.data()), input.size(),
0, reinterpret_cast<sockaddr*>(&remote), &remote_size);
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());
if (bytes < 0) {
if (errno != EAGAIN) {
dns_server_log.error("input error %d", errno);
throw runtime_error("cannot read from udp socket");
}
break;
} else if (bytes == 0) {
break;
} else if (bytes < 0x0C) {
dns_server_log.warning("input query too small");
if (bytes < 0x0C) {
dns_server_log.warning_f("input query too small");
phosg::print_data(stderr, input.data(), bytes);
} else if (!this->banned_ipv4_ranges->check(remote)) {
} else if (!this->state->banned_ipv4_ranges->check(sender_addr)) {
input.resize(bytes);
const sockaddr_in* remote_sin = reinterpret_cast<const sockaddr_in*>(&remote);
uint32_t remote_address = ntohl(remote_sin->sin_addr.s_addr);
uint32_t connect_address = is_local_address(remote_address)
? this->local_connect_address
: this->external_connect_address;
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);
sendto(fd, response.data(), response.size(), 0,
reinterpret_cast<const sockaddr*>(&remote), remote_size);
co_await sock->async_send_to(asio::buffer(response.data(), response.size()), sender_ep, asio::use_awaitable);
}
}
}
+10 -22
View File
@@ -1,7 +1,6 @@
#pragma once
#include <event2/event.h>
#include <asio.hpp>
#include <memory>
#include <set>
#include <string>
@@ -9,36 +8,25 @@
#include "IPV4RangeSet.hh"
struct ServerState;
class DNSServer {
public:
DNSServer(
std::shared_ptr<struct event_base> base,
uint32_t local_connect_address,
uint32_t external_connect_address,
std::shared_ptr<const IPV4RangeSet> banned_ipv4_ranges);
explicit DNSServer(std::shared_ptr<ServerState> state);
DNSServer(const DNSServer&) = delete;
DNSServer(DNSServer&&) = delete;
virtual ~DNSServer();
DNSServer& operator=(const DNSServer&) = delete;
DNSServer& operator=(DNSServer&&) = delete;
virtual ~DNSServer() = default;
inline void set_banned_ipv4_ranges(std::shared_ptr<const IPV4RangeSet> banned_ipv4_ranges) {
this->banned_ipv4_ranges = banned_ipv4_ranges;
}
void listen(const std::string& socket_path);
void listen(const std::string& addr, int port);
void listen(int port);
void add_socket(int fd);
static std::string response_for_query(const void* vdata, size_t size, uint32_t resolved_address);
static std::string response_for_query(const std::string& query, uint32_t resolved_address);
private:
std::shared_ptr<struct event_base> base;
std::unordered_map<int, std::unique_ptr<struct event, void (*)(struct event*)>> fd_to_receive_event;
uint32_t local_connect_address;
uint32_t external_connect_address;
std::shared_ptr<const IPV4RangeSet> banned_ipv4_ranges;
std::shared_ptr<ServerState> state;
std::unordered_set<std::shared_ptr<asio::ip::udp::socket>> sockets;
static void dispatch_on_receive_message(evutil_socket_t fd, short events, void* ctx);
void on_receive_message(int fd, short event);
asio::awaitable<void> dns_server_task(std::shared_ptr<asio::ip::udp::socket> sock);
};
+232 -287
View File
@@ -1,13 +1,7 @@
#include "DownloadSession.hh"
#include <arpa/inet.h>
#include <ctype.h>
#include <errno.h>
#include <event2/buffer.h>
#include <event2/bufferevent.h>
#include <event2/event.h>
#include <event2/listener.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -24,7 +18,6 @@
#include "Loggers.hh"
#include "PSOProtocol.hh"
#include "ProxyCommands.hh"
#include "ReceiveCommands.hh"
#include "ReceiveSubcommands.hh"
#include "SendCommands.hh"
@@ -42,11 +35,12 @@ static string random_name() {
}
DownloadSession::DownloadSession(
std::shared_ptr<struct event_base> base,
const struct sockaddr_storage& remote,
std::shared_ptr<asio::io_context> io_context,
const std::string& remote_host,
uint16_t remote_port,
const std::string& output_dir,
Version version,
uint8_t language,
Language language,
std::shared_ptr<const PSOBBEncryption::KeyFile> bb_key_file,
uint32_t serial_number2,
uint32_t serial_number,
@@ -61,10 +55,15 @@ DownloadSession::DownloadSession(
const std::vector<std::string>& on_request_complete_commands,
bool interactive,
bool show_command_data)
: output_dir(output_dir),
: remote_host(remote_host),
remote_port(remote_port),
output_dir(output_dir),
version(version),
language(language),
show_command_data(show_command_data),
bb_key_file(bb_key_file),
serial_number2(serial_number2),
serial_number(serial_number),
serial_number2(serial_number2),
access_key(access_key),
username(username),
password(password),
@@ -75,33 +74,16 @@ DownloadSession::DownloadSession(
ship_menu_selections(ship_menu_selections),
on_request_complete_commands(on_request_complete_commands),
interactive(interactive),
log(phosg::string_printf("[DownloadSession:%s] ", phosg::name_for_enum(version)), proxy_server_log.min_level),
base(base),
channel(
version,
language,
DownloadSession::dispatch_on_channel_input,
DownloadSession::dispatch_on_channel_error,
this,
phosg::render_sockaddr_storage(remote),
show_command_data ? phosg::TerminalFormat::FG_GREEN : phosg::TerminalFormat::END,
show_command_data ? phosg::TerminalFormat::FG_YELLOW : phosg::TerminalFormat::END),
hardware_id(generate_random_hardware_id(this->channel.version)),
guild_card_number(0),
log(std::format("[DownloadSession:{}] ", phosg::name_for_enum(version)), proxy_server_log.min_level),
io_context(io_context),
hardware_id(generate_random_hardware_id(version)),
prev_cmd_data(0),
client_config(0),
sent_96(false),
should_request_category_list(true),
current_request(0),
current_game_config_index(0),
in_game(false),
bin_complete(false),
dat_complete(false) {
client_config(0) {
if (this->output_dir.empty()) {
this->output_dir = ".";
}
switch (this->channel.version) {
switch (version) {
case Version::DC_V1:
case Version::DC_V2:
if (this->serial_number2 == 0 || this->serial_number == 0 || this->access_key.empty()) {
@@ -132,106 +114,102 @@ DownloadSession::DownloadSession(
throw runtime_error("unsupported version");
}
this->character->inventory.language = this->channel.language;
if (remote.ss_family != AF_INET) {
throw runtime_error("remote is not AF_INET");
}
string netloc_str = phosg::render_sockaddr_storage(remote);
this->log.info("Connecting to %s", netloc_str.c_str());
struct bufferevent* bev = bufferevent_socket_new(
this->base.get(), -1, BEV_OPT_CLOSE_ON_FREE | BEV_OPT_DEFER_CALLBACKS);
if (!bev) {
throw runtime_error(phosg::string_printf("failed to open socket (%d)", EVUTIL_SOCKET_ERROR()));
}
this->channel.set_bufferevent(bev, 0);
if (bufferevent_socket_connect(this->channel.bev.get(),
reinterpret_cast<const sockaddr*>(&remote), sizeof(struct sockaddr_in)) != 0) {
throw runtime_error(phosg::string_printf("failed to connect (%d)", EVUTIL_SOCKET_ERROR()));
}
this->character->inventory.language = language;
}
void DownloadSession::dispatch_on_channel_input(Channel& ch, uint16_t command, uint32_t flag, std::string& data) {
auto* session = reinterpret_cast<DownloadSession*>(ch.context_obj);
session->on_channel_input(command, flag, data);
asio::awaitable<void> DownloadSession::run() {
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));
this->channel = SocketChannel::create(
this->io_context,
std::move(sock),
this->version,
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->log.info_f("Server channel connected");
while (this->channel->connected()) {
auto msg = co_await this->channel->recv();
co_await this->on_message(msg);
}
}
void DownloadSession::send_93_9D_9E(bool extended) {
if (is_v1(this->channel.version)) {
if (is_v1(this->version)) {
C_LoginExtendedV1_DC_93 ret;
ret.player_tag = this->guild_card_number ? 0xFFFF0000 : 0x00010000;
ret.guild_card_number = this->guild_card_number;
ret.hardware_id = this->hardware_id;
ret.sub_version = default_sub_version_for_version(this->channel.version);
ret.sub_version = default_sub_version_for_version(this->version);
ret.is_extended = extended ? 1 : 0;
ret.language = this->channel.language;
ret.serial_number.encode(phosg::string_printf("%08" PRIX32, this->serial_number));
ret.language = this->language;
ret.serial_number.encode(std::format("{:08X}", this->serial_number));
ret.access_key.encode(this->access_key);
ret.serial_number2.encode(phosg::string_printf("%08" PRIX32, this->serial_number2));
ret.name.encode(this->character->disp.name.decode());
this->channel.send(0x93, 0x01, &ret, extended ? sizeof(ret) : sizeof(C_LoginV1_DC_93));
ret.serial_number2.encode(std::format("{:08X}", this->serial_number2));
ret.login_character_name.encode(this->character->disp.name.decode());
this->channel->send(0x93, 0x01, &ret, extended ? sizeof(ret) : sizeof(C_LoginV1_DC_93));
} else if (is_v2(this->channel.version)) {
} else if (is_v2(this->version)) {
C_LoginExtended_PC_9D ret;
ret.player_tag = this->guild_card_number ? 0xFFFF0000 : 0x00010000;
ret.guild_card_number = this->guild_card_number;
ret.hardware_id = this->hardware_id;
ret.sub_version = default_sub_version_for_version(this->channel.version);
ret.sub_version = default_sub_version_for_version(this->version);
ret.is_extended = extended ? 1 : 0;
ret.language = this->channel.language;
ret.serial_number.encode(phosg::string_printf("%08" PRIX32, this->serial_number));
ret.language = this->language;
ret.serial_number.encode(std::format("{:08X}", this->serial_number));
ret.access_key.encode(this->access_key);
ret.serial_number2 = ret.serial_number;
ret.access_key2 = ret.access_key;
ret.name.encode(this->character->disp.name.decode());
ret.login_character_name.encode(this->character->disp.name.decode());
size_t data_size = extended
? ((this->channel.version == Version::PC_V2) ? sizeof(ret) : sizeof(C_LoginExtended_DC_GC_9D))
? ((this->version == Version::PC_V2) ? sizeof(ret) : sizeof(C_LoginExtended_DC_GC_9D))
: sizeof(C_Login_DC_PC_GC_9D);
this->channel.send(0x9D, 0x01, &ret, data_size);
this->channel->send(0x9D, 0x01, &ret, data_size);
} else if (this->channel.version == Version::GC_V3) {
} else if (this->version == Version::GC_V3) {
C_LoginExtended_GC_9E ret;
ret.player_tag = this->guild_card_number ? 0xFFFF0000 : 0x00010000;
ret.guild_card_number = this->guild_card_number;
ret.hardware_id = this->hardware_id;
ret.sub_version = default_sub_version_for_version(this->channel.version);
ret.sub_version = default_sub_version_for_version(this->version);
ret.is_extended = extended ? 1 : 0;
ret.language = this->channel.language;
ret.serial_number.encode(phosg::string_printf("%08" PRIX32, this->serial_number));
ret.language = this->language;
ret.serial_number.encode(std::format("{:08X}", this->serial_number));
ret.access_key.encode(this->access_key);
ret.serial_number2 = ret.serial_number;
ret.access_key2 = ret.access_key;
ret.name.encode(this->character->disp.name.decode());
ret.login_character_name.encode(this->character->disp.name.decode());
ret.client_config = this->client_config;
this->channel.send(0x9E, 0x01, &ret, extended ? sizeof(ret) : sizeof(C_Login_GC_9E));
this->channel->send(0x9E, 0x01, &ret, extended ? sizeof(ret) : sizeof(C_Login_PC_GC_9E));
} else if (this->channel.version == Version::XB_V3) {
} else if (this->version == Version::XB_V3) {
C_LoginExtended_XB_9E ret;
ret.player_tag = this->guild_card_number ? 0xFFFF0000 : 0x00010000;
ret.guild_card_number = this->guild_card_number;
ret.hardware_id = this->hardware_id;
ret.sub_version = default_sub_version_for_version(this->channel.version);
ret.sub_version = default_sub_version_for_version(this->version);
ret.is_extended = extended ? 1 : 0;
ret.language = this->channel.language;
ret.language = this->language;
ret.serial_number.encode(this->xb_gamertag);
ret.access_key.encode(phosg::string_printf("%016" PRIX64, this->xb_user_id));
ret.access_key.encode(std::format("{:016X}", this->xb_user_id));
ret.serial_number2 = ret.serial_number;
ret.access_key2 = ret.access_key;
ret.name.encode(this->character->disp.name.decode());
ret.netloc.internal_ipv4_address = phosg::random_object<uint32_t>();
ret.netloc.external_ipv4_address = phosg::random_object<uint32_t>();
ret.netloc.port = 9500;
phosg::random_data(&ret.netloc.mac_address, sizeof(ret.netloc.mac_address));
ret.netloc.sg_ip_address = phosg::random_object<uint32_t>();
ret.netloc.spi = phosg::random_object<uint32_t>();
ret.netloc.account_id = this->xb_account_id;
ret.netloc.unknown_a3.clear(0);
ret.login_character_name.encode(this->character->disp.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;
phosg::random_data(&ret.xb_netloc.mac_address, sizeof(ret.xb_netloc.mac_address));
ret.xb_netloc.sg_ip_address = phosg::random_object<uint32_t>();
ret.xb_netloc.spi = phosg::random_object<uint32_t>();
ret.xb_netloc.account_id = this->xb_account_id;
ret.xb_netloc.unknown_a3.clear(0);
ret.xb_user_id_high = this->xb_user_id >> 32;
ret.xb_user_id_low = this->xb_user_id;
this->channel.send(0x9E, 0x01, &ret, extended ? sizeof(ret) : sizeof(C_Login_DC_PC_GC_9D));
this->channel->send(0x9E, 0x01, &ret, extended ? sizeof(ret) : sizeof(C_Login_DC_PC_GC_9D));
} else {
throw runtime_error("unsupported version");
@@ -241,41 +219,41 @@ void DownloadSession::send_93_9D_9E(bool extended) {
void DownloadSession::send_61_98(bool is_98) {
uint8_t command = is_98 ? 0x98 : 0x61;
if (is_v1(this->channel.version)) {
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, 1, 1);
this->channel.send(command, 0x01, ret);
ret.disp = convert_player_disp_data<PlayerDispDataDCPCV3, PlayerDispDataBB>(this->character->disp, Language::ENGLISH, Language::ENGLISH);
this->channel->send(command, 0x01, ret);
} else if (this->channel.version == Version::DC_V2) {
} 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, 1, 1);
ret.disp = convert_player_disp_data<PlayerDispDataDCPCV3, PlayerDispDataBB>(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;
this->channel.send(command, 0x02, ret);
this->channel->send(command, 0x02, ret);
} else if (this->channel.version == Version::PC_V2) {
} 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, 1, 1);
ret.disp = convert_player_disp_data<PlayerDispDataDCPCV3, PlayerDispDataBB>(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;
this->channel.send(command, 0x02, ret);
this->channel->send(command, 0x02, ret);
} else if (is_v3(this->channel.version)) {
} 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, 1, 1);
ret.disp = convert_player_disp_data<PlayerDispDataDCPCV3, PlayerDispDataBB>(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;
ret.info_board.encode(this->character->info_board.decode());
this->channel.send(command, 0x03, ret);
this->channel->send(command, 0x03, ret);
} else if (this->channel.version == Version::BB_V4) {
} else if (this->version == Version::BB_V4) {
C_CharacterData_BB_61_98 ret;
ret.inventory = this->character->inventory;
ret.disp = this->character->disp;
@@ -283,34 +261,30 @@ void DownloadSession::send_61_98(bool is_98) {
ret.records.battle = this->character->battle_records;
ret.choice_search_config = this->character->choice_search_config;
ret.info_board.encode(this->character->info_board.decode());
this->channel.send(command, 0x04, ret);
this->channel->send(command, 0x04, ret);
} else {
throw runtime_error("unsupported version");
}
}
void DownloadSession::on_channel_input(uint16_t command, uint32_t flag, std::string& data) {
// TODO: Use the iovec form of print_data here instead of
// prepend_command_header (which copies the string)
string full_cmd = prepend_command_header(this->channel.version, this->channel.crypt_in.get(), command, flag, data);
for (size_t z = 0; z < 0x28 && z < data.size(); z++) {
this->prev_cmd_data[z] = data[z];
asio::awaitable<void> DownloadSession::on_message(Channel::Message& msg) {
for (size_t z = 0; z < 0x28 && z < msg.data.size(); z++) {
this->prev_cmd_data[z] = msg.data[z];
}
switch (command) {
switch (msg.command) {
case 0x03: {
if (this->channel.version != Version::BB_V4) {
if (this->version != Version::BB_V4) {
throw runtime_error("BB server sent non-BB encryption command");
}
if (!this->bb_key_file) {
throw runtime_error("BB encryption requires a key file");
}
const auto& cmd = check_size_t<S_ServerInitDefault_BB_03_9B>(data, 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->log.info("Enabled BB encryption");
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->log.info_f("Enabled BB encryption");
throw runtime_error("not yet implemented"); // Send 93
break;
}
@@ -319,54 +293,54 @@ void DownloadSession::on_channel_input(uint16_t command, uint32_t flag, std::str
case 0x17:
case 0x91:
case 0x9B: {
const auto& cmd = check_size_t<S_ServerInitDefault_DC_PC_V3_02_17_91_9B>(data, 0xFFFF);
if (uses_v3_encryption(this->channel.version)) {
this->channel.crypt_in = make_shared<PSOV3Encryption>(cmd.server_key);
this->channel.crypt_out = make_shared<PSOV3Encryption>(cmd.client_key);
this->log.info("Enabled V3 encryption (server key %08" PRIX32 ", client key %08" PRIX32 ")",
cmd.server_key.load(), cmd.client_key.load());
} else if (!uses_v4_encryption(this->channel.version)) {
this->channel.crypt_in = make_shared<PSOV2Encryption>(cmd.server_key);
this->channel.crypt_out = make_shared<PSOV2Encryption>(cmd.client_key);
this->log.info("Enabled V2 encryption (server key %08" PRIX32 ", client key %08" PRIX32 ")",
cmd.server_key.load(), cmd.client_key.load());
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->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->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");
}
if (command == 0x02) {
bool is_extended = (this->channel.version == Version::XB_V3);
if (msg.command == 0x02) {
bool is_extended = (this->version == Version::XB_V3);
this->send_93_9D_9E(is_extended);
} else {
if (is_v1(this->channel.version)) {
if (is_v1(this->version)) {
C_LoginV1_DC_PC_V3_90 ret;
ret.serial_number.encode(phosg::string_printf("%08" PRIX32, this->serial_number));
ret.serial_number.encode(std::format("{:08X}", this->serial_number));
ret.access_key.encode(this->access_key);
this->channel.send(0x90, 0x00, ret);
this->channel->send(0x90, 0x00, ret);
} else if (is_v2(this->channel.version)) {
} else if (is_v2(this->version)) {
C_Login_DC_PC_V3_9A ret;
ret.serial_number.encode(phosg::string_printf("%08" PRIX32, this->serial_number));
ret.serial_number.encode(std::format("{:08X}", this->serial_number));
ret.access_key.encode(this->access_key);
ret.player_tag = this->guild_card_number ? 0xFFFF0000 : 0x00010000;
ret.guild_card_number = this->guild_card_number;
ret.sub_version = default_sub_version_for_version(this->channel.version);
ret.sub_version = default_sub_version_for_version(this->version);
ret.serial_number2 = ret.serial_number;
ret.access_key2 = ret.access_key;
this->channel.send(0x9A, 0x00, ret);
this->channel->send(0x9A, 0x00, ret);
} else if (this->channel.version == Version::GC_V3) {
} else if (this->version == Version::GC_V3) {
C_VerifyAccount_V3_DB ret;
ret.serial_number.encode(phosg::string_printf("%08" PRIX32, this->serial_number));
ret.serial_number.encode(std::format("{:08X}", this->serial_number));
ret.access_key.encode(this->access_key);
ret.sub_version = default_sub_version_for_version(this->channel.version);
ret.sub_version = default_sub_version_for_version(this->version);
ret.serial_number2 = ret.serial_number;
ret.access_key2 = ret.access_key;
ret.password.encode(this->password);
this->channel.send(0xDB, 0x00, ret);
this->channel->send(0xDB, 0x00, ret);
} else if (this->channel.version == Version::XB_V3) {
} else if (this->version == Version::XB_V3) {
this->send_93_9D_9E(true);
} else {
@@ -379,36 +353,36 @@ void DownloadSession::on_channel_input(uint16_t command, uint32_t flag, std::str
case 0x90:
case 0x9A: {
if (flag == 1) {
if (is_v1(this->channel.version)) {
if (msg.flag == 1) {
if (is_v1(this->version)) {
C_RegisterV1_DC_92 ret;
ret.hardware_id = this->hardware_id;
ret.sub_version = default_sub_version_for_version(this->channel.version);
ret.language = this->channel.language;
ret.serial_number2.encode(phosg::string_printf("%08" PRIX32, this->serial_number2));
this->channel.send(0x92, 0x00, ret);
ret.sub_version = default_sub_version_for_version(this->version);
ret.language = this->language;
ret.serial_number2.encode(std::format("{:08X}", this->serial_number2));
this->channel->send(0x92, 0x00, ret);
} else if (!is_v4(this->channel.version)) {
} else if (!is_v4(this->version)) {
C_Register_DC_PC_V3_9C ret;
ret.hardware_id = this->hardware_id;
ret.sub_version = default_sub_version_for_version(this->channel.version);
ret.language = this->channel.language;
if (this->channel.version == Version::XB_V3) {
ret.sub_version = default_sub_version_for_version(this->version);
ret.language = this->language;
if (this->version == Version::XB_V3) {
ret.serial_number.encode(this->xb_gamertag);
ret.access_key.encode(phosg::string_printf("%016" PRIX64, this->xb_user_id));
ret.access_key.encode(std::format("{:016X}", this->xb_user_id));
ret.password.encode("xbox-pso");
} else {
ret.serial_number.encode(phosg::string_printf("%08" PRIX32, this->serial_number));
ret.serial_number.encode(std::format("{:08X}", this->serial_number));
ret.access_key.encode(this->access_key);
ret.password.encode(this->password);
}
this->channel.send(0x9C, 0x00, ret);
this->channel->send(0x9C, 0x00, ret);
} else {
throw runtime_error("unsupported version");
}
} else if (flag == 0 || flag == 2) {
} else if (msg.flag == 0 || msg.flag == 2) {
this->send_93_9D_9E(true);
} else {
@@ -419,17 +393,17 @@ void DownloadSession::on_channel_input(uint16_t command, uint32_t flag, std::str
case 0x92:
case 0x9C:
if (flag == 0) {
if (msg.flag == 0) {
throw runtime_error("server rejected login credentials");
}
this->send_93_9D_9E(true);
break;
case 0x9F: {
if (is_v1_or_v2(this->channel.version)) {
if (is_v1_or_v2(this->version)) {
throw runtime_error("invalid command");
}
this->channel.send(0x9F, 0x00, this->client_config);
this->channel->send(0x9F, 0x00, this->client_config);
break;
}
@@ -437,16 +411,16 @@ void DownloadSession::on_channel_input(uint16_t command, uint32_t flag, std::str
C_ExecuteCodeResult_B3 ret;
ret.checksum = 0;
ret.return_value = 0;
this->channel.send(0xB3, 0x00, ret);
this->channel->send(0xB3, 0x00, ret);
break;
}
case 0x04: {
const auto& cmd = check_size_t<S_UpdateClientConfig_V3_04>(data, 0x08, sizeof(S_UpdateClientConfig_V3_04));
if (!is_v1_or_v2(this->channel.version)) {
const auto& cmd = msg.check_size_t<S_UpdateClientConfig_V3_04>(0x08, sizeof(S_UpdateClientConfig_V3_04));
if (!is_v1_or_v2(this->version)) {
for (size_t z = 0; z < 0x20; z++) {
size_t read_index = z + 8;
this->client_config[z] = (read_index < data.size()) ? data[read_index] : this->prev_cmd_data[read_index];
this->client_config[z] = (read_index < msg.data.size()) ? msg.data[read_index] : this->prev_cmd_data[read_index];
}
}
this->guild_card_number = cmd.guild_card_number;
@@ -454,14 +428,14 @@ void DownloadSession::on_channel_input(uint16_t command, uint32_t flag, std::str
C_CharSaveInfo_DCv2_PC_V3_BB_96 ret;
ret.creation_timestamp = this->character->creation_timestamp;
ret.event_counter = this->character->save_count;
this->channel.send(0x96, 0x00, ret);
this->channel->send(0x96, 0x00, ret);
this->sent_96 = true;
}
break;
}
case 0x97:
this->channel.send(0xB1, 0x00);
this->channel->send(0xB1, 0x00);
break;
case 0x95:
@@ -469,13 +443,13 @@ void DownloadSession::on_channel_input(uint16_t command, uint32_t flag, std::str
break;
case 0xB1:
this->channel.send(0x99, 0x00);
this->channel->send(0x99, 0x00);
break;
case 0x1A:
case 0xD5:
if (is_v3(this->channel.version)) {
this->channel.send(0xD6, 0x00);
if (is_v3(this->version)) {
this->channel->send(0xD6, 0x00);
}
break;
@@ -483,24 +457,24 @@ void DownloadSession::on_channel_input(uint16_t command, uint32_t flag, std::str
case 0x1F:
case 0xA0:
case 0xA1: {
C_MenuSelection_10_Flag00 ret;
C_MenuSelectionBase_10 ret;
auto handle_command = [&]<typename CmdT>() {
const auto* items = check_size_vec_t<CmdT>(data, flag + 1);
const auto* items = check_size_vec_t<CmdT>(msg.data, msg.flag + 1);
size_t item_index;
this->log.info("Ship Select menu:");
for (item_index = 1; item_index <= flag; item_index++) {
this->log.info_f("Ship Select menu:");
for (item_index = 1; item_index <= msg.flag; item_index++) {
const auto& item = items[item_index];
auto text = strip_color(item.name.decode());
this->log.info("%zu: (%08" PRIX32 " %08" PRIX32 ") %s", item_index, item.menu_id.load(), item.item_id.load(), text.c_str());
this->log.info_f("{}: ({:08X} {:08X}) {}", item_index, item.menu_id, item.item_id, text);
if (this->ship_menu_selections.count(text)) {
break;
}
}
if (item_index > flag) {
if (item_index > msg.flag) {
if (this->interactive) {
while (item_index == 0 || item_index > flag) {
this->log.info("Choose response index:");
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);
}
@@ -512,13 +486,13 @@ void DownloadSession::on_channel_input(uint16_t command, uint32_t flag, std::str
ret.item_id = items[item_index].item_id;
};
if (uses_utf16(this->channel.version)) {
if (uses_utf16(this->version)) {
handle_command.operator()<S_MenuItem_PC_BB_08>();
} else {
handle_command.operator()<S_MenuItem_DC_V3_08_Ep3_E6>();
}
this->channel.send(0x10, 0x00, ret);
this->channel->send(0x10, 0x00, ret);
break;
}
@@ -538,67 +512,60 @@ void DownloadSession::on_channel_input(uint16_t command, uint32_t flag, std::str
break;
case 0x1D:
this->channel.send(0x1D, 0x00);
this->channel->send(0x1D, 0x00);
break;
case 0x19: {
const auto& cmd = check_size_t<S_Reconnect_19>(data, sizeof(S_Reconnect_19), 0xFFFF);
sockaddr_storage ss;
auto* sin = reinterpret_cast<sockaddr_in*>(&ss);
sin->sin_family = AF_INET;
sin->sin_addr.s_addr = htonl(cmd.address);
sin->sin_port = htons(cmd.port);
string netloc_str = phosg::render_sockaddr_storage(ss);
this->log.info("Connecting to %s", netloc_str.c_str());
struct bufferevent* bev = bufferevent_socket_new(this->base.get(), -1, BEV_OPT_CLOSE_ON_FREE | BEV_OPT_DEFER_CALLBACKS);
if (!bev) {
throw runtime_error(phosg::string_printf("failed to open socket (%d)", EVUTIL_SOCKET_ERROR()));
}
this->channel.set_bufferevent(bev, 0);
this->channel.crypt_in.reset();
this->channel.crypt_out.reset();
if (bufferevent_socket_connect(this->channel.bev.get(), reinterpret_cast<const sockaddr*>(&ss), sizeof(struct sockaddr_in)) != 0) {
throw runtime_error(phosg::string_printf("failed to connect (%d)", EVUTIL_SOCKET_ERROR()));
}
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);
this->log.info_f("Connecting to {}", netloc_str);
auto sock = make_unique<asio::ip::tcp::socket>(co_await async_connect_tcp(new_ep));
this->channel = SocketChannel::create(
this->io_context,
std::move(sock),
this->version,
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->log.info_f("Server channel connected");
break;
}
case 0x83: {
const auto* items = check_size_vec_t<S_LobbyListEntry_83>(data, flag, true);
const auto* items = check_size_vec_t<S_LobbyListEntry_83>(msg.data, msg.flag, true);
this->lobby_menu_items.clear();
for (size_t z = 0; z < flag; z++) {
for (size_t z = 0; z < msg.flag; z++) {
this->lobby_menu_items.emplace_back(items[z]);
}
break;
}
case 0x67: {
// Technically we should assign item IDs here, but the server will never
// be able to see that we didn't, so we don't bother
// Technically we should assign item IDs here, but the server will never be able to see that we didn't, so we
// don't bother
const auto& game_config = this->game_configs[this->current_game_config_index];
if (this->channel.version == Version::PC_V2) {
if (this->version == Version::PC_V2) {
C_CreateGame_PC_C1 ret;
ret.name.encode(random_name());
ret.password.encode(random_name());
ret.difficulty = 0;
ret.difficulty = Difficulty::NORMAL;
ret.battle_mode = (game_config.mode == GameMode::BATTLE);
ret.challenge_mode = (game_config.mode == GameMode::CHALLENGE);
ret.episode = 1;
this->channel.send(0xC1, 0x00, ret);
this->channel->send(0xC1, 0x00, ret);
} else if (!is_v4(this->channel.version)) {
} else if (!is_v4(this->version)) {
C_CreateGame_DC_V3_0C_C1_Ep3_EC ret;
ret.name.encode(random_name());
ret.password.encode(random_name());
ret.difficulty = 0;
ret.difficulty = Difficulty::NORMAL;
ret.battle_mode = (game_config.mode == GameMode::BATTLE);
ret.challenge_mode = (game_config.mode == GameMode::CHALLENGE);
if (is_v1(this->channel.version)) {
if (is_v1(this->version)) {
ret.episode = 0;
} else if (game_config.episode == Episode::EP1) {
ret.episode = 1;
@@ -609,13 +576,13 @@ void DownloadSession::on_channel_input(uint16_t command, uint32_t flag, std::str
} else {
throw std::logic_error("invalid episode");
}
this->channel.send(is_v1(this->channel.version) ? 0x0C : 0xC1, 0x00, ret);
this->channel->send(is_v1(this->version) ? 0x0C : 0xC1, 0x00, ret);
} else {
C_CreateGame_BB_C1 ret;
ret.name.encode(random_name());
ret.password.encode(random_name());
ret.difficulty = 0;
ret.difficulty = Difficulty::NORMAL;
ret.battle_mode = (game_config.mode == GameMode::BATTLE);
ret.challenge_mode = (game_config.mode == GameMode::CHALLENGE);
if (game_config.episode == Episode::EP1) {
@@ -628,7 +595,7 @@ void DownloadSession::on_channel_input(uint16_t command, uint32_t flag, std::str
throw std::logic_error("invalid episode");
}
ret.solo_mode = (game_config.mode == GameMode::SOLO);
this->channel.send(is_v1(this->channel.version) ? 0x0C : 0xC1, 0x00, ret);
this->channel->send(is_v1(this->version) ? 0x0C : 0xC1, 0x00, ret);
}
break;
}
@@ -641,32 +608,32 @@ void DownloadSession::on_channel_input(uint16_t command, uint32_t flag, std::str
this->character->inventory.items[z].data.id = 0x00010000 + z;
}
if (!is_v1(this->channel.version)) {
this->channel.send(0x8A, 0x00);
if (!is_v1(this->version)) {
this->channel->send(0x8A, 0x00);
}
this->channel.send(0x6F, 0x00);
this->channel->send(0x6F, 0x00);
this->send_next_request();
break;
}
case 0xA2: {
auto handle_command = [&]<typename CmdT>() {
const auto* items = check_size_vec_t<CmdT>(data, flag);
for (size_t z = 0; z < flag; z++) {
const auto* items = check_size_vec_t<CmdT>(msg.data, msg.flag);
for (size_t z = 0; z < msg.flag; z++) {
const auto& item = items[z];
uint64_t request = (static_cast<uint64_t>(item.menu_id) << 32) | static_cast<uint64_t>(item.item_id);
if (!this->done_requests.count(request)) {
this->log.info("Adding request %016" PRIX64, request);
this->log.info_f("Adding request {:016X}", request);
this->pending_requests.emplace(request, item.name.decode());
}
}
};
if (this->channel.version == Version::PC_V2) {
if (this->version == Version::PC_V2) {
handle_command.operator()<S_QuestMenuEntry_PC_A2_A4>();
} else if (this->channel.version == Version::XB_V3) {
} else if (this->version == Version::XB_V3) {
handle_command.operator()<S_QuestMenuEntry_XB_A2_A4>();
} else if (this->channel.version == Version::BB_V4) {
} else if (this->version == Version::BB_V4) {
handle_command.operator()<S_QuestMenuEntry_BB_A2_A4>();
} else {
handle_command.operator()<S_QuestMenuEntry_DC_GC_A2_A4>();
@@ -677,26 +644,26 @@ void DownloadSession::on_channel_input(uint16_t command, uint32_t flag, std::str
case 0x44:
case 0xA6: {
auto handle_command = [&]<typename CmdT>() {
const auto& cmd = check_size_t<CmdT>(data, 0xFFFF);
const auto& cmd = msg.check_size_t<CmdT>(0xFFFF);
string internal_name = cmd.filename.decode();
string filtered_name;
for (char ch : internal_name) {
filtered_name.push_back((isalnum(ch) || (ch == '-') || (ch == '.') || (ch == '_')) ? ch : '_');
}
string local_filename = phosg::string_printf(
"%s/%016" PRIX64 "_%" PRIu64 "_%s_%c_%s",
this->output_dir.c_str(),
string local_filename = std::format(
"{}/{:016X}_{}_{}_{:c}_{}",
this->output_dir,
this->current_request,
phosg::now(),
phosg::name_for_enum(this->channel.version),
char_for_language_code(this->channel.language),
filtered_name.c_str());
phosg::name_for_enum(this->version),
char_for_language(this->language),
filtered_name);
this->open_files.emplace(internal_name, OpenFile{.request = this->current_request, .filename = local_filename, .total_size = cmd.file_size, .data = ""});
};
if (is_dc(this->channel.version)) {
if (is_dc(this->version)) {
handle_command.operator()<S_OpenFile_DC_44_A6>();
} else if (!is_v4(this->channel.version)) {
} else if (!is_v4(this->version)) {
handle_command.operator()<S_OpenFile_PC_GC_44_A6>();
} else {
handle_command.operator()<S_OpenFile_BB_44_A6>();
@@ -705,25 +672,23 @@ void DownloadSession::on_channel_input(uint16_t command, uint32_t flag, std::str
}
case 0x13:
case 0xA7: {
const auto& cmd = check_size_t<S_WriteFile_13_A7>(data);
const auto& cmd = msg.check_size_t<S_WriteFile_13_A7>();
string internal_filename = cmd.filename.decode();
if (!is_v1_or_v2(this->channel.version)) {
if (!is_v1_or_v2(this->version)) {
C_WriteFileConfirmation_V3_BB_13_A7 ret;
ret.filename.encode(internal_filename);
this->channel.send(command, flag, ret);
this->channel->send(msg.command, msg.flag, ret);
}
auto f_it = this->open_files.find(internal_filename.c_str());
auto f_it = this->open_files.find(internal_filename);
if (f_it == this->open_files.end()) {
this->log.warning("Received data for non-open file %s", internal_filename.c_str());
this->log.warning_f("Received data for non-open file {}", internal_filename);
break;
}
auto& f = this->open_files.at(cmd.filename.decode());
size_t block_offset = flag * 0x400;
size_t allowed_block_size = (block_offset < f.total_size)
? min<size_t>(f.total_size - block_offset, 0x400)
: 0;
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 block_end_offset = block_offset + data_size;
if (block_end_offset > f.data.size()) {
@@ -732,25 +697,25 @@ void DownloadSession::on_channel_input(uint16_t command, uint32_t flag, std::str
memcpy(f.data.data() + block_offset, cmd.data.data(), data_size);
if (cmd.data_size != 0x400) {
phosg::save_file(f.filename, f.data);
this->log.info("Wrote file %s (%zu bytes)", f.filename.c_str(), f.data.size());
this->log.info_f("Wrote file {} ({} bytes)", f.filename, f.data.size());
this->open_files.erase(internal_filename);
if (phosg::ends_with(internal_filename, ".bin")) {
if (internal_filename.ends_with(".bin")) {
this->bin_complete = true;
} else if (phosg::ends_with(internal_filename, ".dat")) {
} else if (internal_filename.ends_with(".dat")) {
this->dat_complete = true;
}
if (this->open_files.empty() && this->bin_complete && this->dat_complete) {
if (is_v1_or_v2(this->channel.version)) {
if (is_v1_or_v2(this->version)) {
this->on_request_complete();
} else {
this->channel.send(0xAC, 0x00);
this->channel->send(0xAC, 0x00);
}
}
}
break;
}
case 0xAC: {
if (is_v1_or_v2(this->channel.version)) {
if (is_v1_or_v2(this->version)) {
throw runtime_error("unsupported version");
}
this->on_request_complete();
@@ -761,24 +726,24 @@ void DownloadSession::on_channel_input(uint16_t command, uint32_t flag, std::str
void DownloadSession::send_next_request() {
if (should_request_category_list) {
this->log.info("Requesting quest list");
this->channel.send(0xA2, 0x00);
if (is_v4(this->channel.version)) {
this->channel.send(0xA2, 0x01);
this->log.info_f("Requesting quest list");
this->channel->send(0xA2, 0x00);
if (is_v4(this->version)) {
this->channel->send(0xA2, 0x01);
}
this->should_request_category_list = false;
} else if (!this->pending_requests.empty()) {
if (interactive) {
const auto& config = this->game_configs[this->current_game_config_index];
this->log.info("Items available to expand (mode=%s, episode=%s):", name_for_mode(config.mode), name_for_episode(config.episode));
this->log.info_f("Items available to expand (mode={}, episode={}):", name_for_mode(config.mode), name_for_episode(config.episode));
for (const auto& it : this->pending_requests) {
this->log.info("%016" PRIX64 ": %s", it.first, it.second.c_str());
this->log.info_f("{:016X}: {}", it.first, it.second);
}
this->log.info("Choose item to expand by ID (q to quit; s to skip to next config):");
this->log.info_f("Choose item to expand by ID (q to quit; s to skip to next config):");
string input = phosg::fgets(stdin);
if (input.empty() || (input == "q\n")) {
this->channel.disconnect();
this->channel->disconnect();
return;
} else if (input == "s\n") {
this->pending_requests.clear();
@@ -794,22 +759,22 @@ void DownloadSession::send_next_request() {
this->current_request = item_it->first;
this->done_requests.emplace(this->current_request);
this->pending_requests.erase(item_it);
this->log.info("Sending request %016" PRIX64, this->current_request);
this->log.info_f("Sending request {:016X}", this->current_request);
}
C_MenuSelection_10_Flag00 cmd;
C_MenuSelectionBase_10 cmd;
cmd.menu_id = (this->current_request >> 32) & 0xFFFFFFFF;
cmd.item_id = this->current_request & 0xFFFFFFFF;
this->channel.send(0x10, 0x00, cmd);
this->channel->send(0x10, 0x00, cmd);
} else {
this->log.info("No pending requests with current parameters");
this->log.info_f("No pending requests with current parameters");
this->on_request_complete();
}
}
void DownloadSession::on_request_complete() {
for (const auto& data : this->on_request_complete_commands) {
this->channel.send(data);
this->channel->send(data);
}
this->send_61_98(true);
@@ -819,14 +784,14 @@ void DownloadSession::on_request_complete() {
C_LobbySelection_84 ret84;
ret84.menu_id = item.menu_id;
ret84.item_id = item.item_id;
this->channel.send(0x84, 0x00, ret84);
this->channel->send(0x84, 0x00, ret84);
if (this->pending_requests.empty()) {
// Advance to next mode/episode combination
this->current_game_config_index++;
bool v1 = is_v1(this->channel.version);
bool v2 = is_v2(this->channel.version);
bool v3 = is_v3(this->channel.version);
bool v1 = is_v1(this->version);
bool v2 = is_v2(this->version);
bool v3 = is_v3(this->version);
while ((this->current_game_config_index < this->game_configs.size()) &&
((v1 && !this->game_configs[this->current_game_config_index].v1) ||
(v2 && !this->game_configs[this->current_game_config_index].v2) ||
@@ -834,36 +799,16 @@ void DownloadSession::on_request_complete() {
this->current_game_config_index++;
}
if (this->current_game_config_index >= this->game_configs.size()) {
this->log.info("All modes complete");
this->channel.disconnect();
this->log.info_f("All modes complete");
this->channel->disconnect();
} else {
const auto& config = this->game_configs[this->current_game_config_index];
this->log.info("Advancing to %s mode in %s", name_for_mode(config.mode), name_for_episode(config.episode));
this->log.info_f("Advancing to {} mode in {}", name_for_mode(config.mode), name_for_episode(config.episode));
this->should_request_category_list = true;
}
}
}
void DownloadSession::dispatch_on_channel_error(Channel& ch, short events) {
auto* session = reinterpret_cast<DownloadSession*>(ch.context_obj);
session->on_channel_error(events);
}
void DownloadSession::on_channel_error(short events) {
if (events & BEV_EVENT_CONNECTED) {
this->log.info("Server channel connected");
}
if (events & BEV_EVENT_ERROR) {
int err = EVUTIL_SOCKET_ERROR();
this->log.warning("Error %d (%s) in server stream", err, evutil_socket_error_to_string(err));
}
if (events & (BEV_EVENT_ERROR | BEV_EVENT_EOF)) {
this->log.info("Server endpoint has disconnected");
this->channel.disconnect();
event_base_loopexit(this->base.get(), nullptr);
}
}
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},
+25 -25
View File
@@ -1,7 +1,5 @@
#pragma once
#include <event2/event.h>
#include <functional>
#include <map>
#include <memory>
@@ -18,11 +16,12 @@
class DownloadSession {
public:
DownloadSession(
std::shared_ptr<struct event_base> base,
const struct sockaddr_storage& remote,
std::shared_ptr<asio::io_context> io_context,
const std::string& remote_host,
uint16_t remote_port,
const std::string& output_dir,
Version version,
uint8_t language,
Language language,
std::shared_ptr<const PSOBBEncryption::KeyFile> bb_key_file,
uint32_t serial_number2,
uint32_t serial_number,
@@ -43,12 +42,19 @@ public:
DownloadSession& operator=(DownloadSession&&) = delete;
virtual ~DownloadSession() = default;
asio::awaitable<void> run();
protected:
// Config (must be set by caller)
std::string remote_host;
uint16_t remote_port;
std::string output_dir;
Version version;
Language language;
bool show_command_data;
std::shared_ptr<const PSOBBEncryption::KeyFile> bb_key_file;
uint32_t serial_number2;
uint32_t serial_number;
uint32_t serial_number2;
std::string access_key;
std::string username;
std::string password;
@@ -62,17 +68,17 @@ protected:
// State (set during session)
phosg::PrefixedLogger log;
std::shared_ptr<struct event_base> base;
Channel channel;
std::shared_ptr<asio::io_context> io_context;
std::shared_ptr<Channel> channel;
uint64_t hardware_id;
uint32_t guild_card_number;
uint32_t guild_card_number = 0;
parray<uint8_t, 0x28> prev_cmd_data;
parray<uint8_t, 0x20> client_config;
bool sent_96;
bool sent_96 = false;
std::vector<S_LobbyListEntry_83> lobby_menu_items;
bool should_request_category_list;
uint64_t current_request;
bool should_request_category_list = true;
uint64_t current_request = 0;
std::map<uint64_t, std::string> pending_requests;
std::unordered_set<uint64_t> done_requests;
@@ -92,20 +98,14 @@ protected:
bool v3;
};
static const std::vector<GameConfig> game_configs;
size_t current_game_config_index;
bool in_game;
bool bin_complete;
bool dat_complete;
size_t current_game_config_index = 0;
bool in_game = false;
bool bin_complete = false;
bool dat_complete = false;
static void dispatch_on_channel_input(Channel& ch, uint16_t command, uint32_t flag, std::string& msg);
static void dispatch_on_channel_error(Channel& ch, short events);
void on_channel_input(uint16_t command, uint32_t flag, std::string& msg);
void on_channel_error(short events);
void send_next_request();
void on_request_complete();
void assign_item_ids(uint32_t base_item_id);
void send_93_9D_9E(bool extended);
void send_61_98(bool is_98);
asio::awaitable<void> on_message(Channel::Message& msg);
void send_next_request();
void on_request_complete();
};
+186 -159
View File
@@ -13,138 +13,145 @@ 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;
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{
// clang-format off
// TYPE FLAGS RT BP ENUM NAME IN-GAME NAME ULTIMATE NAME
{EnemyType::UNKNOWN, 0, 0xFF, 0xFF, "UNKNOWN", "__UNKNOWN__", nullptr},
{EnemyType::NONE, 0, 0xFF, 0xFF, "NONE", "__NONE__", nullptr},
{EnemyType::NON_ENEMY_NPC, EP1 | EP2 | EP4, 0xFF, 0xFF, "NON_ENEMY_NPC", "__NPC__", nullptr},
{EnemyType::AL_RAPPY, EP1 | RARE, 0x06, 0x19, "AL_RAPPY", "Al Rappy", "Pal Rappy"},
{EnemyType::ASTARK, EP4, 0x41, 0x09, "ASTARK", "Astark", nullptr},
{EnemyType::BA_BOOTA, EP4, 0x4F, 0x03, "BA_BOOTA", "Ba Boota", nullptr},
{EnemyType::BARBA_RAY, EP2, 0x49, 0x0F, "BARBA_RAY", "Barba Ray", nullptr},
{EnemyType::BARBAROUS_WOLF, EP1 | EP2, 0x08, 0x03, "BARBAROUS_WOLF", "Barbarous Wolf", "Gulgus-gue"},
{EnemyType::BEE_L, EP1 | EP2, 0xFF, 0xFF, "BEE_L", "Bee L", "Gee L"},
{EnemyType::BEE_R, EP1 | EP2, 0xFF, 0xFF, "BEE_R", "Bee R", "Gee R"},
{EnemyType::BOOMA, EP1, 0x09, 0x4B, "BOOMA", "Booma", "Bartle"},
{EnemyType::BOOTA, EP4, 0x4D, 0x00, "BOOTA", "Boota", nullptr},
{EnemyType::BULCLAW, EP1, 0x28, 0x1F, "BULCLAW", "Bulclaw", nullptr},
{EnemyType::BULK, EP1, 0x27, 0x1F, "BULK", "Bulk", nullptr},
{EnemyType::CANADINE, EP1, 0x1C, 0x07, "CANADINE", "Canadine", "Canabin"},
{EnemyType::CANADINE_GROUP, EP1, 0x1C, 0x08, "CANADINE_GROUP", "Canadine (group)", "Canabin (group)"},
{EnemyType::CANANE, EP1, 0x1D, 0x09, "CANANE", "Canane", "Canune"},
{EnemyType::CHAOS_BRINGER, EP1, 0x24, 0x0D, "CHAOS_BRINGER", "Chaos Bringer", "Dark Bringer"},
{EnemyType::CHAOS_SORCERER, EP1 | EP2, 0x1F, 0x0A, "CHAOS_SORCERER", "Chaos Sorceror", "Gran Sorceror"},
{EnemyType::CLAW, EP1, 0x26, 0x20, "CLAW", "Claw", nullptr},
{EnemyType::DARK_BELRA, EP1 | EP2, 0x25, 0x0E, "DARK_BELRA", "Dark Belra", "Indi Belra"},
{EnemyType::DARK_FALZ_1, EP1, 0xFF, 0x36, "DARK_FALZ_1", "Dark Falz (phase 1)", nullptr},
{EnemyType::DARK_FALZ_2, EP1, 0x2F, 0x37, "DARK_FALZ_2", "Dark Falz (phase 2)", nullptr},
{EnemyType::DARK_FALZ_3, EP1, 0x2F, 0x38, "DARK_FALZ_3", "Dark Falz (phase 3)", nullptr},
{EnemyType::DARK_GUNNER, EP1, 0x22, 0x1E, "DARK_GUNNER", "Dark Gunner", nullptr},
{EnemyType::DARVANT, EP1, 0xFF, 0x35, "DARVANT", "Darvant", nullptr},
{EnemyType::DARVANT_ULTIMATE, EP1, 0xFF, 0x39, "DARVANT_ULTIMATE", "Darvant (ultimate)", nullptr},
{EnemyType::DE_ROL_LE, EP1, 0x2D, 0x0F, "DE_ROL_LE", "De Rol Le", "Dal Ral Lie"},
{EnemyType::DE_ROL_LE_BODY, EP1, 0xFF, 0xFF, "DE_ROL_LE_BODY", "De Rol Le (body)", "Dal Ral Lie (body)"},
{EnemyType::DE_ROL_LE_MINE, EP1, 0xFF, 0xFF, "DE_ROL_LE_MINE", "De Rol Le (mine)", "Dal Ral Lie (mine)"},
{EnemyType::DEATH_GUNNER, EP1, 0x23, 0x1E, "DEATH_GUNNER", "Death Gunner", nullptr},
{EnemyType::DEL_LILY, EP2, 0x53, 0x25, "DEL_LILY", "Del Lily", nullptr},
{EnemyType::DEL_RAPPY_CRATER, EP4, 0x57, 0x06, "DEL_RAPPY_CRATER", "Del Rappy (crater)", nullptr},
{EnemyType::DEL_RAPPY_DESERT, EP4, 0x58, 0x18, "DEL_RAPPY_DESERT", "Del Rappy (desert)", nullptr},
{EnemyType::DELBITER, EP2, 0x48, 0x0D, "DELBITER", "Delbiter", nullptr},
{EnemyType::DELDEPTH, EP2, 0x47, 0x30, "DELDEPTH", "Deldepth", nullptr},
{EnemyType::DELSABER, EP1 | EP2, 0x1E, 0x52, "DELSABER", "Delsaber", nullptr},
{EnemyType::DIMENIAN, EP1 | EP2, 0x29, 0x53, "DIMENIAN", "Dimenian", "Arlan"},
{EnemyType::DOLMDARL, EP2, 0x41, 0x50, "DOLMDARL", "Dolmdarl", nullptr},
{EnemyType::DOLMOLM, EP2, 0x40, 0x4F, "DOLMOLM", "Dolmolm", nullptr},
{EnemyType::DORPHON, EP4, 0x50, 0x0F, "DORPHON", "Dorphon", nullptr},
{EnemyType::DORPHON_ECLAIR, EP4 | RARE, 0x51, 0x10, "DORPHON_ECLAIR", "Dorphon Eclair", nullptr},
{EnemyType::DRAGON, EP1, 0x2C, 0x12, "DRAGON", "Dragon", "Sil Dragon"},
{EnemyType::DUBCHIC, EP1 | EP2, 0x18, 0x1B, "DUBCHIC", "Dubchic", "Dubchich"},
{EnemyType::DUBWITCH, EP1 | EP2, 0xFF, 0xFF, "DUBWITCH", "Dubwitch", "Duvuik"},
{EnemyType::EGG_RAPPY, EP2, 0x51, 0x19, "EGG_RAPPY", "Egg Rappy", nullptr},
{EnemyType::EPSIGARD, EP2, 0xFF, 0xFF, "EPSIGARD", "Episgard", nullptr},
{EnemyType::EPSILON, EP2, 0x54, 0x23, "EPSILON", "Epsilon", nullptr},
{EnemyType::EVIL_SHARK, EP1, 0x10, 0x4F, "EVIL_SHARK", "Evil Shark", "Vulmer"},
{EnemyType::GAEL_OR_GIEL, EP2, 0xFF, 0x2E, "GAEL", "Gael/Giel", nullptr},
{EnemyType::GAL_GRYPHON, EP2, 0x4D, 0x1E, "GAL_GRYPHON", "Gal Gryphon", nullptr},
{EnemyType::GARANZ, EP1 | EP2, 0x19, 0x1D, "GARANZ", "Garanz", "Baranz"},
{EnemyType::GEE, EP2, 0x36, 0x07, "GEE", "Gee", nullptr},
{EnemyType::GI_GUE, EP2, 0x37, 0x1A, "GI_GUE", "Gi Gue", nullptr},
{EnemyType::GIBBLES, EP2, 0x3D, 0x3D, "GIBBLES", "Gibbles", nullptr},
{EnemyType::GIGOBOOMA, EP1, 0x0B, 0x4D, "GIGOBOOMA", "Gigobooma", "Tollaw"},
{EnemyType::GILLCHIC, EP1 | EP2, 0x32, 0x1C, "GILLCHIC", "Gillchic", "Gillchich"},
{EnemyType::GIRTABLULU, EP4, 0x48, 0x1F, "GIRTABLULU", "Girtablulu", nullptr},
{EnemyType::GOBOOMA, EP1, 0x0A, 0x4C, "GOBOOMA", "Gobooma", "Barble"},
{EnemyType::GOL_DRAGON, EP2, 0x4C, 0x12, "GOL_DRAGON", "Gol Dragon", nullptr},
{EnemyType::GORAN, EP4, 0x52, 0x11, "GORAN", "Goran", nullptr},
{EnemyType::GORAN_DETONATOR, EP4, 0x53, 0x13, "GORAN_DETONATOR", "Goran Detonator", nullptr},
{EnemyType::GRASS_ASSASSIN, EP1 | EP2, 0x0C, 0x4E, "GRASS_ASSASSIN", "Grass Assassin", "Crimson Assassin"},
{EnemyType::GUIL_SHARK, EP1, 0x12, 0x51, "GUIL_SHARK", "Guil Shark", "Melqueek"},
{EnemyType::HALLO_RAPPY, EP2, 0x50, 0x19, "HALLO_RAPPY", "Hallo Rappy", nullptr},
{EnemyType::HIDOOM, EP1 | EP2, 0x17, 0x32, "HIDOOM", "Hidoom", nullptr},
{EnemyType::HILDEBEAR, EP1 | EP2, 0x01, 0x49, "HILDEBEAR", "Hildebear", "Hildelt"},
{EnemyType::HILDEBLUE, EP1 | EP2 | RARE, 0x02, 0x4A, "HILDEBLUE", "Hildeblue", "Hildetorr"},
{EnemyType::ILL_GILL, EP2, 0x52, 0x26, "ILL_GILL", "Ill Gill", nullptr},
{EnemyType::KONDRIEU, EP4 | RARE, 0x5B, 0x2A, "KONDRIEU", "Kondrieu", nullptr},
{EnemyType::LA_DIMENIAN, EP1 | EP2, 0x2A, 0x54, "LA_DIMENIAN", "La Dimenian", "Merlan"},
{EnemyType::LOVE_RAPPY, EP2, 0x33, 0x19, "LOVE_RAPPY", "Love Rappy", nullptr},
{EnemyType::MERICARAND, EP2, 0x38, 0x3A, "MERICARAND", "Mericarand", nullptr},
{EnemyType::MERICAROL, EP2, 0x38, 0x3A, "MERICAROL", "Mericarol", nullptr},
{EnemyType::MERICUS, EP2 | RARE, 0x3A, 0x46, "MERICUS", "Mericus", nullptr},
{EnemyType::MERIKLE, EP2 | RARE, 0x39, 0x45, "MERIKLE", "Merikle", nullptr},
{EnemyType::MERILLIA, EP2, 0x34, 0x4B, "MERILLIA", "Merillia", nullptr},
{EnemyType::MERILTAS, EP2, 0x35, 0x4C, "MERILTAS", "Meriltas", nullptr},
{EnemyType::MERISSA_A, EP4, 0x46, 0x19, "MERISSA_A", "Merissa A", nullptr},
{EnemyType::MERISSA_AA, EP4 | RARE, 0x47, 0x1A, "MERISSA_AA", "Merissa AA", nullptr},
{EnemyType::MIGIUM, EP1 | EP2, 0x16, 0x33, "MIGIUM", "Migium", nullptr},
{EnemyType::MONEST, EP1 | EP2, 0x04, 0x01, "MONEST", "Monest", "Mothvist"},
{EnemyType::MORFOS, EP2, 0x42, 0x40, "MORFOS", "Morfos", nullptr},
{EnemyType::MOTHMANT, EP1 | EP2, 0x03, 0x00, "MOTHMANT", "Mothmant", "Mothvert"},
{EnemyType::NANO_DRAGON, EP1, 0x0F, 0x1A, "NANO_DRAGON", "Nano Dragon", nullptr},
{EnemyType::NAR_LILY, EP1 | EP2 | RARE, 0x0E, 0x05, "NAR_LILY", "Nar Lily", "Mil Lily"},
{EnemyType::OLGA_FLOW_1, EP2, 0xFF, 0x2B, "OLGA_FLOW_1", "Olga Flow (phase 1)", nullptr},
{EnemyType::OLGA_FLOW_2, EP2, 0x4E, 0x2C, "OLGA_FLOW_2", "Olga Flow (phase 2)", nullptr},
{EnemyType::PAL_SHARK, EP1, 0x11, 0x50, "PAL_SHARK", "Pal Shark", "Govulmer"},
{EnemyType::PAN_ARMS, EP1 | EP2, 0x15, 0x31, "PAN_ARMS", "Pan Arms", nullptr},
{EnemyType::PAZUZU_CRATER, EP4 | RARE, 0x4B, 0x08, "PAZUZU_CRATER", "Pazuzu (crater)", nullptr},
{EnemyType::PAZUZU_DESERT, EP4 | RARE, 0x4C, 0x1C, "PAZUZU_DESERT", "Pazuzu (desert)", nullptr},
{EnemyType::PIG_RAY, EP2, 0xFF, 0xFF, "PIG_RAY", "Pig Ray", nullptr},
{EnemyType::POFUILLY_SLIME, EP1, 0x13, 0x30, "POFUILLY_SLIME", "Pofuilly Slime", nullptr},
{EnemyType::POUILLY_SLIME, EP1 | RARE, 0x14, 0x2F, "POUILLY_SLIME", "Pouilly Slime", nullptr},
{EnemyType::POISON_LILY, EP1 | EP2, 0x0D, 0x04, "POISON_LILY", "Poison Lily", "Ob Lily"},
{EnemyType::PYRO_GORAN, EP4, 0x54, 0x12, "PYRO_GORAN", "Pyro Goran", nullptr},
{EnemyType::RAG_RAPPY, EP1 | EP2, 0x05, 0x18, "RAG_RAPPY", "Rag Rappy", "El Rappy"},
{EnemyType::RECOBOX, EP2, 0x43, 0x41, "RECOBOX", "Recobox", nullptr},
{EnemyType::RECON, EP2, 0x44, 0x42, "RECON", "Recon", nullptr},
{EnemyType::SAINT_MILION, EP4, 0x59, 0x22, "SAINT_MILION", "Saint-Milion", nullptr},
{EnemyType::SAINT_RAPPY, EP2, 0x4F, 0x19, "SAINT_RAPPY", "Saint Rappy", nullptr},
{EnemyType::SAND_RAPPY_CRATER, EP4, 0x55, 0x05, "SAND_RAPPY_CRATER", "Sand Rappy (crater)", nullptr},
{EnemyType::SAND_RAPPY_DESERT, EP4, 0x56, 0x17, "SAND_RAPPY_DESERT", "Sand Rappy (desert)", nullptr},
{EnemyType::SATELLITE_LIZARD_CRATER, EP4, 0x44, 0x0D, "SATELLITE_LIZARD_CRATER", "Satellite Lizard (crater)", nullptr},
{EnemyType::SATELLITE_LIZARD_DESERT, EP4, 0x45, 0x1D, "SATELLITE_LIZARD_DESERT", "Satellite Lizard (desert)", nullptr},
{EnemyType::SAVAGE_WOLF, EP1 | EP2, 0x07, 0x02, "SAVAGE_WOLF", "Savage Wolf", "Gulgus"},
{EnemyType::SHAMBERTIN, EP4, 0x5A, 0x26, "SHAMBERTIN", "Shambertin", nullptr},
{EnemyType::SINOW_BEAT, EP1, 0x1A, 0x06, "SINOW_BEAT", "Sinow Beat", "Sinow Blue"},
{EnemyType::SINOW_BERILL, EP2, 0x3E, 0x06, "SINOW_BERILL", "Sinow Berill", nullptr},
{EnemyType::SINOW_GOLD, EP1, 0x1B, 0x13, "SINOW_GOLD", "Sinow Gold", "Sinow Red"},
{EnemyType::SINOW_SPIGELL, EP2, 0x3F, 0x13, "SINOW_SPIGELL", "Sinow Spigell", nullptr},
{EnemyType::SINOW_ZELE, EP2, 0x46, 0x44, "SINOW_ZELE", "Sinow Zele", nullptr},
{EnemyType::SINOW_ZOA, EP2, 0x45, 0x43, "SINOW_ZOA", "Sinow Zoa", nullptr},
{EnemyType::SO_DIMENIAN, EP1 | EP2, 0x2B, 0x55, "SO_DIMENIAN", "So Dimenian", "Del-D"},
{EnemyType::UL_GIBBON, EP2, 0x3B, 0x3B, "UL_GIBBON", "Ul Gibbon", nullptr},
{EnemyType::VOL_OPT_1, EP1, 0xFF, 0xFF, "VOL_OPT_1", "Vol Opt (phase 1)", "Vol Opt ver.2 (phase 1)"},
{EnemyType::VOL_OPT_2, EP1, 0x2E, 0x25, "VOL_OPT_2", "Vol Opt (phase 2)", "Vol Opt ver.2 (phase 2)"},
{EnemyType::VOL_OPT_AMP, EP1, 0xFF, 0xFF, "VOL_OPT_AMP", "Vol Opt (amp)", "Vol Opt ver.2 (amp)"},
{EnemyType::VOL_OPT_CORE, EP1, 0xFF, 0xFF, "VOL_OPT_CORE", "Vol Opt (core)", "Vol Opt ver.2 (core)"},
{EnemyType::VOL_OPT_MONITOR, EP1, 0xFF, 0xFF, "VOL_OPT_MONITOR", "Vol Opt (monitor)", "Vol Opt ver.2 (monitor)"},
{EnemyType::VOL_OPT_PILLAR, EP1, 0xFF, 0xFF, "VOL_OPT_PILLAR", "Vol Opt (pillar)", "Vol Opt ver.2 (pillar)"},
{EnemyType::YOWIE_CRATER, EP4, 0x42, 0x0E, "YOWIE_CRATER", "Yowie (crater)", nullptr},
{EnemyType::YOWIE_DESERT, EP4, 0x43, 0x1E, "YOWIE_DESERT", "Yowie (desert)", nullptr},
{EnemyType::ZE_BOOTA, EP4, 0x4E, 0x01, "ZE_BOOTA", "Ze Boota", nullptr},
{EnemyType::ZOL_GIBBON, EP2, 0x3C, 0x3C, "ZOL_GIBBON", "Zol Gibbon", nullptr},
{EnemyType::ZU_CRATER, EP4, 0x49, 0x07, "ZU_CRATER", "Zu (crater)", nullptr},
{EnemyType::ZU_DESERT, EP4, 0x4A, 0x1B, "ZU_DESERT", "Zu (desert)", nullptr},
// 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},
// clang-format on
};
@@ -163,7 +170,7 @@ EnemyType phosg::enum_for_name<EnemyType>(const char* name) {
if (index.empty()) {
for (const auto& def : type_defs) {
if (!index.emplace(def.enum_name, def.type).second) {
throw logic_error(phosg::string_printf("duplicate enemy enum name: %s", def.enum_name));
throw logic_error(std::format("duplicate enemy enum name: {}", def.enum_name));
}
}
}
@@ -171,23 +178,20 @@ EnemyType phosg::enum_for_name<EnemyType>(const char* name) {
}
const vector<EnemyType>& enemy_types_for_rare_table_index(Episode episode, uint8_t rt_index) {
const auto& generate_table = +[](Episode episode) -> vector<vector<EnemyType>> {
vector<vector<EnemyType>> ret;
static array<vector<vector<EnemyType>>, 5> data;
auto& ret = data.at(static_cast<size_t>(episode));
if (ret.empty()) {
for (const auto& def : type_defs) {
if (def.valid_in_episode(episode) && (def.rt_index != 0xFF)) {
if (!def.valid_in_episode(episode)) {
continue;
}
if (def.rt_index != 0xFF) {
if (def.rt_index >= ret.size()) {
ret.resize(def.rt_index + 1);
}
ret[def.rt_index].emplace_back(def.type);
}
}
return ret;
};
static array<vector<vector<EnemyType>>, 5> data;
auto& ret = data.at(static_cast<size_t>(episode));
if (ret.empty()) {
ret = generate_table(episode);
}
try {
return ret.at(rt_index);
@@ -197,29 +201,52 @@ const vector<EnemyType>& enemy_types_for_rare_table_index(Episode episode, uint8
}
}
EnemyType EnemyTypeDefinition::rare_type(Episode episode, uint8_t event, uint8_t floor) const {
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()) {
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);
}
ret[bp_index].emplace_back(def.type);
}
}
}
try {
return ret.at(bp_index);
} catch (const out_of_range&) {
static const vector<EnemyType> empty_vec;
return empty_vec;
}
}
EnemyType EnemyTypeDefinition::rare_type(uint8_t area, uint8_t event) const {
switch (this->type) {
case EnemyType::HILDEBEAR:
return EnemyType::HILDEBLUE;
case EnemyType::RAG_RAPPY:
switch (episode) {
case Episode::EP1:
return EnemyType::AL_RAPPY;
case Episode::EP2:
switch (event) {
case 0x01: // rappy_type 1
return EnemyType::SAINT_RAPPY;
case 0x04: // rappy_type 2
return EnemyType::EGG_RAPPY;
case 0x05: // rappy_type 3
return EnemyType::HALLO_RAPPY;
default:
return EnemyType::LOVE_RAPPY;
}
case Episode::EP4:
return (floor > 0x05) ? EnemyType::DEL_RAPPY_DESERT : EnemyType::DEL_RAPPY_CRATER;
default:
throw logic_error("invalid episode");
if (area < 0x12) {
return EnemyType::AL_RAPPY;
} else if (area < 0x24) {
switch (event) {
case 0x01: // rappy_type 1
return EnemyType::SAINT_RAPPY;
case 0x04: // rappy_type 2
return EnemyType::EGG_RAPPY;
case 0x05: // rappy_type 3
return EnemyType::HALLO_RAPPY;
default:
return EnemyType::LOVE_RAPPY;
}
} else if (area <= 0x28) {
return EnemyType::DEL_RAPPY_CRATER;
} else {
return EnemyType::DEL_RAPPY_DESERT;
}
case EnemyType::POISON_LILY:
return EnemyType::NAR_LILY;
+26 -5
View File
@@ -7,7 +7,13 @@
#include "StaticGameData.hh"
#include "Types.hh"
// We don't know what the actual maximum was, since it was presumably only stored server-side on Sega's servers. The
// client uses values up to 0x6C (Kondrieu), so we just choose a value larger than that.
static constexpr size_t NUM_RT_INDEXES_V3 = 0x64;
static constexpr size_t NUM_RT_INDEXES_V4 = 0x70;
enum class EnemyType : uint8_t {
MIN_VALUE = 0,
UNKNOWN = 0,
NONE,
NON_ENEMY_NPC,
@@ -15,6 +21,7 @@ enum class EnemyType : uint8_t {
ASTARK,
BA_BOOTA,
BARBA_RAY,
BARBA_RAY_JOINT,
BARBAROUS_WOLF,
BEE_L,
BEE_R,
@@ -33,8 +40,8 @@ enum class EnemyType : uint8_t {
DARK_FALZ_2,
DARK_FALZ_3,
DARK_GUNNER,
DARK_GUNNER_CONTROL,
DARVANT,
DARVANT_ULTIMATE,
DE_ROL_LE,
DE_ROL_LE_BODY,
DE_ROL_LE_MINE,
@@ -78,6 +85,7 @@ enum class EnemyType : uint8_t {
HILDEBLUE,
ILL_GILL,
KONDRIEU,
KONDRIEU_SPINNER,
LA_DIMENIAN,
LOVE_RAPPY,
MERICARAND,
@@ -109,6 +117,7 @@ enum class EnemyType : uint8_t {
RECOBOX,
RECON,
SAINT_MILION,
SAINT_MILION_SPINNER,
SAINT_RAPPY,
SAND_RAPPY_CRATER,
SAND_RAPPY_DESERT,
@@ -116,6 +125,7 @@ enum class EnemyType : uint8_t {
SATELLITE_LIZARD_DESERT,
SAVAGE_WOLF,
SHAMBERTIN,
SHAMBERTIN_SPINNER,
SINOW_BEAT,
SINOW_BERILL,
SINOW_GOLD,
@@ -124,6 +134,7 @@ enum class EnemyType : uint8_t {
SINOW_ZOA,
SO_DIMENIAN,
UL_GIBBON,
UL_RAY,
VOL_OPT_1,
VOL_OPT_2,
VOL_OPT_AMP,
@@ -136,7 +147,7 @@ enum class EnemyType : uint8_t {
ZOL_GIBBON,
ZU_CRATER,
ZU_DESERT,
MAX_ENEMY_TYPE,
MAX_VALUE,
};
struct EnemyTypeDefinition {
@@ -145,11 +156,17 @@ struct EnemyTypeDefinition {
VALID_EP2 = 0x02,
VALID_EP4 = 0x04,
IS_RARE = 0x08,
IS_BOSS = 0x10,
};
EnemyType type;
uint8_t flags;
uint8_t rt_index; // 0xFF if not valid (e.g. not an enemy)
uint8_t bp_index; // 0xFF if not valid (e.g. not an enemy)
uint8_t rt_index; // 0xFF if not valid (e.g. not an enemy, or has no drops)
uint8_t legacy_rt_index; // Index used by Schtserv in their Ep4 .rel format
std::vector<uint8_t> bp_stats_indexes;
std::vector<uint8_t> bp_attack_data_indexes;
std::vector<uint8_t> bp_resist_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;
const char* in_game_name;
const char* ultimate_name; // May be null if same as in_game_name
@@ -169,7 +186,10 @@ struct EnemyTypeDefinition {
inline bool is_rare() const {
return (this->flags & Flag::IS_RARE);
}
EnemyType rare_type(Episode episode, uint8_t event, uint8_t floor) const;
inline bool is_boss() const {
return (this->flags & Flag::IS_BOSS);
}
EnemyType rare_type(uint8_t area, uint8_t event) const;
};
const EnemyTypeDefinition& type_definition_for_enemy(EnemyType type);
@@ -180,3 +200,4 @@ 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);
+18 -29
View File
@@ -11,24 +11,19 @@ const vector<uint16_t>& all_assist_card_ids(bool is_nte) {
// 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 = {
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};
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 = {
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, 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};
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,
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};
return is_nte ? ALL_ASSIST_CARD_IDS_TRIAL : ALL_ASSIST_CARD_IDS_FINAL;
}
@@ -174,13 +169,11 @@ uint32_t AssistServer::compute_num_assist_effects_for_client(uint16_t client_id)
if (ce->def.target_mode == TargetMode::TEAM) {
auto this_deck_entry = this->deck_entries[client_id];
auto other_deck_entry = this->deck_entries[z];
if (this_deck_entry && other_deck_entry &&
(this_deck_entry->team_id == other_deck_entry->team_id)) {
if (this_deck_entry && other_deck_entry && (this_deck_entry->team_id == other_deck_entry->team_id)) {
affected = true;
}
} else if ((ce->def.target_mode == TargetMode::SELF) && (z == client_id)) {
affected = true;
} else if (ce->def.target_mode == TargetMode::EVERYONE) {
} else if (((ce->def.target_mode == TargetMode::SELF) && (z == client_id)) ||
(ce->def.target_mode == TargetMode::EVERYONE)) {
affected = true;
}
if (affected) {
@@ -226,9 +219,8 @@ bool AssistServer::should_block_assist_effects_for_client(uint16_t client_id) co
(this->deck_entries[client_id]->team_id == this->deck_entries[z]->team_id)) {
return true;
}
} else if ((ce->def.target_mode == TargetMode::SELF) && (client_id == z)) {
return true;
} else if (ce->def.target_mode == TargetMode::EVERYONE) {
} else if (((ce->def.target_mode == TargetMode::SELF) && (client_id == z)) ||
(ce->def.target_mode == TargetMode::EVERYONE)) {
return true;
}
}
@@ -237,10 +229,7 @@ bool AssistServer::should_block_assist_effects_for_client(uint16_t client_id) co
}
AssistEffect AssistServer::get_active_assist_by_index(size_t index) const {
if (index < this->num_active_assists) {
return this->active_assist_effects[index];
}
return AssistEffect::NONE;
return (index < this->num_active_assists) ? this->active_assist_effects[index] : AssistEffect::NONE;
}
void AssistServer::populate_effects() {
+36 -67
View File
@@ -82,19 +82,19 @@ void BattleRecord::Event::serialize(phosg::StringWriter& w) const {
void BattleRecord::Event::print(FILE* stream) const {
string time_str = phosg::format_time(this->timestamp);
fprintf(stream, "Event @%016" PRIX64 " (%s) ", this->timestamp, time_str.c_str());
phosg::fwrite_fmt(stream, "Event @{:016X} ({}) ", this->timestamp, time_str);
switch (this->type) {
case Type::PLAYER_JOIN:
fprintf(stream, "PLAYER_JOIN %02" PRIX32 "\n", this->players[0].lobby_data.client_id.load());
phosg::fwrite_fmt(stream, "PLAYER_JOIN {:02X}\n", this->players[0].lobby_data.client_id);
this->players[0].print(stream);
break;
case Type::PLAYER_LEAVE:
fprintf(stream, "PLAYER_LEAVE %02hhu\n", this->leaving_client_id);
phosg::fwrite_fmt(stream, "PLAYER_LEAVE {:02}\n", this->leaving_client_id);
break;
case Type::SET_INITIAL_PLAYERS:
fprintf(stream, "SET_INITIAL_PLAYERS");
phosg::fwrite_fmt(stream, "SET_INITIAL_PLAYERS");
for (const auto& player : this->players) {
fprintf(stream, " %02" PRIX32, player.lobby_data.client_id.load());
phosg::fwrite_fmt(stream, " {:02X}", player.lobby_data.client_id);
}
fputc('\n', stream);
for (const auto& player : this->players) {
@@ -102,23 +102,23 @@ void BattleRecord::Event::print(FILE* stream) const {
}
break;
case Type::BATTLE_COMMAND:
fprintf(stream, "BATTLE_COMMAND\n");
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);
break;
case Type::GAME_COMMAND:
fprintf(stream, "GAME_COMMAND\n");
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);
break;
case Type::EP3_GAME_COMMAND:
fprintf(stream, "EP3_GAME_COMMAND\n");
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);
break;
case Type::CHAT_MESSAGE:
fprintf(stream, "CHAT_MESSAGE %08" PRIX32 "\n", this->guild_card_number);
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);
break;
case Type::SERVER_DATA_COMMAND:
fprintf(stream, "SERVER_DATA_COMMAND\n");
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);
break;
default:
@@ -174,21 +174,6 @@ string BattleRecord::serialize() const {
return std::move(w.str());
}
bool BattleRecord::writable() const {
return this->is_writable;
}
bool BattleRecord::battle_in_progress() const {
return (this->battle_start_timestamp != 0);
}
const BattleRecord::Event* BattleRecord::get_first_event() const {
if (this->events.empty()) {
return nullptr;
}
return &this->events.front();
}
void BattleRecord::add_player(
const PlayerLobbyDataDCGC& lobby_data,
const PlayerInventory& inventory,
@@ -256,16 +241,6 @@ void BattleRecord::add_random_data(const void* data, size_t size) {
this->random_stream.append(reinterpret_cast<const char*>(data), size);
}
vector<string> BattleRecord::get_all_server_data_commands() const {
vector<string> ret;
for (const auto& event : this->events) {
if (event.type == Event::Type::SERVER_DATA_COMMAND) {
ret.emplace_back(event.data);
}
}
return ret;
}
const string& BattleRecord::get_random_stream() const {
return this->random_stream;
}
@@ -363,26 +338,24 @@ 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);
fprintf(stream, "BattleRecord %s behavior_flags=%08" PRIX32 " start=%016" PRIX64 " (%s) end=%016" PRIX64 " (%s); %zu events\n",
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.c_str(),
start_str,
this->battle_end_timestamp,
end_str.c_str(), this->events.size());
end_str, this->events.size());
for (const auto& event : this->events) {
event.print(stream);
}
}
BattleRecordPlayer::BattleRecordPlayer(
shared_ptr<const BattleRecord> rec,
shared_ptr<struct event_base> base)
: record(rec),
BattleRecordPlayer::BattleRecordPlayer(std::shared_ptr<asio::io_context> io_context, shared_ptr<const BattleRecord> rec)
: io_context(io_context),
record(rec),
event_it(this->record->events.begin()),
play_start_timestamp(0),
base(base),
next_command_ev(event_new(this->base.get(), -1, EV_TIMEOUT, &BattleRecordPlayer::dispatch_schedule_events, this), event_free) {}
next_command_timer(*this->io_context) {}
shared_ptr<const BattleRecord> BattleRecordPlayer::get_record() const {
return this->record;
@@ -395,40 +368,37 @@ void BattleRecordPlayer::set_lobby(shared_ptr<Lobby> l) {
void BattleRecordPlayer::start() {
if (this->play_start_timestamp == 0) {
this->play_start_timestamp = phosg::now();
this->schedule_events();
asio::co_spawn(*this->io_context, this->play_task(), asio::detached);
}
}
void BattleRecordPlayer::dispatch_schedule_events(evutil_socket_t, short, void* ctx) {
reinterpret_cast<BattleRecordPlayer*>(ctx)->schedule_events();
}
void BattleRecordPlayer::schedule_events() {
// If the lobby is destroyed, we can't replay anything - just return without
// rescheduling
asio::awaitable<void> BattleRecordPlayer::play_task() {
auto l = this->lobby.lock();
if (!l) {
return;
}
for (;;) {
uint64_t relative_ts = phosg::now() - this->play_start_timestamp + this->record->battle_start_timestamp;
// If the lobby is destroyed, we can't replay anything
if (!l) {
co_return;
}
if (this->event_it == this->record->events.end()) {
if (relative_ts >= this->record->battle_end_timestamp) {
// If the record is complete and the end timestamp has been reached,
// send exit commands to all players in the lobby, and don't reschedule
// the event (it will be deleted along with the Player when the lobby is
// destroyed, when the last client leaves)
// the event (it will be deleted along with the Player when the lobby
// is destroyed, when the last client leaves)
send_command(l, 0xED, 0x00);
break;
} else {
// There are no more events to play, but the battle has not officially
// ended yet - reschedule the event for the end time
auto tv = phosg::usecs_to_timeval(this->record->battle_end_timestamp - relative_ts);
event_add(this->next_command_ev.get(), &tv);
// There are no more events to play, but the battle has not actually
// ended yet; wait until the end time
this->next_command_timer.expires_after(std::chrono::microseconds(this->record->battle_end_timestamp - relative_ts));
co_await this->next_command_timer.async_wait(asio::use_awaitable);
l = this->lobby.lock();
}
break;
} else {
if (this->event_it->timestamp <= relative_ts) {
@@ -464,11 +434,10 @@ void BattleRecordPlayer::schedule_events() {
this->event_it++;
} else {
// The next event should not occur yet, so reschedule for the time when
// it should occur
auto tv = phosg::usecs_to_timeval(this->event_it->timestamp - relative_ts);
event_add(this->next_command_ev.get(), &tv);
break;
// The next event should not occur yet, so wait until its time
this->next_command_timer.expires_after(std::chrono::microseconds(this->event_it->timestamp - relative_ts));
co_await this->next_command_timer.async_wait(asio::use_awaitable);
l = this->lobby.lock();
}
}
}
+24 -12
View File
@@ -1,8 +1,8 @@
#pragma once
#include <event2/event.h>
#include <stdint.h>
#include <asio.hpp>
#include <deque>
#include <memory>
#include <phosg/Strings.hh>
@@ -62,10 +62,25 @@ public:
explicit BattleRecord(const std::string& data);
std::string serialize() const;
bool writable() const;
bool battle_in_progress() const;
inline bool writable() const {
return this->is_writable;
}
const Event* get_first_event() const;
inline uint32_t get_behavior_flags() const {
return this->behavior_flags;
}
inline bool battle_in_progress() const {
return (this->battle_start_timestamp != 0);
}
inline const Event* get_first_event() const {
return this->events.empty() ? nullptr : &this->events.front();
}
inline std::deque<Event> get_all_events() const {
return this->events;
}
void add_player(
const PlayerLobbyDataDCGC& lobby_data,
@@ -86,7 +101,6 @@ public:
void print(FILE* stream) const;
std::vector<std::string> get_all_server_data_commands() const;
const std::string& get_random_stream() const;
private:
@@ -108,7 +122,7 @@ private:
class BattleRecordPlayer {
public:
BattleRecordPlayer(std::shared_ptr<const BattleRecord> rec, std::shared_ptr<struct event_base> base);
BattleRecordPlayer(std::shared_ptr<asio::io_context> io_context, std::shared_ptr<const BattleRecord> rec);
~BattleRecordPlayer() = default;
std::shared_ptr<const BattleRecord> get_record() const;
@@ -117,16 +131,14 @@ public:
void start();
private:
static void dispatch_schedule_events(evutil_socket_t, short, void* ctx);
void schedule_events();
std::shared_ptr<asio::io_context> io_context;
std::shared_ptr<const BattleRecord> record;
std::deque<BattleRecord::Event>::const_iterator event_it;
uint64_t play_start_timestamp;
std::shared_ptr<struct event_base> base;
std::weak_ptr<Lobby> lobby;
std::shared_ptr<struct event> next_command_ev;
phosg::StringReader random_r;
asio::steady_timer next_command_timer;
asio::awaitable<void> play_task();
};
} // namespace Episode3
+110 -129
View File
@@ -7,11 +7,7 @@ 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, shared_ptr<Server> server)
: w_server(server),
w_player_state(server->get_player_state(client_id)),
client_id(client_id),
@@ -123,7 +119,7 @@ ssize_t Card::apply_abnormal_condition(
int8_t dice_roll_value,
int8_t random_percent) {
auto s = this->server();
auto log = s->log_stack(phosg::string_printf("apply_abnormal_condition(%02hhX, @%04X, @%04X, %hd, %hhd, %hhd): ", def_effect_index, target_card_ref, sc_card_ref, value, dice_roll_value, random_percent));
auto log = s->log_stack(std::format("apply_abnormal_condition({:02X}, @{:04X}, @{:04X}, {}, {}, {}): ", def_effect_index, target_card_ref, sc_card_ref, value, dice_roll_value, random_percent));
bool is_nte = s->options.is_nte();
ssize_t existing_cond_index;
@@ -132,8 +128,7 @@ ssize_t Card::apply_abnormal_condition(
if (cond.type == eff.type) {
existing_cond_index = z;
if ((!is_nte && eff.type == ConditionType::MV_BONUS) ||
((cond.card_definition_effect_index == def_effect_index) &&
(cond.card_ref == target_card_ref))) {
((cond.card_definition_effect_index == def_effect_index) && (cond.card_ref == target_card_ref))) {
break;
}
} else {
@@ -150,13 +145,13 @@ ssize_t Card::apply_abnormal_condition(
break;
}
}
log.debug("existing_cond_index < 0 (new condition) => cond_index = %zd", cond_index);
log.debug_f("existing_cond_index < 0 (new condition) => cond_index = {}", cond_index);
} else {
log.debug("existing_cond_index = %zd (existing condition)", existing_cond_index);
log.debug_f("existing_cond_index = {} (existing condition)", existing_cond_index);
}
if (cond_index < 0) {
log.debug("no space for condition");
log.debug_f("no space for condition");
return -1;
}
@@ -164,7 +159,7 @@ ssize_t Card::apply_abnormal_condition(
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);
log.debug("MV_BONUS combines => existing_cond_value = %hd", existing_cond_value);
log.debug_f("MV_BONUS combines => existing_cond_value = {}", existing_cond_value);
}
s->card_special->apply_stat_deltas_to_card_from_condition_and_clear_cond(cond, this->shared_from_this());
@@ -173,11 +168,7 @@ ssize_t Card::apply_abnormal_condition(
cond.condition_giver_card_ref = sc_card_ref;
cond.card_definition_effect_index = def_effect_index;
cond.order = 10;
if (dice_roll_value < 0) {
cond.dice_roll_value = this->player_state()->roll_dice_with_effects(1);
} else {
cond.dice_roll_value = dice_roll_value;
}
cond.dice_roll_value = (dice_roll_value < 0) ? this->player_state()->roll_dice_with_effects(1) : dice_roll_value;
cond.flags = 0;
cond.value = value + existing_cond_value;
cond.value8 = value + existing_cond_value;
@@ -205,7 +196,7 @@ ssize_t Card::apply_abnormal_condition(
}
string cond_str = cond.str(s);
log.debug("wrote condition %zd => %s", cond_index, cond_str.c_str());
log.debug_f("wrote condition {} => {}", cond_index, cond_str);
if (!is_nte) {
s->card_special->update_condition_orders(this->shared_from_this());
@@ -214,7 +205,7 @@ ssize_t Card::apply_abnormal_condition(
continue;
}
string cond_str = cond.str(s);
log.debug("sorted conditions: [%zu] => %s", z, cond_str.c_str());
log.debug_f("sorted conditions: [{}] => {}", z, cond_str);
}
}
@@ -298,19 +289,18 @@ void Card::commit_attack(
size_t strike_number,
int16_t* out_effective_damage) {
auto s = this->server();
auto log = s->log_stack(phosg::string_printf("commit_attack(@%04hX #%04hX, @%04hX #%04hX => %hd (str%zu)): ", this->get_card_ref(), this->get_card_id(), attacker_card->get_card_ref(), attacker_card->get_card_id(), damage, strike_number));
auto log = s->log_stack(std::format("commit_attack(@{:04X} #{:04X}, @{:04X} #{:04X} => {} (str{})): ", this->get_card_ref(), this->get_card_id(), attacker_card->get_card_ref(), attacker_card->get_card_id(), damage, strike_number));
bool is_nte = s->options.is_nte();
int16_t effective_damage = damage;
s->card_special->adjust_attack_damage_due_to_conditions(
this->shared_from_this(), &effective_damage, attacker_card->get_card_ref());
log.debug("adjusted damage = %hd", effective_damage);
log.debug_f("adjusted damage = {}", effective_damage);
size_t num_assists = s->assist_server->compute_num_assist_effects_for_client(this->client_id);
for (size_t z = 0; z < num_assists; z++) {
auto eff = s->assist_server->get_active_assist_by_index(z);
if ((eff == AssistEffect::RANSOM) &&
(attacker_card->action_chain.chain.attack_medium == AttackMedium::PHYSICAL)) {
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);
s->team_exp[team_id] -= exp_amount;
@@ -324,36 +314,35 @@ void Card::commit_attack(
}
}
}
log.debug("after assists = %hd", effective_damage);
log.debug_f("after assists = {}", effective_damage);
if (this->action_metadata.check_flag(0x10)) {
effective_damage = 0;
log.debug("flag 0x10 => effective damage = %hd", effective_damage);
log.debug_f("flag 0x10 => effective damage = {}", effective_damage);
}
auto attacker_ps = attacker_card->player_state();
attacker_ps->stats.damage_given += effective_damage;
this->player_state()->stats.damage_taken += effective_damage;
log.debug("updated stats");
log.debug_f("updated stats");
this->current_hp = clamp<int16_t>(this->current_hp - effective_damage, 0, this->max_hp);
log.debug("hp set to %hd", this->current_hp);
log.debug_f("hp set to {}", this->current_hp);
if ((effective_damage > 0) &&
(attacker_ps->stats.max_attack_damage < effective_damage)) {
if ((effective_damage > 0) && (attacker_ps->stats.max_attack_damage < effective_damage)) {
attacker_ps->stats.max_attack_damage = effective_damage;
log.debug("attacker new max damage %hd", effective_damage);
log.debug_f("attacker new max damage {}", effective_damage);
}
this->last_attack_final_damage = effective_damage;
log.debug("last attack final damage = %hd", effective_damage);
log.debug_f("last attack final damage = {}", effective_damage);
if (effective_damage > 0) {
this->card_flags = this->card_flags | 4;
log.debug("set flag 4");
log.debug_f("set flag 4");
}
if (this->current_hp < 1) {
this->destroy_set_card(attacker_card);
log.debug("card destroyed");
log.debug_f("card destroyed");
}
G_ApplyConditionEffect_Ep3_6xB4x06 cmd_to_send;
@@ -396,8 +385,10 @@ int16_t Card::compute_defense_power_for_attacker_card(shared_ptr<const Card> att
}
}
s->card_special->apply_action_conditions(EffectWhen::BEFORE_ANY_CARD_ATTACK, attacker_card, this->shared_from_this(), 0x08, nullptr);
s->card_special->apply_action_conditions(EffectWhen::BEFORE_ANY_CARD_ATTACK, attacker_card, this->shared_from_this(), 0x10, nullptr);
s->card_special->apply_action_conditions(
EffectWhen::BEFORE_ANY_CARD_ATTACK, attacker_card, this->shared_from_this(), 0x08, nullptr);
s->card_special->apply_action_conditions(
EffectWhen::BEFORE_ANY_CARD_ATTACK, attacker_card, this->shared_from_this(), 0x10, nullptr);
return this->action_metadata.defense_power + this->action_metadata.defense_bonus;
}
@@ -439,8 +430,7 @@ void Card::destroy_set_card(shared_ptr<Card> attacker_card) {
}
}
if ((s->map_and_rules->rules.hp_type == HPType::DEFEAT_TEAM) &&
(ps->get_sc_card().get() == this)) {
if ((s->map_and_rules->rules.hp_type == HPType::DEFEAT_TEAM) && (ps->get_sc_card().get() == this)) {
for (size_t set_index = 0; set_index < 8; set_index++) {
auto card = ps->get_set_card(set_index);
if (card) {
@@ -464,7 +454,7 @@ void Card::destroy_set_card(shared_ptr<Card> attacker_card) {
uint8_t other_team_id = s->player_states[client_id]->get_team_id();
uint8_t this_team_id = ps->get_team_id();
if (this_team_id == other_team_id) {
s->add_team_exp(team_id, this->max_hp);
s->add_team_exp(this_team_id, this->max_hp);
}
}
}
@@ -488,8 +478,7 @@ int32_t Card::error_code_for_move_to_location(const Location& loc) const {
if (this->card_flags & 2) {
return -0x60;
}
if (!this->server()->ruler_server->card_ref_can_move(
this->client_id, this->card_ref, 1)) {
if (!this->server()->ruler_server->card_ref_can_move(this->client_id, this->card_ref, 1)) {
return -0x7B;
}
// Note: The original code passes non-null pointers here but ignores the
@@ -507,19 +496,19 @@ void Card::execute_attack(shared_ptr<Card> attacker_card) {
}
auto s = this->server();
auto log = s->log_stack(phosg::string_printf("execute_attack(@%04X #%04X, @%04X #%04X): ", this->get_card_ref(), this->get_card_id(), attacker_card->get_card_ref(), attacker_card->get_card_id()));
auto log = s->log_stack(std::format("execute_attack(@{:04X} #{:04X}, @{:04X} #{:04X}): ", this->get_card_ref(), this->get_card_id(), attacker_card->get_card_ref(), attacker_card->get_card_id()));
bool is_nte = s->options.is_nte();
this->card_flags &= 0xFFFFFFF3;
int16_t attack_ap = this->action_metadata.attack_bonus;
int16_t attack_tp = 0;
int16_t defense_power = is_nte ? 0 : this->compute_defense_power_for_attacker_card(attacker_card);
log.debug("ap=%hd, tp=%hd", attack_ap, attack_tp);
log.debug_f("ap={}, tp={}", attack_ap, attack_tp);
if (!is_nte && (attack_ap == 0) && !this->action_metadata.check_flag(0x20)) {
log.debug("ap == 0 and flag 0x20 not set");
log.debug_f("ap == 0 and flag 0x20 not set");
return;
} else {
log.debug("ap != 0 or flag 0x20 set; continuing...");
log.debug_f("ap != 0 or flag 0x20 set; continuing...");
}
G_ApplyConditionEffect_Ep3_6xB4x06 cmd;
@@ -529,9 +518,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 = 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;
@@ -542,7 +529,7 @@ void Card::execute_attack(shared_ptr<Card> attacker_card) {
if (is_nte) {
defense_power = this->compute_defense_power_for_attacker_card(attacker_card);
log.debug("ap=%hd, tp=%hd, defense=%hd", attack_ap, attack_tp, defense_power);
log.debug_f("ap={}, tp={}, defense={}", attack_ap, attack_tp, defense_power);
attacker_card->compute_action_chain_results(true, false);
attack_ap = attacker_card->action_chain.chain.damage;
if (this->action_chain.chain.attack_medium == AttackMedium::TECH) {
@@ -553,14 +540,14 @@ void Card::execute_attack(shared_ptr<Card> attacker_card) {
}
s->card_special->compute_attack_ap(this->shared_from_this(), &attack_ap, attacker_card->get_card_ref());
log.debug("computed ap %hd", attack_ap);
log.debug_f("computed ap {}", attack_ap);
this->apply_ap_and_tp_adjust_assists_to_attack(attacker_card, &attack_ap, &defense_power, &attack_tp);
log.debug("assist adjusts ap=%hd, defense=%hd", attack_ap, defense_power);
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;
this->last_attack_preliminary_damage = preliminary_damage;
log.debug("raw_damage=%hd, preliminary_damange=%hd", raw_damage, preliminary_damage);
log.debug_f("raw_damage={}, preliminary_damange={}", raw_damage, preliminary_damage);
uint32_t unknown_a9 = 0;
auto target = s->card_special->compute_replaced_target_based_on_conditions(
@@ -568,19 +555,19 @@ void Card::execute_attack(shared_ptr<Card> attacker_card) {
if (!target) {
target = this->shared_from_this();
log.debug("target is not replaced");
log.debug_f("target is not replaced");
} else {
log.debug("target replaced with @%04hX #%04hX", target->get_card_ref(), target->get_card_id());
log.debug_f("target replaced with @{:04X} #{:04X}", target->get_card_ref(), target->get_card_id());
}
if (!is_nte) {
if (unknown_a9 != 0) {
preliminary_damage = 0;
log.debug("a9 nonzero; preliminary_damage = 0");
log.debug_f("a9 nonzero; preliminary_damage = 0");
}
if (!(this->card_flags & 2) && (!attacker_card || !(attacker_card->card_flags & 2))) {
s->card_special->check_for_defense_interference(attacker_card, this->shared_from_this(), &preliminary_damage);
log.debug("checked for defense interference");
log.debug_f("checked for defense interference");
}
}
@@ -592,19 +579,19 @@ void Card::execute_attack(shared_ptr<Card> attacker_card) {
ps->stats.num_attacks_taken++;
if (!(target->card_flags & 2)) {
log.debug("flag 2 not set");
log.debug_f("flag 2 not set");
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);
}
} else {
log.debug("flag 2 set; committing zero-damage attack");
log.debug_f("flag 2 set; committing zero-damage attack");
target->commit_attack(0, attacker_card, &cmd, 0, nullptr);
}
if (!is_nte && (this != target.get())) {
log.debug("target was replaced; committing zero-damage attack on original card");
log.debug_f("target was replaced; committing zero-damage attack on original card");
this->commit_attack(0, attacker_card, &cmd, 0, nullptr);
}
@@ -613,11 +600,7 @@ void Card::execute_attack(shared_ptr<Card> attacker_card) {
}
bool Card::get_condition_value(
ConditionType cond_type,
uint16_t card_ref,
uint8_t def_effect_index,
uint16_t value,
uint16_t* out_value) const {
ConditionType cond_type, uint16_t card_ref, uint8_t def_effect_index, uint16_t value, uint16_t* out_value) const {
return this->action_chain.get_condition_value(cond_type, card_ref, def_effect_index, value, out_value);
}
@@ -697,9 +680,7 @@ int32_t Card::move_to_location(const Location& loc) {
this->card_flags = this->card_flags | 0x80;
// On NTE, traps happen now, not after the Move phase
if (s->options.is_nte() &&
this->def_entry->def.is_sc() &&
((s->overlay_state.tiles[loc.y][loc.x] & 0xF0) == 0x40)) {
if (s->options.is_nte() && this->def_entry->def.is_sc() && ((s->overlay_state.tiles[loc.y][loc.x] & 0xF0) == 0x40)) {
for (size_t z = 0; z < 4; z++) {
auto other_ps = s->player_states[z];
if (!other_ps) {
@@ -752,8 +733,7 @@ void Card::propagate_shared_hp_if_needed() {
((this->def_entry->def.type == CardType::HUNTERS_SC) || (this->def_entry->def.type == CardType::ARKZ_SC))) {
for (size_t other_client_id = 0; other_client_id < 4; other_client_id++) {
auto other_ps = this->server()->player_states[other_client_id];
if ((other_client_id != this->client_id) && other_ps &&
(other_ps->get_team_id() == this->team_id)) {
if ((other_client_id != this->client_id) && other_ps && (other_ps->get_team_id() == this->team_id)) {
other_ps->get_sc_card()->set_current_hp(this->current_hp, false);
}
}
@@ -906,7 +886,8 @@ void Card::clear_action_chain_and_metadata_and_most_flags() {
void Card::compute_action_chain_results(bool apply_action_conditions, bool ignore_this_card_ap_tp) {
auto s = this->server();
auto log = s->log_stack(phosg::string_printf("compute_action_chain_results(@%04hX #%04hX): ", this->get_card_ref(), this->get_card_id()));
auto log = s->log_stack(std::format(
"compute_action_chain_results(@{:04X} #{:04X}): ", this->get_card_ref(), this->get_card_id()));
bool is_nte = s->options.is_nte();
this->action_chain.compute_attack_medium(s);
@@ -914,7 +895,7 @@ void Card::compute_action_chain_results(bool apply_action_conditions, bool ignor
this->action_chain.chain.ap_effect_bonus = 0;
this->action_chain.chain.tp_effect_bonus = 0;
log.debug("(initial) medium=%s, strike_count=%hhu, ap_effect_bonus=%hhd, tp_effect_bonus=%hhd",
log.debug_f("(initial) medium={}, strike_count={}, ap_effect_bonus={}, tp_effect_bonus={}",
phosg::name_for_enum(this->action_chain.chain.attack_medium),
this->action_chain.chain.strike_count,
this->action_chain.chain.ap_effect_bonus,
@@ -929,9 +910,10 @@ void Card::compute_action_chain_results(bool apply_action_conditions, bool ignor
stat_swap_type = StatSwapType::NONE;
} else {
stat_swap_type = s->card_special->compute_stat_swap_type(this->shared_from_this());
log.debug("stat_swap_type = %zu (0=none, 1=a/t, 2=a/h)", static_cast<size_t>(stat_swap_type));
s->card_special->get_effective_ap_tp(stat_swap_type, &effective_ap, &effective_tp, this->get_current_hp(), this->ap, this->tp);
log.debug("effective_ap = %hd, effective_tp = %hd", effective_ap, effective_tp);
log.debug_f("stat_swap_type = {} (0=none, 1=a/t, 2=a/h)", static_cast<size_t>(stat_swap_type));
s->card_special->get_effective_ap_tp(
stat_swap_type, &effective_ap, &effective_tp, this->get_current_hp(), this->ap, this->tp);
log.debug_f("effective_ap = {}, effective_tp = {}", effective_ap, effective_tp);
}
// This option doesn't exist in NTE
@@ -942,7 +924,7 @@ void Card::compute_action_chain_results(bool apply_action_conditions, bool ignor
if (ce) {
effective_ap += ce->def.ap.stat;
effective_tp += ce->def.tp.stat;
log.debug("(action card @%04hX) updated effective_ap = %hd, effective_tp = %hd", this->action_chain.chain.attack_action_card_refs[z].load(), effective_ap, effective_tp);
log.debug_f("(action card @{:04X}) updated effective_ap = {}, effective_tp = {}", this->action_chain.chain.attack_action_card_refs[z], effective_ap, effective_tp);
}
}
@@ -957,7 +939,7 @@ void Card::compute_action_chain_results(bool apply_action_conditions, bool ignor
stat_swap_type, &card_ap, &card_tp, card->get_current_hp(), card->ap, card->tp);
effective_ap += card_ap;
effective_tp += card_tp;
log.debug("(mag card set_index %zu @%04hX) updated effective_ap = %hd, effective_tp = %hd",
log.debug_f("(mag card set_index {} @{:04X}) updated effective_ap = {}, effective_tp = {}",
set_index, card->get_card_ref(), effective_ap, effective_tp);
}
}
@@ -968,25 +950,25 @@ void Card::compute_action_chain_results(bool apply_action_conditions, bool ignor
sc_card->compute_action_chain_results(apply_action_conditions, true);
effective_ap += sc_card->action_chain.chain.effective_ap + sc_card->action_chain.chain.ap_effect_bonus;
effective_tp += sc_card->action_chain.chain.effective_tp + sc_card->action_chain.chain.tp_effect_bonus;
log.debug("(item is attacking; adding SC stats) updated effective_ap = %hd, effective_tp = %hd",
log.debug_f("(item is attacking; adding SC stats) updated effective_ap = {}, effective_tp = {}",
effective_ap, effective_tp);
}
if (!this->action_chain.check_flag(0x10)) {
this->action_chain.chain.effective_ap = is_nte ? effective_ap : min<int16_t>(effective_ap, 99);
log.debug("set chain effective_ap = %hd", this->action_chain.chain.effective_ap);
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);
log.debug("set chain effective_tp = %hd", this->action_chain.chain.effective_tp);
log.debug_f("set chain effective_tp = {}", this->action_chain.chain.effective_tp);
}
if (apply_action_conditions) {
auto this_sh = this->shared_from_this();
s->card_special->apply_action_conditions(EffectWhen::BEFORE_ANY_CARD_ATTACK, this_sh, this_sh, 1, nullptr);
log.debug("applied action conditions (1)");
log.debug_f("applied action conditions (1)");
} else {
log.debug("skipped applying action conditions (1)");
log.debug_f("skipped applying action conditions (1)");
}
size_t num_assists = s->assist_server->compute_num_assist_effects_for_client(this->client_id);
@@ -1008,7 +990,7 @@ void Card::compute_action_chain_results(bool apply_action_conditions, bool ignor
break;
case AssistEffect::INFLUENCE:
if (!is_nte && this->card_type_is_sc_or_creature()) {
int16_t count = ps->count_set_refs();
int16_t count = ps->count_hand_refs();
this->action_chain.chain.ap_effect_bonus += (count >> 1);
}
break;
@@ -1082,8 +1064,7 @@ void Card::compute_action_chain_results(bool apply_action_conditions, bool ignor
}
auto other_sc_card = other_ps->get_sc_card();
if (other_sc_card &&
(abs(this->loc.x - other_sc_card->loc.x) < 2) &&
(abs(this->loc.y - other_sc_card->loc.y) < 2)) {
(abs(this->loc.x - other_sc_card->loc.x) < 2) && (abs(this->loc.y - other_sc_card->loc.y) < 2)) {
num_scs_in_range++;
}
}
@@ -1106,29 +1087,32 @@ void Card::compute_action_chain_results(bool apply_action_conditions, bool ignor
int16_t damage = 0;
if (this->action_chain.chain.attack_medium == AttackMedium::TECH) {
damage = this->action_chain.chain.effective_tp + this->action_chain.chain.tp_effect_bonus;
log.debug("(tech) damage = %hhd (eff) + %hhd (bonus) = %hd", this->action_chain.chain.effective_tp, this->action_chain.chain.tp_effect_bonus, damage);
log.debug_f("(tech) damage = {} (eff) + {} (bonus) = {}",
this->action_chain.chain.effective_tp, this->action_chain.chain.tp_effect_bonus, damage);
} else if (this->action_chain.chain.attack_medium == AttackMedium::PHYSICAL) {
damage = this->action_chain.chain.effective_ap + this->action_chain.chain.ap_effect_bonus;
log.debug("(physical) damage = %hhd (eff) + %hhd (bonus) = %hd", this->action_chain.chain.effective_ap, this->action_chain.chain.ap_effect_bonus, damage);
log.debug_f("(physical) damage = {} (eff) + {} (bonus) = {}",
this->action_chain.chain.effective_ap, this->action_chain.chain.ap_effect_bonus, damage);
} else {
log.debug("(unknown attack medium) damage = 0");
log.debug_f("(unknown attack medium) damage = 0");
}
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);
log.debug("overall chain damage = %hd (base) * %hhd (mult) = %hhd", damage, this->action_chain.chain.damage_multiplier, this->action_chain.chain.damage);
log.debug_f("overall chain damage = {} (base) * {} (mult) = {}",
damage, this->action_chain.chain.damage_multiplier, this->action_chain.chain.damage);
if (apply_action_conditions) {
auto this_sh = this->shared_from_this();
s->card_special->apply_action_conditions(EffectWhen::BEFORE_ANY_CARD_ATTACK, this_sh, this_sh, 2, nullptr);
log.debug("applied action conditions (2)");
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);
log.debug("(has flag 0x100) chain damage = %hhd", this->action_chain.chain.damage);
log.debug_f("(has flag 0x100) chain damage = {}", this->action_chain.chain.damage);
}
} else {
log.debug("skipped applying action conditions (2)");
log.debug_f("skipped applying action conditions (2)");
}
if (!is_nte) {
@@ -1156,9 +1140,9 @@ void Card::compute_action_chain_results(bool apply_action_conditions, bool ignor
}
}
if (log.should_log(phosg::LogLevel::DEBUG)) {
if (log.should_log(phosg::LogLevel::L_DEBUG)) {
string chain_str = this->action_chain.str(s);
log.debug("result computed as %s", chain_str.c_str());
log.debug_f("result computed as {}", chain_str);
}
}
@@ -1226,24 +1210,24 @@ void Card::move_phase_before() {
void Card::unknown_80236374(shared_ptr<Card> other_card, const ActionState* as) {
auto s = this->server();
auto log = s->log_stack(phosg::string_printf("unknown_80236374(@%04hX #%04hX, @%04hX #%04hX): ", this->get_card_ref(), this->get_card_id(), other_card->get_card_ref(), other_card->get_card_id()));
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::DEBUG)) {
if (log.should_log(phosg::LogLevel::L_DEBUG)) {
if (as) {
string as_str = as->str(s);
log.debug("as = %s", as_str.c_str());
log.debug_f("as = {}", as_str);
} else {
log.debug("as = null");
log.debug_f("as = null");
}
}
auto check_card = [&](shared_ptr<Card> card) -> void {
if (card) {
if (!card->unknown_80236554(other_card, as)) {
log.debug("check_card @%04hX #%04hX => false", card->get_card_ref(), card->get_card_id());
log.debug_f("check_card @{:04X} #{:04X} => false", card->get_card_ref(), card->get_card_id());
card->action_metadata.clear_flags(0x20);
} else {
log.debug("check_card @%04hX #%04hX => true", card->get_card_ref(), card->get_card_id());
log.debug_f("check_card @{:04X} #{:04X} => true", card->get_card_ref(), card->get_card_id());
card->action_metadata.set_flags(0x20);
}
}
@@ -1271,8 +1255,7 @@ void Card::unknown_80236374(shared_ptr<Card> other_card, const ActionState* as)
}
void Card::unknown_802379BC(uint16_t card_ref) {
this->action_chain.chain.unknown_card_ref_a3 =
(card_ref == 0xFFFF) ? this->card_ref : card_ref;
this->action_chain.chain.unknown_card_ref_a3 = (card_ref == 0xFFFF) ? this->card_ref : card_ref;
}
void Card::unknown_802379DC(const ActionState& pa) {
@@ -1359,8 +1342,7 @@ void Card::dice_phase_before() {
cond.remaining_turns--;
}
if (cond.remaining_turns < 1) {
s->card_special->apply_stat_deltas_to_card_from_condition_and_clear_cond(
cond, this->shared_from_this());
s->card_special->apply_stat_deltas_to_card_from_condition_and_clear_cond(cond, this->shared_from_this());
}
}
}
@@ -1378,14 +1360,13 @@ bool Card::is_guard_item() const {
bool Card::unknown_80236554(shared_ptr<Card> other_card, const ActionState* as) {
auto s = this->server();
auto log = s->log_stack(other_card
? phosg::string_printf("unknown_80236554(@%04hX #%04hX, @%04hX #%04hX): ", this->get_card_ref(), this->get_card_id(), other_card->get_card_ref(), other_card->get_card_id())
: phosg::string_printf("unknown_80236554(@%04hX #%04hX, null): ", this->get_card_ref(), this->get_card_id()));
if (log.should_log(phosg::LogLevel::DEBUG)) {
? 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())
: std::format("unknown_80236554(@{:04X} #{:04X}, null): ", this->get_card_ref(), this->get_card_id()));
if (log.should_log(phosg::LogLevel::L_DEBUG)) {
if (as) {
string as_str = as->str(s);
log.debug("as = %s", as_str.c_str());
log.debug_f("as = {}", as->str(s));
} else {
log.debug("as = null");
log.debug_f("as = null");
}
}
@@ -1398,7 +1379,7 @@ bool Card::unknown_80236554(shared_ptr<Card> other_card, const ActionState* as)
if (other_card->action_chain.chain.target_card_refs[z] == this->get_card_ref()) {
attack_bonus = other_card->action_chain.chain.damage;
ret = true;
log.debug("attack_bonus = %hd (matched other_card->action_chain.chain.target_card_refs)", attack_bonus);
log.debug_f("attack_bonus = {} (matched other_card->action_chain.chain.target_card_refs)", attack_bonus);
break;
}
}
@@ -1406,7 +1387,7 @@ bool Card::unknown_80236554(shared_ptr<Card> other_card, const ActionState* as)
for (size_t z = 0; (z < 4 * 9) && (as->target_card_refs[z] != 0xFFFF); z++) {
if (as->target_card_refs[z] == this->get_card_ref()) {
attack_bonus = other_card->action_chain.chain.damage;
log.debug("attack_bonus = %hd (matched as->target_card_refs)", attack_bonus);
log.debug_f("attack_bonus = {} (matched as->target_card_refs)", attack_bonus);
ret = true;
break;
}
@@ -1415,26 +1396,26 @@ bool Card::unknown_80236554(shared_ptr<Card> other_card, const ActionState* as)
}
this->action_metadata.attack_bonus = max<int16_t>(attack_bonus, 0);
log.debug("attack_bonus = %hhd", this->action_metadata.attack_bonus);
log.debug_f("attack_bonus = {}", this->action_metadata.attack_bonus);
this->last_attack_preliminary_damage = 0;
this->last_attack_final_damage = 0;
log.debug("last attack damage stats cleared");
log.debug_f("last attack damage stats cleared");
if (other_card) {
log.debug("applying BEFORE_ANY_CARD_ATTACK conditions");
log.debug_f("applying BEFORE_ANY_CARD_ATTACK conditions");
s->card_special->apply_action_conditions(
EffectWhen::BEFORE_ANY_CARD_ATTACK, other_card, this->shared_from_this(), 0x20, as);
log.debug("applying BEFORE_THIS_CARD_ATTACKED conditions");
log.debug_f("applying BEFORE_THIS_CARD_ATTACKED conditions");
s->card_special->apply_action_conditions(
EffectWhen::BEFORE_THIS_CARD_ATTACKED, other_card, this->shared_from_this(), 0x40, as);
if (other_card->action_chain.check_flag(0x20000)) {
log.debug("attack_bonus cleared due to cancellation");
log.debug_f("attack_bonus cleared due to cancellation");
this->action_metadata.attack_bonus = 0;
return ret;
}
}
if (this->card_flags & 2) {
log.debug("attack_bonus cleared due to destruction");
log.debug_f("attack_bonus cleared due to destruction");
this->action_metadata.attack_bonus = 0;
}
return ret;
@@ -1464,7 +1445,7 @@ void Card::apply_attack_result() {
auto ps = this->player_state();
bool is_nte = s->options.is_nte();
auto log = s->log_stack(phosg::string_printf("apply_attack_result(@%04hX #%04hX): ", this->get_card_ref(), this->get_card_id()));
auto log = s->log_stack(std::format("apply_attack_result(@{:04X} #{:04X}): ", this->get_card_ref(), this->get_card_id()));
if (!this->action_chain.can_apply_attack()) {
return;
}
@@ -1573,9 +1554,9 @@ void Card::apply_attack_result() {
}
}
if (log.should_log(phosg::LogLevel::DEBUG)) {
if (log.should_log(phosg::LogLevel::L_DEBUG)) {
string as_str = as.str(s);
log.debug("as constructed as %s", as_str.c_str());
log.debug_f("as constructed as {}", as_str);
}
for (size_t z = 0; z < this->action_chain.chain.target_card_ref_count; z++) {
@@ -1583,36 +1564,36 @@ void Card::apply_attack_result() {
if (card) {
card->current_defense_power = card->action_metadata.attack_bonus;
if (!this->action_chain.check_flag(0x40)) {
log.debug("unknown_8024A6DC(@%04hX #%04hX) ...", card->get_card_ref(), card->get_card_id());
log.debug_f("unknown_8024A6DC(@{:04X} #{:04X}) ...", card->get_card_ref(), card->get_card_id());
s->card_special->unknown_8024A6DC(this->shared_from_this(), card);
}
}
}
log.debug("compute_action_chain_results 1 ...");
log.debug_f("compute_action_chain_results 1 ...");
this->compute_action_chain_results(true, false);
if (!this->action_chain.check_flag(0x40)) {
log.debug("apply_effects_before_attack ...");
log.debug_f("apply_effects_before_attack ...");
s->card_special->apply_effects_before_attack(this->shared_from_this());
}
if (!(this->card_flags & 2)) {
log.debug("compute_action_chain_results 2 ...");
log.debug_f("compute_action_chain_results 2 ...");
this->compute_action_chain_results(true, false);
log.debug("check_for_attack_interference ...");
log.debug_f("check_for_attack_interference ...");
s->card_special->check_for_attack_interference(this->shared_from_this());
}
log.debug("compute_action_chain_results 3 ...");
log.debug_f("compute_action_chain_results 3 ...");
this->compute_action_chain_results(true, false);
log.debug("unknown_80236374 ...");
log.debug_f("unknown_80236374 ...");
this->unknown_80236374(this->shared_from_this(), nullptr);
log.debug("execute_attack_on_all_valid_targets ...");
log.debug_f("execute_attack_on_all_valid_targets ...");
this->execute_attack_on_all_valid_targets(this->shared_from_this());
}
if (!this->action_chain.check_flag(0x40)) {
log.debug("apply_effects_after_attack ...");
log.debug_f("apply_effects_after_attack ...");
s->card_special->apply_effects_after_attack(this->shared_from_this());
}
ps->stats.num_attacks_given++;
@@ -1625,7 +1606,7 @@ void Card::apply_attack_result() {
for (size_t client_id = 0; client_id < 4; client_id++) {
auto ps = s->player_states[client_id];
if (ps) {
log.debug("unknown_8023C110(%zu) ...", client_id);
log.debug_f("unknown_8023C110({}) ...", client_id);
ps->unknown_8023C110();
}
}
File diff suppressed because it is too large Load Diff
+34 -94
View File
@@ -17,23 +17,10 @@ struct InterferenceProbabilityEntry {
};
const InterferenceProbabilityEntry* get_interference_probability_entry(
uint16_t row_card_id,
uint16_t column_card_id,
bool is_attack);
uint16_t row_card_id, uint16_t column_card_id, bool is_attack);
class CardSpecial {
public:
enum class ExpressionTokenType {
SPACE = 0, // Also used for end of string (get_next_expr_token returns null)
REFERENCE = 1, // Reference to a value from the env stats (e.g. hp)
NUMBER = 2, // Constant value (e.g. 2)
SUBTRACT = 3, // "-" in input string
ADD = 4, // "+" in input string
ROUND_DIVIDE = 5, // "/" in input string
FLOOR_DIVIDE = 6, // "//" in input string
MULTIPLY = 7, // "*" in input string
};
struct DiceRoll {
uint8_t client_id;
uint8_t unknown_a2;
@@ -77,9 +64,8 @@ public:
/* 70 */ uint32_t effective_ap_if_not_tech2; // "tt" in expr
/* 74 */ uint32_t team_dice_bonus; // "lv" in expr
/* 78 */ uint32_t sc_effective_ap; // "adm" in expr
// The following fields do not exist in Trial Edition. Because this struct
// is never sent to the client, we use the full struct even when playing
// Trial Edition, just for simplicity.
// The following fields do not exist in Trial Edition. Because this struct is never sent to the client, we use the
// full struct even when playing Trial Edition, just for simplicity.
/* 7C */ uint32_t attack_bonus; // "ddm" in expr
/* 80 */ uint32_t num_sword_type_items_on_team; // "sat" in expr
/* 84 */ uint32_t target_attack_bonus; // "edm" in expr
@@ -126,26 +112,14 @@ public:
uint32_t flags,
bool unknown_p8);
bool apply_defense_conditions(
const ActionState& as,
EffectWhen when,
std::shared_ptr<Card> defender_card,
uint32_t flags);
bool apply_stat_deltas_to_all_cards_from_all_conditions_with_card_ref(
uint16_t card_ref);
bool apply_stat_deltas_to_card_from_condition_and_clear_cond(
Condition& cond, std::shared_ptr<Card> card);
bool apply_stats_deltas_to_card_from_all_conditions_with_card_ref(
uint16_t card_ref, std::shared_ptr<Card> card);
const ActionState& as, EffectWhen when, std::shared_ptr<Card> defender_card, uint32_t flags);
bool apply_stat_deltas_to_all_cards_from_all_conditions_with_card_ref(uint16_t card_ref);
bool apply_stat_deltas_to_card_from_condition_and_clear_cond(Condition& cond, std::shared_ptr<Card> card);
bool apply_stats_deltas_to_card_from_all_conditions_with_card_ref(uint16_t card_ref, std::shared_ptr<Card> card);
bool card_has_condition_with_ref(
std::shared_ptr<const Card> card,
ConditionType cond_type,
uint16_t card_ref,
uint16_t match_card_ref) const;
std::shared_ptr<const Card> card, ConditionType cond_type, uint16_t card_ref, uint16_t match_card_ref) const;
bool card_is_destroyed(std::shared_ptr<const Card> card) const;
void compute_attack_ap(
std::shared_ptr<const Card> target_card,
int16_t* out_value,
uint16_t attacker_card_ref);
void compute_attack_ap(std::shared_ptr<const Card> target_card, int16_t* out_value, uint16_t attacker_card_ref);
AttackEnvStats compute_attack_env_stats(
const ActionState& pa,
std::shared_ptr<const Card> card,
@@ -166,21 +140,16 @@ public:
StatSwapType compute_stat_swap_type(std::shared_ptr<const Card> card) const;
void compute_team_dice_bonus(uint8_t team_id);
bool condition_applies_on_sc_or_item_attack(const Condition& cond) const;
size_t count_action_cards_with_condition_for_all_current_attacks(
ConditionType cond_type, uint16_t card_ref) const;
size_t count_action_cards_with_condition_for_all_current_attacks(ConditionType cond_type, uint16_t card_ref) const;
size_t count_action_cards_with_condition_for_current_attack(
std::shared_ptr<const Card> card, ConditionType cond_type, uint16_t card_ref) const;
size_t count_cards_with_card_id_except_card_ref(
uint16_t card_id, uint16_t card_ref) const;
size_t count_cards_with_card_id_except_card_ref(uint16_t card_id, uint16_t card_ref) const;
std::vector<std::shared_ptr<const Card>> get_all_set_cards_by_team_and_class(
CardClass card_class, uint8_t team_id, bool exclude_destroyed_cards) const;
ActionState create_attack_state_from_card_action_chain(
std::shared_ptr<const Card> attacker_card) const;
ActionState create_attack_state_from_card_action_chain(std::shared_ptr<const Card> attacker_card) const;
ActionState create_defense_state_for_card_pair_action_chains(
std::shared_ptr<const Card> attacker_card,
std::shared_ptr<const Card> defender_card) const;
void destroy_card_if_hp_zero(
std::shared_ptr<Card> card, uint16_t attacker_card_ref);
std::shared_ptr<const Card> attacker_card, std::shared_ptr<const Card> defender_card) const;
void destroy_card_if_hp_zero(std::shared_ptr<Card> card, uint16_t attacker_card_ref);
bool evaluate_effect_arg2_condition(
const ActionState& as,
std::shared_ptr<const Card> card,
@@ -190,10 +159,7 @@ public:
uint16_t sc_card_ref,
uint8_t random_percent,
EffectWhen when) const;
int32_t evaluate_effect_expr(
const AttackEnvStats& ast,
const char* expr,
DiceRoll& dice_roll) const;
int32_t evaluate_effect_expr(const AttackEnvStats& ast, const char* expr, DiceRoll& dice_roll) const;
bool execute_effect(
Condition& cond,
std::shared_ptr<Card> card,
@@ -208,25 +174,13 @@ public:
uint16_t set_card_ref,
uint8_t def_effect_index) const;
Condition* find_condition_with_parameters(
std::shared_ptr<Card> card,
ConditionType cond_type,
uint16_t set_card_ref,
uint8_t def_effect_index) const;
std::shared_ptr<Card> card, ConditionType cond_type, uint16_t set_card_ref, uint8_t def_effect_index) const;
static void get_card1_loc_with_card2_opposite_direction(
Location* out_loc,
std::shared_ptr<const Card> card1,
std::shared_ptr<const Card> card2);
Location* out_loc, std::shared_ptr<const Card> card1, std::shared_ptr<const Card> card2);
uint16_t get_card_id_with_effective_range(
std::shared_ptr<const Card> card1, uint16_t default_card_id, std::shared_ptr<const Card> card2) const;
static void get_effective_ap_tp(
StatSwapType type,
int16_t* effective_ap,
int16_t* effective_tp,
int16_t hp,
int16_t ap,
int16_t tp);
const char* get_next_expr_token(
const char* expr, ExpressionTokenType* out_type, int32_t* out_value) const;
StatSwapType type, int16_t* effective_ap, int16_t* effective_tp, int16_t hp, int16_t ap, int16_t tp);
std::vector<std::shared_ptr<const Card>> get_targeted_cards_for_condition(
uint16_t card_ref,
uint8_t def_effect_index,
@@ -244,18 +198,12 @@ public:
bool is_card_targeted_by_condition(
const Condition& cond, const ActionState& as, std::shared_ptr<const Card> card) const;
void on_card_set(std::shared_ptr<PlayerState> ps, uint16_t card_ref);
const CardDefinition::Effect* original_definition_for_condition(
const Condition& cond) const;
const CardDefinition::Effect* original_definition_for_condition(const Condition& cond) const;
bool card_ref_has_ability_trap(const Condition& eff) const;
void send_6xB4x06_for_exp_change(
std::shared_ptr<const Card> card,
uint16_t attacker_card_ref,
uint8_t dice_roll_value,
bool unknown_p5) const;
void send_6xB4x06_for_card_destroyed(
std::shared_ptr<const Card> destroyed_card, uint16_t attacker_card_ref) const;
uint16_t send_6xB4x06_if_card_ref_invalid(
uint16_t card_ref, int16_t value) const;
std::shared_ptr<const Card> card, uint16_t attacker_card_ref, uint8_t dice_roll_value, bool unknown_p5) const;
void send_6xB4x06_for_card_destroyed(std::shared_ptr<const Card> destroyed_card, uint16_t attacker_card_ref) const;
uint16_t send_6xB4x06_if_card_ref_invalid(uint16_t card_ref, int16_t value) const;
void send_6xB4x06_for_stat_delta(
std::shared_ptr<const Card> card,
uint16_t attacker_card_ref,
@@ -268,19 +216,14 @@ public:
std::shared_ptr<const Card> card,
uint16_t target_card_ref,
uint16_t sc_card_ref) const;
bool should_return_card_ref_to_hand_on_destruction(
uint16_t card_ref) const;
bool should_return_card_ref_to_hand_on_destruction(uint16_t card_ref) const;
size_t sum_last_attack_damage(
std::vector<std::shared_ptr<const Card>>* out_cards,
int32_t* out_damage_sum,
size_t* out_damage_count) const;
std::vector<std::shared_ptr<const Card>>* out_cards, int32_t* out_damage_sum, size_t* out_damage_count) const;
void update_condition_orders(std::shared_ptr<Card> card);
int16_t max_all_attack_bonuses(size_t* out_count) const;
void apply_effects_after_card_move(std::shared_ptr<Card> card);
void check_for_defense_interference(
std::shared_ptr<const Card> attacker_card,
std::shared_ptr<Card> target_card,
int16_t* inout_unknown_p4);
std::shared_ptr<const Card> attacker_card, std::shared_ptr<Card> target_card, int16_t* inout_unknown_p4);
void evaluate_and_apply_effects(
EffectWhen when,
uint16_t set_card_ref,
@@ -294,20 +237,19 @@ public:
ConditionType exclude_cond = ConditionType::NONE,
AssistEffect include_eff = AssistEffect::NONE,
AssistEffect exclude_eff = AssistEffect::NONE) const;
void clear_invalid_conditions_on_card(
std::shared_ptr<Card> card, const ActionState& as);
void on_card_destroyed(
std::shared_ptr<Card> attacker_card, std::shared_ptr<Card> destroyed_card);
std::vector<std::shared_ptr<const Card>> find_cards_in_hp_range(
int16_t min, int16_t max) const;
void clear_invalid_conditions_on_card(std::shared_ptr<Card> card, const ActionState& as);
void on_card_destroyed(std::shared_ptr<Card> attacker_card, std::shared_ptr<Card> destroyed_card);
std::vector<std::shared_ptr<const Card>> find_cards_in_hp_range(int16_t min, int16_t max) const;
std::vector<std::shared_ptr<const Card>> find_all_cards_by_aerial_attribute(bool is_aerial) const;
std::vector<std::shared_ptr<const Card>> find_cards_damaged_by_at_least(int16_t damage) const;
std::vector<std::shared_ptr<const Card>> find_all_set_cards_on_client_team(uint8_t client_id) const;
std::vector<std::shared_ptr<const Card>> find_all_cards_on_same_or_other_team(uint8_t client_id, bool same_team) const;
std::vector<std::shared_ptr<const Card>> find_all_cards_on_same_or_other_team(
uint8_t client_id, bool same_team) const;
std::shared_ptr<const Card> sc_card_for_client_id(uint8_t client_id) const;
std::shared_ptr<const Card> get_attacker_card(const ActionState& as) const;
std::vector<std::shared_ptr<const Card>> get_attacker_card_and_sc_if_item(const ActionState& as) const;
std::vector<std::shared_ptr<const Card>> find_all_set_cards_with_cost_in_range(uint8_t min_cost, uint8_t max_cost) const;
std::vector<std::shared_ptr<const Card>> find_all_set_cards_with_cost_in_range(
uint8_t min_cost, uint8_t max_cost) const;
std::vector<std::shared_ptr<const Card>> filter_cards_by_range(
const std::vector<std::shared_ptr<const Card>>& cards,
std::shared_ptr<const Card> card1,
@@ -334,10 +276,8 @@ public:
void apply_effects_before_attack(std::shared_ptr<Card> card);
void apply_effects_after_attack(std::shared_ptr<Card> card);
bool client_has_atk_dice_boost_condition(uint8_t client_id);
void unknown_8024A6DC(
std::shared_ptr<Card> unknown_p2, std::shared_ptr<Card> unknown_p3);
std::vector<std::shared_ptr<const Card>> find_all_sc_cards_of_class(
CardClass card_class) const;
void unknown_8024A6DC(std::shared_ptr<Card> unknown_p2, std::shared_ptr<Card> unknown_p3);
std::vector<std::shared_ptr<const Card>> find_all_sc_cards_of_class(CardClass card_class) const;
private:
std::weak_ptr<Server> w_server;
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+20 -27
View File
@@ -92,8 +92,7 @@ bool DeckState::draw_card_by_ref(uint16_t card_ref) {
auto& entry = this->entries[index];
if (entry.state == CardState::DISCARDED) {
// If the card is discarded, then it should be before the draw index, and we
// can just change its state.
// If the card is discarded, then it should be before the draw index, and we can just change its state.
entry.state = CardState::IN_HAND;
return true;
}
@@ -102,9 +101,8 @@ bool DeckState::draw_card_by_ref(uint16_t card_ref) {
return false;
}
// If the card is still drawable, we need to move it so it's just in front of
// the draw index, then immediately draw it. Ep3 NTE does not handle this
// case, but we do even when playing NTE.
// If the card is still drawable, we need to move it so it's just in front of the draw index, then immediately draw
// it. Ep3 NTE does not handle this case, but we do even when playing NTE.
size_t ref_index;
for (ref_index = 0; ref_index < this->card_refs.size(); ref_index++) {
if (this->card_refs[ref_index] == card_ref) {
@@ -131,13 +129,8 @@ uint16_t DeckState::card_id_for_card_ref(uint16_t card_ref) const {
if (card_ref == 0xFFFF) {
return 0xFFFF;
}
uint8_t index = index_for_card_ref(card_ref);
if (index < this->entries.size()) {
return this->entries[index].card_id;
} else {
return 0xFFFF;
}
return (index < this->entries.size()) ? this->entries[index].card_id : 0xFFFF;
}
uint16_t DeckState::sc_card_id() const {
@@ -167,8 +160,7 @@ void DeckState::restart() {
}
}
// For any cards that are still in hand or still in play, move their refs to
// the already-drawn part of the deck
// For any cards that are still in hand or still in play, move their refs to the already-drawn part of the deck
this->draw_index = 0;
for (size_t z = 0; z < this->entries.size(); z++) {
if (this->entries[z].state != CardState::DRAWABLE) {
@@ -187,7 +179,7 @@ void DeckState::restart() {
this->shuffle();
}
void DeckState::do_mulligan(bool is_nte) {
void DeckState::redraw_initial_hand(bool is_nte) {
for (size_t z = 0; z < this->entries.size(); z++) {
if (this->entries[z].state == CardState::DISCARDED) {
this->entries[z].state = CardState::DRAWABLE;
@@ -196,8 +188,7 @@ void DeckState::do_mulligan(bool is_nte) {
this->draw_index = 1;
if (is_nte || this->shuffle_enabled) {
// Get the next 5 cards from the deck, and put the previous 5 cards after
// them (so they will be shuffled back in).
// Get the next 5 cards from the deck, and put the previous 5 cards after them (so they will be shuffled back in).
for (uint8_t z = 0; z < 5; z++) {
uint8_t index = z + this->draw_index;
uint16_t temp_ref = this->card_refs[index];
@@ -274,11 +265,9 @@ void DeckState::shuffle() {
size_t max = this->num_drawable_cards();
for (size_t z = 0; z < this->card_refs.size(); z++) {
// Note: This is the way Sega originally implemented shuffling - they just
// do N swaps on the entire array. A more uniform way to do it would be to
// instead swap each item with another random item (possibly itself) that
// doesn't appear earlier than it in the array, but this is not what Sega
// did.
// Note: This is the way Sega originally implemented shuffling - they just do N swaps on the entire array. A more
// uniform way to do it would be to instead swap each item with another random item (possibly itself) that
// doesn't appear earlier than it in the array, but this is not what Sega did.
uint8_t index1 = this->draw_index + s->get_random(max);
uint8_t index2 = this->draw_index + s->get_random(max);
uint16_t temp_ref = this->card_refs[index1];
@@ -308,8 +297,12 @@ static const char* name_for_card_state(DeckState::CardState st) {
}
void DeckState::print(FILE* stream, std::shared_ptr<const CardIndex> card_index) const {
fprintf(stream, "DeckState: client_id=%hhu draw_index=%hhu card_ref_base=@%04hX shuffle=%s loop=%s\n",
this->client_id, this->draw_index, this->card_ref_base, this->shuffle_enabled ? "true" : "false", this->loop_enabled ? "true" : "false");
phosg::fwrite_fmt(stream, "DeckState: client_id={} draw_index={} card_ref_base=@{:04X} shuffle={} loop={}\n",
this->client_id,
this->draw_index,
this->card_ref_base,
this->shuffle_enabled ? "true" : "false",
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;
@@ -320,11 +313,11 @@ void DeckState::print(FILE* stream, std::shared_ptr<const CardIndex> card_index)
}
}
if (ce) {
string name = ce->def.en_name.decode(1);
fprintf(stream, " (%02zu) index=%02hhX ref=@%04hX card_id=#%04hX \"%s\" %s\n",
z, e.deck_index, this->card_refs[z], e.card_id, name.c_str(), name_for_card_state(e.state));
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));
} else {
fprintf(stream, " (%02zu) index=%02hhX ref=@%04hX card_id=#%04hX %s\n",
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));
}
}
+4 -8
View File
@@ -28,9 +28,8 @@ struct DeckEntry {
/* 00 */ pstring<TextEncoding::MARKED, 0x10> name;
/* 10 */ le_uint32_t team_id;
/* 14 */ parray<le_uint16_t, 31> card_ids;
// If the following flag is not set to 3, then the God Whim assist effect can
// use cards that are hidden from the player during deck building. The client
// always sets this to 3, and it's not clear why this even exists.
// If the following flag is not set to 3, then the God Whim assist effect can use cards that are hidden from the
// player during deck building. The client always sets this to 3, and it's not clear why this even exists.
/* 52 */ uint8_t god_whim_flag;
/* 53 */ uint8_t unused1;
/* 54 */ le_uint16_t player_level;
@@ -56,10 +55,7 @@ public:
};
template <typename CardIDT>
DeckState(
uint8_t client_id,
const parray<CardIDT, 0x1F>& card_ids,
std::shared_ptr<Server> server)
DeckState(uint8_t client_id, const parray<CardIDT, 0x1F>& card_ids, std::shared_ptr<Server> server)
: server(server),
client_id(client_id),
draw_index(1),
@@ -96,7 +92,7 @@ public:
void restart();
void shuffle();
void do_mulligan(bool is_nte);
void redraw_initial_hand(bool is_nte);
void print(FILE* stream, std::shared_ptr<const CardIndex> card_index = nullptr) const;
+2 -2
View File
@@ -20,11 +20,11 @@ void MapState::clear() {
}
void MapState::print(FILE* stream) const {
fprintf(stream, "[Map: w=%hu h=%hu]\n", this->width.load(), this->height.load());
phosg::fwrite_fmt(stream, "[Map: w={} h={}]\n", this->width, this->height);
for (size_t y = 0; y < this->height; y++) {
fputc(' ', stream);
for (size_t x = 0; x < this->width; x++) {
fprintf(stream, " %02hhX", this->tiles[y][x]);
phosg::fwrite_fmt(stream, " {:02X}", this->tiles[y][x]);
}
fputc('\n', stream);
}
+64 -111
View File
@@ -9,7 +9,7 @@ namespace Episode3 {
PlayerState::PlayerState(uint8_t client_id, shared_ptr<Server> server)
: w_server(server),
client_id(client_id),
num_mulligans_allowed(1),
num_hand_redraws_allowed(1),
sc_card_type(CardType::HUNTERS_SC),
team_id(0xFF),
atk_points(0),
@@ -110,9 +110,7 @@ void PlayerState::init() {
this->set_card_action_chains,
this->set_card_action_metadatas);
s->ruler_server->set_client_team_id(this->client_id, this->team_id);
s->card_special->on_card_set(this->shared_from_this(), this->sc_card_ref);
this->god_whim_can_use_hidden_cards = (s->deck_entries[this->client_id]->god_whim_flag != 3);
}
@@ -153,8 +151,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(shared_ptr<PlayerState> setter_ps) {
auto s = this->server();
uint16_t assist_card_id = this->set_assist_card_id;
@@ -163,8 +160,7 @@ void PlayerState::apply_assist_card_effect_on_set(
}
auto assist_effect = assist_effect_number_for_card_id(assist_card_id, s->options.is_nte());
if ((assist_effect == AssistEffect::RESISTANCE) ||
(assist_effect == AssistEffect::INDEPENDENT)) {
if ((assist_effect == AssistEffect::RESISTANCE) || (assist_effect == AssistEffect::INDEPENDENT)) {
this->assist_card_set_number = 0;
}
@@ -314,8 +310,7 @@ void PlayerState::apply_assist_card_effect_on_set(
auto other_ps = s->get_player_state(client_id);
if (other_ps.get() != this) {
other_ps->deck_state->draw_card_by_ref(this->card_refs[7]);
other_ps->set_card_from_hand(
this->card_refs[7], 0xF, nullptr, client_id, 1);
other_ps->set_card_from_hand(this->card_refs[7], 0xF, nullptr, client_id, 1);
}
}
break;
@@ -362,9 +357,7 @@ void PlayerState::apply_assist_card_effect_on_set(
}
for (ssize_t set_index = is_nte ? 0 : -1; set_index < 8; set_index++) {
auto card = (set_index == -1)
? other_ps->get_sc_card()
: other_ps->get_set_card(set_index);
auto card = (set_index == -1) ? other_ps->get_sc_card() : other_ps->get_set_card(set_index);
if (card) {
for (size_t cond_index = 0; cond_index < 9; cond_index++) {
auto& cond = card->action_chain.conditions[cond_index];
@@ -404,9 +397,7 @@ void PlayerState::apply_assist_card_effect_on_set(
}
for (ssize_t set_index = is_nte ? 0 : -1; set_index < 8; set_index++) {
auto card = (set_index == -1)
? other_ps->get_sc_card()
: other_ps->get_set_card(set_index);
auto card = (set_index == -1) ? other_ps->get_sc_card() : other_ps->get_set_card(set_index);
if (card) {
for (size_t cond_index = 0; cond_index < 9; cond_index++) {
auto& cond = card->action_chain.conditions[cond_index];
@@ -537,9 +528,9 @@ size_t PlayerState::count_set_cards() const {
return ret;
}
size_t PlayerState::count_set_refs() const {
size_t PlayerState::count_hand_refs() const {
size_t ret = 0;
for (size_t set_index = 0; set_index < 8; set_index++) {
for (size_t set_index = 8; set_index < 16; set_index++) {
if (this->card_refs[set_index] != 0xFFFF) {
ret++;
}
@@ -577,8 +568,7 @@ void PlayerState::discard_all_attack_action_cards_from_hand() {
for (size_t hand_index = 0; hand_index < 6; hand_index++) {
uint16_t card_ref = temp_card_refs[hand_index];
auto ce = s->definition_for_card_ref(card_ref);
if (ce && (ce->def.type == CardType::ACTION) &&
(ce->def.card_class() != CardClass::DEFENSE_ACTION)) {
if (ce && (ce->def.type == CardType::ACTION) && (ce->def.card_class() != CardClass::DEFENSE_ACTION)) {
this->discard_ref_from_hand(card_ref);
}
}
@@ -705,14 +695,14 @@ void PlayerState::discard_set_assist_card() {
s->destroy_cards_with_zero_hp();
}
bool PlayerState::do_mulligan() {
if (!this->is_mulligan_allowed()) {
bool PlayerState::redraw_initial_hand() {
if (!this->is_hand_redraw_allowed()) {
return false;
}
auto s = this->server();
this->num_mulligans_allowed--;
this->num_hand_redraws_allowed--;
while (this->card_refs[0] != 0xFFFF) {
this->discard_ref_from_hand(this->card_refs[0]);
}
@@ -727,7 +717,7 @@ bool PlayerState::do_mulligan() {
s->send(cmd);
}
this->deck_state->do_mulligan(s->options.is_nte());
this->deck_state->redraw_initial_hand(s->options.is_nte());
this->draw_hand(5);
if (!s->options.is_nte()) {
@@ -771,9 +761,8 @@ void PlayerState::draw_hand(ssize_t override_count) {
}
void PlayerState::draw_initial_hand() {
// Note: The original code called this->deck_state->init_card_states here, but
// we don't because that logic is now in the DeckState constructor, and this
// function should only be called during PlayerState construction (so, shortly
// Note: The original code called this->deck_state->init_card_states here, but we don't because that logic is now in
// the DeckState constructor, and this function should only be called during PlayerState construction (so, shortly
// after DeckState construction as well).
this->deck_state->restart();
this->card_refs.clear(0xFFFF);
@@ -782,10 +771,7 @@ void PlayerState::draw_initial_hand() {
}
int32_t PlayerState::error_code_for_client_setting_card(
uint16_t card_ref,
uint8_t card_index,
const Location* loc,
uint8_t assist_target_client_id) const {
uint16_t card_ref, uint8_t card_index, const Location* loc, uint8_t assist_target_client_id) const {
auto s = this->server();
int32_t code = s->ruler_server->error_code_for_client_setting_card(
@@ -816,8 +802,7 @@ int32_t PlayerState::error_code_for_client_setting_card(
if (this->card_refs[card_index + 1] != 0xFFFF) {
return -0x7E;
}
if ((ce->def.type == CardType::CREATURE) &&
!s->map_and_rules->tile_is_vacant(loc->x, loc->y)) {
if ((ce->def.type == CardType::CREATURE) && !s->map_and_rules->tile_is_vacant(loc->x, loc->y)) {
return -0x7A;
}
return 0;
@@ -834,20 +819,17 @@ int32_t PlayerState::error_code_for_client_setting_card(
}
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 {
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("loc=%s, target_team_id=%02hhX", loc_str.c_str(), target_team_id);
log.debug_f("loc={}, target_team_id={:02X}", loc_str, target_team_id);
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()))) {
if (other_ps && ((target_team_id == 0xFF) || (target_team_id == other_ps->get_team_id()))) {
auto card_refs = get_card_refs_within_range(range, loc, *other_ps->card_short_statuses, &log);
ret.insert(ret.end(), card_refs.begin(), card_refs.end());
}
@@ -939,15 +921,14 @@ size_t PlayerState::set_index_for_card_ref(uint16_t card_ref) const {
return -1;
}
bool PlayerState::is_mulligan_allowed() const {
return (this->num_mulligans_allowed > 0);
bool PlayerState::is_hand_redraw_allowed() const {
return (this->num_hand_redraws_allowed > 0);
}
bool PlayerState::is_team_turn() const {
auto s = this->server();
// Note: The original code checks if this->w_server is null before doing this.
// We don't check because that should never happen, and server() will throw if
// it does.
// Note: The original code checks if this->w_server is null before doing this. We don't check because that should
// never happen, and server() will throw if it does.
return s->get_current_team_turn() == this->team_id;
}
@@ -961,9 +942,8 @@ void PlayerState::log_discard(uint16_t card_ref, uint16_t reason) {
}
uint16_t PlayerState::pop_from_discard_log(uint16_t) {
// NTE appears to have a bug here (or some obviated code): it searches for an
// entry with the given reason, then ignores the result of that search and
// always returns the first entry instead.
// NTE appears to have a bug here (or some obviated code): it searches for an entry with the given reason, then
// ignores the result of that search and always returns the first entry instead. That code is:
// size_t z;
// for (size_t z = 0; z < this->discard_log_card_refs.size(); z++) {
// if ((this->discard_log_card_refs[z] != 0xFFFF) && (this->discard_log_reasons[z] == reason)) {
@@ -1030,9 +1010,7 @@ void PlayerState::move_null_hand_refs_to_end() {
void PlayerState::on_cards_destroyed() {
auto s = this->server();
// {card_ref: should_return_to_hand}
unordered_multimap<uint16_t, bool> card_refs_map;
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)) {
@@ -1106,8 +1084,7 @@ void PlayerState::replace_all_set_assists_with_random_assists() {
const auto& assist_card_ids = all_assist_card_ids(is_nte);
for (size_t client_id = 0; client_id < 4; client_id++) {
auto other_ps = s->get_player_state(client_id);
if (other_ps &&
((other_ps->card_refs[6] != 0xFFFF) || (!is_nte && (other_ps->set_assist_card_id != 0xFFFF)))) {
if (other_ps && ((other_ps->card_refs[6] != 0xFFFF) || (!is_nte && (other_ps->set_assist_card_id != 0xFFFF)))) {
uint16_t card_id = 0x0130;
while (card_id == 0x0130) { // God Whim
size_t index = s->get_random(assist_card_ids.size());
@@ -1355,8 +1332,7 @@ bool PlayerState::set_card_from_hand(
return 0;
}
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?
// 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);
auto new_card = this->set_cards[card_index - 7];
new_card->init();
@@ -1365,8 +1341,7 @@ bool PlayerState::set_card_from_hand(
new_card->loc.x = loc->x;
new_card->loc.y = loc->y;
}
// Note: NTE doesn't track this, but NTE can't use it anyway, so we don't
// check for NTE here.
// Note: NTE doesn't track this, but NTE can't use it anyway, so we don't check for NTE here.
this->stats.num_item_or_creature_cards_set++;
} else if (ce->def.type == CardType::ASSIST) {
@@ -1436,7 +1411,6 @@ bool PlayerState::set_card_from_hand(
void PlayerState::set_initial_location() {
auto s = this->server();
auto mr = s->map_and_rules;
uint8_t num_team_players;
@@ -1485,8 +1459,7 @@ void PlayerState::set_initial_location() {
}
}
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(shared_ptr<const Card> card) {
if (!card) {
return;
}
@@ -1498,8 +1471,7 @@ void PlayerState::set_map_occupied_bit_for_card_on_warp_tile(
if ((s->warp_positions[warp_type][warp_end][0] == card->loc.x) &&
(s->warp_positions[warp_type][warp_end][1] == card->loc.y)) {
s->map_and_rules->set_occupied_bit_for_tile(
s->warp_positions[warp_type][warp_end ^ 1][0],
s->warp_positions[warp_type][warp_end ^ 1][1]);
s->warp_positions[warp_type][warp_end ^ 1][0], s->warp_positions[warp_type][warp_end ^ 1][1]);
}
}
}
@@ -1509,8 +1481,7 @@ void PlayerState::set_map_occupied_bits_for_sc_and_creatures() {
auto s = this->server();
if (this->sc_card && !(this->sc_card->card_flags & 2)) {
s->map_and_rules->set_occupied_bit_for_tile(
this->sc_card->loc.x, this->sc_card->loc.y);
s->map_and_rules->set_occupied_bit_for_tile(this->sc_card->loc.x, this->sc_card->loc.y);
this->set_map_occupied_bit_for_card_on_warp_tile(this->sc_card);
}
@@ -1518,8 +1489,7 @@ void PlayerState::set_map_occupied_bits_for_sc_and_creatures() {
for (size_t set_index = 0; set_index < 8; set_index++) {
auto card = this->set_cards[set_index];
if (card) {
s->map_and_rules->set_occupied_bit_for_tile(
card->loc.x, card->loc.y);
s->map_and_rules->set_occupied_bit_for_tile(card->loc.x, card->loc.y);
this->set_map_occupied_bit_for_card_on_warp_tile(card);
}
}
@@ -1530,8 +1500,7 @@ void PlayerState::subtract_def_points(uint8_t cost) {
this->def_points -= cost;
}
bool PlayerState::subtract_or_check_atk_or_def_points_for_action(
const ActionState& pa, bool deduct_points) {
bool PlayerState::subtract_or_check_atk_or_def_points_for_action(const ActionState& pa, bool deduct_points) {
auto s = this->server();
int16_t cost = this->compute_attack_or_defense_atk_costs(pa);
@@ -1580,9 +1549,7 @@ G_UpdateHand_Ep3_6xB4x02 PlayerState::prepare_6xB4x02() const {
cmd.state.assist_card_ref = this->card_refs[6];
cmd.state.sc_card_ref = this->sc_card_ref;
cmd.state.assist_card_ref2 = this->card_refs[6];
cmd.state.assist_card_set_number = (this->card_refs[6] == 0xFFFF)
? 0
: this->assist_card_set_number;
cmd.state.assist_card_set_number = (this->card_refs[6] == 0xFFFF) ? 0 : this->assist_card_set_number;
cmd.state.assist_card_id = this->set_assist_card_id;
cmd.state.assist_remaining_turns = this->assist_remaining_turns;
cmd.state.assist_delay_turns = this->assist_delay_turns;
@@ -1591,8 +1558,7 @@ G_UpdateHand_Ep3_6xB4x02 PlayerState::prepare_6xB4x02() const {
return cmd;
}
void PlayerState::update_hand_and_equip_state_and_send_6xB4x02_if_needed(
bool always_send) {
void PlayerState::update_hand_and_equip_state_and_send_6xB4x02_if_needed(bool always_send) {
auto cmd = this->prepare_6xB4x02();
if (always_send || memcmp(&this->hand_and_equip, &cmd.state, sizeof(this->hand_and_equip))) {
*this->hand_and_equip = cmd.state;
@@ -1618,8 +1584,7 @@ void PlayerState::set_random_assist_card_from_hand_for_free() {
if (!candidate_card_refs.empty()) {
this->discard_set_assist_card();
size_t index = s->get_random(candidate_card_refs.size());
this->set_card_from_hand(
candidate_card_refs[index], 15, nullptr, this->client_id, 1);
this->set_card_from_hand(candidate_card_refs[index], 15, nullptr, this->client_id, 1);
}
}
@@ -1641,11 +1606,9 @@ G_UpdateShortStatuses_Ep3_6xB4x04 PlayerState::prepare_6xB4x04() const {
}
for (size_t hand_index = 0; hand_index < 6; hand_index++) {
this->get_short_status_for_card_index_in_hand(
hand_index + 1, &cmd.card_statuses[hand_index + 1]);
// This write is required to mimic memset()'s effect from the original code.
// This field is probably ignored for hand refs anyway, but we might as well
// be as consistent as possible.
this->get_short_status_for_card_index_in_hand(hand_index + 1, &cmd.card_statuses[hand_index + 1]);
// This write is required to mimic memset()'s effect from the original code. This field is probably ignored for
// hand refs anyway, but we might as well be as consistent as possible.
cmd.card_statuses[hand_index + 1].unused1 = 0;
}
@@ -1674,9 +1637,7 @@ void PlayerState::send_6xB4x04_if_needed(bool always_send) {
}
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 {
const parray<uint8_t, 9 * 9>& range, const Location& loc, CardType type) const {
auto s = this->server();
vector<uint16_t> ret;
@@ -1726,8 +1687,7 @@ void PlayerState::move_phase_before() {
void PlayerState::handle_before_turn_assist_effects() {
auto s = this->server();
if ((this->assist_delay_turns > 0) &&
(--this->assist_delay_turns == 0)) {
if ((this->assist_delay_turns > 0) && (--this->assist_delay_turns == 0)) {
this->update_hand_and_equip_state_and_send_6xB4x02_if_needed();
size_t num_assists = s->assist_server->compute_num_assist_effects_for_client(this->client_id);
for (size_t z = 0; z < num_assists; z++) {
@@ -1736,8 +1696,7 @@ void PlayerState::handle_before_turn_assist_effects() {
s->execute_bomb_assist_effect();
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?
// 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->update_hand_and_equip_state_and_send_6xB4x02_if_needed();
break;
@@ -1766,20 +1725,20 @@ bool PlayerState::set_action_cards_for_action_state(const ActionState& pa) {
auto attacker_card = s->card_for_set_card_ref(pa.attacker_card_ref);
if (attacker_card) {
log.debug("attacker card present");
log.debug_f("attacker card present");
attacker_card->card_flags |= 0x100;
}
auto action_type = s->ruler_server->get_pending_action_type(pa);
if (action_type == ActionType::DEFENSE) {
log.debug("action type is DEFENSE");
log.debug_f("action type is DEFENSE");
} else if (action_type == ActionType::ATTACK) {
log.debug("action type is ATTACK");
log.debug_f("action type is ATTACK");
} else {
log.debug("action type is UNKNOWN");
log.debug_f("action type is UNKNOWN");
}
if (!is_nte) {
log.debug("(non-nte) subtracting action points");
log.debug_f("(non-nte) subtracting action points");
this->subtract_or_check_atk_or_def_points_for_action(pa, true);
}
@@ -1787,7 +1746,7 @@ bool PlayerState::set_action_cards_for_action_state(const ActionState& pa) {
auto card = s->card_for_set_card_ref(pa.attacker_card_ref);
if (card) {
card->loc.direction = pa.facing_direction;
log.debug("set facing direction to %s", phosg::name_for_enum(card->loc.direction));
log.debug_f("set facing direction to {}", phosg::name_for_enum(card->loc.direction));
G_AddToSetCardLog_Ep3_6xB4x4A cmd;
cmd.card_refs.clear(0xFFFF);
@@ -1796,9 +1755,9 @@ bool PlayerState::set_action_cards_for_action_state(const ActionState& pa) {
cmd.entry_count = 0;
size_t z = 0;
do {
if (log.should_log(phosg::LogLevel::DEBUG)) {
if (log.should_log(phosg::LogLevel::L_DEBUG)) {
string ref_str = s->debug_str_for_card_ref(pa.action_card_refs[z]);
log.debug("on action card ref %s", ref_str.c_str());
log.debug_f("on action card ref {}", ref_str);
}
card->unknown_80237A90(pa, pa.action_card_refs[z]);
card->unknown_802379BC(pa.action_card_refs[z]);
@@ -1833,9 +1792,9 @@ bool PlayerState::set_action_cards_for_action_state(const ActionState& pa) {
for (size_t z = 0; (z < 4 * 9) && (pa.target_card_refs[z] != 0xFFFF); z++) {
auto target_card = s->card_for_set_card_ref(pa.target_card_refs[z]);
if (target_card) {
if (log.should_log(phosg::LogLevel::DEBUG)) {
if (log.should_log(phosg::LogLevel::L_DEBUG)) {
string ref_str = s->debug_str_for_card_ref(pa.target_card_refs[z]);
log.debug("on target card ref %s", ref_str.c_str());
log.debug_f("on target card ref {}", ref_str);
}
target_card->unknown_802379DC(pa);
if (!is_nte) {
@@ -1859,13 +1818,13 @@ bool PlayerState::set_action_cards_for_action_state(const ActionState& pa) {
}
}
if (is_nte) {
log.debug("(nte) subtracting action points");
log.debug_f("(nte) subtracting action points");
this->subtract_or_check_atk_or_def_points_for_action(pa, 1);
}
for (size_t z = 0; (z < pa.action_card_refs.size()) && (pa.action_card_refs[z] != 0xFFFF); z++) {
if (log.should_log(phosg::LogLevel::DEBUG)) {
if (log.should_log(phosg::LogLevel::L_DEBUG)) {
string ref_str = s->debug_str_for_card_ref(pa.action_card_refs[z]);
log.debug("discarding %s from hand", ref_str.c_str());
log.debug_f("discarding {} from hand", ref_str);
}
this->discard_ref_from_hand(pa.action_card_refs[z]);
}
@@ -1885,9 +1844,7 @@ void PlayerState::dice_phase_before() {
this->compute_total_set_cards_cost();
this->unknown_a14 = 0;
if ((this->assist_remaining_turns > 0) &&
(this->assist_remaining_turns < 90) &&
(this->assist_delay_turns == 0)) {
if ((this->assist_remaining_turns > 0) && (this->assist_remaining_turns < 90) && (this->assist_delay_turns == 0)) {
this->assist_remaining_turns--;
if (this->assist_remaining_turns < 1) {
this->discard_set_assist_card();
@@ -1984,12 +1941,10 @@ void PlayerState::roll_main_dice_or_apply_after_effects() {
auto s = this->server();
const auto& rules = s->map_and_rules->rules;
// In NTE, the dice behave differently - there is no minimum, and instead the
// player can specify a fixed value for each die or a random value (1-6). The
// implementation of this function is therefore quite different on NTE, but
// since we already support custom ranges for ATK and DEF dice, we just use
// the non-NTE logic and assign the dice ranges at battle start time to yield
// the NTE behavior. (See RulesTrial in DataIndexes.cc for how this is done.)
// In NTE, the dice behave differently - there is no minimum, and instead the player can specify a fixed value for
// each die or a random value (1-6). The implementation of this function is therefore quite different on NTE, but
// since we already support custom ranges for ATK and DEF dice, we just use the non-NTE logic and assign the dice
// ranges at battle start time to yield the NTE behavior. (See RulesTrial in DataIndexes.cc for how this is done.)
bool is_1p_2v1 = (s->team_client_count.at(this->get_team_id()) < s->team_client_count[this->get_team_id() ^ 1]);
@@ -2079,10 +2034,8 @@ 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);
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);
this->update_hand_and_equip_state_and_send_6xB4x02_if_needed();
}
+19 -27
View File
@@ -15,9 +15,8 @@ namespace Episode3 {
class Server;
enum AssistFlag : uint32_t {
// Note: This enum is a uint32_t even though only 16 bits are used because
// the corresponding field in the protocol is a 32-bit field. There may also
// be bits used only by the client which are not documented here.
// Note: This enum is a uint32_t even though only 16 bits are used because the corresponding field in the protocol is
// a 32-bit field. There may also be bits used only by the client which are not documented here.
// clang-format off
NONE = 0x0000,
@@ -57,7 +56,7 @@ public:
void compute_total_set_cards_cost();
size_t count_set_cards_for_env_stats_nte() const;
size_t count_set_cards() const;
size_t count_set_refs() const;
size_t count_hand_refs() const;
void discard_all_assist_cards_from_hand();
void discard_all_attack_action_cards_from_hand();
void discard_all_item_and_creature_cards_from_hand();
@@ -66,18 +65,13 @@ public:
void discard_random_hand_card();
bool discard_ref_from_hand(uint16_t card_ref);
void discard_set_assist_card();
bool do_mulligan();
bool redraw_initial_hand();
void draw_hand(ssize_t override_count = 0);
void draw_initial_hand();
int32_t error_code_for_client_setting_card(
uint16_t card_ref,
uint8_t card_index,
const Location* loc,
uint8_t assist_target_client_id) const;
uint16_t card_ref, uint8_t card_index, const Location* loc, uint8_t assist_target_client_id) const;
std::vector<uint16_t> get_all_cards_within_range(
const parray<uint8_t, 9 * 9>& range,
const Location& loc,
uint8_t target_team_id) const;
const parray<uint8_t, 9 * 9>& range, const Location& loc, uint8_t target_team_id) const;
uint8_t get_atk_points() const;
void get_short_status_for_card_index_in_hand(size_t hand_index, CardShortStatus* stat) const;
std::shared_ptr<DeckState> get_deck();
@@ -95,7 +89,7 @@ public:
uint8_t get_team_id() const;
ssize_t hand_index_for_card_ref(uint16_t card_ref) const;
size_t set_index_for_card_ref(uint16_t card_ref) const;
bool is_mulligan_allowed() const;
bool is_hand_redraw_allowed() const;
bool is_team_turn() const;
void log_discard(uint16_t card_ref, uint16_t reason);
uint16_t pop_from_discard_log(uint16_t reason);
@@ -128,9 +122,7 @@ public:
G_UpdateShortStatuses_Ep3_6xB4x04 prepare_6xB4x04() const;
void send_6xB4x04_if_needed(bool always_send = false);
std::vector<uint16_t> get_card_refs_within_range_from_all_players(
const parray<uint8_t, 9 * 9>& range,
const Location& loc,
CardType type) const;
const parray<uint8_t, 9 * 9>& range, const Location& loc, CardType type) const;
void draw_phase_before();
void action_phase_before();
void move_phase_before();
@@ -152,7 +144,7 @@ public:
std::shared_ptr<Card> sc_card;
bcarray<std::shared_ptr<Card>, 8> set_cards;
uint8_t client_id;
uint16_t num_mulligans_allowed;
uint16_t num_hand_redraws_allowed;
CardType sc_card_type;
uint8_t team_id;
uint8_t atk_points;
@@ -169,10 +161,10 @@ public:
uint16_t sc_card_ref;
// This array is unfortunately heterogeneous; specifically:
// [0] through [5] are hand refs
// [6] is the current assist card ref (which may belong to another player)
// [7] is the previous assist card ref
// [8] through [15] are set refs
// [0] through [5] are hand refs
// [6] is the current assist card ref (which may belong to another player)
// [7] is the previous assist card ref
// [8] through [15] are set refs
parray<uint16_t, 0x10> card_refs;
std::shared_ptr<DeckState> deck_state;
@@ -190,12 +182,12 @@ public:
Direction start_facing_direction;
std::shared_ptr<HandAndEquipState> hand_and_equip;
// Like card_refs above, these arrays are also heterogeneous, but the indices
// are not the same as for card_refs! THe indices here are:
// [0] is the SC card status
// [1] through [6] are hand cards
// [7] through [14] are set cards
// [15] is the assist card
// Like card_refs above, these arrays are also heterogeneous, but the indices are not the same as for card_refs! The
// indices here are:
// [0] is the SC card status
// [1] through [6] are hand cards
// [7] through [14] are set cards
// [15] is the assist card
std::shared_ptr<parray<CardShortStatus, 0x10>> card_short_statuses;
parray<CardShortStatus, 0x10> prev_card_short_statuses;
+96 -149
View File
@@ -62,21 +62,19 @@ void Condition::clear_FF() {
}
std::string Condition::str(shared_ptr<const Server> s) const {
auto card_ref_str = s->debug_str_for_card_ref(this->card_ref);
auto giver_ref_str = s->debug_str_for_card_ref(this->condition_giver_card_ref);
return phosg::string_printf(
"Condition[type=%s, turns=%hhu, a_arg=%hhd, dice=%hhu, flags=%02hhX, "
"def_eff_index=%hhu, ref=%s, value=%hd, giver_ref=%s "
"percent=%hhu value8=%hd order=%hu a8=%hu]",
return std::format(
"Condition[type={}, turns={}, a_arg={}, dice={}, flags={:02X}, "
"def_eff_index={}, ref={}, value={}, giver_ref={} "
"percent={} value8={} order={} a8={}]",
phosg::name_for_enum(this->type),
this->remaining_turns,
this->a_arg_value,
this->dice_roll_value,
this->flags,
this->card_definition_effect_index,
card_ref_str.c_str(),
this->value.load(),
giver_ref_str.c_str(),
s->debug_str_for_card_ref(this->card_ref),
this->value,
s->debug_str_for_card_ref(this->condition_giver_card_ref),
this->random_percent,
this->value8,
this->order,
@@ -101,14 +99,10 @@ void EffectResult::clear() {
}
std::string EffectResult::str(shared_ptr<const Server> s) const {
string attacker_ref_str = s->debug_str_for_card_ref(this->attacker_card_ref);
string target_ref_str = s->debug_str_for_card_ref(this->target_card_ref);
return phosg::string_printf(
"EffectResult[att_ref=%s, target_ref=%s, value=%hhd, "
"cur_hp=%hhd, ap=%hhd, tp=%hhd, flags=%02hhX, op=%hhd, "
"cond_index=%hhu, dice=%hhu]",
attacker_ref_str.c_str(),
target_ref_str.c_str(),
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),
s->debug_str_for_card_ref(this->target_card_ref),
this->value,
this->current_hp,
this->ap,
@@ -137,16 +131,13 @@ bool CardShortStatus::operator!=(const CardShortStatus& other) const {
}
std::string CardShortStatus::str(shared_ptr<const Server> s) const {
string loc_s = this->loc.str();
string ref_str = s->debug_str_for_card_ref(this->card_ref);
return phosg::string_printf(
"CardShortStatus[ref=%s, cur_hp=%hd, flags=%08" PRIX32 ", loc=%s, "
"u1=%04hX, max_hp=%hhd, u2=%hhu]",
ref_str.c_str(),
this->current_hp.load(),
this->card_flags.load(),
loc_s.c_str(),
this->unused1.load(),
return std::format(
"CardShortStatus[ref={}, cur_hp={}, flags={:08X}, loc={}, u1={:04X}, max_hp={}, u2={}]",
s->debug_str_for_card_ref(this->card_ref),
this->current_hp,
this->card_flags,
this->loc.str(),
this->unused1,
this->max_hp,
this->unused2);
}
@@ -188,23 +179,16 @@ void ActionState::clear() {
}
std::string ActionState::str(shared_ptr<const Server> s) const {
string attacker_ref_s = s->debug_str_for_card_ref(this->attacker_card_ref);
string defense_ref_s = s->debug_str_for_card_ref(this->defense_card_ref);
string original_attacker_ref_s = s->debug_str_for_card_ref(this->original_attacker_card_ref);
string target_refs_s = s->debug_str_for_card_refs(this->target_card_refs);
string action_refs_s = s->debug_str_for_card_refs(this->action_card_refs);
return phosg::string_printf(
"ActionState[client=%hX, u=%hhu, facing=%s, attacker_ref=%s, "
"def_ref=%s, target_refs=%s, action_refs=%s, "
"orig_attacker_ref=%s]",
this->client_id.load(),
return std::format(
"ActionState[client={:X}, u={}, facing={}, attacker_ref={}, def_ref={}, target_refs={}, action_refs={}, orig_attacker_ref={}]",
this->client_id,
this->unused,
phosg::name_for_enum(this->facing_direction),
attacker_ref_s.c_str(),
defense_ref_s.c_str(),
target_refs_s.c_str(),
action_refs_s.c_str(),
original_attacker_ref_s.c_str());
s->debug_str_for_card_ref(this->attacker_card_ref),
s->debug_str_for_card_ref(this->defense_card_ref),
s->debug_str_for_card_refs(this->target_card_refs),
s->debug_str_for_card_refs(this->action_card_refs),
s->debug_str_for_card_ref(this->original_attacker_card_ref));
}
ActionChain::ActionChain() {
@@ -239,24 +223,17 @@ bool ActionChain::operator!=(const ActionChain& other) const {
}
std::string ActionChain::str(shared_ptr<const Server> s) const {
string acting_card_ref_s = s->debug_str_for_card_ref(this->acting_card_ref);
string unknown_card_ref_a3_s = s->debug_str_for_card_ref(this->unknown_card_ref_a3);
string attack_action_card_refs_s = s->debug_str_for_card_refs(this->attack_action_card_refs);
string target_card_refs_s = s->debug_str_for_card_refs(this->target_card_refs);
return phosg::string_printf(
"ActionChain[eff_ap=%hhd, eff_tp=%hhd, ap_bonus=%hhd, damage=%hhd, "
"acting_ref=%s, unknown_ref_a3=%s, attack_action_refs=%s, "
"attack_action_ref_count=%hhu, medium=%s, target_ref_count=%hhu, "
"subphase=%s, strikes=%hhu, damage_mult=%hhd, attack_num=%hhu, "
"tp_bonus=%hhd, phys_bonus_nte=%hhu, tech_bonus_nte=%hhu, card_ap=%hhd, "
"card_tp=%hhd, flags=%08" PRIX32 ", target_refs=%s]",
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={}, "
"tp_bonus={}, phys_bonus_nte={}, tech_bonus_nte={}, card_ap={}, card_tp={}, flags={:08X}, target_refs={}]",
this->effective_ap,
this->effective_tp,
this->ap_effect_bonus,
this->damage,
acting_card_ref_s.c_str(),
unknown_card_ref_a3_s.c_str(),
attack_action_card_refs_s.c_str(),
s->debug_str_for_card_ref(this->acting_card_ref),
s->debug_str_for_card_ref(this->unknown_card_ref_a3),
s->debug_str_for_card_refs(this->attack_action_card_refs),
this->attack_action_card_ref_count,
phosg::name_for_enum(this->attack_medium),
this->target_card_ref_count,
@@ -269,8 +246,8 @@ std::string ActionChain::str(shared_ptr<const Server> s) const {
this->tech_attack_bonus_nte,
this->card_ap,
this->card_tp,
this->flags.load(),
target_card_refs_s.c_str());
this->flags,
s->debug_str_for_card_refs(this->target_card_refs));
}
void ActionChain::clear() {
@@ -341,7 +318,7 @@ std::string ActionChainWithConds::str(shared_ptr<const Server> s) const {
if (ret.back() != '[') {
ret += ", ";
}
ret += phosg::string_printf("%zu:", z);
ret += std::format("{}:", z);
ret += this->conditions[z].str(s);
}
}
@@ -406,8 +383,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, shared_ptr<Server> server) {
if (card_ref != 0xFFFF) {
this->chain.attack_action_card_refs[this->chain.attack_action_card_ref_count++] = card_ref;
}
@@ -416,8 +392,7 @@ void ActionChainWithConds::add_attack_action_card_ref(
}
void ActionChainWithConds::add_target_card_ref(uint16_t card_ref) {
if (card_ref != 0xFFFF &&
this->chain.target_card_ref_count < this->chain.target_card_refs.size()) {
if (card_ref != 0xFFFF && this->chain.target_card_ref_count < this->chain.target_card_refs.size()) {
this->chain.target_card_refs[this->chain.target_card_ref_count++] = card_ref;
}
}
@@ -440,11 +415,7 @@ void ActionChainWithConds::compute_attack_medium(shared_ptr<Server> server) {
}
bool ActionChainWithConds::get_condition_value(
ConditionType cond_type,
uint16_t card_ref,
uint8_t def_effect_index,
uint16_t value,
uint16_t* out_value) const {
ConditionType cond_type, uint16_t card_ref, uint8_t def_effect_index, uint16_t value, uint16_t* out_value) const {
bool any_found = false;
uint8_t max_order = 10;
for (size_t z = 0; z < 9; z++) {
@@ -466,8 +437,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(shared_ptr<const Card> card) {
this->chain.action_subphase = card->server()->get_current_action_subphase();
}
@@ -576,26 +546,20 @@ bool ActionMetadata::operator!=(const ActionMetadata& other) const {
}
std::string ActionMetadata::str(shared_ptr<const Server> s) const {
string card_ref_s = s->debug_str_for_card_ref(this->card_ref);
string target_card_refs_s = s->debug_str_for_card_refs(this->target_card_refs);
string defense_card_refs_s = s->debug_str_for_card_refs(this->defense_card_refs);
string original_attacker_card_refs_s = s->debug_str_for_card_refs(this->original_attacker_card_refs);
return phosg::string_printf(
"ActionMetadata[ref=%s, target_ref_count=%hhu, def_ref_count=%hhu, "
"subphase=%s, def_power=%hhd, def_bonus=%hhd, "
"att_bonus=%hhd, flags=%08" PRIX32 ", target_refs=%s, "
"defense_refs=%s, original_attacker_refs=%s]",
card_ref_s.c_str(),
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={}]",
s->debug_str_for_card_ref(this->card_ref),
this->target_card_ref_count,
this->defense_card_ref_count,
phosg::name_for_enum(this->action_subphase),
this->defense_power,
this->defense_bonus,
this->attack_bonus,
this->flags.load(),
target_card_refs_s.c_str(),
defense_card_refs_s.c_str(),
original_attacker_card_refs_s.c_str());
this->flags,
s->debug_str_for_card_refs(this->target_card_refs),
s->debug_str_for_card_refs(this->defense_card_refs),
s->debug_str_for_card_refs(this->original_attacker_card_refs));
}
void ActionMetadata::clear() {
@@ -605,8 +569,7 @@ void ActionMetadata::clear() {
this->action_subphase = ActionSubphase::INVALID_FF;
this->defense_power = 0;
this->defense_bonus = 0;
// TODO: Ep3 NTE doesn't set attack_bonus to zero here. Is the field just
// unused in NTE?
// TODO: Ep3 NTE doesn't set attack_bonus to zero here. Is the field just unused in NTE?
this->attack_bonus = 0;
this->flags = 0;
this->target_card_refs.clear(0xFFFF);
@@ -652,16 +615,13 @@ void ActionMetadata::clear_target_card_refs() {
}
void ActionMetadata::add_target_card_ref(uint16_t card_ref) {
if (card_ref != 0xFFFF &&
this->target_card_ref_count < this->target_card_refs.size()) {
if ((card_ref != 0xFFFF) && (this->target_card_ref_count < this->target_card_refs.size())) {
this->target_card_refs[this->target_card_ref_count++] = 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, 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;
@@ -675,21 +635,10 @@ HandAndEquipState::HandAndEquipState() {
}
std::string HandAndEquipState::str(shared_ptr<const Server> s) const {
string assist_card_ref_s = s->debug_str_for_card_ref(this->assist_card_ref);
string assist_card_ref2_s = s->debug_str_for_card_ref(this->assist_card_ref2);
string assist_card_id_s = s->debug_str_for_card_id(this->assist_card_id);
string sc_card_ref_s = s->debug_str_for_card_ref(this->sc_card_ref);
string hand_card_refs_s = s->debug_str_for_card_refs(this->hand_card_refs);
string set_card_refs_s = s->debug_str_for_card_refs(this->set_card_refs);
string hand_card_refs2_s = s->debug_str_for_card_refs(this->hand_card_refs2);
string set_card_refs2_s = s->debug_str_for_card_refs(this->set_card_refs2);
return phosg::string_printf(
"HandAndEquipState[dice=[%hhu, %hhu], atk=%hhu, def=%hhu, atk2=%hhu, "
"a1=%hhu, total_set_cost=%hhu, is_cpu=%hhu, assist_flags=%08" PRIX32 ", "
"hand_refs=%s, assist_ref=%s, set_refs=%s, sc_ref=%s, hand_refs2=%s, "
"set_refs2=%s, assist_ref2=%s, assist_set_num=%hu, assist_card_id=%s, "
"assist_turns=%hhu, assist_delay=%hhu, atk_bonus=%hhu, def_bonus=%hhu, "
"u2=[%hhu, %hhu]]",
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={}, "
"assist_turns={}, assist_delay={}, atk_bonus={}, def_bonus={}, u2=[{}, {}]]",
this->dice_results[0],
this->dice_results[1],
this->atk_points,
@@ -698,16 +647,16 @@ std::string HandAndEquipState::str(shared_ptr<const Server> s) const {
this->unknown_a1,
this->total_set_cards_cost,
this->is_cpu_player,
this->assist_flags.load(),
hand_card_refs_s.c_str(),
assist_card_ref_s.c_str(),
set_card_refs_s.c_str(),
sc_card_ref_s.c_str(),
hand_card_refs2_s.c_str(),
set_card_refs2_s.c_str(),
assist_card_ref2_s.c_str(),
this->assist_card_set_number.load(),
assist_card_id_s.c_str(),
this->assist_flags,
s->debug_str_for_card_refs(this->hand_card_refs),
s->debug_str_for_card_ref(this->assist_card_ref),
s->debug_str_for_card_refs(this->set_card_refs),
s->debug_str_for_card_ref(this->sc_card_ref),
s->debug_str_for_card_refs(this->hand_card_refs2),
s->debug_str_for_card_refs(this->set_card_refs2),
s->debug_str_for_card_ref(this->assist_card_ref2),
this->assist_card_set_number,
s->debug_str_for_card_id(this->assist_card_id),
this->assist_remaining_turns,
this->assist_delay_turns,
this->atk_bonuses,
@@ -795,17 +744,15 @@ void PlayerBattleStats::clear() {
float PlayerBattleStats::score(size_t num_rounds) const {
// Note: This formula doesn't match the formula on PSO-World, which is:
// 35
// + (Attack Damage - Damage Taken)
// + (Max Card Combo x 3)
// - (Story Character Damage x 1.8)
// - (Turns x 2.7)
// + (Action Card Negated Damage x 0.8)
// I don't know where that formula came from, but this one came from the USA
// Ep3 PsoV3.dol, so it's presumably correct. Is the PSO-World formula simply
// incorrect, or is it from e.g. the Japanese version, which may have a
// 35 + (Attack Damage - Damage Taken)
// + (Max Card Combo x 3)
// - (Story Character Damage x 1.8)
// - (Turns x 2.7)
// + (Action Card Negated Damage x 0.8)
// I don't know where that formula came from, but this one came from the USA Ep3 PsoV3.dol, so it's presumably
// correct. Is the PSO-World formula simply incorrect, or is it from e.g. the Japanese version, which may have a
// different rank calculation function?
return 38.0f + 0.8f * this->action_card_negated_damage - 2.3f * num_rounds - 1.8f * this->sc_damage_taken + 3.0f * this->max_attack_combo_size + (this->damage_given - this->damage_taken);
return 38.0f + (0.8f * this->action_card_negated_damage) - (2.3f * num_rounds) - (1.8f * this->sc_damage_taken) + (3.0f * this->max_attack_combo_size) + (this->damage_given - this->damage_taken);
}
uint8_t PlayerBattleStats::rank(size_t num_rounds) const {
@@ -817,10 +764,8 @@ const char* PlayerBattleStats::rank_name(size_t num_rounds) const {
}
constexpr size_t RANK_THRESHOLD_COUNT = 9;
static const float RANK_THRESHOLDS[RANK_THRESHOLD_COUNT] = {
15.0f, 25.0f, 30.0f, 40.0f, 50.0f, 60.0f, 65.0f, 75.0f, 85.0f};
static const char* RANK_NAMES[RANK_THRESHOLD_COUNT + 1] = {
"E", "D", "D+", "C", "C+", "B", "B+", "A", "A+", "S"};
static const float RANK_THRESHOLDS[RANK_THRESHOLD_COUNT] = {15, 25, 30, 40, 50, 60, 65, 75, 85};
static const char* RANK_NAMES[RANK_THRESHOLD_COUNT + 1] = {"E", "D", "D+", "C", "C+", "B", "B+", "A", "A+", "S"};
uint8_t PlayerBattleStats::rank_for_score(float score) {
size_t rank = 0;
@@ -838,19 +783,19 @@ const char* PlayerBattleStats::name_for_rank(uint8_t rank) {
}
PlayerBattleStatsTrial::PlayerBattleStatsTrial(const PlayerBattleStats& data)
: damage_given(data.damage_given.load()),
damage_taken(data.damage_taken.load()),
num_opponent_cards_destroyed(data.num_opponent_cards_destroyed.load()),
num_owned_cards_destroyed(data.num_owned_cards_destroyed.load()),
total_move_distance(data.total_move_distance.load()) {}
: damage_given(data.damage_given),
damage_taken(data.damage_taken),
num_opponent_cards_destroyed(data.num_opponent_cards_destroyed),
num_owned_cards_destroyed(data.num_owned_cards_destroyed),
total_move_distance(data.total_move_distance) {}
PlayerBattleStatsTrial::operator PlayerBattleStats() const {
PlayerBattleStats ret;
ret.damage_given = this->damage_given.load();
ret.damage_taken = this->damage_taken.load();
ret.num_opponent_cards_destroyed = this->num_opponent_cards_destroyed.load();
ret.num_owned_cards_destroyed = this->num_owned_cards_destroyed.load();
ret.total_move_distance = this->total_move_distance.load();
ret.damage_given = this->damage_given;
ret.damage_taken = this->damage_taken;
ret.num_opponent_cards_destroyed = this->num_opponent_cards_destroyed;
ret.num_owned_cards_destroyed = this->num_owned_cards_destroyed;
ret.total_move_distance = this->total_move_distance;
return ret;
}
@@ -861,26 +806,28 @@ static bool is_card_within_range(
phosg::PrefixedLogger* log) {
if (ss.card_ref == 0xFFFF) {
if (log) {
log->debug("is_card_within_range: (false) ss.card_ref missing");
log->debug_f("is_card_within_range: (false) ss.card_ref missing");
}
return false;
}
if (range[0] == 2) {
if (log) {
log->debug("is_card_within_range: (true) range is entire field");
log->debug_f("is_card_within_range: (true) range is entire field");
}
return true;
}
if ((ss.loc.x < anchor_loc.x - 4) || (ss.loc.x > anchor_loc.x + 4)) {
if (log) {
log->debug("is_card_within_range: (false) outside x range (ss.loc.x=%hhu, anchor_loc.x=%hhu)", ss.loc.x, anchor_loc.x);
log->debug_f(
"is_card_within_range: (false) outside x range (ss.loc.x={}, anchor_loc.x={})", ss.loc.x, anchor_loc.x);
}
return false;
}
if ((ss.loc.y < anchor_loc.y - 4) || (ss.loc.y > anchor_loc.y + 4)) {
if (log) {
log->debug("is_card_within_range: (false) outside y range (ss.loc.y=%hhu, anchor_loc.y=%hhu)", ss.loc.y, anchor_loc.y);
log->debug_f(
"is_card_within_range: (false) outside y range (ss.loc.y={}, anchor_loc.y={})", ss.loc.y, anchor_loc.y);
}
return false;
}
@@ -889,7 +836,7 @@ static bool is_card_within_range(
uint8_t x_index = (ss.loc.x - anchor_loc.x) + 4;
bool ret = (range[y_index * 9 + x_index] != 0);
if (log) {
log->debug("is_card_within_range: (%s) (ss.loc=(%hhu,%hhu), anchor_loc=(%hhu,%hhu), indexes=(%hhu,%hhu))",
log->debug_f("is_card_within_range: ({}) (ss.loc=({},{}), anchor_loc=({},{}), indexes=({},{}))",
ret ? "true" : "false", ss.loc.x, ss.loc.y, anchor_loc.x, anchor_loc.y, x_index, y_index);
}
return ret;
@@ -903,24 +850,24 @@ vector<uint16_t> get_card_refs_within_range(
vector<uint16_t> ret;
if (is_card_within_range(range, loc, short_statuses[0], log)) {
if (log) {
log->debug("get_card_refs_within_range: sc card @%04hX within range", short_statuses[0].card_ref.load());
log->debug_f("get_card_refs_within_range: sc card @{:04X} within range", short_statuses[0].card_ref);
}
ret.emplace_back(short_statuses[0].card_ref);
} else {
if (log) {
log->debug("get_card_refs_within_range: sc card @%04hX not within range", short_statuses[0].card_ref.load());
log->debug_f("get_card_refs_within_range: sc card @{:04X} not within range", short_statuses[0].card_ref);
}
}
for (size_t card_index = 7; card_index < 15; card_index++) {
const auto& ss = short_statuses[card_index];
if (is_card_within_range(range, loc, ss, log)) {
if (log) {
log->debug("get_card_refs_within_range: card @%04hX within range", ss.card_ref.load());
log->debug_f("get_card_refs_within_range: card @{:04X} within range", ss.card_ref);
}
ret.emplace_back(ss.card_ref);
} else {
if (log) {
log->debug("get_card_refs_within_range: card @%04hX not within range", ss.card_ref.load());
log->debug_f("get_card_refs_within_range: card @{:04X} not within range", ss.card_ref);
}
}
}
+3 -7
View File
@@ -102,8 +102,7 @@ struct ActionState {
} __packed_ws__(ActionState, 0x64);
struct ActionChain {
// Note: Episode 3 Trial Edition has a different format for this structure.
// See ActionChainWithCondsTrial for details.
// Note: Trial Edition has a different format for this structure. See ActionChainWithCondsTrial for details.
/* 00 */ int8_t effective_ap;
/* 01 */ int8_t effective_tp;
/* 02 */ int8_t ap_effect_bonus;
@@ -196,8 +195,7 @@ struct ActionChainWithCondsTrial {
/* 0022 */ int8_t card_ap;
/* 0023 */ int8_t card_tp;
/* 0024 */ le_uint32_t flags;
// The only difference between this structure and ActionChainWithConds is that
// these two fields are in the opposite order.
// The only difference between this structure and ActionChainWithConds is that these two fields are swapped.
/* 0028 */ parray<Condition, 9> conditions;
/* 00B8 */ parray<le_uint16_t, 4 * 9> target_card_refs;
/* 0100 */
@@ -236,9 +234,7 @@ struct ActionMetadata {
void clear_target_card_refs();
void add_target_card_ref(uint16_t card_ref);
void add_defense_card_ref(
uint16_t defense_card_ref,
std::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);
std::string str(std::shared_ptr<const Server> s) const;
} __packed_ws__(ActionMetadata, 0x74);
+112 -165
View File
@@ -16,17 +16,16 @@ void compute_effective_range(
const Location& loc,
shared_ptr<const MapAndRulesState> map_and_rules,
phosg::PrefixedLogger* log) {
if (log && log->should_log(phosg::LogLevel::DEBUG)) {
if (log && log->should_log(phosg::LogLevel::L_DEBUG)) {
string loc_str = loc.str();
log->debug("compute_effective_range: card_id=#%04hX, loc=%s", card_id, loc_str.c_str());
log->debug("compute_effective_range: map_and_rules->map:");
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);
}
ret.clear(0);
parray<uint32_t, 6> range_def;
if (card_id == 0xFFFE) {
// Heavy Fog: one tile directly in front
if (card_id == 0xFFFE) { // Heavy Fog: one tile directly in front
range_def[3] = 0x00000100;
} else {
shared_ptr<const CardIndex::CardEntry> ce;
@@ -40,14 +39,14 @@ void compute_effective_range(
}
}
if (log) {
log->debug("compute_effective_range: range_def: %05" PRIX32 " %05" PRIX32 " %05" PRIX32 " %05" PRIX32 " %05" PRIX32 " %05" PRIX32, range_def[0], range_def[1], range_def[2], range_def[3], range_def[4], range_def[5]);
log->debug_f("compute_effective_range: range_def: {:05X} {:05X} {:05X} {:05X} {:05X} {:05X}",
range_def[0], range_def[1], range_def[2], range_def[3], range_def[4], range_def[5]);
}
if (range_def[0] == 0x000FFFFF) {
// Entire field
if (range_def[0] == 0x000FFFFF) { // Entire field
ret.clear(2);
if (log) {
log->debug("compute_effective_range: entire field (2)");
log->debug_f("compute_effective_range: entire field (2)");
}
return;
}
@@ -64,8 +63,10 @@ void compute_effective_range(
}
if (log) {
for (size_t y = 0; y < 9; y++) {
log->debug("compute_effective_range: decoded_range: %hhX %hhX %hhX %hhX %hhX %hhX %hhX %hhX %hhX",
decoded_range[y * 9 + 0], decoded_range[y * 9 + 1], decoded_range[y * 9 + 2], decoded_range[y * 9 + 3], decoded_range[y * 9 + 4], decoded_range[y * 9 + 5], decoded_range[y * 9 + 6], decoded_range[y * 9 + 7], decoded_range[y * 9 + 8]);
log->debug_f("compute_effective_range: decoded_range: {:X} {:X} {:X} {:X} {:X} {:X} {:X} {:X} {:X}",
decoded_range[y * 9 + 0], decoded_range[y * 9 + 1], decoded_range[y * 9 + 2],
decoded_range[y * 9 + 3], decoded_range[y * 9 + 4], decoded_range[y * 9 + 5],
decoded_range[y * 9 + 6], decoded_range[y * 9 + 7], decoded_range[y * 9 + 8]);
}
}
@@ -98,7 +99,8 @@ void compute_effective_range(
}
ret[y * 9 + x] = decoded_range[up_y * 9 + up_x];
if (log) {
log->debug("compute_effective_range: x=%hd y=%hd up_x=%hd up_y=%hd v=%hhX", x, y, up_x, up_y, ret[y * 9 + x]);
log->debug_f(
"compute_effective_range: x={} y={} up_x={} up_y={} v={:X}", x, y, up_x, up_y, ret[y * 9 + x]);
}
}
}
@@ -107,8 +109,9 @@ void compute_effective_range(
if (log) {
for (size_t y = 0; y < 9; y++) {
log->debug("compute_effective_range: ret: %hhX %hhX %hhX %hhX %hhX %hhX %hhX %hhX %hhX",
ret[y * 9 + 0], ret[y * 9 + 1], ret[y * 9 + 2], ret[y * 9 + 3], ret[y * 9 + 4], ret[y * 9 + 5], ret[y * 9 + 6], ret[y * 9 + 7], ret[y * 9 + 8]);
log->debug_f("compute_effective_range: ret: {:X} {:X} {:X} {:X} {:X} {:X} {:X} {:X} {:X}",
ret[y * 9 + 0], ret[y * 9 + 1], ret[y * 9 + 2], ret[y * 9 + 3], ret[y * 9 + 4],
ret[y * 9 + 5], ret[y * 9 + 6], ret[y * 9 + 7], ret[y * 9 + 8]);
}
}
}
@@ -124,9 +127,7 @@ bool card_linkage_is_valid(
bool sc_is_named_android_without_permission_effect = false;
bool sc_is_named_android = sc_ce->def.is_named_android_sc();
if (sc_is_named_android &&
!has_permission_effect &&
(left_ce->def.type == CardType::ITEM)) {
if (sc_is_named_android && !has_permission_effect && (left_ce->def.type == CardType::ITEM)) {
sc_is_named_android_without_permission_effect = true;
}
@@ -136,8 +137,7 @@ bool card_linkage_is_valid(
for (size_t x = 0; x < 8; x++) {
uint8_t right_color = left_ce->def.right_colors[x];
if ((right_color != 0) &&
(!sc_is_named_android_without_permission_effect || (right_color != 3))) {
if ((right_color != 0) && (!sc_is_named_android_without_permission_effect || (right_color != 3))) {
for (size_t y = 0; y < 8; y++) {
if (right_color == right_ce->def.left_colors[y]) {
return true;
@@ -146,15 +146,13 @@ bool card_linkage_is_valid(
}
}
// If we get here, then the linkage does not make sense based only on the
// cards' left/right colors. It may still be allowed if Permission is in
// effect, though.
// If we get here, then the linkage does not make sense based only on the cards' left/right colors. It may still be
// allowed if Permission is in effect, though.
// Ignore Permission effect if the left card is another action card (the Tech
// color linkage must make sense in that case). (The way they do this is kind
// of dumb - they should have checked that type == ACTION, but instead they
// checked that type *isn't* most of the other types... but curiously, ASSIST
// is not checked. This is probably just an oversight.)
// Ignore Permission effect if the left card is another action card (the Tech color linkage must make sense in that
// case). (The way they do this is kind of dumb - they should have checked that type == ACTION, but instead they
// checked that type *isn't* most of the other types... but curiously, ASSIST is not checked. This is probably just
// an oversight.)
if (has_permission_effect &&
(left_ce->def.type != CardType::HUNTERS_SC) &&
(left_ce->def.type != CardType::ARKZ_SC) &&
@@ -198,17 +196,14 @@ shared_ptr<const Server> RulerServer::server() const {
return s;
}
ActionChainWithConds* RulerServer::action_chain_with_conds_for_card_ref(
uint16_t card_ref) {
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));
}
const ActionChainWithConds* RulerServer::action_chain_with_conds_for_card_ref(
uint16_t card_ref) const {
const ActionChainWithConds* RulerServer::action_chain_with_conds_for_card_ref(uint16_t card_ref) const {
uint8_t client_id = client_id_for_card_ref(card_ref);
if (client_id != 0xFF) {
// There appears to be a bug in Trial Edition: the bound on this loop is
// 0x10, not 9.
// There appears to be a bug in Trial Edition: the bound on this loop is 0x10, not 9.
for (size_t z = 0; z < 9; z++) {
const auto* chain = &this->set_card_action_chains[client_id]->at(z);
if (card_ref == chain->chain.acting_card_ref) {
@@ -219,8 +214,7 @@ const ActionChainWithConds* RulerServer::action_chain_with_conds_for_card_ref(
return nullptr;
}
bool RulerServer::any_attack_action_card_is_support_tech_or_support_pb(
const ActionState& pa) const {
bool RulerServer::any_attack_action_card_is_support_tech_or_support_pb(const ActionState& pa) const {
if (pa.attacker_card_ref != 0xFFFF) {
for (size_t z = 0; (z < 8) && (pa.action_card_refs[z] != 0xFFFF); z++) {
uint16_t card_id = this->card_id_for_card_ref(pa.action_card_refs[z]);
@@ -292,8 +286,7 @@ bool RulerServer::card_has_pierce_or_rampage(
const auto& sc_status = short_statuses->at(0);
auto ce = this->definition_for_card_ref(sc_status.card_ref);
// This appears to be an NTE bug: Major Pierce doesn't work on Arkz SCs.
if (ce &&
(!is_nte || (ce->def.type == CardType::HUNTERS_SC)) &&
if (ce && (!is_nte || (ce->def.type == CardType::HUNTERS_SC)) &&
(this->get_card_ref_max_hp(sc_status.card_ref) <= sc_status.current_hp * 2)) {
return ret;
}
@@ -354,8 +347,7 @@ bool RulerServer::attack_action_has_rampage_and_not_pierce(const ActionState& pa
}
}
const auto* chain = this->action_chain_with_conds_for_card_ref(
pa.attacker_card_ref);
const auto* chain = this->action_chain_with_conds_for_card_ref(pa.attacker_card_ref);
if (chain) {
for (ssize_t z = 8; z >= 0; z--) {
bool has_rampage = this->check_pierce_and_rampage(
@@ -396,7 +388,8 @@ bool RulerServer::attack_action_has_pierce_and_not_rampage(const ActionState& pa
}
if ((card_ref1 != 0xFFFF) &&
(card_ref1 != pa.attacker_card_ref) &&
!this->check_usability_or_apply_condition_for_card_refs(card_ref1, pa.attacker_card_ref, stat->at(0).card_ref, 0xFF, AttackMedium::INVALID_FF)) {
!this->check_usability_or_apply_condition_for_card_refs(
card_ref1, pa.attacker_card_ref, stat->at(0).card_ref, 0xFF, AttackMedium::INVALID_FF)) {
return false;
}
@@ -433,8 +426,7 @@ bool RulerServer::attack_action_has_pierce_and_not_rampage(const ActionState& pa
}
for (; last_action_card_index >= 0; last_action_card_index--) {
auto ce = this->definition_for_card_ref(
pa.action_card_refs[last_action_card_index]);
auto ce = this->definition_for_card_ref(pa.action_card_refs[last_action_card_index]);
if (!ce) {
continue;
}
@@ -560,8 +552,7 @@ bool RulerServer::card_ref_can_attack(uint16_t card_ref) {
return true;
}
size_t num_assists = this->assist_server->compute_num_assist_effects_for_client(
client_id);
size_t num_assists = this->assist_server->compute_num_assist_effects_for_client(client_id);
for (size_t z = 0; z < num_assists; z++) {
if (this->assist_server->get_active_assist_by_index(z) == AssistEffect::PERMISSION) {
return true;
@@ -571,8 +562,7 @@ bool RulerServer::card_ref_can_attack(uint16_t card_ref) {
return !ce->def.cannot_attack;
}
bool RulerServer::card_ref_can_move(
uint8_t client_id, uint16_t card_ref, bool ignore_atk_points) const {
bool RulerServer::card_ref_can_move(uint8_t client_id, uint16_t card_ref, bool ignore_atk_points) const {
if (client_id == 0xFF) {
return false;
}
@@ -644,8 +634,7 @@ bool RulerServer::card_ref_can_move(
}
}
bool RulerServer::card_ref_has_class_usability_condition(
uint16_t card_ref) const {
bool RulerServer::card_ref_has_class_usability_condition(uint16_t card_ref) const {
auto ce = this->definition_for_card_ref(card_ref);
if (ce) {
uint8_t criterion = static_cast<uint8_t>(ce->def.usable_criterion);
@@ -685,8 +674,7 @@ bool RulerServer::card_ref_is_aerial(uint16_t card_ref) const {
return this->find_condition_on_card_ref(card_ref, ConditionType::AERIAL);
}
bool RulerServer::card_ref_is_aerial_or_has_free_maneuver(
uint16_t card_ref) const {
bool RulerServer::card_ref_is_aerial_or_has_free_maneuver(uint16_t card_ref) const {
return (this->card_ref_has_free_maneuver(card_ref) || this->card_ref_is_aerial(card_ref));
}
@@ -694,8 +682,7 @@ bool RulerServer::card_ref_is_boss_sc(uint32_t card_ref) const {
return this->card_id_is_boss_sc(this->card_id_for_card_ref(card_ref));
}
bool RulerServer::card_ref_or_any_set_card_has_condition_46(
uint16_t card_ref) const {
bool RulerServer::card_ref_or_any_set_card_has_condition_46(uint16_t card_ref) const {
uint16_t card_id = this->card_id_for_card_ref(card_ref);
if (card_id == 0xFFFF) {
return false;
@@ -752,8 +739,7 @@ bool RulerServer::card_ref_or_sc_has_fixed_range(uint16_t card_ref) const {
return false;
}
return this->find_condition_on_card_ref(
this->short_statuses[client_id]->at(0).card_ref, ConditionType::FIXED_RANGE);
return this->find_condition_on_card_ref(this->short_statuses[client_id]->at(0).card_ref, ConditionType::FIXED_RANGE);
}
bool RulerServer::check_move_path_and_get_cost(
@@ -772,9 +758,8 @@ bool RulerServer::check_move_path_and_get_cost(
}
uint8_t atk = this->hand_and_equip_states[client_id]->atk_points;
// Note: In the original code, it seems atk was signed, which doesn't make
// much sense. We've fixed that here.
// if (atk < 0) { // Uhhh what? This is supposed to be impossible
// Note: In the original code, it seems atk was signed, which doesn't make much sense.
// if (atk < 0) { // This is supposed to be impossible
// return false;
// }
@@ -833,8 +818,7 @@ bool RulerServer::check_pierce_and_rampage(
return false;
}
if ((card_ref != 0xFFFF) &&
(!card_short_status || !this->card_exists_by_status(*card_short_status))) {
if ((card_ref != 0xFFFF) && (!card_short_status || !this->card_exists_by_status(*card_short_status))) {
return false;
}
@@ -941,7 +925,7 @@ bool RulerServer::check_usability_or_condition_apply(
AttackMedium attack_medium) const {
auto s = this->server();
bool is_nte = s->options.is_nte();
auto log = s->log_stack(phosg::string_printf("check_usability_or_condition_apply(%02hhX, #%04hX, %02hhX, #%04hX, #%04hX, %02hhX, %s, %s): ", client_id1, card_id1, client_id2, card_id2, card_id3, def_effect_index, is_item_usability_check ? "true" : "false", phosg::name_for_enum(attack_medium)));
auto log = s->log_stack(std::format("check_usability_or_condition_apply({:02X}, #{:04X}, {:02X}, #{:04X}, #{:04X}, {:02X}, {}, {}): ", client_id1, card_id1, client_id2, card_id2, card_id3, def_effect_index, is_item_usability_check ? "true" : "false", phosg::name_for_enum(attack_medium)));
if (static_cast<uint8_t>(attack_medium) & 0x80) {
attack_medium = AttackMedium::UNKNOWN;
@@ -951,11 +935,11 @@ bool RulerServer::check_usability_or_condition_apply(
auto ce2 = this->definition_for_card_id(card_id2);
auto ce3 = this->definition_for_card_id(card_id3);
if (!ce1) {
log.debug("ce1 missing");
log.debug_f("ce1 missing");
return false;
}
if (!is_nte && (ce1->def.type == CardType::ITEM) && this->card_id_is_boss_sc(card_id2)) {
log.debug("ce1 is item and card_id2 is boss sc");
log.debug_f("ce1 is item and card_id2 is boss sc");
return false;
}
@@ -964,15 +948,14 @@ bool RulerServer::check_usability_or_condition_apply(
criterion_code = ce1->def.usable_criterion;
} else {
if (def_effect_index > 2) {
log.debug("invalid def_effect_index");
log.debug_f("invalid def_effect_index");
return false;
}
criterion_code = ce1->def.effects[def_effect_index].apply_criterion;
}
log.debug("criterion_code=%s", phosg::name_for_enum(criterion_code));
log.debug_f("criterion_code={}", phosg::name_for_enum(criterion_code));
// For item usability checks, prevent criteria that depend on player
// positioning/team setup
// For item usability checks, prevent criteria that depend on player positioning/team setup
if (is_item_usability_check &&
((criterion_code == CriterionCode::SAME_TEAM) ||
(criterion_code == CriterionCode::SAME_PLAYER) ||
@@ -980,17 +963,15 @@ bool RulerServer::check_usability_or_condition_apply(
(criterion_code == CriterionCode::FC) ||
(criterion_code == CriterionCode::NOT_SC) ||
(criterion_code == CriterionCode::SC))) {
log.debug("criterion is forbidden");
log.debug_f("criterion is forbidden");
criterion_code = CriterionCode::NONE;
}
// Presumably this odd-looking expression here is used to handle two different
// cases. When checking for a condition, def_effect_index should be non-0xFF,
// so we'd return true if the criterion passes. When checking if an item or
// creature card is usable, the two client IDs should be the same or the
// second should not be given, so we'd return true if the criterion passes. If
// neither of these cases apply, we should return false as a failsafe even if
// the criterion passes. NTE did not have such a check.
// Presumably this odd-looking expression here is used to handle two different cases. When checking for a condition,
// def_effect_index should be non-0xFF, so we'd return true if the criterion passes. When checking if an item or
// creature card is usable, the two client IDs should be the same or the second should not be given, so we'd return
// true if the criterion passes. If neither of these cases apply, we should return false as a failsafe even if the
// criterion passes. NTE did not have such a check.
bool ret = is_nte || (!(def_effect_index & 0x80) || (client_id1 == client_id2)) || (client_id2 == 0xFF);
switch (criterion_code) {
case CriterionCode::NONE:
@@ -1354,14 +1335,12 @@ bool RulerServer::check_usability_or_condition_apply(
}
}
log.debug("default return (false)");
log.debug_f("default return (false)");
return false;
}
uint16_t RulerServer::compute_attack_or_defense_costs(
const ActionState& pa,
bool allow_mighty_knuckle,
uint8_t* out_ally_cost) const {
const ActionState& pa, bool allow_mighty_knuckle, uint8_t* out_ally_cost) const {
int16_t final_cost = 1;
bool has_mighty_knuckle = false;
int16_t cost_bias = 0;
@@ -1383,8 +1362,7 @@ uint16_t RulerServer::compute_attack_or_defense_costs(
uint8_t client_id = client_id_for_card_ref(pa.attacker_card_ref);
uint16_t sc_card_ref_if_item = 0xFFFF;
if ((client_id != 0xFF) && ce && (ce->def.type == CardType::ITEM) &&
this->short_statuses[client_id]) {
if ((client_id != 0xFF) && ce && (ce->def.type == CardType::ITEM) && this->short_statuses[client_id]) {
sc_card_ref_if_item = this->short_statuses[client_id]->at(0).card_ref;
}
@@ -1478,44 +1456,43 @@ bool RulerServer::compute_effective_range_and_target_mode_for_attack(
for (z = 0; (z < 8) && (pa.action_card_refs[z] != 0xFFFF); z++) {
}
if (z >= 8) {
log.debug("too many action card refs");
log.debug_f("too many action card refs");
return false;
}
log.debug("%zu action card refs", z);
log.debug_f("{} action card refs", z);
uint16_t card_ref = (z == 0) ? pa.attacker_card_ref : pa.action_card_refs[z - 1];
log.debug("base card ref = @%04hX", card_ref);
log.debug_f("base card ref = @{:04X}", card_ref);
uint16_t card_id = this->card_id_for_card_ref(card_ref);
if (card_id == 0xFFFF) {
log.debug("card ref is broken");
log.debug_f("card ref is broken");
return false;
}
auto ce = this->definition_for_card_id(card_id);
uint8_t client_id = client_id_for_card_ref(pa.attacker_card_ref);
if ((client_id == 0xFF) || !ce) {
log.debug("card ref is broken or definition is missing");
log.debug_f("card ref is broken or definition is missing");
return false;
}
if (out_orig_card_ref) {
log.debug("orig_card_ref = @%04hX", card_ref);
log.debug_f("orig_card_ref = @{:04X}", card_ref);
*out_orig_card_ref = card_ref;
}
auto target_mode = ce->def.target_mode;
if (this->card_ref_or_sc_has_fixed_range(pa.attacker_card_ref)) {
const char* target_mode_name = name_for_target_mode(target_mode);
log.debug("attacker card ref @%04hX has fixed range; target mode is %s (%hhu)",
pa.attacker_card_ref.load(), target_mode_name, static_cast<uint8_t>(target_mode));
log.debug_f("attacker card ref @{:04X} has fixed range; target mode is {} ({})",
pa.attacker_card_ref, target_mode_name, static_cast<uint8_t>(target_mode));
card_id = this->card_id_for_card_ref(pa.attacker_card_ref);
if (!is_nte) {
auto sc_ce = this->definition_for_card_id(card_id);
if (sc_ce && (static_cast<uint8_t>(target_mode) < 6)) {
target_mode = sc_ce->def.target_mode;
const char* target_mode_name = name_for_target_mode(target_mode);
log.debug("sc_ce overrides target mode with %s (%hhu)",
target_mode_name, static_cast<uint8_t>(target_mode));
log.debug_f("sc_ce overrides target mode with {} ({})", target_mode_name, static_cast<uint8_t>(target_mode));
}
}
}
@@ -1525,10 +1502,10 @@ bool RulerServer::compute_effective_range_and_target_mode_for_attack(
auto assist_effect = this->assist_server->get_active_assist_by_index(z);
if (assist_effect == AssistEffect::SIMPLE) {
card_id = this->card_id_for_card_ref(pa.attacker_card_ref);
log.debug("SIMPLE assist overrides card id with #%04hX", card_id);
log.debug_f("SIMPLE assist overrides card id with #{:04X}", card_id);
} else if (assist_effect == AssistEffect::HEAVY_FOG) {
card_id = 0xFFFE;
log.debug("HEAVY_FOG assist overrides card id with #%04hX", card_id);
log.debug_f("HEAVY_FOG assist overrides card id with #{:04X}", card_id);
}
}
@@ -1568,9 +1545,7 @@ size_t RulerServer::count_rampage_targets_for_attack(const ActionState& pa, uint
}
bool RulerServer::defense_card_can_apply_to_attack(
uint16_t defense_card_ref,
uint16_t attacker_card_ref,
uint16_t attacker_sc_card_ref) const {
uint16_t defense_card_ref, uint16_t attacker_card_ref, uint16_t attacker_sc_card_ref) const {
uint16_t defense_card_id = this->card_id_for_card_ref(defense_card_ref);
uint16_t attacker_sc_card_id = this->card_id_for_card_ref(attacker_sc_card_ref);
uint16_t attacker_card_id = this->card_id_for_card_ref(attacker_card_ref);
@@ -1654,8 +1629,7 @@ bool RulerServer::defense_card_matches_any_attack_card_top_color(const ActionSta
if (!ce) {
throw runtime_error("defense card definition is missing");
}
const auto* chain = this->action_chain_with_conds_for_card_ref(
pa.original_attacker_card_ref);
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) {
auto other_ce = this->definition_for_card_ref(pa.original_attacker_card_ref);
if (other_ce && other_ce->def.any_top_color_matches(ce->def)) {
@@ -1681,10 +1655,7 @@ shared_ptr<const CardIndex::CardEntry> RulerServer::definition_for_card_ref(uint
}
int32_t RulerServer::error_code_for_client_setting_card(
uint8_t client_id,
uint16_t card_ref,
const Location* loc,
uint8_t assist_target_client_id) const {
uint8_t client_id, uint16_t card_ref, const Location* loc, uint8_t assist_target_client_id) const {
if (client_id > 3) {
return -0x7D;
}
@@ -1843,8 +1814,7 @@ int32_t RulerServer::error_code_for_client_setting_card(
if (!this->get_creature_summon_area(client_id, &summon_area_loc, &summon_area_size)) {
if (team_id != 1) {
if ((loc->x > 0) && (loc->x < this->map_and_rules->map.width - 1)) {
if ((loc->y < this->map_and_rules->map.height - summon_cost - 1) &&
(loc->y > 0)) {
if ((loc->y < this->map_and_rules->map.height - summon_cost - 1) && (loc->y > 0)) {
return 0;
}
if (loc->y == 1) {
@@ -1852,8 +1822,7 @@ int32_t RulerServer::error_code_for_client_setting_card(
}
}
} else {
if ((loc->x > 0) &&
(loc->x < this->map_and_rules->map.width - 1)) {
if ((loc->x > 0) && (loc->x < this->map_and_rules->map.width - 1)) {
if ((summon_cost + 1 <= loc->y) && (loc->y < this->map_and_rules->map.height - 1)) {
return 0;
}
@@ -1965,8 +1934,7 @@ bool RulerServer::flood_fill_move_path(
size_t num_occupied_tiles,
size_t num_vacant_tiles) const {
auto state = this->map_and_rules;
if ((x < 1) || (x >= state->map.width - 1) ||
(y < 1) || (y >= state->map.height - 1)) {
if ((x < 1) || (x >= state->map.width - 1) || (y < 1) || (y >= state->map.height - 1)) {
return 0;
}
@@ -1979,15 +1947,12 @@ bool RulerServer::flood_fill_move_path(
} else {
uint32_t cost = this->get_path_cost(
chain,
num_vacant_tiles + num_occupied_tiles + 1,
is_aerial ? num_occupied_tiles : 0);
chain, num_vacant_tiles + num_occupied_tiles + 1, is_aerial ? num_occupied_tiles : 0);
if (max_atk_points < cost) {
return 0;
}
visited_map->at(x * 0x10 + y) = 1;
if (path && (path->end_loc.x == x) && (path->end_loc.y == y) &&
((path->length == -1) || (cost < path->cost))) {
if (path && (path->end_loc.x == x) && (path->end_loc.y == y) && ((path->length == -1) || (cost < path->cost))) {
ret = true;
path->reset_totals();
path->remaining_distance = max_distance;
@@ -2005,8 +1970,7 @@ bool RulerServer::flood_fill_move_path(
int16_t new_max_distance = max_distance - 1;
if (new_max_distance > 0) {
static const int8_t offsets[4][2] = {
{1, 0}, {0, -1}, {-1, 0}, {0, 1}};
static const int8_t offsets[4][2] = {{1, 0}, {0, -1}, {-1, 0}, {0, 1}};
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) {
@@ -2059,27 +2023,26 @@ shared_ptr<const CardIndex::CardEntry> RulerServer::definition_for_card_id(uint3
uint32_t RulerServer::get_card_id_with_effective_range(
uint16_t card_ref, uint16_t card_id_override, TargetMode* out_target_mode) const {
auto log = this->server()->log_stack(phosg::string_printf("get_card_id_with_effective_range(@%04hX, #%04hX): ", card_ref, card_id_override));
auto log = this->server()->log_stack(std::format("get_card_id_with_effective_range(@{:04X}, #{:04X}): ", card_ref, card_id_override));
uint16_t card_id = (card_id_override == 0xFFFF)
? this->card_id_for_card_ref(card_ref)
: card_id_override;
log.debug("card_id=#%04hX", card_id);
uint16_t card_id = (card_id_override == 0xFFFF) ? this->card_id_for_card_ref(card_ref) : card_id_override;
log.debug_f("card_id=#{:04X}", card_id);
if (card_id != 0xFFFF) {
auto ce = this->definition_for_card_id(card_id);
uint8_t client_id = client_id_for_card_ref(card_ref);
if ((client_id != 0xFF) && ce) {
TargetMode effective_target_mode = ce->def.target_mode;
log.debug("ce valid for #%04hX with effective target mode %s", card_id, name_for_target_mode(effective_target_mode));
log.debug_f("ce valid for #{:04X} with effective target mode {}", card_id, name_for_target_mode(effective_target_mode));
if (this->card_ref_or_sc_has_fixed_range(card_ref)) {
// Undo the override that may have been passed in
log.debug("@%04hX has FIXED_RANGE", card_ref);
log.debug_f("@{:04X} has FIXED_RANGE", card_ref);
card_id = this->card_id_for_card_ref(card_ref);
auto orig_ce = this->definition_for_card_id(card_id);
if (orig_ce && (static_cast<uint8_t>(effective_target_mode) < 6)) {
log.debug("ce valid for #%04hX with effective target mode %s; overriding to %s", card_id, name_for_target_mode(effective_target_mode), name_for_target_mode(orig_ce->def.target_mode));
log.debug_f("ce valid for #{:04X} with effective target mode {}; overriding to {}",
card_id, name_for_target_mode(effective_target_mode), name_for_target_mode(orig_ce->def.target_mode));
effective_target_mode = orig_ce->def.target_mode;
}
}
@@ -2089,17 +2052,17 @@ uint32_t RulerServer::get_card_id_with_effective_range(
auto eff = this->assist_server->get_active_assist_by_index(z);
if (eff == AssistEffect::SIMPLE) {
card_id = this->card_id_for_card_ref(card_ref);
log.debug("SIMPLE assist effect is active; using #%04hX for range", card_id);
log.debug_f("SIMPLE assist effect is active; using #{:04X} for range", card_id);
} else if (eff == AssistEffect::HEAVY_FOG) {
card_id = 0xFFFE;
log.debug("HEAVY_FOG assist effect is active; limiting range to one tile in front");
log.debug_f("HEAVY_FOG assist effect is active; limiting range to one tile in front");
}
}
if (out_target_mode) {
*out_target_mode = effective_target_mode;
}
log.debug("results: card_id=#%04hX, target_mode=%s", card_id, name_for_target_mode(effective_target_mode));
log.debug_f("results: card_id=#{:04X}, target_mode={}", card_id, name_for_target_mode(effective_target_mode));
}
}
@@ -2123,8 +2086,7 @@ uint8_t RulerServer::get_card_ref_max_hp(uint16_t card_ref) const {
}
}
bool RulerServer::get_creature_summon_area(
uint8_t client_id, Location* out_loc, uint8_t* out_region_size) const {
bool RulerServer::get_creature_summon_area(uint8_t client_id, Location* out_loc, uint8_t* out_region_size) const {
if (!this->map_and_rules || (client_id > 3)) {
return false;
}
@@ -2155,8 +2117,7 @@ bool RulerServer::get_creature_summon_area(
region_size = this->map_and_rules->map.height - 3;
break;
default:
// This case isn't in the original code; probably it fell through to one
// of the above
// This case isn't in the original code; probably it fell through to one of the above
return false;
}
@@ -2169,27 +2130,20 @@ bool RulerServer::get_creature_summon_area(
return true;
}
shared_ptr<HandAndEquipState> RulerServer::get_hand_and_equip_state_for_client_id(
uint8_t client_id) {
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 {
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;
}
bool RulerServer::get_move_path_length_and_cost(
uint32_t client_id,
uint32_t card_ref,
const Location& loc,
uint32_t* out_length,
uint32_t* out_cost) const {
uint32_t client_id, uint32_t card_ref, const Location& loc, uint32_t* out_length, uint32_t* out_cost) const {
MovePath path;
parray<uint8_t, 0x100> visited_map;
path.end_loc = loc;
if (!this->check_move_path_and_get_cost(
client_id, card_ref, &visited_map, &path, out_cost)) {
if (!this->check_move_path_and_get_cost(client_id, card_ref, &visited_map, &path, out_cost)) {
return false;
}
@@ -2206,9 +2160,7 @@ bool RulerServer::get_move_path_length_and_cost(
}
ssize_t RulerServer::get_path_cost(
const ActionChainWithConds& chain,
ssize_t path_length,
ssize_t cost_penalty) const {
const ActionChainWithConds& chain, ssize_t path_length, ssize_t cost_penalty) const {
for (size_t x = 0; x < 9; x++) {
const auto& cond = chain.conditions[x];
if (cond.type == ConditionType::SET_MV_COST_TO_0) {
@@ -2253,13 +2205,11 @@ bool RulerServer::is_attack_valid(const ActionState& pa) {
return false;
}
// Note: The original code has a case here that results in error code -0x5E,
// triggered by a function returning false. However, that function always
// returns true and has no side effects, so we've omitted the case here.
// Note: The original code has a case here that results in error code -0x5E, triggered by a function returning false.
// However, that function always returns true and has no side effects, so we've omitted the case here.
const auto* attacker_card_status = this->short_status_for_card_ref(attacker_card_ref);
if (!attacker_card_status ||
!this->card_ref_can_attack(attacker_card_ref) ||
if (!attacker_card_status || !this->card_ref_can_attack(attacker_card_ref) ||
(attacker_card_status->card_flags & 0x500)) {
this->error_code3 = -0x6F;
return false;
@@ -2272,9 +2222,7 @@ bool RulerServer::is_attack_valid(const ActionState& pa) {
auto attacker_ce = this->definition_for_card_ref(attacker_card_ref);
auto attacker_chain = this->action_chain_with_conds_for_card_ref(attacker_card_ref);
if (!attacker_chain ||
(attacker_chain->chain.acting_card_ref != attacker_card_ref) ||
!attacker_ce ||
if (!attacker_chain || (attacker_chain->chain.acting_card_ref != attacker_card_ref) || !attacker_ce ||
((attacker_ce->def.type != CardType::HUNTERS_SC &&
(attacker_ce->def.type != CardType::ARKZ_SC) &&
(attacker_ce->def.type != CardType::CREATURE) &&
@@ -2313,7 +2261,9 @@ bool RulerServer::is_attack_valid(const ActionState& pa) {
return false;
}
auto left_card_ce = (z == 0) ? this->definition_for_card_ref(card_ref) : this->definition_for_card_ref(pa.action_card_refs[z - 1]);
auto left_card_ce = (z == 0)
? this->definition_for_card_ref(card_ref)
: this->definition_for_card_ref(pa.action_card_refs[z - 1]);
auto right_card_ce = this->definition_for_card_ref(right_card_ref);
if (right_card_ce->def.type != CardType::ACTION) {
@@ -2326,7 +2276,9 @@ bool RulerServer::is_attack_valid(const ActionState& pa) {
}
uint8_t attacker_client_id = client_id_for_card_ref(pa.attacker_card_ref);
auto sc_ce = (attacker_client_id != 0xFF) ? this->definition_for_card_ref(this->set_card_action_chains[attacker_client_id]->at(0).chain.acting_card_ref) : nullptr;
auto sc_ce = (attacker_client_id != 0xFF)
? this->definition_for_card_ref(this->set_card_action_chains[attacker_client_id]->at(0).chain.acting_card_ref)
: nullptr;
if (!card_linkage_is_valid(right_card_ce, left_card_ce, sc_ce, has_permission_effect)) {
this->error_code3 = -0x6B;
@@ -2363,8 +2315,7 @@ bool RulerServer::is_attack_valid(const ActionState& pa) {
}
bool RulerServer::is_attack_or_defense_valid(const ActionState& pa) {
// This error code is present in the original code, but is no longer possible
// since we require pa instead of using a pointer.
// This error code is present in the original code, but is no longer possible since we require pa instead.
// if (!pa) {
// this->error_code3 = -0x78;
// return false;
@@ -2450,9 +2401,8 @@ bool RulerServer::is_defense_valid(const ActionState& pa) {
return false;
}
// Note: The original code has a case here that results in error code -0x5E,
// triggered by a function returning false. However, that function always
// returns true and has no side effects, so we've omitted the case here.
// Note: The original code has a case here that results in error code -0x5E, triggered by a function returning false.
// However, that function always returns true and has no side effects, so we've omitted the case here.
const auto* stat = this->short_status_for_card_ref(pa.target_card_refs[0]);
if ((!stat || !this->card_exists_by_status(*stat)) || (stat->card_flags & 0x800)) {
@@ -2590,9 +2540,8 @@ bool RulerServer::MovePath::is_valid() const {
void RulerServer::offsets_for_direction(
const Location& loc, int32_t* out_x_offset, int32_t* out_y_offset) {
// Note: This function has opposite behavior for the UP and DOWN directions
// as compared to the global array of the same name.
// TODO: Figure out why this difference exists and document it.
// Note: This function has opposite behavior for the UP and DOWN directions as compared to the global array of the
// same name. TODO: Figure out why this difference exists and document it.
switch (loc.direction) {
case Direction::LEFT:
*out_x_offset = -1;
@@ -2629,8 +2578,7 @@ void RulerServer::register_player(
this->set_card_action_metadatas[client_id] = set_card_action_metadatas;
}
void RulerServer::replace_D1_D2_rank_cards_with_Attack(
parray<le_uint16_t, 0x1F>& card_ids) const {
void RulerServer::replace_D1_D2_rank_cards_with_Attack(parray<le_uint16_t, 0x1F>& card_ids) const {
for (size_t z = 0; z < card_ids.size(); z++) {
auto ce = this->definition_for_card_id(card_ids[z]);
if (ce && ((ce->def.rank == CardRank::D1) || (ce->def.rank == CardRank::D2))) {
@@ -2737,8 +2685,7 @@ bool RulerServer::should_allow_attacks_on_current_turn() const {
}
int32_t RulerServer::verify_deck(
const parray<le_uint16_t, 0x1F>& card_ids,
const parray<uint8_t, 0x2F0>* owned_card_counts) const {
const parray<le_uint16_t, 0x1F>& card_ids, const parray<uint8_t, 0x2F0>* owned_card_counts) const {
for (size_t z = 0; z < card_ids.size(); z++) {
if (!this->definition_for_card_id(card_ids.at(z))) {
return -0x7C;
+2 -4
View File
@@ -152,8 +152,7 @@ public:
uint32_t get_card_id_with_effective_range(
uint16_t card_ref, uint16_t card_id_override, TargetMode* out_target_mode) const;
uint8_t get_card_ref_max_hp(uint16_t card_ref) const;
bool get_creature_summon_area(
uint8_t client_id, Location* out_loc, uint8_t* out_region_size) const;
bool get_creature_summon_area(uint8_t client_id, Location* out_loc, uint8_t* out_region_size) const;
std::shared_ptr<HandAndEquipState> get_hand_and_equip_state_for_client_id(uint8_t client_id);
std::shared_ptr<const HandAndEquipState> get_hand_and_equip_state_for_client_id(uint8_t client_id) const;
bool get_move_path_length_and_cost(
@@ -191,8 +190,7 @@ public:
const CardShortStatus* short_status_for_card_ref(uint16_t card_ref) const;
bool should_allow_attacks_on_current_turn() const;
int32_t verify_deck(
const parray<le_uint16_t, 0x1F>& card_ids,
const parray<uint8_t, 0x2F0>* owned_card_counts = nullptr) const;
const parray<le_uint16_t, 0x1F>& card_ids, const parray<uint8_t, 0x2F0>* owned_card_counts = nullptr) const;
private:
std::weak_ptr<Server> w_server;
+179 -231
View File
@@ -12,12 +12,10 @@ using namespace std;
namespace Episode3 {
// This is (obviously) not the original string. The original string is:
// "03/05/29 18:00 by K.Toya" (NTE)
// "[V1][FINAL2.0] 03/09/13 15:30 by K.Toya" (Final)
static const char* VERSION_SIGNATURE =
"newserv Ep3 based on [V1][FINAL2.0] 03/09/13 15:30 by K.Toya";
static const char* VERSION_SIGNATURE_NTE =
"newserv Ep3 NTE based on 03/05/29 18:00 by K.Toya";
// 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";
static const char* VERSION_SIGNATURE_NTE = "newserv Ep3 NTE based on 03/05/29 18:00 by K.Toya";
Server::PresenceEntry::PresenceEntry() {
this->clear();
@@ -58,7 +56,7 @@ Server::Server(shared_ptr<Lobby> lobby, Options&& options)
battle_start_usecs(0),
should_copy_prev_states_to_current_states(0),
card_special(nullptr),
clients_done_in_mulligan_phase(false),
clients_done_in_redraw_initial_hand_phase(false),
num_pending_attacks_with_cards(0),
unknown_a14(0),
unknown_a15(0),
@@ -83,12 +81,14 @@ Server::Server(shared_ptr<Lobby> lobby, Options&& options)
Server::~Server() noexcept(false) {
if (this->logger_stack.size() != 1) {
throw logic_error(phosg::string_printf("incorrect logger stack size: expected 1, received %zu", this->logger_stack.size()));
throw logic_error(std::format("incorrect logger stack size: expected 1, received {}", this->logger_stack.size()));
}
delete this->logger_stack.back();
}
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->num_clients_present = 0;
this->overlay_state.clear();
@@ -101,10 +101,9 @@ void Server::init() {
this->card_special = 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.
// 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->state_flags = make_shared<StateFlags>();
@@ -173,9 +172,9 @@ 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 phosg::string_printf("@%04hX (#%04" PRIX32 " %s)", card_ref, ce->def.card_id.load(), name.c_str());
return std::format("@{:04X} (#{:04X} {})", card_ref, ce->def.card_id, name);
} else {
return phosg::string_printf("@%04hX (missing)", card_ref);
return std::format("@{:04X} (missing)", card_ref);
}
}
@@ -186,9 +185,9 @@ 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 phosg::string_printf("#%04hX (%s)", card_id, name.c_str());
return std::format("#{:04X} ({})", card_id, name);
} else {
return phosg::string_printf("#%04hX (missing)", card_id);
return std::format("#{:04X} (missing)", card_id);
}
}
@@ -229,6 +228,11 @@ int8_t Server::get_winner_team_id() const {
void Server::send(const void* data, size_t size, uint8_t command, bool enable_masking) const {
// Note: This function is (obviously) not part of the original implementation.
if (this->options.output_queue) {
this->options.output_queue->emplace_back(reinterpret_cast<const char*>(data), size);
}
if (this->has_lobby) {
auto l = this->lobby.lock();
if (!l) {
@@ -247,10 +251,6 @@ void Server::send(const void* data, size_t size, uint8_t command, bool enable_ma
size = masked_data.size();
}
// Note: Sega's servers sent battle commands with the 60 command. The handlers
// for 60, 62, and C9 on the client are identical, so we choose to use C9
// instead because it's unique to Episode 3, and therefore seems more
// appropriate to convey battle commands.
send_command(l, command, 0x00, data, size);
for (auto watcher_l : l->watcher_lobbies) {
send_command_if_not_loading(watcher_l, command, 0x00, data, size);
@@ -260,127 +260,101 @@ 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("Generated command")) {
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);
}
}
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.
// 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.
// NTE doesn't have the date_str2 field, but we send it anyway to make
// debugging easier.
// NTE doesn't have the date_str2 field, but we send it anyway to make debugging easier.
G_ServerVersionStrings_Ep3_6xB4x46 cmd;
cmd.version_signature.encode(this->options.is_nte() ? VERSION_SIGNATURE_NTE : VERSION_SIGNATURE, 1);
cmd.date_str1.encode(phosg::format_time(this->options.card_index->definitions_mtime() * 1000000), 1);
cmd.version_signature.encode(this->options.is_nte() ? VERSION_SIGNATURE_NTE : VERSION_SIGNATURE, Language::ENGLISH);
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);
cmd.date_str2.encode(phosg::string_printf("newserv %s compiled at %s", GIT_REVISION_HASH, build_date.c_str()), 1);
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, uint8_t language, bool is_nte) {
string Server::prepare_6xB6x41_map_definition(shared_ptr<const MapIndex::Map> map, Language language, bool is_nte) {
auto vm = map->version(language);
const auto& compressed = vm->compressed(is_nte);
phosg::StringWriter w;
uint32_t subcommand_size = (compressed.size() + sizeof(G_MapData_Ep3_6xB6x41) + 3) & (~3);
w.put<G_MapData_Ep3_6xB6x41>({{{{0xB6, 0, 0}, subcommand_size}, 0x41, {}}, vm->map->map_number.load(), compressed.size(), 0});
w.write(compressed);
uint32_t subcommand_size = (compressed->size() + sizeof(G_MapData_Ep3_6xB6x41) + 3) & (~3);
w.put<G_MapData_Ep3_6xB6x41>(
{{{{0xB6, 0, 0}, subcommand_size}, 0x41, {}}, vm->map->map_number.load(), compressed->size(), 0});
w.write(*compressed);
return std::move(w.str());
}
void Server::send_commands_for_joining_spectator(Channel& ch) const {
bool should_send_state = true;
if (this->setup_phase == SetupPhase::REGISTRATION) {
// If registration is still in progress, we only need to send the map data
// (if a map is even chosen yet)
if ((this->registration_phase != RegistrationPhase::REGISTERED) &&
(this->registration_phase != RegistrationPhase::BATTLE_STARTED)) {
should_send_state = false;
}
}
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());
this->log().info("Sending %c version of map %08" PRIX32, char_for_language_code(ch.language), this->last_chosen_map->map_number);
ch.send(0x6C, 0x00, data);
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);
}
if (should_send_state) {
ch.send(0xC9, 0x00, this->prepare_6xB4x03());
// If registration is still in progress, we don't need to send the battle state
if ((this->setup_phase != SetupPhase::REGISTRATION) ||
(this->registration_phase == RegistrationPhase::REGISTERED) ||
(this->registration_phase == RegistrationPhase::BATTLE_STARTED)) {
ch->send(0xC9, 0x00, this->prepare_6xB4x03());
for (uint8_t client_id = 0; client_id < 4; client_id++) {
auto ps = this->player_states[client_id];
if (ps) {
ch.send(0xC9, 0x00, ps->prepare_6xB4x02());
ch.send(0xC9, 0x00, ps->prepare_6xB4x04());
ch->send(0xC9, 0x00, ps->prepare_6xB4x02());
ch->send(0xC9, 0x00, ps->prepare_6xB4x04());
}
}
if (ch.version == Version::GC_EP3_NTE) {
if (ch->version == Version::GC_EP3_NTE) {
G_UpdateMap_Ep3NTE_6xB4x05 cmd;
cmd.state = *this->map_and_rules;
ch.send(0xC9, 0x00, cmd);
ch->send(0xC9, 0x00, cmd);
} else {
G_UpdateMap_Ep3_6xB4x05 cmd;
cmd.state = *this->map_and_rules;
ch.send(0xC9, 0x00, cmd);
ch->send(0xC9, 0x00, cmd);
}
// TODO: Sega does something like this; do we have to do this too?
// for (uint8_t client_id = 0; client_id < 4; client_id++) {
// (send 6xB4x4E, 6xB4x4C, 6xB4x4D for each set card)
// (send 6xB4x4F for client_id)
// }
ch.send(0xC9, 0x00, this->prepare_6xB4x07_decks_update());
// TODO: Sega sends 6xB4x05 here again; why? Is that necessary? They also
// send 6xB4x02 again for each player after that (but not 6xB4x04)
ch.send(0xC9, 0x00, this->prepare_6xB4x1C_names_update());
ch.send(0xC9, 0x00, this->prepare_6xB4x50_trap_tile_locations());
ch->send(0xC9, 0x00, this->prepare_6xB4x07_decks_update());
// TODO: Sega sends 6xB4x05 here again; why? Is that necessary? They also send 6xB4x02 again for each player after
// that (but not 6xB4x04)
ch->send(0xC9, 0x00, this->prepare_6xB4x1C_names_update());
ch->send(0xC9, 0x00, this->prepare_6xB4x50_trap_tile_locations());
{
G_LoadCurrentEnvironment_Ep3_6xB4x3B cmd_3B;
ch.send(0xC9, 0x00, &cmd_3B, sizeof(cmd_3B));
ch->send(0xC9, 0x00, &cmd_3B, sizeof(cmd_3B));
}
}
}
__attribute__((format(printf, 2, 3))) void Server::send_debug_message_printf(const char* fmt, ...) const {
auto l = this->lobby.lock();
if (l && (this->options.behavior_flags & Episode3::BehaviorFlag::ENABLE_STATUS_MESSAGES)) {
va_list va;
va_start(va, fmt);
std::string buf = phosg::string_vprintf(fmt, va);
va_end(va);
send_text_message(l, buf);
}
}
__attribute__((format(printf, 2, 3))) void Server::send_info_message_printf(const char* fmt, ...) const {
auto l = this->lobby.lock();
if (l) {
va_list va;
va_start(va, fmt);
std::string buf = phosg::string_vprintf(fmt, va);
va_end(va);
send_text_message(l, buf);
}
}
void Server::send_debug_command_received_message(
uint8_t client_id, uint8_t subsubcommand, const char* description) const {
this->log().debug("%hhu/CAx%02hhX %s", client_id, subsubcommand, description);
this->send_debug_message_printf("$C5%hhu/CAx%02hhX %s", client_id, subsubcommand, description);
this->log().debug_f("{}/CAx{:02X} {}", client_id, subsubcommand, description);
this->send_debug_message("$C5{}/CAx{:02X} {}", client_id, subsubcommand, description);
}
void Server::send_debug_command_received_message(uint8_t subsubcommand, const char* description) const {
this->log().debug("*/CAx%02hhX %s", subsubcommand, description);
this->send_debug_message_printf("$C5*/CAx%02hhX %s", subsubcommand, description);
this->log().debug_f("*/CAx{:02X} {}", subsubcommand, description);
this->send_debug_message("$C5*/CAx{:02X} {}", subsubcommand, description);
}
void Server::send_debug_message_if_error_code_nonzero(uint8_t client_id, int32_t error_code) const {
if (error_code < 0) {
this->send_debug_message_printf("$C4%hhu/ERROR -0x%zX", client_id, static_cast<ssize_t>(-error_code));
this->send_debug_message("$C4{}/ERROR -0x{:X}", client_id, static_cast<ssize_t>(-error_code));
} else if (error_code > 0) {
this->send_debug_message_printf("$C4%hhu/ERROR 0x%zX", client_id, static_cast<ssize_t>(error_code));
this->send_debug_message("$C4{}/ERROR 0x{:X}", client_id, static_cast<ssize_t>(error_code));
}
}
@@ -612,7 +586,14 @@ void Server::force_replace_assist_card(uint8_t client_id, uint16_t card_id) {
if (!ps) {
throw runtime_error("player does not exist");
}
ps->replace_assist_card_by_id(card_id);
if (card_id == 0xFFFF) {
ps->discard_set_assist_card();
this->check_for_destroyed_cards_and_send_6xB4x05_6xB4x02();
this->check_for_battle_end();
} else {
ps->replace_assist_card_by_id(card_id);
}
}
void Server::force_destroy_field_character(uint8_t client_id, size_t visible_index) {
@@ -621,8 +602,8 @@ void Server::force_destroy_field_character(uint8_t client_id, size_t visible_ind
throw 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
// 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;
for (size_t set_index = 0; set_index < 8; set_index++) {
if (!ps->set_cards[set_index]) {
@@ -671,9 +652,7 @@ void Server::check_for_destroyed_cards_and_send_6xB4x05_6xB4x02() {
}
bool Server::check_presence_entry(uint8_t client_id) const {
return (client_id < 4)
? this->presence_entries[client_id].player_present
: false;
return (client_id < 4) ? this->presence_entries[client_id].player_present : false;
}
void Server::clear_player_flags_after_dice_phase() {
@@ -834,9 +813,8 @@ void Server::draw_phase_after() {
if (this->current_team_turn1 == this->first_team_turn) {
if (this->map_and_rules->rules.overall_time_limit > 0) {
// Battle time limits are specified in increments of 5 minutes.
// Note: This part is not based on the original code because the timing
// facilities used are different.
// Battle time limits are specified in increments of 5 minutes. This part is not based on the original code
// because the timing facilities used are different.
uint64_t limit_5mins = this->map_and_rules->rules.overall_time_limit;
uint64_t end_usecs = this->battle_start_usecs + (limit_5mins * 300 * 1000 * 1000);
if (phosg::now() >= end_usecs) {
@@ -932,9 +910,8 @@ void Server::end_attack_list_for_client(uint8_t client_id) {
void Server::end_action_phase() {
this->num_pending_attacks = 0;
this->unknown_a15 = 1;
// Annoyingly, this is the original logic. We use an enum because it appears
// that this can only ever be 0 or 2, but we may have to delete the enum if
// that turns out to be false.
// Annoyingly, this is the original logic. We use an enum because it appears that this can only ever be 0 or 2, but
// we may have to delete the enum if that turns out to be false.
this->action_subphase = static_cast<ActionSubphase>(static_cast<uint8_t>(this->action_subphase) + 2);
if (this->options.is_nte()) {
this->unknown_8023EEF4();
@@ -949,62 +926,62 @@ 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::DEBUG)) {
if (log.should_log(phosg::LogLevel::L_DEBUG)) {
string s = pa->str(this->shared_from_this());
log.debug("input: %s", s.c_str());
log.debug_f("input: {}", s);
}
if (client_id >= 4) {
this->ruler_server->error_code3 = -0x78;
log.debug("failed: invalid client ID");
log.debug_f("failed: invalid client ID");
return false;
}
auto ps = this->player_states[client_id];
if (!ps) {
this->ruler_server->error_code3 = -0x72;
log.debug("failed: player not present");
log.debug_f("failed: player not present");
return false;
}
if (pa->action_card_refs[0] == 0xFFFF) {
if (pa->defense_card_ref != 0xFFFF) {
pa->action_card_refs[0] = pa->defense_card_ref;
log.debug("moved defense card ref to action card ref 0");
log.debug_f("moved defense card ref to action card ref 0");
}
} else {
pa->defense_card_ref = pa->action_card_refs[0];
log.debug("moved action card ref 0 to defense card ref");
log.debug_f("moved action card ref 0 to defense card ref");
}
if (!this->ruler_server->is_attack_or_defense_valid(*pa)) {
log.debug("failed: attack or defense not valid");
log.debug_f("failed: attack or defense not valid");
return false;
}
int16_t ally_atk_result = this->send_6xB4x33_remove_ally_atk_if_needed(*pa);
if (ally_atk_result == 1) {
log.debug("pending: need ally approval");
log.debug_f("pending: need ally approval");
return true;
} else if (ally_atk_result == -1) {
log.debug("failed: ally declined");
log.debug_f("failed: ally declined");
return false;
}
if (this->num_pending_attacks >= 0x20) {
this->ruler_server->error_code3 = -0x71;
log.debug("failed: too many pending attacks");
log.debug_f("failed: too many pending attacks");
return false;
}
size_t attack_index = this->num_pending_attacks++;
this->pending_attacks[attack_index] = *pa;
if (log.should_log(phosg::LogLevel::DEBUG)) {
if (log.should_log(phosg::LogLevel::L_DEBUG)) {
string pa_str = this->pending_attacks[attack_index].str(this->shared_from_this());
log.debug("set pending attack %zu: %s", attack_index, pa_str.c_str());
log.debug_f("set pending attack {}: {}", attack_index, pa_str);
}
ps->set_action_cards_for_action_state(*pa);
log.debug("set action cards");
log.debug_f("set action cards");
auto card = this->card_for_set_card_ref(this->send_6xB4x06_if_card_ref_invalid(pa->attacker_card_ref, 1));
if (card) {
card->card_flags |= 0x400;
@@ -1013,8 +990,7 @@ bool Server::enqueue_attack_or_defense(uint8_t client_id, ActionState* pa) {
card_ps->send_6xB4x04_if_needed();
}
}
card = this->card_for_set_card_ref(this->send_6xB4x06_if_card_ref_invalid(
pa->original_attacker_card_ref, 2));
card = this->card_for_set_card_ref(this->send_6xB4x06_if_card_ref_invalid(pa->original_attacker_card_ref, 2));
if (card) {
card = this->card_for_set_card_ref(pa->target_card_refs[0]);
if (card) {
@@ -1056,7 +1032,7 @@ uint32_t Server::get_random_raw() {
if (this->options.opt_rand_stream) {
this->options.opt_rand_stream->readx(&ret, sizeof(ret));
} else {
ret = random_from_optional_crypt(this->options.opt_rand_crypt);
ret = this->options.rand_crypt->next();
}
if (this->battle_record && this->battle_record->writable()) {
@@ -1108,8 +1084,7 @@ void Server::move_phase_after() {
auto ps = this->player_states[client_id];
if (ps) {
auto sc_card = ps->get_sc_card();
if (sc_card && (sc_card->card_flags & 0x80) &&
(sc_card->loc.x == trap_x) && (sc_card->loc.y == trap_y)) {
if (sc_card && (sc_card->card_flags & 0x80) && (sc_card->loc.x == trap_x) && (sc_card->loc.y == trap_y)) {
should_trigger = true;
break;
}
@@ -1119,7 +1094,7 @@ void Server::move_phase_after() {
continue;
}
static const array<vector<uint16_t>, 5> default_trap_card_ids = {
static const array<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},
// Blue: Gold Rush, Charity, Requiem
@@ -1133,7 +1108,7 @@ void Server::move_phase_after() {
const 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);
trap_card_ids = &DEFAULT_TRAP_CARD_IDS.at(trap_type);
}
// This is the original implementation. We do something smarter instead.
@@ -1153,9 +1128,7 @@ void Server::move_phase_after() {
auto ps = this->player_states[client_id];
if (ps) {
auto sc_card = ps->get_sc_card();
if (sc_card &&
(abs(sc_card->loc.x - trap_x) < 2) &&
(abs(sc_card->loc.y - trap_y) < 2) &&
if (sc_card && (abs(sc_card->loc.x - trap_x) < 2) && (abs(sc_card->loc.y - trap_y) < 2) &&
ps->replace_assist_card_by_id(trap_card_id)) {
G_EnqueueAnimation_Ep3_6xB4x2C cmd;
cmd.change_type = 0x01;
@@ -1181,14 +1154,12 @@ void Server::move_phase_after() {
// this->chosen_trap_tile_index_of_type[trap_type] = new_index;
// this->send_6xB4x50();
// }
// We instead use an implementation that consumes a constant amount of
// randomness per pass.
// We instead use an implementation that consumes a constant amount of randomness per pass.
if (this->num_trap_tiles_of_type[trap_type] == 2) {
this->chosen_trap_tile_index_of_type[trap_type] ^= 1;
this->send_6xB4x50_trap_tile_locations();
} else if (this->num_trap_tiles_of_type[trap_type] > 2) {
// Generate a new random index, but forbid it from matching the existing
// index
// Generate a new random index, but forbid it from matching the existing index
uint8_t new_index = this->get_random(this->num_trap_tiles_of_type[trap_type] - 1);
if (new_index >= this->chosen_trap_tile_index_of_type[trap_type]) {
new_index++;
@@ -1257,8 +1228,7 @@ int8_t Server::send_6xB4x33_remove_ally_atk_if_needed(const ActionState& pa) {
for (size_t z = 0; z < 4; z++) {
auto ally_ps = this->get_player_state(z);
if ((z != setter_client_id) && ally_ps) {
if ((ally_ps->get_team_id() == setter_ps->get_team_id()) &&
(ally_ps->get_atk_points() >= ally_cost)) {
if ((ally_ps->get_team_id() == setter_ps->get_team_id()) && (ally_ps->get_atk_points() >= ally_cost)) {
ally_has_sufficient_atk = true;
}
}
@@ -1384,11 +1354,10 @@ void Server::set_client_id_ready_to_advance_phase(uint8_t client_id, BattlePhase
ps->assist_flags |= AssistFlag::ELIGIBLE_FOR_DICE_BOOST;
}
} else {
// TODO: It'd be nice to do this in a constant-randomness way, but I'm
// lazy, and this matches Sega's original implementation. The less-lazy
// way to do it would be to roll three dice: one in the range [1, N],
// one in the range [3, N], and one in the range [1, 2] to decide
// whether to swap the first two results.
// TODO: It'd be nice to do this in a constant-randomness way, but I'm lazy, and this matches Sega's original
// implementation. The less-lazy way to do it would be to roll three dice: one in the range [1, 2] to decide
// which of ATK or DEF will be boosted, then roll the ATK die in range [1, N] (or [3, N] if it's boosted), and
// do the same for the DEF die.
for (size_t z = 0; z < 200; z++) {
ps->roll_main_dice_or_apply_after_effects();
if ((ps->get_atk_points() >= 3) || (ps->get_def_points() >= 3)) {
@@ -1439,12 +1408,14 @@ void Server::set_phase_after() {
if (ps) {
auto card = ps->get_sc_card();
if (card) {
this->card_special->apply_action_conditions(EffectWhen::AFTER_SET_PHASE, nullptr, card, is_nte ? 0x1F : 0x04, nullptr);
this->card_special->apply_action_conditions(
EffectWhen::AFTER_SET_PHASE, nullptr, card, is_nte ? 0x1F : 0x04, nullptr);
}
for (size_t set_index = 0; set_index < 8; set_index++) {
auto card = ps->get_set_card(set_index);
if (card) {
this->card_special->apply_action_conditions(EffectWhen::AFTER_SET_PHASE, nullptr, card, is_nte ? 0x1F : 0x04, nullptr);
this->card_special->apply_action_conditions(
EffectWhen::AFTER_SET_PHASE, nullptr, card, is_nte ? 0x1F : 0x04, nullptr);
}
}
}
@@ -1502,9 +1473,7 @@ void Server::set_phase_after() {
for (size_t client_id = 0; client_id < 4; client_id++) {
auto ps = this->player_states[client_id];
if (ps &&
(ps->get_assist_turns_remaining() == 90) &&
(ps->assist_delay_turns < 1)) {
if (ps && (ps->get_assist_turns_remaining() == 90) && (ps->assist_delay_turns < 1)) {
ps->discard_set_assist_card();
ps->update_hand_and_equip_state_and_send_6xB4x02_if_needed();
}
@@ -1638,8 +1607,7 @@ void Server::setup_and_start_battle() {
size_t num_trap_tiles = 0;
for (size_t y = 0; y < 0x10; y++) {
for (size_t x = 0; x < 0x10; x++) {
if ((this->overlay_state.tiles[y][x] == (trap_type | 0x40)) &&
(num_trap_tiles < 8)) {
if ((this->overlay_state.tiles[y][x] == (trap_type | 0x40)) && (num_trap_tiles < 8)) {
this->trap_tile_locs[trap_type][num_trap_tiles][0] = x;
this->trap_tile_locs[trap_type][num_trap_tiles][1] = y;
num_trap_tiles++;
@@ -1647,7 +1615,6 @@ void Server::setup_and_start_battle() {
}
}
this->num_trap_tiles_of_type[trap_type] = num_trap_tiles;
if (num_trap_tiles > 0) {
this->chosen_trap_tile_index_of_type[trap_type] = this->get_random(num_trap_tiles);
}
@@ -1692,8 +1659,7 @@ void Server::setup_and_start_battle() {
this->send_6xB4x46();
// Re-send game metadata to spectator teams, since loading the battle scene
// seems to delete it
// Re-send game metadata to spectator teams, since loading the battle scene seems to delete it
auto l = this->lobby.lock();
if (l) {
send_ep3_update_game_metadata(l);
@@ -1717,11 +1683,7 @@ G_SetStateFlags_Ep3_6xB4x03 Server::prepare_6xB4x03() const {
cmd.state.tournament_flag = this->options.tournament ? 1 : 0;
for (size_t z = 0; z < 4; z++) {
auto ps = this->player_states[z];
if (!ps) {
cmd.state.client_sc_card_types[z] = CardType::INVALID_FF;
} else {
cmd.state.client_sc_card_types[z] = ps->get_sc_card_type();
}
cmd.state.client_sc_card_types[z] = ps ? ps->get_sc_card_type() : CardType::INVALID_FF;
}
return cmd;
}
@@ -1734,26 +1696,25 @@ void Server::update_battle_state_flags_and_send_6xB4x03_if_needed(bool always_se
}
}
// Returns true if the battle can begin
bool Server::update_registration_phase() {
// Returns true if the battle can begin
auto log = this->log_stack("update_registration_phase: ");
if (this->setup_phase != SetupPhase::REGISTRATION) {
log.debug("setup_phase is not REGISTRATION");
log.debug_f("setup_phase is not REGISTRATION");
return false;
}
if (this->map_and_rules->num_players == 0) {
this->registration_phase = RegistrationPhase::AWAITING_NUM_PLAYERS;
log.debug("registration_phase set to AWAITING_NUM_PLAYERS");
log.debug_f("registration_phase set to AWAITING_NUM_PLAYERS");
this->update_battle_state_flags_and_send_6xB4x03_if_needed();
return false;
}
if (this->map_and_rules->num_players != this->num_clients_present) {
this->registration_phase = RegistrationPhase::AWAITING_PLAYERS;
log.debug("registration_phase set to AWAITING_PLAYERS");
log.debug_f("registration_phase set to AWAITING_PLAYERS");
this->update_battle_state_flags_and_send_6xB4x03_if_needed();
return false;
}
@@ -1767,20 +1728,20 @@ bool Server::update_registration_phase() {
if (num_team0_registered_players != this->map_and_rules->num_team0_players) {
this->registration_phase = RegistrationPhase::AWAITING_DECKS;
log.debug("registration_phase set to AWAITING_DECKS");
log.debug_f("registration_phase set to AWAITING_DECKS");
this->update_battle_state_flags_and_send_6xB4x03_if_needed();
return false;
}
this->registration_phase = RegistrationPhase::REGISTERED;
this->update_battle_state_flags_and_send_6xB4x03_if_needed();
log.debug("battle can begin");
log.debug_f("battle can begin");
return true;
}
const unordered_map<uint8_t, Server::handler_t> Server::subcommand_handlers({
{0x0B, &Server::handle_CAx0B_mulligan_hand},
{0x0C, &Server::handle_CAx0C_end_mulligan_phase},
{0x0B, &Server::handle_CAx0B_redraw_initial_hand},
{0x0C, &Server::handle_CAx0C_end_redraw_initial_hand_phase},
{0x0D, &Server::handle_CAx0D_end_non_action_phase},
{0x0E, &Server::handle_CAx0E_discard_card_from_hand},
{0x0F, &Server::handle_CAx0F_set_card_from_hand},
@@ -1809,7 +1770,8 @@ void Server::on_server_data_input(shared_ptr<Client> sender_c, const string& dat
size_t expected_size = header.size * 4;
if (expected_size < data.size()) {
phosg::print_data(stderr, data);
throw runtime_error(phosg::string_printf("command is incomplete: expected %zX bytes, received %zX bytes", expected_size, data.size()));
throw 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");
@@ -1835,7 +1797,7 @@ void Server::on_server_data_input(shared_ptr<Client> sender_c, const string& dat
}
}
void Server::handle_CAx0B_mulligan_hand(shared_ptr<Client>, const string& data) {
void Server::handle_CAx0B_redraw_initial_hand(shared_ptr<Client>, const 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) {
@@ -1854,20 +1816,20 @@ void Server::handle_CAx0B_mulligan_hand(shared_ptr<Client>, const string& data)
if (!ps) {
error_code = -0x72;
} else {
ps->do_mulligan();
ps->redraw_initial_hand();
}
}
if (!this->options.is_nte() || (error_code == 0)) {
G_ActionResult_Ep3_6xB4x1E out_cmd;
out_cmd.sequence_num = in_cmd.header.sequence_num.load();
out_cmd.sequence_num = in_cmd.header.sequence_num;
out_cmd.error_code = error_code;
this->send(out_cmd);
}
this->send_debug_message_if_error_code_nonzero(in_cmd.client_id, error_code);
}
void Server::handle_CAx0C_end_mulligan_phase(shared_ptr<Client>, const string& data) {
void Server::handle_CAx0C_end_redraw_initial_hand_phase(shared_ptr<Client>, const 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) {
@@ -1875,8 +1837,7 @@ void Server::handle_CAx0C_end_mulligan_phase(shared_ptr<Client>, const string& d
}
int32_t error_code = 0;
if ((this->setup_phase != SetupPhase::HAND_REDRAW_OPTION) &&
(this->setup_phase != SetupPhase::STARTER_ROLLS)) {
if ((this->setup_phase != SetupPhase::HAND_REDRAW_OPTION) && (this->setup_phase != SetupPhase::STARTER_ROLLS)) {
error_code = -0x5D;
}
@@ -1898,13 +1859,13 @@ void Server::handle_CAx0C_end_mulligan_phase(shared_ptr<Client>, const string& d
if (!ps) {
error_code = -0x72;
} else {
this->clients_done_in_mulligan_phase[in_cmd.client_id] = true;
this->clients_done_in_redraw_initial_hand_phase[in_cmd.client_id] = true;
ps->assist_flags |= AssistFlag::READY_TO_END_PHASE;
ps->update_hand_and_equip_state_and_send_6xB4x02_if_needed();
bool all_clients_ready = true;
for (size_t z = 0; z < 4; z++) {
if (this->player_states[z] && !this->clients_done_in_mulligan_phase[z]) {
if (this->player_states[z] && !this->clients_done_in_redraw_initial_hand_phase[z]) {
all_clients_ready = false;
break;
}
@@ -2146,15 +2107,14 @@ void Server::handle_CAx13_update_map_during_setup_t(shared_ptr<Client> c, const
(this->registration_phase != RegistrationPhase::REGISTERED) &&
(this->registration_phase != RegistrationPhase::BATTLE_STARTED)) {
*this->map_and_rules = in_cmd.map_and_rules_state;
// The client will likely send incorrect values for the extended rules (or
// in the case of NTE, no values at all, since the Rules structure is
// smaller). So, use the values from the last chosen map if applicable, or
// the values from the $dicerange command if available.
uint8_t language = c ? c->language() : 1;
// The client will likely send incorrect values for the extended rules (or in the case of NTE, no values at all,
// since the Rules structure is smaller). So, use the values from the last chosen map if applicable, or the values
// from the $dicerange command if available.
Language language = c ? c->language() : Language::ENGLISH;
const Rules* map_rules = this->last_chosen_map ? &this->last_chosen_map->version(language)->map->default_rules : nullptr;
auto& server_rules = this->map_and_rules->rules;
// NTE can specify the DEF dice value range in its Rules struct, so we use
// that unless the map or $dicerange overrides it.
// NTE can specify the DEF dice value range in its Rules struct, so we use that unless the map or $dicerange
// overrides it.
server_rules.def_dice_value_range = (map_rules && (map_rules->def_dice_value_range != 0xFF))
? map_rules->def_dice_value_range
: (this->def_dice_value_range_override != 0xFF)
@@ -2173,8 +2133,7 @@ void Server::handle_CAx13_update_map_during_setup_t(shared_ptr<Client> c, const
? this->def_dice_value_range_2v1_override
: 0;
// If this match is part of a tournament, ignore the rules sent by the
// client and use the tournament rules instead.
// If this match is part of a tournament, ignore the rules sent by the client and use the tournament rules instead.
if (this->options.tournament) {
this->map_and_rules->rules = this->options.tournament->get_rules();
}
@@ -2229,7 +2188,7 @@ void Server::handle_CAx14_update_deck_during_setup(shared_ptr<Client>, const str
}
}
if (verify_error) {
throw runtime_error(phosg::string_printf("invalid deck: -0x%" PRIX32, verify_error));
throw 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);
@@ -2256,10 +2215,9 @@ void Server::handle_CAx15_unused_hard_reset_server_state(shared_ptr<Client>, con
const auto& in_cmd = check_size_t<G_HardResetServerState_Ep3_CAx15>(data);
this->send_debug_command_received_message(in_cmd.header.subsubcommand, "HARD RESET");
// In the original implementation, this command recreates the server object.
// This is possible because the dispatch function is not part of the server
// object in the original implementation; however, in our implementation, it
// is, so we don't support this. The original implementation did this:
// In the original implementation, this command recreates the server object. This is possible because the dispatch
// function is not part of the server object in the original implementation; however, in our implementation, it is,
// so we don't support this. The original implementation did this:
// this->base()->recreate_server(); // Destroys *this, which we can't do
// root_card_server = this->server;
// *this->map_and_rules = *this->initial_map_and_rules;
@@ -2279,8 +2237,8 @@ void Server::handle_CAx1B_update_player_name(shared_ptr<Client>, const string& d
this->name_entries_valid[in_cmd.entry.client_id] = false;
}
// Note: This check is not part of the original code. This replaces a
// disconnecting player with a CPU if the battle is in progress.
// Note: This check is not part of the original code. This replaces a disconnecting player with a CPU if the battle
// is in progress.
auto l = this->lobby.lock();
if (l && !l->clients[in_cmd.entry.client_id]) {
this->name_entries[in_cmd.entry.client_id].is_cpu_player = 1;
@@ -2333,8 +2291,8 @@ void Server::handle_CAx1D_start_battle(shared_ptr<Client>, const string& data) {
auto l = this->lobby.lock();
if (l) {
// Note: Sega's implementation doesn't set EX results values here; they
// did it at game join time instead. We do it here for code simplicity.
// Note: Sega's implementation doesn't set EX results values here; they did it at game join time instead. We do
// it here for code simplicity.
if (!this->options.is_nte() && l->ep3_ex_result_values) {
this->send(*l->ep3_ex_result_values);
}
@@ -2378,8 +2336,7 @@ void Server::handle_CAx28_end_defense_list(shared_ptr<Client>, const string& dat
for (size_t z = 0; z < 4; z++) {
auto ps = this->player_states[z];
if (ps && (this->current_team_turn1 != ps->get_team_id())) {
if (!ps->get_sc_card()->check_card_flag(2) &&
(this->defense_list_ended_for_client[z] == 0)) {
if (!ps->get_sc_card()->check_card_flag(2) && (this->defense_list_ended_for_client[z] == 0)) {
all_defense_lists_ended = false;
break;
}
@@ -2525,8 +2482,7 @@ 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) {
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.
// 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) {
@@ -2539,8 +2495,8 @@ void Server::handle_CAx40_map_list_request(shared_ptr<Client> sender_c, const st
}
size_t num_players = l ? l->count_clients() : 1;
uint8_t language = sender_c ? sender_c->language() : 1;
const auto& list_data = this->options.map_index->get_compressed_list(num_players, language);
Language language = sender_c ? sender_c->language() : Language::ENGLISH;
const auto& list_data = this->options.map_index->get_compressed_list(num_players, language, this->options.is_nte());
phosg::StringWriter w;
uint32_t subcommand_size = (list_data.size() + sizeof(G_MapList_Ep3_6xB6x40) + 3) & (~3);
@@ -2566,15 +2522,16 @@ void Server::send_6xB6x41_to_all_clients() const {
if (!c) {
return;
}
if (map_commands_by_language.size() <= c->language()) {
map_commands_by_language.resize(c->language() + 1);
size_t lang_index = static_cast<size_t>(c->language());
if (map_commands_by_language.size() <= lang_index) {
map_commands_by_language.resize(lang_index + 1);
}
if (map_commands_by_language[c->language()].empty()) {
map_commands_by_language[c->language()] = this->prepare_6xB6x41_map_definition(
if (map_commands_by_language[lang_index].empty()) {
map_commands_by_language[lang_index] = this->prepare_6xB6x41_map_definition(
this->last_chosen_map, c->language(), this->options.is_nte());
}
this->log().info("Sending %c version of map %08" PRIX32, char_for_language_code(c->language()), this->last_chosen_map->map_number);
send_command(c, 0x6C, 0x00, map_commands_by_language[c->language()]);
this->log().info_f("Sending {} version of map {:08X}", name_for_language(c->language()), this->last_chosen_map->map_number);
send_command(c, 0x6C, 0x00, map_commands_by_language[lang_index]);
};
for (const auto& c : l->clients) {
send_to_client(c);
@@ -2586,20 +2543,18 @@ 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
// 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) {
if (!data.empty()) {
this->battle_record->add_command(
BattleRecord::Event::Type::BATTLE_COMMAND, std::move(data));
this->battle_record->add_command(BattleRecord::Event::Type::BATTLE_COMMAND, std::move(data));
break;
}
}
}
} else {
auto out_data = this->prepare_6xB6x41_map_definition(this->last_chosen_map, 1, false);
auto out_data = this->prepare_6xB6x41_map_definition(this->last_chosen_map, Language::ENGLISH, false);
this->send(out_data.data(), out_data.size(), 0x6C, false);
}
}
@@ -2608,7 +2563,7 @@ void Server::handle_CAx41_map_request(shared_ptr<Client>, const 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)) {
this->last_chosen_map = this->options.map_index->for_number(cmd.map_number);
this->last_chosen_map = this->options.map_index->map_for_id(cmd.map_number);
this->send_6xB6x41_to_all_clients();
}
}
@@ -2634,8 +2589,7 @@ void Server::handle_CAx49_card_counts(shared_ptr<Client>, const 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");
// Note: Sega's implmentation completely ignores this command. This
// implementation is not based on the original code.
// Note: Sega's implmentation completely ignores this command. This implementation is not based on the original code.
auto& dest_counts = this->client_card_counts[in_cmd.header.sender_client_id];
dest_counts = in_cmd.card_id_to_count;
decrypt_trivial_gci_data(dest_counts.data(), dest_counts.bytes(), in_cmd.basis);
@@ -2782,7 +2736,7 @@ void Server::unknown_8023EEF4() {
auto log = this->log_stack("unknown_8023EEF4: ");
if (this->unknown_a14 >= 0x20) {
log.debug("unknown_a14 too large (0x%" PRIX32 ")", this->unknown_a14);
log.debug_f("unknown_a14 too large (0x{:X})", this->unknown_a14);
return;
}
@@ -2791,34 +2745,34 @@ void Server::unknown_8023EEF4() {
auto card = this->attack_cards[this->unknown_a14];
if (this->get_current_team_turn() == card->get_team_id()) {
ActionState as = this->pending_attacks_with_cards[this->unknown_a14];
if (log.should_log(phosg::LogLevel::DEBUG)) {
log.debug("card @%04hX #%04hX can attack", card->get_card_ref(), card->get_card_id());
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("as: %s", as_str.c_str());
log.debug_f("as: {}", as_str);
}
if (is_nte) {
this->replace_targets_due_to_destruction_nte(&as);
} else {
this->replace_targets_due_to_destruction_or_conditions(&as);
}
if (log.should_log(phosg::LogLevel::DEBUG)) {
if (log.should_log(phosg::LogLevel::L_DEBUG)) {
string as_str = as.str(this->shared_from_this());
log.debug("as after target replacement: %s", as_str.c_str());
log.debug_f("as after target replacement: {}", as_str);
}
if (this->any_target_exists_for_attack(as)) {
log.debug("as is valid");
log.debug_f("as is valid");
break;
} else {
log.debug("as is not valid");
log.debug_f("as is not valid");
}
} else {
log.debug("card @%04hX #%04hX cannot attack (wrong turn)", card->get_card_ref(), card->get_card_id());
log.debug_f("card @{:04X} #{:04X} cannot attack (wrong turn)", card->get_card_ref(), card->get_card_id());
}
this->unknown_a14++;
}
if (this->unknown_a14 < this->num_pending_attacks_with_cards) {
log.debug("a14 (%" PRIu32 ") < num_pending_attacks_with_cards (%" PRIu32 ")", this->unknown_a14, this->num_pending_attacks_with_cards);
log.debug_f("a14 ({}) < num_pending_attacks_with_cards ({})", this->unknown_a14, this->num_pending_attacks_with_cards);
this->defense_list_ended_for_client.clear(false);
G_UpdateAttackTargets_Ep3_6xB4x29 cmd;
@@ -2892,10 +2846,9 @@ void Server::execute_bomb_assist_effect() {
for (size_t client_id = 0; client_id < 4; client_id++) {
auto ps = this->player_states[client_id];
// Possible bug: shouldn't we check should_block_assist_effects_for_client
// here too? If the player has a card with the same HP as another one that
// would be destroyed, it looks like the card can be destroyed even if the
// client should be immune to assist effects here.
// Possible bug: shouldn't we check should_block_assist_effects_for_client here too? If the player has a card with
// the same HP as another one that would be destroyed, it looks like the card can be destroyed even if the client
// should be immune to assist effects here.
if (ps) {
for (size_t set_index = 0; set_index < 8; set_index++) {
auto card = ps->get_set_card(set_index);
@@ -2925,10 +2878,7 @@ void Server::replace_targets_due_to_destruction_nte(ActionState* as) {
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()) {
if (set_card && (set_card != target_card) && !(set_card->card_flags & 2) && set_card->is_guard_item()) {
found_guard_item = set_card;
break;
}
@@ -3047,8 +2997,8 @@ void Server::replace_targets_due_to_destruction_or_conditions(ActionState* as) {
}
}
// Note: The original code only writes a single FFFF after the last card ref
// in this array; we instead clear the entire array.
// Note: The original code only writes a single FFFF after the last card ref in this array; we instead clear the
// entire array.
as->target_card_refs.clear(0xFFFF);
for (size_t z = 0; z < phase1_replaced_card_refs.size(); z++) {
as->target_card_refs[z] = this->send_6xB4x06_if_card_ref_invalid(phase1_replaced_card_refs[z], 4);
@@ -3070,8 +3020,7 @@ void Server::replace_targets_due_to_destruction_or_conditions(ActionState* as) {
}
}
// Note: This is different from the original code in the same way as above: we
// clear the entire array first.
// Note: This is different from the original code in the same way as above: we clear the entire array first.
as->target_card_refs.clear(0xFFFF);
for (size_t z = 0; z < phase2_replaced_card_refs.size(); z++) {
as->target_card_refs[z] = this->send_6xB4x06_if_card_ref_invalid(phase2_replaced_card_refs[z], 4);
@@ -3142,13 +3091,13 @@ void Server::unknown_802402F4() {
if (ps && (this->current_team_turn2 == ps->get_team_id())) {
auto card = ps->get_sc_card();
if (card) {
log.debug("SC card has action chain");
log.debug_f("SC card has action chain");
card->compute_action_chain_results(true, false);
}
for (size_t set_index = 0; set_index < 8; set_index++) {
card = ps->get_set_card(set_index);
if (card) {
log.debug("set card %zu has action chain", set_index);
log.debug_f("set card {} has action chain", set_index);
card->compute_action_chain_results(true, false);
}
}
@@ -3156,8 +3105,7 @@ void Server::unknown_802402F4() {
}
}
vector<shared_ptr<Card>> Server::const_cast_set_cards_v(
const vector<shared_ptr<const Card>>& cards) {
vector<shared_ptr<Card>> Server::const_cast_set_cards_v(const vector<shared_ptr<const Card>>& cards) {
// TODO: This is dumb. Figure out a not-dumb way to do this.
vector<shared_ptr<Card>> ret;
for (auto const_card : cards) {
+63 -66
View File
@@ -20,62 +20,53 @@ struct Lobby;
namespace Episode3 {
/**
* This implementation of Episode 3 battles is derived from Sega's original
* server implementation, reverse-engineered from the Episode 3 client
* executable. The control flow, function breakdown, and structure definitions
* in these files map very closely to how their server implementation was
* written; notable differences (due to necessary environment differences or bug
* fixes) are described in the comments therein.
*
* The following files are direct reverse-engineerings of Sega's original code,
* except where noted in the comments:
* AssistServer.hh/cc
* Card.hh/cc
* CardSpecial.hh/cc
* DeckState.hh/cc
* MapState.hh/cc
* PlayerState.hh/cc
* PlayerStateSubordinates.hh/cc
* RulerServer.hh/cc
* Server.hh/cc
*
* There are likely undiscovered bugs in this code, some originally written by
* Sega, but more written by me as I manually transcribed and updated this code.
*
* Class ownership levels (classes may only contain weak_ptrs, not shared_ptrs,
* to classes at the same or higher level):
* - Server
* - - RulerServer
* - - - AssistServer
* - - - CardSpecial
* - - - - StateFlags
* - - - - DeckEntry
* - - - - PlayerState
* - - - - - Card
* - - - - - - CardShortStatus
* - - - - - - DeckState
* - - - - - - HandAndEquipState
* - - - - - - MapAndRulesState / OverlayState
* - - - - - - - Everything within DataIndexes
*/
// This implementation of Episode 3 battles is derived from Sega's original server implementation, reverse-engineered
// from the Episode 3 client executable. The control flow, function breakdown, and structure definitions in these files
// map very closely to how their server implementation was written; notable differences (due to necessary environment
// differences or bug fixes) are described in the comments therein. There are likely undiscovered bugs in this code,
// some originally written by Sega, but more written by me as I manually transcribed and updated this code.
// The following files are direct reverse-engineerings of Sega's original code, except where noted in the comments:
// AssistServer.hh/cc
// Card.hh/cc
// CardSpecial.hh/cc
// DeckState.hh/cc
// MapState.hh/cc
// PlayerState.hh/cc
// PlayerStateSubordinates.hh/cc
// 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):
// - Server
// - - RulerServer
// - - - AssistServer
// - - - CardSpecial
// - - - - StateFlags
// - - - - DeckEntry
// - - - - PlayerState
// - - - - - Card
// - - - - - - CardShortStatus
// - - - - - - DeckState
// - - - - - - HandAndEquipState
// - - - - - - MapAndRulesState / OverlayState
// - - - - - - - Everything within DataIndexes
class Server : public std::enable_shared_from_this<Server> {
// In the original code, there is a TCardServerBase class and a TCardServer
// class, with the former containing some basic parts of the game state and
// a pointer to the latter. It seems these two classes exist (instead of one
// big class) so that the force reset command could be implemented; however,
// it appears that that command is never sent by the client, so we combine
// the two classes into one in our implementation.
// In the original code, there is a TCardServerBase class and a TCardServer class, with the former containing some
// basic parts of the game state and a pointer to the latter. It seems these two classes exist (instead of one big
// class) so that the force reset command could be implemented; however, it appears that that command is never sent
// by the client, so we combine the two classes into one in our implementation.
public:
struct Options {
std::shared_ptr<const CardIndex> card_index;
std::shared_ptr<const MapIndex> map_index;
uint32_t behavior_flags;
std::shared_ptr<phosg::StringReader> opt_rand_stream;
std::shared_ptr<PSOLFGEncryption> opt_rand_crypt;
std::shared_ptr<RandomGenerator> rand_crypt;
std::shared_ptr<const Tournament> tournament;
std::array<std::vector<uint16_t>, 5> trap_card_ids;
std::shared_ptr<std::deque<std::string>> output_queue; // For replay testing
inline bool is_nte() const {
return (this->behavior_flags & BehaviorFlag::IS_TRIAL_EDITION);
@@ -109,7 +100,7 @@ public:
for (size_t z = 0; z < count; z++) {
if (refs[z] != 0xFFFF) {
std::string ref_str = this->debug_str_for_card_ref(refs[z]);
ret += phosg::string_printf("%zu:%s ", z, ref_str.c_str());
ret += std::format("{}:{} ", z, ref_str);
}
}
if (ret.size() > 1) {
@@ -130,6 +121,9 @@ public:
int8_t get_winner_team_id() const;
// Note: Sega's servers sent battle commands with the 60 command. The handlers for 60, 62, and C9 on the client are
// identical, so we choose to use C9 instead because it's unique to Episode 3, and therefore seems more appropriate
// to convey Episode 3 battle commands.
template <typename T>
void send(const T& cmd, uint8_t command = 0xC9, bool enable_masking = true) const {
if (cmd.header.size != sizeof(cmd) / 4) {
@@ -145,20 +139,23 @@ public:
this->send(&cmd, cmd.header.size * 4, command, enable_masking);
}
void send(const void* data, size_t size, uint8_t command = 0xC9, bool enable_masking = true) const;
void send_commands_for_joining_spectator(Channel& ch) const;
void send_commands_for_joining_spectator(std::shared_ptr<Channel> ch) const;
void force_battle_result(uint8_t surrendered_client_id, bool set_winner);
void force_replace_assist_card(uint8_t client_id, uint16_t card_id);
void force_destroy_field_character(uint8_t client_id, size_t set_index);
__attribute__((format(printf, 2, 3))) void send_debug_message_printf(const char* fmt, ...) const;
__attribute__((format(printf, 2, 3))) void send_info_message_printf(const char* fmt, ...) const;
void send_debug_command_received_message(
uint8_t client_id, uint8_t subsubcommand, const char* description) const;
void send_debug_command_received_message(
uint8_t subsubcommand, const char* description) const;
void send_debug_message_if_error_code_nonzero(
uint8_t client_id, int32_t error_code) const;
template <typename... ArgTs>
void send_debug_message(std::format_string<ArgTs...> fmt, ArgTs&&... args) const {
auto l = this->lobby.lock();
if (l && (this->options.behavior_flags & Episode3::BehaviorFlag::ENABLE_STATUS_MESSAGES)) {
send_text_message(l, std::format(std::forward<std::format_string<ArgTs...>>(fmt), std::forward<ArgTs>(args)...));
}
}
void send_debug_command_received_message(uint8_t client_id, uint8_t subsubcommand, const char* description) const;
void send_debug_command_received_message(uint8_t subsubcommand, const char* description) const;
void send_debug_message_if_error_code_nonzero(uint8_t client_id, int32_t error_code) const;
void send_6xB4x46() const;
@@ -218,8 +215,8 @@ public:
void update_battle_state_flags_and_send_6xB4x03_if_needed(bool always_send = false);
bool update_registration_phase();
void on_server_data_input(std::shared_ptr<Client> sender_c, const std::string& data);
void handle_CAx0B_mulligan_hand(std::shared_ptr<Client> sender_c, const std::string& data);
void handle_CAx0C_end_mulligan_phase(std::shared_ptr<Client> sender_c, const std::string& data);
void handle_CAx0B_redraw_initial_hand(std::shared_ptr<Client> sender_c, const std::string& data);
void handle_CAx0C_end_redraw_initial_hand_phase(std::shared_ptr<Client> sender_c, const std::string& data);
void handle_CAx0D_end_non_action_phase(std::shared_ptr<Client> sender_c, const std::string& data);
void handle_CAx0E_discard_card_from_hand(std::shared_ptr<Client> sender_c, const std::string& data);
void handle_CAx0F_set_card_from_hand(std::shared_ptr<Client> sender_c, const std::string& data);
@@ -237,7 +234,8 @@ public:
void handle_CAx28_end_defense_list(std::shared_ptr<Client> sender_c, const std::string& data);
void handle_CAx2B_legacy_set_card(std::shared_ptr<Client> sender_c, const std::string&);
void handle_CAx34_subtract_ally_atk_points(std::shared_ptr<Client> sender_c, const std::string& data);
void handle_CAx37_client_ready_to_advance_from_starter_roll_phase(std::shared_ptr<Client> sender_c, const std::string& data);
void handle_CAx37_client_ready_to_advance_from_starter_roll_phase(
std::shared_ptr<Client> sender_c, const std::string& data);
void handle_CAx3A_time_limit_expired(std::shared_ptr<Client> sender_c, const std::string& data);
void handle_CAx40_map_list_request(std::shared_ptr<Client> sender_c, const std::string& data);
void handle_CAx41_map_request(std::shared_ptr<Client> sender_c, const std::string& data);
@@ -262,12 +260,12 @@ public:
G_UpdateDecks_Ep3_6xB4x07 prepare_6xB4x07_decks_update() const;
G_SetPlayerNames_Ep3_6xB4x1C prepare_6xB4x1C_names_update() const;
static std::string prepare_6xB6x41_map_definition(std::shared_ptr<const MapIndex::Map> map, uint8_t language, bool is_nte);
static std::string prepare_6xB6x41_map_definition(
std::shared_ptr<const MapIndex::Map> map, Language language, bool is_nte);
void send_6xB6x41_to_all_clients() const;
G_SetTrapTileLocations_Ep3_6xB4x50 prepare_6xB4x50_trap_tile_locations() const;
std::vector<std::shared_ptr<Card>> const_cast_set_cards_v(
const std::vector<std::shared_ptr<const Card>>& cards);
std::vector<std::shared_ptr<Card>> const_cast_set_cards_v(const std::vector<std::shared_ptr<const Card>>& cards);
private:
typedef void (Server::*handler_t)(std::shared_ptr<Client>, const std::string&);
@@ -322,15 +320,14 @@ public:
parray<uint8_t, 4> player_ready_to_end_phase;
uint32_t unknown_a10;
uint32_t overall_time_expired;
// Note: In the original implementation, this is a uint32_t and is measured in
// seconds. In our environment, the simplest implementation uses now(), which
// returns microseconds, so we use a uint64_t instead.
// Note: In the original implementation, this is a uint32_t and is measured in seconds. In our environment, the
// simplest implementation uses now(), which returns microseconds, so we use a uint64_t instead.
uint64_t battle_start_usecs;
uint32_t should_copy_prev_states_to_current_states;
std::shared_ptr<CardSpecial> card_special;
std::shared_ptr<StateFlags> state_flags;
std::array<std::shared_ptr<PlayerState>, 4> player_states;
parray<uint32_t, 4> clients_done_in_mulligan_phase;
parray<uint32_t, 4> clients_done_in_redraw_initial_hand_phase;
uint32_t num_pending_attacks_with_cards;
bcarray<std::shared_ptr<Card>, 0x20> attack_cards;
bcarray<ActionState, 0x20> pending_attacks_with_cards;
+66 -95
View File
@@ -3,7 +3,9 @@
#include <phosg/Random.hh>
#include "../CommandFormats.hh"
#include "../GameServer.hh"
#include "../SendCommands.hh"
#include "../ServerState.hh"
using namespace std;
@@ -16,7 +18,7 @@ Tournament::PlayerEntry::PlayerEntry(uint32_t account_id, const string& player_n
Tournament::PlayerEntry::PlayerEntry(shared_ptr<Client> c)
: account_id(c->login->account->account_id),
client(c),
player_name(c->character()->disp.name.decode(c->language())) {}
player_name(c->character_file()->disp.name.decode(c->language())) {}
Tournament::PlayerEntry::PlayerEntry(
shared_ptr<const COMDeckDefinition> com_deck)
@@ -49,26 +51,23 @@ string Tournament::Team::str() const {
num_com_players += player.is_com();
}
string ret = phosg::string_printf("[Team/%zu %s %zuH/%zuC/%zuP name=%s pass=%s rounds=%zu",
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.c_str(),
this->password.c_str(), this->num_rounds_cleared);
num_human_players, num_com_players, this->max_players, this->name,
this->password, this->num_rounds_cleared);
for (const auto& player : this->players) {
if (player.is_human()) {
if (player.player_name.empty()) {
ret += phosg::string_printf(" %08" PRIX32, player.account_id);
ret += std::format(" {:08X}", player.account_id);
} else {
ret += phosg::string_printf(" %08" PRIX32 " (%s)", player.account_id, player.player_name.c_str());
ret += std::format(" {:08X} ({})", player.account_id, player.player_name);
}
}
}
return ret + "]";
}
void Tournament::Team::register_player(
shared_ptr<Client> c,
const string& team_name,
const string& password) {
void Tournament::Team::register_player(shared_ptr<Client> c, const string& team_name, const string& password) {
if (this->players.size() >= this->max_players) {
throw runtime_error("team is full");
}
@@ -102,8 +101,7 @@ void Tournament::Team::register_player(
bool Tournament::Team::unregister_player(uint32_t account_id) {
size_t index;
for (index = 0; index < this->players.size(); index++) {
if (this->players[index].is_human() &&
(this->players[index].account_id == account_id)) {
if (this->players[index].is_human() && (this->players[index].account_id == account_id)) {
break;
}
}
@@ -121,12 +119,10 @@ bool Tournament::Team::unregister_player(uint32_t account_id) {
return false;
}
// If the tournament has already started, make the team forfeit their game.
// If any player withdraws from a team after the registration phase, the
// entire team essentially forfeits their entry.
// If the tournament has already started, make the team forfeit their game. If any player withdraws from a team
// after the registration phase, the entire team essentially forfeits their entry.
if (tournament->get_state() != Tournament::State::REGISTRATION) {
// Look through the pending matches to see if this team is involved in any
// of them
// 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");
@@ -140,9 +136,8 @@ bool Tournament::Team::unregister_player(uint32_t account_id) {
}
}
// If the tournament has not started yet, just remove the player from the
// team
} 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");
}
@@ -181,9 +176,7 @@ 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)
shared_ptr<Tournament> tournament, shared_ptr<Match> preceding_a, shared_ptr<Match> preceding_b)
: tournament(tournament),
preceding_a(preceding_a),
preceding_b(preceding_b),
@@ -195,9 +188,7 @@ Tournament::Match::Match(
this->round_num = this->preceding_a->round_num + 1;
}
Tournament::Match::Match(
shared_ptr<Tournament> tournament,
shared_ptr<Team> winner_team)
Tournament::Match::Match(shared_ptr<Tournament> tournament, shared_ptr<Team> winner_team)
: tournament(tournament),
preceding_a(nullptr),
preceding_b(nullptr),
@@ -206,7 +197,7 @@ Tournament::Match::Match(
string Tournament::Match::str() const {
string winner_str = this->winner_team ? this->winner_team->str() : "(none)";
return phosg::string_printf("[Match round=%zu winner=%s]", this->round_num, winner_str.c_str());
return std::format("[Match round={} winner={}]", this->round_num, winner_str);
}
bool Tournament::Match::resolve_if_skippable() {
@@ -226,9 +217,8 @@ bool Tournament::Match::resolve_if_skippable() {
this->set_winner_team(winner_a->players.empty() ? winner_b : winner_a);
return true;
}
// If neither preceding winner team has any humans on it, skip this match
// entirely and just make one team advance arbitrarily (note that this also
// handles the case where both preceding winner teams are empty)
// If neither preceding winner team has any humans on it, skip this match entirely and just make one team advance
// arbitrarily (note that this also handles the case where both preceding winner teams are empty)
if (!winner_a->has_any_human_players() && !winner_b->has_any_human_players()) {
this->set_winner_team((phosg::random_object<uint8_t>() & 1) ? winner_b : winner_a);
return true;
@@ -245,8 +235,8 @@ void Tournament::Match::on_winner_team_set() {
tournament->pending_matches.erase(this->shared_from_this());
// Resolve the following match if possible (this skips CPU-only matches). If
// the following match can't be resolved, mark it pending.
// Resolve the following match if possible (this skips CPU-only matches). If the following match can't be resolved,
// mark it pending.
auto following = this->following.lock();
if (following && !following->resolve_if_skippable()) {
tournament->pending_matches.emplace(following);
@@ -257,8 +247,8 @@ void Tournament::Match::on_winner_team_set() {
tournament->current_state = Tournament::State::COMPLETE;
}
// Unlink the losing team's players (if any) - this allows them to enter
// another tournament before this tournament has ended
// Unlink the losing team's players (if any) - this allows them to enter another tournament before this tournament
// has ended
if (this->preceding_a && this->preceding_b) {
auto losing_team = (this->winner_team == this->preceding_a->winner_team)
? this->preceding_b->winner_team
@@ -276,8 +266,7 @@ void Tournament::Match::set_winner_team_without_triggers(shared_ptr<Team> team)
if (!this->preceding_a || !this->preceding_b) {
throw logic_error("set_winner_team called on zero-round match");
}
if ((team != this->preceding_a->winner_team) &&
(team != this->preceding_b->winner_team)) {
if ((team != this->preceding_a->winner_team) && (team != this->preceding_b->winner_team)) {
throw logic_error("winner team did not participate in match");
}
@@ -296,8 +285,7 @@ void Tournament::Match::set_winner_team(shared_ptr<Team> team) {
this->on_winner_team_set();
}
shared_ptr<Tournament::Team> Tournament::Match::opponent_team_for_team(
shared_ptr<Team> team) const {
shared_ptr<Tournament::Team> Tournament::Match::opponent_team_for_team(shared_ptr<Team> team) const {
if (!this->preceding_a || !this->preceding_b) {
throw logic_error("zero-round matches do not have opponents");
}
@@ -318,7 +306,7 @@ Tournament::Tournament(
const Rules& rules,
size_t num_teams,
uint8_t flags)
: log(phosg::string_printf("[Tournament:%s] ", name.c_str())),
: log(std::format("[Tournament:{}] ", name)),
map_index(map_index),
com_deck_index(com_deck_index),
name(name),
@@ -340,10 +328,8 @@ Tournament::Tournament(
}
Tournament::Tournament(
shared_ptr<const MapIndex> map_index,
shared_ptr<const COMDeckIndex> com_deck_index,
const phosg::JSON& json)
: log(phosg::string_printf("[Tournament:%s] ", json.get_string("name").c_str())),
shared_ptr<const MapIndex> map_index, 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),
source_json(json),
@@ -355,7 +341,7 @@ void Tournament::init() {
bool is_registration_complete;
if (!this->source_json.is_null()) {
this->name = this->source_json.get_string("name");
this->map = this->map_index->for_number(this->source_json.get_int("map_number"));
this->map = this->map_index->map_for_id(this->source_json.get_int("map_number"));
this->rules = Rules(this->source_json.at("rules"));
this->flags = this->source_json.get_int("flags", 0x02);
if (this->source_json.get_bool("is_2v2", false)) {
@@ -392,8 +378,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 = 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;
@@ -442,9 +427,7 @@ void Tournament::init() {
// If both preceding matches of the following match are resolved, put
// the following match on the queue since it may be resolvable as well
auto following = match->following.lock();
if (following &&
following->preceding_a->winner_team &&
following->preceding_b->winner_team) {
if (following && following->preceding_a->winner_team && following->preceding_b->winner_team) {
match_queue.emplace(following);
}
}
@@ -475,8 +458,7 @@ void Tournament::create_bracket_matches() {
throw 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
// 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);
@@ -491,10 +473,7 @@ void Tournament::create_bracket_matches() {
while (current_round_matches.size() > 1) {
vector<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 = 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));
@@ -550,8 +529,7 @@ shared_ptr<Tournament::Team> Tournament::get_winner_team() const {
return this->final_match->winner_team;
}
shared_ptr<Tournament::Match> Tournament::next_match_for_team(
shared_ptr<Team> team) const {
shared_ptr<Tournament::Match> Tournament::next_match_for_team(shared_ptr<Team> team) const {
if (this->current_state == Tournament::State::REGISTRATION) {
return nullptr;
}
@@ -559,8 +537,7 @@ shared_ptr<Tournament::Match> Tournament::next_match_for_team(
if (!match->preceding_a || !match->preceding_b) {
throw logic_error("zero-round match is pending after tournament registration phase");
}
if ((team == match->preceding_a->winner_team) ||
(team == match->preceding_b->winner_team)) {
if ((team == match->preceding_a->winner_team) || (team == match->preceding_b->winner_team)) {
return match;
}
}
@@ -571,8 +548,7 @@ 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 {
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;
}
@@ -599,9 +575,8 @@ void Tournament::start() {
bool has_com_teams = (this->flags & Flag::HAS_COM_TEAMS);
// If there aren't enough entrants (1 if has_com_teams is false, else 2),
// don't allow the tournament to start (because it would enter the COMPLETE
// state immediately)
// If there aren't enough entrants (1 if has_com_teams is false, else 2), don't allow the tournament to start
// (because it would enter the COMPLETE state immediately)
size_t num_human_teams = 0;
for (size_t z = 0; z < this->teams.size(); z++) {
if (this->teams[z]->has_any_human_players()) {
@@ -613,9 +588,8 @@ void Tournament::start() {
}
if ((this->flags & Flag::SHUFFLE_ENTRIES) && (this->flags & Flag::RESIZE_ON_START)) {
// If both of these flags are set, pack the human teams into the lowest part
// of the teams list so we can resize the tournament to the smallest
// possible size. This is OK since we're going to shuffle them later anyway
// If both of these flags are set, pack the human teams into the lowest part of the teams list so we can resize the
// tournament to the smallest possible size. This is OK since we're going to shuffle them later anyway
size_t r_offset = 0, w_offset = 0;
for (; r_offset < this->teams.size(); r_offset++) {
if (this->teams[r_offset]->has_any_human_players()) {
@@ -628,8 +602,8 @@ void Tournament::start() {
}
if (this->flags & Flag::RESIZE_ON_START) {
// Resize the tournament by repeatedly deleting the second half of it, until
// the second half contains human players or the tournament size is 4
// Resize the tournament by repeatedly deleting the second half of it, until the second half contains human players
// or the tournament size is 4
while (this->teams.size() > 4) {
size_t z;
for (z = this->teams.size() >> 1; z < this->teams.size(); z++) {
@@ -659,13 +633,12 @@ void Tournament::start() {
this->current_state = State::IN_PROGRESS;
this->create_bracket_matches();
// Assign names to COM teams, and assign COM decks to all empty slots unless
// has_com_teams is false
// Assign names to COM teams, and assign COM decks to all empty slots unless has_com_teams is false
for (size_t z = 0; z < this->zero_round_matches.size(); z++) {
auto m = this->zero_round_matches[z];
auto t = m->winner_team;
if (t->name.empty()) {
t->name = has_com_teams ? phosg::string_printf("COM:%zu", z) : "(no entrant)";
t->name = has_com_teams ? std::format("COM:{}", z) : "(no entrant)";
}
for (const auto& player : t->players) {
if (player.is_com()) {
@@ -675,11 +648,9 @@ void 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");
}
// 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 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()) {
// TODO: Don't allow duplicate COM decks, nor duplicate COM SCs on the
// same team
// TODO: Don't allow duplicate COM decks, nor duplicate COM SCs on the same team
while (t->players.size() < t->max_players) {
t->players.emplace_back(this->com_deck_index->random_deck());
}
@@ -696,9 +667,8 @@ void Tournament::send_all_state_updates() const {
for (const auto& team : this->teams) {
for (const auto& player : team->players) {
auto c = player.client.lock();
// Note: The last check here is to make sure the client is still linked
// with this instance of the tournament - an intervening shell command
// `reload ep3` could have changed the client's linkage
// Note: The last check here is to make sure the client is still linked with this instance of the tournament - an
// intervening shell command `reload ep3` could have changed the client's linkage
if (c && (c->version() == Version::GC_EP3) && (c->ep3_tournament_team.lock() == team)) {
send_ep3_confirm_tournament_entry(c, this->shared_from_this());
}
@@ -718,7 +688,7 @@ void Tournament::send_all_state_updates_on_deletion() const {
}
string Tournament::bracket_str() const {
string ret = phosg::string_printf("Tournament \"%s\"\n", this->name.c_str());
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 {
ret.append(2 * indent_level, ' ');
@@ -735,19 +705,19 @@ string Tournament::bracket_str() const {
}
};
auto en_vm = this->map->version(1);
auto en_vm = this->map->version(Language::ENGLISH);
if (en_vm) {
string map_name = en_vm->map->name.decode(en_vm->language);
ret += phosg::string_printf(" Map: %08" PRIX32 " (%s)\n", this->map->map_number, map_name.c_str());
ret += std::format(" Map: {:08X} ({})\n", this->map->map_number, map_name);
} else {
ret += phosg::string_printf(" Map: %08" PRIX32 "\n", this->map->map_number);
ret += std::format(" Map: {:08X}\n", this->map->map_number);
}
string rules_str = this->rules.str();
ret += phosg::string_printf(" Rules: %s\n", rules_str.c_str());
ret += phosg::string_printf(" Structure: %s, %zu entries\n", (this->flags & Flag::IS_2V2) ? "2v2" : "1v1", this->num_teams);
ret += phosg::string_printf(" COM teams: %s\n", (this->flags & Flag::HAS_COM_TEAMS) ? "allowed" : "forbidden");
ret += phosg::string_printf(" Shuffle entries: %s\n", (this->flags & Flag::SHUFFLE_ENTRIES) ? "yes" : "no");
ret += phosg::string_printf(" Resize on start: %s\n", (this->flags & Flag::RESIZE_ON_START) ? "yes" : "no");
ret += std::format(" Rules: {}\n", 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");
ret += std::format(" Resize on start: {}\n", (this->flags & Flag::RESIZE_ON_START) ? "yes" : "no");
switch (this->current_state) {
case State::REGISTRATION:
ret += " State: REGISTRATION\n";
@@ -770,13 +740,13 @@ string Tournament::bracket_str() const {
ret += " Teams:\n";
for (const auto& team : this->teams) {
string team_str = team->str();
ret += phosg::string_printf(" %s\n", team_str.c_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 += phosg::string_printf(" %s\n", match_str.c_str());
ret += std::format(" {}\n", match_str);
}
}
@@ -826,8 +796,7 @@ TournamentIndex::TournamentIndex(
auto tourn = 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 are
// supposed to already have unique keys
// 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());
}
tourn->set_menu_item_id(this->menu_item_id_to_tournament.size());
@@ -860,8 +829,7 @@ shared_ptr<Tournament> TournamentIndex::create_tournament(
throw 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 = 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");
@@ -940,8 +908,11 @@ void TournamentIndex::link_client(shared_ptr<Client> c) {
}
void TournamentIndex::link_all_clients(std::shared_ptr<ServerState> s) {
for (const auto& c_it : s->channel_to_client) {
this->link_client(c_it.second);
// This can be called before the game server exists, so do nothing in that case
if (s->game_server) {
for (const auto& c : s->game_server->all_clients()) {
this->link_client(c);
}
}
}
+8 -19
View File
@@ -1,6 +1,5 @@
#pragma once
#include <event2/event.h>
#include <stdint.h>
#include <memory>
@@ -63,16 +62,10 @@ public:
size_t num_rounds_cleared;
bool is_active;
Team(
std::shared_ptr<Tournament> tournament,
size_t index,
size_t max_players);
Team(std::shared_ptr<Tournament> tournament, size_t index, size_t max_players);
std::string str() const;
void register_player(
std::shared_ptr<Client> c,
const std::string& team_name,
const std::string& password);
void register_player(std::shared_ptr<Client> c, const std::string& team_name, const std::string& password);
bool unregister_player(uint32_t account_id);
bool has_any_human_players() const;
@@ -92,9 +85,7 @@ public:
std::shared_ptr<Tournament> tournament,
std::shared_ptr<Match> preceding_a,
std::shared_ptr<Match> preceding_b);
Match(
std::shared_ptr<Tournament> tournament,
std::shared_ptr<Team> winner_team);
Match(std::shared_ptr<Tournament> tournament, std::shared_ptr<Team> winner_team);
std::string str() const;
bool resolve_if_skippable();
@@ -181,14 +172,12 @@ private:
std::set<uint32_t> all_player_account_ids;
std::unordered_set<std::shared_ptr<Match>> pending_matches;
// This vector contains all teams in the original starting order of the
// tournament (that is, all teams in the first round). The order within this
// vector determines which team will play against which other team in the
// first round: [0] will play against [1], [2] will play against [3], etc.
// This vector contains all teams in the original starting order of the tournament (that is, all teams in the first
// round). The order within this vector determines which team will play against which other team in the first round:
// [0] will play against [1], [2] will play against [3], etc.
std::vector<std::shared_ptr<Team>> teams;
// The tournament begins with a "zero round", in which each team automatically
// "wins" a match, putting them into the first round. This is just to make the
// data model easier to manage, so we don't have to have a type of match with
// The tournament begins with a "zero round", in which each team automatically "wins" a match, putting them into the
// first round. This is just to make the data model easier to manage, so we don't have to have a type of match with
// no preceding round.
std::vector<std::shared_ptr<Match>> zero_round_matches;
std::shared_ptr<Match> final_match;
-62
View File
@@ -1,62 +0,0 @@
#include "EventUtils.hh"
#include <event2/buffer.h>
#include <event2/event.h>
#include <deque>
#include <functional>
#include <memory>
#include <stdexcept>
using namespace std;
static void dispatch_forward_to_event_thread(evutil_socket_t, short, void* ctx) {
auto* fn = reinterpret_cast<function<void()>*>(ctx);
(*fn)();
delete fn;
}
void forward_to_event_thread(shared_ptr<struct event_base> base, function<void()>&& fn) {
struct timeval tv = {0, 0};
function<void()>* new_fn = new function<void()>(std::move(fn));
event_base_once(base.get(), -1, EV_TIMEOUT, dispatch_forward_to_event_thread, new_fn, &tv);
}
template <>
void call_on_event_thread<void>(shared_ptr<struct event_base> base, function<void()>&& compute) {
bool succeeded = false;
string exc_what;
mutex ret_lock;
condition_variable ret_cv;
unique_lock<mutex> g(ret_lock);
forward_to_event_thread(base, [&]() -> void {
lock_guard<mutex> g(ret_lock);
try {
compute();
succeeded = true;
} catch (const exception& e) {
exc_what = e.what();
}
ret_cv.notify_one();
});
ret_cv.wait(g);
if (!succeeded) {
throw runtime_error(exc_what);
}
}
string evbuffer_remove_str(struct evbuffer* buf, ssize_t size) {
if (!buf) {
return "";
}
if (size < 0) {
size = static_cast<size_t>(evbuffer_get_length(buf));
}
string ret(size, '\0');
ssize_t bytes_removed = evbuffer_remove(buf, ret.data(), ret.size());
if (bytes_removed < 0) {
throw std::runtime_error("can\'t remove data from buffer");
}
ret.resize(bytes_removed);
return ret;
}
-45
View File
@@ -1,45 +0,0 @@
#pragma once
#include <event2/event.h>
#include <condition_variable>
#include <functional>
#include <memory>
#include <mutex>
#include <optional>
#include <stdexcept>
#include <string>
// Calls a function on the given base's event thread. This function returns
// when the call has been enqueued, not necessarily after it returns.
void forward_to_event_thread(std::shared_ptr<struct event_base> base, std::function<void()>&& fn);
// Calls a function on the given base's event thread and waits for it to
// return. Returns the value returned on that thread.
template <typename T>
T call_on_event_thread(std::shared_ptr<struct event_base> base, std::function<T()>&& compute) {
std::optional<T> ret;
std::string exc_what;
std::mutex ret_lock;
std::condition_variable ret_cv;
std::unique_lock<std::mutex> g(ret_lock);
forward_to_event_thread(base, [&]() -> void {
std::lock_guard<std::mutex> g(ret_lock);
try {
ret = compute();
} catch (const std::exception& e) {
exc_what = e.what();
}
ret_cv.notify_one();
});
ret_cv.wait(g);
if (!ret.has_value()) {
throw std::runtime_error(exc_what);
}
return ret.value();
}
template <>
void call_on_event_thread<void>(std::shared_ptr<struct event_base> base, std::function<void()>&& compute);
std::string evbuffer_remove_str(struct evbuffer* buf, ssize_t size = -1);
+2 -4
View File
@@ -44,16 +44,14 @@ 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) {
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) {
shared_ptr<const FileContentsCache::File> FileContentsCache::get_or_throw(const char* name) {
return this->get_or_throw(string(name));
}
+3 -3
View File
@@ -114,9 +114,9 @@ public:
ThreadSafeFileCache& operator=(ThreadSafeFileCache&&) = delete;
~ThreadSafeFileCache() = default;
// Warning: 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);
// 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;
+254 -175
View File
@@ -3,16 +3,15 @@
#include <stdio.h>
#include <string.h>
#include <filesystem>
#include <phosg/Filesystem.hh>
#include <phosg/Hash.hh>
#include <phosg/Time.hh>
#include <stdexcept>
#ifdef HAVE_RESOURCE_FILE
#include <resource_file/Emulators/PPC32Emulator.hh>
#include <resource_file/Emulators/SH4Emulator.hh>
#include <resource_file/Emulators/X86Emulator.hh>
#endif
#include "CommandFormats.hh"
#include "CommonFileFormats.hh"
@@ -21,20 +20,6 @@
using namespace std;
static bool is_function_compiler_available = true;
bool function_compiler_available() {
#ifndef HAVE_RESOURCE_FILE
return false;
#else
return is_function_compiler_available;
#endif
}
void set_function_compiler_available(bool is_available) {
is_function_compiler_available = is_available;
}
const char* name_for_architecture(CompiledFunctionCode::Architecture arch) {
switch (arch) {
case CompiledFunctionCode::Architecture::POWERPC:
@@ -111,41 +96,137 @@ string CompiledFunctionCode::generate_client_command(
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);
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);
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;
return (this->arch == Architecture::POWERPC);
}
shared_ptr<CompiledFunctionCode> compile_function_code(
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) {
#ifndef HAVE_RESOURCE_FILE
(void)arch;
(void)function_directory;
(void)system_directory;
(void)name;
(void)text;
throw runtime_error("function compiler is not available");
#else
auto ret = make_shared<CompiledFunctionCode>();
ret->arch = arch;
ret->short_name = name;
ret->index = 0;
ret->hide_from_patches_menu = false;
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;
@@ -164,11 +245,11 @@ shared_ptr<CompiledFunctionCode> compile_function_code(
}
// Look in the function directory first, then the system directory
string asm_filename = phosg::string_printf("%s/%s.%s.inc.s", function_directory.c_str(), name.c_str(), arch_name_token);
if (!phosg::isfile(asm_filename)) {
asm_filename = phosg::string_printf("%s/%s.%s.inc.s", system_directory.c_str(), name.c_str(), arch_name_token);
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 (phosg::isfile(asm_filename)) {
if (std::filesystem::is_regular_file(asm_filename)) {
if (!get_include_stack.emplace(name).second) {
throw runtime_error("mutual recursion between includes: " + name);
}
@@ -191,100 +272,109 @@ shared_ptr<CompiledFunctionCode> compile_function_code(
}
string bin_filename = function_directory + "/" + name + ".inc.bin";
if (phosg::isfile(bin_filename)) {
if (std::filesystem::is_regular_file(bin_filename)) {
return phosg::load_file(bin_filename);
}
bin_filename = system_directory + "/" + name + ".inc.bin";
if (phosg::isfile(bin_filename)) {
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 + ")");
};
ResourceDASM::EmulatorBase::AssembleResult assembled;
if (arch == CompiledFunctionCode::Architecture::POWERPC) {
assembled = ResourceDASM::PPC32Emulator::assemble(text, get_include);
} else if (arch == CompiledFunctionCode::Architecture::X86) {
assembled = ResourceDASM::X86Emulator::assemble(text, get_include);
} else if (arch == CompiledFunctionCode::Architecture::SH4) {
assembled = ResourceDASM::SH4Emulator::assemble(text, get_include);
} else {
throw runtime_error("invalid architecture");
}
ret->code = std::move(assembled.code);
ret->label_offsets = std::move(assembled.label_offsets);
for (const auto& it : assembled.metadata_keys) {
if (it.first == "hide_from_patches_menu") {
ret->hide_from_patches_menu = true;
} else if (it.first == "index") {
if (it.second.size() != 1) {
throw runtime_error("invalid index value in .meta directive");
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");
}
ret->index = it.second[0];
} else if (it.first == "name") {
ret->long_name = it.second;
} else if (it.first == "description") {
ret->description = it.second;
} else {
throw runtime_error("unknown metadata key: " + it.first);
}
}
set<uint32_t> reloc_indexes;
for (const auto& it : ret->label_offsets) {
if (phosg::starts_with(it.first, "reloc")) {
reloc_indexes.emplace(it.second / 4);
}
}
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);
}
}
try {
ret->entrypoint_offset_offset = ret->label_offsets.at("entry_ptr");
} catch (const out_of_range&) {
throw runtime_error("code does not contain entry_ptr label");
}
set<uint32_t> reloc_indexes;
for (const auto& it : compiled->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 runtime_error("relocation delta too far away");
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());
}
ret->relocation_deltas.emplace_back(delta);
prev_index = it;
}
return ret;
#endif
}
FunctionCodeIndex::FunctionCodeIndex(const string& directory) {
if (!function_compiler_available()) {
function_compiler_log.info("Function compiler is not available");
return;
}
string system_dir_path = phosg::ends_with(directory, "/") ? (directory + "System") : (directory + "/System");
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& subdir_name : phosg::list_directory_sorted(directory)) {
string subdir_path = phosg::ends_with(directory, "/") ? (directory + subdir_name) : (directory + "/" + subdir_name);
if (!phosg::isdir(subdir_path)) {
function_compiler_log.warning("Skipping %s (not a directory)", subdir_name.c_str());
continue;
}
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);
for (const auto& filename : phosg::list_directory_sorted(subdir_path)) {
auto add_file = [&](string filename) -> void {
try {
if (!phosg::ends_with(filename, ".s")) {
continue;
if (!filename.ends_with(".s")) {
return;
}
string name = filename.substr(0, filename.size() - 2);
if (phosg::ends_with(name, ".inc")) {
continue;
if (name.ends_with(".inc")) {
return;
}
bool is_patch = phosg::ends_with(name, ".patch");
bool is_patch = name.ends_with(".patch");
if (is_patch) {
name.resize(name.size() - 6);
}
@@ -293,15 +383,15 @@ FunctionCodeIndex::FunctionCodeIndex(const string& directory) {
CompiledFunctionCode::Architecture arch = CompiledFunctionCode::Architecture::UNKNOWN;
uint32_t specific_version = 0;
string short_name = name;
if (phosg::ends_with(name, ".ppc")) {
if (name.ends_with(".ppc")) {
arch = CompiledFunctionCode::Architecture::POWERPC;
name.resize(name.size() - 4);
short_name = name;
} else if (phosg::ends_with(name, ".x86")) {
} else if (name.ends_with(".x86")) {
arch = CompiledFunctionCode::Architecture::X86;
name.resize(name.size() - 4);
short_name = name;
} else if (phosg::ends_with(name, ".sh4")) {
} else if (name.ends_with(".sh4")) {
arch = CompiledFunctionCode::Architecture::SH4;
name.resize(name.size() - 4);
short_name = name;
@@ -327,69 +417,63 @@ FunctionCodeIndex::FunctionCodeIndex(const string& directory) {
string path = subdir_path + "/" + filename;
string text = phosg::load_file(path);
auto code = compile_function_code(arch, subdir_path, system_dir_path, name, text);
if (code->index != 0) {
if (!this->index_to_function.emplace(code->index, code).second) {
throw runtime_error(phosg::string_printf(
"duplicate function index: %08" PRIX32, code->index));
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);
}
}
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 | specific_version, code);
this->name_and_specific_version_to_patch_function.emplace(
phosg::string_printf("%s-%08" PRIX32, short_name.c_str(), specific_version), code);
}
string index_prefix = code->index ? phosg::string_printf("%02X => ", code->index) : "";
string patch_prefix = is_patch ? phosg::string_printf("[%08" PRIX32 "/%08" PRIX32 "] ", code->menu_item_id, code->specific_version) : "";
function_compiler_log.info("Compiled function %s%s%s (%s)",
index_prefix.c_str(), patch_prefix.c_str(), name.c_str(), name_for_architecture(code->arch));
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) {
function_compiler_log.warning("Failed to compile function %s: %s", filename.c_str(), e.what());
if (raise_on_any_failure) {
throw runtime_error(format("({}) {}", filename, e.what()));
}
function_compiler_log.warning_f("Failed to compile function {}: {}", filename, e.what());
}
}
}
}
};
shared_ptr<const Menu> FunctionCodeIndex::patch_menu(uint32_t specific_version) const {
auto suffix = phosg::string_printf("-%08" PRIX32, specific_version);
auto ret = make_shared<Menu>(MenuID::PATCHES, "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 || !phosg::ends_with(it.first, suffix)) {
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;
}
ret->items.emplace_back(
fn->menu_item_id,
fn->long_name.empty() ? fn->short_name : fn->long_name,
fn->description,
MenuItem::Flag::REQUIRES_SEND_FUNCTION_CALL_RUNS_CODE);
}
return ret;
}
shared_ptr<const Menu> FunctionCodeIndex::patch_switches_menu(
uint32_t specific_version, const std::unordered_set<std::string>& auto_patches_enabled) const {
auto suffix = phosg::string_printf("-%08" PRIX32, specific_version);
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, "Patch switches");
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 || !phosg::ends_with(it.first, suffix)) {
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(auto_patches_enabled.count(fn->short_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);
}
@@ -408,17 +492,12 @@ bool FunctionCodeIndex::patch_menu_empty(uint32_t specific_version) const {
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(
phosg::string_printf("%s-%08" PRIX32, name.c_str(), specific_version));
return this->name_and_specific_version_to_patch_function.at(std::format("{}-{:08X}", name, specific_version));
}
DOLFileIndex::DOLFileIndex(const string& directory) {
if (!function_compiler_available()) {
function_compiler_log.info("Function compiler is not available");
return;
}
if (!phosg::isdir(directory)) {
function_compiler_log.info("DOL file directory is missing");
if (!std::filesystem::is_directory(directory)) {
function_compiler_log.info_f("DOL file directory is missing");
return;
}
@@ -427,9 +506,10 @@ DOLFileIndex::DOLFileIndex(const string& directory) {
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& filename : phosg::list_directory_sorted(directory)) {
bool is_dol = phosg::ends_with(filename, ".dol");
bool is_compressed_dol = phosg::ends_with(filename, ".dol.prs");
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;
}
@@ -458,10 +538,9 @@ DOLFileIndex::DOLFileIndex(const string& directory) {
string compressed_size_str = phosg::format_size(file_data.size());
string decompressed_size_str = phosg::format_size(decompressed_size);
function_compiler_log.info("Loaded compressed DOL file %s (%s -> %s)",
dol->name.c_str(), compressed_size_str.c_str(), decompressed_size_str.c_str());
description = phosg::string_printf("$C6%s$C7\n%s\n%s (orig)",
dol->name.c_str(), compressed_size_str.c_str(), decompressed_size_str.c_str());
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;
@@ -474,8 +553,8 @@ DOLFileIndex::DOLFileIndex(const string& directory) {
dol->data = std::move(w.str());
string size_str = phosg::format_size(dol->data.size());
function_compiler_log.info("Loaded DOL file %s (%s)", filename.c_str(), size_str.c_str());
description = phosg::string_printf("$C6%s$C7\n%s", dol->name.c_str(), size_str.c_str());
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);
@@ -484,7 +563,7 @@ DOLFileIndex::DOLFileIndex(const string& directory) {
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("Failed to load DOL file %s: %s", filename.c_str(), e.what());
function_compiler_log.warning_f("Failed to load DOL file {}: {}", filename, e.what());
}
}
}
@@ -501,7 +580,7 @@ uint32_t specific_version_for_gc_header_checksum(uint32_t header_checksum) {
char developer_code2 = 'P';
uint8_t disc_number = 0;
uint8_t version_code;
} __packed__ data;
} __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++) {
+11 -17
View File
@@ -11,9 +11,6 @@
#include "Menu.hh"
bool function_compiler_available();
void set_function_compiler_available(bool is_available);
// 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.
@@ -28,15 +25,16 @@ struct CompiledFunctionCode {
std::string code;
std::vector<uint16_t> relocation_deltas;
std::unordered_map<std::string, uint32_t> label_offsets;
uint32_t entrypoint_offset_offset;
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
uint8_t index; // 0 = unused (not registered in index_to_function)
uint32_t menu_item_id;
bool hide_from_patches_menu;
uint32_t specific_version;
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;
@@ -55,15 +53,9 @@ struct CompiledFunctionCode {
const char* name_for_architecture(CompiledFunctionCode::Architecture arch);
std::shared_ptr<CompiledFunctionCode> compile_function_code(
CompiledFunctionCode::Architecture arch,
const std::string& directory,
const std::string& name,
const std::string& text);
struct FunctionCodeIndex {
FunctionCodeIndex() = default;
explicit FunctionCodeIndex(const std::string& directory);
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;
@@ -71,8 +63,10 @@ struct FunctionCodeIndex {
// 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_menu(uint32_t specific_version) const;
std::shared_ptr<const Menu> patch_switches_menu(uint32_t specific_version, const std::unordered_set<std::string>& auto_patches_enabled) const;
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;
+3 -5
View File
@@ -15,7 +15,7 @@ struct GSLHeaderEntryT {
U32T<BE> offset; // In pages, so actual offset is this * 0x800
U32T<BE> size;
uint64_t unused;
} __packed__;
} __attribute__((packed));
using GSLHeaderEntry = GSLHeaderEntryT<false>;
using GSLHeaderEntryBE = GSLHeaderEntryT<true>;
@@ -39,8 +39,7 @@ void GSLArchive::load_t() {
}
}
GSLArchive::GSLArchive(shared_ptr<const string> data, bool big_endian)
: data(data) {
GSLArchive::GSLArchive(shared_ptr<const string> data, bool big_endian) : data(data) {
if (big_endian) {
this->load_t<true>();
} else {
@@ -87,8 +86,7 @@ template <bool BE>
string GSLArchive::generate_t(const unordered_map<string, string>& files) {
phosg::StringWriter w;
// Make sure there's enough space for a blank header entry before any file's
// data pages begin
// Make sure there's enough space for a blank header entry before any file's data pages begin
uint32_t data_start_offset = ((sizeof(GSLHeaderEntryT<BE>) * (files.size() + 1)) + 0x7FF) & (~0x7FF);
uint32_t data_offset = data_start_offset;
for (const auto& file : files) {
+198
View File
@@ -0,0 +1,198 @@
#include "GameServer.hh"
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <algorithm>
#include <iostream>
#include <phosg/Encoding.hh>
#include <phosg/Network.hh>
#include <phosg/Strings.hh>
#include <phosg/Time.hh>
#include "Loggers.hh"
#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) {}
void GameServer::listen(
const std::string& name,
const string& addr,
uint16_t port,
Version version,
ServerBehavior behavior) {
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<GameServerSocket>();
sock->name = name;
sock->endpoint = asio::ip::tcp::endpoint(asio_addr, port);
sock->version = version;
sock->behavior = behavior;
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);
c->listener_port = port;
this->log.info_f("Client connected: C-{:X} via TSI-{}-{}-{}",
c->id, port, phosg::name_for_enum(ch->version), phosg::name_for_enum(initial_state));
asio::co_spawn(*this->io_context, this->handle_connected_client(c), asio::detached);
return c;
}
shared_ptr<Client> GameServer::get_client() const {
if (this->clients.empty()) {
throw runtime_error("no clients on game server");
}
if (this->clients.size() > 1) {
throw runtime_error("multiple clients on game server");
}
return *this->clients.begin();
}
vector<shared_ptr<Client>> GameServer::get_clients_by_identifier(const 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&) {
}
try {
account_id_hex = stoul(ident, nullptr, 16);
} catch (const 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;
for (const auto& c : this->clients) {
if (c->login && c->login->account->account_id == account_id_hex) {
results.emplace_back(c);
continue;
}
if (c->login && c->login->account->account_id == account_id_dec) {
results.emplace_back(c);
continue;
}
if (c->login && c->login->xb_license && c->login->xb_license->gamertag == ident) {
results.emplace_back(c);
continue;
}
if (c->login && c->login->bb_license && c->login->bb_license->username == ident) {
results.emplace_back(c);
continue;
}
auto p = c->character_file(false, false);
if (p && p->disp.name.eq(ident, p->inventory.language)) {
results.emplace_back(c);
continue;
}
if (c->channel->name == ident) {
results.emplace_back(c);
continue;
}
if (c->channel->name.starts_with(ident + " ")) {
results.emplace_back(c);
continue;
}
}
return results;
}
shared_ptr<Client> GameServer::create_client(
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 (client_sock.is_open()) {
client_sock.close();
}
return nullptr;
}
auto channel = SocketChannel::create(
this->io_context,
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);
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) {
try {
co_await on_command(c, std::move(msg));
} catch (const 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) {
auto g = phosg::on_close_scope(std::bind(&Client::cancel_pending_promises, c.get()));
try {
co_await on_connect(c);
} catch (const exception& e) {
this->log.warning_f("Error in client initialization: {}", e.what());
c->channel->disconnect();
}
while (c->channel->connected()) {
auto msg = std::make_unique<Channel::Message>(co_await c->channel->recv());
asio::co_spawn(co_await asio::this_coro::executor, this->handle_client_command(c, std::move(msg)), asio::detached);
}
}
asio::awaitable<void> GameServer::destroy_client(std::shared_ptr<Client> c) {
this->log.info_f("Running cleanup tasks for {}", c->channel->name);
// The client may not actually be disconnected yet if an uncaught exception occurred in a handler task
c->channel->disconnect();
// Close the proxy session, if any
if (c->proxy_session) {
if (c->proxy_session->server_channel) {
c->proxy_session->server_channel->disconnect();
}
c->proxy_session.reset();
}
try {
co_await on_disconnect(c);
} catch (const 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);
for (auto h_it : hooks) {
try {
h_it.second();
} catch (const exception& e) {
c->log.warning_f("Disconnect hook {} failed: {}", h_it.first, e.what());
}
}
}

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