From cc99050964b43d7a7a1fabab870085b79fdd6332 Mon Sep 17 00:00:00 2001 From: Martin Michelsen Date: Wed, 30 Apr 2025 21:43:06 -0700 Subject: [PATCH] switch to coroutine execution model --- CMakeLists.txt | 52 +- README.md | 142 +- TODO.md | 11 +- notes/ar-codes.txt | 3 + notes/ep3-nte-differences.txt | 2 + notes/handler-tables.txt | 78 +- src/Account.cc | 80 +- src/AddressTranslator.cc | 86 +- src/AsyncHTTPServer.cc | 409 + src/AsyncHTTPServer.hh | 228 + src/AsyncUtils.cc | 160 + src/AsyncUtils.hh | 252 + src/BattleParamsIndex.cc | 32 +- src/CatSession.cc | 187 - src/CatSession.hh | 54 - src/Channel.cc | 475 +- src/Channel.hh | 192 +- src/ChatCommands.cc | 2581 +- src/ChatCommands.hh | 6 +- src/ChoiceSearch.hh | 6 +- src/Client.cc | 355 +- src/Client.hh | 257 +- src/CommandFormats.hh | 89 +- src/CommonFileFormats.hh | 15 +- src/CommonItemSet.cc | 98 +- src/CommonItemSet.hh | 8 +- src/Compression.cc | 14 +- src/DCSerialNumbers.cc | 20 +- src/DNSServer.cc | 91 +- src/DNSServer.hh | 32 +- src/DownloadSession.cc | 491 +- src/DownloadSession.hh | 48 +- src/EnemyType.cc | 2 +- src/Episode3/BattleRecord.cc | 78 +- src/Episode3/BattleRecord.hh | 14 +- src/Episode3/Card.cc | 168 +- src/Episode3/CardSpecial.cc | 390 +- src/Episode3/DataIndexes.cc | 587 +- src/Episode3/DataIndexes.hh | 5 +- src/Episode3/DeckState.cc | 10 +- src/Episode3/DeckState.hh | 2 +- src/Episode3/MapState.cc | 4 +- src/Episode3/PlayerState.cc | 42 +- src/Episode3/PlayerState.hh | 6 +- src/Episode3/PlayerStateSubordinates.cc | 180 +- src/Episode3/RulerServer.cc | 68 +- src/Episode3/Server.cc | 166 +- src/Episode3/Server.hh | 31 +- src/Episode3/Tournament.cc | 48 +- src/Episode3/Tournament.hh | 1 - src/EventUtils.cc | 62 - src/EventUtils.hh | 45 - src/FunctionCompiler.cc | 107 +- src/FunctionCompiler.hh | 3 - src/GameServer.cc | 196 + src/GameServer.hh | 48 + src/HTTPServer.cc | 1426 +- src/HTTPServer.hh | 124 +- src/IPFrameInfo.cc | 39 +- src/IPFrameInfo.hh | 1 - src/IPStackSimulator.cc | 1366 +- src/IPStackSimulator.hh | 290 +- src/IPV4RangeSet.cc | 12 +- src/IPV4RangeSet.hh | 2 +- src/IntegralExpression.cc | 6 +- src/ItemCreator.cc | 174 +- src/ItemCreator.hh | 6 +- src/ItemData.cc | 8 +- src/ItemNameIndex.cc | 266 +- src/ItemParameterTable.cc | 98 +- src/ItemTranslationTable.cc | 16 +- src/Items.cc | 4 +- src/Items.hh | 2 +- src/LevelTable.cc | 2 +- src/LevelTable.hh | 26 +- src/Lobby.cc | 104 +- src/Lobby.hh | 7 +- src/Loggers.cc | 30 +- src/Main.cc | 734 +- src/Map.cc | 549 +- src/Map.hh | 19 +- src/Menu.cc | 11 +- src/Menu.hh | 37 +- src/NetworkAddresses.cc | 72 +- src/NetworkAddresses.hh | 5 +- src/PPKArchive.cc | 8 +- src/PSOEncryption.cc | 171 +- src/PSOEncryption.hh | 126 +- src/PSOGCObjectGraph.cc | 4 +- src/PSOProtocol.cc | 10 +- src/PSOProtocol.hh | 9 +- src/PatchFileIndex.cc | 54 +- src/PatchServer.cc | 478 - src/PatchServer.hh | 128 - src/PlayerFilesManager.cc | 81 +- src/PlayerFilesManager.hh | 11 +- src/PlayerInventory.hh | 10 +- src/PlayerSubordinates.hh | 219 +- src/ProxyCommands.cc | 3054 +- src/ProxyCommands.hh | 15 +- src/ProxyServer.cc | 1047 - src/ProxyServer.hh | 316 - src/ProxySession.cc | 88 + src/ProxySession.hh | 88 + src/Quest.cc | 170 +- src/QuestScript.cc | 380 +- src/RareItemSet.cc | 130 +- src/ReceiveCommands.cc | 4525 +- src/ReceiveCommands.hh | 16 +- src/ReceiveSubcommands.cc | 1718 +- src/ReceiveSubcommands.hh | 15 +- src/ReplaySession.cc | 750 +- src/ReplaySession.hh | 41 +- src/SaveFileFormats.cc | 206 +- src/SaveFileFormats.hh | 44 +- src/SendCommands.cc | 698 +- src/SendCommands.hh | 164 +- src/Server.cc | 351 - src/Server.hh | 155 +- src/ServerShell.cc | 30 +- src/ServerShell.hh | 5 - src/ServerState.cc | 951 +- src/ServerState.hh | 106 +- src/ShellCommands.cc | 549 +- src/ShellCommands.hh | 13 +- src/SignalWatcher.cc | 64 +- src/SignalWatcher.hh | 9 +- src/TeamIndex.cc | 22 +- src/Text.cc | 11 +- src/Text.hh | 6 +- src/TextIndex.cc | 27 +- src/Version.cc | 100 +- src/Version.hh | 8 +- src/WordSelectTable.cc | 34 +- .../NoRareSelling/NoRareSelling.59NL.patch.s | 6 +- system/config.example.json | 122 +- tests/BB-CreateCharGame.test.disabled.txt | 33708 --- tests/DC-11-2000-GameSmokeTest.test.txt | 2383 + tests/DC-NTE-GameSmokeTest.test.txt | 2456 + tests/DCNTE-GameSmokeTest.test.txt | 5102 - ...1-DCv2-PCv2-CrossplayPrivateDrops.test.txt | 19846 ++ tests/DCv1-GameSmokeTest.test.txt | 1214 - tests/DCv2-GameSmokeTest.test.txt | 1613 - tests/GC-CustomizationSegregation.test.txt | 11968 +- tests/GC-Episode2PrivateDrops2P.test.txt | 13793 -- ...sode3BattleVirusAndCreatureTech.rdtest.txt | 28940 --- ...Episode3BattleWithSpectatorTeam.rdtest.txt | 54704 ----- ...pisode3TrialEditionLobbySmokeTest.test.txt | 2828 - tests/GC-ForestGame.test.txt | 10032 - tests/GC-HeartSymbol.test.txt | 5986 +- tests/GC-JungleGame.test.txt | 16381 ++ tests/GC-PoisonRoom.test.txt | 27497 --- tests/GC-XB-CrossplayForestGame.test.txt | 21990 ++ tests/GCEp3-BattleWithSpectatorTeam.test.txt | 162082 +++++++++++++++ ...de.rdtest.txt => GCEp3-CardTrade.test.txt} | 8813 +- tests/GCEp3-TrialEditionBattle.test.txt | 23386 +++ tests/PC-BasicGame.test.txt | 1167 - tests/PC-DCv1-CrossplayPrivateDrops.test.txt | 7666 - tests/XB-ForestGame.test.txt | 1520 - tests/config.json | 192 +- 160 files changed, 269127 insertions(+), 227736 deletions(-) create mode 100644 src/AsyncHTTPServer.cc create mode 100644 src/AsyncHTTPServer.hh create mode 100644 src/AsyncUtils.cc create mode 100644 src/AsyncUtils.hh delete mode 100644 src/CatSession.cc delete mode 100644 src/CatSession.hh delete mode 100644 src/EventUtils.cc delete mode 100644 src/EventUtils.hh create mode 100644 src/GameServer.cc create mode 100644 src/GameServer.hh delete mode 100644 src/PatchServer.cc delete mode 100644 src/PatchServer.hh delete mode 100644 src/ProxyServer.cc delete mode 100644 src/ProxyServer.hh create mode 100644 src/ProxySession.cc create mode 100644 src/ProxySession.hh delete mode 100644 src/Server.cc delete mode 100644 tests/BB-CreateCharGame.test.disabled.txt create mode 100644 tests/DC-11-2000-GameSmokeTest.test.txt create mode 100644 tests/DC-NTE-GameSmokeTest.test.txt delete mode 100644 tests/DCNTE-GameSmokeTest.test.txt create mode 100644 tests/DCv1-DCv2-PCv2-CrossplayPrivateDrops.test.txt delete mode 100644 tests/DCv1-GameSmokeTest.test.txt delete mode 100644 tests/DCv2-GameSmokeTest.test.txt delete mode 100644 tests/GC-Episode2PrivateDrops2P.test.txt delete mode 100644 tests/GC-Episode3BattleVirusAndCreatureTech.rdtest.txt delete mode 100644 tests/GC-Episode3BattleWithSpectatorTeam.rdtest.txt delete mode 100644 tests/GC-Episode3TrialEditionLobbySmokeTest.test.txt delete mode 100644 tests/GC-ForestGame.test.txt create mode 100644 tests/GC-JungleGame.test.txt delete mode 100644 tests/GC-PoisonRoom.test.txt create mode 100644 tests/GC-XB-CrossplayForestGame.test.txt create mode 100644 tests/GCEp3-BattleWithSpectatorTeam.test.txt rename tests/{GC-Episode3CardTrade.rdtest.txt => GCEp3-CardTrade.test.txt} (58%) create mode 100644 tests/GCEp3-TrialEditionBattle.test.txt delete mode 100644 tests/PC-BasicGame.test.txt delete mode 100644 tests/PC-DCv1-CrossplayPrivateDrops.test.txt delete mode 100644 tests/XB-ForestGame.test.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 25d2cb80..fd45b55c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,18 +19,14 @@ endif() # Library search -find_path (LIBEVENT_INCLUDE_DIR NAMES event.h) -find_library (LIBEVENT_LIBRARY NAMES event) -find_library (LIBEVENT_CORE NAMES event_core) -find_library (LIBEVENT_PTHREADS NAMES event_pthreads) -set (LIBEVENT_INCLUDE_DIRS ${LIBEVENT_INCLUDE_DIR}) -set (LIBEVENT_LIBRARIES - ${LIBEVENT_LIBRARY} - ${LIBEVENT_CORE} - ${LIBEVENT_PTHREADS}) - +find_path(ASIO_INCLUDE_DIR NAMES asio.hpp HINTS "${WINDOWS_ENV}/include" REQUIRED) +if(WIN32) + find_path(Iconv_INCLUDE_DIRS NAMES iconv.h HINTS "${WINDOWS_ENV}/include" REQUIRED) + find_library(Iconv_LIBRARIES NAMES iconv HINTS "${WINDOWS_ENV}/lib" REQUIRED) +else() + find_package(Iconv REQUIRED) +endif() find_package(phosg REQUIRED) -find_package(Iconv REQUIRED) find_package(resource_file REQUIRED) @@ -54,10 +50,12 @@ add_custom_target( set(SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/Revision.cc src/Account.cc + src/AddressTranslator.cc src/AFSArchive.cc + src/AsyncHTTPServer.cc + src/AsyncUtils.cc src/BattleParamsIndex.cc src/BMLArchive.cc - src/CatSession.cc src/Channel.cc src/ChatCommands.cc src/ChoiceSearch.cc @@ -80,9 +78,9 @@ set(SOURCES src/Episode3/RulerServer.cc src/Episode3/Server.cc src/Episode3/Tournament.cc - src/EventUtils.cc src/FileContentsCache.cc src/FunctionCompiler.cc + src/GameServer.cc src/GSLArchive.cc src/HTTPServer.cc src/ImageEncoder.cc @@ -94,8 +92,8 @@ set(SOURCES src/ItemData.cc src/ItemNameIndex.cc src/ItemParameterTable.cc - src/ItemTranslationTable.cc src/Items.cc + src/ItemTranslationTable.cc src/LevelTable.cc src/Lobby.cc src/Loggers.cc @@ -104,12 +102,11 @@ set(SOURCES src/Menu.cc src/NetworkAddresses.cc src/PatchFileIndex.cc - src/PatchServer.cc src/PlayerFilesManager.cc src/PlayerSubordinates.cc src/PPKArchive.cc src/ProxyCommands.cc - src/ProxyServer.cc + src/ProxySession.cc src/PSOEncryption.cc src/PSOGCObjectGraph.cc src/PSOProtocol.cc @@ -119,10 +116,8 @@ set(SOURCES src/ReceiveCommands.cc src/ReceiveSubcommands.cc src/ReplaySession.cc - src/Revision.cc src/SaveFileFormats.cc src/SendCommands.cc - src/Server.cc src/ServerShell.cc src/ServerState.cc src/ShellCommands.cc @@ -135,13 +130,13 @@ set(SOURCES src/WordSelectTable.cc ) -if(resource_file_FOUND) - set(SOURCES ${SOURCES} src/AddressTranslator.cc) -endif() - add_executable(newserv ${SOURCES}) -target_include_directories(newserv PUBLIC ${LIBEVENT_INCLUDE_DIR} ${Iconv_INCLUDE_DIRS}) -target_link_libraries(newserv phosg::phosg ${LIBEVENT_LIBRARIES} ${Iconv_LIBRARIES} pthread resource_file::resource_file) +target_include_directories(newserv PUBLIC ${ASIO_INCLUDE_DIR} ${Iconv_INCLUDE_DIRS}) +target_link_libraries(newserv phosg::phosg ${Iconv_LIBRARIES} pthread resource_file::resource_file) +if (WIN32) + target_compile_definitions(newserv PUBLIC -DWINVER=0x0A00 -D_WIN32_WINNT=0x0A00) + target_link_libraries(newserv ws2_32 mswsock bcrypt iphlpapi -static -static-libgcc -static-libstdc++) +endif() add_dependencies(newserv newserv-Revision-cc) # target_compile_options(newserv PRIVATE -fsanitize=address) @@ -163,15 +158,6 @@ foreach(LogTestCase IN ITEMS ${LogTestCases}) COMMAND ${CMAKE_BINARY_DIR}/newserv --replay-log=${LogTestCase} --config=${CMAKE_SOURCE_DIR}/tests/config.json) endforeach() -if(resource_file_FOUND) - foreach(LogRDTestCase IN ITEMS ${LogRDTestCases}) - add_test( - NAME ${LogRDTestCase} - WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} - COMMAND ${CMAKE_BINARY_DIR}/newserv --replay-log=${LogRDTestCase} --config=${CMAKE_SOURCE_DIR}/tests/config.json) - endforeach() -endif() - file(GLOB ScriptTestCases ${CMAKE_SOURCE_DIR}/tests/*.test.sh) foreach(ScriptTestCase IN ITEMS ${ScriptTestCases}) diff --git a/README.md b/README.md index 2edea95f..a4f58af6 100644 --- a/README.md +++ b/README.md @@ -12,8 +12,8 @@ See TODO.md for a list of known issues and future work I've curated, or go to th * Background * [History](#history) * [Other server projects](#other-server-projects) - * [Developer information](#developer-information) * [Using newserv in other projects](#using-newserv-in-other-projects) + * [Contributing to newserv](#contributing-to-newserv) * [Compatibility](#compatibility) * Setup * [Server setup](#server-setup) @@ -46,7 +46,7 @@ For a while it was essentially necessary to use a proxy to go online at all, so Sometime in 2006 or 2007, I abandoned Khyller and rebuilt the entire thing from scratch, resulting in Aeon. Aeon was substantially cleaner in code than Khyller but still fairly hard to work with, and it lacked a few of the more arcane features I had originally written (for example, the ability to convert any quest into a download quest). In addition, the code still had some stability problems... it turns out that Aeon's concurrency primitives were simply incorrect. I had derived the concept of a mutex myself, before taking any real computer engineering classes, but had implemented it incorrectly. I made the race window as small as possible, but Aeon would still randomly crash after running seemingly fine for a few days. -At the time of its inception, Aeon was also called newserv, and you may find some beta releases floating around the Internet with filenames like `newserv-b3.zip`. I had released betas 1, 2, and 3 before I released the entire source of beta 5 and stopped working on the project when I went to college. This was around the time when I switched from writing software primarily on Windows to primarily on macOS and Linux, so Aeon beta 5 was the last server I wrote that specifically targeted Windows. (newserv, which you're looking at now, is a bit tedious to compile on Windows but does work.) +At the time of its inception, Aeon was also called newserv, and you may find some beta releases floating around the Internet with filenames like `newserv-b3.zip`. I had released betas 1, 2, and 3 before I released the entire source of beta 5 and stopped working on the project when I went to college. This was around the time when I switched from writing software primarily on Windows to primarily on macOS and Linux, so Aeon beta 5 was the last server I wrote that specifically targeted Windows. (newserv, which you're looking at now, is difficult to compile on Windows but does work.) After a long hiatus from PSO and much professional and personal development in my technical abilities, I was reminiscing sometime in October 2018 by reading my old code archives. Somehow inspired when I came across Aeon, I spent a weekend and a couple more evenings rewriting the entire project again, cleaning up ancient patterns I had used eleven years ago, replacing entire modules with simple STL containers, and eliminating even more support files in favor of configuration autodetection. The code is now suitably modern and stable, and I'm not embarrassed by its existence, as I am by Aeon beta 5's source code and my archive of Khyller (which, thankfully, no one else ever saw). @@ -67,9 +67,15 @@ Independently of this project, there are many other PSO servers out there. Those * (2021) **[Phantasmal World](https://github.com/DaanVandenBosch/phantasmal-world)**: A set of PSO tools, including a web-based model viewer and quest builder, and a PSO server, written by Daan Vanden Bosch. * (2021) **[Elseware](http://git.sharnoth.com/jake/elseware)**: A PSOBB server written in Rust by Jake. -## Developer information +## Using newserv in other projects -There is a lot of code in this project that could be useful as a reference. Some of the more notable files are: +You are free to use code from newserv in your own open-source projects; the only condition is that the contents of the LICENSE file must be included in your project if you use code from newserv. Your project does not also have to use the MIT license; you can use any license you want. + +If you want to use parts of newserv in your project, there are two easy ways to do so with proper licensing: +* If you're using a lot of code from newserv, you can put a copy of newserv's LICENSE file in your repository alongside your own license file, or include the contents of newserv's license in your own license file. +* If you're only using a few files from newserv, you can copy and paste the contents of the LICENSE file into a comment at the beginning of each copied file. + +Some of the more likely useful files are: * **src/CommandFormats.hh**: Complete listing of all network commands used in all known versions of the game, and their formats * **src/CommonItemSet.hh/cc**: Format of ItemPT files, shop definition files, and tekker adjustment tables * **src/DCSerialNumbers.hh/cc**: PSO DC serial number validation algorithm and serial number generator @@ -83,13 +89,13 @@ There is a lot of code in this project that could be useful as a reference. Some * **src/Episode3/DataIndexes.hh**: Episode 3 file structures, including card definition format and map/quest format * **system/item-tables/names-v4.json**: Names of all items, indexed by the first 3 bytes of data1 -## Using newserv in other projects +## Contributing to newserv -There is a fair amount of code in this project that could potentially be useful to other projects. You are free to use code from newserv in your own open-source projects; the only condition is that the contents of the LICENSE file must be included in your project if you use code from newserv. Your project does not also have to use the MIT license; you can use any license you want. +The goals of this project are: +* Build stable, extensible PSO server software that includes all vanilla functionality as well as optional modern conveniences, features, and cheats. +* Document the internals of PSO's network protocol, file formats, and game mechanics. This is mainly done through comments in the code. -If you want to use parts of newserv in your project, there are two easy ways to do so with proper licensing: -* If you're using a lot of code from newserv, you can put a copy of newserv's LICENSE file in your repository alongside your own license file, or include the contents of newserv's license in your own license file. -* If you're only using a few files from newserv, you can copy and paste the contents of the LICENSE file into a comment at the beginning of each copied file. +This is a personal project; there is no official development team, official website, or official instance of newserv. Issues and pull requests are certainly welcome, but please only add content (e.g. quests or patches) that you've created, is already public, or you have permission to release publicly. # Compatibility @@ -97,14 +103,14 @@ newserv supports all known versions of PSO, including various development protot | Version | Lobbies | Games | Proxy | |-----------------|----------|----------|----------| -| DC NTE | Yes | Yes | No | -| DC 11/2000 | Yes | Yes | No | +| DC NTE | Yes | Yes | Yes | +| DC 11/2000 | Yes | Yes | Yes | | DC 12/2000 | Yes | Yes | Yes | | DC 01/2001 | Yes | Yes | Yes | | DC V1 | Yes | Yes | Yes | | DC 08/2001 | Yes | Yes | Yes | | DC V2 | Yes | Yes | Yes | -| PC NTE | Yes (1) | Yes | No | +| PC NTE | Yes (1) | Yes | Yes | | PC | Yes | Yes | Yes | | GC Ep1&2 NTE | Yes | Yes | Yes | | GC Ep1&2 | Yes | Yes | Yes | @@ -138,18 +144,19 @@ If you're on an older version of Windows (before Windows 10), the Cygwin librari ### Linux -There are currently no precompiled releases for Linux. To run newserv on Linux, you'll have to build it from source - see the "Building from source" section below. +There are currently no precompiled releases for Linux. To run newserv on Linux, you'll have to build it from source - see the section below. -### Building from source +### Building from source (macOS/Linux) -1. Install the packages newserv depends on. - * If you're on Windows, install [Cygwin](https://www.cygwin.com/). While doing so, install the `cmake`, `gcc-core`, `gcc-g++`, `git`, `libevent2.1_7`, `libevent-devel`, `make`, `libiconv-devel`, and `zlib` packages. Do the rest of these steps inside a Cygwin shell (not a Windows cmd shell or PowerShell). - * If you're on macOS, run `brew install cmake libevent libiconv`. - * If you're on Linux, run `sudo apt-get install cmake libevent-dev` (or use your Linux distribution's package manager). -3. Build and install [phosg](https://github.com/fuzziqersoftware/phosg) and [resource_dasm](https://github.com/fuzziqersoftware/resource_dasm). -5. Run `cmake . && make` in the newserv directory. +To build on macOS or Linux: -After building newserv, edit system/config.example.json as needed **and rename it to system/config.json** (note that this step is not necessary for the precompiled releases!), set up [client patch directories](#client-patch-directories) if you're planning to play Blue Burst, then run `./newserv` in newserv's directory. +1. Install the dependencies needed for your platform: + * macOS: `brew install cmake asio libiconv` + * Linux: `sudo apt-get install cmake libasio-dev` (or use your Linux distribution's package manager) +2. Build and install [phosg](https://github.com/fuzziqersoftware/phosg) and [resource_dasm](https://github.com/fuzziqersoftware/resource_dasm). +3. Run `cmake . && make` in the newserv directory. + +After building newserv, edit system/config.example.json as needed **and rename it to system/config.json** (note that this step is not necessary for the precompiled releases), set up [client patch directories](#client-patch-directories) if you're planning to play Blue Burst, then run `./newserv` in newserv's directory. The server has an interactive shell which can be used to make changes, such as managing user accounts, updating the server's configuration, managing Episode 3 tournaments, and more. Type `help` and press Enter to see all the commands. @@ -157,6 +164,10 @@ On Linux and macOS, the server also responds to SIGUSR1 and SIGUSR2. SIGUSR1 doe To use newserv in other ways (e.g. for translating data), see the end of this document. +### Building from source (Windows) + +The current version of newserv is cross-compiled using mingw-w64 on a macOS build machine, with the necessary libraries manually installed. Setting up such a build environment is tedious and not recommended; it's recommended to just use a release version instead. + ## Client patch directories newserv implements a patch server for PSO PC and PSO BB game data. Any file or directory you put in the system/patch-bb or system/patch-pc directories will be synced to clients when they connect to the patch server. @@ -276,7 +287,8 @@ A license is a set of credentials that a player can use to log in. There are six * *GC licenses* consist of a 10-digit decimal serial number, a 12-character access key, and a password of up to 8 characters. * *XB licenses* consist of a gamertag of up to 16 characters, a 16-character hex user ID, and a 16-character hex account ID. * *BB licenses* consist of a username of up to 16 characters and a password of up to 16 characters. -Each account may have multiple licenses. To add a license to an account, use `add-license` in the shell. + +Each account may have multiple licenses. To add a license to an existing account, use `add-license` in the shell. On BB, character data is scoped to the license, but system and Guild Card data is scoped to the account. That is, an account with multiple BB licenses can have more than 4 characters (up to 4 per license), but they will all share the same team membership and Guild Card lists. @@ -374,7 +386,9 @@ newserv has the ability to save character data on the server side. For PSO BB, t Each account has 4 BB character slots and 16 non-BB character file slots. The non-BB slots are independent of the BB slots, and can be accessed with the `$savechar ` and `$loadchar ` 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 `, which behaves similarly to `$savechar` but writes the character data to a BB character slot in a different account instead (slots are numbered 1 through 4). This can be used to "upgrade" a character to BB from an earlier version. +You can see basic information about a character saved on the server (without affecting your current character) by using `$checkchar `. You can delete a previously-saved character with `$deletechar `. + +There is also the command `$bbchar `, 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: @@ -497,9 +511,9 @@ If you want to play online on remote servers rather than running your own server To use the proxy for PSO DC, PC, or GC, add an entry to the corresponding ProxyDestinations dictionary in config.json, then run newserv and connect to it as normal (see below). You'll see a "Proxy server" option in the main menu, and you can pick which remote server to connect to. -To use the proxy for PSO BB, set the ProxyDestination-BB entry in config.json. If this option is set, it essentially disables the game server for all PSO BB clients - all clients will be proxied to the specified destination instead. Unfortunately, because PSO BB uses a different set of handlers for the data server phase and character selection, there's no in-game way to present the player with a list of options, like there is on PSO PC and PSO GC. +To use the proxy for PSO BB, set the ProxyDestination-BB entry in config.json. If this option is set, it essentially disables the game server for all BB clients - all BB clients will be proxied to the specified destination instead. Unfortunately, because PSO BB uses a different set of handlers for the data server phase and character selection, there's no in-game way to present the player with a list of options, like there is on PSO PC and PSO GC. -When you're on PSO DC, PC, or GC and are connected to a remote server through newserv's proxy, choosing the Change Ship or Change Block action from the lobby counter will send you back to newserv's main menu instead of the remote server's ship or block select menu. You can go back to the server you were just on by choosing it from the proxy server menu again. +When you're on PSO DC, PC, GC, or Xbox and are connected to a remote server through newserv's proxy, choosing the Change Ship or Change Block action from the lobby counter will send you back to newserv's main menu instead of the remote server's ship or block select menu. You can go back to the server you were just on by choosing it from the proxy server menu again. There are many options available when starting a proxy session. All options are off by default unless otherwise noted. The options are: * **Chat commands**: enables chat commands in the proxy session (on by default). @@ -522,54 +536,54 @@ There are many options available when starting a proxy session. All options are * Episode 3 card definitions (saved as .mnr files) * Episode 3 media updates (saved as .gvm, .bml, or .bin files) -The remote server will probably try to assign you a Guild Card number that doesn't match the one you have on newserv. On PSO DC, PC and GC, the proxy server rewrites the commands in transit to make it look like the remote server assigned you the same Guild Card number as you have on newserv, but if the remote server has some external integrations (e.g. forum or Discord bots), they will use the Guild Card number that the remote server believes it has assigned to you. The number assigned by the remote server is shown to you when you first connect to the remote server, and you can retrieve it in lobbies or during games with the `$li` command. +The remote server will probably try to assign you a Guild Card number that doesn't match the one you have on newserv. The proxy rewrites the commands in transit to make it look like the remote server assigned you the same Guild Card number as you have on newserv, but if the remote server has some external integrations (e.g. forum or Discord bots), they will use the Guild Card number that the remote server believes it has assigned to you. The number assigned by the remote server is shown to you when you first connect to the remote server, and you can retrieve it in lobbies or during games with the `$li` command. -Some chat commands (see below) have the same basic function on the proxy server but have different effects or conditions. In addition, there are some server shell commands that affect clients on the proxy (run `help` in the shell to see what they are). If there's only one proxy session open, the shell's proxy commands will affect that session. Otherwise, you'll have to specify which session to affect with the `on` prefix - to send a chat message in LinkedSession:17205AE4, for example, you would run `on 17205AE4 chat ...`. +Some chat commands (see below) have the same basic function on the proxy but have different effects or conditions. In addition, there are some server shell commands that affect clients on the proxy (run `help` in the shell to see what they are). If there's only one proxy session open, the shell's proxy commands will affect that session. Otherwise, you'll have to specify which session to affect with the `on` prefix - to send a chat message in C-17's session, for example, you would run `on C-17 chat ...`. ## Chat commands newserv supports a variety of commands players can use by chatting in-game. Any chat message that begins with `$` is treated as a chat command. (If you actually want to send a chat message starting with `$`, type `$$` instead.) On the DC 11/2000 prototype, `@` is used instead of `$` for all chat commands, since `$` does not appear on the English virtual keyboard. -Some commands only work on the game server and not on the proxy server. The chat commands are: +Some commands only work for clients not in proxy sessions. The chat commands are: * Information commands - * `$li`: Show basic information about the lobby or game you're in. If you're on the proxy server, show information about your connection instead (remote Guild Card number, client ID, etc.). - * `$si` (game server only): Show basic information about the server. - * `$ping`: Show round-trip ping time from the server to you. On the proxy server, show the ping time from you to the proxy and from the proxy to the server. - * `$matcount` (game server only): Show how many of each type of material you've used. - * `$killcount` (game server only): Show the kill count on your currently-equipped weapon. If you're in a game and not on BB, the value is only accurate at the time the item enters the game. + * `$li`: Show basic information about the lobby or game you're in. If you're on the proxy, show information about your connection instead (remote Guild Card number, client ID, etc.). + * `$si`: Show basic information about the server. + * `$ping`: Show round-trip ping time from the server to you. On the proxy, show the ping time from you to the proxy and from the proxy to the server. + * `$matcount` (non-proxy only): Show how many of each type of material you've used. + * `$killcount` (non-proxy only): Show the kill count on your currently-equipped weapon. If you're in a game and not on BB, the value is only accurate at the time the item enters the game. * `$itemnotifs `: Enable item drop notification messages. If the game has private drops enabled, you will only see a notification if the dropped item is visible to you; you won't be notified of other players' drops. The modes are: * `off`: No notifications are shown. * `rare`: You are notified when a rare item drops. * `on`: You are notified when any item drops, except Meseta. * `every`: You are notified when any item drops, including Meseta. * `$announcerares`: Enable or disable announcements for your rare item finds. This determines whether rare items you find will be announced to the game and server, not whether you will see announcements for others finding rare items. - * `$what` (game server only): Show the type, name, and stats of the nearest item on the ground. - * `$where` (game server only): Show your current floor number and coordinates. Mainly useful for debugging. - * `$qfread ` (game server only): Show the value of a quest counter in your player data. The field names are defined in config.json. + * `$what` (non-proxy only): Show the type, name, and stats of the nearest item on the ground. + * `$where`: Show your current floor number and coordinates. Mainly useful for debugging. + * `$qfread ` (non-proxy only): Show the value of a quest counter in your player data. The field names are defined in config.json. * Debugging commands - * `$debug` (game server only): Enable or disable debug. You need the DEBUG flag in your user account to use this command. Enabling debug does several things: + * `$debug`: 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 some actions, like killing enemies, opening boxes, or flipping switches. * You'll see the rare seed value and floor variations when you join a game. * You'll be placed into the last available slot in lobbies and games instead of the first, unless you're joining a BB solo-mode game. * You'll be able to join games with any PSO version, not only those for which cross-version play is normally enabled. See the "Cross-version play" section above for details on this. * Most of the commands in this section are enabled. (A few of them are always enabled and don't require `$debug`.) - * `$whatobj` and `$whatene` (game server only): Tells you what the closest object or enemy spawn point is to your position, along with its coordinates and object or enemy ID. The full definition is also printed to the server's log. These commands can be used without `$debug` enabled. - * `$readmem
` (game server only): Read 4 bytes from the given address and show you the values. - * `$writemem
` (game server only): Write data to the given address. Data is not required to be any specific size. - * `$nativecall
[arg1 ...]` (game server only, GC only): Call a native function on your client. Only arguments passed in registers are supported; calling functions that take many arguments is not supported. - * `$quest ` (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 for this command if the specified quest has the AllowStartFromChatCommand field set in its metadata file. + * `$whatobj` and `$whatene` (non-proxy only): Tells you what the closest object or enemy spawn point is to your position, along with its coordinates and object or enemy ID. The full definition is also printed to the server's log. These commands can be used without `$debug` enabled. + * `$readmem
`: Read 4 bytes from the given address and show you the values. + * `$writemem
`: Write data to the given address. Data is not required to be any specific size. + * `$nativecall
[arg1 ...]` (GC only): Call a native function on your client. Only arguments passed in registers are supported; calling functions that take many arguments is not supported. + * `$quest ` (non-proxy only): Load a quest by quest number. Can be used to load battle or challenge quests with only one player present. `$debug` is not required for this command if the specified quest has the AllowStartFromChatCommand field set in its metadata file. * `$qcall `: Call a quest function on your client. - * `$qcheck ` (game server only): Show the value of a quest flag. This command can be used without `$debug` 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 ` (non-proxy only): Show the value of a quest flag. This command can be used without `$debug` 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 ` or `$qclear `: 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 ` (game server only): Show the value of a quest counter ("global flag"). This command can be used without `$debug` enabled. - * `$qgwrite ` (game server only): Set the value of a quest counter ("global flag") for yourself. + * `$qgread ` (non-proxy only): Show the value of a quest counter ("global flag"). This command can be used without `$debug` enabled. + * `$qgwrite ` (non-proxy only): Set the value of a quest counter ("global flag") for yourself. * `$qsync `: Set a quest register's value for yourself only. `` should be either rXX (e.g. r60) or fXX (e.g. f60); if the latter, `` is parsed as a floating-point value instead of as an integer. * `$qsyncall `: Set a quest register's value for everyone in the game. `` should be either rXX (e.g. r60) or fXX (e.g. f60); if the latter, `` is parsed as a floating-point value instead of as an integer. * `$swset [floor] ` and `$swclear [floor] `: 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` (non-proxy only): Send your own Guild Card to yourself. * `$sc `: Send a command to yourself. * `$ss `: Send a command to the remote server (if in a proxy session) or to the game server. * `$sb `: Send a command to yourself, and to the remote server or game server. @@ -577,31 +591,33 @@ Some commands only work on the game server and not on the proxy server. The chat * Personal state commands * `$arrow `: Change your lobby arrow color. The color may be specified by number (0-12) or by name (red, blue, green, yellow, purple, cyan, orange, pink, white, white2, white3, or black). - * `$secid `: Set your override section ID. After running this command, any games you create will use your override section ID for rare drops instead of your character's actual section ID. If you're in a game and you are the leader of the game, this also immediately changes the item tables used by the server when creating items. To revert to your actual section id, run `$secid` with no name after it. On the proxy server, this will not work if the remote server controls item drops. If the server does not allow cheat mode anywhere (that is, "CheatModeBehavior" is "Off" in config.json), this command does nothing. - * `$rand `: Set your override random seed (specified as a 32-bit hex value). This will make any games you create use the given seed for rare enemies. This also makes item drops deterministic in Blue Burst games hosted by newserv. On the proxy server, this command can cause desyncs with other players in the same game, since they will not see the overridden random seed. To remove the override, run `$rand` with no arguments. If the server does not allow cheat mode anywhere (that is, "CheatModeBehavior" is "Off" in config.json), this command does nothing. - * `$ln [name-or-type]`: Set the lobby number. Visible only to you. This command exists because some non-lobby maps can be loaded as lobbies with invalid lobby numbers. See the "GC lobby types" and "Ep3 lobby types" entries in the information menu for acceptable values here. Note that non-lobby maps do not have a lobby counter, so there's no way to exit the lobby without using either `$ln` again or `$exit`. On the game server, `$ln` reloads the lobby immediately; on the proxy server, it doesn't take effect until you load another lobby yourself (which means you'll like have to use `$exit` to escape). Run this command with no argument to return to the default lobby. + * `$secid `: Set your override section ID. After running this command, any games you create will use your override section ID for rare drops instead of your character's actual section ID. If you're in a game and you are the leader of the game, this also immediately changes the item tables used by the server when creating items. To revert to your actual section id, run `$secid` with no name after it. On the proxy, this will not work if the remote server controls item drops. If the server does not allow cheat mode anywhere (that is, "CheatModeBehavior" is "Off" in config.json), this command does nothing. + * `$rand `: Set your override random seed (specified as a 32-bit hex value). This will make any games you create use the given seed for rare enemies and item drops. On the proxy, this command can cause desyncs with other players in the same game, since they will not see the overridden random seed. To remove the override, run `$rand` with no arguments. If the server does not allow cheat mode anywhere (that is, "CheatModeBehavior" is "Off" in config.json), this command does nothing. + * `$ln [name-or-type]`: Set the lobby number. Visible only to you. This command exists because some non-lobby maps can be loaded as lobbies with invalid lobby numbers. See the "GC lobby types" and "Ep3 lobby types" entries in the information menu for acceptable values here. Note that non-lobby maps do not have a lobby counter, so there's no way to exit the lobby without using either `$ln` again or `$exit`. On the game server, `$ln` reloads the lobby immediately; on the proxy, it doesn't take effect until you load another lobby yourself (which means you'll like have to use `$exit` to escape). Run this command with no argument to return to the default lobby. * `$swa`: Enable or disable switch assist. When enabled, the server will unlock two-player and four-player doors in non-quest games when you step on any of the required switches. * `$exit`: If you're in a lobby, send you to the main menu (which ends your proxy session, if you're in one). If you're in a game or spectator team, send you to the lobby (but does not end your proxy session if you're in one). Does nothing if you're in a non-Episode 3 game and no quest is in progress. * `$patch `: Run a patch on your client. `` must exactly match the name of a patch on the server. -* Character data commands (game server only) +* Character data commands (non-proxy only) * `$savechar `: Save your current character data on the server in the specified slot. See the [server-side saves section](#server-side-saves) for more details. * `$loadchar `: Save your current character data on the server in the specified slot. See the [server-side saves section](#server-side-saves) for more details. * `$bbchar `: Save your current character data on the server in a different account's BB character slots. See the [server-side saves section](#server-side-saves) for more details. + * `$checkchar `: Tells you basic information about a server-side character previously saved using `$savechar`. + * `$deletechar `: Deletes a server-side character previously saved using `$savechar`. * `$edit `: Modify your character data. See the [using $edit](#using-edit) section for details. -* Blue Burst player commands (game server only) +* Blue Burst player commands (non-proxy only) * `$bank [number]`: Switch your current bank, so you can access your other character's banks (if `number` is 1-4) or your shared account bank (if `number` is 0). If `number` is not given, switch back to your current character's bank. * `$save`: Save your character, system, and Guild Card data immediately. (By default, your character is saved every 60 seconds while online, and your account and Guild Card data are saved whenever they change.) -* Game state commands (game server only) +* Game state commands (non-proxy only) * `$maxlevel `: 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 `: Set the minimum level for players to join the current game. * `$password `: Set the game's join password. To unlock the game, run `$password` with nothing after it. * `$dropmode [mode]`: Change the way item drops behave in the current game. `mode` can be `none`, `client`, `shared`, `private`, or `duplicate`. If `mode` is not given, tells you the current drop mode without changing it. See the [item tables and drop modes section](#item-tables-and-drop-modes) for more information. * `$persist`: Enable or disable persistence for the current game. When persistence is on, the game will not be deleted when the last player leaves. The states of enemies, objects, and switches will be saved, and items left on the floor will not be deleted. (But if you're in the private or duplicate drop mode, items dropped by enemies are deleted - to make sure a certain item won't be deleted, you can pick it up and drop it again.) If the game is empty for too long (15 minutes by default), it is then deleted. -* Episode 3 commands (game server only) +* Episode 3 commands (non-proxy only) * `$spec`: Toggle the allow spectators flag for Episode 3 games. If any players are spectating when this flag is disabled, they are sent back to the lobby. * `$inftime`: Toggle infinite-time mode. Must be used before starting a battle. If infinite-time mode is on, the overall and per-phase time limits will be disabled regardless of the values chosen during battle rules setup. After completing a battle, infinite-time mode is reset to the server's default value (which can be set in Episode3BehaviorFlags in config.json). * `$dicerange [d:L-H] [1:L-H] [a1:L-H] [d1:L-H]`: Set override dice ranges for the next battle. The min and max dice values from the rules setup menu always apply to the ATK dice, but you can specify a different range for the DEF dice with `d:2-4` (for example). The `1:` override applies to the 1-player team in a 2v1 game (so you would set the 2-player team's desired dice range in the rules menu). You can also specify the 1-player team's ATK and DEF ranges separately with the `a1:` and `d1:` overrides. Note that these ranges will only be used if the chosen map or quest does not override them. @@ -611,22 +627,22 @@ Some commands only work on the game server and not on the proxy server. The chat * `$playrec `: 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` (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. + * `$cheat` (non-proxy only): Enable or disable cheat mode for the current game. All other cheat mode commands do nothing if cheat mode is disabled. By default, cheat mode is off in new games but can be enabled; there is an option in config.json that allows you to disable cheat mode entirely, or set it to on by default in new games. Cheat mode is always enabled on the proxy, unless cheat mode is disabled on the entire server. * `$infhp`: Enable or disable infinite HP mode. Applies to only you; does not affect other players. When enabled, one-hit KO attacks will still kill you, but on most versions of the game (not DCv1, GC US 1.2, or GC JP 1.5), the server will automatically revive you if you die. On all versions except GC US 1.2 and GC JP 1.5, infinite HP also automatically cures status ailments. * `$inftp`: Enable or disable infinite TP mode. Applies to only you; does not affect other players. * `$warpme ` (or `$warp `): Warp yourself to the given floor. - * `$warpall `: Warp everyone in the game to the given floor. You must be the leader to use this command, unless you're on the proxy server. + * `$warpall `: Warp everyone in the game to the given floor. You must be the leader to use this command, unless you're on the proxy. * `$next`: Warp yourself to the next floor. - * `$item ` (or `$i `): 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 ` (game server only): In an Episode 3 battle, removes one of your set cards from the field. `` is the index of the set card as it appears on your screen - 1 is the card next to your SC's icon, 2 is the card to the right of 1, etc. This does not cause a Hunters-side SC to lose HP, as they normally do when their items are destroyed. - * `$dropmode [mode]` (proxy server): Change the way item drops behave in the current game, if you are not on BB. Unlike the game server version of this command, using this on the proxy server requires cheats to be enabled. This works by intercepting the drop requests sent to and from the leader. (So, if you are the leader and not using server drop mode on the remote server, it affects the entire game; otherwise, it affects only items generated by your actions.) `mode` can be `none` (no drops), `default` (normal drops), or `proxy` (use newserv's drop tables instead of the remote server's). If `mode` is not given, tells you the current drop mode without changing it. + * `$item ` (or `$i `): 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, you must not be using Blue Burst for this command to work. On the game server, this command works for all versions. + * `$unset ` (non-proxy only): In an Episode 3 battle, removes one of your set cards from the field. `` 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 only): Change the way item drops behave in the current game, if you are not on BB. Unlike the game server version of this command, using this on the proxy requires cheats to be enabled. This works by intercepting the drop requests sent to and from the leader. (So, if you are the leader and not using server drop mode on the remote server, it affects the entire game; otherwise, it affects only items generated by your actions.) `mode` can be `none` (no drops), `default` (normal drops), or `proxy` (use newserv's drop tables instead of the remote server's). If `mode` is not given, tells you the current drop mode without changing it. * Aesthetic commands - * `$event `: 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 ` (game server only): Set the current holiday event in all lobbies. + * `$event `: Set the current holiday event in the current lobby. Holiday events are documented in the "Using $event" item in the information menu. If you're on the proxy, this applies to all lobbies and games you join, but only you will see the new event - other players will not. + * `$allevent ` (non-proxy only): Set the current holiday event in all lobbies. * `$song ` (Episode 3 only): Play a specific song in the current lobby. -* Administration commands (game server only) +* Administration commands (non-proxy only) * `$ann `: Send an announcement message. The message is sent as temporary on-screen text to all players in all games and lobbies. On BB, the message appears in the scrolling top bar. * `$ann!`, `$ann?`, `$ann?!`: Same as `$ann`, but with `?`, omits the sender's name, and with `!`, sends the message as a Simple Mail message instead of on-screen text. * `$silence `: Silence a player (remove their ability to chat) or unsilence a player. The identifier may be the player's name or Guild Card number. @@ -690,7 +706,7 @@ The HTTP server has the following endpoints: * `GET /y/data/config`: Returns the server's configuration file. * `GET /y/accounts`: Returns information about all registered accounts. * `GET /y/clients`: Returns information about all connected clients on the game server. -* `GET /y/proxy-clients`: Returns information about all connected clients on the proxy server. +* `GET /y/proxy-clients`: Returns information about all connected clients on the proxy. * `GET /y/lobbies`: Returns information about all lobbies and games. * `GET /y/server`: Returns information about the server. * `GET /y/summary`: Returns a summary of the server's state, connected clients, active games, and proxy sessions. @@ -718,7 +734,7 @@ Upon connecting, you'll get the message `{"ServerType": "newserv"}`. After that, # Non-server features -newserv has many CLI options, which can be used to access functionality other than the game and proxy server. Run `newserv help` to see a full list of the options and how to use each one. +newserv has many CLI options, which can be used to access functionality other than the game server and proxy. Run `newserv help` to see a full list of the options and how to use each one. The data formats that newserv can convert to/from are: diff --git a/TODO.md b/TODO.md index 91da148b..922aea30 100644 --- a/TODO.md +++ b/TODO.md @@ -1,13 +1,18 @@ ## General - Make UI strings localizable (e.g. entries in menus, welcome message, etc.) -- Add an idle connection timeout for proxy sessions - Clean up ItemParameterTable implementation (see comment at the top of the class definition) -- Handle MeetUserExtensions properly in 41 and C4 commands on the proxy (rewrite the embedded 19 command and store a map of received destinations) +- Handle MeetUserExtensions properly in 41 and C4 commands on the proxy (rewrite the embedded 19 command and put some metadata in the persistent config, perhaps) +- Make a server patch version of story flag fixer quest +- Make proxy server handle all login commands, including sending 9C when needed +- Add $switchit command (activates switch flag(s) for nearest object, e.g. laser fence, door, fog collision) +- Add a way to persist flags across connections, at least on v3, because of Meet User + B2 enable quest interactions - maybe update the quest to patch one of the login commands so the server can tell it's enabled +- Handle items in crossplay - use the replacement table ## PSO DC - Investigate if https://crates.io/crates/blaze-ssl-async can be used to implement the HL check server +- v2 challenge data in $savechar/$loadchar doesn't work properly ## Episode 3 @@ -27,3 +32,5 @@ - Figure out why Pouilly Slime EXP doesn't work - Make server-specified rare enemies work with maps loaded by the proxy - Implement serialization for various table types (ItemPMT, ItemPT, etc.) +- Record some BB tests +- Add all necessary Guild Card number rewrites in BB commands on the proxy diff --git a/notes/ar-codes.txt b/notes/ar-codes.txt index 75ff32ee..54ac882f 100644 --- a/notes/ar-codes.txt +++ b/notes/ar-codes.txt @@ -719,3 +719,6 @@ Show extended item info when targeting a dropped item 04005188 38210020 0400518C 7C0803A6 04005190 4E800020 + +All weapons can do 3-hit combos +3OE1 => 041D3248 38000001 diff --git a/notes/ep3-nte-differences.txt b/notes/ep3-nte-differences.txt index 0807ac7c..2a1801e6 100644 --- a/notes/ep3-nte-differences.txt +++ b/notes/ep3-nte-differences.txt @@ -1,4 +1,6 @@ List of differences in Ep3 NTE compared to Final: +- COMs can play more than one defense card per turn +- The battle setup menu allows 1v2 battles - Assist cards - - Dice Fever sets dice to 6, not 5, and there is no Dice Fever + - - Rich + and Charity + also don't exist diff --git a/notes/handler-tables.txt b/notes/handler-tables.txt index 536cc294..12495056 100644 --- a/notes/handler-tables.txt +++ b/notes/handler-tables.txt @@ -504,7 +504,7 @@ Quest opcode dispatch 3SP0 => 8010A404 Quest opcode handlers (format: GET_ARGS EXEC_FUN) - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- 00 8C13C19C-() 8C13C830 8C13F83C-() 8C13FF80 8C1501A4-??? 8C1508E8 8C151764-() 8C151EA8 8C16C6F0-() 8C16CE34 004E12C0-() 004E1A50 00590DD0-() 00595030 80242F44-() 80242304 801ED060-??? 801ECB70 801F2A10-() 801F2520 801F2B94-() 801F26A4 8010D308-() 8010CE18 80109CA8-() 801097B8 00218DF0-??? 002C9010 002190C0-??? 002F76A0 006B101C-() 0061CDB0 nop 01 8C13C19C-() 8C13C834 8C13F83C-() 8C13FF84 8C1501A4-??? 8C1508EC 8C151764-() 8C151EAC 8C16C6F0-() 8C16CE38 004E12C0-() 004E1A60 00590DD0-() 00591560 80242F44-() 802422C0 801ED060-??? 801ECB20 801F2A10-() 801F24D0 801F2B94-() 801F2654 8010D308-() 8010CDC8 80109CA8-() 80109768 00218DF0-??? 00219170 002190C0-??? 00219440 006B101C-() 006B16A0 ret 02 8C13C19C-() 8C13C870 8C13F83C-() 8C13FFC0 8C1501A4-??? 8C150928 8C151764-() 8C151EE8 8C16C6F0-() 8C16CE74 004E12C0-() 004E1AA0 00590DD0-() 005915A0 80242F44-() 802422A8 801ED060-??? 801ECB08 801F2A10-() 801F24B8 801F2B94-() 801F263C 8010D308-() 8010CDB0 80109CA8-() 80109750 00218DF0-??? 002191B0 002190C0-??? 00219480 006B101C-() 006B16E0 sync @@ -519,7 +519,7 @@ Quest opcode handlers (format: GET_ARGS EXEC_FUN) 0B ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ECE40-??? 801ECA70 801F27F0-BW 801F2420 801F2974-BW 801F25A4 8010D0E8-BW 8010CD18 80109A88-BW 801096B8 00218F00-??? 00219270 002191D0-??? 00219540 006B1120-BW 006B174C letw 0C ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ECF80-??? 801ECA58 801F2930-BB 801F2408 801F2AB4-BB 801F258C 8010D228-BB 8010CD00 80109BC8-BB 801096A0 00218E50-??? 00219290 00219120-??? 00219560 006B1058-BB 006B1760 leta 0D ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ECE40-??? 801ECA3C 801F27F0-BW 801F23EC 801F2974-BW 801F2570 8010D0E8-BW 8010CCE4 80109A88-BW 80109684 00218F00-??? 002192B0 002191D0-??? 00219580 006B1120-BW 006B177C leto - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- 10 8C13C1B0-B 8C13C8D4 8C13F850-B 8C140024 8C1501B8-??? 8C15098C 8C151778-B 8C151F4C 8C16C704-B 8C16CED8 004E12D0-B 004E1B50 00590DE0-B 00591650 80242EF8-B 8024220C 801ECFD8-??? 801ECA28 801F2988-B 801F23D8 801F2B0C-B 801F255C 8010D280-B 8010CCD0 80109C20-B 80109670 00218E20-??? 002192D0 002190F0-??? 002195A0 006B1040-B 006B179C set 11 8C13C1B0-B 8C13C8E8 8C13F850-B 8C140038 8C1501B8-??? 8C1509A0 8C151778-B 8C151F60 8C16C704-B 8C16CEEC 004E12D0-B 004E1B70 00590DE0-B 00591670 80242EF8-B 802421F8 801ECFD8-??? 801ECA14 801F2988-B 801F23C4 801F2B0C-B 801F2548 8010D280-B 8010CCBC 80109C20-B 8010965C 00218E20-??? 002192F0 002190F0-??? 002195C0 006B1040-B 006B17B0 clear 12 8C13C1B0-B 8C13C8FC 8C13F850-B 8C14004C 8C1501B8-??? 8C1509B4 8C151778-B 8C151F74 8C16C704-B 8C16CF00 004E12D0-B 004E1B90 00590DE0-B 00591690 80242EF8-B 802421DC 801ECFD8-??? 801EC9F8 801F2988-B 801F23A8 801F2B0C-B 801F252C 8010D280-B 8010CCA0 80109C20-B 80109640 00218E20-??? 00219310 002190F0-??? 002195E0 006B1040-B 006B17C4 rev @@ -536,7 +536,7 @@ Quest opcode handlers (format: GET_ARGS EXEC_FUN) 1D 8C13C21C-BL 8C13CA5C 8C13F8BC-BL 8C1401AC 8C150224-??? 8C150B14 8C1517E4-BL 8C1520D4 8C16C770-BL 8C16D060 004E1350-BL 004E1D80 00590E60-BL 00591880 80242E1C-BL 80242000 801ECF28-??? 801EC81C 801F28D8-BL 801F21CC 801F2A5C-BL 801F2350 8010D1D0-BL 8010CAC4 80109B70-BL 80109464 00218E90-??? 002194B0 00219160-??? 00219780 006B107C-BL 006B1908 muli 1E 8C13C1DC-BB 8C13CA74 8C13F87C-BB 8C1401C4 8C1501E4-??? 8C150B2C 8C1517A4-BB 8C1520EC 8C16C730-BB 8C16D078 004E1300-BB 004E1DA0 00590E10-BB 005918A0 80242EA0-BB 80241FE0 801ECF80-??? 801EC7FC 801F2930-BB 801F21AC 801F2AB4-BB 801F2330 8010D228-BB 8010CAA4 80109BC8-BB 80109444 00218E50-??? 002194D0 00219120-??? 002197A0 006B1058-BB 006B1920 div 1F 8C13C21C-BL 8C13CAA0 8C13F8BC-BL 8C1401F0 8C150224-??? 8C150B58 8C1517E4-BL 8C152118 8C16C770-BL 8C16D0A4 004E1350-BL 004E1DD0 00590E60-BL 005918D0 80242E1C-BL 80241FC8 801ECF28-??? 801EC7E4 801F28D8-BL 801F2194 801F2A5C-BL 801F2318 8010D1D0-BL 8010CA8C 80109B70-BL 8010942C 00218E90-??? 002194F0 00219160-??? 002197C0 006B107C-BL 006B1944 divi - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- 20 8C13C1DC-BB 8C13CAC4 8C13F87C-BB 8C140214 8C1501E4-??? 8C150B7C 8C1517A4-BB 8C15213C 8C16C730-BB 8C16D0C8 004E1300-BB 004E1DF0 00590E10-BB 005918F0 80242EA0-BB 80241FA8 801ECF80-??? 801EC7C4 801F2930-BB 801F2174 801F2AB4-BB 801F22F8 8010D228-BB 8010CA6C 80109BC8-BB 8010940C 00218E50-??? 00219510 00219120-??? 002197E0 006B1058-BB 006B1964 and 21 8C13C21C-BL 8C13CAE4 8C13F8BC-BL 8C140234 8C150224-??? 8C150B9C 8C1517E4-BL 8C15215C 8C16C770-BL 8C16D0E8 004E1350-BL 004E1E20 00590E60-BL 00591920 80242E1C-BL 80241F90 801ECF28-??? 801EC7AC 801F28D8-BL 801F215C 801F2A5C-BL 801F22E0 8010D1D0-BL 8010CA54 80109B70-BL 801093F4 00218E90-??? 00219530 00219160-??? 00219800 006B107C-BL 006B197C andi 22 8C13C1DC-BB 8C13CAFC 8C13F87C-BB 8C14024C 8C1501E4-??? 8C150BB4 8C1517A4-BB 8C152174 8C16C730-BB 8C16D100 004E1300-BB 004E1E40 00590E10-BB 00591940 80242EA0-BB 80241F70 801ECF80-??? 801EC78C 801F2930-BB 801F213C 801F2AB4-BB 801F22C0 8010D228-BB 8010CA34 80109BC8-BB 801093D4 00218E50-??? 00219550 00219120-??? 00219820 006B1058-BB 006B1990 or @@ -553,7 +553,7 @@ Quest opcode handlers (format: GET_ARGS EXEC_FUN) 2D 8C13C48C-BLW 8C13CC5C 8C13FBDC-BLW 8C1403C8 8C150544-??? 8C150D80 8C151B04-BLW 8C152340 8C16CA90-BLW 8C16D2CC 004E15F0-BLW 004E2090 00591100-BLW 00591B90 802428C8-BLW 80241D94 801ECD78-??? 801EC5B8 801F2728-BLW 801F1F68 801F28AC-BLW 801F20EC 8010D020-BLW 8010C860 801099C0-BLW 80109200 00218F90-??? 00219730 00219260-??? 00219A00 006B1174-BLW 006B1B50 jmpi_eq 2E 8C13C420-BBW 8C13CC88 8C13FB70-BBW 8C1403F4 8C1504D8-??? 8C150DAC 8C151A98-BBW 8C15236C 8C16CA24-BBW 8C16D2F8 004E15A0-BBW 004E20D0 005910B0-BBW 00591BD0 80242980-BBW 80241D64 801ECDDC-??? 801EC588 801F278C-BBW 801F1F38 801F2910-BBW 801F20BC 8010D084-BBW 8010C830 80109A24-BBW 801091D0 00218F40-??? 00219760 00219210-??? 00219A30 006B1144-BBW 006B1B78 jmp_ne 2F 8C13C48C-BLW 8C13CCBC 8C13FBDC-BLW 8C140428 8C150544-??? 8C150DE0 8C151B04-BLW 8C1523A0 8C16CA90-BLW 8C16D32C 004E15F0-BLW 004E2110 00591100-BLW 00591C10 802428C8-BLW 80241D3C 801ECD78-??? 801EC560 801F2728-BLW 801F1F10 801F28AC-BLW 801F2094 8010D020-BLW 8010C808 801099C0-BLW 801091A8 00218F90-??? 00219790 00219260-??? 00219A60 006B1174-BLW 006B1BA4 jmpi_ne - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- 30 8C13C420-BBW 8C13CCE8 8C13FB70-BBW 8C140454 8C1504D8-??? 8C150E0C 8C151A98-BBW 8C1523CC 8C16CA24-BBW 8C16D358 004E15A0-BBW 004E2150 005910B0-BBW 00591C50 80242980-BBW 80241D0C 801ECDDC-??? 801EC530 801F278C-BBW 801F1EE0 801F2910-BBW 801F2064 8010D084-BBW 8010C7D8 80109A24-BBW 80109178 00218F40-??? 002197C0 00219210-??? 00219A90 006B1144-BBW 006B1BCC ujmp_gt 31 8C13C48C-BLW 8C13CD1C 8C13FBDC-BLW 8C140488 8C150544-??? 8C150E40 8C151B04-BLW 8C152400 8C16CA90-BLW 8C16D38C 004E15F0-BLW 004E2190 00591100-BLW 00591C90 802428C8-BLW 80241CE4 801ECD78-??? 801EC508 801F2728-BLW 801F1EB8 801F28AC-BLW 801F203C 8010D020-BLW 8010C7B0 801099C0-BLW 80109150 00218F90-??? 002197F0 00219260-??? 00219AC0 006B1174-BLW 006B1BF8 ujmpi_gt 32 8C13C420-BBW 8C13CD48 8C13FB70-BBW 8C1404B4 8C1504D8-??? 8C150E6C 8C151A98-BBW 8C15242C 8C16CA24-BBW 8C16D3B8 004E15A0-BBW 004E21D0 005910B0-BBW 00591CD0 80242980-BBW 80241CB4 801ECDDC-??? 801EC4D8 801F278C-BBW 801F1E88 801F2910-BBW 801F200C 8010D084-BBW 8010C780 80109A24-BBW 80109120 00218F40-??? 00219820 00219210-??? 00219AF0 006B1144-BBW 006B1C20 jmp_gt @@ -570,7 +570,7 @@ Quest opcode handlers (format: GET_ARGS EXEC_FUN) 3D 8C13C48C-BLW 8C13CF5C 8C13FBDC-BLW 8C1406C8 8C150544-??? 8C151080 8C151B04-BLW 8C152640 8C16CA90-BLW 8C16D5CC 004E15F0-BLW 004E2490 00591100-BLW 00591F90 802428C8-BLW 80241AD4 801ECD78-??? 801EC2F8 801F2728-BLW 801F1CA8 801F28AC-BLW 801F1E2C 8010D020-BLW 8010C5A0 801099C0-BLW 80108F40 00218F90-??? 00219A30 00219260-??? 00219D00 006B1174-BLW 006B1DF0 ujmpi_le 3E 8C13C420-BBW 8C13CF88 8C13FB70-BBW 8C1406F4 8C1504D8-??? 8C1510AC 8C151A98-BBW 8C15266C 8C16CA24-BBW 8C16D5F8 004E15A0-BBW 004E24D0 005910B0-BBW 00591FD0 80242980-BBW 80241AA4 801ECDDC-??? 801EC2C8 801F278C-BBW 801F1C78 801F2910-BBW 801F1DFC 8010D084-BBW 8010C570 80109A24-BBW 80108F10 00218F40-??? 00219A60 00219210-??? 00219D30 006B1144-BBW 006B1E18 jmp_le 3F 8C13C48C-BLW 8C13CFBC 8C13FBDC-BLW 8C140728 8C150544-??? 8C1510E0 8C151B04-BLW 8C1526A0 8C16CA90-BLW 8C16D62C 004E15F0-BLW 004E2510 00591100-BLW 00592010 802428C8-BLW 80241A7C 801ECD78-??? 801EC2A0 801F2728-BLW 801F1C50 801F28AC-BLW 801F1DD4 8010D020-BLW 8010C548 801099C0-BLW 80108EE8 00218F90-??? 00219A90 00219260-??? 00219D60 006B1174-BLW 006B1E44 jmpi_le - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- 40 8C13C588-BW* 8C13CFE8 8C13FCD8-BW* 8C140754 8C150640-??? 8C15110C 8C151C00-BW* 8C1526CC 8C16CB8C-BW* 8C16D658 004E16C0-BW* 004E2550 005911D0-BW* 00592050 80242748-BW* 80241A40 801ECC6C-??? 801EC258 801F261C-BW* 801F1C08 801F27A0-BW* 801F1D8C 8010CF14-BW* 8010C500 801098B4-BW* 80108EA0 00219040-??? 00219AC0 00219310-??? 00219D90 006B1274-BW* 006B1E6C switch_jmp 41 8C13C588-BW* 8C13D020 8C13FCD8-BW* 8C14078C 8C150640-??? 8C151144 8C151C00-BW* 8C152704 8C16CB8C-BW* 8C16D690 004E16C0-BW* 004E2590 005911D0-BW* 00592090 80242748-BW* 802419E4 801ECC6C-??? 801EC210 801F261C-BW* 801F1BC0 801F27A0-BW* 801F1D44 8010CF14-BW* 8010C4B8 801098B4-BW* 80108E58 00219040-??? 00219B00 00219310-??? 00219DD0 006B1274-BW* 006B1EB0 switch_call 42 8C13C274-L 8C13D074 8C13F914-L 8C1407E0 8C15027C-??? 8C151198 8C15183C-L 8C152758 8C16C7C8-L 8C16D6E4 004E1390-L 004E1A50 00590EA0-L 00595030 80242DA8-L 802419E0 801ECFD8-??? 801EC1E4 801F2988-B 801F1B94 801F2B0C-B 801F1D18 8010D280-B 8010C48C 80109C20-B 80108E2C 00218E20-??? 00219B50 002190F0-??? 00219E20 006B1040-B 006B1F1C nop_42/stack_push @@ -584,7 +584,7 @@ Quest opcode handlers (format: GET_ARGS EXEC_FUN) 4C ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ECFD8-??? 801E1054 801F2988-B 801E6F04 801F2B0C-B 801E6FC4 8010D280-B 80102B5C 80109C20-B 800FF4D0 00218E20-??? 002223D0 002190F0-??? 002226C0 006B1040-B 006B8B48 arg_pusha 4D ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ECE98-??? 801E1028 801F2848-W 801E6ED8 801F29CC-W 801E6F98 8010D140-W 80102B30 80109AE0-W 800FF4A4 00219100-??? 00222400 002193D0-??? 002226F0 006B10E0-W 006B8B70 arg_pusho 4E ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ECC14-??? 801E1008 801F25C4-S 801E6EB8 801F2748-S 801E6F78 8010CEBC-S 80102B10 8010985C-S 800FF484 002190B0-??? 00222430 00219380-??? 00222660 006B12E4-S 006B8AF4 arg_pushs - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- 50 8C13C684-LS 8C13D078 8C13FDD4-LS 8C1407E4 8C15073C-??? 8C15119C 8C151CFC-LS 8C15275C 8C16CC88-LS 8C16D6E8 004E1810-LS 004E25F0 00591320-LS 005920F0 80242514-LS 802418E0 801ED020-??? 801EC010 801F29D0-... 801F19C0 801F2B54-... 801F1B44 8010D2C8-... 8010C2B8 80109C68-... 80108C58 00218E00-??? 00219C40 002190D0-??? 00219F10 006B1028-... 006B23F8 message 51 8C13C714-BS 8C13D11C 8C13FE64-BS 8C140888 8C1507CC-??? 8C151270 8C151D8C-BS 8C152868 8C16CD18-BS 8C16D7F4 004E18F0-BS 004E26B0 00591400-BS 005921B0 80242404-BS 80241798 801ED020-??? 801EBED8 801F29D0-... 801F1888 801F2B54-... 801F1A0C 8010D2C8-... 8010C180 80109C68-... 80108B20 00218E00-??? 00219D30 002190D0-??? 0021A000 006B1028-... 006B206C list 52 8C13C19C-() 8C13D258 8C13F83C-() 8C1409C4 8C1501A4-??? 8C1513C0 8C151764-() 8C1529B8 8C16C6F0-() 8C16D948 004E12C0-() 004E27E0 00590DD0-() 005922E0 80242F44-() 8024176C 801ED060-??? 801EBEAC 801F2A10-() 801F185C 801F2B94-() 801F19E0 8010D308-() 8010C154 80109CA8-() 80108AF4 00218DF0-??? 00219FB0 002190C0-??? 0021A280 006B101C-() 006B2250 fadein @@ -600,7 +600,7 @@ Quest opcode handlers (format: GET_ARGS EXEC_FUN) 5C 8C13C19C-() 8C13D328 8C13F83C-() 8C140ADC 8C1501A4-??? 8C1515C8 8C151764-() 8C152BF8 8C16C6F0-() 8C16DB88 004E12C0-() 004E29D0 00590DD0-() 005924D0 80242F44-() 80241460 801ED060-??? 801EBBA0 801F2A10-() 801F1550 801F2B94-() 801F16D4 8010D308-() 8010BE48 80109CA8-() 801087E8 00218DF0-??? 0021A260 002190C0-??? 0021A530 006B101C-() 006B24F0 mesend 5D 8C13C1B0-B 8C13DCA0 8C13F850-B 8C1416D8 8C1501B8-??? 8C152418 8C151778-B 8C153AB0 8C16C704-B 8C16EA50 004E12D0-B 004E35F0 00590DE0-B 00593100 80242EF8-B 8023FE3C 801ECFD8-??? 801EA374 801F2988-B 801EFD64 801F2B0C-B 801EFE24 8010D280-B 8010A564 80109C20-B 80106ED8 00218E20-??? 0021B260 002190F0-??? 0021B530 006B1040-B 006B3390 gettime 5E 8C13C19C-() 8C13D3BC 8C13F83C-() 8C140B70 8C1501A4-??? 8C151674 8C151764-() 8C152CD8 8C16C6F0-() 8C16DC68 004E12C0-() 004E2A70 00590DD0-() 00592570 80242F44-() 80241404 801ED060-??? 801EBB58 801F2A10-() 801F1508 801F2B94-() 801F168C 8010D308-() 8010BE00 80109CA8-() 801087A0 00218DF0-??? 0021A310 002190C0-??? 0021A5E0 006B101C-() 006B25B0 winend - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- 60 8C13C2BC-LL 8C13D3E0 8C13F95C-LL 8C140BB0 8C1502C4-??? 8C1516CC 8C151884-LL 8C152D64 8C16C810-LL 8C16DCF4 004E13C0-LL 004E2AC0 00590ED0-LL 005925C0 80242D00-LL 802413B0 801ED020-??? 801EBB20 801F29D0-... 801F14D0 801F2B54-... 801F1654 8010D2C8-... 8010BDC8 80109C68-... 80108768 00218E00-??? 0021A370 002190D0-??? 0021A640 006B1028-... 006B25F8 npc_crt 61 8C13C274-L 8C13D7B0 8C13F914-L 8C1410F4 8C15027C-??? 8C151D8C 8C15183C-L 8C153424 8C16C7C8-L 8C16E3B4 004E1390-L 004E2FB0 00590EA0-L 00592AC0 80242DA8-L 80240A70 801ED020-??? 801EB24C 801F29D0-... 801F0BFC 801F2B54-... 801F0D80 8010D2C8-... 8010B494 80109C68-... 80107E34 00218E00-??? 0021A900 002190D0-??? 0021ABD0 006B1028-... 006B9964 npc_stop 62 8C13C274-L 8C13D7BC 8C13F914-L 8C141100 8C15027C-??? 8C151D98 8C15183C-L 8C153430 8C16C7C8-L 8C16E3C0 004E1390-L 004E2FC0 00590EA0-L 00592AD0 80242DA8-L 80240A4C 801ED020-??? 801EB220 801F29D0-... 801F0BD0 801F2B54-... 801F0D54 8010D2C8-... 8010B468 80109C68-... 80107E08 00218E00-??? 0021A910 002190D0-??? 0021ABE0 006B1028-... 006B2CEC npc_play @@ -615,7 +615,7 @@ Quest opcode handlers (format: GET_ARGS EXEC_FUN) 6C 8C13C19C-() 8C13DAC4 8C13F83C-() 8C1414F4 8C1501A4-??? 8C1521D4 8C151764-() 8C15386C 8C16C6F0-() 8C16E80C 004E12C0-() 004E33B0 00590DD0-() 00592EC0 80242F44-() 80240218 801ED060-??? 801EA778 801F2A10-() 801F0168 801F2B94-() 801F0228 8010D308-() 8010A968 80109CA8-() 801072DC 00218DF0-??? 0021AF80 002190C0-??? 0021B250 006B101C-() 006B31B0 p_enablewarp 6D 8C13C2BC-LL 8C13D97C 8C13F95C-LL 8C1413A4 8C1502C4-??? 8C15203C 8C151884-LL 8C1536D4 8C16C810-LL 8C16E674 004E13C0-LL 004E3240 00590ED0-LL 00592D50 80242D00-LL 80240434 801ECFD8-??? 801EA9E8 801F2988-B 801F0398 801F2B0C-B 801F0458 8010D280-B 8010ABD8 80109C20-B 8010754C 00218E20-??? 0021ADE0 002190F0-??? 0021B0B0 006B1040-B 006B3030 p_move 6E 8C13C274-L 8C13D8C8 8C13F914-L 8C14120C 8C15027C-??? 8C151EA4 8C15183C-L 8C15353C 8C16C7C8-L 8C16E4CC 004E1390-L 004E30E0 00590EA0-L 00592BF0 80242DA8-L 802407FC 801ED020-??? 801EAFA8 801F29D0-... 801F0958 801F2B54-... 801F0ADC 8010D2C8-... 8010B1F0 80109C68-... 80107B90 00218E00-??? 0021AB10 002190D0-??? 0021ADE0 006B1028-... 006B2DA8 p_look - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- 70 8C13C19C-() 8C13DBC4 8C13F83C-() 8C1415FC 8C1501A4-??? 8C1522EC 8C151764-() 8C153984 8C16C6F0-() 8C16E924 004E12C0-() 004E34B0 00590DD0-() 00592FC0 80242F44-() 8024000C 801ED060-??? 801EA544 801F2A10-() 801EFF34 801F2B94-() 801EFFF4 8010D308-() 8010A734 80109CA8-() 801070A8 00218DF0-??? 0021B0C0 002190C0-??? 0021B390 006B101C-() 006B993C p_action_disable 71 8C13C19C-() 8C13DBD0 8C13F83C-() 8C141608 8C1501A4-??? 8C152320 8C151764-() 8C1539B8 8C16C6F0-() 8C16E958 004E12C0-() 004E34D0 00590DD0-() 00592FE0 80242F44-() 8023FFCC 801ED060-??? 801EA504 801F2A10-() 801EFEF4 801F2B94-() 801EFFB4 8010D308-() 8010A6F4 80109CA8-() 80107068 00218DF0-??? 0021B120 002190C0-??? 0021B3F0 006B101C-() 006B32A4 p_action_enable 72 8C13C274-L 8C13D898 8C13F914-L 8C1411DC 8C15027C-??? 8C151E74 8C15183C-L 8C15350C 8C16C7C8-L 8C16E49C 004E1390-L 004E30A0 00590EA0-L 00592BB0 80242DA8-L 80240884 801ED020-??? 801EB040 801F29D0-... 801F09F0 801F2B54-... 801F0B74 8010D2C8-... 8010B288 80109C68-... 80107C28 00218E00-??? 0021AA70 002190D0-??? 0021AD40 006B1028-... 006B2D78 disable_movement1 @@ -632,7 +632,7 @@ Quest opcode handlers (format: GET_ARGS EXEC_FUN) 7D 8C13C2BC-LL 8C13D4F4 8C13F95C-LL 8C140CD8 8C1502C4-??? 8C151970 8C151884-LL 8C153008 8C16C810-LL 8C16DF98 004E13C0-LL 004E2CC0 00590ED0-LL 005927C0 80242D00-LL 80240E8C 801ECFD8-??? 801EB630 801F2988-B 801F0FE0 801F2B0C-B 801F1164 8010D280-B 8010B8A8 80109C20-B 80108248 00218E20-??? 0021A5C0 002190F0-??? 0021A890 006B1040-B 006B2868 npc_crptalk 7E 8C13C2BC-LL 8C13D970 8C13F95C-LL 8C141398 8C1502C4-??? 8C152030 8C151884-LL 8C1536C8 8C16C810-LL 8C16E668 004E13C0-LL 004E3230 00590ED0-LL 00592D40 80242D00-LL 80240510 801ED020-??? 801EAACC 801F29D0-... 801F047C 801F2B54-... 801F053C 8010D2C8-... 8010ACBC 80109C68-... 80107630 00218E00-??? 0021ADC0 002190D0-??? 0021B090 006B1028-... 006B3014 p_look_at 7F 8C13C2BC-LL 8C13D6F4 8C13F95C-LL 8C141038 8C1502C4-??? 8C151CD0 8C151884-LL 8C153368 8C16C810-LL 8C16E2F8 004E13C0-LL 004E2F30 00590ED0-LL 00592A40 80242D00-LL 80240A94 801ECFD8-??? 801EB278 801F2988-B 801F0C28 801F2B0C-B 801F0DAC 8010D280-B 8010B4C0 80109C20-B 80107E60 00218E20-??? 0021A880 002190F0-??? 0021AB50 006B1040-B 006B2C38 npc_crp_id - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- 80 8C13C19C-() 8C13D7C8 8C13F83C-() 8C14110C 8C1501A4-??? 8C151DA4 8C151764-() 8C15343C 8C16C6F0-() 8C16E3CC 004E12C0-() 004E2FD0 00590DD0-() 00592AE0 80242F44-() 80240A28 801ED060-??? 801EB1FC 801F2A10-() 801F0BAC 801F2B94-() 801F0D30 8010D308-() 8010B444 80109CA8-() 80107DE4 00218DF0-??? 0021A920 002190C0-??? 0021ABF0 006B101C-() 006B2CFC cam_quake 81 8C13C19C-() 8C13D868 8C13F83C-() 8C1411AC 8C1501A4-??? 8C151E44 8C151764-() 8C1534DC 8C16C6F0-() 8C16E46C 004E12C0-() 004E3060 00590DD0-() 00592B70 80242F44-() 8024090C 801ED060-??? 801EB0D8 801F2A10-() 801F0A88 801F2B94-() 801F0C0C 8010D308-() 8010B320 80109CA8-() 80107CC0 00218DF0-??? 0021A9F0 002190C0-??? 0021ACC0 006B101C-() 006B2D50 cam_adj 82 8C13C19C-() 8C13D7D4 8C13F83C-() 8C141118 8C1501A4-??? 8C151DB0 8C151764-() 8C153448 8C16C6F0-() 8C16E3D8 004E12C0-() 004E2FE0 00590DD0-() 00592AF0 80242F44-() 80240A08 801ED060-??? 801EB1DC 801F2A10-() 801F0B8C 801F2B94-() 801F0D10 8010D308-() 8010B424 80109CA8-() 80107DC4 00218DF0-??? 0021A950 002190C0-??? 0021AC20 006B101C-() 006B2D08 cam_zmin @@ -649,7 +649,7 @@ Quest opcode handlers (format: GET_ARGS EXEC_FUN) 8D 8C13C1B0-B 8C13DD9C 8C13F850-B 8C1417D4 8C1501B8-??? 8C152514 8C151778-B 8C153BAC 8C16C704-B 8C16EB4C 004E12D0-B 004E36F0 00590DE0-B 00593210 80242EF8-B 8023FC1C 801ECFD8-??? 801EA1A4 801F2988-B 801EFB94 801F2B0C-B 801EFC54 8010D280-B 8010A394 80109C20-B 80106D08 00218E20-??? 0021B330 002190F0-??? 0021B600 006B1040-B 006B34BC at_coords_talk 8E 8C13C1B0-B 8C13DE5C 8C13F850-B 8C141894 8C1501B8-??? 8C1525D4 8C151778-B 8C153C6C 8C16C704-B 8C16EC0C 004E12D0-B 004E37B0 00590DE0-B 005932E0 80242EF8-B 8023FB0C 801ECFD8-??? 801EA0BC 801F2988-B 801EFAAC 801F2B0C-B 801EFB6C 8010D280-B 8010A2AC 80109C20-B 80106C20 00218E20-??? 0021B3C0 002190F0-??? 0021B690 006B1040-B 006B3528 col_npcin 8F 8C13C1B0-B 8C13DF1C 8C13F850-B 8C141954 8C1501B8-??? 8C152694 8C151778-B 8C153D2C 8C16C704-B 8C16ECCC 004E12D0-B 004E3870 00590DE0-B 005933B0 80242EF8-B 8023F9D8 801ECFD8-??? 801E9FAC 801F2988-B 801EF99C 801F2B0C-B 801EFA5C 8010D280-B 8010A19C 80109C20-B 80106B10 00218E20-??? 0021B450 002190F0-??? 0021B720 006B1040-B 006B3594 col_npcinr - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- 90 8C13C274-L 8C13E2F0 8C13F914-L 8C141E8C 8C15027C-??? 8C152C5C 8C15183C-L 8C1542F4 8C16C7C8-L 8C16F280 004E1390-L 004E3DD0 00590EA0-L 00593940 80242DA8-L 8023F288 801ED020-??? 801E98AC 801F29D0-... 801EF29C 801F2B54-... 801EF35C 8010D2C8-... 80109A9C 80109C68-... 80106410 00218E00-??? 0021BA50 002190D0-??? 0021BD20 006B1028-... 006B39F0 switch_on 91 8C13C274-L 8C13E2FC 8C13F914-L 8C141E98 8C15027C-??? 8C152C68 8C15183C-L 8C154300 8C16C7C8-L 8C16F28C 004E1390-L 004E3DE0 00590EA0-L 00593950 80242DA8-L 8023F264 801ED020-??? 801E9880 801F29D0-... 801EF270 801F2B54-... 801EF330 8010D2C8-... 80109A70 80109C68-... 801063E4 00218E00-??? 0021BA60 002190D0-??? 0021BD30 006B1028-... 006B3A00 switch_off 92 8C13C274-L 8C13E308 8C13F914-L 8C141EA4 8C15027C-??? 8C152C74 8C15183C-L 8C15430C 8C16C7C8-L 8C16F298 004E1390-L 004E3DF0 00590EA0-L 00593960 80242DA8-L 8023F244 801ED020-??? 801E9854 801F29D0-... 801EF244 801F2B54-... 801EF304 8010D2C8-... 80109A44 80109C68-... 801063B8 00218E00-??? 0021BA70 002190D0-??? 0021BD40 006B1028-... 006B3A10 playbgm_epi @@ -662,7 +662,7 @@ Quest opcode handlers (format: GET_ARGS EXEC_FUN) 99 8C13C19C-() 8C13E0FC 8C13F83C-() 8C141C80 8C1501A4-??? 8C1529D8 8C151764-() 8C154070 8C16C6F0-() 8C16F010 004E12C0-() 004E3BD0 00590DD0-() 00593730 80242F44-() 8023F630 801ED060-??? 801E9C48 801F2A10-() 801EF638 801F2B94-() 801EF6F8 8010D308-() 80109E38 80109CA8-() 801067AC 00218DF0-??? 0021B730 002190C0-??? 0021BA00 006B101C-() 006B3770 hud_show 9A 8C13C19C-() 8C13E130 8C13F83C-() 8C141CB4 8C1501A4-??? 8C152A1C 8C151764-() 8C1540B4 8C16C6F0-() 8C16F054 004E12C0-() 004E3C00 00590DD0-() 00593760 80242F44-() 8023F60C 801ED060-??? 801E9C24 801F2A10-() 801EF614 801F2B94-() 801EF6D4 8010D308-() 80109E14 80109CA8-() 80106788 00218DF0-??? 0021B780 002190C0-??? 0021BA50 006B101C-() 006B3784 cine_enable 9B 8C13C19C-() 8C13E13C 8C13F83C-() 8C141CC0 8C1501A4-??? 8C152A28 8C151764-() 8C1540C0 8C16C6F0-() 8C16F060 004E12C0-() 004E3C10 00590DD0-() 00593770 80242F44-() 8023F5E8 801ED060-??? 801E9C00 801F2A10-() 801EF5F0 801F2B94-() 801EF6B0 8010D308-() 80109DF0 80109CA8-() 80106764 00218DF0-??? 0021B7B0 002190C0-??? 0021BA80 006B101C-() 006B3790 cine_disable - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- A0 8C13C684-LS 8C13E148 8C13FDD4-LS 8C141CCC 8C15073C-??? 8C152A34 8C151CFC-LS 8C1540CC 8C16CC88-LS 8C16F06C 004E1810-LS 004E3C20 00591320-LS 00593780 80242514-LS 8023F544 801ED020-??? 801E9B50 801F29D0-... 801EF540 801F2B54-... 801EF600 8010D2C8-... 80109D40 80109C68-... 801066B4 00218E00-??? 0021B7E0 002190D0-??? 0021BAB0 006B1028-... 006B379C nop_A0_debug A1 8C13C274-L 8C13E338 8C13F914-L 8C141ED4 8C15027C-??? 8C152D48 8C15183C-L 8C1543E0 8C16C7C8-L 8C16F38C 004E1390-L 004E3EB0 00590EA0-L 00593A40 80242DA8-L 8023F058 801ECE98-??? 801E95F8 801F2848-W 801EEFF0 801F29CC-W 801EF0B0 8010D140-W 801097E8 80109AE0-W 8010615C 00219100-??? 0021BC20 002193D0-??? 0021BEF0 006B10E0-W 006B9930 set_qt_failure A2 8C13C274-L 8C13E344 8C13F914-L 8C141EE0 8C15027C-??? 8C152D54 8C15183C-L 8C1543EC 8C16C7C8-L 8C16F398 004E1390-L 004E3EC0 00590EA0-L 00593A50 80242DA8-L 8023F048 801ECE98-??? 801E95D4 801F2848-W 801EEFCC 801F29CC-W 801EF08C 8010D140-W 801097C4 80109AE0-W 80106138 00219100-??? 0021BC30 002193D0-??? 0021BF00 006B10E0-W 006B3ADC set_qt_success @@ -671,7 +671,7 @@ Quest opcode handlers (format: GET_ARGS EXEC_FUN) A5 8C13C274-L 8C13E350 8C13F914-L 8C141EEC 8C15027C-??? 8C152D60 8C15183C-L 8C1543F8 8C16C7C8-L 8C16F3A4 004E1390-L 004E3ED0 00590EA0-L 00593A60 80242DA8-L 8023F038 801ECE98-??? 801E95B0 801F2848-W 801EEFA8 801F29CC-W 801EF068 8010D140-W 801097A0 80109AE0-W 80106114 00219100-??? 0021BC40 002193D0-??? 0021BF10 006B10E0-W 006B3AE8 set_qt_cancel A6 8C13C19C-() 8C13E36C 8C13F83C-() 8C141F08 8C1501A4-??? 8C152D84 8C151764-() 8C15441C 8C16C6F0-() 8C16F3C8 004E12C0-() 004E3F00 00590DD0-() 00593A90 80242F44-() 8023F000 801ED060-??? 801E9518 801F2A10-() 801EEF3C 801F2B94-() 801EEFFC 8010D308-() 80109708 80109CA8-() 8010607C 00218DF0-??? 0021BC70 002190C0-??? 0021BF40 006B101C-() 006B3B00 clr_qt_cancel A8 8C13C2BC-LL 8C13D8D4 8C13F95C-LL 8C141218 8C1502C4-??? 8C151EB0 8C151884-LL 8C153548 8C16C810-LL 8C16E4D8 004E13C0-LL 004E30F0 00590ED0-LL 00592C00 80242D00-LL 802406B8 801ECFD8-??? 801EAEB8 801F2988-B 801F0868 801F2B0C-B 801F09EC 8010D280-B 8010B100 80109C20-B 80107AA0 00218E20-??? 0021AB20 002190F0-??? 0021ADF0 006B1040-B 006B2DB8 pl_walk - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- B0 8C13C2BC-LL 8C13E370 8C13F95C-LL 8C141F0C 8C1502C4-??? 8C152D90 8C151884-LL 8C154428 8C16C810-LL 8C16F3D4 004E13C0-LL 004E3F10 00590ED0-LL 00593AA0 80242D00-LL 8023EFC4 801ED020-??? 801E94D0 801F29D0-... 801EEEF4 801F2B54-... 801EEFB4 8010D2C8-... 801096C0 80109C68-... 80106034 00218E00-??? 0021BC80 002190D0-??? 0021BF50 006B1028-... 006B3B0C pl_add_meseta B1 8C13C378-W 8C13C888 8C13FAC8-W 8C13FFD8 8C150430-??? 8C150940 8C1519F0-W 8C151F00 8C16C97C-W 8C16CE8C 004E1530-W 004E1AD0 00591040-W 005915D0 80242A98-W 80242260 801ECE98-??? 801ECAC0 801F2848-W 801F2470 801F29CC-W 801F25F4 8010D140-W 8010CD68 80109AE0-W 80109708 00219100-??? 00219200 002193D0-??? 002194D0 006B10E0-W 006B16FC thread_stg B2 8C13C1B0-B 8C13E398 8C13F850-B 8C141F34 8C1501B8-??? 8C152DE8 8C151778-B 8C154480 8C16C704-B 8C16F42C 004E12D0-B 004E3F50 00590DE0-B 00593AE0 80242EF8-B 8023EF58 801ECFD8-??? 801E9460 801F2988-B 801EEE84 801F2B0C-B 801EEF44 8010D280-B 80109650 80109C20-B 80105FC4 00218E20-??? 0021BD20 002190F0-??? 0021BFF0 006B1040-B 006B3BA4 del_obj_param @@ -685,7 +685,7 @@ Quest opcode handlers (format: GET_ARGS EXEC_FUN) BA ------------ -------- 8C13F914-L 8C14268C 8C15027C-??? 8C15360C 8C15183C-L 8C154CA4 8C16C7C8-L 8C16FCC0 004E1390-L 004E4B40 00590EA0-L 005946D0 80242DA8-L 8023E518 801ECE98-??? 801E8A00 801F2848-W 801EE43C 801F29CC-W 801EE4FC 8010D140-W 80108BF0 80109AE0-W 80105564 00219100-??? 0021C730 002193D0-??? 0021CA00 006B10E0-W 006B9918 set_qt_exit BB ------------ -------- 8C13F83C-() 8C142698 8C1501A4-??? 8C153618 8C151764-() 8C154CB0 8C16C6F0-() 8C16FCCC 004E12C0-() 004E4B50 00590DD0-() 005946E0 80242F44-() 8023E504 801ED060-??? 801E89CC 801F2A10-() 801EE418 801F2B94-() 801EE4D8 8010D308-() 80108BBC 80109CA8-() 80105530 00218DF0-??? 0021C740 002190C0-??? 0021CA10 006B101C-() 006B990C clr_qt_exit BC ------------ -------- 8C13FD6C-S 8C1426A4 8C1506D4-??? 8C153624 8C151C94-S 8C154CBC 8C16CC20-S 8C16FCD8 004E1740-S 004E1A50 00591250-S 00595030 80242648-S 8023E500 801ECC14-??? 801E89C8 801F25C4-S 801EE414 801F2748-S 801EE4D4 8010CEBC-S 80108BB8 8010985C-S 8010552C 002190B0-??? 002C9010 00219380-??? 002F76A0 006B12E4-S 0061CDB0 nop_BC - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- C0 ------------ -------- 8C13F95C-LL 8C1412B4 8C1502C4-??? 8C151F4C 8C151884-LL 8C1535E4 8C16C810-LL 8C16E584 004E13C0-LL 004E3170 00590ED0-LL 00592C80 80242D00-LL 80240600 801ECFD8-??? 801EAD78 801F2988-B 801F0728 801F2B0C-B 801F08AC 8010D280-B 8010AFC0 80109C20-B 80107960 00218E20-??? 0021AC10 002190F0-??? 0021AEE0 006B1040-B 006B2EA8 particle C1 ------------ -------- 8C13FDD4-LS 8C141D5C 8C15073C-??? 8C152AC4 8C151CFC-LS 8C15415C 8C16CC88-LS 8C16F108 004E1810-LS 004E3CA0 00591320-LS 00593810 80242514-LS 8023F524 801ED020-??? 801E9B20 801F29D0-... 801EF510 801F2B54-... 801EF5D0 8010D2C8-... 80109D10 80109C68-... 80106684 00218E00-??? 0021B8A0 002190D0-??? 0021BB70 006B1028-... 006B3898 npc_text C2 ------------ -------- 8C13F83C-() 8C141C34 8C1501A4-??? 8C152974 8C151764-() 8C15400C 8C16C6F0-() 8C16EFAC 004E12C0-() 004E3B70 00590DD0-() 005936D0 80242F44-() 8023F6C0 801ED060-??? 801E9CD8 801F2A10-() 801EF6C8 801F2B94-() 801EF788 8010D308-() 80109EC8 80109CA8-() 8010683C 00218DF0-??? 0021B6A0 002190C0-??? 0021B970 006B101C-() 006B3744 npc_chkwarp @@ -702,7 +702,7 @@ Quest opcode handlers (format: GET_ARGS EXEC_FUN) CD ------------ -------- 8C13F95C-LL 8C14131C 8C1502C4-??? 8C151FB4 8C151884-LL 8C15364C 8C16C810-LL 8C16E5EC 004E13C0-LL 004E31C0 00590ED0-LL 00592CD0 80242D00-LL 80240538 801ECFD8-??? 801EABDC 801F2988-B 801F058C 801F2B0C-B 801F0710 8010D280-B 8010AE24 80109C20-B 801077C4 00218E20-??? 0021ACD0 002190F0-??? 0021AFA0 006B1040-B 006B2F38 particle_id CE ------------ -------- 8C13F95C-LL 8C140E2C 8C1502C4-??? 8C151AC4 8C151884-LL 8C15315C 8C16C810-LL 8C16E0EC 004E13C0-LL 004E2DB0 00590ED0-LL 005928C0 80242D00-LL 80240CD4 801ECFD8-??? 801EB498 801F2988-B 801F0E48 801F2B0C-B 801F0FCC 8010D280-B 8010B6E0 80109C20-B 80108080 00218E20-??? 0021A6D0 002190F0-??? 0021A9A0 006B1040-B 006B29F4 npc_crptalk_id CF ------------ -------- 8C13F83C-() 8C141D68 8C1501A4-??? 8C152B38 8C151764-() 8C1541D0 8C16C6F0-() 8C16F17C 004E12C0-() 004E3CF0 00590DD0-() 00593860 80242F44-() 8023F464 801ED060-??? 801E9AB0 801F2A10-() 801EF4A0 801F2B94-() 801EF560 8010D308-() 80109CA0 80109CA8-() 80106614 00218DF0-??? 0021B900 002190C0-??? 0021BBD0 006B101C-() 006B3918 npc_lang_clean - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- D0 ------------ -------- ------------ -------- 8C1501A4-??? 8C15298C 8C151764-() 8C154024 8C16C6F0-() 8C16EFC4 004E12C0-() 004E3B90 00590DD0-() 005936F0 80242F44-() 8023F680 801ED060-??? 801E9C98 801F2A10-() 801EF688 801F2B94-() 801EF748 8010D308-() 80109E88 80109CA8-() 801067FC 00218DF0-??? 0021B6D0 002190C0-??? 0021B9A0 006B101C-() 006B3754 pl_pkon D1 ------------ -------- ------------ -------- 8C1501E4-??? 8C1533A0 8C1517A4-BB 8C154A38 8C16C730-BB 8C16FA44 004E1300-BB 004E4840 00590E10-BB 005943D0 80242EA0-BB 8023E778 801ECF80-??? 801E8C98 801F2930-BB 801EE6BC 801F2AB4-BB 801EE77C 8010D228-BB 80108E88 80109BC8-BB 801057FC 00218E50-??? 0021C3F0 00219120-??? 0021C6C0 006B1058-BB 006B41EC pl_chk_item2 D2 ------------ -------- ------------ -------- 8C1501A4-??? 8C153628 8C151764-() 8C154CC0 8C16C6F0-() 8C16FCDC 004E12C0-() 004E4B60 00590DD0-() 005946F0 80242F44-() 8023E4DC 801ED060-??? 801E89A4 801F2A10-() 801EE3F0 801F2B94-() 801EE4B0 8010D308-() 80108B94 80109CA8-() 80105508 00218DF0-??? 0021C750 002190C0-??? 0021CA20 006B101C-() 006B4448 enable_mainmenu @@ -719,7 +719,7 @@ Quest opcode handlers (format: GET_ARGS EXEC_FUN) DD ------------ -------- ------------ -------- 8C1501A4-??? 8C152B04 8C151764-() 8C15419C 8C16C6F0-() 8C16F148 004E12C0-() 004E3CD0 00590DD0-() 00593840 80242F44-() 8023F484 801ED060-??? 801E9AD0 801F2A10-() 801EF4C0 801F2B94-() 801EF580 8010D308-() 80109CC0 80109CA8-() 80106634 00218DF0-??? 0021B8E0 002190C0-??? 0021BBB0 006B101C-() 006B38E4 load_midi DE ------------ -------- ------------ -------- 8C1501B8-??? 8C1537D8 8C151778-B 8C154E70 8C16C704-B 8C16FF24 004E12D0-B 004E4D70 00590DE0-B 00594900 80242EF8-B 8023E06C 801ECFD8-??? 801E8524 801F2988-B 801EDFFC 801F2B0C-B 801EE0BC 8010D280-B 80108714 80109C20-B 80105088 00218E20-??? 0021CA30 002190F0-??? 0021CD00 006B1040-B 006B45C8 item_detect_bank DF ------------ -------- ------------ -------- 8C1502C4-??? 8C151700 8C151884-LL 8C152D98 8C16C810-LL 8C16DD28 004E13C0-LL 004E2AF0 00590ED0-LL 005925F0 80242D00-LL 802411BC 801ED020-??? 801EB91C 801F29D0-... 801F12CC 801F2B54-... 801F1450 8010D2C8-... 8010BBC4 80109C68-... 80108564 00218E00-??? 0021A3A0 002190D0-??? 0021A670 006B1028-... 006B2640 npc_param - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- E0 ------------ -------- ------------ -------- 8C1501A4-??? 8C15386C 8C151764-() 8C154F04 8C16C6F0-() 8C16FFB8 004E12C0-() 004E4E50 00590DD0-() 005949E0 80242F44-() 8023E04C 801ED060-??? 801E8504 801F2A10-() 801EDFDC 801F2B94-() 801EE09C 8010D308-() 80108710 80109CA8-() 80105084 00218DF0-??? 0021CAD0 002190C0-??? 0021CDA0 006B101C-() 006B4660 pad_dragon E1 ------------ -------- ------------ -------- 8C15027C-??? 8C152C98 8C15183C-L 8C154330 8C16C7C8-L 8C16F2D0 004E1390-L 004E3E10 00590EA0-L 005939A0 80242DA8-L 8023F1D4 801ED020-??? 801E97B4 801F29D0-... 801EF1AC 801F2B54-... 801EF26C 8010D2C8-... 801099A4 80109C68-... 80106318 00218E00-??? 0021BB00 002190D0-??? 0021BDD0 006B1028-... 006B3A48 clear_mainwarp E2 ------------ -------- ------------ -------- 8C15027C-??? 8C152CA4 8C15183C-L 8C15433C 8C16C7C8-L 8C16F2E8 004E1390-L 004E3E30 00590EA0-L 005939C0 80242DA8-L 8023F0A4 801ECFD8-??? 801E967C 801F2988-B 801EF074 801F2B0C-B 801EF134 8010D280-B 8010986C 80109C20-B 801061E0 00218E20-??? 0021BB40 002190F0-??? 0021BE10 006B1040-B 006B3A60 pcam_param @@ -736,11 +736,11 @@ Quest opcode handlers (format: GET_ARGS EXEC_FUN) ED ------------ -------- ------------ -------- 8C1501A4-??? 8C153944 8C151764-() 8C154FDC 8C16C6F0-() 8C170094 004E12C0-() 004E4F70 00590DD0-() 00594B00 80242F44-() 8023DEA8 801ED060-??? 801E838C 801F2A10-() 801EDE70 801F2B94-() 801EDF30 8010D308-() 80108598 80109CA8-() 80104F0C 00218DF0-??? 0021CC40 002190C0-??? 0021CF10 006B101C-() 006B473C create_bgmctrl EE ------------ -------- ------------ -------- 8C15027C-??? 8C152DB8 8C15183C-L 8C154450 8C16C7C8-L 8C16F3FC 004E1390-L 004E3F30 00590EA0-L 00593AC0 80242DA8-L 8023EF84 801ED020-??? 801E948C 801F29D0-... 801EEEB0 801F2B54-... 801EEF70 8010D2C8-... 8010967C 80109C68-... 80105FF0 00218E00-??? 0021BCD0 002190D0-??? 0021BFA0 006B1028-... 006B3B64 pl_add_meseta2 EF ------------ -------- ------------ -------- 8C1502C4-??? 8C153758 8C151884-LL 8C154DF0 8C16C810-LL 8C16FE0C 004E13C0-LL 004E4C60 00590ED0-LL 005947F0 80242D00-LL 8023E308 801ED020-??? 801E8798 801F29D0-... 801EE270 801F2B54-... 801EE330 8010D2C8-... 80108988 80109C68-... 801052FC 00218E00-??? 0021C880 002190D0-??? 0021CB50 006B1028-... 006B44E0 sync_register2 - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- F0 ------------ -------- ------------ -------- 8C1502C4-??? 8C15378C 8C151884-LL 8C154E24 8C16C810-LL 8C16FE40 004E13C0-LL 004E4CA0 00590ED0-LL 00594830 80242D00-LL 8023E2C0 ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- send_regwork F1 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C7C8-L 8C16FE74 004E1390-L 004E4CE0 00590EA0-L 00594870 80242DA8-L 8023E1B8 801ECFD8-??? 801E8688 801F2988-B 801EE160 801F2B0C-B 801EE220 8010D280-B 80108878 80109C20-B 801051EC 00218E20-??? 0021C8F0 002190F0-??? 0021CBC0 006B1040-B 006B450C leti_fixed_camera F2 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C6F0-() 8C16FF00 004E12C0-() 004E4D40 00590DD0-() 005948D0 80242F44-() 8023E198 801ED060-??? 801E8650 801F2A10-() 801EE128 801F2B94-() 801EE1E8 8010D308-() 80108840 80109CA8-() 801051B4 00218DF0-??? 0021C990 002190C0-??? 0021CC60 006B101C-() 006B4574 default_camera_pos1 - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- F800 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C6F0-() 8C1700C4 004E12C0-() 004E4F90 00590DD0-() 00594B20 80242F44-() 8023DE60 ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- debug_F800 F801 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16CD18-BS 8C171E3C 004E18F0-BS 004E1A50 00591400-BS 00596AE0 80242404-BS 8023B008 801ED020-??? 801E4EE0 801F29D0-... 801EAC18 801F2B54-... 801EACD8 8010D2C8-... 801063AC 80109C68-... 80102D20 00218E00-??? 0021F130 002190D0-??? 0021F400 006B1028-... 006B6448 set_chat_callback F808 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C704-B 8C1700F4 004E12D0-B 004E4FB0 00590DE0-B 00594B40 80242EF8-B 8023DE34 801ECFD8-??? 801E8360 801F2988-B 801EDE44 801F2B0C-B 801EDF04 8010D280-B 8010856C 80109C20-B 80104EE0 00218E20-??? 0021CCA0 002190F0-??? 0021CF70 006B1040-B 006B47AC get_difficulty_level_v2 @@ -751,7 +751,7 @@ F80C ------------ -------- ------------ -------- ------------ -------- ----- F80D ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C704-B 8C1701C4 004E12D0-B 004E50C0 00590DE0-B 00594C50 80242EF8-B 8023DCE0 801ECFD8-??? 801E81D0 801F2988-B 801EDCBC 801F2B0C-B 801EDD7C 8010D280-B 801083DC 80109C20-B 80104D50 00218E20-??? 0021CD90 002190F0-??? 0021D060 006B1040-B 006B48C0 map_designate_ex F80E ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C7C8-L 8C17020C 004E1390-L 004E5130 00590EA0-L 00594CC0 80242DA8-L 8023DCB4 801ED020-??? 801E8198 801F29D0-... 801EDC84 801F2B54-... 801EDD44 8010D2C8-... 801083A4 80109C68-... 80104D18 00218E00-??? 0021CDE0 002190D0-??? 0021D0B0 006B1028-... 006B4964 disable_weapon_drop F80F ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C7C8-L 8C170230 004E1390-L 004E5150 00590EA0-L 00594CE0 80242DA8-L 8023DC88 801ED020-??? 801E8160 801F29D0-... 801EDC4C 801F2B54-... 801EDD0C 8010D2C8-... 8010836C 80109C68-... 80104CE0 00218E00-??? 0021CE10 002190D0-??? 0021D0E0 006B1028-... 006B497C enable_weapon_drop - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- F810 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C7C8-L 8C170254 004E1390-L 004E5170 00590EA0-L 00594D00 80242DA8-L 8023DC08 801ED020-??? 801E80EC 801F29D0-... 801EDBD8 801F2B54-... 801EDC98 8010D2C8-... 801082F8 80109C68-... 80104C6C 00218E00-??? 0021CE40 002190D0-??? 0021D110 006B1028-... 006B4994 ba_initial_floor F811 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C6F0-() 8C17029C 004E12C0-() 004E5250 00590DD0-() 00594DE0 80242F44-() 8023DBD8 801ED060-??? 801E80BC 801F2A10-() 801EDBA8 801F2B94-() 801EDC68 8010D308-() 801082C8 80109CA8-() 80104C3C 00218DF0-??? 0021CF20 002190C0-??? 0021D1F0 006B101C-() 006B98E4 set_ba_rules F812 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C7C8-L 8C1702B0 004E1390-L 004E5270 00590EA0-L 00594E00 80242DA8-L 8023DB9C 801ED020-??? 801E8078 801F29D0-... 801EDB64 801F2B54-... 801EDC24 8010D2C8-... 80108284 80109C68-... 80104BF8 00218E00-??? 0021CF40 002190D0-??? 0021D210 006B1028-... 006B4A70 ba_set_tech_disk_mode @@ -767,7 +767,7 @@ F81B ------------ -------- ------------ -------- ------------ -------- ----- F81C ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16CC20-S 8C170418 004E1740-S 004E1A50 00591250-S 00594F40 80242648-S 8023D92C 801ED020-??? 801E7D8C 801F29D0-... 801ED878 801F2B54-... 801ED938 8010D2C8-... 80107FF0 80109C68-... 80104964 00218E00-??? 0021D080 002190D0-??? 0021D350 006B1028-... 006B4BB4 ba_start F81D ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C7C8-L 8C170C48 004E1390-L 004E5CB0 00590EA0-L 00595870 80242DA8-L 8023C994 801ED020-??? 801E6D48 801F29D0-... 801EC87C 801F2B54-... 801EC93C 8010D2C8-... 801078A4 80109C68-... 80104218 00218E00-??? 0021DA30 002190D0-??? 0021DD00 006B1028-... 006B52EC death_lvl_up F81E ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C7C8-L 8C170C54 004E1390-L 004E5CC0 00590EA0-L 00595880 80242DA8-L 8023C96C 801ED020-??? 801E6D18 801F29D0-... 801EC84C 801F2B54-... 801EC90C 8010D2C8-... 80107874 80109C68-... 801041E8 00218E00-??? 0021DA40 002190D0-??? 0021DD10 006B1028-... 006B52F8 ba_set_meseta_drop_mode - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- F820 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C7C8-L 8C17042C 004E1390-L 004E53B0 00590EA0-L 00594F60 80242DA8-L 8023D8C4 801ED020-??? 801E7D0C 801F29D0-... 801ED7F8 801F2B54-... 801ED8B8 8010D2C8-... 80107FEC 80109C68-... 80104960 00218E00-??? 0021D0C0 002190D0-??? 0021D390 006B1028-... 006B4BC8 cmode_stage F821 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C704-B 8C170450 004E12D0-B 004E53D0 00590DE0-B 00594F80 80242EF8-B 8023D824 801ECFD8-??? 801E7C00 801F2988-B 801ED6FC 801F2B0C-B 801ED7BC 8010D280-B 80107FE8 80109C20-B 8010495C 00218E20-??? 0021D120 002190F0-??? 0021D3F0 006B1040-B 006B4BF4 nop_F821 F822 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C704-B 8C1704C8 004E12D0-B 004E1A50 00590DE0-B 00595030 80242EF8-B 8023D820 801ECFD8-??? 801E7B98 801F2988-B 801ED6A4 801F2B0C-B 801ED764 8010D280-B 80107FE4 80109C20-B 80104958 00218E20-??? 002C9010 002190F0-??? 002F76A0 006B1040-B 0061CDB0 nop_F822 @@ -783,7 +783,7 @@ F82B ------------ -------- ------------ -------- ------------ -------- ----- F82C ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C810-LL 8C1706E0 004E13C0-LL 004E56C0 00590ED0-LL 00595280 80242D00-LL 8023D38C 801ED020-??? 801E772C 801F29D0-... 801ED240 801F2B54-... 801ED300 8010D2C8-... 80107DB4 80109C68-... 80104728 00218E00-??? 0021D480 002190D0-??? 0021D750 006B1028-... 006B4E30 lock_door2 F82D ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C704-B 8C170724 004E12D0-B 004E5710 00590DE0-B 005952D0 80242EF8-B 8023D33C 801ECFD8-??? 801E76DC 801F2988-B 801ED1F0 801F2B0C-B 801ED2B0 8010D280-B 80107D64 80109C20-B 801046D8 00218E20-??? 0021D500 002190F0-??? 0021D7D0 006B1040-B 006B4E7C if_switch_not_pressed F82E ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C704-B 8C170758 004E12D0-B 004E5740 00590DE0-B 00595300 80242EF8-B 8023D2E8 801ECFD8-??? 801E7688 801F2988-B 801ED19C 801F2B0C-B 801ED25C 8010D280-B 80107D10 80109C20-B 80104684 00218E20-??? 0021D530 002190F0-??? 0021D800 006B1040-B 006B4EA4 if_switch_pressed - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- F830 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C704-B 8C17056C 004E12D0-B 004E5530 00590DE0-B 005950F0 80242EF8-B 8023D5F8 801ECFD8-??? 801E7960 801F2988-B 801ED46C 801F2B0C-B 801ED52C 8010D280-B 80107FCC 80109C20-B 80104940 00218E20-??? 0021D2B0 002190F0-??? 0021D580 006B1040-B 006B4CE8 control_dragon F831 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C6F0-() 8C170584 004E12C0-() 004E5550 00590DD0-() 00595110 80242F44-() 8023D5D8 801ED060-??? 801E7940 801F2A10-() 801ED44C 801F2B94-() 801ED50C 8010D308-() 80107FC8 80109CA8-() 8010493C 00218DF0-??? 0021D2E0 002190C0-??? 0021D5B0 006B101C-() 006B4D00 release_dragon F838 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C704-B 8C17078C 004E12D0-B 004E5780 00590DE0-B 00595340 80242EF8-B 8023D2A0 801ECFD8-??? 801E7638 801F2988-B 801ED14C 801F2B0C-B 801ED20C 8010D280-B 80107CC0 80109C20-B 80104634 00218E20-??? 0021D560 002190F0-??? 0021D830 006B1040-B 006B4ED4 shrink @@ -793,7 +793,7 @@ F83B ------------ -------- ------------ -------- ------------ -------- ----- F83C ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C704-B 8C170990 004E12D0-B 004E5980 00590DE0-B 00595540 80242EF8-B 8023CF34 801ECFD8-??? 801E7338 801F2988-B 801ECE6C 801F2B0C-B 801ECF2C 8010D280-B 80107A14 80109C20-B 80104388 00218E20-??? 0021D750 002190F0-??? 0021DA20 006B1040-B 006B50D8 display_clock2 F83D ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C7C8-L 8C170C70 004E1390-L 004E5CE0 00590EA0-L 005958A0 80242DA8-L 8023C964 801ED020-??? 801E6CE4 801F29D0-... 801EC820 801F2B54-... 801EC8E0 8010D2C8-... 80107840 80109C68-... 801041B4 00218E00-??? 0021DA60 002190D0-??? 0021DD30 006B1028-... 006B5314 set_area_total F83E ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C7C8-L 8C170C7C 004E1390-L 004E5CF0 00590EA0-L 005958B0 80242DA8-L 8023C95C 801ED020-??? 801E6CB0 801F29D0-... 801EC7F4 801F2B54-... 801EC8B4 8010D2C8-... 8010780C 80109C68-... 80104180 00218E00-??? 0021DA70 002190D0-??? 0021DD40 006B1028-... 006B5320 delete_area_title - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- F840 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C6F0-() 8C17094C 004E12C0-() 004E5940 00590DD0-() 00595500 80242F44-() 8023D088 801ED060-??? 801E73C8 801F2A10-() 801ECEFC 801F2B94-() 801ECFBC 8010D308-() 80107A50 80109CA8-() 801043C4 00218DF0-??? 0021D710 002190C0-??? 0021D9E0 006B101C-() 006B98B8 load_npc_data F841 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C97C-W 8C170958 004E1530-W 004E5950 00591040-W 00595510 80242A98-W 8023CF8C 801ECE98-??? 801E7390 801F2848-W 801ECEC4 801F29CC-W 801ECF84 8010D140-W 80107A18 80109AE0-W 8010438C 00219100-??? 0021D720 002193D0-??? 0021D9F0 006B10E0-W 006B5014 get_npc_data F848 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C704-B 8C1709A8 004E12D0-B 004E59A0 00590DE0-B 00595560 80242EF8-B 8023CEB4 801ECFD8-??? 801E72B4 801F2988-B 801ECDE8 801F2B0C-B 801ECEA8 8010D280-B 80107A10 80109C20-B 80104384 00218E20-??? 0021D7C0 002190F0-??? 0021DA90 006B1040-B 006B50EC give_damage_score @@ -804,7 +804,7 @@ F84C ------------ -------- ------------ -------- ------------ -------- ----- F84D ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C704-B 8C170AC0 004E12D0-B 004E5AE0 00590DE0-B 005956A0 80242EF8-B 8023CC34 801ECFD8-??? 801E7020 801F2988-B 801ECB54 801F2B0C-B 801ECC14 8010D280-B 801079FC 80109C20-B 80104370 00218E20-??? 0021D8B0 002190F0-??? 0021DB80 006B1040-B 006B51C8 death_score F84E ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C704-B 8C170AF8 004E12D0-B 004E5B20 00590DE0-B 005956E0 80242EF8-B 8023CBB4 801ECFD8-??? 801E6F9C 801F2988-B 801ECAD0 801F2B0C-B 801ECB90 8010D280-B 801079F8 80109C20-B 8010436C 00218E20-??? 0021D8E0 002190F0-??? 0021DBB0 006B1040-B 006B51F4 enemy_kill_score F84F ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C704-B 8C170B30 004E12D0-B 004E5B60 00590DE0-B 00595720 80242EF8-B 8023CB34 801ECFD8-??? 801E6F18 801F2988-B 801ECA4C 801F2B0C-B 801ECB0C 8010D280-B 801079F4 80109C20-B 80104368 00218E20-??? 0021D910 002190F0-??? 0021DBE0 006B1040-B 006B5220 enemy_death_score - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- F850 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C704-B 8C170B68 004E12D0-B 004E5BA0 00590DE0-B 00595760 80242EF8-B 8023CAB4 801ECFD8-??? 801E6E94 801F2988-B 801EC9C8 801F2B0C-B 801ECA88 8010D280-B 801079F0 80109C20-B 80104364 00218E20-??? 0021D940 002190F0-??? 0021DC10 006B1040-B 006B524C meseta_score F851 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C704-B 8C170BA0 004E12D0-B 004E5BE0 00590DE0-B 005957A0 80242EF8-B 8023CA68 801ECFD8-??? 801E6E48 801F2988-B 801EC97C 801F2B0C-B 801ECA3C 8010D280-B 801079A4 80109C20-B 80104318 00218E20-??? 0021D970 002190F0-??? 0021DC40 006B1040-B 006B9888 ba_set_trap_count F852 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C7C8-L 8C170BD8 004E1390-L 004E5C20 00590EA0-L 005957E0 80242DA8-L 8023CA50 801ED020-??? 801E6E28 801F29D0-... 801EC95C 801F2B54-... 801ECA1C 8010D2C8-... 80107984 80109C68-... 801042F8 00218E00-??? 0021D9A0 002190D0-??? 0021DC70 006B1028-... 006B5278 ba_set_target @@ -821,7 +821,7 @@ F85C ------------ -------- ------------ -------- ------------ -------- ----- F85D ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C7C8-L 8C170D80 004E1390-L 004E5E00 00590EA0-L 005959C0 80242DA8-L 8023C7D4 801ED020-??? 801E6A78 801F29D0-... 801EC5D4 801F2B54-... 801EC694 8010D2C8-... 8010761C 80109C68-... 80103F90 00218E00-??? 0021DBC0 002190D0-??? 0021DE90 006B1028-... 006B53F8 set_allow_item_flags F85E ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C7C8-L 8C170D8C 004E1390-L 004E5E10 00590EA0-L 005959D0 80242DA8-L 8023C7B0 801ED020-??? 801E6A48 801F29D0-... 801EC5A4 801F2B54-... 801EC664 8010D2C8-... 801075EC 80109C68-... 80103F60 00218E00-??? 0021DBD0 002190D0-??? 0021DEA0 006B1028-... 006B5408 ba_enable_sonar F85F ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C7C8-L 8C170DAC 004E1390-L 004E5E30 00590EA0-L 005959F0 80242DA8-L 8023C7A0 801ED020-??? 801E6A30 801F29D0-... 801EC58C 801F2B54-... 801EC64C 8010D2C8-... 801075D4 80109C68-... 80103F48 00218E00-??? 0021DBF0 002190D0-??? 0021DEC0 006B1028-... 006B5424 ba_use_sonar - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- F860 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C6F0-() 8C170DB8 004E12C0-() 004E5E40 00590DD0-() 00595A00 80242F44-() 8023C778 801ED060-??? 801E6A08 801F2A10-() 801EC564 801F2B94-() 801EC624 8010D308-() 801075D0 80109CA8-() 80103F44 00218DF0-??? 0021DC00 002190C0-??? 0021DED0 006B101C-() 006B5430 clear_score_announce F861 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C7C8-L 8C170DD8 004E1390-L 004E5E60 00590EA0-L 00595A20 80242DA8-L 8023C744 801ED020-??? 801E69C8 801F29D0-... 801EC524 801F2B54-... 801EC5E4 8010D2C8-... 801075CC 80109C68-... 80103F40 00218E00-??? 0021DC20 002190D0-??? 0021DEF0 006B1028-... 006B5464 set_score_announce F862 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C878-LLS 8C170E00 004E1400-LLS 004E5E90 00590F10-LLS 00595A50 80242B98-LLS 8023C6A4 801ED020-??? 801E6924 801F29D0-... 801EC480 801F2B54-... 801EC540 8010D2C8-... 80107528 80109C68-... 80103E9C 00218E00-??? 0021DC50 002190D0-??? 0021DF20 006B1028-... 006B54F4 give_s_rank_weapon @@ -838,7 +838,7 @@ F86C ------------ -------- ------------ -------- ------------ -------- ----- F86D ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C6F0-() 8C1712C4 004E12C0-() 004E6410 00590DD0-() 00595FD0 80242F44-() 8023BFFC 801ED060-??? 801E6274 801F2A10-() 801EBDD0 801F2B94-() 801EBE90 8010D308-() 8010727C 80109CA8-() 80103BF0 00218DF0-??? 0021E4D0 002190C0-??? 0021E7A0 006B101C-() 006B5988 ba_set_trapself F86E ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C6F0-() 8C1712D0 004E12C0-() 004E6420 00590DD0-() 00595FE0 80242F44-() 8023BFF0 801ED060-??? 801E6250 801F2A10-() 801EBDAC 801F2B94-() 801EBE6C 8010D308-() 80107258 80109CA8-() 80103BCC 00218DF0-??? 0021E4E0 002190C0-??? 0021E7B0 006B101C-() 006B5994 ba_clear_trapself F86F ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C7C8-L 8C170BEC 004E1390-L 004E5C30 00590EA0-L 005957F0 80242DA8-L 8023CA34 801ED020-??? 801E6E04 801F29D0-... 801EC938 801F2B54-... 801EC9F8 8010D2C8-... 80107960 80109C68-... 801042D4 00218E00-??? 0021D9B0 002190D0-??? 0021DC80 006B1028-... 006B528C ba_set_lives - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- F870 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C7C8-L 8C170C00 004E1390-L 004E5C50 00590EA0-L 00595810 80242DA8-L 8023CA0C 801ED020-??? 801E6DD8 801F29D0-... 801EC90C 801F2B54-... 801EC9CC 8010D2C8-... 80107934 80109C68-... 801042A8 00218E00-??? 0021D9D0 002190D0-??? 0021DCA0 006B1028-... 006B52A0 ba_set_max_tech_level F871 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C7C8-L 8C170C18 004E1390-L 004E5C70 00590EA0-L 00595830 80242DA8-L 8023C9C8 801ED020-??? 801E6D90 801F29D0-... 801EC8C4 801F2B54-... 801EC984 8010D2C8-... 801078EC 80109C68-... 80104260 00218E00-??? 0021D9F0 002190D0-??? 0021DCC0 006B1028-... 006B52BC ba_set_char_level F872 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C7C8-L 8C170C30 004E1390-L 004E5C90 00590EA0-L 00595850 80242DA8-L 8023C9A4 801ED020-??? 801E6D60 801F29D0-... 801EC894 801F2B54-... 801EC954 8010D2C8-... 801078BC 80109C68-... 80104230 00218E00-??? 0021DA10 002190D0-??? 0021DCE0 006B1028-... 006B52D8 ba_set_time_limit @@ -855,7 +855,7 @@ F87C ------------ -------- ------------ -------- ------------ -------- ----- F87D ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C704-B 8C1716CC 004E12D0-B 004E6830 00590DE0-B 00596410 80242EF8-B 8023BAB0 801ECFD8-??? 801E5CD0 801F2988-B 801EB854 801F2B0C-B 801EB914 8010D280-B 80106DFC 80109C20-B 80103770 00218E20-??? 0021E910 002190F0-??? 0021EBE0 006B1040-B 006B5D88 kill_player F87E ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C704-B 8C1716FC 004E12D0-B 004E6860 00590DE0-B 00596440 80242EF8-B 8023BA5C 801ECFD8-??? 801E5C7C 801F2988-B 801EB800 801F2B0C-B 801EB8C0 8010D280-B 80106DA8 80109C20-B 8010371C 00218E20-??? 0021E970 002190F0-??? 0021EC40 006B1040-B 006B5DA8 get_serial_number F87F ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C730-BB 8C171740 004E1300-BB 004E68A0 00590E10-BB 00596480 80242EA0-BB 8023BA20 801ECF80-??? 801E5C18 801F2930-BB 801EB79C 801F2AB4-BB 801EB85C 8010D228-BB 80106D44 80109BC8-BB 801036B8 00218E50-??? 0021E9F0 00219120-??? 0021ECC0 006B1058-BB 006B5DD0 get_eventflag - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- F880 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C704-B 8C171780 004E12D0-B 004E68E0 00590DE0-B 005964C0 80242EF8-B 8023B9A0 801ECFD8-??? 801E5B84 801F2988-B 801EB708 801F2B0C-B 801EB7C8 8010D280-B 80106CB0 80109C20-B 80103624 00218E20-??? 0021EA30 002190F0-??? 0021ED00 006B1040-B 006B5E04 set_trap_damage F881 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C704-B 8C1717BC 004E12D0-B 004E6920 00590DE0-B 00596500 80242EF8-B 8023B914 801ECFD8-??? 801E5AF8 801F2988-B 801EB67C 801F2B0C-B 801EB73C 8010D280-B 80106C24 80109C20-B 80103598 00218E20-??? 0021EA60 002190F0-??? 0021ED30 006B1040-B 006B5E30 get_pl_name F882 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C704-B 8C17181C 004E12D0-B 004E6980 00590DE0-B 00596560 80242EF8-B 8023B890 801ECFD8-??? 801E5A74 801F2988-B 801EB5F8 801F2B0C-B 801EB6B8 8010D280-B 80106BA0 80109C20-B 80103514 00218E20-??? 0021EAD0 002190F0-??? 0021EDA0 006B1040-B 006B5E84 get_pl_job @@ -872,7 +872,7 @@ F88C ------------ -------- ------------ -------- ------------ -------- ----- F88D ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C704-B 8C171CD4 004E12D0-B 004E6DC0 00590DE0-B 005969A0 80242EF8-B 8023B3C8 801ECFD8-??? 801E52A8 801F2988-B 801EAFE0 801F2B0C-B 801EB0A0 8010D280-B 801064B4 80109C20-B 80102E28 00218E20-??? 0021EF90 002190F0-??? 0021F260 006B1058-BB 006B6370 chl_set_timerecord F88E ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C704-B 8C171CEC 004E12D0-B 004E6DE0 00590DE0-B 005969C0 80242EF8-B 8023B364 801ECFD8-??? 801E5250 801F2988-B 801EAF88 801F2B0C-B 801EB048 8010D280-B 801064B0 80109C20-B 80102E24 00218E20-??? 0021F000 002190F0-??? 0021F2D0 006B1040-B 006B6390 chl_get_timerecord F88F ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C704-B 8C171D14 004E12D0-B 004E6E00 00590DE0-B 005969E0 80242EF8-B 8023B0FC 801ECFD8-??? 801E4FE0 801F2988-B 801EAD18 801F2B0C-B 801EADD8 8010D280-B 801064AC 80109C20-B 80102E20 00218E20-??? 0021F040 002190F0-??? 0021F310 006B1040-B 006B63A4 set_cmode_grave_rates - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- F890 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C6F0-() 8C16F2DC 004E12C0-() 004E3E20 00590DD0-() 005939B0 80242F44-() 8023F1B4 801ED060-??? 801E9794 801F2A10-() 801EF18C 801F2B94-() 801EF24C 8010D308-() 80109984 80109CA8-() 801062F8 00218DF0-??? 0021BB30 002190C0-??? 0021BE00 006B101C-() 006B3A58 clear_mainwarp_all F891 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C7C8-L 8C171ED0 004E1390-L 004E6F00 00590EA0-L 00596B80 80242DA8-L 8023AFE8 801ED020-??? 801E4EB4 801F29D0-... 801EABEC 801F2B54-... 801EACAC 8010D2C8-... 80106380 80109C68-... 80102CF4 00218E00-??? 0021F1D0 002190D0-??? 0021F4A0 006B1028-... 006B64C8 load_enemy_data F892 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C97C-W 8C171EDC 004E1530-W 004E6F10 00591040-W 00596B90 80242A98-W 8023AF18 801ECE98-??? 801E4E50 801F2848-W 801EAB88 801F29CC-W 801EAC48 8010D140-W 8010631C 80109AE0-W 80102C90 00219100-??? 0021F1E0 002193D0-??? 0021F4B0 006B10E0-W 006B64D8 get_physical_data @@ -889,7 +889,7 @@ F89C ------------ -------- ------------ -------- ------------ -------- ----- F89D ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C6F0-() 8C1721CC 004E12C0-() 004E71D0 00590DD0-() 00596E50 80242F44-() 8023A9B4 801ED060-??? 801E49CC 801F2A10-() 801EA758 801F2B94-() 801EA818 8010D308-() 80105F78 80109CA8-() 801028EC 00218DF0-??? 0021F520 002190C0-??? 0021F7F0 006B101C-() 006B6924 chl_reverser F89E ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C7C8-L 8C1721D8 004E1390-L 004E71E0 00590EA0-L 00596E60 80242DA8-L 8023A990 801ED020-??? 801E499C 801F29D0-... 801EA728 801F2B54-... 801EA7E8 8010D2C8-... 80105F48 80109C68-... 801028BC 00218E00-??? 0021F590 002190D0-??? 0021F860 006B1028-... 006B692C ba_forbid_scape_dolls F89F ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C704-B 8C1721F8 004E12D0-B 004E7200 00590DE0-B 00596E80 80242EF8-B 8023A948 801ECFD8-??? 801E4954 801F2988-B 801EA6E0 801F2B0C-B 801EA7A0 8010D280-B 80105F00 80109C20-B 80102874 00218E20-??? 0021F5B0 002190F0-??? 0021F880 006B1040-B 006B6948 player_recovery - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- F8A0 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C6F0-() 8C172234 004E12C0-() 004E7240 00590DD0-() 00596EC0 80242F44-() 8023A900 801ED060-??? 801E4918 801F2A10-() 801EA6A4 801F2B94-() 801EA764 8010D308-() 80105EFC 80109CA8-() 80102870 00218DF0-??? 0021F5F0 002190C0-??? 0021F8C0 006B101C-() 006B6974 disable_bosswarp_option F8A1 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C6F0-() 8C172240 004E12C0-() 004E7250 00590DD0-() 00596ED0 80242F44-() 8023A8B8 801ED060-??? 801E48DC 801F2A10-() 801EA668 801F2B94-() 801EA728 8010D308-() 80105EF8 80109CA8-() 8010286C 00218DF0-??? 0021F640 002190C0-??? 0021F910 006B101C-() 006B6980 enable_bosswarp_option F8A2 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C704-B 8C17224C 004E12D0-B 004E7260 00590DE0-B 00596EE0 80242EF8-B 8023A814 801ECFD8-??? 801E4844 801F2988-B 801EA5D0 801F2B0C-B 801EA690 8010D280-B 80105EF4 80109C20-B 80102868 00218E20-??? 0021F680 002190F0-??? 0021F950 006B1040-B 006B698C is_bosswarp_opt_disabled @@ -906,7 +906,7 @@ F8AC ------------ -------- ------------ -------- ------------ -------- ----- F8AD ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C704-B 8C1726B8 004E12D0-B 004E76B0 00590DE0-B 005973F0 80242EF8-B 8023A1FC 801ECFD8-??? 801E4210 801F2988-B 801E9F9C 801F2B0C-B 801EA05C 8010D280-B 80105A6C 80109C20-B 801023E0 00218E20-??? 0021FBC0 002190F0-??? 0021FE90 006B1040-B 006B6D18 get_number_of_players2 F8AE ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C704-B 8C1726D0 004E12D0-B 004E76D0 00590DE0-B 00597410 80242EF8-B 8023A198 801ECFD8-??? 801E41B0 801F2988-B 801E9F3C 801F2B0C-B 801E9FFC 8010D280-B 80105A68 80109C20-B 801023DC 00218E20-??? 0021FBE0 002190F0-??? 0021FEB0 006B1040-B 006B6D2C party_has_name F8AF ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C704-B 8C172724 004E12D0-B 004E7720 00590DE0-B 00597460 80242EF8-B 8023A138 801ECFD8-??? 801E4150 801F2988-B 801E9EDC 801F2B0C-B 801E9F9C 8010D280-B 80105A08 80109C20-B 8010237C 00218E20-??? 0021FC20 002190F0-??? 0021FEF0 006B1040-B 006B6D64 someone_has_spoken - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- F8B0 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C730-BB 8C17277C 004E1300-BB 004E7770 00590E10-BB 005974B0 80242EA0-BB 8023A118 801ED020-??? 801E4128 801F29D0-... 801E9EB4 801F2B54-... 801E9F74 8010D2C8-... 801059E0 80109C68-... 80102354 00218E00-??? 0021FC60 002190D0-??? 0021FF30 006B1028-... 006B6D9C read1 F8B1 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C730-BB 8C172798 004E1300-BB 004E77A0 00590E10-BB 005974E0 80242EA0-BB 8023A0FC 801ED020-??? 801E4104 801F29D0-... 801E9E90 801F2B54-... 801E9F50 8010D2C8-... 801059BC 80109C68-... 80102330 00218E00-??? 0021FC80 002190D0-??? 0021FF50 006B1028-... 006B6DB8 read2 F8B2 ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C730-BB 8C1727B4 004E1300-BB 004E77D0 00590E10-BB 00597510 80242EA0-BB 8023A0E0 801ED020-??? 801E40E0 801F29D0-... 801E9E6C 801F2B54-... 801E9F2C 8010D2C8-... 80105998 80109C68-... 8010230C 00218E00-??? 0021FCA0 002190D0-??? 0021FF70 006B1028-... 006B6DD4 read4 @@ -920,7 +920,7 @@ F8B9 ------------ -------- ------------ -------- ------------ -------- ----- F8BA ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C6F0-() 8C1728FC 004E12C0-() 004E7920 00590DD0-() 00597660 80242F44-() 80239EB4 801ED060-??? 801E3EA4 801F2A10-() 801E9C30 801F2B94-() 801E9CF0 8010D308-() 80105810 80109CA8-() 80102184 00218DF0-??? 0021FE30 002190C0-??? 00220100 006B101C-() 006B6EE0 load_guild_card_file_creation_time_to_flag_buf F8BB ------------ -------- ------------ -------- ------------ -------- ------------ -------- 8C16C704-B 8C172924 004E12D0-B 004E7930 00590DE0-B 00597670 80242EF8-B 80239E5C 801ECFD8-??? 801E3E84 801F2988-B 801E9C10 801F2B0C-B 801E9CD0 8010D280-B 801057F0 80109C20-B 80102164 00218E20-??? 0021F7A0 002190F0-??? 0021FA70 006B1040-B 006B6F00 write_flag_buf_to_event_flags2 F8BC ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ECEE0-??? 801E3E30 801F2890-L 801E9BBC 801F2A14-L 801E9C7C 8010D188-L 8010579C 80109B28-L 80102110 00218ED0-??? 0021FE50 002191A0-??? 00220120 006B10C8-L 006B6F44 set_episode - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- F8C0 ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ED020-??? 801E3DE8 801F29D0-... 801E9B80 801F2B54-... 801E9C40 8010D2C8-... 80105754 80109C68-... 801020C8 00218E00-??? 0021FE80 002190D0-??? 00220150 006B1028-... 0061CDB0 file_dl_req/nop_F8C0 F8C1 ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ECFD8-??? 801E3D60 801F2988-B 801E9B08 801F2B0C-B 801E9BC8 8010D280-B 801056CC 80109C20-B 80102040 00218E20-??? 0021FEB0 002190F0-??? 00220180 006B1040-B 0061CDB0 get_dl_status/nop_F8C1 F8C2 ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ED060-??? 801E3D30 801F2A10-() 801E9AD8 801F2B94-() 801E9B98 8010D308-() 8010569C 80109CA8-() 80102010 00218DF0-??? 002C9010 002190C0-??? 002F76A0 006B101C-() 0061CDB0 prepare_gba_rom_from_download/nop_F8C2 @@ -937,7 +937,7 @@ F8CC ------------ -------- ------------ -------- ------------ -------- ----- F8CD ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ECFD8-??? 801E3964 801F2988-B 801E9714 801F2B0C-B 801E97D4 8010D280-B 80105370 80109C20-B 80101CE4 00218E20-??? 00220230 002190F0-??? 00220500 006B1040-B 006B7064 set_slot_paralyze F8CE ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ECFD8-??? 801E3934 801F2988-B 801E96E4 801F2B0C-B 801E97A4 8010D280-B 80105340 80109C20-B 80101CB4 00218E20-??? 00220260 002190F0-??? 00220530 006B1040-B 006B707C set_slot_shock F8CF ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ECFD8-??? 801E3904 801F2988-B 801E96B4 801F2B0C-B 801E9774 8010D280-B 80105310 80109C20-B 80101C84 00218E20-??? 00220290 002190F0-??? 00220560 006B1040-B 006B7094 set_slot_freeze - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- F8D0 ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ECFD8-??? 801E38D4 801F2988-B 801E9684 801F2B0C-B 801E9744 8010D280-B 801052E0 80109C20-B 80101C54 00218E20-??? 002202C0 002190F0-??? 00220590 006B1040-B 006B70AC set_slot_slow F8D1 ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ECFD8-??? 801E38A4 801F2988-B 801E9654 801F2B0C-B 801E9714 8010D280-B 801052B0 80109C20-B 80101C24 00218E20-??? 002202F0 002190F0-??? 002205C0 006B1040-B 006B70C4 set_slot_confuse F8D2 ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ECFD8-??? 801E3874 801F2988-B 801E9624 801F2B0C-B 801E96E4 8010D280-B 80105280 80109C20-B 80101BF4 00218E20-??? 00220320 002190F0-??? 002205F0 006B1040-B 006B70DC set_slot_shifta @@ -954,7 +954,7 @@ F8DC ------------ -------- ------------ -------- ------------ -------- ----- F8DD ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ECF80-??? 801E3534 801F2930-BB 801E92F4 801F2AB4-BB 801E93B4 8010D228-BB 80104F40 80109BC8-BB 801018B4 00218E50-??? 002206A0 00219120-??? 00220970 006B1058-BB 006B7298 get_pad_cond F8DE ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ECF80-??? 801E3508 801F2930-BB 801E92C8 801F2AB4-BB 801E9388 8010D228-BB 80104F14 80109BC8-BB 80101888 00218E50-??? 00220700 00219120-??? 002209D0 006B1058-BB 006B73B0 get_button_cond F8DF ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ED060-??? 801E3214 801F2A10-() 801E9018 801F2B94-() 801E90D8 8010D308-() 80104C20 80109CA8-() 80101594 00218DF0-??? 002208A0 002190C0-??? 00220B70 006B101C-() 006B76CC freeze_enemies - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- F8E0 ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ED060-??? 801E31E8 801F2A10-() 801E8FEC 801F2B94-() 801E90AC 8010D308-() 80104BF4 80109CA8-() 80101568 00218DF0-??? 002208B0 002190C0-??? 00220B80 006B101C-() 006B76E4 unfreeze_enemies F8E1 ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ED060-??? 801E31C0 801F2A10-() 801E8FC4 801F2B94-() 801E9084 8010D308-() 80104BCC 80109CA8-() 80101540 00218DF0-??? 002208C0 002190C0-??? 00220B90 006B101C-() 006B76FC freeze_everything F8E2 ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ED060-??? 801E3198 801F2A10-() 801E8F9C 801F2B94-() 801E905C 8010D308-() 80104BA4 80109CA8-() 80101518 00218DF0-??? 002208D0 002190C0-??? 00220BA0 006B101C-() 006B7708 unfreeze_everything @@ -971,12 +971,12 @@ F8EC ------------ -------- ------------ -------- ------------ -------- ----- F8ED ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ECF80-??? 801E59C0 801F2930-BB 801EB544 801F2AB4-BB 801EB604 8010D228-BB 80106AEC 80109BC8-BB 80103460 00218E50-??? 0021EBA0 00219120-??? 0021EE70 006B1058-BB 006B5F10 animation_check F8EE ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ED020-??? 801E5968 801F29D0-... 801EB4EC 801F2B54-... 801EB5AC 8010D2C8-... 80106A94 80109C68-... 80103408 00218E00-??? 0021EBE0 002190D0-??? 0021EEB0 006B1028-... 006B5F40 call_image_data F8EF ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ED060-??? 801E5964 801F2A10-() 801EB4E8 801F2B94-() 801EB5A8 8010D308-() 80106A90 80109CA8-() 80103404 00218DF0-??? 002C9010 002190C0-??? 002F76A0 006B101C-() 0061CDB0 nop_F8EF - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- F8F0 ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ED060-??? 801E5944 801F2A10-() 801EB4C8 801F2B94-() 801EB588 8010D308-() 80106A70 80109CA8-() 801033E4 00218DF0-??? 0021EC20 002190C0-??? 0021EEF0 006B101C-() 006B5F7C turn_off_bgm_p2 F8F1 ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ED060-??? 801E5924 801F2A10-() 801EB4A8 801F2B94-() 801EB568 8010D308-() 80106A50 80109CA8-() 801033C4 00218DF0-??? 0021EC50 002190C0-??? 0021EF20 006B101C-() 006B5F84 turn_on_bgm_p2 F8F2 ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ED020-??? 801E3298 801F29D0-... 801E909C 801F2B54-... 801E915C 8010D2C8-... 80104CA4 80109C68-... 80101618 00218E00-??? 002207E0 002190D0-??? 00220AB0 006B1028-... 006B75A8 unknown_F8F2 F8F3 ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ED020-??? 801EAE3C 801F29D0-... 801F07EC 801F2B54-... 801F0970 8010D2C8-... 8010B084 80109C68-... 80107A24 00218E00-??? 0021ABB0 002190D0-??? 0021AE80 006B1028-... 006B2E34 particle2 - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- F901 ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ECF80-??? 801E2A00 801F2930-BB 801E8828 801F2AB4-BB 801E88E8 8010D228-BB 8010440C 80109BC8-BB 80100D80 00218E50-??? 00220D40 00219120-??? 00221010 006B1058-BB 006B7A88 dec2float F902 ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ECF80-??? 801E29D4 801F2930-BB 801E87FC 801F2AB4-BB 801E88BC 8010D228-BB 801043E0 80109BC8-BB 80100D54 00218E50-??? 00220D60 00219120-??? 00221030 006B1058-BB 006B7AA8 float2dec F903 ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ECF80-??? 801ECAA8 801F2930-BB 801F2458 801F2AB4-BB 801F25DC 8010D228-BB 8010CD50 80109BC8-BB 801096F0 00218E50-??? 00219210 00219120-??? 002194E0 006B1058-BB 006B170C flet @@ -989,7 +989,7 @@ F90C ------------ -------- ------------ -------- ------------ -------- ----- F90D ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ECF28-??? 801E289C 801F28D8-BL 801E86C4 801F2A5C-BL 801E8784 8010D1D0-BL 801042A8 80109B70-BL 80100C1C 00218E90-??? 00220E20 00219160-??? 002210F0 006B107C-BL 006B7B9C fmuli F90E ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ECF80-??? 801E2868 801F2930-BB 801E8690 801F2AB4-BB 801E8750 8010D228-BB 80104274 80109BC8-BB 80100BE8 00218E50-??? 00220E40 00219120-??? 00221110 006B1058-BB 006B7BC0 fdiv F90F ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ECF28-??? 801E2834 801F28D8-BL 801E865C 801F2A5C-BL 801E871C 8010D1D0-BL 80104240 80109B70-BL 80100BB4 00218E90-??? 00220E60 00219160-??? 00221130 006B107C-BL 006B7BE8 fdivi - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- F910 ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ED020-??? 801E2678 801F29D0-... 801E84A0 801F2B54-... 801E8560 8010D2C8-... 80104084 80109C68-... 801009F8 00218E00-??? 00220F50 002190D0-??? 00221220 006B1028-... 006B7CEC get_total_deaths F911 ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ECF80-??? 801E25E8 801F2930-BB 801E8410 801F2AB4-BB 801E84D0 8010D228-BB 80103FF4 80109BC8-BB 80100968 00218E50-??? 00220FB0 00219120-??? 00221280 006B1058-BB 006B7D18 get_stackable_item_count F912 ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ED060-??? 801E25AC 801F2A10-() 801E83E4 801F2B94-() 801E84A4 8010D308-() 80103FB8 80109CA8-() 8010092C 00218DF0-??? 00221030 002190C0-??? 00221300 006B101C-() 006B97D8 freeze_and_hide_equip @@ -1006,7 +1006,7 @@ F91C ------------ -------- ------------ -------- ------------ -------- ----- F91D ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ECFD8-??? 801E1F58 801F2988-B 801E7D90 801F2B0C-B 801E7E50 8010D280-B 80103964 80109C20-B 801002D8 00218E20-??? 00221580 002190F0-??? 00221850 006B1040-B 006B80C4 get_time_played F91E ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ECFD8-??? 801E1F20 801F2988-B 801E7D58 801F2B0C-B 801E7E18 8010D280-B 8010392C 80109C20-B 801002A0 00218E20-??? 002215C0 002190F0-??? 00221890 006B1040-B 006B80F8 get_guildcard_total F91F ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ECFD8-??? 801E1E84 801F2988-B 801E7CBC 801F2B0C-B 801E7D7C 8010D280-B 80103890 80109C20-B 80100204 00218E20-??? 002215E0 002190F0-??? 002218B0 006B1040-B 006B810C get_slot_meseta - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- F920 ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ED020-??? 801E1E04 801F29D0-... 801E7C3C 801F2B54-... 801E7CFC 8010D2C8-... 80103810 80109C68-... 80100184 00218E00-??? 002216B0 002190D0-??? 00221980 006B1028-... 006B8180 get_player_level F921 ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ED020-??? 801E1D54 801F29D0-... 801E7B9C 801F2B54-... 801E7C5C 8010D2C8-... 80103760 80109C68-... 801000D4 00218E00-??? 00221730 002190D0-??? 00221A00 006B1028-... 006B81BC get_section_id F922 ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ED020-??? 801E1C5C 801F29D0-... 801E7AA4 801F2B54-... 801E7B64 8010D2C8-... 80103668 80109C68-... 800FFFDC 00218E00-??? 00221780 002190D0-??? 00221A50 006B1028-... 006B81F0 get_player_hp @@ -1023,7 +1023,7 @@ F92C ------------ -------- ------------ -------- ------------ -------- ----- F92D ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ED020-??? 801E1690 801F29D0-... 801E7530 801F2B54-... 801E75F0 8010D2C8-... 8010309C 80109C68-... 800FFA10 00218E00-??? 00221E00 002190D0-??? 002220D0 006B1028-... 006B8574 color_change F92E ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ED020-??? 801E0FD0 801F29D0-... 801E6E80 801F2B54-... 801E6F40 8010D2C8-... 80102AD8 80109C68-... 800FF44C 00218E00-??? 00222450 002190D0-??? 00222720 006B1028-... 006B8B98 send_statistic F92F ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ED020-??? 801E1DC4 801F29D0-... 801E7C0C 801F2B54-... 801E7CCC 8010D2C8-... 801037D0 80109C68-... 80100144 00218E00-??? 00221710 002190D0-??? 002219E0 006B1028-... 0061CDB0 gba_write_identifiers/nop_F92F - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- F930 ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ED020-??? 801E1570 801F29D0-... 801E7410 801F2B54-... 801E74D0 8010D2C8-... 80102F7C 80109C68-... 800FF8F0 00218E00-??? 00221E60 002190D0-??? 00222130 006B1028-... 006B863C chat_box F931 ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ED020-??? 801E1498 801F29D0-... 801E7348 801F2B54-... 801E7408 8010D2C8-... 80102EA4 80109C68-... 800FF818 00218E00-??? 00221F90 002190D0-??? 00222260 006B1028-... 006B8740 chat_bubble F932 ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ECFD8-??? 801E1444 801F2988-B 801E72F4 801F2B0C-B 801E73B4 8010D280-B 80102E50 80109C20-B 800FF7C4 00218E20-??? 00222040 002190F0-??? 00222310 006B1040-B 006B87FC set_episode2 @@ -1040,7 +1040,7 @@ F93C ------------ -------- ------------ -------- ------------ -------- ----- F93D ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ED020-??? 801E1FB8 801F29D0-... 801E7DF0 801F2B54-... 801E7EB0 8010D2C8-... 801039C4 80109C68-... 80100338 00218E00-??? 00221560 002190D0-??? 00221830 006B1028-... 006B80AC get_lang_setting F93E ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ED020-??? 801E0FAC 801F29D0-... 801E6E5C 801F2B54-... 801E6F1C 8010D2C8-... 80102AB4 80109C68-... 800FF428 00218E00-??? 00222480 002190D0-??? 00222750 006B1028-... 006B96F8 prepare_statistic F93F ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ED020-??? 801E0F88 801F29D0-... 801E6E38 801F2B54-... 801E6EF8 8010D2C8-... 80102A90 80109C68-... 800FF404 00218E00-??? 002224B0 002190D0-??? 00222780 006B1028-... 006B8BC4 keyword_detect - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- F940 ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ED020-??? 801E0F18 801F29D0-... 801E6DC8 801F2B54-... 801E6E88 8010D2C8-... 80102A20 80109C68-... 800FF394 00218E00-??? 002224E0 002190D0-??? 002227B0 006B1028-... 006B8BD4 keyword F941 ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ED020-??? 801E0EB0 801F29D0-... 801E6D60 801F2B54-... 801E6E20 8010D2C8-... 801029B8 80109C68-... 800FF32C 00218E00-??? 00222530 002190D0-??? 00222800 006B1028-... 006B8C10 get_guildcard_num F942 ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801ED020-??? 801E0E54 801F29D0-... 801E6D04 801F2B54-... 801E6DC4 8010D2C8-... 8010295C 80109C68-... 800FF2D0 00218E00-??? 00222580 002190D0-??? 00222850 006B1028-... 006B8C44 get_recent_symbol_chat @@ -1057,7 +1057,7 @@ F94C ------------ -------- ------------ -------- ------------ -------- ----- F94D ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 801F2B54-... 801F064C 8010D280-B 8010ACF4 80109C20-B 80107668 00218E00-??? 00221660 002190D0-??? 00221930 006B1028-... 0061CDB0 has_ep3_save_file/give_card/give_or_take_card/unknown_F94D/nop_F94D F94E ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 006B1028-... 0061CDB0 nop_F94E F94F ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 006B1028-... 0061CDB0 nop_F94F - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- F950 ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 006B1028-... 006B8EA0 bb_p2_menu F951 ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 006B101C-!!! 006B4908 bb_map_designate F952 ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 006B1040-B 006B8EB0 bb_get_number_in_pack @@ -1074,7 +1074,7 @@ F95C ------------ -------- ------------ -------- ------------ -------- ----- F95D ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 006B1028-... 006B93FC bb_exchange_pc F95E ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 006B1028-... 006B941C bb_box_create_bp F95F ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 006B1028-... 006B9104 bb_exchange_pt - DC-NTE--------------- DC112000------------- DC122000---- -------- DCv1USA-------------- DCv2USA-------------- PC-NTE------ -------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- + DC-NTE--------------- DC112000------------- DC122000------------- DCv1USA-------------- DCv2USA-------------- PC-NTE--------------- PC------------------- GC1&2NTE------------- GC-GJAM-------------- GC1&2v11------------- GC1&2v12------------- GCEp3NTE------------- GCEp3USA------------- XBOXBETA------------- XBOX-US0------------- BB------------------- F960 ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 006B1028-... 006B915C bb_send_6xE2 F961 ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- ------------ -------- 006B1040-B 006B91F8 bb_get_6xE3_status diff --git a/src/Account.cc b/src/Account.cc index 02ecdba1..ef8f3b32 100644 --- a/src/Account.cc +++ b/src/Account.cc @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -292,7 +293,7 @@ phosg::JSON Account::json() const { } string Account::str() const { - std::string ret = phosg::string_printf("Account: %010" PRIu32 "/%08" PRIX32 "\n", this->account_id, this->account_id); + std::string ret = std::format("Account: {:010}/{:08X}\n", this->account_id, this->account_id); if (this->flags) { string flags_str = ""; @@ -336,10 +337,10 @@ string Account::str() const { } if (flags_str.empty()) { flags_str = "none"; - } else if (phosg::ends_with(flags_str, ",")) { + } else if (flags_str.ends_with(",")) { flags_str.pop_back(); } - ret += phosg::string_printf(" Flags: %08" PRIX32 " (%s)\n", this->flags, flags_str.c_str()); + ret += std::format(" Flags: {:08X} ({})\n", this->flags, flags_str); } if (this->user_flags) { @@ -349,56 +350,56 @@ string Account::str() const { } if (user_flags_str.empty()) { user_flags_str = "none"; - } else if (phosg::ends_with(user_flags_str, ",")) { + } else if (user_flags_str.ends_with(",")) { user_flags_str.pop_back(); } - ret += phosg::string_printf(" User flags: %08" PRIX32 " (%s)\n", this->user_flags, user_flags_str.c_str()); + ret += std::format(" User flags: {:08X} ({})\n", this->user_flags, user_flags_str); } if (this->ban_end_time) { string time_str = phosg::format_time(this->ban_end_time); - ret += phosg::string_printf(" Banned until: %" PRIu64 " (%s)\n", this->ban_end_time, time_str.c_str()); + ret += std::format(" Banned until: {} ({})\n", this->ban_end_time, time_str); } if (this->ep3_current_meseta || this->ep3_total_meseta_earned) { - ret += phosg::string_printf(" Episode 3 meseta: %" PRIu32 " (total earned: %" PRIu32 ")\n", + ret += std::format(" Episode 3 meseta: {} (total earned: {})\n", this->ep3_current_meseta, this->ep3_total_meseta_earned); } if (!this->last_player_name.empty()) { - ret += phosg::string_printf(" Last player name: \"%s\"\n", this->last_player_name.c_str()); + ret += std::format(" Last player name: \"{}\"\n", this->last_player_name); } if (!this->auto_reply_message.empty()) { - ret += phosg::string_printf(" Auto reply message: \"%s\"\n", this->auto_reply_message.c_str()); + ret += std::format(" Auto reply message: \"{}\"\n", this->auto_reply_message); } if (this->bb_team_id) { - ret += phosg::string_printf(" BB team ID: %08" PRIX32 "\n", this->bb_team_id); + ret += std::format(" BB team ID: {:08X}\n", this->bb_team_id); } if (this->is_temporary) { - ret += phosg::string_printf(" Is temporary license: true\n"); + ret += std::format(" Is temporary license: true\n"); } for (const auto& it : this->dc_nte_licenses) { - ret += phosg::string_printf(" DC NTE license: serial_number=%s access_key=%s\n", - it.second->serial_number.c_str(), it.second->access_key.c_str()); + ret += std::format(" DC NTE license: serial_number={} access_key={}\n", + it.second->serial_number, it.second->access_key); } for (const auto& it : this->dc_licenses) { - ret += phosg::string_printf(" DC license: serial_number=%" PRIX32 " access_key=%s\n", - it.second->serial_number, it.second->access_key.c_str()); + ret += std::format(" DC license: serial_number={:X} access_key={}\n", + it.second->serial_number, it.second->access_key); } for (const auto& it : this->pc_licenses) { - ret += phosg::string_printf(" PC license: serial_number=%" PRIX32 " access_key=%s\n", - it.second->serial_number, it.second->access_key.c_str()); + ret += std::format(" PC license: serial_number={:X} access_key={}\n", + it.second->serial_number, it.second->access_key); } for (const auto& it : this->gc_licenses) { - ret += phosg::string_printf(" GC license: serial_number=%010" PRIu32 " access_key=%s password=%s\n", - it.second->serial_number, it.second->access_key.c_str(), it.second->password.c_str()); + ret += std::format(" GC license: serial_number={:010} access_key={} password={}\n", + it.second->serial_number, it.second->access_key, it.second->password); } for (const auto& it : this->xb_licenses) { - ret += phosg::string_printf(" XB license: gamertag=%s user_id=%016" PRIX64 " account_id=%016" PRIX64 "\n", - it.second->gamertag.c_str(), it.second->user_id, it.second->account_id); + ret += std::format(" XB license: gamertag={} user_id={:016X} account_id={:016X}\n", + it.second->gamertag, it.second->user_id, it.second->account_id); } for (const auto& it : this->bb_licenses) { - ret += phosg::string_printf(" BB license: username=%s password=%s\n", - it.second->username.c_str(), it.second->password.c_str()); + ret += std::format(" BB license: username={} password={}\n", + it.second->username, it.second->password); } phosg::strip_trailing_whitespace(ret); @@ -409,13 +410,13 @@ void Account::save() const { if (!this->is_temporary) { auto json = this->json(); string json_data = json.serialize(phosg::JSON::SerializeOption::FORMAT | phosg::JSON::SerializeOption::HEX_INTEGERS); - string filename = phosg::string_printf("system/licenses/%010" PRIu32 ".json", this->account_id); + string filename = std::format("system/licenses/{:010}.json", this->account_id); phosg::save_file(filename, json_data); } } void Account::delete_file() const { - string filename = phosg::string_printf("system/licenses/%010" PRIu32 ".json", this->account_id); + string filename = std::format("system/licenses/{:010}.json", this->account_id); remove(filename.c_str()); } @@ -440,24 +441,24 @@ uint64_t Login::proxy_session_id() const { } string Login::str() const { - string ret = phosg::string_printf("Account:%08" PRIX32, this->account->account_id); + string ret = std::format("Account:{:08X}", this->account->account_id); if (this->account_was_created) { ret += " (new)"; } if (this->dc_nte_license) { - ret += phosg::string_printf(" via DC NTE serial number %s", this->dc_nte_license->serial_number.c_str()); + ret += std::format(" via DC NTE serial number {}", this->dc_nte_license->serial_number); } else if (this->dc_license) { - ret += phosg::string_printf(" via DC serial number %08" PRIX32, this->dc_license->serial_number); + ret += std::format(" via DC serial number {:08X}", this->dc_license->serial_number); } else if (this->pc_license) { - ret += phosg::string_printf(" via PC serial number %08" PRIX32, this->pc_license->serial_number); + ret += std::format(" via PC serial number {:08X}", this->pc_license->serial_number); } else if (this->gc_license) { - ret += phosg::string_printf(" via GC serial number %010" PRIu32, this->gc_license->serial_number); + ret += std::format(" via GC serial number {:010}", this->gc_license->serial_number); } else if (this->xb_license) { - ret += phosg::string_printf(" via XB user ID %016" PRIX64, this->xb_license->user_id); + ret += std::format(" via XB user ID {:016X}", this->xb_license->user_id); } else if (this->bb_license) { - ret += phosg::string_printf(" via BB username %s", this->bb_license->username.c_str()); + ret += std::format(" via BB username {}", this->bb_license->username); } else { - ret += phosg::string_printf(" artificially"); + ret += std::format(" artificially"); } return ret; } @@ -1043,16 +1044,17 @@ shared_ptr AccountIndex::create_temporary_account_for_shared_account( AccountIndex::AccountIndex(bool force_all_temporary) : force_all_temporary(force_all_temporary) { if (!this->force_all_temporary) { - if (!phosg::isdir("system/licenses")) { - mkdir("system/licenses", 0755); + if (!std::filesystem::is_directory("system/licenses")) { + std::filesystem::create_directories("system/licenses"); } else { - for (const auto& item : phosg::list_directory("system/licenses")) { - if (phosg::ends_with(item, ".json")) { + for (const auto& item : std::filesystem::directory_iterator("system/licenses")) { + string filename = item.path().filename().string(); + if (filename.ends_with(".json")) { try { - phosg::JSON json = phosg::JSON::parse(phosg::load_file("system/licenses/" + item)); + phosg::JSON json = phosg::JSON::parse(phosg::load_file("system/licenses/" + filename)); this->add(make_shared(json)); } catch (const exception& e) { - phosg::log_error("Failed to index account %s", item.c_str()); + phosg::log_error_f("Failed to index account {}", filename); throw; } } diff --git a/src/AddressTranslator.cc b/src/AddressTranslator.cc index 744b6553..c0cf60c0 100644 --- a/src/AddressTranslator.cc +++ b/src/AddressTranslator.cc @@ -1,6 +1,7 @@ #include "AddressTranslator.hh" #include +#include #include #include #include @@ -113,42 +114,43 @@ public: AddressTranslator(const string& directory) : log("[addr-trans] "), directory(directory) { - while (phosg::ends_with(this->directory, "/")) { + while (this->directory.ends_with("/")) { this->directory.pop_back(); } - for (const auto& filename : phosg::list_directory(this->directory)) { + for (const auto& item : std::filesystem::directory_iterator(this->directory)) { + string filename = item.path().filename().string(); if (filename.size() < 4) { continue; } string name = filename.substr(0, filename.size() - 4); string path = directory + "/" + filename; - if (phosg::ends_with(filename, ".dol")) { + if (filename.ends_with(".dol")) { ResourceDASM::DOLFile dol(path.c_str()); auto mem = make_shared(); dol.load_into(mem); this->mems.emplace(name, mem); this->ppc_mems.emplace(mem); - this->log.info("Loaded %s", name.c_str()); - } else if (phosg::ends_with(filename, ".xbe")) { + this->log.info_f("Loaded {}", name); + } else if (filename.ends_with(".xbe")) { ResourceDASM::XBEFile xbe(path.c_str()); auto mem = make_shared(); xbe.load_into(mem); this->mems.emplace(name, mem); - this->log.info("Loaded %s", name.c_str()); - } else if (phosg::ends_with(filename, ".exe")) { + this->log.info_f("Loaded {}", name); + } else if (filename.ends_with(".exe")) { ResourceDASM::PEFile pe(path.c_str()); auto mem = make_shared(); pe.load_into(mem); this->mems.emplace(name, mem); - this->log.info("Loaded %s", name.c_str()); - } else if (phosg::ends_with(filename, ".bin")) { + this->log.info_f("Loaded {}", name); + } else if (filename.ends_with(".bin")) { string data = phosg::load_file(path); auto mem = make_shared(); mem->allocate_at(0x8C010000, data.size()); mem->memcpy(0x8C010000, data.data(), data.size()); this->mems.emplace(name, mem); - this->log.info("Loaded %s", name.c_str()); + this->log.info_f("Loaded {}", name); } } } @@ -202,14 +204,14 @@ public: } } if (r2_low_found && r2_high_found) { - fprintf(stderr, "(%s) r2 = %08" PRIX32 "\n", it.first.c_str(), r2); + phosg::fwrite_fmt(stderr, "({}) r2 = {:08X}\n", it.first, r2); } else { - fprintf(stderr, "(%s) r2 = __MISSING__\n", it.first.c_str()); + phosg::fwrite_fmt(stderr, "({}) r2 = __MISSING__\n", it.first); } if (r13_low_found && r13_high_found) { - fprintf(stderr, "(%s) r13 = %08" PRIX32 "\n", it.first.c_str(), r13); + phosg::fwrite_fmt(stderr, "({}) r13 = {:08X}\n", it.first, r13); } else { - fprintf(stderr, "(%s) r13 = __MISSING__\n", it.first.c_str()); + phosg::fwrite_fmt(stderr, "({}) r13 = __MISSING__\n", it.first); } } } @@ -357,16 +359,16 @@ public: } for (const auto& [type, constructor_to_area_ranges] : table) { - fprintf(stdout, "%04" PRIX32 " =>", type); + phosg::fwrite_fmt(stdout, "{:04X} =>", type); for (const auto& [constructor, area_ranges] : constructor_to_area_ranges) { - fprintf(stdout, " %08" PRIX32, constructor); + phosg::fwrite_fmt(stdout, " {:08X}", constructor); bool is_first = true; for (const auto& [start, end] : area_ranges) { fputc(is_first ? ':' : ',', stdout); if (start == end) { - fprintf(stdout, "%02zX", start); + phosg::fwrite_fmt(stdout, "{:02X}", start); } else { - fprintf(stdout, "%02zX-%02zX", start, end); + phosg::fwrite_fmt(stdout, "{:02X}-{:02X}", start, end); } is_first = false; } @@ -403,17 +405,17 @@ public: if (!cell_data.empty()) { cell_data.push_back(' '); } - cell_data += phosg::string_printf("%08" PRIX32, constructor); + cell_data += std::format("{:08X}", constructor); if (print_area_masks) { - cell_data += phosg::string_printf(":%016" PRIX64, this->area_mask_for_ranges(area_ranges)); + cell_data += std::format(":{:016X}", this->area_mask_for_ranges(area_ranges)); } else { bool is_first = true; for (const auto& [start, end] : area_ranges) { cell_data.push_back(is_first ? ':' : ','); if (start == end) { - cell_data += phosg::string_printf("%02zX", start); + cell_data += std::format("{:02X}", start); } else { - cell_data += phosg::string_printf("%02zX-%02zX", start, end); + cell_data += std::format("{:02X}-{:02X}", start, end); } is_first = false; } @@ -438,7 +440,7 @@ public: header_line += " NAME"; for (const auto& [type, formatted_cells] : formatted_cells_for_type) { - string line = phosg::string_printf("%04" PRIX32 " =>", type); + string line = std::format("{:04X} =>", type); for (const auto& spec : specs) { size_t width = version_widths.at(spec.src_name); try { @@ -465,7 +467,7 @@ public: for (auto& line : formatted_lines) { phosg::strip_trailing_whitespace(line); - fprintf(stdout, "%s\n", line.c_str()); + phosg::fwrite_fmt(stdout, "{}\n", line); } } @@ -500,7 +502,7 @@ public: size_t src_offset = src_addr - src_section.first; size_t src_bytes_available_before = src_offset; size_t src_bytes_available_after = src_section.second - src_offset - 4; - this->log.info("(find_match/%s) Source offset = %08zX with %zX/%zX bytes available before/after", + this->log.info_f("(find_match/{}) Source offset = {:08X} with {:X}/{:X} bytes available before/after", method_token, src_offset, src_bytes_available_before, src_bytes_available_after); size_t match_bytes_before = 0; @@ -570,7 +572,7 @@ public: } } } - this->log.info("(find_match/%s) For match length %zX, %zu matches found", method_token, match_length, num_matches); + this->log.info_f("(find_match/{}) For match length {:X}, {} matches found", method_token, match_length, num_matches); if (num_matches == 1) { return last_match_address; } else if (num_matches == 0) { @@ -641,7 +643,7 @@ public: map results; for (const auto& it : this->mems) { if (it.second == this->src_mem) { - log.info("(%s) %08" PRIX32 " (from source)", it.first.c_str(), src_addr); + log.info_f("({}) {:08X} (from source)", it.first, src_addr); results.emplace(it.first, src_addr); } else { @@ -673,24 +675,24 @@ public: 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); + log.info_f("({}) ({}) {:08X}", it.first, method_name, ret); match_addrs.emplace(ret); } catch (const exception& e) { - log.error("(%s) (%s) failed: %s", it.first.c_str(), method_name, e.what()); + log.error_f("({}) ({}) failed: {}", it.first, method_name, e.what()); } } if (match_addrs.empty()) { - log.error("(%s) no match found", it.first.c_str()); + log.error_f("({}) no match found", it.first); } else if (match_addrs.size() > 1) { - log.error("(%s) different matches found by different methods", it.first.c_str()); + log.error_f("({}) different matches found by different methods", it.first); } else { results.emplace(it.first, *match_addrs.begin()); } } } for (const auto& it : results) { - fprintf(stdout, "%s => %08" PRIX32 "\n", it.first.c_str(), it.second); + phosg::fwrite_fmt(stdout, "{} => {:08X}\n", it.first, it.second); } } @@ -749,7 +751,7 @@ public: } } } - this->log.info("... For match length %zX, %zu matches found", match_length, num_matches); + this->log.info_f("... For match length {:X}, {} matches found", match_length, num_matches); if (num_matches == 1) { return last_match_address; } else if (num_matches == 0) { @@ -778,27 +780,27 @@ public: map results; for (const auto& it : this->mems) { if (it.second == this->src_mem) { - log.info("(%s) %08" PRIX32 " (from source)", it.first.c_str(), src_addr); + log.info_f("({}) {:08X} (from source)", it.first, src_addr); results.emplace(it.first, src_addr); } else { uint32_t ret = 0; try { ret = this->find_be_to_le_data_match(it.second, src_addr, src_size); - log.info("(%s) %08" PRIX32, it.first.c_str(), ret); + log.info_f("({}) {:08X}", it.first, ret); } catch (const exception& e) { - log.error("(%s) failed: %s", it.first.c_str(), e.what()); + log.error_f("({}) failed: {}", it.first, e.what()); } if (ret == 0) { - log.error("(%s) no match found", it.first.c_str()); + log.error_f("({}) no match found", it.first); } else { results.emplace(it.first, ret); } } } for (const auto& it : results) { - fprintf(stdout, "%s => %08" PRIX32 "\n", it.first.c_str(), it.second); + phosg::fwrite_fmt(stdout, "{} => {:08X}\n", it.first, it.second); } } @@ -808,7 +810,7 @@ public: uint32_t last_addr = sec_addr + sec_size - data.size(); for (uint32_t addr = sec_addr; addr < last_addr; addr++) { if (!mem->memcmp(addr, data.data(), data.size())) { - fprintf(stderr, "%s => %08" PRIX32 "\n", name.c_str(), addr); + phosg::fwrite_fmt(stderr, "{} => {:08X}\n", name, addr); } } } @@ -849,9 +851,9 @@ public: void run_shell() { while (!feof(stdin)) { if (!this->src_filename.empty()) { - fprintf(stdout, "addr-trans:%s/%s> ", this->directory.c_str(), this->src_filename.c_str()); + phosg::fwrite_fmt(stdout, "addr-trans:{}/{}> ", this->directory, this->src_filename); } else { - fprintf(stdout, "addr-trans:%s> ", this->directory.c_str()); + phosg::fwrite_fmt(stdout, "addr-trans:{}> ", this->directory); } fflush(stdout); @@ -859,7 +861,7 @@ public: try { this->handle_command(command); } catch (const exception& e) { - this->log.error("Failed: %s", e.what()); + this->log.error_f("Failed: {}", e.what()); } } fputc('\n', stdout); diff --git a/src/AsyncHTTPServer.cc b/src/AsyncHTTPServer.cc new file mode 100644 index 00000000..ea62645e --- /dev/null +++ b/src/AsyncHTTPServer.cc @@ -0,0 +1,409 @@ +#include "AsyncHTTPServer.hh" + +#include +#include + +#include +#include +#include +#include +#include + +#include "AsyncUtils.hh" +#include "Loggers.hh" +#include "Revision.hh" +#include "Server.hh" + +using namespace std; + +static const unordered_map explanation_for_response_code{ + {100, "Continue"}, + {101, "Switching Protocols"}, + {102, "Processing"}, + {200, "OK"}, + {201, "Created"}, + {202, "Accepted"}, + {203, "Non-Authoritative Information"}, + {204, "No Content"}, + {205, "Reset Content"}, + {206, "Partial Content"}, + {207, "Multi-Status"}, + {208, "Already Reported"}, + {226, "IM Used"}, + {300, "Multiple Choices"}, + {301, "Moved Permanently"}, + {302, "Found"}, + {303, "See Other"}, + {304, "Not Modified"}, + {305, "Use Proxy"}, + {307, "Temporary Redirect"}, + {308, "Permanent Redirect"}, + {400, "Bad Request"}, + {401, "Unathorized"}, + {402, "Payment Required"}, + {403, "Forbidden"}, + {404, "Not Found"}, + {405, "Method Not Allowed"}, + {406, "Not Acceptable"}, + {407, "Proxy Authentication Required"}, + {408, "Request Timeout"}, + {409, "Conflict"}, + {410, "Gone"}, + {411, "Length Required"}, + {412, "Precondition Failed"}, + {413, "Request Entity Too Large"}, + {414, "Request-URI Too Long"}, + {415, "Unsupported Media Type"}, + {416, "Requested Range Not Satisfiable"}, + {417, "Expectation Failed"}, + {418, "I\'m a Teapot"}, + {420, "Enhance Your Calm"}, + {422, "Unprocessable Entity"}, + {423, "Locked"}, + {424, "Failed Dependency"}, + {426, "Upgrade Required"}, + {428, "Precondition Required"}, + {429, "Too Many Requests"}, + {431, "Request Header Fields Too Large"}, + {444, "No Response"}, + {449, "Retry With"}, + {451, "Unavailable For Legal Reasons"}, + {500, "Internal Server Error"}, + {501, "Not Implemented"}, + {502, "Bad Gateway"}, + {503, "Service Unavailable"}, + {504, "Gateway Timeout"}, + {505, "HTTP Version Not Supported"}, + {506, "Variant Also Negotiates"}, + {507, "Insufficient Storage"}, + {508, "Loop Detected"}, + {509, "Bandwidth Limit Exceeded"}, + {510, "Not Extended"}, + {511, "Network Authentication Required"}, + {598, "Network Read Timeout Error"}, + {599, "Network Connect Timeout Error"}, +}; + +HTTPError::HTTPError(int code, const std::string& what) + : std::runtime_error(what), code(code) {} + +const std::string* HTTPRequest::get_header(const std::string& name) const { + auto its = this->headers.equal_range(name); + if (its.first == its.second) { + return nullptr; + } + const string* ret = &its.first->second; + its.first++; + if (its.first != its.second) { + throw std::out_of_range("Header appears multiple times: " + name); + } + return ret; +} + +const std::string* HTTPRequest::get_query_param(const std::string& name) const { + auto its = this->query_params.equal_range(name); + if (its.first == its.second) { + return nullptr; + } + const string* ret = &its.first->second; + its.first++; + if (its.first != its.second) { + throw std::out_of_range("Query parameter appears multiple times: " + name); + } + return ret; +} + +static void url_decode_inplace(string& s) { + size_t write_offset = 0, read_offset = 0; + for (; read_offset < s.size(); write_offset++) { + if ((s[read_offset] == '%') && (read_offset < s.size() - 2)) { + s[write_offset] = + static_cast(phosg::value_for_hex_char(s[read_offset + 1]) << 4) | + static_cast(phosg::value_for_hex_char(s[read_offset + 2])); + read_offset += 3; + } else if (s[write_offset] == '+') { + s[write_offset] = ' '; + read_offset++; + } else { + s[write_offset] = s[read_offset]; + read_offset++; + } + } + s.resize(write_offset); +} + +HTTPClient::HTTPClient(asio::ip::tcp::socket&& sock) : r(std::move(sock)) {} + +asio::awaitable HTTPClient::recv_http_request(size_t max_line_size, size_t max_body_size) { + HTTPRequest req; + std::string request_line = co_await this->r.read_line("\r\n", max_line_size); + auto line_tokens = phosg::split(request_line, ' '); + if (line_tokens.size() != 3) { + throw runtime_error("invalid HTTP request line"); + } + const auto& method_token = line_tokens[0]; + if (method_token == "GET") { + req.method = HTTPRequest::Method::GET; + } else if (method_token == "POST") { + req.method = HTTPRequest::Method::POST; + } else if (method_token == "DELETE") { + req.method = HTTPRequest::Method::DELETE; + } else if (method_token == "HEAD") { + req.method = HTTPRequest::Method::HEAD; + } else if (method_token == "PATCH") { + req.method = HTTPRequest::Method::PATCH; + } else if (method_token == "PUT") { + req.method = HTTPRequest::Method::PUT; + } else if (method_token == "UPDATE") { + req.method = HTTPRequest::Method::UPDATE; + } else if (method_token == "OPTIONS") { + req.method = HTTPRequest::Method::OPTIONS; + } else if (method_token == "CONNECT") { + req.method = HTTPRequest::Method::CONNECT; + } else if (method_token == "TRACE") { + req.method = HTTPRequest::Method::TRACE; + } else { + throw HTTPError(400, "unknown request method"); + } + + req.http_version = std::move(line_tokens[2]); + + size_t fragment_start_offset = line_tokens[1].find('#'); + if (fragment_start_offset != string::npos) { + req.fragment = line_tokens[1].substr(fragment_start_offset + 1); + line_tokens[1].resize(fragment_start_offset); + } + + size_t query_start_offset = line_tokens[1].find('?'); + string query; + if (query_start_offset != string::npos) { + query = line_tokens[1].substr(query_start_offset + 1); + line_tokens[1].resize(query_start_offset); + } + + req.path = std::move(line_tokens[1]); + if (req.path.empty()) { + throw std::runtime_error("request path is missing"); + } + + auto query_tokens = phosg::split(query, '&'); + for (auto& token : query_tokens) { + size_t equals_pos = token.find('='); + if (equals_pos == string::npos) { + url_decode_inplace(token); + req.query_params.emplace(std::move(token), ""); + } else { + string key = token.substr(0, equals_pos); + string value = token.substr(equals_pos + 1); + url_decode_inplace(key); + url_decode_inplace(value); + req.query_params.emplace(std::move(key), std::move(value)); + } + } + + auto prev_header_it = req.headers.end(); + for (;;) { + std::string line = co_await this->r.read_line("\r\n", max_line_size); + if (line.empty()) { + break; + } + if (line[0] == ' ' || line[0] == '\t') { + if (prev_header_it == req.headers.end()) { + throw std::runtime_error("received header continuation line before any header"); + } else { + phosg::strip_whitespace(line); + prev_header_it->second.append(1, ' '); + prev_header_it->second += line; + } + } else { + size_t colon_pos = line.find(':'); + if (colon_pos == string::npos) { + throw runtime_error("malformed header line"); + } + string key = line.substr(0, colon_pos); + string value = line.substr(colon_pos + 1); + phosg::strip_whitespace(key); + phosg::strip_whitespace(value); + prev_header_it = req.headers.emplace(phosg::tolower(key), std::move(value)); + } + } + + auto transfer_encoding_header = req.get_header("transfer-encoding"); + if (transfer_encoding_header && phosg::tolower(*transfer_encoding_header) == "chunked") { + deque chunks; + size_t total_data_bytes = 0; + for (;;) { + auto line = co_await this->r.read_line("\r\n", 0x20); + size_t parse_offset = 0; + size_t chunk_size = stoull(line, &parse_offset, 16); + if (parse_offset != line.size()) { + throw HTTPError(400, "invalid chunk header during chunked encoding"); + } + if (chunk_size == 0) { + break; + } + total_data_bytes += chunk_size; + if (total_data_bytes > max_body_size) { + throw HTTPError(400, "request data size too large"); + } + chunks.emplace_back(co_await this->r.read_data(chunk_size)); + auto after_chunk_data = co_await this->r.read_line("\r\n", 0x20); + if (!after_chunk_data.empty()) { + throw HTTPError(400, "incorrect trailing sequence after chunk data"); + } + } + } else { + auto content_length_header = req.get_header("content-length"); + size_t content_length = content_length_header ? stoull(*content_length_header) : 0; + if (content_length > max_body_size) { + throw HTTPError(400, "request data size too large"); + } else if (content_length > 0) { + req.data = co_await this->r.read_data(content_length); + } + } + + co_return req; +} + +asio::awaitable HTTPClient::send_http_response(const HTTPResponse& resp) { + AsyncWriteCollector w; + w.add(std::format("{} {} {}\r\n", + resp.http_version, resp.response_code, explanation_for_response_code.at(resp.response_code))); + for (const auto& it : resp.headers) { + w.add(it.first + ": " + it.second + "\r\n"); + } + if (!resp.data.empty()) { + w.add(std::format("Content-Length: {}\r\n", resp.data.size())); + } + w.add("\r\n"); + if (!resp.data.empty()) { + w.add_reference(resp.data.data(), resp.data.size()); + } + co_await w.write(this->r.get_socket()); +} + +asio::awaitable HTTPClient::recv_websocket_message(size_t max_data_size) { + WebSocketMessage prev_msg; + bool prev_msg_present = false; + + while (this->r.get_socket().is_open()) { + WebSocketMessage msg; + + // We need at most 10 bytes to determine if there's a valid frame, or as + // little as 2 + co_await this->r.read_data_into(msg.header, 2); + + // Get the payload size + bool has_mask = msg.header[1] & 0x80; + size_t payload_size = msg.header[1] & 0x7F; + if (payload_size == 0x7F) { + phosg::be_uint64_t wire_size; + co_await this->r.read_data_into(&wire_size, sizeof(wire_size)); + payload_size = wire_size; + } else if (payload_size == 0x7E) { + phosg::be_uint16_t wire_size; + co_await this->r.read_data_into(&wire_size, sizeof(wire_size)); + payload_size = wire_size; + } + + if (payload_size > max_data_size) { + throw runtime_error("Incoming WebSocket message exceeds size limit"); + } + + // Read the masking key if present + if (has_mask) { + co_await this->r.read_data_into(msg.mask_key, sizeof(msg.mask_key)); + } + + // Read and unmask message data + msg.data = co_await this->r.read_data(payload_size); + if (has_mask) { + for (size_t x = 0; x < msg.data.size(); x++) { + msg.data[x] ^= msg.mask_key[x & 3]; + } + } + + this->last_communication_time = phosg::now(); + + // If the current message is a control message, respond appropriately + // (these can be sent in the middle of fragmented messages) + uint8_t opcode = msg.header[0] & 0x0F; + if (opcode & 0x08) { + if (opcode == 0x0A) { + // Ping response; ignore it + + } else if (opcode == 0x08) { + // Close message + co_await this->send_websocket_message(msg.data, msg.opcode); + this->r.get_socket().close(); + + } else if (opcode == 0x09) { + // Ping message + co_await this->send_websocket_message(msg.data, 0x0A); + + } else { + // Unknown control message type + this->r.get_socket().close(); + } + continue; + } + + // If there's an existing fragment, the current message's opcode should be + // zero; if there's no pending message, it must not be zero + if (prev_msg_present == (opcode != 0)) { + this->r.get_socket().close(); + continue; + } + + // Save the message opcode, if present, and append the frame data + if (!prev_msg_present) { + prev_msg = std::move(msg); + } else { + prev_msg.header[0] = msg.header[0]; + prev_msg.header[1] = msg.header[1]; + if (opcode) { + prev_msg.opcode = msg.opcode; + } + if (has_mask) { + prev_msg.mask_key[0] = msg.mask_key[0]; + prev_msg.mask_key[1] = msg.mask_key[1]; + prev_msg.mask_key[2] = msg.mask_key[2]; + prev_msg.mask_key[3] = msg.mask_key[3]; + } + prev_msg.data += msg.data; + } + + // If the FIN bit is set, then the frame is complete - append the payload + // to any pending payloads and call the message handler. If the FIN bit + // isn't set, we need to receive at least one continuation frame to + // complete the message. + if (prev_msg.header[0] & 0x80) { + co_return prev_msg; + } + } + + throw logic_error("failed to receive websocket message"); +} + +asio::awaitable HTTPClient::send_websocket_message(const void* data, size_t size, uint8_t opcode) { + phosg::StringWriter w; + w.put_u8(0x80 | (opcode & 0x0F)); + if (size > 0xFFFF) { + w.put_u8(0x7F); + w.put_u64b(size); + } else if (size > 0x7D) { + w.put_u8(0x7E); + w.put_u16b(size); + } else { + w.put_u8(size); + } + + array bufs = {asio::const_buffer(w.data(), w.size()), asio::const_buffer(data, size)}; + co_await asio::async_write(this->r.get_socket(), bufs, asio::use_awaitable); +} + +asio::awaitable HTTPClient::send_websocket_message(const std::string& data, uint8_t opcode) { + return this->send_websocket_message(data.data(), data.size(), opcode); +} + +const HTTPServerLimits DEFAULT_HTTP_LIMITS; diff --git a/src/AsyncHTTPServer.hh b/src/AsyncHTTPServer.hh new file mode 100644 index 00000000..8ded5cc0 --- /dev/null +++ b/src/AsyncHTTPServer.hh @@ -0,0 +1,228 @@ +#pragma once + +#include "WindowsPlatform.hh" + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "AsyncUtils.hh" +#include "Server.hh" + +struct HTTPRequest { + enum class Method { + GET = 0, + POST, + DELETE, + HEAD, + PATCH, + PUT, + UPDATE, + OPTIONS, + CONNECT, + TRACE, + }; + std::string http_version; + Method method; + std::string path; + std::string fragment; + std::unordered_multimap headers; // Header names converted to all lowercase + std::unordered_multimap query_params; + std::string data; + + // Header name should be entirely lowercase for this function. Returns + // nullptr if the header doesn't exist; throws http_error(400) if multiple + // instances of it exist. + const std::string* get_header(const std::string& name) const; + + const std::string* get_query_param(const std::string& name) const; +}; + +struct HTTPResponse { + std::string http_version; + int response_code = 200; + // Content-Length should NOT be specified in headers; it is automatically + // added in async_write() if data is not blank. + std::unordered_multimap headers; + std::string data; +}; + +struct WebSocketMessage { + uint8_t header[2] = {0, 0}; + uint8_t opcode = 0x01; + uint8_t mask_key[4] = {0, 0, 0, 0}; + std::string data; +}; + +class HTTPError : public std::runtime_error { +public: + HTTPError(int code, const std::string& what); + int code; +}; + +struct HTTPClient { + AsyncSocketReader r; + uint64_t last_communication_time = 0; + bool is_websocket = false; + + HTTPClient(asio::ip::tcp::socket&& sock); + + asio::awaitable recv_http_request(size_t max_line_size, size_t max_body_size); + asio::awaitable send_http_response(const HTTPResponse& resp); + + asio::awaitable recv_websocket_message(size_t max_data_size); + asio::awaitable send_websocket_message(const void* data, size_t size, uint8_t opcode = 0x01); + asio::awaitable send_websocket_message(const std::string& data, uint8_t opcode = 0x01); +}; + +struct HTTPServerLimits { + size_t max_http_request_line_size = 0x1000; // 4KB + size_t max_http_data_size = 0x200000; // 2MB + size_t max_http_keepalive_idle_usecs = 300 * 1000 * 1000; // 5 minutes (0 = no limit) + size_t max_websocket_message_size = 0x200000; // 2MB + size_t max_websocket_idle_usecs = 300 * 1000 * 1000; // 5 minutes (0 = no limit) +}; + +extern const HTTPServerLimits DEFAULT_HTTP_LIMITS; + +template +class AsyncHTTPServer : public Server { +public: + explicit AsyncHTTPServer( + std::shared_ptr io_context, + const std::string& log_prefix = "[AsyncHTTPServer] ", + const HTTPServerLimits& limits = DEFAULT_HTTP_LIMITS) + : Server(io_context, log_prefix), limits(limits) {} + AsyncHTTPServer(const AsyncHTTPServer&) = delete; + AsyncHTTPServer(AsyncHTTPServer&&) = delete; + AsyncHTTPServer& operator=(const AsyncHTTPServer&) = delete; + AsyncHTTPServer& operator=(AsyncHTTPServer&&) = delete; + virtual ~AsyncHTTPServer() = default; + + void listen(const std::string& addr, int port) { + if (port == 0) { + throw std::runtime_error("Listening port cannot be zero"); + } + asio::ip::address asio_addr = addr.empty() ? asio::ip::address_v4::any() : asio::ip::make_address(addr); + auto sock = std::make_shared(); + sock->name = std::format("http:{}:{}", addr, port); + sock->endpoint = asio::ip::tcp::endpoint(asio_addr, port); + this->add_socket(std::move(sock)); + } + +protected: + HTTPServerLimits limits; + + // Attempts to switch the client to WebSockets. Returns true if this is done + // successfully (and the caller should then receive/send WebSocket messages), + // or false if this failed (and the caller should send an HTTP response). + asio::awaitable enable_websockets(std::shared_ptr c, const HTTPRequest& req) { + if (req.method != HTTPRequest::Method::GET) { + co_return false; + } + + auto connection_header = req.get_header("connection"); + if (!connection_header || phosg::tolower(*connection_header) != "upgrade") { + co_return false; + } + auto upgrade_header = req.get_header("upgrade"); + if (!upgrade_header || phosg::tolower(*upgrade_header) != "websocket") { + co_return false; + } + auto sec_websocket_key_header = req.get_header("sec-websocket-key"); + if (!sec_websocket_key_header) { + co_return false; + } + + std::string sec_websocket_accept_data = *sec_websocket_key_header + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; + std::string sec_websocket_accept = phosg::base64_encode(phosg::SHA1(sec_websocket_accept_data).bin()); + + HTTPResponse resp; + resp.http_version = req.http_version; + resp.response_code = 101; + resp.headers.emplace("Upgrade", "websocket"); + resp.headers.emplace("Connection", "upgrade"); + resp.headers.emplace("Sec-WebSocket-Accept", std::move(sec_websocket_accept)); + co_await c->send_http_response(resp); + + c->is_websocket = true; + co_return true; + } + + [[nodiscard]] virtual std::shared_ptr create_client( + std::shared_ptr, asio::ip::tcp::socket&& client_sock) { + return std::make_shared(std::move(client_sock)); + } + + // handle_request must do one of the following three things: + // 1. Return an HTTP response. + // 2. Call enable_websockets, and if it returns true, return nullptr. After + // this point, handle_request will not be called again for this client; + // handle_websocket_message will be called instead when any WebSocket + // messages are received. If enable_websockets returns false, + // handle_request must still return an HTTP response. + // 3. Throw an exception. In this case, the client receives an HTTP 500 + // response. + virtual asio::awaitable> handle_request(std::shared_ptr c, HTTPRequest&& req) = 0; + virtual asio::awaitable handle_websocket_message(std::shared_ptr, WebSocketMessage&&) { + co_return; + } + + virtual asio::awaitable handle_client(std::shared_ptr c) { + asio::steady_timer idle_timer(*this->io_context); + while (c->r.get_socket().is_open()) { + if (c->is_websocket) { + WebSocketMessage msg = co_await c->recv_websocket_message(this->limits.max_websocket_message_size); + idle_timer.cancel(); + try { + co_await this->handle_websocket_message(c, std::move(msg)); + } catch (const std::exception& e) { + c->r.close(); + } + + } else { + HTTPRequest req = co_await c->recv_http_request( + this->limits.max_http_request_line_size, this->limits.max_http_data_size); + idle_timer.cancel(); + std::unique_ptr resp; + try { + resp = co_await this->handle_request(c, std::move(req)); + } catch (const std::exception& e) { + resp = std::make_unique(); + resp->http_version = req.http_version; + resp->response_code = 500; + resp->headers.emplace("Content-Type", "text/plain"); + resp->data = "Internal server error:\n"; + resp->data += e.what(); + } + if (resp) { + co_await c->send_http_response(*resp); + } + auto* conn_header = req.get_header("connection"); + if (!conn_header || (*conn_header != "keep-alive")) { + c->r.close(); + } + } + + size_t idle_usecs_limit = c->is_websocket + ? this->limits.max_websocket_idle_usecs + : this->limits.max_http_keepalive_idle_usecs; + if (idle_usecs_limit && c->r.get_socket().is_open()) { + idle_timer.expires_after(std::chrono::microseconds(idle_usecs_limit)); + idle_timer.async_wait([c](std::error_code ec) { + if (!ec) { + c->r.close(); + } + }); + } + } + idle_timer.cancel(); + } +}; diff --git a/src/AsyncUtils.cc b/src/AsyncUtils.cc new file mode 100644 index 00000000..12171e8d --- /dev/null +++ b/src/AsyncUtils.cc @@ -0,0 +1,160 @@ +#include "AsyncUtils.hh" + +#include +#include +#include +#include +#include +#include + +using namespace std; + +AsyncEvent::AsyncEvent(asio::any_io_executor ex) + : executor(ex), is_set(false) {} + +void AsyncEvent::set() { + lock_guard g(this->lock); + this->is_set = true; + for (auto& waiter : this->waiters) { + asio::post(this->executor, + [handler = std::move(waiter)]() mutable { + (*handler)(); + }); + } + this->waiters.clear(); +} + +void AsyncEvent::clear() { + lock_guard g(this->lock); + this->is_set = false; +} + +asio::awaitable AsyncEvent::wait() { + auto token = asio::use_awaitable_t<>{}; + co_await asio::async_initiate, void()>( + [this](auto&& handler) -> void { + lock_guard g(this->lock); + if (this->is_set) { + handler(); + } else { + this->waiters.emplace_back(make_unique>(std::move(handler))); + } + }, + token); +} + +AsyncSocketReader::AsyncSocketReader(asio::ip::tcp::socket&& sock) + : sock(std::move(sock)) {} + +asio::awaitable AsyncSocketReader::read_line(const char* delimiter, size_t max_length) { + size_t delimiter_size = strlen(delimiter); + if (delimiter_size == 0) { + throw logic_error("delimiter is empty"); + } + size_t delimiter_backup_bytes = delimiter_size - 1; + + size_t delimiter_pos = this->pending_data.find(delimiter); + while ((delimiter_pos == string::npos) && (!max_length || (this->pending_data.size() < max_length))) { + size_t pre_size = this->pending_data.size(); + this->pending_data.resize(min(max_length, this->pending_data.size() + 0x400)); + + auto buf = asio::buffer(this->pending_data.data() + pre_size, this->pending_data.size() - pre_size); + size_t bytes_read = co_await this->sock.async_read_some(buf, asio::use_awaitable); + this->pending_data.resize(pre_size + bytes_read); + delimiter_pos = this->pending_data.find( + delimiter, + (delimiter_backup_bytes > pre_size) ? 0 : (pre_size - delimiter_backup_bytes)); + } + + if (delimiter_pos == string::npos) { + throw runtime_error("line exceeds max length"); + } + + // TODO: It's not great that we copy the data here. There's probably a more + // idiomatic and efficient way to do this. + string ret = this->pending_data.substr(0, delimiter_pos); + this->pending_data = this->pending_data.substr(delimiter_pos + delimiter_size); + co_return ret; +} + +asio::awaitable AsyncSocketReader::read_data(size_t size) { + string ret; + if (this->pending_data.size() == size) { + this->pending_data.swap(ret); + } else if (this->pending_data.size() > size) { + ret = this->pending_data.substr(0, size); + this->pending_data = this->pending_data.substr(size); + } else { + size_t bytes_to_read = size - this->pending_data.size(); + this->pending_data.swap(ret); + ret.resize(size); + co_await asio::async_read(this->sock, asio::buffer(ret.data() + size - bytes_to_read, bytes_to_read), asio::use_awaitable); + } + co_return ret; +} + +asio::awaitable AsyncSocketReader::read_data_into(void* data, size_t size) { + if (this->pending_data.size() == size) { + memcpy(data, this->pending_data.data(), size); + this->pending_data.clear(); + } else if (this->pending_data.size() > size) { + memcpy(data, this->pending_data.data(), size); + this->pending_data = this->pending_data.substr(size); + } else { + memcpy(data, this->pending_data.data(), this->pending_data.size()); + size_t bytes_to_read = size - this->pending_data.size(); + this->pending_data.clear(); + void* read_buf = reinterpret_cast(data) + size - bytes_to_read; + co_await asio::async_read(this->sock, asio::buffer(read_buf, bytes_to_read), asio::use_awaitable); + } +} + +void AsyncWriteCollector::add(string&& data) { + const auto& item = this->owned_data.emplace_back(std::move(data)); + bufs.emplace_back(asio::buffer(item.data(), item.size())); +} + +void AsyncWriteCollector::add_reference(const void* data, size_t size) { + bufs.emplace_back(asio::buffer(data, size)); +} + +asio::awaitable AsyncWriteCollector::write(asio::ip::tcp::socket& sock) { + deque local_owned_data; + local_owned_data.swap(this->owned_data); + vector local_bufs; + local_bufs.swap(this->bufs); + co_await asio::async_write(sock, local_bufs, asio::use_awaitable); +} + +asio::awaitable async_sleep(chrono::steady_clock::duration duration) { + asio::steady_timer timer(co_await asio::this_coro::executor, duration); + co_await timer.async_wait(asio::use_awaitable); +} + +asio::awaitable async_connect_tcp(uint32_t ipv4_addr, uint16_t port) { + uint8_t octets[4] = { + static_cast(ipv4_addr >> 24), + static_cast(ipv4_addr >> 16), + static_cast(ipv4_addr >> 8), + static_cast(ipv4_addr)}; + return async_connect_tcp(std::format("{}.{}.{}.{}", octets[0], octets[1], octets[2], octets[3]), port); +} + +asio::awaitable async_connect_tcp(const std::string& host, uint16_t port) { + auto executor = co_await asio::this_coro::executor; + + asio::ip::tcp::resolver resolver(executor); + auto endpoints = co_await resolver.async_resolve(host, std::format("{}", port), asio::use_awaitable); + + asio::ip::tcp::socket sock(executor); + co_await asio::async_connect(sock, endpoints, asio::use_awaitable); + + co_return sock; +} + +asio::awaitable async_connect_tcp(const asio::ip::tcp::endpoint& ep) { + auto executor = co_await asio::this_coro::executor; + asio::ip::tcp::socket sock(executor); + co_await sock.async_connect(ep, asio::use_awaitable); + co_return sock; +} diff --git a/src/AsyncUtils.hh b/src/AsyncUtils.hh new file mode 100644 index 00000000..53680283 --- /dev/null +++ b/src/AsyncUtils.hh @@ -0,0 +1,252 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include + +template +class AsyncPromise { +public: + AsyncPromise() = default; + + asio::awaitable get() { + if (!this->exc && !this->val.has_value()) { + auto executor = co_await asio::this_coro::executor; + co_await asio::async_initiate( + [this, &executor](auto&& new_handler) { + this->resolver_ref.emplace(ResolverRef{.resolve = std::move(new_handler), .executor = &executor}); + }, + asio::use_awaitable); + } + + if (this->exc) { + std::rethrow_exception(this->exc); + } else if (this->val.has_value()) { + co_return *this->val; + } else { + throw std::logic_error("AsyncPromise await resolved but did not have a value or exception"); + } + } + + void set_value(T&& result) { + if (this->exc || this->val.has_value()) { + throw std::logic_error("attempted to set value on completed promise"); + } + this->val = result; + this->resolve(); + } + + void set_exception(std::exception_ptr ex) { + if (this->exc || this->val.has_value()) { + throw std::logic_error("attempted to set value on completed promise"); + } + this->exc = ex; + this->resolve(); + } + + void cancel() { + this->set_exception(std::make_exception_ptr(std::runtime_error("AsyncPromise cancelled"))); + } + + bool done() const { + return this->exc || this->val.has_value(); + } + +private: + struct ResolverRef { + asio::detail::awaitable_handler resolve; + asio::any_io_executor* executor; + }; + std::optional val; + std::exception_ptr exc; + std::optional resolver_ref; + + void resolve() { + if (this->resolver_ref.has_value()) { + auto* executor = this->resolver_ref->executor; + asio::post(*executor, [ref = std::move(this->resolver_ref)]() mutable -> void { + ref->resolve(std::error_code{}); + }); + this->resolver_ref.reset(); + } + } +}; + +template <> +class AsyncPromise { +public: + AsyncPromise() = default; + + asio::awaitable get() { + if (!this->exc && !this->returned) { + auto executor = co_await asio::this_coro::executor; + co_await asio::async_initiate( + [this, &executor](auto&& new_handler) { + this->resolver_ref.emplace(ResolverRef{.resolve = std::move(new_handler), .executor = &executor}); + }, + asio::use_awaitable); + } + + if (this->exc) { + std::rethrow_exception(this->exc); + } else if (this->returned) { + co_return; + } else { + throw std::logic_error("AsyncPromise await resolved but did not have a value or exception"); + } + } + + void set_value() { + if (this->exc || this->returned) { + throw std::logic_error("attempted to set value on completed promise"); + } + this->returned = true; + this->resolve(); + } + + void set_exception(std::exception_ptr ex) { + if (this->exc || this->returned) { + throw std::logic_error("attempted to set value on completed promise"); + } + this->exc = ex; + this->resolve(); + } + + void cancel() { + this->set_exception(std::make_exception_ptr(std::runtime_error("AsyncPromise cancelled"))); + } + + bool done() const { + return this->exc || this->returned; + } + +private: + struct ResolverRef { + asio::detail::awaitable_handler resolve; + asio::any_io_executor* executor; + }; + bool returned; + std::exception_ptr exc; + std::optional resolver_ref; + + void resolve() { + if (this->resolver_ref.has_value()) { + auto* executor = this->resolver_ref->executor; + asio::post(*executor, [ref = std::move(this->resolver_ref)]() mutable -> void { + ref->resolve(std::error_code{}); + }); + this->resolver_ref.reset(); + } + } +}; + +class AsyncEvent { +public: + AsyncEvent(asio::any_io_executor ex); + AsyncEvent(const AsyncEvent&) = delete; + AsyncEvent(AsyncEvent&&) = delete; + AsyncEvent& operator=(const AsyncEvent&) = delete; + AsyncEvent& operator=(AsyncEvent&&) = delete; + + void set(); + void clear(); + asio::awaitable wait(); + +private: + asio::any_io_executor executor; + bool is_set; + std::mutex lock; + std::vector>> waiters; +}; + +class AsyncSocketReader { +public: + explicit AsyncSocketReader(asio::ip::tcp::socket&& sock); + AsyncSocketReader(const AsyncSocketReader&) = delete; + AsyncSocketReader(AsyncSocketReader&&) = delete; + AsyncSocketReader& operator=(const AsyncSocketReader&) = delete; + AsyncSocketReader& operator=(AsyncSocketReader&&) = delete; + ~AsyncSocketReader() = default; + + // Reads one line from the socket, buffering any extra data read. The + // delimiter is not included in the returned line. max_length = 0 means no + // maximum length is enforced. + asio::awaitable read_line( + const char* delimiter = "\n", size_t max_length = 0); + asio::awaitable read_data(size_t size); + asio::awaitable read_data_into(void* data, size_t size); + + // The caller cannot know what the socket's read state is, so this should + // only be used when the caller intends to write to the socket, not read + inline asio::ip::tcp::socket& get_socket() { + return this->sock; + } + + inline void close() { + this->sock.close(); + } + +private: + std::string pending_data; // Data read but not yet returned to the caller + asio::ip::tcp::socket sock; +}; + +class AsyncWriteCollector { +public: + AsyncWriteCollector() = default; + AsyncWriteCollector(const AsyncWriteCollector&) = delete; + AsyncWriteCollector(AsyncWriteCollector&&) = delete; + AsyncWriteCollector& operator=(const AsyncWriteCollector&) = delete; + AsyncWriteCollector& operator=(AsyncWriteCollector&&) = delete; + ~AsyncWriteCollector() = default; + + void add(std::string&& data); + + // When using add_reference, it is the caller's responsibility to ensure that + // the buffer is valid until *this is destroyed or write() returns. + void add_reference(const void* data, size_t size); + + asio::awaitable write(asio::ip::tcp::socket& sock); + +private: + std::deque owned_data; + std::vector bufs; +}; + +asio::awaitable async_sleep(std::chrono::steady_clock::duration duration); + +inline asio::ip::tcp::endpoint make_endpoint_ipv4(uint32_t addr, uint16_t port) { + return asio::ip::tcp::endpoint(asio::ip::address_v4(addr), port); +} + +inline std::string str_for_endpoint(const asio::ip::tcp::endpoint& ep) { + return ep.address().to_string() + std::format(":{}", ep.port()); +} + +inline uint32_t ipv4_addr_for_asio_addr(const asio::ip::address& addr) { + if (!addr.is_v4()) { + throw std::runtime_error("Address is not IPv4"); + } + return ntohl(addr.to_v4().to_uint()); +} + +asio::awaitable async_connect_tcp(uint32_t ipv4_addr, uint16_t port); +asio::awaitable async_connect_tcp(const std::string& host, uint16_t port); +asio::awaitable async_connect_tcp(const asio::ip::tcp::endpoint& ep); + +template +asio::awaitable> call_on_thread_pool(asio::thread_pool& pool, FnT&& f, ArgTs&&... args) { + using ReturnT = std::invoke_result_t; + auto bound = std::bind(std::forward(f), std::forward(args)...); + AsyncPromise promise; + + asio::post(pool, [&promise, &bound]() -> void { + promise.set_value(bound()); + }); + co_return co_await promise.get(); +} diff --git a/src/BattleParamsIndex.cc b/src/BattleParamsIndex.cc index 4385e900..b05f8f9c 100644 --- a/src/BattleParamsIndex.cc +++ b/src/BattleParamsIndex.cc @@ -11,25 +11,25 @@ using namespace std; void BattleParamsIndex::Table::print(FILE* stream) const { auto print_entry = +[](FILE* stream, const PlayerStats& e) { - fprintf(stream, - "%5hu %5hu %5hu %5hu %5hu %5hu %5hu %5hu %5" PRIu32 " %5" PRIu32, - e.char_stats.atp.load(), - e.char_stats.mst.load(), - e.char_stats.evp.load(), - e.char_stats.hp.load(), - e.char_stats.dfp.load(), - e.char_stats.ata.load(), - e.char_stats.lck.load(), - e.esp.load(), - e.experience.load(), - e.meseta.load()); + phosg::fwrite_fmt(stream, + "{:5} {:5} {:5} {:5} {:5} {:5} {:5} {:5} {:5} {:5}", + e.char_stats.atp, + e.char_stats.mst, + e.char_stats.evp, + e.char_stats.hp, + e.char_stats.dfp, + e.char_stats.ata, + e.char_stats.lck, + e.esp, + e.experience, + e.meseta); }; for (size_t diff = 0; diff < 4; diff++) { - fprintf(stream, "%c ZZ ATP PSV EVP HP DFP ATA LCK ESP EXP DIFF\n", + phosg::fwrite_fmt(stream, "{} ZZ ATP PSV EVP HP DFP ATA LCK ESP EXP DIFF\n", abbreviation_for_difficulty(diff)); for (size_t z = 0; z < 0x60; z++) { - fprintf(stream, " %02zX ", z); + phosg::fwrite_fmt(stream, " {:02X} ", z); print_entry(stream, this->stats[diff][z]); fputc('\n', stream); } @@ -54,8 +54,8 @@ BattleParamsIndex::BattleParamsIndex( for (uint8_t episode = 0; episode < 3; episode++) { auto& file = this->files[is_solo][episode]; if (file.data->size() < sizeof(Table)) { - throw runtime_error(phosg::string_printf( - "battle params table size is incorrect (expected %zX bytes, have %zX bytes; is_solo=%hhu, episode=%hhu)", + throw runtime_error(std::format( + "battle params table size is incorrect (expected {:X} bytes, have {:X} bytes; is_solo={}, episode={})", sizeof(Table), file.data->size(), is_solo, episode)); } file.table = reinterpret_cast(file.data->data()); diff --git a/src/CatSession.cc b/src/CatSession.cc deleted file mode 100644 index a8e7cf23..00000000 --- a/src/CatSession.cc +++ /dev/null @@ -1,187 +0,0 @@ -#include "CatSession.hh" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -#include "Loggers.hh" -#include "PSOProtocol.hh" -#include "ProxyCommands.hh" -#include "ReceiveCommands.hh" -#include "ReceiveSubcommands.hh" -#include "SendCommands.hh" - -using namespace std; - -CatSession::exit_shell::exit_shell() : runtime_error("shell exited") {} - -CatSession::CatSession( - shared_ptr base, - const struct sockaddr_storage& remote, - Version version, - shared_ptr bb_key_file) - : log(phosg::string_printf("[CatSession:%s] ", phosg::name_for_enum(version)), proxy_server_log.min_level), - base(base), - read_event(event_new(this->base.get(), 0, EV_READ | EV_PERSIST, CatSession::dispatch_read_stdin, this), event_free), - channel(version, 1, CatSession::dispatch_on_channel_input, CatSession::dispatch_on_channel_error, this, "CatSession"), - bb_key_file(bb_key_file) { - - if (remote.ss_family != AF_INET) { - throw runtime_error("remote is not AF_INET"); - } - - string netloc_str = phosg::render_sockaddr_storage(remote); - this->log.info("Connecting to %s", netloc_str.c_str()); - - struct bufferevent* bev = bufferevent_socket_new( - this->base.get(), -1, BEV_OPT_CLOSE_ON_FREE | BEV_OPT_DEFER_CALLBACKS); - if (!bev) { - throw runtime_error(phosg::string_printf("failed to open socket (%d)", EVUTIL_SOCKET_ERROR())); - } - this->channel.set_bufferevent(bev, 0); - - if (bufferevent_socket_connect(this->channel.bev.get(), - reinterpret_cast(&remote), sizeof(struct sockaddr_in)) != 0) { - throw runtime_error(phosg::string_printf("failed to connect (%d)", EVUTIL_SOCKET_ERROR())); - } - - event_add(this->read_event.get(), nullptr); - this->poll.add(0, POLLIN); -} - -void CatSession::execute_command(const std::string& command) { - string full_cmd = phosg::parse_data_string(command, nullptr, phosg::ParseDataFlags::ALLOW_FILES); - send_command_with_header(this->channel, full_cmd.data(), full_cmd.size()); -} - -void CatSession::dispatch_on_channel_input( - Channel& ch, uint16_t command, uint32_t flag, std::string& data) { - auto* session = reinterpret_cast(ch.context_obj); - session->on_channel_input(command, flag, data); -} - -void CatSession::on_channel_input( - uint16_t command, uint32_t flag, std::string& data) { - if (!uses_v4_encryption(this->channel.version)) { - if (command == 0x02 || command == 0x17 || command == 0x91 || command == 0x9B) { - const auto& cmd = check_size_t(data, 0xFFFF); - if (uses_v3_encryption(this->channel.version)) { - this->channel.crypt_in = make_shared(cmd.server_key); - this->channel.crypt_out = make_shared(cmd.client_key); - this->log.info("Enabled V3 encryption (server key %08" PRIX32 ", client key %08" PRIX32 ")", - cmd.server_key.load(), cmd.client_key.load()); - } else { // PC, DC, or patch server - this->channel.crypt_in = make_shared(cmd.server_key); - this->channel.crypt_out = make_shared(cmd.client_key); - this->log.info("Enabled V2 encryption (server key %08" PRIX32 ", client key %08" PRIX32 ")", - cmd.server_key.load(), cmd.client_key.load()); - } - } - } else { // BB - if (command == 0x03 || command == 0x9B) { - if (!this->bb_key_file) { - throw runtime_error("BB encryption requires a key file"); - } - const auto& cmd = check_size_t(data, 0xFFFF); - this->channel.crypt_in = make_shared(*this->bb_key_file, &cmd.server_key[0], sizeof(cmd.server_key)); - this->channel.crypt_out = make_shared(*this->bb_key_file, &cmd.client_key[0], sizeof(cmd.client_key)); - this->log.info("Enabled BB encryption"); - } - } - - // TODO: Use the iovec form of print_data here instead of - // prepend_command_header (which copies the string) - string full_cmd = prepend_command_header(this->channel.version, this->channel.crypt_in.get(), command, flag, data); - phosg::print_data(stdout, full_cmd, 0, nullptr, phosg::PrintDataFlags::PRINT_ASCII | phosg::PrintDataFlags::OFFSET_16_BITS); -} - -void CatSession::dispatch_on_channel_error(Channel& ch, short events) { - auto* session = reinterpret_cast(ch.context_obj); - session->on_channel_error(events); -} - -void CatSession::on_channel_error(short events) { - if (events & BEV_EVENT_CONNECTED) { - this->log.info("Channel connected"); - } - if (events & BEV_EVENT_ERROR) { - int err = EVUTIL_SOCKET_ERROR(); - this->log.warning("Error %d (%s) in unlinked client stream", err, - evutil_socket_error_to_string(err)); - } - if (events & (BEV_EVENT_ERROR | BEV_EVENT_EOF)) { - this->log.info("Session endpoint has disconnected"); - this->channel.disconnect(); - event_base_loopexit(this->base.get(), nullptr); - } -} - -void CatSession::dispatch_read_stdin(evutil_socket_t, short, void* ctx) { - reinterpret_cast(ctx)->read_stdin(); -} - -void CatSession::read_stdin() { - bool any_command_read = false; - for (;;) { - auto poll_result = this->poll.poll(); - short fd_events = 0; - try { - fd_events = poll_result.at(0); - } catch (const out_of_range&) { - } - - if (!(fd_events & POLLIN)) { - break; - } - - string command(2048, '\0'); - if (!fgets(command.data(), command.size(), stdin)) { - if (!any_command_read) { - // ctrl+d probably; we should exit - fputc('\n', stderr); - event_base_loopexit(this->base.get(), nullptr); - return; - } else { - break; // probably not EOF; just no more commands for now - } - } - - // trim the extra data off the string - size_t len = strlen(command.c_str()); - if (len == 0) { - break; - } - if (command[len - 1] == '\n') { - len--; - } - command.resize(len); - any_command_read = true; - - try { - execute_command(command); - } catch (const exit_shell&) { - event_base_loopexit(this->base.get(), nullptr); - return; - } catch (const exception& e) { - fprintf(stderr, "FAILED: %s\n", e.what()); - } - } -} diff --git a/src/CatSession.hh b/src/CatSession.hh deleted file mode 100644 index 6cd664c0..00000000 --- a/src/CatSession.hh +++ /dev/null @@ -1,54 +0,0 @@ -#pragma once - -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -#include "PSOEncryption.hh" -#include "PSOProtocol.hh" -#include "ServerState.hh" - -class CatSession { -public: - CatSession( - std::shared_ptr base, - const struct sockaddr_storage& remote, - Version version, - std::shared_ptr bb_key_file); - CatSession(const CatSession&) = delete; - CatSession(CatSession&&) = delete; - CatSession& operator=(const CatSession&) = delete; - CatSession& operator=(CatSession&&) = delete; - virtual ~CatSession() = default; - -protected: - phosg::PrefixedLogger log; - std::shared_ptr base; - std::unique_ptr read_event; - phosg::Poll poll; - - Channel channel; - std::shared_ptr bb_key_file; - - class exit_shell : public std::runtime_error { - public: - exit_shell(); - ~exit_shell() = default; - }; - - virtual void execute_command(const std::string& command); - - static void dispatch_read_stdin(evutil_socket_t fd, short events, void* ctx); - static void dispatch_on_channel_input(Channel& ch, uint16_t command, uint32_t flag, std::string& msg); - static void dispatch_on_channel_error(Channel& ch, short events); - void on_channel_input(uint16_t command, uint32_t flag, std::string& msg); - void on_channel_error(short events); - void read_stdin(); -}; diff --git a/src/Channel.cc b/src/Channel.cc index ea923f6d..ee414193 100644 --- a/src/Channel.cc +++ b/src/Channel.cc @@ -1,9 +1,6 @@ #include "Channel.hh" #include -#include -#include -#include #include #include @@ -17,230 +14,17 @@ using namespace std; extern bool use_terminal_colors; -static void flush_and_free_bufferevent(struct bufferevent* bev) { - bufferevent_flush(bev, EV_READ | EV_WRITE, BEV_FINISHED); - bufferevent_free(bev); -} - Channel::Channel( Version version, uint8_t language, - on_command_received_t on_command_received, - on_error_t on_error, - void* context_obj, const string& name, phosg::TerminalFormat terminal_send_color, phosg::TerminalFormat terminal_recv_color) - : bev(nullptr, flush_and_free_bufferevent), - virtual_network_id(0), - version(version), + : version(version), language(language), name(name), terminal_send_color(terminal_send_color), - terminal_recv_color(terminal_recv_color), - on_command_received(on_command_received), - on_error(on_error), - context_obj(context_obj) { -} - -Channel::Channel( - struct bufferevent* bev, - uint64_t virtual_network_id, - Version version, - uint8_t language, - on_command_received_t on_command_received, - on_error_t on_error, - void* context_obj, - const string& name, - phosg::TerminalFormat terminal_send_color, - phosg::TerminalFormat terminal_recv_color) - : bev(nullptr, flush_and_free_bufferevent), - version(version), - language(language), - name(name), - terminal_send_color(terminal_send_color), - terminal_recv_color(terminal_recv_color), - on_command_received(on_command_received), - on_error(on_error), - context_obj(context_obj) { - this->set_bufferevent(bev, virtual_network_id); -} - -void Channel::replace_with( - Channel&& other, - on_command_received_t on_command_received, - on_error_t on_error, - void* context_obj, - const std::string& name) { - this->set_bufferevent(other.bev.release(), other.virtual_network_id); - this->local_addr = other.local_addr; - this->remote_addr = other.remote_addr; - this->version = other.version; - this->language = other.language; - this->crypt_in = other.crypt_in; - this->crypt_out = other.crypt_out; - this->name = name; - this->terminal_send_color = other.terminal_send_color; - this->terminal_recv_color = other.terminal_recv_color; - this->on_command_received = on_command_received; - this->on_error = on_error; - this->context_obj = context_obj; - other.disconnect(); // Clears crypts, addrs, etc. -} - -void Channel::set_bufferevent(struct bufferevent* bev, uint64_t virtual_network_id) { - this->bev.reset(bev); - this->virtual_network_id = virtual_network_id; - - if (this->bev.get()) { - int fd = bufferevent_getfd(this->bev.get()); - if (fd < 0) { - memset(&this->local_addr, 0, sizeof(this->local_addr)); - memset(&this->remote_addr, 0, sizeof(this->remote_addr)); - } else { - phosg::get_socket_addresses(fd, &this->local_addr, &this->remote_addr); - } - - bufferevent_setcb(this->bev.get(), &Channel::dispatch_on_input, nullptr, &Channel::dispatch_on_error, this); - bufferevent_enable(this->bev.get(), EV_READ | EV_WRITE); - - } else { - memset(&this->local_addr, 0, sizeof(this->local_addr)); - memset(&this->remote_addr, 0, sizeof(this->remote_addr)); - } -} - -void Channel::disconnect() { - if (this->bev.get()) { - // If the output buffer is not empty, move the bufferevent into the draining - // pool instead of disconnecting it, to make sure all the data gets sent. - struct evbuffer* out_buffer = bufferevent_get_output(this->bev.get()); - if (evbuffer_get_length(out_buffer) == 0) { - this->bev.reset(); // Destructor flushes and frees the bufferevent - } else { - // The callbacks will free it when all the data is sent or the client - // disconnects - - auto on_output = +[](struct bufferevent* bev, void*) -> void { - flush_and_free_bufferevent(bev); - }; - - auto on_error = +[](struct bufferevent* bev, short events, void*) -> void { - if (events & BEV_EVENT_ERROR) { - int err = EVUTIL_SOCKET_ERROR(); - channel_exceptions_log.warning( - "Disconnecting channel caused error %d (%s)", err, - evutil_socket_error_to_string(err)); - } - if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) { - bufferevent_flush(bev, EV_WRITE, BEV_FINISHED); - bufferevent_free(bev); - } - }; - - struct bufferevent* bev = this->bev.release(); - bufferevent_setcb(bev, nullptr, on_output, on_error, bev); - bufferevent_disable(bev, EV_READ); - } - } - - memset(&this->local_addr, 0, sizeof(this->local_addr)); - memset(&this->remote_addr, 0, sizeof(this->remote_addr)); - this->virtual_network_id = false; - this->crypt_in.reset(); - this->crypt_out.reset(); -} - -Channel::Message Channel::recv() { - struct evbuffer* buf = bufferevent_get_input(this->bev.get()); - - size_t header_size = (this->version == Version::BB_V4) ? 8 : 4; - PSOCommandHeader header; - if (evbuffer_copyout(buf, &header, header_size) < static_cast(header_size)) { - throw out_of_range("no command available"); - } - - if (this->crypt_in.get()) { - this->crypt_in->decrypt(&header, header_size, false); - } - - size_t command_logical_size = header.size(version); - - // If encryption is enabled, BB pads commands to 8-byte boundaries, and this - // is not reflected in the size field. This logic does not occur if encryption - // is not yet enabled. - size_t command_physical_size = (this->crypt_in.get() && (version == Version::BB_V4)) - ? ((command_logical_size + 7) & ~7) - : command_logical_size; - if (evbuffer_get_length(buf) < command_physical_size) { - throw out_of_range("no command available"); - } - - // If we get here, then there is a full command in the buffer. Some encryption - // algorithms' advancement depends on the decrypted data, so we have to - // actually decrypt the header again (with advance=true) to keep them in a - // consistent state. - - string header_data(header_size, '\0'); - if (evbuffer_remove(buf, header_data.data(), header_data.size()) < static_cast(header_data.size())) { - throw logic_error("enough bytes available, but could not remove them"); - } - if (this->crypt_in.get()) { - this->crypt_in->decrypt(header_data.data(), header_data.size()); - } - - string command_data(command_physical_size - header_size, '\0'); - if (evbuffer_remove(buf, command_data.data(), command_data.size()) < static_cast(command_data.size())) { - throw logic_error("enough bytes available, but could not remove them"); - } - - if (this->crypt_in.get()) { - // Some versions of PSO DC can send commands whose sizes are not a multiple - // of 4, but the server is expected to always use a multiple of 4 bytes when - // decrypting (the extra cipher bytes are lost). To emulate this behavior, - // we have to round up the size for DC commands here. - size_t orig_size = command_data.size(); - command_data.resize((orig_size + 3) & (~3), 0); - this->crypt_in->decrypt(command_data.data(), command_data.size()); - command_data.resize(orig_size); - } - command_data.resize(command_logical_size - header_size); - - if (command_data_log.should_log(phosg::LogLevel::INFO) && (this->terminal_recv_color != phosg::TerminalFormat::END)) { - if (use_terminal_colors && this->terminal_recv_color != phosg::TerminalFormat::NORMAL) { - print_color_escape(stderr, this->terminal_recv_color, phosg::TerminalFormat::BOLD, phosg::TerminalFormat::END); - } - - if (version == Version::BB_V4) { - command_data_log.info( - "Received from %s (version=BB command=%04hX flag=%08" PRIX32 ")", - this->name.c_str(), - header.command(this->version), - header.flag(this->version)); - } else { - command_data_log.info( - "Received from %s (version=%s command=%02hX flag=%02" PRIX32 ")", - this->name.c_str(), - phosg::name_for_enum(this->version), - header.command(this->version), - header.flag(this->version)); - } - - vector iovs; - iovs.emplace_back(iovec{.iov_base = header_data.data(), .iov_len = header_data.size()}); - iovs.emplace_back(iovec{.iov_base = command_data.data(), .iov_len = command_data.size()}); - phosg::print_data(stderr, iovs, 0, nullptr, phosg::PrintDataFlags::PRINT_ASCII | phosg::PrintDataFlags::DISABLE_COLOR | phosg::PrintDataFlags::OFFSET_16_BITS); - - if (use_terminal_colors && this->terminal_recv_color != phosg::TerminalFormat::NORMAL) { - phosg::print_color_escape(stderr, phosg::TerminalFormat::NORMAL, phosg::TerminalFormat::END); - } - } - - return { - .command = header.command(this->version), - .flag = header.flag(this->version), - .data = std::move(command_data), - }; + terminal_recv_color(terminal_recv_color) { } void Channel::send(uint16_t cmd, uint32_t flag, bool silent) { @@ -249,7 +33,7 @@ void Channel::send(uint16_t cmd, uint32_t flag, bool silent) { void Channel::send(uint16_t cmd, uint32_t flag, const std::vector> blocks, bool silent) { if (!this->connected()) { - channel_exceptions_log.warning("Attempted to send command on closed channel; dropping data"); + channel_exceptions_log.warning_f("Attempted to send command on closed channel; dropping data"); return; } @@ -342,16 +126,16 @@ void Channel::send(uint16_t cmd, uint32_t flag, const std::vectorterminal_send_color != phosg::TerminalFormat::END)) { + if (!silent && (command_data_log.should_log(phosg::LogLevel::L_INFO)) && (this->terminal_send_color != phosg::TerminalFormat::END)) { if (use_terminal_colors && this->terminal_send_color != phosg::TerminalFormat::NORMAL) { print_color_escape(stderr, phosg::TerminalFormat::FG_YELLOW, phosg::TerminalFormat::BOLD, phosg::TerminalFormat::END); } if (version == Version::BB_V4) { - command_data_log.info("Sending to %s (version=BB command=%04hX flag=%08" PRIX32 ")", - this->name.c_str(), cmd, flag); + command_data_log.info_f("Sending to {} (version=BB command={:04X} flag={:08X})", + this->name, cmd, flag); } else { - command_data_log.info("Sending to %s (version=%s command=%02hX flag=%02" PRIX32 ")", - this->name.c_str(), phosg::name_for_enum(version), cmd, flag); + command_data_log.info_f("Sending to {} (version={} command={:02X} flag={:02X})", + this->name, phosg::name_for_enum(version), cmd, flag); } phosg::print_data(stderr, send_data.data(), logical_size, 0, nullptr, phosg::PrintDataFlags::PRINT_ASCII | phosg::PrintDataFlags::DISABLE_COLOR | phosg::PrintDataFlags::OFFSET_16_BITS); if (use_terminal_colors && this->terminal_send_color != phosg::TerminalFormat::NORMAL) { @@ -363,8 +147,7 @@ void Channel::send(uint16_t cmd, uint32_t flag, const std::vectorcrypt_out->encrypt(send_data.data(), send_data.size()); } - struct evbuffer* buf = bufferevent_get_output(this->bev.get()); - evbuffer_add(buf, send_data.data(), send_data.size()); + this->send_raw(std::move(send_data)); } void Channel::send(uint16_t cmd, uint32_t flag, const void* data, size_t size, bool silent) { @@ -387,35 +170,229 @@ void Channel::send(const void* data, size_t size, bool silent) { } void Channel::send(const string& data, bool silent) { - return this->send(data.data(), data.size(), silent); + this->send(data.data(), data.size(), silent); } -void Channel::dispatch_on_input(struct bufferevent*, void* ctx) { - Channel* ch = reinterpret_cast(ctx); - // The client can be disconnected during on_command_received, so we have to - // make sure ch->bev is valid every time before calling recv() - while (ch->bev.get()) { - Message msg; - try { - msg = ch->recv(); - } catch (const out_of_range&) { - break; - } catch (const exception& e) { - channel_exceptions_log.warning("Error receiving on channel: %s", e.what()); - ch->on_error(*ch, BEV_EVENT_ERROR); - break; +asio::awaitable Channel::recv() { + size_t header_size = (this->version == Version::BB_V4) ? 8 : 4; + PSOCommandHeader header; + co_await this->recv_raw(&header, header_size); + if (this->crypt_in.get()) { + this->crypt_in->decrypt(&header, header_size); + } + + size_t command_logical_size = header.size(version); + if (command_logical_size < header_size) { + throw runtime_error("header size field is smaller than header"); + } + + // If encryption is enabled, BB pads commands to 8-byte boundaries, and this + // is not reflected in the size field. This logic does not occur if encryption + // is not yet enabled. + size_t command_physical_size = (this->crypt_in.get() && (version == Version::BB_V4)) + ? ((command_logical_size + 7) & ~7) + : command_logical_size; + + string command_data(command_physical_size - header_size, '\0'); + co_await this->recv_raw(command_data.data(), command_data.size()); + + if (this->crypt_in.get()) { + // Some versions of PSO DC can send commands whose sizes are not a multiple + // of 4, but the server is expected to always use a multiple of 4 bytes when + // decrypting (the extra cipher bytes are lost). To emulate this behavior, + // we have to round up the size for DC commands here. + size_t orig_size = command_data.size(); + command_data.resize((orig_size + 3) & (~3), 0); + this->crypt_in->decrypt(command_data.data(), command_data.size()); + command_data.resize(orig_size); + } + command_data.resize(command_logical_size - header_size); + + if (command_data_log.should_log(phosg::LogLevel::L_INFO) && (this->terminal_recv_color != phosg::TerminalFormat::END)) { + if (use_terminal_colors && this->terminal_recv_color != phosg::TerminalFormat::NORMAL) { + print_color_escape(stderr, this->terminal_recv_color, phosg::TerminalFormat::BOLD, phosg::TerminalFormat::END); } - if (ch->on_command_received) { - ch->on_command_received(*ch, msg.command, msg.flag, msg.data); + + if (version == Version::BB_V4) { + command_data_log.info_f( + "Received from {} (version=BB command={:04X} flag={:08X})", + this->name, + header.command(this->version), + header.flag(this->version)); + } else { + command_data_log.info_f( + "Received from {} (version={} command={:02X} flag={:02X})", + this->name, + phosg::name_for_enum(this->version), + header.command(this->version), + header.flag(this->version)); + } + + vector iovs; + iovs.emplace_back(iovec{.iov_base = &header, .iov_len = header_size}); + iovs.emplace_back(iovec{.iov_base = command_data.data(), .iov_len = command_data.size()}); + phosg::print_data(stderr, iovs, 0, nullptr, phosg::PrintDataFlags::PRINT_ASCII | phosg::PrintDataFlags::DISABLE_COLOR | phosg::PrintDataFlags::OFFSET_16_BITS); + + if (use_terminal_colors && this->terminal_recv_color != phosg::TerminalFormat::NORMAL) { + phosg::print_color_escape(stderr, phosg::TerminalFormat::NORMAL, phosg::TerminalFormat::END); + } + } + + co_return Message{ + .command = header.command(this->version), + .flag = header.flag(this->version), + .data = std::move(command_data), + }; +} + +shared_ptr SocketChannel::create( + std::shared_ptr io_context, + std::unique_ptr&& sock, + Version version, + uint8_t language, + const string& name, + phosg::TerminalFormat terminal_send_color, + phosg::TerminalFormat terminal_recv_color) { + shared_ptr ret(new SocketChannel( + io_context, std::move(sock), version, language, name, terminal_send_color, terminal_recv_color)); + asio::co_spawn(*io_context, ret->send_task(), asio::detached); + return ret; +} + +SocketChannel::SocketChannel( + std::shared_ptr io_context, + std::unique_ptr&& sock, + Version version, + uint8_t language, + const string& name, + phosg::TerminalFormat terminal_send_color, + phosg::TerminalFormat terminal_recv_color) + : Channel(version, language, name, terminal_send_color, terminal_recv_color), + sock(std::move(sock)), + local_addr(this->sock->local_endpoint()), + remote_addr(this->sock->remote_endpoint()), + send_buffer_nonempty_signal(io_context->get_executor()) {} + +std::string SocketChannel::default_name() const { + return "ip:" + str_for_endpoint(this->remote_addr); +} + +bool SocketChannel::connected() const { + return !this->should_disconnect && this->sock && this->sock->is_open(); +} + +void SocketChannel::disconnect() { + this->should_disconnect = true; + this->send_buffer_nonempty_signal.set(); +} + +void SocketChannel::send_raw(string&& data) { + if (this->sock && !this->should_disconnect) { + this->outbound_data.emplace_back(std::move(data)); + this->send_buffer_nonempty_signal.set(); + } +} + +asio::awaitable SocketChannel::recv_raw(void* data, size_t size) { + if (!this->sock || this->should_disconnect) { + throw runtime_error("Cannot receive on closed channel"); + } + co_await asio::async_read(*this->sock, asio::buffer(data, size), asio::use_awaitable); +} + +asio::awaitable SocketChannel::send_task() { + // Ensure *this doesn't get deleted while the socket is open + auto this_sh = this->shared_from_this(); + + while (this->sock->is_open()) { + deque to_send; + to_send.swap(this->outbound_data); + + if (!to_send.empty()) { + vector bufs; + bufs.reserve(to_send.size()); + for (const auto& it : to_send) { + bufs.emplace_back(asio::buffer(it.data(), it.size())); + } + co_await asio::async_write(*this->sock, bufs, asio::use_awaitable); + } + + if (this->outbound_data.empty()) { + if (this->should_disconnect) { + this->sock->close(); + } else { + this->send_buffer_nonempty_signal.clear(); + co_await this->send_buffer_nonempty_signal.wait(); + } } } } -void Channel::dispatch_on_error(struct bufferevent*, short events, void* ctx) { - Channel* ch = reinterpret_cast(ctx); - if (ch->on_error) { - ch->on_error(*ch, events); - } else { - ch->disconnect(); +PeerChannel::PeerChannel( + std::shared_ptr io_context, + Version version, + uint8_t language, + const std::string& name, + phosg::TerminalFormat terminal_send_color, + phosg::TerminalFormat terminal_recv_color) + : Channel(version, language, name, terminal_send_color, terminal_recv_color), + send_buffer_nonempty_signal(io_context->get_executor()) {} + +void PeerChannel::link_peers(std::shared_ptr peer1, std::shared_ptr peer2) { + if (peer1->connected() || peer2->connected()) { + throw logic_error("Cannot link already-connected peer channels"); + } + peer1->peer = peer2; + peer2->peer = peer1; +} + +std::string PeerChannel::default_name() const { + return std::format("peer:{}->{}", reinterpret_cast(this), reinterpret_cast(this->peer.lock().get())); +} + +bool PeerChannel::connected() const { + return (!this->inbound_data.empty()) || (this->peer.lock() != nullptr); +} + +void PeerChannel::disconnect() { + auto peer = this->peer.lock(); + if (peer) { + peer->peer.reset(); + peer->send_buffer_nonempty_signal.set(); + } + this->peer.reset(); + this->send_buffer_nonempty_signal.set(); +} + +void PeerChannel::send_raw(string&& data) { + auto peer = this->peer.lock(); + if (peer) { + peer->inbound_data.emplace_back(std::move(data)); + peer->send_buffer_nonempty_signal.set(); + } +} + +asio::awaitable PeerChannel::recv_raw(void* data, size_t size) { + while (size > 0) { + while (this->inbound_data.empty() && this->peer.lock()) { + this->send_buffer_nonempty_signal.clear(); + co_await this->send_buffer_nonempty_signal.wait(); + } + + if (!this->inbound_data.empty()) { + auto& front_block = this->inbound_data.front(); + if (size < front_block.size()) { + memcpy(data, front_block.data(), size); + front_block = front_block.substr(size); + size = 0; + } else { + memcpy(data, front_block.data(), front_block.size()); + size -= front_block.size(); + data = reinterpret_cast(data) + front_block.size(); + this->inbound_data.pop_front(); + } + } else if (!this->peer.lock()) { + throw runtime_error("Channel peer has disconnected"); + } } } diff --git a/src/Channel.hh b/src/Channel.hh index cb539774..6dac0a92 100644 --- a/src/Channel.hh +++ b/src/Channel.hh @@ -1,20 +1,16 @@ #pragma once -#include - +#include #include #include +#include "AsyncUtils.hh" #include "PSOEncryption.hh" #include "PSOProtocol.hh" #include "Version.hh" -struct Channel { - std::unique_ptr bev; - struct sockaddr_storage local_addr; - struct sockaddr_storage remote_addr; - uint64_t virtual_network_id; // 0 = normal TCP connection - +class Channel { +public: Version version; uint8_t language; std::shared_ptr crypt_in; @@ -28,58 +24,46 @@ struct Channel { uint16_t command; uint32_t flag; std::string data; + + template + const T& check_size_t(size_t min_size, size_t max_size) const { + return ::check_size_t(this->data.data(), this->data.size(), min_size, max_size); + } + template + T& check_size_t(size_t min_size, size_t max_size) { + return ::check_size_t(this->data.data(), this->data.size(), min_size, max_size); + } + + template + const T& check_size_t(size_t max_size) const { + return ::check_size_t(this->data.data(), this->data.size(), sizeof(T), max_size); + } + template + T& check_size_t(size_t max_size) { + return ::check_size_t(this->data.data(), this->data.size(), sizeof(T), max_size); + } + + template + const T& check_size_t() const { + return ::check_size_t(this->data.data(), this->data.size(), sizeof(T), sizeof(T)); + } + template + T& check_size_t() { + return ::check_size_t(this->data.data(), this->data.size(), sizeof(T), sizeof(T)); + } }; - typedef void (*on_command_received_t)(Channel&, uint16_t, uint32_t, std::string&); - typedef void (*on_error_t)(Channel&, short); + virtual ~Channel() = default; - on_command_received_t on_command_received; - on_error_t on_error; - void* context_obj; + virtual std::string default_name() const = 0; - // Creates an unconnected channel - Channel( - Version version, - uint8_t language, - on_command_received_t on_command_received, - on_error_t on_error, - void* context_obj, - const std::string& name, - phosg::TerminalFormat terminal_send_color = phosg::TerminalFormat::END, - phosg::TerminalFormat terminal_recv_color = phosg::TerminalFormat::END); - // Creates a connected channel - Channel( - struct bufferevent* bev, - uint64_t virtual_network_id, - Version version, - uint8_t language, - on_command_received_t on_command_received, - on_error_t on_error, - void* context_obj, - const std::string& name = "", - phosg::TerminalFormat terminal_send_color = phosg::TerminalFormat::END, - phosg::TerminalFormat terminal_recv_color = phosg::TerminalFormat::END); - Channel(const Channel& other) = delete; - Channel(Channel&& other) = delete; - Channel& operator=(const Channel& other) = delete; - Channel& operator=(Channel&& other) = delete; + // Returns whether the channel is connected or not. + virtual bool connected() const = 0; - void replace_with( - Channel&& other, - on_command_received_t on_command_received, - on_error_t on_error, - void* context_obj, - const std::string& name = ""); - - void set_bufferevent(struct bufferevent* bev, uint64_t virtual_network_id); - - inline bool connected() const { - return this->bev.get() != nullptr; - } - void disconnect(); - - // Receives a message. Throws std::out_of_range if no messages are available. - Message recv(); + // Disconnects the channel. Any pending data will still be sent before the + // underlying transport (e.g. socket) is closed, but further send calls will + // do nothing. + virtual void disconnect() = 0; // Sends a message with an automatically-constructed header. void send(uint16_t cmd, uint32_t flag = 0, bool silent = false); @@ -97,7 +81,99 @@ struct Channel { void send(const void* data, size_t size, bool silent = false); void send(const std::string& data, bool silent = false); -private: - static void dispatch_on_input(struct bufferevent*, void* ctx); - static void dispatch_on_error(struct bufferevent*, short events, void* ctx); + // Receives a message. Throws std::out_of_range if no messages are available. + asio::awaitable recv(); + +protected: + Channel( + Version version, + uint8_t language, + const std::string& name, + phosg::TerminalFormat terminal_send_color = phosg::TerminalFormat::END, + phosg::TerminalFormat terminal_recv_color = phosg::TerminalFormat::END); + Channel(const Channel& other) = delete; + Channel(Channel&& other) = delete; + Channel& operator=(const Channel& other) = delete; + Channel& operator=(Channel&& other) = delete; + + // Sends raw data on the underlying transport. If the channel is already + // disconnected, silently drops the data. + virtual void send_raw(std::string&& data) = 0; + // Receives raw data on the underlying transport. Raises when the channel is + // disconnected. + virtual asio::awaitable recv_raw(void* data, size_t size) = 0; +}; + +// Standard channel type, used for most PSO clients. Represents an open TCP +// socket. +class SocketChannel : public Channel, public std::enable_shared_from_this { +public: + std::unique_ptr sock; + asio::ip::tcp::endpoint local_addr; + asio::ip::tcp::endpoint remote_addr; + + // SocketChannel has a static constructor because it has an internal task, + // which is necessary to support flushing before disconnection (for example) + // and also to make send_raw not a coroutine, which keeps the rest of the + // code cleaner. The task needs to hold a shared_ptr to the SocketChannel + // whilc it's open + static std::shared_ptr create(std::shared_ptr io_context, + std::unique_ptr&& sock, + Version version, + uint8_t language, + const std::string& name = "", + phosg::TerminalFormat terminal_send_color = phosg::TerminalFormat::END, + phosg::TerminalFormat terminal_recv_color = phosg::TerminalFormat::END); + + virtual std::string default_name() const; + + virtual bool connected() const; + virtual void disconnect(); + + virtual void send_raw(std::string&& data); + virtual asio::awaitable recv_raw(void* data, size_t size); + +private: + SocketChannel( + std::shared_ptr io_context, + std::unique_ptr&& sock, + Version version, + uint8_t language, + const std::string& name, + phosg::TerminalFormat terminal_send_color, + phosg::TerminalFormat terminal_recv_color); + + std::deque outbound_data; + bool should_disconnect = false; + AsyncEvent send_buffer_nonempty_signal; + + asio::awaitable send_task(); +}; + +// In-process peer channel, used for replay testing. +class PeerChannel : public Channel { +public: + std::weak_ptr peer; + + PeerChannel( + std::shared_ptr io_context, + Version version, + uint8_t language, + const std::string& name = "", + phosg::TerminalFormat terminal_send_color = phosg::TerminalFormat::END, + phosg::TerminalFormat terminal_recv_color = phosg::TerminalFormat::END); + + static void link_peers(std::shared_ptr peer1, std::shared_ptr peer2); + + virtual std::string default_name() const; + + virtual bool connected() const; + virtual void disconnect(); + + virtual void send_raw(std::string&& data); + virtual asio::awaitable recv_raw(void* data, size_t size); + +private: + AsyncEvent send_buffer_nonempty_signal; + std::deque inbound_data; }; diff --git a/src/ChatCommands.cc b/src/ChatCommands.cc index de32d6ee..e7172039 100644 --- a/src/ChatCommands.cc +++ b/src/ChatCommands.cc @@ -2,6 +2,7 @@ #include +#include #include #include #include @@ -10,9 +11,10 @@ #include #include "Client.hh" +#include "GameServer.hh" #include "Lobby.hh" #include "Loggers.hh" -#include "ProxyServer.hh" +#include "ProxySession.hh" #include "ReceiveCommands.hh" #include "Revision.hh" #include "SendCommands.hh" @@ -27,7 +29,10 @@ using namespace std; class precondition_failed { public: - precondition_failed(const std::string& user_msg) : user_msg(user_msg) {} + template + precondition_failed(std::format_string fmt, ArgTs&&... args) + : user_msg(std::format(fmt, std::forward(args)...)) {} + ~precondition_failed() = default; const std::string& what() const { @@ -38,22 +43,19 @@ private: std::string user_msg; }; -precondition_failed precondition_failed_printf(const char* fmt, ...) { - va_list va; - va_start(va, fmt); - string ret = phosg::string_vprintf(fmt, va); - va_end(va); - return precondition_failed(ret); -} - struct Args { std::string text; bool check_permissions; -}; - -struct ServerArgs : public Args { std::shared_ptr c; + void check_is_proxy(bool is_proxy) const { + if (is_proxy && (this->c->proxy_session == nullptr)) { + throw precondition_failed("$C6This command can\nonly be used in\nproxy sessions."); + } else if (!is_proxy && (this->c->proxy_session != nullptr)) { + throw precondition_failed("$C6This command cannot\nbe used in proxy\nsessions."); + } + } + void check_version(Version version) const { if (this->c->version() != version) { throw precondition_failed("$C6This command cannot\nbe used for your\nversion of PSO."); @@ -61,16 +63,21 @@ struct ServerArgs : public Args { } void check_is_ep3(bool is_ep3) const { - if (::is_ep3(this->c->version()) != is_ep3) { - throw precondition_failed( - is_ep3 ? "$C6This command can only\nbe used in Episode 3." : "$C6This command cannot\nbe used in Episode 3."); + if (::is_ep3(this->c->version()) && !is_ep3) { + throw precondition_failed("$C6This command cannot\nbe used in Episode 3."); + } else if (!::is_ep3(this->c->version()) && is_ep3) { + throw precondition_failed("$C6This command can only\nbe used in Episode 3."); } } void check_is_game(bool is_game) const { - if (this->c->require_lobby()->is_game() != is_game) { - throw precondition_failed( - is_game ? "$C6This command cannot\nbe used in lobbies." : "$C6This command cannot\nbe used in games."); + bool client_in_game = this->c->proxy_session + ? this->c->proxy_session->is_in_game + : this->c->require_lobby()->is_game(); + if (client_in_game && !is_game) { + throw precondition_failed("$C6This command cannot\nbe used in games."); + } else if (!client_in_game && is_game) { + throw precondition_failed("$C6This command cannot\nbe used in lobbies."); } } @@ -78,18 +85,18 @@ struct ServerArgs : public Args { if (!this->c->login) { throw precondition_failed("$C6You are not\nlogged in."); } - if (!this->c->login->account->check_flag(flag)) { + if (this->check_permissions && !this->c->login->account->check_flag(flag)) { throw precondition_failed("$C6You do not have\npermission to\nrun this command."); } } void check_debug_enabled() const { - if (this->check_permissions && !this->c->config.check_flag(Client::Flag::DEBUG_ENABLED)) { + if (this->check_permissions && !this->c->check_flag(Client::Flag::DEBUG_ENABLED)) { throw precondition_failed("$C6This command can only\nbe run in debug mode\n(run %sdebug first)"); } } - void check_cheats_enabled(bool behavior_is_cheating) const { + void check_cheats_enabled_in_game(bool behavior_is_cheating) const { if (behavior_is_cheating && this->check_permissions && !this->c->require_lobby()->check_flag(Lobby::Flag::CHEATS_ENABLED) && @@ -98,12 +105,20 @@ struct ServerArgs : public Args { } } - void check_cheats_allowed(bool behavior_is_cheating) const { + void check_cheat_mode_available(bool behavior_is_cheating) const { if (behavior_is_cheating && this->check_permissions && (this->c->require_server_state()->cheat_mode_behavior == ServerState::BehaviorSwitch::OFF) && (!this->c->login || !this->c->login->account->check_flag(Account::Flag::CHEAT_ANYWHERE))) { - throw precondition_failed("$C6Cheats are disabled\non this server."); + throw precondition_failed("$C6Cheats are disabled"); + } + } + + void check_cheats_enabled_or_allowed(bool behavior_is_cheating) const { + if (this->c->proxy_session) { + this->check_cheat_mode_available(behavior_is_cheating); + } else { + this->check_cheats_enabled_in_game(behavior_is_cheating); } } @@ -114,34 +129,19 @@ struct ServerArgs : public Args { } }; -struct ProxyArgs : public Args { - shared_ptr ses; - - void check_cheats_allowed(bool behavior_is_cheating) const { - if (behavior_is_cheating && - this->check_permissions && - (this->ses->require_server_state()->cheat_mode_behavior == ServerState::BehaviorSwitch::OFF) && - (!this->ses->login || !this->ses->login->account->check_flag(Account::Flag::CHEAT_ANYWHERE))) { - throw precondition_failed("$C6Cheats are disabled\non this proxy."); - } - } -}; - //////////////////////////////////////////////////////////////////////////////// // Command definitions struct ChatCommandDefinition { - using ServerHandler = void (*)(const ServerArgs& args); - using ProxyHandler = void (*)(const ProxyArgs& args); + using Handler = asio::awaitable (*)(const Args& args); std::vector names; - ServerHandler server_handler; - ProxyHandler proxy_handler; + Handler handler; static unordered_map all_defs; - ChatCommandDefinition(std::initializer_list names, ServerHandler server_handler, ProxyHandler proxy_handler) - : names(names), server_handler(server_handler), proxy_handler(proxy_handler) { + ChatCommandDefinition(std::initializer_list names, Handler handler) + : names(names), handler(handler) { for (const char* name : this->names) { if (!this->all_defs.emplace(name, this).second) { throw logic_error("duplicate command definition: " + string(name)); @@ -152,20 +152,13 @@ struct ChatCommandDefinition { unordered_map ChatCommandDefinition::all_defs; -void unavailable_on_game_server(const ServerArgs&) { - throw precondition_failed("$C6This command is\nnot available on\nthe game server"); -} - -void unavailable_on_proxy_server(const ProxyArgs&) { - throw precondition_failed("$C6This command is\nnot available on\nthe proxy server"); -} - //////////////////////////////////////////////////////////////////////////////// // All commands (in alphabetical order) ChatCommandDefinition cc_allevent( {"$allevent"}, - +[](const ServerArgs& a) -> void { + +[](const Args& a) -> asio::awaitable { + a.check_is_proxy(false); a.check_account_flag(Account::Flag::CHANGE_EVENT); uint8_t new_event = event_for_name(a.text); @@ -182,12 +175,14 @@ ChatCommandDefinition cc_allevent( l->event = new_event; send_change_event(l, l->event); } - }, - unavailable_on_proxy_server); + co_return; + }); -static void server_command_announce_inner(const ServerArgs& a, bool mail, bool anonymous) { - auto s = a.c->require_server_state(); +static asio::awaitable server_command_announce_inner(const Args& a, bool mail, bool anonymous) { + a.check_is_proxy(false); a.check_account_flag(Account::Flag::ANNOUNCE); + + auto s = a.c->require_server_state(); if (anonymous) { if (mail) { send_simple_mail(s, 0, s->name, a.text); @@ -203,47 +198,45 @@ static void server_command_announce_inner(const ServerArgs& a, bool mail, bool a send_text_or_scrolling_message(s, message, message); } } + co_return; } ChatCommandDefinition cc_ann_named( {"$ann"}, - +[](const ServerArgs& a) { - server_command_announce_inner(a, false, false); - }, - unavailable_on_proxy_server); + +[](const Args& a) -> asio::awaitable { + return server_command_announce_inner(a, false, false); + }); ChatCommandDefinition cc_ann_anonymous( {"$ann?"}, - +[](const ServerArgs& a) { - server_command_announce_inner(a, false, true); - }, - unavailable_on_proxy_server); + +[](const Args& a) -> asio::awaitable { + return server_command_announce_inner(a, false, true); + }); ChatCommandDefinition cc_ann_mail_named( {"$ann!"}, - +[](const ServerArgs& a) { - server_command_announce_inner(a, true, false); - }, - unavailable_on_proxy_server); + +[](const Args& a) -> asio::awaitable { + return server_command_announce_inner(a, true, false); + }); ChatCommandDefinition cc_ann_mail_anonymous( {"$ann?!", "$ann!?"}, - +[](const ServerArgs& a) { - server_command_announce_inner(a, true, true); - }, - unavailable_on_proxy_server); + +[](const Args& a) -> asio::awaitable { + return server_command_announce_inner(a, true, true); + }); -ChatCommandDefinition cc_ann_mail_anonymous2( +ChatCommandDefinition cc_announce_rares( {"$announcerares"}, - +[](const ServerArgs& a) { + +[](const Args& a) -> asio::awaitable { + a.check_is_proxy(false); + a.c->login->account->toggle_user_flag(Account::UserFlag::DISABLE_DROP_NOTIFICATION_BROADCAST); a.c->login->account->save(); - send_text_message_printf(a.c, "$C6Rare announcements\n%s for your\nitems", + send_text_message_fmt(a.c, "$C6Rare announcements\n{} for your\nitems", a.c->login->account->check_user_flag(Account::UserFlag::DISABLE_DROP_NOTIFICATION_BROADCAST) ? "disabled" : "enabled"); - }, - unavailable_on_proxy_server); + co_return; + }); ChatCommandDefinition cc_arrow( {"$arrow"}, - +[](const ServerArgs& a) { - auto l = a.c->require_lobby(); + +[](const Args& a) -> asio::awaitable { if (a.text == "red") { a.c->lobby_arrow_color = 0x01; } else if (a.text == "blue") { @@ -271,31 +264,36 @@ ChatCommandDefinition cc_arrow( } else { a.c->lobby_arrow_color = stoull(a.text, nullptr, 0); } - if (!l->is_game()) { - send_arrow_update(l); + + if (a.c->proxy_session) { + a.c->proxy_session->server_channel->send(0x89, a.c->lobby_arrow_color); + } else { + auto l = a.c->require_lobby(); + if (!l->is_game()) { + send_arrow_update(l); + } } - }, - +[](const ProxyArgs& a) { - a.ses->server_channel.send(0x89, stoull(a.text, nullptr, 0)); + co_return; }); ChatCommandDefinition cc_auction( {"$auction"}, - +[](const ServerArgs& a) { + +[](const Args& a) -> asio::awaitable { a.check_account_flag(Account::Flag::DEBUG); - auto l = a.c->require_lobby(); - if (l->is_game() && l->is_ep3()) { - G_InitiateCardAuction_Ep3_6xB5x42 cmd; - cmd.header.sender_client_id = a.c->lobby_client_id; - send_command_t(l, 0xC9, 0x00, cmd); - } - }, - +[](const ProxyArgs& a) { G_InitiateCardAuction_Ep3_6xB5x42 cmd; - cmd.header.sender_client_id = a.ses->lobby_client_id; - a.ses->client_channel.send(0xC9, 0x00, &cmd, sizeof(cmd)); - a.ses->server_channel.send(0xC9, 0x00, &cmd, sizeof(cmd)); + cmd.header.sender_client_id = a.c->lobby_client_id; + + if (a.c->proxy_session) { + a.c->channel->send(0xC9, 0x00, &cmd, sizeof(cmd)); + a.c->proxy_session->server_channel->send(0xC9, 0x00, &cmd, sizeof(cmd)); + } else { + auto l = a.c->require_lobby(); + if (l->is_game() && l->is_ep3()) { + send_command_t(l, 0xC9, 0x00, cmd); + } + } + co_return; }); static string name_for_client(shared_ptr c) { @@ -305,7 +303,7 @@ static string name_for_client(shared_ptr c) { } if (c->login) { - return phosg::string_printf("SN:%" PRIu32, c->login->account->account_id); + return std::format("SN:{}", c->login->account->account_id); } return "Player"; @@ -313,10 +311,12 @@ static string name_for_client(shared_ptr c) { ChatCommandDefinition cc_ban( {"$ban"}, - +[](const ServerArgs& a) { + +[](const Args& a) -> asio::awaitable { + a.check_is_proxy(false); + a.check_account_flag(Account::Flag::BAN_USER); + auto s = a.c->require_server_state(); auto l = a.c->require_lobby(); - a.check_account_flag(Account::Flag::BAN_USER); size_t space_pos = a.text.find(' '); if (space_pos == string::npos) { @@ -363,17 +363,17 @@ ChatCommandDefinition cc_ban( target->login->account->save(); send_message_box(target, "$C6You have been banned."); string target_name = name_for_client(target); - s->game_server->disconnect_client(target); - send_text_message_printf(a.c, "$C6%s banned", target_name.c_str()); - }, - unavailable_on_proxy_server); + target->channel->disconnect(); + send_text_message_fmt(a.c, "$C6{} banned", target_name); + co_return; + }); ChatCommandDefinition cc_bank( {"$bank"}, - +[](const ServerArgs& a) { + +[](const Args& a) -> asio::awaitable { + a.check_is_proxy(false); a.check_version(Version::BB_V4); - - if (a.c->config.check_flag(Client::Flag::AT_BANK_COUNTER)) { + if (a.c->check_flag(Client::Flag::AT_BANK_COUNTER)) { throw runtime_error("cannot change banks while at the bank counter"); } if (a.c->has_overlay()) { @@ -393,31 +393,35 @@ ChatCommandDefinition cc_bank( auto bp = a.c->current_bank_character(); auto name = escape_player_name(bp->disp.name.decode(a.c->language())); - send_text_message_printf(a.c, "$C6Using %s\'s bank (%zu)", name.c_str(), new_char_index); + send_text_message_fmt(a.c, "$C6Using {}\'s bank ({})", name, new_char_index); } else { throw precondition_failed("$C6Invalid bank number"); } auto& bank = a.c->current_bank(); bank.assign_ids(0x99000000 + (a.c->lobby_client_id << 20)); - a.c->log.info("Assigned bank item IDs"); + a.c->log.info_f("Assigned bank item IDs"); a.c->print_bank(stderr); - send_text_message_printf(a.c, "%" PRIu32 " items\n%" PRIu32 " Meseta", bank.num_items.load(), bank.meseta.load()); - }, - unavailable_on_proxy_server); + send_text_message_fmt(a.c, "{} items\n{} Meseta", bank.num_items, bank.meseta); + co_return; + }); + +static asio::awaitable server_command_bbchar_savechar(const Args& a, bool is_bb_conversion) { + // TODO: We could support this in proxy sessions; we'd just have to handle the 61/30 correctly + a.check_is_proxy(false); + a.check_is_game(false); -static void server_command_bbchar_savechar(const ServerArgs& a, bool is_bb_conversion) { auto s = a.c->require_server_state(); auto l = a.c->require_lobby(); - a.check_is_game(false); if (is_bb_conversion && is_ep3(a.c->version())) { throw precondition_failed("$C6Episode 3 players\ncannot be converted\nto BB format"); } - auto pending_export = make_unique(); - + shared_ptr dest_account; + shared_ptr dest_bb_license; + ssize_t dest_character_index = 0; if (is_bb_conversion) { vector tokens = phosg::split(a.text, ' '); if (tokens.size() != 3) { @@ -425,85 +429,222 @@ static void server_command_bbchar_savechar(const ServerArgs& a, bool is_bb_conve } // username/password are tokens[0] and [1] - pending_export->character_index = stoll(tokens[2]) - 1; - if ((pending_export->character_index > 3) || (pending_export->character_index < 0)) { + dest_character_index = stoll(tokens[2]) - 1; + if ((dest_character_index > 3) || (dest_character_index < 0)) { throw precondition_failed("$C6Player index must\nbe in range 1-4"); } try { 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; + dest_account = dest_login->account; + dest_bb_license = dest_login->bb_license; } catch (const exception& e) { - throw precondition_failed_printf("$C6Login failed: %s", e.what()); + throw precondition_failed("$C6Login failed: {}", e.what()); } } else { - pending_export->character_index = stoll(a.text) - 1; - if ((pending_export->character_index > 15) || (pending_export->character_index < 0)) { + dest_character_index = stoll(a.text) - 1; + if ((dest_character_index > 15) || (dest_character_index < 0)) { throw precondition_failed("$C6Player index must\nbe in range 1-16"); } - pending_export->dest_account = a.c->login->account; + dest_account = a.c->login->account; } - a.c->pending_character_export = std::move(pending_export); + // If the client isn't BB, request the player info. (If they are BB, the + // server already has it) + auto ch = co_await send_get_player_info(a.c, true); - // Request the player data. The client will respond with a 61 or 30, and the - // handler for either of those commands will execute the conversion - send_get_player_info(a.c, true); + string filename = dest_bb_license + ? Client::character_filename(dest_bb_license->username, dest_character_index) + : Client::backup_character_filename(dest_account->account_id, dest_character_index, is_ep3(a.c->version())); + + if (s->player_files_manager->get_character(filename)) { + send_text_message(a.c, "$C6The target player\nis currently loaded.\nSign off in Blue\nBurst and try again."); + co_return; + } + + if (ch.is_full_info) { + // Client sent 30; ch contains the verbatim save file from the client + if (ch.ep3_character) { + try { + Client::save_ep3_character_file(filename, *ch.ep3_character); + send_text_message(a.c, "$C7Character data saved\n(full save file)"); + } catch (const exception& e) { + send_text_message_fmt(a.c, "$C6Character data could\nnot be saved:\n{}", e.what()); + } + } + if (ch.character) { + try { + Client::save_character_file(filename, a.c->system_file(), ch.character); + send_text_message(a.c, "$C7Character data saved\n(full save file)"); + } catch (const exception& e) { + send_text_message_fmt(a.c, "$C6Character data could\nnot be saved:\n{}", e.what()); + } + } + + } else { + // Client sent 61; generate a BB-format player from the information we have + // and save that instead + if (ch.character) { + auto bb_player = PSOBBCharacterFile::create_from_config( + a.c->login->account->account_id, + a.c->language(), + ch.character->disp.visual, + ch.character->disp.name.decode(a.c->language()), + s->level_table(a.c->version())); + bb_player->disp.visual.version = 4; + bb_player->disp.visual.name_color_checksum = 0x00000000; + bb_player->inventory = ch.character->inventory; + // Before V3, player stats can't be correctly computed from other fields + // because material usage isn't stored anywhere. For these versions, we + // have to trust the stats field from the player's data. + auto level_table = s->level_table(a.c->version()); + if (is_v1_or_v2(a.c->version())) { + bb_player->disp.stats = ch.character->disp.stats; + bb_player->import_tethealla_material_usage(level_table); + } else { + level_table->advance_to_level( + bb_player->disp.stats, ch.character->disp.stats.level, bb_player->disp.visual.char_class); + bb_player->disp.stats.char_stats.atp += bb_player->get_material_usage(PSOBBCharacterFile::MaterialType::POWER) * 2; + bb_player->disp.stats.char_stats.mst += bb_player->get_material_usage(PSOBBCharacterFile::MaterialType::MIND) * 2; + bb_player->disp.stats.char_stats.evp += bb_player->get_material_usage(PSOBBCharacterFile::MaterialType::EVADE) * 2; + bb_player->disp.stats.char_stats.dfp += bb_player->get_material_usage(PSOBBCharacterFile::MaterialType::DEF) * 2; + bb_player->disp.stats.char_stats.lck += bb_player->get_material_usage(PSOBBCharacterFile::MaterialType::LUCK) * 2; + bb_player->disp.stats.char_stats.hp += bb_player->get_material_usage(PSOBBCharacterFile::MaterialType::HP) * 2; + bb_player->disp.stats.experience = ch.character->disp.stats.experience; + bb_player->disp.stats.meseta = ch.character->disp.stats.meseta; + } + bb_player->disp.technique_levels_v1 = ch.character->disp.technique_levels_v1; + bb_player->auto_reply = ch.character->auto_reply; + bb_player->info_board = ch.character->info_board; + bb_player->battle_records = ch.character->battle_records; + bb_player->challenge_records = ch.character->challenge_records; + bb_player->choice_search_config = ch.character->choice_search_config; + + try { + Client::save_character_file(filename, a.c->system_file(), bb_player); + send_text_message(a.c, "$C7Character data saved\n(basic only)"); + } catch (const exception& e) { + send_text_message_fmt(a.c, "$C6Character data could\nnot be saved:\n{}", e.what()); + } + } + } + + co_return; } ChatCommandDefinition cc_bbchar( {"$bbchar"}, - +[](const ServerArgs& a) { - server_command_bbchar_savechar(a, true); - }, - unavailable_on_proxy_server); + +[](const Args& a) -> asio::awaitable { + return server_command_bbchar_savechar(a, true); + }); ChatCommandDefinition cc_cheat( {"$cheat"}, - +[](const ServerArgs& a) { - auto s = a.c->require_server_state(); - auto l = a.c->require_lobby(); + +[](const Args& a) -> asio::awaitable { + a.check_is_proxy(false); a.check_is_game(true); a.check_is_leader(); + + auto l = a.c->require_lobby(); if (a.check_permissions && l->check_flag(Lobby::Flag::CANNOT_CHANGE_CHEAT_MODE)) { throw precondition_failed("$C6Cheat mode cannot\nbe changed on this\nserver"); } else { 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_fmt(l, "Cheat mode {}", l->check_flag(Lobby::Flag::CHEATS_ENABLED) ? "enabled" : "disabled"); + auto s = a.c->require_server_state(); if (!l->check_flag(Lobby::Flag::CHEATS_ENABLED) && !a.c->login->account->check_flag(Account::Flag::CHEAT_ANYWHERE) && s->cheat_flags.insufficient_minimum_level) { size_t default_min_level = s->default_min_level_for_game(a.c->version(), l->episode, l->difficulty); if (l->min_level < default_min_level) { l->min_level = default_min_level; - send_text_message_printf(l, "$C6Minimum level set\nto %" PRIu32, l->min_level + 1); + send_text_message_fmt(l, "$C6Minimum level set\nto {}", l->min_level + 1); } } } - }, - unavailable_on_proxy_server); + co_return; + }); + +ChatCommandDefinition cc_checkchar( + {"$checkchar"}, + +[](const Args& a) -> asio::awaitable { + if (a.check_permissions && a.c->login->account->check_flag(Account::Flag::IS_SHARED_ACCOUNT)) { + throw precondition_failed("$C7This command cannot\nbe used on a shared\naccount"); + } + + size_t index = stoull(a.text, nullptr, 0) - 1; + if (index > 15) { + throw precondition_failed("$C6Player index must\nbe in range 1-16"); + } + + try { + if (is_ep3(a.c->version())) { + string filename = a.c->backup_character_filename(a.c->login->account->account_id, index, true); + auto ch = phosg::load_object_file(filename); + send_text_message_fmt(a.c, "Slot {}: $C6{}$C7\n{} {}\nCLv: on {}.{}, off {}.{}", + index + 1, ch.disp.visual.name.decode(), + name_for_section_id(ch.disp.visual.section_id), name_for_char_class(ch.disp.visual.char_class), + (ch.ep3_config.online_clv_exp / 100) + 1, ch.ep3_config.online_clv_exp % 100, + (ch.ep3_config.offline_clv_exp / 100) + 1, ch.ep3_config.offline_clv_exp % 100); + } else { + string filename = a.c->backup_character_filename(a.c->login->account->account_id, index, false); + auto ch = PSOCHARFile::load_shared(filename, false).character_file; + send_text_message_fmt(a.c, "Slot {}: $C6{}$C7\n{} {}\nLevel {}", + index + 1, ch->disp.name.decode(), + name_for_section_id(ch->disp.visual.section_id), name_for_char_class(ch->disp.visual.char_class), + ch->disp.stats.level + 1); + } + } catch (const phosg::cannot_open_file&) { + send_text_message_fmt(a.c, "No character in\nslot {}", index + 1); + } + + co_return; + }); ChatCommandDefinition cc_debug( {"$debug"}, - +[](const ServerArgs& a) { + +[](const Args& a) -> asio::awaitable { a.check_account_flag(Account::Flag::DEBUG); - a.c->config.toggle_flag(Client::Flag::DEBUG_ENABLED); - send_text_message_printf(a.c, "Debug %s", - (a.c->config.check_flag(Client::Flag::DEBUG_ENABLED) ? "enabled" : "disabled")); - }, - unavailable_on_proxy_server); + a.c->toggle_flag(Client::Flag::DEBUG_ENABLED); + send_text_message_fmt(a.c, "Debug {}", + (a.c->check_flag(Client::Flag::DEBUG_ENABLED) ? "enabled" : "disabled")); + co_return; + }); + +ChatCommandDefinition cc_deletechar( + {"$deletechar"}, + +[](const Args& a) -> asio::awaitable { + if (a.check_permissions && a.c->login->account->check_flag(Account::Flag::IS_SHARED_ACCOUNT)) { + throw precondition_failed("$C7This command cannot\nbe used on a shared\naccount"); + } + + size_t index = stoull(a.text, nullptr, 0) - 1; + if (index > 15) { + throw precondition_failed("$C6Player index must\nbe in range 1-16"); + } + + string filename = a.c->backup_character_filename(a.c->login->account->account_id, index, is_ep3(a.c->version())); + if (std::filesystem::is_regular_file(filename)) { + std::filesystem::remove(filename); + send_text_message_fmt(a.c, "Character in slot\n{} deleted", index + 1); + } else { + send_text_message_fmt(a.c, "No character exists\nin slot {}", index + 1); + } + + co_return; + }); ChatCommandDefinition cc_dicerange( {"$dicerange"}, - +[](const ServerArgs& a) { - auto l = a.c->require_lobby(); + +[](const Args& a) -> asio::awaitable { + a.check_is_proxy(false); a.check_is_game(true); a.check_is_ep3(true); a.check_is_leader(); + auto l = a.c->require_lobby(); if (l->episode != Episode::EP3) { throw logic_error("non-Ep3 client in Ep3 game"); } @@ -554,141 +695,147 @@ ChatCommandDefinition cc_dicerange( l->ep3_server->def_dice_value_range_2v1_override = def_dice_range_2v1; if (!def_dice_range && !atk_dice_range_2v1 && !def_dice_range_2v1) { - send_text_message_printf(l, "$C7Dice ranges reset\nto defaults"); + send_text_message_fmt(l, "$C7Dice ranges reset\nto defaults"); } else { - send_text_message_printf(l, "$C7Dice ranges changed:"); + send_text_message_fmt(l, "$C7Dice ranges changed:"); if (def_dice_range) { - send_text_message_printf(l, "$C7DEF: $C6%hhu-%hhu", + send_text_message_fmt(l, "$C7DEF: $C6{}-{}", static_cast(def_dice_range >> 4), static_cast(def_dice_range & 0x0F)); } if (atk_dice_range_2v1) { - send_text_message_printf(l, "$C7ATK (1p in 2v1): $C6%hhu-%hhu", + send_text_message_fmt(l, "$C7ATK (1p in 2v1): $C6{}-{}", static_cast(atk_dice_range_2v1 >> 4), static_cast(atk_dice_range_2v1 & 0x0F)); } if (def_dice_range_2v1) { - send_text_message_printf(l, "$C7DEF (1p in 2v1): $C6%hhu-%hhu", + send_text_message_fmt(l, "$C7DEF (1p in 2v1): $C6{}-{}", static_cast(def_dice_range_2v1 >> 4), static_cast(def_dice_range_2v1 & 0x0F)); } } - }, - unavailable_on_proxy_server); + co_return; + }); ChatCommandDefinition cc_dropmode( {"$dropmode"}, - +[](const ServerArgs& a) { - auto l = a.c->require_lobby(); + +[](const Args& a) -> asio::awaitable { a.check_is_game(true); - if (a.text.empty()) { - switch (l->drop_mode) { - case Lobby::DropMode::DISABLED: - send_text_message(a.c, "Drop mode: disabled"); - break; - case Lobby::DropMode::CLIENT: - send_text_message(a.c, "Drop mode: client"); - break; - case Lobby::DropMode::SERVER_SHARED: - send_text_message(a.c, "Drop mode: server\nshared"); - break; - case Lobby::DropMode::SERVER_PRIVATE: - send_text_message(a.c, "Drop mode: server\nprivate"); - break; - case Lobby::DropMode::SERVER_DUPLICATE: - send_text_message(a.c, "Drop mode: server\nduplicate"); - break; - } + auto s = a.c->require_server_state(); + a.check_cheats_enabled_or_allowed(s->cheat_flags.proxy_override_drops); + + if (a.c->proxy_session) { + + using DropMode = ProxySession::DropMode; + if (a.text.empty()) { + switch (a.c->proxy_session->drop_mode) { + case DropMode::DISABLED: + send_text_message(a.c, "Drop mode: disabled"); + break; + case DropMode::PASSTHROUGH: + send_text_message(a.c, "Drop mode: default"); + break; + case DropMode::INTERCEPT: + send_text_message(a.c, "Drop mode: proxy"); + break; + } - } else { - a.check_is_leader(); - Lobby::DropMode new_mode; - if ((a.text == "none") || (a.text == "disabled")) { - new_mode = Lobby::DropMode::DISABLED; - } else if (a.text == "client") { - new_mode = Lobby::DropMode::CLIENT; - } else if ((a.text == "shared") || (a.text == "server")) { - new_mode = Lobby::DropMode::SERVER_SHARED; - } else if ((a.text == "private") || (a.text == "priv")) { - new_mode = Lobby::DropMode::SERVER_PRIVATE; - } else if ((a.text == "duplicate") || (a.text == "dup")) { - new_mode = Lobby::DropMode::SERVER_DUPLICATE; } else { - throw precondition_failed("Invalid drop mode"); + DropMode new_mode; + if ((a.text == "none") || (a.text == "disabled")) { + new_mode = DropMode::DISABLED; + } else if ((a.text == "default") || (a.text == "passthrough")) { + new_mode = DropMode::PASSTHROUGH; + } else if ((a.text == "proxy") || (a.text == "intercept")) { + new_mode = DropMode::INTERCEPT; + } else { + throw precondition_failed("Invalid drop mode"); + } + + a.c->proxy_session->set_drop_mode(s, a.c->version(), a.c->override_random_seed, new_mode); + switch (a.c->proxy_session->drop_mode) { + case DropMode::DISABLED: + send_text_message(a.c->channel, "Item drops disabled"); + break; + case DropMode::PASSTHROUGH: + send_text_message(a.c->channel, "Item drops changed\nto default mode"); + break; + case DropMode::INTERCEPT: + send_text_message(a.c->channel, "Item drops changed\nto proxy mode"); + break; + } } - if (!(l->allowed_drop_modes & (1 << static_cast(new_mode)))) { - throw precondition_failed("Drop mode not\nallowed"); - } + } else { // Not proxy session + auto l = a.c->require_lobby(); + if (a.text.empty()) { + switch (l->drop_mode) { + case Lobby::DropMode::DISABLED: + send_text_message(a.c, "Drop mode: disabled"); + break; + case Lobby::DropMode::CLIENT: + send_text_message(a.c, "Drop mode: client"); + break; + case Lobby::DropMode::SERVER_SHARED: + send_text_message(a.c, "Drop mode: server\nshared"); + break; + case Lobby::DropMode::SERVER_PRIVATE: + send_text_message(a.c, "Drop mode: server\nprivate"); + break; + case Lobby::DropMode::SERVER_DUPLICATE: + send_text_message(a.c, "Drop mode: server\nduplicate"); + break; + } - l->drop_mode = new_mode; - switch (l->drop_mode) { - case Lobby::DropMode::DISABLED: - send_text_message(l, "Item drops disabled"); - break; - case Lobby::DropMode::CLIENT: - send_text_message(l, "Item drops changed\nto client mode"); - break; - case Lobby::DropMode::SERVER_SHARED: - send_text_message(l, "Item drops changed\nto server shared\nmode"); - break; - case Lobby::DropMode::SERVER_PRIVATE: - send_text_message(l, "Item drops changed\nto server private\nmode"); - break; - case Lobby::DropMode::SERVER_DUPLICATE: - send_text_message(l, "Item drops changed\nto server duplicate\nmode"); - break; - } - } - }, - +[](const ProxyArgs& a) { - auto s = a.ses->require_server_state(); - a.check_cheats_allowed(s->cheat_flags.proxy_override_drops); - - using DropMode = ProxyServer::LinkedSession::DropMode; - if (a.text.empty()) { - switch (a.ses->drop_mode) { - case DropMode::DISABLED: - send_text_message(a.ses->client_channel, "Drop mode: disabled"); - break; - case DropMode::PASSTHROUGH: - send_text_message(a.ses->client_channel, "Drop mode: default"); - break; - case DropMode::INTERCEPT: - send_text_message(a.ses->client_channel, "Drop mode: proxy"); - break; - } - - } else { - DropMode new_mode; - if ((a.text == "none") || (a.text == "disabled")) { - new_mode = DropMode::DISABLED; - } else if ((a.text == "default") || (a.text == "passthrough")) { - new_mode = DropMode::PASSTHROUGH; - } else if ((a.text == "proxy") || (a.text == "intercept")) { - new_mode = DropMode::INTERCEPT; - } else { - throw precondition_failed("Invalid drop mode"); - } - - a.ses->set_drop_mode(new_mode); - switch (a.ses->drop_mode) { - case DropMode::DISABLED: - send_text_message(a.ses->client_channel, "Item drops disabled"); - break; - case DropMode::PASSTHROUGH: - send_text_message(a.ses->client_channel, "Item drops changed\nto default mode"); - break; - case DropMode::INTERCEPT: - send_text_message(a.ses->client_channel, "Item drops changed\nto proxy mode"); - break; + } else { + a.check_is_leader(); + Lobby::DropMode new_mode; + if ((a.text == "none") || (a.text == "disabled")) { + new_mode = Lobby::DropMode::DISABLED; + } else if (a.text == "client") { + new_mode = Lobby::DropMode::CLIENT; + } else if ((a.text == "shared") || (a.text == "server")) { + new_mode = Lobby::DropMode::SERVER_SHARED; + } else if ((a.text == "private") || (a.text == "priv")) { + new_mode = Lobby::DropMode::SERVER_PRIVATE; + } else if ((a.text == "duplicate") || (a.text == "dup")) { + new_mode = Lobby::DropMode::SERVER_DUPLICATE; + } else { + throw precondition_failed("Invalid drop mode"); + } + + if (!(l->allowed_drop_modes & (1 << static_cast(new_mode)))) { + throw precondition_failed("Drop mode not\nallowed"); + } + + l->drop_mode = new_mode; + switch (l->drop_mode) { + case Lobby::DropMode::DISABLED: + send_text_message(l, "Item drops disabled"); + break; + case Lobby::DropMode::CLIENT: + send_text_message(l, "Item drops changed\nto client mode"); + break; + case Lobby::DropMode::SERVER_SHARED: + send_text_message(l, "Item drops changed\nto server shared\nmode"); + break; + case Lobby::DropMode::SERVER_PRIVATE: + send_text_message(l, "Item drops changed\nto server private\nmode"); + break; + case Lobby::DropMode::SERVER_DUPLICATE: + send_text_message(l, "Item drops changed\nto server duplicate\nmode"); + break; + } } } + co_return; }); ChatCommandDefinition cc_edit( {"$edit"}, - +[](const ServerArgs& a) { + +[](const Args& a) -> asio::awaitable { + a.check_is_proxy(false); + a.check_is_game(false); + auto s = a.c->require_server_state(); auto l = a.c->require_lobby(); - a.check_is_game(false); if (!is_v1_or_v2(a.c->version()) && (a.c->version() != Version::BB_V4)) { throw precondition_failed("$C6This command cannot\nbe used for your\nversion of PSO."); } @@ -764,15 +911,13 @@ ChatCommandDefinition cc_edit( } p->recompute_stats(s->level_table(a.c->version())); } else if (tokens.at(0) == "namecolor") { - uint32_t new_color; - sscanf(tokens.at(1).c_str(), "%8X", &new_color); - p->disp.visual.name_color = new_color; + p->disp.visual.name_color = stoul(tokens.at(1), nullptr, 16); } 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)); - a.c->channel.language = new_language; + a.c->channel->language = new_language; p->inventory.language = new_language; p->guild_card.language = new_language; auto sys = a.c->system_file(false); @@ -870,159 +1015,164 @@ ChatCommandDefinition cc_edit( } a.c->v1_v2_last_reported_disp.reset(); s->send_lobby_join_notifications(l, a.c); - }, - unavailable_on_proxy_server); + co_return; + }); ChatCommandDefinition cc_event( {"$event"}, - +[](const ServerArgs& a) { - auto l = a.c->require_lobby(); - a.check_is_game(false); - a.check_account_flag(Account::Flag::CHANGE_EVENT); + +[](const Args& a) -> asio::awaitable { + if (a.c->proxy_session) { + if (a.text.empty()) { + a.c->override_lobby_event = 0xFF; + } else { + uint8_t new_event = event_for_name(a.text); + if (new_event == 0xFF) { + throw precondition_failed("$C6No such lobby event"); + } else { + a.c->override_lobby_event = new_event; + if (!is_v1_or_v2(a.c->version())) { + a.c->channel->send(0xDA, a.c->override_lobby_event); + } + } + } - uint8_t new_event = event_for_name(a.text); - if (new_event == 0xFF) { - throw precondition_failed("$C6No such lobby event"); - } + } else { // Not proxy session + a.check_is_game(false); + a.check_account_flag(Account::Flag::CHANGE_EVENT); + + auto l = a.c->require_lobby(); - l->event = new_event; - send_change_event(l, l->event); - }, - +[](const ProxyArgs& a) { - if (a.text.empty()) { - a.ses->config.override_lobby_event = 0xFF; - } else { uint8_t new_event = event_for_name(a.text); if (new_event == 0xFF) { throw precondition_failed("$C6No such lobby event"); - } else { - a.ses->config.override_lobby_event = new_event; - if (!is_v1_or_v2(a.ses->version())) { - a.ses->client_channel.send(0xDA, a.ses->config.override_lobby_event); - } } + + l->event = new_event; + send_change_event(l, l->event); } + co_return; }); ChatCommandDefinition cc_exit( {"$exit"}, - +[](const ServerArgs& a) { - auto l = a.c->require_lobby(); - if (!l->is_game()) { + +[](const Args& a) -> asio::awaitable { + if (!(a.c->proxy_session + ? a.c->proxy_session->is_in_game + : a.c->require_lobby()->is_game())) { // Client is in the lobby; send them to the login server (main menu) - send_self_leave_notification(a.c); - if (!a.c->config.check_flag(Client::Flag::NO_D6)) { - send_message_box(a.c, ""); + if (a.c->proxy_session) { + if (is_v4(a.c->version())) { + throw precondition_failed("$C6You cannot exit proxy\nsessions on BB"); + } + a.c->proxy_session->server_channel->disconnect(); + } else { + send_self_leave_notification(a.c); + if (!a.c->check_flag(Client::Flag::NO_D6)) { + send_message_box(a.c, ""); + } + co_await start_login_server_procedure(a.c); } - send_client_to_login_server(a.c); - return; + co_return; } + if (is_ep3(a.c->version())) { // Client is on Ep3; command ED triggers game exit - a.c->channel.send(0xED, 0x00); - return; + a.c->channel->send(0xED, 0x00); + co_return; } - if (l->check_flag(Lobby::Flag::QUEST_IN_PROGRESS) || l->check_flag(Lobby::Flag::JOINABLE_QUEST_IN_PROGRESS)) { + + bool is_in_quest; + if (a.c->proxy_session) { + is_in_quest = a.c->proxy_session->is_in_quest; + } else { + auto l = a.c->require_lobby(); + is_in_quest = (l->check_flag(Lobby::Flag::QUEST_IN_PROGRESS) || + l->check_flag(Lobby::Flag::JOINABLE_QUEST_IN_PROGRESS)); + } + if (is_in_quest) { // Client is in a quest; command 6x73 triggers game exit G_UnusedHeader cmd = {0x73, 0x01, 0x0000}; - a.c->channel.send(0x60, 0x00, cmd); + a.c->channel->send(0x60, 0x00, cmd); a.c->floor = 0; - return; + co_return; } - if (a.c->config.check_flag(Client::Flag::HAS_SEND_FUNCTION_CALL) && - a.c->config.check_flag(Client::Flag::SEND_FUNCTION_CALL_ACTUALLY_RUNS_CODE)) { + + if (a.c->check_flag(Client::Flag::HAS_SEND_FUNCTION_CALL) && + a.c->check_flag(Client::Flag::SEND_FUNCTION_CALL_ACTUALLY_RUNS_CODE)) { + co_await prepare_client_for_patches(a.c); auto s = a.c->require_server_state(); shared_ptr fn; try { - fn = s->function_code_index->get_patch("ExitAnywhere", a.c->config.specific_version); + fn = s->function_code_index->get_patch("ExitAnywhere", a.c->specific_version); } catch (const out_of_range&) { } if (fn) { - send_function_call(a.c, fn); - return; + co_await send_function_call(a.c, fn); + co_return; } } throw precondition_failed("$C6You must return to\nthe lobby first"); - }, - +[](const ProxyArgs& a) { - if (a.ses->is_in_game) { - if (is_ep3(a.ses->version())) { - a.ses->client_channel.send(0xED, 0x00); - } else if (a.ses->is_in_quest) { - G_UnusedHeader cmd = {0x73, 0x01, 0x0000}; - a.ses->client_channel.send(0x60, 0x00, cmd); - } else { - throw precondition_failed("$C6You must return to\nthe lobby first"); - } - } else { - a.ses->disconnect_action = ProxyServer::LinkedSession::DisconnectAction::CLOSE_IMMEDIATELY; - a.ses->send_to_game_server(); - } }); ChatCommandDefinition cc_gc( {"$gc"}, - +[](const ServerArgs& a) { - send_guild_card(a.c, a.c); - }, - +[](const ProxyArgs& a) { - bool any_card_sent = false; - for (const auto& p : a.ses->lobby_players) { - if (!p.name.empty() && a.text == p.name) { - send_guild_card(a.ses->client_channel, p.guild_card_number, p.guild_card_number, p.name, "", "", p.language, p.section_id, p.char_class); - any_card_sent = true; + +[](const Args& a) -> asio::awaitable { + if (a.c->proxy_session) { + bool any_card_sent = false; + for (const auto& p : a.c->proxy_session->lobby_players) { + if (!p.name.empty() && a.text == p.name) { + send_guild_card(a.c->channel, p.guild_card_number, p.guild_card_number, p.name, "", "", p.language, p.section_id, p.char_class); + any_card_sent = true; + } } - } - if (!any_card_sent) { - size_t index = stoull(a.text, nullptr, 0); - const auto& p = a.ses->lobby_players.at(index); - if (!p.name.empty()) { - send_guild_card(a.ses->client_channel, p.guild_card_number, p.guild_card_number, p.name, "", "", p.language, p.section_id, p.char_class); + if (!any_card_sent) { + size_t index = stoull(a.text, nullptr, 0); + const auto& p = a.c->proxy_session->lobby_players.at(index); + if (!p.name.empty()) { + send_guild_card(a.c->channel, p.guild_card_number, p.guild_card_number, p.name, "", "", p.language, p.section_id, p.char_class); + } } + } else { + send_guild_card(a.c, a.c); } + co_return; }); ChatCommandDefinition cc_infhp( {"$infhp"}, - +[](const ServerArgs& a) { - auto s = a.c->require_server_state(); - auto l = a.c->require_lobby(); - a.check_is_game(true); + +[](const Args& a) -> asio::awaitable { + if (!a.c->proxy_session) { + a.check_is_game(true); + } - if (a.c->config.check_flag(Client::Flag::INFINITE_HP_ENABLED)) { - a.c->config.clear_flag(Client::Flag::INFINITE_HP_ENABLED); + if (a.c->check_flag(Client::Flag::INFINITE_HP_ENABLED)) { + a.c->clear_flag(Client::Flag::INFINITE_HP_ENABLED); send_text_message(a.c, "$C6Infinite HP disabled"); } else { - a.check_cheats_enabled(s->cheat_flags.infinite_hp_tp); - a.c->config.set_flag(Client::Flag::INFINITE_HP_ENABLED); + auto s = a.c->require_server_state(); + a.check_cheats_enabled_or_allowed(s->cheat_flags.infinite_hp_tp); + a.c->set_flag(Client::Flag::INFINITE_HP_ENABLED); + co_await send_remove_negative_conditions(a.c); + if (a.c->proxy_session) { + send_remove_negative_conditions(a.c->proxy_session->server_channel, a.c->lobby_client_id); + } send_text_message(a.c, "$C6Infinite HP enabled"); - send_remove_negative_conditions(a.c); - } - }, - +[](const ProxyArgs& a) { - auto s = a.ses->require_server_state(); - - if (a.ses->config.check_flag(Client::Flag::INFINITE_HP_ENABLED)) { - a.ses->config.clear_flag(Client::Flag::INFINITE_HP_ENABLED); - send_text_message(a.ses->client_channel, "$C6Infinite HP disabled"); - } else { - a.check_cheats_allowed(s->cheat_flags.infinite_hp_tp); - a.ses->config.set_flag(Client::Flag::INFINITE_HP_ENABLED); - send_text_message(a.ses->client_channel, "$C6Infinite HP enabled"); - send_remove_negative_conditions(a.ses->client_channel, a.ses->lobby_client_id); - send_remove_negative_conditions(a.ses->server_channel, a.ses->lobby_client_id); } + co_return; }); ChatCommandDefinition cc_inftime( {"$inftime"}, - +[](const ServerArgs& a) { - auto l = a.c->require_lobby(); + +[](const Args& a) -> asio::awaitable { + // TODO: We could implement this in proxy sessions by rewriting the rules + // struct from the server in various 6xB4 commands. + a.check_is_proxy(false); a.check_is_game(true); a.check_is_ep3(true); a.check_is_leader(); + auto l = a.c->require_lobby(); if (l->episode != Episode::EP3) { throw logic_error("non-Ep3 client in Ep3 game"); } @@ -1036,127 +1186,95 @@ ChatCommandDefinition cc_inftime( l->ep3_server->options.behavior_flags ^= Episode3::BehaviorFlag::DISABLE_TIME_LIMITS; bool infinite_time_enabled = (l->ep3_server->options.behavior_flags & Episode3::BehaviorFlag::DISABLE_TIME_LIMITS); send_text_message(l, infinite_time_enabled ? "$C6Infinite time enabled" : "$C6Infinite time disabled"); - }, - unavailable_on_proxy_server); + co_return; + }); ChatCommandDefinition cc_inftp( {"$inftp"}, - +[](const ServerArgs& a) { - auto s = a.c->require_server_state(); - auto l = a.c->require_lobby(); - a.check_is_game(true); + +[](const Args& a) -> asio::awaitable { + if (!a.c->proxy_session) { + a.check_is_game(true); + } - if (a.c->config.check_flag(Client::Flag::INFINITE_TP_ENABLED)) { - a.c->config.clear_flag(Client::Flag::INFINITE_TP_ENABLED); + if (a.c->check_flag(Client::Flag::INFINITE_TP_ENABLED)) { + a.c->clear_flag(Client::Flag::INFINITE_TP_ENABLED); send_text_message(a.c, "$C6Infinite TP disabled"); } else { - a.check_cheats_enabled(s->cheat_flags.infinite_hp_tp); - a.c->config.set_flag(Client::Flag::INFINITE_TP_ENABLED); + auto s = a.c->require_server_state(); + a.check_cheats_enabled_or_allowed(s->cheat_flags.infinite_hp_tp); + a.c->set_flag(Client::Flag::INFINITE_TP_ENABLED); send_text_message(a.c, "$C6Infinite TP enabled"); } - }, - +[](const ProxyArgs& a) { - auto s = a.ses->require_server_state(); - - if (a.ses->config.check_flag(Client::Flag::INFINITE_TP_ENABLED)) { - a.ses->config.clear_flag(Client::Flag::INFINITE_TP_ENABLED); - send_text_message(a.ses->client_channel, "$C6Infinite TP disabled"); - } else { - a.check_cheats_allowed(s->cheat_flags.infinite_hp_tp); - a.ses->config.set_flag(Client::Flag::INFINITE_TP_ENABLED); - send_text_message(a.ses->client_channel, "$C6Infinite TP enabled"); - } + co_return; }); ChatCommandDefinition cc_item( {"$item", "$i"}, - +[](const ServerArgs& a) { - auto s = a.c->require_server_state(); - auto l = a.c->require_lobby(); + +[](const Args& a) -> asio::awaitable { a.check_is_game(true); - a.check_cheats_enabled(s->cheat_flags.create_items); + auto s = a.c->require_server_state(); + a.check_cheats_enabled_or_allowed(s->cheat_flags.create_items); - ItemData item = s->parse_item_description(a.c->version(), a.text); - item.id = l->generate_item_id(a.c->lobby_client_id); + ItemData item; + if (a.c->proxy_session) { + if (a.c->version() == Version::BB_V4) { + throw precondition_failed("$C6This command cannot\nbe used in proxy\nsessions in BB games"); + } + a.check_is_leader(); + + item = s->parse_item_description(a.c->version(), a.text); + item.id = phosg::random_object() | 0x80000000; - if ((l->drop_mode == Lobby::DropMode::SERVER_PRIVATE) || (l->drop_mode == Lobby::DropMode::SERVER_DUPLICATE)) { - l->add_item(a.c->floor, item, a.c->pos, nullptr, nullptr, (1 << a.c->lobby_client_id)); send_drop_stacked_item_to_channel(s, a.c->channel, item, a.c->floor, a.c->pos); + send_drop_stacked_item_to_channel(s, a.c->proxy_session->server_channel, item, a.c->floor, a.c->pos); + } else { - l->add_item(a.c->floor, item, a.c->pos, nullptr, nullptr, 0x00F); - send_drop_stacked_item_to_lobby(l, item, a.c->floor, a.c->pos); + auto l = a.c->require_lobby(); + ItemData item = s->parse_item_description(a.c->version(), a.text); + item.id = l->generate_item_id(a.c->lobby_client_id); + + if ((l->drop_mode == Lobby::DropMode::SERVER_PRIVATE) || (l->drop_mode == Lobby::DropMode::SERVER_DUPLICATE)) { + l->add_item(a.c->floor, item, a.c->pos, nullptr, nullptr, (1 << a.c->lobby_client_id)); + send_drop_stacked_item_to_channel(s, a.c->channel, item, a.c->floor, a.c->pos); + } else { + l->add_item(a.c->floor, item, a.c->pos, nullptr, nullptr, 0x00F); + send_drop_stacked_item_to_lobby(l, item, a.c->floor, a.c->pos); + } } string name = s->describe_item(a.c->version(), item, true); send_text_message(a.c, "$C7Item created:\n" + name); - }, - +[](const ProxyArgs& a) { - auto s = a.ses->require_server_state(); - a.check_cheats_allowed(s->cheat_flags.create_items); - if (a.ses->version() == Version::BB_V4) { - throw precondition_failed("$C6This command cannot\nbe used on the proxy\nserver in BB games"); - } - if (!a.ses->is_in_game) { - throw precondition_failed("$C6You must be in\na game to use this\ncommand"); - } - if (a.ses->lobby_client_id != a.ses->leader_client_id) { - throw precondition_failed("$C6You must be the\nleader to use this\ncommand"); - } - - bool set_drop = (!a.text.empty() && (a.text[0] == '!')); - - ItemData item = s->parse_item_description(a.ses->version(), (set_drop ? a.text.substr(1) : a.text)); - item.id = phosg::random_object() | 0x80000000; - - if (set_drop) { - a.ses->next_drop_item = item; - - string name = s->describe_item(a.ses->version(), item, true); - send_text_message(a.ses->client_channel, "$C7Next drop:\n" + name); - - } else { - send_drop_stacked_item_to_channel(s, a.ses->client_channel, item, a.ses->floor, a.ses->pos); - send_drop_stacked_item_to_channel(s, a.ses->server_channel, item, a.ses->floor, a.ses->pos); - - string name = s->describe_item(a.ses->version(), item, true); - send_text_message(a.ses->client_channel, "$C7Item created:\n" + name); - } + co_return; }); -static void command_item_notifs(Channel& ch, Client::Config& config, const std::string& text) { - if (text == "every" || text == "everything") { - config.set_drop_notification_mode(Client::ItemDropNotificationMode::ALL_ITEMS_INCLUDING_MESETA); - send_text_message_printf(ch, "$C6Notifications enabled\nfor all items and\nMeseta"); - } else if (text == "all" || text == "on") { - config.set_drop_notification_mode(Client::ItemDropNotificationMode::ALL_ITEMS); - send_text_message_printf(ch, "$C6Notifications enabled\nfor all items"); - } else if (text == "rare" || text == "rares") { - config.set_drop_notification_mode(Client::ItemDropNotificationMode::RARES_ONLY); - send_text_message_printf(ch, "$C6Notifications enabled\nfor rare items only"); - } else if (text == "none" || text == "off") { - config.set_drop_notification_mode(Client::ItemDropNotificationMode::NOTHING); - send_text_message_printf(ch, "$C6Notifications disabled\nfor all items"); - } else { - throw precondition_failed("$C6You must specify\n$C6off$C7, $C6rare$C7, $C6on$C7, or\n$C6everything$C7"); - } -} - ChatCommandDefinition cc_itemnotifs( {"$itemnotifs"}, - +[](const ServerArgs& a) { - command_item_notifs(a.c->channel, a.c->config, a.text); - }, - +[](const ProxyArgs& a) { - command_item_notifs(a.ses->client_channel, a.ses->config, a.text); + +[](const Args& a) -> asio::awaitable { + if (a.text == "every" || a.text == "everything") { + a.c->set_drop_notification_mode(Client::ItemDropNotificationMode::ALL_ITEMS_INCLUDING_MESETA); + send_text_message_fmt(a.c, "$C6Notifications enabled\nfor all items and\nMeseta"); + } else if (a.text == "all" || a.text == "on") { + a.c->set_drop_notification_mode(Client::ItemDropNotificationMode::ALL_ITEMS); + send_text_message_fmt(a.c, "$C6Notifications enabled\nfor all items"); + } else if (a.text == "rare" || a.text == "rares") { + a.c->set_drop_notification_mode(Client::ItemDropNotificationMode::RARES_ONLY); + send_text_message_fmt(a.c, "$C6Notifications enabled\nfor rare items only"); + } else if (a.text == "none" || a.text == "off") { + a.c->set_drop_notification_mode(Client::ItemDropNotificationMode::NOTHING); + send_text_message_fmt(a.c, "$C6Notifications disabled\nfor all items"); + } else { + throw precondition_failed("$C6You must specify\n$C6off$C7, $C6rare$C7, $C6on$C7, or\n$C6everything$C7"); + } + co_return; }); ChatCommandDefinition cc_kick( {"$kick"}, - +[](const ServerArgs& a) { - auto s = a.c->require_server_state(); - auto l = a.c->require_lobby(); + +[](const Args& a) -> asio::awaitable { + a.check_is_proxy(false); a.check_account_flag(Account::Flag::KICK_USER); + auto s = a.c->require_server_state(); auto target = s->find_client(&a.text); if (!target->login) { // This should be impossible, but I'll bet it's not actually @@ -1175,14 +1293,16 @@ ChatCommandDefinition cc_kick( send_message_box(target, "$C6You have been kicked off the server."); string target_name = name_for_client(target); - s->game_server->disconnect_client(target); - send_text_message_printf(a.c, "$C6%s kicked off", target_name.c_str()); - }, - unavailable_on_proxy_server); + target->channel->disconnect(); + send_text_message_fmt(a.c, "$C6{} kicked off", target_name); + co_return; + }); ChatCommandDefinition cc_killcount( {"$killcount"}, - +[](const ServerArgs& a) { + +[](const Args& a) -> asio::awaitable { + a.check_is_proxy(false); + auto p = a.c->character(); size_t item_index; try { @@ -1203,156 +1323,162 @@ ChatCommandDefinition cc_killcount( // non-BB versions, the kill count is accurate at all times in the lobby // (since kills can't occur there), or at the beginning of a game. if ((a.c->version() == Version::BB_V4) || !a.c->require_lobby()->is_game()) { - send_text_message_printf(a.c, "%hu kills", item.data.get_kill_count()); + send_text_message_fmt(a.c, "{} kills", item.data.get_kill_count()); } else { - send_text_message_printf(a.c, "%hu kills as of\ngame join", item.data.get_kill_count()); + send_text_message_fmt(a.c, "{} kills as of\ngame join", item.data.get_kill_count()); } - }, - unavailable_on_proxy_server); + co_return; + }); ChatCommandDefinition cc_lobby_info( {"$li"}, - +[](const ServerArgs& a) -> void { - vector lines; + +[](const Args& a) -> asio::awaitable { + if (a.c->proxy_session) { + string msg; + // On non-masked-GC sessions (BB), there is no remote Guild Card number, so we + // don't show it. (The user can see it in the pause menu, unlike in masked-GC + // sessions like GC.) + if (a.c->proxy_session->remote_guild_card_number >= 0) { + msg = std::format("$C7GC: $C6{}$C7\n", a.c->proxy_session->remote_guild_card_number); + } + msg += "Slots: "; - auto l = a.c->lobby.lock(); - if (!l) { - lines.emplace_back("$C4No lobby info"); + for (size_t z = 0; z < a.c->proxy_session->lobby_players.size(); z++) { + bool is_self = (z == a.c->lobby_client_id); + bool is_leader = (z == a.c->proxy_session->leader_client_id); + if (a.c->proxy_session->lobby_players[z].guild_card_number == 0) { + msg += std::format("$C0{:X}$C7", z); + } else if (is_self && is_leader) { + msg += std::format("$C6{:X}$C7", z); + } else if (is_self) { + msg += std::format("$C2{:X}$C7", z); + } else if (is_leader) { + msg += std::format("$C4{:X}$C7", z); + } else { + msg += std::format("{:X}", z); + } + } + + vector cheats_tokens; + if (a.c->check_flag(Client::Flag::INFINITE_HP_ENABLED)) { + cheats_tokens.emplace_back("HP"); + } + if (a.c->check_flag(Client::Flag::INFINITE_TP_ENABLED)) { + cheats_tokens.emplace_back("TP"); + } + if (!cheats_tokens.empty()) { + msg += "\n$C7Cheats: $C6"; + msg += phosg::join(cheats_tokens, ","); + } + + vector behaviors_tokens; + if (a.c->check_flag(Client::Flag::SWITCH_ASSIST_ENABLED)) { + behaviors_tokens.emplace_back("SWA"); + } + if (a.c->check_flag(Client::Flag::PROXY_SAVE_FILES)) { + behaviors_tokens.emplace_back("SF"); + } + if (a.c->check_flag(Client::Flag::PROXY_BLOCK_FUNCTION_CALLS)) { + behaviors_tokens.emplace_back("BF"); + } + if (!behaviors_tokens.empty()) { + msg += "\n$C7Flags: $C6"; + msg += phosg::join(behaviors_tokens, ","); + } + + if (a.c->override_section_id != 0xFF) { + msg += "\n$C7SecID*: $C6"; + msg += name_for_section_id(a.c->override_section_id); + } + + send_text_message(a.c->channel, msg); + + } else { // Not proxy session + vector lines; + + auto l = a.c->lobby.lock(); + if (!l) { + lines.emplace_back("$C4No lobby info"); + + } else { + if (l->is_game()) { + if (!l->is_ep3()) { + if (l->max_level == 0xFFFFFFFF) { + lines.emplace_back(std::format("$C6{:08X}$C7 L$C6{}+$C7", l->lobby_id, l->min_level + 1)); + } else { + lines.emplace_back(std::format( + "$C6{:08X}$C7 L$C6{}-{}$C7", l->lobby_id, l->min_level + 1, l->max_level + 1)); + } + lines.emplace_back(std::format( + "$C7Section ID: $C6{}$C7", name_for_section_id(l->effective_section_id()))); + + switch (l->drop_mode) { + case Lobby::DropMode::DISABLED: + lines.emplace_back("Drops disabled"); + break; + case Lobby::DropMode::CLIENT: + lines.emplace_back("Client item table"); + break; + case Lobby::DropMode::SERVER_SHARED: + lines.emplace_back("Server item table"); + break; + case Lobby::DropMode::SERVER_PRIVATE: + lines.emplace_back("Server indiv items"); + break; + case Lobby::DropMode::SERVER_DUPLICATE: + lines.emplace_back("Server dup items"); + break; + default: + lines.emplace_back("$C4Unknown drop mode$C7"); + } + if (l->check_flag(Lobby::Flag::CHEATS_ENABLED)) { + lines.emplace_back("Cheats enabled"); + } - } else { - if (l->is_game()) { - if (!l->is_ep3()) { - if (l->max_level == 0xFFFFFFFF) { - lines.emplace_back(phosg::string_printf("$C6%08X$C7 L$C6%d+$C7", l->lobby_id, l->min_level + 1)); } else { - lines.emplace_back(phosg::string_printf("$C6%08X$C7 L$C6%d-%d$C7", l->lobby_id, l->min_level + 1, l->max_level + 1)); - } - lines.emplace_back(phosg::string_printf("$C7Section ID: $C6%s$C7", name_for_section_id(l->effective_section_id()))); - - switch (l->drop_mode) { - case Lobby::DropMode::DISABLED: - lines.emplace_back("Drops disabled"); - break; - case Lobby::DropMode::CLIENT: - lines.emplace_back("Client item table"); - break; - case Lobby::DropMode::SERVER_SHARED: - lines.emplace_back("Server item table"); - break; - case Lobby::DropMode::SERVER_PRIVATE: - lines.emplace_back("Server indiv items"); - break; - case Lobby::DropMode::SERVER_DUPLICATE: - lines.emplace_back("Server dup items"); - break; - default: - lines.emplace_back("$C4Unknown drop mode$C7"); - } - if (l->check_flag(Lobby::Flag::CHEATS_ENABLED)) { - lines.emplace_back("Cheats enabled"); + char type_ch = dynamic_pointer_cast(l->rand_crypt).get() + ? 'X' + : dynamic_pointer_cast(l->rand_crypt).get() + ? 'M' + : dynamic_pointer_cast(l->rand_crypt).get() + ? 'L' + : '?'; + lines.emplace_back(std::format("$C7State seed: $C6{:08X}/{:c}$C7", l->random_seed, type_ch)); } } else { - lines.emplace_back(phosg::string_printf("$C7State seed: $C6%08X$C7", l->random_seed)); + lines.emplace_back(std::format("$C7Lobby ID: $C6{:08X}$C7", l->lobby_id)); } - } else { - lines.emplace_back(phosg::string_printf("$C7Lobby ID: $C6%08X$C7", l->lobby_id)); - } - - string slots_str = "Slots: "; - for (size_t z = 0; z < l->clients.size(); z++) { - if (!l->clients[z]) { - slots_str += phosg::string_printf("$C0%zX$C7", z); - } else { - bool is_self = (l->clients[z] == a.c); - bool is_leader = (z == l->leader_id); - if (is_self && is_leader) { - slots_str += phosg::string_printf("$C6%zX$C7", z); - } else if (is_self) { - slots_str += phosg::string_printf("$C2%zX$C7", z); - } else if (is_leader) { - slots_str += phosg::string_printf("$C4%zX$C7", z); + string slots_str = "Slots: "; + for (size_t z = 0; z < l->clients.size(); z++) { + if (!l->clients[z]) { + slots_str += std::format("$C0{:X}$C7", z); } else { - slots_str += phosg::string_printf("%zX", z); + bool is_self = (l->clients[z] == a.c); + bool is_leader = (z == l->leader_id); + if (is_self && is_leader) { + slots_str += std::format("$C6{:X}$C7", z); + } else if (is_self) { + slots_str += std::format("$C2{:X}$C7", z); + } else if (is_leader) { + slots_str += std::format("$C4{:X}$C7", z); + } else { + slots_str += std::format("{:X}", z); + } } } + lines.emplace_back(std::move(slots_str)); } - lines.emplace_back(std::move(slots_str)); - } - send_text_message(a.c, phosg::join(lines, "\n")); - }, - +[](const ProxyArgs& a) -> void { - string msg; - // On non-masked-GC sessions (BB), there is no remote Guild Card number, so we - // don't show it. (The user can see it in the pause menu, unlike in masked-GC - // sessions like GC.) - if (a.ses->remote_guild_card_number >= 0) { - msg = phosg::string_printf("$C7GC: $C6%" PRId64 "$C7\n", a.ses->remote_guild_card_number); + send_text_message(a.c, phosg::join(lines, "\n")); } - msg += "Slots: "; - - for (size_t z = 0; z < a.ses->lobby_players.size(); z++) { - bool is_self = (z == a.ses->lobby_client_id); - bool is_leader = (z == a.ses->leader_client_id); - if (a.ses->lobby_players[z].guild_card_number == 0) { - msg += phosg::string_printf("$C0%zX$C7", z); - } else if (is_self && is_leader) { - msg += phosg::string_printf("$C6%zX$C7", z); - } else if (is_self) { - msg += phosg::string_printf("$C2%zX$C7", z); - } else if (is_leader) { - msg += phosg::string_printf("$C4%zX$C7", z); - } else { - msg += phosg::string_printf("%zX", z); - } - } - - vector cheats_tokens; - if (a.ses->config.check_flag(Client::Flag::INFINITE_HP_ENABLED)) { - cheats_tokens.emplace_back("HP"); - } - if (a.ses->config.check_flag(Client::Flag::INFINITE_TP_ENABLED)) { - cheats_tokens.emplace_back("TP"); - } - if (!cheats_tokens.empty()) { - msg += "\n$C7Cheats: $C6"; - msg += phosg::join(cheats_tokens, ","); - } - - vector behaviors_tokens; - if (a.ses->config.check_flag(Client::Flag::SWITCH_ASSIST_ENABLED)) { - behaviors_tokens.emplace_back("SWA"); - } - if (a.ses->config.check_flag(Client::Flag::PROXY_SAVE_FILES)) { - behaviors_tokens.emplace_back("SF"); - } - if (a.ses->config.check_flag(Client::Flag::PROXY_SUPPRESS_REMOTE_LOGIN)) { - behaviors_tokens.emplace_back("SL"); - } - if (a.ses->config.check_flag(Client::Flag::PROXY_BLOCK_FUNCTION_CALLS)) { - behaviors_tokens.emplace_back("BF"); - } - if (!behaviors_tokens.empty()) { - msg += "\n$C7Flags: $C6"; - msg += phosg::join(behaviors_tokens, ","); - } - - if (a.ses->config.override_section_id != 0xFF) { - msg += "\n$C7SecID*: $C6"; - msg += name_for_section_id(a.ses->config.override_section_id); - } - - send_text_message(a.ses->client_channel, msg); + co_return; }); ChatCommandDefinition cc_ln( {"$ln"}, - +[](const ServerArgs& a) -> void { - auto l = a.c->require_lobby(); - a.check_is_game(false); - + +[](const Args& a) -> asio::awaitable { uint8_t new_type; if (a.text.empty()) { new_type = 0x80; @@ -1363,30 +1489,26 @@ ChatCommandDefinition cc_ln( } } - a.c->config.override_lobby_number = new_type; - send_join_lobby(a.c, l); - }, - +[](const ProxyArgs& a) { - uint8_t new_type; - if (a.text.empty()) { - new_type = 0x80; + if (a.c->proxy_session) { + a.c->override_lobby_number = new_type; } else { - new_type = lobby_type_for_name(a.text); - if (new_type == 0x80) { - throw precondition_failed("$C6No such lobby type"); - } + a.check_is_game(false); + a.c->override_lobby_number = new_type; + send_join_lobby(a.c, a.c->require_lobby()); } - a.ses->config.override_lobby_number = new_type; + co_return; }); ChatCommandDefinition cc_loadchar( {"$loadchar"}, - +[](const ServerArgs& a) -> void { + +[](const Args& a) -> asio::awaitable { + a.check_is_proxy(false); + a.check_is_game(false); if (a.check_permissions && a.c->login->account->check_flag(Account::Flag::IS_SHARED_ACCOUNT)) { throw precondition_failed("$C7This command cannot\nbe used on a shared\naccount"); } + auto l = a.c->require_lobby(); - a.check_is_game(false); size_t index = stoull(a.text, nullptr, 0) - 1; if (index > 15) { @@ -1415,54 +1537,41 @@ ChatCommandDefinition cc_loadchar( (a.c->version() == Version::XB_V3)) { // TODO: Support extended player info on other versions auto s = a.c->require_server_state(); - if (!a.c->config.check_flag(Client::Flag::HAS_SEND_FUNCTION_CALL) || - !a.c->config.check_flag(Client::Flag::SEND_FUNCTION_CALL_ACTUALLY_RUNS_CODE)) { - throw precondition_failed_printf("Can\'t load character\ndata on this game\nversion"); + if (!a.c->check_flag(Client::Flag::HAS_SEND_FUNCTION_CALL) || + !a.c->check_flag(Client::Flag::SEND_FUNCTION_CALL_ACTUALLY_RUNS_CODE)) { + throw precondition_failed("Can\'t load character\ndata on this game\nversion"); } - auto send_set_extended_player_info = [](shared_ptr c, const CharT& char_file) -> void { - prepare_client_for_patches(c, [wc = weak_ptr(c), char_file]() { - auto c = wc.lock(); - if (!c) { - return; + auto send_set_extended_player_info = [&a, &s](const CharT& char_file) -> asio::awaitable { + co_await prepare_client_for_patches(a.c); + try { + auto fn = s->function_code_index->get_patch("SetExtendedPlayerInfo", a.c->specific_version); + co_await send_function_call(a.c, fn, {}, &char_file, sizeof(CharT)); + auto l = a.c->lobby.lock(); + if (l) { + send_player_leave_notification(l, a.c->lobby_client_id); + s->send_lobby_join_notifications(l, a.c); } - 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, sizeof(CharT)); - c->function_call_response_queue.emplace_back([wc = weak_ptr(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(); - send_player_leave_notification(l, c->lobby_client_id); - s->send_lobby_join_notifications(l, c); - } - }); - } catch (const exception& e) { - c->log.warning("Failed to set extended player info: %s", e.what()); - throw precondition_failed_printf("Failed to set\nplayer info:\n%s", e.what()); - } - }); + } catch (const exception& e) { + a.c->log.warning_f("Failed to set extended player info: {}", e.what()); + throw precondition_failed("Failed to set\nplayer info:\n{}", e.what()); + } }; if (a.c->version() == Version::DC_V2) { PSODCV2CharacterFile::Character dc_char = *a.c->character(); - send_set_extended_player_info(a.c, dc_char); + co_await send_set_extended_player_info(dc_char); } else if (a.c->version() == Version::GC_NTE) { PSOGCNTECharacterFileCharacter gc_char = *a.c->character(); - send_set_extended_player_info(a.c, gc_char); + co_await send_set_extended_player_info(gc_char); } else if (a.c->version() == Version::GC_V3) { PSOGCCharacterFile::Character gc_char = *a.c->character(); - send_set_extended_player_info(a.c, gc_char); + co_await send_set_extended_player_info(gc_char); } else if (a.c->version() == Version::GC_EP3_NTE) { PSOGCEp3NTECharacter nte_char = *ep3_char; - send_set_extended_player_info(a.c, nte_char); + co_await send_set_extended_player_info(nte_char); } else if (a.c->version() == Version::GC_EP3) { - send_set_extended_player_info(a.c, *ep3_char); + co_await send_set_extended_player_info(*ep3_char); } else if (a.c->version() == Version::XB_V3) { if (!a.c->login || !a.c->login->xb_license) { throw runtime_error("XB client is not logged in"); @@ -1470,7 +1579,7 @@ ChatCommandDefinition cc_loadchar( PSOXBCharacterFileCharacter xb_char = *a.c->character(); xb_char.guild_card.xb_user_id_high = (a.c->login->xb_license->user_id >> 32) & 0xFFFFFFFF; xb_char.guild_card.xb_user_id_low = a.c->login->xb_license->user_id & 0xFFFFFFFF; - send_set_extended_player_info(a.c, xb_char); + co_await send_set_extended_player_info(xb_char); } else { throw logic_error("unimplemented extended player info version"); } @@ -1482,19 +1591,21 @@ ChatCommandDefinition cc_loadchar( send_player_leave_notification(l, a.c->lobby_client_id); s->send_lobby_join_notifications(l, a.c); } - }, - unavailable_on_proxy_server); + co_return; + }); ChatCommandDefinition cc_matcount( {"$matcount"}, - +[](const ServerArgs& a) -> void { + +[](const Args& a) -> asio::awaitable { + a.check_is_proxy(false); + auto p = a.c->character(); if (is_v1_or_v2(a.c->version())) { - send_text_message_printf(a.c, "%hhu HP, %hhu TP", + send_text_message_fmt(a.c, "{} HP, {} TP", p->get_material_usage(PSOBBCharacterFile::MaterialType::HP), p->get_material_usage(PSOBBCharacterFile::MaterialType::TP)); } else { - send_text_message_printf(a.c, "%hhu HP, %hhu TP, %hhu POW\n%hhu MIND, %hhu EVADE\n%hhu DEF, %hhu LUCK", + send_text_message_fmt(a.c, "{} HP, {} TP, {} POW\n{} MIND, {} EVADE\n{} DEF, {} LUCK", p->get_material_usage(PSOBBCharacterFile::MaterialType::HP), p->get_material_usage(PSOBBCharacterFile::MaterialType::TP), p->get_material_usage(PSOBBCharacterFile::MaterialType::POWER), @@ -1503,16 +1614,17 @@ ChatCommandDefinition cc_matcount( p->get_material_usage(PSOBBCharacterFile::MaterialType::DEF), p->get_material_usage(PSOBBCharacterFile::MaterialType::LUCK)); } - }, - unavailable_on_proxy_server); + co_return; + }); ChatCommandDefinition cc_maxlevel( {"$maxlevel"}, - +[](const ServerArgs& a) -> void { - auto l = a.c->require_lobby(); + +[](const Args& a) -> asio::awaitable { + a.check_is_proxy(false); a.check_is_game(true); a.check_is_leader(); + auto l = a.c->require_lobby(); l->max_level = stoull(a.text) - 1; if (l->max_level >= 200) { l->max_level = 0xFFFFFFFF; @@ -1521,70 +1633,61 @@ ChatCommandDefinition cc_maxlevel( if (l->max_level == 0xFFFFFFFF) { send_text_message(l, "$C6Maximum level set\nto unlimited"); } else { - send_text_message_printf(l, "$C6Maximum level set\nto %" PRIu32, l->max_level + 1); + send_text_message_fmt(l, "$C6Maximum level set\nto {}", l->max_level + 1); } - }, - unavailable_on_proxy_server); + co_return; + }); ChatCommandDefinition cc_minlevel( {"$minlevel"}, - +[](const ServerArgs& a) -> void { - auto l = a.c->require_lobby(); + +[](const Args& a) -> asio::awaitable { + a.check_is_proxy(false); a.check_is_game(true); a.check_is_leader(); size_t new_min_level = stoull(a.text) - 1; + auto l = a.c->require_lobby(); auto s = a.c->require_server_state(); bool cheats_allowed = (l->check_flag(Lobby::Flag::CHEATS_ENABLED) || a.c->login->account->check_flag(Account::Flag::CHEAT_ANYWHERE)); if (!cheats_allowed && s->cheat_flags.insufficient_minimum_level) { size_t default_min_level = s->default_min_level_for_game(a.c->version(), l->episode, l->difficulty); if (new_min_level < default_min_level) { - throw precondition_failed_printf("$C6Cannot set minimum\nlevel below %zu", default_min_level + 1); + throw precondition_failed("$C6Cannot set minimum\nlevel below {}", default_min_level + 1); } } l->min_level = new_min_level; - send_text_message_printf(l, "$C6Minimum level set\nto %" PRIu32, l->min_level + 1); - }, - unavailable_on_proxy_server); + send_text_message_fmt(l, "$C6Minimum level set\nto {}", l->min_level + 1); + co_return; + }); ChatCommandDefinition cc_next( {"$next"}, - +[](const ServerArgs& a) -> void { - auto s = a.c->require_server_state(); - auto l = a.c->require_lobby(); + +[](const Args& a) -> asio::awaitable { a.check_is_game(true); - a.check_cheats_enabled(s->cheat_flags.warp); + auto s = a.c->require_server_state(); + a.check_cheats_enabled_or_allowed(s->cheat_flags.warp); - size_t limit = FloorDefinition::limit_for_episode(l->episode); - if (limit == 0) { - return; + auto episode = a.c->proxy_session + ? a.c->proxy_session->lobby_episode + : a.c->require_lobby()->episode; + size_t limit = FloorDefinition::limit_for_episode(episode); + if (limit > 0) { + send_warp(a.c, (a.c->floor + 1) % limit, true); } - send_warp(a.c, (a.c->floor + 1) % limit, true); - }, - +[](const ProxyArgs& a) { - auto s = a.ses->require_server_state(); - a.check_cheats_allowed(s->cheat_flags.warp); - if (!a.ses->is_in_game) { - throw precondition_failed("$C6You must be in a\ngame to use this\ncommand"); - } - - size_t limit = FloorDefinition::limit_for_episode(a.ses->lobby_episode); - if (limit == 0) { - return; - } - send_warp(a.ses->client_channel, a.ses->lobby_client_id, (a.ses->floor + 1) % limit, true); + co_return; }); ChatCommandDefinition cc_password( {"$password"}, - +[](const ServerArgs& a) -> void { - auto l = a.c->require_lobby(); + +[](const Args& a) -> asio::awaitable { + a.check_is_proxy(false); a.check_is_game(true); a.check_is_leader(); + auto l = a.c->require_lobby(); if (a.text.empty()) { l->password.clear(); send_text_message(l, "$C6Game unlocked"); @@ -1592,104 +1695,43 @@ ChatCommandDefinition cc_password( } else { l->password = a.text; string escaped = remove_color(l->password); - send_text_message_printf(l, "$C6Game password:\n%s", escaped.c_str()); + send_text_message_fmt(l, "$C6Game password:\n{}", escaped); } - }, - unavailable_on_proxy_server); - -struct PatchCommandArgs { - string patch_name; - unordered_map label_writes; - - PatchCommandArgs(const string& args) { - auto tokens = phosg::split(args, ' '); - if (tokens.empty()) { - throw runtime_error("not enough arguments"); - } - this->patch_name = std::move(tokens[0]); - for (size_t z = 0; z < tokens.size() - 1; z++) { - this->label_writes.emplace(phosg::string_printf("arg%zu", z), stoul(tokens[z + 1], nullptr, 0)); - } - } -}; + co_return; + }); ChatCommandDefinition cc_patch( {"$patch"}, - +[](const ServerArgs& a) -> void { - PatchCommandArgs pca(a.text); - - prepare_client_for_patches(a.c, [wc = weak_ptr(a.c), pca]() { - auto c = wc.lock(); - if (!c) { - return; - } - try { - auto s = c->require_server_state(); - // Note: We can't look this up outside of the closure because - // c->specific_version can change during prepare_client_for_patches - auto fn = s->function_code_index->get_patch(pca.patch_name, c->config.specific_version); - send_function_call(c, fn, pca.label_writes); - c->function_call_response_queue.emplace_back(empty_function_call_response_handler); - } catch (const out_of_range&) { - send_text_message(c, "$C6Invalid patch name"); - } - }); - }, - +[](const ProxyArgs& a) { - PatchCommandArgs pca(a.text); - - auto send_call = [ses = a.ses, pca](uint32_t specific_version, uint32_t) { - try { - if (ses->config.specific_version != specific_version) { - ses->config.specific_version = specific_version; - ses->log.info("Version detected as %08" PRIX32, ses->config.specific_version); - } - auto s = ses->require_server_state(); - auto fn = s->function_code_index->get_patch(pca.patch_name, ses->config.specific_version); - send_function_call(ses->client_channel, ses->config, fn, pca.label_writes); - // Don't forward the patch response to the server - ses->function_call_return_handler_queue.emplace_back(empty_function_call_response_handler); - } catch (const out_of_range&) { - send_text_message(ses->client_channel, "$C6Invalid patch name"); - } - }; - - auto send_version_detect_or_send_call = [ses = a.ses, send_call]() { - bool is_gc = ::is_gc(ses->version()); - bool is_xb = (ses->version() == Version::XB_V3); - if ((is_gc || is_xb) && specific_version_is_indeterminate(ses->config.specific_version)) { - auto s = ses->require_server_state(); - send_function_call( - ses->client_channel, - ses->config, - s->function_code_index->name_to_function.at(is_xb ? "VersionDetectXB" : "VersionDetectGC")); - ses->function_call_return_handler_queue.emplace_back(send_call); - } else { - send_call(ses->config.specific_version, 0); - } - }; - - // This mirrors the implementation in prepare_client_for_patches - if (!a.ses->config.check_flag(Client::Flag::SEND_FUNCTION_CALL_NO_CACHE_PATCH)) { - auto s = a.ses->require_server_state(); - send_function_call( - a.ses->client_channel, a.ses->config, s->function_code_index->name_to_function.at("CacheClearFix-Phase1"), {}, "", 0, 0, 0x7F2734EC); - a.ses->function_call_return_handler_queue.emplace_back([s, ses = a.ses, send_version_detect_or_send_call](uint32_t, uint32_t) -> void { - send_function_call( - ses->client_channel, ses->config, s->function_code_index->name_to_function.at("CacheClearFix-Phase2")); - ses->function_call_return_handler_queue.emplace_back([ses, send_version_detect_or_send_call](uint32_t, uint32_t) -> void { - ses->config.set_flag(Client::Flag::SEND_FUNCTION_CALL_NO_CACHE_PATCH); - send_version_detect_or_send_call(); - }); - }); - } else { - send_version_detect_or_send_call(); + +[](const Args& a) -> asio::awaitable { + auto tokens = phosg::split(a.text, ' '); + if (tokens.empty()) { + throw runtime_error("not enough arguments"); } + + string patch_name = std::move(tokens[0]); + unordered_map label_writes; + for (size_t z = 0; z < tokens.size() - 1; z++) { + label_writes.emplace(std::format("arg{}", z), stoul(tokens[z + 1], nullptr, 0)); + } + + co_await prepare_client_for_patches(a.c); + try { + auto s = a.c->require_server_state(); + // Note: We can't look this up before prepare_client_for_patches + // because specific_version may not be set at that point + auto fn = s->function_code_index->get_patch(patch_name, a.c->specific_version); + co_await send_function_call(a.c, fn, label_writes); + } catch (const out_of_range&) { + send_text_message(a.c, "$C6Invalid patch name"); + } + co_return; }); ChatCommandDefinition cc_persist( {"$persist"}, - +[](const ServerArgs& a) -> void { + +[](const Args& a) -> asio::awaitable { + a.check_is_proxy(false); + auto l = a.c->require_lobby(); if (l->check_flag(Lobby::Flag::DEFAULT)) { throw precondition_failed("$C6Default lobbies\ncannot be marked\ntemporary"); @@ -1701,24 +1743,25 @@ ChatCommandDefinition cc_persist( throw precondition_failed("$C6Spectator teams\ncannot be marked\npersistent"); } else { l->toggle_flag(Lobby::Flag::PERSISTENT); - send_text_message_printf(l, "Lobby persistence\n%s", l->check_flag(Lobby::Flag::PERSISTENT) ? "enabled" : "disabled"); + send_text_message_fmt(l, "Lobby persistence\n{}", l->check_flag(Lobby::Flag::PERSISTENT) ? "enabled" : "disabled"); } - }, - unavailable_on_proxy_server); + co_return; + }); ChatCommandDefinition cc_ping( {"$ping"}, - +[](const ServerArgs& a) -> void { + +[](const Args& a) -> asio::awaitable { a.c->ping_start_time = phosg::now(); send_command(a.c, 0x1D, 0x00); - }, - +[](const ProxyArgs& a) { - a.ses->client_ping_start_time = phosg::now(); - a.ses->server_ping_start_time = a.ses->client_ping_start_time; - - C_GuildCardSearch_40 cmd = {0x00010000, a.ses->remote_guild_card_number, a.ses->remote_guild_card_number}; - a.ses->client_channel.send(0x1D, 0x00); - a.ses->server_channel.send(0x40, 0x00, &cmd, sizeof(cmd)); + if (a.c->proxy_session) { + a.c->proxy_session->server_ping_start_time = a.c->ping_start_time; + C_GuildCardSearch_40 cmd = { + 0x00010000, + a.c->proxy_session->remote_guild_card_number, + a.c->proxy_session->remote_guild_card_number}; + a.c->proxy_session->server_channel->send(0x40, 0x00, &cmd, sizeof(cmd)); + } + co_return; }); static string file_path_for_recording(const std::string& args, uint32_t account_id) { @@ -1727,15 +1770,14 @@ static string file_path_for_recording(const std::string& args, uint32_t account_ throw runtime_error("invalid recording name"); } } - return phosg::string_printf("system/ep3/battle-records/%010" PRIu32 "_%s.mzrd", account_id, args.c_str()); + return std::format("system/ep3/battle-records/{:010}_{}.mzrd", account_id, args); } ChatCommandDefinition cc_playrec( {"$playrec"}, - +[](const ServerArgs& a) -> void { - if (!is_ep3(a.c->version())) { - throw precondition_failed("$C4This command can\nonly be used on\nEpisode 3"); - } + +[](const Args& a) -> asio::awaitable { + a.check_is_proxy(false); + a.check_is_ep3(true); auto l = a.c->require_lobby(); if (l->is_game() && l->battle_player) { @@ -1757,7 +1799,7 @@ ChatCommandDefinition cc_playrec( throw precondition_failed("$C4The recording does\nnot exist"); } auto record = make_shared(data); - auto battle_player = make_shared(record, s->game_server->get_base()); + auto battle_player = make_shared(s->io_context, record); auto game = create_game_generic( s, a.c, a.text, "", Episode::EP3, GameMode::NORMAL, 0, false, nullptr, battle_player); if (game) { @@ -1765,128 +1807,112 @@ ChatCommandDefinition cc_playrec( game->set_flag(Lobby::Flag::START_BATTLE_PLAYER_IMMEDIATELY); } s->change_client_lobby(a.c, game); - a.c->config.set_flag(Client::Flag::LOADING); - a.c->log.info("LOADING flag set"); + a.c->set_flag(Client::Flag::LOADING); + a.c->log.info_f("LOADING flag set"); } } else { throw precondition_failed("$C4This command cannot\nbe used in a game"); } - }, - unavailable_on_proxy_server); + co_return; + }); ChatCommandDefinition cc_qcall( {"$qcall"}, - +[](const ServerArgs& a) -> void { + +[](const Args& a) -> asio::awaitable { a.check_debug_enabled(); + a.check_is_game(true); auto l = a.c->require_lobby(); if (l->is_game() && l->quest) { send_quest_function_call(a.c, stoul(a.text, nullptr, 0)); - } else { - throw precondition_failed("$C6You must be in a\nquest to use this\ncommand"); - } - }, - +[](const ProxyArgs& a) { - if (a.ses->is_in_game && a.ses->is_in_quest) { - send_quest_function_call(a.ses->client_channel, stoul(a.text, nullptr, 0)); - } else { - throw precondition_failed("$C6You must be in a\nquest to use this\ncommand"); } + co_return; }); ChatCommandDefinition cc_qcheck( {"$qcheck"}, - +[](const ServerArgs& a) -> void { + +[](const Args& a) -> asio::awaitable { + a.check_is_proxy(false); + auto l = a.c->require_lobby(); uint16_t flag_num = stoul(a.text, nullptr, 0); if (l->is_game()) { if (!l->quest_flags_known || l->quest_flags_known->get(l->difficulty, flag_num)) { - send_text_message_printf(a.c, "$C7Game: flag 0x%hX (%hu)\nis %s on %s", + send_text_message_fmt(a.c, "$C7Game: flag 0x{:X} ({})\nis {} on {}", flag_num, flag_num, a.c->character()->quest_flags.get(l->difficulty, flag_num) ? "set" : "not set", name_for_difficulty(l->difficulty)); } else { - send_text_message_printf(a.c, "$C7Game: flag 0x%hX (%hu)\nis unknown on %s", + send_text_message_fmt(a.c, "$C7Game: flag 0x{:X} ({})\nis unknown on {}", flag_num, flag_num, name_for_difficulty(l->difficulty)); } } else if (a.c->version() == Version::BB_V4) { - send_text_message_printf(a.c, "$C7Player: flag 0x%hX (%hu)\nis %s on %s", + send_text_message_fmt(a.c, "$C7Player: flag 0x{:X} ({})\nis {} on {}", flag_num, flag_num, a.c->character()->quest_flags.get(l->difficulty, flag_num) ? "set" : "not set", name_for_difficulty(l->difficulty)); } - }, - unavailable_on_proxy_server); - -static void server_command_qset_qclear(const ServerArgs& a, bool should_set) { - a.check_debug_enabled(); - - auto l = a.c->require_lobby(); - if (!l->is_game()) { - throw precondition_failed("$C6This command cannot\nbe used in the lobby"); - } + co_return; + }); +static void command_qset_qclear(const Args& a, bool should_set) { + a.check_is_game(true); uint16_t flag_num = stoul(a.text, nullptr, 0); - if (l->is_game()) { - if (l->quest_flags_known) { - l->quest_flags_known->set(l->difficulty, flag_num); - } - if (should_set) { - l->quest_flag_values->set(l->difficulty, flag_num); - } else { - l->quest_flag_values->clear(l->difficulty, flag_num); - } - } + if (!a.c->proxy_session) { + a.check_debug_enabled(); - auto p = a.c->character(false); - if (p) { - if (should_set) { - p->quest_flags.set(l->difficulty, flag_num); - } else { - p->quest_flags.clear(l->difficulty, flag_num); + auto l = a.c->require_lobby(); + if (l->is_game()) { + if (l->quest_flags_known) { + l->quest_flags_known->set(l->difficulty, flag_num); + } + if (should_set) { + l->quest_flag_values->set(l->difficulty, flag_num); + } else { + l->quest_flag_values->clear(l->difficulty, flag_num); + } + } + + auto p = a.c->character(false); + if (p) { + if (should_set) { + p->quest_flags.set(l->difficulty, flag_num); + } else { + p->quest_flags.clear(l->difficulty, flag_num); + } } } if (is_v1_or_v2(a.c->version())) { G_UpdateQuestFlag_DC_PC_6x75 cmd = {{0x75, 0x02, 0x0000}, flag_num, should_set ? 0 : 1}; - send_command_t(l, 0x60, 0x00, cmd); + a.c->channel->send(0x60, 0x00, &cmd, sizeof(cmd)); + if (a.c->proxy_session) { + a.c->proxy_session->server_channel->send(0x60, 0x00, &cmd, sizeof(cmd)); + } } else { - G_UpdateQuestFlag_V3_BB_6x75 cmd = {{{0x75, 0x03, 0x0000}, flag_num, should_set ? 0 : 1}, l->difficulty, 0x0000}; - send_command_t(l, 0x60, 0x00, cmd); - } -} - -static void proxy_command_qset_qclear(const ProxyArgs& a, bool should_set) { - if (!a.ses->is_in_game) { - throw precondition_failed("$C6This command cannot\nbe used in the lobby"); - } - - uint16_t flag_num = stoul(a.text, nullptr, 0); - if (is_v1_or_v2(a.ses->version())) { - G_UpdateQuestFlag_DC_PC_6x75 cmd = {{0x75, 0x02, 0x0000}, flag_num, should_set ? 0 : 1}; - a.ses->client_channel.send(0x60, 0x00, &cmd, sizeof(cmd)); - a.ses->server_channel.send(0x60, 0x00, &cmd, sizeof(cmd)); - } else { - G_UpdateQuestFlag_V3_BB_6x75 cmd = {{{0x75, 0x03, 0x0000}, flag_num, should_set ? 0 : 1}, a.ses->lobby_difficulty, 0x0000}; - a.ses->client_channel.send(0x60, 0x00, &cmd, sizeof(cmd)); - a.ses->server_channel.send(0x60, 0x00, &cmd, sizeof(cmd)); + uint8_t difficulty = a.c->proxy_session ? a.c->proxy_session->lobby_difficulty : a.c->require_lobby()->difficulty; + G_UpdateQuestFlag_V3_BB_6x75 cmd = {{{0x75, 0x03, 0x0000}, flag_num, should_set ? 0 : 1}, difficulty, 0x0000}; + a.c->channel->send(0x60, 0x00, &cmd, sizeof(cmd)); + if (a.c->proxy_session) { + a.c->proxy_session->server_channel->send(0x60, 0x00, &cmd, sizeof(cmd)); + } } + return; } ChatCommandDefinition cc_qclear( {"$qclear"}, - +[](const ServerArgs& a) -> void { - server_command_qset_qclear(a, false); - }, - +[](const ProxyArgs& a) -> void { - proxy_command_qset_qclear(a, false); + +[](const Args& a) -> asio::awaitable { + command_qset_qclear(a, false); + co_return; }); ChatCommandDefinition cc_qfread( {"$qfread"}, - +[](const ServerArgs& a) -> void { + +[](const Args& a) -> asio::awaitable { + a.check_is_proxy(false); auto s = a.c->require_server_state(); uint8_t counter_index; @@ -1910,29 +1936,31 @@ ChatCommandDefinition cc_qfread( } if (mask == 1) { - send_text_message_printf(a.c, "$C7Field %s\nhas value %s", a.text.c_str(), counter_value ? "TRUE" : "FALSE"); + send_text_message_fmt(a.c, "$C7Field {}\nhas value {}", a.text, counter_value ? "TRUE" : "FALSE"); } else { - send_text_message_printf(a.c, "$C7Field %s\nhas value %" PRIu32, a.text.c_str(), counter_value); + send_text_message_fmt(a.c, "$C7Field {}\nhas value {}", a.text, counter_value); } - }, - unavailable_on_proxy_server); + co_return; + }); ChatCommandDefinition cc_qgread( {"$qgread"}, - +[](const ServerArgs& a) -> void { + +[](const Args& a) -> asio::awaitable { + a.check_is_proxy(false); uint8_t counter_num = stoul(a.text, nullptr, 0); const auto& counters = a.c->character()->quest_counters; if (counter_num >= counters.size()) { - throw precondition_failed_printf("$C7Counter ID must be\nless than %zu", counters.size()); + throw precondition_failed("$C7Counter ID must be\nless than {}", counters.size()); } else { - send_text_message_printf(a.c, "$C7Quest counter %hhu\nhas value %" PRIu32, counter_num, counters[counter_num].load()); + send_text_message_fmt(a.c, "$C7Quest counter {}\nhas value {}", counter_num, counters[counter_num]); } - }, - unavailable_on_proxy_server); + co_return; + }); ChatCommandDefinition cc_qgwrite( {"$qgwrite"}, - +[](const ServerArgs& a) -> void { + +[](const Args& a) -> asio::awaitable { + a.check_is_proxy(false); if (a.c->version() != Version::BB_V4) { throw precondition_failed("$C6This command can\nonly be used on BB"); } @@ -1951,33 +1979,27 @@ ChatCommandDefinition cc_qgwrite( uint32_t value = stoul(tokens[1], nullptr, 0); auto& counters = a.c->character()->quest_counters; if (counter_num >= counters.size()) { - throw precondition_failed_printf("$C7Counter ID must be\nless than %zu", counters.size()); + throw precondition_failed("$C7Counter ID must be\nless than {}", counters.size()); } else { a.c->character()->quest_counters[counter_num] = value; G_SetQuestCounter_BB_6xD2 cmd = {{0xD2, sizeof(G_SetQuestCounter_BB_6xD2) / 4, a.c->lobby_client_id}, counter_num, value}; send_command_t(a.c, 0x60, 0x00, cmd); - send_text_message_printf(a.c, "$C7Quest counter %hhu\nset to %" PRIu32, counter_num, value); + send_text_message_fmt(a.c, "$C7Quest counter {}\nset to {}", counter_num, value); } - }, - unavailable_on_proxy_server); + co_return; + }); ChatCommandDefinition cc_qset( {"$qset"}, - +[](const ServerArgs& a) -> void { - return server_command_qset_qclear(a, true); - }, - +[](const ProxyArgs& a) { - return proxy_command_qset_qclear(a, true); + +[](const Args& a) -> asio::awaitable { + command_qset_qclear(a, true); + co_return; }); -static void server_command_qsync_qsyncall(const ServerArgs& a, bool send_to_lobby) { +static void command_qsync_qsyncall(const Args& a, bool send_to_lobby) { + a.check_is_game(true); a.check_debug_enabled(); - auto l = a.c->require_lobby(); - if (!l->is_game()) { - throw precondition_failed("$C6This command cannot\nbe used in the lobby"); - } - auto tokens = phosg::split(a.text, ' '); if (tokens.size() != 2) { throw precondition_failed("$C6Incorrect number of\narguments"); @@ -1994,72 +2016,45 @@ static void server_command_qsync_qsyncall(const ServerArgs& a, bool send_to_lobb } else { throw precondition_failed("$C6First argument must\nbe a register"); } + send_command_t(a.c, 0x60, 0x00, cmd); if (send_to_lobby) { - send_command_t(l, 0x60, 0x00, cmd); - } else { - send_command_t(a.c, 0x60, 0x00, cmd); - } -} - -static void proxy_command_qsync_qsyncall(const ProxyArgs& a, bool send_to_lobby) { - if (!a.ses->is_in_game) { - throw precondition_failed("$C6This command cannot\nbe used in the lobby"); - } - - auto tokens = phosg::split(a.text, ' '); - if (tokens.size() != 2) { - throw precondition_failed("$C6Incorrect number of\narguments"); - } - - G_SyncQuestRegister_6x77 cmd; - cmd.header = {0x77, 0x03, 0x0000}; - cmd.register_number = stoul(tokens[0].substr(1), nullptr, 0); - cmd.unused = 0; - if (tokens[0][0] == 'r') { - cmd.value.as_int = stoul(tokens[1], nullptr, 0); - } else if (tokens[0][0] == 'f') { - cmd.value.as_float = stof(tokens[1]); - } else { - throw precondition_failed("$C6First argument must\nbe a register"); - } - a.ses->client_channel.send(0x60, 0x00, cmd); - if (send_to_lobby) { - a.ses->server_channel.send(0x60, 0x00, cmd); + if (a.c->proxy_session) { + send_command_t(a.c->proxy_session->server_channel, 0x60, 0x00, cmd); + } else { + send_command_t(a.c->require_lobby(), 0x60, 0x00, cmd); + } } } ChatCommandDefinition cc_qsync( {"$qsync"}, - +[](const ServerArgs& a) -> void { - server_command_qsync_qsyncall(a, false); - }, - +[](const ProxyArgs& a) { - proxy_command_qsync_qsyncall(a, false); + +[](const Args& a) -> asio::awaitable { + command_qsync_qsyncall(a, false); + co_return; }); ChatCommandDefinition cc_qsyncall( {"$qsyncall"}, - +[](const ServerArgs& a) -> void { - server_command_qsync_qsyncall(a, true); - }, - +[](const ProxyArgs& a) { - proxy_command_qsync_qsyncall(a, true); + +[](const Args& a) -> asio::awaitable { + command_qsync_qsyncall(a, true); + co_return; }); ChatCommandDefinition cc_quest( {"$quest"}, - +[](const ServerArgs& a) -> void { - auto s = a.c->require_server_state(); - auto l = a.c->require_lobby(); + +[](const Args& a) -> asio::awaitable { + a.check_is_proxy(false); a.check_is_game(true); + auto s = a.c->require_server_state(); Version effective_version = is_ep3(a.c->version()) ? Version::GC_V3 : a.c->version(); auto q = s->quest_index(effective_version)->get(stoul(a.text)); if (!q) { throw precondition_failed("$C6Quest not found"); } - if (a.check_permissions && !a.c->config.check_flag(Client::Flag::DEBUG_ENABLED)) { + auto l = a.c->require_lobby(); + if (a.check_permissions && !a.c->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"); } @@ -2069,39 +2064,25 @@ ChatCommandDefinition cc_quest( } set_lobby_quest(a.c->require_lobby(), q, true); - }, - unavailable_on_proxy_server); + co_return; + }); ChatCommandDefinition cc_rand( {"$rand"}, - +[](const ServerArgs& a) -> void { + +[](const Args& a) -> asio::awaitable { auto s = a.c->require_server_state(); auto l = a.c->require_lobby(); a.check_is_game(false); - a.check_cheats_allowed(s->cheat_flags.override_random_seed); + a.check_cheats_enabled_or_allowed(s->cheat_flags.override_random_seed); if (a.text.empty()) { - a.c->config.clear_flag(Client::Flag::USE_OVERRIDE_RANDOM_SEED); - a.c->config.override_random_seed = 0; + a.c->override_random_seed = -1; send_text_message(a.c, "$C6Override seed\nremoved"); } else { - a.c->config.set_flag(Client::Flag::USE_OVERRIDE_RANDOM_SEED); - a.c->config.override_random_seed = stoul(a.text, 0, 16); + a.c->override_random_seed = stoll(a.text, 0, 16); send_text_message(a.c, "$C6Override seed\nset"); } - }, - +[](const ProxyArgs& a) { - auto s = a.ses->require_server_state(); - a.check_cheats_allowed(s->cheat_flags.override_random_seed); - if (a.text.empty()) { - a.ses->config.clear_flag(Client::Flag::USE_OVERRIDE_RANDOM_SEED); - a.ses->config.override_random_seed = 0; - send_text_message(a.ses->client_channel, "$C6Override seed\nremoved"); - } else { - a.ses->config.set_flag(Client::Flag::USE_OVERRIDE_RANDOM_SEED); - a.ses->config.override_random_seed = stoul(a.text, 0, 16); - send_text_message(a.ses->client_channel, "$C6Override seed\nset"); - } + co_return; }); static bool console_address_in_range(Version version, uint32_t addr) { @@ -2116,7 +2097,7 @@ static bool console_address_in_range(Version version, uint32_t addr) { ChatCommandDefinition cc_readmem( {"$readmem"}, - +[](const ServerArgs& a) -> void { + +[](const Args& a) -> asio::awaitable { a.check_debug_enabled(); uint32_t addr = stoul(a.text, nullptr, 16); @@ -2124,64 +2105,60 @@ ChatCommandDefinition cc_readmem( throw precondition_failed("$C4Address out of\nrange"); } - prepare_client_for_patches(a.c, [wc = weak_ptr(a.c), addr]() { - auto c = wc.lock(); - if (!c) { - return; - } - try { - auto s = c->require_server_state(); - const char* function_name = is_dc(c->version()) - ? "ReadMemoryWordDC" - : is_gc(c->version()) - ? "ReadMemoryWordGC" - : "ReadMemoryWordX86"; - auto fn = s->function_code_index->name_to_function.at(function_name); - send_function_call(c, fn, {{"address", addr}}); - c->function_call_response_queue.emplace_back([wc = weak_ptr(c), addr](uint32_t ret, uint32_t) { - auto c = wc.lock(); - if (c) { - string data_str; - if (is_big_endian(c->version())) { - be_uint32_t v = ret; - data_str = phosg::format_data_string(&v, sizeof(v)); - } else { - le_uint32_t v = ret; - data_str = phosg::format_data_string(&v, sizeof(v)); - } - send_text_message_printf(c, "Bytes at %08" PRIX32 ":\n$C6%s", addr, data_str.c_str()); - } - }); - } catch (const out_of_range&) { - throw precondition_failed("Invalid patch name"); - } - }); - }, - unavailable_on_proxy_server); + co_await prepare_client_for_patches(a.c); + + shared_ptr fn; + try { + auto s = a.c->require_server_state(); + const char* function_name = is_dc(a.c->version()) + ? "ReadMemoryWordDC" + : is_gc(a.c->version()) + ? "ReadMemoryWordGC" + : "ReadMemoryWordX86"; + fn = s->function_code_index->name_to_function.at(function_name); + } catch (const out_of_range&) { + throw precondition_failed("Invalid patch name"); + } + + unordered_map label_writes{{"address", addr}}; + auto res = co_await send_function_call(a.c, fn, label_writes); + string data_str; + if (is_big_endian(a.c->version())) { + be_uint32_t v = res.return_value.load(); + data_str = phosg::format_data_string(&v, sizeof(v)); + } else { + data_str = phosg::format_data_string(&res.return_value, sizeof(res.return_value)); + } + send_text_message_fmt(a.c, "Bytes at {:08X}:\n$C6{}", addr, data_str); + }); ChatCommandDefinition cc_save( {"$save"}, - +[](const ServerArgs& a) -> void { + +[](const Args& a) -> asio::awaitable { a.check_version(Version::BB_V4); + a.check_is_proxy(false); a.c->save_all(); send_text_message(a.c, "All data saved"); - a.c->reschedule_save_game_data_event(); - }, - unavailable_on_proxy_server); + a.c->reschedule_save_game_data_timer(); + co_return; + }); ChatCommandDefinition cc_savechar( {"$savechar"}, - +[](const ServerArgs& a) -> void { + +[](const Args& a) -> asio::awaitable { if (a.check_permissions && a.c->login->account->check_flag(Account::Flag::IS_SHARED_ACCOUNT)) { throw precondition_failed("$C7This command cannot\nbe used on a shared\naccount"); } - server_command_bbchar_savechar(a, false); - }, - unavailable_on_proxy_server); + co_await server_command_bbchar_savechar(a, false); + co_return; + }); ChatCommandDefinition cc_saverec( {"$saverec"}, - +[](const ServerArgs& a) -> void { + +[](const Args& a) -> asio::awaitable { + // TODO: We can probably support this on the proxy server, but it would + // only include CA commands from the local player + a.check_is_proxy(false); if (!a.c->ep3_prev_battle_record) { throw precondition_failed("$C4No finished\nrecording is\npresent"); } @@ -2190,59 +2167,47 @@ ChatCommandDefinition cc_saverec( phosg::save_file(file_path, data); send_text_message(a.c, "$C7Recording saved"); a.c->ep3_prev_battle_record.reset(); - }, - unavailable_on_proxy_server); + co_return; + }); -static void server_command_send_command(const ServerArgs& a, bool to_client, bool to_server) { - a.check_debug_enabled(); +static asio::awaitable command_send_command(const Args& a, bool to_client, bool to_server) { + if (!a.c->proxy_session) { + a.check_debug_enabled(); + } string data = phosg::parse_data_string(a.text); data.resize((data.size() + 3) & (~3)); if (to_client) { - a.c->channel.send(data); + a.c->channel->send(data); } if (to_server) { - on_command_with_header(a.c, data); - } -} - -static void proxy_command_send_command(const ProxyArgs& a, bool to_client, bool to_server) { - string data = phosg::parse_data_string(a.text); - data.resize((data.size() + 3) & (~3)); - if (to_client) { - a.ses->client_channel.send(data); - } - if (to_server) { - a.ses->server_channel.send(data); + if (a.c->proxy_session) { + a.c->proxy_session->server_channel->send(data); + } else { + co_await on_command_with_header(a.c, data); + } } + co_return; } ChatCommandDefinition cc_sb( {"$sb"}, - +[](const ServerArgs& a) -> void { - server_command_send_command(a, true, true); - }, - +[](const ProxyArgs& a) -> void { - proxy_command_send_command(a, true, true); + +[](const Args& a) -> asio::awaitable { + return command_send_command(a, true, true); }); ChatCommandDefinition cc_sc( {"$sc"}, - +[](const ServerArgs& a) -> void { - server_command_send_command(a, true, false); - }, - +[](const ProxyArgs& a) -> void { - proxy_command_send_command(a, true, false); + +[](const Args& a) -> asio::awaitable { + return command_send_command(a, true, false); }); ChatCommandDefinition cc_secid( {"$secid"}, - +[](const ServerArgs& a) -> void { - auto l = a.c->require_lobby(); + +[](const Args& a) -> asio::awaitable { auto s = a.c->require_server_state(); - a.check_cheats_allowed(s->cheat_flags.override_section_id); + a.check_cheats_enabled_or_allowed(s->cheat_flags.override_section_id); uint8_t new_override_section_id; - if (a.text.empty()) { new_override_section_id = 0xFF; send_text_message(a.c, "$C6Override section ID\nremoved"); @@ -2251,43 +2216,31 @@ ChatCommandDefinition cc_secid( if (new_override_section_id == 0xFF) { throw precondition_failed("$C6Invalid section ID"); } else { - send_text_message_printf(a.c, "$C6Override section ID\nset to %s", name_for_section_id(new_override_section_id)); + send_text_message_fmt(a.c, "$C6Override section ID\nset to {}", name_for_section_id(new_override_section_id)); } } - a.c->config.override_section_id = new_override_section_id; - if (l->is_game() && (l->leader_id == a.c->lobby_client_id)) { - l->override_section_id = new_override_section_id; - l->create_item_creator(); - } - }, - +[](const ProxyArgs& a) { - auto s = a.ses->require_server_state(); - a.check_cheats_allowed(s->cheat_flags.override_section_id); - - if (a.text.empty()) { - a.ses->config.override_section_id = 0xFF; - send_text_message(a.ses->client_channel, "$C6Override section ID\nremoved"); - } else { - uint8_t new_secid = section_id_for_name(a.text); - if (new_secid == 0xFF) { - throw precondition_failed("$C6Invalid section ID"); - } else { - a.ses->config.override_section_id = new_secid; - send_text_message_printf(a.ses->client_channel, "$C6Override section ID\nset to %s", name_for_section_id(new_secid)); + a.c->override_section_id = new_override_section_id; + if (!a.c->proxy_session) { + auto l = a.c->require_lobby(); + if (l->is_game() && (l->leader_id == a.c->lobby_client_id)) { + l->override_section_id = new_override_section_id; + l->create_item_creator(); } } + co_return; }); ChatCommandDefinition cc_setassist( {"$setassist"}, - +[](const ServerArgs& a) -> void { - auto s = a.c->require_server_state(); - auto l = a.c->require_lobby(); + +[](const Args& a) -> asio::awaitable { + a.check_is_proxy(false); a.check_is_game(true); a.check_is_ep3(true); - a.check_cheats_enabled(s->cheat_flags.ep3_replace_assist); + auto s = a.c->require_server_state(); + a.check_cheats_enabled_in_game(s->cheat_flags.ep3_replace_assist); + auto l = a.c->require_lobby(); if (l->episode != Episode::EP3) { throw logic_error("non-Ep3 client in Ep3 game"); } @@ -2325,38 +2278,30 @@ ChatCommandDefinition cc_setassist( throw precondition_failed("$C6Card is not an\nAssist card"); } l->ep3_server->force_replace_assist_card(client_id, ce->def.card_id); - }, - unavailable_on_proxy_server); + co_return; + }); ChatCommandDefinition cc_server_info( {"$si"}, - +[](const ServerArgs& a) -> void { + +[](const Args& a) -> asio::awaitable { auto s = a.c->require_server_state(); string uptime_str = phosg::format_duration(phosg::now() - s->creation_time); - if (s->proxy_server) { - send_text_message_printf(a.c, - "Uptime: $C6%s$C7\nLobbies: $C6%zu$C7\nClients: $C6%zu$C7(g) $C6%zu$C7(p)", - uptime_str.c_str(), - s->id_to_lobby.size(), - s->channel_to_client.size(), - s->proxy_server->num_sessions()); - } else { - send_text_message_printf(a.c, - "Uptime: $C6%s$C7\nLobbies: $C6%zu$C7\nClients: $C6%zu", - uptime_str.c_str(), - s->id_to_lobby.size(), - s->channel_to_client.size()); - } - }, - unavailable_on_proxy_server); + send_text_message_fmt(a.c, + "Uptime: $C6{}$C7\nLobbies: $C6{}$C7\nClients: $C6{}$C7(g) $C6{}$C7(p)", + uptime_str, + s->id_to_lobby.size(), + s->game_server->all_clients().size() - ProxySession::num_proxy_sessions, + ProxySession::num_proxy_sessions); + co_return; + }); ChatCommandDefinition cc_silence( {"$silence"}, - +[](const ServerArgs& a) -> void { - auto s = a.c->require_server_state(); - auto l = a.c->require_lobby(); + +[](const Args& a) -> asio::awaitable { + a.check_is_proxy(false); a.check_account_flag(Account::Flag::SILENCE_USER); + auto s = a.c->require_server_state(); auto target = s->find_client(&a.text); if (!target->login) { // this should be impossible, but I'll bet it's not actually @@ -2369,32 +2314,31 @@ ChatCommandDefinition cc_silence( target->can_chat = !target->can_chat; string target_name = name_for_client(target); - send_text_message_printf(a.c, "$C6%s %ssilenced", target_name.c_str(), - target->can_chat ? "un" : ""); - }, - unavailable_on_proxy_server); + send_text_message_fmt(a.c, "$C6{} {}silenced", target_name, target->can_chat ? "un" : ""); + co_return; + }); ChatCommandDefinition cc_song( {"$song"}, - +[](const ServerArgs& a) -> void { + +[](const Args& a) -> asio::awaitable { a.check_is_ep3(true); - send_ep3_change_music(a.c->channel, stoul(a.text, nullptr, 0)); - }, - +[](const ProxyArgs& a) { + int32_t song = stol(a.text, nullptr, 0); - if (song < 0) { + if (song < 0 && a.c->proxy_session) { song = -song; - send_ep3_change_music(a.ses->server_channel, song); + send_ep3_change_music(a.c->proxy_session->server_channel, song); } - send_ep3_change_music(a.ses->client_channel, song); + send_ep3_change_music(a.c->channel, stoul(a.text, nullptr, 0)); + co_return; }); ChatCommandDefinition cc_spec( {"$spec"}, - +[](const ServerArgs& a) -> void { - auto l = a.c->require_lobby(); + +[](const Args& a) -> asio::awaitable { + a.check_is_proxy(false); a.check_is_game(true); a.check_is_ep3(true); + auto l = a.c->require_lobby(); if (!l->is_ep3()) { throw logic_error("Episode 3 client in non-Episode 3 game"); } @@ -2422,24 +2366,22 @@ ChatCommandDefinition cc_spec( l->watcher_lobbies.clear(); send_text_message(l, "$C6Spectators forbidden"); } - }, - unavailable_on_proxy_server); + co_return; + }); ChatCommandDefinition cc_ss( {"$ss"}, - +[](const ServerArgs& a) -> void { - server_command_send_command(a, false, true); - }, - +[](const ProxyArgs& a) -> void { - proxy_command_send_command(a, false, true); + +[](const Args& a) -> asio::awaitable { + return command_send_command(a, false, true); }); ChatCommandDefinition cc_stat( {"$stat"}, - +[](const ServerArgs& a) -> void { - auto l = a.c->require_lobby(); + +[](const Args& a) -> asio::awaitable { + a.check_is_proxy(false); a.check_is_game(true); a.check_is_ep3(true); + auto l = a.c->require_lobby(); if (l->episode != Episode::EP3) { throw logic_error("non-Ep3 client in Ep3 game"); } @@ -2462,64 +2404,65 @@ ChatCommandDefinition cc_stat( float score = ps->stats.score(l->ep3_server->get_round_num()); uint8_t rank = ps->stats.rank_for_score(score); const char* rank_name = ps->stats.name_for_rank(rank); - send_text_message_printf(a.c, "$C7Score: %g\nRank: %hhu (%s)", score, rank, rank_name); + send_text_message_fmt(a.c, "$C7Score: {:g}\nRank: {} ({})", score, rank, rank_name); } else if (a.text == "duration") { string s = phosg::format_duration(phosg::now() - l->ep3_server->battle_start_usecs); - send_text_message_printf(a.c, "$C7Duration: %s", s.c_str()); + send_text_message_fmt(a.c, "$C7Duration: {}", s); } else if (a.text == "fcs-destroyed") { - send_text_message_printf(a.c, "$C7Team FCs destroyed:\n%" PRIu32, l->ep3_server->team_num_ally_fcs_destroyed[team_id]); + send_text_message_fmt(a.c, "$C7Team FCs destroyed:\n{}", l->ep3_server->team_num_ally_fcs_destroyed[team_id]); } else if (a.text == "cards-destroyed") { - send_text_message_printf(a.c, "$C7Team cards destroyed:\n%" PRIu32, l->ep3_server->team_num_cards_destroyed[team_id]); + send_text_message_fmt(a.c, "$C7Team cards destroyed:\n{}", l->ep3_server->team_num_cards_destroyed[team_id]); } else if (a.text == "damage-given") { - send_text_message_printf(a.c, "$C7Damage given: %hu", ps->stats.damage_given.load()); + send_text_message_fmt(a.c, "$C7Damage given: {}", ps->stats.damage_given); } else if (a.text == "damage-taken") { - send_text_message_printf(a.c, "$C7Damage taken: %hu", ps->stats.damage_taken.load()); + send_text_message_fmt(a.c, "$C7Damage taken: {}", ps->stats.damage_taken); } else if (a.text == "opp-cards-destroyed") { - send_text_message_printf(a.c, "$C7Opp. cards destroyed:\n%hu", ps->stats.num_opponent_cards_destroyed.load()); + send_text_message_fmt(a.c, "$C7Opp. cards destroyed:\n{}", ps->stats.num_opponent_cards_destroyed); } else if (a.text == "own-cards-destroyed") { - send_text_message_printf(a.c, "$C7Own cards destroyed:\n%hu", ps->stats.num_owned_cards_destroyed.load()); + send_text_message_fmt(a.c, "$C7Own cards destroyed:\n{}", ps->stats.num_owned_cards_destroyed); } else if (a.text == "move-distance") { - send_text_message_printf(a.c, "$C7Move distance: %hu", ps->stats.total_move_distance.load()); + send_text_message_fmt(a.c, "$C7Move distance: {}", ps->stats.total_move_distance); } else if (a.text == "cards-set") { - send_text_message_printf(a.c, "$C7Cards set: %hu", ps->stats.num_cards_set.load()); + send_text_message_fmt(a.c, "$C7Cards set: {}", ps->stats.num_cards_set); } else if (a.text == "fcs-set") { - send_text_message_printf(a.c, "$C7FC cards set: %hu", ps->stats.num_item_or_creature_cards_set.load()); + send_text_message_fmt(a.c, "$C7FC cards set: {}", ps->stats.num_item_or_creature_cards_set); } else if (a.text == "attack-actions-set") { - send_text_message_printf(a.c, "$C7Attack actions set:\n%hu", ps->stats.num_attack_actions_set.load()); + send_text_message_fmt(a.c, "$C7Attack actions set:\n{}", ps->stats.num_attack_actions_set); } else if (a.text == "techs-set") { - send_text_message_printf(a.c, "$C7Techs set: %hu", ps->stats.num_tech_cards_set.load()); + send_text_message_fmt(a.c, "$C7Techs set: {}", ps->stats.num_tech_cards_set); } else if (a.text == "assists-set") { - send_text_message_printf(a.c, "$C7Assists set: %hu", ps->stats.num_assist_cards_set.load()); + send_text_message_fmt(a.c, "$C7Assists set: {}", ps->stats.num_assist_cards_set); } else if (a.text == "defenses-self") { - send_text_message_printf(a.c, "$C7Defenses on self:\n%hu", ps->stats.defense_actions_set_on_self.load()); + send_text_message_fmt(a.c, "$C7Defenses on self:\n{}", ps->stats.defense_actions_set_on_self); } else if (a.text == "defenses-ally") { - send_text_message_printf(a.c, "$C7Defenses on ally:\n%hu", ps->stats.defense_actions_set_on_ally.load()); + send_text_message_fmt(a.c, "$C7Defenses on ally:\n{}", ps->stats.defense_actions_set_on_ally); } else if (a.text == "cards-drawn") { - send_text_message_printf(a.c, "$C7Cards drawn: %hu", ps->stats.num_cards_drawn.load()); + send_text_message_fmt(a.c, "$C7Cards drawn: {}", ps->stats.num_cards_drawn); } else if (a.text == "max-attack-damage") { - send_text_message_printf(a.c, "$C7Maximum attack damage:\n%hu", ps->stats.max_attack_damage.load()); + send_text_message_fmt(a.c, "$C7Maximum attack damage:\n{}", ps->stats.max_attack_damage); } else if (a.text == "max-combo") { - send_text_message_printf(a.c, "$C7Longest combo: %hu", ps->stats.max_attack_combo_size.load()); + send_text_message_fmt(a.c, "$C7Longest combo: {}", ps->stats.max_attack_combo_size); } else if (a.text == "attacks-given") { - send_text_message_printf(a.c, "$C7Attacks given: %hu", ps->stats.num_attacks_given.load()); + send_text_message_fmt(a.c, "$C7Attacks given: {}", ps->stats.num_attacks_given); } else if (a.text == "attacks-taken") { - send_text_message_printf(a.c, "$C7Attacks taken: %hu", ps->stats.num_attacks_taken.load()); + send_text_message_fmt(a.c, "$C7Attacks taken: {}", ps->stats.num_attacks_taken); } else if (a.text == "sc-damage") { - send_text_message_printf(a.c, "$C7SC damage taken: %hu", ps->stats.sc_damage_taken.load()); + send_text_message_fmt(a.c, "$C7SC damage taken: {}", ps->stats.sc_damage_taken); } else if (a.text == "damage-defended") { - send_text_message_printf(a.c, "$C7Damage defended: %hu", ps->stats.action_card_negated_damage.load()); + send_text_message_fmt(a.c, "$C7Damage defended: {}", ps->stats.action_card_negated_damage); } else { throw precondition_failed("$C6Unknown statistic"); } - }, - unavailable_on_proxy_server); + co_return; + }); ChatCommandDefinition cc_surrender( {"$surrender"}, - +[](const ServerArgs& a) -> void { - auto l = a.c->require_lobby(); + +[](const Args& a) -> asio::awaitable { + a.check_is_proxy(false); a.check_is_game(true); a.check_is_ep3(true); + auto l = a.c->require_lobby(); if (l->episode != Episode::EP3) { throw logic_error("non-Ep3 client in Ep3 game"); } @@ -2534,38 +2477,30 @@ ChatCommandDefinition cc_surrender( throw precondition_failed("$C6Defeated players\ncannot surrender"); } string name = remove_color(a.c->character()->disp.name.decode(a.c->language())); - send_text_message_printf(l, "$C6%s has\nsurrendered", name.c_str()); + send_text_message_fmt(l, "$C6{} has\nsurrendered", name); for (const auto& watcher_l : l->watcher_lobbies) { - send_text_message_printf(watcher_l, "$C6%s has\nsurrendered", name.c_str()); + send_text_message_fmt(watcher_l, "$C6{} has\nsurrendered", name); } l->ep3_server->force_battle_result(a.c->lobby_client_id, false); - }, - unavailable_on_proxy_server); + co_return; + }); ChatCommandDefinition cc_swa( {"$swa"}, - +[](const ServerArgs& a) -> void { + +[](const Args& a) -> asio::awaitable { auto s = a.c->require_server_state(); auto l = a.c->require_lobby(); a.check_is_game(true); - a.c->config.toggle_flag(Client::Flag::SWITCH_ASSIST_ENABLED); - send_text_message_printf(a.c, "$C6Switch assist %s", - a.c->config.check_flag(Client::Flag::SWITCH_ASSIST_ENABLED) ? "enabled" : "disabled"); - }, - +[](const ProxyArgs& a) { - auto s = a.ses->require_server_state(); - a.ses->config.toggle_flag(Client::Flag::SWITCH_ASSIST_ENABLED); - send_text_message_printf(a.ses->client_channel, "$C6Switch assist %s", - a.ses->config.check_flag(Client::Flag::SWITCH_ASSIST_ENABLED) ? "enabled" : "disabled"); + a.c->toggle_flag(Client::Flag::SWITCH_ASSIST_ENABLED); + send_text_message_fmt(a.c, "$C6Switch assist {}", + a.c->check_flag(Client::Flag::SWITCH_ASSIST_ENABLED) ? "enabled" : "disabled"); + co_return; }); -static void server_command_swset_swclear(const ServerArgs& a, bool should_set) { +static void command_swset_swclear(const Args& a, bool should_set) { a.check_debug_enabled(); - auto l = a.c->require_lobby(); - if (!l->is_game()) { - throw precondition_failed("$C6This command cannot\nbe used in the lobby"); - } + a.check_is_game(true); auto tokens = phosg::split(a.text, ' '); uint8_t floor, flag_num; @@ -2579,69 +2514,44 @@ static void server_command_swset_swclear(const ServerArgs& a, bool should_set) { throw precondition_failed("$C4Incorrect parameters"); } - 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_WriteSwitchFlag_6x05 cmd = {{0x05, 0x03, 0xFFFF}, 0, 0, flag_num, floor, cmd_flags}; - send_command_t(l, 0x60, 0x00, cmd); -} -static void proxy_command_swset_swclear(const ProxyArgs& a, bool should_set) { - if (!a.ses->is_in_game) { - throw precondition_failed("$C6This command cannot\nbe used in the lobby"); - } - - auto tokens = phosg::split(a.text, ' '); - uint8_t floor, flag_num; - if (tokens.size() == 1) { - floor = a.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); + if (!a.c->proxy_session) { + auto l = a.c->require_lobby(); + if (l->switch_flags) { + if (should_set) { + l->switch_flags->set(floor, flag_num); + } else { + l->switch_flags->clear(floor, flag_num); + } + } + send_command_t(l, 0x60, 0x00, cmd); } else { - throw precondition_failed("$C4Incorrect parameters"); + send_command_t(a.c, 0x60, 0x00, cmd); + send_command_t(a.c->proxy_session->server_channel, 0x60, 0x00, cmd); } - - uint8_t cmd_flags = should_set ? 0x01 : 0x00; - G_WriteSwitchFlag_6x05 cmd = {{0x05, 0x03, 0xFFFF}, 0, 0, flag_num, floor, cmd_flags}; - a.ses->client_channel.send(0x60, 0x00, &cmd, sizeof(cmd)); - a.ses->server_channel.send(0x60, 0x00, &cmd, sizeof(cmd)); } ChatCommandDefinition cc_swclear( {"$swclear"}, - +[](const ServerArgs& a) -> void { - server_command_swset_swclear(a, false); - }, - +[](const ProxyArgs& a) { - proxy_command_swset_swclear(a, false); + +[](const Args& a) -> asio::awaitable { + command_swset_swclear(a, false); + co_return; }); ChatCommandDefinition cc_swset( {"$swset"}, - +[](const ServerArgs& a) -> void { - return server_command_swset_swclear(a, true); - }, - +[](const ProxyArgs& a) { - return proxy_command_swset_swclear(a, true); + +[](const Args& a) -> asio::awaitable { + command_swset_swclear(a, true); + co_return; }); ChatCommandDefinition cc_swsetall( {"$swsetall"}, - +[](const ServerArgs& a) -> void { + +[](const Args& a) -> asio::awaitable { a.check_debug_enabled(); - - auto l = a.c->require_lobby(); - if (!l->is_game()) { - throw precondition_failed("$C6This command cannot\nbe used in the lobby"); - } - - l->switch_flags->data[a.c->floor].clear(0xFF); + a.check_is_game(true); parray cmds; for (size_t z = 0; z < cmds.size(); z++) { @@ -2654,37 +2564,32 @@ ChatCommandDefinition cc_swsetall( cmd.flags = 0x01; } cmds[0].flags = 0x03; // Play room unlock sound - send_command_t(l, 0x6C, 0x00, cmds); - }, - +[](const ProxyArgs& a) { - if (!a.ses->is_in_game) { - throw precondition_failed("$C6This command cannot\nbe used in the lobby"); - } - parray cmds; - for (size_t z = 0; z < cmds.size(); z++) { - auto& cmd = cmds[z]; - cmd.header.subcommand = 0x05; - cmd.header.size = 0x03; - cmd.header.entity_id = 0xFFFF; - cmd.switch_flag_floor = a.ses->floor; - cmd.switch_flag_num = z; - cmd.flags = 0x01; + if (!a.c->proxy_session) { + auto l = a.c->require_lobby(); + if (!l->is_game()) { + throw precondition_failed("$C6This command cannot\nbe used in the lobby"); + } + if (l->switch_flags) { + l->switch_flags->data[a.c->floor].clear(0xFF); + } + send_command_t(l, 0x6C, 0x00, cmds); + } else { + send_command_t(a.c, 0x6C, 0x00, cmds); + send_command_t(a.c->proxy_session->server_channel, 0x6C, 0x00, cmds); } - cmds[0].flags = 0x03; // Play room unlock sound - a.ses->client_channel.send(0x6C, 0x00, &cmds, sizeof(cmds)); - a.ses->server_channel.send(0x6C, 0x00, &cmds, sizeof(cmds)); + co_return; }); ChatCommandDefinition cc_unset( {"$unset"}, - +[](const ServerArgs& a) -> void { - auto s = a.c->require_server_state(); - auto l = a.c->require_lobby(); + +[](const Args& a) -> asio::awaitable { + a.check_is_proxy(false); a.check_is_game(true); a.check_is_ep3(true); - a.check_cheats_enabled(s->cheat_flags.ep3_unset_field_character); - + auto s = a.c->require_server_state(); + a.check_cheats_enabled_in_game(s->cheat_flags.ep3_unset_field_character); + auto l = a.c->require_lobby(); if (l->episode != Episode::EP3) { throw logic_error("non-Ep3 client in Ep3 game"); } @@ -2697,19 +2602,19 @@ ChatCommandDefinition cc_unset( size_t index = stoull(a.text) - 1; l->ep3_server->force_destroy_field_character(a.c->lobby_client_id, index); - }, - unavailable_on_proxy_server); + co_return; + }); ChatCommandDefinition cc_variations( {"$variations"}, - +[](const ServerArgs& a) -> void { + +[](const Args& a) -> asio::awaitable { // Note: This command is intentionally undocumented, since it's primarily used // for testing. If we ever make it public, we should add some kind of user // feedback (currently it sends no message when it runs). - auto s = a.c->require_server_state(); - auto l = a.c->require_lobby(); + a.check_is_proxy(false); a.check_is_game(false); - a.check_cheats_allowed(s->cheat_flags.override_variations); + auto s = a.c->require_server_state(); + a.check_cheats_enabled_in_game(s->cheat_flags.override_variations); a.c->override_variations = make_unique(); for (size_t z = 0; z < min(a.c->override_variations->entries.size() * 2, a.text.size()); z++) { @@ -2721,78 +2626,73 @@ ChatCommandDefinition cc_variations( } } auto vars_str = a.c->override_variations->str(); - a.c->log.info("Override variations set to %s", vars_str.c_str()); - }, - unavailable_on_proxy_server); + a.c->log.info_f("Override variations set to {}", vars_str); + co_return; + }); -static void server_command_warp(const ServerArgs& a, bool is_warpall) { - auto s = a.c->require_server_state(); - auto l = a.c->require_lobby(); +static void command_warp(const Args& a, bool is_warpall) { a.check_is_game(true); - a.check_cheats_enabled(s->cheat_flags.warp); + auto s = a.c->require_server_state(); + a.check_cheats_enabled_or_allowed(s->cheat_flags.warp); uint32_t floor = stoul(a.text, nullptr, 0); if (!is_warpall && (a.c->floor == floor)) { return; } - size_t limit = FloorDefinition::limit_for_episode(l->episode); + Episode episode = a.c->proxy_session + ? a.c->proxy_session->lobby_episode + : a.c->require_lobby()->episode; + size_t limit = FloorDefinition::limit_for_episode(episode); if (limit == 0) { return; } else if (floor > limit) { - throw precondition_failed_printf("$C6Area numbers must\nbe %zu or less", limit); + throw precondition_failed("$C6Area numbers must\nbe {} or less", limit); } - if (is_warpall) { - send_warp(l, floor, false); + if (a.c->proxy_session) { + send_warp(a.c, floor, true); + if (is_warpall) { + for (size_t z = 0; z < a.c->proxy_session->lobby_players.size(); z++) { + const auto& lp = a.c->proxy_session->lobby_players[z]; + if (lp.guild_card_number) { + send_warp(a.c->proxy_session->server_channel, z, floor, true); + } + } + } + } else if (is_warpall) { + send_warp(a.c->require_lobby(), floor, false); } else { send_warp(a.c, floor, true); } } -static void proxy_command_warp(const ProxyArgs& a, bool is_warpall) { - auto s = a.ses->require_server_state(); - a.check_cheats_allowed(s->cheat_flags.warp); - if (!a.ses->is_in_game) { - throw precondition_failed("$C6You must be in a\ngame to use this\ncommand"); - } - uint32_t floor = stoul(a.text, nullptr, 0); - send_warp(a.ses->client_channel, a.ses->lobby_client_id, floor, !is_warpall); - if (is_warpall) { - send_warp(a.ses->server_channel, a.ses->lobby_client_id, floor, false); - } -} - ChatCommandDefinition cc_warp( {"$warp", "$warpme"}, - +[](const ServerArgs& a) -> void { - server_command_warp(a, false); - }, - +[](const ProxyArgs& a) { - proxy_command_warp(a, false); + +[](const Args& a) -> asio::awaitable { + command_warp(a, false); + co_return; }); ChatCommandDefinition cc_warpall( {"$warpall"}, - +[](const ServerArgs& a) -> void { - server_command_warp(a, true); - }, - +[](const ProxyArgs& a) { - proxy_command_warp(a, true); + +[](const Args& a) -> asio::awaitable { + command_warp(a, true); + co_return; }); ChatCommandDefinition cc_what( {"$what"}, - +[](const ServerArgs& a) -> void { - auto l = a.c->require_lobby(); + +[](const Args& a) -> asio::awaitable { + a.check_is_proxy(false); a.check_is_game(true); - + auto l = a.c->require_lobby(); if (!episode_has_arpg_semantics(l->episode)) { - return; + co_return; } - float min_dist2 = 0.0f; shared_ptr nearest_fi; + float min_dist2 = 0.0f; for (const auto& it : l->floor_item_managers.at(a.c->floor).items) { if (!it.second->visible_to_client(a.c->lobby_client_id)) { continue; @@ -2811,14 +2711,15 @@ ChatCommandDefinition cc_what( string name = s->describe_item(a.c->version(), nearest_fi->data, true); send_text_message(a.c, name); } - }, - unavailable_on_proxy_server); + co_return; + }); -static void whatobj_whatene_fn(const ServerArgs& a, bool include_objs, bool include_enes) { - auto s = a.c->require_server_state(); - auto l = a.c->require_lobby(); +static void whatobj_whatene_fn(const Args& a, bool include_objs, bool include_enes) { + // TODO: This probably wouldn't be too hard to implement for proxy sessions. + // We already have the map and most of the lobby metadata (episode, etc.) + a.check_is_proxy(false); a.check_is_game(true); - + auto l = a.c->require_lobby(); if (!l->map_state) { throw precondition_failed("$C4No map loaded"); } @@ -2826,6 +2727,7 @@ static void whatobj_whatene_fn(const ServerArgs& a, bool include_objs, bool incl // TODO: We should use the actual area if a loaded quest has reassigned // them; it's likely that the variations will be wrong if we don't uint8_t area, layout_var; + auto s = a.c->require_server_state(); if (l->episode != Episode::EP3) { auto sdt = s->set_data_table(a.c->version(), l->episode, l->mode, l->difficulty); area = sdt->default_area_for_floor(l->episode, a.c->floor); @@ -2848,7 +2750,7 @@ static void whatobj_whatene_fn(const ServerArgs& a, bool include_objs, bool incl // This is the order in which the game does the rotations; not sure why worldspace_pos = def.set_entry->pos.rotate_x(room.angle.x).rotate_z(room.angle.z).rotate_y(room.angle.y) + room.position; } catch (const out_of_range&) { - a.c->log.warning("Can't find definition for room %02hhX:%02hhX:%08hX", area, layout_var, def.set_entry->room.load()); + a.c->log.warning_f("Can't find definition for room {:02X}:{:02X}:{:08X}", area, layout_var, def.set_entry->room); worldspace_pos = def.set_entry->pos; } } else { @@ -2890,23 +2792,23 @@ static void whatobj_whatene_fn(const ServerArgs& a, bool include_objs, bool incl if (nearest_ene) { const auto* set_entry = nearest_ene->super_ene->version(a.c->version()).set_entry; string type_name = MapFile::name_for_enemy_type(set_entry->base_type, a.c->version(), area); - send_text_message_printf(a.c, "$C5E-%03zX\n$C6%s\n$C2%s\n$C7X:%.2f Z:%.2f", + send_text_message_fmt(a.c, "$C5E-{:03X}\n$C6{}\n$C2{}\n$C7X:{:.2f} Z:{:.2f}", nearest_ene->e_id, phosg::name_for_enum(nearest_ene->type(a.c->version(), l->episode, l->event)), - type_name.c_str(), nearest_worldspace_pos.x.load(), nearest_worldspace_pos.z.load()); + type_name, nearest_worldspace_pos.x, nearest_worldspace_pos.z); auto set_str = set_entry->str(a.c->version(), area); - a.c->log.info("Enemy found via $whatobj: E-%03zX %s at x=%g y=%g z=%g", - nearest_ene->e_id, set_str.c_str(), - nearest_worldspace_pos.x.load(), nearest_worldspace_pos.y.load(), nearest_worldspace_pos.z.load()); + a.c->log.info_f("Enemy found via $whatobj: E-{:03X} {} at x={:g} y={:g} z={:g}", + nearest_ene->e_id, set_str, + nearest_worldspace_pos.x, nearest_worldspace_pos.y, nearest_worldspace_pos.z); } else if (nearest_obj) { const auto* set_entry = nearest_obj->super_obj->version(a.c->version()).set_entry; auto type_name = nearest_obj->type_name(a.c->version()); - send_text_message_printf(a.c, "$C5K-%03zX\n$C6%s\n$C7X:%.2f Z:%.2f", - nearest_obj->k_id, type_name.c_str(), nearest_worldspace_pos.x.load(), nearest_worldspace_pos.z.load()); + send_text_message_fmt(a.c, "$C5K-{:03X}\n$C6{}\n$C7X:{:.2f} Z:{:.2f}", + nearest_obj->k_id, type_name, nearest_worldspace_pos.x, nearest_worldspace_pos.z); auto set_str = set_entry->str(a.c->version(), area); - a.c->log.info("Object found via $whatobj: K-%03zX %s at x=%g y=%g z=%g", - nearest_obj->k_id, set_str.c_str(), - nearest_worldspace_pos.x.load(), nearest_worldspace_pos.y.load(), nearest_worldspace_pos.z.load()); + a.c->log.info_f("Object found via $whatobj: K-{:03X} {} at x={:g} y={:g} z={:g}", + nearest_obj->k_id, set_str, + nearest_worldspace_pos.x, nearest_worldspace_pos.y, nearest_worldspace_pos.z); } else { throw precondition_failed("$C4No objects or\nenemies are nearby"); @@ -2915,43 +2817,55 @@ static void whatobj_whatene_fn(const ServerArgs& a, bool include_objs, bool incl ChatCommandDefinition cc_whatobj( {"$whatobj"}, - +[](const ServerArgs& a) -> void { + +[](const Args& a) -> asio::awaitable { whatobj_whatene_fn(a, true, false); - }, - unavailable_on_proxy_server); + co_return; + }); ChatCommandDefinition cc_whatene( {"$whatene"}, - +[](const ServerArgs& a) -> void { + +[](const Args& a) -> asio::awaitable { whatobj_whatene_fn(a, false, true); - }, - unavailable_on_proxy_server); + co_return; + }); ChatCommandDefinition cc_where( {"$where"}, - +[](const ServerArgs& a) -> void { - auto l = a.c->require_lobby(); - uint32_t floor = l->is_game() ? a.c->floor : 0x0F; - Episode episode = l->is_game() ? l->episode : Episode::EP1; - send_text_message_printf(a.c, "$C7%01" PRIX32 ":%s X:%" PRId32 " Z:%" PRId32, + +[](const Args& a) -> asio::awaitable { + uint32_t floor; + Episode episode; + auto l = a.c->lobby.lock(); + if (a.c->proxy_session) { + floor = a.c->floor; + episode = a.c->proxy_session->lobby_episode; + } else if (l && l->is_game()) { + floor = a.c->floor; + episode = l->episode; + } else { + floor = 0x0F; + episode = Episode::EP1; + } + + send_text_message_fmt(a.c, "$C7{:X}:{} X:{} Z:{}", floor, FloorDefinition::get(episode, floor).short_name, - static_cast(a.c->pos.x.load()), - static_cast(a.c->pos.z.load())); - if (l->is_game()) { + static_cast(a.c->pos.x), + static_cast(a.c->pos.z)); + + if (!a.c->proxy_session && l && l->is_game()) { for (auto lc : l->clients) { if (lc && (lc != a.c)) { string name = lc->character()->disp.name.decode(lc->language()); - send_text_message_printf(a.c, "$C6%s$C7 %01" PRIX32 ":%s", - name.c_str(), lc->floor, FloorDefinition::get(l->episode, lc->floor).short_name); + send_text_message_fmt(a.c, "$C6{}$C7 {:X}:{}", + name, lc->floor, FloorDefinition::get(l->episode, lc->floor).short_name); } } } - }, - unavailable_on_proxy_server); + co_return; + }); ChatCommandDefinition cc_writemem( {"$writemem"}, - +[](const ServerArgs& a) -> void { + +[](const Args& a) -> asio::awaitable { a.check_debug_enabled(); auto tokens = phosg::split(a.text, ' '); @@ -2967,31 +2881,27 @@ ChatCommandDefinition cc_writemem( tokens.erase(tokens.begin()); std::string data = phosg::parse_data_string(phosg::join(tokens, " ")); - prepare_client_for_patches(a.c, [wc = weak_ptr(a.c), addr, data]() { - auto c = wc.lock(); - if (!c) { - return; - } - try { - auto s = c->require_server_state(); - const char* function_name = is_dc(c->version()) - ? "WriteMemoryDC" - : is_gc(c->version()) - ? "WriteMemoryGC" - : "WriteMemoryX86"; - auto fn = s->function_code_index->name_to_function.at(function_name); - send_function_call(c, fn, {{"dest_addr", addr}, {"size", data.size()}}, data.data(), data.size()); - c->function_call_response_queue.emplace_back(empty_function_call_response_handler); - } catch (const out_of_range&) { - throw precondition_failed("Invalid patch name"); - } - }); - }, - unavailable_on_proxy_server); + co_await prepare_client_for_patches(a.c); + + try { + auto s = a.c->require_server_state(); + const char* function_name = is_dc(a.c->version()) + ? "WriteMemoryDC" + : is_gc(a.c->version()) + ? "WriteMemoryGC" + : "WriteMemoryX86"; + auto fn = s->function_code_index->name_to_function.at(function_name); + unordered_map label_writes{{"dest_addr", addr}, {"size", data.size()}}; + co_await send_function_call(a.c, fn, label_writes, data.data(), data.size()); + } catch (const out_of_range&) { + throw precondition_failed("Invalid patch name"); + } + co_return; + }); ChatCommandDefinition cc_nativecall( {"$nativecall"}, - +[](const ServerArgs& a) -> void { + +[](const Args& a) -> asio::awaitable { a.check_debug_enabled(); // TODO: $nativecall is not implemented on x86 (yet) because there are @@ -3014,30 +2924,25 @@ ChatCommandDefinition cc_nativecall( unordered_map label_writes{{"call_addr", addr}}; for (size_t z = 0; z < tokens.size() - 1; z++) { - label_writes.emplace(phosg::string_printf("arg%zu", z), stoull(tokens[z + 1], nullptr, 16)); + label_writes.emplace(std::format("arg{}", z), stoull(tokens[z + 1], nullptr, 16)); } - prepare_client_for_patches(a.c, [wc = weak_ptr(a.c), label_writes = std::move(label_writes)]() { - auto c = wc.lock(); - if (!c) { - return; - } - try { - auto s = c->require_server_state(); - const char* function_name = is_dc(c->version()) - ? "CallNativeFunctionDC" - : is_gc(c->version()) - ? "CallNativeFunctionGC" - : "CallNativeFunctionX86"; - auto fn = s->function_code_index->name_to_function.at(function_name); - send_function_call(c, fn, label_writes); - c->function_call_response_queue.emplace_back(empty_function_call_response_handler); - } catch (const out_of_range&) { - throw precondition_failed("Invalid patch name"); - } - }); - }, - unavailable_on_proxy_server); + co_await prepare_client_for_patches(a.c); + + try { + auto s = a.c->require_server_state(); + const char* function_name = is_dc(a.c->version()) + ? "CallNativeFunctionDC" + : is_gc(a.c->version()) + ? "CallNativeFunctionGC" + : "CallNativeFunctionX86"; + auto fn = s->function_code_index->name_to_function.at(function_name); + co_await send_function_call(a.c, fn, label_writes); + } catch (const out_of_range&) { + throw precondition_failed("Invalid patch name"); + } + co_return; + }); //////////////////////////////////////////////////////////////////////////////// // Dispatch methods @@ -3060,8 +2965,12 @@ struct SplitCommand { // This function is called every time any player sends a chat beginning with a // dollar sign. It is this function's responsibility to see if the chat is a // command, and to execute the command and block the chat if it is. -void on_chat_command(std::shared_ptr c, const std::string& text, bool check_permissions) { +asio::awaitable on_chat_command(std::shared_ptr c, const std::string& text, bool check_permissions) { SplitCommand cmd(text); + + // This function is only called by on_06 if it looks like a chat command + // (starts with $, or @ on 11/2000), so we just normalize all commands to $ + // here if (!cmd.name.empty() && cmd.name[0] == '@') { cmd.name[0] = '$'; } @@ -3070,43 +2979,17 @@ void on_chat_command(std::shared_ptr c, const std::string& text, bool ch try { def = ChatCommandDefinition::all_defs.at(cmd.name); } catch (const out_of_range&) { + } + if (!def) { send_text_message(c, "$C6Unknown command"); - return; + co_return; } - if (!def->server_handler) { - send_text_message(c, "$C6Command not available\non game server"); - } else { - try { - def->server_handler(ServerArgs{{cmd.args, check_permissions}, c}); - } catch (const precondition_failed& e) { - send_text_message(c, e.what()); - } catch (const exception& e) { - send_text_message(c, "$C6Failed:\n" + remove_color(e.what())); - } - } -} - -void on_chat_command(shared_ptr ses, const std::string& text, bool check_permissions) { - SplitCommand cmd(text); - - const ChatCommandDefinition* def = nullptr; try { - def = ChatCommandDefinition::all_defs.at(cmd.name); - } catch (const out_of_range&) { - send_text_message(ses->client_channel, "$C6Unknown command"); - return; - } - - if (!def->proxy_handler) { - send_text_message(ses->client_channel, "$C6Command not available\non proxy server"); - } else { - try { - def->proxy_handler(ProxyArgs{{cmd.args, check_permissions}, ses}); - } catch (const precondition_failed& e) { - send_text_message(ses->client_channel, e.what()); - } catch (const exception& e) { - send_text_message(ses->client_channel, "$C6Failed:\n" + remove_color(e.what())); - } + co_await def->handler(Args{cmd.args, check_permissions, c}); + } catch (const precondition_failed& e) { + send_text_message(c, e.what()); + } catch (const exception& e) { + send_text_message(c, "$C6Failed:\n" + remove_color(e.what())); } } diff --git a/src/ChatCommands.hh b/src/ChatCommands.hh index 50217500..286e0a7a 100644 --- a/src/ChatCommands.hh +++ b/src/ChatCommands.hh @@ -2,13 +2,13 @@ #include +#include #include #include #include "Client.hh" #include "Lobby.hh" -#include "ProxyServer.hh" +#include "ProxySession.hh" #include "ServerState.hh" -void on_chat_command(std::shared_ptr c, const std::string& text, bool check_permissions); -void on_chat_command(std::shared_ptr ses, const std::string& text, bool check_permissions); +asio::awaitable on_chat_command(std::shared_ptr c, const std::string& text, bool check_permissions); diff --git a/src/ChoiceSearch.hh b/src/ChoiceSearch.hh index 87476c05..1ea5f2f1 100644 --- a/src/ChoiceSearch.hh +++ b/src/ChoiceSearch.hh @@ -31,12 +31,12 @@ struct ChoiceSearchConfigT { operator ChoiceSearchConfigT() const { ChoiceSearchConfigT ret; - ret.disabled = this->disabled.load(); + ret.disabled = this->disabled; for (size_t z = 0; z < this->entries.size(); z++) { auto& ret_e = ret.entries[z]; const auto& this_e = this->entries[z]; - ret_e.parent_choice_id = this_e.parent_choice_id.load(); - ret_e.choice_id = this_e.choice_id.load(); + ret_e.parent_choice_id = this_e.parent_choice_id; + ret_e.choice_id = this_e.choice_id; } return ret; } diff --git a/src/Client.cc b/src/Client.cc index 2ca72f8f..d9a41120 100644 --- a/src/Client.cc +++ b/src/Client.cc @@ -1,16 +1,15 @@ #include "Client.hh" #include -#include -#include -#include #include #include #include +#include #include #include +#include "GameServer.hh" #include "IPStackSimulator.hh" #include "Loggers.hh" #include "SendCommands.hh" @@ -23,7 +22,7 @@ const uint64_t CLIENT_CONFIG_MAGIC = 0x8399AC32; static atomic next_id(1); -void Client::Config::set_flags_for_version(Version version, int64_t sub_version) { +void Client::set_flags_for_version(Version version, int64_t sub_version) { this->set_flag(Flag::PROXY_CHAT_COMMANDS_ENABLED); // BB shares some sub_version values with GC Episode 3, so we handle it @@ -153,17 +152,17 @@ void Client::Config::set_flags_for_version(Version version, int64_t sub_version) this->set_flag(Flag::CAN_RECEIVE_ENABLE_B2_QUEST); break; default: - throw runtime_error(phosg::string_printf("unknown sub_version %" PRIX64, sub_version)); + throw runtime_error(std::format("unknown sub_version {:X}", sub_version)); } } -Client::ItemDropNotificationMode Client::Config::get_drop_notification_mode() const { +Client::ItemDropNotificationMode Client::get_drop_notification_mode() const { uint8_t mode_s = (this->check_flag(Flag::ITEM_DROP_NOTIFICATIONS_1) ? 1 : 0) | (this->check_flag(Flag::ITEM_DROP_NOTIFICATIONS_2) ? 2 : 0); return static_cast(mode_s); } -void Client::Config::set_drop_notification_mode(ItemDropNotificationMode new_mode) { +void Client::set_drop_notification_mode(ItemDropNotificationMode new_mode) { uint8_t mode_s = static_cast(new_mode); if (mode_s & 1) { this->set_flag(Client::Flag::ITEM_DROP_NOTIFICATIONS_1); @@ -177,133 +176,121 @@ void Client::Config::set_drop_notification_mode(ItemDropNotificationMode new_mod } } -bool Client::Config::should_update_vs(const Config& other) const { - constexpr uint64_t mask = static_cast(Flag::CLIENT_SIDE_MASK); - return ((this->enabled_flags ^ other.enabled_flags) & mask) || - (this->specific_version != other.specific_version) || - (this->override_random_seed != other.override_random_seed) || - (this->override_section_id != other.override_section_id) || - (this->override_lobby_event != other.override_lobby_event) || - (this->override_lobby_number != other.override_lobby_number) || - (this->proxy_destination_address != other.proxy_destination_address) || - (this->proxy_destination_port != other.proxy_destination_port); -} - Client::Client( - shared_ptr server, - struct bufferevent* bev, - uint64_t virtual_network_id, - Version version, + shared_ptr server, + shared_ptr channel, ServerBehavior server_behavior) : server(server), id(next_id++), - log(phosg::string_printf("[C-%" PRIX64 "] ", this->id), client_log.min_level), - channel(bev, virtual_network_id, version, 1, nullptr, nullptr, this, "", phosg::TerminalFormat::FG_YELLOW, phosg::TerminalFormat::FG_GREEN), + log(std::format("[C-{:X}] ", this->id), client_log.min_level), + channel(channel), server_behavior(server_behavior), - should_disconnect(false), - should_send_to_lobby_server(false), - should_send_to_proxy_server(false), - bb_connection_phase(0xFF), - ping_start_time(0), - sub_version(-1), - floor(0), - lobby_client_id(0), - lobby_arrow_color(0), - preferred_lobby_id(-1), - save_game_data_event( - event_new( - bufferevent_get_base(bev), -1, EV_TIMEOUT | EV_PERSIST, - &Client::dispatch_save_game_data, this), - event_free), - send_ping_event( - event_new( - bufferevent_get_base(bev), -1, EV_TIMEOUT, - &Client::dispatch_send_ping, this), - event_free), - idle_timeout_event( - event_new( - bufferevent_get_base(bev), -1, EV_TIMEOUT, - &Client::dispatch_idle_timeout, this), - event_free), - card_battle_table_number(-1), - card_battle_table_seat_number(0), - card_battle_table_seat_state(0), - last_game_info_requested(0), - should_update_play_time(false), - bb_character_index(-1), - next_exp_value(0), - can_chat(true), - dol_base_addr(0), - external_bank_character_index(-1), - last_play_time_update(0) { + save_game_data_timer(*server->get_io_context()), + send_ping_timer(*server->get_io_context()), + idle_timeout_timer(*server->get_io_context()), + should_update_play_time(false) { this->update_channel_name(); - this->config.set_flags_for_version(version, -1); + // Don't print data sent to patch clients to the logs. The patch server + // protocol is fully understood and data logs for patch clients are generally + // more annoying than helpful at this point. auto s = server->get_state(); - if (is_v1_or_v2(this->version()) ? s->default_rare_notifs_enabled_v1_v2 : s->default_rare_notifs_enabled_v3_v4) { - this->config.set_drop_notification_mode(ItemDropNotificationMode::RARES_ONLY); + if (is_patch(this->version()) && s->hide_download_commands) { + this->channel->terminal_recv_color = phosg::TerminalFormat::END; + this->channel->terminal_send_color = phosg::TerminalFormat::END; + } else { + this->channel->terminal_recv_color = phosg::TerminalFormat::FG_GREEN; + this->channel->terminal_send_color = phosg::TerminalFormat::FG_YELLOW; } - this->config.specific_version = default_specific_version_for_version(version, -1); - memset(&this->next_connection_addr, 0, sizeof(this->next_connection_addr)); + this->set_flags_for_version(this->version(), -1); + if (is_v1_or_v2(this->version()) ? s->default_rare_notifs_enabled_v1_v2 : s->default_rare_notifs_enabled_v3_v4) { + this->set_drop_notification_mode(ItemDropNotificationMode::RARES_ONLY); + } + this->specific_version = default_specific_version_for_version(this->version(), -1); - this->reschedule_save_game_data_event(); - this->reschedule_ping_and_timeout_events(); + this->reschedule_save_game_data_timer(); + this->reschedule_ping_and_timeout_timers(); // Don't print data sent to patch clients to the logs. The patch server // protocol is fully understood and data logs for patch clients are generally // more annoying than helpful at this point. if ((s->hide_download_commands) && - ((this->channel.version == Version::PC_PATCH) || (this->channel.version == Version::BB_PATCH))) { - this->channel.terminal_recv_color = phosg::TerminalFormat::END; - this->channel.terminal_send_color = phosg::TerminalFormat::END; + ((this->version() == Version::PC_PATCH) || (this->version() == Version::BB_PATCH))) { + this->channel->terminal_recv_color = phosg::TerminalFormat::END; + this->channel->terminal_send_color = phosg::TerminalFormat::END; + } else { + this->channel->terminal_send_color = phosg::TerminalFormat::FG_YELLOW; + this->channel->terminal_recv_color = phosg::TerminalFormat::FG_GREEN; } - this->log.info("Created"); + this->log.info_f("Created"); } Client::~Client() { if (!this->disconnect_hooks.empty()) { - this->log.warning("Disconnect hooks pending at client destruction time:"); + this->log.warning_f("Disconnect hooks pending at client destruction time:"); for (const auto& it : this->disconnect_hooks) { - this->log.warning(" %s", it.first.c_str()); + this->log.warning_f(" {}", it.first); } } if ((this->version() == Version::BB_V4) && (this->character_data.get())) { this->save_all(); } - this->log.info("Deleted"); + this->log.info_f("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); + string default_name = this->channel->default_name(); auto player = this->character(false, false); if (player) { string name_str = player->disp.name.decode(this->language()); size_t level = player->disp.stats.level + 1; - this->channel.name = phosg::string_printf("C-%" PRIX64 " (%s Lv.%zu) @ %s", this->id, name_str.c_str(), level, ip_str.c_str()); + this->channel->name = std::format("C-{:X} ({} Lv.{}) @ {}", + this->id, name_str, level, default_name); } else { - this->channel.name = phosg::string_printf("C-%" PRIX64 " @ %s", this->id, ip_str.c_str()); + this->channel->name = std::format("C-{:X} @ {}", this->id, default_name); } - this->log.info("Channel name updated from player data: %s", this->channel.name.c_str()); + this->log.info_f("Channel name updated: {}", this->channel->name); } -void Client::reschedule_save_game_data_event() { - if (this->version() == Version::BB_V4) { - struct timeval tv = phosg::usecs_to_timeval(60000000); // 1 minute - event_add(this->save_game_data_event.get(), &tv); +void Client::reschedule_save_game_data_timer() { + if (this->version() != Version::BB_V4) { + return; } + this->save_game_data_timer.expires_after(std::chrono::seconds(60)); + this->idle_timeout_timer.async_wait([this](std::error_code ec) { + if (!ec) { + if (this->character(false)) { + this->save_all(); + } + } + }); } -void Client::reschedule_ping_and_timeout_events() { +void Client::reschedule_ping_and_timeout_timers() { auto s = this->require_server_state(); - struct timeval ping_tv = phosg::usecs_to_timeval(s->client_ping_interval_usecs); - event_add(this->send_ping_event.get(), &ping_tv); - struct timeval idle_tv = phosg::usecs_to_timeval(s->client_idle_timeout_usecs); - event_add(this->idle_timeout_event.get(), &idle_tv); + if (!is_patch(this->version())) { + this->send_ping_timer.expires_after(std::chrono::microseconds(s->client_ping_interval_usecs)); + this->send_ping_timer.async_wait([this](std::error_code ec) { + if (!ec) { + this->log.info_f("Sending ping command"); + // The game doesn't use this timestamp; we only use it for debugging purposes + be_uint64_t timestamp = phosg::now(); + this->channel->send(0x1D, 0x00, ×tamp, sizeof(be_uint64_t)); + } + }); + } + + this->idle_timeout_timer.expires_after(std::chrono::microseconds(s->client_idle_timeout_usecs)); + this->idle_timeout_timer.async_wait([this](std::error_code ec) { + if (!ec) { + this->log.info_f("Idle timeout expired"); + this->channel->disconnect(); + } + }); } void Client::convert_account_to_temporary_if_nte() { @@ -312,7 +299,7 @@ void Client::convert_account_to_temporary_if_nte() { // replace it with a temporary account. auto s = this->require_server_state(); if (s->use_temp_accounts_for_prototypes && this->login->account_was_created && is_any_nte(this->version())) { - this->log.info("Client is a prototype version and the account was created during this session; converting permanent account to temporary account"); + this->log.info_f("Client is a prototype version and the account was created during this session; converting permanent account to temporary account"); this->login->account->is_temporary = true; this->login->account->delete_file(); this->login->account_was_created = false; @@ -348,7 +335,7 @@ shared_ptr Client::team() const { auto s = this->require_server_state(); auto team = s->team_index->get_by_id(this->login->account->bb_team_id); if (!team) { - this->log.info("Account contains a team ID, but the team does not exist; clearing team ID from account"); + this->log.info_f("Account contains a team ID, but the team does not exist; clearing team ID from account"); this->login->account->bb_team_id = 0; this->login->account->save(); return nullptr; @@ -356,7 +343,7 @@ shared_ptr Client::team() const { auto member_it = team->members.find(this->login->account->account_id); if (member_it == team->members.end()) { - this->log.info("Account contains a team ID, but the team does not contain this member; clearing team ID from account"); + this->log.info_f("Account contains a team ID, but the team does not contain this member; clearing team ID from account"); this->login->account->bb_team_id = 0; this->login->account->save(); return nullptr; @@ -368,7 +355,7 @@ shared_ptr Client::team() const { auto& m = member_it->second; string name = p->disp.name.decode(this->language()); if (m.name != name) { - this->log.info("Updating player name in team config"); + this->log.info_f("Updating player name in team config"); s->team_index->update_member_name(this->login->account->account_id, name); } } @@ -402,9 +389,9 @@ bool Client::evaluate_quest_availability_expression( .v1_present = v1_present, }; int64_t ret = expr->evaluate(env); - if (this->log.should_log(phosg::LogLevel::INFO)) { + if (this->log.should_log(phosg::LogLevel::L_INFO)) { string expr_str = expr->str(); - this->log.info("Evaluated integral expression %s => %s", expr_str.c_str(), ret ? "TRUE" : "FALSE"); + this->log.info_f("Evaluated integral expression {} => {}", expr_str, ret ? "TRUE" : "FALSE"); } return ret; } @@ -448,62 +435,11 @@ bool Client::can_use_chat_commands() const { return this->require_server_state()->enable_chat_commands; } -void Client::dispatch_save_game_data(evutil_socket_t, short, void* ctx) { - reinterpret_cast(ctx)->save_game_data(); -} - -void Client::save_game_data() { - if (this->version() != Version::BB_V4) { - throw logic_error("save_game_data called for non-BB client"); - } - if (this->character(false)) { - this->save_all(); - } -} - -void Client::dispatch_send_ping(evutil_socket_t, short, void* ctx) { - reinterpret_cast(ctx)->send_ping(); -} - -void Client::send_ping() { - if (!is_patch(this->version())) { - this->log.info("Sending ping command"); - // The game doesn't use this timestamp; we only use it for debugging purposes - be_uint64_t timestamp = phosg::now(); - try { - this->channel.send(0x1D, 0x00, ×tamp, sizeof(be_uint64_t)); - } catch (const exception& e) { - this->log.info("Failed to send ping: %s", e.what()); - } - } -} - -void Client::dispatch_idle_timeout(evutil_socket_t, short, void* ctx) { - reinterpret_cast(ctx)->idle_timeout(); -} - -void Client::idle_timeout() { - this->log.info("Idle timeout expired"); - auto s = this->server.lock(); - if (s) { - auto c = this->shared_from_this(); - s->disconnect_client(c); - } else { - this->log.info("Server is deleted; cannot disconnect client"); - } -} - -void Client::suspend_timeouts() { - event_del(this->send_ping_event.get()); - event_del(this->idle_timeout_event.get()); - this->log.info("Timeouts suspended"); -} - void Client::set_login(shared_ptr login) { this->login = login; - if (this->log.should_log(phosg::LogLevel::INFO)) { + if (this->log.should_log(phosg::LogLevel::L_INFO)) { string login_str = this->login->str(); - this->log.info("Login: %s", login_str.c_str()); + this->log.info_f("Login: {}", login_str); } } @@ -669,7 +605,7 @@ string Client::system_filename() const { if (!this->login || !this->login->bb_license) { throw logic_error("client is not logged in"); } - return phosg::string_printf("system/players/system_%s.psosys", this->login->bb_license->username.c_str()); + return std::format("system/players/system_{}.psosys", this->login->bb_license->username); } string Client::character_filename(const std::string& bb_username, ssize_t index) { @@ -679,11 +615,11 @@ string Client::character_filename(const std::string& bb_username, ssize_t index) if (index < 0) { throw logic_error("character index is not set"); } - return phosg::string_printf("system/players/player_%s_%zd.psochar", bb_username.c_str(), index); + return std::format("system/players/player_{}_{}.psochar", bb_username, index); } string Client::backup_character_filename(uint32_t account_id, size_t index, bool is_ep3) { - return phosg::string_printf("system/players/backup_player_%" PRIu32 "_%zu.%s", + return std::format("system/players/backup_player_{}_{}.{}", account_id, index, is_ep3 ? "pso3char" : "psochar"); } @@ -704,7 +640,7 @@ string Client::guild_card_filename() const { if (!this->login || !this->login->bb_license) { throw logic_error("client is not logged in"); } - return phosg::string_printf("system/players/guild_cards_%s.psocard", this->login->bb_license->username.c_str()); + return std::format("system/players/guild_cards_{}.psocard", this->login->bb_license->username); } string Client::shared_bank_filename() const { @@ -714,7 +650,7 @@ string Client::shared_bank_filename() const { if (!this->login || !this->login->bb_license) { throw logic_error("client is not logged in"); } - return phosg::string_printf("system/players/shared_bank_%s.psobank", this->login->bb_license->username.c_str()); + return std::format("system/players/shared_bank_{}.psobank", this->login->bb_license->username); } string Client::legacy_account_filename() const { @@ -724,7 +660,7 @@ string Client::legacy_account_filename() const { if (!this->login || !this->login->bb_license) { throw logic_error("client is not logged in"); } - return phosg::string_printf("system/players/account_%s.nsa", this->login->bb_license->username.c_str()); + return std::format("system/players/account_{}.nsa", this->login->bb_license->username); } string Client::legacy_player_filename() const { @@ -737,9 +673,9 @@ string Client::legacy_player_filename() const { if (this->bb_character_index < 0) { throw logic_error("character index is not set"); } - return phosg::string_printf( - "system/players/player_%s_%zd.nsc", - this->login->bb_license->username.c_str(), + return std::format( + "system/players/player_{}_{}.nsc", + this->login->bb_license->username, static_cast(this->bb_character_index + 1)); } @@ -772,25 +708,25 @@ void Client::load_all_files() { string sys_filename = this->system_filename(); this->system_data = files_manager->get_system(sys_filename); if (this->system_data) { - player_data_log.info("Using loaded system file %s", sys_filename.c_str()); - } else if (phosg::isfile(sys_filename)) { + player_data_log.info_f("Using loaded system file {}", sys_filename); + } else if (std::filesystem::is_regular_file(sys_filename)) { this->system_data = make_shared(phosg::load_object_file(sys_filename, true)); files_manager->set_system(sys_filename, this->system_data); - player_data_log.info("Loaded system data from %s", sys_filename.c_str()); + player_data_log.info_f("Loaded system data from {}", sys_filename); } else { - player_data_log.info("System file is missing: %s", sys_filename.c_str()); + player_data_log.info_f("System file is missing: {}", sys_filename); } if (this->bb_character_index >= 0) { string char_filename = this->character_filename(); this->character_data = files_manager->get_character(char_filename); if (this->character_data) { - player_data_log.info("Using loaded character file %s", char_filename.c_str()); - } else if (phosg::isfile(char_filename)) { + player_data_log.info_f("Using loaded character file {}", char_filename); + } else if (std::filesystem::is_regular_file(char_filename)) { auto psochar = PSOCHARFile::load_shared(char_filename, !this->system_data); this->character_data = psochar.character_file; 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_f("Loaded character data from {}", char_filename); // If there was no .psosys file, use the system file from the .psochar // file instead @@ -800,34 +736,34 @@ void Client::load_all_files() { } this->system_data = psochar.system_file; 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_f("Loaded system data from {}", char_filename); } this->update_character_data_after_load(this->character_data); this->system_data->language = this->language(); } else { - player_data_log.info("Character file is missing: %s", char_filename.c_str()); + player_data_log.info_f("Character file is missing: {}", char_filename); } } string card_filename = this->guild_card_filename(); this->guild_card_data = files_manager->get_guild_card(card_filename); if (this->guild_card_data) { - player_data_log.info("Using loaded Guild Card file %s", card_filename.c_str()); - } else if (phosg::isfile(card_filename)) { + player_data_log.info_f("Using loaded Guild Card file {}", card_filename); + } else if (std::filesystem::is_regular_file(card_filename)) { this->guild_card_data = make_shared(phosg::load_object_file(card_filename)); files_manager->set_guild_card(card_filename, this->guild_card_data); - player_data_log.info("Loaded Guild Card data from %s", card_filename.c_str()); + player_data_log.info_f("Loaded Guild Card data from {}", card_filename); } else { - player_data_log.info("Guild Card file is missing: %s", card_filename.c_str()); + player_data_log.info_f("Guild Card file is missing: {}", card_filename); } // If any of the above files were missing, try to load from .nsa/.nsc files instead if (!this->system_data || (!this->character_data && (this->bb_character_index >= 0)) || !this->guild_card_data) { string nsa_filename = this->legacy_account_filename(); shared_ptr nsa_data; - if (phosg::isfile(nsa_filename)) { + if (std::filesystem::is_regular_file(nsa_filename)) { nsa_data = make_shared(phosg::load_object_file(nsa_filename)); if (!nsa_data->signature.eq(LegacySavedAccountDataBB::SIGNATURE)) { throw runtime_error("account data header is incorrect"); @@ -835,12 +771,12 @@ void Client::load_all_files() { if (!this->system_data) { this->system_data = make_shared(nsa_data->system_file); files_manager->set_system(sys_filename, this->system_data); - player_data_log.info("Loaded legacy system data from %s", nsa_filename.c_str()); + player_data_log.info_f("Loaded legacy system data from {}", nsa_filename); } if (!this->guild_card_data) { this->guild_card_data = make_shared(nsa_data->guild_card_file); files_manager->set_guild_card(card_filename, this->guild_card_data); - player_data_log.info("Loaded legacy Guild Card data from %s", nsa_filename.c_str()); + player_data_log.info_f("Loaded legacy Guild Card data from {}", nsa_filename); } } @@ -854,12 +790,12 @@ void Client::load_all_files() { this->system_data->joystick_config = *s->bb_default_joystick_config; } files_manager->set_system(sys_filename, this->system_data); - player_data_log.info("Created new system data"); + player_data_log.info_f("Created new system data"); } if (!this->guild_card_data) { this->guild_card_data = make_shared(); files_manager->set_guild_card(card_filename, this->guild_card_data); - player_data_log.info("Created new Guild Card data"); + player_data_log.info_f("Created new Guild Card data"); } if (!this->character_data && (this->bb_character_index >= 0)) { @@ -900,9 +836,9 @@ void Client::load_all_files() { this->character_data->option_flags = nsa_data->option_flags; this->character_data->symbol_chats = nsa_data->symbol_chats; this->character_data->shortcuts = nsa_data->shortcuts; - player_data_log.info("Loaded legacy player data from %s and %s", nsa_filename.c_str(), nsc_filename.c_str()); + player_data_log.info_f("Loaded legacy player data from {} and {}", nsa_filename, nsc_filename); } else { - player_data_log.info("Loaded legacy player data from %s", nsc_filename.c_str()); + player_data_log.info_f("Loaded legacy player data from {}", nsc_filename); } this->update_character_data_after_load(this->character_data); } @@ -928,7 +864,7 @@ void Client::update_character_data_after_load(shared_ptr cha 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)); + player_data_log.info_f("Overriding language fields in save files with {:02X} ({})", lang, char_for_language_code(lang)); charfile->inventory.language = lang; charfile->guild_card.language = lang; } @@ -946,7 +882,7 @@ void Client::save_all() { if (this->external_bank) { string filename = this->shared_bank_filename(); phosg::save_object_file(filename, *this->external_bank); - player_data_log.info("Saved shared bank file %s", filename.c_str()); + player_data_log.info_f("Saved shared bank file {}", filename); } if (this->external_bank_character) { this->save_character_file( @@ -962,7 +898,7 @@ void Client::save_system_file() const { } string filename = this->system_filename(); phosg::save_object_file(filename, *this->system_data); - player_data_log.info("Saved system file %s", filename.c_str()); + player_data_log.info_f("Saved system file {}", filename); } void Client::save_character_file( @@ -970,14 +906,14 @@ void Client::save_character_file( shared_ptr system, shared_ptr character) { PSOCHARFile::save(filename, system, character); - player_data_log.info("Saved character file %s", filename.c_str()); + player_data_log.info_f("Saved character file {}", filename); } void Client::save_ep3_character_file( const string& filename, const PSOGCEp3CharacterFile::Character& character) { phosg::save_file(filename, &character, sizeof(character)); - player_data_log.info("Saved Episode 3 character file %s", filename.c_str()); + player_data_log.info_f("Saved Episode 3 character file {}", filename); } void Client::save_character_file() { @@ -993,7 +929,7 @@ void Client::save_character_file() { uint64_t t = phosg::now(); uint64_t seconds = (t - this->last_play_time_update) / 1000000; this->character_data->play_time_seconds += seconds; - player_data_log.info("Added %" PRIu64 " seconds to play time", seconds); + player_data_log.info_f("Added {} seconds to play time", seconds); this->last_play_time_update = t; } @@ -1006,7 +942,7 @@ void Client::save_guild_card_file() const { } string filename = this->guild_card_filename(); phosg::save_object_file(filename, *this->guild_card_data); - player_data_log.info("Saved Guild Card file %s", filename.c_str()); + player_data_log.info_f("Saved Guild Card file {}", filename); } void Client::load_backup_character(uint32_t account_id, size_t index) { @@ -1030,7 +966,7 @@ void Client::save_and_unload_character() { if (this->character_data) { this->save_character_file(); this->character_data.reset(); - this->log.info("Unloaded character"); + this->log.info_f("Unloaded character"); } } @@ -1056,13 +992,13 @@ void Client::use_default_bank() { string filename = this->shared_bank_filename(); phosg::save_object_file(filename, *this->external_bank); this->external_bank.reset(); - player_data_log.info("Detached shared bank %s", filename.c_str()); + player_data_log.info_f("Detached shared bank {}", filename); } if (this->external_bank_character) { string filename = this->character_filename(this->external_bank_character_index); this->save_character_file(filename, this->system_data, this->external_bank_character); this->external_bank_character.reset(); - player_data_log.info("Detached character %s from bank", filename.c_str()); + player_data_log.info_f("Detached character {} from bank", filename); } } @@ -1073,17 +1009,17 @@ bool Client::use_shared_bank() { auto files_manager = this->require_server_state()->player_files_manager; this->external_bank = files_manager->get_bank(filename); if (this->external_bank) { - player_data_log.info("Using loaded shared bank %s", filename.c_str()); + player_data_log.info_f("Using loaded shared bank {}", filename); return true; - } else if (phosg::isfile(filename)) { + } else if (std::filesystem::is_regular_file(filename)) { this->external_bank = make_shared(phosg::load_object_file(filename)); files_manager->set_bank(filename, this->external_bank); - player_data_log.info("Loaded shared bank %s", filename.c_str()); + player_data_log.info_f("Loaded shared bank {}", filename); return true; } else { this->external_bank = make_shared(); files_manager->set_bank(filename, this->external_bank); - player_data_log.info("Created shared bank for %s", filename.c_str()); + player_data_log.info_f("Created shared bank for {}", filename); return false; } } @@ -1097,13 +1033,13 @@ void Client::use_character_bank(ssize_t index) { this->external_bank_character = files_manager->get_character(filename); if (this->external_bank_character) { this->external_bank_character_index = index; - player_data_log.info("Using loaded character file %s for external bank", filename.c_str()); - } else if (phosg::isfile(filename)) { + player_data_log.info_f("Using loaded character file {} for external bank", filename); + } else if (std::filesystem::is_regular_file(filename)) { this->external_bank_character = PSOCHARFile::load_shared(filename, false).character_file; this->update_character_data_after_load(this->external_bank_character); this->external_bank_character_index = index; 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_f("Loaded character data from {} for external bank", filename); } else { throw runtime_error("character does not exist"); } @@ -1113,26 +1049,45 @@ void Client::use_character_bank(ssize_t index) { void Client::print_inventory(FILE* stream) const { auto s = this->require_server_state(); auto p = this->character(); - fprintf(stream, "[PlayerInventory] Meseta: %" PRIu32 "\n", p->disp.stats.meseta.load()); - fprintf(stream, "[PlayerInventory] %hhu items\n", p->inventory.num_items); + phosg::fwrite_fmt(stream, "[PlayerInventory] Meseta: {}\n", p->disp.stats.meseta); + phosg::fwrite_fmt(stream, "[PlayerInventory] {} items\n", p->inventory.num_items); for (size_t x = 0; x < p->inventory.num_items; x++) { const auto& item = p->inventory.items[x]; auto hex = item.data.hex(); auto name = s->describe_item(this->version(), item.data, false); - fprintf(stream, "[PlayerInventory] %2zu: [+%08" PRIX32 "] %s (%s)\n", x, item.flags.load(), hex.c_str(), name.c_str()); + phosg::fwrite_fmt(stream, "[PlayerInventory] {:2}: [+{:08X}] {} ({})\n", x, item.flags, hex, name); } } void Client::print_bank(FILE* stream) const { auto s = this->require_server_state(); auto bank = this->current_bank(); - fprintf(stream, "[PlayerBank] Meseta: %" PRIu32 "\n", bank.meseta.load()); - fprintf(stream, "[PlayerBank] %" PRIu32 " items\n", bank.num_items.load()); + phosg::fwrite_fmt(stream, "[PlayerBank] Meseta: {}\n", bank.meseta); + phosg::fwrite_fmt(stream, "[PlayerBank] {} items\n", bank.num_items); for (size_t x = 0; x < bank.num_items; x++) { const auto& item = bank.items[x]; const char* present_token = item.present ? "" : " (missing present flag)"; auto hex = item.data.hex(); auto name = s->describe_item(this->version(), item.data, false); - fprintf(stream, "[PlayerBank] %3zu: %s (%s) (x%hu)%s\n", x, hex.c_str(), name.c_str(), item.amount.load(), present_token); + phosg::fwrite_fmt(stream, "[PlayerBank] {:3}: {} ({}) (x{}){}\n", x, hex, name, item.amount, present_token); } } + +void Client::cancel_pending_promises() { + for (const auto& promise : this->function_call_response_queue) { + if (!promise->done()) { + promise->cancel(); + } + } + this->function_call_response_queue.clear(); + + if (this->character_data_ready_promise && !this->character_data_ready_promise->done()) { + this->character_data_ready_promise->cancel(); + } + this->character_data_ready_promise.reset(); + + if (this->enable_save_promise && !this->enable_save_promise->done()) { + this->enable_save_promise->cancel(); + } + this->enable_save_promise.reset(); +} diff --git a/src/Client.hh b/src/Client.hh index b82ff93e..bc598335 100644 --- a/src/Client.hh +++ b/src/Client.hh @@ -1,11 +1,10 @@ #pragma once -#include - #include #include #include "Account.hh" +#include "AsyncUtils.hh" #include "Channel.hh" #include "CommandFormats.hh" #include "Episode3/BattleRecord.hh" @@ -15,6 +14,7 @@ #include "PSOEncryption.hh" #include "PSOProtocol.hh" #include "PatchFileIndex.hh" +#include "ProxySession.hh" #include "Quest.hh" #include "QuestScript.hh" #include "TeamIndex.hh" @@ -22,21 +22,22 @@ extern const uint64_t CLIENT_CONFIG_MAGIC; -class Server; +class GameServer; struct Lobby; class Parsed6x70Data; +struct GetPlayerInfoResult { + // Exactly one of the following two shared_ptrs is not null + std::shared_ptr character; + std::shared_ptr ep3_character; + bool is_full_info; // True if the client sent 30; false if it was 61 or 98 +}; + class Client : public std::enable_shared_from_this { public: enum class Flag : uint64_t { // clang-format off - // This mask specifies which flags are sent to the client - // TODO: It'd be nice to use a pattern here (e.g. all server-side flags are - // in the high bits) but that would require re-recording or manually - // rewriting all the tests - CLIENT_SIDE_MASK = 0xEF3CFFFF7C0BFFFB, - // Version-related flags CHECKED_FOR_DC_V1_PROTOTYPE = 0x0000000000000002, NO_D6_AFTER_LOBBY = 0x0000000000000100, @@ -61,8 +62,6 @@ public: SAVE_ENABLED = 0x0000000004000000, HAS_EP3_CARD_DEFS = 0x0000000008000000, HAS_EP3_MEDIA_UPDATES = 0x0000000010000000, - USE_OVERRIDE_RANDOM_SEED = 0x0000000020000000, - HAS_GUILD_CARD_NUMBER = 0x0000000040000000, HAS_AUTO_PATCHES = 0x0000004000000000, AT_BANK_COUNTER = 0x0000000080000000, // Server-side only SHOULD_SEND_ARTIFICIAL_ITEM_STATE = 0x0001000000000000, // Server-side only @@ -86,16 +85,10 @@ public: PROXY_SAVE_FILES = 0x0000001000000000, PROXY_CHAT_COMMANDS_ENABLED = 0x0000002000000000, PROXY_PLAYER_NOTIFICATIONS_ENABLED = 0x0000008000000000, - PROXY_SUPPRESS_CLIENT_PINGS = 0x0000010000000000, - PROXY_SUPPRESS_REMOTE_LOGIN = 0x0000020000000000, - PROXY_ZERO_REMOTE_GUILD_CARD = 0x0000040000000000, PROXY_EP3_INFINITE_MESETA_ENABLED = 0x0000080000000000, PROXY_EP3_INFINITE_TIME_ENABLED = 0x0000100000000000, - PROXY_RED_NAME_ENABLED = 0x0000200000000000, - PROXY_BLANK_NAME_ENABLED = 0x0000400000000000, PROXY_BLOCK_FUNCTION_CALLS = 0x0000800000000000, PROXY_EP3_UNMASK_WHISPERS = 0x0008000000000000, - PROXY_VIRTUAL_CLIENT = 0x0400000000000000, // clang-format on }; enum class ItemDropNotificationMode { @@ -107,123 +100,70 @@ public: static constexpr uint64_t DEFAULT_FLAGS = static_cast(Flag::PROXY_CHAT_COMMANDS_ENABLED); - struct Config { - uint64_t enabled_flags = DEFAULT_FLAGS; // Client::Flag enum - uint32_t specific_version = 0; - int32_t override_random_seed = 0; - uint8_t override_section_id = 0xFF; // FF = no override - uint8_t override_lobby_event = 0xFF; // FF = no override - uint8_t override_lobby_number = 0x80; // 80 = no override - uint32_t proxy_destination_address = 0; - uint16_t proxy_destination_port = 0; - - Config() = default; - - bool operator==(const Config& other) const = default; - bool operator!=(const Config& other) const = default; - - bool should_update_vs(const Config& other) const; - - [[nodiscard]] static inline bool check_flag(uint64_t enabled_flags, Flag flag) { - return !!(enabled_flags & static_cast(flag)); - } - - [[nodiscard]] inline bool check_flag(Flag flag) const { - return this->check_flag(this->enabled_flags, flag); - } - inline void set_flag(Flag flag) { - this->enabled_flags |= static_cast(flag); - } - inline void clear_flag(Flag flag) { - this->enabled_flags &= (~static_cast(flag)); - } - inline void toggle_flag(Flag flag) { - this->enabled_flags ^= static_cast(flag); - } - - void set_flags_for_version(Version version, int64_t sub_version); - - ItemDropNotificationMode get_drop_notification_mode() const; - void set_drop_notification_mode(ItemDropNotificationMode new_mode); - - template - void parse_from(const parray& data) { - phosg::StringReader r(data.data(), data.size()); - if (r.get_u32l() != CLIENT_CONFIG_MAGIC) { - throw std::invalid_argument("config signature is incorrect"); - } - this->specific_version = r.get_u32l(); - this->enabled_flags = r.get_u64l(); - this->override_random_seed = r.get_u32l(); - this->proxy_destination_address = r.get_u32b(); - this->proxy_destination_port = r.get_u16l(); - this->override_section_id = r.get_u8(); - this->override_lobby_event = r.get_u8(); - this->override_lobby_number = r.get_u8(); - } - - template - void serialize_into(parray& data) const { - phosg::StringWriter w; - w.put_u32l(CLIENT_CONFIG_MAGIC); - w.put_u32l(this->specific_version); - w.put_u64l(this->enabled_flags & static_cast(Flag::CLIENT_SIDE_MASK)); - w.put_u32l(this->override_random_seed); - w.put_u32b(this->proxy_destination_address); - w.put_u16l(this->proxy_destination_port); - w.put_u8(this->override_section_id); - w.put_u8(this->override_lobby_event); - w.put_u8(this->override_lobby_number); - - const auto& s = w.str(); - for (size_t z = 0; z < s.size(); z++) { - data[z] = s[z]; - } - data.clear_after(s.size(), 0xFF); - } - }; - - std::weak_ptr server; + std::weak_ptr server; uint64_t id; phosg::PrefixedLogger log; + // Account information (not all of these are used; depends on game version) + std::string username; + std::string password; + std::string email_address; + uint64_t hardware_id = 0; + int32_t sub_version = 0; + uint8_t bb_client_code = 0; + uint8_t bb_connection_phase = 0xFF; + ssize_t bb_character_index = -1; // -1 = not set + uint32_t bb_security_token = 0; + parray bb_client_config; + std::string login_character_name; + std::string serial_number; + std::string access_key; + std::string serial_number2; + std::string access_key2; + std::string v1_serial_number; + std::string v1_access_key; + XBNetworkLocation xb_netloc; + parray xb_unknown_a1a; + uint64_t xb_user_id = 0; + uint32_t xb_unknown_a1b = 0; std::shared_ptr login; + std::shared_ptr proxy_session; + + // Patch server state (only used for PC_PATCH and BB_PATCH versions) + std::vector patch_file_checksum_requests; // Network - Channel channel; - struct sockaddr_storage next_connection_addr; + std::shared_ptr channel; + std::shared_ptr bb_detector_crypt; ServerBehavior server_behavior; - bool should_disconnect; - bool should_send_to_lobby_server; - bool should_send_to_proxy_server; std::unordered_map> disconnect_hooks; - std::shared_ptr xb_netloc; - parray xb_9E_unknown_a1a; - uint8_t bb_connection_phase; - uint64_t ping_start_time; + uint64_t ping_start_time = 0; - // Lobby/positioning - Config config; - Config synced_config; + // Basic state + uint64_t enabled_flags = DEFAULT_FLAGS; // Client::Flag enum + uint32_t specific_version = 0; + uint8_t override_section_id = 0xFF; // FF = no override + uint8_t override_lobby_event = 0xFF; // FF = no override + uint8_t override_lobby_number = 0x80; // 80 = no override + int64_t override_random_seed = -1; std::unique_ptr override_variations; - int32_t sub_version; VectorXZF pos; - uint32_t floor; + uint32_t floor = 0x0F; std::weak_ptr lobby; - uint8_t lobby_client_id; - uint8_t lobby_arrow_color; - int64_t preferred_lobby_id; // <0 = no preference + uint8_t lobby_client_id = 0; + uint8_t lobby_arrow_color = 0; + int64_t preferred_lobby_id = -1; // <0 = no preference - std::unique_ptr save_game_data_event; - std::unique_ptr send_ping_event; - std::unique_ptr idle_timeout_event; - int16_t card_battle_table_number; - uint16_t card_battle_table_seat_number; - uint16_t card_battle_table_seat_state; + asio::steady_timer save_game_data_timer; + asio::steady_timer send_ping_timer; + asio::steady_timer idle_timeout_timer; + int16_t card_battle_table_number = -1; + uint16_t card_battle_table_seat_number = 0; + uint16_t card_battle_table_seat_state = 0; std::weak_ptr ep3_tournament_team; std::shared_ptr ep3_prev_battle_record; std::shared_ptr last_menu_sent; - uint32_t last_game_info_requested; + uint32_t last_game_info_requested = 0; struct JoinCommand { uint16_t command; uint32_t flag; @@ -246,52 +186,72 @@ public: std::unordered_set blocked_senders; std::unique_ptr v1_v2_last_reported_disp; std::shared_ptr 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 pending_item_trade; std::unique_ptr pending_card_trade; - uint32_t telepipe_lobby_id; + uint32_t telepipe_lobby_id = 0; TelepipeState telepipe_state; std::shared_ptr ep3_config; // Null for non-Ep3 - ssize_t bb_character_index; // -1 = not set ItemData bb_identify_result; std::array, 3> bb_shop_contents; // Miscellaneous (used by chat commands) - uint32_t next_exp_value; // next EXP value to give - bool can_chat; - struct PendingCharacterExport { - std::shared_ptr dest_account; - ssize_t character_index = -1; - std::shared_ptr dest_bb_license; // Only used for $bbchar; null for $savechar - }; - std::unique_ptr pending_character_export; - std::deque> function_call_response_queue; + uint32_t next_exp_value = 0; // next EXP value to give + bool can_chat = true; + // NOTE: If you add any new optional promises here, make sure to also add + // them to cancel_pending_promises. + // NOTE: Entries in this queue can be nullptr; that represents a B2 command + // sent by the remote server during a proxy session. We can't just omit those + // from the queue entirely, because if we did, we could end up sending the + // wrong B3 response back. + std::deque>> function_call_response_queue; + std::shared_ptr> character_data_ready_promise; + std::shared_ptr> enable_save_promise; // File loading state - uint32_t dol_base_addr; - std::shared_ptr loading_dol_file; std::unordered_map> sending_files; Client( - std::shared_ptr server, - struct bufferevent* bev, - uint64_t virtual_network_id, - Version version, + std::shared_ptr server, + std::shared_ptr channel, ServerBehavior server_behavior); ~Client(); void update_channel_name(); - void reschedule_save_game_data_event(); - void reschedule_ping_and_timeout_events(); + void reschedule_save_game_data_timer(); + void reschedule_ping_and_timeout_timers(); inline Version version() const { - return this->channel.version; + return this->channel->version; } inline uint8_t language() const { - return this->channel.language; + return this->channel->language; } + [[nodiscard]] static inline bool check_flag(uint64_t enabled_flags, Flag flag) { + return !!(enabled_flags & static_cast(flag)); + } + + [[nodiscard]] inline bool check_flag(Flag flag) const { + return this->check_flag(this->enabled_flags, flag); + } + inline void set_flag(Flag flag) { + this->enabled_flags |= static_cast(flag); + } + inline void clear_flag(Flag flag) { + this->enabled_flags &= (~static_cast(flag)); + } + inline void toggle_flag(Flag flag) { + this->enabled_flags ^= static_cast(flag); + } + + void set_flags_for_version(Version version, int64_t sub_version); + + ItemDropNotificationMode get_drop_notification_mode() const; + void set_drop_notification_mode(ItemDropNotificationMode new_mode); + void convert_account_to_temporary_if_nte(); void sync_config(); @@ -325,15 +285,6 @@ public: bool can_use_chat_commands() const; - static void dispatch_save_game_data(evutil_socket_t, short, void* ctx); - void save_game_data(); - static void dispatch_send_ping(evutil_socket_t, short, void* ctx); - void send_ping(); - static void dispatch_idle_timeout(evutil_socket_t, short, void* ctx); - void idle_timeout(); - - void suspend_timeouts(); - void set_login(std::shared_ptr login); void create_battle_overlay(std::shared_ptr rules, std::shared_ptr level_table); @@ -397,6 +348,8 @@ public: void print_inventory(FILE* stream) const; void print_bank(FILE* stream) const; + void cancel_pending_promises(); + private: // The overlay character data is used in battle and challenge modes, when // character data is temporarily replaced in-game. In other play modes and in @@ -407,10 +360,8 @@ private: std::shared_ptr guild_card_data; std::shared_ptr external_bank; std::shared_ptr external_bank_character; - ssize_t external_bank_character_index; - uint64_t last_play_time_update; - - void save_and_clear_external_bank(); + ssize_t external_bank_character_index = -1; + uint64_t last_play_time_update = 0; void load_all_files(); void update_character_data_after_load(std::shared_ptr character_data); diff --git a/src/CommandFormats.hh b/src/CommandFormats.hh index 36080c4f..3f63d31b 100644 --- a/src/CommandFormats.hh +++ b/src/CommandFormats.hh @@ -172,7 +172,7 @@ struct C_Login_Patch_04 { parray unused; pstring username; pstring password; - pstring email; + pstring email_address; } __packed_ws__(C_Login_Patch_04, 0x6C); // 05 (S->C): Disconnect @@ -514,13 +514,23 @@ struct S_UpdateClientConfig_BB_04 { // appears in PSO v1 and v2, but it is not used, implying that at some point // there was a separate command to send the block list, but it was scrapped. // Perhaps this was used for command A1, which is identical to 07 and A0 in all -// versions of PSO (except DC NTE). +// versions of PSO (except DC NTE, whch uses 07/8E/8F instead). -// This command sets the interaction mode, which affects which objects can be -// interacted with and what certain controls do. It also affects some in-game -// behaviors; for example, if the leader's interaction mode is set incorrectly -// in a game, the leader will not send the game state to joining players, so -// they will wait forever and not be able to actually join. +// In all PSO versions except DC NTE and 11/2000, this command sets the +// interaction mode, which affects which objects can be interacted with and +// what certain controls do. It also affects some in-game behaviors; for +// example, if the leader's interaction mode is set incorrectly in a game, the +// leader will not send the game state to joining players, so they will wait +// forever and not be able to actually join. + +// In DC NTE and 11/2000, a side effect of this command not setting the +// interaction mode is that the menu doesn't work properly unless another +// command that sets the correct interaction mode (1) is sent before it. The +// user-visible effects of the interaction mode not being set are that the +// "Please select a ship" message doesn't appear, the X button does nothing, +// and selecting an item from the menu doesn't dismiss the menu and instead +// softlocks, since the client doesn't send anything.) One such command that +// sets the correct interaction mode is command 04 with an error code of 0. // The menu is titled "Ship Select" unless the first menu item begins with the // text "BLOCK" (all caps), in which case it is titled "Block Select". @@ -1627,7 +1637,7 @@ struct C_ConnectionInfo_DCNTE_8A { le_uint32_t unused = 0; pstring username; pstring password; - pstring email_address; // From Sylverant documentation + pstring email_address; } __packed_ws__(C_ConnectionInfo_DCNTE_8A, 0xA0); // 8A (S->C): Connection information result (DC NTE only) @@ -1661,7 +1671,7 @@ struct C_Login_DCNTE_8B { pstring access_key; pstring username; pstring password; - pstring name; + pstring login_character_name; parray unused; } __packed_ws__(C_Login_DCNTE_8B, 0xAC); @@ -1720,7 +1730,7 @@ struct C_RegisterV1_DC_92 { parray unused2; pstring serial_number2; pstring access_key2; - pstring email; // According to Sylverant documentation + pstring email_address; } __packed_ws__(C_RegisterV1_DC_92, 0xA0); // 92 (S->C): Register result (non-BB) @@ -1741,7 +1751,7 @@ struct C_LoginV1_DC_93 { /* 29 */ pstring access_key; /* 3A */ pstring serial_number2; /* 6A */ pstring access_key2; - /* 9A */ pstring name; + /* 9A */ pstring login_character_name; /* AA */ parray unused2; /* AC */ } __packed_ws__(C_LoginV1_DC_93, 0xAC); @@ -1753,11 +1763,11 @@ struct C_LoginExtendedV1_DC_93 : C_LoginV1_DC_93 { // 93 (C->S): Log in (BB) struct C_LoginBase_BB_93 { - le_uint32_t player_tag = 0x00010000; - le_uint32_t guild_card_number = 0; - le_uint32_t sub_version = 0; - uint8_t language = 0; - int8_t character_slot = 0; + /* 00 */ le_uint32_t player_tag = 0x00010000; + /* 04 */ le_uint32_t guild_card_number = 0; + /* 08 */ le_uint32_t sub_version = 0; + /* 0C */ uint8_t language = 0; + /* 0D */ int8_t character_slot = 0; // Values for connection_phase: // 00 - initial connection (client will request system file, characters, etc.) // 01 - choose character @@ -1766,17 +1776,18 @@ struct C_LoginBase_BB_93 { // 04 - login server // 05 - lobby server // 06 - lobby server (with Meet User fields specified) - uint8_t connection_phase = 0; - uint8_t client_code = 0; - le_uint32_t security_token = 0; - pstring username; - pstring password; + /* 0E */ uint8_t connection_phase = 0; + /* 0F */ uint8_t client_code = 0; + /* 10 */ le_uint32_t security_token = 0; + /* 14 */ pstring username; + /* 44 */ pstring password; // These fields map to the same fields in SC_MeetUserExtensionT. There is no // equivalent of the name field from that structure on BB (though newserv // doesn't use it anyway). - le_uint32_t menu_id = 0; - le_uint32_t preferred_lobby_id = 0; + /* 74 */ le_uint32_t menu_id = 0; + /* 78 */ le_uint32_t preferred_lobby_id = 0; + /* 7C */ } __packed_ws__(C_LoginBase_BB_93, 0x7C); struct C_LoginWithoutHardwareInfo_BB_93 : C_LoginBase_BB_93 { @@ -1784,14 +1795,16 @@ struct C_LoginWithoutHardwareInfo_BB_93 : C_LoginBase_BB_93 { // config at connect time. So the first time the server gets this command, it // will be something like "Ver. 1.24.3". This format is used on older client // versions (before 1.23.8?) - parray client_config; + /* 7C */ parray client_config; + /* A4 */ } __packed_ws__(C_LoginWithoutHardwareInfo_BB_93, 0xA4); struct C_LoginWithHardwareInfo_BB_93 : C_LoginBase_BB_93 { // See the comment in the above structure. This format is used on newer client // versions. - parray hardware_info; - parray client_config; + /* 7C */ be_uint64_t hardware_id; + /* 84 */ parray client_config; + /* AC */ } __packed_ws__(C_LoginWithHardwareInfo_BB_93, 0xAC); // 94: Invalid command @@ -1975,7 +1988,7 @@ struct C_Login_DC_PC_GC_9D { /* 48 */ pstring access_key; // On XB, this is the XBL user ID /* 58 */ pstring serial_number2; // On DCv2, this is the hardware ID; on XB, this is the XBL gamertag /* 88 */ pstring access_key2; // On XB, this is the XBL user ID - /* B8 */ pstring name; + /* B8 */ pstring login_character_name; /* C8 */ } __packed_ws__(C_Login_DC_PC_GC_9D, 0xC8); @@ -2006,13 +2019,13 @@ struct C_LoginExtended_GC_9E : C_Login_GC_9E { struct C_Login_XB_9E : C_Login_DC_PC_GC_9D { /* 00C8 */ parray unknown_a4; - /* 00E8 */ XBNetworkLocation netloc; + /* 00E8 */ XBNetworkLocation xb_netloc; // By default, this array is initialized with [0x5A2773DD, 0xD82B2345, // 0x4FF904D5] on 4OEU (US TU version). - /* 0118 */ parray unknown_a1a; + /* 0118 */ parray xb_unknown_a1a; /* 0124 */ le_uint32_t xb_user_id_high = 0; /* 0128 */ le_uint32_t xb_user_id_low = 0; - /* 012C */ le_uint32_t unknown_a1b = 0; + /* 012C */ le_uint32_t xb_unknown_a1b = 0; /* 0130 */ } __packed_ws__(C_Login_XB_9E, 0x130); @@ -2045,8 +2058,7 @@ struct C_LoginExtended_BB_9E { // 9F (C->S): Client config / security data response (V3/BB) // The data is opaque to the client, as described at the top of this file. -// If newserv ever sent a 9F command (it currently does not). On BB, this -// command does not work during the data server phase. +// On BB, this command does not work during the data server phase. struct C_ClientConfig_V3_9F { parray data; @@ -3444,7 +3456,7 @@ struct SC_TeamChat_BB_07EA { struct S_TeamMemberList_BB_09EA { le_uint32_t entry_count = 0; struct Entry { - // This is displayed as "<%04d> %s" % (rank, name) + // This is displayed as "<{:04}> {}" % (rank, name) le_uint32_t rank = 0; le_uint32_t privilege_level = 0; // 0x10 or 0x20 = green, 0x30 = blue, 0x40 = red, anything else = white le_uint32_t guild_card_number = 0; @@ -4257,11 +4269,11 @@ struct G_UseItem_6x27 { // 6x28: Feed MAG (protected on V3/V4) -struct G_FeedMAG_6x28 { +struct G_FeedMag_6x28 { G_ClientIDHeader header; le_uint32_t mag_item_id = 0; le_uint32_t fed_item_id = 0; -} __packed_ws__(G_FeedMAG_6x28, 0x0C); +} __packed_ws__(G_FeedMag_6x28, 0x0C); // 6x29: Delete inventory item (via bank deposit / sale / feeding MAG) (protected on V3 but not V4) // This subcommand is also used for reducing the size of stacks - if amount is @@ -7351,9 +7363,10 @@ struct G_TournamentMatchResult_Ep3_6xB4x51 { le_uint16_t num_players_per_team = 0; le_uint16_t winner_team_id = 0; le_uint32_t meseta_amount = 0; - // This field apparently is supposed to contain a %s token (as for printf) - // which is replaced with meseta_amount. The results screen animates this text - // counting up from 0 to meseta_amount. + // This field apparently is supposed to contain a %s token (as for sprintf) + // which is replaced with meseta_amount. (Yes, %s, not %d; it seems they + // manually convert it to a string for some reason before calling sprintf.) + // The results screen animates this text counting up from 0 to meseta_amount. pstring meseta_reward_text; } __packed_ws__(G_TournamentMatchResult_Ep3_6xB4x51, 0xF4); diff --git a/src/CommonFileFormats.hh b/src/CommonFileFormats.hh index c2e96d25..963697c8 100644 --- a/src/CommonFileFormats.hh +++ b/src/CommonFileFormats.hh @@ -45,7 +45,7 @@ struct VectorXZF { } inline std::string str() const { - return phosg::string_printf("[VectorXZF x=%g z=%g]", this->x.load(), this->z.load()); + return std::format("[VectorXZF x={:g} z={:g}]", this->x, this->z); } } __packed_ws__(VectorXZF, 0x08); @@ -109,7 +109,7 @@ struct VectorXYZF { } inline std::string str() const { - return phosg::string_printf("[VectorXYZF x=%g y=%g z=%g]", this->x.load(), this->y.load(), this->z.load()); + return std::format("[VectorXYZF x={:g} y={:g} z={:g}]", this->x, this->y, this->z); } } __packed_ws__(VectorXYZF, 0x0C); @@ -143,12 +143,11 @@ struct RELFileFooterT { static constexpr bool IsBE = BE; // Relocations is a list of words (le_uint16_t on DC/PC/XB/BB, be_uint16_t on // GC) containing the number of doublewords (uint32_t) to skip for each - // relocation. The relocation pointer starts immediately after the - // checksum_size field in the header, and advances by the value of one - // relocation word (times 4) before each relocation. At each relocated - // doubleword, the address of the first byte of the code (after checksum_size) - // is added to the existing value. - // For example, if the code segment contains the following data (where R + // relocation. The relocation pointer starts at the beginning of the file + // data, and advances by the value of one relocation word (times 4) before + // each relocation. At each relocated doubleword, the address of the first + // byte of the file is added to the existing value. + // For example, if the file data contains the following data (where R // specifies doublewords to relocate): // RR RR RR RR ?? ?? ?? ?? ?? ?? ?? ?? RR RR RR RR // RR RR RR RR ?? ?? ?? ?? RR RR RR RR diff --git a/src/CommonItemSet.cc b/src/CommonItemSet.cc index 5722b036..6835d9a5 100644 --- a/src/CommonItemSet.cc +++ b/src/CommonItemSet.cc @@ -129,7 +129,7 @@ CommonItemSet::Table::Table(const phosg::JSON& json, Episode episode) static const array episodes = {Episode::EP1, Episode::EP2, Episode::EP4}; for (Episode episode : episodes) { for (auto type : enemy_types_for_rare_table_index(episode, z)) { - string name = phosg::string_printf("%s:%s", abbreviation_for_episode(episode), phosg::name_for_enum(type)); + string name = std::format("{}:{}", abbreviation_for_episode(episode), phosg::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(); @@ -163,8 +163,8 @@ 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"); + phosg::fwrite_fmt(stream, "Enemy tables:\n"); + phosg::fwrite_fmt(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)) { @@ -174,11 +174,11 @@ void CommonItemSet::Table::print(FILE* stream) const { enemies_str += phosg::name_for_enum(enemy_type); } if (drop_probs[z]) { - fprintf(stream, " %02zX %5hu %5hu %3hhu%% %02hX:%s %s\n", + phosg::fwrite_fmt(stream, " {:02X} {:5} {:5} {:3}% {:02X}:{} {}\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()); + name_for_common_item_class(item_classes[z]), enemies_str); } else { - fprintf(stream, " %02zX ----- ----- 0%% -- %s\n", z, enemies_str.c_str()); + phosg::fwrite_fmt(stream, " {:02X} ----- ----- 0% -- {}\n", z, enemies_str); } } @@ -196,8 +196,8 @@ void CommonItemSet::Table::print(FILE* stream) const { "ROD ", "WAND ", }; - fprintf(stream, "Base weapon config:\n"); - fprintf(stream, " TYPE PROB [SB AL] FLOORS\n"); + phosg::fwrite_fmt(stream, "Base weapon config:\n"); + phosg::fwrite_fmt(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) { @@ -213,17 +213,17 @@ void CommonItemSet::Table::print(FILE* stream) const { 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", + phosg::fwrite_fmt(stream, " {:02X}:{} {:3}% [{:02X} {:02X}] {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X}\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"); + phosg::fwrite_fmt(stream, "Box configuration:\n"); + phosg::fwrite_fmt(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", + phosg::fwrite_fmt(stream, " {:02X} {:5} {:5} {:3}% {:3}% {:3}% {:3}% {:3}% {:3}% {:3}%\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], @@ -234,43 +234,43 @@ void CommonItemSet::Table::print(FILE* stream) const { 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"); + phosg::fwrite_fmt(stream, "Weapon drops:\n"); + phosg::fwrite_fmt(stream, " Grinds:\n"); + phosg::fwrite_fmt(stream, " GD AR0% AR1% AR2% AR3%\n"); for (size_t z = 0; z < 9; z++) { - fprintf(stream, " +%zu %3hhd%% %3hhd%% %3hhd%% %3hhd%%\n", z, + phosg::fwrite_fmt(stream, " +{} {:3}% {:3}% {:3}% {:3}%\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"); + phosg::fwrite_fmt(stream, " Bonus value table:\n"); + phosg::fwrite_fmt(stream, " ID"); for (int8_t v = -10; v <= 100; v += 5) { - fprintf(stream, " %5hhd%%", v); + phosg::fwrite_fmt(stream, " {:5}%", v); } fputc('\n', stream); for (size_t z = 0; z < (this->has_rare_bonus_value_prob_table ? 6 : 5); z++) { - fprintf(stream, " %02zX", z); + phosg::fwrite_fmt(stream, " {:02X}", z); for (size_t x = 0; x < 0x17; x++) { - fprintf(stream, " %5hu#", this->bonus_value_prob_table[x][z]); + phosg::fwrite_fmt(stream, " {:5}#", 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"); + phosg::fwrite_fmt(stream, " Area config tables:\n"); + phosg::fwrite_fmt(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", + phosg::fwrite_fmt(stream, " {:02X} {:02X} {:02X} {:02X} {:3}% {:3}% {:3}% {:3}% {:3}% {:3}% {:02X} {:3}%\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"); + phosg::fwrite_fmt(stream, " Tool class table:\n"); + phosg::fwrite_fmt(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); + phosg::fwrite_fmt(stream, " {:02X}", 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]); + phosg::fwrite_fmt(stream, " {:5}", this->tool_class_prob_table[tool_class][area_norm]); } fputc('\n', stream); } @@ -297,42 +297,42 @@ void CommonItemSet::Table::print(FILE* stream) const { "MEGID ", }; - fprintf(stream, " Technique table:\n"); - fprintf(stream, " TECH A1 A2 A3 A4 A5 A6 A7 A8 A9 A10\n"); + phosg::fwrite_fmt(stream, " Technique table:\n"); + phosg::fwrite_fmt(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]); + phosg::fwrite_fmt(stream, " {:02X}:{}", 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); + phosg::fwrite_fmt(stream, " {:5}[{:2}-{:2}]", prob, min_level, max_level); } else { - fprintf(stream, " 0[-----]"); + phosg::fwrite_fmt(stream, " 0[-----]"); } } fputc('\n', stream); } - fprintf(stream, " Armor/shield type bias: %hhu\n", this->armor_or_shield_type_bias); + phosg::fwrite_fmt(stream, " Armor/shield type bias: {}\n", this->armor_or_shield_type_bias); - fprintf(stream, " Armor/shield type index table:\n"); - fprintf(stream, " TY PROB\n"); + phosg::fwrite_fmt(stream, " Armor/shield type index table:\n"); + phosg::fwrite_fmt(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]); + phosg::fwrite_fmt(stream, " {:02X} {:3}%\n", z, this->armor_shield_type_index_prob_table[z]); } - fprintf(stream, " Armor/shield slot count table:\n"); - fprintf(stream, " #S PROB\n"); + phosg::fwrite_fmt(stream, " Armor/shield slot count table:\n"); + phosg::fwrite_fmt(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]); + phosg::fwrite_fmt(stream, " {:02X} {:3}%\n", z, this->armor_slot_count_prob_table[z]); } - fprintf(stream, " Unit maximum stars table:\n"); - fprintf(stream, " AR #*\n"); + phosg::fwrite_fmt(stream, " Unit maximum stars table:\n"); + phosg::fwrite_fmt(stream, " AR #*\n"); for (size_t z = 0; z < 10; z++) { - fprintf(stream, " %02zX %3hhu\n", z, this->unit_max_stars_table[z]); + phosg::fwrite_fmt(stream, " {:02X} {:3}\n", z, this->unit_max_stars_table[z]); } } @@ -344,7 +344,7 @@ phosg::JSON CommonItemSet::Table::json() const { static const array episodes = {Episode::EP1, Episode::EP2, Episode::EP4}; for (Episode episode : episodes) { for (auto type : enemy_types_for_rare_table_index(episode, z)) { - string name = phosg::string_printf("%s:%s", abbreviation_for_episode(episode), phosg::name_for_enum(type)); + string name = std::format("{}:{}", abbreviation_for_episode(episode), phosg::name_for_enum(type)); enemy_meseta_ranges_json.emplace(name, to_json(this->enemy_meseta_ranges[z])); enemy_type_drop_probs_json.emplace(name, this->enemy_type_drop_probs[z]); enemy_item_classes_json.emplace(name, this->enemy_item_classes[z]); @@ -413,7 +413,7 @@ void CommonItemSet::print(FILE* stream) const { 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", + phosg::fwrite_fmt(stream, "============ {} {} {} {}\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&) { @@ -503,7 +503,7 @@ shared_ptr CommonItemSet::get_table( try { return this->tables.at(this->key_for_table(episode, mode, difficulty, secid)); } catch (const out_of_range&) { - throw runtime_error(phosg::string_printf("common item table not available for episode=%s, mode=%s, difficulty=%hu, secid=%hu", + throw runtime_error(std::format("common item table not available for episode={}, mode={}, difficulty={}, secid={}", name_for_episode(episode), name_for_mode(mode), difficulty, secid)); } } @@ -554,11 +554,11 @@ GSLV3V4CommonItemSet::GSLV3V4CommonItemSet(std::shared_ptr gs default: throw runtime_error("invalid episode"); } - return phosg::string_printf( - "ItemPT%s%s%c%1hhu.rel", + return std::format( + "ItemPT{}{}{}{}.rel", is_challenge ? "c" : "", episode_token, - tolower(abbreviation_for_difficulty(difficulty)), + static_cast(tolower(abbreviation_for_difficulty(difficulty))), section_id); }; diff --git a/src/CommonItemSet.hh b/src/CommonItemSet.hh index c6c47ad0..ba6eefbb 100644 --- a/src/CommonItemSet.hh +++ b/src/CommonItemSet.hh @@ -311,22 +311,22 @@ struct ProbabilityTable { return this->items[--this->count]; } - void shuffle(std::shared_ptr opt_rand_crypt) { + void shuffle(std::shared_ptr rand_crypt) { for (size_t z = 1; z < this->count; z++) { - size_t other_z = random_from_optional_crypt(opt_rand_crypt) % (z + 1); + size_t other_z = rand_crypt->next() % (z + 1); ItemT t = this->items[z]; this->items[z] = this->items[other_z]; this->items[other_z] = t; } } - ItemT sample(std::shared_ptr opt_rand_crypt) const { + ItemT sample(std::shared_ptr rand_crypt) const { if (this->count == 0) { throw std::runtime_error("sample from empty probability table"); } else if (this->count == 1) { return this->items[0]; } else { - return this->items[random_from_optional_crypt(opt_rand_crypt) % this->count]; + return this->items[rand_crypt->next() % this->count]; } } }; diff --git a/src/Compression.cc b/src/Compression.cc index 21bd5ed5..afc0b188 100644 --- a/src/Compression.cc +++ b/src/Compression.cc @@ -1018,7 +1018,7 @@ void prs_disassemble(FILE* stream, const void* data, size_t size) { uint8_t buffered_bits = cr.buffered_bits(); if (cr.read()) { uint8_t literal_value = r.get_u8(); - fprintf(stream, "[%zX] %hhu> 1 %02hhX literal %02hhX\n", + phosg::fwrite_fmt(stream, "[{:X}] {}> 1 {:02X} literal {:02X}\n", output_bytes, buffered_bits, literal_value, literal_value); output_bytes++; @@ -1030,19 +1030,19 @@ void prs_disassemble(FILE* stream, const void* data, size_t size) { uint16_t a = (a_high << 8) | a_low; ssize_t offset = (a >> 3) | (~0x1FFF); if (offset == ~0x1FFF) { - fprintf(stream, "[%zX] end\n", output_bytes); + phosg::fwrite_fmt(stream, "[{:X}] end\n", output_bytes); break; } if (a & 7) { count = (a & 7) + 2; read_offset = output_bytes + offset; - fprintf(stream, "[%zX] %hhu> 01 %02hhX %02hhX long copy from %zd (offset=%zX) size=%zX\n", + phosg::fwrite_fmt(stream, "[{:X}] {}> 01 {:02X} {:02X} long copy from {} (offset={:X}) size={:X}\n", output_bytes, buffered_bits, a_low, a_high, offset, read_offset, count); } else { uint8_t count_u8 = r.get_u8(); count = count_u8 + 1; read_offset = output_bytes + offset; - fprintf(stream, "[%zX] %hhu> 01 %02hhX %02hhX %02hhX extended copy from %zd (offset=%zX) size=%zX\n", + phosg::fwrite_fmt(stream, "[{:X}] {}> 01 {:02X} {:02X} {:02X} extended copy from {} (offset={:X}) size={:X}\n", output_bytes, buffered_bits, a_low, a_high, count_u8, offset, read_offset, count); } @@ -1053,7 +1053,7 @@ void prs_disassemble(FILE* stream, const void* data, size_t size) { count = ((first_bit ? 2 : 0) | (second_bit ? 1 : 0)) + 2; ssize_t offset = offset_u8 | (~0xFF); read_offset = output_bytes + offset; - fprintf(stream, "[%zX] %hhu> 00%c%c %02hhX short copy from %zd (offset=%zX) size=%zX\n", + phosg::fwrite_fmt(stream, "[{:X}] {}> 00{}{} {:02X} short copy from {} (offset={:X}) size={:X}\n", output_bytes, buffered_bits, first_bit ? '1' : '0', second_bit ? '1' : '0', offset_u8, offset, read_offset, count); } @@ -1346,11 +1346,11 @@ void bc0_disassemble(FILE* stream, const void* data, size_t size) { uint8_t a2 = r.get_u8(); size_t count = (a2 & 0x0F) + 3; // size_t backreference_offset = a1 | ((a2 << 4) & 0xF00); - fprintf(stream, "[%zX] backreference %02zX\n", output_bytes, count); + phosg::fwrite_fmt(stream, "[{:X}] backreference {:02X}\n", output_bytes, count); output_bytes += count; } else { - fprintf(stream, "[%zX] literal %02hhX\n", output_bytes, r.get_u8()); + phosg::fwrite_fmt(stream, "[{:X}] literal {:02X}\n", output_bytes, r.get_u8()); output_bytes++; } } diff --git a/src/DCSerialNumbers.cc b/src/DCSerialNumbers.cc index e8e7ed7f..13a5a4ce 100644 --- a/src/DCSerialNumbers.cc +++ b/src/DCSerialNumbers.cc @@ -1304,7 +1304,7 @@ string generate_dc_serial_number(uint8_t domain, uint8_t subdomain) { size_t index2 = phosg::random_object() % num_primes2; size_t index3 = phosg::random_object() % num_primes3; uint32_t value = primes1[index1] * primes2[index2] * primes3[index3]; - string s = phosg::string_printf("%08X", value); + string s = std::format("{:08X}", value); string ret; for (char ch : s) { @@ -1336,7 +1336,7 @@ unordered_map generate_all_dc_serial_numbers(uint8_t domain, u while ((serial_number = iter.next()) != 0) { ret[serial_number].push_back(((iter.domain << 2) & 3) | (iter.subdomain & 3)); if (iter.index3 == 0) { - fprintf(stderr, "... (it) domain=%hhu subdomain=%hhu index2=%hu results=%zu (0x%zX)\n", iter.domain, iter.subdomain, iter.index2, ret.size(), ret.size()); + phosg::fwrite_fmt(stderr, "... (it) domain={} subdomain={} index2={} results={} (0x{:X})\n", iter.domain, iter.subdomain, iter.index2, ret.size(), ret.size()); } } return ret; @@ -1389,14 +1389,14 @@ size_t DCSerialNumberIterator::progress() const { void dc_serial_number_speed_test(uint64_t seed) { uint32_t effective_seed = (seed & 0xFFFFFFFF00000000) ? phosg::random_object() : seed; - fprintf(stderr, "Product speed test with seed=%08" PRIX32 "\n", effective_seed); + phosg::fwrite_fmt(stderr, "Product speed test with seed={:08X}\n", effective_seed); PSOV2Encryption crypt(effective_seed); uint64_t time_slow = 0; uint64_t time_fast = 0; size_t num_disagreements = 0; static constexpr size_t count = 0x1000; for (size_t z = 0; z < count; z++) { - string s = phosg::string_printf("%08X", crypt.next()); + string s = std::format("{:08X}", crypt.next()); uint64_t start = phosg::now(); bool is_valid_fast = dc_serial_number_is_valid_fast(s, 1, 0xFF); @@ -1407,17 +1407,17 @@ void dc_serial_number_speed_test(uint64_t seed) { time_slow += phosg::now() - start; if (((z & 0xF) == 0) || is_valid_slow || is_valid_fast) { - fprintf(stderr, "... %02zX: %s => %s %s%s\n", z, s.c_str(), is_valid_slow ? "SLOW" : "----", is_valid_fast ? "FAST" : "----", is_valid_slow != is_valid_fast ? " !!!" : ""); + phosg::fwrite_fmt(stderr, "... {:02X}: {} => {} {}{}\n", z, s, is_valid_slow ? "SLOW" : "----", is_valid_fast ? "FAST" : "----", is_valid_slow != is_valid_fast ? " !!!" : ""); } if (is_valid_fast != is_valid_slow) { num_disagreements++; } } - fprintf(stderr, "Total time (slow): %" PRId64 " usecs (%" PRIu64 " per serial number)\n", time_slow, time_slow / count); - fprintf(stderr, "Total time (fast): %" PRId64 " usecs (%" PRIu64 " per serial number)\n", time_fast, time_fast / count); - fprintf(stderr, "Fast vs. slow speedup: %zux\n", static_cast(time_slow / time_fast)); - fprintf(stderr, "Disagreements: %zu\n", num_disagreements); + phosg::fwrite_fmt(stderr, "Total time (slow): {} usecs ({} per serial number)\n", time_slow, time_slow / count); + phosg::fwrite_fmt(stderr, "Total time (fast): {} usecs ({} per serial number)\n", time_fast, time_fast / count); + phosg::fwrite_fmt(stderr, "Fast vs. slow speedup: {}x\n", static_cast(time_slow / time_fast)); + phosg::fwrite_fmt(stderr, "Disagreements: {}\n", num_disagreements); } string decrypt_dp_address_jpn( @@ -1480,7 +1480,7 @@ std::string crypt_dp_address_jpn_simple(const std::string& data, int64_t mask_ke if (mask_key < 0) { throw runtime_error("cannot determine mask key"); } - phosg::log_info("Determined %08" PRIX64 " to be the most likely mask key", mask_key); + phosg::log_info_f("Determined {:08X} to be the most likely mask key", mask_key); r.go(0); } diff --git a/src/DNSServer.cc b/src/DNSServer.cc index eb4c8290..97090bfa 100644 --- a/src/DNSServer.cc +++ b/src/DNSServer.cc @@ -1,7 +1,5 @@ #include "DNSServer.hh" -#include -#include #include #include #include @@ -14,48 +12,23 @@ #include "Loggers.hh" #include "NetworkAddresses.hh" +#include "ServerState.hh" using namespace std; -DNSServer::DNSServer( - shared_ptr base, - uint32_t local_connect_address, - uint32_t external_connect_address, - shared_ptr banned_ipv4_ranges) - : base(base), - local_connect_address(local_connect_address), - external_connect_address(external_connect_address), - banned_ipv4_ranges(banned_ipv4_ranges) {} - -DNSServer::~DNSServer() { - for (const auto& it : this->fd_to_receive_event) { - close(it.first); - } -} - -void DNSServer::listen(const std::string& socket_path) { - this->add_socket(phosg::listen(socket_path, 0, 0)); -} +DNSServer::DNSServer(shared_ptr state) + : state(state) {} void DNSServer::listen(const std::string& addr, int port) { - this->add_socket(phosg::listen(addr, port, 0)); -} + if (port == 0) { + throw std::runtime_error("Listening port cannot be zero"); + } + asio::ip::address asio_addr = addr.empty() ? asio::ip::address_v4::any() : asio::ip::make_address(addr); + asio::ip::udp::endpoint endpoint(asio_addr, port); + auto sock = make_shared(*this->state->io_context, endpoint); + this->sockets.emplace(sock); -void DNSServer::listen(int port) { - this->add_socket(phosg::listen("", port, 0)); -} - -void DNSServer::add_socket(int fd) { - unique_ptr e( - event_new(this->base.get(), fd, EV_READ | EV_PERSIST, &DNSServer::dispatch_on_receive_message, this), - event_free); - event_add(e.get(), nullptr); - this->fd_to_receive_event.emplace(fd, std::move(e)); -} - -void DNSServer::dispatch_on_receive_message(evutil_socket_t fd, - short events, void* ctx) { - reinterpret_cast(ctx)->on_receive_message(fd, events); + asio::co_spawn(*this->state->io_context, this->dns_server_task(sock), asio::detached); } string DNSServer::response_for_query(const void* vdata, size_t size, uint32_t resolved_address) { @@ -77,45 +50,27 @@ string DNSServer::response_for_query(const void* vdata, size_t size, uint32_t re return response; } -string DNSServer::response_for_query( - const string& query, uint32_t resolved_address) { +string DNSServer::response_for_query(const string& query, uint32_t resolved_address) { return DNSServer::response_for_query(query.data(), query.size(), resolved_address); } -void DNSServer::on_receive_message(int fd, short) { +asio::awaitable DNSServer::dns_server_task(std::shared_ptr sock) { for (;;) { - struct sockaddr_storage remote; - socklen_t remote_size = sizeof(sockaddr_in); - memset(&remote, 0, remote_size); - string input(2048, 0); - ssize_t bytes = recvfrom(fd, const_cast(input.data()), input.size(), - 0, reinterpret_cast(&remote), &remote_size); + asio::ip::udp::endpoint sender_ep; + size_t bytes = co_await sock->async_receive_from(asio::buffer(input), sender_ep, asio::use_awaitable); + uint32_t sender_addr = ipv4_addr_for_asio_addr(sender_ep.address()); - if (bytes < 0) { - if (errno != EAGAIN) { - dns_server_log.error("input error %d", errno); - throw runtime_error("cannot read from udp socket"); - } - break; - - } else if (bytes == 0) { - break; - - } else if (bytes < 0x0C) { - dns_server_log.warning("input query too small"); + if (bytes < 0x0C) { + dns_server_log.warning_f("input query too small"); phosg::print_data(stderr, input.data(), bytes); - - } else if (!this->banned_ipv4_ranges->check(remote)) { + } else if (!this->state->banned_ipv4_ranges->check(sender_addr)) { input.resize(bytes); - const sockaddr_in* remote_sin = reinterpret_cast(&remote); - uint32_t remote_address = ntohl(remote_sin->sin_addr.s_addr); - uint32_t connect_address = is_local_address(remote_address) - ? this->local_connect_address - : this->external_connect_address; + uint32_t connect_address = is_local_address(sender_addr) + ? this->state->local_address + : this->state->external_address; string response = this->response_for_query(input, connect_address); - sendto(fd, response.data(), response.size(), 0, - reinterpret_cast(&remote), remote_size); + co_await sock->async_send_to(asio::buffer(response.data(), response.size()), sender_ep, asio::use_awaitable); } } } diff --git a/src/DNSServer.hh b/src/DNSServer.hh index 8563a3ec..5ffcdecb 100644 --- a/src/DNSServer.hh +++ b/src/DNSServer.hh @@ -1,7 +1,6 @@ #pragma once -#include - +#include #include #include #include @@ -9,36 +8,25 @@ #include "IPV4RangeSet.hh" +struct ServerState; + class DNSServer { public: - DNSServer( - std::shared_ptr base, - uint32_t local_connect_address, - uint32_t external_connect_address, - std::shared_ptr banned_ipv4_ranges); + explicit DNSServer(std::shared_ptr state); DNSServer(const DNSServer&) = delete; DNSServer(DNSServer&&) = delete; - virtual ~DNSServer(); + DNSServer& operator=(const DNSServer&) = delete; + DNSServer& operator=(DNSServer&&) = delete; + virtual ~DNSServer() = default; - inline void set_banned_ipv4_ranges(std::shared_ptr banned_ipv4_ranges) { - this->banned_ipv4_ranges = banned_ipv4_ranges; - } - - void listen(const std::string& socket_path); void listen(const std::string& addr, int port); - void listen(int port); - void add_socket(int fd); static std::string response_for_query(const void* vdata, size_t size, uint32_t resolved_address); static std::string response_for_query(const std::string& query, uint32_t resolved_address); private: - std::shared_ptr base; - std::unordered_map> fd_to_receive_event; - uint32_t local_connect_address; - uint32_t external_connect_address; - std::shared_ptr banned_ipv4_ranges; + std::shared_ptr state; + std::unordered_set> sockets; - static void dispatch_on_receive_message(evutil_socket_t fd, short events, void* ctx); - void on_receive_message(int fd, short event); + asio::awaitable dns_server_task(std::shared_ptr sock); }; diff --git a/src/DownloadSession.cc b/src/DownloadSession.cc index c231780d..95e1622e 100644 --- a/src/DownloadSession.cc +++ b/src/DownloadSession.cc @@ -1,13 +1,7 @@ #include "DownloadSession.hh" -#include #include #include -#include -#include -#include -#include -#include #include #include #include @@ -24,7 +18,6 @@ #include "Loggers.hh" #include "PSOProtocol.hh" -#include "ProxyCommands.hh" #include "ReceiveCommands.hh" #include "ReceiveSubcommands.hh" #include "SendCommands.hh" @@ -42,8 +35,9 @@ static string random_name() { } DownloadSession::DownloadSession( - std::shared_ptr base, - const struct sockaddr_storage& remote, + std::shared_ptr io_context, + const std::string& remote_host, + uint16_t remote_port, const std::string& output_dir, Version version, uint8_t language, @@ -61,10 +55,15 @@ DownloadSession::DownloadSession( const std::vector& on_request_complete_commands, bool interactive, bool show_command_data) - : output_dir(output_dir), + : remote_host(remote_host), + remote_port(remote_port), + output_dir(output_dir), + version(version), + language(language), + show_command_data(show_command_data), bb_key_file(bb_key_file), - serial_number2(serial_number2), serial_number(serial_number), + serial_number2(serial_number2), access_key(access_key), username(username), password(password), @@ -75,33 +74,16 @@ DownloadSession::DownloadSession( ship_menu_selections(ship_menu_selections), on_request_complete_commands(on_request_complete_commands), interactive(interactive), - log(phosg::string_printf("[DownloadSession:%s] ", phosg::name_for_enum(version)), proxy_server_log.min_level), - base(base), - channel( - version, - language, - DownloadSession::dispatch_on_channel_input, - DownloadSession::dispatch_on_channel_error, - this, - phosg::render_sockaddr_storage(remote), - show_command_data ? phosg::TerminalFormat::FG_GREEN : phosg::TerminalFormat::END, - show_command_data ? phosg::TerminalFormat::FG_YELLOW : phosg::TerminalFormat::END), - hardware_id(generate_random_hardware_id(this->channel.version)), - guild_card_number(0), + log(std::format("[DownloadSession:{}] ", phosg::name_for_enum(version)), proxy_server_log.min_level), + io_context(io_context), + hardware_id(generate_random_hardware_id(version)), prev_cmd_data(0), - client_config(0), - sent_96(false), - should_request_category_list(true), - current_request(0), - current_game_config_index(0), - in_game(false), - bin_complete(false), - dat_complete(false) { + client_config(0) { if (this->output_dir.empty()) { this->output_dir = "."; } - switch (this->channel.version) { + switch (version) { case Version::DC_V1: case Version::DC_V2: if (this->serial_number2 == 0 || this->serial_number == 0 || this->access_key.empty()) { @@ -132,106 +114,102 @@ DownloadSession::DownloadSession( throw runtime_error("unsupported version"); } - this->character->inventory.language = this->channel.language; - - if (remote.ss_family != AF_INET) { - throw runtime_error("remote is not AF_INET"); - } - - string netloc_str = phosg::render_sockaddr_storage(remote); - this->log.info("Connecting to %s", netloc_str.c_str()); - - struct bufferevent* bev = bufferevent_socket_new( - this->base.get(), -1, BEV_OPT_CLOSE_ON_FREE | BEV_OPT_DEFER_CALLBACKS); - if (!bev) { - throw runtime_error(phosg::string_printf("failed to open socket (%d)", EVUTIL_SOCKET_ERROR())); - } - this->channel.set_bufferevent(bev, 0); - - if (bufferevent_socket_connect(this->channel.bev.get(), - reinterpret_cast(&remote), sizeof(struct sockaddr_in)) != 0) { - throw runtime_error(phosg::string_printf("failed to connect (%d)", EVUTIL_SOCKET_ERROR())); - } + this->character->inventory.language = language; } -void DownloadSession::dispatch_on_channel_input(Channel& ch, uint16_t command, uint32_t flag, std::string& data) { - auto* session = reinterpret_cast(ch.context_obj); - session->on_channel_input(command, flag, data); +asio::awaitable DownloadSession::run() { + string netloc_str = std::format("{}:{}", this->remote_host, this->remote_port); + this->log.info_f("Connecting to {}", netloc_str); + auto sock = make_unique(co_await async_connect_tcp(this->remote_host, this->remote_port)); + this->channel = SocketChannel::create( + this->io_context, + std::move(sock), + this->version, + this->language, + netloc_str, + this->show_command_data ? phosg::TerminalFormat::FG_GREEN : phosg::TerminalFormat::END, + this->show_command_data ? phosg::TerminalFormat::FG_YELLOW : phosg::TerminalFormat::END); + this->log.info_f("Server channel connected"); + + while (this->channel->connected()) { + auto msg = co_await this->channel->recv(); + co_await this->on_message(msg); + } } void DownloadSession::send_93_9D_9E(bool extended) { - if (is_v1(this->channel.version)) { + if (is_v1(this->version)) { C_LoginExtendedV1_DC_93 ret; ret.player_tag = this->guild_card_number ? 0xFFFF0000 : 0x00010000; ret.guild_card_number = this->guild_card_number; ret.hardware_id = this->hardware_id; - ret.sub_version = default_sub_version_for_version(this->channel.version); + ret.sub_version = default_sub_version_for_version(this->version); ret.is_extended = extended ? 1 : 0; - ret.language = this->channel.language; - ret.serial_number.encode(phosg::string_printf("%08" PRIX32, this->serial_number)); + ret.language = this->language; + ret.serial_number.encode(std::format("{:08X}", this->serial_number)); ret.access_key.encode(this->access_key); - ret.serial_number2.encode(phosg::string_printf("%08" PRIX32, this->serial_number2)); - ret.name.encode(this->character->disp.name.decode()); - this->channel.send(0x93, 0x01, &ret, extended ? sizeof(ret) : sizeof(C_LoginV1_DC_93)); + ret.serial_number2.encode(std::format("{:08X}", this->serial_number2)); + ret.login_character_name.encode(this->character->disp.name.decode()); + this->channel->send(0x93, 0x01, &ret, extended ? sizeof(ret) : sizeof(C_LoginV1_DC_93)); - } else if (is_v2(this->channel.version)) { + } else if (is_v2(this->version)) { C_LoginExtended_PC_9D ret; ret.player_tag = this->guild_card_number ? 0xFFFF0000 : 0x00010000; ret.guild_card_number = this->guild_card_number; ret.hardware_id = this->hardware_id; - ret.sub_version = default_sub_version_for_version(this->channel.version); + ret.sub_version = default_sub_version_for_version(this->version); ret.is_extended = extended ? 1 : 0; - ret.language = this->channel.language; - ret.serial_number.encode(phosg::string_printf("%08" PRIX32, this->serial_number)); + ret.language = this->language; + ret.serial_number.encode(std::format("{:08X}", this->serial_number)); ret.access_key.encode(this->access_key); ret.serial_number2 = ret.serial_number; ret.access_key2 = ret.access_key; - ret.name.encode(this->character->disp.name.decode()); + ret.login_character_name.encode(this->character->disp.name.decode()); size_t data_size = extended - ? ((this->channel.version == Version::PC_V2) ? sizeof(ret) : sizeof(C_LoginExtended_DC_GC_9D)) + ? ((this->version == Version::PC_V2) ? sizeof(ret) : sizeof(C_LoginExtended_DC_GC_9D)) : sizeof(C_Login_DC_PC_GC_9D); - this->channel.send(0x9D, 0x01, &ret, data_size); + this->channel->send(0x9D, 0x01, &ret, data_size); - } else if (this->channel.version == Version::GC_V3) { + } else if (this->version == Version::GC_V3) { C_LoginExtended_GC_9E ret; ret.player_tag = this->guild_card_number ? 0xFFFF0000 : 0x00010000; ret.guild_card_number = this->guild_card_number; ret.hardware_id = this->hardware_id; - ret.sub_version = default_sub_version_for_version(this->channel.version); + ret.sub_version = default_sub_version_for_version(this->version); ret.is_extended = extended ? 1 : 0; - ret.language = this->channel.language; - ret.serial_number.encode(phosg::string_printf("%08" PRIX32, this->serial_number)); + ret.language = this->language; + ret.serial_number.encode(std::format("{:08X}", this->serial_number)); ret.access_key.encode(this->access_key); ret.serial_number2 = ret.serial_number; ret.access_key2 = ret.access_key; - ret.name.encode(this->character->disp.name.decode()); + ret.login_character_name.encode(this->character->disp.name.decode()); ret.client_config = this->client_config; - this->channel.send(0x9E, 0x01, &ret, extended ? sizeof(ret) : sizeof(C_Login_GC_9E)); + this->channel->send(0x9E, 0x01, &ret, extended ? sizeof(ret) : sizeof(C_Login_GC_9E)); - } else if (this->channel.version == Version::XB_V3) { + } else if (this->version == Version::XB_V3) { C_LoginExtended_XB_9E ret; ret.player_tag = this->guild_card_number ? 0xFFFF0000 : 0x00010000; ret.guild_card_number = this->guild_card_number; ret.hardware_id = this->hardware_id; - ret.sub_version = default_sub_version_for_version(this->channel.version); + ret.sub_version = default_sub_version_for_version(this->version); ret.is_extended = extended ? 1 : 0; - ret.language = this->channel.language; + ret.language = this->language; ret.serial_number.encode(this->xb_gamertag); - ret.access_key.encode(phosg::string_printf("%016" PRIX64, this->xb_user_id)); + ret.access_key.encode(std::format("{:016X}", this->xb_user_id)); ret.serial_number2 = ret.serial_number; ret.access_key2 = ret.access_key; - ret.name.encode(this->character->disp.name.decode()); - ret.netloc.internal_ipv4_address = phosg::random_object(); - ret.netloc.external_ipv4_address = phosg::random_object(); - ret.netloc.port = 9500; - phosg::random_data(&ret.netloc.mac_address, sizeof(ret.netloc.mac_address)); - ret.netloc.sg_ip_address = phosg::random_object(); - ret.netloc.spi = phosg::random_object(); - ret.netloc.account_id = this->xb_account_id; - ret.netloc.unknown_a3.clear(0); + ret.login_character_name.encode(this->character->disp.name.decode()); + ret.xb_netloc.internal_ipv4_address = phosg::random_object(); + ret.xb_netloc.external_ipv4_address = phosg::random_object(); + ret.xb_netloc.port = 9500; + phosg::random_data(&ret.xb_netloc.mac_address, sizeof(ret.xb_netloc.mac_address)); + ret.xb_netloc.sg_ip_address = phosg::random_object(); + ret.xb_netloc.spi = phosg::random_object(); + ret.xb_netloc.account_id = this->xb_account_id; + ret.xb_netloc.unknown_a3.clear(0); ret.xb_user_id_high = this->xb_user_id >> 32; ret.xb_user_id_low = this->xb_user_id; - this->channel.send(0x9E, 0x01, &ret, extended ? sizeof(ret) : sizeof(C_Login_DC_PC_GC_9D)); + this->channel->send(0x9E, 0x01, &ret, extended ? sizeof(ret) : sizeof(C_Login_DC_PC_GC_9D)); } else { throw runtime_error("unsupported version"); @@ -241,31 +219,31 @@ void DownloadSession::send_93_9D_9E(bool extended) { void DownloadSession::send_61_98(bool is_98) { uint8_t command = is_98 ? 0x98 : 0x61; - if (is_v1(this->channel.version)) { + if (is_v1(this->version)) { C_CharacterData_DCv1_61_98 ret; ret.inventory = this->character->inventory; ret.disp = convert_player_disp_data(this->character->disp, 1, 1); - this->channel.send(command, 0x01, ret); + this->channel->send(command, 0x01, ret); - } else if (this->channel.version == Version::DC_V2) { + } else if (this->version == Version::DC_V2) { C_CharacterData_DCv2_61_98 ret; ret.inventory = this->character->inventory; ret.disp = convert_player_disp_data(this->character->disp, 1, 1); ret.records.challenge = this->character->challenge_records; ret.records.battle = this->character->battle_records; ret.choice_search_config = this->character->choice_search_config; - this->channel.send(command, 0x02, ret); + this->channel->send(command, 0x02, ret); - } else if (this->channel.version == Version::PC_V2) { + } else if (this->version == Version::PC_V2) { C_CharacterData_PC_61_98 ret; ret.inventory = this->character->inventory; ret.disp = convert_player_disp_data(this->character->disp, 1, 1); ret.records.challenge = this->character->challenge_records; ret.records.battle = this->character->battle_records; ret.choice_search_config = this->character->choice_search_config; - this->channel.send(command, 0x02, ret); + this->channel->send(command, 0x02, ret); - } else if (is_v3(this->channel.version)) { + } else if (is_v3(this->version)) { C_CharacterData_V3_61_98 ret; ret.inventory = this->character->inventory; ret.disp = convert_player_disp_data(this->character->disp, 1, 1); @@ -273,9 +251,9 @@ void DownloadSession::send_61_98(bool is_98) { ret.records.battle = this->character->battle_records; ret.choice_search_config = this->character->choice_search_config; ret.info_board.encode(this->character->info_board.decode()); - this->channel.send(command, 0x03, ret); + this->channel->send(command, 0x03, ret); - } else if (this->channel.version == Version::BB_V4) { + } else if (this->version == Version::BB_V4) { C_CharacterData_BB_61_98 ret; ret.inventory = this->character->inventory; ret.disp = this->character->disp; @@ -283,34 +261,30 @@ void DownloadSession::send_61_98(bool is_98) { ret.records.battle = this->character->battle_records; ret.choice_search_config = this->character->choice_search_config; ret.info_board.encode(this->character->info_board.decode()); - this->channel.send(command, 0x04, ret); + this->channel->send(command, 0x04, ret); } else { throw runtime_error("unsupported version"); } } -void DownloadSession::on_channel_input(uint16_t command, uint32_t flag, std::string& data) { - // TODO: Use the iovec form of print_data here instead of - // prepend_command_header (which copies the string) - string full_cmd = prepend_command_header(this->channel.version, this->channel.crypt_in.get(), command, flag, data); - - for (size_t z = 0; z < 0x28 && z < data.size(); z++) { - this->prev_cmd_data[z] = data[z]; +asio::awaitable DownloadSession::on_message(Channel::Message& msg) { + for (size_t z = 0; z < 0x28 && z < msg.data.size(); z++) { + this->prev_cmd_data[z] = msg.data[z]; } - switch (command) { + switch (msg.command) { case 0x03: { - if (this->channel.version != Version::BB_V4) { + if (this->version != Version::BB_V4) { throw runtime_error("BB server sent non-BB encryption command"); } if (!this->bb_key_file) { throw runtime_error("BB encryption requires a key file"); } - const auto& cmd = check_size_t(data, 0xFFFF); - this->channel.crypt_in = make_shared(*this->bb_key_file, &cmd.server_key[0], sizeof(cmd.server_key)); - this->channel.crypt_out = make_shared(*this->bb_key_file, &cmd.client_key[0], sizeof(cmd.client_key)); - this->log.info("Enabled BB encryption"); + const auto& cmd = msg.check_size_t(0xFFFF); + this->channel->crypt_in = make_shared(*this->bb_key_file, &cmd.server_key[0], sizeof(cmd.server_key)); + this->channel->crypt_out = make_shared(*this->bb_key_file, &cmd.client_key[0], sizeof(cmd.client_key)); + this->log.info_f("Enabled BB encryption"); throw runtime_error("not yet implemented"); // Send 93 break; } @@ -319,54 +293,54 @@ void DownloadSession::on_channel_input(uint16_t command, uint32_t flag, std::str case 0x17: case 0x91: case 0x9B: { - const auto& cmd = check_size_t(data, 0xFFFF); - if (uses_v3_encryption(this->channel.version)) { - this->channel.crypt_in = make_shared(cmd.server_key); - this->channel.crypt_out = make_shared(cmd.client_key); - this->log.info("Enabled V3 encryption (server key %08" PRIX32 ", client key %08" PRIX32 ")", - cmd.server_key.load(), cmd.client_key.load()); - } else if (!uses_v4_encryption(this->channel.version)) { - this->channel.crypt_in = make_shared(cmd.server_key); - this->channel.crypt_out = make_shared(cmd.client_key); - this->log.info("Enabled V2 encryption (server key %08" PRIX32 ", client key %08" PRIX32 ")", - cmd.server_key.load(), cmd.client_key.load()); + const auto& cmd = msg.check_size_t(0xFFFF); + if (uses_v3_encryption(this->version)) { + this->channel->crypt_in = make_shared(cmd.server_key); + this->channel->crypt_out = make_shared(cmd.client_key); + this->log.info_f("Enabled V3 encryption (server key {:08X}, client key {:08X})", + cmd.server_key, cmd.client_key); + } else if (!uses_v4_encryption(this->version)) { + this->channel->crypt_in = make_shared(cmd.server_key); + this->channel->crypt_out = make_shared(cmd.client_key); + this->log.info_f("Enabled V2 encryption (server key {:08X}, client key {:08X})", + cmd.server_key, cmd.client_key); } else { throw runtime_error("BB server sent non-BB encryption command"); } - if (command == 0x02) { - bool is_extended = (this->channel.version == Version::XB_V3); + if (msg.command == 0x02) { + bool is_extended = (this->version == Version::XB_V3); this->send_93_9D_9E(is_extended); } else { - if (is_v1(this->channel.version)) { + if (is_v1(this->version)) { C_LoginV1_DC_PC_V3_90 ret; - ret.serial_number.encode(phosg::string_printf("%08" PRIX32, this->serial_number)); + ret.serial_number.encode(std::format("{:08X}", this->serial_number)); ret.access_key.encode(this->access_key); - this->channel.send(0x90, 0x00, ret); + this->channel->send(0x90, 0x00, ret); - } else if (is_v2(this->channel.version)) { + } else if (is_v2(this->version)) { C_Login_DC_PC_V3_9A ret; - ret.serial_number.encode(phosg::string_printf("%08" PRIX32, this->serial_number)); + ret.serial_number.encode(std::format("{:08X}", this->serial_number)); ret.access_key.encode(this->access_key); ret.player_tag = this->guild_card_number ? 0xFFFF0000 : 0x00010000; ret.guild_card_number = this->guild_card_number; - ret.sub_version = default_sub_version_for_version(this->channel.version); + ret.sub_version = default_sub_version_for_version(this->version); ret.serial_number2 = ret.serial_number; ret.access_key2 = ret.access_key; - this->channel.send(0x9A, 0x00, ret); + this->channel->send(0x9A, 0x00, ret); - } else if (this->channel.version == Version::GC_V3) { + } else if (this->version == Version::GC_V3) { C_VerifyAccount_V3_DB ret; - ret.serial_number.encode(phosg::string_printf("%08" PRIX32, this->serial_number)); + ret.serial_number.encode(std::format("{:08X}", this->serial_number)); ret.access_key.encode(this->access_key); - ret.sub_version = default_sub_version_for_version(this->channel.version); + ret.sub_version = default_sub_version_for_version(this->version); ret.serial_number2 = ret.serial_number; ret.access_key2 = ret.access_key; ret.password.encode(this->password); - this->channel.send(0xDB, 0x00, ret); + this->channel->send(0xDB, 0x00, ret); - } else if (this->channel.version == Version::XB_V3) { + } else if (this->version == Version::XB_V3) { this->send_93_9D_9E(true); } else { @@ -379,36 +353,36 @@ void DownloadSession::on_channel_input(uint16_t command, uint32_t flag, std::str case 0x90: case 0x9A: { - if (flag == 1) { - if (is_v1(this->channel.version)) { + if (msg.flag == 1) { + if (is_v1(this->version)) { C_RegisterV1_DC_92 ret; ret.hardware_id = this->hardware_id; - ret.sub_version = default_sub_version_for_version(this->channel.version); - ret.language = this->channel.language; - ret.serial_number2.encode(phosg::string_printf("%08" PRIX32, this->serial_number2)); - this->channel.send(0x92, 0x00, ret); + ret.sub_version = default_sub_version_for_version(this->version); + ret.language = this->language; + ret.serial_number2.encode(std::format("{:08X}", this->serial_number2)); + this->channel->send(0x92, 0x00, ret); - } else if (!is_v4(this->channel.version)) { + } else if (!is_v4(this->version)) { C_Register_DC_PC_V3_9C ret; ret.hardware_id = this->hardware_id; - ret.sub_version = default_sub_version_for_version(this->channel.version); - ret.language = this->channel.language; - if (this->channel.version == Version::XB_V3) { + ret.sub_version = default_sub_version_for_version(this->version); + ret.language = this->language; + if (this->version == Version::XB_V3) { ret.serial_number.encode(this->xb_gamertag); - ret.access_key.encode(phosg::string_printf("%016" PRIX64, this->xb_user_id)); + ret.access_key.encode(std::format("{:016X}", this->xb_user_id)); ret.password.encode("xbox-pso"); } else { - ret.serial_number.encode(phosg::string_printf("%08" PRIX32, this->serial_number)); + ret.serial_number.encode(std::format("{:08X}", this->serial_number)); ret.access_key.encode(this->access_key); ret.password.encode(this->password); } - this->channel.send(0x9C, 0x00, ret); + this->channel->send(0x9C, 0x00, ret); } else { throw runtime_error("unsupported version"); } - } else if (flag == 0 || flag == 2) { + } else if (msg.flag == 0 || msg.flag == 2) { this->send_93_9D_9E(true); } else { @@ -419,17 +393,17 @@ void DownloadSession::on_channel_input(uint16_t command, uint32_t flag, std::str case 0x92: case 0x9C: - if (flag == 0) { + if (msg.flag == 0) { throw runtime_error("server rejected login credentials"); } this->send_93_9D_9E(true); break; case 0x9F: { - if (is_v1_or_v2(this->channel.version)) { + if (is_v1_or_v2(this->version)) { throw runtime_error("invalid command"); } - this->channel.send(0x9F, 0x00, this->client_config); + this->channel->send(0x9F, 0x00, this->client_config); break; } @@ -437,16 +411,16 @@ void DownloadSession::on_channel_input(uint16_t command, uint32_t flag, std::str C_ExecuteCodeResult_B3 ret; ret.checksum = 0; ret.return_value = 0; - this->channel.send(0xB3, 0x00, ret); + this->channel->send(0xB3, 0x00, ret); break; } case 0x04: { - const auto& cmd = check_size_t(data, 0x08, sizeof(S_UpdateClientConfig_V3_04)); - if (!is_v1_or_v2(this->channel.version)) { + const auto& cmd = msg.check_size_t(0x08, sizeof(S_UpdateClientConfig_V3_04)); + if (!is_v1_or_v2(this->version)) { for (size_t z = 0; z < 0x20; z++) { size_t read_index = z + 8; - this->client_config[z] = (read_index < data.size()) ? data[read_index] : this->prev_cmd_data[read_index]; + this->client_config[z] = (read_index < msg.data.size()) ? msg.data[read_index] : this->prev_cmd_data[read_index]; } } this->guild_card_number = cmd.guild_card_number; @@ -454,14 +428,14 @@ void DownloadSession::on_channel_input(uint16_t command, uint32_t flag, std::str C_CharSaveInfo_DCv2_PC_V3_BB_96 ret; ret.creation_timestamp = this->character->creation_timestamp; ret.event_counter = this->character->save_count; - this->channel.send(0x96, 0x00, ret); + this->channel->send(0x96, 0x00, ret); this->sent_96 = true; } break; } case 0x97: - this->channel.send(0xB1, 0x00); + this->channel->send(0xB1, 0x00); break; case 0x95: @@ -469,13 +443,13 @@ void DownloadSession::on_channel_input(uint16_t command, uint32_t flag, std::str break; case 0xB1: - this->channel.send(0x99, 0x00); + this->channel->send(0x99, 0x00); break; case 0x1A: case 0xD5: - if (is_v3(this->channel.version)) { - this->channel.send(0xD6, 0x00); + if (is_v3(this->version)) { + this->channel->send(0xD6, 0x00); } break; @@ -486,21 +460,21 @@ void DownloadSession::on_channel_input(uint16_t command, uint32_t flag, std::str C_MenuSelection_10_Flag00 ret; auto handle_command = [&]() { - const auto* items = check_size_vec_t(data, flag + 1); + const auto* items = check_size_vec_t(msg.data, msg.flag + 1); size_t item_index; - this->log.info("Ship Select menu:"); - for (item_index = 1; item_index <= flag; item_index++) { + this->log.info_f("Ship Select menu:"); + for (item_index = 1; item_index <= msg.flag; item_index++) { const auto& item = items[item_index]; auto text = strip_color(item.name.decode()); - this->log.info("%zu: (%08" PRIX32 " %08" PRIX32 ") %s", item_index, item.menu_id.load(), item.item_id.load(), text.c_str()); + this->log.info_f("{}: ({:08X} {:08X}) {}", item_index, item.menu_id, item.item_id, text); if (this->ship_menu_selections.count(text)) { break; } } - if (item_index > flag) { + if (item_index > msg.flag) { if (this->interactive) { - while (item_index == 0 || item_index > flag) { - this->log.info("Choose response index:"); + while (item_index == 0 || item_index > msg.flag) { + this->log.info_f("Choose response index:"); string input = phosg::fgets(stdin); item_index = stoul(input, nullptr, 0); } @@ -512,13 +486,13 @@ void DownloadSession::on_channel_input(uint16_t command, uint32_t flag, std::str ret.item_id = items[item_index].item_id; }; - if (uses_utf16(this->channel.version)) { + if (uses_utf16(this->version)) { handle_command.operator()(); } else { handle_command.operator()(); } - this->channel.send(0x10, 0x00, ret); + this->channel->send(0x10, 0x00, ret); break; } @@ -538,39 +512,32 @@ void DownloadSession::on_channel_input(uint16_t command, uint32_t flag, std::str break; case 0x1D: - this->channel.send(0x1D, 0x00); + this->channel->send(0x1D, 0x00); break; case 0x19: { - const auto& cmd = check_size_t(data, sizeof(S_Reconnect_19), 0xFFFF); - - sockaddr_storage ss; - auto* sin = reinterpret_cast(&ss); - sin->sin_family = AF_INET; - sin->sin_addr.s_addr = htonl(cmd.address); - sin->sin_port = htons(cmd.port); - string netloc_str = phosg::render_sockaddr_storage(ss); - this->log.info("Connecting to %s", netloc_str.c_str()); - - struct bufferevent* bev = bufferevent_socket_new(this->base.get(), -1, BEV_OPT_CLOSE_ON_FREE | BEV_OPT_DEFER_CALLBACKS); - if (!bev) { - throw runtime_error(phosg::string_printf("failed to open socket (%d)", EVUTIL_SOCKET_ERROR())); - } - this->channel.set_bufferevent(bev, 0); - this->channel.crypt_in.reset(); - this->channel.crypt_out.reset(); - - if (bufferevent_socket_connect(this->channel.bev.get(), reinterpret_cast(&ss), sizeof(struct sockaddr_in)) != 0) { - throw runtime_error(phosg::string_printf("failed to connect (%d)", EVUTIL_SOCKET_ERROR())); - } + const auto& cmd = msg.check_size_t(sizeof(S_Reconnect_19), 0xFFFF); + auto new_ep = make_endpoint_ipv4(cmd.address, cmd.port); + string netloc_str = str_for_endpoint(new_ep); + this->log.info_f("Connecting to {}", netloc_str); + auto sock = make_unique(co_await async_connect_tcp(new_ep)); + this->channel = SocketChannel::create( + this->io_context, + std::move(sock), + this->version, + this->language, + netloc_str, + this->show_command_data ? phosg::TerminalFormat::FG_GREEN : phosg::TerminalFormat::END, + this->show_command_data ? phosg::TerminalFormat::FG_YELLOW : phosg::TerminalFormat::END); + this->log.info_f("Server channel connected"); break; } case 0x83: { - const auto* items = check_size_vec_t(data, flag, true); + const auto* items = check_size_vec_t(msg.data, msg.flag, true); this->lobby_menu_items.clear(); - for (size_t z = 0; z < flag; z++) { + for (size_t z = 0; z < msg.flag; z++) { this->lobby_menu_items.emplace_back(items[z]); } break; @@ -581,7 +548,7 @@ void DownloadSession::on_channel_input(uint16_t command, uint32_t flag, std::str // be able to see that we didn't, so we don't bother const auto& game_config = this->game_configs[this->current_game_config_index]; - if (this->channel.version == Version::PC_V2) { + if (this->version == Version::PC_V2) { C_CreateGame_PC_C1 ret; ret.name.encode(random_name()); ret.password.encode(random_name()); @@ -589,16 +556,16 @@ void DownloadSession::on_channel_input(uint16_t command, uint32_t flag, std::str ret.battle_mode = (game_config.mode == GameMode::BATTLE); ret.challenge_mode = (game_config.mode == GameMode::CHALLENGE); ret.episode = 1; - this->channel.send(0xC1, 0x00, ret); + this->channel->send(0xC1, 0x00, ret); - } else if (!is_v4(this->channel.version)) { + } else if (!is_v4(this->version)) { C_CreateGame_DC_V3_0C_C1_Ep3_EC ret; ret.name.encode(random_name()); ret.password.encode(random_name()); ret.difficulty = 0; ret.battle_mode = (game_config.mode == GameMode::BATTLE); ret.challenge_mode = (game_config.mode == GameMode::CHALLENGE); - if (is_v1(this->channel.version)) { + if (is_v1(this->version)) { ret.episode = 0; } else if (game_config.episode == Episode::EP1) { ret.episode = 1; @@ -609,7 +576,7 @@ void DownloadSession::on_channel_input(uint16_t command, uint32_t flag, std::str } else { throw std::logic_error("invalid episode"); } - this->channel.send(is_v1(this->channel.version) ? 0x0C : 0xC1, 0x00, ret); + this->channel->send(is_v1(this->version) ? 0x0C : 0xC1, 0x00, ret); } else { C_CreateGame_BB_C1 ret; @@ -628,7 +595,7 @@ void DownloadSession::on_channel_input(uint16_t command, uint32_t flag, std::str throw std::logic_error("invalid episode"); } ret.solo_mode = (game_config.mode == GameMode::SOLO); - this->channel.send(is_v1(this->channel.version) ? 0x0C : 0xC1, 0x00, ret); + this->channel->send(is_v1(this->version) ? 0x0C : 0xC1, 0x00, ret); } break; } @@ -641,32 +608,32 @@ void DownloadSession::on_channel_input(uint16_t command, uint32_t flag, std::str this->character->inventory.items[z].data.id = 0x00010000 + z; } - if (!is_v1(this->channel.version)) { - this->channel.send(0x8A, 0x00); + if (!is_v1(this->version)) { + this->channel->send(0x8A, 0x00); } - this->channel.send(0x6F, 0x00); + this->channel->send(0x6F, 0x00); this->send_next_request(); break; } case 0xA2: { auto handle_command = [&]() { - const auto* items = check_size_vec_t(data, flag); - for (size_t z = 0; z < flag; z++) { + const auto* items = check_size_vec_t(msg.data, msg.flag); + for (size_t z = 0; z < msg.flag; z++) { const auto& item = items[z]; uint64_t request = (static_cast(item.menu_id) << 32) | static_cast(item.item_id); if (!this->done_requests.count(request)) { - this->log.info("Adding request %016" PRIX64, request); + this->log.info_f("Adding request {:016X}", request); this->pending_requests.emplace(request, item.name.decode()); } } }; - if (this->channel.version == Version::PC_V2) { + if (this->version == Version::PC_V2) { handle_command.operator()(); - } else if (this->channel.version == Version::XB_V3) { + } else if (this->version == Version::XB_V3) { handle_command.operator()(); - } else if (this->channel.version == Version::BB_V4) { + } else if (this->version == Version::BB_V4) { handle_command.operator()(); } else { handle_command.operator()(); @@ -677,26 +644,26 @@ void DownloadSession::on_channel_input(uint16_t command, uint32_t flag, std::str case 0x44: case 0xA6: { auto handle_command = [&]() { - const auto& cmd = check_size_t(data, 0xFFFF); + const auto& cmd = msg.check_size_t(0xFFFF); string internal_name = cmd.filename.decode(); string filtered_name; for (char ch : internal_name) { filtered_name.push_back((isalnum(ch) || (ch == '-') || (ch == '.') || (ch == '_')) ? ch : '_'); } - string local_filename = phosg::string_printf( - "%s/%016" PRIX64 "_%" PRIu64 "_%s_%c_%s", - this->output_dir.c_str(), + string local_filename = std::format( + "{}/{:016X}_{}_{}_{}_{}", + this->output_dir, this->current_request, phosg::now(), - phosg::name_for_enum(this->channel.version), - char_for_language_code(this->channel.language), - filtered_name.c_str()); + phosg::name_for_enum(this->version), + char_for_language_code(this->language), + filtered_name); this->open_files.emplace(internal_name, OpenFile{.request = this->current_request, .filename = local_filename, .total_size = cmd.file_size, .data = ""}); }; - if (is_dc(this->channel.version)) { + if (is_dc(this->version)) { handle_command.operator()(); - } else if (!is_v4(this->channel.version)) { + } else if (!is_v4(this->version)) { handle_command.operator()(); } else { handle_command.operator()(); @@ -705,22 +672,22 @@ void DownloadSession::on_channel_input(uint16_t command, uint32_t flag, std::str } case 0x13: case 0xA7: { - const auto& cmd = check_size_t(data); + const auto& cmd = msg.check_size_t(); string internal_filename = cmd.filename.decode(); - if (!is_v1_or_v2(this->channel.version)) { + if (!is_v1_or_v2(this->version)) { C_WriteFileConfirmation_V3_BB_13_A7 ret; ret.filename.encode(internal_filename); - this->channel.send(command, flag, ret); + this->channel->send(msg.command, msg.flag, ret); } - auto f_it = this->open_files.find(internal_filename.c_str()); + auto f_it = this->open_files.find(internal_filename); if (f_it == this->open_files.end()) { - this->log.warning("Received data for non-open file %s", internal_filename.c_str()); + this->log.warning_f("Received data for non-open file {}", internal_filename); break; } auto& f = this->open_files.at(cmd.filename.decode()); - size_t block_offset = flag * 0x400; + size_t block_offset = msg.flag * 0x400; size_t allowed_block_size = (block_offset < f.total_size) ? min(f.total_size - block_offset, 0x400) : 0; @@ -732,25 +699,25 @@ void DownloadSession::on_channel_input(uint16_t command, uint32_t flag, std::str memcpy(f.data.data() + block_offset, cmd.data.data(), data_size); if (cmd.data_size != 0x400) { phosg::save_file(f.filename, f.data); - this->log.info("Wrote file %s (%zu bytes)", f.filename.c_str(), f.data.size()); + this->log.info_f("Wrote file {} ({} bytes)", f.filename, f.data.size()); this->open_files.erase(internal_filename); - if (phosg::ends_with(internal_filename, ".bin")) { + if (internal_filename.ends_with(".bin")) { this->bin_complete = true; - } else if (phosg::ends_with(internal_filename, ".dat")) { + } else if (internal_filename.ends_with(".dat")) { this->dat_complete = true; } if (this->open_files.empty() && this->bin_complete && this->dat_complete) { - if (is_v1_or_v2(this->channel.version)) { + if (is_v1_or_v2(this->version)) { this->on_request_complete(); } else { - this->channel.send(0xAC, 0x00); + this->channel->send(0xAC, 0x00); } } } break; } case 0xAC: { - if (is_v1_or_v2(this->channel.version)) { + if (is_v1_or_v2(this->version)) { throw runtime_error("unsupported version"); } this->on_request_complete(); @@ -761,24 +728,24 @@ void DownloadSession::on_channel_input(uint16_t command, uint32_t flag, std::str void DownloadSession::send_next_request() { if (should_request_category_list) { - this->log.info("Requesting quest list"); - this->channel.send(0xA2, 0x00); - if (is_v4(this->channel.version)) { - this->channel.send(0xA2, 0x01); + this->log.info_f("Requesting quest list"); + this->channel->send(0xA2, 0x00); + if (is_v4(this->version)) { + this->channel->send(0xA2, 0x01); } this->should_request_category_list = false; } else if (!this->pending_requests.empty()) { if (interactive) { const auto& config = this->game_configs[this->current_game_config_index]; - this->log.info("Items available to expand (mode=%s, episode=%s):", name_for_mode(config.mode), name_for_episode(config.episode)); + this->log.info_f("Items available to expand (mode={}, episode={}):", name_for_mode(config.mode), name_for_episode(config.episode)); for (const auto& it : this->pending_requests) { - this->log.info("%016" PRIX64 ": %s", it.first, it.second.c_str()); + this->log.info_f("{:016X}: {}", it.first, it.second); } - this->log.info("Choose item to expand by ID (q to quit; s to skip to next config):"); + this->log.info_f("Choose item to expand by ID (q to quit; s to skip to next config):"); string input = phosg::fgets(stdin); if (input.empty() || (input == "q\n")) { - this->channel.disconnect(); + this->channel->disconnect(); return; } else if (input == "s\n") { this->pending_requests.clear(); @@ -794,22 +761,22 @@ void DownloadSession::send_next_request() { this->current_request = item_it->first; this->done_requests.emplace(this->current_request); this->pending_requests.erase(item_it); - this->log.info("Sending request %016" PRIX64, this->current_request); + this->log.info_f("Sending request {:016X}", this->current_request); } C_MenuSelection_10_Flag00 cmd; cmd.menu_id = (this->current_request >> 32) & 0xFFFFFFFF; cmd.item_id = this->current_request & 0xFFFFFFFF; - this->channel.send(0x10, 0x00, cmd); + this->channel->send(0x10, 0x00, cmd); } else { - this->log.info("No pending requests with current parameters"); + this->log.info_f("No pending requests with current parameters"); this->on_request_complete(); } } void DownloadSession::on_request_complete() { for (const auto& data : this->on_request_complete_commands) { - this->channel.send(data); + this->channel->send(data); } this->send_61_98(true); @@ -819,14 +786,14 @@ void DownloadSession::on_request_complete() { C_LobbySelection_84 ret84; ret84.menu_id = item.menu_id; ret84.item_id = item.item_id; - this->channel.send(0x84, 0x00, ret84); + this->channel->send(0x84, 0x00, ret84); if (this->pending_requests.empty()) { // Advance to next mode/episode combination this->current_game_config_index++; - bool v1 = is_v1(this->channel.version); - bool v2 = is_v2(this->channel.version); - bool v3 = is_v3(this->channel.version); + bool v1 = is_v1(this->version); + bool v2 = is_v2(this->version); + bool v3 = is_v3(this->version); while ((this->current_game_config_index < this->game_configs.size()) && ((v1 && !this->game_configs[this->current_game_config_index].v1) || (v2 && !this->game_configs[this->current_game_config_index].v2) || @@ -834,36 +801,16 @@ void DownloadSession::on_request_complete() { this->current_game_config_index++; } if (this->current_game_config_index >= this->game_configs.size()) { - this->log.info("All modes complete"); - this->channel.disconnect(); + this->log.info_f("All modes complete"); + this->channel->disconnect(); } else { const auto& config = this->game_configs[this->current_game_config_index]; - this->log.info("Advancing to %s mode in %s", name_for_mode(config.mode), name_for_episode(config.episode)); + this->log.info_f("Advancing to {} mode in {}", name_for_mode(config.mode), name_for_episode(config.episode)); this->should_request_category_list = true; } } } -void DownloadSession::dispatch_on_channel_error(Channel& ch, short events) { - auto* session = reinterpret_cast(ch.context_obj); - session->on_channel_error(events); -} - -void DownloadSession::on_channel_error(short events) { - if (events & BEV_EVENT_CONNECTED) { - this->log.info("Server channel connected"); - } - if (events & BEV_EVENT_ERROR) { - int err = EVUTIL_SOCKET_ERROR(); - this->log.warning("Error %d (%s) in server stream", err, evutil_socket_error_to_string(err)); - } - if (events & (BEV_EVENT_ERROR | BEV_EVENT_EOF)) { - this->log.info("Server endpoint has disconnected"); - this->channel.disconnect(); - event_base_loopexit(this->base.get(), nullptr); - } -} - const std::vector DownloadSession::game_configs({ {.mode = GameMode::NORMAL, .episode = Episode::EP1, .v1 = true, .v2 = true, .v3 = true}, {.mode = GameMode::NORMAL, .episode = Episode::EP2, .v1 = false, .v2 = false, .v3 = true}, diff --git a/src/DownloadSession.hh b/src/DownloadSession.hh index f1008122..298d5e5c 100644 --- a/src/DownloadSession.hh +++ b/src/DownloadSession.hh @@ -1,7 +1,5 @@ #pragma once -#include - #include #include #include @@ -18,8 +16,9 @@ class DownloadSession { public: DownloadSession( - std::shared_ptr base, - const struct sockaddr_storage& remote, + std::shared_ptr io_context, + const std::string& remote_host, + uint16_t remote_port, const std::string& output_dir, Version version, uint8_t language, @@ -43,12 +42,19 @@ public: DownloadSession& operator=(DownloadSession&&) = delete; virtual ~DownloadSession() = default; + asio::awaitable run(); + protected: // Config (must be set by caller) + std::string remote_host; + uint16_t remote_port; std::string output_dir; + Version version; + uint8_t language; + bool show_command_data; std::shared_ptr bb_key_file; - uint32_t serial_number2; uint32_t serial_number; + uint32_t serial_number2; std::string access_key; std::string username; std::string password; @@ -62,17 +68,17 @@ protected: // State (set during session) phosg::PrefixedLogger log; - std::shared_ptr base; - Channel channel; + std::shared_ptr io_context; + std::shared_ptr channel; uint64_t hardware_id; - uint32_t guild_card_number; + uint32_t guild_card_number = 0; parray prev_cmd_data; parray client_config; - bool sent_96; + bool sent_96 = false; std::vector lobby_menu_items; - bool should_request_category_list; - uint64_t current_request; + bool should_request_category_list = true; + uint64_t current_request = 0; std::map pending_requests; std::unordered_set done_requests; @@ -92,20 +98,14 @@ protected: bool v3; }; static const std::vector game_configs; - size_t current_game_config_index; - bool in_game; - bool bin_complete; - bool dat_complete; + size_t current_game_config_index = 0; + bool in_game = false; + bool bin_complete = false; + bool dat_complete = false; - static void dispatch_on_channel_input(Channel& ch, uint16_t command, uint32_t flag, std::string& msg); - static void dispatch_on_channel_error(Channel& ch, short events); - void on_channel_input(uint16_t command, uint32_t flag, std::string& msg); - void on_channel_error(short events); - - void send_next_request(); - void on_request_complete(); - - void assign_item_ids(uint32_t base_item_id); void send_93_9D_9E(bool extended); void send_61_98(bool is_98); + asio::awaitable on_message(Channel::Message& msg); + void send_next_request(); + void on_request_complete(); }; diff --git a/src/EnemyType.cc b/src/EnemyType.cc index 658fc14f..2037c34f 100644 --- a/src/EnemyType.cc +++ b/src/EnemyType.cc @@ -164,7 +164,7 @@ EnemyType phosg::enum_for_name(const char* name) { if (index.empty()) { for (const auto& def : type_defs) { if (!index.emplace(def.enum_name, def.type).second) { - throw logic_error(phosg::string_printf("duplicate enemy enum name: %s", def.enum_name)); + throw logic_error(std::format("duplicate enemy enum name: {}", def.enum_name)); } } } diff --git a/src/Episode3/BattleRecord.cc b/src/Episode3/BattleRecord.cc index 4112fc5d..ce77edb7 100644 --- a/src/Episode3/BattleRecord.cc +++ b/src/Episode3/BattleRecord.cc @@ -82,19 +82,19 @@ void BattleRecord::Event::serialize(phosg::StringWriter& w) const { void BattleRecord::Event::print(FILE* stream) const { string time_str = phosg::format_time(this->timestamp); - fprintf(stream, "Event @%016" PRIX64 " (%s) ", this->timestamp, time_str.c_str()); + phosg::fwrite_fmt(stream, "Event @{:016X} ({}) ", this->timestamp, time_str); switch (this->type) { case Type::PLAYER_JOIN: - fprintf(stream, "PLAYER_JOIN %02" PRIX32 "\n", this->players[0].lobby_data.client_id.load()); + phosg::fwrite_fmt(stream, "PLAYER_JOIN {:02X}\n", this->players[0].lobby_data.client_id); this->players[0].print(stream); break; case Type::PLAYER_LEAVE: - fprintf(stream, "PLAYER_LEAVE %02hhu\n", this->leaving_client_id); + phosg::fwrite_fmt(stream, "PLAYER_LEAVE {:02}\n", this->leaving_client_id); break; case Type::SET_INITIAL_PLAYERS: - fprintf(stream, "SET_INITIAL_PLAYERS"); + phosg::fwrite_fmt(stream, "SET_INITIAL_PLAYERS"); for (const auto& player : this->players) { - fprintf(stream, " %02" PRIX32, player.lobby_data.client_id.load()); + phosg::fwrite_fmt(stream, " {:02X}", player.lobby_data.client_id); } fputc('\n', stream); for (const auto& player : this->players) { @@ -102,23 +102,23 @@ void BattleRecord::Event::print(FILE* stream) const { } break; case Type::BATTLE_COMMAND: - fprintf(stream, "BATTLE_COMMAND\n"); + phosg::fwrite_fmt(stream, "BATTLE_COMMAND\n"); phosg::print_data(stream, this->data, 0, nullptr, phosg::PrintDataFlags::PRINT_ASCII | phosg::PrintDataFlags::DISABLE_COLOR | phosg::PrintDataFlags::OFFSET_16_BITS); break; case Type::GAME_COMMAND: - fprintf(stream, "GAME_COMMAND\n"); + phosg::fwrite_fmt(stream, "GAME_COMMAND\n"); phosg::print_data(stream, this->data, 0, nullptr, phosg::PrintDataFlags::PRINT_ASCII | phosg::PrintDataFlags::DISABLE_COLOR | phosg::PrintDataFlags::OFFSET_16_BITS); break; case Type::EP3_GAME_COMMAND: - fprintf(stream, "EP3_GAME_COMMAND\n"); + phosg::fwrite_fmt(stream, "EP3_GAME_COMMAND\n"); phosg::print_data(stream, this->data, 0, nullptr, phosg::PrintDataFlags::PRINT_ASCII | phosg::PrintDataFlags::DISABLE_COLOR | phosg::PrintDataFlags::OFFSET_16_BITS); break; case Type::CHAT_MESSAGE: - fprintf(stream, "CHAT_MESSAGE %08" PRIX32 "\n", this->guild_card_number); + phosg::fwrite_fmt(stream, "CHAT_MESSAGE {:08X}\n", this->guild_card_number); phosg::print_data(stream, this->data, 0, nullptr, phosg::PrintDataFlags::PRINT_ASCII | phosg::PrintDataFlags::DISABLE_COLOR | phosg::PrintDataFlags::OFFSET_16_BITS); break; case Type::SERVER_DATA_COMMAND: - fprintf(stream, "SERVER_DATA_COMMAND\n"); + phosg::fwrite_fmt(stream, "SERVER_DATA_COMMAND\n"); phosg::print_data(stream, this->data, 0, nullptr, phosg::PrintDataFlags::PRINT_ASCII | phosg::PrintDataFlags::DISABLE_COLOR | phosg::PrintDataFlags::OFFSET_16_BITS); break; default: @@ -363,26 +363,24 @@ void BattleRecord::set_battle_end_timestamp() { void BattleRecord::print(FILE* stream) const { string start_str = phosg::format_time(this->battle_start_timestamp); string end_str = phosg::format_time(this->battle_end_timestamp); - fprintf(stream, "BattleRecord %s behavior_flags=%08" PRIX32 " start=%016" PRIX64 " (%s) end=%016" PRIX64 " (%s); %zu events\n", + phosg::fwrite_fmt(stream, "BattleRecord {} behavior_flags={:08X} start={:016X} ({}) end={:016X} ({}); {} events\n", this->is_writable ? "writable" : "read-only", this->behavior_flags, this->battle_start_timestamp, - start_str.c_str(), + start_str, this->battle_end_timestamp, - end_str.c_str(), this->events.size()); + end_str, this->events.size()); for (const auto& event : this->events) { event.print(stream); } } -BattleRecordPlayer::BattleRecordPlayer( - shared_ptr rec, - shared_ptr base) - : record(rec), +BattleRecordPlayer::BattleRecordPlayer(std::shared_ptr io_context, shared_ptr rec) + : io_context(io_context), + record(rec), event_it(this->record->events.begin()), play_start_timestamp(0), - base(base), - next_command_ev(event_new(this->base.get(), -1, EV_TIMEOUT, &BattleRecordPlayer::dispatch_schedule_events, this), event_free) {} + next_command_timer(*this->io_context) {} shared_ptr BattleRecordPlayer::get_record() const { return this->record; @@ -395,40 +393,37 @@ void BattleRecordPlayer::set_lobby(shared_ptr l) { void BattleRecordPlayer::start() { if (this->play_start_timestamp == 0) { this->play_start_timestamp = phosg::now(); - this->schedule_events(); + asio::co_spawn(*this->io_context, this->play_task(), asio::detached); } } -void BattleRecordPlayer::dispatch_schedule_events(evutil_socket_t, short, void* ctx) { - reinterpret_cast(ctx)->schedule_events(); -} - -void BattleRecordPlayer::schedule_events() { - // If the lobby is destroyed, we can't replay anything - just return without - // rescheduling +asio::awaitable BattleRecordPlayer::play_task() { auto l = this->lobby.lock(); - if (!l) { - return; - } for (;;) { uint64_t relative_ts = phosg::now() - this->play_start_timestamp + this->record->battle_start_timestamp; + // If the lobby is destroyed, we can't replay anything + if (!l) { + co_return; + } + if (this->event_it == this->record->events.end()) { if (relative_ts >= this->record->battle_end_timestamp) { // If the record is complete and the end timestamp has been reached, // send exit commands to all players in the lobby, and don't reschedule - // the event (it will be deleted along with the Player when the lobby is - // destroyed, when the last client leaves) + // the event (it will be deleted along with the Player when the lobby + // is destroyed, when the last client leaves) send_command(l, 0xED, 0x00); + break; } else { - // There are no more events to play, but the battle has not officially - // ended yet - reschedule the event for the end time - auto tv = phosg::usecs_to_timeval(this->record->battle_end_timestamp - relative_ts); - event_add(this->next_command_ev.get(), &tv); + // There are no more events to play, but the battle has not actually + // ended yet; wait until the end time + this->next_command_timer.expires_after(std::chrono::microseconds(this->record->battle_end_timestamp - relative_ts)); + co_await this->next_command_timer.async_wait(asio::use_awaitable); + l = this->lobby.lock(); } - break; } else { if (this->event_it->timestamp <= relative_ts) { @@ -464,11 +459,10 @@ void BattleRecordPlayer::schedule_events() { this->event_it++; } else { - // The next event should not occur yet, so reschedule for the time when - // it should occur - auto tv = phosg::usecs_to_timeval(this->event_it->timestamp - relative_ts); - event_add(this->next_command_ev.get(), &tv); - break; + // The next event should not occur yet, so wait until its time + this->next_command_timer.expires_after(std::chrono::microseconds(this->event_it->timestamp - relative_ts)); + co_await this->next_command_timer.async_wait(asio::use_awaitable); + l = this->lobby.lock(); } } } diff --git a/src/Episode3/BattleRecord.hh b/src/Episode3/BattleRecord.hh index d1e1f554..1296c7ba 100644 --- a/src/Episode3/BattleRecord.hh +++ b/src/Episode3/BattleRecord.hh @@ -1,8 +1,8 @@ #pragma once -#include #include +#include #include #include #include @@ -108,7 +108,7 @@ private: class BattleRecordPlayer { public: - BattleRecordPlayer(std::shared_ptr rec, std::shared_ptr base); + BattleRecordPlayer(std::shared_ptr io_context, std::shared_ptr rec); ~BattleRecordPlayer() = default; std::shared_ptr get_record() const; @@ -117,16 +117,14 @@ public: void start(); private: - static void dispatch_schedule_events(evutil_socket_t, short, void* ctx); - void schedule_events(); - + std::shared_ptr io_context; std::shared_ptr record; std::deque::const_iterator event_it; uint64_t play_start_timestamp; - std::shared_ptr base; std::weak_ptr lobby; - std::shared_ptr next_command_ev; - phosg::StringReader random_r; + asio::steady_timer next_command_timer; + + asio::awaitable play_task(); }; } // namespace Episode3 diff --git a/src/Episode3/Card.cc b/src/Episode3/Card.cc index fdfdebbd..01bb49a4 100644 --- a/src/Episode3/Card.cc +++ b/src/Episode3/Card.cc @@ -123,7 +123,7 @@ ssize_t Card::apply_abnormal_condition( int8_t dice_roll_value, int8_t random_percent) { auto s = this->server(); - auto log = s->log_stack(phosg::string_printf("apply_abnormal_condition(%02hhX, @%04X, @%04X, %hd, %hhd, %hhd): ", def_effect_index, target_card_ref, sc_card_ref, value, dice_roll_value, random_percent)); + auto log = s->log_stack(std::format("apply_abnormal_condition({:02X}, @{:04X}, @{:04X}, {}, {}, {}): ", def_effect_index, target_card_ref, sc_card_ref, value, dice_roll_value, random_percent)); bool is_nte = s->options.is_nte(); ssize_t existing_cond_index; @@ -150,13 +150,13 @@ ssize_t Card::apply_abnormal_condition( break; } } - log.debug("existing_cond_index < 0 (new condition) => cond_index = %zd", cond_index); + log.debug_f("existing_cond_index < 0 (new condition) => cond_index = {}", cond_index); } else { - log.debug("existing_cond_index = %zd (existing condition)", existing_cond_index); + log.debug_f("existing_cond_index = {} (existing condition)", existing_cond_index); } if (cond_index < 0) { - log.debug("no space for condition"); + log.debug_f("no space for condition"); return -1; } @@ -164,7 +164,7 @@ ssize_t Card::apply_abnormal_condition( auto& cond = this->action_chain.conditions[cond_index]; if ((eff.type == ConditionType::MV_BONUS) && (cond.type == ConditionType::MV_BONUS)) { existing_cond_value = clamp(cond.value, -99, 99); - log.debug("MV_BONUS combines => existing_cond_value = %hd", existing_cond_value); + log.debug_f("MV_BONUS combines => existing_cond_value = {}", existing_cond_value); } s->card_special->apply_stat_deltas_to_card_from_condition_and_clear_cond(cond, this->shared_from_this()); @@ -205,7 +205,7 @@ ssize_t Card::apply_abnormal_condition( } string cond_str = cond.str(s); - log.debug("wrote condition %zd => %s", cond_index, cond_str.c_str()); + log.debug_f("wrote condition {} => {}", cond_index, cond_str); if (!is_nte) { s->card_special->update_condition_orders(this->shared_from_this()); @@ -214,7 +214,7 @@ ssize_t Card::apply_abnormal_condition( continue; } string cond_str = cond.str(s); - log.debug("sorted conditions: [%zu] => %s", z, cond_str.c_str()); + log.debug_f("sorted conditions: [{}] => {}", z, cond_str); } } @@ -298,13 +298,13 @@ void Card::commit_attack( size_t strike_number, int16_t* out_effective_damage) { auto s = this->server(); - auto log = s->log_stack(phosg::string_printf("commit_attack(@%04hX #%04hX, @%04hX #%04hX => %hd (str%zu)): ", this->get_card_ref(), this->get_card_id(), attacker_card->get_card_ref(), attacker_card->get_card_id(), damage, strike_number)); + auto log = s->log_stack(std::format("commit_attack(@{:04X} #{:04X}, @{:04X} #{:04X} => {} (str{})): ", this->get_card_ref(), this->get_card_id(), attacker_card->get_card_ref(), attacker_card->get_card_id(), damage, strike_number)); bool is_nte = s->options.is_nte(); int16_t effective_damage = damage; s->card_special->adjust_attack_damage_due_to_conditions( this->shared_from_this(), &effective_damage, attacker_card->get_card_ref()); - log.debug("adjusted damage = %hd", effective_damage); + log.debug_f("adjusted damage = {}", effective_damage); size_t num_assists = s->assist_server->compute_num_assist_effects_for_client(this->client_id); for (size_t z = 0; z < num_assists; z++) { @@ -324,36 +324,36 @@ void Card::commit_attack( } } } - log.debug("after assists = %hd", effective_damage); + log.debug_f("after assists = {}", effective_damage); if (this->action_metadata.check_flag(0x10)) { effective_damage = 0; - log.debug("flag 0x10 => effective damage = %hd", effective_damage); + log.debug_f("flag 0x10 => effective damage = {}", effective_damage); } auto attacker_ps = attacker_card->player_state(); attacker_ps->stats.damage_given += effective_damage; this->player_state()->stats.damage_taken += effective_damage; - log.debug("updated stats"); + log.debug_f("updated stats"); this->current_hp = clamp(this->current_hp - effective_damage, 0, this->max_hp); - log.debug("hp set to %hd", this->current_hp); + log.debug_f("hp set to {}", this->current_hp); if ((effective_damage > 0) && (attacker_ps->stats.max_attack_damage < effective_damage)) { attacker_ps->stats.max_attack_damage = effective_damage; - log.debug("attacker new max damage %hd", effective_damage); + log.debug_f("attacker new max damage {}", effective_damage); } this->last_attack_final_damage = effective_damage; - log.debug("last attack final damage = %hd", effective_damage); + log.debug_f("last attack final damage = {}", effective_damage); if (effective_damage > 0) { this->card_flags = this->card_flags | 4; - log.debug("set flag 4"); + log.debug_f("set flag 4"); } if (this->current_hp < 1) { this->destroy_set_card(attacker_card); - log.debug("card destroyed"); + log.debug_f("card destroyed"); } G_ApplyConditionEffect_Ep3_6xB4x06 cmd_to_send; @@ -507,19 +507,19 @@ void Card::execute_attack(shared_ptr attacker_card) { } auto s = this->server(); - auto log = s->log_stack(phosg::string_printf("execute_attack(@%04X #%04X, @%04X #%04X): ", this->get_card_ref(), this->get_card_id(), attacker_card->get_card_ref(), attacker_card->get_card_id())); + auto log = s->log_stack(std::format("execute_attack(@{:04X} #{:04X}, @{:04X} #{:04X}): ", this->get_card_ref(), this->get_card_id(), attacker_card->get_card_ref(), attacker_card->get_card_id())); bool is_nte = s->options.is_nte(); this->card_flags &= 0xFFFFFFF3; int16_t attack_ap = this->action_metadata.attack_bonus; int16_t attack_tp = 0; int16_t defense_power = is_nte ? 0 : this->compute_defense_power_for_attacker_card(attacker_card); - log.debug("ap=%hd, tp=%hd", attack_ap, attack_tp); + log.debug_f("ap={}, tp={}", attack_ap, attack_tp); if (!is_nte && (attack_ap == 0) && !this->action_metadata.check_flag(0x20)) { - log.debug("ap == 0 and flag 0x20 not set"); + log.debug_f("ap == 0 and flag 0x20 not set"); return; } else { - log.debug("ap != 0 or flag 0x20 set; continuing..."); + log.debug_f("ap != 0 or flag 0x20 set; continuing..."); } G_ApplyConditionEffect_Ep3_6xB4x06 cmd; @@ -542,7 +542,7 @@ void Card::execute_attack(shared_ptr attacker_card) { if (is_nte) { defense_power = this->compute_defense_power_for_attacker_card(attacker_card); - log.debug("ap=%hd, tp=%hd, defense=%hd", attack_ap, attack_tp, defense_power); + log.debug_f("ap={}, tp={}, defense={}", attack_ap, attack_tp, defense_power); attacker_card->compute_action_chain_results(true, false); attack_ap = attacker_card->action_chain.chain.damage; if (this->action_chain.chain.attack_medium == AttackMedium::TECH) { @@ -553,14 +553,14 @@ void Card::execute_attack(shared_ptr attacker_card) { } s->card_special->compute_attack_ap(this->shared_from_this(), &attack_ap, attacker_card->get_card_ref()); - log.debug("computed ap %hd", attack_ap); + log.debug_f("computed ap {}", attack_ap); this->apply_ap_and_tp_adjust_assists_to_attack(attacker_card, &attack_ap, &defense_power, &attack_tp); - log.debug("assist adjusts ap=%hd, defense=%hd", attack_ap, defense_power); + log.debug_f("assist adjusts ap={}, defense={}", attack_ap, defense_power); int16_t raw_damage = attack_ap - defense_power; int16_t preliminary_damage = max(raw_damage, 0) - attack_tp; this->last_attack_preliminary_damage = preliminary_damage; - log.debug("raw_damage=%hd, preliminary_damange=%hd", raw_damage, preliminary_damage); + log.debug_f("raw_damage={}, preliminary_damange={}", raw_damage, preliminary_damage); uint32_t unknown_a9 = 0; auto target = s->card_special->compute_replaced_target_based_on_conditions( @@ -568,19 +568,19 @@ void Card::execute_attack(shared_ptr attacker_card) { if (!target) { target = this->shared_from_this(); - log.debug("target is not replaced"); + log.debug_f("target is not replaced"); } else { - log.debug("target replaced with @%04hX #%04hX", target->get_card_ref(), target->get_card_id()); + log.debug_f("target replaced with @{:04X} #{:04X}", target->get_card_ref(), target->get_card_id()); } if (!is_nte) { if (unknown_a9 != 0) { preliminary_damage = 0; - log.debug("a9 nonzero; preliminary_damage = 0"); + log.debug_f("a9 nonzero; preliminary_damage = 0"); } if (!(this->card_flags & 2) && (!attacker_card || !(attacker_card->card_flags & 2))) { s->card_special->check_for_defense_interference(attacker_card, this->shared_from_this(), &preliminary_damage); - log.debug("checked for defense interference"); + log.debug_f("checked for defense interference"); } } @@ -592,19 +592,19 @@ void Card::execute_attack(shared_ptr attacker_card) { ps->stats.num_attacks_taken++; if (!(target->card_flags & 2)) { - log.debug("flag 2 not set"); + log.debug_f("flag 2 not set"); for (size_t strike_num = 0; strike_num < attacker_card->action_chain.chain.strike_count; strike_num++) { int16_t final_effective_damage = 0; target->commit_attack(preliminary_damage, attacker_card, &cmd, strike_num, &final_effective_damage); ps->stats.action_card_negated_damage += max(0, this->current_defense_power - final_effective_damage); } } else { - log.debug("flag 2 set; committing zero-damage attack"); + log.debug_f("flag 2 set; committing zero-damage attack"); target->commit_attack(0, attacker_card, &cmd, 0, nullptr); } if (!is_nte && (this != target.get())) { - log.debug("target was replaced; committing zero-damage attack on original card"); + log.debug_f("target was replaced; committing zero-damage attack on original card"); this->commit_attack(0, attacker_card, &cmd, 0, nullptr); } @@ -906,7 +906,7 @@ void Card::clear_action_chain_and_metadata_and_most_flags() { void Card::compute_action_chain_results(bool apply_action_conditions, bool ignore_this_card_ap_tp) { auto s = this->server(); - auto log = s->log_stack(phosg::string_printf("compute_action_chain_results(@%04hX #%04hX): ", this->get_card_ref(), this->get_card_id())); + auto log = s->log_stack(std::format("compute_action_chain_results(@{:04X} #{:04X}): ", this->get_card_ref(), this->get_card_id())); bool is_nte = s->options.is_nte(); this->action_chain.compute_attack_medium(s); @@ -914,7 +914,7 @@ void Card::compute_action_chain_results(bool apply_action_conditions, bool ignor this->action_chain.chain.ap_effect_bonus = 0; this->action_chain.chain.tp_effect_bonus = 0; - log.debug("(initial) medium=%s, strike_count=%hhu, ap_effect_bonus=%hhd, tp_effect_bonus=%hhd", + log.debug_f("(initial) medium={}, strike_count={}, ap_effect_bonus={}, tp_effect_bonus={}", phosg::name_for_enum(this->action_chain.chain.attack_medium), this->action_chain.chain.strike_count, this->action_chain.chain.ap_effect_bonus, @@ -929,9 +929,9 @@ void Card::compute_action_chain_results(bool apply_action_conditions, bool ignor stat_swap_type = StatSwapType::NONE; } else { stat_swap_type = s->card_special->compute_stat_swap_type(this->shared_from_this()); - log.debug("stat_swap_type = %zu (0=none, 1=a/t, 2=a/h)", static_cast(stat_swap_type)); + log.debug_f("stat_swap_type = {} (0=none, 1=a/t, 2=a/h)", static_cast(stat_swap_type)); s->card_special->get_effective_ap_tp(stat_swap_type, &effective_ap, &effective_tp, this->get_current_hp(), this->ap, this->tp); - log.debug("effective_ap = %hd, effective_tp = %hd", effective_ap, effective_tp); + log.debug_f("effective_ap = {}, effective_tp = {}", effective_ap, effective_tp); } // This option doesn't exist in NTE @@ -942,7 +942,7 @@ void Card::compute_action_chain_results(bool apply_action_conditions, bool ignor if (ce) { effective_ap += ce->def.ap.stat; effective_tp += ce->def.tp.stat; - log.debug("(action card @%04hX) updated effective_ap = %hd, effective_tp = %hd", this->action_chain.chain.attack_action_card_refs[z].load(), effective_ap, effective_tp); + log.debug_f("(action card @{:04X}) updated effective_ap = {}, effective_tp = {}", this->action_chain.chain.attack_action_card_refs[z], effective_ap, effective_tp); } } @@ -957,7 +957,7 @@ void Card::compute_action_chain_results(bool apply_action_conditions, bool ignor stat_swap_type, &card_ap, &card_tp, card->get_current_hp(), card->ap, card->tp); effective_ap += card_ap; effective_tp += card_tp; - log.debug("(mag card set_index %zu @%04hX) updated effective_ap = %hd, effective_tp = %hd", + log.debug_f("(mag card set_index {} @{:04X}) updated effective_ap = {}, effective_tp = {}", set_index, card->get_card_ref(), effective_ap, effective_tp); } } @@ -968,25 +968,25 @@ void Card::compute_action_chain_results(bool apply_action_conditions, bool ignor sc_card->compute_action_chain_results(apply_action_conditions, true); effective_ap += sc_card->action_chain.chain.effective_ap + sc_card->action_chain.chain.ap_effect_bonus; effective_tp += sc_card->action_chain.chain.effective_tp + sc_card->action_chain.chain.tp_effect_bonus; - log.debug("(item is attacking; adding SC stats) updated effective_ap = %hd, effective_tp = %hd", + log.debug_f("(item is attacking; adding SC stats) updated effective_ap = {}, effective_tp = {}", effective_ap, effective_tp); } if (!this->action_chain.check_flag(0x10)) { this->action_chain.chain.effective_ap = is_nte ? effective_ap : min(effective_ap, 99); - log.debug("set chain effective_ap = %hd", this->action_chain.chain.effective_ap); + log.debug_f("set chain effective_ap = {}", this->action_chain.chain.effective_ap); } if (!this->action_chain.check_flag(0x20)) { this->action_chain.chain.effective_tp = is_nte ? effective_tp : min(effective_tp, 99); - log.debug("set chain effective_tp = %hd", this->action_chain.chain.effective_tp); + log.debug_f("set chain effective_tp = {}", this->action_chain.chain.effective_tp); } if (apply_action_conditions) { auto this_sh = this->shared_from_this(); s->card_special->apply_action_conditions(EffectWhen::BEFORE_ANY_CARD_ATTACK, this_sh, this_sh, 1, nullptr); - log.debug("applied action conditions (1)"); + log.debug_f("applied action conditions (1)"); } else { - log.debug("skipped applying action conditions (1)"); + log.debug_f("skipped applying action conditions (1)"); } size_t num_assists = s->assist_server->compute_num_assist_effects_for_client(this->client_id); @@ -1106,29 +1106,29 @@ void Card::compute_action_chain_results(bool apply_action_conditions, bool ignor int16_t damage = 0; if (this->action_chain.chain.attack_medium == AttackMedium::TECH) { damage = this->action_chain.chain.effective_tp + this->action_chain.chain.tp_effect_bonus; - log.debug("(tech) damage = %hhd (eff) + %hhd (bonus) = %hd", this->action_chain.chain.effective_tp, this->action_chain.chain.tp_effect_bonus, damage); + log.debug_f("(tech) damage = {} (eff) + {} (bonus) = {}", this->action_chain.chain.effective_tp, this->action_chain.chain.tp_effect_bonus, damage); } else if (this->action_chain.chain.attack_medium == AttackMedium::PHYSICAL) { damage = this->action_chain.chain.effective_ap + this->action_chain.chain.ap_effect_bonus; - log.debug("(physical) damage = %hhd (eff) + %hhd (bonus) = %hd", this->action_chain.chain.effective_ap, this->action_chain.chain.ap_effect_bonus, damage); + log.debug_f("(physical) damage = {} (eff) + {} (bonus) = {}", this->action_chain.chain.effective_ap, this->action_chain.chain.ap_effect_bonus, damage); } else { - log.debug("(unknown attack medium) damage = 0"); + log.debug_f("(unknown attack medium) damage = 0"); } this->action_chain.chain.damage = is_nte ? (damage * this->action_chain.chain.damage_multiplier) : min(damage * this->action_chain.chain.damage_multiplier, 99); - log.debug("overall chain damage = %hd (base) * %hhd (mult) = %hhd", damage, this->action_chain.chain.damage_multiplier, this->action_chain.chain.damage); + log.debug_f("overall chain damage = {} (base) * {} (mult) = {}", damage, this->action_chain.chain.damage_multiplier, this->action_chain.chain.damage); if (apply_action_conditions) { auto this_sh = this->shared_from_this(); s->card_special->apply_action_conditions(EffectWhen::BEFORE_ANY_CARD_ATTACK, this_sh, this_sh, 2, nullptr); - log.debug("applied action conditions (2)"); + log.debug_f("applied action conditions (2)"); if (!is_nte && this->action_chain.check_flag(0x100)) { this->action_chain.chain.damage = min(this->action_chain.chain.damage + 5, 99); - log.debug("(has flag 0x100) chain damage = %hhd", this->action_chain.chain.damage); + log.debug_f("(has flag 0x100) chain damage = {}", this->action_chain.chain.damage); } } else { - log.debug("skipped applying action conditions (2)"); + log.debug_f("skipped applying action conditions (2)"); } if (!is_nte) { @@ -1156,9 +1156,9 @@ void Card::compute_action_chain_results(bool apply_action_conditions, bool ignor } } - if (log.should_log(phosg::LogLevel::DEBUG)) { + if (log.should_log(phosg::LogLevel::L_DEBUG)) { string chain_str = this->action_chain.str(s); - log.debug("result computed as %s", chain_str.c_str()); + log.debug_f("result computed as {}", chain_str); } } @@ -1226,24 +1226,24 @@ void Card::move_phase_before() { void Card::unknown_80236374(shared_ptr other_card, const ActionState* as) { auto s = this->server(); - auto log = s->log_stack(phosg::string_printf("unknown_80236374(@%04hX #%04hX, @%04hX #%04hX): ", this->get_card_ref(), this->get_card_id(), other_card->get_card_ref(), other_card->get_card_id())); + auto log = s->log_stack(std::format("unknown_80236374(@{:04X} #{:04X}, @{:04X} #{:04X}): ", this->get_card_ref(), this->get_card_id(), other_card->get_card_ref(), other_card->get_card_id())); - if (log.should_log(phosg::LogLevel::DEBUG)) { + if (log.should_log(phosg::LogLevel::L_DEBUG)) { if (as) { string as_str = as->str(s); - log.debug("as = %s", as_str.c_str()); + log.debug_f("as = {}", as_str); } else { - log.debug("as = null"); + log.debug_f("as = null"); } } auto check_card = [&](shared_ptr card) -> void { if (card) { if (!card->unknown_80236554(other_card, as)) { - log.debug("check_card @%04hX #%04hX => false", card->get_card_ref(), card->get_card_id()); + log.debug_f("check_card @{:04X} #{:04X} => false", card->get_card_ref(), card->get_card_id()); card->action_metadata.clear_flags(0x20); } else { - log.debug("check_card @%04hX #%04hX => true", card->get_card_ref(), card->get_card_id()); + log.debug_f("check_card @{:04X} #{:04X} => true", card->get_card_ref(), card->get_card_id()); card->action_metadata.set_flags(0x20); } } @@ -1378,14 +1378,14 @@ bool Card::is_guard_item() const { bool Card::unknown_80236554(shared_ptr other_card, const ActionState* as) { auto s = this->server(); auto log = s->log_stack(other_card - ? phosg::string_printf("unknown_80236554(@%04hX #%04hX, @%04hX #%04hX): ", this->get_card_ref(), this->get_card_id(), other_card->get_card_ref(), other_card->get_card_id()) - : phosg::string_printf("unknown_80236554(@%04hX #%04hX, null): ", this->get_card_ref(), this->get_card_id())); - if (log.should_log(phosg::LogLevel::DEBUG)) { + ? std::format("unknown_80236554(@{:04X} #{:04X}, @{:04X} #{:04X}): ", this->get_card_ref(), this->get_card_id(), other_card->get_card_ref(), other_card->get_card_id()) + : std::format("unknown_80236554(@{:04X} #{:04X}, null): ", this->get_card_ref(), this->get_card_id())); + if (log.should_log(phosg::LogLevel::L_DEBUG)) { if (as) { string as_str = as->str(s); - log.debug("as = %s", as_str.c_str()); + log.debug_f("as = {}", as_str); } else { - log.debug("as = null"); + log.debug_f("as = null"); } } @@ -1398,7 +1398,7 @@ bool Card::unknown_80236554(shared_ptr other_card, const ActionState* as) if (other_card->action_chain.chain.target_card_refs[z] == this->get_card_ref()) { attack_bonus = other_card->action_chain.chain.damage; ret = true; - log.debug("attack_bonus = %hd (matched other_card->action_chain.chain.target_card_refs)", attack_bonus); + log.debug_f("attack_bonus = {} (matched other_card->action_chain.chain.target_card_refs)", attack_bonus); break; } } @@ -1406,7 +1406,7 @@ bool Card::unknown_80236554(shared_ptr other_card, const ActionState* as) for (size_t z = 0; (z < 4 * 9) && (as->target_card_refs[z] != 0xFFFF); z++) { if (as->target_card_refs[z] == this->get_card_ref()) { attack_bonus = other_card->action_chain.chain.damage; - log.debug("attack_bonus = %hd (matched as->target_card_refs)", attack_bonus); + log.debug_f("attack_bonus = {} (matched as->target_card_refs)", attack_bonus); ret = true; break; } @@ -1415,26 +1415,26 @@ bool Card::unknown_80236554(shared_ptr other_card, const ActionState* as) } this->action_metadata.attack_bonus = max(attack_bonus, 0); - log.debug("attack_bonus = %hhd", this->action_metadata.attack_bonus); + log.debug_f("attack_bonus = {}", this->action_metadata.attack_bonus); this->last_attack_preliminary_damage = 0; this->last_attack_final_damage = 0; - log.debug("last attack damage stats cleared"); + log.debug_f("last attack damage stats cleared"); if (other_card) { - log.debug("applying BEFORE_ANY_CARD_ATTACK conditions"); + log.debug_f("applying BEFORE_ANY_CARD_ATTACK conditions"); s->card_special->apply_action_conditions( EffectWhen::BEFORE_ANY_CARD_ATTACK, other_card, this->shared_from_this(), 0x20, as); - log.debug("applying BEFORE_THIS_CARD_ATTACKED conditions"); + log.debug_f("applying BEFORE_THIS_CARD_ATTACKED conditions"); s->card_special->apply_action_conditions( EffectWhen::BEFORE_THIS_CARD_ATTACKED, other_card, this->shared_from_this(), 0x40, as); if (other_card->action_chain.check_flag(0x20000)) { - log.debug("attack_bonus cleared due to cancellation"); + log.debug_f("attack_bonus cleared due to cancellation"); this->action_metadata.attack_bonus = 0; return ret; } } if (this->card_flags & 2) { - log.debug("attack_bonus cleared due to destruction"); + log.debug_f("attack_bonus cleared due to destruction"); this->action_metadata.attack_bonus = 0; } return ret; @@ -1464,7 +1464,7 @@ void Card::apply_attack_result() { auto ps = this->player_state(); bool is_nte = s->options.is_nte(); - auto log = s->log_stack(phosg::string_printf("apply_attack_result(@%04hX #%04hX): ", this->get_card_ref(), this->get_card_id())); + auto log = s->log_stack(std::format("apply_attack_result(@{:04X} #{:04X}): ", this->get_card_ref(), this->get_card_id())); if (!this->action_chain.can_apply_attack()) { return; } @@ -1573,9 +1573,9 @@ void Card::apply_attack_result() { } } - if (log.should_log(phosg::LogLevel::DEBUG)) { + if (log.should_log(phosg::LogLevel::L_DEBUG)) { string as_str = as.str(s); - log.debug("as constructed as %s", as_str.c_str()); + log.debug_f("as constructed as {}", as_str); } for (size_t z = 0; z < this->action_chain.chain.target_card_ref_count; z++) { @@ -1583,36 +1583,36 @@ void Card::apply_attack_result() { if (card) { card->current_defense_power = card->action_metadata.attack_bonus; if (!this->action_chain.check_flag(0x40)) { - log.debug("unknown_8024A6DC(@%04hX #%04hX) ...", card->get_card_ref(), card->get_card_id()); + log.debug_f("unknown_8024A6DC(@{:04X} #{:04X}) ...", card->get_card_ref(), card->get_card_id()); s->card_special->unknown_8024A6DC(this->shared_from_this(), card); } } } - log.debug("compute_action_chain_results 1 ..."); + log.debug_f("compute_action_chain_results 1 ..."); this->compute_action_chain_results(true, false); if (!this->action_chain.check_flag(0x40)) { - log.debug("apply_effects_before_attack ..."); + log.debug_f("apply_effects_before_attack ..."); s->card_special->apply_effects_before_attack(this->shared_from_this()); } if (!(this->card_flags & 2)) { - log.debug("compute_action_chain_results 2 ..."); + log.debug_f("compute_action_chain_results 2 ..."); this->compute_action_chain_results(true, false); - log.debug("check_for_attack_interference ..."); + log.debug_f("check_for_attack_interference ..."); s->card_special->check_for_attack_interference(this->shared_from_this()); } - log.debug("compute_action_chain_results 3 ..."); + log.debug_f("compute_action_chain_results 3 ..."); this->compute_action_chain_results(true, false); - log.debug("unknown_80236374 ..."); + log.debug_f("unknown_80236374 ..."); this->unknown_80236374(this->shared_from_this(), nullptr); - log.debug("execute_attack_on_all_valid_targets ..."); + log.debug_f("execute_attack_on_all_valid_targets ..."); this->execute_attack_on_all_valid_targets(this->shared_from_this()); } if (!this->action_chain.check_flag(0x40)) { - log.debug("apply_effects_after_attack ..."); + log.debug_f("apply_effects_after_attack ..."); s->card_special->apply_effects_after_attack(this->shared_from_this()); } ps->stats.num_attacks_given++; @@ -1625,7 +1625,7 @@ void Card::apply_attack_result() { for (size_t client_id = 0; client_id < 4; client_id++) { auto ps = s->player_states[client_id]; if (ps) { - log.debug("unknown_8023C110(%zu) ...", client_id); + log.debug_f("unknown_8023C110({}) ...", client_id); ps->unknown_8023C110(); } } diff --git a/src/Episode3/CardSpecial.cc b/src/Episode3/CardSpecial.cc index fb34d168..2fc202b2 100644 --- a/src/Episode3/CardSpecial.cc +++ b/src/Episode3/CardSpecial.cc @@ -21,7 +21,7 @@ static string refs_str_for_cards_vector(const vector>& cards) { if (!ret.empty()) { ret += ", "; } - ret += phosg::string_printf("@%04hX", ref_for_card(card)); + ret += std::format("@{:04X}", ref_for_card(card)); } return ret; } @@ -89,45 +89,45 @@ uint32_t CardSpecial::AttackEnvStats::at(size_t index) const { } void CardSpecial::AttackEnvStats::print(FILE* stream) const { - fprintf(stream, "(a) total_num_set_cards = %" PRIu32 "\n", this->total_num_set_cards); - fprintf(stream, "(ab) num_a_beast_creatures = %" PRIu32 "\n", this->num_a_beast_creatures); - fprintf(stream, "(ac) player_num_atk_points = %" PRIu32 "\n", this->player_num_atk_points); - fprintf(stream, "(adm) sc_effective_ap = %" PRIu32 "\n", this->sc_effective_ap); - fprintf(stream, "(ap) effective_ap = %" PRIu32 "\n", this->effective_ap); - fprintf(stream, "(bi) num_native_creatures = %" PRIu32 "\n", this->num_native_creatures); - fprintf(stream, "(cs) card_cost = %" PRIu32 "\n", this->card_cost); - fprintf(stream, "(d) dice_roll_value1 = %" PRIu32 "\n", this->dice_roll_value1); - fprintf(stream, "(dc) dice_roll_value2 = %" PRIu32 "\n", this->dice_roll_value2); - fprintf(stream, "(ddm) attack_bonus = %" PRIu32 "\n", this->attack_bonus); - fprintf(stream, "(df) num_destroyed_ally_fcs = %" PRIu32 "\n", this->num_destroyed_ally_fcs); - fprintf(stream, "(dk) num_dark_creatures = %" PRIu32 "\n", this->num_dark_creatures); - fprintf(stream, "(dm) effective_ap_if_not_tech = %" PRIu32 "\n", this->effective_ap_if_not_tech); - fprintf(stream, "(dn) unknown_a1 = %" PRIu32 "\n", this->unknown_a1); - fprintf(stream, "(edm) target_attack_bonus = %" PRIu32 "\n", this->target_attack_bonus); - 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, "(f) num_set_cards = %" PRIu32 "\n", this->num_set_cards); - 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, "(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, "(hp) current_hp = %" PRIu32 "\n", this->current_hp); - fprintf(stream, "(kap) action_cards_ap = %" PRIu32 "\n", this->action_cards_ap); - fprintf(stream, "(ktp) action_cards_tp = %" PRIu32 "\n", this->action_cards_tp); - fprintf(stream, "(ldm) last_attack_preliminary_damage = %" PRIu32 "\n", this->last_attack_preliminary_damage); - fprintf(stream, "(lv) team_dice_bonus = %" PRIu32 "\n", this->team_dice_bonus); - fprintf(stream, "(mc) num_machine_creatures = %" PRIu32 "\n", this->num_machine_creatures); - fprintf(stream, "(mhp) max_hp = %" PRIu32 "\n", this->max_hp); - fprintf(stream, "(ndm) last_attack_damage_count = %" PRIu32 "\n", this->last_attack_damage_count); - fprintf(stream, "(php) defined_max_hp = %" PRIu32 "\n", this->defined_max_hp); - fprintf(stream, "(rdm) last_attack_damage = %" PRIu32 "\n", this->last_attack_damage); - fprintf(stream, "(sa) num_sword_type_items = %" PRIu32 "\n", this->num_sword_type_items); - fprintf(stream, "(sat) num_sword_type_items_on_team = %" PRIu32 "\n", this->num_sword_type_items_on_team); - fprintf(stream, "(tdm) effective_ap_if_not_physical = %" PRIu32 "\n", this->effective_ap_if_not_physical); - fprintf(stream, "(tf) player_num_destroyed_fcs = %" PRIu32 "\n", this->player_num_destroyed_fcs); - fprintf(stream, "(tp) effective_tp = %" PRIu32 "\n", this->effective_tp); - fprintf(stream, "(tt) effective_ap_if_not_tech2 = %" PRIu32 "\n", this->effective_ap_if_not_tech2); - fprintf(stream, "(wd) num_cane_type_items = %" PRIu32 "\n", this->num_cane_type_items); + phosg::fwrite_fmt(stream, "(a) total_num_set_cards = {}\n", this->total_num_set_cards); + phosg::fwrite_fmt(stream, "(ab) num_a_beast_creatures = {}\n", this->num_a_beast_creatures); + phosg::fwrite_fmt(stream, "(ac) player_num_atk_points = {}\n", this->player_num_atk_points); + phosg::fwrite_fmt(stream, "(adm) sc_effective_ap = {}\n", this->sc_effective_ap); + phosg::fwrite_fmt(stream, "(ap) effective_ap = {}\n", this->effective_ap); + phosg::fwrite_fmt(stream, "(bi) num_native_creatures = {}\n", this->num_native_creatures); + phosg::fwrite_fmt(stream, "(cs) card_cost = {}\n", this->card_cost); + phosg::fwrite_fmt(stream, "(d) dice_roll_value1 = {}\n", this->dice_roll_value1); + phosg::fwrite_fmt(stream, "(dc) dice_roll_value2 = {}\n", this->dice_roll_value2); + phosg::fwrite_fmt(stream, "(ddm) attack_bonus = {}\n", this->attack_bonus); + phosg::fwrite_fmt(stream, "(df) num_destroyed_ally_fcs = {}\n", this->num_destroyed_ally_fcs); + phosg::fwrite_fmt(stream, "(dk) num_dark_creatures = {}\n", this->num_dark_creatures); + phosg::fwrite_fmt(stream, "(dm) effective_ap_if_not_tech = {}\n", this->effective_ap_if_not_tech); + phosg::fwrite_fmt(stream, "(dn) unknown_a1 = {}\n", this->unknown_a1); + phosg::fwrite_fmt(stream, "(edm) target_attack_bonus = {}\n", this->target_attack_bonus); + phosg::fwrite_fmt(stream, "(ef) non_target_team_num_set_cards = {}\n", this->non_target_team_num_set_cards); + phosg::fwrite_fmt(stream, "(ehp) target_current_hp = {}\n", this->target_current_hp); + phosg::fwrite_fmt(stream, "(f) num_set_cards = {}\n", this->num_set_cards); + phosg::fwrite_fmt(stream, "(fdm) final_last_attack_damage = {}\n", this->final_last_attack_damage); + phosg::fwrite_fmt(stream, "(ff) target_team_num_set_cards = {}\n", this->target_team_num_set_cards); + phosg::fwrite_fmt(stream, "(gn) num_gun_type_items = {}\n", this->num_gun_type_items); + phosg::fwrite_fmt(stream, "(hf) num_item_or_creature_cards_in_hand = {}\n", this->num_item_or_creature_cards_in_hand); + phosg::fwrite_fmt(stream, "(hp) current_hp = {}\n", this->current_hp); + phosg::fwrite_fmt(stream, "(kap) action_cards_ap = {}\n", this->action_cards_ap); + phosg::fwrite_fmt(stream, "(ktp) action_cards_tp = {}\n", this->action_cards_tp); + phosg::fwrite_fmt(stream, "(ldm) last_attack_preliminary_damage = {}\n", this->last_attack_preliminary_damage); + phosg::fwrite_fmt(stream, "(lv) team_dice_bonus = {}\n", this->team_dice_bonus); + phosg::fwrite_fmt(stream, "(mc) num_machine_creatures = {}\n", this->num_machine_creatures); + phosg::fwrite_fmt(stream, "(mhp) max_hp = {}\n", this->max_hp); + phosg::fwrite_fmt(stream, "(ndm) last_attack_damage_count = {}\n", this->last_attack_damage_count); + phosg::fwrite_fmt(stream, "(php) defined_max_hp = {}\n", this->defined_max_hp); + phosg::fwrite_fmt(stream, "(rdm) last_attack_damage = {}\n", this->last_attack_damage); + phosg::fwrite_fmt(stream, "(sa) num_sword_type_items = {}\n", this->num_sword_type_items); + phosg::fwrite_fmt(stream, "(sat) num_sword_type_items_on_team = {}\n", this->num_sword_type_items_on_team); + phosg::fwrite_fmt(stream, "(tdm) effective_ap_if_not_physical = {}\n", this->effective_ap_if_not_physical); + phosg::fwrite_fmt(stream, "(tf) player_num_destroyed_fcs = {}\n", this->player_num_destroyed_fcs); + phosg::fwrite_fmt(stream, "(tp) effective_tp = {}\n", this->effective_tp); + phosg::fwrite_fmt(stream, "(tt) effective_ap_if_not_tech2 = {}\n", this->effective_ap_if_not_tech2); + phosg::fwrite_fmt(stream, "(wd) num_cane_type_items = {}\n", this->num_cane_type_items); } CardSpecial::CardSpecial(shared_ptr server) : w_server(server) {} @@ -251,14 +251,14 @@ void CardSpecial::apply_action_conditions( if (attacker_card == defender_card) { temp_as = this->create_attack_state_from_card_action_chain(attacker_card); if (as) { - log.debug("using action state from override"); + log.debug_f("using action state from override"); temp_as = *as; } else { - log.debug("using action state from attacker card"); + log.debug_f("using action state from attacker card"); } } else { temp_as = this->create_defense_state_for_card_pair_action_chains(attacker_card, defender_card); - log.debug("using action state from card pair"); + log.debug_f("using action state from card pair"); } this->apply_defense_conditions(temp_as, when, defender_card, flags); @@ -307,9 +307,9 @@ bool CardSpecial::apply_defense_condition( bool is_nte = s->options.is_nte(); auto log = s->log_stack("apply_defense_condition: "); - if (log.should_log(phosg::LogLevel::DEBUG)) { - log.debug( - "when=%s, cond_index=%hhu, defender_card=(@%04hX #%04hX), flags=%08" PRIX32 ", p8=%s", + if (log.should_log(phosg::LogLevel::L_DEBUG)) { + log.debug_f( + "when={}, cond_index={}, defender_card=(@{:04X} #{:04X}), flags={:08X}, p8={}", phosg::name_for_enum(when), cond_index, defender_card->get_card_ref(), @@ -318,32 +318,32 @@ bool CardSpecial::apply_defense_condition( unknown_p8 ? "true" : "false"); auto defender_cond_str = defender_cond->str(s); auto defense_state_str = defense_state.str(s); - log.debug("defender_cond = %s", defender_cond_str.c_str()); - log.debug("defense_state = %s", defense_state_str.c_str()); + log.debug_f("defender_cond = {}", defender_cond_str); + log.debug_f("defense_state = {}", defense_state_str); } if (defender_cond->type == ConditionType::NONE) { - log.debug("no condition"); + log.debug_f("no condition"); return false; } auto orig_eff = this->original_definition_for_condition(*defender_cond); - if (log.should_log(phosg::LogLevel::DEBUG)) { + if (log.should_log(phosg::LogLevel::L_DEBUG)) { auto orig_eff_str = orig_eff->str(); - log.debug("orig_eff = %s", orig_eff_str.c_str()); + log.debug_f("orig_eff = {}", orig_eff_str); } uint16_t attacker_card_ref = defense_state.attacker_card_ref; if (attacker_card_ref == 0xFFFF) { attacker_card_ref = defense_state.original_attacker_card_ref; } - log.debug("attacker_card_ref = @%04hX", attacker_card_ref); + log.debug_f("attacker_card_ref = @{:04X}", attacker_card_ref); bool defender_has_ability_trap = !is_nte && this->card_ref_has_ability_trap(*defender_cond); - log.debug("defender_has_ability_trap = %s", defender_has_ability_trap ? "true" : "false"); + log.debug_f("defender_has_ability_trap = {}", defender_has_ability_trap ? "true" : "false"); if ((is_nte || (flags & 4)) && !this->is_card_targeted_by_condition(*defender_cond, defense_state, defender_card)) { - log.debug("not targeted by condition"); + log.debug_f("not targeted by condition"); if (defender_cond->type != ConditionType::NONE) { G_ApplyConditionEffect_Ep3_6xB4x06 cmd; cmd.effect.flags = 0x04; @@ -359,7 +359,7 @@ bool CardSpecial::apply_defense_condition( } if ((when == EffectWhen::AFTER_ANY_CARD_ATTACK) && (defender_cond->type == ConditionType::GUOM) && (flags & 4)) { - log.debug("deleting guom condition"); + log.debug_f("deleting guom condition"); CardShortStatus stat = defender_card->get_short_status(); if (stat.card_flags & 4) { G_ApplyConditionEffect_Ep3_6xB4x06 cmd; @@ -396,7 +396,7 @@ bool CardSpecial::apply_defense_condition( } if ((when == EffectWhen::BEFORE_DICE_PHASE_THIS_TEAM_TURN) && (flags & 4) && !defender_has_ability_trap && (defender_cond->type == ConditionType::ACID)) { - log.debug("applying acid"); + log.debug_f("applying acid"); int16_t hp = defender_card->get_current_hp(); if (hp > 0) { this->send_6xB4x06_for_stat_delta(defender_card, defender_cond->card_ref, 0x20, -1, 0, 1); @@ -406,11 +406,11 @@ bool CardSpecial::apply_defense_condition( } if (!orig_eff || (orig_eff->when != when)) { - log.debug("unsetting flag 4"); + log.debug_f("unsetting flag 4"); flags &= ~4; } if ((flags == 0) || defender_has_ability_trap) { - log.debug("no condition remains to apply"); + log.debug_f("no condition remains to apply"); return false; } @@ -427,10 +427,10 @@ bool CardSpecial::apply_defense_condition( string expr = orig_eff->expr.decode(); int16_t expr_value = this->evaluate_effect_expr(astats, expr.c_str(), dice_roll); - log.debug("execute_effect ..."); + log.debug_f("execute_effect ..."); this->execute_effect(*defender_cond, defender_card, expr_value, defender_cond->value, orig_eff->type, flags, attacker_card_ref); if (flags & 4) { - log.debug("recomputing action chaing results"); + log.debug_f("recomputing action chaing results"); if (is_nte || !(defender_card->card_flags & 2)) { defender_card->compute_action_chain_results(true, false); } @@ -440,7 +440,7 @@ bool CardSpecial::apply_defense_condition( } if (dice_roll.value_used_in_expr && !(original_cond_flags & 1) && !unknown_p8) { - log.debug("dice roll was used; setting dice display flag"); + log.debug_f("dice roll was used; setting dice display flag"); defender_cond->flags |= 1; G_ApplyConditionEffect_Ep3_6xB4x06 cmd; cmd.effect.flags = 0x08; @@ -491,11 +491,11 @@ bool CardSpecial::apply_stat_deltas_to_all_cards_from_all_conditions_with_card_r bool CardSpecial::apply_stat_deltas_to_card_from_condition_and_clear_cond(Condition& cond, shared_ptr card) { auto s = this->server(); - auto log = s->log_stack(phosg::string_printf("apply_stat_deltas_to_card_from_condition_and_clear_cond(@%04hX #%04hX): ", card->get_card_ref(), card->get_card_id())); + auto log = s->log_stack(std::format("apply_stat_deltas_to_card_from_condition_and_clear_cond(@{:04X} #{:04X}): ", card->get_card_ref(), card->get_card_id())); bool is_nte = s->options.is_nte(); string cond_str = cond.str(s); - log.debug("cond: %s", cond_str.c_str()); + log.debug_f("cond: {}", cond_str); ConditionType cond_type = cond.type; int16_t cond_value = is_nte ? cond.value.load() : clamp(cond.value, -99, 99); @@ -508,7 +508,7 @@ bool CardSpecial::apply_stat_deltas_to_card_from_condition_and_clear_cond(Condit if (cond_flags & 2) { int16_t ap = clamp(card->ap, -99, 99); int16_t tp = clamp(card->tp, -99, 99); - log.debug("A_T_SWAP_0C: swapping AP (%hd) and TP (%hd)", ap, tp); + log.debug_f("A_T_SWAP_0C: swapping AP ({}) and TP ({})", ap, tp); if (!is_nte) { this->send_6xB4x06_for_stat_delta(card, cond_card_ref, 0xA0, tp - ap, 0, 0); this->send_6xB4x06_for_stat_delta(card, cond_card_ref, 0x80, ap - tp, 0, 0); @@ -516,7 +516,7 @@ bool CardSpecial::apply_stat_deltas_to_card_from_condition_and_clear_cond(Condit card->ap = tp; card->tp = ap; } else { - log.debug("A_T_SWAP_0C: required flag is missing"); + log.debug_f("A_T_SWAP_0C: required flag is missing"); } break; case ConditionType::A_H_SWAP: @@ -524,7 +524,7 @@ bool CardSpecial::apply_stat_deltas_to_card_from_condition_and_clear_cond(Condit int16_t ap = clamp(card->ap, -99, 99); int16_t hp = clamp(card->get_current_hp(), -99, 99); if (hp != ap) { - log.debug("A_H_SWAP: swapping AP (%hd) and HP (%hd)", ap, hp); + log.debug_f("A_H_SWAP: swapping AP ({}) and HP ({})", ap, hp); if (!is_nte) { this->send_6xB4x06_for_stat_delta(card, cond_card_ref, 0xA0, hp - ap, 0, 0); this->send_6xB4x06_for_stat_delta(card, cond_card_ref, 0x20, ap - hp, 0, 0); @@ -533,10 +533,10 @@ bool CardSpecial::apply_stat_deltas_to_card_from_condition_and_clear_cond(Condit card->ap = hp; this->destroy_card_if_hp_zero(card, cond_card_ref); } else { - log.debug("A_H_SWAP: AP (%hd) == HP (%hd)", ap, hp); + log.debug_f("A_H_SWAP: AP ({}) == HP ({})", ap, hp); } } else { - log.debug("A_H_SWAP: required flag is missing"); + log.debug_f("A_H_SWAP: required flag is missing"); } break; case ConditionType::AP_OVERRIDE: @@ -550,12 +550,12 @@ bool CardSpecial::apply_stat_deltas_to_card_from_condition_and_clear_cond(Condit this->send_6xB4x06_for_stat_delta(card, cond_card_ref, 0xA0, -cond_value, 0, 0); } card->ap = max(card->ap - cond_value, 0); - log.debug("AP_OVERRIDE: subtracting %hd from AP => %hd", cond_value, card->ap); + log.debug_f("AP_OVERRIDE: subtracting {} from AP => {}", cond_value, card->ap); } else { other_cond->value = clamp(other_cond->value + cond_value, -99, 99); } } else { - log.debug("AP_OVERRIDE: required flag is missing"); + log.debug_f("AP_OVERRIDE: required flag is missing"); } break; case ConditionType::TP_OVERRIDE: @@ -567,12 +567,12 @@ bool CardSpecial::apply_stat_deltas_to_card_from_condition_and_clear_cond(Condit this->send_6xB4x06_for_stat_delta(card, cond_card_ref, 0x80, -cond_value, 0, 0); } card->tp = max(card->tp - cond_value, 0); - log.debug("TP_OVERRIDE: subtracting %hd from TP => %hd", cond_value, card->tp); + log.debug_f("TP_OVERRIDE: subtracting {} from TP => {}", cond_value, card->tp); } else { other_cond->value = clamp(other_cond->value + cond_value, -99, 99); } } else { - log.debug("TP_OVERRIDE: required flag is missing"); + log.debug_f("TP_OVERRIDE: required flag is missing"); } break; case ConditionType::MISC_AP_BONUSES: @@ -581,9 +581,9 @@ bool CardSpecial::apply_stat_deltas_to_card_from_condition_and_clear_cond(Condit this->send_6xB4x06_for_stat_delta(card, cond_card_ref, 0xA0, -cond_value, 0, 0); } card->ap = max(card->ap - cond_value, 0); - log.debug("MISC_AP_BONUSES: subtracting %hd from AP => %hd", cond_value, card->ap); + log.debug_f("MISC_AP_BONUSES: subtracting {} from AP => {}", cond_value, card->ap); } else { - log.debug("MISC_AP_BONUSES: required flag is missing"); + log.debug_f("MISC_AP_BONUSES: required flag is missing"); } break; case ConditionType::MISC_TP_BONUSES: @@ -592,9 +592,9 @@ bool CardSpecial::apply_stat_deltas_to_card_from_condition_and_clear_cond(Condit this->send_6xB4x06_for_stat_delta(card, cond_card_ref, 0x80, -cond_value, 0, 0); } card->tp = max(card->tp - cond_value, 0); - log.debug("MISC_TP_BONUSES: subtracting %hd from TP => %hd", cond_value, card->tp); + log.debug_f("MISC_TP_BONUSES: subtracting {} from TP => {}", cond_value, card->tp); } else { - log.debug("MISC_TP_BONUSES: required flag is missing"); + log.debug_f("MISC_TP_BONUSES: required flag is missing"); } break; case ConditionType::AP_SILENCE: @@ -604,9 +604,9 @@ bool CardSpecial::apply_stat_deltas_to_card_from_condition_and_clear_cond(Condit if (cond_flags & 2) { this->send_6xB4x06_for_stat_delta(card, cond_card_ref, 0xA0, cond_value, 0, 0); card->ap = max(card->ap + cond_value, 0); - log.debug("AP_SILENCE: adding %hd to AP => %hd", cond_value, card->ap); + log.debug_f("AP_SILENCE: adding {} to AP => {}", cond_value, card->ap); } else { - log.debug("AP_SILENCE: required flag is missing"); + log.debug_f("AP_SILENCE: required flag is missing"); } break; case ConditionType::TP_SILENCE: @@ -616,14 +616,14 @@ bool CardSpecial::apply_stat_deltas_to_card_from_condition_and_clear_cond(Condit if (cond_flags & 2) { this->send_6xB4x06_for_stat_delta(card, cond_card_ref, 0x80, cond_value, 0, 0); card->tp = max(card->tp + cond_value, 0); - log.debug("TP_SILENCE: adding %hd to TP => %hd", cond_value, card->tp); + log.debug_f("TP_SILENCE: adding {} to TP => {}", cond_value, card->tp); } else { - log.debug("TP_SILENCE: required flag is missing"); + log.debug_f("TP_SILENCE: required flag is missing"); } break; trial_unimplemented: default: - log.debug("%s: no adjustments for condition type", phosg::name_for_enum(cond_type)); + log.debug_f("{}: no adjustments for condition type", phosg::name_for_enum(cond_type)); break; } @@ -741,22 +741,22 @@ CardSpecial::AttackEnvStats CardSpecial::compute_attack_env_stats( bool is_nte = s->options.is_nte(); string pa_str = pa.str(s); - log.debug("pa=%s, card=@%04hX #%04hX, dice_roll=%hhu, target=@%04hX, condition_giver=@%04hX", pa_str.c_str(), card->get_card_ref(), card->get_card_id(), dice_roll.value, target_card_ref, condition_giver_card_ref); + log.debug_f("pa={}, card=@{:04X} #{:04X}, dice_roll={}, target=@{:04X}, condition_giver=@{:04X}", pa_str, card->get_card_ref(), card->get_card_id(), dice_roll.value, target_card_ref, condition_giver_card_ref); auto attacker_card = s->card_for_set_card_ref(pa.attacker_card_ref); if (!attacker_card && (pa.original_attacker_card_ref != 0xFFFF)) { attacker_card = s->card_for_set_card_ref(pa.original_attacker_card_ref); - log.debug("attacker=@%04hX #%04hX (from original)", attacker_card->get_card_ref(), attacker_card->get_card_id()); + log.debug_f("attacker=@{:04X} #{:04X} (from original)", attacker_card->get_card_ref(), attacker_card->get_card_id()); } else if (attacker_card) { - log.debug("attacker=@%04hX #%04hX (from set)", attacker_card->get_card_ref(), attacker_card->get_card_id()); + log.debug_f("attacker=@{:04X} #{:04X} (from set)", attacker_card->get_card_ref(), attacker_card->get_card_id()); } else { - log.debug("attacker=null (from set)"); + log.debug_f("attacker=null (from set)"); } AttackEnvStats ast; auto ps = card->player_state(); - log.debug("base ps = %hhu", ps->client_id); + log.debug_f("base ps = {}", ps->client_id); ast.num_set_cards = is_nte ? ps->count_set_cards_for_env_stats_nte() : ps->count_set_cards(); auto condition_giver_card = s->card_for_set_card_ref(condition_giver_card_ref); auto target_card = s->card_for_set_card_ref(target_card_ref); @@ -871,8 +871,8 @@ CardSpecial::AttackEnvStats CardSpecial::compute_attack_env_stats( // Note: The (z < 8) conditions in these two loops are not present in the // original code. for (z = 0; - ((target_card_ref != z_ref) && (z < 8) && ((z_ref = pa.action_card_refs[z]) != 0xFFFF)); - z++) { + ((target_card_ref != z_ref) && (z < 8) && ((z_ref = pa.action_card_refs[z]) != 0xFFFF)); + z++) { } ast.action_cards_ap = 0; @@ -1227,9 +1227,9 @@ shared_ptr CardSpecial::compute_replaced_target_based_on_conditions( StatSwapType CardSpecial::compute_stat_swap_type(shared_ptr card) const { auto s = this->server(); - auto log = s->log_stack(phosg::string_printf("compute_stat_swap_type(@%04hX #%04hX): ", card->get_card_ref(), card->get_card_id())); + auto log = s->log_stack(std::format("compute_stat_swap_type(@{:04X} #{:04X}): ", card->get_card_ref(), card->get_card_id())); if (!card) { - log.debug("card is missing"); + log.debug_f("card is missing"); return StatSwapType::NONE; } @@ -1237,33 +1237,33 @@ StatSwapType CardSpecial::compute_stat_swap_type(shared_ptr card) co for (size_t cond_index = 0; cond_index < 9; cond_index++) { auto& cond = card->action_chain.conditions[cond_index]; if (cond.type != ConditionType::NONE) { - auto cond_log = log.sub(phosg::string_printf("(%zu) ", cond_index)); + auto cond_log = log.sub(std::format("({}) ", cond_index)); string cond_str = cond.str(s); - cond_log.debug("%s", cond_str.c_str()); + cond_log.debug_f("{}", cond_str); if (!this->card_ref_has_ability_trap(cond)) { if (cond.type == ConditionType::UNKNOWN_75) { if (ret == StatSwapType::A_H_SWAP) { - log.debug("UNKNOWN_75: clearing"); + log.debug_f("UNKNOWN_75: clearing"); ret = StatSwapType::NONE; } else { - log.debug("UNKNOWN_75: setting A_H_SWAP"); + log.debug_f("UNKNOWN_75: setting A_H_SWAP"); ret = StatSwapType::A_H_SWAP; } } else if (cond.type == ConditionType::A_T_SWAP) { if (ret == StatSwapType::A_T_SWAP) { - log.debug("A_T_SWAP: clearing"); + log.debug_f("A_T_SWAP: clearing"); ret = StatSwapType::NONE; } else { - log.debug("A_T_SWAP: setting A_T_SWAP"); + log.debug_f("A_T_SWAP: setting A_T_SWAP"); ret = StatSwapType::A_T_SWAP; } } } else { - log.debug("skipping due to ability trap"); + log.debug_f("skipping due to ability trap"); } } } - log.debug("ret = %zu", static_cast(ret)); + log.debug_f("ret = {}", static_cast(ret)); return ret; } @@ -1713,8 +1713,8 @@ int32_t CardSpecial::evaluate_effect_expr( const char* expr, DiceRoll& dice_roll) const { auto log = this->server()->log_stack("evaluate_effect_expr: "); - if (log.min_level == phosg::LogLevel::DEBUG) { - log.debug("ast, expr=\"%s\", dice_roll=(client_id=%02hhX, a2=%02hhX, value=%02hhX, value_used_in_expr=%s, a5=%04hX)", expr, dice_roll.client_id, dice_roll.unknown_a2, dice_roll.value, dice_roll.value_used_in_expr ? "true" : "false", dice_roll.unknown_a5); + if (log.min_level == phosg::LogLevel::L_DEBUG) { + log.debug_f("ast, expr=\"{}\", dice_roll=(client_id={:02X}, a2={:02X}, value={:02X}, value_used_in_expr={}, a5={:04X})", expr, dice_roll.client_id, dice_roll.unknown_a2, dice_roll.value, dice_roll.value_used_in_expr ? "true" : "false", dice_roll.unknown_a5); ast.print(stderr); } @@ -1747,7 +1747,7 @@ int32_t CardSpecial::evaluate_effect_expr( // Operators are evaluated left-to-right - there are no operator precedence // rules int32_t value = 0; - log.debug("value=%" PRId32 " (start)", value); + log.debug_f("value={} (start)", value); for (size_t token_index = 0; token_index < tokens.size(); token_index++) { auto token_type = tokens[token_index].first; int32_t token_value = tokens[token_index].second; @@ -1756,7 +1756,7 @@ int32_t CardSpecial::evaluate_effect_expr( } if (token_type == ExpressionTokenType::NUMBER) { value = token_value; - log.debug("value=%" PRId32 " (token_type=NUMBER, token_value=%" PRId32 ")", value, token_value); + log.debug_f("value={} (token_type=NUMBER, token_value={})", value, token_value); } else { if (token_index >= tokens.size() - 1) { throw runtime_error("no token on right side of binary operator"); @@ -1770,23 +1770,23 @@ int32_t CardSpecial::evaluate_effect_expr( switch (token_type) { case ExpressionTokenType::ROUND_DIVIDE: value = lround(static_cast(value) / right_value); - log.debug("value=%" PRId32 " (token_type=ROUND_DIVIDE, right_token_value=%" PRId32 ")", value, right_value); + log.debug_f("value={} (token_type=ROUND_DIVIDE, right_token_value={})", value, right_value); break; case ExpressionTokenType::SUBTRACT: value -= right_value; - log.debug("value=%" PRId32 " (token_type=SUBTRACT, right_token_value=%" PRId32 ")", value, right_value); + log.debug_f("value={} (token_type=SUBTRACT, right_token_value={})", value, right_value); break; case ExpressionTokenType::ADD: value += right_value; - log.debug("value=%" PRId32 " (token_type=ADD, right_token_value=%" PRId32 ")", value, right_value); + log.debug_f("value={} (token_type=ADD, right_token_value={})", value, right_value); break; case ExpressionTokenType::MULTIPLY: value *= right_value; - log.debug("value=%" PRId32 " (token_type=MULTIPLY, right_token_value=%" PRId32 ")", value, right_value); + log.debug_f("value={} (token_type=MULTIPLY, right_token_value={})", value, right_value); break; case ExpressionTokenType::FLOOR_DIVIDE: value = floor(value / right_value); - log.debug("value=%" PRId32 " (token_type=FLOOR_DIVIDE, right_token_value=%" PRId32 ")", value, right_value); + log.debug_f("value={} (token_type=FLOOR_DIVIDE, right_token_value={})", value, right_value); break; default: throw logic_error("invalid binary operator"); @@ -1794,7 +1794,7 @@ int32_t CardSpecial::evaluate_effect_expr( } } - log.debug("value=%" PRId32 " (result)", value); + log.debug_f("value={} (result)", value); return value; } @@ -1807,10 +1807,10 @@ bool CardSpecial::execute_effect( uint32_t unknown_p7, uint16_t attacker_card_ref) { auto s = this->server(); - auto log = s->log_stack(phosg::string_printf("execute_effect(@%04hX #%04hX): ", card->get_card_ref(), card->get_card_id())); + auto log = s->log_stack(std::format("execute_effect(@{:04X} #{:04X}): ", card->get_card_ref(), card->get_card_id())); { string cond_str = cond.str(s); - log.debug("cond=%s, card=@%04hX, expr_value=%hd, unknown_p5=%hd, cond_type=%s, unknown_p7=%" PRIu32 ", attacker_card_ref=@%04hX", cond_str.c_str(), ref_for_card(card), expr_value, unknown_p5, phosg::name_for_enum(cond_type), unknown_p7, attacker_card_ref); + log.debug_f("cond={}, card=@{:04X}, expr_value={}, unknown_p5={}, cond_type={}, unknown_p7={} attacker_card_ref=@{:04X}", cond_str, ref_for_card(card), expr_value, unknown_p5, phosg::name_for_enum(cond_type), unknown_p7, attacker_card_ref); } bool is_nte = s->options.is_nte(); @@ -1959,7 +1959,7 @@ bool CardSpecial::execute_effect( if (unknown_p7 & 4) { int16_t hp = is_nte ? card->get_current_hp() : clamp(card->get_current_hp(), -99, 99); int16_t new_hp = is_nte ? (hp + positive_expr_value) : clamp(hp + positive_expr_value, -99, 99); - log.debug("HEAL: hp=%hd, positive_expr_value=%hd, new_hp=%hd", hp, positive_expr_value, new_hp); + log.debug_f("HEAL: hp={}, positive_expr_value={}, new_hp={}", hp, positive_expr_value, new_hp); this->send_6xB4x06_for_stat_delta(card, attacker_card_ref, 0x20, new_hp - hp, true, true); if (new_hp != hp) { card->set_current_hp(new_hp); @@ -2842,8 +2842,8 @@ vector> CardSpecial::get_targeted_cards_for_condition( int16_t p_target_type, bool apply_usability_filters) const { auto s = this->server(); - auto log = s->log_stack(phosg::string_printf("get_targeted_cards_for_condition(@%04hX, %hhu, @%04hX): ", card_ref, def_effect_index, setter_card_ref)); - log.debug("card_ref=@%04hX, def_effect_index=%02hhX, setter_card_ref=@%04hX, as, p_target_type=%hd, apply_usability_filters=%s", card_ref, def_effect_index, setter_card_ref, p_target_type, apply_usability_filters ? "true" : "false"); + auto log = s->log_stack(std::format("get_targeted_cards_for_condition(@{:04X}, {}, @{:04X}): ", card_ref, def_effect_index, setter_card_ref)); + log.debug_f("card_ref=@{:04X}, def_effect_index={:02X}, setter_card_ref=@{:04X}, as, p_target_type={}, apply_usability_filters={}", card_ref, def_effect_index, setter_card_ref, p_target_type, apply_usability_filters ? "true" : "false"); vector> ret; @@ -2852,12 +2852,12 @@ vector> CardSpecial::get_targeted_cards_for_condition( if (!card1) { card1 = s->card_for_set_card_ref(setter_card_ref); } - log.debug("card1=@%04hX", ref_for_card(card1)); + log.debug_f("card1=@{:04X}", ref_for_card(card1)); auto card2 = s->card_for_set_card_ref((as.attacker_card_ref == 0xFFFF) ? as.original_attacker_card_ref : as.attacker_card_ref); - log.debug("card2=@%04hX", ref_for_card(card2)); + log.debug_f("card2=@{:04X}", ref_for_card(card2)); Location card1_loc; if (!card1) { @@ -2868,13 +2868,13 @@ vector> CardSpecial::get_targeted_cards_for_condition( this->get_card1_loc_with_card2_opposite_direction(&card1_loc, card1, card2); string card1_loc_str = card1_loc.str(); - log.debug("card1_loc=%s", card1_loc_str.c_str()); + log.debug_f("card1_loc={}", card1_loc_str); } AttackMedium attack_medium = card2 ? card2->action_chain.chain.attack_medium : AttackMedium::UNKNOWN; - log.debug("attack_medium=%s", phosg::name_for_enum(attack_medium)); + log.debug_f("attack_medium={}", phosg::name_for_enum(attack_medium)); auto add_card_refs = [&](const vector& result_card_refs) -> void { for (uint16_t result_card_ref : result_card_refs) { @@ -2890,10 +2890,10 @@ vector> CardSpecial::get_targeted_cards_for_condition( case 0x05: { // p05 auto result_card = s->card_for_set_card_ref(setter_card_ref); if (result_card) { - log.debug("(p01/p05) result_card=@%04hX", ref_for_card(result_card)); + log.debug_f("(p01/p05) result_card=@{:04X}", ref_for_card(result_card)); ret.emplace_back(result_card); } else { - log.debug("(p01/p05) result_card=null"); + log.debug_f("(p01/p05) result_card=null"); } break; } @@ -3073,28 +3073,28 @@ vector> CardSpecial::get_targeted_cards_for_condition( if (def && ps) { // TODO: Again with the Gifoie hardcoding... uint16_t range_card_id = this->get_card_id_with_effective_range(card1, 0x00D9, card2); - log23.debug("effective range card ID is #%04hX", range_card_id); + log23.debug_f("effective range card ID is #{:04X}", range_card_id); parray range; compute_effective_range(range, s->options.card_index, range_card_id, card1_loc, s->map_and_rules, &log23); auto result_card_refs = ps->get_all_cards_within_range(range, card1_loc, 0xFF); - log23.debug("%zu result card refs", result_card_refs.size()); + log23.debug_f("{} result card refs", result_card_refs.size()); for (uint16_t result_card_ref : result_card_refs) { - auto result_log = log23.subf("(result @%04hX) ", result_card_ref); + auto result_log = log23.sub(std::format("(result @{:04X}) ", result_card_ref)); auto result_card = s->card_for_set_card_ref(result_card_ref); if (!result_card) { - result_log.debug("result card not found"); + result_log.debug_f("result card not found"); } else if (result_card->get_definition()->def.type == CardType::ITEM) { - result_log.debug("result card is item"); + result_log.debug_f("result card is item"); } else { - result_log.debug("result card found and is not item"); + result_log.debug_f("result card found and is not item"); ret.emplace_back(result_card); } } } else { - log23.debug("def or ps is missing"); + log23.debug_f("def or ps is missing"); } } else { - log23.debug("card1 is missing"); + log23.debug_f("card1 is missing"); } break; } @@ -3194,28 +3194,28 @@ vector> CardSpecial::get_targeted_cards_for_condition( }; bool is_nte = s->options.is_nte(); if (as.original_attacker_card_ref == 0xFFFF) { - log36.debug("original_attacker_card_ref missing"); + log36.debug_f("original_attacker_card_ref missing"); // debug_str_for_card_ref for (size_t z = 0; (z < 4 * 9) && (as.target_card_refs[z] != 0xFFFF); z++) { string debug_ref_str = s->debug_str_for_card_ref(as.target_card_refs[z]); - log36.debug("examining %s", debug_ref_str.c_str()); + log36.debug_f("examining {}", debug_ref_str); auto result_card = s->card_for_set_card_ref(as.target_card_refs[z]); if (result_card && should_include(result_card->get_definition(), is_nte)) { - log36.debug("adding %s", debug_ref_str.c_str()); + log36.debug_f("adding {}", debug_ref_str); ret.emplace_back(result_card); } else { - log36.debug("skipping %s", debug_ref_str.c_str()); + log36.debug_f("skipping {}", debug_ref_str); } } } else if (card2 && should_include(card2->get_definition(), is_nte)) { string debug_ref_str = s->debug_str_for_card_ref(card2->get_card_ref()); - log36.debug("original_attacker_card_ref present; adding card2 = %s", debug_ref_str.c_str()); + log36.debug_f("original_attacker_card_ref present; adding card2 = {}", debug_ref_str); ret.emplace_back(card2); } else if (card2) { string debug_ref_str = s->debug_str_for_card_ref(card2->get_card_ref()); - log36.debug("original_attacker_card_ref present and card2 (%s) not eligible", debug_ref_str.c_str()); + log36.debug_f("original_attacker_card_ref present and card2 ({}) not eligible", debug_ref_str); } else { - log36.debug("original_attacker_card_ref present and card2 missing"); + log36.debug_f("original_attacker_card_ref present and card2 missing"); } break; } @@ -3252,17 +3252,17 @@ vector> CardSpecial::get_targeted_cards_for_condition( ret = this->find_all_set_cards_with_cost_in_range( (p_target_type == 0x27) ? 4 : 0, (p_target_type == 0x27) ? 99 : 3); - if (log3940.should_log(phosg::LogLevel::DEBUG)) { + if (log3940.should_log(phosg::LogLevel::L_DEBUG)) { for (const auto& card : ret) { - log3940.debug("found target @%04hX #%04hX", card->get_card_ref(), card->get_card_id()); + log3940.debug_f("found target @{:04X} #{:04X}", card->get_card_ref(), card->get_card_id()); } } if (!s->options.is_nte()) { - log3940.debug("filtering targets"); + log3940.debug_f("filtering targets"); ret = this->filter_cards_by_range(ret, card1, card1_loc, card2); - if (log3940.should_log(phosg::LogLevel::DEBUG)) { + if (log3940.should_log(phosg::LogLevel::L_DEBUG)) { for (const auto& card : ret) { - log3940.debug("retained target @%04hX #%04hX", card->get_card_ref(), card->get_card_id()); + log3940.debug_f("retained target @{:04X} #{:04X}", card->get_card_ref(), card->get_card_id()); } } } @@ -3497,9 +3497,9 @@ vector> CardSpecial::get_targeted_cards_for_condition( if (s->ruler_server->check_usability_or_apply_condition_for_card_refs( card_ref, setter_card_ref, c->get_card_ref(), def_effect_index, attack_medium)) { filtered_ret.emplace_back(c); - log.debug("usability filter: kept card @%04hX", ref_for_card(c)); + log.debug_f("usability filter: kept card @{:04X}", ref_for_card(c)); } else { - log.debug("usability filter: removed card @%04hX", ref_for_card(c)); + log.debug_f("usability filter: removed card @{:04X}", ref_for_card(c)); } } return filtered_ret; @@ -3526,16 +3526,16 @@ bool CardSpecial::is_card_targeted_by_condition( auto s = this->server(); auto log = s->log_stack("is_card_targeted_by_condition: "); - if (log.should_log(phosg::LogLevel::DEBUG)) { - log.debug("card=(@%04hX #%04hX)", card->get_card_ref(), card->get_card_id()); + if (log.should_log(phosg::LogLevel::L_DEBUG)) { + log.debug_f("card=(@{:04X} #{:04X})", card->get_card_ref(), card->get_card_id()); auto cond_str = cond.str(s); auto as_str = as.str(s); - log.debug("cond = %s", cond_str.c_str()); - log.debug("as = %s", as_str.c_str()); + log.debug_f("cond = {}", cond_str); + log.debug_f("as = {}", as_str); } if (cond.type == ConditionType::NONE) { - log.debug("condition is NONE (=> true)"); + log.debug_f("condition is NONE (=> true)"); return true; } @@ -3546,12 +3546,12 @@ bool CardSpecial::is_card_targeted_by_condition( ((ce->def.type == CardType::ITEM) || ce->def.is_sc()) && (cond.remaining_turns != 100) && (s->options.is_nte() || (client_id_for_card_ref(card->get_card_ref()) == client_id_for_card_ref(cond.card_ref)))) { - log.debug("failed item or SC check (=> false)"); + log.debug_f("failed item or SC check (=> false)"); return false; } if (cond.remaining_turns != 102) { - log.debug("remaining_turns != 102 (=> true)"); + log.debug_f("remaining_turns != 102 (=> true)"); return true; } @@ -3569,15 +3569,15 @@ bool CardSpecial::is_card_targeted_by_condition( 0); for (auto c : target_cards) { if (c == card) { - log.debug("targeted by p condition (=> true)"); + log.debug_f("targeted by p condition (=> true)"); return true; } } - log.debug("not targeted by p condition (=> false)"); + log.debug_f("not targeted by p condition (=> false)"); return false; } else { - log.debug("SC check does not apply"); + log.debug_f("SC check does not apply"); return false; } } @@ -3806,7 +3806,7 @@ size_t CardSpecial::sum_last_attack_damage( size_t damage_count = 0; auto check_card = [&](shared_ptr c) -> void { if (c && (c->last_attack_final_damage > 0)) { - log.debug("check_card @%04hX #%04hX => %hd", c->get_card_ref(), c->get_card_id(), c->last_attack_final_damage); + log.debug_f("check_card @{:04X} #{:04X} => {}", c->get_card_ref(), c->get_card_id(), c->last_attack_final_damage); if (out_damage_sum) { *out_damage_sum += c->last_attack_final_damage; } @@ -4013,13 +4013,13 @@ void CardSpecial::evaluate_and_apply_effects( bool apply_defense_condition_to_all_cards, uint16_t apply_defense_condition_to_card_ref) { auto s = this->server(); - auto log = s->log_stack(phosg::string_printf("evaluate_and_apply_effects(%s, @%04hX, @%04hX): ", phosg::name_for_enum(when), set_card_ref, sc_card_ref)); + auto log = s->log_stack(std::format("evaluate_and_apply_effects({}, @{:04X}, @{:04X}): ", phosg::name_for_enum(when), set_card_ref, sc_card_ref)); bool is_nte = s->options.is_nte(); { string as_str = as.str(s); - 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", - phosg::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); + log.debug_f("when={}, set_card_ref=@{:04X}, as={}, sc_card_ref=@{:04X}, apply_defense_condition_to_all_cards={}, apply_defense_condition_to_card_ref=@{:04X}", + phosg::name_for_enum(when), set_card_ref, as_str, sc_card_ref, apply_defense_condition_to_all_cards ? "true" : "false", apply_defense_condition_to_card_ref); } if (!is_nte) { @@ -4028,7 +4028,7 @@ void CardSpecial::evaluate_and_apply_effects( auto ce = this->server()->definition_for_card_ref(set_card_ref); if (!ce) { - log.debug("ce missing"); + log.debug_f("ce missing"); return; } @@ -4076,15 +4076,15 @@ void CardSpecial::evaluate_and_apply_effects( dice_roll.unknown_a2 = 3; dice_roll.value_used_in_expr = false; - log.debug("inputs: dice_roll=%02hhX, random_percent=%hhu, unknown_v1=%s", dice_roll.value, random_percent, unknown_v1 ? "true" : "false"); + log.debug_f("inputs: dice_roll={:02X}, random_percent={}, unknown_v1={}", dice_roll.value, random_percent, unknown_v1 ? "true" : "false"); for (size_t def_effect_index = 0; (def_effect_index < 3) && !unknown_v1 && (ce->def.effects[def_effect_index].type != ConditionType::NONE); def_effect_index++) { - auto effect_log = log.sub(phosg::string_printf("(effect:%zu) ", def_effect_index)); + auto effect_log = log.sub(std::format("(effect:{}) ", def_effect_index)); const auto& card_effect = ce->def.effects[def_effect_index]; string card_effect_str = card_effect.str(); - effect_log.debug("effect: %s", card_effect_str.c_str()); + effect_log.debug_f("effect: {}", card_effect_str); if (card_effect.when != when) { - effect_log.debug("does not apply (effect.when=%s, when=%s)", phosg::name_for_enum(card_effect.when), phosg::name_for_enum(when)); + effect_log.debug_f("does not apply (effect.when={}, when={})", phosg::name_for_enum(card_effect.when), phosg::name_for_enum(when)); continue; } @@ -4093,18 +4093,18 @@ void CardSpecial::evaluate_and_apply_effects( throw runtime_error("card effect arg3 is missing"); } int16_t arg3_value = atoi(arg3_s.c_str() + 1); - effect_log.debug("arg3_value=%hd", arg3_value); + effect_log.debug_f("arg3_value={}", arg3_value); auto targeted_cards = this->get_targeted_cards_for_condition( set_card_ref, def_effect_index, sc_card_ref, as, arg3_value, 1); string refs_str = refs_str_for_cards_vector(targeted_cards); - effect_log.debug("targeted_cards=[%s]", refs_str.c_str()); + effect_log.debug_f("targeted_cards=[{}]", refs_str); bool all_targets_matched = false; if (!is_nte && !targeted_cards.empty() && ((card_effect.type == ConditionType::UNKNOWN_64) || (card_effect.type == ConditionType::MISC_DEFENSE_BONUSES) || (card_effect.type == ConditionType::MOSTLY_HALFGUARDS))) { - effect_log.debug("special targeting applies"); + effect_log.debug_f("special targeting applies"); size_t count = 0; for (size_t z = 0; z < targeted_cards.size(); z++) { dice_roll.value_used_in_expr = false; @@ -4132,22 +4132,22 @@ void CardSpecial::evaluate_and_apply_effects( targeted_cards.clear(); } } else { - effect_log.debug("special targeting does not apply"); + effect_log.debug_f("special targeting does not apply"); } for (size_t z = 0; z < targeted_cards.size(); z++) { - auto target_log = effect_log.sub(phosg::string_printf("(target:@%04hX) ", targeted_cards[z]->get_card_ref())); + auto target_log = effect_log.sub(std::format("(target:@{:04X}) ", targeted_cards[z]->get_card_ref())); dice_roll.value_used_in_expr = false; string arg2_str = card_effect.arg2.decode(); - target_log.debug("arg2_str = %s", arg2_str.c_str()); + target_log.debug_f("arg2_str = {}", arg2_str); if (all_targets_matched || this->evaluate_effect_arg2_condition( as, targeted_cards[z], arg2_str.c_str(), dice_roll, set_card_ref, sc_card_ref, random_percent, when)) { - target_log.debug("arg2 condition passed"); + target_log.debug_f("arg2 condition passed"); auto env_stats = this->compute_attack_env_stats(as, targeted_cards[z], dice_roll, set_card_ref, sc_card_ref); string expr_str = card_effect.expr.decode(); int16_t value = this->evaluate_effect_expr(env_stats, expr_str.c_str(), dice_roll); - target_log.debug("expr = %s, value = %hd", expr_str.c_str(), value); + target_log.debug_f("expr = {}, value = {}", expr_str, value); uint32_t unknown_v1 = 0; auto target_card = this->compute_replaced_target_based_on_conditions( @@ -4163,16 +4163,16 @@ void CardSpecial::evaluate_and_apply_effects( sc_card_ref); if (!target_card) { target_card = targeted_cards[z]; - target_log.debug("target card (not replaced) = @%04hX", target_card->get_card_ref()); + target_log.debug_f("target card (not replaced) = @{:04X}", target_card->get_card_ref()); } else { - target_log.debug("target card (replaced) = @%04hX", target_card->get_card_ref()); + target_log.debug_f("target card (replaced) = @{:04X}", target_card->get_card_ref()); } ssize_t applied_cond_index = -1; if ((unknown_v1 == 0) && !this->should_cancel_condition_due_to_anti_abnormality(card_effect, target_card, dice_cmd.effect.target_card_ref, sc_card_ref)) { applied_cond_index = target_card->apply_abnormal_condition( card_effect, def_effect_index, dice_cmd.effect.target_card_ref, sc_card_ref, value, dice_roll.value, random_percent); - target_log.debug("applied abnormal condition"); + target_log.debug_f("applied abnormal condition"); // This debug_print call is in the original code. // this->debug_print(when, 4, &env_stats, "!set_abnormal..", target_card, card_effect.type); } @@ -4202,12 +4202,12 @@ void CardSpecial::evaluate_and_apply_effects( if (apply_defense_condition_to_all_cards || (apply_defense_condition_to_card_ref == targeted_cards[z]->get_card_ref())) { this->apply_defense_condition( when, &target_card->action_chain.conditions[applied_cond_index], applied_cond_index, as, target_card, 4, 1); - target_log.debug("applied defense condition"); + target_log.debug_f("applied defense condition"); } } target_card->send_6xB4x4E_4C_4D_if_needed(0); } else { - target_log.debug("arg2 condition failed"); + target_log.debug_f("arg2 condition failed"); } if (dice_roll.value_used_in_expr) { any_expr_used_dice_roll = true; @@ -4712,73 +4712,73 @@ vector> CardSpecial::filter_cards_by_range( shared_ptr card2) const { auto log = this->server()->log_stack("filter_cards_by_range: "); - if (log.should_log(phosg::LogLevel::DEBUG)) { - auto card1_str = card1 ? phosg::string_printf("@%04hX #%04hX", card1->get_card_ref(), card1->get_card_id()) : "null"; - auto card2_str = card2 ? phosg::string_printf("@%04hX #%04hX", card2->get_card_ref(), card2->get_card_id()) : "null"; + if (log.should_log(phosg::LogLevel::L_DEBUG)) { + auto card1_str = card1 ? std::format("@{:04X} #{:04X}", card1->get_card_ref(), card1->get_card_id()) : "null"; + auto card2_str = card2 ? std::format("@{:04X} #{:04X}", card2->get_card_ref(), card2->get_card_id()) : "null"; auto loc_str = card1_loc.str(); - log.debug("card1=(%s), card2=(%s), loc=%s", card1_str.c_str(), card2_str.c_str(), loc_str.c_str()); + log.debug_f("card1=({}), card2=({}), loc={}", card1_str, card2_str, loc_str); for (const auto& card : cards) { if (card) { - log.debug("input card: @%04hX #%04hX", card->get_card_ref(), card->get_card_id()); + log.debug_f("input card: @{:04X} #{:04X}", card->get_card_ref(), card->get_card_id()); } else { - log.debug("input card: null"); + log.debug_f("input card: null"); } } } vector> ret; if (!card1 || cards.empty()) { - log.debug("card1 missing or input list is blank"); + log.debug_f("card1 missing or input list is blank"); return ret; } auto ps = card1->player_state(); if (!ps) { - log.debug("ps is missing"); + log.debug_f("ps is missing"); return ret; } // TODO: Remove hardcoded card ID here (Earthquake) uint16_t card_id = this->get_card_id_with_effective_range(card1, 0x00ED, card2); - log.debug("card_id = #%04hX", card_id); + log.debug_f("card_id = #{:04X}", card_id); parray range; compute_effective_range(range, this->server()->options.card_index, card_id, card1_loc, this->server()->map_and_rules); - if (log.should_log(phosg::LogLevel::DEBUG)) { + if (log.should_log(phosg::LogLevel::L_DEBUG)) { auto loc_str = card1_loc.str(); - log.debug("compute_effective_range(range, ci, #%04hX, %s, map) =>", card_id, loc_str.c_str()); + log.debug_f("compute_effective_range(range, ci, #{:04X}, {}, map) =>", card_id, loc_str); for (size_t y = 0; y < 9; y++) { const uint8_t* row = &range[y * 9]; - log.debug(" range[%zu] = %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX", + log.debug_f(" range[{}] = {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X}", y, row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8]); } } auto card_refs_in_range = ps->get_card_refs_within_range_from_all_players(range, card1_loc, CardType::ITEM); - if (log.should_log(phosg::LogLevel::DEBUG)) { + if (log.should_log(phosg::LogLevel::L_DEBUG)) { for (uint16_t card_ref : card_refs_in_range) { - log.debug("ref in range: @%04hX", card_ref); + log.debug_f("ref in range: @{:04X}", card_ref); } } for (auto card : cards) { if (!card || (card->get_card_ref() == 0xFFFF)) { if (card) { - log.debug("(@%04hX #%04hX) out of range", card->get_card_ref(), card->get_card_id()); + log.debug_f("(@{:04X} #{:04X}) out of range", card->get_card_ref(), card->get_card_id()); } else { - log.debug("(null) card missing"); + log.debug_f("(null) card missing"); } continue; } for (uint16_t card_ref_in_range : card_refs_in_range) { if (card_ref_in_range == card->get_card_ref()) { - log.debug("(@%04hX #%04hX) in range", card->get_card_ref(), card->get_card_id()); + log.debug_f("(@{:04X} #{:04X}) in range", card->get_card_ref(), card->get_card_id()); ret.emplace_back(card); break; } } - log.debug("(@%04hX #%04hX) out of range", card->get_card_ref(), card->get_card_id()); + log.debug_f("(@{:04X} #{:04X}) out of range", card->get_card_ref(), card->get_card_id()); } return ret; } @@ -4787,7 +4787,7 @@ void CardSpecial::apply_effects_after_attack_target_resolution(const ActionState auto s = this->server(); auto log = s->log_stack("apply_effects_after_attack_target_resolution: "); string as_str = as.str(s); - log.debug("as=%s", as_str.c_str()); + log.debug_f("as={}", as_str); for (size_t z = 0; (z < 8) && (as.action_card_refs[z] != 0xFFFF); z++) { uint16_t card_ref = this->send_6xB4x06_if_card_ref_invalid(as.action_card_refs[z], 0x1E); @@ -4878,7 +4878,7 @@ void CardSpecial::dice_phase_before_for_card(shared_ptr card) { template void CardSpecial::apply_effects_on_phase_change_t(shared_ptr unknown_p2, const ActionState* existing_as) { auto s = this->server(); - auto log = s->log_stack(phosg::string_printf("apply_effects_on_phase_change_t<%s, %s>(@%04hX #%04hX): ", phosg::name_for_enum(When1), phosg::name_for_enum(When2), unknown_p2->get_card_ref(), unknown_p2->get_card_id())); + auto log = s->log_stack(std::format("apply_effects_on_phase_change_t<{}, {}>(@{:04X} #{:04X}): ", phosg::name_for_enum(When1), phosg::name_for_enum(When2), unknown_p2->get_card_ref(), unknown_p2->get_card_id())); bool is_nte = s->options.is_nte(); ActionState as; @@ -4929,7 +4929,7 @@ void CardSpecial::unknown_8024945C(shared_ptr unknown_p2, const ActionStat } void CardSpecial::unknown_8024966C(shared_ptr unknown_p2, const ActionState* existing_as) { - auto log = this->server()->log_stack(phosg::string_printf("unknown_8024966C(@%04hX #%04hX): ", unknown_p2->get_card_ref(), unknown_p2->get_card_id())); + auto log = this->server()->log_stack(std::format("unknown_8024966C(@{:04X} #{:04X}): ", unknown_p2->get_card_ref(), unknown_p2->get_card_id())); ActionState as; if (!existing_as) { @@ -5080,7 +5080,7 @@ template < EffectWhen WhenTargetsAndActionCards> void CardSpecial::apply_effects_before_or_after_attack(shared_ptr unknown_p2) { auto s = this->server(); - auto log = s->log_stack(phosg::string_printf("apply_effects_before_or_after_attack<%s, %s, %s, %s>(@%04hX #%04hX): ", + auto log = s->log_stack(std::format("apply_effects_before_or_after_attack<{}, {}, {}, {}>(@{:04X} #{:04X}): ", phosg::name_for_enum(WhenAllCards), phosg::name_for_enum(WhenAttackerAndActionCards), phosg::name_for_enum(WhenAttackerOrHunterSCCard), phosg::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); diff --git a/src/Episode3/DataIndexes.cc b/src/Episode3/DataIndexes.cc index 7623d8b8..b343f057 100644 --- a/src/Episode3/DataIndexes.cc +++ b/src/Episode3/DataIndexes.cc @@ -3,7 +3,9 @@ #include #include +#include #include +#include #include #include #include @@ -110,7 +112,7 @@ bool Location::operator!=(const Location& other) const { } std::string Location::str() const { - return phosg::string_printf("Location[x=%hhu, y=%hhu, dir=%hhu:%s, u=%hhu]", + return std::format("Location[x={}, y={}, dir={}:{}, u={}]", this->x, this->y, static_cast(this->direction), phosg::name_for_enum(this->direction), this->unused); } @@ -494,13 +496,13 @@ string CardDefinition::Stat::str() const { case Type::BLANK: return "(blank)"; case Type::STAT: - return phosg::string_printf("%hhd", this->stat); + return std::format("{}", this->stat); case Type::PLUS_STAT: - return phosg::string_printf("+%hhd", this->stat); + return std::format("+{}", this->stat); case Type::MINUS_STAT: - return phosg::string_printf("-%d", -this->stat); + return std::format("-{}", -this->stat); case Type::EQUALS_STAT: - return phosg::string_printf("=%hhd", this->stat); + return std::format("={}", this->stat); case Type::UNKNOWN: return "?"; case Type::PLUS_UNKNOWN: @@ -510,7 +512,7 @@ string CardDefinition::Stat::str() const { case Type::EQUALS_UNKNOWN: return "=?"; default: - return phosg::string_printf("[%02hhX %02hhX]", this->type, this->stat); + return std::format("[{:02X} {:02X}]", static_cast(this->type), this->stat); } } @@ -542,44 +544,44 @@ string CardDefinition::Effect::str_for_arg(const string& arg) { switch (arg[0]) { case 'a': - return phosg::string_printf("%s (Each activation lasts for %zu attack%s)", arg.c_str(), value, (value == 1) ? "" : "s"); + return std::format("{} (Each activation lasts for {} attack{})", arg, value, (value == 1) ? "" : "s"); case 'C': case 'c': - return phosg::string_printf("%s (Req. linked item (%zu=>%zu))", arg.c_str(), value / 10, value % 10); + return std::format("{} (Req. linked item ({}=>{}))", arg, value / 10, value % 10); case 'd': - return phosg::string_printf("%s (Req. die roll in [%zu, %zu])", arg.c_str(), value / 10, value % 10); + return std::format("{} (Req. die roll in [{}, {}])", arg, value / 10, value % 10); case 'e': return arg + " (While equipped)"; case 'h': - return phosg::string_printf("%s (Req. HP >= %zu)", arg.c_str(), value); + return std::format("{} (Req. HP >= {})", arg, value); case 'i': - return phosg::string_printf("%s (Req. HP <= %zu)", arg.c_str(), value); + return std::format("{} (Req. HP <= {})", arg, value); case 'n': try { - return phosg::string_printf("%s (Req. condition: %s)", arg.c_str(), description_for_n_condition.at(value)); + return std::format("{} (Req. condition: {})", arg, description_for_n_condition.at(value)); } catch (const out_of_range&) { return arg + " (Req. condition: unknown)"; } case 'o': { const char* suffix = ((value / 10) == 1) ? " on opponent card" : " on self"; if (value == 0) { - return phosg::string_printf("%s (Req. any previous effect%s)", arg.c_str(), suffix); + return std::format("{} (Req. any previous effect{})", arg, suffix); } else { - return phosg::string_printf("%s (Req. effect %zu passed%s)", arg.c_str(), static_cast(value % 10), suffix); + return std::format("{} (Req. effect {} passed{})", arg, static_cast(value % 10), suffix); } } case 'p': try { - return phosg::string_printf("%s (Target: %s)", arg.c_str(), description_for_p_target.at(value)); + return std::format("{} (Target: {})", arg, description_for_p_target.at(value)); } catch (const out_of_range&) { return arg + " (Target: unknown)"; } case 'r': - return phosg::string_printf("%s (Random with %zu%% chance)", arg.c_str(), value == 0 ? 100 : value); + return std::format("{} (Random with {}% chance)", arg, value == 0 ? 100 : value); case 's': - return phosg::string_printf("%s (Req. cost in [%zu, %zu])", arg.c_str(), value / 10, value % 10); + return std::format("{} (Req. cost in [{}, {}])", arg, value / 10, value % 10); case 't': - return phosg::string_printf("%s (Turns: %zu)", arg.c_str(), value); + return std::format("{} (Turns: {})", arg, value); default: return arg + " (unknown)"; } @@ -587,10 +589,10 @@ string CardDefinition::Effect::str_for_arg(const string& arg) { string CardDefinition::Effect::str(const char* separator, const TextSet* text_archive) const { vector tokens; - tokens.emplace_back(phosg::string_printf("%hhu:", this->effect_num)); + tokens.emplace_back(std::format("{}:", this->effect_num)); { uint8_t type = static_cast(this->type); - string cmd_str = phosg::string_printf("cmd=%02hhX", type); + string cmd_str = std::format("cmd={:02X}", type); try { const char* name = description_for_condition_type.at(type).name; if (name) { @@ -604,13 +606,13 @@ string CardDefinition::Effect::str(const char* separator, const TextSet* text_ar if (!this->expr.empty()) { tokens.emplace_back("expr=" + this->expr.decode()); } - tokens.emplace_back(phosg::string_printf("when=%02hhX:%s", static_cast(this->when), phosg::name_for_enum(this->when))); + tokens.emplace_back(std::format("when={:02X}:{}", static_cast(this->when), phosg::name_for_enum(this->when))); 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("arg3=" + this->str_for_arg(this->arg3.decode())); { uint8_t type = static_cast(this->apply_criterion); - string cond_str = phosg::string_printf("cond=%02hhX", type); + string cond_str = std::format("cond={:02X}", type); try { const char* name = phosg::name_for_enum(this->apply_criterion); cond_str += ':'; @@ -634,9 +636,9 @@ string CardDefinition::Effect::str(const char* separator, const TextSet* text_ar ch = '$'; } } - tokens.emplace_back(phosg::string_printf("name=%02hhX \"%s\"", this->name_index, formatted_name.c_str())); + tokens.emplace_back(std::format("name={:02X} \"{}\"", this->name_index, formatted_name)); } else { - tokens.emplace_back(phosg::string_printf("name=%02hhX", this->name_index)); + tokens.emplace_back(std::format("name={:02X}", this->name_index)); } return phosg::join(tokens, separator); @@ -733,7 +735,7 @@ string name_for_rank(CardRank rank) { try { return names.at(static_cast(rank) - 1); } catch (const out_of_range&) { - return phosg::string_printf("(%02hhX)", static_cast(rank)); + return std::format("({:02X})", static_cast(rank)); } } @@ -767,7 +769,7 @@ string string_for_colors(const parray& colors) { try { ret += name_for_link_color(colors[x]); } catch (const invalid_argument&) { - ret += phosg::string_printf("%02hhX", colors[x]); + ret += std::format("{:02X}", colors[x]); } } } @@ -783,16 +785,16 @@ string string_for_assist_turns(uint8_t turns) { } else if (turns == 99) { return "FOREVER"; } else { - return phosg::string_printf("%hhu", turns); + return std::format("{}", turns); } } string string_for_range(const parray& range) { string ret; for (size_t x = 0; x < 6; x++) { - ret += phosg::string_printf("%05" PRIX32 "/", range[x].load()); + ret += std::format("{:05X}/", range[x]); } - while (phosg::starts_with(ret, "00000/")) { + while (ret.starts_with("00000/")) { ret = ret.substr(6); } if (!ret.empty()) { @@ -830,11 +832,11 @@ string string_for_drop_rate(uint16_t drop_rate) { } uint8_t environment_number = (drop_rate / 10) % 100; if (environment_number) { - tokens.emplace_back(phosg::string_printf("environment_number=%02hhX", static_cast(environment_number - 1))); + tokens.emplace_back(std::format("environment_number={:02X}", static_cast(environment_number - 1))); } else { tokens.emplace_back("environment_number=ANY"); } - tokens.emplace_back(phosg::string_printf("rarity_class=%hhu", static_cast((drop_rate / 1000) % 10))); + tokens.emplace_back(std::format("rarity_class={}", static_cast((drop_rate / 1000) % 10))); switch ((drop_rate / 10000) % 10) { case 0: tokens.emplace_back("deck_type=ANY"); @@ -849,7 +851,7 @@ string string_for_drop_rate(uint16_t drop_rate) { tokens.emplace_back("deck_type=__UNKNOWN__"); } string description = phosg::join(tokens, ", "); - return phosg::string_printf("[%hu: %s]", drop_rate, description.c_str()); + return std::format("[{}: {}]", drop_rate, description); } static const char* short_name_for_assist_ai_param_target(uint8_t target) { @@ -915,49 +917,49 @@ string CardDefinition::str(bool single_line, const TextSet* text_archive) const string drop0_str = string_for_drop_rate(this->drop_rates[0]); string drop1_str = string_for_drop_rate(this->drop_rates[1]); - string cost_str = phosg::string_printf("%hhX", this->self_cost); + string cost_str = std::format("{:X}", this->self_cost); if (this->ally_cost) { if (single_line) { - cost_str += phosg::string_printf("+%hhX", this->ally_cost); + cost_str += std::format("+{:X}", this->ally_cost); } else { - cost_str += phosg::string_printf(" (self) + %hhX (ally)", this->ally_cost); + cost_str += std::format(" (self) + {:X} (ally)", this->ally_cost); } } string en_name_s = this->en_name.decode(); if (single_line) { string range_str = string_for_range(this->range); - return phosg::string_printf( - "[Card: %04" PRIX32 " name=%s type=%s usable_condition=%s rank=%s " - "cost=%s target=%s range=%s assist_turns=%s cannot_move=%s " - "cannot_attack=%s cannot_drop=%s hp=%s ap=%s tp=%s mv=%s left=%s right=%s " - "top=%s class=%s assist_ai_params=[target=%s priority=%hhu effect=%hhu] drop_rates=[%s, %s] effects=[%s]]", - this->card_id.load(), - en_name_s.c_str(), - type_str.c_str(), - criterion_str.c_str(), - rank_str.c_str(), - cost_str.c_str(), + return std::format( + "[Card: {:04X} name={} type={} usable_condition={} rank={} " + "cost={} target={} range={} assist_turns={} cannot_move={} " + "cannot_attack={} cannot_drop={} hp={} ap={} tp={} mv={} left={} right={} " + "top={} class={} assist_ai_params=[target={} priority={} effect={}] drop_rates=[{}, {}] effects=[{}]]", + this->card_id, + en_name_s, + type_str, + criterion_str, + rank_str, + cost_str, target_mode_str, - range_str.c_str(), - assist_turns_str.c_str(), + range_str, + assist_turns_str, this->cannot_move ? "true" : "false", this->cannot_attack ? "true" : "false", this->cannot_drop ? "true" : "false", - hp_str.c_str(), - ap_str.c_str(), - tp_str.c_str(), - mv_str.c_str(), - left_str.c_str(), - right_str.c_str(), - top_str.c_str(), - card_class_str.c_str(), + hp_str, + ap_str, + tp_str, + mv_str, + left_str, + right_str, + top_str, + card_class_str, short_name_for_assist_ai_param_target((this->assist_ai_params / 1000) % 10), static_cast((this->assist_ai_params / 100) % 10), static_cast(this->assist_ai_params % 100), - drop0_str.c_str(), - drop1_str.c_str(), - effects_str.c_str()); + drop0_str, + drop1_str, + effects_str); } else { // Not single-line string range_str; @@ -981,65 +983,65 @@ string CardDefinition::str(bool single_line, const TextSet* text_archive) const string jp_name_short_s = this->jp_short_name.decode(); string names_str; if (!en_name_s.empty()) { - names_str += phosg::string_printf(" EN: \"%s\"", en_name_s.c_str()); + names_str += std::format(" EN: \"{}\"", en_name_s); if (!en_name_short_s.empty() && en_name_short_s != en_name_s) { - names_str += phosg::string_printf(" (Abr. \"%s\")", en_name_short_s.c_str()); + names_str += std::format(" (Abr. \"{}\")", en_name_short_s); } } if (!jp_name_s.empty()) { - names_str += phosg::string_printf(" JP: \"%s\"", jp_name_s.c_str()); + names_str += std::format(" JP: \"{}\"", jp_name_s); if (!jp_name_short_s.empty() && jp_name_short_s != jp_name_s) { - names_str += phosg::string_printf(" (Abr. \"%s\")", jp_name_short_s.c_str()); + names_str += std::format(" (Abr. \"{}\")", jp_name_short_s); } } - return phosg::string_printf( + return std::format( "\ -Card: %04" PRIX32 "%s\n\ - Type: %s, class: %s\n\ - Usability condition: %s\n\ - Rank: %s\n\ - Cost: %s\n\ - Target mode: %s\n\ - Range:%s\n\ - Assist turns: %s\n\ - Capabilities: %s move, %s attack\n\ - HP: %s, AP: %s, TP: %s, MV: %s\n\ +Card: {:04X}{}\n\ + Type: {}, class: {}\n\ + Usability condition: {}\n\ + Rank: {}\n\ + Cost: {}\n\ + Target mode: {}\n\ + Range:{}\n\ + Assist turns: {}\n\ + Capabilities: {} move, {} attack\n\ + HP: {}, AP: {}, TP: {}, MV: {}\n\ Colors:\n\ - Left: %s\n\ - Right: %s\n\ - Top: %s\n\ - Assist AI parameters: [target %s, priority %hu, effect %hu]\n\ + Left: {}\n\ + Right: {}\n\ + Top: {}\n\ + Assist AI parameters: [target {}, priority {}, effect {}]\n\ Drop rates:\n\ - %s\n\ - %s\n\ - %s\n\ - Effects:%s", - this->card_id.load(), - names_str.c_str(), - type_str.c_str(), - card_class_str.c_str(), - criterion_str.c_str(), - rank_str.c_str(), - cost_str.c_str(), + {}\n\ + {}\n\ + {}\n\ + Effects:{}", + this->card_id, + names_str, + type_str, + card_class_str, + criterion_str, + rank_str, + cost_str, target_mode_str, - range_str.c_str(), - assist_turns_str.c_str(), + range_str, + assist_turns_str, this->cannot_move ? "cannot" : "can", this->cannot_attack ? "cannot" : "can", - hp_str.c_str(), - ap_str.c_str(), - tp_str.c_str(), - mv_str.c_str(), - left_str.c_str(), - right_str.c_str(), - top_str.c_str(), + hp_str, + ap_str, + tp_str, + mv_str, + left_str, + right_str, + top_str, name_for_assist_ai_param_target((this->assist_ai_params / 1000) % 10), static_cast((this->assist_ai_params / 100) % 10), static_cast(this->assist_ai_params % 100), - drop0_str.c_str(), - drop1_str.c_str(), + drop0_str, + drop1_str, this->cannot_drop ? "Forbidden" : "Permitted", - effects_str.c_str()); + effects_str); } } @@ -1235,51 +1237,51 @@ PlayerConfigNTE::operator PlayerConfig() const { Rules::Rules(const phosg::JSON& json) { this->clear(); - this->overall_time_limit = json.get_int("overall_time_limit", this->overall_time_limit); - this->phase_time_limit = json.get_int("phase_time_limit", this->phase_time_limit); - this->allowed_cards = json.get_enum("allowed_cards", this->allowed_cards); - this->min_dice_value = json.get_int("min_dice", this->min_dice_value); - this->max_dice_value = json.get_int("max_dice", this->max_dice_value); - this->disable_deck_shuffle = json.get_bool("disable_deck_shuffle", this->disable_deck_shuffle); - this->disable_deck_loop = json.get_bool("disable_deck_loop", this->disable_deck_loop); - this->char_hp = json.get_int("char_hp", this->char_hp); - this->hp_type = json.get_enum("hp_type", this->hp_type); - this->no_assist_cards = json.get_bool("no_assist_cards", this->no_assist_cards); - this->disable_dialogue = json.get_bool("disable_dialogue", this->disable_dialogue); - this->dice_exchange_mode = json.get_enum("dice_exchange_mode", this->dice_exchange_mode); - this->disable_dice_boost = json.get_bool("disable_dice_boost", this->disable_dice_boost); - uint8_t min_dice = json.get_int("min_def_dice", (this->def_dice_value_range >> 4) & 0x0F); - uint8_t max_dice = json.get_int("max_def_dice", this->def_dice_value_range & 0x0F); + this->overall_time_limit = json.get_int("OverallTimeLimit", json.get_int("overall_time_limit", this->overall_time_limit)); + this->phase_time_limit = json.get_int("PhaseTimeLimit", json.get_int("phase_time_limit", this->phase_time_limit)); + this->allowed_cards = json.get_enum("AllowedCards", json.get_enum("allowed_cards", this->allowed_cards)); + this->min_dice_value = json.get_int("MinDice", json.get_int("min_dice", this->min_dice_value)); + this->max_dice_value = json.get_int("MaxDice", json.get_int("max_dice", this->max_dice_value)); + this->disable_deck_shuffle = json.get_bool("DisableDeckShuffle", json.get_bool("disable_deck_shuffle", this->disable_deck_shuffle)); + this->disable_deck_loop = json.get_bool("DisableDeckLoop", json.get_bool("disable_deck_loop", this->disable_deck_loop)); + this->char_hp = json.get_int("CharHP", json.get_int("char_hp", this->char_hp)); + this->hp_type = json.get_enum("HPType", json.get_enum("hp_type", this->hp_type)); + this->no_assist_cards = json.get_bool("NoAssistCards", json.get_bool("no_assist_cards", this->no_assist_cards)); + this->disable_dialogue = json.get_bool("DisableDialogue", json.get_bool("disable_dialogue", this->disable_dialogue)); + this->dice_exchange_mode = json.get_enum("DiceExchangeMode", json.get_enum("dice_exchange_mode", this->dice_exchange_mode)); + this->disable_dice_boost = json.get_bool("DisableDiceBoost", json.get_bool("disable_dice_boost", this->disable_dice_boost)); + uint8_t min_dice = json.get_int("MinDEFDice", json.get_int("min_def_dice", (this->def_dice_value_range >> 4) & 0x0F)); + uint8_t max_dice = json.get_int("MaxDEFDice", json.get_int("max_def_dice", this->def_dice_value_range & 0x0F)); this->def_dice_value_range = ((min_dice << 4) & 0xF0) | (max_dice & 0x0F); - min_dice = json.get_int("min_atk_dice_2v1", (this->atk_dice_value_range_2v1 >> 4) & 0x0F); - max_dice = json.get_int("max_atk_dice_2v1", this->atk_dice_value_range_2v1 & 0x0F); + min_dice = json.get_int("MinATKDice2v1", json.get_int("min_atk_dice_2v1", (this->atk_dice_value_range_2v1 >> 4) & 0x0F)); + max_dice = json.get_int("MaxATKDice2v1", json.get_int("max_atk_dice_2v1", this->atk_dice_value_range_2v1 & 0x0F)); this->atk_dice_value_range_2v1 = ((min_dice << 4) & 0xF0) | (max_dice & 0x0F); - min_dice = json.get_int("min_def_dice_2v1", (this->def_dice_value_range_2v1 >> 4) & 0x0F); - max_dice = json.get_int("max_def_dice_2v1", this->def_dice_value_range_2v1 & 0x0F); + min_dice = json.get_int("MinDEFDice2v1", json.get_int("min_def_dice_2v1", (this->def_dice_value_range_2v1 >> 4) & 0x0F)); + max_dice = json.get_int("MaxDEFDice2v1", json.get_int("max_def_dice_2v1", this->def_dice_value_range_2v1 & 0x0F)); this->def_dice_value_range_2v1 = ((min_dice << 4) & 0xF0) | (max_dice & 0x0F); } phosg::JSON Rules::json() const { return phosg::JSON::dict({ - {"overall_time_limit", this->overall_time_limit}, - {"phase_time_limit", this->phase_time_limit}, - {"allowed_cards", phosg::name_for_enum(this->allowed_cards)}, - {"min_dice", this->min_dice_value}, - {"max_dice", this->max_dice_value}, - {"disable_deck_shuffle", static_cast(this->disable_deck_shuffle)}, - {"disable_deck_loop", static_cast(this->disable_deck_loop)}, - {"char_hp", this->char_hp}, - {"hp_type", phosg::name_for_enum(this->hp_type)}, - {"no_assist_cards", static_cast(this->no_assist_cards)}, - {"disable_dialogue", static_cast(this->disable_dialogue)}, - {"dice_exchange_mode", phosg::name_for_enum(this->dice_exchange_mode)}, - {"disable_dice_boost", static_cast(this->disable_dice_boost)}, - {"min_def_dice", ((this->def_dice_value_range >> 4) & 0x0F)}, - {"max_def_dice", (this->def_dice_value_range & 0x0F)}, - {"min_atk_dice_2v1", ((this->atk_dice_value_range_2v1 >> 4) & 0x0F)}, - {"max_atk_dice_2v1", (this->atk_dice_value_range_2v1 & 0x0F)}, - {"min_def_dice_2v1", ((this->def_dice_value_range_2v1 >> 4) & 0x0F)}, - {"max_def_dice_2v1", (this->def_dice_value_range_2v1 & 0x0F)}, + {"OverallTimeLimit", this->overall_time_limit}, + {"PhaseTimeLimit", this->phase_time_limit}, + {"AllowedCards", phosg::name_for_enum(this->allowed_cards)}, + {"MinDice", this->min_dice_value}, + {"MaxDice", this->max_dice_value}, + {"DisableDeckShuffle", static_cast(this->disable_deck_shuffle)}, + {"DisableDeckLoop", static_cast(this->disable_deck_loop)}, + {"CharHP", this->char_hp}, + {"HPType", phosg::name_for_enum(this->hp_type)}, + {"NoAssistCards", static_cast(this->no_assist_cards)}, + {"DisableDialogue", static_cast(this->disable_dialogue)}, + {"DiceExchangeMode", phosg::name_for_enum(this->dice_exchange_mode)}, + {"DisableDiceBoost", static_cast(this->disable_dice_boost)}, + {"MinDEFDice", ((this->def_dice_value_range >> 4) & 0x0F)}, + {"MaxDEFDice", (this->def_dice_value_range & 0x0F)}, + {"MinATKDice2v1", ((this->atk_dice_value_range_2v1 >> 4) & 0x0F)}, + {"MaxATKDice2v1", (this->atk_dice_value_range_2v1 & 0x0F)}, + {"MinDEFDice2v1", ((this->def_dice_value_range_2v1 >> 4) & 0x0F)}, + {"MaxDEFDice2v1", (this->def_dice_value_range_2v1 & 0x0F)}, }); } @@ -1358,7 +1360,7 @@ string Rules::str() const { if (this->char_hp == 0xFF) { tokens.emplace_back("char_hp=(open)"); } else { - tokens.emplace_back(phosg::string_printf("char_hp=%hhu", this->char_hp)); + tokens.emplace_back(std::format("char_hp={}", this->char_hp)); } switch (this->hp_type) { @@ -1375,7 +1377,7 @@ string Rules::str() const { if (static_cast(this->hp_type) == 0xFF) { tokens.emplace_back("hp_type=(open)"); } else { - tokens.emplace_back(phosg::string_printf("hp_type=(%02hhX)", + tokens.emplace_back(std::format("hp_type=({:02X})", static_cast(this->hp_type))); } break; @@ -1388,14 +1390,14 @@ string Rules::str() const { } else if (range.first == 0x00) { s += "min=(default), "; } else { - s += phosg::string_printf("min=%hhu, ", range.first); + s += std::format("min={}, ", range.first); } if (range.second == 0xFF) { s += "max=(open)]"; } else if (range.second == 0x00) { s += "max=(default)]"; } else { - s += phosg::string_printf("max=%hhu]", range.second); + s += std::format("max={}]", range.second); } return s; }; @@ -1424,7 +1426,7 @@ string Rules::str() const { if (static_cast(this->dice_exchange_mode) == 0xFF) { tokens.emplace_back("dice_exchange=(open)"); } else { - tokens.emplace_back(phosg::string_printf("dice_exchange=(%02hhX)", + tokens.emplace_back(std::format("dice_exchange=({:02X})", static_cast(this->dice_exchange_mode))); } break; @@ -1439,7 +1441,7 @@ string Rules::str() const { case 0xFF: return "(open)"; default: - return phosg::string_printf("(%02hhX)", v); + return std::format("({:02X})", v); } }; @@ -1464,7 +1466,7 @@ string Rules::str() const { if (static_cast(this->allowed_cards) == 0xFF) { tokens.emplace_back("allowed_cards=(open)"); } else { - tokens.emplace_back(phosg::string_printf("allowed_cards=(%02hhX)", + tokens.emplace_back(std::format("allowed_cards=({:02X})", static_cast(this->allowed_cards))); } break; @@ -1474,14 +1476,14 @@ string Rules::str() const { if (this->overall_time_limit == 0xFF) { tokens.emplace_back("overall_time_limit=(open)"); } else if (this->overall_time_limit) { - tokens.emplace_back(phosg::string_printf("overall_time_limit=%zumin", static_cast(this->overall_time_limit * 5))); + tokens.emplace_back(std::format("overall_time_limit={}min", static_cast(this->overall_time_limit * 5))); } else { tokens.emplace_back("overall_time_limit=(infinite)"); } if (this->phase_time_limit == 0xFF) { tokens.emplace_back("phase_time_limit=(open)"); } else if (this->phase_time_limit) { - tokens.emplace_back(phosg::string_printf("phase_time_limit=%hhusec", this->phase_time_limit)); + tokens.emplace_back(std::format("phase_time_limit={}sec", this->phase_time_limit)); } else { tokens.emplace_back("phase_time_limit=(infinite)"); } @@ -1790,17 +1792,17 @@ phosg::JSON MapDefinition::EntryState::json() const { // phosg::JSON MapDefinition::json() const { ... } string MapDefinition::CameraSpec::str() const { - return phosg::string_printf( - "CameraSpec[a1=(%g %g %g %g %g %g %g %g %g) camera=(%g %g %g) focus=(%g %g %g) a2=(%g %g %g)]", - this->unknown_a1[0].load(), this->unknown_a1[1].load(), - this->unknown_a1[2].load(), this->unknown_a1[3].load(), - this->unknown_a1[4].load(), this->unknown_a1[5].load(), - this->unknown_a1[6].load(), this->unknown_a1[7].load(), - this->unknown_a1[8].load(), this->camera_x.load(), - this->camera_y.load(), this->camera_z.load(), - this->focus_x.load(), this->focus_y.load(), - this->focus_z.load(), this->unknown_a2[0].load(), - this->unknown_a2[1].load(), this->unknown_a2[2].load()); + return std::format( + "CameraSpec[a1=({:g} {:g} {:g} {:g} {:g} {:g} {:g} {:g} {:g}) camera=({:g} {:g} {:g}) focus=({:g} {:g} {:g}) a2=({:g} {:g} {:g})]", + this->unknown_a1[0], this->unknown_a1[1], + this->unknown_a1[2], this->unknown_a1[3], + this->unknown_a1[4], this->unknown_a1[5], + this->unknown_a1[6], this->unknown_a1[7], + this->unknown_a1[8], this->camera_x, + this->camera_y, this->camera_z, + this->focus_x, this->focus_y, + this->focus_z, this->unknown_a2[0], + this->unknown_a2[1], this->unknown_a2[2]); } string MapDefinition::str(const CardIndex* card_index, uint8_t language) const { @@ -1809,21 +1811,21 @@ string MapDefinition::str(const CardIndex* card_index, uint8_t language) const { for (size_t y = 0; y < this->height; y++) { string line = " "; for (size_t x = 0; x < this->width; x++) { - line += phosg::string_printf(" %02hhX", tiles[y][x]); + line += std::format(" {:02X}", tiles[y][x]); } lines.emplace_back(std::move(line)); } }; - lines.emplace_back(phosg::string_printf("Map %08" PRIX32 ": %hhux%hhu", - this->map_number.load(), this->width, this->height)); - lines.emplace_back(phosg::string_printf(" tag: %08" PRIX32, this->tag.load())); - lines.emplace_back(phosg::string_printf(" environment_number: %02hhX (%s)", this->environment_number, name_for_environment_number(this->environment_number))); - lines.emplace_back(phosg::string_printf(" num_camera_zones: %02hhX", this->num_camera_zones)); + lines.emplace_back(std::format("Map {:08X}: {}x{}", + this->map_number, this->width, this->height)); + lines.emplace_back(std::format(" tag: {:08X}", this->tag)); + lines.emplace_back(std::format(" environment_number: {:02X} ({})", this->environment_number, name_for_environment_number(this->environment_number))); + lines.emplace_back(std::format(" num_camera_zones: {:02X}", this->num_camera_zones)); lines.emplace_back(" tiles:"); add_map(this->map_tiles); - lines.emplace_back(phosg::string_printf( - " start_tile_definitions: A:[1p: %02hhX; 2p: %02hhX,%02hhX; 3p: %02hhX,%02hhX,%02hhX], B:[1p: %02hhX; 2p: %02hhX,%02hhX; 3p: %02hhX,%02hhX,%02hhX]", + lines.emplace_back(std::format( + " start_tile_definitions: A:[1p: {:02X}; 2p: {:02X},{:02X}; 3p: {:02X},{:02X},{:02X}], B:[1p: {:02X}; 2p: {:02X},{:02X}; 3p: {:02X},{:02X},{:02X}]", this->start_tile_definitions[0][0], this->start_tile_definitions[0][1], this->start_tile_definitions[0][2], this->start_tile_definitions[0][3], this->start_tile_definitions[0][4], this->start_tile_definitions[0][5], @@ -1832,7 +1834,7 @@ string MapDefinition::str(const CardIndex* card_index, uint8_t language) const { this->start_tile_definitions[1][4], this->start_tile_definitions[1][5])); for (size_t z = 0; z < this->num_camera_zones; z++) { for (size_t w = 0; w < 2; w++) { - lines.emplace_back(phosg::string_printf(" camera zone %zu (team %c):", z, w ? 'A' : 'B')); + lines.emplace_back(std::format(" camera zone {} (team {}):", z, w ? 'A' : 'B')); add_map(this->camera_zone_maps[w][z]); lines.emplace_back(" " + this->camera_zone_specs[w][z].str()); } @@ -1840,84 +1842,84 @@ string MapDefinition::str(const CardIndex* card_index, uint8_t language) const { for (size_t w = 0; w < 3; w++) { for (size_t z = 0; z < 2; z++) { string spec_str = this->overview_specs[w][z].str(); - lines.emplace_back(phosg::string_printf(" overview_specs[%zu][team %zu]: %s", w, z, spec_str.c_str())); + lines.emplace_back(std::format(" overview_specs[{}][team {}]: {}", w, z, spec_str)); } } lines.emplace_back(" overlay tiles:"); add_map(this->overlay_state.tiles); - lines.emplace_back(phosg::string_printf( - " unused1: %08" PRIX32 " %08" PRIX32 " %08" PRIX32 " %08" PRIX32 " %08" PRIX32, - this->overlay_state.unused1[0].load(), - this->overlay_state.unused1[1].load(), - this->overlay_state.unused1[2].load(), - this->overlay_state.unused1[3].load(), - this->overlay_state.unused1[4].load())); - lines.emplace_back(phosg::string_printf( - " trap_tile_colors_nte: %08" PRIX32 " %08" PRIX32 " %08" PRIX32 " %08" PRIX32 " %08" PRIX32 " %08" PRIX32 " %08" PRIX32 " %08" PRIX32 " %08" PRIX32 " %08" PRIX32 " %08" PRIX32 " %08" PRIX32 " %08" PRIX32 " %08" PRIX32 " %08" PRIX32 " %08" PRIX32, - this->overlay_state.trap_tile_colors_nte[0].load(), - this->overlay_state.trap_tile_colors_nte[1].load(), - this->overlay_state.trap_tile_colors_nte[2].load(), - this->overlay_state.trap_tile_colors_nte[3].load(), - this->overlay_state.trap_tile_colors_nte[4].load(), - this->overlay_state.trap_tile_colors_nte[5].load(), - this->overlay_state.trap_tile_colors_nte[6].load(), - this->overlay_state.trap_tile_colors_nte[7].load(), - this->overlay_state.trap_tile_colors_nte[8].load(), - this->overlay_state.trap_tile_colors_nte[9].load(), - this->overlay_state.trap_tile_colors_nte[10].load(), - this->overlay_state.trap_tile_colors_nte[11].load(), - this->overlay_state.trap_tile_colors_nte[12].load(), - this->overlay_state.trap_tile_colors_nte[13].load(), - this->overlay_state.trap_tile_colors_nte[14].load(), - this->overlay_state.trap_tile_colors_nte[15].load())); - lines.emplace_back(phosg::string_printf( - " trap_card_ids_nte: #%04hX #%04hX #%04hX #%04hX #%04hX #%04hX #%04hX #%04hX #%04hX #%04hX #%04hX #%04hX #%04hX #%04hX #%04hX #%04hX", - this->overlay_state.trap_card_ids_nte[0].load(), - this->overlay_state.trap_card_ids_nte[1].load(), - this->overlay_state.trap_card_ids_nte[2].load(), - this->overlay_state.trap_card_ids_nte[3].load(), - this->overlay_state.trap_card_ids_nte[4].load(), - this->overlay_state.trap_card_ids_nte[5].load(), - this->overlay_state.trap_card_ids_nte[6].load(), - this->overlay_state.trap_card_ids_nte[7].load(), - this->overlay_state.trap_card_ids_nte[8].load(), - this->overlay_state.trap_card_ids_nte[9].load(), - this->overlay_state.trap_card_ids_nte[10].load(), - this->overlay_state.trap_card_ids_nte[11].load(), - this->overlay_state.trap_card_ids_nte[12].load(), - this->overlay_state.trap_card_ids_nte[13].load(), - this->overlay_state.trap_card_ids_nte[14].load(), - this->overlay_state.trap_card_ids_nte[15].load())); + lines.emplace_back(std::format( + " unused1: {:08X} {:08X} {:08X} {:08X} {:08X}", + this->overlay_state.unused1[0], + this->overlay_state.unused1[1], + this->overlay_state.unused1[2], + this->overlay_state.unused1[3], + this->overlay_state.unused1[4])); + lines.emplace_back(std::format( + " trap_tile_colors_nte: {:08X} {:08X} {:08X} {:08X} {:08X} {:08X} {:08X} {:08X} {:08X} {:08X} {:08X} {:08X} {:08X} {:08X} {:08X} {:08X}", + this->overlay_state.trap_tile_colors_nte[0], + this->overlay_state.trap_tile_colors_nte[1], + this->overlay_state.trap_tile_colors_nte[2], + this->overlay_state.trap_tile_colors_nte[3], + this->overlay_state.trap_tile_colors_nte[4], + this->overlay_state.trap_tile_colors_nte[5], + this->overlay_state.trap_tile_colors_nte[6], + this->overlay_state.trap_tile_colors_nte[7], + this->overlay_state.trap_tile_colors_nte[8], + this->overlay_state.trap_tile_colors_nte[9], + this->overlay_state.trap_tile_colors_nte[10], + this->overlay_state.trap_tile_colors_nte[11], + this->overlay_state.trap_tile_colors_nte[12], + this->overlay_state.trap_tile_colors_nte[13], + this->overlay_state.trap_tile_colors_nte[14], + this->overlay_state.trap_tile_colors_nte[15])); + lines.emplace_back(std::format( + " trap_card_ids_nte: #{:04X} #{:04X} #{:04X} #{:04X} #{:04X} #{:04X} #{:04X} #{:04X} #{:04X} #{:04X} #{:04X} #{:04X} #{:04X} #{:04X} #{:04X} #{:04X}", + this->overlay_state.trap_card_ids_nte[0], + this->overlay_state.trap_card_ids_nte[1], + this->overlay_state.trap_card_ids_nte[2], + this->overlay_state.trap_card_ids_nte[3], + this->overlay_state.trap_card_ids_nte[4], + this->overlay_state.trap_card_ids_nte[5], + this->overlay_state.trap_card_ids_nte[6], + this->overlay_state.trap_card_ids_nte[7], + this->overlay_state.trap_card_ids_nte[8], + this->overlay_state.trap_card_ids_nte[9], + this->overlay_state.trap_card_ids_nte[10], + this->overlay_state.trap_card_ids_nte[11], + this->overlay_state.trap_card_ids_nte[12], + this->overlay_state.trap_card_ids_nte[13], + this->overlay_state.trap_card_ids_nte[14], + this->overlay_state.trap_card_ids_nte[15])); lines.emplace_back(" default_rules: " + this->default_rules.str()); lines.emplace_back(" name: " + this->name.decode(language)); lines.emplace_back(" location_name: " + this->location_name.decode(language)); lines.emplace_back(" quest_name: " + this->quest_name.decode(language)); lines.emplace_back(" description: " + this->description.decode(language)); - lines.emplace_back(phosg::string_printf(" map_xy: %hu %hu", this->map_x.load(), this->map_y.load())); + lines.emplace_back(std::format(" map_xy: {} {}", this->map_x, this->map_y)); for (size_t z = 0; z < 3; z++) { - lines.emplace_back(phosg::string_printf(" npc_chars[%zu]:", z)); + lines.emplace_back(std::format(" npc_chars[{}]:", z)); lines.emplace_back(" name: " + this->npc_ai_params[z].ai_name.decode(language)); - lines.emplace_back(phosg::string_printf( - " ai_params: (a1: %04hX %04hX, is_arkz: %02hhX, a2: %02hX %02hX %02hX)", - this->npc_ai_params[z].unknown_a1[0].load(), this->npc_ai_params[z].unknown_a1[1].load(), + lines.emplace_back(std::format( + " ai_params: (a1: {:04X} {:04X}, is_arkz: {:02X}, a2: {:02X} {:02X} {:02X})", + this->npc_ai_params[z].unknown_a1[0], this->npc_ai_params[z].unknown_a1[1], this->npc_ai_params[z].is_arkz, this->npc_ai_params[z].unknown_a2[0], this->npc_ai_params[z].unknown_a2[1], this->npc_ai_params[z].unknown_a2[2])); for (size_t w = 0; w < 0x78; w += 0x08) { - lines.emplace_back(phosg::string_printf( - " ai_params.a3[0x%02zX:0x%02zX]: %04hX %04hX %04hX %04hX %04hX %04hX %04hX %04hX", + lines.emplace_back(std::format( + " ai_params.a3[0x{:02X}:0x{:02X}]: {:04X} {:04X} {:04X} {:04X} {:04X} {:04X} {:04X} {:04X}", w, w + 0x08, - this->npc_ai_params[z].params[w + 0x00].load(), this->npc_ai_params[z].params[w + 0x01].load(), - this->npc_ai_params[z].params[w + 0x02].load(), this->npc_ai_params[z].params[w + 0x03].load(), - this->npc_ai_params[z].params[w + 0x04].load(), this->npc_ai_params[z].params[w + 0x05].load(), - this->npc_ai_params[z].params[w + 0x06].load(), this->npc_ai_params[z].params[w + 0x07].load())); + this->npc_ai_params[z].params[w + 0x00], this->npc_ai_params[z].params[w + 0x01], + this->npc_ai_params[z].params[w + 0x02], this->npc_ai_params[z].params[w + 0x03], + this->npc_ai_params[z].params[w + 0x04], this->npc_ai_params[z].params[w + 0x05], + this->npc_ai_params[z].params[w + 0x06], this->npc_ai_params[z].params[w + 0x07])); } - lines.emplace_back(phosg::string_printf( - " ai_params.a3[0x78:0x7E]: %04hX %04hX %04hX %04hX %04hX %04hX", - this->npc_ai_params[z].params[0x78].load(), this->npc_ai_params[z].params[0x79].load(), - this->npc_ai_params[z].params[0x7A].load(), this->npc_ai_params[z].params[0x7B].load(), - this->npc_ai_params[z].params[0x7C].load(), this->npc_ai_params[z].params[0x7D].load())); - lines.emplace_back(phosg::string_printf(" npc_decks[%zu]:", z)); + lines.emplace_back(std::format( + " ai_params.a3[0x78:0x7E]: {:04X} {:04X} {:04X} {:04X} {:04X} {:04X}", + this->npc_ai_params[z].params[0x78], this->npc_ai_params[z].params[0x79], + this->npc_ai_params[z].params[0x7A], this->npc_ai_params[z].params[0x7B], + this->npc_ai_params[z].params[0x7C], this->npc_ai_params[z].params[0x7D])); + lines.emplace_back(std::format(" npc_decks[{}]:", z)); lines.emplace_back(" name: " + this->npc_decks[z].deck_name.decode(language)); for (size_t w = 0; w < 0x20; w++) { uint16_t card_id = this->npc_decks[z].card_ids[w]; @@ -1930,9 +1932,9 @@ string MapDefinition::str(const CardIndex* card_index, uint8_t language) const { } if (entry) { string name = entry->def.en_name.decode(language); - lines.emplace_back(phosg::string_printf(" cards[%02zu]: #%04hX (%s)", w, card_id, name.c_str())); + lines.emplace_back(std::format(" cards[{:02}]: #{:04X} ({})", w, card_id, name)); } else { - lines.emplace_back(phosg::string_printf(" cards[%02zu]: #%04hX", w, card_id)); + lines.emplace_back(std::format(" cards[{:02}]: #{:04X}", w, card_id)); } } for (size_t x = 0; x < 0x10; x++) { @@ -1940,19 +1942,19 @@ string MapDefinition::str(const CardIndex* card_index, uint8_t language) const { if (set.when == -1 && set.percent_chance == 0xFFFF) { continue; } - lines.emplace_back(phosg::string_printf(" npc_dialogue[%zu][%zu] (when: %04hX, chance: %hu%%):", - z, x, set.when.load(), set.percent_chance.load())); + lines.emplace_back(std::format(" npc_dialogue[{}][{}] (when: {:04X}, chance: {}%):", + z, x, set.when, set.percent_chance)); for (size_t w = 0; w < 4; w++) { if (!set.strings[w].empty() && set.strings[w].at(0) != 0xFF) { string s = set.strings[w].decode(language); - lines.emplace_back(phosg::string_printf(" strings[%zu]: %s", w, s.c_str())); + lines.emplace_back(std::format(" strings[{}]: {}", w, s)); } } } } lines.emplace_back(" a7: " + phosg::format_data_string(this->unknown_a7.data(), this->unknown_a7.bytes())); - lines.emplace_back(phosg::string_printf(" npc_ai_params_entry_index: [%08" PRIX32 ", %08" PRIX32 ", %08" PRIX32 "]", - this->npc_ai_params_entry_index[0].load(), this->npc_ai_params_entry_index[1].load(), this->npc_ai_params_entry_index[2].load())); + lines.emplace_back(std::format(" npc_ai_params_entry_index: [{:08X}, {:08X}, {:08X}]", + this->npc_ai_params_entry_index[0], this->npc_ai_params_entry_index[1], this->npc_ai_params_entry_index[2])); if (!this->before_message.empty()) { lines.emplace_back(" before_message: " + this->before_message.decode(language)); } @@ -1973,17 +1975,17 @@ string MapDefinition::str(const CardIndex* card_index, uint8_t language) const { } if (entry) { string name = entry->def.en_name.decode(language); - lines.emplace_back(phosg::string_printf(" reward_cards[%02zu]: #%04hX (%s)", z, card_id, name.c_str())); + lines.emplace_back(std::format(" reward_cards[{:02}]: #{:04X} ({})", z, card_id, name)); } else { - lines.emplace_back(phosg::string_printf(" reward_cards[%02zu]: #%04hX", z, card_id)); + lines.emplace_back(std::format(" reward_cards[{:02}]: #{:04X}", z, card_id)); } } - lines.emplace_back(phosg::string_printf(" level_overrides: [win: %" PRId32 ", loss: %" PRId32 "]", - this->win_level_override.load(), this->loss_level_override.load())); - lines.emplace_back(phosg::string_printf(" field_offset: (x: %hd units, y:%hd units) (x: %lg tiles, y: %lg tiles)", this->field_offset_x.load(), this->field_offset_y.load(), static_cast(this->field_offset_x) / 25.0, static_cast(this->field_offset_y) / 25.0)); - lines.emplace_back(phosg::string_printf(" map_category: %02hhX", this->map_category)); - lines.emplace_back(phosg::string_printf(" cyber_block_type: %02hhX", this->cyber_block_type)); - lines.emplace_back(phosg::string_printf(" a11: %04hX", this->unknown_a11.load())); + lines.emplace_back(std::format(" level_overrides: [win: {}, loss: {}]", + this->win_level_override, this->loss_level_override)); + lines.emplace_back(std::format(" field_offset: (x: {} units, y:{} units) (x: {:g} tiles, y: {:g} tiles)", this->field_offset_x, this->field_offset_y, static_cast(this->field_offset_x) / 25.0, static_cast(this->field_offset_y) / 25.0)); + lines.emplace_back(std::format(" map_category: {:02X}", this->map_category)); + lines.emplace_back(std::format(" cyber_block_type: {:02X}", this->cyber_block_type)); + lines.emplace_back(std::format(" a11: {:04X}", this->unknown_a11)); static const array sc_card_entry_names = { "00 (Guykild; 0005)", "01 (Kylria; 0006)", @@ -2019,7 +2021,7 @@ string MapDefinition::str(const CardIndex* card_index, uint8_t language) const { unavailable_sc_cards += ", "; } if (this->unavailable_sc_cards[z] >= sc_card_entry_names.size()) { - unavailable_sc_cards += phosg::string_printf("%04hX (invalid)", this->unavailable_sc_cards[z].load()); + unavailable_sc_cards += std::format("{:04X} (invalid)", this->unavailable_sc_cards[z]); } else { unavailable_sc_cards += sc_card_entry_names[this->unavailable_sc_cards[z]]; } @@ -2048,7 +2050,7 @@ string MapDefinition::str(const CardIndex* card_index, uint8_t language) const { player_type = "FREE"; break; default: - player_type = phosg::string_printf("(%02hhX)", this->entry_states[z].player_type); + player_type = std::format("({:02X})", this->entry_states[z].player_type); break; } string deck_type; @@ -2063,11 +2065,11 @@ string MapDefinition::str(const CardIndex* card_index, uint8_t language) const { deck_type = "any deck allowed"; break; default: - deck_type = phosg::string_printf("(%02hhX)", this->entry_states[z].deck_type); + deck_type = std::format("({:02X})", this->entry_states[z].deck_type); break; } - lines.emplace_back(phosg::string_printf( - " entry_states[%zu]: %s / %s", z, player_type.c_str(), deck_type.c_str())); + lines.emplace_back(std::format( + " entry_states[{}]: {} / {}", z, player_type, deck_type)); } return phosg::join(lines, "\n"); } @@ -2326,9 +2328,9 @@ CardIndex::CardIndex( unordered_map card_text; try { string text_bin_data; - if (!decompressed_text_filename.empty() && phosg::isfile(decompressed_text_filename)) { + if (!decompressed_text_filename.empty() && std::filesystem::is_regular_file(decompressed_text_filename)) { text_bin_data = phosg::load_file(decompressed_text_filename); - } else if (!text_filename.empty() && phosg::isfile(text_filename)) { + } else if (!text_filename.empty() && std::filesystem::is_regular_file(text_filename)) { text_bin_data = prs_decompress(phosg::load_file(text_filename)); } if (!text_bin_data.empty()) { @@ -2416,15 +2418,15 @@ CardIndex::CardIndex( } } } catch (const exception& e) { - static_game_data_log.warning("Failed to load card text: %s", e.what()); + static_game_data_log.warning_f("Failed to load card text: {}", e.what()); } unordered_map> card_dice_text; try { string text_bin_data; - if (!decompressed_dice_text_filename.empty() && phosg::isfile(decompressed_dice_text_filename)) { + if (!decompressed_dice_text_filename.empty() && std::filesystem::is_regular_file(decompressed_dice_text_filename)) { text_bin_data = phosg::load_file(decompressed_dice_text_filename); - } else if (!dice_text_filename.empty() && phosg::isfile(dice_text_filename)) { + } else if (!dice_text_filename.empty() && std::filesystem::is_regular_file(dice_text_filename)) { text_bin_data = prs_decompress(phosg::load_file(dice_text_filename)); } if (!text_bin_data.empty()) { @@ -2440,12 +2442,11 @@ CardIndex::CardIndex( } } } catch (const exception& e) { - static_game_data_log.warning("Failed to load card dice text: %s", e.what()); + static_game_data_log.warning_f("Failed to load card dice text: {}", e.what()); } try { string decompressed_data; - this->mtime_for_card_definitions = phosg::stat(filename).st_mtime; try { decompressed_data = phosg::load_file(decompressed_filename); this->compressed_card_definitions.clear(); @@ -2458,6 +2459,7 @@ CardIndex::CardIndex( if (decompressed_data.size() > 0x36EC0) { throw runtime_error("decompressed card list data is too long"); } + this->defs_hash = phosg::fnv1a64(decompressed_data); // The card definitions file is a standard REL file; the root offset points // to an ArrayRef which specifies an array of CardDefinition structs @@ -2482,8 +2484,8 @@ CardIndex::CardIndex( auto entry = make_shared(CardEntry{def, "", "", "", {}}); if (!this->card_definitions.emplace(entry->def.card_id, entry).second) { - throw runtime_error(phosg::string_printf( - "duplicate card id: %08" PRIX32, entry->def.card_id.load())); + throw runtime_error(std::format( + "duplicate card id: {:08X}", entry->def.card_id)); } // Some cards intentionally have the same name, so we just leave them @@ -2522,14 +2524,14 @@ CardIndex::CardIndex( uint64_t start = phosg::now(); this->compressed_card_definitions = prs_compress(decompressed_data); uint64_t diff = phosg::now() - start; - static_game_data_log.info( - "Compressed card definitions (%zu bytes -> %zu bytes) in %" PRIu64 "us", + static_game_data_log.info_f( + "Compressed card definitions ({} bytes -> {} bytes) in {}us", decompressed_data.size(), this->compressed_card_definitions.size(), diff); } if (this->compressed_card_definitions.size() > 0x7BF8) { // Try to reduce the compressed size by clearing out text - static_game_data_log.info("Compressed card list data is too long (0x%zX bytes); removing text", this->compressed_card_definitions.size()); + static_game_data_log.info_f("Compressed card list data is too long (0x{:X} bytes); removing text", this->compressed_card_definitions.size()); for (size_t x = 0; x < count; x++) { if (static_cast(defs[x].type) < 0) { continue; @@ -2540,8 +2542,8 @@ CardIndex::CardIndex( uint64_t start = phosg::now(); this->compressed_card_definitions = prs_compress_optimal(decompressed_data.data(), decompressed_data.size()); uint64_t diff = phosg::now() - start; - static_game_data_log.info( - "Compressed card definitions (0x%zX bytes -> 0x%zX bytes) in %" PRIu64 "us", + static_game_data_log.info_f( + "Compressed card definitions (0x{:X} bytes -> 0x{:X} bytes) in {}us", decompressed_data.size(), this->compressed_card_definitions.size(), diff); } @@ -2549,9 +2551,9 @@ CardIndex::CardIndex( throw runtime_error("compressed card list data is too long"); } - static_game_data_log.info("Indexed %zu Episode 3 card definitions", this->card_definitions.size()); + static_game_data_log.info_f("Indexed {} Episode 3 card definitions", this->card_definitions.size()); } catch (const exception& e) { - static_game_data_log.warning("Failed to load Episode 3 card update: %s", e.what()); + static_game_data_log.warning_f("Failed to load Episode 3 card update: {}", e.what()); } } @@ -2582,8 +2584,8 @@ set CardIndex::all_ids() const { return ret; } -uint64_t CardIndex::definitions_mtime() const { - return this->mtime_for_card_definitions; +uint64_t CardIndex::definitions_hash() const { + return this->defs_hash; } phosg::JSON CardIndex::definitions_json() const { @@ -2618,8 +2620,8 @@ MapIndex::VersionedMap::VersionedMap(std::string&& compressed_data, uint8_t lang } else if (decompressed.size() == sizeof(MapDefinition)) { this->map = make_shared(*reinterpret_cast(decompressed.data())); } else { - throw runtime_error(phosg::string_printf( - "decompressed data size is incorrect (expected %zu bytes, read %zu bytes)", + throw runtime_error(std::format( + "decompressed data size is incorrect (expected {} bytes, read {} bytes)", sizeof(MapDefinition), decompressed.size())); } } @@ -2689,33 +2691,34 @@ shared_ptr MapIndex::Map::version(uint8_t language } MapIndex::MapIndex(const string& directory) { - for (const auto& filename : phosg::list_directory_sorted(directory)) { + for (const auto& item : std::filesystem::directory_iterator(directory)) { + string filename = item.path().filename().string(); try { string base_filename; string compressed_data; shared_ptr decompressed_data; - if (phosg::ends_with(filename, ".mnmd") || phosg::ends_with(filename, ".bind")) { + if (filename.ends_with(".mnmd") || filename.ends_with(".bind")) { decompressed_data = make_shared(phosg::load_object_file(directory + "/" + filename)); base_filename = filename.substr(0, filename.size() - 5); - } else if (phosg::ends_with(filename, ".mnm") || phosg::ends_with(filename, ".bin")) { + } else if (filename.ends_with(".mnm") || filename.ends_with(".bin")) { compressed_data = phosg::load_file(directory + "/" + filename); base_filename = filename.substr(0, filename.size() - 4); - } else if (phosg::ends_with(filename, ".bin.gci") || phosg::ends_with(filename, ".mnm.gci")) { + } else if (filename.ends_with(".bin.gci") || filename.ends_with(".mnm.gci")) { compressed_data = decode_gci_data(phosg::load_file(directory + "/" + filename)); base_filename = filename.substr(0, filename.size() - 8); - } else if (phosg::ends_with(filename, ".gci")) { + } else if (filename.ends_with(".gci")) { compressed_data = decode_gci_data(phosg::load_file(directory + "/" + filename)); base_filename = filename.substr(0, filename.size() - 4); - } else if (phosg::ends_with(filename, ".bin.vms") || phosg::ends_with(filename, ".mnm.vms")) { + } else if (filename.ends_with(".bin.vms") || filename.ends_with(".mnm.vms")) { compressed_data = decode_vms_data(phosg::load_file(directory + "/" + filename)); base_filename = filename.substr(0, filename.size() - 8); - } else if (phosg::ends_with(filename, ".vms")) { + } else if (filename.ends_with(".vms")) { compressed_data = decode_vms_data(phosg::load_file(directory + "/" + filename)); base_filename = filename.substr(0, filename.size() - 4); - } else if (phosg::ends_with(filename, ".bin.dlq") || phosg::ends_with(filename, ".mnm.dlq")) { + } else if (filename.ends_with(".bin.dlq") || filename.ends_with(".mnm.dlq")) { compressed_data = decode_dlq_data(phosg::load_file(directory + "/" + filename)); base_filename = filename.substr(0, filename.size() - 8); - } else if (phosg::ends_with(filename, ".dlq")) { + } else if (filename.ends_with(".dlq")) { compressed_data = decode_dlq_data(phosg::load_file(directory + "/" + filename)); base_filename = filename.substr(0, filename.size() - 4); } else { @@ -2743,26 +2746,26 @@ MapIndex::MapIndex(const string& directory) { auto map_it = this->maps.find(vm->map->map_number); if (map_it == this->maps.end()) { map_it = this->maps.emplace(vm->map->map_number, make_shared(vm)).first; - static_game_data_log.info("(%s) Created Episode 3 map %08" PRIX32 " %c (%s; %s)", - filename.c_str(), - vm->map->map_number.load(), + static_game_data_log.info_f("({}) Created Episode 3 map {:08X} {} ({}; {})", + filename, + vm->map->map_number, char_for_language_code(vm->language), vm->map->is_quest() ? "quest" : "free", - name.c_str()); + name); } else { map_it->second->add_version(vm); - static_game_data_log.info("(%s) Added Episode 3 map version %08" PRIX32 " %c (%s; %s)", - filename.c_str(), - vm->map->map_number.load(), + static_game_data_log.info_f("({}) Added Episode 3 map version {:08X} {} ({}; {})", + filename, + vm->map->map_number, char_for_language_code(vm->language), vm->map->is_quest() ? "quest" : "free", - name.c_str()); + name); } this->maps_by_name.emplace(vm->map->name.decode(vm->language), map_it->second); } catch (const exception& e) { - static_game_data_log.warning("Failed to index Episode 3 map %s: %s", - filename.c_str(), e.what()); + static_game_data_log.warning_f("Failed to index Episode 3 map {}: {}", + filename, e.what()); } } } @@ -2801,7 +2804,7 @@ const string& MapIndex::get_compressed_list(size_t num_players, uint8_t language e.map_x = vm->map->map_x; e.map_y = vm->map->map_y; e.environment_number = vm->map->environment_number; - e.map_number = vm->map->map_number.load(); + e.map_number = vm->map->map_number; e.width = vm->map->width; e.height = vm->map->height; e.map_tiles = vm->map->map_tiles; @@ -2841,10 +2844,10 @@ const string& MapIndex::get_compressed_list(size_t num_players, uint8_t language compressed_w.write(prs.close()); compressed_map_list = std::move(compressed_w.str()); if (compressed_map_list.size() > 0x7BEC) { - throw runtime_error(phosg::string_printf("compressed map list for %zu players is too large (0x%zX bytes)", num_players, compressed_map_list.size())); + throw runtime_error(std::format("compressed map list for {} players is too large (0x{:X} bytes)", num_players, compressed_map_list.size())); } size_t decompressed_size = sizeof(header) + entries_w.size() + strings_w.size(); - static_game_data_log.info("Generated Episode 3 compressed map list for %zu player(s) (%zu maps; 0x%zX -> 0x%zX bytes)", + static_game_data_log.info_f("Generated Episode 3 compressed map list for {} player(s) ({} maps; 0x{:X} -> 0x{:X} bytes)", num_players, num_maps, decompressed_size, compressed_map_list.size()); } return compressed_map_list; @@ -2883,7 +2886,7 @@ COMDeckIndex::COMDeckIndex(const string& filename) { } } } catch (const exception& e) { - static_game_data_log.warning("Failed to load Episode 3 COM decks: %s", e.what()); + static_game_data_log.warning_f("Failed to load Episode 3 COM decks: {}", e.what()); } } diff --git a/src/Episode3/DataIndexes.hh b/src/Episode3/DataIndexes.hh index 3d1eda11..aaed4aa7 100644 --- a/src/Episode3/DataIndexes.hh +++ b/src/Episode3/DataIndexes.hh @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -1566,8 +1567,8 @@ public: std::shared_ptr definition_for_name(const std::string& name) const; std::shared_ptr definition_for_name_normalized(const std::string& name) const; std::set all_ids() const; - uint64_t definitions_mtime() const; phosg::JSON definitions_json() const; + uint64_t definitions_hash() const; private: static std::string normalize_card_name(const std::string& name); @@ -1576,7 +1577,7 @@ private: std::unordered_map> card_definitions; std::unordered_map> card_definitions_by_name; std::unordered_map> card_definitions_by_name_normalized; - uint64_t mtime_for_card_definitions; + uint64_t defs_hash; }; class MapIndex { diff --git a/src/Episode3/DeckState.cc b/src/Episode3/DeckState.cc index cd59b2dd..2fa565a4 100644 --- a/src/Episode3/DeckState.cc +++ b/src/Episode3/DeckState.cc @@ -187,7 +187,7 @@ void DeckState::restart() { this->shuffle(); } -void DeckState::do_mulligan(bool is_nte) { +void DeckState::redraw_initial_hand(bool is_nte) { for (size_t z = 0; z < this->entries.size(); z++) { if (this->entries[z].state == CardState::DISCARDED) { this->entries[z].state = CardState::DRAWABLE; @@ -308,7 +308,7 @@ static const char* name_for_card_state(DeckState::CardState st) { } void DeckState::print(FILE* stream, std::shared_ptr card_index) const { - fprintf(stream, "DeckState: client_id=%hhu draw_index=%hhu card_ref_base=@%04hX shuffle=%s loop=%s\n", + phosg::fwrite_fmt(stream, "DeckState: client_id={} draw_index={} card_ref_base=@{:04X} shuffle={} loop={}\n", this->client_id, this->draw_index, this->card_ref_base, this->shuffle_enabled ? "true" : "false", this->loop_enabled ? "true" : "false"); for (size_t z = 0; z < 31; z++) { const auto& e = this->entries[z]; @@ -321,10 +321,10 @@ void DeckState::print(FILE* stream, std::shared_ptr card_index) } if (ce) { string name = ce->def.en_name.decode(1); - fprintf(stream, " (%02zu) index=%02hhX ref=@%04hX card_id=#%04hX \"%s\" %s\n", - z, e.deck_index, this->card_refs[z], e.card_id, name.c_str(), name_for_card_state(e.state)); + phosg::fwrite_fmt(stream, " ({:02}) index={:02X} ref=@{:04X} card_id=#{:04X} \"{}\" {}\n", + z, e.deck_index, this->card_refs[z], e.card_id, name, name_for_card_state(e.state)); } else { - fprintf(stream, " (%02zu) index=%02hhX ref=@%04hX card_id=#%04hX %s\n", + phosg::fwrite_fmt(stream, " ({:02}) index={:02X} ref=@{:04X} card_id=#{:04X} {}\n", z, e.deck_index, this->card_refs[z], e.card_id, name_for_card_state(e.state)); } } diff --git a/src/Episode3/DeckState.hh b/src/Episode3/DeckState.hh index 07e323ce..b77b7e47 100644 --- a/src/Episode3/DeckState.hh +++ b/src/Episode3/DeckState.hh @@ -96,7 +96,7 @@ public: void restart(); void shuffle(); - void do_mulligan(bool is_nte); + void redraw_initial_hand(bool is_nte); void print(FILE* stream, std::shared_ptr card_index = nullptr) const; diff --git a/src/Episode3/MapState.cc b/src/Episode3/MapState.cc index e0f70001..6860448c 100644 --- a/src/Episode3/MapState.cc +++ b/src/Episode3/MapState.cc @@ -20,11 +20,11 @@ void MapState::clear() { } void MapState::print(FILE* stream) const { - fprintf(stream, "[Map: w=%hu h=%hu]\n", this->width.load(), this->height.load()); + phosg::fwrite_fmt(stream, "[Map: w={} h={}]\n", this->width, this->height); for (size_t y = 0; y < this->height; y++) { fputc(' ', stream); for (size_t x = 0; x < this->width; x++) { - fprintf(stream, " %02hhX", this->tiles[y][x]); + phosg::fwrite_fmt(stream, " {:02X}", this->tiles[y][x]); } fputc('\n', stream); } diff --git a/src/Episode3/PlayerState.cc b/src/Episode3/PlayerState.cc index 4030e802..da9db687 100644 --- a/src/Episode3/PlayerState.cc +++ b/src/Episode3/PlayerState.cc @@ -9,7 +9,7 @@ namespace Episode3 { PlayerState::PlayerState(uint8_t client_id, shared_ptr server) : w_server(server), client_id(client_id), - num_mulligans_allowed(1), + num_hand_redraws_allowed(1), sc_card_type(CardType::HUNTERS_SC), team_id(0xFF), atk_points(0), @@ -705,14 +705,14 @@ void PlayerState::discard_set_assist_card() { s->destroy_cards_with_zero_hp(); } -bool PlayerState::do_mulligan() { - if (!this->is_mulligan_allowed()) { +bool PlayerState::redraw_initial_hand() { + if (!this->is_hand_redraw_allowed()) { return false; } auto s = this->server(); - this->num_mulligans_allowed--; + this->num_hand_redraws_allowed--; while (this->card_refs[0] != 0xFFFF) { this->discard_ref_from_hand(this->card_refs[0]); } @@ -727,7 +727,7 @@ bool PlayerState::do_mulligan() { s->send(cmd); } - this->deck_state->do_mulligan(s->options.is_nte()); + this->deck_state->redraw_initial_hand(s->options.is_nte()); this->draw_hand(5); if (!s->options.is_nte()) { @@ -841,7 +841,7 @@ vector PlayerState::get_all_cards_within_range( auto log = s->log_stack("get_all_cards_within_range: "); string loc_str = loc.str(); - log.debug("loc=%s, target_team_id=%02hhX", loc_str.c_str(), target_team_id); + log.debug_f("loc={}, target_team_id={:02X}", loc_str, target_team_id); vector ret; for (size_t client_id = 0; client_id < 4; client_id++) { @@ -939,8 +939,8 @@ size_t PlayerState::set_index_for_card_ref(uint16_t card_ref) const { return -1; } -bool PlayerState::is_mulligan_allowed() const { - return (this->num_mulligans_allowed > 0); +bool PlayerState::is_hand_redraw_allowed() const { + return (this->num_hand_redraws_allowed > 0); } bool PlayerState::is_team_turn() const { @@ -1766,20 +1766,20 @@ bool PlayerState::set_action_cards_for_action_state(const ActionState& pa) { auto attacker_card = s->card_for_set_card_ref(pa.attacker_card_ref); if (attacker_card) { - log.debug("attacker card present"); + log.debug_f("attacker card present"); attacker_card->card_flags |= 0x100; } auto action_type = s->ruler_server->get_pending_action_type(pa); if (action_type == ActionType::DEFENSE) { - log.debug("action type is DEFENSE"); + log.debug_f("action type is DEFENSE"); } else if (action_type == ActionType::ATTACK) { - log.debug("action type is ATTACK"); + log.debug_f("action type is ATTACK"); } else { - log.debug("action type is UNKNOWN"); + log.debug_f("action type is UNKNOWN"); } if (!is_nte) { - log.debug("(non-nte) subtracting action points"); + log.debug_f("(non-nte) subtracting action points"); this->subtract_or_check_atk_or_def_points_for_action(pa, true); } @@ -1787,7 +1787,7 @@ bool PlayerState::set_action_cards_for_action_state(const ActionState& pa) { auto card = s->card_for_set_card_ref(pa.attacker_card_ref); if (card) { card->loc.direction = pa.facing_direction; - log.debug("set facing direction to %s", phosg::name_for_enum(card->loc.direction)); + log.debug_f("set facing direction to {}", phosg::name_for_enum(card->loc.direction)); G_AddToSetCardLog_Ep3_6xB4x4A cmd; cmd.card_refs.clear(0xFFFF); @@ -1796,9 +1796,9 @@ bool PlayerState::set_action_cards_for_action_state(const ActionState& pa) { cmd.entry_count = 0; size_t z = 0; do { - if (log.should_log(phosg::LogLevel::DEBUG)) { + if (log.should_log(phosg::LogLevel::L_DEBUG)) { string ref_str = s->debug_str_for_card_ref(pa.action_card_refs[z]); - log.debug("on action card ref %s", ref_str.c_str()); + log.debug_f("on action card ref {}", ref_str); } card->unknown_80237A90(pa, pa.action_card_refs[z]); card->unknown_802379BC(pa.action_card_refs[z]); @@ -1833,9 +1833,9 @@ bool PlayerState::set_action_cards_for_action_state(const ActionState& pa) { for (size_t z = 0; (z < 4 * 9) && (pa.target_card_refs[z] != 0xFFFF); z++) { auto target_card = s->card_for_set_card_ref(pa.target_card_refs[z]); if (target_card) { - if (log.should_log(phosg::LogLevel::DEBUG)) { + if (log.should_log(phosg::LogLevel::L_DEBUG)) { string ref_str = s->debug_str_for_card_ref(pa.target_card_refs[z]); - log.debug("on target card ref %s", ref_str.c_str()); + log.debug_f("on target card ref {}", ref_str); } target_card->unknown_802379DC(pa); if (!is_nte) { @@ -1859,13 +1859,13 @@ bool PlayerState::set_action_cards_for_action_state(const ActionState& pa) { } } if (is_nte) { - log.debug("(nte) subtracting action points"); + log.debug_f("(nte) subtracting action points"); this->subtract_or_check_atk_or_def_points_for_action(pa, 1); } for (size_t z = 0; (z < pa.action_card_refs.size()) && (pa.action_card_refs[z] != 0xFFFF); z++) { - if (log.should_log(phosg::LogLevel::DEBUG)) { + if (log.should_log(phosg::LogLevel::L_DEBUG)) { string ref_str = s->debug_str_for_card_ref(pa.action_card_refs[z]); - log.debug("discarding %s from hand", ref_str.c_str()); + log.debug_f("discarding {} from hand", ref_str); } this->discard_ref_from_hand(pa.action_card_refs[z]); } diff --git a/src/Episode3/PlayerState.hh b/src/Episode3/PlayerState.hh index 64926878..e06249bf 100644 --- a/src/Episode3/PlayerState.hh +++ b/src/Episode3/PlayerState.hh @@ -66,7 +66,7 @@ public: void discard_random_hand_card(); bool discard_ref_from_hand(uint16_t card_ref); void discard_set_assist_card(); - bool do_mulligan(); + bool redraw_initial_hand(); void draw_hand(ssize_t override_count = 0); void draw_initial_hand(); int32_t error_code_for_client_setting_card( @@ -95,7 +95,7 @@ public: uint8_t get_team_id() const; ssize_t hand_index_for_card_ref(uint16_t card_ref) const; size_t set_index_for_card_ref(uint16_t card_ref) const; - bool is_mulligan_allowed() const; + bool is_hand_redraw_allowed() const; bool is_team_turn() const; void log_discard(uint16_t card_ref, uint16_t reason); uint16_t pop_from_discard_log(uint16_t reason); @@ -152,7 +152,7 @@ public: std::shared_ptr sc_card; bcarray, 8> set_cards; uint8_t client_id; - uint16_t num_mulligans_allowed; + uint16_t num_hand_redraws_allowed; CardType sc_card_type; uint8_t team_id; uint8_t atk_points; diff --git a/src/Episode3/PlayerStateSubordinates.cc b/src/Episode3/PlayerStateSubordinates.cc index 78bb0e7d..18edfddf 100644 --- a/src/Episode3/PlayerStateSubordinates.cc +++ b/src/Episode3/PlayerStateSubordinates.cc @@ -64,19 +64,19 @@ void Condition::clear_FF() { std::string Condition::str(shared_ptr s) const { auto card_ref_str = s->debug_str_for_card_ref(this->card_ref); auto giver_ref_str = s->debug_str_for_card_ref(this->condition_giver_card_ref); - return phosg::string_printf( - "Condition[type=%s, turns=%hhu, a_arg=%hhd, dice=%hhu, flags=%02hhX, " - "def_eff_index=%hhu, ref=%s, value=%hd, giver_ref=%s " - "percent=%hhu value8=%hd order=%hu a8=%hu]", + return std::format( + "Condition[type={}, turns={}, a_arg={}, dice={}, flags={:02X}, " + "def_eff_index={}, ref={}, value={}, giver_ref={} " + "percent={} value8={} order={} a8={}]", phosg::name_for_enum(this->type), this->remaining_turns, this->a_arg_value, this->dice_roll_value, this->flags, this->card_definition_effect_index, - card_ref_str.c_str(), - this->value.load(), - giver_ref_str.c_str(), + card_ref_str, + this->value, + giver_ref_str, this->random_percent, this->value8, this->order, @@ -103,12 +103,12 @@ void EffectResult::clear() { std::string EffectResult::str(shared_ptr s) const { string attacker_ref_str = s->debug_str_for_card_ref(this->attacker_card_ref); string target_ref_str = s->debug_str_for_card_ref(this->target_card_ref); - return phosg::string_printf( - "EffectResult[att_ref=%s, target_ref=%s, value=%hhd, " - "cur_hp=%hhd, ap=%hhd, tp=%hhd, flags=%02hhX, op=%hhd, " - "cond_index=%hhu, dice=%hhu]", - attacker_ref_str.c_str(), - target_ref_str.c_str(), + return std::format( + "EffectResult[att_ref={}, target_ref={}, value={}, " + "cur_hp={}, ap={}, tp={}, flags={:02X}, op={}, " + "cond_index={}, dice={}]", + attacker_ref_str, + target_ref_str, this->value, this->current_hp, this->ap, @@ -139,14 +139,14 @@ bool CardShortStatus::operator!=(const CardShortStatus& other) const { std::string CardShortStatus::str(shared_ptr s) const { string loc_s = this->loc.str(); string ref_str = s->debug_str_for_card_ref(this->card_ref); - return phosg::string_printf( - "CardShortStatus[ref=%s, cur_hp=%hd, flags=%08" PRIX32 ", loc=%s, " - "u1=%04hX, max_hp=%hhd, u2=%hhu]", - ref_str.c_str(), - this->current_hp.load(), - this->card_flags.load(), - loc_s.c_str(), - this->unused1.load(), + return std::format( + "CardShortStatus[ref={}, cur_hp={}, flags={:08X}, loc={}, " + "u1={:04X}, max_hp={}, u2={}]", + ref_str, + this->current_hp, + this->card_flags, + loc_s, + this->unused1, this->max_hp, this->unused2); } @@ -193,18 +193,18 @@ std::string ActionState::str(shared_ptr s) const { string original_attacker_ref_s = s->debug_str_for_card_ref(this->original_attacker_card_ref); string target_refs_s = s->debug_str_for_card_refs(this->target_card_refs); string action_refs_s = s->debug_str_for_card_refs(this->action_card_refs); - return phosg::string_printf( - "ActionState[client=%hX, u=%hhu, facing=%s, attacker_ref=%s, " - "def_ref=%s, target_refs=%s, action_refs=%s, " - "orig_attacker_ref=%s]", - this->client_id.load(), + return std::format( + "ActionState[client={:X}, u={}, facing={}, attacker_ref={}, " + "def_ref={}, target_refs={}, action_refs={}, " + "orig_attacker_ref={}]", + this->client_id, this->unused, phosg::name_for_enum(this->facing_direction), - attacker_ref_s.c_str(), - defense_ref_s.c_str(), - target_refs_s.c_str(), - action_refs_s.c_str(), - original_attacker_ref_s.c_str()); + attacker_ref_s, + defense_ref_s, + target_refs_s, + action_refs_s, + original_attacker_ref_s); } ActionChain::ActionChain() { @@ -243,20 +243,20 @@ std::string ActionChain::str(shared_ptr s) const { string unknown_card_ref_a3_s = s->debug_str_for_card_ref(this->unknown_card_ref_a3); string attack_action_card_refs_s = s->debug_str_for_card_refs(this->attack_action_card_refs); string target_card_refs_s = s->debug_str_for_card_refs(this->target_card_refs); - return phosg::string_printf( - "ActionChain[eff_ap=%hhd, eff_tp=%hhd, ap_bonus=%hhd, damage=%hhd, " - "acting_ref=%s, unknown_ref_a3=%s, attack_action_refs=%s, " - "attack_action_ref_count=%hhu, medium=%s, target_ref_count=%hhu, " - "subphase=%s, strikes=%hhu, damage_mult=%hhd, attack_num=%hhu, " - "tp_bonus=%hhd, phys_bonus_nte=%hhu, tech_bonus_nte=%hhu, card_ap=%hhd, " - "card_tp=%hhd, flags=%08" PRIX32 ", target_refs=%s]", + return std::format( + "ActionChain[eff_ap={}, eff_tp={}, ap_bonus={}, damage={}, " + "acting_ref={}, unknown_ref_a3={}, attack_action_refs={}, " + "attack_action_ref_count={}, medium={}, target_ref_count={}, " + "subphase={}, strikes={}, damage_mult={}, attack_num={}, " + "tp_bonus={}, phys_bonus_nte={}, tech_bonus_nte={}, card_ap={}, " + "card_tp={}, flags={:08X}, target_refs={}]", this->effective_ap, this->effective_tp, this->ap_effect_bonus, this->damage, - acting_card_ref_s.c_str(), - unknown_card_ref_a3_s.c_str(), - attack_action_card_refs_s.c_str(), + acting_card_ref_s, + unknown_card_ref_a3_s, + attack_action_card_refs_s, this->attack_action_card_ref_count, phosg::name_for_enum(this->attack_medium), this->target_card_ref_count, @@ -269,8 +269,8 @@ std::string ActionChain::str(shared_ptr s) const { this->tech_attack_bonus_nte, this->card_ap, this->card_tp, - this->flags.load(), - target_card_refs_s.c_str()); + this->flags, + target_card_refs_s); } void ActionChain::clear() { @@ -341,7 +341,7 @@ std::string ActionChainWithConds::str(shared_ptr s) const { if (ret.back() != '[') { ret += ", "; } - ret += phosg::string_printf("%zu:", z); + ret += std::format("{}:", z); ret += this->conditions[z].str(s); } } @@ -580,22 +580,22 @@ std::string ActionMetadata::str(shared_ptr s) const { string target_card_refs_s = s->debug_str_for_card_refs(this->target_card_refs); string defense_card_refs_s = s->debug_str_for_card_refs(this->defense_card_refs); string original_attacker_card_refs_s = s->debug_str_for_card_refs(this->original_attacker_card_refs); - return phosg::string_printf( - "ActionMetadata[ref=%s, target_ref_count=%hhu, def_ref_count=%hhu, " - "subphase=%s, def_power=%hhd, def_bonus=%hhd, " - "att_bonus=%hhd, flags=%08" PRIX32 ", target_refs=%s, " - "defense_refs=%s, original_attacker_refs=%s]", - card_ref_s.c_str(), + return std::format( + "ActionMetadata[ref={}, target_ref_count={}, def_ref_count={}, " + "subphase={}, def_power={}, def_bonus={}, " + "att_bonus={}, flags={:08X}, target_refs={}, " + "defense_refs={}, original_attacker_refs={}]", + card_ref_s, this->target_card_ref_count, this->defense_card_ref_count, phosg::name_for_enum(this->action_subphase), this->defense_power, this->defense_bonus, this->attack_bonus, - this->flags.load(), - target_card_refs_s.c_str(), - defense_card_refs_s.c_str(), - original_attacker_card_refs_s.c_str()); + this->flags, + target_card_refs_s, + defense_card_refs_s, + original_attacker_card_refs_s); } void ActionMetadata::clear() { @@ -683,13 +683,13 @@ std::string HandAndEquipState::str(shared_ptr s) const { string set_card_refs_s = s->debug_str_for_card_refs(this->set_card_refs); string hand_card_refs2_s = s->debug_str_for_card_refs(this->hand_card_refs2); string set_card_refs2_s = s->debug_str_for_card_refs(this->set_card_refs2); - return phosg::string_printf( - "HandAndEquipState[dice=[%hhu, %hhu], atk=%hhu, def=%hhu, atk2=%hhu, " - "a1=%hhu, total_set_cost=%hhu, is_cpu=%hhu, assist_flags=%08" PRIX32 ", " - "hand_refs=%s, assist_ref=%s, set_refs=%s, sc_ref=%s, hand_refs2=%s, " - "set_refs2=%s, assist_ref2=%s, assist_set_num=%hu, assist_card_id=%s, " - "assist_turns=%hhu, assist_delay=%hhu, atk_bonus=%hhu, def_bonus=%hhu, " - "u2=[%hhu, %hhu]]", + return std::format( + "HandAndEquipState[dice=[{}, {}], atk={}, def={}, atk2={}, " + "a1={}, total_set_cost={}, is_cpu={}, assist_flags={:08X}, " + "hand_refs={}, assist_ref={}, set_refs={}, sc_ref={}, hand_refs2={}, " + "set_refs2={}, assist_ref2={}, assist_set_num={}, assist_card_id={}, " + "assist_turns={}, assist_delay={}, atk_bonus={}, def_bonus={}, " + "u2=[{}, {}]]", this->dice_results[0], this->dice_results[1], this->atk_points, @@ -698,16 +698,16 @@ std::string HandAndEquipState::str(shared_ptr s) const { this->unknown_a1, this->total_set_cards_cost, this->is_cpu_player, - this->assist_flags.load(), - hand_card_refs_s.c_str(), - assist_card_ref_s.c_str(), - set_card_refs_s.c_str(), - sc_card_ref_s.c_str(), - hand_card_refs2_s.c_str(), - set_card_refs2_s.c_str(), - assist_card_ref2_s.c_str(), - this->assist_card_set_number.load(), - assist_card_id_s.c_str(), + this->assist_flags, + hand_card_refs_s, + assist_card_ref_s, + set_card_refs_s, + sc_card_ref_s, + hand_card_refs2_s, + set_card_refs2_s, + assist_card_ref2_s, + this->assist_card_set_number, + assist_card_id_s, this->assist_remaining_turns, this->assist_delay_turns, this->atk_bonuses, @@ -838,19 +838,19 @@ const char* PlayerBattleStats::name_for_rank(uint8_t rank) { } PlayerBattleStatsTrial::PlayerBattleStatsTrial(const PlayerBattleStats& data) - : damage_given(data.damage_given.load()), - damage_taken(data.damage_taken.load()), - num_opponent_cards_destroyed(data.num_opponent_cards_destroyed.load()), - num_owned_cards_destroyed(data.num_owned_cards_destroyed.load()), - total_move_distance(data.total_move_distance.load()) {} + : damage_given(data.damage_given), + damage_taken(data.damage_taken), + num_opponent_cards_destroyed(data.num_opponent_cards_destroyed), + num_owned_cards_destroyed(data.num_owned_cards_destroyed), + total_move_distance(data.total_move_distance) {} PlayerBattleStatsTrial::operator PlayerBattleStats() const { PlayerBattleStats ret; - ret.damage_given = this->damage_given.load(); - ret.damage_taken = this->damage_taken.load(); - ret.num_opponent_cards_destroyed = this->num_opponent_cards_destroyed.load(); - ret.num_owned_cards_destroyed = this->num_owned_cards_destroyed.load(); - ret.total_move_distance = this->total_move_distance.load(); + ret.damage_given = this->damage_given; + ret.damage_taken = this->damage_taken; + ret.num_opponent_cards_destroyed = this->num_opponent_cards_destroyed; + ret.num_owned_cards_destroyed = this->num_owned_cards_destroyed; + ret.total_move_distance = this->total_move_distance; return ret; } @@ -861,26 +861,26 @@ static bool is_card_within_range( phosg::PrefixedLogger* log) { if (ss.card_ref == 0xFFFF) { if (log) { - log->debug("is_card_within_range: (false) ss.card_ref missing"); + log->debug_f("is_card_within_range: (false) ss.card_ref missing"); } return false; } if (range[0] == 2) { if (log) { - log->debug("is_card_within_range: (true) range is entire field"); + log->debug_f("is_card_within_range: (true) range is entire field"); } return true; } if ((ss.loc.x < anchor_loc.x - 4) || (ss.loc.x > anchor_loc.x + 4)) { if (log) { - log->debug("is_card_within_range: (false) outside x range (ss.loc.x=%hhu, anchor_loc.x=%hhu)", ss.loc.x, anchor_loc.x); + log->debug_f("is_card_within_range: (false) outside x range (ss.loc.x={}, anchor_loc.x={})", ss.loc.x, anchor_loc.x); } return false; } if ((ss.loc.y < anchor_loc.y - 4) || (ss.loc.y > anchor_loc.y + 4)) { if (log) { - log->debug("is_card_within_range: (false) outside y range (ss.loc.y=%hhu, anchor_loc.y=%hhu)", ss.loc.y, anchor_loc.y); + log->debug_f("is_card_within_range: (false) outside y range (ss.loc.y={}, anchor_loc.y={})", ss.loc.y, anchor_loc.y); } return false; } @@ -889,7 +889,7 @@ static bool is_card_within_range( uint8_t x_index = (ss.loc.x - anchor_loc.x) + 4; bool ret = (range[y_index * 9 + x_index] != 0); if (log) { - log->debug("is_card_within_range: (%s) (ss.loc=(%hhu,%hhu), anchor_loc=(%hhu,%hhu), indexes=(%hhu,%hhu))", + log->debug_f("is_card_within_range: ({}) (ss.loc=({},{}), anchor_loc=({},{}), indexes=({},{}))", ret ? "true" : "false", ss.loc.x, ss.loc.y, anchor_loc.x, anchor_loc.y, x_index, y_index); } return ret; @@ -903,24 +903,24 @@ vector get_card_refs_within_range( vector ret; if (is_card_within_range(range, loc, short_statuses[0], log)) { if (log) { - log->debug("get_card_refs_within_range: sc card @%04hX within range", short_statuses[0].card_ref.load()); + log->debug_f("get_card_refs_within_range: sc card @{:04X} within range", short_statuses[0].card_ref); } ret.emplace_back(short_statuses[0].card_ref); } else { if (log) { - log->debug("get_card_refs_within_range: sc card @%04hX not within range", short_statuses[0].card_ref.load()); + log->debug_f("get_card_refs_within_range: sc card @{:04X} not within range", short_statuses[0].card_ref); } } for (size_t card_index = 7; card_index < 15; card_index++) { const auto& ss = short_statuses[card_index]; if (is_card_within_range(range, loc, ss, log)) { if (log) { - log->debug("get_card_refs_within_range: card @%04hX within range", ss.card_ref.load()); + log->debug_f("get_card_refs_within_range: card @{:04X} within range", ss.card_ref); } ret.emplace_back(ss.card_ref); } else { if (log) { - log->debug("get_card_refs_within_range: card @%04hX not within range", ss.card_ref.load()); + log->debug_f("get_card_refs_within_range: card @{:04X} not within range", ss.card_ref); } } } diff --git a/src/Episode3/RulerServer.cc b/src/Episode3/RulerServer.cc index d432a9b7..f1ff5ca3 100644 --- a/src/Episode3/RulerServer.cc +++ b/src/Episode3/RulerServer.cc @@ -16,10 +16,10 @@ void compute_effective_range( const Location& loc, shared_ptr map_and_rules, phosg::PrefixedLogger* log) { - if (log && log->should_log(phosg::LogLevel::DEBUG)) { + if (log && log->should_log(phosg::LogLevel::L_DEBUG)) { string loc_str = loc.str(); - log->debug("compute_effective_range: card_id=#%04hX, loc=%s", card_id, loc_str.c_str()); - log->debug("compute_effective_range: map_and_rules->map:"); + log->debug_f("compute_effective_range: card_id=#{:04X}, loc={}", card_id, loc_str); + log->debug_f("compute_effective_range: map_and_rules->map:"); map_and_rules->map.print(stderr); } ret.clear(0); @@ -40,14 +40,14 @@ void compute_effective_range( } } if (log) { - log->debug("compute_effective_range: range_def: %05" PRIX32 " %05" PRIX32 " %05" PRIX32 " %05" PRIX32 " %05" PRIX32 " %05" PRIX32, range_def[0], range_def[1], range_def[2], range_def[3], range_def[4], range_def[5]); + log->debug_f("compute_effective_range: range_def: {:05X} {:05X} {:05X} {:05X} {:05X} {:05X}", range_def[0], range_def[1], range_def[2], range_def[3], range_def[4], range_def[5]); } if (range_def[0] == 0x000FFFFF) { // Entire field ret.clear(2); if (log) { - log->debug("compute_effective_range: entire field (2)"); + log->debug_f("compute_effective_range: entire field (2)"); } return; } @@ -64,7 +64,7 @@ void compute_effective_range( } if (log) { for (size_t y = 0; y < 9; y++) { - log->debug("compute_effective_range: decoded_range: %hhX %hhX %hhX %hhX %hhX %hhX %hhX %hhX %hhX", + log->debug_f("compute_effective_range: decoded_range: {:X} {:X} {:X} {:X} {:X} {:X} {:X} {:X} {:X}", decoded_range[y * 9 + 0], decoded_range[y * 9 + 1], decoded_range[y * 9 + 2], decoded_range[y * 9 + 3], decoded_range[y * 9 + 4], decoded_range[y * 9 + 5], decoded_range[y * 9 + 6], decoded_range[y * 9 + 7], decoded_range[y * 9 + 8]); } } @@ -98,7 +98,7 @@ void compute_effective_range( } ret[y * 9 + x] = decoded_range[up_y * 9 + up_x]; if (log) { - log->debug("compute_effective_range: x=%hd y=%hd up_x=%hd up_y=%hd v=%hhX", x, y, up_x, up_y, ret[y * 9 + x]); + log->debug_f("compute_effective_range: x={} y={} up_x={} up_y={} v={:X}", x, y, up_x, up_y, ret[y * 9 + x]); } } } @@ -107,7 +107,7 @@ void compute_effective_range( if (log) { for (size_t y = 0; y < 9; y++) { - log->debug("compute_effective_range: ret: %hhX %hhX %hhX %hhX %hhX %hhX %hhX %hhX %hhX", + log->debug_f("compute_effective_range: ret: {:X} {:X} {:X} {:X} {:X} {:X} {:X} {:X} {:X}", ret[y * 9 + 0], ret[y * 9 + 1], ret[y * 9 + 2], ret[y * 9 + 3], ret[y * 9 + 4], ret[y * 9 + 5], ret[y * 9 + 6], ret[y * 9 + 7], ret[y * 9 + 8]); } } @@ -941,7 +941,7 @@ bool RulerServer::check_usability_or_condition_apply( AttackMedium attack_medium) const { auto s = this->server(); bool is_nte = s->options.is_nte(); - auto log = s->log_stack(phosg::string_printf("check_usability_or_condition_apply(%02hhX, #%04hX, %02hhX, #%04hX, #%04hX, %02hhX, %s, %s): ", client_id1, card_id1, client_id2, card_id2, card_id3, def_effect_index, is_item_usability_check ? "true" : "false", phosg::name_for_enum(attack_medium))); + auto log = s->log_stack(std::format("check_usability_or_condition_apply({:02X}, #{:04X}, {:02X}, #{:04X}, #{:04X}, {:02X}, {}, {}): ", client_id1, card_id1, client_id2, card_id2, card_id3, def_effect_index, is_item_usability_check ? "true" : "false", phosg::name_for_enum(attack_medium))); if (static_cast(attack_medium) & 0x80) { attack_medium = AttackMedium::UNKNOWN; @@ -951,11 +951,11 @@ bool RulerServer::check_usability_or_condition_apply( auto ce2 = this->definition_for_card_id(card_id2); auto ce3 = this->definition_for_card_id(card_id3); if (!ce1) { - log.debug("ce1 missing"); + log.debug_f("ce1 missing"); return false; } if (!is_nte && (ce1->def.type == CardType::ITEM) && this->card_id_is_boss_sc(card_id2)) { - log.debug("ce1 is item and card_id2 is boss sc"); + log.debug_f("ce1 is item and card_id2 is boss sc"); return false; } @@ -964,12 +964,12 @@ bool RulerServer::check_usability_or_condition_apply( criterion_code = ce1->def.usable_criterion; } else { if (def_effect_index > 2) { - log.debug("invalid def_effect_index"); + log.debug_f("invalid def_effect_index"); return false; } criterion_code = ce1->def.effects[def_effect_index].apply_criterion; } - log.debug("criterion_code=%s", phosg::name_for_enum(criterion_code)); + log.debug_f("criterion_code={}", phosg::name_for_enum(criterion_code)); // For item usability checks, prevent criteria that depend on player // positioning/team setup @@ -980,7 +980,7 @@ bool RulerServer::check_usability_or_condition_apply( (criterion_code == CriterionCode::FC) || (criterion_code == CriterionCode::NOT_SC) || (criterion_code == CriterionCode::SC))) { - log.debug("criterion is forbidden"); + log.debug_f("criterion is forbidden"); criterion_code = CriterionCode::NONE; } @@ -1354,7 +1354,7 @@ bool RulerServer::check_usability_or_condition_apply( } } - log.debug("default return (false)"); + log.debug_f("default return (false)"); return false; } @@ -1478,43 +1478,43 @@ bool RulerServer::compute_effective_range_and_target_mode_for_attack( for (z = 0; (z < 8) && (pa.action_card_refs[z] != 0xFFFF); z++) { } if (z >= 8) { - log.debug("too many action card refs"); + log.debug_f("too many action card refs"); return false; } - log.debug("%zu action card refs", z); + log.debug_f("{} action card refs", z); uint16_t card_ref = (z == 0) ? pa.attacker_card_ref : pa.action_card_refs[z - 1]; - log.debug("base card ref = @%04hX", card_ref); + log.debug_f("base card ref = @{:04X}", card_ref); uint16_t card_id = this->card_id_for_card_ref(card_ref); if (card_id == 0xFFFF) { - log.debug("card ref is broken"); + log.debug_f("card ref is broken"); return false; } auto ce = this->definition_for_card_id(card_id); uint8_t client_id = client_id_for_card_ref(pa.attacker_card_ref); if ((client_id == 0xFF) || !ce) { - log.debug("card ref is broken or definition is missing"); + log.debug_f("card ref is broken or definition is missing"); return false; } if (out_orig_card_ref) { - log.debug("orig_card_ref = @%04hX", card_ref); + log.debug_f("orig_card_ref = @{:04X}", card_ref); *out_orig_card_ref = card_ref; } auto target_mode = ce->def.target_mode; if (this->card_ref_or_sc_has_fixed_range(pa.attacker_card_ref)) { const char* target_mode_name = name_for_target_mode(target_mode); - log.debug("attacker card ref @%04hX has fixed range; target mode is %s (%hhu)", - pa.attacker_card_ref.load(), target_mode_name, static_cast(target_mode)); + log.debug_f("attacker card ref @{:04X} has fixed range; target mode is {} ({})", + pa.attacker_card_ref, target_mode_name, static_cast(target_mode)); card_id = this->card_id_for_card_ref(pa.attacker_card_ref); if (!is_nte) { auto sc_ce = this->definition_for_card_id(card_id); if (sc_ce && (static_cast(target_mode) < 6)) { target_mode = sc_ce->def.target_mode; const char* target_mode_name = name_for_target_mode(target_mode); - log.debug("sc_ce overrides target mode with %s (%hhu)", + log.debug_f("sc_ce overrides target mode with {} ({})", target_mode_name, static_cast(target_mode)); } } @@ -1525,10 +1525,10 @@ bool RulerServer::compute_effective_range_and_target_mode_for_attack( auto assist_effect = this->assist_server->get_active_assist_by_index(z); if (assist_effect == AssistEffect::SIMPLE) { card_id = this->card_id_for_card_ref(pa.attacker_card_ref); - log.debug("SIMPLE assist overrides card id with #%04hX", card_id); + log.debug_f("SIMPLE assist overrides card id with #{:04X}", card_id); } else if (assist_effect == AssistEffect::HEAVY_FOG) { card_id = 0xFFFE; - log.debug("HEAVY_FOG assist overrides card id with #%04hX", card_id); + log.debug_f("HEAVY_FOG assist overrides card id with #{:04X}", card_id); } } @@ -2059,27 +2059,27 @@ shared_ptr RulerServer::definition_for_card_id(uint3 uint32_t RulerServer::get_card_id_with_effective_range( uint16_t card_ref, uint16_t card_id_override, TargetMode* out_target_mode) const { - auto log = this->server()->log_stack(phosg::string_printf("get_card_id_with_effective_range(@%04hX, #%04hX): ", card_ref, card_id_override)); + auto log = this->server()->log_stack(std::format("get_card_id_with_effective_range(@{:04X}, #{:04X}): ", card_ref, card_id_override)); uint16_t card_id = (card_id_override == 0xFFFF) ? this->card_id_for_card_ref(card_ref) : card_id_override; - log.debug("card_id=#%04hX", card_id); + log.debug_f("card_id=#{:04X}", card_id); if (card_id != 0xFFFF) { auto ce = this->definition_for_card_id(card_id); uint8_t client_id = client_id_for_card_ref(card_ref); if ((client_id != 0xFF) && ce) { TargetMode effective_target_mode = ce->def.target_mode; - log.debug("ce valid for #%04hX with effective target mode %s", card_id, name_for_target_mode(effective_target_mode)); + log.debug_f("ce valid for #{:04X} with effective target mode {}", card_id, name_for_target_mode(effective_target_mode)); if (this->card_ref_or_sc_has_fixed_range(card_ref)) { // Undo the override that may have been passed in - log.debug("@%04hX has FIXED_RANGE", card_ref); + log.debug_f("@{:04X} has FIXED_RANGE", card_ref); card_id = this->card_id_for_card_ref(card_ref); auto orig_ce = this->definition_for_card_id(card_id); if (orig_ce && (static_cast(effective_target_mode) < 6)) { - log.debug("ce valid for #%04hX with effective target mode %s; overriding to %s", card_id, name_for_target_mode(effective_target_mode), name_for_target_mode(orig_ce->def.target_mode)); + log.debug_f("ce valid for #{:04X} with effective target mode {}; overriding to {}", card_id, name_for_target_mode(effective_target_mode), name_for_target_mode(orig_ce->def.target_mode)); effective_target_mode = orig_ce->def.target_mode; } } @@ -2089,17 +2089,17 @@ uint32_t RulerServer::get_card_id_with_effective_range( auto eff = this->assist_server->get_active_assist_by_index(z); if (eff == AssistEffect::SIMPLE) { card_id = this->card_id_for_card_ref(card_ref); - log.debug("SIMPLE assist effect is active; using #%04hX for range", card_id); + log.debug_f("SIMPLE assist effect is active; using #{:04X} for range", card_id); } else if (eff == AssistEffect::HEAVY_FOG) { card_id = 0xFFFE; - log.debug("HEAVY_FOG assist effect is active; limiting range to one tile in front"); + log.debug_f("HEAVY_FOG assist effect is active; limiting range to one tile in front"); } } if (out_target_mode) { *out_target_mode = effective_target_mode; } - log.debug("results: card_id=#%04hX, target_mode=%s", card_id, name_for_target_mode(effective_target_mode)); + log.debug_f("results: card_id=#{:04X}, target_mode={}", card_id, name_for_target_mode(effective_target_mode)); } } diff --git a/src/Episode3/Server.cc b/src/Episode3/Server.cc index e0bf2412..3b9069ea 100644 --- a/src/Episode3/Server.cc +++ b/src/Episode3/Server.cc @@ -58,7 +58,7 @@ Server::Server(shared_ptr lobby, Options&& options) battle_start_usecs(0), should_copy_prev_states_to_current_states(0), card_special(nullptr), - clients_done_in_mulligan_phase(false), + clients_done_in_redraw_initial_hand_phase(false), num_pending_attacks_with_cards(0), unknown_a14(0), unknown_a15(0), @@ -83,12 +83,14 @@ Server::Server(shared_ptr lobby, Options&& options) Server::~Server() noexcept(false) { if (this->logger_stack.size() != 1) { - throw logic_error(phosg::string_printf("incorrect logger stack size: expected 1, received %zu", this->logger_stack.size())); + throw logic_error(std::format("incorrect logger stack size: expected 1, received {}", this->logger_stack.size())); } delete this->logger_stack.back(); } void Server::init() { + this->log().info_f("Creating server with random seed {:08X}", this->options.rand_crypt->seed()); + this->map_and_rules = make_shared(); this->num_clients_present = 0; this->overlay_state.clear(); @@ -173,9 +175,9 @@ std::string Server::debug_str_for_card_ref(uint16_t card_ref) const { auto ce = this->definition_for_card_ref(card_ref); if (ce) { string name = ce->def.en_name.decode(); - return phosg::string_printf("@%04hX (#%04" PRIX32 " %s)", card_ref, ce->def.card_id.load(), name.c_str()); + return std::format("@{:04X} (#{:04X} {})", card_ref, ce->def.card_id, name); } else { - return phosg::string_printf("@%04hX (missing)", card_ref); + return std::format("@{:04X} (missing)", card_ref); } } @@ -186,9 +188,9 @@ std::string Server::debug_str_for_card_id(uint16_t card_id) const { auto ce = this->definition_for_card_id(card_id); if (ce) { string name = ce->def.en_name.decode(); - return phosg::string_printf("#%04hX (%s)", card_id, name.c_str()); + return std::format("#{:04X} ({})", card_id, name); } else { - return phosg::string_printf("#%04hX (missing)", card_id); + return std::format("#{:04X} (missing)", card_id); } } @@ -260,7 +262,7 @@ void Server::send(const void* data, size_t size, uint8_t command, bool enable_ma } } else if ((this->options.behavior_flags & BehaviorFlag::LOG_COMMANDS_IF_LOBBY_MISSING) && - this->log().info("Generated command")) { + this->log().info_f("Generated command")) { phosg::print_data(stderr, data, size, 0, nullptr, phosg::PrintDataFlags::PRINT_ASCII | phosg::PrintDataFlags::DISABLE_COLOR | phosg::PrintDataFlags::OFFSET_16_BITS); } } @@ -273,9 +275,9 @@ void Server::send_6xB4x46() const { // debugging easier. G_ServerVersionStrings_Ep3_6xB4x46 cmd; cmd.version_signature.encode(this->options.is_nte() ? VERSION_SIGNATURE_NTE : VERSION_SIGNATURE, 1); - cmd.date_str1.encode(phosg::format_time(this->options.card_index->definitions_mtime() * 1000000), 1); + cmd.date_str1.encode(std::format("Card definitions: {:016X}", this->options.card_index->definitions_hash()), 1); string build_date = phosg::format_time(BUILD_TIMESTAMP); - cmd.date_str2.encode(phosg::string_printf("newserv %s compiled at %s", GIT_REVISION_HASH, build_date.c_str()), 1); + cmd.date_str2.encode(std::format("newserv {} compiled at {}", GIT_REVISION_HASH, build_date), 1); this->send(cmd); } @@ -291,7 +293,7 @@ string Server::prepare_6xB6x41_map_definition(shared_ptr ma return std::move(w.str()); } -void Server::send_commands_for_joining_spectator(Channel& ch) const { +void Server::send_commands_for_joining_spectator(std::shared_ptr ch) const { bool should_send_state = true; if (this->setup_phase == SetupPhase::REGISTRATION) { // If registration is still in progress, we only need to send the map data @@ -303,84 +305,62 @@ void Server::send_commands_for_joining_spectator(Channel& ch) const { } if (this->last_chosen_map) { - string data = this->prepare_6xB6x41_map_definition(this->last_chosen_map, ch.language, this->options.is_nte()); - this->log().info("Sending %c version of map %08" PRIX32, char_for_language_code(ch.language), this->last_chosen_map->map_number); - ch.send(0x6C, 0x00, data); + string data = this->prepare_6xB6x41_map_definition(this->last_chosen_map, ch->language, this->options.is_nte()); + this->log().info_f("Sending {} version of map {:08X}", char_for_language_code(ch->language), this->last_chosen_map->map_number); + ch->send(0x6C, 0x00, data); } if (should_send_state) { - ch.send(0xC9, 0x00, this->prepare_6xB4x03()); + ch->send(0xC9, 0x00, this->prepare_6xB4x03()); for (uint8_t client_id = 0; client_id < 4; client_id++) { auto ps = this->player_states[client_id]; if (ps) { - ch.send(0xC9, 0x00, ps->prepare_6xB4x02()); - ch.send(0xC9, 0x00, ps->prepare_6xB4x04()); + ch->send(0xC9, 0x00, ps->prepare_6xB4x02()); + ch->send(0xC9, 0x00, ps->prepare_6xB4x04()); } } - if (ch.version == Version::GC_EP3_NTE) { + if (ch->version == Version::GC_EP3_NTE) { G_UpdateMap_Ep3NTE_6xB4x05 cmd; cmd.state = *this->map_and_rules; - ch.send(0xC9, 0x00, cmd); + ch->send(0xC9, 0x00, cmd); } else { G_UpdateMap_Ep3_6xB4x05 cmd; cmd.state = *this->map_and_rules; - ch.send(0xC9, 0x00, cmd); + ch->send(0xC9, 0x00, cmd); } // TODO: Sega does something like this; do we have to do this too? // for (uint8_t client_id = 0; client_id < 4; client_id++) { // (send 6xB4x4E, 6xB4x4C, 6xB4x4D for each set card) // (send 6xB4x4F for client_id) // } - ch.send(0xC9, 0x00, this->prepare_6xB4x07_decks_update()); + ch->send(0xC9, 0x00, this->prepare_6xB4x07_decks_update()); // TODO: Sega sends 6xB4x05 here again; why? Is that necessary? They also // send 6xB4x02 again for each player after that (but not 6xB4x04) - ch.send(0xC9, 0x00, this->prepare_6xB4x1C_names_update()); - ch.send(0xC9, 0x00, this->prepare_6xB4x50_trap_tile_locations()); + ch->send(0xC9, 0x00, this->prepare_6xB4x1C_names_update()); + ch->send(0xC9, 0x00, this->prepare_6xB4x50_trap_tile_locations()); { G_LoadCurrentEnvironment_Ep3_6xB4x3B cmd_3B; - ch.send(0xC9, 0x00, &cmd_3B, sizeof(cmd_3B)); + ch->send(0xC9, 0x00, &cmd_3B, sizeof(cmd_3B)); } } } -__attribute__((format(printf, 2, 3))) void Server::send_debug_message_printf(const char* fmt, ...) const { - auto l = this->lobby.lock(); - if (l && (this->options.behavior_flags & Episode3::BehaviorFlag::ENABLE_STATUS_MESSAGES)) { - va_list va; - va_start(va, fmt); - std::string buf = phosg::string_vprintf(fmt, va); - va_end(va); - send_text_message(l, buf); - } -} - -__attribute__((format(printf, 2, 3))) void Server::send_info_message_printf(const char* fmt, ...) const { - auto l = this->lobby.lock(); - if (l) { - va_list va; - va_start(va, fmt); - std::string buf = phosg::string_vprintf(fmt, va); - va_end(va); - send_text_message(l, buf); - } -} - void Server::send_debug_command_received_message( uint8_t client_id, uint8_t subsubcommand, const char* description) const { - this->log().debug("%hhu/CAx%02hhX %s", client_id, subsubcommand, description); - this->send_debug_message_printf("$C5%hhu/CAx%02hhX %s", client_id, subsubcommand, description); + this->log().debug_f("{}/CAx{:02X} {}", client_id, subsubcommand, description); + this->send_debug_message("$C5{}/CAx{:02X} {}", client_id, subsubcommand, description); } void Server::send_debug_command_received_message(uint8_t subsubcommand, const char* description) const { - this->log().debug("*/CAx%02hhX %s", subsubcommand, description); - this->send_debug_message_printf("$C5*/CAx%02hhX %s", subsubcommand, description); + this->log().debug_f("*/CAx{:02X} {}", subsubcommand, description); + this->send_debug_message("$C5*/CAx{:02X} {}", subsubcommand, description); } void Server::send_debug_message_if_error_code_nonzero(uint8_t client_id, int32_t error_code) const { if (error_code < 0) { - this->send_debug_message_printf("$C4%hhu/ERROR -0x%zX", client_id, static_cast(-error_code)); + this->send_debug_message("$C4{}/ERROR -0x{:X}", client_id, static_cast(-error_code)); } else if (error_code > 0) { - this->send_debug_message_printf("$C4%hhu/ERROR 0x%zX", client_id, static_cast(error_code)); + this->send_debug_message("$C4{}/ERROR 0x{:X}", client_id, static_cast(error_code)); } } @@ -949,62 +929,62 @@ void Server::end_action_phase() { bool Server::enqueue_attack_or_defense(uint8_t client_id, ActionState* pa) { auto log = this->log_stack("enqueue_attack_or_defense: "); - if (log.should_log(phosg::LogLevel::DEBUG)) { + if (log.should_log(phosg::LogLevel::L_DEBUG)) { string s = pa->str(this->shared_from_this()); - log.debug("input: %s", s.c_str()); + log.debug_f("input: {}", s); } if (client_id >= 4) { this->ruler_server->error_code3 = -0x78; - log.debug("failed: invalid client ID"); + log.debug_f("failed: invalid client ID"); return false; } auto ps = this->player_states[client_id]; if (!ps) { this->ruler_server->error_code3 = -0x72; - log.debug("failed: player not present"); + log.debug_f("failed: player not present"); return false; } if (pa->action_card_refs[0] == 0xFFFF) { if (pa->defense_card_ref != 0xFFFF) { pa->action_card_refs[0] = pa->defense_card_ref; - log.debug("moved defense card ref to action card ref 0"); + log.debug_f("moved defense card ref to action card ref 0"); } } else { pa->defense_card_ref = pa->action_card_refs[0]; - log.debug("moved action card ref 0 to defense card ref"); + log.debug_f("moved action card ref 0 to defense card ref"); } if (!this->ruler_server->is_attack_or_defense_valid(*pa)) { - log.debug("failed: attack or defense not valid"); + log.debug_f("failed: attack or defense not valid"); return false; } int16_t ally_atk_result = this->send_6xB4x33_remove_ally_atk_if_needed(*pa); if (ally_atk_result == 1) { - log.debug("pending: need ally approval"); + log.debug_f("pending: need ally approval"); return true; } else if (ally_atk_result == -1) { - log.debug("failed: ally declined"); + log.debug_f("failed: ally declined"); return false; } if (this->num_pending_attacks >= 0x20) { this->ruler_server->error_code3 = -0x71; - log.debug("failed: too many pending attacks"); + log.debug_f("failed: too many pending attacks"); return false; } size_t attack_index = this->num_pending_attacks++; this->pending_attacks[attack_index] = *pa; - if (log.should_log(phosg::LogLevel::DEBUG)) { + if (log.should_log(phosg::LogLevel::L_DEBUG)) { string pa_str = this->pending_attacks[attack_index].str(this->shared_from_this()); - log.debug("set pending attack %zu: %s", attack_index, pa_str.c_str()); + log.debug_f("set pending attack {}: {}", attack_index, pa_str); } ps->set_action_cards_for_action_state(*pa); - log.debug("set action cards"); + log.debug_f("set action cards"); auto card = this->card_for_set_card_ref(this->send_6xB4x06_if_card_ref_invalid(pa->attacker_card_ref, 1)); if (card) { card->card_flags |= 0x400; @@ -1056,7 +1036,7 @@ uint32_t Server::get_random_raw() { if (this->options.opt_rand_stream) { this->options.opt_rand_stream->readx(&ret, sizeof(ret)); } else { - ret = random_from_optional_crypt(this->options.opt_rand_crypt); + ret = this->options.rand_crypt->next(); } if (this->battle_record && this->battle_record->writable()) { @@ -1740,20 +1720,20 @@ bool Server::update_registration_phase() { auto log = this->log_stack("update_registration_phase: "); if (this->setup_phase != SetupPhase::REGISTRATION) { - log.debug("setup_phase is not REGISTRATION"); + log.debug_f("setup_phase is not REGISTRATION"); return false; } if (this->map_and_rules->num_players == 0) { this->registration_phase = RegistrationPhase::AWAITING_NUM_PLAYERS; - log.debug("registration_phase set to AWAITING_NUM_PLAYERS"); + log.debug_f("registration_phase set to AWAITING_NUM_PLAYERS"); this->update_battle_state_flags_and_send_6xB4x03_if_needed(); return false; } if (this->map_and_rules->num_players != this->num_clients_present) { this->registration_phase = RegistrationPhase::AWAITING_PLAYERS; - log.debug("registration_phase set to AWAITING_PLAYERS"); + log.debug_f("registration_phase set to AWAITING_PLAYERS"); this->update_battle_state_flags_and_send_6xB4x03_if_needed(); return false; } @@ -1767,20 +1747,20 @@ bool Server::update_registration_phase() { if (num_team0_registered_players != this->map_and_rules->num_team0_players) { this->registration_phase = RegistrationPhase::AWAITING_DECKS; - log.debug("registration_phase set to AWAITING_DECKS"); + log.debug_f("registration_phase set to AWAITING_DECKS"); this->update_battle_state_flags_and_send_6xB4x03_if_needed(); return false; } this->registration_phase = RegistrationPhase::REGISTERED; this->update_battle_state_flags_and_send_6xB4x03_if_needed(); - log.debug("battle can begin"); + log.debug_f("battle can begin"); return true; } const unordered_map Server::subcommand_handlers({ - {0x0B, &Server::handle_CAx0B_mulligan_hand}, - {0x0C, &Server::handle_CAx0C_end_mulligan_phase}, + {0x0B, &Server::handle_CAx0B_redraw_initial_hand}, + {0x0C, &Server::handle_CAx0C_end_redraw_initial_hand_phase}, {0x0D, &Server::handle_CAx0D_end_non_action_phase}, {0x0E, &Server::handle_CAx0E_discard_card_from_hand}, {0x0F, &Server::handle_CAx0F_set_card_from_hand}, @@ -1809,7 +1789,7 @@ void Server::on_server_data_input(shared_ptr sender_c, const string& dat size_t expected_size = header.size * 4; if (expected_size < data.size()) { phosg::print_data(stderr, data); - throw runtime_error(phosg::string_printf("command is incomplete: expected %zX bytes, received %zX bytes", expected_size, data.size())); + throw runtime_error(std::format("command is incomplete: expected {:X} bytes, received {:X} bytes", expected_size, data.size())); } if (header.subcommand != 0xB3) { throw runtime_error("server data command is not 6xB3"); @@ -1835,7 +1815,7 @@ void Server::on_server_data_input(shared_ptr sender_c, const string& dat } } -void Server::handle_CAx0B_mulligan_hand(shared_ptr, const string& data) { +void Server::handle_CAx0B_redraw_initial_hand(shared_ptr, const string& data) { const auto& in_cmd = check_size_t(data); this->send_debug_command_received_message(in_cmd.client_id, in_cmd.header.subsubcommand, "REDRAW"); if (in_cmd.client_id >= 4) { @@ -1854,20 +1834,20 @@ void Server::handle_CAx0B_mulligan_hand(shared_ptr, const string& data) if (!ps) { error_code = -0x72; } else { - ps->do_mulligan(); + ps->redraw_initial_hand(); } } if (!this->options.is_nte() || (error_code == 0)) { G_ActionResult_Ep3_6xB4x1E out_cmd; - out_cmd.sequence_num = in_cmd.header.sequence_num.load(); + out_cmd.sequence_num = in_cmd.header.sequence_num; out_cmd.error_code = error_code; this->send(out_cmd); } this->send_debug_message_if_error_code_nonzero(in_cmd.client_id, error_code); } -void Server::handle_CAx0C_end_mulligan_phase(shared_ptr, const string& data) { +void Server::handle_CAx0C_end_redraw_initial_hand_phase(shared_ptr, const string& data) { const auto& in_cmd = check_size_t(data); this->send_debug_command_received_message(in_cmd.client_id, in_cmd.header.subsubcommand, "HAND READY"); if (in_cmd.client_id >= 4) { @@ -1898,13 +1878,13 @@ void Server::handle_CAx0C_end_mulligan_phase(shared_ptr, const string& d if (!ps) { error_code = -0x72; } else { - this->clients_done_in_mulligan_phase[in_cmd.client_id] = true; + this->clients_done_in_redraw_initial_hand_phase[in_cmd.client_id] = true; ps->assist_flags |= AssistFlag::READY_TO_END_PHASE; ps->update_hand_and_equip_state_and_send_6xB4x02_if_needed(); bool all_clients_ready = true; for (size_t z = 0; z < 4; z++) { - if (this->player_states[z] && !this->clients_done_in_mulligan_phase[z]) { + if (this->player_states[z] && !this->clients_done_in_redraw_initial_hand_phase[z]) { all_clients_ready = false; break; } @@ -2229,7 +2209,7 @@ void Server::handle_CAx14_update_deck_during_setup(shared_ptr, const str } } if (verify_error) { - throw runtime_error(phosg::string_printf("invalid deck: -0x%" PRIX32, verify_error)); + throw runtime_error(std::format("invalid deck: -0x{:X}", verify_error)); } if (!this->options.is_nte() && !(this->options.behavior_flags & BehaviorFlag::SKIP_D1_D2_REPLACE)) { this->ruler_server->replace_D1_D2_rank_cards_with_Attack(entry.card_ids); @@ -2573,7 +2553,7 @@ void Server::send_6xB6x41_to_all_clients() const { map_commands_by_language[c->language()] = this->prepare_6xB6x41_map_definition( this->last_chosen_map, c->language(), this->options.is_nte()); } - this->log().info("Sending %c version of map %08" PRIX32, char_for_language_code(c->language()), this->last_chosen_map->map_number); + this->log().info_f("Sending {} version of map {:08X}", char_for_language_code(c->language()), this->last_chosen_map->map_number); send_command(c, 0x6C, 0x00, map_commands_by_language[c->language()]); }; for (const auto& c : l->clients) { @@ -2782,7 +2762,7 @@ void Server::unknown_8023EEF4() { auto log = this->log_stack("unknown_8023EEF4: "); if (this->unknown_a14 >= 0x20) { - log.debug("unknown_a14 too large (0x%" PRIX32 ")", this->unknown_a14); + log.debug_f("unknown_a14 too large (0x{:X})", this->unknown_a14); return; } @@ -2791,34 +2771,34 @@ void Server::unknown_8023EEF4() { auto card = this->attack_cards[this->unknown_a14]; if (this->get_current_team_turn() == card->get_team_id()) { ActionState as = this->pending_attacks_with_cards[this->unknown_a14]; - if (log.should_log(phosg::LogLevel::DEBUG)) { - log.debug("card @%04hX #%04hX can attack", card->get_card_ref(), card->get_card_id()); + if (log.should_log(phosg::LogLevel::L_DEBUG)) { + log.debug_f("card @{:04X} #{:04X} can attack", card->get_card_ref(), card->get_card_id()); string as_str = as.str(this->shared_from_this()); - log.debug("as: %s", as_str.c_str()); + log.debug_f("as: {}", as_str); } if (is_nte) { this->replace_targets_due_to_destruction_nte(&as); } else { this->replace_targets_due_to_destruction_or_conditions(&as); } - if (log.should_log(phosg::LogLevel::DEBUG)) { + if (log.should_log(phosg::LogLevel::L_DEBUG)) { string as_str = as.str(this->shared_from_this()); - log.debug("as after target replacement: %s", as_str.c_str()); + log.debug_f("as after target replacement: {}", as_str); } if (this->any_target_exists_for_attack(as)) { - log.debug("as is valid"); + log.debug_f("as is valid"); break; } else { - log.debug("as is not valid"); + log.debug_f("as is not valid"); } } else { - log.debug("card @%04hX #%04hX cannot attack (wrong turn)", card->get_card_ref(), card->get_card_id()); + log.debug_f("card @{:04X} #{:04X} cannot attack (wrong turn)", card->get_card_ref(), card->get_card_id()); } this->unknown_a14++; } if (this->unknown_a14 < this->num_pending_attacks_with_cards) { - log.debug("a14 (%" PRIu32 ") < num_pending_attacks_with_cards (%" PRIu32 ")", this->unknown_a14, this->num_pending_attacks_with_cards); + log.debug_f("a14 ({}) < num_pending_attacks_with_cards ({})", this->unknown_a14, this->num_pending_attacks_with_cards); this->defense_list_ended_for_client.clear(false); G_UpdateAttackTargets_Ep3_6xB4x29 cmd; @@ -3142,13 +3122,13 @@ void Server::unknown_802402F4() { if (ps && (this->current_team_turn2 == ps->get_team_id())) { auto card = ps->get_sc_card(); if (card) { - log.debug("SC card has action chain"); + log.debug_f("SC card has action chain"); card->compute_action_chain_results(true, false); } for (size_t set_index = 0; set_index < 8; set_index++) { card = ps->get_set_card(set_index); if (card) { - log.debug("set card %zu has action chain", set_index); + log.debug_f("set card {} has action chain", set_index); card->compute_action_chain_results(true, false); } } diff --git a/src/Episode3/Server.hh b/src/Episode3/Server.hh index 17622be4..6f7929b6 100644 --- a/src/Episode3/Server.hh +++ b/src/Episode3/Server.hh @@ -73,7 +73,7 @@ public: std::shared_ptr map_index; uint32_t behavior_flags; std::shared_ptr opt_rand_stream; - std::shared_ptr opt_rand_crypt; + std::shared_ptr rand_crypt; std::shared_ptr tournament; std::array, 5> trap_card_ids; @@ -109,7 +109,7 @@ public: for (size_t z = 0; z < count; z++) { if (refs[z] != 0xFFFF) { std::string ref_str = this->debug_str_for_card_ref(refs[z]); - ret += phosg::string_printf("%zu:%s ", z, ref_str.c_str()); + ret += std::format("{}:{} ", z, ref_str); } } if (ret.size() > 1) { @@ -145,20 +145,23 @@ public: this->send(&cmd, cmd.header.size * 4, command, enable_masking); } void send(const void* data, size_t size, uint8_t command = 0xC9, bool enable_masking = true) const; - void send_commands_for_joining_spectator(Channel& ch) const; + void send_commands_for_joining_spectator(std::shared_ptr ch) const; void force_battle_result(uint8_t surrendered_client_id, bool set_winner); void force_replace_assist_card(uint8_t client_id, uint16_t card_id); void force_destroy_field_character(uint8_t client_id, size_t set_index); - __attribute__((format(printf, 2, 3))) void send_debug_message_printf(const char* fmt, ...) const; - __attribute__((format(printf, 2, 3))) void send_info_message_printf(const char* fmt, ...) const; - void send_debug_command_received_message( - uint8_t client_id, uint8_t subsubcommand, const char* description) const; - void send_debug_command_received_message( - uint8_t subsubcommand, const char* description) const; - void send_debug_message_if_error_code_nonzero( - uint8_t client_id, int32_t error_code) const; + template + void send_debug_message(std::format_string fmt, ArgTs&&... args) const { + auto l = this->lobby.lock(); + if (l && (this->options.behavior_flags & Episode3::BehaviorFlag::ENABLE_STATUS_MESSAGES)) { + send_text_message(l, std::format(std::forward>(fmt), std::forward(args)...)); + } + } + + void send_debug_command_received_message(uint8_t client_id, uint8_t subsubcommand, const char* description) const; + void send_debug_command_received_message(uint8_t subsubcommand, const char* description) const; + void send_debug_message_if_error_code_nonzero(uint8_t client_id, int32_t error_code) const; void send_6xB4x46() const; @@ -218,8 +221,8 @@ public: void update_battle_state_flags_and_send_6xB4x03_if_needed(bool always_send = false); bool update_registration_phase(); void on_server_data_input(std::shared_ptr sender_c, const std::string& data); - void handle_CAx0B_mulligan_hand(std::shared_ptr sender_c, const std::string& data); - void handle_CAx0C_end_mulligan_phase(std::shared_ptr sender_c, const std::string& data); + void handle_CAx0B_redraw_initial_hand(std::shared_ptr sender_c, const std::string& data); + void handle_CAx0C_end_redraw_initial_hand_phase(std::shared_ptr sender_c, const std::string& data); void handle_CAx0D_end_non_action_phase(std::shared_ptr sender_c, const std::string& data); void handle_CAx0E_discard_card_from_hand(std::shared_ptr sender_c, const std::string& data); void handle_CAx0F_set_card_from_hand(std::shared_ptr sender_c, const std::string& data); @@ -330,7 +333,7 @@ public: std::shared_ptr card_special; std::shared_ptr state_flags; std::array, 4> player_states; - parray clients_done_in_mulligan_phase; + parray clients_done_in_redraw_initial_hand_phase; uint32_t num_pending_attacks_with_cards; bcarray, 0x20> attack_cards; bcarray pending_attacks_with_cards; diff --git a/src/Episode3/Tournament.cc b/src/Episode3/Tournament.cc index c140b856..0cb22b85 100644 --- a/src/Episode3/Tournament.cc +++ b/src/Episode3/Tournament.cc @@ -3,7 +3,9 @@ #include #include "../CommandFormats.hh" +#include "../GameServer.hh" #include "../SendCommands.hh" +#include "../ServerState.hh" using namespace std; @@ -49,16 +51,16 @@ string Tournament::Team::str() const { num_com_players += player.is_com(); } - string ret = phosg::string_printf("[Team/%zu %s %zuH/%zuC/%zuP name=%s pass=%s rounds=%zu", + string ret = std::format("[Team/{} {} {}H/{}C/{}P name={} pass={} rounds={}", this->index, this->is_active ? "active" : "inactive", - num_human_players, num_com_players, this->max_players, this->name.c_str(), - this->password.c_str(), this->num_rounds_cleared); + num_human_players, num_com_players, this->max_players, this->name, + this->password, this->num_rounds_cleared); for (const auto& player : this->players) { if (player.is_human()) { if (player.player_name.empty()) { - ret += phosg::string_printf(" %08" PRIX32, player.account_id); + ret += std::format(" {:08X}", player.account_id); } else { - ret += phosg::string_printf(" %08" PRIX32 " (%s)", player.account_id, player.player_name.c_str()); + ret += std::format(" {:08X} ({})", player.account_id, player.player_name); } } } @@ -206,7 +208,7 @@ Tournament::Match::Match( string Tournament::Match::str() const { string winner_str = this->winner_team ? this->winner_team->str() : "(none)"; - return phosg::string_printf("[Match round=%zu winner=%s]", this->round_num, winner_str.c_str()); + return std::format("[Match round={} winner={}]", this->round_num, winner_str); } bool Tournament::Match::resolve_if_skippable() { @@ -318,7 +320,7 @@ Tournament::Tournament( const Rules& rules, size_t num_teams, uint8_t flags) - : log(phosg::string_printf("[Tournament:%s] ", name.c_str())), + : log(std::format("[Tournament:{}] ", name)), map_index(map_index), com_deck_index(com_deck_index), name(name), @@ -343,7 +345,7 @@ Tournament::Tournament( shared_ptr map_index, shared_ptr com_deck_index, const phosg::JSON& json) - : log(phosg::string_printf("[Tournament:%s] ", json.get_string("name").c_str())), + : log(std::format("[Tournament:{}] ", json.get_string("name"))), map_index(map_index), com_deck_index(com_deck_index), source_json(json), @@ -665,7 +667,7 @@ void Tournament::start() { auto m = this->zero_round_matches[z]; auto t = m->winner_team; if (t->name.empty()) { - t->name = has_com_teams ? phosg::string_printf("COM:%zu", z) : "(no entrant)"; + t->name = has_com_teams ? std::format("COM:{}", z) : "(no entrant)"; } for (const auto& player : t->players) { if (player.is_com()) { @@ -718,7 +720,7 @@ void Tournament::send_all_state_updates_on_deletion() const { } string Tournament::bracket_str() const { - string ret = phosg::string_printf("Tournament \"%s\"\n", this->name.c_str()); + string ret = std::format("Tournament \"{}\"\n", this->name); function, size_t)> add_match = [&](shared_ptr m, size_t indent_level) -> void { ret.append(2 * indent_level, ' '); @@ -738,16 +740,16 @@ string Tournament::bracket_str() const { auto en_vm = this->map->version(1); if (en_vm) { string map_name = en_vm->map->name.decode(en_vm->language); - ret += phosg::string_printf(" Map: %08" PRIX32 " (%s)\n", this->map->map_number, map_name.c_str()); + ret += std::format(" Map: {:08X} ({})\n", this->map->map_number, map_name); } else { - ret += phosg::string_printf(" Map: %08" PRIX32 "\n", this->map->map_number); + ret += std::format(" Map: {:08X}\n", this->map->map_number); } string rules_str = this->rules.str(); - ret += phosg::string_printf(" Rules: %s\n", rules_str.c_str()); - ret += phosg::string_printf(" Structure: %s, %zu entries\n", (this->flags & Flag::IS_2V2) ? "2v2" : "1v1", this->num_teams); - ret += phosg::string_printf(" COM teams: %s\n", (this->flags & Flag::HAS_COM_TEAMS) ? "allowed" : "forbidden"); - ret += phosg::string_printf(" Shuffle entries: %s\n", (this->flags & Flag::SHUFFLE_ENTRIES) ? "yes" : "no"); - ret += phosg::string_printf(" Resize on start: %s\n", (this->flags & Flag::RESIZE_ON_START) ? "yes" : "no"); + ret += std::format(" Rules: {}\n", rules_str); + ret += std::format(" Structure: {}, {} entries\n", (this->flags & Flag::IS_2V2) ? "2v2" : "1v1", this->num_teams); + ret += std::format(" COM teams: {}\n", (this->flags & Flag::HAS_COM_TEAMS) ? "allowed" : "forbidden"); + ret += std::format(" Shuffle entries: {}\n", (this->flags & Flag::SHUFFLE_ENTRIES) ? "yes" : "no"); + ret += std::format(" Resize on start: {}\n", (this->flags & Flag::RESIZE_ON_START) ? "yes" : "no"); switch (this->current_state) { case State::REGISTRATION: ret += " State: REGISTRATION\n"; @@ -770,13 +772,13 @@ string Tournament::bracket_str() const { ret += " Teams:\n"; for (const auto& team : this->teams) { string team_str = team->str(); - ret += phosg::string_printf(" %s\n", team_str.c_str()); + ret += std::format(" {}\n", team_str); } } else { ret += " Pending matches:\n"; for (const auto& match : this->pending_matches) { string match_str = match->str(); - ret += phosg::string_printf(" %s\n", match_str.c_str()); + ret += std::format(" {}\n", match_str); } } @@ -940,8 +942,12 @@ void TournamentIndex::link_client(shared_ptr c) { } void TournamentIndex::link_all_clients(std::shared_ptr s) { - for (const auto& c_it : s->channel_to_client) { - this->link_client(c_it.second); + // This can be called before the game server exists, so do nothing in that + // case + if (s->game_server) { + for (const auto& c : s->game_server->all_clients()) { + this->link_client(c); + } } } diff --git a/src/Episode3/Tournament.hh b/src/Episode3/Tournament.hh index 603f4fbb..b7038de1 100644 --- a/src/Episode3/Tournament.hh +++ b/src/Episode3/Tournament.hh @@ -1,6 +1,5 @@ #pragma once -#include #include #include diff --git a/src/EventUtils.cc b/src/EventUtils.cc deleted file mode 100644 index 802a56de..00000000 --- a/src/EventUtils.cc +++ /dev/null @@ -1,62 +0,0 @@ -#include "EventUtils.hh" - -#include -#include - -#include -#include -#include -#include - -using namespace std; - -static void dispatch_forward_to_event_thread(evutil_socket_t, short, void* ctx) { - auto* fn = reinterpret_cast*>(ctx); - (*fn)(); - delete fn; -} - -void forward_to_event_thread(shared_ptr base, function&& fn) { - struct timeval tv = {0, 0}; - function* new_fn = new function(std::move(fn)); - event_base_once(base.get(), -1, EV_TIMEOUT, dispatch_forward_to_event_thread, new_fn, &tv); -} - -template <> -void call_on_event_thread(shared_ptr base, function&& compute) { - bool succeeded = false; - string exc_what; - mutex ret_lock; - condition_variable ret_cv; - unique_lock g(ret_lock); - forward_to_event_thread(base, [&]() -> void { - lock_guard g(ret_lock); - try { - compute(); - succeeded = true; - } catch (const exception& e) { - exc_what = e.what(); - } - ret_cv.notify_one(); - }); - ret_cv.wait(g); - if (!succeeded) { - throw runtime_error(exc_what); - } -} - -string evbuffer_remove_str(struct evbuffer* buf, ssize_t size) { - if (!buf) { - return ""; - } - if (size < 0) { - size = static_cast(evbuffer_get_length(buf)); - } - string ret(size, '\0'); - ssize_t bytes_removed = evbuffer_remove(buf, ret.data(), ret.size()); - if (bytes_removed < 0) { - throw std::runtime_error("can\'t remove data from buffer"); - } - ret.resize(bytes_removed); - return ret; -} diff --git a/src/EventUtils.hh b/src/EventUtils.hh deleted file mode 100644 index ddbaf977..00000000 --- a/src/EventUtils.hh +++ /dev/null @@ -1,45 +0,0 @@ -#pragma once - -#include - -#include -#include -#include -#include -#include -#include -#include - -// 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 base, std::function&& 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 -T call_on_event_thread(std::shared_ptr base, std::function&& compute) { - std::optional ret; - std::string exc_what; - std::mutex ret_lock; - std::condition_variable ret_cv; - std::unique_lock g(ret_lock); - forward_to_event_thread(base, [&]() -> void { - std::lock_guard g(ret_lock); - try { - ret = compute(); - } catch (const std::exception& e) { - exc_what = e.what(); - } - ret_cv.notify_one(); - }); - ret_cv.wait(g); - if (!ret.has_value()) { - throw std::runtime_error(exc_what); - } - return ret.value(); -} - -template <> -void call_on_event_thread(std::shared_ptr base, std::function&& compute); - -std::string evbuffer_remove_str(struct evbuffer* buf, ssize_t size = -1); diff --git a/src/FunctionCompiler.cc b/src/FunctionCompiler.cc index d8699e8a..ee27e888 100644 --- a/src/FunctionCompiler.cc +++ b/src/FunctionCompiler.cc @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -19,16 +20,6 @@ using namespace std; -static bool is_function_compiler_available = true; - -bool function_compiler_available() { - return is_function_compiler_available; -} - -void set_function_compiler_available(bool is_available) { - is_function_compiler_available = is_available; -} - const char* name_for_architecture(CompiledFunctionCode::Architecture arch) { switch (arch) { case CompiledFunctionCode::Architecture::POWERPC: @@ -149,11 +140,11 @@ shared_ptr compile_function_code( } // Look in the function directory first, then the system directory - string asm_filename = phosg::string_printf("%s/%s.%s.inc.s", function_directory.c_str(), name.c_str(), arch_name_token); - if (!phosg::isfile(asm_filename)) { - asm_filename = phosg::string_printf("%s/%s.%s.inc.s", system_directory.c_str(), name.c_str(), arch_name_token); + string asm_filename = std::format("{}/{}.{}.inc.s", function_directory, name, arch_name_token); + if (!std::filesystem::is_regular_file(asm_filename)) { + asm_filename = std::format("{}/{}.{}.inc.s", system_directory, name, arch_name_token); } - if (phosg::isfile(asm_filename)) { + if (std::filesystem::is_regular_file(asm_filename)) { if (!get_include_stack.emplace(name).second) { throw runtime_error("mutual recursion between includes: " + name); } @@ -176,11 +167,11 @@ shared_ptr compile_function_code( } string bin_filename = function_directory + "/" + name + ".inc.bin"; - if (phosg::isfile(bin_filename)) { + if (std::filesystem::is_regular_file(bin_filename)) { return phosg::load_file(bin_filename); } bin_filename = system_directory + "/" + name + ".inc.bin"; - if (phosg::isfile(bin_filename)) { + if (std::filesystem::is_regular_file(bin_filename)) { return phosg::load_file(bin_filename); } throw runtime_error("data not found for include: " + name + " (from " + asm_filename + " or " + bin_filename + ")"); @@ -217,7 +208,7 @@ shared_ptr compile_function_code( set reloc_indexes; for (const auto& it : ret->label_offsets) { - if (phosg::starts_with(it.first, "reloc")) { + if (it.first.starts_with("reloc")) { reloc_indexes.emplace(it.second / 4); } } @@ -242,33 +233,30 @@ shared_ptr compile_function_code( } FunctionCodeIndex::FunctionCodeIndex(const string& directory) { - if (!function_compiler_available()) { - function_compiler_log.info("Function compiler is not available"); - return; - } - - string system_dir_path = phosg::ends_with(directory, "/") ? (directory + "System") : (directory + "/System"); + string system_dir_path = directory.ends_with("/") ? (directory + "System") : (directory + "/System"); uint32_t next_menu_item_id = 1; - for (const auto& subdir_name : phosg::list_directory_sorted(directory)) { - string subdir_path = phosg::ends_with(directory, "/") ? (directory + subdir_name) : (directory + "/" + subdir_name); - if (!phosg::isdir(subdir_path)) { - function_compiler_log.warning("Skipping %s (not a directory)", subdir_name.c_str()); + for (const auto& item : std::filesystem::directory_iterator(directory)) { + string subdir_name = item.path().filename().string(); + string subdir_path = directory.ends_with("/") ? (directory + subdir_name) : (directory + "/" + subdir_name); + if (!std::filesystem::is_directory(subdir_path)) { + function_compiler_log.warning_f("Skipping {} (not a directory)", subdir_name); continue; } - for (const auto& filename : phosg::list_directory_sorted(subdir_path)) { + for (const auto& item : std::filesystem::directory_iterator(subdir_path)) { + string filename = item.path().filename().string(); try { - if (!phosg::ends_with(filename, ".s")) { + if (!filename.ends_with(".s")) { continue; } string name = filename.substr(0, filename.size() - 2); - if (phosg::ends_with(name, ".inc")) { + if (name.ends_with(".inc")) { continue; } - bool is_patch = phosg::ends_with(name, ".patch"); + bool is_patch = name.ends_with(".patch"); if (is_patch) { name.resize(name.size() - 6); } @@ -277,15 +265,15 @@ FunctionCodeIndex::FunctionCodeIndex(const string& directory) { CompiledFunctionCode::Architecture arch = CompiledFunctionCode::Architecture::UNKNOWN; uint32_t specific_version = 0; string short_name = name; - if (phosg::ends_with(name, ".ppc")) { + if (name.ends_with(".ppc")) { arch = CompiledFunctionCode::Architecture::POWERPC; name.resize(name.size() - 4); short_name = name; - } else if (phosg::ends_with(name, ".x86")) { + } else if (name.ends_with(".x86")) { arch = CompiledFunctionCode::Architecture::X86; name.resize(name.size() - 4); short_name = name; - } else if (phosg::ends_with(name, ".sh4")) { + } else if (name.ends_with(".sh4")) { arch = CompiledFunctionCode::Architecture::SH4; name.resize(name.size() - 4); short_name = name; @@ -314,8 +302,8 @@ FunctionCodeIndex::FunctionCodeIndex(const string& directory) { auto code = compile_function_code(arch, subdir_path, system_dir_path, name, text); if (code->index != 0) { if (!this->index_to_function.emplace(code->index, code).second) { - throw runtime_error(phosg::string_printf( - "duplicate function index: %08" PRIX32, code->index)); + throw runtime_error(std::format( + "duplicate function index: {:08X}", code->index)); } } code->specific_version = specific_version; @@ -327,16 +315,16 @@ FunctionCodeIndex::FunctionCodeIndex(const string& directory) { this->menu_item_id_and_specific_version_to_patch_function.emplace( static_cast(code->menu_item_id) << 32 | specific_version, code); this->name_and_specific_version_to_patch_function.emplace( - phosg::string_printf("%s-%08" PRIX32, short_name.c_str(), specific_version), code); + std::format("{}-{:08X}", short_name, specific_version), code); } - string index_prefix = code->index ? phosg::string_printf("%02X => ", code->index) : ""; - string patch_prefix = is_patch ? phosg::string_printf("[%08" PRIX32 "/%08" PRIX32 "] ", code->menu_item_id, code->specific_version) : ""; - function_compiler_log.info("Compiled function %s%s%s (%s)", - index_prefix.c_str(), patch_prefix.c_str(), name.c_str(), name_for_architecture(code->arch)); + string index_prefix = code->index ? std::format("{:02X} => ", code->index) : ""; + string patch_prefix = is_patch ? std::format("[{:08X}/{:08X}] ", code->menu_item_id, code->specific_version) : ""; + function_compiler_log.info_f("Compiled function {}{}{} ({})", + index_prefix, patch_prefix, name, name_for_architecture(code->arch)); } catch (const exception& e) { - function_compiler_log.warning("Failed to compile function %s: %s", filename.c_str(), e.what()); + function_compiler_log.warning_f("Failed to compile function {}: {}", filename, e.what()); } } } @@ -344,13 +332,13 @@ FunctionCodeIndex::FunctionCodeIndex(const string& directory) { shared_ptr FunctionCodeIndex::patch_switches_menu( uint32_t specific_version, const std::unordered_set& auto_patches_enabled) const { - auto suffix = phosg::string_printf("-%08" PRIX32, specific_version); + auto suffix = std::format("-{:08X}", specific_version); auto ret = make_shared(MenuID::PATCH_SWITCHES, "Patches"); ret->items.emplace_back(PatchesMenuItemID::GO_BACK, "Go back", "Return to the\nmain menu", 0); for (const auto& it : this->name_and_specific_version_to_patch_function) { const auto& fn = it.second; - if (fn->hide_from_patches_menu || !phosg::ends_with(it.first, suffix)) { + if (fn->hide_from_patches_menu || !it.first.ends_with(suffix)) { continue; } string name; @@ -374,16 +362,12 @@ bool FunctionCodeIndex::patch_menu_empty(uint32_t specific_version) const { std::shared_ptr FunctionCodeIndex::get_patch( const std::string& name, uint32_t specific_version) const { return this->name_and_specific_version_to_patch_function.at( - phosg::string_printf("%s-%08" PRIX32, name.c_str(), specific_version)); + std::format("{}-{:08X}", name, specific_version)); } DOLFileIndex::DOLFileIndex(const string& directory) { - if (!function_compiler_available()) { - function_compiler_log.info("Function compiler is not available"); - return; - } - if (!phosg::isdir(directory)) { - function_compiler_log.info("DOL file directory is missing"); + if (!std::filesystem::is_directory(directory)) { + function_compiler_log.info_f("DOL file directory is missing"); return; } @@ -392,9 +376,10 @@ DOLFileIndex::DOLFileIndex(const string& directory) { menu->items.emplace_back(ProgramsMenuItemID::GO_BACK, "Go back", "Return to the\nmain menu", 0); uint32_t next_menu_item_id = 0; - for (const auto& filename : phosg::list_directory_sorted(directory)) { - bool is_dol = phosg::ends_with(filename, ".dol"); - bool is_compressed_dol = phosg::ends_with(filename, ".dol.prs"); + for (const auto& item : std::filesystem::directory_iterator(directory)) { + string filename = item.path().filename().string(); + bool is_dol = filename.ends_with(".dol"); + bool is_compressed_dol = filename.ends_with(".dol.prs"); if (!is_dol && !is_compressed_dol) { continue; } @@ -423,10 +408,10 @@ DOLFileIndex::DOLFileIndex(const string& directory) { string compressed_size_str = phosg::format_size(file_data.size()); string decompressed_size_str = phosg::format_size(decompressed_size); - function_compiler_log.info("Loaded compressed DOL file %s (%s -> %s)", - dol->name.c_str(), compressed_size_str.c_str(), decompressed_size_str.c_str()); - description = phosg::string_printf("$C6%s$C7\n%s\n%s (orig)", - dol->name.c_str(), compressed_size_str.c_str(), decompressed_size_str.c_str()); + function_compiler_log.info_f("Loaded compressed DOL file {} ({} -> {})", + dol->name, compressed_size_str, decompressed_size_str); + description = std::format("$C6{}$C7\n{}\n{} (orig)", + dol->name, compressed_size_str, decompressed_size_str); } else { phosg::StringWriter w; @@ -439,8 +424,8 @@ DOLFileIndex::DOLFileIndex(const string& directory) { dol->data = std::move(w.str()); string size_str = phosg::format_size(dol->data.size()); - function_compiler_log.info("Loaded DOL file %s (%s)", filename.c_str(), size_str.c_str()); - description = phosg::string_printf("$C6%s$C7\n%s", dol->name.c_str(), size_str.c_str()); + function_compiler_log.info_f("Loaded DOL file {} ({})", filename, size_str); + description = std::format("$C6{}$C7\n{}", dol->name, size_str); } this->name_to_file.emplace(dol->name, dol); @@ -449,7 +434,7 @@ DOLFileIndex::DOLFileIndex(const string& directory) { menu->items.emplace_back(dol->menu_item_id, dol->name, description, MenuItem::Flag::REQUIRES_SEND_FUNCTION_CALL_RUNS_CODE); } catch (const exception& e) { - function_compiler_log.warning("Failed to load DOL file %s: %s", filename.c_str(), e.what()); + function_compiler_log.warning_f("Failed to load DOL file {}: {}", filename, e.what()); } } } diff --git a/src/FunctionCompiler.hh b/src/FunctionCompiler.hh index cfadfe90..553b4236 100644 --- a/src/FunctionCompiler.hh +++ b/src/FunctionCompiler.hh @@ -11,9 +11,6 @@ #include "Menu.hh" -bool function_compiler_available(); -void set_function_compiler_available(bool is_available); - // TODO: Support x86 and SH4 function calls in the future. Currently we only // support PPC32 because I haven't written an appropriate x86 assembler yet. diff --git a/src/GameServer.cc b/src/GameServer.cc new file mode 100644 index 00000000..719995bb --- /dev/null +++ b/src/GameServer.cc @@ -0,0 +1,196 @@ +#include "GameServer.hh" + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "Loggers.hh" +#include "PSOProtocol.hh" +#include "ReceiveCommands.hh" + +using namespace std; +using namespace std::placeholders; + +GameServer::GameServer(shared_ptr state) + : Server(state->io_context, "[GameServer] "), state(state) {} + +void GameServer::listen( + const std::string& name, + const string& addr, + uint16_t port, + Version version, + ServerBehavior behavior) { + if (port == 0) { + throw std::runtime_error("Listening port cannot be zero"); + } + + asio::ip::address asio_addr = addr.empty() ? asio::ip::address_v4::any() : asio::ip::make_address(addr); + auto sock = make_shared(); + sock->name = name; + sock->endpoint = asio::ip::tcp::endpoint(asio_addr, port); + sock->version = version; + sock->behavior = behavior; + this->add_socket(std::move(sock)); +} + +shared_ptr GameServer::connect_channel(shared_ptr ch, uint16_t port, ServerBehavior initial_state) { + auto c = make_shared(this->shared_from_this(), ch, initial_state); + + this->log.info_f("Client connected: C-{:X} via TSI-{}-{}-{}", + c->id, port, phosg::name_for_enum(ch->version), phosg::name_for_enum(initial_state)); + + asio::co_spawn(*this->io_context, this->handle_connected_client(c), asio::detached); + return c; +} + +shared_ptr GameServer::get_client() const { + if (this->clients.empty()) { + throw runtime_error("no clients on game server"); + } + if (this->clients.size() > 1) { + throw runtime_error("multiple clients on game server"); + } + return *this->clients.begin(); +} + +vector> GameServer::get_clients_by_identifier(const string& ident) const { + int64_t account_id_hex = -1; + int64_t account_id_dec = -1; + try { + account_id_dec = stoul(ident, nullptr, 10); + } catch (const invalid_argument&) { + } + try { + account_id_hex = stoul(ident, nullptr, 16); + } catch (const invalid_argument&) { + } + + // TODO: It's kind of not great that we do a linear search here, but this is + // only used in the shell, so it should be pretty rare. + vector> results; + for (const auto& c : this->clients) { + if (c->login && c->login->account->account_id == account_id_hex) { + results.emplace_back(c); + continue; + } + if (c->login && c->login->account->account_id == account_id_dec) { + results.emplace_back(c); + continue; + } + if (c->login && c->login->xb_license && c->login->xb_license->gamertag == ident) { + results.emplace_back(c); + continue; + } + if (c->login && c->login->bb_license && c->login->bb_license->username == ident) { + results.emplace_back(c); + continue; + } + + auto p = c->character(false, false); + if (p && p->disp.name.eq(ident, p->inventory.language)) { + results.emplace_back(c); + continue; + } + + if (c->channel->name == ident) { + results.emplace_back(c); + continue; + } + if (c->channel->name.starts_with(ident + " ")) { + results.emplace_back(c); + continue; + } + } + + return results; +} + +shared_ptr GameServer::create_client(shared_ptr listen_sock, asio::ip::tcp::socket&& client_sock) { + uint32_t addr = ipv4_addr_for_asio_addr(client_sock.remote_endpoint().address()); + if (this->state->banned_ipv4_ranges->check(addr)) { + client_sock.close(); + return nullptr; + } + + auto channel = SocketChannel::create( + this->io_context, + make_unique(std::move(client_sock)), + listen_sock->version, + 1, + "", + phosg::TerminalFormat::FG_YELLOW, + phosg::TerminalFormat::FG_GREEN); + auto c = make_shared(this->shared_from_this(), channel, listen_sock->behavior); + this->log.info_f("Client connected: C-{:X} via {}", c->id, listen_sock->name); + + return c; +} + +asio::awaitable GameServer::handle_client_command(shared_ptr c, unique_ptr msg) { + try { + co_await on_command(c, std::move(msg)); + } catch (const exception& e) { + this->log.warning_f("Error processing client command: {}", e.what()); + c->channel->disconnect(); + } +} + +asio::awaitable GameServer::handle_client(shared_ptr c) { + auto g = phosg::on_close_scope(std::bind(&Client::cancel_pending_promises, c.get())); + + try { + co_await on_connect(c); + } catch (const exception& e) { + this->log.warning_f("Error in client initialization: {}", e.what()); + c->channel->disconnect(); + } + + while (c->channel->connected()) { + auto msg = std::make_unique(co_await c->channel->recv()); + asio::co_spawn(co_await asio::this_coro::executor, this->handle_client_command(c, std::move(msg)), asio::detached); + } +} + +asio::awaitable GameServer::destroy_client(std::shared_ptr c) { + this->log.info_f("Running cleanup tasks for {}", c->channel->name); + + // The client may not actually be disconnected yet if an uncaught exception + // occurred in a handler task + c->channel->disconnect(); + + // Close the proxy session, if any + if (c->proxy_session) { + if (c->proxy_session->server_channel) { + c->proxy_session->server_channel->disconnect(); + } + c->proxy_session.reset(); + } + + try { + co_await on_disconnect(c); + } catch (const exception& e) { + this->log.warning_f("Error during client disconnect cleanup: {}", e.what()); + } + + // Note: It's important to move the disconnect hooks out of the client here + // because the hooks could modify c->disconnect_hooks while it's being + // iterated here, which would invalidate these iterators. + unordered_map> hooks = std::move(c->disconnect_hooks); + for (auto h_it : hooks) { + try { + h_it.second(); + } catch (const exception& e) { + c->log.warning_f("Disconnect hook {} failed: {}", h_it.first, e.what()); + } + } +} diff --git a/src/GameServer.hh b/src/GameServer.hh new file mode 100644 index 00000000..714e87bb --- /dev/null +++ b/src/GameServer.hh @@ -0,0 +1,48 @@ +#pragma once + +#include +#include +#include +#include +#include + +#include "Client.hh" +#include "Server.hh" +#include "ServerState.hh" + +struct GameServerSocket : ServerSocket { + Version version; + ServerBehavior behavior; +}; + +class GameServer + : public Server, + public std::enable_shared_from_this { +public: + GameServer() = delete; + GameServer(const GameServer&) = delete; + GameServer(GameServer&&) = delete; + explicit GameServer(std::shared_ptr state); + virtual ~GameServer() = default; + + void listen(const std::string& name, const std::string& addr, uint16_t port, Version version, ServerBehavior initial_state); + + std::shared_ptr connect_channel(std::shared_ptr ch, uint16_t port, ServerBehavior initial_state); + + std::shared_ptr get_client() const; + std::vector> get_clients_by_identifier(const std::string& ident) const; + + inline std::shared_ptr get_state() const { + return this->state; + } + +protected: + std::shared_ptr state; + + asio::awaitable handle_client_command(std::shared_ptr c, std::unique_ptr msg); + + [[nodiscard]] virtual std::shared_ptr create_client( + std::shared_ptr listen_sock, asio::ip::tcp::socket&& client_sock); + virtual asio::awaitable handle_client(std::shared_ptr c); + virtual asio::awaitable destroy_client(std::shared_ptr c); +}; diff --git a/src/HTTPServer.cc b/src/HTTPServer.cc index 61dec197..1cb9cc8a 100644 --- a/src/HTTPServer.cc +++ b/src/HTTPServer.cc @@ -1,8 +1,5 @@ #include "HTTPServer.hh" -#include -#include -#include #include #include @@ -10,489 +7,58 @@ #include #include -#include "EventUtils.hh" +#include "GameServer.hh" #include "IPStackSimulator.hh" #include "Loggers.hh" -#include "ProxyServer.hh" #include "Revision.hh" #include "Server.hh" #include "ShellCommands.hh" using namespace std; -const unordered_map HTTPServer::explanation_for_response_code({ - {100, "Continue"}, - {101, "Switching Protocols"}, - {102, "Processing"}, - {200, "OK"}, - {201, "Created"}, - {202, "Accepted"}, - {203, "Non-Authoritative Information"}, - {204, "No Content"}, - {205, "Reset Content"}, - {206, "Partial Content"}, - {207, "Multi-Status"}, - {208, "Already Reported"}, - {226, "IM Used"}, - {300, "Multiple Choices"}, - {301, "Moved Permanently"}, - {302, "Found"}, - {303, "See Other"}, - {304, "Not Modified"}, - {305, "Use Proxy"}, - {307, "Temporary Redirect"}, - {308, "Permanent Redirect"}, - {400, "Bad Request"}, - {401, "Unathorized"}, - {402, "Payment Required"}, - {403, "Forbidden"}, - {404, "Not Found"}, - {405, "Method Not Allowed"}, - {406, "Not Acceptable"}, - {407, "Proxy Authentication Required"}, - {408, "Request Timeout"}, - {409, "Conflict"}, - {410, "Gone"}, - {411, "Length Required"}, - {412, "Precondition Failed"}, - {413, "Request Entity Too Large"}, - {414, "Request-URI Too Long"}, - {415, "Unsupported Media Type"}, - {416, "Requested Range Not Satisfiable"}, - {417, "Expectation Failed"}, - {418, "I\'m a Teapot"}, - {420, "Enhance Your Calm"}, - {422, "Unprocessable Entity"}, - {423, "Locked"}, - {424, "Failed Dependency"}, - {426, "Upgrade Required"}, - {428, "Precondition Required"}, - {429, "Too Many Requests"}, - {431, "Request Header Fields Too Large"}, - {444, "No Response"}, - {449, "Retry With"}, - {451, "Unavailable For Legal Reasons"}, - {500, "Internal Server Error"}, - {501, "Not Implemented"}, - {502, "Bad Gateway"}, - {503, "Service Unavailable"}, - {504, "Gateway Timeout"}, - {505, "HTTP Version Not Supported"}, - {506, "Variant Also Negotiates"}, - {507, "Insufficient Storage"}, - {508, "Loop Detected"}, - {509, "Bandwidth Limit Exceeded"}, - {510, "Not Extended"}, - {511, "Network Authentication Required"}, - {598, "Network Read Timeout Error"}, - {599, "Network Connect Timeout Error"}, -}); +HTTPServer::HTTPServer(shared_ptr state) + : AsyncHTTPServer(state->io_context, "[HTTPServer] "), state(state) {} -HTTPServer::http_error::http_error(int code, const string& what) - : runtime_error(what), - code(code) {} +asio::awaitable HTTPServer::send_rare_drop_notification(shared_ptr message) { + if (!this->rare_drop_subscribers.empty()) { + string data = message->serialize(); -void HTTPServer::send_response(struct evhttp_request* req, int code, const char* content_type, struct evbuffer* b) { - struct evkeyvalq* headers = evhttp_request_get_output_headers(req); - evhttp_add_header(headers, "Content-Type", content_type); - evhttp_add_header(headers, "Server", "newserv"); - evhttp_send_reply(req, code, explanation_for_response_code.at(code), b); -} + // Make a copy of the rare drop subscribers set, so we can guarantee that + // the client objects are all valid until this coroutine returns + unordered_set> subscribers = this->rare_drop_subscribers; -void HTTPServer::send_response(struct evhttp_request* req, int code, const char* content_type, const char* fmt, ...) { - unique_ptr out_buffer(evbuffer_new(), evbuffer_free); - va_list va; - va_start(va, fmt); - evbuffer_add_vprintf(out_buffer.get(), fmt, va); - va_end(va); - HTTPServer::send_response(req, code, content_type, out_buffer.get()); -} - -unordered_multimap HTTPServer::parse_url_params(const string& query) { - unordered_multimap params; - if (query.empty()) { - return params; - } - for (auto it : phosg::split(query, '&')) { - size_t first_equals = it.find('='); - if (first_equals != string::npos) { - string value(it, first_equals + 1); - - size_t write_offset = 0, read_offset = 0; - for (; read_offset < value.size(); write_offset++) { - if ((value[read_offset] == '%') && (read_offset < value.size() - 2)) { - value[write_offset] = - static_cast(phosg::value_for_hex_char(value[read_offset + 1]) << 4) | - static_cast(phosg::value_for_hex_char(value[read_offset + 2])); - read_offset += 3; - } else if (value[write_offset] == '+') { - value[write_offset] = ' '; - read_offset++; - } else { - value[write_offset] = value[read_offset]; - read_offset++; - } + size_t expected_results = subscribers.size(); + AsyncPromise complete_promise; + auto fn = [this, &data, &expected_results, &complete_promise](shared_ptr c) -> asio::awaitable { + try { + co_await c->send_websocket_message(data); + } catch (const std::exception& e) { + auto remote_s = str_for_endpoint(c->r.get_socket().remote_endpoint()); + this->log.info_f("Failed to send WebSocket message to {}: {}", remote_s, e.what()); } - value.resize(write_offset); - - params.emplace(piecewise_construct, forward_as_tuple(it, 0, first_equals), forward_as_tuple(value)); - } else { - params.emplace(it, ""); - } - } - return params; -} - -unordered_map HTTPServer::parse_url_params_unique(const string& query) { - unordered_map ret; - for (const auto& it : HTTPServer::parse_url_params(query)) { - ret.emplace(it.first, std::move(it.second)); - } - return ret; -} - -const string& HTTPServer::get_url_param( - const unordered_multimap& params, const string& key, const string* _default) { - - auto range = params.equal_range(key); - if (range.first == range.second) { - if (!_default) { - throw out_of_range("URL parameter " + key + " not present"); - } - return *_default; - } - - return range.first->second; -} - -HTTPServer::HTTPServer(shared_ptr state, shared_ptr shared_base) - : state(state) { - if (!shared_base) { - this->base.reset(event_base_new(), event_base_free); - } else { - this->base = shared_base; - } - this->http.reset(evhttp_new(this->base.get()), evhttp_free); - evhttp_set_gencb(this->http.get(), this->dispatch_handle_request, this); - if (!shared_base) { - this->th = thread(&HTTPServer::thread_fn, this); - } -} - -void HTTPServer::listen(const string& socket_path) { - int fd = phosg::listen(socket_path, 0, SOMAXCONN); - server_log.info("Listening on Unix socket %s on fd %d (HTTP)", socket_path.c_str(), fd); - this->add_socket(fd); -} - -void HTTPServer::listen(const string& addr, int port) { - if (port == 0) { - this->listen(addr); - } else { - int fd = phosg::listen(addr, port, SOMAXCONN); - string netloc_str = phosg::render_netloc(addr, port); - server_log.info("Listening on TCP interface %s on fd %d (HTTP)", netloc_str.c_str(), fd); - this->add_socket(fd); - } -} - -void HTTPServer::listen(int port) { - this->listen("", port); -} - -void HTTPServer::add_socket(int fd) { - evhttp_accept_socket(this->http.get(), fd); -} - -void HTTPServer::schedule_stop() { - event_base_loopexit(this->base.get(), nullptr); -} - -void HTTPServer::wait_for_stop() { - 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(phosg::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::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 = phosg::base64_encode(phosg::SHA1(sec_websocket_accept_data).bin()); - - // 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(ctx)->on_websocket_read(bev); -} - -void HTTPServer::dispatch_on_websocket_error(struct bufferevent* bev, short events, void* ctx) { - reinterpret_cast(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(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 + if (--expected_results == 0) { + complete_promise.set_value(); } - payload_size = phosg::bswap64(*reinterpret_cast(&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 = phosg::bswap16(*reinterpret_cast(&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 c = this->bev_to_websocket_client.at(bev); - c->last_communication_time = phosg::now(); - - // Read and unmask message data - string payload(payload_size, '\0'); - evbuffer_remove(in_buf, const_cast(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(); + co_return; + }; + for (const auto& c : subscribers) { + asio::co_spawn(co_await asio::this_coro::executor, fn(c), asio::detached); } + co_await complete_promise.get(); } + co_return; } -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(const_cast(header.data() + 2)) = phosg::bswap64(message.size()); - } else if (message.size() > 0x7D) { - header.push_back(0x7E); - header.resize(4); - *reinterpret_cast(const_cast(header.data() + 2)) = phosg::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 c, const string& message, uint8_t opcode) { - this->send_websocket_message(c->bev, message, opcode); -} - -void HTTPServer::handle_websocket_message(shared_ptr, uint8_t, const string&) { - // Currently we just ignore any messages from the client -} - -void HTTPServer::handle_websocket_disconnect(shared_ptr c) { - this->rare_drop_subscribers.erase(c); -} - -void HTTPServer::send_rare_drop_notification(shared_ptr 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) { - reinterpret_cast(ctx)->handle_request(req); -} - -phosg::JSON HTTPServer::generate_server_version_st() { - return phosg::JSON::dict({ +std::shared_ptr HTTPServer::generate_server_version() const { + return make_shared(phosg::JSON::dict({ {"ServerType", "newserv"}, {"BuildTime", BUILD_TIMESTAMP}, {"BuildTimeStr", phosg::format_time(BUILD_TIMESTAMP)}, {"Revision", GIT_REVISION_HASH}, - }); + })); } -phosg::JSON HTTPServer::generate_client_config_json_st(const Client::Config& config) { - const char* drop_notifications_mode = "unknown"; - switch (config.get_drop_notification_mode()) { - case Client::ItemDropNotificationMode::NOTHING: - drop_notifications_mode = "off"; - break; - case Client::ItemDropNotificationMode::RARES_ONLY: - drop_notifications_mode = "rare"; - break; - case Client::ItemDropNotificationMode::ALL_ITEMS: - drop_notifications_mode = "on"; - break; - case Client::ItemDropNotificationMode::ALL_ITEMS_INCLUDING_MESETA: - drop_notifications_mode = "every"; - break; - } - - auto ret = phosg::JSON::dict({ - {"SpecificVersion", config.specific_version}, - {"SwitchAssistEnabled", (config.check_flag(Client::Flag::SWITCH_ASSIST_ENABLED) ? true : false)}, - {"InfiniteHPEnabled", (config.check_flag(Client::Flag::INFINITE_HP_ENABLED) ? true : false)}, - {"InfiniteTPEnabled", (config.check_flag(Client::Flag::INFINITE_TP_ENABLED) ? true : false)}, - {"DropNotificationMode", drop_notifications_mode}, - {"DebugEnabled", (config.check_flag(Client::Flag::DEBUG_ENABLED) ? true : false)}, - {"ProxySaveFilesEnabled", (config.check_flag(Client::Flag::PROXY_SAVE_FILES) ? true : false)}, - {"ProxyChatCommandsEnabled", (config.check_flag(Client::Flag::PROXY_CHAT_COMMANDS_ENABLED) ? true : false)}, - {"ProxyPlayerNotificationsEnabled", (config.check_flag(Client::Flag::PROXY_PLAYER_NOTIFICATIONS_ENABLED) ? true : false)}, - {"ProxySuppressClientPings", (config.check_flag(Client::Flag::PROXY_SUPPRESS_CLIENT_PINGS) ? true : false)}, - {"ProxyEp3InfiniteMesetaEnabled", (config.check_flag(Client::Flag::PROXY_EP3_INFINITE_MESETA_ENABLED) ? true : false)}, - {"ProxyEp3InfiniteTimeEnabled", (config.check_flag(Client::Flag::PROXY_EP3_INFINITE_TIME_ENABLED) ? true : false)}, - {"ProxyBlockFunctionCalls", (config.check_flag(Client::Flag::PROXY_BLOCK_FUNCTION_CALLS) ? true : false)}, - {"ProxyEp3UnmaskWhispers", (config.check_flag(Client::Flag::PROXY_EP3_UNMASK_WHISPERS) ? true : false)}, - }); - ret.emplace("OverrideRandomSeed", config.check_flag(Client::Flag::USE_OVERRIDE_RANDOM_SEED) ? config.override_random_seed : phosg::JSON(nullptr)); - ret.emplace("OverrideSectionID", (config.override_section_id != 0xFF) ? config.override_section_id : phosg::JSON(nullptr)); - ret.emplace("OverrideLobbyEvent", (config.override_lobby_event != 0xFF) ? config.override_lobby_event : phosg::JSON(nullptr)); - ret.emplace("OverrideLobbyNumber", (config.override_lobby_number != 0x80) ? config.override_lobby_number : phosg::JSON(nullptr)); - return ret; -} - -phosg::JSON HTTPServer::generate_account_json_st(shared_ptr a) { +std::shared_ptr HTTPServer::generate_account_json(shared_ptr a) const { auto dc_nte_licenses_json = phosg::JSON::list(); for (const auto& it : a->dc_nte_licenses) { dc_nte_licenses_json.emplace_back(it.first); @@ -521,7 +87,7 @@ phosg::JSON HTTPServer::generate_account_json_st(shared_ptr a) { for (const auto& it : a->auto_patches_enabled) { auto_patches_json.emplace_back(it); } - return phosg::JSON::dict({ + return make_shared(phosg::JSON::dict({ {"AccountID", a->account_id}, {"Flags", a->flags}, {"BanEndTime", a->ban_end_time ? a->ban_end_time : phosg::JSON(nullptr)}, @@ -538,68 +104,84 @@ phosg::JSON HTTPServer::generate_account_json_st(shared_ptr a) { {"XBLicenses", std::move(xb_licenses_json)}, {"BBLicenses", std::move(bb_licenses_json)}, {"AutoPatchesEnabled", std::move(auto_patches_json)}, - }); + })); }; -static phosg::JSON format_remote_client_address( - std::shared_ptr ip_stack_simulator, const Channel& ch) { - if (!ch.virtual_network_id) { - if (ch.remote_addr.ss_family == 0) { - return nullptr; - } else { - return phosg::render_sockaddr_storage(ch.remote_addr); - } - } else if (ip_stack_simulator) { - auto network = ip_stack_simulator->get_network(ch.virtual_network_id); - int fd = bufferevent_getfd(network->bev.get()); - if (fd < 0) { - return nullptr; - } else { - struct sockaddr_storage remote_ss; - phosg::get_socket_addresses(fd, nullptr, &remote_ss); - return phosg::render_sockaddr_storage(remote_ss); - } - } else { - return "__unknown_address__"; - } -} - -phosg::JSON HTTPServer::generate_game_client_json_st(shared_ptr c, shared_ptr item_name_index) { +std::shared_ptr HTTPServer::generate_client_json( + shared_ptr c, shared_ptr item_name_index) const { auto s = c->require_server_state(); - auto ret = phosg::JSON::dict({ + + const char* drop_notifications_mode = "unknown"; + switch (c->get_drop_notification_mode()) { + case Client::ItemDropNotificationMode::NOTHING: + drop_notifications_mode = "off"; + break; + case Client::ItemDropNotificationMode::RARES_ONLY: + drop_notifications_mode = "rare"; + break; + case Client::ItemDropNotificationMode::ALL_ITEMS: + drop_notifications_mode = "on"; + break; + case Client::ItemDropNotificationMode::ALL_ITEMS_INCLUDING_MESETA: + drop_notifications_mode = "every"; + break; + } + + auto ret = make_shared(phosg::JSON::dict({ {"ID", c->id}, - {"RemoteAddress", format_remote_client_address(s->ip_stack_simulator, c->channel)}, + {"RemoteAddress", c->channel->default_name()}, {"Version", phosg::name_for_enum(c->version())}, {"SubVersion", c->sub_version}, - {"Config", HTTPServer::generate_client_config_json_st(c->config)}, {"Language", name_for_language_code(c->language())}, {"LocationX", c->pos.x.load()}, {"LocationZ", c->pos.z.load()}, {"LocationFloor", c->floor}, {"CanChat", c->can_chat}, - }); - ret.emplace("Account", c->login ? HTTPServer::generate_account_json_st(c->login->account) : phosg::JSON(nullptr)); + {"SpecificVersion", c->specific_version}, + {"SwitchAssistEnabled", (c->check_flag(Client::Flag::SWITCH_ASSIST_ENABLED) ? true : false)}, + {"InfiniteHPEnabled", (c->check_flag(Client::Flag::INFINITE_HP_ENABLED) ? true : false)}, + {"InfiniteTPEnabled", (c->check_flag(Client::Flag::INFINITE_TP_ENABLED) ? true : false)}, + {"DropNotificationMode", drop_notifications_mode}, + {"DebugEnabled", (c->check_flag(Client::Flag::DEBUG_ENABLED) ? true : false)}, + {"ProxySaveFilesEnabled", (c->check_flag(Client::Flag::PROXY_SAVE_FILES) ? true : false)}, + {"ProxyChatCommandsEnabled", (c->check_flag(Client::Flag::PROXY_CHAT_COMMANDS_ENABLED) ? true : false)}, + {"ProxyPlayerNotificationsEnabled", (c->check_flag(Client::Flag::PROXY_PLAYER_NOTIFICATIONS_ENABLED) ? true : false)}, + {"ProxyEp3InfiniteMesetaEnabled", (c->check_flag(Client::Flag::PROXY_EP3_INFINITE_MESETA_ENABLED) ? true : false)}, + {"ProxyEp3InfiniteTimeEnabled", (c->check_flag(Client::Flag::PROXY_EP3_INFINITE_TIME_ENABLED) ? true : false)}, + {"ProxyBlockFunctionCalls", (c->check_flag(Client::Flag::PROXY_BLOCK_FUNCTION_CALLS) ? true : false)}, + {"ProxyEp3UnmaskWhispers", (c->check_flag(Client::Flag::PROXY_EP3_UNMASK_WHISPERS) ? true : false)}, + {"OverrideRandomSeed", c->override_random_seed}, + {"OverrideSectionID", ((c->override_section_id != 0xFF) ? c->override_section_id : phosg::JSON(nullptr))}, + {"OverrideLobbyEvent", ((c->override_lobby_event != 0xFF) ? c->override_lobby_event : phosg::JSON(nullptr))}, + {"OverrideLobbyNumber", ((c->override_lobby_number != 0x80) ? c->override_lobby_number : phosg::JSON(nullptr))}, + })); + if (c->login) { + auto acc_json = HTTPServer::generate_account_json(c->login->account); + ret->emplace("Account", std::move(*acc_json)); + } else { + ret->emplace("Account", phosg::JSON()); + } auto l = c->lobby.lock(); if (l) { - ret.emplace("LobbyID", l->lobby_id); - ret.emplace("LobbyClientID", c->lobby_client_id); + ret->emplace("LobbyID", l->lobby_id); + ret->emplace("LobbyClientID", c->lobby_client_id); } if (c->version() == Version::BB_V4) { - ret.emplace("BBCharacterIndex", c->bb_character_index); + ret->emplace("BBCharacterIndex", c->bb_character_index); } auto p = c->character(false, false); if (p) { if (!is_ep3(c->version())) { if (c->version() != Version::DC_NTE) { - ret.emplace("InventoryLanguage", name_for_language_code(p->inventory.language)); - ret.emplace("NumHPMaterialsUsed", p->get_material_usage(PSOBBCharacterFile::MaterialType::HP)); - ret.emplace("NumTPMaterialsUsed", p->get_material_usage(PSOBBCharacterFile::MaterialType::TP)); + ret->emplace("InventoryLanguage", name_for_language_code(p->inventory.language)); + ret->emplace("NumHPMaterialsUsed", p->get_material_usage(PSOBBCharacterFile::MaterialType::HP)); + ret->emplace("NumTPMaterialsUsed", p->get_material_usage(PSOBBCharacterFile::MaterialType::TP)); if (!is_v1_or_v2(c->version())) { - ret.emplace("NumPowerMaterialsUsed", p->get_material_usage(PSOBBCharacterFile::MaterialType::POWER)); - ret.emplace("NumDefMaterialsUsed", p->get_material_usage(PSOBBCharacterFile::MaterialType::DEF)); - ret.emplace("NumMindMaterialsUsed", p->get_material_usage(PSOBBCharacterFile::MaterialType::MIND)); - ret.emplace("NumEvadeMaterialsUsed", p->get_material_usage(PSOBBCharacterFile::MaterialType::EVADE)); - ret.emplace("NumLuckMaterialsUsed", p->get_material_usage(PSOBBCharacterFile::MaterialType::LUCK)); + ret->emplace("NumPowerMaterialsUsed", p->get_material_usage(PSOBBCharacterFile::MaterialType::POWER)); + ret->emplace("NumDefMaterialsUsed", p->get_material_usage(PSOBBCharacterFile::MaterialType::DEF)); + ret->emplace("NumMindMaterialsUsed", p->get_material_usage(PSOBBCharacterFile::MaterialType::MIND)); + ret->emplace("NumEvadeMaterialsUsed", p->get_material_usage(PSOBBCharacterFile::MaterialType::EVADE)); + ret->emplace("NumLuckMaterialsUsed", p->get_material_usage(PSOBBCharacterFile::MaterialType::LUCK)); } } phosg::JSON items_json = phosg::JSON::list(); @@ -615,53 +197,53 @@ phosg::JSON HTTPServer::generate_game_client_json_st(shared_ptr c, } items_json.emplace_back(std::move(item_dict)); } - ret.emplace("InventoryItems", std::move(items_json)); - ret.emplace("ATP", p->disp.stats.char_stats.atp.load()); - ret.emplace("MST", p->disp.stats.char_stats.mst.load()); - ret.emplace("EVP", p->disp.stats.char_stats.evp.load()); - ret.emplace("HP", p->disp.stats.char_stats.hp.load()); - ret.emplace("DFP", p->disp.stats.char_stats.dfp.load()); - ret.emplace("ATA", p->disp.stats.char_stats.ata.load()); - ret.emplace("LCK", p->disp.stats.char_stats.lck.load()); - ret.emplace("EXP", p->disp.stats.experience.load()); - ret.emplace("Meseta", p->disp.stats.meseta.load()); + ret->emplace("InventoryItems", std::move(items_json)); + ret->emplace("ATP", p->disp.stats.char_stats.atp.load()); + ret->emplace("MST", p->disp.stats.char_stats.mst.load()); + ret->emplace("EVP", p->disp.stats.char_stats.evp.load()); + ret->emplace("HP", p->disp.stats.char_stats.hp.load()); + ret->emplace("DFP", p->disp.stats.char_stats.dfp.load()); + ret->emplace("ATA", p->disp.stats.char_stats.ata.load()); + ret->emplace("LCK", p->disp.stats.char_stats.lck.load()); + ret->emplace("EXP", p->disp.stats.experience.load()); + ret->emplace("Meseta", p->disp.stats.meseta.load()); auto tech_levels_json = phosg::JSON::dict(); for (size_t z = 0; z < 0x13; z++) { auto level = p->get_technique_level(z); tech_levels_json.emplace(name_for_technique(z), (level != 0xFF) ? level : phosg::JSON(nullptr)); } - ret.emplace("TechniqueLevels", std::move(tech_levels_json)); + ret->emplace("TechniqueLevels", std::move(tech_levels_json)); } - ret.emplace("Height", p->disp.stats.height.load()); - ret.emplace("Level", p->disp.stats.level.load()); - ret.emplace("NameColor", p->disp.visual.name_color.load()); - ret.emplace("ExtraModel", (p->disp.visual.validation_flags & 2) ? p->disp.visual.extra_model : phosg::JSON(nullptr)); - ret.emplace("SectionID", name_for_section_id(p->disp.visual.section_id)); - ret.emplace("CharClass", name_for_char_class(p->disp.visual.char_class)); - ret.emplace("Costume", p->disp.visual.costume.load()); - ret.emplace("Skin", p->disp.visual.skin.load()); - ret.emplace("Face", p->disp.visual.face.load()); - ret.emplace("Head", p->disp.visual.head.load()); - ret.emplace("Hair", p->disp.visual.hair.load()); - ret.emplace("HairR", p->disp.visual.hair_r.load()); - ret.emplace("HairG", p->disp.visual.hair_g.load()); - ret.emplace("HairB", p->disp.visual.hair_b.load()); - ret.emplace("ProportionX", p->disp.visual.proportion_x.load()); - ret.emplace("ProportionY", p->disp.visual.proportion_y.load()); + ret->emplace("Height", p->disp.stats.height.load()); + ret->emplace("Level", p->disp.stats.level.load()); + ret->emplace("NameColor", p->disp.visual.name_color.load()); + ret->emplace("ExtraModel", (p->disp.visual.validation_flags & 2) ? p->disp.visual.extra_model : phosg::JSON(nullptr)); + ret->emplace("SectionID", name_for_section_id(p->disp.visual.section_id)); + ret->emplace("CharClass", name_for_char_class(p->disp.visual.char_class)); + ret->emplace("Costume", p->disp.visual.costume.load()); + ret->emplace("Skin", p->disp.visual.skin.load()); + ret->emplace("Face", p->disp.visual.face.load()); + ret->emplace("Head", p->disp.visual.head.load()); + ret->emplace("Hair", p->disp.visual.hair.load()); + ret->emplace("HairR", p->disp.visual.hair_r.load()); + ret->emplace("HairG", p->disp.visual.hair_g.load()); + ret->emplace("HairB", p->disp.visual.hair_b.load()); + ret->emplace("ProportionX", p->disp.visual.proportion_x.load()); + ret->emplace("ProportionY", p->disp.visual.proportion_y.load()); - ret.emplace("Name", p->disp.name.decode(c->language())); - ret.emplace("PlayTimeSeconds", p->play_time_seconds.load()); + ret->emplace("Name", p->disp.name.decode(c->language())); + ret->emplace("PlayTimeSeconds", p->play_time_seconds.load()); - ret.emplace("AutoReply", p->auto_reply.decode(c->language())); - ret.emplace("InfoBoard", p->info_board.decode(c->language())); + ret->emplace("AutoReply", p->auto_reply.decode(c->language())); + ret->emplace("InfoBoard", p->info_board.decode(c->language())); auto battle_place_counts = phosg::JSON::list({ p->battle_records.place_counts[0].load(), p->battle_records.place_counts[1].load(), p->battle_records.place_counts[2].load(), p->battle_records.place_counts[3].load(), }); - ret.emplace("BattlePlaceCounts", std::move(battle_place_counts)); - ret.emplace("BattleDisconnectCount", p->battle_records.disconnect_count.load()); + ret->emplace("BattlePlaceCounts", std::move(battle_place_counts)); + ret->emplace("BattleDisconnectCount", p->battle_records.disconnect_count.load()); if (!is_ep3(c->version())) { auto json_for_challenge_times = [](const parray& times) -> phosg::JSON { @@ -671,21 +253,21 @@ phosg::JSON HTTPServer::generate_game_client_json_st(shared_ptr c, } return times_json; }; - ret.emplace("ChallengeTitleColorXRGB1555", p->challenge_records.title_color.load()); - ret.emplace("ChallengeTimesEp1Online", json_for_challenge_times(p->challenge_records.times_ep1_online)); - ret.emplace("ChallengeTimesEp2Online", json_for_challenge_times(p->challenge_records.times_ep2_online)); - ret.emplace("ChallengeTimesEp1Offline", json_for_challenge_times(p->challenge_records.times_ep1_offline)); - ret.emplace("ChallengeGraveIsEp2", p->challenge_records.grave_is_ep2 ? true : false); - ret.emplace("ChallengeGraveStageNum", p->challenge_records.grave_stage_num); - ret.emplace("ChallengeGraveFloor", p->challenge_records.grave_floor); - ret.emplace("ChallengeGraveDeaths", p->challenge_records.grave_deaths.load()); + ret->emplace("ChallengeTitleColorXRGB1555", p->challenge_records.title_color.load()); + ret->emplace("ChallengeTimesEp1Online", json_for_challenge_times(p->challenge_records.times_ep1_online)); + ret->emplace("ChallengeTimesEp2Online", json_for_challenge_times(p->challenge_records.times_ep2_online)); + ret->emplace("ChallengeTimesEp1Offline", json_for_challenge_times(p->challenge_records.times_ep1_offline)); + ret->emplace("ChallengeGraveIsEp2", p->challenge_records.grave_is_ep2 ? true : false); + ret->emplace("ChallengeGraveStageNum", p->challenge_records.grave_stage_num); + ret->emplace("ChallengeGraveFloor", p->challenge_records.grave_floor); + ret->emplace("ChallengeGraveDeaths", p->challenge_records.grave_deaths.load()); { uint16_t year = 2000 + ((p->challenge_records.grave_time >> 28) & 0x0F); uint8_t month = (p->challenge_records.grave_time >> 24) & 0x0F; uint8_t day = (p->challenge_records.grave_time >> 16) & 0xFF; uint8_t hour = (p->challenge_records.grave_time >> 8) & 0xFF; uint8_t minute = p->challenge_records.grave_time & 0xFF; - ret.emplace("ChallengeGraveTime", phosg::string_printf("%04hu-%02hhu-%02hhu %02hhu:%02hhu:00", year, month, day, hour, minute)); + ret->emplace("ChallengeGraveTime", std::format("{:04}-{:02}-{:02} {:02}:{:02}:00", year, month, day, hour, minute)); } string grave_enemy_types; if (p->challenge_records.grave_defeated_by_enemy_rt_index) { @@ -696,98 +278,75 @@ phosg::JSON HTTPServer::generate_game_client_json_st(shared_ptr c, grave_enemy_types += phosg::name_for_enum(type); } } - ret.emplace("ChallengeGraveDefeatedByEnemy", std::move(grave_enemy_types)); - ret.emplace("ChallengeGraveX", p->challenge_records.grave_x.load()); - ret.emplace("ChallengeGraveY", p->challenge_records.grave_y.load()); - ret.emplace("ChallengeGraveZ", p->challenge_records.grave_z.load()); - ret.emplace("ChallengeGraveTeam", p->challenge_records.grave_team.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("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("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("ChallengeAwardStateEp1OfflineMaxRank", p->challenge_records.ep1_offline_award_state.maximum_rank.decode()); - ret.emplace("ChallengeRankTitle", p->challenge_records.rank_title.decode()); + ret->emplace("ChallengeGraveDefeatedByEnemy", std::move(grave_enemy_types)); + ret->emplace("ChallengeGraveX", p->challenge_records.grave_x.load()); + ret->emplace("ChallengeGraveY", p->challenge_records.grave_y.load()); + ret->emplace("ChallengeGraveZ", p->challenge_records.grave_z.load()); + ret->emplace("ChallengeGraveTeam", p->challenge_records.grave_team.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("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("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("ChallengeAwardStateEp1OfflineMaxRank", p->challenge_records.ep1_offline_award_state.maximum_rank.decode()); + ret->emplace("ChallengeRankTitle", p->challenge_records.rank_title.decode()); } } + auto ses = c->proxy_session; + if (ses) { + auto lobby_players_json = phosg::JSON::list(); + for (size_t z = 0; z < ses->lobby_players.size(); z++) { + const auto& p = ses->lobby_players[z]; + if (p.guild_card_number) { + lobby_players_json.emplace_back(phosg::JSON::dict({ + {"GuildCardNumber", p.guild_card_number}, + {"Name", p.name}, + {"Language", name_for_language_code(p.language)}, + {"SectionID", name_for_section_id(p.section_id)}, + {"CharClass", name_for_char_class(p.char_class)}, + })); + lobby_players_json.back().emplace("XBUserID", p.xb_user_id ? p.xb_user_id : phosg::JSON(nullptr)); + } else { + lobby_players_json.emplace_back(nullptr); + } + } + + auto ses_json = phosg::JSON::dict({ + {"RemoteServerAddress", ses->server_channel->default_name()}, + {"RemoteGuildCardNumber", ses->remote_guild_card_number}, + {"RemoteClientConfigData", phosg::format_data_string(&ses->remote_client_config_data[0], ses->remote_client_config_data.size())}, + {"IsInGame", ses->is_in_game}, + {"IsInQuest", ses->is_in_quest}, + {"LobbyLeaderClientID", ses->leader_client_id}, + {"LobbyEvent", ses->lobby_event}, + {"LobbyDifficulty", name_for_difficulty(ses->lobby_difficulty)}, + {"LobbySectionID", name_for_section_id(ses->lobby_section_id)}, + {"LobbyMode", name_for_mode(ses->lobby_mode)}, + {"LobbyEpisode", name_for_episode(ses->lobby_episode)}, + {"LobbyRandomSeed", ses->lobby_random_seed}, + {"LobbyPlayers", std::move(lobby_players_json)}, + }); + switch (ses->drop_mode) { + case ProxySession::DropMode::DISABLED: + ses_json.emplace("DropMode", "none"); + break; + case ProxySession::DropMode::PASSTHROUGH: + ses_json.emplace("DropMode", "default"); + break; + case ProxySession::DropMode::INTERCEPT: + ses_json.emplace("DropMode", "proxy"); + break; + } + ret->emplace("ProxySession", std::move(ses_json)); + } else { + ret->emplace("ProxySession", phosg::JSON()); + } return ret; } -phosg::JSON HTTPServer::generate_proxy_client_json_st(shared_ptr ses) { - struct LobbyPlayer { - uint32_t guild_card_number = 0; - uint64_t xb_user_id = 0; - std::string name; - uint8_t language = 0; - uint8_t section_id = 0; - uint8_t char_class = 0; - }; - std::vector lobby_players; - - auto lobby_players_json = phosg::JSON::list(); - for (size_t z = 0; z < ses->lobby_players.size(); z++) { - const auto& p = ses->lobby_players[z]; - if (p.guild_card_number) { - lobby_players_json.emplace_back(phosg::JSON::dict({ - {"GuildCardNumber", p.guild_card_number}, - {"Name", p.name}, - {"Language", name_for_language_code(p.language)}, - {"SectionID", name_for_section_id(p.section_id)}, - {"CharClass", name_for_char_class(p.char_class)}, - })); - lobby_players_json.back().emplace("XBUserID", p.xb_user_id ? p.xb_user_id : phosg::JSON(nullptr)); - } else { - lobby_players_json.emplace_back(nullptr); - } - } - - auto s = ses->require_server_state(); - auto ret = phosg::JSON::dict({ - {"ID", ses->id}, - {"RemoteClientAddress", format_remote_client_address(s->ip_stack_simulator, ses->client_channel)}, - {"RemoteServerAddress", phosg::render_sockaddr_storage(ses->server_channel.remote_addr)}, - {"LocalPort", ses->local_port}, - {"NextDestination", phosg::render_sockaddr_storage(ses->next_destination)}, - {"Version", phosg::name_for_enum(ses->version())}, - {"SubVersion", ses->sub_version}, - {"Name", ses->character_name}, - {"DCSerialNumber2", ses->serial_number2}, - {"RemoteGuildCardNumber", ses->remote_guild_card_number}, - {"RemoteClientConfigData", phosg::format_data_string(&ses->remote_client_config_data[0], ses->remote_client_config_data.size())}, - {"Config", HTTPServer::generate_client_config_json_st(ses->config)}, - {"Language", name_for_language_code(ses->language())}, - {"LobbyClientID", ses->lobby_client_id}, - {"LeaderClientID", ses->leader_client_id}, - {"LocationX", ses->pos.x.load()}, - {"LocationZ", ses->pos.z.load()}, - {"LocationFloor", ses->floor}, - {"IsInGame", ses->is_in_game}, - {"IsInQuest", ses->is_in_quest}, - {"LobbyEvent", ses->lobby_event}, - {"LobbyDifficulty", name_for_difficulty(ses->lobby_difficulty)}, - {"LobbySectionID", name_for_section_id(ses->lobby_section_id)}, - {"LobbyMode", name_for_mode(ses->lobby_mode)}, - {"LobbyEpisode", name_for_episode(ses->lobby_episode)}, - {"LobbyRandomSeed", ses->lobby_random_seed}, - {"LobbyPlayers", std::move(lobby_players_json)}, - }); - switch (ses->drop_mode) { - case ProxyServer::LinkedSession::DropMode::DISABLED: - ret.emplace("DropMode", "none"); - break; - case ProxyServer::LinkedSession::DropMode::PASSTHROUGH: - ret.emplace("DropMode", "default"); - break; - case ProxyServer::LinkedSession::DropMode::INTERCEPT: - ret.emplace("DropMode", "proxy"); - break; - } - ret.emplace("Account", ses->login ? HTTPServer::generate_account_json_st(ses->login->account) : phosg::JSON(nullptr)); - return ret; -} - -phosg::JSON HTTPServer::generate_lobby_json_st(shared_ptr l, shared_ptr item_name_index) { +std::shared_ptr HTTPServer::generate_lobby_json( + shared_ptr l, shared_ptr item_name_index) const { std::array, 12> clients; auto client_ids_json = phosg::JSON::list(); @@ -795,7 +354,7 @@ phosg::JSON HTTPServer::generate_lobby_json_st(shared_ptr l, shared client_ids_json.emplace_back(l->clients[z] ? l->clients[z]->id : phosg::JSON(nullptr)); } - auto ret = phosg::JSON::dict({ + auto ret = make_shared(phosg::JSON::dict({ {"ID", l->lobby_id}, {"AllowedVersions", l->allowed_versions}, {"Event", l->event}, @@ -805,56 +364,56 @@ phosg::JSON HTTPServer::generate_lobby_json_st(shared_ptr l, shared {"ClientIDs", std::move(client_ids_json)}, {"IsGame", l->is_game()}, {"IsPersistent", l->check_flag(Lobby::Flag::PERSISTENT)}, - }); + })); if (l->is_game()) { - ret.emplace("CheatsEnabled", l->check_flag(Lobby::Flag::CHEATS_ENABLED)); - ret.emplace("MinLevel", l->min_level + 1); - ret.emplace("MaxLevel", l->max_level + 1); - ret.emplace("Episode", name_for_episode(l->episode)); - ret.emplace("HasPassword", !l->password.empty()); - ret.emplace("Name", l->name); - ret.emplace("RandomSeed", l->random_seed); + ret->emplace("CheatsEnabled", l->check_flag(Lobby::Flag::CHEATS_ENABLED)); + ret->emplace("MinLevel", l->min_level + 1); + ret->emplace("MaxLevel", l->max_level + 1); + ret->emplace("Episode", name_for_episode(l->episode)); + ret->emplace("HasPassword", !l->password.empty()); + ret->emplace("Name", l->name); + ret->emplace("RandomSeed", l->random_seed); 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("JoinableQuestInProgress", l->check_flag(Lobby::Flag::JOINABLE_QUEST_IN_PROGRESS)); - ret.emplace("Variations", l->variations.json()); - ret.emplace("SectionID", name_for_section_id(l->effective_section_id())); - ret.emplace("Mode", name_for_mode(l->mode)); - ret.emplace("Difficulty", name_for_difficulty(l->difficulty)); - ret.emplace("BaseEXPMultiplier", l->base_exp_multiplier); - ret.emplace("EXPShareMultiplier", l->exp_share_multiplier); - ret.emplace("AllowedDropModes", l->allowed_drop_modes); + 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("JoinableQuestInProgress", l->check_flag(Lobby::Flag::JOINABLE_QUEST_IN_PROGRESS)); + ret->emplace("Variations", l->variations.json()); + ret->emplace("SectionID", name_for_section_id(l->effective_section_id())); + ret->emplace("Mode", name_for_mode(l->mode)); + ret->emplace("Difficulty", name_for_difficulty(l->difficulty)); + ret->emplace("BaseEXPMultiplier", l->base_exp_multiplier); + ret->emplace("EXPShareMultiplier", l->exp_share_multiplier); + ret->emplace("AllowedDropModes", l->allowed_drop_modes); switch (l->drop_mode) { case Lobby::DropMode::DISABLED: - ret.emplace("DropMode", "none"); + ret->emplace("DropMode", "none"); break; case Lobby::DropMode::CLIENT: - ret.emplace("DropMode", "client"); + ret->emplace("DropMode", "client"); break; case Lobby::DropMode::SERVER_SHARED: - ret.emplace("DropMode", "shared"); + ret->emplace("DropMode", "shared"); break; case Lobby::DropMode::SERVER_PRIVATE: - ret.emplace("DropMode", "private"); + ret->emplace("DropMode", "private"); break; case Lobby::DropMode::SERVER_DUPLICATE: - ret.emplace("DropMode", "duplicate"); + ret->emplace("DropMode", "duplicate"); break; } if (l->mode == GameMode::CHALLENGE) { - ret.emplace("ChallengeEXPMultiplier", l->challenge_exp_multiplier); + ret->emplace("ChallengeEXPMultiplier", l->challenge_exp_multiplier); if (l->challenge_params) { - ret.emplace("ChallengeStageNumber", l->challenge_params->stage_number); - ret.emplace("ChallengeRankColor", l->challenge_params->rank_color); - ret.emplace("ChallengeRankText", l->challenge_params->rank_text); - ret.emplace("ChallengeRank0ThresholdBitmask", l->challenge_params->rank_thresholds[0].bitmask); - ret.emplace("ChallengeRank0ThresholdSeconds", l->challenge_params->rank_thresholds[0].seconds); - ret.emplace("ChallengeRank1ThresholdBitmask", l->challenge_params->rank_thresholds[1].bitmask); - ret.emplace("ChallengeRank1ThresholdSeconds", l->challenge_params->rank_thresholds[1].seconds); - ret.emplace("ChallengeRank2ThresholdBitmask", l->challenge_params->rank_thresholds[2].bitmask); - ret.emplace("ChallengeRank2ThresholdSeconds", l->challenge_params->rank_thresholds[2].seconds); + ret->emplace("ChallengeStageNumber", l->challenge_params->stage_number); + ret->emplace("ChallengeRankColor", l->challenge_params->rank_color); + ret->emplace("ChallengeRankText", l->challenge_params->rank_text); + ret->emplace("ChallengeRank0ThresholdBitmask", l->challenge_params->rank_thresholds[0].bitmask); + ret->emplace("ChallengeRank0ThresholdSeconds", l->challenge_params->rank_thresholds[0].seconds); + ret->emplace("ChallengeRank1ThresholdBitmask", l->challenge_params->rank_thresholds[1].bitmask); + ret->emplace("ChallengeRank1ThresholdSeconds", l->challenge_params->rank_thresholds[1].seconds); + ret->emplace("ChallengeRank2ThresholdBitmask", l->challenge_params->rank_thresholds[2].bitmask); + ret->emplace("ChallengeRank2ThresholdSeconds", l->challenge_params->rank_thresholds[2].seconds); } } @@ -877,13 +436,13 @@ phosg::JSON HTTPServer::generate_lobby_json_st(shared_ptr l, shared floor_items_json.emplace_back(std::move(item_dict)); } } - ret.emplace("FloorItems", std::move(floor_items_json)); - ret.emplace("Quest", l->quest ? l->quest->json() : phosg::JSON(nullptr)); + ret->emplace("FloorItems", std::move(floor_items_json)); + ret->emplace("Quest", l->quest ? l->quest->json() : phosg::JSON(nullptr)); } else { - ret.emplace("BattleInProgress", l->check_flag(Lobby::Flag::BATTLE_IN_PROGRESS)); - ret.emplace("IsSpectatorTeam", l->check_flag(Lobby::Flag::IS_SPECTATOR_TEAM)); - ret.emplace("SpectatorsForbidden", l->check_flag(Lobby::Flag::SPECTATORS_FORBIDDEN)); + ret->emplace("BattleInProgress", l->check_flag(Lobby::Flag::BATTLE_IN_PROGRESS)); + ret->emplace("IsSpectatorTeam", l->check_flag(Lobby::Flag::IS_SPECTATOR_TEAM)); + ret->emplace("SpectatorsForbidden", l->check_flag(Lobby::Flag::SPECTATORS_FORBIDDEN)); auto ep3s = l->ep3_server; if (ep3s) { @@ -936,8 +495,7 @@ phosg::JSON HTTPServer::generate_lobby_json_st(shared_ptr l, shared } auto battle_state_json = phosg::JSON::dict({ {"BehaviorFlags", ep3s->options.behavior_flags}, - {"RandomSeed", ep3s->options.opt_rand_crypt ? ep3s->options.opt_rand_crypt->seed() : phosg::JSON(nullptr)}, - {"RandomOffset", ep3s->options.opt_rand_crypt ? ep3s->options.opt_rand_crypt->absolute_offset() : phosg::JSON(nullptr)}, + {"RandomSeed", ep3s->options.rand_crypt->seed()}, {"Tournament", ep3s->options.tournament ? ep3s->options.tournament->json() : nullptr}, {"MapNumber", ep3s->last_chosen_map ? ep3s->last_chosen_map->map_number : phosg::JSON(nullptr)}, {"EnvironmentNumber", ep3s->map_and_rules ? ep3s->map_and_rules->environment_number : phosg::JSON(nullptr)}, @@ -958,359 +516,323 @@ phosg::JSON HTTPServer::generate_lobby_json_st(shared_ptr l, shared }); // std::shared_ptr state_flags; // std::array, 4> player_states; - ret.emplace("Episode3BattleState", std::move(battle_state_json)); + ret->emplace("Episode3BattleState", std::move(battle_state_json)); } else { - ret.emplace("Episode3BattleState", nullptr); + ret->emplace("Episode3BattleState", nullptr); } auto watched_lobby = l->watched_lobby.lock(); if (watched_lobby) { - ret.emplace("WatchedLobbyID", watched_lobby->lobby_id); + ret->emplace("WatchedLobbyID", watched_lobby->lobby_id); } auto watcher_lobby_ids_json = phosg::JSON::list(); for (const auto& watcher_lobby : l->watcher_lobbies) { watcher_lobby_ids_json.emplace_back(watcher_lobby->lobby_id); } - ret.emplace("WatcherLobbyIDs", std::move(watcher_lobby_ids_json)); - ret.emplace("IsReplayLobby", !!l->battle_player); + ret->emplace("WatcherLobbyIDs", std::move(watcher_lobby_ids_json)); + ret->emplace("IsReplayLobby", !!l->battle_player); } } else { // Not game - ret.emplace("IsPublic", l->check_flag(Lobby::Flag::PUBLIC)); - ret.emplace("IsDefault", l->check_flag(Lobby::Flag::DEFAULT)); - ret.emplace("IsOverflow", l->check_flag(Lobby::Flag::IS_OVERFLOW)); - ret.emplace("Block", l->block); + ret->emplace("IsPublic", l->check_flag(Lobby::Flag::PUBLIC)); + ret->emplace("IsDefault", l->check_flag(Lobby::Flag::DEFAULT)); + ret->emplace("IsOverflow", l->check_flag(Lobby::Flag::IS_OVERFLOW)); + ret->emplace("Block", l->block); } return ret; } -phosg::JSON HTTPServer::generate_accounts_json() const { - return call_on_event_thread(this->state->base, [&]() { - auto res = phosg::JSON::list(); - for (const auto& it : this->state->account_index->all()) { - res.emplace_back(it->json()); - } - return res; - }); +std::shared_ptr HTTPServer::generate_accounts_json() const { + auto res = make_shared(phosg::JSON::list()); + for (const auto& it : this->state->account_index->all()) { + res->emplace_back(it->json()); + } + return res; } -phosg::JSON HTTPServer::generate_game_server_clients_json() const { - return call_on_event_thread(this->state->base, [&]() { - auto res = phosg::JSON::list(); - for (const auto& it : this->state->channel_to_client) { - res.emplace_back(this->generate_game_client_json_st(it.second, this->state->item_name_index_opt(it.second->version()))); - } - return res; - }); +std::shared_ptr HTTPServer::generate_clients_json() const { + auto res = make_shared(phosg::JSON::list()); + for (const auto& it : this->state->game_server->all_clients()) { + auto client_json = this->generate_client_json(it, this->state->item_name_index_opt(it->version())); + res->emplace_back(std::move(*client_json)); + } + return res; } -phosg::JSON HTTPServer::generate_proxy_server_clients_json() const { - return call_on_event_thread(this->state->base, [&]() { - phosg::JSON res = phosg::JSON::list(); - if (this->state->proxy_server) { - for (const auto& it : this->state->proxy_server->all_sessions()) { - res.emplace_back(this->generate_proxy_client_json_st(it.second)); - } +std::shared_ptr HTTPServer::generate_server_info_json() const { + size_t game_count = 0; + size_t lobby_count = 0; + for (const auto& it : this->state->id_to_lobby) { + if (it.second->is_game()) { + game_count++; + } else { + lobby_count++; } - return res; - }); + } + uint64_t uptime_usecs = phosg::now() - this->state->creation_time; + return make_shared(phosg::JSON::dict({ + {"StartTimeUsecs", this->state->creation_time}, + {"StartTime", phosg::format_time(this->state->creation_time)}, + {"UptimeUsecs", uptime_usecs}, + {"Uptime", phosg::format_duration(uptime_usecs)}, + {"LobbyCount", lobby_count}, + {"GameCount", game_count}, + {"ClientCount", this->state->game_server->all_clients().size() - ProxySession::num_proxy_sessions}, + {"ProxySessionCount", ProxySession::num_proxy_sessions}, + {"ServerName", this->state->name}, + })); } -phosg::JSON HTTPServer::generate_server_info_json() const { - return call_on_event_thread(this->state->base, [&]() { - size_t game_count = 0; - size_t lobby_count = 0; - for (const auto& it : this->state->id_to_lobby) { - if (it.second->is_game()) { - game_count++; +std::shared_ptr HTTPServer::generate_lobbies_json() const { + auto res = make_shared(phosg::JSON::list()); + for (const auto& it : this->state->id_to_lobby) { + auto leader = it.second->clients[it.second->leader_id]; + Version v = leader ? leader->version() : Version::BB_V4; + auto lobby_json = this->generate_lobby_json(it.second, this->state->item_name_index_opt(v)); + res->emplace_back(std::move(*lobby_json)); + } + return res; +} + +std::shared_ptr HTTPServer::generate_summary_json() const { + auto clients_json = phosg::JSON::list(); + for (const auto& c : this->state->game_server->all_clients()) { + auto p = c->character(false, false); + auto l = c->lobby.lock(); + clients_json.emplace_back(phosg::JSON::dict({ + {"ID", c->id}, + {"AccountID", c->login ? c->login->account->account_id : phosg::JSON(nullptr)}, + {"Name", p ? p->disp.name.decode(c->language()) : phosg::JSON(nullptr)}, + {"Version", phosg::name_for_enum(c->version())}, + {"Language", name_for_language_code(c->language())}, + {"Level", p ? p->disp.stats.level + 1 : phosg::JSON(nullptr)}, + {"Class", p ? name_for_char_class(p->disp.visual.char_class) : phosg::JSON(nullptr)}, + {"SectionID", p ? name_for_section_id(p->disp.visual.section_id) : phosg::JSON(nullptr)}, + {"LobbyID", l ? l->lobby_id : phosg::JSON(nullptr)}, + {"IsOnProxy", c->proxy_session ? true : false}, + })); + } + + auto games_json = phosg::JSON::list(); + for (const auto& it : this->state->id_to_lobby) { + auto l = it.second; + if (l->is_game()) { + auto game_json = phosg::JSON::dict({ + {"ID", l->lobby_id}, + {"Name", l->name}, + {"Players", l->count_clients()}, + {"CheatsEnabled", l->check_flag(Lobby::Flag::CHEATS_ENABLED)}, + {"Episode", name_for_episode(l->episode)}, + {"HasPassword", !l->password.empty()}, + }); + if (l->episode == Episode::EP3) { + auto ep3s = l->ep3_server; + game_json.emplace("BattleInProgress", l->check_flag(Lobby::Flag::BATTLE_IN_PROGRESS)); + game_json.emplace("IsSpectatorTeam", l->check_flag(Lobby::Flag::IS_SPECTATOR_TEAM)); + game_json.emplace("MapNumber", (ep3s && ep3s->last_chosen_map) ? ep3s->last_chosen_map->map_number : phosg::JSON(nullptr)); + game_json.emplace("Rules", (ep3s && ep3s->map_and_rules) ? ep3s->map_and_rules->rules.json() : nullptr); } else { - lobby_count++; + 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("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("Mode", name_for_mode(l->mode)); + game_json.emplace("Difficulty", name_for_difficulty(l->difficulty)); + game_json.emplace("Quest", l->quest ? l->quest->json() : phosg::JSON(nullptr)); } + games_json.emplace_back(std::move(game_json)); } - uint64_t uptime_usecs = phosg::now() - this->state->creation_time; - return phosg::JSON::dict({ - {"StartTimeUsecs", this->state->creation_time}, - {"StartTime", phosg::format_time(this->state->creation_time)}, - {"UptimeUsecs", uptime_usecs}, - {"Uptime", phosg::format_duration(uptime_usecs)}, - {"LobbyCount", lobby_count}, - {"GameCount", game_count}, - {"ClientCount", this->state->channel_to_client.size()}, - {"ProxySessionCount", this->state->proxy_server ? this->state->proxy_server->num_sessions() : 0}, - {"ServerName", this->state->name}, - }); + } + + auto server_json = this->generate_server_info_json(); + return make_shared(phosg::JSON::dict({ + {"Clients", std::move(clients_json)}, + {"Games", std::move(games_json)}, + {"Server", std::move(*server_json)}, + })); +} + +std::shared_ptr HTTPServer::generate_all_json() const { + auto clients_json = this->generate_clients_json(); + auto lobbies_json = this->generate_lobbies_json(); + auto server_json = this->generate_server_info_json(); + return make_shared(phosg::JSON::dict({ + {"Clients", std::move(*clients_json)}, + {"Lobbies", std::move(*lobbies_json)}, + {"Server", std::move(*server_json)}, + })); +} + +asio::awaitable> HTTPServer::generate_ep3_cards_json(bool trial) const { + auto& index = trial ? this->state->ep3_card_index_trial : this->state->ep3_card_index; + co_return co_await call_on_thread_pool(*this->state->thread_pool, [&]() -> shared_ptr { + return make_shared(index->definitions_json()); }); } -phosg::JSON HTTPServer::generate_lobbies_json() const { - return call_on_event_thread(this->state->base, [&]() { - phosg::JSON res = phosg::JSON::list(); - for (const auto& it : this->state->id_to_lobby) { - auto leader = it.second->clients[it.second->leader_id]; - Version v = leader ? leader->version() : Version::BB_V4; - res.emplace_back(this->generate_lobby_json_st(it.second, this->state->item_name_index_opt(v))); - } - return res; +asio::awaitable> HTTPServer::generate_common_tables_json() const { + auto v2_table = this->state->common_item_set_v2; + auto v3_v4_table = this->state->common_item_set_v3_v4; + co_return co_await call_on_thread_pool(*this->state->thread_pool, [&]() -> shared_ptr { + return make_shared(phosg::JSON::dict({{"v1_v2", v2_table->json()}, {"v3_v4", v3_v4_table->json()}})); }); } -phosg::JSON HTTPServer::generate_summary_json() const { - auto ret = call_on_event_thread(this->state->base, [&]() { - auto clients_json = phosg::JSON::list(); - for (const auto& it : this->state->channel_to_client) { - auto c = it.second; - auto p = c->character(false, false); - auto l = c->lobby.lock(); - clients_json.emplace_back(phosg::JSON::dict({ - {"ID", c->id}, - {"AccountID", c->login ? c->login->account->account_id : phosg::JSON(nullptr)}, - {"Name", p ? p->disp.name.decode(it.second->language()) : phosg::JSON(nullptr)}, - {"Version", phosg::name_for_enum(it.second->version())}, - {"Language", name_for_language_code(it.second->language())}, - {"Level", p ? p->disp.stats.level + 1 : phosg::JSON(nullptr)}, - {"Class", p ? name_for_char_class(p->disp.visual.char_class) : phosg::JSON(nullptr)}, - {"SectionID", p ? name_for_section_id(p->disp.visual.section_id) : phosg::JSON(nullptr)}, - {"LobbyID", l ? l->lobby_id : phosg::JSON(nullptr)}, - })); - } - - auto proxy_clients_json = phosg::JSON::list(); - if (this->state->proxy_server) { - for (const auto& it : this->state->proxy_server->all_sessions()) { - proxy_clients_json.emplace_back(phosg::JSON::dict({ - {"AccountID", it.second->login ? it.second->login->account->account_id : phosg::JSON(nullptr)}, - {"Name", it.second->character_name}, - {"Version", phosg::name_for_enum(it.second->version())}, - {"Language", name_for_language_code(it.second->language())}, - })); - } - } - - auto games_json = phosg::JSON::list(); - for (const auto& it : this->state->id_to_lobby) { - auto l = it.second; - if (l->is_game()) { - auto game_json = phosg::JSON::dict({ - {"ID", l->lobby_id}, - {"Name", l->name}, - {"Players", l->count_clients()}, - {"CheatsEnabled", l->check_flag(Lobby::Flag::CHEATS_ENABLED)}, - {"Episode", name_for_episode(l->episode)}, - {"HasPassword", !l->password.empty()}, - }); - if (l->episode == Episode::EP3) { - auto ep3s = l->ep3_server; - game_json.emplace("BattleInProgress", l->check_flag(Lobby::Flag::BATTLE_IN_PROGRESS)); - game_json.emplace("IsSpectatorTeam", l->check_flag(Lobby::Flag::IS_SPECTATOR_TEAM)); - game_json.emplace("MapNumber", (ep3s && ep3s->last_chosen_map) ? ep3s->last_chosen_map->map_number : phosg::JSON(nullptr)); - game_json.emplace("Rules", (ep3s && ep3s->map_and_rules) ? ep3s->map_and_rules->rules.json() : nullptr); - } 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("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("Mode", name_for_mode(l->mode)); - game_json.emplace("Difficulty", name_for_difficulty(l->difficulty)); - game_json.emplace("Quest", l->quest ? l->quest->json() : phosg::JSON(nullptr)); - } - games_json.emplace_back(std::move(game_json)); - } - } - - return phosg::JSON::dict({ - {"Clients", std::move(clients_json)}, - {"ProxyClients", std::move(proxy_clients_json)}, - {"Games", std::move(games_json)}, - }); - }); - ret.emplace("Server", this->generate_server_info_json()); - return ret; -} - -phosg::JSON HTTPServer::generate_all_json() const { - return phosg::JSON::dict({ - {"Clients", this->generate_game_server_clients_json()}, - {"ProxyClients", this->generate_proxy_server_clients_json()}, - {"Lobbies", this->generate_lobbies_json()}, - {"Server", this->generate_server_info_json()}, - }); -} - -phosg::JSON HTTPServer::generate_ep3_cards_json(bool trial) const { - auto index = call_on_event_thread>(this->state->base, [&]() { - return trial ? this->state->ep3_card_index_trial : this->state->ep3_card_index; - }); - return index->definitions_json(); -} - -phosg::JSON HTTPServer::generate_common_tables_json() const { - auto [set_v2, set_v3_v4] = call_on_event_thread, shared_ptr>>(this->state->base, [&]() { - return make_pair(this->state->common_item_set_v2, this->state->common_item_set_v3_v4); - }); - return phosg::JSON::dict({{"v1_v2", set_v2->json()}, {"v3_v4", set_v3_v4->json()}}); -} - -phosg::JSON HTTPServer::generate_rare_tables_json() const { - auto sets = call_on_event_thread>>(this->state->base, [&]() { - return this->state->rare_item_sets; - }); - phosg::JSON ret = phosg::JSON::list(); - for (const auto& it : sets) { - ret.emplace_back(it.first); +std::shared_ptr HTTPServer::generate_rare_table_list_json() const { + auto ret = make_shared(phosg::JSON::list()); + for (const auto& it : this->state->rare_item_sets) { + ret->emplace_back(it.first); } return ret; } -phosg::JSON HTTPServer::generate_rare_table_json(const std::string& table_name) const { +asio::awaitable> HTTPServer::generate_rare_table_json(const std::string& table_name) const { try { - auto colls = call_on_event_thread, shared_ptr>>(this->state->base, [&]() { - const auto& table = this->state->rare_item_sets.at(table_name); - shared_ptr name_index; - if (phosg::ends_with(table_name, "-v1")) { - name_index = this->state->item_name_index_opt(Version::DC_V1); - } else if (phosg::ends_with(table_name, "-v2")) { - name_index = this->state->item_name_index_opt(Version::PC_V2); - } else if (phosg::ends_with(table_name, "-v3")) { - name_index = this->state->item_name_index_opt(Version::GC_V3); - } else if (phosg::ends_with(table_name, "-v4")) { - name_index = this->state->item_name_index_opt(Version::BB_V4); - } - return make_pair(table, name_index); + const auto& table = this->state->rare_item_sets.at(table_name); + shared_ptr name_index; + if (table_name.ends_with("-v1")) { + name_index = this->state->item_name_index_opt(Version::DC_V1); + } else if (table_name.ends_with("-v2")) { + name_index = this->state->item_name_index_opt(Version::PC_V2); + } else if (table_name.ends_with("-v3")) { + name_index = this->state->item_name_index_opt(Version::GC_V3); + } else if (table_name.ends_with("-v4")) { + name_index = this->state->item_name_index_opt(Version::BB_V4); + } + co_return co_await call_on_thread_pool(*this->state->thread_pool, [&]() -> shared_ptr { + return make_shared(table->json(name_index)); }); - return colls.first->json(colls.second); } catch (const out_of_range&) { - throw http_error(404, "table does not exist"); + throw HTTPError(404, "Table does not exist"); } } -phosg::JSON HTTPServer::generate_quest_list_json(std::shared_ptr quest_index) { - return call_on_event_thread(this->state->base, [&]() { - return quest_index->json(); +asio::awaitable> HTTPServer::generate_quest_list_json( + std::shared_ptr quest_index) { + co_return co_await call_on_thread_pool(*this->state->thread_pool, [&]() -> shared_ptr { + return make_shared(quest_index->json()); }); } -void HTTPServer::require_GET(struct evhttp_request* req) { - if (evhttp_request_get_command(req) != EVHTTP_REQ_GET) { - throw HTTPServer::http_error(405, "GET method required for this endpoint"); +void HTTPServer::require_GET(const HTTPRequest& req) { + if (req.method != HTTPRequest::Method::GET) { + throw HTTPError(405, "GET method required for this endpoint"); } } -phosg::JSON HTTPServer::require_POST(struct evhttp_request* req) { - if (evhttp_request_get_command(req) != EVHTTP_REQ_POST) { - throw HTTPServer::http_error(405, "POST method required for this endpoint"); +phosg::JSON HTTPServer::require_POST(const HTTPRequest& req) { + if (req.method != HTTPRequest::Method::POST) { + throw HTTPError(405, "POST method required for this endpoint"); } - const evkeyvalq* headers = evhttp_request_get_input_headers(req); - const char* content_type = evhttp_find_header(headers, "Content-Type"); - if (!content_type || strcmp(content_type, "application/json")) { - throw HTTPServer::http_error(400, "POST requests must use the application/json content type"); - } - struct evbuffer* in_buf = evhttp_request_get_input_buffer(req); - return phosg::JSON::parse(evbuffer_remove_str(in_buf)); -} -void HTTPServer::handle_request(struct evhttp_request* req) { - shared_ptr ret; - uint32_t serialize_options = 0; - uint64_t start_time = phosg::now(); - string uri = evhttp_request_get_uri(req); + auto* content_type = req.get_header("content-type"); + if (!content_type || (*content_type != "application/json")) { + throw HTTPError(400, "POST requests must use the application/json content type"); + } try { - std::unordered_multimap query; - size_t query_pos = uri.find('?'); - if (query_pos != string::npos) { - query = this->parse_url_params(uri.substr(query_pos + 1)); - uri.resize(query_pos); - } + return phosg::JSON::parse(req.data); + } catch (const exception& e) { + throw HTTPError(400, string("Invalid JSON: ") + e.what()); + } +} - static const string default_format_option = "false"; - if (this->get_url_param(query, "format", &default_format_option) == "true") { +asio::awaitable> HTTPServer::handle_request(shared_ptr c, HTTPRequest&& req) { + shared_ptr ret; + uint32_t serialize_options = phosg::JSON::SerializeOption::ESCAPE_CONTROLS_ONLY; + uint64_t start_time = phosg::now(); + + this->log.info_f("{} ...", req.path); + + auto resp = make_unique(); + resp->http_version = req.http_version; + resp->response_code = 200; + resp->headers.emplace("Content-Type", "application/json"); + resp->headers.emplace("Server", "newserv"); + resp->headers.emplace("X-Newserv-Revision", GIT_REVISION_HASH); + resp->headers.emplace("X-Newserv-Build-Timestamp", phosg::format_time(BUILD_TIMESTAMP)); + + try { + auto* format_param = req.get_query_param("format"); + if (format_param && (*format_param == "true")) { serialize_options |= phosg::JSON::SerializeOption::FORMAT | phosg::JSON::SerializeOption::SORT_DICT_KEYS; } - if (this->get_url_param(query, "hex", &default_format_option) == "true") { + auto* hex_param = req.get_query_param("hex"); + if (hex_param && (*hex_param == "true")) { serialize_options |= phosg::JSON::SerializeOption::HEX_INTEGERS; } - if (uri == "/") { + if (req.path == "/") { this->require_GET(req); - ret = make_shared(this->generate_server_version_st()); + ret = this->generate_server_version(); - } else if (uri == "/y/shell-exec") { + } else if (req.path == "/y/shell-exec") { auto json = this->require_POST(req); auto command = json.get_string("command"); try { - ret = make_shared(phosg::JSON::dict( - {{"result", phosg::join(ShellCommand::dispatch_str(this->state, command), "\n")}})); + auto dispatch_res = co_await ShellCommand::dispatch_str(this->state, command); + ret = make_shared(phosg::JSON::dict({{"result", phosg::join(dispatch_res, "\n")}})); } catch (const exception& e) { - throw http_error(400, e.what()); + throw HTTPError(400, e.what()); } - } else if (uri == "/y/rare-drops/stream") { + } else if (req.path == "/y/rare-drops/stream") { this->require_GET(req); - auto c = this->enable_websockets(req); - if (!c) { - throw http_error(400, "this path requires a websocket connection"); + if (!(co_await this->enable_websockets(c, req))) { + throw HTTPError(400, "this path requires a websocket connection"); } else { this->rare_drop_subscribers.emplace(c); - auto version_message = this->generate_server_version_st(); - this->send_websocket_message(c, version_message.serialize()); - return; + auto version_message = this->generate_server_version(); + co_await c->send_websocket_message(version_message->serialize()); + co_return nullptr; } - } else if (uri == "/y/data/ep3-cards") { + } else if (req.path == "/y/data/ep3-cards") { this->require_GET(req); - ret = make_shared(this->generate_ep3_cards_json(false)); - } else if (uri == "/y/data/ep3-cards-trial") { + ret = co_await this->generate_ep3_cards_json(false); + } else if (req.path == "/y/data/ep3-cards-trial") { this->require_GET(req); - ret = make_shared(this->generate_ep3_cards_json(true)); - } else if (uri == "/y/data/common-tables") { + ret = co_await this->generate_ep3_cards_json(true); + } else if (req.path == "/y/data/common-tables") { this->require_GET(req); - ret = make_shared(this->generate_common_tables_json()); - } else if (uri == "/y/data/rare-tables") { + ret = co_await this->generate_common_tables_json(); + } else if (req.path == "/y/data/rare-tables") { this->require_GET(req); - ret = make_shared(this->generate_rare_tables_json()); - } else if (!strncmp(uri.c_str(), "/y/data/rare-tables/", 20)) { + ret = this->generate_rare_table_list_json(); + } else if (!req.path.starts_with("/y/data/rare-tables/")) { this->require_GET(req); - ret = make_shared(this->generate_rare_table_json(uri.substr(20))); - } else if (uri == "/y/data/quests") { + ret = co_await this->generate_rare_table_json(req.path.substr(20)); + } else if (req.path == "/y/data/quests") { this->require_GET(req); - ret = make_shared(this->generate_quest_list_json(this->state->quest_index(Version::GC_V3))); - } else if (uri == "/y/data/config") { + ret = co_await this->generate_quest_list_json(this->state->quest_index(Version::GC_V3)); + } else if (req.path == "/y/data/config") { this->require_GET(req); - ret = call_on_event_thread>(this->state->base, [this]() { return this->state->config_json; }); - } else if (uri == "/y/accounts") { + ret = this->state->config_json; + } else if (req.path == "/y/accounts") { this->require_GET(req); - ret = make_shared(this->generate_accounts_json()); - } else if (uri == "/y/clients") { + ret = this->generate_accounts_json(); + } else if (req.path == "/y/clients") { this->require_GET(req); - ret = make_shared(this->generate_game_server_clients_json()); - } else if (uri == "/y/proxy-clients") { + ret = this->generate_clients_json(); + } else if (req.path == "/y/lobbies") { this->require_GET(req); - ret = make_shared(this->generate_proxy_server_clients_json()); - } else if (uri == "/y/lobbies") { + ret = this->generate_lobbies_json(); + } else if (req.path == "/y/server") { this->require_GET(req); - ret = make_shared(this->generate_lobbies_json()); - } else if (uri == "/y/server") { + ret = this->generate_server_info_json(); + } else if (req.path == "/y/summary") { this->require_GET(req); - ret = make_shared(this->generate_server_info_json()); - } else if (uri == "/y/summary") { - this->require_GET(req); - ret = make_shared(this->generate_summary_json()); + ret = this->generate_summary_json(); } else { - throw http_error(404, "unknown action"); + throw HTTPError(404, "unknown action"); } - } catch (const http_error& e) { - unique_ptr out_buffer(evbuffer_new(), evbuffer_free); - evbuffer_add_printf(out_buffer.get(), "%s", e.what()); - this->send_response(req, e.code, "text/plain", out_buffer.get()); - return; - + } catch (const HTTPError& e) { + ret = make_shared(phosg::JSON::dict({{"Error", true}, {"Message", e.what()}})); + resp->response_code = e.code; } catch (const exception& e) { - unique_ptr out_buffer(evbuffer_new(), evbuffer_free); - evbuffer_add_printf(out_buffer.get(), "Error during request: %s", e.what()); - this->send_response(req, 500, "text/plain", out_buffer.get()); - server_log.warning("internal server error during http request: %s", e.what()); - return; + ret = make_shared(phosg::JSON::dict({{"Error", true}, {"Message", e.what()}})); + resp->response_code = 500; } if (!ret) { @@ -1318,23 +840,21 @@ void HTTPServer::handle_request(struct evhttp_request* req) { } uint64_t handler_end = phosg::now(); - unique_ptr out_buffer(evbuffer_new(), evbuffer_free); - string* serialized = new string(ret->serialize(phosg::JSON::SerializeOption::ESCAPE_CONTROLS_ONLY | serialize_options)); - size_t size = serialized->size(); + resp->data = co_await call_on_thread_pool(*this->state->thread_pool, [&]() -> string { + return ret->serialize(serialize_options, 0); + }); uint64_t serialize_end = phosg::now(); - auto cleanup = +[](const void*, size_t, void* s) -> void { - delete reinterpret_cast(s); - }; - evbuffer_add_reference(out_buffer.get(), serialized->data(), serialized->size(), cleanup, serialized); - this->send_response(req, 200, "application/json", out_buffer.get()); string handler_time = phosg::format_duration(handler_end - start_time); string serialize_time = phosg::format_duration(serialize_end - handler_end); - string size_str = phosg::format_size(size); - server_log.info("[HTTPServer] %s in [handler: %s, serialize: %s, size: %s]", - uri.c_str(), handler_time.c_str(), serialize_time.c_str(), size_str.c_str()); + string size_str = phosg::format_size(resp->data.size()); + this->log.info_f("{} in [handler: {}, serialize: {}, size: {}]", + req.path, handler_time, serialize_time, size_str); + + co_return resp; } -void HTTPServer::thread_fn() { - event_base_loop(this->base.get(), EVLOOP_NO_EXIT_ON_EMPTY); +asio::awaitable HTTPServer::destroy_client(std::shared_ptr c) { + this->rare_drop_subscribers.erase(c); + co_return; } diff --git a/src/HTTPServer.hh b/src/HTTPServer.hh index 9bff1c86..28f45e3a 100644 --- a/src/HTTPServer.hh +++ b/src/HTTPServer.hh @@ -1,122 +1,50 @@ #pragma once -#include -#include -#include #include #include #include -#include "ProxyServer.hh" +#include "AsyncHTTPServer.hh" #include "ServerState.hh" -class HTTPServer { +class HTTPServer : public AsyncHTTPServer<> { public: - // shared_base should be null unless the HTTP server should run on the main - // thread (on Windows). - HTTPServer(std::shared_ptr state, std::shared_ptr shared_base); - + explicit HTTPServer(std::shared_ptr state); HTTPServer(const HTTPServer&) = delete; HTTPServer(HTTPServer&&) = delete; HTTPServer& operator=(const HTTPServer&) = delete; HTTPServer& operator=(HTTPServer&&) = delete; virtual ~HTTPServer() = default; - void listen(const std::string& socket_path); - void listen(const std::string& addr, int port); - void listen(int port); - void add_socket(int fd); - - void schedule_stop(); - void wait_for_stop(); - - void send_rare_drop_notification(std::shared_ptr message); + asio::awaitable send_rare_drop_notification(std::shared_ptr message); protected: - class http_error : public std::runtime_error { - public: - http_error(int code, const std::string& what); - 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 state; - std::shared_ptr base; - std::shared_ptr http; - std::thread th; // Not used on Windows + std::unordered_set> rare_drop_subscribers; - std::unordered_set> rare_drop_subscribers; + std::shared_ptr generate_server_version() const; + std::shared_ptr generate_account_json(std::shared_ptr a) const; + std::shared_ptr generate_client_json( + std::shared_ptr c, std::shared_ptr item_name_index) const; + std::shared_ptr generate_lobby_json( + std::shared_ptr l, std::shared_ptr item_name_index) const; + std::shared_ptr generate_accounts_json() const; + std::shared_ptr generate_clients_json() const; + std::shared_ptr generate_server_info_json() const; + std::shared_ptr generate_lobbies_json() const; + std::shared_ptr generate_summary_json() const; + std::shared_ptr generate_all_json() const; - std::unordered_map> bev_to_websocket_client; + asio::awaitable> generate_ep3_cards_json(bool trial) const; + asio::awaitable> generate_common_tables_json() const; + std::shared_ptr generate_rare_table_list_json() const; + asio::awaitable> generate_rare_table_json(const std::string& table_name) const; + asio::awaitable> generate_quest_list_json(std::shared_ptr q); - static void require_GET(struct evhttp_request* req); - static phosg::JSON require_POST(struct evhttp_request* req); + void require_GET(const HTTPRequest& req); + phosg::JSON require_POST(const HTTPRequest& req); - std::shared_ptr 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 c, const std::string& message, uint8_t opcode = 0x01); - - virtual void handle_websocket_message(std::shared_ptr c, uint8_t opcode, const std::string& message); - virtual void handle_websocket_disconnect(std::shared_ptr c); - - void thread_fn(); - - static void dispatch_handle_request(struct evhttp_request* req, void* ctx); - void handle_request(struct evhttp_request* req); - - static const std::unordered_map explanation_for_response_code; - static void send_response(struct evhttp_request* req, int code, const char* content_type, struct evbuffer* b); - static void send_response(struct evhttp_request* req, int code, const char* content_type, const char* fmt, ...); - - static std::unordered_multimap parse_url_params(const std::string& query); - static std::unordered_map parse_url_params_unique(const std::string& query); - static const std::string& get_url_param( - const std::unordered_multimap& params, - const std::string& key, - const std::string* _default = nullptr); - - static phosg::JSON generate_server_version_st(); - static phosg::JSON generate_client_config_json_st(const Client::Config& config); - static phosg::JSON generate_account_json_st(std::shared_ptr a); - static phosg::JSON generate_game_client_json_st(std::shared_ptr c, std::shared_ptr item_name_index); - static phosg::JSON generate_proxy_client_json_st(std::shared_ptr ses); - static phosg::JSON generate_lobby_json_st(std::shared_ptr l, std::shared_ptr item_name_index); - phosg::JSON generate_accounts_json() const; - phosg::JSON generate_game_server_clients_json() const; - phosg::JSON generate_proxy_server_clients_json() const; - phosg::JSON generate_server_info_json() const; - phosg::JSON generate_lobbies_json() const; - phosg::JSON generate_summary_json() const; - phosg::JSON generate_all_json() const; - - phosg::JSON generate_ep3_cards_json(bool trial) const; - phosg::JSON generate_common_tables_json() const; - phosg::JSON generate_rare_tables_json() const; - phosg::JSON generate_rare_table_json(const std::string& table_name) const; - phosg::JSON generate_quest_list_json(std::shared_ptr q); + virtual asio::awaitable> handle_request(std::shared_ptr c, HTTPRequest&& req); + virtual asio::awaitable destroy_client(std::shared_ptr c); }; diff --git a/src/IPFrameInfo.cc b/src/IPFrameInfo.cc index 11c54354..39d77197 100644 --- a/src/IPFrameInfo.cc +++ b/src/IPFrameInfo.cc @@ -13,9 +13,6 @@ static inline uint16_t collapse_checksum(uint32_t sum) { return (sum & 0xFFFF) + (sum >> 16); } -FrameInfo::FrameInfo(LinkType link_type, const string& data) - : FrameInfo(link_type, data.data(), data.size()) {} - FrameInfo::FrameInfo(LinkType link_type, const void* header_start, size_t size) : FrameInfo() { this->link_type = link_type; @@ -126,37 +123,37 @@ string FrameInfo::header_str() const { string ret; if (this->ether) { - ret = phosg::string_printf( - "ETHER:%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX->%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX", + ret = std::format( + "ETHER:{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}->{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}", this->ether->src_mac[0], this->ether->src_mac[1], this->ether->src_mac[2], this->ether->src_mac[3], this->ether->src_mac[4], this->ether->src_mac[5], this->ether->dest_mac[0], this->ether->dest_mac[1], this->ether->dest_mac[2], this->ether->dest_mac[3], this->ether->dest_mac[4], this->ether->dest_mac[5]); } else if (this->hdlc) { - ret = phosg::string_printf("HDLC:%02hhX/%02hhX", this->hdlc->address, this->hdlc->control); + ret = std::format("HDLC:{:02X}/{:02X}", this->hdlc->address, this->hdlc->control); } else { return ""; } if (this->arp) { - ret += phosg::string_printf( - ",ARP,hw_type=%04hX,proto_type=%04hX,hw_addr_len=%02hhX,proto_addr_len=%02hhX,op=%04hX", - this->arp->hardware_type.load(), this->arp->protocol_type.load(), this->arp->hwaddr_len, this->arp->paddr_len, this->arp->operation.load()); + ret += std::format( + ",ARP,hw_type={:04X},proto_type={:04X},hw_addr_len={:02X},proto_addr_len={:02X},op={:04X}", + this->arp->hardware_type, this->arp->protocol_type, this->arp->hwaddr_len, this->arp->paddr_len, this->arp->operation); } else if (this->ipv4) { - ret += phosg::string_printf( - ",IPv4,size=%04hX,src=%08" PRIX32 ",dest=%08" PRIX32, - this->ipv4->size.load(), this->ipv4->src_addr.load(), this->ipv4->dest_addr.load()); + ret += std::format( + ",IPv4,size={:04X},src={:08X},dest={:08X}", + this->ipv4->size, this->ipv4->src_addr, this->ipv4->dest_addr); if (this->udp) { - ret += phosg::string_printf( - ",UDP,src_port=%04hX,dest_port=%04hX,size=%04hX", - this->udp->src_port.load(), this->udp->dest_port.load(), this->udp->size.load()); + ret += std::format( + ",UDP,src_port={:04X},dest_port={:04X},size={:04X}", + this->udp->src_port, this->udp->dest_port, this->udp->size); } else if (this->tcp) { - ret += phosg::string_printf( - ",TCP,src_port=%04hX,dest_port=%04hX,seq=%08" PRIX32 ",ack=%08" PRIX32 ",flags=%04hX(", - this->tcp->src_port.load(), this->tcp->dest_port.load(), this->tcp->seq_num.load(), this->tcp->ack_num.load(), this->tcp->flags.load()); + ret += std::format( + ",TCP,src_port={:04X},dest_port={:04X},seq={:08X},ack={:08X},flags={:04X}(", + this->tcp->src_port, this->tcp->dest_port, this->tcp->seq_num, this->tcp->ack_num, this->tcp->flags); if (this->tcp->flags & TCPHeader::Flag::FIN) { ret += "FIN,"; } @@ -175,14 +172,14 @@ string FrameInfo::header_str() const { ret += ')'; } else { - ret += phosg::string_printf(",proto=%02hhX", this->ipv4->protocol); + ret += std::format(",proto={:02X}", this->ipv4->protocol); } } else { if (this->ether) { - ret += phosg::string_printf(",proto=%04hX", this->ether->protocol.load()); + ret += std::format(",proto={:04X}", this->ether->protocol); } else if (this->hdlc) { - ret += phosg::string_printf(",proto=%04hX", this->hdlc->protocol.load()); + ret += std::format(",proto={:04X}", this->hdlc->protocol); } } diff --git a/src/IPFrameInfo.hh b/src/IPFrameInfo.hh index 2fc7a91e..c06afed3 100644 --- a/src/IPFrameInfo.hh +++ b/src/IPFrameInfo.hh @@ -156,7 +156,6 @@ struct FrameInfo { size_t payload_size = 0; FrameInfo() = default; - FrameInfo(LinkType link_type, const std::string& data); FrameInfo(LinkType link_type, const void* data, size_t size); std::string header_str() const; diff --git a/src/IPStackSimulator.cc b/src/IPStackSimulator.cc index ac6cd47d..75531032 100644 --- a/src/IPStackSimulator.cc +++ b/src/IPStackSimulator.cc @@ -1,10 +1,5 @@ #include "IPStackSimulator.hh" -#include -#include -#include -#include -#include #include #include @@ -14,38 +9,41 @@ #include #include "DNSServer.hh" +#include "GameServer.hh" #include "IPFrameInfo.hh" #include "Loggers.hh" using namespace std; -static const size_t DEFAULT_RESEND_PUSH_USECS = 200000; // 200ms - -static string unescape_hdlc_frame(const void* data, size_t size) { - phosg::StringReader r(data, size); - if (r.get_u8(data) != 0x7E) { +static size_t unescape_hdlc_frame_inplace(void* vdata, size_t size) { + uint8_t* data = reinterpret_cast(vdata); + if (size < 2) { + throw runtime_error("escaped HDLC frame is too small"); + } + if (data[0] != 0x7E) { throw runtime_error("HDLC frame does not begin with 7E"); } - string ret("\x7E", 1); + if (data[size - 1] != 0x7E) { + throw runtime_error("HDLC frame does not end with 7E"); + } - while (r.get_u8(false) != 0x7E) { - uint8_t ch = r.get_u8(); + size_t read_offset = 1; + size_t write_offset = 1; + while (read_offset < size - 1) { + uint8_t ch = data[read_offset++]; if (ch == 0x7D) { - ch = r.get_u8(); - if (ch == 0x7E) { + if (read_offset >= size - 1) { throw runtime_error("abort sequence received"); } - ret.push_back(ch ^ 0x20); - } else { - ret.push_back(ch); + ch = data[read_offset++] ^ 0x20; } + data[write_offset++] = ch; } - ret.push_back(0x7E); - return ret; -} - -static string unescape_hdlc_frame(const string& data) { - return unescape_hdlc_frame(data.data(), data.size()); + if (write_offset > size - 1) { + throw logic_error("unescaping HDLC frame resulted in longer data string"); + } + data[write_offset++] = 0x7E; + return write_offset; } static string escape_hdlc_frame(const void* data, size_t size, uint32_t escape_control_character_flags = 0xFFFFFFFF) { @@ -100,404 +98,372 @@ static __attribute__((unused)) inline bool seq_num_greater_or_equal(uint32_t a, return (a == b) || seq_num_greater(a, b); } -string IPStackSimulator::str_for_ipv4_netloc(uint32_t addr, uint16_t port) { - be_uint32_t be_addr = addr; - char addr_str[INET_ADDRSTRLEN]; - if (!inet_ntop(AF_INET, &be_addr, addr_str, INET_ADDRSTRLEN)) { - return phosg::string_printf(":%hu", port); - } else { - return phosg::string_printf("%s:%hu", addr_str, port); +IPSSClient::TCPConnection::TCPConnection(std::shared_ptr client) + : client(client), + resend_push_timer(*client->io_context) {} + +void IPSSClient::TCPConnection::drain_outbound_data(size_t size) { + this->outbound_data_bytes -= size; + while (size > 0 && !this->outbound_data.empty()) { + auto& front_block = this->outbound_data.front(); + if (front_block.size() <= size) { + size -= front_block.size(); + this->outbound_data.pop_front(); + } else { + front_block = front_block.substr(size); + size = 0; + } + } + if (size > 0) { + throw logic_error("attempted to drain more outbound data than was present"); } } -string IPStackSimulator::str_for_tcp_connection(shared_ptr c, const IPClient::TCPConnection& conn) { - uint64_t key = IPStackSimulator::tcp_conn_key_for_connection(conn); - string server_netloc_str = str_for_ipv4_netloc(conn.server_addr, conn.server_port); - string client_netloc_str = str_for_ipv4_netloc(c->ipv4_addr, conn.client_port); - int fd = bufferevent_getfd(c->bev.get()); - return phosg::string_printf("%d+%016" PRIX64 " (%s -> %s)", - fd, key, client_netloc_str.c_str(), server_netloc_str.c_str()); +void IPSSClient::TCPConnection::linearize_outbound_data(size_t size) { + while (this->outbound_data.size() > 1 && this->outbound_data.front().size() < size) { + auto second_block_it = this->outbound_data.begin(); + second_block_it++; + this->outbound_data.front() += *second_block_it; + this->outbound_data.erase(second_block_it); + } } -IPStackSimulator::IPStackSimulator( - shared_ptr base, - shared_ptr state) - : base(base), - state(state), - next_network_id(1), - pcap_text_log_file(state->ip_stack_debug ? fopen("IPStackSimulator-Log.txt", "wt") : nullptr) { +IPSSClient::IPSSClient( + shared_ptr sim, + uint64_t network_id, + VirtualNetworkProtocol protocol, + asio::ip::tcp::socket&& sock) + : io_context(sim->get_io_context()), + sim(sim), + network_id(network_id), + sock(std::move(sock)), + protocol(protocol), + mac_addr(0), + ipv4_addr(0), + idle_timeout_timer(*sim->get_io_context()) { + this->reschedule_idle_timeout(); +} + +void IPSSClient::reschedule_idle_timeout() { + auto sim = this->sim.lock(); + if (!sim) { + throw runtime_error("cannot reschedule idle timeout when simulator is missing"); + } + this->idle_timeout_timer.cancel(); + this->idle_timeout_timer.expires_after(std::chrono::microseconds(sim->get_state()->client_idle_timeout_usecs)); + this->idle_timeout_timer.async_wait([this, sim](std::error_code ec) { + if (!ec) { + sim->log.info_f("Idle timeout expired on N-{:X}", this->network_id); + this->sock.close(); + } + }); +} + +IPSSChannel::IPSSChannel( + std::shared_ptr sim, + std::weak_ptr ipss_client, + std::weak_ptr tcp_conn, + Version version, + uint8_t language, + const std::string& name, + phosg::TerminalFormat terminal_send_color, + phosg::TerminalFormat terminal_recv_color) + : Channel(version, language, name, terminal_send_color, terminal_recv_color), + sim(sim), + ipss_client(ipss_client), + tcp_conn(tcp_conn), + data_available_signal(sim->io_context->get_executor()) {} + +std::string IPSSChannel::default_name() const { + auto ipc = this->ipss_client.lock(); + if (ipc) { + string addr_str = str_for_endpoint(ipc->sock.remote_endpoint()); + return std::format("ipss:N-{}:{}", ipc->network_id, addr_str); + } else { + return std::format("ipss:N-{}:__unknown_address__", ipc->network_id); + } +} + +bool IPSSChannel::connected() const { + auto ipss_client = this->ipss_client.lock(); + auto tcp_conn = this->tcp_conn.lock(); + return tcp_conn && ipss_client && ipss_client->sock.is_open(); +} + +void IPSSChannel::disconnect() { + auto c = this->ipss_client.lock(); + auto conn = this->tcp_conn.lock(); + if (c && conn) { + sim->schedule_send_pending_push_frame(conn, 0); + this->tcp_conn.reset(); + this->ipss_client.reset(); + this->data_available_signal.set(); + } +} + +void IPSSChannel::add_inbound_data(const void* data, size_t size) { + // If recv_buf is not null, there is a coroutine waiting to receive data, and + // inbound_data must be empty. Copy the data directly to the waiting + // coroutine's buffer, and put the rest in this->inbound_data if needed. + if (this->recv_buf) { + size_t direct_size = min(this->recv_buf_size, size); + memcpy(this->recv_buf, data, direct_size); + data = reinterpret_cast(data) + direct_size; + size -= direct_size; + this->recv_buf_size -= direct_size; + this->recv_buf = this->recv_buf_size + ? reinterpret_cast(this->recv_buf) + direct_size + : nullptr; + } + + // If there is still data left after the above, add it to the pending inbound + // data buffer + if (size > 0) { + this->inbound_data.emplace_back(reinterpret_cast(data), size); + } + + // Notify the waiting coroutine (if any) that data is available + this->data_available_signal.set(); +} + +void IPSSChannel::send_raw(string&& data) { + auto c = this->ipss_client.lock(); + if (!c) { + return; + } + auto conn = this->tcp_conn.lock(); + if (!conn) { + return; + } + auto sim = c->sim.lock(); + if (!sim) { + return; + } + + conn->outbound_data_bytes += data.size(); + conn->outbound_data.emplace_back(std::move(data)); + + // If we're already waiting for an ACK from the remote client, don't send + // another PSH right now - we will either send another PSH when we receive + // the ACK or will retry sending the PSH soon (which will then include the + // new data, if it's within the MTU from the last acked sequence number). + if (!conn->awaiting_ack) { + sim->schedule_send_pending_push_frame(conn, 0); + } + c->reschedule_idle_timeout(); +} + +asio::awaitable IPSSChannel::recv_raw(void* data, size_t size) { + if (this->recv_buf) { + throw logic_error("recv_raw called again when it was already pending"); + } + + // Receive as much data as possible from the pending inbound data buffer + while (size && !this->inbound_data.empty()) { + auto& front_buf = this->inbound_data.front(); + if (size >= front_buf.size()) { + memcpy(data, front_buf.data(), front_buf.size()); + data = reinterpret_cast(data) + front_buf.size(); + size -= front_buf.size(); + this->inbound_data.pop_front(); + } else { + memcpy(data, front_buf.data(), size); + data = reinterpret_cast(data) + size; + front_buf = front_buf.substr(size); + size = 0; + } + } + + // If there's still more data to read, block until it's available + // (add_inbound_data is responsible for waking this coroutine) + if (size > 0) { + this->recv_buf = data; + this->recv_buf_size = size; + while (this->recv_buf) { + if (!this->connected()) { + throw runtime_error("IPSS channel closed"); + } + this->data_available_signal.clear(); + co_await this->data_available_signal.wait(); + } + } +} + +IPStackSimulator::IPStackSimulator(shared_ptr state) + : Server(state->io_context, "[IPStackSimulator] "), state(state) { this->host_mac_address_bytes.clear(0x90); this->broadcast_mac_address_bytes.clear(0xFF); } -IPStackSimulator::~IPStackSimulator() { - if (this->pcap_text_log_file) { - fclose(this->pcap_text_log_file); - } -} - -void IPStackSimulator::listen(const string& name, const string& socket_path, Protocol proto) { - int fd = phosg::listen(socket_path, 0, SOMAXCONN); - ip_stack_simulator_log.info("Listening on Unix socket %s on fd %d as %s", socket_path.c_str(), fd, name.c_str()); - this->add_socket(name, fd, proto); -} - -void IPStackSimulator::listen(const string& name, const string& addr, int port, Protocol proto) { +void IPStackSimulator::listen(const std::string& name, const string& addr, int port, VirtualNetworkProtocol protocol) { if (port == 0) { - this->listen(name, addr, proto); - } else { - int fd = phosg::listen(addr, port, SOMAXCONN); - string netloc_str = phosg::render_netloc(addr, port); - ip_stack_simulator_log.info("Listening on TCP interface %s on fd %d as %s", netloc_str.c_str(), fd, name.c_str()); - this->add_socket(name, fd, proto); + throw std::runtime_error("Listening port cannot be zero"); } -} - -void IPStackSimulator::listen(const string& name, int port, Protocol proto) { - this->listen(name, "", port, proto); -} - -void IPStackSimulator::add_socket(const string& name, int fd, Protocol proto) { - unique_listener l( - evconnlistener_new( - this->base.get(), - IPStackSimulator::dispatch_on_listen_accept, - this, - LEV_OPT_REUSEABLE, - 0, - fd), - evconnlistener_free); - this->listening_sockets.emplace(piecewise_construct, forward_as_tuple(fd), forward_as_tuple(name, proto, std::move(l))); -} - -shared_ptr IPStackSimulator::get_network(uint64_t network_id) const { - return this->network_id_to_client.at(network_id); + asio::ip::address asio_addr = addr.empty() ? asio::ip::address_v4::any() : asio::ip::make_address(addr); + auto sock = make_shared(); + sock->name = name; + sock->endpoint = asio::ip::tcp::endpoint(asio_addr, port); + sock->protocol = protocol; + this->add_socket(std::move(sock)); } 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 - // Episode III will think they're talking to a remote network and won't reject - // the connection. - if ((remote_addr & 0xFF000000) != 0x23000000) { - return 0x23232323; + // Episode III will think they're talking to a remote network and won't + // reject the connection. + return ((remote_addr & 0xFF000000) == 0x23000000) ? 0x24242424 : 0x23232323; +} + +uint64_t IPStackSimulator::tcp_conn_key_for_connection(std::shared_ptr conn) { + return (static_cast(conn->server_addr) << 32) | + (static_cast(conn->server_port) << 16) | + static_cast(conn->client_port); +} + +uint64_t IPStackSimulator::tcp_conn_key_for_client_frame(const IPv4Header& ipv4, const TCPHeader& tcp) { + return (static_cast(ipv4.dest_addr) << 32) | + (static_cast(tcp.dest_port) << 16) | + static_cast(tcp.src_port); +} + +uint64_t IPStackSimulator::tcp_conn_key_for_client_frame(const FrameInfo& fi) { + if (!fi.ipv4 || !fi.tcp) { + throw logic_error("tcp_conn_key_for_frame called on non-TCP frame"); + } + return IPStackSimulator::tcp_conn_key_for_client_frame(*fi.ipv4, *fi.tcp); +} + +string IPStackSimulator::str_for_ipv4_netloc(uint32_t addr, uint16_t port) { + be_uint32_t be_addr = addr; + char addr_str[INET_ADDRSTRLEN]; + if (!inet_ntop(AF_INET, &be_addr, addr_str, INET_ADDRSTRLEN)) { + return std::format(":{}", port); } else { - return 0x24242424; + return std::format("{}:{}", addr_str, port); } } -IPStackSimulator::IPClient::IPClient( - shared_ptr sim, uint64_t network_id, Protocol protocol, struct bufferevent* bev) - : sim(sim), - network_id(network_id), - bev(bev, bufferevent_free), - protocol(protocol), - mac_addr(0), - ipv4_addr(0), - idle_timeout_event(event_new(sim->base.get(), -1, EV_TIMEOUT, &IPStackSimulator::IPClient::dispatch_on_idle_timeout, this), event_free) { - uint64_t idle_timeout_usecs = sim->state->client_idle_timeout_usecs; - struct timeval tv = phosg::usecs_to_timeval(idle_timeout_usecs); - event_add(this->idle_timeout_event.get(), &tv); +string IPStackSimulator::str_for_tcp_connection( + shared_ptr c, std::shared_ptr conn) { + uint64_t key = IPStackSimulator::tcp_conn_key_for_connection(conn); + string server_netloc_str = str_for_ipv4_netloc(conn->server_addr, conn->server_port); + string client_netloc_str = str_for_ipv4_netloc(c->ipv4_addr, conn->client_port); + return std::format("{:016X} ({} -> {})", key, client_netloc_str, server_netloc_str); } -void IPStackSimulator::IPClient::dispatch_on_idle_timeout(evutil_socket_t, short, void* ctx) { - reinterpret_cast(ctx)->on_idle_timeout(); +asio::awaitable IPStackSimulator::send_ethernet_tapserver_frame( + shared_ptr c, FrameInfo::Protocol proto, const void* data, size_t size) const { + + struct { + phosg::le_uint16_t frame_size; + EthernetHeader ether; + } header; + static_assert(sizeof(header) == 0x10, "Ethernet tapserver header size is incorrect"); + + header.ether.dest_mac = c->mac_addr; + header.ether.src_mac = this->host_mac_address_bytes; + switch (proto) { + case FrameInfo::Protocol::NONE: + throw logic_error("layer 3 protocol not specified"); + case FrameInfo::Protocol::LCP: + throw logic_error("cannot send LCP frame over Ethernet"); + case FrameInfo::Protocol::IPV4: + header.ether.protocol = 0x0800; + break; + case FrameInfo::Protocol::ARP: + header.ether.protocol = 0x0806; + break; + default: + throw logic_error("unknown layer 3 protocol"); + } + header.frame_size = size + sizeof(EthernetHeader); + + array bufs{ + asio::buffer(static_cast(&header), sizeof(header)), + asio::buffer(data, size)}; + co_await asio::async_write(c->sock, bufs, asio::use_awaitable); } -void IPStackSimulator::IPClient::on_idle_timeout() { - auto sim = this->sim.lock(); - if (sim) { - ip_stack_simulator_log.info("Idle timeout expired on virtual network %d", bufferevent_getfd(this->bev.get())); - sim->disconnect_client(this->network_id); +asio::awaitable IPStackSimulator::send_hdlc_frame( + shared_ptr c, FrameInfo::Protocol proto, const void* data, size_t size, bool is_raw) const { + + HDLCHeader hdlc; + hdlc.start_sentinel1 = 0x7E; + hdlc.address = 0xFF; + hdlc.control = 0x03; + switch (proto) { + case FrameInfo::Protocol::NONE: + throw logic_error("layer 3 protocol not specified"); + case FrameInfo::Protocol::LCP: + hdlc.protocol = 0xC021; + break; + case FrameInfo::Protocol::PAP: + hdlc.protocol = 0xC023; + break; + case FrameInfo::Protocol::IPCP: + hdlc.protocol = 0x8021; + break; + case FrameInfo::Protocol::IPV4: + hdlc.protocol = 0x0021; + break; + case FrameInfo::Protocol::ARP: + throw runtime_error("cannot send ARP packets over HDLC"); + default: + throw logic_error("unknown layer 3 protocol"); + } + + phosg::StringWriter w; + w.put(hdlc); + w.write(data, size); + w.put_u16l(FrameInfo::computed_hdlc_checksum(w.str().data() + 1, w.size() - 1)); + w.put_u8(0x7E); + + string escaped = escape_hdlc_frame(w.str(), c->hdlc_escape_control_character_flags); + if (this->log.debug_f("Sending HDLC frame to virtual network (escaped to {:X} bytes)", escaped.size())) { + phosg::print_data(stderr, w.str()); + } + + if (!is_raw) { + phosg::le_uint16_t frame_size = escaped.size(); + array bufs{ + asio::buffer(static_cast(&frame_size), sizeof(frame_size)), + asio::buffer(escaped.data(), escaped.size())}; + co_await asio::async_write(c->sock, bufs, asio::use_awaitable); } else { - ip_stack_simulator_log.info("Idle timeout expired on virtual network %d, but simulator is missing", bufferevent_getfd(this->bev.get())); + co_await asio::async_write(c->sock, asio::buffer(escaped.data(), escaped.size()), asio::use_awaitable); } } -static void flush_and_free_bufferevent(struct bufferevent* bev) { - bufferevent_flush(bev, EV_READ | EV_WRITE, BEV_FINISHED); - bufferevent_free(bev); -} - -IPStackSimulator::IPClient::TCPConnection::TCPConnection() - : server_bev(nullptr, flush_and_free_bufferevent), - pending_data(evbuffer_new(), evbuffer_free), - resend_push_event(nullptr, event_free), - awaiting_first_ack(true), - server_addr(0), - server_port(0), - client_port(0), - next_client_seq(0), - acked_server_seq(0), - resend_push_usecs(DEFAULT_RESEND_PUSH_USECS), - next_push_max_frame_size(1024), - max_frame_size(1024), - bytes_received(0), - bytes_sent(0) {} - -void IPStackSimulator::disconnect_client(uint64_t network_id) { - ip_stack_simulator_log.info("Virtual network N-%" PRIu64 " disconnected", network_id); - this->network_id_to_client.erase(network_id); -} - -void IPStackSimulator::dispatch_on_listen_accept( - struct evconnlistener* listener, evutil_socket_t fd, - struct sockaddr* address, int socklen, void* ctx) { - reinterpret_cast(ctx)->on_listen_accept(listener, fd, address, socklen); -} - -void IPStackSimulator::on_listen_accept(struct evconnlistener* listener, evutil_socket_t fd, struct sockaddr*, int) { - struct sockaddr_storage remote_addr; - phosg::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); - - const ListeningSocket* listening_socket; - try { - listening_socket = &this->listening_sockets.at(listen_fd); - } catch (const out_of_range&) { - ip_stack_simulator_log.info("Virtual network fd %d connected via unknown listener %d; disconnecting", fd, listen_fd); - close(fd); - return; - } - - 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, BEV_OPT_CLOSE_ON_FREE | BEV_OPT_DEFER_CALLBACKS); - auto c = make_shared(this->shared_from_this(), network_id, listening_socket->protocol, bev); - this->network_id_to_client.emplace(c->network_id, c); - - bufferevent_setcb(bev, &IPStackSimulator::IPClient::dispatch_on_client_input, nullptr, - &IPStackSimulator::IPClient::dispatch_on_client_error, c.get()); - bufferevent_enable(bev, EV_READ | EV_WRITE); -} - -void IPStackSimulator::dispatch_on_listen_error( - struct evconnlistener* listener, void* ctx) { - reinterpret_cast(ctx)->on_listen_error(listener); -} - -void IPStackSimulator::on_listen_error(struct evconnlistener* listener) { - int err = EVUTIL_SOCKET_ERROR(); - ip_stack_simulator_log.error("Failure on listening socket %d: %d (%s)", - evconnlistener_get_fd(listener), err, evutil_socket_error_to_string(err)); - event_base_loopexit(this->base.get(), nullptr); -} - -void IPStackSimulator::IPClient::dispatch_on_client_input(struct bufferevent* bev, void* ctx) { - reinterpret_cast(ctx)->on_client_input(bev); -} - -void IPStackSimulator::IPClient::on_client_input(struct bufferevent* bev) { - struct evbuffer* buf = bufferevent_get_input(bev); - - auto sim = this->sim.lock(); - if (!sim) { - size_t bytes = evbuffer_get_length(buf); - ip_stack_simulator_log.warning("Ignoring data from unregistered virtual network (0x%zX bytes)", bytes); - evbuffer_drain(buf, bytes); - return; - } - - uint64_t idle_timeout_usecs = sim ? sim->state->client_idle_timeout_usecs : 60000000; - struct timeval tv = phosg::usecs_to_timeval(idle_timeout_usecs); - event_add(this->idle_timeout_event.get(), &tv); - - switch (this->protocol) { - case Protocol::ETHERNET_TAPSERVER: - case Protocol::HDLC_TAPSERVER: - while (evbuffer_get_length(buf) >= 2) { - uint16_t frame_size; - evbuffer_copyout(buf, &frame_size, 2); - if (evbuffer_get_length(buf) < static_cast(frame_size + 2)) { - break; // No complete frame available; done for now - } - - evbuffer_drain(buf, 2); - string frame(frame_size, '\0'); - evbuffer_remove(buf, frame.data(), frame.size()); - - try { - sim->on_client_frame(this->shared_from_this(), frame); - } catch (const exception& e) { - if (ip_stack_simulator_log.warning("Failed to process frame: %s", e.what())) { - phosg::print_data(stderr, frame); - } - } - } - break; - case Protocol::HDLC_RAW: - while (evbuffer_get_length(buf) >= 2) { - struct evbuffer_ptr res = evbuffer_search(buf, "\x7E", 1, nullptr); - if (res.pos < 0) { - break; - } - size_t start_offset = res.pos; - - if (evbuffer_ptr_set(buf, &res, 1, EVBUFFER_PTR_ADD)) { - ip_stack_simulator_log.warning("Cannot advance search for end of frame"); - break; - } - - struct evbuffer_ptr end_res = evbuffer_search(buf, "\x7E", 1, &res); - if (end_res.pos < 0) { - break; - } - size_t frame_size = end_res.pos + 1 - start_offset; - - if (start_offset) { - evbuffer_drain(buf, start_offset); - } - - string frame(frame_size, '\0'); - evbuffer_remove(buf, frame.data(), frame.size()); - - try { - sim->on_client_frame(this->shared_from_this(), frame); - } catch (const exception& e) { - if (ip_stack_simulator_log.warning("Failed to process frame: %s", e.what())) { - phosg::print_data(stderr, frame); - } - } - } - break; - } -} - -void IPStackSimulator::IPClient::dispatch_on_client_error(struct bufferevent* bev, short events, void* ctx) { - reinterpret_cast(ctx)->on_client_error(bev, events); -} -void IPStackSimulator::IPClient::on_client_error(struct bufferevent*, short events) { - if (events & BEV_EVENT_ERROR) { - int err = EVUTIL_SOCKET_ERROR(); - ip_stack_simulator_log.warning("Virtual network caused error %d (%s)", err, evutil_socket_error_to_string(err)); - } - if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) { - auto sim = this->sim.lock(); - if (sim) { - sim->disconnect_client(this->network_id); - } - } -} - -void IPStackSimulator::send_layer3_frame(shared_ptr c, FrameInfo::Protocol proto, const string& data) const { - this->send_layer3_frame(c, proto, data.data(), data.size()); -} - -void IPStackSimulator::send_layer3_frame(shared_ptr c, FrameInfo::Protocol proto, const void* data, size_t size) const { - struct evbuffer* out_buf = bufferevent_get_output(c->bev.get()); - +asio::awaitable IPStackSimulator::send_layer3_frame( + shared_ptr c, FrameInfo::Protocol proto, const void* data, size_t size) const { switch (c->protocol) { - case Protocol::ETHERNET_TAPSERVER: { - EthernetHeader ether; - ether.dest_mac = c->mac_addr; - ether.src_mac = this->host_mac_address_bytes; - switch (proto) { - case FrameInfo::Protocol::NONE: - throw logic_error("layer 3 protocol not specified"); - case FrameInfo::Protocol::LCP: - throw logic_error("cannot send LCP frame over Ethernet"); - case FrameInfo::Protocol::IPV4: - ether.protocol = 0x0800; - break; - case FrameInfo::Protocol::ARP: - ether.protocol = 0x0806; - break; - default: - throw logic_error("unknown layer 3 protocol"); - } - - le_uint16_t frame_size = size + sizeof(EthernetHeader); - evbuffer_add(out_buf, &frame_size, 2); - evbuffer_add(out_buf, ðer, sizeof(ether)); - evbuffer_add(out_buf, data, size); - if (this->pcap_text_log_file) { - phosg::StringWriter w; - w.write(ðer, sizeof(ether)); - w.write(data, size); - this->log_frame(w.str()); - } + case VirtualNetworkProtocol::ETHERNET_TAPSERVER: + co_await this->send_ethernet_tapserver_frame(c, proto, data, size); break; - } - - case Protocol::HDLC_TAPSERVER: - case Protocol::HDLC_RAW: { - HDLCHeader hdlc; - hdlc.start_sentinel1 = 0x7E; - hdlc.address = 0xFF; - hdlc.control = 0x03; - switch (proto) { - case FrameInfo::Protocol::NONE: - throw logic_error("layer 3 protocol not specified"); - case FrameInfo::Protocol::LCP: - hdlc.protocol = 0xC021; - break; - case FrameInfo::Protocol::PAP: - hdlc.protocol = 0xC023; - break; - case FrameInfo::Protocol::IPCP: - hdlc.protocol = 0x8021; - break; - case FrameInfo::Protocol::IPV4: - hdlc.protocol = 0x0021; - break; - case FrameInfo::Protocol::ARP: - throw runtime_error("cannot send ARP packets over HDLC"); - default: - throw logic_error("unknown layer 3 protocol"); - } - - phosg::StringWriter w; - w.put(hdlc); - w.write(data, size); - w.put_u16l(FrameInfo::computed_hdlc_checksum(w.str().data() + 1, w.size() - 1)); - w.put_u8(0x7E); - - string escaped = escape_hdlc_frame(w.str(), c->hdlc_escape_control_character_flags); - if (ip_stack_simulator_log.debug("Sending HDLC frame to virtual network (escaped to %zX bytes)", escaped.size())) { - phosg::print_data(stderr, w.str()); - } - - if (c->protocol == Protocol::HDLC_TAPSERVER) { - le_uint16_t frame_size = escaped.size(); - evbuffer_add(out_buf, &frame_size, 2); - } - evbuffer_add(out_buf, escaped.data(), escaped.size()); - if (this->pcap_text_log_file) { - this->log_frame(escaped); - } + case VirtualNetworkProtocol::HDLC_TAPSERVER: + co_await this->send_hdlc_frame(c, proto, data, size, false); + break; + case VirtualNetworkProtocol::HDLC_RAW: + co_await this->send_hdlc_frame(c, proto, data, size, true); break; - } - default: throw logic_error("unknown link type"); } } -void IPStackSimulator::on_client_frame(shared_ptr c, const string& frame) { - FrameInfo::LinkType link_type = (c->protocol == Protocol::ETHERNET_TAPSERVER) +asio::awaitable IPStackSimulator::on_client_frame(shared_ptr c, const void* data, size_t size) { + FrameInfo::LinkType link_type = (c->protocol == VirtualNetworkProtocol::ETHERNET_TAPSERVER) ? FrameInfo::LinkType::ETHERNET : FrameInfo::LinkType::HDLC; - const string* effective_data = &frame; - string hdlc_unescaped_data; - if (link_type == FrameInfo::LinkType::HDLC) { - hdlc_unescaped_data = unescape_hdlc_frame(frame); - effective_data = &hdlc_unescaped_data; + if (this->log.debug_f("Virtual network sent frame")) { + phosg::print_data(stderr, data, size); } - if (ip_stack_simulator_log.debug("Virtual network sent frame")) { - phosg::print_data(stderr, *effective_data); - } - this->log_frame(*effective_data); - FrameInfo fi(link_type, *effective_data); - if (ip_stack_simulator_log.should_log(phosg::LogLevel::DEBUG)) { + FrameInfo fi(link_type, data, size); + if (this->log.should_log(phosg::LogLevel::L_DEBUG)) { string fi_header = fi.header_str(); - ip_stack_simulator_log.debug("Frame header: %s", fi_header.c_str()); + this->log.debug_f("Frame header: {}", fi_header); } if (fi.ether) { @@ -510,8 +476,8 @@ void IPStackSimulator::on_client_frame(shared_ptr c, const string& fra uint16_t expected_checksum = fi.computed_hdlc_checksum(); uint16_t stored_checksum = fi.stored_hdlc_checksum(); if (expected_checksum != stored_checksum) { - throw runtime_error(phosg::string_printf( - "HDLC checksum is incorrect (%04hX expected, %04hX received)", + throw runtime_error(std::format( + "HDLC checksum is incorrect ({:04X} expected, {:04X} received)", expected_checksum, stored_checksum)); } } else { @@ -519,23 +485,23 @@ void IPStackSimulator::on_client_frame(shared_ptr c, const string& fra } if (fi.lcp) { - this->on_client_lcp_frame(c, fi); + co_await this->on_client_lcp_frame(c, fi); } else if (fi.pap) { - this->on_client_pap_frame(c, fi); + co_await this->on_client_pap_frame(c, fi); } else if (fi.ipcp) { - this->on_client_ipcp_frame(c, fi); + co_await this->on_client_ipcp_frame(c, fi); } else if (fi.arp) { - this->on_client_arp_frame(c, fi); + co_await this->on_client_arp_frame(c, fi); } else if (fi.ipv4) { uint16_t expected_ipv4_checksum = fi.computed_ipv4_header_checksum(); if (fi.ipv4->checksum != expected_ipv4_checksum) { - throw runtime_error(phosg::string_printf( - "IPv4 header checksum is incorrect (%04hX expected, %04hX received)", - expected_ipv4_checksum, fi.ipv4->checksum.load())); + throw runtime_error(std::format( + "IPv4 header checksum is incorrect ({:04X} expected, {:04X} received)", + expected_ipv4_checksum, fi.ipv4->checksum)); } if ((fi.ipv4->src_addr != c->ipv4_addr) && (fi.ipv4->src_addr != 0)) { @@ -545,20 +511,20 @@ void IPStackSimulator::on_client_frame(shared_ptr c, const string& fra if (fi.udp) { uint16_t expected_udp_checksum = fi.computed_udp4_checksum(); if (fi.udp->checksum != expected_udp_checksum) { - throw runtime_error(phosg::string_printf( - "UDP checksum is incorrect (%04hX expected, %04hX received)", - expected_udp_checksum, fi.udp->checksum.load())); + throw runtime_error(std::format( + "UDP checksum is incorrect ({:04X} expected, {:04X} received)", + expected_udp_checksum, fi.udp->checksum)); } - this->on_client_udp_frame(c, fi); + co_await this->on_client_udp_frame(c, fi); } else if (fi.tcp) { uint16_t expected_tcp_checksum = fi.computed_tcp4_checksum(); if (fi.tcp->checksum != expected_tcp_checksum) { - throw runtime_error(phosg::string_printf( - "TCP checksum is incorrect (%04hX expected, %04hX received)", - expected_tcp_checksum, fi.tcp->checksum.load())); + throw runtime_error(std::format( + "TCP checksum is incorrect ({:04X} expected, {:04X} received)", + expected_tcp_checksum, fi.tcp->checksum)); } - this->on_client_tcp_frame(c, fi); + co_await this->on_client_tcp_frame(c, fi); } else { throw runtime_error("frame uses unsupported IPv4 protocol"); @@ -569,7 +535,7 @@ void IPStackSimulator::on_client_frame(shared_ptr c, const string& fra } } -void IPStackSimulator::on_client_lcp_frame(shared_ptr c, const FrameInfo& fi) { +asio::awaitable IPStackSimulator::on_client_lcp_frame(shared_ptr c, const FrameInfo& fi) { switch (fi.lcp->command) { case 0x01: { // Configure-Request auto opts_r = fi.read_payload(); @@ -593,7 +559,7 @@ void IPStackSimulator::on_client_lcp_frame(shared_ptr c, const FrameIn case 0x04: // Quality protocol case 0x07: // Protocol field compression case 0x08: // Address and control field compression - throw runtime_error(phosg::string_printf("unimplemented LCP option %02hhX (%zu bytes)", opt, opt_data.size())); + throw runtime_error(std::format("unimplemented LCP option {:02X} ({} bytes)", opt, opt_data.size())); default: throw runtime_error("unknown LCP option"); } @@ -622,7 +588,7 @@ void IPStackSimulator::on_client_lcp_frame(shared_ptr c, const FrameIn .size = static_cast(sizeof(LCPHeader) + opts_w.size()), }); request_w.write(opts_w.str()); - this->send_layer3_frame(c, FrameInfo::Protocol::LCP, request_w.str()); + co_await this->send_layer3_frame(c, FrameInfo::Protocol::LCP, request_w.str()); phosg::StringWriter ack_w; ack_w.put(LCPHeader{ @@ -631,7 +597,7 @@ void IPStackSimulator::on_client_lcp_frame(shared_ptr c, const FrameIn .size = fi.lcp->size, }); ack_w.write(fi.payload, fi.payload_size); - this->send_layer3_frame(c, FrameInfo::Protocol::LCP, ack_w.str()); + co_await this->send_layer3_frame(c, FrameInfo::Protocol::LCP, ack_w.str()); break; } @@ -641,14 +607,14 @@ void IPStackSimulator::on_client_lcp_frame(shared_ptr c, const FrameIn c->tcp_connections.clear(); string response(reinterpret_cast(fi.payload), fi.payload_size); response.at(0) = 0x06; // Terminate-Ack - this->send_layer3_frame(c, FrameInfo::Protocol::LCP, response); + co_await this->send_layer3_frame(c, FrameInfo::Protocol::LCP, response); break; } case 0x09: { // Echo-Request string response(reinterpret_cast(fi.payload), fi.payload_size); response.at(0) = 0x0A; // Echo-Reply - this->send_layer3_frame(c, FrameInfo::Protocol::LCP, response); + co_await this->send_layer3_frame(c, FrameInfo::Protocol::LCP, response); break; } @@ -668,7 +634,7 @@ void IPStackSimulator::on_client_lcp_frame(shared_ptr c, const FrameIn } } -void IPStackSimulator::on_client_pap_frame(shared_ptr c, const FrameInfo& fi) { +asio::awaitable IPStackSimulator::on_client_pap_frame(shared_ptr c, const FrameInfo& fi) { if (fi.pap->command != 0x01) { // Authenticate-Request throw runtime_error("client sent incorrect PAP command"); } @@ -676,7 +642,7 @@ void IPStackSimulator::on_client_pap_frame(shared_ptr c, const FrameIn auto r = fi.read_payload(); string username = r.read(r.get_u8()); string password = r.read(r.get_u8()); - ip_stack_simulator_log.info("Client logged in with username \"%s\" and password", username.c_str()); + this->log.info_f("Client logged in with username \"{}\" and password", username); static const string login_message = "newserv PPP simulator"; phosg::StringWriter w; @@ -687,10 +653,10 @@ void IPStackSimulator::on_client_pap_frame(shared_ptr c, const FrameIn }); w.put_u8(login_message.size()); w.write(login_message); - this->send_layer3_frame(c, FrameInfo::Protocol::PAP, w.str()); + co_await this->send_layer3_frame(c, FrameInfo::Protocol::PAP, w.str()); } -void IPStackSimulator::on_client_ipcp_frame(shared_ptr c, const FrameInfo& fi) { +asio::awaitable IPStackSimulator::on_client_ipcp_frame(shared_ptr c, const FrameInfo& fi) { switch (fi.ipcp->command) { case 0x01: { // Configure-Request auto opts_r = fi.read_payload(); @@ -723,7 +689,7 @@ void IPStackSimulator::on_client_ipcp_frame(shared_ptr c, const FrameI case 0x82: // Primary NBNS server address case 0x84: // Secondary NBNS server address case 0x04: // Mobile IP address - throw runtime_error(phosg::string_printf("unimplemented IPCP option %02hhX (%zu bytes)", opt, opt_data.size())); + throw runtime_error(std::format("unimplemented IPCP option {:02X} ({} bytes)", opt, opt_data.size())); default: throw runtime_error("unknown IPCP option"); } @@ -738,7 +704,7 @@ void IPStackSimulator::on_client_ipcp_frame(shared_ptr c, const FrameI .size = sizeof(IPCPHeader) + rejected_opts_w.size(), }); reject_w.write(rejected_opts_w.str()); - this->send_layer3_frame(c, FrameInfo::Protocol::IPCP, reject_w.str()); + co_await this->send_layer3_frame(c, FrameInfo::Protocol::IPCP, reject_w.str()); } else if ((remote_ip != 0x1E1E1E1E) || (remote_primary_dns != 0x23232323) || @@ -763,7 +729,7 @@ void IPStackSimulator::on_client_ipcp_frame(shared_ptr c, const FrameI .size = static_cast(opts_w.size() + sizeof(IPCPHeader)), }); nak_w.write(opts_w.str()); - this->send_layer3_frame(c, FrameInfo::Protocol::IPCP, nak_w.str()); + co_await this->send_layer3_frame(c, FrameInfo::Protocol::IPCP, nak_w.str()); } else { // Options OK c->ipv4_addr = remote_ip; @@ -788,7 +754,7 @@ void IPStackSimulator::on_client_ipcp_frame(shared_ptr c, const FrameI .size = static_cast(opts_w.size() + sizeof(IPCPHeader)), }); request_w.write(opts_w.str()); - this->send_layer3_frame(c, FrameInfo::Protocol::IPCP, request_w.str()); + co_await this->send_layer3_frame(c, FrameInfo::Protocol::IPCP, request_w.str()); phosg::StringWriter ack_w; ack_w.put(IPCPHeader{ @@ -797,7 +763,7 @@ void IPStackSimulator::on_client_ipcp_frame(shared_ptr c, const FrameI .size = fi.ipcp->size, }); ack_w.write(fi.payload, fi.payload_size); - this->send_layer3_frame(c, FrameInfo::Protocol::IPCP, ack_w.str()); + co_await this->send_layer3_frame(c, FrameInfo::Protocol::IPCP, ack_w.str()); } break; } @@ -807,7 +773,7 @@ void IPStackSimulator::on_client_ipcp_frame(shared_ptr c, const FrameI c->tcp_connections.clear(); string response(reinterpret_cast(fi.payload), fi.payload_size); response.at(0) = 0x06; // Terminate-Ack - this->send_layer3_frame(c, FrameInfo::Protocol::LCP, response); + co_await this->send_layer3_frame(c, FrameInfo::Protocol::LCP, response); break; } @@ -824,8 +790,7 @@ void IPStackSimulator::on_client_ipcp_frame(shared_ptr c, const FrameI } } -void IPStackSimulator::on_client_arp_frame( - shared_ptr c, const FrameInfo& fi) { +asio::awaitable IPStackSimulator::on_client_arp_frame(shared_ptr c, const FrameInfo& fi) { if (fi.arp->hwaddr_len != 6 || fi.arp->paddr_len != 4 || fi.arp->hardware_type != 0x0001 || @@ -837,8 +802,7 @@ void IPStackSimulator::on_client_arp_frame( } if (c->ipv4_addr == 0) { - c->ipv4_addr = *reinterpret_cast( - reinterpret_cast(fi.payload) + 6); + c->ipv4_addr = *reinterpret_cast(reinterpret_cast(fi.payload) + 6); } phosg::StringWriter w; @@ -865,10 +829,10 @@ void IPStackSimulator::on_client_arp_frame( w.write(payload_bytes + 16, 4); w.write(payload_bytes, 10); - this->send_layer3_frame(c, FrameInfo::Protocol::ARP, w.str()); + co_await this->send_layer3_frame(c, FrameInfo::Protocol::ARP, w.str()); } -void IPStackSimulator::on_client_udp_frame(shared_ptr c, const FrameInfo& fi) { +asio::awaitable IPStackSimulator::on_client_udp_frame(shared_ptr c, const FrameInfo& fi) { // We only implement DHCP and newserv's DNS server here. // Every received UDP packet will elicit exactly one UDP response from @@ -1027,9 +991,9 @@ void IPStackSimulator::on_client_udp_frame(shared_ptr c, const FrameIn r_udp.checksum = FrameInfo::computed_udp4_checksum( r_ipv4, r_udp, r_data.data(), r_data.size()); - if (ip_stack_simulator_log.should_log(phosg::LogLevel::DEBUG)) { + if (this->log.should_log(phosg::LogLevel::L_DEBUG)) { string remote_str = this->str_for_ipv4_netloc(fi.ipv4->src_addr, fi.udp->src_port); - ip_stack_simulator_log.debug("Sending UDP response to %s", remote_str.c_str()); + this->log.debug_f("Sending UDP response to {}", remote_str); phosg::print_data(stderr, r_data); } @@ -1038,35 +1002,13 @@ void IPStackSimulator::on_client_udp_frame(shared_ptr c, const FrameIn w.put(r_udp); w.write(r_data); - this->send_layer3_frame(c, FrameInfo::Protocol::IPV4, w.str()); + co_await this->send_layer3_frame(c, FrameInfo::Protocol::IPV4, w.str()); } } -uint64_t IPStackSimulator::tcp_conn_key_for_connection( - const IPClient::TCPConnection& conn) { - return (static_cast(conn.server_addr) << 32) | - (static_cast(conn.server_port) << 16) | - static_cast(conn.client_port); -} - -uint64_t IPStackSimulator::tcp_conn_key_for_client_frame( - const IPv4Header& ipv4, const TCPHeader& tcp) { - return (static_cast(ipv4.dest_addr) << 32) | - (static_cast(tcp.dest_port) << 16) | - static_cast(tcp.src_port); -} - -uint64_t IPStackSimulator::tcp_conn_key_for_client_frame(const FrameInfo& fi) { - if (!fi.ipv4 || !fi.tcp) { - throw logic_error("tcp_conn_key_for_frame called on non-TCP frame"); - } - return IPStackSimulator::tcp_conn_key_for_client_frame(*fi.ipv4, *fi.tcp); -} - -void IPStackSimulator::on_client_tcp_frame( - shared_ptr c, const FrameInfo& fi) { - ip_stack_simulator_log.debug("Virtual network sent TCP frame (seq=%08" PRIX32 ", ack=%08" PRIX32 ")", - fi.tcp->seq_num.load(), fi.tcp->ack_num.load()); +asio::awaitable IPStackSimulator::on_client_tcp_frame(shared_ptr c, const FrameInfo& fi) { + this->log.debug_f("Virtual network sent TCP frame (seq={:08X}, ack={:08X})", + fi.tcp->seq_num, fi.tcp->ack_num); if (fi.tcp->flags & (TCPHeader::Flag::NS | TCPHeader::Flag::CWR | TCPHeader::Flag::ECE | TCPHeader::Flag::URG)) { throw runtime_error("unsupported flag in TCP packet"); @@ -1121,63 +1063,58 @@ void IPStackSimulator::on_client_tcp_frame( } } - uint64_t key = this->tcp_conn_key_for_client_frame(fi); - auto emplace_ret = c->tcp_connections.emplace(key, IPClient::TCPConnection()); - auto& conn = emplace_ret.first->second; + shared_ptr conn; string conn_str; - - if (emplace_ret.second) { - // Connection is new; initialize it - conn.client = c; - conn.resend_push_event.reset(event_new(this->base.get(), -1, EV_TIMEOUT, - &IPStackSimulator::dispatch_on_resend_push, &conn)); - conn.server_addr = fi.ipv4->dest_addr; - conn.server_port = fi.tcp->dest_port; - conn.client_port = fi.tcp->src_port; - conn.next_client_seq = fi.tcp->seq_num + 1; - conn.acked_server_seq = phosg::random_object(); - conn.resend_push_usecs = DEFAULT_RESEND_PUSH_USECS; - conn.next_push_max_frame_size = max_frame_size; - conn.awaiting_first_ack = true; - conn.max_frame_size = max_frame_size; - conn.bytes_received = 0; - conn.bytes_sent = 0; + uint64_t key = this->tcp_conn_key_for_client_frame(fi); + auto conn_it = c->tcp_connections.find(key); + if (conn_it == c->tcp_connections.end()) { + conn = make_shared(c); + c->tcp_connections.emplace(key, conn); + conn->server_addr = fi.ipv4->dest_addr; + conn->server_port = fi.tcp->dest_port; + conn->client_port = fi.tcp->src_port; + conn->next_client_seq = fi.tcp->seq_num + 1; + conn->acked_server_seq = phosg::random_object(); + conn->resend_push_usecs = DEFAULT_RESEND_PUSH_USECS; + conn->next_push_max_frame_size = max_frame_size; + conn->max_frame_size = max_frame_size; conn_str = this->str_for_tcp_connection(c, conn); - ip_stack_simulator_log.info("Client opened TCP connection %s (acked_server_seq=%08" PRIX32 ", next_client_seq=%08" PRIX32 ")", - conn_str.c_str(), conn.acked_server_seq, conn.next_client_seq); + this->log.info_f( + "Client opened TCP connection {} (acked_server_seq={:08X}, next_client_seq={:08X})", + conn_str, conn->acked_server_seq, conn->next_client_seq); } else { + conn = conn_it->second; + // Connection is NOT new; this is probably a resend of an earlier SYN - if (!conn.awaiting_first_ack) { + if (!conn->awaiting_first_ack) { throw logic_error("SYN received on already-open connection after initial phase"); } // TODO: We should check the syn/ack numbers here instead of just assuming // they're correct conn_str = this->str_for_tcp_connection(c, conn); - ip_stack_simulator_log.debug("Client resent SYN for TCP connection %s", - conn_str.c_str()); + this->log.debug_f("Client resent SYN for TCP connection {}", conn_str); } // Send a SYN+ACK (send_tcp_frame always adds the ACK flag) - this->send_tcp_frame(c, conn, TCPHeader::Flag::SYN); - ip_stack_simulator_log.debug("Sent SYN+ACK on %s (acked_server_seq=%08" PRIX32 ", next_client_seq=%08" PRIX32 ")", - conn_str.c_str(), conn.acked_server_seq, conn.next_client_seq); + co_await this->send_tcp_frame(c, conn, TCPHeader::Flag::SYN); + this->log.debug_f("Sent SYN+ACK on {} (acked_server_seq={:08X}, next_client_seq={:08X})", + conn_str, conn->acked_server_seq, conn->next_client_seq); } else { // This frame isn't a SYN, so a connection object should already exist uint64_t key = this->tcp_conn_key_for_client_frame(fi); - IPClient::TCPConnection* conn; - try { - conn = &c->tcp_connections.at(key); - } catch (const out_of_range&) { + auto conn_it = c->tcp_connections.find(key); + if (conn_it == c->tcp_connections.end()) { throw runtime_error("non-SYN frame does not correspond to any open TCP connection"); } + auto& conn = conn_it->second; bool conn_valid = true; bool acked_seq_changed = false; if (fi.tcp->flags & TCPHeader::Flag::ACK) { - ip_stack_simulator_log.debug("Client sent ACK %08" PRIX32, fi.tcp->ack_num.load()); + this->log.debug_f("Client sent ACK {:08X}", fi.tcp->ack_num); if (conn->awaiting_first_ack) { if (fi.tcp->ack_num != conn->acked_server_seq + 1) { throw runtime_error("first ack_num was not acked_server_seq + 1"); @@ -1186,21 +1123,22 @@ void IPStackSimulator::on_client_tcp_frame( conn->awaiting_first_ack = false; } else { + conn->awaiting_ack = false; if (seq_num_greater(fi.tcp->ack_num, conn->acked_server_seq)) { - ip_stack_simulator_log.debug("Advancing acked_server_seq from %08" PRIX32, conn->acked_server_seq); + this->log.debug_f("Advancing acked_server_seq from {:08X}", conn->acked_server_seq); uint32_t ack_delta = fi.tcp->ack_num - conn->acked_server_seq; - size_t pending_bytes = evbuffer_get_length(conn->pending_data.get()); - if (pending_bytes < ack_delta) { + if (conn->outbound_data_bytes < ack_delta) { throw runtime_error("client acknowledged beyond end of sent data"); } - evbuffer_drain(conn->pending_data.get(), ack_delta); + conn->drain_outbound_data(ack_delta); conn->acked_server_seq += ack_delta; conn->resend_push_usecs = DEFAULT_RESEND_PUSH_USECS; conn->next_push_max_frame_size = conn->max_frame_size; acked_seq_changed = true; - ip_stack_simulator_log.debug("Removed %08" PRIX32 " bytes from pending buffer and advanced acked_server_seq to %08" PRIX32, + this->log.debug_f( + "Removed {:08X} bytes from pending buffer and advanced acked_server_seq to {:08X}", ack_delta, conn->acked_server_seq); } else if (seq_num_less(fi.tcp->ack_num, conn->acked_server_seq)) { @@ -1208,8 +1146,8 @@ void IPStackSimulator::on_client_tcp_frame( } } - if (!conn->server_bev.get()) { - this->open_server_connection(c, *conn); + if (!conn->server_channel) { + co_await this->open_server_connection(c, conn); } } @@ -1219,29 +1157,34 @@ void IPStackSimulator::on_client_tcp_frame( throw runtime_error("client sent TCP FIN+RST"); } - string conn_str = this->str_for_tcp_connection(c, *conn); - ip_stack_simulator_log.info("Client closed TCP connection %s", conn_str.c_str()); + string conn_str = this->str_for_tcp_connection(c, conn); + this->log.info_f("Client closed TCP connection {}", conn_str); + if (conn->server_channel) { + conn->server_channel->disconnect(); + conn->server_channel.reset(); + } // TODO: Are we supposed to send a response to an RST? Here we do, and the // client probably just ignores it anyway - this->send_tcp_frame(c, *conn, fi.tcp->flags & (TCPHeader::Flag::RST | TCPHeader::Flag::FIN)); + co_await this->send_tcp_frame(c, conn, fi.tcp->flags & (TCPHeader::Flag::RST | TCPHeader::Flag::FIN)); // Delete the connection object. The unique_ptr destructor flushes the // bufferevent, and thereby sends an EOF to the server's end. c->tcp_connections.erase(key); conn_valid = false; - // Note: The PSH flag isn't required to be set on all packets that contain - // data. The PSH flag just means "tell the application that data is - // available", so some senders only set the PSH flag on the last frame of a - // large segment of data, since the application wouldn't be able to process - // the segment until all of it is available. newserv can handle incomplete - // commands, so we just ignore the PSH flag and forward any data to the - // server immediately. } else if (fi.payload_size != 0) { + // Note: The PSH flag isn't required to be set on all packets that + // contain data. The PSH flag just means "tell the application that data + // is available", so some senders only set the PSH flag on the last frame + // of a large segment of data, since the application wouldn't be able to + // process the segment until all of it is available. newserv can handle + // incomplete commands, so we just ignore the PSH flag and forward any + // data to the server immediately (hence the lack of a flag check in the + // above condition). - string conn_str = ip_stack_simulator_log.should_log(phosg::LogLevel::WARNING) - ? this->str_for_tcp_connection(c, *conn) + string conn_str = this->log.should_log(phosg::LogLevel::L_WARNING) + ? this->str_for_tcp_connection(c, conn) : ""; size_t payload_skip_bytes; @@ -1262,9 +1205,9 @@ void IPStackSimulator::on_client_tcp_frame( // Payload is in the future - we must have missed a data frame. We'll // ignore it (but warn) and send an ACK later, and the client should // retransmit the lost data - ip_stack_simulator_log.warning( - "Client sent out-of-order sequence number (expected %08" PRIX32 ", received %08" PRIX32 ", 0x%zX data bytes)", - conn->next_client_seq, fi.tcp->seq_num.load(), fi.payload_size); + this->log.warning_f( + "Client sent out-of-order sequence number (expected {:08X}, received {:08X}, 0x{:X} data bytes)", + conn->next_client_seq, fi.tcp->seq_num, fi.payload_size); payload_skip_bytes = fi.payload_size; } @@ -1278,105 +1221,82 @@ void IPStackSimulator::on_client_tcp_frame( bool was_logged; if (payload_skip_bytes) { - was_logged = ip_stack_simulator_log.debug("Client sent data on TCP connection %s, overlapping existing ack'ed data (0x%zX bytes ignored)", - conn_str.c_str(), payload_skip_bytes); + was_logged = this->log.debug_f( + "Client sent data on TCP connection {}, overlapping existing ack'ed data (0x{:X} bytes ignored)", + conn_str, payload_skip_bytes); } else { - was_logged = ip_stack_simulator_log.debug("Client sent data on TCP connection %s", - conn_str.c_str()); + was_logged = this->log.debug_f("Client sent data on TCP connection {}", conn_str); } if (was_logged) { phosg::print_data(stderr, payload, payload_size); } // Send the new data to the server - struct evbuffer* server_out_buf = bufferevent_get_output( - conn->server_bev.get()); - evbuffer_add(server_out_buf, payload, payload_size); + if (!conn->server_channel) { + this->log.warning_f("Client sent data on TCP connection {}, but server channel is missing", + conn_str); + } else if (!conn->server_channel->connected()) { + this->log.warning_f("Client sent data on TCP connection {}, but server channel is disconnected", + conn_str); + } else { + conn->server_channel->add_inbound_data(payload, payload_size); + } // Update the sequence number and stats conn->next_client_seq += payload_size; conn->bytes_received += payload_size; if (conn->next_client_seq < payload_size) { - ip_stack_simulator_log.warning("Client sequence number has wrapped (next=%08" PRIX32 ", bytes=%zX)", - fi.tcp->seq_num.load(), payload_size); + this->log.warning_f("Client sequence number has wrapped (next={:08X}, bytes={:X})", + fi.tcp->seq_num, payload_size); } } // Send an ACK - this->send_tcp_frame(c, *conn); - ip_stack_simulator_log.debug("Sent PSH ACK on %s (acked_server_seq=%08" PRIX32 ", next_client_seq=%08" PRIX32 ", bytes_received=0x%zX)", - conn_str.c_str(), conn->acked_server_seq, conn->next_client_seq, conn->bytes_received); + co_await this->send_tcp_frame(c, conn); + this->log.debug_f("Sent PSH ACK on {} (acked_server_seq={:08X}, next_client_seq={:08X}, bytes_received=0x{:X})", + conn_str, conn->acked_server_seq, conn->next_client_seq, conn->bytes_received); } if (conn_valid && acked_seq_changed) { // Try to send some more data if the client is waiting on it - this->send_pending_push_frame(c, *conn, true); + this->schedule_send_pending_push_frame(conn, 0); } } } -void IPStackSimulator::open_server_connection(shared_ptr c, IPClient::TCPConnection& conn) { - if (conn.server_bev.get()) { - throw logic_error("server connection is already open"); - } - - struct bufferevent* bevs[2]; - bufferevent_pair_new(this->base.get(), 0, bevs); - - // Set up the IPStackSimulator end of the virtual connection - bufferevent_setcb(bevs[0], &IPStackSimulator::dispatch_on_server_input, - nullptr, &IPStackSimulator::dispatch_on_server_error, &conn); - bufferevent_enable(bevs[0], EV_READ | EV_WRITE); - conn.server_bev.reset(bevs[0]); - - // Link the client to the server - the server sees this as a normal TCP - // connection and treats it as if the client connected to one of its listening - // sockets - shared_ptr port_config; - try { - port_config = this->state->number_to_port_config.at(conn.server_port); - } catch (const out_of_range&) { - bufferevent_free(bevs[1]); - throw logic_error("client connected to port missing from configuration"); - } - - string conn_str = this->str_for_tcp_connection(c, conn); - if (port_config->behavior == ServerBehavior::PROXY_SERVER) { - if (!this->state->proxy_server.get()) { - ip_stack_simulator_log.error("TCP connection %s is to non-running proxy server", conn_str.c_str()); - flush_and_free_bufferevent(bevs[1]); - } else { - 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", conn_str.c_str()); +void IPStackSimulator::schedule_send_pending_push_frame(shared_ptr conn, uint64_t delay_usecs) { + conn->resend_push_timer.expires_after(std::chrono::microseconds(delay_usecs)); + conn->resend_push_timer.async_wait([wconn = weak_ptr(conn)](std::error_code ec) { + if (ec) { + return; } - } else if (this->state->game_server.get()) { - this->state->game_server->connect_virtual_client( - bevs[1], c->network_id, c->ipv4_addr, conn.client_port, - conn.server_port, port_config->version, port_config->behavior); - ip_stack_simulator_log.info("Connected TCP connection %s to game server", conn_str.c_str()); - } else { - ip_stack_simulator_log.error("No server available for TCP connection %s", conn_str.c_str()); - flush_and_free_bufferevent(bevs[1]); - } + auto conn = wconn.lock(); + if (!conn) { + return; + } + auto c = conn->client.lock(); + if (!c) { + return; + } + auto sim = c->sim.lock(); + if (!sim) { + return; + } + asio::co_spawn(*sim->get_io_context(), sim->send_pending_push_frame(c, conn), asio::detached); + }); } -void IPStackSimulator::send_pending_push_frame( - shared_ptr c, IPClient::TCPConnection& conn, bool always_send) { - size_t pending_bytes = evbuffer_get_length(conn.pending_data.get()); - if (!pending_bytes) { - event_del(conn.resend_push_event.get()); - return; +asio::awaitable IPStackSimulator::send_pending_push_frame( + shared_ptr c, shared_ptr conn) { + if (!conn->outbound_data_bytes) { + if (!conn->server_channel || !conn->server_channel->connected()) { + co_await this->close_tcp_connection(c, conn); + } + co_return; } - // If we're waiting to receive an ACK from the client, don't send another PSH - // until we get the ACK (unless this is a resend of a previous PSH due to a - // timeout) - if (!always_send && event_pending(conn.resend_push_event.get(), EV_TIMEOUT, nullptr)) { - return; - } - - size_t bytes_to_send = min(pending_bytes, conn.next_push_max_frame_size); - if (c->protocol == Protocol::HDLC_TAPSERVER) { + size_t bytes_to_send = min(conn->outbound_data_bytes, conn->next_push_max_frame_size); + if (c->protocol == VirtualNetworkProtocol::HDLC_TAPSERVER) { // There is a bug in Dolphin's modem implementation (which I wrote, so it's // my fault) that causes commands to be dropped when too much data is sent // at once. To work around this, we only send up to 200 bytes in each push @@ -1384,32 +1304,42 @@ void IPStackSimulator::send_pending_push_frame( bytes_to_send = min(bytes_to_send, 200); } - ip_stack_simulator_log.debug("Sending PSH frame with seq_num %08" PRIX32 ", 0x%zX/0x%zX data bytes", - conn.acked_server_seq, bytes_to_send, pending_bytes); + this->log.debug_f("Sending PSH frame with seq_num {:08X}, 0x{:X}/0x{:X} data bytes", + conn->acked_server_seq, bytes_to_send, conn->outbound_data_bytes); - this->send_tcp_frame(c, conn, TCPHeader::Flag::PSH, conn.pending_data.get(), bytes_to_send); - struct timeval resend_push_timeout = phosg::usecs_to_timeval(conn.resend_push_usecs); - event_add(conn.resend_push_event.get(), &resend_push_timeout); + conn->linearize_outbound_data(bytes_to_send); + if (conn->outbound_data.empty() || conn->outbound_data.front().size() < bytes_to_send) { + // This should never happen because bytes_to_send should always be less + // than or equal to conn->outbound_data_bytes, which itself should be equal + // to the number of bytes that can be linearized + throw logic_error("failed to linearize enough bytes before sending TCP PSH"); + } + co_await this->send_tcp_frame(c, conn, TCPHeader::Flag::PSH, conn->outbound_data.front().data(), bytes_to_send); + conn->awaiting_ack = true; + + // Schedule the timer for sending another PSH, in case the client doesn't + // respond quickly enough + this->schedule_send_pending_push_frame(conn, conn->resend_push_usecs); // If the client isn't responding to our PSHes, back off exponentially up to // a limit of 5 seconds between PSH frames. This window is reset when // acked_server_seq changes (that is, when the client has acknowledged any new // data). It seems some situations cause GameCube clients to drop packets more // often; to alleviate this, we also try to resend less data. - conn.resend_push_usecs *= 2; - if (conn.resend_push_usecs > 5000000) { - conn.resend_push_usecs = 5000000; + conn->resend_push_usecs *= 2; + if (conn->resend_push_usecs > 5000000) { + conn->resend_push_usecs = 5000000; } - conn.next_push_max_frame_size = max(0x100, conn.next_push_max_frame_size - 0x100); + conn->next_push_max_frame_size = max(0x100, conn->next_push_max_frame_size - 0x100); } -void IPStackSimulator::send_tcp_frame( - shared_ptr c, - IPClient::TCPConnection& conn, +asio::awaitable IPStackSimulator::send_tcp_frame( + shared_ptr c, + shared_ptr conn, uint16_t flags, - struct evbuffer* src_buf, - size_t src_bytes) { - if (!src_bytes != !(flags & TCPHeader::Flag::PSH)) { + const void* payload_data, + size_t payload_size) { + if (!payload_data != !(flags & TCPHeader::Flag::PSH)) { throw logic_error("data should be given if and only if PSH is given"); } @@ -1422,120 +1352,190 @@ void IPStackSimulator::send_tcp_frame( ipv4.ttl = 20; ipv4.protocol = 6; // TCP // ipv4.checksum filled in later - ipv4.src_addr = conn.server_addr; + ipv4.src_addr = conn->server_addr; ipv4.dest_addr = c->ipv4_addr; TCPHeader tcp; - tcp.src_port = conn.server_port; - tcp.dest_port = conn.client_port; - tcp.seq_num = conn.acked_server_seq; - tcp.ack_num = conn.next_client_seq; + tcp.src_port = conn->server_port; + tcp.dest_port = conn->client_port; + tcp.seq_num = conn->acked_server_seq; + tcp.ack_num = conn->next_client_seq; tcp.flags = (5 << 12) | TCPHeader::Flag::ACK | flags; tcp.window = 0x1000; tcp.urgent_ptr = 0; // tcp.checksum filled in later - ipv4.size = sizeof(IPv4Header) + sizeof(TCPHeader) + src_bytes; + ipv4.size = sizeof(IPv4Header) + sizeof(TCPHeader) + payload_size; ipv4.checksum = FrameInfo::computed_ipv4_header_checksum(ipv4); - - const void* linear_data = src_bytes ? evbuffer_pullup(src_buf, src_bytes) : nullptr; - tcp.checksum = FrameInfo::computed_tcp4_checksum(ipv4, tcp, linear_data, src_bytes); + tcp.checksum = FrameInfo::computed_tcp4_checksum(ipv4, tcp, payload_data, payload_size); phosg::StringWriter w; w.put(ipv4); w.put(tcp); - if (src_bytes) { - w.write(linear_data, src_bytes); + if (payload_data) { + w.write(payload_data, payload_size); } - this->send_layer3_frame(c, FrameInfo::Protocol::IPV4, w.str()); + co_await this->send_layer3_frame(c, FrameInfo::Protocol::IPV4, w.str()); } -void IPStackSimulator::dispatch_on_resend_push(evutil_socket_t, short, void* ctx) { - auto* conn = reinterpret_cast(ctx); - auto c = conn->client.lock(); - if (!c.get()) { - ip_stack_simulator_log.warning("Resend push event triggered for deleted client; ignoring"); +asio::awaitable IPStackSimulator::open_server_connection( + shared_ptr c, shared_ptr conn) { + if (conn->server_channel) { + throw logic_error("server connection is already open"); + } + + string conn_str = this->str_for_tcp_connection(c, conn); + + // Figure out which logical port the connection should go to + auto port_config_it = this->state->number_to_port_config.find(conn->server_port); + if (port_config_it == this->state->number_to_port_config.end()) { + this->log.error_f("TCP connection {} is to undefined port {}", conn_str, conn->server_port); + co_await this->close_tcp_connection(c, conn); + co_return; + } + const auto& port_config = port_config_it->second; + + conn->server_channel = make_shared(this->shared_from_this(), c, conn, port_config->version, 1); + + if (!this->state->game_server.get()) { + this->log.error_f("No server available for TCP connection {}", conn_str); + co_await this->close_tcp_connection(c, conn); + co_return; } else { - auto sim = c->sim.lock(); - if (!sim) { - ip_stack_simulator_log.warning("Resend push event triggered for client on deleted simulator; ignoring"); + this->state->game_server->connect_channel(conn->server_channel, conn->server_port, port_config->behavior); + this->log.info_f("Connected TCP connection {} to game server", conn_str); + } +} + +asio::awaitable IPStackSimulator::close_tcp_connection( + shared_ptr c, shared_ptr conn) { + // Send an RST to the client. This is kind of rude (we really should use FIN) + // but the PSO network stack always sends an RST to us when disconnecting, so + // whatever + co_await this->send_tcp_frame(c, conn, TCPHeader::Flag::RST); + + // Delete the connection object + string conn_str = this->str_for_tcp_connection(c, conn); + this->log.info_f("Server closed TCP connection {}", conn_str); + c->tcp_connections.erase(this->tcp_conn_key_for_connection(conn)); +} + +std::shared_ptr IPStackSimulator::create_client( + std::shared_ptr listen_sock, asio::ip::tcp::socket&& client_sock) { + uint32_t addr = ipv4_addr_for_asio_addr(client_sock.remote_endpoint().address()); + if (this->state->banned_ipv4_ranges->check(addr)) { + client_sock.close(); + return nullptr; + } + + uint64_t network_id = this->next_network_id++; + this->log.info_f("Virtual network N-{:X} connected via {}", network_id, listen_sock->name); + return make_shared(this->shared_from_this(), network_id, listen_sock->protocol, std::move(client_sock)); +} + +asio::awaitable IPStackSimulator::handle_tapserver_client(std::shared_ptr c) { + for (;;) { + le_uint16_t frame_size; + co_await asio::async_read(c->sock, asio::buffer(&frame_size, sizeof(frame_size)), asio::use_awaitable); + string frame(frame_size, '\0'); + co_await asio::async_read(c->sock, asio::buffer(frame.data(), frame.size()), asio::use_awaitable); + + if (c->protocol == VirtualNetworkProtocol::HDLC_TAPSERVER) { + frame.resize(unescape_hdlc_frame_inplace(frame.data(), frame.size())); + } + + try { + co_await this->on_client_frame(c, frame.data(), frame.size()); + } catch (const exception& e) { + if (this->log.warning_f("Failed to process frame: {}", e.what())) { + phosg::print_data(stderr, frame); + } + } + + c->reschedule_idle_timeout(); + } +} + +asio::awaitable IPStackSimulator::handle_hdlc_raw_client(std::shared_ptr c) { + std::string buffer(0x1000, 0); + size_t buffer_bytes = 0; + for (;;) { + size_t req_buffer_size = buffer_bytes + 0x400; + if (buffer.size() < req_buffer_size) { + buffer.resize(req_buffer_size); + } + + auto buf = asio::buffer(buffer.data() + buffer_bytes, buffer.size() - buffer_bytes); + buffer_bytes += co_await c->sock.async_read_some(buf, asio::use_awaitable); + + // Process as many packets as possible + size_t frame_start_offset = 0; + while (buffer.size() > frame_start_offset) { + if (buffer[frame_start_offset] != 0x7E) { + throw runtime_error("HDLC frame does not begin with 7E"); + } + size_t frame_end_offset = buffer.find(0x7E, frame_start_offset + 1); + if (frame_end_offset == string::npos) { + break; + } + frame_end_offset++; + + // Unescaping a frame can't make it longer, so we just do it in-place + void* frame_data = buffer.data() + frame_start_offset; + size_t unescaped_size = unescape_hdlc_frame_inplace(frame_data, frame_end_offset - frame_start_offset); + + try { + co_await this->on_client_frame(c, frame_data, unescaped_size); + } catch (const exception& e) { + if (this->log.warning_f("Failed to process frame: {}", e.what())) { + phosg::print_data(stderr, frame_data, unescaped_size); + } + } + + frame_start_offset = frame_end_offset; + } + + // Delete the processed packets from the beginning of the buffer + if (frame_start_offset > buffer_bytes) { + throw logic_error("frame start offset is beyond buffer bounds"); + } else if (frame_start_offset == buffer_bytes) { + buffer_bytes = 0; + } else if (frame_start_offset > 0) { + memcpy(buffer.data(), buffer.data() + frame_start_offset, buffer_bytes - frame_start_offset); + buffer_bytes -= frame_start_offset; + } + + // Reset the idle timer, since the client has sent something valid + c->reschedule_idle_timeout(); + } +} + +asio::awaitable IPStackSimulator::handle_client(std::shared_ptr c) { + switch (c->protocol) { + case VirtualNetworkProtocol::ETHERNET_TAPSERVER: + case VirtualNetworkProtocol::HDLC_TAPSERVER: + co_await this->handle_tapserver_client(c); + break; + case VirtualNetworkProtocol::HDLC_RAW: + co_await this->handle_hdlc_raw_client(c); + break; + default: + throw std::logic_error("unknown virtual network protocol"); + } +} + +asio::awaitable IPStackSimulator::destroy_client(std::shared_ptr c) { + this->log.info_f("Virtual network N-{:X} disconnected ({} TCP connections to close)", c->network_id, c->tcp_connections.size()); + for (const auto& [conn_id, conn] : c->tcp_connections) { + if (conn->server_channel) { + this->log.info_f("Closing TCP connection {:016X} on N-{:X}", conn_id, c->network_id); + conn->server_channel->disconnect(); + conn->server_channel.reset(); } else { - sim->send_pending_push_frame(c, *conn, true); + this->log.info_f("TCP connection {:016X} on N-{:X} has no server channel", conn_id, c->network_id); } } -} - -void IPStackSimulator::dispatch_on_server_input(struct bufferevent*, void* ctx) { - auto* conn = reinterpret_cast(ctx); - auto c = conn->client.lock(); - if (!c.get()) { - ip_stack_simulator_log.warning("Server input event triggered for deleted client; ignoring"); - } else { - auto sim = c->sim.lock(); - if (!sim) { - ip_stack_simulator_log.warning("Server input event triggered for client on deleted simulator; ignoring"); - } else { - sim->on_server_input(c, *conn); - } - } -} - -void IPStackSimulator::on_server_input(shared_ptr c, IPClient::TCPConnection& conn) { - struct evbuffer* buf = bufferevent_get_input(conn.server_bev.get()); - ip_stack_simulator_log.debug("Server input event: 0x%zX bytes to read", - evbuffer_get_length(buf)); - - auto sim = c->sim.lock(); - uint64_t idle_timeout_usecs = sim ? sim->state->client_idle_timeout_usecs : 60000000; - struct timeval tv = phosg::usecs_to_timeval(idle_timeout_usecs); - event_add(c->idle_timeout_event.get(), &tv); - - evbuffer_add_buffer(conn.pending_data.get(), buf); - this->send_pending_push_frame(c, conn, false); -} - -void IPStackSimulator::dispatch_on_server_error( - struct bufferevent*, short events, void* ctx) { - auto* conn = reinterpret_cast(ctx); - auto c = conn->client.lock(); - if (!c.get()) { - ip_stack_simulator_log.warning("Server error event triggered for deleted client; ignoring"); - } else { - auto sim = c->sim.lock(); - if (!sim) { - ip_stack_simulator_log.warning("Server error event triggered for client on deleted simulator; ignoring"); - } else { - sim->on_server_error(c, *conn, events); - } - } -} - -void IPStackSimulator::on_server_error( - shared_ptr c, IPClient::TCPConnection& conn, short events) { - if (events & BEV_EVENT_ERROR) { - int err = EVUTIL_SOCKET_ERROR(); - ip_stack_simulator_log.warning("Received error %d from virtual connection (%s)", err, - evutil_socket_error_to_string(err)); - } - if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) { - // Send an RST to the client. Kind of rude (we really should use FIN) but - // the PSO network stack always sends an RST to us when disconnecting, so - // whatever - this->send_tcp_frame(c, conn, TCPHeader::Flag::RST); - - // Delete the connection object (this also flushes and frees the server - // virtual connection bufferevent) - string conn_str = this->str_for_tcp_connection(c, conn); - ip_stack_simulator_log.info("Server closed TCP connection %s", conn_str.c_str()); - c->tcp_connections.erase(this->tcp_conn_key_for_connection(conn)); - } -} - -void IPStackSimulator::log_frame(const string& data) const { - if (this->pcap_text_log_file) { - phosg::print_data(this->pcap_text_log_file, data, 0, nullptr, phosg::PrintDataFlags::SKIP_SEPARATOR); - fputc('\n', this->pcap_text_log_file); - fflush(this->pcap_text_log_file); - } + + co_return; } diff --git a/src/IPStackSimulator.hh b/src/IPStackSimulator.hh index 376c411e..8854321a 100644 --- a/src/IPStackSimulator.hh +++ b/src/IPStackSimulator.hh @@ -1,169 +1,209 @@ #pragma once -#include #include -#include +#include +#include +#include #include #include #include +#include "AsyncUtils.hh" +#include "Channel.hh" #include "IPFrameInfo.hh" -#include "ProxyServer.hh" #include "Server.hh" #include "ServerState.hh" #include "Text.hh" -class IPStackSimulator : public std::enable_shared_from_this { +class IPStackSimulator; +class IPSSChannel; + +constexpr size_t DEFAULT_RESEND_PUSH_USECS = 200000; // 200ms + +enum class VirtualNetworkProtocol { + ETHERNET_TAPSERVER = 0, + HDLC_TAPSERVER, + HDLC_RAW, +}; + +struct IPSSSocket : ServerSocket { + VirtualNetworkProtocol protocol; +}; + +struct IPSSClient : std::enable_shared_from_this { + std::shared_ptr io_context; + std::weak_ptr sim; + uint64_t network_id; + asio::ip::tcp::socket sock; + VirtualNetworkProtocol protocol; + uint32_t hdlc_escape_control_character_flags = 0xFFFFFFFF; + uint32_t hdlc_remote_magic_number = 0; + parray mac_addr; // Only used for LinkType::ETHERNET + uint32_t ipv4_addr; + asio::steady_timer idle_timeout_timer; + + struct TCPConnection { + std::weak_ptr client; + std::shared_ptr server_channel; + bool awaiting_first_ack = true; + bool awaiting_ack = false; + uint32_t server_addr = 0; + uint16_t server_port = 0; + uint16_t client_port = 0; + uint32_t next_client_seq = 0; + uint32_t acked_server_seq = 0; + size_t resend_push_usecs = DEFAULT_RESEND_PUSH_USECS; + size_t next_push_max_frame_size = 1024; + size_t max_frame_size = 1024; + size_t bytes_received = 0; + size_t bytes_sent = 0; + size_t outbound_data_bytes = 0; + std::list outbound_data; + asio::steady_timer resend_push_timer; + + TCPConnection(std::shared_ptr client); + + inline uint64_t key() const { + return (static_cast(this->server_addr) << 32) | + (static_cast(this->server_port) << 16) | + static_cast(this->client_port); + } + static inline uint64_t key(const IPv4Header& ipv4, const TCPHeader& tcp) { + return (static_cast(ipv4.dest_addr) << 32) | + (static_cast(tcp.dest_port) << 16) | + static_cast(tcp.src_port); + } + static inline uint64_t key(const FrameInfo& fi) { + if (!fi.ipv4 || !fi.tcp) { + throw std::logic_error("tcp_conn_key_for_frame called on non-TCP frame"); + } + return key(*fi.ipv4, *fi.tcp); + } + + void drain_outbound_data(size_t bytes); + void linearize_outbound_data(size_t bytes); + }; + std::unordered_map> tcp_connections; + + IPSSClient( + std::shared_ptr sim, + uint64_t network_id, + VirtualNetworkProtocol protocol, + asio::ip::tcp::socket&& sock); + void reschedule_idle_timeout(); +}; + +// IPSSChannel provides an "unwrapped" connection to the rest of the server. It +// implements the Channel interface and can be used in place of an +// SocketChannel, so the rest of the server doesn't have to know about +// IPStackSimulator. +class IPSSChannel : public Channel { public: - enum class Protocol { - ETHERNET_TAPSERVER = 0, - HDLC_TAPSERVER, - HDLC_RAW, - }; + std::shared_ptr sim; + std::weak_ptr ipss_client; + std::weak_ptr tcp_conn; - using unique_listener = std::unique_ptr; - using unique_bufferevent = std::unique_ptr; - using unique_evbuffer = std::unique_ptr; - using unique_event = std::unique_ptr; + IPSSChannel( + std::shared_ptr sim, + std::weak_ptr ipss_client, + std::weak_ptr tcp_conn, + Version version, + uint8_t language, + const std::string& name = "", + phosg::TerminalFormat terminal_send_color = phosg::TerminalFormat::END, + phosg::TerminalFormat terminal_recv_color = phosg::TerminalFormat::END); - struct IPClient : std::enable_shared_from_this { - std::weak_ptr sim; - uint64_t network_id; + virtual std::string default_name() const; - unique_bufferevent bev; - Protocol protocol; - uint32_t hdlc_escape_control_character_flags = 0xFFFFFFFF; - uint32_t hdlc_remote_magic_number = 0; - parray mac_addr; // Only used for LinkType::ETHERNET - uint32_t ipv4_addr; + virtual bool connected() const; + virtual void disconnect(); - struct TCPConnection { - std::weak_ptr client; + // Adds inbound data, which will then be available via recv_raw(). This + // function is called by IPStackSimulator to forward "unwrapped" data to + // the game/proxy servers. + void add_inbound_data(const void* data, size_t size); - // The PSO protocol begins with the server sending a command, but we - // shouldn't send a PSH immediately after the SYN+ACK, so the connection - // isn't handed to the Server object until after the 3-way handshake - // (receive SYN, send SYN+ACK, receive ACK). This means server_bev is null - // during the first part of the connection phase. - unique_bufferevent server_bev; - // TODO: Get rid of pending_data and just use server_bev's input buffer in - // its place - unique_evbuffer pending_data; - unique_event resend_push_event; + virtual void send_raw(std::string&& data); + virtual asio::awaitable recv_raw(void* data, size_t size); - bool awaiting_first_ack; +private: + AsyncEvent data_available_signal; + std::deque inbound_data; + void* recv_buf = nullptr; + size_t recv_buf_size = 0; +}; - uint32_t server_addr; - uint16_t server_port; - uint16_t client_port; - uint32_t next_client_seq; - uint32_t acked_server_seq; - size_t resend_push_usecs; - size_t next_push_max_frame_size; - size_t max_frame_size; - size_t bytes_received; - size_t bytes_sent; +class IPStackSimulator + : public Server, + public std::enable_shared_from_this { +public: + IPStackSimulator(std::shared_ptr state); + ~IPStackSimulator() = default; - TCPConnection(); - }; - std::unordered_map tcp_connections; - - unique_event idle_timeout_event; - - IPClient(std::shared_ptr 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); - void on_idle_timeout(); - }; - - IPStackSimulator( - std::shared_ptr base, - std::shared_ptr 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); + void listen(const std::string& name, const std::string& addr, int port, VirtualNetworkProtocol protocol); static uint32_t connect_address_for_remote_address(uint32_t remote_addr); - std::shared_ptr get_network(uint64_t network_id) const; - inline const std::unordered_map>& all_networks() const { - return this->network_id_to_client; + inline std::shared_ptr get_state() { + return this->state; } - void disconnect_client(uint64_t network_id); - private: - std::shared_ptr base; std::shared_ptr state; - uint64_t next_network_id; - - struct ListeningSocket { - std::string name; - Protocol protocol; - unique_listener listener; - - ListeningSocket(const std::string& name, Protocol protocol, unique_listener&& l) - : name(name), - protocol(protocol), - listener(std::move(l)) {} - }; - - std::unordered_map listening_sockets; - std::unordered_map> network_id_to_client; + uint64_t next_network_id = 1; parray host_mac_address_bytes; parray broadcast_mac_address_bytes; - FILE* pcap_text_log_file; - - static uint64_t tcp_conn_key_for_connection(const IPClient::TCPConnection& conn); + static uint64_t tcp_conn_key_for_connection(std::shared_ptr 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 FrameInfo& fi); static std::string str_for_ipv4_netloc(uint32_t addr, uint16_t port); - static std::string str_for_tcp_connection(std::shared_ptr c, const IPClient::TCPConnection& conn); + static std::string str_for_tcp_connection( + std::shared_ptr c, std::shared_ptr conn); - static void dispatch_on_listen_accept(struct evconnlistener* listener, - evutil_socket_t fd, struct sockaddr* address, int socklen, void* ctx); - void on_listen_accept(struct evconnlistener* listener, evutil_socket_t fd, struct sockaddr* address, int socklen); - static void dispatch_on_listen_error(struct evconnlistener* listener, void* ctx); - void on_listen_error(struct evconnlistener* listener); + asio::awaitable send_ethernet_tapserver_frame( + std::shared_ptr c, FrameInfo::Protocol proto, const void* data, size_t size) const; + asio::awaitable send_hdlc_frame( + std::shared_ptr c, FrameInfo::Protocol proto, const void* data, size_t size, bool is_raw) const; + asio::awaitable send_layer3_frame( + std::shared_ptr c, FrameInfo::Protocol proto, const void* data, size_t size) const; + [[nodiscard]] inline asio::awaitable send_layer3_frame( + std::shared_ptr c, FrameInfo::Protocol proto, const std::string& data) const { + return this->send_layer3_frame(c, proto, data.data(), data.size()); + } - void send_layer3_frame(std::shared_ptr c, FrameInfo::Protocol proto, const std::string& data) const; - void send_layer3_frame(std::shared_ptr c, FrameInfo::Protocol proto, const void* data, size_t size) const; + asio::awaitable on_client_frame(std::shared_ptr c, const void* data, size_t size); + asio::awaitable on_client_lcp_frame(std::shared_ptr c, const FrameInfo& fi); + asio::awaitable on_client_pap_frame(std::shared_ptr c, const FrameInfo& fi); + asio::awaitable on_client_ipcp_frame(std::shared_ptr c, const FrameInfo& fi); + asio::awaitable on_client_arp_frame(std::shared_ptr c, const FrameInfo& fi); + asio::awaitable on_client_udp_frame(std::shared_ptr c, const FrameInfo& fi); + asio::awaitable on_client_tcp_frame(std::shared_ptr c, const FrameInfo& fi); - void on_client_frame(std::shared_ptr c, const std::string& frame); - void on_client_lcp_frame(std::shared_ptr c, const FrameInfo& fi); - void on_client_pap_frame(std::shared_ptr c, const FrameInfo& fi); - void on_client_ipcp_frame(std::shared_ptr c, const FrameInfo& fi); - void on_client_arp_frame(std::shared_ptr c, const FrameInfo& fi); - void on_client_udp_frame(std::shared_ptr c, const FrameInfo& fi); - void on_client_tcp_frame(std::shared_ptr c, const FrameInfo& fi); - - static void dispatch_on_server_input(struct bufferevent* bev, void* ctx); - void on_server_input(std::shared_ptr c, IPClient::TCPConnection& conn); - static void dispatch_on_server_error(struct bufferevent* bev, short events, void* ctx); - void on_server_error(std::shared_ptr c, IPClient::TCPConnection& conn, short events); - - static void dispatch_on_resend_push(evutil_socket_t, short, void* ctx); - void send_pending_push_frame(std::shared_ptr c, IPClient::TCPConnection& conn, bool always_send); - void send_tcp_frame( - std::shared_ptr c, - IPClient::TCPConnection& conn, + void schedule_send_pending_push_frame(std::shared_ptr conn, uint64_t delay_usecs); + asio::awaitable send_pending_push_frame( + std::shared_ptr c, std::shared_ptr conn); + asio::awaitable send_tcp_frame( + std::shared_ptr c, + std::shared_ptr conn, uint16_t flags = 0, - struct evbuffer* src_buf = nullptr, - size_t src_bytes = 0); + const void* payload_data = nullptr, + size_t payload_bytes = 0); - void open_server_connection(std::shared_ptr c, IPClient::TCPConnection& conn); + asio::awaitable open_server_connection( + std::shared_ptr c, std::shared_ptr conn); + asio::awaitable close_tcp_connection( + std::shared_ptr c, std::shared_ptr conn); - void log_frame(const std::string& data) const; + [[nodiscard]] virtual std::shared_ptr create_client( + std::shared_ptr listen_sock, asio::ip::tcp::socket&& client_sock); + asio::awaitable handle_tapserver_client(std::shared_ptr c); + asio::awaitable handle_hdlc_raw_client(std::shared_ptr c); + virtual asio::awaitable handle_client(std::shared_ptr c); + virtual asio::awaitable destroy_client(std::shared_ptr c); + + friend class IPSSChannel; }; diff --git a/src/IPV4RangeSet.cc b/src/IPV4RangeSet.cc index ec727cb8..261a5264 100644 --- a/src/IPV4RangeSet.cc +++ b/src/IPV4RangeSet.cc @@ -1,7 +1,5 @@ #include "IPV4RangeSet.hh" -#include - using namespace std; IPV4RangeSet::IPV4RangeSet(const phosg::JSON& json) { @@ -45,7 +43,7 @@ phosg::JSON IPV4RangeSet::json() const { for (const auto& it : this->ranges) { uint32_t addr = it.first; uint8_t mask_bits = it.second; - ret.emplace_back(phosg::string_printf("%hhu.%hhu.%hhu.%hhu/%hhu", + ret.emplace_back(std::format("{}.{}.{}.{}/{}", static_cast((addr >> 24) & 0xFF), static_cast((addr >> 16) & 0xFF), static_cast((addr >> 8) & 0xFF), @@ -63,11 +61,3 @@ bool IPV4RangeSet::check(uint32_t addr) const { 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(&ss); - return this->check(ntohl(sin->sin_addr.s_addr)); -} diff --git a/src/IPV4RangeSet.hh b/src/IPV4RangeSet.hh index 1849f2ae..55da17b1 100644 --- a/src/IPV4RangeSet.hh +++ b/src/IPV4RangeSet.hh @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -11,7 +12,6 @@ public: phosg::JSON json() const; bool check(uint32_t addr) const; - bool check(const struct sockaddr_storage& ss) const; protected: std::map ranges; // {addr: mask_bits} diff --git a/src/IntegralExpression.cc b/src/IntegralExpression.cc index b4d3f237..44761971 100644 --- a/src/IntegralExpression.cc +++ b/src/IntegralExpression.cc @@ -184,7 +184,7 @@ int64_t IntegralExpression::FlagLookupNode::evaluate(const Env& env) const { } string IntegralExpression::FlagLookupNode::str() const { - return phosg::string_printf("F_%04hX", this->flag_index); + return std::format("F_{:04X}", this->flag_index); } IntegralExpression::ChallengeCompletionLookupNode::ChallengeCompletionLookupNode( @@ -214,7 +214,7 @@ int64_t IntegralExpression::ChallengeCompletionLookupNode::evaluate(const Env& e } string IntegralExpression::ChallengeCompletionLookupNode::str() const { - return phosg::string_printf("CC_%s_%hhu", abbreviation_for_episode(this->episode), static_cast(this->stage_index + 1)); + return std::format("CC_{}_{}", abbreviation_for_episode(this->episode), static_cast(this->stage_index + 1)); } IntegralExpression::TeamRewardLookupNode::TeamRewardLookupNode(const string& reward_name) @@ -296,7 +296,7 @@ int64_t IntegralExpression::ConstantNode::evaluate(const Env&) const { } string IntegralExpression::ConstantNode::str() const { - return phosg::string_printf("%" PRId64, this->value); + return std::format("{}", this->value); } unique_ptr IntegralExpression::parse_expr(string_view text) { diff --git a/src/ItemCreator.cc b/src/ItemCreator.cc index ebd22e75..686c5790 100644 --- a/src/ItemCreator.cc +++ b/src/ItemCreator.cc @@ -34,9 +34,9 @@ ItemCreator::ItemCreator( GameMode mode, uint8_t difficulty, uint8_t section_id, - std::shared_ptr opt_rand_crypt, + std::shared_ptr rand_crypt, shared_ptr restrictions) - : log(phosg::string_printf("[ItemCreator:%s/%s/%s/%c/%hhu] ", phosg::name_for_enum(stack_limits->version), abbreviation_for_episode(episode), abbreviation_for_mode(mode), abbreviation_for_difficulty(difficulty), section_id), lobby_log.min_level), + : log(std::format("[ItemCreator:{}/{}/{}/{}/{}] ", phosg::name_for_enum(stack_limits->version), abbreviation_for_episode(episode), abbreviation_for_mode(mode), abbreviation_for_difficulty(difficulty), section_id), lobby_log.min_level), logic_version(stack_limits->version), stack_limits(stack_limits), episode(episode), @@ -52,18 +52,14 @@ ItemCreator::ItemCreator( common_item_set(common_item_set), pt(common_item_set->get_table(this->episode, this->mode, this->difficulty, this->section_id)), restrictions(restrictions), - opt_rand_crypt(opt_rand_crypt ? make_shared(opt_rand_crypt->seed()) : nullptr) { + rand_crypt(rand_crypt) { this->generate_unit_stars_tables(); } -void ItemCreator::set_random_crypt(shared_ptr new_random_crypt) { - this->opt_rand_crypt = new_random_crypt; -} - void ItemCreator::set_section_id(uint8_t new_section_id) { if (this->section_id != new_section_id) { this->section_id = new_section_id; - this->log.prefix = phosg::string_printf("[ItemCreator:%s/%s/%s/%c/%hhu] ", + this->log.prefix = std::format("[ItemCreator:{}/{}/{}/{}/{}] ", phosg::name_for_enum(stack_limits->version), abbreviation_for_episode(episode), abbreviation_for_mode(mode), @@ -150,7 +146,7 @@ ItemCreator::DropResult ItemCreator::on_box_item_drop(uint8_t area) { try { return this->on_box_item_drop_with_area_norm(this->normalize_area_number(area)); } catch (const exception& e) { - this->log.error("Exception in item creation: %s", e.what()); + this->log.error_f("Exception in item creation: {}", e.what()); return DropResult(); } } @@ -159,24 +155,20 @@ ItemCreator::DropResult ItemCreator::on_monster_item_drop(uint32_t enemy_type, u try { return this->on_monster_item_drop_with_area_norm(enemy_type, this->normalize_area_number(area)); } catch (const exception& e) { - this->log.error("Exception in item creation: %s", e.what()); + this->log.error_f("Exception in item creation: {}", e.what()); return DropResult(); } } ItemCreator::DropResult ItemCreator::on_box_item_drop_with_area_norm(uint8_t area_norm) { - this->log.info("Box drop checks for area_norm %02hhX", area_norm); - if (this->opt_rand_crypt) { - this->log.info("Random state: %08" PRIX32 " %08" PRIX32, - this->opt_rand_crypt->seed(), this->opt_rand_crypt->absolute_offset()); - } + this->log.info_f("Box drop checks for area_norm {:02X}", area_norm); DropResult res; res.item = this->check_rare_specs_and_create_rare_box_item(area_norm); if (!res.item.empty()) { res.is_from_rare_table = true; } else { uint8_t item_class = this->get_rand_from_weighted_tables_2d_vertical(this->pt->box_item_class_prob_table, area_norm); - this->log.info("Item class is %02hhX", item_class); + this->log.info_f("Item class is {:02X}", item_class); switch (item_class) { case 0: // Weapon res.item.data1[0] = 0; @@ -215,22 +207,18 @@ ItemCreator::DropResult ItemCreator::on_monster_item_drop_with_area_norm(uint32_ // Note: The original GC implementation uses (enemy_type > 0x58) here; we // extend it to the full array size for BB if (enemy_type >= 0x64) { - this->log.warning("Invalid enemy type: %" PRIX32, enemy_type); + this->log.warning_f("Invalid enemy type: {:X}", enemy_type); return DropResult(); } - this->log.info("Enemy type: %" PRIX32 "", enemy_type); - if (this->opt_rand_crypt) { - this->log.info("Random state: %08" PRIX32 " %08" PRIX32, - this->opt_rand_crypt->seed(), this->opt_rand_crypt->absolute_offset()); - } + this->log.info_f("Enemy type: {:X}", enemy_type); uint8_t type_drop_prob = this->pt->enemy_type_drop_probs.at(enemy_type); uint8_t drop_sample = this->rand_int(100); if (drop_sample >= type_drop_prob) { - this->log.info("Drop not chosen (%hhu >= %hhu)", drop_sample, type_drop_prob); + this->log.info_f("Drop not chosen ({} >= {})", drop_sample, type_drop_prob); return DropResult(); } else { - this->log.info("Drop chosen (%hhu < %hhu)", drop_sample, type_drop_prob); + this->log.info_f("Drop chosen ({} < {})", drop_sample, type_drop_prob); } DropResult res; @@ -258,7 +246,7 @@ ItemCreator::DropResult ItemCreator::on_monster_item_drop_with_area_norm(uint32_ throw logic_error("invalid item class determinant"); } - this->log.info("Rare drop not chosen; item class determinant is %" PRIu32 "; item class is %" PRIu32, item_class_determinant, item_class); + this->log.info_f("Rare drop not chosen; item class determinant is {}; item class is {}", item_class_determinant, item_class); switch (item_class) { case 0: // Weapon @@ -303,27 +291,27 @@ ItemData ItemCreator::check_rare_specs_and_create_rare_box_item(uint8_t area_nor for (const auto& spec : rare_specs) { item = this->check_rate_and_create_rare_item(spec, area_norm); if (!item.empty()) { - if (this->log.should_log(phosg::LogLevel::INFO)) { + if (this->log.should_log(phosg::LogLevel::L_INFO)) { auto hex = spec.data.hex(); - this->log.info("Box spec %08" PRIX32 " produced item %s", spec.probability, hex.c_str()); + this->log.info_f("Box spec {:08X} produced item {}", spec.probability, hex); } break; } - if (this->log.should_log(phosg::LogLevel::INFO)) { + if (this->log.should_log(phosg::LogLevel::L_INFO)) { auto hex = spec.data.hex(); - this->log.info("Box spec %08" PRIX32 " did not produce item %s", spec.probability, hex.c_str()); + this->log.info_f("Box spec {:08X} did not produce item {}", spec.probability, hex); } } return item; } uint32_t ItemCreator::rand_int(uint64_t max) { - return random_from_optional_crypt(this->opt_rand_crypt) % max; + return this->rand_crypt->next() % max; } float ItemCreator::rand_float_0_1_from_crypt() { // This lacks some precision, but matches the original implementation. - return (static_cast(random_from_optional_crypt(this->opt_rand_crypt) >> 16) / 65536.0); + return (static_cast(this->rand_crypt->next() >> 16) / 65536.0); } template @@ -344,7 +332,7 @@ uint32_t ItemCreator::choose_meseta_amount( ret = this->rand_int((max - min) + 1) + min; } - this->log.info("Chose %" PRIu32 " Meseta from range [%hu, %hu]", ret, min, max); + this->log.info_f("Chose {} Meseta from range [{}, {}]", ret, min, max); return ret; } @@ -364,15 +352,15 @@ ItemData ItemCreator::check_rare_spec_and_create_rare_enemy_item(uint32_t enemy_ for (const auto& spec : rare_specs) { item = this->check_rate_and_create_rare_item(spec, area_norm); if (!item.empty()) { - if (this->log.should_log(phosg::LogLevel::INFO)) { + if (this->log.should_log(phosg::LogLevel::L_INFO)) { auto hex = spec.data.hex(); - this->log.info("Enemy spec %08" PRIX32 " produced item %s", spec.probability, hex.c_str()); + this->log.info_f("Enemy spec {:08X} produced item {}", spec.probability, hex); } break; } - if (this->log.should_log(phosg::LogLevel::INFO)) { + if (this->log.should_log(phosg::LogLevel::L_INFO)) { auto hex = spec.data.hex(); - this->log.info("Enemy spec %08" PRIX32 " did not produce item %s", spec.probability, hex.c_str()); + this->log.info_f("Enemy spec {:08X} did not produce item {}", spec.probability, hex); } } } @@ -454,12 +442,12 @@ void ItemCreator::generate_common_weapon_bonuses(ItemData& item, uint8_t area_no for (size_t row = 0; row < 3; row++) { uint8_t spec = this->pt->nonrare_bonus_prob_spec.at(row).at(area_norm); if (spec == 0xFF) { - this->log.info("Bonus %zu is forbidden", row); + this->log.info_f("Bonus {} is forbidden", row); } else { item.data1[(row * 2) + 6] = this->get_rand_from_weighted_tables_2d_vertical(this->pt->bonus_type_prob_table, area_norm); int16_t amount = this->get_rand_from_weighted_tables_2d_vertical(this->pt->bonus_value_prob_table, spec); item.data1[(row * 2) + 7] = amount * 5 - 10; - this->log.info("Bonus %zu generated as %02hhX %02hhX from area_norm %02hhX and spec %02hhX", row, item.data1[(row * 2) + 6], item.data1[(row * 2) + 7], area_norm, spec); + this->log.info_f("Bonus {} generated as {:02X} {:02X} from area_norm {:02X} and spec {:02X}", row, item.data1[(row * 2) + 6], item.data1[(row * 2) + 7], area_norm, spec); } // Note: The original code has a special case here, which divides // item.data1[z + 7] by 5 and multiplies it by 5 again if bonus_type is 5 @@ -487,7 +475,7 @@ void ItemCreator::deduplicate_weapon_bonuses(ItemData& item) const { void ItemCreator::set_item_kill_count_if_unsealable(ItemData& item) const { if (this->item_parameter_table->is_unsealable_item(item)) { - this->log.info("Item is unsealable; setting kill count to zero"); + this->log.info_f("Item is unsealable; setting kill count to zero"); item.set_kill_count(0); } } @@ -526,7 +514,7 @@ void ItemCreator::clear_tool_item_if_invalid(ItemData& item) { void ItemCreator::clear_item_if_restricted(ItemData& item) const { if (this->item_parameter_table->is_item_rare(item) && !this->are_rare_drops_allowed()) { - this->log.info("Restricted: item is rare, but rares not allowed"); + this->log.info_f("Restricted: item is rare, but rares not allowed"); item.clear(); return; } @@ -537,12 +525,12 @@ void ItemCreator::clear_item_if_restricted(ItemData& item) const { // (HP/Resurrection and TP/Resurrection) only exist on BB. if (item.data1[0] == 1) { if ((item.data1[1] == 3) && (((item.data1[2] >= 0x33) && (item.data1[2] <= 0x38)) || (item.data1[2] == 0x61) || (item.data1[2] == 0x62))) { - this->log.info("Restricted: restore units not allowed in Challenge mode"); + this->log.info_f("Restricted: restore units not allowed in Challenge mode"); item.clear(); return; } } else if (item.data1[0] == 4) { - this->log.info("Restricted: meseta not allowed in Challenge mode"); + this->log.info_f("Restricted: meseta not allowed in Challenge mode"); item.clear(); return; } @@ -558,12 +546,12 @@ void ItemCreator::clear_item_if_restricted(ItemData& item) const { break; case BattleRules::WeaponAndArmorMode::FORBID_RARES: if (this->item_parameter_table->is_item_rare(item)) { - this->log.info("Restricted: rare weapons and armors not allowed"); + this->log.info_f("Restricted: rare weapons and armors not allowed"); item.clear(); } break; case BattleRules::WeaponAndArmorMode::FORBID_ALL: - this->log.info("Restricted: weapons and armors not allowed"); + this->log.info_f("Restricted: weapons and armors not allowed"); item.clear(); break; default: @@ -572,24 +560,24 @@ void ItemCreator::clear_item_if_restricted(ItemData& item) const { break; case 2: if (this->restrictions->mag_mode == BattleRules::MagMode::FORBID_ALL) { - this->log.info("Restricted: mags not allowed"); + this->log.info_f("Restricted: mags not allowed"); item.clear(); } break; case 3: if (this->restrictions->tool_mode == BattleRules::ToolMode::FORBID_ALL) { - this->log.info("Restricted: tools not allowed"); + this->log.info_f("Restricted: tools not allowed"); item.clear(); } else if (item.data1[1] == 2) { switch (this->restrictions->tech_disk_mode) { case BattleRules::TechDiskMode::ALLOW: break; case BattleRules::TechDiskMode::FORBID_ALL: - this->log.info("Restricted: tech disks not allowed"); + this->log.info_f("Restricted: tech disks not allowed"); item.clear(); break; case BattleRules::TechDiskMode::LIMIT_LEVEL: - this->log.info("Restricted: tech disk level limited to %hhu", + this->log.info_f("Restricted: tech disk level limited to {}", static_cast(this->restrictions->max_tech_level + 1)); if (this->restrictions->max_tech_level == 0) { item.data1[2] = 0; @@ -601,13 +589,13 @@ void ItemCreator::clear_item_if_restricted(ItemData& item) const { throw logic_error("invalid tech disk mode"); } } else if ((item.data1[1] == 9) && this->restrictions->forbid_scape_dolls) { - this->log.info("Restricted: scape dolls not allowed"); + this->log.info_f("Restricted: scape dolls not allowed"); item.clear(); } break; case 4: if (this->restrictions->meseta_mode == BattleRules::MesetaMode::FORBID_ALL) { - this->log.info("Restricted: meseta not allowed"); + this->log.info_f("Restricted: meseta not allowed"); item.clear(); } break; @@ -627,10 +615,10 @@ void ItemCreator::generate_common_item_variances(uint32_t area_norm, ItemData& i float f1 = 1.0 + this->pt->unit_max_stars_table.at(area_norm); float f2 = this->rand_float_0_1_from_crypt(); uint8_t stars = static_cast(f1 * f2) & 0xFF; - this->log.info("Unit stars: %g * %g = %" PRIu32, f1, f2, stars); + this->log.info_f("Unit stars: {:g} * {:g} = {}", f1, f2, stars); this->generate_common_unit_variances(stars, item); if (item.data1[2] == 0xFF) { - this->log.info("Unit subtype not valid; clearing item"); + this->log.info_f("Unit subtype not valid; clearing item"); item.clear(); } } else { @@ -667,7 +655,7 @@ void ItemCreator::generate_common_armor_or_shield_type_and_variances(char area_n } else { item.data1[2] -= 3; } - this->log.info("Armor/shield type: max(%02hhX + %02hhX + %02hhX - 3, 0) = %02hhX", + this->log.info_f("Armor/shield type: max({:02X} + {:02X} + {:02X} - 3, 0) = {:02X}", area_norm, type, this->pt->armor_or_shield_type_bias, item.data1[2]); } @@ -696,7 +684,7 @@ void ItemCreator::generate_common_tool_variances(uint32_t area_norm, ItemData& i if ((!is_v1_or_v2(this->logic_version) || (this->logic_version == Version::GC_NTE)) && (tool_class == 0x1A)) { tool_class = 0x73; } - this->log.info("Generating tool with class %02hhX", tool_class); + this->log.info_f("Generating tool with class {:02X}", tool_class); // Note: This block was originally a separate function called // generate_common_tool_type @@ -711,7 +699,7 @@ void ItemCreator::generate_common_tool_variances(uint32_t area_norm, ItemData& i item.data1[1] = data.first; item.data1[2] = data.second; } catch (const out_of_range&) { - this->log.info("Tool class is missing; skipping item generation"); + this->log.info_f("Tool class is missing; skipping item generation"); return; } } @@ -744,9 +732,9 @@ void ItemCreator::generate_common_mag_variances(ItemData& item) { if (is_pre_v1(this->logic_version)) { item.data2[3] = 0x00; } else if (is_v1_or_v2(this->logic_version)) { - item.data2[3] = random_from_optional_crypt(this->opt_rand_crypt) % 0x0E; + item.data2[3] = this->rand_crypt->next() % 0x0E; } else { - item.data2[3] = random_from_optional_crypt(this->opt_rand_crypt) % 0x12; + item.data2[3] = this->rand_crypt->next() % 0x12; } } } @@ -769,7 +757,7 @@ void ItemCreator::generate_common_weapon_variances(uint8_t area_norm, ItemData& } } - this->log.info("Subtype table: %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX", + this->log.info_f("Subtype table: {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X}", weapon_type_prob_table[0], weapon_type_prob_table[1], weapon_type_prob_table[2], weapon_type_prob_table[3], weapon_type_prob_table[4], weapon_type_prob_table[5], weapon_type_prob_table[6], weapon_type_prob_table[7], weapon_type_prob_table[8], weapon_type_prob_table[9], weapon_type_prob_table[10], weapon_type_prob_table[11], @@ -777,19 +765,19 @@ void ItemCreator::generate_common_weapon_variances(uint8_t area_norm, ItemData& item.data1[1] = this->get_rand_from_weighted_tables_1d(weapon_type_prob_table); if (item.data1[1] == 0) { - this->log.info("00 chosen from subtype table; skipping item"); + this->log.info_f("00 chosen from subtype table; skipping item"); item.clear(); } else { int8_t subtype_base = this->pt->subtype_base_table.at(item.data1[1] - 1); uint8_t area_length = this->pt->subtype_area_length_table.at(item.data1[1] - 1); - this->log.info("Subtype table yielded %02hhX; subtype base is %hhd with area length %hhu", item.data1[1], subtype_base, area_length); + this->log.info_f("Subtype table yielded {:02X}; subtype base is {} with area length {}", item.data1[1], subtype_base, area_length); if (subtype_base < 0) { item.data1[2] = (area_norm + subtype_base) / area_length; - this->log.info("Resulting subtype: (%02hhX + %02hhX) / %02hhX = %02hhX", area_norm, subtype_base, area_length, item.data1[2]); + this->log.info_f("Resulting subtype: ({:02X} + {:02X}) / {:02X} = {:02X}", area_norm, subtype_base, area_length, item.data1[2]); this->generate_common_weapon_grind(item, (area_norm + subtype_base) - (item.data1[2] * area_length)); } else { item.data1[2] = subtype_base + (area_norm / area_length); - this->log.info("Resulting subtype: %02hhX + (%02hhX / %02hhX) = %02hhX", subtype_base, area_norm, area_length, item.data1[2]); + this->log.info_f("Resulting subtype: {:02X} + ({:02X} / {:02X}) = {:02X}", subtype_base, area_norm, area_length, item.data1[2]); this->generate_common_weapon_grind(item, area_norm - (area_norm / area_length) * area_length); } this->generate_common_weapon_bonuses(item, area_norm); @@ -802,7 +790,7 @@ void ItemCreator::generate_common_weapon_grind(ItemData& item, uint8_t offset_wi if (item.data1[0] == 0) { uint8_t offset = clamp(offset_within_subtype_range, 0, 3); item.data1[3] = this->get_rand_from_weighted_tables_2d_vertical(this->pt->grind_prob_table, offset); - this->log.info("Generated grind %02hhX from offset within subtype range %02hhX", item.data1[3], offset_within_subtype_range); + this->log.info_f("Generated grind {:02X} from offset within subtype range {:02X}", item.data1[3], offset_within_subtype_range); } } @@ -811,18 +799,18 @@ void ItemCreator::generate_common_weapon_special(ItemData& item, uint8_t area_no return; } if (this->item_parameter_table->is_item_rare(item)) { - this->log.info("Item is rare; skipping special generation"); + this->log.info_f("Item is rare; skipping special generation"); return; } uint8_t special_mult = this->pt->special_mult.at(area_norm); if (special_mult == 0) { - this->log.info("Special multiplier is zero for area_norm %02hhX; skipping special generation", area_norm); + this->log.info_f("Special multiplier is zero for area_norm {:02X}; skipping special generation", area_norm); return; } uint8_t det = this->rand_int(100); uint8_t prob = this->pt->special_percent.at(area_norm); if (det >= prob) { - this->log.info("Special not chosen (%02hhX > %02hhX)", det, prob); + this->log.info_f("Special not chosen ({:02X} > {:02X})", det, prob); return; } item.data1[4] = this->choose_weapon_special(special_mult * this->rand_float_0_1_from_crypt()); @@ -830,25 +818,25 @@ void ItemCreator::generate_common_weapon_special(ItemData& item, uint8_t area_no uint8_t ItemCreator::choose_weapon_special(uint8_t det) { if (det >= 4) { - this->log.info("Special not chosen (det %02hhX >= 4)", det); + this->log.info_f("Special not chosen (det {:02X} >= 4)", det); return 0; } static const uint8_t maxes[4] = {8, 10, 11, 11}; uint8_t det2 = this->rand_int(maxes[det]); - this->log.info("Choosing special with det %02hhX and det2 %02hhX", det, det2); + this->log.info_f("Choosing special with det {:02X} and det2 {:02X}", det, det2); size_t index = 0; for (size_t z = 1; z < this->item_parameter_table->num_specials; z++) { if (det + 1 == this->item_parameter_table->get_special_stars(z)) { if (index == det2) { - this->log.info("Chose special %02zX", z); + this->log.info_f("Chose special {:02X}", z); return z; } else { index++; } } } - this->log.info("No special was eligible"); + this->log.info_f("No special was eligible"); return 0; } @@ -922,7 +910,7 @@ void ItemCreator::generate_common_unit_variances(uint8_t stars, ItemData& item) const auto& results = this->unit_results_by_star_count.at(stars); if (results.empty()) { - this->log.info("There are no available units with %hhu stars", stars); + this->log.info_f("There are no available units with {} stars", stars); return; } @@ -934,7 +922,7 @@ void ItemCreator::generate_common_unit_variances(uint8_t stars, ItemData& item) const auto& def = this->item_parameter_table->get_unit(result.unit); item.set_unit_bonus(def.modifier_amount * result.modifier); } - this->log.info("Generated unit %02hhX with modifier %hhd, from %zu choices with %hhu stars", + this->log.info_f("Generated unit {:02X} with modifier {}, from {} choices with {} stars", result.unit, result.modifier, results.size(), stars); } @@ -1076,7 +1064,7 @@ void ItemCreator::generate_armor_shop_armors(vector& shop, size_t play pt.push(src_table.first[z].value); } } - pt.shuffle(this->opt_rand_crypt); + pt.shuffle(this->rand_crypt); for (size_t items_generated = 0; items_generated < num_items;) { ItemData item; @@ -1120,7 +1108,7 @@ void ItemCreator::generate_armor_shop_shields(vector& shop, size_t pla pt.push(src_table.first[z].value); } } - pt.shuffle(this->opt_rand_crypt); + pt.shuffle(this->rand_crypt); for (size_t items_generated = 0; items_generated < num_items;) { ItemData item; @@ -1163,7 +1151,7 @@ void ItemCreator::generate_armor_shop_units(vector& shop, size_t playe pt.push(src_table.first[z].value); } } - pt.shuffle(this->opt_rand_crypt); + pt.shuffle(this->rand_crypt); for (size_t items_generated = 0; items_generated < num_items;) { ItemData item; @@ -1266,7 +1254,7 @@ void ItemCreator::generate_rare_tool_shop_recovery_items( pt.push(e.value); } } - pt.shuffle(this->opt_rand_crypt); + pt.shuffle(this->rand_crypt); size_t effective_num_items = num_items; size_t items_generated = 0; @@ -1309,7 +1297,7 @@ void ItemCreator::generate_tool_shop_tech_disks(vector& shop, size_t p pt.push(e.value); } } - pt.shuffle(this->opt_rand_crypt); + pt.shuffle(this->rand_crypt); static const array tech_num_map = { 0x00, 0x03, 0x06, 0x0F, 0x10, 0x0D, 0x0A, 0x0B, 0x0C, 0x01, 0x04, 0x07, @@ -1410,7 +1398,7 @@ vector ItemCreator::generate_weapon_shop_contents(size_t player_level) pt.push(e.value); } } - pt.shuffle(this->opt_rand_crypt); + pt.shuffle(this->rand_crypt); vector shop; while (shop.size() < num_items) { @@ -1608,7 +1596,7 @@ void ItemCreator::generate_weapon_shop_item_special(ItemData& item, size_t playe // Note: The original code shuffles pt and then pops a single value from it. // For simplicity, we just sample a single value (and don't pop it) instead. - switch (pt.sample(this->opt_rand_crypt)) { + switch (pt.sample(this->rand_crypt)) { case 0: item.data1[4] = 0; break; @@ -1660,7 +1648,7 @@ void ItemCreator::generate_weapon_shop_item_bonus1( // Note: The original code shuffles pt and then pops a single value from it. // For simplicity, we just sample a single value (and don't pop it) instead. - item.data1[6] = pt.sample(this->opt_rand_crypt); + item.data1[6] = pt.sample(this->rand_crypt); if (item.data1[6] == 0) { item.data1[7] = 0; @@ -1701,7 +1689,7 @@ void ItemCreator::generate_weapon_shop_item_bonus2(ItemData& item, size_t player pt.push(e.value); } } - pt.shuffle(this->opt_rand_crypt); + pt.shuffle(this->rand_crypt); do { item.data1[8] = pt.pop(); @@ -1782,14 +1770,14 @@ ssize_t ItemCreator::apply_tekker_deltas(ItemData& item, uint8_t section_id) { bool favored = item.data1[1] == favored_weapon_by_section_id[section_id]; ssize_t luck = 0; - this->log.info("Applying tekker deltas for %s weapon", favored ? "favored" : "non-favored"); + this->log.info_f("Applying tekker deltas for {} weapon", favored ? "favored" : "non-favored"); // Adjust the weapon's special { const auto& prob_table = this->tekker_adjustment_set->get_special_upgrade_prob_table(section_id, favored); - uint8_t delta_index = prob_table.sample(this->opt_rand_crypt); + uint8_t delta_index = prob_table.sample(this->rand_crypt); int8_t delta = delta_table.at(delta_index); - this->log.info("(Special) Delta index %hhu, delta %hhd", delta_index, delta); + this->log.info_f("(Special) Delta index {}, delta {}", delta_index, delta); // Note: The original code checks specifically for -1 and +1 here, but the // data files only include delta_indexes 4, 5, and 6 (which correspond to -1, // 0, and 1) anyway, so we just check for positive and negative numbers @@ -1809,29 +1797,29 @@ ssize_t ItemCreator::apply_tekker_deltas(ItemData& item, uint8_t section_id) { this->item_parameter_table->get_special(new_special).type) { item.data1[4] = new_special; } else { - this->log.info("(Special) Delta canceled because it would change special category"); + this->log.info_f("(Special) Delta canceled because it would change special category"); } } } catch (const out_of_range&) { // Invalid special number passed to get_special; just ignore it } luck += this->tekker_adjustment_set->get_luck_for_special_upgrade(delta_index); - this->log.info("(Special) Luck is now %zd", luck); + this->log.info_f("(Special) Luck is now {}", luck); } // Adjust the weapon's grind if it's not rare if (!this->item_parameter_table->is_item_rare(item)) { const auto& weapon_def = this->item_parameter_table->get_weapon(item.data1[1], item.data1[2]); const auto& prob_table = this->tekker_adjustment_set->get_grind_delta_prob_table(section_id, favored); - uint8_t delta_index = prob_table.sample(this->opt_rand_crypt); + uint8_t delta_index = prob_table.sample(this->rand_crypt); int8_t delta = delta_table.at(delta_index); - this->log.info("(Grind) Delta index %hhu, delta %hhd", delta_index, delta); + this->log.info_f("(Grind) Delta index {}, delta {}", delta_index, delta); int16_t new_grind = static_cast(item.data1[3]) + static_cast(delta); item.data1[3] = clamp(new_grind, 0, weapon_def.max_grind); luck += this->tekker_adjustment_set->get_luck_for_grind_delta(delta_index); - this->log.info("(Grind) Luck is now %zd", luck); + this->log.info_f("(Grind) Luck is now {}", luck); } else { - this->log.info("(Grind) Item is rare; skipping grind adjustment"); + this->log.info_f("(Grind) Item is rare; skipping grind adjustment"); } // Adjust the weapon's bonuses @@ -1839,9 +1827,9 @@ ssize_t ItemCreator::apply_tekker_deltas(ItemData& item, uint8_t section_id) { const auto& prob_table = this->tekker_adjustment_set->get_bonus_delta_prob_table(section_id, favored); // Note: The original code really does use the same delta for all three // bonuses. - uint8_t delta_index = prob_table.sample(this->opt_rand_crypt); + uint8_t delta_index = prob_table.sample(this->rand_crypt); int8_t delta = delta_table.at(delta_index); - this->log.info("(Bonuses) Delta index %hhu, delta %hhd", delta_index, delta); + this->log.info_f("(Bonuses) Delta index {}, delta {}", delta_index, delta); // Note: The original code doesn't check if there's actually a bonus in each // slot before incrementing the values. Presumably there's a check later // that will clear any invalid bonuses, but we don't have such a check, so @@ -1852,7 +1840,7 @@ ssize_t ItemCreator::apply_tekker_deltas(ItemData& item, uint8_t section_id) { } } luck += this->tekker_adjustment_set->get_luck_for_bonus_delta(delta_index); - this->log.info("(Bonuses) Luck is now %zd", luck); + this->log.info_f("(Bonuses) Luck is now {}", luck); } return luck; diff --git a/src/ItemCreator.hh b/src/ItemCreator.hh index c4c27916..3df0aa0d 100644 --- a/src/ItemCreator.hh +++ b/src/ItemCreator.hh @@ -24,12 +24,10 @@ public: GameMode mode, uint8_t difficulty, uint8_t section_id, - std::shared_ptr opt_rand_crypt, + std::shared_ptr rand_crypt, std::shared_ptr restrictions = nullptr); ~ItemCreator() = default; - void set_random_crypt(std::shared_ptr new_random_crypt); - struct DropResult { ItemData item; bool is_from_rare_table = false; @@ -101,7 +99,7 @@ private: // [0x0E] - apparently unused // [0x0F] - which common weapon special to generate // [0x10] - apparently unused - std::shared_ptr opt_rand_crypt; + std::shared_ptr rand_crypt; bool are_rare_drops_allowed() const; uint8_t normalize_area_number(uint8_t area) const; diff --git a/src/ItemData.cc b/src/ItemData.cc index 95da08ef..8edadbd3 100644 --- a/src/ItemData.cc +++ b/src/ItemData.cc @@ -797,13 +797,13 @@ ItemData ItemData::from_primary_identifier(const StackLimits& limits, uint32_t p } string ItemData::hex() const { - return phosg::string_printf("%08" PRIX32 " %08" PRIX32 " %08" PRIX32 " (%08" PRIX32 ") %08" PRIX32, - this->data1db[0].load(), this->data1db[1].load(), this->data1db[2].load(), this->id.load(), this->data2db.load()); + return std::format("{:08X} {:08X} {:08X} ({:08X}) {:08X}", + this->data1db[0], this->data1db[1], this->data1db[2], this->id, this->data2db); } string ItemData::short_hex() const { - auto ret = phosg::string_printf("%08" PRIX32 "%08" PRIX32 "%08" PRIX32 "%08" PRIX32, - this->data1db[0].load(), this->data1db[1].load(), this->data1db[2].load(), this->data2db.load()); + auto ret = std::format("{:08X}{:08X}{:08X}{:08X}", + this->data1db[0], this->data1db[1], this->data1db[2], this->data2db); size_t offset = ret.find_last_not_of('0'); if (offset != string::npos) { offset += (offset & 1) ? 1 : 2; diff --git a/src/ItemNameIndex.cc b/src/ItemNameIndex.cc index e3341ac8..b98ca31b 100644 --- a/src/ItemNameIndex.cc +++ b/src/ItemNameIndex.cc @@ -99,7 +99,7 @@ const array name_for_s_rank_special = { std::string ItemNameIndex::describe_item(const ItemData& item, bool include_color_escapes, bool hide_mag_stats) const { if (item.data1[0] == 0x04) { - return phosg::string_printf("%s%" PRIu32 " Meseta", include_color_escapes ? "$C7" : "", item.data2d.load()); + return std::format("{}{} Meseta", include_color_escapes ? "$C7" : "", item.data2d); } vector ret_tokens; @@ -120,7 +120,7 @@ std::string ItemNameIndex::describe_item(const ItemData& item, bool include_colo try { ret_tokens.emplace_back(name_for_weapon_special.at(special_id)); } catch (const out_of_range&) { - ret_tokens.emplace_back(phosg::string_printf("!SP:%02hhX", special_id)); + ret_tokens.emplace_back(std::format("!SP:{:02X}", special_id)); } } } @@ -128,7 +128,7 @@ std::string ItemNameIndex::describe_item(const ItemData& item, bool include_colo try { ret_tokens.emplace_back(name_for_s_rank_special.at(item.data1[2])); } catch (const out_of_range&) { - ret_tokens.emplace_back(phosg::string_printf("!SSP:%02hhX", item.data1[2])); + ret_tokens.emplace_back(std::format("!SSP:{:02X}", item.data1[2])); } } @@ -149,27 +149,27 @@ std::string ItemNameIndex::describe_item(const ItemData& item, bool include_colo technique_name = tech_id_to_name.at(item.data1[4]); technique_name[0] = toupper(technique_name[0]); } catch (const out_of_range&) { - technique_name = phosg::string_printf("!TD:%02hhX", item.data1[4]); + technique_name = std::format("!TD:{:02X}", item.data1[4]); } // Hide the level for Reverser and Ryuker, unless the level isn't 1 if ((item.data1[2] == 0) && ((item.data1[4] == 0x0E) || (item.data1[4] == 0x11))) { - ret_tokens.emplace_back(phosg::string_printf("Disk:%s", technique_name.c_str())); + ret_tokens.emplace_back(std::format("Disk:{}", technique_name)); } else { - ret_tokens.emplace_back(phosg::string_printf("Disk:%s Lv.%d", technique_name.c_str(), item.data1[2] + 1)); + ret_tokens.emplace_back(std::format("Disk:{} Lv.{}", technique_name, item.data1[2] + 1)); } } else { try { auto meta = this->primary_identifier_index.at(primary_identifier); ret_tokens.emplace_back(meta->name); } catch (const out_of_range&) { - ret_tokens.emplace_back(phosg::string_printf("!ID:%08" PRIX32, primary_identifier)); + ret_tokens.emplace_back(std::format("!ID:{:08X}", primary_identifier)); } } if (item.data1[0] == 0x00) { // For weapons, add the grind and bonuses, or S-rank name if applicable if (item.data1[3] > 0) { - ret_tokens.emplace_back(phosg::string_printf("+%hhu", item.data1[3])); + ret_tokens.emplace_back(std::format("+{}", item.data1[3])); } if (item.is_s_rank_weapon()) { @@ -220,9 +220,9 @@ std::string ItemNameIndex::describe_item(const ItemData& item, bool include_colo } if (which & 0x80) { uint16_t kill_count = ((which << 8) & 0x7F00) | (value & 0xFF); - ret_tokens.emplace_back(phosg::string_printf("K:%hu", kill_count)); + ret_tokens.emplace_back(std::format("K:{}", kill_count)); } else if (which > 5) { - ret_tokens.emplace_back(phosg::string_printf("!PC:%02hhX%02hhX", which, value)); + ret_tokens.emplace_back(std::format("!PC:{:02X}{:02X}", which, value)); } else { bonuses[which - 1] = value; } @@ -232,11 +232,11 @@ std::string ItemNameIndex::describe_item(const ItemData& item, bool include_colo 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(phosg::string_printf("%s%hhd/%hhd/%hhd/%hhd/%s%hhd", + ret_tokens.emplace_back(std::format("{}{}/{}/{}/{}/{}{}", color_prefix, bonuses[0], bonuses[1], bonuses[2], bonuses[3], (should_highlight_hit ? "$C6" : ""), bonuses[4])); } else { - ret_tokens.emplace_back(phosg::string_printf("%s%hhd/%hhd/%hhd/%hhd", + ret_tokens.emplace_back(std::format("{}{}/{}/{}/{}", color_prefix, bonuses[0], bonuses[1], bonuses[2], bonuses[3])); } } @@ -255,7 +255,7 @@ std::string ItemNameIndex::describe_item(const ItemData& item, bool include_colo } else if (modifier <= -3) { ret_tokens.back().append("--"); } else if (modifier != 0) { - ret_tokens.emplace_back(phosg::string_printf("!MD:%04hX", modifier)); + ret_tokens.emplace_back(std::format("!MD:{:04X}", modifier)); } } else { // Armor/shields @@ -263,22 +263,22 @@ std::string ItemNameIndex::describe_item(const ItemData& item, bool include_colo if (item.data1[5] == 1) { ret_tokens.emplace_back("(1 slot)"); } else { - ret_tokens.emplace_back(phosg::string_printf("(%hhu slots)", item.data1[5])); + ret_tokens.emplace_back(std::format("({} slots)", item.data1[5])); } } if (item.data1w[3] != 0) { - ret_tokens.emplace_back(phosg::string_printf("+%hdDEF", - static_cast(item.data1w[3].load()))); + ret_tokens.emplace_back(std::format("+{}DEF", + static_cast(item.data1w[3]))); } if (item.data1w[4] != 0) { - ret_tokens.emplace_back(phosg::string_printf("+%hdEVP", - static_cast(item.data1w[4].load()))); + ret_tokens.emplace_back(std::format("+{}EVP", + static_cast(item.data1w[4]))); } } } else if (!hide_mag_stats && (item.data1[0] == 0x02)) { // For mags, add tons of info - ret_tokens.emplace_back(phosg::string_printf("LV%hhu", item.data1[2])); + ret_tokens.emplace_back(std::format("LV{}", item.data1[2])); uint16_t def = item.data1w[2]; uint16_t pow = item.data1w[3]; @@ -288,16 +288,16 @@ std::string ItemNameIndex::describe_item(const ItemData& item, bool include_colo uint16_t level = stat / 100; uint8_t partial = stat % 100; if (partial == 0) { - return phosg::string_printf("%hu", level); + return std::format("{}", level); } else if (partial % 10 == 0) { - return phosg::string_printf("%hu.%hhu", level, static_cast(partial / 10)); + return std::format("{}.{}", level, static_cast(partial / 10)); } else { - return phosg::string_printf("%hu.%02hhu", level, partial); + return std::format("{}.{:02}", level, partial); } }; ret_tokens.emplace_back(format_stat(def) + "/" + format_stat(pow) + "/" + format_stat(dex) + "/" + format_stat(mind)); - ret_tokens.emplace_back(phosg::string_printf("%hhu%%", item.data2[0])); - ret_tokens.emplace_back(phosg::string_printf("%hhuIQ", item.data2[1])); + ret_tokens.emplace_back(std::format("{}%", item.data2[0])); + ret_tokens.emplace_back(std::format("{}IQ", item.data2[1])); uint8_t flags = item.data2[2]; if (flags & 7) { @@ -332,15 +332,15 @@ std::string ItemNameIndex::describe_item(const ItemData& item, bool include_colo } try { - ret_tokens.emplace_back(phosg::string_printf("(%s)", name_for_mag_color.at(item.data2[3]))); + ret_tokens.emplace_back(std::format("({})", name_for_mag_color.at(item.data2[3]))); } catch (const out_of_range&) { - ret_tokens.emplace_back(phosg::string_printf("(!CL:%02hhX)", item.data2[3])); + ret_tokens.emplace_back(std::format("(!CL:{:02X})", item.data2[3])); } } else if (item.data1[0] == 0x03) { // For tools, add the amount (if applicable) if (item.max_stack_size(*this->limits) > 1) { - ret_tokens.emplace_back(phosg::string_printf("x%hhu", item.data1[5])); + ret_tokens.emplace_back(std::format("x{}", item.data1[5])); } } @@ -374,11 +374,11 @@ ItemData ItemNameIndex::parse_item_description(const std::string& desc) const { ret = ItemData::from_data(phosg::parse_data_string(desc)); } catch (const exception& ed) { if (strcmp(e1.what(), e2.what())) { - throw runtime_error(phosg::string_printf("cannot parse item description \"%s\" (as text 1: %s) (as text 2: %s) (as data: %s)", - desc.c_str(), e1.what(), e2.what(), ed.what())); + throw runtime_error(std::format("cannot parse item description \"{}\" (as text 1: {}) (as text 2: {}) (as data: {})", + desc, e1.what(), e2.what(), ed.what())); } else { - throw runtime_error(phosg::string_printf("cannot parse item description \"%s\" (as text: %s) (as data: %s)", - desc.c_str(), e1.what(), ed.what())); + throw runtime_error(std::format("cannot parse item description \"{}\" (as text: {}) (as data: {})", + desc, e1.what(), ed.what())); } } } @@ -394,13 +394,13 @@ ItemData ItemNameIndex::parse_item_description_phase(const std::string& descript ret.data2d = 0; string desc = phosg::tolower(description); - if (phosg::ends_with(desc, " meseta")) { + if (desc.ends_with(" meseta")) { ret.data1[0] = 0x04; ret.data2d = stol(desc, nullptr, 10); return ret; } - if (phosg::starts_with(desc, "disk:")) { + if (desc.starts_with("disk:")) { auto tokens = phosg::split(desc, ' '); tokens[0] = tokens[0].substr(5); // Trim off "disk:" if ((tokens[0] == "reverser") || (tokens[0] == "ryuker")) { @@ -413,7 +413,7 @@ ItemData ItemNameIndex::parse_item_description_phase(const std::string& descript if (tokens.size() != 2) { throw runtime_error("invalid tech disk format"); } - if (!phosg::starts_with(tokens[1], "lv.")) { + if (!tokens[1].starts_with("lv.")) { throw runtime_error("invalid tech disk level"); } uint8_t tech = technique_for_name(tokens[0]); @@ -426,11 +426,11 @@ ItemData ItemNameIndex::parse_item_description_phase(const std::string& descript return ret; } - bool is_wrapped = phosg::starts_with(desc, "wrapped "); + bool is_wrapped = desc.starts_with("wrapped "); if (is_wrapped) { desc = desc.substr(8); } - bool is_unidentified = phosg::starts_with(desc, "?"); + bool is_unidentified = desc.starts_with("?"); if (is_unidentified) { size_t z; for (z = 1; z < desc.size(); z++) { @@ -450,7 +450,7 @@ ItemData ItemNameIndex::parse_item_description_phase(const std::string& descript } string prefix = phosg::tolower(name_for_weapon_special[z]); prefix += ' '; - if (phosg::starts_with(desc, prefix)) { + if (desc.starts_with(prefix)) { weapon_special = z; desc = desc.substr(prefix.size()); break; @@ -464,7 +464,7 @@ ItemData ItemNameIndex::parse_item_description_phase(const std::string& descript // then we'll see Sange & Yasha first, which we should skip. size_t lookback = 0; while (lookback < 4) { - if (name_it != this->name_index.end() && phosg::starts_with(desc, name_it->first)) { + if (name_it != this->name_index.end() && desc.starts_with(name_it->first)) { break; } else if (name_it == this->name_index.begin()) { throw runtime_error("no such item"); @@ -478,7 +478,7 @@ ItemData ItemNameIndex::parse_item_description_phase(const std::string& descript } desc = desc.substr(name_it->first.size()); - if (phosg::starts_with(desc, " ")) { + if (desc.starts_with(" ")) { desc = desc.substr(1); } @@ -499,7 +499,7 @@ ItemData ItemNameIndex::parse_item_description_phase(const std::string& descript if (token.empty()) { continue; } - if (phosg::starts_with(token, "+")) { + if (token.starts_with("+")) { token = token.substr(1); ret.data1[3] = stoul(token, nullptr, 10); @@ -513,7 +513,7 @@ ItemData ItemNameIndex::parse_item_description_phase(const std::string& descript char ch = toupper(token[z]); const char* pos = strchr(s_rank_name_characters, ch); if (!pos) { - throw runtime_error(phosg::string_printf("s-rank name contains invalid character %02hhX (%c)", ch, ch)); + throw runtime_error(std::format("s-rank name contains invalid character {:02X} ({})", ch, ch)); } char_indexes[z] = (pos - s_rank_name_characters); } @@ -563,12 +563,12 @@ ItemData ItemNameIndex::parse_item_description_phase(const std::string& descript for (const auto& token : phosg::split(desc, ' ')) { if (token.empty()) { continue; - } else if (!phosg::starts_with(token, "+")) { + } else if (!token.starts_with("+")) { throw runtime_error("invalid armor/shield modifier"); } - if (phosg::ends_with(token, "def")) { + if (token.ends_with("def")) { ret.data1w[3] = static_cast(stol(token.substr(1, token.size() - 4), nullptr, 10)); - } else if (phosg::ends_with(token, "evp")) { + } else if (token.ends_with("evp")) { ret.data1w[4] = static_cast(stol(token.substr(1, token.size() - 4), nullptr, 10)); } else { ret.data1[5] = stoul(token.substr(1), nullptr, 10); @@ -584,7 +584,7 @@ ItemData ItemNameIndex::parse_item_description_phase(const std::string& descript for (const auto& token : phosg::split(desc, ' ')) { if (token.empty()) { continue; - } else if (phosg::starts_with(token, "pb:")) { // Photon blasts + } else if (token.starts_with("pb:")) { // Photon blasts auto pb_tokens = phosg::split(token.substr(3), ','); if (pb_tokens.size() > 3) { throw runtime_error("too many photon blasts specified"); @@ -602,9 +602,9 @@ ItemData ItemNameIndex::parse_item_description_phase(const std::string& descript for (const auto& pb_token : pb_tokens) { ret.add_mag_photon_blast(name_to_pb_num.at(pb_token)); } - } else if (phosg::ends_with(token, "%")) { // Synchro + } else if (token.ends_with("%")) { // Synchro ret.data2[0] = stoul(token.substr(0, token.size() - 1), nullptr, 10); - } else if (phosg::ends_with(token, "iq")) { // IQ + } else if (token.ends_with("iq")) { // IQ ret.data2[1] = stoul(token.substr(0, token.size() - 2), nullptr, 10); } else if (!token.empty() && isdigit(token[0])) { // Stats auto s_tokens = phosg::split(token, '/'); @@ -636,7 +636,7 @@ ItemData ItemNameIndex::parse_item_description_phase(const std::string& descript } } else if (ret.data1[0] == 0x03) { if (ret.max_stack_size(*this->limits) > 1) { - if (phosg::starts_with(desc, "x")) { + if (desc.starts_with("x")) { ret.data1[5] = stoul(desc.substr(1), nullptr, 10); } else { ret.data1[5] = 1; @@ -662,11 +662,11 @@ ItemData ItemNameIndex::parse_item_description_phase(const std::string& descript void ItemNameIndex::print_table(FILE* stream) const { auto pmt = this->item_parameter_table; - fprintf(stream, "WEAPON => ---ID--- TYPE SKIN POINTS FLAG ATPLO ATPHI ATPRQ MSTRQ ATARQ -MST- GND PH SP ATA SB PJ 1X 1Y 2X 2Y CL A1 A2 A3 A4 A5 TB BF V1 ST* USL ---DIVISOR--- NAME\n"); + phosg::fwrite_fmt(stream, "WEAPON => ---ID--- TYPE SKIN POINTS FLAG ATPLO ATPHI ATPRQ MSTRQ ATARQ -MST- GND PH SP ATA SB PJ 1X 1Y 2X 2Y CL A1 A2 A3 A4 A5 TB BF V1 ST* USL ---DIVISOR--- NAME\n"); for (size_t data1_1 = 0; data1_1 < pmt->num_weapon_classes; data1_1++) { uint8_t v1_replacement = pmt->get_weapon_v1_replacement(data1_1); float sale_divisor = pmt->get_sale_divisor(0x00, data1_1); - string divisor_str = phosg::string_printf("%g", sale_divisor); + string divisor_str = std::format("{:g}", sale_divisor); divisor_str.resize(13, ' '); size_t data1_2_limit = pmt->num_weapons_in_class(data1_1); @@ -681,20 +681,20 @@ void ItemNameIndex::print_table(FILE* stream) const { item.data1[2] = data1_2; string name = this->describe_item(item); - fprintf(stream, "00%02zX%02zX => %08" PRIX32 " %04hX %04hX %6" PRIu32 " %04hX %5hu %5hu %5hu %5hu %5hu %5hu %3hhu %02hhX %02hhX %3hhu %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX %2hhu* %s %s %s\n", + phosg::fwrite_fmt(stream, "00{:02X}{:02X} => {:08X} {:04X} {:04X} {:6} {:04X} {:5} {:5} {:5} {:5} {:5} {:5} {:3} {:02X} {:02X} {:3} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:2}* {} {} {}\n", data1_1, data1_2, - w.base.id.load(), - w.base.type.load(), - w.base.skin.load(), - w.base.team_points.load(), - w.class_flags.load(), - w.atp_min.load(), - w.atp_max.load(), - w.atp_required.load(), - w.mst_required.load(), - w.ata_required.load(), - w.mst.load(), + w.base.id, + w.base.type, + w.base.skin, + w.base.team_points, + w.class_flags, + w.atp_min, + w.atp_max, + w.atp_required, + w.mst_required, + w.ata_required, + w.mst, w.max_grind, w.photon, w.special, @@ -716,15 +716,15 @@ void ItemNameIndex::print_table(FILE* stream) const { v1_replacement, stars, is_unsealable ? "YES" : " no", - divisor_str.c_str(), - name.c_str()); + divisor_str, + name); } } - fprintf(stream, "ARMOR => ---ID--- TYPE SKIN POINTS -DFP- -EVP- BP BE FLAG LVL EFR ETH EIC EDK ELT DFR EVR SB TB FT A4 ST* ---DIVISOR--- NAME\n"); + phosg::fwrite_fmt(stream, "ARMOR => ---ID--- TYPE SKIN POINTS -DFP- -EVP- BP BE FLAG LVL EFR ETH EIC EDK ELT DFR EVR SB TB FT A4 ST* ---DIVISOR--- NAME\n"); for (size_t data1_1 = 1; data1_1 < 3; data1_1++) { float sale_divisor = pmt->get_sale_divisor(0x01, data1_1); - string divisor_str = phosg::string_printf("%g", sale_divisor); + string divisor_str = std::format("{:g}", sale_divisor); divisor_str.resize(13, ' '); size_t data1_2_limit = pmt->num_armors_or_shields_in_class(data1_1); @@ -738,18 +738,18 @@ void ItemNameIndex::print_table(FILE* stream) const { item.data1[2] = data1_2; string name = this->describe_item(item); - fprintf(stream, "01%02zX%02zX => %08" PRIX32 " %04hX %04hX %6" PRIu32 " %5hu %5hu %02hhX %02hhX %04hX %3hhu %3hhu %3hhu %3hhu %3hhu %3hhu %3hhu %3hhu %02hhX %02hhX %02hhX %02hhX %2hhu* %s %s\n", + phosg::fwrite_fmt(stream, "01{:02X}{:02X} => {:08X} {:04X} {:04X} {:6} {:5} {:5} {:02X} {:02X} {:04X} {:3} {:3} {:3} {:3} {:3} {:3} {:3} {:3} {:02X} {:02X} {:02X} {:02X} {:2}* {} {}\n", data1_1, data1_2, - a.base.id.load(), - a.base.type.load(), - a.base.skin.load(), - a.base.team_points.load(), - a.dfp.load(), - a.evp.load(), + a.base.id, + a.base.type, + a.base.skin, + a.base.team_points, + a.dfp, + a.evp, a.block_particle, a.block_effect, - a.class_flags.load(), + a.class_flags, static_cast(a.required_level + 1), a.efr, a.eth, @@ -763,15 +763,15 @@ void ItemNameIndex::print_table(FILE* stream) const { a.flags_type, a.unknown_a4, stars, - divisor_str.c_str(), - name.c_str()); + divisor_str, + name); } } - fprintf(stream, "UNIT => ---ID--- TYPE SKIN POINTS STAT COUNT ST-MOD ST* ---DIVISOR--- NAME\n"); + phosg::fwrite_fmt(stream, "UNIT => ---ID--- TYPE SKIN POINTS STAT COUNT ST-MOD ST* ---DIVISOR--- NAME\n"); { float sale_divisor = pmt->get_sale_divisor(0x01, 0x03); - string divisor_str = phosg::string_printf("%g", sale_divisor); + string divisor_str = std::format("{:g}", sale_divisor); divisor_str.resize(13, ' '); size_t data1_2_limit = pmt->num_units(); @@ -785,29 +785,29 @@ void ItemNameIndex::print_table(FILE* stream) const { item.data1[2] = data1_2; string name = this->describe_item(item); - fprintf(stream, "0103%02zX => %08" PRIX32 " %04hX %04hX %6" PRIu32 " %04hX %5hu %6hd %2hhu* %s %s\n", + phosg::fwrite_fmt(stream, "0103{:02X} => {:08X} {:04X} {:04X} {:6} {:04X} {:5} {:6} {:2}* {} {}\n", data1_2, - u.base.id.load(), - u.base.type.load(), - u.base.skin.load(), - u.base.team_points.load(), - u.stat.load(), - u.stat_amount.load(), - u.modifier_amount.load(), + u.base.id, + u.base.type, + u.base.skin, + u.base.team_points, + u.stat, + u.stat_amount, + u.modifier_amount, stars, - divisor_str.c_str(), - name.c_str()); + divisor_str, + name); } } - fprintf(stream, "MAG => ---ID--- TYPE SKIN POINTS FTBL PB AC E1 E2 E3 E4 C1 C2 C3 C4 FLAG ---DIVISOR--- NAME\n"); + phosg::fwrite_fmt(stream, "MAG => ---ID--- TYPE SKIN POINTS FTBL PB AC E1 E2 E3 E4 C1 C2 C3 C4 FLAG ---DIVISOR--- NAME\n"); { size_t data1_1_limit = pmt->num_mags(); for (size_t data1_1 = 0; data1_1 < data1_1_limit; data1_1++) { const auto& m = pmt->get_mag(data1_1); float sale_divisor = pmt->get_sale_divisor(0x02, data1_1); - string divisor_str = phosg::string_printf("%g", sale_divisor); + string divisor_str = std::format("{:g}", sale_divisor); divisor_str.resize(13, ' '); ItemData item; @@ -816,13 +816,13 @@ void ItemNameIndex::print_table(FILE* stream) const { item.data1[2] = 0x00; string name = this->describe_item(item); - fprintf(stream, "02%02zX00 => %08" PRIX32 " %04hX %04hX %6" PRIu32 " %04hX %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX %02hhX %04hX %s %s\n", + phosg::fwrite_fmt(stream, "02{:02X}00 => {:08X} {:04X} {:04X} {:6} {:04X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:04X} {} {}\n", data1_1, - m.base.id.load(), - m.base.type.load(), - m.base.skin.load(), - m.base.team_points.load(), - m.feed_table.load(), + m.base.id, + m.base.type, + m.base.skin, + m.base.team_points, + m.feed_table, m.photon_blast, m.activation, m.on_pb_full, @@ -833,16 +833,16 @@ void ItemNameIndex::print_table(FILE* stream) const { m.on_low_hp_flag, m.on_death_flag, m.on_boss_flag, - m.class_flags.load(), - divisor_str.c_str(), - name.c_str()); + m.class_flags, + divisor_str, + name); } } - fprintf(stream, "TOOL => ---ID--- TYPE SKIN POINTS COUNT TECH -COST- ITEMFLAG ---DIVISOR--- NAME\n"); + phosg::fwrite_fmt(stream, "TOOL => ---ID--- TYPE SKIN POINTS COUNT TECH -COST- ITEMFLAG ---DIVISOR--- NAME\n"); for (size_t data1_1 = 0; data1_1 < pmt->num_tool_classes; data1_1++) { float sale_divisor = pmt->get_sale_divisor(0x03, data1_1); - string divisor_str = phosg::string_printf("%g", sale_divisor); + string divisor_str = std::format("{:g}", sale_divisor); divisor_str.resize(13, ' '); size_t data1_2_limit = pmt->num_tools_in_class(data1_1); @@ -856,34 +856,34 @@ void ItemNameIndex::print_table(FILE* stream) const { item.set_tool_item_amount(*this->limits, 1); string name = this->describe_item(item); - fprintf(stream, "03%02zX%02zX => %08" PRIX32 " %04hX %04hX %6" PRIu32 " %5hu %04hX %6" PRId32 " %08" PRIX32 " %s %s\n", + phosg::fwrite_fmt(stream, "03{:02X}{:02X} => {:08X} {:04X} {:04X} {:6} {:5} {:04X} {:6} {:08X} {} {}\n", data1_1, data1_2, - t.base.id.load(), - t.base.type.load(), - t.base.skin.load(), - t.base.team_points.load(), - t.amount.load(), - t.tech.load(), - t.cost.load(), - t.item_flags.load(), - divisor_str.c_str(), - name.c_str()); + t.base.id, + t.base.type, + t.base.skin, + t.base.team_points, + t.amount, + t.tech, + t.cost, + t.item_flags, + divisor_str, + name); } } - fprintf(stream, "CLASS => F GF RF B GB RB Z GZ RZ GR DB JL ZL SH RY RS AT RV MG\n"); + phosg::fwrite_fmt(stream, "CLASS => F GF RF B GB RB Z GZ RZ GR DB JL ZL SH RY RS AT RV MG\n"); for (size_t char_class = 0; char_class < 12; char_class++) { - fprintf(stream, "%9s =>", name_for_char_class(char_class)); + phosg::fwrite_fmt(stream, "{:9} =>", name_for_char_class(char_class)); for (size_t tech_num = 0; tech_num < 0x13; tech_num++) { uint8_t max_level = pmt->get_max_tech_level(char_class, tech_num) + 1; if (max_level == 0x00) { - fprintf(stream, " "); + phosg::fwrite_fmt(stream, " "); } else { - fprintf(stream, " %2hhu", max_level); + phosg::fwrite_fmt(stream, " {:2}", max_level); } } - fprintf(stream, "\n"); + phosg::fwrite_fmt(stream, "\n"); } for (size_t table_index = 0; table_index < 8; table_index++) { @@ -891,58 +891,58 @@ void ItemNameIndex::print_table(FILE* stream) const { "Monomate", "Dimate", "Trimate", "Monofluid", "Difluid", "Trifluid", "Antidote", "Antiparalysis", "Sol Atomizer", "Moon Atomizer", "Star Atomizer"}; - fprintf(stream, "TABLE %02zX => -DEF -POW -DEX MIND -IQ- SYNC\n", table_index); + phosg::fwrite_fmt(stream, "TABLE {:02X} => -DEF -POW -DEX MIND -IQ- SYNC\n", table_index); for (size_t which = 0; which < 11; which++) { const auto& res = pmt->get_mag_feed_result(table_index, which); - fprintf(stream, "%14s => %4hhd %4hhd %4hhd %4hhd %4hhd %4hhd\n", + phosg::fwrite_fmt(stream, "{:14} => {:4} {:4} {:4} {:4} {:4} {:4}\n", names[which], res.def, res.pow, res.dex, res.mind, res.iq, res.synchro); } } - fprintf(stream, "SPECIAL => TYPE COUNT ST*\n"); + phosg::fwrite_fmt(stream, "SPECIAL => TYPE COUNT ST*\n"); for (size_t index = 0; index < pmt->num_specials; index++) { const auto& sp = pmt->get_special(index); uint8_t stars = pmt->get_special_stars(index); - fprintf(stream, " %02zX => %04hX %5hu %2hu*\n", index, sp.type.load(), sp.amount.load(), stars); + phosg::fwrite_fmt(stream, " {:02X} => {:04X} {:5} {:2}*\n", index, sp.type, sp.amount, stars); } - fprintf(stream, "---USE + -EQUIP => RESULT MLV GND LVL CLS\n"); + phosg::fwrite_fmt(stream, "---USE + -EQUIP => RESULT MLV GND LVL CLS\n"); for (const auto& combo_list_it : pmt->get_all_item_combinations()) { for (const auto& combo : combo_list_it.second) { - fprintf(stream, "%02hhX%02hhX%02hhX + %02hhX%02hhX%02hhX => %02hhX%02hhX%02hhX", + phosg::fwrite_fmt(stream, "{:02X}{:02X}{:02X} + {:02X}{:02X}{:02X} => {:02X}{:02X}{:02X}", combo.used_item[0], combo.used_item[1], combo.used_item[2], combo.equipped_item[0], combo.equipped_item[1], combo.equipped_item[2], combo.result_item[0], combo.result_item[1], combo.result_item[2]); if (combo.mag_level != 0xFF) { - fprintf(stream, " %3hu", combo.mag_level); + phosg::fwrite_fmt(stream, " {:3}", combo.mag_level); } else { - fprintf(stream, " "); + phosg::fwrite_fmt(stream, " "); } if (combo.grind != 0xFF) { - fprintf(stream, " %3hu", combo.grind); + phosg::fwrite_fmt(stream, " {:3}", combo.grind); } else { - fprintf(stream, " "); + phosg::fwrite_fmt(stream, " "); } if (combo.level != 0xFF) { - fprintf(stream, " %3hu", combo.level); + phosg::fwrite_fmt(stream, " {:3}", combo.level); } else { - fprintf(stream, " "); + phosg::fwrite_fmt(stream, " "); } if (combo.char_class != 0xFF) { - fprintf(stream, " %3hu\n", combo.char_class); + phosg::fwrite_fmt(stream, " {:3}\n", combo.char_class); } else { - fprintf(stream, " \n"); + phosg::fwrite_fmt(stream, " \n"); } } } size_t num_events = pmt->num_events(); for (size_t event_number = 0; event_number < num_events; event_number++) { - fprintf(stream, "EV %3zu => PRB\n", event_number); + phosg::fwrite_fmt(stream, "EV {:3} => PRB\n", event_number); auto events_list = pmt->get_event_items(event_number); for (size_t z = 0; z < events_list.second; z++) { const auto& event_item = events_list.first[z]; - fprintf(stream, "%02hhX%02hhX%02hhX => %3hhu\n", + phosg::fwrite_fmt(stream, "{:02X}{:02X}{:02X} => {:3}\n", event_item.item[0], event_item.item[1], event_item.item[2], event_item.probability); } } diff --git a/src/ItemParameterTable.cc b/src/ItemParameterTable.cc index 22d59ac9..a3a11aef 100644 --- a/src/ItemParameterTable.cc +++ b/src/ItemParameterTable.cc @@ -181,16 +181,16 @@ ItemParameterTable::WeaponV4 ItemParameterTable::WeaponV1V2::to_v4() const { ItemParameterTable::WeaponV4 ItemParameterTable::WeaponGCNTE::to_v4() const { WeaponV4 ret; - ret.base.id = this->base.id.load(); - ret.base.type = this->base.type.load(); - ret.base.skin = this->base.skin.load(); - ret.class_flags = this->class_flags.load(); - ret.atp_min = this->atp_min.load(); - ret.atp_max = this->atp_max.load(); - ret.atp_required = this->atp_required.load(); - ret.mst_required = this->mst_required.load(); - ret.ata_required = this->ata_required.load(); - ret.mst = this->mst.load(); + ret.base.id = this->base.id; + ret.base.type = this->base.type; + ret.base.skin = this->base.skin; + ret.class_flags = this->class_flags; + ret.atp_min = this->atp_min; + ret.atp_max = this->atp_max; + ret.atp_required = this->atp_required; + ret.mst_required = this->mst_required; + ret.ata_required = this->ata_required; + ret.mst = this->mst; ret.max_grind = this->max_grind; ret.photon = this->photon; ret.special = this->special; @@ -211,16 +211,16 @@ ItemParameterTable::WeaponV4 ItemParameterTable::WeaponGCNTE::to_v4() const { template ItemParameterTable::WeaponV4 ItemParameterTable::WeaponV3T::to_v4() const { WeaponV4 ret; - ret.base.id = this->base.id.load(); - ret.base.type = this->base.type.load(); - ret.base.skin = this->base.skin.load(); - ret.class_flags = this->class_flags.load(); - ret.atp_min = this->atp_min.load(); - ret.atp_max = this->atp_max.load(); - ret.atp_required = this->atp_required.load(); - ret.mst_required = this->mst_required.load(); - ret.ata_required = this->ata_required.load(); - ret.mst = this->mst.load(); + ret.base.id = this->base.id; + ret.base.type = this->base.type; + ret.base.skin = this->base.skin; + ret.class_flags = this->class_flags; + ret.atp_min = this->atp_min; + ret.atp_max = this->atp_max; + ret.atp_required = this->atp_required; + ret.mst_required = this->mst_required; + ret.ata_required = this->ata_required; + ret.mst = this->mst; ret.max_grind = this->max_grind; ret.photon = this->photon; ret.special = this->special; @@ -287,14 +287,14 @@ ItemParameterTable::ArmorOrShieldV4 ItemParameterTable::ArmorOrShieldV1V2::to_v4 template ItemParameterTable::ArmorOrShieldV4 ItemParameterTable::ArmorOrShieldV3T::to_v4() const { ArmorOrShieldV4 ret; - ret.base.id = this->base.id.load(); - ret.base.type = this->base.type.load(); - ret.base.skin = this->base.skin.load(); - ret.dfp = this->dfp.load(); - ret.evp = this->evp.load(); + ret.base.id = this->base.id; + ret.base.type = this->base.type; + ret.base.skin = this->base.skin; + ret.dfp = this->dfp; + ret.evp = this->evp; ret.block_particle = this->block_particle; ret.block_effect = this->block_effect; - ret.class_flags = this->class_flags.load(); + ret.class_flags = this->class_flags; ret.required_level = this->required_level; ret.efr = this->efr; ret.eth = this->eth; @@ -330,12 +330,12 @@ ItemParameterTable::UnitV4 ItemParameterTable::UnitV1V2::to_v4() const { template ItemParameterTable::UnitV4 ItemParameterTable::UnitV3T::to_v4() const { UnitV4 ret; - ret.base.id = this->base.id.load(); - ret.base.type = this->base.type.load(); - ret.base.skin = this->base.skin.load(); - ret.stat = this->stat.load(); - ret.stat_amount = this->stat_amount.load(); - ret.modifier_amount = this->modifier_amount.load(); + ret.base.id = this->base.id; + ret.base.type = this->base.type; + ret.base.skin = this->base.skin; + ret.stat = this->stat; + ret.stat_amount = this->stat_amount; + ret.modifier_amount = this->modifier_amount; return ret; } @@ -377,10 +377,10 @@ ItemParameterTable::MagV4 ItemParameterTable::MagV2::to_v4() const { template ItemParameterTable::MagV4 ItemParameterTable::MagV3T::to_v4() const { MagV4 ret; - ret.base.id = this->base.id.load(); - ret.base.type = this->base.type.load(); - ret.base.skin = this->base.skin.load(); - ret.feed_table = this->feed_table.load(); + ret.base.id = this->base.id; + ret.base.type = this->base.type; + ret.base.skin = this->base.skin; + ret.feed_table = this->feed_table; ret.photon_blast = this->photon_blast; ret.activation = this->activation; ret.on_pb_full = this->on_pb_full; @@ -391,7 +391,7 @@ ItemParameterTable::MagV4 ItemParameterTable::MagV3T::to_v4() const { ret.on_low_hp_flag = this->on_low_hp_flag; ret.on_death_flag = this->on_death_flag; ret.on_boss_flag = this->on_boss_flag; - ret.class_flags = this->class_flags.load(); + ret.class_flags = this->class_flags; return ret; } @@ -408,13 +408,13 @@ ItemParameterTable::ToolV4 ItemParameterTable::ToolV1V2::to_v4() const { template ItemParameterTable::ToolV4 ItemParameterTable::ToolV3T::to_v4() const { ToolV4 ret; - ret.base.id = this->base.id.load(); - ret.base.type = this->base.type.load(); - ret.base.skin = this->base.skin.load(); - ret.amount = this->amount.load(); - ret.tech = this->tech.load(); - ret.cost = this->cost.load(); - ret.item_flags = this->item_flags.load(); + ret.base.id = this->base.id; + ret.base.type = this->base.type; + ret.base.skin = this->base.skin; + ret.amount = this->amount; + ret.tech = this->tech; + ret.cost = this->cost; + ret.item_flags = this->item_flags; return ret; } @@ -720,7 +720,7 @@ pair ItemParameterTable::find_tool_by_id_t(uint32_t tool_table } } } - throw out_of_range(phosg::string_printf("invalid tool class %08" PRIX32, item_id)); + throw out_of_range(std::format("invalid tool class {:08X}", item_id)); } pair ItemParameterTable::find_tool_by_id(uint32_t item_id) const { @@ -907,8 +907,8 @@ const ItemParameterTable::Special& ItemParameterTable::get_special(uint8_t speci this->parsed_specials.resize(special + 1); } const auto& sp_be = this->r.pget(this->offsets_gc_nte->special_data_table + sizeof(SpecialBE) * special); - this->parsed_specials[special].type = sp_be.type.load(); - this->parsed_specials[special].amount = sp_be.amount.load(); + this->parsed_specials[special].type = sp_be.type; + this->parsed_specials[special].amount = sp_be.amount; } return this->parsed_specials[special]; } else if (this->offsets_v3_be) { @@ -917,8 +917,8 @@ const ItemParameterTable::Special& ItemParameterTable::get_special(uint8_t speci this->parsed_specials.resize(special + 1); } const auto& sp_be = this->r.pget(this->offsets_v3_be->special_data_table + sizeof(SpecialBE) * special); - this->parsed_specials[special].type = sp_be.type.load(); - this->parsed_specials[special].amount = sp_be.amount.load(); + this->parsed_specials[special].type = sp_be.type; + this->parsed_specials[special].amount = sp_be.amount; } return this->parsed_specials[special]; } else if (this->offsets_v4) { diff --git a/src/ItemTranslationTable.cc b/src/ItemTranslationTable.cc index 46f38fea..384db832 100644 --- a/src/ItemTranslationTable.cc +++ b/src/ItemTranslationTable.cc @@ -23,12 +23,12 @@ ItemTranslationTable::ItemTranslationTable( if (is_canonical(id)) { has_any_canonical_id = true; if (!this->entry_index_for_version[v_s].emplace(id, z).second) { - throw runtime_error(phosg::string_printf("(row %zu) duplicate canonical ID %08" PRIX32, z, id)); + throw runtime_error(std::format("(row {}) duplicate canonical ID {:08X}", z, id)); } } } if (!has_any_canonical_id) { - throw runtime_error(phosg::string_printf("(row %zu) no canonical ID present in row", z)); + throw runtime_error(std::format("(row {}) no canonical ID present in row", z)); } } @@ -46,25 +46,25 @@ ItemTranslationTable::ItemTranslationTable( uint32_t e_id = this->entries[z].id_for_version[v_s]; if (is_canonical(e_id)) { if (!entry_index.count(e_id)) { - throw logic_error(phosg::string_printf("(row %zu version %s) canonical ID %" PRIX32 " is missing from the index", z, phosg::name_for_enum(v), e_id)); + throw logic_error(std::format("(row {} version {}) canonical ID {:X} is missing from the index", z, phosg::name_for_enum(v), e_id)); } try { item_parameter_table->definition_for_primary_identifier(e_id); } catch (const out_of_range&) { - throw runtime_error(phosg::string_printf("(row %zu version %s) ID %" PRIX32 " not defined in item parameter table", z, phosg::name_for_enum(v), e_id)); + throw runtime_error(std::format("(row {} version {}) ID {:X} not defined in item parameter table", z, phosg::name_for_enum(v), e_id)); } if (!remaining_identifiers.erase(e_id)) { - throw runtime_error(phosg::string_printf("(row %zu version %s) ID %" PRIX32 " not in item parameter table's primary identifier list", z, phosg::name_for_enum(v), e_id)); + throw runtime_error(std::format("(row {} version {}) ID {:X} not in item parameter table's primary identifier list", z, phosg::name_for_enum(v), e_id)); } } else if (!entry_index.count(make_canonical(e_id))) { - throw runtime_error(phosg::string_printf("(row %zu version %s) ID %" PRIX32 " refers to nonexistent canonical ID", z, phosg::name_for_enum(v), e_id)); + throw runtime_error(std::format("(row {} version {}) ID {:X} refers to nonexistent canonical ID", z, phosg::name_for_enum(v), e_id)); } } if (!remaining_identifiers.empty()) { - string missing_str = phosg::string_printf("(version %s) not all identifiers in the item parameter table are defined in the translation table; missing:", phosg::name_for_enum(v)); + string missing_str = std::format("(version {}) not all identifiers in the item parameter table are defined in the translation table; missing:", phosg::name_for_enum(v)); for (uint32_t id : remaining_identifiers) { - missing_str += phosg::string_printf(" %08" PRIX32, id); + missing_str += std::format(" {:08X}", id); } throw runtime_error(missing_str); } diff --git a/src/Items.cc b/src/Items.cc index 58d598e9..e840a997 100644 --- a/src/Items.cc +++ b/src/Items.cc @@ -6,7 +6,7 @@ using namespace std; -void player_use_item(shared_ptr c, size_t item_index, shared_ptr opt_rand_crypt) { +void player_use_item(shared_ptr c, size_t item_index, shared_ptr rand_crypt) { auto s = c->require_server_state(); // On PC (and presumably DC), the client sends a 6x29 after this to delete the @@ -177,7 +177,7 @@ void player_use_item(shared_ptr c, size_t item_index, shared_ptrnext() % sum; for (size_t z = 0; z < table.second; z++) { const auto& entry = table.first[z]; if (det > entry.probability) { diff --git a/src/Items.hh b/src/Items.hh index 20f48b0c..308dda02 100644 --- a/src/Items.hh +++ b/src/Items.hh @@ -12,7 +12,7 @@ #include "ServerState.hh" #include "StaticGameData.hh" -void player_use_item(std::shared_ptr c, size_t item_index, std::shared_ptr opt_rand_crypt); +void player_use_item(std::shared_ptr c, size_t item_index, std::shared_ptr rand_crypt); void player_feed_mag(std::shared_ptr c, size_t mag_item_index, size_t fed_item_index); void apply_mag_feed_result( diff --git a/src/LevelTable.cc b/src/LevelTable.cc index d075ed1b..1d40a03a 100644 --- a/src/LevelTable.cc +++ b/src/LevelTable.cc @@ -129,7 +129,7 @@ LevelTableV3BE::LevelTableV3BE(const string& data, bool encrypted) { dest_delta.ata = src_delta.ata; dest_delta.lck = src_delta.lck; dest_delta.tp = src_delta.tp; - dest_delta.experience = src_delta.experience.load(); + dest_delta.experience = src_delta.experience; } } } diff --git a/src/LevelTable.hh b/src/LevelTable.hh index c4b403e7..54398e0e 100644 --- a/src/LevelTable.hh +++ b/src/LevelTable.hh @@ -24,13 +24,13 @@ struct CharacterStatsT { operator CharacterStatsT() const { CharacterStatsT ret; - ret.atp = this->atp.load(); - ret.mst = this->mst.load(); - ret.evp = this->evp.load(); - ret.hp = this->hp.load(); - ret.dfp = this->dfp.load(); - ret.ata = this->ata.load(); - ret.lck = this->lck.load(); + ret.atp = this->atp; + ret.mst = this->mst; + ret.evp = this->evp; + ret.hp = this->hp; + ret.dfp = this->dfp; + ret.ata = this->ata; + ret.lck = this->lck; return ret; } } __attribute__((packed)); @@ -53,12 +53,12 @@ struct PlayerStatsT { operator PlayerStatsT() const { PlayerStatsT 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(); + ret.esp = this->esp; + ret.height = this->height; + ret.unknown_a3 = this->unknown_a3; + ret.level = this->level; + ret.experience = this->experience; + ret.meseta = this->meseta; return ret; } } __attribute__((packed)); diff --git a/src/Lobby.cc b/src/Lobby.cc index db8eb8a3..09a74198 100644 --- a/src/Lobby.cc +++ b/src/Lobby.cc @@ -7,6 +7,7 @@ #include "Compression.hh" #include "Loggers.hh" #include "SendCommands.hh" +#include "ServerState.hh" #include "Text.hh" using namespace std; @@ -16,7 +17,7 @@ bool Lobby::FloorItem::visible_to_client(uint8_t client_id) const { } Lobby::FloorItemManager::FloorItemManager(uint32_t lobby_id, uint8_t floor) - : log(phosg::string_printf("[Lobby:%08" PRIX32 ":FloorItems:%02hhX] ", lobby_id, floor), lobby_log.min_level), + : log(std::format("[Lobby:{:08X}:FloorItems:{:02X}] ", lobby_id, floor), lobby_log.min_level), next_drop_number(0) {} bool Lobby::FloorItemManager::exists(uint32_t item_id) const { @@ -57,8 +58,8 @@ void Lobby::FloorItemManager::add(shared_ptr 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 " with flags %03hX", - fi->data.id.load(), fi->pos.x.load(), fi->pos.z.load(), fi->drop_number, fi->flags); + this->log.info_f("Added floor item {:08X} at {:g}, {:g} with drop number {} with flags {:03X}", + fi->data.id, fi->pos.x, fi->pos.z, fi->drop_number, fi->flags); } std::shared_ptr Lobby::FloorItemManager::remove(uint32_t item_id, uint8_t client_id) { @@ -76,8 +77,8 @@ std::shared_ptr Lobby::FloorItemManager::remove(uint32_t item_ } } this->items.erase(item_it); - this->log.info("Removed floor item %08" PRIX32 " at %g, %g with drop number %" PRIu64 " with flags %03hX", - fi->data.id.load(), fi->pos.x.load(), fi->pos.z.load(), fi->drop_number, fi->flags); + this->log.info_f("Removed floor item {:08X} at {:g}, {:g} with drop number {} with flags {:03X}", + fi->data.id, fi->pos.x, fi->pos.z, fi->drop_number, fi->flags); return fi; } @@ -88,7 +89,7 @@ std::unordered_set> Lobby::FloorItemManager::e ret.emplace(this->remove(this->queue_for_client[z].begin()->second->data.id, 0xFF)); } } - this->log.info("Evicted %zu items", ret.size()); + this->log.info_f("Evicted {} items", ret.size()); return ret; } @@ -102,7 +103,7 @@ void Lobby::FloorItemManager::clear_inaccessible(uint16_t remaining_clients_mask for (uint32_t item_id : item_ids_to_delete) { this->remove(item_id, 0xFF); } - this->log.info("Deleted %zu inaccessible items", item_ids_to_delete.size()); + this->log.info_f("Deleted {} inaccessible items", item_ids_to_delete.size()); } void Lobby::FloorItemManager::clear_private() { @@ -115,7 +116,7 @@ void Lobby::FloorItemManager::clear_private() { for (uint32_t item_id : item_ids_to_delete) { this->remove(item_id, 0xFF); } - this->log.info("Deleted %zu private items", item_ids_to_delete.size()); + this->log.info_f("Deleted {} private items", item_ids_to_delete.size()); } void Lobby::FloorItemManager::clear() { @@ -125,7 +126,7 @@ void Lobby::FloorItemManager::clear() { queue.clear(); } this->next_drop_number = 0; - this->log.info("Deleted %zu items", num_items); + this->log.info_f("Deleted {} items", num_items); } uint32_t Lobby::FloorItemManager::reassign_all_item_ids(uint32_t next_item_id) { @@ -143,7 +144,7 @@ uint32_t Lobby::FloorItemManager::reassign_all_item_ids(uint32_t next_item_id) { Lobby::Lobby(shared_ptr s, uint32_t id, bool is_game) : server_state(s), - log(phosg::string_printf("[%s:%" PRIX32 "] ", is_game ? "Game" : "Lobby", id), lobby_log.min_level), + log(std::format("[{}:{:X}] ", is_game ? "Game" : "Lobby", id), lobby_log.min_level), lobby_id(id), min_level(0), max_level(0xFFFFFFFF), @@ -157,6 +158,7 @@ Lobby::Lobby(shared_ptr s, uint32_t id, bool is_game) exp_share_multiplier(0.5), challenge_exp_multiplier(1.0f), random_seed(phosg::random_object()), + rand_crypt(make_shared()), drop_mode(DropMode::CLIENT), event(0), block(0), @@ -164,10 +166,8 @@ Lobby::Lobby(shared_ptr s, uint32_t id, bool is_game) max_clients(12), enabled_flags(0), idle_timeout_usecs(0), - idle_timeout_event( - event_new(s->base.get(), -1, EV_TIMEOUT | EV_PERSIST, &Lobby::dispatch_on_idle_timeout, this), - event_free) { - this->log.info("Created"); + idle_timeout_timer(*s->io_context) { + this->log.info_f("Created"); if (is_game) { this->set_flag(Flag::GAME); } @@ -175,7 +175,7 @@ Lobby::Lobby(shared_ptr s, uint32_t id, bool is_game) } Lobby::~Lobby() { - this->log.info("Deleted"); + this->log.info_f("Deleted"); } void Lobby::reset_next_item_ids() { @@ -249,6 +249,12 @@ void Lobby::create_item_creator(Version logic_version) { throw logic_error("invalid lobby base version"); } + shared_ptr rand_crypt; + if (s->use_psov2_rand_crypt) { + rand_crypt = make_shared(this->rand_crypt->seed()); + } else { + rand_crypt = make_shared(this->rand_crypt->seed()); + } this->item_creator = make_shared( common_item_set, rare_item_set, @@ -262,7 +268,7 @@ void Lobby::create_item_creator(Version logic_version) { (this->mode == GameMode::SOLO) ? GameMode::NORMAL : this->mode, this->difficulty, this->effective_section_id(), - this->opt_rand_crypt, + rand_crypt, this->quest ? this->quest->battle_rules : nullptr); } @@ -300,7 +306,7 @@ void Lobby::load_maps() { this->event, this->random_seed, this->rare_enemy_rates, - this->opt_rand_crypt, + this->rand_crypt, this->quest->get_supermap(this->random_seed)); } else { auto s = this->require_server_state(); @@ -310,7 +316,7 @@ void Lobby::load_maps() { this->event, this->random_seed, this->rare_enemy_rates, - this->opt_rand_crypt, + this->rand_crypt, s->supermaps_for_variations(this->episode, this->mode, this->difficulty, this->variations)); } @@ -331,9 +337,9 @@ void Lobby::load_maps() { void Lobby::create_ep3_server() { auto s = this->require_server_state(); if (!this->ep3_server) { - this->log.info("Creating Episode 3 server state"); + this->log.info_f("Creating Episode 3 server state"); } else { - this->log.info("Recreating Episode 3 server state"); + this->log.info_f("Recreating Episode 3 server state"); } auto tourn = this->tournament_match ? this->tournament_match->tournament.lock() : nullptr; @@ -343,7 +349,7 @@ void Lobby::create_ep3_server() { .map_index = s->ep3_map_index, .behavior_flags = s->ep3_behavior_flags, .opt_rand_stream = nullptr, - .opt_rand_crypt = this->opt_rand_crypt, + .rand_crypt = this->rand_crypt, .tournament = tourn, .trap_card_ids = s->ep3_trap_card_ids, }; @@ -376,9 +382,9 @@ bool Lobby::any_client_loading() const { if (!lc.get()) { continue; } - if (lc->config.check_flag(Client::Flag::LOADING) || - lc->config.check_flag(Client::Flag::LOADING_QUEST) || - lc->config.check_flag(Client::Flag::LOADING_RUNNING_JOINABLE_QUEST)) { + if (lc->check_flag(Client::Flag::LOADING) || + lc->check_flag(Client::Flag::LOADING_QUEST) || + lc->check_flag(Client::Flag::LOADING_RUNNING_JOINABLE_QUEST)) { return true; } } @@ -419,7 +425,7 @@ void Lobby::add_client(shared_ptr c, ssize_t required_client_id) { this->clients[required_client_id] = c; index = required_client_id; - } else if (c->config.check_flag(Client::Flag::DEBUG_ENABLED) && (this->mode != GameMode::SOLO)) { + } else if (c->check_flag(Client::Flag::DEBUG_ENABLED) && (this->mode != GameMode::SOLO)) { for (index = this->max_clients - 1; index >= min_client_id; index--) { if (!this->clients[index].get()) { this->clients[index] = c; @@ -480,7 +486,7 @@ void Lobby::add_client(shared_ptr c, ssize_t required_client_id) { // On BB, we send artificial flag state to fix an Episode 2 bug where the // CCA door lock state is overwritten by quests. if (this->is_game() && (c->version() == Version::BB_V4)) { - c->config.set_flag(Client::Flag::SHOULD_SEND_ARTIFICIAL_FLAG_STATE); + c->set_flag(Client::Flag::SHOULD_SEND_ARTIFICIAL_FLAG_STATE); } // If the lobby is recording a battle record, add the player join event @@ -510,17 +516,16 @@ void Lobby::add_client(shared_ptr c, ssize_t required_client_id) { } // There is a player in the lobby, so it is no longer idle - if (event_pending(this->idle_timeout_event.get(), EV_TIMEOUT, nullptr)) { - event_del(this->idle_timeout_event.get()); - this->log.info("Idle timeout cancelled"); + if (this->idle_timeout_timer.cancel()) { + this->log.info_f("Idle timeout cancelled"); } } void Lobby::remove_client(shared_ptr c) { if (this->clients.at(c->lobby_client_id) != c) { auto other_c = this->clients[c->lobby_client_id].get(); - throw logic_error(phosg::string_printf( - "client\'s lobby client id (%hhu) does not match client list (%u)", + throw logic_error(std::format( + "client\'s lobby client id ({}) does not match client list ({})", c->lobby_client_id, static_cast(other_c ? other_c->lobby_client_id : 0xFF))); } @@ -580,9 +585,18 @@ void Lobby::remove_client(shared_ptr c) { (this->idle_timeout_usecs > 0)) { // If the lobby is persistent but has an idle timeout, make it expire after // the specified time - auto tv = phosg::usecs_to_timeval(this->idle_timeout_usecs); - event_add(this->idle_timeout_event.get(), &tv); - this->log.info("Idle timeout scheduled"); + this->idle_timeout_timer.expires_after(std::chrono::microseconds(this->idle_timeout_usecs)); + this->idle_timeout_timer.async_wait([this](std::error_code ec) { + if (!ec) { + if (this->count_clients() == 0) { + this->log.info_f("Idle timeout expired"); + this->require_server_state()->remove_lobby(this->shared_from_this()); + } else { + this->log.error_f("Idle timeout occurred, but clients are present in lobby"); + } + } + }); + this->log.info_f("Idle timeout scheduled"); } } @@ -631,7 +645,7 @@ Lobby::JoinError Lobby::join_error_for_client(std::shared_ptr c, const s if (this->count_clients() >= this->max_clients) { return JoinError::FULL; } - bool debug_enabled = c->config.check_flag(Client::Flag::DEBUG_ENABLED); + bool debug_enabled = c->check_flag(Client::Flag::DEBUG_ENABLED); if (!this->version_is_allowed(c->version()) && !debug_enabled) { return JoinError::VERSION_CONFLICT; } @@ -649,7 +663,7 @@ Lobby::JoinError Lobby::join_error_for_client(std::shared_ptr c, const s return JoinError::SOLO; } if (!debug_enabled && - (this->check_flag(Flag::IS_CLIENT_CUSTOMIZATION) != c->config.check_flag(Client::Flag::IS_CLIENT_CUSTOMIZATION))) { + (this->check_flag(Flag::IS_CLIENT_CUSTOMIZATION) != c->check_flag(Client::Flag::IS_CLIENT_CUSTOMIZATION))) { return JoinError::VERSION_CONFLICT; } if (!c->login->account->check_flag(Account::Flag::FREE_JOIN_GAMES)) { @@ -759,15 +773,15 @@ void Lobby::assign_inventory_and_bank_item_ids(shared_ptr c, bool consum this->next_item_id_for_client[c->lobby_client_id] = orig_next_item_id; } - if (c->log.info("Assigned inventory item IDs%s", consume_ids ? "" : " but did not mark IDs as used")) { + if (c->log.info_f("Assigned inventory item IDs{}", consume_ids ? "" : " but did not mark IDs as used")) { c->print_inventory(stderr); auto& bank = c->current_bank(); if (p->bank.num_items) { bank.assign_ids(0x99000000 + (c->lobby_client_id << 20)); - c->log.info("Assigned bank item IDs"); + c->log.info_f("Assigned bank item IDs"); c->print_bank(stderr); } else { - c->log.info("Bank is empty"); + c->log.info_f("Bank is empty"); } } } @@ -800,18 +814,6 @@ QuestIndex::IncludeCondition Lobby::quest_include_condition() const { }; } -void Lobby::dispatch_on_idle_timeout(evutil_socket_t, short, void* ctx) { - auto l = reinterpret_cast(ctx)->shared_from_this(); - if (l->count_clients() == 0) { - l->log.info("Idle timeout expired"); - auto s = l->require_server_state(); - s->remove_lobby(l); - } else { - l->log.error("Idle timeout occurred, but clients are present in lobby"); - event_del(l->idle_timeout_event.get()); - } -} - bool Lobby::compare_shared(const shared_ptr& a, const shared_ptr& b) { // Sort keys: // 1. Priority class: has free space < empty (persistent) < full < non-joinable (in quest/battle) diff --git a/src/Lobby.hh b/src/Lobby.hh index 7c69cd14..7a1e68c9 100644 --- a/src/Lobby.hh +++ b/src/Lobby.hh @@ -1,6 +1,5 @@ #pragma once -#include #include #include @@ -135,7 +134,7 @@ struct Lobby : public std::enable_shared_from_this { std::string name; // This seed is also sent to the client for rare enemy generation uint32_t random_seed; - std::shared_ptr opt_rand_crypt; + std::shared_ptr rand_crypt; uint8_t allowed_drop_modes; DropMode drop_mode; std::shared_ptr item_creator; // Always null for lobbies, never null for games @@ -185,7 +184,7 @@ struct Lobby : public std::enable_shared_from_this { // This is only used when the PERSISTENT flag is set and idle_timeout_usecs // is not zero uint64_t idle_timeout_usecs; - std::unique_ptr idle_timeout_event; + asio::steady_timer idle_timeout_timer; Lobby(std::shared_ptr s, uint32_t id, bool is_game); Lobby(const Lobby&) = delete; @@ -287,8 +286,6 @@ struct Lobby : public std::enable_shared_from_this { std::unordered_map> clients_by_account_id() const; - static void dispatch_on_idle_timeout(evutil_socket_t, short, void* ctx); - static bool compare_shared(const std::shared_ptr& a, const std::shared_ptr& b); }; diff --git a/src/Loggers.cc b/src/Loggers.cc index f48079e4..e541d139 100644 --- a/src/Loggers.cc +++ b/src/Loggers.cc @@ -4,26 +4,26 @@ using namespace std; -phosg::PrefixedLogger channel_exceptions_log("[Channel] ", phosg::LogLevel::USE_DEFAULT); -phosg::PrefixedLogger client_log("", phosg::LogLevel::USE_DEFAULT); -phosg::PrefixedLogger command_data_log("[Commands] ", phosg::LogLevel::USE_DEFAULT); -phosg::PrefixedLogger config_log("[Config] ", phosg::LogLevel::USE_DEFAULT); -phosg::PrefixedLogger dns_server_log("[DNSServer] ", phosg::LogLevel::USE_DEFAULT); -phosg::PrefixedLogger function_compiler_log("[FunctionCompiler] ", phosg::LogLevel::USE_DEFAULT); -phosg::PrefixedLogger ip_stack_simulator_log("[IPStackSimulator] ", phosg::LogLevel::USE_DEFAULT); -phosg::PrefixedLogger lobby_log("", phosg::LogLevel::USE_DEFAULT); -phosg::PrefixedLogger patch_index_log("[PatchFileIndex] ", phosg::LogLevel::USE_DEFAULT); -phosg::PrefixedLogger player_data_log("", phosg::LogLevel::USE_DEFAULT); -phosg::PrefixedLogger proxy_server_log("[ProxyServer] ", phosg::LogLevel::USE_DEFAULT); -phosg::PrefixedLogger replay_log("[ReplaySession] ", phosg::LogLevel::USE_DEFAULT); -phosg::PrefixedLogger server_log("[Server] ", phosg::LogLevel::USE_DEFAULT); -phosg::PrefixedLogger static_game_data_log("[StaticGameData] ", phosg::LogLevel::USE_DEFAULT); +phosg::PrefixedLogger channel_exceptions_log("[Channel] ", phosg::LogLevel::L_USE_DEFAULT); +phosg::PrefixedLogger client_log("", phosg::LogLevel::L_USE_DEFAULT); +phosg::PrefixedLogger command_data_log("[Commands] ", phosg::LogLevel::L_USE_DEFAULT); +phosg::PrefixedLogger config_log("[Config] ", phosg::LogLevel::L_USE_DEFAULT); +phosg::PrefixedLogger dns_server_log("[DNSServer] ", phosg::LogLevel::L_USE_DEFAULT); +phosg::PrefixedLogger function_compiler_log("[FunctionCompiler] ", phosg::LogLevel::L_USE_DEFAULT); +phosg::PrefixedLogger ip_stack_simulator_log("[IPStackSimulator] ", phosg::LogLevel::L_USE_DEFAULT); +phosg::PrefixedLogger lobby_log("", phosg::LogLevel::L_USE_DEFAULT); +phosg::PrefixedLogger patch_index_log("[PatchFileIndex] ", phosg::LogLevel::L_USE_DEFAULT); +phosg::PrefixedLogger player_data_log("", phosg::LogLevel::L_USE_DEFAULT); +phosg::PrefixedLogger proxy_server_log("[ProxyServer] ", phosg::LogLevel::L_USE_DEFAULT); +phosg::PrefixedLogger replay_log("[ReplaySession] ", phosg::LogLevel::L_USE_DEFAULT); +phosg::PrefixedLogger server_log("[Server] ", phosg::LogLevel::L_USE_DEFAULT); +phosg::PrefixedLogger static_game_data_log("[StaticGameData] ", phosg::LogLevel::L_USE_DEFAULT); static void set_log_level_from_json( phosg::PrefixedLogger& log, const phosg::JSON& d, const char* json_key) { try { string name = phosg::toupper(d.at(json_key).as_string()); - log.min_level = phosg::enum_for_name(name.c_str()); + log.min_level = phosg::enum_for_name(name); } catch (const out_of_range&) { } } diff --git a/src/Main.cc b/src/Main.cc index 28f8d2e4..18fb25fd 100644 --- a/src/Main.cc +++ b/src/Main.cc @@ -1,9 +1,13 @@ -#include -#include -#include +#include + #include #include +#ifndef PHOSG_WINDOWS +#include +#endif +#include +#include #include #include #include @@ -19,12 +23,12 @@ #include "AddressTranslator.hh" #include "BMLArchive.hh" -#include "CatSession.hh" #include "Compression.hh" #include "DCSerialNumbers.hh" #include "DNSServer.hh" #include "DownloadSession.hh" #include "GSLArchive.hh" +#include "GameServer.hh" #include "HTTPServer.hh" #include "IPStackSimulator.hh" #include "ImageEncoder.hh" @@ -33,8 +37,6 @@ #include "PPKArchive.hh" #include "PSOGCObjectGraph.hh" #include "PSOProtocol.hh" -#include "PatchServer.hh" -#include "ProxyServer.hh" #include "Quest.hh" #include "QuestScript.hh" #include "ReplaySession.hh" @@ -76,32 +78,34 @@ vector parse_int_vector(const phosg::JSON& o) { return ret; } +#ifndef PHOSG_WINDOWS void drop_privileges(const string& username) { if ((getuid() != 0) || (getgid() != 0)) { - throw runtime_error(phosg::string_printf( - "newserv was not started as root; can\'t switch to user %s", - username.c_str())); + throw runtime_error(std::format( + "newserv was not started as root; can\'t switch to user {}", + username)); } struct passwd* pw = getpwnam(username.c_str()); if (!pw) { string error = phosg::string_for_error(errno); - throw runtime_error(phosg::string_printf("user %s not found (%s)", - username.c_str(), error.c_str())); + throw runtime_error(std::format("user {} not found ({})", + username, error)); } if (setgid(pw->pw_gid) != 0) { string error = phosg::string_for_error(errno); - throw runtime_error(phosg::string_printf("can\'t switch to group %d (%s)", - pw->pw_gid, error.c_str())); + throw runtime_error(std::format("can\'t switch to group {} ({})", + pw->pw_gid, error)); } if (setuid(pw->pw_uid) != 0) { string error = phosg::string_for_error(errno); - throw runtime_error(phosg::string_printf("can\'t switch to user %d (%s)", - pw->pw_uid, error.c_str())); + throw runtime_error(std::format("can\'t switch to user {} ({})", + pw->pw_uid, error)); } - config_log.info("Switched to user %s (%d:%d)", username.c_str(), pw->pw_uid, pw->pw_gid); + config_log.info_f("Switched to user {} ({}:{})", username, pw->pw_uid, pw->pw_gid); } +#endif Version get_cli_version(phosg::Arguments& args, Version default_value = Version::UNKNOWN) { if (args.get("pc-patch")) { @@ -246,7 +250,7 @@ struct Action { run(run) { auto emplace_ret = all_actions.emplace(this->name, this); if (!emplace_ret.second) { - throw logic_error(phosg::string_printf("multiple actions with the same name: %s", this->name)); + throw logic_error(std::format("multiple actions with the same name: {}", this->name)); } action_order.emplace_back(this); } @@ -270,11 +274,11 @@ Action a_version( static void a_compress_decompress_fn(phosg::Arguments& args) { const auto& action = args.get(0); - bool is_prs = phosg::ends_with(action, "-prs"); - bool is_bc0 = phosg::ends_with(action, "-bc0"); - bool is_pr2 = phosg::ends_with(action, "-pr2"); - bool is_prc = phosg::ends_with(action, "-prc"); - bool is_decompress = phosg::starts_with(action, "decompress-"); + bool is_prs = action.ends_with("-prs"); + bool is_bc0 = action.ends_with("-bc0"); + bool is_pr2 = action.ends_with("-pr2"); + bool is_prc = action.ends_with("-prc"); + bool is_decompress = action.starts_with("decompress-"); bool is_big_endian = args.get("big-endian"); bool is_optimal = args.get("optimal"); bool is_pessimal = args.get("pessimal"); @@ -295,14 +299,14 @@ static void a_compress_decompress_fn(phosg::Arguments& args) { auto progress_fn = [&](auto, size_t input_progress, size_t, size_t output_progress) -> void { float progress = static_cast(input_progress * 100) / input_bytes; float size_ratio = static_cast(output_progress * 100) / input_progress; - fprintf(stderr, "... %zu/%zu (%g%%) => %zu (%g%%) \r", + phosg::fwrite_fmt(stderr, "... {}/{} ({:g}%) => {} ({:g}%) \r", input_progress, input_bytes, progress, output_progress, size_ratio); }; auto optimal_progress_fn = [&](auto phase, size_t input_progress, size_t input_bytes, size_t output_progress) -> void { const char* phase_name = phosg::name_for_enum(phase); float progress = static_cast(input_progress * 100) / input_bytes; float size_ratio = static_cast(output_progress * 100) / input_progress; - fprintf(stderr, "... [%s] %zu/%zu (%g%%) => %zu (%g%%) \r", + phosg::fwrite_fmt(stderr, "... [{}] {}/{} ({:g}%) => {} ({:g}%) \r", phase_name, input_progress, input_bytes, progress, output_progress, size_ratio); }; @@ -336,12 +340,12 @@ static void a_compress_decompress_fn(phosg::Arguments& args) { float size_ratio = static_cast(data.size() * 100) / input_bytes; double bytes_per_sec = input_bytes / (static_cast(end - start) / 1000000.0); string bytes_per_sec_str = phosg::format_size(bytes_per_sec); - phosg::log_info("%zu (0x%zX) bytes input => %zu (0x%zX) bytes output (%g%%) in %s (%s / sec)", - input_bytes, input_bytes, data.size(), data.size(), size_ratio, time_str.c_str(), bytes_per_sec_str.c_str()); + phosg::log_info_f("{} (0x{:X}) bytes input => {} (0x{:X}) bytes output ({:g}%) in {} ({} / sec)", + input_bytes, input_bytes, data.size(), data.size(), size_ratio, time_str, bytes_per_sec_str); if (is_pr2 || is_prc) { if (is_decompress && (data.size() != pr2_expected_size)) { - phosg::log_warning("Result data size (%zu bytes) does not match expected size from PR2 header (%zu bytes)", data.size(), pr2_expected_size); + phosg::log_warning_f("Result data size ({} bytes) does not match expected size from PR2 header ({} bytes)", data.size(), pr2_expected_size); } else if (!is_decompress) { uint32_t pr2_seed = seed.empty() ? phosg::random_object() : stoul(seed, nullptr, 16); data = is_big_endian @@ -408,7 +412,7 @@ Action a_prs_size( string data = read_input_data(args); size_t input_bytes = data.size(); size_t output_bytes = prs_decompress_size(data); - phosg::log_info("%zu (0x%zX) bytes input => %zu (0x%zX) bytes output", + phosg::log_info_f("{} (0x{:X}) bytes input => {} (0x{:X}) bytes output", input_bytes, input_bytes, output_bytes, output_bytes); }); @@ -535,7 +539,7 @@ static void a_encrypt_decrypt_trivial_fn(phosg::Arguments& args) { best_seed_score = score; } } - fprintf(stderr, "Basis appears to be %02hhX (%zu zero bytes in output)\n", + phosg::fwrite_fmt(stderr, "Basis appears to be {:02X} ({} zero bytes in output)\n", best_seed, best_seed_score); basis = best_seed; } else { @@ -572,7 +576,7 @@ Action a_parse_pc_v2_registry( exported from the HKEY_CURRENT_USER\\Software\\SonicTeam\\PSOV2 key.\n", +[](phosg::Arguments& args) { string data = read_input_data(args); - if (phosg::starts_with(data, "\xFF\xFE")) { + if (data.starts_with("\xFF\xFE")) { data = tt_utf16_to_utf8(data.substr(2)); } data = phosg::str_replace_all(data, "\r", ""); @@ -581,15 +585,15 @@ Action a_parse_pc_v2_registry( bool in_psov2_section = false; string serial_data, access_data, email_data; for (string line : phosg::split(data, '\n')) { - if (phosg::starts_with(line, "[")) { + if (line.starts_with("[")) { in_psov2_section = (line == "[HKEY_CURRENT_USER\\Software\\SonicTeam\\PSOV2]"); } else if (!in_psov2_section) { // Wrong section; skip the line - } else if (phosg::starts_with(line, "\"SERIAL\"=hex:")) { + } else if (line.starts_with("\"SERIAL\"=hex:")) { serial_data = phosg::parse_data_string(line.substr(13)); - } else if (phosg::starts_with(line, "\"ACCESS\"=hex:")) { + } else if (line.starts_with("\"ACCESS\"=hex:")) { access_data = phosg::parse_data_string(line.substr(13)); - } else if (phosg::starts_with(line, "\"E-MAIL\"=hex:")) { + } else if (line.starts_with("\"E-MAIL\"=hex:")) { email_data = phosg::parse_data_string(line.substr(13)); } } @@ -610,8 +614,8 @@ Action a_parse_pc_v2_registry( uint32_t serial_number = stoul(serial_data, nullptr, 16); phosg::strip_trailing_zeroes(access_data); phosg::strip_trailing_zeroes(email_data); - fprintf(stderr, "Serial number (decimal): %" PRIu32 "\nSerial number (hex): %08" PRIX32 "\nAccess key: %s\nEmail address: %s\n", - serial_number, serial_number, access_data.c_str(), email_data.c_str()); + phosg::fwrite_fmt(stderr, "Serial number (decimal): {}\nSerial number (hex): {:08X}\nAccess key: {}\nEmail address: {}\n", + serial_number, serial_number, access_data, email_data); }); Action a_generate_pc_v2_registry( @@ -627,9 +631,9 @@ Action a_generate_pc_v2_registry( if (data.size() == 0) { return string(); } - string ret = phosg::string_printf("%02hx", data[0]); + string ret = std::format("{:02x}", data[0]); for (size_t z = 1; z < data.size(); z++) { - ret += phosg::string_printf(",%02hx", data[z]); + ret += std::format(",{:02x}", data[z]); } return ret; }; @@ -645,7 +649,7 @@ Action a_generate_pc_v2_registry( } email.resize(0x40, '\0'); - string serial_data = decrypt_v2_registry_value(phosg::string_printf("%08" PRIX32, serial_number)); + string serial_data = decrypt_v2_registry_value(std::format("{:08X}", serial_number)); string access_data = decrypt_v2_registry_value(access_key); string email_data = decrypt_v2_registry_value(email); @@ -653,8 +657,8 @@ Action a_generate_pc_v2_registry( string access_hex = hex_str_for_data(access_data); string email_hex = hex_str_for_data(email_data); - string output_data = phosg::string_printf("Windows Registry Editor Version 5.00\r\n\r\n[HKEY_CURRENT_USER\\Software\\SonicTeam\\PSOV2]\r\n\r\n\"SERIAL\"=hex:%s\r\n\"ACCESS\"=hex:%s\r\n\"E-MAIL\"=hex:%s\r\n", - serial_hex.c_str(), access_hex.c_str(), email_hex.c_str()); + string output_data = std::format("Windows Registry Editor Version 5.00\r\n\r\n[HKEY_CURRENT_USER\\Software\\SonicTeam\\PSOV2]\r\n\r\n\"SERIAL\"=hex:{}\r\n\"ACCESS\"=hex:{}\r\n\"E-MAIL\"=hex:{}\r\n", + serial_hex, access_hex, email_hex); write_output_data(args, output_data.data(), output_data.size(), "reg"); }); @@ -726,7 +730,7 @@ static void a_encrypt_decrypt_vms_save_fn(phosg::Arguments& args) { iter.complete = true; } lock_guard g(output_lock); - fprintf(stderr, "\nFound serial number: %08" PRIX32 "\n", serial_number); + phosg::fwrite_fmt(stderr, "\nFound serial number: {:08X}\n", serial_number); *reinterpret_cast(data.data() + data_start_offset) = decrypted; } catch (const runtime_error&) { @@ -744,7 +748,7 @@ static void a_encrypt_decrypt_vms_save_fn(phosg::Arguments& args) { size_t progress = iter.progress(); size_t total_count = iter.total_count(); float progress_percent = static_cast(progress * 100) / total_count; - fprintf(stderr, "... %zu/%zu (%g%%, domain %02hhX, subdomain %02hhX, index2 %04hX, index3 %04hX)\r", + phosg::fwrite_fmt(stderr, "... {}/{} ({:g}%, domain {:02X}, subdomain {:02X}, index2 {:04X}, index3 {:04X})\r", progress, total_count, progress_percent, iter.domain, iter.subdomain, iter.index2, iter.index3); if (iter.complete) { break; @@ -764,7 +768,7 @@ static void a_encrypt_decrypt_vms_save_fn(phosg::Arguments& args) { data_section, sizeof(StructT), serial_number, skip_checksum, override_round2_seed); lock_guard g(output_lock); - fprintf(stderr, "\nFound serial number: %08" PRIX64 "\n", serial_number); + phosg::fwrite_fmt(stderr, "\nFound serial number: {:08X}\n", serial_number); *reinterpret_cast(data.data() + data_start_offset) = decrypted; return true; @@ -796,32 +800,32 @@ static void a_encrypt_decrypt_vms_save_fn(phosg::Arguments& args) { bool is_v2 = header.is_v2(); if (!is_v2 && (header.data_size == sizeof(PSODCNTECharacterFile))) { - fprintf(stderr, "File type: DC NTE character\n"); + phosg::fwrite_fmt(stderr, "File type: DC NTE character\n"); process_file.template operator()(); } else if (!is_v2 && (header.data_size == sizeof(PSODCNTEGuildCardFile))) { - fprintf(stderr, "File type: DC NTE Guild Card list\n"); + phosg::fwrite_fmt(stderr, "File type: DC NTE Guild Card list\n"); throw runtime_error("DC NTE Guild Card files are not encrypted"); } else if (!is_v2 && (header.data_size == sizeof(PSODC112000CharacterFile))) { - fprintf(stderr, "File type: DC 11/2000 character\n"); + phosg::fwrite_fmt(stderr, "File type: DC 11/2000 character\n"); process_file.template operator()(); } else if (!is_v2 && (header.data_size == sizeof(PSODC112000GuildCardFile))) { - fprintf(stderr, "File type: DC 11/2000 Guild Card list\n"); + phosg::fwrite_fmt(stderr, "File type: DC 11/2000 Guild Card list\n"); throw runtime_error("DC 11/2000 Guild Card files are not encrypted"); } else if (!is_v2 && (header.data_size == sizeof(PSODCV1CharacterFile))) { - fprintf(stderr, "File type: DC v1 character\n"); + phosg::fwrite_fmt(stderr, "File type: DC v1 character\n"); process_file.template operator()(); } else if (is_v2 && (header.data_size == sizeof(PSODCV2CharacterFile))) { - fprintf(stderr, "File type: DC v2 character\n"); + phosg::fwrite_fmt(stderr, "File type: DC v2 character\n"); process_file.template operator()(); } else if (header.data_size == sizeof(PSODCV1V2GuildCardFile)) { // There appears to be a copy/paste error here: the game uses the character // file size when checksumming the Guild Card file, so we must do the same if (!is_v2) { - fprintf(stderr, "File type: DC v1 Guild Card list\n"); + phosg::fwrite_fmt(stderr, "File type: DC v1 Guild Card list\n"); static_assert(sizeof(PSODCV1CharacterFile) <= sizeof(PSODCV1V2GuildCardFile::EncryptedSection)); process_file.template operator()(); } else { - fprintf(stderr, "File type: DC v2 Guild Card list\n"); + phosg::fwrite_fmt(stderr, "File type: DC v2 Guild Card list\n"); static_assert(sizeof(PSODCV2CharacterFile) <= sizeof(PSODCV1V2GuildCardFile::EncryptedSection)); process_file.template operator()(); } @@ -951,7 +955,7 @@ static void a_encrypt_decrypt_pc_save_fn(phosg::Arguments& args) { charfile->entries[z].encrypted = decrypt_fixed_size_data_section_t( &charfile->entries[z].encrypted, sizeof(charfile->entries[z].encrypted), round1_seed, skip_checksum, override_round2_seed); } catch (const exception& e) { - fprintf(stderr, "warning: cannot decrypt character %zu: %s\n", z, e.what()); + phosg::fwrite_fmt(stderr, "warning: cannot decrypt character {}: {}\n", z, e.what()); } } } @@ -1089,11 +1093,11 @@ Action a_decode_gci_snapshot( try { header.check(); } catch (const exception& e) { - phosg::log_warning("File header failed validation (%s)", e.what()); + phosg::log_warning_f("File header failed validation ({})", e.what()); } const auto& file = r.get(); if (!file.checksum_correct()) { - phosg::log_warning("File internal checksum is incorrect"); + phosg::log_warning_f("File internal checksum is incorrect"); } auto img = file.decode_image(); @@ -1173,13 +1177,13 @@ Action a_salvage_gci( header.check(); const auto& system = r.get(); likely_round1_seed = system.creation_timestamp; - phosg::log_info("System file appears to be in order; round1 seed is %08" PRIX64, likely_round1_seed); + phosg::log_info_f("System file appears to be in order; round1 seed is {:08X}", likely_round1_seed); } catch (const exception& e) { - phosg::log_warning("Cannot parse system file (%s); ignoring it", e.what()); + phosg::log_warning_f("Cannot parse system file ({}); ignoring it", e.what()); } } else if (!seed.empty()) { likely_round1_seed = stoul(seed, nullptr, 16); - phosg::log_info("Specified round1 seed is %08" PRIX64, likely_round1_seed); + phosg::log_info_f("Specified round1 seed is {:08X}", likely_round1_seed); } if (round2 && likely_round1_seed > 0x100000000) { @@ -1226,7 +1230,7 @@ Action a_salvage_gci( const char* sys_seed_str = (!round2 && (it.second == likely_round1_seed)) ? " (this is the seed from the system file)" : ""; - phosg::log_info("Round %c seed %08" PRIX32 " resulted in %zu zero bytes%s", + phosg::log_info_f("Round {} seed {:08X} resulted in {} zero bytes{}", round2 ? '2' : '1', it.second, it.first, sys_seed_str); } }; @@ -1272,7 +1276,7 @@ Action a_salvage_gci( } print_top_seeds(intermediate_top_seeds); round2_lower_half = intermediate_top_seeds.rbegin()->second & 0xFFFF; - phosg::log_info("Lower half of seed is likely %04" PRIX32 " (%zu zero bytes)", round2_lower_half, intermediate_top_seeds.rbegin()->first); + phosg::log_info_f("Lower half of seed is likely {:04X} ({} zero bytes)", round2_lower_half, intermediate_top_seeds.rbegin()->first); for (auto& top_seeds : top_seeds_by_thread) { top_seeds.clear(); } @@ -1386,9 +1390,9 @@ Action a_find_decryption_seed( 0, 0x100000000, 0x1000, num_threads); if (seed < 0x100000000) { - phosg::log_info("Found seed %08" PRIX64, seed); + phosg::log_info_f("Found seed {:08X}", seed); } else { - phosg::log_error("No seed found"); + phosg::log_error_f("No seed found"); } }); @@ -1474,10 +1478,10 @@ Action a_encode_qst( bool download = args.get("download"); string bin_filename = input_filename; - string dat_filename = phosg::ends_with(bin_filename, ".bin") + string dat_filename = bin_filename.ends_with(".bin") ? (bin_filename.substr(0, bin_filename.size() - 3) + "dat") : (bin_filename + ".dat"); - string pvr_filename = phosg::ends_with(bin_filename, ".bin") + string pvr_filename = bin_filename.ends_with(".bin") ? (bin_filename.substr(0, bin_filename.size() - 3) + "pvr") : (bin_filename + ".pvr"); auto bin_data = make_shared(phosg::load_file(bin_filename)); @@ -1556,9 +1560,9 @@ Action a_disassemble_free_map( +[](phosg::Arguments& args) { const string& input_filename = args.get(1, true); string input_filename_lower = phosg::tolower(input_filename); - bool is_events = phosg::ends_with(input_filename_lower, ".evt"); - bool is_enemies = phosg::ends_with(input_filename_lower, "e.dat") || phosg::ends_with(input_filename_lower, "e_s.dat") || phosg::ends_with(input_filename_lower, "e_c1.dat") || phosg::ends_with(input_filename_lower, "e_d.dat"); - bool is_objects = phosg::ends_with(input_filename_lower, "o.dat") || phosg::ends_with(input_filename_lower, "o_s.dat") || phosg::ends_with(input_filename_lower, "o_c1.dat") || phosg::ends_with(input_filename_lower, "o_d.dat"); + bool is_events = input_filename_lower.ends_with(".evt"); + bool is_enemies = input_filename_lower.ends_with("e.dat") || input_filename_lower.ends_with("e_s.dat") || input_filename_lower.ends_with("e_c1.dat") || input_filename_lower.ends_with("e_d.dat"); + bool is_objects = input_filename_lower.ends_with("o.dat") || input_filename_lower.ends_with("o_s.dat") || input_filename_lower.ends_with("o_c1.dat") || input_filename_lower.ends_with("o_d.dat"); if (!is_objects && !is_enemies && !is_events) { throw runtime_error("cannot determine input file type"); } @@ -1647,7 +1651,7 @@ Action a_assemble_all_patches( w.write(data); string out_path = code->source_path + (encrypted ? ".enc.bin" : ".std.bin"); phosg::save_file(out_path, w.str()); - fprintf(stderr, "... %s\n", out_path.c_str()); + phosg::fwrite_fmt(stderr, "... {}\n", out_path); } }; @@ -1687,18 +1691,19 @@ Action a_generate_gsl( string output_filename = args.get(2, false); if (output_filename.empty()) { output_filename = input_directory; - while (phosg::ends_with(output_filename, "/")) { + while (output_filename.ends_with("/")) { output_filename.pop_back(); } output_filename += ".gsl"; } unordered_map files; - for (const auto& filename : phosg::list_directory(input_directory)) { + for (const auto& item : std::filesystem::directory_iterator(input_directory)) { + string filename = item.path().filename().string(); string file_path = input_directory + "/" + filename; - if (!phosg::isfile(file_path)) { - throw std::runtime_error(phosg::string_printf( - "input directory contains %s which is not a file", filename.c_str())); + if (!std::filesystem::is_regular_file(file_path)) { + throw std::runtime_error(std::format( + "input directory contains {} which is not a file", filename)); } files.emplace(filename, phosg::load_file(file_path)); } @@ -1726,9 +1731,9 @@ void a_extract_archive_fn(phosg::Arguments& args) { const auto& all_entries = arch.all_entries(); for (size_t z = 0; z < all_entries.size(); z++) { auto e = arch.get(z); - string out_file = phosg::string_printf("%s-%zu", output_prefix.c_str(), z); - phosg::save_file(out_file.c_str(), e.first, e.second); - fprintf(stderr, "... %s\n", out_file.c_str()); + string out_file = std::format("{}-{}", output_prefix, z); + phosg::save_file(out_file, e.first, e.second); + phosg::fwrite_fmt(stderr, "... {}\n", out_file); } } else if (args.get(0) == "extract-gsl") { GSLArchive arch(data_shared, args.get("big-endian")); @@ -1736,7 +1741,7 @@ void a_extract_archive_fn(phosg::Arguments& args) { auto e = arch.get(entry_it.first); string out_file = output_prefix + entry_it.first; phosg::save_file(out_file, e.first, e.second); - fprintf(stderr, "... %s\n", out_file.c_str()); + phosg::fwrite_fmt(stderr, "... {}\n", out_file); } } else if (args.get(0) == "extract-bml") { BMLArchive arch(data_shared, args.get("big-endian")); @@ -1746,7 +1751,7 @@ void a_extract_archive_fn(phosg::Arguments& args) { string data = prs_decompress(e.first, e.second); string out_file = output_prefix + entry_it.first; phosg::save_file(out_file, data); - fprintf(stderr, "... %s\n", out_file.c_str()); + phosg::fwrite_fmt(stderr, "... {}\n", out_file); } auto gvm_e = arch.get_gvm(entry_it.first); @@ -1754,7 +1759,7 @@ void a_extract_archive_fn(phosg::Arguments& args) { string data = prs_decompress(gvm_e.first, gvm_e.second); string out_file = output_prefix + entry_it.first + ".gvm"; phosg::save_file(out_file, data); - fprintf(stderr, "... %s\n", out_file.c_str()); + phosg::fwrite_fmt(stderr, "... {}\n", out_file); } } } else if (args.get(0) == "extract-ppk") { @@ -1762,7 +1767,7 @@ void a_extract_archive_fn(phosg::Arguments& args) { for (const auto& [filename, data] : files) { string out_file = output_prefix + filename; phosg::save_file(out_file, data); - fprintf(stderr, "... %s\n", out_file.c_str()); + phosg::fwrite_fmt(stderr, "... {}\n", out_file); } } else { throw logic_error("unimplemented archive type"); @@ -1802,8 +1807,9 @@ Action a_decode_text_archive( "decode-text-archive", "\ decode-text-archive [INPUT-FILENAME [OUTPUT-FILENAME]]\n\ Decode a text archive to JSON. --collections=NUM_COLLECTIONS is given,\n\ - expects a fixed number of collections in the input. If --has-pr3 is given,\n\ - expects the input not to have a REL footer.\n", + expects a fixed number of collections in the input (this is needed for DC\n\ + NTE and 11/2000). If --has-pr3 is given, expects the input not to have a\n\ + REL footer.\n", +[](phosg::Arguments& args) { string data = read_input_data(args); bool is_sjis = args.get("japanese"); @@ -1835,13 +1841,13 @@ Action a_encode_text_archive( if (input_filename.empty() || (input_filename == "-")) { throw runtime_error("encoded text archive cannot be written to stdout"); } - phosg::save_file(phosg::string_printf("%s.pr2", input_filename.c_str()), result.first); - phosg::save_file(phosg::string_printf("%s.pr3", input_filename.c_str()), result.second); + phosg::save_file(std::format("{}.pr2", input_filename), result.first); + phosg::save_file(std::format("{}.pr3", input_filename), result.second); } else if (output_filename == "-") { throw runtime_error("encoded text archive cannot be written to stdout"); } else { string out_filename = output_filename; - if (phosg::ends_with(out_filename, ".pr2")) { + if (out_filename.ends_with(".pr2")) { phosg::save_file(out_filename, result.first); out_filename[out_filename.size() - 1] = '3'; phosg::save_file(out_filename, result.second); @@ -1886,9 +1892,9 @@ Action a_decode_word_select_set( const vector* unitxt_collection; if (!unitxt_filename.empty()) { unique_ptr uts; - if (phosg::ends_with(unitxt_filename, ".prs")) { + if (unitxt_filename.ends_with(".prs")) { uts = make_unique(phosg::load_file(unitxt_filename)); - } else if (phosg::ends_with(unitxt_filename, ".json")) { + } else if (unitxt_filename.ends_with(".json")) { uts = make_unique(phosg::JSON::parse(phosg::load_file(unitxt_filename))); } else { throw runtime_error("unitxt filename must end in .prs or .json"); @@ -1909,9 +1915,9 @@ Action a_print_word_select_table( option is given, prints the token table sorted by canonical name.\n", +[](phosg::Arguments& args) { auto s = make_shared(get_config_filename(args)); - s->load_patch_indexes(false); - s->load_text_index(false); - s->load_word_select_table(false); + s->load_patch_indexes(); + s->load_text_index(); + s->load_word_select_table(); Version v; try { v = get_cli_version(args); @@ -1925,33 +1931,6 @@ Action a_print_word_select_table( } }); -Action a_cat_client( - "cat-client", "\ - cat-client ADDR:PORT\n\ - Connect to the given server and simulate a PSO client. newserv will then\n\ - print all the received commands to stdout, and forward any commands typed\n\ - into stdin to the remote server. It is assumed that the input and output\n\ - are terminals, so all commands are hex-encoded. The --patch, --dc, --pc,\n\ - --gc, and --bb options can be used to select the command format and\n\ - encryption. If --bb is used, the --key=KEY-NAME option is also required (as\n\ - in decrypt-data above).\n", - +[](phosg::Arguments& args) { - auto version = get_cli_version(args); - shared_ptr key; - if (uses_v4_encryption(version)) { - string key_file_name = args.get("key"); - if (key_file_name.empty()) { - throw runtime_error("a key filename is required for BB client emulation"); - } - key = make_shared( - phosg::load_object_file("system/blueburst/keys/" + key_file_name + ".nsk")); - } - shared_ptr base(event_base_new(), event_base_free); - auto cat_client_remote = phosg::make_sockaddr_storage(phosg::parse_netloc(args.get(1))).first; - CatSession session(base, cat_client_remote, get_cli_version(args), key); - event_base_dispatch(base.get()); - }); - Action a_download_files( "download-files", nullptr, +[](phosg::Arguments& args) { @@ -1965,8 +1944,7 @@ Action a_download_files( key = make_shared( phosg::load_object_file("system/blueburst/keys/" + key_file_name + ".nsk")); } - shared_ptr base(event_base_new(), event_base_free); - auto remote = phosg::make_sockaddr_storage(phosg::parse_netloc(args.get(1))).first; + auto [remote_host, remote_port] = phosg::parse_netloc(args.get(1)); auto character = PSOCHARFile::load_shared(args.get("character", true), false).character_file; auto ship_menu_selections_str = args.get("ship-menu-selections", false); @@ -1989,9 +1967,11 @@ Action a_download_files( "serial-number", 0, is_v1_or_v2(version) ? phosg::Arguments::IntFormat::HEX : phosg::Arguments::IntFormat::DEFAULT); + auto io_context = make_shared(); DownloadSession session( - base, - remote, + io_context, + remote_host, + remote_port, args.get("output-dir", true), version, args.get("language"), @@ -2009,7 +1989,7 @@ Action a_download_files( on_request_complete_commands, args.get("interactive"), args.get("show-command-data")); - event_base_dispatch(base.get()); + io_context->run(); }); Action a_convert_rare_item_set( @@ -2031,10 +2011,10 @@ Action a_convert_rare_item_set( double rate_factor = args.get("multiply", 1.0); auto s = make_shared(get_config_filename(args)); s->load_config_early(); - s->load_patch_indexes(false); - s->load_text_index(false); - s->load_item_definitions(false); - s->load_item_name_indexes(false); + s->load_patch_indexes(); + s->load_text_index(); + s->load_item_definitions(); + s->load_item_name_indexes(); string input_filename = args.get(1, false); if (input_filename.empty() || (input_filename == "-")) { @@ -2044,15 +2024,15 @@ Action a_convert_rare_item_set( string input_filename_lower = phosg::tolower(input_filename); auto data = make_shared(read_input_data(args)); shared_ptr rs; - if (phosg::ends_with(input_filename_lower, ".json")) { + if (input_filename_lower.ends_with(".json")) { rs = make_shared(phosg::JSON::parse(*data), s->item_name_index_opt(get_cli_version(args, Version::BB_V4))); - } else if (phosg::ends_with(input_filename_lower, ".gsl")) { + } else if (input_filename_lower.ends_with(".gsl")) { rs = make_shared(GSLArchive(data, false), false); - } else if (phosg::ends_with(input_filename_lower, ".gslb")) { + } else if (input_filename_lower.ends_with(".gslb")) { rs = make_shared(GSLArchive(data, true), true); - } else if (phosg::ends_with(input_filename_lower, ".afs")) { + } else if (input_filename_lower.ends_with(".afs")) { rs = make_shared(AFSArchive(data), is_v1(get_cli_version(args, Version::DC_V2))); - } else if (phosg::ends_with(input_filename_lower, ".rel")) { + } else if (input_filename_lower.ends_with(".rel")) { rs = make_shared(*data, true); } else { throw runtime_error("cannot determine input format; use a filename ending with .json, .gsl, .gslb, .afs, or .rel"); @@ -2066,21 +2046,21 @@ Action a_convert_rare_item_set( string output_filename_lower = phosg::tolower(output_filename); if (output_filename.empty() || (output_filename == "-")) { rs->print_all_collections(stdout, s->item_name_index_opt(get_cli_version(args, Version::BB_V4))); - } else if (phosg::ends_with(output_filename_lower, ".json")) { + } else if (output_filename_lower.ends_with(".json")) { auto json = rs->json(s->item_name_index_opt(get_cli_version(args, Version::BB_V4))); string data = json.serialize(phosg::JSON::SerializeOption::FORMAT | phosg::JSON::SerializeOption::HEX_INTEGERS | phosg::JSON::SerializeOption::SORT_DICT_KEYS); write_output_data(args, data.data(), data.size(), nullptr); - } else if (phosg::ends_with(output_filename_lower, ".gsl")) { + } else if (output_filename_lower.ends_with(".gsl")) { string data = rs->serialize_gsl(args.get("big-endian")); write_output_data(args, data.data(), data.size(), nullptr); - } else if (phosg::ends_with(output_filename_lower, ".gslb")) { + } else if (output_filename_lower.ends_with(".gslb")) { string data = rs->serialize_gsl(true); write_output_data(args, data.data(), data.size(), nullptr); - } else if (phosg::ends_with(output_filename_lower, ".afs")) { + } else if (output_filename_lower.ends_with(".afs")) { bool is_v1 = ::is_v1(get_cli_version(args, Version::DC_V2)); string data = rs->serialize_afs(is_v1); write_output_data(args, data.data(), data.size(), nullptr); - } else if (phosg::ends_with(output_filename_lower, ".html")) { + } else if (output_filename_lower.ends_with(".html")) { bool is_v1 = ::is_v1(get_cli_version(args, Version::BB_V4)); static const array modes = {GameMode::NORMAL, GameMode::BATTLE, GameMode::CHALLENGE, GameMode::SOLO}; for (GameMode mode : modes) { @@ -2094,7 +2074,7 @@ Action a_convert_rare_item_set( string data = rs->serialize_html(mode, episode, difficulty, item_name_index); string out_filename = output_filename.substr(0, output_filename.size() - 5) + "." + name_for_mode(mode) + "." + abbreviation_for_episode(episode) + "." + abbreviation_for_difficulty(difficulty) + output_filename.substr(output_filename.size() - 5); phosg::save_file(out_filename, data); - phosg::log_info("... %s", out_filename.c_str()); + phosg::log_info_f("... {}", out_filename); } } } @@ -2119,11 +2099,11 @@ Action a_convert_common_item_set( auto data = make_shared(read_input_data(args)); shared_ptr cs; - if (phosg::ends_with(input_filename, ".json")) { + if (input_filename.ends_with(".json")) { cs = make_shared(phosg::JSON::parse(*data)); - } else if (phosg::ends_with(input_filename, ".gsl")) { + } else if (input_filename.ends_with(".gsl")) { cs = make_shared(data, args.get("big-endian")); - } else if (phosg::ends_with(input_filename, ".gslb")) { + } else if (input_filename.ends_with(".gslb")) { cs = make_shared(data, true); } else { throw runtime_error("cannot determine input format; use a filename ending with .json, .gsl, .gslb, or .afs"); @@ -2151,10 +2131,10 @@ Action a_describe_item( auto s = make_shared(get_config_filename(args)); s->load_config_early(); - s->load_patch_indexes(false); - s->load_text_index(false); - s->load_item_definitions(false); - s->load_item_name_indexes(false); + s->load_patch_indexes(); + s->load_text_index(); + s->load_item_definitions(); + s->load_item_name_indexes(); auto name_index = s->item_name_index(version); ItemData item = name_index->parse_item_description(description); @@ -2166,7 +2146,7 @@ Action a_describe_item( string desc = name_index->describe_item(item); string desc_colored = name_index->describe_item(item, true); - phosg::log_info("Data (decoded): %02hhX%02hhX%02hhX%02hhX %02hhX%02hhX%02hhX%02hhX %02hhX%02hhX%02hhX%02hhX -------- %02hhX%02hhX%02hhX%02hhX", + phosg::log_info_f("Data (decoded): {:02X}{:02X}{:02X}{:02X} {:02X}{:02X}{:02X}{:02X} {:02X}{:02X}{:02X}{:02X} -------- {:02X}{:02X}{:02X}{:02X}", item.data1[0], item.data1[1], item.data1[2], item.data1[3], item.data1[4], item.data1[5], item.data1[6], item.data1[7], item.data1[8], item.data1[9], item.data1[10], item.data1[11], @@ -2177,14 +2157,14 @@ Action a_describe_item( ItemData item_v2_decoded = item_v2; item_v2_decoded.decode_for_version(Version::PC_V2); - phosg::log_info("Data (V2-encoded): %02hhX%02hhX%02hhX%02hhX %02hhX%02hhX%02hhX%02hhX %02hhX%02hhX%02hhX%02hhX -------- %02hhX%02hhX%02hhX%02hhX", + phosg::log_info_f("Data (V2-encoded): {:02X}{:02X}{:02X}{:02X} {:02X}{:02X}{:02X}{:02X} {:02X}{:02X}{:02X}{:02X} -------- {:02X}{:02X}{:02X}{:02X}", item_v2.data1[0], item_v2.data1[1], item_v2.data1[2], item_v2.data1[3], item_v2.data1[4], item_v2.data1[5], item_v2.data1[6], item_v2.data1[7], item_v2.data1[8], item_v2.data1[9], item_v2.data1[10], item_v2.data1[11], item_v2.data2[0], item_v2.data2[1], item_v2.data2[2], item_v2.data2[3]); if (item_v2_decoded != item) { - phosg::log_warning("V2-decoded data does not match original data"); - phosg::log_warning("Data (V2-decoded): %02hhX%02hhX%02hhX%02hhX %02hhX%02hhX%02hhX%02hhX %02hhX%02hhX%02hhX%02hhX -------- %02hhX%02hhX%02hhX%02hhX", + phosg::log_warning_f("V2-decoded data does not match original data"); + phosg::log_warning_f("Data (V2-decoded): {:02X}{:02X}{:02X}{:02X} {:02X}{:02X}{:02X}{:02X} {:02X}{:02X}{:02X}{:02X} -------- {:02X}{:02X}{:02X}{:02X}", item_v2_decoded.data1[0], item_v2_decoded.data1[1], item_v2_decoded.data1[2], item_v2_decoded.data1[3], item_v2_decoded.data1[4], item_v2_decoded.data1[5], item_v2_decoded.data1[6], item_v2_decoded.data1[7], item_v2_decoded.data1[8], item_v2_decoded.data1[9], item_v2_decoded.data1[10], item_v2_decoded.data1[11], @@ -2196,38 +2176,38 @@ Action a_describe_item( ItemData item_gc_decoded = item_gc; item_gc_decoded.decode_for_version(Version::GC_V3); - phosg::log_info("Data (GC-encoded): %02hhX%02hhX%02hhX%02hhX %02hhX%02hhX%02hhX%02hhX %02hhX%02hhX%02hhX%02hhX -------- %02hhX%02hhX%02hhX%02hhX", + phosg::log_info_f("Data (GC-encoded): {:02X}{:02X}{:02X}{:02X} {:02X}{:02X}{:02X}{:02X} {:02X}{:02X}{:02X}{:02X} -------- {:02X}{:02X}{:02X}{:02X}", item_gc.data1[0], item_gc.data1[1], item_gc.data1[2], item_gc.data1[3], item_gc.data1[4], item_gc.data1[5], item_gc.data1[6], item_gc.data1[7], item_gc.data1[8], item_gc.data1[9], item_gc.data1[10], item_gc.data1[11], item_gc.data2[0], item_gc.data2[1], item_gc.data2[2], item_gc.data2[3]); if (item_gc_decoded != item) { - phosg::log_warning("GC-decoded data does not match original data"); - phosg::log_warning("Data (GC-decoded): %02hhX%02hhX%02hhX%02hhX %02hhX%02hhX%02hhX%02hhX %02hhX%02hhX%02hhX%02hhX -------- %02hhX%02hhX%02hhX%02hhX", + phosg::log_warning_f("GC-decoded data does not match original data"); + phosg::log_warning_f("Data (GC-decoded): {:02X}{:02X}{:02X}{:02X} {:02X}{:02X}{:02X}{:02X} {:02X}{:02X}{:02X}{:02X} -------- {:02X}{:02X}{:02X}{:02X}", item_gc_decoded.data1[0], item_gc_decoded.data1[1], item_gc_decoded.data1[2], item_gc_decoded.data1[3], item_gc_decoded.data1[4], item_gc_decoded.data1[5], item_gc_decoded.data1[6], item_gc_decoded.data1[7], item_gc_decoded.data1[8], item_gc_decoded.data1[9], item_gc_decoded.data1[10], item_gc_decoded.data1[11], item_gc_decoded.data2[0], item_gc_decoded.data2[1], item_gc_decoded.data2[2], item_gc_decoded.data2[3]); } - phosg::log_info("Description: %s", desc.c_str()); - phosg::log_info("Description (in-game): %s", desc_colored.c_str()); + phosg::log_info_f("Description: {}", desc); + phosg::log_info_f("Description (in-game): {}", desc_colored); size_t purchase_price = s->item_parameter_table(Version::BB_V4)->price_for_item(item); size_t sale_price = purchase_price >> 3; - phosg::log_info("Purchase price: %zu; sale price: %zu", purchase_price, sale_price); + phosg::log_info_f("Purchase price: {}; sale price: {}", purchase_price, sale_price); }); Action a_name_all_items( "name-all-items", nullptr, +[](phosg::Arguments& args) { auto s = make_shared(get_config_filename(args)); - s->clear_file_caches(false); + s->clear_file_caches(); s->load_config_early(); - s->load_patch_indexes(false); - s->load_text_index(false); - s->load_item_definitions(false); - s->load_item_name_indexes(false); - s->load_ep3_cards(false); + s->load_patch_indexes(); + s->load_text_index(); + s->load_item_definitions(); + s->load_item_name_indexes(); + s->load_ep3_cards(); s->load_config_late(); set all_primary_identifiers; @@ -2241,7 +2221,7 @@ Action a_name_all_items( if (args.get("list")) { for (uint32_t primary_identifier : all_primary_identifiers) { - fprintf(stdout, "%08" PRIX32 "\n", primary_identifier); + phosg::fwrite_fmt(stdout, "{:08X}\n", primary_identifier); for (Version v : ALL_VERSIONS) { const auto& index = s->item_name_index_opt(v); if (index) { @@ -2250,9 +2230,9 @@ Action a_name_all_items( string name = index->describe_item(item); try { bool is_rare = pmt->is_item_rare(item); - fprintf(stdout, " %10s: %s %s\n", phosg::name_for_enum(v), is_rare ? "+++" : "---", name.c_str()); + phosg::fwrite_fmt(stdout, " {:10}: {} {}\n", phosg::name_for_enum(v), is_rare ? "+++" : "---", name); } catch (const out_of_range&) { - fprintf(stdout, " %10s: (missing)\n", phosg::name_for_enum(v)); + phosg::fwrite_fmt(stdout, " {:10}: (missing)\n", phosg::name_for_enum(v)); } } } @@ -2262,11 +2242,11 @@ Action a_name_all_items( bool separate_classes = args.get("separate-classes"); auto print_header = [&]() -> void { - fprintf(stdout, "IDENT :"); + phosg::fwrite_fmt(stdout, "IDENT :"); for (Version v : ALL_VERSIONS) { const auto& index = s->item_name_index_opt(v); if (index) { - fprintf(stdout, " %30s ", phosg::name_for_enum(v)); + phosg::fwrite_fmt(stdout, " {:30} ", phosg::name_for_enum(v)); } } fputc('\n', stdout); @@ -2281,7 +2261,7 @@ Action a_name_all_items( } prev_ident = primary_identifier; - fprintf(stdout, "%08" PRIX32 ":", primary_identifier); + phosg::fwrite_fmt(stdout, "{:08X}:", primary_identifier); for (Version v : ALL_VERSIONS) { const auto& index = s->item_name_index_opt(v); if (index) { @@ -2290,9 +2270,9 @@ Action a_name_all_items( if (index->exists(item)) { string name = index->describe_item(item); bool is_rare = pmt->is_item_rare(item); - fprintf(stdout, " %30s%s", name.c_str(), is_rare ? " ***" : " ..."); + phosg::fwrite_fmt(stdout, " {:30}{}", name, is_rare ? " ***" : " ..."); } else { - fprintf(stdout, " ------------------------------ ---"); + phosg::fwrite_fmt(stdout, " ------------------------------ ---"); } } } @@ -2308,9 +2288,9 @@ Action a_print_level_stats( +[](phosg::Arguments& args) { auto s = make_shared(get_config_filename(args)); s->load_config_early(); - s->clear_file_caches(false); - s->load_patch_indexes(false); - s->load_level_tables(false); + s->clear_file_caches(); + s->load_patch_indexes(); + s->load_level_tables(); vector level_1_v1_v2; vector level_100_v1_v2; @@ -2342,38 +2322,38 @@ Action a_print_level_stats( } auto print_stats_set = [](const vector& stats_vec, const char* name) -> void { - fprintf(stdout, "%s ", name); + phosg::fwrite_fmt(stdout, "{} ", name); for (size_t z = 0; z < stats_vec.size(); z++) { - fprintf(stdout, " %s", abbreviation_for_char_class(z)); + phosg::fwrite_fmt(stdout, " {}", abbreviation_for_char_class(z)); } - fprintf(stdout, "\n%s ATP", name); + phosg::fwrite_fmt(stdout, "\n{} ATP", name); for (const auto& stats : stats_vec) { - fprintf(stdout, " %4hu", stats.char_stats.atp.load()); + phosg::fwrite_fmt(stdout, " {:4}", stats.char_stats.atp); } - fprintf(stdout, "\n%s DFP", name); + phosg::fwrite_fmt(stdout, "\n{} DFP", name); for (const auto& stats : stats_vec) { - fprintf(stdout, " %4hu", stats.char_stats.dfp.load()); + phosg::fwrite_fmt(stdout, " {:4}", stats.char_stats.dfp); } - fprintf(stdout, "\n%s MST", name); + phosg::fwrite_fmt(stdout, "\n{} MST", name); for (const auto& stats : stats_vec) { - fprintf(stdout, " %4hu", stats.char_stats.mst.load()); + phosg::fwrite_fmt(stdout, " {:4}", stats.char_stats.mst); } - fprintf(stdout, "\n%s ATA", name); + phosg::fwrite_fmt(stdout, "\n{} ATA", name); for (const auto& stats : stats_vec) { - fprintf(stdout, " %4hu", stats.char_stats.ata.load()); + phosg::fwrite_fmt(stdout, " {:4}", stats.char_stats.ata); } - fprintf(stdout, "\n%s EVP", name); + phosg::fwrite_fmt(stdout, "\n{} EVP", name); for (const auto& stats : stats_vec) { - fprintf(stdout, " %4hu", stats.char_stats.evp.load()); + phosg::fwrite_fmt(stdout, " {:4}", stats.char_stats.evp); } - fprintf(stdout, "\n%s LCK", name); + phosg::fwrite_fmt(stdout, "\n{} LCK", name); for (const auto& stats : stats_vec) { - fprintf(stdout, " %4hu", stats.char_stats.lck.load()); + phosg::fwrite_fmt(stdout, " {:4}", stats.char_stats.lck); } - fprintf(stdout, "\n%s HP", name); + phosg::fwrite_fmt(stdout, "\n{} HP", name); for (const auto& stats : stats_vec) { - fprintf(stdout, " %4hu", stats.char_stats.hp.load()); + phosg::fwrite_fmt(stdout, " {:4}", stats.char_stats.hp); } fputc('\n', stdout); }; @@ -2398,11 +2378,11 @@ Action a_print_item_parameter_tables( format.\n", +[](phosg::Arguments& args) { auto s = make_shared(get_config_filename(args)); - s->load_all(); + s->load_all(false); for (Version v : ALL_VERSIONS) { const auto& index = s->item_name_index_opt(v); if (index) { - fprintf(stdout, "======== %s\n", phosg::name_for_enum(v)); + phosg::fwrite_fmt(stdout, "======== {}\n", phosg::name_for_enum(v)); index->print_table(stdout); } } @@ -2417,7 +2397,7 @@ Action a_show_ep3_cards( bool one_line = args.get("one-line"); auto s = make_shared(get_config_filename(args)); - s->load_ep3_cards(false); + s->load_ep3_cards(); unique_ptr text_english; try { @@ -2427,28 +2407,28 @@ Action a_show_ep3_cards( } auto card_ids = s->ep3_card_index->all_ids(); - phosg::log_info("%zu card definitions", card_ids.size()); + phosg::log_info_f("{} card definitions", card_ids.size()); for (uint32_t card_id : card_ids) { auto entry = s->ep3_card_index->definition_for_id(card_id); string def_str = entry->def.str(one_line, text_english.get()); if (one_line) { - fprintf(stdout, "%s\n", def_str.c_str()); + phosg::fwrite_fmt(stdout, "{}\n", def_str); } else { - fprintf(stdout, "%s\n", def_str.c_str()); + phosg::fwrite_fmt(stdout, "{}\n", def_str); if (!entry->debug_tags.empty()) { string tags = phosg::join(entry->debug_tags, ", "); - fprintf(stdout, " Tags: %s\n", tags.c_str()); + phosg::fwrite_fmt(stdout, " Tags: {}\n", tags); } if (!entry->dice_caption.empty()) { - fprintf(stdout, " Dice caption: %s\n", entry->dice_caption.c_str()); + phosg::fwrite_fmt(stdout, " Dice caption: {}\n", entry->dice_caption); } if (!entry->dice_caption.empty()) { - fprintf(stdout, " Dice text: %s\n", entry->dice_text.c_str()); + phosg::fwrite_fmt(stdout, " Dice text: {}\n", entry->dice_text); } if (!entry->text.empty()) { string text = phosg::str_replace_all(entry->text, "\n", "\n "); phosg::strip_trailing_whitespace(text); - fprintf(stdout, " Text:\n %s\n", text.c_str()); + phosg::fwrite_fmt(stdout, " Text:\n {}\n", text); } fputc('\n', stdout); } @@ -2472,10 +2452,10 @@ Action a_generate_ep3_cards_html( bool no_disassembly = args.get("no-disassembly"); auto s = make_shared(get_config_filename(args)); - s->clear_file_caches(false); - s->load_patch_indexes(false); - s->load_text_index(false); - s->load_ep3_cards(false); + s->clear_file_caches(); + s->load_patch_indexes(); + s->load_text_index(); + s->load_ep3_cards(); shared_ptr text_english; try { @@ -2521,7 +2501,8 @@ Action a_generate_ep3_cards_html( } if (cardtex_directory) { - for (const auto& filename : phosg::list_directory_sorted(cardtex_directory)) { + for (const auto& item : std::filesystem::directory_iterator(cardtex_directory)) { + string filename = item.path().filename().string(); if ((filename[0] == 'C' || filename[0] == 'M' || filename[0] == 'L') && (filename[1] == '_')) { size_t card_id = stoull(filename.substr(2, 3), nullptr, 10); if (this->card_infos.size() <= card_id) { @@ -2592,7 +2573,7 @@ Action a_generate_ep3_cards_html( blocks.emplace_back(""); for (const auto& vi : version_infos) { - blocks.emplace_back(phosg::string_printf("", + blocks.emplace_back(std::format("", vi.num_output_columns, vi.name)); } blocks.emplace_back(""); @@ -2627,12 +2608,12 @@ Action a_generate_ep3_cards_html( continue; } - blocks.emplace_back(phosg::string_printf("", card_id)); + blocks.emplace_back(std::format("", card_id)); for (const auto& vi : version_infos) { const VersionInfo::CardInfo* entry = vi.get_entry(card_id); if (!entry) { - blocks.emplace_back(phosg::string_printf("", + blocks.emplace_back(std::format("", vi.num_output_columns)); continue; } @@ -2649,7 +2630,7 @@ Action a_generate_ep3_cards_html( background_color = "#333333"; } - string td_tag = phosg::string_printf("
ID%s{}
%04zX
{:04X}
No entry
No entry
", background_color); + string td_tag = std::format("", background_color); if (vi.show_small_column) { blocks.emplace_back(td_tag); if (!entry->small_data_url.empty()) { @@ -2710,14 +2691,14 @@ Action a_show_ep3_maps( Print the Episode 3 maps from the system/ep3 directory in a (sort of)\n\ human-readable format.\n", +[](phosg::Arguments& args) { - config_log.info("Collecting Episode 3 data"); + config_log.info_f("Collecting Episode 3 data"); auto s = make_shared(get_config_filename(args)); - s->load_ep3_cards(false); - s->load_ep3_maps(false); + s->load_ep3_cards(); + s->load_ep3_maps(); auto map_ids = s->ep3_map_index->all_numbers(); - phosg::log_info("%zu maps", map_ids.size()); + phosg::log_info_f("{} maps", map_ids.size()); for (uint32_t map_id : map_ids) { auto map = s->ep3_map_index->for_number(map_id); const auto& vms = map->all_versions(); @@ -2726,7 +2707,7 @@ Action a_show_ep3_maps( continue; } string map_s = vms[language]->map->str(s->ep3_card_index.get(), language); - fprintf(stdout, "(%c) %s\n", char_for_language_code(language), map_s.c_str()); + phosg::fwrite_fmt(stdout, "({}) {}\n", char_for_language_code(language), map_s); } } }); @@ -2738,20 +2719,20 @@ Action a_show_battle_params( in a human-readable format.\n", +[](phosg::Arguments& args) { auto s = make_shared(get_config_filename(args)); - s->load_patch_indexes(false); - s->load_battle_params(false); + s->load_patch_indexes(); + s->load_battle_params(); - fprintf(stdout, "Episode 1 multi\n"); + phosg::fwrite_fmt(stdout, "Episode 1 multi\n"); s->battle_params->get_table(false, Episode::EP1).print(stdout); - fprintf(stdout, "Episode 1 solo\n"); + phosg::fwrite_fmt(stdout, "Episode 1 solo\n"); s->battle_params->get_table(true, Episode::EP1).print(stdout); - fprintf(stdout, "Episode 2 multi\n"); + phosg::fwrite_fmt(stdout, "Episode 2 multi\n"); s->battle_params->get_table(false, Episode::EP2).print(stdout); - fprintf(stdout, "Episode 2 solo\n"); + phosg::fwrite_fmt(stdout, "Episode 2 solo\n"); s->battle_params->get_table(true, Episode::EP2).print(stdout); - fprintf(stdout, "Episode 4 multi\n"); + phosg::fwrite_fmt(stdout, "Episode 4 multi\n"); s->battle_params->get_table(false, Episode::EP4).print(stdout); - fprintf(stdout, "Episode 4 solo\n"); + phosg::fwrite_fmt(stdout, "Episode 4 solo\n"); s->battle_params->get_table(true, Episode::EP4).print(stdout); }); @@ -2768,10 +2749,12 @@ Action a_check_supermaps( auto s = make_shared(get_config_filename(args)); s->load_config_early(); - s->clear_file_caches(false); - s->load_patch_indexes(false); - s->load_set_data_tables(false); - s->load_maps(false); + s->clear_file_caches(); + s->load_patch_indexes(); + s->load_set_data_tables(); + s->load_maps(); + + auto rand_crypt = make_shared(phosg::random_object()); SuperMap::EfficiencyStats all_free_maps_eff; for (const auto& it : s->supermap_for_free_play_key) { @@ -2784,8 +2767,8 @@ Action a_check_supermaps( string filename_token; if (save_disassembly) { - string filename = phosg::string_printf( - "supermap_%s_%s_%c_%02hhX_%02hhx_%02hhX.txt", + string filename = std::format( + "supermap_{}_{}_{}_{:02X}_{:02X}_{:02X}.txt", abbreviation_for_episode(episode), abbreviation_for_mode(mode), abbreviation_for_difficulty(difficulty), @@ -2798,15 +2781,15 @@ Action a_check_supermaps( auto eff = it.second->efficiency(); all_free_maps_eff += eff; auto eff_str = eff.str(); - fprintf(stderr, "FREE MAP: %08" PRIX32 " => %s %s %c floor=%02hhX layout=%02hhX entities=%02hhX => %s%s\n", + phosg::fwrite_fmt(stderr, "FREE MAP: {:08X} => {} {} {} floor={:02X} layout={:02X} entities={:02X} => {}{}\n", it.first, abbreviation_for_episode(episode), abbreviation_for_mode(mode), abbreviation_for_difficulty(difficulty), - floor, layout, entities, eff_str.c_str(), filename_token.c_str()); + floor, layout, entities, eff_str, filename_token); } string all_free_maps_eff_str = all_free_maps_eff.str(); - fprintf(stderr, "ALL FREE MAPS: %s\n", all_free_maps_eff_str.c_str()); + phosg::fwrite_fmt(stderr, "ALL FREE MAPS: {}\n", all_free_maps_eff_str); // Generate MapStates for a few random variations for (size_t z = 0; z < 0x20; z++) { @@ -2818,26 +2801,26 @@ Action a_check_supermaps( uint8_t difficulty = phosg::random_object() % 4; uint8_t event = phosg::random_object() % 8; uint32_t random_seed = phosg::random_object(); - fprintf(stderr, "FREE MAP STATE TEST: %s %s %c\n", + phosg::fwrite_fmt(stderr, "FREE MAP STATE TEST: {} {} {}\n", abbreviation_for_episode(episode), abbreviation_for_mode(mode), abbreviation_for_difficulty(difficulty)); auto sdt = s->set_data_table(Version::BB_V4, episode, mode, difficulty); - auto variations = sdt->generate_variations(episode, (mode == GameMode::SOLO), nullptr); + auto variations = sdt->generate_variations(episode, (mode == GameMode::SOLO), rand_crypt); auto supermaps = s->supermaps_for_variations(episode, mode, difficulty, variations); auto map_state = make_shared( - 0, difficulty, event, random_seed, MapState::DEFAULT_RARE_ENEMIES, nullptr, supermaps); + 0, difficulty, event, random_seed, MapState::DEFAULT_RARE_ENEMIES, rand_crypt, supermaps); map_state->verify(); - fprintf(stderr, " map state ok: 0x%zX objects, 0x%zX enemies, 0x%zX enemy sets, 0x%zX events\n", + phosg::fwrite_fmt(stderr, " map state ok: 0x{:X} objects, 0x{:X} enemies, 0x{:X} enemy sets, 0x{:X} events\n", map_state->object_states.size(), map_state->enemy_states.size(), map_state->enemy_set_states.size(), map_state->event_states.size()); } - s->load_quest_index(false); + s->load_quest_index(); SuperMap::EfficiencyStats all_quests_eff; uint32_t random_seed = args.get("random-seed", 0, phosg::Arguments::IntFormat::HEX); @@ -2849,9 +2832,9 @@ Action a_check_supermaps( string filename_token; if (save_disassembly) { - string filename = phosg::string_printf("supermap_quest_%" PRIu32 "_%08" PRIX32 ".txt", it.first, random_seed); + string filename = std::format("supermap_quest_{}_{:08X}.txt", it.first, random_seed); auto f = phosg::fopen_unique(filename, "wt"); - fprintf(f.get(), "QUEST %" PRIu32 " (%s)\n", it.first, it.second->name.c_str()); + phosg::fwrite_fmt(f.get(), "QUEST {} ({})\n", it.first, it.second->name); supermap->print(f.get()); filename_token = " => " + filename; } @@ -2860,10 +2843,10 @@ Action a_check_supermaps( for (Version v : ALL_NON_PATCH_VERSIONS) { counts_for_version[static_cast(v)] = supermap->count_enemy_sets_for_version(v); } - string filename = phosg::string_printf("supermap_quest_%" PRIu32 "_%08" PRIX32 "_enemy_counts.txt", it.first, random_seed); + string filename = std::format("supermap_quest_{}_{:08X}_enemy_counts.txt", it.first, random_seed); auto f = phosg::fopen_unique(filename, "wt"); - fprintf(f.get(), "QUEST %" PRIu32 " (%s)\n", it.first, it.second->name.c_str()); - fprintf(f.get(), "ENEMY--------------- DCNTE 11/2K DC-V1 DC-V2 PCNTE PC-V2 GCNTE GC-V3 XB-V3 BB-V4\n"); + phosg::fwrite_fmt(f.get(), "QUEST {} ({})\n", it.first, it.second->name); + phosg::fwrite_fmt(f.get(), "ENEMY--------------- DCNTE 11/2K DC-V1 DC-V2 PCNTE PC-V2 GCNTE GC-V3 XB-V3 BB-V4\n"); for (size_t type_ss = 0; type_ss < static_cast(EnemyType::MAX_ENEMY_TYPE); type_ss++) { EnemyType type = static_cast(type_ss); bool any_count_nonzero = false; @@ -2880,11 +2863,11 @@ Action a_check_supermaps( } } if (any_count_nonzero) { - fprintf(f.get(), "%20s", phosg::name_for_enum(type)); + phosg::fwrite_fmt(f.get(), "{:20}", phosg::name_for_enum(type)); for (Version v : ALL_NON_PATCH_VERSIONS) { size_t count = counts[static_cast(v)]; if (count > 0) { - fprintf(f.get(), " %5zu", count); + phosg::fwrite_fmt(f.get(), " {:5}", count); } else { fputs(" ", f.get()); } @@ -2896,7 +2879,7 @@ Action a_check_supermaps( auto eff = supermap->efficiency(); all_quests_eff += eff; auto eff_str = eff.str(); - fprintf(stderr, "QUEST MAP: %08" PRIX32 " => %s%s\n", it.first, eff_str.c_str(), filename_token.c_str()); + phosg::fwrite_fmt(stderr, "QUEST MAP: {:08X} => {}{}\n", it.first, eff_str, filename_token); auto map_state = make_shared( 0, @@ -2904,18 +2887,18 @@ Action a_check_supermaps( 0, phosg::random_object(), MapState::DEFAULT_RARE_ENEMIES, - nullptr, + rand_crypt, supermap); map_state->verify(); - fprintf(stderr, " map state ok: 0x%zX objects, 0x%zX enemies, 0x%zX enemy sets, 0x%zX events\n", + phosg::fwrite_fmt(stderr, " map state ok: 0x{:X} objects, 0x{:X} enemies, 0x{:X} enemy sets, 0x{:X} events\n", map_state->object_states.size(), map_state->enemy_states.size(), map_state->enemy_set_states.size(), map_state->event_states.size()); } string all_quests_eff_str = all_quests_eff.str(); - fprintf(stderr, "ALL QUEST MAPS: %s\n", all_quests_eff_str.c_str()); + phosg::fwrite_fmt(stderr, "ALL QUEST MAPS: {}\n", all_quests_eff_str); }); Action a_parse_object_graph( @@ -2935,7 +2918,7 @@ Action a_generate_dc_serial_number( uint8_t domain = args.get(1); uint8_t subdomain = args.get(2); string serial_number = generate_dc_serial_number(domain, subdomain); - fprintf(stdout, "%s\n", serial_number.c_str()); + phosg::fwrite_fmt(stdout, "{}\n", serial_number); }); Action a_generate_all_dc_serial_numbers( "dc-serial-number-generator-test", nullptr, @@ -2951,7 +2934,7 @@ Action a_generate_all_dc_serial_numbers( while ((serial_number = iter.next()) != 0) { serial_numbers[iter.domain * 3 + iter.subdomain].emplace(serial_number); if (((++num_serial_numbers) % 0x10000) == 0) { - fprintf(stderr, "... %08zX (domain=%02hhX, subdomain=%02hhX, index2=%04hX, index3=%04hX) counts=[%zu, %zu, %zu, %zu, %zu, %zu, %zu, %zu, %zu]\n", + phosg::fwrite_fmt(stderr, "... {:08X} (domain={:02X}, subdomain={:02X}, index2={:04X}, index3={:04X}) counts=[{}, {}, {}, {}, {}, {}, {}, {}, {}]\n", num_serial_numbers, iter.domain, iter.subdomain, iter.index2, iter.index3, serial_numbers[0].size(), serial_numbers[1].size(), serial_numbers[2].size(), serial_numbers[3].size(), serial_numbers[4].size(), serial_numbers[5].size(), @@ -2969,7 +2952,7 @@ Action a_generate_all_dc_serial_numbers( bool was_iterated = serial_numbers[domain * 3 + subdomain].count(serial_number); if (is_valid != was_iterated) { lock_guard g(output_lock); - fprintf(stdout, "Mismatch at %08" PRIX64 " (domain=%hhu, subdomain=%hhu): is_valid=%s, was_iterated=%s\n", + phosg::fwrite_fmt(stdout, "Mismatch at {:08X} (domain={}, subdomain={}): is_valid={}, was_iterated={}\n", serial_number, domain, subdomain, is_valid ? "true" : "false", was_iterated ? "true" : "false"); } else if (is_valid && was_iterated) { found_counts[domain * 3 + subdomain]++; @@ -2979,7 +2962,7 @@ Action a_generate_all_dc_serial_numbers( return false; }; auto progress_fn = [&](uint64_t, uint64_t, uint64_t current_value, uint64_t) -> void { - fprintf(stderr, "... %08" PRIX64 " %" PRId64 " mismatches; counts: [%zu/%zu, %zu/%zu, %zu/%zu, %zu/%zu, %zu/%zu, %zu/%zu, %zu/%zu, %zu/%zu, %zu/%zu]\r", current_value, num_mismatches.load(), + phosg::fwrite_fmt(stderr, "... {:08X} {} mismatches; counts: [{}/{}, {}/{}, {}/{}, {}/{}, {}/{}, {}/{}, {}/{}, {}/{}, {}/{}]\r", current_value, num_mismatches.load(), found_counts[0].load(), serial_numbers[0].size(), found_counts[1].load(), serial_numbers[1].size(), found_counts[2].load(), serial_numbers[2].size(), @@ -3012,13 +2995,13 @@ Action a_inspect_dc_serial_number( for (uint8_t domain = 0; domain < 3; domain++) { for (uint8_t subdomain = 0; subdomain < 3; subdomain++) { if (dc_serial_number_is_valid_fast(serial_number_str, domain, subdomain)) { - fprintf(stdout, "%s is valid in domain %hhu subdomain %hhu\n", serial_number_str.c_str(), domain, subdomain); + phosg::fwrite_fmt(stdout, "{} is valid in domain {} subdomain {}\n", serial_number_str, domain, subdomain); num_valid_subdomains++; } } } if (num_valid_subdomains == 0) { - fprintf(stdout, "%s is not valid in any domain\n", serial_number_str.c_str()); + phosg::fwrite_fmt(stdout, "{} is not valid in any domain\n", serial_number_str); } }); Action a_dc_serial_number_speed_test( @@ -3048,10 +3031,10 @@ Action a_diff_executables( const string& a_filename = args.get(1); const string& b_filename = args.get(2); bool show_pre = args.get("show-pre"); - bool a_is_dol = phosg::ends_with(a_filename, ".dol"); - bool b_is_dol = phosg::ends_with(b_filename, ".dol"); - bool a_is_xbe = phosg::ends_with(a_filename, ".xbe"); - bool b_is_xbe = phosg::ends_with(b_filename, ".xbe"); + bool a_is_dol = a_filename.ends_with(".dol"); + bool b_is_dol = b_filename.ends_with(".dol"); + bool a_is_xbe = a_filename.ends_with(".xbe"); + bool b_is_xbe = b_filename.ends_with(".xbe"); std::vector result; if (a_is_dol && b_is_dol) { result = diff_dol_files(a_filename, b_filename); @@ -3064,9 +3047,9 @@ Action a_diff_executables( string b_str = phosg::format_data_string(it.b_data, nullptr, phosg::FormatDataFlags::HEX_ONLY); if (show_pre) { string a_str = phosg::format_data_string(it.a_data, nullptr, phosg::FormatDataFlags::HEX_ONLY); - fprintf(stdout, "%08" PRIX32 ": %s => %s\n", it.address, a_str.c_str(), b_str.c_str()); + phosg::fwrite_fmt(stdout, "{:08X}: {} => {}\n", it.address, a_str, b_str); } else { - fprintf(stdout, "%08" PRIX32 " %s\n", it.address, b_str.c_str()); + phosg::fwrite_fmt(stdout, "{:08X} {}\n", it.address, b_str); } } }); @@ -3077,7 +3060,7 @@ Action a_generate_hangame_creds( const string& token = args.get(2); const string& unused = args.get(3, false); string hex = phosg::format_data_string(encode_psobb_hangame_credentials(user_id, token, unused)); - fprintf(stdout, "psobb.exe 1196310600 %s\n", hex.c_str()); + phosg::fwrite_fmt(stdout, "psobb.exe 1196310600 {}\n", hex); }); Action a_format_ep3_battle_record( @@ -3090,8 +3073,8 @@ Action a_format_ep3_battle_record( Action a_replay_ep3_battle_commands( "replay-ep3-battle-commands", nullptr, +[](phosg::Arguments& args) { auto s = make_shared(get_config_filename(args)); - s->load_ep3_cards(false); - s->load_ep3_maps(false); + s->load_ep3_cards(); + s->load_ep3_maps(); int64_t base_seed = args.get("seed", -1); bool is_trial = (get_cli_version(args, Version::GC_EP3) == Version::GC_EP3_NTE); @@ -3111,7 +3094,7 @@ Action a_replay_ep3_battle_commands( .map_index = s->ep3_map_index, .behavior_flags = 0x0092, .opt_rand_stream = nullptr, - .opt_rand_crypt = (seed >= 0) ? make_shared(seed) : nullptr, + .rand_crypt = make_shared(seed), .tournament = nullptr, .trap_card_ids = {}, }; @@ -3142,8 +3125,8 @@ Action a_replay_ep3_battle_record( auto rec = make_shared(read_input_data(args)); auto s = make_shared(get_config_filename(args)); - s->load_ep3_cards(false); - s->load_ep3_maps(false); + s->load_ep3_cards(); + s->load_ep3_maps(); bool is_trial = (get_cli_version(args, Version::GC_EP3) == Version::GC_EP3_NTE); @@ -3155,7 +3138,7 @@ Action a_replay_ep3_battle_record( Episode3::BehaviorFlag::DISABLE_MASKING | Episode3::BehaviorFlag::LOG_COMMANDS_IF_LOBBY_MISSING), .opt_rand_stream = make_shared(rec->get_random_stream()), - .opt_rand_crypt = nullptr, + .rand_crypt = make_shared(), .tournament = nullptr, .trap_card_ids = {}, }; @@ -3166,7 +3149,7 @@ Action a_replay_ep3_battle_record( auto server = make_shared(nullptr, std::move(options)); server->init(); for (const auto& command : rec->get_all_server_data_commands()) { - phosg::log_info("Server data command"); + phosg::log_info_f("Server data command"); phosg::print_data(stderr, command, 0, nullptr, phosg::PrintDataFlags::PRINT_ASCII | phosg::PrintDataFlags::DISABLE_COLOR | phosg::PrintDataFlags::OFFSET_16_BITS); server->on_server_data_input(nullptr, command); } @@ -3181,161 +3164,103 @@ Action a_run_server_replay_log( "", nullptr, +[](phosg::Arguments& args) { { string build_date = phosg::format_time(BUILD_TIMESTAMP); - config_log.info("newserv %s compiled at %s", GIT_REVISION_HASH, build_date.c_str()); + config_log.info_f("newserv {} compiled at {}", GIT_REVISION_HASH, build_date); } - if (evthread_use_pthreads()) { - throw runtime_error("failed to set up libevent threads"); - } - - if (!phosg::isdir("system/players")) { - config_log.info("Players directory does not exist; creating it"); - mkdir("system/players", 0755); + if (!std::filesystem::is_directory("system/players")) { + config_log.info_f("Players directory does not exist; creating it"); + std::filesystem::create_directories("system/players"); } const string& replay_log_filename = args.get("replay-log"); - bool is_replay = !replay_log_filename.empty(); +#ifndef PHOSG_WINDOWS signal(SIGPIPE, SIG_IGN); - if (isatty(fileno(stderr))) { +#endif + if (!phosg::is_windows() && isatty(fileno(stderr))) { use_terminal_colors = true; } - if (is_replay) { - set_function_compiler_available(false); - } + auto state = make_shared(get_config_filename(args)); + state->load_all(true); - shared_ptr base(event_base_new(), event_base_free); - auto state = make_shared(base, get_config_filename(args), is_replay); - state->load_all(); - - if (state->dns_server_port && !is_replay) { + if (state->dns_server_port) { if (!state->dns_server_addr.empty()) { - config_log.info("Starting DNS server on %s:%hu", state->dns_server_addr.c_str(), state->dns_server_port); + config_log.info_f("Starting DNS server on {}:{}", state->dns_server_addr, state->dns_server_port); } else { - config_log.info("Starting DNS server on port %hu", state->dns_server_port); + config_log.info_f("Starting DNS server on port {}", state->dns_server_port); } - state->dns_server = make_shared( - base, state->local_address, state->external_address, state->banned_ipv4_ranges); + state->dns_server = make_shared(state); state->dns_server->listen(state->dns_server_addr, state->dns_server_port); } else { - config_log.info("DNS server is disabled"); + config_log.info_f("DNS server is disabled"); } shared_ptr shell; shared_ptr replay_session; shared_ptr signal_watcher; - if (is_replay) { - config_log.info("Starting proxy server"); - state->proxy_server = make_shared(base, state); - config_log.info("Starting game server"); - state->game_server = make_shared(base, state); + if (!replay_log_filename.empty()) { + config_log.info_f("Starting game server"); + state->game_server = make_shared(state); - auto nop_destructor = +[](FILE*) {}; - shared_ptr log_f(stdin, nop_destructor); - if (replay_log_filename != "-") { - log_f = phosg::fopen_shared(replay_log_filename, "rt"); - } + auto log_f = phosg::fopen_shared(replay_log_filename, "rt"); - replay_session = make_shared(base, log_f.get(), state, args.get("require-basic-credentials")); - replay_session->start(); + replay_session = make_shared(state, log_f.get(), false); + asio::co_spawn(*state->io_context, replay_session->run(), asio::detached); } else { - config_log.info("Opening sockets"); - for (const auto& it : state->name_to_port_config) { - const auto& pc = it.second; - if (pc->behavior == ServerBehavior::PROXY_SERVER) { - if (!state->proxy_server.get()) { - config_log.info("Starting proxy server"); - state->proxy_server = make_shared(base, state); - } - - // For PC and GC, proxy sessions are dynamically created when a client - // picks a destination from the menu. For patch and BB clients, there's - // no way to ask the client which destination they want, so only one - // destination is supported, and we have to manually specify the - // destination netloc here. - if (is_patch(pc->version)) { - auto [ss, size] = phosg::make_sockaddr_storage( - state->proxy_destination_patch.first, - state->proxy_destination_patch.second); - state->proxy_server->listen(pc->addr, pc->port, pc->version, &ss); - } else if (is_v4(pc->version)) { - auto [ss, size] = phosg::make_sockaddr_storage( - state->proxy_destination_bb.first, - state->proxy_destination_bb.second); - state->proxy_server->listen(pc->addr, pc->port, pc->version, &ss); - } else { - state->proxy_server->listen(pc->addr, pc->port, pc->version); - } - - } else if (pc->behavior == ServerBehavior::PATCH_SERVER_PC) { - if (!state->pc_patch_server.get()) { - config_log.info("Starting PC_V2 patch server"); - state->pc_patch_server = make_shared(state->generate_patch_server_config(false)); - } - string spec = phosg::string_printf("TU-%hu-%s-patch2", pc->port, pc->name.c_str()); - state->pc_patch_server->listen(spec, pc->addr, pc->port, Version::PC_PATCH); - - } else if (pc->behavior == ServerBehavior::PATCH_SERVER_BB) { - if (!state->bb_patch_server.get()) { - config_log.info("Starting BB_V4 patch server"); - state->bb_patch_server = make_shared(state->generate_patch_server_config(true)); - } - string spec = phosg::string_printf("TU-%hu-%s-patch4", pc->port, pc->name.c_str()); - state->bb_patch_server->listen(spec, pc->addr, pc->port, Version::BB_PATCH); - - } else { - if (!state->game_server.get()) { - config_log.info("Starting game server"); - state->game_server = make_shared(base, state); - } - string spec = phosg::string_printf("TG-%hu-%s-%s-%s", pc->port, phosg::name_for_enum(pc->version), pc->name.c_str(), phosg::name_for_enum(pc->behavior)); - state->game_server->listen(spec, pc->addr, pc->port, pc->version, pc->behavior); + config_log.info_f("Opening sockets"); + for (const auto& [_, pc] : state->name_to_port_config) { + if (!state->game_server.get()) { + config_log.info_f("Starting game server"); + state->game_server = make_shared(state); } + string spec = std::format("TG-{}-{}-{}-{}", + pc->port, phosg::name_for_enum(pc->version), pc->name, phosg::name_for_enum(pc->behavior)); + state->game_server->listen(spec, pc->addr, pc->port, pc->version, pc->behavior); } if (!state->ip_stack_addresses.empty() || !state->ppp_stack_addresses.empty() || !state->ppp_raw_addresses.empty()) { - config_log.info("Starting IP/PPP stack simulator"); - state->ip_stack_simulator = make_shared(base, state); + config_log.info_f("Starting IP/PPP stack simulator"); + state->ip_stack_simulator = make_shared(state); for (const auto& it : state->ip_stack_addresses) { auto netloc = phosg::parse_netloc(it); - string spec = (netloc.second == 0) ? ("T-IPS-" + netloc.first) : phosg::string_printf("T-IPS-%hu", netloc.second); + string spec = (netloc.second == 0) ? ("T-IPS-" + netloc.first) : std::format("T-IPS-{}", netloc.second); state->ip_stack_simulator->listen( - spec, netloc.first, netloc.second, IPStackSimulator::Protocol::ETHERNET_TAPSERVER); + spec, netloc.first, netloc.second, VirtualNetworkProtocol::ETHERNET_TAPSERVER); } for (const auto& it : state->ppp_stack_addresses) { auto netloc = phosg::parse_netloc(it); - string spec = (netloc.second == 0) ? ("T-PPPST-" + netloc.first) : phosg::string_printf("T-PPPST-%hu", netloc.second); + string spec = (netloc.second == 0) ? ("T-PPPST-" + netloc.first) : std::format("T-PPPST-{}", netloc.second); state->ip_stack_simulator->listen( - spec, netloc.first, netloc.second, IPStackSimulator::Protocol::HDLC_TAPSERVER); + spec, netloc.first, netloc.second, VirtualNetworkProtocol::HDLC_TAPSERVER); } for (const auto& it : state->ppp_raw_addresses) { auto netloc = phosg::parse_netloc(it); - string spec = (netloc.second == 0) ? ("T-PPPSR-" + netloc.first) : phosg::string_printf("T-PPPSR-%hu", netloc.second); + string spec = (netloc.second == 0) ? ("T-PPPSR-" + netloc.first) : std::format("T-PPPSR-{}", netloc.second); state->ip_stack_simulator->listen( - spec, netloc.first, netloc.second, IPStackSimulator::Protocol::HDLC_RAW); + spec, netloc.first, netloc.second, VirtualNetworkProtocol::HDLC_RAW); if (netloc.second) { if (state->local_address == 0 && state->external_address == 0) { - config_log.info( - "Cannot generate Devolution phone numbers for %s because LocalAddress and ExternalAddress are not specified in the configuration", - spec.c_str()); + config_log.info_f( + "Cannot generate Devolution phone numbers for {} because LocalAddress and ExternalAddress are not specified in the configuration", + spec); } else if (state->local_address == 0) { - config_log.info( - "Note: The Devolution phone number for %s is %" PRIu64 " (external)", - spec.c_str(), devolution_phone_number_for_netloc(state->external_address, netloc.second)); + config_log.info_f( + "Note: The Devolution phone number for {} is {} (external)", + spec, devolution_phone_number_for_netloc(state->external_address, netloc.second)); } else if (state->external_address == 0) { - config_log.info( - "Note: The Devolution phone number for %s is %" PRIu64 " (local)", - spec.c_str(), devolution_phone_number_for_netloc(state->local_address, netloc.second)); + config_log.info_f( + "Note: The Devolution phone number for {} is {} (local)", + spec, devolution_phone_number_for_netloc(state->local_address, netloc.second)); } else if (state->local_address == state->external_address) { - config_log.info( - "Note: The Devolution phone number for %s is %" PRIu64 " (local+external)", - spec.c_str(), devolution_phone_number_for_netloc(state->local_address, netloc.second)); + config_log.info_f( + "Note: The Devolution phone number for {} is {} (local+external)", + spec, devolution_phone_number_for_netloc(state->local_address, netloc.second)); } else { - config_log.info( - "Note: The Devolution phone numbers for %s are %" PRIu64 " (local) and %" PRIu64 " (external)", - spec.c_str(), + config_log.info_f( + "Note: The Devolution phone numbers for {} are {} (local) and {} (external)", + spec, devolution_phone_number_for_netloc(state->local_address, netloc.second), devolution_phone_number_for_netloc(state->external_address, netloc.second)); } @@ -3344,9 +3269,8 @@ Action a_run_server_replay_log( } if (!state->http_addresses.empty() || !state->http_addresses.empty()) { - config_log.info("Starting HTTP server"); - shared_ptr shared_base = IS_WINDOWS ? state->base : nullptr; - state->http_server = make_shared(state, shared_base); + config_log.info_f("Starting HTTP server"); + state->http_server = make_shared(state); for (const auto& it : state->http_addresses) { auto netloc = phosg::parse_netloc(it); state->http_server->listen(netloc.first, netloc.second); @@ -3354,15 +3278,17 @@ Action a_run_server_replay_log( } #ifndef PHOSG_WINDOWS - config_log.info("Enabling signal watcher"); + config_log.info_f("Enabling signal watcher"); signal_watcher = make_shared(state); #endif } +#ifndef PHOSG_WINDOWS if (!state->username.empty()) { - config_log.info("Switching to user %s", state->username.c_str()); + config_log.info_f("Switching to user {}", state->username); drop_privileges(state->username); } +#endif bool should_run_shell; if (state->run_shell_behavior == ServerState::RunShellBehavior::DEFAULT) { @@ -3376,50 +3302,22 @@ Action a_run_server_replay_log( should_run_shell = !replay_session.get(); } - config_log.info("Ready"); + config_log.info_f("Ready"); if (should_run_shell) { shell = make_shared(state); } - event_base_dispatch(base.get()); + state->io_context->run(); + config_log.info_f("Normal shutdown"); - if (replay_session) { - // If in a replay session, run the event loop for a bit longer to make - // sure the server doesn't send anything unexpected after the end of - // the session. - auto tv = phosg::usecs_to_timeval(500000); - event_base_loopexit(base.get(), &tv); - event_base_dispatch(base.get()); + if (replay_session && replay_session->failed()) { + throw runtime_error("Replay failed"); } - - config_log.info("Normal shutdown"); - if (state->pc_patch_server) { - state->pc_patch_server->schedule_stop(); - } - if (state->bb_patch_server) { - state->bb_patch_server->schedule_stop(); - } - if (state->http_server) { - state->http_server->schedule_stop(); - } - if (state->pc_patch_server) { - config_log.info("Waiting for PC_V2 patch server to stop"); - state->pc_patch_server->wait_for_stop(); - } - if (state->bb_patch_server) { - config_log.info("Waiting for BB_V4 patch server to stop"); - state->bb_patch_server->wait_for_stop(); - } - if (state->http_server) { - config_log.info("Waiting for HTTP server to stop"); - state->http_server->wait_for_stop(); - } - state->proxy_server.reset(); // Break reference cycle }); void print_version_info() { string build_date = phosg::format_time(BUILD_TIMESTAMP); - fprintf(stderr, "newserv-%s built %s UTC\n", GIT_REVISION_HASH, build_date.c_str()); + phosg::fwrite_fmt(stderr, "newserv-{} built {} UTC\n", GIT_REVISION_HASH, build_date); } void print_usage() { @@ -3487,7 +3385,7 @@ int main(int argc, char** argv) { try { a = all_actions.at(action_name); } catch (const out_of_range&) { - phosg::log_error("Unknown or invalid action; try --help"); + phosg::log_error_f("Unknown or invalid action; try --help"); return 1; } if (IS_WINDOWS) { @@ -3497,19 +3395,19 @@ int main(int argc, char** argv) { try { a->run(args); } catch (const phosg::cannot_open_file& e) { - phosg::log_error("Top-level exception (cannot_open_file): %s", e.what()); + phosg::log_error_f("Top-level exception (cannot_open_file): {}", e.what()); throw; } catch (const invalid_argument& e) { - phosg::log_error("Top-level exception (invalid_argument): %s", e.what()); + phosg::log_error_f("Top-level exception (invalid_argument): {}", e.what()); throw; } catch (const out_of_range& e) { - phosg::log_error("Top-level exception (out_of_range): %s", e.what()); + phosg::log_error_f("Top-level exception (out_of_range): {}", e.what()); throw; } catch (const runtime_error& e) { - phosg::log_error("Top-level exception (runtime_error): %s", e.what()); + phosg::log_error_f("Top-level exception (runtime_error): {}", e.what()); throw; } catch (const exception& e) { - phosg::log_error("Top-level exception: %s", e.what()); + phosg::log_error_f("Top-level exception: {}", e.what()); throw; } } else { diff --git a/src/Map.cc b/src/Map.cc index af0c4f29..8c1493bb 100644 --- a/src/Map.cc +++ b/src/Map.cc @@ -26,7 +26,7 @@ string Variations::str() const { if (!ret.empty()) { ret += ","; } - ret += phosg::string_printf("%02zX:[%" PRIX32 ",%" PRIX32 "]", z, e.layout.load(), e.entities.load()); + ret += std::format("{:02X}:[{:X},{:X}]", z, e.layout, e.entities); } return ret; } @@ -35,10 +35,7 @@ phosg::JSON Variations::json() const { auto ret = phosg::JSON::list(); for (size_t z = 0; z < this->entries.size(); z++) { const auto& e = this->entries[z]; - ret.emplace_back(phosg::JSON::dict({ - {"layout", e.layout.load()}, - {"entities", e.entities.load()}, - })); + ret.emplace_back(phosg::JSON::dict({{"layout", e.layout.load()}, {"entities", e.entities.load()}})); } return ret; } @@ -46,13 +43,13 @@ phosg::JSON Variations::json() const { SetDataTableBase::SetDataTableBase(Version version) : version(version) {} Variations SetDataTableBase::generate_variations( - Episode episode, bool is_solo, shared_ptr opt_rand_crypt) const { + Episode episode, bool is_solo, shared_ptr rand_crypt) const { Variations ret; for (size_t floor = 0; floor < ret.entries.size(); floor++) { auto& e = ret.entries[floor]; auto num_vars = this->num_free_play_variations_for_floor(episode, is_solo, floor); - e.layout = (num_vars.layout > 1) ? (random_from_optional_crypt(opt_rand_crypt) % num_vars.layout) : 0; - e.entities = (num_vars.entities > 1) ? (random_from_optional_crypt(opt_rand_crypt) % num_vars.entities) : 0; + e.layout = (num_vars.layout > 1) ? (rand_crypt->next() % num_vars.layout) : 0; + e.entities = (num_vars.entities > 1) ? (rand_crypt->next() % num_vars.entities) : 0; } return ret; } @@ -236,15 +233,15 @@ string SetDataTable::map_filename_for_variation( string SetDataTable::str() const { vector lines; - lines.emplace_back(phosg::string_printf("FL/V1/V2 => ----------------------OBJECT -----------------ENEMY+EVENT -----------------------SETUP\n")); + lines.emplace_back(std::format("FL/V1/V2 => ----------------------OBJECT -----------------ENEMY+EVENT -----------------------SETUP\n")); for (size_t a = 0; a < this->entries.size(); a++) { const auto& v1_v = this->entries[a]; for (size_t v1 = 0; v1 < v1_v.size(); v1++) { const auto& v2_v = v1_v[v1]; for (size_t v2 = 0; v2 < v2_v.size(); v2++) { const auto& e = v2_v[v2]; - lines.emplace_back(phosg::string_printf("%02zX/%02zX/%02zX => %28s %28s %28s\n", - a, v1, v2, e.object_list_basename.c_str(), e.enemy_and_event_list_basename.c_str(), e.area_setup_filename.c_str())); + lines.emplace_back(std::format("{:02X}/{:02X}/{:02X} => {:28} {:28} {:28}\n", + a, v1, v2, e.object_list_basename, e.enemy_and_event_list_basename, e.area_setup_filename)); } } } @@ -1327,7 +1324,7 @@ static const vector dat_object_definitions({ // param2 = base height // param3 = area depth // param4 = launch frequency (when a firework is launched, the game - // generates a random number R in range [0, 0x7FFF] and waits + // generates a random number r in range [0, 0x7FFF] and waits // ((param4 + 60) * (r / 0x8000) * 3.0)) frames before launching the // next firework) {0x0053, F_V0_V4, 0x0000600400040001, "TObjCity_Season_FireWorkCtrl"}, @@ -1422,8 +1419,8 @@ static const vector dat_object_definitions({ // param3 = if zero, then bonuses, grinds, etc. are applied to the item // after it's generated; if nonzero, the item is not randomized at // all and drops exactly as specified in param4-6 - // param4-6 = item definition. see base_item_for_specialized_box in - // ItemCreator.cc for how these values are decoded + // param4-6 = item definition (see base_item_for_specialized_box in + // ItemCreator.cc for how these values are decoded) // In the non-specialized case (param1 <= 0), param3-6 are still sent via // the 6xA2 command when the box is opened on v3 and later, and the // server may choose to use those parameters for some purpose. The client @@ -1675,7 +1672,7 @@ static const vector dat_object_definitions({ {0x0102, F_V0_V4, 0x00000000000000C0, "TODoorMachine02"}, {0x0102, F_V4, 0x00004E0000000000, "__EP4_TEST_DOOR__"}, - // Large cryo-tube. There appear to be no parameters. + // Large cryotube. There appear to be no parameters. {0x0103, F_V0_V4, 0x00004008000000C0, "TOCapsuleMachine01"}, // Computer. Same parameters as 0x008D (TOCapsuleAncient01). @@ -1715,9 +1712,8 @@ static const vector dat_object_definitions({ // 0x2C, 0x2D, and 0x2E to determine the state of each seal on the door // (for Forest, Caves, and Mines respectively). It then checks quest flag // 0x2F; if this flag is set, then all seals are unlocked regardless of - // the preceding three flags' values. Curiously, it seems that these - // flags are checked every frame, even though it's normally impossible to - // change their values in the area where this object appears. + // the preceding three flags' values. All of these flags are checked every + // frame, not only at construction time. // No parameters. {0x0130, F_V0_V4, 0x0000400000002000, "TODoorVoShip"}, @@ -1830,18 +1826,19 @@ static const vector dat_object_definitions({ {0x015E, F_V0_V4, 0x0000400000000700, "TOWreckAncient06"}, {0x015F, F_V0_V4, 0x0000400000000700, "TOWreckAncient07"}, - // This ID constructs different objects depending on where it's used. On + // 0x0160 constructs different objects depending on where it's used. On // floor 0D (Vol Opt), it constructs TObjWarpBoss03; on other floors // where it's valid, it constructs TObjFogCollisionPoison. + // TObjWarpBoss03 creates an invisible warp. This is used for the warp + // behind the door to Ruins after defeating Vol Opt. Params: + // param4 = destination floor + {0x0160, F_V0_V4, 0x0000400000002000, "TObjWarpBoss03"}, + // TObjFogCollisionPoison creates a switchable, foggy area that's visible // and hurts the player if the switch flag isn't on. Params are the same // as for 0x0018 (TObjFogCollisionSwitch), but there is also: // param2 = poison power (scaled by difficulty: Normal = x1, Hard = x2, // Very Hard = x3, Ultimate = x6) - // TObjWarpBoss03 creates an invisible warp. This is used for the warp - // behind the door to Ruins after defeating Vol Opt. Params: - // param4 = destination floor - {0x0160, F_V0_V4, 0x0000400000002000, "TObjWarpBoss03"}, {0x0160, F_V0_V4, 0x00004FF030600700, "TObjFogCollisionPoison"}, // Ruins specialized box. Same parameters as 0x0088 (TObjContainerBase2). @@ -2095,7 +2092,7 @@ static const vector dat_object_definitions({ {0x0202, F_V3_V4, 0x0000400C0F800000, "TObjDoorJung"}, // CCA item box. Same parameters as 0x0088 (TObjContainerBase2). - // In the Episode 4 Crater areas, this object constructs 0x92 + // In the Episode 4 Crater areas, this object constructs 0x0092 // (TObjContainerBase) instead. {0x0203, F_V3_V4, 0x0000400C4F800000, "TObjContainerJungEx"}, {0x0203, F_V4, 0x000001F000000000, "TObjContainerBase(0203)"}, @@ -2142,8 +2139,8 @@ static const vector dat_object_definitions({ // Bird objects. Params: // param4 = model number? (clamped to [0, 2]) - {0x020C, F_V3_V4, 0x00004E0C0B000000, "__WHITE_BIRD__"}, // Formerly __TObjPathObj_subclass_020C__ - {0x020D, F_V3_V4, 0x000040080B000000, "__ORANGE_BIRD__"}, // Formerly __TObjPathObj_subclass_020D__ + {0x020C, F_V3_V4, 0x00004E0C0B000000, "__WHITE_BIRD__"}, + {0x020D, F_V3_V4, 0x000040080B000000, "__ORANGE_BIRD__"}, // Jungle box that triggers a wave event when opened. Params: // param4 = event number @@ -2191,7 +2188,7 @@ static const vector dat_object_definitions({ {0x0220, F_V3_V4, 0x0000400439008000, "TObjFish"}, {0x0220, F_EP3, 0x0000000000008002, "TObjFish"}, - // Seabed multiplayer door. Params: + // Seabed multiplayer doors. Params: // param4 = base switch flag number (the actual switch flags used are // param4, param4 + 1, param4 + 2, etc.; if this is negative, the // door is always unlocked) @@ -3368,7 +3365,7 @@ static string name_for_entity_type( } return ret.empty() - ? phosg::string_printf("__UNKNOWN_ENTITY_%04hX__", type) + ? std::format("__UNKNOWN_ENTITY_{:04X}__", type) : ret; } @@ -3383,26 +3380,26 @@ string MapFile::name_for_enemy_type(uint16_t type, Version version, uint8_t area string MapFile::ObjectSetEntry::str(Version version, uint8_t area) const { string name_str = MapFile::name_for_object_type(this->base_type, version, area); - return phosg::string_printf("[ObjectSetEntry type=%04hX \"%s\" floor=%04hX group=%04hX room=%04hX a3=%04hX x=%g y=%g z=%g x_angle=%08" PRIX32 " y_angle=%08" PRIX32 " z_angle=%08" PRIX32 " params=[%g %g %g %08" PRIX32 " %08" PRIX32 " %08" PRIX32 "] unused=%08" PRIX32 "]", - this->base_type.load(), - name_str.c_str(), - this->floor.load(), - this->group.load(), - this->room.load(), - this->unknown_a3.load(), - this->pos.x.load(), - this->pos.y.load(), - this->pos.z.load(), - this->angle.x.load(), - this->angle.y.load(), - this->angle.z.load(), - this->param1.load(), - this->param2.load(), - this->param3.load(), - this->param4.load(), - this->param5.load(), - this->param6.load(), - this->unused.load()); + return std::format("[ObjectSetEntry type={:04X} \"{}\" floor={:04X} group={:04X} room={:04X} a3={:04X} x={:g} y={:g} z={:g} x_angle={:08X} y_angle={:08X} z_angle={:08X} params=[{:g} {:g} {:g} {:08X} {:08X} {:08X}] unused={:08X}]", + this->base_type, + name_str, + this->floor, + this->group, + this->room, + this->unknown_a3, + this->pos.x, + this->pos.y, + this->pos.z, + this->angle.x, + this->angle.y, + this->angle.z, + this->param1, + this->param2, + this->param3, + this->param4, + this->param5, + this->param6, + this->unused); } uint64_t MapFile::ObjectSetEntry::semantic_hash(uint8_t floor) const { @@ -3423,29 +3420,29 @@ uint64_t MapFile::ObjectSetEntry::semantic_hash(uint8_t floor) const { string MapFile::EnemySetEntry::str(Version version, uint8_t area) const { auto type_name = MapFile::name_for_enemy_type(this->base_type, version, area); - return phosg::string_printf("[EnemySetEntry type=%04hX \"%s\" num_children=%04hX floor=%04hX room=%04hX wave_number=%04hX wave_number2=%04hX a1=%04hX x=%g y=%g z=%g x_angle=%08" PRIX32 " y_angle=%08" PRIX32 " z_angle=%08" PRIX32 " params=[%g %g %g %g %g %04hX %04hX] unused=%08" PRIX32 "]", - this->base_type.load(), - type_name.c_str(), - this->num_children.load(), - this->floor.load(), - this->room.load(), - this->wave_number.load(), - this->wave_number2.load(), - this->unknown_a1.load(), - this->pos.x.load(), - this->pos.y.load(), - this->pos.z.load(), - this->angle.x.load(), - this->angle.y.load(), - this->angle.z.load(), - this->param1.load(), - this->param2.load(), - this->param3.load(), - this->param4.load(), - this->param5.load(), - this->param6.load(), - this->param7.load(), - this->unused.load()); + return std::format("[EnemySetEntry type={:04X} \"{}\" num_children={:04X} floor={:04X} room={:04X} wave_number={:04X} wave_number2={:04X} a1={:04X} x={:g} y={:g} z={:g} x_angle={:08X} y_angle={:08X} z_angle={:08X} params=[{:g} {:g} {:g} {:g} {:g} {:04X} {:04X}] unused={:08X}]", + this->base_type, + type_name, + this->num_children, + this->floor, + this->room, + this->wave_number, + this->wave_number2, + this->unknown_a1, + this->pos.x, + this->pos.y, + this->pos.z, + this->angle.x, + this->angle.y, + this->angle.z, + this->param1, + this->param2, + this->param3, + this->param4, + this->param5, + this->param6, + this->param7, + this->unused); } uint64_t MapFile::EnemySetEntry::semantic_hash(uint8_t floor) const { @@ -3468,14 +3465,14 @@ uint64_t MapFile::EnemySetEntry::semantic_hash(uint8_t floor) const { } string MapFile::Event1Entry::str() const { - return phosg::string_printf("[Event1Entry event_id=%08" PRIX32 " flags=%04hX event_type=%04hX room=%04hX wave_number=%04hX delay=%08" PRIX32 " action_stream_offset=%08" PRIX32 "]", - this->event_id.load(), - this->flags.load(), - this->event_type.load(), - this->room.load(), - this->wave_number.load(), - this->delay.load(), - this->action_stream_offset.load()); + return std::format("[Event1Entry event_id={:08X} flags={:04X} event_type={:04X} room={:04X} wave_number={:04X} delay={:08X} action_stream_offset={:08X}]", + this->event_id, + this->flags, + this->event_type, + this->room, + this->wave_number, + this->delay, + this->action_stream_offset); } uint64_t MapFile::Event1Entry::semantic_hash(uint8_t floor) const { @@ -3487,48 +3484,48 @@ uint64_t MapFile::Event1Entry::semantic_hash(uint8_t floor) const { } string MapFile::Event2Entry::str() const { - return phosg::string_printf("[Event2Entry event_id=%08" PRIX32 " flags=%04hX event_type=%04hX room=%04hX wave_number=%04hX min_delay=%08" PRIX32 " max_delay=%08" PRIX32 " min_enemies=%02hhX max_enemies=%02hhX max_waves=%04hX action_stream_offset=%08" PRIX32 "]", - this->event_id.load(), - this->flags.load(), - this->event_type.load(), - this->room.load(), - this->wave_number.load(), - this->min_delay.load(), - this->max_delay.load(), + return std::format("[Event2Entry event_id={:08X} flags={:04X} event_type={:04X} room={:04X} wave_number={:04X} min_delay={:08X} max_delay={:08X} min_enemies={:02X} max_enemies={:02X} max_waves={:04X} action_stream_offset={:08X}]", + this->event_id, + this->flags, + this->event_type, + this->room, + this->wave_number, + this->min_delay, + this->max_delay, this->min_enemies, this->max_enemies, - this->max_waves.load(), - this->action_stream_offset.load()); + this->max_waves, + this->action_stream_offset); } string MapFile::RandomEnemyLocationEntry::str() const { - return phosg::string_printf("[RandomEnemyLocationEntry x=%g y=%g z=%g x_angle=%08" PRIX32 " y_angle=%08" PRIX32 " z_angle=%08" PRIX32 "a9=%04hX a10=%04hX]", - this->pos.x.load(), - this->pos.y.load(), - this->pos.z.load(), - this->angle.x.load(), - this->angle.y.load(), - this->angle.z.load(), - this->unknown_a9.load(), - this->unknown_a10.load()); + return std::format("[RandomEnemyLocationEntry x={:g} y={:g} z={:g} x_angle={:08X} y_angle={:08X} z_angle={:08X}a9={:04X} a10={:04X}]", + this->pos.x, + this->pos.y, + this->pos.z, + this->angle.x, + this->angle.y, + this->angle.z, + this->unknown_a9, + this->unknown_a10); } string MapFile::RandomEnemyDefinition::str() const { - return phosg::string_printf("[RandomEnemyDefinition params=[%g %g %g %g %g %04hX %04hX] entry_num=%08" PRIX32 " min_children=%04hX max_children=%04hX]", - this->param1.load(), - this->param2.load(), - this->param3.load(), - this->param4.load(), - this->param5.load(), - this->param6.load(), - this->param7.load(), - this->entry_num.load(), - this->min_children.load(), - this->max_children.load()); + return std::format("[RandomEnemyDefinition params=[{:g} {:g} {:g} {:g} {:g} {:04X} {:04X}] entry_num={:08X} min_children={:04X} max_children={:04X}]", + this->param1, + this->param2, + this->param3, + this->param4, + this->param5, + this->param6, + this->param7, + this->entry_num, + this->min_children, + this->max_children); } string MapFile::RandomEnemyWeight::str() const { - return phosg::string_printf("[RandomEnemyWeight base_type_index=%02hhX def_entry_num=%02hhX weight=%02hhX a4=%02hhX]", + return std::format("[RandomEnemyWeight base_type_index={:02X} def_entry_num={:02X} weight={:02X} a4={:02X}]", this->base_type_index, this->def_entry_num, this->weight, @@ -3614,7 +3611,7 @@ MapFile::MapFile(std::shared_ptr data) { break; } if (header.section_size < sizeof(header)) { - throw runtime_error(phosg::string_printf("quest entities list has invalid section header at offset 0x%zX", r.where() - sizeof(header))); + throw runtime_error(std::format("quest entities list has invalid section header at offset 0x{:X}", r.where() - sizeof(header))); } if (header.floor >= this->sections_for_floor.size()) { @@ -4015,51 +4012,51 @@ string MapFile::disassemble_action_stream(const void* data, size_t size) { uint8_t opcode = r.get_u8(); switch (opcode) { case 0x00: - ret.emplace_back(phosg::string_printf(" 00 nop")); + ret.emplace_back(std::format(" 00 nop")); break; case 0x01: - ret.emplace_back(phosg::string_printf(" 01 stop")); + ret.emplace_back(std::format(" 01 stop")); r.go(r.size()); break; case 0x08: { uint16_t room = r.get_u16l(); uint16_t group = r.get_u16l(); - ret.emplace_back(phosg::string_printf(" 08 %04hX %04hX construct_objects room=%04hX group=%04hX", + ret.emplace_back(std::format(" 08 {:04X} {:04X} construct_objects room={:04X} group={:04X}", room, group, room, group)); break; } case 0x09: { uint16_t room = r.get_u16l(); uint16_t wave_number = r.get_u16l(); - ret.emplace_back(phosg::string_printf(" 09 %04hX %04hX construct_enemies room=%04hX wave_number=%04hX", + ret.emplace_back(std::format(" 09 {:04X} {:04X} construct_enemies room={:04X} wave_number={:04X}", room, wave_number, room, wave_number)); break; } case 0x0A: { uint16_t id = r.get_u16l(); - ret.emplace_back(phosg::string_printf(" 0A %04hX enable_switch_flag id=%04hX", id, id)); + ret.emplace_back(std::format(" 0A {:04X} enable_switch_flag id={:04X}", id, id)); break; } case 0x0B: { uint16_t id = r.get_u16l(); - ret.emplace_back(phosg::string_printf(" 0B %04hX disable_switch_flag id=%04hX", id, id)); + ret.emplace_back(std::format(" 0B {:04X} disable_switch_flag id={:04X}", id, id)); break; } case 0x0C: { uint32_t event_id = r.get_u32l(); - ret.emplace_back(phosg::string_printf(" 0C %08" PRIX32 " trigger_event event_id=%08" PRIX32, event_id, event_id)); + ret.emplace_back(std::format(" 0C {:08X} trigger_event event_id={:08X}", event_id, event_id)); break; } case 0x0D: { uint16_t room = r.get_u16l(); uint16_t wave_number = r.get_u16l(); - ret.emplace_back(phosg::string_printf(" 0D %04hX %04hX construct_enemies_stop room=%04hX wave_number=%04hX", + ret.emplace_back(std::format(" 0D {:04X} {:04X} construct_enemies_stop room={:04X} wave_number={:04X}", room, wave_number, room, wave_number)); r.go(r.size()); break; } default: - ret.emplace_back(phosg::string_printf(" %02hhX .invalid", opcode)); + ret.emplace_back(std::format(" {:02X} .invalid", opcode)); } } @@ -4074,9 +4071,9 @@ string MapFile::disassemble(bool reassembly, Version version) const { if (sf.object_sets) { if (reassembly) { - ret.emplace_back(phosg::string_printf(".object_sets %hhu", floor)); + ret.emplace_back(std::format(".object_sets {}", floor)); } else { - ret.emplace_back(phosg::string_printf(".object_sets %hhu /* 0x%zX in file; 0x%zX bytes */", + ret.emplace_back(std::format(".object_sets {} /* 0x{:X} in file; 0x{:X} bytes */", floor, sf.object_sets_file_offset, sf.object_sets_file_size)); } for (size_t z = 0; z < sf.object_set_count; z++) { @@ -4084,15 +4081,15 @@ string MapFile::disassemble(bool reassembly, Version version) const { ret.emplace_back(sf.object_sets[z].str(version)); } else { size_t k_id = z + sf.first_object_set_index; - ret.emplace_back(phosg::string_printf("/* K-%03zX */ ", k_id) + sf.object_sets[z].str(version)); + ret.emplace_back(std::format("/* K-{:03X} */ ", k_id) + sf.object_sets[z].str(version)); } } } if (sf.enemy_sets) { if (reassembly) { - ret.emplace_back(phosg::string_printf(".enemy_sets %hhu", floor)); + ret.emplace_back(std::format(".enemy_sets {}", floor)); } else { - ret.emplace_back(phosg::string_printf(".enemy_sets %hhu /* 0x%zX in file; 0x%zX bytes */", + ret.emplace_back(std::format(".enemy_sets {} /* 0x{:X} in file; 0x{:X} bytes */", floor, sf.enemy_sets_file_offset, sf.enemy_sets_file_size)); } for (size_t z = 0; z < sf.enemy_set_count; z++) { @@ -4100,15 +4097,15 @@ string MapFile::disassemble(bool reassembly, Version version) const { ret.emplace_back(sf.enemy_sets[z].str(version)); } else { size_t s_id = z + sf.first_enemy_set_index; - ret.emplace_back(phosg::string_printf("/* S-%03zX */ ", s_id) + sf.enemy_sets[z].str(version)); + ret.emplace_back(std::format("/* S-{:03X} */ ", s_id) + sf.enemy_sets[z].str(version)); } } } if (sf.events1) { if (reassembly) { - ret.emplace_back(phosg::string_printf(".events %hhu", floor)); + ret.emplace_back(std::format(".events {}", floor)); } else { - ret.emplace_back(phosg::string_printf(".events %hhu /* 0x%zX in file; 0x%zX bytes; 0x%zX bytes in action stream */", + ret.emplace_back(std::format(".events {} /* 0x{:X} in file; 0x{:X} bytes; 0x{:X} bytes in action stream */", floor, sf.events_file_offset, sf.events_file_size, sf.event_action_stream_bytes)); } for (size_t z = 0; z < sf.event_count; z++) { @@ -4117,12 +4114,12 @@ string MapFile::disassemble(bool reassembly, Version version) const { ret.emplace_back(ev.str()); } else { size_t w_id = z + sf.first_event_set_index; - ret.emplace_back(phosg::string_printf("/* W-%03zX */ ", w_id) + ev.str()); + ret.emplace_back(std::format("/* W-{:03X} */ ", w_id) + ev.str()); } if (ev.action_stream_offset >= sf.event_action_stream_bytes) { - ret.emplace_back(phosg::string_printf( - " // WARNING: Event action stream offset (0x%" PRIX32 ") is outside of this section", - ev.action_stream_offset.load())); + ret.emplace_back(std::format( + " // WARNING: Event action stream offset (0x{:X}) is outside of this section", + ev.action_stream_offset)); } size_t as_size = as_r.size() - ev.action_stream_offset; ret.emplace_back(this->disassemble_action_stream(as_r.pgetv(ev.action_stream_offset, as_size), as_size)); @@ -4130,10 +4127,10 @@ string MapFile::disassemble(bool reassembly, Version version) const { } if (sf.events2) { if (reassembly) { - ret.emplace_back(phosg::string_printf(".random_events %hhu", floor)); + ret.emplace_back(std::format(".random_events {}", floor)); } else { - ret.emplace_back(phosg::string_printf( - ".random_events %hhu /* 0x%zX in file; 0x%zX bytes; 0x%zX bytes in action stream */", + ret.emplace_back(std::format( + ".random_events {} /* 0x{:X} in file; 0x{:X} bytes; 0x{:X} bytes in action stream */", floor, sf.events_file_offset, sf.events_file_size, sf.event_action_stream_bytes)); } for (size_t z = 0; z < sf.event_count; z++) { @@ -4141,12 +4138,12 @@ string MapFile::disassemble(bool reassembly, Version version) const { if (reassembly) { ret.emplace_back(ev.str()); } else { - ret.emplace_back(phosg::string_printf("/* index %zu */", z) + ev.str()); + ret.emplace_back(std::format("/* index {} */", z) + ev.str()); } if (ev.action_stream_offset >= sf.event_action_stream_bytes) { - ret.emplace_back(phosg::string_printf( - " // WARNING: Event action stream offset (0x%" PRIX32 ") is outside of this section", - ev.action_stream_offset.load())); + ret.emplace_back(std::format( + " // WARNING: Event action stream offset (0x{:X}) is outside of this section", + ev.action_stream_offset)); } size_t as_size = as_r.size() - ev.action_stream_offset; ret.emplace_back(this->disassemble_action_stream(as_r.pgetv(ev.action_stream_offset, as_size), as_size)); @@ -4154,18 +4151,18 @@ string MapFile::disassemble(bool reassembly, Version version) const { } if (sf.random_enemy_locations_data) { if (reassembly) { - ret.emplace_back(phosg::string_printf(".random_enemy_locations %hhu", floor)); + ret.emplace_back(std::format(".random_enemy_locations {}", floor)); } else { - ret.emplace_back(phosg::string_printf(".random_enemy_locations %hhu /* 0x%zX in file; 0x%zX bytes */", + ret.emplace_back(std::format(".random_enemy_locations {} /* 0x{:X} in file; 0x{:X} bytes */", floor, sf.random_enemy_locations_file_offset, sf.random_enemy_locations_file_size)); } ret.emplace_back(phosg::format_data(sf.random_enemy_locations_data, sf.random_enemy_locations_data_size)); } if (sf.random_enemy_definitions_data) { if (reassembly) { - ret.emplace_back(phosg::string_printf(".random_enemy_definitions %hhu", floor)); + ret.emplace_back(std::format(".random_enemy_definitions {}", floor)); } else { - ret.emplace_back(phosg::string_printf(".random_enemy_definitions %hhu /* 0x%zX in file; 0x%zX bytes */", + ret.emplace_back(std::format(".random_enemy_definitions {} /* 0x{:X} in file; 0x{:X} bytes */", floor, sf.random_enemy_definitions_file_offset, sf.random_enemy_definitions_file_size)); } ret.emplace_back(phosg::format_data(sf.random_enemy_definitions_data, sf.random_enemy_definitions_data_size)); @@ -4178,7 +4175,7 @@ string MapFile::disassemble(bool reassembly, Version version) const { // Super map string SuperMap::Object::id_str() const { - return phosg::string_printf("KS-%02hhX-%03zX", this->floor, this->super_id); + return std::format("KS-{:02X}-{:03X}", this->floor, this->super_id); } string SuperMap::Object::str() const { @@ -4187,8 +4184,8 @@ string SuperMap::Object::str() const { const auto& def = this->version(v); if (def.relative_object_index != 0xFFFF) { string args_str = def.set_entry->str(v); - ret += phosg::string_printf( - " %s:[%04hX => %s]", phosg::name_for_enum(v), def.relative_object_index, args_str.c_str()); + ret += std::format( + " {}:[{:04X} => {}]", phosg::name_for_enum(v), def.relative_object_index, args_str); } } ret += "]"; @@ -4196,11 +4193,11 @@ string SuperMap::Object::str() const { } string SuperMap::Enemy::id_str() const { - return phosg::string_printf("ES-%02hhX-%03zX-%03zX", this->floor, this->super_set_id, this->super_id); + return std::format("ES-{:02X}-{:03X}-{:03X}", this->floor, this->super_set_id, this->super_id); } string SuperMap::Enemy::str() const { - string ret = phosg::string_printf("[Enemy ES-%02hhX-%03zX-%03zX type=%s child_index=%hX alias_enemy_index_delta=%hX is_default_rare_v123=%s is_default_rare_bb=%s", + string ret = std::format("[Enemy ES-{:02X}-{:03X}-{:03X} type={} child_index={:X} alias_enemy_index_delta={:X} is_default_rare_v123={} is_default_rare_bb={}", this->floor, this->super_set_id, this->super_id, @@ -4213,12 +4210,12 @@ string SuperMap::Enemy::str() const { const auto& def = this->version(v); if (def.relative_enemy_index != 0xFFFF) { string args_str = def.set_entry->str(v); - ret += phosg::string_printf( - " %s:[%04hX/%04hX => %s]", + ret += std::format( + " {}:[{:04X}/{:04X} => {}]", phosg::name_for_enum(v), def.relative_set_index, def.relative_enemy_index, - args_str.c_str()); + args_str); } } ret += "]"; @@ -4226,7 +4223,7 @@ string SuperMap::Enemy::str() const { } string SuperMap::Event::id_str() const { - return phosg::string_printf("WS-%02hhX-%03zX", this->floor, this->super_id); + return std::format("WS-{:02X}-{:03X}", this->floor, this->super_id); } string SuperMap::Event::str() const { @@ -4236,12 +4233,12 @@ string SuperMap::Event::str() const { if (def.relative_event_index != 0xFFFF) { string action_stream_str = phosg::format_data_string(def.action_stream, def.action_stream_size); string args_str = def.set_entry->str(); - ret += phosg::string_printf( - " %s:[%04hX => %s+%s]", + ret += std::format( + " {}:[{:04X} => {}+{}]", phosg::name_for_enum(v), def.relative_event_index, - args_str.c_str(), - action_stream_str.c_str()); + args_str, + action_stream_str); } } ret += "]"; @@ -4466,7 +4463,7 @@ shared_ptr SuperMap::add_enemy_and_children( add(EnemyType::NON_ENEMY_NPC); break; case 0x0040: { // TObjEneMoja - bool is_rare = (set_entry->param6.load() >= 1); + bool is_rare = (set_entry->param6 >= 1); add(EnemyType::HILDEBEAR, is_rare, is_rare); break; } @@ -4526,7 +4523,7 @@ shared_ptr SuperMap::add_enemy_and_children( } case 0x0065: // TObjEnePanarms if ((set_entry->num_children != 0) && (set_entry->num_children != 2)) { - this->log.warning("PAN_ARMS has an unusual num_children (0x%hX)", set_entry->num_children.load()); + this->log.warning_f("PAN_ARMS has an unusual num_children (0x{:X})", set_entry->num_children); } default_num_children = -1; // Skip adding children (because we do it here) add(EnemyType::PAN_ARMS); @@ -4559,7 +4556,7 @@ shared_ptr SuperMap::add_enemy_and_children( break; case 0x00A1: // TObjEneRe4Sorcerer if ((set_entry->num_children != 0) && (set_entry->num_children != 2)) { - this->log.warning("CHAOS_SORCERER has an unusual num_children (0x%hX)", set_entry->num_children.load()); + this->log.warning_f("CHAOS_SORCERER has an unusual num_children (0x{:X})", set_entry->num_children); } default_num_children = -1; // Skip adding children (because we do it here) add(EnemyType::CHAOS_SORCERER); @@ -4602,7 +4599,7 @@ shared_ptr SuperMap::add_enemy_and_children( break; case 0x00C1: // TBoss2DeRolLe if ((set_entry->num_children != 0) && (set_entry->num_children != 0x13)) { - this->log.warning("DE_ROL_LE has an unusual num_children (0x%hX)", set_entry->num_children.load()); + this->log.warning_f("DE_ROL_LE has an unusual num_children (0x{:X})", set_entry->num_children); } default_num_children = -1; // Skip adding children (because we do it here) add(EnemyType::DE_ROL_LE); @@ -4615,7 +4612,7 @@ shared_ptr SuperMap::add_enemy_and_children( break; case 0x00C2: // TBoss3Volopt if ((set_entry->num_children != 0) && (set_entry->num_children != 0x23)) { - this->log.warning("VOL_OPT has an unusual num_children (0x%hX)", set_entry->num_children.load()); + this->log.warning_f("VOL_OPT has an unusual num_children (0x{:X})", set_entry->num_children); } default_num_children = -1; // Skip adding children (because we do it here) add(EnemyType::VOL_OPT_1); @@ -4637,7 +4634,7 @@ shared_ptr SuperMap::add_enemy_and_children( break; case 0x00C8: // TBoss4DarkFalz if ((set_entry->num_children != 0) && (set_entry->num_children != 0x200)) { - this->log.warning("DARK_FALZ has an unusual num_children (0x%hX)", set_entry->num_children.load()); + this->log.warning_f("DARK_FALZ has an unusual num_children (0x{:X})", set_entry->num_children); } add(EnemyType::DARK_FALZ_3); default_num_children = -1; // Skip adding children (because we do it here) @@ -4805,7 +4802,7 @@ shared_ptr SuperMap::add_enemy_and_children( default: add(EnemyType::UNKNOWN); - this->log.warning("Invalid enemy type %04hX", set_entry->base_type.load()); + this->log.warning_f("Invalid enemy type {:04X}", set_entry->base_type); break; } @@ -4921,9 +4918,9 @@ void SuperMap::link_event_version( size_t map_file_action_stream_size) { if (entry->action_stream_offset >= map_file_action_stream_size) { string s = entry->str(); - throw runtime_error(phosg::string_printf( - "action stream offset 0x%" PRIX32 " is beyond end of action stream (0x%zX) for event %s", - entry->action_stream_offset.load(), map_file_action_stream_size, s.c_str())); + throw runtime_error(std::format( + "action stream offset 0x{:X} is beyond end of action stream (0x{:X}) for event {}", + entry->action_stream_offset, map_file_action_stream_size, s)); } const void* ev_action_stream_start = reinterpret_cast(map_file_action_stream) + entry->action_stream_offset; @@ -5014,7 +5011,7 @@ vector compute_edit_path( action_ch = 'D'; break; } - fprintf(stream, " %c %03.2g", action_ch, entry.cost); + phosg::fwrite_fmt(stream, " {} {:03.2g}", action_ch, entry.cost); } fputc('\n', stream); } @@ -5483,8 +5480,8 @@ std::string SuperMap::EfficiencyStats::str() const { double event_eff = this->total_event_slots ? (static_cast(this->filled_event_slots * 100) / static_cast(this->total_event_slots)) : 0; - return phosg::string_printf( - "EfficiencyStats[K = %zu/%zu (%lg%%), E = %zu/%zu (%lg%%), W = %zu/%zu (%g%%)]", + return std::format( + "EfficiencyStats[K = {}/{} ({:g}%), E = {}/{} ({:g}%), W = {}/{} ({:g}%)]", this->filled_object_slots, this->total_object_slots, object_eff, this->filled_enemy_set_slots, this->total_enemy_set_slots, enemy_set_eff, this->filled_event_slots, this->total_event_slots, event_eff); @@ -5550,14 +5547,14 @@ void SuperMap::verify() const { } } if (ene->super_set_id != super_set_id) { - throw logic_error(phosg::string_printf( - "enemy super_set_id is incorrect; expected S-%03zX, received S-%03zX", + throw logic_error(std::format( + "enemy super_set_id is incorrect; expected S-{:03X}, received S-{:03X}", super_set_id, ene->super_set_id)); } } if (super_set_id != this->enemy_sets.size() - 1) { - throw logic_error(phosg::string_printf( - "not all enemy sets are in the enemies list; ended with 0x%zX, expected 0x%zX", + throw logic_error(std::format( + "not all enemy sets are in the enemies list; ended with 0x{:X}, expected 0x{:X}", super_set_id, this->enemy_sets.size())); } } @@ -5688,92 +5685,92 @@ void SuperMap::verify() const { } void SuperMap::print(FILE* stream) const { - fprintf(stream, "SuperMap %s random=%08" PRIX64 "\n", name_for_episode(this->episode), this->random_seed); + phosg::fwrite_fmt(stream, "SuperMap {} random={:08X}\n", name_for_episode(this->episode), this->random_seed); - fprintf(stream, " DCTE DCPR DCV1 DCV2 PCTE PCV2 GCTE GCV3 E3TE GCE3 XBV3 BBV4\n"); - fprintf(stream, " MAP "); + phosg::fwrite_fmt(stream, " DCTE DCPR DCV1 DCV2 PCTE PCV2 GCTE GCV3 E3TE GCE3 XBV3 BBV4\n"); + phosg::fwrite_fmt(stream, " MAP "); for (const auto& v : ALL_NON_PATCH_VERSIONS) { const auto& entities = this->version(v); - fprintf(stream, " %s", entities.map_file ? "++++" : "----"); + phosg::fwrite_fmt(stream, " {}", entities.map_file ? "++++" : "----"); } fputc('\n', stream); for (uint8_t floor = 0; floor < 0x12; floor++) { - fprintf(stream, " KS START %02hhX", floor); + phosg::fwrite_fmt(stream, " KS START {:02X}", floor); for (const auto& v : ALL_NON_PATCH_VERSIONS) { const auto& entities = this->version(v); - fprintf(stream, " %04zX", entities.object_floor_start_indexes[floor]); + phosg::fwrite_fmt(stream, " {:04X}", entities.object_floor_start_indexes[floor]); } fputc('\n', stream); } for (uint8_t floor = 0; floor < 0x12; floor++) { - fprintf(stream, " ES START %02hhX", floor); + phosg::fwrite_fmt(stream, " ES START {:02X}", floor); for (const auto& v : ALL_NON_PATCH_VERSIONS) { const auto& entities = this->version(v); - fprintf(stream, " %04zX", entities.enemy_floor_start_indexes[floor]); + phosg::fwrite_fmt(stream, " {:04X}", entities.enemy_floor_start_indexes[floor]); } fputc('\n', stream); } for (uint8_t floor = 0; floor < 0x12; floor++) { - fprintf(stream, " ESS START %02hhX", floor); + phosg::fwrite_fmt(stream, " ESS START {:02X}", floor); for (const auto& v : ALL_NON_PATCH_VERSIONS) { const auto& entities = this->version(v); - fprintf(stream, " %04zX", entities.enemy_set_floor_start_indexes[floor]); + phosg::fwrite_fmt(stream, " {:04X}", entities.enemy_set_floor_start_indexes[floor]); } fputc('\n', stream); } for (uint8_t floor = 0; floor < 0x12; floor++) { - fprintf(stream, " WS START %02hhX", floor); + phosg::fwrite_fmt(stream, " WS START {:02X}", floor); for (const auto& v : ALL_NON_PATCH_VERSIONS) { const auto& entities = this->version(v); - fprintf(stream, " %04zX", entities.event_floor_start_indexes[floor]); + phosg::fwrite_fmt(stream, " {:04X}", entities.event_floor_start_indexes[floor]); } fputc('\n', stream); } - fprintf(stream, " KS-FL-ID DCTE DCPR DCV1 DCV2 PCTE PCV2 GCTE GCV3 E3TE GCE3 XBV3 BBV4 DEFINITION\n"); + phosg::fwrite_fmt(stream, " KS-FL-ID DCTE DCPR DCV1 DCV2 PCTE PCV2 GCTE GCV3 E3TE GCE3 XBV3 BBV4 DEFINITION\n"); for (const auto& obj : this->objects) { - fprintf(stream, " KS-%02hhX-%03zX", obj->floor, obj->super_id); + phosg::fwrite_fmt(stream, " KS-{:02X}-{:03X}", obj->floor, obj->super_id); for (Version v : ALL_NON_PATCH_VERSIONS) { const auto& obj_ver = obj->version(v); if (obj_ver.relative_object_index == 0xFFFF) { - fprintf(stream, " ----"); + phosg::fwrite_fmt(stream, " ----"); } else { - fprintf(stream, " %04hX", obj_ver.relative_object_index); + phosg::fwrite_fmt(stream, " {:04X}", obj_ver.relative_object_index); } } auto obj_str = obj->str(); - fprintf(stream, " %s\n", obj_str.c_str()); + phosg::fwrite_fmt(stream, " {}\n", obj_str); } - fprintf(stream, " ES-FL-ID DCTE----- DCPR----- DCV1----- DCV2----- PCTE----- PCV2----- GCTE----- GCV3----- EP3TE---- GCEP3---- XBV3----- BBV4----- DEFINITION\n"); + phosg::fwrite_fmt(stream, " ES-FL-ID DCTE----- DCPR----- DCV1----- DCV2----- PCTE----- PCV2----- GCTE----- GCV3----- EP3TE---- GCEP3---- XBV3----- BBV4----- DEFINITION\n"); for (const auto& ene : this->enemies) { - fprintf(stream, " ES-%02hhX-%03zX", ene->floor, ene->super_id); + phosg::fwrite_fmt(stream, " ES-{:02X}-{:03X}", ene->floor, ene->super_id); for (Version v : ALL_NON_PATCH_VERSIONS) { const auto& ene_ver = ene->version(v); if (ene_ver.relative_enemy_index == 0xFFFF) { - fprintf(stream, " ----:----"); + phosg::fwrite_fmt(stream, " ----:----"); } else { - fprintf(stream, " %04hX:%04hX", ene_ver.relative_set_index, ene_ver.relative_enemy_index); + phosg::fwrite_fmt(stream, " {:04X}:{:04X}", ene_ver.relative_set_index, ene_ver.relative_enemy_index); } } auto ene_str = ene->str(); - fprintf(stream, " %s\n", ene_str.c_str()); + phosg::fwrite_fmt(stream, " {}\n", ene_str); } - fprintf(stream, " WS-FL-ID DCTE DCPR DCV1 DCV2 PCTE PCV2 GCTE GCV3 E3TE GCE3 XBV3 BBV4 DEFINITION\n"); + phosg::fwrite_fmt(stream, " WS-FL-ID DCTE DCPR DCV1 DCV2 PCTE PCV2 GCTE GCV3 E3TE GCE3 XBV3 BBV4 DEFINITION\n"); for (const auto& ev : this->events) { - fprintf(stream, " WS-%02hhX-%03zX", ev->floor, ev->super_id); + phosg::fwrite_fmt(stream, " WS-{:02X}-{:03X}", ev->floor, ev->super_id); for (Version v : ALL_NON_PATCH_VERSIONS) { const auto& ev_ver = ev->version(v); if (ev_ver.relative_event_index == 0xFFFF) { - fprintf(stream, " ----"); + phosg::fwrite_fmt(stream, " ----"); } else { - fprintf(stream, " %04hX", ev_ver.relative_event_index); + phosg::fwrite_fmt(stream, " {:04X}", ev_ver.relative_event_index); } } auto ev_str = ev->str(); - fprintf(stream, " %s\n", ev_str.c_str()); + phosg::fwrite_fmt(stream, " {}\n", ev_str); } } @@ -5803,7 +5800,7 @@ MapState::RareEnemyRates::RareEnemyRates(const phosg::JSON& json) kondrieu(json.get_int("Kondrieu", DEFAULT_RARE_BOSS_RATE_V4)) {} string MapState::RareEnemyRates::str() const { - return phosg::string_printf("RareEnemyRates(hildeblue=%08" PRIX32 ", rappy=%08" PRIX32 ", nar_lily=%08" PRIX32 ", pouilly_slime=%08" PRIX32 ", mericarand=%08" PRIX32 ", merissa_aa=%08" PRIX32 ", pazuzu=%08" PRIX32 ", dorphon_eclair=%08" PRIX32 ", kondrieu=%08" PRIX32 ")", + return std::format("RareEnemyRates(hildeblue={:08X}, rappy={:08X}, nar_lily={:08X}, pouilly_slime={:08X}, mericarand={:08X}, merissa_aa={:08X}, pazuzu={:08X}, dorphon_eclair={:08X}, kondrieu={:08X})", this->hildeblue, this->rappy, this->nar_lily, this->pouilly_slime, this->mericarand, this->merissa_aa, this->pazuzu, this->dorphon_eclair, this->kondrieu); } @@ -5876,8 +5873,16 @@ uint32_t MapState::EnemyState::convert_game_flags(uint32_t game_flags, bool to_v // y = is near enemy // H = is enemy? // I = is object? (some entities have both H and I set though) - // It could be that the flags 0x70000000 are actually a 3-bit integer rather - // than individual flags. TODO: Investigate this. + + // TODO: The above might all be wrong. + // GC 00100000 10010000 00001110 00000000 + // PC 00101001 00000000 01100100 00000000 + + // PC 00101001 10110000 00101110 00000000 + // GC 00100000 10011011 00000111 00000000 + + // PC 00101001 10010000 00101110 00000000 + // GC 00100000 10011001 00000111 00000000 if (to_v3) { return (game_flags & 0xE00000FF) | @@ -5977,9 +5982,9 @@ MapState::MapState( uint8_t event, uint32_t random_seed, std::shared_ptr bb_rare_rates, - std::shared_ptr opt_rand_crypt, + std::shared_ptr rand_crypt, std::vector> floor_map_defs) - : log(phosg::string_printf("[MapState(free):%08" PRIX64 "] ", lobby_or_session_id), lobby_log.min_level), + : log(std::format("[MapState(free):{:08X}] ", lobby_or_session_id), lobby_log.min_level), difficulty(difficulty), event(event), random_seed(random_seed), @@ -5990,7 +5995,7 @@ MapState::MapState( auto& this_fc = this->floor_config_entries[floor]; this_fc.super_map = (floor < floor_map_defs.size()) ? floor_map_defs[floor] : nullptr; if (this_fc.super_map) { - this->index_super_map(this_fc, opt_rand_crypt); + this->index_super_map(this_fc, rand_crypt); } if (floor < this->floor_config_entries.size() - 1) { @@ -6027,16 +6032,16 @@ MapState::MapState( uint8_t event, uint32_t random_seed, std::shared_ptr bb_rare_rates, - std::shared_ptr opt_rand_crypt, + std::shared_ptr rand_crypt, std::shared_ptr quest_map_def) - : log(phosg::string_printf("[MapState(free):%08" PRIX64 "] ", lobby_or_session_id), lobby_log.min_level), + : log(std::format("[MapState(free):{:08X}] ", lobby_or_session_id), lobby_log.min_level), difficulty(difficulty), event(event), random_seed(random_seed), bb_rare_rates(bb_rare_rates) { FloorConfig& fc = this->floor_config_entries.emplace_back(); fc.super_map = quest_map_def; - this->index_super_map(fc, opt_rand_crypt); + this->index_super_map(fc, rand_crypt); this->compute_dynamic_object_base_indexes(); this->verify(); } @@ -6057,7 +6062,7 @@ void MapState::reset() { } } -void MapState::index_super_map(const FloorConfig& fc, shared_ptr opt_rand_crypt) { +void MapState::index_super_map(const FloorConfig& fc, shared_ptr rand_crypt) { if (!fc.super_map) { throw logic_error("cannot index floor config with no map definition"); } @@ -6148,9 +6153,7 @@ void MapState::index_super_map(const FloorConfig& fc, shared_ptr 0) && - (this->bb_rare_enemy_indexes.size() < 0x10) && - (random_from_optional_crypt(opt_rand_crypt) < bb_rare_rate)) { + } else if ((bb_rare_rate > 0) && (this->bb_rare_enemy_indexes.size() < 0x10) && (rand_crypt->next() < bb_rare_rate)) { this->bb_rare_enemy_indexes.emplace_back(enemy_index); ene_st->set_rare(v); if ((type == EnemyType::MERICARAND) && (enemy_index & 1)) { @@ -6364,7 +6367,7 @@ vector> MapState::event_states_for_floor_room_w void MapState::import_object_states_from_sync( Version from_version, const SyncObjectStateEntry* entries, size_t entry_count) { - this->log.info("Importing object state from sync command"); + this->log.info_f("Importing object state from sync command"); size_t object_index = 0; for (const auto& fc : this->floor_config_entries) { if (!fc.super_map) { @@ -6382,8 +6385,8 @@ void MapState::import_object_states_from_sync( if (from_version == Version::DC_NTE) { fc_end_object_index = entry_count; } else { - throw runtime_error(phosg::string_printf( - "the map has more objects (at least 0x%zX) than the client has (0x%zX)", + throw runtime_error(std::format( + "the map has more objects (at least 0x{:X}) than the client has (0x{:X})", fc_end_object_index, entry_count)); } } @@ -6395,20 +6398,20 @@ void MapState::import_object_states_from_sync( throw logic_error("super object link is incorrect"); } if (obj_st->game_flags != entry.flags) { - this->log.warning("(%04zX => K-%03zX) Game flags from client (%04hX) do not match game flags from map (%04hX)", - object_index, obj_st->k_id, entry.flags.load(), obj_st->game_flags); + this->log.warning_f("({:04X} => K-{:03X}) Game flags from client ({:04X}) do not match game flags from map ({:04X})", + object_index, obj_st->k_id, entry.flags, obj_st->game_flags); obj_st->game_flags = entry.flags; } } } if (object_index < entry_count) { - throw runtime_error(phosg::string_printf("the client has more objects (0x%zX) than the map has (0x%zX)", + throw runtime_error(std::format("the client has more objects (0x{:X}) than the map has (0x{:X})", entry_count, object_index)); } } void MapState::import_enemy_states_from_sync(Version from_version, const SyncEnemyStateEntry* entries, size_t entry_count) { - this->log.info("Importing enemy state from sync command"); + this->log.info_f("Importing enemy state from sync command"); size_t enemy_index = 0; bool is_v3 = !is_v1_or_v2(from_version); for (const auto& fc : this->floor_config_entries) { @@ -6422,7 +6425,7 @@ void MapState::import_enemy_states_from_sync(Version from_version, const SyncEne const auto& entities = fc.super_map->version(from_version); size_t fc_end_enemy_index = base_indexes.base_enemy_index + entities.enemies.size(); if (fc_end_enemy_index > entry_count) { - throw runtime_error(phosg::string_printf("the map has more enemies than the client has (0x%zX)", entry_count)); + throw runtime_error(std::format("the map has more enemies than the client has (0x{:X})", entry_count)); } for (; enemy_index < min(fc_end_enemy_index, entry_count); enemy_index++) { const auto& entry = entries[enemy_index]; @@ -6432,24 +6435,24 @@ void MapState::import_enemy_states_from_sync(Version from_version, const SyncEne throw logic_error("super enemy link is incorrect"); } if (ene_st->get_game_flags(is_v3) != entry.flags) { - this->log.warning("(%04zX => E-%03zX) Flags from client (%08" PRIX32 "(%s)) do not match game flags from map (%08" PRIX32 "(%s))", + this->log.warning_f("({:04X} => E-{:03X}) Flags from client ({:08X}({})) do not match game flags from map ({:08X}({}))", enemy_index, ene_st->e_id, - entry.flags.load(), + entry.flags, is_v3 ? "v3" : "v2", ene_st->game_flags, (ene_st->server_flags & MapState::EnemyState::Flag::GAME_FLAGS_IS_V3) ? "v3" : "v2"); ene_st->set_game_flags(entry.flags, !is_v1_or_v2(from_version)); } if (ene_st->total_damage != entry.total_damage) { - this->log.warning("(%04zX => E-%03zX) Total damage from client (%hu) does not match total damage from map (%hu)", - enemy_index, ene_st->e_id, entry.total_damage.load(), ene_st->total_damage); + this->log.warning_f("({:04X} => E-{:03X}) Total damage from client ({}) does not match total damage from map ({})", + enemy_index, ene_st->e_id, entry.total_damage, ene_st->total_damage); ene_st->total_damage = entry.total_damage; } } } if (enemy_index < entry_count) { - throw runtime_error(phosg::string_printf("the client has more enemies (0x%zX) than the map has (0x%zX)", + throw runtime_error(std::format("the client has more enemies (0x{:X}) than the map has (0x{:X})", entry_count, enemy_index)); } } @@ -6463,7 +6466,7 @@ void MapState::import_flag_states_from_sync( const le_uint16_t* event_flags, size_t event_flags_count) { { - this->log.info("Importing object set flags from sync command"); + this->log.info_f("Importing object set flags from sync command"); size_t object_index = 0; for (const auto& fc : this->floor_config_entries) { if (!fc.super_map) { @@ -6481,8 +6484,8 @@ void MapState::import_flag_states_from_sync( if (from_version == Version::DC_NTE) { fc_end_object_index = object_set_flags_count; } else { - throw runtime_error(phosg::string_printf( - "the map has more objects (at least 0x%zX) than the client has (0x%zX)", + throw runtime_error(std::format( + "the map has more objects (at least 0x{:X}) than the client has (0x{:X})", fc_end_object_index, object_set_flags_count)); } } @@ -6494,20 +6497,20 @@ void MapState::import_flag_states_from_sync( throw logic_error("super object link is incorrect"); } if (obj_st->set_flags != set_flags) { - this->log.warning("(%04zX => K-%03zX) Set flags from client (%04hX) do not match set flags from map (%04hX)", + this->log.warning_f("({:04X} => K-{:03X}) Set flags from client ({:04X}) do not match set flags from map ({:04X})", object_index, obj_st->k_id, set_flags, obj_st->set_flags); obj_st->set_flags = set_flags; } } } if (object_index < object_set_flags_count) { - throw runtime_error(phosg::string_printf("the client has more objects (0x%zX) than the map has (0x%zX)", + throw runtime_error(std::format("the client has more objects (0x{:X}) than the map has (0x{:X})", object_set_flags_count, object_index)); } } { - this->log.info("Importing enemy set flags from sync command"); + this->log.info_f("Importing enemy set flags from sync command"); size_t enemy_set_index = 0; for (const auto& fc : this->floor_config_entries) { if (!fc.super_map) { @@ -6530,7 +6533,7 @@ void MapState::import_flag_states_from_sync( throw logic_error("super enemy link is incorrect"); } if (ene_st->set_flags != set_flags) { - this->log.warning("(%04zX => E-%03zX) Set flags from client (%04hX) do not match set flags from map (%04hX)", + this->log.warning_f("({:04X} => E-{:03X}) Set flags from client ({:04X}) do not match set flags from map ({:04X})", enemy_set_index, ene_st->e_id, set_flags, ene_st->set_flags); ene_st->set_flags = set_flags; } @@ -6542,7 +6545,7 @@ void MapState::import_flag_states_from_sync( } { - this->log.info("Importing event flags from sync command"); + this->log.info_f("Importing event flags from sync command"); size_t event_index = 0; for (const auto& fc : this->floor_config_entries) { if (!fc.super_map) { @@ -6562,7 +6565,7 @@ void MapState::import_flag_states_from_sync( const auto& ev = entities.events.at(event_index - base_indexes.base_event_index); auto& ev_st = this->event_states.at(fc.base_super_ids.base_event_index + ev->super_id); if (ev_st->flags != flags) { - this->log.warning("(%04zX => W-%03zX) Set flags from client (%04hX) do not match flags from map (%04hX)", + this->log.warning_f("({:04X} => W-{:03X}) Set flags from client ({:04X}) do not match flags from map ({:04X})", event_index, ev_st->w_id, flags, ev_st->flags); ev_st->flags = flags; } @@ -6591,23 +6594,23 @@ void MapState::verify() const { } } if (this->object_states.size() != total_object_count) { - throw logic_error(phosg::string_printf( - "map state object count (0x%zX) does not match supermap object count (0x%zX)", + throw logic_error(std::format( + "map state object count (0x{:X}) does not match supermap object count (0x{:X})", this->object_states.size(), total_object_count)); } if (this->enemy_states.size() != total_enemy_count) { - throw logic_error(phosg::string_printf( - "map state enemy count (0x%zX) does not match supermap enemy count (0x%zX)", + throw logic_error(std::format( + "map state enemy count (0x{:X}) does not match supermap enemy count (0x{:X})", this->enemy_states.size(), total_enemy_count)); } if (this->enemy_set_states.size() != total_enemy_set_count) { - throw logic_error(phosg::string_printf( - "map state enemy set count (0x%zX) does not match supermap enemy set count (0x%zX)", + throw logic_error(std::format( + "map state enemy set count (0x{:X}) does not match supermap enemy set count (0x{:X})", this->enemy_set_states.size(), total_enemy_set_count)); } if (this->event_states.size() != total_event_count) { - throw logic_error(phosg::string_printf( - "map state event count (0x%zX) does not match supermap event count (0x%zX)", + throw logic_error(std::format( + "map state event count (0x{:X}) does not match supermap event count (0x{:X})", this->event_states.size(), total_event_count)); } @@ -6694,13 +6697,13 @@ void MapState::verify() const { size_t base_enemy_index = this->floor_config(ene->super_ene->floor).base_indexes_for_version(Version::BB_V4).base_enemy_index; size_t enemy_index = base_enemy_index + ene->super_ene->version(Version::BB_V4).relative_enemy_index; if (!remaining_bb_rare_indexes.erase(enemy_index)) { - throw logic_error(phosg::string_printf("BB random rare enemy index %04zX not present in indexes set", enemy_index)); + throw logic_error(std::format("BB random rare enemy index {:04X} not present in indexes set", enemy_index)); } } if (!remaining_bb_rare_indexes.empty()) { vector indexes; for (uint16_t index : remaining_bb_rare_indexes) { - indexes.emplace_back(phosg::string_printf("%04hX", index)); + indexes.emplace_back(std::format("{:04X}", index)); } throw logic_error("not all BB random rare enemies were accounted for; remaining: " + phosg::join(indexes, ", ")); } @@ -6711,32 +6714,32 @@ void MapState::verify() const { } void MapState::print(FILE* stream) const { - fprintf(stream, "Difficulty %s, event %02hhX, state random seed %08" PRIX32 "\n", + phosg::fwrite_fmt(stream, "Difficulty {}, event {:02X}, state random seed {:08X}\n", name_for_difficulty(this->difficulty), this->event, this->random_seed); auto rare_rates_str = this->bb_rare_rates->str(); - fprintf(stream, "BB rare rates: %s\n", rare_rates_str.c_str()); + phosg::fwrite_fmt(stream, "BB rare rates: {}\n", rare_rates_str); - fprintf(stream, "Base indexes:\n"); - fprintf(stream, " FL DCTE----------- DCPR----------- DCV1----------- DCV2----------- PCTE----------- PCV2----------- GCTE----------- GCV3----------- GCEP3TE-------- GCEP3---------- XBV3----------- BBV4-----------\n"); - fprintf(stream, " FL KST EST ESS EVT KST EST ESS EVT KST EST ESS EVT KST EST ESS EVT KST EST ESS EVT KST EST ESS EVT KST EST ESS EVT KST EST ESS EVT KST EST ESS EVT KST EST ESS EVT KST EST ESS EVT KST EST ESS EVT\n"); + phosg::fwrite_fmt(stream, "Base indexes:\n"); + phosg::fwrite_fmt(stream, " FL DCTE----------- DCPR----------- DCV1----------- DCV2----------- PCTE----------- PCV2----------- GCTE----------- GCV3----------- GCEP3TE-------- GCEP3---------- XBV3----------- BBV4-----------\n"); + phosg::fwrite_fmt(stream, " FL KST EST ESS EVT KST EST ESS EVT KST EST ESS EVT KST EST ESS EVT KST EST ESS EVT KST EST ESS EVT KST EST ESS EVT KST EST ESS EVT KST EST ESS EVT KST EST ESS EVT KST EST ESS EVT KST EST ESS EVT\n"); for (size_t floor = 0; floor < this->floor_config_entries.size(); floor++) { auto fc = this->floor_config_entries[floor]; if (fc.super_map) { - fprintf(stream, " %02zX", floor); + phosg::fwrite_fmt(stream, " {:02X}", floor); for (Version v : ALL_NON_PATCH_VERSIONS) { const auto& indexes = fc.base_indexes_for_version(v); - fprintf(stream, " %03zX %03zX %03zX %03zX", indexes.base_object_index, indexes.base_enemy_index, indexes.base_enemy_set_index, indexes.base_event_index); + phosg::fwrite_fmt(stream, " {:03X} {:03X} {:03X} {:03X}", indexes.base_object_index, indexes.base_enemy_index, indexes.base_enemy_set_index, indexes.base_event_index); } fputc('\n', stream); } else { - fprintf(stream, " %02zX --------------- --------------- --------------- --------------- --------------- --------------- --------------- --------------- --------------- --------------- --------------- ---------------\n", floor); + phosg::fwrite_fmt(stream, " {:02X} --------------- --------------- --------------- --------------- --------------- --------------- --------------- --------------- --------------- --------------- --------------- ---------------\n", floor); } } - fprintf(stream, "Objects:\n"); - fprintf(stream, " FL OBJID DCTE DCPR DCV1 DCV2 PCTE PCV2 GCTE GCV3 E3TE GCE3 XBV3 BBV4 OBJECT\n"); + phosg::fwrite_fmt(stream, "Objects:\n"); + phosg::fwrite_fmt(stream, " FL OBJID DCTE DCPR DCV1 DCV2 PCTE PCV2 GCTE GCV3 E3TE GCE3 XBV3 BBV4 OBJECT\n"); for (const auto& obj_st : this->object_states) { - fprintf(stream, " %02hhX K-%03zX", obj_st->super_obj->floor, obj_st->k_id); + phosg::fwrite_fmt(stream, " {:02X} K-{:03X}", obj_st->super_obj->floor, obj_st->k_id); const auto& fc = this->floor_config(obj_st->super_obj->floor); for (Version v : ALL_NON_PATCH_VERSIONS) { const auto& obj_v = obj_st->super_obj->version(v); @@ -6744,18 +6747,18 @@ void MapState::print(FILE* stream) const { fputs(" ----", stream); } else { uint16_t index = fc.base_indexes_for_version(v).base_object_index + obj_v.relative_object_index; - fprintf(stream, " %04hX", index); + phosg::fwrite_fmt(stream, " {:04X}", index); } } string obj_str = obj_st->super_obj->str(); - fprintf(stream, " %s game_flags=%04hX set_flags=%04hX item_drop_checked=%s\n", - obj_str.c_str(), obj_st->game_flags, obj_st->set_flags, obj_st->item_drop_checked ? "true" : "false"); + phosg::fwrite_fmt(stream, " {} game_flags={:04X} set_flags={:04X} item_drop_checked={}\n", + obj_str, obj_st->game_flags, obj_st->set_flags, obj_st->item_drop_checked ? "true" : "false"); } - fprintf(stream, "Enemies:\n"); - fprintf(stream, " FL ENEID DCTE----- DCPR----- DCV1----- DCV2----- PCTE----- PCV2----- GCTE----- GCV3----- EP3TE---- GCEP3---- XBV3----- BBV4----- ENEMY\n"); + phosg::fwrite_fmt(stream, "Enemies:\n"); + phosg::fwrite_fmt(stream, " FL ENEID DCTE----- DCPR----- DCV1----- DCV2----- PCTE----- PCV2----- GCTE----- GCV3----- EP3TE---- GCEP3---- XBV3----- BBV4----- ENEMY\n"); for (const auto& ene_st : this->enemy_states) { - fprintf(stream, " %02hhX E-%03zX", ene_st->super_ene->floor, ene_st->e_id); + phosg::fwrite_fmt(stream, " {:02X} E-{:03X}", ene_st->super_ene->floor, ene_st->e_id); const auto& fc = this->floor_config(ene_st->super_ene->floor); for (Version v : ALL_NON_PATCH_VERSIONS) { const auto& ene_v = ene_st->super_ene->version(v); @@ -6764,12 +6767,12 @@ void MapState::print(FILE* stream) const { } else { uint16_t index = fc.base_indexes_for_version(v).base_enemy_index + ene_v.relative_enemy_index; uint16_t set_index = fc.base_indexes_for_version(v).base_enemy_set_index + ene_v.relative_set_index; - fprintf(stream, " %04hX-%04hX", index, set_index); + phosg::fwrite_fmt(stream, " {:04X}-{:04X}", index, set_index); } } string ene_str = ene_st->super_ene->str(); - fprintf(stream, " %s total_damage=%04hX rare_flags=%04hX game_flags=%08" PRIX32 "(%s) set_flags=%04hX server_flags=%04hX\n", - ene_str.c_str(), + phosg::fwrite_fmt(stream, " {} total_damage={:04X} rare_flags={:04X} game_flags={:08X}({}) set_flags={:04X} server_flags={:04X}\n", + ene_str, ene_st->total_damage, ene_st->rare_flags, ene_st->game_flags, @@ -6779,19 +6782,19 @@ void MapState::print(FILE* stream) const { } if (this->bb_rare_enemy_indexes.empty()) { - fprintf(stream, "BB rare enemy indexes: (none)\n"); + phosg::fwrite_fmt(stream, "BB rare enemy indexes: (none)\n"); } else { string s; for (auto index : this->bb_rare_enemy_indexes) { - s += phosg::string_printf(" %04zX", index); + s += std::format(" {:04X}", index); } - fprintf(stream, "BB rare enemy indexes:%s\n", s.c_str()); + phosg::fwrite_fmt(stream, "BB rare enemy indexes:{}\n", s); } - fprintf(stream, "Events:\n"); - fprintf(stream, " FL EVTID DCTE DCPR DCV1 DCV2 PCTE PCV2 GCTE GCV3 E3TE GCE3 XBV3 BBV4 EVENT\n"); + phosg::fwrite_fmt(stream, "Events:\n"); + phosg::fwrite_fmt(stream, " FL EVTID DCTE DCPR DCV1 DCV2 PCTE PCV2 GCTE GCV3 E3TE GCE3 XBV3 BBV4 EVENT\n"); for (const auto& ev_st : this->event_states) { - fprintf(stream, " %02hhX W-%03zX", ev_st->super_ev->floor, ev_st->w_id); + phosg::fwrite_fmt(stream, " {:02X} W-{:03X}", ev_st->super_ev->floor, ev_st->w_id); const auto& fc = this->floor_config(ev_st->super_ev->floor); for (Version v : ALL_NON_PATCH_VERSIONS) { const auto& ev_v = ev_st->super_ev->version(v); @@ -6799,11 +6802,11 @@ void MapState::print(FILE* stream) const { fputs(" ----", stream); } else { uint16_t index = fc.base_indexes_for_version(v).base_event_index + ev_v.relative_event_index; - fprintf(stream, " %04hX", index); + phosg::fwrite_fmt(stream, " {:04X}", index); } } string ev_str = ev_st->super_ev->str(); - fprintf(stream, " %s set_flags=%04hX\n", ev_str.c_str(), ev_st->flags); + phosg::fwrite_fmt(stream, " {} set_flags={:04X}\n", ev_str, ev_st->flags); } } diff --git a/src/Map.hh b/src/Map.hh index e847777f..4cee30b6 100644 --- a/src/Map.hh +++ b/src/Map.hh @@ -52,10 +52,7 @@ class SetDataTableBase { public: virtual ~SetDataTableBase() = default; - Variations generate_variations( - Episode episode, - bool is_solo, - std::shared_ptr opt_rand_crypt = nullptr) const; + Variations generate_variations(Episode episode, bool is_solo, std::shared_ptr rand_crypt) const; virtual Variations::Entry num_available_variations_for_floor(Episode episode, uint8_t floor) const = 0; virtual Variations::Entry num_free_play_variations_for_floor(Episode episode, bool is_solo, uint8_t floor) const = 0; @@ -339,7 +336,7 @@ public: uint32_t location_indexes_used; uint32_t location_entries_base_offset; - RandomState(uint32_t random_seed); + explicit RandomState(uint32_t random_seed); size_t rand_int_biased(size_t min_v, size_t max_v); uint32_t next_location_index(); void generate_shuffled_location_table(const RandomEnemyLocationsHeader& header, phosg::StringReader r, uint16_t room); @@ -389,7 +386,7 @@ public: std::shared_ptr enemy_sets_data, std::shared_ptr events_data); // Constructor for materialize_random_sections - MapFile(uint32_t random_seed); + explicit MapFile(uint32_t random_seed); ~MapFile() = default; inline uint64_t source_hash() const { @@ -913,25 +910,25 @@ public: uint64_t lobby_or_session_id, uint8_t difficulty, uint8_t event, - uint32_t random_seed, + uint32_t random_seed, // For client-matched rare enemies (non-BB) std::shared_ptr bb_rare_rates, - std::shared_ptr opt_rand_crypt, + std::shared_ptr rand_crypt, std::vector> floor_map_defs); // Constructor for quests MapState( uint64_t lobby_or_session_id, uint8_t difficulty, uint8_t event, - uint32_t random_seed, + uint32_t random_seed, // For client-matched rare enemies (non-BB) std::shared_ptr bb_rare_rates, - std::shared_ptr opt_rand_crypt, + std::shared_ptr rand_crypt, std::shared_ptr quest_map_def); // Constructor for empty maps (used in challenge mode before a quest starts) MapState(); ~MapState() = default; - void index_super_map(const FloorConfig& floor_config, std::shared_ptr opt_rand_crypt); + void index_super_map(const FloorConfig& floor_config, std::shared_ptr rand_crypt); void compute_dynamic_object_base_indexes(); inline FloorConfig& floor_config(uint8_t floor) { diff --git a/src/Menu.cc b/src/Menu.cc index df3e199f..85b85944 100644 --- a/src/Menu.cc +++ b/src/Menu.cc @@ -2,14 +2,21 @@ using namespace std; -MenuItem::MenuItem(uint32_t item_id, const string& name, const string& description, uint32_t flags) +MenuItem::MenuItem( + uint32_t item_id, + const string& name, + const string& description, + uint32_t flags) : item_id(item_id), name(name), description(description), get_description(nullptr), flags(flags) {} -MenuItem::MenuItem(uint32_t item_id, const string& name, std::function get_description, uint32_t flags) +MenuItem::MenuItem(uint32_t item_id, + const string& name, + std::function get_description, + uint32_t flags) : item_id(item_id), name(name), description(), diff --git a/src/Menu.hh b/src/Menu.hh index c1e61bbf..ad13f24a 100644 --- a/src/Menu.hh +++ b/src/Menu.hh @@ -2,6 +2,7 @@ #include +#include #include #include #include @@ -71,21 +72,15 @@ constexpr uint32_t GO_BACK = 0xAAFFFFAA; constexpr uint32_t CHAT_COMMANDS = 0xAA0101AA; constexpr uint32_t PLAYER_NOTIFICATIONS = 0xAA0202AA; constexpr uint32_t DROP_NOTIFICATIONS = 0xAA0303AA; -constexpr uint32_t BLOCK_PINGS = 0xAA0404AA; -constexpr uint32_t INFINITE_HP = 0xAA0505AA; -constexpr uint32_t INFINITE_TP = 0xAA0606AA; -constexpr uint32_t SWITCH_ASSIST = 0xAA0707AA; -constexpr uint32_t BLOCK_EVENTS = 0xAA0808AA; -constexpr uint32_t BLOCK_PATCHES = 0xAA0909AA; -constexpr uint32_t SAVE_FILES = 0xAA0A0AAA; -constexpr uint32_t VIRTUAL_CLIENT = 0xAA0B0BAA; -constexpr uint32_t RED_NAME = 0xAA0C0CAA; -constexpr uint32_t BLANK_NAME = 0xAA0D0DAA; -constexpr uint32_t SUPPRESS_LOGIN = 0xAA0E0EAA; -constexpr uint32_t SKIP_CARD = 0xAA0F0FAA; -constexpr uint32_t EP3_INFINITE_MESETA = 0xAA1010AA; -constexpr uint32_t EP3_INFINITE_TIME = 0xAA1111AA; -constexpr uint32_t EP3_UNMASK_WHISPERS = 0xAA1212AA; +constexpr uint32_t INFINITE_HP = 0xAA0404AA; +constexpr uint32_t INFINITE_TP = 0xAA0505AA; +constexpr uint32_t SWITCH_ASSIST = 0xAA0606AA; +constexpr uint32_t BLOCK_EVENTS = 0xAA0707AA; +constexpr uint32_t BLOCK_PATCHES = 0xAA0808AA; +constexpr uint32_t SAVE_FILES = 0xAA0909AA; +constexpr uint32_t EP3_INFINITE_MESETA = 0xAA0A0AAA; +constexpr uint32_t EP3_INFINITE_TIME = 0xAA0B0BAA; +constexpr uint32_t EP3_UNMASK_WHISPERS = 0xAA0C0CAA; } // namespace ProxyOptionsMenuItemID namespace TeamRewardMenuItemID { @@ -134,8 +129,16 @@ struct MenuItem { std::function get_description; uint32_t flags; - MenuItem(uint32_t item_id, const std::string& name, const std::string& description, uint32_t flags); - MenuItem(uint32_t item_id, const std::string& name, std::function get_description, uint32_t flags); + MenuItem( + uint32_t item_id, + const std::string& name, + const std::string& description, + uint32_t flags); + MenuItem( + uint32_t item_id, + const std::string& name, + std::function get_description, + uint32_t flags); }; struct Menu { diff --git a/src/NetworkAddresses.cc b/src/NetworkAddresses.cc index bf584a25..b00035f6 100644 --- a/src/NetworkAddresses.cc +++ b/src/NetworkAddresses.cc @@ -1,12 +1,15 @@ +#include "WindowsPlatform.hh" + #include "NetworkAddresses.hh" -#include #include -#include -#include -#include -#include #include +#ifndef PHOSG_WINDOWS +#include +#else +#include +#include +#endif #include #include @@ -16,40 +19,17 @@ using namespace std; -uint32_t resolve_address(const char* address) { - struct addrinfo* res0; - if (getaddrinfo(address, nullptr, nullptr, &res0)) { - auto e = phosg::string_for_error(errno); - throw runtime_error(phosg::string_printf( - "can\'t resolve hostname %s: %s", address, e.c_str())); - } - - std::unique_ptr res0_unique(res0, freeaddrinfo); - struct addrinfo* res4 = nullptr; - for (struct addrinfo* res = res0; res; res = res->ai_next) { - if (res->ai_family == AF_INET) { - res4 = res; - } - } - if (!res4) { - throw runtime_error(phosg::string_printf( - "can\'t resolve hostname %s: no usable data", address)); - } - - struct sockaddr_in* res_sin = (struct sockaddr_in*)res4->ai_addr; - return ntohl(res_sin->sin_addr.s_addr); -} - map get_local_addresses() { + map ret; + +#ifndef PHOSG_WINDOWS struct ifaddrs* ifa_raw; if (getifaddrs(&ifa_raw)) { auto s = phosg::string_for_error(errno); - throw runtime_error(phosg::string_printf("failed to get interface addresses: %s", s.c_str())); + throw runtime_error(std::format("failed to get interface addresses: {}", s)); } - unique_ptr ifa(ifa_raw, freeifaddrs); - map ret; for (struct ifaddrs* i = ifa.get(); i; i = i->ifa_next) { if (!i->ifa_addr) { continue; @@ -63,6 +43,32 @@ map get_local_addresses() { ret.emplace(i->ifa_name, ntohl(sin->sin_addr.s_addr)); } +#else + ULONG buffer_size = 0x1000; + std::vector buffer(buffer_size); + + auto* adapters = reinterpret_cast(buffer.data()); + DWORD result = GetAdaptersAddresses(AF_INET, GAA_FLAG_INCLUDE_PREFIX, nullptr, adapters, &buffer_size); + if (result == ERROR_BUFFER_OVERFLOW) { + buffer.resize(buffer_size); + adapters = reinterpret_cast(buffer.data()); + result = GetAdaptersAddresses(AF_INET, GAA_FLAG_INCLUDE_PREFIX, nullptr, adapters, &buffer_size); + } + + if (result != NO_ERROR) { + throw runtime_error(std::format("GetAdaptersAddresses failed: {}", result)); + } + + for (IP_ADAPTER_ADDRESSES* adapter = adapters; adapter != nullptr; adapter = adapter->Next) { + for (IP_ADAPTER_UNICAST_ADDRESS* ua = adapter->FirstUnicastAddress; ua != nullptr; ua = ua->Next) { + if (ua->Address.lpSockaddr->sa_family == AF_INET) { + sockaddr_in* sa_in = reinterpret_cast(ua->Address.lpSockaddr); + ret.emplace(adapter->AdapterName, ntohl(sa_in->sin_addr.S_un.S_addr)); + } + } + } +#endif + return ret; } @@ -87,7 +93,7 @@ bool is_local_address(const sockaddr_storage& daddr) { } string string_for_address(uint32_t address) { - return phosg::string_printf("%hhu.%hhu.%hhu.%hhu", + return std::format("{}.{}.{}.{}", static_cast(address >> 24), static_cast(address >> 16), static_cast(address >> 8), static_cast(address)); } diff --git a/src/NetworkAddresses.hh b/src/NetworkAddresses.hh index 635d61b9..d61e4623 100644 --- a/src/NetworkAddresses.hh +++ b/src/NetworkAddresses.hh @@ -1,20 +1,19 @@ #pragma once -#include #include +#include #include #include // PSO is IPv4-only, so we just treat addresses as uint32_t everywhere because // it's easier -uint32_t resolve_address(const char* address); std::map get_local_addresses(); uint32_t get_connected_address(int fd); bool is_loopback_address(uint32_t addr); bool is_local_address(uint32_t daddr); -bool is_local_address(const sockaddr_storage& daddr); +bool is_local_address(asio::ip::tcp::endpoint& daddr); std::string string_for_address(uint32_t address); uint32_t address_for_string(const char* address); diff --git a/src/PPKArchive.cc b/src/PPKArchive.cc index b7c08418..38531e00 100644 --- a/src/PPKArchive.cc +++ b/src/PPKArchive.cc @@ -60,15 +60,15 @@ std::unordered_map decode_ppk_file(const std::string& decrypt_ppk_data(data, phosg::tolower(filename), password); uint32_t checksum = phosg::crc32(data.data(), data.size()); if (checksum != entry.checksum) { - throw runtime_error(phosg::string_printf( - "incorrect checksum for file %s (expected %08" PRIX32 "; received %08" PRIX32 ")", - filename.c_str(), entry.checksum.load(), checksum)); + throw runtime_error(std::format( + "incorrect checksum for file {} (expected {:08X}; received {:08X})", + filename, entry.checksum, checksum)); } if (entry.compressed_size < entry.decompressed_size) { data = prs_decompress(data); } if (!ret.emplace(filename, data).second) { - throw runtime_error(phosg::string_printf("archive contains multiple files with the same name (%s)", filename.c_str())); + throw runtime_error(std::format("archive contains multiple files with the same name ({})", filename)); } offset = entry_offset - 4; } diff --git a/src/PSOEncryption.cc b/src/PSOEncryption.cc index c683173b..d55fbdf8 100644 --- a/src/PSOEncryption.cc +++ b/src/PSOEncryption.cc @@ -13,100 +13,65 @@ using namespace std; // TODO: fix style in this file, especially in psobb functions +RandomGenerator::RandomGenerator(uint32_t seed) : initial_seed(seed) {} + +DisabledRandomGenerator::DisabledRandomGenerator() : RandomGenerator(0) {} + +uint32_t DisabledRandomGenerator::next() { + throw std::runtime_error("Random data cannot be generated in this context"); +} + +MT19937Generator::MT19937Generator(uint32_t seed) : RandomGenerator(seed), gen(seed) {} + +uint32_t MT19937Generator::next() { + return this->gen(); +} + // Most ciphers used by PSO are symmetric; alias decrypt to encrypt by default -void PSOEncryption::decrypt(void* data, size_t size, bool advance) { - this->encrypt(data, size, advance); +void PSOEncryption::decrypt(void* data, size_t size) { + this->encrypt(data, size); } PSOLFGEncryption::PSOLFGEncryption( uint32_t seed, size_t stream_length, size_t end_offset) - : stream(stream_length, 0), + : RandomGenerator(seed), + stream(stream_length, 0), offset(0), - end_offset(end_offset), - initial_seed(seed), - cycles(0) {} + end_offset(end_offset) {} -uint32_t PSOLFGEncryption::next(bool advance) { +uint32_t PSOLFGEncryption::next() { if (this->offset == this->end_offset) { this->update_stream(); } - uint32_t ret = this->stream[this->offset]; - if (advance) { - this->offset++; - } - return ret; + return this->stream[this->offset++]; } -template -void PSOLFGEncryption::encrypt_t(void* vdata, size_t size, bool advance) { - if (!advance && (size != 4)) { - throw logic_error("cannot peek-encrypt/decrypt with size > 4"); - } - - size_t uint32_count = size >> 2; - size_t extra_bytes = size & 3; - U32T* data = reinterpret_cast*>(vdata); - for (size_t x = 0; x < uint32_count; x++) { - data[x] ^= this->next(advance); - } - if (extra_bytes) { - U32T last = 0; - memcpy(&last, &data[uint32_count], extra_bytes); - last ^= this->next(advance); - memcpy(&data[uint32_count], &last, extra_bytes); - } +void PSOLFGEncryption::encrypt(void* vdata, size_t size) { + this->encrypt_t(vdata, size); } -template -void PSOLFGEncryption::encrypt_minus_t(void* vdata, size_t size, bool advance) { - if (!advance && (size != 4)) { - throw logic_error("cannot peek-encrypt/decrypt with size > 4"); - } - - size_t uint32_count = size >> 2; - size_t extra_bytes = size & 3; - U32T* data = reinterpret_cast*>(vdata); - for (size_t x = 0; x < uint32_count; x++) { - data[x] = this->next(advance) - data[x]; - } - if (extra_bytes) { - U32T last = 0; - memcpy(&last, &data[uint32_count], extra_bytes); - last = this->next(advance) - last; - memcpy(&data[uint32_count], &last, extra_bytes); - } +void PSOLFGEncryption::encrypt_big_endian(void* vdata, size_t size) { + this->encrypt_t(vdata, size); } -void PSOLFGEncryption::encrypt(void* vdata, size_t size, bool advance) { - this->encrypt_t(vdata, size, advance); +void PSOLFGEncryption::encrypt_minus(void* vdata, size_t size) { + this->encrypt_minus_t(vdata, size); } -void PSOLFGEncryption::encrypt_big_endian(void* vdata, size_t size, bool advance) { - this->encrypt_t(vdata, size, advance); +void PSOLFGEncryption::encrypt_big_endian_minus(void* vdata, size_t size) { + this->encrypt_minus_t(vdata, size); } -void PSOLFGEncryption::encrypt_minus(void* vdata, size_t size, bool advance) { - this->encrypt_minus_t(vdata, size, advance); -} - -void PSOLFGEncryption::encrypt_big_endian_minus(void* vdata, size_t size, bool advance) { - this->encrypt_minus_t(vdata, size, advance); -} - -void PSOLFGEncryption::encrypt_both_endian( - void* le_vdata, void* be_vdata, size_t size, bool advance) { +void PSOLFGEncryption::encrypt_both_endian(void* le_vdata, void* be_vdata, size_t size) { if (size & 3) { throw invalid_argument("size must be a multiple of 4"); } - if (!advance && (size != 4)) { - throw logic_error("cannot peek-encrypt/decrypt with size > 4"); - } size >>= 2; le_uint32_t* le_data = reinterpret_cast(le_vdata); be_uint32_t* be_data = reinterpret_cast(be_vdata); for (size_t x = 0; x < size; x++) { - uint32_t key = this->next(advance); + uint32_t key = this->next(); le_data[x] ^= key; be_data[x] ^= key; } @@ -125,7 +90,6 @@ PSOV2Encryption::PSOV2Encryption(uint32_t seed) for (size_t x = 0; x < 5; x++) { this->update_stream(); } - this->cycles = 0; } void PSOV2Encryption::update_stream() { @@ -136,7 +100,6 @@ void PSOV2Encryption::update_stream() { this->stream[z] -= this->stream[z - 0x18]; } this->offset = 1; - this->cycles++; } PSOEncryption::Type PSOV2Encryption::type() const { @@ -174,7 +137,6 @@ PSOV3Encryption::PSOV3Encryption(uint32_t seed) for (size_t x = 0; x < 4; x++) { this->update_stream(); } - this->cycles = 0; } void PSOV3Encryption::update_stream() { @@ -186,7 +148,6 @@ void PSOV3Encryption::update_stream() { this->stream[z] ^= this->stream[z - PHASE2_OFFSET]; } this->offset = 0; - this->cycles++; } PSOEncryption::Type PSOV3Encryption::type() const { @@ -199,7 +160,7 @@ PSOBBEncryption::PSOBBEncryption( this->apply_seed(original_seed, seed_size); } -void PSOBBEncryption::encrypt(void* vdata, size_t size, bool advance) { +void PSOBBEncryption::encrypt(void* vdata, size_t size) { if (this->state.subtype == Subtype::TFS1) { if (size & 7) { throw invalid_argument("size must be a multiple of 8"); @@ -231,21 +192,13 @@ void PSOBBEncryption::encrypt(void* vdata, size_t size, bool advance) { if (size & 1) { throw invalid_argument("size must be a multiple of 2"); } - if (!advance && (size > 0x100)) { - throw logic_error("JSD1 can only peek-encrypt up to 0x100 bytes"); - } uint8_t* bytes = reinterpret_cast(vdata); for (size_t z = 0; z < size; z++) { uint8_t v = bytes[z]; bytes[z] = v ^ this->state.private_keys.as8[this->state.initial_keys.jsd1_stream_offset]; - if (advance) { - this->state.private_keys.as8[this->state.initial_keys.jsd1_stream_offset] -= v; - } + this->state.private_keys.as8[this->state.initial_keys.jsd1_stream_offset] -= v; this->state.initial_keys.jsd1_stream_offset++; } - if (!advance) { - this->state.initial_keys.jsd1_stream_offset -= size; - } for (size_t z = 0; z < size; z += 2) { uint8_t a = bytes[z]; uint8_t b = bytes[z + 1]; @@ -296,7 +249,7 @@ void PSOBBEncryption::encrypt(void* vdata, size_t size, bool advance) { } } -void PSOBBEncryption::decrypt(void* vdata, size_t size, bool advance) { +void PSOBBEncryption::decrypt(void* vdata, size_t size) { if (this->state.subtype == Subtype::TFS1) { if (size & 7) { throw invalid_argument("size must be a multiple of 8"); @@ -328,9 +281,6 @@ void PSOBBEncryption::decrypt(void* vdata, size_t size, bool advance) { if (size & 1) { throw invalid_argument("size must be a multiple of 2"); } - if (!advance && (size > 0x100)) { - throw logic_error("JSD1 can only peek-decrypt up to 0x100 bytes"); - } uint8_t* bytes = reinterpret_cast(vdata); for (size_t z = 0; z < size; z += 2) { uint8_t a = bytes[z]; @@ -340,14 +290,9 @@ void PSOBBEncryption::decrypt(void* vdata, size_t size, bool advance) { } for (size_t z = 0; z < size; z++) { bytes[z] ^= this->state.private_keys.as8[this->state.initial_keys.jsd1_stream_offset]; - if (advance) { - this->state.private_keys.as8[this->state.initial_keys.jsd1_stream_offset] -= bytes[z]; - } + this->state.private_keys.as8[this->state.initial_keys.jsd1_stream_offset] -= bytes[z]; this->state.initial_keys.jsd1_stream_offset++; } - if (!advance) { - this->state.initial_keys.jsd1_stream_offset -= size; - } } else { // STANDARD or MOCB1 if (size & 7) { @@ -676,7 +621,7 @@ PSOV2OrV3DetectorEncryption::PSOV2OrV3DetectorEncryption( v2_matches(v2_matches), v3_matches(v3_matches) {} -void PSOV2OrV3DetectorEncryption::encrypt(void* data, size_t size, bool advance) { +void PSOV2OrV3DetectorEncryption::encrypt(void* data, size_t size) { if (!this->active_crypt) { if (size != 4) { throw logic_error("initial detector decrypt size must be 4"); @@ -686,29 +631,29 @@ void PSOV2OrV3DetectorEncryption::encrypt(void* data, size_t size, bool advance) le_uint32_t decrypted_v2 = encrypted; auto v2_crypt = make_unique(this->key); - v2_crypt->decrypt(&decrypted_v2, sizeof(decrypted_v2), false); + v2_crypt->decrypt(&decrypted_v2, sizeof(decrypted_v2)); le_uint32_t decrypted_v3 = encrypted; auto v3_crypt = make_unique(this->key); - v3_crypt->decrypt(&decrypted_v3, sizeof(decrypted_v3), false); + v3_crypt->decrypt(&decrypted_v3, sizeof(decrypted_v3)); bool v2_match = this->v2_matches.count(decrypted_v2); bool v3_match = this->v3_matches.count(decrypted_v3); if (!v2_match && !v3_match) { - throw runtime_error(phosg::string_printf( - "unable to determine crypt version (input=%08" PRIX32 ", v2=%08" PRIX32 ", v3=%08" PRIX32 ")", - encrypted.load(), decrypted_v2.load(), decrypted_v3.load())); + throw runtime_error(std::format( + "unable to determine crypt version (input={:08X}, v2={:08X}, v3={:08X})", encrypted, decrypted_v2, decrypted_v3)); } else if (v2_match && v3_match) { - throw runtime_error(phosg::string_printf( - "ambiguous crypt version (v2=%08" PRIX32 ", v3=%08" PRIX32 ")", - decrypted_v2.load(), decrypted_v3.load())); + throw runtime_error(std::format("ambiguous crypt version (v2={:08X}, v3={:08X})", decrypted_v2, decrypted_v3)); } else if (v2_match) { this->active_crypt = std::move(v2_crypt); + *reinterpret_cast(data) = decrypted_v2; } else { this->active_crypt = std::move(v3_crypt); + *reinterpret_cast(data) = decrypted_v3; } + } else { + this->active_crypt->encrypt(data, size); } - this->active_crypt->encrypt(data, size, advance); } PSOEncryption::Type PSOV2OrV3DetectorEncryption::type() const { @@ -723,7 +668,7 @@ PSOV2OrV3ImitatorEncryption::PSOV2OrV3ImitatorEncryption( : key(key), detector_crypt(detector_crypt) {} -void PSOV2OrV3ImitatorEncryption::encrypt(void* data, size_t size, bool advance) { +void PSOV2OrV3ImitatorEncryption::encrypt(void* data, size_t size) { if (!this->active_crypt) { auto t = this->detector_crypt->type(); if (t == Type::V2) { @@ -734,7 +679,7 @@ void PSOV2OrV3ImitatorEncryption::encrypt(void* data, size_t size, bool advance) throw logic_error("detector crypt is not V2 or V3"); } } - this->active_crypt->encrypt(data, size, advance); + this->active_crypt->encrypt(data, size); } PSOEncryption::Type PSOV2OrV3ImitatorEncryption::type() const { @@ -753,14 +698,14 @@ PSOBBMultiKeyDetectorEncryption::PSOBBMultiKeyDetectorEncryption( expected_first_data(expected_first_data), seed(reinterpret_cast(seed), seed_size) {} -void PSOBBMultiKeyDetectorEncryption::encrypt(void* data, size_t size, bool advance) { +void PSOBBMultiKeyDetectorEncryption::encrypt(void* data, size_t size) { if (!this->active_crypt.get()) { throw logic_error("PSOBB multi-key encryption requires client input first"); } - this->active_crypt->encrypt(data, size, advance); + this->active_crypt->encrypt(data, size); } -void PSOBBMultiKeyDetectorEncryption::decrypt(void* data, size_t size, bool advance) { +void PSOBBMultiKeyDetectorEncryption::decrypt(void* data, size_t size) { if (!this->active_crypt.get()) { if (size != 8) { throw logic_error("initial decryption size does not match expected first data size"); @@ -770,7 +715,7 @@ void PSOBBMultiKeyDetectorEncryption::decrypt(void* data, size_t size, bool adva this->active_key = key; this->active_crypt = make_shared(*this->active_key, this->seed.data(), this->seed.size()); string test_data(reinterpret_cast(data), size); - this->active_crypt->decrypt(test_data.data(), test_data.size(), false); + this->active_crypt->decrypt(test_data.data(), test_data.size()); if (this->expected_first_data.count(test_data)) { break; } @@ -781,7 +726,7 @@ void PSOBBMultiKeyDetectorEncryption::decrypt(void* data, size_t size, bool adva throw runtime_error("none of the registered private keys are valid for this client"); } } - this->active_crypt->decrypt(data, size, advance); + this->active_crypt->decrypt(data, size); } PSOEncryption::Type PSOBBMultiKeyDetectorEncryption::type() const { @@ -797,12 +742,12 @@ PSOBBMultiKeyImitatorEncryption::PSOBBMultiKeyImitatorEncryption( seed(reinterpret_cast(seed), seed_size), jsd1_use_detector_seed(jsd1_use_detector_seed) {} -void PSOBBMultiKeyImitatorEncryption::encrypt(void* data, size_t size, bool advance) { - this->ensure_crypt()->encrypt(data, size, advance); +void PSOBBMultiKeyImitatorEncryption::encrypt(void* data, size_t size) { + this->ensure_crypt()->encrypt(data, size); } -void PSOBBMultiKeyImitatorEncryption::decrypt(void* data, size_t size, bool advance) { - this->ensure_crypt()->decrypt(data, size, advance); +void PSOBBMultiKeyImitatorEncryption::decrypt(void* data, size_t size) { + this->ensure_crypt()->decrypt(data, size); } PSOEncryption::Type PSOBBMultiKeyImitatorEncryption::type() const { @@ -835,7 +780,7 @@ JSD0Encryption::JSD0Encryption(const void* seed, size_t seed_size) : key(0) { } } -void JSD0Encryption::decrypt(void* data, size_t size, bool) { +void JSD0Encryption::decrypt(void* data, size_t size) { uint8_t* bytes = reinterpret_cast(data); for (size_t z = 0; z < size; z++) { bytes[z] ^= this->key; @@ -843,7 +788,7 @@ void JSD0Encryption::decrypt(void* data, size_t size, bool) { } } -void JSD0Encryption::encrypt(void* data, size_t size, bool) { +void JSD0Encryption::encrypt(void* data, size_t size) { uint8_t* bytes = reinterpret_cast(data); for (size_t z = 0; z < size; z++) { bytes[z] += this->key; diff --git a/src/PSOEncryption.hh b/src/PSOEncryption.hh index 95f14124..acaf9697 100644 --- a/src/PSOEncryption.hh +++ b/src/PSOEncryption.hh @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -14,6 +15,39 @@ #include "Text.hh" #include "Types.hh" +class RandomGenerator { +public: + virtual ~RandomGenerator() = default; + virtual uint32_t next() = 0; + + inline uint32_t seed() const { + return this->initial_seed; + } + +protected: + uint32_t initial_seed; + RandomGenerator(uint32_t seed); +}; + +class DisabledRandomGenerator : public RandomGenerator { +public: + DisabledRandomGenerator(); + virtual ~DisabledRandomGenerator() = default; + + virtual uint32_t next(); +}; + +class MT19937Generator : public RandomGenerator { +public: + explicit MT19937Generator(uint32_t seed); + virtual ~MT19937Generator() = default; + + virtual uint32_t next(); + +private: + std::mt19937 gen; +}; + class PSOEncryption { public: enum class Type { @@ -25,14 +59,14 @@ public: virtual ~PSOEncryption() = default; - virtual void encrypt(void* data, size_t size, bool advance = true) = 0; - virtual void decrypt(void* data, size_t size, bool advance = true); + virtual void encrypt(void* data, size_t size) = 0; + virtual void decrypt(void* data, size_t size); - inline void encrypt(std::string& data, bool advance = true) { - this->encrypt(data.data(), data.size(), advance); + inline void encrypt(std::string& data) { + this->encrypt(data.data(), data.size()); } - inline void decrypt(std::string& data, bool advance = true) { - this->decrypt(data.data(), data.size(), advance); + inline void decrypt(std::string& data) { + this->decrypt(data.data(), data.size()); } virtual Type type() const = 0; @@ -41,28 +75,48 @@ protected: PSOEncryption() = default; }; -class PSOLFGEncryption : public PSOEncryption { +class PSOLFGEncryption : public PSOEncryption, public RandomGenerator { public: - virtual void encrypt(void* data, size_t size, bool advance = true); - void encrypt_big_endian(void* data, size_t size, bool advance = true); - void encrypt_minus(void* data, size_t size, bool advance = true); - void encrypt_big_endian_minus(void* data, size_t size, bool advance = true); - void encrypt_both_endian(void* le_data, void* be_data, size_t size, bool advance = true); + virtual void encrypt(void* data, size_t size); + void encrypt_big_endian(void* data, size_t size); + void encrypt_minus(void* data, size_t size); + void encrypt_big_endian_minus(void* data, size_t size); + void encrypt_both_endian(void* le_data, void* be_data, size_t size); template - void encrypt_t(void* data, size_t size, bool advance = true); + void encrypt_t(void* vdata, size_t size) { + size_t uint32_count = size >> 2; + size_t extra_bytes = size & 3; + U32T* data = reinterpret_cast*>(vdata); + for (size_t x = 0; x < uint32_count; x++) { + data[x] ^= this->next(); + } + if (extra_bytes) { + U32T last = 0; + memcpy(&last, &data[uint32_count], extra_bytes); + last ^= this->next(); + memcpy(&data[uint32_count], &last, extra_bytes); + } + } + template - void encrypt_minus_t(void* data, size_t size, bool advance = true); - - uint32_t next(bool advance = true); - - inline uint32_t seed() const { - return this->initial_seed; - } - uint32_t absolute_offset() const { - return (this->cycles * this->end_offset) + this->offset; + void encrypt_minus_t(void* vdata, size_t size) { + size_t uint32_count = size >> 2; + size_t extra_bytes = size & 3; + U32T* data = reinterpret_cast*>(vdata); + for (size_t x = 0; x < uint32_count; x++) { + data[x] = this->next() - data[x]; + } + if (extra_bytes) { + U32T last = 0; + memcpy(&last, &data[uint32_count], extra_bytes); + last = this->next() - last; + memcpy(&data[uint32_count], &last, extra_bytes); + } } + virtual uint32_t next(); + protected: PSOLFGEncryption(uint32_t seed, size_t stream_length, size_t end_offset); @@ -71,8 +125,6 @@ protected: std::vector stream; size_t offset; size_t end_offset; - uint32_t initial_seed; - size_t cycles; }; class PSOV2Encryption : public PSOLFGEncryption { @@ -134,8 +186,8 @@ public: PSOBBEncryption(const KeyFile& key, const void* seed, size_t seed_size); - virtual void encrypt(void* data, size_t size, bool advance = true); - virtual void decrypt(void* data, size_t size, bool advance = true); + virtual void encrypt(void* data, size_t size); + virtual void decrypt(void* data, size_t size); virtual Type type() const; @@ -156,7 +208,7 @@ public: const std::unordered_set& v2_matches, const std::unordered_set& v3_matches); - virtual void encrypt(void* data, size_t size, bool advance = true); + virtual void encrypt(void* data, size_t size); virtual Type type() const; @@ -172,7 +224,7 @@ public: PSOV2OrV3ImitatorEncryption( uint32_t key, std::shared_ptr client_crypt); - virtual void encrypt(void* data, size_t size, bool advance = true); + virtual void encrypt(void* data, size_t size); virtual Type type() const; @@ -194,8 +246,8 @@ public: const void* seed, size_t seed_size); - virtual void encrypt(void* data, size_t size, bool advance = true); - virtual void decrypt(void* data, size_t size, bool advance = true); + virtual void encrypt(void* data, size_t size); + virtual void decrypt(void* data, size_t size); inline std::shared_ptr get_active_key() const { return this->active_key; @@ -222,8 +274,8 @@ public: size_t seed_size, bool jsd1_use_detector_seed); - virtual void encrypt(void* data, size_t size, bool advance = true); - virtual void decrypt(void* data, size_t size, bool advance = true); + virtual void encrypt(void* data, size_t size); + virtual void decrypt(void* data, size_t size); virtual Type type() const; @@ -240,8 +292,8 @@ class JSD0Encryption : public PSOEncryption { public: JSD0Encryption(const void* seed, size_t seed_size); - virtual void encrypt(void* data, size_t size, bool advance = true); - virtual void decrypt(void* data, size_t size, bool advance = true); + virtual void encrypt(void* data, size_t size); + virtual void decrypt(void* data, size_t size); virtual Type type() const = 0; @@ -260,7 +312,7 @@ private: U32T value; public: - ChallengeTimeT() = default; + ChallengeTimeT() : value(0) {} ChallengeTimeT(uint16_t v) { this->encode(v); } @@ -353,7 +405,3 @@ std::string encrypt_pr2_data(const std::string& data, size_t decompressed_size, } return ret; } - -inline uint32_t random_from_optional_crypt(std::shared_ptr random_crypt) { - return random_crypt ? random_crypt->next() : phosg::random_object(); -} diff --git a/src/PSOGCObjectGraph.cc b/src/PSOGCObjectGraph.cc index c09c0959..1485c06e 100644 --- a/src/PSOGCObjectGraph.cc +++ b/src/PSOGCObjectGraph.cc @@ -81,8 +81,8 @@ void PSOGCObjectGraph::Object::print(FILE* stream, size_t indent_level) const { fputc(' ', stream); fputc(' ', stream); } - fprintf(stream, "%s +%04hX @ %08" PRIX32 " (VT %08" PRIX32 ": destroy=%08" PRIX32 " update=%08" PRIX32 " render=%08" PRIX32 " render_shadow=%08" PRIX32 ")\n", - this->type_name.c_str(), + phosg::fwrite_fmt(stream, "{} +{:04X} @ {:08X} (VT {:08X}: destroy={:08X} update={:08X} render={:08X} render_shadow={:08X})\n", + this->type_name, this->flags, this->address, this->vtable->address, diff --git a/src/PSOProtocol.cc b/src/PSOProtocol.cc index 24ca4987..32ce49ad 100644 --- a/src/PSOProtocol.cc +++ b/src/PSOProtocol.cc @@ -1,7 +1,5 @@ #include "PSOProtocol.hh" -#include - #include #include @@ -190,16 +188,16 @@ void PSOCommandHeader::set_flag(Version version, uint32_t flag) { void check_size_v(size_t size, size_t min_size, size_t max_size) { if (size < min_size) { - throw std::runtime_error(phosg::string_printf( - "command too small (expected at least 0x%zX bytes, received 0x%zX bytes)", + throw std::runtime_error(std::format( + "command too small (expected at least 0x{:X} bytes, received 0x{:X} bytes)", min_size, size)); } if (max_size < min_size) { max_size = min_size; } if (size > max_size) { - throw std::runtime_error(phosg::string_printf( - "command too large (expected at most 0x%zX bytes, received 0x%zX bytes)", + throw std::runtime_error(std::format( + "command too large (expected at most 0x{:X} bytes, received 0x{:X} bytes)", max_size, size)); } } diff --git a/src/PSOProtocol.hh b/src/PSOProtocol.hh index 4e841d2a..7aba7440 100644 --- a/src/PSOProtocol.hh +++ b/src/PSOProtocol.hh @@ -1,6 +1,5 @@ #pragma once -#include #include #include @@ -56,13 +55,13 @@ RetT& check_size_generic( size_t min_size, size_t max_size) { if (size < min_size) { - throw std::runtime_error(phosg::string_printf( - "command too small (expected at least 0x%zX bytes, received 0x%zX bytes)", + throw std::runtime_error(std::format( + "command too small (expected at least 0x{:X} bytes, received 0x{:X} bytes)", min_size, size)); } if (size > max_size) { - throw std::runtime_error(phosg::string_printf( - "command too large (expected at most 0x%zX bytes, received 0x%zX bytes)", + throw std::runtime_error(std::format( + "command too large (expected at most 0x{:X} bytes, received 0x{:X} bytes)", max_size, size)); } return *reinterpret_cast(data); diff --git a/src/PatchFileIndex.cc b/src/PatchFileIndex.cc index 291c6b5b..b847c900 100644 --- a/src/PatchFileIndex.cc +++ b/src/PatchFileIndex.cc @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -13,6 +14,13 @@ using namespace std; +int64_t file_mtime_int(const std::string& path) { + auto mtime = std::filesystem::last_write_time(path); + auto sctp = std::chrono::time_point_cast( + mtime - std::filesystem::file_time_type::clock::now() + std::chrono::system_clock::now()); + return sctp.time_since_epoch().count(); +} + PatchFileIndex::File::File(PatchFileIndex* index) : index(index), crc32(0), @@ -22,7 +30,7 @@ std::shared_ptr PatchFileIndex::File::load_data() { if (!this->loaded_data) { string relative_path = phosg::join(this->path_directories, "/") + "/" + this->name; string full_path = this->index->root_dir + "/" + relative_path; - patch_index_log.info("Loading data for %s", relative_path.c_str()); + patch_index_log.info_f("Loading data for {}", relative_path); this->loaded_data = make_shared(phosg::load_file(full_path)); this->size = this->loaded_data->size(); } @@ -37,10 +45,10 @@ PatchFileIndex::PatchFileIndex(const string& root_dir) try { string metadata_text = phosg::load_file(metadata_cache_filename); metadata_cache_json = phosg::JSON::parse(metadata_text); - patch_index_log.info("Loaded patch metadata cache from %s", metadata_cache_filename.c_str()); + patch_index_log.info_f("Loaded patch metadata cache from {}", metadata_cache_filename); } catch (const exception& e) { metadata_cache_json = phosg::JSON::dict(); - patch_index_log.warning("Cannot load patch metadata cache from %s: %s", metadata_cache_filename.c_str(), e.what()); + patch_index_log.warning_f("Cannot load patch metadata cache from {}: {}", metadata_cache_filename, e.what()); } // Assuming it's rare for patch files to change, we skip writing the metadata @@ -54,36 +62,38 @@ PatchFileIndex::PatchFileIndex(const string& root_dir) string relative_dirs = phosg::join(path_directories, "/"); string full_dir_path = root_dir + '/' + relative_dirs; - patch_index_log.info("Listing directory %s", full_dir_path.c_str()); + patch_index_log.info_f("Listing directory {}", full_dir_path); + + for (const auto& dir_item : std::filesystem::directory_iterator(full_dir_path)) { + string item = dir_item.path().filename().string(); - for (const auto& item : phosg::list_directory(full_dir_path)) { // Skip invisible files (e.g. .DS_Store on macOS) - if (phosg::starts_with(item, ".")) { + if (item.starts_with(".")) { continue; } string relative_item_path = relative_dirs + '/' + item; string full_item_path = root_dir + '/' + relative_item_path; - if (phosg::isdir(full_item_path)) { + if (std::filesystem::is_directory(full_item_path)) { collect_dir(item); - } else if (phosg::isfile(full_item_path)) { - - auto st = phosg::stat(full_item_path); + } else if (std::filesystem::is_regular_file(full_item_path)) { auto f = make_shared(this); f->path_directories = path_directories; f->name = item; + int64_t file_mtime = file_mtime_int(full_item_path); + string compute_crc32s_message; // If not empty, should compute crc32s phosg::JSON cache_item_json; try { cache_item_json = metadata_cache_json.at(relative_item_path); uint64_t cached_size = cache_item_json.get_int(0); - uint64_t cached_mtime = cache_item_json.get_int(1); - if (static_cast(st.st_mtime) != cached_mtime) { + int64_t cached_mtime = cache_item_json.get_int(1); + if (file_mtime != cached_mtime) { throw runtime_error("file has been modified"); } - if (static_cast(st.st_size) != cached_size) { + if (std::filesystem::file_size(full_item_path) != cached_size) { throw runtime_error("file size has changed"); } f->size = cached_size; @@ -110,7 +120,7 @@ PatchFileIndex::PatchFileIndex(const string& root_dir) chunk_crcs_item.emplace_back(chunk_crc); } new_metadata_cache_json.emplace( - relative_item_path, phosg::JSON::list({f->size, st.st_mtime, f->crc32, std::move(chunk_crcs_item)})); + relative_item_path, phosg::JSON::list({f->size, file_mtime, f->crc32, std::move(chunk_crcs_item)})); should_write_metadata_cache = true; } else { @@ -123,13 +133,11 @@ PatchFileIndex::PatchFileIndex(const string& root_dir) this->files_by_patch_order.emplace_back(f); this->files_by_name.emplace(relative_item_path, f); if (compute_crc32s_message.empty()) { - patch_index_log.info( - "Added file %s (%" PRIu32 " bytes; %zu chunks; %08" PRIX32 " from cache)", - full_item_path.c_str(), f->size, f->chunk_crcs.size(), f->crc32); + patch_index_log.info_f("Added file {} ({} bytes; {} chunks; {:08X} from cache)", + full_item_path, f->size, f->chunk_crcs.size(), f->crc32); } else { - patch_index_log.info( - "Added file %s (%" PRIu32 " bytes; %zu chunks; %08" PRIX32 " [%s])", - full_item_path.c_str(), f->size, f->chunk_crcs.size(), f->crc32, compute_crc32s_message.c_str()); + patch_index_log.info_f("Added file {} ({} bytes; {} chunks; {:08X} [{}])", + full_item_path, f->size, f->chunk_crcs.size(), f->crc32, compute_crc32s_message); } } } @@ -142,12 +150,12 @@ PatchFileIndex::PatchFileIndex(const string& root_dir) if (should_write_metadata_cache) { try { phosg::save_file(metadata_cache_filename, new_metadata_cache_json.serialize()); - patch_index_log.info("Saved patch metadata cache to %s", metadata_cache_filename.c_str()); + patch_index_log.info_f("Saved patch metadata cache to {}", metadata_cache_filename); } catch (const exception& e) { - patch_index_log.warning("Cannot save patch metadata cache to %s: %s", metadata_cache_filename.c_str(), e.what()); + patch_index_log.warning_f("Cannot save patch metadata cache to {}: {}", metadata_cache_filename, e.what()); } } else { - patch_index_log.info("No files were modified; skipping metadata cache update"); + patch_index_log.info_f("No files were modified; skipping metadata cache update"); } } diff --git a/src/PatchServer.cc b/src/PatchServer.cc deleted file mode 100644 index 89d24bfb..00000000 --- a/src/PatchServer.cc +++ /dev/null @@ -1,478 +0,0 @@ -#include "PatchServer.hh" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -#include "EventUtils.hh" -#include "Loggers.hh" -#include "PSOProtocol.hh" -#include "ReceiveCommands.hh" - -using namespace std; - -static atomic next_id(1); - -PatchServer::Client::Client( - shared_ptr server, - struct bufferevent* bev, - Version version, - uint64_t idle_timeout_usecs, - bool hide_data_from_logs) - : server(server), - id(next_id++), - log(phosg::string_printf("[C-%" PRIX64 "] ", this->id), client_log.min_level), - channel(bev, 0, version, 1, nullptr, nullptr, this, phosg::string_printf("C-%" PRIX64, this->id), phosg::TerminalFormat::FG_YELLOW, phosg::TerminalFormat::FG_GREEN), - idle_timeout_usecs(idle_timeout_usecs), - idle_timeout_event( - event_new(bufferevent_get_base(bev), -1, EV_TIMEOUT, &PatchServer::Client::dispatch_idle_timeout, this), - event_free) { - this->reschedule_timeout_event(); - - // Don't print data sent to patch clients to the logs. The patch server - // protocol is fully understood and data logs for patch clients are generally - // more annoying than helpful at this point. - if (hide_data_from_logs) { - this->channel.terminal_recv_color = phosg::TerminalFormat::END; - this->channel.terminal_send_color = phosg::TerminalFormat::END; - } - - this->log.info("Created"); -} - -void PatchServer::Client::reschedule_timeout_event() { - struct timeval idle_tv = phosg::usecs_to_timeval(this->idle_timeout_usecs); - event_add(this->idle_timeout_event.get(), &idle_tv); -} - -void PatchServer::Client::dispatch_idle_timeout(evutil_socket_t, short, void* ctx) { - reinterpret_cast(ctx)->idle_timeout(); -} - -void PatchServer::Client::idle_timeout() { - this->log.info("Idle timeout expired"); - auto s = this->server.lock(); - if (s) { - auto c = this->shared_from_this(); - s->disconnect_client(c); - } else { - this->channel.disconnect(); - this->log.info("Server is deleted; cannot disconnect client"); - } -} - -void PatchServer::send_server_init(shared_ptr c) const { - uint32_t server_key = phosg::random_object(); - uint32_t client_key = phosg::random_object(); - - S_ServerInit_Patch_02 cmd; - cmd.copyright.encode("Patch Server. Copyright SonicTeam, LTD. 2001"); - cmd.server_key = server_key; - cmd.client_key = client_key; - c->channel.send(0x02, 0x00, cmd); - - c->channel.crypt_out = make_shared(server_key); - c->channel.crypt_in = make_shared(client_key); -} - -void PatchServer::send_message_box(shared_ptr c, const string& text) const { - phosg::StringWriter w; - try { - if (c->version() == Version::PC_PATCH) { - w.write(tt_encode_marked_optional(text, c->channel.language, true)); - } else if (c->version() == Version::BB_PATCH) { - w.write(tt_encode_marked_optional(add_color(text), c->channel.language, true)); - } else { - throw logic_error("non-patch client on patch server"); - } - } catch (const runtime_error& e) { - phosg::log_warning("Failed to encode message for patch message box command: %s", e.what()); - return; - } - w.put_u16(0); - while (w.str().size() & 3) { - w.put_u8(0); - } - c->channel.send(0x13, 0x00, w.str()); -} - -void PatchServer::send_enter_directory(shared_ptr c, const string& dir) const { - S_EnterDirectory_Patch_09 cmd = {{dir, 1}}; - c->channel.send(0x09, 0x00, cmd); -} - -void PatchServer::on_02(shared_ptr c, string& data) { - check_size_v(data.size(), 0); - c->channel.send(0x04, 0x00); // This requests the user's login information -} - -void PatchServer::change_to_directory( - shared_ptr c, - vector& client_path_directories, - const vector& file_path_directories) const { - // First, exit all leaf directories that don't match the desired path - while (!client_path_directories.empty() && - ((client_path_directories.size() > file_path_directories.size()) || - (client_path_directories.back() != file_path_directories[client_path_directories.size() - 1]))) { - c->channel.send(0x0A, 0x00); - client_path_directories.pop_back(); - } - - // At this point, client_path_directories should be a prefix of - // file_path_directories (or should match exactly) - if (client_path_directories.size() > file_path_directories.size()) { - throw logic_error("did not exit all necessary directories"); - } - for (size_t x = 0; x < client_path_directories.size(); x++) { - if (client_path_directories[x] != file_path_directories[x]) { - throw logic_error("intermediate path is not a prefix of final path"); - } - } - - // Second, enter all necessary leaf directories - while (client_path_directories.size() < file_path_directories.size()) { - const string& dir = file_path_directories[client_path_directories.size()]; - this->send_enter_directory(c, dir); - client_path_directories.emplace_back(dir); - } -} - -void PatchServer::on_04(shared_ptr c, string& data) { - const auto& cmd = check_size_t(data); - - string username = cmd.username.decode(); - string password = cmd.password.decode(); - - // There are 3 cases here: - // - No login information at all: just proceed without checking credentials - // - Username: check that account exists if allow_unregistered_users is off - // - Username and password: call verify_bb - if (!username.empty() && !password.empty()) { - try { - this->config->account_index->from_bb_credentials(username, &password, false); - - } catch (const AccountIndex::incorrect_password& e) { - c->channel.send(0x15, 0x03); - this->disconnect_client(c); - return; - - } catch (const AccountIndex::missing_account& e) { - if (!this->config->allow_unregistered_users) { - c->channel.send(0x15, 0x08); - this->disconnect_client(c); - return; - } - } - - } else if (!username.empty() && !this->config->allow_unregistered_users) { - try { - this->config->account_index->from_bb_credentials(username, nullptr, false); - } catch (const AccountIndex::missing_account& e) { - c->channel.send(0x15, 0x08); - this->disconnect_client(c); - return; - } - } - - if (!this->config->message.empty()) { - this->send_message_box(c, this->config->message.c_str()); - } - - const auto& index = this->config->patch_file_index; - if (index.get()) { - c->channel.send(0x0B, 0x00); // Start patch session; go to root directory - - vector path_directories; - for (const auto& file : index->all_files()) { - this->change_to_directory(c, path_directories, file->path_directories); - - S_FileChecksumRequest_Patch_0C req = {c->patch_file_checksum_requests.size(), {file->name, 1}}; - c->channel.send(0x0C, 0x00, req); - c->patch_file_checksum_requests.emplace_back(file); - } - this->change_to_directory(c, path_directories, {}); - - c->channel.send(0x0D, 0x00); // End of checksum requests - - } else { - // No patch index present: just do something that will satisfy the client - // without actually checking or downloading any files - this->send_enter_directory(c, "."); - this->send_enter_directory(c, "data"); - this->send_enter_directory(c, "scene"); - c->channel.send(0x0A, 0x00); - c->channel.send(0x0A, 0x00); - c->channel.send(0x0A, 0x00); - c->channel.send(0x12, 0x00); - } -} - -void PatchServer::on_0F(shared_ptr c, string& data) { - auto& cmd = check_size_t(data); - auto& req = c->patch_file_checksum_requests.at(cmd.request_id); - req.crc32 = cmd.checksum; - req.size = cmd.size; - req.response_received = true; -} - -void PatchServer::on_10(shared_ptr c, string&) { - S_StartFileDownloads_Patch_11 start_cmd = {0, 0}; - for (const auto& req : c->patch_file_checksum_requests) { - if (!req.response_received) { - throw runtime_error("client did not respond to checksum request"); - } - if (req.needs_update()) { - c->log.info("File %s needs update (CRC: %08" PRIX32 "/%08" PRIX32 ", size: %" PRIu32 "/%" PRIu32 ")", - req.file->name.c_str(), req.file->crc32, req.crc32, req.file->size, req.size); - start_cmd.total_bytes += req.file->size; - start_cmd.num_files++; - } else { - c->log.info("File %s is up to date", req.file->name.c_str()); - } - } - - if (start_cmd.num_files) { - c->channel.send(0x11, 0x00, start_cmd); - vector path_directories; - for (const auto& req : c->patch_file_checksum_requests) { - if (req.needs_update()) { - this->change_to_directory(c, path_directories, req.file->path_directories); - - S_OpenFile_Patch_06 open_cmd = {0, req.file->size, {req.file->name, 1}}; - c->channel.send(0x06, 0x00, open_cmd); - - for (size_t x = 0; x < req.file->chunk_crcs.size(); x++) { - auto data = req.file->load_data(); - size_t chunk_size = min(req.file->size - (x * 0x4000), 0x4000); - - vector> blocks; - S_WriteFileHeader_Patch_07 cmd_header = {x, req.file->chunk_crcs[x], chunk_size}; - blocks.emplace_back(&cmd_header, sizeof(cmd_header)); - blocks.emplace_back(data->data() + (x * 0x4000), chunk_size); - c->channel.send(0x07, 0x00, blocks); - } - - S_CloseCurrentFile_Patch_08 close_cmd = {0}; - c->channel.send(0x08, 0x00, close_cmd); - } - } - this->change_to_directory(c, path_directories, {}); - } - - c->channel.send(0x12, 0x00); -} - -void PatchServer::disconnect_client(shared_ptr c) { - if (c->channel.virtual_network_id) { - server_log.info("Client disconnected: C-%" PRIX64 " on N-%" PRIu64, c->id, c->channel.virtual_network_id); - } else if (c->channel.bev) { - server_log.info("Client disconnected: C-%" PRIX64, c->id); - } else { - server_log.info("Client C-%" PRIX64 " removed from patch server", c->id); - } - - this->channel_to_client.erase(&c->channel); - c->channel.disconnect(); - - // We can't just let c be destroyed here, since disconnect_client can be - // called from within the client's channel's receive handler. So, we instead - // move it to another set, which we'll clear in an immediately-enqueued - // callback after the current event. This will also call the client's - // disconnect hooks (if any). - this->clients_to_destroy.insert(std::move(c)); - this->enqueue_destroy_clients(); -} - -void PatchServer::enqueue_destroy_clients() { - auto tv = phosg::usecs_to_timeval(0); - event_add(this->destroy_clients_ev.get(), &tv); -} - -void PatchServer::dispatch_destroy_clients(evutil_socket_t, short, void* ctx) { - reinterpret_cast(ctx)->clients_to_destroy.clear(); -} - -void PatchServer::dispatch_on_listen_accept( - struct evconnlistener* listener, evutil_socket_t fd, - struct sockaddr* address, int socklen, void* ctx) { - reinterpret_cast(ctx)->on_listen_accept(listener, fd, address, socklen); -} - -void PatchServer::dispatch_on_listen_error( - struct evconnlistener* listener, void* ctx) { - reinterpret_cast(ctx)->on_listen_error(listener); -} - -void PatchServer::on_listen_accept(struct evconnlistener* listener, evutil_socket_t fd, struct sockaddr*, int) { - struct sockaddr_storage remote_addr; - phosg::get_socket_addresses(fd, nullptr, &remote_addr); - if (this->config->banned_ipv4_ranges->check(remote_addr)) { - close(fd); - return; - } - - int listen_fd = evconnlistener_get_fd(listener); - ListeningSocket* listening_socket; - try { - listening_socket = &this->listening_sockets.at(listen_fd); - } catch (const out_of_range& e) { - server_log.warning("Can\'t determine version for socket %d; disconnecting client", listen_fd); - close(fd); - return; - } - - struct bufferevent* bev = bufferevent_socket_new(this->base.get(), fd, BEV_OPT_CLOSE_ON_FREE | BEV_OPT_DEFER_CALLBACKS); - auto c = make_shared( - this->shared_from_this(), - bev, - listening_socket->version, - this->config->idle_timeout_usecs, - this->config->hide_data_from_logs); - c->channel.on_command_received = PatchServer::on_client_input; - c->channel.on_error = PatchServer::on_client_error; - c->channel.context_obj = this; - this->channel_to_client.emplace(&c->channel, c); - - server_log.info("Patch client connected: C-%" PRIX64 " on fd %d via %d (%s)", - c->id, fd, listen_fd, listening_socket->addr_str.c_str()); - - this->send_server_init(c); -} - -void PatchServer::on_listen_error(struct evconnlistener* listener) { - int err = EVUTIL_SOCKET_ERROR(); - server_log.error("Failure on listening socket %d: %d (%s)", - evconnlistener_get_fd(listener), err, evutil_socket_error_to_string(err)); - event_base_loopexit(this->base.get(), nullptr); -} - -void PatchServer::on_client_input(Channel& ch, uint16_t command, uint32_t, std::string& data) { - PatchServer* server = reinterpret_cast(ch.context_obj); - shared_ptr c = server->channel_to_client.at(&ch); - - try { - switch (command) { - case 0x02: - server->on_02(c, data); - break; - case 0x04: - server->on_04(c, data); - break; - case 0x0F: - server->on_0F(c, data); - break; - case 0x10: - server->on_10(c, data); - break; - default: - throw runtime_error("invalid command"); - } - } catch (const exception& e) { - server_log.warning("Error processing client command: %s", e.what()); - } -} - -void PatchServer::on_client_error(Channel& ch, short events) { - PatchServer* server = reinterpret_cast(ch.context_obj); - shared_ptr c = server->channel_to_client.at(&ch); - - if (events & BEV_EVENT_ERROR) { - int err = EVUTIL_SOCKET_ERROR(); - server_log.warning("Client caused error %d (%s)", err, evutil_socket_error_to_string(err)); - } - if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) { - server->disconnect_client(c); - } -} - -PatchServer::PatchServer(shared_ptr config) - : config(config) { - if (config->shared_base) { - this->base = config->shared_base; - this->base_is_shared = true; - } else { - this->base.reset(event_base_new(), event_base_free); - this->base_is_shared = false; - } - this->destroy_clients_ev.reset( - event_new(this->base.get(), -1, EV_TIMEOUT, &PatchServer::dispatch_destroy_clients, this), event_free); - if (!this->base_is_shared) { - this->th = thread(&PatchServer::thread_fn, this); - } -} - -void PatchServer::schedule_stop() { - if (!this->base_is_shared) { - event_base_loopexit(this->base.get(), nullptr); - } -} - -void PatchServer::wait_for_stop() { - if (!this->base_is_shared) { - this->th.join(); - } -} - -void PatchServer::listen(const std::string& addr_str, const string& socket_path, Version version) { - int fd = phosg::listen(socket_path, 0, SOMAXCONN); - server_log.info("Listening on Unix socket %s on fd %d as %s", socket_path.c_str(), fd, addr_str.c_str()); - this->add_socket(addr_str, fd, version); -} - -void PatchServer::listen(const std::string& addr_str, const string& addr, int port, Version version) { - if (port == 0) { - this->listen(addr_str, addr, version); - } else { - int fd = phosg::listen(addr, port, SOMAXCONN); - string netloc_str = phosg::render_netloc(addr, port); - server_log.info("Listening on TCP interface %s on fd %d as %s", netloc_str.c_str(), fd, addr_str.c_str()); - this->add_socket(addr_str, fd, version); - } -} - -void PatchServer::listen(const std::string& addr_str, int port, Version version) { - this->listen(addr_str, "", port, version); -} - -PatchServer::ListeningSocket::ListeningSocket(PatchServer* s, const std::string& addr_str, int fd, Version version) - : addr_str(addr_str), - fd(fd), - version(version), - listener(evconnlistener_new(s->base.get(), PatchServer::dispatch_on_listen_accept, s, LEV_OPT_REUSEABLE, 0, this->fd), - evconnlistener_free) { - evconnlistener_set_error_cb(this->listener.get(), PatchServer::dispatch_on_listen_error); -} - -void PatchServer::add_socket(const std::string& addr_str, int fd, Version version) { - this->listening_sockets.emplace(piecewise_construct, forward_as_tuple(fd), forward_as_tuple(this, addr_str, fd, version)); -} - -void PatchServer::thread_fn() { - event_base_loop(this->base.get(), EVLOOP_NO_EXIT_ON_EMPTY); -} - -void PatchServer::set_config(std::shared_ptr config) { - if (this->base_is_shared) { - this->config = config; - } else { - forward_to_event_thread(this->base, [s = this->shared_from_this(), config = std::move(config)]() { - s->config = config; - }); - } -} diff --git a/src/PatchServer.hh b/src/PatchServer.hh deleted file mode 100644 index b60f9776..00000000 --- a/src/PatchServer.hh +++ /dev/null @@ -1,128 +0,0 @@ -#pragma once - -#include -#include - -#include -#include -#include -#include - -#include "Account.hh" -#include "Channel.hh" -#include "IPV4RangeSet.hh" -#include "PatchFileIndex.hh" -#include "Version.hh" - -class PatchServer : public std::enable_shared_from_this { -public: - struct Config { - bool allow_unregistered_users; - bool hide_data_from_logs; - uint64_t idle_timeout_usecs; - std::string message; - std::shared_ptr account_index; - std::shared_ptr patch_file_index; - std::shared_ptr banned_ipv4_ranges; - std::shared_ptr shared_base; - }; - - PatchServer() = delete; - explicit PatchServer(std::shared_ptr config); - PatchServer(const PatchServer&) = delete; - PatchServer(PatchServer&&) = delete; - PatchServer& operator=(const PatchServer&) = delete; - PatchServer& operator=(PatchServer&&) = delete; - virtual ~PatchServer() = default; - - void schedule_stop(); - void wait_for_stop(); - - void listen(const std::string& addr_str, const std::string& socket_path, Version version); - void listen(const std::string& addr_str, const std::string& addr, int port, Version version); - void listen(const std::string& addr_str, int port, Version version); - void add_socket(const std::string& addr_str, int fd, Version version); - - void set_config(std::shared_ptr config); - -private: - class Client : public std::enable_shared_from_this { - public: - std::weak_ptr server; - uint64_t id; - phosg::PrefixedLogger log; - - Channel channel; - std::vector patch_file_checksum_requests; - uint64_t idle_timeout_usecs; - - std::unique_ptr idle_timeout_event; - - Client( - std::shared_ptr server, - struct bufferevent* bev, - Version version, - uint64_t idle_timeout_usecs, - bool hide_data_from_logs); - ~Client() = default; - - void reschedule_timeout_event(); - - inline Version version() const { - return this->channel.version; - } - - static void dispatch_idle_timeout(evutil_socket_t, short, void* ctx); - void idle_timeout(); - }; - - struct ListeningSocket { - std::string addr_str; - int fd; - Version version; - std::unique_ptr listener; - - ListeningSocket(PatchServer* s, const std::string& name, int fd, Version version); - }; - - std::shared_ptr base; - bool base_is_shared; - std::shared_ptr config; - - std::unordered_set> clients_to_destroy; - std::shared_ptr destroy_clients_ev; - - std::unordered_map listening_sockets; - std::unordered_map> channel_to_client; - - std::thread th; - - void send_server_init(std::shared_ptr c) const; - void send_message_box(std::shared_ptr c, const std::string& text) const; - void send_enter_directory(std::shared_ptr c, const std::string& dir) const; - void change_to_directory( - std::shared_ptr c, - std::vector& client_path_directories, - const std::vector& file_path_directories) const; - void on_02(std::shared_ptr c, std::string& data); - void on_04(std::shared_ptr c, std::string& data); - void on_0F(std::shared_ptr c, std::string& data); - void on_10(std::shared_ptr c, std::string& data); - - void disconnect_client(std::shared_ptr c); - - void enqueue_destroy_clients(); - static void dispatch_destroy_clients(evutil_socket_t, short, void* ctx); - - static void dispatch_on_listen_accept(struct evconnlistener* listener, - evutil_socket_t fd, struct sockaddr* address, int socklen, void* ctx); - static void dispatch_on_listen_error(struct evconnlistener* listener, void* ctx); - - void on_listen_accept(struct evconnlistener* listener, evutil_socket_t fd, struct sockaddr* address, int socklen); - void on_listen_error(struct evconnlistener* listener); - - static void on_client_input(Channel& ch, uint16_t command, uint32_t flag, std::string& data); - static void on_client_error(Channel& ch, short events); - - void thread_fn(); -}; diff --git a/src/PlayerFilesManager.cc b/src/PlayerFilesManager.cc index 4a91412c..c499deb4 100644 --- a/src/PlayerFilesManager.cc +++ b/src/PlayerFilesManager.cc @@ -19,27 +19,10 @@ using namespace std; -PlayerFilesManager::PlayerFilesManager(std::shared_ptr base) - : base(base), - clear_expired_files_event( - event_new(this->base.get(), -1, EV_TIMEOUT | EV_PERSIST, &PlayerFilesManager::clear_expired_files, this), - event_free) { - auto tv = phosg::usecs_to_timeval(30 * 1000 * 1000); - event_add(this->clear_expired_files_event.get(), &tv); -} - -template -size_t erase_unused(std::unordered_map>& m) { - size_t ret = 0; - for (auto it = m.begin(); it != m.end();) { - if (it->second.use_count() <= 1) { - it = m.erase(it); - ret++; - } else { - it++; - } - } - return ret; +PlayerFilesManager::PlayerFilesManager(std::shared_ptr io_context) + : io_context(io_context), + clear_expired_files_timer(*this->io_context) { + this->schedule_callback(); } std::shared_ptr PlayerFilesManager::get_system(const std::string& filename) { @@ -98,22 +81,42 @@ void PlayerFilesManager::set_bank(const std::string& filename, std::shared_ptr

(ctx); - size_t num_deleted = erase_unused(self->loaded_system_files); - if (num_deleted) { - player_data_log.info("Cleared %zu expired system file(s)", num_deleted); - } - num_deleted = erase_unused(self->loaded_character_files); - if (num_deleted) { - player_data_log.info("Cleared %zu expired character file(s)", num_deleted); - } - num_deleted = erase_unused(self->loaded_guild_card_files); - if (num_deleted) { - player_data_log.info("Cleared %zu expired Guild Card file(s)", num_deleted); - } - num_deleted = erase_unused(self->loaded_bank_files); - if (num_deleted) { - player_data_log.info("Cleared %zu expired bank file(s)", num_deleted); - } +void PlayerFilesManager::schedule_callback() { + this->clear_expired_files_timer.expires_after(std::chrono::seconds(30)); + this->clear_expired_files_timer.async_wait(bind(&PlayerFilesManager::clear_expired_files, this)); +} + +template +size_t erase_unused(std::unordered_map>& m) { + size_t ret = 0; + for (auto it = m.begin(); it != m.end();) { + if (it->second.use_count() <= 1) { + it = m.erase(it); + ret++; + } else { + it++; + } + } + return ret; +} + +void PlayerFilesManager::clear_expired_files() { + size_t num_deleted = erase_unused(this->loaded_system_files); + if (num_deleted) { + player_data_log.info_f("Cleared {} expired system file(s)", num_deleted); + } + num_deleted = erase_unused(this->loaded_character_files); + if (num_deleted) { + player_data_log.info_f("Cleared {} expired character file(s)", num_deleted); + } + num_deleted = erase_unused(this->loaded_guild_card_files); + if (num_deleted) { + player_data_log.info_f("Cleared {} expired Guild Card file(s)", num_deleted); + } + num_deleted = erase_unused(this->loaded_bank_files); + if (num_deleted) { + player_data_log.info_f("Cleared {} expired bank file(s)", num_deleted); + } + + this->schedule_callback(); } diff --git a/src/PlayerFilesManager.hh b/src/PlayerFilesManager.hh index 007a35db..1c858527 100644 --- a/src/PlayerFilesManager.hh +++ b/src/PlayerFilesManager.hh @@ -1,10 +1,10 @@ #pragma once -#include #include #include #include +#include #include #include #include @@ -21,7 +21,7 @@ class PlayerFilesManager { public: - explicit PlayerFilesManager(std::shared_ptr base); + explicit PlayerFilesManager(std::shared_ptr io_context); ~PlayerFilesManager() = default; std::shared_ptr get_system(const std::string& filename); @@ -35,13 +35,14 @@ public: void set_bank(const std::string& filename, std::shared_ptr file); private: - std::shared_ptr base; - std::unique_ptr clear_expired_files_event; + std::shared_ptr io_context; + asio::steady_timer clear_expired_files_timer; std::unordered_map> loaded_system_files; std::unordered_map> loaded_character_files; std::unordered_map> loaded_guild_card_files; std::unordered_map> loaded_bank_files; - static void clear_expired_files(evutil_socket_t fd, short events, void* ctx); + void schedule_callback(); + void clear_expired_files(); }; diff --git a/src/PlayerInventory.hh b/src/PlayerInventory.hh index 29ff690e..82cf57f0 100644 --- a/src/PlayerInventory.hh +++ b/src/PlayerInventory.hh @@ -72,7 +72,7 @@ struct PlayerInventoryItemT { ret.unknown_a1 = this->unknown_a1; ret.extension_data1 = this->extension_data1; ret.extension_data2 = this->extension_data2; - ret.flags = this->flags.load(); + ret.flags = this->flags; ret.data = this->data; ret.data.id.store_raw(phosg::bswap32(ret.data.id.load_raw())); return ret; @@ -97,8 +97,8 @@ struct PlayerBankItemT { operator PlayerBankItemT() const { PlayerBankItemT ret; ret.data = this->data; - ret.amount = this->amount.load(); - ret.present = this->present.load(); + ret.amount = this->amount; + ret.present = this->present; return ret; } } __attribute__((packed)); @@ -409,8 +409,8 @@ struct PlayerBankT { template operator PlayerBankT() const { PlayerBankT ret; - ret.num_items = std::min(ret.items.size(), this->num_items.load()); - ret.meseta = this->meseta.load(); + ret.num_items = std::min(ret.items.size(), this->num_items); + ret.meseta = this->meseta; for (size_t z = 0; z < std::min(ret.items.size(), this->items.size()); z++) { ret.items[z] = this->items[z]; } diff --git a/src/PlayerSubordinates.hh b/src/PlayerSubordinates.hh index 892ef694..c99bdfcd 100644 --- a/src/PlayerSubordinates.hh +++ b/src/PlayerSubordinates.hh @@ -209,25 +209,25 @@ struct PlayerVisualConfigT { PlayerVisualConfigT ret; ret.name = this->name; ret.unknown_a2 = this->unknown_a2; - ret.name_color = this->name_color.load(); + ret.name_color = this->name_color; ret.extra_model = this->extra_model; ret.unused = this->unused; - ret.name_color_checksum = this->name_color_checksum.load(); + ret.name_color_checksum = this->name_color_checksum; ret.section_id = this->section_id; ret.char_class = this->char_class; ret.validation_flags = this->validation_flags; ret.version = this->version; - ret.class_flags = this->class_flags.load(); - ret.costume = this->costume.load(); - ret.skin = this->skin.load(); - ret.face = this->face.load(); - ret.head = this->head.load(); - ret.hair = this->hair.load(); - ret.hair_r = this->hair_r.load(); - ret.hair_g = this->hair_g.load(); - ret.hair_b = this->hair_b.load(); - ret.proportion_x = this->proportion_x.load(); - ret.proportion_y = this->proportion_y.load(); + ret.class_flags = this->class_flags; + ret.costume = this->costume; + ret.skin = this->skin; + ret.face = this->face; + ret.head = this->head; + ret.hair = this->hair; + ret.hair_r = this->hair_r; + ret.hair_g = this->hair_g; + ret.hair_b = this->hair_b; + ret.proportion_x = this->proportion_x; + ret.proportion_y = this->proportion_y; return ret; } } __attribute__((packed)); @@ -428,7 +428,7 @@ struct GuildCardBB { operator GuildCardGCT() const { GuildCardGCT ret; ret.player_tag = 0x00010000; - ret.guild_card_number = this->guild_card_number.load(); + ret.guild_card_number = this->guild_card_number; ret.name.encode(this->name.decode(this->language), this->language); ret.description.encode(this->description.decode(this->language), this->language); ret.present = this->present; @@ -443,7 +443,7 @@ struct GuildCardBB { template GuildCardGCT::operator GuildCardBB() const { GuildCardBB ret; - ret.guild_card_number = this->guild_card_number.load(); + ret.guild_card_number = this->guild_card_number; ret.name.encode(this->name.decode(this->language), this->language); ret.description.encode(this->description.decode(this->language), this->language); ret.present = this->present; @@ -526,7 +526,7 @@ struct ChallengeAwardStateT { operator ChallengeAwardStateT() const { ChallengeAwardStateT ret; - ret.rank_award_flags = this->rank_award_flags.load(); + ret.rank_award_flags = this->rank_award_flags; ret.maximum_rank = this->maximum_rank; return ret; } @@ -573,33 +573,30 @@ template struct PlayerRecordsChallengeV3T { // Offsets are (1) relative to start of C5 entry, and (2) relative to start // of save file structure - struct Stats { - /* 00:1C */ U16T title_color = 0x7FFF; // XRGB1555 - /* 02:1E */ parray unknown_u0; - /* 04:20 */ parray, 9> times_ep1_online; - /* 28:44 */ parray, 5> times_ep2_online; - /* 3C:58 */ parray, 9> times_ep1_offline; - /* 60:7C */ uint8_t grave_is_ep2 = 0; - /* 61:7D */ uint8_t grave_stage_num = 0; - /* 62:7E */ uint8_t grave_floor = 0; - /* 63:7F */ uint8_t unknown_g0 = 0; - /* 64:80 */ U16T grave_deaths = 0; - /* 66:82 */ parray unknown_u4; - /* 68:84 */ U32T grave_time = 0; // Encoded as in PlayerRecordsChallengeDCPC - /* 6C:88 */ U32T grave_defeated_by_enemy_rt_index = 0; - /* 70:8C */ F32T grave_x = 0.0f; - /* 74:90 */ F32T grave_y = 0.0f; - /* 78:94 */ F32T grave_z = 0.0f; - /* 7C:98 */ pstring grave_team; - /* 90:AC */ pstring grave_message; - /* B0:CC */ parray unknown_m5; - /* B4:D0 */ parray, 3> unknown_t6; - /* C0:DC */ ChallengeAwardStateT ep1_online_award_state; - /* C8:E4 */ ChallengeAwardStateT ep2_online_award_state; - /* D0:EC */ ChallengeAwardStateT ep1_offline_award_state; - /* D8:F4 */ - } __attribute__((packed)); - /* 0000:001C */ Stats stats; + /* 0000:001C */ U16T title_color = 0x7FFF; // XRGB1555 + /* 0002:001E */ parray unknown_u0; + /* 0004:0020 */ parray, 9> times_ep1_online; + /* 0028:0044 */ parray, 5> times_ep2_online; + /* 003C:0058 */ parray, 9> times_ep1_offline; + /* 0060:007C */ uint8_t grave_is_ep2 = 0; + /* 0061:007D */ uint8_t grave_stage_num = 0; + /* 0062:007E */ uint8_t grave_floor = 0; + /* 0063:007F */ uint8_t unknown_g0 = 0; + /* 0064:0080 */ U16T grave_deaths = 0; + /* 0066:0082 */ parray unknown_u4; + /* 0068:0084 */ U32T grave_time = 0; // Encoded as in PlayerRecordsChallengeDCPC + /* 006C:0088 */ U32T grave_defeated_by_enemy_rt_index = 0; + /* 0070:008C */ F32T grave_x = 0.0f; + /* 0074:0090 */ F32T grave_y = 0.0f; + /* 0078:0094 */ F32T grave_z = 0.0f; + /* 007C:0098 */ pstring grave_team; + /* 0090:00AC */ pstring grave_message; + /* 00B0:00CC */ parray unknown_m5; + /* 00B4:00D0 */ parray, 3> unknown_t6; + /* 00C0:00DC */ ChallengeAwardStateT ep1_online_award_state; + /* 00C8:00E4 */ ChallengeAwardStateT ep2_online_award_state; + /* 00D0:00EC */ ChallengeAwardStateT ep1_offline_award_state; + /* 00D8:00F4 */ // On Episode 3, there are special cases that apply to this field - if the // text ends with certain strings, the player will have particle effects // emanate from their character in the lobby every 2 seconds. The effects are: @@ -616,6 +613,34 @@ using PlayerRecordsChallengeV3BE = PlayerRecordsChallengeV3T; check_struct_size(PlayerRecordsChallengeV3, 0x100); check_struct_size(PlayerRecordsChallengeV3BE, 0x100); +struct PlayerRecordsChallengeEp3 { + /* 00:1C */ be_uint16_t title_color = 0x7FFF; // XRGB1555 + /* 02:1E */ parray unknown_u0; + /* 04:20 */ parray, 9> times_ep1_online; + /* 28:44 */ parray, 5> times_ep2_online; + /* 3C:58 */ parray, 9> times_ep1_offline; + /* 60:7C */ uint8_t grave_is_ep2 = 0; + /* 61:7D */ uint8_t grave_stage_num = 0; + /* 62:7E */ uint8_t grave_floor = 0; + /* 63:7F */ uint8_t unknown_g0 = 0; + /* 64:80 */ be_uint16_t grave_deaths = 0; + /* 66:82 */ parray unknown_u4; + /* 68:84 */ be_uint32_t grave_time = 0; // Encoded as in PlayerRecordsChallengeDCPC + /* 6C:88 */ be_uint32_t grave_defeated_by_enemy_rt_index = 0; + /* 70:8C */ be_float grave_x = 0.0f; + /* 74:90 */ be_float grave_y = 0.0f; + /* 78:94 */ be_float grave_z = 0.0f; + /* 7C:98 */ pstring grave_team; + /* 90:AC */ pstring grave_message; + /* B0:CC */ parray unknown_m5; + /* B4:D0 */ parray unknown_t6; + /* C0:DC */ ChallengeAwardStateT ep1_online_award_state; + /* C8:E4 */ ChallengeAwardStateT ep2_online_award_state; + /* D0:EC */ ChallengeAwardStateT ep1_offline_award_state; + /* D8:F4 */ +} __attribute__((packed)); +check_struct_size(PlayerRecordsChallengeEp3, 0xD8); + struct PlayerRecordsChallengeBB { /* 0000 */ le_uint16_t title_color = 0x7FFF; // XRGB1555 /* 0002 */ parray unknown_u0; @@ -653,32 +678,32 @@ struct PlayerRecordsChallengeBB { template PlayerRecordsChallengeBB(const PlayerRecordsChallengeV3T& rec) - : title_color(rec.stats.title_color.load()), - unknown_u0(rec.stats.unknown_u0), - times_ep1_online(rec.stats.times_ep1_online), - times_ep2_online(rec.stats.times_ep2_online), - times_ep1_offline(rec.stats.times_ep1_offline), - grave_is_ep2(rec.stats.grave_is_ep2), - grave_stage_num(rec.stats.grave_stage_num), - grave_floor(rec.stats.grave_floor), - unknown_g0(rec.stats.unknown_g0), - grave_deaths(rec.stats.grave_deaths.load()), - unknown_u4(rec.stats.unknown_u4), - grave_time(rec.stats.grave_time.load()), - grave_defeated_by_enemy_rt_index(rec.stats.grave_defeated_by_enemy_rt_index.load()), - grave_x(rec.stats.grave_x.load()), - grave_y(rec.stats.grave_y.load()), - grave_z(rec.stats.grave_z.load()), - grave_team(rec.stats.grave_team.decode(), 1), - grave_message(rec.stats.grave_message.decode(), 1), - unknown_m5(rec.stats.unknown_m5), - ep1_online_award_state(rec.stats.ep1_online_award_state), - ep2_online_award_state(rec.stats.ep2_online_award_state), - ep1_offline_award_state(rec.stats.ep1_offline_award_state), + : title_color(rec.title_color), + unknown_u0(rec.unknown_u0), + times_ep1_online(rec.times_ep1_online), + times_ep2_online(rec.times_ep2_online), + times_ep1_offline(rec.times_ep1_offline), + grave_is_ep2(rec.grave_is_ep2), + grave_stage_num(rec.grave_stage_num), + grave_floor(rec.grave_floor), + unknown_g0(rec.unknown_g0), + grave_deaths(rec.grave_deaths), + unknown_u4(rec.unknown_u4), + grave_time(rec.grave_time), + grave_defeated_by_enemy_rt_index(rec.grave_defeated_by_enemy_rt_index), + grave_x(rec.grave_x), + grave_y(rec.grave_y), + grave_z(rec.grave_z), + grave_team(rec.grave_team.decode(), 1), + grave_message(rec.grave_message.decode(), 1), + unknown_m5(rec.unknown_m5), + ep1_online_award_state(rec.ep1_online_award_state), + ep2_online_award_state(rec.ep2_online_award_state), + ep1_offline_award_state(rec.ep1_offline_award_state), rank_title(rec.rank_title.decode(), 1), unknown_l7(rec.unknown_l7) { - for (size_t z = 0; z < std::min(this->unknown_t6.size(), rec.stats.unknown_t6.size()); z++) { - this->unknown_t6[z] = rec.stats.unknown_t6[z].load(); + for (size_t z = 0; z < std::min(this->unknown_t6.size(), rec.unknown_t6.size()); z++) { + this->unknown_t6[z] = rec.unknown_t6[z]; } } @@ -687,31 +712,31 @@ struct PlayerRecordsChallengeBB { template operator PlayerRecordsChallengeV3T() const { PlayerRecordsChallengeV3T ret; - ret.stats.title_color = this->title_color.load(); - ret.stats.unknown_u0 = this->unknown_u0; - ret.stats.times_ep1_online = this->times_ep1_online; - ret.stats.times_ep2_online = this->times_ep2_online; - ret.stats.times_ep1_offline = this->times_ep1_offline; - ret.stats.grave_is_ep2 = this->grave_is_ep2; - ret.stats.grave_stage_num = this->grave_stage_num; - ret.stats.grave_floor = this->grave_floor; - ret.stats.unknown_g0 = this->unknown_g0; - ret.stats.grave_deaths = this->grave_deaths.load(); - ret.stats.unknown_u4 = this->unknown_u4; - ret.stats.grave_time = this->grave_time.load(); - ret.stats.grave_defeated_by_enemy_rt_index = this->grave_defeated_by_enemy_rt_index.load(); - ret.stats.grave_x = this->grave_x.load(); - ret.stats.grave_y = this->grave_y.load(); - ret.stats.grave_z = this->grave_z.load(); - ret.stats.grave_team.encode(this->grave_team.decode(), 1); - ret.stats.grave_message.encode(this->grave_message.decode(), 1); - ret.stats.unknown_m5 = this->unknown_m5; - for (size_t z = 0; z < std::min(ret.stats.unknown_t6.size(), this->unknown_t6.size()); z++) { - ret.stats.unknown_t6[z] = this->unknown_t6[z].load(); + ret.title_color = this->title_color; + ret.unknown_u0 = this->unknown_u0; + ret.times_ep1_online = this->times_ep1_online; + ret.times_ep2_online = this->times_ep2_online; + ret.times_ep1_offline = this->times_ep1_offline; + ret.grave_is_ep2 = this->grave_is_ep2; + ret.grave_stage_num = this->grave_stage_num; + ret.grave_floor = this->grave_floor; + ret.unknown_g0 = this->unknown_g0; + ret.grave_deaths = this->grave_deaths; + ret.unknown_u4 = this->unknown_u4; + ret.grave_time = this->grave_time; + ret.grave_defeated_by_enemy_rt_index = this->grave_defeated_by_enemy_rt_index; + ret.grave_x = this->grave_x; + ret.grave_y = this->grave_y; + ret.grave_z = this->grave_z; + ret.grave_team.encode(this->grave_team.decode(), 1); + ret.grave_message.encode(this->grave_message.decode(), 1); + ret.unknown_m5 = this->unknown_m5; + for (size_t z = 0; z < std::min(ret.unknown_t6.size(), this->unknown_t6.size()); z++) { + ret.unknown_t6[z] = this->unknown_t6[z]; } - ret.stats.ep1_online_award_state = this->ep1_online_award_state; - ret.stats.ep2_online_award_state = this->ep2_online_award_state; - ret.stats.ep1_offline_award_state = this->ep1_offline_award_state; + ret.ep1_online_award_state = this->ep1_online_award_state; + ret.ep2_online_award_state = this->ep2_online_award_state; + ret.ep1_offline_award_state = this->ep1_offline_award_state; ret.rank_title.encode(this->rank_title.decode(), 1); ret.unknown_l7 = this->unknown_l7; return ret; @@ -731,14 +756,14 @@ struct PlayerRecordsBattleT { operator PlayerRecordsBattleT() const { PlayerRecordsBattleT ret; for (size_t z = 0; z < this->place_counts.size(); z++) { - ret.place_counts[z] = this->place_counts[z].load(); + ret.place_counts[z] = this->place_counts[z]; } - ret.disconnect_count = this->disconnect_count.load(); + ret.disconnect_count = this->disconnect_count; for (size_t z = 0; z < this->unknown_a1.size(); z++) { - ret.unknown_a1[z] = this->unknown_a1[z].load(); + ret.unknown_a1[z] = this->unknown_a1[z]; } for (size_t z = 0; z < this->unknown_a2.size(); z++) { - ret.unknown_a2[z] = this->unknown_a2[z].load(); + ret.unknown_a2[z] = this->unknown_a2[z]; } return ret; } @@ -985,9 +1010,9 @@ struct SymbolChatT { operator SymbolChatT() const { SymbolChatT ret; - ret.spec = this->spec.load(); + ret.spec = this->spec; for (size_t z = 0; z < this->corner_objects.size(); z++) { - ret.corner_objects[z] = this->corner_objects[z].load(); + ret.corner_objects[z] = this->corner_objects[z]; } ret.face_parts = this->face_parts; return ret; diff --git a/src/ProxyCommands.cc b/src/ProxyCommands.cc index 2b3d223a..3f505328 100644 --- a/src/ProxyCommands.cc +++ b/src/ProxyCommands.cc @@ -1,13 +1,7 @@ -#include "ProxyServer.hh" +#include "ProxyCommands.hh" -#include #include #include -#include -#include -#include -#include -#include #include #include #include @@ -37,41 +31,20 @@ using namespace std; -struct HandlerResult { - enum class Type { - FORWARD = 0, - SUPPRESS, - MODIFIED, - }; - Type type; - // These are only used if Type is MODIFIED. If either are -1, then the - // original command's value is used instead. - int32_t new_command; - int64_t new_flag; - - HandlerResult(Type type) - : type(type), - new_command(-1), - new_flag(-1) {} - HandlerResult(Type type, uint16_t new_command, uint32_t new_flag) - : type(type), - new_command(new_command), - new_flag(new_flag) {} +enum class HandlerResult { + FORWARD = 0, + SUPPRESS, + MODIFIED, }; -typedef HandlerResult (*on_command_t)( - shared_ptr ses, - uint16_t command, - uint32_t flag, - string& data); +typedef asio::awaitable (*on_message_t)(shared_ptr c, Channel::Message& msg); -static void forward_command(shared_ptr ses, bool to_server, - uint16_t command, uint32_t flag, string& data, bool print_contents = true) { - auto& ch = to_server ? ses->server_channel : ses->client_channel; - if (!ch.connected()) { - proxy_server_log.warning("No endpoint is present; dropping command"); +static void forward_command(shared_ptr c, bool to_server, const Channel::Message& msg, bool print_contents = true) { + auto ch = to_server ? (c->proxy_session ? c->proxy_session->server_channel : nullptr) : c->channel; + if (!ch || !ch->connected()) { + proxy_server_log.warning_f("No endpoint is present; dropping command"); } else { - ch.send(command, flag, data, !print_contents); + ch->send(msg.command, msg.flag, msg.data, !print_contents); } } @@ -86,168 +59,215 @@ static void forward_command(shared_ptr ses, bool to_ // the command handler is for all versions (for example, the 97 handler is like // this). -static HandlerResult default_handler(shared_ptr, uint16_t, uint32_t, string&) { - return HandlerResult::Type::FORWARD; +static asio::awaitable default_handler(shared_ptr, Channel::Message&) { + co_return HandlerResult::FORWARD; } -static HandlerResult S_invalid(shared_ptr ses, uint16_t command, uint32_t flag, string&) { - ses->log.error("Server sent invalid command"); - string error_str = is_v4(ses->version()) - ? phosg::string_printf("Server sent invalid\ncommand: %04hX %08" PRIX32, command, flag) - : phosg::string_printf("Server sent invalid\ncommand: %02hX %02" PRIX32, command, flag); - ses->send_to_game_server(error_str.c_str()); - return HandlerResult::Type::SUPPRESS; +static asio::awaitable S_invalid(shared_ptr c, Channel::Message& msg) { + c->log.error_f("Server sent invalid command"); + string error_str = is_v4(c->version()) + ? std::format("Server sent invalid\ncommand: {:04X} {:08X}", msg.command, msg.flag) + : std::format("Server sent invalid\ncommand: {:02X} {:02X}", msg.command, msg.flag); + c->proxy_session->server_channel->disconnect(); + co_return HandlerResult::SUPPRESS; } -static HandlerResult C_05(shared_ptr ses, uint16_t, uint32_t, string&) { - ses->disconnect_action = is_v4(ses->version()) - ? ProxyServer::LinkedSession::DisconnectAction::MEDIUM_TIMEOUT - : ProxyServer::LinkedSession::DisconnectAction::SHORT_TIMEOUT; - return HandlerResult::Type::FORWARD; -} - -static HandlerResult C_1D(shared_ptr ses, uint16_t, uint32_t, string&) { - if (ses->client_ping_start_time) { - uint64_t ping_usecs = phosg::now() - ses->client_ping_start_time; - ses->client_ping_start_time = 0; +static asio::awaitable C_1D(shared_ptr c, Channel::Message&) { + if (c->ping_start_time) { + uint64_t ping_usecs = phosg::now() - c->ping_start_time; + c->ping_start_time = 0; double ping_ms = static_cast(ping_usecs) / 1000.0; - send_text_message_printf(ses->client_channel, "To proxy: %gms", ping_ms); + send_text_message_fmt(c->channel, "To proxy: {:g}ms", ping_ms); } - return ses->config.check_flag(Client::Flag::PROXY_SUPPRESS_CLIENT_PINGS) - ? HandlerResult::Type::SUPPRESS - : HandlerResult::Type::FORWARD; + co_return HandlerResult::SUPPRESS; } -static HandlerResult S_1D(shared_ptr ses, uint16_t, uint32_t, string&) { - if (ses->config.check_flag(Client::Flag::PROXY_SUPPRESS_CLIENT_PINGS)) { - ses->server_channel.send(0x1D); - return HandlerResult::Type::SUPPRESS; - } else { - return HandlerResult::Type::FORWARD; - } +static asio::awaitable S_1D(shared_ptr c, Channel::Message&) { + c->proxy_session->server_channel->send(0x1D); + co_return HandlerResult::SUPPRESS; } -static HandlerResult S_97(shared_ptr ses, uint16_t, uint32_t, string&) { +static asio::awaitable S_97(shared_ptr c, Channel::Message&) { // We always assume a 97 has already been received by the client - we should // have sent 97 01 before sending the client to the proxy server. - ses->server_channel.send(0xB1, 0x00); - return HandlerResult::Type::SUPPRESS; + c->proxy_session->server_channel->send(0xB1, 0x00); + co_return HandlerResult::SUPPRESS; } -static HandlerResult C_G_9E(shared_ptr ses, uint16_t, uint32_t, string&) { - if (ses->config.check_flag(Client::Flag::PROXY_SUPPRESS_REMOTE_LOGIN) && ses->login) { - le_uint64_t checksum = phosg::random_object() & 0x0000FFFFFFFFFFFF; - ses->server_channel.send(0x96, 0x00, &checksum, sizeof(checksum)); - - S_UpdateClientConfig_V3_04 cmd; - cmd.player_tag = 0x00010000; - cmd.guild_card_number = ses->login->account->account_id; - cmd.client_config.clear(0xFF); - ses->client_channel.send(0x04, 0x00, &cmd, sizeof(cmd)); - - return HandlerResult::Type::SUPPRESS; - - } else { - return HandlerResult::Type::FORWARD; - } +static void send_90_to_server(std::shared_ptr c) { + C_LoginV1_DC_PC_V3_90 cmd; + cmd.serial_number.encode(c->serial_number); + cmd.access_key.encode(c->access_key); + c->proxy_session->server_channel->send(0x90, 0x00, &cmd, sizeof(cmd)); } -static HandlerResult S_G_9A(shared_ptr ses, uint16_t, uint32_t, string&) { - if (ses->config.check_flag(Client::Flag::PROXY_SUPPRESS_REMOTE_LOGIN) || !ses->login || !ses->login->gc_license) { - return HandlerResult::Type::FORWARD; - } - - C_LoginExtended_GC_9E cmd; - if (ses->remote_guild_card_number < 0) { +static void send_93_to_server(std::shared_ptr c) { + C_LoginV1_DC_93 cmd; + if (c->proxy_session->remote_guild_card_number < 0) { cmd.player_tag = 0xFFFF0000; cmd.guild_card_number = 0xFFFFFFFF; } else { cmd.player_tag = 0x00010000; - cmd.guild_card_number = ses->remote_guild_card_number; + cmd.guild_card_number = c->proxy_session->remote_guild_card_number; } - cmd.hardware_id = ses->hardware_id; - cmd.sub_version = ses->effective_sub_version(); - cmd.is_extended = (ses->remote_guild_card_number < 0) ? 1 : 0; - cmd.language = ses->language(); - if (ses->login->gc_license) { - cmd.serial_number.encode(phosg::string_printf("%08" PRIX32 "", ses->login->gc_license->serial_number)); + cmd.hardware_id = c->hardware_id; + cmd.sub_version = c->sub_version; + cmd.is_extended = 0; + cmd.language = c->language(); + cmd.serial_number.encode(c->serial_number); + cmd.access_key.encode(c->access_key); + cmd.serial_number2.encode(c->serial_number2); + cmd.access_key2.encode(c->access_key2); + cmd.login_character_name.encode(c->login_character_name, c->language()); + c->proxy_session->server_channel->send(0x93, 0x00, &cmd, sizeof(cmd)); +} + +static void send_9A_to_server(std::shared_ptr c) { + C_Login_DC_PC_V3_9A cmd; + cmd.v1_serial_number.encode(c->v1_serial_number); + cmd.v1_access_key.encode(c->v1_access_key); + cmd.serial_number.encode(c->serial_number); + cmd.access_key.encode(c->access_key); + if (c->proxy_session->remote_guild_card_number < 0) { + cmd.player_tag = 0xFFFF0000; + cmd.guild_card_number = 0xFFFFFFFF; } else { - cmd.serial_number.encode(phosg::string_printf("%08" PRIX32 "", ses->login->account->account_id)); + cmd.player_tag = 0x00010000; + cmd.guild_card_number = c->proxy_session->remote_guild_card_number; } - cmd.access_key.encode(ses->login->gc_license->access_key); - cmd.serial_number2 = cmd.serial_number; - cmd.access_key2 = cmd.access_key; - if (ses->config.check_flag(Client::Flag::PROXY_BLANK_NAME_ENABLED)) { - cmd.name.encode(" ", ses->language()); + cmd.sub_version = c->sub_version; + cmd.serial_number2.encode(c->serial_number2); + cmd.access_key2.encode(c->access_key2); + cmd.email_address.encode(c->email_address); + c->proxy_session->server_channel->send(0x9A, 0x00, &cmd, sizeof(cmd)); +} + +static void send_9D_to_server(std::shared_ptr c) { + C_Login_DC_PC_GC_9D cmd; + if (c->proxy_session->remote_guild_card_number < 0) { + cmd.player_tag = 0xFFFF0000; + cmd.guild_card_number = 0xFFFFFFFF; } else { - cmd.name.encode(ses->character_name, ses->language()); + cmd.player_tag = 0x00010000; + cmd.guild_card_number = c->proxy_session->remote_guild_card_number; } - cmd.client_config = ses->remote_client_config_data; + cmd.hardware_id = c->hardware_id; + cmd.sub_version = c->sub_version; + cmd.is_extended = 0; + cmd.language = c->language(); + cmd.v1_serial_number.encode(c->v1_serial_number); + cmd.v1_access_key.encode(c->v1_access_key); + cmd.serial_number.encode(c->serial_number); + cmd.access_key.encode(c->access_key); + cmd.serial_number2.encode(c->serial_number2); + cmd.access_key2.encode(c->access_key2); + cmd.login_character_name.encode(c->login_character_name, c->language()); + c->proxy_session->server_channel->send(0x9D, 0x00, &cmd, sizeof(cmd)); +} + +static void send_DB_to_server(std::shared_ptr c) { + C_VerifyAccount_V3_DB cmd; + cmd.v1_serial_number.encode(c->v1_serial_number); + cmd.v1_access_key.encode(c->v1_access_key); + cmd.serial_number.encode(c->serial_number); + cmd.access_key.encode(c->access_key); + cmd.hardware_id = c->hardware_id; + cmd.sub_version = c->sub_version; + cmd.serial_number2.encode(c->serial_number2); + cmd.access_key2.encode(c->access_key2); + cmd.password.encode(c->password); + c->proxy_session->server_channel->send(0xDB, 0x00, &cmd, sizeof(cmd)); +} + +static void send_9E_XB_to_server(std::shared_ptr c) { + C_LoginExtended_XB_9E cmd; + if (c->proxy_session->remote_guild_card_number < 0) { + cmd.player_tag = 0xFFFF0000; + cmd.guild_card_number = 0xFFFFFFFF; + } else { + cmd.player_tag = 0x00010000; + cmd.guild_card_number = c->proxy_session->remote_guild_card_number; + } + cmd.hardware_id = c->hardware_id; + cmd.sub_version = c->sub_version; + cmd.is_extended = (c->proxy_session->remote_guild_card_number < 0) ? 1 : 0; + cmd.language = c->language(); + cmd.v1_serial_number.encode(c->v1_serial_number); + cmd.v1_access_key.encode(c->v1_access_key); + cmd.serial_number.encode(c->serial_number); + cmd.access_key.encode(c->access_key); + cmd.serial_number2.encode(c->serial_number2); + cmd.access_key2.encode(c->access_key2); + cmd.login_character_name.encode(c->login_character_name, c->language()); + cmd.xb_netloc = c->xb_netloc; + cmd.xb_unknown_a1a = c->xb_unknown_a1a; + cmd.xb_user_id_high = (c->xb_user_id >> 32) & 0xFFFFFFFF; + cmd.xb_user_id_low = c->xb_user_id & 0xFFFFFFFF; + cmd.xb_unknown_a1b = c->xb_unknown_a1b; + c->proxy_session->server_channel->send(0x9E, 0x01, &cmd, sizeof(C_LoginExtended_XB_9E)); +} + +static asio::awaitable S_G_9A(shared_ptr c, Channel::Message&) { + // TODO: Either delete this handler or finish implementing it (flag=00/02 + // should do the below, 01 should send 9C, anything else should end the + // session) + C_LoginExtended_GC_9E cmd; + if (c->proxy_session->remote_guild_card_number < 0) { + cmd.player_tag = 0xFFFF0000; + cmd.guild_card_number = 0xFFFFFFFF; + } else { + cmd.player_tag = 0x00010000; + cmd.guild_card_number = c->proxy_session->remote_guild_card_number; + } + cmd.hardware_id = c->hardware_id; + cmd.sub_version = c->sub_version; + cmd.is_extended = (c->proxy_session->remote_guild_card_number < 0) ? 1 : 0; + cmd.language = c->language(); + cmd.v1_serial_number.encode(c->v1_serial_number); + cmd.v1_access_key.encode(c->v1_access_key); + cmd.serial_number.encode(c->serial_number); + cmd.access_key.encode(c->access_key); + cmd.serial_number2.encode(c->serial_number2); + cmd.access_key2.encode(c->access_key2); + cmd.login_character_name.encode(c->login_character_name, c->language()); + cmd.client_config = c->proxy_session->remote_client_config_data; // If there's a guild card number, a shorter 9E is sent that ends // right after the client config data - - ses->server_channel.send( + c->proxy_session->server_channel->send( 0x9E, 0x01, &cmd, cmd.is_extended ? sizeof(C_LoginExtended_GC_9E) : sizeof(C_Login_GC_9E)); - return HandlerResult::Type::SUPPRESS; + + co_return HandlerResult::SUPPRESS; } -static HandlerResult S_V123P_02_17( - shared_ptr ses, - uint16_t command, - uint32_t flag, - string& data) { - if (is_patch(ses->version()) && command == 0x17) { +static asio::awaitable S_V123U_02_17(shared_ptr c, Channel::Message& msg) { + if (is_patch(c->version()) && msg.command == 0x17) { throw invalid_argument("patch server sent 17 server init"); } // Most servers don't include after_message or have a shorter // after_message than newserv does, so don't require it - const auto& cmd = check_size_t(data, 0xFFFF); - - if (!ses->login) { - ses->log.info("Linked session is not logged in"); - - // We have to forward the command BEFORE setting up encryption, so the - // client will be able to understand what we sent. - forward_command(ses, false, command, flag, data); - - if (uses_v3_encryption(ses->version())) { - ses->server_channel.crypt_in = make_shared(cmd.server_key); - ses->server_channel.crypt_out = make_shared(cmd.client_key); - ses->client_channel.crypt_in = make_shared(cmd.client_key); - ses->client_channel.crypt_out = make_shared(cmd.server_key); - } else { // DC, PC, or patch server (they all use V2 encryption) - ses->server_channel.crypt_in = make_shared(cmd.server_key); - ses->server_channel.crypt_out = make_shared(cmd.client_key); - ses->client_channel.crypt_in = make_shared(cmd.client_key); - ses->client_channel.crypt_out = make_shared(cmd.server_key); - } - - return HandlerResult::Type::SUPPRESS; - } - - ses->log.info("Linked session is logged in"); + const auto& cmd = msg.check_size_t(0xFFFF); // This isn't forwarded to the client, so don't recreate the client's crypts - if (uses_v3_encryption(ses->version())) { - ses->server_channel.crypt_in = make_shared(cmd.server_key); - ses->server_channel.crypt_out = make_shared(cmd.client_key); + if (uses_v3_encryption(c->version())) { + c->proxy_session->server_channel->crypt_in = make_shared(cmd.server_key); + c->proxy_session->server_channel->crypt_out = make_shared(cmd.client_key); } else { - ses->server_channel.crypt_in = make_shared(cmd.server_key); - ses->server_channel.crypt_out = make_shared(cmd.client_key); + c->proxy_session->server_channel->crypt_in = make_shared(cmd.server_key); + c->proxy_session->server_channel->crypt_out = make_shared(cmd.client_key); } // Respond with an appropriate login command. We don't let the client do this // because it believes it already did (when it was in an unlinked session, or // in the patch server case, during the current session due to a hidden // redirect). - switch (ses->version()) { + switch (c->version()) { case Version::PC_PATCH: case Version::BB_PATCH: - ses->server_channel.send(0x02); - return HandlerResult::Type::SUPPRESS; + c->proxy_session->server_channel->send(0x02); + co_return HandlerResult::SUPPRESS; case Version::DC_NTE: // TODO @@ -255,36 +275,12 @@ static HandlerResult S_V123P_02_17( case Version::DC_11_2000: case Version::DC_V1: - if (!ses->login->dc_license) { - throw runtime_error("DC license missing from login"); - } - if (command == 0x17) { - C_LoginV1_DC_PC_V3_90 cmd; - cmd.serial_number.encode(phosg::string_printf("%08" PRIX32 "", ses->login->dc_license->serial_number)); - cmd.access_key.encode(ses->login->dc_license->access_key); - cmd.access_key.clear_after_bytes(8); - ses->server_channel.send(0x90, 0x00, &cmd, sizeof(cmd)); - return HandlerResult::Type::SUPPRESS; + if (msg.command == 0x17) { + send_90_to_server(c); + co_return HandlerResult::SUPPRESS; } else { - C_LoginV1_DC_93 cmd; - if (ses->remote_guild_card_number < 0) { - cmd.player_tag = 0xFFFF0000; - cmd.guild_card_number = 0xFFFFFFFF; - } else { - cmd.player_tag = 0x00010000; - cmd.guild_card_number = ses->remote_guild_card_number; - } - cmd.hardware_id = ses->hardware_id; - cmd.sub_version = ses->effective_sub_version(); - cmd.is_extended = 0; - cmd.language = ses->language(); - cmd.serial_number.encode(phosg::string_printf("%08" PRIX32 "", ses->login->dc_license->serial_number)); - cmd.access_key.encode(ses->login->dc_license->access_key); - cmd.access_key.clear_after_bytes(8); - cmd.serial_number2.encode(ses->serial_number2); - cmd.name.encode(ses->character_name); - ses->server_channel.send(0x93, 0x00, &cmd, sizeof(cmd)); - return HandlerResult::Type::SUPPRESS; + send_93_to_server(c); + co_return HandlerResult::SUPPRESS; } break; @@ -292,85 +288,12 @@ static HandlerResult S_V123P_02_17( case Version::PC_NTE: case Version::PC_V2: case Version::GC_NTE: { - uint32_t serial_number; - const string* access_key; - if (ses->version() == Version::DC_V2) { - if (!ses->login->dc_license) { - throw runtime_error("incorrect login type"); - } - serial_number = ses->login->dc_license->serial_number; - access_key = &ses->login->dc_license->access_key; - } else if (ses->version() != Version::GC_NTE) { - if (!ses->login->pc_license) { - throw runtime_error("incorrect login type"); - } - serial_number = ses->login->pc_license->serial_number; - access_key = &ses->login->pc_license->access_key; + if (msg.command == 0x17) { + send_9A_to_server(c); + co_return HandlerResult::SUPPRESS; } else { - if (!ses->login->gc_license) { - throw runtime_error("incorrect login type"); - } - serial_number = ses->login->gc_license->serial_number; - access_key = &ses->login->gc_license->access_key; - } - - if (command == 0x17) { - C_Login_DC_PC_V3_9A cmd; - if (ses->remote_guild_card_number < 0) { - cmd.player_tag = 0xFFFF0000; - cmd.guild_card_number = 0xFFFFFFFF; - } else { - cmd.player_tag = 0x00010000; - cmd.guild_card_number = ses->remote_guild_card_number; - } - cmd.sub_version = ses->effective_sub_version(); - cmd.serial_number.encode(phosg::string_printf("%08" PRIX32 "", serial_number)); - cmd.access_key.encode(*access_key); - if (ses->version() != Version::GC_NTE) { - cmd.access_key.clear_after_bytes(8); - } - if (is_dc(ses->version())) { - cmd.serial_number2.encode(ses->serial_number2); - } else { - cmd.serial_number2 = cmd.serial_number; - } - cmd.access_key2 = cmd.access_key; - // TODO: We probably should set email_address, but we currently don't - // keep that value anywhere in the session object, nor is it saved in - // the Account object. - ses->server_channel.send(0x9A, 0x00, &cmd, sizeof(cmd)); - return HandlerResult::Type::SUPPRESS; - } else { - C_Login_DC_PC_GC_9D cmd; - if (ses->remote_guild_card_number < 0) { - cmd.player_tag = 0xFFFF0000; - cmd.guild_card_number = 0xFFFFFFFF; - } else { - cmd.player_tag = 0x00010000; - cmd.guild_card_number = ses->remote_guild_card_number; - } - cmd.hardware_id = ses->hardware_id; - cmd.sub_version = ses->effective_sub_version(); - cmd.is_extended = 0; - cmd.language = ses->language(); - cmd.serial_number.encode(phosg::string_printf("%08" PRIX32 "", serial_number)); - cmd.access_key.encode(*access_key); - if (ses->version() != Version::GC_NTE) { - cmd.access_key.clear_after_bytes(8); - } - if (is_dc(ses->version())) { - cmd.serial_number2.encode(ses->serial_number2); - } else { - cmd.serial_number2 = cmd.serial_number; - } - cmd.access_key2 = cmd.access_key; - if (ses->config.check_flag(Client::Flag::PROXY_BLANK_NAME_ENABLED)) { - cmd.name.encode(" ", ses->language()); - } else { - cmd.name.encode(ses->character_name); - } - ses->server_channel.send(0x9D, 0x00, &cmd, sizeof(cmd)); - return HandlerResult::Type::SUPPRESS; + send_9D_to_server(c); + co_return HandlerResult::SUPPRESS; } break; } @@ -378,94 +301,18 @@ static HandlerResult S_V123P_02_17( case Version::GC_V3: case Version::GC_EP3_NTE: case Version::GC_EP3: - if (!ses->login->gc_license) { - throw runtime_error("GC license missing from login"); - } - if (command == 0x17) { - C_VerifyAccount_V3_DB cmd; - cmd.serial_number.encode(phosg::string_printf("%08" PRIX32 "", ses->login->gc_license->serial_number)); - cmd.access_key.encode(ses->login->gc_license->access_key); - cmd.sub_version = ses->effective_sub_version(); - cmd.serial_number2 = cmd.serial_number; - cmd.access_key2 = cmd.access_key; - cmd.password.encode(ses->login->gc_license->password); - ses->server_channel.send(0xDB, 0x00, &cmd, sizeof(cmd)); - return HandlerResult::Type::SUPPRESS; - - } else if (ses->config.check_flag(Client::Flag::PROXY_SUPPRESS_REMOTE_LOGIN)) { - uint32_t guild_card_number; - if (ses->remote_guild_card_number >= 0) { - guild_card_number = ses->remote_guild_card_number; - ses->log.info("Using Guild Card number %" PRIu32 " from session", guild_card_number); - } else { - guild_card_number = phosg::random_object(); - ses->log.info("Using Guild Card number %" PRIu32 " from random generator", guild_card_number); - } - - uint32_t fake_serial_number = phosg::random_object() & 0x7FFFFFFF; - uint64_t fake_access_key = phosg::random_object(); - string fake_access_key_str = phosg::string_printf("00000000000%" PRIu64, fake_access_key); - if (fake_access_key_str.size() > 12) { - fake_access_key_str = fake_access_key_str.substr(fake_access_key_str.size() - 12); - } - - C_LoginExtended_GC_9E cmd; - cmd.player_tag = 0x00010000; - cmd.guild_card_number = guild_card_number; - cmd.hardware_id = ses->hardware_id; - cmd.sub_version = ses->effective_sub_version(); - cmd.is_extended = 0; - cmd.language = ses->language(); - cmd.serial_number.encode(phosg::string_printf("%08" PRIX32, fake_serial_number)); - cmd.access_key.encode(fake_access_key_str); - cmd.serial_number2 = cmd.serial_number; - cmd.access_key2 = cmd.access_key; - if (ses->config.check_flag(Client::Flag::PROXY_BLANK_NAME_ENABLED)) { - cmd.name.encode(" ", ses->language()); - } else { - cmd.name.encode(ses->character_name, ses->language()); - } - cmd.client_config = ses->remote_client_config_data; - ses->server_channel.send(0x9E, 0x01, &cmd, sizeof(C_Login_GC_9E)); - return HandlerResult::Type::SUPPRESS; - + if (msg.command == 0x17) { + send_DB_to_server(c); + co_return HandlerResult::SUPPRESS; } else { // For command 02, send the same as if we had received 9A from the server - return S_G_9A(ses, command, flag, data); + co_return co_await S_G_9A(c, msg); } throw logic_error("GC init command not handled"); case Version::XB_V3: { - if (!ses->login->xb_license) { - throw runtime_error("XB license missing from login"); - } - C_LoginExtended_XB_9E cmd; - if (ses->remote_guild_card_number < 0) { - cmd.player_tag = 0xFFFF0000; - cmd.guild_card_number = 0xFFFFFFFF; - } else { - cmd.player_tag = 0x00010000; - cmd.guild_card_number = ses->remote_guild_card_number; - } - cmd.hardware_id = ses->hardware_id; - cmd.sub_version = ses->effective_sub_version(); - cmd.is_extended = (ses->remote_guild_card_number < 0) ? 1 : 0; - cmd.language = ses->language(); - cmd.serial_number.encode(ses->login->xb_license->gamertag); - cmd.access_key.encode(phosg::string_printf("%016" PRIX64, ses->login->xb_license->user_id)); - cmd.serial_number2 = cmd.serial_number; - cmd.access_key2 = cmd.access_key; - if (ses->config.check_flag(Client::Flag::PROXY_BLANK_NAME_ENABLED)) { - cmd.name.encode(" ", ses->language()); - } else { - cmd.name.encode(ses->character_name, ses->language()); - } - cmd.netloc = ses->xb_netloc; - cmd.unknown_a1a = ses->xb_9E_unknown_a1a; - cmd.xb_user_id_high = (ses->login->xb_license->user_id >> 32) & 0xFFFFFFFF; - cmd.xb_user_id_low = ses->login->xb_license->user_id & 0xFFFFFFFF; - ses->server_channel.send(0x9E, 0x01, &cmd, sizeof(C_LoginExtended_XB_9E)); - return HandlerResult::Type::SUPPRESS; + send_9E_XB_to_server(c); + co_return HandlerResult::SUPPRESS; } case Version::BB_V4: @@ -475,74 +322,91 @@ static HandlerResult S_V123P_02_17( } } -static HandlerResult S_B_03(shared_ptr ses, uint16_t, uint32_t, string& data) { - // Most servers don't include after_message or have a shorter after_message - // than newserv does, so don't require it - const auto& cmd = check_size_t(data, 0xFFFF); - - // If the session has a detector crypt, then it was resumed from an unlinked - // session, during which we already sent an 03 command. - if (ses->detector_crypt.get()) { - if (ses->login_command_bb.empty()) { - throw logic_error("linked BB session does not have a saved login command"); - } - - // This isn't forwarded to the client, so only recreate the server's crypts. - // Use the same crypt type as the client... the server has the luxury of - // being able to try all the crypts it knows to detect what type the client - // uses, but the client can't do this since it sends the first encrypted - // data on the connection. - ses->server_channel.crypt_in = make_shared( - ses->detector_crypt, cmd.server_key.data(), sizeof(cmd.server_key), false); - ses->server_channel.crypt_out = make_shared( - ses->detector_crypt, cmd.client_key.data(), sizeof(cmd.client_key), false); - - // Forward the login command we saved during the unlinked ses-> - if (ses->enable_remote_ip_crc_patch && (ses->login_command_bb.size() >= 0x98)) { - *reinterpret_cast(ses->login_command_bb.data() + 0x94) = - ses->remote_ip_crc ^ (1309539928UL + 1248334810UL); - } - ses->server_channel.send(0x93, 0x00, ses->login_command_bb); - - return HandlerResult::Type::SUPPRESS; - - // If there's no detector crypt, then the session is new and was linked - // immediately at connect time, and an 03 was not yet sent to the client, so - // we should forward this one. - } else { - // Forward the command to the client before setting up the crypts, so the - // client receives the unencrypted data - ses->client_channel.send(0x03, 0x00, data); - - ses->detector_crypt = make_shared( - ses->require_server_state()->bb_private_keys, - bb_crypt_initial_client_commands, - cmd.client_key.data(), - sizeof(cmd.client_key)); - ses->client_channel.crypt_in = ses->detector_crypt; - ses->client_channel.crypt_out = make_shared( - ses->detector_crypt, cmd.server_key.data(), sizeof(cmd.server_key), true); - ses->server_channel.crypt_in = make_shared( - ses->detector_crypt, cmd.server_key.data(), sizeof(cmd.server_key), false); - ses->server_channel.crypt_out = make_shared( - ses->detector_crypt, cmd.client_key.data(), sizeof(cmd.client_key), false); - - // We already forwarded the command, so don't do so again - return HandlerResult::Type::SUPPRESS; - } +static asio::awaitable S_U_04(shared_ptr c, Channel::Message&) { + C_Login_Patch_04 ret; + ret.username.encode(c->username); + ret.password.encode(c->password); + ret.email_address.encode(c->email_address); + c->proxy_session->server_channel->send(0x04, 0x00, &ret, sizeof(ret)); + co_return HandlerResult::SUPPRESS; } -static HandlerResult S_V123_04(shared_ptr ses, uint16_t, uint32_t, string& data) { - // Suppress extremely short commands from the server instead of disconnecting. - if (data.size() < offsetof(S_UpdateClientConfig_V3_04, client_config)) { +static asio::awaitable S_B_03(shared_ptr c, Channel::Message& msg) { + // Most servers don't include after_message or have a shorter after_message + // than newserv does, so don't require it + const auto& cmd = msg.check_size_t(0xFFFF); + + // This isn't forwarded to the client, so only recreate the server's crypts. + // Use the same crypt type as the client... the server has the luxury of + // being able to try all the crypts it knows to detect what type the client + // uses, but the client can't do this since it sends the first encrypted + // data on the connection. + if (!c->bb_detector_crypt) { + throw logic_error("Client proxy session started with missing detector crypt"); + } + c->proxy_session->server_channel->crypt_in = make_shared( + c->bb_detector_crypt, cmd.server_key.data(), sizeof(cmd.server_key), false); + c->proxy_session->server_channel->crypt_out = make_shared( + c->bb_detector_crypt, cmd.client_key.data(), sizeof(cmd.client_key), false); + + C_LoginWithHardwareInfo_BB_93 resp; + resp.guild_card_number = c->proxy_session->remote_guild_card_number; + resp.sub_version = c->sub_version; + resp.language = c->language(); + resp.character_slot = c->bb_character_index; + resp.connection_phase = c->bb_connection_phase; + resp.client_code = c->bb_client_code; + resp.security_token = c->proxy_session->remote_bb_security_token; + resp.username.encode(c->username, c->language()); + resp.password.encode(c->password, c->language()); + resp.hardware_id = c->hardware_id; + resp.client_config = c->proxy_session->remote_client_config_data; + if (c->proxy_session->enable_remote_ip_crc_patch) { + *reinterpret_cast(resp.client_config.data() + 0x10) = + c->proxy_session->remote_ip_crc ^ (1309539928UL + 1248334810UL); + } + c->proxy_session->server_channel->send(0x93, 0x00, &resp, sizeof(resp)); + + co_return HandlerResult::SUPPRESS; +} + +static asio::awaitable S_B_E6(shared_ptr c, Channel::Message& msg) { + const auto& cmd = msg.check_size_t(0xFFFF); + c->proxy_session->remote_guild_card_number = cmd.guild_card_number; + c->proxy_session->remote_bb_security_token = cmd.security_token; + c->proxy_session->remote_client_config_data = cmd.client_config; + + auto s = c->require_server_state(); + auto& pc = s->proxy_persistent_configs[c->login->account->account_id]; + pc.account_id = c->login->account->account_id; + pc.remote_guild_card_number = c->proxy_session->remote_guild_card_number; + pc.remote_bb_security_token = c->proxy_session->remote_bb_security_token; + pc.remote_client_config_data = c->proxy_session->remote_client_config_data; + pc.enable_remote_ip_crc_patch = c->proxy_session->enable_remote_ip_crc_patch; + c->log.info_f("Updated persistent config for proxy session"); + + if ((c->bb_connection_phase == 0) && c->proxy_session->received_reconnect) { + c->proxy_session->server_channel->send(0x00E0); // Request system file + } + + co_return HandlerResult::SUPPRESS; +} + +static asio::awaitable C_B_E0(shared_ptr, Channel::Message&) { + co_return HandlerResult::SUPPRESS; +} + +static asio::awaitable S_V123_04(shared_ptr c, Channel::Message& msg) { + // Suppress extremely short commands from the server instead of disconnecting + if (msg.data.size() < offsetof(S_UpdateClientConfig_V3_04, client_config)) { le_uint64_t checksum = phosg::random_object() & 0x0000FFFFFFFFFFFF; - ses->server_channel.send(0x96, 0x00, &checksum, sizeof(checksum)); - return HandlerResult::Type::SUPPRESS; + c->proxy_session->server_channel->send(0x96, 0x00, &checksum, sizeof(checksum)); + co_return HandlerResult::SUPPRESS; } // Some servers send a short 04 command if they don't use all of the 0x20 // bytes available. We should be prepared to handle that. - auto& cmd = check_size_t(data, + auto& cmd = msg.check_size_t( offsetof(S_UpdateClientConfig_V3_04, client_config), sizeof(S_UpdateClientConfig_V3_04)); @@ -550,18 +414,17 @@ static HandlerResult S_V123_04(shared_ptr ses, uint1 // remote server so the client doesn't see it change. If this is a logged-out // session, then the client never received a guild card number from newserv // anyway, so we can let the client see the number from the remote server. - bool had_guild_card_number = (ses->remote_guild_card_number >= 0); - if (ses->remote_guild_card_number != cmd.guild_card_number) { - ses->remote_guild_card_number = cmd.guild_card_number; - ses->log.info("Remote guild card number set to %" PRId64, - ses->remote_guild_card_number); - string message = phosg::string_printf( - "The remote server\nhas assigned your\nGuild Card number:\n$C6%" PRId64, - ses->remote_guild_card_number); - send_ship_info(ses->client_channel, message); + bool had_guild_card_number = (c->proxy_session->remote_guild_card_number >= 0); + if (c->proxy_session->remote_guild_card_number != cmd.guild_card_number) { + c->proxy_session->remote_guild_card_number = cmd.guild_card_number; + c->log.info_f("Remote guild card number set to {}", c->proxy_session->remote_guild_card_number); + string message = std::format( + "The remote server\nhas assigned your\nGuild Card number:\n$C6{}", + c->proxy_session->remote_guild_card_number); + send_ship_info(c->channel, message); } - if (ses->login) { - cmd.guild_card_number = ses->login->account->account_id; + if (c->login) { + cmd.guild_card_number = c->login->account->account_id; } // It seems the client ignores the length of the 04 command, and always copies @@ -570,32 +433,32 @@ static HandlerResult S_V123_04(shared_ptr ses, uint1 // the copyright string from the server init command). We simulate that here. // If there was previously a guild card number, assume we got the lobby server // init text instead of the port map init text. - memcpy(ses->remote_client_config_data.data(), + memcpy(c->proxy_session->remote_client_config_data.data(), had_guild_card_number ? "t Lobby Server. Copyright SEGA E" : "t Port Map. Copyright SEGA Enter", - ses->remote_client_config_data.bytes()); - memcpy(ses->remote_client_config_data.data(), &cmd.client_config, - min(data.size() - offsetof(S_UpdateClientConfig_V3_04, client_config), - ses->remote_client_config_data.bytes())); + 0x20); + memcpy(c->proxy_session->remote_client_config_data.data(), &cmd.client_config, + min(msg.data.size() - offsetof(S_UpdateClientConfig_V3_04, client_config), + c->proxy_session->remote_client_config_data.bytes())); // If the guild card number was not set, pretend (to the server) that this is // the first 04 command the client has received. The client responds with a 96 // (checksum) in that case. if (!had_guild_card_number) { le_uint64_t checksum = phosg::random_object() & 0x0000FFFFFFFFFFFF; - ses->server_channel.send(0x96, 0x00, &checksum, sizeof(checksum)); + c->proxy_session->server_channel->send(0x96, 0x00, &checksum, sizeof(checksum)); } - return ses->login ? HandlerResult::Type::MODIFIED : HandlerResult::Type::FORWARD; + co_return c->login ? HandlerResult::MODIFIED : HandlerResult::FORWARD; } -static HandlerResult S_V123_06(shared_ptr ses, uint16_t, uint32_t, string& data) { +static asio::awaitable S_V123_06(shared_ptr c, Channel::Message& msg) { bool modified = false; - if (ses->login) { - auto& cmd = check_size_t(data, 0xFFFF); - if (cmd.guild_card_number == ses->remote_guild_card_number) { - cmd.guild_card_number = ses->login->account->account_id; + if (c->login && c->login->account->account_id != c->proxy_session->remote_guild_card_number) { + auto& cmd = msg.check_size_t(0xFFFF); + if (cmd.guild_card_number == c->proxy_session->remote_guild_card_number) { + cmd.guild_card_number = c->login->account->account_id; modified = true; } } @@ -603,107 +466,108 @@ static HandlerResult S_V123_06(shared_ptr ses, uint1 // If the session is Ep3, and Unmask Whispers is on, and there's enough data, // and the message has private_flags, and the private_flags say that you // shouldn't see the message, then change the private_flags - if (is_ep3(ses->version()) && - ses->config.check_flag(Client::Flag::PROXY_EP3_UNMASK_WHISPERS) && - (data.size() >= 12) && - (data[sizeof(SC_TextHeader_01_06_11_B0_EE)] != '\t') && - (data[sizeof(SC_TextHeader_01_06_11_B0_EE)] & (1 << ses->lobby_client_id))) { - data[sizeof(SC_TextHeader_01_06_11_B0_EE)] &= ~(1 << ses->lobby_client_id); + if (is_ep3(c->version()) && + c->check_flag(Client::Flag::PROXY_EP3_UNMASK_WHISPERS) && + (msg.data.size() >= 12) && + (msg.data[sizeof(SC_TextHeader_01_06_11_B0_EE)] != '\t') && + (msg.data[sizeof(SC_TextHeader_01_06_11_B0_EE)] & (1 << c->lobby_client_id))) { + msg.data[sizeof(SC_TextHeader_01_06_11_B0_EE)] &= ~(1 << c->lobby_client_id); modified = true; } - return modified ? HandlerResult::Type::MODIFIED : HandlerResult::Type::FORWARD; + co_return modified ? HandlerResult::MODIFIED : HandlerResult::FORWARD; } template -static HandlerResult S_41(shared_ptr ses, uint16_t, uint32_t, string& data) { - if (ses->login) { - auto& cmd = check_size_t(data); - if ((cmd.searcher_guild_card_number == ses->remote_guild_card_number) && - (cmd.result_guild_card_number == ses->remote_guild_card_number) && - ses->server_ping_start_time) { - uint64_t ping_usecs = phosg::now() - ses->server_ping_start_time; - ses->server_ping_start_time = 0; +static asio::awaitable S_41(shared_ptr c, Channel::Message& msg) { + if (c->login) { + auto& cmd = msg.check_size_t(); + if ((cmd.searcher_guild_card_number == c->proxy_session->remote_guild_card_number) && + (cmd.result_guild_card_number == c->proxy_session->remote_guild_card_number) && + c->proxy_session->server_ping_start_time) { + uint64_t ping_usecs = phosg::now() - c->proxy_session->server_ping_start_time; + c->proxy_session->server_ping_start_time = 0; double ping_ms = static_cast(ping_usecs) / 1000.0; - send_text_message_printf(ses->client_channel, "To server: %gms", ping_ms); - return HandlerResult::Type::SUPPRESS; + send_text_message_fmt(c->channel, "To server: {:g}ms", ping_ms); + co_return HandlerResult::SUPPRESS; } else { bool modified = false; - if (cmd.searcher_guild_card_number == ses->remote_guild_card_number) { - cmd.searcher_guild_card_number = ses->login->account->account_id; - modified = true; + if (c->login->account->account_id != c->proxy_session->remote_guild_card_number) { + if (cmd.searcher_guild_card_number == c->proxy_session->remote_guild_card_number) { + cmd.searcher_guild_card_number = c->login->account->account_id; + modified = true; + } + if (cmd.result_guild_card_number == c->proxy_session->remote_guild_card_number) { + cmd.result_guild_card_number = c->login->account->account_id; + modified = true; + } } - if (cmd.result_guild_card_number == ses->remote_guild_card_number) { - cmd.result_guild_card_number = ses->login->account->account_id; - modified = true; - } - return modified ? HandlerResult::Type::MODIFIED : HandlerResult::Type::FORWARD; + co_return modified ? HandlerResult::MODIFIED : HandlerResult::FORWARD; } } else { - return HandlerResult::Type::FORWARD; + co_return HandlerResult::FORWARD; } } -constexpr on_command_t S_DGX_41 = &S_41; -constexpr on_command_t S_P_41 = &S_41; -constexpr on_command_t S_B_41 = &S_41; +constexpr on_message_t S_DGX_41 = &S_41; +constexpr on_message_t S_P_41 = &S_41; +constexpr on_message_t S_B_41 = &S_41; template -static HandlerResult S_81(shared_ptr ses, uint16_t, uint32_t, string& data) { +static asio::awaitable S_81(shared_ptr c, Channel::Message& msg) { bool modified = false; - if (ses->login) { - auto& cmd = check_size_t(data); - if (cmd.from_guild_card_number == ses->remote_guild_card_number) { - cmd.from_guild_card_number = ses->login->account->account_id; + if (c->login && c->login->account->account_id != c->proxy_session->remote_guild_card_number) { + auto& cmd = msg.check_size_t(); + if (cmd.from_guild_card_number == c->proxy_session->remote_guild_card_number) { + cmd.from_guild_card_number = c->login->account->account_id; modified = true; } - if (cmd.to_guild_card_number == ses->remote_guild_card_number) { - cmd.to_guild_card_number = ses->login->account->account_id; + if (cmd.to_guild_card_number == c->proxy_session->remote_guild_card_number) { + cmd.to_guild_card_number = c->login->account->account_id; modified = true; } } - return modified ? HandlerResult::Type::MODIFIED : HandlerResult::Type::FORWARD; + co_return modified ? HandlerResult::MODIFIED : HandlerResult::FORWARD; } -constexpr on_command_t S_DGX_81 = &S_81; -constexpr on_command_t S_P_81 = &S_81; -constexpr on_command_t S_B_81 = &S_81; +constexpr on_message_t S_DGX_81 = &S_81; +constexpr on_message_t S_P_81 = &S_81; +constexpr on_message_t S_B_81 = &S_81; -static HandlerResult S_88(shared_ptr ses, uint16_t, uint32_t flag, string& data) { +static asio::awaitable S_88(shared_ptr c, Channel::Message& msg) { bool modified = false; - if (ses->login) { - size_t expected_size = sizeof(S_ArrowUpdateEntry_88) * flag; - auto* entries = &check_size_t( - data, expected_size, expected_size); - for (size_t x = 0; x < flag; x++) { - if (entries[x].guild_card_number == ses->remote_guild_card_number) { - entries[x].guild_card_number = ses->login->account->account_id; + if (c->login && c->login->account->account_id != c->proxy_session->remote_guild_card_number) { + size_t expected_size = sizeof(S_ArrowUpdateEntry_88) * msg.flag; + auto* entries = &msg.check_size_t(expected_size, expected_size); + for (size_t x = 0; x < msg.flag; x++) { + if (entries[x].guild_card_number == c->proxy_session->remote_guild_card_number) { + entries[x].guild_card_number = c->login->account->account_id; modified = true; } } } - return modified ? HandlerResult::Type::MODIFIED : HandlerResult::Type::FORWARD; + co_return modified ? HandlerResult::MODIFIED : HandlerResult::FORWARD; } -static HandlerResult S_B1(shared_ptr ses, uint16_t, uint32_t, string&) { +static asio::awaitable S_B1(shared_ptr c, Channel::Message&) { // Block all time updates from the remote server, so client's time remains // consistent - ses->server_channel.send(0x99, 0x00); - return HandlerResult::Type::SUPPRESS; + c->proxy_session->server_channel->send(0x99, 0x00); + co_return HandlerResult::SUPPRESS; } template -static HandlerResult S_B2(shared_ptr ses, uint16_t, uint32_t flag, string& data) { - const auto& cmd = check_size_t(data, 0xFFFF); +static asio::awaitable S_B2(shared_ptr c, Channel::Message& msg) { + const auto& cmd = msg.check_size_t(0xFFFF); - if (cmd.code_size && ses->config.check_flag(Client::Flag::PROXY_SAVE_FILES)) { + if (cmd.code_size && c->check_flag(Client::Flag::PROXY_SAVE_FILES)) { uint64_t filename_timestamp = phosg::now(); - string code = data.substr(sizeof(S_ExecuteCode_B2)); + string code = msg.data.substr(sizeof(S_ExecuteCode_B2)); - if (ses->config.check_flag(Client::Flag::ENCRYPTED_SEND_FUNCTION_CALL)) { + if (c->check_flag(Client::Flag::ENCRYPTED_SEND_FUNCTION_CALL)) { phosg::StringReader r(code); - bool is_big_endian = ::is_big_endian(ses->version()); + bool is_big_endian = ::is_big_endian(c->version()); uint32_t decompressed_size = is_big_endian ? r.get_u32b() : r.get_u32l(); uint32_t key = is_big_endian ? r.get_u32b() : r.get_u32l(); @@ -728,22 +592,22 @@ static HandlerResult S_B2(shared_ptr ses, uint16_t, } } else { - code = data.substr(sizeof(S_ExecuteCode_B2)); + code = msg.data.substr(sizeof(S_ExecuteCode_B2)); if (code.size() < cmd.code_size) { code.resize(cmd.code_size); } } - string output_filename = phosg::string_printf("code.%" PRId64 ".bin", filename_timestamp); - phosg::save_file(output_filename, data); - ses->log.info("Wrote code from server to file %s", output_filename.c_str()); + string output_filename = std::format("code.{}.bin", filename_timestamp); + phosg::save_file(output_filename, msg.data); + c->log.info_f("Wrote code from server to file {}", output_filename); using FooterT = RELFileFooterT; // TODO: Support SH-4 disassembly too - bool is_ppc = ::is_ppc(ses->version()); - bool is_x86 = ::is_x86(ses->version()); - bool is_sh4 = ::is_sh4(ses->version()); + bool is_ppc = ::is_ppc(c->version()); + bool is_x86 = ::is_x86(c->version()); + bool is_sh4 = ::is_sh4(c->version()); if (is_ppc || is_x86 || is_sh4) { try { if (code.size() < sizeof(FooterT)) { @@ -760,129 +624,128 @@ static HandlerResult S_B2(shared_ptr ses, uint16_t, uint32_t reloc_offset = 0; for (size_t x = 0; x < footer.num_relocations; x++) { reloc_offset += (r.get>() * 4); - labels.emplace(reloc_offset, phosg::string_printf("reloc%zu", x)); + labels.emplace(reloc_offset, std::format("reloc{}", x)); } - labels.emplace(footer.root_offset.load(), "entry_ptr"); + labels.emplace(footer.root_offset, "entry_ptr"); labels.emplace(footer_offset, "footer"); labels.emplace(r.pget>(footer.root_offset), "start"); string disassembly; if (is_ppc) { - disassembly = ResourceDASM::PPC32Emulator::disassemble( - &r.pget(0, code.size()), - code.size(), - 0, - &labels); + disassembly = ResourceDASM::PPC32Emulator::disassemble(&r.pget(0, code.size()), code.size(), 0, &labels); } else if (is_x86) { - disassembly = ResourceDASM::X86Emulator::disassemble( - &r.pget(0, code.size()), - code.size(), - 0, - &labels); + disassembly = ResourceDASM::X86Emulator::disassemble(&r.pget(0, code.size()), code.size(), 0, &labels); } else if (is_sh4) { - disassembly = ResourceDASM::SH4Emulator::disassemble( - &r.pget(0, code.size()), - code.size(), - 0, - &labels); + disassembly = ResourceDASM::SH4Emulator::disassemble(&r.pget(0, code.size()), code.size(), 0, &labels); } else { // We shouldn't have entered the outer if statement if this happens throw logic_error("unsupported architecture"); } - output_filename = phosg::string_printf("code.%" PRId64 ".txt", filename_timestamp); + output_filename = std::format("code.{}.txt", filename_timestamp); { auto f = phosg::fopen_unique(output_filename, "wt"); - fprintf(f.get(), "// code_size = 0x%" PRIX32 "\n", cmd.code_size.load()); - fprintf(f.get(), "// checksum_addr = 0x%" PRIX32 "\n", cmd.checksum_start.load()); - fprintf(f.get(), "// checksum_size = 0x%" PRIX32 "\n", cmd.checksum_size.load()); + phosg::fwrite_fmt(f.get(), "// code_size = 0x{:X}\n", cmd.code_size); + phosg::fwrite_fmt(f.get(), "// checksum_addr = 0x{:X}\n", cmd.checksum_start); + phosg::fwrite_fmt(f.get(), "// checksum_size = 0x{:X}\n", cmd.checksum_size); phosg::fwritex(f.get(), disassembly); } - ses->log.info("Wrote disassembly to file %s", output_filename.c_str()); + c->log.info_f("Wrote disassembly to file {}", output_filename); } catch (const exception& e) { - ses->log.info("Failed to disassemble code from server: %s", e.what()); + c->log.info_f("Failed to disassemble code from server: {}", e.what()); } } } - if (ses->config.check_flag(Client::Flag::PROXY_BLOCK_FUNCTION_CALLS)) { - ses->log.info("Blocking function call from server"); + if (c->check_flag(Client::Flag::PROXY_BLOCK_FUNCTION_CALLS)) { + c->log.info_f("Blocking function call from server"); C_ExecuteCodeResult_B3 cmd; cmd.return_value = 0xFFFFFFFF; cmd.checksum = 0x00000000; - ses->server_channel.send(0xB3, flag, &cmd, sizeof(cmd)); - return HandlerResult::Type::SUPPRESS; + c->proxy_session->server_channel->send(0xB3, msg.flag, &cmd, sizeof(cmd)); + co_return HandlerResult::SUPPRESS; } else { - ses->function_call_return_handler_queue.emplace_back(nullptr); - return HandlerResult::Type::FORWARD; + c->function_call_response_queue.emplace_back(nullptr); + co_return HandlerResult::FORWARD; } } -static HandlerResult C_B3(shared_ptr ses, uint16_t, uint32_t, string& data) { - auto cmd = check_size_t(data); - if (ses->function_call_return_handler_queue.empty()) { - ses->log.warning("Received function call result with empty result queue"); - return HandlerResult::Type::FORWARD; +static asio::awaitable C_B3(shared_ptr c, Channel::Message& msg) { + auto cmd = msg.check_size_t(); + + shared_ptr> promise; + if (!c->function_call_response_queue.empty()) { + promise = std::move(c->function_call_response_queue.front()); + c->function_call_response_queue.pop_front(); } - auto handler = std::move(ses->function_call_return_handler_queue.front()); - ses->function_call_return_handler_queue.pop_front(); - if (handler != nullptr) { - handler(cmd.return_value, cmd.checksum); - return HandlerResult::Type::SUPPRESS; + if (promise) { + promise->set_value(std::move(cmd)); + co_return HandlerResult::SUPPRESS; } else { - return HandlerResult::Type::FORWARD; + co_return HandlerResult::FORWARD; } } -static HandlerResult S_B_E7(shared_ptr ses, uint16_t command, uint32_t flag, string& data) { - if (ses->config.check_flag(Client::Flag::PROXY_SAVE_FILES)) { - string output_filename = phosg::string_printf("player.%" PRId64 ".psochar", phosg::now()); +static asio::awaitable S_B_E2(shared_ptr c, Channel::Message& msg) { + if (c->check_flag(Client::Flag::PROXY_SAVE_FILES)) { + string output_filename = std::format("system.{}.psosys", phosg::now()); + phosg::save_object_file(output_filename, msg.check_size_t()); + c->log.info_f("Wrote system file to {}", output_filename); + } + co_return HandlerResult::FORWARD; +} + +static asio::awaitable S_B_E7(shared_ptr c, Channel::Message& msg) { + if (c->check_flag(Client::Flag::PROXY_SAVE_FILES)) { + string output_filename = std::format("player.{}.psochar", phosg::now()); auto f = phosg::fopen_unique(output_filename, "wb"); - PSOCommandHeaderBB header = {data.size() + sizeof(PSOCommandHeaderBB), command, flag}; + PSOCommandHeaderBB header = {msg.data.size() + sizeof(PSOCommandHeaderBB), msg.command, msg.flag}; phosg::fwritex(f.get(), &header, sizeof(header)); - phosg::fwritex(f.get(), data); - ses->log.info("Wrote player data to file %s", output_filename.c_str()); + phosg::fwritex(f.get(), msg.data); + c->log.info_f("Wrote player data to {}", output_filename); } - return HandlerResult::Type::FORWARD; + co_return HandlerResult::FORWARD; } template -static HandlerResult S_C4(shared_ptr ses, uint16_t, uint32_t flag, string& data) { +static asio::awaitable S_C4(shared_ptr c, Channel::Message& msg) { bool modified = false; - if (ses->login) { - size_t expected_size = sizeof(CmdT) * flag; + if (c->login && c->login->account->account_id != c->proxy_session->remote_guild_card_number) { + size_t expected_size = sizeof(CmdT) * msg.flag; // Some servers (e.g. Schtserv) send extra data on the end of this command; // the client ignores it so we can ignore it too - auto* entries = &check_size_t(data, expected_size, 0xFFFF); - for (size_t x = 0; x < flag; x++) { - if (entries[x].guild_card_number == ses->remote_guild_card_number) { - entries[x].guild_card_number = ses->login->account->account_id; + auto* entries = &msg.check_size_t(expected_size, 0xFFFF); + for (size_t x = 0; x < msg.flag; x++) { + if (entries[x].guild_card_number == c->proxy_session->remote_guild_card_number) { + entries[x].guild_card_number = c->login->account->account_id; modified = true; } } } - return modified ? HandlerResult::Type::MODIFIED : HandlerResult::Type::FORWARD; + co_return modified ? HandlerResult::MODIFIED : HandlerResult::FORWARD; } -constexpr on_command_t S_DGX_C4 = &S_C4; -constexpr on_command_t S_P_C4 = &S_C4; -constexpr on_command_t S_B_C4 = &S_C4; +constexpr on_message_t S_DGX_C4 = &S_C4; +constexpr on_message_t S_P_C4 = &S_C4; +constexpr on_message_t S_B_C4 = &S_C4; -static HandlerResult S_G_E4(shared_ptr ses, uint16_t, uint32_t, string& data) { - auto& cmd = check_size_t(data); +static asio::awaitable S_G_E4(shared_ptr c, Channel::Message& msg) { + auto& cmd = msg.check_size_t(); bool modified = false; - for (size_t x = 0; x < 4; x++) { - if (cmd.entries[x].guild_card_number == ses->remote_guild_card_number) { - cmd.entries[x].guild_card_number = ses->login->account->account_id; - modified = true; + if (c->login && c->login->account->account_id != c->proxy_session->remote_guild_card_number) { + for (size_t x = 0; x < 4; x++) { + if (cmd.entries[x].guild_card_number == c->proxy_session->remote_guild_card_number) { + cmd.entries[x].guild_card_number = c->login->account->account_id; + modified = true; + } } } - return modified ? HandlerResult::Type::MODIFIED : HandlerResult::Type::FORWARD; + co_return modified ? HandlerResult::MODIFIED : HandlerResult::FORWARD; } -static HandlerResult S_B_22(shared_ptr ses, uint16_t, uint32_t, string& data) { +static asio::awaitable S_B_22(shared_ptr c, Channel::Message& msg) { // We use this command (which is sent before the init encryption command) to // detect a particular server behavior that we'll have to work around later. // It looks like this command's existence is another anti-proxy measure, since @@ -892,15 +755,14 @@ static HandlerResult S_B_22(shared_ptr ses, uint16_t // Editor's note: There's an unsavory message in this command's data field, // hence the hash here instead of a direct string comparison. I'd love to hear // the story behind why they put that string there. - if ((data.size() == 0x2C) && - (phosg::fnv1a64(data.data(), data.size()) == 0x8AF8314316A27994)) { - ses->log.info("Enabling remote IP CRC patch"); - ses->enable_remote_ip_crc_patch = true; + if ((msg.data.size() == 0x2C) && (phosg::fnv1a64(msg.data.data(), msg.data.size()) == 0x8AF8314316A27994)) { + c->log.info_f("Enabling remote IP CRC patch"); + c->proxy_session->enable_remote_ip_crc_patch = true; } - return HandlerResult::Type::FORWARD; + co_return HandlerResult::FORWARD; } -static HandlerResult S_19_P_14(shared_ptr ses, uint16_t, uint32_t, string& data) { +static asio::awaitable S_19_U_14(shared_ptr c, Channel::Message& msg) { // If the command is shorter than 6 bytes, use the previous server command to // fill it in. This simulates a behavior used by some private servers where a // longer previous command is used to fill part of the client's receive buffer @@ -908,351 +770,342 @@ static HandlerResult S_19_P_14(shared_ptr ses, uint1 // which results in the client using the previous command's data as part of // the 19 command's contents. They presumably do this in an attempt to prevent // people from using proxies. - if (data.size() < sizeof(ses->prev_server_command_bytes)) { - data.append( - reinterpret_cast(&ses->prev_server_command_bytes[data.size()]), - sizeof(ses->prev_server_command_bytes) - data.size()); + if (msg.data.size() < sizeof(c->proxy_session->prev_server_command_bytes)) { + msg.data.append( + reinterpret_cast(&c->proxy_session->prev_server_command_bytes[msg.data.size()]), + sizeof(c->proxy_session->prev_server_command_bytes) - msg.data.size()); } - if (data.size() < sizeof(S_Reconnect_19)) { - data.resize(sizeof(S_Reconnect_19), '\0'); + if (msg.data.size() < sizeof(S_Reconnect_19)) { + msg.data.resize(sizeof(S_Reconnect_19), '\0'); } - if (ses->enable_remote_ip_crc_patch) { - ses->remote_ip_crc = phosg::crc32(data.data(), 4); + c->proxy_session->received_reconnect = true; + if (c->proxy_session->enable_remote_ip_crc_patch) { + c->proxy_session->remote_ip_crc = phosg::crc32(msg.data.data(), 4); } - // Set the destination netloc appropriately - memset(&ses->next_destination, 0, sizeof(ses->next_destination)); - struct sockaddr_in* sin = reinterpret_cast( - &ses->next_destination); - sin->sin_family = AF_INET; - if (is_patch(ses->version())) { - auto& cmd = check_size_t(data); - sin->sin_addr.s_addr = cmd.address.load_raw(); // Already big-endian - sin->sin_port = htons(cmd.port); + // Get the new endpoint + asio::ip::tcp::endpoint new_ep; + if (is_patch(c->version())) { + auto& cmd = msg.check_size_t(); + new_ep = make_endpoint_ipv4(cmd.address, cmd.port); } else { // This weird maximum size is here to properly handle the version-split // command that some servers (including newserv) use on port 9100 - auto& cmd = check_size_t(data, 0xFFFF); - sin->sin_addr.s_addr = cmd.address.load_raw(); // Already big-endian - sin->sin_port = htons(cmd.port); + auto& cmd = msg.check_size_t(0xFFFF); + new_ep = make_endpoint_ipv4(cmd.address, cmd.port); } - if (!ses->client_channel.connected()) { - ses->log.warning("Received reconnect command with no destination present"); - return HandlerResult::Type::SUPPRESS; + // Replace the server channel with a new channel to the new endpoint + string netloc_str = str_for_endpoint(new_ep); + c->log.info_f("Connecting to {}", netloc_str); + auto sock = make_unique(co_await async_connect_tcp(new_ep)); - } else if (!is_v4(ses->version())) { - // Hide redirects from the client completely. The new destination server - // will presumably send a new encryption init command, which the handlers - // will appropriately respond to. - ses->server_channel.crypt_in.reset(); - ses->server_channel.crypt_out.reset(); + // Close the old channel only after replacing it with the new one + auto s = c->require_server_state(); + auto old_channel = c->proxy_session->server_channel; + auto new_channel = SocketChannel::create( + s->io_context, + std::move(sock), + old_channel->version, + old_channel->language, + std::format("C-{} proxy remote server at {}", c->id, netloc_str), + old_channel->terminal_send_color, + old_channel->terminal_recv_color); + c->proxy_session->server_channel = new_channel; + asio::co_spawn(*s->io_context, handle_proxy_server_commands(c, c->proxy_session, new_channel), asio::detached); + c->log.info_f("Server channel connected"); + old_channel->disconnect(); - // We already modified next_destination, so start the connection process - ses->connect(); - return HandlerResult::Type::SUPPRESS; - - } else { - const struct sockaddr_in* sin = reinterpret_cast( - &ses->client_channel.local_addr); - if (sin->sin_family != AF_INET) { - throw logic_error("existing connection is not ipv4"); - } - auto& cmd = check_size_t(data, 0xFFFF); - cmd.address.store_raw(sin->sin_addr.s_addr); - cmd.port = ntohs(sin->sin_port); - return HandlerResult::Type::MODIFIED; - } + // Hide redirects from the client completely + co_return HandlerResult::SUPPRESS; } -static HandlerResult S_V3_1A_D5(shared_ptr ses, uint16_t, uint32_t, string&) { +static asio::awaitable S_V3_1A_D5(shared_ptr c, Channel::Message&) { // If the client is a version that sends close confirmations and the client // has the no-close-confirmation flag set in its newserv client config, send a // fake confirmation to the remote server immediately. - if (is_v3(ses->version()) && ses->config.check_flag(Client::Flag::NO_D6)) { - ses->server_channel.send(0xD6); + if (is_v3(c->version()) && c->check_flag(Client::Flag::NO_D6)) { + c->proxy_session->server_channel->send(0xD6); } - return HandlerResult::Type::FORWARD; + co_return HandlerResult::FORWARD; } -static HandlerResult S_V3_BB_DA(shared_ptr ses, uint16_t, uint32_t flag, string&) { +static asio::awaitable S_V3_BB_DA(shared_ptr c, Channel::Message& msg) { // This command is supported on all V3 and V4 versions except Ep1&2 Trial - if (ses->version() == Version::GC_NTE) { - return HandlerResult::Type::SUPPRESS; - } else if ((ses->config.override_lobby_event != 0xFF) && (flag != ses->config.override_lobby_event)) { - return HandlerResult(HandlerResult::Type::MODIFIED, 0xDA, ses->config.override_lobby_event); + if (c->version() == Version::GC_NTE) { + co_return HandlerResult::SUPPRESS; + } else if ((c->override_lobby_event != 0xFF) && (msg.flag != c->override_lobby_event)) { + msg.flag = c->override_lobby_event; + co_return HandlerResult::MODIFIED; } else { - return HandlerResult::Type::FORWARD; + co_return HandlerResult::FORWARD; } } -static HandlerResult SC_6x60_6xA2(shared_ptr ses, const string& data) { - if (!ses->is_in_game) { - return HandlerResult::Type::FORWARD; +static asio::awaitable SC_6x60_6xA2(shared_ptr c, Channel::Message& msg) { + if (!c->proxy_session->is_in_game) { + co_return HandlerResult::FORWARD; } - if (ses->next_drop_item.data1d[0]) { - G_SpecializableItemDropRequest_6xA2 cmd = normalize_drop_request(data.data(), data.size()); - auto s = ses->require_server_state(); - ses->next_drop_item.id = ses->next_item_id++; - uint8_t source_type = (cmd.rt_index == 0x30) ? 2 : 1; - send_drop_item_to_channel(s, ses->server_channel, ses->next_drop_item, source_type, cmd.floor, cmd.pos, cmd.entity_index); - send_drop_item_to_channel(s, ses->client_channel, ses->next_drop_item, source_type, cmd.floor, cmd.pos, cmd.entity_index); - ses->next_drop_item.clear(); - return HandlerResult::Type::SUPPRESS; - } - - using DropMode = ProxyServer::LinkedSession::DropMode; - switch (ses->drop_mode) { + using DropMode = ProxySession::DropMode; + switch (c->proxy_session->drop_mode) { case DropMode::DISABLED: - return HandlerResult::Type::SUPPRESS; + co_return HandlerResult::SUPPRESS; case DropMode::PASSTHROUGH: - return HandlerResult::Type::FORWARD; + co_return HandlerResult::FORWARD; case DropMode::INTERCEPT: break; default: throw logic_error("invalid drop mode"); } - if (!ses->item_creator) { - ses->log.warning("Session is in INTERCEPT drop mode, but item creator is missing"); - return HandlerResult::Type::FORWARD; + if (!c->proxy_session->item_creator) { + c->log.warning_f("Session is in INTERCEPT drop mode, but item creator is missing"); + co_return HandlerResult::FORWARD; } - if (!ses->map_state) { - ses->log.warning("Session is in INTERCEPT drop mode, but map state is missing"); - return HandlerResult::Type::FORWARD; + if (!c->proxy_session->map_state) { + c->log.warning_f("Session is in INTERCEPT drop mode, but map state is missing"); + co_return HandlerResult::FORWARD; } - G_SpecializableItemDropRequest_6xA2 cmd = normalize_drop_request(data.data(), data.size()); + G_SpecializableItemDropRequest_6xA2 cmd = normalize_drop_request(msg.data.data(), msg.data.size()); auto rec = reconcile_drop_request_with_map( - ses->log, - ses->client_channel, - cmd, - ses->lobby_episode, - ses->lobby_event, - ses->config, - ses->map_state, - false); + c, cmd, c->proxy_session->lobby_episode, c->proxy_session->lobby_event, c->proxy_session->map_state, false); ItemCreator::DropResult res; if (rec.obj_st) { if (rec.ignore_def) { - ses->log.info("Creating item from box %04hX (area %02hX)", cmd.entity_index.load(), cmd.effective_area); - res = ses->item_creator->on_box_item_drop(cmd.effective_area); + c->log.info_f("Creating item from box {:04X} (area {:02X})", cmd.entity_index, cmd.effective_area); + res = c->proxy_session->item_creator->on_box_item_drop(cmd.effective_area); } else { - ses->log.info("Creating item from box %04hX (area %02hX; specialized with %g %08" PRIX32 " %08" PRIX32 " %08" PRIX32 ")", - cmd.entity_index.load(), cmd.effective_area, cmd.param3.load(), cmd.param4.load(), cmd.param5.load(), cmd.param6.load()); - res = ses->item_creator->on_specialized_box_item_drop(cmd.effective_area, cmd.param3, cmd.param4, cmd.param5, cmd.param6); + c->log.info_f("Creating item from box {:04X} (area {:02X}; specialized with {:g} {:08X} {:08X} {:08X})", + cmd.entity_index, cmd.effective_area, + cmd.param3, cmd.param4, cmd.param5, cmd.param6); + res = c->proxy_session->item_creator->on_specialized_box_item_drop( + cmd.effective_area, cmd.param3, cmd.param4, cmd.param5, cmd.param6); } } else { - ses->log.info("Creating item from enemy %04hX (area %02hX)", cmd.entity_index.load(), cmd.effective_area); - res = ses->item_creator->on_monster_item_drop(rec.effective_rt_index, cmd.effective_area); + c->log.info_f("Creating item from enemy {:04X} (area {:02X})", cmd.entity_index, cmd.effective_area); + res = c->proxy_session->item_creator->on_monster_item_drop(rec.effective_rt_index, cmd.effective_area); } if (res.item.empty()) { - ses->log.info("No item was created"); + c->log.info_f("No item was created"); } else { - auto s = ses->require_server_state(); - string name = s->describe_item(ses->version(), res.item, false); - ses->log.info("Entity %04hX (area %02hX) created item %s", cmd.entity_index.load(), cmd.effective_area, name.c_str()); - res.item.id = ses->next_item_id++; - ses->log.info("Creating item %08" PRIX32 " at %02hhX:%g,%g for all clients", - res.item.id.load(), cmd.floor, cmd.pos.x.load(), cmd.pos.z.load()); - send_drop_item_to_channel(s, ses->client_channel, res.item, rec.obj_st ? 2 : 1, cmd.floor, cmd.pos, cmd.entity_index); - send_drop_item_to_channel(s, ses->server_channel, res.item, rec.obj_st ? 2 : 1, cmd.floor, cmd.pos, cmd.entity_index); - send_item_notification_if_needed(s, ses->client_channel, ses->config, res.item, res.is_from_rare_table); + auto s = c->require_server_state(); + string name = s->describe_item(c->version(), res.item, false); + c->log.info_f("Entity {:04X} (area {:02X}) created item {}", cmd.entity_index, cmd.effective_area, name); + res.item.id = c->proxy_session->next_item_id++; + c->log.info_f("Creating item {:08X} at {:02X}:{:g},{:g} for all clients", + res.item.id, cmd.floor, cmd.pos.x, cmd.pos.z); + send_drop_item_to_channel(s, c->channel, res.item, rec.obj_st ? 2 : 1, cmd.floor, cmd.pos, cmd.entity_index); + send_drop_item_to_channel(s, c->proxy_session->server_channel, res.item, rec.obj_st ? 2 : 1, cmd.floor, cmd.pos, cmd.entity_index); + send_item_notification_if_needed(c, res.item, res.is_from_rare_table); } - return HandlerResult::Type::SUPPRESS; + co_return HandlerResult::SUPPRESS; } -static HandlerResult S_6x(shared_ptr ses, uint16_t, uint32_t, string& data) { - auto s = ses->require_server_state(); +static asio::awaitable S_6x(shared_ptr c, Channel::Message& msg) { + auto s = c->require_server_state(); - if (ses->config.check_flag(Client::Flag::PROXY_SAVE_FILES)) { - if (is_ep3(ses->version()) && (data.size() >= 0x14)) { - if (static_cast(data[0]) == 0xB6) { - const auto& header = check_size_t(data, 0xFFFF); - if (header.subsubcommand == 0x00000041) { - const auto& cmd = check_size_t(data, 0xFFFF); - string filename = phosg::string_printf("map%08" PRIX32 ".%" PRIu64 ".mnmd", - cmd.map_number.load(), phosg::now()); - string map_data = prs_decompress( - data.data() + sizeof(cmd), data.size() - sizeof(cmd)); - phosg::save_file(filename, map_data); - if (map_data.size() != sizeof(Episode3::MapDefinition) && map_data.size() != sizeof(Episode3::MapDefinitionTrial)) { - ses->log.warning("Wrote %zu bytes to %s (expected %zu or %zu bytes; the file may be invalid)", - map_data.size(), filename.c_str(), sizeof(Episode3::MapDefinitionTrial), sizeof(Episode3::MapDefinition)); - } else { - ses->log.info("Wrote %zu bytes to %s", map_data.size(), filename.c_str()); - } - } - } - } + if (msg.data.size() < 4) { + co_return HandlerResult::SUPPRESS; } bool modified = false; - if (!data.empty()) { - // Unmask any masked Episode 3 commands from the server - if (is_ep3(ses->version()) && (data.size() > 8) && - ((static_cast(data[0]) == 0xB3) || - (static_cast(data[0]) == 0xB4) || - (static_cast(data[0]) == 0xB5))) { - const auto& header = check_size_t(data, 0xFFFF); - if (header.mask_key && (ses->version() != Version::GC_EP3_NTE)) { - set_mask_for_ep3_game_command(data.data(), data.size(), 0); - modified = true; - } + uint8_t subcommand = translate_subcommand_number(Version::BB_V4, c->version(), msg.data[0]); + switch (subcommand) { + case 0x00: + c->log.warning_f("Blocking invalid subcommand from server"); + co_return HandlerResult::SUPPRESS; - if (ses->config.check_flag(Client::Flag::PROXY_EP3_INFINITE_TIME_ENABLED) && (header.subcommand == 0xB4)) { - if (header.subsubcommand == 0x3D) { - if (ses->version() == Version::GC_EP3_NTE) { - auto& cmd = check_size_t(data); - if (cmd.rules.overall_time_limit || cmd.rules.phase_time_limit) { - cmd.rules.overall_time_limit = 0; - cmd.rules.phase_time_limit = 0; - modified = true; - } - } else { - auto& cmd = check_size_t(data); - if (cmd.rules.overall_time_limit || cmd.rules.phase_time_limit) { - cmd.rules.overall_time_limit = 0; - cmd.rules.phase_time_limit = 0; - modified = true; - } - } - } else if (header.subsubcommand == 0x05) { - if (ses->version() == Version::GC_EP3_NTE) { - auto& cmd = check_size_t(data); - if (cmd.state.rules.overall_time_limit || cmd.state.rules.phase_time_limit) { - cmd.state.rules.overall_time_limit = 0; - cmd.state.rules.phase_time_limit = 0; - modified = true; - } - } else { - auto& cmd = check_size_t(data); - if (cmd.state.rules.overall_time_limit || cmd.state.rules.phase_time_limit) { - cmd.state.rules.overall_time_limit = 0; - cmd.state.rules.phase_time_limit = 0; - modified = true; - } - } - } + case 0x46: { + const auto& cmd = msg.check_size_t( + offsetof(G_AttackFinished_6x46, targets), sizeof(G_AttackFinished_6x46)); + if (cmd.target_count > min(cmd.header.size - 2, cmd.targets.size())) { + c->log.warning_f("Blocking subcommand 6x46 with invalid count"); + co_return HandlerResult::SUPPRESS; } + break; } - if (data[0] == 0x46) { - const auto& cmd = check_size_t(data, - offsetof(G_AttackFinished_6x46, targets), - sizeof(G_AttackFinished_6x46)); + case 0x47: { + const auto& cmd = msg.check_size_t( + offsetof(G_CastTechnique_6x47, targets), sizeof(G_CastTechnique_6x47)); if (cmd.target_count > min(cmd.header.size - 2, cmd.targets.size())) { - ses->log.warning("Blocking subcommand 6x46 with invalid count"); - return HandlerResult::Type::SUPPRESS; + c->log.warning_f("Blocking subcommand 6x47 with invalid count"); + co_return HandlerResult::SUPPRESS; } - } else if (data[0] == 0x47) { - const auto& cmd = check_size_t(data, - offsetof(G_CastTechnique_6x47, targets), - sizeof(G_CastTechnique_6x47)); - if (cmd.target_count > min(cmd.header.size - 2, cmd.targets.size())) { - ses->log.warning("Blocking subcommand 6x47 with invalid count"); - return HandlerResult::Type::SUPPRESS; - } - } else if (data[0] == 0x49) { - const auto& cmd = check_size_t(data, - offsetof(G_ExecutePhotonBlast_6x49, targets), - sizeof(G_ExecutePhotonBlast_6x49)); + break; + } + + case 0x49: { + const auto& cmd = msg.check_size_t( + offsetof(G_ExecutePhotonBlast_6x49, targets), sizeof(G_ExecutePhotonBlast_6x49)); if (cmd.target_count > min(cmd.header.size - 3, cmd.targets.size())) { - ses->log.warning("Blocking subcommand 6x49 with invalid count"); - return HandlerResult::Type::SUPPRESS; + c->log.warning_f("Blocking subcommand 6x49 with invalid count"); + co_return HandlerResult::SUPPRESS; } + break; + } - } else if (data[0] == 0x5F) { - const auto& cmd = check_size_t(data, sizeof(G_DropItem_PC_V3_BB_6x5F)); + case 0x5F: { + const auto& cmd = msg.check_size_t(sizeof(G_DropItem_PC_V3_BB_6x5F)); ItemData item = cmd.item.item; - item.decode_for_version(ses->version()); - send_item_notification_if_needed(ses->require_server_state(), ses->client_channel, ses->config, item, true); + item.decode_for_version(c->version()); + send_item_notification_if_needed(c, item, true); + break; + } - } else if ((data[0] == 0x60) || (static_cast(data[0]) == 0xA2)) { - return SC_6x60_6xA2(ses, data); + case 0x60: + case 0xA2: + co_return co_await SC_6x60_6xA2(c, msg); - } else if ((static_cast(data[0]) == 0xB5) && is_ep3(ses->version()) && (data.size() >= 8)) { - set_mask_for_ep3_game_command(data.data(), data.size(), 0); - if (data[4] == 0x1A) { - return HandlerResult::Type::SUPPRESS; - } else if (data[4] == 0x20) { - auto& cmd = check_size_t(data); - if (cmd.client_id >= 12) { - return HandlerResult::Type::SUPPRESS; - } - } else if (data[4] == 0x31) { - auto& cmd = check_size_t(data); - if (cmd.menu_type >= 0x15) { - return HandlerResult::Type::SUPPRESS; - } - } else if (data[4] == 0x32) { - auto& cmd = check_size_t(data); - if (cmd.menu_type >= 0x15) { - return HandlerResult::Type::SUPPRESS; - } - } else if (data[4] == 0x36) { - auto& cmd = check_size_t(data); - if (ses->is_in_game && (cmd.client_id >= 4)) { - return HandlerResult::Type::SUPPRESS; - } + case 0xB3: + case 0xB4: + case 0xB5: { + if (!is_ep3(c->version()) || (msg.data.size() < 8)) { + break; } - - } else if ((static_cast(data[0]) == 0xBB) && is_ep3(ses->version())) { - if (!validate_6xBB(check_size_t(data))) { - return HandlerResult::Type::SUPPRESS; - } - return HandlerResult::Type::MODIFIED; - - } else if ((static_cast(data[0]) == 0xBC) && !ses->config.check_flag(Client::Flag::EP3_ALLOW_6xBC)) { - return HandlerResult::Type::SUPPRESS; - - } else if ((static_cast(data[0]) == 0xBD) && - ses->config.check_flag(Client::Flag::PROXY_EP3_UNMASK_WHISPERS) && - is_ep3(ses->version())) { - auto& cmd = check_size_t(data); - if (cmd.private_flags & (1 << ses->lobby_client_id)) { - cmd.private_flags &= ~(1 << ses->lobby_client_id); + // Unmask any masked Episode 3 commands from the server + const auto& header = msg.check_size_t(0xFFFF); + if (header.mask_key && (c->version() != Version::GC_EP3_NTE)) { + set_mask_for_ep3_game_command(msg.data.data(), msg.data.size(), 0); modified = true; } - } - } - return modified ? HandlerResult::Type::MODIFIED : HandlerResult::Type::FORWARD; + if ((subcommand == 0xB4) && c->check_flag(Client::Flag::PROXY_EP3_INFINITE_TIME_ENABLED)) { + if (header.subsubcommand == 0x05) { + if (c->version() == Version::GC_EP3_NTE) { + auto& cmd = msg.check_size_t(); + if (cmd.state.rules.overall_time_limit || cmd.state.rules.phase_time_limit) { + cmd.state.rules.overall_time_limit = 0; + cmd.state.rules.phase_time_limit = 0; + modified = true; + } + } else { + auto& cmd = msg.check_size_t(); + if (cmd.state.rules.overall_time_limit || cmd.state.rules.phase_time_limit) { + cmd.state.rules.overall_time_limit = 0; + cmd.state.rules.phase_time_limit = 0; + modified = true; + } + } + } else if (header.subsubcommand == 0x3D) { + if (c->version() == Version::GC_EP3_NTE) { + auto& cmd = msg.check_size_t(); + if (cmd.rules.overall_time_limit || cmd.rules.phase_time_limit) { + cmd.rules.overall_time_limit = 0; + cmd.rules.phase_time_limit = 0; + modified = true; + } + } else { + auto& cmd = msg.check_size_t(); + if (cmd.rules.overall_time_limit || cmd.rules.phase_time_limit) { + cmd.rules.overall_time_limit = 0; + cmd.rules.phase_time_limit = 0; + modified = true; + } + } + } + + } else if (subcommand == 0xB5) { + set_mask_for_ep3_game_command(msg.data.data(), msg.data.size(), 0); + if (msg.data[4] == 0x1A) { + co_return HandlerResult::SUPPRESS; + } else if (msg.data[4] == 0x20) { + auto& cmd = msg.check_size_t(); + if (cmd.client_id >= 12) { + c->log.warning_f("Blocking 6xB5x20 from server with invalid client ID"); + co_return HandlerResult::SUPPRESS; + } + } else if (msg.data[4] == 0x31) { + auto& cmd = msg.check_size_t(); + if (cmd.menu_type >= 0x15) { + c->log.warning_f("Blocking 6xB5x31 from server with invalid menu type"); + co_return HandlerResult::SUPPRESS; + } + } else if (msg.data[4] == 0x32) { + auto& cmd = msg.check_size_t(); + if (cmd.menu_type >= 0x15) { + c->log.warning_f("Blocking 6xB5x32 from server with invalid menu type"); + co_return HandlerResult::SUPPRESS; + } + } else if (msg.data[4] == 0x36) { + auto& cmd = msg.check_size_t(); + if (c->proxy_session->is_in_game && (cmd.client_id >= 4)) { + c->log.warning_f("Blocking 6xB5x36 from server with invalid client ID"); + co_return HandlerResult::SUPPRESS; + } + } + } + break; + } + + case 0xB6: + if (is_ep3(c->version()) && (msg.data.size() >= 0x14) && c->check_flag(Client::Flag::PROXY_SAVE_FILES)) { + const auto& header = msg.check_size_t(0xFFFF); + if (header.subsubcommand == 0x00000041) { + const auto& cmd = msg.check_size_t(0xFFFF); + string filename = std::format("map{:08X}.{}.mnmd", cmd.map_number, phosg::now()); + string map_data = prs_decompress(msg.data.data() + sizeof(cmd), msg.data.size() - sizeof(cmd)); + phosg::save_file(filename, map_data); + if ((map_data.size() != sizeof(Episode3::MapDefinition)) && + (map_data.size() != sizeof(Episode3::MapDefinitionTrial))) { + c->log.warning_f("Wrote {} bytes to {} (expected {} or {} bytes; the file may be invalid)", + map_data.size(), filename, sizeof(Episode3::MapDefinitionTrial), sizeof(Episode3::MapDefinition)); + } else { + c->log.info_f("Wrote {} bytes to {}", map_data.size(), filename); + } + } + } + break; + + case 0xBB: + if (is_ep3(c->version()) && !validate_6xBB(msg.check_size_t())) { + co_return HandlerResult::SUPPRESS; + } + break; + + case 0xBC: + if (!c->check_flag(Client::Flag::EP3_ALLOW_6xBC)) { + co_return HandlerResult::SUPPRESS; + } + break; + + case 0xBD: + if (is_ep3(c->version()) && c->check_flag(Client::Flag::PROXY_EP3_UNMASK_WHISPERS)) { + auto& cmd = msg.check_size_t(); + if (cmd.private_flags & (1 << c->lobby_client_id)) { + cmd.private_flags &= ~(1 << c->lobby_client_id); + modified = true; + } + } + break; + + default: + break; + } + co_return modified ? HandlerResult::MODIFIED : HandlerResult::FORWARD; } -static HandlerResult C_GXB_61(shared_ptr ses, uint16_t, uint32_t flag, string& data) { +static asio::awaitable C_GXB_61(shared_ptr c, Channel::Message& msg) { bool modified = false; // TODO: We should check if the info board text was actually modified and // return MODIFIED if so. - if (is_v4(ses->version())) { - auto& pd = check_size_t(data, 0xFFFF); - pd.info_board.encode(add_color(pd.info_board.decode(ses->language())), ses->language()); - if (ses->config.check_flag(Client::Flag::PROXY_BLANK_NAME_ENABLED)) { - pd.disp.name.encode(" ", ses->language()); - modified = true; - } - if (ses->config.check_flag(Client::Flag::PROXY_RED_NAME_ENABLED) && pd.disp.visual.name_color != 0xFFFF0000) { - pd.disp.visual.name_color = 0xFFFF0000; - pd.records.challenge.title_color = 0x7C00; - modified = true; - } else if (ses->config.check_flag(Client::Flag::PROXY_BLANK_NAME_ENABLED) && pd.disp.visual.name_color != 0x00000000) { - pd.disp.visual.name_color = 0x00000000; - modified = true; - } - if (!ses->challenge_rank_title_override.empty()) { - pd.records.challenge.title_color = encode_rgba8888_to_argb1555(ses->challenge_rank_color_override); - pd.records.challenge.rank_title.encode(ses->challenge_rank_title_override, ses->language()); - } + if (is_v4(c->version())) { + auto& pd = msg.check_size_t(0xFFFF); + pd.info_board.encode(add_color(pd.info_board.decode(c->language())), c->language()); } else { C_CharacterData_V3_61_98* pd; - if (flag == 4) { // Episode 3 - auto& ep3_pd = check_size_t(data); + if (msg.flag == 4) { // Episode 3 + auto& ep3_pd = msg.check_size_t(); + // Technically we could decrypt the Ep3 config struct within the player + // data, but this may confuse some non-newserv upstream servers if they + // implement this structure incorrectly. The decryption would go like: // if (ep3_pd.ep3_config.is_encrypted) { // decrypt_trivial_gci_data( // &ep3_pd.ep3_config.card_counts, @@ -1264,79 +1117,73 @@ static HandlerResult C_GXB_61(shared_ptr ses, uint16 // } pd = reinterpret_cast(&ep3_pd); } else { - if (is_ep3(ses->version()) && (ses->version() != Version::GC_EP3_NTE)) { - ses->log.info("Version changed to GC_EP3_NTE"); - ses->set_version(Version::GC_EP3_NTE); - ses->config.specific_version = SPECIFIC_VERSION_GC_EP3_NTE; + if (is_ep3(c->version()) && (c->version() != Version::GC_EP3_NTE)) { + c->log.info_f("Version changed to GC_EP3_NTE"); + c->channel->version = Version::GC_EP3_NTE; + c->proxy_session->server_channel->version = Version::GC_EP3_NTE; + c->specific_version = SPECIFIC_VERSION_GC_EP3_NTE; } - pd = &check_size_t(data, 0xFFFF); - } - pd->info_board.encode(add_color(pd->info_board.decode(ses->language())), ses->language()); - if (ses->config.check_flag(Client::Flag::PROXY_BLANK_NAME_ENABLED)) { - pd->disp.visual.name.encode(" ", ses->language()); - modified = true; - } - if (ses->config.check_flag(Client::Flag::PROXY_RED_NAME_ENABLED) && pd->disp.visual.name_color != 0xFFFF0000) { - pd->disp.visual.name_color = 0xFFFF0000; - pd->records.challenge.stats.title_color = 0x7C00; - modified = true; - } else if (ses->config.check_flag(Client::Flag::PROXY_BLANK_NAME_ENABLED) && pd->disp.visual.name_color != 0x00000000) { - pd->disp.visual.name_color = 0x00000000; - modified = true; - } - if (!ses->challenge_rank_title_override.empty()) { - pd->records.challenge.stats.title_color = encode_rgba8888_to_argb1555(ses->challenge_rank_color_override); - pd->records.challenge.rank_title.encode(ses->challenge_rank_title_override, ses->language()); + pd = &msg.check_size_t(0xFFFF); } + pd->info_board.encode(add_color(pd->info_board.decode(c->language())), c->language()); } - return modified ? HandlerResult::Type::MODIFIED : HandlerResult::Type::FORWARD; + co_return modified ? HandlerResult::MODIFIED : HandlerResult::FORWARD; } -static HandlerResult C_GX_D9(shared_ptr, uint16_t, uint32_t, string& data) { - data = add_color(data); +static asio::awaitable C_GX_D9(shared_ptr, Channel::Message& msg) { + phosg::strip_trailing_zeroes(msg.data); + msg.data = add_color(msg.data); + msg.data.push_back(0); + while (msg.data.size() & 3) { + msg.data.push_back(0); + } // TODO: We should check if the info board text was actually modified and // return FORWARD if not. - return HandlerResult::Type::MODIFIED; + co_return HandlerResult::MODIFIED; } -static HandlerResult C_B_D9(shared_ptr ses, uint16_t, uint32_t, string& data) { +static asio::awaitable C_B_D9(shared_ptr c, Channel::Message& msg) { try { - string decoded = tt_utf16_to_utf8(data.data(), data.size()); + phosg::strip_trailing_zeroes(msg.data); + if (msg.data.size() & 1) { + msg.data.push_back(0); + } + string decoded = tt_utf16_to_utf8(msg.data.data(), msg.data.size()); add_color_inplace(decoded); - data = tt_utf8_to_utf16(data.data(), data.size()); + msg.data = tt_utf8_to_utf16(decoded.data(), decoded.size()); + while (msg.data.size() & 3) { + msg.data.push_back(0); + } } catch (const runtime_error& e) { - ses->log.warning("Failed to decode and unescape D9 command: %s", e.what()); + c->log.warning_f("Failed to decode and unescape D9 command: {}", e.what()); } // TODO: We should check if the info board text was actually modified and // return HandlerResult::FORWARD if not. - return HandlerResult::Type::MODIFIED; + co_return HandlerResult::MODIFIED; } template -static HandlerResult S_44_A6(shared_ptr ses, uint16_t command, uint32_t, string& data) { - const auto& cmd = check_size_t(data); +static asio::awaitable S_44_A6(shared_ptr c, Channel::Message& msg) { + const auto& cmd = msg.check_size_t(); string filename = cmd.filename.decode(); string output_filename; - bool is_download = (command == 0xA6); - if (ses->config.check_flag(Client::Flag::PROXY_SAVE_FILES)) { + bool is_download = (msg.command == 0xA6); + if (c->check_flag(Client::Flag::PROXY_SAVE_FILES)) { size_t extension_offset = filename.rfind('.'); string basename, extension; if (extension_offset != string::npos) { basename = filename.substr(0, extension_offset); extension = filename.substr(extension_offset); - if (extension == ".bin" && is_ep3(ses->version())) { + if (extension == ".bin" && is_ep3(c->version())) { extension += ".mnm"; } } else { basename = filename; } - output_filename = phosg::string_printf("%s.%s.%" PRIu64 "%s", - basename.c_str(), - is_download ? "download" : "online", - phosg::now(), - extension.c_str()); + output_filename = std::format("{}.{}.{}{}", + basename, is_download ? "download" : "online", phosg::now(), extension); for (size_t x = 0; x < output_filename.size(); x++) { if (output_filename[x] < 0x20 || output_filename[x] > 0x7E || output_filename[x] == '/') { @@ -1349,52 +1196,58 @@ static HandlerResult S_44_A6(shared_ptr ses, uint16_ } // Episode 3 download quests aren't DLQ-encoded (but they are on Trial Edition) - bool decode_dlq = is_download && (ses->version() != Version::GC_EP3); - ProxyServer::LinkedSession::SavingFile sf(filename, output_filename, cmd.file_size, decode_dlq); - ses->saving_files.emplace(filename, std::move(sf)); - if (ses->config.check_flag(Client::Flag::PROXY_SAVE_FILES)) { - ses->log.info("Saving %s from server to %s", filename.c_str(), output_filename.c_str()); + bool decode_dlq = is_download && (c->version() != Version::GC_EP3); + auto emplace_ret = c->proxy_session->saving_files.emplace(filename, ProxySession::SavingFile()); + auto& sf = emplace_ret.first->second; + sf.basename = filename; + sf.output_filename = output_filename; + sf.total_size = cmd.file_size; + sf.is_download = decode_dlq; + if (c->check_flag(Client::Flag::PROXY_SAVE_FILES)) { + c->log.info_f("Saving {} from server to {}", filename, output_filename); } else { - ses->log.info("Tracking file %s", filename.c_str()); + c->log.info_f("Tracking file {}", filename); } - return HandlerResult::Type::FORWARD; + co_return HandlerResult::FORWARD; } -constexpr on_command_t S_D_44_A6 = &S_44_A6; -constexpr on_command_t S_PG_44_A6 = &S_44_A6; -constexpr on_command_t S_X_44_A6 = &S_44_A6; -constexpr on_command_t S_B_44_A6 = &S_44_A6; +constexpr on_message_t S_D_44_A6 = &S_44_A6; +constexpr on_message_t S_PG_44_A6 = &S_44_A6; +constexpr on_message_t S_X_44_A6 = &S_44_A6; +constexpr on_message_t S_B_44_A6 = &S_44_A6; -static HandlerResult S_13_A7(shared_ptr ses, uint16_t, uint32_t flag, string& data) { - auto& cmd = check_size_t(data); +static asio::awaitable S_13_A7(shared_ptr c, Channel::Message& msg) { + auto& cmd = msg.check_size_t(); bool modified = false; - ProxyServer::LinkedSession::SavingFile* sf = nullptr; + ProxySession::SavingFile* sf = nullptr; try { - sf = &ses->saving_files.at(cmd.filename.decode()); + sf = &c->proxy_session->saving_files.at(cmd.filename.decode()); } catch (const out_of_range&) { string filename = cmd.filename.decode(); - ses->log.warning("Received data for non-open file %s", filename.c_str()); - return HandlerResult::Type::FORWARD; + c->log.warning_f("Received data for non-open file {}", filename); + } + if (!sf) { + co_return HandlerResult::FORWARD; } bool is_last_block = (cmd.data_size != 0x400); - size_t block_offset = flag * 0x400; + size_t block_offset = msg.flag * 0x400; size_t allowed_block_size = (block_offset < sf->total_size) ? min(sf->total_size - block_offset, 0x400) : 0; if (cmd.data_size > allowed_block_size) { - ses->log.warning("Block size extends beyond allowed size; truncating block"); + c->log.warning_f("Block size extends beyond allowed size; truncating block"); cmd.data_size = allowed_block_size; modified = true; } if (!sf->output_filename.empty()) { - ses->log.info("Adding %" PRIu32 " bytes to %s:%02" PRIX32 " => %s:%zX", - cmd.data_size.load(), sf->basename.c_str(), flag, sf->output_filename.c_str(), block_offset); - if (ses->config.check_flag(Client::Flag::PROXY_SAVE_FILES)) { + c->log.info_f("Adding {} bytes to {}:{:02X} => {}:{:X}", + cmd.data_size, sf->basename, msg.flag, sf->output_filename, block_offset); + if (c->check_flag(Client::Flag::PROXY_SAVE_FILES)) { size_t block_end_offset = block_offset + cmd.data_size; if (sf->data.size() < block_end_offset) { sf->data.resize(block_end_offset); @@ -1404,112 +1257,112 @@ static HandlerResult S_13_A7(shared_ptr ses, uint16_ } if (is_last_block) { - if (ses->config.check_flag(Client::Flag::PROXY_SAVE_FILES)) { - ses->log.info("Writing file %s => %s", sf->basename.c_str(), sf->output_filename.c_str()); + if (c->check_flag(Client::Flag::PROXY_SAVE_FILES)) { + c->log.info_f("Writing file {} => {}", sf->basename, sf->output_filename); - if (sf->is_download && (phosg::ends_with(sf->basename, ".bin") || phosg::ends_with(sf->basename, ".dat") || phosg::ends_with(sf->basename, ".pvr"))) { + if (sf->is_download && (sf->basename.ends_with(".bin") || sf->basename.ends_with(".dat") || sf->basename.ends_with(".pvr"))) { sf->data = decode_dlq_data(sf->data); } phosg::save_file(sf->output_filename, sf->data); - if (phosg::ends_with(sf->basename, ".bin")) { + if (sf->basename.ends_with(".bin")) { try { string decompressed = prs_decompress(sf->data); - auto disassembly = disassemble_quest_script(decompressed.data(), decompressed.size(), ses->version(), ses->language(), false); + auto disassembly = disassemble_quest_script(decompressed.data(), decompressed.size(), c->version(), c->language(), false); phosg::save_file(sf->output_filename + ".txt", disassembly); } catch (const exception& e) { - ses->log.warning("Failed to disassemble quest file: %s", e.what()); + c->log.warning_f("Failed to disassemble quest file: {}", e.what()); } } } else { - ses->log.info("Download complete for file %s", sf->basename.c_str()); + c->log.info_f("Download complete for file {}", sf->basename); } - if (!sf->is_download && phosg::ends_with(sf->basename, ".dat")) { + if (!sf->is_download && sf->basename.ends_with(".dat")) { auto quest_dat_data = make_shared(prs_decompress(sf->data)); try { auto map_file = make_shared(quest_dat_data); - auto materialized_map_file = map_file->materialize_random_sections(ses->lobby_random_seed); + auto materialized_map_file = map_file->materialize_random_sections(c->proxy_session->lobby_random_seed); array, NUM_VERSIONS> map_files; - map_files.at(static_cast(ses->version())) = materialized_map_file; - auto supermap = make_shared(ses->lobby_episode, map_files); + map_files.at(static_cast(c->version())) = materialized_map_file; + auto supermap = make_shared(c->proxy_session->lobby_episode, map_files); - ses->map_state = make_shared( - ses->id, - ses->lobby_difficulty, - ses->lobby_event, - ses->lobby_random_seed, + c->proxy_session->map_state = make_shared( + c->id, + c->proxy_session->lobby_difficulty, + c->proxy_session->lobby_event, + c->proxy_session->lobby_random_seed, MapState::DEFAULT_RARE_ENEMIES, - make_shared(ses->lobby_random_seed), + make_shared(c->proxy_session->lobby_random_seed), supermap); } catch (const exception& e) { - ses->log.warning("Failed to load quest map: %s", e.what()); - ses->map_state.reset(); + c->log.warning_f("Failed to load quest map: {}", e.what()); + c->proxy_session->map_state.reset(); } } - ses->saving_files.erase(cmd.filename.decode()); + c->proxy_session->saving_files.erase(cmd.filename.decode()); } - return modified ? HandlerResult::Type::MODIFIED : HandlerResult::Type::FORWARD; + co_return modified ? HandlerResult::MODIFIED : HandlerResult::FORWARD; } -static HandlerResult S_G_B7(shared_ptr ses, uint16_t, uint32_t, string& data) { - if (is_ep3(ses->version())) { - if (ses->config.check_flag(Client::Flag::PROXY_EP3_INFINITE_MESETA_ENABLED)) { - auto& cmd = check_size_t(data); +static asio::awaitable S_G_B7(shared_ptr c, Channel::Message& msg) { + if (is_ep3(c->version())) { + if (c->check_flag(Client::Flag::PROXY_EP3_INFINITE_MESETA_ENABLED)) { + auto& cmd = msg.check_size_t(); if (cmd.current_meseta != 1000000) { cmd.current_meseta = 1000000; - return HandlerResult::Type::MODIFIED; + co_return HandlerResult::MODIFIED; } } - return HandlerResult::Type::FORWARD; + co_return HandlerResult::FORWARD; } else { - ses->server_channel.send(0xB7, 0x00); - return HandlerResult::Type::SUPPRESS; + c->proxy_session->server_channel->send(0xB7, 0x00); + co_return HandlerResult::SUPPRESS; } } -static HandlerResult S_G_B8(shared_ptr ses, uint16_t, uint32_t, string& data) { - if (ses->config.check_flag(Client::Flag::PROXY_SAVE_FILES)) { - if (data.size() < 4) { - ses->log.warning("Card list data size is too small; not saving file"); - return HandlerResult::Type::FORWARD; +static asio::awaitable S_G_B8(shared_ptr c, Channel::Message& msg) { + if (c->check_flag(Client::Flag::PROXY_SAVE_FILES)) { + if (msg.data.size() < 4) { + c->log.warning_f("Card list data size is too small; not saving file"); + co_return HandlerResult::FORWARD; } - phosg::StringReader r(data); + phosg::StringReader r(msg.data); size_t size = r.get_u32l(); if (r.remaining() < size) { - ses->log.warning("Card list data size extends beyond end of command; not saving file"); - return HandlerResult::Type::FORWARD; + c->log.warning_f("Card list data size extends beyond end of command; not saving file"); + co_return HandlerResult::FORWARD; } - string output_filename = phosg::string_printf("card-definitions.%" PRIu64 ".mnr", phosg::now()); + string output_filename = std::format("card-definitions.{}.mnr", phosg::now()); phosg::save_file(output_filename, r.read(size)); - ses->log.info("Wrote %zu bytes to %s", size, output_filename.c_str()); + c->log.info_f("Wrote {} bytes to {}", size, output_filename); } // Unset the flag specifying that the client has newserv's card definitions, // so the file sill be sent again if the client returns to newserv. - ses->config.clear_flag(Client::Flag::HAS_EP3_CARD_DEFS); + c->clear_flag(Client::Flag::HAS_EP3_CARD_DEFS); - return is_ep3(ses->version()) ? HandlerResult::Type::FORWARD : HandlerResult::Type::SUPPRESS; + co_return is_ep3(c->version()) ? HandlerResult::FORWARD : HandlerResult::SUPPRESS; } -static HandlerResult S_G_B9(shared_ptr ses, uint16_t, uint32_t, string& data) { - if (ses->config.check_flag(Client::Flag::PROXY_SAVE_FILES)) { +static asio::awaitable S_G_B9(shared_ptr c, Channel::Message& msg) { + if (c->check_flag(Client::Flag::PROXY_SAVE_FILES)) { try { - const auto& header = check_size_t(data, 0xFFFF); + const auto& header = msg.check_size_t(0xFFFF); - if (data.size() - sizeof(header) < header.size) { + if (msg.data.size() - sizeof(header) < header.size) { throw runtime_error("Media data size extends beyond end of command; not saving file"); } string decompressed_data = prs_decompress( - data.data() + sizeof(header), data.size() - sizeof(header)); + msg.data.data() + sizeof(header), msg.data.size() - sizeof(header)); - string output_filename = phosg::string_printf("media-update.%" PRIu64, phosg::now()); + string output_filename = std::format("media-update.{}", phosg::now()); if (header.type == 1) { output_filename += ".gvm"; } else if (header.type == 2 || header.type == 3) { @@ -1518,140 +1371,143 @@ static HandlerResult S_G_B9(shared_ptr ses, uint16_t output_filename += ".bin"; } phosg::save_file(output_filename, decompressed_data); - ses->log.info("Wrote %zu bytes to %s", - decompressed_data.size(), output_filename.c_str()); + c->log.info_f("Wrote {} bytes to {}", decompressed_data.size(), output_filename); } catch (const exception& e) { - ses->log.warning("Failed to save file: %s", e.what()); + c->log.warning_f("Failed to save file: {}", e.what()); } } // This command exists only in final Episode 3 and not in Trial Edition // (hence not using is_ep3() here) - return (ses->version() == Version::GC_EP3) - ? HandlerResult::Type::FORWARD - : HandlerResult::Type::SUPPRESS; + co_return (c->version() == Version::GC_EP3) ? HandlerResult::FORWARD : HandlerResult::SUPPRESS; } -static HandlerResult S_G_EF(shared_ptr ses, uint16_t, uint32_t, string& data) { - if (is_ep3(ses->version())) { - if (ses->config.check_flag(Client::Flag::PROXY_EP3_INFINITE_MESETA_ENABLED)) { - auto& cmd = check_size_t(data, - offsetof(S_StartCardAuction_Ep3_EF, unused), 0xFFFF); +static asio::awaitable S_G_EF(shared_ptr c, Channel::Message& msg) { + if (is_ep3(c->version())) { + if (c->check_flag(Client::Flag::PROXY_EP3_INFINITE_MESETA_ENABLED)) { + auto& cmd = msg.check_size_t(offsetof(S_StartCardAuction_Ep3_EF, unused), 0xFFFF); if (cmd.points_available != 0x7FFF) { cmd.points_available = 0x7FFF; - return HandlerResult::Type::MODIFIED; + co_return HandlerResult::MODIFIED; } } - return HandlerResult::Type::FORWARD; + co_return HandlerResult::FORWARD; } else { - return HandlerResult::Type::SUPPRESS; + co_return HandlerResult::SUPPRESS; } } -static HandlerResult S_B_EF(shared_ptr, uint16_t, uint32_t, string&) { - return HandlerResult::Type::SUPPRESS; +static asio::awaitable S_B_EF(shared_ptr, Channel::Message&) { + // See the comments on EF in CommandFormats.hh for why we unconditionally + // suppress these. + co_return HandlerResult::SUPPRESS; } -static HandlerResult S_G_BA(shared_ptr ses, uint16_t, uint32_t, string& data) { - if (ses->config.check_flag(Client::Flag::PROXY_EP3_INFINITE_MESETA_ENABLED)) { - auto& cmd = check_size_t(data); +static asio::awaitable S_G_BA(shared_ptr c, Channel::Message& msg) { + if (c->check_flag(Client::Flag::PROXY_EP3_INFINITE_MESETA_ENABLED)) { + auto& cmd = msg.check_size_t(); if (cmd.current_meseta != 1000000) { cmd.current_meseta = 1000000; - return HandlerResult::Type::MODIFIED; + co_return HandlerResult::MODIFIED; } } - return HandlerResult::Type::FORWARD; + co_return HandlerResult::FORWARD; } -static void update_leader_id(shared_ptr ses, uint8_t leader_id) { - if (ses->leader_client_id != leader_id) { - ses->leader_client_id = leader_id; - ses->log.info("Changed room leader to %zu", ses->leader_client_id); - if (ses->config.check_flag(Client::Flag::PROXY_PLAYER_NOTIFICATIONS_ENABLED) && (ses->leader_client_id == ses->lobby_client_id)) { - send_text_message(ses->client_channel, "$C6You are now the leader"); +static void update_leader_id(shared_ptr c, uint8_t leader_id) { + if (c->proxy_session->leader_client_id != leader_id) { + c->proxy_session->leader_client_id = leader_id; + c->log.info_f("Changed room leader to {:X}", c->proxy_session->leader_client_id); + if (c->check_flag(Client::Flag::PROXY_PLAYER_NOTIFICATIONS_ENABLED) && + (c->proxy_session->leader_client_id == c->lobby_client_id)) { + send_text_message(c->channel, "$C6You are now the leader"); } } } template -static HandlerResult S_65_67_68_EB(shared_ptr ses, uint16_t command, uint32_t flag, string& data) { - if (command == 0x67) { - ses->clear_lobby_players(12); - ses->is_in_game = false; - ses->is_in_quest = false; - ses->floor = 0x0F; - ses->lobby_difficulty = 0; - ses->lobby_section_id = 0; - ses->lobby_mode = GameMode::NORMAL; - ses->lobby_episode = Episode::EP1; - ses->lobby_random_seed = 0; - ses->item_creator.reset(); - ses->map_state.reset(); +static asio::awaitable S_65_67_68_EB(shared_ptr c, Channel::Message& msg) { + if (msg.command == 0x67) { + c->proxy_session->clear_lobby_players(12); + c->proxy_session->is_in_game = false; + c->proxy_session->is_in_quest = false; + c->floor = 0x0F; + c->proxy_session->lobby_difficulty = 0; + c->proxy_session->lobby_section_id = 0; + c->proxy_session->lobby_mode = GameMode::NORMAL; + c->proxy_session->lobby_episode = Episode::EP1; + c->proxy_session->lobby_random_seed = 0; + c->proxy_session->item_creator.reset(); + c->proxy_session->map_state.reset(); // This command can cause the client to no longer send D6 responses when // 1A/D5 large message boxes are closed. newserv keeps track of this // behavior in the client config, so if it happens during a proxy session, // update the client config that we'll restore if the client uses the change // ship or change block command. - if (ses->config.check_flag(Client::Flag::NO_D6_AFTER_LOBBY)) { - ses->config.set_flag(Client::Flag::NO_D6); + if (c->check_flag(Client::Flag::NO_D6_AFTER_LOBBY)) { + c->set_flag(Client::Flag::NO_D6); } } - size_t expected_size = offsetof(CmdT, entries) + sizeof(typename CmdT::Entry) * flag; - auto& cmd = check_size_t(data, expected_size, 0xFFFF); + size_t expected_size = offsetof(CmdT, entries) + sizeof(typename CmdT::Entry) * msg.flag; + auto& cmd = msg.check_size_t(expected_size, 0xFFFF); bool modified = false; size_t num_replacements = 0; - ses->lobby_client_id = cmd.lobby_flags.client_id; - ses->lobby_event = cmd.lobby_flags.event; - update_leader_id(ses, cmd.lobby_flags.leader_id); - for (size_t x = 0; x < flag; x++) { + c->lobby_client_id = cmd.lobby_flags.client_id; + update_leader_id(c, cmd.lobby_flags.leader_id); + for (size_t x = 0; x < msg.flag; x++) { auto& entry = cmd.entries[x]; size_t index = entry.lobby_data.client_id; - if (index >= ses->lobby_players.size()) { - ses->log.warning("Ignoring invalid player index %zu at position %zu", index, x); + if (index >= c->proxy_session->lobby_players.size()) { + c->log.warning_f("Ignoring invalid player index {} at position {}", index, x); } else { string name = escape_player_name(entry.disp.visual.name.decode(entry.inventory.language)); - if (ses->login && (entry.lobby_data.guild_card_number == ses->remote_guild_card_number)) { - entry.lobby_data.guild_card_number = ses->login->account->account_id; + if (c->login && (entry.lobby_data.guild_card_number == c->proxy_session->remote_guild_card_number)) { num_replacements++; - modified = true; - } else if (ses->config.check_flag(Client::Flag::PROXY_PLAYER_NOTIFICATIONS_ENABLED) && - (command != 0x67)) { - send_text_message_printf(ses->client_channel, "$C6Join: %zu/%" PRIu32 "\n%s", - index, entry.lobby_data.guild_card_number.load(), name.c_str()); + if (c->login->account->account_id != c->proxy_session->remote_guild_card_number) { + entry.lobby_data.guild_card_number = c->login->account->account_id; + modified = true; + } + } else if (c->check_flag(Client::Flag::PROXY_PLAYER_NOTIFICATIONS_ENABLED) && (msg.command != 0x67)) { + send_text_message_fmt(c->channel, "$C6Join: {}/{}\n{}", + index, entry.lobby_data.guild_card_number, name); } - auto& p = ses->lobby_players[index]; + auto& p = c->proxy_session->lobby_players[index]; p.guild_card_number = entry.lobby_data.guild_card_number; p.name = name; p.language = entry.inventory.language; p.section_id = entry.disp.visual.section_id; p.char_class = entry.disp.visual.char_class; - ses->log.info("Added lobby player: (%zu) %" PRIu32 " %s", - index, p.guild_card_number, p.name.c_str()); + c->log.info_f("Added lobby player: ({}) {} {}", + index, p.guild_card_number, p.name); } } if (num_replacements > 1) { - ses->log.warning("Proxied player appears multiple times in lobby"); + c->log.warning_f("Proxied player appears multiple times in lobby"); } - if (ses->config.override_lobby_event != 0xFF) { - cmd.lobby_flags.event = ses->config.override_lobby_event; - modified = true; - } - if (ses->config.override_lobby_number != 0x80) { - cmd.lobby_flags.lobby_number = ses->config.override_lobby_number; - modified = true; + if constexpr (sizeof(cmd.lobby_flags) > sizeof(LobbyFlags_DCNTE)) { + c->proxy_session->lobby_event = cmd.lobby_flags.event; + if (c->override_lobby_event != 0xFF) { + cmd.lobby_flags.event = c->override_lobby_event; + modified = true; + } + if (c->override_lobby_number != 0x80) { + cmd.lobby_flags.lobby_number = c->override_lobby_number; + modified = true; + } } - return modified ? HandlerResult::Type::MODIFIED : HandlerResult::Type::FORWARD; + co_return modified ? HandlerResult::MODIFIED : HandlerResult::FORWARD; } -constexpr on_command_t S_DG_65_67_68_EB = &S_65_67_68_EB; -constexpr on_command_t S_P_65_67_68 = &S_65_67_68_EB; -constexpr on_command_t S_X_65_67_68 = &S_65_67_68_EB; -constexpr on_command_t S_B_65_67_68 = &S_65_67_68_EB; +constexpr on_message_t S_N_65_67_68 = &S_65_67_68_EB; +constexpr on_message_t S_DG_65_67_68_EB = &S_65_67_68_EB; +constexpr on_message_t S_P_65_67_68 = &S_65_67_68_EB; +constexpr on_message_t S_X_65_67_68 = &S_65_67_68_EB; +constexpr on_message_t S_B_65_67_68 = &S_65_67_68_EB; template Episode get_episode(const CmdT&) { @@ -1698,76 +1554,101 @@ Episode get_episode(const S_JoinGame_Ep3_64&) { } template -static HandlerResult S_64(shared_ptr ses, uint16_t, uint32_t flag, string& data) { +static asio::awaitable S_64(shared_ptr c, Channel::Message& msg) { CmdT* cmd; S_JoinGame_Ep3_64* cmd_ep3 = nullptr; - if (ses->sub_version >= 0x40) { - cmd = &check_size_t(data, sizeof(S_JoinGame_Ep3_64)); - cmd_ep3 = &check_size_t(data); - } else if (ses->version() == Version::XB_V3) { + if (c->sub_version >= 0x40) { + cmd = &msg.check_size_t(sizeof(S_JoinGame_Ep3_64)); + cmd_ep3 = &msg.check_size_t(); + } else if (c->version() == Version::XB_V3) { // Schtserv doesn't send the unknown_a1 field in this command, and we don't // use it here, so we allow it to be omitted. - cmd = &check_size_t(data.data(), data.size(), sizeof(CmdT) - 0x18, sizeof(CmdT)); + cmd = &msg.check_size_t(sizeof(CmdT) - 0x18, sizeof(CmdT)); } else { - cmd = &check_size_t(data, 0xFFFF); + cmd = &msg.check_size_t(0xFFFF); } - ses->clear_lobby_players(4); - ses->floor = 0; - ses->is_in_game = true; - ses->is_in_quest = false; - ses->lobby_event = cmd->event; - ses->lobby_difficulty = cmd->difficulty; - ses->lobby_section_id = cmd->section_id; - // We only need the game mode for overriding drops, and SOLO behaves the same - // as NORMAL in that regard, so we can conveniently ignore SOLO here - if (cmd->battle_mode) { - ses->lobby_mode = GameMode::BATTLE; - } else if (cmd->challenge_mode) { - ses->lobby_mode = GameMode::CHALLENGE; + bool modified = false; + + c->proxy_session->clear_lobby_players(4); + c->floor = 0; + c->proxy_session->is_in_game = true; + c->proxy_session->is_in_quest = false; + if constexpr (sizeof(cmd) > sizeof(S_JoinGame_DCNTE_64)) { + c->proxy_session->lobby_event = cmd->event; + c->proxy_session->lobby_difficulty = cmd->difficulty; + c->proxy_session->lobby_section_id = cmd->section_id; + // We only need the game mode for overriding drops, and SOLO behaves the same + // as NORMAL in that regard, so we can conveniently ignore SOLO here + if (cmd->battle_mode) { + c->proxy_session->lobby_mode = GameMode::BATTLE; + } else if (cmd->challenge_mode) { + c->proxy_session->lobby_mode = GameMode::CHALLENGE; + } else { + c->proxy_session->lobby_mode = GameMode::NORMAL; + } + c->proxy_session->lobby_random_seed = cmd->random_seed; + + if (c->override_section_id != 0xFF) { + cmd->section_id = c->override_section_id; + modified = true; + } + if (c->override_lobby_event != 0xFF) { + cmd->event = c->override_lobby_event; + modified = true; + } + if (c->override_random_seed >= 0) { + cmd->random_seed = c->override_random_seed; + modified = true; + } + } else { - ses->lobby_mode = GameMode::NORMAL; + c->proxy_session->lobby_event = 0; + c->proxy_session->lobby_difficulty = 0; + c->proxy_session->lobby_section_id = c->character()->disp.visual.section_id; + c->proxy_session->lobby_mode = GameMode::NORMAL; + c->proxy_session->lobby_random_seed = phosg::random_object(); } - ses->lobby_random_seed = cmd->random_seed; if (cmd_ep3) { - ses->lobby_episode = Episode::EP3; + c->proxy_session->lobby_episode = Episode::EP3; } else { - ses->lobby_episode = get_episode(*cmd); + c->proxy_session->lobby_episode = get_episode(*cmd); } - if (ses->version() == Version::GC_NTE) { + if (c->version() == Version::GC_NTE) { // GC NTE ignores the variations field entirely, so clear the array to // ensure we'll load the correct maps cmd->variations = Variations(); } // Recreate the item creator if needed, and load maps - auto s = ses->require_server_state(); - ses->set_drop_mode(ses->drop_mode); - if (!is_ep3(ses->version()) && (ses->lobby_mode != GameMode::CHALLENGE)) { - auto s = ses->require_server_state(); - ses->map_state = make_shared( - ses->id, - ses->lobby_difficulty, - ses->lobby_event, - ses->lobby_random_seed, + auto s = c->require_server_state(); + c->proxy_session->set_drop_mode(s, c->version(), c->override_random_seed, c->proxy_session->drop_mode); + if (!is_ep3(c->version()) && (c->proxy_session->lobby_mode != GameMode::CHALLENGE)) { + auto supermaps = s->supermaps_for_variations( + c->proxy_session->lobby_episode, c->proxy_session->lobby_mode, c->proxy_session->lobby_difficulty, cmd->variations); + c->proxy_session->map_state = make_shared( + c->id, + c->proxy_session->lobby_difficulty, + c->proxy_session->lobby_event, + c->proxy_session->lobby_random_seed, MapState::DEFAULT_RARE_ENEMIES, - make_shared(ses->lobby_random_seed), - s->supermaps_for_variations(ses->lobby_episode, ses->lobby_mode, ses->lobby_difficulty, cmd->variations)); + make_shared(c->proxy_session->lobby_random_seed), + supermaps); } else { - ses->map_state.reset(); + c->proxy_session->map_state.reset(); } - bool modified = false; - - ses->lobby_client_id = cmd->client_id; - update_leader_id(ses, cmd->leader_id); - for (size_t x = 0; x < flag; x++) { - if (cmd->lobby_data[x].guild_card_number == ses->remote_guild_card_number) { - cmd->lobby_data[x].guild_card_number = ses->login->account->account_id; + c->lobby_client_id = cmd->client_id; + update_leader_id(c, cmd->leader_id); + for (size_t x = 0; x < msg.flag; x++) { + if (cmd->lobby_data[x].guild_card_number == c->proxy_session->remote_guild_card_number && + c->login && + c->login->account->account_id != c->proxy_session->remote_guild_card_number) { + cmd->lobby_data[x].guild_card_number = c->login->account->account_id; modified = true; } - auto& p = ses->lobby_players[x]; + auto& p = c->proxy_session->lobby_players[x]; p.guild_card_number = cmd->lobby_data[x].guild_card_number; if (cmd_ep3) { const auto& p_ep3 = cmd_ep3->players_ep3[x]; @@ -1778,630 +1659,634 @@ static HandlerResult S_64(shared_ptr ses, uint16_t, } else { p.name.clear(); } - ses->log.info("Added lobby player: (%zu) %" PRIu32 " %s", x, p.guild_card_number, p.name.c_str()); + c->log.info_f("Added lobby player: ({}) {} {}", x, p.guild_card_number, p.name); } - if (ses->config.override_section_id != 0xFF) { - cmd->section_id = ses->config.override_section_id; - modified = true; - } - if (ses->config.override_lobby_event != 0xFF) { - cmd->event = ses->config.override_lobby_event; - modified = true; - } - if (ses->config.check_flag(Client::Flag::USE_OVERRIDE_RANDOM_SEED)) { - cmd->random_seed = ses->config.override_random_seed; - modified = true; - } - - return modified ? HandlerResult::Type::MODIFIED : HandlerResult::Type::FORWARD; + co_return modified ? HandlerResult::MODIFIED : HandlerResult::FORWARD; } -constexpr on_command_t S_D_64 = &S_64; -constexpr on_command_t S_P_64 = &S_64; -constexpr on_command_t S_G_64 = &S_64; -constexpr on_command_t S_X_64 = &S_64; -constexpr on_command_t S_B_64 = &S_64; +constexpr on_message_t S_N_64 = &S_64; +constexpr on_message_t S_D_64 = &S_64; +constexpr on_message_t S_P_64 = &S_64; +constexpr on_message_t S_G_64 = &S_64; +constexpr on_message_t S_X_64 = &S_64; +constexpr on_message_t S_B_64 = &S_64; -static HandlerResult S_E8(shared_ptr ses, uint16_t, uint32_t, string& data) { - auto& cmd = check_size_t(data); +static asio::awaitable S_E8(shared_ptr c, Channel::Message& msg) { + auto& cmd = msg.check_size_t(); - ses->clear_lobby_players(12); - ses->floor = 0; - ses->is_in_game = true; - ses->is_in_quest = false; - ses->lobby_event = cmd.event; - ses->lobby_difficulty = 0; - ses->lobby_section_id = cmd.section_id; - ses->lobby_mode = GameMode::NORMAL; - ses->lobby_random_seed = 0; - ses->lobby_episode = Episode::EP3; - ses->item_creator.reset(); - ses->map_state.reset(); + c->floor = 0; + c->proxy_session->is_in_game = true; + c->proxy_session->is_in_quest = false; + c->proxy_session->lobby_event = cmd.event; + c->proxy_session->lobby_difficulty = 0; + c->proxy_session->lobby_section_id = cmd.section_id; + c->proxy_session->lobby_mode = GameMode::NORMAL; + c->proxy_session->lobby_random_seed = 0; + c->proxy_session->lobby_episode = Episode::EP3; + c->proxy_session->item_creator.reset(); + c->proxy_session->map_state.reset(); + c->proxy_session->clear_lobby_players(12); bool modified = false; - ses->lobby_client_id = cmd.client_id; - update_leader_id(ses, cmd.leader_id); + c->lobby_client_id = cmd.client_id; + update_leader_id(c, cmd.leader_id); for (size_t x = 0; x < 12; x++) { auto& player_entry = (x < 4) ? cmd.players[x] : cmd.spectator_players[x - 4]; auto& spec_entry = cmd.entries[x]; - if (player_entry.lobby_data.guild_card_number == ses->remote_guild_card_number) { - player_entry.lobby_data.guild_card_number = ses->login->account->account_id; - modified = true; - } - if (spec_entry.guild_card_number == ses->remote_guild_card_number) { - spec_entry.guild_card_number = ses->login->account->account_id; - modified = true; + if (c->login && c->login->account->account_id != c->proxy_session->remote_guild_card_number) { + if (player_entry.lobby_data.guild_card_number == c->proxy_session->remote_guild_card_number) { + player_entry.lobby_data.guild_card_number = c->login->account->account_id; + modified = true; + } + if (spec_entry.guild_card_number == c->proxy_session->remote_guild_card_number) { + spec_entry.guild_card_number = c->login->account->account_id; + modified = true; + } } - auto& p = ses->lobby_players[x]; + auto& p = c->proxy_session->lobby_players[x]; p.guild_card_number = player_entry.lobby_data.guild_card_number; p.language = player_entry.inventory.language; p.name = player_entry.disp.visual.name.decode(p.language); p.section_id = player_entry.disp.visual.section_id; p.char_class = player_entry.disp.visual.char_class; - ses->log.info("Added lobby player: (%zu) %" PRIu32 " %s", x, p.guild_card_number, p.name.c_str()); + c->log.info_f("Added lobby player: ({}) {} {}", x, p.guild_card_number, p.name); } - if (ses->config.override_section_id != 0xFF) { - cmd.section_id = ses->config.override_section_id; + if (c->override_section_id != 0xFF) { + cmd.section_id = c->override_section_id; modified = true; } - if (ses->config.override_lobby_event != 0xFF) { - cmd.event = ses->config.override_lobby_event; + if (c->override_lobby_event != 0xFF) { + cmd.event = c->override_lobby_event; modified = true; } - if (ses->config.check_flag(Client::Flag::USE_OVERRIDE_RANDOM_SEED)) { - cmd.random_seed = ses->config.override_random_seed; + if (c->override_random_seed >= 0) { + cmd.random_seed = c->override_random_seed; modified = true; } - return modified ? HandlerResult::Type::MODIFIED : HandlerResult::Type::FORWARD; + co_return modified ? HandlerResult::MODIFIED : HandlerResult::FORWARD; } -static HandlerResult S_AC(shared_ptr ses, uint16_t, uint32_t, string&) { - if (!ses->is_in_game) { - return HandlerResult::Type::SUPPRESS; +static asio::awaitable S_AC(shared_ptr c, Channel::Message&) { + if (!c->proxy_session->is_in_game) { + co_return HandlerResult::SUPPRESS; } else { - ses->is_in_quest = true; - return HandlerResult::Type::FORWARD; + c->proxy_session->is_in_quest = true; + co_return HandlerResult::FORWARD; } } -static HandlerResult S_66_69_E9(shared_ptr ses, uint16_t, uint32_t, string& data) { +static asio::awaitable S_66_69_E9(shared_ptr c, Channel::Message& msg) { // Schtserv sends a large command here for unknown reasons. The client ignores // the extra data, so we allow the large command here. - const auto& cmd = check_size_t(data, 0xFFFF); + const auto& cmd = msg.check_size_t(0xFFFF); size_t index = cmd.client_id; - if (index >= ses->lobby_players.size()) { - ses->log.warning("Lobby leave command references missing position"); + if (index >= c->proxy_session->lobby_players.size()) { + c->log.warning_f("Lobby leave command references missing position"); } else { - auto& p = ses->lobby_players[index]; + auto& p = c->proxy_session->lobby_players[index]; string name = escape_player_name(p.name); - if (ses->config.check_flag(Client::Flag::PROXY_PLAYER_NOTIFICATIONS_ENABLED)) { - send_text_message_printf(ses->client_channel, "$C4Leave: %zu/%" PRIu32 "\n%s", - index, p.guild_card_number, name.c_str()); + if (c->check_flag(Client::Flag::PROXY_PLAYER_NOTIFICATIONS_ENABLED)) { + send_text_message_fmt(c->channel, "$C4Leave: {}/{}\n{}", index, p.guild_card_number, name); } p.guild_card_number = 0; p.name.clear(); - ses->log.info("Removed lobby player (%zu)", index); + c->log.info_f("Removed lobby player ({})", index); } - update_leader_id(ses, cmd.leader_id); - return HandlerResult::Type::FORWARD; + update_leader_id(c, cmd.leader_id); + co_return HandlerResult::FORWARD; } -static HandlerResult C_98(shared_ptr ses, uint16_t command, uint32_t flag, string& data) { - ses->floor = 0x0F; - ses->is_in_game = false; - ses->is_in_quest = false; - ses->lobby_event = 0; - ses->lobby_difficulty = 0; - ses->lobby_section_id = 0; - ses->lobby_episode = Episode::EP1; - ses->lobby_mode = GameMode::NORMAL; - ses->lobby_random_seed = 0; - ses->item_creator.reset(); - ses->map_state.reset(); +static asio::awaitable C_98(shared_ptr c, Channel::Message& msg) { + c->floor = 0x0F; + c->proxy_session->is_in_game = false; + c->proxy_session->is_in_quest = false; + c->proxy_session->lobby_event = 0; + c->proxy_session->lobby_difficulty = 0; + c->proxy_session->lobby_section_id = 0; + c->proxy_session->lobby_episode = Episode::EP1; + c->proxy_session->lobby_mode = GameMode::NORMAL; + c->proxy_session->lobby_random_seed = 0; + c->proxy_session->item_creator.reset(); + c->proxy_session->map_state.reset(); + c->proxy_session->clear_lobby_players(12); - if (is_v3(ses->version()) || is_v4(ses->version())) { - return C_GXB_61(ses, command, flag, data); + if (is_v3(c->version()) || is_v4(c->version())) { + co_return co_await C_GXB_61(c, msg); } else { - return HandlerResult::Type::FORWARD; + co_return HandlerResult::FORWARD; } } -static HandlerResult C_06(shared_ptr ses, uint16_t, uint32_t, string& data) { - if (data.size() >= 12) { - const auto& cmd = check_size_t(data, 0xFFFF); +static asio::awaitable C_06(shared_ptr c, Channel::Message& msg) { + if (msg.data.size() >= 0x0C) { + const auto& cmd = msg.check_size_t(0xFFFF); - string text = data.substr(sizeof(cmd)); + string text = msg.data.substr(sizeof(cmd)); phosg::strip_trailing_zeroes(text); uint8_t private_flags = 0; try { - if (uses_utf16(ses->version())) { + if (uses_utf16(c->version())) { if (text.size() & 1) { text.push_back(0); } - text = tt_decode_marked(text, ses->language(), true); - } else if (!text.empty() && (text[0] != '\t') && is_ep3(ses->version())) { + text = tt_decode_marked(text, c->language(), true); + } else if (!text.empty() && (text[0] != '\t') && is_ep3(c->version())) { private_flags = text[0]; - text = tt_decode_marked(text.substr(1), ses->language(), false); + text = tt_decode_marked(text.substr(1), c->language(), false); } else { - text = tt_decode_marked(text, ses->language(), false); + text = tt_decode_marked(text, c->language(), false); } } catch (const runtime_error& e) { - ses->log.warning("Failed to decode and unescape chat text: %s", e.what()); - return HandlerResult::Type::FORWARD; + c->log.warning_f("Failed to decode and unescape chat text: {}", e.what()); + text.clear(); } if (text.empty()) { - return HandlerResult::Type::SUPPRESS; + co_return HandlerResult::FORWARD; } - char command_sentinel = (ses->version() == Version::DC_11_2000) ? '@' : '$'; + char command_sentinel = (c->version() == Version::DC_11_2000) ? '@' : '$'; bool is_command = (text[0] == command_sentinel) || (text[0] == '\t' && text[1] != 'C' && text[2] == command_sentinel); - if (is_command && ses->config.check_flag(Client::Flag::PROXY_CHAT_COMMANDS_ENABLED)) { + if (is_command && c->check_flag(Client::Flag::PROXY_CHAT_COMMANDS_ENABLED)) { size_t offset = ((text[0] & 0xF0) == 0x40) ? 1 : 0; offset += (text[offset] == command_sentinel) ? 0 : 2; text = text.substr(offset); if (text.size() >= 2 && text[1] == command_sentinel) { - send_chat_message_from_client(ses->server_channel, text.substr(1), private_flags); - return HandlerResult::Type::SUPPRESS; + send_chat_message_from_client(c->proxy_session->server_channel, text.substr(1), private_flags); + co_return HandlerResult::SUPPRESS; } else { - on_chat_command(ses, text, true); - return HandlerResult::Type::SUPPRESS; + co_await on_chat_command(c, text, true); + co_return HandlerResult::SUPPRESS; } - } else { - return HandlerResult::Type::FORWARD; + co_return HandlerResult::FORWARD; } - } else { - return HandlerResult::Type::FORWARD; + co_return HandlerResult::FORWARD; } } -static HandlerResult C_40(shared_ptr ses, uint16_t, uint32_t, string& data) { +static asio::awaitable C_40(shared_ptr c, Channel::Message& msg) { bool modified = false; - if (ses->login) { - auto& cmd = check_size_t(data); - if (cmd.searcher_guild_card_number == ses->login->account->account_id) { - cmd.searcher_guild_card_number = ses->remote_guild_card_number; + if (c->login && c->login->account->account_id != c->proxy_session->remote_guild_card_number) { + auto& cmd = msg.check_size_t(); + if (cmd.searcher_guild_card_number == c->login->account->account_id) { + cmd.searcher_guild_card_number = c->proxy_session->remote_guild_card_number; modified = true; } - if (cmd.target_guild_card_number == ses->login->account->account_id) { - cmd.target_guild_card_number = ses->remote_guild_card_number; + if (cmd.target_guild_card_number == c->login->account->account_id) { + cmd.target_guild_card_number = c->proxy_session->remote_guild_card_number; modified = true; } } - return modified ? HandlerResult::Type::MODIFIED : HandlerResult::Type::FORWARD; + co_return modified ? HandlerResult::MODIFIED : HandlerResult::FORWARD; } template -static HandlerResult C_81(shared_ptr ses, uint16_t, uint32_t, string& data) { - auto& cmd = check_size_t(data); - if (ses->login) { - if (cmd.from_guild_card_number == ses->login->account->account_id) { - cmd.from_guild_card_number = ses->remote_guild_card_number; +static asio::awaitable C_81(shared_ptr c, Channel::Message& msg) { + auto& cmd = msg.check_size_t(); + if (c->login && c->login->account->account_id != c->proxy_session->remote_guild_card_number) { + if (cmd.from_guild_card_number == c->login->account->account_id) { + cmd.from_guild_card_number = c->proxy_session->remote_guild_card_number; } - if (cmd.to_guild_card_number == ses->login->account->account_id) { - cmd.to_guild_card_number = ses->remote_guild_card_number; + if (cmd.to_guild_card_number == c->login->account->account_id) { + cmd.to_guild_card_number = c->proxy_session->remote_guild_card_number; } } // GC clients send uninitialized memory here; don't forward it cmd.text.clear_after_bytes(cmd.text.used_chars_8()); - return HandlerResult::Type::MODIFIED; + co_return HandlerResult::MODIFIED; } -constexpr on_command_t C_DGX_81 = &C_81; -constexpr on_command_t C_P_81 = &C_81; -constexpr on_command_t C_B_81 = &C_81; - -template -void C_6x_movement(shared_ptr ses, const string& data) { - ses->pos = check_size_t(data).pos; -} +constexpr on_message_t C_DGX_81 = &C_81; +constexpr on_message_t C_P_81 = &C_81; +constexpr on_message_t C_B_81 = &C_81; template -static HandlerResult C_6x(shared_ptr ses, uint16_t command, uint32_t flag, string& data) { - if (ses->login && !data.empty()) { - // On BB, the 6x06 command is blank - the server generates the actual Guild - // Card contents and sends it to the target client. - if ((data[0] == 0x06) && !is_v4(ses->version())) { - auto& cmd = check_size_t(data); - if (cmd.guild_card.guild_card_number == ses->login->account->account_id) { - cmd.guild_card.guild_card_number = ses->remote_guild_card_number; - } - } +asio::awaitable C_6x(shared_ptr c, Channel::Message& msg) { + if (msg.data.size() < 4) { + co_return HandlerResult::FORWARD; } - return C_6x(ses, command, flag, data); -} -constexpr on_command_t C_D_6x = &C_6x; -constexpr on_command_t C_P_6x = &C_6x; -constexpr on_command_t C_G_6x = &C_6x; -constexpr on_command_t C_X_6x = &C_6x; -constexpr on_command_t C_B_6x = &C_6x; + bool modified = false; + uint8_t subcommand = translate_subcommand_number(Version::BB_V4, c->version(), msg.data[0]); + switch (subcommand) { + case 0x05: + if (c->check_flag(Client::Flag::SWITCH_ASSIST_ENABLED)) { + auto& cmd = msg.check_size_t(); + if (c->proxy_session->map_state && (cmd.flags & 1) && (cmd.header.entity_id != 0xFFFF)) { + auto door_states = c->proxy_session->map_state->door_states_for_switch_flag( + c->version(), cmd.switch_flag_floor, cmd.switch_flag_num); + for (auto& door_state : door_states) { + if (door_state->game_flags & 0x0001) { + continue; + } + door_state->game_flags |= 1; -template <> -HandlerResult C_6x(shared_ptr ses, uint16_t, uint32_t, string& data) { - if (!data.empty()) { - if ((data[0] == 0x05) && ses->config.check_flag(Client::Flag::SWITCH_ASSIST_ENABLED)) { - auto& cmd = check_size_t(data); - if (ses->map_state && (cmd.flags & 1) && (cmd.header.entity_id != 0xFFFF)) { - auto door_states = ses->map_state->door_states_for_switch_flag( - ses->version(), cmd.switch_flag_floor, cmd.switch_flag_num); - for (auto& door_state : door_states) { - if (door_state->game_flags & 0x0001) { - continue; + uint16_t object_index = c->proxy_session->map_state->index_for_object_state(c->version(), door_state); + G_UpdateObjectState_6x0B cmd0B; + cmd0B.header.subcommand = 0x0B; + cmd0B.header.size = sizeof(cmd0B) / 4; + cmd0B.header.entity_id = object_index | 0x4000; + cmd0B.flags = door_state->game_flags; + cmd0B.object_index = object_index; + c->channel->send(0x60, 0x00, &cmd0B, sizeof(cmd0B)); + c->proxy_session->server_channel->send(0x60, 0x00, &cmd0B, sizeof(cmd0B)); } - door_state->game_flags |= 1; - - uint16_t object_index = ses->map_state->index_for_object_state(ses->version(), door_state); - G_UpdateObjectState_6x0B cmd0B; - cmd0B.header.subcommand = 0x0B; - cmd0B.header.size = sizeof(cmd0B) / 4; - cmd0B.header.entity_id = object_index | 0x4000; - cmd0B.flags = door_state->game_flags; - cmd0B.object_index = object_index; - ses->client_channel.send(0x60, 0x00, &cmd0B, sizeof(cmd0B)); - ses->server_channel.send(0x60, 0x00, &cmd0B, sizeof(cmd0B)); } } + break; - } else if (data[0] == 0x21) { - const auto& cmd = check_size_t(data); - ses->floor = cmd.floor; - - } else if (data[0] == 0x0C) { - if (ses->config.check_flag(Client::Flag::INFINITE_HP_ENABLED)) { - send_remove_negative_conditions(ses->client_channel, ses->lobby_client_id); - send_remove_negative_conditions(ses->server_channel, ses->lobby_client_id); + case 0x06: + // On BB, the 6x06 command is blank - the server generates the actual + // Guild Card contents and sends it to the target client, so we only + // expect data here if the client isn't BB. + if (!is_v4(c->version()) && + c->login && + c->login->account->account_id != c->proxy_session->remote_guild_card_number) { + auto& cmd = msg.check_size_t(); + if (cmd.guild_card.guild_card_number == c->login->account->account_id) { + cmd.guild_card.guild_card_number = c->proxy_session->remote_guild_card_number; + modified = true; + } } + break; - } else if (data[0] == 0x2F || data[0] == 0x4B || data[0] == 0x4C) { - if (ses->config.check_flag(Client::Flag::INFINITE_HP_ENABLED)) { - send_player_stats_change(ses->client_channel, - ses->lobby_client_id, PlayerStatsChange::ADD_HP, 2550); - send_player_stats_change(ses->server_channel, - ses->lobby_client_id, PlayerStatsChange::ADD_HP, 2550); + case 0x0C: + if (c->check_flag(Client::Flag::INFINITE_HP_ENABLED)) { + co_await send_remove_negative_conditions(c); + send_remove_negative_conditions(c->proxy_session->server_channel, c->lobby_client_id); } + break; - } else if (data[0] == 0x3E) { - C_6x_movement(ses, data); + case 0x21: + c->floor = msg.check_size_t().floor; + break; - } else if (data[0] == 0x3F) { - C_6x_movement(ses, data); - - } else if (data[0] == 0x40) { - C_6x_movement(ses, data); - - } else if ((data[0] == 0x41) || (data[0] == 0x42)) { - C_6x_movement(ses, data); - - } else if (data[0] == 0x48) { - if (ses->config.check_flag(Client::Flag::INFINITE_TP_ENABLED)) { - send_player_stats_change(ses->client_channel, ses->lobby_client_id, PlayerStatsChange::ADD_TP, 255); - send_player_stats_change(ses->server_channel, ses->lobby_client_id, PlayerStatsChange::ADD_TP, 255); + case 0x2F: + case 0x4A: + case 0x4B: + case 0x4C: + if (!is_v1(c->version()) && c->check_flag(Client::Flag::INFINITE_HP_ENABLED)) { + send_player_stats_change(c->channel, c->lobby_client_id, PlayerStatsChange::ADD_HP, 2550); + send_player_stats_change(c->proxy_session->server_channel, c->lobby_client_id, PlayerStatsChange::ADD_HP, 2550); } + break; - } else if (data[0] == 0x5F) { - const auto& cmd = check_size_t(data, sizeof(G_DropItem_PC_V3_BB_6x5F)); - send_item_notification_if_needed(ses->require_server_state(), ses->client_channel, ses->config, cmd.item.item, true); + case 0x3E: + c->pos = msg.check_size_t().pos; + break; - } else if (data[0] == 0x60 || static_cast(data[0]) == 0xA2) { - return SC_6x60_6xA2(ses, data); + case 0x3F: + c->pos = msg.check_size_t().pos; + break; - } else if (is_ep3(ses->version()) && (data.size() > 4) && (static_cast(data[0]) == 0xB5)) { - if (data[4] == 0x38) { - ses->config.set_flag(Client::Flag::EP3_ALLOW_6xBC); - } else if (data[4] == 0x3C) { - ses->config.clear_flag(Client::Flag::EP3_ALLOW_6xBC); + case 0x40: + c->pos = msg.check_size_t().pos; + break; + + case 0x41: + c->pos = msg.check_size_t().pos; + break; + + case 0x48: + if (!is_v1(c->version()) && c->check_flag(Client::Flag::INFINITE_TP_ENABLED)) { + send_player_stats_change(c->channel, c->lobby_client_id, PlayerStatsChange::ADD_TP, 255); + send_player_stats_change(c->proxy_session->server_channel, c->lobby_client_id, PlayerStatsChange::ADD_TP, 255); } - } + break; + + case 0x5F: + send_item_notification_if_needed( + c, msg.check_size_t(sizeof(G_DropItem_PC_V3_BB_6x5F)).item.item, true); + break; + + case 0x60: + case 0xA2: + co_return co_await SC_6x60_6xA2(c, msg); + break; + + case 0xB5: + if (is_ep3(c->version()) && (msg.data.size() > 4)) { + if (msg.data[4] == 0x38) { + c->set_flag(Client::Flag::EP3_ALLOW_6xBC); + } else if (msg.data[4] == 0x3C) { + c->clear_flag(Client::Flag::EP3_ALLOW_6xBC); + } + } + break; + + default: + break; } - return HandlerResult::Type::FORWARD; + co_return modified ? HandlerResult::MODIFIED : HandlerResult::FORWARD; } -static HandlerResult C_V123_A0_A1(shared_ptr ses, uint16_t, uint32_t, string&) { - if (!ses->login) { - return HandlerResult::Type::FORWARD; - } +constexpr on_message_t C_N_6x = &C_6x; +constexpr on_message_t C_D_6x = &C_6x; +constexpr on_message_t C_P_6x = &C_6x; +constexpr on_message_t C_G_6x = &C_6x; +constexpr on_message_t C_X_6x = &C_6x; +constexpr on_message_t C_B_6x = &C_6x; - // For logged-in sessions, send them back to newserv's main menu instead of - // going to the remote server's ship/block select menu - ses->send_to_game_server(); - ses->disconnect_action = ProxyServer::LinkedSession::DisconnectAction::CLOSE_IMMEDIATELY; - return HandlerResult::Type::SUPPRESS; +static asio::awaitable C_V123_A0_A1(shared_ptr c, Channel::Message&) { + // We override Change Ship and Change Block to send the player back to the + // original server (ending the proxy session), except on BB. + c->proxy_session->server_channel->disconnect(); + co_return HandlerResult::SUPPRESS; } // Indexed as [command][version][is_from_client] static_assert(NUM_VERSIONS == 14, "Don\'t forget to update the ProxyCommands handlers table"); -static on_command_t handlers[0x100][NUM_VERSIONS][2] = { +static on_message_t handlers[0x100][NUM_VERSIONS][2] = { // clang-format off -// CMD S_PC_PATCH C S_BB_PATCH C S_DC_NTE C S_DC_V1_12_2000_PROTO C S_DC_V1 C S_DC_V2 C S_PC_NTE C S_PC_V2 C S_GC_NTE C S_GC_V3 C S_GC_EP3_NTE C S_GC_EP3 C S_XB_V3 C S_BB_V4 C -/* 00 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 01 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, -/* 02 */ {{S_V123P_02_17, nullptr}, {S_V123P_02_17, nullptr}, {nullptr, nullptr}, {S_V123P_02_17, nullptr}, {S_V123P_02_17, nullptr}, {S_V123P_02_17, nullptr}, {S_V123P_02_17, nullptr}, {S_V123P_02_17, nullptr}, {S_V123P_02_17, nullptr}, {S_V123P_02_17, nullptr}, {S_V123P_02_17, nullptr}, {S_V123P_02_17, nullptr}, {S_V123P_02_17, nullptr}, {nullptr, nullptr}}, -/* 03 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_B_03, nullptr}}, -/* 04 */ {{nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_V123_04, nullptr}, {S_V123_04, nullptr}, {S_V123_04, nullptr}, {S_V123_04, nullptr}, {S_V123_04, nullptr}, {S_V123_04, nullptr}, {S_V123_04, nullptr}, {S_V123_04, nullptr}, {S_V123_04, nullptr}, {S_V123_04, nullptr}, {nullptr, nullptr}}, -/* 05 */ {{nullptr, C_05}, {nullptr, C_05}, {nullptr, nullptr}, {nullptr, C_05}, {nullptr, C_05}, {nullptr, C_05}, {nullptr, C_05}, {nullptr, C_05}, {nullptr, C_05}, {nullptr, C_05}, {nullptr, C_05}, {nullptr, C_05}, {nullptr, C_05}, {nullptr, C_05}}, -/* 06 */ {{nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_V123_06, C_06}, {S_V123_06, C_06}, {S_V123_06, C_06}, {S_V123_06, C_06}, {S_V123_06, C_06}, {S_V123_06, C_06}, {S_V123_06, C_06}, {S_V123_06, C_06}, {S_V123_06, C_06}, {S_V123_06, C_06}, {nullptr, C_06}}, -/* 07 */ {{nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, -/* 08 */ {{nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, -/* 09 */ {{nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 0A */ {{nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 0B */ {{nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 0C */ {{nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 0D */ {{nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 0E */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, -/* 0F */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -// CMD S_PC_PATCH C S_BB_PATCH C S_DC_NTE C S_DC_V1_12_2000_PROTO C S_DC_V1 C S_DC_V2 C S_PC_NTE C S_PC_V2 C S_GC_NTE C S_GC_V3 C S_GC_EP3_NTE C S_GC_EP3 C S_XB_V3 C S_BB_V4 C -/* 10 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 11 */ {{nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, -/* 12 */ {{nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, -/* 13 */ {{nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}}, -/* 14 */ {{S_19_P_14, nullptr}, {S_19_P_14, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, -/* 15 */ {{nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 16 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, -/* 17 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_V123P_02_17, nullptr}, {S_V123P_02_17, nullptr}, {S_V123P_02_17, nullptr}, {S_V123P_02_17, nullptr}, {S_V123P_02_17, nullptr}, {S_V123P_02_17, nullptr}, {S_V123P_02_17, nullptr}, {S_V123P_02_17, nullptr}, {S_V123P_02_17, nullptr}, {S_V123P_02_17, nullptr}, {S_invalid, nullptr}}, -/* 18 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}}, -/* 19 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_19_P_14, nullptr}, {S_19_P_14, nullptr}, {S_19_P_14, nullptr}, {S_19_P_14, nullptr}, {S_19_P_14, nullptr}, {S_19_P_14, nullptr}, {S_19_P_14, nullptr}, {S_19_P_14, nullptr}, {S_19_P_14, nullptr}, {S_19_P_14, nullptr}, {S_19_P_14, nullptr}}, -/* 1A */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_V3_1A_D5, nullptr}, {S_V3_1A_D5, nullptr}, {S_V3_1A_D5, nullptr}, {S_V3_1A_D5, nullptr}, {nullptr, nullptr}}, -/* 1B */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}}, -/* 1C */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}}, -/* 1D */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_1D, C_1D}, {S_1D, C_1D}, {S_1D, C_1D}, {S_1D, C_1D}, {S_1D, C_1D}, {S_1D, C_1D}, {S_1D, C_1D}, {S_1D, C_1D}, {S_1D, C_1D}, {S_1D, C_1D}, {S_1D, C_1D}}, -/* 1E */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 1F */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, -// CMD S_PC_PATCH C S_BB_PATCH C S_DC_NTE C S_DC_V1_12_2000_PROTO C S_DC_V1 C S_DC_V2 C S_PC_NTE C S_PC_V2 C S_GC_NTE C S_GC_V3 C S_GC_EP3_NTE C S_GC_EP3 C S_XB_V3 C S_BB_V4 C -/* 20 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 21 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 22 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_B_22, nullptr}}, -/* 23 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, -/* 24 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, -/* 25 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, -/* 26 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 27 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 28 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 29 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 2A */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 2B */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 2C */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 2D */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 2E */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 2F */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -// CMD S_PC_PATCH C S_BB_PATCH C S_DC_NTE C S_DC_V1_12_2000_PROTO C S_DC_V1 C S_DC_V2 C S_PC_NTE C S_PC_V2 C S_GC_NTE C S_GC_V3 C S_GC_EP3_NTE C S_GC_EP3 C S_XB_V3 C S_BB_V4 C -/* 30 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 31 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 32 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 33 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 34 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 35 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 36 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 37 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 38 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 39 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 3A */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 3B */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 3C */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 3D */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 3E */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 3F */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -// CMD S_PC_PATCH C S_BB_PATCH C S_DC_NTE C S_DC_V1_12_2000_PROTO C S_DC_V1 C S_DC_V2 C S_PC_NTE C S_PC_V2 C S_GC_NTE C S_GC_V3 C S_GC_EP3_NTE C S_GC_EP3 C S_XB_V3 C S_BB_V4 C -/* 40 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, C_40}, {S_invalid, C_40}, {S_invalid, C_40}, {S_invalid, C_40}, {S_invalid, C_40}, {S_invalid, C_40}, {S_invalid, C_40}, {S_invalid, C_40}, {S_invalid, C_40}, {S_invalid, C_40}, {S_invalid, C_40}}, -/* 41 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_DGX_41, nullptr}, {S_DGX_41, nullptr}, {S_DGX_41, nullptr}, {S_P_41, nullptr}, {S_P_41, nullptr}, {S_DGX_41, nullptr}, {S_DGX_41, nullptr}, {S_DGX_41, nullptr}, {S_DGX_41, nullptr}, {S_DGX_41, nullptr}, {S_B_41, nullptr}}, -/* 42 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 43 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 44 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_D_44_A6, nullptr}, {S_D_44_A6, nullptr}, {S_D_44_A6, nullptr}, {S_PG_44_A6, nullptr}, {S_PG_44_A6, nullptr}, {S_D_44_A6, nullptr}, {S_PG_44_A6, nullptr}, {S_PG_44_A6, nullptr}, {S_PG_44_A6, nullptr}, {S_X_44_A6, nullptr}, {S_B_44_A6, nullptr}}, -/* 45 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 46 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 47 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 48 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 49 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 4A */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 4B */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 4C */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 4D */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 4E */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 4F */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -// CMD S_PC_PATCH C S_BB_PATCH C S_DC_NTE C S_DC_V1_12_2000_PROTO C S_DC_V1 C S_DC_V2 C S_PC_NTE C S_PC_V2 C S_GC_NTE C S_GC_V3 C S_GC_EP3_NTE C S_GC_EP3 C S_XB_V3 C S_BB_V4 C -/* 50 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 51 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 52 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 53 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 54 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 55 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 56 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 57 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 58 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 59 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 5A */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 5B */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 5C */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 5D */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 5E */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 5F */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -// CMD S_PC_PATCH C S_BB_PATCH C S_DC_NTE C S_DC_V1_12_2000_PROTO C S_DC_V1 C S_DC_V2 C S_PC_NTE C S_PC_V2 C S_GC_NTE C S_GC_V3 C S_GC_EP3_NTE C S_GC_EP3 C S_XB_V3 C S_BB_V4 C -/* 60 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_6x, C_D_6x}, {S_6x, C_D_6x}, {S_6x, C_D_6x}, {S_6x, C_P_6x}, {S_6x, C_P_6x}, {S_6x, C_D_6x}, {S_6x, C_G_6x}, {S_6x, C_G_6x}, {S_6x, C_G_6x}, {S_6x, C_X_6x}, {S_6x, C_B_6x}}, -/* 61 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, C_GXB_61}, {S_invalid, C_GXB_61}, {S_invalid, C_GXB_61}, {S_invalid, C_GXB_61}, {S_invalid, C_GXB_61}}, -/* 62 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_6x, C_D_6x}, {S_6x, C_D_6x}, {S_6x, C_D_6x}, {S_6x, C_P_6x}, {S_6x, C_P_6x}, {S_6x, C_D_6x}, {S_6x, C_G_6x}, {S_6x, C_G_6x}, {S_6x, C_G_6x}, {S_6x, C_X_6x}, {S_6x, C_B_6x}}, -/* 63 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 64 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_D_64, nullptr}, {S_D_64, nullptr}, {S_D_64, nullptr}, {S_P_64, nullptr}, {S_P_64, nullptr}, {S_G_64, nullptr}, {S_G_64, nullptr}, {S_G_64, nullptr}, {S_G_64, nullptr}, {S_X_64, nullptr}, {S_B_64, nullptr}}, -/* 65 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_P_65_67_68, nullptr}, {S_P_65_67_68, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_X_65_67_68, nullptr}, {S_B_65_67_68, nullptr}}, -/* 66 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}}, -/* 67 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_P_65_67_68, nullptr}, {S_P_65_67_68, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_X_65_67_68, nullptr}, {S_B_65_67_68, nullptr}}, -/* 68 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_P_65_67_68, nullptr}, {S_P_65_67_68, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_X_65_67_68, nullptr}, {S_B_65_67_68, nullptr}}, -/* 69 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}}, -/* 6A */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 6B */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 6C */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_6x, C_D_6x}, {S_6x, C_D_6x}, {S_6x, C_D_6x}, {S_6x, C_P_6x}, {S_6x, C_P_6x}, {S_6x, C_D_6x}, {S_6x, C_G_6x}, {S_6x, C_G_6x}, {S_6x, C_G_6x}, {S_6x, C_X_6x}, {S_6x, C_B_6x}}, -/* 6D */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_6x, C_D_6x}, {S_6x, C_D_6x}, {S_6x, C_D_6x}, {S_6x, C_P_6x}, {S_6x, C_P_6x}, {S_6x, C_D_6x}, {S_6x, C_G_6x}, {S_6x, C_G_6x}, {S_6x, C_G_6x}, {S_6x, C_X_6x}, {S_6x, C_B_6x}}, -/* 6E */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 6F */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -// CMD S_PC_PATCH C S_BB_PATCH C S_DC_NTE C S_DC_V1_12_2000_PROTO C S_DC_V1 C S_DC_V2 C S_PC_NTE C S_PC_V2 C S_GC_NTE C S_GC_V3 C S_GC_EP3_NTE C S_GC_EP3 C S_XB_V3 C S_BB_V4 C -/* 70 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 71 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 72 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 73 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 74 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 75 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 76 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 77 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 78 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 79 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 7A */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 7B */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 7C */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 7D */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 7E */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 7F */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -// CMD S_PC_PATCH C S_BB_PATCH C S_DC_NTE C S_DC_V1_12_2000_PROTO C S_DC_V1 C S_DC_V2 C S_PC_NTE C S_PC_V2 C S_GC_NTE C S_GC_V3 C S_GC_EP3_NTE C S_GC_EP3 C S_XB_V3 C S_BB_V4 C -/* 80 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, -/* 81 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_DGX_81, C_DGX_81}, {S_DGX_81, C_DGX_81}, {S_DGX_81, C_DGX_81}, {S_P_81, C_P_81}, {S_P_81, C_P_81}, {S_DGX_81, C_DGX_81}, {S_DGX_81, C_DGX_81}, {S_DGX_81, C_DGX_81}, {S_DGX_81, C_DGX_81}, {S_DGX_81, C_DGX_81}, {S_B_81, C_B_81}}, -/* 82 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 83 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, -/* 84 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 85 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 86 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 87 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 88 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_88, nullptr}, {S_88, nullptr}, {S_88, nullptr}, {S_88, nullptr}, {S_88, nullptr}, {S_88, nullptr}, {S_88, nullptr}, {S_88, nullptr}, {S_88, nullptr}, {S_88, nullptr}, {S_88, nullptr}}, -/* 89 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 8A */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, -/* 8B */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 8C */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 8D */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 8E */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 8F */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -// CMD S_PC_PATCH C S_BB_PATCH C S_DC_NTE C S_DC_V1_12_2000_PROTO C S_DC_V1 C S_DC_V2 C S_PC_NTE C S_PC_V2 C S_GC_NTE C S_GC_V3 C S_GC_EP3_NTE C S_GC_EP3 C S_XB_V3 C S_BB_V4 C -/* 90 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, -/* 91 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, -/* 92 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, -/* 93 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 94 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 95 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, -/* 96 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 97 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_97, nullptr}, {S_97, nullptr}, {S_97, nullptr}, {S_97, nullptr}, {S_97, nullptr}, {S_97, nullptr}, {S_97, nullptr}, {S_97, nullptr}, {S_97, nullptr}, {S_97, nullptr}, {nullptr, nullptr}}, -/* 98 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, C_98}, {S_invalid, C_98}, {S_invalid, C_98}, {S_invalid, C_98}, {S_invalid, C_98}, {S_invalid, C_98}, {S_invalid, C_98}, {S_invalid, C_98}, {S_invalid, C_98}, {S_invalid, C_98}, {S_invalid, C_98}}, -/* 99 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 9A */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_G_9A, nullptr}, {S_G_9A, nullptr}, {S_G_9A, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, -/* 9B */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, -/* 9C */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, -/* 9D */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 9E */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, C_G_9E}, {S_invalid, C_G_9E}, {S_invalid, C_G_9E}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* 9F */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, -// CMD S_PC_PATCH C S_BB_PATCH C S_DC_NTE C S_DC_V1_12_2000_PROTO C S_DC_V1 C S_DC_V2 C S_PC_NTE C S_PC_V2 C S_GC_NTE C S_GC_V3 C S_GC_EP3_NTE C S_GC_EP3 C S_XB_V3 C S_BB_V4 C -/* A0 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, C_V123_A0_A1},{nullptr, C_V123_A0_A1},{nullptr, C_V123_A0_A1}, {nullptr, C_V123_A0_A1}, {nullptr, C_V123_A0_A1}, {nullptr, C_V123_A0_A1},{nullptr, C_V123_A0_A1}, {nullptr, C_V123_A0_A1}, {nullptr, C_V123_A0_A1}, {nullptr, C_V123_A0_A1}, {nullptr, nullptr}}, -/* A1 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, C_V123_A0_A1},{nullptr, C_V123_A0_A1},{nullptr, C_V123_A0_A1}, {nullptr, C_V123_A0_A1}, {nullptr, C_V123_A0_A1}, {nullptr, C_V123_A0_A1},{nullptr, C_V123_A0_A1}, {nullptr, C_V123_A0_A1}, {nullptr, C_V123_A0_A1}, {nullptr, C_V123_A0_A1}, {nullptr, nullptr}}, -/* A2 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, -/* A3 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, -/* A4 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, -/* A5 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, -/* A6 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_D_44_A6, nullptr}, {S_D_44_A6, nullptr}, {S_D_44_A6, nullptr}, {S_PG_44_A6, nullptr}, {S_PG_44_A6, nullptr}, {S_D_44_A6, nullptr}, {S_PG_44_A6, nullptr}, {S_PG_44_A6, nullptr}, {S_PG_44_A6, nullptr}, {S_X_44_A6, nullptr}, {S_B_44_A6, nullptr}}, -/* A7 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}}, -/* A8 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* A9 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* AA */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* AB */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, -/* AC */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_AC, nullptr}, {S_AC, nullptr}, {S_AC, nullptr}, {S_AC, nullptr}, {S_AC, nullptr}}, -/* AD */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* AE */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* AF */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -// CMD S_PC_PATCH C S_BB_PATCH C S_DC_NTE C S_DC_V1_12_2000_PROTO C S_DC_V1 C S_DC_V2 C S_PC_NTE C S_PC_V2 C S_GC_NTE C S_GC_V3 C S_GC_EP3_NTE C S_GC_EP3 C S_XB_V3 C S_BB_V4 C -/* B0 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, -/* B1 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_B1, nullptr}, {S_B1, nullptr}, {S_B1, nullptr}, {S_B1, nullptr}, {S_B1, nullptr}, {S_B1, nullptr}, {S_B1, nullptr}, {S_B1, nullptr}, {S_B1, nullptr}, {S_B1, nullptr}}, -/* B2 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_B2, nullptr}, {S_B2, nullptr}, {S_B2, nullptr}, {S_B2, nullptr}, {S_B2, nullptr}, {S_B2, nullptr}, {S_B2, nullptr}, {S_B2, nullptr}, {S_B2, nullptr}}, -/* B3 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, C_B3}, {S_invalid, C_B3}, {S_invalid, C_B3}, {S_invalid, C_B3}, {S_invalid, C_B3}, {S_invalid, C_B3}, {S_invalid, C_B3}, {S_invalid, C_B3}, {S_invalid, C_B3}}, -/* B4 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* B5 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* B6 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* B7 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_G_B7, nullptr}, {S_G_B7, nullptr}, {S_G_B7, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* B8 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_G_B8, nullptr}, {S_G_B8, nullptr}, {S_G_B8, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, -/* B9 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_G_B9, nullptr}, {S_G_B9, nullptr}, {S_G_B9, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* BA */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_G_BA, nullptr}, {S_G_BA, nullptr}, {S_G_BA, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* BB */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* BC */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* BD */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* BE */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* BF */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -// CMD S_PC_PATCH C S_BB_PATCH C S_DC_NTE C S_DC_V1_12_2000_PROTO C S_DC_V1 C S_DC_V2 C S_PC_NTE C S_PC_V2 C S_GC_NTE C S_GC_V3 C S_GC_EP3_NTE C S_GC_EP3 C S_XB_V3 C S_BB_V4 C -/* C0 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, -/* C1 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* C2 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* C3 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* C4 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_DGX_C4, nullptr}, {S_P_C4, nullptr}, {S_P_C4, nullptr}, {S_DGX_C4, nullptr}, {S_DGX_C4, nullptr}, {S_DGX_C4, nullptr}, {S_DGX_C4, nullptr}, {S_DGX_C4, nullptr}, {S_B_C4, nullptr}}, -/* C5 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, -/* C6 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* C7 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* C8 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* C9 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_6x, nullptr}, {S_6x, nullptr}, {S_6x, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* CA */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* CB */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_6x, nullptr}, {S_6x, nullptr}, {S_6x, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* CC */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* CD */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* CE */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* CF */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -// CMD S_PC_PATCH C S_BB_PATCH C S_DC_NTE C S_DC_V1_12_2000_PROTO C S_DC_V1 C S_DC_V2 C S_PC_NTE C S_PC_V2 C S_GC_NTE C S_GC_V3 C S_GC_EP3_NTE C S_GC_EP3 C S_XB_V3 C S_BB_V4 C -/* D0 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* D1 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, -/* D2 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* D3 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, -/* D4 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, -/* D5 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_V3_1A_D5, nullptr}, {S_V3_1A_D5, nullptr}, {S_V3_1A_D5, nullptr}, {S_V3_1A_D5, nullptr}, {nullptr, nullptr}}, -/* D6 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* D7 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, -/* D8 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, -/* D9 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, C_GX_D9}, {S_invalid, C_GX_D9}, {S_invalid, C_GX_D9}, {S_invalid, C_GX_D9}, {S_invalid, C_B_D9}}, -/* DA */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_V3_BB_DA, nullptr}, {S_V3_BB_DA, nullptr}, {S_V3_BB_DA, nullptr}, {S_V3_BB_DA, nullptr}, {S_V3_BB_DA, nullptr}}, -/* DB */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* DC */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, -/* DD */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, -/* DE */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, -/* DF */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -// CMD S_PC_PATCH C S_BB_PATCH C S_DC_NTE C S_DC_V1_12_2000_PROTO C S_DC_V1 C S_DC_V2 C S_PC_NTE C S_PC_V2 C S_GC_NTE C S_GC_V3 C S_GC_EP3_NTE C S_GC_EP3 C S_XB_V3 C S_BB_V4 C -/* E0 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, -/* E1 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, -/* E2 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, -/* E3 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, -/* E4 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_G_E4, nullptr}, {S_G_E4, nullptr}, {S_G_E4, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, -/* E5 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, -/* E6 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, -/* E7 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_B_E7, nullptr}}, -/* E8 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_E8, nullptr}, {S_E8, nullptr}, {S_E8, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, -/* E9 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, -/* EA */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, -/* EB */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, -/* EC */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* ED */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, -/* EE */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, -/* EF */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_G_EF, nullptr}, {S_G_EF, nullptr}, {S_G_EF, nullptr}, {S_invalid, nullptr}, {S_B_EF, nullptr}}, -// CMD S_PC_PATCH C S_BB_PATCH C S_DC_NTE C S_DC_V1_12_2000_PROTO C S_DC_V1 C S_DC_V2 C S_PC_NTE C S_PC_V2 C S_GC_NTE C S_GC_V3 C S_GC_EP3_NTE C S_GC_EP3 C S_XB_V3 C S_BB_V4 C -/* F0 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, -/* F1 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* F2 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* F3 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* F4 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* F5 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* F6 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* F7 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* F8 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* F9 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* FA */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* FB */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* FC */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* FD */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* FE */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -/* FF */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, -// CMD S_PC_PATCH C S_BB_PATCH C S_DC_NTE C S_DC_V1_12_2000_PROTO C S_DC_V1 C S_DC_V2 C S_PC_NTE C S_PC_V2 C S_GC_NTE C S_GC_V3 C S_GC_EP3_NTE C S_GC_EP3 C S_XB_V3 C S_BB_V4 C +// CMD S_PC_PATCH C S_BB_PATCH C S_DC_NTE C S_DC_12_2000 C S_DC_V1 C S_DC_V2 C S_PC_NTE C S_PC_V2 C S_GC_NTE C S_GC_V3 C S_GC_EP3_NTE C S_GC_EP3 C S_XB_V3 C S_BB_V4 C +/* 00 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 01 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, +/* 02 */ {{S_V123U_02_17, nullptr}, {S_V123U_02_17, nullptr}, {S_V123U_02_17, nullptr}, {S_V123U_02_17, nullptr}, {S_V123U_02_17, nullptr}, {S_V123U_02_17, nullptr}, {S_V123U_02_17, nullptr}, {S_V123U_02_17, nullptr}, {S_V123U_02_17, nullptr}, {S_V123U_02_17, nullptr}, {S_V123U_02_17, nullptr}, {S_V123U_02_17, nullptr}, {S_V123U_02_17, nullptr}, {nullptr, nullptr}}, +/* 03 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_B_03, nullptr}}, +/* 04 */ {{S_U_04, nullptr}, {S_U_04, nullptr}, {S_V123_04, nullptr}, {S_V123_04, nullptr}, {S_V123_04, nullptr}, {S_V123_04, nullptr}, {S_V123_04, nullptr}, {S_V123_04, nullptr}, {S_V123_04, nullptr}, {S_V123_04, nullptr}, {S_V123_04, nullptr}, {S_V123_04, nullptr}, {S_V123_04, nullptr}, {nullptr, nullptr}}, +/* 05 */ {{nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, +/* 06 */ {{nullptr, nullptr}, {nullptr, nullptr}, {S_V123_06, nullptr}, {S_V123_06, C_06}, {S_V123_06, C_06}, {S_V123_06, C_06}, {S_V123_06, C_06}, {S_V123_06, C_06}, {S_V123_06, C_06}, {S_V123_06, C_06}, {S_V123_06, C_06}, {S_V123_06, C_06}, {S_V123_06, C_06}, {nullptr, C_06}}, +/* 07 */ {{nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, +/* 08 */ {{nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, +/* 09 */ {{nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 0A */ {{nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 0B */ {{nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 0C */ {{nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 0D */ {{nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 0E */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, +/* 0F */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +// CMD S_PC_PATCH C S_BB_PATCH C S_DC_NTE C S_DC_12_2000 C S_DC_V1 C S_DC_V2 C S_PC_NTE C S_PC_V2 C S_GC_NTE C S_GC_V3 C S_GC_EP3_NTE C S_GC_EP3 C S_XB_V3 C S_BB_V4 C +/* 10 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 11 */ {{nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, +/* 12 */ {{nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, +/* 13 */ {{nullptr, nullptr}, {nullptr, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}}, +/* 14 */ {{S_19_U_14, nullptr}, {S_19_U_14, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, +/* 15 */ {{nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 16 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, +/* 17 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_V123U_02_17, nullptr}, {S_V123U_02_17, nullptr}, {S_V123U_02_17, nullptr}, {S_V123U_02_17, nullptr}, {S_V123U_02_17, nullptr}, {S_V123U_02_17, nullptr}, {S_V123U_02_17, nullptr}, {S_V123U_02_17, nullptr}, {S_V123U_02_17, nullptr}, {S_V123U_02_17, nullptr}, {S_V123U_02_17, nullptr}, {S_invalid, nullptr}}, +/* 18 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}}, +/* 19 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_19_U_14, nullptr}, {S_19_U_14, nullptr}, {S_19_U_14, nullptr}, {S_19_U_14, nullptr}, {S_19_U_14, nullptr}, {S_19_U_14, nullptr}, {S_19_U_14, nullptr}, {S_19_U_14, nullptr}, {S_19_U_14, nullptr}, {S_19_U_14, nullptr}, {S_19_U_14, nullptr}, {S_19_U_14, nullptr}}, +/* 1A */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_V3_1A_D5, nullptr}, {S_V3_1A_D5, nullptr}, {S_V3_1A_D5, nullptr}, {S_V3_1A_D5, nullptr}, {nullptr, nullptr}}, +/* 1B */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}}, +/* 1C */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}}, +/* 1D */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_1D, C_1D}, {S_1D, C_1D}, {S_1D, C_1D}, {S_1D, C_1D}, {S_1D, C_1D}, {S_1D, C_1D}, {S_1D, C_1D}, {S_1D, C_1D}, {S_1D, C_1D}, {S_1D, C_1D}, {S_1D, C_1D}, {S_1D, C_1D}}, +/* 1E */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 1F */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, +// CMD S_PC_PATCH C S_BB_PATCH C S_DC_NTE C S_DC_12_2000 C S_DC_V1 C S_DC_V2 C S_PC_NTE C S_PC_V2 C S_GC_NTE C S_GC_V3 C S_GC_EP3_NTE C S_GC_EP3 C S_XB_V3 C S_BB_V4 C +/* 20 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 21 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 22 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_B_22, nullptr}}, +/* 23 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, +/* 24 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, +/* 25 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, +/* 26 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 27 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 28 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 29 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 2A */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 2B */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 2C */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 2D */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 2E */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 2F */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +// CMD S_PC_PATCH C S_BB_PATCH C S_DC_NTE C S_DC_12_2000 C S_DC_V1 C S_DC_V2 C S_PC_NTE C S_PC_V2 C S_GC_NTE C S_GC_V3 C S_GC_EP3_NTE C S_GC_EP3 C S_XB_V3 C S_BB_V4 C +/* 30 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 31 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 32 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 33 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 34 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 35 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 36 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 37 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 38 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 39 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 3A */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 3B */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 3C */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 3D */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 3E */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 3F */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +// CMD S_PC_PATCH C S_BB_PATCH C S_DC_NTE C S_DC_12_2000 C S_DC_V1 C S_DC_V2 C S_PC_NTE C S_PC_V2 C S_GC_NTE C S_GC_V3 C S_GC_EP3_NTE C S_GC_EP3 C S_XB_V3 C S_BB_V4 C +/* 40 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, C_40}, {S_invalid, C_40}, {S_invalid, C_40}, {S_invalid, C_40}, {S_invalid, C_40}, {S_invalid, C_40}, {S_invalid, C_40}, {S_invalid, C_40}, {S_invalid, C_40}, {S_invalid, C_40}, {S_invalid, C_40}}, +/* 41 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_DGX_41, nullptr}, {S_DGX_41, nullptr}, {S_DGX_41, nullptr}, {S_DGX_41, nullptr}, {S_P_41, nullptr}, {S_P_41, nullptr}, {S_DGX_41, nullptr}, {S_DGX_41, nullptr}, {S_DGX_41, nullptr}, {S_DGX_41, nullptr}, {S_DGX_41, nullptr}, {S_B_41, nullptr}}, +/* 42 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 43 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 44 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_D_44_A6, nullptr}, {S_D_44_A6, nullptr}, {S_D_44_A6, nullptr}, {S_PG_44_A6, nullptr}, {S_PG_44_A6, nullptr}, {S_D_44_A6, nullptr}, {S_PG_44_A6, nullptr}, {S_PG_44_A6, nullptr}, {S_PG_44_A6, nullptr}, {S_X_44_A6, nullptr}, {S_B_44_A6, nullptr}}, +/* 45 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 46 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 47 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 48 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 49 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 4A */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 4B */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 4C */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 4D */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 4E */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 4F */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +// CMD S_PC_PATCH C S_BB_PATCH C S_DC_NTE C S_DC_12_2000 C S_DC_V1 C S_DC_V2 C S_PC_NTE C S_PC_V2 C S_GC_NTE C S_GC_V3 C S_GC_EP3_NTE C S_GC_EP3 C S_XB_V3 C S_BB_V4 C +/* 50 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 51 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 52 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 53 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 54 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 55 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 56 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 57 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 58 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 59 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 5A */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 5B */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 5C */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 5D */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 5E */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 5F */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +// CMD S_PC_PATCH C S_BB_PATCH C S_DC_NTE C S_DC_12_2000 C S_DC_V1 C S_DC_V2 C S_PC_NTE C S_PC_V2 C S_GC_NTE C S_GC_V3 C S_GC_EP3_NTE C S_GC_EP3 C S_XB_V3 C S_BB_V4 C +/* 60 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_6x, C_N_6x}, {S_6x, C_D_6x}, {S_6x, C_D_6x}, {S_6x, C_D_6x}, {S_6x, C_P_6x}, {S_6x, C_P_6x}, {S_6x, C_D_6x}, {S_6x, C_G_6x}, {S_6x, C_G_6x}, {S_6x, C_G_6x}, {S_6x, C_X_6x}, {S_6x, C_B_6x}}, +/* 61 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, C_GXB_61}, {S_invalid, C_GXB_61}, {S_invalid, C_GXB_61}, {S_invalid, C_GXB_61}, {S_invalid, C_GXB_61}}, +/* 62 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_6x, C_N_6x}, {S_6x, C_D_6x}, {S_6x, C_D_6x}, {S_6x, C_D_6x}, {S_6x, C_P_6x}, {S_6x, C_P_6x}, {S_6x, C_D_6x}, {S_6x, C_G_6x}, {S_6x, C_G_6x}, {S_6x, C_G_6x}, {S_6x, C_X_6x}, {S_6x, C_B_6x}}, +/* 63 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 64 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_N_64, nullptr}, {S_N_64, nullptr}, {S_D_64, nullptr}, {S_D_64, nullptr}, {S_P_64, nullptr}, {S_P_64, nullptr}, {S_G_64, nullptr}, {S_G_64, nullptr}, {S_G_64, nullptr}, {S_G_64, nullptr}, {S_X_64, nullptr}, {S_B_64, nullptr}}, +/* 65 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_N_65_67_68, nullptr}, {S_N_65_67_68, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_P_65_67_68, nullptr}, {S_P_65_67_68, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_X_65_67_68, nullptr}, {S_B_65_67_68, nullptr}}, +/* 66 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}}, +/* 67 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_N_65_67_68, nullptr}, {S_N_65_67_68, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_P_65_67_68, nullptr}, {S_P_65_67_68, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_X_65_67_68, nullptr}, {S_B_65_67_68, nullptr}}, +/* 68 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_N_65_67_68, nullptr}, {S_N_65_67_68, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_P_65_67_68, nullptr}, {S_P_65_67_68, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_X_65_67_68, nullptr}, {S_B_65_67_68, nullptr}}, +/* 69 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}}, +/* 6A */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 6B */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 6C */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_6x, C_N_6x}, {S_6x, C_D_6x}, {S_6x, C_D_6x}, {S_6x, C_D_6x}, {S_6x, C_P_6x}, {S_6x, C_P_6x}, {S_6x, C_D_6x}, {S_6x, C_G_6x}, {S_6x, C_G_6x}, {S_6x, C_G_6x}, {S_6x, C_X_6x}, {S_6x, C_B_6x}}, +/* 6D */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_6x, C_N_6x}, {S_6x, C_D_6x}, {S_6x, C_D_6x}, {S_6x, C_D_6x}, {S_6x, C_P_6x}, {S_6x, C_P_6x}, {S_6x, C_D_6x}, {S_6x, C_G_6x}, {S_6x, C_G_6x}, {S_6x, C_G_6x}, {S_6x, C_X_6x}, {S_6x, C_B_6x}}, +/* 6E */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 6F */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +// CMD S_PC_PATCH C S_BB_PATCH C S_DC_NTE C S_DC_12_2000 C S_DC_V1 C S_DC_V2 C S_PC_NTE C S_PC_V2 C S_GC_NTE C S_GC_V3 C S_GC_EP3_NTE C S_GC_EP3 C S_XB_V3 C S_BB_V4 C +/* 70 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 71 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 72 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 73 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 74 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 75 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 76 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 77 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 78 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 79 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 7A */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 7B */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 7C */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 7D */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 7E */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 7F */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +// CMD S_PC_PATCH C S_BB_PATCH C S_DC_NTE C S_DC_12_2000 C S_DC_V1 C S_DC_V2 C S_PC_NTE C S_PC_V2 C S_GC_NTE C S_GC_V3 C S_GC_EP3_NTE C S_GC_EP3 C S_XB_V3 C S_BB_V4 C +/* 80 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, +/* 81 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_DGX_81, C_DGX_81}, {S_DGX_81, C_DGX_81}, {S_DGX_81, C_DGX_81}, {S_DGX_81, C_DGX_81}, {S_P_81, C_P_81}, {S_P_81, C_P_81}, {S_DGX_81, C_DGX_81}, {S_DGX_81, C_DGX_81}, {S_DGX_81, C_DGX_81}, {S_DGX_81, C_DGX_81}, {S_DGX_81, C_DGX_81}, {S_B_81, C_B_81}}, +/* 82 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 83 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, +/* 84 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 85 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 86 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 87 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 88 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_88, nullptr}, {S_88, nullptr}, {S_88, nullptr}, {S_88, nullptr}, {S_88, nullptr}, {S_88, nullptr}, {S_88, nullptr}, {S_88, nullptr}, {S_88, nullptr}, {S_88, nullptr}, {S_88, nullptr}}, +/* 89 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 8A */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, +/* 8B */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 8C */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 8D */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 8E */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 8F */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +// CMD S_PC_PATCH C S_BB_PATCH C S_DC_NTE C S_DC_12_2000 C S_DC_V1 C S_DC_V2 C S_PC_NTE C S_PC_V2 C S_GC_NTE C S_GC_V3 C S_GC_EP3_NTE C S_GC_EP3 C S_XB_V3 C S_BB_V4 C +/* 90 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, +/* 91 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, +/* 92 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, +/* 93 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 94 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 95 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, +/* 96 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 97 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_97, nullptr}, {S_97, nullptr}, {S_97, nullptr}, {S_97, nullptr}, {S_97, nullptr}, {S_97, nullptr}, {S_97, nullptr}, {S_97, nullptr}, {S_97, nullptr}, {S_97, nullptr}, {nullptr, nullptr}}, +/* 98 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, C_98}, {S_invalid, C_98}, {S_invalid, C_98}, {S_invalid, C_98}, {S_invalid, C_98}, {S_invalid, C_98}, {S_invalid, C_98}, {S_invalid, C_98}, {S_invalid, C_98}, {S_invalid, C_98}, {S_invalid, C_98}}, +/* 99 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 9A */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_G_9A, nullptr}, {S_G_9A, nullptr}, {S_G_9A, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, +/* 9B */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, +/* 9C */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, +/* 9D */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 9E */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* 9F */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, +// CMD S_PC_PATCH C S_BB_PATCH C S_DC_NTE C S_DC_12_2000 C S_DC_V1 C S_DC_V2 C S_PC_NTE C S_PC_V2 C S_GC_NTE C S_GC_V3 C S_GC_EP3_NTE C S_GC_EP3 C S_XB_V3 C S_BB_V4 C +/* A0 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, C_V123_A0_A1},{nullptr, C_V123_A0_A1},{nullptr, C_V123_A0_A1}, {nullptr, C_V123_A0_A1}, {nullptr, C_V123_A0_A1}, {nullptr, C_V123_A0_A1},{nullptr, C_V123_A0_A1}, {nullptr, C_V123_A0_A1}, {nullptr, C_V123_A0_A1}, {nullptr, C_V123_A0_A1}, {nullptr, nullptr}}, +/* A1 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, C_V123_A0_A1},{nullptr, C_V123_A0_A1},{nullptr, C_V123_A0_A1}, {nullptr, C_V123_A0_A1}, {nullptr, C_V123_A0_A1}, {nullptr, C_V123_A0_A1},{nullptr, C_V123_A0_A1}, {nullptr, C_V123_A0_A1}, {nullptr, C_V123_A0_A1}, {nullptr, C_V123_A0_A1}, {nullptr, nullptr}}, +/* A2 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, +/* A3 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, +/* A4 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, +/* A5 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, +/* A6 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_D_44_A6, nullptr}, {S_D_44_A6, nullptr}, {S_D_44_A6, nullptr}, {S_PG_44_A6, nullptr}, {S_PG_44_A6, nullptr}, {S_D_44_A6, nullptr}, {S_PG_44_A6, nullptr}, {S_PG_44_A6, nullptr}, {S_PG_44_A6, nullptr}, {S_X_44_A6, nullptr}, {S_B_44_A6, nullptr}}, +/* A7 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}, {S_13_A7, nullptr}}, +/* A8 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* A9 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* AA */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* AB */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, +/* AC */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_AC, nullptr}, {S_AC, nullptr}, {S_AC, nullptr}, {S_AC, nullptr}, {S_AC, nullptr}}, +/* AD */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* AE */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* AF */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +// CMD S_PC_PATCH C S_BB_PATCH C S_DC_NTE C S_DC_12_2000 C S_DC_V1 C S_DC_V2 C S_PC_NTE C S_PC_V2 C S_GC_NTE C S_GC_V3 C S_GC_EP3_NTE C S_GC_EP3 C S_XB_V3 C S_BB_V4 C +/* B0 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, +/* B1 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {S_B1, nullptr}, {S_B1, nullptr}, {S_B1, nullptr}, {S_B1, nullptr}, {S_B1, nullptr}, {S_B1, nullptr}, {S_B1, nullptr}, {S_B1, nullptr}, {S_B1, nullptr}, {S_B1, nullptr}}, +/* B2 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_B2, nullptr}, {S_B2, nullptr}, {S_B2, nullptr}, {S_B2, nullptr}, {S_B2, nullptr}, {S_B2, nullptr}, {S_B2, nullptr}, {S_B2, nullptr}, {S_B2, nullptr}}, +/* B3 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, C_B3}, {S_invalid, C_B3}, {S_invalid, C_B3}, {S_invalid, C_B3}, {S_invalid, C_B3}, {S_invalid, C_B3}, {S_invalid, C_B3}, {S_invalid, C_B3}, {S_invalid, C_B3}}, +/* B4 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* B5 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* B6 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* B7 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_G_B7, nullptr}, {S_G_B7, nullptr}, {S_G_B7, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* B8 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_G_B8, nullptr}, {S_G_B8, nullptr}, {S_G_B8, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, +/* B9 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_G_B9, nullptr}, {S_G_B9, nullptr}, {S_G_B9, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* BA */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_G_BA, nullptr}, {S_G_BA, nullptr}, {S_G_BA, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* BB */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* BC */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* BD */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* BE */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* BF */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +// CMD S_PC_PATCH C S_BB_PATCH C S_DC_NTE C S_DC_12_2000 C S_DC_V1 C S_DC_V2 C S_PC_NTE C S_PC_V2 C S_GC_NTE C S_GC_V3 C S_GC_EP3_NTE C S_GC_EP3 C S_XB_V3 C S_BB_V4 C +/* C0 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, +/* C1 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* C2 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* C3 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* C4 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_DGX_C4, nullptr}, {S_P_C4, nullptr}, {S_P_C4, nullptr}, {S_DGX_C4, nullptr}, {S_DGX_C4, nullptr}, {S_DGX_C4, nullptr}, {S_DGX_C4, nullptr}, {S_DGX_C4, nullptr}, {S_B_C4, nullptr}}, +/* C5 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, +/* C6 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* C7 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* C8 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* C9 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_6x, nullptr}, {S_6x, nullptr}, {S_6x, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* CA */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* CB */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_6x, nullptr}, {S_6x, nullptr}, {S_6x, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* CC */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* CD */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* CE */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* CF */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +// CMD S_PC_PATCH C S_BB_PATCH C S_DC_NTE C S_DC_12_2000 C S_DC_V1 C S_DC_V2 C S_PC_NTE C S_PC_V2 C S_GC_NTE C S_GC_V3 C S_GC_EP3_NTE C S_GC_EP3 C S_XB_V3 C S_BB_V4 C +/* D0 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* D1 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, +/* D2 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* D3 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, +/* D4 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, +/* D5 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_V3_1A_D5, nullptr}, {S_V3_1A_D5, nullptr}, {S_V3_1A_D5, nullptr}, {S_V3_1A_D5, nullptr}, {nullptr, nullptr}}, +/* D6 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* D7 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, +/* D8 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}, +/* D9 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, C_GX_D9}, {S_invalid, C_GX_D9}, {S_invalid, C_GX_D9}, {S_invalid, C_GX_D9}, {S_invalid, C_B_D9}}, +/* DA */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_V3_BB_DA, nullptr}, {S_V3_BB_DA, nullptr}, {S_V3_BB_DA, nullptr}, {S_V3_BB_DA, nullptr}, {S_V3_BB_DA, nullptr}}, +/* DB */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* DC */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, +/* DD */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, +/* DE */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, +/* DF */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +// CMD S_PC_PATCH C S_BB_PATCH C S_DC_NTE C S_DC_12_2000 C S_DC_V1 C S_DC_V2 C S_PC_NTE C S_PC_V2 C S_GC_NTE C S_GC_V3 C S_GC_EP3_NTE C S_GC_EP3 C S_XB_V3 C S_BB_V4 C +/* E0 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {nullptr, C_B_E0}}, +/* E1 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, +/* E2 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_B_E2, nullptr}}, +/* E3 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, +/* E4 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_G_E4, nullptr}, {S_G_E4, nullptr}, {S_G_E4, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, +/* E5 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, +/* E6 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_B_E6, nullptr}}, +/* E7 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {S_B_E7, nullptr}}, +/* E8 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_E8, nullptr}, {S_E8, nullptr}, {S_E8, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, +/* E9 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_66_69_E9, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, +/* EA */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, +/* EB */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_DG_65_67_68_EB, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, +/* EC */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* ED */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, +/* EE */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, +/* EF */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_G_EF, nullptr}, {S_G_EF, nullptr}, {S_G_EF, nullptr}, {S_invalid, nullptr}, {S_B_EF, nullptr}}, +// CMD S_PC_PATCH C S_BB_PATCH C S_DC_NTE C S_DC_12_2000 C S_DC_V1 C S_DC_V2 C S_PC_NTE C S_PC_V2 C S_GC_NTE C S_GC_V3 C S_GC_EP3_NTE C S_GC_EP3 C S_XB_V3 C S_BB_V4 C +/* F0 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {nullptr, nullptr}}, +/* F1 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* F2 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* F3 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* F4 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* F5 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* F6 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* F7 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* F8 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* F9 */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* FA */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* FB */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* FC */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* FD */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* FE */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +/* FF */ {{S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}, {S_invalid, nullptr}}, +// CMD S_PC_PATCH C S_BB_PATCH C S_DC_NTE C S_DC_12_2000 C S_DC_V1 C S_DC_V2 C S_PC_NTE C S_PC_V2 C S_GC_NTE C S_GC_V3 C S_GC_EP3_NTE C S_GC_EP3 C S_XB_V3 C S_BB_V4 C // clang-format on }; -static on_command_t get_handler(Version version, bool from_server, uint8_t command) { +static on_message_t get_handler(Version version, bool from_server, uint8_t command) { size_t version_index = static_cast(version); if (version_index >= sizeof(handlers[0]) / sizeof(handlers[0][0])) { throw logic_error("invalid game version on proxy server"); @@ -2410,40 +2295,59 @@ static on_command_t get_handler(Version version, bool from_server, uint8_t comma return ret ? ret : default_handler; } -void on_proxy_command( - shared_ptr ses, - bool from_server, - uint16_t command, - uint32_t flag, - string& data) { +asio::awaitable on_proxy_command(shared_ptr c, bool from_server, unique_ptr msg) { + auto fn = get_handler(c->version(), from_server, msg->command); try { - auto fn = get_handler(ses->version(), from_server, command); - auto res = fn(ses, command, flag, data); - if (res.type == HandlerResult::Type::FORWARD) { - forward_command(ses, !from_server, command, flag, data, false); - } else if (res.type == HandlerResult::Type::MODIFIED) { - ses->log.info("The preceding command from the %s was modified in transit", - from_server ? "server" : "client"); - forward_command( - ses, - !from_server, - res.new_command >= 0 ? res.new_command : command, - res.new_flag >= 0 ? res.new_flag : flag, - data); - } else if (res.type == HandlerResult::Type::SUPPRESS) { - ses->log.info("The preceding command from the %s was not forwarded", - from_server ? "server" : "client"); + auto res = co_await fn(c, *msg); + if (res == HandlerResult::FORWARD) { + forward_command(c, !from_server, *msg, false); + } else if (res == HandlerResult::MODIFIED) { + c->log.info_f("The preceding command from the {} was modified in transit", from_server ? "server" : "client"); + forward_command(c, !from_server, *msg); + } else if (res == HandlerResult::SUPPRESS) { + c->log.info_f("The preceding command from the {} was not forwarded", from_server ? "server" : "client"); } else { throw logic_error("invalid handler result"); } } catch (const exception& e) { - ses->log.error("Failed to process command: %s", e.what()); - if (from_server) { - string error_str = "Error: "; - error_str += e.what(); - ses->send_to_game_server(error_str.c_str()); - } else { - ses->disconnect(); + c->log.error_f("Error in proxy command handler: {}", e.what()); + if (c->proxy_session && c->proxy_session->server_channel) { + c->proxy_session->server_channel->disconnect(); } } } + +asio::awaitable handle_proxy_server_commands(shared_ptr c, shared_ptr ses, shared_ptr channel) { + std::string error_str; + // server_channel can be changed by receiving a 19 command, hence the + // exception handler is inside the loop here + while ((c->proxy_session == ses) && (ses->server_channel == channel) && channel->connected()) { + unique_ptr msg; + try { + msg = make_unique(co_await channel->recv()); + if (c->proxy_session == ses) { + asio::co_spawn(co_await asio::this_coro::executor, on_proxy_command(c, true, std::move(msg)), asio::detached); + } + } catch (const std::system_error& e) { + c->log.info_f("Error in proxy server channel handler (command {:04X}): {}", msg ? msg->command : 0, e.what()); + const auto& ec = e.code(); + if (ec == asio::error::eof || ec == asio::error::connection_reset) { + error_str = "Server channel\ndisconnected"; + } else if (ec == asio::error::operation_aborted) { + // This happens when the player chooses Change Ship/Change Block, so we + // don't show an error message + } else { + error_str = e.what(); + } + channel->disconnect(); + } catch (const exception& e) { + c->log.info_f("Error in proxy server channel handler (command {:04X}): {}", msg ? msg->command : 0, e.what()); + error_str = e.what(); + channel->disconnect(); + } + } + if (c->proxy_session == ses && ses->server_channel == channel) { + c->log.info_f("Ending proxy session"); + co_await end_proxy_session(c, error_str); + } +} diff --git a/src/ProxyCommands.hh b/src/ProxyCommands.hh index 0f007ed4..116d4460 100644 --- a/src/ProxyCommands.hh +++ b/src/ProxyCommands.hh @@ -1,15 +1,6 @@ #pragma once -#include +#include "Client.hh" -#include - -#include "ProxyServer.hh" -#include "ServerState.hh" - -void on_proxy_command( - std::shared_ptr ses, - bool from_server, - uint16_t command, - uint32_t flag, - std::string& data); +asio::awaitable on_proxy_command(std::shared_ptr c, bool from_server, std::unique_ptr msg); +asio::awaitable handle_proxy_server_commands(std::shared_ptr c, std::shared_ptr ses, std::shared_ptr channel); diff --git a/src/ProxyServer.cc b/src/ProxyServer.cc deleted file mode 100644 index 65767a67..00000000 --- a/src/ProxyServer.cc +++ /dev/null @@ -1,1047 +0,0 @@ -#include "ProxyServer.hh" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "IPStackSimulator.hh" -#include "Loggers.hh" -#include "NetworkAddresses.hh" -#include "PSOProtocol.hh" -#include "ProxyCommands.hh" -#include "ReceiveCommands.hh" -#include "ReceiveSubcommands.hh" -#include "SendCommands.hh" - -using namespace std; -using namespace std::placeholders; - -ProxyServer::ProxyServer( - shared_ptr base, - shared_ptr state) - : base(base), - destroy_sessions_ev(event_new(this->base.get(), -1, EV_TIMEOUT, &ProxyServer::dispatch_destroy_sessions, this), event_free), - state(state), - next_unlinked_session_id(this->FIRST_UNLINKED_SESSION_ID), - next_logged_out_session_id(this->FIRST_LINKED_LOGGED_OUT_SESSION_ID) {} - -void ProxyServer::listen(const std::string& addr, uint16_t port, Version version, const struct sockaddr_storage* default_destination) { - auto socket_obj = make_shared(this, addr, port, version, default_destination); - if (!this->listeners.emplace(port, socket_obj).second) { - throw runtime_error("duplicate port in proxy server configuration"); - } -} - -ProxyServer::ListeningSocket::ListeningSocket( - ProxyServer* server, - const std::string& addr, - uint16_t port, - Version version, - const struct sockaddr_storage* default_destination) - : server(server), - log(phosg::string_printf("[ProxyServer:T-%hu] ", port), proxy_server_log.min_level), - port(port), - fd(phosg::listen(addr, port, SOMAXCONN)), - listener(nullptr, evconnlistener_free), - version(version) { - if (!this->fd.is_open()) { - throw runtime_error("cannot listen on port"); - } - this->listener.reset(evconnlistener_new( - this->server->base.get(), - &ProxyServer::ListeningSocket::dispatch_on_listen_accept, - this, - LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE, - 0, - this->fd)); - if (!listener) { - throw runtime_error("cannot create listener"); - } - evconnlistener_set_error_cb( - this->listener.get(), &ProxyServer::ListeningSocket::dispatch_on_listen_error); - - if (default_destination) { - this->default_destination = *default_destination; - } else { - this->default_destination.ss_family = 0; - } - - this->log.info("Listening on TCP port %hu (%s) on fd %d", this->port, phosg::name_for_enum(this->version), static_cast(this->fd)); -} - -void ProxyServer::ListeningSocket::dispatch_on_listen_accept( - struct evconnlistener*, evutil_socket_t fd, struct sockaddr*, int, void* ctx) { - reinterpret_cast(ctx)->on_listen_accept(fd); -} - -void ProxyServer::ListeningSocket::dispatch_on_listen_error( - struct evconnlistener*, void* ctx) { - reinterpret_cast(ctx)->on_listen_error(); -} - -void ProxyServer::ListeningSocket::on_listen_accept(int fd) { - struct sockaddr_storage remote_addr; - phosg::get_socket_addresses(fd, nullptr, &remote_addr); - if (this->server->state->banned_ipv4_ranges->check(remote_addr)) { - close(fd); - return; - } - - this->log.info("Client connected on fd %d (port %hu, version %s)", fd, this->port, phosg::name_for_enum(this->version)); - auto* bev = bufferevent_socket_new(this->server->base.get(), fd, BEV_OPT_CLOSE_ON_FREE | BEV_OPT_DEFER_CALLBACKS); - this->server->on_client_connect(bev, 0, this->port, this->version, - (this->default_destination.ss_family == AF_INET) ? &this->default_destination : nullptr); -} - -void ProxyServer::ListeningSocket::on_listen_error() { - int err = EVUTIL_SOCKET_ERROR(); - this->log.error("Failure on listening socket %d: %d (%s)", - evconnlistener_get_fd(this->listener.get()), - err, evutil_socket_error_to_string(err)); - event_base_loopexit(this->server->base.get(), nullptr); -} - -void ProxyServer::connect_virtual_client(struct bufferevent* bev, uint64_t virtual_network_id, uint16_t server_port) { - // Look up the listening socket for the given port, and use that game version. - // We don't support default-destination proxying for virtual connections (yet) - Version version; - try { - version = this->listeners.at(server_port)->version; - } catch (const out_of_range&) { - proxy_server_log.info("Virtual connection received on unregistered port %hu; closing it", server_port); - bufferevent_flush(bev, EV_READ | EV_WRITE, BEV_FINISHED); - bufferevent_free(bev); - return; - } - - proxy_server_log.info("Client connected on virtual connection %p (port %hu)", bev, server_port); - this->on_client_connect(bev, virtual_network_id, server_port, version, nullptr); -} - -void ProxyServer::on_client_connect( - struct bufferevent* bev, - uint64_t virtual_network_id, - uint16_t listen_port, - Version version, - const struct sockaddr_storage* default_destination) { - // If a default destination exists for this client and the client is a patch - // client, create a linked session immediately and connect to the remote - // server. This creates a direct session. - if (default_destination && is_patch(version)) { - uint64_t session_id = this->next_logged_out_session_id++; - if (this->next_logged_out_session_id == this->FIRST_LINKED_LOGGED_OUT_SESSION_ID) { - this->next_logged_out_session_id = this->FIRST_LINKED_LOGGED_OUT_SESSION_ID; - } - - auto emplace_ret = this->id_to_linked_session.emplace(session_id, make_shared(this->shared_from_this(), session_id, listen_port, version, *default_destination)); - if (!emplace_ret.second) { - throw logic_error("linked session already exists for logged-out client"); - } - auto ses = emplace_ret.first->second; - ses->log.info("Opened linked session"); - - Channel ch(bev, virtual_network_id, version, 1, nullptr, nullptr, ses.get(), "", phosg::TerminalFormat::FG_YELLOW, phosg::TerminalFormat::FG_GREEN); - ses->resume(std::move(ch)); - - } else { - // If no default destination exists, or the client is not a patch client, - // create an unlinked session - we'll have to get the destination from the - // client's config, which we'll get via a 9E command soon. - uint64_t session_id = this->next_unlinked_session_id++; - if (this->next_unlinked_session_id == this->FIRST_LINKED_LOGGED_OUT_SESSION_ID) { - this->next_unlinked_session_id = this->FIRST_UNLINKED_SESSION_ID; - } - - auto emplace_ret = this->id_to_unlinked_session.emplace( - session_id, make_shared(this->shared_from_this(), session_id, bev, virtual_network_id, listen_port, version)); - if (!emplace_ret.second) { - throw logic_error("stale unlinked session exists"); - } - auto ses = emplace_ret.first->second; - proxy_server_log.info("Opened unlinked session"); - - // Note that this should only be set when the linked session is created, not - // when it is resumed! - if (default_destination) { - ses->next_destination = *default_destination; - } - - switch (version) { - case Version::PC_PATCH: - case Version::BB_PATCH: - throw logic_error("cannot create unlinked patch session"); - case Version::DC_NTE: - case Version::DC_11_2000: - case Version::DC_V1: - case Version::DC_V2: - case Version::PC_NTE: - case Version::PC_V2: - case Version::GC_NTE: - case Version::GC_V3: - case Version::GC_EP3_NTE: - case Version::GC_EP3: - case Version::XB_V3: { - uint32_t server_key = phosg::random_object(); - uint32_t client_key = phosg::random_object(); - auto cmd = prepare_server_init_contents_console(server_key, client_key, 0); - ses->channel.send(0x02, 0x00, &cmd, sizeof(cmd)); - if (uses_v2_encryption(version)) { - ses->channel.crypt_out = make_shared(server_key); - ses->channel.crypt_in = make_shared(client_key); - } else { - ses->channel.crypt_out = make_shared(server_key); - ses->channel.crypt_in = make_shared(client_key); - } - break; - } - case Version::BB_V4: { - parray server_key; - parray client_key; - phosg::random_data(server_key.data(), server_key.bytes()); - phosg::random_data(client_key.data(), client_key.bytes()); - auto cmd = prepare_server_init_contents_bb(server_key, client_key, 0); - ses->channel.send(0x03, 0x00, &cmd, sizeof(cmd)); - ses->detector_crypt = make_shared( - this->state->bb_private_keys, - bb_crypt_initial_client_commands, - cmd.basic_cmd.client_key.data(), - sizeof(cmd.basic_cmd.client_key)); - ses->channel.crypt_in = ses->detector_crypt; - ses->channel.crypt_out = make_shared( - ses->detector_crypt, - cmd.basic_cmd.server_key.data(), - sizeof(cmd.basic_cmd.server_key), - true); - break; - } - default: - throw logic_error("unsupported game version on proxy server"); - } - } -} - -ProxyServer::UnlinkedSession::UnlinkedSession( - shared_ptr server, - uint64_t id, - struct bufferevent* bev, - uint64_t virtual_network_id, - uint16_t local_port, - Version version) - : server(server), - id(id), - log(phosg::string_printf("[ProxyServer:US-%" PRIX64 "] ", this->id), proxy_server_log.min_level), - channel( - bev, - virtual_network_id, - version, - 1, - ProxyServer::UnlinkedSession::on_input, - ProxyServer::UnlinkedSession::on_error, - this, - "", - phosg::TerminalFormat::FG_YELLOW, - phosg::TerminalFormat::FG_GREEN), - local_port(local_port) { - string ip_str = server->state->format_address_for_channel_name(this->channel.remote_addr, this->channel.virtual_network_id); - this->channel.name = phosg::string_printf("US-%" PRIX64 " @ %s", this->id, ip_str.c_str()); - memset(&this->next_destination, 0, sizeof(this->next_destination)); -} - -std::shared_ptr ProxyServer::UnlinkedSession::require_server() const { - auto server = this->server.lock(); - if (!server) { - throw logic_error("server is deleted"); - } - return server; -} - -std::shared_ptr ProxyServer::UnlinkedSession::require_server_state() const { - return this->require_server()->state; -} - -void ProxyServer::UnlinkedSession::set_login(std::shared_ptr login) { - this->login = login; - if (this->log.should_log(phosg::LogLevel::INFO)) { - string login_str = this->login->str(); - this->log.info("Login: %s", login_str.c_str()); - } -} - -void ProxyServer::UnlinkedSession::on_input(Channel& ch, uint16_t command, uint32_t, std::string& data) { - auto* ses = reinterpret_cast(ch.context_obj); - auto server = ses->require_server(); - auto s = server->state; - - bool should_close_unlinked_session = false; - - try { - switch (ses->version()) { - case Version::DC_NTE: - case Version::DC_11_2000: - case Version::DC_V1: - case Version::DC_V2: - case Version::GC_NTE: - // We should only get an 8B, 93 or 9D while the session is unlinked - if (command == 0x8B) { - ses->channel.version = Version::DC_NTE; - ses->log.info("Version changed to DC_NTE"); - ses->config.specific_version = SPECIFIC_VERSION_DC_NTE; - const auto& cmd = check_size_t(data, sizeof(C_LoginExtended_DCNTE_8B)); - ses->set_login(s->account_index->from_dc_nte_credentials( - cmd.serial_number.decode(), cmd.access_key.decode(), false)); - ses->sub_version = cmd.sub_version; - ses->channel.language = cmd.language; - ses->character_name = cmd.name.decode(ses->channel.language); - ses->hardware_id = cmd.hardware_id; - - } else if (command == 0x93) { // 11/2000 proto through DC V1 - ses->channel.version = Version::DC_V1; - ses->log.info("Version changed to DC_V1"); - if (specific_version_is_indeterminate(ses->config.specific_version)) { - ses->config.specific_version = SPECIFIC_VERSION_DC_V1_INDETERMINATE; - } - const auto& cmd = check_size_t(data); - ses->set_login(s->account_index->from_dc_credentials( - stoul(cmd.serial_number.decode(), nullptr, 16), cmd.access_key.decode(), cmd.name.decode(), false)); - ses->sub_version = cmd.sub_version; - ses->channel.language = cmd.language; - ses->character_name = cmd.name.decode(ses->channel.language); - ses->serial_number2 = cmd.serial_number2.decode(); - ses->hardware_id = cmd.hardware_id; - - } else if (command == 0x9D) { - const auto& cmd = check_size_t(data, sizeof(C_LoginExtended_DC_GC_9D)); - if (cmd.sub_version >= 0x30) { - ses->log.info("Version changed to GC_NTE"); - ses->channel.version = Version::GC_NTE; - ses->config.specific_version = SPECIFIC_VERSION_GC_NTE; - ses->set_login(s->account_index->from_gc_credentials( - stoul(cmd.serial_number.decode(), nullptr, 16), cmd.access_key.decode(), nullptr, cmd.name.decode(), false)); - } else { // DC V2 - ses->log.info("Version changed to DC_V2"); - ses->channel.version = Version::DC_V2; - if (specific_version_is_indeterminate(ses->config.specific_version)) { - ses->config.specific_version = SPECIFIC_VERSION_DC_V2_INDETERMINATE; - } - ses->set_login(s->account_index->from_dc_credentials( - stoul(cmd.serial_number.decode(), nullptr, 16), cmd.access_key.decode(), cmd.name.decode(), false)); - } - ses->sub_version = cmd.sub_version; - ses->channel.language = cmd.language; - ses->character_name = cmd.name.decode(ses->channel.language); - ses->config.set_flags_for_version(ses->version(), cmd.sub_version); - ses->hardware_id = cmd.hardware_id; - - } else { - throw runtime_error("command is not 93 or 9D"); - } - break; - - case Version::PC_NTE: - case Version::PC_V2: { - // We should only get a 9D while the session is unlinked - if (command != 0x9D) { - throw runtime_error("command is not 9D"); - } - const auto& cmd = check_size_t(data, sizeof(C_LoginExtended_PC_9D)); - ses->set_login(s->account_index->from_pc_credentials( - stoul(cmd.serial_number.decode(), nullptr, 16), cmd.access_key.decode(), cmd.name.decode(), false)); - ses->sub_version = cmd.sub_version; - ses->channel.language = cmd.language; - ses->character_name = cmd.name.decode(ses->channel.language); - ses->hardware_id = cmd.hardware_id; - break; - } - - case Version::GC_V3: - case Version::GC_EP3_NTE: - case Version::GC_EP3: - // We should only get a 9E while the session is unlinked - if (command == 0x9E) { - const auto& cmd = check_size_t(data, sizeof(C_LoginExtended_GC_9E)); - ses->set_login(s->account_index->from_gc_credentials( - stoul(cmd.serial_number.decode(), nullptr, 16), cmd.access_key.decode(), nullptr, cmd.name.decode(), false)); - ses->sub_version = cmd.sub_version; - ses->channel.language = cmd.language; - ses->character_name = cmd.name.decode(ses->channel.language); - ses->hardware_id = cmd.hardware_id; - ses->config.parse_from(cmd.client_config); - if (cmd.sub_version >= 0x40) { - ses->log.info("Version changed to GC_EP3"); - ses->channel.version = Version::GC_EP3; - if (specific_version_is_indeterminate(ses->config.specific_version)) { - ses->config.specific_version = SPECIFIC_VERSION_GC_EP3_INDETERMINATE; - } - } - } else { - throw runtime_error("command is not 9D or 9E"); - } - break; - - case Version::XB_V3: - // We should only get a 9E or 9F while the session is unlinked - if (command == 0x9E) { - const auto& cmd = check_size_t(data, sizeof(C_LoginExtended_XB_9E)); - string xb_gamertag = cmd.serial_number.decode(); - uint64_t xb_user_id = stoull(cmd.access_key.decode(), nullptr, 16); - uint64_t xb_account_id = cmd.netloc.account_id; - ses->set_login(s->account_index->from_xb_credentials(xb_gamertag, xb_user_id, xb_account_id, false)); - ses->sub_version = cmd.sub_version; - ses->channel.language = cmd.language; - ses->character_name = cmd.name.decode(ses->channel.language); - ses->xb_netloc = cmd.netloc; - ses->xb_9E_unknown_a1a = cmd.unknown_a1a; - ses->hardware_id = cmd.hardware_id; - ses->channel.send(0x9F, 0x00); - return; - } else if (command == 0x9F) { - const auto& cmd = check_size_t(data); - ses->config.parse_from(cmd.data); - } else { - throw runtime_error("command is not 9E or 9F"); - } - break; - - case Version::BB_V4: { - // We should only get a 93 while the session is unlinked; if we get - // anything else, disconnect - if (command != 0x93) { - throw runtime_error("command is not 93"); - } - const auto& cmd = check_size_t(data, 0xFFFF); - string password = cmd.password.decode(); - ses->set_login(s->account_index->from_bb_credentials(cmd.username.decode(), &password, s->allow_unregistered_users)); - ses->login_command_bb = std::move(data); - break; - } - - default: - throw runtime_error("unvalid unlinked session version"); - } - } catch (const exception& e) { - ses->log.error("Failed to process command from unlinked client: %s", e.what()); - should_close_unlinked_session = true; - } - - uint64_t unlinked_session_id = ses->id; - - // If login is present, then the client has credentials and can be connected - // to the remote lobby server. - if (ses->login) { - // At this point, we will always close the unlinked session, even if it - // doesn't get converted/merged to a linked session - should_close_unlinked_session = true; - - // Look up the linked session for this account (if any) - shared_ptr linked_ses; - try { - linked_ses = server->id_to_linked_session.at(ses->login->proxy_session_id()); - linked_ses->log.info("Resuming linked session from unlinked session"); - - } catch (const out_of_range&) { - // If there's no open session for this account, then there must be a valid - // destination somewhere - either in the client config or in the unlinked - // session - if (ses->config.proxy_destination_address != 0) { - linked_ses = make_shared(server, ses->local_port, ses->version(), ses->login, ses->config); - linked_ses->log.info("Opened logged-in session for unlinked session based on client config"); - } else if (ses->next_destination.ss_family == AF_INET) { - linked_ses = make_shared(server, ses->local_port, ses->version(), ses->login, ses->next_destination); - linked_ses->log.info("Opened logged-in session for unlinked session based on unlinked default destination"); - } else { - ses->log.error("Cannot open linked session: no valid destination in client config or unlinked session"); - } - } - - if (linked_ses.get()) { - server->id_to_linked_session.emplace(linked_ses->id, linked_ses); - // Resume the linked session using the unlinked session - try { - if (ses->version() == Version::BB_V4) { - linked_ses->resume( - std::move(ses->channel), - ses->detector_crypt, - std::move(ses->login_command_bb)); - } else { - linked_ses->resume( - std::move(ses->channel), - ses->detector_crypt, - ses->sub_version, - ses->character_name, - ses->serial_number2, - ses->hardware_id, - ses->xb_netloc, - ses->xb_9E_unknown_a1a); - } - } catch (const exception& e) { - linked_ses->log.error("Failed to resume linked session: %s", e.what()); - } - } - } - - if (should_close_unlinked_session) { - server->delete_session(unlinked_session_id); - } -} - -void ProxyServer::UnlinkedSession::on_error(Channel& ch, short events) { - auto* ses = reinterpret_cast(ch.context_obj); - - if (events & BEV_EVENT_ERROR) { - int err = EVUTIL_SOCKET_ERROR(); - ses->log.warning("Error %d (%s) in unlinked client stream", err, - evutil_socket_error_to_string(err)); - } - if (events & (BEV_EVENT_ERROR | BEV_EVENT_EOF)) { - ses->log.info("Client has disconnected"); - ses->require_server()->delete_session(ses->id); - } -} - -ProxyServer::LinkedSession::LinkedSession( - shared_ptr server, - uint64_t id, - uint16_t local_port, - Version version) - : server(server), - id(id), - log(phosg::string_printf("[ProxyServer:LS-%016" PRIX64 "] ", this->id), proxy_server_log.min_level), - timeout_event(event_new(server->base.get(), -1, EV_TIMEOUT, &LinkedSession::dispatch_on_timeout, this), event_free), - login(nullptr), - client_channel( - version, - 1, - nullptr, - nullptr, - this, - phosg::string_printf("LS-%016" PRIX64 "-C", this->id), - phosg::TerminalFormat::FG_YELLOW, - phosg::TerminalFormat::FG_GREEN), - server_channel( - version, - 1, - nullptr, - nullptr, - this, - phosg::string_printf("LS-%016" PRIX64 "-S", this->id), - phosg::TerminalFormat::FG_YELLOW, - phosg::TerminalFormat::FG_RED), - local_port(local_port), - disconnect_action(DisconnectAction::LONG_TIMEOUT), - remote_ip_crc(0), - enable_remote_ip_crc_patch(false), - sub_version(0), // This is set during resume() - remote_guild_card_number(-1), - next_item_id(0x0F000000), - drop_mode(DropMode::PASSTHROUGH), - lobby_players(12), - lobby_client_id(0), - leader_client_id(0), - floor(0), - is_in_game(false), - is_in_quest(false), - lobby_event(0), - lobby_difficulty(0), - lobby_section_id(0), - lobby_mode(GameMode::NORMAL), - lobby_episode(Episode::EP1), - lobby_random_seed(0) { - memset(this->prev_server_command_bytes, 0, sizeof(this->prev_server_command_bytes)); -} - -ProxyServer::LinkedSession::LinkedSession( - shared_ptr server, - uint16_t local_port, - Version version, - shared_ptr login, - const Client::Config& config) - : LinkedSession(server, login->proxy_session_id(), local_port, version) { - this->login = login; - this->config = config; - memset(&this->next_destination, 0, sizeof(this->next_destination)); - struct sockaddr_in* dest_sin = reinterpret_cast(&this->next_destination); - dest_sin->sin_family = AF_INET; - dest_sin->sin_port = htons(this->config.proxy_destination_port); - dest_sin->sin_addr.s_addr = htonl(this->config.proxy_destination_address); -} - -ProxyServer::LinkedSession::LinkedSession( - shared_ptr server, - uint16_t local_port, - Version version, - std::shared_ptr login, - const struct sockaddr_storage& next_destination) - : LinkedSession(server, login->proxy_session_id(), local_port, version) { - this->login = login; - this->next_destination = next_destination; -} - -ProxyServer::LinkedSession::LinkedSession( - shared_ptr server, - uint64_t id, - uint16_t local_port, - Version version, - const struct sockaddr_storage& destination) - : LinkedSession(server, id, local_port, version) { - this->next_destination = destination; -} - -shared_ptr ProxyServer::LinkedSession::require_server() const { - auto server = this->server.lock(); - if (!server) { - throw logic_error("server is deleted"); - } - return server; -} - -std::shared_ptr ProxyServer::LinkedSession::require_server_state() const { - return this->require_server()->state; -} - -void ProxyServer::LinkedSession::set_version(Version v) { - this->client_channel.version = v; - this->server_channel.version = v; -} - -void ProxyServer::LinkedSession::resume( - Channel&& client_channel, - shared_ptr detector_crypt, - uint32_t sub_version, - const string& character_name, - const string& serial_number2, - uint64_t hardware_id, - const XBNetworkLocation& xb_netloc, - const parray& xb_9E_unknown_a1a) { - this->sub_version = sub_version; - this->character_name = character_name; - this->serial_number2 = serial_number2; - this->hardware_id = hardware_id; - this->xb_netloc = xb_netloc; - this->xb_9E_unknown_a1a = xb_9E_unknown_a1a; - this->resume_inner(std::move(client_channel), detector_crypt); -} - -void ProxyServer::LinkedSession::resume( - Channel&& client_channel, - shared_ptr detector_crypt, - string&& login_command_bb) { - this->login_command_bb = std::move(login_command_bb); - this->resume_inner(std::move(client_channel), detector_crypt); -} - -void ProxyServer::LinkedSession::resume(Channel&& client_channel) { - this->sub_version = 0; - this->character_name.clear(); - this->resume_inner(std::move(client_channel), nullptr); -} - -void ProxyServer::LinkedSession::resume_inner( - Channel&& client_channel, - shared_ptr detector_crypt) { - if (this->client_channel.connected()) { - throw runtime_error("client connection is already open for this session"); - } - if (this->next_destination.ss_family != AF_INET) { - throw logic_error("attempted to resume an logged-out linked session without destination set"); - } - - auto s = this->server.lock(); - if (!s) { - throw logic_error("ProxyServer is missing during LinkedSession resume"); - } - - this->client_channel.replace_with( - std::move(client_channel), - ProxyServer::LinkedSession::on_input, - ProxyServer::LinkedSession::on_error, - this, - ""); - this->server_channel.language = this->client_channel.language; - this->server_channel.version = this->client_channel.version; - - this->detector_crypt = detector_crypt; - this->server_channel.disconnect(); - this->saving_files.clear(); - - this->connect(); -} - -void ProxyServer::LinkedSession::connect() { - // Connect to the remote server. The command handlers will do the login steps - // and set up forwarding - const struct sockaddr_in* dest_sin = reinterpret_cast( - &this->next_destination); - if (dest_sin->sin_family != AF_INET) { - throw runtime_error("destination is not AF_INET"); - } - - string netloc_str = phosg::render_sockaddr_storage(this->next_destination); - this->log.info("Connecting to %s", netloc_str.c_str()); - - this->server_channel.set_bufferevent( - bufferevent_socket_new(this->require_server()->base.get(), -1, BEV_OPT_CLOSE_ON_FREE | BEV_OPT_DEFER_CALLBACKS), 0); - if (bufferevent_socket_connect(this->server_channel.bev.get(), - reinterpret_cast(dest_sin), sizeof(*dest_sin)) != 0) { - throw runtime_error(phosg::string_printf("failed to connect (%d)", EVUTIL_SOCKET_ERROR())); - } - - this->server_channel.on_command_received = ProxyServer::LinkedSession::on_input; - this->server_channel.on_error = ProxyServer::LinkedSession::on_error; - this->server_channel.context_obj = this; - - this->update_channel_names(); - - // Cancel the session delete timeout - event_del(this->timeout_event.get()); -} - -void ProxyServer::LinkedSession::update_channel_names() { - auto s = this->require_server_state(); - auto client_ip_str = s->format_address_for_channel_name( - this->client_channel.remote_addr, this->client_channel.virtual_network_id); - auto server_ip_str = s->format_address_for_channel_name(this->server_channel.remote_addr, 0); - this->client_channel.name = phosg::string_printf("LS-%016" PRIX64 "-C @ %s", this->id, client_ip_str.c_str()); - this->server_channel.name = phosg::string_printf("LS-%016" PRIX64 "-S @ %s", this->id, server_ip_str.c_str()); -} - -ProxyServer::LinkedSession::SavingFile::SavingFile( - const string& basename, - const string& output_filename, - size_t total_size, - bool is_download) - : basename(basename), - output_filename(output_filename), - is_download(is_download), - total_size(total_size) {} - -void ProxyServer::LinkedSession::dispatch_on_timeout(evutil_socket_t, short, void* ctx) { - reinterpret_cast(ctx)->on_timeout(); -} - -void ProxyServer::LinkedSession::on_timeout() { - this->require_server()->delete_session(this->id); -} - -void ProxyServer::LinkedSession::on_error(Channel& ch, short events) { - auto* ses = reinterpret_cast(ch.context_obj); - bool is_server_stream = (&ch == &ses->server_channel); - - if (events & BEV_EVENT_CONNECTED) { - ses->log.info("%s channel connected", is_server_stream ? "Server" : "Client"); - if (is_server_stream) { - phosg::get_socket_addresses(bufferevent_getfd(ch.bev.get()), &ch.local_addr, &ch.remote_addr); - ses->update_channel_names(); - } - - if (is_server_stream && (ses->config.override_lobby_event != 0xFF) && (is_v3(ses->version()) || is_v4(ses->version()))) { - ses->client_channel.send(0xDA, ses->config.override_lobby_event); - } - } - if (events & BEV_EVENT_ERROR) { - int err = EVUTIL_SOCKET_ERROR(); - ses->log.warning("Error %d (%s) in %s stream", - err, evutil_socket_error_to_string(err), - is_server_stream ? "server" : "client"); - } - if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) { - ses->log.info("%s has disconnected", - is_server_stream ? "Server" : "Client"); - // If the server disconnected, send the client back to the game server so - // they're not disconnected completely. - if (is_server_stream) { - ses->send_to_game_server("The server has\ndisconnected."); - } - ses->disconnect(); - } -} - -void ProxyServer::LinkedSession::clear_lobby_players(size_t num_slots) { - this->lobby_players.clear(); - this->lobby_players.resize(num_slots); - this->log.info("Cleared lobby players"); -} - -void ProxyServer::LinkedSession::set_drop_mode(DropMode new_mode) { - this->drop_mode = new_mode; - if (this->drop_mode == DropMode::INTERCEPT) { - auto s = this->require_server_state(); - auto version = this->version(); - - shared_ptr rare_item_set; - shared_ptr common_item_set; - switch (version) { - case Version::PC_PATCH: - case Version::BB_PATCH: - case Version::GC_EP3_NTE: - case Version::GC_EP3: - throw runtime_error("cannot create item creator for this base version"); - case Version::DC_NTE: - case Version::DC_11_2000: - case Version::DC_V1: - // TODO: We should probably have a v1 common item set at some point too - common_item_set = s->common_item_set_v2; - rare_item_set = s->rare_item_sets.at("rare-table-v1"); - break; - case Version::DC_V2: - case Version::PC_NTE: - case Version::PC_V2: - common_item_set = s->common_item_set_v2; - rare_item_set = s->rare_item_sets.at("rare-table-v2"); - break; - case Version::GC_NTE: - case Version::GC_V3: - case Version::XB_V3: - common_item_set = s->common_item_set_v3_v4; - rare_item_set = s->rare_item_sets.at("rare-table-v3"); - break; - case Version::BB_V4: - common_item_set = s->common_item_set_v3_v4; - rare_item_set = s->rare_item_sets.at("rare-table-v4"); - break; - default: - throw logic_error("invalid lobby base version"); - } - auto opt_rand_crypt = this->config.check_flag(Client::Flag::USE_OVERRIDE_RANDOM_SEED) - ? make_shared(this->config.override_random_seed) - : nullptr; - this->item_creator = make_shared( - common_item_set, - rare_item_set, - s->armor_random_set, - s->tool_random_set, - s->weapon_random_sets.at(this->lobby_difficulty), - s->tekker_adjustment_set, - s->item_parameter_table(version), - s->item_stack_limits(version), - this->lobby_episode, - (this->lobby_mode == GameMode::SOLO) ? GameMode::NORMAL : this->lobby_mode, - this->lobby_difficulty, - this->lobby_section_id, - opt_rand_crypt, - // TODO: Can we get battle rules here somehow? - nullptr); - } else { - this->item_creator.reset(); - } -} - -void ProxyServer::LinkedSession::send_to_game_server(const char* error_message) { - // If there is no account, do nothing - we can't return to the game server - // from logged-out sessions - if (!this->login) { - this->disconnect(); - return; - } - // On BB, do nothing - we can't return to the game server since the remote - // server likely sent different game data than what newserv would have sent - if (this->version() == Version::BB_V4) { - this->disconnect(); - return; - } - - // Delete all the other players - for (size_t x = 0; x < this->lobby_players.size(); x++) { - if (this->lobby_players[x].guild_card_number == 0) { - continue; - } - uint8_t leaving_id = x; - uint8_t leader_id = this->lobby_client_id; - S_LeaveLobby_66_69_Ep3_E9 cmd = {leaving_id, leader_id, 1, 0}; - this->client_channel.send(this->is_in_game ? 0x66 : 0x69, leaving_id, &cmd, sizeof(cmd)); - } - - auto s = this->require_server_state(); - if (this->is_in_game) { - send_ship_info(this->client_channel, phosg::string_printf("You cannot return\nto $C6%s$C7\nwhile in a game.\n\n%s", s->name.c_str(), error_message ? error_message : "")); - this->disconnect(); - - } else { - send_ship_info(this->client_channel, phosg::string_printf("You\'ve returned to\n$C6%s$C7\n\n%s", s->name.c_str(), error_message ? error_message : "")); - - // Restore newserv_client_config, so the login server gets the client flags - if (is_v3(this->version())) { - S_UpdateClientConfig_V3_04 update_client_config_cmd; - update_client_config_cmd.player_tag = 0x00010000; - update_client_config_cmd.guild_card_number = this->login->account->account_id; - this->config.serialize_into(update_client_config_cmd.client_config); - this->client_channel.send(0x04, 0x00, &update_client_config_cmd, sizeof(update_client_config_cmd)); - } - - string port_name = login_port_name_for_version(this->version()); - S_Reconnect_19 reconnect_cmd = {0, s->name_to_port_config.at(port_name)->port, 0}; - - // If the client is on a virtual connection, we can use any address - // here and they should be able to connect back to the game server - if (this->client_channel.virtual_network_id) { - struct sockaddr_in* dest_sin = reinterpret_cast(&this->next_destination); - if (dest_sin->sin_family != AF_INET) { - throw logic_error("ss not AF_INET"); - } - reconnect_cmd.address = IPStackSimulator::connect_address_for_remote_address(ntohl(dest_sin->sin_addr.s_addr)); - } else { - const struct sockaddr_in* sin = reinterpret_cast(&this->client_channel.remote_addr); - if (sin->sin_family != AF_INET) { - throw logic_error("existing connection is not ipv4"); - } - reconnect_cmd.address = is_local_address(ntohl(sin->sin_addr.s_addr)) ? s->local_address : s->external_address; - } - - this->client_channel.send(0x19, 0x00, &reconnect_cmd, sizeof(reconnect_cmd)); - this->disconnect_action = DisconnectAction::CLOSE_IMMEDIATELY; - } -} - -uint64_t ProxyServer::LinkedSession::timeout_for_disconnect_action( - DisconnectAction action) { - switch (action) { - case DisconnectAction::LONG_TIMEOUT: - return 5 * 60 * 1000 * 1000; // 5 minutes - case DisconnectAction::MEDIUM_TIMEOUT: - return 30 * 1000 * 1000; // 30 seconds - case DisconnectAction::SHORT_TIMEOUT: - return 10 * 1000 * 1000; // 10 seconds - case DisconnectAction::CLOSE_IMMEDIATELY: - return 0; - default: - throw logic_error("disconnect action does not have a timeout"); - } -} - -void ProxyServer::LinkedSession::disconnect() { - // Disconnect both ends - this->client_channel.disconnect(); - this->server_channel.disconnect(); - - // Set a timeout to delete the session entirely (in case the client doesn't - // reconnect) - struct timeval tv = phosg::usecs_to_timeval(this->timeout_for_disconnect_action(this->disconnect_action)); - event_add(this->timeout_event.get(), &tv); -} - -bool ProxyServer::LinkedSession::is_connected() const { - return (this->server_channel.connected() && this->client_channel.connected()); -} - -void ProxyServer::LinkedSession::on_input(Channel& ch, uint16_t command, uint32_t flag, std::string& data) { - auto* ses = reinterpret_cast(ch.context_obj); - bool is_server_stream = (&ch == &ses->server_channel); - - try { - if (is_server_stream) { - size_t bytes_to_save = min(data.size(), sizeof(ses->prev_server_command_bytes)); - memcpy(ses->prev_server_command_bytes, data.data(), bytes_to_save); - } - on_proxy_command( - ses->shared_from_this(), - is_server_stream, - command, - flag, - data); - } catch (const exception& e) { - ses->log.error("Failed to process command from %s: %s", - is_server_stream ? "server" : "client", e.what()); - ses->disconnect(); - } -} - -shared_ptr ProxyServer::get_session() const { - if (this->id_to_linked_session.empty()) { - throw runtime_error("no sessions exist"); - } - if (this->id_to_linked_session.size() > 1) { - throw runtime_error("multiple sessions exist"); - } - return this->id_to_linked_session.begin()->second; -} - -shared_ptr ProxyServer::get_session_by_name(const std::string& name) const { - try { - uint64_t session_id = stoull(name, nullptr, 16); - return this->id_to_linked_session.at(session_id); - } catch (const invalid_argument&) { - throw runtime_error("invalid session name"); - } catch (const out_of_range&) { - throw runtime_error("no such session"); - } -} - -const unordered_map>& ProxyServer::all_sessions() const { - return this->id_to_linked_session; -} - -shared_ptr ProxyServer::create_logged_in_session( - shared_ptr login, - uint16_t local_port, - Version version, - const Client::Config& config) { - auto session = make_shared(this->shared_from_this(), local_port, version, login, config); - auto emplace_ret = this->id_to_linked_session.emplace(session->id, session); - if (!emplace_ret.second) { - throw runtime_error("session already exists for this account"); - } - session->log.info("Opening logged-in session"); - return emplace_ret.first->second; -} - -void ProxyServer::delete_session(uint64_t id) { - if (id >= this->FIRST_LINKED_LOGGED_OUT_SESSION_ID) { - if (this->id_to_linked_session.erase(id)) { - proxy_server_log.info("Closed LS-%016" PRIX64, id); - } - } else { - auto it = this->id_to_unlinked_session.find(id); - if (it == this->id_to_unlinked_session.end()) { - throw logic_error("unlinked session exists but is not registered"); - } - it->second->log.info("Closing session"); - this->unlinked_sessions_to_destroy.emplace(std::move(it->second)); - this->id_to_unlinked_session.erase(it); - - auto tv = phosg::usecs_to_timeval(0); - event_add(this->destroy_sessions_ev.get(), &tv); - } -} - -void ProxyServer::dispatch_destroy_sessions(evutil_socket_t, short, void* ctx) { - reinterpret_cast(ctx)->destroy_sessions(); -} - -void ProxyServer::destroy_sessions() { - this->unlinked_sessions_to_destroy.clear(); -} - -size_t ProxyServer::num_sessions() const { - return this->id_to_linked_session.size(); -} - -size_t ProxyServer::delete_disconnected_sessions() { - size_t count = 0; - for (auto it = this->id_to_linked_session.begin(); it != this->id_to_linked_session.end();) { - if (!it->second->is_connected()) { - it = this->id_to_linked_session.erase(it); - count++; - } else { - it++; - } - } - return count; -} diff --git a/src/ProxyServer.hh b/src/ProxyServer.hh deleted file mode 100644 index 5ca7ea21..00000000 --- a/src/ProxyServer.hh +++ /dev/null @@ -1,316 +0,0 @@ -#pragma once - -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "PSOEncryption.hh" -#include "PSOProtocol.hh" -#include "ServerState.hh" - -class ProxyServer : public std::enable_shared_from_this { -public: - ProxyServer() = delete; - ProxyServer(const ProxyServer&) = delete; - ProxyServer(ProxyServer&&) = delete; - ProxyServer( - std::shared_ptr base, - std::shared_ptr state); - virtual ~ProxyServer() = default; - - void listen(const std::string& addr, uint16_t port, Version version, const struct sockaddr_storage* default_destination = nullptr); - - void connect_virtual_client(struct bufferevent* bev, uint64_t virtual_network_id, uint16_t server_port); - - struct LinkedSession : std::enable_shared_from_this { - std::weak_ptr server; - uint64_t id; - phosg::PrefixedLogger log; - - std::unique_ptr timeout_event; - - std::shared_ptr login; - - Channel client_channel; - Channel server_channel; - uint16_t local_port; - struct sockaddr_storage next_destination; - - enum class DisconnectAction { - LONG_TIMEOUT = 0, - MEDIUM_TIMEOUT, - SHORT_TIMEOUT, - CLOSE_IMMEDIATELY, - }; - DisconnectAction disconnect_action; - - uint8_t prev_server_command_bytes[6]; - uint32_t remote_ip_crc; - bool enable_remote_ip_crc_patch; - - uint32_t sub_version; - std::string character_name; - std::string serial_number2; // Only used for DC sessions - uint64_t hardware_id; - std::string login_command_bb; - XBNetworkLocation xb_netloc; - parray xb_9E_unknown_a1a; - - uint32_t challenge_rank_color_override; - std::string challenge_rank_title_override; - int64_t remote_guild_card_number; - parray remote_client_config_data; - Client::Config config; - // A null handler in here means to forward the response to the remote server - std::deque> function_call_return_handler_queue; - ItemData next_drop_item; - uint32_t next_item_id; - - enum class DropMode { - DISABLED = 0, - PASSTHROUGH, - INTERCEPT, - }; - DropMode drop_mode; - std::shared_ptr quest_dat_data; - std::shared_ptr item_creator; - std::shared_ptr map_state; - - struct LobbyPlayer { - uint32_t guild_card_number = 0; - uint64_t xb_user_id = 0; - std::string name; - uint8_t language = 0; - uint8_t section_id = 0; - uint8_t char_class = 0; - }; - std::vector lobby_players; - size_t lobby_client_id; - size_t leader_client_id; - uint16_t floor; - VectorXZF pos; - bool is_in_game; - bool is_in_quest; - uint8_t lobby_event; - uint8_t lobby_difficulty; - uint8_t lobby_section_id; - GameMode lobby_mode; - Episode lobby_episode; - uint32_t lobby_random_seed; - uint64_t client_ping_start_time = 0; - uint64_t server_ping_start_time = 0; - - std::shared_ptr detector_crypt; - - struct SavingFile { - std::string basename; - std::string output_filename; - bool is_download; - size_t total_size; - std::string data; - - SavingFile( - const std::string& basename, - const std::string& output_filename, - size_t total_size, - bool is_download); - }; - std::unordered_map saving_files; - - // TODO: This first constructor should be private - LinkedSession( - std::shared_ptr server, - uint64_t id, - uint16_t local_port, - Version version); - LinkedSession( - std::shared_ptr server, - uint16_t local_port, - Version version, - std::shared_ptr login, - const Client::Config& config); - LinkedSession( - std::shared_ptr server, - uint16_t local_port, - Version version, - std::shared_ptr login, - const struct sockaddr_storage& next_destination); - LinkedSession( - std::shared_ptr server, - uint64_t id, - uint16_t local_port, - Version version, - const struct sockaddr_storage& next_destination); - - std::shared_ptr require_server() const; - std::shared_ptr require_server_state() const; - - inline Version version() const { - return this->client_channel.version; - } - inline uint8_t language() const { - return this->client_channel.language; - } - inline uint32_t effective_sub_version() const { - return this->config.check_flag(Client::Flag::PROXY_VIRTUAL_CLIENT) - ? default_sub_version_for_version(this->version()) - : this->sub_version; - } - void set_version(Version v); - - void resume( - Channel&& client_channel, - std::shared_ptr detector_crypt, - uint32_t sub_version, - const std::string& character_name, - const std::string& serial_number2, - uint64_t hardware_id, - const XBNetworkLocation& xb_netloc, - const parray& xb_9E_unknown_a1a); - void resume( - Channel&& client_channel, - std::shared_ptr detector_crypt, - std::string&& login_command_bb); - void resume(Channel&& client_channel); - void resume_inner( - Channel&& client_channel, - std::shared_ptr detector_crypt); - void connect(); - - static uint64_t timeout_for_disconnect_action(DisconnectAction action); - static void dispatch_on_timeout(evutil_socket_t fd, short what, void* ctx); - static void on_input(Channel& ch, uint16_t, uint32_t, std::string& msg); - static void on_error(Channel& ch, short events); - void on_timeout(); - - void update_channel_names(); - - void clear_lobby_players(size_t num_slots); - - void set_drop_mode(DropMode new_mode); - - void send_to_game_server(const char* error_message = nullptr); - void disconnect(); - bool is_connected() const; - }; - - std::shared_ptr get_session() const; - std::shared_ptr get_session_by_name(const std::string& name) const; - const std::unordered_map>& all_sessions() const; - - std::shared_ptr create_logged_in_session( - std::shared_ptr login, - uint16_t local_port, - Version version, - const Client::Config& config); - void delete_session(uint64_t id); - - size_t num_sessions() const; - - size_t delete_disconnected_sessions(); - -private: - struct ListeningSocket { - ProxyServer* server; - - phosg::PrefixedLogger log; - uint16_t port; - phosg::scoped_fd fd; - std::unique_ptr listener; - Version version; - struct sockaddr_storage default_destination; - - ListeningSocket( - ProxyServer* server, - const std::string& addr, - uint16_t port, - Version version, - const struct sockaddr_storage* default_destination); - - static void dispatch_on_listen_accept(struct evconnlistener* listener, - evutil_socket_t fd, struct sockaddr* address, int socklen, void* ctx); - static void dispatch_on_listen_error(struct evconnlistener* listener, void* ctx); - void on_listen_accept(int fd); - void on_listen_error(); - }; - - struct UnlinkedSession { - std::weak_ptr server; - uint64_t id; - - phosg::PrefixedLogger log; - Channel channel; - uint16_t local_port; - struct sockaddr_storage next_destination; - - std::shared_ptr detector_crypt; - - // Temporary state used just before resuming a LinkedSession. These aren't - // just local variables inside on_input because XB requires two commands to - // get started (9E and 9F), so we need to store this state somewhere between - // those commands. - std::shared_ptr login; - uint32_t sub_version = 0; - std::string character_name; - Client::Config config; - std::string login_command_bb; - std::string serial_number2; - uint64_t hardware_id; - XBNetworkLocation xb_netloc; - parray xb_9E_unknown_a1a; - - UnlinkedSession( - std::shared_ptr server, - uint64_t id, - struct bufferevent* bev, - uint64_t virtual_network_id, - uint16_t port, - Version version); - - std::shared_ptr require_server() const; - std::shared_ptr require_server_state() const; - - inline Version version() const { - return this->channel.version; - } - - void set_login(std::shared_ptr login); - - void receive_and_process_commands(); - - static void on_input(Channel& ch, uint16_t command, uint32_t flag, std::string& msg); - static void on_error(Channel& ch, short events); - }; - - std::shared_ptr base; - std::shared_ptr destroy_sessions_ev; - std::shared_ptr state; - std::map> listeners; - std::unordered_map> id_to_unlinked_session; - std::unordered_set> unlinked_sessions_to_destroy; - std::unordered_map> id_to_linked_session; - uint64_t next_unlinked_session_id; - uint64_t next_logged_out_session_id; - - static void dispatch_destroy_sessions(evutil_socket_t, short, void* ctx); - void destroy_sessions(); - - void on_client_connect( - struct bufferevent* bev, - uint64_t virtual_network_id, - uint16_t listen_port, - Version version, - const struct sockaddr_storage* default_destination); - - static constexpr uint64_t FIRST_UNLINKED_SESSION_ID = 0x0000000000000001; - static constexpr uint64_t FIRST_LINKED_LOGGED_OUT_SESSION_ID = 0x0000000080000000; - static constexpr uint64_t FIRST_LINKED_LOGGED_IN_SESSION_ID = 0x00000000FFFFFFFF; -}; diff --git a/src/ProxySession.cc b/src/ProxySession.cc new file mode 100644 index 00000000..bf5b3d5d --- /dev/null +++ b/src/ProxySession.cc @@ -0,0 +1,88 @@ +#include "ProxySession.hh" + +#include "ServerState.hh" + +using namespace std; + +size_t ProxySession::num_proxy_sessions = 0; + +ProxySession::ProxySession(shared_ptr server_channel, const PersistentConfig* pc) + : server_channel(server_channel) { + if (pc) { + this->remote_guild_card_number = pc->remote_guild_card_number; + this->remote_bb_security_token = pc->remote_bb_security_token; + this->remote_client_config_data = pc->remote_client_config_data; + this->enable_remote_ip_crc_patch = pc->enable_remote_ip_crc_patch; + } else if (is_v4(this->server_channel->version)) { + this->remote_guild_card_number = 0; + } + this->num_proxy_sessions++; +} + +ProxySession::~ProxySession() { + this->num_proxy_sessions--; +} + +void ProxySession::set_drop_mode(shared_ptr s, Version version, int64_t override_random_seed, DropMode new_mode) { + this->drop_mode = new_mode; + if (this->drop_mode == DropMode::INTERCEPT) { + shared_ptr rare_item_set; + shared_ptr common_item_set; + switch (version) { + case Version::PC_PATCH: + case Version::BB_PATCH: + case Version::GC_EP3_NTE: + case Version::GC_EP3: + throw runtime_error("cannot create item creator for this base version"); + case Version::DC_NTE: + case Version::DC_11_2000: + case Version::DC_V1: + // TODO: We should probably have a v1 common item set at some point too + common_item_set = s->common_item_set_v2; + rare_item_set = s->rare_item_sets.at("rare-table-v1"); + break; + case Version::DC_V2: + case Version::PC_NTE: + case Version::PC_V2: + common_item_set = s->common_item_set_v2; + rare_item_set = s->rare_item_sets.at("rare-table-v2"); + break; + case Version::GC_NTE: + case Version::GC_V3: + case Version::XB_V3: + common_item_set = s->common_item_set_v3_v4; + rare_item_set = s->rare_item_sets.at("rare-table-v3"); + break; + case Version::BB_V4: + common_item_set = s->common_item_set_v3_v4; + rare_item_set = s->rare_item_sets.at("rare-table-v4"); + break; + default: + throw logic_error("invalid lobby base version"); + } + auto rand_crypt = make_shared((override_random_seed >= 0) ? override_random_seed : this->lobby_random_seed); + this->item_creator = make_shared( + common_item_set, + rare_item_set, + s->armor_random_set, + s->tool_random_set, + s->weapon_random_sets.at(this->lobby_difficulty), + s->tekker_adjustment_set, + s->item_parameter_table(version), + s->item_stack_limits(version), + this->lobby_episode, + (this->lobby_mode == GameMode::SOLO) ? GameMode::NORMAL : this->lobby_mode, + this->lobby_difficulty, + this->lobby_section_id, + std::move(rand_crypt), + // TODO: Can we get battle rules here somehow? + nullptr); + } else { + this->item_creator.reset(); + } +} + +void ProxySession::clear_lobby_players(size_t num_slots) { + this->lobby_players.clear(); + this->lobby_players.resize(num_slots); +} diff --git a/src/ProxySession.hh b/src/ProxySession.hh new file mode 100644 index 00000000..83c8f0ec --- /dev/null +++ b/src/ProxySession.hh @@ -0,0 +1,88 @@ +#pragma once + +#include "WindowsPlatform.hh" + +#include +#include +#include +#include + +#include "Channel.hh" +#include "ItemCreator.hh" +#include "Map.hh" + +struct ServerState; + +struct ProxySession { + static size_t num_proxy_sessions; + + std::shared_ptr server_channel; + + parray prev_server_command_bytes; + uint32_t remote_ip_crc = 0; + bool received_reconnect = false; + bool enable_remote_ip_crc_patch = false; + + struct LobbyPlayer { + uint32_t guild_card_number = 0; + uint64_t xb_user_id = 0; + std::string name; + uint8_t language = 0; + uint8_t section_id = 0; + uint8_t char_class = 0; + }; + std::vector lobby_players; + bool is_in_game = false; + bool is_in_quest = false; + uint8_t leader_client_id = 0; + uint8_t lobby_event = 0; + uint8_t lobby_difficulty = 0; + uint8_t lobby_section_id = 0; + GameMode lobby_mode = GameMode::NORMAL; + Episode lobby_episode = Episode::EP1; + uint32_t lobby_random_seed = 0; + uint64_t server_ping_start_time = 0; + + int64_t remote_guild_card_number = -1; + uint32_t remote_bb_security_token = 0; + parray remote_client_config_data; + + enum class DropMode { + DISABLED = 0, + PASSTHROUGH, + INTERCEPT, + }; + DropMode drop_mode = DropMode::PASSTHROUGH; + std::shared_ptr quest_dat_data; + std::shared_ptr item_creator; + std::shared_ptr map_state; + // TODO: Be less lazy and track item IDs correctly in proxy games. (Then + // change this to use the actual client's next item ID, not this hardcoded + // default.) + uint32_t next_item_id = 0x44000000; + + struct PersistentConfig { + uint32_t account_id; + uint32_t remote_guild_card_number; + uint32_t remote_bb_security_token; + parray remote_client_config_data; + bool enable_remote_ip_crc_patch; + std::unique_ptr expire_timer; + }; + + explicit ProxySession(std::shared_ptr server_channel, const PersistentConfig* pc); + ~ProxySession(); + + struct SavingFile { + std::string basename; + std::string output_filename; + bool is_download; + size_t total_size; + std::string data; + }; + std::unordered_map saving_files; + + void set_drop_mode(std::shared_ptr s, Version version, int64_t override_random_seed, DropMode new_mode); + + void clear_lobby_players(size_t num_slots); +}; diff --git a/src/Quest.cc b/src/Quest.cc index 08cc8d38..39b95db2 100644 --- a/src/Quest.cc +++ b/src/Quest.cc @@ -1,6 +1,7 @@ #include "Quest.hh" #include +#include #include #include #include @@ -91,16 +92,16 @@ string decrypt_download_quest_data_section( size_t decompressed_size = prs_decompress_size( r.getv(r.remaining(), false), r.remaining(), sizeof(Episode3::MapDefinitionTrial), true); if (decompressed_size < sizeof(Episode3::MapDefinitionTrial)) { - throw runtime_error(phosg::string_printf( - "decompressed size (%zu) does not match expected size (%zu)", + throw runtime_error(std::format( + "decompressed size ({}) does not match expected size ({})", decompressed_size, sizeof(Episode3::MapDefinitionTrial))); } return decrypted.substr(0x28); } else { if (header->decompressed_size & 0xFFF00000) { - throw runtime_error(phosg::string_printf( - "decompressed_size too large (%08" PRIX32 ")", header->decompressed_size.load())); + throw runtime_error(std::format( + "decompressed_size too large ({:08X})", header->decompressed_size)); } if (!skip_checksum) { @@ -109,8 +110,8 @@ string decrypt_download_quest_data_section( uint32_t actual_crc = phosg::crc32(decrypted.data(), orig_size); header->checksum = expected_crc; if (expected_crc != actual_crc && expected_crc != phosg::bswap32(actual_crc)) { - throw runtime_error(phosg::string_printf( - "incorrect decrypted data section checksum: expected %08" PRIX32 "; received %08" PRIX32, + throw runtime_error(std::format( + "incorrect decrypted data section checksum: expected {:08X}; received {:08X}", expected_crc, actual_crc)); } } @@ -127,11 +128,11 @@ string decrypt_download_quest_data_section( size_t decompressed_size = prs_decompress_size( decrypted.data() + sizeof(HeaderT), decrypted.size() - sizeof(HeaderT)); - size_t expected_decompressed_size = header->decompressed_size.load(); + size_t expected_decompressed_size = header->decompressed_size; if ((decompressed_size != expected_decompressed_size) && (decompressed_size != expected_decompressed_size - 8)) { - throw runtime_error(phosg::string_printf( - "decompressed size (%zu) does not match expected size (%zu)", + throw runtime_error(std::format( + "decompressed size ({}) does not match expected size ({})", decompressed_size, expected_decompressed_size)); } @@ -153,8 +154,8 @@ string decrypt_vms_v1_data_section(const void* data_section, size_t size) { size_t actual_decompressed_size = prs_decompress_size(data); if (actual_decompressed_size != expected_decompressed_size) { - throw runtime_error(phosg::string_printf( - "decompressed size (%zu) does not match size in header (%" PRId32 ")", + throw runtime_error(std::format( + "decompressed size ({}) does not match size in header ({})", actual_decompressed_size, expected_decompressed_size)); } @@ -180,7 +181,7 @@ string find_seed_and_decrypt_download_quest_data_section( 0, 0x100000000, 0x1000, num_threads); if (!result.empty() && (result_seed < 0x100000000)) { - static_game_data_log.info("Found seed %08" PRIX64, result_seed); + static_game_data_log.info_f("Found seed {:08X}", result_seed); return result; } else { throw runtime_error("no seed found"); @@ -224,9 +225,9 @@ void VersionedQuest::assert_valid() const { string VersionedQuest::bin_filename() const { if (this->episode == Episode::EP3) { - return phosg::string_printf("m%06" PRIu32 "p_e.bin", this->quest_number); + return std::format("m{:06}p_e.bin", this->quest_number); } else { - return phosg::string_printf("quest%" PRIu32 ".bin", this->quest_number); + return std::format("quest{}.bin", this->quest_number); } } @@ -234,7 +235,7 @@ string VersionedQuest::dat_filename() const { if (this->episode == Episode::EP3) { throw logic_error("Episode 3 quests do not have .dat files"); } else { - return phosg::string_printf("quest%" PRIu32 ".dat", this->quest_number); + return std::format("quest{}.dat", this->quest_number); } } @@ -242,7 +243,7 @@ string VersionedQuest::pvr_filename() const { if (this->episode == Episode::EP3) { throw logic_error("Episode 3 quests do not have .pvr files"); } else { - return phosg::string_printf("quest%" PRIu32 ".pvr", this->quest_number); + return std::format("quest{}.pvr", this->quest_number); } } @@ -250,18 +251,18 @@ string VersionedQuest::xb_filename() const { if (this->episode == Episode::EP3) { throw logic_error("Episode 3 quests do not have Xbox filenames"); } else { - return phosg::string_printf("quest%" PRIu32 "_%c.dat", this->quest_number, tolower(char_for_language_code(this->language))); + return std::format("quest{}_{}.dat", this->quest_number, static_cast(tolower(char_for_language_code(this->language)))); } } string VersionedQuest::encode_qst() const { unordered_map> files; - files.emplace(phosg::string_printf("quest%" PRIu32 ".bin", this->quest_number), this->bin_contents); - files.emplace(phosg::string_printf("quest%" PRIu32 ".dat", this->quest_number), this->dat_contents); + files.emplace(std::format("quest{}.bin", this->quest_number), this->bin_contents); + files.emplace(std::format("quest{}.dat", this->quest_number), this->dat_contents); if (this->pvr_contents) { - files.emplace(phosg::string_printf("quest%" PRIu32 ".pvr", this->quest_number), this->pvr_contents); + files.emplace(std::format("quest{}.pvr", this->quest_number), this->pvr_contents); } - string xb_filename = phosg::string_printf("quest%" PRIu32 "_%c.dat", quest_number, tolower(char_for_language_code(language))); + string xb_filename = std::format("quest{}_{}.dat", quest_number, static_cast(tolower(char_for_language_code(language)))); return encode_qst_file(files, this->name, this->quest_number, xb_filename, this->version, this->is_dlq_encoded); } @@ -325,85 +326,85 @@ uint32_t Quest::versions_key(Version v, uint8_t language) { void Quest::add_version(shared_ptr vq) { if (this->quest_number != vq->quest_number) { - throw logic_error(phosg::string_printf( - "incorrect versioned quest number (existing: %08" PRIX32 ", new: %08" PRIX32 ")", + throw logic_error(std::format( + "incorrect versioned quest number (existing: {:08X}, new: {:08X})", this->quest_number, vq->quest_number)); } if (this->category_id != vq->category_id) { - throw runtime_error(phosg::string_printf( - "quest version is in a different category (existing: %08" PRIX32 ", new: %08" PRIX32 ")", + throw runtime_error(std::format( + "quest version is in a different category (existing: {:08X}, new: {:08X})", this->category_id, vq->category_id)); } if (this->episode != vq->episode) { - throw runtime_error(phosg::string_printf( - "quest version is in a different episode (existing: %s, new: %s)", + throw runtime_error(std::format( + "quest version is in a different episode (existing: {}, new: {})", name_for_episode(this->episode), name_for_episode(vq->episode))); } if (this->allow_start_from_chat_command != vq->allow_start_from_chat_command) { - throw runtime_error(phosg::string_printf( - "quest version has a different allow_start_from_chat_command state (existing: %s, new: %s)", + throw runtime_error(std::format( + "quest version has a different allow_start_from_chat_command state (existing: {}, new: {})", this->allow_start_from_chat_command ? "true" : "false", vq->allow_start_from_chat_command ? "true" : "false")); } if (this->joinable != vq->joinable) { - throw runtime_error(phosg::string_printf( - "quest version has a different joinability state (existing: %s, new: %s)", + throw runtime_error(std::format( + "quest version has a different joinability state (existing: {}, new: {})", this->joinable ? "true" : "false", vq->joinable ? "true" : "false")); } if (this->max_players != vq->max_players) { - throw runtime_error(phosg::string_printf( - "quest version has a different maximum player count (existing: %hhu, new: %hhu)", + throw runtime_error(std::format( + "quest version has a different maximum player count (existing: {}, new: {})", this->max_players, vq->max_players)); } if (this->lock_status_register != vq->lock_status_register) { - throw runtime_error(phosg::string_printf( - "quest version has a different lock status register (existing: %04hX, new: %04hX)", + throw runtime_error(std::format( + "quest version has a different lock status register (existing: {:04X}, new: {:04X})", this->lock_status_register, vq->lock_status_register)); } if (!this->battle_rules != !vq->battle_rules) { - throw runtime_error(phosg::string_printf( - "quest version has a different battle rules presence state (existing: %s, new: %s)", + throw runtime_error(std::format( + "quest version has a different battle rules presence state (existing: {}, new: {})", this->battle_rules ? "present" : "absent", vq->battle_rules ? "present" : "absent")); } if (this->battle_rules && (*this->battle_rules != *vq->battle_rules)) { string existing_str = this->battle_rules->json().serialize(); string new_str = vq->battle_rules->json().serialize(); - throw runtime_error(phosg::string_printf( - "quest version has different battle rules (existing: %s, new: %s)", - existing_str.c_str(), new_str.c_str())); + throw runtime_error(std::format( + "quest version has different battle rules (existing: {}, new: {})", + existing_str, new_str)); } if (this->challenge_template_index != vq->challenge_template_index) { - throw runtime_error(phosg::string_printf( - "quest version has different challenge template index (existing: %zd, new: %zd)", + throw runtime_error(std::format( + "quest version has different challenge template index (existing: {}, new: {})", this->challenge_template_index, vq->challenge_template_index)); } if (this->description_flag != vq->description_flag) { - throw runtime_error(phosg::string_printf( - "quest version has different description flag (existing: %02hhX, new: %02hhX)", + throw runtime_error(std::format( + "quest version has different description flag (existing: {:02X}, new: {:02X})", this->description_flag, vq->description_flag)); } if (!this->available_expression != !vq->available_expression) { - throw runtime_error(phosg::string_printf( - "quest version has available expression but root quest does not, or vice versa (existing: %s, new: %s)", + throw runtime_error(std::format( + "quest version has available expression but root quest does not, or vice versa (existing: {}, new: {})", this->available_expression ? "present" : "absent", vq->available_expression ? "present" : "absent")); } if (this->available_expression && *this->available_expression != *vq->available_expression) { string existing_str = this->available_expression->str(); string new_str = vq->available_expression->str(); - throw runtime_error(phosg::string_printf( - "quest version has a different available expression (existing: %s, new: %s)", - existing_str.c_str(), new_str.c_str())); + throw runtime_error(std::format( + "quest version has a different available expression (existing: {}, new: {})", + existing_str, new_str)); } if (!this->enabled_expression != !vq->enabled_expression) { - throw runtime_error(phosg::string_printf( - "quest version has enabled expression but root quest does not, or vice versa (existing: %s, new: %s)", + throw runtime_error(std::format( + "quest version has enabled expression but root quest does not, or vice versa (existing: {}, new: {})", this->enabled_expression ? "present" : "absent", vq->enabled_expression ? "present" : "absent")); } if (this->enabled_expression && *this->enabled_expression != *vq->enabled_expression) { string existing_str = this->enabled_expression->str(); string new_str = vq->enabled_expression->str(); - throw runtime_error(phosg::string_printf( - "quest version has a different enabled expression (existing: %s, new: %s)", - existing_str.c_str(), new_str.c_str())); + throw runtime_error(std::format( + "quest version has a different enabled expression (existing: {}, new: {})", + existing_str, new_str)); } this->versions.emplace(this->versions_key(vq->version, vq->language), vq); @@ -441,8 +442,8 @@ std::shared_ptr Quest::get_supermap(int64_t random_seed) const { if (save_to_cache) { this->supermap = supermap; } - static_game_data_log.info("Constructed %s supermap for quest %" PRIu32 " (%s)", - save_to_cache ? "cacheable" : "temporary", this->quest_number, this->name.c_str()); + static_game_data_log.info_f("Constructed {} supermap for quest {} ({})", + save_to_cache ? "cacheable" : "temporary", this->quest_number, this->name); return supermap; } @@ -561,11 +562,12 @@ QuestIndex::QuestIndex( }; string cat_path = directory + "/" + cat->directory_name; - if (!phosg::isdir(cat_path)) { - static_game_data_log.warning("Quest category directory %s is missing; skipping it", cat_path.c_str()); + if (!std::filesystem::is_directory(cat_path)) { + static_game_data_log.warning_f("Quest category directory {} is missing; skipping it", cat_path); continue; } - for (string filename : phosg::list_directory_sorted(cat_path)) { + for (const auto& item : std::filesystem::directory_iterator(cat_path)) { + string filename = item.path().filename().string(); if (filename == ".DS_Store") { continue; } @@ -575,16 +577,16 @@ QuestIndex::QuestIndex( try { string orig_filename = filename; string file_data; - if (phosg::ends_with(filename, ".gci")) { + if (filename.ends_with(".gci")) { file_data = decode_gci_data(phosg::load_file(file_path)); filename.resize(filename.size() - 4); - } else if (phosg::ends_with(filename, ".vms")) { + } else if (filename.ends_with(".vms")) { file_data = decode_vms_data(phosg::load_file(file_path)); filename.resize(filename.size() - 4); - } else if (phosg::ends_with(filename, ".dlq")) { + } else if (filename.ends_with(".dlq")) { file_data = decode_dlq_data(phosg::load_file(file_path)); filename.resize(filename.size() - 4); - } else if (phosg::ends_with(filename, ".bin.txt")) { + } else if (filename.ends_with(".bin.txt")) { string include_dir = phosg::dirname(file_path); assembled = make_unique(assemble_quest_script( phosg::load_file(file_path), @@ -592,7 +594,7 @@ QuestIndex::QuestIndex( {include_dir, "system/quests/includes", "system/client-functions/System"})); file_data = std::move(assembled->data); filename.resize(filename.size() - 4); - if (phosg::ends_with(filename, ".bin")) { + if (filename.ends_with(".bin")) { filename.push_back('d'); } } else { @@ -624,11 +626,11 @@ QuestIndex::QuestIndex( } else if (extension == "qst") { auto files = decode_qst_data(file_data); for (auto& it : files) { - if (phosg::ends_with(it.first, ".bin")) { + if (it.first.ends_with(".bin")) { add_bin_file(file_basename, orig_filename, std::move(it.second), nullptr); - } else if (phosg::ends_with(it.first, ".dat")) { + } else if (it.first.ends_with(".dat")) { add_dat_file(file_basename, orig_filename, std::move(it.second)); - } else if (phosg::ends_with(it.first, ".pvr")) { + } else if (it.first.ends_with(".pvr")) { add_file(pvr_files, file_basename, orig_filename, std::move(it.second), true); } else { throw runtime_error("qst file contains unsupported file type: " + it.first); @@ -637,7 +639,7 @@ QuestIndex::QuestIndex( } } catch (const exception& e) { - static_game_data_log.warning("(%s) Failed to load quest file: (%s)", filename.c_str(), e.what()); + static_game_data_log.warning_f("({}) Failed to load quest file: ({})", filename, e.what()); } } } @@ -919,41 +921,41 @@ QuestIndex::QuestIndex( auto category_name = this->category_index->at(vq->category_id)->name; string filenames_str = entry.filename; if (dat_filedata) { - filenames_str += phosg::string_printf("/%s", dat_filedata->filename.c_str()); + filenames_str += std::format("/{}", dat_filedata->filename); } if (pvr_filedata) { - filenames_str += phosg::string_printf("/%s", pvr_filedata->filename.c_str()); + filenames_str += std::format("/{}", pvr_filedata->filename); } if (json_filedata) { - filenames_str += phosg::string_printf("/%s", json_filedata->filename.c_str()); + filenames_str += std::format("/{}", json_filedata->filename); } auto q_it = this->quests_by_number.find(vq->quest_number); if (q_it != this->quests_by_number.end()) { q_it->second->add_version(vq); - static_game_data_log.info("(%s) Added %s %c version of quest %" PRIu32 " (%s)", - filenames_str.c_str(), + static_game_data_log.info_f("({}) Added {} {} version of quest {} ({})", + filenames_str, phosg::name_for_enum(vq->version), char_for_language_code(vq->language), vq->quest_number, - vq->name.c_str()); + vq->name); } else { auto q = make_shared(vq); this->quests_by_number.emplace(vq->quest_number, q); this->quests_by_name.emplace(vq->name, q); this->quests_by_category_id_and_number[q->category_id].emplace(vq->quest_number, q); - static_game_data_log.info("(%s) Created %s %c quest %" PRIu32 " (%s) (%s, %s (%" PRIu32 "), %s)", - filenames_str.c_str(), + static_game_data_log.info_f("({}) Created {} {} quest {} ({}) ({}, {} ({}), {})", + filenames_str, phosg::name_for_enum(vq->version), char_for_language_code(vq->language), vq->quest_number, - vq->name.c_str(), + vq->name, name_for_episode(vq->episode), - category_name.c_str(), + category_name, vq->category_id, vq->joinable ? "joinable" : "not joinable"); } } catch (const exception& e) { - static_game_data_log.warning("(%s) Failed to index quest file: %s", basename.c_str(), e.what()); + static_game_data_log.warning_f("({}) Failed to index quest file: {}", basename, e.what()); } } } @@ -1198,8 +1200,8 @@ string decode_gci_data( size_t expected_decompressed_bytes = dlq_header.decompressed_size - 8; if (decompressed_bytes < expected_decompressed_bytes) { - throw runtime_error(phosg::string_printf( - "GCI decompressed data is smaller than expected size (have 0x%zX bytes, expected 0x%zX bytes)", + throw runtime_error(std::format( + "GCI decompressed data is smaller than expected size (have 0x{:X} bytes, expected 0x{:X} bytes)", decompressed_bytes, expected_decompressed_bytes)); } @@ -1247,8 +1249,8 @@ string decode_gci_data( size_t decompressed_size = prs_decompress_size(decrypted); if (decompressed_size != sizeof(Episode3::MapDefinition)) { - throw runtime_error(phosg::string_printf( - "decompressed quest is 0x%zX bytes; expected 0x%zX bytes", + throw runtime_error(std::format( + "decompressed quest is 0x{:X} bytes; expected 0x{:X} bytes", decompressed_size, sizeof(Episode3::MapDefinition))); } return decrypted; @@ -1394,7 +1396,7 @@ static unordered_map decode_qst_data_t(const string& data) { for (const auto& it : file_remaining_bytes) { if (it.second) { - throw runtime_error(phosg::string_printf("expected %zu (0x%zX) more bytes for file %s", it.second, it.second, it.first.c_str())); + throw runtime_error(std::format("expected {} (0x{:X}) more bytes for file {}", it.second, it.second, it.first)); } } diff --git a/src/QuestScript.cc b/src/QuestScript.cc index edae40f3..db7099bd 100644 --- a/src/QuestScript.cc +++ b/src/QuestScript.cc @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -117,7 +118,7 @@ static string escape_string(const string& data, TextEncoding encoding = TextEnco } else if (ch == '\t') { ret += "\\t"; } else if (static_cast(ch) < 0x20) { - ret += phosg::string_printf("\\x%02hhX", ch); + ret += std::format("\\x{:02X}", ch); } else if (ch == '\'') { ret += "\\\'"; } else if (ch == '\"') { @@ -221,8 +222,8 @@ struct QuestScriptOpcodeDefinition { flags(flags) {} std::string str() const { - string name_str = this->qedit_name ? phosg::string_printf("%s (qedit: %s)", this->name, this->qedit_name) : this->name; - return phosg::string_printf("%04hX: %s flags=%04hX", this->opcode, name_str.c_str(), this->flags); + string name_str = this->qedit_name ? std::format("{} (qedit: {})", this->name, this->qedit_name) : this->name; + return std::format("{:04X}: {} flags={:04X}", this->opcode, name_str, this->flags); } }; @@ -406,13 +407,16 @@ static const QuestScriptOpcodeDefinition opcode_defs[] = { // Sets a regA to 0 if it's nonzero and vice versa {0x12, "rev", nullptr, {REG}, F_V0_V4}, - // Sets flagA to 1 + // Sets flagA to 1. Sends 6x75. {0x13, "gset", nullptr, {INT16}, F_V0_V4}, - // Clears flagA to 0 + // Clears flagA to 0. Sends 6x75 on BB, but does not send anything on other + // versions. {0x14, "gclear", nullptr, {INT16}, F_V0_V4}, - // Inverts flagA + // Inverts flagA. Like the above two opcodes, sends 6x75 if the flag is set + // by this opcode. Only BB sends 6x75 if the flag is cleared by this + // opcode. {0x15, "grev", nullptr, {INT16}, F_V0_V4}, // If regB is nonzero, sets flagA; otherwise, clears it @@ -2849,7 +2853,7 @@ opcodes_for_version(Version v) { continue; } if (!index.emplace(def.opcode, &def).second) { - throw logic_error(phosg::string_printf("duplicate definition for opcode %04hX", def.opcode)); + throw logic_error(std::format("duplicate definition for opcode {:04X}", def.opcode)); } } } @@ -2872,12 +2876,12 @@ opcodes_by_name_for_version(Version v) { continue; } if (def.name && !index.emplace(phosg::tolower(def.name), &def).second) { - throw logic_error(phosg::string_printf("duplicate definition for opcode %04hX", def.opcode)); + throw logic_error(std::format("duplicate definition for opcode {:04X}", def.opcode)); } if (def.qedit_name) { string lower_qedit_name = phosg::tolower(phosg::tolower(def.qedit_name)); if ((lower_qedit_name != def.name) && !index.emplace(lower_qedit_name, &def).second) { - throw logic_error(phosg::string_printf("duplicate definition for opcode %04hX", def.opcode)); + throw logic_error(std::format("duplicate definition for opcode {:04X}", def.opcode)); } } } @@ -2903,7 +2907,7 @@ void check_opcode_definitions() { for (Version v : versions) { const auto& opcodes_by_name = opcodes_by_name_for_version(v); const auto& opcodes = opcodes_for_version(v); - phosg::log_info("Version %s has %zu opcodes with %zu mnemonics", phosg::name_for_enum(v), opcodes.size(), opcodes_by_name.size()); + phosg::log_info_f("Version {} has {} opcodes with {} mnemonics", phosg::name_for_enum(v), opcodes.size(), opcodes_by_name.size()); } } @@ -2916,7 +2920,7 @@ std::string disassemble_quest_script( bool use_qedit_names) { phosg::StringReader r(data, size); deque lines; - lines.emplace_back(phosg::string_printf(".version %s", phosg::name_for_enum(version))); + lines.emplace_back(std::format(".version {}", phosg::name_for_enum(version))); bool use_wstrs = false; size_t code_offset = 0; @@ -2944,8 +2948,8 @@ std::string disassemble_quest_script( } else { language = 1; } - lines.emplace_back(phosg::string_printf(".quest_num %hu", header.quest_number.load())); - lines.emplace_back(phosg::string_printf(".language %hhu", header.language)); + lines.emplace_back(std::format(".quest_num {}", header.quest_number)); + lines.emplace_back(std::format(".language {}", header.language)); lines.emplace_back(".name " + escape_string(header.name.decode(language))); lines.emplace_back(".short_desc " + escape_string(header.short_description.decode(language))); lines.emplace_back(".long_desc " + escape_string(header.long_description.decode(language))); @@ -2964,8 +2968,8 @@ std::string disassemble_quest_script( } else { language = 1; } - lines.emplace_back(phosg::string_printf(".quest_num %hu", header.quest_number.load())); - lines.emplace_back(phosg::string_printf(".language %hhu", header.language)); + lines.emplace_back(std::format(".quest_num {}", header.quest_number)); + lines.emplace_back(std::format(".language {}", header.language)); lines.emplace_back(".name " + escape_string(header.name.decode(language))); lines.emplace_back(".short_desc " + escape_string(header.short_description.decode(language))); lines.emplace_back(".long_desc " + escape_string(header.long_description.decode(language))); @@ -2986,8 +2990,8 @@ std::string disassemble_quest_script( } else { language = 1; } - lines.emplace_back(phosg::string_printf(".quest_num %hu", header.quest_number.load())); - lines.emplace_back(phosg::string_printf(".language %hhu", header.language)); + lines.emplace_back(std::format(".quest_num {}", header.quest_number)); + lines.emplace_back(std::format(".language {}", header.language)); lines.emplace_back(".name " + escape_string(header.name.decode(language))); lines.emplace_back(".short_desc " + escape_string(header.short_description.decode(language))); lines.emplace_back(".long_desc " + escape_string(header.long_description.decode(language))); @@ -3003,9 +3007,9 @@ std::string disassemble_quest_script( } else { language = 1; } - lines.emplace_back(phosg::string_printf(".quest_num %hu", header.quest_number.load())); - lines.emplace_back(phosg::string_printf(".episode %s", name_for_header_episode_number(header.episode))); - lines.emplace_back(phosg::string_printf(".max_players %hhu", header.max_players ? header.max_players : 4)); + lines.emplace_back(std::format(".quest_num {}", header.quest_number)); + lines.emplace_back(std::format(".episode {}", name_for_header_episode_number(header.episode))); + lines.emplace_back(std::format(".max_players {}", header.max_players ? header.max_players : 4)); if (header.joinable) { lines.emplace_back(".joinable"); } @@ -3047,7 +3051,7 @@ std::string disassemble_quest_script( while (!function_table_r.eof()) { try { uint32_t label_index = function_table.size(); - string name = (label_index == 0) ? "start" : phosg::string_printf("label%04" PRIX32, label_index); + string name = (label_index == 0) ? "start" : std::format("label{:04X}", label_index); uint32_t offset = function_table_r.get_u32l(); auto l = make_shared