Compare commits

..

145 Commits

Author SHA1 Message Date
Martin Michelsen e2d76f77be extend switch assist to 4-player doors 2024-03-14 00:14:40 -07:00
Martin Michelsen 0b80af3f41 fix format code in event action stream disassembly 2024-03-13 22:04:39 -07:00
Martin Michelsen f65acda803 reorder initializers in Map::Object construction 2024-03-13 10:06:07 -07:00
Martin Michelsen 53f485b8f2 fix variable overshadow in 6x6F queued case 2024-03-13 09:53:47 -07:00
Martin Michelsen 69f40f9157 extend persistence to enemy, set, and switch flags 2024-03-12 23:43:08 -07:00
Martin Michelsen 84bb946e05 fix error message for bad entry in trap card list 2024-03-12 20:15:53 -07:00
Martin Michelsen eb132f38d2 fix Ep3 map formatting bug 2024-03-12 20:15:53 -07:00
Martin Michelsen 0f1fbb1069 fix infinite loop edge case in text transcoding 2024-03-12 12:09:12 -07:00
Martin Michelsen c9f7ca2259 add BULK and DEATH_GUNNER to rare tables 2024-03-10 15:21:29 -07:00
Martin Michelsen 8594e5af3c add condition clearing and auto-revive to infinite hp mode 2024-03-10 12:07:30 -07:00
Martin Michelsen 6b5e657630 make name colors appear correctly in v2/v3 crossplay 2024-03-10 12:07:30 -07:00
Martin Michelsen a7845e4b0e add logging for p36 target mode in Ep3 2024-03-10 12:07:30 -07:00
Martin Michelsen c0624334c4 fix format width in log messages 2024-03-09 11:59:48 -08:00
Martin Michelsen 34bac4c5b5 add enemy, object, and event tracking for persistence 2024-03-09 11:28:49 -08:00
Martin Michelsen b81385efdb add TODO for item table serialization 2024-03-09 09:56:49 -08:00
Martin Michelsen 2aae90e65a add option to use game creator section ID 2024-03-09 09:45:20 -08:00
Martin Michelsen 64f2cb8f9e add ServerGlobalDropRateMultiplier 2024-03-09 09:21:36 -08:00
Martin Michelsen 2820b8866c update readme for $secid change 2024-03-08 21:24:30 -08:00
Martin Michelsen a39881fa89 change game section ID on leader change 2024-03-08 21:19:56 -08:00
Martin Michelsen 9d4116f035 fix size field when forwarding 6x7C 2024-03-08 14:31:14 -08:00
Martin Michelsen 287296cf48 fix PCv2 6x7C command 2024-03-08 13:42:54 -08:00
Martin Michelsen b491a57f57 don't load maps for ep3 games on proxy server 2024-03-08 09:17:23 -08:00
Martin Michelsen 19e7f1c677 add confirmation for clear license action 2024-03-08 00:02:50 -08:00
Martin Michelsen 8a7e19757a add --multiply option to convert-rare-item-set 2024-03-07 22:51:32 -08:00
Martin Michelsen 70c57e7727 add V_V1Present token in quest conditions 2024-03-07 21:18:51 -08:00
Martin Michelsen 4a8415308e support extended attributes in json rare tables 2024-03-07 20:52:40 -08:00
Martin Michelsen 0e3df10fc0 print Devolution phone numbers during startup 2024-03-06 13:03:10 -08:00
Martin Michelsen 33b95015a2 add option to override name colors by game version 2024-03-06 13:03:10 -08:00
Martin Michelsen 2ecef68a72 update option_flags description 2024-03-06 12:49:03 -08:00
Martin Michelsen 0db0a55e6b update Ep3 lobby banner instructions 2024-03-06 09:53:48 -08:00
Martin Michelsen 0aedfcc17f don't let exceptions fall out of reload config 2024-03-05 10:11:15 -08:00
Martin Michelsen 581f95051d filter solo-extra quests by episode for consistency 2024-03-05 08:52:32 -08:00
Martin Michelsen 31005ec39d add option to disable chat commands 2024-03-04 22:48:05 -08:00
Martin Michelsen b0b3bb6140 fix NPC last-hit EXP 2024-03-04 21:50:48 -08:00
Martin Michelsen 7e4bc52d99 enable episode filter flag on solo-story category 2024-03-04 21:50:48 -08:00
Martin Michelsen b9f1a1d964 add commands for announcements via Simple Mail 2024-03-04 19:59:21 -08:00
Martin Michelsen a48f79eafa auto-port several codes 2024-03-04 19:47:17 -08:00
Martin Michelsen 907c4fda3c add poison room test 2024-03-04 09:21:30 -08:00
Martin Michelsen 3189b71d46 fix 6x2F client ID check 2024-03-03 23:34:24 -08:00
Martin Michelsen 6ae08e9b05 update event metadata for quests 2024-03-03 23:22:40 -08:00
Martin Michelsen 7cd5aa1c2d fix event lookups in quest availability expressions 2024-03-03 23:15:57 -08:00
Martin Michelsen 6d6a8621bb fix per-lobby events in config.json 2024-03-03 23:15:35 -08:00
Martin Michelsen db254a977b fix long credentials on 11/2000 2024-03-03 22:36:12 -08:00
Martin Michelsen 454bcf107b add DC NTE format for 6x06 command 2024-03-03 22:33:55 -08:00
Martin Michelsen 52688982ea use MARKED encoding for info board 2024-03-03 21:32:56 -08:00
Martin Michelsen 2432d8b32b handle JP heart symbol correctly 2024-03-03 21:24:13 -08:00
Martin Michelsen 7f71b87b9b add $variations command 2024-03-03 21:01:41 -08:00
Martin Michelsen 4faad54872 split team points update 2024-03-02 18:38:31 -08:00
Martin Michelsen e2da4322e2 fix name field in BB 6x70 2024-03-02 16:52:23 -08:00
Martin Michelsen f44706570a alias ep3 item indexes to v3 index 2024-03-02 11:00:54 -08:00
Martin Michelsen b452b11854 handle GC_NTE 6x7C properly 2024-03-02 10:55:53 -08:00
Martin Michelsen f2b5f0950f fix describe-item action 2024-03-02 10:55:40 -08:00
Martin Michelsen f43563edb3 add full versions in get_cli_version 2024-03-02 10:54:59 -08:00
Martin Michelsen bec6d741d4 fix gc nte mag encoding 2024-03-02 10:54:47 -08:00
Martin Michelsen d93e6405c3 fix v1-encoded item descriptions 2024-03-01 23:19:18 -08:00
Martin Michelsen a2e3f4882d make quest episode filter configurable 2024-03-01 21:22:14 -08:00
Martin Michelsen ef101894d1 update solo story quest flag expressions 2024-03-01 20:52:09 -08:00
Martin Michelsen 6eb896f83d clean up some is_nte flags in ep3 server 2024-03-01 19:51:47 -08:00
Martin Michelsen c7812bf764 make bcarray not packed 2024-02-29 23:33:31 -08:00
Martin Michelsen 11f49af6f9 fix using incorrect card object in 59:SLAYERS_ASSASSINS 2024-02-29 22:49:06 -08:00
Martin Michelsen af1c51b2b5 fix v1 unidentified item logic 2024-02-29 21:28:15 -08:00
Martin Michelsen f7c63d82f9 fix material usage on GC NTE 2024-02-29 19:25:14 -08:00
Martin Michelsen a00c25ee17 port vip card patch to all ep3 versions 2024-02-29 09:54:38 -08:00
Martin Michelsen 913f7d04f7 fix non-Japanese encoding in Episode 3 maps 2024-02-28 21:57:25 -08:00
Martin Michelsen b37224a453 add asan definition in comments 2024-02-28 21:53:54 -08:00
Martin Michelsen 8375c61236 add some tools for ep3 replay 2024-02-28 21:08:04 -08:00
Martin Michelsen 424f191bc6 ignore client's equip slot if item can't be equipped in it 2024-02-28 19:52:15 -08:00
Martin Michelsen 90152b4138 add TODO for proxy meet user extension 2024-02-28 19:49:02 -08:00
Martin Michelsen c8041558f5 fix Poison Lily rare check 2024-02-28 19:49:02 -08:00
Martin Michelsen 1f10d03923 describe 6x6B and 6x6C more completely 2024-02-28 19:49:02 -08:00
Martin Michelsen bb560c1153 add XBOX-US1 handlers 2024-02-28 19:38:36 -08:00
Martin Michelsen 72794ad50e write xb decoction patch 2024-02-27 23:07:35 -08:00
Martin Michelsen af1c0a548d add map event files 2024-02-27 00:14:15 -08:00
Martin Michelsen 2f5d547c19 delay all new TCP PSH frames until timeout or ACK is received 2024-02-26 20:28:38 -08:00
Martin Michelsen 32f056c6eb add HTTP /y/data/common-tables 2024-02-26 20:07:28 -08:00
Martin Michelsen ac62cc455c add more xbox patches 2024-02-25 21:55:25 -08:00
Martin Michelsen 79f85f46dc add xbe patch translator shell 2024-02-25 21:40:58 -08:00
Martin Michelsen e2e5875c8d fix xb item loss patches 2024-02-25 10:55:18 -08:00
Martin Michelsen 3868a9fc50 fix eu xb movement patches 2024-02-25 10:23:55 -08:00
Martin Michelsen 28cb1c52b5 support full DC NTE credentials 2024-02-24 22:49:37 -08:00
Martin Michelsen 70325793d9 add missing include on linux 2024-02-24 22:00:58 -08:00
Martin Michelsen a2d1eb4532 add non-US versions of XB item loss patch 2024-02-24 21:54:19 -08:00
Martin Michelsen b17ccd264a move HTTP server to separate thread 2024-02-24 21:53:17 -08:00
Martin Michelsen eaa02b2b78 add ep3 cards and rare tables to HTTP server 2024-02-24 19:13:18 -08:00
Martin Michelsen c3b3cf5140 add other projects to readme 2024-02-24 18:14:17 -08:00
Martin Michelsen 3be7b5f56b add PPPRawListen to example config 2024-02-24 18:03:14 -08:00
Martin Michelsen 14bf23c496 only send next TCP PSH if client's acked seq has changed 2024-02-24 10:24:03 -08:00
Martin Michelsen 5b79785c96 remove unused alias 2024-02-24 09:46:13 -08:00
Martin Michelsen f92fe61aa7 fix ep3 dice range override 2024-02-24 09:42:31 -08:00
Martin Michelsen b7c9fb3864 fix Japanese symbol chat name 2024-02-24 09:40:42 -08:00
Martin Michelsen 294d180e68 use system randomness by default unless overridden 2024-02-23 23:58:10 -08:00
Martin Michelsen 7dc5a02a83 bring back history section in readme 2024-02-23 23:58:10 -08:00
Martin Michelsen 82004b05dc add PPP_RAW protocol 2024-02-23 23:52:17 -08:00
Martin Michelsen a4f69f6ca3 add xbox movement patch 2024-02-23 23:52:17 -08:00
Martin Michelsen 66571d751f color unidentified weapon names in $what 2024-02-23 09:25:29 -08:00
Martin Michelsen 680a1a797c define some flags in 6x0A 2024-02-23 09:25:04 -08:00
Martin Michelsen 543bbb45dc add Xbox beta to handler-tables 2024-02-22 19:11:02 -08:00
Martin Michelsen 38504b3133 clear x bit on all files in system/ 2024-02-22 18:28:21 -08:00
Martin Michelsen f0d15be552 decompress PC NTE map files 2024-02-22 18:20:13 -08:00
Martin Michelsen 0383dc90b8 allow overriding stack sizes 2024-02-22 00:10:42 -08:00
Martin Michelsen 4e4ba5650d add B/T/K language markers 2024-02-20 22:59:53 -08:00
Martin Michelsen 29baaf2d95 fix loading long names on BB 2024-02-20 21:34:30 -08:00
Martin Michelsen 67e64d6836 update readme 2024-02-20 21:34:30 -08:00
Martin Michelsen af8c27dcef mark XB beta as tested 2024-02-20 21:31:02 -08:00
Martin Michelsen 163ec73c04 fix JP v1.3 D6 behavior 2024-02-20 20:47:07 -08:00
Martin Michelsen b74ad9d639 add Quest field in game summary JSON 2024-02-20 09:27:11 -08:00
Martin Michelsen 42c72b92ac fix some edge cases in GC NTE item creation 2024-02-19 23:22:22 -08:00
Martin Michelsen b46be572a6 enforce name length limit at edge only 2024-02-19 21:25:50 -08:00
Martin Michelsen 5d2d4cf2ad fix 6x70 transcoding between BB/non-BB 2024-02-19 21:21:01 -08:00
Martin Michelsen 2ba4224a83 add server info to api 2024-02-19 21:13:12 -08:00
Martin Michelsen 9687a0e522 split game flags in api according to game episode 2024-02-19 20:59:20 -08:00
Martin Michelsen cd77fae4e3 fix play time field and marked utf16 fields 2024-02-19 20:59:20 -08:00
Martin Michelsen f2f1007cee clarify $sropmode text a bit 2024-02-19 20:59:20 -08:00
Martin Michelsen db2c2a4774 implement $dropmode on proxy server 2024-02-18 22:41:42 -08:00
Martin Michelsen f16b8ef983 add HTTP server 2024-02-18 22:41:42 -08:00
Martin Michelsen bd13950ba6 fix system file updates when overlay is present 2024-02-18 10:05:25 -08:00
Martin Michelsen cda86e586d fix Dragon and De Rol Le drops on v1 2024-02-18 09:33:38 -08:00
Martin Michelsen 255878bf60 add $itemnotifs every mode 2024-02-18 09:33:21 -08:00
Martin Michelsen 1d42faac3e move patch servers to separate threads 2024-02-17 22:28:03 -08:00
Martin Michelsen 350a89f3da describe 6x7C command 2024-02-17 17:49:04 -08:00
Martin Michelsen 5bfda213c7 move shell to separate thread 2024-02-16 22:52:46 -08:00
Martin Michelsen d3d63dd36c fix battle table disconnect hook 2024-02-16 18:19:53 -08:00
Martin Michelsen 4dd7b75232 don't show item notifs option on ep3 2024-02-15 20:11:47 -08:00
Martin Michelsen 26abf2f306 update readme 2024-02-15 20:11:34 -08:00
Martin Michelsen 9ff7d6fff3 fix Ep3 NTE DEF die rules not working 2024-02-14 18:53:15 -08:00
Michael Stenberg 8c514a0688 fix/add GC NTE ClassMaxes 2024-02-14 08:33:38 -08:00
Martin Michelsen 08ba5d821b fix case where map selection is changed during setup 2024-02-13 21:37:15 -08:00
Martin Michelsen 35e2a9d6f4 use quest extended rules if present 2024-02-13 21:23:33 -08:00
Martin Michelsen 46e509aa69 fix segfault when attacks default back to SC 2024-02-11 21:39:17 -08:00
Martin Michelsen 198db59816 make invalid label index errors clearer 2024-02-11 15:50:53 -08:00
Martin Michelsen 46667bce46 fix 6xB4x3D NTE format 2024-02-11 15:50:38 -08:00
Martin Michelsen 639c1c3e95 add 06 phase to 93 notes 2024-02-11 15:50:28 -08:00
Martin Michelsen 07ebafa8c6 fix Ep3 NTE tournament menu bugs 2024-02-11 12:17:48 -08:00
Martin Michelsen f548fc04e2 make some text messages shorter 2024-02-11 10:54:16 -08:00
Martin Michelsen c55b19dbc0 fix $dicerange 2024-02-11 10:50:34 -08:00
Martin Michelsen c78c91d408 add Ep3 NTE AR codes 2024-02-11 10:49:55 -08:00
Martin Michelsen e07f65eec5 fix Ep3 NTE target replacement function 2024-02-10 21:53:21 -08:00
Martin Michelsen cfbbdc7216 add nop command in shell 2024-02-10 21:53:21 -08:00
Martin Michelsen cb34b350b0 fix Ep4 boss battle param indexes 2024-02-10 21:53:21 -08:00
Martin Michelsen 23f3bfabaa fix angle_x type in AttackData 2024-02-10 21:53:21 -08:00
Martin Michelsen b66069c10b name PlayerStats::esp 2024-02-10 21:53:21 -08:00
Martin Michelsen 093ba1fd38 replace $defrange with $dicerange 2024-02-10 14:29:37 -08:00
Martin Michelsen a312191ced add AllCards patch for Ep3 NTE 2024-02-10 12:29:54 -08:00
Martin Michelsen 841c722178 fix assembly of F_ARGS opcodes on pre-v3 2024-02-10 12:17:04 -08:00
Martin Michelsen 1ed2112bff update to-do list 2024-02-10 10:23:32 -08:00
1358 changed files with 54434 additions and 7695 deletions
+9 -3
View File
@@ -27,10 +27,12 @@ link_directories(${LOCAL_LIB_DIR})
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_CORE}
${LIBEVENT_PTHREADS})
find_package(phosg REQUIRED)
find_package(Iconv REQUIRED)
@@ -81,10 +83,12 @@ set(SOURCES
src/Episode3/RulerServer.cc
src/Episode3/Server.cc
src/Episode3/Tournament.cc
src/EventUtils.cc
src/FileContentsCache.cc
src/FunctionCompiler.cc
src/GSLArchive.cc
src/GVMEncoder.cc
src/HTTPServer.cc
src/IPFrameInfo.cc
src/IPStackSimulator.cc
src/ItemCreator.cc
@@ -100,6 +104,7 @@ set(SOURCES
src/Map.cc
src/Menu.cc
src/NetworkAddresses.cc
src/PatchServer.cc
src/PatchFileIndex.cc
src/PlayerFilesManager.cc
src/PlayerSubordinates.cc
@@ -121,9 +126,7 @@ set(SOURCES
src/Server.cc
src/ServerShell.cc
src/ServerState.cc
src/Shell.cc
src/StaticGameData.cc
src/StepGraph.cc
src/TeamIndex.cc
src/Text.cc
src/TextIndex.cc
@@ -140,6 +143,9 @@ target_include_directories(newserv PUBLIC ${LIBEVENT_INCLUDE_DIR} ${Iconv_INCLUD
target_link_libraries(newserv phosg ${LIBEVENT_LIBRARIES} ${Iconv_LIBRARIES} pthread)
add_dependencies(newserv newserv-Revision-cc)
# target_compile_options(newserv PRIVATE -fsanitize=address)
# target_link_options(newserv PRIVATE -fsanitize=address)
if(resource_file_FOUND)
target_compile_definitions(newserv PUBLIC HAVE_RESOURCE_FILE)
target_link_libraries(newserv resource_file)
+89 -32
View File
@@ -1,4 +1,4 @@
# newserv <img align="right" src="s-newserv.png" />
# newserv <img align="right" src="static/s-newserv.png" />
newserv is a game server, proxy, and reverse-engineering tool for Phantasy Star Online (PSO).
@@ -9,6 +9,9 @@ Feel free to submit GitHub issues if you find bugs or have feature requests. I'd
See TODO.md for a list of known issues and future work I've curated, or go to the GitHub issue tracker for issues and requests submitted by the community.
**Table of contents**
* Background
* [History](#history)
* [Other server projects](#other-server-projects)
* [Compatibility](#compatibility)
* Setup
* [Server setup](#server-setup)
@@ -24,28 +27,65 @@ See TODO.md for a list of known issues and future work I've curated, or go to th
* [Chat commands](#chat-commands)
* [Non-server features](#non-server-features)
# History
The history of this project essentially mirrors my development as a software engineer from the beginning of my hobby until now. If you don't care about the story, skip to the "Compatibility" or "Setup" sections below.
I originally purchased PSO GC when I heard about PSUL, and wanted to play around with running homebrew on my GameCube. This pathway eventually led to [GCARS-CS](https://github.com/fuzziqersoftware/gcars-cs), but that's another story.
<img align="left" src="static/s-khyps.png" /> After playing PSO for a while, both offline and online, I wrote a proxy called Khyps sometime in 2003. This was back in the days of the official Sega servers, where vulnerabilities weren't addressed in a timely manner or at all. It was common for malicious players using their own proxies or Action Replay codes (a story for another time) to send invalid commands that the servers would blindly forward, and cause the receiving clients to crash. These crashes were more than simply inconvenient; they could also corrupt your save data, destroying the hours of work you may have put into hunting items and leveling up your character.
For a while it was essentially necessary to use a proxy to go online at all, so the proxy could block these invalid commands. Khyps was designed primarily with this function in mind, though it also implemented some convenient cheats, like the ability to give yourself or other players infinite HP and allow you to teleport to different places without using an in-game teleporter.
<img align="left" src="static/s-khyller.png" /> After Khyps I took on the larger challenge of writing a server, which resulted in Khyller sometime in 2005. This was the first server of any type I had ever written. This project eventually evolved into a full-featured environment supporting all versions of the game that I had access to - at the time, PC, GC, and BB. (However, I suspect from reading the ancient source files that Khyller's BB support was very buggy.) As Khyller evolved, the code became increasingly cumbersome, littered with debugging filth that I never cleaned up and odd coding patterns I had picked up over the years. My understanding of the C++ language was woefully incomplete as well (as opposed to now, when it is still incomplete but not woefully so), which resulted in Khyller being essentially a C project that had a couple of classes in it.
<img align="left" src="static/s-aeon.png" /> Sometime in 2006 or 2007, I abandoned Khyller and rebuilt the entire thing from scratch, resulting in Aeon. Aeon was substantially cleaner in code than Khyller but still fairly hard to work with, and it lacked a few of the more arcane features I had originally written (for example, the ability to convert any quest into a download quest). In addition, the code still had some stability problems... it turns out that Aeon's concurrency primitives were simply incorrect. I had derived the concept of a mutex myself, before taking any real computer engineering classes, but had implemented it incorrectly. I made the race window as small as possible, but Aeon would still randomly crash after running seemingly fine for a few days.
At the time of its inception, Aeon was also called newserv, and you may find some beta releases floating around the Internet with filenames like `newserv-b3.zip`. I had released betas 1, 2, and 3 before I released the entire source of beta 5 and stopped working on the project when I went to college. This was around the time when I switched from writing software primarily on Windows to primarily on macOS and Linux, so Aeon beta 5 was the last server I wrote that specifically targeted Windows. (newserv, which you're looking at now, is a bit tedious to compile on Windows but does work.)
<img align="left" src="static/s-newserv.png" /> After a long hiatus from PSO and much professional and personal development in my technical abilities, I was reminiscing sometime in October 2018 by reading my old code archives. Somehow inspired when I came across Aeon, I spent a weekend and a couple more evenings rewriting the entire project again, cleaning up ancient patterns I had used eleven years ago, replacing entire modules with simple STL containers, and eliminating even more support files in favor of configuration autodetection. The code is now suitably modern and stable, and I'm not embarrassed by its existence, as I am by Aeon beta 5's source code and my archive of Khyller (which, thankfully, no one else ever saw).
## Other server projects
Independently of this project, there are many other PSO servers out there. Those that I know of that are (or were) public are listed here in approximate chronological order:
* (Early 2000s) **[Schtserv](https://schtserv.com/)**: The first public-access PSO server; written in Delphi by Schthack. Still active and popular as of this writing (early 2024). Schtserv is also the only other unofficial server to support all versions of PSO, including Episode 3.
* (2005) **Khyller**: An early attempt of mine to support PSO PC, GC, and BB. See above for more details.
* (2006) **Aeon**: My second attempt. Better than Khyller, but still unreliable.
* (2008) **Tethealla**: A fairly extensive implementation of PSOBB, written in C by Sodaboy. The public version of Tethealla has been [officially disowned](https://www.pioneer2.net/community/threads/tethealla-server-forums-removal.26365/) (as it is now more than 15 years old), but closed-source development continues. [Ephinea](https://ephinea.pioneer2.net/), currently the most popular PSOBB server, is the continuation of this project. Several other modern PSOBB servers are forks of the initial public version of Tethealla as well.
* (2008) **[Sylverant](https://sylverant.net/)** [(source)](https://sourceforge.net/projects/sylverant/): The second public-access PSO server; written in C by BlueCrab. Still active and popular as of this writing (early 2024).
* (2015) **[Archon](https://github.com/dcrodman/archon)**: A PSOBB server written in Go by Drew Rodman.
* (2015) **[Idola](https://github.com/HybridEidolon/idolapsoserv)**: A PSOBB server written in Rust by HybridEidolon. Functionality status unknown; the project has been archived.
* (2017) **[Aselia](https://github.com/Solybum/Aselia)**: A PSOBB server written written in C# by Soly. It seems this was planned to be open-source at some point, but that has not (yet) happened.
* (2018) **newserv**: This project right here.
* (2019) **[Mechonis](https://gitlab.com/sora3087/mechonis)**: A PSOBB server with a microservice architecture written in TypeScript by TrueVision.
* (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.
* (2022) **[PSOSERVER](https://github.com/Sancaros/PSOSERVER)**: A server for all versions, written in C by Sancaros and based on Sylverant and newserv.
# Compatibility
newserv supports several versions of PSO, including various development prototypes. Specifically:
| Version | Lobbies | Games | Proxy |
|----------------|--------------|--------------|--------------|
| DC NTE | Yes | Yes | No |
| DC 11/2000 | Yes | Yes | No |
| 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 (3) | Yes | No |
| PC | Yes | Yes | Yes |
| GC Ep1&2 NTE | Yes | Yes | Yes |
| GC Ep1&2 | Yes | Yes | Yes |
| GC Ep1&2 Plus | Yes | Yes | Yes |
| GC Ep3 NTE | Yes | Yes (1) | Yes |
| GC Ep3 | Yes | Yes | Yes |
| Xbox Ep1&2 | Yes | Yes | Yes |
| BB (vanilla) | Yes | Yes (2) | Yes |
| BB (Tethealla) | Yes | Yes (2) | Yes |
newserv supports all known versions of PSO, including development prototypes. Specifically:
| Version | Lobbies | Games | Proxy |
|-----------------|----------|----------|----------|
| DC NTE | Yes | Yes | No |
| DC 11/2000 | Yes | Yes | No |
| 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 (3) | Yes | No |
| PC | Yes | Yes | Yes |
| GC Ep1&2 NTE | Yes | Yes | Yes |
| GC Ep1&2 | Yes | Yes | Yes |
| GC Ep1&2 Plus | Yes | Yes | Yes |
| GC Ep3 NTE | Yes | Yes (1) | Yes |
| GC Ep3 | Yes | Yes | Yes |
| Xbox Ep1&2 Beta | Yes | Yes | Yes |
| Xbox Ep1&2 | Yes | Yes | Yes |
| BB (vanilla) | Yes | Yes (2) | Yes |
| BB (Tethealla) | Yes | Yes (2) | Yes |
*Notes:*
1. *Ep3 NTE battles are not well-tested; some things may not work. See notes/ep3-nte-differences.txt for a list of known differences between NTE and the final version. NTE and non-NTE players cannot battle each other.*
@@ -143,6 +183,17 @@ You can make PSO connect to newserv by setting its default gateway and DNS serve
If you have PSO Plus or Episode III, it won't want to connect to a server on the same local network as the GameCube itself, as determined by the GameCube's IP address and subnet mask. In the old days, one way to get around this was to create a fake network adapter on the server (or use an existing real one) that has an IP address on a different subnet, tell the GameCube that the server is the default gateway (as above), and have the server reply to the DNS request with its non-local IP address. To do this with newserv, just set LocalAddress in the config file to a different interface. For example, if the GameCube is on the 192.168.0.x network and your other adapter has address 10.0.1.6, set newserv's LocalAddress to 10.0.1.6 and set PSO's DNS server and default gateway addresses to the server's 192.168.0.x address. This may not work on modern systems or on non-Windows machines - I haven't tested it in many years.
### PSO GC on a Wii or Wii U
Using a Wii or Wii U to connect to newserv requires the Wii or vWii to be softmodded. How to do this is beyond the scope of this document.
Nintendont includes BBA emulation and is compatible with all PSO GameCube versions except Episodes I&II Trial Edition. To use Nintendont, enable BBA emulation in Nintendont's settings and follow the instructions in the above section (PSO GC on a real GameCube).
Devolution includes modem emulation and is compatible with all PSO GameCube versions including Episodes I&II Trial Edition. newserv can act as a PPP server, which Devolution can directly cnnect to. To do this:
1. Enable the PPPRawListen option according to the comments in config.json.
2. Start newserv.
3. In the game's network settings, set the username and password to anything (they cannot be blank), and set the phone number to the number that newserv outputs to the console during startup. (It will be near the end of all the startup log messages.) If your Wii is on the same network as newserv, use the local number; otherwise, use the external number.
### PSO GC on Dolphin
If you're using the HLE BBA type, set the BBA's DNS server address to newserv's IP address and it should work. (If newserv is on the same machine as Dolphin, you will need to use an action replay code directed at 127.0.0.1 to connect, as PSO rejects DNS queries from the same IP address.) Set PSO's network settings the same as listed below.
@@ -238,7 +289,7 @@ There are five different available behaviors for item drops:
In the `SERVER_PRIVATE` and `SERVER_DUPLICATE` modes, there is no incentive to pick up items before another player, since other players cannot pick up the items you see dropped from boxes and enemies. However, if you pick up an item and drop it later, it can then be seen and picked up by any player.
The drop mode can be changed at any time during a game with the `$dropmode` chat command. If the mode is changed after some items have already been dropped, the existing items retain their visibility (that is, they still can't be picked up by other players since they were dropped before the mode was changed). You can configure which drop modes are used by default, and which modes players are allowed to choose, in config.json. See the comments above the AllowedDropModes and DefaultDropMode keys.
The drop mode can be changed at any time during a game with the `$dropmode` chat command. If the mode is changed after some items have already been dropped, the existing items retain their visibility (that is, items dropped in private mode still can't be picked up by other players since they were dropped before the mode was changed). You can configure which drop modes are used by default, and which modes players are allowed to choose, in config.json. See the comments above the AllowedDropModes and DefaultDropMode keys.
In the server drop modes, the item tables used to generate common items are in the `system/item-tables/ItemPT-*` files. (The V2 files are used for V1 as well.) The rare item tables are in the `rare-table-*.json` files. Unlike the original formats, it's possible to make each enemy drop multiple different rare items at different rates, though the default tables never do this.
@@ -309,7 +360,7 @@ In addition, these features are only supported for the following game versions:
You can put memory patches in the system/client-functions directory with filenames like PatchName.patch.s and they will appear in the Patches menu for PSO GC, XB, and BB clients that support patching. Memory patches are written in PowerPC or x86 assembly and are compiled when newserv is started. The assembly system's features are documented in the comments in system/client-functions/WriteMemory.ppc.s.
newserv comes with a set of patches for GC Episodes 1&2 based on AR codes originally made by Ralf at GC-Forever. Many of them were originally posted in [this thread](https://www.gc-forever.com/forums/viewtopic.php?f=38&t=2050).
newserv comes with a set of patches for GC Episodes 1&2 based on AR codes originally made by Ralf at GC-Forever and Aleron Ives. Many of them were originally posted in [this thread](https://www.gc-forever.com/forums/viewtopic.php?f=38&t=2050).
You can also put DOL files in the system/dol directory, and they will appear in the Programs menu for GC clients. Selecting a DOL file there will load the file into the GameCube's memory and run it, just like the old homebrew loaders (PSUL and PSOload) did. For this to work, ReadMemoryWord.ppc.s, WriteMemory.ppc.s, and RunDOL.ppc.s must be present in the system/client-functions directory. This has been tested on Dolphin but not on a real GameCube, so results may vary.
@@ -334,7 +385,7 @@ There are many options available when starting a proxy session. All options are
* **Block pings**: blocks automatic pings sent by the client, and responds to ping commands from the server automatically. This works around a bug in Sylverant's login server.
* **Infinite HP**: automatically heals you whenever you get hit. An attack that kills you in one hit will still kill you, however.
* **Infinite TP**: automatically restores your TP whenever you use any technique.
* **Switch assist**: attempts to unlock doors that require two players in a one-player game.
* **Switch assist**: attempts to unlock doors that require two or four players in a one-player game.
* **Infinite Meseta** (Episode 3 only): gives you 1,000,000 Meseta, regardless of the value sent by the remote server.
* **Block events**: disables holiday events sent by the remote server.
* **Block patches**: prevents any B2 (patch) commands from reaching the client.
@@ -363,7 +414,11 @@ Some commands only work on the game server and not on the proxy server. The chat
* `$si` (game server only): Shows basic information about the server.
* `$ping`: Shows round-trip ping time from the server to you. On the proxy server, shows the ping time from you to the proxy and from the proxy to the server.
* `$matcount` (game server only): Shows how many of each type of material you've used.
* `$itemnotifs <mode>`: Enables item drop notification messages. The modes are `off`, `rare`, and `on`, which should be self-explanatory. 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' rare drops.
* `$itemnotifs <mode>`: Enables item drop notification messages. If the game has private drops enabled, you will only see a notification if the dropped item is visible to you; you won't be notified of other players' drops. The modes are:
* `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.
* `$what` (game server only): Shows the type, name, and stats of the nearest item on the ground.
* `$where` (game server only): Shows your current floor number and coordinates. Mainly useful for debugging.
@@ -376,8 +431,8 @@ Some commands only work on the game server and not on the proxy server. The chat
* The rest of the commands in this section are enabled on the game server. (They are always enabled on the proxy server.)
* `$quest <number>` (game server only): Load a quest by quest number. Can be used to load battle or challenge quests with only one player present.
* `$qcall <function-id>`: Call a quest function on your client.
* `$qcheck <flag-num>` (game server only): Show the value of a quest flag. This command can be used without debug mode enabled.
* `$qset <flag-num>` or `$qclear <flag-num>`: Set or clear a quest flag for everyone in the game.
* `$qcheck <flag-num>` (game server only): Show the value of a quest flag. This command can be used without debug mode enabled. If you're in a game, show the value of the flag in that game; if you're in the lobby, show the saved value of that quest flag for your character (BB only).
* `$qset <flag-num>` or `$qclear <flag-num>`: Set or clear a quest flag for everyone in the game. If you're in the lobby and on BB, set or clear the saved value of a quest flag in your character file.
* `$qgread <flag-num>` (game server only): Get the value of a quest counter ("global flag"). This command can be used without debug mode enabled.
* `$qgwrite <flag-num> <value>` (game server only): Set the value of a quest counter ("global flag") for yourself.
* `$qsync <reg-num> <value>`: Set a quest register's value for yourself only. `<reg-num>` should be either rXX (e.g. r60) or fXX (e.g. f60); if the latter, `<value>` is parsed as a floating-point value instead of as an integer.
@@ -391,10 +446,10 @@ Some commands only work on the game server and not on the proxy server. The chat
* Personal state commands
* `$arrow <color-id>`: Changes your lobby arrow color.
* `$secid <section-id>`: Sets your override section ID. After running this command, any games you create will use your override section ID for rare drops instead of your character's actual section ID. To revert to your actual section id, run `$secid` with no name after it. On the proxy server, this will not work if the remote server controls item drops (e.g. on BB, or on Schtserv with server drops enabled). If the server does not allow cheat mode anywhere (that is, "CheatModeBehavior" is "Off" in config.json), this command does nothing.
* `$secid <section-id>`: Sets your override section ID. After running this command, any games you create will use your override section ID for rare drops instead of your character's actual section ID. If you're in a game and you are the leader of the game, this also immediately changes the item tables used by the server when creating items. To revert to your actual section id, run `$secid` with no name after it. On the proxy server, this will not work if the remote server controls item drops (e.g. on BB, or on Schtserv with server drops enabled). If the server does not allow cheat mode anywhere (that is, "CheatModeBehavior" is "Off" in config.json), this command does nothing.
* `$rand <seed>`: Sets your override random seed (specified as a 32-bit hex value). This will make any games you create use the given seed for rare enemies. This also makes item drops deterministic in Blue Burst games hosted by newserv. On the proxy server, this command can cause desyncs with other players in the same game, since they will not see the overridden random seed. To remove the override, run `$rand` with no arguments. If the server does not allow cheat mode anywhere (that is, "CheatModeBehavior" is "Off" in config.json), this command does nothing.
* `$ln [name-or-type]`: Sets the lobby number. Visible only to you. This command exists because some non-lobby maps can be loaded as lobbies with invalid lobby numbers. See the "GC lobby types" and "Ep3 lobby types" entries in the information menu for acceptable values here. Note that non-lobby maps do not have a lobby counter, so there's no way to exit the lobby without using either `$ln` again or `$exit`. On the game server, `$ln` reloads the lobby immediately; on the proxy server, it doesn't take effect until you load another lobby yourself (which means you'll like have to use `$exit` to escape). Run this command with no argument to return to the default lobby.
* `$swa`: Enables or disables switch assist. When enabled, the server will attempt to automatically unlock two-player doors in non-quest games if you step on both switches sequentially.
* `$swa`: Enables or disables switch assist. When enabled, the server will attempt to automatically unlock two-player and four-player doors in non-quest games if you step on all the required switches sequentially.
* `$exit`: If you're in a lobby, sends you to the main menu (which ends your proxy session, if you're in one). If you're in a game or spectator team, sends you to the lobby (but does not end your proxy session if you're in one). Does nothing if you're in a non-Episode 3 game and no quest is in progress.
* `$patch <name>`: Run a patch on your client. `<name>` must exactly match the name of a patch on the server.
@@ -413,12 +468,12 @@ Some commands only work on the game server and not on the proxy server. The chat
* `$minlevel <level>`: Sets the minimum level for players to join the current game.
* `$password <password>`: Sets the game's join password. To unlock the game, run `$password` with nothing after it.
* `$dropmode [mode]`: Changes the way item drops behave in the current game. `mode` can be `none`, `client`, `shared`, `private`, or `duplicate`. If `mode` is not given, tells you the current drop mode without changing it. See the "Item tables and drop modes" section for more information.
* `$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 state of enemies and objects on the map will be reset when the last player leaves, but dropped items will not be deleted. If the game is empty for too long (15 minutes by default), it is then deleted.
* `$persist`: Enable or disable persistence for the current game. When persistence is on, the game will not be deleted when the last player leaves. The states of enemies, objects, and switches will be saved, and items left on the floor will not be deleted (except items only visible to the leaving player). If the game is empty for too long (15 minutes by default), it is then deleted. There is an edge case with persistence: if the player defeats a boss, leaves the room, joins again, and returns to the boss arena, neither the boss nor the exit warp will spawn, so they will be stuck there and have to use $warp or Quit Game to get out. For this reason, `$warp 0` is allowed in boss arenas once the boss is defeated, even if cheat mode is disabled.
* Episode 3 commands (game server only)
* `$spec`: Toggles the allow spectators flag for Episode 3 games. If any players are spectating when this flag is disabled, they will be sent back to the lobby.
* `$inftime`: Toggles infinite-time mode. Must be used before starting a battle. If infinite-time mode is enabled, the overall and per-phase time limits will be disabled regardless of the values chosen during battle setup. After completing a battle, infinite-time mode is reset to the server's default value (which can be set in Episode3BehaviorFlags in config.json).
* `$defrange <min>-<max>`: Sets the DEF dice range for the next battle. If this is used, the dice range set during battle rules setup will apply only to ATK dice; DEF dice will use this range instead. Assist cards and other dice effects will still apply. Dice exchange also still applies if it is enabled.
* `$dicerange [d:L-H] [1:L-H] [a1:L-H] [d1:L-H]`: Sets override dice ranges for the next battle. The min and max dice values from the rules setup menu always apply to the ATK dice, but you can specify a different range for the DEF dice with `d:2-4` (for example). The `1:` override applies to the 1-player team in a 2v1 game (so you would set the 2-player team's desired dice range in the rules menu). You can also specify the 1-player team's ATK and DEF ranges separately with the `a1:` and `d1:` overrides. Note that these ranges will only be used if the chosen map or quest does not override them.
* `$stat <what>`: Shows a statistic about your player or team in the current battle. `<what>` can be `duration`, `fcs-destroyed`, `cards-destroyed`, `damage-given`, `damage-taken`, `opp-cards-destroyed`, `own-cards-destroyed`, `move-distance`, `cards-set`, `fcs-set`, `attack-actions-set`, `techs-set`, `assists-set`, `defenses-self`, `defenses-ally`, `cards-drawn`, `max-attack-damage`, `max-combo`, `attacks-given`, `attacks-taken`, `sc-damage`, `damage-defended`, or `rank`.
* `$surrender`: Causes your team to immediately lose the current battle.
* `$saverec <name>`: Saves the recording of the last battle.
@@ -432,6 +487,7 @@ Some commands only work on the game server and not on the proxy server. The chat
* `$next`: Warps yourself to the next floor.
* `$item <desc>` (or `$i <desc>`): Create an item. `desc` may be a description of the item (e.g. "Hell Saber +5 0/10/25/0/10") or a string of hex data specifying the item code. Item codes are 16 hex bytes; at least 2 bytes must be specified, and all unspecified bytes are zeroes. If you are on the proxy server, you must not be using Blue Burst for this command to work. On the game server, this command works for all versions.
* `$unset <index>` (game server only): In an Episode 3 battle, removes one of your set cards from the field. `<index>` is the index of the set card as it appears on your screen - 1 is the card next to your SC's icon, 2 is the card to the right of 1, etc. This does not cause a Hunters-side SC to lose HP, as they normally do when their items are destroyed.
* `$dropmode [mode]` (proxy server): Changes the way item drops behave in the current game, if you are not on BB. Unlike the game server version of this command, using this on the proxy server requires cheats to be enabled. This works by intercepting the drop requests sent to and from the leader. (So, if you are the leader and not using server drop mode on the remote server, it affects the entire game; otherwise, it affects only items generated by your actions.) `mode` can be `none` (no drops), `default` (normal drops), or `proxy` (use newserv's drop tables instead of the remote server's). If `mode` is not given, tells you the current drop mode without changing it.
* Configuration commands
* `$event <event>`: Sets the current holiday event in the current lobby. Holiday events are documented in the "Using $event" item in the information menu. If you're on the proxy server, this applies to all lobbies and games you join, but only you will see the new event - other players will not.
@@ -439,7 +495,8 @@ Some commands only work on the game server and not on the proxy server. The chat
* `$song <song-id>` (Episode 3 only): Plays a specific song in the current lobby.
* Administration commands (game server only)
* `$ann <message>`: Sends an announcement message. The message text is sent to all players in all games and lobbies.
* `$ann <message>`: Sends an announcement message. The message is sent as temporary on-screen text to all players in all games and lobbies.
* `$ann! <message>`: Sends an announcement message. The message is sent as a Simple Mail message to all players in all games and lobbies.
* `$ax <message>`: Sends a message to the server's terminal. This cannot be used to run server shell commands; it only prints text to stderr.
* `$silence <identifier>`: Silences a player (remove their ability to chat) or unsilences a player. The identifier may be the player's name or Guild Card number.
* `$kick <identifier>`: Disconnects a player. The identifier may be the player's name or Guild Card number.
+5 -2
View File
@@ -4,7 +4,8 @@
- Implement decrypt/encrypt actions for VMS files
- 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 ad the top of the class definition)
- Clean up ItemParameterTable implementation (see comment at the top of the class definition)
- Handle MeetUserExtensions properly in 41 and C4 commands on the proxy (rewrite the embedded 19 command and store a map of )
## PSO DC
@@ -15,7 +16,7 @@
- Enforce tournament deck restrictions (e.g. rank checks, No Assist option) when populating COMs at tournament start time
- Make `reload licenses` not vulnerable to online players' licenses overwriting licenses on disk somehow
- Implement ranks (based on total Meseta earned)
- Support Trial Edition battles
- Make an AR code that gets rid of the SAMPLE overlays on NTE
## PSO XBOX
@@ -26,3 +27,5 @@
- Test all quest item subcommands
- 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.)
+114 -34
View File
@@ -1,29 +1,83 @@
(Ep1&2 USA) Unlock all songs in BGM test
Unlock all songs in BGM test
(Note: sadly, there are no secret/unused ones)
04368960 38600001
04368964 4E800020
Ep12-JP12 => 04367A68 38600001
04367A6C 4E800020
Ep12-JP13 => 04368ED8 38600001
04368EDC 4E800020
Ep12-JP14 => 0436A434 38600001
0436A438 4E800020
Ep12-JP15 => 0436A1E8 38600001
0436A1EC 4E800020
Ep12-US10 => 0436891C 38600001
04368920 4E800020
Ep12-US11 => 04368960 38600001
04368964 4E800020
Ep12-US12 => 0436A5B4 38600001
0436A5B8 4E800020
Ep12-EU => 043699A8 38600001
043699AC 4E800020
Ep3-NTE => 041EA948 38600001
041EA94C 4E800020
Ep3-JP => 041D8CF0 38600001
041D8CF4 4E800020
Ep3-US => 041D8D7C 38600001
041D8D80 4E800020
Ep3-EU => 041D93F0 38600001
041D93F4 4E800020
(Ep1&2 USA v1.1) Play lobby (and event) music on Pioneer 2 also
0417E0F0 60000000
Play lobby (and event) music in Morgue also
Ep12-JP12 => 0417DD34 60000000
Ep12-JP13 => 0417E0E8 60000000
Ep12-JP14 => 0417E24C 60000000
Ep12-JP15 => 0417E1AC 60000000
Ep12-US10 => 0417E0F0 60000000
Ep12-US11 => 0417E0F0 60000000
Ep12-US12 => 0417E210 60000000
Ep12-EU => 0417E6D4 60000000
Ep3-NTE => 040B8C7C 60000000
Ep3-US => 040B7028 60000000
Ep3-JP => 040B7044 60000000
Ep3-EU => 040B746C 60000000
(Ep3 USA) Play lobby (and event) music in Morgue also
040B7028 60000000
Skip white logo screens during startup
Ep12-JP12 => 0413EE54 38000007
Ep12-JP13 => 0413F1DC 38000007
Ep12-JP14 => 0413F338 38000007
Ep12-JP15 => 0413F298 38000007
Ep12-US10 => 0413F190 38000007
Ep12-US11 => 0413F190 38000007
Ep12-US12 => 0413F2A8 38000007
Ep12-EU => 0413F524 38000007
Ep3-NTE => 0409E10C 38000007
Ep3-JP => 0409D810 38000007
Ep3-US => 0409D774 38000007
Ep3-EU => 0409D9A4 38000007
(Ep3 USA) Skip white logo screens during startup
0409D774 38000007
(Episodes 1&2 USA v1.1) Skip white logo screens during startup
0413F190 38000007
Skip agreement prompts before online game
Ep12-JP12 => 0432737C 38000003
Ep12-JP13 => 043283CC 38000003
Ep12-JP14 => 043298E8 38000003
Ep12-JP15 => 04329690 38000003
Ep12-US10 => 04327D3C 38000003
Ep12-US11 => 04327D80 38000003
Ep12-US12 => 0432984C 38000003
Ep12-EU => 04328C58 38000003
Ep3-NTE => 041C67D0 38000003
Ep3-JP => 041B5234 38000003
Ep3-US => 041B50C8 38000003
Ep3-EU => 041B574C 38000003
(Ep3 USA) Skip agreement prompts before online game
041B50C8 38000003
(Episodes 1&2 USA v1.1) Skip agreement prompt before online game
04327D80 38000003
Disable rate limit for pressing A during loading screens
Ep3-NTE => 042E1030 38000000
Ep3-JP => 042F8BE4 38000000
Ep3-US => 042F9B30 38000000
Ep3-EU => 042FA734 38000000
(Ep3 USA) Disable rate limit for pressing A during loading screens
042F9B30 38000000
(Ep3 USA) Auto-press A as fast as possible during loading screens
042F9AC0 60000000
Auto-press A as fast as possible during loading screens
Ep3-EU => 042FA6C4 60000000
Ep3-US => 042F9AC0 60000000
Ep3-NTE => 040C2C48 60000000
Ep3-JP => 042F8B74 60000000
(Ep3 USA) Replace loading screen A button sounds with random sounds
042F9B18 4804BB19
@@ -32,6 +86,13 @@
042F9B24 64630005
042F9B28 38800000
(Ep3 NTE) Replace loading screen A button sounds with random sounds
042E1018 480309A9
042E101C 5463063E
042E1020 60631400
042E1024 64630005
042E1028 38800000
(Ep3 USA) Change color of loading screens
(Replace AA, RR, GG, BB appropriately)
042FA704 3CC0AARR
@@ -43,11 +104,16 @@
0400BD64 EC5D00B2
0400BD68 4E800020
(Ep3 USA) Disable darkening effect during battle details mode
042F951C 4E800020
Disable darkening effect during battle details mode
Ep3-NTE => 042E09D8 4E800020
Ep3-JP => 042F85D0 4E800020
Ep3-US => 042F951C 4E800020
Ep3-EU => 042FA120 4E800020
(Ep3 USA) Unlock all COM decks
042CA908 38600001
Unlock all COM decks
Ep3-JP => 042C9B34 38600001
Ep3-EU => 042CB414 38600001
Ep3-US => 042CA908 38600001
(Ep3 USA) Enable all lobby counter options in non-CARD lobbies
04096A8C 480000C0
@@ -103,8 +169,11 @@
040002BC 7C633050
040002C0 4E800020
(Ep3 USA) Unlock all offline free battle maps
042CAA00 38600001
Unlock all offline free battle maps
Ep3-NTE => 042BE538 38600001
Ep3-JP => 042C9C2C 38600001
Ep3-EU => 042CB50C 38600001
Ep3-US => 042CAA00 38600001
(This unlocks ALL maps, including a bunch of maps with garbage names that crash if you try to play them)
(Ep3 USA) Talk to auction counter offline to get all cards
@@ -165,8 +234,10 @@
025CB6AA 00000001
TODO: Figure out more debug message conditionals (vars/functions) and add them here
(Episode 3 USA) Able to find VIP cards offline (but they're still rare)
042C0B20 4800000C
Able to find VIP cards offline (but they're still rare)
Ep3-EU => 042C15DC 4800000C
Ep3-JP => 042BFE24 4800000C
Ep3-US => 042C0B20 4800000C
(Ep3 USA) Hold L when starting battle to enter debug menu
042C5460 4BD3AF78
@@ -181,8 +252,11 @@ TODO: Figure out more debug message conditionals (vars/functions) and add them h
040003F8 3800001A
040003FC 482C5068
(Ep3 USA) Dressing room always accessible
041A16FC 38600001
Dressing room always accessible
Ep3-NTE => 041B2A2C 38600001
Ep3-JP => 041A1920 38600001
Ep3-EU => 041A1C84 38600001
Ep3-US => 041A16FC 38600001
(Ep3 USA) Full dressing room v1
Can't change your class, but you start with your existing appearance
@@ -201,8 +275,11 @@ Go online with this code on after using the dressing room to fully save changes
(Ep3 USA) Replace Options menu with debug menu
04149E70 38600019
(Ep3 USA) Jukebox is free
0430D1DC 48000024
Jukebox is free
Ep3-NTE => 042248C4 48000024 (useless because the jukebox isn't loaded in NTE, but apparently the code for it exists)
Ep3-JP => 0430C178 48000024
Ep3-US => 0430D1DC 48000024
Ep3-EU => 0430DE3C 48000024
(Ep3 USA) Use own character in battle (online only)
041FFAB0 4800001C
@@ -233,8 +310,11 @@ Go online with this code on after using the dressing room to fully save changes
0412F8D4 7D0803A6
0412F8D8 4BEDEBF4
(Ep3 USA) Metal tiles don't appear in Simulator map
04296904 4E800020
Metal tiles don't appear in Simulator map
Ep3-NTE => 0428FED8 4E800020
Ep3-JP => 04296054 4E800020
Ep3-US => 04296904 4E800020
Ep3-EU => 04297278 4E800020
(Ep3 USA) Enable Boooo and Laughter soundchat sounds
Note: Without a TextEnglish.pr2/pr3 patch, the menu items for these sounds will be blank (but they will still work)
+929 -910
View File
File diff suppressed because it is too large Load Diff
+4
View File
@@ -9,6 +9,10 @@ inline void run_ar_code_translator(const std::string&, const std::string&, const
throw std::runtime_error("resource_file is not available; install it and rebuild newserv");
}
inline void run_xbe_patch_translator(const std::string&, const std::string&, const std::string&) {
throw std::runtime_error("resource_file is not available; install it and rebuild newserv");
}
inline std::vector<std::pair<uint32_t, std::string>> diff_dol_files(const std::string&, const std::string&) {
throw std::runtime_error("resource_file is not available; install it and rebuild newserv");
}
+242 -3
View File
@@ -5,6 +5,7 @@
#include <phosg/Filesystem.hh>
#include <phosg/Strings.hh>
#include <resource_file/ExecutableFormats/DOLFile.hh>
#include <resource_file/ExecutableFormats/XBEFile.hh>
using namespace std;
@@ -63,11 +64,11 @@ public:
}
void set_source_file(const string& filename) {
this->src_filename = filename;
this->src_file = files.at(this->src_filename);
this->src_file = this->files.at(this->src_filename);
}
void find_rtoc_global_regs() const {
for (const auto& it : files) {
for (const auto& it : this->files) {
bool r2_high_found = false;
bool r2_low_found = false;
bool r13_high_found = false;
@@ -258,7 +259,7 @@ public:
}
unordered_map<string, uint32_t> results;
for (const auto& it : files) {
for (const auto& it : this->files) {
if (it.second == this->src_file) {
log.info("(%s) %08" PRIX32 " (from source)", it.first.c_str(), src_addr);
results.emplace(it.first, src_addr);
@@ -349,6 +350,231 @@ private:
shared_ptr<const DOLFile> src_file;
};
class XBEPatchTranslator {
public:
enum class ExpandMethod {
FORWARD = 0,
BACKWARD,
BOTH,
};
static const char* name_for_expand_method(ExpandMethod method) {
switch (method) {
case ExpandMethod::FORWARD:
return "FORWARD";
case ExpandMethod::BACKWARD:
return "BACKWARD";
case ExpandMethod::BOTH:
return "BOTH";
default:
throw logic_error("invalid expand method");
}
}
XBEPatchTranslator(const string& directory)
: log("[xbe-trans] "),
directory(directory) {
while (ends_with(this->directory, "/")) {
this->directory.pop_back();
}
for (const auto& filename : list_directory(this->directory)) {
if (ends_with(filename, ".xbe")) {
string name = filename.substr(0, filename.size() - 4);
string path = directory + "/" + filename;
this->files.emplace(name, make_shared<XBEFile>(path.c_str()));
this->log.info("Loaded %s", name.c_str());
}
}
}
~XBEPatchTranslator() = default;
const string& get_source_filename() const {
return this->src_filename;
}
void set_source_file(const string& filename) {
this->src_filename = filename;
this->src_file = this->files.at(this->src_filename);
}
uint32_t find_match(
shared_ptr<const XBEFile> dest_file,
uint32_t src_address,
uint32_t src_size,
ExpandMethod expand_method) const {
if (!this->src_file) {
throw runtime_error("no source file selected");
}
const XBEFile::Section* src_section = nullptr;
for (const auto& sec : this->src_file->sections) {
if (src_address >= sec.addr && src_address < sec.addr + sec.file_size) {
src_section = &sec;
break;
}
}
if (!src_section) {
throw runtime_error("source address not within any section");
}
const char* method_token = this->name_for_expand_method(expand_method);
size_t src_offset = src_address - src_section->addr;
size_t src_bytes_available_before = src_offset;
size_t src_bytes_available_after = src_section->file_size - src_offset - src_size;
this->log.info("(find_match/%s) Source offset = %08zX with %zX/%zX bytes available before/after",
method_token, src_offset, src_bytes_available_before, src_bytes_available_after);
size_t match_bytes_before = 0;
size_t match_bytes_after = 0;
while (match_bytes_before + match_bytes_after + src_size < 0x100) {
size_t num_matches = 0;
size_t last_match_address = 0;
size_t match_length = match_bytes_before + match_bytes_after + src_size;
auto src_r = this->src_file->read_from_addr(src_section->addr + src_offset - match_bytes_before, match_length);
for (const auto& dest_section : dest_file->sections) {
for (size_t dest_match_offset = 0; dest_match_offset + match_length <= dest_section.file_size; dest_match_offset++) {
src_r.go(0);
StringReader dest_r = dest_file->read_from_addr(dest_section.addr + dest_match_offset, match_length);
size_t z;
for (z = 0; z < match_length; z++) {
uint8_t src_data = src_r.get_u8();
uint8_t dest_data = dest_r.get_u8();
if (src_data != dest_data) {
break;
}
}
if (z == match_length) {
num_matches++;
last_match_address = dest_section.addr + dest_match_offset + match_bytes_before;
}
}
}
this->log.info("(find_match/%s) For match length %zX, %zu matches found", method_token, match_length, num_matches);
if (num_matches == 1) {
return last_match_address;
} else if (num_matches == 0) {
throw runtime_error("did not find exactly one match");
}
bool can_expand_backward = false;
bool can_expand_forward = false;
switch (expand_method) {
case ExpandMethod::BACKWARD:
can_expand_backward = (src_bytes_available_before > match_bytes_before);
break;
case ExpandMethod::FORWARD:
can_expand_forward = (src_bytes_available_after > match_bytes_after);
break;
case ExpandMethod::BOTH:
can_expand_backward = (src_bytes_available_before > match_bytes_before);
can_expand_forward = (src_bytes_available_after > match_bytes_after);
break;
default:
throw logic_error("invalid expand method");
}
if (!can_expand_backward && !can_expand_forward) {
throw runtime_error("no further expansion is allowed");
}
if (can_expand_backward) {
match_bytes_before++;
}
if (can_expand_forward) {
match_bytes_after++;
}
}
throw runtime_error("scan field too long; too many matches");
}
void find_all_matches(uint32_t src_addr, uint32_t src_size) const {
if (!this->src_file) {
throw runtime_error("no source file selected");
}
unordered_map<string, uint32_t> results;
for (const auto& it : files) {
if (it.second == this->src_file) {
log.info("(%s) %08" PRIX32 " (from source)", it.first.c_str(), src_addr);
results.emplace(it.first, src_addr);
} else {
array<future<uint32_t>, 3> futures;
static const array<ExpandMethod, 3> methods = {
ExpandMethod::FORWARD,
ExpandMethod::BACKWARD,
ExpandMethod::BOTH,
};
for (size_t z = 0; z < methods.size(); z++) {
futures[z] = async(&XBEPatchTranslator::find_match, this, it.second, src_addr, src_size, methods[z]);
}
unordered_set<uint32_t> match_addrs;
for (size_t z = 0; z < futures.size(); z++) {
const char* method_name = this->name_for_expand_method(methods[z]);
try {
uint32_t ret = futures[z].get();
log.info("(%s) (%s) %08" PRIX32, it.first.c_str(), method_name, ret);
match_addrs.emplace(ret);
} catch (const exception& e) {
log.error("(%s) (%s) failed: %s", it.first.c_str(), method_name, e.what());
}
}
if (match_addrs.empty()) {
log.error("(%s) no match found", it.first.c_str());
} else if (match_addrs.size() > 1) {
log.error("(%s) different matches found by different methods", it.first.c_str());
} else {
results.emplace(it.first, *match_addrs.begin());
}
}
}
for (const auto& it : results) {
fprintf(stdout, "%s => %08" PRIX32 "\n", it.first.c_str(), it.second);
}
}
void handle_command(const string& command) {
auto tokens = split(command, ' ');
if (tokens.empty()) {
throw runtime_error("no command given");
}
strip_trailing_whitespace(tokens[tokens.size() - 1]);
if (tokens[0] == "use") {
this->set_source_file(tokens.at(1));
} else if (tokens[0] == "match") {
this->find_all_matches(stoul(tokens.at(1), nullptr, 16), stoul(tokens.at(2), nullptr, 16));
} else if (!tokens[0].empty()) {
throw runtime_error("unknown command");
}
}
void run_shell() {
while (!feof(stdin)) {
if (!this->src_filename.empty()) {
fprintf(stdout, "ar-trans:%s/%s> ", this->directory.c_str(), this->src_filename.c_str());
} else {
fprintf(stdout, "ar-trans:%s> ", this->directory.c_str());
}
fflush(stdout);
string command = fgets(stdin);
try {
this->handle_command(command);
} catch (const exception& e) {
this->log.error("Failed: %s", e.what());
}
}
fputc('\n', stdout);
}
private:
PrefixedLogger log;
string directory;
unordered_map<string, shared_ptr<const XBEFile>> files;
string src_filename;
shared_ptr<const XBEFile> src_file;
};
void run_ar_code_translator(const std::string& directory, const std::string& use_filename, const std::string& command) {
ARCodeTranslator trans(directory);
if (!use_filename.empty()) {
@@ -362,6 +588,19 @@ void run_ar_code_translator(const std::string& directory, const std::string& use
}
}
void run_xbe_patch_translator(const std::string& directory, const std::string& use_filename, const std::string& command) {
XBEPatchTranslator trans(directory);
if (!use_filename.empty()) {
trans.set_source_file(use_filename);
}
if (!command.empty()) {
trans.handle_command(command);
} else {
trans.run_shell();
}
}
vector<pair<uint32_t, string>> diff_dol_files(const string& a_filename, const string& b_filename) {
DOLFile a(a_filename.c_str());
DOLFile b(b_filename.c_str());
+2 -1
View File
@@ -6,5 +6,6 @@
#include <utility>
#include <vector>
void run_ar_code_translator(const std::string& initial_directory, const std::string& use_file, const std::string& command);
void run_ar_code_translator(const std::string& directory, const std::string& use_filename, const std::string& command);
void run_xbe_patch_translator(const std::string& directory, const std::string& use_filename, const std::string& command);
std::vector<std::pair<uint32_t, std::string>> diff_dol_files(const std::string& a_filename, const std::string& b_filename);
+1 -1
View File
@@ -20,7 +20,7 @@ void BattleParamsIndex::Table::print(FILE* stream) const {
e.char_stats.dfp.load(),
e.char_stats.ata.load(),
e.char_stats.lck.load(),
e.unknown_a1.load(),
e.esp.load(),
e.experience.load(),
e.meseta.load());
};
+1 -1
View File
@@ -24,7 +24,7 @@ public:
/* 04 */ le_int16_t ata_bonus;
/* 06 */ le_uint16_t unknown_a4;
/* 08 */ le_float distance_x;
/* 0C */ le_float angle_x;
/* 0C */ le_uint32_t angle_x; // Out of 0x10000 (high 16 bits are unused)
/* 10 */ le_float distance_y;
/* 14 */ le_uint16_t unknown_a8;
/* 16 */ le_uint16_t unknown_a9;
+65 -14
View File
@@ -31,21 +31,19 @@
using namespace std;
CatSession::exit_shell::exit_shell() : runtime_error("shell exited") {}
CatSession::CatSession(
shared_ptr<struct event_base> base,
const struct sockaddr_storage& remote,
Version version,
shared_ptr<const PSOBBEncryption::KeyFile> bb_key_file)
: Shell(base),
log(string_printf("[CatSession:%s] ", name_for_enum(version)), proxy_server_log.min_level),
channel(
version,
1,
CatSession::dispatch_on_channel_input,
CatSession::dispatch_on_channel_error,
this,
"CatSession"),
: log(string_printf("[CatSession:%s] ", 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");
}
@@ -64,6 +62,14 @@ CatSession::CatSession(
reinterpret_cast<const sockaddr*>(&remote), sizeof(struct sockaddr_in)) != 0) {
throw runtime_error(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 = parse_data_string(command, nullptr, ParseDataFlags::ALLOW_FILES);
send_command_with_header(this->channel, full_cmd.data(), full_cmd.size());
}
void CatSession::dispatch_on_channel_input(
@@ -129,9 +135,54 @@ void CatSession::on_channel_error(short events) {
}
}
void CatSession::print_prompt() {}
void CatSession::execute_command(const std::string& command) {
string full_cmd = parse_data_string(command, nullptr, ParseDataFlags::ALLOW_FILES);
send_command_with_header(this->channel, full_cmd.data(), full_cmd.size());
void CatSession::dispatch_read_stdin(evutil_socket_t, short, void* ctx) {
reinterpret_cast<CatSession*>(ctx)->read_stdin();
}
void CatSession::read_stdin() {
bool any_command_read = false;
for (;;) {
auto poll_result = this->poll.poll();
short fd_events = 0;
try {
fd_events = poll_result.at(0);
} catch (const out_of_range&) {
}
if (!(fd_events & POLLIN)) {
break;
}
string command(2048, '\0');
if (!fgets(command.data(), command.size(), stdin)) {
if (!any_command_read) {
// ctrl+d probably; we should exit
fputc('\n', stderr);
event_base_loopexit(this->base.get(), nullptr);
return;
} else {
break; // probably not EOF; just no more commands for now
}
}
// trim the extra data off the string
size_t len = strlen(command.c_str());
if (len == 0) {
break;
}
if (command[len - 1] == '\n') {
len--;
}
command.resize(len);
any_command_read = true;
try {
execute_command(command);
} catch (const exit_shell&) {
event_base_loopexit(this->base.get(), nullptr);
return;
} catch (const exception& e) {
fprintf(stderr, "FAILED: %s\n", e.what());
}
}
}
+18 -5
View File
@@ -14,28 +14,41 @@
#include "PSOEncryption.hh"
#include "PSOProtocol.hh"
#include "ServerState.hh"
#include "Shell.hh"
class CatSession : public Shell {
class CatSession {
public:
CatSession(
std::shared_ptr<struct event_base> base,
const struct sockaddr_storage& remote,
Version version,
std::shared_ptr<const PSOBBEncryption::KeyFile> bb_key_file);
CatSession(const CatSession&) = delete;
CatSession(CatSession&&) = delete;
CatSession& operator=(const CatSession&) = delete;
CatSession& operator=(CatSession&&) = delete;
virtual ~CatSession() = default;
protected:
PrefixedLogger log;
std::shared_ptr<struct event_base> base;
std::unique_ptr<struct event, void (*)(struct event*)> read_event;
Poll poll;
Channel channel;
std::shared_ptr<const PSOBBEncryption::KeyFile> bb_key_file;
virtual void print_prompt();
class exit_shell : public std::runtime_error {
public:
exit_shell();
~exit_shell() = default;
};
virtual void execute_command(const std::string& command);
static void dispatch_on_channel_input(
Channel& ch, uint16_t command, uint32_t flag, std::string& msg);
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();
};
+199 -63
View File
@@ -96,11 +96,8 @@ static void check_is_leader(shared_ptr<Lobby> l, shared_ptr<Client> c) {
static void server_command_server_info(shared_ptr<Client> c, const std::string&) {
auto s = c->require_server_state();
string uptime_str = format_duration(now() - s->creation_time);
string build_date = format_time(BUILD_TIMESTAMP);
send_text_message_printf(c,
"Revision: $C6%s$C7\n$C6%s$C7\nUptime: $C6%s$C7\nLobbies: $C6%zu$C7\nClients: $C6%zu$C7(g) $C6%zu$C7(p)",
GIT_REVISION_HASH,
build_date.c_str(),
"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(),
@@ -122,7 +119,7 @@ static void server_command_lobby_info(shared_ptr<Client> c, const std::string&)
} else {
lines.emplace_back(string_printf("$C6%08X$C7 L$C6%d-%d$C7", l->lobby_id, l->min_level + 1, l->max_level + 1));
}
lines.emplace_back(string_printf("$C7Section ID: $C6%s$C7", name_for_section_id(l->section_id)));
lines.emplace_back(string_printf("$C7Section ID: $C6%s$C7", name_for_section_id(l->effective_section_id())));
switch (l->drop_mode) {
case Lobby::DropMode::DISABLED:
@@ -268,6 +265,12 @@ static void server_command_announce(shared_ptr<Client> c, const std::string& arg
send_text_message(s, args);
}
static void server_command_announce_mail(shared_ptr<Client> c, const std::string& args) {
auto s = c->require_server_state();
check_license_flag(c, License::Flag::ANNOUNCE);
send_simple_mail(s, 0, s->name, args);
}
static void server_command_arrow(shared_ptr<Client> c, const std::string& args) {
auto l = c->require_lobby();
c->lobby_arrow_color = stoull(args, nullptr, 0);
@@ -308,10 +311,22 @@ static void server_command_qcheck(shared_ptr<Client> c, const std::string& args)
auto l = c->require_lobby();
uint16_t flag_num = stoul(args, nullptr, 0);
send_text_message_printf(c, "$C7Quest flag 0x%hX (%hu)\nis %s on %s",
flag_num, flag_num,
c->character()->quest_flags.get(l->difficulty, flag_num) ? "set" : "not set",
name_for_difficulty(l->difficulty));
if (l->is_game()) {
if (!l->quest_flags_known || l->quest_flags_known->get(l->difficulty, flag_num)) {
send_text_message_printf(c, "$C7Game: flag 0x%hX (%hu)\nis %s on %s",
flag_num, flag_num,
c->character()->quest_flags.get(l->difficulty, flag_num) ? "set" : "not set",
name_for_difficulty(l->difficulty));
} else {
send_text_message_printf(c, "$C7Game: flag 0x%hX (%hu)\nis unknown on %s",
flag_num, flag_num, name_for_difficulty(l->difficulty));
}
} else if (c->version() == Version::BB_V4) {
send_text_message_printf(c, "$C7Player: flag 0x%hX (%hu)\nis %s on %s",
flag_num, flag_num,
c->character()->quest_flags.get(l->difficulty, flag_num) ? "set" : "not set",
name_for_difficulty(l->difficulty));
}
}
static void server_command_qset_qclear(shared_ptr<Client> c, const std::string& args, bool should_set) {
@@ -327,10 +342,22 @@ static void server_command_qset_qclear(shared_ptr<Client> c, const std::string&
uint16_t flag_num = stoul(args, nullptr, 0);
if (should_set) {
c->character()->quest_flags.set(l->difficulty, flag_num);
} else {
c->character()->quest_flags.clear(l->difficulty, flag_num);
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);
}
} else if (c->version() == Version::BB_V4) {
auto p = c->character();
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(c->version())) {
@@ -362,7 +389,7 @@ static void proxy_command_qset_qclear(shared_ptr<ProxyServer::LinkedSession> ses
ses->client_channel.send(0x60, 0x00, &cmd, sizeof(cmd));
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}, ses->difficulty, 0x0000};
G_UpdateQuestFlag_V3_BB_6x75 cmd = {{{0x75, 0x03, 0x0000}, flag_num, should_set ? 0 : 1}, ses->lobby_difficulty, 0x0000};
ses->client_channel.send(0x60, 0x00, &cmd, sizeof(cmd));
ses->server_channel.send(0x60, 0x00, &cmd, sizeof(cmd));
}
@@ -570,8 +597,7 @@ static void server_command_patch(shared_ptr<Client> c, const std::string& args)
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->name_and_specific_version_to_patch_function.at(
string_printf("%s-%08" PRIX32, args.c_str(), c->config.specific_version));
auto fn = s->function_code_index->get_patch(args, c->config.specific_version);
send_function_call(c, fn);
c->function_call_response_queue.emplace_back(empty_function_call_response_handler);
} catch (const out_of_range&) {
@@ -590,8 +616,7 @@ static void proxy_command_patch(shared_ptr<ProxyServer::LinkedSession> ses, cons
ses->log.info("Version detected as %08" PRIX32, ses->config.specific_version);
}
auto s = ses->require_server_state();
auto fn = s->function_code_index->name_and_specific_version_to_patch_function.at(
string_printf("%s-%08" PRIX32, args.c_str(), ses->config.specific_version));
auto fn = s->function_code_index->get_patch(args, ses->config.specific_version);
send_function_call(ses->client_channel, ses->config, fn);
// Don't forward the patch response to the server
ses->function_call_return_handler_queue.emplace_back(empty_patch_return_handler);
@@ -657,6 +682,7 @@ static void server_command_exit(shared_ptr<Client> c, const std::string&) {
G_UnusedHeader cmd = {0x73, 0x01, 0x0000};
c->channel.send(0x60, 0x00, cmd);
c->floor = 0;
c->recent_switch_flags.clear();
} else if (is_ep3(c->version())) {
c->channel.send(0xED, 0x00);
} else {
@@ -924,21 +950,28 @@ static void server_command_meseta(shared_ptr<Client> c, const std::string& args)
static void server_command_secid(shared_ptr<Client> c, const std::string& args) {
auto l = c->require_lobby();
check_is_game(l, false);
check_cheats_allowed(c->require_server_state(), c);
uint8_t new_override_section_id;
if (!args[0]) {
c->config.override_section_id = 0xFF;
new_override_section_id = 0xFF;
send_text_message(c, "$C6Override section ID\nremoved");
} else {
uint8_t new_secid = section_id_for_name(args);
if (new_secid == 0xFF) {
new_override_section_id = section_id_for_name(args);
if (new_override_section_id == 0xFF) {
send_text_message(c, "$C6Invalid section ID");
return;
} else {
c->config.override_section_id = new_secid;
send_text_message_printf(c, "$C6Override section ID\nset to %s", name_for_section_id(new_secid));
send_text_message_printf(c, "$C6Override section ID\nset to %s", name_for_section_id(new_override_section_id));
}
}
c->config.override_section_id = new_override_section_id;
if (l->is_game() && (l->leader_id == c->lobby_client_id)) {
l->override_section_id = new_override_section_id;
l->change_section_id();
}
}
static void proxy_command_secid(shared_ptr<ProxyServer::LinkedSession> ses, const std::string& args) {
@@ -957,6 +990,22 @@ static void proxy_command_secid(shared_ptr<ProxyServer::LinkedSession> ses, cons
}
}
static void server_command_variations(shared_ptr<Client> c, const std::string& args) {
// 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 = c->require_server_state();
auto l = c->require_lobby();
check_is_game(l, false);
check_cheats_allowed(s, c);
c->override_variations = make_unique<parray<le_uint32_t, 0x20>>();
c->override_variations->clear(0);
for (size_t z = 0; z < min<size_t>(c->override_variations->size(), args.size()); z++) {
c->override_variations->at(z) = args[z] - '0';
}
}
static void server_command_rand(shared_ptr<Client> c, const std::string& args) {
auto s = c->require_server_state();
auto l = c->require_lobby();
@@ -1129,9 +1178,7 @@ static void server_command_edit(shared_ptr<Client> c, const std::string& args) {
}
} else if (tokens.at(0) == "name") {
vector<string> orig_tokens = split(args, ' ');
string name = ((p->inventory.language == 0) ? "\tE" : "\tJ") + orig_tokens.at(1);
p->disp.name.clear();
p->disp.name.encode(name, p->inventory.language);
p->disp.name.encode(orig_tokens.at(1), p->inventory.language);
} else if (tokens.at(0) == "npc") {
if (tokens.at(1) == "none") {
p->disp.visual.extra_model = 0;
@@ -1425,18 +1472,34 @@ static void server_command_warp(shared_ptr<Client> c, const std::string& args, b
auto s = c->require_server_state();
auto l = c->require_lobby();
check_is_game(l, true);
check_cheats_enabled(l, c);
uint32_t floor = stoul(args, nullptr, 0);
if (c->floor == floor) {
return;
}
// Special case: $warp[me] 0 is allowed in boss arenas if the boss is already
// defeated, even if cheats are disabled. This is because if a player returns
// to a boss arena after a persistence gap in the game, the exit warp won't
// exist, so they need a way to get out.
bool should_check_cheats = is_warpall || (floor != 0) || !floor_is_boss_arena(l->episode, c->floor);
if (!should_check_cheats) {
for (const auto* event : l->map->get_events(c->floor)) {
if (!(event->flags & 0x18)) {
should_check_cheats = true;
break;
}
}
}
if (should_check_cheats) {
check_cheats_enabled(l, c);
}
size_t limit = floor_limit_for_episode(l->episode);
if (limit == 0) {
return;
} else if (floor > limit) {
send_text_message_printf(c, "$C6Area numbers must\nbe %zu or less.", limit);
send_text_message_printf(c, "$C6Area numbers must\nbe %zu or less", limit);
return;
}
@@ -1556,20 +1619,20 @@ static void proxy_command_song(shared_ptr<ProxyServer::LinkedSession> ses, const
}
static void command_item_notifs(Channel& ch, Client::Config& config, const std::string& args) {
if (args == "all" || args == "on") {
config.clear_flag(Client::Flag::RARE_DROP_NOTIFICATIONS_ENABLED);
config.set_flag(Client::Flag::ALL_DROP_NOTIFICATIONS_ENABLED);
if (args == "every" || args == "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 (args == "all" || args == "on") {
config.set_drop_notification_mode(Client::ItemDropNotificationMode::ALL_ITEMS);
send_text_message_printf(ch, "$C6Notifications enabled\nfor all items");
} else if (args == "rare" || args == "rares") {
config.set_flag(Client::Flag::RARE_DROP_NOTIFICATIONS_ENABLED);
config.clear_flag(Client::Flag::ALL_DROP_NOTIFICATIONS_ENABLED);
config.set_drop_notification_mode(Client::ItemDropNotificationMode::RARES_ONLY);
send_text_message_printf(ch, "$C6Notifications enabled\nfor rare items only");
} else if (args == "none" || args == "off") {
config.clear_flag(Client::Flag::RARE_DROP_NOTIFICATIONS_ENABLED);
config.clear_flag(Client::Flag::ALL_DROP_NOTIFICATIONS_ENABLED);
config.set_drop_notification_mode(Client::ItemDropNotificationMode::NOTHING);
send_text_message_printf(ch, "$C6Notifications disabled\nfor all items");
} else {
send_text_message_printf(ch, "$C6You must specify\n$C6off$C7, $C6rare$C7, or $C6on$C7");
send_text_message_printf(ch, "$C6You must specify\n$C6off$C7, $C6rare$C7, $C6on$C7, or\n$C6everything$C7");
}
}
@@ -1590,7 +1653,7 @@ static void server_command_infinite_hp(shared_ptr<Client> c, const std::string&)
c->config.toggle_flag(Client::Flag::INFINITE_HP_ENABLED);
bool enabled = c->config.check_flag(Client::Flag::INFINITE_HP_ENABLED);
send_text_message_printf(c, "$C6Infinite HP %s", enabled ? "enabled" : "disabled");
if (enabled && l->is_game() && is_v1_or_v2(c->version())) {
if (enabled && l->is_game()) {
send_remove_conditions(c);
}
}
@@ -1601,7 +1664,7 @@ static void proxy_command_infinite_hp(shared_ptr<ProxyServer::LinkedSession> ses
ses->config.toggle_flag(Client::Flag::INFINITE_HP_ENABLED);
bool enabled = ses->config.check_flag(Client::Flag::INFINITE_HP_ENABLED);
send_text_message_printf(ses->client_channel, "$C6Infinite HP %s", enabled ? "enabled" : "disabled");
if (enabled && ses->is_in_game && is_v1_or_v2(ses->version())) {
if (enabled && ses->is_in_game) {
send_remove_conditions(ses->client_channel, ses->lobby_client_id);
send_remove_conditions(ses->server_channel, ses->lobby_client_id);
}
@@ -1708,6 +1771,51 @@ static void server_command_dropmode(shared_ptr<Client> c, const std::string& arg
}
}
static void proxy_command_dropmode(shared_ptr<ProxyServer::LinkedSession> ses, const std::string& args) {
check_cheats_allowed(ses->require_server_state(), ses);
using DropMode = ProxyServer::LinkedSession::DropMode;
if (args.empty()) {
switch (ses->drop_mode) {
case DropMode::DISABLED:
send_text_message(ses->client_channel, "Drop mode: disabled");
break;
case DropMode::PASSTHROUGH:
send_text_message(ses->client_channel, "Drop mode: default");
break;
case DropMode::INTERCEPT:
send_text_message(ses->client_channel, "Drop mode: proxy");
break;
}
} else {
DropMode new_mode;
if ((args == "none") || (args == "disabled")) {
new_mode = DropMode::DISABLED;
} else if ((args == "default") || (args == "passthrough")) {
new_mode = DropMode::PASSTHROUGH;
} else if ((args == "proxy") || (args == "intercept")) {
new_mode = DropMode::INTERCEPT;
} else {
send_text_message(ses->client_channel, "Invalid drop mode");
return;
}
ses->set_drop_mode(new_mode);
switch (ses->drop_mode) {
case DropMode::DISABLED:
send_text_message(ses->client_channel, "Item drops disabled");
break;
case DropMode::PASSTHROUGH:
send_text_message(ses->client_channel, "Item drops changed\nto default mode");
break;
case DropMode::INTERCEPT:
send_text_message(ses->client_channel, "Item drops changed\nto proxy mode");
break;
}
}
}
static void server_command_item(shared_ptr<Client> c, const std::string& args) {
auto s = c->require_server_state();
auto l = c->require_lobby();
@@ -1817,7 +1925,7 @@ static void server_command_ep3_infinite_time(shared_ptr<Client> c, const std::st
send_text_message(l, infinite_time_enabled ? "$C6Infinite time enabled" : "$C6Infinite time disabled");
}
static void server_command_ep3_set_def_dice_range(shared_ptr<Client> c, const std::string& args) {
static void server_command_ep3_set_dice_range(shared_ptr<Client> c, const std::string& args) {
auto l = c->require_lobby();
check_is_game(l, true);
check_is_ep3(c, true);
@@ -1835,37 +1943,63 @@ static void server_command_ep3_set_def_dice_range(shared_ptr<Client> c, const st
return;
}
if (l->tournament_match) {
send_text_message(c, "$C6Cannot override\nDEF range in a\ntournament");
send_text_message(c, "$C6Cannot override\ndice ranges in a\ntournament");
return;
}
if (args.empty()) {
l->ep3_server->map_and_rules->rules.def_dice_range = 0;
send_text_message_printf(l, "$C6DEF dice range\nset to default");
} else {
uint8_t min_dice, max_dice;
auto tokens = split(args, '-');
auto parse_dice_range = +[](const string& spec) -> uint8_t {
auto tokens = split(spec, '-');
if (tokens.size() == 1) {
min_dice = stoul(tokens[0]);
max_dice = min_dice;
uint8_t v = stoull(spec);
return (v << 4) | (v & 0x0F);
} else if (tokens.size() == 2) {
min_dice = stoul(tokens[0]);
max_dice = stoul(tokens[1]);
return (stoull(tokens[0]) << 4) | (stoull(tokens[1]) & 0x0F);
} else {
send_text_message(c, "$C6Specify DEF dice\nrange as MIN-MAX");
throw runtime_error("invalid dice spec format");
}
};
uint8_t def_dice_range = 0;
uint8_t atk_dice_range_2v1 = 0;
uint8_t def_dice_range_2v1 = 0;
for (const auto& spec : split(args, ' ')) {
auto tokens = split(spec, ':');
if (tokens.size() != 2) {
send_text_message(c, "$C6Invalid dice spec\nformat");
return;
}
if (min_dice == 0 || min_dice > 9 || max_dice == 0 || max_dice > 9) {
send_text_message(c, "$C6DEF dice must be\nin range 1-9");
return;
if (tokens[0] == "d") {
def_dice_range = parse_dice_range(tokens[1]);
} else if (tokens[0] == "1") {
atk_dice_range_2v1 = parse_dice_range(tokens[1]);
def_dice_range_2v1 = atk_dice_range_2v1;
} else if (tokens[0] == "a1") {
atk_dice_range_2v1 = parse_dice_range(tokens[1]);
} else if (tokens[0] == "d1") {
def_dice_range_2v1 = parse_dice_range(tokens[1]);
}
if (min_dice > max_dice) {
uint8_t t = min_dice;
min_dice = max_dice;
max_dice = t;
}
l->ep3_server->def_dice_value_range_override = def_dice_range;
l->ep3_server->atk_dice_value_range_2v1_override = atk_dice_range_2v1;
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");
} else {
send_text_message_printf(l, "$C7Dice ranges changed:");
if (def_dice_range) {
send_text_message_printf(l, "$C7DEF: $C6%hhu-%hhu",
static_cast<uint8_t>(def_dice_range >> 4), static_cast<uint8_t>(def_dice_range & 0x0F));
}
if (atk_dice_range_2v1) {
send_text_message_printf(l, "$C7ATK (1p in 2v1): $C6%hhu-%hhu",
static_cast<uint8_t>(atk_dice_range_2v1 >> 4), static_cast<uint8_t>(atk_dice_range_2v1 & 0x0F));
}
if (def_dice_range_2v1) {
send_text_message_printf(l, "$C7DEF (1p in 2v1): $C6%hhu-%hhu",
static_cast<uint8_t>(def_dice_range_2v1 >> 4), static_cast<uint8_t>(def_dice_range_2v1 & 0x0F));
}
l->ep3_server->map_and_rules->rules.def_dice_range = ((min_dice << 4) & 0xF0) | (max_dice & 0x0F);
send_text_message_printf(l, "$C6DEF dice range\nset to %hhu-%hhu", min_dice, max_dice);
}
}
@@ -2059,6 +2193,7 @@ struct ChatCommandDefinition {
static const unordered_map<string, ChatCommandDefinition> chat_commands({
{"$allevent", {server_command_lobby_event_all, nullptr}},
{"$ann", {server_command_announce, nullptr}},
{"$ann!", {server_command_announce_mail, nullptr}},
{"$arrow", {server_command_arrow, proxy_command_arrow}},
{"$auction", {server_command_auction, proxy_command_auction}},
{"$ax", {server_command_ax, nullptr}},
@@ -2067,8 +2202,8 @@ static const unordered_map<string, ChatCommandDefinition> chat_commands({
{"$bbchar", {server_command_bbchar, nullptr}},
{"$cheat", {server_command_cheat, nullptr}},
{"$debug", {server_command_debug, nullptr}},
{"$defrange", {server_command_ep3_set_def_dice_range, nullptr}},
{"$dropmode", {server_command_dropmode, nullptr}},
{"$dicerange", {server_command_ep3_set_dice_range, nullptr}},
{"$dropmode", {server_command_dropmode, proxy_command_dropmode}},
{"$edit", {server_command_edit, nullptr}},
{"$ep3battledebug", {server_command_enable_ep3_battle_debug_menu, nullptr}},
{"$event", {server_command_lobby_event, proxy_command_lobby_event}},
@@ -2119,6 +2254,7 @@ static const unordered_map<string, ChatCommandDefinition> chat_commands({
{"$surrender", {server_command_surrender, nullptr}},
{"$swa", {server_command_switch_assist, proxy_command_switch_assist}},
{"$unset", {server_command_ep3_unset_field_character, nullptr}},
{"$variations", {server_command_variations, nullptr}},
{"$warp", {server_command_warpme, proxy_command_warpme}},
{"$warpme", {server_command_warpme, proxy_command_warpme}},
{"$warpall", {server_command_warpall, proxy_command_warpall}},
+66 -65
View File
@@ -73,8 +73,6 @@ void Client::Config::set_flags_for_version(Version version, int64_t sub_version)
case 0x21: // DCv1 US
this->set_flag(Flag::NO_D6);
this->set_flag(Flag::NO_SEND_FUNCTION_CALL);
// In the case of DCNTE, the IS_DC_TRIAL_EDITION flag is already set when
// we get here
break;
case 0x23: // DCv1 EU
this->set_flag(Flag::NO_D6);
@@ -93,13 +91,10 @@ void Client::Config::set_flags_for_version(Version version, int64_t sub_version)
break;
case 0x30: // GC Ep1&2 GameJam demo, GC Ep1&2 Trial Edition, GC Ep1&2 JP v1.2, at least one version of XB
case 0x31: // GC Ep1&2 US v1.0, GC US v1.1, XB US
case 0x34: // GC Ep1&2 JP v1.3
// In the case of GC Trial Edition, the IS_GC_TRIAL_EDITION flag is
// already set when we get here (because the client has used V2 encryption
// instead of V3)
break;
case 0x32: // GC Ep1&2 EU 50Hz
case 0x33: // GC Ep1&2 EU 60Hz
case 0x34: // GC Ep1&2 JP v1.3
this->set_flag(Flag::NO_D6_AFTER_LOBBY);
break;
case 0x35: // GC Ep1&2 JP v1.4 (Plus)
@@ -117,8 +112,7 @@ void Client::Config::set_flags_for_version(Version version, int64_t sub_version)
this->set_flag(Flag::ENCRYPTED_SEND_FUNCTION_CALL);
this->set_flag(Flag::SEND_FUNCTION_CALL_NO_CACHE_PATCH);
// sub_version can't be used to tell JP final and Trial Edition apart; we
// instead look at header.flag in the 61 command and set the
// IS_EP3_TRIAL_EDITION flag there.
// instead look at header.flag in the 61 command and set the version then.
break;
case 0x41: // GC Ep3 US (and BB)
this->set_flag(Flag::NO_D6_AFTER_LOBBY);
@@ -135,6 +129,26 @@ void Client::Config::set_flags_for_version(Version version, int64_t sub_version)
}
}
Client::ItemDropNotificationMode Client::Config::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<Client::ItemDropNotificationMode>(mode_s);
}
void Client::Config::set_drop_notification_mode(ItemDropNotificationMode new_mode) {
uint8_t mode_s = static_cast<uint8_t>(new_mode);
if (mode_s & 1) {
this->set_flag(Client::Flag::ITEM_DROP_NOTIFICATIONS_1);
} else {
this->clear_flag(Client::Flag::ITEM_DROP_NOTIFICATIONS_1);
}
if (mode_s & 2) {
this->set_flag(Client::Flag::ITEM_DROP_NOTIFICATIONS_2);
} else {
this->clear_flag(Client::Flag::ITEM_DROP_NOTIFICATIONS_2);
}
}
bool Client::Config::should_update_vs(const Config& other) const {
constexpr uint64_t mask = static_cast<uint64_t>(Flag::CLIENT_SIDE_MASK);
return ((this->enabled_flags ^ other.enabled_flags) & mask) ||
@@ -197,11 +211,10 @@ Client::Client(
this->config.set_flags_for_version(version, -1);
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_flag(Flag::RARE_DROP_NOTIFICATIONS_ENABLED);
this->config.set_drop_notification_mode(ItemDropNotificationMode::RARES_ONLY);
}
this->config.specific_version = default_specific_version_for_version(version, -1);
this->last_switch_enabled_command.header.subcommand = 0;
memset(&this->next_connection_addr, 0, sizeof(this->next_connection_addr));
this->reschedule_save_game_data_event();
@@ -335,46 +348,52 @@ shared_ptr<const TeamIndex::Team> Client::team() const {
return team;
}
bool Client::can_see_quest(shared_ptr<const Quest> q, uint8_t event, uint8_t difficulty, size_t num_players) const {
bool Client::evaluate_quest_availability_expression(
shared_ptr<const QuestAvailabilityExpression> expr,
uint8_t event,
uint8_t difficulty,
size_t num_players,
bool v1_present) const {
if (this->license && this->license->check_flag(License::Flag::DISABLE_QUEST_REQUIREMENTS)) {
return true;
}
if (!q->available_expression) {
if (!expr) {
return true;
}
auto l = this->lobby.lock();
auto p = this->character();
string expr = q->available_expression->str();
QuestAvailabilityExpression::Env env = {
.flags = &p->quest_flags.data.at(difficulty),
.flags = (l && !l->quest_flags_known) ? &l->quest_flag_values->data.at(difficulty) : &p->quest_flags.data.at(difficulty),
.challenge_records = &p->challenge_records,
.team = this->team(),
.num_players = num_players,
.event = event,
.v1_present = v1_present,
};
int64_t ret = q->available_expression->evaluate(env);
this->log.info("Evaluated quest availability expression %s => %s", expr.c_str(), ret ? "TRUE" : "FALSE");
int64_t ret = expr->evaluate(env);
if (this->log.should_log(LogLevel::INFO)) {
string expr_str = expr->str();
this->log.info("Evaluated quest availability expression %s => %s", expr_str.c_str(), ret ? "TRUE" : "FALSE");
}
return ret;
}
bool Client::can_play_quest(shared_ptr<const Quest> q, uint8_t event, uint8_t difficulty, size_t num_players) const {
if (this->license && this->license->check_flag(License::Flag::DISABLE_QUEST_REQUIREMENTS)) {
bool Client::can_see_quest(shared_ptr<const Quest> q, uint8_t event, uint8_t difficulty, size_t num_players, bool v1_present) const {
return this->evaluate_quest_availability_expression(q->available_expression, event, difficulty, num_players, v1_present);
}
bool Client::can_play_quest(shared_ptr<const Quest> q, uint8_t event, uint8_t difficulty, size_t num_players, bool v1_present) const {
return this->evaluate_quest_availability_expression(q->enabled_expression, event, difficulty, num_players, v1_present);
}
bool Client::can_use_chat_commands() const {
if (!this->license) {
return false;
}
if (this->license->check_flag(License::Flag::ALWAYS_ENABLE_CHAT_COMMANDS)) {
return true;
}
if (!q->enabled_expression) {
return true;
}
auto p = this->character();
string expr = q->enabled_expression->str();
QuestAvailabilityExpression::Env env = {
.flags = &p->quest_flags.data.at(difficulty),
.challenge_records = &p->challenge_records,
.team = this->team(),
.num_players = num_players,
.event = event,
};
bool ret = q->enabled_expression->evaluate(env);
this->log.info("Evaluating quest enabled expression %s => %s", expr.c_str(), ret ? "TRUE" : "FALSE");
return ret;
return this->require_server_state()->enable_chat_commands;
}
void Client::dispatch_save_game_data(evutil_socket_t, short, void* ctx) {
@@ -454,7 +473,7 @@ void Client::create_battle_overlay(shared_ptr<const BattleRules> rules, shared_p
stats.reset_to_base(char_class, level_table);
stats.advance_to_level(char_class, target_level, level_table);
stats.unknown_a1 = 40;
stats.esp = 40;
stats.meseta = 300;
}
if (rules->tech_disk_mode == BattleRules::TechDiskMode::LIMIT_LEVEL) {
@@ -500,7 +519,7 @@ void Client::create_challenge_overlay(Version version, size_t template_index, sh
overlay->disp.stats.reset_to_base(overlay->disp.visual.char_class, level_table);
overlay->disp.stats.advance_to_level(overlay->disp.visual.char_class, tpl.level, level_table);
overlay->disp.stats.unknown_a1 = 40;
overlay->disp.stats.esp = 40;
overlay->disp.stats.unknown_a3 = 10.0;
overlay->disp.stats.experience = level_table->stats_delta_for_level(overlay->disp.visual.char_class, overlay->disp.stats.level).experience;
overlay->disp.stats.meseta = 0;
@@ -795,7 +814,7 @@ void Client::load_all_files() {
files_manager->set_character(this->character_filename(), this->character_data);
this->character_data->inventory = nsc_data.inventory;
this->character_data->disp = nsc_data.disp;
this->character_data->play_time_seconds = nsc_data.disp.play_time;
this->character_data->play_time_seconds = 0;
this->character_data->unknown_a2 = nsc_data.unknown_a2;
this->character_data->quest_flags = nsc_data.quest_flags;
this->character_data->death_count = nsc_data.death_count;
@@ -824,11 +843,6 @@ void Client::load_all_files() {
}
}
if (this->character_data) {
this->license->auto_reply_message = this->character_data->auto_reply.decode();
this->license->save();
}
this->blocked_senders.clear();
for (size_t z = 0; z < this->guild_card_data->blocked.size(); z++) {
if (this->guild_card_data->blocked[z].present) {
@@ -837,6 +851,10 @@ void Client::load_all_files() {
}
if (this->character_data) {
// Clear legacy play_time field
this->character_data->disp.name.clear_after_bytes(0x18);
this->license->auto_reply_message = this->character_data->auto_reply.decode();
this->license->save();
this->last_play_time_update = now();
}
}
@@ -909,8 +927,7 @@ void Client::save_character_file() {
// off each time we save. I'm lazy, so insert shrug emoji here.
uint64_t t = now();
uint64_t seconds = (t - this->last_play_time_update) / 1000000;
this->character_data->disp.play_time += seconds;
this->character_data->play_time_seconds = this->character_data->disp.play_time;
this->character_data->play_time_seconds += seconds;
player_data_log.info("Added %" PRIu64 " seconds to play time", seconds);
this->last_play_time_update = t;
}
@@ -1035,44 +1052,28 @@ void Client::use_character_bank(int8_t index) {
}
void Client::print_inventory(FILE* stream) const {
auto s = this->require_server_state();
auto p = this->character();
shared_ptr<const ItemNameIndex> name_index;
try {
name_index = this->require_server_state()->item_name_index(this->version());
} catch (const runtime_error&) {
}
fprintf(stream, "[PlayerInventory] Meseta: %" PRIu32 "\n", p->disp.stats.meseta.load());
fprintf(stream, "[PlayerInventory] %hhu 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();
if (name_index) {
auto name = name_index->describe_item(item.data);
fprintf(stream, "[PlayerInventory] %2zu: [+%08" PRIX32 "] %s (%s)\n", x, item.flags.load(), hex.c_str(), name.c_str());
} else {
fprintf(stream, "[PlayerInventory] %2zu: [+%08" PRIX32 "] %s\n", x, item.flags.load(), hex.c_str());
}
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());
}
}
void Client::print_bank(FILE* stream) const {
auto s = this->require_server_state();
auto p = this->character();
shared_ptr<const ItemNameIndex> name_index;
try {
name_index = this->require_server_state()->item_name_index(this->version());
} catch (const runtime_error&) {
}
fprintf(stream, "[PlayerBank] Meseta: %" PRIu32 "\n", p->bank.meseta.load());
fprintf(stream, "[PlayerBank] %" PRIu32 " items\n", p->bank.num_items.load());
for (size_t x = 0; x < p->bank.num_items; x++) {
const auto& item = p->bank.items[x];
const char* present_token = item.present ? "" : " (missing present flag)";
auto hex = item.data.hex();
if (name_index) {
auto name = name_index->describe_item(item.data);
fprintf(stream, "[PlayerBank] %3zu: %s (%s) (x%hu)%s\n", x, hex.c_str(), name.c_str(), item.amount.load(), present_token);
} else {
fprintf(stream, "[PlayerBank] %3zu: %s (x%hu)%s\n", x, hex.c_str(), item.amount.load(), present_token);
}
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);
}
}
+67 -50
View File
@@ -34,62 +34,70 @@ public:
// 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 = 0xFFFFFFFFFC0FFFFB,
CLIENT_SIDE_MASK = 0xFF3CFFFF7C0FFFFB,
// Version-related flags
CHECKED_FOR_DC_V1_PROTOTYPE = 0x0000000000000002,
LICENSE_WAS_CREATED = 0x0000000000000004, // Server-side only
NO_D6_AFTER_LOBBY = 0x0000000000000100,
NO_D6 = 0x0000000000000200,
FORCE_ENGLISH_LANGUAGE_BB = 0x0000000000000400,
CHECKED_FOR_DC_V1_PROTOTYPE = 0x0000000000000002,
LICENSE_WAS_CREATED = 0x0000000000000004, // Server-side only
NO_D6_AFTER_LOBBY = 0x0000000000000100,
NO_D6 = 0x0000000000000200,
FORCE_ENGLISH_LANGUAGE_BB = 0x0000000000000400,
// Flags describing the behavior for send_function_call
NO_SEND_FUNCTION_CALL = 0x0000000000001000,
ENCRYPTED_SEND_FUNCTION_CALL = 0x0000000000002000,
SEND_FUNCTION_CALL_CHECKSUM_ONLY = 0x0000000000004000,
SEND_FUNCTION_CALL_NO_CACHE_PATCH = 0x0000000000008000,
USE_OVERFLOW_FOR_SEND_FUNCTION_CALL = 0x0000000000010000,
NO_SEND_FUNCTION_CALL = 0x0000000000001000,
ENCRYPTED_SEND_FUNCTION_CALL = 0x0000000000002000,
SEND_FUNCTION_CALL_CHECKSUM_ONLY = 0x0000000000004000,
SEND_FUNCTION_CALL_NO_CACHE_PATCH = 0x0000000000008000,
USE_OVERFLOW_FOR_SEND_FUNCTION_CALL = 0x0000000000010000,
// State flags
LOADING = 0x0000000000100000, // Server-side only
LOADING_QUEST = 0x0000000000200000, // Server-side only
LOADING_RUNNING_JOINABLE_QUEST = 0x0000000000400000, // Server-side only
LOADING_TOURNAMENT = 0x0000000000800000, // Server-side only
IN_INFORMATION_MENU = 0x0000000001000000, // Server-side only
AT_WELCOME_MESSAGE = 0x0000000002000000, // Server-side only
SAVE_ENABLED = 0x0000000004000000,
HAS_EP3_CARD_DEFS = 0x0000000008000000,
HAS_EP3_MEDIA_UPDATES = 0x0000000010000000,
USE_OVERRIDE_RANDOM_SEED = 0x0000000020000000,
HAS_GUILD_CARD_NUMBER = 0x0000000040000000,
AT_BANK_COUNTER = 0x0000000080000000, // Server-side only
SHOULD_SEND_ARTIFICIAL_ITEM_STATE = 0x0001000000000000, // Server-side only
SHOULD_SEND_ARTIFICIAL_FLAG_STATE = 0x0002000000000000, // Server-side only
SHOULD_SEND_ENABLE_SAVE = 0x0004000000000000,
SWITCH_ASSIST_ENABLED = 0x0000000100000000,
LOADING = 0x0000000000100000, // Server-side only
LOADING_QUEST = 0x0000000000200000, // Server-side only
LOADING_RUNNING_JOINABLE_QUEST = 0x0000000000400000, // Server-side only
LOADING_TOURNAMENT = 0x0000000000800000, // Server-side only
IN_INFORMATION_MENU = 0x0000000001000000, // Server-side only
AT_WELCOME_MESSAGE = 0x0000000002000000, // Server-side only
SAVE_ENABLED = 0x0000000004000000,
HAS_EP3_CARD_DEFS = 0x0000000008000000,
HAS_EP3_MEDIA_UPDATES = 0x0000000010000000,
USE_OVERRIDE_RANDOM_SEED = 0x0000000020000000,
HAS_GUILD_CARD_NUMBER = 0x0000000040000000,
AT_BANK_COUNTER = 0x0000000080000000, // Server-side only
SHOULD_SEND_ARTIFICIAL_ITEM_STATE = 0x0001000000000000, // Server-side only
SHOULD_SEND_ARTIFICIAL_ENEMY_AND_SET_STATE = 0x0040000000000000, // Server-side only
SHOULD_SEND_ARTIFICIAL_OBJECT_STATE = 0x0080000000000000, // Server-side only
SHOULD_SEND_ARTIFICIAL_FLAG_STATE = 0x0002000000000000, // Server-side only
SHOULD_SEND_ENABLE_SAVE = 0x0004000000000000,
SWITCH_ASSIST_ENABLED = 0x0000000100000000,
// Cheat mode and option flags
INFINITE_HP_ENABLED = 0x0000000200000000,
INFINITE_TP_ENABLED = 0x0000000400000000,
DEBUG_ENABLED = 0x0000000800000000,
RARE_DROP_NOTIFICATIONS_ENABLED = 0x0010000000000000,
ALL_DROP_NOTIFICATIONS_ENABLED = 0x0020000000000000,
INFINITE_HP_ENABLED = 0x0000000200000000,
INFINITE_TP_ENABLED = 0x0000000400000000,
DEBUG_ENABLED = 0x0000000800000000,
ITEM_DROP_NOTIFICATIONS_1 = 0x0010000000000000,
ITEM_DROP_NOTIFICATIONS_2 = 0x0020000000000000,
// Proxy option flags
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_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,
// clang-format on
};
enum class ItemDropNotificationMode {
NOTHING = 0,
RARES_ONLY = 1,
ALL_ITEMS = 2,
ALL_ITEMS_INCLUDING_MESETA = 3,
};
static constexpr uint64_t DEFAULT_FLAGS = static_cast<uint64_t>(Flag::PROXY_CHAT_COMMANDS_ENABLED);
@@ -129,6 +137,9 @@ public:
void set_flags_for_version(Version version, int64_t sub_version);
ItemDropNotificationMode get_drop_notification_mode() const;
void set_drop_notification_mode(ItemDropNotificationMode new_mode);
template <size_t Bytes>
void parse_from(const parray<uint8_t, Bytes>& data) {
StringReader r(data.data(), data.size());
@@ -186,12 +197,10 @@ public:
uint8_t bb_connection_phase;
uint64_t ping_start_time;
// Patch server
std::vector<PatchFileChecksumRequest> patch_file_checksum_requests;
// Lobby/positioning
Config config;
Config synced_config;
std::unique_ptr<parray<le_uint32_t, 0x20>> override_variations;
int32_t sub_version;
float x;
float z;
@@ -241,7 +250,7 @@ public:
// Miscellaneous (used by chat commands)
uint32_t next_exp_value; // next EXP value to give
G_SwitchStateChanged_6x05 last_switch_enabled_command;
RecentSwitchFlags recent_switch_flags; // used for switch assist
bool can_chat;
struct PendingCharacterExport {
std::shared_ptr<const License> license;
@@ -283,8 +292,16 @@ public:
std::shared_ptr<const TeamIndex::Team> team() const;
bool can_see_quest(std::shared_ptr<const Quest> q, uint8_t event, uint8_t difficulty, size_t num_players) const;
bool can_play_quest(std::shared_ptr<const Quest> q, uint8_t event, uint8_t difficulty, size_t num_players) const;
bool evaluate_quest_availability_expression(
std::shared_ptr<const QuestAvailabilityExpression> expr,
uint8_t event,
uint8_t difficulty,
size_t num_players,
bool v1_present) const;
bool can_see_quest(std::shared_ptr<const Quest> q, uint8_t event, uint8_t difficulty, size_t num_players, bool v1_present) const;
bool can_play_quest(std::shared_ptr<const Quest> q, uint8_t event, uint8_t difficulty, size_t num_players, bool v1_present) const;
bool can_use_chat_commands() const;
static void dispatch_save_game_data(evutil_socket_t, short, void* ctx);
void save_game_data();
+156 -111
View File
@@ -58,8 +58,11 @@
// instead in newserv, since the server substitutes most usage of $ in player-
// provided text with \t. The escape codes are:
// - Language codes
// - - $E: Set text interpretation to English
// - - $J: Set text interpretation to Japanese
// - - $E: Set text interpretation to English / use Roman font
// - - $J: Set text interpretation to Japanese / use Japanese font
// - - $B: Use Simplified Chinese font (PC/BB)
// - - $T: Use Traditional Chinese font (PC/BB)
// - - $K: Use Korean font (PC/BB)
// - Color codes
// - - $C0: Black (000000)
// - - $C1: Blue (0000FF)
@@ -1439,26 +1442,28 @@ struct S_GenerateID_DC_PC_V3_80 {
// contains uninitialized memory when the client sends this command. newserv
// clears the uninitialized data for security reasons before forwarding.
template <TextEncoding Encoding>
struct SC_SimpleMail_81 {
struct SC_SimpleMail_PC_81 {
// If player_tag and from_guild_card_number are zero, the message cannot be
// replied to.
le_uint32_t player_tag = 0x00010000;
le_uint32_t from_guild_card_number = 0;
pstring<Encoding, 0x10> from_name;
pstring<TextEncoding::UTF16, 0x10> from_name;
le_uint32_t to_guild_card_number = 0;
pstring<Encoding, 0x200> text;
pstring<TextEncoding::UTF16, 0x200> text;
} __packed__;
struct SC_SimpleMail_PC_81 : SC_SimpleMail_81<TextEncoding::UTF16> {
} __packed__;
struct SC_SimpleMail_DC_V3_81 : SC_SimpleMail_81<TextEncoding::MARKED> {
struct SC_SimpleMail_DC_V3_81 {
le_uint32_t player_tag = 0x00010000;
le_uint32_t from_guild_card_number = 0;
pstring<TextEncoding::MARKED, 0x10> from_name;
le_uint32_t to_guild_card_number = 0;
pstring<TextEncoding::MARKED, 0x200> text;
} __packed__;
struct SC_SimpleMail_BB_81 {
le_uint32_t player_tag = 0x00010000;
le_uint32_t from_guild_card_number = 0;
pstring<TextEncoding::UTF16, 0x10> from_name;
pstring<TextEncoding::UTF16_ALWAYS_MARKED, 0x10> from_name;
le_uint32_t to_guild_card_number = 0;
pstring<TextEncoding::UTF16, 0x14> received_date;
pstring<TextEncoding::UTF16, 0x200> text;
@@ -1696,7 +1701,8 @@ struct C_LoginBase_BB_93 {
// 02 - create character
// 03 - apply updates from dressing room
// 04 - login server
// 05 - lobby server (and beyond)
// 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;
@@ -2505,7 +2511,7 @@ struct S_ChoiceSearchResultEntry_DC_V3_C4 : S_ChoiceSearchResultEntry_C4<PSOComm
} __packed__;
struct S_ChoiceSearchResultEntry_PC_C4 : S_ChoiceSearchResultEntry_C4<PSOCommandHeaderPC, TextEncoding::UTF16, TextEncoding::UTF16, TextEncoding::UTF16> {
} __packed__;
struct S_ChoiceSearchResultEntry_BB_C4 : S_ChoiceSearchResultEntry_C4<PSOCommandHeaderBB, TextEncoding::UTF16, TextEncoding::UTF16, TextEncoding::UTF16> {
struct S_ChoiceSearchResultEntry_BB_C4 : S_ChoiceSearchResultEntry_C4<PSOCommandHeaderBB, TextEncoding::UTF16_ALWAYS_MARKED, TextEncoding::UTF16, TextEncoding::UTF16> {
} __packed__;
// C5 (S->C): Player records update (DCv2 and later versions)
@@ -2702,7 +2708,7 @@ struct S_InfoBoardEntry_D8 {
pstring<NameEncoding, 0x10> name;
pstring<MessageEncoding, 0xAC> message;
} __packed__;
struct S_InfoBoardEntry_BB_D8 : S_InfoBoardEntry_D8<TextEncoding::UTF16, TextEncoding::UTF16> {
struct S_InfoBoardEntry_BB_D8 : S_InfoBoardEntry_D8<TextEncoding::UTF16_ALWAYS_MARKED, TextEncoding::UTF16> {
} __packed__;
struct S_InfoBoardEntry_V3_D8 : S_InfoBoardEntry_D8<TextEncoding::ASCII, TextEncoding::MARKED> {
} __packed__;
@@ -2826,8 +2832,24 @@ struct C_CreateChallengeModeAwardItem_BB_07DF {
// The client will send 09 and 10 commands to inspect or enter a tournament. The
// server should respond to an 09 command with an E3 command; the server should
// respond to a 10 command with an E2 command.
// header.flag is the count of filled-in entries.
struct S_TournamentList_Ep3NTE_E0 {
struct Entry {
le_uint32_t menu_id = 0;
le_uint32_t item_id = 0;
uint8_t unknown_a1 = 0;
uint8_t locked = 0;
uint8_t state = 0;
uint8_t unknown_a2 = 0;
le_uint32_t start_time = 0; // In seconds since Unix epoch
pstring<TextEncoding::MARKED, 0x20> name;
le_uint16_t num_teams = 0;
le_uint16_t max_teams = 0;
} __packed__;
parray<Entry, 0x20> entries;
} __packed__;
struct S_TournamentList_Ep3_E0 {
struct Entry {
le_uint32_t menu_id = 0;
@@ -2856,8 +2878,8 @@ struct S_TournamentList_Ep3_E0 {
pstring<TextEncoding::MARKED, 0x20> name;
le_uint16_t num_teams = 0;
le_uint16_t max_teams = 0;
le_uint16_t unknown_a3 = 0;
le_uint16_t unknown_a4 = 0;
le_uint16_t unknown_a3 = 0xFFFF;
le_uint16_t unknown_a4 = 0xFFFF;
} __packed__;
parray<Entry, 0x20> entries;
} __packed__;
@@ -3319,7 +3341,7 @@ struct C_AddOrRemoveTeamMember_BB_03EA_05EA {
// 07EA: Team chat
struct SC_TeamChat_BB_07EA {
pstring<TextEncoding::UTF16, 0x10> sender_name;
pstring<TextEncoding::UTF16_ALWAYS_MARKED, 0x10> sender_name;
// Text follows here. The message is truncated by the client if it is longer
// than 0x8F wchar_ts.
} __packed__;
@@ -3336,7 +3358,7 @@ struct S_TeamMemberList_BB_09EA {
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;
pstring<TextEncoding::UTF16, 0x10> name;
pstring<TextEncoding::UTF16_ALWAYS_MARKED, 0x10> name;
} __packed__;
// Variable-length field:
// Entry entries[entry_count];
@@ -3357,7 +3379,7 @@ struct S_Unknown_BB_0CEA {
struct S_TeamName_BB_0EEA {
parray<uint8_t, 0x10> unused;
pstring<TextEncoding::UTF16, 0x10> team_name;
pstring<TextEncoding::UTF16_ALWAYS_MARKED, 0x10> team_name;
} __packed__;
// 0FEA (C->S): Set team flag
@@ -3395,7 +3417,7 @@ struct S_TeamMembershipInformation_BB_12EA {
uint8_t unknown_a7 = 0;
uint8_t unknown_a8 = 0;
uint8_t unknown_a9 = 0;
pstring<TextEncoding::UTF16, 0x10> team_name;
pstring<TextEncoding::UTF16_ALWAYS_MARKED, 0x10> team_name;
} __packed__;
// 13EA: Team info for lobby players
@@ -3412,10 +3434,10 @@ struct S_TeamInfoForPlayer_BB_13EA_15EA_Entry {
/* 0011 */ uint8_t unknown_a7 = 0;
/* 0012 */ uint8_t unknown_a8 = 0;
/* 0013 */ uint8_t unknown_a9 = 0;
/* 0014 */ pstring<TextEncoding::UTF16, 0x10> team_name;
/* 0014 */ pstring<TextEncoding::UTF16_ALWAYS_MARKED, 0x10> team_name;
/* 0034 */ le_uint32_t guild_card_number2 = 0;
/* 0038 */ le_uint32_t lobby_client_id = 0;
/* 003C */ pstring<TextEncoding::UTF16, 0x10> player_name;
/* 003C */ pstring<TextEncoding::UTF16_ALWAYS_MARKED, 0x10> player_name;
/* 005C */ parray<le_uint16_t, 0x20 * 0x20> flag_data;
/* 085C */
} __packed__;
@@ -3443,7 +3465,7 @@ struct S_IntraTeamRanking_BB_18EA {
/* 00 */ le_uint32_t rank = 0;
/* 04 */ le_uint32_t privilege_level = 0;
/* 08 */ le_uint32_t guild_card_number = 0;
/* 0C */ pstring<TextEncoding::UTF16, 0x10> player_name;
/* 0C */ pstring<TextEncoding::UTF16_ALWAYS_MARKED, 0x10> player_name;
/* 2C */ le_uint32_t points = 0;
/* 30 */
} __packed__;
@@ -3480,7 +3502,7 @@ struct S_TeamRewardList_BB_19EA_1AEA {
struct S_CrossTeamRanking_BB_1CEA {
le_uint32_t num_entries;
struct Entry {
/* 00 */ pstring<TextEncoding::UTF16, 0x10> team_name;
/* 00 */ pstring<TextEncoding::UTF16_ALWAYS_MARKED, 0x10> team_name;
/* 20 */ le_uint32_t team_points = 0;
/* 24 */ le_uint32_t unknown_a1 = 0;
/* 28 */
@@ -3496,7 +3518,7 @@ struct S_CrossTeamRanking_BB_1CEA {
// header.flag is used, but it's unknown what the value means.
struct C_RenameTeam_BB_1EEA {
pstring<TextEncoding::UTF16, 0x10> new_team_name;
pstring<TextEncoding::UTF16_ALWAYS_MARKED, 0x10> new_team_name;
} __packed__;
// 1FEA (S->C): Rename team result
@@ -3806,26 +3828,39 @@ struct G_ExtendedHeader {
struct G_Unknown_6x04 {
G_ClientIDHeader header;
le_uint16_t unknown_a1 = 0;
le_uint16_t unused = 0;
le_uint16_t unknown_a2 = 0;
} __packed__;
// 6x05: Switch state changed
// Some things that don't look like switches are implemented as switches using
// this subcommand. For example, when all enemies in a room are defeated, this
// subcommand is used to unlock the doors.
// Note: In the client, this is a subclass of 6x04, similar to how 6xA2 is a
// subclass of 6x60.
struct G_SwitchStateChanged_6x05 {
// Note: header.object_id is 0xFFFF for room clear when all enemies defeated
G_ObjectIDHeader header;
parray<uint8_t, 2> unknown_a1;
// TODO: Some of these might be big-endian on GC; it only byteswaps
// unknown_a3. Are the others actually uint16, or are they uint8[2]?
le_uint16_t unknown_a1 = 0;
le_uint16_t unknown_a2 = 0;
parray<uint8_t, 2> unknown_a3;
uint8_t floor = 0;
le_uint16_t switch_flag_num = 0;
uint8_t switch_flag_floor = 0;
// Only two bits in flags have meanings:
// 01 - set unlock flag (if not set, the flag is cleared instead)
// 02 - play room unlock sound if floor matches client's floor
uint8_t flags = 0; // Bit field, with 2 lowest bits having meaning
} __packed__;
// 6x06: Send guild card
struct G_SendGuildCard_DCNTE_6x06 {
G_UnusedHeader header;
GuildCardDCNTE guild_card;
uint8_t unused;
} __packed__;
struct G_SendGuildCard_DC_6x06 {
G_UnusedHeader header;
GuildCardDC guild_card;
@@ -3868,24 +3903,27 @@ struct G_Unknown_6x09 {
G_EnemyIDHeader header;
} __packed__;
// 6x0A: Enemy hit
// 6x0A: Update enemy state
template <bool IsBigEndian>
struct G_EnemyHitByPlayer_6x0A {
struct G_UpdateEnemyState_6x0A {
G_EnemyIDHeader header;
le_uint16_t enemy_index = 0; // [0, 0xB50)
le_uint16_t total_damage = 0;
// Flags:
// 00000400 - should play hit animation
// 00000800 - is dead
typename std::conditional<IsBigEndian, be_uint32_t, le_uint32_t>::type flags = 0;
} __packed__;
struct G_EnemyHitByPlayer_GC_6x0A : G_EnemyHitByPlayer_6x0A<true> {
struct G_UpdateEnemyState_GC_6x0A : G_UpdateEnemyState_6x0A<true> {
} __packed__;
struct G_EnemyHitByPlayer_DC_PC_XB_BB_6x0A : G_EnemyHitByPlayer_6x0A<false> {
struct G_UpdateEnemyState_DC_PC_XB_BB_6x0A : G_UpdateEnemyState_6x0A<false> {
} __packed__;
// 6x0B: Box destroyed
// 6x0B: Update object state
struct G_BoxDestroyed_6x0B {
struct G_UpdateObjectState_6x0B {
G_ClientIDHeader header;
le_uint32_t flags = 0;
le_uint32_t object_index = 0;
@@ -4153,12 +4191,12 @@ struct G_SetOrClearPlayerFlags_6x2E {
le_uint32_t or_mask = 0;
} __packed__;
// 6x2F: Hit by enemy
// 6x2F: Change player HP
struct G_HitByEnemy_6x2F {
G_ClientIDHeader header;
le_uint32_t hit_type = 0; // 0 = set HP, 1 = add/subtract HP, 2 = add/sub fixed HP
le_uint16_t damage = 0;
struct G_ChangePlayerHP_6x2F {
G_UnusedHeader header;
le_uint32_t type = 0; // 0 = set HP, 1 = add/subtract HP, 2 = add/sub fixed HP
le_uint16_t amount = 0;
le_uint16_t client_id = 0;
} __packed__;
@@ -4311,13 +4349,14 @@ struct G_Attack_6x43_6x44_6x45 {
// 6x46: Attack finished (sent after each of 43, 44, and 45) (protected on V3/V4)
struct TargetEntry {
le_uint16_t entity_id = 0;
le_uint16_t unknown_a2 = 0;
} __packed__;
struct G_AttackFinished_6x46 {
G_ClientIDHeader header;
le_uint32_t count = 0;
struct TargetEntry {
le_uint16_t unknown_a1 = 0;
le_uint16_t unknown_a2 = 0;
} __packed__;
// The client may send a shorter command if not all of these are used.
parray<TargetEntry, 10> targets;
} __packed__;
@@ -4335,11 +4374,7 @@ struct G_CastTechnique_6x47 {
// compatibility, and never cleaned it up.
uint8_t level = 0;
uint8_t target_count = 0; // Must be in [0, 10]
struct TargetEntry {
le_uint16_t client_id = 0;
le_uint16_t unknown_a2 = 0;
} __packed__;
// The client mauy send a shorter command if not all of these are used.
// The client may send a shorter command if not all of these are used.
parray<TargetEntry, 10> targets;
} __packed__;
@@ -4394,15 +4429,15 @@ struct G_PlayerDied_6x4D {
le_uint32_t unknown_a1 = 0;
} __packed__;
// 6x4E: Player died (protected on V3/V4)
// 6x4E: Player is dead can be revived (protected on V3/V4)
struct G_PlayerDied_6x4E {
struct G_PlayerRevivable_6x4E {
G_ClientIDHeader header;
} __packed__;
// 6x4F: Player resurrected (via Scape Doll) (protected on V3/V4)
// 6x4F: Player revived (protected on V3/V4)
struct G_PlayerUsedScapeDoll_6x4F {
struct G_PlayerRevived_6x4F {
G_ClientIDHeader header;
} __packed__;
@@ -4601,15 +4636,13 @@ struct G_UseStarAtomizer_6x66 {
parray<le_uint16_t, 4> target_client_ids;
} __packed__;
// 6x67: Create enemy set
// 6x67: Trigger set event
struct G_CreateEnemySet_6x67 {
struct G_TriggerSetEvent_6x67 {
G_UnusedHeader header;
// unused1 could be floor; the client checks this against a global but the
// logic is the same in both branches
le_uint32_t unused1 = 0;
le_uint32_t unknown_a1 = 0;
le_uint32_t unused2 = 0;
le_uint32_t floor = 0;
le_uint32_t event_id = 0; // NOT event index
le_uint32_t client_id = 0;
} __packed__;
// 6x68: Create telepipe / cast Ryuker
@@ -4662,9 +4695,9 @@ struct G_SyncGameStateHeader_6x6B_6x6C_6x6D_6x6E {
// Decompressed format is a list of these
struct G_SyncEnemyState_6x6B_Entry_Decompressed {
le_uint32_t flags = 0;
le_uint16_t last_attacker = 0;
le_uint16_t remaining_hp = 0;
le_uint32_t flags = 0; // Same as flags in 6x0A
le_uint16_t item_drop_id = 0;
le_uint16_t total_damage = 0; // Same as in 6x0A
uint8_t red_buff_type = 0;
uint8_t red_buff_level = 0;
uint8_t blue_buff_type = 0;
@@ -4677,7 +4710,7 @@ struct G_SyncEnemyState_6x6B_Entry_Decompressed {
// Decompressed format is a list of these
struct G_SyncObjectState_6x6C_Entry_Decompressed {
le_uint16_t flags = 0;
le_uint16_t object_index = 0;
le_uint16_t item_drop_id = 0;
} __packed__;
// 6x6D: Sync item state (used while loading into game)
@@ -4721,24 +4754,38 @@ struct G_SyncItemState_6x6D_Decompressed {
// FloorItem items[sum(floor_item_count_per_floor)];
} __packed__;
// 6x6E: Sync flag state (used while loading into game)
// 6x6E: Sync set flag state (used while loading into game)
// Compressed format is the same as 6x6B.
struct G_SyncFlagState_6x6E_Decompressed {
// The three unknowns here are the sizes (in bytes) of three fields
// immediately following this structure. It is currently unknown what these
// fields represent. The three unknown fields always sum to the size field.
le_uint16_t size = 0;
le_uint16_t unknown_a1 = 0;
le_uint16_t unknown_a2 = 0;
le_uint16_t unknown_a3 = 0;
// Three variable-length fields follow here. They are in the same order as the
// unknown fields above.
struct G_SyncSetFlagState_6x6E_Decompressed {
le_uint16_t total_size = 0; // == sum of the following 3 fields
le_uint16_t entity_set_flags_size = 0;
le_uint16_t event_set_flags_size = 0;
le_uint16_t switch_flags_size = 0;
// Variable-length fields follow here:
// EntitySetFlags entity_set_flags; // Total size is set_flags_size
// le_uint16_t event_set_flags[event_set_flags_size / 2]; // Same order as in map files (NOT sorted by event_id)
// SwitchFlags switch_flags; // 0x200 bytes on v1 abd earlier; 0x240 bytes on v2 and later
struct EntitySetFlags {
le_uint32_t object_set_flags_offset = 0;
le_uint32_t num_object_sets = 0;
le_uint32_t enemy_set_flags_offset = 0;
le_uint32_t num_enemy_sets = 0;
// Variable-length fields follow here:
// le_uint16_t object_set_flags[num_object_sets];
// le_uint16_t enemy_set_flags[num_enemy_sets];
} __packed__;
} __packed__;
// 6x6F: Set quest flags (used while loading into game)
struct G_SetQuestFlags_6x6F {
struct G_SetQuestFlagsV1_6x6F {
G_UnusedHeader header;
QuestFlagsV1 quest_flags;
} __packed__;
struct G_SetQuestFlagsV2V3V4_6x6F {
G_UnusedHeader header;
QuestFlags quest_flags;
} __packed__;
@@ -4748,7 +4795,7 @@ struct G_SetQuestFlags_6x6F {
// and instead rearranged a bunch of things.
struct Telepipe {
/* 00 */ le_uint16_t client_id = 0xFFFF;
/* 00 */ le_uint16_t owner_client_id = 0xFFFF;
/* 02 */ le_uint16_t unknown_a1 = 0;
/* 04 */ le_uint32_t unknown_a2 = 0;
/* 08 */ le_float x = 0.0f;
@@ -4890,7 +4937,7 @@ struct G_SyncPlayerDispAndInventory_BB_6x70 {
// Offsets in this struct are relative to the overall command header
/* 0008 */ G_ExtendedHeader<G_ClientIDHeader> header = {{0x70, 0x00, 0x0000}, sizeof(G_SyncPlayerDispAndInventory_BB_6x70)};
/* 0010 */ G_SyncPlayerDispAndInventory_BaseV1 base;
/* 0128 */ pstring<TextEncoding::UTF16, 0x10> name;
/* 0128 */ pstring<TextEncoding::UTF16_ALWAYS_MARKED, 0x10> name;
/* 0148 */ PlayerStats stats;
/* 016C */ le_uint32_t num_items = 0;
/* 0170 */ parray<PlayerInventoryItem, 0x1E> items;
@@ -4953,12 +5000,14 @@ struct G_UpdateQuestFlag_V3_BB_6x75 : G_UpdateQuestFlag_DC_PC_6x75 {
le_uint16_t unused = 0;
} __packed__;
// 6x76: Enemy killed
// 6x76: Set entity set flags
// This command can only be used to set set flags, since the game performs a
// bitwise OR operation instead of a simple assignment.
struct G_EnemyKilled_6x76 {
G_EnemyIDHeader header;
le_uint16_t unknown_a1 = 0;
le_uint16_t unknown_a2 = 0; // Flags of some sort
struct G_SetEntitySetFlags_6x76 {
G_EnemyIDHeader header; // 1000-3FFF = enemy, 4000-FFFF = object
le_uint16_t floor = 0;
le_uint16_t flags = 0;
} __packed__;
// 6x77: Sync quest data
@@ -5007,29 +5056,25 @@ struct G_Unknown_6x7B {
G_ClientIDHeader header;
} __packed__;
// 6x7C: Set challenge mode data (not valid on Episode 3)
// 6x7C: Set Challenge records (not valid on Episode 3)
struct G_SetChallengeModeData_6x7C {
struct G_SetChallengeRecordsBase_6x7C {
G_UnusedHeader header;
le_uint16_t client_id = 0;
parray<uint8_t, 2> unknown_a1;
le_uint16_t unknown_a2 = 0;
parray<uint8_t, 2> unknown_a3;
parray<le_uint32_t, 0x17> unknown_a4;
parray<uint8_t, 4> unknown_a5;
le_uint16_t unknown_a6 = 0;
parray<uint8_t, 2> unknown_a7;
le_uint32_t unknown_a8 = 0;
le_uint32_t unknown_a9 = 0;
le_uint32_t unknown_a10 = 0;
le_uint32_t unknown_a11 = 0;
le_uint32_t unknown_a12 = 0;
parray<uint8_t, 0x34> unknown_a13;
struct Entry {
le_uint32_t unknown_a1 = 0;
le_uint32_t unknown_a2 = 0;
} __packed__;
parray<Entry, 3> entries;
} __packed__;
struct G_SetChallengeRecords_DC_6x7C : G_SetChallengeRecordsBase_6x7C {
PlayerRecordsDC_Challenge records;
} __packed__;
struct G_SetChallengeRecords_PC_6x7C : G_SetChallengeRecordsBase_6x7C {
PlayerRecordsPC_Challenge records;
} __packed__;
struct G_SetChallengeRecords_V3_6x7C : G_SetChallengeRecordsBase_6x7C {
PlayerRecordsV3_Challenge<false> records;
} __packed__;
struct G_SetChallengeRecords_BB_6x7C : G_SetChallengeRecordsBase_6x7C {
PlayerRecordsBB_Challenge records;
} __packed__;
// 6x7D: Set battle mode data (not valid on Episode 3)
@@ -5197,8 +5242,9 @@ struct G_Unknown_6x91 {
le_uint32_t unknown_a2 = 0;
le_uint16_t unknown_a3 = 0;
le_uint16_t unknown_a4 = 0;
le_uint16_t unknown_a5 = 0;
parray<uint8_t, 2> unknown_a6;
le_uint16_t switch_flag_num = 0;
uint8_t should_set = 0; // The switch flag is only set if this is equal to 1; otherwise it's cleared
uint8_t switch_flag_floor = 0;
} __packed__;
// 6x92: Unknown (not valid on Episode 3)
@@ -5213,9 +5259,9 @@ struct G_Unknown_6x92 {
struct G_ActivateTimedSwitch_6x93 {
G_UnusedHeader header;
le_uint16_t floor = 0;
le_uint16_t switch_id = 0;
uint8_t unknown_a1 = 0; // Logic is different if this is 1 vs. any other value
le_uint16_t switch_flag_floor = 0;
le_uint16_t switch_flag_num = 0;
uint8_t should_set = 0; // The switch flag is only set if this is equal to 1; otherwise it's cleared
parray<uint8_t, 3> unused;
} __packed__;
@@ -5324,10 +5370,9 @@ struct G_GalGryphonBossActions_6xA0 {
parray<le_uint32_t, 4> unknown_a4;
} __packed__;
// 6xA1: Unknown (not valid on pre-V3) (protected on V3/V4)
// Part of revive process. Occurs right after revive command; function unclear.
// 6xA1: Revive player (not valid on pre-V3) (protected on V3/V4)
struct G_Unknown_6xA1 {
struct G_RevivePlayer_V3_BB_6xA1 {
G_ClientIDHeader header;
} __packed__;
@@ -6728,7 +6773,7 @@ struct G_SetTournamentPlayerDecks_Ep3_6xB4x3D_Entry {
struct G_SetTournamentPlayerDecks_Ep3NTE_6xB4x3D {
/* 0000 */ G_CardBattleCommandHeader header = {0xB4, sizeof(G_SetTournamentPlayerDecks_Ep3NTE_6xB4x3D) / 4, 0, 0x3D, 0, 0, 0};
/* 0008 */ Episode3::Rules rules;
/* 0008 */ Episode3::RulesTrial rules;
/* 0014 */ parray<G_SetTournamentPlayerDecks_Ep3_6xB4x3D_Entry, 4> entries;
/* 01BC */ le_uint32_t map_number = 0;
/* 01C0 */ uint8_t player_slot = 0; // Which deck slot is editable by the client
+112
View File
@@ -1,11 +1,123 @@
#include "CommonItemSet.hh"
#include "AFSArchive.hh"
#include "EnemyType.hh"
#include "GSLArchive.hh"
#include "StaticGameData.hh"
using namespace std;
template <typename IntT, size_t Count>
JSON to_json(const parray<IntT, Count>& v) {
auto ret = JSON::list();
for (size_t z = 0; z < Count; z++) {
ret.emplace_back(v[z]);
}
return ret;
}
template <typename IntT, size_t Count>
JSON to_json(const parray<CommonItemSet::Table::Range<IntT>, Count>& v) {
auto ret = JSON::list();
for (size_t z = 0; z < Count; z++) {
ret.emplace_back(to_json(v[z]));
}
return ret;
}
template <typename IntT>
JSON to_json(const CommonItemSet::Table::Range<IntT>& v) {
if (v.min == v.max) {
return JSON(v.min);
} else {
return JSON::list({v.min, v.max});
}
}
template <typename IntT, size_t Count1, size_t Count2>
JSON to_json(const parray<parray<IntT, Count2>, Count1>& v) {
auto ret = JSON::list();
for (size_t z = 0; z < Count1; z++) {
ret.emplace_back(to_json(v[z]));
}
return ret;
}
JSON CommonItemSet::Table::json() const {
JSON enemy_meseta_ranges_json = JSON::dict();
JSON enemy_type_drop_probs_json = JSON::dict();
JSON enemy_item_classes_json = JSON::dict();
for (size_t z = 0; z < 0x64; z++) {
static const array<Episode, 3> episodes = {Episode::EP1, Episode::EP2, Episode::EP4};
for (Episode episode : episodes) {
JSON enemy_meseta_ranges_episode_json = JSON::dict();
JSON enemy_type_drop_probs_episode_json = JSON::dict();
JSON enemy_item_classes_episode_json = JSON::dict();
for (auto type : enemy_types_for_rare_table_index(episode, z)) {
string type_str = name_for_enum(type);
enemy_meseta_ranges_episode_json.emplace(type_str, to_json(this->enemy_meseta_ranges[z]));
enemy_type_drop_probs_episode_json.emplace(type_str, this->enemy_type_drop_probs[z]);
enemy_item_classes_episode_json.emplace(type_str, this->enemy_item_classes[z]);
}
string name = name_for_episode(episode);
enemy_meseta_ranges_json.emplace(name, std::move(enemy_meseta_ranges_episode_json));
enemy_type_drop_probs_json.emplace(name, std::move(enemy_type_drop_probs_episode_json));
enemy_item_classes_json.emplace(name, std::move(enemy_item_classes_episode_json));
}
}
return JSON::dict({
{"BaseWeaponTypeProbTable", to_json(this->base_weapon_type_prob_table)},
{"SubtypeBaseTable", to_json(this->subtype_base_table)},
{"SubtypeAreaLengthTable", to_json(this->subtype_area_length_table)},
{"GrindProbTable", to_json(this->grind_prob_table)},
{"ArmorShieldTypeIndexProbTable", to_json(this->armor_shield_type_index_prob_table)},
{"ArmorSlotCountProbTable", to_json(this->armor_slot_count_prob_table)},
{"EnemyMesetaRanges", std::move(enemy_meseta_ranges_json)},
{"EnemyTypeDropProbs", std::move(enemy_type_drop_probs_json)},
{"EnemyItemClasses", std::move(enemy_item_classes_json)},
{"BoxMesetaRanges", to_json(this->box_meseta_ranges)},
{"HasRareBonusValueProbTable", this->has_rare_bonus_value_prob_table},
{"BonusValueProbTable", to_json(this->bonus_value_prob_table)},
{"NonRareBonusProbSpec", to_json(this->nonrare_bonus_prob_spec)},
{"BonusTypeProbTable", to_json(this->bonus_type_prob_table)},
{"SpecialMult", to_json(this->special_mult)},
{"SpecialPercent", to_json(this->special_percent)},
{"ToolClassProbTable", to_json(this->tool_class_prob_table)},
{"TechniqueIndexProbTable", to_json(this->technique_index_prob_table)},
{"TechniqueLevelRanges", to_json(this->technique_level_ranges)},
{"ArmorOrShieldTypeBias", this->armor_or_shield_type_bias},
{"UnitMaxStarsTable", to_json(this->unit_max_stars_table)},
{"BoxItemClassProbTable", to_json(this->box_item_class_prob_table)},
});
}
JSON CommonItemSet::json() const {
auto modes_dict = JSON::dict();
static const array<GameMode, 4> modes = {GameMode::NORMAL, GameMode::BATTLE, GameMode::CHALLENGE, GameMode::SOLO};
for (const auto& mode : modes) {
auto episodes_dict = JSON::dict();
static const array<Episode, 3> episodes = {Episode::EP1, Episode::EP2, Episode::EP4};
for (const auto& episode : episodes) {
auto difficulty_dict = JSON::dict();
for (uint8_t difficulty = 0; difficulty < 4; difficulty++) {
auto section_id_dict = JSON::dict();
for (uint8_t section_id = 0; section_id < 10; section_id++) {
try {
auto table = this->get_table(episode, mode, difficulty, section_id);
section_id_dict.emplace(name_for_section_id(section_id), table->json());
} catch (const runtime_error&) {
}
}
difficulty_dict.emplace(token_name_for_difficulty(difficulty), std::move(section_id_dict));
}
episodes_dict.emplace(token_name_for_episode(episode), std::move(difficulty_dict));
}
modes_dict.emplace(name_for_mode(mode), std::move(episodes_dict));
}
return modes_dict;
}
CommonItemSet::Table::Table(const StringReader& r, bool is_big_endian, bool is_v3) {
if (is_big_endian) {
this->parse_itempt_t<true>(r, is_v3);
+7 -4
View File
@@ -2,6 +2,7 @@
#include <array>
#include <phosg/Encoding.hh>
#include <phosg/JSON.hh>
#include "GSLArchive.hh"
#include "PSOEncryption.hh"
@@ -45,6 +46,7 @@ public:
parray<parray<uint8_t, 10>, 7> box_item_class_prob_table;
void print_enemy_table(FILE* stream) const;
JSON json() const;
private:
template <bool IsBigEndian>
@@ -256,6 +258,7 @@ public:
};
std::shared_ptr<const Table> get_table(Episode episode, GameMode mode, uint8_t difficulty, uint8_t secid) const;
JSON json() const;
protected:
CommonItemSet() = default;
@@ -298,22 +301,22 @@ struct ProbabilityTable {
return this->items[--this->count];
}
void shuffle(PSOLFGEncryption& random_crypt) {
void shuffle(std::shared_ptr<PSOLFGEncryption> opt_rand_crypt) {
for (size_t z = 1; z < this->count; z++) {
size_t other_z = random_crypt.next() % (z + 1);
size_t other_z = random_from_optional_crypt(opt_rand_crypt) % (z + 1);
ItemT t = this->items[z];
this->items[z] = this->items[other_z];
this->items[other_z] = t;
}
}
ItemT sample(PSOLFGEncryption& random_crypt) const {
ItemT sample(std::shared_ptr<PSOLFGEncryption> opt_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_crypt.next() % this->count];
return this->items[random_from_optional_crypt(opt_rand_crypt) % this->count];
}
}
};
+123 -110
View File
@@ -38,6 +38,8 @@ const char* name_for_enum<EnemyType>(EnemyType type) {
return "BOOTA";
case EnemyType::BULCLAW:
return "BULCLAW";
case EnemyType::BULK:
return "BULK";
case EnemyType::CANADINE:
return "CANADINE";
case EnemyType::CANADINE_GROUP:
@@ -287,6 +289,7 @@ EnemyType enum_for_name<EnemyType>(const char* name) {
{"BOOMA", EnemyType::BOOMA},
{"BOOTA", EnemyType::BOOTA},
{"BULCLAW", EnemyType::BULCLAW},
{"BULK", EnemyType::BULK},
{"CANADINE", EnemyType::CANADINE},
{"CANADINE_GROUP", EnemyType::CANADINE_GROUP},
{"CANANE", EnemyType::CANANE},
@@ -409,150 +412,152 @@ bool enemy_type_valid_for_episode(Episode episode, EnemyType enemy_type) {
switch (episode) {
case Episode::EP1:
switch (enemy_type) {
case EnemyType::MOTHMANT:
case EnemyType::MONEST:
case EnemyType::SAVAGE_WOLF:
case EnemyType::BARBAROUS_WOLF:
case EnemyType::POISON_LILY:
case EnemyType::NAR_LILY:
case EnemyType::SINOW_BEAT:
case EnemyType::CANADINE:
case EnemyType::CANADINE_GROUP:
case EnemyType::CANANE:
case EnemyType::CHAOS_SORCERER:
case EnemyType::CHAOS_BRINGER:
case EnemyType::DARK_BELRA:
case EnemyType::DE_ROL_LE:
case EnemyType::DRAGON:
case EnemyType::SINOW_GOLD:
case EnemyType::RAG_RAPPY:
case EnemyType::AL_RAPPY:
case EnemyType::NANO_DRAGON:
case EnemyType::DUBCHIC:
case EnemyType::GILLCHIC:
case EnemyType::GARANZ:
case EnemyType::DARK_GUNNER:
case EnemyType::BARBAROUS_WOLF:
case EnemyType::BOOMA:
case EnemyType::BULCLAW:
case EnemyType::BULK:
case EnemyType::CANADINE_GROUP:
case EnemyType::CANADINE:
case EnemyType::CANANE:
case EnemyType::CHAOS_BRINGER:
case EnemyType::CHAOS_SORCERER:
case EnemyType::CLAW:
case EnemyType::VOL_OPT_2:
case EnemyType::POUILLY_SLIME:
case EnemyType::POFUILLY_SLIME:
case EnemyType::PAN_ARMS:
case EnemyType::HIDOOM:
case EnemyType::MIGIUM:
case EnemyType::DARVANT:
case EnemyType::DARVANT_ULTIMATE:
case EnemyType::DARK_BELRA:
case EnemyType::DARK_FALZ_1:
case EnemyType::DARK_FALZ_2:
case EnemyType::DARK_FALZ_3:
case EnemyType::HILDEBEAR:
case EnemyType::HILDEBLUE:
case EnemyType::BOOMA:
case EnemyType::GOBOOMA:
case EnemyType::GIGOBOOMA:
case EnemyType::GRASS_ASSASSIN:
case EnemyType::EVIL_SHARK:
case EnemyType::PAL_SHARK:
case EnemyType::GUIL_SHARK:
case EnemyType::DARK_GUNNER:
case EnemyType::DARVANT_ULTIMATE:
case EnemyType::DARVANT:
case EnemyType::DE_ROL_LE:
case EnemyType::DEATH_GUNNER:
case EnemyType::DELSABER:
case EnemyType::DIMENIAN:
case EnemyType::DRAGON:
case EnemyType::DUBCHIC:
case EnemyType::EVIL_SHARK:
case EnemyType::GARANZ:
case EnemyType::GIGOBOOMA:
case EnemyType::GILLCHIC:
case EnemyType::GOBOOMA:
case EnemyType::GRASS_ASSASSIN:
case EnemyType::GUIL_SHARK:
case EnemyType::HIDOOM:
case EnemyType::HILDEBEAR:
case EnemyType::HILDEBLUE:
case EnemyType::LA_DIMENIAN:
case EnemyType::MIGIUM:
case EnemyType::MONEST:
case EnemyType::MOTHMANT:
case EnemyType::NANO_DRAGON:
case EnemyType::NAR_LILY:
case EnemyType::PAL_SHARK:
case EnemyType::PAN_ARMS:
case EnemyType::POFUILLY_SLIME:
case EnemyType::POISON_LILY:
case EnemyType::POUILLY_SLIME:
case EnemyType::RAG_RAPPY:
case EnemyType::SAVAGE_WOLF:
case EnemyType::SINOW_BEAT:
case EnemyType::SINOW_GOLD:
case EnemyType::SO_DIMENIAN:
case EnemyType::VOL_OPT_2:
return true;
default:
return false;
}
case Episode::EP2:
switch (enemy_type) {
case EnemyType::MOTHMANT:
case EnemyType::MONEST:
case EnemyType::SAVAGE_WOLF:
case EnemyType::BARBAROUS_WOLF:
case EnemyType::POISON_LILY:
case EnemyType::NAR_LILY:
case EnemyType::SINOW_BERILL:
case EnemyType::GEE:
case EnemyType::CHAOS_SORCERER:
case EnemyType::DELBITER:
case EnemyType::DARK_BELRA:
case EnemyType::BARBA_RAY:
case EnemyType::GOL_DRAGON:
case EnemyType::SINOW_SPIGELL:
case EnemyType::RAG_RAPPY:
case EnemyType::LOVE_RAPPY:
case EnemyType::SAINT_RAPPY:
case EnemyType::EGG_RAPPY:
case EnemyType::HALLO_RAPPY:
case EnemyType::GI_GUE:
case EnemyType::DUBCHIC:
case EnemyType::GILLCHIC:
case EnemyType::GARANZ:
case EnemyType::GAL_GRYPHON:
case EnemyType::EPSILON:
case EnemyType::BARBAROUS_WOLF:
case EnemyType::CHAOS_SORCERER:
case EnemyType::DARK_BELRA:
case EnemyType::DEL_LILY:
case EnemyType::ILL_GILL:
case EnemyType::OLGA_FLOW_1:
case EnemyType::OLGA_FLOW_2:
case EnemyType::GAEL:
case EnemyType::DELBITER:
case EnemyType::DELDEPTH:
case EnemyType::PAN_ARMS:
case EnemyType::HIDOOM:
case EnemyType::MIGIUM:
case EnemyType::MERICAROL:
case EnemyType::UL_GIBBON:
case EnemyType::ZOL_GIBBON:
case EnemyType::GIBBLES:
case EnemyType::MORFOS:
case EnemyType::RECOBOX:
case EnemyType::RECON:
case EnemyType::SINOW_ZOA:
case EnemyType::SINOW_ZELE:
case EnemyType::MERIKLE:
case EnemyType::MERICUS:
case EnemyType::HILDEBEAR:
case EnemyType::HILDEBLUE:
case EnemyType::MERILLIA:
case EnemyType::MERILTAS:
case EnemyType::GRASS_ASSASSIN:
case EnemyType::DOLMOLM:
case EnemyType::DOLMDARL:
case EnemyType::DELSABER:
case EnemyType::DIMENIAN:
case EnemyType::DOLMDARL:
case EnemyType::DOLMOLM:
case EnemyType::DUBCHIC:
case EnemyType::EGG_RAPPY:
case EnemyType::EPSILON:
case EnemyType::GAEL:
case EnemyType::GAL_GRYPHON:
case EnemyType::GARANZ:
case EnemyType::GEE:
case EnemyType::GI_GUE:
case EnemyType::GIBBLES:
case EnemyType::GILLCHIC:
case EnemyType::GOL_DRAGON:
case EnemyType::GRASS_ASSASSIN:
case EnemyType::HALLO_RAPPY:
case EnemyType::HIDOOM:
case EnemyType::HILDEBEAR:
case EnemyType::HILDEBLUE:
case EnemyType::ILL_GILL:
case EnemyType::LA_DIMENIAN:
case EnemyType::LOVE_RAPPY:
case EnemyType::MERICAROL:
case EnemyType::MERICUS:
case EnemyType::MERIKLE:
case EnemyType::MERILLIA:
case EnemyType::MERILTAS:
case EnemyType::MIGIUM:
case EnemyType::MONEST:
case EnemyType::MORFOS:
case EnemyType::MOTHMANT:
case EnemyType::NAR_LILY:
case EnemyType::OLGA_FLOW_1:
case EnemyType::OLGA_FLOW_2:
case EnemyType::PAN_ARMS:
case EnemyType::POISON_LILY:
case EnemyType::RAG_RAPPY:
case EnemyType::RECOBOX:
case EnemyType::RECON:
case EnemyType::SAINT_RAPPY:
case EnemyType::SAVAGE_WOLF:
case EnemyType::SINOW_BERILL:
case EnemyType::SINOW_SPIGELL:
case EnemyType::SINOW_ZELE:
case EnemyType::SINOW_ZOA:
case EnemyType::SO_DIMENIAN:
case EnemyType::UL_GIBBON:
case EnemyType::ZOL_GIBBON:
return true;
default:
return false;
}
case Episode::EP4:
switch (enemy_type) {
case EnemyType::BOOTA:
case EnemyType::ZE_BOOTA:
case EnemyType::BA_BOOTA:
case EnemyType::SAND_RAPPY:
case EnemyType::DEL_RAPPY:
case EnemyType::ZU:
case EnemyType::PAZUZU:
case EnemyType::ASTARK:
case EnemyType::SATELLITE_LIZARD:
case EnemyType::YOWIE:
case EnemyType::DORPHON:
case EnemyType::DORPHON_ECLAIR:
case EnemyType::GORAN:
case EnemyType::PYRO_GORAN:
case EnemyType::GORAN_DETONATOR:
case EnemyType::SAND_RAPPY_ALT:
case EnemyType::BA_BOOTA:
case EnemyType::BOOTA:
case EnemyType::DEL_RAPPY_ALT:
case EnemyType::DEL_RAPPY:
case EnemyType::DORPHON_ECLAIR:
case EnemyType::DORPHON:
case EnemyType::GIRTABLULU:
case EnemyType::GORAN_DETONATOR:
case EnemyType::GORAN:
case EnemyType::KONDRIEU:
case EnemyType::MERISSA_A:
case EnemyType::MERISSA_AA:
case EnemyType::ZU_ALT:
case EnemyType::PAZUZU_ALT:
case EnemyType::SATELLITE_LIZARD_ALT:
case EnemyType::YOWIE_ALT:
case EnemyType::GIRTABLULU:
case EnemyType::PAZUZU:
case EnemyType::PYRO_GORAN:
case EnemyType::SAINT_MILLION:
case EnemyType::SAND_RAPPY_ALT:
case EnemyType::SAND_RAPPY:
case EnemyType::SATELLITE_LIZARD_ALT:
case EnemyType::SATELLITE_LIZARD:
case EnemyType::SHAMBERTIN:
case EnemyType::KONDRIEU:
case EnemyType::YOWIE_ALT:
case EnemyType::YOWIE:
case EnemyType::ZE_BOOTA:
case EnemyType::ZU_ALT:
case EnemyType::ZU:
return true;
default:
return false;
@@ -611,8 +616,10 @@ uint8_t battle_param_index_for_enemy_type(Episode episode, EnemyType enemy_type)
case EnemyType::GARANZ:
return 0x1D;
case EnemyType::DARK_GUNNER:
case EnemyType::DEATH_GUNNER:
return 0x1E;
case EnemyType::BULCLAW:
case EnemyType::BULK:
return 0x1F;
case EnemyType::CLAW:
return 0x20;
@@ -834,9 +841,11 @@ uint8_t battle_param_index_for_enemy_type(Episode episode, EnemyType enemy_type)
case EnemyType::GIRTABLULU:
return 0x1F;
case EnemyType::SAINT_MILLION:
case EnemyType::SHAMBERTIN:
case EnemyType::KONDRIEU:
return 0x22;
case EnemyType::SHAMBERTIN:
return 0x26;
case EnemyType::KONDRIEU:
return 0x2A;
default:
throw runtime_error(string_printf("%s does not have battle parameters in Episode 4", name_for_enum(enemy_type)));
}
@@ -863,6 +872,8 @@ uint8_t rare_table_index_for_enemy_type(EnemyType enemy_type) {
return 0x09;
case EnemyType::BOOTA:
return 0x4D;
case EnemyType::BULK:
return 0x27;
case EnemyType::BULCLAW:
return 0x28;
case EnemyType::CANADINE:
@@ -884,6 +895,8 @@ uint8_t rare_table_index_for_enemy_type(EnemyType enemy_type) {
return 0x2F;
case EnemyType::DARK_GUNNER:
return 0x22;
case EnemyType::DEATH_GUNNER:
return 0x23;
case EnemyType::DE_ROL_LE:
return 0x2D;
case EnemyType::DEL_LILY:
+1
View File
@@ -20,6 +20,7 @@ enum class EnemyType {
BOOMA,
BOOTA,
BULCLAW,
BULK,
CANADINE,
CANADINE_GROUP,
CANANE,
+7 -7
View File
@@ -39,17 +39,17 @@ private:
public:
parray<AssistEffect, 4> assist_effects;
std::shared_ptr<const CardIndex::CardEntry> assist_card_defs[4];
bcarray<std::shared_ptr<const CardIndex::CardEntry>, 4> assist_card_defs;
uint32_t num_assist_cards_set;
parray<uint8_t, 4> client_ids_with_assists;
parray<AssistEffect, 4> active_assist_effects;
std::shared_ptr<const CardIndex::CardEntry> active_assist_card_defs[4];
bcarray<std::shared_ptr<const CardIndex::CardEntry>, 4> active_assist_card_defs;
uint32_t num_active_assists;
std::shared_ptr<HandAndEquipState> hand_and_equip_states[4];
std::shared_ptr<parray<CardShortStatus, 0x10>> card_short_statuses[4];
std::shared_ptr<DeckEntry> deck_entries[4];
std::shared_ptr<parray<ActionChainWithConds, 9>> set_card_action_chains[4];
std::shared_ptr<parray<ActionMetadata, 9>> set_card_action_metadatas[4];
bcarray<std::shared_ptr<HandAndEquipState>, 4> hand_and_equip_states;
bcarray<std::shared_ptr<parray<CardShortStatus, 0x10>>, 4> card_short_statuses;
bcarray<std::shared_ptr<DeckEntry>, 4> deck_entries;
bcarray<std::shared_ptr<parray<ActionChainWithConds, 9>>, 4> set_card_action_chains;
bcarray<std::shared_ptr<parray<ActionMetadata, 9>>, 4> set_card_action_metadatas;
};
} // namespace Episode3
+1 -3
View File
@@ -96,9 +96,7 @@ private:
class BattleRecordPlayer {
public:
BattleRecordPlayer(
std::shared_ptr<const BattleRecord> rec,
std::shared_ptr<struct event_base> base);
BattleRecordPlayer(std::shared_ptr<const BattleRecord> rec, std::shared_ptr<struct event_base> base);
~BattleRecordPlayer() = default;
std::shared_ptr<const BattleRecord> get_record() const;
+11 -9
View File
@@ -204,7 +204,7 @@ ssize_t Card::apply_abnormal_condition(
}
}
string cond_str = cond.str();
string cond_str = cond.str(s);
log.debug("wrote condition %zd => %s", cond_index, cond_str.c_str());
if (!is_nte) {
@@ -213,7 +213,7 @@ ssize_t Card::apply_abnormal_condition(
if (this->action_chain.conditions[z].type == ConditionType::NONE) {
continue;
}
string cond_str = cond.str();
string cond_str = cond.str(s);
log.debug("sorted conditions: [%zu] => %s", z, cond_str.c_str());
}
}
@@ -914,7 +914,7 @@ void Card::compute_action_chain_results(bool apply_action_conditions, bool ignor
this->action_chain.chain.tp_effect_bonus = 0;
log.debug("(initial) medium=%s, strike_count=%hhu, ap_effect_bonus=%hhd, tp_effect_bonus=%hhd",
name_for_attack_medium(this->action_chain.chain.attack_medium),
name_for_enum(this->action_chain.chain.attack_medium),
this->action_chain.chain.strike_count,
this->action_chain.chain.ap_effect_bonus,
this->action_chain.chain.tp_effect_bonus);
@@ -1361,11 +1361,12 @@ bool Card::is_guard_item() const {
}
bool Card::unknown_80236554(shared_ptr<Card> other_card, const ActionState* as) {
auto log = this->server()->log_stack(other_card
auto s = this->server();
auto log = s->log_stack(other_card
? 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())
: string_printf("unknown_80236554(@%04hX #%04hX, null): ", this->get_card_ref(), this->get_card_id()));
if (as) {
string as_str = as->str();
string as_str = as->str(s);
log.debug("as = %s", as_str.c_str());
} else {
log.debug("as = null");
@@ -1403,8 +1404,8 @@ bool Card::unknown_80236554(shared_ptr<Card> other_card, const ActionState* as)
log.debug("last attack damage stats cleared");
if (other_card) {
this->server()->card_special->apply_action_conditions(0x03, other_card, this->shared_from_this(), 0x20, as);
this->server()->card_special->apply_action_conditions(0x17, other_card, this->shared_from_this(), 0x40, as);
s->card_special->apply_action_conditions(0x03, other_card, this->shared_from_this(), 0x20, as);
s->card_special->apply_action_conditions(0x17, other_card, this->shared_from_this(), 0x40, as);
if (other_card->action_chain.check_flag(0x20000)) {
this->action_metadata.attack_bonus = 0;
return ret;
@@ -1417,8 +1418,9 @@ bool Card::unknown_80236554(shared_ptr<Card> other_card, const ActionState* as)
}
void Card::unknown_802362D8(shared_ptr<Card> other_card) {
auto s = this->server();
for (size_t client_id = 0; client_id < 4; client_id++) {
auto ps = this->server()->player_states[client_id];
auto ps = s->player_states[client_id];
if (ps) {
shared_ptr<Card> card = ps->get_sc_card();
if (card) {
@@ -1493,7 +1495,7 @@ void Card::apply_attack_result() {
} else {
auto target_sc = target_ps->get_sc_card();
if (!(target_sc->card_flags & 2)) {
temp_chain.chain.target_card_refs[temp_chain.chain.target_card_ref_count] = candidate_card->get_card_ref();
temp_chain.chain.target_card_refs[temp_chain.chain.target_card_ref_count] = target_sc->get_card_ref();
temp_chain.chain.target_card_ref_count++;
}
}
+4 -11
View File
@@ -15,11 +15,7 @@ class PlayerState;
class Card : public std::enable_shared_from_this<Card> {
public:
Card(
uint16_t card_id,
uint16_t card_ref,
uint16_t client_id,
std::shared_ptr<Server> server);
Card(uint16_t card_id, uint16_t card_ref, uint16_t client_id, std::shared_ptr<Server> server);
void init();
std::shared_ptr<Server> server();
std::shared_ptr<const Server> server() const;
@@ -47,8 +43,7 @@ public:
G_ApplyConditionEffect_Ep3_6xB4x06* cmd,
size_t strike_number,
int16_t* out_effective_damage);
int16_t compute_defense_power_for_attacker_card(
std::shared_ptr<const Card> attacker_card);
int16_t compute_defense_power_for_attacker_card(std::shared_ptr<const Card> attacker_card);
void destroy_set_card(std::shared_ptr<Card> attacker_card);
int32_t error_code_for_move_to_location(const Location& loc) const;
void execute_attack(std::shared_ptr<Card> attacker_card);
@@ -73,12 +68,10 @@ public:
void send_6xB4x4E_4C_4D_if_needed(bool always_send = false);
void send_6xB4x4E_if_needed(bool always_send = false);
void set_current_and_max_hp(int16_t hp);
void set_current_hp(
uint32_t new_hp, bool propagate_shared_hp = true, bool enforce_max_hp = true);
void set_current_hp(uint32_t new_hp, bool propagate_shared_hp = true, bool enforce_max_hp = true);
void update_stats_on_destruction();
void clear_action_chain_and_metadata_and_most_flags();
void compute_action_chain_results(
bool apply_action_conditions, bool ignore_this_card_ap_tp);
void compute_action_chain_results(bool apply_action_conditions, bool ignore_this_card_ap_tp);
void unknown_802380C0();
void unknown_80237F98(bool require_condition_20_or_21);
void unknown_80237F88();
+62 -55
View File
@@ -393,9 +393,7 @@ 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);
this->execute_effect(
*defender_cond, defender_card, expr_value, defender_cond->value,
orig_eff->type, flags, attacker_card_ref);
this->execute_effect(*defender_cond, defender_card, expr_value, defender_cond->value, orig_eff->type, flags, attacker_card_ref);
if (flags & 4) {
if (is_nte || !(defender_card->card_flags & 2)) {
defender_card->compute_action_chain_results(true, false);
@@ -460,7 +458,7 @@ bool CardSpecial::apply_stat_deltas_to_card_from_condition_and_clear_cond(Condit
auto log = s->log_stack(string_printf("apply_stat_deltas_to_card_from_condition_and_clear_cond(@%04hX #%04hX): ", card->get_card_ref(), card->get_card_id()));
bool is_nte = s->options.is_nte();
string cond_str = cond.str();
string cond_str = cond.str(s);
log.debug("cond: %s", cond_str.c_str());
ConditionType cond_type = cond.type;
@@ -589,7 +587,7 @@ bool CardSpecial::apply_stat_deltas_to_card_from_condition_and_clear_cond(Condit
break;
trial_unimplemented:
default:
log.debug("%s: no adjustments for condition type", name_for_condition_type(cond_type));
log.debug("%s: no adjustments for condition type", name_for_enum(cond_type));
break;
}
@@ -706,7 +704,7 @@ CardSpecial::AttackEnvStats CardSpecial::compute_attack_env_stats(
auto log = s->log_stack("compute_attack_env_stats: ");
bool is_nte = s->options.is_nte();
string pa_str = pa.str();
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);
auto attacker_card = s->card_for_set_card_ref(pa.attacker_card_ref);
@@ -834,16 +832,16 @@ CardSpecial::AttackEnvStats CardSpecial::compute_attack_env_stats(
size_t z = 0;
uint16_t z_ref = pa.attacker_card_ref;
// Note: The (z < 9) conditions in these two loops are not present in the
// 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 < 9) && ((z_ref = pa.action_card_refs[z]) != 0xFFFF));
((target_card_ref != z_ref) && (z < 8) && ((z_ref = pa.action_card_refs[z]) != 0xFFFF));
z++) {
}
ast.action_cards_ap = 0;
ast.action_cards_tp = 0;
for (; (z < 9) && (pa.action_card_refs[z] != 0xFFFF); z++) {
for (; (z < 8) && (pa.action_card_refs[z] != 0xFFFF); z++) {
auto ce = s->definition_for_card_ref(pa.action_card_refs[z]);
if (ce) {
if (ce->def.ap.type != CardDefinition::Stat::Type::MINUS_STAT) {
@@ -1192,7 +1190,8 @@ shared_ptr<Card> CardSpecial::compute_replaced_target_based_on_conditions(
}
StatSwapType CardSpecial::compute_stat_swap_type(shared_ptr<const Card> card) const {
auto log = this->server()->log_stack(string_printf("compute_stat_swap_type(@%04hX #%04hX): ", card->get_card_ref(), card->get_card_id()));
auto s = this->server();
auto log = s->log_stack(string_printf("compute_stat_swap_type(@%04hX #%04hX): ", card->get_card_ref(), card->get_card_id()));
if (!card) {
log.debug("card is missing");
return StatSwapType::NONE;
@@ -1203,7 +1202,7 @@ StatSwapType CardSpecial::compute_stat_swap_type(shared_ptr<const Card> card) co
auto& cond = card->action_chain.conditions[cond_index];
if (cond.type != ConditionType::NONE) {
auto cond_log = log.sub(string_printf("(%zu) ", cond_index));
string cond_str = cond.str();
string cond_str = cond.str(s);
cond_log.debug("%s", cond_str.c_str());
if (!this->card_ref_has_ability_trap(cond)) {
if (cond.type == ConditionType::UNKNOWN_75) {
@@ -1423,15 +1422,14 @@ bool CardSpecial::evaluate_effect_arg2_condition(
attacker_card_ref = as.original_attacker_card_ref;
}
bool is_nte = s->options.is_nte();
auto set_card = s->card_for_set_card_ref(set_card_ref);
bool set_card_has_ability_trap =
(!s->options.is_nte() &&
set_card &&
this->card_has_condition_with_ref(set_card, ConditionType::ABILITY_TRAP, 0xFFFF, 0xFFFF));
(!is_nte && set_card && this->card_has_condition_with_ref(set_card, ConditionType::ABILITY_TRAP, 0xFFFF, 0xFFFF));
switch (arg2_text[0]) {
case 'C':
if (s->options.is_nte()) {
if (is_nte) {
return false;
}
card = s->card_for_set_card_ref(set_card_ref);
@@ -1533,16 +1531,16 @@ bool CardSpecial::evaluate_effect_arg2_condition(
uint16_t action_card_ref = as.action_card_refs[z];
if (action_card_ref != 0xFFFF) {
auto ce = s->definition_for_card_ref(action_card_ref);
if (card_class_is_tech_like(ce->def.card_class(), s->options.is_nte())) {
if (card_class_is_tech_like(ce->def.card_class(), is_nte)) {
return true;
}
}
}
return false;
case 0x04: // n04
return card->action_chain.check_flag(s->options.is_nte() ? 0x00000080 : 0x0001E000);
return card->action_chain.check_flag(is_nte ? 0x00000080 : 0x0001E000);
case 0x05: // n05
return card->action_chain.check_flag(s->options.is_nte() ? 0x00000002 : 0x00001E00);
return card->action_chain.check_flag(is_nte ? 0x00000002 : 0x00001E00);
case 0x06: // n06
return (card->get_definition()->def.card_class() == CardClass::NATIVE_CREATURE);
case 0x07: // n07
@@ -1560,9 +1558,8 @@ bool CardSpecial::evaluate_effect_arg2_condition(
case 0x0D: { // n13
auto ce = card->get_definition();
return ((ce->def.card_class() == CardClass::GUARD_ITEM) ||
(!s->options.is_nte() && (ce->def.card_class() == CardClass::MAG_ITEM)) ||
s->ruler_server->find_condition_on_card_ref(
card->get_card_ref(), ConditionType::GUARD_CREATURE, 0, 0, 0));
(!is_nte && (ce->def.card_class() == CardClass::MAG_ITEM)) ||
s->ruler_server->find_condition_on_card_ref(card->get_card_ref(), ConditionType::GUARD_CREATURE, 0, 0, 0));
}
case 0x0E: // n14
return card->get_definition()->def.is_sc();
@@ -1600,7 +1597,7 @@ bool CardSpecial::evaluate_effect_arg2_condition(
return s->ruler_server->find_condition_on_card_ref(
card->get_card_ref(), ConditionType::FREEZE, 0, 0, 0);
case 0x15: { // n21
if (!s->options.is_nte()) {
if (!is_nte) {
uint8_t client_id = client_id_for_card_ref(sc_card_ref);
if (client_id != 0xFF) {
return card->action_chain.check_flag(0x00002000 << client_id);
@@ -1609,7 +1606,7 @@ bool CardSpecial::evaluate_effect_arg2_condition(
return false;
}
case 0x16: { // n22
if (!s->options.is_nte()) {
if (!is_nte) {
uint8_t client_id = client_id_for_card_ref(sc_card_ref);
if (client_id != 0xFF) {
return card->action_chain.check_flag(0x00000200 << client_id);
@@ -1637,7 +1634,7 @@ bool CardSpecial::evaluate_effect_arg2_condition(
card, ConditionType::ANY, set_card_ref, ((v % 10) == 0) ? 0xFF : (v % 10)) != nullptr);
}
case 'r':
return (!set_card_has_ability_trap || s->options.is_nte()) && (random_percent < atoi(arg2_text + 1));
return (!set_card_has_ability_trap || is_nte) && (random_percent < atoi(arg2_text + 1));
case 's': {
auto ce = card->get_definition();
return ((ce->def.self_cost >= arg2_text[1] - '0') &&
@@ -1651,7 +1648,7 @@ bool CardSpecial::evaluate_effect_arg2_condition(
uint8_t v = atoi(arg2_text + 1);
// TODO: Figure out what this logic actually does and rename the variables
// or comment it appropriately.
if (s->options.is_nte()) {
if (is_nte) {
return (v < set_card->unknown_a9);
} else if (when == 4) {
uint32_t y = set_card->unknown_a9 & 0xFFFFFFFE;
@@ -1775,8 +1772,8 @@ bool CardSpecial::execute_effect(
auto s = this->server();
auto log = s->log_stack(string_printf("execute_effect(@%04hX #%04hX): ", card->get_card_ref(), card->get_card_id()));
{
string cond_str = cond.str();
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, name_for_condition_type(cond_type), unknown_p7, attacker_card_ref);
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, name_for_enum(cond_type), unknown_p7, attacker_card_ref);
}
bool is_nte = s->options.is_nte();
@@ -2460,13 +2457,13 @@ bool CardSpecial::execute_effect(
[[fallthrough]];
case ConditionType::SLAYERS_ASSASSINS:
if (is_nte) {
auto card = s->card_for_set_card_ref(attacker_card_ref);
auto set_card = s->card_for_set_card_ref(attacker_card_ref);
bool card_found = false;
if (!card) {
if (!set_card) {
card_found = false;
} else {
for (size_t z = 0; z < card->action_chain.chain.target_card_ref_count; z++) {
if (card->action_chain.chain.target_card_refs[z] == card->get_card_ref()) {
for (size_t z = 0; z < set_card->action_chain.chain.target_card_ref_count; z++) {
if (set_card->action_chain.chain.target_card_refs[z] == card->get_card_ref()) {
card_found = true;
break;
}
@@ -2484,7 +2481,7 @@ bool CardSpecial::execute_effect(
} else if (unknown_p7 & 0x20) {
card->action_metadata.attack_bonus = clamp<int16_t>(
positive_expr_value + card->action_metadata.attack_bonus, -99, 99);
card->action_metadata.attack_bonus + positive_expr_value, -99, 99);
}
return true;
@@ -2840,7 +2837,7 @@ vector<shared_ptr<const Card>> CardSpecial::get_targeted_cards_for_condition(
AttackMedium attack_medium = card2
? card2->action_chain.chain.attack_medium
: AttackMedium::UNKNOWN;
log.debug("attack_medium=%s", name_for_attack_medium(attack_medium));
log.debug("attack_medium=%s", name_for_enum(attack_medium));
auto add_card_refs = [&](const vector<uint16_t>& result_card_refs) -> void {
for (uint16_t result_card_ref : result_card_refs) {
@@ -2899,9 +2896,9 @@ vector<shared_ptr<const Card>> CardSpecial::get_targeted_cards_for_condition(
break;
case 0x04: // p04
size_t z;
for (z = 0; (z < 9) && (as.action_card_refs[z] != 0xFFFF) && (as.action_card_refs[z] != card_ref); z++) {
for (z = 0; (z < 8) && (as.action_card_refs[z] != 0xFFFF) && (as.action_card_refs[z] != card_ref); z++) {
}
for (; (z < 9) && (as.action_card_refs[z] != 0xFFFF); z++) {
for (; (z < 8) && (as.action_card_refs[z] != 0xFFFF); z++) {
auto result_card = s->card_for_set_card_ref(as.action_card_refs[z]);
if (result_card) {
ret.emplace_back(result_card);
@@ -3153,20 +3150,35 @@ vector<shared_ptr<const Card>> CardSpecial::get_targeted_cards_for_condition(
}
break;
case 0x24: { // p36
auto log36 = log.sub("(p36) ");
// On NTE, this includes SCs and items; on other versions, it's SCs only
static const auto should_include = +[](shared_ptr<const CardIndex::CardEntry> ce, bool is_nte) -> bool {
return (ce && (ce->def.is_sc() || (is_nte ? (ce->def.type == CardType::ITEM) : false)));
};
bool is_nte = s->options.is_nte();
if (as.original_attacker_card_ref == 0xFFFF) {
log36.debug("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());
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());
ret.emplace_back(result_card);
} else {
log36.debug("skipping %s", debug_ref_str.c_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());
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());
} else {
log36.debug("original_attacker_card_ref present and card2 missing");
}
break;
}
@@ -3510,8 +3522,7 @@ void CardSpecial::on_card_set(shared_ptr<PlayerState> ps, uint16_t card_ref) {
this->evaluate_and_apply_effects(0x01, card_ref, as, sc_card_ref);
}
const CardDefinition::Effect* CardSpecial::original_definition_for_condition(
const Condition& cond) const {
const CardDefinition::Effect* CardSpecial::original_definition_for_condition(const Condition& cond) const {
auto ce = this->server()->definition_for_card_ref(cond.card_ref);
if (!ce) {
return nullptr;
@@ -3525,8 +3536,7 @@ bool CardSpecial::card_ref_has_ability_trap(const Condition& cond) const {
if (!card) {
return false;
} else {
return this->card_has_condition_with_ref(
card, ConditionType::ABILITY_TRAP, 0xFFFF, 0xFFFF);
return this->card_has_condition_with_ref(card, ConditionType::ABILITY_TRAP, 0xFFFF, 0xFFFF);
}
}
@@ -3566,8 +3576,7 @@ void CardSpecial::send_6xB4x06_for_card_destroyed(
this->server()->send(cmd);
}
uint16_t CardSpecial::send_6xB4x06_if_card_ref_invalid(
uint16_t card_ref, int16_t value) const {
uint16_t CardSpecial::send_6xB4x06_if_card_ref_invalid(uint16_t card_ref, int16_t value) const {
auto s = this->server();
if (!s->options.is_nte() && !s->card_ref_is_empty_or_has_valid_card_id(card_ref)) {
if (value != 0) {
@@ -3938,7 +3947,7 @@ void CardSpecial::evaluate_and_apply_effects(
bool is_nte = s->options.is_nte();
{
string as_str = as.str();
string as_str = as.str(s);
log.debug("when=%02hhX, set_card_ref=@%04hX, as=%s, sc_card_ref=@%04hX, apply_defense_condition_to_all_cards=%s, apply_defense_condition_to_card_ref=@%04hX",
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);
}
@@ -4114,7 +4123,7 @@ void CardSpecial::evaluate_and_apply_effects(
// bug probably does nothing in any reasonable scenario, since the
// target card refs array immediately precedes the conditions array,
// and the target card refs array is excessively long, so OR'ing a
// value that is amost certainly already 0xFFFF with 1 would do
// value that is almost certainly already 0xFFFF with 1 would do
// nothing. In our implementation, however, we bounds-check
// everything, so we've moved this check inside the relevant if block.
if (dice_roll.value_used_in_expr) {
@@ -4139,8 +4148,7 @@ void CardSpecial::evaluate_and_apply_effects(
if (any_expr_used_dice_roll) {
dice_cmd.effect.flags = 0x08;
dice_cmd.effect.attacker_card_ref = this->send_6xB4x06_if_card_ref_invalid(
as_attacker_card_ref, 0x15);
dice_cmd.effect.attacker_card_ref = this->send_6xB4x06_if_card_ref_invalid(as_attacker_card_ref, 0x15);
dice_cmd.effect.dice_roll_value = dice_roll.value;
s->send(dice_cmd);
}
@@ -4664,13 +4672,13 @@ vector<shared_ptr<const Card>> CardSpecial::filter_cards_by_range(
}
void CardSpecial::unknown_8024AAB8(const ActionState& as) {
auto log = this->server()->log_stack("unknown_8024AAB8: ");
string as_str = as.str();
auto s = this->server();
auto log = s->log_stack("unknown_8024AAB8: ");
string as_str = as.str(s);
log.debug("as=%s", as_str.c_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);
uint16_t card_ref = this->send_6xB4x06_if_card_ref_invalid(as.action_card_refs[z], 0x1E);
if (card_ref == 0xFFFF) {
break;
}
@@ -4697,8 +4705,7 @@ void CardSpecial::unknown_8024AAB8(const ActionState& as) {
card_ref2 = this->send_6xB4x06_if_card_ref_invalid(as.attacker_card_ref, 0x26);
this->evaluate_and_apply_effects(0x34, card_ref2, as, card_ref1);
for (size_t z = 0; (z < 4 * 9) && (as.target_card_refs[z] != 0xFFFF); z++) {
uint16_t card_ref = this->send_6xB4x06_if_card_ref_invalid(
as.action_card_refs[z], 0x27);
uint16_t card_ref = this->send_6xB4x06_if_card_ref_invalid(as.action_card_refs[z], 0x27);
if (card_ref == 0xFFFF) {
break;
}
@@ -4720,9 +4727,10 @@ void CardSpecial::move_phase_before_for_card(shared_ptr<Card> card) {
void CardSpecial::dice_phase_before_for_card(shared_ptr<Card> card) {
auto s = this->server();
bool is_nte = s->options.is_nte();
auto ps = card->player_state();
if (s->options.is_nte() && (!ps || !ps->is_team_turn())) {
if (is_nte && (!ps || !ps->is_team_turn())) {
return;
}
@@ -4739,7 +4747,7 @@ void CardSpecial::dice_phase_before_for_card(shared_ptr<Card> card) {
}
}
if (!s->options.is_nte()) {
if (!is_nte) {
this->apply_defense_conditions(as, 0x46, card, 0x04);
this->evaluate_and_apply_effects(0x46, card->get_card_ref(), as, sc_card_ref);
}
@@ -4940,8 +4948,7 @@ void CardSpecial::check_for_attack_interference(shared_ptr<Card> unknown_p2) {
G_ApplyConditionEffect_Ep3_6xB4x06 cmd;
cmd.effect.flags = 0x04;
cmd.effect.attacker_card_ref = this->send_6xB4x06_if_card_ref_invalid(
unknown_p2->get_card_ref(), 0x11);
cmd.effect.attacker_card_ref = this->send_6xB4x06_if_card_ref_invalid(unknown_p2->get_card_ref(), 0x11);
cmd.effect.target_card_ref = unknown_p2->get_card_ref();
cmd.effect.value = 0;
cmd.effect.operation = 0x7D;
File diff suppressed because it is too large Load Diff
+64 -41
View File
@@ -37,6 +37,7 @@ enum BehaviorFlag : uint32_t {
DISABLE_INTERFERENCE = 0x00000100,
ALLOW_NON_COM_INTERFERENCE = 0x00000200,
IS_TRIAL_EDITION = 0x00000400,
LOG_COMMANDS_IF_LOBBY_MISSING = 0x00000800,
};
enum class StatSwapType : uint8_t {
@@ -59,8 +60,6 @@ enum class AttackMedium : uint8_t {
INVALID_FF = 0xFF,
};
const char* name_for_attack_medium(AttackMedium medium);
enum class CriterionCode : uint8_t {
NONE = 0x00,
HU_CLASS_SC = 0x01,
@@ -99,8 +98,6 @@ enum class CriterionCode : uint8_t {
NON_PHYSICAL_NON_TECH_NON_UNKNOWN_ATTACK_MEDIUM_NON_SC = 0x22,
};
const char* name_for_criterion_code(CriterionCode code);
enum class CardRank : uint8_t {
N1 = 0x01,
R1 = 0x02,
@@ -138,8 +135,6 @@ enum class CardType : uint8_t {
END_CARD_LIST = 0xFF,
};
const char* name_for_card_type(CardType type);
enum class CardClass : uint16_t {
HU_SC = 0x0000,
RA_SC = 0x0001,
@@ -163,7 +158,6 @@ enum class CardClass : uint16_t {
ASSIST = 0x0028,
};
const char* name_for_card_class(CardClass cc);
bool card_class_is_tech_like(CardClass cc, bool is_nte);
enum class TargetMode : uint8_t {
@@ -310,8 +304,6 @@ enum class ConditionType : uint8_t {
ANY_FF = 0xFF, // Used as a wildcard in some search functions
};
const char* name_for_condition_type(ConditionType cond_type);
enum class AssistEffect : uint16_t {
NONE = 0x0000,
DICE_HALF = 0x0001,
@@ -408,8 +400,6 @@ enum class ActionSubphase : uint8_t {
INVALID_FF = 0xFF,
};
const char* name_for_action_subphase(ActionSubphase subphase);
enum class SetupPhase : uint8_t {
REGISTRATION = 0,
STARTER_ROLLS = 1,
@@ -439,7 +429,6 @@ enum class Direction : uint8_t {
Direction turn_left(Direction d);
Direction turn_right(Direction d);
Direction turn_around(Direction d);
const char* name_for_direction(Direction d);
struct Location {
/* 00 */ uint8_t x;
@@ -480,6 +469,7 @@ struct CardDefinition {
void decode_code();
std::string str() const;
JSON json() const;
} __attribute__((packed));
struct Effect {
@@ -516,10 +506,11 @@ struct CardDefinition {
bool is_empty() const;
static std::string str_for_arg(const std::string& arg);
std::string str(const char* separator = ", ", const TextSet* text_archive = nullptr) const;
JSON json() const;
} __attribute__((packed));
/* 0000 */ be_uint32_t card_id;
/* 0004 */ parray<uint8_t, 0x40> jp_name;
/* 0004 */ pstring<TextEncoding::SJIS, 0x40> jp_name;
// The list of card definitions ends with a "sentinel" definition that isn't a
// real card, but instead has a negative number in the type field here.
@@ -765,9 +756,9 @@ struct CardDefinition {
// enormous comment? That's what this array stores.
/* 009C */ parray<be_uint16_t, 2> drop_rates;
/* 00A0 */ pstring<TextEncoding::SJIS, 0x14> en_name;
/* 00A0 */ pstring<TextEncoding::ISO8859, 0x14> en_name;
/* 00B4 */ pstring<TextEncoding::SJIS, 0x0B> jp_short_name;
/* 00BF */ pstring<TextEncoding::SJIS, 0x08> en_short_name;
/* 00BF */ pstring<TextEncoding::ISO8859, 0x08> en_short_name;
// These effects modify the card's behavior in various situations. Only
// effects for which effect_num is not zero are used.
/* 00C7 */ parray<Effect, 3> effects;
@@ -782,6 +773,7 @@ struct CardDefinition {
void decode_range();
std::string str(bool single_line = true, const TextSet* text_archive = nullptr) const;
JSON json() const;
} __attribute__((packed)); // 0x128 bytes in total
struct CardDefinitionsFooter {
@@ -801,7 +793,7 @@ struct CardDefinitionsFooter {
} __attribute__((packed));
struct DeckDefinition {
/* 00 */ pstring<TextEncoding::SJIS, 0x10> name;
/* 00 */ pstring<TextEncoding::MARKED, 0x10> name;
/* 10 */ be_uint32_t client_id; // 0-3
// List of card IDs. The card count is the number of nonzero entries here
// before a zero entry (or 50 if no entries are nonzero). The first card ID is
@@ -824,7 +816,7 @@ struct PlayerConfig {
// The game splits this internally into two structures. The first column of
// offsets is relative to the start of the first structure; the second column
// is relative to the start of the second structure.
/* 0000:---- */ pstring<TextEncoding::SJIS, 12> rank_text; // From B7 command
/* 0000:---- */ pstring<TextEncoding::MARKED, 12> rank_text; // From B7 command
/* 000C:---- */ parray<uint8_t, 0x1C> unknown_a1;
/* 0028:---- */ parray<be_uint16_t, 20> tech_menu_shortcut_entries;
/* 0050:---- */ parray<be_uint32_t, 10> choice_search_config;
@@ -886,7 +878,7 @@ struct PlayerConfig {
/* 2124:1FD0 */ be_uint32_t online_clv_exp; // CLvOn = this / 100
struct PlayerReference {
/* 00 */ be_uint32_t guild_card_number;
/* 04 */ pstring<TextEncoding::SJIS, 0x18> player_name;
/* 04 */ pstring<TextEncoding::MARKED, 0x18> name;
} __attribute__((packed));
// This array is updated when a battle is started (via a 6xB4x05 command). The
// client adds the opposing players' info to ths first two entries here if the
@@ -940,8 +932,8 @@ struct Rules {
/* 00 */ uint8_t overall_time_limit = 0;
/* 01 */ uint8_t phase_time_limit = 0; // In seconds; 0 = unlimited
/* 02 */ AllowedCards allowed_cards = AllowedCards::ALL;
/* 03 */ uint8_t min_dice = 1; // 0 = default (1)
/* 04 */ uint8_t max_dice = 6; // 0 = default (6)
/* 03 */ uint8_t min_dice_value = 1; // 0 = default (1)
/* 04 */ uint8_t max_dice_value = 6; // 0 = default (6)
/* 05 */ uint8_t disable_deck_shuffle = 0; // 0 = shuffle on, 1 = off
/* 06 */ uint8_t disable_deck_loop = 0; // 0 = loop on, 1 = off
/* 07 */ uint8_t char_hp = 15;
@@ -952,8 +944,11 @@ struct Rules {
/* 0C */ uint8_t disable_dice_boost = 0; // 0 = dice boost on, 1 = off
// NOTE: The following fields are unused in PSO's implementation, but newserv
// uses them to implement extended rules.
/* 0D */ uint8_t def_dice_range = 0; // High 4 bits = min, low 4 = max
/* 0E */ parray<uint8_t, 6> unused;
/* 0D */ uint8_t def_dice_value_range = 0; // High 4 bits = min, low 4 = max
// These fields specify override dice ranges for the 1-player team in 2v1
/* 0E */ uint8_t atk_dice_value_range_2v1 = 0; // High 4 bits = min, low 4 = max
/* 0F */ uint8_t def_dice_value_range_2v1 = 0; // High 4 bits = min, low 4 = max
/* 10 */ parray<uint8_t, 4> unused;
/* 14 */
// Annoyingly, this structure is a different size in Episode 3 Trial Edition.
@@ -973,8 +968,8 @@ struct Rules {
bool check_invalid_fields() const;
bool check_and_reset_invalid_fields();
uint8_t min_def_dice() const;
uint8_t max_def_dice() const;
std::pair<uint8_t, uint8_t> atk_dice_range(bool is_1p_2v1) const;
std::pair<uint8_t, uint8_t> def_dice_range(bool is_1p_2v1) const;
std::string str() const;
} __attribute__((packed));
@@ -1159,6 +1154,7 @@ struct MapDefinition { // .mnmd format; also the format of (decompressed) quests
/* 48 */
std::string str() const;
JSON json() const;
} __attribute__((packed));
// This array specifies the camera zone maps. A camera zone map is a subset of
@@ -1202,10 +1198,10 @@ struct MapDefinition { // .mnmd format; also the format of (decompressed) quests
/* 1D68 */ parray<uint8_t, 0x74> unknown_a5;
/* 1DDC */ Rules default_rules;
/* 1DF0 */ pstring<TextEncoding::SJIS, 0x14> name;
/* 1E04 */ pstring<TextEncoding::SJIS, 0x14> location_name;
/* 1E18 */ pstring<TextEncoding::SJIS, 0x3C> quest_name; // == location_name if not a quest
/* 1E54 */ pstring<TextEncoding::SJIS, 0x190> description;
/* 1DF0 */ pstring<TextEncoding::MARKED, 0x14> name;
/* 1E04 */ pstring<TextEncoding::MARKED, 0x14> location_name;
/* 1E18 */ pstring<TextEncoding::MARKED, 0x3C> quest_name; // == location_name if not a quest
/* 1E54 */ pstring<TextEncoding::MARKED, 0x190> description;
// These fields describe where the map cursor on the preview screen should
// scroll to
@@ -1213,9 +1209,10 @@ struct MapDefinition { // .mnmd format; also the format of (decompressed) quests
/* 1FE6 */ be_uint16_t map_y;
struct NPCDeck {
/* 00 */ pstring<TextEncoding::SJIS, 0x18> name;
/* 00 */ pstring<TextEncoding::MARKED, 0x18> deck_name;
/* 18 */ parray<be_uint16_t, 0x20> card_ids; // Last one appears to always be FFFF
/* 58 */
JSON json(uint8_t language) const;
} __attribute__((packed));
/* 1FE8 */ parray<NPCDeck, 3> npc_decks; // Unused if name[0] == 0
@@ -1227,10 +1224,11 @@ struct MapDefinition { // .mnmd format; also the format of (decompressed) quests
/* 0000 */ parray<be_uint16_t, 2> unknown_a1;
/* 0004 */ uint8_t is_arkz;
/* 0005 */ parray<uint8_t, 3> unknown_a2;
/* 0008 */ pstring<TextEncoding::SJIS, 0x10> name;
/* 0008 */ pstring<TextEncoding::MARKED, 0x10> ai_name;
// TODO: Figure out exactly how these are used and document here.
/* 0018 */ parray<be_uint16_t, 0x7E> params;
/* 0114 */
JSON json(uint8_t language) const;
} __attribute__((packed));
/* 20F0 */ parray<AIParams, 3> npc_ai_params; // Unused if name[0] == 0
@@ -1261,9 +1259,9 @@ struct MapDefinition { // .mnmd format; also the format of (decompressed) quests
// appears after the battle if it's not blank. dispatch_message appears right
// before the player chooses a deck if it's not blank; usually it says
// something like "You can only dispatch <character>".
/* 2440 */ pstring<TextEncoding::SJIS, 0x190> before_message;
/* 25D0 */ pstring<TextEncoding::SJIS, 0x190> after_message;
/* 2760 */ pstring<TextEncoding::SJIS, 0x190> dispatch_message;
/* 2440 */ pstring<TextEncoding::MARKED, 0x190> before_message;
/* 25D0 */ pstring<TextEncoding::MARKED, 0x190> after_message;
/* 2760 */ pstring<TextEncoding::MARKED, 0x190> dispatch_message;
struct DialogueSet {
// Dialogue sets specify lines that COMs can say at certain points during
@@ -1281,8 +1279,9 @@ struct MapDefinition { // .mnmd format; also the format of (decompressed) quests
/* 0002 */ be_uint16_t percent_chance; // 0-100, or FFFF if unused
// If the dialogue set activates, the game randomly chooses one of these
// strings, excluding any that are empty or begin with the character '^'.
/* 0004 */ parray<pstring<TextEncoding::SJIS, 0x40>, 4> strings;
/* 0004 */ parray<pstring<TextEncoding::MARKED, 0x40>, 4> strings;
/* 0104 */
JSON json(uint8_t language) const;
} __attribute__((packed));
// There are up to 0x10 of these per valid NPC, but only the first 13 of them
@@ -1365,6 +1364,7 @@ struct MapDefinition { // .mnmd format; also the format of (decompressed) quests
bool operator==(const EntryState& other) const = default;
bool operator!=(const EntryState& other) const = default;
JSON json() const;
} __attribute__((packed));
/* 5A10 */ parray<EntryState, 4> entry_states;
/* 5A18 */
@@ -1380,6 +1380,7 @@ struct MapDefinition { // .mnmd format; also the format of (decompressed) quests
void assert_semantically_equivalent(const MapDefinition& other) const;
std::string str(const CardIndex* card_index, uint8_t language) const;
JSON json(uint8_t language) const;
} __attribute__((packed));
struct MapDefinitionTrial {
@@ -1400,19 +1401,19 @@ struct MapDefinitionTrial {
/* 1C68 */ parray<parray<uint8_t, 0x10>, 0x10> modification_tiles;
/* 1D68 */ parray<uint8_t, 0x74> unknown_a5;
/* 1DD4 */ RulesTrial default_rules;
/* 1DE8 */ pstring<TextEncoding::SJIS, 0x14> name;
/* 1DFC */ pstring<TextEncoding::SJIS, 0x14> location_name;
/* 1E10 */ pstring<TextEncoding::SJIS, 0x3C> quest_name;
/* 1E4C */ pstring<TextEncoding::SJIS, 0x190> description;
/* 1DE8 */ pstring<TextEncoding::MARKED, 0x14> name;
/* 1DFC */ pstring<TextEncoding::MARKED, 0x14> location_name;
/* 1E10 */ pstring<TextEncoding::MARKED, 0x3C> quest_name;
/* 1E4C */ pstring<TextEncoding::MARKED, 0x190> description;
/* 1FDC */ be_uint16_t map_x;
/* 1FDE */ be_uint16_t map_y;
/* 1FE0 */ parray<MapDefinition::NPCDeck, 3> npc_decks;
/* 20E8 */ parray<MapDefinition::AIParams, 3> npc_ai_params;
/* 2424 */ parray<uint8_t, 8> unknown_a7;
/* 242C */ parray<be_int32_t, 3> npc_ai_params_entry_index;
/* 2438 */ pstring<TextEncoding::SJIS, 0x190> before_message;
/* 25C8 */ pstring<TextEncoding::SJIS, 0x190> after_message;
/* 2758 */ pstring<TextEncoding::SJIS, 0x190> dispatch_message;
/* 2438 */ pstring<TextEncoding::MARKED, 0x190> before_message;
/* 25C8 */ pstring<TextEncoding::MARKED, 0x190> after_message;
/* 2758 */ pstring<TextEncoding::MARKED, 0x190> dispatch_message;
/* 28E8 */ parray<parray<MapDefinition::DialogueSet, 8>, 3> dialogue_sets;
/* 4148 */ parray<be_uint16_t, 0x10> reward_card_ids;
/* 4168 */ be_int32_t win_level_override;
@@ -1462,6 +1463,7 @@ public:
std::shared_ptr<const CardEntry> definition_for_name_normalized(const std::string& name) const;
std::set<uint32_t> all_ids() const;
uint64_t definitions_mtime() const;
JSON definitions_json() const;
private:
static std::string normalize_card_name(const std::string& name);
@@ -1553,3 +1555,24 @@ template <>
Episode3::AllowedCards enum_for_name<Episode3::AllowedCards>(const char* name);
template <>
const char* name_for_enum<Episode3::AllowedCards>(Episode3::AllowedCards allowed_cards);
template <>
const char* name_for_enum<Episode3::BattlePhase>(Episode3::BattlePhase phase);
template <>
const char* name_for_enum<Episode3::SetupPhase>(Episode3::SetupPhase phase);
template <>
const char* name_for_enum<Episode3::RegistrationPhase>(Episode3::RegistrationPhase phase);
template <>
const char* name_for_enum<Episode3::ActionSubphase>(Episode3::ActionSubphase phase);
template <>
const char* name_for_enum<Episode3::AttackMedium>(Episode3::AttackMedium medium);
template <>
const char* name_for_enum<Episode3::CriterionCode>(Episode3::CriterionCode code);
template <>
const char* name_for_enum<Episode3::CardType>(Episode3::CardType type);
template <>
const char* name_for_enum<Episode3::CardClass>(Episode3::CardClass cc);
template <>
const char* name_for_enum<Episode3::ConditionType>(Episode3::ConditionType cond_type);
template <>
const char* name_for_enum<Episode3::Direction>(Episode3::Direction d);
+4 -4
View File
@@ -205,8 +205,8 @@ void DeckState::do_mulligan(bool is_nte) {
size_t max = this->num_drawable_cards() - 5;
uint8_t base_index = this->draw_index + 5;
for (size_t z = 0; z < this->card_refs.size(); z++) {
uint8_t index1 = this->random_crypt->next() % max;
uint8_t index2 = this->random_crypt->next() % max;
uint8_t index1 = random_from_optional_crypt(this->opt_rand_crypt) % max;
uint8_t index2 = random_from_optional_crypt(this->opt_rand_crypt) % max;
uint16_t temp_ref = this->card_refs[base_index + index1];
this->card_refs[base_index + index1] = this->card_refs[base_index + index2];
this->card_refs[base_index + index2] = temp_ref;
@@ -265,8 +265,8 @@ void DeckState::shuffle() {
// instead swap each item with another random item (possibly itself) that
// doesn't appear earlier than it in the array, but this is not what Sega
// did.
uint8_t index1 = this->draw_index + this->random_crypt->next() % max;
uint8_t index2 = this->draw_index + this->random_crypt->next() % max;
uint8_t index1 = this->draw_index + random_from_optional_crypt(this->opt_rand_crypt) % max;
uint8_t index2 = this->draw_index + random_from_optional_crypt(this->opt_rand_crypt) % max;
uint16_t temp_ref = this->card_refs[index1];
this->card_refs[index1] = this->card_refs[index2];
this->card_refs[index2] = temp_ref;
+5 -5
View File
@@ -11,7 +11,7 @@
namespace Episode3 {
struct NameEntry {
/* 00 */ parray<char, 0x10> name;
/* 00 */ pstring<TextEncoding::MARKED, 0x10> name;
/* 10 */ uint8_t client_id;
/* 11 */ uint8_t present;
/* 12 */ uint8_t is_cpu_player;
@@ -23,7 +23,7 @@ struct NameEntry {
} __attribute__((packed));
struct DeckEntry {
/* 00 */ pstring<TextEncoding::SJIS, 0x10> name;
/* 00 */ pstring<TextEncoding::MARKED, 0x10> name;
/* 10 */ le_uint32_t team_id;
/* 14 */ parray<le_uint16_t, 31> card_ids;
// If the following flag is not set to 3, then the God Whim assist effect can
@@ -57,13 +57,13 @@ public:
DeckState(
uint8_t client_id,
const parray<CardIDT, 0x1F>& card_ids,
std::shared_ptr<PSOLFGEncryption> random_crypt)
std::shared_ptr<PSOLFGEncryption> opt_rand_crypt)
: client_id(client_id),
draw_index(1),
card_ref_base(this->client_id << 8),
shuffle_enabled(true),
loop_enabled(true),
random_crypt(random_crypt) {
opt_rand_crypt(opt_rand_crypt) {
for (size_t z = 0; z < card_ids.size(); z++) {
auto& e = this->entries[z];
e.card_id = card_ids[z];
@@ -112,7 +112,7 @@ private:
parray<CardEntry, 31> entries;
parray<uint16_t, 31> card_refs;
std::shared_ptr<PSOLFGEncryption> random_crypt;
std::shared_ptr<PSOLFGEncryption> opt_rand_crypt;
};
} // namespace Episode3
+39 -35
View File
@@ -41,6 +41,7 @@ PlayerState::PlayerState(uint8_t client_id, shared_ptr<Server> server)
void PlayerState::init() {
auto s = this->server();
auto log = s->log_stack("PlayerState::init: ");
if (s->player_states.at(this->client_id).get() != this) {
// Note: The original code handles this, but we don't. This appears not to
@@ -48,7 +49,7 @@ void PlayerState::init() {
throw logic_error("replacing a player state object is not permitted");
}
this->deck_state = make_shared<DeckState>(this->client_id, s->deck_entries[client_id]->card_ids, s->options.random_crypt);
this->deck_state = make_shared<DeckState>(this->client_id, s->deck_entries[client_id]->card_ids, s->options.opt_rand_crypt);
if (s->map_and_rules->rules.disable_deck_shuffle) {
this->deck_state->disable_shuffle();
}
@@ -832,7 +833,7 @@ vector<uint16_t> PlayerState::get_all_cards_within_range(
uint8_t target_team_id) const {
auto s = this->server();
auto log = this->server()->log_stack("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);
@@ -1754,22 +1755,33 @@ int16_t PlayerState::get_assist_turns_remaining() {
bool PlayerState::set_action_cards_for_action_state(const ActionState& pa) {
auto s = this->server();
auto log = s->log_stack("set_action_cards_for_action_state: ");
bool is_nte = s->options.is_nte();
auto attacker_card = s->card_for_set_card_ref(pa.attacker_card_ref);
if (attacker_card) {
log.debug("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");
} else if (action_type == ActionType::ATTACK) {
log.debug("action type is ATTACK");
} else {
log.debug("action type is UNKNOWN");
}
if (!is_nte) {
this->subtract_or_check_atk_or_def_points_for_action(pa, 1);
log.debug("(non-nte) subtracting action points");
this->subtract_or_check_atk_or_def_points_for_action(pa, true);
}
if (action_type == ActionType::ATTACK) {
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", name_for_enum(card->loc.direction));
G_Unknown_Ep3_6xB4x4A cmd;
cmd.card_refs.clear(0xFFFF);
@@ -1778,6 +1790,10 @@ bool PlayerState::set_action_cards_for_action_state(const ActionState& pa) {
cmd.entry_count = 0;
size_t z = 0;
do {
if (log.should_log(LogLevel::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());
}
card->unknown_80237A90(pa, pa.action_card_refs[z]);
card->unknown_802379BC(pa.action_card_refs[z]);
if (!is_nte) {
@@ -1811,6 +1827,10 @@ 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(LogLevel::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());
}
target_card->unknown_802379DC(pa);
if (!is_nte) {
if (this->client_id == target_card->get_client_id()) {
@@ -1833,9 +1853,14 @@ bool PlayerState::set_action_cards_for_action_state(const ActionState& pa) {
}
}
if (is_nte) {
log.debug("(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(LogLevel::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());
}
this->discard_ref_from_hand(pa.action_card_refs[z]);
}
this->update_hand_and_equip_state_and_send_6xB4x02_if_needed();
@@ -1960,44 +1985,23 @@ void PlayerState::roll_main_dice_or_apply_after_effects() {
// the non-NTE logic and assign the dice ranges at battle start time to yield
// the NTE behavior. (See RulesTrial in DataIndexes.cc for how this is done.)
uint8_t min_atk_dice = rules.min_dice;
uint8_t max_atk_dice = rules.max_dice;
if (min_atk_dice == 0) {
min_atk_dice = 1;
}
if (max_atk_dice == 0) {
max_atk_dice = 6;
}
if (max_atk_dice < min_atk_dice) {
uint8_t t = max_atk_dice;
max_atk_dice = min_atk_dice;
min_atk_dice = t;
}
uint8_t atk_dice_range_width = (max_atk_dice - min_atk_dice) + 1;
bool is_1p_2v1 = (s->team_client_count.at(this->get_team_id()) < s->team_client_count[this->get_team_id() ^ 1]);
auto atk_range = rules.atk_dice_range(is_1p_2v1);
auto def_range = rules.def_dice_range(is_1p_2v1);
uint8_t atk_dice_range_width = (atk_range.second - atk_range.first) + 1;
if (atk_dice_range_width < 2) {
this->dice_results[0] = min_atk_dice;
this->dice_results[0] = atk_range.first;
} else {
this->dice_results[0] = min_atk_dice + s->get_random(atk_dice_range_width);
this->dice_results[0] = atk_range.first + s->get_random(atk_dice_range_width);
}
uint8_t min_def_dice = rules.min_def_dice() ? rules.min_def_dice() : rules.min_dice;
uint8_t max_def_dice = rules.max_def_dice() ? rules.max_def_dice() : rules.max_dice;
if (min_def_dice == 0) {
min_def_dice = 1;
}
if (max_def_dice == 0) {
max_def_dice = 6;
}
if (max_def_dice < min_def_dice) {
uint8_t t = max_def_dice;
max_def_dice = min_def_dice;
min_def_dice = t;
}
uint8_t def_dice_range_width = (max_def_dice - min_def_dice) + 1;
uint8_t def_dice_range_width = (def_range.second - def_range.first) + 1;
if (def_dice_range_width < 2) {
this->dice_results[1] = min_def_dice;
this->dice_results[1] = def_range.first;
} else {
this->dice_results[1] = min_def_dice + s->get_random(def_dice_range_width);
this->dice_results[1] = def_range.first + s->get_random(def_dice_range_width);
}
bool should_exchange = false;
+1 -1
View File
@@ -148,7 +148,7 @@ private:
public:
std::shared_ptr<Card> sc_card;
std::shared_ptr<Card> set_cards[8];
bcarray<std::shared_ptr<Card>, 8> set_cards;
uint8_t client_id;
uint16_t num_mulligans_allowed;
CardType sc_card_type;
+73 -76
View File
@@ -6,22 +6,6 @@ using namespace std;
namespace Episode3 {
template <size_t Count>
std::string string_for_refs(const parray<le_uint16_t, Count>& card_refs) {
string ret = "[";
for (size_t z = 0; z < Count; z++) {
if (card_refs[z] != 0xFFFF) {
ret += string_printf("%zu:@$%04X ", z, card_refs[z].load());
}
}
if (!ret.empty()) {
ret.back() = ']'; // Replace the ' ' from the last added item
} else {
ret.push_back(']');
}
return ret;
}
Condition::Condition() {
this->clear();
}
@@ -77,20 +61,22 @@ void Condition::clear_FF() {
this->unknown_a8 = 0xFF;
}
std::string Condition::str() const {
std::string Condition::str(shared_ptr<const Server> s) const {
auto card_ref_str = s->debug_str_for_card_ref(this->card_ref);
auto giver_ref_str = s->debug_str_for_card_ref(this->condition_giver_card_ref);
return string_printf(
"Condition[type=%s, turns=%hhu, a_arg=%hhd, dice=%hhu, flags=%02hhX, "
"def_eff_index=%hhu, ref=@%04hX, value=%hd, giver_ref=@%04hX "
"def_eff_index=%hhu, ref=%s, value=%hd, giver_ref=%s "
"percent=%hhu value8=%hd order=%hu a8=%hu]",
name_for_condition_type(this->type),
name_for_enum(this->type),
this->remaining_turns,
this->a_arg_value,
this->dice_roll_value,
this->flags,
this->card_definition_effect_index,
this->card_ref.load(),
card_ref_str.c_str(),
this->value.load(),
this->condition_giver_card_ref.load(),
giver_ref_str.c_str(),
this->random_percent,
this->value8,
this->order,
@@ -114,13 +100,15 @@ void EffectResult::clear() {
this->dice_roll_value = 0;
}
std::string EffectResult::str() const {
std::string EffectResult::str(shared_ptr<const Server> s) const {
string attacker_ref_str = s->debug_str_for_card_ref(this->attacker_card_ref);
string target_ref_str = s->debug_str_for_card_ref(this->target_card_ref);
return string_printf(
"EffectResult[att_ref=@%04hX, target_ref=@%04hX, value=%hhd, "
"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]",
this->attacker_card_ref.load(),
this->target_card_ref.load(),
attacker_ref_str.c_str(),
target_ref_str.c_str(),
this->value,
this->current_hp,
this->ap,
@@ -148,12 +136,13 @@ bool CardShortStatus::operator!=(const CardShortStatus& other) const {
return !this->operator==(other);
}
std::string CardShortStatus::str() const {
std::string CardShortStatus::str(shared_ptr<const Server> s) const {
string loc_s = this->loc.str();
string ref_str = s->debug_str_for_card_ref(this->card_ref);
return string_printf(
"CardShortStatus[ref=@%04hX, cur_hp=%hd, flags=%08" PRIX32 ", loc=%s, "
"CardShortStatus[ref=%s, cur_hp=%hd, flags=%08" PRIX32 ", loc=%s, "
"u1=%04hX, max_hp=%hhd, u2=%hhu]",
this->card_ref.load(),
ref_str.c_str(),
this->current_hp.load(),
this->card_flags.load(),
loc_s.c_str(),
@@ -195,23 +184,27 @@ void ActionState::clear() {
this->original_attacker_card_ref = 0xFFFF;
this->target_card_refs.clear(0xFFFF);
this->action_card_refs.clear(0xFFFF);
this->unused2 = 0xFFFF;
}
std::string ActionState::str() const {
string target_refs_s = string_for_refs(this->target_card_refs);
string action_refs_s = string_for_refs(this->action_card_refs);
std::string ActionState::str(shared_ptr<const Server> s) const {
string attacker_ref_s = s->debug_str_for_card_ref(this->attacker_card_ref);
string defense_ref_s = s->debug_str_for_card_ref(this->defense_card_ref);
string original_attacker_ref_s = s->debug_str_for_card_ref(this->original_attacker_card_ref);
string target_refs_s = s->debug_str_for_card_refs(this->target_card_refs);
string action_refs_s = s->debug_str_for_card_refs(this->action_card_refs);
return string_printf(
"ActionState[client=%hu, u=%hhu, facing=%s, attacker_ref=@%04hX, "
"def_ref=@%04hX, target_refs=%s, action_refs=%s, "
"orig_attacker_ref=@%04hX]",
"ActionState[client=%hu, u=%hhu, facing=%s, attacker_ref=%s, "
"def_ref=%s, target_refs=%s, action_refs=%s, "
"orig_attacker_ref=%s]",
this->client_id.load(),
this->unused,
name_for_direction(this->facing_direction),
this->attacker_card_ref.load(),
this->defense_card_ref.load(),
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(),
this->original_attacker_card_ref.load());
original_attacker_ref_s.c_str());
}
ActionChain::ActionChain() {
@@ -245,28 +238,29 @@ bool ActionChain::operator!=(const ActionChain& other) const {
return !this->operator==(other);
}
std::string ActionChain::str() const {
string attack_action_card_refs_s = string_for_refs(this->attack_action_card_refs);
string target_card_refs_s = string_for_refs(this->target_card_refs);
std::string ActionChain::str(shared_ptr<const Server> s) const {
string acting_card_ref_s = s->debug_str_for_card_ref(this->acting_card_ref);
string unknown_card_ref_a3_s = s->debug_str_for_card_ref(this->unknown_card_ref_a3);
string attack_action_card_refs_s = s->debug_str_for_card_refs(this->attack_action_card_refs);
string target_card_refs_s = s->debug_str_for_card_refs(this->target_card_refs);
return string_printf(
"ActionChain[eff_ap=%hhd, eff_tp=%hhd, ap_bonus=%hhd, damage=%hhd, "
"acting_ref=@%04hX, unknown_ref_a3=@%04hX, "
"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, "
"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]",
this->effective_ap,
this->effective_tp,
this->ap_effect_bonus,
this->damage,
this->acting_card_ref.load(),
this->unknown_card_ref_a3.load(),
acting_card_ref_s.c_str(),
unknown_card_ref_a3_s.c_str(),
attack_action_card_refs_s.c_str(),
this->attack_action_card_ref_count,
name_for_attack_medium(this->attack_medium),
name_for_enum(this->attack_medium),
this->target_card_ref_count,
name_for_action_subphase(this->action_subphase),
name_for_enum(this->action_subphase),
this->strike_count,
this->damage_multiplier,
this->attack_number,
@@ -338,17 +332,17 @@ bool ActionChainWithConds::operator!=(const ActionChainWithConds& other) const {
return !this->operator==(other);
}
std::string ActionChainWithConds::str() const {
std::string ActionChainWithConds::str(shared_ptr<const Server> s) const {
string ret = "ActionChainWithConds[chain=";
ret += this->chain.str();
ret += this->chain.str(s);
ret += ", conds=[";
for (size_t z = 0; z < this->conditions.size(); z++) {
if (this->conditions[z].type != ConditionType::NONE) {
if (ret.back() != '=') {
if (ret.back() != '[') {
ret += ", ";
}
ret += string_printf("%zu:", z);
ret += this->conditions[z].str();
ret += this->conditions[z].str(s);
}
}
ret += "]]";
@@ -581,19 +575,20 @@ bool ActionMetadata::operator!=(const ActionMetadata& other) const {
return !this->operator==(other);
}
std::string ActionMetadata::str() const {
string target_card_refs_s = string_for_refs(this->target_card_refs);
string defense_card_refs_s = string_for_refs(this->defense_card_refs);
string original_attacker_card_refs_s = string_for_refs(this->original_attacker_card_refs);
std::string ActionMetadata::str(shared_ptr<const Server> s) const {
string card_ref_s = s->debug_str_for_card_ref(this->card_ref);
string target_card_refs_s = s->debug_str_for_card_refs(this->target_card_refs);
string defense_card_refs_s = s->debug_str_for_card_refs(this->defense_card_refs);
string original_attacker_card_refs_s = s->debug_str_for_card_refs(this->original_attacker_card_refs);
return string_printf(
"ActionMetadata[ref=@%04hX, target_ref_count=%hhu, def_ref_count=%hhu, "
"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]",
this->card_ref.load(),
card_ref_s.c_str(),
this->target_card_ref_count,
this->defense_card_ref_count,
name_for_action_subphase(this->action_subphase),
name_for_enum(this->action_subphase),
this->defense_power,
this->defense_bonus,
this->attack_bonus,
@@ -679,20 +674,22 @@ HandAndEquipState::HandAndEquipState() {
this->clear();
}
std::string HandAndEquipState::str() const {
string hand_card_refs_s = string_for_refs(this->hand_card_refs);
string set_card_refs_s = string_for_refs(this->set_card_refs);
string hand_card_refs2_s = string_for_refs(this->hand_card_refs2);
string set_card_refs2_s = string_for_refs(this->set_card_refs2);
std::string HandAndEquipState::str(shared_ptr<const Server> s) const {
string assist_card_ref_s = s->debug_str_for_card_ref(this->assist_card_ref);
string assist_card_ref2_s = s->debug_str_for_card_ref(this->assist_card_ref2);
string assist_card_id_s = s->debug_str_for_card_id(this->assist_card_id);
string sc_card_ref_s = s->debug_str_for_card_ref(this->sc_card_ref);
string hand_card_refs_s = s->debug_str_for_card_refs(this->hand_card_refs);
string set_card_refs_s = s->debug_str_for_card_refs(this->set_card_refs);
string hand_card_refs2_s = s->debug_str_for_card_refs(this->hand_card_refs2);
string set_card_refs2_s = s->debug_str_for_card_refs(this->set_card_refs2);
return 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=@%04hX, set_refs=%s, sc_ref=@%04hX, "
"hand_refs2=%s, set_refs2=%s, assist_ref2=@%04hX, "
"assist_set_num=%hu, assist_card_id=#%04hX, "
"assist_turns=%hhu, assit_dely=%hhu, atk_bonus=%hhu, "
"def_bonus=%hhu, u2=[%hhu, %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]]",
this->dice_results[0],
this->dice_results[1],
this->atk_points,
@@ -703,14 +700,14 @@ std::string HandAndEquipState::str() const {
this->is_cpu_player,
this->assist_flags.load(),
hand_card_refs_s.c_str(),
this->assist_card_ref.load(),
assist_card_ref_s.c_str(),
set_card_refs_s.c_str(),
this->sc_card_ref.load(),
sc_card_ref_s.c_str(),
hand_card_refs2_s.c_str(),
set_card_refs2_s.c_str(),
this->assist_card_ref2.load(),
assist_card_ref2_s.c_str(),
this->assist_card_set_number.load(),
this->assist_card_id.load(),
assist_card_id_s.c_str(),
this->assist_remaining_turns,
this->assist_delay_turns,
this->atk_bonuses,
+12 -11
View File
@@ -35,7 +35,7 @@ struct Condition {
void clear();
void clear_FF();
std::string str() const;
std::string str(std::shared_ptr<const Server> s) const;
} __attribute__((packed));
struct EffectResult {
@@ -55,9 +55,9 @@ struct EffectResult {
bool operator==(const EffectResult& other) const;
bool operator!=(const EffectResult& other) const;
std::string str() const;
void clear();
std::string str(std::shared_ptr<const Server> s) const;
} __attribute__((packed));
struct CardShortStatus {
@@ -77,7 +77,7 @@ struct CardShortStatus {
void clear();
void clear_FF();
std::string str() const;
std::string str(std::shared_ptr<const Server> s) const;
} __attribute__((packed));
struct ActionState {
@@ -87,7 +87,8 @@ struct ActionState {
/* 04 */ le_uint16_t attacker_card_ref;
/* 06 */ le_uint16_t defense_card_ref;
/* 08 */ parray<le_uint16_t, 4 * 9> target_card_refs;
/* 50 */ parray<le_uint16_t, 9> action_card_refs;
/* 50 */ parray<le_uint16_t, 8> action_card_refs;
/* 60 */ le_uint16_t unused2;
/* 62 */ le_uint16_t original_attacker_card_ref;
/* 64 */
@@ -97,7 +98,7 @@ struct ActionState {
void clear();
std::string str() const;
std::string str(std::shared_ptr<const Server> s) const;
} __attribute__((packed));
struct ActionChain {
@@ -133,7 +134,7 @@ struct ActionChain {
void clear();
void clear_FF();
std::string str() const;
std::string str(std::shared_ptr<const Server> s) const;
} __attribute__((packed));
struct ActionChainWithConds {
@@ -171,7 +172,7 @@ struct ActionChainWithConds {
uint8_t get_adjusted_move_ability_nte(uint8_t ability) const;
std::string str() const;
std::string str(std::shared_ptr<const Server> s) const;
} __attribute__((packed));
struct ActionChainWithCondsTrial {
@@ -224,8 +225,6 @@ struct ActionMetadata {
bool operator==(const ActionMetadata& other) const;
bool operator!=(const ActionMetadata& other) const;
std::string str() const;
void clear();
void clear_FF();
@@ -240,6 +239,8 @@ struct ActionMetadata {
uint16_t defense_card_ref,
std::shared_ptr<Card> card,
uint16_t original_attacker_card_ref);
std::string str(std::shared_ptr<const Server> s) const;
} __attribute__((packed));
struct HandAndEquipState {
@@ -274,7 +275,7 @@ struct HandAndEquipState {
void clear();
void clear_FF();
std::string str() const;
std::string str(std::shared_ptr<const Server> s) const;
} __attribute__((packed));
struct PlayerBattleStats {
+11 -12
View File
@@ -939,7 +939,8 @@ bool RulerServer::check_usability_or_condition_apply(
bool is_item_usability_check,
AttackMedium attack_medium) const {
auto s = this->server();
auto log = s->log_stack(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", name_for_attack_medium(attack_medium)));
bool is_nte = s->options.is_nte();
auto log = s->log_stack(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", name_for_enum(attack_medium)));
if (static_cast<uint8_t>(attack_medium) & 0x80) {
attack_medium = AttackMedium::UNKNOWN;
@@ -952,7 +953,7 @@ bool RulerServer::check_usability_or_condition_apply(
log.debug("ce1 missing");
return false;
}
if (!s->options.is_nte() && (ce1->def.type == CardType::ITEM) && this->card_id_is_boss_sc(card_id2)) {
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");
return false;
}
@@ -967,7 +968,7 @@ bool RulerServer::check_usability_or_condition_apply(
}
criterion_code = ce1->def.effects[def_effect_index].apply_criterion;
}
log.debug("criterion_code=%s", name_for_criterion_code(criterion_code));
log.debug("criterion_code=%s", name_for_enum(criterion_code));
// For item usability checks, prevent criteria that depend on player
// positioning/team setup
@@ -989,7 +990,7 @@ bool RulerServer::check_usability_or_condition_apply(
// second should not be given, so we'd return true if the criterion passes. If
// neither of these cases apply, we should return false as a failsafe even if
// the criterion passes. NTE did not have such a check.
bool ret = s->options.is_nte() || (!(def_effect_index & 0x80) || (client_id1 == client_id2)) || (client_id2 == 0xFF);
bool ret = is_nte || (!(def_effect_index & 0x80) || (client_id1 == client_id2)) || (client_id2 == 0xFF);
switch (criterion_code) {
case CriterionCode::NONE:
return ret;
@@ -1413,7 +1414,7 @@ uint16_t RulerServer::compute_attack_or_defense_costs(
return 99;
}
total_cost += (ce->def.self_cost + cost_bias);
if (card_class_is_tech_like(ce->def.card_class(), s->options.is_nte())) {
if (card_class_is_tech_like(ce->def.card_class(), is_nte)) {
total_cost += tech_cost_bias;
}
total_ally_cost += ce->def.ally_cost;
@@ -1441,8 +1442,7 @@ uint16_t RulerServer::compute_attack_or_defense_costs(
assist_cost_bias++;
} else if (is_nte && (assist_effect == AssistEffect::DEFLATION)) {
assist_cost_bias--;
} else if ((assist_effect == AssistEffect::BATTLE_ROYALE) &&
(pa.action_card_refs[0] == 0xFFFF)) {
} else if ((assist_effect == AssistEffect::BATTLE_ROYALE) && (pa.action_card_refs[0] == 0xFFFF)) {
total_cost = 0;
final_cost = 0;
}
@@ -1470,9 +1470,9 @@ bool RulerServer::compute_effective_range_and_target_mode_for_attack(
TargetMode* out_effective_target_mode,
uint16_t* out_orig_card_ref) const {
size_t z;
for (z = 0; (z < 9) && (pa.action_card_refs[z] != 0xFFFF); z++) {
for (z = 0; (z < 8) && (pa.action_card_refs[z] != 0xFFFF); z++) {
}
if (z >= 9) {
if (z >= 8) {
return false;
}
uint16_t card_ref = (z == 0) ? pa.attacker_card_ref : pa.action_card_refs[z - 1];
@@ -1630,8 +1630,7 @@ bool RulerServer::defense_card_can_apply_to_attack(
return true;
}
bool RulerServer::defense_card_matches_any_attack_card_top_color(
const ActionState& pa) const {
bool RulerServer::defense_card_matches_any_attack_card_top_color(const ActionState& pa) const {
auto ce = this->definition_for_card_ref(pa.action_card_refs[0]);
if (!ce) {
throw runtime_error("defense card definition is missing");
@@ -2274,7 +2273,7 @@ bool RulerServer::is_attack_valid(const ActionState& pa) {
size_t conditional_card_count = 0;
size_t z;
for (z = 0; z < 9; z++) {
for (z = 0; z < 8; z++) {
uint16_t right_card_ref = pa.action_card_refs[z];
if (right_card_ref == 0xFFFF) {
break;
+5 -5
View File
@@ -198,11 +198,11 @@ private:
std::weak_ptr<Server> w_server;
public:
std::shared_ptr<HandAndEquipState> hand_and_equip_states[4];
std::shared_ptr<parray<CardShortStatus, 0x10>> short_statuses[4];
std::shared_ptr<DeckEntry> deck_entries[4];
std::shared_ptr<parray<ActionChainWithConds, 9>> set_card_action_chains[4];
std::shared_ptr<parray<ActionMetadata, 9>> set_card_action_metadatas[4];
bcarray<std::shared_ptr<HandAndEquipState>, 4> hand_and_equip_states;
bcarray<std::shared_ptr<parray<CardShortStatus, 0x10>>, 4> short_statuses;
bcarray<std::shared_ptr<DeckEntry>, 4> deck_entries;
bcarray<std::shared_ptr<parray<ActionChainWithConds, 9>>, 4> set_card_action_chains;
bcarray<std::shared_ptr<parray<ActionMetadata, 9>>, 4> set_card_action_metadatas;
std::shared_ptr<MapAndRulesState> map_and_rules;
std::shared_ptr<StateFlags> state_flags;
std::shared_ptr<AssistServer> assist_server;
+137 -53
View File
@@ -34,6 +34,9 @@ Server::Server(shared_ptr<Lobby> lobby, Options&& options)
last_chosen_map(this->options.tournament ? this->options.tournament->get_map() : nullptr),
tournament_match_result_sent(false),
override_environment_number(0xFF),
def_dice_value_range_override(0xFF),
atk_dice_value_range_2v1_override(0xFF),
def_dice_value_range_2v1_override(0xFF),
battle_finished(false),
battle_in_progress(false),
round_num(1),
@@ -96,10 +99,10 @@ void Server::init() {
this->card_special = make_shared<CardSpecial>(this->shared_from_this());
// Note: The original implementation calls the default PSOV2Encryption
// constructor for random_crypt, which just uses 0 as the seed. It then
// constructor for opt_rand_crypt, which just uses 0 as the seed. It then
// re-seeds the generator later. We instead expect the caller to provide a
// seeded generator, and we don't re-seed it at all.
// this->random_crypt = make_shared<PSOV2Encryption>(0);
// this->opt_rand_crypt = make_shared<PSOV2Encryption>(0);
this->state_flags = make_shared<StateFlags>();
@@ -160,6 +163,32 @@ const Server::StackLogger& Server::log() const {
return *this->logger_stack.back();
}
std::string Server::debug_str_for_card_ref(uint16_t card_ref) const {
if (card_ref == 0xFFFF) {
return "@FFFF";
}
auto ce = this->definition_for_card_ref(card_ref);
if (ce) {
string name = ce->def.en_name.decode();
return string_printf("@%04hX (#%04" PRIX32 " %s)", card_ref, ce->def.card_id.load(), name.c_str());
} else {
return string_printf("@%04hX (missing)", card_ref);
}
}
std::string Server::debug_str_for_card_id(uint16_t card_id) const {
if (card_id == 0xFFFF) {
return "#FFFF";
}
auto ce = this->definition_for_card_id(card_id);
if (ce) {
string name = ce->def.en_name.decode();
return string_printf("#%04hX (%s)", card_id, name.c_str());
} else {
return string_printf("#%04hX (missing)", card_id);
}
}
int8_t Server::get_winner_team_id() const {
// Note: This function is not part of the original implementation.
@@ -227,7 +256,8 @@ void Server::send(const void* data, size_t size, uint8_t command, bool enable_ma
l->battle_record->add_command(BattleRecord::Event::Type::BATTLE_COMMAND, data, size);
}
} else if (this->log().info("Generated command")) {
} else if ((this->options.behavior_flags & BehaviorFlag::LOG_COMMANDS_IF_LOBBY_MISSING) &&
this->log().info("Generated command")) {
print_data(stderr, data, size);
}
}
@@ -235,25 +265,26 @@ void Server::send(const void* data, size_t size, uint8_t command, bool enable_ma
void Server::send_6xB4x46() const {
// Note: This function is not part of the original implementation; it was
// factored out from its callsites in this file and the strings were changed.
if (this->options.is_nte()) {
G_ServerVersionStrings_Ep3NTE_6xB4x46 cmd;
cmd.version_signature.encode(VERSION_SIGNATURE_NTE, 1);
cmd.date_str1.encode(format_time(this->options.card_index->definitions_mtime() * 1000000), 1);
this->send(cmd);
} else {
G_ServerVersionStrings_Ep3_6xB4x46 cmd;
cmd.version_signature.encode(VERSION_SIGNATURE, 1);
cmd.date_str1.encode(format_time(this->options.card_index->definitions_mtime() * 1000000), 1);
string date_str2 = string_printf(
// NTE doesn't have the date_str2 field, but we send it anyway to make
// debugging easier.
G_ServerVersionStrings_Ep3_6xB4x46 cmd;
cmd.version_signature.encode(this->options.is_nte() ? VERSION_SIGNATURE_NTE : VERSION_SIGNATURE, 1);
cmd.date_str1.encode(format_time(this->options.card_index->definitions_mtime() * 1000000), 1);
string date_str2;
if (this->options.opt_rand_crypt) {
date_str2 = string_printf(
"Random:%08" PRIX32 "+%08" PRIX32,
this->options.random_crypt->seed(),
this->options.random_crypt->absolute_offset());
if (this->last_chosen_map) {
date_str2 += string_printf(" Map:%08" PRIX32, this->last_chosen_map->map_number);
}
cmd.date_str2.encode(date_str2, 1);
this->send(cmd);
this->options.opt_rand_crypt->seed(),
this->options.opt_rand_crypt->absolute_offset());
} else {
date_str2 = "Random:<SYS>";
}
if (this->last_chosen_map) {
date_str2 += string_printf(" Map:%08" PRIX32, this->last_chosen_map->map_number);
}
cmd.date_str2.encode(date_str2, 1);
this->send(cmd);
}
string Server::prepare_6xB6x41_map_definition(shared_ptr<const MapIndex::Map> map, uint8_t language, bool is_nte) {
@@ -946,44 +977,63 @@ 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(LogLevel::DEBUG)) {
string s = pa->str(this->shared_from_this());
log.debug("input: %s", s.c_str());
}
if (client_id >= 4) {
this->ruler_server->error_code3 = -0x78;
log.debug("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");
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");
}
} else {
pa->defense_card_ref = pa->action_card_refs[0];
log.debug("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");
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");
return true;
} else if (ally_atk_result == -1) {
log.debug("failed: ally declined");
return false;
}
if (this->num_pending_attacks >= 0x20) {
this->ruler_server->error_code3 = -0x71;
log.debug("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(LogLevel::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());
}
ps->set_action_cards_for_action_state(*pa);
log.debug("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;
@@ -1032,14 +1082,14 @@ shared_ptr<const PlayerState> Server::get_player_state(uint8_t client_id) const
uint32_t Server::get_random(uint32_t max) {
// The original implementation was essentially:
// return (static_cast<double>(this->random_crypt->next() >> 16) / 65536.0) * max
// This is unnecessarily complicated, so we instead just do this:
return this->options.random_crypt->next() % max;
// return (static_cast<double>(this->opt_rand_crypt->next() >> 16) / 65536.0) * max
// This is unnecessarily complicated and imprecise, so we instead just do:
return random_from_optional_crypt(this->options.opt_rand_crypt) % max;
}
float Server::get_random_float_0_1() {
// This lacks some precision, but matches the original implementation.
return (static_cast<double>(this->options.random_crypt->next() >> 16) / 65536.0);
return (static_cast<double>(random_from_optional_crypt(this->options.opt_rand_crypt) >> 16) / 65536.0);
}
uint32_t Server::get_round_num() const {
@@ -1318,6 +1368,13 @@ void Server::set_battle_started() {
this->send_6xB4x05();
}
bool Server::player_can_receive_dice_boost(uint8_t client_id) const {
auto ps = this->player_states[client_id];
bool is_1p_2v1 = (this->team_client_count.at(ps->get_team_id()) < this->team_client_count[ps->get_team_id() ^ 1]);
const auto& rules = this->map_and_rules->rules;
return (rules.atk_dice_range(is_1p_2v1).second >= 3) || (rules.def_dice_range(is_1p_2v1).second >= 3);
}
void Server::set_client_id_ready_to_advance_phase(uint8_t client_id, BattlePhase battle_phase) {
if (client_id >= 4) {
return;
@@ -1332,7 +1389,10 @@ void Server::set_client_id_ready_to_advance_phase(uint8_t client_id, BattlePhase
ps->assist_flags |= AssistFlag::READY_TO_END_PHASE;
ps->update_hand_and_equip_state_and_send_6xB4x02_if_needed();
if (this->battle_phase == BattlePhase::DICE) {
if (is_nte || !(ps->assist_flags & AssistFlag::ELIGIBLE_FOR_DICE_BOOST) || this->map_and_rules->rules.disable_dice_boost) {
if (is_nte ||
!(ps->assist_flags & AssistFlag::ELIGIBLE_FOR_DICE_BOOST) ||
this->map_and_rules->rules.disable_dice_boost ||
!this->player_can_receive_dice_boost(client_id)) {
ps->assist_flags &= (~AssistFlag::ELIGIBLE_FOR_DICE_BOOST);
ps->roll_main_dice_or_apply_after_effects();
if (!is_nte && (ps->get_atk_points() < 3) && (ps->get_def_points() < 3)) {
@@ -1486,8 +1546,8 @@ void Server::setup_and_start_battle() {
this->setup_phase = SetupPhase::STARTER_ROLLS;
// Note: This is where original implementation re-seeds random_crypt (it uses
// time() as the seed value).
// Note: This is where original implementation re-seeds opt_rand_crypt (it
// uses time() as the seed value).
for (size_t z = 0; z < 4; z++) {
if (!this->check_presence_entry(z)) {
@@ -1761,8 +1821,10 @@ const unordered_map<uint8_t, Server::handler_t> Server::subcommand_handlers({
void Server::on_server_data_input(shared_ptr<Client> sender_c, const string& data) {
auto header = check_size_t<G_CardBattleCommandHeader>(data, 0xFFFF);
if (header.size * 4 < data.size()) {
throw runtime_error("command is incomplete");
size_t expected_size = header.size * 4;
if (expected_size < data.size()) {
print_data(stderr, data);
throw runtime_error(string_printf("command is incomplete: expected %zX bytes, received %zX bytes", expected_size, data.size()));
}
if (header.subcommand != 0xB3) {
throw runtime_error("server data command is not 6xB3");
@@ -1775,7 +1837,7 @@ void Server::on_server_data_input(shared_ptr<Client> sender_c, const string& dat
throw runtime_error("unknown CAx subsubcommand");
}
if (this->options.is_nte() || !header.mask_key) {
if ((sender_c->version() == Version::GC_EP3_NTE) || !header.mask_key) {
(this->*handler)(sender_c, data);
} else {
string unmasked_data = data;
@@ -2091,22 +2153,41 @@ void Server::handle_CAx12_end_attack_list(shared_ptr<Client>, const string& data
}
template <typename CmdT>
void Server::handle_CAx13_update_map_during_setup_t(shared_ptr<Client>, const string& data) {
void Server::handle_CAx13_update_map_during_setup_t(shared_ptr<Client> c, const string& data) {
const auto& in_cmd = check_size_t<CmdT>(data);
this->send_debug_command_received_message(
in_cmd.header.subsubcommand, "UPDATE MAP");
this->send_debug_command_received_message(in_cmd.header.subsubcommand, "UPDATE MAP");
if (!this->battle_in_progress &&
(this->setup_phase == SetupPhase::REGISTRATION) &&
(this->map_and_rules->num_players == 0) &&
(this->registration_phase != RegistrationPhase::REGISTERED) &&
(this->registration_phase != RegistrationPhase::BATTLE_STARTED)) {
// newserv's extended rules are stored in unused parts of the Rules struct,
// and clients will probably overwrite them with zeroes if we allow them to.
// So, we preserve the extended rules manually here.
uint8_t def_dice_range = this->map_and_rules->rules.def_dice_range;
*this->map_and_rules = in_cmd.map_and_rules_state;
this->map_and_rules->rules.def_dice_range = def_dice_range;
// The client will likely send incorrect values for the extended rules (or
// in the case of NTE, no values at all, since the Rules structure is
// smaller). So, use the values from the last chosen map if applicable, or
// the values from the $dicerange command if available.
const Rules* map_rules = this->last_chosen_map ? &this->last_chosen_map->version(c->language())->map->default_rules : nullptr;
auto& server_rules = this->map_and_rules->rules;
// NTE can specify the DEF dice value range in its Rules struct, so we use
// that unless the map or $dicerange overrides it.
server_rules.def_dice_value_range = (map_rules && (map_rules->def_dice_value_range != 0xFF))
? map_rules->def_dice_value_range
: (this->def_dice_value_range_override != 0xFF)
? this->def_dice_value_range_override
: this->options.is_nte()
? server_rules.def_dice_value_range
: 0;
server_rules.atk_dice_value_range_2v1 = (map_rules && (map_rules->atk_dice_value_range_2v1 != 0xFF))
? map_rules->atk_dice_value_range_2v1
: (this->atk_dice_value_range_2v1_override != 0xFF)
? this->atk_dice_value_range_2v1_override
: 0;
server_rules.def_dice_value_range_2v1 = (map_rules && (map_rules->def_dice_value_range_2v1 != 0xFF))
? map_rules->def_dice_value_range_2v1
: (this->def_dice_value_range_2v1_override != 0xFF)
? this->def_dice_value_range_2v1_override
: 0;
// If this match is part of a tournament, ignore the rules sent by the
// client and use the tournament rules instead.
@@ -2413,8 +2494,7 @@ void Server::handle_CAx34_subtract_ally_atk_points(shared_ptr<Client>, const str
attacker_card->card_flags |= 0x400;
attacker_card->player_state()->send_6xB4x04_if_needed();
}
uint16_t card_ref = this->send_6xB4x06_if_card_ref_invalid(
pa.original_attacker_card_ref, 9);
uint16_t card_ref = this->send_6xB4x06_if_card_ref_invalid(pa.original_attacker_card_ref, 9);
auto orig_attacker_card = this->card_for_set_card_ref(card_ref);
auto target_card = this->card_for_set_card_ref(pa.target_card_refs[0]);
if (orig_attacker_card && target_card) {
@@ -2499,6 +2579,10 @@ void Server::handle_CAx40_map_list_request(shared_ptr<Client> sender_c, const st
}
void Server::send_6xB6x41_to_all_clients() const {
if (!this->last_chosen_map) {
throw logic_error("cannot send 6xB4x41 without a map chosen");
}
auto l = this->lobby.lock();
if (l) {
vector<string> map_commands_by_language;
@@ -2546,9 +2630,7 @@ void Server::send_6xB6x41_to_all_clients() const {
void Server::handle_CAx41_map_request(shared_ptr<Client>, const string& data) {
const auto& cmd = check_size_t<G_MapDataRequest_Ep3_CAx41>(data);
this->send_debug_command_received_message(
cmd.header.subsubcommand, "MAP DATA");
this->send_debug_command_received_message(cmd.header.subsubcommand, "MAP DATA");
this->last_chosen_map = this->options.map_index->for_number(cmd.map_number);
this->send_6xB6x41_to_all_clients();
}
@@ -2713,11 +2795,9 @@ uint32_t Server::get_team_exp(uint8_t team_id) const {
return this->team_exp[team_id];
}
uint32_t Server::send_6xB4x06_if_card_ref_invalid(
uint16_t card_ref, int16_t negative_value) {
uint32_t Server::send_6xB4x06_if_card_ref_invalid(uint16_t card_ref, int16_t negative_value) {
if (this->card_special) {
return this->card_special->send_6xB4x06_if_card_ref_invalid(
card_ref, -negative_value);
return this->card_special->send_6xB4x06_if_card_ref_invalid(card_ref, -negative_value);
}
return card_ref;
}
@@ -2735,16 +2815,20 @@ 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];
log.debug("card @%04hX #%04hX can attack", card->get_card_ref(), card->get_card_id());
string as_str = as.str();
log.debug("as: %s", as_str.c_str());
if (log.should_log(LogLevel::DEBUG)) {
log.debug("card @%04hX #%04hX 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());
}
if (is_nte) {
this->replace_targets_due_to_destruction_nte(&as);
} else {
this->replace_targets_due_to_destruction_or_conditions(&as);
}
as_str = as.str();
log.debug("as after target replacement: %s", as_str.c_str());
if (log.should_log(LogLevel::DEBUG)) {
string as_str = as.str(this->shared_from_this());
log.debug("as after target replacement: %s", as_str.c_str());
}
if (this->any_target_exists_for_attack(as)) {
log.debug("as is valid");
break;
@@ -2856,7 +2940,7 @@ void Server::replace_targets_due_to_destruction_nte(ActionState* as) {
if (!target_card) {
break;
}
if ((target_card->card_flags & 2) ||
if (!(target_card->card_flags & 2) ||
(target_card->get_definition()->def.type != CardType::ITEM) ||
attacker_card->action_chain.check_flag(0x02)) {
continue;
+37 -6
View File
@@ -71,7 +71,7 @@ public:
std::shared_ptr<const CardIndex> card_index;
std::shared_ptr<const MapIndex> map_index;
uint32_t behavior_flags;
std::shared_ptr<PSOLFGEncryption> random_crypt;
std::shared_ptr<PSOLFGEncryption> opt_rand_crypt;
std::shared_ptr<const Tournament> tournament;
std::array<std::vector<uint16_t>, 5> trap_card_ids;
@@ -99,6 +99,33 @@ public:
StackLogger log_stack(const std::string& prefix) const;
const StackLogger& log() const;
std::string debug_str_for_card_ref(uint16_t card_ref) const;
std::string debug_str_for_card_id(uint16_t card_id) const;
template <typename U16T>
std::string debug_str_for_card_refs(const U16T* refs, size_t count) const {
std::string ret = "[";
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 += string_printf("%zu:%s ", z, ref_str.c_str());
}
}
if (ret.size() > 1) {
ret.back() = ']'; // Replace the ' ' from the last added item
} else {
ret.push_back(']');
}
return ret;
}
template <typename U16T>
std::string debug_str_for_card_refs(const std::vector<U16T>& refs) const {
return this->debug_str_for_card_refs(refs.data(), refs.size());
}
template <typename U16T, size_t Count>
std::string debug_str_for_card_refs(const parray<U16T, Count>& refs) const {
return this->debug_str_for_card_refs(refs.data(), refs.size());
}
int8_t get_winner_team_id() const;
template <typename T>
@@ -178,6 +205,7 @@ public:
void send_set_card_updates_and_6xB4x04_if_needed();
void set_battle_ended();
void set_battle_started();
bool player_can_receive_dice_boost(uint8_t client_id) const;
void set_client_id_ready_to_advance_phase(uint8_t client_id, BattlePhase battle_phase);
void set_phase_after();
void move_phase_before();
@@ -250,6 +278,9 @@ public:
std::shared_ptr<const MapIndex::Map> last_chosen_map;
bool tournament_match_result_sent;
uint8_t override_environment_number;
uint8_t def_dice_value_range_override;
uint8_t atk_dice_value_range_2v1_override;
uint8_t def_dice_value_range_2v1_override;
mutable std::deque<StackLogger*> logger_stack;
// These fields were originally contained in the TCardServerBase object
@@ -261,7 +292,7 @@ public:
void clear();
} __attribute__((packed));
std::shared_ptr<MapAndRulesState> map_and_rules;
std::shared_ptr<DeckEntry> deck_entries[4];
bcarray<std::shared_ptr<DeckEntry>, 4> deck_entries;
parray<PresenceEntry, 4> presence_entries;
uint8_t num_clients_present;
parray<NameEntry, 4> name_entries;
@@ -280,7 +311,7 @@ public:
RegistrationPhase registration_phase;
ActionSubphase action_subphase;
uint8_t current_team_turn2;
ActionState pending_attacks[0x20];
bcarray<ActionState, 0x20> pending_attacks;
uint32_t num_pending_attacks;
parray<uint8_t, 4> client_done_enqueuing_attacks;
parray<uint8_t, 4> player_ready_to_end_phase;
@@ -296,8 +327,8 @@ public:
std::array<std::shared_ptr<PlayerState>, 4> player_states;
parray<uint32_t, 4> clients_done_in_mulligan_phase;
uint32_t num_pending_attacks_with_cards;
std::shared_ptr<Card> attack_cards[0x20];
ActionState pending_attacks_with_cards[0x20];
bcarray<std::shared_ptr<Card>, 0x20> attack_cards;
bcarray<ActionState, 0x20> pending_attacks_with_cards;
uint32_t unknown_a14;
uint32_t unknown_a15;
parray<uint32_t, 4> defense_list_ended_for_client;
@@ -315,7 +346,7 @@ public:
parray<parray<parray<uint8_t, 2>, 8>, 5> trap_tile_locs;
parray<parray<uint8_t, 2>, 0x10> trap_tile_locs_nte;
size_t num_trap_tiles_nte;
ActionState pb_action_states[4];
bcarray<ActionState, 4> pb_action_states;
parray<uint8_t, 4> has_done_pb;
parray<parray<uint8_t, 4>, 4> has_done_pb_with_client;
mutable uint32_t num_6xB4x06_commands_sent;
+43
View File
@@ -0,0 +1,43 @@
#include "EventUtils.hh"
#include <event2/event.h>
#include <deque>
#include <functional>
#include <memory>
#include <stdexcept>
static void dispatch_forward_to_event_thread(evutil_socket_t, short, void* ctx) {
auto* fn = reinterpret_cast<std::function<void()>*>(ctx);
(*fn)();
delete fn;
}
void forward_to_event_thread(std::shared_ptr<struct event_base> base, std::function<void()>&& fn) {
struct timeval tv = {0, 0};
std::function<void()>* new_fn = new std::function<void()>(std::move(fn));
event_base_once(base.get(), -1, EV_TIMEOUT, dispatch_forward_to_event_thread, new_fn, &tv);
}
template <>
void call_on_event_thread<void>(std::shared_ptr<struct event_base> base, std::function<void()>&& compute) {
bool succeeded = false;
std::string exc_what;
std::mutex ret_lock;
std::condition_variable ret_cv;
std::unique_lock<std::mutex> g(ret_lock);
forward_to_event_thread(base, [&]() -> void {
std::lock_guard<std::mutex> g(ret_lock);
try {
compute();
succeeded = true;
} catch (const std::exception& e) {
exc_what = e.what();
}
ret_cv.notify_one();
});
ret_cv.wait(g);
if (!succeeded) {
throw std::runtime_error(exc_what);
}
}
+38
View File
@@ -0,0 +1,38 @@
#pragma once
#include <event2/event.h>
#include <condition_variable>
#include <functional>
#include <memory>
#include <mutex>
#include <optional>
#include <stdexcept>
void forward_to_event_thread(std::shared_ptr<struct event_base> base, std::function<void()>&& fn);
template <typename T>
T call_on_event_thread(std::shared_ptr<struct event_base> base, std::function<T()>&& compute) {
std::optional<T> ret;
std::string exc_what;
std::mutex ret_lock;
std::condition_variable ret_cv;
std::unique_lock<std::mutex> g(ret_lock);
forward_to_event_thread(base, [&]() -> void {
std::lock_guard<std::mutex> g(ret_lock);
try {
ret = compute();
} catch (const std::exception& e) {
exc_what = e.what();
}
ret_cv.notify_one();
});
ret_cv.wait(g);
if (!ret.has_value()) {
throw std::runtime_error(exc_what);
}
return ret.value();
}
template <>
void call_on_event_thread<void>(std::shared_ptr<struct event_base> base, std::function<void()>&& compute);
+6
View File
@@ -345,6 +345,12 @@ bool FunctionCodeIndex::patch_menu_empty(uint32_t specific_version) const {
return true;
}
std::shared_ptr<const CompiledFunctionCode> FunctionCodeIndex::get_patch(
const std::string& name, uint32_t specific_version) const {
return this->name_and_specific_version_to_patch_function.at(
string_printf("%s-%08" PRIX32, name.c_str(), specific_version));
}
DOLFileIndex::DOLFileIndex(const string& directory) {
if (!function_compiler_available()) {
function_compiler_log.info("Function compiler is not available");
+2
View File
@@ -70,6 +70,8 @@ struct FunctionCodeIndex {
std::shared_ptr<const Menu> patch_menu(uint32_t specific_version) const;
bool patch_menu_empty(uint32_t specific_version) const;
std::shared_ptr<const CompiledFunctionCode> get_patch(const std::string& name, uint32_t specific_version) const;
};
struct DOLFileIndex {
+992
View File
@@ -0,0 +1,992 @@
#include "HTTPServer.hh"
#include <event2/buffer.h>
#include <event2/event.h>
#include <event2/http.h>
#include <inttypes.h>
#include <stdlib.h>
#include <phosg/Network.hh>
#include <string>
#include <vector>
#include "EventUtils.hh"
#include "Loggers.hh"
#include "ProxyServer.hh"
#include "Server.hh"
using namespace std;
const unordered_map<int, const char*> 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::http_error::http_error(int code, const string& what)
: runtime_error(what),
code(code) {}
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);
}
void HTTPServer::send_response(struct evhttp_request* req, int code, const char* content_type, const char* fmt, ...) {
unique_ptr<struct evbuffer, void (*)(struct evbuffer*)> 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<string, string> HTTPServer::parse_url_params(const string& query) {
unordered_multimap<string, string> params;
if (query.empty()) {
return params;
}
for (auto it : 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<char>(value_for_hex_char(value[read_offset + 1]) << 4) |
static_cast<char>(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++;
}
}
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<string, string> HTTPServer::parse_url_params_unique(const string& query) {
unordered_map<string, string> 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<string, string>& 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<ServerState> state)
: state(state),
base(event_base_new(), event_base_free),
http(evhttp_new(this->base.get()), evhttp_free),
th(&HTTPServer::thread_fn, this) {
evhttp_set_gencb(this->http.get(), this->dispatch_handle_request, this);
}
void HTTPServer::listen(const string& socket_path) {
int fd = ::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 = ::listen(addr, port, SOMAXCONN);
string netloc_str = 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();
}
void HTTPServer::dispatch_handle_request(struct evhttp_request* req, void* ctx) {
reinterpret_cast<HTTPServer*>(ctx)->handle_request(req);
}
JSON HTTPServer::generate_quest_json_st(shared_ptr<const Quest> q) {
if (!q) {
return nullptr;
}
auto battle_rules_json = q->battle_rules ? q->battle_rules->json() : nullptr;
auto challenge_template_index_json = (q->challenge_template_index >= 0)
? q->challenge_template_index
: JSON(nullptr);
return JSON::dict({
{"Number", q->quest_number},
{"Episode", name_for_episode(q->episode)},
{"Joinable", q->joinable},
{"Name", q->name},
{"BattleRules", std::move(battle_rules_json)},
{"ChallengeTemplateIndex", std::move(challenge_template_index_json)},
});
}
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 = 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 : JSON(nullptr));
ret.emplace("OverrideSectionID", (config.override_section_id != 0xFF) ? config.override_section_id : JSON(nullptr));
ret.emplace("OverrideLobbyEvent", (config.override_lobby_event != 0xFF) ? config.override_lobby_event : JSON(nullptr));
ret.emplace("OverrideLobbyNumber", (config.override_lobby_number != 0x80) ? config.override_lobby_number : JSON(nullptr));
return ret;
}
JSON HTTPServer::generate_license_json_st(shared_ptr<const License> l) {
auto ret = JSON::dict({
{"SerialNumber", l->serial_number},
{"Flags", l->flags},
{"Ep3CurrentMeseta", l->ep3_current_meseta},
{"Ep3TotalMesetaEarned", l->ep3_total_meseta_earned},
{"BBTeamID", l->bb_team_id},
});
ret.emplace("BanEndTime", l->ban_end_time ? l->ban_end_time : JSON(nullptr));
ret.emplace("XBGamertag", !l->xb_gamertag.empty() ? l->xb_gamertag : JSON(nullptr));
ret.emplace("XBUserID", l->xb_user_id ? l->xb_user_id : JSON(nullptr));
ret.emplace("BBUsername", !l->bb_username.empty() ? l->bb_username : JSON(nullptr));
return ret;
};
JSON HTTPServer::generate_game_client_json_st(shared_ptr<const Client> c, shared_ptr<const ItemNameIndex> item_name_index) {
auto ret = JSON::dict({
{"ID", c->id},
{"RemoteAddress", render_sockaddr_storage(c->channel.remote_addr)},
{"Version", 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->x},
{"LocationZ", c->z},
{"LocationFloor", c->floor},
{"CanChat", c->can_chat},
});
ret.emplace("license", c->license ? HTTPServer::generate_license_json_st(c->license) : JSON(nullptr));
auto l = c->lobby.lock();
if (l) {
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);
}
auto p = c->character(false, false);
if (p) {
if (!is_ep3(c->version())) {
ret.emplace("InventoryItems", p->inventory.num_items);
if (c->version() != Version::DC_NTE) {
ret.emplace("InventoryLanguage", 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));
}
}
JSON items_json = JSON::list();
for (size_t z = 0; z < p->inventory.num_items; z++) {
const auto& item = p->inventory.items[z];
auto item_dict = JSON::dict({
{"Flags", item.flags.load()},
{"Data", item.data.hex()},
{"ItemID", item.data.id.load()},
});
if (item_name_index) {
item_dict.emplace("Description", item_name_index->describe_item(item.data, false));
}
items_json.emplace_back(std::move(item_dict));
}
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 = 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 : JSON(nullptr));
}
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 : JSON(nullptr));
ret.emplace("SectionID", name_for_section_id(p->disp.visual.section_id));
ret.emplace("CharClass", name_for_char_class(p->disp.visual.section_id));
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("AutoReply", p->auto_reply.decode(c->language()));
ret.emplace("InfoBoard", p->info_board.decode(c->language()));
auto battle_place_counts = 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());
if (!is_ep3(c->version())) {
auto json_for_challenge_times = []<size_t Count>(const parray<ChallengeTime<false>, Count>& times) -> JSON {
auto times_json = JSON::list();
for (size_t z = 0; z < times.size(); z++) {
times_json.emplace_back(times[z].load());
}
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());
{
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", string_printf("%04hu-%02hhu-%02hhu %02hhu:%02hhu:00", year, month, day, hour, minute));
}
string grave_enemy_types;
if (p->challenge_records.grave_defeated_by_enemy_rt_index) {
for (EnemyType type : enemy_types_for_rare_table_index(p->challenge_records.grave_is_ep2 ? Episode::EP2 : Episode::EP1, p->challenge_records.grave_defeated_by_enemy_rt_index)) {
if (!grave_enemy_types.empty()) {
grave_enemy_types += "/";
}
grave_enemy_types += 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.load());
ret.emplace("ChallengeAwardStateEp2OnlineFlags", p->challenge_records.ep2_online_award_state.rank_award_flags.load());
ret.emplace("ChallengeAwardStateEp2OnlineMaxRank", p->challenge_records.ep2_online_award_state.maximum_rank.load());
ret.emplace("ChallengeAwardStateEp1OfflineFlags", p->challenge_records.ep1_offline_award_state.rank_award_flags.load());
ret.emplace("ChallengeAwardStateEp1OfflineMaxRank", p->challenge_records.ep1_offline_award_state.maximum_rank.load());
ret.emplace("ChallengeRankTitle", p->challenge_records.rank_title.decode());
}
}
return ret;
}
JSON HTTPServer::generate_proxy_client_json_st(shared_ptr<const ProxyServer::LinkedSession> 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<LobbyPlayer> lobby_players;
auto lobby_players_json = 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(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 : JSON(nullptr));
} else {
lobby_players_json.emplace_back(nullptr);
}
}
auto ret = JSON::dict({
{"ID", ses->id},
{"RemoteClientAddress", render_sockaddr_storage(ses->client_channel.remote_addr)},
{"RemoteServerAddress", render_sockaddr_storage(ses->server_channel.remote_addr)},
{"LocalPort", ses->local_port},
{"NextDestination", render_sockaddr_storage(ses->next_destination)},
{"Version", name_for_enum(ses->version())},
{"SubVersion", ses->sub_version},
{"Name", ses->character_name},
{"DCHardwareID", ses->hardware_id},
{"RemoteGuildCardNumber", ses->remote_guild_card_number},
{"RemoteClientConfigData", 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->x},
{"LocationZ", ses->z},
{"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("License", ses->license ? HTTPServer::generate_license_json_st(ses->license) : JSON(nullptr));
return ret;
}
JSON HTTPServer::generate_lobby_json_st(shared_ptr<const Lobby> l, shared_ptr<const ItemNameIndex> item_name_index) {
std::array<std::shared_ptr<Client>, 12> clients;
auto client_ids_json = JSON::list();
for (size_t z = 0; z < l->max_clients; z++) {
client_ids_json.emplace_back(l->clients[z] ? l->clients[z]->id : JSON(nullptr));
}
auto ret = JSON::dict({
{"ID", l->lobby_id},
{"AllowedVersions", l->allowed_versions},
{"Event", l->event},
{"LeaderClientID", l->leader_id},
{"MaxClients", l->max_clients},
{"IdleTimeoutUsecs", l->idle_timeout_usecs},
{"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("BaseVersion", l->base_version);
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("QuestInProgress", l->check_flag(Lobby::Flag::QUEST_IN_PROGRESS));
ret.emplace("JoinableQuestInProgress", l->check_flag(Lobby::Flag::JOINABLE_QUEST_IN_PROGRESS));
auto variations_json = JSON::list();
for (size_t z = 0; z < l->variations.size(); z++) {
variations_json.emplace_back(l->variations[z].load());
}
ret.emplace("Variations", std::move(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("AllowedDropModes", l->allowed_drop_modes);
switch (l->drop_mode) {
case Lobby::DropMode::DISABLED:
ret.emplace("DropMode", "none");
break;
case Lobby::DropMode::CLIENT:
ret.emplace("DropMode", "client");
break;
case Lobby::DropMode::SERVER_SHARED:
ret.emplace("DropMode", "shared");
break;
case Lobby::DropMode::SERVER_PRIVATE:
ret.emplace("DropMode", "private");
break;
case Lobby::DropMode::SERVER_DUPLICATE:
ret.emplace("DropMode", "duplicate");
break;
}
if (l->mode == GameMode::CHALLENGE) {
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);
}
}
auto floor_items_json = JSON::list();
for (size_t floor = 0; floor < l->floor_item_managers.size(); floor++) {
for (const auto& it : l->floor_item_managers[floor].items) {
const auto& item = it.second;
auto item_dict = JSON::dict({
{"LocationFloor", floor},
{"LocationX", item->x},
{"LocationZ", item->z},
{"DropNumber", item->drop_number},
{"VisibilityFlags", item->visibility_flags},
{"Data", item->data.hex()},
{"ItemID", item->data.id.load()},
});
if (item_name_index) {
item_dict.emplace("Description", item_name_index->describe_item(item->data, false));
}
floor_items_json.emplace_back(std::move(item_dict));
}
}
ret.emplace("FloorItems", std::move(floor_items_json));
ret.emplace("Quest", HTTPServer::generate_quest_json_st(l->quest));
} 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));
auto ep3s = l->ep3_server;
if (ep3s) {
auto players_json = JSON::list();
for (size_t z = 0; z < 4; z++) {
if (!ep3s->name_entries[z].present) {
players_json.emplace_back(nullptr);
} else {
auto lc = l->clients[z];
auto deck_entry = ep3s->deck_entries[z];
JSON deck_json = nullptr;
if (deck_entry) {
auto cards_json = JSON::list();
for (size_t w = 0; w < deck_entry->card_ids.size(); w++) {
try {
const auto& ce = ep3s->options.card_index->definition_for_id(deck_entry->card_ids[w]);
auto name = ce->def.en_name.decode();
if (name.empty()) {
name = ce->def.en_short_name.decode();
}
if (name.empty()) {
name = ce->def.jp_name.decode();
}
if (name.empty()) {
name = ce->def.jp_short_name.decode();
}
cards_json.emplace_back(name);
} catch (const out_of_range&) {
cards_json.emplace_back(deck_entry->card_ids[w].load());
}
}
deck_json = JSON::dict({
{"Name", deck_entry->name.decode(lc ? lc->language() : 1)},
{"TeamID", deck_entry->team_id.load()},
{"Cards", std::move(cards_json)},
{"GodWhimFlag", deck_entry->god_whim_flag},
{"PlayerLevel", deck_entry->player_level.load()},
});
}
auto player_json = JSON::dict({
{"PlayerName", ep3s->name_entries[z].name.decode(lc ? lc->language() : 1)},
{"ClientID", ep3s->name_entries[z].client_id},
{"IsCOM", !!ep3s->name_entries[z].is_cpu_player},
{"Deck", std::move(deck_json)},
});
players_json.emplace_back(std::move(player_json));
}
}
auto battle_state_json = JSON::dict({
{"BehaviorFlags", ep3s->options.behavior_flags},
{"RandomSeed", ep3s->options.opt_rand_crypt ? ep3s->options.opt_rand_crypt->seed() : JSON(nullptr)},
{"RandomOffset", ep3s->options.opt_rand_crypt ? ep3s->options.opt_rand_crypt->absolute_offset() : JSON(nullptr)},
{"Tournament", ep3s->options.tournament ? ep3s->options.tournament->json() : nullptr},
{"MapNumber", ep3s->last_chosen_map ? ep3s->last_chosen_map->map_number : JSON(nullptr)},
{"EnvironmentNumber", ep3s->map_and_rules ? ep3s->map_and_rules->environment_number : JSON(nullptr)},
{"Rules", ep3s->map_and_rules ? ep3s->map_and_rules->rules.json() : nullptr},
{"Players", std::move(players_json)},
{"IsBattleFinished", ep3s->battle_finished},
{"IsBattleInprogress", ep3s->battle_in_progress},
{"RoundNumber", ep3s->round_num},
{"FirstTeamTurn", ep3s->first_team_turn},
{"CurrentTeamTurn", ep3s->current_team_turn1},
{"BattlePhase", name_for_enum(ep3s->battle_phase)},
{"SetupPhase", ep3s->setup_phase},
{"RegistrationPhase", ep3s->registration_phase},
{"ActionSubphase", ep3s->action_subphase},
{"BattleStartTimeUsecs", ep3s->battle_start_usecs},
{"TeamEXP", JSON::list({ep3s->team_exp[0], ep3s->team_exp[1]})},
{"TeamDiceBonus", JSON::list({ep3s->team_dice_bonus[0], ep3s->team_dice_bonus[1]})},
});
// std::shared_ptr<StateFlags> state_flags;
// std::array<std::shared_ptr<PlayerState>, 4> player_states;
ret.emplace("Episode3BattleState", std::move(battle_state_json));
} else {
ret.emplace("Episode3BattleState", nullptr);
}
auto watched_lobby = l->watched_lobby.lock();
if (watched_lobby) {
ret.emplace("WatchedLobbyID", watched_lobby->lobby_id);
}
auto watcher_lobby_ids_json = 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);
}
} 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);
}
return ret;
}
JSON HTTPServer::generate_game_server_clients_json() const {
return call_on_event_thread<JSON>(this->state->base, [&]() {
auto res = 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;
});
}
JSON HTTPServer::generate_proxy_server_clients_json() const {
return call_on_event_thread<JSON>(this->state->base, [&]() {
JSON res = JSON::list();
for (const auto& it : this->state->proxy_server->all_sessions()) {
res.emplace_back(this->generate_proxy_client_json_st(it.second));
}
return res;
});
}
JSON HTTPServer::generate_server_info_json() const {
return call_on_event_thread<JSON>(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++;
} else {
lobby_count++;
}
}
uint64_t uptime_usecs = now() - this->state->creation_time;
return JSON::dict({
{"StartTimeUsecs", this->state->creation_time},
{"StartTime", format_time(this->state->creation_time)},
{"UptimeUsecs", uptime_usecs},
{"Uptime", format_duration(uptime_usecs)},
{"LobbyCount", lobby_count},
{"GameCount", game_count},
{"ClientCount", this->state->channel_to_client.size()},
{"ProxySessionCount", this->state->proxy_server->num_sessions()},
{"ServerName", this->state->name},
});
});
}
JSON HTTPServer::generate_lobbies_json() const {
return call_on_event_thread<JSON>(this->state->base, [&]() {
JSON res = JSON::list();
for (const auto& it : this->state->id_to_lobby) {
res.emplace_back(this->generate_lobby_json_st(it.second, this->state->item_name_index_opt(it.second->base_version)));
}
return res;
});
}
JSON HTTPServer::generate_summary_json() const {
auto ret = call_on_event_thread<JSON>(this->state->base, [&]() {
auto clients_json = 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(JSON::dict({
{"ID", c->id},
{"SerialNumber", c->license ? c->license->serial_number : JSON(nullptr)},
{"Name", p ? p->disp.name.decode(it.second->language()) : JSON(nullptr)},
{"Version", name_for_enum(it.second->version())},
{"Language", name_for_language_code(it.second->language())},
{"Level", p ? p->disp.stats.level + 1 : JSON(nullptr)},
{"Class", p ? name_for_char_class(p->disp.visual.char_class) : JSON(nullptr)},
{"SectionID", p ? name_for_section_id(p->disp.visual.section_id) : JSON(nullptr)},
{"LobbyID", l ? l->lobby_id : JSON(nullptr)},
}));
}
auto proxy_clients_json = JSON::list();
for (const auto& it : this->state->proxy_server->all_sessions()) {
proxy_clients_json.emplace_back(JSON::dict({
{"SerialNumber", it.second->license ? it.second->license->serial_number : JSON(nullptr)},
{"Name", it.second->character_name},
{"Version", name_for_enum(it.second->version())},
{"Language", name_for_language_code(it.second->language())},
}));
}
auto games_json = JSON::list();
for (const auto& it : this->state->id_to_lobby) {
auto l = it.second;
if (l->is_game()) {
auto game_json = JSON::dict({
{"ID", l->lobby_id},
{"Name", l->name},
{"BaseVersion", name_for_enum(l->base_version)},
{"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 : JSON(nullptr));
game_json.emplace("Rules", (ep3s && ep3s->map_and_rules) ? ep3s->map_and_rules->rules.json() : nullptr);
} else {
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", this->generate_quest_json_st(l->quest));
}
games_json.emplace_back(std::move(game_json));
}
}
return 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;
}
JSON HTTPServer::generate_all_json() const {
return 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()},
});
}
JSON HTTPServer::generate_ep3_cards_json(bool trial) const {
auto index = call_on_event_thread<shared_ptr<const Episode3::CardIndex>>(this->state->base, [&]() {
return trial ? this->state->ep3_card_index_trial : this->state->ep3_card_index;
});
return index->definitions_json();
}
JSON HTTPServer::generate_common_tables_json() const {
auto [set_v2, set_v3_v4] = call_on_event_thread<pair<shared_ptr<const CommonItemSet>, shared_ptr<const CommonItemSet>>>(this->state->base, [&]() {
return make_pair(this->state->common_item_set_v2, this->state->common_item_set_v3_v4);
});
return JSON::dict({
{"v1_v2", set_v2->json()},
{"v3_v4", set_v3_v4->json()},
});
}
JSON HTTPServer::generate_rare_tables_json() const {
auto sets = call_on_event_thread<unordered_map<string, shared_ptr<const RareItemSet>>>(this->state->base, [&]() {
return this->state->rare_item_sets;
});
JSON ret = JSON::list();
for (const auto& it : sets) {
ret.emplace_back(it.first);
}
return ret;
}
JSON HTTPServer::generate_rare_table_json(const std::string& table_name) const {
try {
auto colls = call_on_event_thread<pair<shared_ptr<const RareItemSet>, shared_ptr<const ItemNameIndex>>>(this->state->base, [&]() {
const auto& table = this->state->rare_item_sets.at(table_name);
shared_ptr<const ItemNameIndex> name_index;
if (ends_with(table_name, "-v1")) {
name_index = this->state->item_name_index_opt(Version::DC_V1);
} else if (ends_with(table_name, "-v2")) {
name_index = this->state->item_name_index_opt(Version::PC_V2);
} else if (ends_with(table_name, "-v3")) {
name_index = this->state->item_name_index_opt(Version::GC_V3);
} else if (ends_with(table_name, "-v4")) {
name_index = this->state->item_name_index_opt(Version::BB_V4);
}
return make_pair(table, name_index);
});
return colls.first->json(colls.second);
} catch (const out_of_range&) {
throw http_error(404, "table does not exist");
}
}
void HTTPServer::handle_request(struct evhttp_request* req) {
shared_ptr<const JSON> ret;
uint32_t serialize_options = 0;
uint64_t start_time = now();
string uri = evhttp_request_get_uri(req);
try {
std::unordered_multimap<std::string, std::string> 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);
}
static const string default_format_option = "false";
if (this->get_url_param(query, "format", &default_format_option) == "true") {
serialize_options |= JSON::SerializeOption::FORMAT | JSON::SerializeOption::SORT_DICT_KEYS;
}
if (this->get_url_param(query, "hex", &default_format_option) == "true") {
serialize_options |= JSON::SerializeOption::HEX_INTEGERS;
}
if (uri == "/") {
auto endpoints_json = JSON::list({
"/y/data/ep3-cards",
"/y/data/ep3-cards-trial",
"/y/data/common-tables",
"/y/data/rare-tables",
"/y/data/rare-tables/<TABLE-NAME>",
"/y/data/config",
"/y/clients",
"/y/proxy-clients",
"/y/lobbies",
"/y/server",
"/y/summary",
"/y/all",
});
ret = make_shared<JSON>(JSON::dict({{"endpoints", std::move(endpoints_json)}}));
} else if (uri == "/y/data/ep3-cards") {
ret = make_shared<JSON>(this->generate_ep3_cards_json(false));
} else if (uri == "/y/data/ep3-cards-trial") {
ret = make_shared<JSON>(this->generate_ep3_cards_json(true));
} else if (uri == "/y/data/common-tables") {
ret = make_shared<JSON>(this->generate_common_tables_json());
} else if (uri == "/y/data/rare-tables") {
ret = make_shared<JSON>(this->generate_rare_tables_json());
} else if (!strncmp(uri.c_str(), "/y/data/rare-tables/", 20)) {
ret = make_shared<JSON>(this->generate_rare_table_json(uri.substr(20)));
} else if (uri == "/y/data/config") {
ret = call_on_event_thread<shared_ptr<const JSON>>(this->state->base, [this]() { return this->state->config_json; });
} else if (uri == "/y/clients") {
ret = make_shared<JSON>(this->generate_game_server_clients_json());
} else if (uri == "/y/proxy-clients") {
ret = make_shared<JSON>(this->generate_proxy_server_clients_json());
} else if (uri == "/y/lobbies") {
ret = make_shared<JSON>(this->generate_lobbies_json());
} else if (uri == "/y/server") {
ret = make_shared<JSON>(this->generate_server_info_json());
} else if (uri == "/y/summary") {
ret = make_shared<JSON>(this->generate_summary_json());
} else if (uri == "/y/all") {
ret = make_shared<JSON>(this->generate_all_json());
} else {
throw http_error(404, "unknown action");
}
} catch (const http_error& e) {
unique_ptr<struct evbuffer, void (*)(struct evbuffer*)> 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 exception& e) {
unique_ptr<struct evbuffer, void (*)(struct evbuffer*)> 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;
}
uint64_t handler_end = now();
unique_ptr<struct evbuffer, void (*)(struct evbuffer*)> out_buffer(evbuffer_new(), evbuffer_free);
string* serialized = new string(ret->serialize(JSON::SerializeOption::ESCAPE_CONTROLS_ONLY | serialize_options));
size_t size = serialized->size();
uint64_t serialize_end = now();
auto cleanup = +[](const void*, size_t, void* s) -> void {
delete reinterpret_cast<string*>(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 = format_duration(handler_end - start_time);
string serialize_time = format_duration(serialize_end - handler_end);
string size_str = 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());
}
void HTTPServer::thread_fn() {
event_base_loop(this->base.get(), EVLOOP_NO_EXIT_ON_EMPTY);
}
+76
View File
@@ -0,0 +1,76 @@
#pragma once
#include <event2/buffer.h>
#include <event2/event.h>
#include <event2/http.h>
#include <stdlib.h>
#include <memory>
#include <string>
#include "ProxyServer.hh"
#include "ServerState.hh"
class HTTPServer {
public:
HTTPServer(std::shared_ptr<ServerState> 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();
protected:
class http_error : public std::runtime_error {
public:
http_error(int code, const std::string& what);
int code;
};
std::shared_ptr<ServerState> state;
std::shared_ptr<struct event_base> base;
std::shared_ptr<struct evhttp> http;
std::thread th;
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<int, const char*> 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<std::string, std::string> parse_url_params(const std::string& query);
static std::unordered_map<std::string, std::string> parse_url_params_unique(const std::string& query);
static const std::string& get_url_param(
const std::unordered_multimap<std::string, std::string>& params,
const std::string& key,
const std::string* _default = nullptr);
static JSON generate_quest_json_st(std::shared_ptr<const Quest> q);
static JSON generate_client_config_json_st(const Client::Config& config);
static JSON generate_license_json_st(std::shared_ptr<const License> l);
static JSON generate_game_client_json_st(std::shared_ptr<const Client> c, std::shared_ptr<const ItemNameIndex> item_name_index);
static JSON generate_proxy_client_json_st(std::shared_ptr<const ProxyServer::LinkedSession> ses);
static JSON generate_lobby_json_st(std::shared_ptr<const Lobby> l, std::shared_ptr<const ItemNameIndex> item_name_index);
JSON generate_game_server_clients_json() const;
JSON generate_proxy_server_clients_json() const;
JSON generate_server_info_json() const;
JSON generate_lobbies_json() const;
JSON generate_summary_json() const;
JSON generate_all_json() const;
JSON generate_ep3_cards_json(bool trial) const;
JSON generate_common_tables_json() const;
JSON generate_rare_tables_json() const;
JSON generate_rare_table_json(const std::string& table_name) const;
};
+100 -47
View File
@@ -135,28 +135,28 @@ IPStackSimulator::~IPStackSimulator() {
}
}
void IPStackSimulator::listen(const string& name, const string& socket_path, FrameInfo::LinkType link_type) {
void IPStackSimulator::listen(const string& name, const string& socket_path, Protocol proto) {
int fd = ::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, link_type);
this->add_socket(name, fd, proto);
}
void IPStackSimulator::listen(const string& name, const string& addr, int port, FrameInfo::LinkType link_type) {
void IPStackSimulator::listen(const string& name, const string& addr, int port, Protocol proto) {
if (port == 0) {
this->listen(name, addr, link_type);
this->listen(name, addr, proto);
} else {
int fd = ::listen(addr, port, SOMAXCONN);
string netloc_str = 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, link_type);
this->add_socket(name, fd, proto);
}
}
void IPStackSimulator::listen(const string& name, int port, FrameInfo::LinkType link_type) {
this->listen(name, "", port, link_type);
void IPStackSimulator::listen(const string& name, int port, Protocol proto) {
this->listen(name, "", port, proto);
}
void IPStackSimulator::add_socket(const string& name, int fd, FrameInfo::LinkType link_type) {
void IPStackSimulator::add_socket(const string& name, int fd, Protocol proto) {
unique_listener l(
evconnlistener_new(
this->base.get(),
@@ -166,7 +166,7 @@ void IPStackSimulator::add_socket(const string& name, int fd, FrameInfo::LinkTyp
0,
fd),
evconnlistener_free);
this->listening_sockets.emplace(piecewise_construct, forward_as_tuple(fd), forward_as_tuple(name, link_type, std::move(l)));
this->listening_sockets.emplace(piecewise_construct, forward_as_tuple(fd), forward_as_tuple(name, proto, std::move(l)));
}
uint32_t IPStackSimulator::connect_address_for_remote_address(uint32_t remote_addr) {
@@ -180,10 +180,10 @@ uint32_t IPStackSimulator::connect_address_for_remote_address(uint32_t remote_ad
}
}
IPStackSimulator::IPClient::IPClient(shared_ptr<IPStackSimulator> sim, FrameInfo::LinkType link_type, struct bufferevent* bev)
IPStackSimulator::IPClient::IPClient(shared_ptr<IPStackSimulator> sim, Protocol protocol, struct bufferevent* bev)
: sim(sim),
bev(bev, bufferevent_free),
link_type(link_type),
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) {
@@ -256,7 +256,7 @@ void IPStackSimulator::on_listen_accept(struct evconnlistener* listener,
struct bufferevent* bev = bufferevent_socket_new(this->base.get(), fd,
BEV_OPT_CLOSE_ON_FREE | BEV_OPT_DEFER_CALLBACKS);
auto c = make_shared<IPClient>(this->shared_from_this(), listening_socket->link_type, bev);
auto c = make_shared<IPClient>(this->shared_from_this(), listening_socket->protocol, bev);
this->bev_to_client.emplace(make_pair(bev, c));
bufferevent_setcb(bev, &IPStackSimulator::dispatch_on_client_input, nullptr,
@@ -300,24 +300,64 @@ void IPStackSimulator::on_client_input(struct bufferevent* bev) {
struct timeval tv = usecs_to_timeval(idle_timeout_usecs);
event_add(c->idle_timeout_event.get(), &tv);
while (evbuffer_get_length(buf) >= 2) {
uint16_t frame_size;
evbuffer_copyout(buf, &frame_size, 2);
if (evbuffer_get_length(buf) < static_cast<size_t>(frame_size + 2)) {
break; // No complete frame available; done for now
}
switch (c->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<size_t>(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());
evbuffer_drain(buf, 2);
string frame(frame_size, '\0');
evbuffer_remove(buf, frame.data(), frame.size());
try {
this->on_client_frame(c, frame);
} catch (const exception& e) {
if (ip_stack_simulator_log.warning("Failed to process frame: %s", e.what())) {
print_data(stderr, frame);
try {
this->on_client_frame(c, frame);
} catch (const exception& e) {
if (ip_stack_simulator_log.warning("Failed to process frame: %s", e.what())) {
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 {
this->on_client_frame(c, frame);
} catch (const exception& e) {
if (ip_stack_simulator_log.warning("Failed to process frame: %s", e.what())) {
print_data(stderr, frame);
}
}
}
break;
}
}
@@ -343,8 +383,8 @@ void IPStackSimulator::send_layer3_frame(shared_ptr<IPClient> c, FrameInfo::Prot
void IPStackSimulator::send_layer3_frame(shared_ptr<IPClient> c, FrameInfo::Protocol proto, const void* data, size_t size) const {
struct evbuffer* out_buf = bufferevent_get_output(c->bev.get());
switch (c->link_type) {
case FrameInfo::LinkType::ETHERNET: {
switch (c->protocol) {
case Protocol::ETHERNET_TAPSERVER: {
EthernetHeader ether;
ether.dest_mac = c->mac_addr;
ether.src_mac = this->host_mac_address_bytes;
@@ -376,7 +416,8 @@ void IPStackSimulator::send_layer3_frame(shared_ptr<IPClient> c, FrameInfo::Prot
break;
}
case FrameInfo::LinkType::HDLC: {
case Protocol::HDLC_TAPSERVER:
case Protocol::HDLC_RAW: {
HDLCHeader hdlc;
hdlc.start_sentinel1 = 0x7E;
hdlc.address = 0xFF;
@@ -413,8 +454,10 @@ void IPStackSimulator::send_layer3_frame(shared_ptr<IPClient> c, FrameInfo::Prot
print_data(stderr, w.str());
}
le_uint16_t frame_size = escaped.size();
evbuffer_add(out_buf, &frame_size, 2);
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);
@@ -428,9 +471,13 @@ void IPStackSimulator::send_layer3_frame(shared_ptr<IPClient> c, FrameInfo::Prot
}
void IPStackSimulator::on_client_frame(shared_ptr<IPClient> c, const string& frame) {
FrameInfo::LinkType link_type = (c->protocol == Protocol::ETHERNET_TAPSERVER)
? FrameInfo::LinkType::ETHERNET
: FrameInfo::LinkType::HDLC;
const string* effective_data = &frame;
string hdlc_unescaped_data;
if (c->link_type == FrameInfo::LinkType::HDLC) {
if (link_type == FrameInfo::LinkType::HDLC) {
hdlc_unescaped_data = unescape_hdlc_frame(frame);
effective_data = &hdlc_unescaped_data;
}
@@ -439,7 +486,7 @@ void IPStackSimulator::on_client_frame(shared_ptr<IPClient> c, const string& fra
}
this->log_frame(*effective_data);
FrameInfo fi(c->link_type, *effective_data);
FrameInfo fi(link_type, *effective_data);
if (ip_stack_simulator_log.should_log(LogLevel::DEBUG)) {
string fi_header = fi.header_str();
ip_stack_simulator_log.debug("Frame header: %s", fi_header.c_str());
@@ -1119,6 +1166,7 @@ void IPStackSimulator::on_client_tcp_frame(
throw runtime_error("non-SYN frame does not correspond to any open TCP connection");
}
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());
@@ -1142,6 +1190,7 @@ void IPStackSimulator::on_client_tcp_frame(
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,
ack_delta, conn->acked_server_seq);
@@ -1251,9 +1300,9 @@ void IPStackSimulator::on_client_tcp_frame(
conn_str.c_str(), conn->acked_server_seq, conn->next_client_seq, conn->bytes_received);
}
if (conn_valid) {
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);
this->send_pending_push_frame(c, *conn, true);
}
}
}
@@ -1307,19 +1356,28 @@ void IPStackSimulator::open_server_connection(shared_ptr<IPClient> c, IPClient::
}
}
void IPStackSimulator::send_pending_push_frame(shared_ptr<IPClient> c, IPClient::TCPConnection& conn) {
void IPStackSimulator::send_pending_push_frame(
shared_ptr<IPClient> 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;
}
// 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<size_t>(pending_bytes, conn.next_push_max_frame_size);
if ((c->link_type == FrameInfo::LinkType::HDLC) && (bytes_to_send > 200)) {
if (c->protocol == Protocol::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
// frame.
bytes_to_send = 200;
bytes_to_send = min<size_t>(bytes_to_send, 200);
}
ip_stack_simulator_log.debug("Sending PSH frame with seq_num %08" PRIX32 ", 0x%zX/0x%zX data bytes",
@@ -1338,8 +1396,7 @@ void IPStackSimulator::send_pending_push_frame(shared_ptr<IPClient> c, IPClient:
if (conn.resend_push_usecs > 5000000) {
conn.resend_push_usecs = 5000000;
}
conn.next_push_max_frame_size = max<size_t>(
0x100, conn.next_push_max_frame_size - 0x100);
conn.next_push_max_frame_size = max<size_t>(0x100, conn.next_push_max_frame_size - 0x100);
}
void IPStackSimulator::send_tcp_frame(
@@ -1400,15 +1457,11 @@ void IPStackSimulator::dispatch_on_resend_push(evutil_socket_t, short, void* ctx
if (!sim) {
ip_stack_simulator_log.warning("Resend push event triggered for client on deleted simulator; ignoring");
} else {
sim->on_resend_push(c, *conn);
sim->send_pending_push_frame(c, *conn, true);
}
}
}
void IPStackSimulator::on_resend_push(shared_ptr<IPClient> c, IPClient::TCPConnection& conn) {
this->send_pending_push_frame(c, conn);
}
void IPStackSimulator::dispatch_on_server_input(struct bufferevent*, void* ctx) {
auto* conn = reinterpret_cast<IPClient::TCPConnection*>(ctx);
auto c = conn->client.lock();
@@ -1435,7 +1488,7 @@ void IPStackSimulator::on_server_input(shared_ptr<IPClient> c, IPClient::TCPConn
event_add(c->idle_timeout_event.get(), &tv);
evbuffer_add_buffer(conn.pending_data.get(), buf);
this->send_pending_push_frame(c, conn);
this->send_pending_push_frame(c, conn, false);
}
void IPStackSimulator::dispatch_on_server_error(
+17 -13
View File
@@ -16,15 +16,21 @@
class IPStackSimulator : public std::enable_shared_from_this<IPStackSimulator> {
public:
enum class Protocol {
ETHERNET_TAPSERVER = 0,
HDLC_TAPSERVER,
HDLC_RAW,
};
IPStackSimulator(
std::shared_ptr<struct event_base> base,
std::shared_ptr<ServerState> state);
~IPStackSimulator();
void listen(const std::string& name, const std::string& socket_path, FrameInfo::LinkType link_type);
void listen(const std::string& name, const std::string& addr, int port, FrameInfo::LinkType link_type);
void listen(const std::string& name, int port, FrameInfo::LinkType link_type);
void add_socket(const std::string& name, int fd, FrameInfo::LinkType link_type);
void listen(const std::string& name, const std::string& socket_path, Protocol protocol);
void listen(const std::string& name, const std::string& addr, int port, Protocol protocol);
void listen(const std::string& name, int port, Protocol protocol);
void add_socket(const std::string& name, int fd, Protocol protocol);
static uint32_t connect_address_for_remote_address(uint32_t remote_addr);
@@ -41,7 +47,7 @@ private:
std::weak_ptr<IPStackSimulator> sim;
unique_bufferevent bev;
FrameInfo::LinkType link_type;
Protocol protocol;
uint32_t hdlc_escape_control_character_flags = 0xFFFFFFFF;
uint32_t hdlc_remote_magic_number = 0;
parray<uint8_t, 6> mac_addr; // Only used for LinkType::ETHERNET
@@ -80,7 +86,7 @@ private:
unique_event idle_timeout_event;
IPClient(std::shared_ptr<IPStackSimulator> sim, FrameInfo::LinkType link_type, struct bufferevent* bev);
IPClient(std::shared_ptr<IPStackSimulator> sim, Protocol protocol, struct bufferevent* bev);
static void dispatch_on_idle_timeout(evutil_socket_t fd, short events, void* ctx);
void on_idle_timeout();
@@ -88,12 +94,12 @@ private:
struct ListeningSocket {
std::string name;
FrameInfo::LinkType link_type;
Protocol protocol;
unique_listener listener;
ListeningSocket(const std::string& name, FrameInfo::LinkType link_type, unique_listener&& l)
ListeningSocket(const std::string& name, Protocol protocol, unique_listener&& l)
: name(name),
link_type(link_type),
protocol(protocol),
listener(std::move(l)) {}
};
@@ -138,15 +144,13 @@ private:
void on_client_udp_frame(std::shared_ptr<IPClient> c, const FrameInfo& fi);
void on_client_tcp_frame(std::shared_ptr<IPClient> c, const FrameInfo& fi);
static void dispatch_on_resend_push(evutil_socket_t fd, short events, void* ctx);
void on_resend_push(std::shared_ptr<IPClient> c, IPClient::TCPConnection& conn);
static void dispatch_on_server_input(struct bufferevent* bev, void* ctx);
void on_server_input(std::shared_ptr<IPClient> c, IPClient::TCPConnection& conn);
static void dispatch_on_server_error(struct bufferevent* bev, short events, void* ctx);
void on_server_error(std::shared_ptr<IPClient> c, IPClient::TCPConnection& conn, short events);
void send_pending_push_frame(std::shared_ptr<IPClient> c, IPClient::TCPConnection& conn);
static void dispatch_on_resend_push(evutil_socket_t, short, void* ctx);
void send_pending_push_frame(std::shared_ptr<IPClient> c, IPClient::TCPConnection& conn, bool always_send);
void send_tcp_frame(
std::shared_ptr<IPClient> c,
IPClient::TCPConnection& conn,
+118 -87
View File
@@ -18,15 +18,16 @@ ItemCreator::ItemCreator(
shared_ptr<const WeaponRandomSet> weapon_random_set,
shared_ptr<const TekkerAdjustmentSet> tekker_adjustment_set,
shared_ptr<const ItemParameterTable> item_parameter_table,
Version version,
std::shared_ptr<const ItemData::StackLimits> stack_limits,
Episode episode,
GameMode mode,
uint8_t difficulty,
uint8_t section_id,
uint32_t random_seed,
std::shared_ptr<PSOLFGEncryption> opt_rand_crypt,
shared_ptr<const BattleRules> restrictions)
: log(string_printf("[ItemCreator:%s/%s/%s/%c/%hhu] ", name_for_enum(version), abbreviation_for_episode(episode), abbreviation_for_mode(mode), abbreviation_for_difficulty(difficulty), section_id), lobby_log.min_level),
version(version),
: log(string_printf("[ItemCreator:%s/%s/%s/%c/%hhu] ", 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),
mode(mode),
difficulty(difficulty),
@@ -37,18 +38,27 @@ ItemCreator::ItemCreator(
weapon_random_set(weapon_random_set),
tekker_adjustment_set(tekker_adjustment_set),
item_parameter_table(item_parameter_table),
common_item_set(common_item_set),
pt(common_item_set->get_table(this->episode, this->mode, this->difficulty, this->section_id)),
restrictions(restrictions),
random_crypt(random_seed) {
opt_rand_crypt(opt_rand_crypt ? make_shared<PSOV2Encryption>(opt_rand_crypt->seed()) : nullptr) {
this->generate_unit_stars_tables();
}
void ItemCreator::set_random_state(uint32_t seed, uint32_t absolute_offset) {
if ((this->random_crypt.seed() != seed) || (this->random_crypt.absolute_offset() > absolute_offset)) {
this->random_crypt = PSOV2Encryption(seed);
}
while (this->random_crypt.absolute_offset() < absolute_offset) {
this->random_crypt.next();
void ItemCreator::set_random_crypt(shared_ptr<PSOLFGEncryption> 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 = string_printf("[ItemCreator:%s/%s/%s/%c/%hhu] ",
name_for_enum(stack_limits->version),
abbreviation_for_episode(episode),
abbreviation_for_mode(mode),
abbreviation_for_difficulty(difficulty),
this->section_id);
this->pt = common_item_set->get_table(this->episode, this->mode, this->difficulty, this->section_id);
}
}
@@ -122,16 +132,29 @@ uint8_t ItemCreator::normalize_area_number(uint8_t area) const {
}
ItemCreator::DropResult ItemCreator::on_box_item_drop(uint8_t area) {
return this->on_box_item_drop_with_area_norm(this->normalize_area_number(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());
return DropResult();
}
}
ItemCreator::DropResult ItemCreator::on_monster_item_drop(uint32_t enemy_type, uint8_t area) {
return this->on_monster_item_drop_with_area_norm(enemy_type, this->normalize_area_number(area));
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());
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; random state: %08" PRIX32 " %08" PRIX32,
area_norm, this->random_crypt.seed(), this->random_crypt.absolute_offset());
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());
}
DropResult res;
res.item = this->check_rare_specs_and_create_rare_box_item(area_norm);
if (!res.item.empty()) {
@@ -178,7 +201,11 @@ ItemCreator::DropResult ItemCreator::on_monster_item_drop_with_area_norm(uint32_
this->log.warning("Invalid enemy type: %" PRIX32, enemy_type);
return DropResult();
}
this->log.info("Enemy type: %" PRIX32 "; random state: %08" PRIX32 " %08" PRIX32, enemy_type, this->random_crypt.seed(), this->random_crypt.absolute_offset());
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());
}
uint8_t type_drop_prob = this->pt->enemy_type_drop_probs.at(enemy_type);
uint8_t drop_sample = this->rand_int(100);
@@ -259,23 +286,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()) {
this->log.info("Box spec %08" PRIX32 " produced item %02hhX%02hhX%02hhX",
spec.probability, spec.item_code[0], spec.item_code[1], spec.item_code[2]);
if (this->log.should_log(LogLevel::INFO)) {
auto hex = spec.data.hex();
this->log.info("Box spec %08" PRIX32 " produced item %s", spec.probability, hex.c_str());
}
break;
}
this->log.info("Box spec %08" PRIX32 " did not produce item %02hhX%02hhX%02hhX",
spec.probability, spec.item_code[0], spec.item_code[1], spec.item_code[2]);
if (this->log.should_log(LogLevel::INFO)) {
auto hex = spec.data.hex();
this->log.info("Box spec %08" PRIX32 " did not produce item %s", spec.probability, hex.c_str());
}
}
return item;
}
uint32_t ItemCreator::rand_int(uint64_t max) {
return this->random_crypt.next() % max;
return random_from_optional_crypt(this->opt_rand_crypt) % max;
}
float ItemCreator::rand_float_0_1_from_crypt() {
// This lacks some precision, but matches the original implementation.
return (static_cast<double>(this->random_crypt.next() >> 16) / 65536.0);
return (static_cast<double>(random_from_optional_crypt(this->opt_rand_crypt) >> 16) / 65536.0);
}
template <size_t NumRanges>
@@ -316,12 +347,16 @@ 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()) {
this->log.info("Enemy spec %08" PRIX32 " produced item %02hhX%02hhX%02hhX",
spec.probability, spec.item_code[0], spec.item_code[1], spec.item_code[2]);
if (this->log.should_log(LogLevel::INFO)) {
auto hex = spec.data.hex();
this->log.info("Enemy spec %08" PRIX32 " produced item %s", spec.probability, hex.c_str());
}
break;
}
this->log.info("Enemy spec %08" PRIX32 " did not produce item %02hhX%02hhX%02hhX",
spec.probability, spec.item_code[0], spec.item_code[1], spec.item_code[2]);
if (this->log.should_log(LogLevel::INFO)) {
auto hex = spec.data.hex();
this->log.info("Enemy spec %08" PRIX32 " did not produce item %s", spec.probability, hex.c_str());
}
}
}
return item;
@@ -338,37 +373,36 @@ ItemData ItemCreator::check_rate_and_create_rare_item(const RareItemSet::Expande
return ItemData();
}
ItemData item;
item.data1[0] = drop.item_code[0];
item.data1[1] = drop.item_code[1];
item.data1[2] = drop.item_code[2];
switch (item.data1[0]) {
case 0:
if (this->pt->has_rare_bonus_value_prob_table) {
this->generate_rare_weapon_bonuses(item, this->rand_int(10));
} else {
this->generate_common_weapon_bonuses(item, area_norm);
}
this->set_item_unidentified_flag_if_not_challenge(item);
break;
case 1:
this->generate_common_armor_slots_and_bonuses(item);
break;
case 2:
this->generate_common_mag_variances(item);
break;
case 3:
this->clear_tool_item_if_invalid(item);
this->set_tool_item_amount_to_1(item);
break;
case 4:
break;
default:
throw logic_error("invalid item class");
ItemData item = drop.data;
if (item.can_be_encoded_in_rel_rare_table()) {
switch (item.data1[0]) {
case 0:
if (this->pt->has_rare_bonus_value_prob_table) {
this->generate_rare_weapon_bonuses(item, this->rand_int(10));
} else {
this->generate_common_weapon_bonuses(item, area_norm);
}
this->set_item_unidentified_flag_if_not_challenge(item);
break;
case 1:
this->generate_common_armor_slots_and_bonuses(item);
break;
case 2:
this->generate_common_mag_variances(item);
break;
case 3:
this->clear_tool_item_if_invalid(item);
this->set_tool_item_amount_to_1(item);
break;
case 4:
break;
default:
throw logic_error("invalid item class");
}
this->set_item_kill_count_if_unsealable(item);
}
this->clear_item_if_restricted(item);
this->set_item_kill_count_if_unsealable(item);
return item;
}
@@ -448,23 +482,21 @@ void ItemCreator::set_item_unidentified_flag_if_not_challenge(ItemData& item) co
if (item.data1[0] != 0x00) {
return;
}
// On V3, all rare weapons and weapons with specials are untekked when
// created; on V2, only rares that are not in the standard item classes are
// untekked when created.
if (this->is_v3()) {
if (this->item_parameter_table->is_item_rare(item) || (item.data1[4] != 0)) {
item.data1[4] |= 0x80;
}
} else {
if (this->item_parameter_table->is_item_rare(item) ? (item.data1[1] > 0x0C) : (item.data1[4] != 0)) {
item.data1[4] |= 0x80;
}
// On V1, V3, and V4, all rare weapons and weapons with specials are untekked
// when created; on V2, only rares that are not in the standard item classes
// are untekked when created.
bool is_rare = this->item_parameter_table->is_item_rare(item);
bool use_v2_logic = is_v2(this->logic_version) && (this->logic_version != Version::GC_NTE);
if (use_v2_logic
? (is_rare ? (item.data1[1] > 0x0C) : (item.data1[4] != 0))
: (is_rare || (item.data1[4] != 0))) {
item.data1[4] |= 0x80;
}
}
void ItemCreator::set_tool_item_amount_to_1(ItemData& item) const {
if (item.data1[0] == 0x03) {
item.set_tool_item_amount(this->version, 1);
item.set_tool_item_amount(*this->stack_limits, 1);
}
}
@@ -644,7 +676,7 @@ void ItemCreator::generate_common_tool_variances(uint32_t area_norm, ItemData& i
item.clear();
uint8_t tool_class = this->get_rand_from_weighted_tables_2d_vertical(this->pt->tool_class_prob_table, area_norm);
if (this->is_v3() && (tool_class == 0x1A)) {
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);
@@ -692,12 +724,12 @@ void ItemCreator::generate_common_mag_variances(ItemData& item) {
// The original code (on PSO GC) assigns the mag color as 0x0E. We assign
// a random color instead.
if (is_pre_v1(this->version)) {
if (is_pre_v1(this->logic_version)) {
item.data2[3] = 0x00;
} else if (is_v1_or_v2(this->version)) {
item.data2[3] = this->random_crypt.next() % 0x0E;
} else if (is_v1_or_v2(this->logic_version)) {
item.data2[3] = random_from_optional_crypt(this->opt_rand_crypt) % 0x0E;
} else {
item.data2[3] = this->random_crypt.next() % 0x12;
item.data2[3] = random_from_optional_crypt(this->opt_rand_crypt) % 0x12;
}
}
}
@@ -810,7 +842,7 @@ void ItemCreator::generate_unit_stars_tables() {
size_t star_base_index;
uint8_t num_units;
switch (this->version) {
switch (this->logic_version) {
case Version::PC_PATCH:
case Version::BB_PATCH:
case Version::GC_EP3_NTE:
@@ -1007,8 +1039,7 @@ bool ItemCreator::shop_does_not_contain_duplicate_item_by_data1_0_1_2(
return true;
}
void ItemCreator::generate_armor_shop_armors(
vector<ItemData>& shop, size_t player_level) {
void ItemCreator::generate_armor_shop_armors(vector<ItemData>& shop, size_t player_level) {
size_t num_items;
if (player_level < 11) {
num_items = 4;
@@ -1028,7 +1059,7 @@ void ItemCreator::generate_armor_shop_armors(
pt.push(src_table.first[z].value);
}
}
pt.shuffle(this->random_crypt);
pt.shuffle(this->opt_rand_crypt);
for (size_t items_generated = 0; items_generated < num_items;) {
ItemData item;
@@ -1072,7 +1103,7 @@ void ItemCreator::generate_armor_shop_shields(vector<ItemData>& shop, size_t pla
pt.push(src_table.first[z].value);
}
}
pt.shuffle(this->random_crypt);
pt.shuffle(this->opt_rand_crypt);
for (size_t items_generated = 0; items_generated < num_items;) {
ItemData item;
@@ -1115,7 +1146,7 @@ void ItemCreator::generate_armor_shop_units(vector<ItemData>& shop, size_t playe
pt.push(src_table.first[z].value);
}
}
pt.shuffle(this->random_crypt);
pt.shuffle(this->opt_rand_crypt);
for (size_t items_generated = 0; items_generated < num_items;) {
ItemData item;
@@ -1218,7 +1249,7 @@ void ItemCreator::generate_rare_tool_shop_recovery_items(
pt.push(e.value);
}
}
pt.shuffle(this->random_crypt);
pt.shuffle(this->opt_rand_crypt);
size_t effective_num_items = num_items;
size_t items_generated = 0;
@@ -1261,7 +1292,7 @@ void ItemCreator::generate_tool_shop_tech_disks(vector<ItemData>& shop, size_t p
pt.push(e.value);
}
}
pt.shuffle(this->random_crypt);
pt.shuffle(this->opt_rand_crypt);
static const array<uint8_t, 0x13> tech_num_map = {
0x00, 0x03, 0x06, 0x0F, 0x10, 0x0D, 0x0A, 0x0B, 0x0C, 0x01, 0x04, 0x07,
@@ -1362,7 +1393,7 @@ vector<ItemData> ItemCreator::generate_weapon_shop_contents(size_t player_level)
pt.push(e.value);
}
}
pt.shuffle(this->random_crypt);
pt.shuffle(this->opt_rand_crypt);
vector<ItemData> shop;
while (shop.size() < num_items) {
@@ -1562,7 +1593,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->random_crypt)) {
switch (pt.sample(this->opt_rand_crypt)) {
case 0:
item.data1[4] = 0;
break;
@@ -1614,7 +1645,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->random_crypt);
item.data1[6] = pt.sample(this->opt_rand_crypt);
if (item.data1[6] == 0) {
item.data1[7] = 0;
@@ -1655,7 +1686,7 @@ void ItemCreator::generate_weapon_shop_item_bonus2(ItemData& item, size_t player
pt.push(e.value);
}
}
pt.shuffle(this->random_crypt);
pt.shuffle(this->opt_rand_crypt);
do {
item.data1[8] = pt.pop();
@@ -1713,7 +1744,7 @@ ItemData ItemCreator::base_item_for_specialized_box(uint32_t def0, uint32_t def1
if (item.data1[1] == 0x02) {
item.data1[4] = def0 & 0xFF;
}
item.set_tool_item_amount(this->version, 1);
item.set_tool_item_amount(*this->stack_limits, 1);
break;
case 0x04:
item.data2d = ((def1 >> 0x10) & 0xFFFF) * 10;
@@ -1741,7 +1772,7 @@ ssize_t ItemCreator::apply_tekker_deltas(ItemData& item, uint8_t section_id) {
// 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->random_crypt);
uint8_t delta_index = prob_table.sample(this->opt_rand_crypt);
int8_t delta = delta_table.at(delta_index);
this->log.info("(Special) Delta index %hhu, delta %hhd", delta_index, delta);
// Note: The original code checks specifically for -1 and +1 here, but the
@@ -1777,7 +1808,7 @@ ssize_t ItemCreator::apply_tekker_deltas(ItemData& item, uint8_t section_id) {
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->random_crypt);
uint8_t delta_index = prob_table.sample(this->opt_rand_crypt);
int8_t delta = delta_table.at(delta_index);
this->log.info("(Grind) Delta index %hhu, delta %hhd", delta_index, delta);
int16_t new_grind = static_cast<int16_t>(item.data1[3]) + static_cast<int16_t>(delta);
@@ -1793,7 +1824,7 @@ 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->random_crypt);
uint8_t delta_index = prob_table.sample(this->opt_rand_crypt);
int8_t delta = delta_table.at(delta_index);
this->log.info("(Bonuses) Delta index %hhu, delta %hhd", delta_index, delta);
// Note: The original code doesn't check if there's actually a bonus in each
+11 -9
View File
@@ -19,16 +19,16 @@ public:
std::shared_ptr<const WeaponRandomSet> weapon_random_set,
std::shared_ptr<const TekkerAdjustmentSet> tekker_adjustment_set,
std::shared_ptr<const ItemParameterTable> item_parameter_table,
Version version,
std::shared_ptr<const ItemData::StackLimits> stack_limits,
Episode episode,
GameMode mode,
uint8_t difficulty,
uint8_t section_id,
uint32_t random_seed,
std::shared_ptr<PSOLFGEncryption> opt_rand_crypt,
std::shared_ptr<const BattleRules> restrictions = nullptr);
~ItemCreator() = default;
void set_random_state(uint32_t seed, uint32_t absolute_offset);
void set_random_crypt(std::shared_ptr<PSOLFGEncryption> new_random_crypt);
struct DropResult {
ItemData item;
@@ -52,10 +52,15 @@ public:
inline void set_restrictions(std::shared_ptr<const BattleRules> restrictions) {
this->restrictions = restrictions;
}
inline uint8_t get_section_id() const {
return this->section_id;
}
void set_section_id(uint8_t new_section_id);
private:
PrefixedLogger log;
Version version;
Version logic_version;
std::shared_ptr<const ItemData::StackLimits> stack_limits;
Episode episode;
GameMode mode;
uint8_t difficulty;
@@ -66,6 +71,7 @@ private:
std::shared_ptr<const WeaponRandomSet> weapon_random_set;
std::shared_ptr<const TekkerAdjustmentSet> tekker_adjustment_set;
std::shared_ptr<const ItemParameterTable> item_parameter_table;
std::shared_ptr<const CommonItemSet> common_item_set;
std::shared_ptr<const CommonItemSet::Table> pt;
std::shared_ptr<const BattleRules> restrictions;
@@ -77,11 +83,7 @@ private:
// Note: The original implementation uses 17 different random states for some
// reason. We forego that and use only one for simplicity.
PSOV2Encryption random_crypt;
inline bool is_v3() const {
return !is_v1_or_v2(this->version);
}
std::shared_ptr<PSOLFGEncryption> opt_rand_crypt;
bool are_rare_drops_allowed() const;
uint8_t normalize_area_number(uint8_t area) const;
+88 -38
View File
@@ -8,6 +8,39 @@
using namespace std;
const vector<uint8_t> ItemData::StackLimits::DEFAULT_TOOL_LIMITS_DC_11_2000(
{10});
const vector<uint8_t> ItemData::StackLimits::DEFAULT_TOOL_LIMITS_V1_V2(
{10, 10, 1, 10, 10, 10, 10, 10, 10, 1});
const vector<uint8_t> ItemData::StackLimits::DEFAULT_TOOL_LIMITS_V3_V4(
{10, 10, 1, 10, 10, 10, 10, 10, 10, 1, 1, 1, 1, 1, 1, 1, 99, 1});
ItemData::StackLimits::StackLimits(
Version version, const vector<uint8_t>& max_tool_stack_sizes_by_data1_1, uint32_t max_meseta_stack_size)
: version(version),
max_tool_stack_sizes_by_data1_1(max_tool_stack_sizes_by_data1_1),
max_meseta_stack_size(max_meseta_stack_size) {}
ItemData::StackLimits::StackLimits(Version version, const JSON& json)
: version(version) {
this->max_tool_stack_sizes_by_data1_1.clear();
for (const auto& limit_json : json.at("ToolLimits").as_list()) {
this->max_tool_stack_sizes_by_data1_1.emplace_back(limit_json->as_int());
}
this->max_meseta_stack_size = json.at("MesetaLimit").as_int();
}
uint8_t ItemData::StackLimits::get(uint8_t data1_0, uint8_t data1_1) const {
if (data1_0 == 4) {
return this->max_meseta_stack_size;
}
if (data1_0 == 3) {
const auto& vec = this->max_tool_stack_sizes_by_data1_1;
return vec.at(min<size_t>(data1_1, vec.size() - 1));
}
return 1;
}
ItemData::ItemData() {
this->clear();
}
@@ -85,7 +118,7 @@ uint32_t ItemData::primary_identifier() const {
}
}
bool ItemData::is_wrapped(Version version) const {
bool ItemData::is_wrapped(const StackLimits& limits) const {
switch (this->data1[0]) {
case 0:
case 1:
@@ -93,7 +126,7 @@ bool ItemData::is_wrapped(Version version) const {
case 2:
return this->data2[2] & 0x40;
case 3:
return !this->is_stackable(version) && (this->data1[3] & 0x40);
return !this->is_stackable(limits) && (this->data1[3] & 0x40);
case 4:
return false;
default:
@@ -101,7 +134,7 @@ bool ItemData::is_wrapped(Version version) const {
}
}
void ItemData::wrap(Version version) {
void ItemData::wrap(const StackLimits& limits) {
switch (this->data1[0]) {
case 0:
case 1:
@@ -111,7 +144,7 @@ void ItemData::wrap(Version version) {
this->data2[2] |= 0x40;
break;
case 3:
if (!this->is_stackable(version)) {
if (!this->is_stackable(limits)) {
this->data1[3] |= 0x40;
}
break;
@@ -122,7 +155,7 @@ void ItemData::wrap(Version version) {
}
}
void ItemData::unwrap(Version version) {
void ItemData::unwrap(const StackLimits& limits) {
switch (this->data1[0]) {
case 0:
case 1:
@@ -132,7 +165,7 @@ void ItemData::unwrap(Version version) {
this->data2[2] &= 0xBF;
break;
case 3:
if (!this->is_stackable(version)) {
if (!this->is_stackable(limits)) {
this->data1[3] &= 0xBF;
}
break;
@@ -143,23 +176,23 @@ void ItemData::unwrap(Version version) {
}
}
bool ItemData::is_stackable(Version version) const {
return this->max_stack_size(version) > 1;
bool ItemData::is_stackable(const StackLimits& limits) const {
return this->max_stack_size(limits) > 1;
}
size_t ItemData::stack_size(Version version) const {
if (max_stack_size_for_item(version, this->data1[0], this->data1[1]) > 1) {
size_t ItemData::stack_size(const StackLimits& limits) const {
if (this->max_stack_size(limits) > 1) {
return this->data1[5];
}
return 1;
}
size_t ItemData::max_stack_size(Version version) const {
return max_stack_size_for_item(version, this->data1[0], this->data1[1]);
size_t ItemData::max_stack_size(const StackLimits& limits) const {
return limits.get(this->data1[0], this->data1[1]);
}
void ItemData::enforce_min_stack_size(Version version) {
if (this->stack_size(version) == 0) {
void ItemData::enforce_min_stack_size(const StackLimits& limits) {
if (this->stack_size(limits) == 0) {
this->data1[5] = 1;
}
}
@@ -306,7 +339,7 @@ void ItemData::add_mag_photon_blast(uint8_t pb_num) {
void ItemData::decode_for_version(Version from_version) {
uint8_t encoded_v2_data = this->get_encoded_v2_data();
bool should_decode_v2_data = (is_v1(from_version) || is_v2(from_version)) &&
bool should_decode_v2_data = (is_v1(from_version) || is_v2(from_version)) && (from_version != Version::GC_NTE) &&
(encoded_v2_data != 0x00) && this->has_encoded_v2_data();
switch (this->data1[0]) {
@@ -330,13 +363,9 @@ void ItemData::decode_for_version(Version from_version) {
this->data1[1] = encoded_v2_data + 0x2B;
}
if (is_big_endian(from_version)) {
// PSO GC erroneously byteswaps the data2d field, even though it's actually
// just four individual bytes, so we correct for that here.
this->data2d = bswap32(this->data2d);
} else if (is_v1(from_version) || is_v2(from_version)) {
// PSO PC encodes mags in a tediously annoying manner. The first four bytes are the same, but then...
if (is_v1(from_version) || is_v2(from_version)) {
// PSO PC and GC NTE encode mags in a tediously annoying manner. The
// first four bytes are the same, but then...
// V2: pHHHHHHHHHHHHHHc pIIIIIIIIIIIIIIc JJJJJJJJJJJJJJJc KKKKKKKKKKKKKKKc QQQQQQQQ QQQQQQQQ YYYYYYYY pYYYYYYY
// V3: HHHHHHHHHHHHHHHH IIIIIIIIIIIIIIII JJJJJJJJJJJJJJJJ KKKKKKKKKKKKKKKK YYYYYYYY QQQQQQQQ PPPPPPPP CCCCCCCC
// c = color in V2 (4 bits; low bit first)
@@ -352,10 +381,18 @@ void ItemData::decode_for_version(Version from_version) {
this->data2[0] = this->data2w[1] & 0x7FFF; // Synchro
this->data2[2] = ((this->data2[3] >> 7) & 1) | ((this->data1w[2] >> 14) & 2) | ((this->data1w[3] >> 13) & 4); // PB flags
this->data2[3] = (this->data1w[2] & 1) | ((this->data1w[3] & 1) << 1) | ((this->data1w[4] & 1) << 2) | ((this->data1w[5] & 1) << 3); // Color
// 01000080
this->data1w[2] &= 0x7FFE;
this->data1w[3] &= 0x7FFE;
this->data1w[4] &= 0xFFFE;
this->data1w[5] &= 0xFFFE;
} else if (is_big_endian(from_version)) {
// PSO GC (but not GC NTE, which uses the above logic) byteswaps the
// data2d field, since internally it's actually a uint32_t. We treat it
// as individual bytes instead, so we correct for the client's
// byteswapping here.
this->data2d = bswap32(this->data2d);
}
break;
@@ -386,7 +423,7 @@ void ItemData::decode_for_version(Version from_version) {
}
void ItemData::encode_for_version(Version to_version, shared_ptr<const ItemParameterTable> item_parameter_table) {
bool should_encode_v2_data = (is_v1(to_version) || is_v2(to_version)) && !this->has_encoded_v2_data();
bool should_encode_v2_data = (is_v1(to_version) || is_v2(to_version)) && (to_version != Version::GC_NTE) && !this->has_encoded_v2_data();
switch (this->data1[0]) {
case 0x00:
@@ -425,9 +462,7 @@ void ItemData::encode_for_version(Version to_version, shared_ptr<const ItemParam
// This logic is the inverse of the corresponding logic in
// decode_for_version; see that function for a description of what's
// going on here.
if (is_big_endian(to_version)) {
this->data2d = bswap32(this->data2d);
} else if (is_v1(to_version) || is_v2(to_version)) {
if (is_v1(to_version) || is_v2(to_version)) {
this->data1w[2] = (this->data1w[2] & 0x7FFE) | ((this->data2[2] << 14) & 0x8000) | (this->data2[3] & 1);
this->data1w[3] = (this->data1w[3] & 0x7FFE) | ((this->data2[2] << 13) & 0x8000) | ((this->data2[3] >> 1) & 1);
this->data1w[4] = (this->data1w[4] & 0xFFFE) | ((this->data2[3] >> 2) & 1);
@@ -435,6 +470,8 @@ void ItemData::encode_for_version(Version to_version, shared_ptr<const ItemParam
// Order is important; data2w[0] must not be written before data2[0] is read
this->data2w[1] = this->data2[0] | ((this->data2[2] << 15) & 0x8000);
this->data2w[0] = this->data2[1];
} else if (is_big_endian(to_version)) {
this->data2d = bswap32(this->data2d);
}
break;
@@ -502,12 +539,12 @@ void ItemData::set_sealed_item_kill_count(uint16_t v) {
}
}
uint8_t ItemData::get_tool_item_amount(Version version) const {
return this->is_stackable(version) ? this->data1[5] : 1;
uint8_t ItemData::get_tool_item_amount(const StackLimits& limits) const {
return this->is_stackable(limits) ? this->data1[5] : 1;
}
void ItemData::set_tool_item_amount(Version version, uint8_t amount) {
if (this->is_stackable(version)) {
void ItemData::set_tool_item_amount(const StackLimits& limits, uint8_t amount) {
if (this->is_stackable(limits)) {
this->data1[5] = amount;
} else if (this->data1[0] == 0x03) {
this->data1[5] = 0x00;
@@ -628,6 +665,10 @@ bool ItemData::can_be_equipped_in_slot(EquipSlot slot) const {
}
}
bool ItemData::can_be_encoded_in_rel_rare_table() const {
return !(this->data1[3] || this->data1d[1] || this->data1d[2] || this->data2d);
}
bool ItemData::compare_for_sort(const ItemData& a, const ItemData& b) {
for (size_t z = 0; z < 12; z++) {
if (a.data1[z] < b.data1[z]) {
@@ -667,7 +708,7 @@ ItemData ItemData::from_data(const string& data) {
return ret;
}
ItemData ItemData::from_primary_identifier(Version version, uint32_t primary_identifier) {
ItemData ItemData::from_primary_identifier(const StackLimits& limits, uint32_t primary_identifier) {
ItemData ret;
if (primary_identifier > 0x04000000) {
throw runtime_error("invalid item class");
@@ -680,15 +721,24 @@ ItemData ItemData::from_primary_identifier(Version version, uint32_t primary_ide
} else {
ret.data1[2] = (primary_identifier >> 8) & 0xFF;
}
ret.set_tool_item_amount(version, 1);
ret.set_tool_item_amount(limits, 1);
return ret;
}
string ItemData::hex() const {
return string_printf("%02hhX%02hhX%02hhX%02hhX %02hhX%02hhX%02hhX%02hhX %02hhX%02hhX%02hhX%02hhX (%08" PRIX32 ") %02hhX%02hhX%02hhX%02hhX",
this->data1[0], this->data1[1], this->data1[2], this->data1[3],
this->data1[4], this->data1[5], this->data1[6], this->data1[7],
this->data1[8], this->data1[9], this->data1[10], this->data1[11],
this->id.load(),
this->data2[0], this->data2[1], this->data2[2], this->data2[3]);
return 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());
}
string ItemData::short_hex() const {
auto ret = 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());
size_t offset = ret.find_last_not_of('0');
if (offset != string::npos) {
offset += (offset & 1) ? 1 : 2;
if (offset < ret.size()) {
ret.resize(offset);
}
}
return ret;
}
+34 -11
View File
@@ -1,6 +1,7 @@
#pragma once
#include <phosg/Encoding.hh>
#include <phosg/JSON.hh>
#include <string>
#include "Text.hh"
@@ -54,7 +55,26 @@ struct ItemMagStats {
}
};
struct ItemData { // 0x14 bytes
struct ItemData {
struct StackLimits {
Version version;
std::vector<uint8_t> max_tool_stack_sizes_by_data1_1;
uint32_t max_meseta_stack_size;
StackLimits(Version version, const std::vector<uint8_t>& max_tool_stack_sizes_by_data1_1, uint32_t max_meseta_stack_size);
StackLimits(Version version, const JSON& json);
StackLimits(const StackLimits& other) = default;
StackLimits(StackLimits&& other) = default;
StackLimits& operator=(const StackLimits& other) = default;
StackLimits& operator=(StackLimits&& other) = default;
uint8_t get(uint8_t data1_0, uint8_t data1_1) const;
static const std::vector<uint8_t> DEFAULT_TOOL_LIMITS_DC_11_2000;
static const std::vector<uint8_t> DEFAULT_TOOL_LIMITS_V1_V2;
static const std::vector<uint8_t> DEFAULT_TOOL_LIMITS_V3_V4;
};
// QUICK ITEM FORMAT REFERENCE
// data1/0 data1/4 data1/8 data2
// Weapon: 00ZZZZGG SS00AABB AABBAABB 00000000
@@ -124,18 +144,19 @@ struct ItemData { // 0x14 bytes
void clear();
static ItemData from_data(const std::string& data);
static ItemData from_primary_identifier(Version version, uint32_t primary_identifier);
static ItemData from_primary_identifier(const StackLimits& limits, uint32_t primary_identifier);
std::string hex() const;
std::string short_hex() const;
uint32_t primary_identifier() const;
bool is_wrapped(Version version) const;
void wrap(Version version);
void unwrap(Version version);
bool is_wrapped(const StackLimits& limits) const;
void wrap(const StackLimits& limits);
void unwrap(const StackLimits& limits);
bool is_stackable(Version version) const;
size_t stack_size(Version version) const;
size_t max_stack_size(Version version) const;
void enforce_min_stack_size(Version version);
bool is_stackable(const StackLimits& limits) const;
size_t stack_size(const StackLimits& limits) const;
size_t max_stack_size(const StackLimits& limits) const;
void enforce_min_stack_size(const StackLimits& limits);
static bool is_common_consumable(uint32_t primary_identifier);
bool is_common_consumable() const;
@@ -154,8 +175,8 @@ struct ItemData { // 0x14 bytes
uint16_t get_sealed_item_kill_count() const;
void set_sealed_item_kill_count(uint16_t v);
uint8_t get_tool_item_amount(Version version) const;
void set_tool_item_amount(Version version, uint8_t amount);
uint8_t get_tool_item_amount(const StackLimits& limits) const;
void set_tool_item_amount(const StackLimits& limits, uint8_t amount);
int16_t get_armor_or_shield_defense_bonus() const;
void set_armor_or_shield_defense_bonus(int16_t bonus);
int16_t get_common_armor_evasion_bonus() const;
@@ -169,6 +190,8 @@ struct ItemData { // 0x14 bytes
EquipSlot default_equip_slot() const;
bool can_be_equipped_in_slot(EquipSlot slot) const;
bool can_be_encoded_in_rel_rare_table() const;
bool empty() const;
static bool compare_for_sort(const ItemData& a, const ItemData& b);
+27 -16
View File
@@ -5,16 +5,16 @@
using namespace std;
ItemNameIndex::ItemNameIndex(
Version version,
std::shared_ptr<const ItemParameterTable> item_parameter_table,
std::shared_ptr<const ItemData::StackLimits> limits,
const std::vector<std::string>& name_coll)
: version(version),
item_parameter_table(item_parameter_table) {
: item_parameter_table(item_parameter_table),
limits(limits) {
for (uint32_t primary_identifier : item_parameter_table->compute_all_valid_primary_identifiers()) {
const string* name = nullptr;
try {
ItemData item = ItemData::from_primary_identifier(this->version, primary_identifier);
ItemData item = ItemData::from_primary_identifier(*this->limits, primary_identifier);
name = &name_coll.at(item_parameter_table->get_item_id(item));
} catch (const out_of_range&) {
}
@@ -97,9 +97,7 @@ const array<const char*, 0x11> name_for_s_rank_special = {
"King\'s",
};
std::string ItemNameIndex::describe_item(
const ItemData& item,
bool include_color_escapes) const {
std::string ItemNameIndex::describe_item(const ItemData& item, bool include_color_escapes) const {
if (item.data1[0] == 0x04) {
return string_printf("%s%" PRIu32 " Meseta", include_color_escapes ? "$C7" : "", item.data2d.load());
}
@@ -107,8 +105,9 @@ std::string ItemNameIndex::describe_item(
vector<string> ret_tokens;
// For weapons, specials appear before the weapon name
bool is_unidentified = false;
if ((item.data1[0] == 0x00) && (item.data1[4] != 0x00) && !item.is_s_rank_weapon()) {
bool is_unidentified = item.data1[4] & 0x80;
is_unidentified = item.data1[4] & 0x80;
bool is_present = item.data1[4] & 0x40;
uint8_t special_id = item.data1[4] & 0x3F;
if (is_present) {
@@ -138,7 +137,7 @@ std::string ItemNameIndex::describe_item(
// flags in a different location.
if (((item.data1[1] == 0x01) && (item.data1[4] & 0x40)) ||
((item.data1[0] == 0x02) && (item.data2[2] & 0x40)) ||
((item.data1[0] == 0x03) && !item.is_stackable(this->version) && (item.data1[3] & 0x40))) {
((item.data1[0] == 0x03) && !item.is_stackable(*this->limits) && (item.data1[3] & 0x40))) {
ret_tokens.emplace_back("Wrapped");
}
@@ -318,14 +317,16 @@ std::string ItemNameIndex::describe_item(
// For tools, add the amount (if applicable)
} else if (item.data1[0] == 0x03) {
if (item.max_stack_size(this->version) > 1) {
if (item.max_stack_size(*this->limits) > 1) {
ret_tokens.emplace_back(string_printf("x%hhu", item.data1[5]));
}
}
string ret = join(ret_tokens, " ");
if (include_color_escapes) {
if (item.is_s_rank_weapon()) {
if (is_unidentified) {
return "$C3" + ret;
} else if (item.is_s_rank_weapon()) {
return "$C4" + ret;
} else if (this->item_parameter_table->is_item_rare(item)) {
return "$C6" + ret;
@@ -360,7 +361,7 @@ ItemData ItemNameIndex::parse_item_description(const std::string& desc) const {
}
}
}
ret.enforce_min_stack_size(this->version);
ret.enforce_min_stack_size(*this->limits);
return ret;
}
@@ -407,6 +408,16 @@ ItemData ItemNameIndex::parse_item_description_phase(const std::string& descript
if (is_wrapped) {
desc = desc.substr(8);
}
bool is_unidentified = starts_with(desc, "?");
if (is_unidentified) {
size_t z;
for (z = 1; z < desc.size(); z++) {
if (desc[z] != ' ' && desc[z] != '?') {
break;
}
}
desc = desc.substr(z);
}
// TODO: It'd be nice to be able to parse S-rank weapon specials here too.
uint8_t weapon_special = 0;
@@ -458,7 +469,7 @@ ItemData ItemNameIndex::parse_item_description_phase(const std::string& descript
if (ret.data1[0] == 0x00) {
// Weapons: add special, grind and percentages (or name, if S-rank)
ret.data1[4] = weapon_special | (is_wrapped ? 0x40 : 0x00);
ret.data1[4] = weapon_special | (is_wrapped ? 0x40 : 0x00) | (is_unidentified ? 0x80 : 0x00);
auto tokens = split(desc, ' ');
for (auto& token : tokens) {
@@ -596,7 +607,7 @@ ItemData ItemNameIndex::parse_item_description_phase(const std::string& descript
ret.data2[2] |= 0x40;
}
} else if (ret.data1[0] == 0x03) {
if (ret.max_stack_size(this->version) > 1) {
if (ret.max_stack_size(*this->limits) > 1) {
if (starts_with(desc, "x")) {
ret.data1[5] = stoul(desc.substr(1), nullptr, 10);
} else {
@@ -607,7 +618,7 @@ ItemData ItemNameIndex::parse_item_description_phase(const std::string& descript
}
if (is_wrapped) {
if (ret.is_stackable(this->version)) {
if (ret.is_stackable(*this->limits)) {
throw runtime_error("stackable items cannot be wrapped");
} else {
ret.data1[3] |= 0x40;
@@ -816,7 +827,7 @@ void ItemNameIndex::print_table(FILE* stream) const {
item.data1[0] = 0x03;
item.data1[1] = data1_1;
item.data1[(data1_1 == 0x02) ? 4 : 2] = data1_2;
item.set_tool_item_amount(this->version, 1);
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 " %2hhu* %s %s\n",
+2 -2
View File
@@ -20,8 +20,8 @@ public:
};
ItemNameIndex(
Version version,
std::shared_ptr<const ItemParameterTable> pmt,
std::shared_ptr<const ItemData::StackLimits> limits,
const std::vector<std::string>& name_coll);
inline size_t entry_count() const {
@@ -43,8 +43,8 @@ public:
private:
ItemData parse_item_description_phase(const std::string& description, bool skip_special) const;
Version version;
std::shared_ptr<const ItemParameterTable> item_parameter_table;
std::shared_ptr<const ItemData::StackLimits> limits;
std::unordered_map<uint32_t, std::shared_ptr<const ItemMetadata>> primary_identifier_index;
std::map<std::string, std::shared_ptr<const ItemMetadata>> name_index;
+5 -1
View File
@@ -1038,7 +1038,11 @@ uint8_t ItemParameterTable::get_item_adjusted_stars(const ItemData& item) const
}
bool ItemParameterTable::is_item_rare(const ItemData& item) const {
return (this->get_item_base_stars(item) >= 9);
try {
return (this->get_item_base_stars(item) >= 9);
} catch (const out_of_range&) {
return false;
}
}
bool ItemParameterTable::is_unsealable_item(uint8_t data1_0, uint8_t data1_1, uint8_t data1_2) const {
+7 -7
View File
@@ -6,7 +6,7 @@
using namespace std;
void player_use_item(shared_ptr<Client> c, size_t item_index, shared_ptr<PSOLFGEncryption> random_crypt) {
void player_use_item(shared_ptr<Client> c, size_t item_index, shared_ptr<PSOLFGEncryption> opt_rand_crypt) {
auto s = c->require_server_state();
// On PC (and presumably DC), the client sends a 6x29 after this to delete the
@@ -81,7 +81,7 @@ void player_use_item(shared_ptr<Client> c, size_t item_index, shared_ptr<PSOLFGE
break;
case 6: // Hit Material (v1/v2) or Luck Material (v3/v4)
type = Type::LUCK;
if (!is_v3_or_later) {
if (!is_v3_or_later && (c->version() != Version::GC_NTE)) {
// Hit material doesn't exist on v3/v4, but we'll ignore type anyway
// in this case because track_non_hp_tp_materials is false
p->disp.stats.char_stats.ata += 2;
@@ -91,7 +91,7 @@ void player_use_item(shared_ptr<Client> c, size_t item_index, shared_ptr<PSOLFGE
break;
case 7: // Luck Material (v1/v2)
type = Type::LUCK;
if (!is_v3_or_later) {
if (!is_v3_or_later && (c->version() != Version::GC_NTE)) {
p->disp.stats.char_stats.lck += 2;
} else {
throw runtime_error("unknown material used");
@@ -111,9 +111,9 @@ void player_use_item(shared_ptr<Client> c, size_t item_index, shared_ptr<PSOLFGE
}
armor.data.data1[5]++;
} else if (item.data.is_wrapped(c->version())) {
} else if (item.data.is_wrapped(*s->item_stack_limits(c->version()))) {
// Unwrap present
item.data.unwrap(c->version());
item.data.unwrap(*s->item_stack_limits(c->version()));
should_delete_item = false;
} else if (primary_identifier == 0x00330000) {
@@ -177,7 +177,7 @@ void player_use_item(shared_ptr<Client> c, size_t item_index, shared_ptr<PSOLFGE
if (sum == 0) {
throw runtime_error("no unwrap results available for event");
}
size_t det = random_crypt->next() % sum;
size_t det = random_from_optional_crypt(opt_rand_crypt) % sum;
for (size_t z = 0; z < table.second; z++) {
const auto& entry = table.first[z];
if (det > entry.probability) {
@@ -250,7 +250,7 @@ void player_use_item(shared_ptr<Client> c, size_t item_index, shared_ptr<PSOLFGE
if (should_delete_item) {
// Allow overdrafting meseta if the client is not BB, since the server isn't
// informed when meseta is added or removed from the bank.
player->remove_item(item.data.id, 1, c->version());
player->remove_item(item.data.id, 1, *s->item_stack_limits(c->version()));
}
}
+1 -1
View File
@@ -12,7 +12,7 @@
#include "ServerState.hh"
#include "StaticGameData.hh"
void player_use_item(std::shared_ptr<Client> c, size_t item_index, std::shared_ptr<PSOLFGEncryption> random_crypt);
void player_use_item(std::shared_ptr<Client> c, size_t item_index, std::shared_ptr<PSOLFGEncryption> opt_rand_crypt);
void player_feed_mag(std::shared_ptr<Client> c, size_t mag_item_index, size_t fed_item_index);
void apply_mag_feed_result(
+1 -1
View File
@@ -22,7 +22,7 @@ struct CharacterStats {
struct PlayerStats {
/* 00 */ CharacterStats char_stats;
/* 0E */ le_uint16_t unknown_a1 = 0;
/* 0E */ le_uint16_t esp = 0;
/* 10 */ le_float height = 0.0;
/* 14 */ le_float unknown_a3 = 0.0;
/* 18 */ le_uint32_t level = 0;
+38 -4
View File
@@ -19,6 +19,8 @@ License::License(const JSON& json)
bb_team_id(0) {
this->serial_number = json.get_int("SerialNumber");
this->access_key = json.get_string("AccessKey", "");
this->dc_nte_serial_number = json.get_string("DCNTESerialNumber", "");
this->dc_nte_access_key = json.get_string("DCNTEAccessKey", "");
this->gc_password = json.get_string("GCPassword", "");
this->xb_gamertag = json.get_string("XBGamerTag", "");
this->xb_user_id = json.get_int("XBUserID", 0);
@@ -38,6 +40,8 @@ JSON License::json() const {
return JSON::dict({
{"SerialNumber", this->serial_number},
{"AccessKey", this->access_key},
{"DCNTESerialNumber", this->dc_nte_serial_number},
{"DCNTEAccessKey", this->dc_nte_access_key},
{"GCPassword", this->gc_password},
{"XBGamerTag", this->xb_gamertag},
{"XBUserID", this->xb_user_id},
@@ -63,6 +67,12 @@ string License::str() const {
if (!this->access_key.empty()) {
tokens.emplace_back("access_key=" + this->access_key);
}
if (!this->dc_nte_serial_number.empty()) {
tokens.emplace_back("dc_nte_serial_number=" + this->dc_nte_serial_number);
}
if (!this->dc_nte_access_key.empty()) {
tokens.emplace_back("dc_nte_access_key=" + this->dc_nte_access_key);
}
if (!this->gc_password.empty()) {
tokens.emplace_back("gc_password=" + this->gc_password);
}
@@ -147,23 +157,47 @@ vector<shared_ptr<License>> LicenseIndex::all() const {
void LicenseIndex::add(shared_ptr<License> l) {
this->serial_number_to_license[l->serial_number] = l;
if (!l->bb_username.empty()) {
this->bb_username_to_license[l->bb_username] = l;
if (!l->dc_nte_serial_number.empty()) {
this->dc_nte_serial_number_to_license[l->dc_nte_serial_number] = l;
}
if (!l->xb_gamertag.empty()) {
this->xb_gamertag_to_license[l->xb_gamertag] = l;
}
if (!l->bb_username.empty()) {
this->bb_username_to_license[l->bb_username] = l;
}
}
void LicenseIndex::remove(uint32_t serial_number) {
auto l = this->serial_number_to_license.at(serial_number);
this->serial_number_to_license.erase(l->serial_number);
if (!l->bb_username.empty()) {
this->bb_username_to_license.erase(l->bb_username);
if (!l->dc_nte_serial_number.empty()) {
this->dc_nte_serial_number_to_license.erase(l->dc_nte_serial_number);
}
if (!l->xb_gamertag.empty()) {
this->xb_gamertag_to_license.erase(l->xb_gamertag);
}
if (!l->bb_username.empty()) {
this->bb_username_to_license.erase(l->bb_username);
}
}
shared_ptr<License> LicenseIndex::verify_dc_nte(const string& serial_number, const string& access_key) const {
if (serial_number.empty()) {
throw no_username();
}
try {
auto& license = this->dc_nte_serial_number_to_license.at(serial_number);
if (license->ban_end_time && (license->ban_end_time >= now())) {
throw invalid_argument("user is banned");
}
if (license->dc_nte_access_key != access_key) {
throw incorrect_access_key();
}
return license;
} catch (const out_of_range&) {
throw missing_license();
}
}
shared_ptr<License> LicenseIndex::verify_v1_v2(
+20 -16
View File
@@ -14,27 +14,29 @@ class License {
public:
enum class Flag : uint32_t {
// clang-format off
KICK_USER = 0x00000001,
BAN_USER = 0x00000002,
SILENCE_USER = 0x00000004,
CHANGE_EVENT = 0x00000010,
ANNOUNCE = 0x00000020,
FREE_JOIN_GAMES = 0x00000040,
DEBUG = 0x01000000,
CHEAT_ANYWHERE = 0x02000000,
DISABLE_QUEST_REQUIREMENTS = 0x04000000,
MODERATOR = 0x00000007,
ADMINISTRATOR = 0x000000FF,
ROOT = 0x7FFFFFFF,
IS_SHARED_SERIAL = 0x80000000,
KICK_USER = 0x00000001,
BAN_USER = 0x00000002,
SILENCE_USER = 0x00000004,
CHANGE_EVENT = 0x00000010,
ANNOUNCE = 0x00000020,
FREE_JOIN_GAMES = 0x00000040,
DEBUG = 0x01000000,
CHEAT_ANYWHERE = 0x02000000,
DISABLE_QUEST_REQUIREMENTS = 0x04000000,
ALWAYS_ENABLE_CHAT_COMMANDS = 0x08000000,
MODERATOR = 0x00000007,
ADMINISTRATOR = 0x000000FF,
ROOT = 0x7FFFFFFF,
IS_SHARED_SERIAL = 0x80000000,
// NOTE: When adding or changing license flags, don't forget to change the
// documentation in the shell's help text.
UNUSED_BITS = 0x78FFFF00,
UNUSED_BITS = 0x70FFFF00,
// clang-format on
};
uint32_t serial_number = 0;
std::string dc_nte_serial_number;
std::string dc_nte_access_key;
std::string access_key;
std::string gc_password;
std::string xb_gamertag;
@@ -123,6 +125,7 @@ public:
void add(std::shared_ptr<License> l);
void remove(uint32_t serial_number);
std::shared_ptr<License> verify_dc_nte(const std::string& serial_number, const std::string& access_key) const;
std::shared_ptr<License> verify_v1_v2(
uint32_t serial_number,
const std::string& access_key,
@@ -140,8 +143,9 @@ public:
std::shared_ptr<License> verify_bb(const std::string& username, const std::string& password) const;
protected:
std::unordered_map<std::string, std::shared_ptr<License>> bb_username_to_license;
std::unordered_map<std::string, std::shared_ptr<License>> dc_nte_serial_number_to_license;
std::unordered_map<std::string, std::shared_ptr<License>> xb_gamertag_to_license;
std::unordered_map<std::string, std::shared_ptr<License>> bb_username_to_license;
std::unordered_map<uint32_t, std::shared_ptr<License>> serial_number_to_license;
std::shared_ptr<License> create_temporary_license_for_shared_license(
+114 -44
View File
@@ -144,7 +144,7 @@ Lobby::Lobby(shared_ptr<ServerState> s, uint32_t id, bool is_game)
next_game_item_id(0xCC000000),
base_version(Version::GC_V3),
allowed_versions(0x0000),
section_id(0),
override_section_id(0xFF),
episode(Episode::NONE),
mode(GameMode::NORMAL),
difficulty(0),
@@ -252,15 +252,44 @@ void Lobby::create_item_creator() {
s->weapon_random_sets.at(this->difficulty),
s->tekker_adjustment_set,
s->item_parameter_table(this->base_version),
this->base_version,
s->item_stack_limits(this->base_version),
this->episode,
(this->mode == GameMode::SOLO) ? GameMode::NORMAL : this->mode,
this->difficulty,
this->section_id,
this->random_seed,
this->effective_section_id(),
this->opt_rand_crypt,
this->quest ? this->quest->battle_rules : nullptr);
}
void Lobby::change_section_id() {
if (this->item_creator) {
uint8_t new_section_id = this->effective_section_id();
if (this->item_creator->get_section_id() != new_section_id) {
this->log.info("Changing section ID to %s", name_for_section_id(new_section_id));
this->item_creator->set_section_id(new_section_id);
for (const auto& c : this->clients) {
if (c && c->config.check_flag(Client::Flag::DEBUG_ENABLED)) {
send_text_message_printf(c, "$C5Section ID changed\nto %s (%hhu)", name_for_section_id(new_section_id), new_section_id);
}
}
}
}
}
uint8_t Lobby::effective_section_id() const {
if (this->override_section_id != 0xFF) {
return this->override_section_id;
}
if (this->check_flag(Lobby::Flag::USE_CREATOR_SECTION_ID)) {
return this->creator_section_id;
}
auto leader = this->clients.at(this->leader_id);
if (leader) {
return leader->character()->disp.visual.section_id;
}
return 0;
}
shared_ptr<Map> Lobby::load_maps(
Version version,
Episode episode,
@@ -268,18 +297,16 @@ shared_ptr<Map> Lobby::load_maps(
uint8_t event,
uint32_t lobby_id,
shared_ptr<const Map::RareEnemyRates> rare_rates,
shared_ptr<PSOLFGEncryption> random_crypt,
shared_ptr<const VersionedQuest> vq) {
if (!vq->dat_contents_decompressed) {
throw runtime_error("quest does not have DAT data");
}
auto map = make_shared<Map>(version, lobby_id, random_crypt);
map->add_enemies_and_objects_from_quest_data(
uint32_t random_seed,
shared_ptr<PSOLFGEncryption> opt_rand_crypt,
shared_ptr<const string> quest_dat_contents_decompressed) {
auto map = make_shared<Map>(version, lobby_id, random_seed, opt_rand_crypt);
map->add_entities_from_quest_data(
episode,
difficulty,
event,
vq->dat_contents_decompressed->data(),
vq->dat_contents_decompressed->size(),
quest_dat_contents_decompressed->data(),
quest_dat_contents_decompressed->size(),
rare_rates);
return map;
}
@@ -294,17 +321,34 @@ shared_ptr<Map> Lobby::load_maps(
shared_ptr<const SetDataTableBase> sdt,
function<shared_ptr<const string>(Version, const string&)> get_file_data,
shared_ptr<const Map::RareEnemyRates> rare_rates,
shared_ptr<PSOLFGEncryption> random_crypt,
uint32_t random_seed,
shared_ptr<PSOLFGEncryption> opt_rand_crypt,
const parray<le_uint32_t, 0x20>& variations,
const PrefixedLogger* log) {
auto enemy_filenames = sdt->map_filenames_for_variations(variations, episode, mode, true);
auto object_filenames = sdt->map_filenames_for_variations(variations, episode, mode, false);
return Lobby::load_maps(enemy_filenames, object_filenames, version, episode, mode, difficulty, event, lobby_id, get_file_data, rare_rates, random_crypt, log);
auto enemy_filenames = sdt->map_filenames_for_variations(variations, episode, mode, SetDataTable::FilenameType::ENEMIES);
auto object_filenames = sdt->map_filenames_for_variations(variations, episode, mode, SetDataTable::FilenameType::OBJECTS);
auto event_filenames = sdt->map_filenames_for_variations(variations, episode, mode, SetDataTable::FilenameType::EVENTS);
return Lobby::load_maps(
enemy_filenames,
object_filenames,
event_filenames,
version,
episode,
mode,
difficulty,
event,
lobby_id,
get_file_data,
rare_rates,
random_seed,
opt_rand_crypt,
log);
}
shared_ptr<Map> Lobby::load_maps(
const vector<string>& enemy_filenames,
const vector<string>& object_filenames,
const vector<string>& event_filenames,
Version version,
Episode episode,
GameMode mode,
@@ -313,9 +357,10 @@ shared_ptr<Map> Lobby::load_maps(
uint32_t lobby_id,
function<shared_ptr<const string>(Version, const string&)> get_file_data,
shared_ptr<const Map::RareEnemyRates> rare_rates,
shared_ptr<PSOLFGEncryption> random_crypt,
uint32_t rare_seed,
shared_ptr<PSOLFGEncryption> opt_rand_crypt,
const PrefixedLogger* log) {
auto map = make_shared<Map>(version, lobby_id, random_crypt);
auto map = make_shared<Map>(version, lobby_id, rare_seed, opt_rand_crypt);
// Don't load free-roam maps in Challenge mode, since players can't go to
// Ragol without a quest loaded
@@ -360,6 +405,21 @@ shared_ptr<Map> Lobby::load_maps(
} else if (log) {
log->info("No objects to load for floor %02zX", floor);
}
const auto& floor_event_filename = event_filenames.at(floor);
if (!floor_event_filename.empty()) {
auto map_data = get_file_data(version, floor_event_filename);
if (map_data) {
map->add_events_from_map_data(floor, map_data->data(), map_data->size());
if (log) {
log->info("Loaded events map %s for floor %02zX", floor_event_filename.c_str(), floor);
}
} else if (log) {
log->info("Events map %s for floor %02zX cannot be used; skipping", floor_event_filename.c_str(), floor);
}
} else if (log) {
log->info("No events to load for floor %02zX", floor);
}
}
return map;
@@ -377,6 +437,9 @@ void Lobby::load_maps() {
}
auto vq = this->quest->version(this->base_version, leader_c->language());
if (!vq->dat_contents_decompressed) {
throw runtime_error("quest does not have DAT data");
}
this->map = this->load_maps(
this->base_version,
this->episode,
@@ -384,8 +447,9 @@ void Lobby::load_maps() {
this->event,
this->lobby_id,
rare_rates,
this->random_crypt,
vq);
this->random_seed,
this->opt_rand_crypt,
vq->dat_contents_decompressed);
} else if (this->mode != GameMode::CHALLENGE) {
auto s = this->require_server_state();
@@ -399,12 +463,13 @@ void Lobby::load_maps() {
s->set_data_table(this->base_version, this->episode, this->mode, this->difficulty),
bind(&ServerState::load_map_file, s.get(), placeholders::_1, placeholders::_2),
rare_rates,
this->random_crypt,
this->random_seed,
this->opt_rand_crypt,
this->variations,
&this->log);
} else {
this->map = make_shared<Map>(this->base_version, this->lobby_id, this->random_crypt);
this->map = make_shared<Map>(this->base_version, this->lobby_id, this->random_seed, this->opt_rand_crypt);
}
this->log.info("Generated objects list (%zu entries):", this->map->objects.size());
@@ -417,6 +482,11 @@ void Lobby::load_maps() {
string e_str = this->map->enemies[z].str();
this->log.info("(E-%zX) %s", z, e_str.c_str());
}
this->log.info("Generated events list (%zu entries):", this->map->events.size());
for (size_t z = 0; z < this->map->events.size(); z++) {
string e_str = this->map->events[z].str();
this->log.info("%s", e_str.c_str());
}
this->log.info("Loaded maps contain %zu object entries and %zu enemy entries overall (%zu as rares)",
this->map->objects.size(), this->map->enemies.size(), this->map->rare_enemy_indexes.size());
}
@@ -434,7 +504,7 @@ void Lobby::create_ep3_server() {
.card_index = is_nte ? s->ep3_card_index_trial : s->ep3_card_index,
.map_index = s->ep3_map_index,
.behavior_flags = s->ep3_behavior_flags,
.random_crypt = this->random_crypt,
.opt_rand_crypt = this->opt_rand_crypt,
.tournament = tourn,
.trap_card_ids = s->ep3_trap_card_ids,
};
@@ -452,8 +522,9 @@ void Lobby::reassign_leader_on_client_departure(size_t leaving_client_index) {
if (x == leaving_client_index) {
continue;
}
if (this->clients[x].get()) {
if (this->clients[x]) {
this->leader_id = x;
this->change_section_id();
return;
}
}
@@ -478,13 +549,22 @@ bool Lobby::any_client_loading() const {
size_t Lobby::count_clients() const {
size_t ret = 0;
for (size_t x = 0; x < this->max_clients; x++) {
if (this->clients[x].get()) {
if (this->clients[x]) {
ret++;
}
}
return ret;
}
bool Lobby::any_v1_clients_present() const {
for (size_t x = 0; x < this->max_clients; x++) {
if (this->clients[x] && is_v1(this->clients[x]->version())) {
return true;
}
}
return false;
}
void Lobby::add_client(shared_ptr<Client> c, ssize_t required_client_id) {
ssize_t index;
ssize_t min_client_id = this->check_flag(Lobby::Flag::IS_SPECTATOR_TEAM) ? 4 : 0;
@@ -531,6 +611,7 @@ void Lobby::add_client(shared_ptr<Client> c, ssize_t required_client_id) {
}
if (leader_index >= this->max_clients) {
this->leader_id = c->lobby_client_id;
this->change_section_id();
}
// If this is a lobby or no one was here before this, reassign all the floor
@@ -734,8 +815,9 @@ Lobby::JoinError Lobby::join_error_for_client(std::shared_ptr<Client> c, const s
}
if (this->quest) {
size_t num_clients = this->count_clients() + 1;
if (!c->can_see_quest(this->quest, this->event, this->difficulty, num_clients) ||
!c->can_play_quest(this->quest, this->event, this->difficulty, num_clients)) {
bool v1_present = is_v1(c->version()) || this->any_v1_clients_present();
if (!c->can_see_quest(this->quest, this->event, this->difficulty, num_clients, v1_present) ||
!c->can_play_quest(this->quest, this->event, this->difficulty, num_clients, v1_present)) {
return JoinError::NO_ACCESS_TO_QUEST;
}
}
@@ -749,19 +831,6 @@ Lobby::JoinError Lobby::join_error_for_client(std::shared_ptr<Client> c, const s
return JoinError::ALLOWED;
}
uint8_t Lobby::game_event_for_lobby_event(uint8_t lobby_event) {
if (lobby_event > 7) {
return 0;
}
if (lobby_event == 7) {
return 2;
}
if (lobby_event == 2) {
return 0;
}
return lobby_event;
}
bool Lobby::item_exists(uint8_t floor, uint32_t item_id) const {
if (floor >= this->floor_item_managers.size()) {
return false;
@@ -857,13 +926,14 @@ unordered_map<uint32_t, shared_ptr<Client>> Lobby::clients_by_serial_number() co
QuestIndex::IncludeCondition Lobby::quest_include_condition() const {
size_t num_players = this->count_clients();
return [this, num_players](shared_ptr<const Quest> q) -> QuestIndex::IncludeState {
bool v1_present = this->any_v1_clients_present();
return [this, num_players, v1_present](shared_ptr<const Quest> q) -> QuestIndex::IncludeState {
bool is_enabled = true;
for (const auto& lc : this->clients) {
if (lc && !lc->can_see_quest(q, this->event, this->difficulty, num_players)) {
if (lc && !lc->can_see_quest(q, this->event, this->difficulty, num_players, v1_present)) {
return QuestIndex::IncludeState::HIDDEN;
}
if (lc && !lc->can_play_quest(q, this->event, this->difficulty, num_players)) {
if (lc && !lc->can_play_quest(q, this->event, this->difficulty, num_players, v1_present)) {
is_enabled = false;
}
}
+33 -25
View File
@@ -56,23 +56,24 @@ struct Lobby : public std::enable_shared_from_this<Lobby> {
uint32_t reassign_all_item_ids(uint32_t next_item_id);
};
enum class Flag {
GAME = 0x00000001,
PERSISTENT = 0x00000002,
// clang-format off
GAME = 0x00000001,
PERSISTENT = 0x00000002,
// Flags used only for games
CHEATS_ENABLED = 0x00000100,
QUEST_IN_PROGRESS = 0x00000200,
BATTLE_IN_PROGRESS = 0x00000400,
JOINABLE_QUEST_IN_PROGRESS = 0x00000800,
IS_SPECTATOR_TEAM = 0x00002000, // episode must be EP3 also
SPECTATORS_FORBIDDEN = 0x00004000,
CHEATS_ENABLED = 0x00000100,
QUEST_IN_PROGRESS = 0x00000200,
BATTLE_IN_PROGRESS = 0x00000400,
JOINABLE_QUEST_IN_PROGRESS = 0x00000800,
IS_SPECTATOR_TEAM = 0x00002000, // episode must be EP3 also
SPECTATORS_FORBIDDEN = 0x00004000,
START_BATTLE_PLAYER_IMMEDIATELY = 0x00008000,
CANNOT_CHANGE_CHEAT_MODE = 0x00010000,
CANNOT_CHANGE_CHEAT_MODE = 0x00010000,
USE_CREATOR_SECTION_ID = 0x00020000,
// Flags used only for lobbies
PUBLIC = 0x01000000,
DEFAULT = 0x02000000,
IS_OVERFLOW = 0x08000000,
PUBLIC = 0x01000000,
DEFAULT = 0x02000000,
IS_OVERFLOW = 0x08000000,
// clang-format on
};
enum class DropMode {
DISABLED = 0,
@@ -91,15 +92,16 @@ struct Lobby : public std::enable_shared_from_this<Lobby> {
uint32_t min_level;
uint32_t max_level;
// Item state
// Game state
std::array<uint32_t, 12> next_item_id_for_client;
uint32_t next_game_item_id;
std::vector<FloorItemManager> floor_item_managers;
// Map state
std::shared_ptr<const Map::RareEnemyRates> rare_enemy_rates;
std::shared_ptr<Map> map;
parray<le_uint32_t, 0x20> variations;
std::unique_ptr<QuestFlags> quest_flags_known; // If null, ALL quest flags are known
std::unique_ptr<QuestFlags> quest_flag_values;
std::unique_ptr<SwitchFlags> switch_flags;
// Game config
Version base_version;
@@ -107,7 +109,8 @@ struct Lobby : public std::enable_shared_from_this<Lobby> {
// bits are indexed as (1 << version), where version is a value from the
// Version enum.
uint16_t allowed_versions;
uint8_t section_id;
uint8_t creator_section_id;
uint8_t override_section_id;
Episode episode;
GameMode mode;
uint8_t difficulty; // 0-3
@@ -117,7 +120,7 @@ struct Lobby : public std::enable_shared_from_this<Lobby> {
std::string name;
// This seed is also sent to the client for rare enemy generation
uint32_t random_seed;
std::shared_ptr<PSOLFGEncryption> random_crypt;
std::shared_ptr<PSOLFGEncryption> opt_rand_crypt;
uint8_t allowed_drop_modes;
DropMode drop_mode;
std::shared_ptr<ItemCreator> item_creator;
@@ -195,6 +198,8 @@ struct Lobby : public std::enable_shared_from_this<Lobby> {
std::shared_ptr<ChallengeParameters> require_challenge_params() const;
void set_drop_mode(DropMode new_mode);
void create_item_creator();
void change_section_id();
uint8_t effective_section_id() const;
static std::shared_ptr<Map> load_maps(
Version version,
Episode episode,
@@ -202,8 +207,9 @@ struct Lobby : public std::enable_shared_from_this<Lobby> {
uint8_t event,
uint32_t lobby_id,
std::shared_ptr<const Map::RareEnemyRates> rare_rates,
std::shared_ptr<PSOLFGEncryption> random_crypt,
std::shared_ptr<const VersionedQuest> vq);
uint32_t random_seed,
std::shared_ptr<PSOLFGEncryption> opt_rand_crypt,
std::shared_ptr<const std::string> quest_dat_contents_decompressed);
static std::shared_ptr<Map> load_maps(
Version version,
Episode episode,
@@ -214,12 +220,14 @@ struct Lobby : public std::enable_shared_from_this<Lobby> {
std::shared_ptr<const SetDataTableBase> sdt,
std::function<std::shared_ptr<const std::string>(Version, const std::string&)> get_file_data,
std::shared_ptr<const Map::RareEnemyRates> rare_rates,
std::shared_ptr<PSOLFGEncryption> random_crypt,
uint32_t random_seed,
std::shared_ptr<PSOLFGEncryption> opt_rand_crypt,
const parray<le_uint32_t, 0x20>& variations,
const PrefixedLogger* log = nullptr);
static std::shared_ptr<Map> load_maps(
const std::vector<std::string>& enemy_filenames,
const std::vector<std::string>& object_filenames,
const std::vector<std::string>& event_filenames,
Version version,
Episode episode,
GameMode mode,
@@ -228,7 +236,8 @@ struct Lobby : public std::enable_shared_from_this<Lobby> {
uint32_t lobby_id,
std::function<std::shared_ptr<const std::string>(Version, const std::string&)> get_file_data,
std::shared_ptr<const Map::RareEnemyRates> rare_rates,
std::shared_ptr<PSOLFGEncryption> random_crypt,
uint32_t random_seed,
std::shared_ptr<PSOLFGEncryption> opt_rand_crypt,
const PrefixedLogger* log = nullptr);
void load_maps();
void create_ep3_server();
@@ -249,6 +258,7 @@ struct Lobby : public std::enable_shared_from_this<Lobby> {
void reassign_leader_on_client_departure(size_t leaving_client_id);
size_t count_clients() const;
bool any_v1_clients_present() const;
bool any_client_loading() const;
void add_client(std::shared_ptr<Client> c, ssize_t required_client_id = -1);
@@ -291,8 +301,6 @@ struct Lobby : public std::enable_shared_from_this<Lobby> {
QuestIndex::IncludeCondition quest_include_condition() const;
static uint8_t game_event_for_lobby_event(uint8_t lobby_event);
std::unordered_map<uint32_t, std::shared_ptr<Client>> clients_by_serial_number() const;
static void dispatch_on_idle_timeout(evutil_socket_t, short, void* ctx);
+409 -288
View File
File diff suppressed because it is too large Load Diff
+372 -99
View File
@@ -14,6 +14,10 @@ using namespace std;
static constexpr float UINT32_MAX_AS_FLOAT = 4294967296.0f;
static uint64_t section_index_key(uint8_t floor, uint16_t section, uint16_t wave_number) {
return (static_cast<uint64_t>(floor) << 32) | (static_cast<uint64_t>(section) << 16) | static_cast<uint64_t>(wave_number);
}
const char* Map::name_for_object_type(uint16_t type) {
switch (type) {
case 0x0000:
@@ -655,24 +659,44 @@ string Map::EnemyEntry::str() const {
this->unused.load());
}
Map::Enemy::Enemy(uint16_t enemy_id, size_t source_index, uint8_t floor, EnemyType type)
Map::Enemy::Enemy(
uint16_t enemy_id,
size_t source_index,
size_t set_index,
uint8_t floor,
uint16_t section,
uint16_t wave_number,
EnemyType type)
: source_index(source_index),
set_index(set_index),
enemy_id(enemy_id),
total_damage(0),
game_flags(0),
section(section),
wave_number(wave_number),
type(type),
floor(floor),
state_flags(0),
last_hit_by_client_id(0) {
}
state_flags(0) {}
string Map::Enemy::str() const {
return string_printf("[Map::Enemy E-%hX source %zX %s%s floor=%02hhX flags=%02hhX last_hit_by_client_id=%hu]",
return string_printf("[Map::Enemy E-%hX source %zX %s%s floor=%02hhX section=%04hX wave_number=%04hX flags=%02hhX]",
this->enemy_id,
this->source_index,
name_for_enum(this->type),
enemy_type_is_rare(this->type) ? " RARE" : "",
this->floor,
this->state_flags,
this->last_hit_by_client_id);
this->section,
this->wave_number,
this->state_flags);
}
string Map::Event::str() const {
return string_printf("[Map::Event W-%02hhX-%" PRIX32 " flags=%04hX floor=%02hhX action_stream_offset=%" PRIX32 "]",
this->floor,
this->event_id,
this->flags,
this->floor,
this->action_stream_offset);
}
string Map::Object::str() const {
@@ -689,10 +713,11 @@ string Map::Object::str() const {
this->item_drop_checked ? "true" : "false");
}
Map::Map(Version version, uint32_t lobby_id, std::shared_ptr<PSOLFGEncryption> random_crypt)
Map::Map(Version version, uint32_t lobby_id, uint32_t rare_seed, std::shared_ptr<PSOLFGEncryption> opt_rand_crypt)
: log(string_printf("[Lobby:%08" PRIX32 ":map] ", lobby_id), lobby_log.min_level),
version(version),
random_crypt(random_crypt) {}
rare_seed(rare_seed),
opt_rand_crypt(opt_rand_crypt) {}
void Map::clear() {
this->objects.clear();
@@ -711,17 +736,22 @@ void Map::add_objects_from_map_data(uint8_t floor, const void* data, size_t size
uint16_t object_id = this->objects.size();
this->objects.emplace_back(Object{
.source_index = z,
.object_id = object_id,
.floor = floor,
.object_id = object_id,
.base_type = objects[z].base_type,
.section = objects[z].section,
.group = objects[z].group,
.param1 = objects[z].param1,
.param3 = objects[z].param3,
.param4 = objects[z].param4,
.param5 = objects[z].param5,
.param6 = objects[z].param6,
.game_flags = 0,
.set_flags = 0,
.item_drop_checked = false,
});
uint64_t k = section_index_key(floor, objects[z].section, objects[z].group);
this->floor_section_and_group_to_object_index.emplace(k, object_id);
}
}
@@ -735,7 +765,7 @@ bool Map::check_and_log_rare_enemy(bool default_is_rare, uint32_t rare_rate) {
// versions, we must match the client's logic, even though it's more
// computationally expensive.
if (this->version == Version::BB_V4) {
if ((this->rare_enemy_indexes.size() < 0x10) && (this->random_crypt->next() < rare_rate)) {
if ((this->rare_enemy_indexes.size() < 0x10) && (random_from_optional_crypt(this->opt_rand_crypt) < rare_rate)) {
this->rare_enemy_indexes.emplace_back(this->enemies.size());
return true;
}
@@ -744,7 +774,7 @@ bool Map::check_and_log_rare_enemy(bool default_is_rare, uint32_t rare_rate) {
// TODO: We only need the first value from this crypt, so it's unfortunate
// that we have to initialize the entire thing. Find a way to make this
// faster.
PSOV2Encryption crypt(this->random_crypt->seed() + 0x1000 + this->enemies.size());
PSOV2Encryption crypt(this->rare_seed + 0x1000 + this->enemies.size());
float det = (static_cast<float>((crypt.next() >> 16) & 0xFFFF) / 65536.0f);
// On v1 and v2 (and GC NTE), the rare rate is 0.1% instead of 0.2%.
float threshold = is_v1_or_v2(this->version) ? 0.001f : 0.002f;
@@ -762,12 +792,17 @@ void Map::add_enemy(
uint8_t difficulty,
uint8_t event,
uint8_t floor,
size_t index,
size_t source_index,
const EnemyEntry& e,
std::shared_ptr<const RareEnemyRates> rare_rates) {
size_t set_index = this->enemy_set_flags.size();
this->enemy_set_flags.emplace_back(0);
auto add = [&](EnemyType type) -> void {
uint16_t enemy_id = this->enemies.size();
this->enemies.emplace_back(enemy_id, index, floor, type);
this->enemies.emplace_back(enemy_id, source_index, set_index, floor, e.section, e.wave_number, type);
uint64_t k = section_index_key(floor, e.section, e.wave_number);
this->floor_section_and_wave_number_to_enemy_index.emplace(k, enemy_id);
};
EnemyType child_type = EnemyType::UNKNOWN;
@@ -903,7 +938,7 @@ void Map::add_enemy(
if ((episode == Episode::EP2) && (e.floor == 0x11)) {
add(EnemyType::DEL_LILY);
} else {
add(this->check_and_log_rare_enemy((this->version == Version::BB_V4) && (e.uparam1 & 1), rare_rates->nar_lily)
add(this->check_and_log_rare_enemy(false, rare_rates->nar_lily)
? EnemyType::NAR_LILY
: EnemyType::POISON_LILY);
}
@@ -1183,16 +1218,13 @@ void Map::add_enemy(
case 0x00C7: // TBoss3VoloptHiraisin
case 0x0118:
add(EnemyType::UNKNOWN);
this->log.warning(
"(Entry %zu, offset %zX in file) Unknown enemy type %04hX",
index, index * sizeof(EnemyEntry), e.base_type.load());
break;
default:
add(EnemyType::UNKNOWN);
this->log.warning(
"(Entry %zu, offset %zX in file) Invalid enemy type %04hX",
index, index * sizeof(EnemyEntry), e.base_type.load());
source_index, source_index * sizeof(EnemyEntry), e.base_type.load());
break;
}
@@ -1314,6 +1346,10 @@ void Map::add_random_enemies_from_map_data(
}
wave_events_segment_r.go(wave_events_header.entries_offset);
size_t action_stream_base_offset = this->event_action_stream.size();
this->event_action_stream += wave_events_segment_r.pread(
wave_events_header.action_stream_offset, wave_events_segment_r.size() - wave_events_header.action_stream_offset);
const auto& locations_header = locations_segment_r.get<RandomEnemyLocationsHeader>();
const auto& definitions_header = definitions_segment_r.get<RandomEnemyDefinitionsHeader>();
auto definitions_r = definitions_segment_r.sub(
@@ -1330,6 +1366,7 @@ void Map::add_random_enemies_from_map_data(
size_t remaining_waves = random_state->rand_int_biased(1, entry.max_waves);
// Trace: at 0080E125 EAX is wave count
le_uint32_t wave_next_event_id = entry.event_id;
uint32_t wave_number = entry.wave_number;
while (remaining_waves) {
remaining_waves--;
@@ -1416,17 +1453,65 @@ void Map::add_random_enemies_from_map_data(
}
}
if (remaining_waves) {
// We don't generate the event stream here, but the client does, and in
// doing so, it uses one value from random to determine the delay
// parameter of the event. To keep our state in sync with what the
// client would do, we skip a random value here.
random_state->random.next();
/* ev.delay = */ random_state->rand_int_biased(entry.min_delay, entry.max_delay);
this->add_event(wave_next_event_id, entry.flags, floor, entry.section, wave_number, this->event_action_stream.size());
this->event_action_stream.push_back(0x0C);
wave_next_event_id = entry.event_id + wave_number + 10000;
this->event_action_stream.append(reinterpret_cast<const char*>(&wave_next_event_id), sizeof(wave_next_event_id));
this->event_action_stream.push_back(0x01);
wave_number++;
}
}
// For the same reason as above, we need to skip another random value here.
random_state->random.next();
/* ev.delay = */ random_state->rand_int_biased(entry.min_delay, entry.max_delay);
this->add_event(wave_next_event_id, entry.flags, floor, entry.section, wave_number, action_stream_base_offset + entry.action_stream_offset);
wave_number++;
}
}
void Map::add_event(uint32_t event_id, uint16_t flags, uint8_t floor, uint16_t section, uint16_t wave_number, uint32_t action_stream_offset) {
size_t index = this->events.size();
auto& ev = this->events.emplace_back();
ev.event_id = event_id;
ev.section = section;
ev.wave_number = wave_number;
ev.flags = flags;
ev.floor = floor;
ev.action_stream_offset = action_stream_offset;
uint64_t k = (static_cast<uint64_t>(floor) << 32) | event_id;
if (!this->floor_and_event_id_to_index.emplace(k, index).second) {
this->log.warning("Duplicate event ID: W-%02hhX-%" PRIX32, floor, event_id);
}
k = section_index_key(floor, section, wave_number);
this->floor_section_and_wave_number_to_event_index.emplace(k, index);
}
Map::Event& Map::get_event(uint8_t floor, uint32_t event_id) {
uint64_t k = (static_cast<uint64_t>(floor) << 32) | event_id;
return this->events.at(this->floor_and_event_id_to_index.at(k));
}
const Map::Event& Map::get_event(uint8_t floor, uint32_t event_id) const {
uint64_t k = (static_cast<uint64_t>(floor) << 32) | event_id;
return this->events.at(this->floor_and_event_id_to_index.at(k));
}
void Map::add_events_from_map_data(uint8_t floor, const void* data, size_t size) {
StringReader r(data, size);
const auto& header = r.get<EventsSectionHeader>();
if (header.format != 0) {
throw runtime_error("events section format is not zero");
}
size_t action_stream_base_offset = this->event_action_stream.size();
this->event_action_stream += r.pread(header.action_stream_offset, r.size() - header.action_stream_offset);
this->events.reserve(this->events.size() + header.entry_count);
auto events_r = r.sub(header.entries_offset, sizeof(Event1Entry) * header.entry_count);
while (!events_r.eof()) {
const auto& entry = events_r.get<Event1Entry>();
this->add_event(entry.event_id, entry.flags, floor, entry.section, entry.wave_number, entry.action_stream_offset + action_stream_base_offset);
}
}
@@ -1491,7 +1576,7 @@ vector<Map::DATSectionsForFloor> Map::collect_quest_map_data_sections(const void
return ret;
}
void Map::add_enemies_and_objects_from_quest_data(
void Map::add_entities_from_quest_data(
Episode episode,
uint8_t difficulty,
uint8_t event,
@@ -1513,28 +1598,15 @@ void Map::add_enemies_and_objects_from_quest_data(
this->add_objects_from_map_data(floor, r.pgetv(floor_sections.objects + sizeof(header), header.data_size), header.data_size);
}
if (floor_sections.enemies != 0xFFFFFFFF) {
const auto& header = r.pget<SectionHeader>(floor_sections.enemies);
if (header.data_size % sizeof(EnemyEntry)) {
throw runtime_error("quest layout enemy section size is not a multiple of enemy entry size");
}
this->add_enemies_from_map_data(
episode,
difficulty,
event,
floor,
r.pgetv(floor_sections.enemies + sizeof(header), header.data_size),
header.data_size,
rare_rates);
} else if ((floor_sections.wave_events != 0xFFFFFFFF) &&
if ((floor_sections.wave_events != 0xFFFFFFFF) &&
(floor_sections.random_enemy_locations != 0xFFFFFFFF) &&
(floor_sections.random_enemy_definitions != 0xFFFFFFFF)) {
// Challenge Mode random enemy waves
const auto& wave_events_header = r.pget<SectionHeader>(floor_sections.wave_events);
const auto& random_enemy_locations_header = r.pget<SectionHeader>(floor_sections.random_enemy_locations);
const auto& random_enemy_definitions_header = r.pget<SectionHeader>(floor_sections.random_enemy_definitions);
if (!random_state) {
random_state = make_shared<DATParserRandomState>(this->random_crypt->seed());
random_state = make_shared<DATParserRandomState>(this->rare_seed);
}
this->add_random_enemies_from_map_data(
episode,
@@ -1546,6 +1618,29 @@ void Map::add_enemies_and_objects_from_quest_data(
r.sub(floor_sections.random_enemy_definitions + sizeof(SectionHeader), random_enemy_definitions_header.data_size),
random_state,
rare_rates);
} else {
// Non-Challenge (standard) enemies
if (floor_sections.enemies != 0xFFFFFFFF) {
const auto& header = r.pget<SectionHeader>(floor_sections.enemies);
if (header.data_size % sizeof(EnemyEntry)) {
throw runtime_error("quest layout enemy section size is not a multiple of enemy entry size");
}
this->add_enemies_from_map_data(
episode,
difficulty,
event,
floor,
r.pgetv(floor_sections.enemies + sizeof(header), header.data_size),
header.data_size,
rare_rates);
}
if (floor_sections.wave_events != 0xFFFFFFFF) {
const auto& wave_events_header = r.pget<SectionHeader>(floor_sections.wave_events);
const void* data = r.pgetv(floor_sections.wave_events + sizeof(SectionHeader), wave_events_header.data_size);
this->add_events_from_map_data(floor, data, wave_events_header.data_size);
}
}
}
}
@@ -1568,6 +1663,154 @@ Map::Enemy& Map::find_enemy(uint8_t floor, EnemyType type) {
throw out_of_range("enemy not found");
}
std::vector<Map::Object*> Map::get_objects(uint8_t floor, uint16_t section, uint16_t group) {
uint64_t k = section_index_key(floor, section, group);
vector<Object*> ret;
for (auto its = this->floor_section_and_group_to_object_index.equal_range(k); its.first != its.second; its.first++) {
ret.emplace_back(&this->objects.at(its.first->second));
}
return ret;
}
std::vector<Map::Enemy*> Map::get_enemies(uint8_t floor, uint16_t section, uint16_t wave_number) {
uint64_t k = section_index_key(floor, section, wave_number);
vector<Enemy*> ret;
for (auto its = this->floor_section_and_wave_number_to_enemy_index.equal_range(k); its.first != its.second; its.first++) {
ret.emplace_back(&this->enemies.at(its.first->second));
}
return ret;
}
std::vector<Map::Event*> Map::get_events(uint8_t floor, uint16_t section, uint16_t wave_number) {
uint64_t k = section_index_key(floor, section, wave_number);
vector<Event*> ret;
for (auto its = this->floor_section_and_wave_number_to_event_index.equal_range(k); its.first != its.second; its.first++) {
ret.emplace_back(&this->events.at(its.first->second));
}
return ret;
}
std::vector<Map::Event*> Map::get_events(uint8_t floor) {
uint64_t k_start = (static_cast<uint64_t>(floor) << 32);
uint64_t k_end = (static_cast<uint64_t>(floor + 1) << 32);
vector<Event*> ret;
for (auto it = this->floor_and_event_id_to_index.lower_bound(k_start);
(it != this->floor_and_event_id_to_index.end()) && (it->first < k_end);
it++) {
ret.emplace_back(&this->events.at(it->second));
}
return ret;
}
template <typename EntryT>
static string disassemble_vector_file_t(const void* data, size_t size, size_t* entry_number, char type_ch) {
deque<string> ret;
StringReader r(data, size);
size_t local_entry_number = 0;
if (!entry_number) {
entry_number = &local_entry_number;
}
while (r.remaining() >= sizeof(EntryT)) {
string o_str = r.get<EntryT>().str();
ret.emplace_back(string_printf("/* %c-%zX */ %s", type_ch, (*entry_number)++, o_str.c_str()));
}
if (r.remaining()) {
ret.emplace_back("// Warning: section size is not a multiple of entry size");
size_t size = r.remaining();
ret.emplace_back(format_data(r.getv(size), size));
}
return join(ret, "\n");
}
string Map::disassemble_objects_data(const void* data, size_t size, size_t* object_number) {
return disassemble_vector_file_t<ObjectEntry>(data, size, object_number, 'K');
}
string Map::disassemble_enemies_data(const void* data, size_t size, size_t* enemy_number) {
return disassemble_vector_file_t<EnemyEntry>(data, size, enemy_number, 'S');
}
string Map::disassemble_wave_events_data(const void* data, size_t size, uint8_t floor) {
deque<string> ret;
StringReader r(data, size);
const auto& evt_header = r.get<EventsSectionHeader>();
if (evt_header.format == 0x65767432) { // 'evt2'
ret.emplace_back(".evt2_format"); // TODO
size_t size = r.remaining();
ret.emplace_back(format_data(r.getv(size), size));
} else {
auto action_stream_r = r.sub(evt_header.action_stream_offset);
for (size_t z = 0; z < evt_header.entry_count; z++) {
const auto& entry = r.get<Event1Entry>();
ret.emplace_back(string_printf("/* W-%02hhX-%" PRIX32 " */ [Event1Entry flags=%04hX type=%04hX section=%04hX wave_number=%04hX delay=%" PRIu32 "]",
floor,
entry.event_id.load(),
entry.flags.load(),
entry.event_type.load(),
entry.section.load(),
entry.wave_number.load(),
entry.delay.load()));
auto ev_actions_r = action_stream_r.sub(entry.action_stream_offset);
bool should_continue = true;
while (!ev_actions_r.eof() && should_continue) {
uint8_t opcode = ev_actions_r.get_u8();
switch (opcode) {
case 0x00:
ret.emplace_back(string_printf(" 00 nop"));
break;
case 0x01:
ret.emplace_back(string_printf(" 01 stop"));
should_continue = false;
break;
case 0x08: {
uint16_t section = ev_actions_r.get_u16l();
uint16_t group = ev_actions_r.get_u16l();
ret.emplace_back(string_printf(" 08 %04hX %04hX construct_objects section=%04hX group=%04hX",
section, group, section, group));
break;
}
case 0x09: {
uint16_t section = ev_actions_r.get_u16l();
uint16_t wave_number = ev_actions_r.get_u16l();
ret.emplace_back(string_printf(" 09 %04hX %04hX construct_enemies section=%04hX wave_number=%04hX",
section, wave_number, section, wave_number));
break;
}
case 0x0A: {
uint16_t id = ev_actions_r.get_u16l();
ret.emplace_back(string_printf(" 0A %04hX enable_switch_flag id=%04hX", id, id));
break;
}
case 0x0B: {
uint16_t id = ev_actions_r.get_u16l();
ret.emplace_back(string_printf(" 0B %04hX disable_switch_flag id=%04hX", id, id));
break;
}
case 0x0C: {
uint32_t event_id = ev_actions_r.get_u32l();
ret.emplace_back(string_printf(" 0C %08" PRIX32 " trigger_event event_id=%08" PRIX32, event_id, event_id));
break;
}
case 0x0D: {
uint16_t section = ev_actions_r.get_u16l();
uint16_t wave_number = ev_actions_r.get_u16l();
ret.emplace_back(string_printf(" 0D %04hX %04hX construct_enemies_stop section=%04hX wave_number=%04hX",
section, wave_number, section, wave_number));
break;
}
default:
ret.emplace_back(string_printf(" %02hhX .invalid", opcode));
}
}
}
}
return join(ret, "\n");
}
string Map::disassemble_quest_data(const void* data, size_t size) {
auto all_floor_sections = Map::collect_quest_map_data_sections(data, size);
@@ -1581,56 +1824,34 @@ string Map::disassemble_quest_data(const void* data, size_t size) {
if (floor_sections.objects != 0xFFFFFFFF) {
ret.emplace_back(string_printf(".objects %zu", floor));
const auto& header = r.pget<SectionHeader>(floor_sections.objects);
auto sub_r = r.sub(floor_sections.objects + sizeof(SectionHeader), header.data_size);
while (sub_r.remaining() >= sizeof(ObjectEntry)) {
string o_str = sub_r.get<ObjectEntry>().str();
ret.emplace_back(string_printf("/* K-%zX */ %s", object_number++, o_str.c_str()));
}
if (sub_r.remaining()) {
ret.emplace_back("// Warning: object section size is not a multiple of object entry size");
size_t offset = floor_sections.objects + sizeof(SectionHeader) + r.where();
size_t bytes = r.remaining();
ret.emplace_back(format_data(r.getv(r.remaining()), bytes, offset));
}
size_t offset = floor_sections.objects + sizeof(SectionHeader);
ret.emplace_back(Map::disassemble_objects_data(r.pgetv(offset, header.data_size), header.data_size, &object_number));
}
if (floor_sections.enemies != 0xFFFFFFFF) {
ret.emplace_back(string_printf(".enemies %zu", floor));
const auto& header = r.pget<SectionHeader>(floor_sections.enemies);
auto sub_r = r.sub(floor_sections.enemies + sizeof(SectionHeader), header.data_size);
while (sub_r.remaining() >= sizeof(EnemyEntry)) {
string e_str = sub_r.get<EnemyEntry>().str();
ret.emplace_back(string_printf("/* entry %zX */ %s", enemy_number++, e_str.c_str()));
}
if (sub_r.remaining()) {
ret.emplace_back("// Warning: enemy section size is not a multiple of enemy entry size");
size_t offset = floor_sections.objects + sizeof(SectionHeader) + r.where();
size_t bytes = r.remaining();
ret.emplace_back(format_data(r.getv(r.remaining()), bytes, offset));
}
size_t offset = floor_sections.enemies + sizeof(SectionHeader);
ret.emplace_back(Map::disassemble_enemies_data(r.pgetv(offset, header.data_size), header.data_size, &enemy_number));
}
// TODO: Add disassembly for these section types
if (floor_sections.wave_events != 0xFFFFFFFF) {
ret.emplace_back(string_printf(".wave_events %zu", floor));
const auto& header = r.pget<SectionHeader>(floor_sections.wave_events);
size_t offset = floor_sections.wave_events + sizeof(SectionHeader);
auto sub_r = r.sub(offset, header.data_size);
ret.emplace_back(format_data(r.getv(r.remaining()), header.data_size, offset));
ret.emplace_back(Map::disassemble_wave_events_data(r.pgetv(offset, header.data_size), header.data_size, floor));
}
if (floor_sections.random_enemy_locations != 0xFFFFFFFF) {
ret.emplace_back(string_printf(".random_enemy_locations %zu", floor));
const auto& header = r.pget<SectionHeader>(floor_sections.random_enemy_locations);
size_t offset = floor_sections.random_enemy_locations + sizeof(SectionHeader);
auto sub_r = r.sub(offset, header.data_size);
ret.emplace_back(format_data(r.getv(r.remaining()), header.data_size, offset));
ret.emplace_back(format_data(sub_r.getv(sub_r.remaining()), header.data_size, offset));
}
if (floor_sections.random_enemy_definitions != 0xFFFFFFFF) {
ret.emplace_back(string_printf(".random_enemy_definitions %zu", floor));
const auto& header = r.pget<SectionHeader>(floor_sections.random_enemy_definitions);
size_t offset = floor_sections.random_enemy_definitions + sizeof(SectionHeader);
auto sub_r = r.sub(offset, header.data_size);
ret.emplace_back(format_data(r.getv(r.remaining()), header.data_size, offset));
ret.emplace_back(format_data(sub_r.getv(sub_r.remaining()), header.data_size, offset));
}
}
@@ -1640,25 +1861,25 @@ string Map::disassemble_quest_data(const void* data, size_t size) {
SetDataTableBase::SetDataTableBase(Version version) : version(version) {}
parray<le_uint32_t, 0x20> SetDataTableBase::generate_variations(
Episode episode, bool is_solo, std::shared_ptr<PSOLFGEncryption> random_crypt) const {
Episode episode, bool is_solo, std::shared_ptr<PSOLFGEncryption> opt_rand_crypt) const {
parray<le_uint32_t, 0x20> ret;
for (size_t floor = 0; floor < 0x10; floor++) {
auto num_vars = this->num_free_roam_variations_for_floor(episode, is_solo, floor);
ret[floor * 2] = (num_vars.first > 1) ? (random_crypt->next() % num_vars.first) : 0;
ret[floor * 2 + 1] = (num_vars.second > 1) ? (random_crypt->next() % num_vars.second) : 0;
ret[floor * 2] = (num_vars.first > 1) ? (random_from_optional_crypt(opt_rand_crypt) % num_vars.first) : 0;
ret[floor * 2 + 1] = (num_vars.second > 1) ? (random_from_optional_crypt(opt_rand_crypt) % num_vars.second) : 0;
}
return ret;
}
vector<string> SetDataTableBase::map_filenames_for_variations(
const parray<le_uint32_t, 0x20>& variations, Episode episode, GameMode mode, bool is_enemies) const {
const parray<le_uint32_t, 0x20>& variations, Episode episode, GameMode mode, FilenameType type) const {
vector<string> ret;
for (uint8_t floor = 0; floor < 0x10; floor++) {
ret.emplace_back(this->map_filename_for_variation(
floor, variations[floor * 2], variations[floor * 2 + 1], episode, mode, is_enemies));
floor, variations[floor * 2], variations[floor * 2 + 1], episode, mode, type));
}
for (uint8_t floor = 0x10; floor < 0x12; floor++) {
ret.emplace_back(this->map_filename_for_variation(floor, 0, 0, episode, mode, is_enemies));
ret.emplace_back(this->map_filename_for_variation(floor, 0, 0, episode, mode, type));
}
return ret;
}
@@ -1732,8 +1953,8 @@ void SetDataTable::load_table_t(const string& data) {
while (!var2_r.eof()) {
auto& entry = var2_v.emplace_back();
entry.object_list_basename = r.pget_cstr(var2_r.get<U32T>());
entry.enemy_list_basename = r.pget_cstr(var2_r.get<U32T>());
entry.event_list_basename = r.pget_cstr(var2_r.get<U32T>());
entry.enemy_and_event_list_basename = r.pget_cstr(var2_r.get<U32T>());
entry.area_setup_filename = r.pget_cstr(var2_r.get<U32T>());
}
}
}
@@ -1744,7 +1965,10 @@ pair<uint32_t, uint32_t> SetDataTable::num_available_variations_for_floor(Episod
if (area == 0xFF) {
return make_pair(1, 1);
} else {
const auto& e = this->entries.at(area);
if (area >= this->entries.size()) {
return make_pair(1, 1);
}
const auto& e = this->entries[area];
return make_pair(e.size(), e.at(0).size());
}
}
@@ -1783,7 +2007,7 @@ pair<uint32_t, uint32_t> SetDataTable::num_free_roam_variations_for_floor(Episod
}
string SetDataTable::map_filename_for_variation(
uint8_t floor, uint32_t var1, uint32_t var2, Episode episode, GameMode mode, bool is_enemies) const {
uint8_t floor, uint32_t var1, uint32_t var2, Episode episode, GameMode mode, FilenameType type) const {
uint8_t area = this->default_area_for_floor(episode, floor);
if (area == 0xFF) {
return "";
@@ -1794,22 +2018,35 @@ string SetDataTable::map_filename_for_variation(
}
const auto& entry = this->entries.at(area).at(var1).at(var2);
string filename = is_enemies ? entry.enemy_list_basename : entry.object_list_basename;
filename += (is_enemies ? "e" : "o");
string filename;
switch (type) {
case FilenameType::OBJECTS:
filename = entry.object_list_basename + "o";
break;
case FilenameType::ENEMIES:
filename = entry.enemy_and_event_list_basename + "e";
break;
case FilenameType::EVENTS:
filename = entry.enemy_and_event_list_basename;
break;
default:
throw logic_error("invalid map filename type");
}
bool is_events = (type == FilenameType::EVENTS);
switch ((floor != 0) ? GameMode::NORMAL : mode) {
case GameMode::NORMAL:
filename += ".dat";
filename += is_events ? ".evt" : ".dat";
break;
case GameMode::SOLO:
filename += "_s.dat";
filename += is_events ? "_s.evt" : "_s.dat";
break;
case GameMode::CHALLENGE:
filename += "_c1.dat";
filename += is_events ? "_c1.evt" : "_c1.dat";
break;
case GameMode::BATTLE:
filename += "_d.dat";
filename += is_events ? "_d.evt" : "_d.dat";
break;
default:
throw logic_error("invalid game mode");
@@ -1820,14 +2057,15 @@ string SetDataTable::map_filename_for_variation(
string SetDataTable::str() const {
vector<string> lines;
lines.emplace_back(string_printf("FL/V1/V2 => ----------------------OBJECT -----------------------ENEMY -----------------------EVENT\n"));
lines.emplace_back(string_printf("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(string_printf("%02zX/%02zX/%02zX => %28s %28s %28s\n", a, v1, v2, e.object_list_basename.c_str(), e.enemy_list_basename.c_str(), e.event_list_basename.c_str()));
lines.emplace_back(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()));
}
}
}
@@ -1848,7 +2086,7 @@ struct AreaMapFileInfo {
variation2_values(variation2_values) {}
};
const array<vector<vector<string>>, 0x10> SetDataTableDCNTE::NAMES = {{
const array<vector<vector<string>>, 0x12> SetDataTableDCNTE::NAMES = {{
/* 00 */ {{"map_city00_00"}},
/* 01 */ {{"map_forest01_00", "map_forest01_01"}},
/* 02 */ {{"map_forest02_00", "map_forest02_03"}},
@@ -1865,12 +2103,15 @@ const array<vector<vector<string>>, 0x10> SetDataTableDCNTE::NAMES = {{
/* 0D */ {{"map_boss03"}},
/* 0E */ {{"map_boss04"}},
/* 0F */ {{"map_visuallobby"}, {"map_visuallobby"}, {"map_visuallobby"}, {"map_visuallobby"}, {"map_visuallobby"}, {"map_visuallobby"}, {"map_visuallobby"}, {"map_visuallobby"}, {"map_visuallobby"}, {"map_visuallobby"}},
/* 10 */ {},
/* 11 */ {},
}};
SetDataTableDCNTE::SetDataTableDCNTE() : SetDataTableBase(Version::DC_NTE) {}
pair<uint32_t, uint32_t> SetDataTableDCNTE::num_available_variations_for_floor(Episode, uint8_t floor) const {
return make_pair(this->NAMES[floor].size(), this->NAMES[floor][0].size());
const auto& floor_names = this->NAMES.at(floor);
return make_pair(floor_names.size(), floor_names.empty() ? 0 : this->NAMES.at(floor)[0].size());
}
pair<uint32_t, uint32_t> SetDataTableDCNTE::num_free_roam_variations_for_floor(Episode episode, bool, uint8_t floor) const {
@@ -1878,14 +2119,29 @@ pair<uint32_t, uint32_t> SetDataTableDCNTE::num_free_roam_variations_for_floor(E
}
string SetDataTableDCNTE::map_filename_for_variation(
uint8_t floor, uint32_t var1, uint32_t var2, Episode, GameMode, bool is_enemies) const {
if (floor >= this->NAMES.size()) {
uint8_t floor, uint32_t var1, uint32_t var2, Episode, GameMode, FilenameType type) const {
try {
string basename = this->NAMES.at(floor).at(var1).at(var2);
switch (type) {
case FilenameType::ENEMIES:
basename += "e.dat";
break;
case FilenameType::OBJECTS:
basename += "o.dat";
break;
case FilenameType::EVENTS:
basename += ".evt";
break;
default:
throw logic_error("invalid map filename type");
}
return basename;
} catch (const out_of_range&) {
return "";
}
return this->NAMES.at(floor).at(var1).at(var2) + (is_enemies ? "e.dat" : "o.dat");
}
const array<vector<vector<string>>, 0x10> SetDataTableDC112000::NAMES = {{
const array<vector<vector<string>>, 0x12> SetDataTableDC112000::NAMES = {{
/* 00 */ {{"map_city00_00"}},
/* 01 */ {{"map_forest01_00", "map_forest01_01", "map_forest01_02", "map_forest01_03", "map_forest01_04"}},
/* 02 */ {{"map_forest02_00", "map_forest02_01", "map_forest02_02", "map_forest02_03", "map_forest02_04"}},
@@ -1902,12 +2158,15 @@ const array<vector<vector<string>>, 0x10> SetDataTableDC112000::NAMES = {{
/* 0D */ {{"map_boss03"}},
/* 0E */ {{"map_boss04"}},
/* 0F */ {{"map_visuallobby"}, {"map_visuallobby"}, {"map_visuallobby"}, {"map_visuallobby"}, {"map_visuallobby"}, {"map_visuallobby"}, {"map_visuallobby"}, {"map_visuallobby"}, {"map_visuallobby"}, {"map_visuallobby"}},
/* 10 */ {},
/* 11 */ {},
}};
SetDataTableDC112000::SetDataTableDC112000() : SetDataTableBase(Version::DC_V1_11_2000_PROTOTYPE) {}
pair<uint32_t, uint32_t> SetDataTableDC112000::num_available_variations_for_floor(Episode, uint8_t floor) const {
return make_pair(this->NAMES[floor].size(), this->NAMES[floor][0].size());
const auto& floor_names = this->NAMES.at(floor);
return make_pair(floor_names.size(), floor_names.empty() ? 0 : this->NAMES.at(floor)[0].size());
}
pair<uint32_t, uint32_t> SetDataTableDC112000::num_free_roam_variations_for_floor(Episode episode, bool, uint8_t floor) const {
@@ -1915,11 +2174,25 @@ pair<uint32_t, uint32_t> SetDataTableDC112000::num_free_roam_variations_for_floo
}
string SetDataTableDC112000::map_filename_for_variation(
uint8_t floor, uint32_t var1, uint32_t var2, Episode, GameMode, bool is_enemies) const {
uint8_t floor, uint32_t var1, uint32_t var2, Episode, GameMode, FilenameType type) const {
if (floor >= this->NAMES.size()) {
return "";
}
return this->NAMES.at(floor).at(var1).at(var2) + (is_enemies ? "e.dat" : "o.dat");
string basename = this->NAMES.at(floor).at(var1).at(var2);
switch (type) {
case FilenameType::ENEMIES:
basename += "e.dat";
break;
case FilenameType::OBJECTS:
basename += "o.dat";
break;
case FilenameType::EVENTS:
basename += ".evt";
break;
default:
throw logic_error("invalid map filename type");
}
return basename;
}
static const vector<AreaMapFileInfo> map_file_info_dc_nte = {
+89 -22
View File
@@ -94,7 +94,7 @@ struct Map {
} __attribute__((packed));
struct EventsSectionHeader { // Section type 3 (WAVE_EVENTS)
/* 00 */ le_uint32_t footer_offset;
/* 00 */ le_uint32_t action_stream_offset;
/* 04 */ le_uint32_t entries_offset;
/* 08 */ le_uint32_t entry_count;
/* 0C */ be_uint32_t format; // 0 or 'evt2'
@@ -103,19 +103,23 @@ struct Map {
struct Event1Entry { // Section type 3 (WAVE_EVENTS) if format == 0
/* 00 */ le_uint32_t event_id;
// Bits in flags:
// 0004 = is active
// 0008 = post-wave actions have been run
// 0010 = all enemies killed
/* 04 */ le_uint16_t flags;
/* 06 */ le_uint16_t unknown_a2;
/* 06 */ le_uint16_t event_type;
/* 08 */ le_uint16_t section;
/* 0A */ le_uint16_t wave_number;
/* 0C */ le_uint32_t delay;
/* 10 */ le_uint32_t clear_events_index;
/* 10 */ le_uint32_t action_stream_offset;
/* 14 */
} __attribute__((packed));
struct Event2Entry { // Section type 3 (WAVE_EVENTS) if format == 'evt2'
/* 00 */ le_uint32_t event_id;
/* 04 */ le_uint16_t flags;
/* 06 */ le_uint16_t unknown_a2;
/* 06 */ le_uint16_t event_type;
/* 08 */ le_uint16_t section;
/* 0A */ le_uint16_t wave_number;
/* 0C */ le_uint16_t min_delay;
@@ -123,7 +127,7 @@ struct Map {
/* 10 */ uint8_t min_enemies;
/* 11 */ uint8_t max_enemies;
/* 12 */ le_uint16_t max_waves;
/* 14 */ le_uint32_t clear_events_index;
/* 14 */ le_uint32_t action_stream_offset;
/* 18 */
} __attribute__((packed));
@@ -208,15 +212,20 @@ struct Map {
// TODO: Add more fields in here if we ever care about them. Currently we
// only care about boxes with fixed item drops.
size_t source_index;
uint16_t object_id;
uint8_t floor;
uint16_t object_id;
uint16_t base_type;
uint16_t section;
uint16_t group;
float param1; // If <= 0, this is a specialized box, and the specialization is in param4/5/6
float param3; // If == 0, the item should be varied by difficulty and area
uint32_t param4;
uint32_t param5;
uint32_t param6;
uint16_t game_flags;
// Technically set_flags shouldn't be part of the Object struct, but all
// object entries always generate exactly one object, so we store it here.
uint16_t set_flags;
bool item_drop_checked;
std::string str() const;
@@ -231,16 +240,39 @@ struct Map {
ITEM_DROPPED = 0x10,
};
size_t source_index;
size_t set_index;
uint16_t enemy_id;
uint16_t total_damage;
uint32_t game_flags; // From 6x0A
uint16_t section;
uint16_t wave_number;
EnemyType type;
uint8_t floor;
uint8_t state_flags;
uint8_t last_hit_by_client_id;
Enemy(uint16_t enemy_id, size_t source_index, uint8_t floor, EnemyType type);
Enemy(
uint16_t enemy_id,
size_t source_index,
size_t set_index,
uint8_t floor,
uint16_t section,
uint16_t wave_number,
EnemyType type);
std::string str() const;
} __attribute__((packed));
};
struct Event {
uint32_t event_id;
uint16_t flags;
uint16_t section;
uint16_t wave_number;
uint8_t floor;
uint32_t action_stream_offset;
std::vector<size_t> enemy_indexes;
std::string str() const;
};
struct DATParserRandomState {
PSOV2Encryption random;
@@ -256,7 +288,7 @@ struct Map {
void generate_shuffled_location_table(const Map::RandomEnemyLocationsHeader& header, StringReader r, uint16_t section);
};
Map(Version version, uint32_t lobby_id, std::shared_ptr<PSOLFGEncryption> random_crypt);
Map(Version version, uint32_t lobby_id, uint32_t rare_seed, std::shared_ptr<PSOLFGEncryption> opt_rand_crypt);
~Map() = default;
void clear();
@@ -291,6 +323,17 @@ struct Map {
std::shared_ptr<DATParserRandomState> random_state,
std::shared_ptr<const RareEnemyRates> rare_rates = DEFAULT_RARE_ENEMIES);
void add_event(
uint32_t event_id,
uint16_t flags,
uint8_t floor,
uint16_t section,
uint16_t wave_number,
uint32_t action_stream_offset);
Event& get_event(uint8_t floor, uint32_t event_id);
const Event& get_event(uint8_t floor, uint32_t event_id) const;
void add_events_from_map_data(uint8_t floor, const void* data, size_t size);
struct DATSectionsForFloor {
uint32_t objects = 0xFFFFFFFF;
uint32_t enemies = 0xFFFFFFFF;
@@ -300,7 +343,7 @@ struct Map {
};
static std::vector<DATSectionsForFloor> collect_quest_map_data_sections(const void* data, size_t size);
void add_enemies_and_objects_from_quest_data(
void add_entities_from_quest_data(
Episode episode,
uint8_t difficulty,
uint8_t event,
@@ -310,29 +353,53 @@ struct Map {
const Enemy& find_enemy(uint8_t floor, EnemyType type) const;
Enemy& find_enemy(uint8_t floor, EnemyType type);
std::vector<Object*> get_objects(uint8_t floor, uint16_t section, uint16_t wave_number);
std::vector<Enemy*> get_enemies(uint8_t floor, uint16_t section, uint16_t wave_number);
std::vector<Event*> get_events(uint8_t floor, uint16_t section, uint16_t wave_number);
std::vector<Event*> get_events(uint8_t floor);
static std::string disassemble_objects_data(const void* data, size_t size, size_t* object_number = nullptr);
static std::string disassemble_enemies_data(const void* data, size_t size, size_t* enemy_number = nullptr);
static std::string disassemble_wave_events_data(const void* data, size_t size, uint8_t floor = 0xFF);
static std::string disassemble_quest_data(const void* data, size_t size);
PrefixedLogger log;
Version version;
std::shared_ptr<PSOLFGEncryption> random_crypt;
uint32_t rare_seed;
std::shared_ptr<PSOLFGEncryption> opt_rand_crypt;
std::vector<Object> objects;
std::vector<Enemy> enemies;
std::vector<uint16_t> enemy_set_flags;
std::vector<size_t> rare_enemy_indexes;
std::vector<Event> events;
std::string event_action_stream;
std::map<uint64_t, size_t> floor_and_event_id_to_index;
std::unordered_multimap<uint64_t, size_t> floor_section_and_group_to_object_index;
std::unordered_multimap<uint64_t, size_t> floor_section_and_wave_number_to_enemy_index;
std::unordered_multimap<uint64_t, size_t> floor_section_and_wave_number_to_event_index;
};
class SetDataTableBase {
public:
virtual ~SetDataTableBase() = default;
parray<le_uint32_t, 0x20> generate_variations(Episode episode, bool is_solo, std::shared_ptr<PSOLFGEncryption> random_crypt) const;
parray<le_uint32_t, 0x20> generate_variations(
Episode episode,
bool is_solo,
std::shared_ptr<PSOLFGEncryption> opt_rand_crypt = nullptr) const;
virtual std::pair<uint32_t, uint32_t> num_available_variations_for_floor(Episode episode, uint8_t floor) const = 0;
virtual std::pair<uint32_t, uint32_t> num_free_roam_variations_for_floor(Episode episode, bool is_solo, uint8_t floor) const = 0;
enum class FilenameType {
OBJECTS = 0,
ENEMIES,
EVENTS,
};
virtual std::string map_filename_for_variation(
uint8_t floor, uint32_t var1, uint32_t var2, Episode episode, GameMode mode, bool is_enemies) const = 0;
uint8_t floor, uint32_t var1, uint32_t var2, Episode episode, GameMode mode, FilenameType type) const = 0;
std::vector<std::string> map_filenames_for_variations(
const parray<le_uint32_t, 0x20>& variations, Episode episode, GameMode mode, bool is_enemies) const;
const parray<le_uint32_t, 0x20>& variations, Episode episode, GameMode mode, FilenameType type) const;
uint8_t default_area_for_floor(Episode episode, uint8_t floor) const;
@@ -346,8 +413,8 @@ class SetDataTable : public SetDataTableBase {
public:
struct SetEntry {
std::string object_list_basename;
std::string enemy_list_basename;
std::string event_list_basename;
std::string enemy_and_event_list_basename;
std::string area_setup_filename;
};
SetDataTable(Version version, const std::string& data);
@@ -356,7 +423,7 @@ public:
virtual std::pair<uint32_t, uint32_t> num_available_variations_for_floor(Episode episode, uint8_t floor) const;
virtual std::pair<uint32_t, uint32_t> num_free_roam_variations_for_floor(Episode episode, bool is_solo, uint8_t floor) const;
virtual std::string map_filename_for_variation(
uint8_t floor, uint32_t var1, uint32_t var2, Episode episode, GameMode mode, bool is_enemies) const;
uint8_t floor, uint32_t var1, uint32_t var2, Episode episode, GameMode mode, FilenameType type) const;
std::string str() const;
@@ -377,10 +444,10 @@ public:
virtual std::pair<uint32_t, uint32_t> num_available_variations_for_floor(Episode episode, uint8_t floor) const;
virtual std::pair<uint32_t, uint32_t> num_free_roam_variations_for_floor(Episode episode, bool is_solo, uint8_t floor) const;
virtual std::string map_filename_for_variation(
uint8_t floor, uint32_t var1, uint32_t var2, Episode episode, GameMode mode, bool is_enemies) const;
uint8_t floor, uint32_t var1, uint32_t var2, Episode episode, GameMode mode, FilenameType type) const;
private:
static const std::array<std::vector<std::vector<std::string>>, 0x10> NAMES;
static const std::array<std::vector<std::vector<std::string>>, 0x12> NAMES;
};
class SetDataTableDC112000 : public SetDataTableBase {
@@ -391,10 +458,10 @@ public:
virtual std::pair<uint32_t, uint32_t> num_available_variations_for_floor(Episode episode, uint8_t floor) const;
virtual std::pair<uint32_t, uint32_t> num_free_roam_variations_for_floor(Episode episode, bool is_solo, uint8_t floor) const;
virtual std::string map_filename_for_variation(
uint8_t floor, uint32_t var1, uint32_t var2, Episode episode, GameMode mode, bool is_enemies) const;
uint8_t floor, uint32_t var1, uint32_t var2, Episode episode, GameMode mode, FilenameType type) const;
private:
static const std::array<std::vector<std::vector<std::string>>, 0x10> NAMES;
static const std::array<std::vector<std::vector<std::string>>, 0x12> NAMES;
};
void generate_variations_deprecated(
+6
View File
@@ -14,6 +14,7 @@
namespace MenuID {
constexpr uint32_t MAIN = 0x11000011;
constexpr uint32_t CLEAR_LICENSE_CONFIRMATION = 0x11111111;
constexpr uint32_t INFORMATION = 0x22000022;
constexpr uint32_t LOBBY = 0x33000033;
constexpr uint32_t GAME = 0x44000044;
@@ -40,6 +41,11 @@ constexpr uint32_t DISCONNECT = 0x11888811;
constexpr uint32_t CLEAR_LICENSE = 0x11999911;
} // namespace MainMenuItemID
namespace ClearLicenseConfirmationMenuItemID {
constexpr uint32_t CANCEL = 0x01010101;
constexpr uint32_t CLEAR_LICENSE = 0x02020202;
} // namespace ClearLicenseConfirmationMenuItemID
namespace InformationMenuItemID {
constexpr uint32_t GO_BACK = 0x22FFFF22;
}
+14
View File
@@ -88,3 +88,17 @@ string string_for_address(uint32_t address) {
uint32_t address_for_string(const char* address) {
return ntohl(inet_addr(address));
}
uint64_t devolution_phone_number_for_netloc(uint32_t addr, uint16_t port) {
// It seems the address part of the number is fixed-width, but the port is
// not. Why did they do it this way?
if (port & 0xF000) {
return (static_cast<uint64_t>(addr) << 16) | port;
} else if (port & 0x0F00) {
return (static_cast<uint64_t>(addr) << 12) | port;
} else if (port & 0x00F0) {
return (static_cast<uint64_t>(addr) << 8) | port;
} else {
return (static_cast<uint64_t>(addr) << 4) | port;
}
}
+2
View File
@@ -17,3 +17,5 @@ bool is_local_address(const sockaddr_storage& daddr);
std::string string_for_address(uint32_t address);
uint32_t address_for_string(const char* address);
uint64_t devolution_phone_number_for_netloc(uint32_t addr, uint16_t port);
+5
View File
@@ -5,6 +5,7 @@
#include <memory>
#include <phosg/Encoding.hh>
#include <phosg/Random.hh>
#include <stdexcept>
#include <string>
#include <vector>
@@ -342,3 +343,7 @@ 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<PSOLFGEncryption> random_crypt) {
return random_crypt ? random_crypt->next() : random_object<uint32_t>();
}
+453
View File
@@ -0,0 +1,453 @@
#include "PatchServer.hh"
#include <arpa/inet.h>
#include <ctype.h>
#include <errno.h>
#include <event2/buffer.h>
#include <event2/bufferevent.h>
#include <event2/event.h>
#include <event2/listener.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <algorithm>
#include <iostream>
#include <phosg/Encoding.hh>
#include <phosg/Network.hh>
#include <phosg/Strings.hh>
#include <phosg/Time.hh>
#include "EventUtils.hh"
#include "Loggers.hh"
#include "PSOProtocol.hh"
#include "ReceiveCommands.hh"
using namespace std;
static atomic<uint64_t> next_id(1);
PatchServer::Client::Client(
shared_ptr<PatchServer> server,
struct bufferevent* bev,
Version version,
uint64_t idle_timeout_usecs,
bool hide_data_from_logs)
: server(server),
id(next_id++),
log(string_printf("[C-%" PRIX64 "] ", this->id), client_log.min_level),
channel(bev, version, 1, nullptr, nullptr, this, string_printf("C-%" PRIX64, this->id), TerminalFormat::FG_YELLOW, TerminalFormat::FG_GREEN),
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 = TerminalFormat::END;
this->channel.terminal_send_color = TerminalFormat::END;
}
this->log.info("Created");
}
void PatchServer::Client::reschedule_timeout_event() {
struct timeval idle_tv = 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<Client*>(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<Client> c) const {
uint32_t server_key = random_object<uint32_t>();
uint32_t client_key = random_object<uint32_t>();
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<PSOV2Encryption>(server_key);
c->channel.crypt_in = make_shared<PSOV2Encryption>(client_key);
}
void PatchServer::send_message_box(shared_ptr<Client> c, const string& text) const {
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) {
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<Client> c, const string& dir) const {
S_EnterDirectory_Patch_09 cmd = {{dir, 1}};
c->channel.send(0x09, 0x00, cmd);
}
void PatchServer::on_02(shared_ptr<Client> 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<Client> c,
vector<string>& client_path_directories,
const vector<string>& 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<Client> c, string& data) {
const auto& cmd = check_size_t<C_Login_Patch_04>(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 license
// - Username only: check that license exists if allow_unregistered_users is off
// - Username and password: call verify_bb
if (!username.empty() && !password.empty()) {
try {
this->config->license_index->verify_bb(username, password);
} catch (const LicenseIndex::incorrect_password& e) {
this->send_message_box(c, string_printf("Login failed: %s", e.what()));
this->disconnect_client(c);
return;
} catch (const LicenseIndex::missing_license& e) {
if (!this->config->allow_unregistered_users) {
this->send_message_box(c, string_printf("Login failed: %s", e.what()));
this->disconnect_client(c);
return;
}
}
} else if (!username.empty() && !this->config->allow_unregistered_users) {
try {
this->config->license_index->get_by_bb_username(username);
} catch (const LicenseIndex::missing_license& e) {
this->send_message_box(c, string_printf("Login failed: %s", e.what()));
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<string> 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<Client> c, string& data) {
auto& cmd = check_size_t<C_FileInformation_Patch_0F>(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<Client> 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<string> 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<uint32_t>(req.file->size - (x * 0x4000), 0x4000);
vector<pair<const void*, size_t>> 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<Client> c) {
if (c->channel.is_virtual_connection) {
server_log.info("Client disconnected: C-%" PRIX64 " on virtual connection %p", c->id, c->channel.bev.get());
} else if (c->channel.bev) {
server_log.info("Client disconnected: C-%" PRIX64 " on fd %d", c->id, bufferevent_getfd(c->channel.bev.get()));
} else {
server_log.info("Client C-%" PRIX64 " removed from game 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 = 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<PatchServer*>(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<PatchServer*>(ctx)->on_listen_accept(listener, fd, address, socklen);
}
void PatchServer::dispatch_on_listen_error(
struct evconnlistener* listener, void* ctx) {
reinterpret_cast<PatchServer*>(ctx)->on_listen_error(listener);
}
void PatchServer::on_listen_accept(struct evconnlistener* listener, evutil_socket_t fd, struct sockaddr*, int) {
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<Client>(
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: U-%" 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<PatchServer*>(ch.context_obj);
shared_ptr<Client> 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<PatchServer*>(ch.context_obj);
shared_ptr<Client> 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<const Config> config)
: base(event_base_new(), event_base_free),
config(config),
destroy_clients_ev(event_new(this->base.get(), -1, EV_TIMEOUT, &PatchServer::dispatch_destroy_clients, this), event_free),
th(&PatchServer::thread_fn, this) {}
void PatchServer::schedule_stop() {
event_base_loopexit(this->base.get(), nullptr);
}
void PatchServer::wait_for_stop() {
this->th.join();
}
void PatchServer::listen(const std::string& addr_str, const string& socket_path, Version version) {
int fd = ::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 = ::listen(addr, port, SOMAXCONN);
string netloc_str = 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<const Config> config) {
forward_to_event_thread(this->base, [s = this->shared_from_this(), config = std::move(config)]() {
s->config = config;
});
}
+127
View File
@@ -0,0 +1,127 @@
#pragma once
#include <event2/event.h>
#include <stdint.h>
#include <memory>
#include <string>
#include <unordered_set>
#include <vector>
#include "Channel.hh"
#include "License.hh"
#include "PatchFileIndex.hh"
#include "Version.hh"
class PatchServer : public std::enable_shared_from_this<PatchServer> {
public:
struct Config {
bool allow_unregistered_users;
bool hide_data_from_logs;
uint64_t idle_timeout_usecs;
std::string message;
std::shared_ptr<const LicenseIndex> license_index;
std::shared_ptr<const PatchFileIndex> patch_file_index;
};
PatchServer() = delete;
explicit PatchServer(std::shared_ptr<const Config> 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<const Config> config);
private:
class Client : public std::enable_shared_from_this<Client> {
public:
std::weak_ptr<PatchServer> server;
uint64_t id;
PrefixedLogger log;
Channel channel;
std::vector<PatchFileChecksumRequest> patch_file_checksum_requests;
uint64_t idle_timeout_usecs;
std::unique_ptr<struct event, void (*)(struct event*)> idle_timeout_event;
Client(
std::shared_ptr<PatchServer> 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();
const std::string& get_bb_username() const;
void set_bb_username(const std::string& bb_username);
};
struct ListeningSocket {
std::string addr_str;
int fd;
Version version;
std::unique_ptr<struct evconnlistener, void (*)(struct evconnlistener*)> listener;
ListeningSocket(PatchServer* s, const std::string& name, int fd, Version version);
};
std::shared_ptr<struct event_base> base;
std::shared_ptr<const Config> config;
std::unordered_set<std::shared_ptr<Client>> clients_to_destroy;
std::shared_ptr<struct event> destroy_clients_ev;
std::unordered_map<int, ListeningSocket> listening_sockets;
std::unordered_map<Channel*, std::shared_ptr<Client>> channel_to_client;
std::thread th;
void send_server_init(std::shared_ptr<Client> c) const;
void send_message_box(std::shared_ptr<Client> c, const std::string& text) const;
void send_enter_directory(std::shared_ptr<Client> c, const std::string& dir) const;
void change_to_directory(
std::shared_ptr<Client> c,
std::vector<std::string>& client_path_directories,
const std::vector<std::string>& file_path_directories) const;
void on_02(std::shared_ptr<Client> c, std::string& data);
void on_04(std::shared_ptr<Client> c, std::string& data);
void on_0F(std::shared_ptr<Client> c, std::string& data);
void on_10(std::shared_ptr<Client> c, std::string& data);
void disconnect_client(std::shared_ptr<Client> 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();
};
+62 -33
View File
@@ -90,7 +90,20 @@ void PlayerVisualConfig::enforce_lobby_join_limits_for_version(Version v) {
{0x0000, 0x0000, 0x0000, 0x0000, 0x0000}};
const ClassMaxes* maxes;
if (is_v1_or_v2(v)) {
if (v == Version::GC_NTE) {
// GC NTE has HUcaseal, FOmar, and RAmarl, but missing others
if (this->char_class >= 12) {
this->char_class = 0; // Invalid classes -> HUmar
}
// GC NTE is basically v2, but uses v3 maxes
this->version = min<uint8_t>(this->version, 2);
maxes = &v3_v4_class_maxes[this->char_class];
// Prevent GC NTE from crashing from extra models
this->extra_model = 0;
this->validation_flags &= 0xFD;
} else if (is_v1_or_v2(v)) {
// V1/V2 have fewer classes, so we'll substitute some here
switch (this->char_class) {
case 0: // HUmar
@@ -152,10 +165,7 @@ void PlayerVisualConfig::enforce_lobby_join_limits_for_version(Version v) {
this->name_color_checksum = 0;
}
this->class_flags = class_flags_for_class(this->char_class);
if (!is_v4(v) && (this->name.at(0) == '\t') && (this->name.at(1) == 'J' || this->name.at(1) == 'E')) {
this->name.encode(this->name.decode().substr(2));
}
this->name.clear_after_bytes(0x0C);
}
void PlayerDispDataDCPCV3::enforce_lobby_join_limits_for_version(Version v) {
@@ -164,13 +174,7 @@ void PlayerDispDataDCPCV3::enforce_lobby_join_limits_for_version(Version v) {
void PlayerDispDataBB::enforce_lobby_join_limits_for_version(Version v) {
this->visual.enforce_lobby_join_limits_for_version(v);
if (!is_v4(v)) {
throw logic_error("PlayerDispDataBB being sent to non-BB client");
}
this->play_time = 0;
if (this->name.at(0) != '\t' || (this->name.at(1) != 'E' && this->name.at(1) != 'J')) {
this->name.encode("\tJ" + this->name.decode());
}
this->name.clear_after_bytes(0x18); // 12 characters
}
PlayerDispDataBB PlayerDispDataDCPCV3::to_bb(uint8_t to_language, uint8_t from_language) const {
@@ -196,16 +200,6 @@ PlayerDispDataDCPCV3 PlayerDispDataBB::to_dcpcv3(uint8_t to_language, uint8_t fr
return ret;
}
PlayerDispDataBBPreview PlayerDispDataBB::to_preview() const {
PlayerDispDataBBPreview pre;
pre.level = this->stats.level;
pre.experience = this->stats.experience;
pre.visual = this->visual;
pre.name = this->name;
pre.play_time = this->play_time;
return pre;
}
void PlayerDispDataBB::apply_dressing_room(const PlayerDispDataBBPreview& pre) {
this->visual.name_color = pre.visual.name_color;
this->visual.extra_model = pre.visual.extra_model;
@@ -330,7 +324,7 @@ PlayerRecordsBB_Challenge::PlayerRecordsBB_Challenge(const PlayerRecordsPC_Chall
grave_message(rec.grave_message.decode(), 1),
unknown_m5(0),
unknown_t6(0),
rank_title(rec.rank_title),
rank_title(rec.rank_title.decode(), 1),
unknown_l7(0) {}
PlayerRecordsBB_Challenge::PlayerRecordsBB_Challenge(const PlayerRecordsV3_Challenge<false>& rec)
@@ -394,7 +388,7 @@ PlayerRecordsBB_Challenge::operator PlayerRecordsPC_Challenge() const {
PlayerRecordsPC_Challenge ret;
ret.title_color = this->title_color;
ret.unknown_u0 = this->unknown_u0;
ret.rank_title = this->rank_title;
ret.rank_title.encode(this->rank_title.decode());
ret.times_ep1_online = this->times_ep1_online;
if (this->grave_is_ep2) {
ret.grave_stage_num = 0;
@@ -450,7 +444,7 @@ PlayerRecordsBB_Challenge::operator PlayerRecordsV3_Challenge<false>() const {
return ret;
}
void PlayerBank::add_item(const ItemData& item, Version version) {
void PlayerBank::add_item(const ItemData& item, const ItemData::StackLimits& limits) {
uint32_t primary_identifier = item.primary_identifier();
if (primary_identifier == 0x04000000) {
@@ -461,7 +455,7 @@ void PlayerBank::add_item(const ItemData& item, Version version) {
return;
}
size_t combine_max = item.max_stack_size(version);
size_t combine_max = item.max_stack_size(limits);
if (combine_max > 1) {
size_t y;
for (y = 0; y < this->num_items; y++) {
@@ -486,17 +480,17 @@ void PlayerBank::add_item(const ItemData& item, Version version) {
}
auto& last_item = this->items[this->num_items];
last_item.data = item;
last_item.amount = (item.max_stack_size(version) > 1) ? item.data1[5] : 1;
last_item.amount = (item.max_stack_size(limits) > 1) ? item.data1[5] : 1;
last_item.present = 1;
this->num_items++;
}
ItemData PlayerBank::remove_item(uint32_t item_id, uint32_t amount, Version version) {
ItemData PlayerBank::remove_item(uint32_t item_id, uint32_t amount, const ItemData::StackLimits& limits) {
size_t index = this->find_item(item_id);
auto& bank_item = this->items[index];
ItemData ret;
if (amount && (bank_item.data.stack_size(version) > 1) && (amount < bank_item.data.data1[5])) {
if (amount && (bank_item.data.stack_size(limits) > 1) && (amount < bank_item.data.data1[5])) {
ret = bank_item.data;
ret.data1[5] = amount;
bank_item.data.data1[5] -= amount;
@@ -582,13 +576,10 @@ void PlayerInventory::equip_item_id(uint32_t item_id, EquipSlot slot, bool allow
void PlayerInventory::equip_item_index(size_t index, EquipSlot slot, bool allow_overwrite) {
auto& item = this->items[index];
if (slot == EquipSlot::UNKNOWN) {
if ((slot == EquipSlot::UNKNOWN) || !item.data.can_be_equipped_in_slot(slot)) {
slot = item.data.default_equip_slot();
}
if (!item.data.can_be_equipped_in_slot(slot)) {
throw runtime_error("incorrect item type for equip slot");
}
if (this->has_equipped_item(slot)) {
if (allow_overwrite) {
this->unequip_item_slot(slot);
@@ -706,6 +697,21 @@ void PlayerBank::assign_ids(uint32_t base_id) {
}
}
QuestFlagsV1& QuestFlagsV1::operator=(const QuestFlags& other) {
this->data[0] = other.data[0];
this->data[1] = other.data[1];
this->data[2] = other.data[2];
return *this;
}
QuestFlagsV1::operator QuestFlags() const {
QuestFlags ret;
ret.data[0] = this->data[0];
ret.data[1] = this->data[1];
ret.data[2] = this->data[2];
return ret;
}
BattleRules::BattleRules(const JSON& json) {
static const JSON empty_list = JSON::list();
@@ -1085,3 +1091,26 @@ SymbolChat::SymbolChat()
: spec(0),
corner_objects(0x00FF),
face_parts() {}
void RecentSwitchFlags::add(uint16_t flag_num) {
if ((flag_num != ((this->flag_nums >> 48) & 0xFFFF)) &&
(flag_num != ((this->flag_nums >> 32) & 0xFFFF)) &&
(flag_num != ((this->flag_nums >> 16) & 0xFFFF)) &&
(flag_num != (this->flag_nums & 0xFFFF))) {
this->flag_nums = this->flag_nums << 16 | flag_num;
}
}
string RecentSwitchFlags::enable_commands(uint8_t floor) const {
StringWriter w;
uint64_t flag_nums = this->flag_nums;
for (size_t z = 0; z < 4; z++) {
uint16_t flag_num = flag_nums;
if (flag_num == 0xFFFF) {
continue;
}
w.put(G_SwitchStateChanged_6x05{{0x05, 0x03, 0xFFFF}, 0, 0, flag_num, static_cast<uint8_t>(floor), 0x01});
flag_nums >>= 16;
}
return std::move(w.str());
}
+75 -31
View File
@@ -101,8 +101,8 @@ struct PlayerBank {
/* 0008 */ parray<PlayerBankItem, 200> items;
/* 12C8 */
void add_item(const ItemData& item, Version version);
ItemData remove_item(uint32_t item_id, uint32_t amount, Version version);
void add_item(const ItemData& item, const ItemData::StackLimits& limits);
ItemData remove_item(uint32_t item_id, uint32_t amount, const ItemData::StackLimits& limits);
size_t find_item(uint32_t item_id);
void sort();
@@ -174,8 +174,8 @@ struct PlayerDispDataBBPreview {
// The name field in this structure is used for the player's Guild Card
// number, apparently (possibly because it's a char array and this is BB)
/* 08 */ PlayerVisualConfig visual;
/* 58 */ pstring<TextEncoding::UTF16, 0x10> name;
/* 78 */ uint32_t play_time = 0;
/* 58 */ pstring<TextEncoding::UTF16_ALWAYS_MARKED, 0x10> name;
/* 78 */ uint32_t play_time_seconds = 0;
/* 7C */
} __attribute__((packed));
@@ -183,20 +183,30 @@ struct PlayerDispDataBBPreview {
struct PlayerDispDataBB {
/* 0000 */ PlayerStats stats;
/* 0024 */ PlayerVisualConfig visual;
/* 0074 */ pstring<TextEncoding::UTF16, 0x0C> name;
/* 008C */ le_uint32_t play_time = 0;
/* 0090 */ uint32_t unknown_a3 = 0;
/* 0074 */ pstring<TextEncoding::UTF16_ALWAYS_MARKED, 0x10> name;
/* 0094 */ parray<uint8_t, 0xE8> config;
/* 017C */ parray<uint8_t, 0x14> technique_levels_v1;
/* 0190 */
void enforce_lobby_join_limits_for_version(Version v);
PlayerDispDataDCPCV3 to_dcpcv3(uint8_t to_language, uint8_t from_language) const;
PlayerDispDataBBPreview to_preview() const;
void apply_preview(const PlayerDispDataBBPreview&);
void apply_dressing_room(const PlayerDispDataBBPreview&);
} __attribute__((packed));
struct GuildCardDCNTE {
/* 00 */ le_uint32_t player_tag = 0;
/* 04 */ le_uint32_t guild_card_number = 0;
/* 08 */ pstring<TextEncoding::ASCII, 0x18> name;
/* 20 */ pstring<TextEncoding::MARKED, 0x48> description;
/* 68 */ parray<uint8_t, 0x0F> unused2;
/* 77 */ uint8_t present = 0;
/* 78 */ uint8_t language = 0;
/* 79 */ uint8_t section_id = 0;
/* 7A */ uint8_t char_class = 0;
/* 7B */
} __attribute__((packed));
struct GuildCardDC {
/* 00 */ le_uint32_t player_tag = 0;
/* 04 */ le_uint32_t guild_card_number = 0;
@@ -251,8 +261,8 @@ struct GuildCardXB {
struct GuildCardBB {
/* 0000 */ le_uint32_t guild_card_number = 0;
/* 0004 */ pstring<TextEncoding::UTF16, 0x18> name;
/* 0034 */ pstring<TextEncoding::UTF16, 0x10> team_name;
/* 0004 */ pstring<TextEncoding::UTF16_ALWAYS_MARKED, 0x18> name;
/* 0034 */ pstring<TextEncoding::UTF16_ALWAYS_MARKED, 0x10> team_name;
/* 0054 */ pstring<TextEncoding::UTF16, 0x58> description;
/* 0104 */ uint8_t present = 0;
/* 0105 */ uint8_t language = 0;
@@ -320,7 +330,7 @@ struct PlayerLobbyDataBB {
/* 0C */ le_uint32_t team_id = 0;
/* 10 */ parray<uint8_t, 0x0C> unknown_a1;
/* 1C */ le_uint32_t client_id = 0;
/* 20 */ pstring<TextEncoding::UTF16, 0x10> name;
/* 20 */ pstring<TextEncoding::UTF16_ALWAYS_MARKED, 0x10> name;
// If this field is zero, the "Press F1 for help" prompt appears in the corner
// of the screen in the lobby and on Pioneer 2.
/* 40 */ le_uint32_t hide_help_prompt = 1;
@@ -338,13 +348,14 @@ struct ChallengeAwardState {
template <TextEncoding UnencryptedEncoding, TextEncoding EncryptedEncoding>
struct PlayerRecordsDCPC_Challenge {
/* 00 */ le_uint16_t title_color = 0x7FFF;
/* 02 */ parray<uint8_t, 2> unknown_u0;
/* 04 */ pstring<EncryptedEncoding, 0x0C> rank_title;
/* 10 */ parray<ChallengeTime<false>, 9> times_ep1_online; // TODO: This might be offline times
/* 34 */ uint8_t grave_stage_num = 0;
/* 35 */ uint8_t grave_floor = 0;
/* 36 */ le_uint16_t grave_deaths = 0;
/* DC:PC */
/* 00:00 */ le_uint16_t title_color = 0x7FFF;
/* 02:02 */ parray<uint8_t, 2> unknown_u0;
/* 04:04 */ pstring<EncryptedEncoding, 0x0C> rank_title;
/* 10:1C */ parray<ChallengeTime<false>, 9> times_ep1_online; // TODO: This might be offline times
/* 34:40 */ uint8_t grave_stage_num = 0;
/* 35:41 */ uint8_t grave_floor = 0;
/* 36:42 */ le_uint16_t grave_deaths = 0;
// grave_time is encoded with the following bit fields:
// YYYYMMMM DDDDDDDD HHHHHHHH mmmmmmmm
// Y = year after 2000 (clamped to [0, 15])
@@ -352,22 +363,22 @@ struct PlayerRecordsDCPC_Challenge {
// D = day
// H = hour
// m = minute
/* 38 */ le_uint32_t grave_time = 0;
/* 3C */ le_uint32_t grave_defeated_by_enemy_rt_index = 0;
/* 40 */ le_float grave_x = 0.0f;
/* 44 */ le_float grave_y = 0.0f;
/* 48 */ le_float grave_z = 0.0f;
/* 4C */ pstring<UnencryptedEncoding, 0x14> grave_team;
/* 60 */ pstring<UnencryptedEncoding, 0x18> grave_message;
/* 78 */ parray<ChallengeTime<false>, 9> times_ep1_offline; // TODO: This might be online times
/* 9C */ parray<uint8_t, 4> unknown_l4;
/* A0 */
/* 38:44 */ le_uint32_t grave_time = 0;
/* 3C:48 */ le_uint32_t grave_defeated_by_enemy_rt_index = 0;
/* 40:4C */ le_float grave_x = 0.0f;
/* 44:50 */ le_float grave_y = 0.0f;
/* 48:54 */ le_float grave_z = 0.0f;
/* 4C:58 */ pstring<UnencryptedEncoding, 0x14> grave_team;
/* 60:80 */ pstring<UnencryptedEncoding, 0x18> grave_message;
/* 78:B0 */ parray<ChallengeTime<false>, 9> times_ep1_offline; // TODO: This might be online times
/* 9C:D4 */ parray<uint8_t, 4> unknown_l4;
/* A0:D8 */
} __attribute__((packed));
struct PlayerRecordsDC_Challenge : PlayerRecordsDCPC_Challenge<TextEncoding::CHALLENGE8, TextEncoding::ASCII> {
struct PlayerRecordsDC_Challenge : PlayerRecordsDCPC_Challenge<TextEncoding::ASCII, TextEncoding::CHALLENGE8> {
} __attribute__((packed));
struct PlayerRecordsPC_Challenge : PlayerRecordsDCPC_Challenge<TextEncoding::CHALLENGE16, TextEncoding::UTF16> {
struct PlayerRecordsPC_Challenge : PlayerRecordsDCPC_Challenge<TextEncoding::UTF16, TextEncoding::CHALLENGE16> {
} __attribute__((packed));
template <bool IsBigEndian>
@@ -441,7 +452,7 @@ struct PlayerRecordsBB_Challenge {
/* 00F4 */ ChallengeAwardState<false> ep1_online_award_state;
/* 00FC */ ChallengeAwardState<false> ep2_online_award_state;
/* 0104 */ ChallengeAwardState<false> ep1_offline_award_state;
/* 010C */ pstring<TextEncoding::UTF16, 0x0C> rank_title; // Encrypted; see decrypt_challenge_rank_text
/* 010C */ pstring<TextEncoding::CHALLENGE16, 0x0C> rank_title;
/* 0124 */ parray<uint8_t, 0x1C> unknown_l7;
/* 0140 */
@@ -547,6 +558,27 @@ struct QuestFlags {
}
} __attribute__((packed));
struct QuestFlagsV1 {
parray<QuestFlagsForDifficulty, 3> data;
QuestFlagsV1& operator=(const QuestFlags& other);
operator QuestFlags() const;
} __attribute__((packed));
struct SwitchFlags {
parray<parray<uint8_t, 0x20>, 0x12> data;
inline bool get(uint8_t floor, uint16_t flag_num) const {
return this->data[floor][flag_num >> 3] & (0x80 >> (flag_num & 7));
}
inline void set(uint8_t floor, uint16_t flag_num) {
this->data[floor][flag_num >> 3] |= (0x80 >> (flag_num & 7));
}
inline void clear(uint8_t floor, uint16_t flag_num) {
this->data[floor][flag_num >> 3] &= ~(0x80 >> (flag_num & 7));
}
} __attribute__((packed));
struct BattleRules {
enum class TechDiskMode : uint8_t {
ALLOW = 0,
@@ -701,3 +733,15 @@ struct SymbolChat {
SymbolChat();
} __attribute__((packed));
struct RecentSwitchFlags {
uint64_t flag_nums = 0xFFFFFFFFFFFFFFFF;
inline void clear() {
this->flag_nums = 0xFFFFFFFFFFFFFFFF;
}
void add(uint16_t flag_num);
std::string enable_commands(uint8_t floor) const;
};
+261 -89
View File
@@ -76,18 +76,6 @@ static void forward_command(shared_ptr<ProxyServer::LinkedSession> ses, bool to_
}
}
static void check_implemented_subcommand(
shared_ptr<ProxyServer::LinkedSession> ses, const string& data) {
if (data.size() < 4) {
ses->log.warning("Received broadcast/target command with no contents");
} else {
if (!subcommand_is_implemented(data[0])) {
ses->log.warning("Received subcommand %02hhX which is not implemented on the server",
data[0]);
}
}
}
// Command handlers. These are called to preprocess or react to specific
// commands in either direction. The functions have abbreviated names in order
// to make the massive table more readable. The functions' names are, in
@@ -530,7 +518,7 @@ static HandlerResult S_V123_04(shared_ptr<ProxyServer::LinkedSession> ses, uint1
ses->log.info("Remote guild card number set to %" PRId64,
ses->remote_guild_card_number);
string message = string_printf(
"The remote server\nhas assigned your\nGuild Card number:\n\tC6%" PRId64,
"The remote server\nhas assigned your\nGuild Card number:\n$C6%" PRId64,
ses->remote_guild_card_number);
send_ship_info(ses->client_channel, message);
}
@@ -954,6 +942,78 @@ static HandlerResult S_V3_BB_DA(shared_ptr<ProxyServer::LinkedSession> ses, uint
}
}
static HandlerResult SC_6x60_6xA2(shared_ptr<ProxyServer::LinkedSession> ses, const string& data) {
if (!ses->is_in_game) {
return HandlerResult::Type::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++;
bool is_box = (cmd.rt_index == 0x30);
send_drop_item_to_channel(s, ses->server_channel, ses->next_drop_item, !is_box, cmd.floor, cmd.x, cmd.z, cmd.entity_id);
send_drop_item_to_channel(s, ses->client_channel, ses->next_drop_item, !is_box, cmd.floor, cmd.x, cmd.z, cmd.entity_id);
ses->next_drop_item.clear();
return HandlerResult::Type::SUPPRESS;
}
using DropMode = ProxyServer::LinkedSession::DropMode;
switch (ses->drop_mode) {
case DropMode::DISABLED:
return HandlerResult::Type::SUPPRESS;
case DropMode::PASSTHROUGH:
return HandlerResult::Type::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 (!ses->map) {
ses->log.warning("Session is in INTERCEPT drop mode, but map is missing");
return HandlerResult::Type::FORWARD;
}
G_SpecializableItemDropRequest_6xA2 cmd = normalize_drop_request(data.data(), data.size());
auto rec = reconcile_drop_request_with_map(
ses->log, ses->client_channel, cmd, ses->version(), ses->lobby_episode, ses->config, ses->map, false);
ItemCreator::DropResult res;
if (rec.is_box) {
if (rec.ignore_def) {
ses->log.info("Creating item from box %04hX (area %02hX)", cmd.entity_id.load(), cmd.effective_area);
res = ses->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_id.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);
}
} else {
ses->log.info("Creating item from enemy %04hX (area %02hX)", cmd.entity_id.load(), cmd.effective_area);
res = ses->item_creator->on_monster_item_drop(rec.effective_rt_index, cmd.effective_area);
}
if (res.item.empty()) {
ses->log.info("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_id.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.x.load(), cmd.z.load());
send_drop_item_to_channel(s, ses->client_channel, res.item, !rec.is_box, cmd.floor, cmd.x, cmd.z, cmd.entity_id);
send_drop_item_to_channel(s, ses->server_channel, res.item, !rec.is_box, cmd.floor, cmd.x, cmd.z, cmd.entity_id);
send_item_notification_if_needed(s, ses->client_channel, ses->config, res.item, res.is_from_rare_table);
}
return HandlerResult::Type::SUPPRESS;
}
static HandlerResult S_6x(shared_ptr<ProxyServer::LinkedSession> ses, uint16_t, uint32_t, string& data) {
auto s = ses->require_server_state();
@@ -1061,32 +1121,15 @@ static HandlerResult S_6x(shared_ptr<ProxyServer::LinkedSession> ses, uint16_t,
const auto& cmd = check_size_t<G_DropItem_DC_6x5F>(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);
} else if ((data[0] == 0x60) && ses->next_drop_item.data1d[0] && !is_v4(ses->version())) {
const auto& cmd = check_size_t<G_StandardDropItemRequest_DC_6x60>(
data, sizeof(G_StandardDropItemRequest_PC_V3_BB_6x60));
ses->next_drop_item.id = ses->next_item_id++;
send_drop_item_to_channel(s, ses->server_channel, ses->next_drop_item, true, cmd.floor, cmd.x, cmd.z, cmd.entity_id);
send_drop_item_to_channel(s, ses->client_channel, ses->next_drop_item, true, cmd.floor, cmd.x, cmd.z, cmd.entity_id);
ses->next_drop_item.clear();
return HandlerResult::Type::SUPPRESS;
// Note: This static_cast is required to make compilers not complain that
// the comparison is always false (which even happens in some environments
// if we use -0x5E... apparently char is unsigned on some systems, or
// std::string's char_type isn't char??)
} else if ((static_cast<uint8_t>(data[0]) == 0xA2) && ses->next_drop_item.data1d[0] && !is_v4(ses->version())) {
const auto& cmd = check_size_t<G_SpecializableItemDropRequest_6xA2>(data);
ses->next_drop_item.id = ses->next_item_id++;
send_drop_item_to_channel(s, ses->server_channel, ses->next_drop_item, false, cmd.floor, cmd.x, cmd.z, cmd.entity_id);
send_drop_item_to_channel(s, ses->client_channel, ses->next_drop_item, false, cmd.floor, cmd.x, cmd.z, cmd.entity_id);
ses->next_drop_item.clear();
return HandlerResult::Type::SUPPRESS;
} else if ((data[0] == 0x60) || (static_cast<uint8_t>(data[0]) == 0xA2)) {
return SC_6x60_6xA2(ses, data);
} else if ((static_cast<uint8_t>(data[0]) == 0xB5) && is_ep3(ses->version()) && (data.size() > 4)) {
if (data[4] == 0x1A) {
return HandlerResult::Type::SUPPRESS;
} else if (data[4] == 0x36) {
const auto& cmd = check_size_t<G_RecreatePlayer_Ep3_6xB5x36>(data);
auto& cmd = check_size_t<G_RecreatePlayer_Ep3_6xB5x36>(data);
set_mask_for_ep3_game_command(&cmd, sizeof(cmd), 0);
if (ses->is_in_game && (cmd.client_id >= 4)) {
return HandlerResult::Type::SUPPRESS;
}
@@ -1299,6 +1342,21 @@ static HandlerResult S_13_A7(shared_ptr<ProxyServer::LinkedSession> ses, uint16_
} else {
ses->log.info("Download complete for file %s", sf->basename.c_str());
}
if (!sf->is_download && ends_with(sf->basename, ".dat")) {
auto quest_dat_data = make_shared<std::string>(join(sf->blocks));
ses->map = Lobby::load_maps(
ses->version(),
ses->lobby_episode,
ses->lobby_difficulty,
ses->lobby_event,
ses->id,
Map::DEFAULT_RARE_ENEMIES,
ses->lobby_random_seed,
make_shared<PSOV2Encryption>(ses->lobby_random_seed),
quest_dat_data);
}
ses->saving_files.erase(cmd.filename.decode());
}
@@ -1430,7 +1488,13 @@ static HandlerResult S_65_67_68_EB(shared_ptr<ProxyServer::LinkedSession> ses, u
ses->is_in_game = false;
ses->is_in_quest = false;
ses->floor = 0x0F;
ses->difficulty = 0;
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.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
@@ -1448,6 +1512,7 @@ static HandlerResult S_65_67_68_EB(shared_ptr<ProxyServer::LinkedSession> ses, u
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++) {
auto& entry = cmd.entries[x];
@@ -1496,6 +1561,50 @@ constexpr on_command_t S_P_65_67_68 = &S_65_67_68_EB<S_JoinLobby_PC_65_67_68>;
constexpr on_command_t S_X_65_67_68 = &S_65_67_68_EB<S_JoinLobby_XB_65_67_68>;
constexpr on_command_t S_B_65_67_68 = &S_65_67_68_EB<S_JoinLobby_BB_65_67_68>;
template <typename CmdT>
Episode get_episode(const CmdT&) {
return Episode::EP1;
}
template <>
Episode get_episode<S_JoinGame_GC_64>(const S_JoinGame_GC_64& cmd) {
switch (cmd.episode) {
case 1:
return Episode::EP1;
case 2:
return Episode::EP2;
default:
return Episode::NONE;
}
}
template <>
Episode get_episode<S_JoinGame_XB_64>(const S_JoinGame_XB_64& cmd) {
switch (cmd.episode) {
case 1:
return Episode::EP1;
case 2:
return Episode::EP2;
default:
return Episode::NONE;
}
}
template <>
Episode get_episode<S_JoinGame_BB_64>(const S_JoinGame_BB_64& cmd) {
switch (cmd.episode) {
case 1:
return Episode::EP1;
case 2:
return Episode::EP2;
case 3:
return Episode::EP4;
default:
return Episode::NONE;
}
}
template <>
Episode get_episode<S_JoinGame_Ep3_64>(const S_JoinGame_Ep3_64&) {
return Episode::EP3;
}
template <typename CmdT>
static HandlerResult S_64(shared_ptr<ProxyServer::LinkedSession> ses, uint16_t, uint32_t flag, string& data) {
CmdT* cmd;
@@ -1515,7 +1624,50 @@ static HandlerResult S_64(shared_ptr<ProxyServer::LinkedSession> ses, uint16_t,
ses->floor = 0;
ses->is_in_game = true;
ses->is_in_quest = false;
ses->difficulty = cmd->difficulty;
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;
} else {
ses->lobby_mode = GameMode::NORMAL;
}
ses->lobby_random_seed = cmd->rare_seed;
if (cmd_ep3) {
ses->lobby_episode = Episode::EP3;
} else {
ses->lobby_episode = get_episode(*cmd);
}
if (ses->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.clear(0);
}
// 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->map = Lobby::load_maps(
ses->version(),
ses->lobby_episode,
ses->lobby_mode,
ses->lobby_difficulty,
ses->lobby_event,
ses->id,
s->set_data_table(ses->version(), ses->lobby_episode, ses->lobby_mode, ses->lobby_difficulty),
bind(&ServerState::load_map_file, s.get(), placeholders::_1, placeholders::_2),
Map::DEFAULT_RARE_ENEMIES,
ses->lobby_random_seed,
make_shared<PSOV2Encryption>(ses->lobby_random_seed),
cmd->variations,
&ses->log);
}
bool modified = false;
@@ -1569,7 +1721,14 @@ static HandlerResult S_E8(shared_ptr<ProxyServer::LinkedSession> ses, uint16_t,
ses->floor = 0;
ses->is_in_game = true;
ses->is_in_quest = false;
ses->difficulty = 0;
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.reset();
bool modified = false;
@@ -1649,7 +1808,15 @@ static HandlerResult C_98(shared_ptr<ProxyServer::LinkedSession> ses, uint16_t c
ses->floor = 0x0F;
ses->is_in_game = false;
ses->is_in_quest = false;
ses->difficulty = 0;
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.reset();
if (is_v3(ses->version()) || is_v4(ses->version())) {
return C_GXB_61(ses, command, flag, data);
} else {
@@ -1765,44 +1932,6 @@ static HandlerResult C_6x(shared_ptr<ProxyServer::LinkedSession> ses, uint16_t c
}
}
}
if (!data.empty()) {
if (data[0] == 0x21) {
const auto& cmd = check_size_t<G_InterLevelWarp_6x21>(data);
ses->floor = cmd.floor;
} else if (data[0] == 0x0C) {
if (is_v1_or_v2(ses->version()) && ses->config.check_flag(Client::Flag::INFINITE_HP_ENABLED)) {
send_remove_conditions(ses->client_channel, ses->lobby_client_id);
send_remove_conditions(ses->server_channel, ses->lobby_client_id);
}
} 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);
}
} else if (data[0] == 0x3E) {
C_6x_movement<G_StopAtPosition_6x3E>(ses, data);
} else if (data[0] == 0x3F) {
C_6x_movement<G_SetPosition_6x3F>(ses, data);
} else if (data[0] == 0x40) {
C_6x_movement<G_WalkToPosition_6x40>(ses, data);
} else if (data[0] == 0x42) {
C_6x_movement<G_RunToPosition_6x42>(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);
}
} else if (data[0] == 0x5F) {
const auto& cmd = check_size_t<G_DropItem_DC_6x5F>(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);
}
}
return C_6x<void>(ses, command, flag, data);
}
@@ -1814,19 +1943,62 @@ constexpr on_command_t C_B_6x = &C_6x<G_SendGuildCard_BB_6x06>;
template <>
HandlerResult C_6x<void>(shared_ptr<ProxyServer::LinkedSession> ses, uint16_t, uint32_t, string& data) {
check_implemented_subcommand(ses, data);
if (!data.empty() && (data[0] == 0x05) && ses->config.check_flag(Client::Flag::SWITCH_ASSIST_ENABLED)) {
auto& cmd = check_size_t<G_SwitchStateChanged_6x05>(data);
if (cmd.flags && cmd.header.object_id != 0xFFFF) {
if (ses->last_switch_enabled_command.header.subcommand == 0x05) {
ses->log.info("Switch assist: replaying previous enable command");
ses->server_channel.send(0x60, 0x00, &ses->last_switch_enabled_command,
sizeof(ses->last_switch_enabled_command));
ses->client_channel.send(0x60, 0x00, &ses->last_switch_enabled_command,
sizeof(ses->last_switch_enabled_command));
if (!data.empty()) {
if ((data[0] == 0x05) && ses->config.check_flag(Client::Flag::SWITCH_ASSIST_ENABLED)) {
auto& cmd = check_size_t<G_SwitchStateChanged_6x05>(data);
if ((cmd.flags & 1) && (cmd.header.object_id != 0xFFFF)) {
ses->recent_switch_flags.add(cmd.switch_flag_num);
string commands = ses->recent_switch_flags.enable_commands(ses->floor);
if (!commands.empty()) {
ses->server_channel.send(0x60, 0x00, commands);
ses->client_channel.send(0x60, 0x00, commands);
}
}
ses->last_switch_enabled_command = cmd;
} else if (data[0] == 0x21) {
const auto& cmd = check_size_t<G_InterLevelWarp_6x21>(data);
ses->floor = cmd.floor;
} else if (data[0] == 0x0C) {
if (ses->config.check_flag(Client::Flag::INFINITE_HP_ENABLED)) {
send_remove_conditions(ses->client_channel, ses->lobby_client_id);
send_remove_conditions(ses->server_channel, ses->lobby_client_id);
}
} 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);
}
} else if (data[0] == 0x3E) {
C_6x_movement<G_StopAtPosition_6x3E>(ses, data);
} else if (data[0] == 0x3F) {
C_6x_movement<G_SetPosition_6x3F>(ses, data);
} else if (data[0] == 0x40) {
C_6x_movement<G_WalkToPosition_6x40>(ses, data);
} else if (data[0] == 0x42) {
C_6x_movement<G_RunToPosition_6x42>(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);
}
} else if (data[0] == 0x5F) {
const auto& cmd = check_size_t<G_DropItem_DC_6x5F>(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);
} else if (data[0] == 0x60 || static_cast<uint8_t>(data[0]) == 0xA2) {
return SC_6x60_6xA2(ses, data);
}
}
@@ -1956,7 +2128,7 @@ static on_command_t handlers[0x100][NUM_VERSIONS][2] = {
/* 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_D_64, nullptr}, {S_G_64, nullptr}, {S_G_64, nullptr}, {S_G_64, nullptr}, {S_X_64, nullptr}, {S_B_64, 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}},
+80 -8
View File
@@ -522,6 +522,7 @@ ProxyServer::LinkedSession::LinkedSession(
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),
@@ -529,8 +530,13 @@ ProxyServer::LinkedSession::LinkedSession(
x(0.0),
z(0.0),
is_in_game(false),
is_in_quest(false) {
this->last_switch_enabled_command.header.subcommand = 0;
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));
}
@@ -681,8 +687,7 @@ ProxyServer::LinkedSession::SavingFile::SavingFile(
is_download(is_download),
remaining_bytes(remaining_bytes) {}
void ProxyServer::LinkedSession::dispatch_on_timeout(
evutil_socket_t, short, void* ctx) {
void ProxyServer::LinkedSession::dispatch_on_timeout(evutil_socket_t, short, void* ctx) {
reinterpret_cast<LinkedSession*>(ctx)->on_timeout();
}
@@ -725,6 +730,70 @@ void ProxyServer::LinkedSession::clear_lobby_players(size_t 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<const RareItemSet> rare_item_set;
shared_ptr<const CommonItemSet> 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_V1_11_2000_PROTOTYPE:
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<PSOV2Encryption>(this->config.override_random_seed)
: nullptr;
this->item_creator = make_shared<ItemCreator>(
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 license, do nothing - we can't return to the game server
// from unlicensed sessions
@@ -756,7 +825,7 @@ void ProxyServer::LinkedSession::send_to_game_server(const char* error_message)
this->disconnect();
} else {
send_ship_info(this->client_channel, string_printf("You\'ve returned to\n\tC6%s$C7\n\n%s", s->name.c_str(), error_message ? error_message : ""));
send_ship_info(this->client_channel, 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())) {
@@ -844,7 +913,7 @@ void ProxyServer::LinkedSession::on_input(Channel& ch, uint16_t command, uint32_
}
}
shared_ptr<ProxyServer::LinkedSession> ProxyServer::get_session() {
shared_ptr<ProxyServer::LinkedSession> ProxyServer::get_session() const {
if (this->id_to_session.empty()) {
throw runtime_error("no sessions exist");
}
@@ -854,8 +923,7 @@ shared_ptr<ProxyServer::LinkedSession> ProxyServer::get_session() {
return this->id_to_session.begin()->second;
}
shared_ptr<ProxyServer::LinkedSession> ProxyServer::get_session_by_name(
const std::string& name) {
shared_ptr<ProxyServer::LinkedSession> ProxyServer::get_session_by_name(const std::string& name) const {
try {
uint64_t session_id = stoull(name, nullptr, 16);
return this->id_to_session.at(session_id);
@@ -866,6 +934,10 @@ shared_ptr<ProxyServer::LinkedSession> ProxyServer::get_session_by_name(
}
}
const unordered_map<uint64_t, shared_ptr<ProxyServer::LinkedSession>>& ProxyServer::all_sessions() const {
return this->id_to_session;
}
shared_ptr<ProxyServer::LinkedSession> ProxyServer::create_licensed_session(
shared_ptr<License> l,
uint16_t local_port,
+23 -4
View File
@@ -70,10 +70,20 @@ public:
Client::Config config;
// A null handler in here means to forward the response to the remote server
std::deque<std::function<void(uint32_t return_value, uint32_t checksum)>> function_call_return_handler_queue;
G_SwitchStateChanged_6x05 last_switch_enabled_command;
RecentSwitchFlags recent_switch_flags; // used for switch assist
ItemData next_drop_item;
uint32_t next_item_id;
enum class DropMode {
DISABLED = 0,
PASSTHROUGH,
INTERCEPT,
};
DropMode drop_mode;
std::shared_ptr<std::string> quest_dat_data;
std::shared_ptr<ItemCreator> item_creator;
std::shared_ptr<Map> map;
struct LobbyPlayer {
uint32_t guild_card_number = 0;
uint64_t xb_user_id = 0;
@@ -90,7 +100,12 @@ public:
float z;
bool is_in_game;
bool is_in_quest;
uint8_t difficulty;
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;
@@ -173,13 +188,17 @@ public:
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<LinkedSession> get_session();
std::shared_ptr<LinkedSession> get_session_by_name(const std::string& name);
std::shared_ptr<LinkedSession> get_session() const;
std::shared_ptr<LinkedSession> get_session_by_name(const std::string& name) const;
const std::unordered_map<uint64_t, std::shared_ptr<LinkedSession>>& all_sessions() const;
std::shared_ptr<LinkedSession> create_licensed_session(
std::shared_ptr<License> l,
uint16_t local_port,
+4 -11
View File
@@ -777,14 +777,9 @@ vector<shared_ptr<const QuestCategoryIndex::Category>> QuestIndex::categories(
Episode episode,
Version version,
IncludeCondition include_condition) const {
// The episode filter should apply in normal or solo mode
if ((menu_type != QuestMenuType::NORMAL) && (menu_type != QuestMenuType::SOLO)) {
episode = Episode::NONE;
}
vector<shared_ptr<const QuestCategoryIndex::Category>> ret;
for (const auto& cat : this->category_index->categories) {
if (cat->check_flag(menu_type) && !this->filter(menu_type, episode, version, cat->category_id, include_condition, 1).empty()) {
if (cat->check_flag(menu_type) && !this->filter(episode, version, cat->category_id, include_condition, 1).empty()) {
ret.emplace_back(cat);
}
}
@@ -792,15 +787,13 @@ vector<shared_ptr<const QuestCategoryIndex::Category>> QuestIndex::categories(
}
vector<pair<QuestIndex::IncludeState, shared_ptr<const Quest>>> QuestIndex::filter(
QuestMenuType menu_type,
Episode episode,
Version version,
uint32_t category_id,
IncludeCondition include_condition,
size_t limit) const {
if ((menu_type != QuestMenuType::NORMAL) && (menu_type != QuestMenuType::SOLO)) {
episode = Episode::NONE;
}
auto cat = this->category_index->at(category_id);
Episode effective_episode = cat->enable_episode_filter() ? episode : Episode::NONE;
vector<pair<IncludeState, shared_ptr<const Quest>>> ret;
auto category_it = this->quests_by_category_id_and_number.find(category_id);
@@ -808,7 +801,7 @@ vector<pair<QuestIndex::IncludeState, shared_ptr<const Quest>>> QuestIndex::filt
return ret;
}
for (auto it : category_it->second) {
if (((episode == Episode::NONE) || (it.second->episode == episode)) &&
if (((effective_episode == Episode::NONE) || (it.second->episode == effective_episode)) &&
it.second->has_version_any_language(version)) {
IncludeState state = include_condition ? include_condition(it.second) : IncludeState::AVAILABLE;
if (state == IncludeState::HIDDEN) {
+4 -1
View File
@@ -31,6 +31,7 @@ enum class QuestMenuType {
GOVERNMENT = 4,
DOWNLOAD = 5,
EP3_DOWNLOAD = 6,
// 7 can't be used as a menu type (it enables the per-episode filter)
};
struct QuestCategoryIndex {
@@ -46,6 +47,9 @@ struct QuestCategoryIndex {
[[nodiscard]] inline bool check_flag(QuestMenuType menu_type) const {
return this->enabled_flags & (1 << static_cast<uint8_t>(menu_type));
}
[[nodiscard]] inline bool enable_episode_filter() const {
return this->enabled_flags & 0x80;
}
};
std::vector<std::shared_ptr<Category>> categories;
@@ -151,7 +155,6 @@ struct QuestIndex {
Version version,
IncludeCondition include_condition = nullptr) const;
std::vector<std::pair<QuestIndex::IncludeState, std::shared_ptr<const Quest>>> filter(
QuestMenuType menu_type,
Episode episode,
Version version,
uint32_t category_id,
+19 -2
View File
@@ -252,14 +252,28 @@ bool QuestAvailabilityExpression::EventLookupNode::operator==(const Node& other)
}
int64_t QuestAvailabilityExpression::EventLookupNode::evaluate(const Env& env) const {
return env.num_players;
return env.event;
}
string QuestAvailabilityExpression::EventLookupNode::str() const {
return "V_Event";
}
QuestAvailabilityExpression::ConstantNode::ConstantNode(bool value)
QuestAvailabilityExpression::V1PresenceLookupNode::V1PresenceLookupNode() {}
bool QuestAvailabilityExpression::V1PresenceLookupNode::operator==(const Node& other) const {
return dynamic_cast<const V1PresenceLookupNode*>(&other) != nullptr;
}
int64_t QuestAvailabilityExpression::V1PresenceLookupNode::evaluate(const Env& env) const {
return env.v1_present ? 1 : 0;
}
string QuestAvailabilityExpression::V1PresenceLookupNode::str() const {
return "V_V1Present";
}
QuestAvailabilityExpression::ConstantNode::ConstantNode(int64_t value)
: value(value) {}
bool QuestAvailabilityExpression::ConstantNode::operator==(const Node& other) const {
@@ -412,6 +426,9 @@ unique_ptr<const QuestAvailabilityExpression::Node> QuestAvailabilityExpression:
if (text == "V_Event") {
return make_unique<EventLookupNode>();
}
if (text == "V_V1Present") {
return make_unique<V1PresenceLookupNode>();
}
// Check for constants
if (text == "true") {
+11 -1
View File
@@ -21,6 +21,7 @@ public:
std::shared_ptr<const TeamIndex::Team> team;
size_t num_players;
uint8_t event;
bool v1_present;
};
QuestAvailabilityExpression(const std::string& text);
@@ -160,9 +161,18 @@ protected:
virtual std::string str() const;
};
class V1PresenceLookupNode : public Node {
public:
V1PresenceLookupNode();
virtual ~V1PresenceLookupNode() = default;
virtual bool operator==(const Node& other) const;
virtual int64_t evaluate(const Env& env) const;
virtual std::string str() const;
};
class ConstantNode : public Node {
public:
ConstantNode(bool value);
ConstantNode(int64_t value);
virtual ~ConstantNode() = default;
virtual bool operator==(const Node& other) const;
virtual int64_t evaluate(const Env& env) const;
+19 -12
View File
@@ -51,10 +51,11 @@ static string escape_string(const string& data, TextEncoding encoding = TextEnco
decoded = data;
break;
case TextEncoding::UTF16:
case TextEncoding::UTF16_ALWAYS_MARKED:
decoded = tt_utf16_to_utf8(data);
break;
case TextEncoding::SJIS:
decoded = tt_sjis_to_utf8(data);
decoded = tt_sega_sjis_to_utf8(data);
break;
case TextEncoding::ISO8859:
decoded = tt_8859_to_utf8(data);
@@ -1233,7 +1234,7 @@ std::string disassemble_quest_script(const void* data, size_t size, Version vers
} else {
string s = cmd_r.get_cstr();
if (def->flags & F_PASS) {
arg_stack_values.emplace_back(language ? tt_8859_to_utf8(s) : tt_sjis_to_utf8(s));
arg_stack_values.emplace_back(language ? tt_8859_to_utf8(s) : tt_sega_sjis_to_utf8(s));
}
dasm_arg = escape_string(s, encoding_for_language(language));
}
@@ -1507,7 +1508,7 @@ std::string disassemble_quest_script(const void* data, size_t size, Version vers
lines.emplace_back(string_printf(" %04zX dfp %04hX /* %hu */", l->offset + offsetof(PlayerStats, char_stats.dfp), stats.char_stats.dfp.load(), stats.char_stats.dfp.load()));
lines.emplace_back(string_printf(" %04zX ata %04hX /* %hu */", l->offset + offsetof(PlayerStats, char_stats.ata), stats.char_stats.ata.load(), stats.char_stats.ata.load()));
lines.emplace_back(string_printf(" %04zX lck %04hX /* %hu */", l->offset + offsetof(PlayerStats, char_stats.lck), stats.char_stats.lck.load(), stats.char_stats.lck.load()));
lines.emplace_back(string_printf(" %04zX a1 %04hX /* %hu */", l->offset + offsetof(PlayerStats, unknown_a1), stats.unknown_a1.load(), stats.unknown_a1.load()));
lines.emplace_back(string_printf(" %04zX esp %04hX /* %hu */", l->offset + offsetof(PlayerStats, esp), stats.esp.load(), stats.esp.load()));
lines.emplace_back(string_printf(" %04zX height %08" PRIX32 " /* %g */", l->offset + offsetof(PlayerStats, height), stats.height.load_raw(), stats.height.load()));
lines.emplace_back(string_printf(" %04zX a3 %08" PRIX32 " /* %g */", l->offset + offsetof(PlayerStats, unknown_a3), stats.unknown_a3.load_raw(), stats.unknown_a3.load()));
lines.emplace_back(string_printf(" %04zX level %08" PRIX32 " /* level %" PRIu32 " */", l->offset + offsetof(PlayerStats, level), stats.level.load(), stats.level.load() + 1));
@@ -1535,7 +1536,7 @@ std::string disassemble_quest_script(const void* data, size_t size, Version vers
lines.emplace_back(string_printf(" %04zX ata_bonus %04hX /* %hd */", l->offset + offsetof(AttackData, ata_bonus), attack.ata_bonus.load(), attack.ata_bonus.load()));
lines.emplace_back(string_printf(" %04zX a4 %04hX /* %hu */", l->offset + offsetof(AttackData, unknown_a4), attack.unknown_a4.load(), attack.unknown_a4.load()));
lines.emplace_back(string_printf(" %04zX distance_x %08" PRIX32 " /* %g */", l->offset + offsetof(AttackData, distance_x), attack.distance_x.load_raw(), attack.distance_x.load()));
lines.emplace_back(string_printf(" %04zX angle_x %08" PRIX32 " /* %g */", l->offset + offsetof(AttackData, angle_x), attack.angle_x.load_raw(), attack.angle_x.load()));
lines.emplace_back(string_printf(" %04zX angle_x %08" PRIX32 " /* %" PRIu32 "/65536 */", l->offset + offsetof(AttackData, angle_x), attack.angle_x.load_raw(), attack.angle_x.load()));
lines.emplace_back(string_printf(" %04zX distance_y %08" PRIX32 " /* %g */", l->offset + offsetof(AttackData, distance_y), attack.distance_y.load_raw(), attack.distance_y.load()));
lines.emplace_back(string_printf(" %04zX a8 %04hX /* %hu */", l->offset + offsetof(AttackData, unknown_a8), attack.unknown_a8.load(), attack.unknown_a8.load()));
lines.emplace_back(string_printf(" %04zX a9 %04hX /* %hu */", l->offset + offsetof(AttackData, unknown_a9), attack.unknown_a9.load(), attack.unknown_a9.load()));
@@ -1840,7 +1841,11 @@ std::string assemble_quest_script(const std::string& text) {
label->name = line.substr(0, line.size() - 1);
size_t at_offset = label->name.find('@');
if (at_offset != string::npos) {
label->index = stoul(label->name.substr(at_offset + 1), nullptr, 0);
try {
label->index = stoul(label->name.substr(at_offset + 1), nullptr, 0);
} catch (const exception& e) {
throw runtime_error(string_printf("(line %zu) invalid index in label (%s)", line_num, e.what()));
}
label->name.resize(at_offset);
if (label->name == "start" && label->index != 0) {
throw runtime_error("start label cannot have a nonzero label ID");
@@ -1879,6 +1884,7 @@ std::string assemble_quest_script(const std::string& text) {
}
// Assemble code segment
bool version_has_args = F_HAS_ARGS & v_flag(quest_version);
const auto& opcodes = opcodes_by_name_for_version(quest_version);
StringWriter code_w;
for (size_t line_num = 1; line_num <= lines.size(); line_num++) {
@@ -1908,7 +1914,8 @@ std::string assemble_quest_script(const std::string& text) {
auto line_tokens = split(line, ' ', 1);
const auto& opcode_def = opcodes.at(line_tokens.at(0));
if (!(opcode_def->flags & F_ARGS)) {
bool use_args = version_has_args && (opcode_def->flags & F_ARGS);
if (!use_args) {
if ((opcode_def->opcode & 0xFF00) == 0x0000) {
code_w.put_u8(opcode_def->opcode);
} else {
@@ -1930,7 +1937,7 @@ std::string assemble_quest_script(const std::string& text) {
strip_leading_whitespace(line_tokens[1]);
if (starts_with(line_tokens[1], "...")) {
if (!(opcode_def->flags & F_ARGS)) {
if (!use_args) {
throw runtime_error(string_printf("(line %zu) \'...\' can only be used with F_ARGS opcodes", line_num));
}
@@ -1962,7 +1969,7 @@ std::string assemble_quest_script(const std::string& text) {
auto add_cstr = [&](const string& text) -> void {
switch (quest_version) {
case Version::DC_NTE:
code_w.write(tt_utf8_to_sjis(text));
code_w.write(tt_utf8_to_sega_sjis(text));
code_w.put_u8(0);
break;
case Version::DC_V1_11_2000_PROTOTYPE:
@@ -1973,7 +1980,7 @@ std::string assemble_quest_script(const std::string& text) {
case Version::GC_EP3_NTE:
case Version::GC_EP3:
case Version::XB_V3:
code_w.write(quest_language ? tt_utf8_to_8859(text) : tt_utf8_to_sjis(text));
code_w.write(quest_language ? tt_utf8_to_8859(text) : tt_utf8_to_sega_sjis(text));
code_w.put_u8(0);
break;
case Version::PC_NTE:
@@ -1987,7 +1994,7 @@ std::string assemble_quest_script(const std::string& text) {
}
};
if (opcode_def->flags & F_ARGS) {
if (use_args) {
auto label_it = labels_by_name.find(arg);
if (arg.empty()) {
throw runtime_error("argument is empty");
@@ -2058,7 +2065,7 @@ std::string assemble_quest_script(const std::string& text) {
}
}
} else { // Not F_ARGS
} else { // Not use_args
auto add_label = [&](const string& name, bool is32) -> void {
if (!labels_by_name.count(name)) {
throw runtime_error("label not defined: " + name);
@@ -2157,7 +2164,7 @@ std::string assemble_quest_script(const std::string& text) {
}
}
if (opcode_def->flags & F_ARGS) {
if (use_args) {
if ((opcode_def->opcode & 0xFF00) == 0x0000) {
code_w.put_u8(opcode_def->opcode);
} else {
+61 -47
View File
@@ -12,20 +12,16 @@ using namespace std;
string RareItemSet::ExpandedDrop::str() const {
auto frac = reduce_fraction<uint64_t>(this->probability, 0x100000000);
auto hex = this->data.hex();
return string_printf(
"(%08" PRIX32 " => %" PRIu64 "/%" PRIu64 ") %02hhX%02hhX%02hhX",
this->probability, frac.first, frac.second, this->item_code[0], this->item_code[1], this->item_code[2]);
"(%08" PRIX32 " => %" PRIu64 "/%" PRIu64 ") %s",
this->probability, frac.first, frac.second, hex.c_str());
}
string RareItemSet::ExpandedDrop::str(shared_ptr<const ItemNameIndex> name_index) const {
ItemData item;
item.data1[0] = this->item_code[0];
item.data1[1] = this->item_code[1];
item.data1[2] = this->item_code[2];
string ret = this->str();
ret += " (";
ret += name_index->describe_item(item);
ret += name_index->describe_item(this->data);
ret += ")";
return ret;
}
@@ -78,14 +74,22 @@ uint8_t RareItemSet::compress_rate(uint32_t probability) {
}
RareItemSet::ParsedRELData::PackedDrop::PackedDrop(const ExpandedDrop& exp)
: probability(RareItemSet::compress_rate(exp.probability)),
item_code(exp.item_code) {}
: probability(RareItemSet::compress_rate(exp.probability)) {
if (!exp.data.can_be_encoded_in_rel_rare_table()) {
throw runtime_error("item " + exp.data.short_hex() + " has extended attributes and cannot be encoded in a REL file");
}
this->item_code[0] = exp.data.data1[0];
this->item_code[1] = exp.data.data1[1];
this->item_code[2] = exp.data.data1[2];
}
RareItemSet::ExpandedDrop RareItemSet::ParsedRELData::PackedDrop::expand() const {
return ExpandedDrop{
.probability = RareItemSet::expand_rate(this->probability),
.item_code = this->item_code,
};
ExpandedDrop ret;
ret.probability = RareItemSet::expand_rate(this->probability);
ret.data.data1[0] = this->item_code[0];
ret.data.data1[1] = this->item_code[1];
ret.data.data1[2] = this->item_code[2];
return ret;
}
template <bool IsBigEndian>
@@ -184,10 +188,9 @@ RareItemSet::ParsedRELData::ParsedRELData(const SpecCollection& collection) {
for (const auto& specs : collection.rt_index_to_specs) {
ExpandedDrop effective_spec;
for (const auto& spec : specs) {
if (effective_spec.item_code.is_filled_with(0)) {
if (effective_spec.data.empty()) {
effective_spec = spec;
} else if ((effective_spec.probability != spec.probability) ||
(effective_spec.item_code != spec.item_code)) {
} else if ((effective_spec.probability != spec.probability) || (effective_spec.data != spec.data)) {
throw runtime_error("monster spec cannot be converted to ItemRT format");
}
}
@@ -216,7 +219,7 @@ RareItemSet::SpecCollection RareItemSet::ParsedRELData::as_collection() const {
SpecCollection ret;
for (size_t z = 0; z < this->monster_rares.size(); z++) {
const auto& drop = this->monster_rares[z];
if (drop.item_code.is_filled_with(0)) {
if (drop.data.empty()) {
continue;
}
if (z >= ret.rt_index_to_specs.size()) {
@@ -225,7 +228,7 @@ RareItemSet::SpecCollection RareItemSet::ParsedRELData::as_collection() const {
ret.rt_index_to_specs[z].emplace_back(drop);
}
for (const auto& drop : this->box_rares) {
if (drop.drop.item_code.is_filled_with(0)) {
if (drop.drop.data.empty()) {
continue;
}
if (drop.area >= ret.box_area_to_specs.size()) {
@@ -362,17 +365,14 @@ RareItemSet::RareItemSet(const JSON& json, shared_ptr<const ItemNameIndex> name_
auto item_desc = spec_json->at(1);
if (item_desc.is_int()) {
uint32_t item_code = item_desc.as_int();
d.item_code[0] = (item_code >> 16) & 0xFF;
d.item_code[1] = (item_code >> 8) & 0xFF;
d.item_code[2] = item_code & 0xFF;
d.data.data1[0] = (item_code >> 16) & 0xFF;
d.data.data1[1] = (item_code >> 8) & 0xFF;
d.data.data1[2] = item_code & 0xFF;
} else if (item_desc.is_string()) {
if (!name_index) {
throw runtime_error("item name index is not available");
}
ItemData data = name_index->parse_item_description(item_desc.as_string());
d.item_code[0] = data.data1[0];
d.item_code[1] = data.data1[1];
d.item_code[2] = data.data1[2];
d.data = name_index->parse_item_description(item_desc.as_string());
} else {
throw runtime_error("invalid item description type");
}
@@ -427,7 +427,7 @@ std::string RareItemSet::serialize_gsl(bool big_endian) const {
return GSLArchive::generate(files, big_endian);
}
std::string RareItemSet::serialize_json(shared_ptr<const ItemNameIndex> name_index) const {
JSON RareItemSet::json(shared_ptr<const ItemNameIndex> name_index) const {
auto modes_dict = JSON::dict();
static const array<GameMode, 4> modes = {GameMode::NORMAL, GameMode::BATTLE, GameMode::CHALLENGE, GameMode::SOLO};
for (const auto& mode : modes) {
@@ -446,19 +446,18 @@ std::string RareItemSet::serialize_json(shared_ptr<const ItemNameIndex> name_ind
}
for (const auto& spec : this->get_enemy_specs(GameMode::NORMAL, episode, difficulty, section_id, rt_index)) {
uint32_t data1_0_1_2 = (spec.item_code[0] << 16) | (spec.item_code[1] << 8) | spec.item_code[2];
if (data1_0_1_2 == 0) {
if (spec.data.empty()) {
continue;
}
auto frac = reduce_fraction<uint64_t>(spec.probability, 0x100000000);
auto spec_json = JSON::list({string_printf("%" PRIu64 "/%" PRIu64, frac.first, frac.second), data1_0_1_2});
auto spec_json = JSON::list({string_printf("%" PRIu64 "/%" PRIu64, frac.first, frac.second)});
if (spec.data.can_be_encoded_in_rel_rare_table()) {
spec_json.emplace_back((spec.data.data1[0] << 16) | (spec.data.data1[1] << 8) | spec.data.data1[2]);
} else {
spec_json.emplace_back(spec.data.short_hex());
}
if (name_index) {
ItemData data;
data.data1[0] = spec.item_code[0];
data.data1[1] = spec.item_code[1];
data.data1[2] = spec.item_code[2];
spec_json.emplace_back(name_index->describe_item(data));
spec_json.emplace_back(name_index->describe_item(spec.data));
}
for (const auto& enemy_type : enemy_types) {
if (enemy_type_valid_for_episode(episode, enemy_type)) {
@@ -473,20 +472,20 @@ std::string RareItemSet::serialize_json(shared_ptr<const ItemNameIndex> name_ind
auto area_list = JSON::list();
for (const auto& spec : this->get_box_specs(GameMode::NORMAL, episode, difficulty, section_id, area)) {
uint32_t data1_0_1_2 = (spec.item_code[0] << 16) | (spec.item_code[1] << 8) | spec.item_code[2];
if (data1_0_1_2 == 0) {
if (spec.data.empty()) {
continue;
}
auto frac = reduce_fraction<uint64_t>(spec.probability, 0x100000000);
area_list.emplace_back(JSON::list({string_printf("%" PRIu64 "/%" PRIu64, frac.first, frac.second), data1_0_1_2}));
if (name_index) {
ItemData data;
data.data1[0] = spec.item_code[0];
data.data1[1] = spec.item_code[1];
data.data1[2] = spec.item_code[2];
area_list.back().emplace_back(name_index->describe_item(data));
auto spec_json = JSON::list({string_printf("%" PRIu64 "/%" PRIu64, frac.first, frac.second)});
if (spec.data.can_be_encoded_in_rel_rare_table()) {
spec_json.emplace_back((spec.data.data1[0] << 16) | (spec.data.data1[1] << 8) | spec.data.data1[2]);
} else {
spec_json.emplace_back(spec.data.short_hex());
}
if (name_index) {
spec_json.emplace_back(name_index->describe_item(spec.data));
}
area_list.emplace_back(std::move(spec_json));
}
if (!area_list.empty()) {
@@ -507,7 +506,22 @@ std::string RareItemSet::serialize_json(shared_ptr<const ItemNameIndex> name_ind
modes_dict.emplace(name_for_mode(mode), std::move(episodes_dict));
}
return modes_dict.serialize(JSON::SerializeOption::FORMAT | JSON::SerializeOption::HEX_INTEGERS | JSON::SerializeOption::SORT_DICT_KEYS);
return modes_dict;
}
void RareItemSet::multiply_all_rates(double factor) {
auto multiply_rates_vec = +[](vector<vector<ExpandedDrop>>& vec, double factor) -> void {
for (auto& vec_it : vec) {
for (auto& z_it : vec_it) {
uint64_t new_probability = z_it.probability * factor;
z_it.probability = min<uint64_t>(new_probability, 0xFFFFFFFF);
}
}
};
for (auto& coll_it : this->collections) {
multiply_rates_vec(coll_it.second.rt_index_to_specs, factor);
multiply_rates_vec(coll_it.second.box_area_to_specs, factor);
}
}
void RareItemSet::print_collection(
+4 -2
View File
@@ -19,7 +19,7 @@ class RareItemSet {
public:
struct ExpandedDrop {
uint32_t probability = 0;
parray<uint8_t, 3> item_code;
ItemData data;
std::string str() const;
std::string str(std::shared_ptr<const ItemNameIndex> name_index) const;
@@ -37,7 +37,9 @@ public:
std::string serialize_afs(bool is_v1) const;
std::string serialize_gsl(bool big_endian) const;
std::string serialize_json(std::shared_ptr<const ItemNameIndex> name_index = nullptr) const;
JSON json(std::shared_ptr<const ItemNameIndex> name_index = nullptr) const;
void multiply_all_rates(double factor);
void print_collection(
FILE* stream,
+462 -565
View File
File diff suppressed because it is too large Load Diff
+982 -315
View File
File diff suppressed because it is too large Load Diff
+20
View File
@@ -3,6 +3,7 @@
#include <stdint.h>
#include "Client.hh"
#include "CommandFormats.hh"
#include "Lobby.hh"
#include "PSOProtocol.hh"
#include "ServerState.hh"
@@ -16,3 +17,22 @@ void send_item_notification_if_needed(
const Client::Config& config,
const ItemData& item,
bool is_from_rare_table);
G_SpecializableItemDropRequest_6xA2 normalize_drop_request(const void* data, size_t size);
struct DropReconcileResult {
uint8_t effective_rt_index;
bool is_box;
bool should_drop;
bool ignore_def;
};
DropReconcileResult reconcile_drop_request_with_map(
PrefixedLogger& log,
Channel& client_channel,
G_SpecializableItemDropRequest_6xA2& cmd,
Version version,
Episode episode,
const Client::Config& config,
std::shared_ptr<Map> map,
bool mark_drop);
-4
View File
@@ -6,7 +6,6 @@
#include "Loggers.hh"
#include "Server.hh"
#include "Shell.hh"
using namespace std;
@@ -474,9 +473,6 @@ ReplaySession::ReplaySession(
while (!feof(input_log)) {
line_num++;
string line = fgets(input_log);
if (starts_with(line, Shell::PROMPT)) {
line = line.substr(Shell::PROMPT.size());
}
if (ends_with(line, "\n")) {
line.resize(line.size() - 1);
}
+24 -14
View File
@@ -210,6 +210,16 @@ PSOBBBaseSystemFile::PSOBBBaseSystemFile() {
}
}
PlayerDispDataBBPreview PSOBBCharacterFile::to_preview() const {
PlayerDispDataBBPreview pre;
pre.level = this->disp.stats.level;
pre.experience = this->disp.stats.experience;
pre.visual = this->disp.visual;
pre.name = this->disp.name;
pre.play_time_seconds = this->play_time_seconds;
return pre;
}
shared_ptr<PSOBBCharacterFile> PSOBBCharacterFile::create_from_config(
uint32_t guild_card_number,
uint8_t language,
@@ -365,7 +375,7 @@ shared_ptr<PSOBBCharacterFile> PSOBBCharacterFile::create_from_config(
ret->guild_card.section_id = ret->disp.visual.section_id;
ret->guild_card.char_class = ret->disp.visual.char_class;
for (size_t z = 0; z < PSOBBCharacterFile::DEFAULT_SYMBOL_CHATS.size(); z++) {
ret->symbol_chats[z] = PSOBBCharacterFile::DEFAULT_SYMBOL_CHATS[z].to_entry();
ret->symbol_chats[z] = PSOBBCharacterFile::DEFAULT_SYMBOL_CHATS[z].to_entry(language);
}
for (size_t z = 0; z < PSOBBCharacterFile::DEFAULT_TECH_MENU_CONFIG.size(); z++) {
ret->tech_menu_config[z] = PSOBBCharacterFile::DEFAULT_TECH_MENU_CONFIG[z];
@@ -382,10 +392,10 @@ shared_ptr<PSOBBCharacterFile> PSOBBCharacterFile::create_from_preview(
guild_card_number, language, preview.visual, preview.name.decode(language), level_table);
}
PSOBBCharacterFile::SymbolChatEntry PSOBBCharacterFile::DefaultSymbolChatEntry::to_entry() const {
PSOBBCharacterFile::SymbolChatEntry PSOBBCharacterFile::DefaultSymbolChatEntry::to_entry(uint8_t language) const {
SymbolChatEntry ret;
ret.present = 1;
ret.name.encode(this->name, 1);
ret.name.encode(this->language_to_name.at(language), language);
ret.data.spec = this->spec;
for (size_t z = 0; z < 4; z++) {
ret.data.corner_objects[z] = this->corner_objects[z];
@@ -398,7 +408,7 @@ PSOBBCharacterFile::SymbolChatEntry PSOBBCharacterFile::DefaultSymbolChatEntry::
// TODO: Eliminate duplication between this function and the parallel function
// in PlayerBank
void PSOBBCharacterFile::add_item(const ItemData& item, Version version) {
void PSOBBCharacterFile::add_item(const ItemData& item, const ItemData::StackLimits& limits) {
uint32_t primary_identifier = item.primary_identifier();
// Annoyingly, meseta is in the disp data, not in the inventory struct. If the
@@ -409,7 +419,7 @@ void PSOBBCharacterFile::add_item(const ItemData& item, Version version) {
}
// Handle combinable items
size_t combine_max = item.max_stack_size(version);
size_t combine_max = item.max_stack_size(limits);
if (combine_max > 1) {
// Get the item index if there's already a stack of the same item in the
// player's inventory
@@ -446,13 +456,13 @@ void PSOBBCharacterFile::add_item(const ItemData& item, Version version) {
// TODO: Eliminate code duplication between this function and the parallel
// function in PlayerBank
ItemData PSOBBCharacterFile::remove_item(uint32_t item_id, uint32_t amount, Version version) {
ItemData PSOBBCharacterFile::remove_item(uint32_t item_id, uint32_t amount, const ItemData::StackLimits& limits) {
ItemData ret;
// If we're removing meseta (signaled by an invalid item ID), then create a
// meseta item.
if (item_id == 0xFFFFFFFF) {
this->remove_meseta(amount, !is_v4(version));
this->remove_meseta(amount, !is_v4(limits.version));
ret.data1[0] = 0x04;
ret.data2d = amount;
return ret;
@@ -466,7 +476,7 @@ ItemData PSOBBCharacterFile::remove_item(uint32_t item_id, uint32_t amount, Vers
// then create a new item and reduce the amount of the existing stack. Note
// that passing amount == 0 means to remove the entire stack, so this only
// applies if amount is nonzero.
if (amount && (inventory_item.data.stack_size(version) > 1) &&
if (amount && (inventory_item.data.stack_size(limits) > 1) &&
(amount < inventory_item.data.data1[5])) {
if (is_equipped) {
throw runtime_error("character has a combine item equipped");
@@ -581,12 +591,12 @@ void PSOBBCharacterFile::clear_all_material_usage() {
}
const array<PSOBBCharacterFile::DefaultSymbolChatEntry, 6> PSOBBCharacterFile::DEFAULT_SYMBOL_CHATS = {
DefaultSymbolChatEntry{"\tEHello", 0x28, {0xFFFF, 0x000D, 0xFFFF, 0xFFFF}, {SymbolChat::FacePart{0x05, 0x18, 0x1D, 0x00}, {0x05, 0x28, 0x1D, 0x01}, {0x36, 0x20, 0x2A, 0x00}, {0x3C, 0x00, 0x32, 0x00}, {0xFF, 0x00, 0x00, 0x00}, {0xFF, 0x00, 0x00, 0x00}, {0xFF, 0x00, 0x00, 0x00}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}}},
DefaultSymbolChatEntry{"\tEGood-bye", 0x74, {0x0476, 0x000C, 0xFFFF, 0xFFFF}, {SymbolChat::FacePart{0x06, 0x15, 0x14, 0x00}, {0x06, 0x2B, 0x14, 0x01}, {0x05, 0x18, 0x1F, 0x00}, {0x05, 0x28, 0x1F, 0x01}, {0x36, 0x20, 0x2A, 0x00}, {0x3C, 0x00, 0x32, 0x00}, {0xFF, 0x00, 0x00, 0x00}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}}},
DefaultSymbolChatEntry{"\tEHurrah!", 0x28, {0x0362, 0x0362, 0xFFFF, 0xFFFF}, {SymbolChat::FacePart{0x09, 0x16, 0x1B, 0x00}, {0x09, 0x2B, 0x1B, 0x01}, {0x37, 0x20, 0x2C, 0x00}, {0xFF, 0x00, 0x00, 0x00}, {0xFF, 0x00, 0x00, 0x00}, {0xFF, 0x00, 0x00, 0x00}, {0xFF, 0x00, 0x00, 0x00}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}}},
DefaultSymbolChatEntry{"\tECrying", 0x74, {0x074F, 0xFFFF, 0xFFFF, 0xFFFF}, {SymbolChat::FacePart{0x06, 0x15, 0x14, 0x00}, {0x06, 0x2B, 0x14, 0x01}, {0x05, 0x18, 0x1F, 0x00}, {0x05, 0x28, 0x1F, 0x01}, {0x21, 0x20, 0x2E, 0x00}, {0xFF, 0x00, 0x00, 0x00}, {0xFF, 0x00, 0x00, 0x00}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}}},
DefaultSymbolChatEntry{"\tEI\'m angry!", 0x5C, {0x0116, 0x0001, 0xFFFF, 0xFFFF}, {SymbolChat::FacePart{0x0B, 0x18, 0x1B, 0x01}, {0x0B, 0x28, 0x1B, 0x00}, {0x33, 0x20, 0x2A, 0x00}, {0xFF, 0x00, 0x00, 0x00}, {0xFF, 0x00, 0x00, 0x00}, {0xFF, 0x00, 0x00, 0x00}, {0xFF, 0x00, 0x00, 0x00}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}}},
DefaultSymbolChatEntry{"\tEHelp me!", 0xEC, {0x065E, 0x0138, 0xFFFF, 0xFFFF}, {SymbolChat::FacePart{0x02, 0x17, 0x1B, 0x01}, {0x02, 0x2A, 0x1B, 0x00}, {0x31, 0x20, 0x2C, 0x00}, {0xFF, 0x00, 0x00, 0x00}, {0xFF, 0x00, 0x00, 0x00}, {0xFF, 0x00, 0x00, 0x00}, {0xFF, 0x00, 0x00, 0x00}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}}},
DefaultSymbolChatEntry{{"\tJ\xE3\x81\x93\xE3\x82\x93\xE3\x81\xAB\xE3\x81\xA1\xE3\x81\xAF", "\tEHello", "\tEHallo", "\tESalut", "\tEHola", "\tB\xE4\xBD\xA0\xE5\xA5\xBD", "\tT\xE4\xBD\xA0\xE5\xA5\xBD", "\tK\xEC\x95\x88\xEB\x85\x95"}, 0x28, {0xFFFF, 0x000D, 0xFFFF, 0xFFFF}, {SymbolChat::FacePart{0x05, 0x18, 0x1D, 0x00}, {0x05, 0x28, 0x1D, 0x01}, {0x36, 0x20, 0x2A, 0x00}, {0x3C, 0x00, 0x32, 0x00}, {0xFF, 0x00, 0x00, 0x00}, {0xFF, 0x00, 0x00, 0x00}, {0xFF, 0x00, 0x00, 0x00}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}}},
DefaultSymbolChatEntry{{"\tJ\xE3\x81\x95\xE3\x82\x88\xE3\x81\x86\xE3\x81\xAA\xE3\x82\x89", "\tEGood-bye", "\tETschus", "\tEAu revoir", "\tEAdios", "\tB\xE5\x86\x8D\xE8\xA7\x81", "\tT\xE5\x86\x8D\xE8\xA6\x8B", "\tK\xEC\x9E\x98\xEA\xB0\x80"}, 0x74, {0x0476, 0x000C, 0xFFFF, 0xFFFF}, {SymbolChat::FacePart{0x06, 0x15, 0x14, 0x00}, {0x06, 0x2B, 0x14, 0x01}, {0x05, 0x18, 0x1F, 0x00}, {0x05, 0x28, 0x1F, 0x01}, {0x36, 0x20, 0x2A, 0x00}, {0x3C, 0x00, 0x32, 0x00}, {0xFF, 0x00, 0x00, 0x00}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}}},
DefaultSymbolChatEntry{{"\tJ\xE3\x81\xB0\xE3\x82\x93\xE3\x81\x96\xE3\x83\xBC\xE3\x81\x84", "\tEHurrah!", "\tEHurra!", "\tEHourra !", "\tEHurra", "\tB\xE4\xB8\x87\xE5\xB2\x81", "\tT\xE8\x90\xAC\xE6\xAD\xB2", "\tK\xEB\xA7\x8C\xEC\x84\xB8"}, 0x28, {0x0362, 0x0362, 0xFFFF, 0xFFFF}, {SymbolChat::FacePart{0x09, 0x16, 0x1B, 0x00}, {0x09, 0x2B, 0x1B, 0x01}, {0x37, 0x20, 0x2C, 0x00}, {0xFF, 0x00, 0x00, 0x00}, {0xFF, 0x00, 0x00, 0x00}, {0xFF, 0x00, 0x00, 0x00}, {0xFF, 0x00, 0x00, 0x00}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}}},
DefaultSymbolChatEntry{{"\tJ\xE3\x81\x86\xE3\x81\x87\xEF\xBD\x9E\xE3\x82\x93", "\tECrying", "\tEIch bin sauer!", "\tEJe suis triste", "\tELlanto", "\tB\xE5\x96\x82\xEF\xBD\x9E", "\tT\xE5\x96\x82\xEF\xBD\x9E", "\tK\xEC\x9D\x91~"}, 0x74, {0x074F, 0xFFFF, 0xFFFF, 0xFFFF}, {SymbolChat::FacePart{0x06, 0x15, 0x14, 0x00}, {0x06, 0x2B, 0x14, 0x01}, {0x05, 0x18, 0x1F, 0x00}, {0x05, 0x28, 0x1F, 0x01}, {0x21, 0x20, 0x2E, 0x00}, {0xFF, 0x00, 0x00, 0x00}, {0xFF, 0x00, 0x00, 0x00}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}}},
DefaultSymbolChatEntry{{"\tJ\xE3\x81\x8A\xE3\x81\x93\xE3\x81\xA3\xE3\x81\x9F\xEF\xBC\x81", "\tEI'm angry!", "\tEWeinen", "\tEJe suis en colere !", "\tEEnfado", "\tB\xE7\x94\x9F\xE6\xB0\x94\xE4\xBA\x86\xEF\xBC\x81", "\tT\xE7\x94\x9F\xE6\xB0\xA3\xE4\xBA\x86\xEF\xBC\x81", "\tK\xEB\x82\x98\xEC\x99\x94\xEB\x8B\xA4\xEF\xBC\x81"}, 0x5C, {0x0116, 0x0001, 0xFFFF, 0xFFFF}, {SymbolChat::FacePart{0x0B, 0x18, 0x1B, 0x01}, {0x0B, 0x28, 0x1B, 0x00}, {0x33, 0x20, 0x2A, 0x00}, {0xFF, 0x00, 0x00, 0x00}, {0xFF, 0x00, 0x00, 0x00}, {0xFF, 0x00, 0x00, 0x00}, {0xFF, 0x00, 0x00, 0x00}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}}},
DefaultSymbolChatEntry{{"\tJ\xE3\x81\x9F\xE3\x81\x99\xE3\x81\x91\xE3\x81\xA6\xEF\xBC\x81", "\tEHelp me!", "\tEHilf mir!", "\tEAide-moi !", "\tEAyuda", "\tB\xE6\x95\x91\xE5\x91\xBD\xE5\x95\x8A\xEF\xBC\x81", "\tT\xE6\x95\x91\xE5\x91\xBD\xE5\x95\x8A\xEF\xBC\x81", "\tK\xEB\x8F\x84\xEC\x99\x80\xEC\xA4\x98\xEF\xBC\x81"}, 0xEC, {0x065E, 0x0138, 0xFFFF, 0xFFFF}, {SymbolChat::FacePart{0x02, 0x17, 0x1B, 0x01}, {0x02, 0x2A, 0x1B, 0x00}, {0x31, 0x20, 0x2C, 0x00}, {0xFF, 0x00, 0x00, 0x00}, {0xFF, 0x00, 0x00, 0x00}, {0xFF, 0x00, 0x00, 0x00}, {0xFF, 0x00, 0x00, 0x00}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}, {0xFF, 0x00, 0x00, 0x02}}},
};
const array<uint16_t, 20> PSOBBCharacterFile::DEFAULT_TECH_MENU_CONFIG = {
+11 -10
View File
@@ -142,7 +142,7 @@ struct PSOBBTeamMembership {
/* 0011 */ uint8_t unknown_a7 = 0;
/* 0012 */ uint8_t unknown_a8 = 0;
/* 0013 */ uint8_t unknown_a9 = 0;
/* 0014 */ pstring<TextEncoding::UTF16, 0x0010> team_name;
/* 0014 */ pstring<TextEncoding::UTF16_ALWAYS_MARKED, 0x10> team_name;
/* 0034 */ parray<le_uint16_t, 0x20 * 0x20> flag_data;
/* 0834 */ le_uint32_t reward_flags = 0;
/* 0838 */
@@ -173,18 +173,18 @@ struct PSOBBFullSystemFile {
struct PSOBBCharacterFile {
struct SymbolChatEntry {
/* 00 */ le_uint32_t present = 0;
/* 04 */ pstring<TextEncoding::UTF16, 0x14> name;
/* 04 */ pstring<TextEncoding::UTF16_ALWAYS_MARKED, 0x14> name;
/* 2C */ SymbolChat data;
/* 68 */
} __attribute__((packed));
struct DefaultSymbolChatEntry {
const char* name;
std::array<const char*, 8> language_to_name;
uint32_t spec;
std::array<uint16_t, 4> corner_objects;
std::array<SymbolChat::FacePart, 12> face_parts;
SymbolChatEntry to_entry() const;
SymbolChatEntry to_entry(uint8_t language) const;
};
/* 0000 */ PlayerInventory inventory;
@@ -219,6 +219,8 @@ struct PSOBBCharacterFile {
PSOBBCharacterFile() = default;
PlayerDispDataBBPreview to_preview() const;
static std::shared_ptr<PSOBBCharacterFile> create_from_config(
uint32_t guild_card_number,
uint8_t language,
@@ -231,8 +233,8 @@ struct PSOBBCharacterFile {
const PlayerDispDataBBPreview& preview,
std::shared_ptr<const LevelTable> level_table);
void add_item(const ItemData& item, Version version);
ItemData remove_item(uint32_t item_id, uint32_t amount, Version version);
void add_item(const ItemData& item, const ItemData::StackLimits& limits);
ItemData remove_item(uint32_t item_id, uint32_t amount, const ItemData::StackLimits& limits);
void add_meseta(uint32_t amount);
void remove_meseta(uint32_t amount, bool allow_overdraft);
@@ -336,9 +338,8 @@ struct PSOGCCharacterFile {
// H = Player lobby labels (0 = name; 1 = name, language, and level;
// 2 = W/D counts; 3 = challenge rank; 4 = nothing)
// I = Idle disconnect time (0 = 15 mins; 1 = 30 mins; 2 = 45 mins;
// 3 = 60 mins; 4 or 5: immediately; 6: 16 seconds; 7: 32 seconds).
// Obviously the behaviors for 4-7 are unintended; this is the result
// of a missing bounds check.
// 3 = 60 mins; 4: never; 5-7: undefined behavior due to a missing
// bounds check).
// J = Message speed (0 = slow; 1 = normal; 2 = fast; 3 = very fast)
// P = Cursor position (0 = saved; 1 = non-saved)
// Q = Button config (0 = normal; 1 = L/R reversed)
@@ -784,6 +785,6 @@ struct LegacySavedAccountDataBB { // .nsa file format
/* E13C */ le_uint32_t option_flags;
/* E140 */ parray<uint8_t, 0x0A40> shortcuts;
/* EB80 */ parray<PSOBBCharacterFile::SymbolChatEntry, 12> symbol_chats;
/* F060 */ pstring<TextEncoding::UTF16, 0x0010> team_name;
/* F060 */ pstring<TextEncoding::UTF16_ALWAYS_MARKED, 0x10> team_name;
/* F080 */
} __attribute__((packed));
+335 -133
View File
@@ -2,6 +2,7 @@
#include <event2/buffer.h>
#include <inttypes.h>
#include <stdarg.h>
#include <string.h>
#include <functional>
@@ -17,6 +18,7 @@
#include "Compression.hh"
#include "FileContentsCache.hh"
#include "PSOProtocol.hh"
#include "ProxyServer.hh"
#include "StaticGameData.hh"
#include "Text.hh"
@@ -152,7 +154,6 @@ static const char* dc_port_map_copyright = "DreamCast Port Map. Copyright SEGA E
static const char* dc_lobby_server_copyright = "DreamCast Lobby Server. Copyright SEGA Enterprises. 1999";
static const char* bb_game_server_copyright = "Phantasy Star Online Blue Burst Game Server. Copyright 1999-2004 SONICTEAM.";
static const char* bb_pm_server_copyright = "PSO NEW PM Server. Copyright 1999-2002 SONICTEAM.";
static const char* patch_server_copyright = "Patch Server. Copyright SonicTeam, LTD. 2001";
S_ServerInitWithAfterMessage_DC_PC_V3_02_17_91_9B<0xB4>
prepare_server_init_contents_console(
@@ -240,20 +241,6 @@ void send_server_init_bb(shared_ptr<Client> c, uint8_t flags) {
sizeof(cmd.basic_cmd.server_key), true);
}
void send_server_init_patch(shared_ptr<Client> c) {
uint32_t server_key = random_object<uint32_t>();
uint32_t client_key = random_object<uint32_t>();
S_ServerInit_Patch_02 cmd;
cmd.copyright.encode(patch_server_copyright);
cmd.server_key = server_key;
cmd.client_key = client_key;
send_command_t(c, 0x02, 0x00, cmd);
c->channel.crypt_out = make_shared<PSOV2Encryption>(server_key);
c->channel.crypt_in = make_shared<PSOV2Encryption>(client_key);
}
void send_server_init(shared_ptr<Client> c, uint8_t flags) {
switch (c->version()) {
case Version::DC_NTE:
@@ -269,10 +256,6 @@ void send_server_init(shared_ptr<Client> c, uint8_t flags) {
case Version::XB_V3:
send_server_init_dc_pc_v3(c, flags);
break;
case Version::PC_PATCH:
case Version::BB_PATCH:
send_server_init_patch(c);
break;
case Version::BB_V4:
send_server_init_bb(c, flags);
break;
@@ -494,6 +477,62 @@ void send_function_call(
ch.send(0xB2, code ? code->index : 0x00, data);
}
bool send_protected_command(std::shared_ptr<Client> c, const void* data, size_t size, bool echo_to_lobby) {
switch (c->version()) {
case Version::DC_NTE:
case Version::DC_V1_11_2000_PROTOTYPE:
case Version::DC_V1:
case Version::DC_V2:
case Version::PC_NTE:
case Version::PC_V2:
case Version::GC_NTE:
if (echo_to_lobby) {
send_command(c->require_lobby(), 0x60, 0x00, data, size);
} else {
send_command(c, 0x60, 0x00, data, size);
}
return true;
case Version::GC_V3:
case Version::XB_V3:
case Version::GC_EP3_NTE:
case Version::GC_EP3: {
auto s = c->require_server_state();
if (!s->enable_v3_v4_protected_subcommands ||
c->config.check_flag(Client::Flag::NO_SEND_FUNCTION_CALL) ||
c->config.check_flag(Client::Flag::SEND_FUNCTION_CALL_CHECKSUM_ONLY)) {
return false;
}
prepare_client_for_patches(c, [wc = weak_ptr<Client>(c), data = string(reinterpret_cast<const char*>(data), size), echo_to_lobby]() {
auto c = wc.lock();
if (!c) {
return;
}
try {
auto s = c->require_server_state();
auto fn = s->function_code_index->get_patch("CallProtectedHandler", c->config.specific_version);
uint32_t size_label_value = is_big_endian(c->version()) ? data.size() : bswap32(data.size());
send_function_call(c, fn, {{"size", size_label_value}}, data);
c->function_call_response_queue.emplace_back(empty_function_call_response_handler);
if (echo_to_lobby) {
auto l = c->lobby.lock();
if (l) {
send_command_excluding_client(l, c, 0x60, 0x00, data.data(), data.size());
}
}
} catch (const exception& e) {
c->log.warning("Failed to send protected command: %s", e.what());
}
});
return true;
}
default:
return false;
}
}
void send_reconnect(shared_ptr<Client> c, uint32_t address, uint16_t port) {
S_Reconnect_19 cmd = {{address, port, 0}};
send_command_t(c, is_patch(c->version()) ? 0x14 : 0x19, 0x00, cmd);
@@ -672,37 +711,9 @@ void send_complete_player_bb(shared_ptr<Client> c) {
if (team) {
cmd.system_file.team_membership = team->membership_for_member(c->license->serial_number);
}
cmd.char_file.disp.play_time = 0;
send_command_t(c, 0x00E7, 0x00000000, cmd);
}
////////////////////////////////////////////////////////////////////////////////
// patch functions
void send_enter_directory_patch(shared_ptr<Client> c, const string& dir) {
S_EnterDirectory_Patch_09 cmd = {{dir, 1}};
send_command_t(c, 0x09, 0x00, cmd);
}
void send_patch_file(shared_ptr<Client> c, shared_ptr<PatchFileIndex::File> f) {
S_OpenFile_Patch_06 open_cmd = {0, f->size, {f->name, 1}};
send_command_t(c, 0x06, 0x00, open_cmd);
for (size_t x = 0; x < f->chunk_crcs.size(); x++) {
auto data = f->load_data();
size_t chunk_size = min<uint32_t>(f->size - (x * 0x4000), 0x4000);
vector<pair<const void*, size_t>> blocks;
S_WriteFileHeader_Patch_07 cmd_header = {x, f->chunk_crcs[x], chunk_size};
blocks.emplace_back(&cmd_header, sizeof(cmd_header));
blocks.emplace_back(data->data() + (x * 0x4000), chunk_size);
send_command(c, 0x07, 0x00, blocks);
}
S_CloseCurrentFile_Patch_08 close_cmd = {0};
send_command_t(c, 0x08, 0x00, close_cmd);
}
////////////////////////////////////////////////////////////////////////////////
// message functions
@@ -852,10 +863,10 @@ void send_text_message(shared_ptr<Lobby> l, const string& text) {
}
void send_text_message(shared_ptr<ServerState> s, const string& text) {
// TODO: We should have a collection of all clients (even those not in any
// lobby) and use that instead here
for (auto& l : s->all_lobbies()) {
send_text_message(l, text);
for (auto& it : s->channel_to_client) {
if (it.second->license && !is_patch(it.second->version())) {
send_text_message(it.second, text);
}
}
}
@@ -883,7 +894,7 @@ string prepare_chat_data(
string data;
if (version == Version::BB_V4) {
data.append("\tJ");
data.append(language ? "\tE" : "\tJ");
}
data.append(from_name);
if (version == Version::DC_NTE) {
@@ -900,7 +911,7 @@ string prepare_chat_data(
data.append(text);
return tt_utf8_to_utf16(data);
} else if (version == Version::DC_NTE) {
data.append(tt_utf8_to_sjis(text));
data.append(tt_utf8_to_sega_sjis(text));
return data;
} else {
data.append(tt_encode_marked(text, language, false));
@@ -963,7 +974,7 @@ void send_chat_message(
template <typename CmdT>
void send_simple_mail_t(shared_ptr<Client> c, uint32_t from_guild_card_number, const string& from_name, const string& text) {
CmdT cmd;
cmd.player_tag = 0x00010000;
cmd.player_tag = from_guild_card_number ? 0x00010000 : 0;
cmd.from_guild_card_number = from_guild_card_number;
cmd.from_name.encode(from_name, c->language());
cmd.to_guild_card_number = c->license->serial_number;
@@ -973,7 +984,7 @@ void send_simple_mail_t(shared_ptr<Client> c, uint32_t from_guild_card_number, c
void send_simple_mail_bb(shared_ptr<Client> c, uint32_t from_guild_card_number, const string& from_name, const string& text) {
SC_SimpleMail_BB_81 cmd;
cmd.player_tag = 0x00010000;
cmd.player_tag = from_guild_card_number ? 0x00010000 : 0;
cmd.from_guild_card_number = from_guild_card_number;
cmd.from_name.encode(from_name, c->language());
cmd.to_guild_card_number = c->license->serial_number;
@@ -1007,6 +1018,14 @@ void send_simple_mail(shared_ptr<Client> c, uint32_t from_guild_card_number, con
}
}
void send_simple_mail(shared_ptr<ServerState> s, uint32_t from_guild_card_number, const string& from_name, const string& text) {
for (const auto& it : s->channel_to_client) {
if (it.second->license && !is_patch(it.second->version())) {
send_simple_mail(it.second, from_guild_card_number, from_name, text);
}
}
}
////////////////////////////////////////////////////////////////////////////////
// info board
@@ -1029,10 +1048,8 @@ void send_info_board_t(shared_ptr<Client> c) {
void send_info_board(shared_ptr<Client> c) {
if (uses_utf16(c->version())) {
send_info_board_t<TextEncoding::UTF16, TextEncoding::UTF16>(c);
} else if (c->language()) {
send_info_board_t<TextEncoding::ASCII, TextEncoding::ISO8859>(c);
} else {
send_info_board_t<TextEncoding::ASCII, TextEncoding::SJIS>(c);
send_info_board_t<TextEncoding::ASCII, TextEncoding::MARKED>(c);
}
}
@@ -1225,10 +1242,12 @@ void send_guild_card(
uint8_t char_class) {
switch (ch.version) {
case Version::DC_NTE:
send_guild_card_dc_pc_gc_t<G_SendGuildCard_DCNTE_6x06>(
ch, guild_card_number, name, description, language, section_id, char_class);
break;
case Version::DC_V1_11_2000_PROTOTYPE:
case Version::DC_V1:
case Version::DC_V2:
// TODO: Is this the right format and subcommand for NTE and the prototype?
send_guild_card_dc_pc_gc_t<G_SendGuildCard_DC_6x06>(
ch, guild_card_number, name, description, language, section_id, char_class);
break;
@@ -1461,7 +1480,9 @@ void send_game_menu(
shared_ptr<Client> c,
bool is_spectator_team_list,
bool show_tournaments_only) {
if (uses_utf16(c->version())) {
if (is_v4(c->version())) {
send_game_menu_t<TextEncoding::UTF16_ALWAYS_MARKED>(c, is_spectator_team_list, show_tournaments_only);
} else if (uses_utf16(c->version())) {
send_game_menu_t<TextEncoding::UTF16>(c, is_spectator_team_list, show_tournaments_only);
} else {
send_game_menu_t<TextEncoding::MARKED>(c, is_spectator_team_list, show_tournaments_only);
@@ -1685,12 +1706,7 @@ void populate_lobby_data_for_client<PlayerLobbyDataBB>(PlayerLobbyDataBB& ret, s
ret.team_id = 0;
}
string name = c->character()->disp.name.decode(c->language());
if ((name.size() >= 2) && (name[0] == '\t') && (name[1] != 'C')) {
ret.name.encode(name, viewer_c->language());
} else {
const char* marker = c->language() ? "\tE" : "\tJ";
ret.name.encode(marker + name, viewer_c->language());
}
ret.name.encode(name, viewer_c->language());
}
static void send_join_spectator_team(shared_ptr<Client> c, shared_ptr<Lobby> l) {
@@ -1711,7 +1727,7 @@ static void send_join_spectator_team(shared_ptr<Client> c, shared_ptr<Lobby> l)
cmd.variations.clear(0);
cmd.client_id = c->lobby_client_id;
cmd.event = l->event;
cmd.section_id = l->section_id;
cmd.section_id = l->effective_section_id();
cmd.rare_seed = l->random_seed;
cmd.episode = 0xFF;
@@ -1743,6 +1759,11 @@ static void send_join_spectator_team(shared_ptr<Client> c, shared_ptr<Lobby> l)
: wc_p->disp.stats.level.load();
e.name_color = wc_p->disp.visual.name_color;
if (s->version_name_colors) {
p.disp.visual.name_color = s->name_color_for_version(wc->version());
e.name_color = p.disp.visual.name_color;
}
player_count++;
}
@@ -1806,6 +1827,11 @@ static void send_join_spectator_team(shared_ptr<Client> c, shared_ptr<Lobby> l)
: other_p->disp.stats.level.load();
cmd_e.name_color = other_p->disp.visual.name_color;
if (s->version_name_colors) {
cmd_p.disp.visual.name_color = s->name_color_for_version(other_c->version());
cmd_e.name_color = cmd_p.disp.visual.name_color;
}
player_count++;
}
}
@@ -1841,7 +1867,7 @@ void send_join_game(shared_ptr<Client> c, shared_ptr<Lobby> l) {
cmd.difficulty = l->difficulty;
cmd.battle_mode = (l->mode == GameMode::BATTLE) ? 1 : 0;
cmd.event = l->event;
cmd.section_id = l->section_id;
cmd.section_id = l->effective_section_id();
cmd.challenge_mode = (l->mode == GameMode::CHALLENGE) ? 1 : 0;
cmd.rare_seed = l->random_seed;
return populate_lobby_data(cmd);
@@ -1907,10 +1933,14 @@ void send_join_game(shared_ptr<Client> c, shared_ptr<Lobby> l) {
for (size_t x = 0; x < 4; x++) {
if (l->clients[x]) {
auto other_p = l->clients[x]->character();
cmd.players_ep3[x].inventory = other_p->inventory;
cmd.players_ep3[x].inventory.encode_for_client(c);
cmd.players_ep3[x].disp = convert_player_disp_data<PlayerDispDataDCPCV3>(other_p->disp, c->language(), other_p->inventory.language);
cmd.players_ep3[x].disp.enforce_lobby_join_limits_for_version(c->version());
auto& cmd_p = cmd.players_ep3[x];
cmd_p.inventory = other_p->inventory;
cmd_p.inventory.encode_for_client(c);
cmd_p.disp = convert_player_disp_data<PlayerDispDataDCPCV3>(other_p->disp, c->language(), other_p->inventory.language);
cmd_p.disp.enforce_lobby_join_limits_for_version(c->version());
if (s->version_name_colors) {
cmd_p.disp.visual.name_color = s->name_color_for_version(l->clients[x]->version());
}
}
}
send_command_t(c, 0x64, player_count, cmd);
@@ -2027,6 +2057,12 @@ void send_join_lobby_t(shared_ptr<Client> c, shared_ptr<Lobby> l, shared_ptr<Cli
} else {
e.disp = convert_player_disp_data<DispDataT>(lp->disp, c->language(), lp->inventory.language);
e.disp.enforce_lobby_join_limits_for_version(c->version());
if (s->version_name_colors) {
e.disp.visual.name_color = s->name_color_for_version(lc->version());
if (is_v1_or_v2(c->version())) {
e.disp.visual.compute_name_color_checksum();
}
}
}
}
@@ -2093,6 +2129,9 @@ void send_join_lobby_xb(shared_ptr<Client> c, shared_ptr<Lobby> l, shared_ptr<Cl
e.inventory.encode_for_client(c);
e.disp = convert_player_disp_data<PlayerDispDataDCPCV3>(lp->disp, c->language(), lp->inventory.language);
e.disp.enforce_lobby_join_limits_for_version(c->version());
if (s->version_name_colors) {
e.disp.visual.name_color = s->name_color_for_version(lc->version());
}
}
send_command(c, command, used_entries, &cmd, cmd.size(used_entries));
@@ -2141,6 +2180,10 @@ void send_join_lobby_dc_nte(shared_ptr<Client> c, shared_ptr<Lobby> l,
} else {
e.disp = convert_player_disp_data<PlayerDispDataDCPCV3>(lp->disp, c->language(), lp->inventory.language);
e.disp.enforce_lobby_join_limits_for_version(c->version());
if (s->version_name_colors) {
e.disp.visual.name_color = s->name_color_for_version(lc->version());
e.disp.visual.compute_name_color_checksum();
}
}
}
@@ -2367,12 +2410,14 @@ void send_player_stats_change(Channel& ch, uint16_t client_id, PlayerStatsChange
}
void send_remove_conditions(shared_ptr<Client> c) {
auto l = c->require_lobby();
for (auto& lc : l->clients) {
if (lc) {
send_remove_conditions(lc->channel, c->lobby_client_id);
}
parray<G_AddOrRemoveCondition_6x0C_6x0D, 4> cmds;
for (size_t z = 0; z < 4; z++) {
auto& cmd = cmds[z];
cmd.header = {0x0D, sizeof(G_AddOrRemoveCondition_6x0C_6x0D) >> 2, c->lobby_client_id};
cmd.unknown_a1 = z;
cmd.unknown_a2 = 0;
}
send_protected_command(c, &cmds, sizeof(cmds), true);
}
void send_remove_conditions(Channel& ch, uint16_t client_id) {
@@ -2394,6 +2439,7 @@ void send_warp(Channel& ch, uint8_t client_id, uint32_t floor, bool is_private)
void send_warp(shared_ptr<Client> c, uint32_t floor, bool is_private) {
send_warp(c->channel, c->lobby_client_id, floor, is_private);
c->floor = floor;
c->recent_switch_flags.clear();
}
void send_warp(shared_ptr<Lobby> l, uint32_t floor, bool is_private) {
@@ -2409,6 +2455,59 @@ void send_ep3_change_music(Channel& ch, uint32_t song) {
ch.send(0x60, 0x00, cmd);
}
void send_game_join_sync_command(
shared_ptr<Client> c, const void* data, size_t size, uint8_t dc_nte_sc, uint8_t dc_11_2000_sc, uint8_t sc) {
string compressed_data = bc0_compress(data, size);
send_game_join_sync_command_compressed(c, compressed_data.data(), compressed_data.size(), size, dc_nte_sc, dc_11_2000_sc, sc);
}
void send_game_join_sync_command(shared_ptr<Client> c, const string& data, uint8_t dc_nte_sc, uint8_t dc_11_2000_sc, uint8_t sc) {
send_game_join_sync_command(c, data.data(), data.size(), dc_nte_sc, dc_11_2000_sc, sc);
}
void send_game_join_sync_command_compressed(
shared_ptr<Client> c,
const void* data,
size_t size,
size_t decompressed_size,
uint8_t dc_nte_sc,
uint8_t dc_11_2000_sc,
uint8_t sc) {
StringWriter w;
if (is_pre_v1(c->version())) {
G_SyncGameStateHeader_DCNTE_6x6B_6x6C_6x6D_6x6E compressed_header;
compressed_header.header.basic_header.subcommand = (c->version() == Version::DC_NTE) ? dc_nte_sc : dc_11_2000_sc;
compressed_header.header.basic_header.size = 0x00;
compressed_header.header.basic_header.unused = 0x0000;
compressed_header.header.size = (size + sizeof(G_SyncGameStateHeader_DCNTE_6x6B_6x6C_6x6D_6x6E) + 3) & (~3);
compressed_header.decompressed_size = decompressed_size;
w.put(compressed_header);
} else {
G_SyncGameStateHeader_6x6B_6x6C_6x6D_6x6E compressed_header;
compressed_header.header.basic_header.subcommand = sc;
compressed_header.header.basic_header.size = 0x00;
compressed_header.header.basic_header.unused = 0x0000;
compressed_header.header.size = (size + sizeof(G_SyncGameStateHeader_6x6B_6x6C_6x6D_6x6E) + 3) & (~3);
compressed_header.decompressed_size = decompressed_size;
compressed_header.compressed_size = size;
w.put(compressed_header);
}
w.write(data, size);
while (w.size() & 3) {
w.put_u8(0x00);
}
if (c->game_join_command_queue) {
c->log.info("Client not ready to receive join commands; adding to queue");
auto& cmd = c->game_join_command_queue->emplace_back();
cmd.command = 0x6D;
cmd.flag = c->lobby_client_id;
cmd.data = std::move(w.str());
} else {
send_command(c, 0x6D, c->lobby_client_id, w.str());
}
}
void send_game_item_state(shared_ptr<Client> c) {
auto l = c->require_lobby();
auto s = c->require_server_state();
@@ -2456,66 +2555,164 @@ void send_game_item_state(shared_ptr<Client> c) {
StringWriter decompressed_w;
decompressed_w.put(decompressed_header);
decompressed_w.write(floor_items_w.str());
const auto& data = decompressed_w.str();
send_game_join_sync_command(c, data.data(), data.size(), 0x5E, 0x65, 0x6D);
}
string compressed_data = bc0_compress(decompressed_w.str());
void send_game_enemy_state(shared_ptr<Client> c) {
auto l = c->require_lobby();
if (!l->map) {
return;
}
auto s = c->require_server_state();
vector<G_SyncEnemyState_6x6B_Entry_Decompressed> entries;
entries.reserve(l->map->enemies.size());
for (size_t z = 0; z < l->map->enemies.size(); z++) {
const auto& enemy = l->map->enemies[z];
auto& entry = entries.emplace_back();
entry.flags = enemy.game_flags;
entry.item_drop_id = (enemy.state_flags & Map::Enemy::Flag::ITEM_DROPPED) ? 0xFFFF : (0xCA0 + z);
entry.total_damage = enemy.total_damage;
}
send_game_join_sync_command(c, entries.data(), entries.size() * sizeof(entries[0]), 0x5C, 0x63, 0x6B);
}
void send_game_object_state(shared_ptr<Client> c) {
auto l = c->require_lobby();
if (!l->map) {
return;
}
auto s = c->require_server_state();
vector<G_SyncObjectState_6x6C_Entry_Decompressed> entries;
entries.reserve(l->map->objects.size());
for (size_t z = 0; z < l->map->objects.size(); z++) {
const auto& obj = l->map->objects[z];
auto& entry = entries.emplace_back();
entry.flags = obj.game_flags;
entry.item_drop_id = (obj.item_drop_checked) ? 0xFFFF : (0x100 + z);
}
send_game_join_sync_command(c, entries.data(), entries.size() * sizeof(entries[0]), 0x5D, 0x64, 0x6C);
}
void send_game_set_state(shared_ptr<Client> c) {
auto l = c->require_lobby();
if (!l->map) {
return;
}
size_t num_object_sets = l->map->objects.size();
size_t num_enemy_sets = l->map->enemy_set_flags.size();
G_SyncSetFlagState_6x6E_Decompressed::EntitySetFlags entity_set_flags_header;
entity_set_flags_header.object_set_flags_offset = sizeof(entity_set_flags_header);
entity_set_flags_header.num_object_sets = num_object_sets;
entity_set_flags_header.enemy_set_flags_offset = sizeof(entity_set_flags_header) + num_object_sets * sizeof(le_uint16_t);
entity_set_flags_header.num_enemy_sets = num_enemy_sets;
G_SyncSetFlagState_6x6E_Decompressed header;
header.entity_set_flags_size = sizeof(entity_set_flags_header) + (num_object_sets + num_enemy_sets) * sizeof(le_uint16_t);
header.event_set_flags_size = sizeof(le_uint16_t) * l->map->events.size();
header.switch_flags_size = is_v1(c->version()) ? 0x200 : 0x240;
header.total_size = header.entity_set_flags_size + header.event_set_flags_size + header.switch_flags_size;
StringWriter w;
if (is_pre_v1(c->version())) {
G_SyncGameStateHeader_DCNTE_6x6B_6x6C_6x6D_6x6E compressed_header;
compressed_header.header.basic_header.subcommand = (c->version() == Version::DC_NTE) ? 0x5E : 0x65;
compressed_header.header.basic_header.size = 0x00;
compressed_header.header.basic_header.unused = 0x0000;
compressed_header.header.size = (compressed_data.size() + sizeof(G_SyncGameStateHeader_DCNTE_6x6B_6x6C_6x6D_6x6E) + 3) & (~3);
compressed_header.decompressed_size = decompressed_w.size();
w.put(compressed_header);
} else {
G_SyncGameStateHeader_6x6B_6x6C_6x6D_6x6E compressed_header;
compressed_header.header.basic_header.subcommand = 0x6D;
compressed_header.header.basic_header.size = 0x00;
compressed_header.header.basic_header.unused = 0x0000;
compressed_header.header.size = (compressed_data.size() + sizeof(G_SyncGameStateHeader_6x6B_6x6C_6x6D_6x6E) + 3) & (~3);
compressed_header.decompressed_size = decompressed_w.size();
compressed_header.compressed_size = compressed_data.size();
w.put(compressed_header);
w.put(header);
w.put(entity_set_flags_header);
for (const auto& obj : l->map->objects) {
w.put_u16l(obj.set_flags);
}
w.write(compressed_data);
while (w.size() & 3) {
w.put_u8(0x00);
for (uint16_t enemy_set_flags : l->map->enemy_set_flags) {
w.put_u16l(enemy_set_flags);
}
for (const auto& event : l->map->events) {
w.put_u16l(event.flags);
}
if (l->switch_flags) {
static_assert(sizeof(SwitchFlags) == 0x240, "switch_flags size is incorrect");
w.write(l->switch_flags->data.data(), header.switch_flags_size);
} else {
w.extend_by(header.switch_flags_size, 0x00);
}
if (c->game_join_command_queue) {
c->log.info("Client not ready to receive join commands; adding to queue");
auto& cmd = c->game_join_command_queue->emplace_back();
cmd.command = 0x6D;
cmd.flag = c->lobby_client_id;
cmd.data = std::move(w.str());
} else {
send_command(c, 0x6D, c->lobby_client_id, w.str());
send_game_join_sync_command(c, w.str(), 0x5F, 0x66, 0x6E);
}
template <typename CmdT>
void send_game_flag_state_t(shared_ptr<Client> c) {
auto l = c->require_lobby();
if (l->quest_flags_known) { // Not all flags known; send multiple 6x75s
StringWriter w;
bool use_v3_cmd = !is_v1_or_v2(c->version()) || (c->version() == Version::GC_NTE);
for (uint8_t difficulty = 0; difficulty < 4; difficulty++) {
if ((difficulty != l->difficulty) && !use_v3_cmd) {
continue;
}
const auto& diff_flags = l->quest_flag_values->data.at(difficulty);
const auto& diff_known_flags = l->quest_flags_known->data.at(difficulty);
for (uint8_t z = 0; z < diff_known_flags.data.size(); z++) {
uint8_t known_flags = diff_known_flags.data[z];
if (!known_flags) {
continue;
}
uint8_t flag_values = diff_flags.data[z];
for (uint8_t sh = 0; sh < 8; sh++) {
if ((known_flags << sh) & 0x80) {
uint16_t flag_num = ((z << 3) | sh);
if (use_v3_cmd) {
w.put(G_UpdateQuestFlag_V3_BB_6x75{
{{0x75, 0x03, 0x0000}, flag_num, (((flag_values << sh) & 0x80) ? 0 : 1)}, difficulty, 0});
} else {
w.put(G_UpdateQuestFlag_DC_PC_6x75{
{0x75, 0x02, 0x0000}, flag_num, (((flag_values << sh) & 0x80) ? 0 : 1)});
}
}
}
}
}
if (w.size() > 0) {
if (c->game_join_command_queue) {
c->log.info("Client not ready to receive join commands; adding to queue");
auto& cmd = c->game_join_command_queue->emplace_back();
cmd.command = 0x006D;
cmd.flag = c->lobby_client_id;
cmd.data = std::move(w.str());
} else {
send_command(c, 0x6D, c->lobby_client_id, w.str());
}
}
} else { // All flags known; send 6x6F
CmdT cmd;
cmd.header.subcommand = 0x6F;
cmd.header.size = sizeof(CmdT) >> 2;
cmd.header.unused = 0x0000;
cmd.quest_flags = (l && !l->quest_flags_known) ? *l->quest_flag_values : c->character()->quest_flags;
if (c->game_join_command_queue) {
c->log.info("Client not ready to receive join commands; adding to queue");
auto& queue_cmd = c->game_join_command_queue->emplace_back();
queue_cmd.command = 0x0062;
queue_cmd.flag = c->lobby_client_id;
queue_cmd.data.assign(reinterpret_cast<const char*>(&cmd), sizeof(cmd));
} else {
send_command_t(c, 0x62, c->lobby_client_id, cmd);
}
}
}
void send_game_flag_state(shared_ptr<Client> c) {
auto l = c->require_lobby();
G_SetQuestFlags_6x6F cmd;
cmd.header.subcommand = 0x6F;
cmd.header.size = sizeof(G_SetQuestFlags_6x6F) >> 2;
cmd.header.unused = 0x0000;
cmd.quest_flags = c->character()->quest_flags;
for (const auto& lc : l->clients) {
if (!lc) {
continue;
}
if (lc->game_join_command_queue) {
lc->log.info("Client not ready to receive join commands; adding to queue");
auto& cmd = lc->game_join_command_queue->emplace_back();
cmd.command = 0x0060;
cmd.flag = 0x00000000;
cmd.data.assign(reinterpret_cast<const char*>(&cmd), sizeof(cmd));
} else {
send_command_t(lc, 0x60, 0x00, cmd);
}
// DC NTE and 11/2000 don't have this command at all; v1 has it but it doesn't
// include flags for Ultimate.
if (!is_v1(c->version())) {
send_game_flag_state_t<G_SetQuestFlagsV2V3V4_6x6F>(c);
} else if (!is_pre_v1(c->version())) {
send_game_flag_state_t<G_SetQuestFlagsV1_6x6F>(c);
}
}
@@ -2840,12 +3037,11 @@ void send_ep3_confirm_tournament_entry(
send_command_t(c, 0xCC, tourn ? 0x01 : 0x00, cmd);
}
void send_ep3_tournament_list(
shared_ptr<Client> c,
bool is_for_spectator_team_create) {
template <typename CmdT>
void send_ep3_tournament_list_t(shared_ptr<Client> c, bool is_for_spectator_team_create) {
auto s = c->require_server_state();
S_TournamentList_Ep3_E0 cmd;
CmdT cmd;
size_t z = 0;
for (const auto& it : s->ep3_tournament_index->all_tournaments()) {
const auto& tourn = it.second;
@@ -2876,13 +3072,19 @@ void send_ep3_tournament_list(
}
}
entry.max_teams = teams.size();
entry.unknown_a3 = 0xFFFF;
entry.unknown_a4 = 0xFFFF;
z++;
}
send_command_t(c, 0xE0, z, cmd);
}
void send_ep3_tournament_list(shared_ptr<Client> c, bool is_for_spectator_team_create) {
if (c->version() == Version::GC_EP3_NTE) {
send_ep3_tournament_list_t<S_TournamentList_Ep3NTE_E0>(c, is_for_spectator_team_create);
} else {
send_ep3_tournament_list_t<S_TournamentList_Ep3_E0>(c, is_for_spectator_team_create);
}
}
void send_ep3_tournament_entry_list(
shared_ptr<Client> c,
shared_ptr<const Episode3::Tournament> tourn,
+22 -4
View File
@@ -159,6 +159,7 @@ void send_function_call(
uint32_t checksum_addr = 0,
uint32_t checksum_size = 0,
uint32_t override_relocations_offset = 0);
bool send_protected_command(std::shared_ptr<Client> c, const void* data, size_t size, bool echo_to_lobby);
void send_reconnect(std::shared_ptr<Client> c, uint32_t address, uint16_t port);
void send_pc_console_split_reconnect(
@@ -178,9 +179,6 @@ void send_stream_file_chunk_bb(std::shared_ptr<Client> c, uint32_t chunk_index);
void send_approve_player_choice_bb(std::shared_ptr<Client> c);
void send_complete_player_bb(std::shared_ptr<Client> c);
void send_enter_directory_patch(std::shared_ptr<Client> c, const std::string& dir);
void send_patch_file(std::shared_ptr<Client> c, std::shared_ptr<PatchFileIndex::File> f);
void send_message_box(std::shared_ptr<Client> c, const std::string& text);
void send_ep3_timed_message_box(Channel& ch, uint32_t frames, const std::string& text);
void send_lobby_name(std::shared_ptr<Client> c, const std::string& text);
@@ -221,7 +219,12 @@ void send_chat_message(
char private_flags);
void send_simple_mail(
std::shared_ptr<Client> c,
uint32_t from_serial_number,
uint32_t from_guild_card_number,
const std::string& from_name,
const std::string& text);
void send_simple_mail(
std::shared_ptr<ServerState> s,
uint32_t from_guild_card_number,
const std::string& from_name,
const std::string& text);
@@ -307,7 +310,22 @@ void send_warp(std::shared_ptr<Lobby> l, uint32_t floor, bool is_private);
void send_ep3_change_music(Channel& ch, uint32_t song);
void send_revive_player(std::shared_ptr<Client> c);
void send_game_join_sync_command(
std::shared_ptr<Client> c, const void* data, size_t size, uint8_t dc_nte_sc, uint8_t dc_11_2000_sc, uint8_t sc);
void send_game_join_sync_command(
std::shared_ptr<Client> c, const std::string& data, uint8_t dc_nte_sc, uint8_t dc_11_2000_sc, uint8_t sc);
void send_game_join_sync_command_compressed(
std::shared_ptr<Client> c,
const void* data,
size_t size,
size_t decompressed_size,
uint8_t dc_nte_sc,
uint8_t dc_11_2000_sc,
uint8_t sc);
void send_game_item_state(std::shared_ptr<Client> c);
void send_game_enemy_state(std::shared_ptr<Client> c);
void send_game_object_state(std::shared_ptr<Client> c);
void send_game_set_state(std::shared_ptr<Client> c);
void send_game_flag_state(std::shared_ptr<Client> c);
void send_drop_item_to_channel(std::shared_ptr<ServerState> s, Channel& ch, const ItemData& item,
bool from_enemy, uint8_t floor, float x, float z, uint16_t request_id);
+2 -7
View File
@@ -134,6 +134,7 @@ void Server::connect_client(
c->channel.on_command_received = Server::on_client_input;
c->channel.on_error = Server::on_client_error;
c->channel.context_obj = this;
this->state->channel_to_client.emplace(&c->channel, c);
server_log.info(
"Client connected: C-%" PRIX64 " on virtual connection %p via T-%hu-%s-%s-VI",
@@ -143,8 +144,6 @@ void Server::connect_client(
name_for_enum(version),
name_for_enum(initial_state));
this->state->channel_to_client.emplace(&c->channel, c);
// Manually set the remote address, since the bufferevent has no fd and the
// Channel constructor can't figure out the virtual remote address
auto* remote_sin = reinterpret_cast<sockaddr_in*>(&c->channel.remote_addr);
@@ -217,11 +216,7 @@ Server::Server(
destroy_clients_ev(event_new(this->base.get(), -1, EV_TIMEOUT, &Server::dispatch_destroy_clients, this), event_free),
state(state) {}
void Server::listen(
const std::string& addr_str,
const string& socket_path,
Version version,
ServerBehavior behavior) {
void Server::listen(const std::string& addr_str, const string& socket_path, Version version, ServerBehavior behavior) {
int fd = ::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());
+2 -4
View File
@@ -30,8 +30,7 @@ public:
void disconnect_client(std::shared_ptr<Client> c);
std::shared_ptr<Client> get_client() const;
std::vector<std::shared_ptr<Client>> get_clients_by_identifier(
const std::string& ident) const;
std::vector<std::shared_ptr<Client>> get_clients_by_identifier(const std::string& ident) const;
std::shared_ptr<struct event_base> get_base() const;
inline std::shared_ptr<ServerState> get_state() const {
@@ -69,8 +68,7 @@ private:
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_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);
+751 -663
View File
File diff suppressed because it is too large Load Diff
+14 -13
View File
@@ -6,27 +6,28 @@
#include <event2/event.h>
#include "ProxyServer.hh"
#include "Shell.hh"
#include "ServerState.hh"
#define SHELL_PROMPT "newserv> "
class ServerShell : public Shell {
class ServerShell : public std::enable_shared_from_this<ServerShell> {
public:
ServerShell(
std::shared_ptr<struct event_base> base,
std::shared_ptr<ServerState> state);
virtual ~ServerShell() = default;
class exit_shell : public std::runtime_error {
public:
exit_shell();
~exit_shell() = default;
};
explicit ServerShell(std::shared_ptr<ServerState> state);
ServerShell(const ServerShell&) = delete;
ServerShell(ServerShell&&) = delete;
ServerShell& operator=(const ServerShell&) = delete;
ServerShell& operator=(ServerShell&&) = delete;
~ServerShell();
std::shared_ptr<ProxyServer::LinkedSession> get_proxy_session(const std::string& name);
protected:
std::shared_ptr<ServerState> state;
std::thread th;
std::shared_ptr<ProxyServer::LinkedSession> get_proxy_session(
const std::string& name);
virtual void print_prompt();
virtual void execute_command(const std::string& command);
void thread_fn();
};
+550 -367
View File
File diff suppressed because it is too large Load Diff
+68 -32
View File
@@ -15,6 +15,7 @@
#include "CommonItemSet.hh"
#include "Episode3/DataIndexes.hh"
#include "Episode3/Tournament.hh"
#include "EventUtils.hh"
#include "FunctionCompiler.hh"
#include "GSLArchive.hh"
#include "ItemNameIndex.hh"
@@ -23,9 +24,9 @@
#include "License.hh"
#include "Lobby.hh"
#include "Menu.hh"
#include "PatchServer.hh"
#include "PlayerFilesManager.hh"
#include "Quest.hh"
#include "StepGraph.hh"
#include "TeamIndex.hh"
#include "WordSelectTable.hh"
@@ -65,12 +66,11 @@ struct ServerState : public std::enable_shared_from_this<ServerState> {
std::shared_ptr<struct event_base> base;
std::string config_filename;
std::shared_ptr<const JSON> config_json;
bool is_replay = false;
bool config_loaded = false;
bool one_time_config_loaded = false;
bool default_lobbies_created = false;
StepGraph load_step_graph;
std::string name;
std::unordered_map<std::string, std::shared_ptr<PortConfiguration>> name_to_port_config;
std::unordered_map<uint16_t, std::shared_ptr<PortConfiguration>> number_to_port_config;
@@ -79,14 +79,19 @@ struct ServerState : public std::enable_shared_from_this<ServerState> {
uint16_t dns_server_port = 0;
std::vector<std::string> ip_stack_addresses;
std::vector<std::string> ppp_stack_addresses;
std::vector<std::string> ppp_raw_addresses;
std::vector<std::string> http_addresses;
uint64_t client_ping_interval_usecs = 30000000;
uint64_t client_idle_timeout_usecs = 60000000;
uint64_t patch_client_idle_timeout_usecs = 300000000;
bool ip_stack_debug = false;
bool allow_unregistered_users = false;
bool allow_pc_nte = false;
bool use_temp_licenses_for_prototypes = true;
bool allow_dc_pc_games = true;
bool allow_gc_xb_games = true;
bool enable_chat_commands = true;
std::unique_ptr<std::array<uint32_t, NUM_NON_PATCH_VERSIONS>> version_name_colors;
uint8_t allowed_drop_modes_v1_v2_normal = 0x1F;
uint8_t allowed_drop_modes_v1_v2_battle = 0x07;
uint8_t allowed_drop_modes_v1_v2_challenge = 0x07;
@@ -108,6 +113,7 @@ struct ServerState : public std::enable_shared_from_this<ServerState> {
QuestFlagsForDifficulty quest_flag_persist_mask;
uint64_t persistent_game_idle_timeout_usecs = 0;
bool ep3_send_function_call_enabled = false;
bool enable_v3_v4_protected_subcommands = false;
bool catch_handler_exceptions = true;
bool ep3_infinite_meseta = false;
std::vector<uint32_t> ep3_defeat_player_meseta_rewards = {400, 500, 600, 700, 800};
@@ -118,6 +124,7 @@ struct ServerState : public std::enable_shared_from_this<ServerState> {
bool hide_download_commands = true;
RunShellBehavior run_shell_behavior = RunShellBehavior::DEFAULT;
BehaviorSwitch cheat_mode_behavior = BehaviorSwitch::OFF_BY_DEFAULT;
bool use_game_creator_section_id = false;
bool default_rare_notifs_enabled_v1_v2 = false;
bool default_rare_notifs_enabled_v3_v4 = false;
std::vector<std::shared_ptr<const PSOBBEncryption::KeyFile>> bb_private_keys;
@@ -147,6 +154,7 @@ struct ServerState : public std::enable_shared_from_this<ServerState> {
std::array<std::shared_ptr<const WeaponRandomSet>, 4> weapon_random_sets;
std::shared_ptr<const TekkerAdjustmentSet> tekker_adjustment_set;
std::array<std::shared_ptr<const ItemParameterTable>, NUM_VERSIONS> item_parameter_tables;
std::array<std::shared_ptr<const ItemData::StackLimits>, NUM_VERSIONS> item_stack_limits_tables;
std::shared_ptr<const MagEvolutionTable> mag_evolution_table;
std::shared_ptr<const TextIndex> text_index;
std::array<std::shared_ptr<const ItemNameIndex>, NUM_VERSIONS> item_name_indexes;
@@ -176,6 +184,7 @@ struct ServerState : public std::enable_shared_from_this<ServerState> {
QuestF960Result quest_F960_failure_results;
std::vector<ItemData> secret_lottery_results;
uint16_t bb_global_exp_multiplier = 1;
double server_global_drop_rate_multiplier = 1.0;
std::shared_ptr<Episode3::TournamentIndex> ep3_tournament_index;
@@ -237,6 +246,8 @@ struct ServerState : public std::enable_shared_from_this<ServerState> {
std::shared_ptr<ProxyServer> proxy_server;
std::shared_ptr<Server> game_server;
std::shared_ptr<PatchServer> pc_patch_server;
std::shared_ptr<PatchServer> bb_patch_server;
explicit ServerState(const std::string& config_filename = "");
ServerState(std::shared_ptr<struct event_base> base, const std::string& config_filename, bool is_replay);
@@ -245,11 +256,6 @@ struct ServerState : public std::enable_shared_from_this<ServerState> {
ServerState& operator=(const ServerState&) = delete;
ServerState& operator=(ServerState&&) = delete;
void load_objects_and_downstream_dependents(const std::string& what);
void load_objects_and_downstream_dependents(const std::vector<std::string>& what);
void load_objects_and_upstream_dependents(const std::string& what);
void load_objects_and_upstream_dependents(const std::vector<std::string>& what);
void add_client_to_available_lobby(std::shared_ptr<Client> c);
void remove_client_from_lobby(std::shared_ptr<Client> c);
bool change_client_lobby(
@@ -283,14 +289,18 @@ struct ServerState : public std::enable_shared_from_this<ServerState> {
std::shared_ptr<const ItemParameterTable> item_parameter_table(Version version) const;
std::shared_ptr<const ItemParameterTable> item_parameter_table_for_encode(Version version) const;
void set_item_parameter_table(Version version, std::shared_ptr<const ItemParameterTable> table);
std::shared_ptr<const ItemNameIndex> item_name_index(Version version) const;
void set_item_name_index(Version version, std::shared_ptr<const ItemNameIndex> index);
std::shared_ptr<const ItemData::StackLimits> item_stack_limits(Version version) const;
std::shared_ptr<const ItemNameIndex> item_name_index_opt(Version version) const; // Returns null if missing
std::shared_ptr<const ItemNameIndex> item_name_index(Version version) const; // Throws if missing
std::string describe_item(Version version, const ItemData& item, bool include_color_codes) const;
ItemData parse_item_description(Version version, const std::string& description) const;
const std::vector<uint32_t> public_lobby_search_order(Version version) const;
inline uint32_t name_color_for_version(Version v) const {
return this->version_name_colors ? this->version_name_colors->at(static_cast<size_t>(v) - NUM_PATCH_VERSIONS) : 0;
}
std::shared_ptr<const std::vector<std::string>> information_contents_for_client(std::shared_ptr<const Client> c) const;
std::shared_ptr<const QuestIndex> quest_index(Version version) const;
@@ -308,29 +318,55 @@ struct ServerState : public std::enable_shared_from_this<ServerState> {
std::pair<std::string, uint16_t> parse_port_spec(const JSON& json) const;
std::vector<PortConfiguration> parse_port_configuration(const JSON& json) const;
void create_load_step_graph();
template <typename T>
inline void call_on_event_thread(std::function<T()>&& fn) {
return ::call_on_event_thread<T>(this->base, std::move(fn));
}
inline void forward_to_event_thread(std::function<void()>&& fn) {
::forward_to_event_thread(this->base, std::move(fn));
}
inline void forward_or_call(bool from_non_event_thread, std::function<void()>&& fn) {
if (from_non_event_thread) {
::forward_to_event_thread(this->base, std::move(fn));
} else {
fn();
}
}
std::shared_ptr<PatchServer::Config> generate_patch_server_config(bool is_bb) const;
void update_patch_server_configs() const;
// The following functions may only be called from a non-event thread if they
// take a from_non_event_thread argument; any function that does not have this
// argument must be called only from the event thread.
void create_default_lobbies();
void collect_network_addresses();
void load_config();
void load_bb_private_keys();
void load_licenses();
void load_teams();
void load_patch_indexes();
void load_config_early();
void load_config_late();
void load_bb_private_keys(bool from_non_event_thread);
void load_licenses(bool from_non_event_thread);
void load_teams(bool from_non_event_thread);
void load_patch_indexes(bool from_non_event_thread);
void clear_map_file_caches();
void load_battle_params();
void load_level_table();
void load_text_index();
static std::shared_ptr<ItemNameIndex> create_item_name_index_for_version(
Version version, std::shared_ptr<const ItemParameterTable> pmt, std::shared_ptr<const TextIndex> text_index);
void load_item_name_indexes();
void load_drop_tables();
void load_item_definitions();
void load_set_data_tables();
void load_word_select_table();
void load_ep3_data();
void load_quest_index();
void compile_functions();
void load_dol_files();
void load_battle_params(bool from_non_event_thread);
void load_level_table(bool from_non_event_thread);
void load_text_index(bool from_non_event_thread);
std::shared_ptr<ItemNameIndex> create_item_name_index_for_version(
std::shared_ptr<const ItemParameterTable> pmt,
std::shared_ptr<const ItemData::StackLimits> limits,
std::shared_ptr<const TextIndex> text_index) const;
void load_item_name_indexes(bool from_non_event_thread);
void load_drop_tables(bool from_non_event_thread);
void load_item_definitions(bool from_non_event_thread);
void load_set_data_tables(bool from_non_event_thread);
void load_word_select_table(bool from_non_event_thread);
void load_ep3_cards(bool from_non_event_thread);
void load_ep3_maps(bool from_non_event_thread);
void load_ep3_tournament_state(bool from_non_event_thread);
void load_quest_index(bool from_non_event_thread);
void compile_functions(bool from_non_event_thread);
void load_dol_files(bool from_non_event_thread);
void load_all();
void enqueue_destroy_lobbies();
static void dispatch_destroy_lobbies(evutil_socket_t, short, void* ctx);
-94
View File
@@ -1,94 +0,0 @@
#include "Shell.hh"
#include <event2/event.h>
#include <stdio.h>
#include <string.h>
#include <phosg/Strings.hh>
using namespace std;
const std::string Shell::PROMPT("newserv> ");
Shell::exit_shell::exit_shell() : runtime_error("shell exited") {}
Shell::Shell(std::shared_ptr<struct event_base> base)
: base(base),
read_event(
event_new(this->base.get(), 0, EV_READ | EV_PERSIST, &Shell::dispatch_read_stdin, this),
event_free),
prompt_event(
event_new(this->base.get(), 0, EV_TIMEOUT, &Shell::dispatch_print_prompt, this),
event_free) {
event_add(this->read_event.get(), nullptr);
// Schedule an event to print the prompt as soon as the event loop starts
// running. We do this so the prompt appears after any initialization
// messages that come after starting the shell
struct timeval tv = {0, 0};
event_add(this->prompt_event.get(), &tv);
this->poll.add(0, POLLIN);
}
void Shell::dispatch_print_prompt(evutil_socket_t, short, void* ctx) {
reinterpret_cast<Shell*>(ctx)->print_prompt();
}
void Shell::print_prompt() {
// Default behavior: no prompt
}
void Shell::dispatch_read_stdin(evutil_socket_t, short, void* ctx) {
reinterpret_cast<Shell*>(ctx)->read_stdin();
}
void Shell::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());
}
}
this->print_prompt();
}
-40
View File
@@ -1,40 +0,0 @@
#pragma once
#include <event2/event.h>
#include <memory>
#include <phosg/Filesystem.hh>
#include <stdexcept>
#include <string>
#include "ServerState.hh"
class Shell {
public:
Shell(std::shared_ptr<struct event_base> base);
virtual ~Shell() = default;
Shell(const Shell&) = delete;
Shell(Shell&&) = delete;
Shell& operator=(const Shell&) = delete;
Shell& operator=(Shell&&) = delete;
static const std::string PROMPT;
protected:
std::shared_ptr<struct event_base> base;
std::unique_ptr<struct event, void (*)(struct event*)> read_event;
std::unique_ptr<struct event, void (*)(struct event*)> prompt_event;
Poll poll;
class exit_shell : public std::runtime_error {
public:
exit_shell();
~exit_shell() = default;
};
static void dispatch_print_prompt(evutil_socket_t fd, short events, void* ctx);
static void dispatch_read_stdin(evutil_socket_t fd, short events, void* ctx);
virtual void print_prompt();
void read_stdin();
virtual void execute_command(const std::string& command) = 0;
};
+31 -23
View File
@@ -229,7 +229,7 @@ const char* abbreviation_for_section_id(uint8_t section_id) {
if (section_id < section_id_to_abbreviation.size()) {
return section_id_to_abbreviation[section_id];
} else {
return "<Unknown>";
return "Unknown";
}
}
@@ -237,7 +237,7 @@ const char* name_for_section_id(uint8_t section_id) {
if (section_id < section_id_to_name.size()) {
return section_id_to_name[section_id];
} else {
return "<Unknown>";
return "Unknown";
}
}
@@ -262,7 +262,7 @@ const string& name_for_event(uint8_t event) {
if (event < lobby_event_to_name.size()) {
return lobby_event_to_name[event];
} else {
static const string ret = "<Unknown lobby event>";
static const string ret = "Unknown lobby event";
return ret;
}
}
@@ -287,7 +287,7 @@ const string& name_for_lobby_type(uint8_t type) {
try {
return lobby_type_to_name.at(type);
} catch (const out_of_range&) {
static const string ret = "<Unknown lobby type>";
static const string ret = "Unknown lobby type";
return ret;
}
}
@@ -312,7 +312,7 @@ const string& name_for_npc(uint8_t npc) {
try {
return npc_id_to_name.at(npc);
} catch (const out_of_range&) {
static const string ret = "<Unknown NPC>";
static const string ret = "Unknown NPC";
return ret;
}
}
@@ -459,6 +459,18 @@ char abbreviation_for_difficulty(uint8_t difficulty) {
}
}
const char* name_for_language_code(uint8_t language_code) {
array<const char*, 8> names = {{"Japanese",
"English",
"German",
"French",
"Spanish",
"Simplified Chinese",
"Traditional Chinese",
"Korean"}};
return (language_code < 8) ? names[language_code] : "Unknown";
}
char char_for_language_code(uint8_t language_code) {
return (language_code < 8) ? "JEGFSBTK"[language_code] : '?';
}
@@ -494,23 +506,6 @@ uint8_t language_code_for_char(char language_char) {
}
}
size_t max_stack_size_for_item(Version version, uint8_t data0, uint8_t data1) {
if (data0 == 4) {
return 999999;
}
if (data0 == 3) {
if (version == Version::DC_V1_11_2000_PROTOTYPE) {
// All tool items are stackable up to x10 on this version
return 10;
} else if ((data1 < 9) && (data1 != 2)) {
return 10;
} else if (data1 == 0x10) {
return 99;
}
}
return 1;
}
const vector<string> tech_id_to_name = {
"foie", "gifoie", "rafoie",
"barta", "gibarta", "rabarta",
@@ -544,7 +539,7 @@ const string& name_for_technique(uint8_t tech) {
try {
return tech_id_to_name.at(tech);
} catch (const out_of_range&) {
static const string ret = "<Unknown technique>";
static const string ret = "Unknown technique";
return ret;
}
}
@@ -780,6 +775,19 @@ const char* name_for_floor(Episode episode, uint8_t floor) {
}
}
bool floor_is_boss_arena(Episode episode, uint8_t floor) {
switch (episode) {
case Episode::EP1:
return (floor >= 0x0B) && (floor <= 0x0E);
case Episode::EP2:
return (floor >= 0x0C) && (floor <= 0x0F);
case Episode::EP4:
return (floor == 0x09);
default:
return false;
}
}
uint32_t class_flags_for_class(uint8_t char_class) {
static constexpr uint8_t flags[12] = {
0x25, 0x2A, 0x31, 0x45, 0x51, 0x52, 0x86, 0x89, 0x8A, 0x32, 0x85, 0x46};
+2 -2
View File
@@ -33,8 +33,6 @@ enum class GameMode {
const char* name_for_mode(GameMode mode);
const char* abbreviation_for_mode(GameMode mode);
size_t max_stack_size_for_item(Version version, uint8_t data0, uint8_t data1);
extern const std::vector<std::string> tech_id_to_name;
extern const std::unordered_map<std::string, uint8_t> name_to_tech_id;
@@ -68,6 +66,7 @@ const char* name_for_difficulty(uint8_t difficulty);
const char* token_name_for_difficulty(uint8_t difficulty);
char abbreviation_for_difficulty(uint8_t difficulty);
const char* name_for_language_code(uint8_t language_code);
char char_for_language_code(uint8_t language_code);
uint8_t language_code_for_char(char language_char);
@@ -77,6 +76,7 @@ extern const std::unordered_map<std::string, uint8_t> mag_color_for_name;
size_t floor_limit_for_episode(Episode ep);
uint8_t floor_for_name(const std::string& name);
const char* name_for_floor(Episode episode, uint8_t floor);
bool floor_is_boss_arena(Episode episode, uint8_t floor);
uint32_t class_flags_for_class(uint8_t char_class);
-83
View File
@@ -1,83 +0,0 @@
#include "StepGraph.hh"
using namespace std;
void StepGraph::add_step(const string& name, const vector<string>& depends_on_names, function<void()>&& execute) {
auto new_step = make_shared<Step>();
new_step->execute = std::move(execute);
this->steps.emplace(name, new_step);
for (const auto& depends_on_name : depends_on_names) {
auto upstream_step = this->steps.at(depends_on_name);
upstream_step->downstream_dependencies.emplace_back(new_step);
new_step->upstream_dependencies.emplace_back(upstream_step);
}
}
void StepGraph::run(const string& start_step_name, bool run_upstreams) {
vector<string> start_step_names({start_step_name});
this->run(start_step_names, run_upstreams);
}
void StepGraph::run(const vector<string>& start_step_names, bool run_upstreams) {
// Collect all steps to run
deque<shared_ptr<Step>> steps_to_visit;
try {
for (const auto& start_step_name : start_step_names) {
steps_to_visit.emplace_back(this->steps.at(start_step_name));
}
} catch (const out_of_range&) {
throw runtime_error("invalid step name");
}
unordered_set<shared_ptr<Step>> steps_to_run;
while (!steps_to_visit.empty()) {
auto step = std::move(steps_to_visit.front());
steps_to_visit.pop_front();
if (steps_to_run.emplace(step).second) {
if (run_upstreams) {
for (const auto& w_other_step : step->upstream_dependencies) {
auto other_step = w_other_step.lock();
if (!other_step) {
throw runtime_error("upstream step is deleted");
}
steps_to_visit.emplace_back(other_step);
}
} else {
for (const auto& other_step : step->downstream_dependencies) {
steps_to_visit.emplace_back(other_step);
}
}
}
}
// Topological sort: repeatedly take all steps that are not a downstream
// dependency of any other step in the set
vector<shared_ptr<Step>> steps_order;
steps_order.reserve(steps_to_run.size());
while (!steps_to_run.empty()) {
unordered_set<shared_ptr<Step>> candidate_steps = steps_to_run;
for (const auto& step : steps_to_run) {
for (const auto& downstream_step : step->downstream_dependencies) {
candidate_steps.erase(downstream_step);
}
}
if (candidate_steps.empty()) {
throw logic_error("dependency graph contains a cycle");
}
for (const auto& step : candidate_steps) {
steps_to_run.erase(step);
steps_order.emplace_back(step);
}
}
// Run the steps in order
uint64_t run_id = ++this->last_run_id;
for (auto step : steps_order) {
if (step->last_run_id < run_id) {
step->last_run_id = run_id;
if (step->execute) {
step->execute();
}
}
}
}
-28
View File
@@ -1,28 +0,0 @@
#pragma once
#include <stdint.h>
#include <functional>
#include <memory>
#include <phosg/Strings.hh>
#include <string>
#include <unordered_map>
#include <vector>
struct StepGraph {
struct Step {
std::vector<std::shared_ptr<Step>> downstream_dependencies;
std::vector<std::weak_ptr<Step>> upstream_dependencies;
std::function<void()> execute;
uint64_t last_run_id = 0;
};
std::unordered_map<std::string, std::shared_ptr<Step>> steps;
uint64_t last_run_id = 0;
StepGraph() = default;
void add_step(const std::string& name, const std::vector<std::string>& depends_on_names, std::function<void()>&& execute);
void run(const std::string& start_step, bool run_upstreams);
void run(const std::vector<std::string>& start_steps, bool run_upstreams);
};

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