Compare commits

...

274 Commits

Author SHA1 Message Date
Martin Michelsen 5ea3d0ad4b add missing quest files 2024-07-10 20:41:18 -07:00
Martin Michelsen 90efde7aa9 handle rare shell i/o error 2024-07-10 19:48:51 -07:00
Martin Michelsen 55f1869125 add replace sound effects code 2024-07-08 01:08:43 -07:00
Martin Michelsen b4efd90fdc replace q050 and q052 with direct backports 2024-07-07 16:39:52 -07:00
Martin Michelsen 87dd554592 remove offset comments in reassembly mode 2024-07-07 15:19:18 -07:00
Martin Michelsen 58974ae1be use JP version as default sub_version 2024-07-06 12:57:30 -07:00
Martin Michelsen 21c8bab91c handle one 6x63 data race 2024-07-06 09:54:07 -07:00
Martin Michelsen c58b37be23 add GC-GJAM quest opcodes in handler-tables 2024-07-05 21:55:16 -07:00
Martin Michelsen d3d98c44b8 clear ep3 media when switching to proxy server 2024-07-04 16:25:21 -07:00
Martin Michelsen dc2e73d198 fix item notifications for mags on GC 2024-07-04 16:24:36 -07:00
Martin Michelsen 774f9649da fix binary operator bind order 2024-07-04 16:24:18 -07:00
Martin Michelsen 093287af75 fix condition icons in enemy HP bars patch 2024-06-29 22:25:32 -07:00
Martin Michelsen 0126189cbd fix 3OJ2 and 3OJ3 item loss patches 2024-06-29 12:22:27 -07:00
Martin Michelsen c250a2dbc4 update enemy HP bars patch 2024-06-28 20:57:16 -07:00
Martin Michelsen 2ff9df19c8 don't allow language fallback for q88500 2024-06-28 14:10:06 -07:00
Martin Michelsen 528593651b update TODO.md 2024-06-28 10:13:24 -07:00
Martin Michelsen 9f073d07cd don't use member initialization 2024-06-28 10:10:39 -07:00
Martin Michelsen 4bd6ef12a9 implement $savechar on Episode 3 2024-06-28 09:48:09 -07:00
Martin Michelsen 52644695a3 fix grind limit overdraft 2024-06-26 20:00:22 -07:00
Martin Michelsen 45e619718c fix patch menu on BB 2024-06-26 19:37:21 -07:00
nolrinale 43fd979763 Fixed Coren rewards item names causing parsing issues 2024-06-26 09:36:21 -07:00
Martin Michelsen 082bc49a4d add customization segregation test 2024-06-24 00:01:09 -07:00
Martin Michelsen 4adcaa7bee skip max grind check on v3 2024-06-24 00:00:48 -07:00
Martin Michelsen 630ae0beb4 fix stack limits on DC NTE 2024-06-23 22:38:46 -07:00
Martin Michelsen 246dfd9fe0 update notes on DC NTE quest commands 2024-06-23 22:38:46 -07:00
Martin Michelsen 6f056cb1bd update proxy options 2024-06-23 22:38:46 -07:00
Martin Michelsen 9322c023da fix missing sub_version check 2024-06-23 22:38:46 -07:00
Martin Michelsen fd4719f8ec clean up comments in q88500 2024-06-23 08:38:51 -07:00
Martin Michelsen 3a22a5c489 add Ep3 codepaths to B2 enabler 2024-06-23 00:24:01 -07:00
Martin Michelsen 862b3d27da add B2 patch support on PSO Plus 2024-06-22 21:42:30 -07:00
Martin Michelsen 998664d2fb add loading screen AR code 2024-06-22 17:22:02 -07:00
Martin Michelsen 0bf2d950ac fix offsets on DCv1 item loss patches 2024-06-22 15:23:38 -07:00
Martin Michelsen 3ae5e875a1 fix comments on some quest opcodes 2024-06-22 15:23:23 -07:00
Martin Michelsen a88795d8b9 fix edge case in quest episode detection 2024-06-22 15:22:52 -07:00
Martin Michelsen 9ca1b79409 add .include directives in quest assembler 2024-06-22 15:22:32 -07:00
Martin Michelsen ce8277b96a describe 6x51 command 2024-06-22 15:20:48 -07:00
Martin Michelsen 25731eb71f add comments about UDP subcommands 2024-06-22 15:20:39 -07:00
Martin Michelsen e55963b82b specify types on some quest handlers 2024-06-22 14:22:29 -07:00
Martin Michelsen b9d9b38351 add US v1.2 quest opcodes to handler-tables 2024-06-22 09:29:40 -07:00
Martin Michelsen 782babf3ae add quest opcode names in handler-tables 2024-06-22 09:07:37 -07:00
Martin Michelsen 9869fa03c2 only send notifs for client-generated items if the game drop mode is CLIENT 2024-06-21 11:02:59 -07:00
Martin Michelsen 0ae02b0191 add websocket endpoint for rare drop stream 2024-06-21 10:59:01 -07:00
Martin Michelsen c0ea976fdc fix typo in VersionDetectGC 2024-06-21 10:59:01 -07:00
nolrinale c4bf9e7d5b Adjusted Coren rewards to match Retail server ones 2024-06-20 20:24:05 -07:00
Martin Michelsen 2e5d95d612 fix data race in 6xCA command 2024-06-19 23:31:51 -07:00
nolrinale 75b2827da9 Corrected BB english client files 2024-06-18 19:37:37 -07:00
Martin Michelsen 5b72e59ebe fix mag flag reset during combo item apply 2024-06-18 09:56:29 -07:00
Matt Swift d2c16b5363 Add Ultimate Map Fix for DCv2 2024-06-18 09:53:08 -07:00
Martin Michelsen 977ed05526 don't let item parsing from config.json prevent server startup 2024-06-18 08:55:55 -07:00
Martin Michelsen e2c34dfb70 add option to enable switch assist by default 2024-06-17 22:02:31 -07:00
Martin Michelsen 4416579210 add 11/2000 item loss patch 2024-06-17 00:42:31 -07:00
Martin Michelsen 5f591ac189 add DCv1 item loss patches 2024-06-16 23:50:37 -07:00
Martin Michelsen aa9d2beffe convert all CRLF line endings to LF only 2024-06-16 21:03:00 -07:00
Martin Michelsen 24656d587b make $where show other players' floors 2024-06-16 11:03:44 -07:00
Martin Michelsen fbaf7d722d delete overlay before parsing character data in 98 command 2024-06-16 10:44:16 -07:00
Martin Michelsen bda5c40cc2 fix disassembly of invalid episode numbers in quest headers 2024-06-16 10:44:00 -07:00
Martin Michelsen eeac5ccf4d refine battle mode commands structures 2024-06-16 10:43:46 -07:00
Martin Michelsen bbff30071e fix battle mode level up on players close to level 200 2024-06-16 10:11:35 -07:00
Martin Michelsen a7a512682c fix unsealable item kill count check 2024-06-16 00:10:12 -07:00
Martin Michelsen f3f933aaca add lobby order option for client customization 2024-06-15 17:25:58 -07:00
Martin Michelsen 5433663866 fix Guild Card comment update on BB 2024-06-15 17:11:31 -07:00
Martin Michelsen 598120c661 implement BB EXP share 2024-06-15 16:45:09 -07:00
Martin Michelsen d4f885fad1 fix 6xE3 client ID field 2024-06-15 15:20:08 -07:00
Martin Michelsen 8ab1eabda7 remove TODO 2024-06-15 15:09:43 -07:00
Martin Michelsen d23775f069 use overlay for 07ED command 2024-06-15 10:18:19 -07:00
Martin Michelsen de45f49b78 add name color for IS_CLIENT_CUSTOMIZATION flag 2024-06-15 09:56:04 -07:00
Martin Michelsen 2608d5d601 fix episode 4 bonus value tables 2024-06-14 23:43:36 -07:00
Martin Michelsen 92df4ff1e2 add CommonItemSet introspection 2024-06-14 23:41:29 -07:00
Martin Michelsen 27ecab2993 fix register reassignment if name doesn't appear first in file 2024-06-13 23:38:20 -07:00
Martin Michelsen 3dc106b42e add comments in EventUtils 2024-06-10 20:20:51 -07:00
Martin Michelsen 768e8bbfe2 make label/register assignment order deterministic 2024-06-04 22:08:18 -07:00
Martin Michelsen 324f681c46 use concise existence checks in test scripts 2024-06-04 21:28:20 -07:00
Martin Michelsen d178d062a8 add named registers in quest assembler 2024-06-04 21:17:22 -07:00
Martin Michelsen 3ac421cf55 add note about GC target crashes 2024-06-03 21:00:42 -07:00
Martin Michelsen 0e9bd019af add comment about TItemDrop random states 2024-06-02 08:43:38 -07:00
Martin Michelsen 5ce4eb8cfc fix unary operator bind order in integral tree parser 2024-05-31 23:05:19 -07:00
Martin Michelsen 64082fa872 fix attribution on some patch ports 2024-05-28 22:52:10 -07:00
Martin Michelsen 063f67d3f6 add section on license 2024-05-28 22:12:40 -07:00
Martin Michelsen 5df98fb691 speed up quest loading 2024-05-28 22:12:17 -07:00
Martin Michelsen a686d81d4c fix gcc-specific compiler warnings 2024-05-28 22:12:17 -07:00
Martin Michelsen bc9fc25799 add number as well as name for when 2024-05-28 22:12:17 -07:00
Martin Michelsen 07d8e1df7b add enum for when 2024-05-28 22:12:17 -07:00
Martin Michelsen 7427fbd252 add room unlock sound in swsetall 2024-05-28 22:12:17 -07:00
Martin Michelsen 679f58937f move ep3 offline maps to not be available by default 2024-05-28 22:12:17 -07:00
Martin Michelsen af5770058b sync config files after major quest update 2024-05-28 22:12:17 -07:00
Matt Swift d2cb7a4cb8 Actually make Caelum C1 2v2 compatible 2024-05-28 22:12:17 -07:00
Matt Swift 62c778d877 Update Caelum C1 to be 2v2 compatible 2024-05-28 22:12:17 -07:00
Matt Swift 9dd6339fe8 Add custom content for Episode 3 2024-05-28 22:12:17 -07:00
Matt Swift 7b6b8151a7 Add custom content and backports for V1 to V3 2024-05-28 22:12:17 -07:00
Martin Michelsen e77ee397cd improve bank handling across version boundaries 2024-05-28 22:12:17 -07:00
Matt Swift 8775367043 Fix typo in GC targets for XB 2024-05-24 10:16:59 -07:00
Martin Michelsen ba752eb7dc update GC connection instructions in readme 2024-05-23 21:52:17 -07:00
Martin Michelsen 8421ab16d5 fix xbox rare alert patch 2024-05-22 22:13:47 -07:00
Martin Michelsen 340a36878b add $qfread command 2024-05-22 21:19:53 -07:00
Martin Michelsen 836704e987 track telepipe state in games 2024-05-21 20:29:32 -07:00
Martin Michelsen d0ff9bd048 update specific_version comments 2024-05-21 20:29:32 -07:00
Martin Michelsen 001c2c905f fix BB item subcommands in joinable quests 2024-05-19 09:06:38 -07:00
Martin Michelsen 443a0a3037 prevent players from joining game when quest menu is open 2024-05-19 09:06:38 -07:00
Martin Michelsen d294dbcc55 actually add v2/v3 level tables 2024-05-18 23:21:16 -07:00
Martin Michelsen 0c63d6a07f add v2/v3 level table files 2024-05-18 23:16:28 -07:00
Martin Michelsen 3f6157c03f add 3OJT versions of PlayerInfo patches 2024-05-18 23:16:05 -07:00
Martin Michelsen c8eab046c0 add GC NTE save file format 2024-05-18 21:25:11 -07:00
Martin Michelsen d8230eb37a load non-v4 level tables 2024-05-17 20:32:52 -07:00
Martin Michelsen f71980382a add favored weapon type table in comments 2024-05-16 21:35:21 -07:00
Martin Michelsen 0a8678fda7 fix tekker section id 2024-05-16 19:57:46 -07:00
Martin Michelsen adb5d51510 update save file structs and encode/decode pathways 2024-05-15 22:06:11 -07:00
Martin Michelsen 45679a7f98 add index comments in shop generator 2024-05-15 22:05:34 -07:00
Martin Michelsen f6f5ca47e9 fix flag check during mag evolution 2024-05-15 10:01:38 -07:00
Martin Michelsen a4ade28755 fix BB team flag transparency 2024-05-13 22:43:12 -07:00
Martin Michelsen c957ea6c10 reorganize some quests 2024-05-13 22:34:37 -07:00
Martin Michelsen 6bfb84d999 fix joinability flags on Japanese BB government quests 2024-05-13 22:16:44 -07:00
Martin Michelsen 49fbacf0fa make ep4 quests use orange icon 2024-05-13 21:55:42 -07:00
Martin Michelsen 79efce5252 fix Tethealla client detection 2024-05-13 21:55:32 -07:00
Martin Michelsen cb9a0ed1c4 move illegal delay slot instructions out of delay slots 2024-05-13 21:13:52 -07:00
Martin Michelsen fc5788364b don't encode/decode inventories in GC extended player data 2024-05-13 20:47:21 -07:00
Martin Michelsen df2b64a601 fix big-endian ints in xbox save file format 2024-05-13 20:40:36 -07:00
Martin Michelsen 2ff75fe132 implement savechar/loadchar on DCv2 and Xbox 2024-05-12 22:40:43 -07:00
Martin Michelsen 625e8e0624 recompress all quest files 2024-05-12 16:16:45 -07:00
Martin Michelsen de8ed72233 fix disassembly of max_players header field 2024-05-12 16:11:03 -07:00
Martin Michelsen ce2607253c add missing JP quests from PR #486 2024-05-12 16:08:57 -07:00
Martin Michelsen b6fb9051b6 refine PC save file format 2024-05-12 15:09:31 -07:00
Martin Michelsen f069622b94 add DCv1 save file structure 2024-05-12 00:17:52 -07:00
Martin Michelsen 0b7e532b32 clarify $infhp behaviors 2024-05-11 22:34:12 -07:00
Martin Michelsen f4e6a40097 clean up SaveFileFormats.hh 2024-05-11 22:31:16 -07:00
Martin Michelsen 2ed97974e0 add CallProtectedHandler on BB 2024-05-11 22:31:09 -07:00
Martin Michelsen 251a9ecd0a add DC version of GetExtendedPlayerInfo 2024-05-11 21:33:31 -07:00
Martin Michelsen 777ffc1108 update GC connection instructions 2024-05-11 19:36:39 -07:00
Martin Michelsen 3951a46386 fix typo in readme 2024-05-11 19:33:23 -07:00
Martin Michelsen bfbf1ba87e update BB connection instructions 2024-05-11 19:30:17 -07:00
Martin Michelsen dc7c3eb58c add DC v2 save file format 2024-05-11 18:18:17 -07:00
Martin Michelsen a0126bd6b5 fix bug in GetExtendedPlayerInfoGC 2024-05-11 18:18:17 -07:00
Martin Michelsen c86ecbe9ef explicitly clear unsaved flag in DC item loss patch 2024-05-11 14:28:31 -07:00
Martin Michelsen 99a606be18 add flag 0x40 in part2 2024-05-11 14:28:08 -07:00
Martin Michelsen 7ebae9ed9d update check_for_hacking quest opcode flags 2024-05-11 14:27:54 -07:00
Martin Michelsen e803ca54c6 update DC item loss patches 2024-05-10 00:37:09 -07:00
Martin Michelsen d619bff349 update xbox network location struct 2024-05-10 00:36:53 -07:00
Martin Michelsen c7cb81e0fc add DCv2 item loss prevention patch 2024-05-09 00:24:50 -07:00
Martin Michelsen f7c847bcf0 fix comment 2024-05-08 21:04:10 -07:00
Martin Michelsen b81d119906 update 6x49 command name 2024-05-07 21:34:13 -07:00
Martin Michelsen 5535d749b9 update headers in handler tables 2024-05-07 21:30:34 -07:00
Martin Michelsen 992d204a83 recompress all quest files 2024-05-07 20:29:21 -07:00
Martin Michelsen b478c035bb deduplicate E/J government quest dat files 2024-05-07 20:12:30 -07:00
nolrinale 0f81d98c6e Adjusted quest filtering defaults for BB's Gov. Quests 2024-05-07 19:10:41 -07:00
nolrinale edc659a241 Adds all the Government quests in Japanese for BB 2024-05-07 19:10:41 -07:00
Martin Michelsen ef08805f93 add BB idle disconnect patch 2024-05-06 09:11:39 -07:00
Martin Michelsen 70413668d8 support B2 patches on BB 2024-05-05 10:52:09 -07:00
Martin Michelsen 27bbb2c7e4 add --language option to disassemble_quest_script 2024-05-05 09:03:08 -07:00
Martin Michelsen 43ad1597a4 change quest category menu icons 2024-05-05 08:47:08 -07:00
Martin Michelsen ce0badde87 fix mag stats reset on item combination 2024-05-05 08:42:59 -07:00
Martin Michelsen 9d46d1042b more ep3 debugging 2024-05-05 08:42:44 -07:00
Martin Michelsen 2e7c792b97 fix equip state after item combinations applied 2024-05-04 20:39:52 -07:00
Martin Michelsen c411cec06c remove debug stub 2024-05-04 12:28:49 -07:00
Martin Michelsen 451c8d5e09 add DC idle disconnect patch 2024-05-04 11:52:06 -07:00
Martin Michelsen a35753fdf1 add GetExtendedPlayerInfo for xbox 2024-05-04 11:20:44 -07:00
Martin Michelsen ca6605877a set up DC patch framework 2024-05-04 10:51:42 -07:00
Martin Michelsen 59db3c82f9 generalize ARCodeTranslator 2024-05-04 10:49:51 -07:00
Martin Michelsen e42cfb649f fix NPC inventory item creation 2024-05-03 10:10:56 -07:00
Martin Michelsen cf88455975 override BB player language code at load time 2024-05-02 22:51:24 -07:00
Martin Michelsen b272f2326e check old drop tables against new tables 2024-05-02 21:43:24 -07:00
Martin Michelsen a29494b120 describe 6x8A in more detail 2024-05-02 09:47:18 -07:00
Martin Michelsen 4d172fff64 fix challenge mode times window 2024-05-01 23:26:46 -07:00
Martin Michelsen 57ea246dd7 prep for $loadchar on xbox 2024-05-01 23:26:46 -07:00
Martin Michelsen 636309952e don't allow loading quests in incorrect game mode 2024-05-01 08:31:00 -07:00
Martin Michelsen dfeeed2b1a clarify comments in b88001.json 2024-04-30 22:32:49 -07:00
Martin Michelsen f83822bba0 add option to allow $quest without $debug for certain quests 2024-04-30 22:27:45 -07:00
Martin Michelsen 60f67fa791 add debugging for compute_effective_range_and_target_mode_for_attack 2024-04-30 21:30:45 -07:00
Martin Michelsen 9b6a6e4412 fix HTTP server segfault if proxy server is disabled 2024-04-30 09:11:02 -07:00
Martin Michelsen 83b8c199b9 support GetExtendedPlayerInfo on xbox 2024-04-30 09:10:10 -07:00
Martin Michelsen 3f1939e674 increase number of savechar slots to 16 2024-04-29 22:21:08 -07:00
Martin Michelsen 31616954cc implement extended $loadchar on GC 2024-04-28 23:48:02 -07:00
Martin Michelsen ee21885f13 add more missing initializers 2024-04-28 15:38:57 -07:00
Martin Michelsen 2cc6a85d4b add missing initializer 2024-04-28 15:33:40 -07:00
Martin Michelsen 29320f0858 don't skip server data commands before battle start 2024-04-28 15:19:31 -07:00
Martin Michelsen 29f200b83e add a way for joinable quests to lock themselves 2024-04-28 00:23:21 -07:00
Martin Michelsen 09bf81f77f fix duplicate 6xDD commands 2024-04-27 18:31:10 -07:00
Martin Michelsen ddbb922b95 support joinable quests on all versions 2024-04-27 18:31:10 -07:00
Martin Michelsen c7dd98ccc0 use flag to separate customized GC clients from non-customized 2024-04-27 14:25:46 -07:00
Martin Michelsen f5c2c930d8 don't use $CG in any server announcements 2024-04-27 12:08:31 -07:00
Martin Michelsen 79fee4cec4 explain overwritten field in DecoctionXB 2024-04-27 10:26:02 -07:00
Martin Michelsen 0bec4d0f49 update sub_version conditions 2024-04-27 10:25:45 -07:00
Martin Michelsen a4fc133d75 block 6xB2 in most cases 2024-04-26 21:09:21 -07:00
Martin Michelsen 45c9dc9a23 rename PSOXReticleColors to match convention 2024-04-26 20:43:47 -07:00
Martin Michelsen 594ffbe7e6 add xbox rare drop notifs patch 2024-04-25 20:00:19 -07:00
Martin Michelsen 7decab75c2 update 6xB2 structure 2024-04-24 23:35:15 -07:00
Martin Michelsen 9815126ced save battle records when CA handler raises 2024-04-23 22:36:29 -07:00
Martin Michelsen 4b5eba3727 upgrade to c++23 2024-04-23 22:23:25 -07:00
Martin Michelsen 49010b02f1 sort CMakeLists 2024-04-22 22:07:04 -07:00
Martin Michelsen d08aaef0f8 add remote address to command log messages 2024-04-21 15:19:16 -07:00
Martin Michelsen 245df782b9 fix v2 battle record init sequence 2024-04-21 01:27:29 -07:00
Martin Michelsen 9ffe429a1f implement ban/unban accounts via the shell 2024-04-21 01:14:10 -07:00
Martin Michelsen 673c767a42 add random stream into Ep3 battle records 2024-04-21 01:14:10 -07:00
Martin Michelsen de42135532 implement IPv4 range bans 2024-04-21 01:14:10 -07:00
Martin Michelsen 79bf6b3fa9 fix rendering issue in readme 2024-04-20 14:31:41 -07:00
Martin Michelsen 741456d1da organize system/client-functions 2024-04-20 10:51:48 -07:00
Martin Michelsen c95b158e4e add decrypt/encrypt for simple DCv2 executable encryption 2024-04-20 10:51:48 -07:00
Martin Michelsen d40c260d18 fix infinite loop in determine_first_team_turn 2024-04-17 11:36:32 -07:00
Martin Michelsen 454e0e558b clean up notes directory 2024-04-17 08:30:00 -07:00
Martin Michelsen 5ea49425c7 don't fail on proxy server if maps don't load properly 2024-04-17 00:39:26 -07:00
Martin Michelsen 08ea9403e9 add encrypt/decrypt actions for DCv2 executables 2024-04-17 00:37:57 -07:00
Martin Michelsen f01882db39 improve PRS disassembly output 2024-04-17 00:37:30 -07:00
Martin Michelsen 1870273f89 add further learnings about Ep3 B9 command 2024-04-15 22:53:14 -07:00
Martin Michelsen d6edf1b24d set up framework for DC patching 2024-04-14 22:20:28 -07:00
Martin Michelsen 8ecbe6798d fix --config option to less-common commands 2024-04-14 20:58:55 -07:00
Martin Michelsen 587ad1933d add DC 50Hz sub_versions 2024-04-14 20:57:45 -07:00
Martin Michelsen 70548aef04 move Ep3 recording finalization to CA command handler 2024-04-14 13:56:24 -07:00
Martin Michelsen 43663cbe79 add missing include on linux 2024-04-12 22:24:04 -07:00
Martin Michelsen 5f2e7e543b fix some patch metadata 2024-04-12 22:17:16 -07:00
Martin Michelsen c98d1081a3 add support for auto-patching 2024-04-12 22:17:16 -07:00
Martin Michelsen 0b2272bfa7 don't show non-unique team rewards in purchased list 2024-04-12 22:09:52 -07:00
Martin Michelsen 04982d919c fix 11/2000 set data table 2024-04-12 22:09:52 -07:00
Martin Michelsen 34751f99e9 allow multiple licenses per account 2024-04-12 22:09:52 -07:00
Martin Michelsen 40d5c6ee64 fix --config option to non-server actions 2024-04-07 14:40:18 -07:00
Martin Michelsen be0b70f903 use existing test config for load-maps-test 2024-04-07 13:35:29 -07:00
Martin Michelsen 76aeacfdfd fix permission on custom-sji test input 2024-04-07 13:34:06 -07:00
Martin Michelsen dec979fb52 fix custom-sjis test 2024-04-07 13:13:58 -07:00
Martin Michelsen 1c85d46436 add load-maps test 2024-04-07 13:04:08 -07:00
Martin Michelsen f05dc6d9f9 handle PSO font characters properly 2024-04-07 13:03:11 -07:00
Martin Michelsen e141642dd6 fix episode field in game list command 2024-04-06 22:58:53 -07:00
Martin Michelsen af4d3a3325 implement full character backups on GC 2024-04-06 19:52:22 -07:00
Martin Michelsen 91131f8b36 update notes on xb bugfix patch 2024-04-02 22:22:09 -07:00
Martin Michelsen b2ea059fd8 add xb reticle color patches 2024-04-02 22:21:21 -07:00
Martin Michelsen 150acda1ea add union field team reward 2024-04-02 00:01:15 -07:00
Martin Michelsen 3e1449bb80 add team size field for union field 2024-04-02 00:01:05 -07:00
Martin Michelsen 4c104443bc fix non-unique team rewards 2024-04-01 23:31:55 -07:00
Martin Michelsen de8a210d0f add debug messages for wave events and switch flags 2024-04-01 23:28:41 -07:00
Martin Michelsen 9d2b36b787 add idle disconnect patch 2024-04-01 21:50:39 -07:00
Martin Michelsen 03b78c3825 add WIP XB bugfixes patch 2024-04-01 21:50:39 -07:00
Matt 3c8674dcc7 Provide updated Teth client links (#1)
This adds archive links to updated Teth clients to make set up work with newserv more seamlessly. The Teth clients linked:

- Have map files for Coren
- Have fixed unitxt_e.prs and unitxt_j.prs files so no "Revival Curiass" error
- Connect to 127.0.0.1 by default for the common use case of local server on same machine
- Japanese client has had the version string changed to "JTethVer12513" so it doesn't get overriden to English

I will probably update these archives in the future to use the English files properly with the English client but for now this will make things just work in the mean time hopefully.
2024-04-01 19:22:03 -07:00
Martin Michelsen 95919b8b01 add xbox hungry mag sound patch 2024-03-31 17:59:18 -07:00
Martin Michelsen 1712b13106 add link to original installer in readme 2024-03-31 16:49:35 -07:00
Martin Michelsen 50a32429be split rare announcement item sets by game version 2024-03-31 12:31:25 -07:00
Martin Michelsen 6f0124f7ec add $edit language 2024-03-31 11:59:21 -07:00
Martin Michelsen acbebaeb70 use scrolling message for rare and max level announcements on BB 2024-03-31 10:06:47 -07:00
Martin Michelsen d44b0b3d62 add max level notifications 2024-03-30 23:37:50 -07:00
Martin Michelsen 4a3b0118a8 replace UnlockAllAreas and PreventPersistQuestFlags with generalized rewrite map 2024-03-30 22:36:09 -07:00
Martin Michelsen 7c7df39e6d clarify BB behavior with UnlockAllAreas 2024-03-30 20:47:26 -07:00
Martin Michelsen dba49be1e3 add name for 6xB4x4A 2024-03-30 20:47:08 -07:00
Martin Michelsen 33483bbfbf handle duplicate set event IDs properly 2024-03-30 13:38:17 -07:00
Martin Michelsen 9630b06284 refine 6x68 structure 2024-03-30 13:37:50 -07:00
Martin Michelsen e6acea8247 add $swset, $swclear, and $swsetall 2024-03-29 21:08:42 -07:00
Martin Michelsen 2cd4c733ef switch item pickup notifs to explicit lists 2024-03-29 21:08:42 -07:00
Matt 05e5705537 Update ReceiveCommands.cc 2024-03-29 20:02:08 -07:00
Martin Michelsen 24e48b1abd write short readme section about accounts 2024-03-28 22:51:25 -07:00
Martin Michelsen 6d73cae91b fix desyncs if protected commands aren't supported by client 2024-03-28 22:50:57 -07:00
Martin Michelsen dd9bc51457 implement rare item pickup notifications 2024-03-28 21:44:05 -07:00
Martin Michelsen dce0f91678 highlight hit% if dropped weapon has positive bonus 2024-03-27 20:15:16 -07:00
Martin Michelsen eb5701ece9 update BB patch directory setup instructions 2024-03-26 14:00:48 -07:00
Martin Michelsen 6f99b3b1c8 run patch server on main thread on windows 2024-03-25 22:28:15 -07:00
Martin Michelsen da9765f1aa fix cleanup in compression test 2024-03-25 22:28:02 -07:00
Martin Michelsen b7897cddf2 show uncaught exception messages on windows 2024-03-24 22:00:22 -07:00
Martin Michelsen ce2300b116 add pessimal compression 2024-03-24 21:59:28 -07:00
Martin Michelsen cb05dce764 handle quest loading client bug 2024-03-24 15:43:35 -07:00
Martin Michelsen a762c0f8f8 make prev battle record const 2024-03-24 10:24:36 -07:00
Martin Michelsen cd008ab0ba rewrite DeckState::draw_card_by_ref 2024-03-23 21:02:00 -07:00
Martin Michelsen 53b36d7074 put an extra \n in choice search result text 2024-03-23 21:02:00 -07:00
Martin Michelsen 5a1880bd65 allow sender_c to be null in Ep3 server command handlers 2024-03-23 21:02:00 -07:00
Martin Michelsen 8e280a1464 fix wrong type in default ep3 behavior flags 2024-03-22 22:25:14 -07:00
Martin Michelsen 0bcdd9997e define choice_search_config in gc char file format 2024-03-22 22:25:04 -07:00
Martin Michelsen d5351c4580 set BB player mag color at char creation time 2024-03-22 22:24:45 -07:00
Martin Michelsen 76bc2385ca add PSOBB Hangame functions 2024-03-22 22:24:04 -07:00
Martin Michelsen 325f7c6efc add UnlockAllAreas config option 2024-03-18 10:03:37 -07:00
Martin Michelsen 93d97d3e5b factor out debug mode check 2024-03-17 21:16:31 -07:00
Martin Michelsen 66b64603a0 add $sb command 2024-03-17 19:03:24 -07:00
Martin Michelsen 7405eaea0b add format-ep3-battle-record command 2024-03-17 14:12:57 -07:00
Martin Michelsen 477e433361 update some command notes 2024-03-17 14:12:57 -07:00
Martin Michelsen 7ca2012bc4 add CA commands into Ep3 battle record format 2024-03-16 18:48:27 -07:00
Martin Michelsen dace165ef2 fix enemy data json in /y/data/common-tables 2024-03-16 18:45:35 -07:00
Martin Michelsen f6df2b5b45 add note about C4 crash 2024-03-16 18:45:11 -07:00
Martin Michelsen 1a310df17e fix choice search crash 2024-03-16 09:57:35 -07:00
Martin Michelsen 31edec701b refine game info messages 2024-03-15 22:59:50 -07:00
Martin Michelsen dc36d2ae8d fix quest expr checks from lobby 2024-03-15 10:20:19 -07:00
Martin Michelsen 4e733b0dc6 add object type name in map disassembly 2024-03-15 00:32:00 -07:00
Martin Michelsen 6eadaaca66 use pthreads for libevent on windows 2024-03-15 00:31:50 -07:00
Martin Michelsen d778340999 add BB format of 6x6F command 2024-03-15 00:31:33 -07:00
3205 changed files with 87564 additions and 183133 deletions
+6 -5
View File
@@ -6,7 +6,7 @@ cmake_minimum_required(VERSION 3.10)
project(newserv) project(newserv)
set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED True) set(CMAKE_CXX_STANDARD_REQUIRED True)
if (MSVC) if (MSVC)
add_compile_options(/W4 /WX) add_compile_options(/W4 /WX)
@@ -58,6 +58,7 @@ add_custom_target(
set(SOURCES set(SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/src/Revision.cc ${CMAKE_CURRENT_SOURCE_DIR}/src/Revision.cc
src/Account.cc
src/AFSArchive.cc src/AFSArchive.cc
src/BattleParamsIndex.cc src/BattleParamsIndex.cc
src/BMLArchive.cc src/BMLArchive.cc
@@ -89,23 +90,24 @@ set(SOURCES
src/GSLArchive.cc src/GSLArchive.cc
src/GVMEncoder.cc src/GVMEncoder.cc
src/HTTPServer.cc src/HTTPServer.cc
src/IntegralExpression.cc
src/IPFrameInfo.cc src/IPFrameInfo.cc
src/IPStackSimulator.cc src/IPStackSimulator.cc
src/IPV4RangeSet.cc
src/ItemCreator.cc src/ItemCreator.cc
src/ItemData.cc src/ItemData.cc
src/ItemNameIndex.cc src/ItemNameIndex.cc
src/ItemParameterTable.cc src/ItemParameterTable.cc
src/Items.cc src/Items.cc
src/LevelTable.cc src/LevelTable.cc
src/License.cc
src/Lobby.cc src/Lobby.cc
src/Loggers.cc src/Loggers.cc
src/Main.cc src/Main.cc
src/Map.cc src/Map.cc
src/Menu.cc src/Menu.cc
src/NetworkAddresses.cc src/NetworkAddresses.cc
src/PatchServer.cc
src/PatchFileIndex.cc src/PatchFileIndex.cc
src/PatchServer.cc
src/PlayerFilesManager.cc src/PlayerFilesManager.cc
src/PlayerSubordinates.cc src/PlayerSubordinates.cc
src/ProxyCommands.cc src/ProxyCommands.cc
@@ -114,7 +116,6 @@ set(SOURCES
src/PSOGCObjectGraph.cc src/PSOGCObjectGraph.cc
src/PSOProtocol.cc src/PSOProtocol.cc
src/Quest.cc src/Quest.cc
src/QuestAvailabilityExpression.cc
src/QuestScript.cc src/QuestScript.cc
src/RareItemSet.cc src/RareItemSet.cc
src/ReceiveCommands.cc src/ReceiveCommands.cc
@@ -135,7 +136,7 @@ set(SOURCES
) )
if(resource_file_FOUND) if(resource_file_FOUND)
set(SOURCES ${SOURCES} src/ARCodeTranslator.cc) set(SOURCES ${SOURCES} src/AddressTranslator.cc)
endif() endif()
add_executable(newserv ${SOURCES}) add_executable(newserv ${SOURCES})
+1 -2
View File
@@ -1,6 +1,6 @@
The MIT License (MIT) The MIT License (MIT)
Copyright (c) 2023 Martin Michelsen Copyright (c) 2024 Martin Michelsen
Permission is hereby granted, free of charge, to any person obtaining a copy of Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in this software and associated documentation files (the "Software"), to deal in
@@ -18,4 +18,3 @@ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+177 -80
View File
@@ -12,12 +12,14 @@ See TODO.md for a list of known issues and future work I've curated, or go to th
* Background * Background
* [History](#history) * [History](#history)
* [Other server projects](#other-server-projects) * [Other server projects](#other-server-projects)
* [Using newserv in other projects](#using-newserv-in-other-projects)
* [Compatibility](#compatibility) * [Compatibility](#compatibility)
* Setup * Setup
* [Server setup](#server-setup) * [Server setup](#server-setup)
* [Client patch directories for PC and BB](#client-patch-directories) * [Client patch directories for PC and BB](#client-patch-directories)
* [How to connect](#how-to-connect) * [How to connect](#how-to-connect)
* Features and configuration * Features and configuration
* [User accounts](#user-accounts)
* [Installing quests](#installing-quests) * [Installing quests](#installing-quests)
* [Item tables and drop modes](#item-tables-and-drop-modes) * [Item tables and drop modes](#item-tables-and-drop-modes)
* [Cross-version play](#cross-version-play) * [Cross-version play](#cross-version-play)
@@ -61,7 +63,14 @@ Independently of this project, there are many other PSO servers out there. Those
* (2019) **[Mechonis](https://gitlab.com/sora3087/mechonis)**: A PSOBB server with a microservice architecture written in TypeScript by TrueVision. * (2019) **[Mechonis](https://gitlab.com/sora3087/mechonis)**: A PSOBB server with a microservice architecture written in TypeScript by TrueVision.
* (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) **[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. * (2021) **[Elseware](http://git.sharnoth.com/jake/elseware)**: A PSOBB server written in Rust by Jake.
* (2022) **[PSOSERVER](https://github.com/Sancaros/PSOSERVER)**: A server for all versions, written in C by Sancaros and based on Sylverant and newserv.
## Using newserv in other projects
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.
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.
# Compatibility # Compatibility
@@ -131,7 +140,10 @@ 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: For Blue Burst set up, the below is mandatory for a smooth experience:
1. Browse to your chosen client's data directory. 1. Browse to your chosen client's data directory.
2. Copy all the map_*.dat files and the data.gsl file and place them in `system/patch-bb/data` 2. Copy all the map_*.dat files, 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 do not have a BB client, or 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.
For BB clients, newserv reads some files out of the patch data to implement game logic, so it's important that certain game files are synchronized between the server and the client. newserv contains defaults for these files in the system/maps/bb-v4 directory, but if these don't match the client's copies of the files, odd behavior will occur in games. For BB clients, newserv reads some files out of the patch data to implement game logic, so it's important that certain game files are synchronized between the server and the client. newserv contains defaults for these files in the system/maps/bb-v4 directory, but if these don't match the client's copies of the files, odd behavior will occur in games.
@@ -179,9 +191,13 @@ The version of PSO PC I have has the server addresses starting at offset 0x29CB3
### PSO GC on a real GameCube ### PSO GC on a real GameCube
You can make PSO connect to newserv by setting its default gateway and DNS server addresses in network settings to newserv's address. newserv's DNS server must be running on port 53 and must be accessible to the GameCube. You can make PSO connect to newserv by setting the default gateway and DNS server addresses in the game's network settings to newserv's address. newserv's DNS server must be running on port 53 and must be accessible to the GameCube. If you're not playing PSO Plus or Episode III, this should be all you need to do, assuming you already set LocalAddress in config.json to your PC's private IP address.
If you have PSO Plus or Episode III, it won't want to connect to a server on the same local network as the GameCube itself, as determined by the GameCube's IP address and subnet mask. In the old days, one way to get around this was to create a fake network adapter on the server (or use an existing real one) that has an IP address on a different subnet, tell the GameCube that the server is the default gateway (as above), and have the server reply to the DNS request with its non-local IP address. To do this with newserv, just set LocalAddress in the config file to a different interface. For example, if the GameCube is on the 192.168.0.x network and your other adapter has address 10.0.1.6, set newserv's LocalAddress to 10.0.1.6 and set PSO's DNS server and default gateway addresses to the server's 192.168.0.x address. This may not work on modern systems or on non-Windows machines - I haven't tested it in many years. If you have PSO Plus or Episode III, it won't want to connect to a server on the same local network as the GameCube itself, as determined by the GameCube's IP address and subnet mask. There are a couple of ways to get around this.
Sodaboy described a fairly easy method, which is to forward the PSO and DNS ports in your router's configuration to your PC's private IP address (the PSO ports are in config.json, and are all TCP; the DNS port is 53 and is UDP). Then, set LocalAddress and ExternalAddress in config.json to your external IP address (from e.g. whatismyip.com). Most routers will let you connect to your public IP address even from within the local network, but the GameCube will think it's connecting to a different network, so it won't reject the connection. If you're concerned about security and don't want your server to be publicly accessible, you can use Windows Firewall or UFW on Linux block incoming connections on the ports you opened, except for connections from the IP addresses you specify.
Another method is to use two network interfaces on the same PC, and tell the GameCube to connect to the one that appears to be on a different network. For example, if your GameCube is on the 10.0.0.x subnet and your PC's address is 10.0.0.5, you can create a fake network adapter on your PC (or use an existing real one) that has an IP address on a different subnet than the GameCube, such as 192.168.0.8. Then, in PSO's network config, set the default gateway and DNS server addresses to 192.168.0.8, and set LocalAddress in config.json to 192.168.0.8, and PSO should connect. This is what I did back in the old days when I primarily developed software on Windows, but I haven't tried it in many years.
### PSO GC on a Wii or Wii U ### PSO GC on a Wii or Wii U
@@ -189,7 +205,7 @@ Using a Wii or Wii U to connect to newserv requires the Wii or vWii to be softmo
Nintendont includes BBA emulation and is compatible with all PSO GameCube versions except Episodes I&II Trial Edition. To use Nintendont, enable BBA emulation in Nintendont's settings and follow the instructions in the above section (PSO GC on a real GameCube). Nintendont includes BBA emulation and is compatible with all PSO GameCube versions except Episodes I&II Trial Edition. To use Nintendont, enable BBA emulation in Nintendont's settings and follow the instructions in the above section (PSO GC on a real GameCube).
Devolution includes modem emulation and is compatible with all PSO GameCube versions including Episodes I&II Trial Edition. newserv can act as a PPP server, which Devolution can directly cnnect to. To do this: Devolution includes modem emulation and is compatible with all PSO GameCube versions including Episodes I&II Trial Edition. newserv can act as a PPP server, which Devolution can directly connect to. To do this:
1. Enable the PPPRawListen option according to the comments in config.json. 1. Enable the PPPRawListen option according to the comments in config.json.
2. Start newserv. 2. Start newserv.
3. In the game's network settings, set the username and password to anything (they cannot be blank), and set the phone number to the number that newserv outputs to the console during startup. (It will be near the end of all the startup log messages.) If your Wii is on the same network as newserv, use the local number; otherwise, use the external number. 3. In the game's network settings, set the username and password to anything (they cannot be blank), and set the phone number to the number that newserv outputs to the console during startup. (It will be near the end of all the startup log messages.) If your Wii is on the same network as newserv, use the local number; otherwise, use the external number.
@@ -200,9 +216,9 @@ If you're using the HLE BBA type, set the BBA's DNS server address to newserv's
If you're using the TAP BBA type, you'll have to set PSO's network settings appropriately for your tap interface. Set the DNS server address in PSO's network settings to newserv's IP address. If you're using the TAP BBA type, you'll have to set PSO's network settings appropriately for your tap interface. Set the DNS server address in PSO's network settings to newserv's IP address.
If you're using a version of Dolphin with tapserver support, you can make it connect to a newserv instance running on the same machine via the tapserver interface. You do not need to install or run tapserver. To do this: If you're using the tapserver BBA or modem type, you can make it connect to a newserv instance running on the same machine via the tapserver interface. To do this:
1. Set Dolphin's BBA type to tapserver (Config -> GameCube -> SP1). 1. In the GameCube pane of the Config window, set the SP1 device to Broadband Adapter (tapserver) or Modem Adapter (tapserver).
2. Enable newserv's IP stack simulator according to the comments in config.json and start newserv. 2. Set IPStackListen (for BBA) or PPPStackListen (for modem) according to the comments in config.json and start newserv.
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. 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. 4. Start an online game.
@@ -210,9 +226,12 @@ If you're using a version of Dolphin with tapserver support, you can make it con
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 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" section for instructions on setting this up.) 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 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" section for instructions on setting this up.)
The original Japanese and US versions of PSO BB should work, but you'll have to modify your hosts file or edit psobb.exe to point to your newserv instance. The original versions are packed, so this is a more involved process than simply opening the executable in a hex editor and finding/replacing some strings. The original Japanese and US versions of PSO BB work with newserv (the last Japanese release can be found [here](https://archive.org/details/psobb_jp_setup_12511_20240109/)). To get them to connect to your server, do one of the following:
* Use a drop-in patcher like [AzureFlare](https://github.com/Repflez/AzureFlare).
* Modify your hosts file to redirect the client's destination address to localhost or your server's address.
* Edit psobb.exe to point to your newserv instance. The original clients are packed with various versions of ASProtect, so this is a more involved process than simply opening the executable in a hex editor and finding/replacing some strings.
Alternatively, you can use the Tethealla client (https://archive.org/details/psobb-tethealla-client); you can find the connection addresses starting at 0x56D724 in psobb.exe. Overwrite these addresses with your server's hostname or IP address, and you should be able to connect. Alternatively, you can use the Tethealla client ([English](https://web.archive.org/web/20240402011115/https://ragol.org/files/bb/TethVer12513_English.zip) or [Japanese](https://web.archive.org/web/20240402013127/https://ragol.org/files/bb/TethVer12513_Japanese.zip)). If the server is on the same PC as the client and you don't plan to have any external players, these Tethealla clients will automatically connect to the server without any modifications. This version of the client is not packed, and you can find the connection addresses starting at 0x56D724 in psobb.exe. Overwrite these addresses with your server's hostname or IP address, and you should be able to connect.
### Connecting external clients ### Connecting external clients
@@ -222,6 +241,23 @@ For GC clients, you'll have to use newserv's built-in DNS server or set up your
# Server feature configuration # Server feature configuration
## User accounts
By default, newserv does not require users to pre-register before playing; the server will instead automatically create an account the first time each player connects. These accounts have no special permissions. You can view, create, edit, and delete user accounts in the server's shell (run `help` in the shell to see how to do this).
A license is a set of credentials that a player can use to log in. There are six types of licenses:
* *DC NTE licenses* consist of a 16-character serial number and 16-character access key.
* *DC licenses* consist of an 8-character hex serial number and an 8-character access key.
* *PC licenses* are the same format as DC licenses, but are used for PC v2.
* *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.
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.
You may want to give your account elevated privileges. To do so, run `update-account ACCOUNT-ID flags=root` (replacing ACCOUNT-ID with your actual account-id). You can also use update-account to edit other parts of the account; see the help text for more information.
## Installing quests ## Installing quests
newserv automatically finds quests in the subdirectories of the system/quests/ directory. To install your own quests, or to use quests you've saved using the proxy's Save Files option, just put them in one of the subdirectories there and name them appropriately. The subdirectories and their behaviors (e.g. in which game modes they should appear and for which PSO versions) is defined in the QuestCategories field in config.json. newserv automatically finds quests in the subdirectories of the system/quests/ directory. To install your own quests, or to use quests you've saved using the proxy's Save Files option, just put them in one of the subdirectories there and name them appropriately. The subdirectories and their behaviors (e.g. in which game modes they should appear and for which PSO versions) is defined in the QuestCategories field in config.json.
@@ -303,6 +339,32 @@ All versions of PSO can see and interact with each other in the lobby. newserv a
In V1/V2 cross-version play, when any of the server drop modes are used, the server uses the drop table corresponding to the version the game was created with. (For example, if a DC V1 player created the game, rare-table-v1.json will be used, even after V2 players join.) In V1/V2 cross-version play, when any of the server drop modes are used, the server uses the drop table corresponding to the version the game was created with. (For example, if a DC V1 player created the game, rare-table-v1.json will be used, even after V2 players join.)
## Server-side saves
newserv has the ability to save character data on the server side. For PSO BB, this is required of course, but this feature can also be used on other PSO versions.
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.
Exactly which data is saved and loaded depends on the game version:
| Game | Inventory | Character | Options/chats | Quest flags | Bank | Battle/challenge |
|----------------------|-----------|-----------|---------------|-------------|------|------------------|
| PSO DC v1 prototypes | Yes | Yes | No | No | No | N/A |
| PSO DC v1 | Yes | Yes | No | No | No | N/A |
| PSO DC v2 | Yes | Yes | Yes | Yes | Yes | Yes |
| PSO PC (v2) | Yes | Yes | No | No | No | Save only |
| PSO GC NTE | Yes | Yes | Yes | Yes | Yes | Yes |
| PSO GC (not Plus) | Yes | Yes | Yes | Yes | Yes | Yes |
| PSO GC Plus (1) | Save only | Save only | No | No | No | Save only |
| PSO GC Ep3 (1) | No | Save only | No | No | No | Save only |
| PSO Xbox | Yes | Yes | Yes | Yes | Yes | Yes |
| PSO BB | Yes | Yes | Yes | Yes | Yes | Yes |
*Notes*:
1. *If EnableSendFunctionCallQuestNumber is enabled in config.json, then $savechar and $loadchar can save and restore all character data on these versions, just like on GC non-Plus. Episode 3 characters exist in a separate namespace; that is, you can't use $savechar and $loadchar to convert an Ep3 character to non-Ep3, or vice versa.*
## Episode 3 features ## Episode 3 features
newserv supports many features unique to Episode 3: newserv supports many features unique to Episode 3:
@@ -334,35 +396,65 @@ 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.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. * 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. * 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 all the original online and offline maps, including Story Mode quests. If you don't want the offline maps and quests to be playable online, delete the .bind files system/ep3/maps. * 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-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.
* 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). * 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 .bin or .mnm files before editing them, but you don't need to compress the files again to use them - just put the .bind or .mnmd file in the maps directory and newserv will make it available. 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-data` in the interactive shell to make the changes take effect without restarting the server. 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 ## 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. *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.*
In addition, these features are only supported for the following game versions: 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/WriteMemory.ppc.s.
* PSO GameCube Episodes 1&2 Trial Edition
* PSO GameCube Episodes 1&2 JP, USA, and EU but not Plus The VERS token in client function filenames refers to the specific version of the game that the client function applies to. Some versions do not support receiving client functions at all. The specific versions are:
* PSO GameCube Episodes 1&2 Plus JP v1.4 but not v1.5
* PSO GameCube Episode 3 Trial Edition | Game | VERS | Architecture |
* PSO GameCube Episode 3 JP |-------------------|------|---------------|
* PSO GameCube Episode 3 USA (experimental; must be manually enabled in config.json) | PSO DC NTE | 1OJ1 | Not supported |
* PSO Xbox (all versions) | PSO DC 11/2000 | 1OJ2 | Not supported |
* PSO BB | PSO DC 12/2000 | 1OJ3 | Not supported |
| PSO DC 01/2001 | 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 | 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 NTE | 3OJT | PowerPC |
| PSO GC v1.2 JP | 3OJ2 | PowerPC |
| PSO GC v1.3 JP | 3OJ3 | PowerPC |
| PSO GC v1.4 JP | 3OJ4 | PowerPC |
| PSO GC v1.5 JP | 3OJ5 | Not supported |
| PSO GC v1.0 US | 3OE0 | PowerPC |
| PSO GC v1.1 US | 3OE1 | PowerPC |
| PSO GC v1.2 US | 3OE2 | Not supported |
| PSO GC v1.0 EU | 3OP0 | PowerPC |
| PSO GC Ep3 NTE | 3SJT | PowerPC |
| PSO GC Ep3 JP | 3SJ0 | PowerPC |
| PSO GC Ep3 US | 3SE0 | Not supported |
| PSO GC Ep3 EU | 3SP0 | Not supported |
| 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.13 | 51OC | x86 |
| PSO BB Tethealla | 51OC | x86 |
*Note: newserv uses the shorter GameCube versioning convention, where discs labeled DOL-XXXX-0-0Y are version 1.Y. The PSO community seems to use the convention 1.0Y in some places instead, but these are the same version. For example, the version that newserv calls v1.4 is the same as v1.04, and is labeled DOL-GPOJ-0-04 on the underside of the disc.* *Note: newserv uses the shorter GameCube versioning convention, where discs labeled DOL-XXXX-0-0Y are version 1.Y. The PSO community seems to use the convention 1.0Y in some places instead, but these are the same version. For example, the version that newserv calls v1.4 is the same as v1.04, and is labeled DOL-GPOJ-0-04 on the underside of the disc.*
You can put memory patches in the system/client-functions directory with filenames like PatchName.patch.s and they will appear in the Patches menu for PSO GC, XB, and BB clients that support patching. Memory patches are written in 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/WriteMemory.ppc.s. newserv comes with a set of patches for some 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 GC Episodes 1&2 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). 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, ReadMemoryWord.ppc.s, WriteMemory.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.
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, ReadMemoryWord.ppc.s, WriteMemory.ppc.s, and RunDOL.ppc.s must be present in the system/client-functions 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. 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.
@@ -410,97 +502,102 @@ newserv supports a variety of commands players can use by chatting in-game. Any
Some commands only work on the game server and not on the proxy server. The chat commands are: Some commands only work on the game server and not on the proxy server. The chat commands are:
* Information commands * Information commands
* `$li`: Shows basic information about the lobby or game you're in. If you're on the proxy server, shows information about your connection instead (remote Guild Card number, client ID, etc.). * `$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): Shows basic information about the server. * `$si` (game server only): Show basic information about the server.
* `$ping`: Shows round-trip ping time from the server to you. On the proxy server, shows the ping time from you to the proxy and from the proxy to 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): Shows how many of each type of material you've used. * `$matcount` (game server only): Show how many of each type of material you've used.
* `$itemnotifs <mode>`: Enables 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: * `$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. * `off`: No notifications are shown.
* `rare`: You are notified when a rare item drops. * `rare`: You are notified when a rare item drops.
* `on`: You are notified when any item drops, except Meseta. * `on`: You are notified when any item drops, except Meseta.
* `every`: You are notified when any item drops, including Meseta. * `every`: You are notified when any item drops, including Meseta.
* `$what` (game server only): Shows the type, name, and stats of the nearest item on the ground. * `$what` (game server only): Show the type, name, and stats of the nearest item on the ground.
* `$where` (game server only): Shows your current floor number and coordinates. Mainly useful for debugging. * `$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.
* Debugging commands * Debugging commands
* `$debug` (game server only): Enable or disable debug. You need the DEBUG permission in your user license to use this command. Enabling debug does a few things: * `$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:
* You'll see in-game messages from the server when you take certain actions, like killing an enemy in BB. * You'll see in-game messages from the server when you take certain actions, like killing an enemy in BB.
* You'll see the rare seed value and floor variations when you join a game. * You'll see the rare seed value and floor variations when you join a game.
* You'll be placed into the highest available slot in lobbies and games instead of the lowest, unless you're joining a BB solo-mode 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 crossplay is normally supported. Be prepared for client crashes and other client-side brokenness if you do this. Please do not submit any issues for broken behaviors in crossplay, unless the situation is explicitly supported (see the "Cross-version play" section above). * You'll be able to join games with any PSO version, not only those for which crossplay is normally supported. Be prepared for client crashes and other client-side brokenness if you do this. Do not submit any issues for broken behaviors in crossplay, unless the situation is explicitly supported (see the "Cross-version play" section above).
* The rest of the commands in this section are enabled on the game server. (They are always enabled on the proxy server.) * The rest of the commands in this section are enabled on the game server. (They are always enabled on the proxy server.)
* `$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. * `$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.
* `$qcall <function-id>`: Call a quest function on your client. * `$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). * `$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. * `$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): Get the value of a quest counter ("global flag"). This command can be used without debug mode enabled. * `$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>` (game server 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. * `$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. * `$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. * `$gc` (game server only): Send your own Guild Card to yourself.
* `$sc <data>`: Send a command to yourself. * `$sc <data>`: Send a command to yourself.
* `$ss <data>` (proxy server only): Send a command to the remote server. * `$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.
* `$meseta <amount>` (game server only; Episode 3 only): Add the given amount to your Meseta total. * `$meseta <amount>` (game server only; Episode 3 only): Add the given amount to your Meseta total.
* `$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, regardless of how many players are in the game or if you have a VIP card.
* `$ep3battledebug` (game server only; Episode 3 only): Enable or disable TCard00_Select. If enabled, the game will enter the debug menu when you start a battle. * `$ep3battledebug` (game server only; Episode 3 only): Enable or disable TCard00_Select. If enabled, the game will enter the debug menu when you start a battle.
* Personal state commands * Personal state commands
* `$arrow <color-id>`: Changes your lobby arrow color. * `$arrow <color-id>`: Change your lobby arrow color.
* `$secid <section-id>`: Sets 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 (e.g. on BB, or on Schtserv with server drops enabled). If the server does not allow cheat mode anywhere (that is, "CheatModeBehavior" is "Off" in config.json), this command does nothing. * `$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 (e.g. on BB, or on Schtserv with server drops enabled). If the server does not allow cheat mode anywhere (that is, "CheatModeBehavior" is "Off" in config.json), this command does nothing.
* `$rand <seed>`: Sets 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. * `$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]`: Sets 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. * `$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.
* `$swa`: Enables or disables switch assist. When enabled, the server will attempt to automatically unlock two-player and four-player doors in non-quest games if you step on all the required switches sequentially. * `$swa`: Enable or disable switch assist. When enabled, the server will attempt to automatically unlock two-player and four-player doors in non-quest games if you step on all the required switches sequentially.
* `$exit`: If you're in a lobby, sends you to the main menu (which ends your proxy session, if you're in one). If you're in a game or spectator team, sends 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. * `$exit`: If you're in a lobby, send you to the main menu (which ends your proxy session, if you're in one). If you're in a game or spectator team, send you to the lobby (but does not end your proxy session if you're in one). Does nothing if you're in a non-Episode 3 game and no quest is in progress.
* `$patch <name>`: Run a patch on your client. `<name>` must exactly match the name of a patch on the server. * `$patch <name>`: Run a 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 (game server only)
* `$savechar <slot>`: Saves your current character data on the server in the specified slot (each serial number has 4 slots, numbered 1-4). These slots are separate from BB character slots; using this command does not affect BB characters. * `$savechar <slot>`: Save your current character data on the server in the specified slot. See the "Server-side saves" section for more details.
* `$loadchar <slot>` (v1 and v2 only): Loads your character data from the specified slot. The changes will be undone if you join a game - to save your changes, disconnect from the lobby. * `$loadchar <slot>`: Save your current character data on the server in the specified slot. See the "Server-side saves" section for more details.
* `$bbchar <username> <password> <slot>`: Use this command when playing on a non-BB version of PSO. If the username and password are correct, this command converts your current character to BB format and saves it on the server in the given slot (1-4). Any character already in that slot is overwritten. (This command is similar to `$savechar`, except it overwrites a BB character slot, and can transfer characters across accounts.) Note that the character's chat data, quick menu config, and bank contents are not copied, since there is no way for the server to request those types of data. * `$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 for more details.
* `$edit <stat> <value>`: Modifies your character data. If you are on V3 (GameCube/Xbox), this command does nothing. If you are on V1 or V2 (DC or PC, not BB), your changes will be undone if you join a game - to save your changes, disconnect from the lobby. If cheats are allowed on the server, `<stat>` can be any of `atp`, `mst`, `evp`, `hp`, `dfp`, `ata`, `lck`, `meseta`, `exp`, `level`, `namecolor`, `secid`, `name`, `npc`, or `tech`. If cheats are not allowed, only `namecolor`, `name`, and `npc` can be used. * `$edit <stat> <value>`: Modify your character data. If you are on V3 (GameCube/Xbox), this command does nothing. If you are on V1 or V2 (DC or PC, not BB), your changes will be undone if you join a game - to save your changes, disconnect from the lobby. If cheats are allowed on the server, `<stat>` can be any of `atp`, `mst`, `evp`, `hp`, `dfp`, `ata`, `lck`, `meseta`, `exp`, `level`, `namecolor`, `secid`, `name`, `language`, `npc`, or `tech`. If cheats are not allowed, only `namecolor`, `name`, `language`, and `npc` can be used. Changing your character's language is only useful on BB; to do so, use a single-character language code (e.g. to switch your character to English, use `$edit language E`; for Japanese, use `$edit language J`).
* Blue Burst player commands (game server only) * Blue Burst player commands (game server only)
* `$bank [number]`: Switches 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, switches back to your current character's bank. * `$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`: Saves 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.) * `$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 (game server only)
* `$maxlevel <level>`: Sets 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.) * `$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>`: Sets the minimum level for players to join the current game. * `$minlevel <level>`: Set the minimum level for players to join the current game.
* `$password <password>`: Sets the game's join password. To unlock the game, run `$password` with nothing after it. * `$password <password>`: Set the game's join password. To unlock the game, run `$password` with nothing after it.
* `$dropmode [mode]`: Changes 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 for more information. * `$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 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 (except items only visible to the leaving player). If the game is empty for too long (15 minutes by default), it is then deleted. There is an edge case with persistence: if the player defeats a boss, leaves the room, joins again, and returns to the boss arena, neither the boss nor the exit warp will spawn, so they will be stuck there and have to use $warp or Quit Game to get out. For this reason, `$warp 0` is allowed in boss arenas once the boss is defeated, even if cheat mode is disabled. * `$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 (except items that were only visible to the leaving player, which are deleted). If the game is empty for too long (15 minutes by default), it is then deleted. There is an edge case with persistence: if the player defeats a boss, leaves the room, joins again, and returns to the boss arena, neither the boss nor the exit warp will appear, so they will be stuck there and have to use $warp or Quit Game to get out. For this reason, `$warp 0` is allowed in boss arenas once the boss is defeated, even if cheat mode is disabled.
* Episode 3 commands (game server only) * Episode 3 commands (game server only)
* `$spec`: Toggles the allow spectators flag for Episode 3 games. If any players are spectating when this flag is disabled, they will be sent back to the lobby. * `$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`: Toggles infinite-time mode. Must be used before starting a battle. If infinite-time mode is enabled, the overall and per-phase time limits will be disabled regardless of the values chosen during battle 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). * `$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]`: Sets 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. * `$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>`: Shows 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`. * `$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`: Causes your team to immediately lose the current battle. * `$surrender`: Cause your team to immediately lose the current battle.
* `$saverec <name>`: Saves the recording of the last battle. * `$saverec <name>`: Save the recording of the last battle.
* `$playrec <name>`: Plays a battle recording. This command creates a spectator team and replays the specified battle log within it. There is a bug in Dolphin that makes use of this command unstable in emulation (see the "Battle records" section above). * `$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).
* Cheat mode commands * Cheat mode commands
* `$cheat` (game server only): Enables or disables 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. * `$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` / `$inftp`: Enables or disables infinite HP or TP mode. Applies to only you. In infinite HP mode, one-hit KO attacks will still kill you. On V1 and V2, infinite HP also automatically cures status ailments. * `$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.
* `$warpme <floor-id>` (or `$warp <floor-id>`): Warps yourself to the given floor. * `$inftp`: Enable or disable infinite TP mode. Applies to only you; does not affect other players.
* `$warpall <floor-id>`: Warps everyone in the game to the given floor. You must be the leader to use this command, unless you're on the proxy server. * `$warpme <floor-id>` (or `$warp <floor-id>`): Warp yourself to the given floor.
* `$next`: Warps yourself to the next 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.
* `$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. * `$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. * `$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): Changes 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. * `$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.
* Configuration commands * Aesthetic commands
* `$event <event>`: Sets 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. * `$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): Sets the current holiday event in all lobbies. * `$allevent <event>` (game server only): Set the current holiday event in all lobbies.
* `$song <song-id>` (Episode 3 only): Plays a specific song in the current lobby. * `$song <song-id>` (Episode 3 only): Play a specific song in the current lobby.
* Administration commands (game server only) * Administration commands (game server only)
* `$ann <message>`: Sends an announcement message. The message is sent as temporary on-screen text to all players in all games and lobbies. * `$ann <message>`: Send an announcement message. The message is sent as temporary on-screen text to all players in all games and lobbies.
* `$ann! <message>`: Sends an announcement message. The message is sent as a Simple Mail message to all players in all games and lobbies. * `$ann! <message>`: Send an announcement message. The message is sent as a Simple Mail message to all players in all games and lobbies.
* `$ax <message>`: Sends a message to the server's terminal. This cannot be used to run server shell commands; it only prints text to stderr. * `$ax <message>`: Send a message to the server's terminal. This cannot be used to run server shell commands; it only prints text to stderr.
* `$silence <identifier>`: Silences a player (remove their ability to chat) or unsilences a player. The identifier may be the player's name or Guild Card number. * `$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.
* `$kick <identifier>`: Disconnects a player. The identifier may be the player's name or Guild Card number. * `$kick <identifier>`: Disconnect a player. The identifier may be the player's name or Guild Card number.
* `$ban <identifier>`: Bans a player. The identifier may be the player's name or Guild Card number. * `$ban <duration> <identifier>`: Ban a player. The duration should be of the form `10m` (minutes), `10h` (hours), `10d` (days), `10w` (weeks), `10M` (months), or `10y` (years). (Numbers other than 10 may be used, of course.) As with `$kick`, the identifier may be the player's name or Guild Card number.
# Non-server features # Non-server features
+3 -4
View File
@@ -1,11 +1,10 @@
## General ## General
- Make reloading happen on separate threads so compression doesn't block active clients
- Implement decrypt/encrypt actions for VMS files - Implement decrypt/encrypt actions for VMS files
- Make UI strings localizable (e.g. entries in menus, welcome message, etc.) - Make UI strings localizable (e.g. entries in menus, welcome message, etc.)
- Add an idle connection timeout for proxy sessions - Add an idle connection timeout for proxy sessions
- Clean up ItemParameterTable implementation (see comment at the top of the class definition) - 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 ) - Handle MeetUserExtensions properly in 41 and C4 commands on the proxy (rewrite the embedded 19 command and store a map of received destinations)
## PSO DC ## PSO DC
@@ -14,7 +13,7 @@
## Episode 3 ## Episode 3
- Enforce tournament deck restrictions (e.g. rank checks, No Assist option) when populating COMs at tournament start time - Enforce tournament deck restrictions (e.g. rank checks, No Assist option) when populating COMs at tournament start time
- Make `reload licenses` not vulnerable to online players' licenses overwriting licenses on disk somehow - Make `reload accounts` not vulnerable to online players' accounts overwriting accounts on disk somehow
- Implement ranks (based on total Meseta earned) - Implement ranks (based on total Meseta earned)
- Make an AR code that gets rid of the SAMPLE overlays on NTE - Make an AR code that gets rid of the SAMPLE overlays on NTE
@@ -22,10 +21,10 @@
- Fix receiving Guild Cards from non-Xbox players - Fix receiving Guild Cards from non-Xbox players
- Research the F94D quest opcode - Research the F94D quest opcode
- Finish porting the remaining GC patches
## PSOBB ## PSOBB
- Test all quest item subcommands
- Figure out why Pouilly Slime EXP doesn't work - Figure out why Pouilly Slime EXP doesn't work
- Make server-specified rare enemies work with maps loaded by the proxy - Make server-specified rare enemies work with maps loaded by the proxy
- Implement serialization for various table types (ItemPMT, ItemPT, etc.) - Implement serialization for various table types (ItemPMT, ItemPT, etc.)
-16
View File
@@ -1,16 +0,0 @@
struct AITalkBin {
be_uint32_t num_scs;
be_uint32_t sc_offsets[num_scs];
struct SCDialogueEntry {
be_uint32_t num_entries;
be_uint32_t unknown_a1;
be_uint32_t size; // in bytes
struct WhenEntry {
be_uint32_t when;
be_uint32_t percent_chance; // 0-100
be_uint32_t count;
be_uint32_t string_ids[count];
} __attribute__((packed));
} __attribute__((packed));
} __attribute__((packed));
+10
View File
@@ -79,6 +79,11 @@ Ep3-US => 042F9AC0 60000000
Ep3-NTE => 040C2C48 60000000 Ep3-NTE => 040C2C48 60000000
Ep3-JP => 042F8B74 60000000 Ep3-JP => 042F8B74 60000000
(Ep1&2 USA v1.1) Change type of all loading screens
0401CA04 3BE0000X
0401CA08 48000038
Values for X: 0 = lobby/game join, 1 = quest load, 3 = pipe up, 4 = pipe down, anything else = silent black screen
(Ep3 USA) Replace loading screen A button sounds with random sounds (Ep3 USA) Replace loading screen A button sounds with random sounds
042F9B18 4804BB19 042F9B18 4804BB19
042F9B1C 5463063E 042F9B1C 5463063E
@@ -350,3 +355,8 @@ Note: Without a TextEnglish.pr2/pr3 patch, the menu items for these sounds will
0408E448 38000001 0408E448 38000001
0408E44C 900DA62C 0408E44C 900DA62C
0408E450 4E800020 0408E450 4E800020
(v1.1 USA) Replace all sound effects with specified sound effect
042256E4 3F40XXXX
042256E8 635AYYYY
042256EC 4800000C
+602 -602
View File
File diff suppressed because it is too large Load Diff
+41 -11
View File
@@ -73,9 +73,9 @@ ItemLossPrevention
*** desc=Don't lose items if\nyou don't log off\nnormally *** desc=Don't lose items if\nyou don't log off\nnormally
JP12------------- JP13------------- JP14------------- JP15------------- US10------------- US11------------- US12------------- EU--------------- DISASSEMBLY (US10) JP12------------- JP13------------- JP14------------- JP15------------- US10------------- US11------------- US12------------- EU--------------- DISASSEMBLY (US10)
801D33E4 4800004C 801D38EC 4800004C 801D3CC4 4800004C 801D39B8 4800004C 801D381C 4800004C 801D381C 4800004C 801D3A1C 4800004C 801D3ED8 4800004C b +0x0000004C /* 801D3868 */ 801D33E4 4800004C 801D38EC 4800004C 801D3CC4 4800004C 801D39B8 4800004C 801D381C 4800004C 801D381C 4800004C 801D3A1C 4800004C 801D3ED8 4800004C b +0x0000004C /* 801D3868 */
8020010C 60000000 801FF710 60000000 801FF0FC 60000000 801FF0FC 60000000 801FFA44 60000000 801FF9E0 60000000 nop 801FE900 60000000 801FF174 60000000 8020010C 60000000 801FF710 60000000 801FF0FC 60000000 801FF0FC 60000000 801FFA44 60000000 801FF9E0 60000000 nop
802016CC 60000000 80200C9C 60000000 80200658 60000000 80200658 60000000 80200FD0 60000000 80200F3C 60000000 nop 801FFE5C 60000000 802006D0 60000000 802016CC 60000000 80200C9C 60000000 80200658 60000000 80200658 60000000 80200FD0 60000000 80200F3C 60000000 nop
801FD944 38000000 80202860 38000000 802021C4 38000000 802021C4 38000000 80202B94 38000000 80202AA8 38000000 li r0, 0x0000 802019C8 38000000 8020223C 38000000 801FD944 38000000 80202860 38000000 802021C4 38000000 802021C4 38000000 80202B94 38000000 80202AA8 38000000 li r0, 0x0000
802C2060 4800004C 802C2F98 4800004C 802C42E4 4800004C 802C3E78 4800004C 802C2A40 4800004C 802C2A84 4800004C 802C402C 4800004C 802C37C0 4800004C b +0x0000004C /* 802C2A8C */ 802C2060 4800004C 802C2F98 4800004C 802C42E4 4800004C 802C3E78 4800004C 802C2A40 4800004C 802C2A84 4800004C 802C402C 4800004C 802C37C0 4800004C b +0x0000004C /* 802C2A8C */
802D0AA0 48000020 802D1A58 48000020 802D2C10 48000020 802D2938 48000020 802D1480 48000020 802D14C4 48000020 802D2AEC 48000020 802D2280 48000020 b +0x00000020 /* 802D14A0 */ 802D0AA0 48000020 802D1A58 48000020 802D2C10 48000020 802D2938 48000020 802D1480 48000020 802D14C4 48000020 802D2AEC 48000020 802D2280 48000020 b +0x00000020 /* 802D14A0 */
@@ -754,15 +754,45 @@ Show Enemy HP Bars
EnemyHPBars EnemyHPBars
*** name=Enemy HP bars *** name=Enemy HP bars
*** desc=Show HP bars in\nenemy info windows *** desc=Show HP bars in\nenemy info windows
JP12------------- JP13------------- JP14------------- JP15------------- US10------------- US11------------- US12------------- EU--------------- DISASSEMBLY (US10) JP12------------- JP13------------- JP14------------- JP15------------- US10------------- US11------------- US12------------- EU--------------- DISASSEMBLY (US12)
802612C4 4BFE1541 80261E9C 4BFE1349 80262EE4 4BFE0665 80262C98 4BFE1241 80261B9C 4BFE1545 80261B9C 4BFE1545 80262F5C 4BFE12B1 802627A4 4BFE12B1 bl -0x0001EABC /* 802430E0 */ 802612C4 4BFE1541 80261E9C 4BFE1349 80262EE4 4BFE0665 80262C98 4BFE1241 80261B9C 4BFE1545 80261B9C 4BFE1545 80262F5C 4BFE12B1 802627A4 4BFE12B1 bl -0x0001EABC /* 802430E0 */
804CAF00 42300000 804CE650 42300000 804D0BA0 42300000 804D0940 42300000 804CB6D0 42300000 804CBBB0 42300000 804D0218 42300000 804D0608 42300000 bdnz cr4, +0x00000000 /* 804CB6D0 */ 804CAF00 42780000 804CE650 42780000 804D0BA0 42780000 804D0940 42780000 804CB6D0 42780000 804CBBB0 42780000 804D0218 42780000 804D0608 42780000
804CAF1C FF00FF15 804CE66C FF00FF15 804D0BBC FF00FF15 804D095C FF00FF15 804CB6EC FF00FF15 804CBBCC FF00FF15 804D0234 FF00FF15 804D0624 FF00FF15 .invalid FC, 0 804CAF1C FF00FF15 804CE66C FF00FF15 804D0BBC FF00FF15 804D095C FF00FF15 804CB6EC FF00FF15 804CBBCC FF00FF15 804D0234 FF00FF15 804D0624 FF00FF15
805CBFBC 42A00000 805D65BC 42A00000 805DDA5C 42A00000 805DD7FC 42A00000 805CC8C4 42A00000 805D38E4 42A00000 805DD104 42A00000 805D9344 42A00000 b +0x00000000 /* 805CC8C4 */ 805CBFBC 42C00000 805D65BC 42C00000 805DDA5C 42C00000 805DD7FC 42C00000 805CC8C4 42C00000 805D38E4 42C00000 805DD104 42C00000 805D9344 42C00000
804CAE40 42640000 804CE590 42640000 804D0AE0 42640000 804D0880 42640000 804CB610 42640000 804CBAF0 42640000 804D0158 42640000 804D0548 42640000 bc 19, 4, +0x00000000 /* 804CB610 */ 804CAE40 42960000 804CE590 42960000 804D0AE0 42960000 804D0880 42960000 804CB610 42960000 804CBAF0 42960000 804D0158 42960000 804D0548 42960000
804CAE4C 42640000 804CE59C 42640000 804D0AEC 42640000 804D088C 42640000 804CB61C 42640000 804CBAFC 42640000 804D0164 42640000 804D0554 42640000 bc 19, 4, +0x00000000 /* 804CB61C */ 804CAE4C 42960000 804CE59C 42960000 804D0AEC 42960000 804D088C 42960000 804CB61C 42960000 804CBAFC 42960000 804D0164 42960000 804D0554 42960000
804CAE58 42640000 804CE5A8 42640000 804D0AF8 42640000 804D0898 42640000 804CB628 42640000 804CBB08 42640000 804D0170 42640000 804D0560 42640000 bc 19, 4, +0x00000000 /* 804CB628 */ 804CAE58 42960000 804CE5A8 42960000 804D0AF8 42960000 804D0898 42960000 804CB628 42960000 804CBB08 42960000 804D0170 42960000 804D0560 42960000
804CAE64 42640000 804CE5B4 42640000 804D0B04 42640000 804D08A4 42640000 804CB634 42640000 804CBB14 42640000 804D017C 42640000 804D056C 42640000 bc 19, 4, +0x00000000 /* 804CB634 */ 804CAE64 42960000 804CE5B4 42960000 804D0B04 42960000 804D08A4 42960000 804CB634 42960000 804CBB14 42960000 804D017C 42960000 804D056C 42960000
804CAE70 42960000 804CE5C0 42960000 804D0B10 42960000 804D08B0 42960000 804CB640 42960000 804CBB20 42960000 804D0188 42960000 804D0578 42960000
80261260 4BDAA3F1 80261E38 4BDA9819 80262E80 4BDA87D1 80262C34 4BDA8A1D 80261B38 4BDA9B19 80261B38 4BDA9B19 80262EF8 4BDA8759 80262740 4BDA8F11 bl -0x002578A8 /* 8000B650 */
80261420 4BDAA245 80261FF8 4BDA966D 80263040 4BDA8625 80262DF4 4BDA8871 80261CF8 4BDA996D 80261CF8 4BDA996D 802630B8 4BDA85AD 80262900 4BDA8D65 bl -0x00257A54 /* 8000B664 */
8000B650 3CA08001 8000B650 3CA08001 8000B650 3CA08001 8000B650 3CA08001 8000B650 3CA08001 8000B650 3CA08001 8000B650 3CA08001 8000B650 3CA08001 lis r5, 0x8001
8000B654 8065B6BC 8000B654 8065B6BC 8000B654 8065B6BC 8000B654 8065B6BC 8000B654 8065B6BC 8000B654 8065B6BC 8000B654 8065B6BC 8000B654 8065B6BC lwz r3, [r5 - 0x4944]
8000B658 7FFEFB78 8000B658 7FFEFB78 8000B658 7FFEFB78 8000B658 7FFEFB78 8000B658 7FFEFB78 8000B658 7FFEFB78 8000B658 7FFEFB78 8000B658 7FFEFB78 mr r30, r31
8000B65C A8DE032C 8000B65C A8DE032C 8000B65C A8DE032C 8000B65C A8DE032C 8000B65C A8DE032C 8000B65C A8DE032C 8000B65C A8DE032C 8000B65C A8DE032C lha r6, [r30 + 0x032C]
8000B660 48000010 8000B660 48000010 8000B660 48000010 8000B660 48000010 8000B660 48000010 8000B660 48000010 8000B660 48000010 8000B660 48000010 b +0x00000010 /* 8000B670 */
8000B664 A8DE02B8 8000B664 A8DE02B8 8000B664 A8DE02B8 8000B664 A8DE02B8 8000B664 A8DE02B8 8000B664 A8DE02B8 8000B664 A8DE02B8 8000B664 A8DE02B8 lha r6, [r30 + 0x02B8]
8000B668 3CA08001 8000B668 3CA08001 8000B668 3CA08001 8000B668 3CA08001 8000B668 3CA08001 8000B668 3CA08001 8000B668 3CA08001 8000B668 3CA08001 lis r5, 0x8001
8000B66C 9065B6BC 8000B66C 9065B6BC 8000B66C 9065B6BC 8000B66C 9065B6BC 8000B66C 9065B6BC 8000B66C 9065B6BC 8000B66C 9065B6BC 8000B66C 9065B6BC stw [r5 - 0x4944], r3
8000B670 7C0802A6 8000B670 7C0802A6 8000B670 7C0802A6 8000B670 7C0802A6 8000B670 7C0802A6 8000B670 7C0802A6 8000B670 7C0802A6 8000B670 7C0802A6 mflr r0
8000B674 9005B6C0 8000B674 9005B6C0 8000B674 9005B6C0 8000B674 9005B6C0 8000B674 9005B6C0 8000B674 9005B6C0 8000B674 9005B6C0 8000B674 9005B6C0 stw [r5 - 0x4940], r0
8000B678 7C651B78 8000B678 7C651B78 8000B678 7C651B78 8000B678 7C651B78 8000B678 7C651B78 8000B678 7C651B78 8000B678 7C651B78 8000B678 7C651B78 mr r5, r3
8000B67C A8FE02B8 8000B67C A8FE02B8 8000B67C A8FE02B8 8000B67C A8FE02B8 8000B67C A8FE02B8 8000B67C A8FE02B8 8000B67C A8FE02B8 8000B67C A8FE02B8 lha r7, [r30 + 0x02B8]
8000B680 3C808000 8000B680 3C808000 8000B680 3C808000 8000B680 3C808000 8000B680 3C808000 8000B680 3C808000 8000B680 3C808000 8000B680 3C808000 lis r4, 0x8000
8000B684 6084B6AC 8000B684 6084B6AC 8000B684 6084B6AC 8000B684 6084B6AC 8000B684 6084B6AC 8000B684 6084B6AC 8000B684 6084B6AC 8000B684 6084B6AC ori r4, r4, 0xB6AC
8000B688 38640018 8000B688 38640018 8000B688 38640018 8000B688 38640018 8000B688 38640018 8000B688 38640018 8000B688 38640018 8000B688 38640018 addi r3, r4, 0x0018
8000B68C 4CC63182 8000B68C 4CC63182 8000B68C 4CC63182 8000B68C 4CC63182 8000B68C 4CC63182 8000B68C 4CC63182 8000B68C 4CC63182 8000B68C 4CC63182 crxor crb6, crb6, crb6
8000B690 4838A86D 8000B690 4838D275 8000B690 4838F115 8000B690 4838EEC5 8000B690 4838BB3D 8000B690 4838BB95 8000B690 4838F295 8000B690 4838DD85 bl sprintf /* 8039A924 */
8000B694 3C808000 8000B694 3C808000 8000B694 3C808000 8000B694 3C808000 8000B694 3C808000 8000B694 3C808000 8000B694 3C808000 8000B694 3C808000 lis r4, 0x8000
8000B698 6084B6C4 8000B698 6084B6C4 8000B698 6084B6C4 8000B698 6084B6C4 8000B698 6084B6C4 8000B698 6084B6C4 8000B698 6084B6C4 8000B698 6084B6C4 ori r4, r4, 0xB6C4
8000B69C 7F83E378 8000B69C 7F83E378 8000B69C 7F83E378 8000B69C 7F83E378 8000B69C 7F83E378 8000B69C 7F83E378 8000B69C 7F83E378 8000B69C 7F83E378 mr r3, r28
8000B6A0 8004FFFC 8000B6A0 8004FFFC 8000B6A0 8004FFFC 8000B6A0 8004FFFC 8000B6A0 8004FFFC 8000B6A0 8004FFFC 8000B6A0 8004FFFC 8000B6A0 8004FFFC lwz r0, [r4 - 0x0004]
8000B6A4 7C0803A6 8000B6A4 7C0803A6 8000B6A4 7C0803A6 8000B6A4 7C0803A6 8000B6A4 7C0803A6 8000B6A4 7C0803A6 8000B6A4 7C0803A6 8000B6A4 7C0803A6 mtlr r0
8000B6A8 4E800020 8000B6A8 4E800020 8000B6A8 4E800020 8000B6A8 4E800020 8000B6A8 4E800020 8000B6A8 4E800020 8000B6A8 4E800020 8000B6A8 4E800020 blr
8000B6AC 25730A0A 8000B6AC 25730A0A 8000B6AC 25730A0A 8000B6AC 25730A0A 8000B6AC 25730A0A 8000B6AC 25730A0A 8000B6AC 25730A0A 8000B6AC 25730A0A .invalid
8000B6B0 48503A25 8000B6B0 48503A25 8000B6B0 48503A25 8000B6B0 48503A25 8000B6B0 48503A25 8000B6B0 48503A25 8000B6B0 48503A25 8000B6B0 48503A25 bl +0x00503A24 /* 8050F0D4 */
8000B6B4 642F2564 8000B6B4 642F2564 8000B6B4 642F2564 8000B6B4 642F2564 8000B6B4 642F2564 8000B6B4 642F2564 8000B6B4 642F2564 8000B6B4 642F2564 oris r15, r1, 0x2564
8000B6B8 00000000 8000B6B8 00000000 8000B6B8 00000000 8000B6B8 00000000 8000B6B8 00000000 8000B6B8 00000000 8000B6B8 00000000 8000B6B8 00000000 .invalid
PSO DC Reticle Colours PSO DC Reticle Colours
DCReticleColors DCReticleColors
-2785
View File
File diff suppressed because it is too large Load Diff
@@ -92,5 +92,3 @@ You'll win someday!
In case you want to try again, In case you want to try again,
come back to me once more. come back to me once more.
Binary file not shown.
Binary file not shown.
Binary file not shown.
View File
Binary file not shown.
View File
File diff suppressed because it is too large Load Diff
-11
View File
@@ -1,11 +0,0 @@
star value tables
psobb [B1-437]
00010203 04090909 01020304 05090909 01020304 05090909 01020304 05090909 01020304 05090909 00010203 04090909 01020304 05090909 01020304 05090909 01020304 05090909 00010203 09090901 02030409 09090102 03040909 09090A0A 090A0A09 0A0A090C 0B0A0A0A 0A0A0A0B 0A090A0A 0A0A0A09 0A0A0A0A 0A0A0A0A 0A0A0B0A 0C0C0B0A 0A090A09 090A0A0A 0A0C090C 0B0A090A 090C0A0B 0A0A0A0A 0A0A0A0B 0B0A0A0A 09090A09 0C0A0A0A 0B0A0B09 0A0A090A 0A0B090B 0A0B0B0A 090A090A 0B090A0A 0A0A0A0A 0A0A0A09 090C0C0C 0C0C0C0C 0C0C0C0C 0C0C0C0C 0C0C0C0C 0C0C0C0C 0C0C0009 0A0A0A0B 090A0A09 0A0A0B0A 0A0A0A0A 0A0A0A0A 0A0A0A0A 0A0A0A0A 0A0A0A0A 0B0B0B0B 0B0B0B0B 0B0B0B0A 0C0A0C0B 0A0A0A0A 0A0B0A0B 0B0B0B0B 0A0A090A 0A0A090B 0B0B0B0C 0C0C0C0C 0A0A0C0A 090A0C09 0A0B0A0A 0A0A0C0A 0A0A0A09 0A0C0A09 0A0A0A0A 0A090C0B 09090909 09090909 09090909 09090909 0909090B 0A0C0A0B 0B0C0A0A 0A090A0A 0A0A0B0A 0A0A0A0A 0909090A 0A090C0A 0C0C0C0C 0C0C0C0C 0C0C0C0C 0C0C0C0C 0C0C0C0C 0C0C0C0C 0C0C0C0C 0C0C0102 03040102 03040203 04020304 01020304 01020304 01020304 01020304 01020304 01020304 03040000 00010102 02030304 04050506 06070707 07080808 08080809 09090A0A 0A0A0A0A 0B0C0A0A 0A0A0A0A 0B0B0B0A 0B0B0C0B 0B0B0B0B 0A0A0A0A 0A0A0C09 0909090A 0A0B0C09 0B0A0A0A 0A0A0A0A 0A0A0A0B 0A0A0A0A 0A0A0A00 00010203 03040405 05050606 07070808 08080808 0A0A0A0A 0A0A0909 090A0A0A 0A0A0A0A 0A0A0B0A 0A0B0A09 0909090A 0B0B0000 0B000000 00080808 08080808 09080808 09080808 09070707 07070909 090C0909 09090909 09090909 09090909 09090909 09090909 09090909 09090909 09090909 09090909 09090909 09090909 09090909 0A0A0B0A 0B0A0909 0B0B0B0C 0A0A0A09 0A0A0A0A 090A0A0A 0A0A0A0A 0A0A0A0A 0203050B 0203050B 0203050B 0203050B 0204060B 0204060B 0203050B 080B080A 0B020305 02030502 03050304 06030405 07080B04 06090406 09040609 06090B06 090B0909 09090909 0A0B0B0B 0B0B0B0B 0B0B0B0B 0B0B0B0B 0B0B0B0B 0A0B0B0B 0B0B0B0B
psogc [94-2F7]
00010203 04090909 01020304 05090909 01020304 05090909 01020304 05090909 01020304 05090909 00010203 04090909 01020304 05090909 01020304 05090909 01020304 05090909 00010203 09090901 02030409 09090102 03040909 09090A0A 090A0A09 0A0A090C 0B0A0A0A 0A0A0A0B 0A090A0A 0A0A0A09 0A0A0A0A 0A0A0A0A 0A0A0B0A 0C0C0B0A 0A090A09 090A0A0A 0A0C090C 0B0A090A 090C0A0B 0A0A0A0A 0A0A0A0B 0B0A0A0A 09090A09 0C0A0A0A 0B0A0B09 0A0A090A 0A0B090B 0A0B0B0A 090A090A 0B090A0A 0A0A0A0A 0A0A0A09 090C0C0C 0C0C0C0C 0C0C0C0C 0C0C0C0C 0C0C0C0C 0C0C0C0C 0C0C0009 0A0A0A0B 090A0A09 0A0A0B0A 0A0A0A0A 0A0A0A0A 0A0A0A0A 0A0A0A0A 0A0A0A0A 0B0B0B0B 0B0B0B0B 0B0B0B0A 0C0A0C0B 0A0A0A0A 0A0B0A0B 0B0B0B0B 0A0A090A 0A0A090B 0B0B0B0C 0C0C0C0C 01020304 01020304 02030402 03040102 03040102 03040102 03040102 03040102 03040102 03040304 00000001 01020203 03040405 05060607 07070708 08080808 08090909 0A0A0A0A 0A0A0B0C 0A0A0A0A 0A0A0B0B 0B0A0B0B 0C0B0B0B 0B0B0000 01020303 04040505 05060607 07080808 0808080A 0A0A0A0A 0A090909 0A0A0A0A 0A0A0A0A 0A0B0A0A 0B0A0909 09090A0B 0B000000 00000000 08080808 08080809 08080809 08080809 07070707 07090909 0C090909 09090909 09090909 09090909 09090909 09090909 09090909 09090909 09090909 09090909 09090909 09090909 09090902 03050B02 03050B02 03050B02 03050B02 04060B02 04060B02 03050B08 0B080A0B 02030502 03050203 05030406 03040507 080B0406 09040609 04060906 090B0609 0B090909 090909
0203050B0203050B0203050B0203050B
+4 -2
View File
@@ -7,6 +7,8 @@
#include <phosg/Filesystem.hh> #include <phosg/Filesystem.hh>
#include <phosg/Strings.hh> #include <phosg/Strings.hh>
#include "Text.hh"
using namespace std; using namespace std;
AFSArchive::AFSArchive(shared_ptr<const string> data) AFSArchive::AFSArchive(shared_ptr<const string> data)
@@ -14,12 +16,12 @@ AFSArchive::AFSArchive(shared_ptr<const string> data)
struct FileHeader { struct FileHeader {
be_uint32_t magic; be_uint32_t magic;
le_uint32_t num_files; le_uint32_t num_files;
} __attribute__((packed)); } __packed_ws__(FileHeader, 8);
struct FileEntry { struct FileEntry {
le_uint32_t offset; le_uint32_t offset;
le_uint32_t size; le_uint32_t size;
} __attribute__((packed)); } __packed_ws__(FileEntry, 8);
StringReader r(*this->data); StringReader r(*this->data);
const auto& header = r.get<FileHeader>(); const auto& header = r.get<FileHeader>();
-18
View File
@@ -1,18 +0,0 @@
#pragma once
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
inline void run_ar_code_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 void run_xbe_patch_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<std::pair<uint32_t, std::string>> diff_dol_files(const std::string&, const std::string&) {
throw std::runtime_error("resource_file is not available; install it and rebuild newserv");
}
-640
View File
@@ -1,640 +0,0 @@
#include "ARCodeTranslator.hh"
#include <array>
#include <future>
#include <phosg/Filesystem.hh>
#include <phosg/Strings.hh>
#include <resource_file/ExecutableFormats/DOLFile.hh>
#include <resource_file/ExecutableFormats/XBEFile.hh>
using namespace std;
class ARCodeTranslator {
public:
enum class ExpandMethod {
FORWARD = 0,
FORWARD_WITH_BARRIER,
BACKWARD,
BACKWARD_WITH_BARRIER,
BOTH,
BOTH_WITH_BARRIER,
BOTH_IGNORE_ORIGIN,
};
static const char* name_for_expand_method(ExpandMethod method) {
switch (method) {
case ExpandMethod::FORWARD:
return "FORWARD";
case ExpandMethod::FORWARD_WITH_BARRIER:
return "FORWARD_WITH_BARRIER";
case ExpandMethod::BACKWARD:
return "BACKWARD";
case ExpandMethod::BACKWARD_WITH_BARRIER:
return "BACKWARD_WITH_BARRIER";
case ExpandMethod::BOTH:
return "BOTH";
case ExpandMethod::BOTH_WITH_BARRIER:
return "BOTH_WITH_BARRIER";
case ExpandMethod::BOTH_IGNORE_ORIGIN:
return "BOTH_IGNORE_ORIGIN";
default:
throw logic_error("invalid expand method");
}
}
ARCodeTranslator(const string& directory)
: log("[ar-trans] "),
directory(directory) {
while (ends_with(this->directory, "/")) {
this->directory.pop_back();
}
for (const auto& filename : list_directory(this->directory)) {
if (ends_with(filename, ".dol")) {
string name = filename.substr(0, filename.size() - 4);
string path = directory + "/" + filename;
this->files.emplace(name, make_shared<DOLFile>(path.c_str()));
this->log.info("Loaded %s", name.c_str());
}
}
}
~ARCodeTranslator() = default;
const string& get_source_filename() const {
return this->src_filename;
}
void set_source_file(const string& filename) {
this->src_filename = filename;
this->src_file = this->files.at(this->src_filename);
}
void find_rtoc_global_regs() const {
for (const auto& it : this->files) {
bool r2_high_found = false;
bool r2_low_found = false;
bool r13_high_found = false;
bool r13_low_found = false;
uint32_t r2 = 0;
uint32_t r13 = 0;
for (const auto& section : it.second->sections) {
if (!section.is_text) {
continue;
}
StringReader r(section.data);
while (!r.eof() && r.where()) {
uint32_t opcode = r.get_u32b();
if ((opcode & 0xFFFF0000) == 0x3DA00000) {
if (r13_high_found) {
throw runtime_error("multiple values for r13_high");
}
r13_high_found = true;
r13 |= (opcode << 16);
} else if ((opcode & 0xFFFF0000) == 0x3C400000) {
if (r2_high_found) {
throw runtime_error("multiple values for r2_high");
}
r2_high_found = true;
r2 |= (opcode << 16);
} else if ((opcode & 0xFFFF0000) == 0x61AD0000) {
if (r13_low_found) {
throw runtime_error("multiple values for r13_low");
}
r13_low_found = true;
r13 |= (opcode & 0xFFFF);
} else if ((opcode & 0xFFFF0000) == 0x60420000) {
if (r2_low_found) {
throw runtime_error("multiple values for r2_low");
}
r2_low_found = true;
r2 |= (opcode & 0xFFFF);
}
}
}
if (r2_low_found && r2_high_found) {
fprintf(stderr, "(%s) r2 = %08" PRIX32 "\n", it.first.c_str(), r2);
} else {
fprintf(stderr, "(%s) r2 = __MISSING__\n", it.first.c_str());
}
if (r13_low_found && r13_high_found) {
fprintf(stderr, "(%s) r13 = %08" PRIX32 "\n", it.first.c_str(), r13);
} else {
fprintf(stderr, "(%s) r13 = __MISSING__\n", it.first.c_str());
}
}
}
uint32_t find_match(shared_ptr<const DOLFile> dest_file, uint32_t src_address, ExpandMethod expand_method) const {
if (!this->src_file) {
throw runtime_error("no source file selected");
}
const DOLFile::Section* src_section = nullptr;
for (const auto& sec : this->src_file->sections) {
if (src_address >= sec.address && src_address < sec.address + sec.data.size()) {
src_section = &sec;
break;
}
}
if (!src_section) {
throw runtime_error("source address not within any section");
}
const char* method_token = this->name_for_expand_method(expand_method);
size_t src_offset = src_address - src_section->address;
size_t src_bytes_available_before = src_offset;
size_t src_bytes_available_after = src_section->data.size() - src_offset - 4;
this->log.info("(find_match/%s) Source offset = %08zX with %zX/%zX bytes available before/after",
method_token, src_offset, src_bytes_available_before, src_bytes_available_after);
size_t match_bytes_before = 0;
size_t match_bytes_after = 0;
while (match_bytes_before + match_bytes_after + 4 < 0x100) {
size_t num_matches = 0;
size_t last_match_address = 0;
size_t match_length = match_bytes_before + match_bytes_after + 4;
StringReader src_r(src_section->data.data() + src_offset - match_bytes_before, match_length);
for (const auto& dest_section : dest_file->sections) {
for (size_t dest_match_offset = 0;
dest_match_offset < dest_section.data.size();
dest_match_offset += 4) {
src_r.go(0);
StringReader dest_r(dest_section.data.data() + dest_match_offset, match_length);
size_t z;
for (z = 0; z < match_length; z += 4) {
if (expand_method == ExpandMethod::BOTH_IGNORE_ORIGIN && z == match_bytes_before) {
src_r.skip(4);
dest_r.skip(4);
} else if (src_section->is_text) {
uint32_t src_opcode = src_r.get_u32b();
uint32_t dest_opcode = dest_r.get_u32b();
uint32_t src_class = src_opcode & 0xFC000000;
if (src_class != (dest_opcode & 0xFC000000)) {
break;
}
if (src_class == 0x48000000) {
// b +-offset
src_opcode &= 0xFC000003;
dest_opcode &= 0xFC000003;
} else if (((src_opcode & 0xAC1F0000) == 0x800D0000) || ((src_opcode & 0xAC1F0000) == 0x80020000)) {
// lwz/lfs rXX/fXX, [r2/r13 +- offset] OR stw/stfs [r2/r13 +- offset], rXX/fXX
src_opcode &= 0xFFFF0000;
dest_opcode &= 0xFFFF0000;
}
if (src_opcode != dest_opcode) {
break;
}
} else {
uint32_t src_data = src_r.get_u32b();
uint32_t dest_data = dest_r.get_u32b();
if ((src_data & 0xFE000000) == 0x80000000) {
src_data &= 0xFE000003;
}
if ((dest_data & 0xFE000000) == 0x80000000) {
dest_data &= 0xFE000003;
}
if (src_data != dest_data) {
break;
}
}
}
if (z == match_length) {
num_matches++;
last_match_address = dest_section.address + dest_match_offset + match_bytes_before;
}
}
}
this->log.info("(find_match/%s) For match length %zX, %zu matches found", method_token, match_length, num_matches);
if (num_matches == 1) {
return last_match_address;
} else if (num_matches == 0) {
throw runtime_error("did not find exactly one match");
}
bool can_expand_backward = false;
bool can_expand_forward = false;
switch (expand_method) {
case ExpandMethod::BACKWARD_WITH_BARRIER:
can_expand_backward = (src_r.pget_u32b(0) != 0x4E800020) &&
(src_bytes_available_before >= match_bytes_before + 4);
break;
case ExpandMethod::BACKWARD:
can_expand_backward = (src_bytes_available_before >= match_bytes_before + 4);
break;
case ExpandMethod::FORWARD_WITH_BARRIER:
can_expand_forward = (src_r.pget_u32b(src_r.size() - 4) != 0x4E800020) &&
(src_bytes_available_after >= match_bytes_after + 4);
break;
case ExpandMethod::FORWARD:
can_expand_forward = (src_bytes_available_after >= match_bytes_after + 4);
break;
case ExpandMethod::BOTH_WITH_BARRIER:
case ExpandMethod::BOTH_IGNORE_ORIGIN:
can_expand_backward = (src_r.pget_u32b(0) != 0x4E800020) &&
(src_bytes_available_before >= match_bytes_before + 4);
can_expand_forward = (src_r.pget_u32b(src_r.size() - 4) != 0x4E800020) &&
(src_bytes_available_after >= match_bytes_after + 4);
break;
case ExpandMethod::BOTH:
can_expand_backward = (src_bytes_available_before >= match_bytes_before + 4);
can_expand_forward = (src_bytes_available_after >= match_bytes_after + 4);
break;
default:
throw logic_error("invalid expand method");
}
if (!can_expand_backward && !can_expand_forward) {
throw runtime_error("no further expansion is allowed");
}
if (can_expand_backward) {
match_bytes_before += 4;
}
if (can_expand_forward) {
match_bytes_after += 4;
}
}
throw runtime_error("scan field too long; too many matches");
}
void find_all_matches(uint32_t src_addr) const {
if (!this->src_file) {
throw runtime_error("no source file selected");
}
unordered_map<string, uint32_t> results;
for (const auto& it : this->files) {
if (it.second == this->src_file) {
log.info("(%s) %08" PRIX32 " (from source)", it.first.c_str(), src_addr);
results.emplace(it.first, src_addr);
} else {
array<future<uint32_t>, 7> futures;
static const array<ExpandMethod, 7> methods = {
ExpandMethod::FORWARD,
ExpandMethod::FORWARD_WITH_BARRIER,
ExpandMethod::BACKWARD,
ExpandMethod::BACKWARD_WITH_BARRIER,
ExpandMethod::BOTH,
ExpandMethod::BOTH_WITH_BARRIER,
ExpandMethod::BOTH_IGNORE_ORIGIN,
};
for (size_t z = 0; z < methods.size(); z++) {
futures[z] = async(&ARCodeTranslator::find_match, this, it.second, src_addr, methods[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]);
try {
uint32_t ret = futures[z].get();
log.info("(%s) (%s) %08" PRIX32, it.first.c_str(), 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());
}
}
if (match_addrs.empty()) {
log.error("(%s) no match found", it.first.c_str());
} else if (match_addrs.size() > 1) {
log.error("(%s) different matches found by different methods", it.first.c_str());
} 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);
}
}
void handle_command(const string& command) {
auto tokens = split(command, ' ');
if (tokens.empty()) {
throw runtime_error("no command given");
}
strip_trailing_whitespace(tokens[tokens.size() - 1]);
if (tokens[0] == "use") {
this->set_source_file(tokens.at(1));
} else if (tokens[0] == "match") {
this->find_all_matches(stoul(tokens.at(1), nullptr, 16));
} else if (tokens[0] == "find-globals") {
this->find_rtoc_global_regs();
} else if (!tokens[0].empty()) {
throw runtime_error("unknown command");
}
}
void run_shell() {
while (!feof(stdin)) {
if (!this->src_filename.empty()) {
fprintf(stdout, "ar-trans:%s/%s> ", this->directory.c_str(), this->src_filename.c_str());
} else {
fprintf(stdout, "ar-trans:%s> ", this->directory.c_str());
}
fflush(stdout);
string command = fgets(stdin);
try {
this->handle_command(command);
} catch (const exception& e) {
this->log.error("Failed: %s", e.what());
}
}
fputc('\n', stdout);
}
private:
PrefixedLogger log;
string directory;
unordered_map<string, shared_ptr<const DOLFile>> files;
string src_filename;
shared_ptr<const DOLFile> src_file;
};
class XBEPatchTranslator {
public:
enum class ExpandMethod {
FORWARD = 0,
BACKWARD,
BOTH,
};
static const char* name_for_expand_method(ExpandMethod method) {
switch (method) {
case ExpandMethod::FORWARD:
return "FORWARD";
case ExpandMethod::BACKWARD:
return "BACKWARD";
case ExpandMethod::BOTH:
return "BOTH";
default:
throw logic_error("invalid expand method");
}
}
XBEPatchTranslator(const string& directory)
: log("[xbe-trans] "),
directory(directory) {
while (ends_with(this->directory, "/")) {
this->directory.pop_back();
}
for (const auto& filename : list_directory(this->directory)) {
if (ends_with(filename, ".xbe")) {
string name = filename.substr(0, filename.size() - 4);
string path = directory + "/" + filename;
this->files.emplace(name, make_shared<XBEFile>(path.c_str()));
this->log.info("Loaded %s", name.c_str());
}
}
}
~XBEPatchTranslator() = default;
const string& get_source_filename() const {
return this->src_filename;
}
void set_source_file(const string& filename) {
this->src_filename = filename;
this->src_file = this->files.at(this->src_filename);
}
uint32_t find_match(
shared_ptr<const XBEFile> dest_file,
uint32_t src_address,
uint32_t src_size,
ExpandMethod expand_method) const {
if (!this->src_file) {
throw runtime_error("no source file selected");
}
const XBEFile::Section* src_section = nullptr;
for (const auto& sec : this->src_file->sections) {
if (src_address >= sec.addr && src_address < sec.addr + sec.file_size) {
src_section = &sec;
break;
}
}
if (!src_section) {
throw runtime_error("source address not within any section");
}
const char* method_token = this->name_for_expand_method(expand_method);
size_t src_offset = src_address - src_section->addr;
size_t src_bytes_available_before = src_offset;
size_t src_bytes_available_after = src_section->file_size - src_offset - src_size;
this->log.info("(find_match/%s) Source offset = %08zX with %zX/%zX bytes available before/after",
method_token, src_offset, src_bytes_available_before, src_bytes_available_after);
size_t match_bytes_before = 0;
size_t match_bytes_after = 0;
while (match_bytes_before + match_bytes_after + src_size < 0x100) {
size_t num_matches = 0;
size_t last_match_address = 0;
size_t match_length = match_bytes_before + match_bytes_after + src_size;
auto src_r = this->src_file->read_from_addr(src_section->addr + src_offset - match_bytes_before, match_length);
for (const auto& dest_section : dest_file->sections) {
for (size_t dest_match_offset = 0; dest_match_offset + match_length <= dest_section.file_size; dest_match_offset++) {
src_r.go(0);
StringReader dest_r = dest_file->read_from_addr(dest_section.addr + dest_match_offset, match_length);
size_t z;
for (z = 0; z < match_length; z++) {
uint8_t src_data = src_r.get_u8();
uint8_t dest_data = dest_r.get_u8();
if (src_data != dest_data) {
break;
}
}
if (z == match_length) {
num_matches++;
last_match_address = dest_section.addr + dest_match_offset + match_bytes_before;
}
}
}
this->log.info("(find_match/%s) For match length %zX, %zu matches found", method_token, match_length, num_matches);
if (num_matches == 1) {
return last_match_address;
} else if (num_matches == 0) {
throw runtime_error("did not find exactly one match");
}
bool can_expand_backward = false;
bool can_expand_forward = false;
switch (expand_method) {
case ExpandMethod::BACKWARD:
can_expand_backward = (src_bytes_available_before > match_bytes_before);
break;
case ExpandMethod::FORWARD:
can_expand_forward = (src_bytes_available_after > match_bytes_after);
break;
case ExpandMethod::BOTH:
can_expand_backward = (src_bytes_available_before > match_bytes_before);
can_expand_forward = (src_bytes_available_after > match_bytes_after);
break;
default:
throw logic_error("invalid expand method");
}
if (!can_expand_backward && !can_expand_forward) {
throw runtime_error("no further expansion is allowed");
}
if (can_expand_backward) {
match_bytes_before++;
}
if (can_expand_forward) {
match_bytes_after++;
}
}
throw runtime_error("scan field too long; too many matches");
}
void find_all_matches(uint32_t src_addr, uint32_t src_size) const {
if (!this->src_file) {
throw runtime_error("no source file selected");
}
unordered_map<string, uint32_t> results;
for (const auto& it : files) {
if (it.second == this->src_file) {
log.info("(%s) %08" PRIX32 " (from source)", it.first.c_str(), src_addr);
results.emplace(it.first, src_addr);
} else {
array<future<uint32_t>, 3> futures;
static const array<ExpandMethod, 3> methods = {
ExpandMethod::FORWARD,
ExpandMethod::BACKWARD,
ExpandMethod::BOTH,
};
for (size_t z = 0; z < methods.size(); z++) {
futures[z] = async(&XBEPatchTranslator::find_match, this, it.second, src_addr, src_size, methods[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]);
try {
uint32_t ret = futures[z].get();
log.info("(%s) (%s) %08" PRIX32, it.first.c_str(), 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());
}
}
if (match_addrs.empty()) {
log.error("(%s) no match found", it.first.c_str());
} else if (match_addrs.size() > 1) {
log.error("(%s) different matches found by different methods", it.first.c_str());
} 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);
}
}
void handle_command(const string& command) {
auto tokens = split(command, ' ');
if (tokens.empty()) {
throw runtime_error("no command given");
}
strip_trailing_whitespace(tokens[tokens.size() - 1]);
if (tokens[0] == "use") {
this->set_source_file(tokens.at(1));
} else if (tokens[0] == "match") {
this->find_all_matches(stoul(tokens.at(1), nullptr, 16), stoul(tokens.at(2), nullptr, 16));
} else if (!tokens[0].empty()) {
throw runtime_error("unknown command");
}
}
void run_shell() {
while (!feof(stdin)) {
if (!this->src_filename.empty()) {
fprintf(stdout, "ar-trans:%s/%s> ", this->directory.c_str(), this->src_filename.c_str());
} else {
fprintf(stdout, "ar-trans:%s> ", this->directory.c_str());
}
fflush(stdout);
string command = fgets(stdin);
try {
this->handle_command(command);
} catch (const exception& e) {
this->log.error("Failed: %s", e.what());
}
}
fputc('\n', stdout);
}
private:
PrefixedLogger log;
string directory;
unordered_map<string, shared_ptr<const XBEFile>> files;
string src_filename;
shared_ptr<const XBEFile> src_file;
};
void run_ar_code_translator(const std::string& directory, const std::string& use_filename, const std::string& command) {
ARCodeTranslator trans(directory);
if (!use_filename.empty()) {
trans.set_source_file(use_filename);
}
if (!command.empty()) {
trans.handle_command(command);
} else {
trans.run_shell();
}
}
void run_xbe_patch_translator(const std::string& directory, const std::string& use_filename, const std::string& command) {
XBEPatchTranslator trans(directory);
if (!use_filename.empty()) {
trans.set_source_file(use_filename);
}
if (!command.empty()) {
trans.handle_command(command);
} else {
trans.run_shell();
}
}
vector<pair<uint32_t, string>> diff_dol_files(const string& a_filename, const string& b_filename) {
DOLFile a(a_filename.c_str());
DOLFile b(b_filename.c_str());
auto a_mem = make_shared<MemoryContext>();
auto b_mem = make_shared<MemoryContext>();
a.load_into(a_mem);
b.load_into(b_mem);
uint32_t min_addr = 0xFFFFFFFF;
uint32_t max_addr = 0x00000000;
for (const auto& sec : a.sections) {
min_addr = min<uint32_t>(min_addr, sec.address);
max_addr = max<uint32_t>(max_addr, sec.address + sec.data.size());
}
for (const auto& sec : b.sections) {
min_addr = min<uint32_t>(min_addr, sec.address);
max_addr = max<uint32_t>(max_addr, sec.address + sec.data.size());
}
vector<pair<uint32_t, string>> ret;
for (uint32_t addr = min_addr; addr < max_addr; addr += 4) {
bool a_exists = a_mem->exists(addr, 4);
bool b_exists = b_mem->exists(addr, 4);
if (a_exists && b_exists) {
string a_value = a_mem->read(addr, 4);
string b_value = b_mem->read(addr, 4);
if (a_value != b_value) {
if (!ret.empty() && (ret.back().first + ret.back().second.size() == addr)) {
ret.back().second += b_value;
} else {
ret.emplace_back(make_pair(addr, b_value));
}
}
}
}
return ret;
}
-11
View File
@@ -1,11 +0,0 @@
#pragma once
#include <stdint.h>
#include <string>
#include <utility>
#include <vector>
void run_ar_code_translator(const std::string& directory, const std::string& use_filename, const std::string& command);
void run_xbe_patch_translator(const std::string& directory, const std::string& use_filename, const std::string& command);
std::vector<std::pair<uint32_t, std::string>> diff_dol_files(const std::string& a_filename, const std::string& b_filename);
+1003
View File
File diff suppressed because it is too large Load Diff
+263
View File
@@ -0,0 +1,263 @@
#pragma once
#include <memory>
#include <mutex>
#include <phosg/JSON.hh>
#include <shared_mutex>
#include <string>
#include <unordered_map>
#include <vector>
#include "Text.hh"
class LicenseIndex;
struct DCNTELicense {
std::string serial_number;
std::string access_key;
static std::shared_ptr<DCNTELicense> from_json(const JSON& json);
JSON json() const;
};
struct V1V2License {
uint32_t serial_number = 0;
std::string access_key;
static std::shared_ptr<V1V2License> from_json(const JSON& json);
JSON json() const;
};
struct GCLicense {
uint32_t serial_number = 0;
std::string access_key;
std::string password;
static std::shared_ptr<GCLicense> from_json(const JSON& json);
JSON json() const;
};
struct XBLicense {
std::string gamertag;
uint64_t user_id = 0;
uint64_t account_id = 0;
static std::shared_ptr<XBLicense> from_json(const JSON& json);
JSON json() const;
};
struct BBLicense {
std::string username;
std::string password;
static std::shared_ptr<BBLicense> from_json(const JSON& json);
JSON json() const;
};
struct Account {
enum class Flag : uint32_t {
// clang-format off
KICK_USER = 0x00000001,
BAN_USER = 0x00000002,
SILENCE_USER = 0x00000004,
CHANGE_EVENT = 0x00000010,
ANNOUNCE = 0x00000020,
FREE_JOIN_GAMES = 0x00000040,
DEBUG = 0x01000000,
CHEAT_ANYWHERE = 0x02000000,
DISABLE_QUEST_REQUIREMENTS = 0x04000000,
ALWAYS_ENABLE_CHAT_COMMANDS = 0x08000000,
MODERATOR = 0x00000007,
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.
UNUSED_BITS = 0x70FFFF00,
// clang-format on
};
// account_id is also the account's guild card number
uint32_t account_id = 0;
uint32_t flags = 0;
uint64_t ban_end_time = 0; // 0 = not banned
std::string last_player_name;
std::string auto_reply_message;
uint32_t ep3_current_meseta = 0;
uint32_t ep3_total_meseta_earned = 0;
uint32_t bb_team_id = 0;
bool is_temporary = false; // If true, isn't saved to disk
std::unordered_set<std::string> auto_patches_enabled;
std::unordered_map<std::string, std::shared_ptr<DCNTELicense>> dc_nte_licenses;
std::unordered_map<uint32_t, std::shared_ptr<V1V2License>> dc_licenses;
std::unordered_map<uint32_t, std::shared_ptr<V1V2License>> pc_licenses;
std::unordered_map<uint32_t, std::shared_ptr<GCLicense>> gc_licenses;
std::unordered_map<std::string, std::shared_ptr<XBLicense>> xb_licenses;
std::unordered_map<std::string, std::shared_ptr<BBLicense>> bb_licenses;
Account() = default;
explicit Account(const JSON& json);
virtual ~Account() = default;
JSON json() const;
virtual void save() const;
virtual void delete_file() const;
[[nodiscard]] inline bool check_flag(Flag flag) const {
return !!(this->flags & static_cast<uint32_t>(flag));
}
inline void set_flag(Flag flag) {
this->flags |= static_cast<uint32_t>(flag);
}
inline void clear_flag(Flag flag) {
this->flags &= (~static_cast<uint32_t>(flag));
}
inline void toggle_flag(Flag flag) {
this->flags ^= static_cast<uint32_t>(flag);
}
inline void replace_all_flags(Flag mask) {
this->flags = static_cast<uint32_t>(mask);
}
void print(FILE* stream) const;
};
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
std::shared_ptr<DCNTELicense> dc_nte_license;
std::shared_ptr<V1V2License> dc_license;
std::shared_ptr<V1V2License> pc_license;
std::shared_ptr<GCLicense> gc_license;
std::shared_ptr<XBLicense> xb_license;
std::shared_ptr<BBLicense> bb_license;
};
class AccountIndex {
public:
class no_username : public std::invalid_argument {
public:
no_username() : invalid_argument("serial number is zero or username is missing") {}
};
class incorrect_password : public std::invalid_argument {
public:
incorrect_password() : invalid_argument("incorrect password") {}
};
class incorrect_access_key : public std::invalid_argument {
public:
incorrect_access_key() : invalid_argument("incorrect access key") {}
};
class missing_account : public std::invalid_argument {
public:
missing_account() : invalid_argument("missing account") {}
};
explicit AccountIndex(bool force_all_temporary);
virtual ~AccountIndex() = default;
std::shared_ptr<Account> create_account(bool is_temporary) const;
size_t count() const;
std::vector<std::shared_ptr<Account>> all() const;
void add(std::shared_ptr<Account> a);
void remove(uint32_t serial_number);
void add_dc_nte_license(std::shared_ptr<Account> account, std::shared_ptr<DCNTELicense> license);
void add_dc_license(std::shared_ptr<Account> account, std::shared_ptr<V1V2License> license);
void add_pc_license(std::shared_ptr<Account> account, std::shared_ptr<V1V2License> license);
void add_gc_license(std::shared_ptr<Account> account, std::shared_ptr<GCLicense> license);
void add_xb_license(std::shared_ptr<Account> account, std::shared_ptr<XBLicense> license);
void add_bb_license(std::shared_ptr<Account> account, std::shared_ptr<BBLicense> license);
void remove_dc_nte_license(std::shared_ptr<Account> account, const std::string& serial_number);
void remove_dc_license(std::shared_ptr<Account> account, uint32_t serial_number);
void remove_pc_license(std::shared_ptr<Account> account, uint32_t serial_number);
void remove_gc_license(std::shared_ptr<Account> account, uint32_t serial_number);
void remove_xb_license(std::shared_ptr<Account> account, const std::string& gamertag);
void remove_bb_license(std::shared_ptr<Account> account, const std::string& username);
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);
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);
std::shared_ptr<Login> from_pc_credentials(
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,
const std::string* password,
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);
std::shared_ptr<Login> from_bb_credentials(
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;
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;
std::unordered_map<uint32_t, std::shared_ptr<Account>> by_dc_serial_number;
std::unordered_map<uint32_t, std::shared_ptr<Account>> by_pc_serial_number;
std::unordered_map<uint32_t, std::shared_ptr<Account>> by_gc_serial_number;
std::unordered_map<std::string, std::shared_ptr<Account>> by_xb_gamertag;
std::unordered_map<std::string, std::shared_ptr<Account>> by_bb_username;
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);
std::shared_ptr<Login> from_dc_credentials_locked(
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);
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(
const std::string& gamertag,
uint64_t user_id,
uint64_t account_id);
std::shared_ptr<Login> from_bb_credentials_locked(
const std::string& username,
const std::string* password);
};
+14
View File
@@ -0,0 +1,14 @@
#pragma once
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
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<std::pair<uint32_t, std::string>> diff_dol_files(const std::string&, const std::string&) {
throw std::runtime_error("resource_file is not available; install it and rebuild newserv");
}
+528
View File
@@ -0,0 +1,528 @@
#include "AddressTranslator.hh"
#include <array>
#include <future>
#include <phosg/Filesystem.hh>
#include <phosg/Strings.hh>
#include <resource_file/ExecutableFormats/DOLFile.hh>
#include <resource_file/ExecutableFormats/XBEFile.hh>
using namespace std;
class AddressTranslator {
public:
enum class ExpandMethod {
PPC_TEXT_FORWARD = 0,
PPC_TEXT_FORWARD_WITH_BARRIER,
PPC_TEXT_BACKWARD,
PPC_TEXT_BACKWARD_WITH_BARRIER,
PPC_TEXT_BOTH,
PPC_TEXT_BOTH_WITH_BARRIER,
PPC_TEXT_BOTH_IGNORE_ORIGIN,
PPC_DATA_FORWARD,
PPC_DATA_BACKWARD,
PPC_DATA_BOTH,
RAW_FORWARD,
RAW_BACKWARD,
RAW_BOTH,
};
static const char* name_for_expand_method(ExpandMethod method) {
switch (method) {
case ExpandMethod::PPC_TEXT_FORWARD:
return "PPC_TEXT_FORWARD";
case ExpandMethod::PPC_TEXT_FORWARD_WITH_BARRIER:
return "PPC_TEXT_FORWARD_WITH_BARRIER";
case ExpandMethod::PPC_TEXT_BACKWARD:
return "PPC_TEXT_BACKWARD";
case ExpandMethod::PPC_TEXT_BACKWARD_WITH_BARRIER:
return "PPC_TEXT_BACKWARD_WITH_BARRIER";
case ExpandMethod::PPC_TEXT_BOTH:
return "PPC_TEXT_BOTH";
case ExpandMethod::PPC_TEXT_BOTH_WITH_BARRIER:
return "PPC_TEXT_BOTH_WITH_BARRIER";
case ExpandMethod::PPC_TEXT_BOTH_IGNORE_ORIGIN:
return "PPC_TEXT_BOTH_IGNORE_ORIGIN";
case ExpandMethod::PPC_DATA_FORWARD:
return "PPC_DATA_FORWARD";
case ExpandMethod::PPC_DATA_BACKWARD:
return "PPC_DATA_BACKWARD";
case ExpandMethod::PPC_DATA_BOTH:
return "PPC_DATA_BOTH";
case ExpandMethod::RAW_FORWARD:
return "RAW_FORWARD";
case ExpandMethod::RAW_BACKWARD:
return "RAW_BACKWARD";
case ExpandMethod::RAW_BOTH:
return "RAW_BOTH";
default:
throw logic_error("invalid expand method");
}
}
static bool is_ppc_expand_method(ExpandMethod method) {
switch (method) {
case ExpandMethod::PPC_TEXT_FORWARD:
case ExpandMethod::PPC_TEXT_FORWARD_WITH_BARRIER:
case ExpandMethod::PPC_TEXT_BACKWARD:
case ExpandMethod::PPC_TEXT_BACKWARD_WITH_BARRIER:
case ExpandMethod::PPC_TEXT_BOTH:
case ExpandMethod::PPC_TEXT_BOTH_WITH_BARRIER:
case ExpandMethod::PPC_TEXT_BOTH_IGNORE_ORIGIN:
case ExpandMethod::PPC_DATA_FORWARD:
case ExpandMethod::PPC_DATA_BACKWARD:
case ExpandMethod::PPC_DATA_BOTH:
return true;
case ExpandMethod::RAW_FORWARD:
case ExpandMethod::RAW_BACKWARD:
case ExpandMethod::RAW_BOTH:
return false;
default:
throw logic_error("invalid expand method");
}
}
static bool is_ppc_data_expand_method(ExpandMethod method) {
switch (method) {
case ExpandMethod::PPC_DATA_FORWARD:
case ExpandMethod::PPC_DATA_BACKWARD:
case ExpandMethod::PPC_DATA_BOTH:
return true;
case ExpandMethod::PPC_TEXT_FORWARD:
case ExpandMethod::PPC_TEXT_FORWARD_WITH_BARRIER:
case ExpandMethod::PPC_TEXT_BACKWARD:
case ExpandMethod::PPC_TEXT_BACKWARD_WITH_BARRIER:
case ExpandMethod::PPC_TEXT_BOTH:
case ExpandMethod::PPC_TEXT_BOTH_WITH_BARRIER:
case ExpandMethod::PPC_TEXT_BOTH_IGNORE_ORIGIN:
case ExpandMethod::RAW_FORWARD:
case ExpandMethod::RAW_BACKWARD:
case ExpandMethod::RAW_BOTH:
return false;
default:
throw logic_error("invalid expand method");
}
}
AddressTranslator(const string& directory)
: log("[addr-trans] "),
directory(directory),
enable_ppc(false) {
while (ends_with(this->directory, "/")) {
this->directory.pop_back();
}
for (const auto& filename : list_directory(this->directory)) {
if (ends_with(filename, ".dol")) {
string name = filename.substr(0, filename.size() - 4);
string path = directory + "/" + filename;
DOLFile dol(path.c_str());
auto mem = make_shared<MemoryContext>();
dol.load_into(mem);
this->mems.emplace(name, mem);
this->enable_ppc = true;
this->log.info("Loaded %s", name.c_str());
} else if (ends_with(filename, ".xbe")) {
string name = filename.substr(0, filename.size() - 4);
string path = directory + "/" + filename;
XBEFile xbe(path.c_str());
auto mem = make_shared<MemoryContext>();
xbe.load_into(mem);
this->mems.emplace(name, mem);
this->log.info("Loaded %s", name.c_str());
} else if (ends_with(filename, ".bin")) {
string name = filename.substr(0, filename.size() - 4);
string path = directory + "/" + filename;
string data = load_file(path);
auto mem = make_shared<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());
}
}
}
~AddressTranslator() = default;
const string& get_source_filename() const {
return this->src_filename;
}
void set_source_file(const string& filename) {
this->src_filename = filename;
this->src_mem = this->mems.at(this->src_filename);
}
void find_ppc_rtoc_global_regs() const {
for (const auto& it : this->mems) {
bool r2_high_found = false;
bool r2_low_found = false;
bool r13_high_found = false;
bool r13_low_found = false;
uint32_t r2 = 0;
uint32_t r13 = 0;
for (const auto& block : it.second->allocated_blocks()) {
StringReader r = it.second->reader(block.first, block.second);
while (!r.eof() && r.where()) {
uint32_t opcode = r.get_u32b();
if ((opcode & 0xFFFF0000) == 0x3DA00000) {
if (r13_high_found) {
throw runtime_error("multiple values for r13_high");
}
r13_high_found = true;
r13 |= (opcode << 16);
} else if ((opcode & 0xFFFF0000) == 0x3C400000) {
if (r2_high_found) {
throw runtime_error("multiple values for r2_high");
}
r2_high_found = true;
r2 |= (opcode << 16);
} else if ((opcode & 0xFFFF0000) == 0x61AD0000) {
if (r13_low_found) {
throw runtime_error("multiple values for r13_low");
}
r13_low_found = true;
r13 |= (opcode & 0xFFFF);
} else if ((opcode & 0xFFFF0000) == 0x60420000) {
if (r2_low_found) {
throw runtime_error("multiple values for r2_low");
}
r2_low_found = true;
r2 |= (opcode & 0xFFFF);
}
}
}
if (r2_low_found && r2_high_found) {
fprintf(stderr, "(%s) r2 = %08" PRIX32 "\n", it.first.c_str(), r2);
} else {
fprintf(stderr, "(%s) r2 = __MISSING__\n", it.first.c_str());
}
if (r13_low_found && r13_high_found) {
fprintf(stderr, "(%s) r13 = %08" PRIX32 "\n", it.first.c_str(), r13);
} else {
fprintf(stderr, "(%s) r13 = __MISSING__\n", it.first.c_str());
}
}
}
uint32_t find_match(
shared_ptr<const MemoryContext> dest_mem,
uint32_t src_addr,
uint32_t src_size,
ExpandMethod expand_method) const {
bool is_ppc = this->is_ppc_expand_method(expand_method);
bool is_ppc_data = this->is_ppc_data_expand_method(expand_method);
if (!this->src_mem) {
throw runtime_error("no source file selected");
}
if (src_size == 0) {
src_size = is_ppc ? 4 : 1;
}
pair<uint32_t, uint32_t> src_section = make_pair(0, 0);
for (const auto& sec : this->src_mem->allocated_blocks()) {
if (src_addr >= sec.first && src_addr + src_size <= sec.first + sec.second) {
src_section = sec;
break;
}
}
if (!src_section.second) {
throw runtime_error("source address not within any section");
}
const char* method_token = this->name_for_expand_method(expand_method);
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",
method_token, src_offset, src_bytes_available_before, src_bytes_available_after);
size_t match_bytes_before = 0;
size_t match_bytes_after = 0;
while (match_bytes_before + match_bytes_after + 4 < 0x100) {
size_t num_matches = 0;
size_t last_match_address = 0;
size_t match_length = match_bytes_before + match_bytes_after + 4;
StringReader src_r = this->src_mem->reader(src_section.first + src_offset - match_bytes_before, match_length);
for (const auto& dest_section : dest_mem->allocated_blocks()) {
for (size_t dest_match_offset = 0;
dest_match_offset + match_length < dest_section.second;
dest_match_offset += (is_ppc ? 4 : 1)) {
src_r.go(0);
StringReader dest_r = dest_mem->reader(dest_section.first + dest_match_offset, match_length);
size_t z;
if (is_ppc) {
for (z = 0; z < match_length; z += 4) {
if ((expand_method == ExpandMethod::PPC_TEXT_BOTH_IGNORE_ORIGIN) && (z == match_bytes_before)) {
src_r.skip(4);
dest_r.skip(4);
} else if (!is_ppc_data) {
uint32_t src_opcode = src_r.get_u32b();
uint32_t dest_opcode = dest_r.get_u32b();
uint32_t src_class = src_opcode & 0xFC000000;
if (src_class != (dest_opcode & 0xFC000000)) {
break;
}
if (src_class == 0x48000000) {
// b +-offset
src_opcode &= 0xFC000003;
dest_opcode &= 0xFC000003;
} else if (((src_opcode & 0xAC1F0000) == 0x800D0000) || ((src_opcode & 0xAC1F0000) == 0x80020000)) {
// lwz/lfs rXX/fXX, [r2/r13 +- offset] OR stw/stfs [r2/r13 +- offset], rXX/fXX
src_opcode &= 0xFFFF0000;
dest_opcode &= 0xFFFF0000;
}
if (src_opcode != dest_opcode) {
break;
}
} else {
uint32_t src_data = src_r.get_u32b();
uint32_t dest_data = dest_r.get_u32b();
if ((src_data & 0xFE000000) == 0x80000000) {
src_data &= 0xFE000003;
}
if ((dest_data & 0xFE000000) == 0x80000000) {
dest_data &= 0xFE000003;
}
if (src_data != dest_data) {
break;
}
}
}
} else {
for (z = 0; z < match_length; z++) {
uint8_t src_data = src_r.get_u8();
uint8_t dest_data = dest_r.get_u8();
if (src_data != dest_data) {
break;
}
}
}
if (z == match_length) {
num_matches++;
last_match_address = dest_section.first + dest_match_offset + match_bytes_before;
}
}
}
this->log.info("(find_match/%s) For match length %zX, %zu matches found", method_token, match_length, num_matches);
if (num_matches == 1) {
return last_match_address;
} else if (num_matches == 0) {
throw runtime_error("did not find exactly one match");
}
bool can_expand_backward = false;
bool can_expand_forward = false;
switch (expand_method) {
case ExpandMethod::PPC_TEXT_BACKWARD_WITH_BARRIER:
can_expand_backward = (src_r.pget_u32b(0) != 0x4E800020) &&
(src_bytes_available_before >= match_bytes_before + 4);
break;
case ExpandMethod::PPC_TEXT_BACKWARD:
case ExpandMethod::PPC_DATA_BACKWARD:
can_expand_backward = (src_bytes_available_before >= match_bytes_before + 4);
break;
case ExpandMethod::PPC_TEXT_FORWARD_WITH_BARRIER:
can_expand_forward = (src_r.pget_u32b(src_r.size() - 4) != 0x4E800020) &&
(src_bytes_available_after >= match_bytes_after + 4);
break;
case ExpandMethod::PPC_TEXT_FORWARD:
case ExpandMethod::PPC_DATA_FORWARD:
can_expand_forward = (src_bytes_available_after >= match_bytes_after + 4);
break;
case ExpandMethod::PPC_TEXT_BOTH_WITH_BARRIER:
case ExpandMethod::PPC_TEXT_BOTH_IGNORE_ORIGIN:
can_expand_backward = (src_r.pget_u32b(0) != 0x4E800020) &&
(src_bytes_available_before >= match_bytes_before + 4);
can_expand_forward = (src_r.pget_u32b(src_r.size() - 4) != 0x4E800020) &&
(src_bytes_available_after >= match_bytes_after + 4);
break;
case ExpandMethod::PPC_TEXT_BOTH:
case ExpandMethod::PPC_DATA_BOTH:
can_expand_backward = (src_bytes_available_before >= match_bytes_before + 4);
can_expand_forward = (src_bytes_available_after >= match_bytes_after + 4);
break;
case ExpandMethod::RAW_BACKWARD:
can_expand_backward = (src_bytes_available_before > match_bytes_before);
break;
case ExpandMethod::RAW_FORWARD:
can_expand_forward = (src_bytes_available_after > match_bytes_after);
break;
case ExpandMethod::RAW_BOTH:
can_expand_backward = (src_bytes_available_before > match_bytes_before);
can_expand_forward = (src_bytes_available_after > match_bytes_after);
break;
default:
throw logic_error("invalid expand method");
}
if (!can_expand_backward && !can_expand_forward) {
throw runtime_error("no further expansion is allowed");
}
if (can_expand_backward) {
match_bytes_before += (is_ppc ? 4 : 1);
}
if (can_expand_forward) {
match_bytes_after += (is_ppc ? 4 : 1);
}
}
throw runtime_error("scan field too long; too many matches");
}
void find_all_matches(uint32_t src_addr, uint32_t src_size) const {
if (!this->src_mem) {
throw runtime_error("no source file selected");
}
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);
results.emplace(it.first, src_addr);
} else {
vector<future<uint32_t>> futures;
static const vector<ExpandMethod> ppc_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,
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]));
}
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]);
try {
uint32_t ret = futures[z].get();
log.info("(%s) (%s) %08" PRIX32, it.first.c_str(), 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());
}
}
if (match_addrs.empty()) {
log.error("(%s) no match found", it.first.c_str());
} else if (match_addrs.size() > 1) {
log.error("(%s) different matches found by different methods", it.first.c_str());
} 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);
}
}
void handle_command(const string& command) {
auto tokens = split(command, ' ');
if (tokens.empty()) {
throw runtime_error("no command given");
}
strip_trailing_whitespace(tokens[tokens.size() - 1]);
if (tokens[0] == "use") {
this->set_source_file(tokens.at(1));
} else if (tokens[0] == "match") {
this->find_all_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].empty()) {
throw runtime_error("unknown command");
}
}
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());
} else {
fprintf(stdout, "addr-trans:%s> ", this->directory.c_str());
}
fflush(stdout);
string command = fgets(stdin);
try {
this->handle_command(command);
} catch (const exception& e) {
this->log.error("Failed: %s", e.what());
}
}
fputc('\n', stdout);
}
private:
PrefixedLogger log;
string directory;
unordered_map<string, shared_ptr<const MemoryContext>> mems;
string src_filename;
shared_ptr<const MemoryContext> src_mem;
bool enable_ppc;
};
void run_address_translator(const std::string& directory, const std::string& use_filename, const std::string& command) {
AddressTranslator trans(directory);
if (!use_filename.empty()) {
trans.set_source_file(use_filename);
}
if (!command.empty()) {
trans.handle_command(command);
} else {
trans.run_shell();
}
}
vector<pair<uint32_t, string>> diff_dol_files(const string& a_filename, const string& b_filename) {
DOLFile a(a_filename.c_str());
DOLFile b(b_filename.c_str());
auto a_mem = make_shared<MemoryContext>();
auto b_mem = make_shared<MemoryContext>();
a.load_into(a_mem);
b.load_into(b_mem);
uint32_t min_addr = 0xFFFFFFFF;
uint32_t max_addr = 0x00000000;
for (const auto& sec : a.sections) {
min_addr = min<uint32_t>(min_addr, sec.address);
max_addr = max<uint32_t>(max_addr, sec.address + sec.data.size());
}
for (const auto& sec : b.sections) {
min_addr = min<uint32_t>(min_addr, sec.address);
max_addr = max<uint32_t>(max_addr, sec.address + sec.data.size());
}
vector<pair<uint32_t, string>> ret;
for (uint32_t addr = min_addr; addr < max_addr; addr += 4) {
bool a_exists = a_mem->exists(addr, 4);
bool b_exists = b_mem->exists(addr, 4);
if (a_exists && b_exists) {
string a_value = a_mem->read(addr, 4);
string b_value = b_mem->read(addr, 4);
if (a_value != b_value) {
if (!ret.empty() && (ret.back().first + ret.back().second.size() == addr)) {
ret.back().second += b_value;
} else {
ret.emplace_back(make_pair(addr, b_value));
}
}
}
}
return ret;
}
+10
View File
@@ -0,0 +1,10 @@
#pragma once
#include <stdint.h>
#include <string>
#include <utility>
#include <vector>
void run_address_translator(const std::string& directory, const std::string& use_filename, const std::string& command);
std::vector<std::pair<uint32_t, std::string>> diff_dol_files(const std::string& a_filename, const std::string& b_filename);
+16 -6
View File
@@ -9,16 +9,21 @@
using namespace std; using namespace std;
template <bool IsBigEndian> template <bool IsBigEndian>
struct BMLHeader { struct BMLHeaderT {
using U32T = typename std::conditional<IsBigEndian, be_uint32_t, le_uint32_t>::type; using U32T = typename std::conditional<IsBigEndian, be_uint32_t, le_uint32_t>::type;
parray<uint8_t, 0x04> unknown_a1; parray<uint8_t, 0x04> unknown_a1;
U32T num_entries; U32T num_entries;
parray<uint8_t, 0x38> unknown_a2; parray<uint8_t, 0x38> unknown_a2;
} __attribute__((packed)); } __packed__;
using BMLHeader = BMLHeaderT<false>;
using BMLHeaderBE = BMLHeaderT<true>;
check_struct_size(BMLHeader, 0x40);
check_struct_size(BMLHeaderBE, 0x40);
template <bool IsBigEndian> template <bool IsBigEndian>
struct BMLHeaderEntry { struct BMLHeaderEntryT {
using U32T = typename std::conditional<IsBigEndian, be_uint32_t, le_uint32_t>::type; using U32T = typename std::conditional<IsBigEndian, be_uint32_t, le_uint32_t>::type;
pstring<TextEncoding::ASCII, 0x20> filename; pstring<TextEncoding::ASCII, 0x20> filename;
@@ -28,17 +33,22 @@ struct BMLHeaderEntry {
U32T compressed_gvm_size; U32T compressed_gvm_size;
U32T decompressed_gvm_size; U32T decompressed_gvm_size;
parray<uint8_t, 0x0C> unknown_a2; parray<uint8_t, 0x0C> unknown_a2;
} __attribute__((packed)); } __packed__;
using BMLHeaderEntry = BMLHeaderEntryT<false>;
using BMLHeaderEntryBE = BMLHeaderEntryT<true>;
check_struct_size(BMLHeaderEntry, 0x40);
check_struct_size(BMLHeaderEntryBE, 0x40);
template <bool IsBigEndian> template <bool IsBigEndian>
void BMLArchive::load_t() { void BMLArchive::load_t() {
StringReader r(*this->data); StringReader r(*this->data);
const auto& header = r.get<BMLHeader<IsBigEndian>>(); const auto& header = r.get<BMLHeaderT<IsBigEndian>>();
size_t offset = 0x800; size_t offset = 0x800;
while (this->entries.size() < header.num_entries) { while (this->entries.size() < header.num_entries) {
const auto& entry = r.get<BMLHeaderEntry<IsBigEndian>>(); const auto& entry = r.get<BMLHeaderEntryT<IsBigEndian>>();
if (offset + entry.compressed_size > this->data->size()) { if (offset + entry.compressed_size > this->data->size()) {
throw runtime_error("BML data entry extends beyond end of data"); throw runtime_error("BML data entry extends beyond end of data");
+4 -4
View File
@@ -36,7 +36,7 @@ public:
/* 28 */ le_uint32_t unknown_a15; /* 28 */ le_uint32_t unknown_a15;
/* 2C */ le_uint32_t unknown_a16; /* 2C */ le_uint32_t unknown_a16;
/* 30 */ /* 30 */
} __attribute__((packed)); } __packed_ws__(AttackData, 0x30);
struct ResistData { struct ResistData {
/* 00 */ le_int16_t evp_bonus; /* 00 */ le_int16_t evp_bonus;
@@ -51,7 +51,7 @@ public:
/* 18 */ le_uint32_t unknown_a9; /* 18 */ le_uint32_t unknown_a9;
/* 1C */ le_int32_t dfp_bonus; /* 1C */ le_int32_t dfp_bonus;
/* 20 */ /* 20 */
} __attribute__((packed)); } __packed_ws__(ResistData, 0x20);
struct MovementData { struct MovementData {
/* 00 */ le_float idle_move_speed; /* 00 */ le_float idle_move_speed;
@@ -67,7 +67,7 @@ public:
/* 28 */ le_uint32_t unknown_a7; /* 28 */ le_uint32_t unknown_a7;
/* 2C */ le_uint32_t unknown_a8; /* 2C */ le_uint32_t unknown_a8;
/* 30 */ /* 30 */
} __attribute__((packed)); } __packed_ws__(MovementData, 0x30);
struct Table { struct Table {
/* 0000 */ parray<parray<PlayerStats, 0x60>, 4> stats; /* 0000 */ parray<parray<PlayerStats, 0x60>, 4> stats;
@@ -77,7 +77,7 @@ public:
/* F600 */ /* F600 */
void print(FILE* stream) const; void print(FILE* stream) const;
} __attribute__((packed)); } __packed_ws__(Table, 0xF600);
BattleParamsIndex( BattleParamsIndex(
std::shared_ptr<const std::string> data_on_ep1, // BattleParamEntry_on.dat std::shared_ptr<const std::string> data_on_ep1, // BattleParamEntry_on.dat
+1 -1
View File
@@ -56,7 +56,7 @@ CatSession::CatSession(
if (!bev) { if (!bev) {
throw runtime_error(string_printf("failed to open socket (%d)", EVUTIL_SOCKET_ERROR())); throw runtime_error(string_printf("failed to open socket (%d)", EVUTIL_SOCKET_ERROR()));
} }
this->channel.set_bufferevent(bev); this->channel.set_bufferevent(bev, 0);
if (bufferevent_socket_connect(this->channel.bev.get(), if (bufferevent_socket_connect(this->channel.bev.get(),
reinterpret_cast<const sockaddr*>(&remote), sizeof(struct sockaddr_in)) != 0) { reinterpret_cast<const sockaddr*>(&remote), sizeof(struct sockaddr_in)) != 0) {
+8 -11
View File
@@ -32,6 +32,7 @@ Channel::Channel(
TerminalFormat terminal_send_color, TerminalFormat terminal_send_color,
TerminalFormat terminal_recv_color) TerminalFormat terminal_recv_color)
: bev(nullptr, flush_and_free_bufferevent), : bev(nullptr, flush_and_free_bufferevent),
virtual_network_id(0),
version(version), version(version),
language(language), language(language),
name(name), name(name),
@@ -44,6 +45,7 @@ Channel::Channel(
Channel::Channel( Channel::Channel(
struct bufferevent* bev, struct bufferevent* bev,
uint64_t virtual_network_id,
Version version, Version version,
uint8_t language, uint8_t language,
on_command_received_t on_command_received, on_command_received_t on_command_received,
@@ -61,7 +63,7 @@ Channel::Channel(
on_command_received(on_command_received), on_command_received(on_command_received),
on_error(on_error), on_error(on_error),
context_obj(context_obj) { context_obj(context_obj) {
this->set_bufferevent(bev); this->set_bufferevent(bev, virtual_network_id);
} }
void Channel::replace_with( void Channel::replace_with(
@@ -70,10 +72,9 @@ void Channel::replace_with(
on_error_t on_error, on_error_t on_error,
void* context_obj, void* context_obj,
const std::string& name) { const std::string& name) {
this->set_bufferevent(other.bev.release()); this->set_bufferevent(other.bev.release(), other.virtual_network_id);
this->local_addr = other.local_addr; this->local_addr = other.local_addr;
this->remote_addr = other.remote_addr; this->remote_addr = other.remote_addr;
this->is_virtual_connection = other.is_virtual_connection;
this->version = other.version; this->version = other.version;
this->language = other.language; this->language = other.language;
this->crypt_in = other.crypt_in; this->crypt_in = other.crypt_in;
@@ -87,27 +88,23 @@ void Channel::replace_with(
other.disconnect(); // Clears crypts, addrs, etc. other.disconnect(); // Clears crypts, addrs, etc.
} }
void Channel::set_bufferevent(struct bufferevent* bev) { void Channel::set_bufferevent(struct bufferevent* bev, uint64_t virtual_network_id) {
this->bev.reset(bev); this->bev.reset(bev);
this->virtual_network_id = virtual_network_id;
if (this->bev.get()) { if (this->bev.get()) {
int fd = bufferevent_getfd(this->bev.get()); int fd = bufferevent_getfd(this->bev.get());
if (fd < 0) { if (fd < 0) {
this->is_virtual_connection = true;
memset(&this->local_addr, 0, sizeof(this->local_addr)); memset(&this->local_addr, 0, sizeof(this->local_addr));
memset(&this->remote_addr, 0, sizeof(this->remote_addr)); memset(&this->remote_addr, 0, sizeof(this->remote_addr));
} else { } else {
this->is_virtual_connection = false;
get_socket_addresses(fd, &this->local_addr, &this->remote_addr); get_socket_addresses(fd, &this->local_addr, &this->remote_addr);
} }
bufferevent_setcb(this->bev.get(), bufferevent_setcb(this->bev.get(), &Channel::dispatch_on_input, nullptr, &Channel::dispatch_on_error, this);
&Channel::dispatch_on_input, nullptr,
&Channel::dispatch_on_error, this);
bufferevent_enable(this->bev.get(), EV_READ | EV_WRITE); bufferevent_enable(this->bev.get(), EV_READ | EV_WRITE);
} else { } else {
this->is_virtual_connection = false;
memset(&this->local_addr, 0, sizeof(this->local_addr)); memset(&this->local_addr, 0, sizeof(this->local_addr));
memset(&this->remote_addr, 0, sizeof(this->remote_addr)); memset(&this->remote_addr, 0, sizeof(this->remote_addr));
} }
@@ -149,7 +146,7 @@ void Channel::disconnect() {
memset(&this->local_addr, 0, sizeof(this->local_addr)); memset(&this->local_addr, 0, sizeof(this->local_addr));
memset(&this->remote_addr, 0, sizeof(this->remote_addr)); memset(&this->remote_addr, 0, sizeof(this->remote_addr));
this->is_virtual_connection = false; this->virtual_network_id = false;
this->crypt_in.reset(); this->crypt_in.reset();
this->crypt_out.reset(); this->crypt_out.reset();
} }
+3 -2
View File
@@ -13,7 +13,7 @@ struct Channel {
std::unique_ptr<struct bufferevent, void (*)(struct bufferevent*)> bev; std::unique_ptr<struct bufferevent, void (*)(struct bufferevent*)> bev;
struct sockaddr_storage local_addr; struct sockaddr_storage local_addr;
struct sockaddr_storage remote_addr; struct sockaddr_storage remote_addr;
bool is_virtual_connection; uint64_t virtual_network_id; // 0 = normal TCP connection
Version version; Version version;
uint8_t language; uint8_t language;
@@ -50,6 +50,7 @@ struct Channel {
// Creates a connected channel // Creates a connected channel
Channel( Channel(
struct bufferevent* bev, struct bufferevent* bev,
uint64_t virtual_network_id,
Version version, Version version,
uint8_t language, uint8_t language,
on_command_received_t on_command_received, on_command_received_t on_command_received,
@@ -70,7 +71,7 @@ struct Channel {
void* context_obj, void* context_obj,
const std::string& name = ""); const std::string& name = "");
void set_bufferevent(struct bufferevent* bev); void set_bufferevent(struct bufferevent* bev, uint64_t virtual_network_id);
inline bool connected() const { inline bool connected() const {
return this->bev.get() != nullptr; return this->bev.get() != nullptr;
+382 -101
View File
@@ -38,11 +38,11 @@ private:
std::string user_msg; std::string user_msg;
}; };
static void check_license_flag(shared_ptr<Client> c, License::Flag flag) { static void check_account_flag(shared_ptr<Client> c, Account::Flag flag) {
if (!c->license) { if (!c->login) {
throw precondition_failed("$C6You are not\nlogged in."); throw precondition_failed("$C6You are not\nlogged in.");
} }
if (!c->license->check_flag(flag)) { if (!c->login->account->check_flag(flag)) {
throw precondition_failed("$C6You do not have\npermission to\nrun this command."); throw precondition_failed("$C6You do not have\npermission to\nrun this command.");
} }
} }
@@ -65,21 +65,29 @@ static void check_is_ep3(shared_ptr<Client> c, bool is_ep3) {
} }
} }
static void check_debug_enabled(shared_ptr<Client> c) {
if (!c->config.check_flag(Client::Flag::DEBUG_ENABLED)) {
throw precondition_failed("$C6This command can only\nbe run in debug mode\n(run %sdebug first)");
}
}
static void check_cheats_enabled(shared_ptr<Lobby> l, shared_ptr<Client> c) { static void check_cheats_enabled(shared_ptr<Lobby> l, shared_ptr<Client> c) {
if (!l->check_flag(Lobby::Flag::CHEATS_ENABLED) && !c->license->check_flag(License::Flag::CHEAT_ANYWHERE)) { if (!l->check_flag(Lobby::Flag::CHEATS_ENABLED) &&
!c->login->account->check_flag(Account::Flag::CHEAT_ANYWHERE)) {
throw precondition_failed("$C6This command can\nonly be used in\ncheat mode."); throw precondition_failed("$C6This command can\nonly be used in\ncheat mode.");
} }
} }
static void check_cheats_allowed(shared_ptr<ServerState> s, shared_ptr<Client> c) { static void check_cheats_allowed(shared_ptr<ServerState> s, shared_ptr<Client> c) {
if ((s->cheat_mode_behavior == ServerState::BehaviorSwitch::OFF) && !c->license->check_flag(License::Flag::CHEAT_ANYWHERE)) { if ((s->cheat_mode_behavior == ServerState::BehaviorSwitch::OFF) &&
!c->login->account->check_flag(Account::Flag::CHEAT_ANYWHERE)) {
throw precondition_failed("$C6Cheats are disabled\non this server."); throw precondition_failed("$C6Cheats are disabled\non this server.");
} }
} }
static void check_cheats_allowed(shared_ptr<ServerState> s, shared_ptr<ProxyServer::LinkedSession> ses) { static void check_cheats_allowed(shared_ptr<ServerState> s, shared_ptr<ProxyServer::LinkedSession> ses) {
if ((s->cheat_mode_behavior == ServerState::BehaviorSwitch::OFF) && if ((s->cheat_mode_behavior == ServerState::BehaviorSwitch::OFF) &&
(!ses->license || !ses->license->check_flag(License::Flag::CHEAT_ANYWHERE))) { (!ses->login || !ses->login->account->check_flag(Account::Flag::CHEAT_ANYWHERE))) {
throw precondition_failed("$C6Cheats are disabled\non this proxy."); throw precondition_failed("$C6Cheats are disabled\non this proxy.");
} }
} }
@@ -255,19 +263,19 @@ static void proxy_command_lobby_info(shared_ptr<ProxyServer::LinkedSession> ses,
} }
static void server_command_ax(shared_ptr<Client> c, const std::string& args) { static void server_command_ax(shared_ptr<Client> c, const std::string& args) {
check_license_flag(c, License::Flag::ANNOUNCE); check_account_flag(c, Account::Flag::ANNOUNCE);
ax_messages_log.info("%s", args.c_str()); ax_messages_log.info("%s", args.c_str());
} }
static void server_command_announce(shared_ptr<Client> c, const std::string& args) { static void server_command_announce(shared_ptr<Client> c, const std::string& args) {
auto s = c->require_server_state(); auto s = c->require_server_state();
check_license_flag(c, License::Flag::ANNOUNCE); check_account_flag(c, Account::Flag::ANNOUNCE);
send_text_message(s, args); send_text_message(s, args);
} }
static void server_command_announce_mail(shared_ptr<Client> c, const std::string& args) { static void server_command_announce_mail(shared_ptr<Client> c, const std::string& args) {
auto s = c->require_server_state(); auto s = c->require_server_state();
check_license_flag(c, License::Flag::ANNOUNCE); check_account_flag(c, Account::Flag::ANNOUNCE);
send_simple_mail(s, 0, s->name, args); send_simple_mail(s, 0, s->name, args);
} }
@@ -284,27 +292,35 @@ static void proxy_command_arrow(shared_ptr<ProxyServer::LinkedSession> ses, cons
} }
static void server_command_debug(shared_ptr<Client> c, const std::string&) { static void server_command_debug(shared_ptr<Client> c, const std::string&) {
check_license_flag(c, License::Flag::DEBUG); check_account_flag(c, Account::Flag::DEBUG);
c->config.toggle_flag(Client::Flag::DEBUG_ENABLED); c->config.toggle_flag(Client::Flag::DEBUG_ENABLED);
send_text_message_printf(c, "Debug %s", (c->config.check_flag(Client::Flag::DEBUG_ENABLED) ? "enabled" : "disabled")); send_text_message_printf(c, "Debug %s", (c->config.check_flag(Client::Flag::DEBUG_ENABLED) ? "enabled" : "disabled"));
} }
static void server_command_quest(shared_ptr<Client> c, const std::string& args) { static void server_command_quest(shared_ptr<Client> c, const std::string& args) {
if (!c->config.check_flag(Client::Flag::DEBUG_ENABLED)) { auto s = c->require_server_state();
send_text_message(c, "$C6This command can only\nbe run in debug mode\n(run %sdebug first)"); auto l = c->require_lobby();
return; if (!l->is_game()) {
throw precondition_failed("$C6Quests cannot be\nstarted from the\nlobby");
} }
Version effective_version = is_ep3(c->version()) ? Version::GC_V3 : c->version(); Version effective_version = is_ep3(c->version()) ? Version::GC_V3 : c->version();
auto s = c->require_server_state();
auto l = c->require_lobby();
auto q = s->quest_index(effective_version)->get(stoul(args)); auto q = s->quest_index(effective_version)->get(stoul(args));
if (!q) { if (!q) {
send_text_message(c, "$C6Quest not found"); send_text_message(c, "$C6Quest not found");
} else { return;
set_lobby_quest(c->require_lobby(), q, true);
} }
if (!c->config.check_flag(Client::Flag::DEBUG_ENABLED)) {
if (l->count_clients() > 1) {
throw precondition_failed("$C6This command can only\nbe used with no\nother players present");
}
if (!q->allow_start_from_chat_command) {
throw precondition_failed("$C6This quest cannot\nbe started with the\n%squest command");
}
}
set_lobby_quest(c->require_lobby(), q, true);
} }
static void server_command_qcheck(shared_ptr<Client> c, const std::string& args) { static void server_command_qcheck(shared_ptr<Client> c, const std::string& args) {
@@ -329,11 +345,126 @@ static void server_command_qcheck(shared_ptr<Client> c, const std::string& args)
} }
} }
static void server_command_qset_qclear(shared_ptr<Client> c, const std::string& args, bool should_set) { static void server_command_swset_swclear(shared_ptr<Client> c, const std::string& args, bool should_set) {
if (!c->config.check_flag(Client::Flag::DEBUG_ENABLED)) { check_debug_enabled(c);
send_text_message(c, "$C6This command can only\nbe run in debug mode\n(run %sdebug first)"); auto l = c->require_lobby();
if (!l->is_game()) {
send_text_message(c, "$C6This command cannot\nbe used in the lobby");
return; return;
} }
auto tokens = split(args, ' ');
uint8_t floor, flag_num;
if (tokens.size() == 1) {
floor = c->floor;
flag_num = stoul(tokens[0], nullptr, 0);
} else if (tokens.size() == 2) {
floor = stoul(tokens[0], nullptr, 0);
flag_num = stoul(tokens[1], nullptr, 0);
} else {
send_text_message(c, "$C4Incorrect parameters");
return;
}
if (should_set) {
l->switch_flags->set(floor, flag_num);
} else {
l->switch_flags->clear(floor, flag_num);
}
uint8_t cmd_flags = should_set ? 0x01 : 0x00;
G_SwitchStateChanged_6x05 cmd = {{0x05, 0x03, 0xFFFF}, 0, 0, flag_num, floor, cmd_flags};
send_command_t(l, 0x60, 0x00, cmd);
}
static void server_command_swset(shared_ptr<Client> c, const std::string& args) {
return server_command_swset_swclear(c, args, true);
}
static void server_command_swclear(shared_ptr<Client> c, const std::string& args) {
return server_command_swset_swclear(c, args, false);
}
static void proxy_command_swset_swclear(shared_ptr<ProxyServer::LinkedSession> ses, const std::string& args, bool should_set) {
if (!ses->is_in_game) {
send_text_message(ses->client_channel, "$C6This command cannot\nbe used in the lobby");
return;
}
auto tokens = split(args, ' ');
uint8_t floor, flag_num;
if (tokens.size() == 1) {
floor = ses->floor;
flag_num = stoul(tokens[0], nullptr, 0);
} else if (tokens.size() == 2) {
floor = stoul(tokens[0], nullptr, 0);
flag_num = stoul(tokens[1], nullptr, 0);
} else {
send_text_message(ses->client_channel, "$C4Incorrect parameters");
return;
}
uint8_t cmd_flags = should_set ? 0x01 : 0x00;
G_SwitchStateChanged_6x05 cmd = {{0x05, 0x03, 0xFFFF}, 0, 0, flag_num, floor, cmd_flags};
ses->client_channel.send(0x60, 0x00, &cmd, sizeof(cmd));
ses->server_channel.send(0x60, 0x00, &cmd, sizeof(cmd));
}
static void proxy_command_swset(shared_ptr<ProxyServer::LinkedSession> ses, const std::string& args) {
return proxy_command_swset_swclear(ses, args, true);
}
static void proxy_command_swclear(shared_ptr<ProxyServer::LinkedSession> ses, const std::string& args) {
return proxy_command_swset_swclear(ses, args, false);
}
static void server_command_swsetall(shared_ptr<Client> c, const std::string&) {
check_debug_enabled(c);
auto l = c->require_lobby();
if (!l->is_game()) {
send_text_message(c, "$C6This command cannot\nbe used in the lobby");
return;
}
l->switch_flags->data[c->floor].clear(0xFF);
parray<G_SwitchStateChanged_6x05, 0x100> cmds;
for (size_t z = 0; z < cmds.size(); z++) {
auto& cmd = cmds[z];
cmd.header.subcommand = 0x05;
cmd.header.size = 0x03;
cmd.header.object_id = 0xFFFF;
cmd.switch_flag_floor = c->floor;
cmd.switch_flag_num = z;
cmd.flags = 0x01;
}
cmds[0].flags = 0x03; // Play room unlock sound
send_command_t(l, 0x6C, 0x00, cmds);
}
static void proxy_command_swsetall(shared_ptr<ProxyServer::LinkedSession> ses, const std::string&) {
if (!ses->is_in_game) {
send_text_message(ses->client_channel, "$C6This command cannot\nbe used in the lobby");
return;
}
parray<G_SwitchStateChanged_6x05, 0x100> cmds;
for (size_t z = 0; z < cmds.size(); z++) {
auto& cmd = cmds[z];
cmd.header.subcommand = 0x05;
cmd.header.size = 0x03;
cmd.header.object_id = 0xFFFF;
cmd.switch_flag_floor = ses->floor;
cmd.switch_flag_num = z;
cmd.flags = 0x01;
}
cmds[0].flags = 0x03; // Play room unlock sound
ses->client_channel.send(0x6C, 0x00, &cmds, sizeof(cmds));
ses->server_channel.send(0x6C, 0x00, &cmds, sizeof(cmds));
}
static void server_command_qset_qclear(shared_ptr<Client> c, const std::string& args, bool should_set) {
check_debug_enabled(c);
auto l = c->require_lobby(); auto l = c->require_lobby();
if (!l->is_game()) { if (!l->is_game()) {
send_text_message(c, "$C6This command cannot\nbe used in the lobby"); send_text_message(c, "$C6This command cannot\nbe used in the lobby");
@@ -351,8 +482,10 @@ static void server_command_qset_qclear(shared_ptr<Client> c, const std::string&
} else { } else {
l->quest_flag_values->clear(l->difficulty, flag_num); l->quest_flag_values->clear(l->difficulty, flag_num);
} }
} else if (c->version() == Version::BB_V4) { }
auto p = c->character();
auto p = c->character(false);
if (p) {
if (should_set) { if (should_set) {
p->quest_flags.set(l->difficulty, flag_num); p->quest_flags.set(l->difficulty, flag_num);
} else { } else {
@@ -409,8 +542,38 @@ static void server_command_qgread(shared_ptr<Client> c, const std::string& args)
if (flag_num >= flags.size()) { if (flag_num >= flags.size()) {
send_text_message_printf(c, "$C7Flag number must be\nless than %zu", flags.size()); send_text_message_printf(c, "$C7Flag number must be\nless than %zu", flags.size());
} else { } else {
send_text_message_printf(c, "$C7Quest counter %hhu\nhas value %" PRIu32, send_text_message_printf(c, "$C7Quest counter %hhu\nhas value %" PRIu32, flag_num, flags[flag_num].load());
flag_num, flags[flag_num].load()); }
}
static void server_command_qfread(shared_ptr<Client> c, const std::string& args) {
auto s = c->require_server_state();
uint8_t counter_index;
uint32_t mask;
try {
const auto& def = s->quest_counter_fields.at(args);
counter_index = def.first;
mask = def.second;
} catch (const out_of_range&) {
send_text_message(c, "$C4Invalid field name");
return;
}
if (mask == 0) {
throw runtime_error("invalid quest counter definition");
}
uint32_t counter_value = c->character()->quest_counters.at(counter_index) & mask;
while (!(mask & 1)) {
mask >>= 1;
counter_value >>= 1;
}
if (mask == 1) {
send_text_message_printf(c, "$C7Field %s\nhas value %s", args.c_str(), counter_value ? "TRUE" : "FALSE");
} else {
send_text_message_printf(c, "$C7Field %s\nhas value %" PRIu32, args.c_str(), counter_value);
} }
} }
@@ -419,10 +582,7 @@ static void server_command_qgwrite(shared_ptr<Client> c, const std::string& args
send_text_message(c, "$C6This command can\nonly be used on BB"); send_text_message(c, "$C6This command can\nonly be used on BB");
return; return;
} }
if (!c->config.check_flag(Client::Flag::DEBUG_ENABLED)) { check_debug_enabled(c);
send_text_message(c, "$C6This command can only\nbe run in debug mode\n(run %sdebug first)");
return;
}
auto l = c->require_lobby(); auto l = c->require_lobby();
if (!l->is_game()) { if (!l->is_game()) {
send_text_message(c, "$C6This command cannot\nbe used in the lobby"); send_text_message(c, "$C6This command cannot\nbe used in the lobby");
@@ -449,10 +609,7 @@ static void server_command_qgwrite(shared_ptr<Client> c, const std::string& args
} }
static void server_command_qsync_qsyncall(shared_ptr<Client> c, const std::string& args, bool send_to_lobby) { static void server_command_qsync_qsyncall(shared_ptr<Client> c, const std::string& args, bool send_to_lobby) {
if (!c->config.check_flag(Client::Flag::DEBUG_ENABLED)) { check_debug_enabled(c);
send_text_message(c, "$C6This command can only\nbe run in debug mode\n(run %sdebug first)");
return;
}
auto l = c->require_lobby(); auto l = c->require_lobby();
if (!l->is_game()) { if (!l->is_game()) {
send_text_message(c, "$C6This command cannot\nbe used in the lobby"); send_text_message(c, "$C6This command cannot\nbe used in the lobby");
@@ -465,7 +622,7 @@ static void server_command_qsync_qsyncall(shared_ptr<Client> c, const std::strin
return; return;
} }
G_SyncQuestData_6x77 cmd; G_SyncQuestRegister_6x77 cmd;
cmd.header = {0x77, 0x03, 0x0000}; cmd.header = {0x77, 0x03, 0x0000};
cmd.register_number = stoul(tokens[0].substr(1), nullptr, 0); cmd.register_number = stoul(tokens[0].substr(1), nullptr, 0);
cmd.unused = 0; cmd.unused = 0;
@@ -504,7 +661,7 @@ static void proxy_command_qsync_qsyncall(shared_ptr<ProxyServer::LinkedSession>
return; return;
} }
G_SyncQuestData_6x77 cmd; G_SyncQuestRegister_6x77 cmd;
cmd.header = {0x77, 0x03, 0x0000}; cmd.header = {0x77, 0x03, 0x0000};
cmd.register_number = stoul(tokens[0].substr(1), nullptr, 0); cmd.register_number = stoul(tokens[0].substr(1), nullptr, 0);
cmd.unused = 0; cmd.unused = 0;
@@ -531,10 +688,7 @@ static void proxy_command_qsyncall(shared_ptr<ProxyServer::LinkedSession> ses, c
} }
static void server_command_qcall(shared_ptr<Client> c, const std::string& args) { static void server_command_qcall(shared_ptr<Client> c, const std::string& args) {
if (!c->config.check_flag(Client::Flag::DEBUG_ENABLED)) { check_debug_enabled(c);
send_text_message(c, "$C6This command can only\nbe run in debug mode\n(run %sdebug first)");
return;
}
auto l = c->require_lobby(); auto l = c->require_lobby();
if (l->is_game() && l->quest) { if (l->is_game() && l->quest) {
@@ -571,7 +725,7 @@ static void server_command_show_material_counts(shared_ptr<Client> c, const std:
} }
static void server_command_auction(shared_ptr<Client> c, const std::string&) { static void server_command_auction(shared_ptr<Client> c, const std::string&) {
check_license_flag(c, License::Flag::DEBUG); check_account_flag(c, Account::Flag::DEBUG);
auto l = c->require_lobby(); auto l = c->require_lobby();
if (l->is_game() && l->is_ep3()) { if (l->is_game() && l->is_ep3()) {
G_InitiateCardAuction_Ep3_6xB5x42 cmd; G_InitiateCardAuction_Ep3_6xB5x42 cmd;
@@ -628,8 +782,7 @@ static void proxy_command_patch(shared_ptr<ProxyServer::LinkedSession> ses, cons
auto send_version_detect_or_send_call = [args, ses, send_call]() { auto send_version_detect_or_send_call = [args, ses, send_call]() {
bool is_gc = ::is_gc(ses->version()); bool is_gc = ::is_gc(ses->version());
bool is_xb = (ses->version() == Version::XB_V3); bool is_xb = (ses->version() == Version::XB_V3);
if ((is_gc || is_xb) && if ((is_gc || is_xb) && specific_version_is_indeterminate(ses->config.specific_version)) {
ses->config.specific_version == default_specific_version_for_version(ses->version(), -1)) {
auto s = ses->require_server_state(); auto s = ses->require_server_state();
send_function_call( send_function_call(
ses->client_channel, ses->client_channel,
@@ -742,11 +895,19 @@ static void proxy_command_get_player_card(shared_ptr<ProxyServer::LinkedSession>
} }
static void server_command_send_client(shared_ptr<Client> c, const std::string& args) { static void server_command_send_client(shared_ptr<Client> c, const std::string& args) {
check_debug_enabled(c);
string data = parse_data_string(args); string data = parse_data_string(args);
data.resize((data.size() + 3) & (~3)); data.resize((data.size() + 3) & (~3));
c->channel.send(data); c->channel.send(data);
} }
static void server_command_send_server(shared_ptr<Client> c, const std::string& args) {
check_debug_enabled(c);
string data = parse_data_string(args);
data.resize((data.size() + 3) & (~3));
on_command_with_header(c, data);
}
static void proxy_command_send_client(shared_ptr<ProxyServer::LinkedSession> ses, const std::string& args) { static void proxy_command_send_client(shared_ptr<ProxyServer::LinkedSession> ses, const std::string& args) {
string data = parse_data_string(args); string data = parse_data_string(args);
data.resize((data.size() + 3) & (~3)); data.resize((data.size() + 3) & (~3));
@@ -759,6 +920,16 @@ static void proxy_command_send_server(shared_ptr<ProxyServer::LinkedSession> ses
ses->server_channel.send(data); ses->server_channel.send(data);
} }
static void server_command_send_both(shared_ptr<Client> c, const std::string& args) {
server_command_send_client(c, args);
server_command_send_server(c, args);
}
static void proxy_command_send_both(shared_ptr<ProxyServer::LinkedSession> ses, const std::string& args) {
proxy_command_send_client(ses, args);
proxy_command_send_server(ses, args);
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Lobby commands // Lobby commands
@@ -773,7 +944,7 @@ static void server_command_cheat(shared_ptr<Client> c, const std::string&) {
l->toggle_flag(Lobby::Flag::CHEATS_ENABLED); l->toggle_flag(Lobby::Flag::CHEATS_ENABLED);
send_text_message_printf(l, "Cheat mode %s", l->check_flag(Lobby::Flag::CHEATS_ENABLED) ? "enabled" : "disabled"); send_text_message_printf(l, "Cheat mode %s", l->check_flag(Lobby::Flag::CHEATS_ENABLED) ? "enabled" : "disabled");
if (!c->license->check_flag(License::Flag::CHEAT_ANYWHERE)) { if (!c->login->account->check_flag(Account::Flag::CHEAT_ANYWHERE)) {
size_t default_min_level = s->default_min_level_for_game(l->base_version, l->episode, l->difficulty); size_t default_min_level = s->default_min_level_for_game(l->base_version, l->episode, l->difficulty);
if (l->min_level < default_min_level) { if (l->min_level < default_min_level) {
l->min_level = default_min_level; l->min_level = default_min_level;
@@ -786,7 +957,7 @@ static void server_command_cheat(shared_ptr<Client> c, const std::string&) {
static void server_command_lobby_event(shared_ptr<Client> c, const std::string& args) { static void server_command_lobby_event(shared_ptr<Client> c, const std::string& args) {
auto l = c->require_lobby(); auto l = c->require_lobby();
check_is_game(l, false); check_is_game(l, false);
check_license_flag(c, License::Flag::CHANGE_EVENT); check_account_flag(c, Account::Flag::CHANGE_EVENT);
uint8_t new_event = event_for_name(args); uint8_t new_event = event_for_name(args);
if (new_event == 0xFF) { if (new_event == 0xFF) {
@@ -815,7 +986,7 @@ static void proxy_command_lobby_event(shared_ptr<ProxyServer::LinkedSession> ses
} }
static void server_command_lobby_event_all(shared_ptr<Client> c, const std::string& args) { static void server_command_lobby_event_all(shared_ptr<Client> c, const std::string& args) {
check_license_flag(c, License::Flag::CHANGE_EVENT); check_account_flag(c, Account::Flag::CHANGE_EVENT);
uint8_t new_event = event_for_name(args); uint8_t new_event = event_for_name(args);
if (new_event == 0xFF) { if (new_event == 0xFF) {
@@ -867,13 +1038,13 @@ static void proxy_command_lobby_type(shared_ptr<ProxyServer::LinkedSession> ses,
ses->config.override_lobby_number = new_type; ses->config.override_lobby_number = new_type;
} }
static string file_path_for_recording(const std::string& args, uint32_t serial_number) { static string file_path_for_recording(const std::string& args, uint32_t account_id) {
for (char ch : args) { for (char ch : args) {
if (ch <= 0x20 || ch > 0x7E || ch == '/') { if (ch <= 0x20 || ch > 0x7E || ch == '/') {
throw runtime_error("invalid recording name"); throw runtime_error("invalid recording name");
} }
} }
return string_printf("system/ep3/battle-records/%010" PRIu32 "_%s.mzrd", serial_number, args.c_str()); return string_printf("system/ep3/battle-records/%010" PRIu32 "_%s.mzrd", account_id, args.c_str());
} }
static void server_command_saverec(shared_ptr<Client> c, const std::string& args) { static void server_command_saverec(shared_ptr<Client> c, const std::string& args) {
@@ -881,7 +1052,7 @@ static void server_command_saverec(shared_ptr<Client> c, const std::string& args
send_text_message(c, "$C4No finished\nrecording is\npresent"); send_text_message(c, "$C4No finished\nrecording is\npresent");
return; return;
} }
string file_path = file_path_for_recording(args, c->license->serial_number); string file_path = file_path_for_recording(args, c->login->account->account_id);
string data = c->ep3_prev_battle_record->serialize(); string data = c->ep3_prev_battle_record->serialize();
save_file(file_path, data); save_file(file_path, data);
send_text_message(c, "$C7Recording saved"); send_text_message(c, "$C7Recording saved");
@@ -898,7 +1069,7 @@ static void server_command_playrec(shared_ptr<Client> c, const std::string& args
if (l->is_game() && l->battle_player) { if (l->is_game() && l->battle_player) {
l->battle_player->start(); l->battle_player->start();
} else if (!l->is_game()) { } else if (!l->is_game()) {
string file_path = file_path_for_recording(args, c->license->serial_number); string file_path = file_path_for_recording(args, c->login->account->account_id);
auto s = c->require_server_state(); auto s = c->require_server_state();
string filename = args; string filename = args;
@@ -924,6 +1095,7 @@ static void server_command_playrec(shared_ptr<Client> c, const std::string& args
} }
s->change_client_lobby(c, game); s->change_client_lobby(c, game);
c->config.set_flag(Client::Flag::LOADING); c->config.set_flag(Client::Flag::LOADING);
c->log.info("LOADING flag set");
} }
} else { } else {
send_text_message(c, "$C4This command cannot\nbe used in a game"); send_text_message(c, "$C4This command cannot\nbe used in a game");
@@ -932,17 +1104,14 @@ static void server_command_playrec(shared_ptr<Client> c, const std::string& args
static void server_command_meseta(shared_ptr<Client> c, const std::string& args) { static void server_command_meseta(shared_ptr<Client> c, const std::string& args) {
check_is_ep3(c, true); check_is_ep3(c, true);
if (!c->config.check_flag(Client::Flag::DEBUG_ENABLED)) { check_debug_enabled(c);
send_text_message(c, "$C6This command can only\nbe run in debug mode\n(run %sdebug first)");
return;
}
uint32_t amount = stoul(args, nullptr, 0); uint32_t amount = stoul(args, nullptr, 0);
c->license->ep3_current_meseta += amount; c->login->account->ep3_current_meseta += amount;
c->license->ep3_total_meseta_earned += amount; c->login->account->ep3_total_meseta_earned += amount;
c->license->save(); c->login->account->save();
send_ep3_rank_update(c); send_ep3_rank_update(c);
send_text_message_printf(c, "You now have\n$C6%" PRIu32 "$C7 Meseta", c->license->ep3_current_meseta); send_text_message_printf(c, "You now have\n$C6%" PRIu32 "$C7 Meseta", c->login->account->ep3_current_meseta);
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@@ -1093,7 +1262,8 @@ static void server_command_min_level(shared_ptr<Client> c, const std::string& ar
size_t new_min_level = stoull(args) - 1; size_t new_min_level = stoull(args) - 1;
auto s = c->require_server_state(); auto s = c->require_server_state();
bool cheats_allowed = (l->check_flag(Lobby::Flag::CHEATS_ENABLED) || c->license->check_flag(License::Flag::CHEAT_ANYWHERE)); bool cheats_allowed = (l->check_flag(Lobby::Flag::CHEATS_ENABLED) ||
c->login->account->check_flag(Account::Flag::CHEAT_ANYWHERE));
if (!cheats_allowed) { if (!cheats_allowed) {
size_t default_min_level = s->default_min_level_for_game(l->base_version, l->episode, l->difficulty); size_t default_min_level = s->default_min_level_for_game(l->base_version, l->episode, l->difficulty);
if (new_min_level < default_min_level) { if (new_min_level < default_min_level) {
@@ -1135,7 +1305,7 @@ static void server_command_edit(shared_ptr<Client> c, const std::string& args) {
} }
bool cheats_allowed = ((s->cheat_mode_behavior != ServerState::BehaviorSwitch::OFF) || bool cheats_allowed = ((s->cheat_mode_behavior != ServerState::BehaviorSwitch::OFF) ||
c->license->check_flag(License::Flag::CHEAT_ANYWHERE)); c->login->account->check_flag(Account::Flag::CHEAT_ANYWHERE));
string encoded_args = tolower(args); string encoded_args = tolower(args);
vector<string> tokens = split(encoded_args, ' '); vector<string> tokens = split(encoded_args, ' ');
@@ -1162,12 +1332,25 @@ static void server_command_edit(shared_ptr<Client> c, const std::string& args) {
p->disp.stats.experience = stoul(tokens.at(1)); p->disp.stats.experience = stoul(tokens.at(1));
} else if (tokens.at(0) == "level" && cheats_allowed) { } else if (tokens.at(0) == "level" && cheats_allowed) {
uint32_t level = stoul(tokens.at(1)) - 1; uint32_t level = stoul(tokens.at(1)) - 1;
p->disp.stats.reset_to_base(p->disp.visual.char_class, s->level_table); auto level_table = s->level_table(c->version());
p->disp.stats.advance_to_level(p->disp.visual.char_class, level, s->level_table); level_table->reset_to_base(p->disp.stats, p->disp.visual.char_class);
level_table->advance_to_level(p->disp.stats, level, p->disp.visual.char_class);
} else if (tokens.at(0) == "namecolor") { } else if (tokens.at(0) == "namecolor") {
uint32_t new_color; uint32_t new_color;
sscanf(tokens.at(1).c_str(), "%8X", &new_color); sscanf(tokens.at(1).c_str(), "%8X", &new_color);
p->disp.visual.name_color = new_color; p->disp.visual.name_color = new_color;
} else if (tokens.at(0) == "language" || tokens.at(0) == "lang") {
if (tokens.at(1).size() != 1) {
throw runtime_error("invalid language");
}
uint8_t new_language = language_code_for_char(tokens.at(1).at(0));
c->channel.language = new_language;
p->inventory.language = new_language;
p->guild_card.language = new_language;
auto sys = c->system_file(false);
if (sys) {
sys->base.language = new_language;
}
} else if (tokens.at(0) == "secid" && cheats_allowed) { } else if (tokens.at(0) == "secid" && cheats_allowed) {
uint8_t secid = section_id_for_name(tokens.at(1)); uint8_t secid = section_id_for_name(tokens.at(1));
if (secid == 0xFF) { if (secid == 0xFF) {
@@ -1266,8 +1449,12 @@ static void server_command_bbchar_savechar(shared_ptr<Client> c, const std::stri
auto l = c->require_lobby(); auto l = c->require_lobby();
check_is_game(l, false); check_is_game(l, false);
if (is_bb_conversion && is_ep3(c->version())) {
send_text_message(c, "$C6Episode 3 players\ncannot be converted\nto BB format");
return;
}
auto pending_export = make_unique<Client::PendingCharacterExport>(); auto pending_export = make_unique<Client::PendingCharacterExport>();
pending_export->is_bb_conversion = is_bb_conversion;
if (is_bb_conversion) { if (is_bb_conversion) {
vector<string> tokens = split(args, ' '); vector<string> tokens = split(args, ' ');
@@ -1284,7 +1471,9 @@ static void server_command_bbchar_savechar(shared_ptr<Client> c, const std::stri
} }
try { try {
c->pending_character_export->license = s->license_index->verify_bb(tokens[0].c_str(), tokens[1].c_str()); auto dest_login = s->account_index->from_bb_credentials(tokens[0], &tokens[1], false);
pending_export->dest_account = dest_login->account;
pending_export->dest_bb_license = dest_login->bb_license;
} catch (const exception& e) { } catch (const exception& e) {
send_text_message_printf(c, "$C6Login failed: %s", e.what()); send_text_message_printf(c, "$C6Login failed: %s", e.what());
return; return;
@@ -1292,17 +1481,18 @@ static void server_command_bbchar_savechar(shared_ptr<Client> c, const std::stri
} else { } else {
pending_export->character_index = stoll(args) - 1; pending_export->character_index = stoll(args) - 1;
if ((pending_export->character_index > 3) || (pending_export->character_index < 0)) { if ((pending_export->character_index > 15) || (pending_export->character_index < 0)) {
send_text_message(c, "$C6Player index must\nbe in range 1-4"); send_text_message(c, "$C6Player index must\nbe in range 1-16");
return; return;
} }
pending_export->license = c->license; pending_export->dest_account = c->login->account;
} }
c->pending_character_export = std::move(pending_export); c->pending_character_export = std::move(pending_export);
// Request the player data. The client will respond with a 61, and the handler
// for that command will execute the conversion // Request the player data. The client will respond with a 61 or 30, and the
send_get_player_info(c); // handler for either of those commands will execute the conversion
send_get_player_info(c, true);
} }
static void server_command_bbchar(shared_ptr<Client> c, const std::string& args) { static void server_command_bbchar(shared_ptr<Client> c, const std::string& args) {
@@ -1310,35 +1500,115 @@ static void server_command_bbchar(shared_ptr<Client> c, const std::string& args)
} }
static void server_command_savechar(shared_ptr<Client> c, const std::string& args) { static void server_command_savechar(shared_ptr<Client> c, const std::string& args) {
if (c->license->check_flag(License::Flag::IS_SHARED_SERIAL)) { if (c->login->account->check_flag(Account::Flag::IS_SHARED_ACCOUNT)) {
send_text_message(c, "$C7This command cannot\nbe used on a shared\nserial number"); send_text_message(c, "$C7This command cannot\nbe used on a shared\naccount");
return; return;
} }
server_command_bbchar_savechar(c, args, false); server_command_bbchar_savechar(c, args, false);
} }
static void server_command_loadchar(shared_ptr<Client> c, const std::string& args) { static void server_command_loadchar(shared_ptr<Client> c, const std::string& args) {
if (!is_v1_or_v2(c->version())) { if (c->login->account->check_flag(Account::Flag::IS_SHARED_ACCOUNT)) {
send_text_message(c, "$C7This command can only\nbe used on v1 or v2"); send_text_message(c, "$C7This command cannot\nbe used on a shared\naccount");
return;
}
if (c->license->check_flag(License::Flag::IS_SHARED_SERIAL)) {
send_text_message(c, "$C7This command cannot\nbe used on a shared\nserial number");
return; return;
} }
auto l = c->require_lobby(); auto l = c->require_lobby();
check_is_game(l, false); check_is_game(l, false);
size_t index = stoull(args, nullptr, 0) - 1; size_t index = stoull(args, nullptr, 0) - 1;
if (index > 3) { if (index > 15) {
send_text_message(c, "$C6Player index must\nbe in range 1-4"); send_text_message(c, "$C6Player index must\nbe in range 1-16");
return; return;
} }
c->load_backup_character(c->license->serial_number, index);
shared_ptr<PSOGCEp3CharacterFile::Character> ep3_char;
if (is_ep3(c->version())) {
ep3_char = c->load_ep3_backup_character(c->login->account->account_id, index);
} else {
c->load_backup_character(c->login->account->account_id, index);
}
if (c->version() == Version::BB_V4) {
// On BB, it suffices to simply send the character file again
auto s = c->require_server_state();
send_complete_player_bb(c);
send_player_leave_notification(l, c->lobby_client_id);
s->send_lobby_join_notifications(l, c);
} else if ((c->version() == Version::DC_V2) ||
(c->version() == Version::GC_NTE) ||
(c->version() == Version::GC_V3) ||
(c->version() == Version::GC_EP3_NTE) ||
(c->version() == Version::GC_EP3) ||
(c->version() == Version::XB_V3)) {
// TODO: Support extended player info on other versions
auto s = c->require_server_state();
if (!c->config.check_flag(Client::Flag::HAS_SEND_FUNCTION_CALL) ||
c->config.check_flag(Client::Flag::SEND_FUNCTION_CALL_CHECKSUM_ONLY)) {
send_text_message_printf(c, "Can\'t load character\ndata on this game\nversion");
return;
}
auto send_set_extended_player_info = []<typename CharT>(shared_ptr<Client> c, shared_ptr<const CharT> char_file) -> void {
prepare_client_for_patches(c, [wc = weak_ptr<Client>(c), char_file]() {
auto c = wc.lock();
if (!c) {
return;
}
try {
auto s = c->require_server_state();
auto fn = s->function_code_index->get_patch("SetExtendedPlayerInfo", c->config.specific_version);
send_function_call(c, fn, {}, char_file.get(), sizeof(CharT));
c->function_call_response_queue.emplace_back([wc = weak_ptr<Client>(c)](uint32_t, uint32_t) -> void {
auto c = wc.lock();
if (!c) {
return;
}
auto l = c->lobby.lock();
if (l) {
auto s = c->require_server_state(); auto s = c->require_server_state();
send_player_leave_notification(l, c->lobby_client_id); send_player_leave_notification(l, c->lobby_client_id);
s->send_lobby_join_notifications(l, c); s->send_lobby_join_notifications(l, c);
}
});
} catch (const exception& e) {
c->log.warning("Failed to set extended player info: %s", e.what());
send_text_message_printf(c, "Failed to set\nplayer info:\n%s", e.what());
}
});
};
if (c->version() == Version::DC_V2) {
auto dc_char = make_shared<PSODCV2CharacterFile>(c->character()->to_dc_v2());
send_set_extended_player_info.operator()<PSODCV2CharacterFile>(c, dc_char);
} else if (c->version() == Version::GC_NTE) {
auto gc_char = make_shared<PSOGCNTECharacterFileCharacter>(c->character()->to_gc_nte());
send_set_extended_player_info.operator()<PSOGCNTECharacterFileCharacter>(c, gc_char);
} else if (c->version() == Version::GC_V3) {
auto gc_char = make_shared<PSOGCCharacterFile::Character>(c->character()->to_gc());
send_set_extended_player_info.operator()<PSOGCCharacterFile::Character>(c, gc_char);
} else if (c->version() == Version::GC_EP3_NTE) {
auto nte_char = make_shared<PSOGCEp3NTECharacter>(*ep3_char);
send_set_extended_player_info.operator()<PSOGCEp3NTECharacter>(c, nte_char);
} else if (c->version() == Version::GC_EP3) {
send_set_extended_player_info.operator()<PSOGCEp3CharacterFile::Character>(c, ep3_char);
} else if (c->version() == Version::XB_V3) {
if (!c->login || !c->login->xb_license) {
throw runtime_error("XB client is not logged in");
}
auto xb_char = make_shared<PSOXBCharacterFileCharacter>(c->character()->to_xb(c->login->xb_license->user_id));
send_set_extended_player_info.operator()<PSOXBCharacterFileCharacter>(c, xb_char);
} else {
throw logic_error("unimplemented extended player info version");
}
} else {
// On v1 and v2, the client will assign its character data from the lobby
// join command, so it suffices to just resend the join notification.
auto s = c->require_server_state();
send_player_leave_notification(l, c->lobby_client_id);
s->send_lobby_join_notifications(l, c);
}
} }
static void server_command_save(shared_ptr<Client> c, const std::string&) { static void server_command_save(shared_ptr<Client> c, const std::string&) {
@@ -1361,8 +1631,8 @@ static string name_for_client(shared_ptr<Client> c) {
return escape_player_name(player->disp.name.decode(player->inventory.language)); return escape_player_name(player->disp.name.decode(player->inventory.language));
} }
if (c->license.get()) { if (c->login) {
return string_printf("SN:%" PRIu32, c->license->serial_number); return string_printf("SN:%" PRIu32, c->login->account->account_id);
} }
return "Player"; return "Player";
@@ -1371,16 +1641,16 @@ static string name_for_client(shared_ptr<Client> c) {
static void server_command_silence(shared_ptr<Client> c, const std::string& args) { static void server_command_silence(shared_ptr<Client> c, const std::string& args) {
auto s = c->require_server_state(); auto s = c->require_server_state();
auto l = c->require_lobby(); auto l = c->require_lobby();
check_license_flag(c, License::Flag::SILENCE_USER); check_account_flag(c, Account::Flag::SILENCE_USER);
auto target = s->find_client(&args); auto target = s->find_client(&args);
if (!target->license) { if (!target->login) {
// this should be impossible, but I'll bet it's not actually // this should be impossible, but I'll bet it's not actually
send_text_message(c, "$C6Client not logged in"); send_text_message(c, "$C6Client not logged in");
return; return;
} }
if (target->license->check_flag(License::Flag::SILENCE_USER)) { if (target->login->account->check_flag(Account::Flag::SILENCE_USER)) {
send_text_message(c, "$C6You do not have\nsufficient privileges."); send_text_message(c, "$C6You do not have\nsufficient privileges.");
return; return;
} }
@@ -1394,21 +1664,21 @@ static void server_command_silence(shared_ptr<Client> c, const std::string& args
static void server_command_kick(shared_ptr<Client> c, const std::string& args) { static void server_command_kick(shared_ptr<Client> c, const std::string& args) {
auto s = c->require_server_state(); auto s = c->require_server_state();
auto l = c->require_lobby(); auto l = c->require_lobby();
check_license_flag(c, License::Flag::KICK_USER); check_account_flag(c, Account::Flag::KICK_USER);
auto target = s->find_client(&args); auto target = s->find_client(&args);
if (!target->license) { if (!target->login) {
// This should be impossible, but I'll bet it's not actually // This should be impossible, but I'll bet it's not actually
send_text_message(c, "$C6Client not logged in"); send_text_message(c, "$C6Client not logged in");
return; return;
} }
if (target->license->check_flag(License::Flag::KICK_USER)) { if (target->login->account->check_flag(Account::Flag::KICK_USER)) {
send_text_message(c, "$C6You do not have\nsufficient privileges."); send_text_message(c, "$C6You do not have\nsufficient privileges.");
return; return;
} }
send_message_box(target, "$C6You were kicked off by a moderator."); send_message_box(target, "$C6You have been kicked off the server.");
target->should_disconnect = true; target->should_disconnect = true;
string target_name = name_for_client(target); string target_name = name_for_client(target);
send_text_message_printf(l, "$C6%s kicked off", target_name.c_str()); send_text_message_printf(l, "$C6%s kicked off", target_name.c_str());
@@ -1417,7 +1687,7 @@ static void server_command_kick(shared_ptr<Client> c, const std::string& args) {
static void server_command_ban(shared_ptr<Client> c, const std::string& args) { static void server_command_ban(shared_ptr<Client> c, const std::string& args) {
auto s = c->require_server_state(); auto s = c->require_server_state();
auto l = c->require_lobby(); auto l = c->require_lobby();
check_license_flag(c, License::Flag::BAN_USER); check_account_flag(c, Account::Flag::BAN_USER);
size_t space_pos = args.find(' '); size_t space_pos = args.find(' ');
if (space_pos == string::npos) { if (space_pos == string::npos) {
@@ -1427,13 +1697,13 @@ static void server_command_ban(shared_ptr<Client> c, const std::string& args) {
string identifier = args.substr(space_pos + 1); string identifier = args.substr(space_pos + 1);
auto target = s->find_client(&identifier); auto target = s->find_client(&identifier);
if (!target->license) { if (!target->login) {
// This should be impossible, but I'll bet it's not actually // This should be impossible, but I'll bet it's not actually
send_text_message(c, "$C6Client not logged in"); send_text_message(c, "$C6Client not logged in");
return; return;
} }
if (target->license->check_flag(License::Flag::BAN_USER)) { if (target->login->account->check_flag(Account::Flag::BAN_USER)) {
send_text_message(c, "$C6You do not have\nsufficient privileges."); send_text_message(c, "$C6You do not have\nsufficient privileges.");
return; return;
} }
@@ -1457,9 +1727,9 @@ static void server_command_ban(shared_ptr<Client> c, const std::string& args) {
usecs *= 60 * 60 * 24 * 365; usecs *= 60 * 60 * 24 * 365;
} }
target->license->ban_end_time = now() + usecs; target->login->account->ban_end_time = now() + usecs;
target->license->save(); target->login->account->save();
send_message_box(target, "$C6You were banned by a moderator."); send_message_box(target, "$C6You have been banned.");
target->should_disconnect = true; target->should_disconnect = true;
string target_name = name_for_client(target); string target_name = name_for_client(target);
send_text_message_printf(l, "$C6%s banned", target_name.c_str()); send_text_message_printf(l, "$C6%s banned", target_name.c_str());
@@ -1567,7 +1837,16 @@ static void proxy_command_next(shared_ptr<ProxyServer::LinkedSession> ses, const
} }
static void server_command_where(shared_ptr<Client> c, const std::string&) { static void server_command_where(shared_ptr<Client> c, const std::string&) {
send_text_message_printf(c, "$C7Floor: %02" PRIX32 "\nX: %g\nZ: %g", c->floor, c->x, c->z); auto l = c->require_lobby();
send_text_message_printf(c, "$C7%01" PRIX32 ":%s X:%" PRId32 " Z:%" PRId32,
c->floor, short_name_for_floor(l->episode, c->floor), static_cast<int32_t>(c->x), static_cast<int32_t>(c->z));
for (auto lc : l->clients) {
if (lc && (lc != c)) {
string name = lc->character()->disp.name.decode(lc->language());
send_text_message_printf(c, "$C6%s$C7 %01" PRIX32 ":%s",
name.c_str(), lc->floor, short_name_for_floor(l->episode, lc->floor));
}
}
} }
static void server_command_what(shared_ptr<Client> c, const std::string&) { static void server_command_what(shared_ptr<Client> c, const std::string&) {
@@ -1874,10 +2153,7 @@ static void proxy_command_item(shared_ptr<ProxyServer::LinkedSession> ses, const
} }
static void server_command_enable_ep3_battle_debug_menu(shared_ptr<Client> c, const std::string& args) { static void server_command_enable_ep3_battle_debug_menu(shared_ptr<Client> c, const std::string& args) {
if (!c->config.check_flag(Client::Flag::DEBUG_ENABLED)) { check_debug_enabled(c);
send_text_message(c, "$C6This command can only\nbe run in debug mode\n(run %sdebug first)");
return;
}
auto l = c->require_lobby(); auto l = c->require_lobby();
check_is_game(l, true); check_is_game(l, true);
@@ -2232,6 +2508,7 @@ static const unordered_map<string, ChatCommandDefinition> chat_commands({
{"$qcall", {server_command_qcall, proxy_command_qcall}}, {"$qcall", {server_command_qcall, proxy_command_qcall}},
{"$qcheck", {server_command_qcheck, nullptr}}, {"$qcheck", {server_command_qcheck, nullptr}},
{"$qclear", {server_command_qclear, proxy_command_qclear}}, {"$qclear", {server_command_qclear, proxy_command_qclear}},
{"$qfread", {server_command_qfread, nullptr}},
{"$qgread", {server_command_qgread, nullptr}}, {"$qgread", {server_command_qgread, nullptr}},
{"$qgwrite", {server_command_qgwrite, nullptr}}, {"$qgwrite", {server_command_qgwrite, nullptr}},
{"$qset", {server_command_qset, proxy_command_qset}}, {"$qset", {server_command_qset, proxy_command_qset}},
@@ -2242,6 +2519,7 @@ static const unordered_map<string, ChatCommandDefinition> chat_commands({
{"$save", {server_command_save, nullptr}}, {"$save", {server_command_save, nullptr}},
{"$savechar", {server_command_savechar, nullptr}}, {"$savechar", {server_command_savechar, nullptr}},
{"$saverec", {server_command_saverec, nullptr}}, {"$saverec", {server_command_saverec, nullptr}},
{"$sb", {server_command_send_both, proxy_command_send_both}},
{"$sc", {server_command_send_client, proxy_command_send_client}}, {"$sc", {server_command_send_client, proxy_command_send_client}},
{"$secid", {server_command_secid, proxy_command_secid}}, {"$secid", {server_command_secid, proxy_command_secid}},
{"$setassist", {server_command_ep3_replace_assist_card, nullptr}}, {"$setassist", {server_command_ep3_replace_assist_card, nullptr}},
@@ -2249,10 +2527,13 @@ static const unordered_map<string, ChatCommandDefinition> chat_commands({
{"$silence", {server_command_silence, nullptr}}, {"$silence", {server_command_silence, nullptr}},
{"$song", {server_command_song, proxy_command_song}}, {"$song", {server_command_song, proxy_command_song}},
{"$spec", {server_command_toggle_spectator_flag, nullptr}}, {"$spec", {server_command_toggle_spectator_flag, nullptr}},
{"$ss", {nullptr, proxy_command_send_server}}, {"$ss", {server_command_send_server, proxy_command_send_server}},
{"$stat", {server_command_get_ep3_battle_stat, nullptr}}, {"$stat", {server_command_get_ep3_battle_stat, nullptr}},
{"$surrender", {server_command_surrender, nullptr}}, {"$surrender", {server_command_surrender, nullptr}},
{"$swa", {server_command_switch_assist, proxy_command_switch_assist}}, {"$swa", {server_command_switch_assist, proxy_command_switch_assist}},
{"$swclear", {server_command_swclear, proxy_command_swclear}},
{"$swset", {server_command_swset, proxy_command_swset}},
{"$swsetall", {server_command_swsetall, proxy_command_swsetall}},
{"$unset", {server_command_ep3_unset_field_character, nullptr}}, {"$unset", {server_command_ep3_unset_field_character, nullptr}},
{"$variations", {server_command_variations, nullptr}}, {"$variations", {server_command_variations, nullptr}},
{"$warp", {server_command_warpme, proxy_command_warpme}}, {"$warp", {server_command_warpme, proxy_command_warpme}},
+27 -6
View File
@@ -10,12 +10,16 @@
class Client; class Client;
struct ChoiceSearchConfig { template <bool IsBigEndian>
le_uint32_t disabled = 1; // 0 = enabled, 1 = disabled. Unused in command C3 struct ChoiceSearchConfigT {
using U16T = typename std::conditional<IsBigEndian, be_uint16_t, le_uint16_t>::type;
using U32T = typename std::conditional<IsBigEndian, be_uint32_t, le_uint32_t>::type;
U32T disabled = 1; // 0 = enabled, 1 = disabled. Unused in command C3
struct Entry { struct Entry {
le_uint16_t parent_choice_id = 0; U16T parent_choice_id = 0;
le_uint16_t choice_id = 0; U16T choice_id = 0;
} __attribute__((packed)); } __packed_ws__(Entry, 4);
parray<Entry, 5> entries; parray<Entry, 5> entries;
int32_t get_setting(uint16_t parent_choice_id) const { int32_t get_setting(uint16_t parent_choice_id) const {
@@ -26,7 +30,24 @@ struct ChoiceSearchConfig {
} }
return -1; return -1;
} }
} __attribute__((packed));
operator ChoiceSearchConfigT<!IsBigEndian>() const {
ChoiceSearchConfigT<!IsBigEndian> ret;
ret.disabled = this->disabled.load();
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();
}
return ret;
}
} __packed__;
using ChoiceSearchConfig = ChoiceSearchConfigT<false>;
using ChoiceSearchConfigBE = ChoiceSearchConfigT<true>;
check_struct_size(ChoiceSearchConfig, 0x18);
check_struct_size(ChoiceSearchConfigBE, 0x18);
struct ChoiceSearchCategory { struct ChoiceSearchCategory {
struct Choice { struct Choice {
+164 -96
View File
@@ -11,7 +11,9 @@
#include <phosg/Network.hh> #include <phosg/Network.hh>
#include <phosg/Time.hh> #include <phosg/Time.hh>
#include "IPStackSimulator.hh"
#include "Loggers.hh" #include "Loggers.hh"
#include "SendCommands.hh"
#include "Server.hh" #include "Server.hh"
#include "Version.hh" #include "Version.hh"
@@ -29,6 +31,7 @@ void Client::Config::set_flags_for_version(Version version, int64_t sub_version)
if (version == Version::BB_V4) { if (version == Version::BB_V4) {
this->set_flag(Flag::NO_D6); this->set_flag(Flag::NO_D6);
this->set_flag(Flag::SAVE_ENABLED); this->set_flag(Flag::SAVE_ENABLED);
this->set_flag(Flag::HAS_SEND_FUNCTION_CALL);
this->set_flag(Flag::SEND_FUNCTION_CALL_NO_CACHE_PATCH); this->set_flag(Flag::SEND_FUNCTION_CALL_NO_CACHE_PATCH);
return; return;
} }
@@ -39,29 +42,40 @@ void Client::Config::set_flags_for_version(Version version, int64_t sub_version)
case Version::PC_PATCH: case Version::PC_PATCH:
case Version::BB_PATCH: case Version::BB_PATCH:
this->set_flag(Flag::NO_D6); this->set_flag(Flag::NO_D6);
this->set_flag(Flag::NO_SEND_FUNCTION_CALL);
break; break;
case Version::DC_NTE: case Version::DC_NTE:
case Version::DC_V1_11_2000_PROTOTYPE: case Version::DC_V1_11_2000_PROTOTYPE:
case Version::DC_V1: case Version::DC_V1:
this->set_flag(Flag::NO_D6);
break;
case Version::DC_V2: case Version::DC_V2:
this->set_flag(Flag::NO_D6); this->set_flag(Flag::NO_D6);
this->set_flag(Flag::HAS_SEND_FUNCTION_CALL);
this->set_flag(Flag::SEND_FUNCTION_CALL_NO_CACHE_PATCH); this->set_flag(Flag::SEND_FUNCTION_CALL_NO_CACHE_PATCH);
break; break;
case Version::PC_NTE: case Version::PC_NTE:
case Version::PC_V2: case Version::PC_V2:
this->set_flag(Flag::NO_D6); this->set_flag(Flag::NO_D6);
this->set_flag(Flag::HAS_SEND_FUNCTION_CALL);
this->set_flag(Flag::SEND_FUNCTION_CALL_CHECKSUM_ONLY); this->set_flag(Flag::SEND_FUNCTION_CALL_CHECKSUM_ONLY);
this->set_flag(Flag::SEND_FUNCTION_CALL_NO_CACHE_PATCH); this->set_flag(Flag::SEND_FUNCTION_CALL_NO_CACHE_PATCH);
break; break;
case Version::GC_NTE: case Version::GC_NTE:
case Version::GC_V3: this->set_flag(Flag::HAS_SEND_FUNCTION_CALL);
break;
case Version::GC_EP3_NTE: case Version::GC_EP3_NTE:
this->set_flag(Flag::HAS_SEND_FUNCTION_CALL);
this->set_flag(Flag::ENCRYPTED_SEND_FUNCTION_CALL);
break;
case Version::GC_V3:
case Version::GC_EP3: case Version::GC_EP3:
// Some of these versions have send_function_call and some don't; we
// have to set these flags later when we get sub_version
break; break;
case Version::XB_V3: case Version::XB_V3:
// TODO: Do all versions of XB need this flag? US does, at least. // TODO: Do all versions of XB need this flag? US does, at least.
this->set_flag(Flag::NO_D6_AFTER_LOBBY); this->set_flag(Flag::NO_D6_AFTER_LOBBY);
this->set_flag(Flag::HAS_SEND_FUNCTION_CALL);
this->set_flag(Flag::SEND_FUNCTION_CALL_NO_CACHE_PATCH); this->set_flag(Flag::SEND_FUNCTION_CALL_NO_CACHE_PATCH);
break; break;
default: default:
@@ -72,57 +86,62 @@ void Client::Config::set_flags_for_version(Version version, int64_t sub_version)
case 0x20: // DCNTE, possibly also DCv1 JP case 0x20: // DCNTE, possibly also DCv1 JP
case 0x21: // DCv1 US case 0x21: // DCv1 US
this->set_flag(Flag::NO_D6); this->set_flag(Flag::NO_D6);
this->set_flag(Flag::NO_SEND_FUNCTION_CALL);
break; break;
case 0x23: // DCv1 EU case 0x22: // DCv1 EU 50Hz (presumably)
case 0x23: // DCv1 EU 60Hz (presumably)
this->set_flag(Flag::NO_D6); this->set_flag(Flag::NO_D6);
this->set_flag(Flag::NO_SEND_FUNCTION_CALL);
break; break;
case 0x25: // DCv2 JP case 0x25: // DCv2 JP
case 0x26: // DCv2 US case 0x26: // DCv2 US
case 0x28: // DCv2 EU case 0x27: // DCv2 EU 50Hz (presumably)
case 0x28: // DCv2 EU 60Hz (presumably)
this->set_flag(Flag::NO_D6); this->set_flag(Flag::NO_D6);
this->set_flag(Flag::HAS_SEND_FUNCTION_CALL);
this->set_flag(Flag::SEND_FUNCTION_CALL_NO_CACHE_PATCH); this->set_flag(Flag::SEND_FUNCTION_CALL_NO_CACHE_PATCH);
break; break;
case 0x29: // PC case 0x29: // PC
this->set_flag(Flag::NO_D6); this->set_flag(Flag::NO_D6);
this->set_flag(Flag::HAS_SEND_FUNCTION_CALL);
this->set_flag(Flag::SEND_FUNCTION_CALL_CHECKSUM_ONLY); this->set_flag(Flag::SEND_FUNCTION_CALL_CHECKSUM_ONLY);
this->set_flag(Flag::SEND_FUNCTION_CALL_NO_CACHE_PATCH); this->set_flag(Flag::SEND_FUNCTION_CALL_NO_CACHE_PATCH);
break; break;
case 0x30: // GC Ep1&2 GameJam demo, GC Ep1&2 Trial Edition, GC Ep1&2 JP v1.2, at least one version of XB case 0x30: // GC Ep1&2 GameJam demo, GC Ep1&2 Trial Edition, GC Ep1&2 JP v1.2, at least one version of XB
case 0x31: // GC Ep1&2 US v1.0, GC US v1.1, XB US case 0x31: // GC Ep1&2 US v1.0, GC US v1.1, XB US
this->set_flag(Flag::HAS_SEND_FUNCTION_CALL);
break; break;
case 0x32: // GC Ep1&2 EU 50Hz case 0x32: // GC Ep1&2 EU 50Hz
case 0x33: // GC Ep1&2 EU 60Hz case 0x33: // GC Ep1&2 EU 60Hz
case 0x34: // GC Ep1&2 JP v1.3 case 0x34: // GC Ep1&2 JP v1.3
this->set_flag(Flag::NO_D6_AFTER_LOBBY); this->set_flag(Flag::NO_D6_AFTER_LOBBY);
this->set_flag(Flag::HAS_SEND_FUNCTION_CALL);
break; break;
case 0x35: // GC Ep1&2 JP v1.4 (Plus) case 0x35: // GC Ep1&2 JP v1.4 (Plus)
this->set_flag(Flag::NO_D6_AFTER_LOBBY); this->set_flag(Flag::NO_D6_AFTER_LOBBY);
this->set_flag(Flag::HAS_SEND_FUNCTION_CALL);
this->set_flag(Flag::ENCRYPTED_SEND_FUNCTION_CALL); this->set_flag(Flag::ENCRYPTED_SEND_FUNCTION_CALL);
this->set_flag(Flag::SEND_FUNCTION_CALL_NO_CACHE_PATCH); this->set_flag(Flag::SEND_FUNCTION_CALL_NO_CACHE_PATCH);
break; break;
case 0x3A: // GC Ep1&2 US v1.2 (Plus) GMK edition
this->set_flag(Flag::IS_CLIENT_CUSTOMIZATION);
[[fallthrough]];
case 0x36: // GC Ep1&2 US v1.2 (Plus) case 0x36: // GC Ep1&2 US v1.2 (Plus)
case 0x39: // GC Ep1&2 JP v1.5 (Plus) case 0x39: // GC Ep1&2 JP v1.5 (Plus)
this->set_flag(Flag::NO_D6_AFTER_LOBBY); this->set_flag(Flag::NO_D6_AFTER_LOBBY);
this->set_flag(Flag::NO_SEND_FUNCTION_CALL); this->set_flag(Flag::CAN_RECEIVE_ENABLE_B2_QUEST);
break; break;
case 0x40: // GC Ep3 JP and Trial Edition (and BB) case 0x40: // GC Ep3 JP and Trial Edition (and BB)
this->set_flag(Flag::NO_D6_AFTER_LOBBY); this->set_flag(Flag::NO_D6_AFTER_LOBBY);
this->set_flag(Flag::HAS_SEND_FUNCTION_CALL);
this->set_flag(Flag::ENCRYPTED_SEND_FUNCTION_CALL); this->set_flag(Flag::ENCRYPTED_SEND_FUNCTION_CALL);
this->set_flag(Flag::SEND_FUNCTION_CALL_NO_CACHE_PATCH); this->set_flag(Flag::SEND_FUNCTION_CALL_NO_CACHE_PATCH);
// sub_version can't be used to tell JP final and Trial Edition apart; we // sub_version can't be used to tell JP final and Trial Edition apart; we
// instead look at header.flag in the 61 command and set the version then. // instead look at header.flag in the 61 command and set the version then.
break; break;
case 0x41: // GC Ep3 US (and BB) case 0x41: // GC Ep3 US (and BB)
this->set_flag(Flag::NO_D6_AFTER_LOBBY);
this->set_flag(Flag::USE_OVERFLOW_FOR_SEND_FUNCTION_CALL);
this->set_flag(Flag::SEND_FUNCTION_CALL_NO_CACHE_PATCH);
break;
case 0x42: // GC Ep3 EU 50Hz case 0x42: // GC Ep3 EU 50Hz
case 0x43: // GC Ep3 EU 60Hz case 0x43: // GC Ep3 EU 60Hz
this->set_flag(Flag::NO_D6_AFTER_LOBBY); this->set_flag(Flag::NO_D6_AFTER_LOBBY);
this->set_flag(Flag::NO_SEND_FUNCTION_CALL); this->set_flag(Flag::CAN_RECEIVE_ENABLE_B2_QUEST);
break; break;
default: default:
throw runtime_error(string_printf("unknown sub_version %" PRIX64, sub_version)); throw runtime_error(string_printf("unknown sub_version %" PRIX64, sub_version));
@@ -164,12 +183,13 @@ bool Client::Config::should_update_vs(const Config& other) const {
Client::Client( Client::Client(
shared_ptr<Server> server, shared_ptr<Server> server,
struct bufferevent* bev, struct bufferevent* bev,
uint64_t virtual_network_id,
Version version, Version version,
ServerBehavior server_behavior) ServerBehavior server_behavior)
: server(server), : server(server),
id(next_id++), id(next_id++),
log(string_printf("[C-%" PRIX64 "] ", this->id), client_log.min_level), log(string_printf("[C-%" PRIX64 "] ", this->id), client_log.min_level),
channel(bev, version, 1, nullptr, nullptr, this, string_printf("C-%" PRIX64, this->id), TerminalFormat::FG_YELLOW, TerminalFormat::FG_GREEN), channel(bev, virtual_network_id, version, 1, nullptr, nullptr, this, "", TerminalFormat::FG_YELLOW, TerminalFormat::FG_GREEN),
server_behavior(server_behavior), server_behavior(server_behavior),
should_disconnect(false), should_disconnect(false),
should_send_to_lobby_server(false), should_send_to_lobby_server(false),
@@ -201,6 +221,7 @@ Client::Client(
card_battle_table_number(-1), card_battle_table_number(-1),
card_battle_table_seat_number(0), card_battle_table_seat_number(0),
card_battle_table_seat_state(0), card_battle_table_seat_state(0),
last_game_info_requested(0),
should_update_play_time(false), should_update_play_time(false),
bb_character_index(-1), bb_character_index(-1),
next_exp_value(0), next_exp_value(0),
@@ -208,6 +229,8 @@ Client::Client(
dol_base_addr(0), dol_base_addr(0),
external_bank_character_index(-1), external_bank_character_index(-1),
last_play_time_update(0) { last_play_time_update(0) {
this->update_channel_name();
this->config.set_flags_for_version(version, -1); this->config.set_flags_for_version(version, -1);
auto s = server->get_state(); auto s = server->get_state();
if (is_v1_or_v2(this->version()) ? s->default_rare_notifs_enabled_v1_v2 : s->default_rare_notifs_enabled_v3_v4) { if (is_v1_or_v2(this->version()) ? s->default_rare_notifs_enabled_v1_v2 : s->default_rare_notifs_enabled_v3_v4) {
@@ -246,6 +269,19 @@ Client::~Client() {
this->log.info("Deleted"); this->log.info("Deleted");
} }
void Client::update_channel_name() {
string ip_str = this->require_server_state()->format_address_for_channel_name(
this->channel.remote_addr, this->channel.virtual_network_id);
auto player = this->character(false, false);
if (player) {
string name_str = player->disp.name.decode(this->language());
this->channel.name = string_printf("C-%" PRIX64 " (%s) @ %s", this->id, name_str.c_str(), ip_str.c_str());
} else {
this->channel.name = string_printf("C-%" PRIX64 " @ %s", this->id, ip_str.c_str());
}
}
void Client::reschedule_save_game_data_event() { void Client::reschedule_save_game_data_event() {
if (this->version() == Version::BB_V4) { if (this->version() == Version::BB_V4) {
struct timeval tv = usecs_to_timeval(60000000); // 1 minute struct timeval tv = usecs_to_timeval(60000000); // 1 minute
@@ -261,33 +297,16 @@ void Client::reschedule_ping_and_timeout_events() {
event_add(this->idle_timeout_event.get(), &idle_tv); event_add(this->idle_timeout_event.get(), &idle_tv);
} }
void Client::set_license(shared_ptr<License> l) { void Client::convert_account_to_temporary_if_nte() {
if (this->version() == Version::BB_V4) { // If the session is a prototype version and the account was created and we
// Make sure bb_username is filename-safe // should use a temporary account instead, delete the permanent account and
for (char ch : l->bb_username) { // replace it with a temporary account.
if (!isalnum(ch) && (ch != '-') && (ch != '_')) {
throw runtime_error("invalid characters in username");
}
}
}
this->license = l;
}
void Client::convert_license_to_temporary_if_nte() {
// If the session is a prototype version and the license was created and we
// should use a temporary license instead, delete the permanent license and
// replace it with a temporary license.
auto s = this->require_server_state(); auto s = this->require_server_state();
if (s->use_temp_licenses_for_prototypes && if (s->use_temp_accounts_for_prototypes && this->login->account_was_created && is_any_nte(this->version())) {
this->config.check_flag(Client::Flag::LICENSE_WAS_CREATED) && this->log.info("Client is a prototype version and the account was created during this session; converting permanent account to temporary account");
is_any_nte(this->version())) { this->login->account->is_temporary = true;
this->log.info("Client is a prototype version and the license was created during this session; converting permanent license to temporary license"); this->login->account->delete_file();
s->license_index->remove(this->license->serial_number); this->login->account_was_created = false;
auto new_l = s->license_index->create_temporary_license();
this->license->delete_file();
*new_l = std::move(*this->license);
this->set_license(new_l);
this->config.clear_flag(Client::Flag::LICENSE_WAS_CREATED);
} }
} }
@@ -308,29 +327,29 @@ shared_ptr<Lobby> Client::require_lobby() const {
} }
shared_ptr<const TeamIndex::Team> Client::team() const { shared_ptr<const TeamIndex::Team> Client::team() const {
if (!this->license) { if (!this->login) {
throw logic_error("Client::team called on client with no license"); throw logic_error("Client::team called on client with no account");
} }
if (this->license->bb_team_id == 0) { if (this->login->account->bb_team_id == 0) {
return nullptr; return nullptr;
} }
auto p = this->character(false); auto p = this->character(false);
auto s = this->require_server_state(); auto s = this->require_server_state();
auto team = s->team_index->get_by_id(this->license->bb_team_id); auto team = s->team_index->get_by_id(this->login->account->bb_team_id);
if (!team) { if (!team) {
this->log.info("License contains a team ID, but the team does not exist; clearing team ID from license"); this->log.info("Account contains a team ID, but the team does not exist; clearing team ID from account");
this->license->bb_team_id = 0; this->login->account->bb_team_id = 0;
this->license->save(); this->login->account->save();
return nullptr; return nullptr;
} }
auto member_it = team->members.find(this->license->serial_number); auto member_it = team->members.find(this->login->account->account_id);
if (member_it == team->members.end()) { if (member_it == team->members.end()) {
this->log.info("License contains a team ID, but the team does not contain this member; clearing team ID from license"); this->log.info("Account contains a team ID, but the team does not contain this member; clearing team ID from account");
this->license->bb_team_id = 0; this->login->account->bb_team_id = 0;
this->license->save(); this->login->account->save();
return nullptr; return nullptr;
} }
@@ -341,7 +360,7 @@ shared_ptr<const TeamIndex::Team> Client::team() const {
string name = p->disp.name.decode(this->language()); string name = p->disp.name.decode(this->language());
if (m.name != name) { if (m.name != name) {
this->log.info("Updating player name in team config"); this->log.info("Updating player name in team config");
s->team_index->update_member_name(this->license->serial_number, name); s->team_index->update_member_name(this->login->account->account_id, name);
} }
} }
@@ -349,21 +368,24 @@ shared_ptr<const TeamIndex::Team> Client::team() const {
} }
bool Client::evaluate_quest_availability_expression( bool Client::evaluate_quest_availability_expression(
shared_ptr<const QuestAvailabilityExpression> expr, shared_ptr<const IntegralExpression> expr,
shared_ptr<const Lobby> game,
uint8_t event, uint8_t event,
uint8_t difficulty, uint8_t difficulty,
size_t num_players, size_t num_players,
bool v1_present) const { bool v1_present) const {
if (this->license && this->license->check_flag(License::Flag::DISABLE_QUEST_REQUIREMENTS)) { if (this->login && this->login->account->check_flag(Account::Flag::DISABLE_QUEST_REQUIREMENTS)) {
return true; return true;
} }
if (!expr) { if (!expr) {
return true; return true;
} }
auto l = this->lobby.lock(); if (game && !game->quest_flag_values) {
throw logic_error("quest flags are missing from game");
}
auto p = this->character(); auto p = this->character();
QuestAvailabilityExpression::Env env = { IntegralExpression::Env env = {
.flags = (l && !l->quest_flags_known) ? &l->quest_flag_values->data.at(difficulty) : &p->quest_flags.data.at(difficulty), .flags = &p->quest_flags.data.at(difficulty),
.challenge_records = &p->challenge_records, .challenge_records = &p->challenge_records,
.team = this->team(), .team = this->team(),
.num_players = num_players, .num_players = num_players,
@@ -373,24 +395,36 @@ bool Client::evaluate_quest_availability_expression(
int64_t ret = expr->evaluate(env); int64_t ret = expr->evaluate(env);
if (this->log.should_log(LogLevel::INFO)) { if (this->log.should_log(LogLevel::INFO)) {
string expr_str = expr->str(); string expr_str = expr->str();
this->log.info("Evaluated quest availability expression %s => %s", expr_str.c_str(), ret ? "TRUE" : "FALSE"); this->log.info("Evaluated integral expression %s => %s", expr_str.c_str(), ret ? "TRUE" : "FALSE");
} }
return ret; return ret;
} }
bool Client::can_see_quest(shared_ptr<const Quest> q, uint8_t event, uint8_t difficulty, size_t num_players, bool v1_present) const { bool Client::can_see_quest(
return this->evaluate_quest_availability_expression(q->available_expression, event, difficulty, num_players, v1_present); shared_ptr<const Quest> q,
shared_ptr<const Lobby> game,
uint8_t event,
uint8_t difficulty,
size_t num_players,
bool v1_present) const {
return this->evaluate_quest_availability_expression(q->available_expression, game, event, difficulty, num_players, v1_present);
} }
bool Client::can_play_quest(shared_ptr<const Quest> q, uint8_t event, uint8_t difficulty, size_t num_players, bool v1_present) const { bool Client::can_play_quest(
return this->evaluate_quest_availability_expression(q->enabled_expression, event, difficulty, num_players, v1_present); shared_ptr<const Quest> q,
shared_ptr<const Lobby> game,
uint8_t event,
uint8_t difficulty,
size_t num_players,
bool v1_present) const {
return this->evaluate_quest_availability_expression(q->enabled_expression, game, event, difficulty, num_players, v1_present);
} }
bool Client::can_use_chat_commands() const { bool Client::can_use_chat_commands() const {
if (!this->license) { if (!this->login) {
return false; return false;
} }
if (this->license->check_flag(License::Flag::ALWAYS_ENABLE_CHAT_COMMANDS)) { if (this->login->account->check_flag(Account::Flag::ALWAYS_ENABLE_CHAT_COMMANDS)) {
return true; return true;
} }
return this->require_server_state()->enable_chat_commands; return this->require_server_state()->enable_chat_commands;
@@ -470,8 +504,8 @@ void Client::create_battle_overlay(shared_ptr<const BattleRules> rules, shared_p
uint8_t char_class = this->overlay_character_data->disp.visual.char_class; uint8_t char_class = this->overlay_character_data->disp.visual.char_class;
auto& stats = this->overlay_character_data->disp.stats; auto& stats = this->overlay_character_data->disp.stats;
stats.reset_to_base(char_class, level_table); level_table->reset_to_base(stats, char_class);
stats.advance_to_level(char_class, target_level, level_table); level_table->advance_to_level(stats, target_level, char_class);
stats.esp = 40; stats.esp = 40;
stats.meseta = 300; stats.meseta = 300;
@@ -516,8 +550,8 @@ void Client::create_challenge_overlay(Version version, size_t template_index, sh
overlay->inventory.items[13].extension_data2 = 1; overlay->inventory.items[13].extension_data2 = 1;
overlay->disp.stats.reset_to_base(overlay->disp.visual.char_class, level_table); level_table->reset_to_base(overlay->disp.stats, overlay->disp.visual.char_class);
overlay->disp.stats.advance_to_level(overlay->disp.visual.char_class, tpl.level, level_table); level_table->advance_to_level(overlay->disp.stats, tpl.level, overlay->disp.visual.char_class);
overlay->disp.stats.esp = 40; overlay->disp.stats.esp = 40;
overlay->disp.stats.unknown_a3 = 10.0; overlay->disp.stats.unknown_a3 = 10.0;
@@ -606,10 +640,10 @@ string Client::system_filename() const {
if (this->version() != Version::BB_V4) { if (this->version() != Version::BB_V4) {
throw logic_error("non-BB players do not have system data"); throw logic_error("non-BB players do not have system data");
} }
if (!this->license) { if (!this->login || !this->login->bb_license) {
throw logic_error("client is not logged in"); throw logic_error("client is not logged in");
} }
return string_printf("system/players/system_%s.psosys", this->license->bb_username.c_str()); return string_printf("system/players/system_%s.psosys", this->login->bb_license->username.c_str());
} }
string Client::character_filename(const std::string& bb_username, int8_t index) { string Client::character_filename(const std::string& bb_username, int8_t index) {
@@ -622,55 +656,56 @@ string Client::character_filename(const std::string& bb_username, int8_t index)
return string_printf("system/players/player_%s_%hhd.psochar", bb_username.c_str(), index); return string_printf("system/players/player_%s_%hhd.psochar", bb_username.c_str(), index);
} }
string Client::backup_character_filename(uint32_t serial_number, size_t index) { string Client::backup_character_filename(uint32_t account_id, size_t index, bool is_ep3) {
return string_printf("system/players/backup_player_%" PRIu32 "_%zu.psochar", serial_number, index); return string_printf("system/players/backup_player_%" PRIu32 "_%zu.%s",
account_id, index, is_ep3 ? "pso3char" : "psochar");
} }
string Client::character_filename(int8_t index) const { string Client::character_filename(int8_t index) const {
if (this->version() != Version::BB_V4) { if (this->version() != Version::BB_V4) {
throw logic_error("non-BB players do not have character data"); throw logic_error("non-BB players do not have character data");
} }
if (!this->license) { if (!this->login || !this->login->bb_license) {
throw logic_error("client is not logged in"); throw logic_error("client is not logged in");
} }
return this->character_filename(this->license->bb_username, (index < 0) ? this->bb_character_index : index); return this->character_filename(this->login->bb_license->username, (index < 0) ? this->bb_character_index : index);
} }
string Client::guild_card_filename() const { string Client::guild_card_filename() const {
if (this->version() != Version::BB_V4) { if (this->version() != Version::BB_V4) {
throw logic_error("non-BB players do not have character data"); throw logic_error("non-BB players do not have character data");
} }
if (!this->license) { if (!this->login || !this->login->bb_license) {
throw logic_error("client is not logged in"); throw logic_error("client is not logged in");
} }
return string_printf("system/players/guild_cards_%s.psocard", this->license->bb_username.c_str()); return string_printf("system/players/guild_cards_%s.psocard", this->login->bb_license->username.c_str());
} }
string Client::shared_bank_filename() const { string Client::shared_bank_filename() const {
if (this->version() != Version::BB_V4) { if (this->version() != Version::BB_V4) {
throw logic_error("non-BB players do not have character data"); throw logic_error("non-BB players do not have character data");
} }
if (!this->license) { if (!this->login || !this->login->bb_license) {
throw logic_error("client is not logged in"); throw logic_error("client is not logged in");
} }
return string_printf("system/players/shared_bank_%s.psobank", this->license->bb_username.c_str()); return string_printf("system/players/shared_bank_%s.psobank", this->login->bb_license->username.c_str());
} }
string Client::legacy_account_filename() const { string Client::legacy_account_filename() const {
if (this->version() != Version::BB_V4) { if (this->version() != Version::BB_V4) {
throw logic_error("non-BB players do not have character data"); throw logic_error("non-BB players do not have character data");
} }
if (!this->license) { if (!this->login || !this->login->bb_license) {
throw logic_error("client is not logged in"); throw logic_error("client is not logged in");
} }
return string_printf("system/players/account_%s.nsa", this->license->bb_username.c_str()); return string_printf("system/players/account_%s.nsa", this->login->bb_license->username.c_str());
} }
string Client::legacy_player_filename() const { string Client::legacy_player_filename() const {
if (this->version() != Version::BB_V4) { if (this->version() != Version::BB_V4) {
throw logic_error("non-BB players do not have character data"); throw logic_error("non-BB players do not have character data");
} }
if (!this->license) { if (!this->login || !this->login->bb_license) {
throw logic_error("client is not logged in"); throw logic_error("client is not logged in");
} }
if (this->bb_character_index < 0) { if (this->bb_character_index < 0) {
@@ -678,7 +713,7 @@ string Client::legacy_player_filename() const {
} }
return string_printf( return string_printf(
"system/players/player_%s_%hhd.nsc", "system/players/player_%s_%hhd.nsc",
this->license->bb_username.c_str(), this->login->bb_license->username.c_str(),
static_cast<int8_t>(this->bb_character_index + 1)); static_cast<int8_t>(this->bb_character_index + 1));
} }
@@ -698,7 +733,7 @@ void Client::load_all_files() {
this->guild_card_data = make_shared<PSOBBGuildCardFile>(); this->guild_card_data = make_shared<PSOBBGuildCardFile>();
return; return;
} }
if (!this->license) { if (!this->login || !this->login->bb_license) {
throw logic_error("cannot load BB player data until client is logged in"); throw logic_error("cannot load BB player data until client is logged in");
} }
@@ -737,6 +772,7 @@ void Client::load_all_files() {
if (header.flag != 0x00000000) { if (header.flag != 0x00000000) {
throw runtime_error("incorrect flag in character file header"); throw runtime_error("incorrect flag in character file header");
} }
static_assert(sizeof(PSOBBCharacterFile) + sizeof(PSOBBFullSystemFile) == 0x3994, ".psochar size is incorrect");
this->character_data = make_shared<PSOBBCharacterFile>(freadx<PSOBBCharacterFile>(f.get())); this->character_data = make_shared<PSOBBCharacterFile>(freadx<PSOBBCharacterFile>(f.get()));
files_manager->set_character(char_filename, this->character_data); files_manager->set_character(char_filename, this->character_data);
player_data_log.info("Loaded character data from %s", char_filename.c_str()); player_data_log.info("Loaded character data from %s", char_filename.c_str());
@@ -748,6 +784,10 @@ void Client::load_all_files() {
files_manager->set_system(sys_filename, this->system_data); files_manager->set_system(sys_filename, this->system_data);
player_data_log.info("Loaded system data from %s", char_filename.c_str()); player_data_log.info("Loaded system data from %s", char_filename.c_str());
} }
this->update_character_data_after_load(this->character_data);
this->system_data->base.language = this->language();
} else { } else {
player_data_log.info("Character file is missing: %s", char_filename.c_str()); player_data_log.info("Character file is missing: %s", char_filename.c_str());
} }
@@ -815,11 +855,10 @@ void Client::load_all_files() {
this->character_data->inventory = nsc_data.inventory; this->character_data->inventory = nsc_data.inventory;
this->character_data->disp = nsc_data.disp; this->character_data->disp = nsc_data.disp;
this->character_data->play_time_seconds = 0; this->character_data->play_time_seconds = 0;
this->character_data->unknown_a2 = nsc_data.unknown_a2;
this->character_data->quest_flags = nsc_data.quest_flags; this->character_data->quest_flags = nsc_data.quest_flags;
this->character_data->death_count = nsc_data.death_count; this->character_data->death_count = nsc_data.death_count;
this->character_data->bank = nsc_data.bank; this->character_data->bank = nsc_data.bank;
this->character_data->guild_card.guild_card_number = this->license->serial_number; this->character_data->guild_card.guild_card_number = this->login->account->account_id;
this->character_data->guild_card.name = nsc_data.disp.name; this->character_data->guild_card.name = nsc_data.disp.name;
this->character_data->guild_card.description = nsc_data.guild_card_description; this->character_data->guild_card.description = nsc_data.guild_card_description;
this->character_data->guild_card.present = 1; this->character_data->guild_card.present = 1;
@@ -830,7 +869,7 @@ void Client::load_all_files() {
this->character_data->info_board = nsc_data.info_board; this->character_data->info_board = nsc_data.info_board;
this->character_data->battle_records = nsc_data.battle_records; this->character_data->battle_records = nsc_data.battle_records;
this->character_data->challenge_records = nsc_data.challenge_records; this->character_data->challenge_records = nsc_data.challenge_records;
this->character_data->tech_menu_config = nsc_data.tech_menu_config; this->character_data->tech_menu_shortcut_entries = nsc_data.tech_menu_shortcut_entries;
this->character_data->quest_counters = nsc_data.quest_counters; this->character_data->quest_counters = nsc_data.quest_counters;
if (nsa_data) { if (nsa_data) {
this->character_data->option_flags = nsa_data->option_flags; this->character_data->option_flags = nsa_data->option_flags;
@@ -840,6 +879,7 @@ void Client::load_all_files() {
} else { } else {
player_data_log.info("Loaded legacy player data from %s", nsc_filename.c_str()); player_data_log.info("Loaded legacy player data from %s", nsc_filename.c_str());
} }
this->update_character_data_after_load(this->character_data);
} }
} }
@@ -853,12 +893,21 @@ void Client::load_all_files() {
if (this->character_data) { if (this->character_data) {
// Clear legacy play_time field // Clear legacy play_time field
this->character_data->disp.name.clear_after_bytes(0x18); this->character_data->disp.name.clear_after_bytes(0x18);
this->license->auto_reply_message = this->character_data->auto_reply.decode(); this->login->account->auto_reply_message = this->character_data->auto_reply.decode();
this->license->save(); this->login->account->save();
this->last_play_time_update = now(); this->last_play_time_update = now();
} }
} }
void Client::update_character_data_after_load(shared_ptr<PSOBBCharacterFile> charfile) {
charfile->import_tethealla_material_usage(this->require_server_state()->level_table(this->version()));
uint8_t lang = this->language();
player_data_log.info("Overriding language fields in save files with %02hhX (%c)", lang, char_for_language_code(lang));
charfile->inventory.language = lang;
charfile->guild_card.language = lang;
}
void Client::save_all() { void Client::save_all() {
if (this->system_data) { if (this->system_data) {
this->save_system_file(); this->save_system_file();
@@ -871,7 +920,7 @@ void Client::save_all() {
} }
if (this->external_bank) { if (this->external_bank) {
string filename = this->shared_bank_filename(); string filename = this->shared_bank_filename();
save_object_file<PlayerBank>(filename, *this->external_bank); save_object_file<PlayerBank200>(filename, *this->external_bank);
player_data_log.info("Saved shared bank file %s", filename.c_str()); player_data_log.info("Saved shared bank file %s", filename.c_str());
} }
if (this->external_bank_character) { if (this->external_bank_character) {
@@ -901,10 +950,10 @@ void Client::save_character_file(
fwritex(f.get(), *character); fwritex(f.get(), *character);
fwritex(f.get(), *system); fwritex(f.get(), *system);
// TODO: Technically, we should write the actual team membership struct to the // TODO: Technically, we should write the actual team membership struct to the
// file here, but that would cause Client to depend on License, which // file here, but that would cause Client to depend on Account, which
// it currently does not. This data doesn't matter at all for correctness // it currently does not. This data doesn't matter at all for correctness
// within newserv, since it ignores this data entirely and instead generates // within newserv, since it ignores this data entirely and instead generates
// the membership struct from the team ID in the License and the team's state. // the membership struct from the team ID in the Account and the team's state.
// So, writing correct data here would mostly be for compatibility with other // So, writing correct data here would mostly be for compatibility with other
// PSO servers. But if the other server is newserv, then this data would be // PSO servers. But if the other server is newserv, then this data would be
// used anyway, and if it's not, then it would presumably have a different set // used anyway, and if it's not, then it would presumably have a different set
@@ -915,6 +964,13 @@ void Client::save_character_file(
player_data_log.info("Saved character file %s", filename.c_str()); player_data_log.info("Saved character file %s", filename.c_str());
} }
void Client::save_ep3_character_file(
const string& filename,
const PSOGCEp3CharacterFile::Character& character) {
save_file(filename, &character, sizeof(character));
player_data_log.info("Saved Episode 3 character file %s", filename.c_str());
}
void Client::save_character_file() { void Client::save_character_file() {
if (!this->system_data.get()) { if (!this->system_data.get()) {
throw logic_error("no system file loaded"); throw logic_error("no system file loaded");
@@ -944,8 +1000,8 @@ void Client::save_guild_card_file() const {
player_data_log.info("Saved Guild Card file %s", filename.c_str()); player_data_log.info("Saved Guild Card file %s", filename.c_str());
} }
void Client::load_backup_character(uint32_t serial_number, size_t index) { void Client::load_backup_character(uint32_t account_id, size_t index) {
string filename = this->backup_character_filename(serial_number, index); string filename = this->backup_character_filename(account_id, index, false);
auto f = fopen_unique(filename, "rb"); auto f = fopen_unique(filename, "rb");
auto header = freadx<PSOCommandHeaderBB>(f.get()); auto header = freadx<PSOCommandHeaderBB>(f.get());
if (header.size != 0x399C) { if (header.size != 0x399C) {
@@ -958,9 +1014,20 @@ void Client::load_backup_character(uint32_t serial_number, size_t index) {
throw runtime_error("incorrect flag in character file header"); throw runtime_error("incorrect flag in character file header");
} }
this->character_data = make_shared<PSOBBCharacterFile>(freadx<PSOBBCharacterFile>(f.get())); this->character_data = make_shared<PSOBBCharacterFile>(freadx<PSOBBCharacterFile>(f.get()));
this->update_character_data_after_load(this->character_data);
this->v1_v2_last_reported_disp.reset(); this->v1_v2_last_reported_disp.reset();
} }
shared_ptr<PSOGCEp3CharacterFile::Character> Client::load_ep3_backup_character(uint32_t account_id, size_t index) {
string filename = this->backup_character_filename(account_id, index, true);
auto ch = make_shared<PSOGCEp3CharacterFile::Character>(load_object_file<PSOGCEp3CharacterFile::Character>(filename));
this->character_data = PSOBBCharacterFile::create_from_ep3(*ch);
this->ep3_config = make_shared<Episode3::PlayerConfig>(ch->ep3_config);
this->update_character_data_after_load(this->character_data);
this->v1_v2_last_reported_disp.reset();
return ch;
}
void Client::save_and_unload_character() { void Client::save_and_unload_character() {
if (this->character_data) { if (this->character_data) {
this->save_character_file(); this->save_character_file();
@@ -969,7 +1036,7 @@ void Client::save_and_unload_character() {
} }
} }
PlayerBank& Client::current_bank() { PlayerBank200& Client::current_bank() {
if (this->external_bank) { if (this->external_bank) {
return *this->external_bank; return *this->external_bank;
} else if (this->external_bank_character) { } else if (this->external_bank_character) {
@@ -985,7 +1052,7 @@ std::shared_ptr<PSOBBCharacterFile> Client::current_bank_character() {
void Client::use_default_bank() { void Client::use_default_bank() {
if (this->external_bank) { if (this->external_bank) {
string filename = this->shared_bank_filename(); string filename = this->shared_bank_filename();
save_object_file<PlayerBank>(filename, *this->external_bank); save_object_file<PlayerBank200>(filename, *this->external_bank);
this->external_bank.reset(); this->external_bank.reset();
player_data_log.info("Detached shared bank %s", filename.c_str()); player_data_log.info("Detached shared bank %s", filename.c_str());
} }
@@ -1007,12 +1074,12 @@ bool Client::use_shared_bank() {
player_data_log.info("Using loaded shared bank %s", filename.c_str()); player_data_log.info("Using loaded shared bank %s", filename.c_str());
return true; return true;
} else if (isfile(filename)) { } else if (isfile(filename)) {
this->external_bank = make_shared<PlayerBank>(load_object_file<PlayerBank>(filename)); this->external_bank = make_shared<PlayerBank200>(load_object_file<PlayerBank200>(filename));
files_manager->set_bank(filename, this->external_bank); files_manager->set_bank(filename, this->external_bank);
player_data_log.info("Loaded shared bank %s", filename.c_str()); player_data_log.info("Loaded shared bank %s", filename.c_str());
return true; return true;
} else { } else {
this->external_bank = make_shared<PlayerBank>(); this->external_bank = make_shared<PlayerBank200>();
files_manager->set_bank(filename, this->external_bank); files_manager->set_bank(filename, this->external_bank);
player_data_log.info("Created shared bank for %s", filename.c_str()); player_data_log.info("Created shared bank for %s", filename.c_str());
return false; return false;
@@ -1042,6 +1109,7 @@ void Client::use_character_bank(int8_t index) {
throw runtime_error("incorrect flag in character file header"); throw runtime_error("incorrect flag in character file header");
} }
this->external_bank_character = make_shared<PSOBBCharacterFile>(freadx<PSOBBCharacterFile>(f.get())); this->external_bank_character = make_shared<PSOBBCharacterFile>(freadx<PSOBBCharacterFile>(f.get()));
this->update_character_data_after_load(this->external_bank_character);
this->external_bank_character_index = index; this->external_bank_character_index = index;
files_manager->set_character(filename, this->external_bank_character); files_manager->set_character(filename, this->external_bank_character);
player_data_log.info("Loaded character data from %s for external bank", filename.c_str()); player_data_log.info("Loaded character data from %s for external bank", filename.c_str());
+47 -19
View File
@@ -5,13 +5,13 @@
#include <memory> #include <memory>
#include <stdexcept> #include <stdexcept>
#include "Account.hh"
#include "Channel.hh" #include "Channel.hh"
#include "CommandFormats.hh" #include "CommandFormats.hh"
#include "Episode3/BattleRecord.hh" #include "Episode3/BattleRecord.hh"
#include "Episode3/Tournament.hh" #include "Episode3/Tournament.hh"
#include "FileContentsCache.hh" #include "FileContentsCache.hh"
#include "FunctionCompiler.hh" #include "FunctionCompiler.hh"
#include "License.hh"
#include "PSOEncryption.hh" #include "PSOEncryption.hh"
#include "PSOProtocol.hh" #include "PSOProtocol.hh"
#include "PatchFileIndex.hh" #include "PatchFileIndex.hh"
@@ -24,6 +24,7 @@ extern const uint64_t CLIENT_CONFIG_MAGIC;
class Server; class Server;
struct Lobby; struct Lobby;
class Parsed6x70Data;
class Client : public std::enable_shared_from_this<Client> { class Client : public std::enable_shared_from_this<Client> {
public: public:
@@ -34,21 +35,21 @@ public:
// TODO: It'd be nice to use a pattern here (e.g. all server-side flags are // 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 // in the high bits) but that would require re-recording or manually
// rewriting all the tests // rewriting all the tests
CLIENT_SIDE_MASK = 0xFF3CFFFF7C0FFFFB, CLIENT_SIDE_MASK = 0xFF3CFFFF7C0BFFFB,
// Version-related flags // Version-related flags
CHECKED_FOR_DC_V1_PROTOTYPE = 0x0000000000000002, CHECKED_FOR_DC_V1_PROTOTYPE = 0x0000000000000002,
LICENSE_WAS_CREATED = 0x0000000000000004, // Server-side only
NO_D6_AFTER_LOBBY = 0x0000000000000100, NO_D6_AFTER_LOBBY = 0x0000000000000100,
NO_D6 = 0x0000000000000200, NO_D6 = 0x0000000000000200,
FORCE_ENGLISH_LANGUAGE_BB = 0x0000000000000400, FORCE_ENGLISH_LANGUAGE_BB = 0x0000000000000400,
// Flags describing the behavior for send_function_call // Flags describing the behavior for send_function_call
NO_SEND_FUNCTION_CALL = 0x0000000000001000, HAS_SEND_FUNCTION_CALL = 0x0000000000001000,
ENCRYPTED_SEND_FUNCTION_CALL = 0x0000000000002000, ENCRYPTED_SEND_FUNCTION_CALL = 0x0000000000002000,
SEND_FUNCTION_CALL_CHECKSUM_ONLY = 0x0000000000004000, SEND_FUNCTION_CALL_CHECKSUM_ONLY = 0x0000000000004000,
SEND_FUNCTION_CALL_NO_CACHE_PATCH = 0x0000000000008000, SEND_FUNCTION_CALL_NO_CACHE_PATCH = 0x0000000000008000,
USE_OVERFLOW_FOR_SEND_FUNCTION_CALL = 0x0000000000010000, CAN_RECEIVE_ENABLE_B2_QUEST = 0x0000000000020000,
AWAITING_ENABLE_B2_QUEST = 0x0000000000040000, // Server-side only
// State flags // State flags
LOADING = 0x0000000000100000, // Server-side only LOADING = 0x0000000000100000, // Server-side only
@@ -62,13 +63,16 @@ public:
HAS_EP3_MEDIA_UPDATES = 0x0000000010000000, HAS_EP3_MEDIA_UPDATES = 0x0000000010000000,
USE_OVERRIDE_RANDOM_SEED = 0x0000000020000000, USE_OVERRIDE_RANDOM_SEED = 0x0000000020000000,
HAS_GUILD_CARD_NUMBER = 0x0000000040000000, HAS_GUILD_CARD_NUMBER = 0x0000000040000000,
HAS_AUTO_PATCHES = 0x0000004000000000,
AT_BANK_COUNTER = 0x0000000080000000, // Server-side only AT_BANK_COUNTER = 0x0000000080000000, // Server-side only
SHOULD_SEND_ARTIFICIAL_ITEM_STATE = 0x0001000000000000, // 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_ENEMY_AND_SET_STATE = 0x0040000000000000, // Server-side only
SHOULD_SEND_ARTIFICIAL_OBJECT_STATE = 0x0080000000000000, // Server-side only SHOULD_SEND_ARTIFICIAL_OBJECT_STATE = 0x0080000000000000, // Server-side only
SHOULD_SEND_ARTIFICIAL_FLAG_STATE = 0x0002000000000000, // 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, SHOULD_SEND_ENABLE_SAVE = 0x0004000000000000,
SWITCH_ASSIST_ENABLED = 0x0000000100000000, SWITCH_ASSIST_ENABLED = 0x0000000100000000,
IS_CLIENT_CUSTOMIZATION = 0x0100000000000000,
// Cheat mode and option flags // Cheat mode and option flags
INFINITE_HP_ENABLED = 0x0000000200000000, INFINITE_HP_ENABLED = 0x0000000200000000,
@@ -90,6 +94,7 @@ public:
PROXY_BLANK_NAME_ENABLED = 0x0000400000000000, PROXY_BLANK_NAME_ENABLED = 0x0000400000000000,
PROXY_BLOCK_FUNCTION_CALLS = 0x0000800000000000, PROXY_BLOCK_FUNCTION_CALLS = 0x0000800000000000,
PROXY_EP3_UNMASK_WHISPERS = 0x0008000000000000, PROXY_EP3_UNMASK_WHISPERS = 0x0008000000000000,
PROXY_VIRTUAL_CLIENT = 0x0400000000000000,
// clang-format on // clang-format on
}; };
enum class ItemDropNotificationMode { enum class ItemDropNotificationMode {
@@ -181,8 +186,7 @@ public:
uint64_t id; uint64_t id;
PrefixedLogger log; PrefixedLogger log;
// License & account std::shared_ptr<Login> login;
std::shared_ptr<License> license;
// Network // Network
Channel channel; Channel channel;
@@ -217,8 +221,9 @@ public:
uint16_t card_battle_table_seat_number; uint16_t card_battle_table_seat_number;
uint16_t card_battle_table_seat_state; uint16_t card_battle_table_seat_state;
std::weak_ptr<Episode3::Tournament::Team> ep3_tournament_team; std::weak_ptr<Episode3::Tournament::Team> ep3_tournament_team;
std::shared_ptr<Episode3::BattleRecord> ep3_prev_battle_record; std::shared_ptr<const Episode3::BattleRecord> ep3_prev_battle_record;
std::shared_ptr<const Menu> last_menu_sent; std::shared_ptr<const Menu> last_menu_sent;
uint32_t last_game_info_requested;
struct JoinCommand { struct JoinCommand {
uint16_t command; uint16_t command;
uint32_t flag; uint32_t flag;
@@ -240,9 +245,12 @@ public:
bool should_update_play_time; bool should_update_play_time;
std::unordered_set<uint32_t> blocked_senders; std::unordered_set<uint32_t> blocked_senders;
std::unique_ptr<PlayerDispDataDCPCV3> v1_v2_last_reported_disp; std::unique_ptr<PlayerDispDataDCPCV3> v1_v2_last_reported_disp;
std::shared_ptr<Parsed6x70Data> last_reported_6x70;
// These are null unless the client is within the trade sequence (D0-D4 or EE commands) // 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<PendingItemTrade> pending_item_trade;
std::unique_ptr<PendingCardTrade> pending_card_trade; std::unique_ptr<PendingCardTrade> pending_card_trade;
uint32_t telepipe_lobby_id;
G_SetTelepipeState_6x68 telepipe_state;
std::shared_ptr<Episode3::PlayerConfig> ep3_config; // Null for non-Ep3 std::shared_ptr<Episode3::PlayerConfig> ep3_config; // Null for non-Ep3
int8_t bb_character_index; int8_t bb_character_index;
ItemData bb_identify_result; ItemData bb_identify_result;
@@ -253,9 +261,9 @@ public:
RecentSwitchFlags recent_switch_flags; // used for switch assist RecentSwitchFlags recent_switch_flags; // used for switch assist
bool can_chat; bool can_chat;
struct PendingCharacterExport { struct PendingCharacterExport {
std::shared_ptr<const License> license; std::shared_ptr<const Account> dest_account;
ssize_t character_index = -1; ssize_t character_index = -1;
bool is_bb_conversion = false; std::shared_ptr<const BBLicense> dest_bb_license; // Only used for $bbchar; null for $savechar
}; };
std::unique_ptr<PendingCharacterExport> pending_character_export; std::unique_ptr<PendingCharacterExport> pending_character_export;
std::deque<std::function<void(uint32_t, uint32_t)>> function_call_response_queue; std::deque<std::function<void(uint32_t, uint32_t)>> function_call_response_queue;
@@ -268,10 +276,13 @@ public:
Client( Client(
std::shared_ptr<Server> server, std::shared_ptr<Server> server,
struct bufferevent* bev, struct bufferevent* bev,
uint64_t virtual_network_id,
Version version, Version version,
ServerBehavior server_behavior); ServerBehavior server_behavior);
~Client(); ~Client();
void update_channel_name();
void reschedule_save_game_data_event(); void reschedule_save_game_data_event();
void reschedule_ping_and_timeout_events(); void reschedule_ping_and_timeout_events();
@@ -282,8 +293,7 @@ public:
return this->channel.language; return this->channel.language;
} }
void set_license(std::shared_ptr<License> l); void convert_account_to_temporary_if_nte();
void convert_license_to_temporary_if_nte();
void sync_config(); void sync_config();
@@ -293,13 +303,26 @@ public:
std::shared_ptr<const TeamIndex::Team> team() const; std::shared_ptr<const TeamIndex::Team> team() const;
bool evaluate_quest_availability_expression( bool evaluate_quest_availability_expression(
std::shared_ptr<const QuestAvailabilityExpression> expr, std::shared_ptr<const IntegralExpression> expr,
std::shared_ptr<const Lobby> game,
uint8_t event,
uint8_t 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,
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 event,
uint8_t difficulty, uint8_t difficulty,
size_t num_players, size_t num_players,
bool v1_present) const; bool v1_present) const;
bool can_see_quest(std::shared_ptr<const Quest> q, uint8_t event, uint8_t difficulty, size_t num_players, bool v1_present) const;
bool can_play_quest(std::shared_ptr<const Quest> q, uint8_t event, uint8_t difficulty, size_t num_players, bool v1_present) const;
bool can_use_chat_commands() const; bool can_use_chat_commands() const;
@@ -341,7 +364,7 @@ public:
std::string system_filename() const; std::string system_filename() const;
static std::string character_filename(const std::string& bb_username, int8_t index); static std::string character_filename(const std::string& bb_username, int8_t index);
static std::string backup_character_filename(uint32_t serial_number, size_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 character_filename(int8_t index = -1) const;
std::string guild_card_filename() const; std::string guild_card_filename() const;
std::string shared_bank_filename() const; std::string shared_bank_filename() const;
@@ -355,14 +378,18 @@ public:
const std::string& filename, const std::string& filename,
std::shared_ptr<const PSOBBBaseSystemFile> sys, std::shared_ptr<const PSOBBBaseSystemFile> sys,
std::shared_ptr<const PSOBBCharacterFile> character); 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. // Note: This function is not const because it updates the player's play time.
void save_character_file(); void save_character_file();
void save_guild_card_file() const; void save_guild_card_file() const;
void load_backup_character(uint32_t serial_number, size_t index); 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 save_and_unload_character();
PlayerBank& current_bank(); PlayerBank200& current_bank();
std::shared_ptr<PSOBBCharacterFile> current_bank_character(); std::shared_ptr<PSOBBCharacterFile> current_bank_character();
bool use_shared_bank(); // Returns true if the bank exists; false if it was created 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_character_bank(int8_t bb_character_index);
@@ -379,7 +406,7 @@ private:
std::shared_ptr<PSOBBCharacterFile> overlay_character_data; std::shared_ptr<PSOBBCharacterFile> overlay_character_data;
std::shared_ptr<PSOBBCharacterFile> character_data; std::shared_ptr<PSOBBCharacterFile> character_data;
std::shared_ptr<PSOBBGuildCardFile> guild_card_data; std::shared_ptr<PSOBBGuildCardFile> guild_card_data;
std::shared_ptr<PlayerBank> external_bank; std::shared_ptr<PlayerBank200> external_bank;
std::shared_ptr<PSOBBCharacterFile> external_bank_character; std::shared_ptr<PSOBBCharacterFile> external_bank_character;
int8_t external_bank_character_index; int8_t external_bank_character_index;
uint64_t last_play_time_update; uint64_t last_play_time_update;
@@ -387,4 +414,5 @@ private:
void save_and_clear_external_bank(); void save_and_clear_external_bank();
void load_all_files(); void load_all_files();
void update_character_data_after_load(std::shared_ptr<PSOBBCharacterFile> character_data);
}; };
+983 -884
View File
File diff suppressed because it is too large Load Diff
+350 -52
View File
@@ -16,6 +16,16 @@ JSON to_json(const parray<IntT, Count>& v) {
return ret; return ret;
} }
template <typename IntT, size_t Count>
void from_json_into(const JSON& json, parray<IntT, Count>& ret) {
if (json.size() != Count) {
throw runtime_error("incorrect array length");
}
for (size_t z = 0; z < Count; z++) {
ret[z] = json.at(z).as_int();
}
}
template <typename IntT, size_t Count> template <typename IntT, size_t Count>
JSON to_json(const parray<CommonItemSet::Table::Range<IntT>, Count>& v) { JSON to_json(const parray<CommonItemSet::Table::Range<IntT>, Count>& v) {
auto ret = JSON::list(); auto ret = JSON::list();
@@ -25,6 +35,16 @@ JSON to_json(const parray<CommonItemSet::Table::Range<IntT>, Count>& v) {
return ret; return ret;
} }
template <typename IntT, size_t Count>
void from_json_into(const JSON& json, parray<CommonItemSet::Table::Range<IntT>, Count>& ret) {
if (json.size() != Count) {
throw runtime_error("incorrect array length");
}
for (size_t z = 0; z < Count; z++) {
from_json_into(json.at(z), ret[z]);
}
}
template <typename IntT> template <typename IntT>
JSON to_json(const CommonItemSet::Table::Range<IntT>& v) { JSON to_json(const CommonItemSet::Table::Range<IntT>& v) {
if (v.min == v.max) { if (v.min == v.max) {
@@ -34,6 +54,22 @@ JSON to_json(const CommonItemSet::Table::Range<IntT>& v) {
} }
} }
template <typename IntT>
void from_json_into(const JSON& json, CommonItemSet::Table::Range<IntT>& ret) {
if (json.is_int()) {
IntT v = json.as_int();
ret.min = v;
ret.max = v;
} else {
const auto& l = json.as_list();
if (l.size() != 2) {
throw runtime_error("incorrect range list length");
}
ret.min = l.at(0)->as_int();
ret.max = l.at(1)->as_int();
}
}
template <typename IntT, size_t Count1, size_t Count2> template <typename IntT, size_t Count1, size_t Count2>
JSON to_json(const parray<parray<IntT, Count2>, Count1>& v) { JSON to_json(const parray<parray<IntT, Count2>, Count1>& v) {
auto ret = JSON::list(); auto ret = JSON::list();
@@ -43,6 +79,262 @@ JSON to_json(const parray<parray<IntT, Count2>, Count1>& v) {
return ret; return ret;
} }
template <typename IntT, size_t Count1, size_t Count2>
void from_json_into(const JSON& json, parray<parray<IntT, Count2>, Count1>& ret) {
if (json.size() != Count1) {
throw runtime_error("incorrect array length");
}
for (size_t z = 0; z < Count1; z++) {
from_json_into(json.at(z), ret[z]);
}
}
template <typename IntT, size_t Count1, size_t Count2>
void from_json_into(const JSON& json, parray<parray<CommonItemSet::Table::Range<IntT>, Count2>, Count1>& ret) {
if (json.size() != Count1) {
throw runtime_error("incorrect array length");
}
for (size_t z = 0; z < Count1; z++) {
from_json_into(json.at(z), ret[z]);
}
}
CommonItemSet::Table::Table(const JSON& json, Episode episode)
: episode(episode) {
from_json_into(json.at("BaseWeaponTypeProbTable"), this->base_weapon_type_prob_table);
from_json_into(json.at("SubtypeBaseTable"), this->subtype_base_table);
from_json_into(json.at("SubtypeAreaLengthTable"), this->subtype_area_length_table);
from_json_into(json.at("GrindProbTable"), this->grind_prob_table);
from_json_into(json.at("ArmorShieldTypeIndexProbTable"), this->armor_shield_type_index_prob_table);
from_json_into(json.at("ArmorSlotCountProbTable"), this->armor_slot_count_prob_table);
from_json_into(json.at("BoxMesetaRanges"), this->box_meseta_ranges);
this->has_rare_bonus_value_prob_table = json.at("HasRareBonusValueProbTable").as_bool();
from_json_into(json.at("BonusValueProbTable"), this->bonus_value_prob_table);
from_json_into(json.at("NonRareBonusProbSpec"), this->nonrare_bonus_prob_spec);
from_json_into(json.at("BonusTypeProbTable"), this->bonus_type_prob_table);
from_json_into(json.at("SpecialMult"), this->special_mult);
from_json_into(json.at("SpecialPercent"), this->special_percent);
from_json_into(json.at("ToolClassProbTable"), this->tool_class_prob_table);
from_json_into(json.at("TechniqueIndexProbTable"), this->technique_index_prob_table);
from_json_into(json.at("TechniqueLevelRanges"), this->technique_level_ranges);
this->armor_or_shield_type_bias = json.at("ArmorOrShieldTypeBias").as_int();
from_json_into(json.at("UnitMaxStarsTable"), this->unit_max_stars_table);
from_json_into(json.at("BoxItemClassProbTable"), this->box_item_class_prob_table);
const auto& enemy_meseta_ranges_json = json.at("EnemyMesetaRanges").as_dict();
const auto& enemy_type_drop_probs_json = json.at("EnemyTypeDropProbs").as_dict();
const auto& enemy_item_classes_json = json.at("EnemyItemClasses").as_dict();
for (size_t z = 0; z < 0x64; z++) {
static const array<Episode, 3> episodes = {Episode::EP1, Episode::EP2, Episode::EP4};
for (Episode episode : episodes) {
for (auto type : enemy_types_for_rare_table_index(episode, z)) {
string name = string_printf("%s:%s", abbreviation_for_episode(episode), name_for_enum(type));
from_json_into(*enemy_meseta_ranges_json.at(name), this->enemy_meseta_ranges[z]);
this->enemy_type_drop_probs[z] = enemy_type_drop_probs_json.at(name)->as_int();
this->enemy_item_classes[z] = enemy_item_classes_json.at(name)->as_int();
}
}
}
}
static const char* name_for_common_item_class(uint8_t item_class) {
switch (item_class) {
case 0x00:
return "WEAPON ";
case 0x01:
return "ARMOR ";
case 0x02:
return "SHIELD ";
case 0x03:
return "UNIT ";
case 0x04:
return "TOOL ";
case 0x05:
return "MESETA ";
case 0x06:
return "NOTHING";
default:
return "UNKNOWN";
}
}
void CommonItemSet::Table::print(FILE* stream) const {
const auto& meseta_ranges = this->enemy_meseta_ranges;
const auto& drop_probs = this->enemy_type_drop_probs;
const auto& item_classes = this->enemy_item_classes;
fprintf(stream, "Enemy tables:\n");
fprintf(stream, " ## $LOW $HIGH DAR%% ITEM ENEMIES\n");
for (size_t z = 0; z < 0x64; z++) {
string enemies_str;
for (EnemyType enemy_type : enemy_types_for_rare_table_index(this->episode, z)) {
if (!enemies_str.empty()) {
enemies_str += ", ";
}
enemies_str += name_for_enum(enemy_type);
}
if (drop_probs[z]) {
fprintf(stream, " %02zX %5hu %5hu %3hhu%% %02hX:%s %s\n",
z, meseta_ranges[z].min, meseta_ranges[z].max, drop_probs[z], item_classes[z],
name_for_common_item_class(item_classes[z]), enemies_str.c_str());
} else {
fprintf(stream, " %02zX ----- ----- 0%% -- %s\n", z, enemies_str.c_str());
}
}
static const array<const char*, 12> base_weapon_type_names = {
"SABER ",
"SWORD ",
"DAGGER ",
"PARTISAN",
"SLICER ",
"HANDGUN ",
"RIFLE ",
"MECHGUN ",
"SHOT ",
"CANE ",
"ROD ",
"WAND ",
};
fprintf(stream, "Base weapon config:\n");
fprintf(stream, " TYPE PROB [SB AL] FLOORS\n");
for (size_t z = 0; z < 12; z++) {
uint8_t floor_to_class[10];
if (this->subtype_base_table[z] < 0) {
size_t start_floor = std::min<size_t>(-this->subtype_area_length_table[z], 10);
for (size_t x = 0; x < start_floor; x++) {
floor_to_class[x] = 0xFF;
}
for (size_t x = start_floor; x < 10; x++) {
floor_to_class[x] = (x - start_floor) / this->subtype_area_length_table[z];
}
} else {
for (size_t x = 0; x < 10; x++) {
floor_to_class[x] = this->subtype_base_table[z] + (x / this->subtype_area_length_table[z]);
}
}
fprintf(stream, " %02zX:%s %3hhu%% [%02hhX %02hhX] %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX\n",
z, base_weapon_type_names[z], this->base_weapon_type_prob_table[z],
this->subtype_base_table[z], this->subtype_area_length_table[z],
floor_to_class[0], floor_to_class[1], floor_to_class[2], floor_to_class[3], floor_to_class[4],
floor_to_class[5], floor_to_class[6], floor_to_class[7], floor_to_class[8], floor_to_class[9]);
}
fprintf(stream, "Box configuration:\n");
fprintf(stream, " AR $LOW $HIGH WEP%% ARM%% SHD%% UNI%% TL%% MST%% NO%%\n");
for (size_t z = 0; z < 10; z++) {
fprintf(stream, " %02zX %5hu %5hu %3hhu%% %3hhu%% %3hhu%% %3hhu%% %3hhu%% %3hhu%% %3hhu%%\n",
z, this->box_meseta_ranges[z].min, this->box_meseta_ranges[z].max,
this->box_item_class_prob_table[0][z],
this->box_item_class_prob_table[1][z],
this->box_item_class_prob_table[2][z],
this->box_item_class_prob_table[3][z],
this->box_item_class_prob_table[4][z],
this->box_item_class_prob_table[5][z],
this->box_item_class_prob_table[6][z]);
}
fprintf(stream, "Weapon drops:\n");
fprintf(stream, " Grinds:\n");
fprintf(stream, " GD AR0%% AR1%% AR2%% AR3%%\n");
for (size_t z = 0; z < 9; z++) {
fprintf(stream, " +%zu %3hhd%% %3hhd%% %3hhd%% %3hhd%%\n", z,
this->grind_prob_table[z][0], this->grind_prob_table[z][1],
this->grind_prob_table[z][2], this->grind_prob_table[z][3]);
}
fprintf(stream, " Bonus value table:\n");
fprintf(stream, " ID");
for (int8_t v = -10; v <= 100; v += 5) {
fprintf(stream, " %5hhd%%", v);
}
fputc('\n', stream);
for (size_t z = 0; z < (this->has_rare_bonus_value_prob_table ? 6 : 5); z++) {
fprintf(stream, " %02zX", z);
for (size_t x = 0; x < 0x17; x++) {
fprintf(stream, " %5hu#", this->bonus_value_prob_table[x][z]);
}
fputc('\n', stream);
}
fprintf(stream, " Area config tables:\n");
fprintf(stream, " AR BONUS SP NO%% NTV%% AB%% MAC%% DRK%% HIT%% SM SPC%%\n");
for (size_t z = 0; z < 10; z++) {
fprintf(stream, " %02zX %02hhX %02hhX %02hhX %3hhu%% %3hhu%% %3hhu%% %3hhu%% %3hhu%% %3hhu%% %02hhX %3hhu%%\n",
z, this->nonrare_bonus_prob_spec[0][z], this->nonrare_bonus_prob_spec[1][z], this->nonrare_bonus_prob_spec[2][z],
this->bonus_type_prob_table[0][z], this->bonus_type_prob_table[1][z], this->bonus_type_prob_table[2][z],
this->bonus_type_prob_table[3][z], this->bonus_type_prob_table[4][z], this->bonus_type_prob_table[5][z],
this->special_mult[z], this->special_percent[z]);
}
fprintf(stream, " Tool class table:\n");
fprintf(stream, " CS A1 A2 A3 A4 A5 A6 A7 A8 A9 A10\n");
for (size_t tool_class = 0; tool_class < this->tool_class_prob_table.size(); tool_class++) {
fprintf(stream, " %02zX", tool_class);
for (size_t area_norm = 0; area_norm < 10; area_norm++) {
fprintf(stream, " %5hu", this->tool_class_prob_table[tool_class][area_norm]);
}
fputc('\n', stream);
}
static const array<const char*, 19> technique_names = {
"FOIE ",
"GIFOIE ",
"RAFOIE ",
"BARTA ",
"GIBARTA ",
"RABARTA ",
"ZONDE ",
"GIZONDE ",
"RAZONDE ",
"GRANTS ",
"DEBAND ",
"JELLEN ",
"ZALURE ",
"SHIFTA ",
"RYUKER ",
"RESTA ",
"ANTI ",
"REVERSER",
"MEGID ",
};
fprintf(stream, " Technique table:\n");
fprintf(stream, " TECH A1 A2 A3 A4 A5 A6 A7 A8 A9 A10\n");
for (size_t tech_num = 0; tech_num < this->technique_index_prob_table.size(); tech_num++) {
fprintf(stream, " %02zX:%s", tech_num, technique_names[tech_num]);
for (size_t area_norm = 0; area_norm < 10; area_norm++) {
uint16_t prob = this->technique_index_prob_table[tech_num][area_norm];
if (prob) {
const auto& level_range = this->technique_level_ranges[tech_num][area_norm];
size_t min_level = level_range.min + 1;
size_t max_level = level_range.max + 1;
fprintf(stream, " %5hu[%2zu-%2zu]", prob, min_level, max_level);
} else {
fprintf(stream, " 0[-----]");
}
}
fputc('\n', stream);
}
fprintf(stream, " Armor/shield type bias: %hhu\n", this->armor_or_shield_type_bias);
fprintf(stream, " Armor/shield type index table:\n");
fprintf(stream, " TY PROB\n");
for (size_t z = 0; z < 5; z++) {
fprintf(stream, " %02zX %3hhu%%\n", z, this->armor_shield_type_index_prob_table[z]);
}
fprintf(stream, " Armor/shield slot count table:\n");
fprintf(stream, " #S PROB\n");
for (size_t z = 0; z < 5; z++) {
fprintf(stream, " %02zX %3hhu%%\n", z, this->armor_slot_count_prob_table[z]);
}
fprintf(stream, " Unit maximum stars table:\n");
fprintf(stream, " AR #*\n");
for (size_t z = 0; z < 10; z++) {
fprintf(stream, " %02zX %3hhu\n", z, this->unit_max_stars_table[z]);
}
}
JSON CommonItemSet::Table::json() const { JSON CommonItemSet::Table::json() const {
JSON enemy_meseta_ranges_json = JSON::dict(); JSON enemy_meseta_ranges_json = JSON::dict();
JSON enemy_type_drop_probs_json = JSON::dict(); JSON enemy_type_drop_probs_json = JSON::dict();
@@ -50,19 +342,12 @@ JSON CommonItemSet::Table::json() const {
for (size_t z = 0; z < 0x64; z++) { for (size_t z = 0; z < 0x64; z++) {
static const array<Episode, 3> episodes = {Episode::EP1, Episode::EP2, Episode::EP4}; static const array<Episode, 3> episodes = {Episode::EP1, Episode::EP2, Episode::EP4};
for (Episode episode : episodes) { for (Episode episode : episodes) {
JSON enemy_meseta_ranges_episode_json = JSON::dict();
JSON enemy_type_drop_probs_episode_json = JSON::dict();
JSON enemy_item_classes_episode_json = JSON::dict();
for (auto type : enemy_types_for_rare_table_index(episode, z)) { for (auto type : enemy_types_for_rare_table_index(episode, z)) {
string type_str = name_for_enum(type); string name = string_printf("%s:%s", abbreviation_for_episode(episode), name_for_enum(type));
enemy_meseta_ranges_episode_json.emplace(type_str, to_json(this->enemy_meseta_ranges[z])); enemy_meseta_ranges_json.emplace(name, to_json(this->enemy_meseta_ranges[z]));
enemy_type_drop_probs_episode_json.emplace(type_str, this->enemy_type_drop_probs[z]); enemy_type_drop_probs_json.emplace(name, this->enemy_type_drop_probs[z]);
enemy_item_classes_episode_json.emplace(type_str, this->enemy_item_classes[z]); enemy_item_classes_json.emplace(name, this->enemy_item_classes[z]);
} }
string name = name_for_episode(episode);
enemy_meseta_ranges_json.emplace(name, std::move(enemy_meseta_ranges_episode_json));
enemy_type_drop_probs_json.emplace(name, std::move(enemy_type_drop_probs_episode_json));
enemy_item_classes_json.emplace(name, std::move(enemy_item_classes_episode_json));
} }
} }
return JSON::dict({ return JSON::dict({
@@ -118,7 +403,28 @@ JSON CommonItemSet::json() const {
return modes_dict; return modes_dict;
} }
CommonItemSet::Table::Table(const StringReader& r, bool is_big_endian, bool is_v3) { void CommonItemSet::print(FILE* stream) const {
static const array<GameMode, 4> modes = {GameMode::NORMAL, GameMode::BATTLE, GameMode::CHALLENGE, GameMode::SOLO};
for (const auto& mode : modes) {
static const array<Episode, 3> episodes = {Episode::EP1, Episode::EP2, Episode::EP4};
for (const auto& episode : episodes) {
for (uint8_t difficulty = 0; difficulty < 4; difficulty++) {
for (uint8_t section_id = 0; section_id < 10; section_id++) {
try {
auto table = this->get_table(episode, mode, difficulty, section_id);
fprintf(stream, "============ %s %s %s %s\n",
name_for_mode(mode), name_for_episode(episode), name_for_difficulty(difficulty), name_for_section_id(section_id));
table->print(stream);
} catch (const runtime_error&) {
}
}
}
}
}
}
CommonItemSet::Table::Table(const StringReader& r, bool is_big_endian, bool is_v3, Episode episode)
: episode(episode) {
if (is_big_endian) { if (is_big_endian) {
this->parse_itempt_t<true>(r, is_v3); this->parse_itempt_t<true>(r, is_v3);
} else { } else {
@@ -131,7 +437,7 @@ void CommonItemSet::Table::parse_itempt_t(const StringReader& r, bool is_v3) {
using U16T = typename std::conditional<IsBigEndian, be_uint16_t, le_uint16_t>::type; using U16T = typename std::conditional<IsBigEndian, be_uint16_t, le_uint16_t>::type;
using U32T = typename std::conditional<IsBigEndian, be_uint32_t, le_uint32_t>::type; using U32T = typename std::conditional<IsBigEndian, be_uint32_t, le_uint32_t>::type;
const auto& offsets = r.pget<Offsets<IsBigEndian>>(r.pget<U32T>(r.size() - 0x10)); const auto& offsets = r.pget<OffsetsT<IsBigEndian>>(r.pget<U32T>(r.size() - 0x10));
this->base_weapon_type_prob_table = r.pget<parray<uint8_t, 0x0C>>(offsets.base_weapon_type_prob_table_offset); this->base_weapon_type_prob_table = r.pget<parray<uint8_t, 0x0C>>(offsets.base_weapon_type_prob_table_offset);
this->subtype_base_table = r.pget<parray<int8_t, 0x0C>>(offsets.subtype_base_table_offset); this->subtype_base_table = r.pget<parray<int8_t, 0x0C>>(offsets.subtype_base_table_offset);
@@ -186,41 +492,6 @@ void CommonItemSet::Table::parse_itempt_t(const StringReader& r, bool is_v3) {
this->box_item_class_prob_table = r.pget<parray<parray<uint8_t, 10>, 7>>(offsets.box_item_class_prob_table_offset); this->box_item_class_prob_table = r.pget<parray<parray<uint8_t, 10>, 7>>(offsets.box_item_class_prob_table_offset);
} }
void CommonItemSet::Table::print_enemy_table(FILE* stream) const {
const auto& meseta_ranges = this->enemy_meseta_ranges;
const auto& drop_probs = this->enemy_type_drop_probs;
const auto& item_classes = this->enemy_item_classes;
// const parray<Range<uint16_t>, 0x64>& enemy_meseta_ranges() const;
// const parray<uint8_t, 0x64>& enemy_type_drop_probs() const;
// const parray<uint8_t, 0x64>& enemy_item_classes() const;
fprintf(stream, "## $_LOW $_HIGH DAR ITEM\n");
for (size_t z = 0; z < 0x64; z++) {
const char* item_class_name = "__UNKNOWN__";
switch (item_classes[z]) {
case 0x00:
item_class_name = "WEAPON";
break;
case 0x01:
item_class_name = "ARMOR";
break;
case 0x02:
item_class_name = "SHIELD";
break;
case 0x03:
item_class_name = "UNIT";
break;
case 0x04:
item_class_name = "TOOL";
break;
case 0x05:
item_class_name = "MESETA";
break;
}
fprintf(stream, "%02zX %5hu %5hu %3hhu %02hX (%s)\n",
z, meseta_ranges[z].min, meseta_ranges[z].max, drop_probs[z], item_classes[z], item_class_name);
}
}
uint16_t CommonItemSet::key_for_table(Episode episode, GameMode mode, uint8_t difficulty, uint8_t secid) { uint16_t CommonItemSet::key_for_table(Episode episode, GameMode mode, uint8_t difficulty, uint8_t secid) {
// Bits: -----EEEMMDDSSSS // Bits: -----EEEMMDDSSSS
return (((static_cast<uint16_t>(episode) << 8) & 0x0700) | return (((static_cast<uint16_t>(episode) << 8) & 0x0700) |
@@ -248,7 +519,7 @@ AFSV2CommonItemSet::AFSV2CommonItemSet(
for (size_t section_id = 0; section_id < 10; section_id++) { for (size_t section_id = 0; section_id < 10; section_id++) {
auto entry = pt_afs.get(difficulty * 10 + section_id); auto entry = pt_afs.get(difficulty * 10 + section_id);
StringReader r(entry.first, entry.second); StringReader r(entry.first, entry.second);
auto table = make_shared<Table>(r, false, false); auto table = make_shared<Table>(r, false, false, Episode::EP1);
this->tables.emplace(this->key_for_table(Episode::EP1, GameMode::NORMAL, difficulty, section_id), table); this->tables.emplace(this->key_for_table(Episode::EP1, GameMode::NORMAL, difficulty, section_id), table);
this->tables.emplace(this->key_for_table(Episode::EP1, GameMode::BATTLE, difficulty, section_id), table); this->tables.emplace(this->key_for_table(Episode::EP1, GameMode::BATTLE, difficulty, section_id), table);
this->tables.emplace(this->key_for_table(Episode::EP1, GameMode::SOLO, difficulty, section_id), table); this->tables.emplace(this->key_for_table(Episode::EP1, GameMode::SOLO, difficulty, section_id), table);
@@ -260,7 +531,7 @@ AFSV2CommonItemSet::AFSV2CommonItemSet(
AFSArchive ct_afs(ct_afs_data); AFSArchive ct_afs(ct_afs_data);
for (size_t difficulty = 0; difficulty < 4; difficulty++) { for (size_t difficulty = 0; difficulty < 4; difficulty++) {
auto r = ct_afs.get_reader(difficulty * 10); auto r = ct_afs.get_reader(difficulty * 10);
auto table = make_shared<Table>(r, false, false); auto table = make_shared<Table>(r, false, false, Episode::EP1);
for (size_t section_id = 0; section_id < 10; section_id++) { for (size_t section_id = 0; section_id < 10; section_id++) {
this->tables.emplace(this->key_for_table(Episode::EP1, GameMode::CHALLENGE, difficulty, section_id), table); this->tables.emplace(this->key_for_table(Episode::EP1, GameMode::CHALLENGE, difficulty, section_id), table);
} }
@@ -312,7 +583,7 @@ GSLV3V4CommonItemSet::GSLV3V4CommonItemSet(std::shared_ptr<const std::string> gs
throw; throw;
} }
} }
auto table = make_shared<Table>(r, is_big_endian, true); auto table = make_shared<Table>(r, is_big_endian, true, episode);
this->tables.emplace(this->key_for_table(episode, GameMode::NORMAL, difficulty, section_id), table); this->tables.emplace(this->key_for_table(episode, GameMode::NORMAL, difficulty, section_id), table);
this->tables.emplace(this->key_for_table(episode, GameMode::BATTLE, difficulty, section_id), table); this->tables.emplace(this->key_for_table(episode, GameMode::BATTLE, difficulty, section_id), table);
this->tables.emplace(this->key_for_table(episode, GameMode::SOLO, difficulty, section_id), table); this->tables.emplace(this->key_for_table(episode, GameMode::SOLO, difficulty, section_id), table);
@@ -322,7 +593,7 @@ GSLV3V4CommonItemSet::GSLV3V4CommonItemSet(std::shared_ptr<const std::string> gs
if (episode != Episode::EP4) { if (episode != Episode::EP4) {
for (size_t difficulty = 0; difficulty < 4; difficulty++) { for (size_t difficulty = 0; difficulty < 4; difficulty++) {
auto r = gsl.get_reader(filename_for_table(episode, difficulty, 0, true)); auto r = gsl.get_reader(filename_for_table(episode, difficulty, 0, true));
auto table = make_shared<Table>(r, is_big_endian, true); auto table = make_shared<Table>(r, is_big_endian, true, episode);
for (size_t section_id = 0; section_id < 10; section_id++) { for (size_t section_id = 0; section_id < 10; section_id++) {
this->tables.emplace(this->key_for_table(episode, GameMode::CHALLENGE, difficulty, section_id), table); this->tables.emplace(this->key_for_table(episode, GameMode::CHALLENGE, difficulty, section_id), table);
} }
@@ -331,6 +602,33 @@ GSLV3V4CommonItemSet::GSLV3V4CommonItemSet(std::shared_ptr<const std::string> gs
} }
} }
JSONCommonItemSet::JSONCommonItemSet(const JSON& json) {
for (const auto& mode_it : json.as_dict()) {
static const unordered_map<string, GameMode> mode_keys(
{{"Normal", GameMode::NORMAL}, {"Battle", GameMode::BATTLE}, {"Challenge", GameMode::CHALLENGE}, {"Solo", GameMode::SOLO}});
GameMode mode = mode_keys.at(mode_it.first);
for (const auto& episode_it : mode_it.second->as_dict()) {
static const unordered_map<string, Episode> episode_keys(
{{"Episode1", Episode::EP1}, {"Episode2", Episode::EP2}, {"Episode4", Episode::EP4}});
Episode episode = episode_keys.at(episode_it.first);
for (const auto& difficulty_it : episode_it.second->as_dict()) {
static const unordered_map<string, uint8_t> difficulty_keys(
{{"Normal", 0}, {"Hard", 1}, {"VeryHard", 2}, {"Ultimate", 3}});
uint8_t difficulty = difficulty_keys.at(difficulty_it.first);
for (const auto& section_id_it : difficulty_it.second->as_dict()) {
uint8_t section_id = section_id_for_name(section_id_it.first);
this->tables.emplace(
this->key_for_table(episode, mode, difficulty, section_id),
make_shared<Table>(*section_id_it.second, episode));
}
}
}
}
}
RELFileSet::RELFileSet(std::shared_ptr<const std::string> data) RELFileSet::RELFileSet(std::shared_ptr<const std::string> data)
: data(data), : data(data),
r(*this->data) {} r(*this->data) {}
+27 -13
View File
@@ -14,14 +14,16 @@ public:
class Table { class Table {
public: public:
Table() = delete; Table() = delete;
Table(const StringReader& r, bool big_endian, bool is_v3); Table(const JSON& json, Episode episode);
Table(const StringReader& r, bool big_endian, bool is_v3, Episode episode);
template <typename IntT> template <typename IntT>
struct Range { struct Range {
IntT min; IntT min;
IntT max; IntT max;
} __attribute__((packed)); } __packed__;
Episode episode;
parray<uint8_t, 0x0C> base_weapon_type_prob_table; parray<uint8_t, 0x0C> base_weapon_type_prob_table;
parray<int8_t, 0x0C> subtype_base_table; parray<int8_t, 0x0C> subtype_base_table;
parray<uint8_t, 0x0C> subtype_area_length_table; parray<uint8_t, 0x0C> subtype_area_length_table;
@@ -45,15 +47,15 @@ public:
parray<uint8_t, 0x0A> unit_max_stars_table; parray<uint8_t, 0x0A> unit_max_stars_table;
parray<parray<uint8_t, 10>, 7> box_item_class_prob_table; parray<parray<uint8_t, 10>, 7> box_item_class_prob_table;
void print_enemy_table(FILE* stream) const;
JSON json() const; JSON json() const;
void print(FILE* stream) const;
private: private:
template <bool IsBigEndian> template <bool IsBigEndian>
void parse_itempt_t(const StringReader& r, bool is_v3); void parse_itempt_t(const StringReader& r, bool is_v3);
template <bool IsBigEndian> template <bool IsBigEndian>
struct Offsets { struct OffsetsT {
using U16T = typename std::conditional<IsBigEndian, be_uint16_t, le_uint16_t>::type; using U16T = typename std::conditional<IsBigEndian, be_uint16_t, le_uint16_t>::type;
using U32T = typename std::conditional<IsBigEndian, be_uint32_t, le_uint32_t>::type; using U32T = typename std::conditional<IsBigEndian, be_uint32_t, le_uint32_t>::type;
@@ -254,11 +256,16 @@ public:
/* 50 */ U32T box_item_class_prob_table_offset; /* 50 */ U32T box_item_class_prob_table_offset;
// There are several unused fields here. // There are several unused fields here.
} __attribute__((packed)); } __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; std::shared_ptr<const Table> get_table(Episode episode, GameMode mode, uint8_t difficulty, uint8_t secid) const;
JSON json() const; JSON json() const;
void print(FILE* stream) const;
protected: protected:
CommonItemSet() = default; CommonItemSet() = default;
@@ -278,6 +285,11 @@ public:
GSLV3V4CommonItemSet(std::shared_ptr<const std::string> gsl_data, bool is_big_endian); GSLV3V4CommonItemSet(std::shared_ptr<const std::string> gsl_data, bool is_big_endian);
}; };
class JSONCommonItemSet : public CommonItemSet {
public:
explicit JSONCommonItemSet(const JSON& json);
};
// Note: There are clearly better ways of doing this, but this implementation // Note: There are clearly better ways of doing this, but this implementation
// closely follows what the original code in the client does. // closely follows what the original code in the client does.
template <typename ItemT, size_t MaxCount> template <typename ItemT, size_t MaxCount>
@@ -327,10 +339,12 @@ public:
struct WeightTableEntry { struct WeightTableEntry {
ValueT value; ValueT value;
WeightT weight; WeightT weight;
} __attribute__((packed)); } __packed__;
using WeightTableEntry8 = WeightTableEntry<uint8_t>; using WeightTableEntry8 = WeightTableEntry<uint8_t>;
using WeightTableEntry32 = WeightTableEntry<be_uint32_t>; using WeightTableEntry32 = WeightTableEntry<be_uint32_t>;
check_struct_size(WeightTableEntry8, 2);
check_struct_size(WeightTableEntry32, 8);
protected: protected:
std::shared_ptr<const std::string> data; std::shared_ptr<const std::string> data;
@@ -340,7 +354,7 @@ protected:
be_uint32_t offset; be_uint32_t offset;
uint8_t entries_per_table; uint8_t entries_per_table;
parray<uint8_t, 3> unused; parray<uint8_t, 3> unused;
} __attribute__((packed)); } __packed_ws__(TableSpec, 8);
RELFileSet(std::shared_ptr<const std::string> data); RELFileSet(std::shared_ptr<const std::string> data);
@@ -381,7 +395,7 @@ public:
Mode mode; Mode mode;
uint8_t player_level_divisor_or_min_level; uint8_t player_level_divisor_or_min_level;
uint8_t max_level; uint8_t max_level;
} __attribute__((packed)); } __packed_ws__(TechDiskLevelEntry, 3);
std::pair<const uint8_t*, size_t> get_common_recovery_table(size_t index) const; std::pair<const uint8_t*, size_t> get_common_recovery_table(size_t index) const;
std::pair<const WeightTableEntry8*, size_t> get_rare_recovery_table(size_t index) const; std::pair<const WeightTableEntry8*, size_t> get_rare_recovery_table(size_t index) const;
@@ -403,7 +417,7 @@ public:
struct RangeTableEntry { struct RangeTableEntry {
be_uint32_t min; be_uint32_t min;
be_uint32_t max; be_uint32_t max;
} __attribute__((packed)); } __packed_ws__(RangeTableEntry, 8);
std::pair<const WeightTableEntry8*, size_t> get_weapon_type_table(size_t index) const; std::pair<const WeightTableEntry8*, size_t> get_weapon_type_table(size_t index) const;
const parray<WeightTableEntry32, 6>* get_bonus_type_table(size_t which, size_t index) const; const parray<WeightTableEntry32, 6>* get_bonus_type_table(size_t which, size_t index) const;
@@ -422,7 +436,7 @@ private:
be_uint32_t special_mode_table; // [[{u32 value, u32 weight}](3)](8) be_uint32_t special_mode_table; // [[{u32 value, u32 weight}](3)](8)
be_uint32_t standard_grind_range_table; // [{u32 min, u32 max}](6) be_uint32_t standard_grind_range_table; // [{u32 min, u32 max}](6)
be_uint32_t favored_grind_range_table; // [{u32 min, u32 max}](6) be_uint32_t favored_grind_range_table; // [{u32 min, u32 max}](6)
} __attribute__((packed)); } __packed_ws__(Offsets, 0x20);
const Offsets* offsets; const Offsets* offsets;
}; };
@@ -455,11 +469,11 @@ private:
uint8_t delta_index; uint8_t delta_index;
uint8_t count_default; uint8_t count_default;
uint8_t count_favored; uint8_t count_favored;
} __attribute__((packed)); } __packed_ws__(DeltaProbabilityEntry, 3);
struct LuckTableEntry { struct LuckTableEntry {
uint8_t delta_index; uint8_t delta_index;
int8_t luck; int8_t luck;
} __attribute__((packed)); } __packed_ws__(LuckTableEntry, 2);
struct Offsets { struct Offsets {
// Each section ID's favored weapon class has different probabilities than // Each section ID's favored weapon class has different probabilities than
@@ -565,7 +579,7 @@ private:
// In PSO V3, the bonus delta luck table is: // In PSO V3, the bonus delta luck table is:
// +10 => +15, +5 => +8, 0 => 0, -5 => -8, -10 => -15 // +10 => +15, +5 => +8, 0 => 0, -5 => -8, -10 => -15
be_uint32_t bonus_delta_luck_offset; // LuckTableEntry[...]; ending with FF FF be_uint32_t bonus_delta_luck_offset; // LuckTableEntry[...]; ending with FF FF
} __attribute__((packed)); } __packed_ws__(Offsets, 0x18);
const Offsets* offsets; const Offsets* offsets;
+64 -18
View File
@@ -438,6 +438,45 @@ string prs_compress_optimal(const string& data, ProgressCallback progress_fn) {
return prs_compress_optimal(data.data(), data.size(), progress_fn); return prs_compress_optimal(data.data(), data.size(), 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.
WindowIndex<0x1FFF, 1> window(in_data, size);
LZSSInterleavedWriter w;
for (size_t z = 0; z < size; z++) {
auto match = window.get_best_match();
if (match.second >= 1) {
// Write extended copy
int16_t offset = match.first - window.offset;
w.write_control(false);
w.flush_if_ready();
w.write_control(true);
uint16_t a = (offset << 3);
w.write_data(a & 0xFF);
w.write_data(a >> 8);
w.write_data(0);
} else {
// Write literal
w.write_control(true);
w.write_data(in_data[z]);
}
w.flush_if_ready();
window.advance();
}
// Write stop command
w.write_control(false);
w.flush_if_ready();
w.write_control(true);
w.write_data(0);
w.write_data(0);
return std::move(w.close());
}
PRSCompressor::PRSCompressor( PRSCompressor::PRSCompressor(
ssize_t compression_level, ProgressCallback progress_fn) ssize_t compression_level, ProgressCallback progress_fn)
: compression_level(compression_level), : compression_level(compression_level),
@@ -976,41 +1015,48 @@ void prs_disassemble(FILE* stream, const void* data, size_t size) {
ControlStreamReader cr(r); ControlStreamReader cr(r);
while (!r.eof()) { while (!r.eof()) {
uint8_t buffered_bits = cr.buffered_bits();
if (cr.read()) { if (cr.read()) {
fprintf(stream, "[%zX] literal %02hhX\n", output_bytes, r.get_u8()); uint8_t literal_value = r.get_u8();
fprintf(stream, "[%zX] %hhu> 1 %02hhX literal %02hhX\n",
output_bytes, buffered_bits, literal_value, literal_value);
output_bytes++; output_bytes++;
} else { } else {
ssize_t offset; size_t count, read_offset;
size_t count;
const char* copy_type;
if (cr.read()) { if (cr.read()) {
uint16_t a = r.get_u8(); uint8_t a_low = r.get_u8();
a |= (r.get_u8() << 8); uint8_t a_high = r.get_u8();
offset = (a >> 3) | (~0x1FFF); uint16_t a = (a_high << 8) | a_low;
ssize_t offset = (a >> 3) | (~0x1FFF);
if (offset == ~0x1FFF) { if (offset == ~0x1FFF) {
fprintf(stream, "[%zX] end\n", output_bytes); fprintf(stream, "[%zX] end\n", output_bytes);
break; break;
} }
if (a & 7) { if (a & 7) {
copy_type = "long";
count = (a & 7) + 2; 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",
output_bytes, buffered_bits, a_low, a_high, offset, read_offset, count);
} else { } else {
copy_type = "extended"; uint8_t count_u8 = r.get_u8();
count = r.get_u8() + 1; 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",
output_bytes, buffered_bits, a_low, a_high, count_u8, offset, read_offset, count);
} }
} else { } else {
copy_type = "short"; bool first_bit = cr.read();
count = cr.read() << 1; bool second_bit = cr.read();
count = (count | cr.read()) + 2; uint8_t offset_u8 = r.get_u8();
offset = r.get_u8() | (~0xFF); 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",
output_bytes, buffered_bits, first_bit ? '1' : '0', second_bit ? '1' : '0', offset_u8, offset, read_offset, count);
} }
size_t read_offset = output_bytes + offset;
fprintf(stream, "[%zX] %s copy %zX\n", output_bytes, copy_type, count);
if (read_offset >= output_bytes) { if (read_offset >= output_bytes) {
throw runtime_error("backreference offset beyond beginning of output"); throw runtime_error("backreference offset beyond beginning of output");
} }
+4
View File
@@ -177,6 +177,10 @@ std::string prs_compress_indexed(
std::string prs_compress_optimal(const void* vdata, size_t size, ProgressCallback progress_fn = nullptr); 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); 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.
std::string prs_compress_pessimal(const void* vdata, size_t size);
// Decompresses PRS-compressed data. // Decompresses PRS-compressed data.
struct PRSDecompressResult { struct PRSDecompressResult {
std::string data; std::string data;
+71
View File
@@ -1386,3 +1386,74 @@ void dc_serial_number_speed_test(uint64_t seed) {
fprintf(stderr, "Fast vs. slow speedup: %zux\n", static_cast<size_t>(time_slow / time_fast)); fprintf(stderr, "Fast vs. slow speedup: %zux\n", static_cast<size_t>(time_slow / time_fast));
fprintf(stderr, "Disagreements: %zu\n", num_disagreements); fprintf(stderr, "Disagreements: %zu\n", num_disagreements);
} }
string decrypt_dp_address_jpn(
const string& executable,
const string& values,
const string& indexes) {
StringReader values_r(values);
StringReader indexes_r(indexes);
size_t fixup_values_offset = values_r.pget_u32l(0x3FFC) - 0x8C004000;
size_t fixup_steps_offset = indexes_r.pget_u32l(0x3BFC) - 0x8C008400;
StringReader fixup_values_r = values_r.sub(fixup_values_offset);
StringReader fixup_steps_r = indexes_r.sub(fixup_steps_offset);
auto decrypted = decrypt_pr2_data<false>(executable);
size_t fixup_offset = 0;
while (fixup_steps_r.get_u8(false)) {
fixup_offset += (fixup_steps_r.get_u8() << 2);
fixup_steps_r.skip(1);
if (fixup_offset + 4 > decrypted.compressed_data.size()) {
throw runtime_error("fixup beyond end of compressed data");
}
*reinterpret_cast<le_uint32_t*>(decrypted.compressed_data.data() + fixup_offset) = fixup_values_r.get_u32l();
}
return prs_decompress(decrypted.compressed_data);
}
EncryptedDCv2Executables encrypt_dp_address_jpn(const string& executable, const string& indexes) {
EncryptedDCv2Executables ret;
string compressed = prs_compress(executable);
ret.executable = encrypt_pr2_data<false>(compressed, executable.size(), random_object<uint32_t>() & 0x7FFFFF7F);
StringReader indexes_r(indexes);
size_t fixup_steps_offset = indexes_r.pget_u32l(0x3BFC) - 0x8C008400;
ret.indexes = indexes;
ret.indexes.at(fixup_steps_offset) = 0;
return ret;
}
std::string crypt_dp_address_jpn_simple(const std::string& data, int64_t mask_key) {
if (data.size() & 3) {
throw runtime_error("size is not a multiple of 4");
}
StringReader r(data);
if (mask_key < 0) {
unordered_map<uint32_t, size_t> key_freq;
while (!r.eof()) {
key_freq[r.get_u32l()] += 1;
}
size_t max_v = 0;
for (const auto& it : key_freq) {
if (it.second > max_v) {
max_v = it.second;
mask_key = it.first;
}
}
if (mask_key < 0) {
throw runtime_error("cannot determine mask key");
}
log_info("Determined %08" PRIX64 " to be the most likely mask key", mask_key);
r.go(0);
}
StringWriter w;
while (!r.eof()) {
w.put_u32l(r.get_u32l() ^ mask_key);
}
return std::move(w.str());
}
+13
View File
@@ -21,3 +21,16 @@ 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);
void dc_serial_number_speed_test(uint64_t seed = 0xFFFFFFFFFFFFFFFF); void dc_serial_number_speed_test(uint64_t seed = 0xFFFFFFFFFFFFFFFF);
struct EncryptedDCv2Executables {
std::string executable;
std::string indexes;
};
std::string decrypt_dp_address_jpn(
const std::string& dp_address_jpn_data,
const std::string& iwashi_sea_data,
const std::string& katsuo_sea_data);
EncryptedDCv2Executables encrypt_dp_address_jpn(const std::string& executable, const std::string& indexes);
std::string crypt_dp_address_jpn_simple(const std::string& data, int64_t seed = -1);
+10 -7
View File
@@ -19,10 +19,13 @@ using namespace std;
DNSServer::DNSServer( DNSServer::DNSServer(
shared_ptr<struct event_base> base, shared_ptr<struct event_base> base,
uint32_t local_connect_address, uint32_t external_connect_address) uint32_t local_connect_address,
uint32_t external_connect_address,
shared_ptr<const IPV4RangeSet> banned_ipv4_ranges)
: base(base), : base(base),
local_connect_address(local_connect_address), local_connect_address(local_connect_address),
external_connect_address(external_connect_address) {} external_connect_address(external_connect_address),
banned_ipv4_ranges(banned_ipv4_ranges) {}
DNSServer::~DNSServer() { DNSServer::~DNSServer() {
for (const auto& it : this->fd_to_receive_event) { for (const auto& it : this->fd_to_receive_event) {
@@ -55,8 +58,7 @@ void DNSServer::dispatch_on_receive_message(evutil_socket_t fd,
reinterpret_cast<DNSServer*>(ctx)->on_receive_message(fd, events); reinterpret_cast<DNSServer*>(ctx)->on_receive_message(fd, events);
} }
string DNSServer::response_for_query( string DNSServer::response_for_query(const void* vdata, size_t size, uint32_t resolved_address) {
const void* vdata, size_t size, uint32_t resolved_address) {
if (size < 0x0C) { if (size < 0x0C) {
throw invalid_argument("query too small"); throw invalid_argument("query too small");
} }
@@ -82,7 +84,7 @@ string DNSServer::response_for_query(
void DNSServer::on_receive_message(int fd, short) { void DNSServer::on_receive_message(int fd, short) {
for (;;) { for (;;) {
sockaddr_in remote; struct sockaddr_storage remote;
socklen_t remote_size = sizeof(sockaddr_in); socklen_t remote_size = sizeof(sockaddr_in);
memset(&remote, 0, remote_size); memset(&remote, 0, remote_size);
@@ -104,9 +106,10 @@ void DNSServer::on_receive_message(int fd, short) {
dns_server_log.warning("input query too small"); dns_server_log.warning("input query too small");
print_data(stderr, input.data(), bytes); print_data(stderr, input.data(), bytes);
} else { } else if (!this->banned_ipv4_ranges->check(remote)) {
input.resize(bytes); input.resize(bytes);
uint32_t remote_address = ntohl(remote.sin_addr.s_addr); 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) uint32_t connect_address = is_local_address(remote_address)
? this->local_connect_address ? this->local_connect_address
: this->external_connect_address; : this->external_connect_address;
+14 -6
View File
@@ -7,29 +7,37 @@
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include "IPV4RangeSet.hh"
class DNSServer { class DNSServer {
public: public:
DNSServer(std::shared_ptr<struct event_base> base, DNSServer(
uint32_t local_connect_address, uint32_t external_connect_address); 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);
DNSServer(const DNSServer&) = delete; DNSServer(const DNSServer&) = delete;
DNSServer(DNSServer&&) = delete; DNSServer(DNSServer&&) = delete;
virtual ~DNSServer(); virtual ~DNSServer();
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& socket_path);
void listen(const std::string& addr, int port); void listen(const std::string& addr, int port);
void listen(int port); void listen(int port);
void add_socket(int fd); void add_socket(int fd);
static std::string response_for_query( static std::string response_for_query(const void* vdata, size_t size, uint32_t resolved_address);
const void* vdata, size_t size, uint32_t resolved_address); static std::string response_for_query(const std::string& query, uint32_t resolved_address);
static std::string response_for_query(
const std::string& query, uint32_t resolved_address);
private: private:
std::shared_ptr<struct event_base> base; std::shared_ptr<struct event_base> base;
std::unordered_map<int, std::unique_ptr<struct event, void (*)(struct event*)>> fd_to_receive_event; std::unordered_map<int, std::unique_ptr<struct event, void (*)(struct event*)>> fd_to_receive_event;
uint32_t local_connect_address; uint32_t local_connect_address;
uint32_t external_connect_address; uint32_t external_connect_address;
std::shared_ptr<const IPV4RangeSet> banned_ipv4_ranges;
static void dispatch_on_receive_message(evutil_socket_t fd, short events, void* ctx); static void dispatch_on_receive_message(evutil_socket_t fd, short events, void* ctx);
void on_receive_message(int fd, short event); void on_receive_message(int fd, short event);
+109 -4
View File
@@ -9,6 +9,12 @@ using namespace std;
namespace Episode3 { namespace Episode3 {
void BattleRecord::PlayerEntry::print(FILE* stream) const {
// TODO: Format this nicely somehow. Maybe factor out the functions in
// QuestScript that format some of these structures
print_data(stream, this, sizeof(*this), 0, nullptr, PrintDataFlags::PRINT_ASCII | PrintDataFlags::DISABLE_COLOR | PrintDataFlags::OFFSET_16_BITS);
}
BattleRecord::Event::Event(StringReader& r) { BattleRecord::Event::Event(StringReader& r) {
this->type = r.get<Event::Type>(); this->type = r.get<Event::Type>();
this->timestamp = r.get_u64l(); this->timestamp = r.get_u64l();
@@ -32,6 +38,7 @@ BattleRecord::Event::Event(StringReader& r) {
case Event::Type::GAME_COMMAND: case Event::Type::GAME_COMMAND:
case Event::Type::BATTLE_COMMAND: case Event::Type::BATTLE_COMMAND:
case Event::Type::EP3_GAME_COMMAND: case Event::Type::EP3_GAME_COMMAND:
case Event::Type::SERVER_DATA_COMMAND:
this->data = r.read(r.get_u16l()); this->data = r.read(r.get_u16l());
break; break;
default: default:
@@ -64,6 +71,7 @@ void BattleRecord::Event::serialize(StringWriter& w) const {
case Event::Type::GAME_COMMAND: case Event::Type::GAME_COMMAND:
case Event::Type::BATTLE_COMMAND: case Event::Type::BATTLE_COMMAND:
case Event::Type::EP3_GAME_COMMAND: case Event::Type::EP3_GAME_COMMAND:
case Event::Type::SERVER_DATA_COMMAND:
w.put_u16l(this->data.size()); w.put_u16l(this->data.size());
w.write(this->data); w.write(this->data);
break; break;
@@ -72,6 +80,52 @@ void BattleRecord::Event::serialize(StringWriter& w) const {
} }
} }
void BattleRecord::Event::print(FILE* stream) const {
string time_str = format_time(this->timestamp);
fprintf(stream, "Event @%016" PRIX64 " (%s) ", this->timestamp, time_str.c_str());
switch (this->type) {
case Type::PLAYER_JOIN:
fprintf(stream, "PLAYER_JOIN %02" PRIX32 "\n", this->players[0].lobby_data.client_id.load());
this->players[0].print(stream);
break;
case Type::PLAYER_LEAVE:
fprintf(stream, "PLAYER_LEAVE %02hhu\n", this->leaving_client_id);
break;
case Type::SET_INITIAL_PLAYERS:
fprintf(stream, "SET_INITIAL_PLAYERS");
for (const auto& player : this->players) {
fprintf(stream, " %02" PRIX32, player.lobby_data.client_id.load());
}
fputc('\n', stream);
for (const auto& player : this->players) {
player.print(stream);
}
break;
case Type::BATTLE_COMMAND:
fprintf(stream, "BATTLE_COMMAND\n");
print_data(stream, this->data, 0, nullptr, PrintDataFlags::PRINT_ASCII | PrintDataFlags::DISABLE_COLOR | PrintDataFlags::OFFSET_16_BITS);
break;
case Type::GAME_COMMAND:
fprintf(stream, "GAME_COMMAND\n");
print_data(stream, this->data, 0, nullptr, PrintDataFlags::PRINT_ASCII | PrintDataFlags::DISABLE_COLOR | PrintDataFlags::OFFSET_16_BITS);
break;
case Type::EP3_GAME_COMMAND:
fprintf(stream, "EP3_GAME_COMMAND\n");
print_data(stream, this->data, 0, nullptr, PrintDataFlags::PRINT_ASCII | PrintDataFlags::DISABLE_COLOR | PrintDataFlags::OFFSET_16_BITS);
break;
case Type::CHAT_MESSAGE:
fprintf(stream, "CHAT_MESSAGE %08" PRIX32 "\n", this->guild_card_number);
print_data(stream, this->data, 0, nullptr, PrintDataFlags::PRINT_ASCII | PrintDataFlags::DISABLE_COLOR | PrintDataFlags::OFFSET_16_BITS);
break;
case Type::SERVER_DATA_COMMAND:
fprintf(stream, "SERVER_DATA_COMMAND\n");
print_data(stream, this->data, 0, nullptr, PrintDataFlags::PRINT_ASCII | PrintDataFlags::DISABLE_COLOR | PrintDataFlags::OFFSET_16_BITS);
break;
default:
throw runtime_error("unknown event type in battle record");
}
}
BattleRecord::BattleRecord(uint32_t behavior_flags) BattleRecord::BattleRecord(uint32_t behavior_flags)
: is_writable(true), : is_writable(true),
behavior_flags(behavior_flags), behavior_flags(behavior_flags),
@@ -84,14 +138,23 @@ BattleRecord::BattleRecord(const string& data)
battle_start_timestamp(0), battle_start_timestamp(0),
battle_end_timestamp(0) { battle_end_timestamp(0) {
StringReader r(data); StringReader r(data);
uint64_t signature = r.get_u64l(); uint64_t signature = r.get_u64l();
if (signature != this->SIGNATURE) { bool has_random_stream;
if (signature == this->SIGNATURE_V1) {
has_random_stream = false;
} else if (signature == this->SIGNATURE_V2) {
has_random_stream = true;
} else {
throw runtime_error("incorrect battle record signature"); throw runtime_error("incorrect battle record signature");
} }
this->battle_start_timestamp = r.get_u64l(); this->battle_start_timestamp = r.get_u64l();
this->battle_end_timestamp = r.get_u64l(); this->battle_end_timestamp = r.get_u64l();
this->behavior_flags = r.get_u32l(); this->behavior_flags = r.get_u32l();
if (has_random_stream) {
this->random_stream = r.read(r.get_u32l());
}
while (!r.eof()) { while (!r.eof()) {
this->events.emplace_back(r); this->events.emplace_back(r);
} }
@@ -99,10 +162,12 @@ BattleRecord::BattleRecord(const string& data)
string BattleRecord::serialize() const { string BattleRecord::serialize() const {
StringWriter w; StringWriter w;
w.put_u64l(this->SIGNATURE); w.put_u64l(this->SIGNATURE_V2);
w.put_u64l(this->battle_start_timestamp); w.put_u64l(this->battle_start_timestamp);
w.put_u64l(this->battle_end_timestamp); w.put_u64l(this->battle_end_timestamp);
w.put_u32l(this->behavior_flags); w.put_u32l(this->behavior_flags);
w.put_u32l(this->random_stream.size());
w.write(this->random_stream);
for (const auto& ev : this->events) { for (const auto& ev : this->events) {
ev.serialize(w); ev.serialize(w);
} }
@@ -187,6 +252,24 @@ void BattleRecord::add_chat_message(
ev.data = std::move(data); ev.data = std::move(data);
} }
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;
}
bool BattleRecord::is_map_definition_event(const Event& ev) { bool BattleRecord::is_map_definition_event(const Event& ev) {
if (ev.type == Event::Type::BATTLE_COMMAND) { if (ev.type == Event::Type::BATTLE_COMMAND) {
auto& header = check_size_t<G_CardBattleCommandHeader>(ev.data, 0xFFFF); auto& header = check_size_t<G_CardBattleCommandHeader>(ev.data, 0xFFFF);
@@ -263,17 +346,35 @@ void BattleRecord::set_battle_start_timestamp() {
} }
} }
for (; it != this->events.end(); it++) { for (; it != this->events.end(); it++) {
if (it->type == Event::Type::BATTLE_COMMAND) { if ((it->type == Event::Type::BATTLE_COMMAND) || (it->type == Event::Type::SERVER_DATA_COMMAND)) {
new_events.emplace_back(std::move(*it)); new_events.emplace_back(std::move(*it));
} }
} }
this->events = std::move(new_events); this->events = std::move(new_events);
// Clear any existing random data (there shouldn't be any)
this->random_stream.clear();
} }
void BattleRecord::set_battle_end_timestamp() { void BattleRecord::set_battle_end_timestamp() {
this->battle_end_timestamp = now(); this->battle_end_timestamp = now();
} }
void BattleRecord::print(FILE* stream) const {
string start_str = format_time(this->battle_start_timestamp);
string end_str = 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",
this->is_writable ? "writable" : "read-only",
this->behavior_flags,
this->battle_start_timestamp,
start_str.c_str(),
this->battle_end_timestamp,
end_str.c_str(), this->events.size());
for (const auto& event : this->events) {
event.print(stream);
}
}
BattleRecordPlayer::BattleRecordPlayer( BattleRecordPlayer::BattleRecordPlayer(
shared_ptr<const BattleRecord> rec, shared_ptr<const BattleRecord> rec,
shared_ptr<struct event_base> base) shared_ptr<struct event_base> base)
@@ -287,7 +388,7 @@ shared_ptr<const BattleRecord> BattleRecordPlayer::get_record() const {
return this->record; return this->record;
} }
void BattleRecordPlayer::set_lobby(std::shared_ptr<Lobby> l) { void BattleRecordPlayer::set_lobby(shared_ptr<Lobby> l) {
this->lobby = l; this->lobby = l;
} }
@@ -356,6 +457,10 @@ void BattleRecordPlayer::schedule_events() {
case BattleRecord::Event::Type::CHAT_MESSAGE: case BattleRecord::Event::Type::CHAT_MESSAGE:
send_prepared_chat_message(l, ev.guild_card_number, ev.data); send_prepared_chat_message(l, ev.guild_card_number, ev.data);
break; break;
case BattleRecord::Event::Type::SERVER_DATA_COMMAND:
// These are not replayed, since the battle record also contains
// the results of these commands.
break;
} }
this->event_it++; this->event_it++;
+15 -2
View File
@@ -24,7 +24,9 @@ public:
PlayerInventory inventory; PlayerInventory inventory;
PlayerDispDataDCPCV3 disp; PlayerDispDataDCPCV3 disp;
le_uint32_t level; le_uint32_t level;
} __attribute__((packed));
void print(FILE* stream) const;
} __packed_ws__(PlayerEntry, 0x440);
struct Event { struct Event {
enum class Type : uint8_t { enum class Type : uint8_t {
@@ -35,6 +37,7 @@ public:
GAME_COMMAND = 4, GAME_COMMAND = 4,
EP3_GAME_COMMAND = 5, EP3_GAME_COMMAND = 5,
CHAT_MESSAGE = 6, CHAT_MESSAGE = 6,
SERVER_DATA_COMMAND = 7,
}; };
// Fields used for all events // Fields used for all events
@@ -52,6 +55,7 @@ public:
Event() = default; Event() = default;
explicit Event(StringReader& r); explicit Event(StringReader& r);
void serialize(StringWriter& w) const; void serialize(StringWriter& w) const;
void print(FILE* stream) const;
}; };
explicit BattleRecord(uint32_t behavior_flags); explicit BattleRecord(uint32_t behavior_flags);
@@ -72,6 +76,7 @@ public:
void add_command(Event::Type type, const void* data, size_t size); void add_command(Event::Type type, const void* data, size_t size);
void add_command(Event::Type type, std::string&& data); void add_command(Event::Type type, std::string&& data);
void add_chat_message(uint32_t guild_card_number, std::string&& data); void add_chat_message(uint32_t guild_card_number, std::string&& data);
void add_random_data(const void* data, size_t size);
// This function collapses all the existing player join/leave events into a // This function collapses all the existing player join/leave events into a
// single SET_INITIAL_PLAYERS event, and deletes all events before the latest // single SET_INITIAL_PLAYERS event, and deletes all events before the latest
// BATTLE_COMMAND command that specifies the battle map. This should provide a // BATTLE_COMMAND command that specifies the battle map. This should provide a
@@ -79,8 +84,14 @@ public:
void set_battle_start_timestamp(); void set_battle_start_timestamp();
void set_battle_end_timestamp(); void set_battle_end_timestamp();
void print(FILE* stream) const;
std::vector<std::string> get_all_server_data_commands() const;
const std::string& get_random_stream() const;
private: private:
static constexpr uint64_t SIGNATURE = 0x14C946D56D1DAC50; static constexpr uint64_t SIGNATURE_V1 = 0x14C946D56D1DAC50;
static constexpr uint64_t SIGNATURE_V2 = 0xD01E5EC12853C377;
static bool is_map_definition_event(const Event& ev); static bool is_map_definition_event(const Event& ev);
@@ -90,6 +101,7 @@ private:
uint64_t battle_start_timestamp; uint64_t battle_start_timestamp;
uint64_t battle_end_timestamp; uint64_t battle_end_timestamp;
std::deque<Event> events; std::deque<Event> events;
std::string random_stream;
friend class BattleRecordPlayer; friend class BattleRecordPlayer;
}; };
@@ -114,6 +126,7 @@ private:
std::shared_ptr<struct event_base> base; std::shared_ptr<struct event_base> base;
std::weak_ptr<Lobby> lobby; std::weak_ptr<Lobby> lobby;
std::shared_ptr<struct event> next_command_ev; std::shared_ptr<struct event> next_command_ev;
StringReader random_r;
}; };
} // namespace Episode3 } // namespace Episode3
+20 -22
View File
@@ -396,8 +396,8 @@ int16_t Card::compute_defense_power_for_attacker_card(shared_ptr<const Card> att
} }
} }
s->card_special->apply_action_conditions(3, 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(), 0x08, nullptr);
s->card_special->apply_action_conditions(3, 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(), 0x10, nullptr);
return this->action_metadata.defense_power + this->action_metadata.defense_bonus; return this->action_metadata.defense_power + this->action_metadata.defense_bonus;
} }
@@ -982,7 +982,7 @@ void Card::compute_action_chain_results(bool apply_action_conditions, bool ignor
if (apply_action_conditions) { if (apply_action_conditions) {
auto this_sh = this->shared_from_this(); auto this_sh = this->shared_from_this();
s->card_special->apply_action_conditions(3, this_sh, this_sh, 1, nullptr); 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("applied action conditions (1)");
} else { } else {
log.debug("skipped applying action conditions (1)"); log.debug("skipped applying action conditions (1)");
@@ -1120,7 +1120,7 @@ void Card::compute_action_chain_results(bool apply_action_conditions, bool ignor
if (apply_action_conditions) { if (apply_action_conditions) {
auto this_sh = this->shared_from_this(); auto this_sh = this->shared_from_this();
s->card_special->apply_action_conditions(0x03, this_sh, this_sh, 2, nullptr); 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("applied action conditions (2)");
if (!is_nte && this->action_chain.check_flag(0x100)) { if (!is_nte && this->action_chain.check_flag(0x100)) {
this->action_chain.chain.damage = min<int16_t>(this->action_chain.chain.damage + 5, 99); this->action_chain.chain.damage = min<int16_t>(this->action_chain.chain.damage + 5, 99);
@@ -1171,7 +1171,7 @@ void Card::unknown_80237F98(bool require_condition_20_or_21) {
for (ssize_t z = 8; z >= 0; z--) { for (ssize_t z = 8; z >= 0; z--) {
if (this->action_chain.conditions[z].type != ConditionType::NONE) { if (this->action_chain.conditions[z].type != ConditionType::NONE) {
if (!require_condition_20_or_21 || if (!require_condition_20_or_21 ||
s->card_special->condition_has_when_20_or_21(this->action_chain.conditions[z])) { s->card_special->condition_applies_on_sc_or_item_attack(this->action_chain.conditions[z])) {
ActionState as; ActionState as;
auto& cond = this->action_chain.conditions[z]; auto& cond = this->action_chain.conditions[z];
if (!s->card_special->is_card_targeted_by_condition(cond, as, this->shared_from_this())) { if (!s->card_special->is_card_targeted_by_condition(cond, as, this->shared_from_this())) {
@@ -1219,7 +1219,9 @@ void Card::move_phase_before() {
} }
void Card::unknown_80236374(shared_ptr<Card> other_card, const ActionState* as) { void Card::unknown_80236374(shared_ptr<Card> other_card, const ActionState* as) {
auto log = this->server()->log_stack(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 s = this->server();
auto log = s->log_stack(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 check_card = [&](shared_ptr<Card> card) -> void { auto check_card = [&](shared_ptr<Card> card) -> void {
if (card) { if (card) {
if (!card->unknown_80236554(other_card, as)) { if (!card->unknown_80236554(other_card, as)) {
@@ -1231,28 +1233,24 @@ void Card::unknown_80236374(shared_ptr<Card> other_card, const ActionState* as)
}; };
for (size_t client_id = 0; client_id < 4; client_id++) { for (size_t client_id = 0; client_id < 4; client_id++) {
auto ps = this->server()->player_states[client_id]; auto ps = s->player_states[client_id];
if (ps) { if (ps && (s->get_current_team_turn2() != ps->get_team_id())) {
if (this->server()->get_current_team_turn2() != ps->get_team_id()) {
check_card(ps->get_sc_card()); check_card(ps->get_sc_card());
for (size_t set_index = 0; set_index < 8; set_index++) { for (size_t set_index = 0; set_index < 8; set_index++) {
check_card(ps->get_set_card(set_index)); check_card(ps->get_set_card(set_index));
} }
} }
} }
}
for (size_t client_id = 0; client_id < 4; client_id++) { for (size_t client_id = 0; client_id < 4; client_id++) {
auto ps = this->server()->player_states[client_id]; auto ps = s->player_states[client_id];
if (ps) { if (ps && (s->get_current_team_turn2() == ps->get_team_id())) {
if (this->server()->get_current_team_turn2() == ps->get_team_id()) {
check_card(ps->get_sc_card()); check_card(ps->get_sc_card());
for (size_t set_index = 0; set_index < 8; set_index++) { for (size_t set_index = 0; set_index < 8; set_index++) {
check_card(ps->get_set_card(set_index)); check_card(ps->get_set_card(set_index));
} }
} }
} }
}
} }
void Card::unknown_802379BC(uint16_t card_ref) { void Card::unknown_802379BC(uint16_t card_ref) {
@@ -1404,8 +1402,8 @@ bool Card::unknown_80236554(shared_ptr<Card> other_card, const ActionState* as)
log.debug("last attack damage stats cleared"); log.debug("last attack damage stats cleared");
if (other_card) { if (other_card) {
s->card_special->apply_action_conditions(0x03, other_card, this->shared_from_this(), 0x20, as); s->card_special->apply_action_conditions(EffectWhen::BEFORE_ANY_CARD_ATTACK, other_card, this->shared_from_this(), 0x20, as);
s->card_special->apply_action_conditions(0x17, other_card, this->shared_from_this(), 0x40, as); 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)) { if (other_card->action_chain.check_flag(0x20000)) {
this->action_metadata.attack_bonus = 0; this->action_metadata.attack_bonus = 0;
return ret; return ret;
@@ -1520,7 +1518,7 @@ void Card::apply_attack_result() {
this->compute_action_chain_results(true, false); this->compute_action_chain_results(true, false);
if (!this->action_chain.check_flag(0x40)) { if (!this->action_chain.check_flag(0x40)) {
s->card_special->unknown_8024997C(this->shared_from_this()); s->card_special->apply_effects_before_attack(this->shared_from_this());
} }
this->compute_action_chain_results(true, false); this->compute_action_chain_results(true, false);
@@ -1560,21 +1558,21 @@ void Card::apply_attack_result() {
} }
} }
this->compute_action_chain_results(1, 0); this->compute_action_chain_results(true, false);
if (!this->action_chain.check_flag(0x40)) { if (!this->action_chain.check_flag(0x40)) {
s->card_special->unknown_8024997C(this->shared_from_this()); s->card_special->apply_effects_before_attack(this->shared_from_this());
} }
if (!(this->card_flags & 2)) { if (!(this->card_flags & 2)) {
this->compute_action_chain_results(1, 0); this->compute_action_chain_results(true, false);
s->card_special->check_for_attack_interference(this->shared_from_this()); s->card_special->check_for_attack_interference(this->shared_from_this());
} }
this->compute_action_chain_results(1, 0); this->compute_action_chain_results(true, false);
this->unknown_80236374(this->shared_from_this(), nullptr); this->unknown_80236374(this->shared_from_this(), nullptr);
this->unknown_802362D8(this->shared_from_this()); this->unknown_802362D8(this->shared_from_this());
} }
if (!this->action_chain.check_flag(0x40)) { if (!this->action_chain.check_flag(0x40)) {
s->card_special->unknown_8024A394(this->shared_from_this()); s->card_special->apply_effects_after_attack(this->shared_from_this());
} }
ps->stats.num_attacks_given++; ps->stats.num_attacks_given++;
+126 -106
View File
@@ -79,7 +79,7 @@ void CardSpecial::AttackEnvStats::clear() {
this->target_attack_bonus = 0; this->target_attack_bonus = 0;
this->last_attack_preliminary_damage = 0; this->last_attack_preliminary_damage = 0;
this->last_attack_damage = 0; this->last_attack_damage = 0;
this->total_last_attack_damage = 0; this->final_last_attack_damage = 0;
this->last_attack_damage_count = 0; this->last_attack_damage_count = 0;
this->target_current_hp = 0; this->target_current_hp = 0;
} }
@@ -107,7 +107,7 @@ void CardSpecial::AttackEnvStats::print(FILE* stream) const {
fprintf(stream, "(ef) non_target_team_num_set_cards = %" PRIu32 "\n", this->non_target_team_num_set_cards); fprintf(stream, "(ef) non_target_team_num_set_cards = %" PRIu32 "\n", this->non_target_team_num_set_cards);
fprintf(stream, "(ehp) target_current_hp = %" PRIu32 "\n", this->target_current_hp); fprintf(stream, "(ehp) target_current_hp = %" PRIu32 "\n", this->target_current_hp);
fprintf(stream, "(f) num_set_cards = %" PRIu32 "\n", this->num_set_cards); fprintf(stream, "(f) num_set_cards = %" PRIu32 "\n", this->num_set_cards);
fprintf(stream, "(fdm) total_last_attack_damage = %" PRIu32 "\n", this->total_last_attack_damage); fprintf(stream, "(fdm) final_last_attack_damage = %" PRIu32 "\n", this->final_last_attack_damage);
fprintf(stream, "(ff) target_team_num_set_cards = %" PRIu32 "\n", this->target_team_num_set_cards); fprintf(stream, "(ff) target_team_num_set_cards = %" PRIu32 "\n", this->target_team_num_set_cards);
fprintf(stream, "(gn) num_gun_type_items = %" PRIu32 "\n", this->num_gun_type_items); fprintf(stream, "(gn) num_gun_type_items = %" PRIu32 "\n", this->num_gun_type_items);
fprintf(stream, "(hf) num_item_or_creature_cards_in_hand = %" PRIu32 "\n", this->num_item_or_creature_cards_in_hand); fprintf(stream, "(hf) num_item_or_creature_cards_in_hand = %" PRIu32 "\n", this->num_item_or_creature_cards_in_hand);
@@ -239,7 +239,7 @@ void CardSpecial::adjust_dice_boost_if_team_has_condition_52(
} }
void CardSpecial::apply_action_conditions( void CardSpecial::apply_action_conditions(
uint8_t when, EffectWhen when,
shared_ptr<const Card> attacker_card, shared_ptr<const Card> attacker_card,
shared_ptr<Card> defender_card, shared_ptr<Card> defender_card,
uint32_t flags, uint32_t flags,
@@ -252,8 +252,7 @@ void CardSpecial::apply_action_conditions(
temp_as = *as; temp_as = *as;
} }
} else { } else {
temp_as = this->create_defense_state_for_card_pair_action_chains( temp_as = this->create_defense_state_for_card_pair_action_chains(attacker_card, defender_card);
attacker_card, defender_card);
} }
this->apply_defense_conditions(temp_as, when, defender_card, flags); this->apply_defense_conditions(temp_as, when, defender_card, flags);
@@ -291,7 +290,7 @@ bool CardSpecial::apply_attribute_guard_if_possible(
} }
bool CardSpecial::apply_defense_condition( bool CardSpecial::apply_defense_condition(
uint8_t when, EffectWhen when,
Condition* defender_cond, Condition* defender_cond,
uint8_t cond_index, uint8_t cond_index,
const ActionState& defense_state, const ActionState& defense_state,
@@ -328,7 +327,7 @@ bool CardSpecial::apply_defense_condition(
return false; return false;
} }
if ((when == 0x02) && (defender_cond->type == ConditionType::GUOM) && (flags & 4)) { if ((when == EffectWhen::AFTER_ANY_CARD_ATTACK) && (defender_cond->type == ConditionType::GUOM) && (flags & 4)) {
CardShortStatus stat = defender_card->get_short_status(); CardShortStatus stat = defender_card->get_short_status();
if (stat.card_flags & 4) { if (stat.card_flags & 4) {
G_ApplyConditionEffect_Ep3_6xB4x06 cmd; G_ApplyConditionEffect_Ep3_6xB4x06 cmd;
@@ -347,7 +346,7 @@ bool CardSpecial::apply_defense_condition(
if (s->options.is_nte()) { if (s->options.is_nte()) {
auto defender_ps = defender_card->player_state(); auto defender_ps = defender_card->player_state();
if ((when == 0x0F) && (flags & 4) && (defender_cond->type == ConditionType::DROP) && defender_ps) { if ((when == EffectWhen::BEFORE_DRAW_PHASE) && (flags & 4) && (defender_cond->type == ConditionType::DROP) && defender_ps) {
auto defender_sc_card = defender_ps->get_sc_card(); auto defender_sc_card = defender_ps->get_sc_card();
uint8_t defender_team_id = defender_ps->get_team_id(); uint8_t defender_team_id = defender_ps->get_team_id();
if (defender_sc_card && s->team_exp[defender_team_id]) { if (defender_sc_card && s->team_exp[defender_team_id]) {
@@ -364,7 +363,7 @@ bool CardSpecial::apply_defense_condition(
} }
} }
if ((when == 0x04) && (flags & 4) && !defender_has_ability_trap && (defender_cond->type == ConditionType::ACID)) { if ((when == EffectWhen::BEFORE_DICE_PHASE_THIS_TEAM_TURN) && (flags & 4) && !defender_has_ability_trap && (defender_cond->type == ConditionType::ACID)) {
int16_t hp = defender_card->get_current_hp(); int16_t hp = defender_card->get_current_hp();
if (hp > 0) { if (hp > 0) {
this->send_6xB4x06_for_stat_delta(defender_card, defender_cond->card_ref, 0x20, -1, 0, 1); this->send_6xB4x06_for_stat_delta(defender_card, defender_cond->card_ref, 0x20, -1, 0, 1);
@@ -418,12 +417,11 @@ bool CardSpecial::apply_defense_condition(
bool CardSpecial::apply_defense_conditions( bool CardSpecial::apply_defense_conditions(
const ActionState& as, const ActionState& as,
uint8_t when, EffectWhen when,
shared_ptr<Card> defender_card, shared_ptr<Card> defender_card,
uint32_t flags) { uint32_t flags) {
for (size_t z = 0; z < 9; z++) { for (size_t z = 0; z < 9; z++) {
this->apply_defense_condition( this->apply_defense_condition(when, &defender_card->action_chain.conditions[z], z, as, defender_card, flags, 0);
when, &defender_card->action_chain.conditions[z], z, as, defender_card, flags, 0);
} }
return true; return true;
} }
@@ -812,7 +810,7 @@ CardSpecial::AttackEnvStats CardSpecial::compute_attack_env_stats(
int32_t total_last_attack_damage = 0; int32_t total_last_attack_damage = 0;
size_t last_attack_damage_count = 0; size_t last_attack_damage_count = 0;
this->sum_last_attack_damage(nullptr, &total_last_attack_damage, &last_attack_damage_count); this->sum_last_attack_damage(nullptr, &total_last_attack_damage, &last_attack_damage_count);
ast.total_last_attack_damage = total_last_attack_damage; ast.final_last_attack_damage = total_last_attack_damage;
ast.last_attack_damage_count = last_attack_damage_count; ast.last_attack_damage_count = last_attack_damage_count;
if (!target_card) { if (!target_card) {
@@ -1238,13 +1236,14 @@ void CardSpecial::compute_team_dice_bonus(uint8_t team_id) {
s->team_dice_bonus[team_id] = min<uint8_t>(value, 8); s->team_dice_bonus[team_id] = min<uint8_t>(value, 8);
} }
bool CardSpecial::condition_has_when_20_or_21(const Condition& cond) const { bool CardSpecial::condition_applies_on_sc_or_item_attack(const Condition& cond) const {
auto ce = this->server()->definition_for_card_ref(cond.card_ref); auto ce = this->server()->definition_for_card_ref(cond.card_ref);
if (!ce) { if (!ce) {
return false; return false;
} }
uint8_t when = ce->def.effects[cond.card_definition_effect_index].when; EffectWhen when = ce->def.effects[cond.card_definition_effect_index].when;
return ((when == 0x20) || (when == 0x21)); return ((when == EffectWhen::AFTER_CREATURE_OR_HUNTER_SC_ATTACK) ||
(when == EffectWhen::BEFORE_CREATURE_OR_HUNTER_SC_ATTACK));
} }
size_t CardSpecial::count_action_cards_with_condition_for_all_current_attacks( size_t CardSpecial::count_action_cards_with_condition_for_all_current_attacks(
@@ -1410,7 +1409,7 @@ bool CardSpecial::evaluate_effect_arg2_condition(
uint16_t set_card_ref, uint16_t set_card_ref,
uint16_t sc_card_ref, uint16_t sc_card_ref,
uint8_t random_percent, uint8_t random_percent,
uint8_t when) const { EffectWhen when) const {
// Note: In the original code, as and dice_roll were optional pointers, but // Note: In the original code, as and dice_roll were optional pointers, but
// they are non-null at all callsites, so we've replaced them with references // they are non-null at all callsites, so we've replaced them with references
// (and eliminated the null checks within this function). // (and eliminated the null checks within this function).
@@ -1650,7 +1649,7 @@ bool CardSpecial::evaluate_effect_arg2_condition(
// or comment it appropriately. // or comment it appropriately.
if (is_nte) { if (is_nte) {
return (v < set_card->unknown_a9); return (v < set_card->unknown_a9);
} else if (when == 4) { } else if (when == EffectWhen::BEFORE_DICE_PHASE_THIS_TEAM_TURN) {
uint32_t y = set_card->unknown_a9 & 0xFFFFFFFE; uint32_t y = set_card->unknown_a9 & 0xFFFFFFFE;
if ((set_card->unknown_a9 > 0) && if ((set_card->unknown_a9 > 0) &&
(y == (y / (v & 0xFFFFFFFE)) * (v & 0xFFFFFFFE))) { (y == (y / (v & 0xFFFFFFFE)) * (v & 0xFFFFFFFE))) {
@@ -3519,7 +3518,7 @@ void CardSpecial::on_card_set(shared_ptr<PlayerState> ps, uint16_t card_ref) {
uint16_t sc_card_ref = sc_card ? sc_card->get_card_ref() : 0xFFFF; uint16_t sc_card_ref = sc_card ? sc_card->get_card_ref() : 0xFFFF;
ActionState as; ActionState as;
this->evaluate_and_apply_effects(0x01, card_ref, as, sc_card_ref); this->evaluate_and_apply_effects(EffectWhen::CARD_SET, card_ref, as, sc_card_ref);
} }
const CardDefinition::Effect* CardSpecial::original_definition_for_condition(const Condition& cond) const { const CardDefinition::Effect* CardSpecial::original_definition_for_condition(const Condition& cond) const {
@@ -3827,7 +3826,7 @@ int16_t CardSpecial::max_all_attack_bonuses(size_t* out_count) const {
return max_attack_bonus; return max_attack_bonus;
} }
void CardSpecial::unknown_80244AA8(shared_ptr<Card> card) { void CardSpecial::apply_effects_after_card_move(shared_ptr<Card> card) {
ActionState as = this->create_attack_state_from_card_action_chain(card); ActionState as = this->create_attack_state_from_card_action_chain(card);
bool is_nte = this->server()->options.is_nte(); bool is_nte = this->server()->options.is_nte();
@@ -3847,20 +3846,22 @@ void CardSpecial::unknown_80244AA8(shared_ptr<Card> card) {
} }
} }
} }
this->apply_defense_conditions(as, 0x27, card, 0x04); this->apply_defense_conditions(as, EffectWhen::BEFORE_MOVE_PHASE_AND_AFTER_CARD_MOVE_FINAL, card, 0x04);
this->evaluate_and_apply_effects(0x27, card->get_card_ref(), as, 0xFFFF); this->evaluate_and_apply_effects(EffectWhen::BEFORE_MOVE_PHASE_AND_AFTER_CARD_MOVE_FINAL, card->get_card_ref(), as, 0xFFFF);
} }
this->apply_defense_conditions(as, 0x13, card, is_nte ? 0x1F : 0x04); this->apply_defense_conditions(as, EffectWhen::AFTER_CARD_MOVE, card, is_nte ? 0x1F : 0x04);
this->evaluate_and_apply_effects(0x13, card->get_card_ref(), as, 0xFFFF); this->evaluate_and_apply_effects(EffectWhen::AFTER_CARD_MOVE, card->get_card_ref(), as, 0xFFFF);
} }
void CardSpecial::check_for_defense_interference( void CardSpecial::check_for_defense_interference(
shared_ptr<const Card> attacker_card, shared_ptr<const Card> attacker_card,
shared_ptr<Card> target_card, shared_ptr<Card> target_card,
int16_t* inout_unknown_p4) { int16_t* inout_unknown_p4) {
auto s = this->server();
// Note: This check is not part of the original implementation. // Note: This check is not part of the original implementation.
if (this->server()->options.behavior_flags & BehaviorFlag::DISABLE_INTERFERENCE) { if (s->options.behavior_flags & BehaviorFlag::DISABLE_INTERFERENCE) {
return; return;
} }
@@ -3871,13 +3872,13 @@ void CardSpecial::check_for_defense_interference(
return; return;
} }
uint16_t ally_sc_card_ref = this->server()->ruler_server->get_ally_sc_card_ref( uint16_t ally_sc_card_ref = s->ruler_server->get_ally_sc_card_ref(
target_card->get_card_ref()); target_card->get_card_ref());
if (ally_sc_card_ref == 0xFFFF) { if (ally_sc_card_ref == 0xFFFF) {
return; return;
} }
auto ally_sc = this->server()->card_for_set_card_ref(ally_sc_card_ref); auto ally_sc = s->card_for_set_card_ref(ally_sc_card_ref);
if (!ally_sc || (ally_sc->card_flags & 2)) { if (!ally_sc || (ally_sc->card_flags & 2)) {
return; return;
} }
@@ -3892,17 +3893,17 @@ void CardSpecial::check_for_defense_interference(
return; return;
} }
auto ally_hes = this->server()->ruler_server->get_hand_and_equip_state_for_client_id(target_ally_client_id); auto ally_hes = s->ruler_server->get_hand_and_equip_state_for_client_id(target_ally_client_id);
if (!ally_hes || (!(this->server()->options.behavior_flags & BehaviorFlag::ALLOW_NON_COM_INTERFERENCE) && !ally_hes->is_cpu_player)) { if (!ally_hes || (!(s->options.behavior_flags & BehaviorFlag::ALLOW_NON_COM_INTERFERENCE) && !ally_hes->is_cpu_player)) {
return; return;
} }
uint16_t target_card_id = this->server()->card_id_for_card_ref(target_card->get_card_ref()); uint16_t target_card_id = s->card_id_for_card_ref(target_card->get_card_ref());
if (target_card_id == 0xFFFF) { if (target_card_id == 0xFFFF) {
return; return;
} }
uint16_t ally_sc_card_id = this->server()->card_id_for_card_ref(ally_sc_card_ref); uint16_t ally_sc_card_id = s->card_id_for_card_ref(ally_sc_card_ref);
if (ally_sc_card_id == 0xFFFF) { if (ally_sc_card_id == 0xFFFF) {
return; return;
} }
@@ -3916,7 +3917,7 @@ void CardSpecial::check_for_defense_interference(
} }
auto entry = get_interference_probability_entry( auto entry = get_interference_probability_entry(
target_card_id, ally_sc_card_id, false); target_card_id, ally_sc_card_id, false);
if (!entry || (this->server()->get_random(99) >= entry->defense_probability)) { if (!entry || (s->get_random(99) >= entry->defense_probability)) {
return; return;
} }
@@ -3928,7 +3929,7 @@ void CardSpecial::check_for_defense_interference(
cmd.effect.target_card_ref = target_card->get_card_ref(); cmd.effect.target_card_ref = target_card->get_card_ref();
cmd.effect.value = 0; cmd.effect.value = 0;
cmd.effect.operation = 0x7D; cmd.effect.operation = 0x7D;
this->server()->send(cmd); s->send(cmd);
if (inout_unknown_p4) { if (inout_unknown_p4) {
*inout_unknown_p4 = 0; *inout_unknown_p4 = 0;
target_card->action_metadata.set_flags(0x10); target_card->action_metadata.set_flags(0x10);
@@ -3936,20 +3937,20 @@ void CardSpecial::check_for_defense_interference(
} }
void CardSpecial::evaluate_and_apply_effects( void CardSpecial::evaluate_and_apply_effects(
uint8_t when, EffectWhen when,
uint16_t set_card_ref, uint16_t set_card_ref,
const ActionState& as, const ActionState& as,
uint16_t sc_card_ref, uint16_t sc_card_ref,
bool apply_defense_condition_to_all_cards, bool apply_defense_condition_to_all_cards,
uint16_t apply_defense_condition_to_card_ref) { uint16_t apply_defense_condition_to_card_ref) {
auto s = this->server(); auto s = this->server();
auto log = s->log_stack(string_printf("evaluate_and_apply_effects(%02hhX, @%04hX, @%04hX): ", when, set_card_ref, sc_card_ref)); auto log = s->log_stack(string_printf("evaluate_and_apply_effects(%s, @%04hX, @%04hX): ", name_for_enum(when), set_card_ref, sc_card_ref));
bool is_nte = s->options.is_nte(); bool is_nte = s->options.is_nte();
{ {
string as_str = as.str(s); string as_str = as.str(s);
log.debug("when=%02hhX, set_card_ref=@%04hX, as=%s, sc_card_ref=@%04hX, apply_defense_condition_to_all_cards=%s, apply_defense_condition_to_card_ref=@%04hX", log.debug("when=%s, set_card_ref=@%04hX, as=%s, sc_card_ref=@%04hX, apply_defense_condition_to_all_cards=%s, apply_defense_condition_to_card_ref=@%04hX",
when, set_card_ref, as_str.c_str(), sc_card_ref, apply_defense_condition_to_all_cards ? "true" : "false", apply_defense_condition_to_card_ref); name_for_enum(when), set_card_ref, as_str.c_str(), sc_card_ref, apply_defense_condition_to_all_cards ? "true" : "false", apply_defense_condition_to_card_ref);
} }
if (!is_nte) { if (!is_nte) {
@@ -4014,7 +4015,7 @@ void CardSpecial::evaluate_and_apply_effects(
string card_effect_str = card_effect.str(); string card_effect_str = card_effect.str();
effect_log.debug("effect: %s", card_effect_str.c_str()); effect_log.debug("effect: %s", card_effect_str.c_str());
if (card_effect.when != when) { if (card_effect.when != when) {
effect_log.debug("does not apply (effect.when=%02hhX, when=%02" PRIX32 ")", card_effect.when, when); effect_log.debug("does not apply (effect.when=%s, when=%s)", name_for_enum(card_effect.when), name_for_enum(when));
continue; continue;
} }
@@ -4426,10 +4427,10 @@ void CardSpecial::on_card_destroyed(
attacker_card, destroyed_card); attacker_card, destroyed_card);
uint16_t destroyed_card_ref = destroyed_card->get_card_ref(); uint16_t destroyed_card_ref = destroyed_card->get_card_ref();
this->evaluate_and_apply_effects(0x05, destroyed_card_ref, defense_as, 0xFFFF); this->evaluate_and_apply_effects(EffectWhen::CARD_DESTROYED, destroyed_card_ref, defense_as, 0xFFFF);
for (size_t z = 0; (z < 8) && (defense_as.action_card_refs[z] != 0xFFFF); z++) { for (size_t z = 0; (z < 8) && (defense_as.action_card_refs[z] != 0xFFFF); z++) {
this->evaluate_and_apply_effects( this->evaluate_and_apply_effects(
0x05, defense_as.action_card_refs[z], defense_as, destroyed_card->get_card_ref()); EffectWhen::CARD_DESTROYED, defense_as.action_card_refs[z], defense_as, destroyed_card->get_card_ref());
} }
if (attacker_card) { if (attacker_card) {
@@ -4671,9 +4672,9 @@ vector<shared_ptr<const Card>> CardSpecial::filter_cards_by_range(
return ret; return ret;
} }
void CardSpecial::unknown_8024AAB8(const ActionState& as) { void CardSpecial::apply_effects_after_attack_target_resolution(const ActionState& as) {
auto s = this->server(); auto s = this->server();
auto log = s->log_stack("unknown_8024AAB8: "); auto log = s->log_stack("apply_effects_after_attack_target_resolution: ");
string as_str = as.str(s); string as_str = as.str(s);
log.debug("as=%s", as_str.c_str()); log.debug("as=%s", as_str.c_str());
@@ -4685,31 +4686,37 @@ void CardSpecial::unknown_8024AAB8(const ActionState& as) {
if (this->send_6xB4x06_if_card_ref_invalid(as.original_attacker_card_ref, 0x1F) == 0xFFFF) { if (this->send_6xB4x06_if_card_ref_invalid(as.original_attacker_card_ref, 0x1F) == 0xFFFF) {
this->evaluate_and_apply_effects( this->evaluate_and_apply_effects(
0x01, as.action_card_refs[z], as, this->send_6xB4x06_if_card_ref_invalid(as.attacker_card_ref, 0x21)); EffectWhen::CARD_SET,
as.action_card_refs[z],
as,
this->send_6xB4x06_if_card_ref_invalid(as.attacker_card_ref, 0x21));
this->evaluate_and_apply_effects( this->evaluate_and_apply_effects(
0x0B, as.action_card_refs[z], as, this->send_6xB4x06_if_card_ref_invalid(as.attacker_card_ref, 0x22)); EffectWhen::AFTER_ATTACK_TARGET_RESOLUTION,
as.action_card_refs[z],
as,
this->send_6xB4x06_if_card_ref_invalid(as.attacker_card_ref, 0x22));
} else { } else {
uint16_t card_ref = this->send_6xB4x06_if_card_ref_invalid(as.target_card_refs[0], 0x20); uint16_t card_ref = this->send_6xB4x06_if_card_ref_invalid(as.target_card_refs[0], 0x20);
if (card_ref != 0xFFFF) { if (card_ref != 0xFFFF) {
this->evaluate_and_apply_effects(0x01, as.action_card_refs[z], as, card_ref); this->evaluate_and_apply_effects(EffectWhen::CARD_SET, as.action_card_refs[z], as, card_ref);
this->evaluate_and_apply_effects(0x15, as.action_card_refs[z], as, card_ref); this->evaluate_and_apply_effects(EffectWhen::UNKNOWN_15, as.action_card_refs[z], as, card_ref);
} }
} }
} }
if (as.original_attacker_card_ref == 0xffff) { if (as.original_attacker_card_ref == 0xFFFF) {
uint16_t card_ref1 = this->send_6xB4x06_if_card_ref_invalid(as.attacker_card_ref, 0x23); uint16_t card_ref1 = this->send_6xB4x06_if_card_ref_invalid(as.attacker_card_ref, 0x23);
uint16_t card_ref2 = this->send_6xB4x06_if_card_ref_invalid(as.attacker_card_ref, 0x25); uint16_t card_ref2 = this->send_6xB4x06_if_card_ref_invalid(as.attacker_card_ref, 0x25);
this->evaluate_and_apply_effects(0x33, card_ref2, as, card_ref1); this->evaluate_and_apply_effects(EffectWhen::UNKNOWN_33, card_ref2, as, card_ref1);
card_ref1 = this->send_6xB4x06_if_card_ref_invalid(as.attacker_card_ref, 0x24); card_ref1 = this->send_6xB4x06_if_card_ref_invalid(as.attacker_card_ref, 0x24);
card_ref2 = this->send_6xB4x06_if_card_ref_invalid(as.attacker_card_ref, 0x26); card_ref2 = this->send_6xB4x06_if_card_ref_invalid(as.attacker_card_ref, 0x26);
this->evaluate_and_apply_effects(0x34, card_ref2, as, card_ref1); this->evaluate_and_apply_effects(EffectWhen::UNKNOWN_34, card_ref2, as, card_ref1);
for (size_t z = 0; (z < 4 * 9) && (as.target_card_refs[z] != 0xFFFF); z++) { for (size_t z = 0; (z < 4 * 9) && (as.target_card_refs[z] != 0xFFFF); z++) {
uint16_t card_ref = this->send_6xB4x06_if_card_ref_invalid(as.action_card_refs[z], 0x27); uint16_t card_ref = this->send_6xB4x06_if_card_ref_invalid(as.action_card_refs[z], 0x27);
if (card_ref == 0xFFFF) { if (card_ref == 0xFFFF) {
break; break;
} }
this->evaluate_and_apply_effects(0x35, as.target_card_refs[z], as, as.attacker_card_ref); this->evaluate_and_apply_effects(EffectWhen::UNKNOWN_35, as.target_card_refs[z], as, as.attacker_card_ref);
} }
} }
} }
@@ -4717,11 +4724,11 @@ void CardSpecial::unknown_8024AAB8(const ActionState& as) {
void CardSpecial::move_phase_before_for_card(shared_ptr<Card> card) { void CardSpecial::move_phase_before_for_card(shared_ptr<Card> card) {
bool is_nte = this->server()->options.is_nte(); bool is_nte = this->server()->options.is_nte();
ActionState as = this->create_attack_state_from_card_action_chain(card); ActionState as = this->create_attack_state_from_card_action_chain(card);
this->apply_defense_conditions(as, 0x09, card, is_nte ? 0x1F : 0x04); this->apply_defense_conditions(as, EffectWhen::BEFORE_MOVE_PHASE, card, is_nte ? 0x1F : 0x04);
this->evaluate_and_apply_effects(0x09, card->get_card_ref(), as, 0xFFFF); this->evaluate_and_apply_effects(EffectWhen::BEFORE_MOVE_PHASE, card->get_card_ref(), as, 0xFFFF);
if (!is_nte) { if (!is_nte) {
this->apply_defense_conditions(as, 0x27, card, 0x04); this->apply_defense_conditions(as, EffectWhen::BEFORE_MOVE_PHASE_AND_AFTER_CARD_MOVE_FINAL, card, 0x04);
this->evaluate_and_apply_effects(0x27, card->get_card_ref(), as, 0xFFFF); this->evaluate_and_apply_effects(EffectWhen::BEFORE_MOVE_PHASE_AND_AFTER_CARD_MOVE_FINAL, card->get_card_ref(), as, 0xFFFF);
} }
} }
@@ -4748,19 +4755,19 @@ void CardSpecial::dice_phase_before_for_card(shared_ptr<Card> card) {
} }
if (!is_nte) { if (!is_nte) {
this->apply_defense_conditions(as, 0x46, card, 0x04); this->apply_defense_conditions(as, EffectWhen::BEFORE_DICE_PHASE_ALL_TURNS_FINAL, card, 0x04);
this->evaluate_and_apply_effects(0x46, card->get_card_ref(), as, sc_card_ref); this->evaluate_and_apply_effects(EffectWhen::BEFORE_DICE_PHASE_ALL_TURNS_FINAL, card->get_card_ref(), as, sc_card_ref);
} }
if (ps->is_team_turn()) { if (ps->is_team_turn()) {
this->apply_defense_conditions(as, 0x04, card, 0x04); this->apply_defense_conditions(as, EffectWhen::BEFORE_DICE_PHASE_THIS_TEAM_TURN, card, 0x04);
this->evaluate_and_apply_effects(0x04, card->get_card_ref(), as, sc_card_ref); this->evaluate_and_apply_effects(EffectWhen::BEFORE_DICE_PHASE_THIS_TEAM_TURN, card->get_card_ref(), as, sc_card_ref);
} }
} }
template <uint8_t When1, uint8_t When2> template <EffectWhen When1, EffectWhen When2>
void CardSpecial::apply_effects_on_phase_change_t(shared_ptr<Card> unknown_p2, const ActionState* existing_as) { void CardSpecial::apply_effects_on_phase_change_t(shared_ptr<Card> unknown_p2, const ActionState* existing_as) {
auto s = this->server(); auto s = this->server();
auto log = s->log_stack(string_printf("apply_effects_on_phase_change_t<%02hhX, %02hhX>(@%04hX #%04hX): ", When1, When2, unknown_p2->get_card_ref(), unknown_p2->get_card_id())); auto log = s->log_stack(string_printf("apply_effects_on_phase_change_t<%s, %s>(@%04hX #%04hX): ", name_for_enum(When1), name_for_enum(When2), unknown_p2->get_card_ref(), unknown_p2->get_card_id()));
bool is_nte = s->options.is_nte(); bool is_nte = s->options.is_nte();
ActionState as; ActionState as;
@@ -4797,17 +4804,17 @@ void CardSpecial::apply_effects_on_phase_change_t(shared_ptr<Card> unknown_p2, c
} }
void CardSpecial::draw_phase_before_for_card(shared_ptr<Card> unknown_p2) { void CardSpecial::draw_phase_before_for_card(shared_ptr<Card> unknown_p2) {
this->apply_effects_on_phase_change_t<0x0F, 0x0A>(unknown_p2); this->apply_effects_on_phase_change_t<EffectWhen::BEFORE_DRAW_PHASE, EffectWhen::UNKNOWN_0A>(unknown_p2);
} }
void CardSpecial::action_phase_before_for_card(shared_ptr<Card> unknown_p2) { void CardSpecial::action_phase_before_for_card(shared_ptr<Card> unknown_p2) {
if (unknown_p2->player_state()->is_team_turn()) { if (unknown_p2->player_state()->is_team_turn()) {
this->apply_effects_on_phase_change_t<0x0E, 0x0A>(unknown_p2); this->apply_effects_on_phase_change_t<EffectWhen::BEFORE_ACT_PHASE, EffectWhen::UNKNOWN_0A>(unknown_p2);
} }
} }
void CardSpecial::unknown_8024945C(shared_ptr<Card> unknown_p2, const ActionState* existing_as) { void CardSpecial::unknown_8024945C(shared_ptr<Card> unknown_p2, const ActionState* existing_as) {
this->apply_effects_on_phase_change_t<0x0A, 0x0A>(unknown_p2, this->server()->options.is_nte() ? nullptr : existing_as); this->apply_effects_on_phase_change_t<EffectWhen::UNKNOWN_0A, EffectWhen::UNKNOWN_0A>(unknown_p2, this->server()->options.is_nte() ? nullptr : existing_as);
} }
void CardSpecial::unknown_8024966C(shared_ptr<Card> unknown_p2, const ActionState* existing_as) { void CardSpecial::unknown_8024966C(shared_ptr<Card> unknown_p2, const ActionState* existing_as) {
@@ -4826,38 +4833,38 @@ void CardSpecial::unknown_8024966C(shared_ptr<Card> unknown_p2, const ActionStat
auto ce = unknown_p2->get_definition(); auto ce = unknown_p2->get_definition();
auto defender_card = (ce && (ce->def.type == CardType::ITEM) && card) ? card : unknown_p2; auto defender_card = (ce && (ce->def.type == CardType::ITEM) && card) ? card : unknown_p2;
this->apply_defense_conditions(as, 0x3D, unknown_p2, 4); this->apply_defense_conditions(as, EffectWhen::ATTACK_STAT_OVERRIDES, unknown_p2, 4);
this->apply_defense_conditions(as, 0x3E, unknown_p2, 4); this->apply_defense_conditions(as, EffectWhen::ATTACK_DAMAGE_ADJUSTMENT, unknown_p2, 4);
if (defender_card) { if (defender_card) {
this->apply_defense_conditions(as, 0x22, defender_card, 4); this->apply_defense_conditions(as, EffectWhen::UNKNOWN_22, defender_card, 4);
} }
for (size_t z = 0; (z < 4 * 9) && (as.target_card_refs[z] != 0xFFFF); z++) { for (size_t z = 0; (z < 4 * 9) && (as.target_card_refs[z] != 0xFFFF); z++) {
auto card = this->server()->card_for_set_card_ref(as.target_card_refs[z]); auto card = this->server()->card_for_set_card_ref(as.target_card_refs[z]);
if (card) { if (card) {
ActionState defense_as = this->create_defense_state_for_card_pair_action_chains(unknown_p2, card); ActionState defense_as = this->create_defense_state_for_card_pair_action_chains(unknown_p2, card);
this->apply_defense_conditions(defense_as, 0x3D, card, 4); this->apply_defense_conditions(defense_as, EffectWhen::ATTACK_STAT_OVERRIDES, card, 4);
this->apply_defense_conditions(defense_as, 0x3F, card, 4); this->apply_defense_conditions(defense_as, EffectWhen::DEFENSE_DAMAGE_ADJUSTMENT, card, 4);
} }
} }
this->evaluate_and_apply_effects(0x3D, unknown_p2->get_card_ref(), as, card_ref); this->evaluate_and_apply_effects(EffectWhen::ATTACK_STAT_OVERRIDES, unknown_p2->get_card_ref(), as, card_ref);
this->evaluate_and_apply_effects(0x3E, unknown_p2->get_card_ref(), as, card_ref); this->evaluate_and_apply_effects(EffectWhen::ATTACK_DAMAGE_ADJUSTMENT, unknown_p2->get_card_ref(), as, card_ref);
if (defender_card) { if (defender_card) {
this->evaluate_and_apply_effects(0x22, defender_card->get_card_ref(), as, card_ref); this->evaluate_and_apply_effects(EffectWhen::UNKNOWN_22, defender_card->get_card_ref(), as, card_ref);
} }
for (size_t z = 0; (z < 8) && (as.action_card_refs[z] != 0xFFFF); z++) { for (size_t z = 0; (z < 8) && (as.action_card_refs[z] != 0xFFFF); z++) {
this->evaluate_and_apply_effects(0x3D, as.action_card_refs[z], as, unknown_p2->get_card_ref()); this->evaluate_and_apply_effects(EffectWhen::ATTACK_STAT_OVERRIDES, as.action_card_refs[z], as, unknown_p2->get_card_ref());
this->evaluate_and_apply_effects(0x3E, as.action_card_refs[z], as, unknown_p2->get_card_ref()); this->evaluate_and_apply_effects(EffectWhen::ATTACK_DAMAGE_ADJUSTMENT, as.action_card_refs[z], as, unknown_p2->get_card_ref());
} }
for (size_t z = 0; (z < 4 * 9) && (as.target_card_refs[z] != 0xFFFF); z++) { for (size_t z = 0; (z < 4 * 9) && (as.target_card_refs[z] != 0xFFFF); z++) {
card = this->server()->card_for_set_card_ref(as.target_card_refs[z]); card = this->server()->card_for_set_card_ref(as.target_card_refs[z]);
if (card) { if (card) {
ActionState defense_as = this->create_defense_state_for_card_pair_action_chains(unknown_p2, card); ActionState defense_as = this->create_defense_state_for_card_pair_action_chains(unknown_p2, card);
this->evaluate_and_apply_effects(0x3D, card->get_card_ref(), defense_as, unknown_p2->get_card_ref()); this->evaluate_and_apply_effects(EffectWhen::ATTACK_STAT_OVERRIDES, card->get_card_ref(), defense_as, unknown_p2->get_card_ref());
this->evaluate_and_apply_effects(0x3F, card->get_card_ref(), defense_as, unknown_p2->get_card_ref()); this->evaluate_and_apply_effects(EffectWhen::DEFENSE_DAMAGE_ADJUSTMENT, card->get_card_ref(), defense_as, unknown_p2->get_card_ref());
} }
} }
} }
@@ -4871,11 +4878,11 @@ void CardSpecial::unknown_8024A9D8(const ActionState& pa, uint16_t action_card_r
for (size_t z = 0; (z < 8) && (pa.action_card_refs[z] != 0xFFFF); z++) { for (size_t z = 0; (z < 8) && (pa.action_card_refs[z] != 0xFFFF); z++) {
if (this->server()->options.is_nte() || (action_card_ref == 0xFFFF) || (action_card_ref == pa.action_card_refs[z])) { if (this->server()->options.is_nte() || (action_card_ref == 0xFFFF) || (action_card_ref == pa.action_card_refs[z])) {
if (pa.original_attacker_card_ref == 0xFFFF) { if (pa.original_attacker_card_ref == 0xFFFF) {
this->evaluate_and_apply_effects(0x29, pa.action_card_refs[z], pa, pa.attacker_card_ref); this->evaluate_and_apply_effects(EffectWhen::UNKNOWN_29, pa.action_card_refs[z], pa, pa.attacker_card_ref);
this->evaluate_and_apply_effects(0x2A, pa.action_card_refs[z], pa, pa.attacker_card_ref); this->evaluate_and_apply_effects(EffectWhen::UNKNOWN_2A, pa.action_card_refs[z], pa, pa.attacker_card_ref);
} else { } else {
this->evaluate_and_apply_effects(0x29, pa.action_card_refs[z], pa, pa.target_card_refs[0]); this->evaluate_and_apply_effects(EffectWhen::UNKNOWN_29, pa.action_card_refs[z], pa, pa.target_card_refs[0]);
this->evaluate_and_apply_effects(0x2B, pa.action_card_refs[z], pa, pa.target_card_refs[0]); this->evaluate_and_apply_effects(EffectWhen::UNKNOWN_2B, pa.action_card_refs[z], pa, pa.target_card_refs[0]);
} }
} }
} }
@@ -4955,10 +4962,15 @@ void CardSpecial::check_for_attack_interference(shared_ptr<Card> unknown_p2) {
this->server()->send(cmd); this->server()->send(cmd);
} }
template <uint8_t When1, uint8_t When2, uint8_t When3, uint8_t When4> template <
void CardSpecial::unknown_t2(shared_ptr<Card> unknown_p2) { EffectWhen WhenAllCards,
EffectWhen WhenAttackerAndActionCards,
EffectWhen WhenAttackerOrHunterSCCard,
EffectWhen WhenTargetsAndActionCards>
void CardSpecial::apply_effects_before_or_after_attack(shared_ptr<Card> unknown_p2) {
auto s = this->server(); auto s = this->server();
auto log = s->log_stack(string_printf("unknown_t2<%02hhX, %02hhX, %02hhX, %02hhX>(@%04hX #%04hX): ", When1, When2, When3, When4, unknown_p2->get_card_ref(), unknown_p2->get_card_id())); auto log = s->log_stack(string_printf("apply_effects_before_or_after_attack<%s, %s, %s, %s>(@%04hX #%04hX): ",
name_for_enum(WhenAllCards), name_for_enum(WhenAttackerAndActionCards), name_for_enum(WhenAttackerOrHunterSCCard), name_for_enum(WhenTargetsAndActionCards), unknown_p2->get_card_ref(), unknown_p2->get_card_id()));
ActionState as = this->create_attack_state_from_card_action_chain(unknown_p2); ActionState as = this->create_attack_state_from_card_action_chain(unknown_p2);
@@ -4968,56 +4980,64 @@ void CardSpecial::unknown_t2(shared_ptr<Card> unknown_p2) {
sc_card_ref = sc_card->get_card_ref(); sc_card_ref = sc_card->get_card_ref();
} }
auto defender_card = unknown_p2; auto attacker_card = unknown_p2;
if (unknown_p2->get_definition() && (unknown_p2->get_definition()->def.type == CardType::ITEM) && sc_card) { if (unknown_p2->get_definition() && (unknown_p2->get_definition()->def.type == CardType::ITEM) && sc_card) {
defender_card = sc_card; attacker_card = sc_card;
} }
uint8_t apply_defense_conditions_flags = s->options.is_nte() ? 0x1F : 0x04; uint8_t apply_defense_conditions_flags = s->options.is_nte() ? 0x1F : 0x04;
this->apply_defense_conditions(as, When1, unknown_p2, apply_defense_conditions_flags); this->apply_defense_conditions(as, WhenAllCards, unknown_p2, apply_defense_conditions_flags);
this->apply_defense_conditions(as, When2, unknown_p2, apply_defense_conditions_flags); this->apply_defense_conditions(as, WhenAttackerAndActionCards, unknown_p2, apply_defense_conditions_flags);
if (defender_card) { if (attacker_card) {
this->apply_defense_conditions(as, When3, defender_card, apply_defense_conditions_flags); this->apply_defense_conditions(as, WhenAttackerOrHunterSCCard, attacker_card, apply_defense_conditions_flags);
} }
for (size_t z = 0; (z < 4 * 9) && (as.target_card_refs[z] != 0xFFFF); z++) { for (size_t z = 0; (z < 4 * 9) && (as.target_card_refs[z] != 0xFFFF); z++) {
auto set_card = s->card_for_set_card_ref(as.target_card_refs[z]); auto set_card = s->card_for_set_card_ref(as.target_card_refs[z]);
if (set_card) { if (set_card) {
ActionState target_as = this->create_defense_state_for_card_pair_action_chains(unknown_p2, set_card); ActionState target_as = this->create_defense_state_for_card_pair_action_chains(unknown_p2, set_card);
this->apply_defense_conditions(target_as, When1, set_card, apply_defense_conditions_flags); this->apply_defense_conditions(target_as, WhenAllCards, set_card, apply_defense_conditions_flags);
this->apply_defense_conditions(target_as, When4, set_card, apply_defense_conditions_flags); this->apply_defense_conditions(target_as, WhenTargetsAndActionCards, set_card, apply_defense_conditions_flags);
} }
} }
this->evaluate_and_apply_effects(When1, unknown_p2->get_card_ref(), as, sc_card_ref); this->evaluate_and_apply_effects(WhenAllCards, unknown_p2->get_card_ref(), as, sc_card_ref);
this->evaluate_and_apply_effects(When2, unknown_p2->get_card_ref(), as, sc_card_ref); this->evaluate_and_apply_effects(WhenAttackerAndActionCards, unknown_p2->get_card_ref(), as, sc_card_ref);
if (defender_card) { if (attacker_card) {
this->evaluate_and_apply_effects(When3, defender_card->get_card_ref(), as, sc_card_ref); this->evaluate_and_apply_effects(WhenAttackerOrHunterSCCard, attacker_card->get_card_ref(), as, sc_card_ref);
} }
for (size_t z = 0; (z < 8) && (as.action_card_refs[z] != 0xFFFF); z++) { for (size_t z = 0; (z < 8) && (as.action_card_refs[z] != 0xFFFF); z++) {
this->evaluate_and_apply_effects(When1, as.action_card_refs[z], as, unknown_p2->get_card_ref()); this->evaluate_and_apply_effects(WhenAllCards, as.action_card_refs[z], as, unknown_p2->get_card_ref());
this->evaluate_and_apply_effects(When2, as.action_card_refs[z], as, unknown_p2->get_card_ref()); this->evaluate_and_apply_effects(WhenAttackerAndActionCards, as.action_card_refs[z], as, unknown_p2->get_card_ref());
} }
for (size_t z = 0; (z < 4 * 9) && (as.target_card_refs[z] != 0xFFFF); z++) { for (size_t z = 0; (z < 4 * 9) && (as.target_card_refs[z] != 0xFFFF); z++) {
auto set_card = s->card_for_set_card_ref(as.target_card_refs[z]); auto set_card = s->card_for_set_card_ref(as.target_card_refs[z]);
if (set_card) { if (set_card) {
ActionState target_as = this->create_defense_state_for_card_pair_action_chains(unknown_p2, set_card); ActionState target_as = this->create_defense_state_for_card_pair_action_chains(unknown_p2, set_card);
this->evaluate_and_apply_effects(When1, set_card->get_card_ref(), target_as, unknown_p2->get_card_ref()); this->evaluate_and_apply_effects(WhenAllCards, set_card->get_card_ref(), target_as, unknown_p2->get_card_ref());
this->evaluate_and_apply_effects(When4, set_card->get_card_ref(), target_as, unknown_p2->get_card_ref()); this->evaluate_and_apply_effects(WhenTargetsAndActionCards, set_card->get_card_ref(), target_as, unknown_p2->get_card_ref());
for (size_t z = 0; (z < 8) && (target_as.action_card_refs[z] != 0xFFFF); z++) { for (size_t z = 0; (z < 8) && (target_as.action_card_refs[z] != 0xFFFF); z++) {
this->evaluate_and_apply_effects(When1, target_as.action_card_refs[z], target_as, set_card->get_card_ref()); this->evaluate_and_apply_effects(WhenAllCards, target_as.action_card_refs[z], target_as, set_card->get_card_ref());
this->evaluate_and_apply_effects(When4, target_as.action_card_refs[z], target_as, set_card->get_card_ref()); this->evaluate_and_apply_effects(WhenTargetsAndActionCards, target_as.action_card_refs[z], target_as, set_card->get_card_ref());
} }
} }
} }
} }
void CardSpecial::unknown_8024997C(shared_ptr<Card> card) { void CardSpecial::apply_effects_after_attack(shared_ptr<Card> card) {
return this->unknown_t2<0x03, 0x0D, 0x21, 0x17>(card); return this->apply_effects_before_or_after_attack<
EffectWhen::AFTER_ANY_CARD_ATTACK,
EffectWhen::AFTER_THIS_CARD_ATTACK,
EffectWhen::AFTER_CREATURE_OR_HUNTER_SC_ATTACK,
EffectWhen::AFTER_THIS_CARD_ATTACKED>(card);
} }
void CardSpecial::unknown_8024A394(shared_ptr<Card> card) { void CardSpecial::apply_effects_before_attack(shared_ptr<Card> card) {
return this->unknown_t2<0x02, 0x0C, 0x20, 0x16>(card); return this->apply_effects_before_or_after_attack<
EffectWhen::BEFORE_ANY_CARD_ATTACK,
EffectWhen::BEFORE_THIS_CARD_ATTACK,
EffectWhen::BEFORE_CREATURE_OR_HUNTER_SC_ATTACK,
EffectWhen::BEFORE_THIS_CARD_ATTACKED>(card);
} }
bool CardSpecial::client_has_atk_dice_boost_condition(uint8_t client_id) { bool CardSpecial::client_has_atk_dice_boost_condition(uint8_t client_id) {
@@ -5054,8 +5074,8 @@ void CardSpecial::unknown_8024A6DC(shared_ptr<Card> unknown_p2, shared_ptr<Card>
ActionState as = this->create_defense_state_for_card_pair_action_chains( ActionState as = this->create_defense_state_for_card_pair_action_chains(
unknown_p2, unknown_p3); unknown_p2, unknown_p3);
for (size_t z = 0; (z < 8) && (as.action_card_refs[z] != 0xFFFF); z++) { for (size_t z = 0; (z < 8) && (as.action_card_refs[z] != 0xFFFF); z++) {
this->evaluate_and_apply_effects(0x01, as.action_card_refs[z], as, unknown_p3->get_card_ref()); this->evaluate_and_apply_effects(EffectWhen::CARD_SET, as.action_card_refs[z], as, unknown_p3->get_card_ref());
this->evaluate_and_apply_effects(0x15, as.action_card_refs[z], as, unknown_p3->get_card_ref()); this->evaluate_and_apply_effects(EffectWhen::UNKNOWN_15, as.action_card_refs[z], as, unknown_p3->get_card_ref());
} }
} }
+20 -15
View File
@@ -6,6 +6,7 @@
#include "../Text.hh" #include "../Text.hh"
#include "DataIndexes.hh" #include "DataIndexes.hh"
#include "Server.hh"
namespace Episode3 { namespace Episode3 {
@@ -84,7 +85,7 @@ public:
/* 84 */ uint32_t target_attack_bonus; // "edm" in expr /* 84 */ uint32_t target_attack_bonus; // "edm" in expr
/* 88 */ uint32_t last_attack_preliminary_damage; // "ldm" in expr /* 88 */ uint32_t last_attack_preliminary_damage; // "ldm" in expr
/* 8C */ uint32_t last_attack_damage; // "rdm" in expr /* 8C */ uint32_t last_attack_damage; // "rdm" in expr
/* 90 */ uint32_t total_last_attack_damage; // "fdm" in expr /* 90 */ uint32_t final_last_attack_damage; // "fdm" in expr
/* 94 */ uint32_t last_attack_damage_count; // "ndm" in expr /* 94 */ uint32_t last_attack_damage_count; // "ndm" in expr
/* 98 */ uint32_t target_current_hp; // "ehp" in expr /* 98 */ uint32_t target_current_hp; // "ehp" in expr
/* 9C */ /* 9C */
@@ -94,7 +95,7 @@ public:
void print(FILE* stream) const; void print(FILE* stream) const;
uint32_t at(size_t index) const; uint32_t at(size_t index) const;
} __attribute__((packed)); } __packed_ws__(AttackEnvStats, 0x9C);
CardSpecial(std::shared_ptr<Server> server); CardSpecial(std::shared_ptr<Server> server);
std::shared_ptr<Server> server(); std::shared_ptr<Server> server();
@@ -105,7 +106,7 @@ public:
void adjust_dice_boost_if_team_has_condition_52( void adjust_dice_boost_if_team_has_condition_52(
uint8_t team_id, uint8_t* inout_dice_boost, std::shared_ptr<const Card> card); uint8_t team_id, uint8_t* inout_dice_boost, std::shared_ptr<const Card> card);
void apply_action_conditions( void apply_action_conditions(
uint8_t when, EffectWhen when,
std::shared_ptr<const Card> attacker_card, std::shared_ptr<const Card> attacker_card,
std::shared_ptr<Card> defender_card, std::shared_ptr<Card> defender_card,
uint32_t flags, uint32_t flags,
@@ -117,7 +118,7 @@ public:
uint16_t condition_giver_card_ref, uint16_t condition_giver_card_ref,
uint16_t attacker_card_ref); uint16_t attacker_card_ref);
bool apply_defense_condition( bool apply_defense_condition(
uint8_t when, EffectWhen when,
Condition* defender_cond, Condition* defender_cond,
uint8_t cond_index, uint8_t cond_index,
const ActionState& defense_state, const ActionState& defense_state,
@@ -126,7 +127,7 @@ public:
bool unknown_p8); bool unknown_p8);
bool apply_defense_conditions( bool apply_defense_conditions(
const ActionState& as, const ActionState& as,
uint8_t when, EffectWhen when,
std::shared_ptr<Card> defender_card, std::shared_ptr<Card> defender_card,
uint32_t flags); uint32_t flags);
bool apply_stat_deltas_to_all_cards_from_all_conditions_with_card_ref( bool apply_stat_deltas_to_all_cards_from_all_conditions_with_card_ref(
@@ -164,7 +165,7 @@ public:
uint16_t sc_card_ref); uint16_t sc_card_ref);
StatSwapType compute_stat_swap_type(std::shared_ptr<const Card> card) const; StatSwapType compute_stat_swap_type(std::shared_ptr<const Card> card) const;
void compute_team_dice_bonus(uint8_t team_id); void compute_team_dice_bonus(uint8_t team_id);
bool condition_has_when_20_or_21(const Condition& cond) const; bool condition_applies_on_sc_or_item_attack(const Condition& cond) const;
size_t count_action_cards_with_condition_for_all_current_attacks( size_t count_action_cards_with_condition_for_all_current_attacks(
ConditionType cond_type, uint16_t card_ref) const; ConditionType cond_type, uint16_t card_ref) const;
size_t count_action_cards_with_condition_for_current_attack( size_t count_action_cards_with_condition_for_current_attack(
@@ -188,7 +189,7 @@ public:
uint16_t set_card_ref, uint16_t set_card_ref,
uint16_t sc_card_ref, uint16_t sc_card_ref,
uint8_t random_percent, uint8_t random_percent,
uint8_t when) const; EffectWhen when) const;
int32_t evaluate_effect_expr( int32_t evaluate_effect_expr(
const AttackEnvStats& ast, const AttackEnvStats& ast,
const char* expr, const char* expr,
@@ -275,13 +276,13 @@ public:
size_t* out_damage_count) const; size_t* out_damage_count) const;
void update_condition_orders(std::shared_ptr<Card> card); void update_condition_orders(std::shared_ptr<Card> card);
int16_t max_all_attack_bonuses(size_t* out_count) const; int16_t max_all_attack_bonuses(size_t* out_count) const;
void unknown_80244AA8(std::shared_ptr<Card> card); void apply_effects_after_card_move(std::shared_ptr<Card> card);
void check_for_defense_interference( void check_for_defense_interference(
std::shared_ptr<const Card> attacker_card, std::shared_ptr<const Card> attacker_card,
std::shared_ptr<Card> target_card, std::shared_ptr<Card> target_card,
int16_t* inout_unknown_p4); int16_t* inout_unknown_p4);
void evaluate_and_apply_effects( void evaluate_and_apply_effects(
uint8_t when, EffectWhen when,
uint16_t set_card_ref, uint16_t set_card_ref,
const ActionState& as, const ActionState& as,
uint16_t sc_card_ref, uint16_t sc_card_ref,
@@ -312,10 +313,10 @@ public:
std::shared_ptr<const Card> card1, std::shared_ptr<const Card> card1,
const Location& card1_loc, const Location& card1_loc,
std::shared_ptr<const Card> card2) const; std::shared_ptr<const Card> card2) const;
void unknown_8024AAB8(const ActionState& as); void apply_effects_after_attack_target_resolution(const ActionState& as);
void move_phase_before_for_card(std::shared_ptr<Card> unknown_p2); void move_phase_before_for_card(std::shared_ptr<Card> unknown_p2);
void dice_phase_before_for_card(std::shared_ptr<Card> card); void dice_phase_before_for_card(std::shared_ptr<Card> card);
template <uint8_t When1, uint8_t When2> template <EffectWhen When1, EffectWhen When2>
void apply_effects_on_phase_change_t(std::shared_ptr<Card> unknown_p2, const ActionState* existing_as = nullptr); void apply_effects_on_phase_change_t(std::shared_ptr<Card> unknown_p2, const ActionState* existing_as = nullptr);
void draw_phase_before_for_card(std::shared_ptr<Card> unknown_p2); void draw_phase_before_for_card(std::shared_ptr<Card> unknown_p2);
void action_phase_before_for_card(std::shared_ptr<Card> unknown_p2); void action_phase_before_for_card(std::shared_ptr<Card> unknown_p2);
@@ -324,10 +325,14 @@ public:
static std::shared_ptr<Card> sc_card_for_card(std::shared_ptr<Card> unknown_p2); static std::shared_ptr<Card> sc_card_for_card(std::shared_ptr<Card> unknown_p2);
void unknown_8024A9D8(const ActionState& pa, uint16_t action_card_ref); void unknown_8024A9D8(const ActionState& pa, uint16_t action_card_ref);
void check_for_attack_interference(std::shared_ptr<Card> unknown_p2); void check_for_attack_interference(std::shared_ptr<Card> unknown_p2);
template <uint8_t When1, uint8_t When2, uint8_t When3, uint8_t When4> template <
void unknown_t2(std::shared_ptr<Card> unknown_p2); EffectWhen WhenAllCards,
void unknown_8024997C(std::shared_ptr<Card> card); EffectWhen WhenAttackerAndActionCards,
void unknown_8024A394(std::shared_ptr<Card> card); EffectWhen WhenAttackerOrHunterSCCard,
EffectWhen WhenTargetsAndActionCards>
void apply_effects_before_or_after_attack(std::shared_ptr<Card> unknown_p2);
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); bool client_has_atk_dice_boost_condition(uint8_t client_id);
void unknown_8024A6DC( void unknown_8024A6DC(
std::shared_ptr<Card> unknown_p2, std::shared_ptr<Card> unknown_p3); std::shared_ptr<Card> unknown_p2, std::shared_ptr<Card> unknown_p3);
+144 -16
View File
@@ -109,8 +109,8 @@ bool Location::operator!=(const Location& other) const {
} }
std::string Location::str() const { std::string Location::str() const {
return string_printf("Location[x=%hhu, y=%hhu, dir=%s, u=%hhu]", return string_printf("Location[x=%hhu, y=%hhu, dir=%hhu:%s, u=%hhu]",
this->x, this->y, name_for_enum(this->direction), this->unused); this->x, this->y, static_cast<uint8_t>(this->direction), name_for_enum(this->direction), this->unused);
} }
void Location::clear() { void Location::clear() {
@@ -517,7 +517,7 @@ bool CardDefinition::Effect::is_empty() const {
return (this->effect_num == 0 && return (this->effect_num == 0 &&
this->type == ConditionType::NONE && this->type == ConditionType::NONE &&
this->expr.empty() && this->expr.empty() &&
this->when == 0 && this->when == EffectWhen::NONE &&
this->arg1.empty() && this->arg1.empty() &&
this->arg2.empty() && this->arg2.empty() &&
this->arg3.empty() && this->arg3.empty() &&
@@ -603,7 +603,7 @@ string CardDefinition::Effect::str(const char* separator, const TextSet* text_ar
if (!this->expr.empty()) { if (!this->expr.empty()) {
tokens.emplace_back("expr=" + this->expr.decode()); tokens.emplace_back("expr=" + this->expr.decode());
} }
tokens.emplace_back(string_printf("when=%02hhX", this->when)); tokens.emplace_back(string_printf("when=%02hhX:%s", static_cast<uint8_t>(this->when), name_for_enum(this->when)));
tokens.emplace_back("arg1=" + this->str_for_arg(this->arg1.decode())); tokens.emplace_back("arg1=" + this->str_for_arg(this->arg1.decode()));
tokens.emplace_back("arg2=" + this->str_for_arg(this->arg2.decode())); tokens.emplace_back("arg2=" + this->str_for_arg(this->arg2.decode()));
tokens.emplace_back("arg3=" + this->str_for_arg(this->arg3.decode())); tokens.emplace_back("arg3=" + this->str_for_arg(this->arg3.decode()));
@@ -736,23 +736,23 @@ string name_for_rank(CardRank rank) {
} }
} }
string name_for_target_mode(TargetMode target_mode) { const char* name_for_target_mode(TargetMode target_mode) {
static const vector<const char*> names({ static const vector<const char*> names({
"NONE", "NONE",
"SINGLE", "SINGLE_RANGE",
"MULTI", "MULTI_RANGE",
"SELF", "SELF",
"TEAM", "TEAM",
"EVERYONE",
"MULTI_RANGE_ALLIES",
"ALL_ALLIES",
"ALL", "ALL",
"MULTI-ALLY", "OWN_FCS",
"ALL-ALLY",
"ALL-ATTACK",
"OWN-FCS",
}); });
try { try {
return names.at(static_cast<uint8_t>(target_mode)); return names.at(static_cast<uint8_t>(target_mode));
} catch (const out_of_range&) { } catch (const out_of_range&) {
return string_printf("(%02hhX)", static_cast<uint8_t>(target_mode)); return "__UNKNOWN__";
} }
} }
@@ -886,7 +886,7 @@ string CardDefinition::str(bool single_line, const TextSet* text_archive) const
string criterion_str = name_for_enum(this->usable_criterion); string criterion_str = name_for_enum(this->usable_criterion);
string card_class_str = name_for_enum(this->card_class()); string card_class_str = name_for_enum(this->card_class());
string rank_str = name_for_rank(this->rank); string rank_str = name_for_rank(this->rank);
string target_mode_str = name_for_target_mode(this->target_mode); const char* target_mode_str = name_for_target_mode(this->target_mode);
string assist_turns_str = string_for_assist_turns(this->assist_turns); string assist_turns_str = string_for_assist_turns(this->assist_turns);
string hp_str = this->hp.str(); string hp_str = this->hp.str();
string ap_str = this->ap.str(); string ap_str = this->ap.str();
@@ -937,7 +937,7 @@ string CardDefinition::str(bool single_line, const TextSet* text_archive) const
criterion_str.c_str(), criterion_str.c_str(),
rank_str.c_str(), rank_str.c_str(),
cost_str.c_str(), cost_str.c_str(),
target_mode_str.c_str(), target_mode_str,
range_str.c_str(), range_str.c_str(),
assist_turns_str.c_str(), assist_turns_str.c_str(),
this->cannot_move ? "true" : "false", this->cannot_move ? "true" : "false",
@@ -1004,7 +1004,7 @@ Card: %04" PRIX32 " \"%s\"\n\
criterion_str.c_str(), criterion_str.c_str(),
rank_str.c_str(), rank_str.c_str(),
cost_str.c_str(), cost_str.c_str(),
target_mode_str.c_str(), target_mode_str,
range_str.c_str(), range_str.c_str(),
assist_turns_str.c_str(), assist_turns_str.c_str(),
this->cannot_move ? "cannot" : "can", this->cannot_move ? "cannot" : "can",
@@ -1068,7 +1068,7 @@ JSON CardDefinition::Effect::json() const {
{"EffectNum", this->effect_num}, {"EffectNum", this->effect_num},
{"ConditionType", name_for_enum(this->type)}, {"ConditionType", name_for_enum(this->type)},
{"Expression", this->expr.decode()}, {"Expression", this->expr.decode()},
{"When", this->when}, {"When", name_for_enum(this->when)},
{"Arg1", this->arg1.decode()}, {"Arg1", this->arg1.decode()},
{"Arg2", this->arg2.decode()}, {"Arg2", this->arg2.decode()},
{"Arg3", this->arg3.decode()}, {"Arg3", this->arg3.decode()},
@@ -1158,6 +1158,62 @@ void PlayerConfig::encrypt(uint8_t basis) {
this->basis = basis; this->basis = basis;
} }
PlayerConfigNTE::PlayerConfigNTE(const PlayerConfig& config)
: rank_text(config.rank_text),
unknown_a1(config.unknown_a1),
tech_menu_shortcut_entries(config.tech_menu_shortcut_entries),
choice_search_config(config.choice_search_config),
scenario_progress(config.scenario_progress),
unused_offline_records(config.unused_offline_records),
unknown_a4(config.unknown_a4),
is_encrypted(config.is_encrypted),
basis(config.basis),
unused(config.unused),
card_counts(config.card_counts),
card_count_checksums(config.card_count_checksums),
rare_tokens(config.rare_tokens),
decks(config.decks),
unknown_a8(config.unknown_a8),
offline_clv_exp(config.offline_clv_exp),
online_clv_exp(config.online_clv_exp),
recent_human_opponents(config.recent_human_opponents),
unknown_a10(config.unknown_a10),
init_timestamp(config.init_timestamp),
last_online_battle_start_timestamp(config.last_online_battle_start_timestamp),
unknown_t3(config.unknown_t3),
unknown_a14(config.unknown_a14) {
// TODO: Do we need to recompute card_count_checksums? (Here or in operator
// PlayerConfig?)
}
PlayerConfigNTE::operator PlayerConfig() const {
PlayerConfig ret;
ret.rank_text = this->rank_text;
ret.unknown_a1 = this->unknown_a1;
ret.tech_menu_shortcut_entries = this->tech_menu_shortcut_entries;
ret.choice_search_config = this->choice_search_config;
ret.scenario_progress = this->scenario_progress;
ret.unused_offline_records = this->unused_offline_records;
ret.unknown_a4 = this->unknown_a4;
ret.is_encrypted = this->is_encrypted;
ret.basis = this->basis;
ret.unused = this->unused;
ret.card_counts = this->card_counts;
ret.card_count_checksums = this->card_count_checksums;
ret.rare_tokens = this->rare_tokens;
ret.decks = this->decks;
ret.unknown_a8 = this->unknown_a8;
ret.offline_clv_exp = this->offline_clv_exp;
ret.online_clv_exp = this->online_clv_exp;
ret.recent_human_opponents = this->recent_human_opponents;
ret.unknown_a10 = this->unknown_a10;
ret.init_timestamp = this->init_timestamp;
ret.last_online_battle_start_timestamp = this->last_online_battle_start_timestamp;
ret.unknown_t3 = this->unknown_t3;
ret.unknown_a14 = this->unknown_a14;
return ret;
}
Rules::Rules(const JSON& json) { Rules::Rules(const JSON& json) {
this->clear(); this->clear();
this->overall_time_limit = json.get_int("overall_time_limit", this->overall_time_limit); this->overall_time_limit = json.get_int("overall_time_limit", this->overall_time_limit);
@@ -3117,6 +3173,78 @@ const char* name_for_enum<Episode3::ConditionType>(Episode3::ConditionType cond_
} }
} }
template <>
const char* name_for_enum<Episode3::EffectWhen>(Episode3::EffectWhen when) {
switch (when) {
case Episode3::EffectWhen::NONE:
return "NONE";
case Episode3::EffectWhen::CARD_SET:
return "CARD_SET";
case Episode3::EffectWhen::AFTER_ANY_CARD_ATTACK:
return "AFTER_ANY_CARD_ATTACK";
case Episode3::EffectWhen::BEFORE_ANY_CARD_ATTACK:
return "BEFORE_ANY_CARD_ATTACK";
case Episode3::EffectWhen::BEFORE_DICE_PHASE_THIS_TEAM_TURN:
return "BEFORE_DICE_PHASE_THIS_TEAM_TURN";
case Episode3::EffectWhen::CARD_DESTROYED:
return "CARD_DESTROYED";
case Episode3::EffectWhen::AFTER_SET_PHASE:
return "AFTER_SET_PHASE";
case Episode3::EffectWhen::BEFORE_MOVE_PHASE:
return "BEFORE_MOVE_PHASE";
case Episode3::EffectWhen::UNKNOWN_0A:
return "UNKNOWN_0A";
case Episode3::EffectWhen::AFTER_ATTACK_TARGET_RESOLUTION:
return "AFTER_ATTACK_TARGET_RESOLUTION";
case Episode3::EffectWhen::AFTER_THIS_CARD_ATTACK:
return "AFTER_THIS_CARD_ATTACK";
case Episode3::EffectWhen::BEFORE_THIS_CARD_ATTACK:
return "BEFORE_THIS_CARD_ATTACK";
case Episode3::EffectWhen::BEFORE_ACT_PHASE:
return "BEFORE_ACT_PHASE";
case Episode3::EffectWhen::BEFORE_DRAW_PHASE:
return "BEFORE_DRAW_PHASE";
case Episode3::EffectWhen::AFTER_CARD_MOVE:
return "AFTER_CARD_MOVE";
case Episode3::EffectWhen::UNKNOWN_15:
return "UNKNOWN_15";
case Episode3::EffectWhen::AFTER_THIS_CARD_ATTACKED:
return "AFTER_THIS_CARD_ATTACKED";
case Episode3::EffectWhen::BEFORE_THIS_CARD_ATTACKED:
return "BEFORE_THIS_CARD_ATTACKED";
case Episode3::EffectWhen::AFTER_CREATURE_OR_HUNTER_SC_ATTACK:
return "AFTER_CREATURE_OR_HUNTER_SC_ATTACK";
case Episode3::EffectWhen::BEFORE_CREATURE_OR_HUNTER_SC_ATTACK:
return "BEFORE_CREATURE_OR_HUNTER_SC_ATTACK";
case Episode3::EffectWhen::UNKNOWN_22:
return "UNKNOWN_22";
case Episode3::EffectWhen::BEFORE_MOVE_PHASE_AND_AFTER_CARD_MOVE_FINAL:
return "BEFORE_MOVE_PHASE_AND_AFTER_CARD_MOVE_FINAL";
case Episode3::EffectWhen::UNKNOWN_29:
return "UNKNOWN_29";
case Episode3::EffectWhen::UNKNOWN_2A:
return "UNKNOWN_2A";
case Episode3::EffectWhen::UNKNOWN_2B:
return "UNKNOWN_2B";
case Episode3::EffectWhen::UNKNOWN_33:
return "UNKNOWN_33";
case Episode3::EffectWhen::UNKNOWN_34:
return "UNKNOWN_34";
case Episode3::EffectWhen::UNKNOWN_35:
return "UNKNOWN_35";
case Episode3::EffectWhen::ATTACK_STAT_OVERRIDES:
return "ATTACK_STAT_OVERRIDES";
case Episode3::EffectWhen::ATTACK_DAMAGE_ADJUSTMENT:
return "ATTACK_DAMAGE_ADJUSTMENT";
case Episode3::EffectWhen::DEFENSE_DAMAGE_ADJUSTMENT:
return "DEFENSE_DAMAGE_ADJUSTMENT";
case Episode3::EffectWhen::BEFORE_DICE_PHASE_ALL_TURNS_FINAL:
return "BEFORE_DICE_PHASE_ALL_TURNS_FINAL";
default:
return "__INVALID__";
}
}
template <> template <>
const char* name_for_enum<Episode3::Direction>(Episode3::Direction d) { const char* name_for_enum<Episode3::Direction>(Episode3::Direction d) {
switch (d) { switch (d) {
+99 -26
View File
@@ -173,6 +173,8 @@ enum class TargetMode : uint8_t {
OWN_FCS = 0x09, // e.g. Traitor OWN_FCS = 0x09, // e.g. Traitor
}; };
const char* name_for_target_mode(TargetMode target_mode);
enum class ConditionType : uint8_t { enum class ConditionType : uint8_t {
NONE = 0x00, NONE = 0x00,
AP_BOOST = 0x01, // Temporarily increase AP by N AP_BOOST = 0x01, // Temporarily increase AP by N
@@ -304,6 +306,41 @@ enum class ConditionType : uint8_t {
ANY_FF = 0xFF, // Used as a wildcard in some search functions ANY_FF = 0xFF, // Used as a wildcard in some search functions
}; };
enum class EffectWhen : uint8_t {
NONE = 0x00,
CARD_SET = 0x01, // Permanent effects like RAMPAGE/PIERCE on SCs, BIG_SWING, AERIAL, etc.; many AC effects also
AFTER_ANY_CARD_ATTACK = 0x02, // GIVE_DAMAGE, HEAL, A_H_SWAP_PERM
BEFORE_ANY_CARD_ATTACK = 0x03, // AP_LOSS, COMBO_TP
BEFORE_DICE_PHASE_THIS_TEAM_TURN = 0x04, // Many different effects
CARD_DESTROYED = 0x05, // RETURN_TO_HAND, RETURN, FILIAL, GIVE_OR_TAKE_EXP
AFTER_SET_PHASE = 0x06, // Unused
BEFORE_MOVE_PHASE = 0x09, // Unused
UNKNOWN_0A = 0x0A, // ANTI_ABNORMALITY_2 on Tollaw (non-SC version of another when?)
AFTER_ATTACK_TARGET_RESOLUTION = 0x0B, // ABILITY_TRAP via First Attack action card only
AFTER_THIS_CARD_ATTACK = 0x0C, // Many effects
BEFORE_THIS_CARD_ATTACK = 0x0D, // Conditions, AP_BOOST/TP_BOOST, AP_SILENCE, MULTI_STRIKE
BEFORE_ACT_PHASE = 0x0E, // Before act phase (ANTI_ABNORMALITY_2, FIXED_RANGE)
BEFORE_DRAW_PHASE = 0x0F, // Unused
AFTER_CARD_MOVE = 0x13, // Unused
UNKNOWN_15 = 0x15, // Unused
AFTER_THIS_CARD_ATTACKED = 0x16, // Conditions, DEATH_COMPANION, GIVE_DAMAGE, AP_GROWTH (Nidra)
BEFORE_THIS_CARD_ATTACKED = 0x17, // Defense damage adjustments
AFTER_CREATURE_OR_HUNTER_SC_ATTACK = 0x20, // RETURN_TO_HAND, A_T_SWAP_PERM, GIVE_OR_TAKE_EXP
BEFORE_CREATURE_OR_HUNTER_SC_ATTACK = 0x21, // Unused
UNKNOWN_22 = 0x22, // MISC_AP_BONUSES (SCs only?)
BEFORE_MOVE_PHASE_AND_AFTER_CARD_MOVE_FINAL = 0x27, // SET_MV
UNKNOWN_29 = 0x29, // MIGHTY_KNUCKLE
UNKNOWN_2A = 0x2A, // Unused
UNKNOWN_2B = 0x2B, // Unused
UNKNOWN_33 = 0x33, // DEF_DISABLE_BY_COST
UNKNOWN_34 = 0x34, // Unused
UNKNOWN_35 = 0x35, // Unused
ATTACK_STAT_OVERRIDES = 0x3D, // BONUS_FROM_LEADER, COPY, ABILITY_TRAP
ATTACK_DAMAGE_ADJUSTMENT = 0x3E, // AP_BOOST, SLAYERS_ASSASSINS, WEAK_SPOT_INFLUENCE, GROUP
DEFENSE_DAMAGE_ADJUSTMENT = 0x3F, // MOSTLY_HALFGUARDS, ACTION_DISRUPTER
BEFORE_DICE_PHASE_ALL_TURNS_FINAL = 0x46, // Pollux Timed Pierce
};
enum class AssistEffect : uint16_t { enum class AssistEffect : uint16_t {
NONE = 0x0000, NONE = 0x0000,
DICE_HALF = 0x0001, DICE_HALF = 0x0001,
@@ -447,7 +484,7 @@ struct Location {
void clear(); void clear();
void clear_FF(); void clear_FF();
} __attribute__((packed)); } __packed_ws__(Location, 4);
struct CardDefinition { struct CardDefinition {
struct Stat { struct Stat {
@@ -470,7 +507,7 @@ struct CardDefinition {
void decode_code(); void decode_code();
std::string str() const; std::string str() const;
JSON json() const; JSON json() const;
} __attribute__((packed)); } __packed_ws__(Stat, 4);
struct Effect { struct Effect {
// effect_num is the 1-based index of this effect within the card definition // effect_num is the 1-based index of this effect within the card definition
@@ -486,7 +523,7 @@ struct CardDefinition {
/* 02 */ pstring<TextEncoding::ASCII, 0x0F> expr; /* 02 */ pstring<TextEncoding::ASCII, 0x0F> expr;
// when specifies in which phase the effect should activate. // when specifies in which phase the effect should activate.
// TODO: There are many values that can be used here; document them. // TODO: There are many values that can be used here; document them.
/* 11 */ uint8_t when; /* 11 */ EffectWhen when;
// arg1 generally specifies how long the effect activates for. // arg1 generally specifies how long the effect activates for.
/* 12 */ pstring<TextEncoding::ASCII, 4> arg1; /* 12 */ pstring<TextEncoding::ASCII, 4> arg1;
// arg2 generally specifies a condition for when the effect activates. // arg2 generally specifies a condition for when the effect activates.
@@ -507,7 +544,7 @@ struct CardDefinition {
static std::string str_for_arg(const std::string& arg); static std::string str_for_arg(const std::string& arg);
std::string str(const char* separator = ", ", const TextSet* text_archive = nullptr) const; std::string str(const char* separator = ", ", const TextSet* text_archive = nullptr) const;
JSON json() const; JSON json() const;
} __attribute__((packed)); } __packed_ws__(Effect, 0x20);
/* 0000 */ be_uint32_t card_id; /* 0000 */ be_uint32_t card_id;
/* 0004 */ pstring<TextEncoding::SJIS, 0x40> jp_name; /* 0004 */ pstring<TextEncoding::SJIS, 0x40> jp_name;
@@ -774,7 +811,7 @@ struct CardDefinition {
void decode_range(); void decode_range();
std::string str(bool single_line = true, const TextSet* text_archive = nullptr) const; std::string str(bool single_line = true, const TextSet* text_archive = nullptr) const;
JSON json() const; JSON json() const;
} __attribute__((packed)); // 0x128 bytes in total } __packed_ws__(CardDefinition, 0x128);
struct CardDefinitionsFooter { struct CardDefinitionsFooter {
// Technically the card definitions file is a REL file, so the last 0x20 bytes // Technically the card definitions file is a REL file, so the last 0x20 bytes
@@ -790,7 +827,7 @@ struct CardDefinitionsFooter {
/* 48 */ be_uint32_t footer_offset; /* 48 */ be_uint32_t footer_offset;
/* 4C */ parray<be_uint32_t, 3> unused2; /* 4C */ parray<be_uint32_t, 3> unused2;
/* 58 */ /* 58 */
} __attribute__((packed)); } __packed_ws__(CardDefinitionsFooter, 0x58);
struct DeckDefinition { struct DeckDefinition {
/* 00 */ pstring<TextEncoding::MARKED, 0x10> name; /* 00 */ pstring<TextEncoding::MARKED, 0x10> name;
@@ -810,7 +847,7 @@ struct DeckDefinition {
/* 82 */ uint8_t second; /* 82 */ uint8_t second;
/* 83 */ uint8_t unknown_a2; /* 83 */ uint8_t unknown_a2;
/* 84 */ /* 84 */
} __attribute__((packed)); } __packed_ws__(DeckDefinition, 0x84);
struct PlayerConfig { struct PlayerConfig {
// The game splits this internally into two structures. The first column of // The game splits this internally into two structures. The first column of
@@ -828,7 +865,7 @@ struct PlayerConfig {
// earlier version, this was the offline records structure, but they later // earlier version, this was the offline records structure, but they later
// decided to just count online and offline records together in the main // decided to just count online and offline records together in the main
// records structure and didn't remove the codepath that reads from this. // records structure and didn't remove the codepath that reads from this.
/* 0138:---- */ PlayerRecords_Battle<true> unused_offline_records; /* 0138:---- */ PlayerRecordsBattleBE unused_offline_records;
/* 0150:---- */ parray<uint8_t, 4> unknown_a4; /* 0150:---- */ parray<uint8_t, 4> unknown_a4;
// The PlayerDataSegment structure begins here. In newserv, we combine this // The PlayerDataSegment structure begins here. In newserv, we combine this
// structure into PlayerConfig since the two are always used together. // structure into PlayerConfig since the two are always used together.
@@ -870,16 +907,16 @@ struct PlayerConfig {
// card counts array is encrypted in memory most of the time, and they went // card counts array is encrypted in memory most of the time, and they went
// out of their way to ensure the game uses an area of memory that almost no // out of their way to ensure the game uses an area of memory that almost no
// other game uses, which is also used by the Action Replay.) // other game uses, which is also used by the Action Replay.)
/* 05A4:0450 */ parray<be_uint64_t, 0x1C2> rare_tokens; /* 05A4:0450 */ parray<be_uint64_t, 450> rare_tokens;
/* 13B4:1260 */ parray<uint8_t, 0x80> unknown_a7; /* 13B4:1260 */ parray<uint8_t, 0x80> unknown_a7;
/* 1434:12E0 */ parray<DeckDefinition, 25> decks; /* 1434:12E0 */ parray<DeckDefinition, 25> decks;
/* 2118:1FC4 */ parray<uint8_t, 0x08> unknown_a8; /* 2118:1FC4 */ parray<uint8_t, 0x08> unknown_a8;
/* 2120:1FCC */ be_uint32_t offline_clv_exp; // CLvOff = this / 100 /* 2120:1FCC */ be_uint32_t offline_clv_exp; // CLvOff = (this / 100) + 1
/* 2124:1FD0 */ be_uint32_t online_clv_exp; // CLvOn = this / 100 /* 2124:1FD0 */ be_uint32_t online_clv_exp; // CLvOn = (this / 100) + 1
struct PlayerReference { struct PlayerReference {
/* 00 */ be_uint32_t guild_card_number; /* 00 */ be_uint32_t guild_card_number;
/* 04 */ pstring<TextEncoding::MARKED, 0x18> name; /* 04 */ pstring<TextEncoding::MARKED, 0x18> name;
} __attribute__((packed)); } __packed_ws__(PlayerReference, 0x1C);
// This array is updated when a battle is started (via a 6xB4x05 command). The // This array is updated when a battle is started (via a 6xB4x05 command). The
// client adds the opposing players' info to ths first two entries here if the // client adds the opposing players' info to ths first two entries here if the
// opponents are human. (The existing entries are always moved back by two // opponents are human. (The existing entries are always moved back by two
@@ -902,7 +939,41 @@ struct PlayerConfig {
void decrypt(); void decrypt();
void encrypt(uint8_t basis); void encrypt(uint8_t basis);
} __attribute__((packed)); } __packed_ws__(PlayerConfig, 0x2350);
struct PlayerConfigNTE {
/* 0000 */ pstring<TextEncoding::MARKED, 12> rank_text;
/* 000C */ parray<uint8_t, 0x1C> unknown_a1;
/* 0028 */ parray<be_uint16_t, 20> tech_menu_shortcut_entries;
/* 0050 */ parray<be_uint32_t, 10> choice_search_config;
/* 0078 */ parray<be_uint32_t, 0x10> scenario_progress; // Final has 0x30 entries here
/* 00B8 */ PlayerRecordsBattleBE unused_offline_records;
/* 00D0 */ parray<uint8_t, 4> unknown_a4;
/* 00D4 */ uint8_t is_encrypted;
/* 00D5 */ uint8_t basis;
/* 00D6 */ parray<uint8_t, 2> unused;
/* 00D8 */ parray<uint8_t, 1000> card_counts;
/* 04C0 */ parray<be_uint16_t, 50> card_count_checksums;
/* 0524 */ parray<be_uint64_t, 300> rare_tokens;
/* 0E84 */ parray<DeckDefinition, 25> decks;
/* 1B68 */ parray<uint8_t, 0x08> unknown_a8;
/* 1B70 */ be_uint32_t offline_clv_exp;
/* 1B74 */ be_uint32_t online_clv_exp;
/* 1B78 */ parray<PlayerConfig::PlayerReference, 10> recent_human_opponents;
/* 1C90 */ parray<uint8_t, 0x28> unknown_a10;
/* 1CB8 */ be_uint32_t init_timestamp;
/* 1CBC */ be_uint32_t last_online_battle_start_timestamp;
/* 1CC0 */ be_uint32_t unknown_t3;
/* 1CC4 */ parray<uint8_t, 0x94> unknown_a14;
/* 1D58 */
PlayerConfigNTE() = default;
explicit PlayerConfigNTE(const PlayerConfig& config);
operator PlayerConfig() const;
void decrypt();
void encrypt(uint8_t basis);
} __packed_ws__(PlayerConfigNTE, 0x1D58);
enum class HPType : uint8_t { enum class HPType : uint8_t {
DEFEAT_PLAYER = 0, DEFEAT_PLAYER = 0,
@@ -972,7 +1043,7 @@ struct Rules {
std::pair<uint8_t, uint8_t> def_dice_range(bool is_1p_2v1) const; std::pair<uint8_t, uint8_t> def_dice_range(bool is_1p_2v1) const;
std::string str() const; std::string str() const;
} __attribute__((packed)); } __packed_ws__(Rules, 0x14);
struct RulesTrial { struct RulesTrial {
// Most fields here have the same meanings as in the final version. // Most fields here have the same meanings as in the final version.
@@ -996,7 +1067,7 @@ struct RulesTrial {
RulesTrial() = default; RulesTrial() = default;
RulesTrial(const Rules&); RulesTrial(const Rules&);
operator Rules() const; operator Rules() const;
} __attribute__((packed)); } __packed_ws__(RulesTrial, 0x0C);
struct StateFlags { struct StateFlags {
/* 00 */ le_uint16_t turn_num; /* 00 */ le_uint16_t turn_num;
@@ -1018,7 +1089,7 @@ struct StateFlags {
bool operator!=(const StateFlags& other) const; bool operator!=(const StateFlags& other) const;
void clear(); void clear();
void clear_FF(); void clear_FF();
} __attribute__((packed)); } __packed_ws__(StateFlags, 0x18);
struct MapList { struct MapList {
be_uint32_t num_maps; be_uint32_t num_maps;
@@ -1046,18 +1117,18 @@ struct MapList {
/* 021C */ uint8_t map_category; /* 021C */ uint8_t map_category;
/* 021D */ parray<uint8_t, 3> unused; /* 021D */ parray<uint8_t, 3> unused;
/* 0220 */ /* 0220 */
} __attribute__((packed)); } __packed_ws__(Entry, 0x220);
// Variable-length fields: // Variable-length fields:
// Entry entries[num_maps]; // Entry entries[num_maps];
// char strings[...EOF]; // Null-terminated strings, pointed to by offsets in Entry structs // char strings[...EOF]; // Null-terminated strings, pointed to by offsets in Entry structs
} __attribute__((packed)); } __packed_ws__(MapList, 0x10);
struct CompressedMapHeader { // .mnm file format struct CompressedMapHeader { // .mnm file format
le_uint32_t map_number; le_uint32_t map_number;
le_uint32_t compressed_data_size; le_uint32_t compressed_data_size;
// Compressed data immediately follows (which decompresses to a MapDefinition) // Compressed data immediately follows (which decompresses to a MapDefinition)
} __attribute__((packed)); } __packed_ws__(CompressedMapHeader, 8);
struct MapDefinition { // .mnmd format; also the format of (decompressed) quests struct MapDefinition { // .mnmd format; also the format of (decompressed) quests
// If tag is not 0x00000100, the game considers the map to be corrupt in // If tag is not 0x00000100, the game considers the map to be corrupt in
@@ -1155,7 +1226,7 @@ struct MapDefinition { // .mnmd format; also the format of (decompressed) quests
std::string str() const; std::string str() const;
JSON json() const; JSON json() const;
} __attribute__((packed)); } __packed_ws__(CameraSpec, 0x48);
// This array specifies the camera zone maps. A camera zone map is a subset of // This array specifies the camera zone maps. A camera zone map is a subset of
// the main map (specified in map_tiles). Tiles that are part of each camera // the main map (specified in map_tiles). Tiles that are part of each camera
@@ -1213,7 +1284,7 @@ struct MapDefinition { // .mnmd format; also the format of (decompressed) quests
/* 18 */ parray<be_uint16_t, 0x20> card_ids; // Last one appears to always be FFFF /* 18 */ parray<be_uint16_t, 0x20> card_ids; // Last one appears to always be FFFF
/* 58 */ /* 58 */
JSON json(uint8_t language) const; JSON json(uint8_t language) const;
} __attribute__((packed)); } __packed_ws__(NPCDeck, 0x58);
/* 1FE8 */ parray<NPCDeck, 3> npc_decks; // Unused if name[0] == 0 /* 1FE8 */ parray<NPCDeck, 3> npc_decks; // Unused if name[0] == 0
// These are almost (but not quite) the same format as the entries in // These are almost (but not quite) the same format as the entries in
@@ -1229,7 +1300,7 @@ struct MapDefinition { // .mnmd format; also the format of (decompressed) quests
/* 0018 */ parray<be_uint16_t, 0x7E> params; /* 0018 */ parray<be_uint16_t, 0x7E> params;
/* 0114 */ /* 0114 */
JSON json(uint8_t language) const; JSON json(uint8_t language) const;
} __attribute__((packed)); } __packed_ws__(AIParams, 0x114);
/* 20F0 */ parray<AIParams, 3> npc_ai_params; // Unused if name[0] == 0 /* 20F0 */ parray<AIParams, 3> npc_ai_params; // Unused if name[0] == 0
/* 242C */ parray<uint8_t, 8> unknown_a7; /* 242C */ parray<uint8_t, 8> unknown_a7;
@@ -1282,7 +1353,7 @@ struct MapDefinition { // .mnmd format; also the format of (decompressed) quests
/* 0004 */ parray<pstring<TextEncoding::MARKED, 0x40>, 4> strings; /* 0004 */ parray<pstring<TextEncoding::MARKED, 0x40>, 4> strings;
/* 0104 */ /* 0104 */
JSON json(uint8_t language) const; JSON json(uint8_t language) const;
} __attribute__((packed)); } __packed_ws__(DialogueSet, 0x104);
// There are up to 0x10 of these per valid NPC, but only the first 13 of them // There are up to 0x10 of these per valid NPC, but only the first 13 of them
// are used, since each one must have a unique value for .when and the values // are used, since each one must have a unique value for .when and the values
@@ -1365,7 +1436,7 @@ struct MapDefinition { // .mnmd format; also the format of (decompressed) quests
bool operator==(const EntryState& other) const = default; bool operator==(const EntryState& other) const = default;
bool operator!=(const EntryState& other) const = default; bool operator!=(const EntryState& other) const = default;
JSON json() const; JSON json() const;
} __attribute__((packed)); } __packed_ws__(EntryState, 2);
/* 5A10 */ parray<EntryState, 4> entry_states; /* 5A10 */ parray<EntryState, 4> entry_states;
/* 5A18 */ /* 5A18 */
@@ -1381,7 +1452,7 @@ struct MapDefinition { // .mnmd format; also the format of (decompressed) quests
std::string str(const CardIndex* card_index, uint8_t language) const; std::string str(const CardIndex* card_index, uint8_t language) const;
JSON json(uint8_t language) const; JSON json(uint8_t language) const;
} __attribute__((packed)); } __packed_ws__(MapDefinition, 0x5A18);
struct MapDefinitionTrial { struct MapDefinitionTrial {
// This is the format of Episode 3 Trial Edition maps. See the comments in // This is the format of Episode 3 Trial Edition maps. See the comments in
@@ -1430,7 +1501,7 @@ struct MapDefinitionTrial {
MapDefinitionTrial(const MapDefinition& map); MapDefinitionTrial(const MapDefinition& map);
operator MapDefinition() const; operator MapDefinition() const;
} __attribute__((packed)); } __packed_ws__(MapDefinitionTrial, 0x41A0);
struct COMDeckDefinition { struct COMDeckDefinition {
size_t index; size_t index;
@@ -1575,4 +1646,6 @@ const char* name_for_enum<Episode3::CardClass>(Episode3::CardClass cc);
template <> template <>
const char* name_for_enum<Episode3::ConditionType>(Episode3::ConditionType cond_type); const char* name_for_enum<Episode3::ConditionType>(Episode3::ConditionType cond_type);
template <> template <>
const char* name_for_enum<Episode3::EffectWhen>(Episode3::EffectWhen when);
template <>
const char* name_for_enum<Episode3::Direction>(Episode3::Direction d); const char* name_for_enum<Episode3::Direction>(Episode3::Direction d);
+36 -22
View File
@@ -1,5 +1,7 @@
#include "DeckState.hh" #include "DeckState.hh"
#include "Server.hh"
using namespace std; using namespace std;
namespace Episode3 { namespace Episode3 {
@@ -84,43 +86,45 @@ bool DeckState::draw_card_by_ref(uint16_t card_ref) {
} }
uint8_t index = index_for_card_ref(card_ref); uint8_t index = index_for_card_ref(card_ref);
if (index > this->entries.size()) { if (index >= this->entries.size()) {
return false; return false;
} }
if (this->entries[index].state == CardState::DISCARDED) { 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 // If the card is discarded, then it should be before the draw index, and we
// can just change its state. // can just change its state.
this->entries[index].state = CardState::IN_HAND; entry.state = CardState::IN_HAND;
return true; return true;
}
} else if (this->entries[index].state == CardState::DRAWABLE) { if (entry.state != CardState::DRAWABLE) {
// If the card is still drawable, we need to move it so it's just in front return false;
// of the draw index, then immediately draw it. Ep3 NTE does not handle this }
// 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. // case, but we do even when playing NTE.
ssize_t ref_index; size_t ref_index;
for (ref_index = this->card_refs.size(); ref_index >= 0; ref_index--) { for (ref_index = 0; ref_index < this->card_refs.size(); ref_index++) {
if (this->card_refs[ref_index] == card_ref) { if (this->card_refs[ref_index] == card_ref) {
break; break;
} }
} }
if (ref_index < 0) { if (ref_index >= this->card_refs.size()) {
return false; return false;
} }
size_t ref_uindex = ref_index; for (; ref_index > this->draw_index; ref_index--) {
for (; ref_uindex > this->draw_index; ref_uindex--) { // this->draw_index is also unsigned, so ref_index cannot be zero here
// Note: draw_index is also unsigned, so ref_uindex cannot be zero here this->card_refs[ref_index] = this->card_refs[ref_index - 1];
this->card_refs[ref_uindex] = this->card_refs[ref_uindex - 1];
} }
this->card_refs[this->draw_index] = card_ref; this->card_refs[this->draw_index] = card_ref;
this->entries[index].state = CardState::IN_HAND;
// Draw the card
entry.state = CardState::IN_HAND;
this->draw_index++; this->draw_index++;
return true; return true;
} else {
return false;
}
} }
uint16_t DeckState::card_id_for_card_ref(uint16_t card_ref) const { uint16_t DeckState::card_id_for_card_ref(uint16_t card_ref) const {
@@ -201,12 +205,17 @@ void DeckState::do_mulligan(bool is_nte) {
this->card_refs[index + 5] = temp_ref; this->card_refs[index + 5] = temp_ref;
} }
auto s = this->server.lock();
if (!s) {
throw runtime_error("server is missing");
}
// Shuffle the deck, except the first 5 cards (which are about to be drawn). // Shuffle the deck, except the first 5 cards (which are about to be drawn).
size_t max = this->num_drawable_cards() - 5; size_t max = this->num_drawable_cards() - 5;
uint8_t base_index = this->draw_index + 5; uint8_t base_index = this->draw_index + 5;
for (size_t z = 0; z < this->card_refs.size(); z++) { for (size_t z = 0; z < this->card_refs.size(); z++) {
uint8_t index1 = random_from_optional_crypt(this->opt_rand_crypt) % max; uint8_t index1 = s->get_random(max);
uint8_t index2 = random_from_optional_crypt(this->opt_rand_crypt) % max; uint8_t index2 = s->get_random(max);
uint16_t temp_ref = this->card_refs[base_index + index1]; uint16_t temp_ref = this->card_refs[base_index + index1];
this->card_refs[base_index + index1] = this->card_refs[base_index + index2]; this->card_refs[base_index + index1] = this->card_refs[base_index + index2];
this->card_refs[base_index + index2] = temp_ref; this->card_refs[base_index + index2] = temp_ref;
@@ -258,6 +267,11 @@ void DeckState::set_card_discarded(uint16_t card_ref) {
void DeckState::shuffle() { void DeckState::shuffle() {
if (this->shuffle_enabled) { if (this->shuffle_enabled) {
auto s = this->server.lock();
if (!s) {
throw runtime_error("server is missing");
}
size_t max = this->num_drawable_cards(); size_t max = this->num_drawable_cards();
for (size_t z = 0; z < this->card_refs.size(); z++) { for (size_t z = 0; z < this->card_refs.size(); z++) {
// Note: This is the way Sega originally implemented shuffling - they just // Note: This is the way Sega originally implemented shuffling - they just
@@ -265,8 +279,8 @@ void DeckState::shuffle() {
// instead swap each item with another random item (possibly itself) that // 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 // doesn't appear earlier than it in the array, but this is not what Sega
// did. // did.
uint8_t index1 = this->draw_index + random_from_optional_crypt(this->opt_rand_crypt) % max; uint8_t index1 = this->draw_index + s->get_random(max);
uint8_t index2 = this->draw_index + random_from_optional_crypt(this->opt_rand_crypt) % max; uint8_t index2 = this->draw_index + s->get_random(max);
uint16_t temp_ref = this->card_refs[index1]; uint16_t temp_ref = this->card_refs[index1];
this->card_refs[index1] = this->card_refs[index2]; this->card_refs[index1] = this->card_refs[index2];
this->card_refs[index2] = temp_ref; this->card_refs[index2] = temp_ref;
+10 -8
View File
@@ -10,6 +10,8 @@
namespace Episode3 { namespace Episode3 {
class Server;
struct NameEntry { struct NameEntry {
/* 00 */ pstring<TextEncoding::MARKED, 0x10> name; /* 00 */ pstring<TextEncoding::MARKED, 0x10> name;
/* 10 */ uint8_t client_id; /* 10 */ uint8_t client_id;
@@ -20,7 +22,7 @@ struct NameEntry {
NameEntry(); NameEntry();
void clear(); void clear();
} __attribute__((packed)); } __packed_ws__(NameEntry, 0x14);
struct DeckEntry { struct DeckEntry {
/* 00 */ pstring<TextEncoding::MARKED, 0x10> name; /* 00 */ pstring<TextEncoding::MARKED, 0x10> name;
@@ -37,7 +39,7 @@ struct DeckEntry {
DeckEntry(); DeckEntry();
void clear(); void clear();
} __attribute__((packed)); } __packed_ws__(DeckEntry, 0x58);
uint8_t index_for_card_ref(uint16_t card_ref); uint8_t index_for_card_ref(uint16_t card_ref);
uint8_t client_id_for_card_ref(uint16_t card_ref); uint8_t client_id_for_card_ref(uint16_t card_ref);
@@ -57,13 +59,13 @@ public:
DeckState( DeckState(
uint8_t client_id, uint8_t client_id,
const parray<CardIDT, 0x1F>& card_ids, const parray<CardIDT, 0x1F>& card_ids,
std::shared_ptr<PSOLFGEncryption> opt_rand_crypt) std::shared_ptr<Server> server)
: client_id(client_id), : server(server),
client_id(client_id),
draw_index(1), draw_index(1),
card_ref_base(this->client_id << 8), card_ref_base(this->client_id << 8),
shuffle_enabled(true), shuffle_enabled(true),
loop_enabled(true), loop_enabled(true) {
opt_rand_crypt(opt_rand_crypt) {
for (size_t z = 0; z < card_ids.size(); z++) { for (size_t z = 0; z < card_ids.size(); z++) {
auto& e = this->entries[z]; auto& e = this->entries[z];
e.card_id = card_ids[z]; e.card_id = card_ids[z];
@@ -99,6 +101,8 @@ public:
void print(FILE* stream, std::shared_ptr<const CardIndex> card_index = nullptr) const; void print(FILE* stream, std::shared_ptr<const CardIndex> card_index = nullptr) const;
private: private:
std::weak_ptr<Server> server;
struct CardEntry { struct CardEntry {
uint16_t card_id; uint16_t card_id;
uint8_t deck_index; uint8_t deck_index;
@@ -111,8 +115,6 @@ private:
bool loop_enabled; bool loop_enabled;
parray<CardEntry, 31> entries; parray<CardEntry, 31> entries;
parray<uint16_t, 31> card_refs; parray<uint16_t, 31> card_refs;
std::shared_ptr<PSOLFGEncryption> opt_rand_crypt;
}; };
} // namespace Episode3 } // namespace Episode3
+4 -4
View File
@@ -20,7 +20,7 @@ struct MapState {
void clear(); void clear();
void print(FILE* stream) const; void print(FILE* stream) const;
} __attribute__((packed)); } __packed_ws__(MapState, 0x110);
struct MapAndRulesState { struct MapAndRulesState {
/* 0000 */ MapState map; /* 0000 */ MapState map;
@@ -45,7 +45,7 @@ struct MapAndRulesState {
void set_occupied_bit_for_tile(uint8_t x, uint8_t y); void set_occupied_bit_for_tile(uint8_t x, uint8_t y);
void clear_occupied_bit_for_tile(uint8_t x, uint8_t y); void clear_occupied_bit_for_tile(uint8_t x, uint8_t y);
} __attribute__((packed)); } __packed_ws__(MapAndRulesState, 0x138);
struct MapAndRulesStateTrial { struct MapAndRulesStateTrial {
/* 0000 */ MapState map; /* 0000 */ MapState map;
@@ -65,7 +65,7 @@ struct MapAndRulesStateTrial {
MapAndRulesStateTrial() = default; MapAndRulesStateTrial() = default;
MapAndRulesStateTrial(const MapAndRulesState& state); MapAndRulesStateTrial(const MapAndRulesState& state);
operator MapAndRulesState() const; operator MapAndRulesState() const;
} __attribute__((packed)); } __packed_ws__(MapAndRulesStateTrial, 0x130);
struct OverlayState { struct OverlayState {
parray<parray<uint8_t, 0x10>, 0x10> tiles; parray<parray<uint8_t, 0x10>, 0x10> tiles;
@@ -75,6 +75,6 @@ struct OverlayState {
OverlayState(); OverlayState();
void clear(); void clear();
} __attribute__((packed)); } __packed_ws__(OverlayState, 0x174);
} // namespace Episode3 } // namespace Episode3
+6 -7
View File
@@ -49,7 +49,7 @@ void PlayerState::init() {
throw logic_error("replacing a player state object is not permitted"); throw logic_error("replacing a player state object is not permitted");
} }
this->deck_state = make_shared<DeckState>(this->client_id, s->deck_entries[client_id]->card_ids, s->options.opt_rand_crypt); this->deck_state = make_shared<DeckState>(this->client_id, s->deck_entries[client_id]->card_ids, s);
if (s->map_and_rules->rules.disable_deck_shuffle) { if (s->map_and_rules->rules.disable_deck_shuffle) {
this->deck_state->disable_shuffle(); this->deck_state->disable_shuffle();
} }
@@ -224,8 +224,7 @@ void PlayerState::apply_assist_card_effect_on_set(
size_t log_index; size_t log_index;
for (log_index = 0; log_index < 0x10; log_index++) { for (log_index = 0; log_index < 0x10; log_index++) {
auto ce = s->definition_for_card_ref( auto ce = s->definition_for_card_ref(this->discard_log_card_refs[log_index]);
this->discard_log_card_refs[log_index]);
if (ce && ((ce->def.type == CardType::ITEM || ce->def.type == CardType::CREATURE))) { if (ce && ((ce->def.type == CardType::ITEM || ce->def.type == CardType::CREATURE))) {
break; break;
} }
@@ -1004,7 +1003,7 @@ bool PlayerState::move_card_to_location_by_card_index(size_t card_index, const L
this->send_6xB4x04_if_needed(); this->send_6xB4x04_if_needed();
s->send_6xB4x05(); s->send_6xB4x05();
s->send_6xB4x39(); s->send_6xB4x39();
s->card_special->unknown_80244AA8(card); s->card_special->apply_effects_after_card_move(card);
return true; return true;
} }
@@ -1416,7 +1415,7 @@ bool PlayerState::set_card_from_hand(
s->send_6xB4x05(); s->send_6xB4x05();
if (!is_nte) { if (!is_nte) {
G_Unknown_Ep3_6xB4x4A cmd; G_AddToSetCardlog_Ep3_6xB4x4A cmd;
cmd.card_refs.clear(0xFFFF); cmd.card_refs.clear(0xFFFF);
cmd.card_refs[0] = card_ref; cmd.card_refs[0] = card_ref;
cmd.client_id = this->client_id; cmd.client_id = this->client_id;
@@ -1783,7 +1782,7 @@ bool PlayerState::set_action_cards_for_action_state(const ActionState& pa) {
card->loc.direction = pa.facing_direction; card->loc.direction = pa.facing_direction;
log.debug("set facing direction to %s", name_for_enum(card->loc.direction)); log.debug("set facing direction to %s", name_for_enum(card->loc.direction));
G_Unknown_Ep3_6xB4x4A cmd; G_AddToSetCardlog_Ep3_6xB4x4A cmd;
cmd.card_refs.clear(0xFFFF); cmd.card_refs.clear(0xFFFF);
cmd.client_id = this->client_id; cmd.client_id = this->client_id;
cmd.round_num = s->get_round_num(); cmd.round_num = s->get_round_num();
@@ -1843,7 +1842,7 @@ bool PlayerState::set_action_cards_for_action_state(const ActionState& pa) {
} }
} }
if (!is_nte) { if (!is_nte) {
G_Unknown_Ep3_6xB4x4A cmd; G_AddToSetCardlog_Ep3_6xB4x4A cmd;
cmd.card_refs.clear(0xFFFF); cmd.card_refs.clear(0xFFFF);
cmd.client_id = this->client_id; cmd.client_id = this->client_id;
cmd.round_num = s->get_round_num(); cmd.round_num = s->get_round_num();
+11 -11
View File
@@ -36,7 +36,7 @@ struct Condition {
void clear_FF(); void clear_FF();
std::string str(std::shared_ptr<const Server> s) const; std::string str(std::shared_ptr<const Server> s) const;
} __attribute__((packed)); } __packed_ws__(Condition, 0x10);
struct EffectResult { struct EffectResult {
/* 00 */ le_uint16_t attacker_card_ref; /* 00 */ le_uint16_t attacker_card_ref;
@@ -58,7 +58,7 @@ struct EffectResult {
void clear(); void clear();
std::string str(std::shared_ptr<const Server> s) const; std::string str(std::shared_ptr<const Server> s) const;
} __attribute__((packed)); } __packed_ws__(EffectResult, 0x0C);
struct CardShortStatus { struct CardShortStatus {
/* 00 */ le_uint16_t card_ref; /* 00 */ le_uint16_t card_ref;
@@ -78,7 +78,7 @@ struct CardShortStatus {
void clear_FF(); void clear_FF();
std::string str(std::shared_ptr<const Server> s) const; std::string str(std::shared_ptr<const Server> s) const;
} __attribute__((packed)); } __packed_ws__(CardShortStatus, 0x10);
struct ActionState { struct ActionState {
/* 00 */ le_uint16_t client_id; /* 00 */ le_uint16_t client_id;
@@ -99,7 +99,7 @@ struct ActionState {
void clear(); void clear();
std::string str(std::shared_ptr<const Server> s) const; std::string str(std::shared_ptr<const Server> s) const;
} __attribute__((packed)); } __packed_ws__(ActionState, 0x64);
struct ActionChain { struct ActionChain {
// Note: Episode 3 Trial Edition has a different format for this structure. // Note: Episode 3 Trial Edition has a different format for this structure.
@@ -135,7 +135,7 @@ struct ActionChain {
void clear_FF(); void clear_FF();
std::string str(std::shared_ptr<const Server> s) const; std::string str(std::shared_ptr<const Server> s) const;
} __attribute__((packed)); } __packed_ws__(ActionChain, 0x70);
struct ActionChainWithConds { struct ActionChainWithConds {
/* 0000 */ ActionChain chain; /* 0000 */ ActionChain chain;
@@ -173,7 +173,7 @@ struct ActionChainWithConds {
uint8_t get_adjusted_move_ability_nte(uint8_t ability) const; uint8_t get_adjusted_move_ability_nte(uint8_t ability) const;
std::string str(std::shared_ptr<const Server> s) const; std::string str(std::shared_ptr<const Server> s) const;
} __attribute__((packed)); } __packed_ws__(ActionChainWithConds, 0x100);
struct ActionChainWithCondsTrial { struct ActionChainWithCondsTrial {
/* 0000 */ int8_t effective_ap; /* 0000 */ int8_t effective_ap;
@@ -205,7 +205,7 @@ struct ActionChainWithCondsTrial {
ActionChainWithCondsTrial() = default; ActionChainWithCondsTrial() = default;
ActionChainWithCondsTrial(const ActionChainWithConds& src); ActionChainWithCondsTrial(const ActionChainWithConds& src);
operator ActionChainWithConds() const; operator ActionChainWithConds() const;
} __attribute__((packed)); } __packed_ws__(ActionChainWithCondsTrial, 0x100);
struct ActionMetadata { struct ActionMetadata {
/* 00 */ le_uint16_t card_ref; /* 00 */ le_uint16_t card_ref;
@@ -241,7 +241,7 @@ struct ActionMetadata {
uint16_t original_attacker_card_ref); uint16_t original_attacker_card_ref);
std::string str(std::shared_ptr<const Server> s) const; std::string str(std::shared_ptr<const Server> s) const;
} __attribute__((packed)); } __packed_ws__(ActionMetadata, 0x74);
struct HandAndEquipState { struct HandAndEquipState {
/* 00 */ parray<uint8_t, 2> dice_results; /* 00 */ parray<uint8_t, 2> dice_results;
@@ -276,7 +276,7 @@ struct HandAndEquipState {
void clear_FF(); void clear_FF();
std::string str(std::shared_ptr<const Server> s) const; std::string str(std::shared_ptr<const Server> s) const;
} __attribute__((packed)); } __packed_ws__(HandAndEquipState, 0x54);
struct PlayerBattleStats { struct PlayerBattleStats {
/* 00 */ le_uint16_t damage_given; /* 00 */ le_uint16_t damage_given;
@@ -310,7 +310,7 @@ struct PlayerBattleStats {
static uint8_t rank_for_score(float score); static uint8_t rank_for_score(float score);
static const char* name_for_rank(uint8_t rank); static const char* name_for_rank(uint8_t rank);
} __attribute__((packed)); } __packed_ws__(PlayerBattleStats, 0x28);
struct PlayerBattleStatsTrial { struct PlayerBattleStatsTrial {
/* 00 */ le_uint32_t damage_given = 0; /* 00 */ le_uint32_t damage_given = 0;
@@ -323,7 +323,7 @@ struct PlayerBattleStatsTrial {
PlayerBattleStatsTrial() = default; PlayerBattleStatsTrial() = default;
PlayerBattleStatsTrial(const PlayerBattleStats& data); PlayerBattleStatsTrial(const PlayerBattleStats& data);
operator PlayerBattleStats() const; operator PlayerBattleStats() const;
} __attribute__((packed)); } __packed_ws__(PlayerBattleStatsTrial, 0x14);
std::vector<uint16_t> get_card_refs_within_range( std::vector<uint16_t> get_card_refs_within_range(
const parray<uint8_t, 9 * 9>& range, const parray<uint8_t, 9 * 9>& range,
+32 -4
View File
@@ -2,6 +2,7 @@
#include <optional> #include <optional>
#include "DataIndexes.hh"
#include "Server.hh" #include "Server.hh"
using namespace std; using namespace std;
@@ -1469,36 +1470,52 @@ bool RulerServer::compute_effective_range_and_target_mode_for_attack(
uint16_t* out_effective_card_id, uint16_t* out_effective_card_id,
TargetMode* out_effective_target_mode, TargetMode* out_effective_target_mode,
uint16_t* out_orig_card_ref) const { uint16_t* out_orig_card_ref) const {
auto s = this->server();
bool is_nte = s->options.is_nte();
auto log = s->log_stack("compute_effective_range_and_target_mode_for_attack: ");
size_t z; size_t z;
for (z = 0; (z < 8) && (pa.action_card_refs[z] != 0xFFFF); z++) { for (z = 0; (z < 8) && (pa.action_card_refs[z] != 0xFFFF); z++) {
} }
if (z >= 8) { if (z >= 8) {
log.debug("too many action card refs");
return false; return false;
} }
log.debug("%zu action card refs", z);
uint16_t card_ref = (z == 0) ? pa.attacker_card_ref : pa.action_card_refs[z - 1]; uint16_t card_ref = (z == 0) ? pa.attacker_card_ref : pa.action_card_refs[z - 1];
log.debug("base card ref = @%04hX", card_ref);
uint16_t card_id = this->card_id_for_card_ref(card_ref); uint16_t card_id = this->card_id_for_card_ref(card_ref);
if (card_id == 0xFFFF) { if (card_id == 0xFFFF) {
log.debug("card ref is broken");
return false; return false;
} }
auto ce = this->definition_for_card_id(card_id); auto ce = this->definition_for_card_id(card_id);
uint8_t client_id = client_id_for_card_ref(pa.attacker_card_ref); uint8_t client_id = client_id_for_card_ref(pa.attacker_card_ref);
if ((client_id == 0xFF) || !ce) { if ((client_id == 0xFF) || !ce) {
log.debug("card ref is broken or definition is missing");
return false; return false;
} }
if (out_orig_card_ref) { if (out_orig_card_ref) {
log.debug("orig_card_ref = @%04hX", card_ref);
*out_orig_card_ref = card_ref; *out_orig_card_ref = card_ref;
} }
auto target_mode = ce->def.target_mode; auto target_mode = ce->def.target_mode;
if (this->card_ref_or_sc_has_fixed_range(pa.attacker_card_ref)) { 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));
card_id = this->card_id_for_card_ref(pa.attacker_card_ref); card_id = this->card_id_for_card_ref(pa.attacker_card_ref);
if (!this->server()->options.is_nte()) { if (!is_nte) {
auto sc_ce = this->definition_for_card_id(card_id); auto sc_ce = this->definition_for_card_id(card_id);
if (sc_ce && (static_cast<uint8_t>(target_mode) < 6)) { if (sc_ce && (static_cast<uint8_t>(target_mode) < 6)) {
target_mode = sc_ce->def.target_mode; 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));
} }
} }
} }
@@ -1508,8 +1525,10 @@ bool RulerServer::compute_effective_range_and_target_mode_for_attack(
auto assist_effect = this->assist_server->get_active_assist_by_index(z); auto assist_effect = this->assist_server->get_active_assist_by_index(z);
if (assist_effect == AssistEffect::SIMPLE) { if (assist_effect == AssistEffect::SIMPLE) {
card_id = this->card_id_for_card_ref(pa.attacker_card_ref); card_id = this->card_id_for_card_ref(pa.attacker_card_ref);
log.debug("SIMPLE assist overrides card id with #%04hX", card_id);
} else if (assist_effect == AssistEffect::HEAVY_FOG) { } else if (assist_effect == AssistEffect::HEAVY_FOG) {
card_id = 0xFFFE; card_id = 0xFFFE;
log.debug("HEAVY_FOG assist overrides card id with #%04hX", card_id);
} }
} }
@@ -2040,21 +2059,27 @@ shared_ptr<const CardIndex::CardEntry> RulerServer::definition_for_card_id(uint3
uint32_t RulerServer::get_card_id_with_effective_range( uint32_t RulerServer::get_card_id_with_effective_range(
uint16_t card_ref, uint16_t card_id_override, TargetMode* out_target_mode) const { uint16_t card_ref, uint16_t card_id_override, TargetMode* out_target_mode) const {
auto log = this->server()->log_stack(string_printf("get_card_id_with_effective_range(@%04hX, #%04hX): ", card_ref, card_id_override));
uint16_t card_id = (card_id_override == 0xFFFF) uint16_t card_id = (card_id_override == 0xFFFF)
? this->card_id_for_card_ref(card_ref) ? this->card_id_for_card_ref(card_ref)
: card_id_override; : card_id_override;
log.debug("card_id=#%04hX", card_id);
if (card_id != 0xFFFF) { if (card_id != 0xFFFF) {
auto ce = this->definition_for_card_id(card_id); auto ce = this->definition_for_card_id(card_id);
uint8_t client_id = client_id_for_card_ref(card_ref); uint8_t client_id = client_id_for_card_ref(card_ref);
if ((client_id != 0xFF) && ce) { if ((client_id != 0xFF) && ce) {
TargetMode effective_target_mode = ce->def.target_mode; 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));
if (this->card_ref_or_sc_has_fixed_range(card_ref)) { if (this->card_ref_or_sc_has_fixed_range(card_ref)) {
// Undo the override that may have been passed in // Undo the override that may have been passed in
auto ce = this->definition_for_card_id(this->card_id_for_card_ref(card_ref)); log.debug("@%04hX has FIXED_RANGE", card_ref);
if (ce && (static_cast<uint8_t>(effective_target_mode) < 6)) { auto orig_ce = this->definition_for_card_id(this->card_id_for_card_ref(card_ref));
effective_target_mode = ce->def.target_mode; 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));
effective_target_mode = orig_ce->def.target_mode;
} }
} }
@@ -2063,14 +2088,17 @@ uint32_t RulerServer::get_card_id_with_effective_range(
auto eff = this->assist_server->get_active_assist_by_index(z); auto eff = this->assist_server->get_active_assist_by_index(z);
if (eff == AssistEffect::SIMPLE) { if (eff == AssistEffect::SIMPLE) {
card_id = this->card_id_for_card_ref(card_ref); card_id = this->card_id_for_card_ref(card_ref);
log.debug("SIMPLE assist effect is active; using #%04hX for range", card_id);
} else if (eff == AssistEffect::HEAVY_FOG) { } else if (eff == AssistEffect::HEAVY_FOG) {
card_id = 0xFFFE; card_id = 0xFFFE;
log.debug("HEAVY_FOG assist effect is active; limiting range to one tile in front");
} }
} }
if (out_target_mode) { if (out_target_mode) {
*out_target_mode = effective_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));
} }
} }
+48 -35
View File
@@ -4,6 +4,7 @@
#include <phosg/Time.hh> #include <phosg/Time.hh>
#include "../Loggers.hh" #include "../Loggers.hh"
#include "../Revision.hh"
#include "../SendCommands.hh" #include "../SendCommands.hh"
using namespace std; using namespace std;
@@ -29,6 +30,7 @@ void Server::PresenceEntry::clear() {
Server::Server(shared_ptr<Lobby> lobby, Options&& options) Server::Server(shared_ptr<Lobby> lobby, Options&& options)
: lobby(lobby), : lobby(lobby),
battle_record(lobby ? lobby->battle_record : nullptr),
has_lobby(lobby != nullptr), has_lobby(lobby != nullptr),
options(std::move(options)), options(std::move(options)),
last_chosen_map(this->options.tournament ? this->options.tournament->get_map() : nullptr), last_chosen_map(this->options.tournament ? this->options.tournament->get_map() : nullptr),
@@ -99,10 +101,10 @@ void Server::init() {
this->card_special = make_shared<CardSpecial>(this->shared_from_this()); this->card_special = make_shared<CardSpecial>(this->shared_from_this());
// Note: The original implementation calls the default PSOV2Encryption // Note: The original implementation calls the default PSOV2Encryption
// constructor for opt_rand_crypt, which just uses 0 as the seed. It then // 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 // re-seeds the generator later. We instead expect the caller to provide a
// seeded generator, and we don't re-seed it at all. // seeded generator, and we don't re-seed it at all.
// this->opt_rand_crypt = make_shared<PSOV2Encryption>(0); // this->random_crypt = make_shared<PSOV2Encryption>(0);
this->state_flags = make_shared<StateFlags>(); this->state_flags = make_shared<StateFlags>();
@@ -252,13 +254,13 @@ void Server::send(const void* data, size_t size, uint8_t command, bool enable_ma
for (auto watcher_l : l->watcher_lobbies) { for (auto watcher_l : l->watcher_lobbies) {
send_command_if_not_loading(watcher_l, command, 0x00, data, size); send_command_if_not_loading(watcher_l, command, 0x00, data, size);
} }
if (l->battle_record && l->battle_record->writable()) { if (this->battle_record && this->battle_record->writable()) {
l->battle_record->add_command(BattleRecord::Event::Type::BATTLE_COMMAND, data, size); this->battle_record->add_command(BattleRecord::Event::Type::BATTLE_COMMAND, data, size);
} }
} else if ((this->options.behavior_flags & BehaviorFlag::LOG_COMMANDS_IF_LOBBY_MISSING) && } else if ((this->options.behavior_flags & BehaviorFlag::LOG_COMMANDS_IF_LOBBY_MISSING) &&
this->log().info("Generated command")) { this->log().info("Generated command")) {
print_data(stderr, data, size); print_data(stderr, data, size, 0, nullptr, PrintDataFlags::PRINT_ASCII | PrintDataFlags::DISABLE_COLOR | PrintDataFlags::OFFSET_16_BITS);
} }
} }
@@ -271,19 +273,8 @@ void Server::send_6xB4x46() const {
G_ServerVersionStrings_Ep3_6xB4x46 cmd; G_ServerVersionStrings_Ep3_6xB4x46 cmd;
cmd.version_signature.encode(this->options.is_nte() ? VERSION_SIGNATURE_NTE : VERSION_SIGNATURE, 1); cmd.version_signature.encode(this->options.is_nte() ? VERSION_SIGNATURE_NTE : VERSION_SIGNATURE, 1);
cmd.date_str1.encode(format_time(this->options.card_index->definitions_mtime() * 1000000), 1); cmd.date_str1.encode(format_time(this->options.card_index->definitions_mtime() * 1000000), 1);
string date_str2; string build_date = format_time(BUILD_TIMESTAMP);
if (this->options.opt_rand_crypt) { cmd.date_str2.encode(string_printf("newserv %s compiled at %s", GIT_REVISION_HASH, build_date.c_str()), 1);
date_str2 = string_printf(
"Random:%08" PRIX32 "+%08" PRIX32,
this->options.opt_rand_crypt->seed(),
this->options.opt_rand_crypt->absolute_offset());
} else {
date_str2 = "Random:<SYS>";
}
if (this->last_chosen_map) {
date_str2 += string_printf(" Map:%08" PRIX32, this->last_chosen_map->map_number);
}
cmd.date_str2.encode(date_str2, 1);
this->send(cmd); this->send(cmd);
} }
@@ -783,6 +774,9 @@ void Server::destroy_cards_with_zero_hp() {
void Server::determine_first_team_turn() { void Server::determine_first_team_turn() {
this->team_client_count[0] = this->map_and_rules->num_team0_players; this->team_client_count[0] = this->map_and_rules->num_team0_players;
this->team_client_count[1] = this->map_and_rules->num_players - this->team_client_count[0]; this->team_client_count[1] = this->map_and_rules->num_players - this->team_client_count[0];
if (this->team_client_count[0] == 0 || this->team_client_count[1] == 0) {
throw runtime_error("one or both teams have no players");
}
this->first_team_turn = 0xFF; this->first_team_turn = 0xFF;
while (this->first_team_turn == 0xFF) { while (this->first_team_turn == 0xFF) {
uint8_t results[2] = {0, 0}; uint8_t results[2] = {0, 0};
@@ -1080,16 +1074,30 @@ shared_ptr<const PlayerState> Server::get_player_state(uint8_t client_id) const
return this->player_states[client_id]; return this->player_states[client_id];
} }
uint32_t Server::get_random_raw() {
le_uint32_t ret;
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);
}
if (this->battle_record && this->battle_record->writable()) {
this->battle_record->add_random_data(&ret, sizeof(ret));
}
return ret;
}
uint32_t Server::get_random(uint32_t max) { uint32_t Server::get_random(uint32_t max) {
// The original implementation was essentially: // The original implementation was essentially:
// return (static_cast<double>(this->opt_rand_crypt->next() >> 16) / 65536.0) * max // return (static_cast<double>(this->random_source->next() >> 16) / 65536.0) * max
// This is unnecessarily complicated and imprecise, so we instead just do: // This is unnecessarily complicated and imprecise, so we instead just do:
return random_from_optional_crypt(this->options.opt_rand_crypt) % max; return this->get_random_raw() % max;
} }
float Server::get_random_float_0_1() { float Server::get_random_float_0_1() {
// This lacks some precision, but matches the original implementation. // This lacks some precision, but matches the original implementation.
return (static_cast<double>(random_from_optional_crypt(this->options.opt_rand_crypt) >> 16) / 65536.0); return (static_cast<double>(this->get_random_raw() >> 16) / 65536.0);
} }
uint32_t Server::get_round_num() const { uint32_t Server::get_round_num() const {
@@ -1454,12 +1462,12 @@ void Server::set_phase_after() {
if (ps) { if (ps) {
auto card = ps->get_sc_card(); auto card = ps->get_sc_card();
if (card) { if (card) {
this->card_special->apply_action_conditions(0x06, 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++) { for (size_t set_index = 0; set_index < 8; set_index++) {
auto card = ps->get_set_card(set_index); auto card = ps->get_set_card(set_index);
if (card) { if (card) {
this->card_special->apply_action_conditions(0x06, nullptr, card, is_nte ? 0x1F : 0x04, nullptr); this->card_special->apply_action_conditions(EffectWhen::AFTER_SET_PHASE, nullptr, card, is_nte ? 0x1F : 0x04, nullptr);
} }
} }
} }
@@ -1546,8 +1554,8 @@ void Server::setup_and_start_battle() {
this->setup_phase = SetupPhase::STARTER_ROLLS; this->setup_phase = SetupPhase::STARTER_ROLLS;
// Note: This is where original implementation re-seeds opt_rand_crypt (it // Note: This is where original implementation re-seeds its random generator
// uses time() as the seed value). // (it uses time() as the seed value).
for (size_t z = 0; z < 4; z++) { for (size_t z = 0; z < 4; z++) {
if (!this->check_presence_entry(z)) { if (!this->check_presence_entry(z)) {
@@ -1837,7 +1845,11 @@ void Server::on_server_data_input(shared_ptr<Client> sender_c, const string& dat
throw runtime_error("unknown CAx subsubcommand"); throw runtime_error("unknown CAx subsubcommand");
} }
if ((sender_c->version() == Version::GC_EP3_NTE) || !header.mask_key) { if (this->battle_record && this->battle_record->writable()) {
this->battle_record->add_command(BattleRecord::Event::Type::SERVER_DATA_COMMAND, data.data(), data.size());
}
if ((sender_c && (sender_c->version() == Version::GC_EP3_NTE)) || !header.mask_key) {
(this->*handler)(sender_c, data); (this->*handler)(sender_c, data);
} else { } else {
string unmasked_data = data; string unmasked_data = data;
@@ -2167,7 +2179,8 @@ void Server::handle_CAx13_update_map_during_setup_t(shared_ptr<Client> c, const
// in the case of NTE, no values at all, since the Rules structure is // 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 // smaller). So, use the values from the last chosen map if applicable, or
// the values from the $dicerange command if available. // the values from the $dicerange command if available.
const Rules* map_rules = this->last_chosen_map ? &this->last_chosen_map->version(c->language())->map->default_rules : nullptr; uint8_t language = c ? c->language() : 1;
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; auto& server_rules = this->map_and_rules->rules;
// NTE can specify the DEF dice value range in its Rules struct, so we use // NTE can specify the DEF dice value range in its Rules struct, so we use
// that unless the map or $dicerange overrides it. // that unless the map or $dicerange overrides it.
@@ -2347,11 +2360,12 @@ void Server::handle_CAx1D_start_battle(shared_ptr<Client>, const string& data) {
} }
if (should_start) { if (should_start) {
if (this->battle_record && this->battle_record->writable()) {
this->battle_record->set_battle_start_timestamp();
}
auto l = this->lobby.lock(); auto l = this->lobby.lock();
if (l) { if (l) {
if (l->battle_record) {
l->battle_record->set_battle_start_timestamp();
}
// Note: Sega's implementation doesn't set EX results values here; they // 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. // did it at game join time instead. We do it here for code simplicity.
if ((l->base_version != Version::GC_EP3_NTE) && l->ep3_ex_result_values) { if ((l->base_version != Version::GC_EP3_NTE) && l->ep3_ex_result_values) {
@@ -2554,8 +2568,7 @@ void Server::handle_CAx3A_time_limit_expired(shared_ptr<Client>, const string& d
void Server::handle_CAx40_map_list_request(shared_ptr<Client> sender_c, const string& data) { void Server::handle_CAx40_map_list_request(shared_ptr<Client> sender_c, const string& data) {
const auto& in_cmd = check_size_t<G_MapListRequest_Ep3_CAx40>(data); const auto& in_cmd = check_size_t<G_MapListRequest_Ep3_CAx40>(data);
this->send_debug_command_received_message( this->send_debug_command_received_message(in_cmd.header.subsubcommand, "MAP LIST");
in_cmd.header.subsubcommand, "MAP LIST");
auto l = this->lobby.lock(); auto l = this->lobby.lock();
if (!l) { if (!l) {
@@ -2609,13 +2622,13 @@ void Server::send_6xB6x41_to_all_clients() const {
} }
} }
if (l->battle_record && l->battle_record->writable()) { if (this->battle_record && this->battle_record->writable()) {
// TODO: It's not great that we just pick the first one; ideally we'd put // 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 // all of them in the recording and send the appropriate one to the client
// in the playback lobby // in the playback lobby
for (string& data : map_commands_by_language) { for (string& data : map_commands_by_language) {
if (!data.empty()) { if (!data.empty()) {
l->battle_record->add_command( this->battle_record->add_command(
BattleRecord::Event::Type::BATTLE_COMMAND, std::move(data)); BattleRecord::Event::Type::BATTLE_COMMAND, std::move(data));
break; break;
} }
@@ -2856,7 +2869,7 @@ void Server::unknown_8023EEF4() {
ActionState as = cmd.state; ActionState as = cmd.state;
this->send(cmd); this->send(cmd);
this->card_special->unknown_8024AAB8(as); this->card_special->apply_effects_after_attack_target_resolution(as);
if (!is_nte) { if (!is_nte) {
this->attack_cards[this->unknown_a14]->compute_action_chain_results(1, 0); this->attack_cards[this->unknown_a14]->compute_action_chain_results(1, 0);
+6 -1
View File
@@ -9,6 +9,7 @@
#include "../CommandFormats.hh" #include "../CommandFormats.hh"
#include "../Text.hh" #include "../Text.hh"
#include "AssistServer.hh" #include "AssistServer.hh"
#include "BattleRecord.hh"
#include "CardSpecial.hh" #include "CardSpecial.hh"
#include "MapState.hh" #include "MapState.hh"
#include "PlayerState.hh" #include "PlayerState.hh"
@@ -71,6 +72,7 @@ public:
std::shared_ptr<const CardIndex> card_index; std::shared_ptr<const CardIndex> card_index;
std::shared_ptr<const MapIndex> map_index; std::shared_ptr<const MapIndex> map_index;
uint32_t behavior_flags; uint32_t behavior_flags;
std::shared_ptr<StringReader> opt_rand_stream;
std::shared_ptr<PSOLFGEncryption> opt_rand_crypt; std::shared_ptr<PSOLFGEncryption> opt_rand_crypt;
std::shared_ptr<const Tournament> tournament; std::shared_ptr<const Tournament> tournament;
std::array<std::vector<uint16_t>, 5> trap_card_ids; std::array<std::vector<uint16_t>, 5> trap_card_ids;
@@ -191,6 +193,7 @@ public:
uint8_t get_current_team_turn() const; uint8_t get_current_team_turn() const;
std::shared_ptr<PlayerState> get_player_state(uint8_t client_id); std::shared_ptr<PlayerState> get_player_state(uint8_t client_id);
std::shared_ptr<const PlayerState> get_player_state(uint8_t client_id) const; std::shared_ptr<const PlayerState> get_player_state(uint8_t client_id) const;
uint32_t get_random_raw();
uint32_t get_random(uint32_t max); uint32_t get_random(uint32_t max);
float get_random_float_0_1(); float get_random_float_0_1();
uint32_t get_round_num() const; uint32_t get_round_num() const;
@@ -273,6 +276,7 @@ private:
public: public:
// These fields are not part of the original implementation // These fields are not part of the original implementation
std::weak_ptr<Lobby> lobby; std::weak_ptr<Lobby> lobby;
std::shared_ptr<BattleRecord> battle_record;
bool has_lobby; bool has_lobby;
Options options; Options options;
std::shared_ptr<const MapIndex::Map> last_chosen_map; std::shared_ptr<const MapIndex::Map> last_chosen_map;
@@ -290,7 +294,8 @@ public:
uint8_t is_cpu_player; uint8_t is_cpu_player;
PresenceEntry(); PresenceEntry();
void clear(); void clear();
} __attribute__((packed)); } __packed_ws__(PresenceEntry, 3);
std::shared_ptr<MapAndRulesState> map_and_rules; std::shared_ptr<MapAndRulesState> map_and_rules;
bcarray<std::shared_ptr<DeckEntry>, 4> deck_entries; bcarray<std::shared_ptr<DeckEntry>, 4> deck_entries;
parray<PresenceEntry, 4> presence_entries; parray<PresenceEntry, 4> presence_entries;
+31 -31
View File
@@ -9,18 +9,18 @@ using namespace std;
namespace Episode3 { namespace Episode3 {
Tournament::PlayerEntry::PlayerEntry(uint32_t serial_number, const string& player_name) Tournament::PlayerEntry::PlayerEntry(uint32_t account_id, const string& player_name)
: serial_number(serial_number), : account_id(account_id),
player_name(player_name) {} player_name(player_name) {}
Tournament::PlayerEntry::PlayerEntry(shared_ptr<Client> c) Tournament::PlayerEntry::PlayerEntry(shared_ptr<Client> c)
: serial_number(c->license->serial_number), : account_id(c->login->account->account_id),
client(c), client(c),
player_name(c->character()->disp.name.decode(c->language())) {} player_name(c->character()->disp.name.decode(c->language())) {}
Tournament::PlayerEntry::PlayerEntry( Tournament::PlayerEntry::PlayerEntry(
shared_ptr<const COMDeckDefinition> com_deck) shared_ptr<const COMDeckDefinition> com_deck)
: serial_number(0), : account_id(0),
com_deck(com_deck) {} com_deck(com_deck) {}
bool Tournament::PlayerEntry::is_com() const { bool Tournament::PlayerEntry::is_com() const {
@@ -28,7 +28,7 @@ bool Tournament::PlayerEntry::is_com() const {
} }
bool Tournament::PlayerEntry::is_human() const { bool Tournament::PlayerEntry::is_human() const {
return (this->serial_number != 0); return (this->account_id != 0);
} }
Tournament::Team::Team( Tournament::Team::Team(
@@ -56,9 +56,9 @@ string Tournament::Team::str() const {
for (const auto& player : this->players) { for (const auto& player : this->players) {
if (player.is_human()) { if (player.is_human()) {
if (player.player_name.empty()) { if (player.player_name.empty()) {
ret += string_printf(" %08" PRIX32, player.serial_number); ret += string_printf(" %08" PRIX32, player.account_id);
} else { } else {
ret += string_printf(" %08" PRIX32 " (%s)", player.serial_number, player.player_name.c_str()); ret += string_printf(" %08" PRIX32 " (%s)", player.account_id, player.player_name.c_str());
} }
} }
} }
@@ -81,12 +81,12 @@ void Tournament::Team::register_player(
if (!tournament) { if (!tournament) {
throw runtime_error("tournament has been deleted"); throw runtime_error("tournament has been deleted");
} }
if (!tournament->all_player_serial_numbers.emplace(c->license->serial_number).second) { if (!tournament->all_player_account_ids.emplace(c->login->account->account_id).second) {
throw runtime_error("player already registered in same tournament"); throw runtime_error("player already registered in same tournament");
} }
for (const auto& player : this->players) { for (const auto& player : this->players) {
if (player.is_human() && (player.serial_number == c->license->serial_number)) { if (player.is_human() && (player.account_id == c->login->account->account_id)) {
throw logic_error("player already registered in team but not in tournament"); throw logic_error("player already registered in team but not in tournament");
} }
} }
@@ -99,11 +99,11 @@ void Tournament::Team::register_player(
} }
} }
bool Tournament::Team::unregister_player(uint32_t serial_number) { bool Tournament::Team::unregister_player(uint32_t account_id) {
size_t index; size_t index;
for (index = 0; index < this->players.size(); index++) { for (index = 0; index < this->players.size(); index++) {
if (this->players[index].is_human() && if (this->players[index].is_human() &&
(this->players[index].serial_number == serial_number)) { (this->players[index].account_id == account_id)) {
break; break;
} }
} }
@@ -143,7 +143,7 @@ bool Tournament::Team::unregister_player(uint32_t serial_number) {
// If the tournament has not started yet, just remove the player from the // If the tournament has not started yet, just remove the player from the
// team // team
} else { } else {
if (!tournament->all_player_serial_numbers.erase(serial_number)) { if (!tournament->all_player_account_ids.erase(account_id)) {
throw logic_error("player removed from team but not from tournament"); throw logic_error("player removed from team but not from tournament");
} }
} }
@@ -371,13 +371,13 @@ void Tournament::init() {
team_index_to_rounds_cleared.emplace_back(team_json->get_int("num_rounds_cleared")); team_index_to_rounds_cleared.emplace_back(team_json->get_int("num_rounds_cleared"));
for (const auto& player_json : team_json->get_list("player_specs")) { for (const auto& player_json : team_json->get_list("player_specs")) {
if (player_json->is_list()) { if (player_json->is_list()) {
uint32_t serial_number = player_json->at(0).as_int(); uint32_t account_id = player_json->at(0).as_int();
team->players.emplace_back(serial_number, player_json->at(1).as_string()); team->players.emplace_back(account_id, player_json->at(1).as_string());
this->all_player_serial_numbers.emplace(serial_number); this->all_player_account_ids.emplace(account_id);
} else if (player_json->is_int()) { } else if (player_json->is_int()) {
uint32_t serial_number = player_json->as_int(); uint32_t account_id = player_json->as_int();
team->players.emplace_back(serial_number); team->players.emplace_back(account_id);
this->all_player_serial_numbers.emplace(serial_number); this->all_player_account_ids.emplace(account_id);
} else if (player_json->is_string()) { } else if (player_json->is_string()) {
team->players.emplace_back(this->com_deck_index->deck_for_name(player_json->as_string())); team->players.emplace_back(this->com_deck_index->deck_for_name(player_json->as_string()));
} else { } else {
@@ -511,9 +511,9 @@ JSON Tournament::json() const {
for (const auto& player : team->players) { for (const auto& player : team->players) {
if (player.is_human()) { if (player.is_human()) {
if (!player.player_name.empty()) { if (!player.player_name.empty()) {
players_list.emplace_back(JSON::list({player.serial_number, player.player_name})); players_list.emplace_back(JSON::list({player.account_id, player.player_name}));
} else { } else {
players_list.emplace_back(player.serial_number); players_list.emplace_back(player.account_id);
} }
} else { } else {
players_list.emplace_back(player.com_deck->deck_name); players_list.emplace_back(player.com_deck->deck_name);
@@ -571,25 +571,25 @@ shared_ptr<Tournament::Match> Tournament::get_final_match() const {
return this->final_match; return this->final_match;
} }
shared_ptr<Tournament::Team> Tournament::team_for_serial_number( shared_ptr<Tournament::Team> Tournament::team_for_account_id(
uint32_t serial_number) const { uint32_t account_id) const {
if (!this->all_player_serial_numbers.count(serial_number)) { if (!this->all_player_account_ids.count(account_id)) {
return nullptr; return nullptr;
} }
for (auto team : this->teams) { for (auto team : this->teams) {
for (const auto& player : team->players) { for (const auto& player : team->players) {
if (player.serial_number == serial_number) { if (player.account_id == account_id) {
return team->is_active ? team : nullptr; return team->is_active ? team : nullptr;
} }
} }
} }
throw logic_error("serial number registered in tournament but not in any team"); throw logic_error("account ID registered in tournament but not in any team");
} }
const set<uint32_t>& Tournament::get_all_player_serial_numbers() const { const set<uint32_t>& Tournament::get_all_player_account_ids() const {
return this->all_player_serial_numbers; return this->all_player_account_ids;
} }
void Tournament::start() { void Tournament::start() {
@@ -896,10 +896,10 @@ bool TournamentIndex::delete_tournament(const string& name) {
return true; return true;
} }
shared_ptr<Tournament::Team> TournamentIndex::team_for_serial_number(uint32_t serial_number) const { shared_ptr<Tournament::Team> TournamentIndex::team_for_account_id(uint32_t account_id) const {
for (const auto& it : this->name_to_tournament) { for (const auto& it : this->name_to_tournament) {
const auto& tourn = it.second; const auto& tourn = it.second;
auto team = tourn->team_for_serial_number(serial_number); auto team = tourn->team_for_account_id(account_id);
if (team) { if (team) {
return team; return team;
} }
@@ -912,11 +912,11 @@ void TournamentIndex::link_client(shared_ptr<Client> c) {
return; return;
} }
auto team = this->team_for_serial_number(c->license->serial_number); auto team = this->team_for_account_id(c->login->account->account_id);
auto tourn = team ? team->tournament.lock() : nullptr; auto tourn = team ? team->tournament.lock() : nullptr;
if (team && team->is_active && tourn) { if (team && team->is_active && tourn) {
for (auto& player : team->players) { for (auto& player : team->players) {
if (player.serial_number == c->license->serial_number) { if (player.account_id == c->login->account->account_id) {
c->ep3_tournament_team = team; c->ep3_tournament_team = team;
player.client = c; player.client = c;
if (c->version() == Version::GC_EP3) { if (c->version() == Version::GC_EP3) {
+9 -9
View File
@@ -33,16 +33,16 @@ public:
}; };
struct PlayerEntry { struct PlayerEntry {
// Invariant: (serial_number == 0) != (com_deck == nullptr) // Invariant: (account_id == 0) != (com_deck == nullptr)
// (that is, exactly one of the following must be valid) // (that is, exactly one of the following must be valid)
uint32_t serial_number; uint32_t account_id;
std::shared_ptr<const COMDeckDefinition> com_deck; std::shared_ptr<const COMDeckDefinition> com_deck;
// client is valid if serial_number is nonzero and the client is connected // client is valid if account_id is nonzero and the client is connected
std::weak_ptr<Client> client; std::weak_ptr<Client> client;
std::string player_name; // Not used for COM decks std::string player_name; // Not used for COM decks
explicit PlayerEntry(uint32_t serial_number, const std::string& player_name = ""); explicit PlayerEntry(uint32_t account_id, const std::string& player_name = "");
explicit PlayerEntry(std::shared_ptr<Client> c); explicit PlayerEntry(std::shared_ptr<Client> c);
explicit PlayerEntry(std::shared_ptr<const COMDeckDefinition> com_deck); explicit PlayerEntry(std::shared_ptr<const COMDeckDefinition> com_deck);
@@ -73,7 +73,7 @@ public:
std::shared_ptr<Client> c, std::shared_ptr<Client> c,
const std::string& team_name, const std::string& team_name,
const std::string& password); const std::string& password);
bool unregister_player(uint32_t serial_number); bool unregister_player(uint32_t account_id);
bool has_any_human_players() const; bool has_any_human_players() const;
size_t num_human_players() const; size_t num_human_players() const;
@@ -152,8 +152,8 @@ public:
std::shared_ptr<Team> get_winner_team() const; std::shared_ptr<Team> get_winner_team() const;
std::shared_ptr<Match> next_match_for_team(std::shared_ptr<Team> team) const; std::shared_ptr<Match> next_match_for_team(std::shared_ptr<Team> team) const;
std::shared_ptr<Match> get_final_match() const; std::shared_ptr<Match> get_final_match() const;
std::shared_ptr<Team> team_for_serial_number(uint32_t serial_number) const; std::shared_ptr<Team> team_for_account_id(uint32_t account_id) const;
const std::set<uint32_t>& get_all_player_serial_numbers() const; const std::set<uint32_t>& get_all_player_account_ids() const;
void start(); void start();
@@ -178,7 +178,7 @@ private:
State current_state; State current_state;
uint32_t menu_item_id; uint32_t menu_item_id;
std::set<uint32_t> all_player_serial_numbers; std::set<uint32_t> all_player_account_ids;
std::unordered_set<std::shared_ptr<Match>> pending_matches; std::unordered_set<std::shared_ptr<Match>> pending_matches;
// This vector contains all teams in the original starting order of the // This vector contains all teams in the original starting order of the
@@ -231,7 +231,7 @@ public:
uint8_t flags); uint8_t flags);
bool delete_tournament(const std::string& name); bool delete_tournament(const std::string& name);
std::shared_ptr<Tournament::Team> team_for_serial_number(uint32_t serial_number) const; std::shared_ptr<Tournament::Team> team_for_account_id(uint32_t account_id) const;
void link_client(std::shared_ptr<Client> c); void link_client(std::shared_ptr<Client> c);
void link_all_clients(std::shared_ptr<ServerState> s); void link_all_clients(std::shared_ptr<ServerState> s);
+4
View File
@@ -9,8 +9,12 @@
#include <optional> #include <optional>
#include <stdexcept> #include <stdexcept>
// 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); 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> template <typename T>
T call_on_event_thread(std::shared_ptr<struct event_base> base, std::function<T()>&& compute) { T call_on_event_thread(std::shared_ptr<struct event_base> base, std::function<T()>&& compute) {
std::optional<T> ret; std::optional<T> ret;
+74 -18
View File
@@ -10,6 +10,7 @@
#ifdef HAVE_RESOURCE_FILE #ifdef HAVE_RESOURCE_FILE
#include <resource_file/Emulators/PPC32Emulator.hh> #include <resource_file/Emulators/PPC32Emulator.hh>
#include <resource_file/Emulators/SH4Emulator.hh>
#include <resource_file/Emulators/X86Emulator.hh> #include <resource_file/Emulators/X86Emulator.hh>
#endif #endif
@@ -39,6 +40,8 @@ const char* name_for_architecture(CompiledFunctionCode::Architecture arch) {
return "PowerPC"; return "PowerPC";
case CompiledFunctionCode::Architecture::X86: case CompiledFunctionCode::Architecture::X86:
return "x86"; return "x86";
case CompiledFunctionCode::Architecture::SH4:
return "SH-4";
default: default:
throw logic_error("invalid architecture"); throw logic_error("invalid architecture");
} }
@@ -47,7 +50,8 @@ const char* name_for_architecture(CompiledFunctionCode::Architecture arch) {
template <typename FooterT> template <typename FooterT>
string CompiledFunctionCode::generate_client_command_t( string CompiledFunctionCode::generate_client_command_t(
const unordered_map<string, uint32_t>& label_writes, const unordered_map<string, uint32_t>& label_writes,
const string& suffix, const void* suffix_data,
size_t suffix_size,
uint32_t override_relocations_offset) const { uint32_t override_relocations_offset) const {
FooterT footer; FooterT footer;
footer.num_relocations = this->relocation_deltas.size(); footer.num_relocations = this->relocation_deltas.size();
@@ -69,7 +73,9 @@ string CompiledFunctionCode::generate_client_command_t(
} else { } else {
w.write(this->code); w.write(this->code);
} }
w.write(suffix); if (suffix_size) {
w.write(suffix_data, suffix_size);
}
while (w.size() & 3) { while (w.size() & 3) {
w.put_u8(0); w.put_u8(0);
} }
@@ -98,14 +104,15 @@ string CompiledFunctionCode::generate_client_command_t(
string CompiledFunctionCode::generate_client_command( string CompiledFunctionCode::generate_client_command(
const unordered_map<string, uint32_t>& label_writes, const unordered_map<string, uint32_t>& label_writes,
const string& suffix, const void* suffix_data,
size_t suffix_size,
uint32_t override_relocations_offset) const { uint32_t override_relocations_offset) const {
if (this->arch == Architecture::POWERPC) { if (this->arch == Architecture::POWERPC) {
return this->generate_client_command_t<S_ExecuteCode_Footer_GC_B2>( return this->generate_client_command_t<S_ExecuteCode_Footer_GC_B2>(
label_writes, suffix, override_relocations_offset); label_writes, suffix_data, suffix_size, override_relocations_offset);
} else if ((this->arch == Architecture::X86) || (this->arch == Architecture::SH4)) { } else if ((this->arch == Architecture::X86) || (this->arch == Architecture::SH4)) {
return this->generate_client_command_t<S_ExecuteCode_Footer_DC_PC_XB_BB_B2>( return this->generate_client_command_t<S_ExecuteCode_Footer_DC_PC_XB_BB_B2>(
label_writes, suffix, override_relocations_offset); label_writes, suffix_data, suffix_size, override_relocations_offset);
} else { } else {
throw logic_error("invalid architecture"); throw logic_error("invalid architecture");
} }
@@ -117,12 +124,14 @@ bool CompiledFunctionCode::is_big_endian() const {
shared_ptr<CompiledFunctionCode> compile_function_code( shared_ptr<CompiledFunctionCode> compile_function_code(
CompiledFunctionCode::Architecture arch, CompiledFunctionCode::Architecture arch,
const string& directory, const string& function_directory,
const string& system_directory,
const string& name, const string& name,
const string& text) { const string& text) {
#ifndef HAVE_RESOURCE_FILE #ifndef HAVE_RESOURCE_FILE
(void)arch; (void)arch;
(void)directory; (void)function_directory;
(void)system_directory;
(void)name; (void)name;
(void)text; (void)text;
throw runtime_error("function compiler is not available"); throw runtime_error("function compiler is not available");
@@ -151,7 +160,11 @@ shared_ptr<CompiledFunctionCode> compile_function_code(
throw runtime_error("unknown architecture"); throw runtime_error("unknown architecture");
} }
string asm_filename = string_printf("%s/%s.%s.inc.s", directory.c_str(), name.c_str(), arch_name_token); // Look in the function directory first, then the system directory
string asm_filename = string_printf("%s/%s.%s.inc.s", function_directory.c_str(), name.c_str(), arch_name_token);
if (!isfile(asm_filename)) {
asm_filename = string_printf("%s/%s.%s.inc.s", system_directory.c_str(), name.c_str(), arch_name_token);
}
if (isfile(asm_filename)) { if (isfile(asm_filename)) {
if (!get_include_stack.emplace(name).second) { if (!get_include_stack.emplace(name).second) {
throw runtime_error("mutual recursion between includes: " + name); throw runtime_error("mutual recursion between includes: " + name);
@@ -165,14 +178,20 @@ shared_ptr<CompiledFunctionCode> compile_function_code(
ret = X86Emulator::assemble(load_file(asm_filename), get_include); ret = X86Emulator::assemble(load_file(asm_filename), get_include);
break; break;
case CompiledFunctionCode::Architecture::SH4: case CompiledFunctionCode::Architecture::SH4:
throw runtime_error("cannot compuile SH-4 assembly"); ret = SH4Emulator::assemble(load_file(asm_filename), get_include);
break;
default: default:
throw runtime_error("unknown architecture"); throw runtime_error("unknown architecture");
} }
get_include_stack.erase(name); get_include_stack.erase(name);
return ret.code; return ret.code;
} }
string bin_filename = directory + "/" + name + ".inc.bin";
string bin_filename = function_directory + "/" + name + ".inc.bin";
if (isfile(bin_filename)) {
return load_file(bin_filename);
}
bin_filename = system_directory + "/" + name + ".inc.bin";
if (isfile(bin_filename)) { if (isfile(bin_filename)) {
return load_file(bin_filename); return load_file(bin_filename);
} }
@@ -184,6 +203,10 @@ shared_ptr<CompiledFunctionCode> compile_function_code(
assembled = PPC32Emulator::assemble(text, get_include); assembled = PPC32Emulator::assemble(text, get_include);
} else if (arch == CompiledFunctionCode::Architecture::X86) { } else if (arch == CompiledFunctionCode::Architecture::X86) {
assembled = X86Emulator::assemble(text, get_include); assembled = X86Emulator::assemble(text, get_include);
} else if (arch == CompiledFunctionCode::Architecture::SH4) {
assembled = SH4Emulator::assemble(text, get_include);
} else {
throw runtime_error("invalid architecture");
} }
ret->code = std::move(assembled.code); ret->code = std::move(assembled.code);
ret->label_offsets = std::move(assembled.label_offsets); ret->label_offsets = std::move(assembled.label_offsets);
@@ -237,8 +260,17 @@ FunctionCodeIndex::FunctionCodeIndex(const string& directory) {
return; return;
} }
uint32_t next_menu_item_id = 0; string system_dir_path = ends_with(directory, "/") ? (directory + "System") : (directory + "/System");
for (const auto& filename : list_directory_sorted(directory)) {
uint32_t next_menu_item_id = 1;
for (const auto& subdir_name : list_directory_sorted(directory)) {
string subdir_path = ends_with(directory, "/") ? (directory + subdir_name) : (directory + "/" + subdir_name);
if (!isdir(subdir_path)) {
function_compiler_log.warning("Skipping %s (not a directory)", subdir_name.c_str());
continue;
}
for (const auto& filename : list_directory_sorted(subdir_path)) {
try { try {
if (!ends_with(filename, ".s")) { if (!ends_with(filename, ".s")) {
continue; continue;
@@ -272,7 +304,9 @@ FunctionCodeIndex::FunctionCodeIndex(const string& directory) {
short_name = name; short_name = name;
} else if (is_patch && (name.size() >= 5) && (name[name.size() - 5] == '.')) { } else if (is_patch && (name.size() >= 5) && (name[name.size() - 5] == '.')) {
specific_version = (name[name.size() - 4] << 24) | (name[name.size() - 3] << 16) | (name[name.size() - 2] << 8) | name[name.size() - 1]; specific_version = (name[name.size() - 4] << 24) | (name[name.size() - 3] << 16) | (name[name.size() - 2] << 8) | name[name.size() - 1];
if (specific_version_is_gc(specific_version)) { if (specific_version_is_dc(specific_version)) {
arch = CompiledFunctionCode::Architecture::SH4;
} else if (specific_version_is_gc(specific_version)) {
arch = CompiledFunctionCode::Architecture::POWERPC; arch = CompiledFunctionCode::Architecture::POWERPC;
} else if (specific_version_is_xb(specific_version) || specific_version_is_bb(specific_version)) { } else if (specific_version_is_xb(specific_version) || specific_version_is_bb(specific_version)) {
arch = CompiledFunctionCode::Architecture::X86; arch = CompiledFunctionCode::Architecture::X86;
@@ -286,9 +320,9 @@ FunctionCodeIndex::FunctionCodeIndex(const string& directory) {
throw runtime_error("unable to determine architecture"); throw runtime_error("unable to determine architecture");
} }
string path = directory + "/" + filename; string path = subdir_path + "/" + filename;
string text = load_file(path); string text = load_file(path);
auto code = compile_function_code(arch, directory, name, text); auto code = compile_function_code(arch, subdir_path, system_dir_path, name, text);
if (code->index != 0) { if (code->index != 0) {
if (!this->index_to_function.emplace(code->index, code).second) { if (!this->index_to_function.emplace(code->index, code).second) {
throw runtime_error(string_printf( throw runtime_error(string_printf(
@@ -316,6 +350,7 @@ FunctionCodeIndex::FunctionCodeIndex(const string& directory) {
function_compiler_log.warning("Failed to compile function %s: %s", filename.c_str(), e.what()); function_compiler_log.warning("Failed to compile function %s: %s", filename.c_str(), e.what());
} }
} }
}
} }
shared_ptr<const Menu> FunctionCodeIndex::patch_menu(uint32_t specific_version) const { shared_ptr<const Menu> FunctionCodeIndex::patch_menu(uint32_t specific_version) const {
@@ -325,20 +360,41 @@ shared_ptr<const Menu> FunctionCodeIndex::patch_menu(uint32_t specific_version)
ret->items.emplace_back(PatchesMenuItemID::GO_BACK, "Go back", "Return to the\nmain menu", 0); 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) { for (const auto& it : this->name_and_specific_version_to_patch_function) {
const auto& fn = it.second; const auto& fn = it.second;
if (!fn->hide_from_patches_menu && ends_with(it.first, suffix)) { if (fn->hide_from_patches_menu || !ends_with(it.first, suffix)) {
continue;
}
ret->items.emplace_back( ret->items.emplace_back(
fn->menu_item_id, fn->menu_item_id,
fn->long_name.empty() ? fn->short_name : fn->long_name, fn->long_name.empty() ? fn->short_name : fn->long_name,
fn->description, fn->description,
MenuItem::Flag::REQUIRES_SEND_FUNCTION_CALL); MenuItem::Flag::REQUIRES_SEND_FUNCTION_CALL);
} }
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 = string_printf("-%08" PRIX32, specific_version);
auto ret = make_shared<Menu>(MenuID::PATCH_SWITCHES, "Patch switches");
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 || !ends_with(it.first, suffix)) {
continue;
}
string name;
name.push_back(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);
} }
return ret; return ret;
} }
bool FunctionCodeIndex::patch_menu_empty(uint32_t specific_version) const { bool FunctionCodeIndex::patch_menu_empty(uint32_t specific_version) const {
uint32_t mask = specific_version_is_indeterminate(specific_version) ? 0xFF000000 : 0xFFFFFFFF;
for (const auto& it : this->menu_item_id_and_specific_version_to_patch_function) { for (const auto& it : this->menu_item_id_and_specific_version_to_patch_function) {
if ((it.first & 0xFF000000) == (specific_version & 0xFF000000)) { if ((it.first & mask) == (specific_version & mask)) {
return false; return false;
} }
} }
@@ -440,7 +496,7 @@ uint32_t specific_version_for_gc_header_checksum(uint32_t header_checksum) {
char developer_code2 = 'P'; char developer_code2 = 'P';
uint8_t disc_number = 0; uint8_t disc_number = 0;
uint8_t version_code; uint8_t version_code;
} __attribute__((packed)) data; } __packed__ data;
for (const char* game_code2 = "OS"; *game_code2; game_code2++) { for (const char* game_code2 = "OS"; *game_code2; game_code2++) {
data.game_code2 = *game_code2; data.game_code2 = *game_code2;
for (const char* region_code = "JEP"; *region_code; region_code++) { for (const char* region_code = "JEP"; *region_code; region_code++) {
+6 -2
View File
@@ -6,6 +6,7 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <unordered_set>
#include <vector> #include <vector>
#include "Menu.hh" #include "Menu.hh"
@@ -42,11 +43,13 @@ struct CompiledFunctionCode {
template <typename FooterT> template <typename FooterT>
std::string generate_client_command_t( std::string generate_client_command_t(
const std::unordered_map<std::string, uint32_t>& label_writes, const std::unordered_map<std::string, uint32_t>& label_writes,
const std::string& suffix, const void* suffix_data = nullptr,
size_t suffix_size = 0,
uint32_t override_relocations_offset = 0) const; uint32_t override_relocations_offset = 0) const;
std::string generate_client_command( std::string generate_client_command(
const std::unordered_map<std::string, uint32_t>& label_writes = {}, const std::unordered_map<std::string, uint32_t>& label_writes = {},
const std::string& suffix = "", const void* suffix_data = nullptr,
size_t suffix_size = 0,
uint32_t override_relocations_offset = 0) const; uint32_t override_relocations_offset = 0) const;
}; };
@@ -69,6 +72,7 @@ struct FunctionCodeIndex {
std::map<std::string, std::shared_ptr<CompiledFunctionCode>> name_and_specific_version_to_patch_function; 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_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;
bool patch_menu_empty(uint32_t specific_version) 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; std::shared_ptr<const CompiledFunctionCode> get_patch(const std::string& name, uint32_t specific_version) const;
+10 -5
View File
@@ -9,21 +9,26 @@
using namespace std; using namespace std;
template <bool IsBigEndian> template <bool IsBigEndian>
struct GSLHeaderEntry { struct GSLHeaderEntryT {
using U32T = typename std::conditional<IsBigEndian, be_uint32_t, le_uint32_t>::type; using U32T = typename std::conditional<IsBigEndian, be_uint32_t, le_uint32_t>::type;
pstring<TextEncoding::ASCII, 0x20> filename; pstring<TextEncoding::ASCII, 0x20> filename;
U32T offset; // In pages, so actual offset is this * 0x800 U32T offset; // In pages, so actual offset is this * 0x800
U32T size; U32T size;
uint64_t unused; uint64_t unused;
} __attribute__((packed)); } __packed__;
using GSLHeaderEntry = GSLHeaderEntryT<false>;
using GSLHeaderEntryBE = GSLHeaderEntryT<true>;
check_struct_size(GSLHeaderEntry, 0x30);
check_struct_size(GSLHeaderEntryBE, 0x30);
template <bool IsBigEndian> template <bool IsBigEndian>
void GSLArchive::load_t() { void GSLArchive::load_t() {
StringReader r(*this->data); StringReader r(*this->data);
uint64_t min_data_offset = 0xFFFFFFFFFFFFFFFF; uint64_t min_data_offset = 0xFFFFFFFFFFFFFFFF;
while (r.where() < min_data_offset) { while (r.where() < min_data_offset) {
const auto& entry = r.get<GSLHeaderEntry<IsBigEndian>>(); const auto& entry = r.get<GSLHeaderEntryT<IsBigEndian>>();
if (entry.filename.empty()) { if (entry.filename.empty()) {
break; break;
} }
@@ -85,10 +90,10 @@ string GSLArchive::generate_t(const unordered_map<string, string>& files) {
// Make sure there's enough space for a blank header entry before any file's // Make sure there's enough space for a blank header entry before any file's
// data pages begin // data pages begin
uint32_t data_start_offset = ((sizeof(GSLHeaderEntry<IsBigEndian>) * (files.size() + 1)) + 0x7FF) & (~0x7FF); uint32_t data_start_offset = ((sizeof(GSLHeaderEntryT<IsBigEndian>) * (files.size() + 1)) + 0x7FF) & (~0x7FF);
uint32_t data_offset = data_start_offset; uint32_t data_offset = data_start_offset;
for (const auto& file : files) { for (const auto& file : files) {
GSLHeaderEntry<IsBigEndian> entry; GSLHeaderEntryT<IsBigEndian> entry;
entry.filename.encode(file.first); entry.filename.encode(file.first);
entry.offset = data_offset >> 11; entry.offset = data_offset >> 11;
entry.size = file.second.size(); entry.size = file.second.size();
+29 -18
View File
@@ -11,15 +11,18 @@ using namespace std;
struct GVMFileEntry { struct GVMFileEntry {
be_uint16_t file_num; be_uint16_t file_num;
pstring<TextEncoding::ASCII, 0x1C> name; pstring<TextEncoding::ASCII, 0x1C> name;
parray<be_uint32_t, 2> unknown_a1; uint8_t format_flags; // Same as in GVRHeader
} __attribute__((packed)); GVRDataFormat data_format; // Same as in GVRHeader
be_uint16_t dimensions; // As powers of two in low nybbles (so e.g. 128x128 = 0x0055)
be_uint32_t global_index;
} __packed_ws__(GVMFileEntry, 0x26);
struct GVMFileHeader { struct GVMFileHeader {
be_uint32_t magic; // 'GVMH' be_uint32_t signature; // 'GVMH'
le_uint32_t header_size; le_uint32_t header_size;
be_uint16_t flags; be_uint16_t flags; // Specifies which fields are present in GVMFileEntries; we always use 0xF (all fields present)
be_uint16_t num_files; be_uint16_t num_files;
} __attribute__((packed)); } __packed_ws__(GVMFileHeader, 0x0C);
struct GVRHeader { struct GVRHeader {
be_uint32_t magic; // 'GVRT' be_uint32_t magic; // 'GVRT'
@@ -29,21 +32,26 @@ struct GVRHeader {
GVRDataFormat data_format; GVRDataFormat data_format;
be_uint16_t width; be_uint16_t width;
be_uint16_t height; be_uint16_t height;
} __attribute__((packed)); } __packed_ws__(GVRHeader, 0x10);
string encode_gvm(const Image& img, GVRDataFormat data_format) { string encode_gvm(const Image& img, GVRDataFormat data_format, const std::string& internal_name, uint32_t global_index) {
if (img.get_width() > 0xFFFF) { int8_t dimensions_field = -2;
throw runtime_error("image is too wide to be encoded as a GVR texture"); {
size_t h = img.get_height();
size_t w = img.get_width();
if ((h != w) || (w & (w - 1)) || (h & (h - 1))) {
throw runtime_error("image must be square and dimensions must be powers of 2");
} }
if (img.get_height() > 0xFFFF) { for (w >>= 1; w; w >>= 1, dimensions_field++) {
throw runtime_error("image is too tall to be encoded as a GVR texture");
} }
if (img.get_width() & 3) { if (dimensions_field < 1) {
throw runtime_error("image width is not a multiple of 4"); throw runtime_error("image is too small");
} }
if (img.get_height() & 3) { if (dimensions_field > 0xF) {
throw runtime_error("image height is not a multiple of 4"); throw runtime_error("image is too large");
} }
}
size_t pixel_count = img.get_width() * img.get_height(); size_t pixel_count = img.get_width() * img.get_height();
size_t pixel_bytes = 0; size_t pixel_bytes = 0;
switch (data_format) { switch (data_format) {
@@ -59,11 +67,14 @@ string encode_gvm(const Image& img, GVRDataFormat data_format) {
} }
StringWriter w; StringWriter w;
w.put<GVMFileHeader>({.magic = 0x47564D48, .header_size = 0x48, .flags = 0x010F, .num_files = 1}); w.put<GVMFileHeader>({.signature = 0x47564D48, .header_size = 0x48, .flags = 0x000F, .num_files = 1});
GVMFileEntry file_entry; GVMFileEntry file_entry;
file_entry.file_num = 0; file_entry.file_num = 0;
file_entry.name.encode("img", 1); file_entry.name.encode(internal_name, 1);
file_entry.unknown_a1.clear(0); file_entry.data_format = data_format;
file_entry.format_flags = 0;
file_entry.dimensions = (dimensions_field << 4) | dimensions_field;
file_entry.global_index = global_index;
w.put(file_entry); w.put(file_entry);
w.extend_to(0x50, 0x00); w.extend_to(0x50, 0x00);
w.put<GVRHeader>({.magic = 0x47565254, w.put<GVRHeader>({.magic = 0x47565254,
+1 -1
View File
@@ -19,7 +19,7 @@ enum class GVRDataFormat : uint8_t {
DXT1 = 0x0E, DXT1 = 0x0E,
}; };
std::string encode_gvm(const Image& img, GVRDataFormat data_format); std::string encode_gvm(const Image& img, GVRDataFormat data_format, const std::string& internal_name, uint32_t global_index);
constexpr uint16_t encode_rgb565(uint8_t r, uint8_t g, uint8_t b) { constexpr uint16_t encode_rgb565(uint8_t r, uint8_t g, uint8_t b) {
return ((r << 8) & 0xF800) | ((g << 3) & 0x07E0) | ((b >> 3) & 0x001F); return ((r << 8) & 0xF800) | ((g << 3) & 0x07E0) | ((b >> 3) & 0x001F);
+306 -29
View File
@@ -132,8 +132,7 @@ unordered_multimap<string, string> HTTPServer::parse_url_params(const string& qu
} }
value.resize(write_offset); value.resize(write_offset);
params.emplace(piecewise_construct, forward_as_tuple(it, 0, first_equals), params.emplace(piecewise_construct, forward_as_tuple(it, 0, first_equals), forward_as_tuple(value));
forward_as_tuple(value));
} else { } else {
params.emplace(it, ""); params.emplace(it, "");
} }
@@ -204,6 +203,233 @@ void HTTPServer::wait_for_stop() {
this->th.join(); this->th.join();
} }
HTTPServer::WebsocketClient::WebsocketClient(struct evhttp_connection* conn)
: conn(conn),
bev(evhttp_connection_get_bufferevent(this->conn)),
pending_opcode(0xFF),
last_communication_time(now()) {}
HTTPServer::WebsocketClient::~WebsocketClient() {
evhttp_connection_free(this->conn);
}
void HTTPServer::WebsocketClient::reset_pending_frame() {
this->pending_opcode = 0xFF;
this->pending_data.clear();
}
shared_ptr<HTTPServer::WebsocketClient> HTTPServer::enable_websockets(struct evhttp_request* req) {
if (evhttp_request_get_command(req) != EVHTTP_REQ_GET) {
return nullptr;
}
struct evkeyvalq* in_headers = evhttp_request_get_input_headers(req);
const char* connection_header = evhttp_find_header(in_headers, "Connection");
if (!connection_header || strcasecmp(connection_header, "upgrade")) {
return nullptr;
}
const char* upgrade_header = evhttp_find_header(in_headers, "Upgrade");
if (!upgrade_header || strcasecmp(upgrade_header, "websocket")) {
return nullptr;
}
const char* sec_websocket_key_header = evhttp_find_header(in_headers, "Sec-WebSocket-Key");
if (!sec_websocket_key_header) {
return nullptr;
}
// Note: it's important that we make a copy of this header's value since
// we're about to free the original
string sec_websocket_key = sec_websocket_key_header;
string sec_websocket_accept_data = sec_websocket_key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
string sec_websocket_accept = base64_encode(sha1(sec_websocket_accept_data));
// Hijack the bufferevent since it's no longer handling HTTP at all
struct evhttp_connection* conn = evhttp_request_get_connection(req);
struct bufferevent* bev = evhttp_connection_get_bufferevent(conn);
bufferevent_setcb(bev, &this->dispatch_on_websocket_read, NULL, &this->dispatch_on_websocket_error, this);
bufferevent_enable(bev, EV_READ | EV_WRITE);
// Send the HTTP reply, which enables websockets
struct evbuffer* out_buf = bufferevent_get_output(bev);
evbuffer_add_printf(out_buf, "HTTP/1.1 101 Switching Protocols\r\n\
Upgrade: websocket\r\n\
Connection: upgrade\r\n\
Sec-WebSocket-Accept: %s\r\n\
\r\n",
sec_websocket_accept.c_str());
return this->bev_to_websocket_client.emplace(bev, new WebsocketClient(conn)).first->second;
}
void HTTPServer::dispatch_on_websocket_read(struct bufferevent* bev, void* ctx) {
reinterpret_cast<HTTPServer*>(ctx)->on_websocket_read(bev);
}
void HTTPServer::dispatch_on_websocket_error(struct bufferevent* bev, short events, void* ctx) {
reinterpret_cast<HTTPServer*>(ctx)->on_websocket_error(bev, events);
}
void HTTPServer::on_websocket_read(struct bufferevent* bev) {
struct evbuffer* in_buf = bufferevent_get_input(bev);
for (;;) {
// We need at most 10 bytes to determine if there's a valid frame, or as
// little as 2
string header_data(10, '\0');
ssize_t bytes_read = evbuffer_copyout(in_buf, const_cast<char*>(header_data.data()), header_data.size());
if (bytes_read < 2) {
break; // Full header not yet available
}
// Get the payload size
bool has_mask = header_data[1] & 0x80;
size_t header_size = 2;
size_t payload_size = header_data[1] & 0x7F;
if (payload_size == 0x7F) {
if (bytes_read < 10) {
break; // Full 64-bit header not yet available
}
payload_size = bswap64(*reinterpret_cast<const uint64_t*>(&header_data[2]));
header_size = 10;
} else if (payload_size == 0x7E) {
if (bytes_read < 4) {
break; // Full 16-bit size header not yet available
}
payload_size = bswap16(*reinterpret_cast<const uint16_t*>(&header_data[2]));
header_size = 4;
}
if (evbuffer_get_length(in_buf) < header_size + payload_size) {
break; // Full message not yet available
}
// Full message is available; skip the header bytes (we already read them)
// and read the masking key if needed
evbuffer_drain(in_buf, header_size);
uint8_t mask_key[4];
if (has_mask) {
evbuffer_remove(in_buf, mask_key, 4);
}
shared_ptr<WebsocketClient> c = this->bev_to_websocket_client.at(bev);
c->last_communication_time = now();
// Read and unmask message data
string payload(payload_size, '\0');
evbuffer_remove(in_buf, const_cast<char*>(payload.data()), payload_size);
if (has_mask) {
for (size_t x = 0; x < payload_size; x++) {
payload[x] ^= mask_key[x & 3];
}
}
// If the current message is a control message, respond appropriately
// (these can be sent in the middle of fragmented messages)
uint8_t opcode = header_data[0] & 0x0F;
if (opcode & 0x08) {
if (opcode == 0x0A) {
// Ping response; ignore it
} else if (opcode == 0x08) {
// Close message
this->send_websocket_message(bev, payload, 0x08);
this->disconnect_websocket_client(bev);
} else if (opcode == 0x09) {
// Ping message
this->send_websocket_message(bev, payload, 0x0A);
} else {
// Unknown control message type
this->disconnect_websocket_client(bev);
}
break;
}
// If there's an existing pending message, the current message's opcode
// should be zero; if there's no pending message, it must not be zero
if ((c->pending_opcode != 0xFF) == (opcode != 0)) {
this->disconnect_websocket_client(bev);
break;
}
// At this point, we have read a full message; we must not break out of
// this loop in case there are further messages available.
// Save the message opcode, if present, and append the frame data
if (opcode) {
c->pending_opcode = opcode;
}
c->pending_data += payload;
// 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 (header_data[0] & 0x80) {
this->handle_websocket_message(c, c->pending_opcode, c->pending_data);
c->reset_pending_frame();
}
}
}
void HTTPServer::on_websocket_error(struct bufferevent* bev, short events) {
if (events & (BEV_EVENT_ERROR | BEV_EVENT_EOF)) {
this->disconnect_websocket_client(bev);
}
}
void HTTPServer::disconnect_websocket_client(struct bufferevent* bev) {
auto it = this->bev_to_websocket_client.find(bev);
this->handle_websocket_disconnect(it->second);
this->bev_to_websocket_client.erase(it);
}
void HTTPServer::send_websocket_message(struct bufferevent* bev,
const string& message, uint8_t opcode) {
string header;
header.push_back(0x80 | (opcode & 0x0F));
if (message.size() > 65535) {
header.push_back(0x7F);
header.resize(10);
*reinterpret_cast<uint64_t*>(const_cast<char*>(header.data() + 2)) = bswap64(message.size());
} else if (message.size() > 0x7D) {
header.push_back(0x7E);
header.resize(4);
*reinterpret_cast<uint16_t*>(const_cast<char*>(header.data() + 2)) = bswap16(message.size());
} else {
header.push_back(message.size());
}
struct evbuffer* out_buf = bufferevent_get_output(bev);
evbuffer_add(out_buf, header.data(), header.size());
evbuffer_add(out_buf, message.data(), message.size());
}
void HTTPServer::send_websocket_message(shared_ptr<WebsocketClient> c, const string& message, uint8_t opcode) {
this->send_websocket_message(c->bev, message, opcode);
}
void HTTPServer::handle_websocket_message(shared_ptr<WebsocketClient>, uint8_t, const string&) {
// Currently we just ignore any messages from the client
}
void HTTPServer::handle_websocket_disconnect(shared_ptr<WebsocketClient> c) {
this->rare_drop_subscribers.erase(c);
}
void HTTPServer::send_rare_drop_notification(shared_ptr<const JSON> message) {
forward_to_event_thread(this->base, [this, message]() -> void {
if (this->rare_drop_subscribers.empty()) {
return;
}
string serialized = message->serialize();
for (const auto& c : this->rare_drop_subscribers) {
this->send_websocket_message(c, serialized);
}
});
}
void HTTPServer::dispatch_handle_request(struct evhttp_request* req, void* ctx) { void HTTPServer::dispatch_handle_request(struct evhttp_request* req, void* ctx) {
reinterpret_cast<HTTPServer*>(ctx)->handle_request(req); reinterpret_cast<HTTPServer*>(ctx)->handle_request(req);
} }
@@ -220,6 +446,7 @@ JSON HTTPServer::generate_quest_json_st(shared_ptr<const Quest> q) {
{"Number", q->quest_number}, {"Number", q->quest_number},
{"Episode", name_for_episode(q->episode)}, {"Episode", name_for_episode(q->episode)},
{"Joinable", q->joinable}, {"Joinable", q->joinable},
{"LockStatusRegister", (q->lock_status_register >= 0) ? q->lock_status_register : JSON(nullptr)},
{"Name", q->name}, {"Name", q->name},
{"BattleRules", std::move(battle_rules_json)}, {"BattleRules", std::move(battle_rules_json)},
{"ChallengeTemplateIndex", std::move(challenge_template_index_json)}, {"ChallengeTemplateIndex", std::move(challenge_template_index_json)},
@@ -266,19 +493,53 @@ JSON HTTPServer::generate_client_config_json_st(const Client::Config& config) {
return ret; return ret;
} }
JSON HTTPServer::generate_license_json_st(shared_ptr<const License> l) { JSON HTTPServer::generate_account_json_st(shared_ptr<const Account> a) {
auto ret = JSON::dict({ auto dc_nte_licenses_json = JSON::list();
{"SerialNumber", l->serial_number}, for (const auto& it : a->dc_nte_licenses) {
{"Flags", l->flags}, dc_nte_licenses_json.emplace_back(it.first);
{"Ep3CurrentMeseta", l->ep3_current_meseta}, }
{"Ep3TotalMesetaEarned", l->ep3_total_meseta_earned}, auto dc_licenses_json = JSON::list();
{"BBTeamID", l->bb_team_id}, for (const auto& it : a->dc_licenses) {
dc_licenses_json.emplace_back(it.first);
}
auto pc_licenses_json = JSON::list();
for (const auto& it : a->pc_licenses) {
pc_licenses_json.emplace_back(it.first);
}
auto gc_licenses_json = JSON::list();
for (const auto& it : a->gc_licenses) {
gc_licenses_json.emplace_back(it.first);
}
auto xb_licenses_json = JSON::list();
for (const auto& it : a->xb_licenses) {
xb_licenses_json.emplace_back(it.first);
}
auto bb_licenses_json = JSON::list();
for (const auto& it : a->bb_licenses) {
bb_licenses_json.emplace_back(it.first);
}
auto auto_patches_json = JSON::list();
for (const auto& it : a->auto_patches_enabled) {
auto_patches_json.emplace_back(it);
}
return JSON::dict({
{"AccountID", a->account_id},
{"Flags", a->flags},
{"BanEndTime", a->ban_end_time ? a->ban_end_time : JSON(nullptr)},
{"Ep3CurrentMeseta", a->ep3_current_meseta},
{"Ep3TotalMesetaEarned", a->ep3_total_meseta_earned},
{"BBTeamID", a->bb_team_id},
{"LastPlayerName", a->last_player_name},
{"AutoReplyMessage", a->auto_reply_message},
{"IsTemporary", a->is_temporary},
{"DCNTELicenses", std::move(dc_nte_licenses_json)},
{"DCLicenses", std::move(dc_licenses_json)},
{"PCLicenses", std::move(pc_licenses_json)},
{"GCLicenses", std::move(gc_licenses_json)},
{"XBLicenses", std::move(xb_licenses_json)},
{"BBLicenses", std::move(bb_licenses_json)},
{"AutoPatchesEnabled", std::move(auto_patches_json)},
}); });
ret.emplace("BanEndTime", l->ban_end_time ? l->ban_end_time : JSON(nullptr));
ret.emplace("XBGamertag", !l->xb_gamertag.empty() ? l->xb_gamertag : JSON(nullptr));
ret.emplace("XBUserID", l->xb_user_id ? l->xb_user_id : JSON(nullptr));
ret.emplace("BBUsername", !l->bb_username.empty() ? l->bb_username : JSON(nullptr));
return ret;
}; };
JSON HTTPServer::generate_game_client_json_st(shared_ptr<const Client> c, shared_ptr<const ItemNameIndex> item_name_index) { JSON HTTPServer::generate_game_client_json_st(shared_ptr<const Client> c, shared_ptr<const ItemNameIndex> item_name_index) {
@@ -294,7 +555,7 @@ JSON HTTPServer::generate_game_client_json_st(shared_ptr<const Client> c, shared
{"LocationFloor", c->floor}, {"LocationFloor", c->floor},
{"CanChat", c->can_chat}, {"CanChat", c->can_chat},
}); });
ret.emplace("license", c->license ? HTTPServer::generate_license_json_st(c->license) : JSON(nullptr)); ret.emplace("Account", c->login ? HTTPServer::generate_account_json_st(c->login->account) : JSON(nullptr));
auto l = c->lobby.lock(); auto l = c->lobby.lock();
if (l) { if (l) {
ret.emplace("LobbyID", l->lobby_id); ret.emplace("LobbyID", l->lobby_id);
@@ -380,10 +641,10 @@ JSON HTTPServer::generate_game_client_json_st(shared_ptr<const Client> c, shared
ret.emplace("BattleDisconnectCount", p->battle_records.disconnect_count.load()); ret.emplace("BattleDisconnectCount", p->battle_records.disconnect_count.load());
if (!is_ep3(c->version())) { if (!is_ep3(c->version())) {
auto json_for_challenge_times = []<size_t Count>(const parray<ChallengeTime<false>, Count>& times) -> JSON { auto json_for_challenge_times = []<size_t Count>(const parray<ChallengeTime, Count>& times) -> JSON {
auto times_json = JSON::list(); auto times_json = JSON::list();
for (size_t z = 0; z < times.size(); z++) { for (size_t z = 0; z < times.size(); z++) {
times_json.emplace_back(times[z].load()); times_json.emplace_back(times[z].decode());
} }
return times_json; return times_json;
}; };
@@ -419,11 +680,11 @@ JSON HTTPServer::generate_game_client_json_st(shared_ptr<const Client> c, shared
ret.emplace("ChallengeGraveTeam", p->challenge_records.grave_team.decode()); ret.emplace("ChallengeGraveTeam", p->challenge_records.grave_team.decode());
ret.emplace("ChallengeGraveMessage", p->challenge_records.grave_message.decode()); ret.emplace("ChallengeGraveMessage", p->challenge_records.grave_message.decode());
ret.emplace("ChallengeAwardStateEp1OnlineFlags", p->challenge_records.ep1_online_award_state.rank_award_flags.load()); ret.emplace("ChallengeAwardStateEp1OnlineFlags", p->challenge_records.ep1_online_award_state.rank_award_flags.load());
ret.emplace("ChallengeAwardStateEp1OnlineMaxRank", p->challenge_records.ep1_online_award_state.maximum_rank.load()); ret.emplace("ChallengeAwardStateEp1OnlineMaxRank", p->challenge_records.ep1_online_award_state.maximum_rank.decode());
ret.emplace("ChallengeAwardStateEp2OnlineFlags", p->challenge_records.ep2_online_award_state.rank_award_flags.load()); ret.emplace("ChallengeAwardStateEp2OnlineFlags", p->challenge_records.ep2_online_award_state.rank_award_flags.load());
ret.emplace("ChallengeAwardStateEp2OnlineMaxRank", p->challenge_records.ep2_online_award_state.maximum_rank.load()); ret.emplace("ChallengeAwardStateEp2OnlineMaxRank", p->challenge_records.ep2_online_award_state.maximum_rank.decode());
ret.emplace("ChallengeAwardStateEp1OfflineFlags", p->challenge_records.ep1_offline_award_state.rank_award_flags.load()); ret.emplace("ChallengeAwardStateEp1OfflineFlags", p->challenge_records.ep1_offline_award_state.rank_award_flags.load());
ret.emplace("ChallengeAwardStateEp1OfflineMaxRank", p->challenge_records.ep1_offline_award_state.maximum_rank.load()); ret.emplace("ChallengeAwardStateEp1OfflineMaxRank", p->challenge_records.ep1_offline_award_state.maximum_rank.decode());
ret.emplace("ChallengeRankTitle", p->challenge_records.rank_title.decode()); ret.emplace("ChallengeRankTitle", p->challenge_records.rank_title.decode());
} }
} }
@@ -498,7 +759,7 @@ JSON HTTPServer::generate_proxy_client_json_st(shared_ptr<const ProxyServer::Lin
ret.emplace("DropMode", "proxy"); ret.emplace("DropMode", "proxy");
break; break;
} }
ret.emplace("License", ses->license ? HTTPServer::generate_license_json_st(ses->license) : JSON(nullptr)); ret.emplace("Account", ses->login ? HTTPServer::generate_account_json_st(ses->login->account) : JSON(nullptr));
return ret; return ret;
} }
@@ -532,6 +793,7 @@ JSON HTTPServer::generate_lobby_json_st(shared_ptr<const Lobby> l, shared_ptr<co
ret.emplace("Name", l->name); ret.emplace("Name", l->name);
ret.emplace("RandomSeed", l->random_seed); ret.emplace("RandomSeed", l->random_seed);
if (l->episode != Episode::EP3) { if (l->episode != Episode::EP3) {
ret.emplace("QuestSelectionInProgress", l->check_flag(Lobby::Flag::QUEST_SELECTION_IN_PROGRESS));
ret.emplace("QuestInProgress", l->check_flag(Lobby::Flag::QUEST_IN_PROGRESS)); ret.emplace("QuestInProgress", l->check_flag(Lobby::Flag::QUEST_IN_PROGRESS));
ret.emplace("JoinableQuestInProgress", l->check_flag(Lobby::Flag::JOINABLE_QUEST_IN_PROGRESS)); ret.emplace("JoinableQuestInProgress", l->check_flag(Lobby::Flag::JOINABLE_QUEST_IN_PROGRESS));
auto variations_json = JSON::list(); auto variations_json = JSON::list();
@@ -543,6 +805,7 @@ JSON HTTPServer::generate_lobby_json_st(shared_ptr<const Lobby> l, shared_ptr<co
ret.emplace("Mode", name_for_mode(l->mode)); ret.emplace("Mode", name_for_mode(l->mode));
ret.emplace("Difficulty", name_for_difficulty(l->difficulty)); ret.emplace("Difficulty", name_for_difficulty(l->difficulty));
ret.emplace("BaseEXPMultiplier", l->base_exp_multiplier); ret.emplace("BaseEXPMultiplier", l->base_exp_multiplier);
ret.emplace("EXPShareMultiplier", l->exp_share_multiplier);
ret.emplace("AllowedDropModes", l->allowed_drop_modes); ret.emplace("AllowedDropModes", l->allowed_drop_modes);
switch (l->drop_mode) { switch (l->drop_mode) {
case Lobby::DropMode::DISABLED: case Lobby::DropMode::DISABLED:
@@ -585,7 +848,7 @@ JSON HTTPServer::generate_lobby_json_st(shared_ptr<const Lobby> l, shared_ptr<co
{"LocationX", item->x}, {"LocationX", item->x},
{"LocationZ", item->z}, {"LocationZ", item->z},
{"DropNumber", item->drop_number}, {"DropNumber", item->drop_number},
{"VisibilityFlags", item->visibility_flags}, {"Flags", item->flags},
{"Data", item->data.hex()}, {"Data", item->data.hex()},
{"ItemID", item->data.id.load()}, {"ItemID", item->data.id.load()},
}); });
@@ -714,9 +977,11 @@ JSON HTTPServer::generate_game_server_clients_json() const {
JSON HTTPServer::generate_proxy_server_clients_json() const { JSON HTTPServer::generate_proxy_server_clients_json() const {
return call_on_event_thread<JSON>(this->state->base, [&]() { return call_on_event_thread<JSON>(this->state->base, [&]() {
JSON res = JSON::list(); JSON res = JSON::list();
if (this->state->proxy_server) {
for (const auto& it : this->state->proxy_server->all_sessions()) { for (const auto& it : this->state->proxy_server->all_sessions()) {
res.emplace_back(this->generate_proxy_client_json_st(it.second)); res.emplace_back(this->generate_proxy_client_json_st(it.second));
} }
}
return res; return res;
}); });
} }
@@ -741,7 +1006,7 @@ JSON HTTPServer::generate_server_info_json() const {
{"LobbyCount", lobby_count}, {"LobbyCount", lobby_count},
{"GameCount", game_count}, {"GameCount", game_count},
{"ClientCount", this->state->channel_to_client.size()}, {"ClientCount", this->state->channel_to_client.size()},
{"ProxySessionCount", this->state->proxy_server->num_sessions()}, {"ProxySessionCount", this->state->proxy_server ? this->state->proxy_server->num_sessions() : 0},
{"ServerName", this->state->name}, {"ServerName", this->state->name},
}); });
}); });
@@ -766,7 +1031,7 @@ JSON HTTPServer::generate_summary_json() const {
auto l = c->lobby.lock(); auto l = c->lobby.lock();
clients_json.emplace_back(JSON::dict({ clients_json.emplace_back(JSON::dict({
{"ID", c->id}, {"ID", c->id},
{"SerialNumber", c->license ? c->license->serial_number : JSON(nullptr)}, {"AccountID", c->login ? c->login->account->account_id : JSON(nullptr)},
{"Name", p ? p->disp.name.decode(it.second->language()) : JSON(nullptr)}, {"Name", p ? p->disp.name.decode(it.second->language()) : JSON(nullptr)},
{"Version", name_for_enum(it.second->version())}, {"Version", name_for_enum(it.second->version())},
{"Language", name_for_language_code(it.second->language())}, {"Language", name_for_language_code(it.second->language())},
@@ -778,14 +1043,16 @@ JSON HTTPServer::generate_summary_json() const {
} }
auto proxy_clients_json = JSON::list(); auto proxy_clients_json = JSON::list();
if (this->state->proxy_server) {
for (const auto& it : this->state->proxy_server->all_sessions()) { for (const auto& it : this->state->proxy_server->all_sessions()) {
proxy_clients_json.emplace_back(JSON::dict({ proxy_clients_json.emplace_back(JSON::dict({
{"SerialNumber", it.second->license ? it.second->license->serial_number : JSON(nullptr)}, {"AccountID", it.second->login ? it.second->login->account->account_id : JSON(nullptr)},
{"Name", it.second->character_name}, {"Name", it.second->character_name},
{"Version", name_for_enum(it.second->version())}, {"Version", name_for_enum(it.second->version())},
{"Language", name_for_language_code(it.second->language())}, {"Language", name_for_language_code(it.second->language())},
})); }));
} }
}
auto games_json = JSON::list(); auto games_json = JSON::list();
for (const auto& it : this->state->id_to_lobby) { for (const auto& it : this->state->id_to_lobby) {
@@ -807,6 +1074,7 @@ JSON HTTPServer::generate_summary_json() const {
game_json.emplace("MapNumber", (ep3s && ep3s->last_chosen_map) ? ep3s->last_chosen_map->map_number : JSON(nullptr)); game_json.emplace("MapNumber", (ep3s && ep3s->last_chosen_map) ? ep3s->last_chosen_map->map_number : JSON(nullptr));
game_json.emplace("Rules", (ep3s && ep3s->map_and_rules) ? ep3s->map_and_rules->rules.json() : nullptr); game_json.emplace("Rules", (ep3s && ep3s->map_and_rules) ? ep3s->map_and_rules->rules.json() : nullptr);
} else { } else {
game_json.emplace("QuestSelectionInProgress", l->check_flag(Lobby::Flag::QUEST_SELECTION_IN_PROGRESS));
game_json.emplace("QuestInProgress", l->check_flag(Lobby::Flag::QUEST_IN_PROGRESS)); game_json.emplace("QuestInProgress", l->check_flag(Lobby::Flag::QUEST_IN_PROGRESS));
game_json.emplace("JoinableQuestInProgress", l->check_flag(Lobby::Flag::JOINABLE_QUEST_IN_PROGRESS)); game_json.emplace("JoinableQuestInProgress", l->check_flag(Lobby::Flag::JOINABLE_QUEST_IN_PROGRESS));
game_json.emplace("SectionID", name_for_section_id(l->effective_section_id())); game_json.emplace("SectionID", name_for_section_id(l->effective_section_id()));
@@ -848,10 +1116,7 @@ JSON HTTPServer::generate_common_tables_json() const {
auto [set_v2, set_v3_v4] = call_on_event_thread<pair<shared_ptr<const CommonItemSet>, shared_ptr<const CommonItemSet>>>(this->state->base, [&]() { auto [set_v2, set_v3_v4] = call_on_event_thread<pair<shared_ptr<const CommonItemSet>, shared_ptr<const CommonItemSet>>>(this->state->base, [&]() {
return make_pair(this->state->common_item_set_v2, this->state->common_item_set_v3_v4); return make_pair(this->state->common_item_set_v2, this->state->common_item_set_v3_v4);
}); });
return JSON::dict({ return JSON::dict({{"v1_v2", set_v2->json()}, {"v3_v4", set_v3_v4->json()}});
{"v1_v2", set_v2->json()},
{"v3_v4", set_v3_v4->json()},
});
} }
JSON HTTPServer::generate_rare_tables_json() const { JSON HTTPServer::generate_rare_tables_json() const {
@@ -921,11 +1186,23 @@ void HTTPServer::handle_request(struct evhttp_request* req) {
"/y/proxy-clients", "/y/proxy-clients",
"/y/lobbies", "/y/lobbies",
"/y/server", "/y/server",
"/y/rare-drops/stream",
"/y/summary", "/y/summary",
"/y/all", "/y/all",
}); });
ret = make_shared<JSON>(JSON::dict({{"endpoints", std::move(endpoints_json)}})); ret = make_shared<JSON>(JSON::dict({{"endpoints", std::move(endpoints_json)}}));
} else if (uri == "/y/rare-drops/stream") {
auto c = this->enable_websockets(req);
if (!c) {
throw http_error(400, "this path requires a websocket connection");
} else {
this->rare_drop_subscribers.emplace(c);
auto version_message = JSON::dict({{"ServerType", "newserv"}});
this->send_websocket_message(c, version_message.serialize());
return;
}
} else if (uri == "/y/data/ep3-cards") { } else if (uri == "/y/data/ep3-cards") {
ret = make_shared<JSON>(this->generate_ep3_cards_json(false)); ret = make_shared<JSON>(this->generate_ep3_cards_json(false));
} else if (uri == "/y/data/ep3-cards-trial") { } else if (uri == "/y/data/ep3-cards-trial") {
+39 -1
View File
@@ -28,6 +28,8 @@ public:
void schedule_stop(); void schedule_stop();
void wait_for_stop(); void wait_for_stop();
void send_rare_drop_notification(std::shared_ptr<const JSON> message);
protected: protected:
class http_error : public std::runtime_error { class http_error : public std::runtime_error {
public: public:
@@ -35,11 +37,47 @@ protected:
int code; int code;
}; };
struct WebsocketClient {
struct evhttp_connection* conn;
struct bufferevent* bev;
uint8_t pending_opcode;
std::string pending_data;
uint64_t last_communication_time;
void* context;
WebsocketClient(struct evhttp_connection* conn);
~WebsocketClient();
void reset_pending_frame();
};
std::shared_ptr<ServerState> state; std::shared_ptr<ServerState> state;
std::shared_ptr<struct event_base> base; std::shared_ptr<struct event_base> base;
std::shared_ptr<struct evhttp> http; std::shared_ptr<struct evhttp> http;
std::thread th; std::thread th;
std::unordered_set<std::shared_ptr<WebsocketClient>> rare_drop_subscribers;
std::unordered_map<struct bufferevent*, std::shared_ptr<WebsocketClient>> bev_to_websocket_client;
std::shared_ptr<WebsocketClient> enable_websockets(struct evhttp_request* req);
static void dispatch_on_websocket_read(struct bufferevent* bev, void* ctx);
static void dispatch_on_websocket_error(struct bufferevent* bev, short events, void* ctx);
void on_websocket_read(struct bufferevent* bev);
void on_websocket_error(struct bufferevent* bev, short events);
void disconnect_websocket_client(struct bufferevent* bev);
void send_websocket_message(struct bufferevent* bev, const std::string& message, uint8_t opcode = 0x01);
void send_websocket_message(std::shared_ptr<WebsocketClient> c, const std::string& message, uint8_t opcode = 0x01);
virtual void handle_websocket_message(std::shared_ptr<WebsocketClient> c, uint8_t opcode, const std::string& message);
virtual void handle_websocket_disconnect(std::shared_ptr<WebsocketClient> c);
void thread_fn(); void thread_fn();
static void dispatch_handle_request(struct evhttp_request* req, void* ctx); static void dispatch_handle_request(struct evhttp_request* req, void* ctx);
@@ -58,7 +96,7 @@ protected:
static JSON generate_quest_json_st(std::shared_ptr<const Quest> q); static JSON generate_quest_json_st(std::shared_ptr<const Quest> q);
static JSON generate_client_config_json_st(const Client::Config& config); static JSON generate_client_config_json_st(const Client::Config& config);
static JSON generate_license_json_st(std::shared_ptr<const License> l); static JSON generate_account_json_st(std::shared_ptr<const Account> a);
static JSON generate_game_client_json_st(std::shared_ptr<const Client> c, std::shared_ptr<const ItemNameIndex> item_name_index); static JSON generate_game_client_json_st(std::shared_ptr<const Client> c, std::shared_ptr<const ItemNameIndex> item_name_index);
static JSON generate_proxy_client_json_st(std::shared_ptr<const ProxyServer::LinkedSession> ses); static JSON generate_proxy_client_json_st(std::shared_ptr<const ProxyServer::LinkedSession> ses);
static JSON generate_lobby_json_st(std::shared_ptr<const Lobby> l, std::shared_ptr<const ItemNameIndex> item_name_index); static JSON generate_lobby_json_st(std::shared_ptr<const Lobby> l, std::shared_ptr<const ItemNameIndex> item_name_index);
+10 -10
View File
@@ -12,31 +12,31 @@ struct HDLCHeader {
uint8_t address; // 0xFF usually uint8_t address; // 0xFF usually
uint8_t control; // 0x03 for PPP uint8_t control; // 0x03 for PPP
be_uint16_t protocol; be_uint16_t protocol;
} __attribute__((packed)); } __packed_ws__(HDLCHeader, 5);
struct LCPHeader { struct LCPHeader {
uint8_t command; uint8_t command;
uint8_t request_id; uint8_t request_id;
be_uint16_t size; be_uint16_t size;
} __attribute__((packed)); } __packed_ws__(LCPHeader, 4);
struct PAPHeader { struct PAPHeader {
uint8_t command; uint8_t command;
uint8_t request_id; uint8_t request_id;
be_uint16_t size; be_uint16_t size;
} __attribute__((packed)); } __packed_ws__(PAPHeader, 4);
struct IPCPHeader { struct IPCPHeader {
uint8_t command; uint8_t command;
uint8_t request_id; uint8_t request_id;
be_uint16_t size; be_uint16_t size;
} __attribute__((packed)); } __packed_ws__(IPCPHeader, 4);
struct EthernetHeader { struct EthernetHeader {
parray<uint8_t, 6> dest_mac; parray<uint8_t, 6> dest_mac;
parray<uint8_t, 6> src_mac; parray<uint8_t, 6> src_mac;
be_uint16_t protocol; be_uint16_t protocol;
} __attribute__((packed)); } __packed_ws__(EthernetHeader, 0x0E);
struct ARPHeader { struct ARPHeader {
be_uint16_t hardware_type; be_uint16_t hardware_type;
@@ -44,7 +44,7 @@ struct ARPHeader {
uint8_t hwaddr_len; uint8_t hwaddr_len;
uint8_t paddr_len; uint8_t paddr_len;
be_uint16_t operation; be_uint16_t operation;
} __attribute__((packed)); } __packed_ws__(ARPHeader, 8);
struct IPv4Header { struct IPv4Header {
uint8_t version_ihl; uint8_t version_ihl;
@@ -57,14 +57,14 @@ struct IPv4Header {
be_uint16_t checksum; be_uint16_t checksum;
be_uint32_t src_addr; be_uint32_t src_addr;
be_uint32_t dest_addr; be_uint32_t dest_addr;
} __attribute__((packed)); } __packed_ws__(IPv4Header, 0x14);
struct UDPHeader { struct UDPHeader {
be_uint16_t src_port; be_uint16_t src_port;
be_uint16_t dest_port; be_uint16_t dest_port;
be_uint16_t size; be_uint16_t size;
be_uint16_t checksum; be_uint16_t checksum;
} __attribute__((packed)); } __packed_ws__(UDPHeader, 8);
struct TCPHeader { struct TCPHeader {
enum Flag { enum Flag {
@@ -87,7 +87,7 @@ struct TCPHeader {
be_uint16_t window; be_uint16_t window;
be_uint16_t checksum; be_uint16_t checksum;
be_uint16_t urgent_ptr; be_uint16_t urgent_ptr;
} __attribute__((packed)); } __packed_ws__(TCPHeader, 0x14);
struct DHCPHeader { struct DHCPHeader {
uint8_t opcode = 0; uint8_t opcode = 0;
@@ -105,7 +105,7 @@ struct DHCPHeader {
parray<uint8_t, 0xC0> unused_bootp_legacy; parray<uint8_t, 0xC0> unused_bootp_legacy;
be_uint32_t magic = 0x63825363; be_uint32_t magic = 0x63825363;
// Options follow here, terminated with FF // Options follow here, terminated with FF
} __attribute__((packed)); } __packed_ws__(DHCPHeader, 0xF0);
struct FrameInfo { struct FrameInfo {
enum class LinkType { enum class LinkType {
+55 -51
View File
@@ -124,6 +124,7 @@ IPStackSimulator::IPStackSimulator(
shared_ptr<ServerState> state) shared_ptr<ServerState> state)
: base(base), : base(base),
state(state), state(state),
next_network_id(1),
pcap_text_log_file(state->ip_stack_debug ? fopen("IPStackSimulator-Log.txt", "wt") : nullptr) { pcap_text_log_file(state->ip_stack_debug ? fopen("IPStackSimulator-Log.txt", "wt") : nullptr) {
this->host_mac_address_bytes.clear(0x90); this->host_mac_address_bytes.clear(0x90);
this->broadcast_mac_address_bytes.clear(0xFF); this->broadcast_mac_address_bytes.clear(0xFF);
@@ -169,6 +170,10 @@ void IPStackSimulator::add_socket(const string& name, int fd, Protocol proto) {
this->listening_sockets.emplace(piecewise_construct, forward_as_tuple(fd), forward_as_tuple(name, proto, std::move(l))); this->listening_sockets.emplace(piecewise_construct, forward_as_tuple(fd), forward_as_tuple(name, proto, std::move(l)));
} }
shared_ptr<IPStackSimulator::IPClient> IPStackSimulator::get_network(uint64_t network_id) const {
return this->network_id_to_client.at(network_id);
}
uint32_t IPStackSimulator::connect_address_for_remote_address(uint32_t remote_addr) { uint32_t IPStackSimulator::connect_address_for_remote_address(uint32_t remote_addr) {
// Use an address not on the same subnet as the client, so that PSO Plus and // Use an address not on the same subnet as the client, so that PSO Plus and
// Episode III will think they're talking to a remote network and won't reject // Episode III will think they're talking to a remote network and won't reject
@@ -180,8 +185,10 @@ uint32_t IPStackSimulator::connect_address_for_remote_address(uint32_t remote_ad
} }
} }
IPStackSimulator::IPClient::IPClient(shared_ptr<IPStackSimulator> sim, Protocol protocol, struct bufferevent* bev) IPStackSimulator::IPClient::IPClient(
shared_ptr<IPStackSimulator> sim, uint64_t network_id, Protocol protocol, struct bufferevent* bev)
: sim(sim), : sim(sim),
network_id(network_id),
bev(bev, bufferevent_free), bev(bev, bufferevent_free),
protocol(protocol), protocol(protocol),
mac_addr(0), mac_addr(0),
@@ -200,7 +207,7 @@ void IPStackSimulator::IPClient::on_idle_timeout() {
auto sim = this->sim.lock(); auto sim = this->sim.lock();
if (sim) { if (sim) {
ip_stack_simulator_log.info("Idle timeout expired on virtual network %d", bufferevent_getfd(this->bev.get())); ip_stack_simulator_log.info("Idle timeout expired on virtual network %d", bufferevent_getfd(this->bev.get()));
sim->disconnect_client(this->bev.get()); sim->disconnect_client(this->network_id);
} else { } else {
ip_stack_simulator_log.info("Idle timeout expired on virtual network %d, but simulator is missing", bufferevent_getfd(this->bev.get())); ip_stack_simulator_log.info("Idle timeout expired on virtual network %d, but simulator is missing", bufferevent_getfd(this->bev.get()));
} }
@@ -227,40 +234,45 @@ IPStackSimulator::IPClient::TCPConnection::TCPConnection()
bytes_received(0), bytes_received(0),
bytes_sent(0) {} bytes_sent(0) {}
void IPStackSimulator::disconnect_client(struct bufferevent* bev) { void IPStackSimulator::disconnect_client(uint64_t network_id) {
ip_stack_simulator_log.info("Virtual network %d disconnected", bufferevent_getfd(bev)); ip_stack_simulator_log.info("Virtual network N-%" PRIu64 " disconnected", network_id);
this->bev_to_client.erase(bev); this->network_id_to_client.erase(network_id);
} }
void IPStackSimulator::dispatch_on_listen_accept( void IPStackSimulator::dispatch_on_listen_accept(
struct evconnlistener* listener, evutil_socket_t fd, struct evconnlistener* listener, evutil_socket_t fd,
struct sockaddr* address, int socklen, void* ctx) { struct sockaddr* address, int socklen, void* ctx) {
reinterpret_cast<IPStackSimulator*>(ctx)->on_listen_accept( reinterpret_cast<IPStackSimulator*>(ctx)->on_listen_accept(listener, fd, address, socklen);
listener, fd, address, socklen);
} }
void IPStackSimulator::on_listen_accept(struct evconnlistener* listener, void IPStackSimulator::on_listen_accept(struct evconnlistener* listener, evutil_socket_t fd, struct sockaddr*, int) {
evutil_socket_t fd, struct sockaddr*, int) { struct sockaddr_storage remote_addr;
get_socket_addresses(fd, nullptr, &remote_addr);
if (this->state->banned_ipv4_ranges->check(remote_addr)) {
close(fd);
return;
}
int listen_fd = evconnlistener_get_fd(listener); int listen_fd = evconnlistener_get_fd(listener);
const ListeningSocket* listening_socket; const ListeningSocket* listening_socket;
try { try {
listening_socket = &this->listening_sockets.at(listen_fd); listening_socket = &this->listening_sockets.at(listen_fd);
} catch (const out_of_range&) { } catch (const out_of_range&) {
ip_stack_simulator_log.info("Virtual network %d connected via unknown listener %d; disconnecting", fd, listen_fd); ip_stack_simulator_log.info("Virtual network fd %d connected via unknown listener %d; disconnecting", fd, listen_fd);
close(fd); close(fd);
return; return;
} }
ip_stack_simulator_log.info("Virtual network %d connected via %s", fd, listening_socket->name.c_str()); uint64_t network_id = this->next_network_id++;
ip_stack_simulator_log.info("Virtual network N-%" PRIu64 " connected via %s", network_id, listening_socket->name.c_str());
struct bufferevent* bev = bufferevent_socket_new(this->base.get(), fd, struct bufferevent* bev = bufferevent_socket_new(this->base.get(), fd, BEV_OPT_CLOSE_ON_FREE | BEV_OPT_DEFER_CALLBACKS);
BEV_OPT_CLOSE_ON_FREE | BEV_OPT_DEFER_CALLBACKS); auto c = make_shared<IPClient>(this->shared_from_this(), network_id, listening_socket->protocol, bev);
auto c = make_shared<IPClient>(this->shared_from_this(), listening_socket->protocol, bev); this->network_id_to_client.emplace(c->network_id, c);
this->bev_to_client.emplace(make_pair(bev, c));
bufferevent_setcb(bev, &IPStackSimulator::dispatch_on_client_input, nullptr, bufferevent_setcb(bev, &IPStackSimulator::IPClient::dispatch_on_client_input, nullptr,
&IPStackSimulator::dispatch_on_client_error, this); &IPStackSimulator::IPClient::dispatch_on_client_error, c.get());
bufferevent_enable(bev, EV_READ | EV_WRITE); bufferevent_enable(bev, EV_READ | EV_WRITE);
} }
@@ -276,31 +288,26 @@ void IPStackSimulator::on_listen_error(struct evconnlistener* listener) {
event_base_loopexit(this->base.get(), nullptr); event_base_loopexit(this->base.get(), nullptr);
} }
void IPStackSimulator::dispatch_on_client_input( void IPStackSimulator::IPClient::dispatch_on_client_input(struct bufferevent* bev, void* ctx) {
struct bufferevent* bev, void* ctx) { reinterpret_cast<IPClient*>(ctx)->on_client_input(bev);
reinterpret_cast<IPStackSimulator*>(ctx)->on_client_input(bev);
} }
void IPStackSimulator::on_client_input(struct bufferevent* bev) { void IPStackSimulator::IPClient::on_client_input(struct bufferevent* bev) {
struct evbuffer* buf = bufferevent_get_input(bev); struct evbuffer* buf = bufferevent_get_input(bev);
shared_ptr<IPClient> c; auto sim = this->sim.lock();
try { if (!sim) {
c = this->bev_to_client.at(bev);
} catch (const out_of_range&) {
size_t bytes = evbuffer_get_length(buf); size_t bytes = evbuffer_get_length(buf);
ip_stack_simulator_log.warning("Ignoring data received from unregistered virtual network (0x%zX bytes)", ip_stack_simulator_log.warning("Ignoring data from unregistered virtual network (0x%zX bytes)", bytes);
bytes);
evbuffer_drain(buf, bytes); evbuffer_drain(buf, bytes);
return; return;
} }
auto sim = c->sim.lock();
uint64_t idle_timeout_usecs = sim ? sim->state->client_idle_timeout_usecs : 60000000; uint64_t idle_timeout_usecs = sim ? sim->state->client_idle_timeout_usecs : 60000000;
struct timeval tv = usecs_to_timeval(idle_timeout_usecs); struct timeval tv = usecs_to_timeval(idle_timeout_usecs);
event_add(c->idle_timeout_event.get(), &tv); event_add(this->idle_timeout_event.get(), &tv);
switch (c->protocol) { switch (this->protocol) {
case Protocol::ETHERNET_TAPSERVER: case Protocol::ETHERNET_TAPSERVER:
case Protocol::HDLC_TAPSERVER: case Protocol::HDLC_TAPSERVER:
while (evbuffer_get_length(buf) >= 2) { while (evbuffer_get_length(buf) >= 2) {
@@ -315,7 +322,7 @@ void IPStackSimulator::on_client_input(struct bufferevent* bev) {
evbuffer_remove(buf, frame.data(), frame.size()); evbuffer_remove(buf, frame.data(), frame.size());
try { try {
this->on_client_frame(c, frame); sim->on_client_frame(this->shared_from_this(), frame);
} catch (const exception& e) { } catch (const exception& e) {
if (ip_stack_simulator_log.warning("Failed to process frame: %s", e.what())) { if (ip_stack_simulator_log.warning("Failed to process frame: %s", e.what())) {
print_data(stderr, frame); print_data(stderr, frame);
@@ -350,7 +357,7 @@ void IPStackSimulator::on_client_input(struct bufferevent* bev) {
evbuffer_remove(buf, frame.data(), frame.size()); evbuffer_remove(buf, frame.data(), frame.size());
try { try {
this->on_client_frame(c, frame); sim->on_client_frame(this->shared_from_this(), frame);
} catch (const exception& e) { } catch (const exception& e) {
if (ip_stack_simulator_log.warning("Failed to process frame: %s", e.what())) { if (ip_stack_simulator_log.warning("Failed to process frame: %s", e.what())) {
print_data(stderr, frame); print_data(stderr, frame);
@@ -361,18 +368,19 @@ void IPStackSimulator::on_client_input(struct bufferevent* bev) {
} }
} }
void IPStackSimulator::dispatch_on_client_error( void IPStackSimulator::IPClient::dispatch_on_client_error(struct bufferevent* bev, short events, void* ctx) {
struct bufferevent* bev, short events, void* ctx) { reinterpret_cast<IPClient*>(ctx)->on_client_error(bev, events);
reinterpret_cast<IPStackSimulator*>(ctx)->on_client_error(bev, events);
} }
void IPStackSimulator::on_client_error(struct bufferevent* bev, short events) { void IPStackSimulator::IPClient::on_client_error(struct bufferevent*, short events) {
if (events & BEV_EVENT_ERROR) { if (events & BEV_EVENT_ERROR) {
int err = EVUTIL_SOCKET_ERROR(); int err = EVUTIL_SOCKET_ERROR();
ip_stack_simulator_log.warning("Virtual network caused error %d (%s)", err, ip_stack_simulator_log.warning("Virtual network caused error %d (%s)", err, evutil_socket_error_to_string(err));
evutil_socket_error_to_string(err));
} }
if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) { if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) {
this->disconnect_client(bev); auto sim = this->sim.lock();
if (sim) {
sim->disconnect_client(this->network_id);
}
} }
} }
@@ -1335,23 +1343,19 @@ void IPStackSimulator::open_server_connection(shared_ptr<IPClient> c, IPClient::
string conn_str = this->str_for_tcp_connection(c, conn); string conn_str = this->str_for_tcp_connection(c, conn);
if (port_config->behavior == ServerBehavior::PROXY_SERVER) { if (port_config->behavior == ServerBehavior::PROXY_SERVER) {
if (!this->state->proxy_server.get()) { if (!this->state->proxy_server.get()) {
ip_stack_simulator_log.error("TCP connection %s is to non-running proxy server", ip_stack_simulator_log.error("TCP connection %s is to non-running proxy server", conn_str.c_str());
conn_str.c_str());
flush_and_free_bufferevent(bevs[1]); flush_and_free_bufferevent(bevs[1]);
} else { } else {
this->state->proxy_server->connect_client(bevs[1], conn.server_port); this->state->proxy_server->connect_virtual_client(bevs[1], c->network_id, conn.server_port);
ip_stack_simulator_log.info("Connected TCP connection %s to proxy server", ip_stack_simulator_log.info("Connected TCP connection %s to proxy server", conn_str.c_str());
conn_str.c_str());
} }
} else if (this->state->game_server.get()) { } else if (this->state->game_server.get()) {
this->state->game_server->connect_client(bevs[1], c->ipv4_addr, this->state->game_server->connect_virtual_client(
conn.client_port, conn.server_port, port_config->version, bevs[1], c->network_id, c->ipv4_addr, conn.client_port,
port_config->behavior); conn.server_port, port_config->version, port_config->behavior);
ip_stack_simulator_log.info("Connected TCP connection %s to game server", ip_stack_simulator_log.info("Connected TCP connection %s to game server", conn_str.c_str());
conn_str.c_str());
} else { } else {
ip_stack_simulator_log.error("No server available for TCP connection %s", ip_stack_simulator_log.error("No server available for TCP connection %s", conn_str.c_str());
conn_str.c_str());
flush_and_free_bufferevent(bevs[1]); flush_and_free_bufferevent(bevs[1]);
} }
} }
+36 -32
View File
@@ -22,29 +22,14 @@ public:
HDLC_RAW, HDLC_RAW,
}; };
IPStackSimulator(
std::shared_ptr<struct event_base> base,
std::shared_ptr<ServerState> state);
~IPStackSimulator();
void listen(const std::string& name, const std::string& socket_path, Protocol protocol);
void listen(const std::string& name, const std::string& addr, int port, Protocol protocol);
void listen(const std::string& name, int port, Protocol protocol);
void add_socket(const std::string& name, int fd, Protocol protocol);
static uint32_t connect_address_for_remote_address(uint32_t remote_addr);
private:
std::shared_ptr<struct event_base> base;
std::shared_ptr<ServerState> state;
using unique_listener = std::unique_ptr<struct evconnlistener, void (*)(struct evconnlistener*)>; using unique_listener = std::unique_ptr<struct evconnlistener, void (*)(struct evconnlistener*)>;
using unique_bufferevent = std::unique_ptr<struct bufferevent, void (*)(struct bufferevent*)>; using unique_bufferevent = std::unique_ptr<struct bufferevent, void (*)(struct bufferevent*)>;
using unique_evbuffer = std::unique_ptr<struct evbuffer, void (*)(struct evbuffer*)>; using unique_evbuffer = std::unique_ptr<struct evbuffer, void (*)(struct evbuffer*)>;
using unique_event = std::unique_ptr<struct event, void (*)(struct event*)>; using unique_event = std::unique_ptr<struct event, void (*)(struct event*)>;
struct IPClient { struct IPClient : std::enable_shared_from_this<IPClient> {
std::weak_ptr<IPStackSimulator> sim; std::weak_ptr<IPStackSimulator> sim;
uint64_t network_id;
unique_bufferevent bev; unique_bufferevent bev;
Protocol protocol; Protocol protocol;
@@ -86,12 +71,41 @@ private:
unique_event idle_timeout_event; unique_event idle_timeout_event;
IPClient(std::shared_ptr<IPStackSimulator> sim, Protocol protocol, struct bufferevent* bev); IPClient(std::shared_ptr<IPStackSimulator> sim, uint64_t network_id, Protocol protocol, struct bufferevent* bev);
static void dispatch_on_client_input(struct bufferevent* bev, void* ctx);
void on_client_input(struct bufferevent* bev);
static void dispatch_on_client_error(struct bufferevent* bev, short events, void* ctx);
void on_client_error(struct bufferevent* bev, short events);
static void dispatch_on_idle_timeout(evutil_socket_t fd, short events, void* ctx); static void dispatch_on_idle_timeout(evutil_socket_t fd, short events, void* ctx);
void on_idle_timeout(); void on_idle_timeout();
}; };
IPStackSimulator(
std::shared_ptr<struct event_base> base,
std::shared_ptr<ServerState> state);
~IPStackSimulator();
void listen(const std::string& name, const std::string& socket_path, Protocol protocol);
void listen(const std::string& name, const std::string& addr, int port, Protocol protocol);
void listen(const std::string& name, int port, Protocol protocol);
void add_socket(const std::string& name, int fd, Protocol protocol);
static uint32_t connect_address_for_remote_address(uint32_t remote_addr);
std::shared_ptr<IPClient> get_network(uint64_t network_id) const;
inline const std::unordered_map<uint64_t, std::shared_ptr<IPClient>>& all_networks() const {
return this->network_id_to_client;
}
void disconnect_client(uint64_t network_id);
private:
std::shared_ptr<struct event_base> base;
std::shared_ptr<ServerState> state;
uint64_t next_network_id;
struct ListeningSocket { struct ListeningSocket {
std::string name; std::string name;
Protocol protocol; Protocol protocol;
@@ -104,35 +118,26 @@ private:
}; };
std::unordered_map<int, ListeningSocket> listening_sockets; std::unordered_map<int, ListeningSocket> listening_sockets;
std::unordered_map<struct bufferevent*, std::shared_ptr<IPClient>> bev_to_client; std::unordered_map<uint64_t, std::shared_ptr<IPClient>> network_id_to_client;
parray<uint8_t, 6> host_mac_address_bytes; parray<uint8_t, 6> host_mac_address_bytes;
parray<uint8_t, 6> broadcast_mac_address_bytes; parray<uint8_t, 6> broadcast_mac_address_bytes;
FILE* pcap_text_log_file; FILE* pcap_text_log_file;
void disconnect_client(struct bufferevent* bev);
static uint64_t tcp_conn_key_for_connection(const IPClient::TCPConnection& conn); static uint64_t tcp_conn_key_for_connection(const IPClient::TCPConnection& conn);
static uint64_t tcp_conn_key_for_client_frame(const IPv4Header& ipv4, const TCPHeader& tcp); static uint64_t tcp_conn_key_for_client_frame(const IPv4Header& ipv4, const TCPHeader& tcp);
static uint64_t tcp_conn_key_for_client_frame(const FrameInfo& fi); static uint64_t tcp_conn_key_for_client_frame(const FrameInfo& fi);
static std::string str_for_ipv4_netloc(uint32_t addr, uint16_t port); static std::string str_for_ipv4_netloc(uint32_t addr, uint16_t port);
static std::string str_for_tcp_connection(std::shared_ptr<const IPClient> c, static std::string str_for_tcp_connection(std::shared_ptr<const IPClient> c, const IPClient::TCPConnection& conn);
const IPClient::TCPConnection& conn);
static void dispatch_on_listen_accept(struct evconnlistener* listener, static void dispatch_on_listen_accept(struct evconnlistener* listener,
evutil_socket_t fd, struct sockaddr* address, int socklen, void* ctx); evutil_socket_t fd, struct sockaddr* address, int socklen, void* ctx);
void on_listen_accept(struct evconnlistener* listener, evutil_socket_t fd, void on_listen_accept(struct evconnlistener* listener, evutil_socket_t fd, struct sockaddr* address, int socklen);
struct sockaddr* address, int socklen);
static void dispatch_on_listen_error(struct evconnlistener* listener, void* ctx); static void dispatch_on_listen_error(struct evconnlistener* listener, void* ctx);
void on_listen_error(struct evconnlistener* listener); void on_listen_error(struct evconnlistener* listener);
static void dispatch_on_client_input(struct bufferevent* bev, void* ctx);
void on_client_input(struct bufferevent* bev);
static void dispatch_on_client_error(struct bufferevent* bev, short events, void* ctx);
void on_client_error(struct bufferevent* bev, short events);
void send_layer3_frame(std::shared_ptr<IPClient> c, FrameInfo::Protocol proto, const std::string& data) const; void send_layer3_frame(std::shared_ptr<IPClient> c, FrameInfo::Protocol proto, const std::string& data) const;
void send_layer3_frame(std::shared_ptr<IPClient> c, FrameInfo::Protocol proto, const void* data, size_t size) const; void send_layer3_frame(std::shared_ptr<IPClient> c, FrameInfo::Protocol proto, const void* data, size_t size) const;
@@ -158,8 +163,7 @@ private:
struct evbuffer* src_buf = nullptr, struct evbuffer* src_buf = nullptr,
size_t src_bytes = 0); size_t src_bytes = 0);
void open_server_connection( void open_server_connection(std::shared_ptr<IPClient> c, IPClient::TCPConnection& conn);
std::shared_ptr<IPClient> c, IPClient::TCPConnection& conn);
void log_frame(const std::string& data) const; void log_frame(const std::string& data) const;
}; };
+73
View File
@@ -0,0 +1,73 @@
#include "IPV4RangeSet.hh"
#include <arpa/inet.h>
using namespace std;
IPV4RangeSet::IPV4RangeSet(const JSON& json) {
for (const auto& it : json.as_list()) {
// String should be of the form a.b.c.d or a.b.c.d/e
auto tokens = split(it->as_string(), '/');
size_t mask_bits;
if (tokens.size() == 1) {
mask_bits = 32;
} else if (tokens.size() == 2) {
mask_bits = stoul(tokens[1], nullptr, 10);
if (mask_bits > 32) {
throw runtime_error("invalid IPv4 address range");
}
} else {
throw runtime_error("invalid IPv4 address range");
}
auto addr_tokens = split(tokens[0], '.');
if (addr_tokens.size() != 4) {
throw runtime_error("invalid IPv4 address");
}
uint32_t addr = 0;
for (size_t z = 0; z < 4; z++) {
size_t end_pos = 0;
size_t new_byte = stoul(addr_tokens[z], &end_pos, 10);
if (end_pos != addr_tokens[z].size() || new_byte > 0xFF) {
throw runtime_error("invalid IPv4 address");
}
addr = (addr << 8) | new_byte;
}
addr &= (0xFFFFFFFF << (32 - mask_bits));
this->ranges.emplace(addr, mask_bits);
}
}
JSON IPV4RangeSet::json() const {
auto ret = JSON::list();
for (const auto& it : this->ranges) {
uint32_t addr = it.first;
uint8_t mask_bits = it.second;
ret.emplace_back(string_printf("%hhu.%hhu.%hhu.%hhu/%hhu",
static_cast<uint8_t>((addr >> 24) & 0xFF),
static_cast<uint8_t>((addr >> 16) & 0xFF),
static_cast<uint8_t>((addr >> 8) & 0xFF),
static_cast<uint8_t>(addr & 0xFF),
mask_bits));
}
return ret;
}
bool IPV4RangeSet::check(uint32_t addr) const {
auto it = this->ranges.upper_bound(addr);
if (it == this->ranges.begin()) {
return false; // addr is before any range
}
const auto& range = *(--it);
return (((range.first ^ addr) & (0xFFFFFFFF << (32 - range.second))) == 0);
}
bool IPV4RangeSet::check(const struct sockaddr_storage& ss) const {
if (ss.ss_family != AF_INET) {
return false;
}
const sockaddr_in* sin = reinterpret_cast<const sockaddr_in*>(&ss);
return this->check(ntohl(sin->sin_addr.s_addr));
}
+18
View File
@@ -0,0 +1,18 @@
#pragma once
#include <phosg/JSON.hh>
#include <set>
class IPV4RangeSet {
public:
IPV4RangeSet() = default;
explicit IPV4RangeSet(const JSON& json);
JSON json() const;
bool check(uint32_t addr) const;
bool check(const struct sockaddr_storage& ss) const;
protected:
std::map<uint32_t, uint8_t> ranges; // {addr: mask_bits}
};
+455
View File
@@ -0,0 +1,455 @@
#include "IntegralExpression.hh"
#include <algorithm>
#include <mutex>
#include <phosg/Encoding.hh>
#include <phosg/Filesystem.hh>
#include <phosg/Hash.hh>
#include <phosg/Random.hh>
#include <phosg/Strings.hh>
#include <phosg/Tools.hh>
#include <string>
#include <unordered_map>
#include "CommandFormats.hh"
#include "Compression.hh"
#include "Loggers.hh"
#include "PSOEncryption.hh"
#include "QuestScript.hh"
#include "SaveFileFormats.hh"
#include "Text.hh"
using namespace std;
IntegralExpression::IntegralExpression(const string& text)
: root(this->parse_expr(text)) {}
IntegralExpression::BinaryOperatorNode::BinaryOperatorNode(
Type type, unique_ptr<const Node>&& left, unique_ptr<const Node>&& right)
: type(type),
left(std::move(left)),
right(std::move(right)) {}
bool IntegralExpression::BinaryOperatorNode::operator==(const Node& other) const {
try {
const BinaryOperatorNode& other_bin = dynamic_cast<const BinaryOperatorNode&>(other);
return other_bin.type == this->type && *other_bin.left == *this->left && *other_bin.right == *this->right;
} catch (const bad_cast&) {
return false;
}
}
int64_t IntegralExpression::BinaryOperatorNode::evaluate(const Env& env) const {
switch (this->type) {
case Type::LOGICAL_OR:
return this->left->evaluate(env) || this->right->evaluate(env);
case Type::LOGICAL_AND:
return this->left->evaluate(env) && this->right->evaluate(env);
case Type::BITWISE_OR:
return this->left->evaluate(env) | this->right->evaluate(env);
case Type::BITWISE_AND:
return this->left->evaluate(env) & this->right->evaluate(env);
case Type::BITWISE_XOR:
return this->left->evaluate(env) ^ this->right->evaluate(env);
case Type::LEFT_SHIFT:
return this->left->evaluate(env) << this->right->evaluate(env);
case Type::RIGHT_SHIFT:
return this->left->evaluate(env) >> this->right->evaluate(env);
case Type::LESS_THAN:
return this->left->evaluate(env) < this->right->evaluate(env);
case Type::GREATER_THAN:
return this->left->evaluate(env) > this->right->evaluate(env);
case Type::LESS_OR_EQUAL:
return this->left->evaluate(env) <= this->right->evaluate(env);
case Type::GREATER_OR_EQUAL:
return this->left->evaluate(env) >= this->right->evaluate(env);
case Type::EQUAL:
return this->left->evaluate(env) == this->right->evaluate(env);
case Type::NOT_EQUAL:
return this->left->evaluate(env) != this->right->evaluate(env);
case Type::ADD:
return this->left->evaluate(env) + this->right->evaluate(env);
case Type::SUBTRACT:
return this->left->evaluate(env) - this->right->evaluate(env);
case Type::MULTIPLY:
return this->left->evaluate(env) * this->right->evaluate(env);
case Type::DIVIDE:
return this->left->evaluate(env) / this->right->evaluate(env);
case Type::MODULUS:
return this->left->evaluate(env) % this->right->evaluate(env);
default:
throw logic_error("invalid binary operator type");
}
}
string IntegralExpression::BinaryOperatorNode::str() const {
switch (this->type) {
case Type::LOGICAL_OR:
return "(" + this->left->str() + ") || (" + this->right->str() + ")";
case Type::LOGICAL_AND:
return "(" + this->left->str() + ") && (" + this->right->str() + ")";
case Type::BITWISE_OR:
return "(" + this->left->str() + ") | (" + this->right->str() + ")";
case Type::BITWISE_AND:
return "(" + this->left->str() + ") & (" + this->right->str() + ")";
case Type::BITWISE_XOR:
return "(" + this->left->str() + ") ^ (" + this->right->str() + ")";
case Type::LEFT_SHIFT:
return "(" + this->left->str() + ") << (" + this->right->str() + ")";
case Type::RIGHT_SHIFT:
return "(" + this->left->str() + ") >> (" + this->right->str() + ")";
case Type::LESS_THAN:
return "(" + this->left->str() + ") < (" + this->right->str() + ")";
case Type::GREATER_THAN:
return "(" + this->left->str() + ") > (" + this->right->str() + ")";
case Type::LESS_OR_EQUAL:
return "(" + this->left->str() + ") <= (" + this->right->str() + ")";
case Type::GREATER_OR_EQUAL:
return "(" + this->left->str() + ") >= (" + this->right->str() + ")";
case Type::EQUAL:
return "(" + this->left->str() + ") == (" + this->right->str() + ")";
case Type::NOT_EQUAL:
return "(" + this->left->str() + ") != (" + this->right->str() + ")";
case Type::ADD:
return "(" + this->left->str() + ") + (" + this->right->str() + ")";
case Type::SUBTRACT:
return "(" + this->left->str() + ") - (" + this->right->str() + ")";
case Type::MULTIPLY:
return "(" + this->left->str() + ") * (" + this->right->str() + ")";
case Type::DIVIDE:
return "(" + this->left->str() + ") / (" + this->right->str() + ")";
case Type::MODULUS:
return "(" + this->left->str() + ") % (" + this->right->str() + ")";
default:
throw logic_error("invalid binary operator type");
}
}
IntegralExpression::UnaryOperatorNode::UnaryOperatorNode(Type type, unique_ptr<const Node>&& sub)
: type(type),
sub(std::move(sub)) {}
bool IntegralExpression::UnaryOperatorNode::operator==(const Node& other) const {
try {
const UnaryOperatorNode& other_un = dynamic_cast<const UnaryOperatorNode&>(other);
return other_un.type == this->type && *other_un.sub == *this->sub;
} catch (const bad_cast&) {
return false;
}
}
int64_t IntegralExpression::UnaryOperatorNode::evaluate(const Env& env) const {
switch (this->type) {
case Type::LOGICAL_NOT:
return !this->sub->evaluate(env);
case Type::BITWISE_NOT:
return ~this->sub->evaluate(env);
case Type::NEGATIVE:
return -this->sub->evaluate(env);
default:
throw logic_error("invalid unary operator type");
}
}
string IntegralExpression::UnaryOperatorNode::str() const {
switch (this->type) {
case Type::LOGICAL_NOT:
return "!(" + this->sub->str() + ")";
case Type::BITWISE_NOT:
return "~(" + this->sub->str() + ")";
case Type::NEGATIVE:
return "-(" + this->sub->str() + ")";
default:
throw logic_error("invalid unary operator type");
}
}
IntegralExpression::FlagLookupNode::FlagLookupNode(uint16_t flag_index)
: flag_index(flag_index) {}
bool IntegralExpression::FlagLookupNode::operator==(const Node& other) const {
try {
const FlagLookupNode& other_flag = dynamic_cast<const FlagLookupNode&>(other);
return other_flag.flag_index == this->flag_index;
} catch (const bad_cast&) {
return false;
}
}
int64_t IntegralExpression::FlagLookupNode::evaluate(const Env& env) const {
if (!env.flags) {
throw runtime_error("quest flags not available");
}
return env.flags->get(this->flag_index) ? 1 : 0;
}
string IntegralExpression::FlagLookupNode::str() const {
return string_printf("F_%04hX", this->flag_index);
}
IntegralExpression::ChallengeCompletionLookupNode::ChallengeCompletionLookupNode(
Episode episode, uint8_t stage_index)
: episode(episode),
stage_index(stage_index) {}
bool IntegralExpression::ChallengeCompletionLookupNode::operator==(const Node& other) const {
try {
const ChallengeCompletionLookupNode& other_cc = dynamic_cast<const ChallengeCompletionLookupNode&>(other);
return other_cc.episode == this->episode && other_cc.stage_index == this->stage_index;
} catch (const bad_cast&) {
return false;
}
}
int64_t IntegralExpression::ChallengeCompletionLookupNode::evaluate(const Env& env) const {
if (!env.challenge_records) {
throw runtime_error("challenge records not available");
}
if (this->episode == Episode::EP1) {
return env.challenge_records->times_ep1_online.at(this->stage_index).has_value();
} else if (this->episode == Episode::EP2) {
return env.challenge_records->times_ep2_online.at(this->stage_index).has_value();
}
return false;
}
string IntegralExpression::ChallengeCompletionLookupNode::str() const {
return string_printf("CC_%s_%hhu", abbreviation_for_episode(this->episode), static_cast<uint8_t>(this->stage_index + 1));
}
IntegralExpression::TeamRewardLookupNode::TeamRewardLookupNode(const string& reward_name)
: reward_name(reward_name) {}
bool IntegralExpression::TeamRewardLookupNode::operator==(const Node& other) const {
try {
const TeamRewardLookupNode& other_team_reward = dynamic_cast<const TeamRewardLookupNode&>(other);
return other_team_reward.reward_name == this->reward_name;
} catch (const bad_cast&) {
return false;
}
}
int64_t IntegralExpression::TeamRewardLookupNode::evaluate(const Env& env) const {
return (env.team && env.team->has_reward(this->reward_name)) ? 1 : 0;
}
string IntegralExpression::TeamRewardLookupNode::str() const {
return "T_" + this->reward_name;
}
IntegralExpression::NumPlayersLookupNode::NumPlayersLookupNode() {}
bool IntegralExpression::NumPlayersLookupNode::operator==(const Node& other) const {
return dynamic_cast<const NumPlayersLookupNode*>(&other) != nullptr;
}
int64_t IntegralExpression::NumPlayersLookupNode::evaluate(const Env& env) const {
return env.num_players;
}
string IntegralExpression::NumPlayersLookupNode::str() const {
return "V_NumPlayers";
}
IntegralExpression::EventLookupNode::EventLookupNode() {}
bool IntegralExpression::EventLookupNode::operator==(const Node& other) const {
return dynamic_cast<const EventLookupNode*>(&other) != nullptr;
}
int64_t IntegralExpression::EventLookupNode::evaluate(const Env& env) const {
return env.event;
}
string IntegralExpression::EventLookupNode::str() const {
return "V_Event";
}
IntegralExpression::V1PresenceLookupNode::V1PresenceLookupNode() {}
bool IntegralExpression::V1PresenceLookupNode::operator==(const Node& other) const {
return dynamic_cast<const V1PresenceLookupNode*>(&other) != nullptr;
}
int64_t IntegralExpression::V1PresenceLookupNode::evaluate(const Env& env) const {
return env.v1_present ? 1 : 0;
}
string IntegralExpression::V1PresenceLookupNode::str() const {
return "V_V1Present";
}
IntegralExpression::ConstantNode::ConstantNode(int64_t value)
: value(value) {}
bool IntegralExpression::ConstantNode::operator==(const Node& other) const {
try {
const ConstantNode& other_const = dynamic_cast<const ConstantNode&>(other);
return other_const.value == this->value;
} catch (const bad_cast&) {
return false;
}
}
int64_t IntegralExpression::ConstantNode::evaluate(const Env&) const {
return this->value;
}
string IntegralExpression::ConstantNode::str() const {
return string_printf("%" PRId64, this->value);
}
unique_ptr<const IntegralExpression::Node> IntegralExpression::parse_expr(string_view text) {
// Strip off spaces and fully-enclosing parentheses
for (;;) {
size_t starting_size = text.size();
while (text.at(0) == ' ') {
text = text.substr(1);
}
while (text.at(text.size() - 1) == ' ') {
text = text.substr(0, text.size() - 1);
}
if (text.at(0) == '(' && text.at(text.size() - 1) == ')') {
// It doesn't suffice to just check the first ant last characters, since
// text could be like "(a) && (b)". Instead, we ignore the first and last
// characters, and don't strip anything if the internal parentheses are
// unbalanced.
size_t paren_level = 1;
for (size_t z = 1; z < text.size() - 1; z++) {
if (text[z] == '(') {
paren_level++;
} else if (text[z] == ')') {
paren_level--;
if (paren_level == 0) {
break;
}
}
}
if (paren_level > 0) {
text = text.substr(1, text.size() - 2);
}
}
if (text.size() == starting_size) {
break;
}
}
if (text.empty()) {
throw runtime_error("invalid expression");
}
// Check for binary operators at the root level
using BinType = BinaryOperatorNode::Type;
static const vector<vector<pair<std::string, BinaryOperatorNode::Type>>> binary_operator_levels = {
{{make_pair("||", BinType::LOGICAL_OR)}},
{{make_pair("&&", BinType::LOGICAL_AND)}},
{{make_pair("|", BinType::BITWISE_OR)}},
{{make_pair("^", BinType::BITWISE_XOR)}},
{{make_pair("&", BinType::BITWISE_AND)}},
{{make_pair("==", BinType::EQUAL)}, {make_pair("!=", BinType::NOT_EQUAL)}},
{{make_pair("<=", BinType::LESS_OR_EQUAL)}, {make_pair(">=", BinType::GREATER_OR_EQUAL)}, {make_pair("<", BinType::LESS_THAN)}, {make_pair(">", BinType::GREATER_THAN)}},
{{make_pair("<<", BinType::LEFT_SHIFT)}, {make_pair(">>", BinType::RIGHT_SHIFT)}},
{{make_pair("+", BinType::ADD)}, {make_pair("-", BinType::SUBTRACT)}},
{{make_pair("*", BinType::MULTIPLY)}, {make_pair("/", BinType::DIVIDE)}, {make_pair("%", BinType::MODULUS)}},
};
for (const auto& operators : binary_operator_levels) {
size_t paren_level = 0;
for (size_t z = 0; z < text.size() - 1; z++) {
if (text[z] == '(') {
paren_level++;
continue;
} else if (text[z] == ')') {
paren_level--;
continue;
}
if (!paren_level) {
for (const auto& oper : operators) {
// Awful hack (because I'm too lazy to add a tokenization step): if
// the operator is followed or preceded by another copy of itself,
// don't match it (this prevents us from matching & when the token is
// actually &&)
if ((text.size() > z + oper.first.size()) &&
((z < oper.first.size()) || (text.compare(z - oper.first.size(), oper.first.size(), oper.first) != 0)) &&
(text.compare(z, oper.first.size(), oper.first) == 0) &&
(text.compare(z + oper.first.size(), oper.first.size(), oper.first) != 0)) {
auto left = IntegralExpression::parse_expr(text.substr(0, z));
auto right = IntegralExpression::parse_expr(text.substr(z + oper.first.size()));
return make_unique<BinaryOperatorNode>(oper.second, std::move(left), std::move(right));
}
}
}
}
}
// Check for unary operators
if (text[0] == '!') {
return make_unique<UnaryOperatorNode>(UnaryOperatorNode::Type::LOGICAL_NOT,
IntegralExpression::parse_expr(text.substr(1)));
} else if (text[0] == '~') {
return make_unique<UnaryOperatorNode>(UnaryOperatorNode::Type::BITWISE_NOT,
IntegralExpression::parse_expr(text.substr(1)));
} else if (text[0] == '-') {
return make_unique<UnaryOperatorNode>(UnaryOperatorNode::Type::NEGATIVE,
IntegralExpression::parse_expr(text.substr(1)));
}
// Check for env lookups
if (text.starts_with("F_")) {
char* endptr = nullptr;
uint64_t flag = strtoul(text.data() + 2, &endptr, 16);
if (endptr != text.data() + text.size()) {
throw runtime_error("invalid flag lookup token");
}
if (flag >= 0x400) {
throw runtime_error("invalid flag index");
}
return make_unique<FlagLookupNode>(flag);
}
if (text.starts_with("CC_")) {
Episode episode;
if (text.starts_with("CC_Ep1_")) {
episode = Episode::EP1;
} else if (text.starts_with("CC_Ep2_")) {
episode = Episode::EP2;
} else {
throw runtime_error("invalid challenge episode");
}
char* endptr = nullptr;
uint64_t stage_index = strtoul(text.data() + 7, &endptr, 0) - 1;
if (endptr != text.data() + text.size()) {
throw runtime_error("invalid challenge completion lookup token");
}
if ((episode == Episode::EP1 && stage_index > 8) || (episode == Episode::EP2 && stage_index > 4)) {
throw runtime_error("invalid challenge stage index");
}
return make_unique<ChallengeCompletionLookupNode>(episode, stage_index);
}
if (text.starts_with("T_")) {
return make_unique<TeamRewardLookupNode>(string(text.substr(2)));
}
if (text == "V_NumPlayers") {
return make_unique<NumPlayersLookupNode>();
}
if (text == "V_Event") {
return make_unique<EventLookupNode>();
}
if (text == "V_V1Present") {
return make_unique<V1PresenceLookupNode>();
}
// Check for constants
if (text == "true") {
return make_unique<ConstantNode>(1);
}
if (text == "false") {
return make_unique<ConstantNode>(0);
}
try {
size_t endpos;
int64_t v = stoll(string(text), &endpos, 0);
if (endpos == text.size()) {
return make_unique<ConstantNode>(v);
}
} catch (const exception&) {
}
throw runtime_error("unparseable expression");
}
+188
View File
@@ -0,0 +1,188 @@
#pragma once
#include <stdint.h>
#include <map>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
#include "PlayerSubordinates.hh"
#include "QuestScript.hh"
#include "StaticGameData.hh"
#include "TeamIndex.hh"
class IntegralExpression {
public:
struct Env {
const QuestFlagsForDifficulty* flags;
const PlayerRecordsChallengeBB* challenge_records;
std::shared_ptr<const TeamIndex::Team> team;
size_t num_players;
uint8_t event;
bool v1_present;
};
IntegralExpression(const std::string& text);
~IntegralExpression() = default;
inline bool operator==(const IntegralExpression& other) const {
return this->root->operator==(*other.root);
}
inline bool operator!=(const IntegralExpression& other) const {
return !this->operator==(other);
}
inline int64_t evaluate(const Env& env) const {
return this->root->evaluate(env);
}
inline std::string str() const {
return this->root->str();
}
protected:
class Node {
public:
virtual ~Node() = default;
virtual bool operator==(const Node& other) const = 0;
inline bool operator!=(const Node& other) const {
return !this->operator==(other);
}
virtual int64_t evaluate(const Env& env) const = 0;
virtual std::string str() const = 0;
protected:
Node() = default;
};
class BinaryOperatorNode : public Node {
public:
enum class Type {
LOGICAL_OR = 0,
LOGICAL_AND,
BITWISE_OR,
BITWISE_AND,
BITWISE_XOR,
LEFT_SHIFT,
RIGHT_SHIFT,
LESS_THAN,
GREATER_THAN,
LESS_OR_EQUAL,
GREATER_OR_EQUAL,
EQUAL,
NOT_EQUAL,
ADD,
SUBTRACT,
MULTIPLY,
DIVIDE,
MODULUS,
};
BinaryOperatorNode(Type type, std::unique_ptr<const Node>&& left, std::unique_ptr<const Node>&& right);
virtual ~BinaryOperatorNode() = default;
virtual bool operator==(const Node& other) const;
virtual int64_t evaluate(const Env& env) const;
virtual std::string str() const;
protected:
Type type;
std::unique_ptr<const Node> left;
std::unique_ptr<const Node> right;
};
class UnaryOperatorNode : public Node {
public:
enum class Type {
LOGICAL_NOT = 0,
BITWISE_NOT,
NEGATIVE,
};
UnaryOperatorNode(Type type, std::unique_ptr<const Node>&& sub);
virtual ~UnaryOperatorNode() = default;
virtual bool operator==(const Node& other) const;
virtual int64_t evaluate(const Env& env) const;
virtual std::string str() const;
protected:
Type type;
std::unique_ptr<const Node> sub;
};
class FlagLookupNode : public Node {
public:
FlagLookupNode(uint16_t flag_index);
virtual ~FlagLookupNode() = default;
virtual bool operator==(const Node& other) const;
virtual int64_t evaluate(const Env& env) const;
virtual std::string str() const;
protected:
uint16_t flag_index;
};
class ChallengeCompletionLookupNode : public Node {
public:
ChallengeCompletionLookupNode(Episode episode, uint8_t stage_index);
virtual ~ChallengeCompletionLookupNode() = default;
virtual bool operator==(const Node& other) const;
virtual int64_t evaluate(const Env& env) const;
virtual std::string str() const;
protected:
Episode episode;
uint8_t stage_index;
};
class TeamRewardLookupNode : public Node {
public:
TeamRewardLookupNode(const std::string& reward_name);
virtual ~TeamRewardLookupNode() = default;
virtual bool operator==(const Node& other) const;
virtual int64_t evaluate(const Env& env) const;
virtual std::string str() const;
protected:
std::string reward_name;
};
class NumPlayersLookupNode : public Node {
public:
NumPlayersLookupNode();
virtual ~NumPlayersLookupNode() = default;
virtual bool operator==(const Node& other) const;
virtual int64_t evaluate(const Env& env) const;
virtual std::string str() const;
};
class EventLookupNode : public Node {
public:
EventLookupNode();
virtual ~EventLookupNode() = default;
virtual bool operator==(const Node& other) const;
virtual int64_t evaluate(const Env& env) const;
virtual std::string str() const;
};
class V1PresenceLookupNode : public Node {
public:
V1PresenceLookupNode();
virtual ~V1PresenceLookupNode() = default;
virtual bool operator==(const Node& other) const;
virtual int64_t evaluate(const Env& env) const;
virtual std::string str() const;
};
class ConstantNode : public Node {
public:
ConstantNode(int64_t value);
virtual ~ConstantNode() = default;
virtual bool operator==(const Node& other) const;
virtual int64_t evaluate(const Env& env) const;
virtual std::string str() const;
protected:
int64_t value;
};
std::unique_ptr<const Node> parse_expr(std::string_view text);
std::unique_ptr<const Node> root;
};
+89 -79
View File
@@ -7,6 +7,17 @@
using namespace std; using namespace std;
// The favored weapon type table is hardcoded in the game client. The table is:
// Viridia shots
// Greennill rifles
// Skyly swords
// Bluefull partisans
// Purplenum mechguns
// Pinkal canes
// Redria (none)
// Oran daggers
// Yellowboze (none)
// Whitill slicers
static const array<uint8_t, 10> favored_weapon_by_section_id = { static const array<uint8_t, 10> favored_weapon_by_section_id = {
0x09, 0x07, 0x02, 0x04, 0x08, 0x0A, 0xFF, 0x03, 0xFF, 0x05}; 0x09, 0x07, 0x02, 0x04, 0x08, 0x0A, 0xFF, 0x03, 0xFF, 0x05};
@@ -66,8 +77,9 @@ bool ItemCreator::are_rare_drops_allowed() const {
// Note: The client has an additional check here, which appears to be a subtle // Note: The client has an additional check here, which appears to be a subtle
// anti-cheating measure. There is a flag on the client, initially zero, which // anti-cheating measure. There is a flag on the client, initially zero, which
// is set to 1 when certain unexpected item-related things happen (for // is set to 1 when certain unexpected item-related things happen (for
// example, a player possessing a mag with a level above 200). When the flag // example, a player possessing a mag with a level above 200, or a stack of
// is set, this function returns false, which prevents all rare item drops. // consumables with an amount above the stack size limit). When the flag is
// set, this function returns false, which prevents all rare item drops.
// newserv intentionally does not implement this flag. // newserv intentionally does not implement this flag.
return (this->mode != GameMode::CHALLENGE); return (this->mode != GameMode::CHALLENGE);
} }
@@ -471,7 +483,7 @@ void ItemCreator::deduplicate_weapon_bonuses(ItemData& item) const {
void ItemCreator::set_item_kill_count_if_unsealable(ItemData& item) const { void ItemCreator::set_item_kill_count_if_unsealable(ItemData& item) const {
if (this->item_parameter_table->is_unsealable_item(item)) { if (this->item_parameter_table->is_unsealable_item(item)) {
this->log.info("Item is unsealable; setting kill count to zero"); this->log.info("Item is unsealable; setting kill count to zero");
item.set_sealed_item_kill_count(0); item.set_kill_count(0);
} }
} }
@@ -1438,78 +1450,78 @@ vector<ItemData> ItemCreator::generate_weapon_shop_contents(size_t player_level)
} else { } else {
static const vector<pair<uint8_t, uint8_t>> defs({ static const vector<pair<uint8_t, uint8_t>> defs({
{0x01, 0x00}, /* 00 */ {0x01, 0x00},
{0x01, 0x01}, /* 01 */ {0x01, 0x01},
{0x01, 0x02}, /* 02 */ {0x01, 0x02},
{0x01, 0x03}, /* 03 */ {0x01, 0x03},
{0x01, 0x04}, /* 04 */ {0x01, 0x04},
{0x03, 0x00}, /* 05 */ {0x03, 0x00},
{0x03, 0x01}, /* 06 */ {0x03, 0x01},
{0x03, 0x02}, /* 07 */ {0x03, 0x02},
{0x03, 0x03}, /* 08 */ {0x03, 0x03},
{0x03, 0x04}, /* 09 */ {0x03, 0x04},
{0x02, 0x00}, /* 0A */ {0x02, 0x00},
{0x02, 0x01}, /* 0B */ {0x02, 0x01},
{0x02, 0x02}, /* 0C */ {0x02, 0x02},
{0x02, 0x03}, /* 0D */ {0x02, 0x03},
{0x02, 0x04}, /* 0E */ {0x02, 0x04},
{0x05, 0x00}, /* 0F */ {0x05, 0x00},
{0x05, 0x01}, /* 10 */ {0x05, 0x01},
{0x05, 0x02}, /* 11 */ {0x05, 0x02},
{0x05, 0x03}, /* 12 */ {0x05, 0x03},
{0x05, 0x04}, /* 13 */ {0x05, 0x04},
{0x04, 0x00}, /* 14 */ {0x04, 0x00},
{0x04, 0x01}, /* 15 */ {0x04, 0x01},
{0x04, 0x02}, /* 16 */ {0x04, 0x02},
{0x04, 0x03}, /* 17 */ {0x04, 0x03},
{0x04, 0x04}, /* 18 */ {0x04, 0x04},
{0x06, 0x00}, /* 19 */ {0x06, 0x00},
{0x06, 0x01}, /* 1A */ {0x06, 0x01},
{0x06, 0x02}, /* 1B */ {0x06, 0x02},
{0x06, 0x03}, /* 1C */ {0x06, 0x03},
{0x06, 0x04}, /* 1D */ {0x06, 0x04},
{0x07, 0x00}, /* 1E */ {0x07, 0x00},
{0x07, 0x01}, /* 1F */ {0x07, 0x01},
{0x07, 0x02}, /* 20 */ {0x07, 0x02},
{0x07, 0x03}, /* 21 */ {0x07, 0x03},
{0x07, 0x04}, /* 22 */ {0x07, 0x04},
{0x08, 0x00}, /* 23 */ {0x08, 0x00},
{0x08, 0x01}, /* 24 */ {0x08, 0x01},
{0x08, 0x02}, /* 25 */ {0x08, 0x02},
{0x08, 0x03}, /* 26 */ {0x08, 0x03},
{0x08, 0x04}, /* 27 */ {0x08, 0x04},
{0x09, 0x00}, /* 28 */ {0x09, 0x00},
{0x09, 0x01}, /* 29 */ {0x09, 0x01},
{0x09, 0x02}, /* 2A */ {0x09, 0x02},
{0x09, 0x03}, /* 2B */ {0x09, 0x03},
{0x09, 0x04}, /* 2C */ {0x09, 0x04},
{0x0A, 0x00}, /* 2D */ {0x0A, 0x00},
{0x0A, 0x01}, /* 2E */ {0x0A, 0x01},
{0x0A, 0x02}, /* 2F */ {0x0A, 0x02},
{0x0A, 0x03}, /* 30 */ {0x0A, 0x03},
{0x0B, 0x00}, /* 31 */ {0x0B, 0x00},
{0x0B, 0x01}, /* 32 */ {0x0B, 0x01},
{0x0B, 0x02}, /* 33 */ {0x0B, 0x02},
{0x0B, 0x03}, /* 34 */ {0x0B, 0x03},
{0x0C, 0x00}, /* 35 */ {0x0C, 0x00},
{0x0C, 0x01}, /* 36 */ {0x0C, 0x01},
{0x0C, 0x02}, /* 37 */ {0x0C, 0x02},
{0x0C, 0x03}, /* 38 */ {0x0C, 0x03},
{0xFF, 0xFF}, /* 39 */ {0xFF, 0xFF}, // Special-cased above
{0xFF, 0xFF}, /* 3A */ {0xFF, 0xFF}, // Special-cased above
{0x01, 0x05}, /* 3B */ {0x01, 0x05},
{0x02, 0x05}, /* 3C */ {0x02, 0x05},
{0x06, 0x05}, /* 3D */ {0x06, 0x05},
{0x08, 0x05}, /* 3E */ {0x08, 0x05},
{0x0A, 0x04}, /* 3F */ {0x0A, 0x04},
{0x0C, 0x04}, /* 40 */ {0x0C, 0x04},
{0x0B, 0x04}, /* 41 */ {0x0B, 0x04},
{0x01, 0x06}, /* 42 */ {0x01, 0x06},
{0x03, 0x05}, /* 43 */ {0x03, 0x05},
{0x07, 0x05}, /* 44 */ {0x07, 0x05},
{0x0A, 0x05}, /* 45 */ {0x0A, 0x05},
{0x0C, 0x05}, /* 46 */ {0x0C, 0x05},
{0x0B, 0x05}, /* 47 */ {0x0B, 0x05},
}); });
const auto& def = defs.at(which); const auto& def = defs.at(which);
item.data1[0] = 0; item.data1[0] = 0;
@@ -1555,10 +1567,8 @@ void ItemCreator::generate_weapon_shop_item_grind(ItemData& item, size_t player_
? this->weapon_random_set->get_favored_grind_range(table_index) ? this->weapon_random_set->get_favored_grind_range(table_index)
: this->weapon_random_set->get_standard_grind_range(table_index); : this->weapon_random_set->get_standard_grind_range(table_index);
const auto& weapon_def = this->item_parameter_table->get_weapon( const auto& weapon_def = this->item_parameter_table->get_weapon(item.data1[1], item.data1[2]);
item.data1[1], item.data1[2]); item.data1[3] = clamp<uint8_t>(this->rand_int(range->max + 1), range->min, weapon_def.max_grind);
item.data1[3] = clamp<uint8_t>(
this->rand_int(range->max + 1), range->min, weapon_def.max_grind);
} }
void ItemCreator::generate_weapon_shop_item_special(ItemData& item, size_t player_level) { void ItemCreator::generate_weapon_shop_item_special(ItemData& item, size_t player_level) {
+19 -1
View File
@@ -78,11 +78,29 @@ private:
struct UnitResult { struct UnitResult {
uint8_t unit; uint8_t unit;
int8_t modifier; int8_t modifier;
} __attribute__((packed)); } __packed_ws__(UnitResult, 2);
std::array<std::vector<UnitResult>, 13> unit_results_by_star_count; std::array<std::vector<UnitResult>, 13> unit_results_by_star_count;
// Note: The original implementation uses 17 different random states for some // Note: The original implementation uses 17 different random states for some
// reason. We forego that and use only one for simplicity. // reason. We forego that and use only one for simplicity.
// Originally, the 17 random states were used for:
// [0x00] - drop-anything rate check
// [0x01] - common item class check
// [0x02] - get_rand_from_weighted_tables16 determinants
// [0x03] - get_rand_from_weighted_tables8 determinants
// [0x04] - tech disk levels
// [0x05] - meseta amounts
// [0x06] - rare drop rate check
// [0x07] - rare weapon special table index
// [0x08] - apparently unused
// [0x09] - whether to generate a common weapon special
// [0x0A] - number of stars for common weapon special
// [0x0B] - unit modifiers
// [0x0C] - common armor DFP bonuses
// [0x0D] - common armor EVP bonuses
// [0x0E] - apparently unused
// [0x0F] - which common weapon special to generate
// [0x10] - apparently unused
std::shared_ptr<PSOLFGEncryption> opt_rand_crypt; std::shared_ptr<PSOLFGEncryption> opt_rand_crypt;
bool are_rare_drops_allowed() const; bool are_rare_drops_allowed() const;
+21 -4
View File
@@ -101,6 +101,14 @@ bool ItemData::empty() const {
} }
uint32_t ItemData::primary_identifier() const { uint32_t ItemData::primary_identifier() const {
// Primary identifiers are like:
// - 00TTSS00 = weapon (T = type, S = subtype; subtype is 0 for ES weapons)
// - 01TTSS00 = armor/shield/unit
// - 02TT0000 = mag
// - 0302ZZLL = tech disk (Z = tech number, L = level)
// - 03TTSS00 = tool
// - 04000000 = meseta
// The game treats any item starting with 04 as Meseta, and ignores the rest // The game treats any item starting with 04 as Meseta, and ignores the rest
// of data1 (the value is in data2) // of data1 (the value is in data2)
if (this->data1[0] == 0x04) { if (this->data1[0] == 0x04) {
@@ -423,7 +431,10 @@ void ItemData::decode_for_version(Version from_version) {
} }
void ItemData::encode_for_version(Version to_version, shared_ptr<const ItemParameterTable> item_parameter_table) { void ItemData::encode_for_version(Version to_version, shared_ptr<const ItemParameterTable> item_parameter_table) {
bool should_encode_v2_data = (is_v1(to_version) || is_v2(to_version)) && (to_version != Version::GC_NTE) && !this->has_encoded_v2_data(); bool should_encode_v2_data = item_parameter_table &&
(is_v1(to_version) || is_v2(to_version)) &&
(to_version != Version::GC_NTE) &&
!this->has_encoded_v2_data();
switch (this->data1[0]) { switch (this->data1[0]) {
case 0x00: case 0x00:
@@ -526,17 +537,23 @@ bool ItemData::has_encoded_v2_data() const {
: (this->get_encoded_v2_data() != 0); : (this->get_encoded_v2_data() != 0);
} }
uint16_t ItemData::get_sealed_item_kill_count() const { bool ItemData::has_kill_count() const {
return ((this->data1[10] << 8) | this->data1[11]) & 0x7FFF; return !this->is_s_rank_weapon() && (this->data1[10] & 0x80);
} }
void ItemData::set_sealed_item_kill_count(uint16_t v) { uint16_t ItemData::get_kill_count() const {
return this->has_kill_count() ? (((this->data1[10] << 8) | this->data1[11]) & 0x7FFF) : 0;
}
void ItemData::set_kill_count(uint16_t v) {
if (!this->is_s_rank_weapon()) {
if (v > 0x7FFF) { if (v > 0x7FFF) {
this->data1w[5] = 0xFFFF; this->data1w[5] = 0xFFFF;
} else { } else {
this->data1[10] = (v >> 8) | 0x80; this->data1[10] = (v >> 8) | 0x80;
this->data1[11] = v; this->data1[11] = v;
} }
}
} }
uint8_t ItemData::get_tool_item_amount(const StackLimits& limits) const { uint8_t ItemData::get_tool_item_amount(const StackLimits& limits) const {
+7 -5
View File
@@ -84,6 +84,7 @@ struct ItemData {
// Mag: 02ZZLLWW HHHHIIII JJJJKKKK YYQQPPVV // Mag: 02ZZLLWW HHHHIIII JJJJKKKK YYQQPPVV
// Tool: 03ZZZZFF 00CC0000 00000000 00000000 // Tool: 03ZZZZFF 00CC0000 00000000 00000000
// Meseta: 04000000 00000000 00000000 MMMMMMMM // Meseta: 04000000 00000000 00000000 MMMMMMMM
// 01034D00 00000000 204E0000
// A = attribute type (for S-ranks, custom name) // A = attribute type (for S-ranks, custom name)
// B = attribute amount (for S-ranks, custom name) // B = attribute amount (for S-ranks, custom name)
// C = stack size (for tools) // C = stack size (for tools)
@@ -121,7 +122,7 @@ struct ItemData {
parray<be_uint16_t, 6> data1wb; parray<be_uint16_t, 6> data1wb;
parray<le_uint32_t, 3> data1d; parray<le_uint32_t, 3> data1d;
parray<be_uint32_t, 3> data1db; parray<be_uint32_t, 3> data1db;
} __attribute__((packed)); } __packed__;
le_uint32_t id; le_uint32_t id;
union { union {
parray<uint8_t, 4> data2; parray<uint8_t, 4> data2;
@@ -129,7 +130,7 @@ struct ItemData {
parray<be_uint16_t, 2> data2wb; parray<be_uint16_t, 2> data2wb;
le_uint32_t data2d; le_uint32_t data2d;
be_uint32_t data2db; be_uint32_t data2db;
} __attribute__((packed)); } __packed__;
ItemData(); ItemData();
ItemData(const ItemData& other); ItemData(const ItemData& other);
@@ -173,8 +174,9 @@ struct ItemData {
uint8_t get_encoded_v2_data() const; uint8_t get_encoded_v2_data() const;
bool has_encoded_v2_data() const; bool has_encoded_v2_data() const;
uint16_t get_sealed_item_kill_count() const; bool has_kill_count() const;
void set_sealed_item_kill_count(uint16_t v); uint16_t get_kill_count() const;
void set_kill_count(uint16_t v);
uint8_t get_tool_item_amount(const StackLimits& limits) const; uint8_t get_tool_item_amount(const StackLimits& limits) const;
void set_tool_item_amount(const StackLimits& limits, uint8_t amount); void set_tool_item_amount(const StackLimits& limits, uint8_t amount);
int16_t get_armor_or_shield_defense_bonus() const; int16_t get_armor_or_shield_defense_bonus() const;
@@ -195,4 +197,4 @@ struct ItemData {
bool empty() const; bool empty() const;
static bool compare_for_sort(const ItemData& a, const ItemData& b); static bool compare_for_sort(const ItemData& a, const ItemData& b);
} __attribute__((packed)); } __packed_ws__(ItemData, 0x14);
+20 -8
View File
@@ -166,7 +166,7 @@ std::string ItemNameIndex::describe_item(const ItemData& item, bool include_colo
} }
} }
// For weapons, add the grind and percentages, or S-rank name if applicable // For weapons, add the grind and bonuses, or S-rank name if applicable
if (item.data1[0] == 0x00) { if (item.data1[0] == 0x00) {
if (item.data1[3] > 0) { if (item.data1[3] > 0) {
ret_tokens.emplace_back(string_printf("+%hhu", item.data1[3])); ret_tokens.emplace_back(string_printf("+%hhu", item.data1[3]));
@@ -201,22 +201,34 @@ std::string ItemNameIndex::describe_item(const ItemData& item, bool include_colo
} }
} else { // Not S-rank (extended name bits not set) } else { // Not S-rank (extended name bits not set)
parray<int8_t, 5> percentages(0); parray<int8_t, 5> bonuses(0);
for (size_t x = 0; x < 3; x++) { for (size_t x = 0; x < 3; x++) {
uint8_t which = item.data1[6 + 2 * x]; uint8_t which = item.data1[6 + 2 * x];
uint8_t value = item.data1[7 + 2 * x]; uint8_t value = item.data1[7 + 2 * x];
if (which == 0) { if (which == 0) {
continue; continue;
} }
if (which > 5) { if (which & 0x80) {
uint16_t kill_count = ((which << 8) & 0x7F00) | (value & 0xFF);
ret_tokens.emplace_back(string_printf("K:%hu", kill_count));
} else if (which > 5) {
ret_tokens.emplace_back(string_printf("!PC:%02hhX%02hhX", which, value)); ret_tokens.emplace_back(string_printf("!PC:%02hhX%02hhX", which, value));
} else { } else {
percentages[which - 1] = value; bonuses[which - 1] = value;
} }
} }
if (!percentages.is_filled_with(0)) { if (!bonuses.is_filled_with(0)) {
ret_tokens.emplace_back(string_printf("%hhd/%hhd/%hhd/%hhd/%hhd", bool should_include_hit = (bonuses[4] != 0);
percentages[0], percentages[1], percentages[2], percentages[3], percentages[4])); bool should_highlight_hit = include_color_escapes && (bonuses[4] > 0);
const char* color_prefix = include_color_escapes ? "$C7" : "";
if (should_include_hit) {
ret_tokens.emplace_back(string_printf("%s%hhd/%hhd/%hhd/%hhd/%s%hhd",
color_prefix, bonuses[0], bonuses[1], bonuses[2], bonuses[3],
(should_highlight_hit ? "$C6" : ""), bonuses[4]));
} else {
ret_tokens.emplace_back(string_printf("%s%hhd/%hhd/%hhd/%hhd",
color_prefix, bonuses[0], bonuses[1], bonuses[2], bonuses[3]));
}
} }
} }
@@ -840,7 +852,7 @@ void ItemNameIndex::print_table(FILE* stream) const {
t.amount.load(), t.amount.load(),
t.tech.load(), t.tech.load(),
t.cost.load(), t.cost.load(),
t.item_flag.load(), t.item_flags.load(),
stars, stars,
divisor_str.c_str(), divisor_str.c_str(),
name.c_str()); name.c_str());
+60 -59
View File
@@ -79,9 +79,9 @@ ItemParameterTable::ItemParameterTable(shared_ptr<const string> data, Version ve
case Version::GC_V3: case Version::GC_V3:
case Version::XB_V3: { case Version::XB_V3: {
if (is_big_endian(this->version)) { if (is_big_endian(this->version)) {
this->offsets_v3_be = &this->r.pget<TableOffsetsV3V4<true>>(offset_table_offset); this->offsets_v3_be = &this->r.pget<TableOffsetsV3V4BE>(offset_table_offset);
} else { } else {
this->offsets_v3_le = &this->r.pget<TableOffsetsV3V4<false>>(offset_table_offset); this->offsets_v3_le = &this->r.pget<TableOffsetsV3V4>(offset_table_offset);
} }
this->num_weapon_classes = 0xAA; this->num_weapon_classes = 0xAA;
this->num_tool_classes = 0x18; this->num_tool_classes = 0x18;
@@ -93,7 +93,7 @@ ItemParameterTable::ItemParameterTable(shared_ptr<const string> data, Version ve
} }
case Version::BB_V4: { case Version::BB_V4: {
this->offsets_v4 = &this->r.pget<TableOffsetsV3V4<false>>(offset_table_offset); this->offsets_v4 = &this->r.pget<TableOffsetsV3V4>(offset_table_offset);
this->num_weapon_classes = 0xED; this->num_weapon_classes = 0xED;
this->num_tool_classes = 0x1B; this->num_tool_classes = 0x1B;
this->item_stars_first_id = 0xB1; this->item_stars_first_id = 0xB1;
@@ -207,7 +207,7 @@ ItemParameterTable::WeaponV4 ItemParameterTable::WeaponGCNTE::to_v4() const {
} }
template <bool IsBigEndian> template <bool IsBigEndian>
ItemParameterTable::WeaponV4 ItemParameterTable::WeaponV3<IsBigEndian>::to_v4() const { ItemParameterTable::WeaponV4 ItemParameterTable::WeaponV3T<IsBigEndian>::to_v4() const {
WeaponV4 ret; WeaponV4 ret;
ret.base.id = this->base.id.load(); ret.base.id = this->base.id.load();
ret.base.type = this->base.type.load(); ret.base.type = this->base.type.load();
@@ -282,7 +282,7 @@ ItemParameterTable::ArmorOrShieldV4 ItemParameterTable::ArmorOrShieldV1V2::to_v4
} }
template <bool IsBigEndian> template <bool IsBigEndian>
ItemParameterTable::ArmorOrShieldV4 ItemParameterTable::ArmorOrShieldV3<IsBigEndian>::to_v4() const { ItemParameterTable::ArmorOrShieldV4 ItemParameterTable::ArmorOrShieldV3T<IsBigEndian>::to_v4() const {
ArmorOrShieldV4 ret; ArmorOrShieldV4 ret;
ret.base.id = this->base.id.load(); ret.base.id = this->base.id.load();
ret.base.type = this->base.type.load(); ret.base.type = this->base.type.load();
@@ -324,7 +324,7 @@ ItemParameterTable::UnitV4 ItemParameterTable::UnitV1V2::to_v4() const {
} }
template <bool IsBigEndian> template <bool IsBigEndian>
ItemParameterTable::UnitV4 ItemParameterTable::UnitV3<IsBigEndian>::to_v4() const { ItemParameterTable::UnitV4 ItemParameterTable::UnitV3T<IsBigEndian>::to_v4() const {
UnitV4 ret; UnitV4 ret;
ret.base.id = this->base.id.load(); ret.base.id = this->base.id.load();
ret.base.type = this->base.type.load(); ret.base.type = this->base.type.load();
@@ -371,7 +371,7 @@ ItemParameterTable::MagV4 ItemParameterTable::MagV2::to_v4() const {
} }
template <bool IsBigEndian> template <bool IsBigEndian>
ItemParameterTable::MagV4 ItemParameterTable::MagV3<IsBigEndian>::to_v4() const { ItemParameterTable::MagV4 ItemParameterTable::MagV3T<IsBigEndian>::to_v4() const {
MagV4 ret; MagV4 ret;
ret.base.id = this->base.id.load(); ret.base.id = this->base.id.load();
ret.base.type = this->base.type.load(); ret.base.type = this->base.type.load();
@@ -397,12 +397,12 @@ ItemParameterTable::ToolV4 ItemParameterTable::ToolV1V2::to_v4() const {
ret.amount = this->amount; ret.amount = this->amount;
ret.tech = this->tech; ret.tech = this->tech;
ret.cost = this->cost; ret.cost = this->cost;
ret.item_flag = this->item_flag; ret.item_flags = this->item_flags;
return ret; return ret;
} }
template <bool IsBigEndian> template <bool IsBigEndian>
ItemParameterTable::ToolV4 ItemParameterTable::ToolV3<IsBigEndian>::to_v4() const { ItemParameterTable::ToolV4 ItemParameterTable::ToolV3T<IsBigEndian>::to_v4() const {
ToolV4 ret; ToolV4 ret;
ret.base.id = this->base.id.load(); ret.base.id = this->base.id.load();
ret.base.type = this->base.type.load(); ret.base.type = this->base.type.load();
@@ -410,19 +410,19 @@ ItemParameterTable::ToolV4 ItemParameterTable::ToolV3<IsBigEndian>::to_v4() cons
ret.amount = this->amount.load(); ret.amount = this->amount.load();
ret.tech = this->tech.load(); ret.tech = this->tech.load();
ret.cost = this->cost.load(); ret.cost = this->cost.load();
ret.item_flag = this->item_flag.load(); ret.item_flags = this->item_flags.load();
return ret; return ret;
} }
template <bool IsBigEndian> template <bool IsBigEndian>
size_t indirect_lookup_2d_count(const StringReader& r, size_t root_offset, size_t co_index) { size_t indirect_lookup_2d_count(const StringReader& r, size_t root_offset, size_t co_index) {
using ArrayRefT = typename std::conditional_t<IsBigEndian, ItemParameterTable::ArrayRefBE, ItemParameterTable::ArrayRefLE>; using ArrayRefT = typename std::conditional_t<IsBigEndian, ItemParameterTable::ArrayRefBE, ItemParameterTable::ArrayRef>;
return r.pget<ArrayRefT>(root_offset + sizeof(ArrayRefT) * co_index).count; return r.pget<ArrayRefT>(root_offset + sizeof(ArrayRefT) * co_index).count;
} }
template <typename T, bool IsBigEndian> template <typename T, bool IsBigEndian>
const T& indirect_lookup_2d(const StringReader& r, size_t root_offset, size_t co_index, size_t item_index) { const T& indirect_lookup_2d(const StringReader& r, size_t root_offset, size_t co_index, size_t item_index) {
using ArrayRefT = typename std::conditional_t<IsBigEndian, ItemParameterTable::ArrayRefBE, ItemParameterTable::ArrayRefLE>; using ArrayRefT = typename std::conditional_t<IsBigEndian, ItemParameterTable::ArrayRefBE, ItemParameterTable::ArrayRef>;
const auto& co = r.pget<ArrayRefT>(root_offset + sizeof(ArrayRefT) * co_index); const auto& co = r.pget<ArrayRefT>(root_offset + sizeof(ArrayRefT) * co_index);
if (item_index >= co.count) { if (item_index >= co.count) {
@@ -473,9 +473,9 @@ const ItemParameterTable::WeaponV4& ItemParameterTable::get_weapon(uint8_t data1
} else if (this->offsets_gc_nte) { } else if (this->offsets_gc_nte) {
def_v4 = indirect_lookup_2d<WeaponGCNTE, true>(this->r, this->offsets_gc_nte->weapon_table, data1_1, data1_2).to_v4(); def_v4 = indirect_lookup_2d<WeaponGCNTE, true>(this->r, this->offsets_gc_nte->weapon_table, data1_1, data1_2).to_v4();
} else if (this->offsets_v3_le) { } else if (this->offsets_v3_le) {
def_v4 = indirect_lookup_2d<WeaponV3<false>, false>(this->r, this->offsets_v3_le->weapon_table, data1_1, data1_2).to_v4(); def_v4 = indirect_lookup_2d<WeaponV3, false>(this->r, this->offsets_v3_le->weapon_table, data1_1, data1_2).to_v4();
} else if (this->offsets_v3_be) { } else if (this->offsets_v3_be) {
def_v4 = indirect_lookup_2d<WeaponV3<true>, true>(this->r, this->offsets_v3_be->weapon_table, data1_1, data1_2).to_v4(); def_v4 = indirect_lookup_2d<WeaponV3BE, true>(this->r, this->offsets_v3_be->weapon_table, data1_1, data1_2).to_v4();
} else { } else {
throw logic_error("table is not v2, v3, or v4"); throw logic_error("table is not v2, v3, or v4");
} }
@@ -528,11 +528,11 @@ const ItemParameterTable::ArmorOrShieldV4& ItemParameterTable::get_armor_or_shie
} else if (this->offsets_v1_v2) { } else if (this->offsets_v1_v2) {
def_v4 = indirect_lookup_2d<ArmorOrShieldV1V2, false>(this->r, this->offsets_v1_v2->armor_table, data1_1 - 1, data1_2).to_v4(); def_v4 = indirect_lookup_2d<ArmorOrShieldV1V2, false>(this->r, this->offsets_v1_v2->armor_table, data1_1 - 1, data1_2).to_v4();
} else if (this->offsets_gc_nte) { } else if (this->offsets_gc_nte) {
def_v4 = indirect_lookup_2d<ArmorOrShieldV3<true>, true>(this->r, this->offsets_gc_nte->armor_table, data1_1 - 1, data1_2).to_v4(); def_v4 = indirect_lookup_2d<ArmorOrShieldV3BE, true>(this->r, this->offsets_gc_nte->armor_table, data1_1 - 1, data1_2).to_v4();
} else if (this->offsets_v3_le) { } else if (this->offsets_v3_le) {
def_v4 = indirect_lookup_2d<ArmorOrShieldV3<false>, false>(this->r, this->offsets_v3_le->armor_table, data1_1 - 1, data1_2).to_v4(); def_v4 = indirect_lookup_2d<ArmorOrShieldV3, false>(this->r, this->offsets_v3_le->armor_table, data1_1 - 1, data1_2).to_v4();
} else if (this->offsets_v3_be) { } else if (this->offsets_v3_be) {
def_v4 = indirect_lookup_2d<ArmorOrShieldV3<true>, true>(this->r, this->offsets_v3_be->armor_table, data1_1 - 1, data1_2).to_v4(); def_v4 = indirect_lookup_2d<ArmorOrShieldV3BE, true>(this->r, this->offsets_v3_be->armor_table, data1_1 - 1, data1_2).to_v4();
} else { } else {
throw logic_error("table is not v2, v3, or v4"); throw logic_error("table is not v2, v3, or v4");
} }
@@ -580,11 +580,11 @@ const ItemParameterTable::UnitV4& ItemParameterTable::get_unit(uint8_t data1_2)
} else if (this->offsets_v1_v2) { } else if (this->offsets_v1_v2) {
def_v4 = indirect_lookup_2d<UnitV1V2, false>(this->r, this->offsets_v1_v2->unit_table, 0, data1_2).to_v4(); def_v4 = indirect_lookup_2d<UnitV1V2, false>(this->r, this->offsets_v1_v2->unit_table, 0, data1_2).to_v4();
} else if (this->offsets_gc_nte) { } else if (this->offsets_gc_nte) {
def_v4 = indirect_lookup_2d<UnitV3<true>, true>(this->r, this->offsets_gc_nte->unit_table, 0, data1_2).to_v4(); def_v4 = indirect_lookup_2d<UnitV3BE, true>(this->r, this->offsets_gc_nte->unit_table, 0, data1_2).to_v4();
} else if (this->offsets_v3_le) { } else if (this->offsets_v3_le) {
def_v4 = indirect_lookup_2d<UnitV3<false>, false>(this->r, this->offsets_v3_le->unit_table, 0, data1_2).to_v4(); def_v4 = indirect_lookup_2d<UnitV3, false>(this->r, this->offsets_v3_le->unit_table, 0, data1_2).to_v4();
} else if (this->offsets_v3_be) { } else if (this->offsets_v3_be) {
def_v4 = indirect_lookup_2d<UnitV3<true>, true>(this->r, this->offsets_v3_be->unit_table, 0, data1_2).to_v4(); def_v4 = indirect_lookup_2d<UnitV3BE, true>(this->r, this->offsets_v3_be->unit_table, 0, data1_2).to_v4();
} else { } else {
throw logic_error("table is not v2, v3, or v4"); throw logic_error("table is not v2, v3, or v4");
} }
@@ -636,11 +636,11 @@ const ItemParameterTable::MagV4& ItemParameterTable::get_mag(uint8_t data1_1) co
def_v4 = indirect_lookup_2d<MagV2, false>(this->r, this->offsets_v1_v2->mag_table, 0, data1_1).to_v4(); def_v4 = indirect_lookup_2d<MagV2, false>(this->r, this->offsets_v1_v2->mag_table, 0, data1_1).to_v4();
} }
} else if (this->offsets_gc_nte) { } else if (this->offsets_gc_nte) {
def_v4 = indirect_lookup_2d<MagV3<true>, true>(this->r, this->offsets_gc_nte->mag_table, 0, data1_1).to_v4(); def_v4 = indirect_lookup_2d<MagV3BE, true>(this->r, this->offsets_gc_nte->mag_table, 0, data1_1).to_v4();
} else if (this->offsets_v3_le) { } else if (this->offsets_v3_le) {
def_v4 = indirect_lookup_2d<MagV3<false>, false>(this->r, this->offsets_v3_le->mag_table, 0, data1_1).to_v4(); def_v4 = indirect_lookup_2d<MagV3, false>(this->r, this->offsets_v3_le->mag_table, 0, data1_1).to_v4();
} else if (this->offsets_v3_be) { } else if (this->offsets_v3_be) {
def_v4 = indirect_lookup_2d<MagV3<true>, true>(this->r, this->offsets_v3_be->mag_table, 0, data1_1).to_v4(); def_v4 = indirect_lookup_2d<MagV3BE, true>(this->r, this->offsets_v3_be->mag_table, 0, data1_1).to_v4();
} else { } else {
throw logic_error("table is not v2, v3, or v4"); throw logic_error("table is not v2, v3, or v4");
} }
@@ -693,11 +693,11 @@ const ItemParameterTable::ToolV4& ItemParameterTable::get_tool(uint8_t data1_1,
} else if (this->offsets_v1_v2) { } else if (this->offsets_v1_v2) {
def_v4 = indirect_lookup_2d<ToolV1V2, false>(this->r, this->offsets_v1_v2->tool_table, data1_1, data1_2).to_v4(); def_v4 = indirect_lookup_2d<ToolV1V2, false>(this->r, this->offsets_v1_v2->tool_table, data1_1, data1_2).to_v4();
} else if (this->offsets_gc_nte) { } else if (this->offsets_gc_nte) {
def_v4 = indirect_lookup_2d<ToolV3<true>, true>(this->r, this->offsets_gc_nte->tool_table, data1_1, data1_2).to_v4(); def_v4 = indirect_lookup_2d<ToolV3BE, true>(this->r, this->offsets_gc_nte->tool_table, data1_1, data1_2).to_v4();
} else if (this->offsets_v3_le) { } else if (this->offsets_v3_le) {
def_v4 = indirect_lookup_2d<ToolV3<false>, false>(this->r, this->offsets_v3_le->tool_table, data1_1, data1_2).to_v4(); def_v4 = indirect_lookup_2d<ToolV3, false>(this->r, this->offsets_v3_le->tool_table, data1_1, data1_2).to_v4();
} else if (this->offsets_v3_be) { } else if (this->offsets_v3_be) {
def_v4 = indirect_lookup_2d<ToolV3<true>, true>(this->r, this->offsets_v3_be->tool_table, data1_1, data1_2).to_v4(); def_v4 = indirect_lookup_2d<ToolV3BE, true>(this->r, this->offsets_v3_be->tool_table, data1_1, data1_2).to_v4();
} else { } else {
throw logic_error("table is not v2, v3, or v4"); throw logic_error("table is not v2, v3, or v4");
} }
@@ -706,13 +706,13 @@ const ItemParameterTable::ToolV4& ItemParameterTable::get_tool(uint8_t data1_1,
} }
} }
template <typename ToolT, bool IsBigEndian> template <typename ToolDefT, bool IsBigEndian>
pair<uint8_t, uint8_t> ItemParameterTable::find_tool_by_id_t(uint32_t tool_table_offset, uint32_t item_id) const { pair<uint8_t, uint8_t> ItemParameterTable::find_tool_by_id_t(uint32_t tool_table_offset, uint32_t item_id) const {
const auto* cos = &this->r.pget<ArrayRef<IsBigEndian>>( const auto* cos = &this->r.pget<ArrayRefT<IsBigEndian>>(
tool_table_offset, this->num_tool_classes * sizeof(ArrayRef<IsBigEndian>)); tool_table_offset, this->num_tool_classes * sizeof(ArrayRefT<IsBigEndian>));
for (size_t z = 0; z < this->num_tool_classes; z++) { for (size_t z = 0; z < this->num_tool_classes; z++) {
const auto& co = cos[z]; const auto& co = cos[z];
const auto* defs = &this->r.pget<ToolT>(co.offset, sizeof(ToolT) * co.count); const auto* defs = &this->r.pget<ToolDefT>(co.offset, sizeof(ToolDefT) * co.count);
for (size_t y = 0; y < co.count; y++) { for (size_t y = 0; y < co.count; y++) {
if (defs[y].base.id == item_id) { if (defs[y].base.id == item_id) {
return make_pair(z, y); return make_pair(z, y);
@@ -728,11 +728,11 @@ pair<uint8_t, uint8_t> ItemParameterTable::find_tool_by_id(uint32_t item_id) con
} else if (this->offsets_v1_v2) { } else if (this->offsets_v1_v2) {
return this->find_tool_by_id_t<ToolV1V2, false>(this->offsets_v1_v2->tool_table, item_id); return this->find_tool_by_id_t<ToolV1V2, false>(this->offsets_v1_v2->tool_table, item_id);
} else if (this->offsets_gc_nte) { } else if (this->offsets_gc_nte) {
return this->find_tool_by_id_t<ToolV3<true>, true>(this->offsets_gc_nte->tool_table, item_id); return this->find_tool_by_id_t<ToolV3BE, true>(this->offsets_gc_nte->tool_table, item_id);
} else if (this->offsets_v3_le) { } else if (this->offsets_v3_le) {
return this->find_tool_by_id_t<ToolV3<false>, false>(this->offsets_v3_le->tool_table, item_id); return this->find_tool_by_id_t<ToolV3, false>(this->offsets_v3_le->tool_table, item_id);
} else if (this->offsets_v3_be) { } else if (this->offsets_v3_be) {
return this->find_tool_by_id_t<ToolV3<true>, true>(this->offsets_v3_be->tool_table, item_id); return this->find_tool_by_id_t<ToolV3BE, true>(this->offsets_v3_be->tool_table, item_id);
} else if (this->offsets_v4) { } else if (this->offsets_v4) {
return this->find_tool_by_id_t<ToolV4, false>(this->offsets_v4->tool_table, item_id); return this->find_tool_by_id_t<ToolV4, false>(this->offsets_v4->tool_table, item_id);
} else { } else {
@@ -752,7 +752,7 @@ float ItemParameterTable::get_sale_divisor_t(const OffsetsT* offsets, uint8_t da
return this->r.pget<FloatT>(offsets->weapon_sale_divisor_table + data1_1 * sizeof(FloatT)); return this->r.pget<FloatT>(offsets->weapon_sale_divisor_table + data1_1 * sizeof(FloatT));
case 1: { case 1: {
const auto& divisors = this->r.pget<NonWeaponSaleDivisors<IsBigEndian>>(offsets->sale_divisor_table); const auto& divisors = this->r.pget<NonWeaponSaleDivisorsT<IsBigEndian>>(offsets->sale_divisor_table);
switch (data1_1) { switch (data1_1) {
case 1: case 1:
return divisors.armor_divisor; return divisors.armor_divisor;
@@ -765,7 +765,7 @@ float ItemParameterTable::get_sale_divisor_t(const OffsetsT* offsets, uint8_t da
} }
case 2: { case 2: {
const auto& divisors = this->r.pget<NonWeaponSaleDivisors<IsBigEndian>>(offsets->sale_divisor_table); const auto& divisors = this->r.pget<NonWeaponSaleDivisorsT<IsBigEndian>>(offsets->sale_divisor_table);
return divisors.mag_divisor; return divisors.mag_divisor;
} }
@@ -803,22 +803,22 @@ const ItemParameterTable::MagFeedResult& ItemParameterTable::get_mag_feed_result
uint32_t offset; uint32_t offset;
if (this->offsets_dc_protos) { if (this->offsets_dc_protos) {
const auto& table_offsets = this->r.pget<MagFeedResultsListOffsets<false>>(this->offsets_dc_protos->mag_feed_table); const auto& table_offsets = this->r.pget<MagFeedResultsListOffsets>(this->offsets_dc_protos->mag_feed_table);
offset = table_offsets.offsets[table_index]; offset = table_offsets.offsets[table_index];
} else if (this->offsets_v1_v2) { } else if (this->offsets_v1_v2) {
const auto& table_offsets = this->r.pget<MagFeedResultsListOffsets<false>>(this->offsets_v1_v2->mag_feed_table); const auto& table_offsets = this->r.pget<MagFeedResultsListOffsets>(this->offsets_v1_v2->mag_feed_table);
offset = table_offsets.offsets[table_index]; offset = table_offsets.offsets[table_index];
} else if (this->offsets_gc_nte) { } else if (this->offsets_gc_nte) {
const auto& table_offsets = this->r.pget<MagFeedResultsListOffsets<true>>(this->offsets_gc_nte->mag_feed_table); const auto& table_offsets = this->r.pget<MagFeedResultsListOffsetsBE>(this->offsets_gc_nte->mag_feed_table);
offset = table_offsets.offsets[table_index]; offset = table_offsets.offsets[table_index];
} else if (this->offsets_v3_le) { } else if (this->offsets_v3_le) {
const auto& table_offsets = this->r.pget<MagFeedResultsListOffsets<false>>(this->offsets_v3_le->mag_feed_table); const auto& table_offsets = this->r.pget<MagFeedResultsListOffsets>(this->offsets_v3_le->mag_feed_table);
offset = table_offsets.offsets[table_index]; offset = table_offsets.offsets[table_index];
} else if (this->offsets_v3_be) { } else if (this->offsets_v3_be) {
const auto& table_offsets = this->r.pget<MagFeedResultsListOffsets<true>>(this->offsets_v3_be->mag_feed_table); const auto& table_offsets = this->r.pget<MagFeedResultsListOffsetsBE>(this->offsets_v3_be->mag_feed_table);
offset = table_offsets.offsets[table_index]; offset = table_offsets.offsets[table_index];
} else if (this->offsets_v4) { } else if (this->offsets_v4) {
const auto& table_offsets = this->r.pget<MagFeedResultsListOffsets<false>>(this->offsets_v4->mag_feed_table); const auto& table_offsets = this->r.pget<MagFeedResultsListOffsets>(this->offsets_v4->mag_feed_table);
offset = table_offsets.offsets[table_index]; offset = table_offsets.offsets[table_index];
} else { } else {
throw logic_error("table is not v2, v3, or v4"); throw logic_error("table is not v2, v3, or v4");
@@ -856,24 +856,24 @@ uint8_t ItemParameterTable::get_special_stars(uint8_t special) const {
: 0; : 0;
} }
const ItemParameterTable::Special<false>& ItemParameterTable::get_special(uint8_t special) const { const ItemParameterTable::Special& ItemParameterTable::get_special(uint8_t special) const {
special &= 0x3F; special &= 0x3F;
if (special >= this->num_specials) { if (special >= this->num_specials) {
throw out_of_range("invalid special index"); throw out_of_range("invalid special index");
} }
if (this->offsets_dc_protos) { if (this->offsets_dc_protos) {
return this->r.pget<Special<false>>(this->offsets_dc_protos->special_data_table + sizeof(Special<false>) * special); return this->r.pget<Special>(this->offsets_dc_protos->special_data_table + sizeof(Special) * special);
} else if (this->offsets_v1_v2) { } else if (this->offsets_v1_v2) {
return this->r.pget<Special<false>>(this->offsets_v1_v2->special_data_table + sizeof(Special<false>) * special); return this->r.pget<Special>(this->offsets_v1_v2->special_data_table + sizeof(Special) * special);
} else if (this->offsets_v3_le) { } else if (this->offsets_v3_le) {
return this->r.pget<Special<false>>(this->offsets_v3_le->special_data_table + sizeof(Special<false>) * special); return this->r.pget<Special>(this->offsets_v3_le->special_data_table + sizeof(Special) * special);
} else if (this->offsets_gc_nte) { } else if (this->offsets_gc_nte) {
if ((special >= this->parsed_specials.size()) || (this->parsed_specials[special].type != 0xFFFF)) { if ((special >= this->parsed_specials.size()) || (this->parsed_specials[special].type != 0xFFFF)) {
if (special >= this->parsed_specials.size()) { if (special >= this->parsed_specials.size()) {
this->parsed_specials.resize(special + 1); this->parsed_specials.resize(special + 1);
} }
const auto& sp_be = this->r.pget<Special<true>>(this->offsets_gc_nte->special_data_table + sizeof(Special<true>) * special); const auto& sp_be = this->r.pget<SpecialBE>(this->offsets_gc_nte->special_data_table + sizeof(SpecialBE) * special);
this->parsed_specials[special].type = sp_be.type.load(); this->parsed_specials[special].type = sp_be.type.load();
this->parsed_specials[special].amount = sp_be.amount.load(); this->parsed_specials[special].amount = sp_be.amount.load();
} }
@@ -883,13 +883,13 @@ const ItemParameterTable::Special<false>& ItemParameterTable::get_special(uint8_
if (special >= this->parsed_specials.size()) { if (special >= this->parsed_specials.size()) {
this->parsed_specials.resize(special + 1); this->parsed_specials.resize(special + 1);
} }
const auto& sp_be = this->r.pget<Special<true>>(this->offsets_v3_be->special_data_table + sizeof(Special<true>) * special); const auto& sp_be = this->r.pget<SpecialBE>(this->offsets_v3_be->special_data_table + sizeof(SpecialBE) * special);
this->parsed_specials[special].type = sp_be.type.load(); this->parsed_specials[special].type = sp_be.type.load();
this->parsed_specials[special].amount = sp_be.amount.load(); this->parsed_specials[special].amount = sp_be.amount.load();
} }
return this->parsed_specials[special]; return this->parsed_specials[special];
} else if (this->offsets_v4) { } else if (this->offsets_v4) {
return this->r.pget<Special<false>>(this->offsets_v4->special_data_table + sizeof(Special<false>) * special); return this->r.pget<Special>(this->offsets_v4->special_data_table + sizeof(Special) * special);
} else { } else {
throw logic_error("table is not v2, v3, or v4"); throw logic_error("table is not v2, v3, or v4");
} }
@@ -1008,20 +1008,21 @@ uint8_t ItemParameterTable::get_item_base_stars(const ItemData& item) const {
const auto& def = (item.data1[1] == 2) const auto& def = (item.data1[1] == 2)
? this->get_tool(2, item.data1[4]) ? this->get_tool(2, item.data1[4])
: this->get_tool(item.data1[1], item.data1[2]); : this->get_tool(item.data1[1], item.data1[2]);
return (def.item_flag & 0x80) ? 12 : 0; return (def.item_flags & 0x80) ? 12 : 0;
} else { } else {
return 0; return 0;
} }
} }
uint8_t ItemParameterTable::get_item_adjusted_stars(const ItemData& item) const { uint8_t ItemParameterTable::get_item_adjusted_stars(const ItemData& item, bool ignore_unidentified) const {
uint8_t ret = this->get_item_base_stars(item); uint8_t ret = this->get_item_base_stars(item);
if (item.data1[0] == 0) { if (item.data1[0] == 0) {
bool is_unidentified = (!ignore_unidentified) && (item.data1[4] & 0x80);
if (ret < 9) { if (ret < 9) {
if (!(item.data1[4] & 0x80)) { if (!is_unidentified) {
ret += this->get_special_stars(item.data1[4]); ret += this->get_special_stars(item.data1[4]);
} }
} else if (item.data1[4] & 0x80) { } else if (is_unidentified) {
ret = 0; ret = 0;
} }
} else if (item.data1[0] == 1) { } else if (item.data1[0] == 1) {
@@ -1050,7 +1051,7 @@ bool ItemParameterTable::is_unsealable_item(uint8_t data1_0, uint8_t data1_1, ui
if (this->offsets_dc_protos || this->offsets_v1_v2 || this->offsets_gc_nte) { if (this->offsets_dc_protos || this->offsets_v1_v2 || this->offsets_gc_nte) {
return false; return false;
} else if (this->offsets_v3_le) { } else if (this->offsets_v3_le) {
const auto& co = this->r.pget<ArrayRefLE>(this->offsets_v3_le->unsealable_table); const auto& co = this->r.pget<ArrayRef>(this->offsets_v3_le->unsealable_table);
offset = co.offset; offset = co.offset;
count = co.count; count = co.count;
} else if (this->offsets_v3_be) { } else if (this->offsets_v3_be) {
@@ -1058,7 +1059,7 @@ bool ItemParameterTable::is_unsealable_item(uint8_t data1_0, uint8_t data1_1, ui
offset = co.offset; offset = co.offset;
count = co.count; count = co.count;
} else if (this->offsets_v4) { } else if (this->offsets_v4) {
const auto& co = this->r.pget<ArrayRefLE>(this->offsets_v4->unsealable_table); const auto& co = this->r.pget<ArrayRef>(this->offsets_v4->unsealable_table);
offset = co.offset; offset = co.offset;
count = co.count; count = co.count;
} else { } else {
@@ -1110,7 +1111,7 @@ const std::map<uint32_t, std::vector<ItemParameterTable::ItemCombination>>& Item
static const std::map<uint32_t, std::vector<ItemParameterTable::ItemCombination>> empty_map; static const std::map<uint32_t, std::vector<ItemParameterTable::ItemCombination>> empty_map;
return empty_map; return empty_map;
} else if (this->offsets_v3_le) { } else if (this->offsets_v3_le) {
const auto& co = this->r.pget<ArrayRefLE>(this->offsets_v3_le->combination_table); const auto& co = this->r.pget<ArrayRef>(this->offsets_v3_le->combination_table);
offset = co.offset; offset = co.offset;
count = co.count; count = co.count;
} else if (this->offsets_v3_be) { } else if (this->offsets_v3_be) {
@@ -1118,7 +1119,7 @@ const std::map<uint32_t, std::vector<ItemParameterTable::ItemCombination>>& Item
offset = co.offset; offset = co.offset;
count = co.count; count = co.count;
} else if (this->offsets_v4) { } else if (this->offsets_v4) {
const auto& co = this->r.pget<ArrayRefLE>(this->offsets_v4->combination_table); const auto& co = this->r.pget<ArrayRef>(this->offsets_v4->combination_table);
offset = co.offset; offset = co.offset;
count = co.count; count = co.count;
} else { } else {
@@ -1137,7 +1138,7 @@ const std::map<uint32_t, std::vector<ItemParameterTable::ItemCombination>>& Item
template <bool IsBigEndian> template <bool IsBigEndian>
size_t ItemParameterTable::num_events_t(uint32_t base_offset) const { size_t ItemParameterTable::num_events_t(uint32_t base_offset) const {
return this->r.pget<ArrayRef<IsBigEndian>>(base_offset).count; return this->r.pget<ArrayRefT<IsBigEndian>>(base_offset).count;
} }
size_t ItemParameterTable::num_events() const { size_t ItemParameterTable::num_events() const {
@@ -1157,11 +1158,11 @@ size_t ItemParameterTable::num_events() const {
template <bool IsBigEndian> template <bool IsBigEndian>
std::pair<const ItemParameterTable::EventItem*, size_t> ItemParameterTable::get_event_items_t( std::pair<const ItemParameterTable::EventItem*, size_t> ItemParameterTable::get_event_items_t(
uint32_t base_offset, uint8_t event_number) const { uint32_t base_offset, uint8_t event_number) const {
const auto& co = this->r.pget<ArrayRef<IsBigEndian>>(base_offset); const auto& co = this->r.pget<ArrayRefT<IsBigEndian>>(base_offset);
if (event_number >= co.count) { if (event_number >= co.count) {
throw out_of_range("invalid event number"); throw out_of_range("invalid event number");
} }
const auto& event_co = this->r.pget<ArrayRef<IsBigEndian>>(co.offset + sizeof(ArrayRef<IsBigEndian>) * event_number); const auto& event_co = this->r.pget<ArrayRefT<IsBigEndian>>(co.offset + sizeof(ArrayRefT<IsBigEndian>) * event_number);
const auto* defs = &this->r.pget<EventItem>(event_co.offset, event_co.count * sizeof(EventItem)); const auto* defs = &this->r.pget<EventItem>(event_co.offset, event_co.count * sizeof(EventItem));
return make_pair(defs, event_co.count); return make_pair(defs, event_co.count);
} }
+158 -107
View File
@@ -19,42 +19,43 @@ public:
// being null or not in each public function. Rewrite this and make it better. // being null or not in each public function. Rewrite this and make it better.
template <bool IsBigEndian> template <bool IsBigEndian>
struct ArrayRef { struct ArrayRefT {
using U32T = typename std::conditional<IsBigEndian, be_uint32_t, le_uint32_t>::type; using U32T = typename std::conditional<IsBigEndian, be_uint32_t, le_uint32_t>::type;
/* 00 */ U32T count; /* 00 */ U32T count;
/* 04 */ U32T offset; /* 04 */ U32T offset;
/* 08 */ /* 08 */
} __attribute__((packed)); } __packed__;
struct ArrayRefLE : ArrayRef<false> {
} __attribute__((packed)); using ArrayRef = ArrayRefT<false>;
struct ArrayRefBE : ArrayRef<true> { using ArrayRefBE = ArrayRefT<true>;
} __attribute__((packed)); check_struct_size(ArrayRef, 8);
check_struct_size(ArrayRefBE, 8);
template <bool IsBigEndian> template <bool IsBigEndian>
struct ItemBaseV2 { struct ItemBaseV2T {
using U32T = typename std::conditional<IsBigEndian, be_uint32_t, le_uint32_t>::type; using U32T = typename std::conditional<IsBigEndian, be_uint32_t, le_uint32_t>::type;
// id specifies several things; notably, it doubles as the index of the // id specifies several things; notably, it doubles as the index of the
// item's name in the text archive (e.g. TextEnglish) collection 0. // item's name in the text archive (e.g. TextEnglish) collection 0.
/* 00 */ U32T id = 0xFFFFFFFF; /* 00 */ U32T id = 0xFFFFFFFF;
/* 04 */ /* 04 */
} __attribute__((packed)); } __packed__;
template <bool IsBigEndian> template <bool IsBigEndian>
struct ItemBaseV3 : ItemBaseV2<IsBigEndian> { struct ItemBaseV3T : ItemBaseV2T<IsBigEndian> {
using U16T = typename std::conditional<IsBigEndian, be_uint16_t, le_uint16_t>::type; using U16T = typename std::conditional<IsBigEndian, be_uint16_t, le_uint16_t>::type;
/* 04 */ U16T type = 0; /* 04 */ U16T type = 0;
/* 06 */ U16T skin = 0; /* 06 */ U16T skin = 0;
/* 08 */ /* 08 */
} __attribute__((packed)); } __packed__;
template <bool IsBigEndian> template <bool IsBigEndian>
struct ItemBaseV4 : ItemBaseV3<IsBigEndian> { struct ItemBaseV4T : ItemBaseV3T<IsBigEndian> {
using U32T = typename std::conditional<IsBigEndian, be_uint32_t, le_uint32_t>::type; using U32T = typename std::conditional<IsBigEndian, be_uint32_t, le_uint32_t>::type;
/* 08 */ U32T team_points = 0; /* 08 */ U32T team_points = 0;
/* 0C */ /* 0C */
} __attribute__((packed)); } __packed__;
struct WeaponV4; struct WeaponV4;
struct WeaponDCProtos { struct WeaponDCProtos {
/* 00 */ ItemBaseV2<false> base; /* 00 */ ItemBaseV2T<false> base;
/* 04 */ le_uint16_t class_flags = 0; /* 04 */ le_uint16_t class_flags = 0;
/* 06 */ le_uint16_t atp_min = 0; /* 06 */ le_uint16_t atp_min = 0;
/* 08 */ le_uint16_t atp_max = 0; /* 08 */ le_uint16_t atp_max = 0;
@@ -68,10 +69,10 @@ public:
/* 14 */ /* 14 */
WeaponV4 to_v4() const; WeaponV4 to_v4() const;
} __attribute__((packed)); } __packed_ws__(WeaponDCProtos, 0x14);
struct WeaponV1V2 { struct WeaponV1V2 {
/* 00 */ ItemBaseV2<false> base; /* 00 */ ItemBaseV2T<false> base;
/* 04 */ le_uint16_t class_flags = 0; /* 04 */ le_uint16_t class_flags = 0;
/* 06 */ le_uint16_t atp_min = 0; /* 06 */ le_uint16_t atp_min = 0;
/* 08 */ le_uint16_t atp_max = 0; /* 08 */ le_uint16_t atp_max = 0;
@@ -87,10 +88,10 @@ public:
/* 18 */ /* 18 */
WeaponV4 to_v4() const; WeaponV4 to_v4() const;
} __attribute__((packed)); } __packed_ws__(WeaponV1V2, 0x18);
struct WeaponGCNTE { struct WeaponGCNTE {
/* 00 */ ItemBaseV3<true> base; /* 00 */ ItemBaseV3T<true> base;
/* 08 */ be_uint16_t class_flags = 0; /* 08 */ be_uint16_t class_flags = 0;
/* 0A */ be_uint16_t atp_min = 0; /* 0A */ be_uint16_t atp_min = 0;
/* 0C */ be_uint16_t atp_max = 0; /* 0C */ be_uint16_t atp_max = 0;
@@ -115,12 +116,12 @@ public:
/* 24 */ /* 24 */
WeaponV4 to_v4() const; WeaponV4 to_v4() const;
} __attribute__((packed)); } __packed_ws__(WeaponGCNTE, 0x24);
template <bool IsBigEndian> template <bool IsBigEndian>
struct WeaponV3 { struct WeaponV3T {
using U16T = typename std::conditional<IsBigEndian, be_uint16_t, le_uint16_t>::type; using U16T = typename std::conditional<IsBigEndian, be_uint16_t, le_uint16_t>::type;
/* 00 */ ItemBaseV3<IsBigEndian> base; /* 00 */ ItemBaseV3T<IsBigEndian> base;
/* 08 */ U16T class_flags = 0; /* 08 */ U16T class_flags = 0;
/* 0A */ U16T atp_min = 0; /* 0A */ U16T atp_min = 0;
/* 0C */ U16T atp_max = 0; /* 0C */ U16T atp_max = 0;
@@ -149,10 +150,15 @@ public:
/* 28 */ /* 28 */
WeaponV4 to_v4() const; WeaponV4 to_v4() const;
} __attribute__((packed)); } __packed__;
using WeaponV3 = WeaponV3T<false>;
using WeaponV3BE = WeaponV3T<true>;
check_struct_size(WeaponV3, 0x28);
check_struct_size(WeaponV3BE, 0x28);
struct WeaponV4 { struct WeaponV4 {
/* 00 */ ItemBaseV4<false> base; /* 00 */ ItemBaseV4T<false> base;
/* 0C */ le_uint16_t class_flags = 0x00FF; /* 0C */ le_uint16_t class_flags = 0x00FF;
/* 0E */ le_uint16_t atp_min = 0; /* 0E */ le_uint16_t atp_min = 0;
/* 10 */ le_uint16_t atp_max = 0; /* 10 */ le_uint16_t atp_max = 0;
@@ -179,10 +185,10 @@ public:
/* 2A */ uint8_t tech_boost = 0; /* 2A */ uint8_t tech_boost = 0;
/* 2B */ uint8_t combo_type = 0; /* 2B */ uint8_t combo_type = 0;
/* 2C */ /* 2C */
} __attribute__((packed)); } __packed_ws__(WeaponV4, 0x2C);
template <typename BaseT, bool IsBigEndian> template <typename BaseT, bool IsBigEndian>
struct ArmorOrShield { struct ArmorOrShieldT {
using U16T = typename std::conditional<IsBigEndian, be_uint16_t, le_uint16_t>::type; using U16T = typename std::conditional<IsBigEndian, be_uint16_t, le_uint16_t>::type;
/* V1/V2 offsets */ /* V1/V2 offsets */
/* 00 */ BaseT base; /* 00 */ BaseT base;
@@ -200,33 +206,36 @@ public:
/* 12 */ uint8_t dfp_range = 0; /* 12 */ uint8_t dfp_range = 0;
/* 13 */ uint8_t evp_range = 0; /* 13 */ uint8_t evp_range = 0;
/* 14 */ /* 14 */
} __attribute__((packed)); } __packed__;
struct ArmorOrShieldV4;
struct ArmorOrShieldDCProtos : ArmorOrShield<ItemBaseV2<false>, false> {
ArmorOrShieldV4 to_v4() const;
} __attribute__((packed));
template <typename BaseT, bool IsBigEndian> template <typename BaseT, bool IsBigEndian>
struct ArmorOrShieldFinal : ArmorOrShield<BaseT, IsBigEndian> { struct ArmorOrShieldFinalT : ArmorOrShieldT<BaseT, IsBigEndian> {
using U16T = typename std::conditional<IsBigEndian, be_uint16_t, le_uint16_t>::type; using U16T = typename std::conditional<IsBigEndian, be_uint16_t, le_uint16_t>::type;
/* 14 */ uint8_t stat_boost = 0; /* 14 */ uint8_t stat_boost = 0;
/* 15 */ uint8_t tech_boost = 0; /* 15 */ uint8_t tech_boost = 0;
/* 16 */ U16T unknown_a2 = 0; /* 16 */ U16T unknown_a2 = 0;
/* 18 */ /* 18 */
} __attribute__((packed)); } __packed__;
using ArmorOrShieldV4 = ArmorOrShieldFinalT<ItemBaseV4T<false>, false>;
check_struct_size(ArmorOrShieldV4, 0x20);
struct ArmorOrShieldDCProtos : ArmorOrShieldT<ItemBaseV2T<false>, false> {
ArmorOrShieldV4 to_v4() const;
} __packed_ws__(ArmorOrShieldDCProtos, 0x14);
struct ArmorOrShieldV1V2 : ArmorOrShieldFinal<ItemBaseV2<false>, false> { struct ArmorOrShieldV1V2 : ArmorOrShieldFinalT<ItemBaseV2T<false>, false> {
ArmorOrShieldV4 to_v4() const; ArmorOrShieldV4 to_v4() const;
} __attribute__((packed)); } __packed_ws__(ArmorOrShieldV1V2, 0x18);
template <bool IsBigEndian> template <bool IsBigEndian>
struct ArmorOrShieldV3 : ArmorOrShieldFinal<ItemBaseV3<IsBigEndian>, IsBigEndian> { struct ArmorOrShieldV3T : ArmorOrShieldFinalT<ItemBaseV3T<IsBigEndian>, IsBigEndian> {
ArmorOrShieldV4 to_v4() const; ArmorOrShieldV4 to_v4() const;
} __attribute__((packed)); } __packed__;
struct ArmorOrShieldV4 : ArmorOrShieldFinal<ItemBaseV4<false>, false> { using ArmorOrShieldV3 = ArmorOrShieldV3T<false>;
} __attribute__((packed)); using ArmorOrShieldV3BE = ArmorOrShieldV3T<true>;
check_struct_size(ArmorOrShieldV3, 0x1C);
check_struct_size(ArmorOrShieldV3BE, 0x1C);
template <typename BaseT, bool IsBigEndian> template <typename BaseT, bool IsBigEndian>
struct Unit { struct UnitT {
using U16T = typename std::conditional<IsBigEndian, be_uint16_t, le_uint16_t>::type; using U16T = typename std::conditional<IsBigEndian, be_uint16_t, le_uint16_t>::type;
using S16T = typename std::conditional<IsBigEndian, be_int16_t, le_int16_t>::type; using S16T = typename std::conditional<IsBigEndian, be_int16_t, le_int16_t>::type;
/* V1/V2 offsets */ /* V1/V2 offsets */
@@ -234,31 +243,34 @@ public:
/* 04 */ U16T stat = 0; /* 04 */ U16T stat = 0;
/* 06 */ U16T stat_amount = 0; /* 06 */ U16T stat_amount = 0;
/* 08 */ /* 08 */
} __attribute__((packed)); } __packed__;
struct UnitV4;
struct UnitDCProtos : Unit<ItemBaseV2<false>, false> {
UnitV4 to_v4() const;
} __attribute__((packed));
template <typename BaseT, bool IsBigEndian> template <typename BaseT, bool IsBigEndian>
struct UnitFinal : Unit<BaseT, IsBigEndian> { struct UnitFinalT : UnitT<BaseT, IsBigEndian> {
using S16T = typename std::conditional<IsBigEndian, be_int16_t, le_int16_t>::type; using S16T = typename std::conditional<IsBigEndian, be_int16_t, le_int16_t>::type;
/* 08 */ S16T modifier_amount = 0; /* 08 */ S16T modifier_amount = 0;
/* 0A */ parray<uint8_t, 2> unused; /* 0A */ parray<uint8_t, 2> unused;
/* 0C */ /* 0C */
} __attribute__((packed)); } __packed__;
struct UnitV1V2 : UnitFinal<ItemBaseV2<false>, false> { using UnitV4 = UnitFinalT<ItemBaseV4T<false>, false>;
check_struct_size(UnitV4, 0x14);
struct UnitDCProtos : UnitT<ItemBaseV2T<false>, false> {
UnitV4 to_v4() const; UnitV4 to_v4() const;
} __attribute__((packed)); } __packed_ws__(UnitDCProtos, 0x08);
struct UnitV1V2 : UnitFinalT<ItemBaseV2T<false>, false> {
UnitV4 to_v4() const;
} __packed_ws__(UnitV1V2, 0x0C);
template <bool IsBigEndian> template <bool IsBigEndian>
struct UnitV3 : UnitFinal<ItemBaseV3<IsBigEndian>, IsBigEndian> { struct UnitV3T : UnitFinalT<ItemBaseV3T<IsBigEndian>, IsBigEndian> {
UnitV4 to_v4() const; UnitV4 to_v4() const;
} __attribute__((packed)); } __packed__;
struct UnitV4 : UnitFinal<ItemBaseV4<false>, false> { using UnitV3 = UnitV3T<false>;
} __attribute__((packed)); using UnitV3BE = UnitV3T<true>;
check_struct_size(UnitV3, 0x10);
check_struct_size(UnitV3BE, 0x10);
template <typename BaseT, bool IsBigEndian> template <typename BaseT, bool IsBigEndian>
struct Mag { struct MagT {
using U16T = typename std::conditional<IsBigEndian, be_uint16_t, le_uint16_t>::type; using U16T = typename std::conditional<IsBigEndian, be_uint16_t, le_uint16_t>::type;
/* V1/V2 offsets */ /* V1/V2 offsets */
/* 00 */ BaseT base; /* 00 */ BaseT base;
@@ -289,36 +301,38 @@ public:
/* 0E */ uint8_t on_death_flag = 0; /* 0E */ uint8_t on_death_flag = 0;
/* 0F */ uint8_t on_boss_flag = 0; /* 0F */ uint8_t on_boss_flag = 0;
/* 10 */ /* 10 */
} __attribute__((packed)); } __packed__;
struct MagV4; struct MagV4 : MagT<ItemBaseV4T<false>, false> {
struct MagV1 : Mag<ItemBaseV2<false>, false> { le_uint16_t class_flags = 0x00FF;
parray<uint8_t, 2> unused;
} __packed_ws__(MagV4, 0x1C);
struct MagV1 : MagT<ItemBaseV2T<false>, false> {
MagV4 to_v4() const; MagV4 to_v4() const;
} __attribute__((packed)); } __packed_ws__(MagV1, 0x10);
struct MagV2 : Mag<ItemBaseV2<false>, false> { struct MagV2 : MagT<ItemBaseV2T<false>, false> {
/* 10 */ le_uint16_t class_flags = 0x00FF; /* 10 */ le_uint16_t class_flags = 0x00FF;
/* 12 */ parray<uint8_t, 2> unused; /* 12 */ parray<uint8_t, 2> unused;
/* 14 */ /* 14 */
MagV4 to_v4() const; MagV4 to_v4() const;
} __attribute__((packed)); } __packed_ws__(MagV2, 0x14);
template <bool IsBigEndian> template <bool IsBigEndian>
struct MagV3 : Mag<ItemBaseV3<IsBigEndian>, IsBigEndian> { struct MagV3T : MagT<ItemBaseV3T<IsBigEndian>, IsBigEndian> {
using U16T = typename std::conditional<IsBigEndian, be_uint16_t, le_uint16_t>::type; using U16T = typename std::conditional<IsBigEndian, be_uint16_t, le_uint16_t>::type;
/* 10 */ U16T class_flags = 0x00FF; /* 10 */ U16T class_flags = 0x00FF;
/* 12 */ parray<uint8_t, 2> unused; /* 12 */ parray<uint8_t, 2> unused;
/* 14 */ /* 14 */
MagV4 to_v4() const; MagV4 to_v4() const;
} __attribute__((packed)); } __packed__;
struct MagV4 : Mag<ItemBaseV4<false>, false> { using MagV3 = MagV3T<false>;
/* 10 */ le_uint16_t class_flags = 0x00FF; using MagV3BE = MagV3T<true>;
/* 12 */ parray<uint8_t, 2> unused; check_struct_size(MagV3, 0x18);
/* 14 */ check_struct_size(MagV3BE, 0x18);
} __attribute__((packed));
template <typename BaseT, bool IsBigEndian> template <typename BaseT, bool IsBigEndian>
struct Tool { struct ToolT {
using U16T = typename std::conditional<IsBigEndian, be_uint16_t, le_uint16_t>::type; using U16T = typename std::conditional<IsBigEndian, be_uint16_t, le_uint16_t>::type;
using S32T = typename std::conditional<IsBigEndian, be_int32_t, le_int32_t>::type; using S32T = typename std::conditional<IsBigEndian, be_int32_t, le_int32_t>::type;
using U32T = typename std::conditional<IsBigEndian, be_uint32_t, le_uint32_t>::type; using U32T = typename std::conditional<IsBigEndian, be_uint32_t, le_uint32_t>::type;
@@ -327,20 +341,32 @@ public:
/* 04 */ U16T amount = 0; /* 04 */ U16T amount = 0;
/* 06 */ U16T tech = 0; /* 06 */ U16T tech = 0;
/* 08 */ S32T cost = 0; /* 08 */ S32T cost = 0;
/* 0C */ U32T item_flag = 0; // Bits in item_flags:
// 00000001 - ever usable by player ("Use" appears in inventory menu)
// 00000002 - unknown
// 00000004 - unknown
// 00000008 - usable by android characters
// 00000010 - usable in Pioneer 2 / Lab
// 00000020 - usable in boss arenas
// 00000040 - usable in Challenge mode
// 00000080 - is rare (renders as red box; V3+)
/* 0C */ U32T item_flags = 0;
/* 10 */ /* 10 */
} __attribute__((packed)); } __packed__;
struct ToolV4; using ToolV4 = ToolT<ItemBaseV4T<false>, false>;
struct ToolV1V2 : Tool<ItemBaseV2<false>, false> { check_struct_size(ToolV4, 0x18);
struct ToolV1V2 : ToolT<ItemBaseV2T<false>, false> {
ToolV4 to_v4() const; ToolV4 to_v4() const;
} __attribute__((packed)); } __packed_ws__(ToolV1V2, 0x10);
template <bool IsBigEndian> template <bool IsBigEndian>
struct ToolV3 : Tool<ItemBaseV3<IsBigEndian>, IsBigEndian> { struct ToolV3T : ToolT<ItemBaseV3T<IsBigEndian>, IsBigEndian> {
ToolV4 to_v4() const; ToolV4 to_v4() const;
} __attribute__((packed)); } __packed__;
struct ToolV4 : Tool<ItemBaseV4<false>, false> { using ToolV3 = ToolV3T<false>;
} __attribute__((packed)); using ToolV3BE = ToolV3T<true>;
check_struct_size(ToolV3, 0x14);
check_struct_size(ToolV3BE, 0x14);
struct MagFeedResult { struct MagFeedResult {
int8_t def = 0; int8_t def = 0;
@@ -350,31 +376,43 @@ public:
int8_t iq = 0; int8_t iq = 0;
int8_t synchro = 0; int8_t synchro = 0;
parray<uint8_t, 2> unused; parray<uint8_t, 2> unused;
} __attribute__((packed)); } __packed_ws__(MagFeedResult, 8);
using MagFeedResultsList = parray<MagFeedResult, 11>; using MagFeedResultsList = parray<MagFeedResult, 11>;
template <bool IsBigEndian> template <bool IsBigEndian>
struct MagFeedResultsListOffsets { struct MagFeedResultsListOffsetsT {
using U32T = typename std::conditional<IsBigEndian, be_uint32_t, le_uint32_t>::type; using U32T = typename std::conditional<IsBigEndian, be_uint32_t, le_uint32_t>::type;
parray<U32T, 8> offsets; // Offsets of MagFeedResultsList objects parray<U32T, 8> offsets; // Offsets of MagFeedResultsList objects
} __attribute__((packed)); } __packed__;
using MagFeedResultsListOffsets = MagFeedResultsListOffsetsT<false>;
using MagFeedResultsListOffsetsBE = MagFeedResultsListOffsetsT<true>;
check_struct_size(MagFeedResultsListOffsets, 0x20);
check_struct_size(MagFeedResultsListOffsetsBE, 0x20);
template <bool IsBigEndian> template <bool IsBigEndian>
struct Special { struct SpecialT {
using U16T = typename std::conditional<IsBigEndian, be_uint16_t, le_uint16_t>::type; using U16T = typename std::conditional<IsBigEndian, be_uint16_t, le_uint16_t>::type;
U16T type = 0xFFFF; U16T type = 0xFFFF;
U16T amount = 0; U16T amount = 0;
} __attribute__((packed)); } __packed__;
using Special = SpecialT<false>;
using SpecialBE = SpecialT<true>;
check_struct_size(Special, 4);
check_struct_size(SpecialBE, 4);
template <bool IsBigEndian> template <bool IsBigEndian>
struct StatBoost { struct StatBoostT {
using U16T = typename std::conditional<IsBigEndian, be_uint16_t, le_uint16_t>::type; using U16T = typename std::conditional<IsBigEndian, be_uint16_t, le_uint16_t>::type;
uint8_t stat1 = 0; uint8_t stat1 = 0;
uint8_t stat2 = 0; uint8_t stat2 = 0;
U16T amount1 = 0; U16T amount1 = 0;
U16T amount2 = 0; U16T amount2 = 0;
} __attribute__((packed)); } __packed__;
using StatBoost = StatBoostT<false>;
using StatBoostBE = StatBoostT<true>;
check_struct_size(StatBoost, 6);
check_struct_size(StatBoostBE, 6);
// Indexed as [tech_num][char_class] // Indexed as [tech_num][char_class]
using MaxTechniqueLevels = parray<parray<uint8_t, 12>, 19>; using MaxTechniqueLevels = parray<parray<uint8_t, 12>, 19>;
@@ -388,10 +426,10 @@ public:
uint8_t level = 0; uint8_t level = 0;
uint8_t char_class = 0; uint8_t char_class = 0;
parray<uint8_t, 3> unused; parray<uint8_t, 3> unused;
} __attribute__((packed)); } __packed_ws__(ItemCombination, 0x10);
template <bool IsBigEndian> template <bool IsBigEndian>
struct TechniqueBoost { struct TechniqueBoostT {
using U32T = typename std::conditional<IsBigEndian, be_uint32_t, le_uint32_t>::type; using U32T = typename std::conditional<IsBigEndian, be_uint32_t, le_uint32_t>::type;
using FloatT = typename std::conditional<IsBigEndian, be_float, le_float>::type; using FloatT = typename std::conditional<IsBigEndian, be_float, le_float>::type;
U32T tech1 = 0; U32T tech1 = 0;
@@ -400,26 +438,34 @@ public:
FloatT boost2 = 0.0f; FloatT boost2 = 0.0f;
U32T tech3 = 0; U32T tech3 = 0;
FloatT boost3 = 0.0f; FloatT boost3 = 0.0f;
} __attribute__((packed)); } __packed__;
using TechniqueBoost = TechniqueBoostT<false>;
using TechniqueBoostBE = TechniqueBoostT<true>;
check_struct_size(TechniqueBoost, 0x18);
check_struct_size(TechniqueBoostBE, 0x18);
struct EventItem { struct EventItem {
parray<uint8_t, 3> item; parray<uint8_t, 3> item;
uint8_t probability = 0; uint8_t probability = 0;
} __attribute__((packed)); } __packed_ws__(EventItem, 4);
struct UnsealableItem { struct UnsealableItem {
parray<uint8_t, 3> item; parray<uint8_t, 3> item;
uint8_t unused = 0; uint8_t unused = 0;
} __attribute__((packed)); } __packed_ws__(UnsealableItem, 4);
template <bool IsBigEndian> template <bool IsBigEndian>
struct NonWeaponSaleDivisors { struct NonWeaponSaleDivisorsT {
using FloatT = typename std::conditional<IsBigEndian, be_float, le_float>::type; using FloatT = typename std::conditional<IsBigEndian, be_float, le_float>::type;
FloatT armor_divisor = 0.0f; FloatT armor_divisor = 0.0f;
FloatT shield_divisor = 0.0f; FloatT shield_divisor = 0.0f;
FloatT unit_divisor = 0.0f; FloatT unit_divisor = 0.0f;
FloatT mag_divisor = 0.0f; FloatT mag_divisor = 0.0f;
} __attribute__((packed)); } __packed__;
using NonWeaponSaleDivisors = NonWeaponSaleDivisorsT<false>;
using NonWeaponSaleDivisorsBE = NonWeaponSaleDivisorsT<true>;
check_struct_size(NonWeaponSaleDivisors, 0x10);
check_struct_size(NonWeaponSaleDivisorsBE, 0x10);
ItemParameterTable(std::shared_ptr<const std::string> data, Version version); ItemParameterTable(std::shared_ptr<const std::string> data, Version version);
~ItemParameterTable() = default; ~ItemParameterTable() = default;
@@ -443,14 +489,14 @@ public:
const MagFeedResult& get_mag_feed_result(uint8_t table_index, uint8_t which) const; const MagFeedResult& get_mag_feed_result(uint8_t table_index, uint8_t which) const;
uint8_t get_item_stars(uint32_t id) const; uint8_t get_item_stars(uint32_t id) const;
uint8_t get_special_stars(uint8_t special) const; uint8_t get_special_stars(uint8_t special) const;
const Special<false>& get_special(uint8_t special) const; const Special& get_special(uint8_t special) const;
uint8_t get_max_tech_level(uint8_t char_class, uint8_t tech_num) const; uint8_t get_max_tech_level(uint8_t char_class, uint8_t tech_num) const;
uint8_t get_weapon_v1_replacement(uint8_t data1_1) const; uint8_t get_weapon_v1_replacement(uint8_t data1_1) const;
uint32_t get_item_id(const ItemData& item) const; uint32_t get_item_id(const ItemData& item) const;
uint32_t get_item_team_points(const ItemData& item) const; uint32_t get_item_team_points(const ItemData& item) const;
uint8_t get_item_base_stars(const ItemData& item) const; uint8_t get_item_base_stars(const ItemData& item) const;
uint8_t get_item_adjusted_stars(const ItemData& item) const; uint8_t get_item_adjusted_stars(const ItemData& item, bool ignore_unidentified = false) const;
bool is_item_rare(const ItemData& item) const; bool is_item_rare(const ItemData& item) const;
bool is_unsealable_item(uint8_t data1_0, uint8_t data1_1, uint8_t data1_2) const; bool is_unsealable_item(uint8_t data1_0, uint8_t data1_1, uint8_t data1_2) const;
bool is_unsealable_item(const ItemData& param_1) const; bool is_unsealable_item(const ItemData& param_1) const;
@@ -493,7 +539,7 @@ private:
/* 44 / 0668 / 0668 */ le_uint32_t unknown_a2; /* 44 / 0668 / 0668 */ le_uint32_t unknown_a2;
/* 48 / 030C / 030C */ le_uint32_t unknown_a3; /* 48 / 030C / 030C */ le_uint32_t unknown_a3;
/* 4C / 2CE4 / 2D78 */ le_uint32_t unknown_a4; /* 4C / 2CE4 / 2D78 */ le_uint32_t unknown_a4;
} __attribute__((packed)); } __packed_ws__(TableOffsetsDCProtos, 0x50);
struct TableOffsetsV1V2 { struct TableOffsetsV1V2 {
// TODO: Is weapon count 0x89 or 0x8A? It could be that the last entry in // TODO: Is weapon count 0x89 or 0x8A? It could be that the last entry in
@@ -516,7 +562,7 @@ private:
/* 38 / 2C12 / 4540 */ le_uint32_t special_data_table; // -> [Special](0x29) /* 38 / 2C12 / 4540 */ le_uint32_t special_data_table; // -> [Special](0x29)
/* 3C / 2CB8 / 58DC */ le_uint32_t stat_boost_table; // -> [StatBoost] /* 3C / 2CB8 / 58DC */ le_uint32_t stat_boost_table; // -> [StatBoost]
/* 40 / 3198 / 5704 */ le_uint32_t shield_effect_table; // -> [8-byte structs] /* 40 / 3198 / 5704 */ le_uint32_t shield_effect_table; // -> [8-byte structs]
} __attribute__((packed)); } __packed_ws__(TableOffsetsV1V2, 0x44);
struct TableOffsetsGCNTE { struct TableOffsetsGCNTE {
/* 00 / 6F0C */ be_uint32_t weapon_table; // -> [{count, offset -> [WeaponV3/WeaponV4]}](0xED) /* 00 / 6F0C */ be_uint32_t weapon_table; // -> [{count, offset -> [WeaponV3/WeaponV4]}](0xED)
@@ -539,10 +585,10 @@ private:
/* 44 / 737C */ be_uint32_t combination_table; // -> {count, offset -> [ItemCombination]} /* 44 / 737C */ be_uint32_t combination_table; // -> {count, offset -> [ItemCombination]}
/* 48 / 68B0 */ be_uint32_t unknown_a1; /* 48 / 68B0 */ be_uint32_t unknown_a1;
/* 4C / 6B1C */ be_uint32_t tech_boost_table; // -> [TechniqueBoost] (always 0x2C of them? from counts struct?) /* 4C / 6B1C */ be_uint32_t tech_boost_table; // -> [TechniqueBoost] (always 0x2C of them? from counts struct?)
} __attribute__((packed)); } __packed_ws__(TableOffsetsGCNTE, 0x50);
template <bool IsBigEndian> template <bool IsBigEndian>
struct TableOffsetsV3V4 { struct TableOffsetsV3V4T {
using U32T = typename std::conditional<IsBigEndian, be_uint32_t, le_uint32_t>::type; using U32T = typename std::conditional<IsBigEndian, be_uint32_t, le_uint32_t>::type;
/* ## / GC / BB */ /* ## / GC / BB */
/* 00 / F078 / 14884 */ U32T weapon_table; // -> [{count, offset -> [WeaponV3/WeaponV4]}](0xED) /* 00 / F078 / 14884 */ U32T weapon_table; // -> [{count, offset -> [WeaponV3/WeaponV4]}](0xED)
@@ -568,7 +614,11 @@ private:
/* 50 / F5F0 / 15014 */ U32T unwrap_table; // -> {count, offset -> [{count, offset -> [EventItem]}]} /* 50 / F5F0 / 15014 */ U32T unwrap_table; // -> {count, offset -> [{count, offset -> [EventItem]}]}
/* 54 / F5F8 / 1501C */ U32T unsealable_table; // -> {count, offset -> [UnsealableItem]} /* 54 / F5F8 / 1501C */ U32T unsealable_table; // -> {count, offset -> [UnsealableItem]}
/* 58 / F600 / 15024 */ U32T ranged_special_table; // -> {count, offset -> [4-byte structs]} /* 58 / F600 / 15024 */ U32T ranged_special_table; // -> {count, offset -> [4-byte structs]}
} __attribute__((packed)); } __packed__;
using TableOffsetsV3V4 = TableOffsetsV3V4T<false>;
using TableOffsetsV3V4BE = TableOffsetsV3V4T<true>;
check_struct_size(TableOffsetsV3V4, 0x5C);
check_struct_size(TableOffsetsV3V4BE, 0x5C);
Version version; Version version;
std::shared_ptr<const std::string> data; std::shared_ptr<const std::string> data;
@@ -576,9 +626,9 @@ private:
const TableOffsetsDCProtos* offsets_dc_protos; const TableOffsetsDCProtos* offsets_dc_protos;
const TableOffsetsV1V2* offsets_v1_v2; const TableOffsetsV1V2* offsets_v1_v2;
const TableOffsetsGCNTE* offsets_gc_nte; const TableOffsetsGCNTE* offsets_gc_nte;
const TableOffsetsV3V4<false>* offsets_v3_le; const TableOffsetsV3V4* offsets_v3_le;
const TableOffsetsV3V4<true>* offsets_v3_be; const TableOffsetsV3V4BE* offsets_v3_be;
const TableOffsetsV3V4<false>* offsets_v4; const TableOffsetsV3V4* offsets_v4;
// These are unused if offsets_v4 is not null (in that case, we just return // These are unused if offsets_v4 is not null (in that case, we just return
// references pointing inside the data string) // references pointing inside the data string)
@@ -588,13 +638,13 @@ private:
mutable std::vector<UnitV4> parsed_units; mutable std::vector<UnitV4> parsed_units;
mutable std::vector<MagV4> parsed_mags; mutable std::vector<MagV4> parsed_mags;
mutable std::unordered_map<uint16_t, ToolV4> parsed_tools; mutable std::unordered_map<uint16_t, ToolV4> parsed_tools;
mutable std::vector<Special<false>> parsed_specials; mutable std::vector<Special> parsed_specials;
// Key is used_item. We can't index on (used_item, equipped_item) because // Key is used_item. We can't index on (used_item, equipped_item) because
// equipped_item may contain wildcards, and the matching order matters. // equipped_item may contain wildcards, and the matching order matters.
mutable std::map<uint32_t, std::vector<ItemCombination>> item_combination_index; mutable std::map<uint32_t, std::vector<ItemCombination>> item_combination_index;
template <typename ToolT, bool IsBigEndian> template <typename ToolDefT, bool IsBigEndian>
std::pair<uint8_t, uint8_t> find_tool_by_id_t(uint32_t tool_table_offset, uint32_t id) const; std::pair<uint8_t, uint8_t> find_tool_by_id_t(uint32_t tool_table_offset, uint32_t id) const;
template <bool IsBigEndian, typename OffsetsT> template <bool IsBigEndian, typename OffsetsT>
float get_sale_divisor_t(const OffsetsT* offsets, uint8_t data1_0, uint8_t data1_1) const; float get_sale_divisor_t(const OffsetsT* offsets, uint8_t data1_0, uint8_t data1_1) const;
@@ -607,17 +657,18 @@ private:
class MagEvolutionTable { class MagEvolutionTable {
public: public:
struct TableOffsets { struct TableOffsets {
/* 00 / 0400 */ le_uint32_t unknown_a1; // -> [offset -> (0xC-byte struct)[0x53], offset -> (same as first offset)] // num_mags = 0x53 in BB, 0x43 in V3
/* 04 / 0408 */ le_uint32_t unknown_a2; // -> (2-byte struct, or single word)[0x53] /* 00 / 0400 */ le_uint32_t unknown_a1; // -> [offset -> (0xC-byte struct)[num_mags], offset -> (same as first offset)]
/* 04 / 0408 */ le_uint32_t unknown_a2; // -> (2-byte struct, or single word)[num_mags]
/* 08 / 04AE */ le_uint32_t unknown_a3; // -> (0xA8 bytes; possibly (8-byte struct)[0x15]) /* 08 / 04AE */ le_uint32_t unknown_a3; // -> (0xA8 bytes; possibly (8-byte struct)[0x15])
/* 0C / 0556 */ le_uint32_t unknown_a4; // -> (uint8_t)[0x53] /* 0C / 0556 */ le_uint32_t unknown_a4; // -> (uint8_t)[num_mags]
/* 10 / 05AC */ le_uint32_t unknown_a5; // -> (float)[0x48] /* 10 / 05AC */ le_uint32_t unknown_a5; // -> (float)[0x48]
/* 14 / 06CC */ le_uint32_t evolution_number; // -> (uint8_t)[0x53] /* 14 / 06CC */ le_uint32_t evolution_number; // -> (uint8_t)[num_mags]
} __attribute__((packed)); } __packed_ws__(TableOffsets, 0x18);
struct EvolutionNumberTable { struct EvolutionNumberTable {
parray<uint8_t, 0x53> values; parray<uint8_t, 0x53> values;
} __attribute__((packed)); } __packed_ws__(EvolutionNumberTable, 0x53);
MagEvolutionTable(std::shared_ptr<const std::string> data); MagEvolutionTable(std::shared_ptr<const std::string> data);
~MagEvolutionTable() = default; ~MagEvolutionTable() = default;
+15 -12
View File
@@ -12,7 +12,8 @@ void player_use_item(shared_ptr<Client> c, size_t item_index, shared_ptr<PSOLFGE
// On PC (and presumably DC), the client sends a 6x29 after this to delete the // On PC (and presumably DC), the client sends a 6x29 after this to delete the
// used item. On GC and later versions, this does not happen, so we should // used item. On GC and later versions, this does not happen, so we should
// delete the item here. // delete the item here.
bool is_v3_or_later = is_v3(c->version()) || is_v4(c->version()); bool is_v4 = ::is_v4(c->version());
bool is_v3_or_later = is_v3(c->version()) || is_v4;
bool should_delete_item = is_v3_or_later; bool should_delete_item = is_v3_or_later;
auto player = c->character(); auto player = c->character();
@@ -36,20 +37,14 @@ void player_use_item(shared_ptr<Client> c, size_t item_index, shared_ptr<PSOLFGE
} }
auto& weapon = player->inventory.items[player->inventory.find_equipped_item(EquipSlot::WEAPON)]; auto& weapon = player->inventory.items[player->inventory.find_equipped_item(EquipSlot::WEAPON)];
// Don't enforce the weapon's grind limit on V1 and V2. This is necessary // Only enforce grind limits on BB, since the server doesn't have direct
// because the V2 client replaces its inventory items on the fly with items // control over players' inventories on other versions
// compatible with V1 when sending the 61 and 98 commands. There appears to
// be no way to disable this behavior, so there's no way for the server to
// get an accurate picture of what's actually in the player's inventory, so
// there's no way to know if we would be enforcing the correct grind limit.
if (is_v3_or_later) {
auto item_parameter_table = s->item_parameter_table(c->version()); auto item_parameter_table = s->item_parameter_table(c->version());
auto weapon_def = item_parameter_table->get_weapon(weapon.data.data1[1], weapon.data.data1[2]); auto weapon_def = item_parameter_table->get_weapon(weapon.data.data1[1], weapon.data.data1[2]);
if (weapon.data.data1[3] >= weapon_def.max_grind) { if (is_v4 && (weapon.data.data1[3] >= weapon_def.max_grind)) {
throw runtime_error("weapon already at maximum grind"); throw runtime_error("weapon already at maximum grind");
} }
} weapon.data.data1[3] = min<uint8_t>(weapon.data.data1[3] + item.data.data1[2] + 1, weapon_def.max_grind);
weapon.data.data1[3] += (item.data.data1[2] + 1);
} else if ((primary_identifier & 0xFFFF0000) == 0x030B0000) { // Material } else if ((primary_identifier & 0xFFFF0000) == 0x030B0000) { // Material
auto p = c->character(); auto p = c->character();
@@ -119,21 +114,25 @@ void player_use_item(shared_ptr<Client> c, size_t item_index, shared_ptr<PSOLFGE
} else if (primary_identifier == 0x00330000) { } else if (primary_identifier == 0x00330000) {
// Unseal Sealed J-Sword => Tsumikiri J-Sword // Unseal Sealed J-Sword => Tsumikiri J-Sword
item.data.data1[1] = 0x32; item.data.data1[1] = 0x32;
item.flags &= (~8); // Unequip it
should_delete_item = false; should_delete_item = false;
} else if (primary_identifier == 0x00AB0000) { } else if (primary_identifier == 0x00AB0000) {
// Unseal Lame d'Argent => Excalibur // Unseal Lame d'Argent => Excalibur
item.data.data1[1] = 0xAC; item.data.data1[1] = 0xAC;
item.flags &= (~8); // Unequip it
should_delete_item = false; should_delete_item = false;
} else if (primary_identifier == 0x01034D00) { } else if (primary_identifier == 0x01034D00) {
// Unseal Limiter => Adept // Unseal Limiter => Adept
item.data.data1[2] = 0x4E; item.data.data1[2] = 0x4E;
item.flags &= (~8); // Unequip it
should_delete_item = false; should_delete_item = false;
} else if (primary_identifier == 0x01034F00) { } else if (primary_identifier == 0x01034F00) {
// Unseal Swordsman Lore => Proof of Sword-Saint // Unseal Swordsman Lore => Proof of Sword-Saint
item.data.data1[2] = 0x50; item.data.data1[2] = 0x50;
item.flags &= (~8); // Unequip it
should_delete_item = false; should_delete_item = false;
} else if (primary_identifier == 0x030C0000) { } else if (primary_identifier == 0x030C0000) {
@@ -239,9 +238,13 @@ void player_use_item(shared_ptr<Client> c, size_t item_index, shared_ptr<PSOLFGE
inv_item.data.data1[0] = combo.result_item[0]; inv_item.data.data1[0] = combo.result_item[0];
inv_item.data.data1[1] = combo.result_item[1]; inv_item.data.data1[1] = combo.result_item[1];
// For mags, don't reset level + PBs + stats
if (inv_item.data.data1[0] != 0x02) {
inv_item.data.data1[2] = combo.result_item[2]; inv_item.data.data1[2] = combo.result_item[2];
inv_item.data.data1[3] = 0; // Grind inv_item.data.data1[3] = 0; // Grind
inv_item.data.data1[4] = 0; // Flags + special inv_item.data.data1[4] = 0; // Flags + special
}
inv_item.flags &= (~8); // Unequip it
} catch (const out_of_range&) { } catch (const out_of_range&) {
} }
} }
@@ -343,7 +346,7 @@ void apply_mag_feed_result(
if (evolution_number < 2) { if (evolution_number < 2) {
uint16_t flags = mag_item.compute_mag_strength_flags(); uint16_t flags = mag_item.compute_mag_strength_flags();
if (mag_number == 0x0D) { if (mag_number == 0x0D) {
if ((flags & 0x110) == 0) { if (flags & 0x110) {
mag_item.data1[1] = 0x02; mag_item.data1[1] = 0x02;
} else if (flags & 8) { } else if (flags & 8) {
mag_item.data1[1] = 0x03; mag_item.data1[1] = 0x03;
+16 -16
View File
@@ -9,26 +9,26 @@
using namespace std; using namespace std;
void PlayerStats::reset_to_base(uint8_t char_class, shared_ptr<const LevelTable> level_table) { void LevelTable::reset_to_base(PlayerStats& stats, uint8_t char_class) const {
this->level = 0; stats.level = 0;
this->experience = 0; stats.experience = 0;
this->char_stats = level_table->base_stats_for_class(char_class); stats.char_stats = this->base_stats_for_class(char_class);
} }
void PlayerStats::advance_to_level(uint8_t char_class, uint32_t level, shared_ptr<const LevelTable> level_table) { void LevelTable::advance_to_level(PlayerStats& stats, uint32_t level, uint8_t char_class) const {
for (; this->level < level; this->level++) { for (; stats.level < level; stats.level++) {
const auto& level_stats = level_table->stats_delta_for_level(char_class, this->level + 1); const auto& level_stats = this->stats_delta_for_level(char_class, stats.level + 1);
// The original code clamps the resulting stat values to [0, max_stat]; we // The original code clamps the resulting stat values to [0, max_stat]; we
// don't have max_stat handy so we just allow them to be unbounded // don't have max_stat handy so we just allow them to be unbounded
this->char_stats.atp += level_stats.atp; stats.char_stats.atp += level_stats.atp;
this->char_stats.mst += level_stats.mst; stats.char_stats.mst += level_stats.mst;
this->char_stats.evp += level_stats.evp; stats.char_stats.evp += level_stats.evp;
this->char_stats.hp += level_stats.hp; stats.char_stats.hp += level_stats.hp;
this->char_stats.dfp += level_stats.dfp; stats.char_stats.dfp += level_stats.dfp;
this->char_stats.ata += level_stats.ata; stats.char_stats.ata += level_stats.ata;
// Note: It is not a bug that lck is ignored here; the original code // Note: It is not a bug that lck is ignored here; the original code
// ignores it too. // ignores it too.
this->experience = level_stats.experience; stats.experience = level_stats.experience;
} }
} }
@@ -52,7 +52,7 @@ LevelTableV2::LevelTableV2(const string& data, bool compressed) {
le_uint32_t unknown_a10; // -> u32[3] -> (0x10-byte struct)[0x0C] le_uint32_t unknown_a10; // -> u32[3] -> (0x10-byte struct)[0x0C]
le_uint32_t unknown_a11; // -> u32[3] -> (0x30-bytes) le_uint32_t unknown_a11; // -> u32[3] -> (0x30-bytes)
le_uint32_t unknown_a12; // -> u32[3] -> (0x14-byte struct)[0x0F] le_uint32_t unknown_a12; // -> u32[3] -> (0x14-byte struct)[0x0F]
} __attribute__((packed)); } __packed_ws__(Offsets, 0x40);
StringReader r; StringReader r;
string decompressed_data; string decompressed_data;
@@ -158,7 +158,7 @@ LevelTableV4::LevelTableV4(const string& data, bool compressed) {
struct Offsets { struct Offsets {
le_uint32_t base_stats; // -> u32[12] -> CharacterStats le_uint32_t base_stats; // -> u32[12] -> CharacterStats
le_uint32_t level_deltas; // -> u32[12] -> LevelStatsDelta[200] le_uint32_t level_deltas; // -> u32[12] -> LevelStatsDelta[200]
} __attribute__((packed)); } __packed_ws__(Offsets, 8);
StringReader r; StringReader r;
string decompressed_data; string decompressed_data;
+71 -30
View File
@@ -7,35 +7,74 @@
#include <phosg/Encoding.hh> #include <phosg/Encoding.hh>
#include <string> #include <string>
#include "Text.hh"
class LevelTable; class LevelTable;
struct CharacterStats { template <bool IsBigEndian>
/* 00 */ le_uint16_t atp = 0; struct CharacterStatsT {
/* 02 */ le_uint16_t mst = 0; using U16T = typename std::conditional<IsBigEndian, be_uint16_t, le_uint16_t>::type;
/* 04 */ le_uint16_t evp = 0;
/* 06 */ le_uint16_t hp = 0; /* 00 */ U16T atp = 0;
/* 08 */ le_uint16_t dfp = 0; /* 02 */ U16T mst = 0;
/* 0A */ le_uint16_t ata = 0; /* 04 */ U16T evp = 0;
/* 0C */ le_uint16_t lck = 0; /* 06 */ U16T hp = 0;
/* 08 */ U16T dfp = 0;
/* 0A */ U16T ata = 0;
/* 0C */ U16T lck = 0;
/* 0E */ /* 0E */
} __attribute__((packed));
struct PlayerStats { operator CharacterStatsT<!IsBigEndian>() const {
/* 00 */ CharacterStats char_stats; CharacterStatsT<!IsBigEndian> ret;
/* 0E */ le_uint16_t esp = 0; ret.atp = this->atp.load();
/* 10 */ le_float height = 0.0; ret.mst = this->mst.load();
/* 14 */ le_float unknown_a3 = 0.0; ret.evp = this->evp.load();
/* 18 */ le_uint32_t level = 0; ret.hp = this->hp.load();
/* 1C */ le_uint32_t experience = 0; ret.dfp = this->dfp.load();
/* 20 */ le_uint32_t meseta = 0; ret.ata = this->ata.load();
/* 24 */ ret.lck = this->lck.load();
return ret;
void reset_to_base(uint8_t char_class, std::shared_ptr<const LevelTable> level_table); }
void advance_to_level(uint8_t char_class, uint32_t level, std::shared_ptr<const LevelTable> level_table); } __packed__;
} __attribute__((packed)); using CharacterStats = CharacterStatsT<false>;
using CharacterStatsBE = CharacterStatsT<true>;
check_struct_size(CharacterStats, 0x0E);
check_struct_size(CharacterStatsBE, 0x0E);
template <bool IsBigEndian> template <bool IsBigEndian>
struct LevelStatsDeltaBase { struct PlayerStatsT {
using U16T = typename std::conditional<IsBigEndian, be_uint16_t, le_uint16_t>::type;
using U32T = typename std::conditional<IsBigEndian, be_uint32_t, le_uint32_t>::type;
using F32T = typename std::conditional<IsBigEndian, be_float, le_float>::type;
/* 00 */ CharacterStatsT<IsBigEndian> char_stats;
/* 0E */ U16T esp = 0;
/* 10 */ F32T height = 0.0;
/* 14 */ F32T unknown_a3 = 0.0;
/* 18 */ U32T level = 0;
/* 1C */ U32T experience = 0;
/* 20 */ U32T meseta = 0;
/* 24 */
operator PlayerStatsT<!IsBigEndian>() const {
PlayerStatsT<!IsBigEndian> ret;
ret.char_stats = this->char_stats;
ret.esp = this->esp.load();
ret.height = this->height.load();
ret.unknown_a3 = this->unknown_a3.load();
ret.level = this->level.load();
ret.experience = this->experience.load();
ret.meseta = this->meseta.load();
return ret;
}
} __packed__;
using PlayerStats = PlayerStatsT<false>;
using PlayerStatsBE = PlayerStatsT<true>;
check_struct_size(PlayerStats, 0x24);
check_struct_size(PlayerStatsBE, 0x24);
template <bool IsBigEndian>
struct LevelStatsDeltaT {
using U32T = typename std::conditional<IsBigEndian, be_uint32_t, le_uint32_t>::type; using U32T = typename std::conditional<IsBigEndian, be_uint32_t, le_uint32_t>::type;
/* 00 */ uint8_t atp; /* 00 */ uint8_t atp;
@@ -58,12 +97,11 @@ struct LevelStatsDeltaBase {
ps.mst += this->mst; ps.mst += this->mst;
ps.lck += this->lck; ps.lck += this->lck;
} }
} __attribute__((packed)); } __packed__;
using LevelStatsDelta = LevelStatsDeltaT<false>;
struct LevelStatsDelta : LevelStatsDeltaBase<false> { using LevelStatsDeltaBE = LevelStatsDeltaT<true>;
} __attribute__((packed)); check_struct_size(LevelStatsDelta, 0x0C);
struct LevelStatsDeltaBE : LevelStatsDeltaBase<true> { check_struct_size(LevelStatsDeltaBE, 0x0C);
} __attribute__((packed));
class LevelTable { class LevelTable {
// This is the base class for all the LevelTable implementations. The public // This is the base class for all the LevelTable implementations. The public
@@ -76,6 +114,9 @@ public:
virtual const CharacterStats& base_stats_for_class(uint8_t char_class) const = 0; virtual const CharacterStats& base_stats_for_class(uint8_t char_class) const = 0;
virtual const LevelStatsDelta& stats_delta_for_level(uint8_t char_class, uint8_t level) const = 0; virtual const LevelStatsDelta& stats_delta_for_level(uint8_t char_class, uint8_t level) const = 0;
void reset_to_base(PlayerStats& stats, uint8_t char_class) const;
void advance_to_level(PlayerStats& stats, uint32_t level, uint8_t char_class) const;
protected: protected:
LevelTable() = default; LevelTable() = default;
}; };
@@ -89,7 +130,7 @@ public:
/* 14 */ le_float unknown_a3 = 0.0; /* 14 */ le_float unknown_a3 = 0.0;
/* 18 */ le_uint32_t level = 0; /* 18 */ le_uint32_t level = 0;
/* 1C */ /* 1C */
} __attribute__((packed)); } __packed_ws__(Level100Entry, 0x1C);
LevelTableV2(const std::string& data, bool compressed); LevelTableV2(const std::string& data, bool compressed);
virtual ~LevelTableV2() = default; virtual ~LevelTableV2() = default;
-392
View File
@@ -1,392 +0,0 @@
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include <phosg/Filesystem.hh>
#include <phosg/Hash.hh>
#include <phosg/Time.hh>
#include "License.hh"
using namespace std;
License::License(const JSON& json)
: serial_number(0),
flags(0),
ban_end_time(0),
ep3_current_meseta(0),
ep3_total_meseta_earned(0),
bb_team_id(0) {
this->serial_number = json.get_int("SerialNumber");
this->access_key = json.get_string("AccessKey", "");
this->dc_nte_serial_number = json.get_string("DCNTESerialNumber", "");
this->dc_nte_access_key = json.get_string("DCNTEAccessKey", "");
this->gc_password = json.get_string("GCPassword", "");
this->xb_gamertag = json.get_string("XBGamerTag", "");
this->xb_user_id = json.get_int("XBUserID", 0);
this->xb_account_id = json.get_int("XBAccountID", 0);
this->bb_username = json.get_string("BBUsername", "");
this->bb_password = json.get_string("BBPassword", "");
this->flags = json.get_int("Flags", 0);
this->ban_end_time = json.get_int("BanEndTime", 0);
this->last_player_name = json.get_string("LastPlayerName", "");
this->auto_reply_message = json.get_string("AutoReplyMessage", "");
this->ep3_current_meseta = json.get_int("Ep3CurrentMeseta", 0);
this->ep3_total_meseta_earned = json.get_int("Ep3TotalMesetaEarned", 0);
this->bb_team_id = json.get_int("BBTeamID", 0);
}
JSON License::json() const {
return JSON::dict({
{"SerialNumber", this->serial_number},
{"AccessKey", this->access_key},
{"DCNTESerialNumber", this->dc_nte_serial_number},
{"DCNTEAccessKey", this->dc_nte_access_key},
{"GCPassword", this->gc_password},
{"XBGamerTag", this->xb_gamertag},
{"XBUserID", this->xb_user_id},
{"XBAccountID", this->xb_account_id},
{"BBUsername", this->bb_username},
{"BBPassword", this->bb_password},
{"Flags", this->flags},
{"BanEndTime", this->ban_end_time},
{"LastPlayerName", this->last_player_name},
{"AutoReplyMessage", this->auto_reply_message},
{"Ep3CurrentMeseta", this->ep3_current_meseta},
{"Ep3TotalMesetaEarned", this->ep3_total_meseta_earned},
{"BBTeamID", this->bb_team_id},
});
}
void License::save() const {}
void License::delete_file() const {}
string License::str() const {
vector<string> tokens;
tokens.emplace_back(string_printf("serial_number=%010" PRIu32 "/%08" PRIX32, this->serial_number, this->serial_number));
if (!this->access_key.empty()) {
tokens.emplace_back("access_key=" + this->access_key);
}
if (!this->dc_nte_serial_number.empty()) {
tokens.emplace_back("dc_nte_serial_number=" + this->dc_nte_serial_number);
}
if (!this->dc_nte_access_key.empty()) {
tokens.emplace_back("dc_nte_access_key=" + this->dc_nte_access_key);
}
if (!this->gc_password.empty()) {
tokens.emplace_back("gc_password=" + this->gc_password);
}
if (!this->xb_gamertag.empty()) {
tokens.emplace_back("xb_gamertag=" + this->xb_gamertag);
}
if (this->xb_user_id != 0) {
tokens.emplace_back(string_printf("xb_user_id=%016" PRIX64, this->xb_user_id));
}
if (this->xb_account_id != 0) {
tokens.emplace_back(string_printf("xb_account_id=%016" PRIX64, this->xb_account_id));
}
if (!this->bb_username.empty()) {
tokens.emplace_back("bb_username=" + this->bb_username);
}
if (!this->bb_password.empty()) {
tokens.emplace_back("bb_password=" + this->bb_password);
}
tokens.emplace_back(string_printf("flags=%08" PRIX32, this->flags));
if (this->ban_end_time) {
tokens.emplace_back(string_printf("ban_end_time=%016" PRIX64, this->ban_end_time));
}
if (this->ep3_current_meseta) {
tokens.emplace_back(string_printf("ep3_current_meseta=%" PRIu32, this->ep3_current_meseta));
}
if (this->ep3_total_meseta_earned) {
tokens.emplace_back(string_printf("ep3_total_meseta_earned=%" PRIu32, this->ep3_total_meseta_earned));
}
return "[License: " + join(tokens, ", ") + "]";
}
DiskLicense::DiskLicense(const JSON& json) : License(json) {}
void DiskLicense::save() const {
auto json = this->json();
string json_data = json.serialize(JSON::SerializeOption::FORMAT | JSON::SerializeOption::HEX_INTEGERS);
string filename = string_printf("system/licenses/%010" PRIu32 ".json", this->serial_number);
save_file(filename, json_data);
}
void DiskLicense::delete_file() const {
string filename = string_printf("system/licenses/%010" PRIu32 ".json", this->serial_number);
remove(filename.c_str());
}
shared_ptr<License> LicenseIndex::create_license() const {
return make_shared<License>();
}
shared_ptr<License> LicenseIndex::create_temporary_license() const {
return make_shared<License>();
}
size_t LicenseIndex::count() const {
return this->serial_number_to_license.size();
}
shared_ptr<License> LicenseIndex::get(uint32_t serial_number) const {
try {
return this->serial_number_to_license.at(serial_number);
} catch (const out_of_range&) {
throw missing_license();
}
}
shared_ptr<License> LicenseIndex::get_by_bb_username(const string& bb_username) const {
try {
return this->bb_username_to_license.at(bb_username);
} catch (const out_of_range&) {
throw missing_license();
}
}
vector<shared_ptr<License>> LicenseIndex::all() const {
vector<shared_ptr<License>> ret;
ret.reserve(this->serial_number_to_license.size());
for (const auto& it : this->serial_number_to_license) {
ret.emplace_back(it.second);
}
return ret;
}
void LicenseIndex::add(shared_ptr<License> l) {
this->serial_number_to_license[l->serial_number] = l;
if (!l->dc_nte_serial_number.empty()) {
this->dc_nte_serial_number_to_license[l->dc_nte_serial_number] = l;
}
if (!l->xb_gamertag.empty()) {
this->xb_gamertag_to_license[l->xb_gamertag] = l;
}
if (!l->bb_username.empty()) {
this->bb_username_to_license[l->bb_username] = l;
}
}
void LicenseIndex::remove(uint32_t serial_number) {
auto l = this->serial_number_to_license.at(serial_number);
this->serial_number_to_license.erase(l->serial_number);
if (!l->dc_nte_serial_number.empty()) {
this->dc_nte_serial_number_to_license.erase(l->dc_nte_serial_number);
}
if (!l->xb_gamertag.empty()) {
this->xb_gamertag_to_license.erase(l->xb_gamertag);
}
if (!l->bb_username.empty()) {
this->bb_username_to_license.erase(l->bb_username);
}
}
shared_ptr<License> LicenseIndex::verify_dc_nte(const string& serial_number, const string& access_key) const {
if (serial_number.empty()) {
throw no_username();
}
try {
auto& license = this->dc_nte_serial_number_to_license.at(serial_number);
if (license->ban_end_time && (license->ban_end_time >= now())) {
throw invalid_argument("user is banned");
}
if (license->dc_nte_access_key != access_key) {
throw incorrect_access_key();
}
return license;
} catch (const out_of_range&) {
throw missing_license();
}
}
shared_ptr<License> LicenseIndex::verify_v1_v2(
uint32_t serial_number,
const string& access_key,
const string& character_name) const {
if (serial_number == 0) {
throw no_username();
}
try {
auto& license = this->serial_number_to_license.at(serial_number);
if (license->ban_end_time && (license->ban_end_time >= now())) {
throw invalid_argument("user is banned");
}
if (license->check_flag(License::Flag::IS_SHARED_SERIAL)) {
return this->create_temporary_license_for_shared_license(license->flags, serial_number, access_key, "", character_name);
}
if (license->access_key.compare(0, 8, access_key) != 0) {
throw incorrect_access_key();
}
return license;
} catch (const out_of_range&) {
throw missing_license();
}
}
shared_ptr<License> LicenseIndex::verify_gc_no_password(
uint32_t serial_number,
const string& access_key,
const string& character_name) const {
if (serial_number == 0) {
throw no_username();
}
try {
auto& license = this->serial_number_to_license.at(serial_number);
if (license->ban_end_time && (license->ban_end_time >= now())) {
throw invalid_argument("user is banned");
}
if (license->check_flag(License::Flag::IS_SHARED_SERIAL)) {
return this->create_temporary_license_for_shared_license(license->flags, serial_number, access_key, "", character_name);
}
if (license->access_key != access_key) {
throw incorrect_access_key();
}
return license;
} catch (const out_of_range&) {
throw missing_license();
}
}
shared_ptr<License> LicenseIndex::verify_gc_with_password(
uint32_t serial_number,
const string& access_key,
const string& password,
const string& character_name) const {
if (serial_number == 0) {
throw no_username();
}
try {
auto& license = this->serial_number_to_license.at(serial_number);
if (license->ban_end_time && (license->ban_end_time >= now())) {
throw invalid_argument("user is banned");
}
if (license->check_flag(License::Flag::IS_SHARED_SERIAL)) {
return this->create_temporary_license_for_shared_license(license->flags, serial_number, access_key, password, character_name);
}
if (license->access_key != access_key) {
throw incorrect_access_key();
}
if (license->gc_password != password) {
throw incorrect_password();
}
return license;
} catch (const out_of_range&) {
throw missing_license();
}
}
shared_ptr<License> LicenseIndex::verify_xb(const string& gamertag, uint64_t user_id, uint64_t account_id) const {
if (user_id == 0 || account_id == 0) {
throw incorrect_access_key();
}
try {
auto& license = this->xb_gamertag_to_license.at(gamertag);
if (license->check_flag(License::Flag::IS_SHARED_SERIAL)) {
throw missing_license(); // XB users cannot use shared serials
}
if (license->ban_end_time && (license->ban_end_time >= now())) {
throw invalid_argument("user is banned");
}
if (license->xb_user_id && (license->xb_user_id != user_id)) {
throw incorrect_access_key();
}
if (license->xb_account_id && (license->xb_account_id != account_id)) {
throw incorrect_access_key();
}
return license;
} catch (const out_of_range&) {
throw missing_license();
}
}
shared_ptr<License> LicenseIndex::verify_bb(const string& username, const string& password) const {
if (username.empty() || password.empty()) {
throw no_username();
}
try {
auto& license = this->bb_username_to_license.at(username);
if (license->check_flag(License::Flag::IS_SHARED_SERIAL)) {
throw missing_license(); // BB users cannot use shared serials
}
if (license->ban_end_time && (license->ban_end_time >= now())) {
throw invalid_argument("user is banned");
}
if (license->bb_password != password) {
throw incorrect_password();
}
return license;
} catch (const out_of_range&) {
throw missing_license();
}
}
shared_ptr<License> LicenseIndex::create_temporary_license_for_shared_license(
uint32_t base_flags,
uint32_t serial_number,
const string& access_key,
const string& password,
const string& character_name) const {
uint32_t temp_serial_number = fnv1a32(&serial_number, sizeof(serial_number));
temp_serial_number = fnv1a32(access_key, temp_serial_number);
temp_serial_number = fnv1a32(password, temp_serial_number);
temp_serial_number = fnv1a32(character_name, temp_serial_number);
auto ret = this->create_temporary_license();
ret->serial_number = temp_serial_number & 0x7FFFFFFF;
ret->flags = base_flags;
ret->set_flag(License::Flag::IS_SHARED_SERIAL);
return ret;
}
DiskLicenseIndex::DiskLicenseIndex() {
struct BinaryLicense {
pstring<TextEncoding::ASCII, 0x14> username; // BB username (max. 16 chars; should technically be Unicode)
pstring<TextEncoding::ASCII, 0x14> bb_password; // BB password (max. 16 chars)
uint32_t serial_number; // PC/GC serial number. MUST BE PRESENT FOR BB LICENSES TOO; this is also the player's guild card number.
pstring<TextEncoding::ASCII, 0x10> access_key; // PC/GC access key. (to log in using PC on a GC license, just enter the first 8 characters of the GC access key)
pstring<TextEncoding::ASCII, 0x0C> gc_password; // GC password
uint32_t privileges; // privilege level
uint64_t ban_end_time; // end time of ban (zero = not banned)
} __attribute__((packed));
if (!isdir("system/licenses")) {
mkdir("system/licenses", 0755);
}
// Convert binary licenses to JSON licenses and save them
if (isfile("system/licenses.nsi")) {
auto bin_licenses = load_vector_file<BinaryLicense>("system/licenses.nsi");
for (const auto& bin_license : bin_licenses) {
// Only add licenses from the binary file if there isn't a JSON version of
// the same license
try {
this->get(bin_license.serial_number);
} catch (const missing_license&) {
License license;
license.serial_number = bin_license.serial_number;
license.access_key = bin_license.access_key.decode();
license.gc_password = bin_license.gc_password.decode();
license.bb_username = bin_license.username.decode();
license.bb_password = bin_license.bb_password.decode();
license.flags = bin_license.privileges;
license.ban_end_time = bin_license.ban_end_time;
license.ep3_current_meseta = 0;
license.ep3_total_meseta_earned = 0;
license.save();
}
}
::remove("system/licenses.nsi");
}
for (const auto& item : list_directory("system/licenses")) {
if (ends_with(item, ".json")) {
JSON json = JSON::parse(load_file("system/licenses/" + item));
auto license = make_shared<DiskLicense>(json);
this->add(license);
}
}
}
shared_ptr<License> DiskLicenseIndex::create_license() const {
return make_shared<DiskLicense>();
}
-165
View File
@@ -1,165 +0,0 @@
#pragma once
#include <memory>
#include <phosg/JSON.hh>
#include <string>
#include <unordered_map>
#include <vector>
#include "Text.hh"
class LicenseIndex;
class License {
public:
enum class Flag : uint32_t {
// clang-format off
KICK_USER = 0x00000001,
BAN_USER = 0x00000002,
SILENCE_USER = 0x00000004,
CHANGE_EVENT = 0x00000010,
ANNOUNCE = 0x00000020,
FREE_JOIN_GAMES = 0x00000040,
DEBUG = 0x01000000,
CHEAT_ANYWHERE = 0x02000000,
DISABLE_QUEST_REQUIREMENTS = 0x04000000,
ALWAYS_ENABLE_CHAT_COMMANDS = 0x08000000,
MODERATOR = 0x00000007,
ADMINISTRATOR = 0x000000FF,
ROOT = 0x7FFFFFFF,
IS_SHARED_SERIAL = 0x80000000,
// 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
};
uint32_t serial_number = 0;
std::string dc_nte_serial_number;
std::string dc_nte_access_key;
std::string access_key;
std::string gc_password;
std::string xb_gamertag;
uint64_t xb_user_id = 0;
uint64_t xb_account_id = 0;
std::string bb_username;
std::string bb_password;
uint32_t flags = 0;
uint64_t ban_end_time = 0; // 0 = not banned
std::string last_player_name;
std::string auto_reply_message;
uint32_t ep3_current_meseta = 0;
uint32_t ep3_total_meseta_earned = 0;
uint32_t bb_team_id = 0;
License() = default;
explicit License(const JSON& json);
virtual ~License() = default;
JSON json() const;
virtual void save() const;
virtual void delete_file() const;
[[nodiscard]] inline bool check_flag(Flag flag) const {
return !!(this->flags & static_cast<uint32_t>(flag));
}
inline void set_flag(Flag flag) {
this->flags |= static_cast<uint32_t>(flag);
}
inline void clear_flag(Flag flag) {
this->flags &= (~static_cast<uint32_t>(flag));
}
inline void toggle_flag(Flag flag) {
this->flags ^= static_cast<uint32_t>(flag);
}
inline void replace_all_flags(Flag mask) {
this->flags = static_cast<uint32_t>(mask);
}
std::string str() const;
};
class DiskLicense : public License {
public:
DiskLicense() = default;
explicit DiskLicense(const JSON& json);
virtual ~DiskLicense() = default;
virtual void save() const;
virtual void delete_file() const;
};
class LicenseIndex {
public:
class no_username : public std::invalid_argument {
public:
no_username() : invalid_argument("serial number is zero or username is missing") {}
};
class incorrect_password : public std::invalid_argument {
public:
incorrect_password() : invalid_argument("incorrect password") {}
};
class incorrect_access_key : public std::invalid_argument {
public:
incorrect_access_key() : invalid_argument("incorrect access key") {}
};
class missing_license : public std::invalid_argument {
public:
missing_license() : invalid_argument("missing license") {}
};
LicenseIndex() = default;
virtual ~LicenseIndex() = default;
virtual std::shared_ptr<License> create_license() const;
virtual std::shared_ptr<License> create_temporary_license() const;
size_t count() const;
std::shared_ptr<License> get(uint32_t serial_number) const;
std::shared_ptr<License> get_by_bb_username(const std::string& bb_username) const;
std::vector<std::shared_ptr<License>> all() const;
void add(std::shared_ptr<License> l);
void remove(uint32_t serial_number);
std::shared_ptr<License> verify_dc_nte(const std::string& serial_number, const std::string& access_key) const;
std::shared_ptr<License> verify_v1_v2(
uint32_t serial_number,
const std::string& access_key,
const std::string& character_name) const;
std::shared_ptr<License> verify_gc_no_password(
uint32_t serial_number,
const std::string& access_key,
const std::string& character_name) const;
std::shared_ptr<License> verify_gc_with_password(
uint32_t serial_number,
const std::string& access_key,
const std::string& password,
const std::string& character_name) const;
std::shared_ptr<License> verify_xb(const std::string& gamertag, uint64_t user_id, uint64_t account_id) const;
std::shared_ptr<License> verify_bb(const std::string& username, const std::string& password) const;
protected:
std::unordered_map<std::string, std::shared_ptr<License>> dc_nte_serial_number_to_license;
std::unordered_map<std::string, std::shared_ptr<License>> xb_gamertag_to_license;
std::unordered_map<std::string, std::shared_ptr<License>> bb_username_to_license;
std::unordered_map<uint32_t, std::shared_ptr<License>> serial_number_to_license;
std::shared_ptr<License> create_temporary_license_for_shared_license(
uint32_t base_flags,
uint32_t serial_number,
const std::string& access_key,
const std::string& password,
const std::string& character_name) const;
};
class DiskLicenseIndex : public LicenseIndex {
public:
DiskLicenseIndex();
virtual ~DiskLicenseIndex() = default;
virtual std::shared_ptr<License> create_license() const;
};
+44 -27
View File
@@ -12,7 +12,7 @@
using namespace std; using namespace std;
bool Lobby::FloorItem::visible_to_client(uint8_t client_id) const { bool Lobby::FloorItem::visible_to_client(uint8_t client_id) const {
return this->visibility_flags & (1 << client_id); return this->flags & (1 << client_id);
} }
Lobby::FloorItemManager::FloorItemManager(uint32_t lobby_id, uint8_t floor) Lobby::FloorItemManager::FloorItemManager(uint32_t lobby_id, uint8_t floor)
@@ -27,18 +27,18 @@ shared_ptr<Lobby::FloorItem> Lobby::FloorItemManager::find(uint32_t item_id) con
return this->items.at(item_id); return this->items.at(item_id);
} }
void Lobby::FloorItemManager::add(const ItemData& item, float x, float z, uint16_t visibility_flags) { void Lobby::FloorItemManager::add(const ItemData& item, float x, float z, uint16_t flags) {
auto fi = make_shared<FloorItem>(); auto fi = make_shared<FloorItem>();
fi->data = item; fi->data = item;
fi->x = x; fi->x = x;
fi->z = z; fi->z = z;
fi->drop_number = this->next_drop_number++; fi->drop_number = this->next_drop_number++;
fi->visibility_flags = visibility_flags & 0x0FFF; fi->flags = flags;
this->add(fi); this->add(fi);
} }
void Lobby::FloorItemManager::add(shared_ptr<Lobby::FloorItem> fi) { void Lobby::FloorItemManager::add(shared_ptr<Lobby::FloorItem> fi) {
if (fi->visibility_flags == 0) { if (fi->flags == 0) {
throw logic_error("floor item is not visible to any player"); throw logic_error("floor item is not visible to any player");
} }
@@ -51,8 +51,8 @@ void Lobby::FloorItemManager::add(shared_ptr<Lobby::FloorItem> fi) {
this->queue_for_client[z].emplace(fi->drop_number, fi); this->queue_for_client[z].emplace(fi->drop_number, fi);
} }
} }
this->log.info("Added floor item %08" PRIX32 " at %g, %g with drop number %" PRIu64 " visible to %03hX", this->log.info("Added floor item %08" PRIX32 " at %g, %g with drop number %" PRIu64 " with flags %03hX",
fi->data.id.load(), fi->x, fi->z, fi->drop_number, fi->visibility_flags); fi->data.id.load(), fi->x, fi->z, fi->drop_number, fi->flags);
} }
std::shared_ptr<Lobby::FloorItem> Lobby::FloorItemManager::remove(uint32_t item_id, uint8_t client_id) { std::shared_ptr<Lobby::FloorItem> Lobby::FloorItemManager::remove(uint32_t item_id, uint8_t client_id) {
@@ -70,8 +70,8 @@ std::shared_ptr<Lobby::FloorItem> Lobby::FloorItemManager::remove(uint32_t item_
} }
} }
this->items.erase(item_it); this->items.erase(item_it);
this->log.info("Removed floor item %08" PRIX32 " at %g, %g with drop number %" PRIu64 " visible to %03hX", this->log.info("Removed floor item %08" PRIX32 " at %g, %g with drop number %" PRIu64 " with flags %03hX",
fi->data.id.load(), fi->x, fi->z, fi->drop_number, fi->visibility_flags); fi->data.id.load(), fi->x, fi->z, fi->drop_number, fi->flags);
return fi; return fi;
} }
@@ -89,7 +89,7 @@ std::unordered_set<std::shared_ptr<Lobby::FloorItem>> Lobby::FloorItemManager::e
void Lobby::FloorItemManager::clear_inaccessible(uint16_t remaining_clients_mask) { void Lobby::FloorItemManager::clear_inaccessible(uint16_t remaining_clients_mask) {
unordered_set<uint32_t> item_ids_to_delete; unordered_set<uint32_t> item_ids_to_delete;
for (const auto& it : this->items) { for (const auto& it : this->items) {
if ((it.second->visibility_flags & remaining_clients_mask) == 0) { if ((it.second->flags & remaining_clients_mask) == 0) {
item_ids_to_delete.emplace(it.first); item_ids_to_delete.emplace(it.first);
} }
} }
@@ -102,7 +102,7 @@ void Lobby::FloorItemManager::clear_inaccessible(uint16_t remaining_clients_mask
void Lobby::FloorItemManager::clear_private() { void Lobby::FloorItemManager::clear_private() {
unordered_set<uint32_t> item_ids_to_delete; unordered_set<uint32_t> item_ids_to_delete;
for (const auto& it : this->items) { for (const auto& it : this->items) {
if ((it.second->visibility_flags & 0x00F) != 0x00F) { if ((it.second->flags & 0x00F) != 0x00F) {
item_ids_to_delete.emplace(it.first); item_ids_to_delete.emplace(it.first);
} }
} }
@@ -149,6 +149,7 @@ Lobby::Lobby(shared_ptr<ServerState> s, uint32_t id, bool is_game)
mode(GameMode::NORMAL), mode(GameMode::NORMAL),
difficulty(0), difficulty(0),
base_exp_multiplier(1), base_exp_multiplier(1),
exp_share_multiplier(0.5),
challenge_exp_multiplier(1.0f), challenge_exp_multiplier(1.0f),
random_seed(random_object<uint32_t>()), random_seed(random_object<uint32_t>()),
drop_mode(DropMode::CLIENT), drop_mode(DropMode::CLIENT),
@@ -504,6 +505,7 @@ void Lobby::create_ep3_server() {
.card_index = is_nte ? s->ep3_card_index_trial : s->ep3_card_index, .card_index = is_nte ? s->ep3_card_index_trial : s->ep3_card_index,
.map_index = s->ep3_map_index, .map_index = s->ep3_map_index,
.behavior_flags = s->ep3_behavior_flags, .behavior_flags = s->ep3_behavior_flags,
.opt_rand_stream = nullptr,
.opt_rand_crypt = this->opt_rand_crypt, .opt_rand_crypt = this->opt_rand_crypt,
.tournament = tourn, .tournament = tourn,
.trap_card_ids = s->ep3_trap_card_ids, .trap_card_ids = s->ep3_trap_card_ids,
@@ -566,6 +568,10 @@ bool Lobby::any_v1_clients_present() const {
} }
void Lobby::add_client(shared_ptr<Client> c, ssize_t required_client_id) { void Lobby::add_client(shared_ptr<Client> c, ssize_t required_client_id) {
if (!c->login) {
throw runtime_error("client is not logged in");
}
ssize_t index; ssize_t index;
ssize_t min_client_id = this->check_flag(Lobby::Flag::IS_SPECTATOR_TEAM) ? 4 : 0; ssize_t min_client_id = this->check_flag(Lobby::Flag::IS_SPECTATOR_TEAM) ? 4 : 0;
@@ -645,12 +651,12 @@ void Lobby::add_client(shared_ptr<Client> c, ssize_t required_client_id) {
auto p = c->character(); auto p = c->character();
PlayerLobbyDataDCGC lobby_data; PlayerLobbyDataDCGC lobby_data;
lobby_data.player_tag = 0x00010000; lobby_data.player_tag = 0x00010000;
lobby_data.guild_card_number = c->license->serial_number; lobby_data.guild_card_number = c->login->account->account_id;
lobby_data.name.encode(p->disp.name.decode(c->language()), c->language()); lobby_data.name.encode(p->disp.name.decode(c->language()), c->language());
this->battle_record->add_player( this->battle_record->add_player(
lobby_data, lobby_data,
p->inventory, p->inventory,
p->disp.to_dcpcv3(c->language(), c->language()), p->disp.to_dcpcv3<false>(c->language(), c->language()),
c->ep3_config ? (c->ep3_config->online_clv_exp / 100) : 0); c->ep3_config ? (c->ep3_config->online_clv_exp / 100) : 0);
} }
@@ -767,14 +773,13 @@ void Lobby::move_client_to_lobby(
dest_lobby->add_client(c, required_client_id); dest_lobby->add_client(c, required_client_id);
} }
shared_ptr<Client> Lobby::find_client(const string* identifier, uint64_t serial_number) { shared_ptr<Client> Lobby::find_client(const string* identifier, uint64_t account_id) {
for (size_t x = 0; x < this->max_clients; x++) { for (size_t x = 0; x < this->max_clients; x++) {
auto lc = this->clients[x]; auto lc = this->clients[x];
if (!lc) { if (!lc) {
continue; continue;
} }
if (serial_number && lc->license && if (account_id && lc->login && (lc->login->account->account_id == account_id)) {
(lc->license->serial_number == serial_number)) {
return lc; return lc;
} }
if (identifier && (lc->character()->disp.name.eq(*identifier, lc->language()))) { if (identifier && (lc->character()->disp.name.eq(*identifier, lc->language()))) {
@@ -789,10 +794,14 @@ Lobby::JoinError Lobby::join_error_for_client(std::shared_ptr<Client> c, const s
if (this->count_clients() >= this->max_clients) { if (this->count_clients() >= this->max_clients) {
return JoinError::FULL; return JoinError::FULL;
} }
if (!this->version_is_allowed(c->version()) && !c->config.check_flag(Client::Flag::DEBUG_ENABLED)) { bool debug_enabled = c->config.check_flag(Client::Flag::DEBUG_ENABLED);
if (!this->version_is_allowed(c->version()) && !debug_enabled) {
return JoinError::VERSION_CONFLICT; return JoinError::VERSION_CONFLICT;
} }
if (this->is_game()) { if (this->is_game()) {
if (this->check_flag(Flag::QUEST_SELECTION_IN_PROGRESS)) {
return JoinError::QUEST_SELECTION_IN_PROGRESS;
}
if (this->check_flag(Flag::QUEST_IN_PROGRESS)) { if (this->check_flag(Flag::QUEST_IN_PROGRESS)) {
return JoinError::QUEST_IN_PROGRESS; return JoinError::QUEST_IN_PROGRESS;
} }
@@ -802,7 +811,11 @@ Lobby::JoinError Lobby::join_error_for_client(std::shared_ptr<Client> c, const s
if (this->mode == GameMode::SOLO) { if (this->mode == GameMode::SOLO) {
return JoinError::SOLO; return JoinError::SOLO;
} }
if (!c->license->check_flag(License::Flag::FREE_JOIN_GAMES)) { if (!debug_enabled &&
(this->check_flag(Flag::IS_CLIENT_CUSTOMIZATION) != c->config.check_flag(Client::Flag::IS_CLIENT_CUSTOMIZATION))) {
return JoinError::VERSION_CONFLICT;
}
if (!c->login->account->check_flag(Account::Flag::FREE_JOIN_GAMES)) {
if (password && !this->password.empty() && (*password != this->password)) { if (password && !this->password.empty() && (*password != this->password)) {
return JoinError::INCORRECT_PASSWORD; return JoinError::INCORRECT_PASSWORD;
} }
@@ -816,8 +829,9 @@ Lobby::JoinError Lobby::join_error_for_client(std::shared_ptr<Client> c, const s
if (this->quest) { if (this->quest) {
size_t num_clients = this->count_clients() + 1; size_t num_clients = this->count_clients() + 1;
bool v1_present = is_v1(c->version()) || this->any_v1_clients_present(); bool v1_present = is_v1(c->version()) || this->any_v1_clients_present();
if (!c->can_see_quest(this->quest, this->event, this->difficulty, num_clients, v1_present) || auto this_sh = this->shared_from_this();
!c->can_play_quest(this->quest, this->event, this->difficulty, num_clients, v1_present)) { if (!c->can_see_quest(this->quest, this_sh, this->event, this->difficulty, num_clients, v1_present) ||
!c->can_play_quest(this->quest, this_sh, this->event, this->difficulty, num_clients, v1_present)) {
return JoinError::NO_ACCESS_TO_QUEST; return JoinError::NO_ACCESS_TO_QUEST;
} }
} }
@@ -842,9 +856,9 @@ shared_ptr<Lobby::FloorItem> Lobby::find_item(uint8_t floor, uint32_t item_id) c
return this->floor_item_managers.at(floor).find(item_id); return this->floor_item_managers.at(floor).find(item_id);
} }
void Lobby::add_item(uint8_t floor, const ItemData& data, float x, float z, uint16_t visibility_flags) { void Lobby::add_item(uint8_t floor, const ItemData& data, float x, float z, uint16_t flags) {
auto& m = this->floor_item_managers.at(floor); auto& m = this->floor_item_managers.at(floor);
m.add(data, x, z, visibility_flags); m.add(data, x, z, flags);
this->evict_items_from_floor(floor); this->evict_items_from_floor(floor);
} }
@@ -914,11 +928,11 @@ void Lobby::assign_inventory_and_bank_item_ids(shared_ptr<Client> c, bool consum
} }
} }
unordered_map<uint32_t, shared_ptr<Client>> Lobby::clients_by_serial_number() const { unordered_map<uint32_t, shared_ptr<Client>> Lobby::clients_by_account_id() const {
unordered_map<uint32_t, shared_ptr<Client>> ret; unordered_map<uint32_t, shared_ptr<Client>> ret;
for (auto c : this->clients) { for (auto c : this->clients) {
if (c) { if (c && c->login) {
ret.emplace(c->license->serial_number, c); ret.emplace(c->login->account->account_id, c);
} }
} }
return ret; return ret;
@@ -930,10 +944,11 @@ QuestIndex::IncludeCondition Lobby::quest_include_condition() const {
return [this, num_players, v1_present](shared_ptr<const Quest> q) -> QuestIndex::IncludeState { return [this, num_players, v1_present](shared_ptr<const Quest> q) -> QuestIndex::IncludeState {
bool is_enabled = true; bool is_enabled = true;
for (const auto& lc : this->clients) { for (const auto& lc : this->clients) {
if (lc && !lc->can_see_quest(q, this->event, this->difficulty, num_players, v1_present)) { auto this_sh = this->shared_from_this();
if (lc && !lc->can_see_quest(q, this_sh, this->event, this->difficulty, num_players, v1_present)) {
return QuestIndex::IncludeState::HIDDEN; return QuestIndex::IncludeState::HIDDEN;
} }
if (lc && !lc->can_play_quest(q, this->event, this->difficulty, num_players, v1_present)) { if (lc && !lc->can_play_quest(q, this_sh, this->event, this->difficulty, num_players, v1_present)) {
is_enabled = false; is_enabled = false;
} }
} }
@@ -962,7 +977,9 @@ bool Lobby::compare_shared(const shared_ptr<const Lobby>& a, const shared_ptr<co
// 5. Difficulty: Normal < Hard < Very Hard < Ultimate // 5. Difficulty: Normal < Hard < Very Hard < Ultimate
// 6. Game name // 6. Game name
static auto get_priority = +[](const shared_ptr<const Lobby>& l) -> size_t { static auto get_priority = +[](const shared_ptr<const Lobby>& l) -> size_t {
if (l->check_flag(Lobby::Flag::QUEST_IN_PROGRESS) || l->check_flag(Lobby::Flag::BATTLE_IN_PROGRESS)) { if (l->check_flag(Lobby::Flag::QUEST_SELECTION_IN_PROGRESS) ||
l->check_flag(Lobby::Flag::QUEST_IN_PROGRESS) ||
l->check_flag(Lobby::Flag::BATTLE_IN_PROGRESS)) {
return 4; return 4;
} }
size_t num_clients = l->count_clients(); size_t num_clients = l->count_clients();
+24 -17
View File
@@ -29,7 +29,13 @@ struct Lobby : public std::enable_shared_from_this<Lobby> {
float x; float x;
float z; float z;
uint64_t drop_number; uint64_t drop_number;
uint16_t visibility_flags; // The low 12 bits of flags are visibility flags, specifying which clients
// can see the item. (In practice, only the lowest 4 of these bits are used,
// but the game has fields for 12 players so we do too.)
// The 13th bit (0x1000) specifies whether a rare item notification should
// be sent to all players when the item is picked up. This has no effect for
// non-rare items.
uint16_t flags;
bool visible_to_client(uint8_t client_id) const; bool visible_to_client(uint8_t client_id) const;
}; };
@@ -46,7 +52,7 @@ struct Lobby : public std::enable_shared_from_this<Lobby> {
bool exists(uint32_t item_id) const; bool exists(uint32_t item_id) const;
std::shared_ptr<FloorItem> find(uint32_t item_id) const; std::shared_ptr<FloorItem> find(uint32_t item_id) const;
void add(const ItemData& item, float x, float z, uint16_t visibility_flags); void add(const ItemData& item, float x, float z, uint16_t flags);
void add(std::shared_ptr<FloorItem> fi); void add(std::shared_ptr<FloorItem> fi);
std::shared_ptr<FloorItem> remove(uint32_t item_id, uint8_t client_id); std::shared_ptr<FloorItem> remove(uint32_t item_id, uint8_t client_id);
std::unordered_set<std::shared_ptr<FloorItem>> evict(); std::unordered_set<std::shared_ptr<FloorItem>> evict();
@@ -61,14 +67,16 @@ struct Lobby : public std::enable_shared_from_this<Lobby> {
PERSISTENT = 0x00000002, PERSISTENT = 0x00000002,
// Flags used only for games // Flags used only for games
CHEATS_ENABLED = 0x00000100, CHEATS_ENABLED = 0x00000100,
QUEST_IN_PROGRESS = 0x00000200, QUEST_SELECTION_IN_PROGRESS = 0x00000200,
BATTLE_IN_PROGRESS = 0x00000400, QUEST_IN_PROGRESS = 0x00000400,
JOINABLE_QUEST_IN_PROGRESS = 0x00000800, BATTLE_IN_PROGRESS = 0x00000800,
IS_SPECTATOR_TEAM = 0x00002000, // episode must be EP3 also JOINABLE_QUEST_IN_PROGRESS = 0x00001000,
SPECTATORS_FORBIDDEN = 0x00004000, IS_CLIENT_CUSTOMIZATION = 0x00002000,
START_BATTLE_PLAYER_IMMEDIATELY = 0x00008000, IS_SPECTATOR_TEAM = 0x00004000, // .episode must be EP3 also
CANNOT_CHANGE_CHEAT_MODE = 0x00010000, SPECTATORS_FORBIDDEN = 0x00008000,
USE_CREATOR_SECTION_ID = 0x00020000, START_BATTLE_PLAYER_IMMEDIATELY = 0x00010000,
CANNOT_CHANGE_CHEAT_MODE = 0x00020000,
USE_CREATOR_SECTION_ID = 0x00040000,
// Flags used only for lobbies // Flags used only for lobbies
PUBLIC = 0x01000000, PUBLIC = 0x01000000,
DEFAULT = 0x02000000, DEFAULT = 0x02000000,
@@ -83,8 +91,7 @@ struct Lobby : public std::enable_shared_from_this<Lobby> {
SERVER_DUPLICATE = 4, SERVER_DUPLICATE = 4,
}; };
std::weak_ptr<ServerState> std::weak_ptr<ServerState> server_state;
server_state;
PrefixedLogger log; PrefixedLogger log;
uint32_t lobby_id; uint32_t lobby_id;
@@ -115,6 +122,7 @@ struct Lobby : public std::enable_shared_from_this<Lobby> {
GameMode mode; GameMode mode;
uint8_t difficulty; // 0-3 uint8_t difficulty; // 0-3
uint16_t base_exp_multiplier; uint16_t base_exp_multiplier;
float exp_share_multiplier;
float challenge_exp_multiplier; float challenge_exp_multiplier;
std::string password; std::string password;
std::string name; std::string name;
@@ -269,14 +277,13 @@ struct Lobby : public std::enable_shared_from_this<Lobby> {
std::shared_ptr<Client> c, std::shared_ptr<Client> c,
ssize_t required_client_id = -1); ssize_t required_client_id = -1);
std::shared_ptr<Client> find_client( std::shared_ptr<Client> find_client(const std::string* identifier = nullptr, uint64_t account_id = 0);
const std::string* identifier = nullptr,
uint64_t serial_number = 0);
enum class JoinError { enum class JoinError {
ALLOWED = 0, ALLOWED = 0,
FULL, FULL,
VERSION_CONFLICT, VERSION_CONFLICT,
QUEST_SELECTION_IN_PROGRESS,
QUEST_IN_PROGRESS, QUEST_IN_PROGRESS,
BATTLE_IN_PROGRESS, BATTLE_IN_PROGRESS,
LOADING, LOADING,
@@ -290,7 +297,7 @@ struct Lobby : public std::enable_shared_from_this<Lobby> {
bool item_exists(uint8_t floor, uint32_t item_id) const; bool item_exists(uint8_t floor, uint32_t item_id) const;
std::shared_ptr<FloorItem> find_item(uint8_t floor, uint32_t item_id) const; std::shared_ptr<FloorItem> find_item(uint8_t floor, uint32_t item_id) const;
void add_item(uint8_t floor, const ItemData& item, float x, float z, uint16_t visibility_flags); void add_item(uint8_t floor, const ItemData& item, float x, float z, uint16_t flags);
void add_item(uint8_t floor, std::shared_ptr<FloorItem>); void add_item(uint8_t floor, std::shared_ptr<FloorItem>);
void evict_items_from_floor(uint8_t floor); void evict_items_from_floor(uint8_t floor);
std::shared_ptr<FloorItem> remove_item(uint8_t floor, uint32_t item_id, uint8_t requesting_client_id); std::shared_ptr<FloorItem> remove_item(uint8_t floor, uint32_t item_id, uint8_t requesting_client_id);
@@ -301,7 +308,7 @@ struct Lobby : public std::enable_shared_from_this<Lobby> {
QuestIndex::IncludeCondition quest_include_condition() const; QuestIndex::IncludeCondition quest_include_condition() const;
std::unordered_map<uint32_t, std::shared_ptr<Client>> clients_by_serial_number() const; std::unordered_map<uint32_t, std::shared_ptr<Client>> clients_by_account_id() const;
static void dispatch_on_idle_timeout(evutil_socket_t, short, void* ctx); static void dispatch_on_idle_timeout(evutil_socket_t, short, void* ctx);

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