Compare commits
482 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c3aca29d9c | |||
| 94bbd5685e | |||
| 4a4f06e9ac | |||
| 34afd42391 | |||
| 6e80ccca54 | |||
| a485c25eb8 | |||
| fe5a15a1ab | |||
| 203a2aaeb4 | |||
| 78968f86dd | |||
| f1a64e6dbf | |||
| 215f5deff6 | |||
| 2b959386d7 | |||
| b3ab759717 | |||
| 1595555b53 | |||
| fdee74195b | |||
| bac429af94 | |||
| 8f0a33eb77 | |||
| c7009569b7 | |||
| 0250e3c9e5 | |||
| fdf7af20bc | |||
| 4ed641e6f4 | |||
| 9ff23b2aee | |||
| 34812d5037 | |||
| 79b0e82c50 | |||
| 43395492b2 | |||
| 97172717da | |||
| 078fd4ac08 | |||
| fc6a26ee38 | |||
| 32c08032c5 | |||
| 138d2609a2 | |||
| 37438c94c7 | |||
| ca551039ce | |||
| bfdb6c0695 | |||
| 1394dd681e | |||
| ba4a017ffb | |||
| d5773b93da | |||
| bebb69649c | |||
| 4946978ed7 | |||
| 1eba82c739 | |||
| ef11592439 | |||
| 6955b7ea0c | |||
| 95c1b4b6e8 | |||
| 3bb061951d | |||
| 649246cda2 | |||
| ca439c7a0f | |||
| e9899a33a2 | |||
| 6ced274108 | |||
| 6ffeda93a7 | |||
| c45246c1b5 | |||
| 8582e18861 | |||
| ed770a8b74 | |||
| 3cf309a008 | |||
| d1a830040f | |||
| 64d7ec5cde | |||
| 77f919980a | |||
| c5f4f2907e | |||
| 0ffa03d2b6 | |||
| 1fdbcd6c4e | |||
| ec453d1fa8 | |||
| a631fd50b4 | |||
| 8affe23c0d | |||
| 8cf11b3c48 | |||
| c39e60af8b | |||
| 194ed550e1 | |||
| ef0b72e95b | |||
| f3481fbd9f | |||
| 39d394cfae | |||
| 1b0f6cccf6 | |||
| 37c8491dc3 | |||
| e364ce2d9c | |||
| 15bbaa0837 | |||
| edf234c0ff | |||
| 4b63475662 | |||
| 4da71e127d | |||
| 9d688c2092 | |||
| d669f7ce6c | |||
| b02c82bb0d | |||
| a4f52b9b22 | |||
| 9b136d9444 | |||
| 7a5e759d9a | |||
| f923f51c22 | |||
| 133ca0b3cc | |||
| a937e50681 | |||
| b5b7345e5f | |||
| 61751d681e | |||
| 9ac01875fb | |||
| d076838747 | |||
| 8c5160e36f | |||
| e77228fa97 | |||
| 517a735ab2 | |||
| 353614e65c | |||
| d337517317 | |||
| 3c7b652f3a | |||
| cb11677214 | |||
| 1dbdd3f191 | |||
| 007e439281 | |||
| 350afbb436 | |||
| 08386c4019 | |||
| f57f903207 | |||
| 6727a25df0 | |||
| a57b6ce57b | |||
| b52700c08e | |||
| 68abac4fd4 | |||
| 52db9008a8 | |||
| eb2463a820 | |||
| dfad80eb9a | |||
| de7239e3fb | |||
| d6256183b5 | |||
| dbfb088630 | |||
| 3bb33a4de7 | |||
| 5a25c3e865 | |||
| 007359e220 | |||
| 5094db1306 | |||
| be5d85fa04 | |||
| 2ff3f8b4fb | |||
| 090379e520 | |||
| f3dfa0989f | |||
| c8b89a7cad | |||
| 1042b8df46 | |||
| afacf72034 | |||
| 53938cf6a6 | |||
| f2751a4e49 | |||
| 7c98f42722 | |||
| 5175c50945 | |||
| 13c438273b | |||
| 99c8d9957a | |||
| a28ef86c60 | |||
| aa19fd347e | |||
| e5a9b1f330 | |||
| 2eb4770bdd | |||
| a6ac56943c | |||
| d288fca087 | |||
| 889913400a | |||
| 5e2a42d852 | |||
| abd2fb9e92 | |||
| 5625999a90 | |||
| 08dfbbcb5c | |||
| 224e0df87e | |||
| 1bb0545b21 | |||
| 27cdf7e078 | |||
| c01d1f623c | |||
| 7612621fe9 | |||
| fa95a2f6d8 | |||
| 0b17b7174f | |||
| cf2f1ef529 | |||
| ae49ca0189 | |||
| 79374d3dd1 | |||
| 846401469e | |||
| 6f11410107 | |||
| 025556ecd3 | |||
| 5bcd16b6f2 | |||
| d52b882679 | |||
| 0d7f69eb66 | |||
| 391a70f68d | |||
| e858b2101d | |||
| ed2568fc7a | |||
| 9a2ed4c5ec | |||
| 398a93b56f | |||
| cceaf5efde | |||
| 14639c63e3 | |||
| 2ee7ca8600 | |||
| e800fd3fff | |||
| fb4aa0df22 | |||
| b0a32600be | |||
| 12caf95f5d | |||
| c3192bb398 | |||
| 8323c5e0af | |||
| bdff48c343 | |||
| 5f04cbaecb | |||
| 93f42a9398 | |||
| 2eacaa993e | |||
| 9bb168b693 | |||
| 9a1ba56982 | |||
| 8c2ea48b80 | |||
| d4115450b2 | |||
| fd8f968994 | |||
| 7634e61400 | |||
| 1a7981dff5 | |||
| c3c6f60664 | |||
| 421f27d63c | |||
| c314cb7cec | |||
| 9f4b53178a | |||
| 85fbd1b389 | |||
| 4f57ea30a1 | |||
| 1ea44ac55c | |||
| d44be66958 | |||
| 1a5d2537ad | |||
| f68308a242 | |||
| f622c9c91e | |||
| 0828029051 | |||
| 2e3089cb10 | |||
| f8da4ac7be | |||
| b82be91edd | |||
| 0870d66806 | |||
| 8efc9f1b3e | |||
| 95b4d34593 | |||
| 2819798791 | |||
| dc319e3a5d | |||
| 53efff5c4a | |||
| 37153fae79 | |||
| 81dcc14934 | |||
| 41a858935b | |||
| 9f2f0ccc14 | |||
| 8481ba23c5 | |||
| 3e8fa44be9 | |||
| 9dfaad9ae8 | |||
| ad3d9869ed | |||
| 85bdb1a7a2 | |||
| 759442ee62 | |||
| 51c13b8462 | |||
| 17496ab9fe | |||
| 690d4bdb14 | |||
| fd0ba1bbf6 | |||
| e321fd5bca | |||
| 7be3aad58b | |||
| b4410594b0 | |||
| 94e6ba2a91 | |||
| 5998fee2a7 | |||
| cb1e405a66 | |||
| dab83f27d3 | |||
| 92c51830bb | |||
| 846100cf16 | |||
| b0edffdef1 | |||
| debca5aad4 | |||
| 1421e633be | |||
| a0a802f42f | |||
| 247904f019 | |||
| 10ab632c59 | |||
| 51ccecf1bd | |||
| dedea228b1 | |||
| 3e74bde880 | |||
| 0a1eb5f0d7 | |||
| ed81599cc9 | |||
| 2ce9e58177 | |||
| 52625aed9c | |||
| 9140b04ca6 | |||
| c9e5d1f677 | |||
| 0e53ea08ba | |||
| 378fd0521e | |||
| 4edcbc5d4d | |||
| 14837447a3 | |||
| 23a0424acf | |||
| 4f63d3672e | |||
| 0033cb2eda | |||
| 239ffd1323 | |||
| f18953c31e | |||
| f088454c25 | |||
| 48905bfa10 | |||
| 830a151db7 | |||
| 5b3a94f018 | |||
| 1cb14b48c9 | |||
| 3d036404f7 | |||
| 4eb46b293e | |||
| 026befe6ac | |||
| d9413b3559 | |||
| e14a4f83db | |||
| ba928306ba | |||
| d5154f0a5d | |||
| 7ffa043941 | |||
| 513fab03c8 | |||
| e2525ffd36 | |||
| 9e161d99cf | |||
| 6a0df79fad | |||
| ce87348bf5 | |||
| 50e1b79b1e | |||
| a16c207f4d | |||
| f6987d6627 | |||
| 1c6ba33be3 | |||
| 63958b7c5d | |||
| 7ed0866c2b | |||
| 333fc803ce | |||
| 32176caff8 | |||
| 73278fe9ab | |||
| 5ec90db9eb | |||
| 6afc029152 | |||
| 44e28fd906 | |||
| 0969a6eb1d | |||
| 8d206133a3 | |||
| e4b5fbf2ba | |||
| 867f86da5e | |||
| e273629cd1 | |||
| bc071155b0 | |||
| 295bb9c4a4 | |||
| c9d62e26ef | |||
| c8cb3e61f7 | |||
| 0b85f46ce2 | |||
| a01472666c | |||
| 613789057f | |||
| dbf44e60ff | |||
| a8f888b829 | |||
| 125c8f910c | |||
| 27bccc5571 | |||
| 1a477b28a0 | |||
| fbc0eaeaa1 | |||
| 96caa94d1e | |||
| 95220bfbdc | |||
| cd01848eb9 | |||
| 34cde304dc | |||
| 0951132c01 | |||
| 7d950e01ab | |||
| e73fb2fbba | |||
| 8f2bf60d62 | |||
| bdc60ac601 | |||
| 6e6b161847 | |||
| d4d7797741 | |||
| 110db06191 | |||
| a9cf98a24f | |||
| 9524d05279 | |||
| c43fa7a40d | |||
| 5d314f4e96 | |||
| 29fc74470d | |||
| 7bcb040e8d | |||
| afba535e00 | |||
| 152a90a37b | |||
| e998cb4a92 | |||
| 79dde31d7f | |||
| a1c86189e4 | |||
| 8afc952294 | |||
| 30426acbbe | |||
| 38117390f7 | |||
| fb08c45cb7 | |||
| 58aef33edc | |||
| e7a821bcba | |||
| dcf89865f5 | |||
| 5b93ac046f | |||
| 481b9b3040 | |||
| 34d32418e3 | |||
| b9902f6189 | |||
| 1b949c67da | |||
| 1a3dd26cb3 | |||
| 42d12e2a18 | |||
| 19b093cfc5 | |||
| 425338877d | |||
| 764a930213 | |||
| f166dae1c6 | |||
| 176e0fb6d6 | |||
| 60bb758bc4 | |||
| 3e5a961b68 | |||
| 0180296c49 | |||
| 8937333a2b | |||
| 861d4e432a | |||
| c46db6eccd | |||
| 9a35f5ca63 | |||
| 89285fef98 | |||
| b6d6474356 | |||
| 010f753a08 | |||
| 3a7c3c0fe9 | |||
| 01e4518c8e | |||
| 47c2269fca | |||
| 507af79203 | |||
| 74fdf3cdeb | |||
| e0b0ff989a | |||
| e8f79628ca | |||
| 6eb77a7193 | |||
| 1ed06283a2 | |||
| 0908ba5599 | |||
| 8ef18eab13 | |||
| d2bcc5d261 | |||
| f59347c5c2 | |||
| bca76322bf | |||
| dc278a7843 | |||
| 34b70a8a03 | |||
| 7380b34d9d | |||
| 5d3d1e1900 | |||
| 4abd91cb8f | |||
| 56494f7e9d | |||
| 5ede882715 | |||
| b612d50c17 | |||
| eaf1ad036c | |||
| c5375c11aa | |||
| 77cea58fc5 | |||
| e808a7b6a3 | |||
| 4066f80407 | |||
| 7cce105a09 | |||
| 202427e331 | |||
| 2a7fdceba9 | |||
| 6af3a8e8cd | |||
| 1fdf258e7f | |||
| 3e14bc306f | |||
| c592542f07 | |||
| d2b9023cfc | |||
| 716825ffaf | |||
| 907883d176 | |||
| a0d994962f | |||
| a6442c6208 | |||
| 8bff95052c | |||
| 8c82fccb5b | |||
| c1ea579758 | |||
| 25c68ef43c | |||
| d006359f87 | |||
| 39c7b37a84 | |||
| 50643df49e | |||
| c62f1e9fa0 | |||
| 69d2c6d95c | |||
| b4780a80a8 | |||
| e958753a09 | |||
| aa8cf5fd1b | |||
| 7a1eb677dc | |||
| 9b837c5b6c | |||
| 176641aebe | |||
| 80bac6c89e | |||
| c43c023b4b | |||
| 293cc86092 | |||
| 09f0d1f3de | |||
| 8f75823f7d | |||
| 42c1d251eb | |||
| ed36471a4e | |||
| 1164f99957 | |||
| 8c7cee8fd5 | |||
| 442f33733d | |||
| 286997188e | |||
| f4517ab92e | |||
| 7c28ee05cc | |||
| 8ac15c9aa3 | |||
| 431cd480e8 | |||
| 2cb49030f0 | |||
| cf59858e1e | |||
| b901e8846d | |||
| 174c53d751 | |||
| 89cb07a376 | |||
| 4b666a079b | |||
| 0ebf2ba8ef | |||
| 2fe51519d4 | |||
| 7f7137ed81 | |||
| ecf02943d4 | |||
| ca2c17360d | |||
| ea62275f89 | |||
| 415aa88bd3 | |||
| e6b05196fd | |||
| 02e98008d3 | |||
| ce2cf1b56b | |||
| 22c36b4874 | |||
| ec205062ad | |||
| d61c65cf16 | |||
| 7461d36cb8 | |||
| 7983f71159 | |||
| 4c20097de7 | |||
| df80933f40 | |||
| 52a853092c | |||
| 515a0b70be | |||
| a2e53b2b33 | |||
| 8d16ff7e7c | |||
| 8f64c2f3ba | |||
| 309a347312 | |||
| 0d57bee368 | |||
| c3ccd74e80 | |||
| e55cf3bc7c | |||
| 4163f2affa | |||
| 5f836711c7 | |||
| 37b7119ea5 | |||
| f43a7c9277 | |||
| 2590a2f24b | |||
| ff7031544a | |||
| 34ab156451 | |||
| 0a115427a1 | |||
| abe64af17b | |||
| 39942dc4bd | |||
| 41d03670d6 | |||
| b031f2e8ad | |||
| 5dba862117 | |||
| 9cf306b73c | |||
| a8fb7a2eda | |||
| 0327e242fc | |||
| 10cb76aefd | |||
| 949a651be1 | |||
| e5227080b8 | |||
| 58f0501010 | |||
| b0319d34a0 | |||
| 070db173dd | |||
| 2394a330ff | |||
| d5308449e3 | |||
| f823c2b907 | |||
| a7e3d4853a | |||
| 38b0539124 | |||
| d543db187f | |||
| 3b1f4f4324 | |||
| 2bb3118c1a | |||
| 7064821ac5 | |||
| 9f81041dc7 | |||
| a81df27bc9 | |||
| 38ca60bcbe | |||
| 6b958c9f25 |
@@ -54,4 +54,4 @@ jobs:
|
||||
|
||||
- name: Test
|
||||
working-directory: ${{github.workspace}}/build
|
||||
run: ctest -C ${{env.BUILD_TYPE}}
|
||||
run: ctest -C ${{env.BUILD_TYPE}} --output-on-failure
|
||||
|
||||
+21
-3
@@ -5,17 +5,35 @@
|
||||
newserv
|
||||
|
||||
# CMake files
|
||||
cmake_install.cmake
|
||||
CMakeCache.txt
|
||||
CMakeFiles
|
||||
Makefile
|
||||
CTestTestFile.cmake
|
||||
Testing
|
||||
cmake_install.cmake
|
||||
install_manifest.txt
|
||||
Makefile
|
||||
Testing
|
||||
|
||||
# Files modified by the user and/or server that don't have defaults
|
||||
system/config.json
|
||||
system/ep3/tournament-state.json
|
||||
system/ep3/maps-free/*.bind
|
||||
system/ep3/maps-quest/*.bind
|
||||
system/licenses.nsi
|
||||
system/players/player_*
|
||||
system/players/account_*
|
||||
system/players/bank_*
|
||||
system/patch-pc/.metadata-cache.json
|
||||
system/patch-bb/.metadata-cache.json
|
||||
|
||||
# Files fuzziqersoftware uses that don't make sense to be committed to the main
|
||||
# repository
|
||||
files
|
||||
make_release.py
|
||||
notes
|
||||
old-khyller
|
||||
old-newserv
|
||||
release
|
||||
release.zip
|
||||
system/patch-bb/data
|
||||
system/patch-bb/psobb.pat
|
||||
system/dol
|
||||
|
||||
+49
-18
@@ -14,10 +14,11 @@ else()
|
||||
add_compile_options(-Wall -Wextra -Werror -Wno-address-of-packed-member)
|
||||
endif()
|
||||
|
||||
include_directories("/usr/local/include")
|
||||
link_directories("/usr/local/lib")
|
||||
|
||||
set(CMAKE_BUILD_TYPE Debug)
|
||||
set(LOCAL_INCLUDE_DIR "/usr/local/include")
|
||||
set(LOCAL_LIB_DIR "/usr/local/lib")
|
||||
list(APPEND CMAKE_PREFIX_PATH ${LOCAL_LIB_DIR})
|
||||
include_directories(${LOCAL_INCLUDE_DIR})
|
||||
link_directories(${LOCAL_LIB_DIR})
|
||||
|
||||
|
||||
|
||||
@@ -31,47 +32,58 @@ set (LIBEVENT_LIBRARIES
|
||||
${LIBEVENT_LIBRARY}
|
||||
${LIBEVENT_CORE})
|
||||
|
||||
find_path (RESOURCE_FILE_INCLUDE_DIR NAMES resource_file/ResourceFile.hh)
|
||||
find_library (RESOURCE_FILE_LIBRARY NAMES resource_file)
|
||||
|
||||
if(RESOURCE_FILE_INCLUDE_DIR AND RESOURCE_FILE_LIBRARY)
|
||||
set(RESOURCE_FILE_FOUND 1)
|
||||
else()
|
||||
set(RESOURCE_FILE_FOUND 0)
|
||||
endif()
|
||||
find_package(phosg REQUIRED)
|
||||
find_package(resource_file QUIET)
|
||||
|
||||
|
||||
|
||||
# Executable definition
|
||||
|
||||
add_executable(newserv
|
||||
src/CatSession.cc
|
||||
src/Channel.cc
|
||||
src/ChatCommands.cc
|
||||
src/Client.cc
|
||||
src/Compression.cc
|
||||
src/DNSServer.cc
|
||||
src/Episode3.cc
|
||||
src/Episode3/AssistServer.cc
|
||||
src/Episode3/BattleRecord.cc
|
||||
src/Episode3/Card.cc
|
||||
src/Episode3/CardSpecial.cc
|
||||
src/Episode3/DataIndex.cc
|
||||
src/Episode3/DeckState.cc
|
||||
src/Episode3/MapState.cc
|
||||
src/Episode3/PlayerState.cc
|
||||
src/Episode3/PlayerStateSubordinates.cc
|
||||
src/Episode3/RulerServer.cc
|
||||
src/Episode3/Server.cc
|
||||
src/Episode3/Tournament.cc
|
||||
src/FileContentsCache.cc
|
||||
src/FunctionCompiler.cc
|
||||
src/GSLArchive.cc
|
||||
src/IPFrameInfo.cc
|
||||
src/IPStackSimulator.cc
|
||||
src/Items.cc
|
||||
src/LevelTable.cc
|
||||
src/License.cc
|
||||
src/Lobby.cc
|
||||
src/Loggers.cc
|
||||
src/Main.cc
|
||||
src/Map.cc
|
||||
src/Menu.cc
|
||||
src/NetworkAddresses.cc
|
||||
src/PatchFileIndex.cc
|
||||
src/Player.cc
|
||||
src/ProxyCommands.cc
|
||||
src/ProxyServer.cc
|
||||
src/PSOEncryption.cc
|
||||
src/PSOGCObjectGraph.cc
|
||||
src/PSOProtocol.cc
|
||||
src/Quest.cc
|
||||
src/RareItemSet.cc
|
||||
src/ReceiveCommands.cc
|
||||
src/ReceiveSubcommands.cc
|
||||
src/ReplaySession.cc
|
||||
src/SendCommands.cc
|
||||
src/Server.cc
|
||||
src/ServerShell.cc
|
||||
@@ -82,19 +94,38 @@ add_executable(newserv
|
||||
src/Version.cc
|
||||
)
|
||||
target_include_directories(newserv PUBLIC ${LIBEVENT_INCLUDE_DIR})
|
||||
target_link_libraries(newserv phosg ${LIBEVENT_LIBRARIES})
|
||||
target_link_libraries(newserv phosg ${LIBEVENT_LIBRARIES} pthread)
|
||||
|
||||
if(RESOURCE_FILE_FOUND)
|
||||
if(resource_file_FOUND)
|
||||
target_compile_definitions(newserv PUBLIC HAVE_RESOURCE_FILE)
|
||||
target_include_directories(newserv PUBLIC ${RESOURCE_FILE_INCLUDE_DIR})
|
||||
target_link_libraries(newserv ${RESOURCE_FILE_LIBRARY})
|
||||
target_link_libraries(newserv resource_file)
|
||||
message(STATUS "libresource_file found; enabling patch support")
|
||||
else()
|
||||
message(WARNING "libresource_file not available; disabling patch support")
|
||||
message(WARNING "libresource_file not found; disabling patch support")
|
||||
endif()
|
||||
|
||||
|
||||
|
||||
# Test configuration
|
||||
|
||||
enable_testing()
|
||||
|
||||
file(GLOB TestCases ${CMAKE_SOURCE_DIR}/tests/*.test.txt)
|
||||
|
||||
foreach(TestCase IN ITEMS ${TestCases})
|
||||
add_test(
|
||||
NAME ${TestCase}
|
||||
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
|
||||
COMMAND ${CMAKE_BINARY_DIR}/newserv replay-log ${TestCase} --config=${CMAKE_SOURCE_DIR}/tests/config.json --require-password=password --require-access-key=111111111111)
|
||||
endforeach()
|
||||
|
||||
add_test(
|
||||
NAME compression
|
||||
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
|
||||
COMMAND ${CMAKE_SOURCE_DIR}/test-compression.sh ${CMAKE_BINARY_DIR}/newserv)
|
||||
|
||||
|
||||
|
||||
# Installation configuration
|
||||
|
||||
install(TARGETS newserv DESTINATION bin)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2018 Martin Michelsen
|
||||
Copyright (c) 2023 Martin Michelsen
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
|
||||
@@ -1,74 +1,214 @@
|
||||
# newserv
|
||||
# newserv <img align="right" src="s-newserv.png" />
|
||||
|
||||
newserv is a game server and proxy for Phantasy Star Online (PSO).
|
||||
|
||||
This project includes code that was reverse-engineered by the community in ages long past, and has been included in many projects since then. It also includes some game data from Phantasy Star Online itself; this data was originally created by Sega.
|
||||
This project includes code that was reverse-engineered by the community in ages long past, and has been included in many projects since then. It also includes some game data from Phantasy Star Online itself, which was originally created by Sega.
|
||||
|
||||
This project is a rewrite of a rewrite of a game server that I wrote many years ago. So far, it works well with PSO GC Episodes 1 & 2, and lobbies (but not games) are implemented on Episode 3. Some basic functionality works on PSO PC and PSO BB, but there are probably still some cases that lead to errors (which will disconnect the client). The proxy works well with PSO GC and PSO BB.
|
||||
## History
|
||||
|
||||
Feel free to submit GitHub issues if you find bugs or have feature requests. I'd like to make the server as stable and complete as possible, but I can't promise that I'll respond to issues in a timely manner.
|
||||
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 "Usage" 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="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="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="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="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).
|
||||
|
||||
## Future
|
||||
|
||||
This project is primarily for my own nostalgia; I offer no guarantees on how or when this project will advance.
|
||||
newserv is many things - a server, a proxy, an encryption and decryption tool, a decoder of various PSO-related formats, and more. Primarily, it's a reverse-engineering project in which I try to unravel the secrets of a 20-year-old video game, for honestly no reason. Solving these problems and documenting them in code has been fun, and I'll continue to do it when my time allows.
|
||||
|
||||
Current known issues / missing features:
|
||||
- Test all the communication features (info board, simple mail, card search, etc.)
|
||||
- The trade window isn't implemented yet.
|
||||
- PSO PC and PSOBB are not well-tested and likely will disconnect when clients try to use unimplemented features. Only GC is known to be stable and mostly complete.
|
||||
- Patches currently are platform-specific but not version-specific. This makes them quite a bit harder to use properly.
|
||||
With that said, I offer no guarantees on how or when this project will advance. Feel free to submit GitHub issues if you find bugs or have feature requests; I'd like to make the server as stable and complete as possible, but I can't promise that I'll respond to issues in a timely manner. If you feel like contributing to newserv yourself, pull requests are welcome as well.
|
||||
|
||||
Current known issues / missing features / things to do:
|
||||
- Episode 3 battles are implemented but are not well-tested.
|
||||
- Fix behavior when joining a spectator team after the beginning of a battle.
|
||||
- PSOBB is not well-tested and likely will disconnect or misbehave when clients try to use unimplemented features.
|
||||
- Enemy indexes also desync slightly in most games, often in later areas, leading to incorrect EXP values being given for killed enemies.
|
||||
- Fix some edge cases on the BB proxy server (e.g. make sure Change Ship does the right thing, which is not the same as what it should do on V2/V3).
|
||||
- PSOX is not tested at all.
|
||||
- Memory patches currently are platform-specific but not version-specific. This makes them quite a bit harder to write and use properly.
|
||||
- Find a way to silence audio in RunDOL.s. Some old DOLs don't reset audio systems at load time and it's annoying to hear the crash buzz when the GC hasn't actually crashed.
|
||||
- Implement private lobbies, and add a way to make games persistent.
|
||||
- Implement private and overflow lobbies.
|
||||
- Enforce client-side size limits (e.g. for 60/62 commands) on the server side as well. (For 60/62 specifically, perhaps transform them to 6C/6D if needed.)
|
||||
- Encapsulate BB server-side random state and make replays deterministic.
|
||||
- VMS decoding doesn't work. Complete this reverse-engineering project.
|
||||
- Code style
|
||||
- The internal menu abstraction is ugly and hard to work with. Rewrite it.
|
||||
- Add default values for all commands (like we use for Episode 3 battle commands).
|
||||
- Clean up the way proxy session options are passed to the session from the client object (and add user-settable options for e.g. chat filter, which currently doesn't appear in the menu).
|
||||
- Episode 3 bugs
|
||||
- Disconnecting during a match turns you into a COM if there are other humans in the match, even if the match is part of a tournament. This may be incorrect behavior for tournaments.
|
||||
- Disconnecting during a tournament when there are no other humans in the match simply cancels the match (so it can be replayed) instead of forfeiting, which is almost certainly incorrect behavior. (Then again, no one likes losing tournaments to COMs...)
|
||||
- Tournament deck restrictions aren't enforced when populating COMs at tournament start time. This can cause weird behavior if, for example, a COM deck contains assist cards and the tournament rules forbid them.
|
||||
- There is a rare failure mode during battles that causes one of the clients to be disconnected.
|
||||
|
||||
## Compatibility
|
||||
|
||||
newserv supports several versions of PSO. Specifically:
|
||||
| Version | Basic commands | Lobbies | Games | Proxy |
|
||||
|----------------------|----------------|---------------|---------------|---------------|
|
||||
| Dreamcast Trial | Partial (6) | Not supported | Not supported | Not supported |
|
||||
| Dreamcast V1 | Supported (1) | Supported | Supported | Supported |
|
||||
| Dreamcast V2 | Supported (1) | Supported | Supported | Supported |
|
||||
| PC | Supported | Supported | Supported | Supported |
|
||||
| GameCube Ep1&2 Trial | Untested (2) | Untested (2) | Untested (2) | Untested (2) |
|
||||
| GameCube Ep1&2 | Supported | Supported | Supported | Supported |
|
||||
| GameCube Ep1&2 Plus | Supported | Supported | Supported | Supported |
|
||||
| GameCube Ep3 Trial | Supported | Supported | Supported (3) | Supported |
|
||||
| GameCube Ep3 | Supported | Supported | Supported (3) | Supported |
|
||||
| XBOX Ep1&2 | Untested (4) | Untested (4) | Untested (4) | Untested (4) |
|
||||
| Blue Burst | Supported | Supported | Partial (5) | Supported |
|
||||
|
||||
*Notes:*
|
||||
1. *DC support has only been tested with the US versions of PSO DC. Other versions probably don't work, but will be easy to add. Please submit a GitHub issue if you have a non-US DC version, and can provide a log from a connection attempt.*
|
||||
2. *This version only supports the modem adapter, which Dolphin does not currently emulate, so it's difficult to test.*
|
||||
3. *See the following section about Episode 3 functionality.*
|
||||
4. *newserv's implementation of PSOX is based on disassembly of the client executable; it has never been tested with a real client and most likely doesn't work.*
|
||||
5. *Some basic features are not implemented in Blue Burst games, so the games are not very playable. A lot of work has to be done to get BB games to a playable state.*
|
||||
6. *Support for PSO Dreamcast Trial Edition is very incomplete and probably never will be complete. This is really just exploring a curiosity that sheds some light on early network engineering done by Sega, not an actual attempt at supporting this version of the game.*
|
||||
|
||||
### Episode 3
|
||||
|
||||
The following Episode 3 features are well-tested and work normally:
|
||||
* Downloading quests.
|
||||
* Creating and joining games.
|
||||
* Trading cards.
|
||||
* Participating in card auctions. (The auction contents must be configured in config.json.)
|
||||
* Tournaments. (See below)
|
||||
|
||||
The following Episode 3 features are implemented, but only partially tested:
|
||||
* CARD battles. If you find a feature or card ability that doesn't work, please make a GitHub issue and describe the situation (including the attacking card(s), defending card(s), and ability that didn't work).
|
||||
* Spectator teams are partially implemented, but are not well-tested. There is a known issue that prevents viewing battles unless you're in the spectator team when the battle begins.
|
||||
* Battle replays sometimes cause the client to crash during the replay. Using the $playrec command is therefore not recommended.
|
||||
|
||||
Tournaments work differently than they did on Sega's servers. Tournaments can be created with the `create-tournament` shell command, which enables players to register for them. (Use `help` to see all the arguments - there are many!) The `start-tournament` shell command starts the tournament, but this doesn't schedule any matches. Instead, players who are ready to play their next match can all stand at the rightmost 4-player battle table in the same CARD lobby, and the tournament match will start automatically. (This also means that, for example, not all matches in round 1 must be complete before round 2 can begin - only the matches preceding each individual match must be complete for that match to be playable.)
|
||||
|
||||
Because newserv gives all players 1000000 meseta, there is no reward for winning a tournament. This may change in the future.
|
||||
|
||||
Episode 3 state and game data is stored in the system/ep3 directory. The files in there are:
|
||||
* card-definitions.mnr: Compressed card definition list, sent to Episode 3 clients at connect time. Card stats and abilities can be changed by editing this file.
|
||||
* card-definitions.mnrd: Decompressed version of the above. If present, newserv will use this instead of the compressed version, since this is easier to edit.
|
||||
* card-text.mnr: Compressed card text archive. Generally only used for debugging.
|
||||
* com-decks.json: COM decks used in tournaments. The default decks in this file come from logs from Sega's servers, so the file doesn't include every COM deck Sega ever made - the rest are probably lost to time.
|
||||
* maps-free/ and maps-quest/: Online free battle and quest maps (.mnm/.bin/.mnmd/.bind files). Free battle and quest files have exactly the same format; the only difference between the files in these directories is which section of the menu they will appear in on the client.
|
||||
* tournament-state.json: State of all active tournaments. This file is automatically written when any tournament changes state for any reason (e.g. a tournament is created/started/deleted or a match is resolved).
|
||||
|
||||
## Usage
|
||||
|
||||
Currently this code should build on macOS and Ubuntu. It will likely work on other Linux flavors too. It should work on Windows as well, but I haven't tested it - the build process could be very manual.
|
||||
Currently newserv should build on macOS and Ubuntu. It will likely work on other Linux flavors too. It should work on Windows as well, but I haven't tested it recently - the build process could be very manual. Cygwin is likely the easiest Windows environment in which to build newserv.
|
||||
|
||||
There is a probably-not-too-old macOS release on the newserv GitHub repository (look in the right sidebar).
|
||||
There is a fairly recent macOS ARM64 release on the newserv GitHub repository. You may need to install libevent manually even if you use this release (run `brew install libevent`).
|
||||
|
||||
If you're running Linux or want to build newserv yourself, here's what you do:
|
||||
If you're using an older AMD64 Mac, you're running Linux, or you just want to build newserv yourself, here's what you do:
|
||||
1. Make sure you have CMake and libevent installed. (`brew install cmake libevent` on macOS, `sudo apt-get install cmake libevent-dev` on most Linuxes)
|
||||
2. Build and install phosg (https://github.com/fuzziqersoftware/phosg).
|
||||
3. Optionally, install resource_dasm (https://github.com/fuzziqersoftware/resource_dasm). This will enable newserv to load DOL files on PSO GC clients. PSO GC clients can play PSO normally on newserv without this.
|
||||
4. Run `cmake . && make` on the newserv directory.
|
||||
3. Optionally, install resource_dasm (https://github.com/fuzziqersoftware/resource_dasm). This will enable newserv to send memory patches and load DOL files on PSO GC clients. PSO GC clients can play PSO normally on newserv without this.
|
||||
4. Run `cmake . && make` in the newserv directory.
|
||||
|
||||
After building newserv or downloading a release, do this to set it up and use it:
|
||||
1. In the system/ directory, make a copy of config.example.json named config.json, and edit it appropriately.
|
||||
2. Run `./newserv` in the newserv directory. This will start the game server and run the interactive shell. You may need `sudo` if newserv's built-in DNS server is enabled.
|
||||
3. Use the interactive shell to add a license. Run `help` in the shell to see how to do this.
|
||||
2. If you plan to play PSO Blue Burst on newserv, set up the patch directory appropriately. See the "Client patch directories" section below.
|
||||
3. Run `./newserv` in the newserv directory. This will start the game server and run the interactive shell. You may need `sudo` if newserv's built-in DNS server is enabled.
|
||||
4. Use the interactive shell to add a license. Run `help` in the shell to see how to do this.
|
||||
5. Set your client's network settings appropriately and start an online game. See the "Connecting local clients" or "Connecting remote clients" section to see how to get your game client to connect.
|
||||
|
||||
To use newserv in other ways (e.g. for translating data), see the end of this document.
|
||||
|
||||
### Installing quests
|
||||
|
||||
newserv automatically finds quests in the system/quests/ directory. To install your own quests, or to use quests you've saved using the proxy's set-save-files option, just put them in that directory and name them appropriately.
|
||||
|
||||
Standard quest file names should be like `q###-CATEGORY-VERSION.EXT`; battle quests should be named like `b###-VERSION.EXT`, and challenge quests should be named like `c###-VERSION.EXT`. The fields in each filename are:
|
||||
- `###`: quest number (this doesn't really matter; it should just be unique for the version)
|
||||
Standard quest files should be named like `q###-CATEGORY-VERSION.EXT`, battle quests should be named like `b###-VERSION.EXT`, challenge quests should be named like `c###-VERSION.EXT`, and Episode 3 download quests should be named like `e###-gc3.EXT`. The fields in each filename are:
|
||||
- `###`: quest number (this doesn't really matter; it should just be unique for the PSO version)
|
||||
- `CATEGORY`: ret = Retrieval, ext = Extermination, evt = Events, shp = Shops, vr = VR, twr = Tower, gov = Government (BB only), dl = Download (these don't appear during online play), 1p = Solo (BB only)
|
||||
- `VERSION`: d1 = Dreamcast v1, dc = Dreamcast v2, pc = PC, gc = GameCube Episodes 1 & 2, gc3 = Episode 3, bb = Blue Burst
|
||||
- `EXT`: file extension (bin, dat, bin.gci, dat.gci, bin.dlq, dat.dlq, or qst)
|
||||
- `EXT`: file extension (see table below)
|
||||
|
||||
There are multiple PSO quest formats out there; newserv supports most of them. Specifically, newserv can use quests in any of the following formats:
|
||||
- bin/dat format: These quests consist of two files with the same base name, a .bin file and a .dat file.
|
||||
- Unencrypted GCI format: These quests also consist of a .bin and .dat file, but an encoding is applied on top of them. The filenames should end in .bin.gci and .dat.gci. (Note that there also exists an encrypted GCI format, which newserv does not support.)
|
||||
- Encrypted DLQ format: These quests also consist of a .bin and .dat file, but download quest encryption is applied on top of them. The filenames should end in .bin.dlq and .dat.dlq.
|
||||
- QST format: These quests consist of only a .qst file, which contains both the .bin and .dat files within it.
|
||||
For example, the GameCube version of Lost HEAT SWORD is in two files named `q058-ret-gc.bin` and `q058-ret-gc.dat`. newserv knows these files are quests because they're in the system/quests/ directory, it knows they're for PSO GC because the filenames contain `-gc`, and it puts them in the Retrieval category because the filenames contain `-ret`.
|
||||
|
||||
There are multiple PSO quest formats out there; newserv supports most of them. It can also decode any known format to standard .bin/.dat format. Specifically:
|
||||
|
||||
| Format | Extension | Supported | Decode action |
|
||||
|---------------------------|-----------------------|---------------|------------------|
|
||||
| Compressed | .bin and .dat | Yes | None (1) |
|
||||
| Compressed Ep3 | .bin or .mnm | Yes (4) | None (1) |
|
||||
| Uncompressed | .bind and .datd | Yes | compress-prs (2) |
|
||||
| Uncompressed Ep3 | .bind or .mnmd | Yes (4) | compress-prs (2) |
|
||||
| Unencrypted GCI | .bin.gci and .dat.gci | Yes | decode-gci |
|
||||
| Encrypted GCI with key | .bin.gci and .dat.gci | Yes | decode-gci |
|
||||
| Encrypted GCI without key | .bin.gci and .dat.gci | No | decode-gci (3) |
|
||||
| Ep3 GCI | .bin.gci or .mnm.gci | Download only | decode-gci |
|
||||
| Encrypted DLQ | .bin.dlq and .dat.dlq | Yes | decode-dlq |
|
||||
| Ep3 DLQ | .bin.dlq or .mnm.dlq | Download only | decode-dlq |
|
||||
| Online QST | .qst | Yes | decode-qst |
|
||||
| Download QST | .qst | Yes | decode-qst |
|
||||
|
||||
*Notes:*
|
||||
1. *This is the default format. You can convert these to uncompressed format by running `newserv decompress-prs FILENAME.bin FILENAME.bind` (and similarly for .dat -> .datd)*
|
||||
2. *Similar to (1), to compress an uncompressed quest file: `newserv compress-prs FILENAME.bind FILENAME.bin` (and likewise for .datd -> .dat)*
|
||||
3. *If you know the encryption seed (serial number), pass it in as a hex string with the `--seed=` option. If you don't know the encryption seed, newserv will find it for you, which will likely take a long time.*
|
||||
4. *Episode 3 online quests don't go in the system/quests directory; they instead go in the system/ep3/maps-free or system/ep3/maps-quest directory. If you want an Episode 3 quest to be available for both online play and for downloading, the file must exist in both system/quests and in one of the map directories in system/ep3.*
|
||||
|
||||
Episode 3 download quests consist only of a .bin file - there is no corresponding .dat file. Episode 3 download quest files may be named with the .mnm extension instead of .bin, since the format is the same as the standard map files (in system/ep3/). These files can be encoded in any of the formats described above, except .qst. There are no encrypted Episode 3 GCI formats because the game doesn't encrypt quests saved to the memory card, unlike Episodes 1&2.
|
||||
|
||||
When newserv indexes the quests during startup, it will warn (but not fail) if any quests are corrupt or in unrecognized formats.
|
||||
|
||||
If you've changed the contents of the quests directory, you can re-index the quests without restarting the server by running `reload quests` in the interactive shell.
|
||||
If you've changed the contents of the quests directory, you can re-index the quests without restarting the server by running `reload quests` in the interactive shell. The new quests will be available immediately, but any games with quests already in progress will continue using the old versions of the quests until those quests end.
|
||||
|
||||
All quests, including those originally in GCI or DLQ format, are treated as online quests unless their filenames specify the dl category. newserv allows players to download all quests, even those in non-download categories.
|
||||
|
||||
### Patches and DOL files
|
||||
### Client patch directories
|
||||
|
||||
If you're not playing PSO Blue Burst on newserv, you can skip these steps.
|
||||
|
||||
newserv implements a patch server for PSO PC and PSO BB game data. Any file or directory you put in the system/patch-bb or system/patch-pc directories will be synced to clients when they connect to the patch server.
|
||||
|
||||
To make server startup faster, newserv caches the modification times, sizes, and checksums of the files in the patch directories. If the patch server appears to be misbehaving, try deleting the .metadata-cache.json file in the relevant patch directory to force newserv to recompute all the checksums. Also, in the case when checksums are cached, newserv may not actually load the data for a patch file until it's needed by a client. Therefore, modifying any part of the patch tree while newserv is running can cause clients to see an inconsistent view of it.
|
||||
|
||||
For BB clients, newserv reads some files out of the patch data to implement game logic, so it's important that certain game files are synchronized between the server and the client. newserv contains defaults for these files in the system/blueburst/map directory, but if these don't match the client's copies of the files, odd behavior will occur in games.
|
||||
|
||||
Specifically, the patch-bb directory should contain at least the data.gsl file and all map_*.dat files from the version of PSOBB that you want to play on newserv. You can copy these files out of the client's data directory from a clean installation, and put them in system/patch-bb/data.
|
||||
|
||||
### Memory patches and DOL files
|
||||
|
||||
Everything in this section requires resource_dasm to be installed, so newserv can use the PowerPC assembler and disassembler from its libresource_file library. If resource_dasm is not installed, newserv will still build and run, but these features will not be available.
|
||||
|
||||
You can put patches in the system/ppc directory with filenames like PatchName.patch.s and they will appear in the Patches menu for PSO GC clients that support patching. Patches are written in PowerPC assembly and are compiled when newserv is started. See system/ppc/WriteMemory.s for a commented example of such a function.
|
||||
In addition, these features are only supported for the following game versions:
|
||||
* PSO GameCube Episodes 1&2 JP, USA, and EU (not Plus)
|
||||
* PSO GameCube Episodes 1&2 Plus JP v1.04 (not v1.05)
|
||||
* PSO GameCube Episode 3 Trial Edition
|
||||
* PSO GameCube Episode 3 JP
|
||||
* PSO GameCube Episode 3 USA (experimental; must be manually enabled in config.json)
|
||||
|
||||
You can also put DOL files in the system/dol directory, and they will appear in the Programs menu. Selecting a DOL file there will load the file into their GameCube's memory and run it, just like the old homebrew loaders (PSUL and PSOload) did. For this to work, ReadMemoryWord.s, WriteMemory.s, and RunDOL.s must be present in the system/ppc directory. This has been tested on Dolphin but not on a real GameCube, so results may vary.
|
||||
You can put memory patches in the system/ppc directory with filenames like PatchName.patch.s and they will appear in the Patches menu for PSO GC clients that support patching. Memory patches are written in PowerPC assembly and are compiled when newserv is started. The PowerPC assembly system's features are documented in the comments in system/ppc/WriteMemory.s - this file is not a memory patch itself, but it describes how memory patches may be written and the restrictions that apply to them.
|
||||
|
||||
You can also put DOL files in the system/dol directory, and they will appear in the Programs menu. 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.s, WriteMemory.s, and RunDOL.s must be present in the system/ppc directory. This has been tested on Dolphin but not on a real GameCube, so results may vary.
|
||||
|
||||
I mainly built the DOL loading functionality for documentation purposes. By now, there are many better ways to load homebrew code on an unmodified GameCube, but to my knowledge there isn't another open-source implementation of this method in existence.
|
||||
|
||||
### Using newserv as a proxy
|
||||
|
||||
If you want to play online on remote servers rather than running your own server, newserv also includes a PSO proxy. Currently this works with PSO GC and may work with PC and DC; it also works with some BB clients in specific situations.
|
||||
|
||||
To use the proxy for PSO DC, PC, or GC, add an entry to the corresponding ProxyDestinations dictionary in config.json, then run newserv and connect to it as normal (see below). You'll see a "Proxy server" option in the main menu, and you can pick which remote server to connect to.
|
||||
|
||||
To use the proxy for PSO BB, set the ProxyDestination-BB entry in config.json. If this option is set, it essentially disables the game server for all PSO BB clients - all clients will be proxied to the specified destination instead. Unfortunately, because PSO BB uses a different set of handlers for the data server phase and character selection, there's no in-game way to present the player with a list of options, like there is on PSO PC and PSO GC.
|
||||
|
||||
When you're on PSO DC, PC, or GC and are connected to a remote server through newserv's proxy, choosing the Change Ship or Change Block action from the lobby counter will send you back to newserv's main menu instead of the remote server's ship or block select menu. You can go back to the server you were just on by choosing it from the proxy server menu again.
|
||||
|
||||
The remote server will probably try to assign you a Guild Card number that doesn't match the one you have on newserv. On PSO DC, PC and GC, the proxy server rewrites the commands in transit to make it look like the remote server assigned you the same Guild Card number as you have on newserv, but if the remote server has some external integrations (e.g. forum or Discord bots), they will use the Guild Card number that the remote server believes it has assigned to you. The number assigned by the remote server is shown to you when you first connect to the remote server, and you can retrieve it in lobbies or during games with the $li command.
|
||||
|
||||
Some chat commands (see below) have the same basic function on the proxy server but have different effects or conditions. In addition, there are some server shell commands that affect clients on the proxy (run 'help' in the shell to see what they are). All proxy commands in the server shell only work when there's exactly one client connected through the proxy, since there isn't (yet) a way to say via the shell which session you want the command to apply to.
|
||||
|
||||
### Chat commands
|
||||
|
||||
The server's shell supports a variety of administration commands. If the interactive shell is enabled, you can enter these commands at any time, even if the prompt isn't visible. Run `help` in the server's shell to see all of the commands and how to use them.
|
||||
@@ -78,34 +218,47 @@ newserv also supports a variety of commands players can use via the chat interfa
|
||||
Some commands only work on the game server and not on the proxy server. The chat commands are:
|
||||
|
||||
* Information commands
|
||||
* `$li`: Shows basic information about the lobby or game you're in. If you're on the proxy server, shows information about your connection (remote Guild Card number, client ID, etc.) instead.
|
||||
* `$li`: Shows basic information about the lobby or game you're in. If you're on the proxy server, shows information about your connection instead (remote Guild Card number, client ID, etc.).
|
||||
* `$what` (game server only): Shows the type, name, and stats of the nearest item on the ground.
|
||||
|
||||
* Debugging commands
|
||||
* `$dbgid` (game server only): Enable or disable high ID preference. When enabled, you'll be placed into the latest available slot in lobbies and games instead of the earliest. Can be useful for finding commands for which newserv doesn't handle client IDs properly.
|
||||
* `$gc` (game server only): Send your own Guild Card to yourself.
|
||||
* `$persist` (game server only): Enable or disable persistence for the current lobby or game. This determines whether the lobby/game is deleted when the last player leaves. You need the DEBUG permission in your user license to use this command because there are no game state checks when you do this. For example, if you make a game persistent, start a quest, then leave the game, the game can't be joined by anyone but also can't be deleted.
|
||||
* `$sc <data>`: Send a command to yourself.
|
||||
* `$ss <data>` (proxy server only): Send a command to the remote server.
|
||||
|
||||
* 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.
|
||||
* `$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.
|
||||
* `$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 an Episode 3 game or spectator team, sends you to the lobby (but does not end your proxy session if you're in one).
|
||||
* `$patch <name>`: Run a patch on your client. `<name>` must exactly match the name of a patch on the server.
|
||||
|
||||
* Blue Burst player commands (game server only)
|
||||
* `$bbchar <username> <password> <1-4>`: Use this command when playing on a non-BB version of PSO. If the username and password are correct, this command converts your current character to BB format and saves it on the server in the given slot.
|
||||
* `$bbchar <username> <password> <1-4>`: Use this command when playing on a non-BB version of PSO. If the username and password are correct, this command converts your current character to BB format and saves it on the server in the given slot. Any character already in that slot is overwritten.
|
||||
* `$edit <stat> <value>`: Modifies your character data.
|
||||
* `$item <data>`: Sets the next item to be dropped from an enemy or box.
|
||||
|
||||
* Game state commands (game server only)
|
||||
* `$maxlevel <level>`: Sets the maximum level for players to join the current game.
|
||||
* `$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.
|
||||
* `$spec`: Toggles the allow spectators flag. If any players are spectating when this flag is disabled, they will be sent back to the lobby.
|
||||
* `$saverec <name>`: Save the recording of the last Episode 3 battle.
|
||||
* `$playrec <name>`: Play a battle recording. This command creates a spectator team and replays the specified battle log within it. There is a known issue which causes spectators to crash in some cases, so use of this command is currently not recommended.
|
||||
|
||||
* Cheat mode commands
|
||||
* `$cheat`: Enables or disables cheat mode for the current game. All other cheat mode commands do nothing if cheat mode is disabled. This command does nothing on the proxy server, since cheat mode is always enabled there.
|
||||
* `$cheat`: Enables or disables cheat mode for the current game. All other cheat mode commands do nothing if cheat mode is disabled. This command does nothing on the proxy server - cheat commands are always available there.
|
||||
* `$infhp` / `$inftp`: Enables or disables infinite HP or TP mode. Applies to only you. In infinite HP mode, one-hit KO attacks will still kill you.
|
||||
* `$warp <area-id>`: Warps yourself to the given area.
|
||||
* `$next` (game server only): Warps yourself to the next area.
|
||||
* `$next`: Warps yourself to the next area.
|
||||
* `$swa`: Enables or disables switch assist. When enabled, the server will attempt to automatically unlock two-player doors in solo games if you step on both switches sequentially.
|
||||
* `$item <data>` (or `$i <data>`): Create an item. 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.
|
||||
|
||||
* 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, only you will see the new event; other players will not.
|
||||
* `$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.
|
||||
* `$allevent <event>` (game server only): Sets the current holiday event in all lobbies.
|
||||
* `$song <song-id>` (game server only, Episode 3 only): Plays a specific song in the current lobby.
|
||||
* `$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.
|
||||
@@ -114,36 +267,68 @@ Some commands only work on the game server and not on the proxy server. The chat
|
||||
* `$kick <identifier>`: Disconnects a player. The identifier may be the player's name or Guild Card number.
|
||||
* `$ban <identifier>`: Bans a player. The identifier may be the player's name or Guild Card number.
|
||||
|
||||
### Using newserv as a proxy
|
||||
|
||||
If you want to play online on remote servers rather than running your own server, newserv also includes a PSO proxy. Currently this works with PSO GC and may work with PC; it also works with some BB clients in specific situations.
|
||||
|
||||
To use the proxy, add an entry to the ProxyDestinations dictionary in config.json, then run newserv and connect to it as normal (see below). You'll see a "Proxy server" option in the main menu, and you can pick which remote server to connect to.
|
||||
|
||||
A few things to be aware of when using the proxy server:
|
||||
- On PC and GC, using the Change Ship or Change Block actions from the lobby counter will bring you back to newserv's main menu, not the remote server's ship select. You can go back to the server you were just on by choosing it from newserv's proxy server menu again.
|
||||
- The remote server will probably try to assign you a Guild Card number that doesn't match the one you have on newserv. The proxy server rewrites the commands on the fly to make it look like the remote server assigned you the same Guild Card number as you have on newserv, but if the remote server has some external integrations (e.g. forum or Discord bots), they will use the Guild Card number that the remote server believes it has assigned to you. The number assigned by the remote server is shown to you when you first connect to the remote server, and you can retrieve it in lobbies or during games with the $li command.
|
||||
- There are shell commands that affect clients on the proxy (run 'help' in the shell to see what they are). All proxy commands in the shell only work when there's exactly one client connected through the proxy, since there isn't (yet) a way to say via the shell which session you want to affect.
|
||||
|
||||
### Connecting local clients
|
||||
|
||||
If you're running PSO on a real GameCube, you can make it connect to newserv by setting its default gateway and DNS server addresses to newserv's address. newserv's DNS server must be running on port 53 and accessible.
|
||||
#### PSO DC
|
||||
|
||||
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, 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.
|
||||
Some versions of PSO DC will connect to a private server if you just set their DNS server address (in the network configuration) to newserv's address, and enable newserv's DNS server. This will not work for other versions; for those, you'll need a cheat code. Creating such a code is beyond the scope of this document.
|
||||
|
||||
If you're emulating PSO using a version of Dolphin with tapserver support (currently only the macOS version), you can make it connect to a newserv instance running on the same machine via the tapserver interface. This works for all PSO versions, including Plus and Episode III, without the trickery described above. To do this:
|
||||
- Set Dolphin's BBA type to tapserver (Config -> GameCube -> SP1).
|
||||
- Enable newserv's IP stack simulator according to the comments in config.json, and start newserv. You do not need to install or run tapserver.
|
||||
- In PSO, you have to configure the network settings manually (DHCP doesn't work), but the actual values don't matter as long as they're valid IP addresses. Example values:
|
||||
If you're emulating PSO DC or have a disc image, you can patch the appropriate files within the disc image to make it connect to any address you want. Creating such a patch is also beyond the scope of this document.
|
||||
|
||||
Finally, if you're emulating PSO DC, you can modify the loaded executable in memory to make it connect anywhere you want. There is a script included with newserv that can do this for Flycast. The script only works on macOS because it uses memwatch, which is specifically for macOS, but a similar technique could be done manually using scanmem on Linux or Cheat Engine on Windows. (The script is fairly short, and what it does should be easy to understand so you can duplicate its effects with scanmem or Cheat Engine.)
|
||||
|
||||
To use the script, do this:
|
||||
1. Build and install memwatch (https://github.com/fuzziqersoftware/memwatch).
|
||||
2. Start Flycast and run PSO. (You must run the script below after PSO is loaded - it won't work if you run it before loading the game.)
|
||||
3. Run `sudo patch_flycast_memory.py <original-destination>`. Replace `<original-destination>` with the hostname that PSO wants to connect to (you can find this out by using Wireshark and looking for DNS queries). The script may take up to a minute; you can continue using Flycast while it runs, but don't start an online game until the script is done.
|
||||
4. Run newserv and start an online game in PSO.
|
||||
|
||||
If you use this method, you'll have to run the script every time you start PSO in Flycast, but you won't have to run it again if you start another online game without restarting emulation.
|
||||
|
||||
Finally, the script takes an optional second argument that allows you to redirect the connection elsewhere (instead of the local machine). THis allows you to connect directly to remote servers if desired.
|
||||
|
||||
#### PSO PC
|
||||
|
||||
The version of PSO PC I have has the server addresses starting at offset 0x29CB34 in pso.exe. Using a hex editor, change those to "localhost" (without quotes) if you just want to connect to a locally-running newserv instance. Alternatively, you can add an entry to the Windows hosts file (C:\Windows\System32\drivers\etc\hosts) to redirect the connection to 127.0.0.1 (localhost) or any other IP address.
|
||||
|
||||
#### PSO GC on a real GameCube
|
||||
|
||||
You can make PSO connect to newserv by setting its default gateway and DNS server addresses to newserv's address. newserv's DNS server must be running on port 53 and must be accessible to the GameCube.
|
||||
|
||||
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 Dolphin
|
||||
|
||||
If you have BBA support via a tap interface, you may be able to just set the DNS server address (as you would on a real GameCube, above) and it may work. This does not work on macOS, but you can use the tapserver interface instead (below).
|
||||
|
||||
If you're using a version of Dolphin with tapserver support (currently only the macOS version), you can make it connect to a newserv instance running on the same machine via the tapserver interface. You do not need to install or run tapserver, and this works for all PSO versions without any of the dual-interface trickery described above. To do this:
|
||||
1. Set Dolphin's BBA type to tapserver (Config -> GameCube -> SP1).
|
||||
2. Enable newserv's IP stack simulator according to the comments in config.json and start newserv.
|
||||
3. In PSO, you have to configure the network settings manually (DHCP doesn't work), but the actual values don't matter as long as they're valid IP addresses. Example values:
|
||||
- IP address: `10.0.1.5`
|
||||
- Subnet mask: `255.255.255.0`
|
||||
- Default gateway: `10.0.1.1`
|
||||
- DNS server address 1: `10.0.1.1`
|
||||
- Leave everything else blank
|
||||
- Start an online game.
|
||||
4. Start an online game.
|
||||
|
||||
### Connecting external clients
|
||||
|
||||
If you want to accept connections from outside your local network, you'll need to set ExternalAddress to your public IP address in the configuration file, and you'll likely need to open some ports in your router's NAT configuration - specifically, all the TCP ports listed in PortConfiguration in config.json.
|
||||
|
||||
For GC clients, you'll have to use newserv's built-in DNS server or set up your own DNS server as well. If you want external clients to be able to use your DNS server, you'll have to forward UDP port 53 to your newserv instance. Remote players can then connect to your server by entering your DNS server's IP address in their client's network configuration.
|
||||
|
||||
### Non-server usage
|
||||
|
||||
newserv has many CLI options, which can be used to access functionality other than the game/proxy server. Run `newserv help` to see these options and how to use them. The non-server things newserv can do are:
|
||||
|
||||
* Compress or decompress data in the PRS and BC0 formats
|
||||
* Compute the decompressed size of compressed PRS data without decompressing it
|
||||
* Encrypt or decrypt data using any PSO version's network encryption scheme
|
||||
* Encrypt or decrypt data using Episode 3's trivial scheme
|
||||
* Run a brute-force search for a decryption seed
|
||||
* Decode Shift-JIS text to UTF-16
|
||||
* Convert quests in .gci, .dlq, or .qst format to .bin/.dat format
|
||||
* Extract the contents of a .gsl archive
|
||||
* Connect to another PSO server and pretend to be a client
|
||||
* Format Episode 3 game data in a human-readable manner
|
||||
|
||||
Executable
+39
@@ -0,0 +1,39 @@
|
||||
#!/bin/env python3
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
|
||||
def get_ip_address(ifname):
|
||||
data = subprocess.check_output(['ifconfig', ifname])
|
||||
for line in data.splitlines():
|
||||
line = line.strip()
|
||||
if line.startswith(b'inet '):
|
||||
return line.split()[1].decode('ascii')
|
||||
raise RuntimeError('cannot get address for interface ' + ifname)
|
||||
|
||||
|
||||
def main(argv):
|
||||
if len(argv) < 2:
|
||||
raise RuntimeError(f'Usage: {argv[0]} <original-destination> [new-destination]')
|
||||
if os.geteuid() != 0:
|
||||
raise RuntimeError('You must use sudo to run this script')
|
||||
original_destination = argv[1]
|
||||
new_destination = argv[2] if len(argv) > 2 else get_ip_address('en0')
|
||||
|
||||
print(f'Finding occurrences of \"{original_destination}\"')
|
||||
addresses_str = subprocess.check_output(['memwatch', 'Flycast.app', 'find', f'\"{original_destination}\"'])
|
||||
for line in addresses_str.splitlines():
|
||||
# line is like '(0) 00007FFF038500A0 (rw-)' (we care only about the address)
|
||||
tokens = line.split()
|
||||
if len(tokens) != 3:
|
||||
continue
|
||||
print(f'Replacing \"{original_destination}\" with \"{new_destination}\" at {tokens[1]} in Flycast')
|
||||
subprocess.check_call(['memwatch', 'Flycast.app', 'write', tokens[1], f'\"{new_destination}\" 00'])
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main(sys.argv))
|
||||
BIN
Binary file not shown.
|
After Width: | Height: | Size: 364 B |
Binary file not shown.
|
After Width: | Height: | Size: 364 B |
BIN
Binary file not shown.
|
After Width: | Height: | Size: 442 B |
Binary file not shown.
|
After Width: | Height: | Size: 560 B |
@@ -0,0 +1,141 @@
|
||||
#include "CatSession.hh"
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <event2/buffer.h>
|
||||
#include <event2/bufferevent.h>
|
||||
#include <event2/event.h>
|
||||
#include <event2/listener.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <phosg/Encoding.hh>
|
||||
#include <phosg/Filesystem.hh>
|
||||
#include <phosg/Network.hh>
|
||||
#include <phosg/Random.hh>
|
||||
#include <phosg/Strings.hh>
|
||||
#include <phosg/Time.hh>
|
||||
|
||||
#include "Loggers.hh"
|
||||
#include "PSOProtocol.hh"
|
||||
#include "SendCommands.hh"
|
||||
#include "ReceiveCommands.hh"
|
||||
#include "ReceiveSubcommands.hh"
|
||||
#include "ProxyCommands.hh"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
|
||||
CatSession::CatSession(
|
||||
shared_ptr<struct event_base> base,
|
||||
const struct sockaddr_storage& remote,
|
||||
GameVersion version,
|
||||
shared_ptr<const PSOBBEncryption::KeyFile> bb_key_file)
|
||||
: Shell(base),
|
||||
log("[CatSession] ", proxy_server_log.min_level),
|
||||
channel(
|
||||
version,
|
||||
CatSession::dispatch_on_channel_input,
|
||||
CatSession::dispatch_on_channel_error,
|
||||
this,
|
||||
"CatSession"),
|
||||
bb_key_file(bb_key_file) {
|
||||
if (remote.ss_family != AF_INET) {
|
||||
throw runtime_error("remote is not AF_INET");
|
||||
}
|
||||
|
||||
string netloc_str = render_sockaddr_storage(remote);
|
||||
this->log.info("Connecting to %s", netloc_str.c_str());
|
||||
|
||||
struct bufferevent* bev = bufferevent_socket_new(
|
||||
this->base.get(), -1, BEV_OPT_CLOSE_ON_FREE | BEV_OPT_DEFER_CALLBACKS);
|
||||
if (!bev) {
|
||||
throw runtime_error(string_printf("failed to open socket (%d)", EVUTIL_SOCKET_ERROR()));
|
||||
}
|
||||
this->channel.set_bufferevent(bev);
|
||||
|
||||
if (bufferevent_socket_connect(this->channel.bev.get(),
|
||||
reinterpret_cast<const sockaddr*>(&remote), sizeof(struct sockaddr_in)) != 0) {
|
||||
throw runtime_error(string_printf("failed to connect (%d)", EVUTIL_SOCKET_ERROR()));
|
||||
}
|
||||
}
|
||||
|
||||
void CatSession::dispatch_on_channel_input(
|
||||
Channel& ch, uint16_t command, uint32_t flag, std::string& data) {
|
||||
auto* session = reinterpret_cast<CatSession*>(ch.context_obj);
|
||||
session->on_channel_input(command, flag, data);
|
||||
}
|
||||
|
||||
void CatSession::on_channel_input(
|
||||
uint16_t command, uint32_t flag, std::string& data) {
|
||||
if (this->channel.version != GameVersion::BB) {
|
||||
if (command == 0x02 || command == 0x17 || command == 0x91 || command == 0x9B) {
|
||||
const auto& cmd = check_size_t<S_ServerInitDefault_DC_PC_V3_02_17_91_9B>(data,
|
||||
sizeof(S_ServerInitDefault_DC_PC_V3_02_17_91_9B), 0xFFFF);
|
||||
if ((this->channel.version == GameVersion::GC) ||
|
||||
(this->channel.version == GameVersion::XB)) {
|
||||
this->channel.crypt_in.reset(new PSOV3Encryption(cmd.server_key));
|
||||
this->channel.crypt_out.reset(new PSOV3Encryption(cmd.client_key));
|
||||
this->log.info("Enabled V3 encryption (server key %08" PRIX32 ", client key %08" PRIX32 ")",
|
||||
cmd.server_key.load(), cmd.client_key.load());
|
||||
} else { // PC, DC, or patch server
|
||||
this->channel.crypt_in.reset(new PSOV2Encryption(cmd.server_key));
|
||||
this->channel.crypt_out.reset(new PSOV2Encryption(cmd.client_key));
|
||||
this->log.info("Enabled V2 encryption (server key %08" PRIX32 ", client key %08" PRIX32 ")",
|
||||
cmd.server_key.load(), cmd.client_key.load());
|
||||
}
|
||||
}
|
||||
} else { // BB
|
||||
if (command == 0x03 || command == 0x9B) {
|
||||
if (!this->bb_key_file) {
|
||||
throw runtime_error("BB encryption requires a key file");
|
||||
}
|
||||
const auto& cmd = check_size_t<S_ServerInitDefault_BB_03_9B>(data,
|
||||
sizeof(S_ServerInitDefault_BB_03_9B), 0xFFFF);
|
||||
this->channel.crypt_in.reset(new PSOBBEncryption(*this->bb_key_file, &cmd.server_key[0], sizeof(cmd.server_key)));
|
||||
this->channel.crypt_out.reset(new PSOBBEncryption(*this->bb_key_file, &cmd.client_key[0], sizeof(cmd.client_key)));
|
||||
this->log.info("Enabled BB encryption");
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Use the iovec form of print_data here instead of
|
||||
// prepend_command_header (which copies the string)
|
||||
string full_cmd = prepend_command_header(
|
||||
this->channel.version, this->channel.crypt_in.get(), command, flag, data);
|
||||
print_data(stdout, full_cmd, 0, nullptr, PrintDataFlags::PRINT_ASCII | PrintDataFlags::OFFSET_16_BITS);
|
||||
}
|
||||
|
||||
void CatSession::dispatch_on_channel_error(Channel& ch, short events) {
|
||||
auto* session = reinterpret_cast<CatSession*>(ch.context_obj);
|
||||
session->on_channel_error(events);
|
||||
}
|
||||
|
||||
void CatSession::on_channel_error(short events) {
|
||||
if (events & BEV_EVENT_CONNECTED) {
|
||||
this->log.info("Channel connected");
|
||||
}
|
||||
if (events & BEV_EVENT_ERROR) {
|
||||
int err = EVUTIL_SOCKET_ERROR();
|
||||
this->log.warning("Error %d (%s) in unlinked client stream", err,
|
||||
evutil_socket_error_to_string(err));
|
||||
}
|
||||
if (events & (BEV_EVENT_ERROR | BEV_EVENT_EOF)) {
|
||||
this->log.info("Session endpoint has disconnected");
|
||||
this->channel.disconnect();
|
||||
event_base_loopexit(this->base.get(), nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
void CatSession::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());
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
|
||||
#include <event2/event.h>
|
||||
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <phosg/Filesystem.hh>
|
||||
|
||||
#include "PSOEncryption.hh"
|
||||
#include "PSOProtocol.hh"
|
||||
#include "ServerState.hh"
|
||||
#include "Shell.hh"
|
||||
|
||||
|
||||
|
||||
class CatSession : public Shell {
|
||||
public:
|
||||
CatSession(
|
||||
std::shared_ptr<struct event_base> base,
|
||||
const struct sockaddr_storage& remote,
|
||||
GameVersion version,
|
||||
std::shared_ptr<const PSOBBEncryption::KeyFile> bb_key_file);
|
||||
virtual ~CatSession() = default;
|
||||
|
||||
protected:
|
||||
PrefixedLogger log;
|
||||
Channel channel;
|
||||
std::shared_ptr<const PSOBBEncryption::KeyFile> bb_key_file;
|
||||
|
||||
virtual void print_prompt();
|
||||
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_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);
|
||||
};
|
||||
+38
-22
@@ -10,6 +10,7 @@
|
||||
#include <phosg/Network.hh>
|
||||
#include <phosg/Time.hh>
|
||||
|
||||
#include "Loggers.hh"
|
||||
#include "Version.hh"
|
||||
|
||||
using namespace std;
|
||||
@@ -133,7 +134,8 @@ void Channel::disconnect() {
|
||||
auto on_error = +[](struct bufferevent* bev, short events, void*) -> void {
|
||||
if (events & BEV_EVENT_ERROR) {
|
||||
int err = EVUTIL_SOCKET_ERROR();
|
||||
log(WARNING, "Disconnecting channel caused error %d (%s)", err,
|
||||
channel_exceptions_log.warning(
|
||||
"Disconnecting channel caused error %d (%s)", err,
|
||||
evutil_socket_error_to_string(err));
|
||||
}
|
||||
if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) {
|
||||
@@ -201,8 +203,16 @@ Channel::Message Channel::recv(bool print_contents) {
|
||||
< static_cast<ssize_t>(command_data.size())) {
|
||||
throw logic_error("enough bytes available, but could not remove them");
|
||||
}
|
||||
|
||||
if (this->crypt_in.get()) {
|
||||
// Some versions of PSO DC can send commands whose sizes are not a multiple
|
||||
// of 4, but the server is expected to always use a multiple of 4 bytes when
|
||||
// decrypting (the extra cipher bytes are lost). To emulate this behavior,
|
||||
// we have to round up the size for DC commands here.
|
||||
size_t orig_size = command_data.size();
|
||||
command_data.resize((orig_size + 3) & (~3), 0);
|
||||
this->crypt_in->decrypt(command_data.data(), command_data.size());
|
||||
command_data.resize(orig_size);
|
||||
}
|
||||
command_data.resize(command_logical_size - header_size);
|
||||
|
||||
@@ -211,20 +221,23 @@ Channel::Message Channel::recv(bool print_contents) {
|
||||
print_color_escape(stderr, this->terminal_recv_color, TerminalFormat::BOLD, TerminalFormat::END);
|
||||
}
|
||||
|
||||
string name_token;
|
||||
if (!this->name.empty()) {
|
||||
name_token = " from " + this->name;
|
||||
if (version == GameVersion::BB) {
|
||||
command_data_log.info("Received from %s (version=BB command=%04hX flag=%08" PRIX32 ")",
|
||||
this->name.c_str(),
|
||||
header.command(this->version),
|
||||
header.flag(this->version));
|
||||
} else {
|
||||
command_data_log.info("Received from %s (version=%s command=%02hX flag=%02" PRIX32 ")",
|
||||
this->name.c_str(),
|
||||
name_for_version(this->version),
|
||||
header.command(this->version),
|
||||
header.flag(this->version));
|
||||
}
|
||||
log(INFO, "Received%s (version=%s command=%04hX flag=%08X)",
|
||||
name_token.c_str(),
|
||||
name_for_version(this->version),
|
||||
header.command(this->version),
|
||||
header.flag(this->version));
|
||||
|
||||
vector<struct iovec> iovs;
|
||||
iovs.emplace_back(iovec{.iov_base = header_data.data(), .iov_len = header_data.size()});
|
||||
iovs.emplace_back(iovec{.iov_base = command_data.data(), .iov_len = command_data.size()});
|
||||
print_data(stderr, iovs, 0, nullptr, PrintDataFlags::PRINT_ASCII | PrintDataFlags::DISABLE_COLOR);
|
||||
print_data(stderr, iovs, 0, nullptr, PrintDataFlags::PRINT_ASCII | PrintDataFlags::DISABLE_COLOR | PrintDataFlags::OFFSET_16_BITS);
|
||||
|
||||
if (use_terminal_colors && this->terminal_recv_color != TerminalFormat::NORMAL) {
|
||||
print_color_escape(stderr, TerminalFormat::NORMAL, TerminalFormat::END);
|
||||
@@ -241,16 +254,18 @@ Channel::Message Channel::recv(bool print_contents) {
|
||||
void Channel::send(uint16_t cmd, uint32_t flag, const void* data, size_t size,
|
||||
bool print_contents) {
|
||||
if (!this->connected()) {
|
||||
log(WARNING, "Attempted to send command on closed channel; dropping data");
|
||||
channel_exceptions_log.warning("Attempted to send command on closed channel; dropping data");
|
||||
return;
|
||||
}
|
||||
|
||||
string send_data;
|
||||
size_t logical_size;
|
||||
size_t send_data_size = 0;
|
||||
switch (this->version) {
|
||||
case GameVersion::DC:
|
||||
case GameVersion::GC:
|
||||
case GameVersion::DC: {
|
||||
PSOCommandHeaderDCGC header;
|
||||
case GameVersion::XB: {
|
||||
PSOCommandHeaderDCV3 header;
|
||||
if (this->crypt_out.get()) {
|
||||
send_data_size = (sizeof(header) + size + 3) & ~3;
|
||||
} else {
|
||||
@@ -306,7 +321,7 @@ void Channel::send(uint16_t cmd, uint32_t flag, const void* data, size_t size,
|
||||
throw logic_error("unimplemented game version in send_command");
|
||||
}
|
||||
|
||||
// All versions of PSO I've seen (PC, GC, BB) have a receive buffer 0x7C00
|
||||
// All versions of PSO I've seen (so far) have a receive buffer 0x7C00
|
||||
// bytes in size
|
||||
if (send_data_size > 0x7C00) {
|
||||
throw runtime_error("outbound command too large");
|
||||
@@ -318,16 +333,17 @@ void Channel::send(uint16_t cmd, uint32_t flag, const void* data, size_t size,
|
||||
}
|
||||
|
||||
if (print_contents && (this->terminal_send_color != TerminalFormat::END)) {
|
||||
string name_token;
|
||||
if (!this->name.empty()) {
|
||||
name_token = " to " + this->name;
|
||||
}
|
||||
if (use_terminal_colors && this->terminal_send_color != TerminalFormat::NORMAL) {
|
||||
print_color_escape(stderr, TerminalFormat::FG_YELLOW, TerminalFormat::BOLD, TerminalFormat::END);
|
||||
}
|
||||
log(INFO, "Sending%s (version=%s command=%04hX flag=%08X)",
|
||||
name_token.c_str(), name_for_version(version), cmd, flag);
|
||||
print_data(stderr, send_data.data(), logical_size, 0, nullptr, PrintDataFlags::PRINT_ASCII | PrintDataFlags::DISABLE_COLOR);
|
||||
if (version == GameVersion::BB) {
|
||||
command_data_log.info("Sending to %s (version=BB command=%04hX flag=%08" PRIX32 ")",
|
||||
this->name.c_str(), cmd, flag);
|
||||
} else {
|
||||
command_data_log.info("Sending to %s (version=%s command=%02hX flag=%02" PRIX32 ")",
|
||||
this->name.c_str(), name_for_version(version), cmd, flag);
|
||||
}
|
||||
print_data(stderr, send_data.data(), logical_size, 0, nullptr, PrintDataFlags::PRINT_ASCII | PrintDataFlags::DISABLE_COLOR | PrintDataFlags::OFFSET_16_BITS);
|
||||
if (use_terminal_colors && this->terminal_send_color != TerminalFormat::NORMAL) {
|
||||
print_color_escape(stderr, TerminalFormat::NORMAL, TerminalFormat::END);
|
||||
}
|
||||
@@ -373,7 +389,7 @@ void Channel::dispatch_on_input(struct bufferevent*, void* ctx) {
|
||||
} catch (const out_of_range&) {
|
||||
break;
|
||||
} catch (const exception& e) {
|
||||
log(WARNING, "Error receiving on channel: %s", e.what());
|
||||
channel_exceptions_log.warning("Error receiving on channel: %s", e.what());
|
||||
ch->on_error(*ch, BEV_EVENT_ERROR);
|
||||
break;
|
||||
}
|
||||
|
||||
+7
-1
@@ -38,14 +38,16 @@ struct Channel {
|
||||
on_error_t on_error;
|
||||
void* context_obj;
|
||||
|
||||
// Creates an unconnected channel
|
||||
Channel(
|
||||
GameVersion version,
|
||||
on_command_received_t on_command_received,
|
||||
on_error_t on_error,
|
||||
void* context_obj,
|
||||
const std::string& name = "",
|
||||
const std::string& name,
|
||||
TerminalFormat terminal_send_color = TerminalFormat::END,
|
||||
TerminalFormat terminal_recv_color = TerminalFormat::END);
|
||||
// Creates a connected channel
|
||||
Channel(
|
||||
struct bufferevent* bev,
|
||||
GameVersion version,
|
||||
@@ -80,6 +82,10 @@ struct Channel {
|
||||
// Sends a message with an automatically-constructed header.
|
||||
void send(uint16_t cmd, uint32_t flag = 0, const void* data = nullptr, size_t size = 0, bool print_contents = true);
|
||||
void send(uint16_t cmd, uint32_t flag, const std::string& data, bool print_contents = true);
|
||||
template <typename CmdT>
|
||||
void send(uint16_t cmd, uint32_t flag, const CmdT& data) {
|
||||
this->send(cmd, flag, &data, sizeof(data));
|
||||
}
|
||||
|
||||
// Sends a message with a pre-existing header (as the first few bytes in the
|
||||
// data)
|
||||
|
||||
+567
-186
File diff suppressed because it is too large
Load Diff
+2
-2
@@ -10,7 +10,7 @@
|
||||
#include "Client.hh"
|
||||
#include "ProxyServer.hh"
|
||||
|
||||
void process_chat_command(std::shared_ptr<ServerState> s, std::shared_ptr<Lobby> l,
|
||||
void on_chat_command(std::shared_ptr<ServerState> s, std::shared_ptr<Lobby> l,
|
||||
std::shared_ptr<Client> c, const std::u16string& text);
|
||||
void process_chat_command(std::shared_ptr<ServerState> s,
|
||||
void on_chat_command(std::shared_ptr<ServerState> s,
|
||||
ProxyServer::LinkedSession& session, const std::u16string& text);
|
||||
|
||||
+76
-11
@@ -2,13 +2,16 @@
|
||||
|
||||
#include <event2/buffer.h>
|
||||
#include <event2/bufferevent.h>
|
||||
#include <event2/event.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <phosg/Network.hh>
|
||||
#include <phosg/Time.hh>
|
||||
|
||||
#include "Loggers.hh"
|
||||
#include "Version.hh"
|
||||
|
||||
using namespace std;
|
||||
@@ -16,6 +19,30 @@ using namespace std;
|
||||
|
||||
|
||||
const uint64_t CLIENT_CONFIG_MAGIC = 0x492A890E82AC9839;
|
||||
FileContentsCache client_options_cache(3600000000ULL);
|
||||
|
||||
static atomic<uint64_t> next_id(1);
|
||||
|
||||
|
||||
|
||||
ClientOptions::ClientOptions()
|
||||
: switch_assist(false),
|
||||
infinite_hp(false),
|
||||
infinite_tp(false),
|
||||
prefer_high_lobby_client_id(false),
|
||||
override_section_id(-1),
|
||||
override_lobby_event(-1),
|
||||
override_lobby_number(-1),
|
||||
override_random_seed(-1),
|
||||
save_files(false),
|
||||
enable_chat_commands(true),
|
||||
enable_chat_filter(true),
|
||||
suppress_remote_login(false),
|
||||
zero_remote_guild_card(false),
|
||||
ep3_infinite_meseta(false),
|
||||
red_name(false),
|
||||
blank_name(false),
|
||||
function_call_return_value(-1) { }
|
||||
|
||||
|
||||
|
||||
@@ -23,13 +50,15 @@ Client::Client(
|
||||
struct bufferevent* bev,
|
||||
GameVersion version,
|
||||
ServerBehavior server_behavior)
|
||||
: version(version),
|
||||
: id(next_id++),
|
||||
log("", client_log.min_level),
|
||||
bb_game_state(0),
|
||||
flags(flags_for_version(this->version, 0)),
|
||||
channel(bev, this->version, nullptr, nullptr, this, "", TerminalFormat::FG_YELLOW, TerminalFormat::FG_GREEN),
|
||||
flags(flags_for_version(version, -1)),
|
||||
channel(bev, version, nullptr, nullptr, this, string_printf("C-%" PRIX64, this->id), TerminalFormat::FG_YELLOW, TerminalFormat::FG_GREEN),
|
||||
server_behavior(server_behavior),
|
||||
should_disconnect(false),
|
||||
should_send_to_lobby_server(false),
|
||||
should_send_to_proxy_server(false),
|
||||
proxy_destination_address(0),
|
||||
proxy_destination_port(0),
|
||||
x(0.0f),
|
||||
@@ -38,23 +67,41 @@ Client::Client(
|
||||
lobby_id(0),
|
||||
lobby_client_id(0),
|
||||
lobby_arrow_color(0),
|
||||
prefer_high_lobby_client_id(false),
|
||||
preferred_lobby_id(-1),
|
||||
save_game_data_event(
|
||||
event_new(
|
||||
bufferevent_get_base(bev), -1, EV_TIMEOUT | EV_PERSIST,
|
||||
&Client::dispatch_save_game_data, this),
|
||||
event_free),
|
||||
card_battle_table_number(-1),
|
||||
card_battle_table_seat_number(0),
|
||||
card_battle_table_seat_state(0),
|
||||
next_exp_value(0),
|
||||
override_section_id(-1),
|
||||
infinite_hp(false),
|
||||
infinite_tp(false),
|
||||
switch_assist(false),
|
||||
can_chat(true),
|
||||
pending_bb_save_player_index(0),
|
||||
dol_base_addr(0) {
|
||||
this->last_switch_enabled_command.subcommand = 0;
|
||||
this->last_switch_enabled_command.header.subcommand = 0;
|
||||
memset(&this->next_connection_addr, 0, sizeof(this->next_connection_addr));
|
||||
|
||||
if (this->version() == GameVersion::BB) {
|
||||
struct timeval tv = usecs_to_timeval(60000000); // 1 minute
|
||||
event_add(this->save_game_data_event.get(), &tv);
|
||||
}
|
||||
}
|
||||
|
||||
Client::~Client() {
|
||||
if (!this->disconnect_hooks.empty()) {
|
||||
this->log.warning("Disconnect hooks pending at client destruction time:");
|
||||
for (const auto& it : this->disconnect_hooks) {
|
||||
this->log.warning(" %s", it.first.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Client::set_license(shared_ptr<const License> l) {
|
||||
this->license = l;
|
||||
this->game_data.serial_number = this->license->serial_number;
|
||||
if (this->version == GameVersion::BB) {
|
||||
this->game_data.guild_card_number = this->license->serial_number;
|
||||
if (this->version() == GameVersion::BB) {
|
||||
this->game_data.bb_username = this->license->username;
|
||||
}
|
||||
}
|
||||
@@ -92,3 +139,21 @@ void Client::import_config(const ClientConfigBB& cc) {
|
||||
this->bb_game_state = cc.bb_game_state;
|
||||
this->game_data.bb_player_index = cc.bb_player_index;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Client::dispatch_save_game_data(evutil_socket_t, short, void* ctx) {
|
||||
reinterpret_cast<Client*>(ctx)->save_game_data();
|
||||
}
|
||||
|
||||
void Client::save_game_data() {
|
||||
if (this->version() != GameVersion::BB) {
|
||||
throw logic_error("save_game_data called for non-BB client");
|
||||
}
|
||||
if (this->game_data.account(false)) {
|
||||
this->game_data.save_account_data();
|
||||
}
|
||||
if (this->game_data.player(false)) {
|
||||
this->game_data.save_player_data();
|
||||
}
|
||||
}
|
||||
|
||||
+93
-31
@@ -6,66 +6,112 @@
|
||||
|
||||
#include "Channel.hh"
|
||||
#include "CommandFormats.hh"
|
||||
#include "FileContentsCache.hh"
|
||||
#include "FunctionCompiler.hh"
|
||||
#include "License.hh"
|
||||
#include "PatchFileIndex.hh"
|
||||
#include "Player.hh"
|
||||
#include "PSOEncryption.hh"
|
||||
#include "PSOProtocol.hh"
|
||||
#include "Text.hh"
|
||||
#include "Episode3/BattleRecord.hh"
|
||||
#include "Episode3/Tournament.hh"
|
||||
|
||||
|
||||
|
||||
extern const uint64_t CLIENT_CONFIG_MAGIC;
|
||||
extern FileContentsCache client_options_cache;
|
||||
|
||||
|
||||
|
||||
struct ClientOptions {
|
||||
// Options used on both game and proxy server
|
||||
bool switch_assist;
|
||||
bool infinite_hp;
|
||||
bool infinite_tp;
|
||||
bool prefer_high_lobby_client_id;
|
||||
int16_t override_section_id;
|
||||
int16_t override_lobby_event;
|
||||
int16_t override_lobby_number;
|
||||
int64_t override_random_seed;
|
||||
|
||||
// Options used only on proxy server
|
||||
bool save_files;
|
||||
bool enable_chat_commands;
|
||||
bool enable_chat_filter;
|
||||
bool suppress_remote_login;
|
||||
bool zero_remote_guild_card;
|
||||
bool ep3_infinite_meseta;
|
||||
bool red_name;
|
||||
bool blank_name;
|
||||
int64_t function_call_return_value; // -1 = don't block function calls
|
||||
|
||||
ClientOptions();
|
||||
};
|
||||
|
||||
struct Client {
|
||||
enum Flag {
|
||||
// This flag has two meanings. If set on a client with GameVersion::DC, then
|
||||
// IS_DC_V1 is also set. In this case, the client is DC Network Trial
|
||||
// Edition, which uses several commands that no other version uses. If this
|
||||
// flag is set without IS_DC_V1, then the client is GC Episodes 1 & 2 Trial
|
||||
// Edition, and therefore uses V2 encryption instead of V3 encryption, and
|
||||
// doesn't support some commands.
|
||||
// Note that this flag is NOT set for Episode 3 Trial Edition clients, since
|
||||
// that version is similar enough to the release version of Episode 3 that
|
||||
// newserv does not have to change its behavior at all.
|
||||
IS_TRIAL_EDITION = 0x00002000,
|
||||
// Client is DC v1
|
||||
IS_DC_V1 = 0x00000010,
|
||||
// For patch server clients, client is Blue Burst rather than PC
|
||||
BB_PATCH = 0x0001,
|
||||
IS_BB_PATCH = 0x00000001,
|
||||
// After joining a lobby, client will no longer send D6 commands when they
|
||||
// close message boxes
|
||||
NO_MESSAGE_BOX_CLOSE_CONFIRMATION_AFTER_LOBBY_JOIN = 0x0002,
|
||||
NO_D6_AFTER_LOBBY = 0x00000002,
|
||||
// Client has the above flag and has already joined a lobby, or is not GC
|
||||
NO_MESSAGE_BOX_CLOSE_CONFIRMATION = 0x0004,
|
||||
NO_D6 = 0x00000004,
|
||||
// Client is Episode 3, should be able to see CARD lobbies, and should only
|
||||
// be able to see/join games with the IS_EPISODE_3 flag
|
||||
EPISODE_3 = 0x0008,
|
||||
// Client is DC v1 (disables some features)
|
||||
DCV1 = 0x0010,
|
||||
// be able to see/join games with the EPISODE_3_ONLY flag
|
||||
IS_EPISODE_3 = 0x00000008,
|
||||
// Client disconnects if it receives B2 (send_function_call)
|
||||
NO_SEND_FUNCTION_CALL = 0x00000200,
|
||||
// Client requires doubly-encrypted code section in send_function_call
|
||||
ENCRYPTED_SEND_FUNCTION_CALL = 0x00000800,
|
||||
// Client supports send_function_call but does not actually run the code
|
||||
SEND_FUNCTION_CALL_CHECKSUM_ONLY = 0x00001000,
|
||||
// Client is vulnerable to a buffer overflow that we can use to enable
|
||||
// send_function_call
|
||||
USE_OVERFLOW_FOR_SEND_FUNCTION_CALL = 0x00008000,
|
||||
|
||||
// Client is loading into a game
|
||||
LOADING = 0x0020,
|
||||
LOADING = 0x00000020,
|
||||
// Client is loading a quest
|
||||
LOADING_QUEST = 0x0040,
|
||||
LOADING_QUEST = 0x00000040,
|
||||
// Client is waiting to join an Episode 3 card auction
|
||||
AWAITING_CARD_AUCTION = 0x00010000,
|
||||
// Client is in the information menu (login server only)
|
||||
IN_INFORMATION_MENU = 0x0080,
|
||||
IN_INFORMATION_MENU = 0x00000080,
|
||||
// Client is at the welcome message (login server only)
|
||||
AT_WELCOME_MESSAGE = 0x0100,
|
||||
// Client disconnect if it receives B2 (send_function_call)
|
||||
DOES_NOT_SUPPORT_SEND_FUNCTION_CALL = 0x0200,
|
||||
AT_WELCOME_MESSAGE = 0x00000100,
|
||||
// Client has already received a 97 (enable saves) command, so don't show
|
||||
// the programs menu anymore
|
||||
SAVE_ENABLED = 0x0400,
|
||||
|
||||
// TODO: Do DCv1 and PC support send_function_call? Here we assume they don't
|
||||
DEFAULT_V1 = DCV1 | NO_MESSAGE_BOX_CLOSE_CONFIRMATION | DOES_NOT_SUPPORT_SEND_FUNCTION_CALL,
|
||||
DEFAULT_V2_DC = NO_MESSAGE_BOX_CLOSE_CONFIRMATION,
|
||||
DEFAULT_V2_PC = NO_MESSAGE_BOX_CLOSE_CONFIRMATION | DOES_NOT_SUPPORT_SEND_FUNCTION_CALL,
|
||||
DEFAULT_V3_GC = 0x0000,
|
||||
DEFAULT_V3_GC_PLUS = NO_MESSAGE_BOX_CLOSE_CONFIRMATION_AFTER_LOBBY_JOIN | DOES_NOT_SUPPORT_SEND_FUNCTION_CALL,
|
||||
DEFAULT_V3_GC_EP3 = NO_MESSAGE_BOX_CLOSE_CONFIRMATION_AFTER_LOBBY_JOIN | EPISODE_3 | DOES_NOT_SUPPORT_SEND_FUNCTION_CALL,
|
||||
DEFAULT_V4_BB = NO_MESSAGE_BOX_CLOSE_CONFIRMATION_AFTER_LOBBY_JOIN | NO_MESSAGE_BOX_CLOSE_CONFIRMATION | SAVE_ENABLED,
|
||||
SAVE_ENABLED = 0x00000400,
|
||||
// Client has received newserv's Episode 3 card definitions, so don't send
|
||||
// them again
|
||||
HAS_EP3_CARD_DEFS = 0x00004000,
|
||||
};
|
||||
|
||||
uint64_t id;
|
||||
PrefixedLogger log;
|
||||
|
||||
// License & account
|
||||
std::shared_ptr<const License> license;
|
||||
GameVersion version;
|
||||
|
||||
// Note: these fields are included in the client config. On GC, the client
|
||||
// config can be up to 0x20 bytes; on BB it can be 0x28 bytes. We don't use
|
||||
// all of that space.
|
||||
uint8_t bb_game_state;
|
||||
uint16_t flags;
|
||||
uint32_t flags;
|
||||
|
||||
// Network
|
||||
Channel channel;
|
||||
@@ -73,35 +119,48 @@ struct Client {
|
||||
ServerBehavior server_behavior;
|
||||
bool should_disconnect;
|
||||
bool should_send_to_lobby_server;
|
||||
bool should_send_to_proxy_server;
|
||||
uint32_t proxy_destination_address;
|
||||
uint16_t proxy_destination_port;
|
||||
std::unordered_map<std::string, std::function<void()>> disconnect_hooks;
|
||||
|
||||
// Patch server
|
||||
std::vector<PatchFileChecksumRequest> patch_file_checksum_requests;
|
||||
|
||||
// Lobby/positioning
|
||||
ClientOptions options;
|
||||
float x;
|
||||
float z;
|
||||
uint32_t area; // which area is the client in?
|
||||
uint32_t lobby_id; // which lobby is this person in?
|
||||
uint8_t lobby_client_id; // which client number is this person?
|
||||
uint8_t lobby_arrow_color; // lobby arrow color ID
|
||||
bool prefer_high_lobby_client_id;
|
||||
int64_t preferred_lobby_id; // <0 = no preference
|
||||
ClientGameData game_data;
|
||||
std::unique_ptr<struct event, void(*)(struct event*)> save_game_data_event;
|
||||
int16_t card_battle_table_number;
|
||||
uint16_t card_battle_table_seat_number;
|
||||
uint16_t card_battle_table_seat_state;
|
||||
std::weak_ptr<Episode3::Tournament::Team> ep3_tournament_team;
|
||||
|
||||
// Miscellaneous (used by chat commands)
|
||||
uint32_t next_exp_value; // next EXP value to give
|
||||
int16_t override_section_id; // valid if >= 0
|
||||
bool infinite_hp; // cheats enabled
|
||||
bool infinite_tp; // cheats enabled
|
||||
bool switch_assist; // cheats enabled
|
||||
G_SwitchStateChanged_6x05 last_switch_enabled_command;
|
||||
bool can_chat;
|
||||
std::string pending_bb_save_username;
|
||||
uint8_t pending_bb_save_player_index;
|
||||
|
||||
// DOL file loading state
|
||||
// File loading state
|
||||
uint32_t dol_base_addr;
|
||||
std::shared_ptr<DOLFileIndex::DOLFile> loading_dol_file;
|
||||
std::unordered_map<std::string, std::shared_ptr<const std::string>> sending_files;
|
||||
|
||||
Client(struct bufferevent* bev, GameVersion version, ServerBehavior server_behavior);
|
||||
~Client();
|
||||
|
||||
inline GameVersion version() const {
|
||||
return this->channel.version;
|
||||
}
|
||||
|
||||
void set_license(std::shared_ptr<const License> l);
|
||||
|
||||
@@ -109,4 +168,7 @@ struct Client {
|
||||
ClientConfigBB export_config_bb() const;
|
||||
void import_config(const ClientConfig& cc);
|
||||
void import_config(const ClientConfigBB& cc);
|
||||
|
||||
static void dispatch_save_game_data(evutil_socket_t, short, void* ctx);
|
||||
void save_game_data();
|
||||
};
|
||||
|
||||
+4325
-1465
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,205 @@
|
||||
#pragma once
|
||||
|
||||
#include <phosg/Encoding.hh>
|
||||
|
||||
#include "Text.hh"
|
||||
|
||||
|
||||
|
||||
// This file describes the structure of PSO GC ItemPT.gsl entries.
|
||||
// TODO: This is not (yet) used anywhere in newserv, but we can use it as a base
|
||||
// for PSOBB common item generation. Implement this.
|
||||
|
||||
// The ItemPT structure below describes the format of a single ItemPT entry
|
||||
// (which corresponds to a single difficulty, episode, section ID, and challenge
|
||||
// flag). ItemPT entries (within ItemPT.gsl) are named like this:
|
||||
// string_printf("ItemPT_%s%s%c%1d.rel",
|
||||
// (is_challenge ? "c" : ""),
|
||||
// (is_ep2 ? "l" : ""),
|
||||
// char_for_difficulty(difficulty), // One of "nhvu"
|
||||
// section_id);
|
||||
|
||||
template <typename IntT>
|
||||
struct Range {
|
||||
IntT low;
|
||||
IntT high;
|
||||
} __attribute__((packed));
|
||||
|
||||
// For GC, use be_uint16_t/be_uint32_t; for other platforms use the le variants
|
||||
template <typename U16T, U32T>
|
||||
struct ItemPT {
|
||||
// This data structure uses index probability tables in multiple places. An
|
||||
// index probability table is a table where each entry holds the probability
|
||||
// that that entry's index is used. For example, if the armor slot count
|
||||
// probability table contains [77, 17, 5, 1, 0], this means there is a 77%
|
||||
// chance of no slots, 17% chance of 1 slot, 5% chance of 2 slots, 1% chance
|
||||
// of 3 slots, and no chance of 4 slots. The values in index probability
|
||||
// tables do not have to add up to 100; the game sums all of them and chooses
|
||||
// a random number less than that maximum.
|
||||
|
||||
// The area (floor) number is used in many places as well. Unlike the normal
|
||||
// area numbers, which start with Pioneer 2, the area numbers in this
|
||||
// structure start with Forest 1, and boss areas are treated as the first area
|
||||
// of the next section (so De Rol Le has Mines 1 drops, for example). Final
|
||||
// boss areas are treated as the last non-boss area (so Dark Falz boxes are
|
||||
// like Ruins 3 boxes). We refer to these adjusted area numbers as (area - 1).
|
||||
|
||||
// This index probability table determines the types of non-rare weapons. The
|
||||
// indexes in this table correspond to the non-rare weapon types 01 through 0C
|
||||
// (Saber through Wand).
|
||||
/* 0000 */ parray<uint8_t, 0x0C> base_weapon_type_prob_table;
|
||||
|
||||
// This table specifies the base subtype for each weapon type. Negative values
|
||||
// here mean that the weapon cannot be found in the first N areas (so -2, for
|
||||
// example, means that the weapon never appears in Forest 1 or 2 at all).
|
||||
// Nonnegative values here mean the subtype can be found in all areas, and
|
||||
// specify the base subtype (usually in the range [0, 4]). The subtype of
|
||||
// weapon that actually appears depends on this value and a value from the
|
||||
// following table.
|
||||
/* 000C */ parray<int8_t, 0x0C> subtype_base_table;
|
||||
|
||||
// This table specifies how many areas each weapon subtype appears in. For
|
||||
// example, if Sword (subtype 02, which is index 1 in this table and the table
|
||||
// above) has a subtype base of -2 and a subtype area lneght of 4, then Sword
|
||||
// items can be found when area - 1 is 2, 3, 4, or 5 (Cave 1 through Mine 1),
|
||||
// and Gigush (the next sword subtype) can be found in Mine 1 through Ruins 3.
|
||||
/* 0018 */ parray<uint8_t, 0x0C> subtype_area_length_table;
|
||||
|
||||
// This index probability table specifies how likely each possible grind value
|
||||
// is. The table is indexed as [grind][subtype_area_index], where the subtype
|
||||
// area index is how many areas the player is beyond the first area in which
|
||||
// the subtype can first be found (clamped to [0, 3]). To continue the example
|
||||
// above, in Cave 3, subtype_area_index would be 2, since Swords can first be
|
||||
// found two areas earlier in Cave 1.
|
||||
// For example, this table could look like this:
|
||||
// [64 1E 19 14] // Chance of getting a grind +0
|
||||
// [00 1E 17 0F] // Chance of getting a grind +1
|
||||
// [00 14 14 0E] // Chance of getting a grind +2
|
||||
// ...
|
||||
// C1 C2 C3 M1 // (Episode 1 area values from the example, for reference)
|
||||
/* 0024 */ parray<parray<uint8_t, 4>, 9> grind_prob_tables;
|
||||
|
||||
// This array specifies the chance that a rare weapon will have each possible
|
||||
// bonus value. The table is indexed as [(bonus_value - 10 / 5)][spec], so the
|
||||
// first row refers the probability of getting a -10% bonus, the next row is
|
||||
// the chance of getting -5%, etc., all the way up to +100%. For non-rare
|
||||
// items, spec is determined randomly based on the following field; for rare
|
||||
// items, spec is always 5.
|
||||
/* 0048 */ parray<parray<U16T, 6>, 0x17> bonus_value_prob_tables;
|
||||
|
||||
// This array specifies the value of spec to be used in the above lookup for
|
||||
// non-rare items. This is NOT an index probability table; this is a direct
|
||||
// lookup with indexes [bonus_index][area - 1]. A value of 0xFF in any byte of
|
||||
// this array prevents any weapon from having a bonus in that slot.
|
||||
// For example, the array might look like this:
|
||||
// [00 00 00 01 01 01 01 02 02 02]
|
||||
// [FF FF FF 00 00 00 01 01 01 01]
|
||||
// [FF FF FF FF FF FF FF FF FF 00]
|
||||
// F1 F2 C1 C2 C3 M1 M2 R1 R2 R3 // (Episode 1 areas, for reference)
|
||||
// In this example, spec is 0, 1, or 2 in all cases where a weapon can have a
|
||||
// bonus. In Forest 1 and 2 and Cave 1, weapons may have at most one bonus; in
|
||||
// all other areas except Ruins 3, they can have at most two bonuses, and in
|
||||
// Ruins 3, they can have up to three bonuses.
|
||||
/* 015C */ parray<parray<uint8_t, 10>, 3> nonrare_bonus_prob_spec;
|
||||
|
||||
// This array specifies the chance that a weapon will have each bonus type.
|
||||
// The table is indexed as [bonus_type][area - 1] for non-rare items; for rare
|
||||
// items, a random value in the range [0, 9] is used instead of (area - 1).
|
||||
// For example, the table might look like this:
|
||||
// [46 46 3F 3E 3E 3D 3C 3C 3A 3A] // Chance of getting no bonus
|
||||
// [14 14 0A 0A 09 02 02 04 05 05] // Chance of getting Native bonus
|
||||
// [0A 0A 12 11 11 09 09 08 08 08] // Chance of getting A.Beast bonus
|
||||
// [00 00 09 0A 0B 13 12 08 09 09] // Chance of getting Machine bonus
|
||||
// [00 00 00 01 01 08 0A 13 13 13] // Chance of getting Dark bonus
|
||||
// [00 00 00 00 00 01 01 01 01 01] // Chance of getting Hit bonus
|
||||
// F1 F2 C1 C2 C3 M1 M2 R1 R2 R3 // (Episode 1 areas, for reference)
|
||||
/* 017A */ parray<parray<uint8_t, 10>, 6> bonus_type_prob_tables;
|
||||
|
||||
// This array (indexed by area - 1) specifies a multiplier of used in special
|
||||
// ability determination. It seems this uses the star values from ItemPMT, but
|
||||
// not yet clear exactly in what way.
|
||||
// TODO: Figure out exactly what this does. Anchor: 80106FEC
|
||||
/* 01B6 */ parray<uint8_t, 0x0A> special_mult;
|
||||
|
||||
// This array (indexed by area - 1) specifies the probability that any
|
||||
// non-rare weapon will have a special ability.
|
||||
/* 01C0 */ parray<uint8_t, 0x0A> special_percent;
|
||||
|
||||
// TODO: Figure out exactly how this table is used. Anchor: 80106D34
|
||||
/* 01CA */ parray<uint8_t, 0x05> armor_shield_type_index_prob_table;
|
||||
|
||||
// This index probability table specifies how common each possible slot count
|
||||
// is for armor drops.
|
||||
/* 01CF */ parray<uint8_t, 0x05> armor_slot_count_prob_table;
|
||||
|
||||
// These values specify maximum indexes into another array which is generated
|
||||
// at runtime. The values here are multiplied by a random float in the range
|
||||
// [0, n] to look up the value in the secondary array, which is what ends up
|
||||
// determining the unit type.
|
||||
// TODO: Figure out and document the exact logic here. Anchor: 80106364
|
||||
/* 01D4 */ parray<uint8_t, 0x0A> unit_maxes;
|
||||
|
||||
// This index probability table is indexed by [tool_class][area - 1]. The tool
|
||||
// class refers to an entry in ItemPMT, which links it to the actual item
|
||||
// code.
|
||||
/* 01DE */ parray<parray<U16T, 0x0A>, 0x1C> tool_class_prob_table;
|
||||
|
||||
// This index probability table determines how likely each technique is to
|
||||
// appear. The table is indexed as [technique_num][area - 1].
|
||||
/* 040E */ parray<parray<uint8_t, 0x0A> 0x13> technique_index_prob_table;
|
||||
|
||||
// This table specifies the ranges for technique disk levels. The table is
|
||||
// indexed as [technique_num][area - 1]. If either min or max in the range is
|
||||
// 0xFF, or if max < min, technique disks are not dropped for that technique
|
||||
// and area pair.
|
||||
/* 04CC */ parray<parray<Range<uint8_t>, 0x0A>, 0x13> technique_level_ranges;
|
||||
|
||||
// Each byte in this table (indexed by enemy_type) represents the percent
|
||||
// chance that the enemy drops anything at all. (This check is done after the
|
||||
// rare drop check, so it only applies to non-rare items.)
|
||||
/* 0648 */ parray<uint8_t, 0x64> enemy_type_drop_probs;
|
||||
|
||||
// This array (indexed by enemy_id) specifies the range of meseta values that
|
||||
// each enemy can drop.
|
||||
/* 06AC */ parray<Range<U16T>, 0x64> enemy_meseta_ranges;
|
||||
|
||||
// Each byte in this table (indexed by enemy_type) represents the class of
|
||||
// item that the enemy can drop. The values are:
|
||||
// 00 = weapon
|
||||
// 01 = armor
|
||||
// 02 = shield
|
||||
// 03 = unit
|
||||
// 04 = tool
|
||||
// 05 = meseta
|
||||
// Anything else = no item
|
||||
/* 083C */ parray<uint8_t, 0x64> enemy_item_classes;
|
||||
|
||||
// This table (indexed by area - 1) specifies the ranges of meseta values that
|
||||
// can drop from boxes.
|
||||
/* 08A0 */ parray<Range<U16T>, 0x0A> box_meseta_ranges;
|
||||
|
||||
// This index probability table determines which type of items drop from
|
||||
// boxes. The table is indexed as [item_class][area - 1], with item_class as
|
||||
// the result value (that is, in the example below, the game looks at a single
|
||||
// column and sums the values going down, then the chosen item class is one of
|
||||
// the row indexes based on the weight values in the column.) The resulting
|
||||
// item_class value has the same meaning as in enemy_item_classes above.
|
||||
// For example, this array might look like the following:
|
||||
// [07 07 08 08 06 07 08 09 09 0A] // Chances per area of a weapon drop
|
||||
// [02 02 02 02 03 02 02 02 03 03] // Chances per area of an armor drop
|
||||
// [02 02 02 02 03 02 02 02 03 03] // Chances per area of a shield drop
|
||||
// [00 00 03 03 03 04 03 04 05 05] // Chances per area of a unit drop
|
||||
// [11 11 12 12 12 12 12 12 12 12] // Chances per area of a tool drop
|
||||
// [32 32 32 32 32 32 32 32 32 32] // Chances per area of a meseta drop
|
||||
// [16 16 11 11 11 11 11 0F 0C 0B] // Chances per area of an empty box
|
||||
// F1 F2 C1 C2 C3 M1 M2 R1 R2 R3 // (Episode 1 areas, for reference)
|
||||
/* 08C8 */ parray<parray<uint8_t, 10>, 7> box_item_class_prob_tables;
|
||||
|
||||
/* 0910 */ U32T offset_table[0x1C];
|
||||
/* 0980 */ U16T unknown_f1[0x20];
|
||||
/* 09C0 */ U32T unknown_f1_offset;
|
||||
/* 09C4 */ U32T unknown_f2[3];
|
||||
/* 09D0 */ U32T offset_table_offset;
|
||||
/* 09D4 */ U32T unknown_f3[3];
|
||||
/* 09E0 (end of structure) */
|
||||
} __attribute__((packed));
|
||||
+592
-309
@@ -8,361 +8,644 @@
|
||||
|
||||
#include <phosg/Strings.hh>
|
||||
|
||||
#include "Text.hh"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
|
||||
struct prs_compress_ctx {
|
||||
uint8_t bitpos;
|
||||
std::string forward_log;
|
||||
std::string output;
|
||||
PRSCompressor::PRSCompressor(function<void(size_t, size_t)> progress_fn)
|
||||
: progress_fn(progress_fn),
|
||||
closed(false),
|
||||
control_byte_offset(0),
|
||||
pending_control_bits(0),
|
||||
input_bytes(0),
|
||||
compression_offset(0),
|
||||
reverse_log_index(0x100) {
|
||||
this->output.put_u8(0);
|
||||
}
|
||||
|
||||
prs_compress_ctx() : bitpos(0), forward_log("\0", 1) { }
|
||||
|
||||
string finish() {
|
||||
this->put_control_bit(0);
|
||||
this->put_control_bit(1);
|
||||
this->put_static_data(0);
|
||||
this->put_static_data(0);
|
||||
this->output += this->forward_log;
|
||||
this->forward_log.clear();
|
||||
return this->output;
|
||||
void PRSCompressor::add(const void* data, size_t size) {
|
||||
if (this->closed) {
|
||||
throw logic_error("compressor is closed");
|
||||
}
|
||||
|
||||
void put_control_bit_nosave(bool bit) {
|
||||
if (bit) {
|
||||
this->forward_log[0] |= 1 << this->bitpos;
|
||||
StringReader r(data, size);
|
||||
while (!r.eof()) {
|
||||
this->add_byte(r.get_u8());
|
||||
}
|
||||
}
|
||||
|
||||
void PRSCompressor::add(const string& data) {
|
||||
this->add(data.data(), data.size());
|
||||
}
|
||||
|
||||
void PRSCompressor::add_byte(uint8_t v) {
|
||||
if (this->compression_offset + 0x100 <= this->input_bytes) {
|
||||
this->advance();
|
||||
}
|
||||
this->forward_log[this->input_bytes & 0xFF] = v;
|
||||
this->input_bytes++;
|
||||
}
|
||||
|
||||
void PRSCompressor::advance() {
|
||||
// Search for a match in the decompressed data history
|
||||
size_t best_match_size = 0;
|
||||
size_t best_match_offset = 0;
|
||||
|
||||
uint8_t first_v = this->forward_log[this->compression_offset & 0xFF];
|
||||
const auto& start_offsets = this->reverse_log_index[first_v];
|
||||
|
||||
for (auto it = start_offsets.begin(); (it != start_offsets.end()) && (best_match_size < 0x100); it++) {
|
||||
size_t match_offset = *it;
|
||||
if (match_offset == this->compression_offset - 0x2000) {
|
||||
continue;
|
||||
}
|
||||
this->bitpos++;
|
||||
}
|
||||
|
||||
void put_control_save() {
|
||||
if (this->bitpos >= 8) {
|
||||
this->bitpos = 0;
|
||||
this->output += this->forward_log;
|
||||
this->forward_log.resize(1);
|
||||
this->forward_log[0] = 0;
|
||||
size_t match_size = 0;
|
||||
size_t match_loop_bytes = this->compression_offset - match_offset;
|
||||
while ((match_size < 0x100) &&
|
||||
(this->compression_offset + match_size < this->input_bytes) &&
|
||||
(this->reverse_log[(match_offset + (match_size % match_loop_bytes)) & 0x1FFF] == this->forward_log[(this->compression_offset + match_size) & 0xFF])) {
|
||||
match_size++;
|
||||
}
|
||||
|
||||
// If there are multiple matches of the longest length, use the latest one,
|
||||
// since it's more likely that it can be expressed as a short copy instead
|
||||
// of a long copy.
|
||||
if (match_size >= best_match_size) {
|
||||
best_match_offset = match_offset;
|
||||
best_match_size = match_size;
|
||||
}
|
||||
}
|
||||
|
||||
void put_control_bit(bool bit) {
|
||||
this->put_control_bit_nosave(bit);
|
||||
this->put_control_save();
|
||||
}
|
||||
// If there is a suitable match, write a backreference
|
||||
bool should_write_literal = false;
|
||||
size_t advance_bytes = 0;
|
||||
ssize_t backreference_offset = best_match_offset - this->compression_offset;
|
||||
if (best_match_size < 2) {
|
||||
should_write_literal = true;
|
||||
|
||||
void put_static_data(uint8_t data) {
|
||||
this->forward_log.push_back(static_cast<char>(data));
|
||||
}
|
||||
} else {
|
||||
// The backreference should be encoded:
|
||||
// - As a short copy if offset in [-0x100, -1] and size in [2, 5]
|
||||
// - As a long copy if offset in [-0x1FFF, -1] and size in [3, 9]
|
||||
// - As an extended copy if offset in [-0x1FFF, -1] and size in [10, 0x100]
|
||||
// Technically an extended copy can be used for sizes 1-9 as well, but if
|
||||
// size is 1 or 2, writing literals is better (since it uses fewer data
|
||||
// bytes and control bits), and a long copy can cover sizes 3-9 (and also
|
||||
// uses fewer data bytes and control bits).
|
||||
|
||||
void raw_byte(uint8_t value) {
|
||||
this->put_control_bit_nosave(1);
|
||||
this->put_static_data(value);
|
||||
this->put_control_save();
|
||||
}
|
||||
if ((backreference_offset >= -0x100) && (best_match_size <= 5)) {
|
||||
// Write short copy
|
||||
uint8_t size = best_match_size - 2;
|
||||
this->write_control(false);
|
||||
this->write_control(false);
|
||||
this->write_control(size & 2);
|
||||
this->write_control(size & 1);
|
||||
this->output.put_u8(backreference_offset & 0xFF);
|
||||
advance_bytes = best_match_size;
|
||||
|
||||
void short_copy(ssize_t offset, uint8_t size) {
|
||||
size -= 2;
|
||||
this->put_control_bit(0);
|
||||
this->put_control_bit(0);
|
||||
this->put_control_bit((size >> 1) & 1);
|
||||
this->put_control_bit_nosave(size & 1);
|
||||
this->put_static_data(offset & 0xFF);
|
||||
this->put_control_save();
|
||||
}
|
||||
} else if (best_match_size < 3) {
|
||||
// Can't use a long copy for size 2, and it's not worth it to use extended
|
||||
// copy for this either (as noted above)
|
||||
should_write_literal = true;
|
||||
|
||||
} else if ((backreference_offset >= -0x1FFF) && (best_match_size <= 9)) {
|
||||
// Write long copy
|
||||
this->write_control(false);
|
||||
this->write_control(true);
|
||||
uint16_t a = (backreference_offset << 3) | (best_match_size - 2);
|
||||
this->output.put_u8(a & 0xFF);
|
||||
this->output.put_u8(a >> 8);
|
||||
advance_bytes = best_match_size;
|
||||
|
||||
} else if ((backreference_offset >= -0x1FFF) && (best_match_size <= 0x100)) {
|
||||
// Write extended copy
|
||||
this->write_control(false);
|
||||
this->write_control(true);
|
||||
uint16_t a = (backreference_offset << 3);
|
||||
this->output.put_u8(a & 0xFF);
|
||||
this->output.put_u8(a >> 8);
|
||||
this->output.put_u8(best_match_size - 1);
|
||||
advance_bytes = best_match_size;
|
||||
|
||||
void long_copy(ssize_t offset, uint8_t size) {
|
||||
if (size <= 9) {
|
||||
this->put_control_bit(0);
|
||||
this->put_control_bit_nosave(1);
|
||||
this->put_static_data(((offset << 3) & 0xF8) | ((size - 2) & 0x07));
|
||||
this->put_static_data((offset >> 5) & 0xFF);
|
||||
this->put_control_save();
|
||||
} else {
|
||||
this->put_control_bit(0);
|
||||
this->put_control_bit_nosave(1);
|
||||
this->put_static_data((offset << 3) & 0xF8);
|
||||
this->put_static_data((offset >> 5) & 0xFF);
|
||||
this->put_static_data(size - 1);
|
||||
this->put_control_save();
|
||||
throw logic_error("invalid best match");
|
||||
}
|
||||
}
|
||||
|
||||
void copy(ssize_t offset, uint8_t size) {
|
||||
if ((offset > -0x100) && (size <= 5)) {
|
||||
this->short_copy(offset, size);
|
||||
} else {
|
||||
this->long_copy(offset, size);
|
||||
}
|
||||
if (should_write_literal) {
|
||||
this->write_control(true);
|
||||
this->output.put_u8(this->forward_log[this->compression_offset & 0xFF]);
|
||||
advance_bytes = 1;
|
||||
}
|
||||
|
||||
for (size_t z = 0; z < advance_bytes; z++) {
|
||||
if ((this->compression_offset & 0x1000) && this->progress_fn) {
|
||||
this->progress_fn(this->compression_offset, this->output.size());
|
||||
}
|
||||
size_t reverse_log_offset = this->compression_offset & 0x1FFF;
|
||||
uint8_t existing_v = this->reverse_log[reverse_log_offset];
|
||||
uint8_t new_v = this->forward_log[this->compression_offset & 0xFF];
|
||||
|
||||
if (this->compression_offset & (~0x1FFF)) {
|
||||
this->reverse_log_index[existing_v].pop_front();
|
||||
}
|
||||
this->reverse_log[reverse_log_offset] = new_v;
|
||||
this->reverse_log_index[new_v].emplace_back(this->compression_offset);
|
||||
this->compression_offset++;
|
||||
}
|
||||
}
|
||||
|
||||
string& PRSCompressor::close() {
|
||||
if (!this->closed) {
|
||||
// Advance until all input is consumed
|
||||
while (this->compression_offset < this->input_bytes) {
|
||||
this->advance();
|
||||
}
|
||||
// Write stop command
|
||||
this->write_control(false);
|
||||
this->write_control(true);
|
||||
this->output.put_u8(0);
|
||||
this->output.put_u8(0);
|
||||
// Write remaining control bits
|
||||
this->flush_control();
|
||||
this->closed = true;
|
||||
}
|
||||
return this->output.str();
|
||||
}
|
||||
|
||||
void PRSCompressor::write_control(bool z) {
|
||||
if (this->pending_control_bits & 0x0100) {
|
||||
this->output.pput_u8(
|
||||
this->control_byte_offset, this->pending_control_bits & 0xFF);
|
||||
this->control_byte_offset = this->output.size();
|
||||
this->output.put_u8(0);
|
||||
this->pending_control_bits = z ? 0x8080 : 0x8000;
|
||||
} else {
|
||||
this->pending_control_bits =
|
||||
(this->pending_control_bits >> 1) | (z ? 0x8080 : 0x8000);
|
||||
}
|
||||
}
|
||||
|
||||
void PRSCompressor::flush_control() {
|
||||
if (this->pending_control_bits & 0xFF00) {
|
||||
while (!(this->pending_control_bits & 0x0100)) {
|
||||
this->pending_control_bits >>= 1;
|
||||
}
|
||||
this->output.pput_u8(
|
||||
this->control_byte_offset, this->pending_control_bits & 0xFF);
|
||||
} else {
|
||||
if (this->control_byte_offset != this->output.size() - 1) {
|
||||
throw logic_error("data written without control bits");
|
||||
}
|
||||
this->output.str().resize(this->output.str().size() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
string prs_compress(
|
||||
const void* vdata, size_t size, function<void(size_t, size_t)> progress_fn) {
|
||||
PRSCompressor prs(progress_fn);
|
||||
prs.add(vdata, size);
|
||||
return move(prs.close());
|
||||
}
|
||||
|
||||
string prs_compress(
|
||||
const string& data, function<void(size_t, size_t)> progress_fn) {
|
||||
return prs_compress(data.data(), data.size(), progress_fn);
|
||||
}
|
||||
|
||||
|
||||
|
||||
class ControlStreamReader {
|
||||
public:
|
||||
ControlStreamReader(StringReader& r) : r(r), bits(0x0000) { }
|
||||
|
||||
bool read() {
|
||||
if (!(this->bits & 0x0100)) {
|
||||
this->bits = 0xFF00 | this->r.get_u8();
|
||||
}
|
||||
bool ret = this->bits & 1;
|
||||
this->bits >>= 1;
|
||||
return ret;
|
||||
}
|
||||
|
||||
private:
|
||||
StringReader& r;
|
||||
uint16_t bits;
|
||||
};
|
||||
|
||||
string prs_compress(const void* vdata, size_t size) {
|
||||
const uint8_t* data = reinterpret_cast<const uint8_t*>(vdata);
|
||||
prs_compress_ctx pc;
|
||||
string prs_decompress(const void* data, size_t size, size_t max_output_size) {
|
||||
// PRS is an LZ77-based compression algorithm. Compressed data is split into
|
||||
// two streams: a control stream and a data stream. The control stream is read
|
||||
// one bit at a time, and the data stream is read one byte at a time. The
|
||||
// streams are interleaved such that the decompressor never has to move
|
||||
// backward in the input stream - when the decompressor needs a control bit
|
||||
// and there are no unused bits from the previous byte of the control stream,
|
||||
// it reads a byte from the input and treats it as the next 8 control bits.
|
||||
|
||||
ssize_t data_ssize = static_cast<ssize_t>(size);
|
||||
ssize_t read_offset = 0;
|
||||
while (read_offset < data_ssize) {
|
||||
// There are 3 distinct commands in PRS, labeled here with their control bits:
|
||||
// 1 - Literal byte. The decompressor copies one byte from the input data
|
||||
// stream to the output.
|
||||
// 00 - Short backreference. The decompressor reads two control bits and adds
|
||||
// 2 to this value to determine the number of bytes to copy, then reads
|
||||
// one byte from the data stream to determine how far back in the output
|
||||
// to copy from. This byte is treated as an 8-bit negative number - so
|
||||
// 0xF7, for example, means to start copying data from 9 bytes before the
|
||||
// end of the output. The range must start before the end of the output,
|
||||
// but the end of the range may be beyond the end of the output. In this
|
||||
// case, the bytes between the beginning of the range and original end of
|
||||
// the output are simply repeated.
|
||||
// 01 - Long backreference. The decompressor reads two bytes from the data and
|
||||
// byteswaps the resulting 16-bit value (that is, the low byte is read
|
||||
// first). The start offset (again, as a negative number) is the top 13
|
||||
// bits of this value; the size is the low 3 bits of this value, plus 2.
|
||||
// If the size bits are all zero, an additional byte is read from the
|
||||
// data stream and 1 is added to it to determine the backreference size
|
||||
// (we call this an extended backreference). Therefore, the maximum
|
||||
// backreference size is 256 bytes.
|
||||
// Decompression ends when either there are no more input bytes to read, or
|
||||
// when a long backreference is read with all zeroes in its offset field. The
|
||||
// original implementation stops decompression successfully when any attempt
|
||||
// to read from the input encounters the end of the stream, but newserv's
|
||||
// implementation only allows this at the end of an opcode - if end-of-stream
|
||||
// is encountered partway through an opcode, we throw instead, because it's
|
||||
// likely the input has been truncated or is malformed in some way.
|
||||
|
||||
// look for a chunk of data in history matching what's at the current offset
|
||||
ssize_t best_offset = 0;
|
||||
ssize_t best_size = 0;
|
||||
for (ssize_t this_offset = -3; // min copy size is 3 bytes
|
||||
(this_offset + read_offset >= 0) && // don't go before the beginning
|
||||
(this_offset > -0x1FF0) && // max offset is -0x1FF0
|
||||
(best_size < 255); // max size is 0xFF bytes
|
||||
this_offset--) {
|
||||
StringWriter w;
|
||||
StringReader r(data, size);
|
||||
ControlStreamReader cr(r);
|
||||
|
||||
// for this offset, expand the match as much as possible
|
||||
ssize_t this_size = 1;
|
||||
while ((this_size < 0x100) && // max copy size is 255 bytes
|
||||
((this_offset + this_size) < 0) && // don't copy past the read offset
|
||||
(this_size <= data_ssize - read_offset) && // don't copy past the end
|
||||
!memcmp(data + read_offset + this_offset, data + read_offset,
|
||||
this_size)) {
|
||||
this_size++;
|
||||
}
|
||||
this_size--;
|
||||
|
||||
if (this_size > best_size) {
|
||||
best_offset = this_offset;
|
||||
best_size = this_size;
|
||||
}
|
||||
}
|
||||
|
||||
// if there are no good matches, write the byte directly
|
||||
if (best_size < 3) {
|
||||
pc.raw_byte(data[read_offset]);
|
||||
read_offset++;
|
||||
|
||||
} else {
|
||||
pc.copy(best_offset, best_size);
|
||||
read_offset += best_size;
|
||||
}
|
||||
}
|
||||
|
||||
return pc.finish();
|
||||
}
|
||||
|
||||
string prs_compress(const string& data) {
|
||||
return prs_compress(data.data(), data.size());
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int16_t get_u8_or_eof(StringReader& r) {
|
||||
return r.eof() ? -1 : r.get_u8();
|
||||
}
|
||||
|
||||
string prs_decompress(const string& data, size_t max_size) {
|
||||
string output;
|
||||
StringReader r(data.data(), data.size());
|
||||
|
||||
int32_t r3, r5;
|
||||
int bitpos = 9;
|
||||
int16_t currentbyte; // int16_t because it can be -1 when EOF occurs
|
||||
int flag;
|
||||
int offset;
|
||||
unsigned long x, t;
|
||||
|
||||
currentbyte = get_u8_or_eof(r);
|
||||
if (currentbyte == EOF) {
|
||||
return output;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
bitpos--;
|
||||
if (bitpos == 0) {
|
||||
currentbyte = get_u8_or_eof(r);
|
||||
if (currentbyte == EOF) {
|
||||
return output;
|
||||
}
|
||||
bitpos = 8;
|
||||
}
|
||||
flag = currentbyte & 1;
|
||||
currentbyte = currentbyte >> 1;
|
||||
if (flag) {
|
||||
int ch = get_u8_or_eof(r);
|
||||
if (ch == EOF) {
|
||||
return output;
|
||||
}
|
||||
output += static_cast<char>(ch);
|
||||
if (max_size && (output.size() > max_size)) {
|
||||
while (!r.eof()) {
|
||||
// Control 1 = literal byte
|
||||
if (cr.read()) {
|
||||
if (max_output_size && w.size() == max_output_size) {
|
||||
throw runtime_error("maximum output size exceeded");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
bitpos--;
|
||||
if (bitpos == 0) {
|
||||
currentbyte = get_u8_or_eof(r);
|
||||
if (currentbyte == EOF) {
|
||||
return output;
|
||||
}
|
||||
bitpos = 8;
|
||||
}
|
||||
flag = currentbyte & 1;
|
||||
currentbyte = currentbyte >> 1;
|
||||
if (flag) {
|
||||
r3 = get_u8_or_eof(r);
|
||||
if (r3 == EOF) {
|
||||
return output;
|
||||
}
|
||||
int high_byte = get_u8_or_eof(r);
|
||||
if (high_byte == EOF) {
|
||||
return output;
|
||||
}
|
||||
offset = ((high_byte & 0xFF) << 8) | (r3 & 0xFF);
|
||||
if (offset == 0) {
|
||||
return output;
|
||||
}
|
||||
r3 = r3 & 0x00000007;
|
||||
r5 = (offset >> 3) | 0xFFFFE000;
|
||||
if (r3 == 0) {
|
||||
flag = 0;
|
||||
r3 = get_u8_or_eof(r);
|
||||
if (r3 == EOF) {
|
||||
return output;
|
||||
w.put_u8(r.get_u8());
|
||||
|
||||
} else {
|
||||
ssize_t offset;
|
||||
size_t count;
|
||||
|
||||
// Control 01 = long backreference
|
||||
if (cr.read()) {
|
||||
// The bits stored in the data stream are AAAAABBBCCCCCCCC, which we
|
||||
// rearrange into offset = CCCCCCCCAAAAA and size = BBB.
|
||||
uint16_t a = r.get_u8();
|
||||
a |= (r.get_u8() << 8);
|
||||
offset = (a >> 3) | (~0x1FFF);
|
||||
// If offset is zero, it's a stop opcode
|
||||
if (offset == ~0x1FFF) {
|
||||
break;
|
||||
}
|
||||
r3 = (r3 & 0xFF) + 1;
|
||||
// If the size field is zero, it's an extended backreference (size comes
|
||||
// from another byte in the data stream)
|
||||
count = (a & 7) ? ((a & 7) + 2) : (r.get_u8() + 1);
|
||||
|
||||
// Control 00 = short backreference
|
||||
} else {
|
||||
r3 += 2;
|
||||
// Count comes from 2 bits in the control stream instead of from the
|
||||
// data stream (and 2 is added). Importantly, the control stream bits
|
||||
// are read first - this may involve reading another control stream
|
||||
// byte, which happens before the offset is read from the data stream.
|
||||
count = cr.read() << 1;
|
||||
count = (count | cr.read()) + 2;
|
||||
offset = r.get_u8() | (~0xFF);
|
||||
}
|
||||
} else {
|
||||
r3 = 0;
|
||||
for (x = 0; x < 2; x++) {
|
||||
bitpos--;
|
||||
if (bitpos == 0) {
|
||||
currentbyte = get_u8_or_eof(r);
|
||||
if (currentbyte == EOF) {
|
||||
return output;
|
||||
}
|
||||
bitpos = 8;
|
||||
|
||||
// Copy bytes from the referenced location in the output. Importantly,
|
||||
// copy only one byte at a time, in order to support ranges that cover the
|
||||
// current end of the output.
|
||||
size_t read_offset = w.size() + offset;
|
||||
if (read_offset >= w.size()) {
|
||||
throw runtime_error("backreference offset beyond beginning of output");
|
||||
}
|
||||
for (size_t z = 0; z < count; z++) {
|
||||
if (max_output_size && w.size() == max_output_size) {
|
||||
throw runtime_error("maximum output size exceeded");
|
||||
}
|
||||
flag = currentbyte & 1;
|
||||
currentbyte = currentbyte >> 1;
|
||||
offset = r3 << 1;
|
||||
r3 = offset | flag;
|
||||
}
|
||||
offset = get_u8_or_eof(r);
|
||||
if (offset == EOF) {
|
||||
return output;
|
||||
}
|
||||
r3 += 2;
|
||||
r5 = offset | 0xFFFFFF00;
|
||||
}
|
||||
if (r3 == 0) {
|
||||
continue;
|
||||
}
|
||||
t = r3;
|
||||
for (x = 0; x < t; x++) {
|
||||
output += output.at(output.size() + r5);
|
||||
if (max_size && (output.size() > max_size)) {
|
||||
throw runtime_error("maximum output size exceeded");
|
||||
w.put_u8(w.str()[read_offset + z]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return move(w.str());
|
||||
}
|
||||
|
||||
size_t prs_decompress_size(const string& data, size_t max_size) {
|
||||
size_t output_size = 0;
|
||||
StringReader r(data.data(), data.size());
|
||||
string prs_decompress(const string& data, size_t max_output_size) {
|
||||
return prs_decompress(data.data(), data.size(), max_output_size);
|
||||
}
|
||||
|
||||
int32_t r3;
|
||||
int bitpos = 9;
|
||||
int16_t currentbyte; // int16_t because it can be -1 when EOF occurs
|
||||
int flag;
|
||||
int offset;
|
||||
unsigned long x;
|
||||
size_t prs_decompress_size(const void* data, size_t size, size_t max_output_size) {
|
||||
size_t ret = 0;
|
||||
StringReader r(data, size);
|
||||
ControlStreamReader cr(r);
|
||||
|
||||
currentbyte = get_u8_or_eof(r);
|
||||
if (currentbyte == EOF) {
|
||||
return output_size;
|
||||
}
|
||||
while (!r.eof()) {
|
||||
if (cr.read()) {
|
||||
ret++;
|
||||
r.get_u8();
|
||||
|
||||
for (;;) {
|
||||
bitpos--;
|
||||
if (bitpos == 0) {
|
||||
currentbyte = get_u8_or_eof(r);
|
||||
if (currentbyte == EOF) {
|
||||
return output_size;
|
||||
}
|
||||
bitpos = 8;
|
||||
}
|
||||
flag = currentbyte & 1;
|
||||
currentbyte = currentbyte >> 1;
|
||||
if (flag) {
|
||||
int ch = get_u8_or_eof(r);
|
||||
if (ch == EOF) {
|
||||
return output_size;
|
||||
}
|
||||
output_size++;
|
||||
if (max_size && (output_size > max_size)) {
|
||||
throw runtime_error("maximum output size exceeded");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
bitpos--;
|
||||
if (bitpos == 0) {
|
||||
currentbyte = get_u8_or_eof(r);
|
||||
if (currentbyte == EOF) {
|
||||
return output_size;
|
||||
}
|
||||
bitpos = 8;
|
||||
}
|
||||
flag = currentbyte & 1;
|
||||
currentbyte = currentbyte >> 1;
|
||||
if (flag) {
|
||||
r3 = get_u8_or_eof(r);
|
||||
if (r3 == EOF) {
|
||||
return output_size;
|
||||
}
|
||||
int high_byte = get_u8_or_eof(r);
|
||||
if (high_byte == EOF) {
|
||||
return output_size;
|
||||
}
|
||||
offset = ((high_byte & 0xFF) << 8) | (r3 & 0xFF);
|
||||
if (offset == 0) {
|
||||
return output_size;
|
||||
}
|
||||
r3 = r3 & 0x00000007;
|
||||
if (r3 == 0) {
|
||||
flag = 0;
|
||||
r3 = get_u8_or_eof(r);
|
||||
if (r3 == EOF) {
|
||||
return output_size;
|
||||
}
|
||||
r3 = (r3 & 0xFF) + 1;
|
||||
} else {
|
||||
r3 += 2;
|
||||
}
|
||||
} else {
|
||||
r3 = 0;
|
||||
for (x = 0; x < 2; x++) {
|
||||
bitpos--;
|
||||
if (bitpos == 0) {
|
||||
currentbyte = get_u8_or_eof(r);
|
||||
if (currentbyte == EOF) {
|
||||
return output_size;
|
||||
}
|
||||
bitpos = 8;
|
||||
ssize_t offset;
|
||||
size_t count;
|
||||
|
||||
if (cr.read()) {
|
||||
uint16_t a = r.get_u8();
|
||||
a |= (r.get_u8() << 8);
|
||||
offset = (a >> 3) | (~0x1FFF);
|
||||
if (offset == ~0x1FFF) {
|
||||
break;
|
||||
}
|
||||
flag = currentbyte & 1;
|
||||
currentbyte = currentbyte >> 1;
|
||||
offset = r3 << 1;
|
||||
r3 = offset | flag;
|
||||
count = (a & 7) ? ((a & 7) + 2) : (r.get_u8() + 1);
|
||||
|
||||
} else {
|
||||
count = cr.read() << 1;
|
||||
count = (count | cr.read()) + 2;
|
||||
offset = r.get_u8() | (~0xFF);
|
||||
}
|
||||
offset = get_u8_or_eof(r);
|
||||
if (offset == EOF) {
|
||||
return output_size;
|
||||
|
||||
size_t read_offset = ret + offset;
|
||||
if (read_offset >= ret) {
|
||||
throw runtime_error("backreference offset beyond beginning of output");
|
||||
}
|
||||
r3 += 2;
|
||||
ret += count;
|
||||
}
|
||||
if (r3 == 0) {
|
||||
continue;
|
||||
}
|
||||
output_size += r3;
|
||||
if (max_size && (output_size > max_size)) {
|
||||
|
||||
if (max_output_size && ret > max_output_size) {
|
||||
throw runtime_error("maximum output size exceeded");
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
size_t prs_decompress_size(const string& data, size_t max_output_size) {
|
||||
return prs_decompress_size(data.data(), data.size(), max_output_size);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void prs_disassemble(FILE* stream, const void* data, size_t size) {
|
||||
size_t output_bytes = 0;
|
||||
StringReader r(data, size);
|
||||
ControlStreamReader cr(r);
|
||||
|
||||
while (!r.eof()) {
|
||||
size_t r_offset = r.where();
|
||||
if (cr.read()) {
|
||||
fprintf(stream, "[%zX => %zX] literal %02hhX\n", r_offset, output_bytes, r.get_u8());
|
||||
output_bytes++;
|
||||
|
||||
} else {
|
||||
ssize_t offset;
|
||||
size_t count;
|
||||
|
||||
bool is_long_copy = cr.read();
|
||||
if (is_long_copy) {
|
||||
uint16_t a = r.get_u8();
|
||||
a |= (r.get_u8() << 8);
|
||||
offset = (a >> 3) | (~0x1FFF);
|
||||
if (offset == ~0x1FFF) {
|
||||
fprintf(stream, "[%zX => %zX] end\n", r_offset, output_bytes);
|
||||
break;
|
||||
}
|
||||
count = (a & 7) ? ((a & 7) + 2) : (r.get_u8() + 1);
|
||||
|
||||
} else {
|
||||
count = cr.read() << 1;
|
||||
count = (count | cr.read()) + 2;
|
||||
offset = r.get_u8() | (~0xFF);
|
||||
}
|
||||
|
||||
size_t read_offset = output_bytes + offset;
|
||||
fprintf(stream, "[%zX => %zX] %s copy -%zX (from %zX) %zX\n",
|
||||
r_offset, output_bytes, is_long_copy ? "long" : "short",
|
||||
-offset, read_offset, count);
|
||||
|
||||
if (read_offset >= output_bytes) {
|
||||
throw runtime_error("backreference offset beyond beginning of output");
|
||||
}
|
||||
output_bytes += count;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void prs_disassemble(FILE* stream, const std::string& data) {
|
||||
return prs_disassemble(stream, data.data(), data.size());
|
||||
}
|
||||
|
||||
|
||||
|
||||
// BC0 is a compression algorithm fairly similar to PRS, but with a simpler set
|
||||
// of commands. Like PRS, there is a control stream, indicating when to copy a
|
||||
// literal byte from the input and when to copy from a backreference; unlike
|
||||
// PRS, there is only one type of backreference. Also, there is no stop opcode;
|
||||
// the decompressor simply stops when there are no more input bytes to read.
|
||||
|
||||
string bc0_compress(
|
||||
const string& data, function<void(size_t, size_t)> progress_fn) {
|
||||
StringReader r(data);
|
||||
StringWriter w;
|
||||
|
||||
parray<uint8_t, 0x1000> memo;
|
||||
uint16_t memo_offset = 0x0FEE;
|
||||
vector<deque<size_t>> memo_index(0x100);
|
||||
auto write_memo = [&](uint8_t new_v) -> void {
|
||||
uint8_t existing_v = memo[memo_offset];
|
||||
if (existing_v != new_v) {
|
||||
if (!memo_index[existing_v].empty()) {
|
||||
memo_index[existing_v].pop_front();
|
||||
}
|
||||
memo[memo_offset] = new_v;
|
||||
memo_index[new_v].emplace_back(memo_offset);
|
||||
}
|
||||
memo_offset = (memo_offset + 1) & 0xFFF;
|
||||
};
|
||||
|
||||
size_t next_control_byte_offset = w.size();
|
||||
w.put_u8(0);
|
||||
uint16_t pending_control_bits = 0x0000;
|
||||
|
||||
parray<uint8_t, 18> match_buf;
|
||||
while (!r.eof()) {
|
||||
if ((r.where() & 0x1000) && progress_fn) {
|
||||
progress_fn(r.where(), w.size());
|
||||
}
|
||||
|
||||
// Search in the memo for the longest string matching the upcoming data, of
|
||||
// size 3-18 bytes
|
||||
size_t best_match_offset = 0;
|
||||
size_t best_match_size = 0;
|
||||
size_t max_match_size = min<size_t>(r.remaining(), 18);
|
||||
const uint8_t* match_buf = &r.get<uint8_t>(false, max_match_size);
|
||||
|
||||
for (size_t match_size = 3; match_size <= max_match_size; match_size++) {
|
||||
for (size_t offset : memo_index[match_buf[0]]) {
|
||||
// Forbid matches that span the memo boundary - during decompression,
|
||||
// the client will be overwriting its memo while reading from it and
|
||||
// will likely generate incorrect data
|
||||
// TODO: We can actually support this (and it will improve compression),
|
||||
// but we have to set a loop boundary like we have in prs_compress and
|
||||
// I'm lazy.
|
||||
size_t start_memo_offset = offset;
|
||||
size_t end_memo_offset = (offset + match_size) & 0xFFF;
|
||||
if (end_memo_offset < start_memo_offset) {
|
||||
if ((memo_offset < end_memo_offset) || (memo_offset > start_memo_offset)) {
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
if ((memo_offset > start_memo_offset) && (memo_offset < end_memo_offset)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Note: We don't have to explicitly forbid matches that span the
|
||||
// uninitialized part of the memo (during the first 0x12 bytes) because
|
||||
// the preceding check will catch those too (and there can't be any
|
||||
// start offsets in the memo index within that region anyway).
|
||||
|
||||
bool match_found = true;
|
||||
for (size_t z = 0; z < match_size; z++) {
|
||||
if (match_buf[z] != memo[(offset + z) & 0xFFF]) {
|
||||
match_found = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// If a match was found at this size, don't bother looking for other
|
||||
// matches of the same size
|
||||
if (match_found) {
|
||||
best_match_size = match_size;
|
||||
best_match_offset = offset;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// If no matches were found at the current size, don't bother looking for
|
||||
// longer matches
|
||||
if (best_match_size < match_size) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Write a backreference if a match was found; otherwise, write a literal
|
||||
if (best_match_size >= 3) {
|
||||
pending_control_bits = (pending_control_bits >> 1) | 0x8000;
|
||||
w.put_u8(best_match_offset & 0xFF); // a1
|
||||
w.put_u8(((best_match_offset >> 4) & 0xF0) | (best_match_size - 3)); // a2
|
||||
for (size_t z = 0; z < best_match_size; z++) {
|
||||
write_memo(r.get_u8());
|
||||
}
|
||||
} else {
|
||||
pending_control_bits = (pending_control_bits >> 1) | 0x8080;
|
||||
uint8_t v = r.get_u8();
|
||||
w.put_u8(v);
|
||||
write_memo(v);
|
||||
}
|
||||
|
||||
// Write the control byte to the output if needed, and reserve space for the
|
||||
// next one
|
||||
if (pending_control_bits & 0x0100) {
|
||||
w.pput_u8(next_control_byte_offset, pending_control_bits & 0xFF);
|
||||
next_control_byte_offset = w.size();
|
||||
w.put_u8(0);
|
||||
pending_control_bits = 0x0000;
|
||||
}
|
||||
}
|
||||
|
||||
// Write the final control byte to the output if needed. If not needed, then
|
||||
// there should be an empty reserved space at the end; delete it since none of
|
||||
// its bits will be used.
|
||||
if (pending_control_bits & 0xFF00) {
|
||||
while (!(pending_control_bits & 0x0100)) {
|
||||
pending_control_bits >>= 1;
|
||||
}
|
||||
w.pput_u8(next_control_byte_offset, pending_control_bits & 0xFF);
|
||||
} else {
|
||||
if (next_control_byte_offset != w.size() - 1) {
|
||||
throw logic_error("data written without control bits");
|
||||
}
|
||||
w.str().resize(w.str().size() - 1);
|
||||
}
|
||||
|
||||
return move(w.str());
|
||||
}
|
||||
|
||||
// The BC0 decompression implementation in PSO GC is vulnerable to overflow
|
||||
// attacks - there is no bounds checking on the output buffer. It is unlikely
|
||||
// that this can be usefully exploited (e.g. for RCE) because the output pointer
|
||||
// is loaded from memory before every byte is written, so we cannot change the
|
||||
// output pointer to any arbitrary address.
|
||||
|
||||
string bc0_decompress(const string& data) {
|
||||
StringReader r(data);
|
||||
StringWriter w;
|
||||
|
||||
// Unlike PRS, BC0 uses a memo which "rolls over" every 0x1000 bytes. The
|
||||
// boundaries of these "memo pages" are offset by -0x12 bytes for some reason,
|
||||
// so the first output byte corresponds to position 0xFEE on the first memo
|
||||
// page. Backreferences refer to offsets based on the start of memo pages; for
|
||||
// example, if the current output offset is 0x1234, a backreference with
|
||||
// offset 0x123 refers to the byte that was written at offset 0x1112 (because
|
||||
// that byte is at offset 0x112 in the memo, because the memo rolls over every
|
||||
// 0x1000 bytes and the first memo byte was 0x12 bytes before the beginning of
|
||||
// the next page). The memo is initially zeroed from 0 to 0xFEE; it seems PSO
|
||||
// GC doesn't initialize the last 0x12 bytes of the first memo page. For this
|
||||
// reason, we avoid generating backreferences that refer to those bytes.
|
||||
parray<uint8_t, 0x1000> memo;
|
||||
uint16_t memo_offset = 0x0FEE;
|
||||
|
||||
// The low byte of this value contains the control stream data; the high bits
|
||||
// specify which low bits are valid. When the last 1 is shifted out of the
|
||||
// high bit, we need to read a new control stream byte to get the next set of
|
||||
// control bits.
|
||||
uint16_t control_stream_bits = 0x0000;
|
||||
|
||||
while (!r.eof()) {
|
||||
// Read control stream bits if needed
|
||||
control_stream_bits >>= 1;
|
||||
if ((control_stream_bits & 0x100) == 0) {
|
||||
control_stream_bits = 0xFF00 | r.get_u8();
|
||||
if (r.eof()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Control bit 0 means to perform a backreference copy. The offset and
|
||||
// size are stored in two bytes in the input stream, laid out as follows:
|
||||
// a1 = 0bBBBBBBBB
|
||||
// a2 = 0bAAAACCCC
|
||||
// The offset is the concatenation of bits AAAABBBBBBBB, which refers to a
|
||||
// position in the memo; the number of bytes to copy is (CCCC + 3). The
|
||||
// decompressor copies that many bytes from that offset in the memo, and
|
||||
// writes them to the output and to the current position in the memo.
|
||||
if ((control_stream_bits & 1) == 0) {
|
||||
uint8_t a1 = r.get_u8();
|
||||
if (r.eof()) {
|
||||
break;
|
||||
}
|
||||
uint8_t a2 = r.get_u8();
|
||||
size_t count = (a2 & 0x0F) + 3;
|
||||
size_t backreference_offset = a1 | ((a2 << 4) & 0xF00);
|
||||
for (size_t z = 0; z < count; z++) {
|
||||
uint8_t v = memo[(backreference_offset + z) & 0x0FFF];
|
||||
w.put_u8(v);
|
||||
memo[memo_offset] = v;
|
||||
memo_offset = (memo_offset + 1) & 0x0FFF;
|
||||
}
|
||||
|
||||
// Control bit 1 means to write a byte directly from the input to the
|
||||
// output. As above, the byte is also written to the memo.
|
||||
} else {
|
||||
uint8_t v = r.get_u8();
|
||||
w.put_u8(v);
|
||||
memo[memo_offset] = v;
|
||||
memo_offset = (memo_offset + 1) & 0x0FFF;
|
||||
}
|
||||
}
|
||||
|
||||
return move(w.str());
|
||||
}
|
||||
|
||||
+71
-4
@@ -3,11 +3,78 @@
|
||||
#include <stddef.h>
|
||||
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include <deque>
|
||||
|
||||
#include "Text.hh"
|
||||
|
||||
|
||||
|
||||
std::string prs_compress(const void* vdata, size_t size);
|
||||
std::string prs_compress(const std::string& data);
|
||||
// Use this class if you need to compress from multiple input buffers, or need
|
||||
// to compress multiple chunks and don't want to copy their contents
|
||||
// unnecessarily. (For most common use cases, use prs_compress (below) instead.)
|
||||
class PRSCompressor {
|
||||
public:
|
||||
// To use this class, instantiate it, then call .add() one or more times, then
|
||||
// call .close() and use the returned string as the compressed result.
|
||||
PRSCompressor(std::function<void(size_t, size_t)> progress_fn = nullptr);
|
||||
~PRSCompressor() = default;
|
||||
|
||||
std::string prs_decompress(const std::string& data, size_t max_size = 0);
|
||||
size_t prs_decompress_size(const std::string& data, size_t max_size = 0);
|
||||
// Adds more input data to be compressed, which logically comes after all
|
||||
// previous data provided via add() calls. Cannot be called after close() is
|
||||
// called.
|
||||
void add(const void* data, size_t size);
|
||||
void add(const std::string& data);
|
||||
|
||||
// Ends compression and returns the complete compressed result. It's OK to
|
||||
// std::move() from the returned string reference.
|
||||
std::string& close();
|
||||
|
||||
// Returns the total number of bytes passed to add() calls so far.
|
||||
inline size_t input_size() const {
|
||||
return this->input_bytes;
|
||||
}
|
||||
|
||||
private:
|
||||
void add_byte(uint8_t v);
|
||||
void advance();
|
||||
void write_control(bool z);
|
||||
void flush_control();
|
||||
|
||||
std::function<void(size_t, size_t)> progress_fn;
|
||||
bool closed;
|
||||
|
||||
size_t control_byte_offset;
|
||||
uint16_t pending_control_bits;
|
||||
|
||||
size_t input_bytes;
|
||||
parray<uint8_t, 0x100> forward_log;
|
||||
size_t compression_offset;
|
||||
parray<uint8_t, 0x2000> reverse_log;
|
||||
std::vector<std::deque<size_t>> reverse_log_index;
|
||||
|
||||
StringWriter output;
|
||||
};
|
||||
|
||||
// Compresses data from a single input buffer using PRS and returns the
|
||||
// compressed result. This is a shortcut for constructing a PRSCompressor,
|
||||
// calling .add() once, and calling .close().
|
||||
std::string prs_compress(const void* vdata, size_t size, std::function<void(size_t, size_t)> progress_fn = nullptr);
|
||||
std::string prs_compress(const std::string& data, std::function<void(size_t, size_t)> progress_fn = nullptr);
|
||||
|
||||
// Decompresses PRS-compressed data.
|
||||
std::string prs_decompress(const void* data, size_t size, size_t max_output_size = 0);
|
||||
std::string prs_decompress(const std::string& data, size_t max_output_size = 0);
|
||||
|
||||
// Returns the decompressed size of PRS-compressed data, without actually
|
||||
// decompressing it.
|
||||
size_t prs_decompress_size(const void* data, size_t size, size_t max_output_size = 0);
|
||||
size_t prs_decompress_size(const std::string& data, size_t max_output_size = 0);
|
||||
|
||||
// Prints the command stream from a PRS-compressed buffer.
|
||||
void prs_disassemble(FILE* stream, const void* data, size_t size);
|
||||
void prs_disassemble(FILE* stream, const std::string& data);
|
||||
|
||||
// Compresses and decompresses data using the BC0 algorithm.
|
||||
std::string bc0_compress(const std::string& data, std::function<void(size_t, size_t)> progress_fn = nullptr);
|
||||
std::string bc0_decompress(const std::string& data);
|
||||
|
||||
+3
-2
@@ -12,6 +12,7 @@
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#include "Loggers.hh"
|
||||
#include "NetworkAddresses.hh"
|
||||
|
||||
using namespace std;
|
||||
@@ -91,7 +92,7 @@ void DNSServer::on_receive_message(int fd, short) {
|
||||
|
||||
if (bytes < 0) {
|
||||
if (errno != EAGAIN) {
|
||||
log(INFO, "[DNSServer] input error %d", errno);
|
||||
dns_server_log.error("input error %d", errno);
|
||||
throw runtime_error("cannot read from udp socket");
|
||||
}
|
||||
break;
|
||||
@@ -100,7 +101,7 @@ void DNSServer::on_receive_message(int fd, short) {
|
||||
break;
|
||||
|
||||
} else if (bytes < 0x0C) {
|
||||
log(WARNING, "[DNSServer] input query too small");
|
||||
dns_server_log.warning("input query too small");
|
||||
print_data(stderr, input.data(), bytes);
|
||||
|
||||
} else {
|
||||
|
||||
-894
@@ -1,894 +0,0 @@
|
||||
#include "Episode3.hh"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <array>
|
||||
#include <phosg/Filesystem.hh>
|
||||
|
||||
#include "Compression.hh"
|
||||
#include "Text.hh"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
|
||||
static const vector<const char*> name_for_card_type({
|
||||
"HunterSC",
|
||||
"ArkzSC",
|
||||
"Item",
|
||||
"Creature",
|
||||
"Action",
|
||||
"Assist",
|
||||
});
|
||||
|
||||
static const unordered_map<uint8_t, const char*> description_for_when({
|
||||
{0x01, "Set"}, // TODO: Is 01 this, or "Permanent"?
|
||||
{0x02, "Attack"},
|
||||
{0x03, "??? (TODO)"},
|
||||
{0x04, "Before turn"},
|
||||
{0x05, "Destroyed"},
|
||||
{0x0A, "Permanent"}, // only used on Tollaw; could be same as 01
|
||||
{0x0B, "Battle"},
|
||||
{0x0C, "Opponent destroyed"}, // TODO: but this is also used for some support things like Shifta, and for Snatch, which also applies when opponents are not destroyed
|
||||
{0x0D, "Attack lands"},
|
||||
{0x0E, "Before attack phase"},
|
||||
{0x16, "Battle end"},
|
||||
{0x17, "Each defense"},
|
||||
{0x20, "Each attack"},
|
||||
{0x22, "Act phase"},
|
||||
{0x27, "Move phase"},
|
||||
{0x29, "Set and act phases"},
|
||||
{0x33, "Defense phase"},
|
||||
{0x3D, "Battle"}, // TODO: how is this different from 3D and 0B?
|
||||
{0x3E, "Battle"}, // TODO: how is this different from 3D and 0B?
|
||||
{0x3F, "Each defense"}, // TODO: how is this different from 17?
|
||||
{0x46, "On specific turn"},
|
||||
});
|
||||
|
||||
static const unordered_map<string, const char*> description_for_expr_token({
|
||||
{"f", "Number of FCs controlled by current SC"},
|
||||
{"d", "Die roll"},
|
||||
{"ap", "Attacker AP"}, // Unused
|
||||
{"tp", "Attacker TP"},
|
||||
{"hp", "Attacker HP"}, // TODO: How is this different from ehp?
|
||||
{"mhp", "Attacker maximum HP"},
|
||||
{"dm", "Unknown: dm"}, // Unused
|
||||
{"tdm", "Unknown: tdm"}, // Unused
|
||||
{"tf", "Number of SC\'s destroyed FCs"},
|
||||
{"ac", "Remaining ATK points"},
|
||||
{"php", "Unknown: php"}, // Unused
|
||||
{"dc", "Unknown: dc"}, // Unused
|
||||
{"cs", "Unknown: cs"}, // Unused
|
||||
{"a", "Unknown: a"}, // Unused
|
||||
{"kap", "Action cards AP"},
|
||||
{"ktp", "Action cards TP"},
|
||||
{"dn", "Unknown: dn"}, // Unused
|
||||
{"hf", "Unknown: hf"}, // Unused
|
||||
{"df", "Number of destroyed ally FCs (including SC\'s own)"},
|
||||
{"ff", "Number of ally FCs (including SC\'s own)"},
|
||||
{"ef", "Number of enemy FCs"},
|
||||
{"bi", "Number of Native FCs on either team"},
|
||||
{"ab", "Number of A.Beast FCs on either team"},
|
||||
{"mc", "Number of Machine FCs on either team"},
|
||||
{"dk", "Number of Dark FCs on either team"},
|
||||
{"sa", "Number of Sword-type items on either team"},
|
||||
{"gn", "Number of Gun-type items on either team"},
|
||||
{"wd", "Number of Cane-type items on either team"},
|
||||
{"tt", "Unknown: tt"}, // Unused
|
||||
{"lv", "Dice bonus"},
|
||||
{"adm", "Attack damage"},
|
||||
{"ddm", "Defending damage"},
|
||||
{"sat", "Number of Sword-type items on SC\'s team"},
|
||||
{"edm", "Defending damage"}, // TODO: How is this different from ddm?
|
||||
{"ldm", "Unknown: ldm"}, // Unused
|
||||
{"rdm", "Defending damage"}, // TODO: How is this different from ddm/edm?
|
||||
{"fdm", "Final damage (after defense)"},
|
||||
{"ndm", "Unknown: ndm"}, // Unused
|
||||
{"ehp", "Attacker HP"},
|
||||
});
|
||||
|
||||
// Arguments are encoded as 3-character null-terminated strings (why?!), and are
|
||||
// used for adding conditions to effects (e.g. making them only trigger in
|
||||
// certain situations) or otherwise customizing their results.
|
||||
// Argument meanings:
|
||||
// a01 = ???
|
||||
// cXY/CXY = linked items (require item with cYX/CYX to be equipped as well)
|
||||
// dXY = roll one die; require result between X and Y inclusive
|
||||
// e00 = effect lasts while equipped? (in contrast to tXX)
|
||||
// hXX = require HP >= XX
|
||||
// iXX = require HP <= XX
|
||||
// nXX = require condition XX (see description_for_n_condition)
|
||||
// oXX = seems to be "require previous random-condition effect to have happened"
|
||||
// TODO: this is used as both o01 (recovery) and o11 (reflection)
|
||||
// conditions - why the difference?
|
||||
// pXX = who to target (see description_for_p_target)
|
||||
// rXX = randomly pass with XX% chance (if XX == 00, 100% chance?)
|
||||
// sXY = require card cost between X and Y ATK points (inclusive)
|
||||
// tXX = lasts XX turns, or activate after XX turns
|
||||
|
||||
static const vector<const char*> description_for_n_condition({
|
||||
/* n00 */ "Always true",
|
||||
/* n01 */ "??? (TODO)",
|
||||
/* n02 */ "Destroyed with a single attack?",
|
||||
/* n03 */ "Unknown", // Unused
|
||||
/* n04 */ "Attack has Pierce",
|
||||
/* n05 */ "Attack has Rampage",
|
||||
/* n06 */ "Native attribute",
|
||||
/* n07 */ "A.Beast attribute",
|
||||
/* n08 */ "Machine attribute",
|
||||
/* n09 */ "Dark attribute",
|
||||
/* n10 */ "Sword-type item",
|
||||
/* n11 */ "Gun-type item",
|
||||
/* n12 */ "Cane-type item",
|
||||
/* n13 */ "Guard item",
|
||||
/* n14 */ "Story Character",
|
||||
/* n15 */ "Attacker does not use action cards",
|
||||
/* n16 */ "Aerial attribute",
|
||||
/* n17 */ "Same AP as opponent",
|
||||
/* n18 */ "Opponent is SC",
|
||||
/* n19 */ "Has Paralyzed condition",
|
||||
/* n20 */ "Has Frozen condition",
|
||||
});
|
||||
|
||||
static const vector<const char*> description_for_p_target({
|
||||
/* p00 */ "Unknown: p00", // Unused; probably invalid
|
||||
/* p01 */ "SC / FC who set the card",
|
||||
/* p02 */ "Attacking SC / FC",
|
||||
/* p03 */ "Unknown: p03", // Unused
|
||||
/* p04 */ "Unknown: p04", // Unused
|
||||
/* p05 */ "Unknown: p05", // Unused
|
||||
/* p06 */ "??? (TODO)",
|
||||
/* p07 */ "??? (TODO; Weakness)",
|
||||
/* p08 */ "FC's owner SC",
|
||||
/* p09 */ "Unknown: p09", // Unused
|
||||
/* p10 */ "All ally FCs",
|
||||
/* p11 */ "All ally FCs", // TODO: how is this different from p10?
|
||||
/* p12 */ "All non-aerial FCs on both teams",
|
||||
/* p13 */ "All FCs on both teams that are Frozen",
|
||||
/* p14 */ "All FCs on both teams that have <= 3 HP",
|
||||
/* p15 */ "All FCs except SCs", // TODO: used during attacks only?
|
||||
/* p16 */ "All FCs except SCs", // TODO: used during attacks only? how is this different from p15?
|
||||
/* p17 */ "This card",
|
||||
/* p18 */ "SC who equipped this card",
|
||||
/* p19 */ "Unknown: p19", // Unused
|
||||
/* p20 */ "Unknown: p20", // Unused
|
||||
/* p21 */ "Unknown: p21", // Unused
|
||||
/* p22 */ "All characters (SCs & FCs) including this card", // TODO: But why does Shifta apply only to allies then?
|
||||
/* p23 */ "All characters (SCs & FCs) except this card",
|
||||
/* p24 */ "All FCs on both teams that have Paralysis",
|
||||
/* p25 */ "Unknown: p25", // Unused
|
||||
/* p26 */ "Unknown: p26", // Unused
|
||||
/* p27 */ "Unknown: p27", // Unused
|
||||
/* p28 */ "Unknown: p28", // Unused
|
||||
/* p29 */ "Unknown: p29", // Unused
|
||||
/* p30 */ "Unknown: p30", // Unused
|
||||
/* p31 */ "Unknown: p31", // Unused
|
||||
/* p32 */ "Unknown: p32", // Unused
|
||||
/* p33 */ "Unknown: p33", // Unused
|
||||
/* p34 */ "Unknown: p34", // Unused
|
||||
/* p35 */ "All characters (SCs & FCs) within range", // Used for Explosion effect
|
||||
/* p36 */ "All ally SCs within range, but not the caster", // Resta
|
||||
/* p37 */ "All FCs or all opponent FCs (TODO)", // TODO: when to use which selector? is a3 involved here somehow?
|
||||
/* p38 */ "All allies except items within range (and not this card)",
|
||||
/* p39 */ "All FCs that cost 4 or more points",
|
||||
/* p40 */ "All FCs that cost 3 or fewer points",
|
||||
/* p41 */ "Unknown: p41", // Unused
|
||||
/* p42 */ "Attacker during defense phase", // Only used by TP Defense
|
||||
/* p43 */ "Owner SC of defending FC during attack",
|
||||
/* p44 */ "SC\'s own creature FCs within range",
|
||||
/* p45 */ "Both attacker and defender", // Used for Snatch, which moves EXP from one to the other
|
||||
/* p46 */ "All SCs & FCs one space left or right of this card",
|
||||
/* p47 */ "FC\'s owner Boss SC", // Only used for Gibbles+ which explicitly mentions Boss SC, so it looks like this is p08 but for bosses
|
||||
/* p48 */ "Everything within range, including this card\'s user", // Madness
|
||||
/* p49 */ "All ally FCs within range except this card",
|
||||
});
|
||||
|
||||
struct Ep3AbilityDescription {
|
||||
uint8_t command;
|
||||
bool has_expr;
|
||||
const char* name;
|
||||
const char* description;
|
||||
};
|
||||
|
||||
static const std::vector<Ep3AbilityDescription> name_for_effect_command({
|
||||
{0x00, false, nullptr, nullptr},
|
||||
{0x01, true, "AP Boost", "Temporarily increase AP by N"},
|
||||
{0x02, false, "Rampage", "Rampage"},
|
||||
{0x03, true, "Multi Strike", "Duplicate attack N times"},
|
||||
{0x04, true, "Damage Modifier 1", "Set attack damage / AP to N after action cards applied (step 1)"},
|
||||
{0x05, false, "Immobile", "Give Immobile condition"},
|
||||
{0x06, false, "Hold", "Give Hold condition"},
|
||||
{0x07, false, nullptr, nullptr},
|
||||
{0x08, true, "TP Boost", "Add N TP temporarily during attack"},
|
||||
{0x09, true, "Give Damage", "Cause direct N HP loss"},
|
||||
{0x0A, false, "Guom", "Give Guom condition"},
|
||||
{0x0B, false, "Paralyze", "Give Paralysis condition"},
|
||||
{0x0C, false, nullptr, nullptr},
|
||||
{0x0D, false, "A/H Swap", "Swap AP and HP temporarily"},
|
||||
{0x0E, false, "Pierce", "Attack SC directly even if they have items equipped"},
|
||||
{0x0F, false, nullptr, nullptr},
|
||||
{0x10, true, "Heal", "Increase HP by N"},
|
||||
{0x11, false, "Return to Hand", "Return card to hand"},
|
||||
{0x12, false, nullptr, nullptr},
|
||||
{0x13, false, nullptr, nullptr},
|
||||
{0x14, false, "Acid", "Give Acid condition"},
|
||||
{0x15, false, nullptr, nullptr},
|
||||
{0x16, true, "Mighty Knuckle", "Temporarily increase AP by N, and set ATK dice to zero"},
|
||||
{0x17, true, "Unit Blow", "Temporarily increase AP by N * number of this card set within phase"},
|
||||
{0x18, false, "Curse", "Give Curse condition"},
|
||||
{0x19, false, "Combo (AP)", "Temporarily increase AP by number of this card set within phase"},
|
||||
{0x1A, false, "Pierce/Rampage Block", "Block attack if Pierce/Rampage (?)"},
|
||||
{0x1B, false, "Ability Trap", "Temporarily disable opponent abilities"},
|
||||
{0x1C, false, "Freeze", "Give Freeze condition"},
|
||||
{0x1D, false, "Anti-Abnormality", "Cure all conditions"},
|
||||
{0x1E, false, nullptr, nullptr},
|
||||
{0x1F, false, "Explosion", "Damage all SCs and FCs by number of this same card set * 2"},
|
||||
{0x20, false, nullptr, nullptr},
|
||||
{0x21, false, nullptr, nullptr},
|
||||
{0x22, false, nullptr, nullptr},
|
||||
{0x23, false, "Return to Deck", "Cancel discard and move to bottom of deck instead"},
|
||||
{0x24, false, "Aerial", "Give Aerial status"},
|
||||
{0x25, true, "AP Loss", "Make attacker temporarily lose N AP during defense"},
|
||||
{0x26, true, "Bonus From Leader", "Gain AP equal to the number of cards of type N on the field"},
|
||||
{0x27, false, "Free Maneuver", "Enable movement over occupied tiles"},
|
||||
{0x28, false, "Haste", "Make move actions free"},
|
||||
{0x29, true, "Clone", "Make setting this card free if at least one card of type N is already on the field"},
|
||||
{0x2A, true, "DEF Disable by Cost", "Disable use of any defense cards costing between (N / 10) and (N % 10) points, inclusive"},
|
||||
{0x2B, true, "Filial", "Increase controlling SC\'s HP by N when this card is destroyed"},
|
||||
{0x2C, true, "Snatch", "Steal N EXP during attack"},
|
||||
{0x2D, true, "Hand Disrupter", "DIscard N cards from hand immediately"},
|
||||
{0x2E, false, "Drop", "Give Drop condition"},
|
||||
{0x2F, false, "Action Disrupter", "Destroy all action cards used by attacker"},
|
||||
{0x30, true, "Set HP", "Set HP to N (?) (TODO)"},
|
||||
{0x31, false, "Native Shield", "Block attacks from Native creatures"},
|
||||
{0x32, false, "A.Beast Shield", "Block attacks from A.Beast creatures"},
|
||||
{0x33, false, "Machine Shield", "Block attacks from Machine creatures"},
|
||||
{0x34, false, "Dark Shield", "Block attacks from Dark creatures"},
|
||||
{0x35, false, "Sword Shield", "Block attacks from Sword items"},
|
||||
{0x36, false, "Gun Shield", "Block attacks from Gun items"},
|
||||
{0x37, false, "Cane Shield", "Block attacks from Cane items"},
|
||||
{0x38, false, nullptr, nullptr},
|
||||
{0x39, false, nullptr, nullptr},
|
||||
{0x3A, false, "Defender", "Make attacks go to setter of this card instead of original target"},
|
||||
{0x3B, false, "Survival Decoys", "Redirect damage for multi-sided attack"},
|
||||
{0x3C, true, "Give/Take EXP", "Give N EXP, or take if N is negative"},
|
||||
{0x3D, false, nullptr, nullptr},
|
||||
{0x3E, false, "Death Companion", "If this card has 1 or 2 HP, set its HP to N"},
|
||||
{0x3F, true, "EXP Decoy", "If defender has EXP, lose EXP instead of getting damage when attacked"},
|
||||
{0x40, true, "Set MV", "Set MV to N"},
|
||||
{0x41, true, "Group", "Temporarily increase AP by N * number of this card on field, excluding itself"},
|
||||
{0x42, false, "Berserk", "User of this card receives the same damage as target, and isn't helped by target's defense cards"},
|
||||
{0x43, false, "Guard Creature", "Attacks on controlling SC damage this card instead"},
|
||||
{0x44, false, "Tech", "Technique cards cost 1 fewer ATK point"},
|
||||
{0x45, false, "Big Swing", "Increase all attacking ATK costs by 1"},
|
||||
{0x46, false, nullptr, nullptr},
|
||||
{0x47, false, "Shield Weapon", "Limit attacker\'s choice of target to guard items"},
|
||||
{0x48, false, "ATK Dice Boost", "Increase ATK dice roll by 1"},
|
||||
{0x49, false, nullptr, nullptr},
|
||||
{0x4A, false, "Major Pierce", "If SC has over half of max HP, attacks target SC instead of equipped items"},
|
||||
{0x4B, false, "Heavy Pierce", "If SC has 3 or more items equipped, attacks target SC instead of equipped items"},
|
||||
{0x4C, false, "Major Rampage", "If SC has over half of max HP, attacks target SC and all equipped items"},
|
||||
{0x4D, false, "Heavy Rampage", "If SC has 3 or more items equipped, attacks target SC and all equipped items"},
|
||||
{0x4E, true, "AP Growth", "Permanently increase AP by N"},
|
||||
{0x4F, true, "TP Growth", "Permanently increase TP by N"},
|
||||
{0x50, true, "Reborn", "If any card of type N is on the field, this card goes to the hand when destroyed instead of being discarded"},
|
||||
{0x51, true, "Copy", "Temporarily set AP/TP to N percent (or 100% if N is 0) of opponent\'s values"},
|
||||
{0x52, false, nullptr, nullptr},
|
||||
{0x53, true, "Misc. Guards", "Add N to card's defense value"},
|
||||
{0x54, true, "AP Override", "Set AP to N temporarily"},
|
||||
{0x55, true, "TP Override", "Set TP to N temporarily"},
|
||||
{0x56, false, "Return", "Return card to hand on destruction instead of discarding"},
|
||||
{0x57, false, "A/T Swap Perm", "Permanently swap AP and TP"},
|
||||
{0x58, false, "A/H Swap Perm", "Permanently swap AP and HP"},
|
||||
{0x59, true, "Slayers/Assassins", "Temporarily increase AP during attack"},
|
||||
{0x5A, false, "Anti-Abnormality", "Remove all conditions"},
|
||||
{0x5B, false, "Fixed Range", "Use SC\'s range instead of weapon or attack card ranges"},
|
||||
{0x5C, false, "Elude", "SC does not lose HP when equipped items are destroyed"},
|
||||
{0x5D, false, "Parry", "Forward attack to a random FC within one tile of original target, excluding attacker and original target"},
|
||||
{0x5E, false, "Block Attack", "Completely block attack"},
|
||||
{0x5F, false, nullptr, nullptr},
|
||||
{0x60, false, nullptr, nullptr},
|
||||
{0x61, true, "Combo (TP)", "Gain TP equal to the number of cards of type N on the field"},
|
||||
{0x62, true, "Misc. AP Bonuses", "Temporarily increase AP by N"},
|
||||
{0x63, true, "Misc. TP Bonuses", "Temporarily increase TP by N"},
|
||||
{0x64, false, nullptr, nullptr},
|
||||
{0x65, true, "Misc. Defense Bonuses", "Decrease damage by N"},
|
||||
{0x66, true, "Mostly Halfguards", "Reduce damage from incoming attack by N"},
|
||||
{0x67, false, "Periodic Field", "Swap immunity to tech or physical attacks"},
|
||||
{0x68, false, "Unlimited Summoning", "Allow unlimited summoning"},
|
||||
{0x69, false, nullptr, nullptr},
|
||||
{0x6A, true, "MV Bonus", "Increase MV by N"},
|
||||
{0x6B, true, "Forward Damage", "Give N damage back to attacker during defense (?) (TODO)"},
|
||||
{0x6C, true, "Weak Spot / Influence", "Temporarily decrease AP by N"},
|
||||
{0x6D, true, "Damage Modifier 2", "Set attack damage / AP after action cards applied (step 2)"},
|
||||
{0x6E, true, "Weak Hit Block", "Block all attacks of N damage or less"},
|
||||
{0x6F, true, "AP Silence", "Temporarily decrease AP of opponent by N"},
|
||||
{0x70, true, "TP Silence", "Temporarily decrease TP of opponent by N"},
|
||||
{0x71, false, "A/T Swap", "Temporarily swap AP and TP"},
|
||||
{0x72, true, "Halfguard", "Halve damage from attacks that would inflict N or more damage"},
|
||||
{0x73, false, nullptr, nullptr},
|
||||
{0x74, true, "Rampage AP Loss", "Temporarily reduce AP by N"},
|
||||
{0x75, false, nullptr, nullptr},
|
||||
{0x76, false, "Reflect", "Generate reverse attack"},
|
||||
});
|
||||
|
||||
void Ep3CardStats::Stat::decode_code() {
|
||||
this->type = static_cast<Type>(this->code / 1000);
|
||||
int16_t value = this->code - (this->type * 1000);
|
||||
if (value != 999) {
|
||||
switch (this->type) {
|
||||
case Type::BLANK:
|
||||
this->stat = 0;
|
||||
break;
|
||||
case Type::STAT:
|
||||
case Type::PLUS_STAT:
|
||||
case Type::EQUALS_STAT:
|
||||
this->stat = value;
|
||||
break;
|
||||
case Type::MINUS_STAT:
|
||||
this->stat = -value;
|
||||
break;
|
||||
default:
|
||||
throw runtime_error("invalid card stat type");
|
||||
}
|
||||
} else {
|
||||
this->stat = 0;
|
||||
this->type = static_cast<Type>(this->type + 4);
|
||||
}
|
||||
}
|
||||
|
||||
string Ep3CardStats::Stat::str() const {
|
||||
switch (this->type) {
|
||||
case Type::BLANK:
|
||||
return "";
|
||||
case Type::STAT:
|
||||
return string_printf("%hhd", this->stat);
|
||||
case Type::PLUS_STAT:
|
||||
return string_printf("+%hhd", this->stat);
|
||||
case Type::MINUS_STAT:
|
||||
return string_printf("-%d", -this->stat);
|
||||
case Type::EQUALS_STAT:
|
||||
return string_printf("=%hhd", this->stat);
|
||||
case Type::UNKNOWN:
|
||||
return "?";
|
||||
case Type::PLUS_UNKNOWN:
|
||||
return "+?";
|
||||
case Type::MINUS_UNKNOWN:
|
||||
return "-?";
|
||||
case Type::EQUALS_UNKNOWN:
|
||||
return "=?";
|
||||
default:
|
||||
return string_printf("[%02hhX %02hhX]", this->type, this->stat);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Ep3CardStats::Effect::is_empty() const {
|
||||
return (this->command == 0 &&
|
||||
this->expr.is_filled_with(0) &&
|
||||
this->when == 0 &&
|
||||
this->arg1.is_filled_with(0) &&
|
||||
this->arg2.is_filled_with(0) &&
|
||||
this->arg3.is_filled_with(0) &&
|
||||
this->unknown_a3.is_filled_with(0));
|
||||
}
|
||||
|
||||
string Ep3CardStats::Effect::str_for_arg(const std::string& arg) {
|
||||
if (arg.empty()) {
|
||||
return arg;
|
||||
}
|
||||
if (arg.size() != 3) {
|
||||
return arg + "/(invalid)";
|
||||
}
|
||||
size_t value;
|
||||
try {
|
||||
value = stoul(arg.c_str() + 1, nullptr, 10);
|
||||
} catch (const invalid_argument&) {
|
||||
return arg + "/(invalid)";
|
||||
}
|
||||
|
||||
switch (arg[0]) {
|
||||
case 'a':
|
||||
return arg + "/(unknown)";
|
||||
case 'C':
|
||||
case 'c':
|
||||
return string_printf("%s/Req. linked item (%zu=>%zu)", arg.c_str(), value / 10, value % 10);
|
||||
case 'd':
|
||||
return string_printf("%s/Req. die roll in [%zu, %zu]", arg.c_str(), value / 10, value % 10);
|
||||
case 'e':
|
||||
return arg + "/While equipped";
|
||||
case 'h':
|
||||
return string_printf("%s/Req. HP >= %zu", arg.c_str(), value);
|
||||
case 'i':
|
||||
return string_printf("%s/Req. HP <= %zu", arg.c_str(), value);
|
||||
case 'n':
|
||||
try {
|
||||
return string_printf("%s/Req. condition: %s", arg.c_str(), description_for_n_condition.at(value));
|
||||
} catch (const out_of_range&) {
|
||||
return arg + "/(unknown)";
|
||||
}
|
||||
case 'o':
|
||||
return arg + "/Req. prev effect conditions passed";
|
||||
case 'p':
|
||||
try {
|
||||
return string_printf("%s/Target: %s", arg.c_str(), description_for_p_target.at(value));
|
||||
} catch (const out_of_range&) {
|
||||
return arg + "/(unknown)";
|
||||
}
|
||||
case 'r':
|
||||
return string_printf("%s/Req. random with %zu%% chance", arg.c_str(), value == 0 ? 100 : value);
|
||||
case 's':
|
||||
return string_printf("%s/Req. cost in [%zu, %zu]", arg.c_str(), value / 10, value % 10);
|
||||
case 't':
|
||||
return string_printf("%s/Turns: %zu", arg.c_str(), value);
|
||||
default:
|
||||
return arg + "/(unknown)";
|
||||
}
|
||||
}
|
||||
|
||||
string Ep3CardStats::Effect::str() const {
|
||||
string cmd_str = string_printf("%02hhX", this->command);
|
||||
try {
|
||||
const char* name = name_for_effect_command.at(this->command).name;
|
||||
if (name) {
|
||||
cmd_str += ':';
|
||||
cmd_str += name;
|
||||
}
|
||||
} catch (const out_of_range&) { }
|
||||
|
||||
string when_str = string_printf("%02hhX", this->when);
|
||||
try {
|
||||
const char* name = description_for_when.at(this->when);
|
||||
if (name) {
|
||||
when_str += ':';
|
||||
when_str += name;
|
||||
}
|
||||
} catch (const out_of_range&) { }
|
||||
|
||||
string expr_str = this->expr;
|
||||
if (!expr_str.empty()) {
|
||||
expr_str = ", expr=" + expr_str;
|
||||
}
|
||||
|
||||
string arg1str = this->str_for_arg(this->arg1);
|
||||
string arg2str = this->str_for_arg(this->arg2);
|
||||
string arg3str = this->str_for_arg(this->arg3);
|
||||
string a3str = format_data_string(this->unknown_a3.data(), sizeof(this->unknown_a3));
|
||||
return string_printf("(cmd=%s%s, when=%s, arg1=%s, arg2=%s, arg3=%s, a3=%s)",
|
||||
cmd_str.c_str(), expr_str.c_str(), when_str.c_str(), arg1str.data(),
|
||||
arg2str.data(), arg3str.data(), a3str.c_str());
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Ep3CardStats::decode_range() {
|
||||
// If the cell representing the FC is nonzero, the card has a range from a
|
||||
// list of constants. Otherwise, its range is already defined in the range
|
||||
// array and should be left alone.
|
||||
uint8_t index = (this->range[4] >> 8) & 0xF;
|
||||
if (index != 0) {
|
||||
this->range.clear(0);
|
||||
switch (index) {
|
||||
case 1: // Single cell in front of FC
|
||||
this->range[3] = 0x00000100;
|
||||
break;
|
||||
case 2: // Cell in front of FC and the front-left and front-right (Slash)
|
||||
this->range[3] = 0x00001110;
|
||||
break;
|
||||
case 3: // 3 cells in a line in front of FC
|
||||
this->range[1] = 0x00000100;
|
||||
this->range[2] = 0x00000100;
|
||||
this->range[3] = 0x00000100;
|
||||
break;
|
||||
case 4: // All 8 cells around FC
|
||||
this->range[3] = 0x00001110;
|
||||
this->range[4] = 0x00001010;
|
||||
this->range[5] = 0x00001110;
|
||||
break;
|
||||
case 5: // 2 cells in a line in front of FC
|
||||
this->range[2] = 0x00000100;
|
||||
this->range[3] = 0x00000100;
|
||||
break;
|
||||
case 6: // Entire field (renders as "A")
|
||||
for (size_t x = 0; x < 6; x++) {
|
||||
this->range[x] = 0x000FFFFF;
|
||||
}
|
||||
break;
|
||||
case 7: // Superposition of 4 and 5 (unused)
|
||||
this->range[2] = 0x00000100;
|
||||
this->range[3] = 0x00001110;
|
||||
this->range[4] = 0x00001010;
|
||||
this->range[5] = 0x00001110;
|
||||
break;
|
||||
case 8: // All 8 cells around FC and FC's cell
|
||||
this->range[3] = 0x00001110;
|
||||
this->range[4] = 0x00001110;
|
||||
this->range[5] = 0x00001110;
|
||||
break;
|
||||
case 9: // No cells
|
||||
break;
|
||||
// The table in the DOL file only appears to contain 9 entries; there are
|
||||
// some pointers immediately after. So probably if a card specified A-F,
|
||||
// its range would be filled in with garbage in the original game.
|
||||
default:
|
||||
throw runtime_error("invalid fixed range index");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
string name_for_rarity(uint8_t rarity) {
|
||||
static const vector<const char*> names({
|
||||
"N1",
|
||||
"R1",
|
||||
"S",
|
||||
"E",
|
||||
"N2",
|
||||
"N3",
|
||||
"N4",
|
||||
"R2",
|
||||
"R3",
|
||||
"R4",
|
||||
"SS",
|
||||
"D1",
|
||||
"D2",
|
||||
"INVIS",
|
||||
});
|
||||
try {
|
||||
return names.at(rarity - 1);
|
||||
} catch (const out_of_range&) {
|
||||
return string_printf("(%02hhX)", rarity);
|
||||
}
|
||||
}
|
||||
|
||||
string name_for_target_mode(uint8_t target_mode) {
|
||||
static const vector<const char*> names({
|
||||
"NONE",
|
||||
"SINGLE",
|
||||
"MULTI",
|
||||
"SELF",
|
||||
"TEAM",
|
||||
"ALL",
|
||||
"MULTI-ALLY",
|
||||
"ALL-ALLY",
|
||||
"ALL-ATTACK",
|
||||
"OWN-FCS",
|
||||
});
|
||||
try {
|
||||
return names.at(target_mode);
|
||||
} catch (const out_of_range&) {
|
||||
return string_printf("(%02hhX)", target_mode);
|
||||
}
|
||||
}
|
||||
|
||||
string string_for_colors(const parray<uint8_t, 8>& colors) {
|
||||
string ret;
|
||||
for (size_t x = 0; x < 8; x++) {
|
||||
if (colors[x]) {
|
||||
ret += '0' + colors[x];
|
||||
}
|
||||
}
|
||||
if (ret.empty()) {
|
||||
return "none";
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
string string_for_assist_turns(uint8_t turns) {
|
||||
if (turns == 90) {
|
||||
return "ONCE";
|
||||
} else if (turns == 99) {
|
||||
return "FOREVER";
|
||||
} else {
|
||||
return string_printf("%hhu", turns);
|
||||
}
|
||||
}
|
||||
|
||||
string string_for_range(const parray<be_uint32_t, 6>& range) {
|
||||
string ret;
|
||||
for (size_t x = 0; x < 6; x++) {
|
||||
ret += string_printf("%05" PRIX32 "/", range[x].load());
|
||||
}
|
||||
while (starts_with(ret, "00000/")) {
|
||||
ret = ret.substr(6);
|
||||
}
|
||||
if (!ret.empty()) {
|
||||
ret.resize(ret.size() - 1);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
string Ep3CardStats::str() const {
|
||||
string type_str;
|
||||
try {
|
||||
type_str = name_for_card_type.at(this->type);
|
||||
} catch (const out_of_range&) {
|
||||
type_str = string_printf("%02hhX", this->type);
|
||||
}
|
||||
string rarity_str = name_for_rarity(this->rarity);
|
||||
string target_mode_str = name_for_target_mode(this->target_mode);
|
||||
string range_str = string_for_range(this->range);
|
||||
string assist_turns_str = string_for_assist_turns(this->assist_turns);
|
||||
string hp_str = this->hp.str();
|
||||
string ap_str = this->ap.str();
|
||||
string tp_str = this->tp.str();
|
||||
string mv_str = this->mv.str();
|
||||
string left_str = string_for_colors(this->left_colors);
|
||||
string right_str = string_for_colors(this->right_colors);
|
||||
string top_str = string_for_colors(this->top_colors);
|
||||
string effects_str;
|
||||
for (size_t x = 0; x < 3; x++) {
|
||||
if (this->effects[x].is_empty()) {
|
||||
continue;
|
||||
}
|
||||
if (!effects_str.empty()) {
|
||||
effects_str += ", ";
|
||||
}
|
||||
effects_str += this->effects[x].str();
|
||||
}
|
||||
return string_printf(
|
||||
"[Card: %04" PRIX32 " name=%s type=%s-%02hhX rare=%s cost=%hhX+%hhX "
|
||||
"target=%s range=%s assist_turns=%s cannot_move=%s cannot_attack=%s "
|
||||
"hidden=%s hp=%s ap=%s tp=%s mv=%s left=%s right=%s top=%s a2=%08" PRIX32 " "
|
||||
"assist_effect=[%hu, %hu] a3=[%hu, %hu] has_effects=%s effects=[%s]]",
|
||||
this->card_id.load(),
|
||||
this->name.data(),
|
||||
type_str.c_str(),
|
||||
this->subtype,
|
||||
rarity_str.c_str(),
|
||||
this->self_cost,
|
||||
this->ally_cost,
|
||||
target_mode_str.c_str(),
|
||||
range_str.c_str(),
|
||||
assist_turns_str.c_str(),
|
||||
this->cannot_move ? "true" : "false",
|
||||
this->cannot_attack ? "true" : "false",
|
||||
this->hide_in_deck_edit ? "true" : "false",
|
||||
hp_str.c_str(),
|
||||
ap_str.c_str(),
|
||||
tp_str.c_str(),
|
||||
mv_str.c_str(),
|
||||
left_str.c_str(),
|
||||
right_str.c_str(),
|
||||
top_str.c_str(),
|
||||
this->unknown_a2.load(),
|
||||
this->assist_effect[0].load(),
|
||||
this->assist_effect[1].load(),
|
||||
this->unknown_a3[0].load(),
|
||||
this->unknown_a3[1].load(),
|
||||
this->has_effects ? "true" : "false",
|
||||
effects_str.c_str());
|
||||
}
|
||||
|
||||
|
||||
|
||||
Ep3DataIndex::Ep3DataIndex(const string& directory) {
|
||||
static constexpr bool debug_enabled = false;
|
||||
|
||||
unordered_map<uint32_t, vector<string>> card_tags;
|
||||
if (debug_enabled) {
|
||||
unordered_map<uint32_t, string> card_text;
|
||||
try {
|
||||
string data = prs_decompress(load_file(directory + "/cardtext.mnr"));
|
||||
StringReader r(data);
|
||||
|
||||
while (!r.eof()) {
|
||||
uint32_t card_id = stoul(r.get_cstr());
|
||||
|
||||
// Most cards have multiple pages, but we only care about the first page
|
||||
// (for now)
|
||||
string text = r.get_cstr();
|
||||
|
||||
// Preprocess text: first, delete all color markers
|
||||
size_t offset = text.find("\tC");
|
||||
while (offset != string::npos) {
|
||||
text = text.substr(0, offset) + text.substr(offset + 3);
|
||||
offset = text.find("\tC");
|
||||
}
|
||||
// Preprocess text: delete all initial lines that don't start with \t
|
||||
offset = text.find('\t');
|
||||
if (offset == string::npos) {
|
||||
text.clear();
|
||||
} else {
|
||||
text = text.substr(offset);
|
||||
}
|
||||
// Preprocess text: merge lines that don't begin with \t
|
||||
for (offset = 0; offset < text.size(); offset++) {
|
||||
if (text[offset] == '\n' && text[offset + 1] != '\t') {
|
||||
text = text.substr(0, offset) + text.substr(offset + 1);
|
||||
offset--;
|
||||
}
|
||||
}
|
||||
|
||||
// Split text into tags
|
||||
vector<string> tags;
|
||||
auto lines = split(text, '\n');
|
||||
for (const auto& line : lines) {
|
||||
if (line[0] == '\t' && line[1] == 'D') {
|
||||
tags.emplace_back("D: " + line.substr(2));
|
||||
} else if (line[0] == '\t' && line[1] == 'S') {
|
||||
tags.emplace_back("S: " + line.substr(2));
|
||||
}
|
||||
}
|
||||
|
||||
if (!card_text.emplace(card_id, move(text)).second) {
|
||||
throw runtime_error("duplicate card text id");
|
||||
}
|
||||
if (!card_tags.emplace(card_id, move(tags)).second) {
|
||||
throw logic_error("duplicate card tags id");
|
||||
}
|
||||
|
||||
r.go((r.where() + 0x3FF) & (~0x3FF));
|
||||
}
|
||||
|
||||
} catch (const exception& e) {
|
||||
log(WARNING, "Failed to load card text: %s", e.what());
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
this->compressed_card_definitions = load_file(directory + "/cardupdate.mnr");
|
||||
string data = prs_decompress(this->compressed_card_definitions);
|
||||
// There's a footer after the card definitions, but we ignore it
|
||||
if (data.size() % sizeof(Ep3CardStats) != sizeof(Ep3CardStatsFooter)) {
|
||||
throw runtime_error(string_printf(
|
||||
"decompressed card update file size %zX is not aligned with card definition size %zX (%zX extra bytes)",
|
||||
data.size(), sizeof(Ep3CardStats), data.size() % sizeof(Ep3CardStats)));
|
||||
}
|
||||
const auto* stats = reinterpret_cast<const Ep3CardStats*>(data.data());
|
||||
size_t max_cards = data.size() / sizeof(Ep3CardStats);
|
||||
for (size_t x = 0; x < max_cards; x++) {
|
||||
// The last card entry has the build date and some other metadata (and
|
||||
// isn't a real card, obviously), so skip it. Seems like the card ID is
|
||||
// always a large number that won't fit in a uint16_t, so we use that to
|
||||
// determine if the entry is a real card or not.
|
||||
if (stats[x].card_id & 0xFFFF0000) {
|
||||
continue;
|
||||
}
|
||||
shared_ptr<CardEntry> entry(new CardEntry({stats[x], {}}));
|
||||
if (!this->card_definitions.emplace(entry->stats.card_id, entry).second) {
|
||||
throw runtime_error(string_printf(
|
||||
"duplicate card id: %08" PRIX32, entry->stats.card_id.load()));
|
||||
}
|
||||
|
||||
entry->stats.hp.decode_code();
|
||||
entry->stats.ap.decode_code();
|
||||
entry->stats.tp.decode_code();
|
||||
entry->stats.mv.decode_code();
|
||||
entry->stats.decode_range();
|
||||
|
||||
if (debug_enabled) {
|
||||
string card_str = entry->stats.str();
|
||||
try {
|
||||
string tags_str = join(card_tags.at(stats[x].card_id), ", ");
|
||||
fprintf(stderr, "%s tags: [%s]\n", card_str.c_str(), tags_str.c_str());
|
||||
} catch (const out_of_range&) {
|
||||
fprintf(stderr, "%s\n", card_str.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log(INFO, "Indexed %zu Episode 3 card definitions", this->card_definitions.size());
|
||||
} catch (const exception& e) {
|
||||
log(WARNING, "Failed to load Episode 3 card update: %s", e.what());
|
||||
}
|
||||
|
||||
for (const auto& filename : list_directory(directory)) {
|
||||
try {
|
||||
shared_ptr<MapEntry> entry;
|
||||
|
||||
if (ends_with(filename, ".mnmd")) {
|
||||
entry.reset(new MapEntry(load_object_file<Ep3Map>(directory + "/" + filename)));
|
||||
} else if (ends_with(filename, ".mnm")) {
|
||||
entry.reset(new MapEntry(load_file(directory + "/" + filename)));
|
||||
}
|
||||
|
||||
if (entry.get()) {
|
||||
if (!this->maps.emplace(entry->map.map_number, entry).second) {
|
||||
throw runtime_error("duplicate map number");
|
||||
}
|
||||
string name = entry->map.name;
|
||||
log(INFO, "Indexed Episode 3 map %s (%08" PRIX32 "; %s)",
|
||||
filename.c_str(), entry->map.map_number.load(), name.c_str());
|
||||
}
|
||||
|
||||
} catch (const exception& e) {
|
||||
log(WARNING, "Failed to index Episode 3 map %s: %s",
|
||||
filename.c_str(), e.what());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ep3DataIndex::MapEntry::MapEntry(const Ep3Map& map) : map(map) { }
|
||||
|
||||
Ep3DataIndex::MapEntry::MapEntry(const string& compressed)
|
||||
: compressed_data(compressed) {
|
||||
string decompressed = prs_decompress(this->compressed_data);
|
||||
if (decompressed.size() != sizeof(Ep3Map)) {
|
||||
throw runtime_error(string_printf(
|
||||
"decompressed data size is incorrect (expected %zu bytes, read %zu bytes)",
|
||||
sizeof(Ep3Map), decompressed.size()));
|
||||
}
|
||||
this->map = *reinterpret_cast<const Ep3Map*>(decompressed.data());
|
||||
}
|
||||
|
||||
string Ep3DataIndex::MapEntry::compressed() const {
|
||||
if (this->compressed_data.empty()) {
|
||||
this->compressed_data = prs_compress(&this->map, sizeof(this->map));
|
||||
}
|
||||
return this->compressed_data;
|
||||
}
|
||||
|
||||
const string& Ep3DataIndex::get_compressed_card_definitions() const {
|
||||
if (this->compressed_card_definitions.empty()) {
|
||||
throw runtime_error("card definitions are not available");
|
||||
}
|
||||
return this->compressed_card_definitions;
|
||||
}
|
||||
|
||||
shared_ptr<const Ep3DataIndex::CardEntry> Ep3DataIndex::get_card_definition(
|
||||
uint32_t id) const {
|
||||
return this->card_definitions.at(id);
|
||||
}
|
||||
|
||||
const string& Ep3DataIndex::get_compressed_map_list() const {
|
||||
if (this->compressed_map_list.empty()) {
|
||||
// TODO: Write a version of prs_compress that takes iovecs (or something
|
||||
// similar) so we can eliminate all this string copying here.
|
||||
StringWriter entries_w;
|
||||
StringWriter strings_w;
|
||||
|
||||
for (const auto& map_it : this->maps) {
|
||||
Ep3MapList::Entry e;
|
||||
const auto& map = map_it.second->map;
|
||||
e.map_x = map.map_x;
|
||||
e.map_y = map.map_y;
|
||||
e.scene_data2 = map.scene_data2;
|
||||
e.map_number = map.map_number.load();
|
||||
e.width = map.width;
|
||||
e.height = map.height;
|
||||
e.map_tiles = map.map_tiles;
|
||||
e.modification_tiles = map.modification_tiles;
|
||||
|
||||
e.name_offset = strings_w.size();
|
||||
strings_w.write(map.name.data(), map.name.len());
|
||||
strings_w.put_u8(0);
|
||||
e.location_name_offset = strings_w.size();
|
||||
strings_w.write(map.location_name.data(), map.location_name.len());
|
||||
strings_w.put_u8(0);
|
||||
e.quest_name_offset = strings_w.size();
|
||||
strings_w.write(map.quest_name.data(), map.quest_name.len());
|
||||
strings_w.put_u8(0);
|
||||
e.description_offset = strings_w.size();
|
||||
strings_w.write(map.description.data(), map.description.len());
|
||||
strings_w.put_u8(0);
|
||||
|
||||
e.unknown_a2 = 0xFF000000;
|
||||
|
||||
entries_w.put(e);
|
||||
}
|
||||
|
||||
Ep3MapList header;
|
||||
header.num_maps = this->maps.size();
|
||||
header.unknown_a1 = 0;
|
||||
header.strings_offset = entries_w.size();
|
||||
header.total_size = sizeof(Ep3MapList) + entries_w.size() + strings_w.size();
|
||||
|
||||
StringWriter w;
|
||||
w.put(header);
|
||||
w.write(entries_w.str());
|
||||
w.write(strings_w.str());
|
||||
|
||||
StringWriter compressed_w;
|
||||
compressed_w.put_u32b(w.str().size());
|
||||
compressed_w.write(prs_compress(w.str()));
|
||||
this->compressed_map_list = move(compressed_w.str());
|
||||
log(INFO, "Generated Episode 3 compressed map list (%zu -> %zu bytes)",
|
||||
w.size(), this->compressed_map_list.size());
|
||||
}
|
||||
return this->compressed_map_list;
|
||||
}
|
||||
|
||||
shared_ptr<const Ep3DataIndex::MapEntry> Ep3DataIndex::get_map(uint32_t id) const {
|
||||
return this->maps.at(id);
|
||||
}
|
||||
-351
@@ -1,351 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <phosg/Encoding.hh>
|
||||
|
||||
#include "Text.hh"
|
||||
|
||||
|
||||
|
||||
// Note: Much of the structures and enums here are based on the card list file,
|
||||
// and comparing the card text with the data in the file. Some inferences may be
|
||||
// incorrect here, since Episode 3's card text is wrong in various places.
|
||||
|
||||
struct Ep3CardStats {
|
||||
enum Rarity : uint8_t {
|
||||
N1 = 0x01,
|
||||
R1 = 0x02,
|
||||
S = 0x03,
|
||||
E = 0x04,
|
||||
N2 = 0x05,
|
||||
N3 = 0x06,
|
||||
N4 = 0x07,
|
||||
R2 = 0x08,
|
||||
R3 = 0x09,
|
||||
R4 = 0x0A,
|
||||
SS = 0x0B,
|
||||
D1 = 0x0C,
|
||||
D2 = 0x0D,
|
||||
INVIS = 0x0E,
|
||||
};
|
||||
|
||||
enum Type : uint8_t {
|
||||
SC_HUNTERS = 0x00, // No subtypes
|
||||
SC_ARKZ = 0x01, // No subtypes
|
||||
ITEM = 0x02, // Subtype 01 = sword, 02 = gun, 03 = cane. TODO: there are many more subtypes than those 3
|
||||
CREATURE = 0x03, // No subtypes (TODO: Where are attributes stored then?)
|
||||
ACTION = 0x04, // TODO: What do the subtypes mean? Are they actually flags instead?
|
||||
ASSIST = 0x05, // No subtypes
|
||||
};
|
||||
|
||||
struct Stat {
|
||||
enum Type : uint8_t {
|
||||
BLANK = 0,
|
||||
STAT = 1,
|
||||
PLUS_STAT = 2,
|
||||
MINUS_STAT = 3,
|
||||
EQUALS_STAT = 4,
|
||||
UNKNOWN = 5,
|
||||
PLUS_UNKNOWN = 6,
|
||||
MINUS_UNKNOWN = 7,
|
||||
EQUALS_UNKNOWN = 8,
|
||||
};
|
||||
be_uint16_t code;
|
||||
Type type;
|
||||
int8_t stat;
|
||||
|
||||
void decode_code();
|
||||
std::string str() const;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct Effect {
|
||||
uint8_t command;
|
||||
ptext<char, 0x0F> expr; // May be blank if the command doesn't use it
|
||||
uint8_t when;
|
||||
ptext<char, 4> arg1;
|
||||
ptext<char, 4> arg2;
|
||||
ptext<char, 4> arg3;
|
||||
parray<uint8_t, 3> unknown_a3;
|
||||
|
||||
bool is_empty() const;
|
||||
static std::string str_for_arg(const std::string& arg);
|
||||
std::string str() const;
|
||||
} __attribute__((packed));
|
||||
|
||||
be_uint32_t card_id;
|
||||
parray<uint8_t, 0x40> jp_name;
|
||||
int8_t type; // Type enum. If <0, then this is the end of the card list
|
||||
uint8_t self_cost; // ATK dice points required
|
||||
uint8_t ally_cost; // ATK points from allies required; PBs use this
|
||||
uint8_t unused_a0; // Always 0
|
||||
Stat hp;
|
||||
Stat ap;
|
||||
Stat tp;
|
||||
Stat mv;
|
||||
parray<uint8_t, 8> left_colors;
|
||||
parray<uint8_t, 8> right_colors;
|
||||
parray<uint8_t, 8> top_colors;
|
||||
parray<be_uint32_t, 6> range;
|
||||
be_uint32_t unused_a1; // Always 0
|
||||
// Target modes:
|
||||
// 00 = no targeting (used for defense cards, mags, shields, etc.)
|
||||
// 01 = single enemy
|
||||
// 02 = multiple enemies (with range)
|
||||
// 03 = self (assist)
|
||||
// 04 = team (assist)
|
||||
// 05 = everyone (assist)
|
||||
// 06 = multiple allies (with range); only used by Shifta
|
||||
// 07 = all allies including yourself; see Anti, Resta, Leilla
|
||||
// 08 = all (attack); see e.g. Last Judgment, Earthquake
|
||||
// 09 = your own FCs but not SCs; see Traitor
|
||||
uint8_t target_mode;
|
||||
uint8_t assist_turns; // 90 (dec) = once, 99 (dec) = forever
|
||||
uint8_t cannot_move; // 0 for SC and creature cards; 1 for everything else
|
||||
uint8_t cannot_attack; // 1 for shields, mags, defense actions, and assist cards
|
||||
uint8_t unused_a2; // Always 0
|
||||
uint8_t hide_in_deck_edit; // 0 = player can use this card (appears in deck edit)
|
||||
uint8_t subtype; // e.g. gun, sword, etc. (used for checking if SCs can use it)
|
||||
uint8_t rarity; // Rarity enum
|
||||
be_uint32_t unknown_a2;
|
||||
// These two fields seem to always contain the same value, and are always 0
|
||||
// for non-assist cards and nonzero for assists. Each assist card has a unique
|
||||
// value here and no effects, which makes it look like this is how assist
|
||||
// effects are implemented. There seems to be some 1k-modulation going on here
|
||||
// too; most cards are in the range 101-174 but a few have e.g. 1150, 2141. A
|
||||
// few pairs of cards have the same effect, which makes it look like some
|
||||
// other fields are also involved in determining their effects (see e.g. Skip
|
||||
// Draw / Skip Move, Dice Fever / Dice Fever +, Reverse Card / Rich +).
|
||||
parray<be_uint16_t, 2> assist_effect;
|
||||
parray<be_uint16_t, 2> unknown_a3;
|
||||
ptext<char, 0x14> name;
|
||||
ptext<char, 0x0B> jp_short_name;
|
||||
ptext<char, 0x07> short_name;
|
||||
be_uint16_t has_effects; // 1 if any of the following structs are not blank
|
||||
Effect effects[3];
|
||||
|
||||
void decode_range();
|
||||
std::string str() const;
|
||||
} __attribute__((packed)); // 0x128 bytes in total
|
||||
|
||||
struct Ep3CardStatsFooter {
|
||||
be_uint32_t num_cards1;
|
||||
be_uint32_t unknown_a1;
|
||||
be_uint32_t num_cards2;
|
||||
be_uint32_t unknown_a2[11];
|
||||
be_uint32_t unknown_offset_a3;
|
||||
be_uint32_t unknown_a4[3];
|
||||
be_uint32_t footer_offset;
|
||||
be_uint32_t unknown_a5[3];
|
||||
} __attribute__((packed));
|
||||
|
||||
struct Ep3Deck {
|
||||
ptext<char, 0x10> name;
|
||||
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
|
||||
// the SC card, which the game implicitly subtracts from the limit - so a
|
||||
// valid deck should actually have 31 cards in it.
|
||||
parray<le_uint16_t, 50> card_ids;
|
||||
be_uint32_t unknown_a1;
|
||||
// Last modification time
|
||||
le_uint16_t year;
|
||||
uint8_t month;
|
||||
uint8_t day;
|
||||
uint8_t hour;
|
||||
uint8_t minute;
|
||||
uint8_t second;
|
||||
uint8_t unknown_a2;
|
||||
} __attribute__((packed)); // 0x84 bytes in total
|
||||
|
||||
struct Ep3Config {
|
||||
// Offsets in comments in this struct are relative to start of 61/98 command
|
||||
/* 0728 */ parray<uint8_t, 0x1434> unknown_a1;
|
||||
/* 1B5C */ parray<Ep3Deck, 25> decks;
|
||||
/* 2840 */ uint64_t unknown_a2;
|
||||
/* 2848 */ be_uint32_t offline_clv_exp; // CLvOff = this / 100
|
||||
/* 284C */ be_uint32_t online_clv_exp; // CLvOn = this / 100
|
||||
/* 2850 */ parray<uint8_t, 0x14C> unknown_a3;
|
||||
/* 299C */ ptext<char, 0x10> name;
|
||||
// Other records are probably somewhere in here - e.g. win/loss, play time, etc.
|
||||
/* 29AC */ parray<uint8_t, 0xCC> unknown_a4;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct Ep3BattleRules {
|
||||
// When this structure is used in a map/quest definition, FF in any of these
|
||||
// fields means the user is allowed to override it. Any non-FF fields are
|
||||
// fixed for the map/quest and cannot be overridden.
|
||||
uint8_t overall_time_limit; // In increments of 5 minutes; 0 = unlimited
|
||||
uint8_t phase_time_limit; // In seconds; 0 = unlimited
|
||||
uint8_t allowed_cards; // 0 = any, 1 = N-rank only, 2 = N and R, 3 = N, R, and S
|
||||
uint8_t min_dice; // 0 = default (1)
|
||||
// 4
|
||||
uint8_t max_dice; // 0 = default (6)
|
||||
uint8_t disable_deck_shuffle; // 0 = shuffle on, 1 = off
|
||||
uint8_t disable_deck_loop; // 0 = loop on, 1 = off
|
||||
uint8_t char_hp;
|
||||
// 8
|
||||
uint8_t hp_type; // 0 = defeat player, 1 = defeat team, 2 = common hp
|
||||
uint8_t no_assist_cards; // 1 = assist cards disallowed
|
||||
uint8_t disable_dialogue; // 0 = dialogue on, 1 = dialogue off
|
||||
uint8_t dice_exchange_mode; // 0 = high attack, 1 = high defense, 2 = none
|
||||
// C
|
||||
uint8_t disable_dice_boost; // 0 = dice boost on, 1 = off
|
||||
parray<uint8_t, 3> unused;
|
||||
} __attribute__((packed));
|
||||
|
||||
|
||||
|
||||
struct Ep3MapList {
|
||||
be_uint32_t num_maps;
|
||||
be_uint32_t unknown_a1; // Always 0?
|
||||
be_uint32_t strings_offset; // From after total_size field (add 0x10 to this value)
|
||||
be_uint32_t total_size; // Including header, entries, and strings
|
||||
|
||||
struct Entry { // Should be 0x220 bytes in total
|
||||
// These 3 fields probably include the location ID (scenery to load) and the
|
||||
// music ID
|
||||
be_uint16_t map_x;
|
||||
be_uint16_t map_y;
|
||||
be_uint16_t scene_data2;
|
||||
be_uint16_t map_number;
|
||||
// Text offsets are from the beginning of the strings block after all map
|
||||
// entries (that is, add strings_offset to them to get the string offset)
|
||||
be_uint32_t name_offset;
|
||||
be_uint32_t location_name_offset;
|
||||
be_uint32_t quest_name_offset;
|
||||
be_uint32_t description_offset;
|
||||
be_uint16_t width;
|
||||
be_uint16_t height;
|
||||
parray<uint8_t, 0x100> map_tiles;
|
||||
parray<uint8_t, 0x100> modification_tiles;
|
||||
be_uint32_t unknown_a2; // Seems to always be 0xFF000000
|
||||
} __attribute__((packed));
|
||||
|
||||
// Variable-length fields:
|
||||
// Entry entries[num_maps];
|
||||
// char strings[...EOF]; // Null-terminated strings, pointed to by offsets in Entry structs
|
||||
} __attribute__((packed));
|
||||
|
||||
struct Ep3CompressedMapHeader { // .mnm file format
|
||||
le_uint32_t map_number;
|
||||
le_uint32_t compressed_data_size;
|
||||
// Compressed data immediately follows (which decompresses to an Ep3Map)
|
||||
} __attribute__((packed));
|
||||
|
||||
struct Ep3Map { // .mnmd format
|
||||
/* 0000 */ be_uint32_t unknown_a1;
|
||||
/* 0004 */ be_uint32_t map_number;
|
||||
/* 0008 */ uint8_t width;
|
||||
/* 0009 */ uint8_t height;
|
||||
/* 000A */ uint8_t scene_data2; // TODO: What is this?
|
||||
// All alt_maps fields (including the floats) past num_alt_maps are filled in
|
||||
// with FF. For example, if num_alt_maps == 8, the last two fields in each
|
||||
// alt_maps array are filled with FF.
|
||||
/* 000B */ uint8_t num_alt_maps; // TODO: What are the alt maps for?
|
||||
// In the map_tiles array, the values are:
|
||||
// 00 = not a valid tile
|
||||
// 01 = valid tile unless punched out (later)
|
||||
// 02 = team A start (1v1)
|
||||
// 03, 04 = team A start (2v2)
|
||||
// 05 = ???
|
||||
// 06, 07 = team B start (2v2)
|
||||
// 08 = team B start (1v1)
|
||||
// Note that the game displays the map reversed vertically in the preview
|
||||
// window. For example, player 1 is on team A, which usually starts at the top
|
||||
// of the map as defined in this struct, or at the bottom as shown in the
|
||||
// preview window.
|
||||
/* 000C */ parray<uint8_t, 0x100> map_tiles;
|
||||
/* 010C */ parray<uint8_t, 0x0C> unknown_a2;
|
||||
/* 0118 */ parray<uint8_t, 0x100> alt_maps1[0x0A];
|
||||
/* 0B18 */ parray<uint8_t, 0x100> alt_maps2[0x0A];
|
||||
/* 1518 */ parray<be_float, 0x12> alt_maps_unknown_a3[0x0A];
|
||||
/* 17E8 */ parray<be_float, 0x12> alt_maps_unknown_a4[0x0A];
|
||||
/* 1AB8 */ parray<be_float, 0x6C> unknown_a5;
|
||||
// In the modification_tiles array, the values are:
|
||||
// 10 = blocked (as if the corresponding map_tiles value was 00)
|
||||
// 20 = blocked (maybe one of 10 or 20 are passable by Aerial characters though)
|
||||
// 30, 31 = teleporters (green, red)
|
||||
// 40-44 = ???? (used in 244, 2E4, 2F9)
|
||||
// 50 = appears as improperly-z-buffered teal cube in preview
|
||||
// TODO: There may be more values that are valid here.
|
||||
/* 1C68 */ parray<uint8_t, 0x100> modification_tiles;
|
||||
/* 1D68 */ parray<uint8_t, 0x74> unknown_a6;
|
||||
/* 1DDC */ Ep3BattleRules default_rules;
|
||||
/* 1DEC */ parray<uint8_t, 4> unknown_a7;
|
||||
/* 1DF0 */ ptext<char, 0x14> name;
|
||||
/* 1E04 */ ptext<char, 0x14> location_name;
|
||||
/* 1E18 */ ptext<char, 0x3C> quest_name; // == location_name if not a quest
|
||||
/* 1E54 */ ptext<char, 0x190> description;
|
||||
/* 1FE4 */ be_uint16_t map_x;
|
||||
/* 1FE6 */ be_uint16_t map_y;
|
||||
struct NPCDeck {
|
||||
ptext<char, 0x18> name;
|
||||
parray<be_uint16_t, 0x20> card_ids; // Last one appears to always be FFFF
|
||||
} __attribute__((packed));
|
||||
/* 1FE8 */ NPCDeck npc_decks[3]; // Unused if name[0] == 0
|
||||
struct NPCCharacter {
|
||||
parray<be_uint16_t, 2> unknown_a1;
|
||||
parray<uint8_t, 4> unknown_a2;
|
||||
ptext<char, 0x10> name;
|
||||
parray<be_uint16_t, 0x7E> unknown_a3;
|
||||
} __attribute__((packed));
|
||||
/* 20F0 */ NPCCharacter npc_chars[3]; // Unused if name[0] == 0
|
||||
/* 242C */ parray<uint8_t, 0x14> unknown_a8; // Always FF?
|
||||
/* 2440 */ ptext<char, 0x190> before_message;
|
||||
/* 25D0 */ ptext<char, 0x190> after_message;
|
||||
/* 2760 */ ptext<char, 0x190> dispatch_message; // Usually "You can only dispatch <character>" or blank
|
||||
struct DialogueSet {
|
||||
be_uint16_t unknown_a1;
|
||||
be_uint16_t unknown_a2; // Always 0x0064 if valid, 0xFFFF if unused?
|
||||
ptext<char, 0x40> strings[4];
|
||||
} __attribute__((packed)); // Total size: 0x104 bytes
|
||||
/* 28F0 */ DialogueSet dialogue_sets[3][0x10]; // Up to 0x10 per valid NPC
|
||||
/* 59B0 */ be_uint16_t reward_card_id; // TODO: This could be an array. The only examples I've seen have only one here
|
||||
/* 59B2 */ parray<be_uint16_t, 0x33> unknown_a9;
|
||||
/* 5A18 */
|
||||
} __attribute__((packed));
|
||||
|
||||
class Ep3DataIndex {
|
||||
public:
|
||||
explicit Ep3DataIndex(const std::string& directory);
|
||||
|
||||
struct CardEntry {
|
||||
Ep3CardStats stats;
|
||||
std::vector<std::string> text;
|
||||
};
|
||||
|
||||
class MapEntry {
|
||||
public:
|
||||
Ep3Map map;
|
||||
|
||||
MapEntry(const Ep3Map& map);
|
||||
MapEntry(const std::string& compressed_data);
|
||||
|
||||
std::string compressed() const;
|
||||
|
||||
private:
|
||||
mutable std::string compressed_data;
|
||||
};
|
||||
|
||||
const std::string& get_compressed_card_definitions() const;
|
||||
std::shared_ptr<const CardEntry> get_card_definition(uint32_t id) const;
|
||||
|
||||
const std::string& get_compressed_map_list() const;
|
||||
std::shared_ptr<const MapEntry> get_map(uint32_t id) const;
|
||||
|
||||
private:
|
||||
std::string compressed_card_definitions;
|
||||
std::unordered_map<uint32_t, std::shared_ptr<CardEntry>> card_definitions;
|
||||
|
||||
// The compressed map list is generated on demand from the maps map below.
|
||||
// It's marked mutable because the logical consistency of the Ep3DataIndex
|
||||
// object is not violated from the caller's perspective even if we don't
|
||||
// generate the compressed map list at load time.
|
||||
mutable std::string compressed_map_list;
|
||||
std::map<uint32_t, std::shared_ptr<MapEntry>> maps;
|
||||
};
|
||||
@@ -0,0 +1,286 @@
|
||||
#include "AssistServer.hh"
|
||||
|
||||
#include "Server.hh"
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace Episode3 {
|
||||
|
||||
|
||||
|
||||
// Note: This order matches the order that the cards are defined in the original
|
||||
// code. This is relevant for consistency of results when choosing a random card
|
||||
// (for God Whim).
|
||||
const vector<uint16_t> ALL_ASSIST_CARD_IDS = {
|
||||
0x0018, 0x0019, 0x001A, 0x00F5, 0x00F6, 0x00F7, 0x00F8, 0x00F9, 0x00FA,
|
||||
0x00FB, 0x00FC, 0x00FD, 0x00FE, 0x00FF, 0x0100, 0x0101, 0x0102, 0x0103,
|
||||
0x0104, 0x0105, 0x0106, 0x0107, 0x0108, 0x0109, 0x010A, 0x010B, 0x010C,
|
||||
0x010D, 0x010E, 0x010F, 0x0121, 0x0125, 0x0126, 0x0127, 0x0128, 0x0129,
|
||||
0x012A, 0x012B, 0x012C, 0x012D, 0x012E, 0x012F, 0x0130, 0x0131, 0x0132,
|
||||
0x0133, 0x0134, 0x0135, 0x0136, 0x0137, 0x0138, 0x0139, 0x013A, 0x013B,
|
||||
0x013C, 0x013D, 0x013E, 0x013F, 0x0140, 0x0141, 0x0142, 0x0143, 0x0144,
|
||||
0x0145, 0x0146, 0x0148, 0x014A, 0x014B, 0x014C, 0x014D, 0x014E, 0x023F,
|
||||
0x0240, 0x0241, 0x0242};
|
||||
|
||||
AssistEffect assist_effect_number_for_card_id(uint16_t card_id) {
|
||||
static const unordered_map<uint16_t, AssistEffect> card_id_to_effect({
|
||||
{0x00F5, /* 0x0001 */ AssistEffect::DICE_HALF},
|
||||
{0x00F6, /* 0x0002 */ AssistEffect::DICE_PLUS_1},
|
||||
{0x00F7, /* 0x0003 */ AssistEffect::DICE_FEVER},
|
||||
{0x00F8, /* 0x0004 */ AssistEffect::CARD_RETURN},
|
||||
{0x00F9, /* 0x0005 */ AssistEffect::LAND_PRICE},
|
||||
{0x00FA, /* 0x0006 */ AssistEffect::POWERLESS_RAIN},
|
||||
{0x00FB, /* 0x0007 */ AssistEffect::BRAVE_WIND},
|
||||
{0x00FC, /* 0x0008 */ AssistEffect::SILENT_COLOSSEUM},
|
||||
{0x00FD, /* 0x0009 */ AssistEffect::RESISTANCE},
|
||||
{0x00FE, /* 0x000A */ AssistEffect::INDEPENDENT},
|
||||
{0x00FF, /* 0x000B */ AssistEffect::ASSISTLESS},
|
||||
{0x0100, /* 0x000C */ AssistEffect::ATK_DICE_2},
|
||||
{0x0101, /* 0x000D */ AssistEffect::DEFLATION},
|
||||
{0x0102, /* 0x000E */ AssistEffect::INFLATION},
|
||||
{0x0103, /* 0x000F */ AssistEffect::EXCHANGE},
|
||||
{0x0104, /* 0x0010 */ AssistEffect::INFLUENCE},
|
||||
{0x0105, /* 0x0011 */ AssistEffect::SKIP_SET},
|
||||
{0x0106, /* 0x0012 */ AssistEffect::SKIP_MOVE},
|
||||
{0x0121, /* 0x0013 */ AssistEffect::SKIP_ACT},
|
||||
{0x0137, /* 0x0014 */ AssistEffect::SKIP_DRAW},
|
||||
{0x0107, /* 0x0015 */ AssistEffect::FLY},
|
||||
{0x0108, /* 0x0016 */ AssistEffect::NECROMANCER},
|
||||
{0x0109, /* 0x0017 */ AssistEffect::PERMISSION},
|
||||
{0x010A, /* 0x0018 */ AssistEffect::SHUFFLE_ALL},
|
||||
{0x010B, /* 0x0019 */ AssistEffect::LEGACY},
|
||||
{0x010C, /* 0x001A */ AssistEffect::ASSIST_REVERSE},
|
||||
{0x010D, /* 0x001B */ AssistEffect::STAMINA},
|
||||
{0x010E, /* 0x001C */ AssistEffect::AP_ABSORPTION},
|
||||
{0x010F, /* 0x001D */ AssistEffect::HEAVY_FOG},
|
||||
{0x0125, /* 0x001E */ AssistEffect::TRASH_1},
|
||||
{0x0126, /* 0x001F */ AssistEffect::EMPTY_HAND},
|
||||
{0x0127, /* 0x0020 */ AssistEffect::HITMAN},
|
||||
{0x0128, /* 0x0021 */ AssistEffect::ASSIST_TRASH},
|
||||
{0x0129, /* 0x0022 */ AssistEffect::SHUFFLE_GROUP},
|
||||
{0x012A, /* 0x0023 */ AssistEffect::ASSIST_VANISH},
|
||||
{0x012B, /* 0x0024 */ AssistEffect::CHARITY},
|
||||
{0x012C, /* 0x0025 */ AssistEffect::INHERITANCE},
|
||||
{0x012D, /* 0x0026 */ AssistEffect::FIX},
|
||||
{0x012E, /* 0x0027 */ AssistEffect::MUSCULAR},
|
||||
{0x012F, /* 0x0028 */ AssistEffect::CHANGE_BODY},
|
||||
{0x0130, /* 0x0029 */ AssistEffect::GOD_WHIM},
|
||||
{0x0131, /* 0x002A */ AssistEffect::GOLD_RUSH},
|
||||
{0x0132, /* 0x002B */ AssistEffect::ASSIST_RETURN},
|
||||
{0x0133, /* 0x002C */ AssistEffect::REQUIEM},
|
||||
{0x0134, /* 0x002D */ AssistEffect::RANSOM},
|
||||
{0x0135, /* 0x002E */ AssistEffect::SIMPLE},
|
||||
{0x0136, /* 0x002F */ AssistEffect::SLOW_TIME},
|
||||
{0x023F, /* 0x0030 */ AssistEffect::QUICK_TIME},
|
||||
{0x0138, /* 0x0031 */ AssistEffect::TERRITORY},
|
||||
{0x0139, /* 0x0032 */ AssistEffect::OLD_TYPE},
|
||||
{0x013A, /* 0x0033 */ AssistEffect::FLATLAND},
|
||||
{0x013B, /* 0x0034 */ AssistEffect::IMMORTALITY},
|
||||
{0x013C, /* 0x0035 */ AssistEffect::SNAIL_PACE},
|
||||
{0x013D, /* 0x0036 */ AssistEffect::TECH_FIELD},
|
||||
{0x013E, /* 0x0037 */ AssistEffect::FOREST_RAIN},
|
||||
{0x013F, /* 0x0038 */ AssistEffect::CAVE_WIND},
|
||||
{0x0140, /* 0x0039 */ AssistEffect::MINE_BRIGHTNESS},
|
||||
{0x0141, /* 0x003A */ AssistEffect::RUIN_DARKNESS},
|
||||
{0x0142, /* 0x003B */ AssistEffect::SABER_DANCE},
|
||||
{0x0143, /* 0x003C */ AssistEffect::BULLET_STORM},
|
||||
{0x0144, /* 0x003D */ AssistEffect::CANE_PALACE},
|
||||
{0x0145, /* 0x003E */ AssistEffect::GIANT_GARDEN},
|
||||
{0x0146, /* 0x003F */ AssistEffect::MARCH_OF_THE_MEEK},
|
||||
{0x0148, /* 0x0040 */ AssistEffect::SUPPORT},
|
||||
{0x014A, /* 0x0041 */ AssistEffect::RICH},
|
||||
{0x014B, /* 0x0042 */ AssistEffect::REVERSE_CARD},
|
||||
{0x014C, /* 0x0043 */ AssistEffect::VENGEANCE},
|
||||
{0x014D, /* 0x0044 */ AssistEffect::SQUEEZE},
|
||||
{0x014E, /* 0x0045 */ AssistEffect::HOMESICK},
|
||||
{0x0240, /* 0x0046 */ AssistEffect::BOMB},
|
||||
{0x0241, /* 0x0047 */ AssistEffect::SKIP_TURN},
|
||||
{0x0242, /* 0x0048 */ AssistEffect::BATTLE_ROYALE},
|
||||
{0x0018, /* 0x0049 */ AssistEffect::DICE_FEVER_PLUS},
|
||||
{0x0019, /* 0x004A */ AssistEffect::RICH_PLUS},
|
||||
{0x001A, /* 0x004B */ AssistEffect::CHARITY_PLUS},
|
||||
});
|
||||
try {
|
||||
return card_id_to_effect.at(card_id);
|
||||
} catch (const out_of_range&) {
|
||||
return AssistEffect::NONE;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
AssistServer::AssistServer(shared_ptr<Server> server)
|
||||
: w_server(server),
|
||||
assist_effects(AssistEffect::NONE),
|
||||
num_assist_cards_set(0),
|
||||
client_ids_with_assists(0xFF),
|
||||
active_assist_effects(AssistEffect::NONE),
|
||||
num_active_assists(0) { }
|
||||
|
||||
shared_ptr<Server> AssistServer::server() {
|
||||
auto s = this->w_server.lock();
|
||||
if (!s) {
|
||||
throw runtime_error("server is deleted");
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
shared_ptr<const Server> AssistServer::server() const {
|
||||
auto s = this->w_server.lock();
|
||||
if (!s) {
|
||||
throw runtime_error("server is deleted");
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
uint16_t AssistServer::card_id_for_card_ref(uint16_t card_ref) const {
|
||||
return this->server()->card_id_for_card_ref(card_ref);
|
||||
}
|
||||
|
||||
shared_ptr<const DataIndex::CardEntry> AssistServer::definition_for_card_id(
|
||||
uint16_t card_id) const {
|
||||
return this->server()->definition_for_card_id(card_id);
|
||||
}
|
||||
|
||||
uint32_t AssistServer::compute_num_assist_effects_for_client(uint16_t client_id) {
|
||||
this->populate_effects();
|
||||
this->num_assist_cards_set = 0;
|
||||
if (this->should_block_assist_effects_for_client(client_id)) {
|
||||
this->num_active_assists = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (size_t z = 0; z < 4; z++) {
|
||||
auto ce = this->assist_card_defs[z];
|
||||
auto hes = this->hand_and_equip_states[z];
|
||||
if (ce && (!hes || (hes->assist_delay_turns < 1))) {
|
||||
bool affected = false;
|
||||
if (ce->def.target_mode == TargetMode::TEAM) {
|
||||
auto this_deck_entry = this->deck_entries[client_id];
|
||||
auto other_deck_entry = this->deck_entries[z];
|
||||
if (this_deck_entry && other_deck_entry &&
|
||||
(this_deck_entry->team_id == other_deck_entry->team_id)) {
|
||||
affected = true;
|
||||
}
|
||||
} else if ((ce->def.target_mode == TargetMode::SELF) && (z == client_id)) {
|
||||
affected = true;
|
||||
} else if (ce->def.target_mode == TargetMode::EVERYONE) {
|
||||
affected = true;
|
||||
}
|
||||
if (affected) {
|
||||
this->client_ids_with_assists[this->num_assist_cards_set++] = z;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this->recompute_effects();
|
||||
return this->num_assist_cards_set;
|
||||
}
|
||||
|
||||
uint32_t AssistServer::compute_num_assist_effects_for_team(uint32_t team_id) {
|
||||
this->num_assist_cards_set = 0;
|
||||
for (size_t z = 0; z < 4; z++) {
|
||||
auto ce = this->assist_card_defs[z];
|
||||
auto hes = this->hand_and_equip_states[z];
|
||||
if (ce && (!hes || (hes->assist_delay_turns < 1))) {
|
||||
bool affected = false;
|
||||
if (ce->def.target_mode == TargetMode::TEAM) {
|
||||
if (this->deck_entries[z] && (this->deck_entries[z]->team_id == team_id)) {
|
||||
affected = true;
|
||||
}
|
||||
} else if (ce->def.target_mode == TargetMode::EVERYONE) {
|
||||
affected = true;
|
||||
}
|
||||
if (affected) {
|
||||
this->client_ids_with_assists[this->num_assist_cards_set++] = z;
|
||||
}
|
||||
}
|
||||
}
|
||||
this->recompute_effects();
|
||||
return this->num_assist_cards_set;
|
||||
}
|
||||
|
||||
bool AssistServer::should_block_assist_effects_for_client(uint16_t client_id) const {
|
||||
for (size_t z = 0; z < 4; z++) {
|
||||
auto eff = this->assist_effects[z];
|
||||
auto ce = this->assist_card_defs[z];
|
||||
if (((eff == AssistEffect::RESISTANCE) || (eff == AssistEffect::INDEPENDENT)) && ce) {
|
||||
if (ce->def.target_mode == TargetMode::TEAM) {
|
||||
if (this->deck_entries[client_id] && this->deck_entries[z] &&
|
||||
(this->deck_entries[client_id]->team_id == this->deck_entries[z]->team_id)) {
|
||||
return true;
|
||||
}
|
||||
} else if ((ce->def.target_mode == TargetMode::SELF) && (client_id == z)) {
|
||||
return true;
|
||||
} else if (ce->def.target_mode == TargetMode::EVERYONE) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
AssistEffect AssistServer::get_active_assist_by_index(size_t index) const {
|
||||
if (index < this->num_active_assists) {
|
||||
return this->active_assist_effects[index];
|
||||
}
|
||||
return AssistEffect::NONE;
|
||||
}
|
||||
|
||||
void AssistServer::populate_effects() {
|
||||
for (size_t z = 0; z < 4; z++) {
|
||||
this->assist_card_defs[z] = nullptr;
|
||||
this->assist_effects[z] = AssistEffect::NONE;
|
||||
const auto& hes = this->hand_and_equip_states[z];
|
||||
if (hes) {
|
||||
uint16_t card_id = hes->assist_card_id == 0xFFFF
|
||||
? this->card_id_for_card_ref(hes->assist_card_ref)
|
||||
: hes->assist_card_id.load();
|
||||
this->assist_effects[z] = assist_effect_number_for_card_id(card_id);
|
||||
if (this->assist_effects[z] != AssistEffect::NONE) {
|
||||
this->assist_card_defs[z] = this->definition_for_card_id(card_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AssistServer::recompute_effects() {
|
||||
for (size_t z = 0; z < 4; z++) {
|
||||
this->active_assist_effects[z] = AssistEffect::NONE;
|
||||
this->active_assist_card_defs[z] = nullptr;
|
||||
}
|
||||
this->num_active_assists = 0;
|
||||
|
||||
if (this->num_assist_cards_set != 0) {
|
||||
for (size_t z = 0; z < this->num_assist_cards_set; z++) {
|
||||
auto eff = this->assist_effects[this->client_ids_with_assists[z]];
|
||||
if (eff == AssistEffect::RESISTANCE || eff == AssistEffect::INDEPENDENT) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Note: this->num_assist_cards_set is > 0 when we get here
|
||||
for (size_t z = 0; z < this->num_assist_cards_set - 1; z++) {
|
||||
for (size_t w = z + 1; w < this->num_assist_cards_set; w++) {
|
||||
uint8_t z_client_id = this->client_ids_with_assists[z];
|
||||
uint8_t w_client_id = this->client_ids_with_assists[w];
|
||||
if (this->hand_and_equip_states[w_client_id]->assist_card_set_number <
|
||||
this->hand_and_equip_states[z_client_id]->assist_card_set_number) {
|
||||
this->client_ids_with_assists[z] = w_client_id;
|
||||
this->client_ids_with_assists[w] = z_client_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this->num_active_assists = this->num_assist_cards_set;
|
||||
for (size_t z = 0; z < this->num_assist_cards_set; z++) {
|
||||
this->active_assist_effects[z] = this->assist_effects[this->client_ids_with_assists[z]];
|
||||
this->active_assist_card_defs[z] = this->assist_card_defs[this->client_ids_with_assists[z]];
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace Episode3
|
||||
@@ -0,0 +1,60 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "DataIndex.hh"
|
||||
#include "PlayerState.hh"
|
||||
#include "DeckState.hh"
|
||||
|
||||
namespace Episode3 {
|
||||
|
||||
|
||||
|
||||
class Server;
|
||||
|
||||
extern const std::vector<uint16_t> ALL_ASSIST_CARD_IDS;
|
||||
|
||||
AssistEffect assist_effect_number_for_card_id(uint16_t card_id);
|
||||
|
||||
class AssistServer {
|
||||
public:
|
||||
explicit AssistServer(std::shared_ptr<Server> server);
|
||||
std::shared_ptr<Server> server();
|
||||
std::shared_ptr<const Server> server() const;
|
||||
|
||||
uint16_t card_id_for_card_ref(uint16_t card_ref) const;
|
||||
std::shared_ptr<const DataIndex::CardEntry> definition_for_card_id(uint16_t card_id) const;
|
||||
|
||||
uint32_t compute_num_assist_effects_for_client(uint16_t client_id);
|
||||
uint32_t compute_num_assist_effects_for_team(uint32_t team_id);
|
||||
|
||||
bool should_block_assist_effects_for_client(uint16_t client_id) const;
|
||||
AssistEffect get_active_assist_by_index(size_t index) const;
|
||||
|
||||
void populate_effects();
|
||||
void recompute_effects();
|
||||
|
||||
private:
|
||||
std::weak_ptr<Server> w_server;
|
||||
|
||||
public:
|
||||
parray<AssistEffect, 4> assist_effects;
|
||||
std::shared_ptr<const DataIndex::CardEntry> assist_card_defs[4];
|
||||
uint32_t num_assist_cards_set;
|
||||
parray<uint8_t, 4> client_ids_with_assists;
|
||||
parray<AssistEffect, 4> active_assist_effects;
|
||||
std::shared_ptr<const DataIndex::CardEntry> active_assist_card_defs[4];
|
||||
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];
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Episode3
|
||||
@@ -0,0 +1,382 @@
|
||||
#include "BattleRecord.hh"
|
||||
|
||||
#include <phosg/Time.hh>
|
||||
|
||||
#include "../CommandFormats.hh"
|
||||
#include "../SendCommands.hh"
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace Episode3 {
|
||||
|
||||
|
||||
|
||||
BattleRecord::Event::Event(StringReader& r) {
|
||||
this->type = r.get<Event::Type>();
|
||||
this->timestamp = r.get_u64l();
|
||||
switch (this->type) {
|
||||
case Event::Type::PLAYER_JOIN:
|
||||
this->players.emplace_back(r.get<PlayerEntry>());
|
||||
break;
|
||||
case Event::Type::PLAYER_LEAVE:
|
||||
this->leaving_client_id = r.get_u8();
|
||||
break;
|
||||
case Event::Type::SET_INITIAL_PLAYERS: {
|
||||
uint8_t count = r.get_u8();
|
||||
while (this->players.size() < count) {
|
||||
this->players.emplace_back(r.get<PlayerEntry>());
|
||||
}
|
||||
break;
|
||||
}
|
||||
case Event::Type::CHAT_MESSAGE:
|
||||
this->guild_card_number = r.get_u32l();
|
||||
[[fallthrough]];
|
||||
case Event::Type::GAME_COMMAND:
|
||||
case Event::Type::BATTLE_COMMAND:
|
||||
case Event::Type::EP3_GAME_COMMAND:
|
||||
this->data = r.read(r.get_u16l());
|
||||
break;
|
||||
default:
|
||||
throw logic_error("unknown event type");
|
||||
}
|
||||
}
|
||||
|
||||
void BattleRecord::Event::serialize(StringWriter& w) const {
|
||||
w.put(this->type);
|
||||
w.put_u64l(this->timestamp);
|
||||
switch (this->type) {
|
||||
case Event::Type::PLAYER_JOIN:
|
||||
if (this->players.size() != 1) {
|
||||
throw logic_error("player join event does not contain 1 player entry");
|
||||
}
|
||||
w.put(this->players[0]);
|
||||
break;
|
||||
case Event::Type::PLAYER_LEAVE:
|
||||
w.put_u8(this->leaving_client_id);
|
||||
break;
|
||||
case Event::Type::SET_INITIAL_PLAYERS:
|
||||
w.put_u8(this->players.size());
|
||||
for (const auto& player : this->players) {
|
||||
w.put(player);
|
||||
}
|
||||
break;
|
||||
case Event::Type::CHAT_MESSAGE:
|
||||
w.put_u32l(this->guild_card_number);
|
||||
[[fallthrough]];
|
||||
case Event::Type::GAME_COMMAND:
|
||||
case Event::Type::BATTLE_COMMAND:
|
||||
case Event::Type::EP3_GAME_COMMAND:
|
||||
w.put_u16l(this->data.size());
|
||||
w.write(this->data);
|
||||
break;
|
||||
default:
|
||||
throw logic_error("unknown event type");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
BattleRecord::BattleRecord(uint32_t behavior_flags)
|
||||
: is_writable(true),
|
||||
behavior_flags(behavior_flags),
|
||||
battle_start_timestamp(0),
|
||||
battle_end_timestamp(0) { }
|
||||
|
||||
BattleRecord::BattleRecord(const string& data)
|
||||
: is_writable(false),
|
||||
behavior_flags(0),
|
||||
battle_start_timestamp(0),
|
||||
battle_end_timestamp(0) {
|
||||
StringReader r(data);
|
||||
uint64_t signature = r.get_u64l();
|
||||
if (signature != this->SIGNATURE) {
|
||||
throw runtime_error("incorrect battle record signature");
|
||||
}
|
||||
|
||||
this->battle_start_timestamp = r.get_u64l();
|
||||
this->battle_end_timestamp = r.get_u64l();
|
||||
this->behavior_flags = r.get_u32l();
|
||||
while (!r.eof()) {
|
||||
this->events.emplace_back(r);
|
||||
}
|
||||
}
|
||||
|
||||
string BattleRecord::serialize() const {
|
||||
StringWriter w;
|
||||
w.put_u64l(this->SIGNATURE);
|
||||
w.put_u64l(this->battle_start_timestamp);
|
||||
w.put_u64l(this->battle_end_timestamp);
|
||||
w.put_u32l(this->behavior_flags);
|
||||
for (const auto& ev : this->events) {
|
||||
ev.serialize(w);
|
||||
}
|
||||
return move(w.str());
|
||||
}
|
||||
|
||||
bool BattleRecord::writable() const {
|
||||
return this->is_writable;
|
||||
}
|
||||
|
||||
bool BattleRecord::battle_in_progress() const {
|
||||
return (this->battle_start_timestamp != 0);
|
||||
}
|
||||
|
||||
const BattleRecord::Event* BattleRecord::get_first_event() const {
|
||||
if (this->events.empty()) {
|
||||
return nullptr;
|
||||
}
|
||||
return &this->events.front();
|
||||
}
|
||||
|
||||
|
||||
void BattleRecord::add_player(
|
||||
const PlayerLobbyDataDCGC& lobby_data,
|
||||
const PlayerInventory& inventory,
|
||||
const PlayerDispDataDCPCV3& disp) {
|
||||
if (!this->is_writable) {
|
||||
throw logic_error("cannot write to battle record");
|
||||
}
|
||||
if (this->battle_start_timestamp != 0) {
|
||||
throw runtime_error("cannot add player during battle");
|
||||
}
|
||||
Event& ev = this->events.emplace_back();
|
||||
ev.type = Event::Type::PLAYER_JOIN;
|
||||
ev.timestamp = now();
|
||||
auto& player = ev.players.emplace_back();
|
||||
player.lobby_data = lobby_data;
|
||||
player.inventory = inventory;
|
||||
player.disp = disp;
|
||||
}
|
||||
|
||||
void BattleRecord::delete_player(uint8_t client_id) {
|
||||
if (!this->is_writable) {
|
||||
throw logic_error("cannot write to battle record");
|
||||
}
|
||||
Event& ev = this->events.emplace_back();
|
||||
ev.type = Event::Type::PLAYER_LEAVE;
|
||||
ev.timestamp = now();
|
||||
ev.leaving_client_id = client_id;
|
||||
}
|
||||
|
||||
void BattleRecord::add_command(Event::Type type, const void* data, size_t size) {
|
||||
if (!this->is_writable) {
|
||||
throw logic_error("cannot write to battle record");
|
||||
}
|
||||
Event& ev = this->events.emplace_back();
|
||||
ev.type = type;
|
||||
ev.timestamp = now();
|
||||
ev.data.assign(reinterpret_cast<const char*>(data), size);
|
||||
}
|
||||
|
||||
void BattleRecord::add_command(Event::Type type, string&& data) {
|
||||
if (!this->is_writable) {
|
||||
throw logic_error("cannot write to battle record");
|
||||
}
|
||||
Event& ev = this->events.emplace_back();
|
||||
ev.type = type;
|
||||
ev.timestamp = now();
|
||||
ev.data = move(data);
|
||||
}
|
||||
|
||||
void BattleRecord::add_chat_message(
|
||||
uint32_t guild_card_number, string&& data) {
|
||||
if (!this->is_writable) {
|
||||
throw logic_error("cannot write to battle record");
|
||||
}
|
||||
Event& ev = this->events.emplace_back();
|
||||
ev.type = Event::Type::CHAT_MESSAGE;
|
||||
ev.timestamp = now();
|
||||
ev.guild_card_number = guild_card_number;
|
||||
ev.data = move(data);
|
||||
}
|
||||
|
||||
bool BattleRecord::is_map_definition_event(const Event& ev) {
|
||||
if (ev.type == Event::Type::BATTLE_COMMAND) {
|
||||
auto& header = check_size_t<G_CardBattleCommandHeader>(
|
||||
ev.data, sizeof(G_CardBattleCommandHeader), 0xFFFF);
|
||||
if (header.subcommand == 0xB6) {
|
||||
auto& header = check_size_t<G_MapSubsubcommand_GC_Ep3_6xB6>(
|
||||
ev.data, sizeof(G_MapSubsubcommand_GC_Ep3_6xB6), 0xFFFF);
|
||||
if (header.subsubcommand == 0x41) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void BattleRecord::set_battle_start_timestamp() {
|
||||
if (this->battle_start_timestamp != 0) {
|
||||
throw logic_error("battle start timestamp is already set");
|
||||
}
|
||||
this->battle_start_timestamp = now();
|
||||
|
||||
// First, find the correct map definition subcommand to keep, and execute
|
||||
// player join/leave events to get the present players
|
||||
size_t num_map_events = 0;
|
||||
PlayerEntry players[4];
|
||||
bool players_present[4];
|
||||
for (auto& ev : this->events) {
|
||||
if (ev.type == Event::Type::PLAYER_JOIN) {
|
||||
if (ev.players.size() != 1) {
|
||||
throw logic_error("player join event does not contain 1 player entry");
|
||||
}
|
||||
auto& player = ev.players[0];
|
||||
if (player.lobby_data.client_id >= 4) {
|
||||
throw runtime_error("invalid client ID");
|
||||
}
|
||||
players[player.lobby_data.client_id] = player;
|
||||
players_present[player.lobby_data.client_id] = true;
|
||||
|
||||
} else if (ev.type == Event::Type::PLAYER_LEAVE) {
|
||||
if (ev.leaving_client_id >= 4) {
|
||||
throw logic_error("invalid client ID");
|
||||
}
|
||||
players_present[ev.leaving_client_id] = false;
|
||||
|
||||
} else if (ev.type == Event::Type::SET_INITIAL_PLAYERS) {
|
||||
throw logic_error("BattleRecord::set_battle_start_timestamp called twice");
|
||||
|
||||
} else if (this->is_map_definition_event(ev)) {
|
||||
num_map_events++;
|
||||
}
|
||||
}
|
||||
|
||||
deque<Event> new_events;
|
||||
|
||||
// Generate the initial players event
|
||||
Event initial_ev;
|
||||
initial_ev.type = Event::Type::SET_INITIAL_PLAYERS;
|
||||
initial_ev.timestamp = this->battle_start_timestamp;
|
||||
for (size_t z = 0; z < 4; z++) {
|
||||
if (players_present[z]) {
|
||||
initial_ev.players.emplace_back(players[z]);
|
||||
}
|
||||
}
|
||||
new_events.emplace_back(move(initial_ev));
|
||||
|
||||
// Skip all events before the last map definition event, and only retain
|
||||
// battle commands between then and now (since these battle commands will all
|
||||
// be replayed at once)
|
||||
auto it = this->events.begin();
|
||||
for (; it != this->events.end(); it++) {
|
||||
if (this->is_map_definition_event(*it)) {
|
||||
num_map_events--;
|
||||
if (num_map_events == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (; it != this->events.end(); it++) {
|
||||
if (it->type == Event::Type::BATTLE_COMMAND) {
|
||||
new_events.emplace_back(move(*it));
|
||||
}
|
||||
}
|
||||
this->events = move(new_events);
|
||||
}
|
||||
|
||||
void BattleRecord::set_battle_end_timestamp() {
|
||||
this->battle_end_timestamp = now();
|
||||
}
|
||||
|
||||
|
||||
|
||||
BattleRecordPlayer::BattleRecordPlayer(
|
||||
shared_ptr<const BattleRecord> rec,
|
||||
shared_ptr<struct event_base> base)
|
||||
: record(rec),
|
||||
event_it(this->record->events.begin()),
|
||||
play_start_timestamp(0),
|
||||
base(base),
|
||||
next_command_ev(event_new(this->base.get(), -1, EV_TIMEOUT, &BattleRecordPlayer::dispatch_schedule_events, this), event_free) { }
|
||||
|
||||
shared_ptr<const BattleRecord> BattleRecordPlayer::get_record() const {
|
||||
return this->record;
|
||||
}
|
||||
|
||||
void BattleRecordPlayer::set_lobby(std::shared_ptr<Lobby> l) {
|
||||
this->lobby = l;
|
||||
}
|
||||
|
||||
void BattleRecordPlayer::start() {
|
||||
if (this->play_start_timestamp == 0) {
|
||||
this->play_start_timestamp = now();
|
||||
this->schedule_events();
|
||||
}
|
||||
}
|
||||
|
||||
void BattleRecordPlayer::dispatch_schedule_events(
|
||||
evutil_socket_t, short, void* ctx) {
|
||||
reinterpret_cast<BattleRecordPlayer*>(ctx)->schedule_events();
|
||||
}
|
||||
|
||||
void BattleRecordPlayer::schedule_events() {
|
||||
// If the lobby is destroyed, we can't replay anything - just return without
|
||||
// rescheduling
|
||||
auto l = this->lobby.lock();
|
||||
if (!l) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
uint64_t relative_ts = now() - this->play_start_timestamp + this->record->battle_start_timestamp;
|
||||
|
||||
if (this->event_it == this->record->events.end()) {
|
||||
if (relative_ts >= this->record->battle_end_timestamp) {
|
||||
// If the record is complete and the end timestamp has been reached, so
|
||||
// send exit commands to all players in the lobby, and don't reschedule
|
||||
// the event (it will be deleted along with the Player when the lobby is
|
||||
// destroyed, when the last client leaves)
|
||||
send_command(l, 0xED, 0x00);
|
||||
|
||||
} else {
|
||||
// There are no more events to play, but the battle has not officially
|
||||
// ended yet - reschedule the event for the end time
|
||||
auto tv = usecs_to_timeval(this->record->battle_end_timestamp - relative_ts);
|
||||
event_add(this->next_command_ev.get(), &tv);
|
||||
}
|
||||
break;
|
||||
|
||||
} else {
|
||||
if (this->event_it->timestamp <= relative_ts) {
|
||||
// Play the next event
|
||||
auto& ev = *this->event_it;
|
||||
switch (ev.type) {
|
||||
case BattleRecord::Event::Type::PLAYER_JOIN:
|
||||
// Technically we can support this, but it should never happen
|
||||
throw runtime_error("player join event during battle replay");
|
||||
case BattleRecord::Event::Type::PLAYER_LEAVE:
|
||||
send_player_leave_notification(l, ev.leaving_client_id);
|
||||
break;
|
||||
case BattleRecord::Event::Type::SET_INITIAL_PLAYERS:
|
||||
// This should have been handled before the lobby was even created
|
||||
break;
|
||||
case BattleRecord::Event::Type::BATTLE_COMMAND:
|
||||
send_command(l, (ev.data.size() >= 0x400) ? 0x6C : 0xC9, 0x00, ev.data);
|
||||
break;
|
||||
case BattleRecord::Event::Type::GAME_COMMAND:
|
||||
send_command(l, (ev.data.size() >= 0x400) ? 0x6C : 0x60, 0x00, ev.data);
|
||||
break;
|
||||
case BattleRecord::Event::Type::EP3_GAME_COMMAND:
|
||||
send_command(l, 0xCB, 0x00, ev.data);
|
||||
break;
|
||||
case BattleRecord::Event::Type::CHAT_MESSAGE:
|
||||
send_chat_message(l, ev.guild_card_number, decode_sjis(ev.data));
|
||||
break;
|
||||
}
|
||||
this->event_it++;
|
||||
|
||||
} else {
|
||||
// The next event should not occur yet, so reschedule for the time when
|
||||
// it should occur
|
||||
auto tv = usecs_to_timeval(this->event_it->timestamp - relative_ts);
|
||||
event_add(this->next_command_ev.get(), &tv);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace Episode3
|
||||
@@ -0,0 +1,124 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <event2/event.h>
|
||||
|
||||
#include <deque>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <phosg/Strings.hh>
|
||||
|
||||
#include "../Player.hh"
|
||||
|
||||
struct Lobby;
|
||||
|
||||
namespace Episode3 {
|
||||
|
||||
// The comment in Server.hh does not apply to this file (and BattleRecord.cc).
|
||||
|
||||
|
||||
|
||||
class BattleRecord {
|
||||
public:
|
||||
struct PlayerEntry {
|
||||
PlayerLobbyDataDCGC lobby_data;
|
||||
PlayerInventory inventory;
|
||||
PlayerDispDataDCPCV3 disp;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct Event {
|
||||
enum class Type : uint8_t {
|
||||
PLAYER_JOIN = 0,
|
||||
PLAYER_LEAVE = 1,
|
||||
SET_INITIAL_PLAYERS = 2,
|
||||
BATTLE_COMMAND = 3,
|
||||
GAME_COMMAND = 4,
|
||||
EP3_GAME_COMMAND = 5,
|
||||
CHAT_MESSAGE = 6,
|
||||
};
|
||||
|
||||
// Fields used for all events
|
||||
Type type;
|
||||
uint64_t timestamp;
|
||||
// Fields used for PLAYER_JOIN and SET_INITIAL_PLAYERS only
|
||||
std::vector<PlayerEntry> players;
|
||||
// Fields used for PLAYER_LEAVE only
|
||||
uint8_t leaving_client_id;
|
||||
// Fields used for CHAT_MESSAGE only
|
||||
uint32_t guild_card_number;
|
||||
// Fields used for the COMMAND types and CHAT_MESSAGE
|
||||
std::string data;
|
||||
|
||||
Event() = default;
|
||||
explicit Event(StringReader& r);
|
||||
void serialize(StringWriter& w) const;
|
||||
};
|
||||
|
||||
explicit BattleRecord(uint32_t behavior_flags);
|
||||
explicit BattleRecord(const std::string& data);
|
||||
std::string serialize() const;
|
||||
|
||||
bool writable() const;
|
||||
bool battle_in_progress() const;
|
||||
|
||||
const Event* get_first_event() const;
|
||||
|
||||
void add_player(
|
||||
const PlayerLobbyDataDCGC& lobby_data,
|
||||
const PlayerInventory& inventory,
|
||||
const PlayerDispDataDCPCV3& disp);
|
||||
void delete_player(uint8_t client_id);
|
||||
void add_command(Event::Type type, const void* data, size_t size);
|
||||
void add_command(Event::Type type, std::string&& data);
|
||||
void add_chat_message(uint32_t guild_card_number, std::string&& data);
|
||||
// This function collapses all the existing player join/leave events into a
|
||||
// single SET_INITIAL_PLAYERS event, and deletes all events before the latest
|
||||
// BATTLE_COMMAND command that specifies the battle map. This should provide a
|
||||
// minimal set of commands to set up and start the battle during a replay.
|
||||
void set_battle_start_timestamp();
|
||||
void set_battle_end_timestamp();
|
||||
|
||||
private:
|
||||
static constexpr uint64_t SIGNATURE = 0x14C946D56D1DAC5A;
|
||||
|
||||
static bool is_map_definition_event(const Event& ev);
|
||||
|
||||
bool is_writable;
|
||||
|
||||
uint32_t behavior_flags;
|
||||
uint64_t battle_start_timestamp;
|
||||
uint64_t battle_end_timestamp;
|
||||
std::deque<Event> events;
|
||||
|
||||
friend class BattleRecordPlayer;
|
||||
};
|
||||
|
||||
class BattleRecordPlayer {
|
||||
public:
|
||||
BattleRecordPlayer(
|
||||
std::shared_ptr<const BattleRecord> rec,
|
||||
std::shared_ptr<struct event_base> base);
|
||||
~BattleRecordPlayer() = default;
|
||||
|
||||
std::shared_ptr<const BattleRecord> get_record() const;
|
||||
|
||||
void set_lobby(std::shared_ptr<Lobby> l);
|
||||
void start();
|
||||
|
||||
private:
|
||||
static void dispatch_schedule_events(evutil_socket_t, short, void* ctx);
|
||||
void schedule_events();
|
||||
|
||||
std::shared_ptr<const BattleRecord> record;
|
||||
std::deque<BattleRecord::Event>::const_iterator event_it;
|
||||
uint64_t play_start_timestamp;
|
||||
std::shared_ptr<struct event_base> base;
|
||||
std::weak_ptr<Lobby> lobby;
|
||||
std::shared_ptr<struct event> next_command_ev;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace Episode3
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,131 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "../Text.hh"
|
||||
#include "../CommandFormats.hh"
|
||||
#include "DataIndex.hh"
|
||||
|
||||
namespace Episode3 {
|
||||
|
||||
|
||||
|
||||
class ServerBase;
|
||||
class Server;
|
||||
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);
|
||||
void init();
|
||||
std::shared_ptr<Server> server();
|
||||
std::shared_ptr<const Server> server() const;
|
||||
std::shared_ptr<PlayerState> player_state();
|
||||
std::shared_ptr<const PlayerState> player_state() const;
|
||||
|
||||
ssize_t apply_abnormal_condition(
|
||||
const CardDefinition::Effect& eff,
|
||||
uint8_t def_effect_index,
|
||||
uint16_t target_card_ref,
|
||||
uint16_t sc_card_ref,
|
||||
int16_t value,
|
||||
int8_t dice_roll_value,
|
||||
int8_t random_percent);
|
||||
void apply_ap_adjust_assists_to_attack(
|
||||
std::shared_ptr<const Card> attacker_card,
|
||||
int16_t* inout_attacker_ap,
|
||||
int16_t* in_defense_power) const;
|
||||
bool card_type_is_sc_or_creature() const;
|
||||
bool check_card_flag(uint32_t flags) const;
|
||||
void commit_attack(
|
||||
int16_t damage,
|
||||
std::shared_ptr<Card> attacker_card,
|
||||
G_ApplyConditionEffect_GC_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);
|
||||
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);
|
||||
bool get_attack_condition_value(
|
||||
ConditionType cond_type,
|
||||
uint16_t card_ref,
|
||||
uint8_t def_effect_index,
|
||||
uint16_t value,
|
||||
uint16_t* out_value) const;
|
||||
std::shared_ptr<const DataIndex::CardEntry> get_definition() const;
|
||||
uint16_t get_card_ref() const;
|
||||
uint8_t get_client_id() const;
|
||||
uint8_t get_current_hp() const;
|
||||
uint8_t get_max_hp() const;
|
||||
CardShortStatus get_short_status();
|
||||
uint8_t get_team_id() const;
|
||||
int32_t move_to_location(const Location& loc);
|
||||
void propagate_shared_hp_if_needed();
|
||||
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 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 unknown_802380C0();
|
||||
void unknown_80237F98(bool require_condition_20_or_21);
|
||||
void unknown_80237F88();
|
||||
void unknown_80235AA0();
|
||||
void unknown_80235AD4();
|
||||
void unknown_80235B10();
|
||||
void unknown_80236374(std::shared_ptr<Card> other_card, const ActionState* as);
|
||||
void unknown_802379BC(uint16_t card_ref);
|
||||
void unknown_802379DC(const ActionState& pa);
|
||||
void unknown_80237A90(const ActionState& pa, uint16_t action_card_ref);
|
||||
void unknown_8023813C();
|
||||
bool is_guard_item() const;
|
||||
bool unknown_80236554(std::shared_ptr<Card> other_card, const ActionState* as);
|
||||
void unknown_802362D8(std::shared_ptr<Card> other_card);
|
||||
void unknown_80237734();
|
||||
|
||||
private:
|
||||
std::weak_ptr<Server> w_server;
|
||||
std::weak_ptr<PlayerState> w_player_state;
|
||||
|
||||
public:
|
||||
int16_t max_hp;
|
||||
int16_t current_hp;
|
||||
std::shared_ptr<const DataIndex::CardEntry> def_entry;
|
||||
uint8_t client_id;
|
||||
uint16_t card_id;
|
||||
uint16_t card_ref;
|
||||
uint16_t sc_card_ref;
|
||||
std::shared_ptr<const DataIndex::CardEntry> sc_def_entry;
|
||||
CardType sc_card_type;
|
||||
uint8_t team_id;
|
||||
uint32_t card_flags;
|
||||
Location loc;
|
||||
Direction facing_direction;
|
||||
ActionChainWithConds action_chain;
|
||||
ActionMetadata action_metadata;
|
||||
int16_t ap;
|
||||
int16_t tp;
|
||||
uint32_t num_ally_fcs_destroyed_at_set_time;
|
||||
uint32_t num_cards_destroyed_by_team_at_set_time;
|
||||
uint32_t unknown_a9;
|
||||
int16_t last_attack_preliminary_damage;
|
||||
int16_t last_attack_final_damage;
|
||||
uint32_t num_destroyed_ally_fcs;
|
||||
std::weak_ptr<Card> w_destroyer_sc_card;
|
||||
int16_t current_defense_power;
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Episode3
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,346 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "../Text.hh"
|
||||
#include "DataIndex.hh"
|
||||
|
||||
namespace Episode3 {
|
||||
|
||||
|
||||
|
||||
struct InterferenceProbabilityEntry {
|
||||
uint16_t card_id;
|
||||
uint8_t attack_probability;
|
||||
uint8_t defense_probability;
|
||||
};
|
||||
|
||||
const InterferenceProbabilityEntry* get_interference_probability_entry(
|
||||
uint16_t row_card_id,
|
||||
uint16_t column_card_id,
|
||||
bool is_attack);
|
||||
|
||||
|
||||
|
||||
class CardSpecial {
|
||||
public:
|
||||
enum class ExpressionTokenType {
|
||||
SPACE = 0, // Also used for end of string (get_next_expr_token returns null)
|
||||
REFERENCE = 1, // Reference to a value from the env stats (e.g. hp)
|
||||
NUMBER = 2, // Constant value (e.g. 2)
|
||||
SUBTRACT = 3, // "-" in input string
|
||||
ADD = 4, // "+" in input string
|
||||
ROUND_DIVIDE = 5, // "/" in input string
|
||||
FLOOR_DIVIDE = 6, // "//" in input string
|
||||
MULTIPLY = 7, // "*" in input string
|
||||
};
|
||||
|
||||
struct DiceRoll {
|
||||
uint8_t client_id;
|
||||
uint8_t unknown_a2;
|
||||
uint8_t value;
|
||||
bool value_used_in_expr;
|
||||
uint16_t unknown_a5;
|
||||
|
||||
DiceRoll();
|
||||
void clear();
|
||||
};
|
||||
|
||||
struct AttackEnvStats {
|
||||
uint32_t num_set_cards; // "f" in expr
|
||||
uint32_t dice_roll_value1; // "d" in expr
|
||||
uint32_t effective_ap; // "ap" in expr
|
||||
uint32_t effective_tp; // "tp" in expr
|
||||
uint32_t current_hp; // "hp" in expr
|
||||
uint32_t max_hp; // "mhp" in expr
|
||||
uint32_t effective_ap_if_not_tech; // "dm" in expr
|
||||
uint32_t effective_ap_if_not_physical; // "tdm" in expr
|
||||
uint32_t player_num_destroyed_fcs; // "tf" in expr
|
||||
uint32_t player_num_atk_points; // "ac" in expr
|
||||
uint32_t defined_max_hp; // "php" in expr
|
||||
uint32_t dice_roll_value2; // "dc" in expr
|
||||
uint32_t card_cost; // "cs" in expr
|
||||
uint32_t total_num_set_cards; // "a" in expr
|
||||
uint32_t action_cards_ap; // "kap" in expr
|
||||
uint32_t action_cards_tp; // "ktp" in expr
|
||||
uint32_t unknown_a1; // "dn" in expr
|
||||
uint32_t num_item_or_creature_cards_in_hand; // "hf" in expr
|
||||
uint32_t num_destroyed_ally_fcs; // "df" in expr
|
||||
uint32_t target_team_num_set_cards; // "ff" in expr
|
||||
uint32_t condition_giver_team_num_set_cards; // "ef" in expr
|
||||
uint32_t num_native_creatures; // "bi" in expr
|
||||
uint32_t num_a_beast_creatures; // "ab" in expr
|
||||
uint32_t num_machine_creatures; // "mc" in expr
|
||||
uint32_t num_dark_creatures; // "dk" in expr
|
||||
uint32_t num_sword_type_items; // "sa" in expr
|
||||
uint32_t num_gun_type_items; // "gn" in expr
|
||||
uint32_t num_cane_type_items; // "wd" in expr
|
||||
uint32_t effective_ap_if_not_tech2; // "tt" in expr
|
||||
uint32_t team_dice_boost; // "lv" in expr
|
||||
uint32_t sc_effective_ap; // "adm" in expr
|
||||
uint32_t attack_bonus; // "ddm" in expr
|
||||
uint32_t num_sword_type_items_on_team; // "sat" in expr
|
||||
uint32_t target_attack_bonus; // "edm" in expr
|
||||
uint32_t last_attack_preliminary_damage; // "ldm" in expr
|
||||
uint32_t last_attack_damage; // "rdm" in expr
|
||||
uint32_t total_last_attack_damage; // "fdm" in expr
|
||||
uint32_t last_attack_damage_count; // "ndm" in expr
|
||||
uint32_t target_current_hp; // "ehp" in expr
|
||||
|
||||
AttackEnvStats();
|
||||
void clear();
|
||||
|
||||
uint32_t at(size_t offset) const;
|
||||
} __attribute__((packed));
|
||||
|
||||
CardSpecial(std::shared_ptr<Server> server);
|
||||
std::shared_ptr<Server> server();
|
||||
std::shared_ptr<const Server> server() const;
|
||||
|
||||
void adjust_attack_damage_due_to_conditions(
|
||||
std::shared_ptr<const Card> target_card, int16_t* inout_damage, uint16_t attacker_card_ref);
|
||||
void adjust_dice_boost_if_team_has_condition_52(
|
||||
uint8_t team_id, uint8_t* inout_dice_boost, std::shared_ptr<const Card> card);
|
||||
void apply_action_conditions(
|
||||
uint8_t when,
|
||||
std::shared_ptr<const Card> attacker_card,
|
||||
std::shared_ptr<Card> defender_card,
|
||||
uint32_t flags,
|
||||
const ActionState* as);
|
||||
bool apply_attribute_guard_if_possible(
|
||||
uint32_t flags,
|
||||
CardClass card_class,
|
||||
std::shared_ptr<Card> card,
|
||||
uint16_t condition_giver_card_ref,
|
||||
uint16_t attacker_card_ref);
|
||||
bool apply_defense_condition(
|
||||
uint8_t when,
|
||||
Condition* defender_cond,
|
||||
uint8_t cond_index,
|
||||
const ActionState& defense_state,
|
||||
std::shared_ptr<Card> defender_card,
|
||||
uint32_t flags,
|
||||
bool unknown_p8);
|
||||
bool apply_defense_conditions(
|
||||
const ActionState& as,
|
||||
uint8_t when,
|
||||
std::shared_ptr<Card> defender_card,
|
||||
uint32_t flags);
|
||||
bool apply_stat_deltas_to_all_cards_from_all_conditions_with_card_ref(
|
||||
uint16_t card_ref);
|
||||
bool apply_stat_deltas_to_card_from_condition_and_clear_cond(
|
||||
Condition& cond, std::shared_ptr<Card> card);
|
||||
bool apply_stats_deltas_to_card_from_all_conditions_with_card_ref(
|
||||
uint16_t card_ref, std::shared_ptr<Card> card);
|
||||
bool card_has_condition_with_ref(
|
||||
std::shared_ptr<const Card> card,
|
||||
ConditionType cond_type,
|
||||
uint16_t card_ref,
|
||||
uint16_t match_card_ref) const;
|
||||
bool card_is_destroyed(std::shared_ptr<const Card> card) const;
|
||||
void compute_attack_ap(
|
||||
std::shared_ptr<const Card> target_card,
|
||||
int16_t* out_value,
|
||||
uint16_t attacker_card_ref);
|
||||
AttackEnvStats compute_attack_env_stats(
|
||||
const ActionState& pa,
|
||||
std::shared_ptr<const Card> card,
|
||||
const DiceRoll& dice_roll,
|
||||
uint16_t target_card_ref,
|
||||
uint16_t condition_giver_card_ref);
|
||||
std::shared_ptr<Card> compute_replaced_target_based_on_conditions(
|
||||
uint16_t target_card_ref,
|
||||
int unknown_p3,
|
||||
int unknown_p4,
|
||||
uint16_t attacker_card_ref,
|
||||
uint16_t set_card_ref,
|
||||
int unknown_p7,
|
||||
uint32_t* unknown_p9,
|
||||
uint8_t def_effect_index,
|
||||
uint32_t* unknown_p11,
|
||||
uint16_t sc_card_ref);
|
||||
StatSwapType compute_stat_swap_type(std::shared_ptr<const Card> card) const;
|
||||
void compute_team_dice_boost(uint8_t team_id);
|
||||
bool condition_has_when_20_or_21(const Condition& cond) const;
|
||||
size_t count_action_cards_with_condition_for_all_current_attacks(
|
||||
ConditionType cond_type, uint16_t card_ref) const;
|
||||
size_t count_action_cards_with_condition_for_current_attack(
|
||||
std::shared_ptr<const Card> card, ConditionType cond_type, uint16_t card_ref) const;
|
||||
size_t count_cards_with_card_id_set_by_player_except_card_ref(
|
||||
uint16_t card_id, uint16_t card_ref) const;
|
||||
std::vector<std::shared_ptr<const Card>> get_all_set_cards_by_team_and_class(
|
||||
CardClass card_class, uint8_t team_id, bool exclude_destroyed_cards) const;
|
||||
ActionState create_attack_state_from_card_action_chain(
|
||||
std::shared_ptr<const Card> attacker_card) const;
|
||||
ActionState create_defense_state_for_card_pair_action_chains(
|
||||
std::shared_ptr<const Card> attacker_card,
|
||||
std::shared_ptr<const Card> defender_card) const;
|
||||
void destroy_card_if_hp_zero(
|
||||
std::shared_ptr<Card> card, uint16_t attacker_card_ref);
|
||||
bool evaluate_effect_arg2_condition(
|
||||
const ActionState& as,
|
||||
std::shared_ptr<const Card> card,
|
||||
const char* arg2_text,
|
||||
DiceRoll& dice_roll,
|
||||
uint16_t set_card_ref,
|
||||
uint16_t sc_card_ref,
|
||||
uint8_t random_percent,
|
||||
uint8_t when) const;
|
||||
int32_t evaluate_effect_expr(
|
||||
const AttackEnvStats& ast,
|
||||
const char* expr,
|
||||
DiceRoll& dice_roll) const;
|
||||
bool execute_effect(
|
||||
Condition& cond,
|
||||
std::shared_ptr<Card> card,
|
||||
int16_t expr_value,
|
||||
int16_t unknown_p5,
|
||||
ConditionType cond_type,
|
||||
uint unknown_p7,
|
||||
uint16_t attacker_card_ref);
|
||||
const Condition* find_condition_with_parameters(
|
||||
std::shared_ptr<const Card> card,
|
||||
ConditionType cond_type,
|
||||
uint16_t set_card_ref,
|
||||
uint8_t def_effect_index) const;
|
||||
Condition* find_condition_with_parameters(
|
||||
std::shared_ptr<Card> card,
|
||||
ConditionType cond_type,
|
||||
uint16_t set_card_ref,
|
||||
uint8_t def_effect_index) const;
|
||||
static void get_card1_loc_with_card2_opposite_direction(
|
||||
Location* out_loc,
|
||||
std::shared_ptr<const Card> card1,
|
||||
std::shared_ptr<const Card> card2);
|
||||
uint16_t get_card_id_with_effective_range(
|
||||
std::shared_ptr<const Card> card1, uint16_t default_card_id, std::shared_ptr<const Card> card2) const;
|
||||
static void get_effective_ap_tp(
|
||||
StatSwapType type,
|
||||
int16_t* effective_ap,
|
||||
int16_t* effective_tp,
|
||||
int16_t hp,
|
||||
int16_t ap,
|
||||
int16_t tp);
|
||||
const char* get_next_expr_token(
|
||||
const char *expr, ExpressionTokenType* out_type, int32_t* out_value) const;
|
||||
std::vector<std::shared_ptr<const Card>> get_targeted_cards_for_condition(
|
||||
uint16_t card_ref,
|
||||
uint8_t def_effect_index,
|
||||
uint16_t setter_card_ref,
|
||||
const ActionState& as,
|
||||
int16_t p_target_type,
|
||||
bool apply_usability_filters) const;
|
||||
std::vector<std::shared_ptr<Card>> get_targeted_cards_for_condition(
|
||||
uint16_t card_ref,
|
||||
uint8_t def_effect_index,
|
||||
uint16_t setter_card_ref,
|
||||
const ActionState& as,
|
||||
int16_t p_target_type,
|
||||
bool apply_usability_filters);
|
||||
bool is_card_targeted_by_condition(
|
||||
const Condition& cond, const ActionState& as, std::shared_ptr<const Card> card) const;
|
||||
void on_card_set(std::shared_ptr<PlayerState> ps, uint16_t card_ref);
|
||||
const CardDefinition::Effect* original_definition_for_condition(
|
||||
const Condition& cond) const;
|
||||
bool card_ref_has_ability_trap(const Condition& eff) const;
|
||||
void send_6xB4x06_for_exp_change(
|
||||
std::shared_ptr<const Card> card,
|
||||
uint16_t attacker_card_ref,
|
||||
uint8_t dice_roll_value,
|
||||
bool unknown_p5) const;
|
||||
void send_6xB4x06_for_card_destroyed(
|
||||
std::shared_ptr<const Card> destroyed_card, uint16_t attacker_card_ref) const;
|
||||
uint16_t send_6xB4x06_if_card_ref_invalid(
|
||||
uint16_t card_ref, int16_t value) const;
|
||||
void send_6xB4x06_for_stat_delta(
|
||||
std::shared_ptr<const Card> card,
|
||||
uint16_t attacker_card_ref,
|
||||
uint32_t flags,
|
||||
int16_t hp_delta,
|
||||
bool unknown_p6,
|
||||
bool unknown_p7) const;
|
||||
bool should_cancel_condition_due_to_anti_abnormality(
|
||||
const CardDefinition::Effect& eff,
|
||||
std::shared_ptr<const Card> card,
|
||||
uint16_t target_card_ref,
|
||||
uint16_t sc_card_ref) const;
|
||||
bool should_return_card_ref_to_hand_on_destruction(
|
||||
uint16_t card_ref) const;
|
||||
size_t sum_last_attack_damage(
|
||||
std::vector<std::shared_ptr<const Card>>* out_cards,
|
||||
int32_t* out_damage_sum,
|
||||
size_t* out_damage_count) const;
|
||||
void update_condition_orders(std::shared_ptr<Card> card);
|
||||
int16_t max_all_attack_bonuses(size_t* out_count) const;
|
||||
void unknown_80244AA8(std::shared_ptr<Card> card);
|
||||
void check_for_defense_interference(
|
||||
std::shared_ptr<const Card> attacker_card,
|
||||
std::shared_ptr<Card> target_card,
|
||||
int16_t* inout_unknown_p4);
|
||||
void unknown_8024C2B0(
|
||||
uint32_t when,
|
||||
uint16_t set_card_ref,
|
||||
const ActionState& as,
|
||||
uint16_t sc_card_ref,
|
||||
bool apply_defense_condition_to_all_cards = true,
|
||||
uint16_t apply_defense_condition_to_card_ref = 0xFFFF);
|
||||
std::vector<std::shared_ptr<const Card>> get_all_set_cards() const;
|
||||
std::vector<std::shared_ptr<const Card>> find_cards_by_condition_inc_exc(
|
||||
ConditionType include_cond,
|
||||
ConditionType exclude_cond = ConditionType::NONE,
|
||||
AssistEffect include_eff = AssistEffect::NONE,
|
||||
AssistEffect exclude_eff = AssistEffect::NONE) const;
|
||||
void clear_invalid_conditions_on_card(
|
||||
std::shared_ptr<Card> card, const ActionState& as);
|
||||
void on_card_destroyed(
|
||||
std::shared_ptr<Card> attacker_card, std::shared_ptr<Card> destroyed_card);
|
||||
std::vector<std::shared_ptr<const Card>> find_cards_in_hp_range(
|
||||
int16_t min, int16_t max) const;
|
||||
std::vector<std::shared_ptr<const Card>> find_all_cards_by_aerial_attribute(bool is_aerial) const;
|
||||
std::vector<std::shared_ptr<const Card>> find_cards_damaged_by_at_least(int16_t damage) const;
|
||||
std::vector<std::shared_ptr<const Card>> find_all_set_cards_on_client_team(uint8_t client_id) const;
|
||||
std::vector<std::shared_ptr<const Card>> find_all_cards_on_same_or_other_team(uint8_t client_id, bool same_team) const;
|
||||
std::shared_ptr<const Card> sc_card_for_client_id(uint8_t client_id) const;
|
||||
std::shared_ptr<const Card> get_attacker_card(const ActionState& as) const;
|
||||
std::vector<std::shared_ptr<const Card>> get_attacker_card_and_sc_if_item(const ActionState& as) const;
|
||||
std::vector<std::shared_ptr<const Card>> find_all_set_cards_with_cost_in_range(uint8_t min_cost, uint8_t max_cost) const;
|
||||
std::vector<std::shared_ptr<const Card>> filter_cards_by_range(
|
||||
const std::vector<std::shared_ptr<const Card>>& cards,
|
||||
std::shared_ptr<const Card> card1,
|
||||
const Location& card1_loc,
|
||||
std::shared_ptr<const Card> card2) const;
|
||||
void unknown_8024AAB8(const ActionState& as);
|
||||
void unknown_80244BE4(std::shared_ptr<Card> unknown_p2);
|
||||
void unknown_80244CA8(std::shared_ptr<Card> card);
|
||||
template <uint8_t When1, uint8_t When2>
|
||||
void unknown1_t(
|
||||
std::shared_ptr<Card> unknown_p2, const ActionState* existing_as = nullptr);
|
||||
void unknown_80249060(std::shared_ptr<Card> unknown_p2);
|
||||
void unknown_80249254(std::shared_ptr<Card> unknown_p2);
|
||||
void unknown_8024945C(std::shared_ptr<Card> unknown_p2, const ActionState& existing_as);
|
||||
void unknown_8024966C(std::shared_ptr<Card> unknown_p2, const ActionState* existing_as);
|
||||
static std::shared_ptr<Card> sc_card_for_card(std::shared_ptr<Card> unknown_p2);
|
||||
void unknown_8024A9D8(const ActionState& pa, uint16_t action_card_ref);
|
||||
void check_for_attack_interference(std::shared_ptr<Card> unknown_p2);
|
||||
template <uint8_t When1, uint8_t When2, uint8_t When3, uint8_t When4>
|
||||
void unknown_t2(std::shared_ptr<Card> unknown_p2);
|
||||
void unknown_8024997C(std::shared_ptr<Card> card);
|
||||
void unknown_8024A394(std::shared_ptr<Card> card);
|
||||
bool client_has_atk_dice_boost_condition(uint8_t client_id);
|
||||
void unknown_8024A6DC(
|
||||
std::shared_ptr<Card> unknown_p2, std::shared_ptr<Card> unknown_p3);
|
||||
std::vector<std::shared_ptr<const Card>> find_all_sc_cards_of_class(
|
||||
CardClass card_class) const;
|
||||
|
||||
private:
|
||||
std::weak_ptr<Server> w_server;
|
||||
ActionState unknown_action_state_a1;
|
||||
ActionState action_state;
|
||||
uint16_t unknown_a2;
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Episode3
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,964 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <phosg/Encoding.hh>
|
||||
#include <phosg/JSON.hh>
|
||||
|
||||
#include "../Text.hh"
|
||||
|
||||
namespace Episode3 {
|
||||
|
||||
|
||||
|
||||
// The comment in Server.hh does not apply to this file (and DataIndex.cc).
|
||||
// Except for the Location structure, these structures and functions are not
|
||||
// based on Sega's original implementation.
|
||||
|
||||
class DataIndex;
|
||||
|
||||
|
||||
|
||||
enum BehaviorFlag {
|
||||
SKIP_DECK_VERIFY = 0x00000001,
|
||||
IGNORE_CARD_COUNTS = 0x00000002,
|
||||
SKIP_D1_D2_REPLACE = 0x00000004,
|
||||
DISABLE_TIME_LIMITS = 0x00000008,
|
||||
ENABLE_STATUS_MESSAGES = 0x00000010,
|
||||
LOAD_CARD_TEXT = 0x00000020,
|
||||
ENABLE_RECORDING = 0x00000040,
|
||||
DISABLE_MASKING = 0x00000080,
|
||||
DISABLE_INTERFERENCE = 0x00000100,
|
||||
};
|
||||
|
||||
|
||||
|
||||
enum class StatSwapType : uint8_t {
|
||||
NONE = 0,
|
||||
A_T_SWAP = 1,
|
||||
A_H_SWAP = 2,
|
||||
};
|
||||
|
||||
enum class ActionType : uint8_t {
|
||||
INVALID_00 = 0,
|
||||
DEFENSE = 1,
|
||||
ATTACK = 2,
|
||||
};
|
||||
|
||||
enum class AttackMedium : uint8_t {
|
||||
UNKNOWN = 0,
|
||||
PHYSICAL = 1,
|
||||
TECH = 2,
|
||||
UNKNOWN_03 = 3, // Probably Resta
|
||||
INVALID_FF = 0xFF,
|
||||
};
|
||||
|
||||
const char* name_for_attack_medium(AttackMedium medium);
|
||||
|
||||
enum class CriterionCode : uint8_t {
|
||||
NONE = 0x00,
|
||||
HU_CLASS_SC = 0x01,
|
||||
RA_CLASS_SC = 0x02,
|
||||
FO_CLASS_SC = 0x03,
|
||||
SAME_TEAM = 0x04,
|
||||
SAME_PLAYER = 0x05,
|
||||
SAME_TEAM_NOT_SAME_PLAYER = 0x06, // Allies only
|
||||
UNKNOWN_07 = 0x07,
|
||||
NOT_SC = 0x08,
|
||||
SC = 0x09,
|
||||
HU_OR_RA_CLASS_SC = 0x0A,
|
||||
HUNTER_HUMAN_SC = 0x0B,
|
||||
HUNTER_HU_CLASS_MALE_SC = 0x0C,
|
||||
HUNTER_FEMALE_SC = 0x0D,
|
||||
HUNTER_HU_OR_FO_CLASS_HUMAN_SC = 0x0E,
|
||||
HUNTER_HU_CLASS_ANDROID_SC = 0x0F,
|
||||
UNKNOWN_10 = 0x10,
|
||||
UNKNOWN_11 = 0x11,
|
||||
HUNTER_HUNEWEARL_CLASS_SC = 0x12,
|
||||
HUNTER_RA_CLASS_MALE_SC = 0x13,
|
||||
HUNTER_RA_CLASS_FEMALE_SC = 0x14,
|
||||
HUNTER_RA_OR_FO_CLASS_FEMALE_SC = 0x15,
|
||||
HUNTER_HU_OR_RA_CLASS_HUMAN_SC = 0x16,
|
||||
HUNTER_RA_CLASS_ANDROID_SC = 0x17,
|
||||
HUNTER_FO_CLASS_FEMALE_SC = 0x18,
|
||||
HUNTER_FEMALE_HUMAN_SC = 0x19,
|
||||
HUNTER_ANDROID_SC = 0x1A,
|
||||
HU_OR_FO_CLASS_SC = 0x1B,
|
||||
RA_OR_FO_CLASS_SC = 0x1C,
|
||||
PHYSICAL_OR_UNKNOWN_ATTACK_MEDIUM = 0x1D,
|
||||
TECH_OR_UNKNOWN_ATTACK_MEDIUM = 0x1E,
|
||||
PHYSICAL_OR_TECH_OR_UNKNOWN_ATTACK_MEDIUM = 0x1F,
|
||||
UNKNOWN_20 = 0x20,
|
||||
UNKNOWN_21 = 0x21,
|
||||
UNKNOWN_22 = 0x22,
|
||||
};
|
||||
|
||||
enum class CardRarity : uint8_t {
|
||||
N1 = 0x01,
|
||||
R1 = 0x02,
|
||||
S = 0x03,
|
||||
E = 0x04,
|
||||
N2 = 0x05,
|
||||
N3 = 0x06,
|
||||
N4 = 0x07,
|
||||
R2 = 0x08,
|
||||
R3 = 0x09,
|
||||
R4 = 0x0A,
|
||||
SS = 0x0B,
|
||||
D1 = 0x0C,
|
||||
D2 = 0x0D,
|
||||
INVIS = 0x0E,
|
||||
};
|
||||
|
||||
enum class CardType : uint8_t {
|
||||
HUNTERS_SC = 0x00,
|
||||
ARKZ_SC = 0x01,
|
||||
ITEM = 0x02,
|
||||
CREATURE = 0x03,
|
||||
ACTION = 0x04,
|
||||
ASSIST = 0x05,
|
||||
INVALID_FF = 0xFF,
|
||||
END_CARD_LIST = 0xFF,
|
||||
};
|
||||
|
||||
enum class CardClass : uint16_t {
|
||||
HU_SC = 0x0000,
|
||||
RA_SC = 0x0001,
|
||||
FO_SC = 0x0002,
|
||||
NATIVE_CREATURE = 0x000A,
|
||||
A_BEAST_CREATURE = 0x000B,
|
||||
MACHINE_CREATURE = 0x000C,
|
||||
DARK_CREATURE = 0x000D,
|
||||
GUARD_ITEM = 0x0015,
|
||||
MAG_ITEM = 0x0017,
|
||||
SWORD_ITEM = 0x0018,
|
||||
GUN_ITEM = 0x0019,
|
||||
CANE_ITEM = 0x001A,
|
||||
ATTACK_ACTION = 0x001E,
|
||||
DEFENSE_ACTION = 0x001F,
|
||||
TECH = 0x0020,
|
||||
PHOTON_BLAST = 0x0021,
|
||||
CONNECT_ONLY_ATTACK_ACTION = 0x0022,
|
||||
BOSS_ATTACK_ACTION = 0x0023,
|
||||
BOSS_TECH = 0x0024,
|
||||
ASSIST = 0x0028,
|
||||
};
|
||||
|
||||
bool card_class_is_tech_like(CardClass cc);
|
||||
|
||||
enum class TargetMode : uint8_t {
|
||||
NONE = 0x00, // Used for defense cards, mags, shields, etc.
|
||||
SINGLE_RANGE = 0x01,
|
||||
MULTI_RANGE = 0x02,
|
||||
SELF = 0x03,
|
||||
TEAM = 0x04,
|
||||
EVERYONE = 0x05,
|
||||
MULTI_RANGE_ALLIES = 0x06, // e.g. Shifta
|
||||
ALL_ALLIES = 0x07, // e.g. Anti, Resta, Leilla
|
||||
ALL = 0x08, // e.g. Last Judgment, Earthquake
|
||||
OWN_FCS = 0x09, // e.g. Traitor
|
||||
};
|
||||
|
||||
enum class ConditionType : uint8_t {
|
||||
NONE = 0x00,
|
||||
AP_BOOST = 0x01, // Temporarily increase AP by N
|
||||
RAMPAGE = 0x02,
|
||||
MULTI_STRIKE = 0x03, // Duplicate attack N times
|
||||
DAMAGE_MOD_1 = 0x04, // Set attack damage / AP to N after action cards applied (step 1)
|
||||
IMMOBILE = 0x05, // Give Immobile condition
|
||||
HOLD = 0x06, // Give Hold condition
|
||||
UNKNOWN_07 = 0x07,
|
||||
TP_BOOST = 0x08, // Add N TP temporarily during attack
|
||||
GIVE_DAMAGE = 0x09, // Cause direct N HP loss
|
||||
GUOM = 0x0A, // Give Guom condition
|
||||
PARALYZE = 0x0B, // Give Paralysis condition
|
||||
UNKNOWN_0C = 0x0C, // Swap AP and TP temporarily (presumably)
|
||||
A_H_SWAP = 0x0D, // Swap AP and HP temporarily
|
||||
PIERCE = 0x0E, // Attack SC directly even if they have items equipped
|
||||
UNKNOWN_0F = 0x0F,
|
||||
HEAL = 0x10, // Increase HP by N
|
||||
RETURN_TO_HAND = 0x11, // Return card to hand
|
||||
UNKNOWN_12 = 0x12,
|
||||
UNKNOWN_13 = 0x13,
|
||||
ACID = 0x14, // Give Acid condition
|
||||
UNKNOWN_15 = 0x15,
|
||||
MIGHTY_KNUCKLE = 0x16, // Temporarily increase AP by N, and set ATK dice to zero
|
||||
UNIT_BLOW = 0x17, // Temporarily increase AP by N * number of this card set within phase
|
||||
CURSE = 0x18, // Give Curse condition
|
||||
COMBO_AP = 0x19, // Temporarily increase AP by number of this card set within phase
|
||||
PIERCE_RAMPAGE_BLOCK = 0x1A, // Block attack if Pierce/Rampage
|
||||
ABILITY_TRAP = 0x1B, // Temporarily disable opponent abilities
|
||||
FREEZE = 0x1C, // Give Freeze condition
|
||||
ANTI_ABNORMALITY_1 = 0x1D, // Cure all abnormal conditions
|
||||
UNKNOWN_1E = 0x1E,
|
||||
EXPLOSION = 0x1F, // Damage all SCs and FCs by number of this same card set * 2
|
||||
UNKNOWN_20 = 0x20,
|
||||
UNKNOWN_21 = 0x21,
|
||||
UNKNOWN_22 = 0x22,
|
||||
RETURN_TO_DECK = 0x23, // Cancel discard and move to bottom of deck instead
|
||||
AERIAL = 0x24, // Give Aerial status
|
||||
AP_LOSS = 0x25, // Make attacker temporarily lose N AP during defense
|
||||
BONUS_FROM_LEADER = 0x26, // Gain AP equal to the number of cards of type N on the field
|
||||
FREE_MANEUVER = 0x27, // Enable movement over occupied tiles
|
||||
HASTE = 0x28, // Multiply all move action costs by expr (which may be zero)
|
||||
CLONE = 0x29, // Make setting this card free if at least one card of type N is already on the field
|
||||
DEF_DISABLE_BY_COST = 0x2A, // Disable use of any defense cards costing between (N / 10) and (N % 10) points, inclusive
|
||||
FILIAL = 0x2B, // Increase controlling SC's HP by N when this card is destroyed
|
||||
SNATCH = 0x2C, // Steal N EXP during attack
|
||||
HAND_DISRUPTER = 0x2D, // Discard N cards from hand immediately
|
||||
DROP = 0x2E, // Give Drop condition
|
||||
ACTION_DISRUPTER = 0x2F, // Destroy all action cards used by attacker
|
||||
SET_HP = 0x30, // Set HP to N
|
||||
NATIVE_SHIELD = 0x31, // Block attacks from Native creatures
|
||||
A_BEAST_SHIELD = 0x32, // Block attacks from A.Beast creatures
|
||||
MACHINE_SHIELD = 0x33, // Block attacks from Machine creatures
|
||||
DARK_SHIELD = 0x34, // Block attacks from Dark creatures
|
||||
SWORD_SHIELD = 0x35, // Block attacks from Sword items
|
||||
GUN_SHIELD = 0x36, // Block attacks from Gun items
|
||||
CANE_SHIELD = 0x37, // Block attacks from Cane items
|
||||
UNKNOWN_38 = 0x38,
|
||||
UNKNOWN_39 = 0x39,
|
||||
DEFENDER = 0x3A, // Make attacks go to setter of this card instead of original target
|
||||
SURVIVAL_DECOYS = 0x3B, // Redirect damage for multi-sided attack
|
||||
GIVE_OR_TAKE_EXP = 0x3C, // Give N EXP, or take if N is negative
|
||||
UNKNOWN_3D = 0x3D,
|
||||
DEATH_COMPANION = 0x3E, // If this card has 1 or 2 HP, set its HP to N
|
||||
EXP_DECOY = 0x3F, // If defender has EXP, lose EXP instead of getting damage when attacked
|
||||
SET_MV = 0x40, // Set MV to N
|
||||
GROUP = 0x41, // Temporarily increase AP by N * number of this card on field, excluding itself
|
||||
BERSERK = 0x42, // User of this card receives the same damage as target, and isn't helped by target's defense cards
|
||||
GUARD_CREATURE = 0x43, // Attacks on controlling SC damage this card instead
|
||||
TECH = 0x44, // Technique cards cost 1 fewer ATK point
|
||||
BIG_SWING = 0x45, // Increase all attacking ATK costs by 1
|
||||
UNKNOWN_46 = 0x46,
|
||||
SHIELD_WEAPON = 0x47, // Limit attacker's choice of target to guard items
|
||||
ATK_DICE_BOOST = 0x48, // Increase ATK dice roll by 1
|
||||
UNKNOWN_49 = 0x49,
|
||||
MAJOR_PIERCE = 0x4A, // If SC has over half of max HP, attacks target SC instead of equipped items
|
||||
HEAVY_PIERCE = 0x4B, // If SC has 3 or more items equipped, attacks target SC instead of equipped items
|
||||
MAJOR_RAMPAGE = 0x4C, // If SC has over half of max HP, attacks target SC and all equipped items
|
||||
HEAVY_RAMPAGE = 0x4D, // If SC has 3 or more items equipped, attacks target SC and all equipped items
|
||||
AP_GROWTH = 0x4E, // Permanently increase AP by N
|
||||
TP_GROWTH = 0x4F, // Permanently increase TP by N
|
||||
REBORN = 0x50, // If any card of type N is on the field, this card goes to the hand when destroyed instead of being discarded
|
||||
COPY = 0x51, // Temporarily set AP/TP to N percent (or 100% if N is 0) of opponent's values
|
||||
UNKNOWN_52 = 0x52,
|
||||
MISC_GUARDS = 0x53, // Add N to card's defense value
|
||||
AP_OVERRIDE = 0x54, // Set AP to N temporarily
|
||||
TP_OVERRIDE = 0x55, // Set TP to N temporarily
|
||||
RETURN = 0x56, // Return card to hand on destruction instead of discarding
|
||||
A_T_SWAP_PERM = 0x57, // Permanently swap AP and TP
|
||||
A_H_SWAP_PERM = 0x58, // Permanently swap AP and HP
|
||||
SLAYERS_ASSASSINS = 0x59, // Temporarily increase AP during attack
|
||||
ANTI_ABNORMALITY_2 = 0x5A, // Remove all conditions
|
||||
FIXED_RANGE = 0x5B, // Use SC's range instead of weapon or attack card ranges
|
||||
ELUDE = 0x5C, // SC does not lose HP when equipped items are destroyed
|
||||
PARRY = 0x5D, // Forward attack to a random FC within one tile of original target, excluding attacker and original target
|
||||
BLOCK_ATTACK = 0x5E, // Completely block attack
|
||||
UNKNOWN_5F = 0x5F,
|
||||
UNKNOWN_60 = 0x60,
|
||||
COMBO_TP = 0x61, // Gain TP equal to the number of cards of type N on the field
|
||||
MISC_AP_BONUSES = 0x62, // Temporarily increase AP by N
|
||||
MISC_TP_BONUSES = 0x63, // Temporarily increase TP by N
|
||||
UNKNOWN_64 = 0x64,
|
||||
MISC_DEFENSE_BONUSES = 0x65, // Decrease damage by N
|
||||
MOSTLY_HALFGUARDS = 0x66, // Reduce damage from incoming attack by N
|
||||
PERIODIC_FIELD = 0x67, // Swap immunity to tech or physical attacks
|
||||
FC_LIMIT_BY_COUNT = 0x68, // Change FC limit from 8 ATK points total to 4 FCs total
|
||||
UNKNOWN_69 = 0x69,
|
||||
MV_BONUS = 0x6A, // Increase MV by N
|
||||
FORWARD_DAMAGE = 0x6B,
|
||||
WEAK_SPOT_INFLUENCE = 0x6C, // Temporarily decrease AP by N
|
||||
DAMAGE_MODIFIER_2 = 0x6D, // Set attack damage / AP after action cards applied (step 2)
|
||||
WEAK_HIT_BLOCK = 0x6E, // Block all attacks of N damage or less
|
||||
AP_SILENCE = 0x6F, // Temporarily decrease AP of opponent by N
|
||||
TP_SILENCE = 0x70, // Temporarily decrease TP of opponent by N
|
||||
A_T_SWAP = 0x71, // Temporarily swap AP and TP
|
||||
HALFGUARD = 0x72, // Halve damage from attacks that would inflict N or more damage
|
||||
UNKNOWN_73 = 0x73,
|
||||
RAMPAGE_AP_LOSS = 0x74, // Temporarily reduce AP by N
|
||||
UNKNOWN_75 = 0x75,
|
||||
REFLECT = 0x76, // Generate reverse attack
|
||||
UNKNOWN_77 = 0x77,
|
||||
ANY = 0x78, // Not a real condition; used as a wildcard in search functions
|
||||
UNKNOWN_79 = 0x79,
|
||||
UNKNOWN_7A = 0x7A,
|
||||
UNKNOWN_7B = 0x7B,
|
||||
UNKNOWN_7C = 0x7C,
|
||||
UNKNOWN_7D = 0x7D,
|
||||
INVALID_FF = 0xFF,
|
||||
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,
|
||||
DICE_PLUS_1 = 0x0002,
|
||||
DICE_FEVER = 0x0003,
|
||||
CARD_RETURN = 0x0004,
|
||||
LAND_PRICE = 0x0005,
|
||||
POWERLESS_RAIN = 0x0006,
|
||||
BRAVE_WIND = 0x0007,
|
||||
SILENT_COLOSSEUM = 0x0008,
|
||||
RESISTANCE = 0x0009,
|
||||
INDEPENDENT = 0x000A,
|
||||
ASSISTLESS = 0x000B,
|
||||
ATK_DICE_2 = 0x000C,
|
||||
DEFLATION = 0x000D,
|
||||
INFLATION = 0x000E,
|
||||
EXCHANGE = 0x000F,
|
||||
INFLUENCE = 0x0010,
|
||||
SKIP_SET = 0x0011,
|
||||
SKIP_MOVE = 0x0012,
|
||||
SKIP_ACT = 0x0013,
|
||||
SKIP_DRAW = 0x0014,
|
||||
FLY = 0x0015,
|
||||
NECROMANCER = 0x0016,
|
||||
PERMISSION = 0x0017,
|
||||
SHUFFLE_ALL = 0x0018,
|
||||
LEGACY = 0x0019,
|
||||
ASSIST_REVERSE = 0x001A,
|
||||
STAMINA = 0x001B,
|
||||
AP_ABSORPTION = 0x001C,
|
||||
HEAVY_FOG = 0x001D,
|
||||
TRASH_1 = 0x001E,
|
||||
EMPTY_HAND = 0x001F,
|
||||
HITMAN = 0x0020,
|
||||
ASSIST_TRASH = 0x0021,
|
||||
SHUFFLE_GROUP = 0x0022,
|
||||
ASSIST_VANISH = 0x0023,
|
||||
CHARITY = 0x0024,
|
||||
INHERITANCE = 0x0025,
|
||||
FIX = 0x0026,
|
||||
MUSCULAR = 0x0027,
|
||||
CHANGE_BODY = 0x0028,
|
||||
GOD_WHIM = 0x0029,
|
||||
GOLD_RUSH = 0x002A,
|
||||
ASSIST_RETURN = 0x002B,
|
||||
REQUIEM = 0x002C,
|
||||
RANSOM = 0x002D,
|
||||
SIMPLE = 0x002E,
|
||||
SLOW_TIME = 0x002F,
|
||||
QUICK_TIME = 0x0030,
|
||||
TERRITORY = 0x0031,
|
||||
OLD_TYPE = 0x0032,
|
||||
FLATLAND = 0x0033,
|
||||
IMMORTALITY = 0x0034,
|
||||
SNAIL_PACE = 0x0035,
|
||||
TECH_FIELD = 0x0036,
|
||||
FOREST_RAIN = 0x0037,
|
||||
CAVE_WIND = 0x0038,
|
||||
MINE_BRIGHTNESS = 0x0039,
|
||||
RUIN_DARKNESS = 0x003A,
|
||||
SABER_DANCE = 0x003B,
|
||||
BULLET_STORM = 0x003C,
|
||||
CANE_PALACE = 0x003D,
|
||||
GIANT_GARDEN = 0x003E,
|
||||
MARCH_OF_THE_MEEK = 0x003F,
|
||||
SUPPORT = 0x0040,
|
||||
RICH = 0x0041,
|
||||
REVERSE_CARD = 0x0042,
|
||||
VENGEANCE = 0x0043,
|
||||
SQUEEZE = 0x0044,
|
||||
HOMESICK = 0x0045,
|
||||
BOMB = 0x0046,
|
||||
SKIP_TURN = 0x0047,
|
||||
BATTLE_ROYALE = 0x0048,
|
||||
DICE_FEVER_PLUS = 0x0049,
|
||||
RICH_PLUS = 0x004A,
|
||||
CHARITY_PLUS = 0x004B,
|
||||
ANY = 0x004C, // Unused on cards; used in some search functions
|
||||
};
|
||||
|
||||
enum class BattlePhase : uint8_t {
|
||||
INVALID_00 = 0,
|
||||
DICE = 1,
|
||||
SET = 2,
|
||||
MOVE = 3,
|
||||
ACTION = 4,
|
||||
DRAW = 5,
|
||||
INVALID_FF = 0xFF,
|
||||
};
|
||||
|
||||
enum class ActionSubphase : uint8_t {
|
||||
ATTACK = 0,
|
||||
DEFENSE = 2,
|
||||
INVALID_FF = 0xFF,
|
||||
};
|
||||
|
||||
const char* name_for_action_subphase(ActionSubphase subphase);
|
||||
|
||||
enum class SetupPhase : uint8_t {
|
||||
REGISTRATION = 0,
|
||||
STARTER_ROLLS = 1,
|
||||
HAND_REDRAW_OPTION = 2,
|
||||
MAIN_BATTLE = 3,
|
||||
BATTLE_ENDED = 4,
|
||||
INVALID_FF = 0xFF,
|
||||
};
|
||||
|
||||
enum class RegistrationPhase : uint8_t {
|
||||
AWAITING_NUM_PLAYERS = 0, // num_players not set yet
|
||||
AWAITING_PLAYERS = 1, // num_players set, but some players not registered
|
||||
AWAITING_DECKS = 2, // all players registered, but some decks missing
|
||||
REGISTERED = 3, // All players/decks present, but battle not started yet
|
||||
BATTLE_STARTED = 4,
|
||||
INVALID_FF = 0xFF,
|
||||
};
|
||||
|
||||
|
||||
|
||||
enum class Direction : uint8_t {
|
||||
RIGHT = 0,
|
||||
UP = 1,
|
||||
LEFT = 2,
|
||||
DOWN = 3,
|
||||
INVALID_FF = 0xFF,
|
||||
};
|
||||
|
||||
Direction turn_left(Direction d);
|
||||
Direction turn_right(Direction d);
|
||||
Direction turn_around(Direction d);
|
||||
const char* name_for_direction(Direction d);
|
||||
|
||||
struct Location {
|
||||
uint8_t x;
|
||||
uint8_t y;
|
||||
Direction direction;
|
||||
uint8_t unused;
|
||||
|
||||
Location();
|
||||
Location(uint8_t x, uint8_t y);
|
||||
Location(uint8_t x, uint8_t y, Direction direction);
|
||||
bool operator==(const Location& other) const;
|
||||
bool operator!=(const Location& other) const;
|
||||
|
||||
std::string str() const;
|
||||
|
||||
void clear();
|
||||
void clear_FF();
|
||||
} __attribute__((packed));
|
||||
|
||||
struct CardDefinition {
|
||||
struct Stat {
|
||||
enum Type : uint8_t {
|
||||
BLANK = 0,
|
||||
STAT = 1,
|
||||
PLUS_STAT = 2,
|
||||
MINUS_STAT = 3,
|
||||
EQUALS_STAT = 4,
|
||||
UNKNOWN = 5,
|
||||
PLUS_UNKNOWN = 6,
|
||||
MINUS_UNKNOWN = 7,
|
||||
EQUALS_UNKNOWN = 8,
|
||||
};
|
||||
be_uint16_t code;
|
||||
Type type;
|
||||
int8_t stat;
|
||||
|
||||
void decode_code();
|
||||
std::string str() const;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct Effect {
|
||||
uint8_t effect_num;
|
||||
ConditionType type;
|
||||
ptext<char, 0x0F> expr; // May be blank if the condition type doesn't use it
|
||||
uint8_t when;
|
||||
ptext<char, 4> arg1;
|
||||
ptext<char, 4> arg2;
|
||||
ptext<char, 4> arg3;
|
||||
CriterionCode apply_criterion;
|
||||
uint8_t unknown_a2;
|
||||
|
||||
bool is_empty() const;
|
||||
static std::string str_for_arg(const std::string& arg);
|
||||
std::string str() const;
|
||||
} __attribute__((packed));
|
||||
|
||||
be_uint32_t card_id;
|
||||
parray<uint8_t, 0x40> jp_name;
|
||||
CardType type; // If <0 (signed), then this is the end of the card list
|
||||
uint8_t self_cost; // ATK dice points required
|
||||
uint8_t ally_cost; // ATK points from allies required; PBs use this
|
||||
uint8_t unused1;
|
||||
Stat hp;
|
||||
Stat ap;
|
||||
Stat tp;
|
||||
Stat mv;
|
||||
parray<uint8_t, 8> left_colors;
|
||||
parray<uint8_t, 8> right_colors;
|
||||
parray<uint8_t, 8> top_colors;
|
||||
parray<be_uint32_t, 6> range;
|
||||
be_uint32_t unused2;
|
||||
TargetMode target_mode;
|
||||
uint8_t assist_turns; // 90 (dec) = once, 99 (dec) = forever
|
||||
uint8_t cannot_move; // 0 for SC and creature cards; 1 for everything else
|
||||
uint8_t cannot_attack; // 1 for shields, mags, defense actions, and assist cards
|
||||
uint8_t unused3;
|
||||
uint8_t hide_in_deck_edit; // 0 = player can use this card (appears in deck edit)
|
||||
CriterionCode usable_criterion;
|
||||
CardRarity rarity;
|
||||
be_uint16_t unknown_a2;
|
||||
be_uint16_t be_card_class; // Used for checking attributes (e.g. item types)
|
||||
// These two fields seem to always contain the same value, and are always 0
|
||||
// for non-assist cards and nonzero for assists. Each assist card has a unique
|
||||
// value here and no effects, which makes it look like this is how assist
|
||||
// effects are implemented. There seems to be some 1k-modulation going on here
|
||||
// too; most cards are in the range 101-174 but a few have e.g. 1150, 2141. A
|
||||
// few pairs of cards have the same effect, which makes it look like some
|
||||
// other fields are also involved in determining their effects (see e.g. Skip
|
||||
// Draw / Skip Move, Dice Fever / Dice Fever +, Reverse Card / Rich +).
|
||||
parray<be_uint16_t, 2> assist_effect;
|
||||
// Drop rates are decimal-encoded with the following fields:
|
||||
// - rate % 10 (that is, the lowest decimal place) specifies the required game
|
||||
// mode. 0 means any mode, 1 means offline only, 2 means 1P free-battle, 3
|
||||
// means 2P+ free battle, 4 means story mode.
|
||||
// - (rate / 10) % 100 (that is, the tens and hundreds decimal places) specify
|
||||
// something else, but it's not clear what exactly.
|
||||
// - rate / 1000 (the thousands decimal place) specifies the level class
|
||||
// required to get this drop.
|
||||
// - rate / 10000 (the ten-thousands decimal place) must be either 0, 1, or 2,
|
||||
// but it's not clear yet what each value means.
|
||||
// The drop rates are completely ignored if any of the following are true
|
||||
// (which means the card can never be found in a normal post-battle draw):
|
||||
// - type is SC_HUNTERS or SC_ARKZ
|
||||
// - unknown_a3 is 0x23 or 0x24
|
||||
// - rarity is E, D1, D2, or INVIS
|
||||
// - hide_in_deck_edit is 1 (specifically 1; other nonzero values here don't
|
||||
// prevent the card from appearing in post-battle draws)
|
||||
parray<be_uint16_t, 2> drop_rates;
|
||||
ptext<char, 0x14> en_name;
|
||||
ptext<char, 0x0B> jp_short_name;
|
||||
ptext<char, 0x08> en_short_name;
|
||||
Effect effects[3];
|
||||
uint8_t unused4;
|
||||
|
||||
bool is_sc() const;
|
||||
bool is_fc() const;
|
||||
bool is_named_android_sc() const;
|
||||
bool any_top_color_matches(const CardDefinition& other) const;
|
||||
CardClass card_class() const;
|
||||
|
||||
void decode_range();
|
||||
std::string str() const;
|
||||
} __attribute__((packed)); // 0x128 bytes in total
|
||||
|
||||
struct CardDefinitionsFooter {
|
||||
be_uint32_t num_cards1;
|
||||
be_uint32_t unknown_a1;
|
||||
be_uint32_t num_cards2;
|
||||
be_uint32_t unknown_a2[11];
|
||||
be_uint32_t unknown_offset_a3;
|
||||
be_uint32_t unknown_a4[3];
|
||||
be_uint32_t footer_offset;
|
||||
be_uint32_t unknown_a5[3];
|
||||
} __attribute__((packed));
|
||||
|
||||
struct DeckDefinition {
|
||||
ptext<char, 0x10> name;
|
||||
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
|
||||
// the SC card, which the game implicitly subtracts from the limit - so a
|
||||
// valid deck should actually have 31 cards in it.
|
||||
parray<le_uint16_t, 50> card_ids;
|
||||
be_uint32_t unknown_a1;
|
||||
// Last modification time
|
||||
le_uint16_t year;
|
||||
uint8_t month;
|
||||
uint8_t day;
|
||||
uint8_t hour;
|
||||
uint8_t minute;
|
||||
uint8_t second;
|
||||
uint8_t unknown_a2;
|
||||
} __attribute__((packed)); // 0x84 bytes in total
|
||||
|
||||
struct PlayerConfig {
|
||||
// The first offsets in the comments in this struct are relative to start of
|
||||
// 61/98 command; the second are relative to the start of the
|
||||
// Ep3PlayerDataSegment structure in the reverse-engineering project.
|
||||
// TODO: Fill in the unknown fields here by looking around callsites of
|
||||
// get_player_data_segment
|
||||
/* 0728:---- */ parray<uint8_t, 0x154> unknown_a1;
|
||||
/* 087C:0000 */ uint8_t is_encrypted;
|
||||
/* 087D:0001 */ uint8_t basis;
|
||||
/* 087E:0002 */ parray<uint8_t, 2> unused;
|
||||
// The following fields (here through the beginning of decks) are encrypted
|
||||
// using the trivial algorithm, with the basis specified above, if
|
||||
// is_encrypted is equal to 1.
|
||||
// It appears the card counts field in this structure is actually 1000 (0x3E8)
|
||||
// bytes long, even though in every other place the counts array appears it's
|
||||
// 0x2F0 bytes long. They presumably did this because of the checksum logic.
|
||||
/* 0880:0004 */ parray<uint8_t, 1000> card_counts;
|
||||
// These appear to be an attempt at checksumming the card counts array, but
|
||||
// the algorithm don't cover the entire array and instead reads from later
|
||||
// parts of this structure. This appears to be due to a copy/paste error in
|
||||
// the original code. The algorithm sums card_counts [0] through [19] and puts
|
||||
// the result in card_count_checksums[0], then sums card counts [50] through
|
||||
// [69] and puts the result in card_count_checksums[1], etc. Presumably they
|
||||
// intended to use 20 as the stride instead of 50, which would have exactly
|
||||
// covered the entire card_counts array.
|
||||
/* 0C68:03EC */ parray<be_uint16_t, 50> card_count_checksums;
|
||||
// Yes, these are actually 64-bit integers. They include card IDs and some
|
||||
// other data, encoded in a way I don't fully understand yet.
|
||||
/* 0CCC:0450 */ parray<be_uint64_t, 0x1C2> unknown_a4;
|
||||
/* 1ADC:1260 */ parray<uint8_t, 0x80> unknown_a7;
|
||||
/* 1B5C:12E0 */ parray<DeckDefinition, 25> decks;
|
||||
/* 2840:1FC4 */ parray<uint8_t, 0x08> unknown_a8;
|
||||
/* 2848:1FCC */ be_uint32_t offline_clv_exp; // CLvOff = this / 100
|
||||
/* 284C:1FD0 */ be_uint32_t online_clv_exp; // CLvOn = this / 100
|
||||
struct PlayerReference {
|
||||
/* 00 */ be_uint32_t guild_card_number;
|
||||
/* 04 */ ptext<char, 0x18> player_name;
|
||||
} __attribute__((packed));
|
||||
// TODO: What do these player references mean? When are entries added to or
|
||||
// removed from this list?
|
||||
/* 2850:1FD4 */ parray<PlayerReference, 9> unknown_a9;
|
||||
/* 294C:20D0 */ parray<uint8_t, 0x50> unknown_a10;
|
||||
/* 299C:2120 */ ptext<char, 0x10> name;
|
||||
/* 29AC:2130 */ parray<uint8_t, 0xCC> unknown_a11;
|
||||
/* 2A78:21FC */
|
||||
} __attribute__((packed));
|
||||
|
||||
enum class HPType : uint8_t {
|
||||
DEFEAT_PLAYER = 0,
|
||||
DEFEAT_TEAM = 1,
|
||||
COMMON_HP = 2,
|
||||
};
|
||||
|
||||
enum class DiceExchangeMode : uint8_t {
|
||||
HIGH_ATK = 0,
|
||||
HIGH_DEF = 1,
|
||||
NONE = 2,
|
||||
};
|
||||
|
||||
enum class AllowedCards : uint8_t {
|
||||
ALL = 0,
|
||||
N_ONLY = 1,
|
||||
N_R_ONLY = 2,
|
||||
N_R_S_ONLY = 3,
|
||||
};
|
||||
|
||||
struct Rules {
|
||||
// When this structure is used in a map/quest definition, FF in any of these
|
||||
// fields means the user is allowed to override it. Any non-FF fields are
|
||||
// fixed for the map/quest and cannot be overridden.
|
||||
uint8_t overall_time_limit; // In increments of 5 minutes; 0 = unlimited
|
||||
uint8_t phase_time_limit; // In seconds; 0 = unlimited
|
||||
AllowedCards allowed_cards;
|
||||
uint8_t min_dice; // 0 = default (1)
|
||||
// 4
|
||||
uint8_t max_dice; // 0 = default (6)
|
||||
uint8_t disable_deck_shuffle; // 0 = shuffle on, 1 = off
|
||||
uint8_t disable_deck_loop; // 0 = loop on, 1 = off
|
||||
uint8_t char_hp;
|
||||
// 8
|
||||
HPType hp_type;
|
||||
uint8_t no_assist_cards; // 1 = assist cards disallowed
|
||||
uint8_t disable_dialogue; // 0 = dialogue on, 1 = dialogue off
|
||||
DiceExchangeMode dice_exchange_mode;
|
||||
// C
|
||||
uint8_t disable_dice_boost; // 0 = dice boost on, 1 = off
|
||||
parray<uint8_t, 3> unused;
|
||||
|
||||
Rules();
|
||||
explicit Rules(std::shared_ptr<const JSONObject> json);
|
||||
std::shared_ptr<JSONObject> json() const;
|
||||
bool operator==(const Rules& other) const;
|
||||
bool operator!=(const Rules& other) const;
|
||||
void clear();
|
||||
void set_defaults();
|
||||
|
||||
bool check_invalid_fields() const;
|
||||
bool check_and_reset_invalid_fields();
|
||||
|
||||
std::string str() const;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct StateFlags {
|
||||
le_uint16_t turn_num;
|
||||
BattlePhase battle_phase;
|
||||
uint8_t current_team_turn1;
|
||||
uint8_t current_team_turn2;
|
||||
ActionSubphase action_subphase;
|
||||
SetupPhase setup_phase;
|
||||
RegistrationPhase registration_phase;
|
||||
parray<le_uint32_t, 2> team_exp;
|
||||
parray<uint8_t, 2> team_dice_boost;
|
||||
uint8_t first_team_turn;
|
||||
uint8_t tournament_flag;
|
||||
parray<CardType, 4> client_sc_card_types;
|
||||
|
||||
StateFlags();
|
||||
bool operator==(const StateFlags& other) const;
|
||||
bool operator!=(const StateFlags& other) const;
|
||||
void clear();
|
||||
void clear_FF();
|
||||
} __attribute__((packed));
|
||||
|
||||
|
||||
|
||||
struct MapList {
|
||||
be_uint32_t num_maps;
|
||||
be_uint32_t unknown_a1; // Always 0?
|
||||
be_uint32_t strings_offset; // From after total_size field (add 0x10 to this value)
|
||||
be_uint32_t total_size; // Including header, entries, and strings
|
||||
|
||||
struct Entry { // Should be 0x220 bytes in total
|
||||
be_uint16_t map_x;
|
||||
be_uint16_t map_y;
|
||||
be_uint16_t environment_number;
|
||||
be_uint16_t map_number;
|
||||
// Text offsets are from the beginning of the strings block after all map
|
||||
// entries (that is, add strings_offset to them to get the string offset)
|
||||
be_uint32_t name_offset;
|
||||
be_uint32_t location_name_offset;
|
||||
be_uint32_t quest_name_offset;
|
||||
be_uint32_t description_offset;
|
||||
be_uint16_t width;
|
||||
be_uint16_t height;
|
||||
parray<parray<uint8_t, 0x10>, 0x10> map_tiles;
|
||||
parray<parray<uint8_t, 0x10>, 0x10> modification_tiles;
|
||||
// This appears to be 0xFF for free battle maps, and 0 for quests.
|
||||
// TODO: Figure out what this field's meaning actually is
|
||||
uint8_t unknown_a1;
|
||||
parray<uint8_t, 3> unused;
|
||||
} __attribute__((packed));
|
||||
|
||||
// Variable-length fields:
|
||||
// Entry entries[num_maps];
|
||||
// char strings[...EOF]; // Null-terminated strings, pointed to by offsets in Entry structs
|
||||
} __attribute__((packed));
|
||||
|
||||
struct CompressedMapHeader { // .mnm file format
|
||||
le_uint32_t map_number;
|
||||
le_uint32_t compressed_data_size;
|
||||
// Compressed data immediately follows (which decompresses to a MapDefinition)
|
||||
} __attribute__((packed));
|
||||
|
||||
struct MapDefinition { // .mnmd format; also the format of (decompressed) quests
|
||||
/* 0000 */ be_uint32_t unknown_a1;
|
||||
/* 0004 */ be_uint32_t map_number;
|
||||
/* 0008 */ uint8_t width;
|
||||
/* 0009 */ uint8_t height;
|
||||
// The environment number specifies several things:
|
||||
// - The model to load for the main battle stage
|
||||
// - The music to play during the main battle
|
||||
// - The color of the battle tile outlines (probably)
|
||||
// - The preview image to show in the upper-left corner in the map select menu
|
||||
// The environment numbers are:
|
||||
// 00 - Unguis Lapis
|
||||
// 01 - Nebula Montana (1)
|
||||
// 02 - Lupus Silva (1)
|
||||
// 03 - Lupus Silva (2)
|
||||
// 04 - Molae Venti
|
||||
// 05 - Nebula Montana (2)
|
||||
// 06 - Tener Sinus
|
||||
// 07 - Mortis Fons
|
||||
// 08 - Morgue (destroyed)
|
||||
// 09 - Tower of Caelum
|
||||
// 0A = ??? (referred to as "^mapname"; crashes)
|
||||
// 0B = Cyber
|
||||
// 0C = Morgue (not destroyed)
|
||||
// 0D = (Castor/Pollux map)
|
||||
// 0E - Dolor Odor
|
||||
// 0F = Ravum Aedes Sacra
|
||||
// 10 - (Amplum Umbla map)
|
||||
// 11 - Via Tubus
|
||||
// 12 = Morgue (same as 08?)
|
||||
// 13 = ??? (crashes)
|
||||
// Environment numbers beyond 13 are not used in any known quests or maps.
|
||||
/* 000A */ uint8_t environment_number;
|
||||
// All alt_maps fields (including the floats) past num_alt_maps are filled in
|
||||
// with FF. For example, if num_alt_maps == 8, the last two fields in each
|
||||
// alt_maps array are filled with FF.
|
||||
/* 000B */ uint8_t num_alt_maps; // TODO: What are the alt maps for?
|
||||
// In the map_tiles array, the values are usually:
|
||||
// 00 = not a valid tile (blocked)
|
||||
// 01 = valid tile unless modified out (via modification_tiles)
|
||||
// 02 = team A start (1v1)
|
||||
// 03, 04 = team A start (2v2)
|
||||
// 06, 07 = team B start (2v2)
|
||||
// 08 = team B start (1v1)
|
||||
// These values can be redefined by start_tile_definitions below, however.
|
||||
// Note that the game displays the map reversed vertically in the preview
|
||||
// window. For example, player 1 is on team A, which usually starts at the top
|
||||
// of the map as defined in this struct, or at the bottom as shown in the
|
||||
// preview window.
|
||||
/* 000C */ parray<parray<uint8_t, 0x10>, 0x10> map_tiles;
|
||||
// The start_tile_definitions field is a list of 6 bytes for each team. The
|
||||
// low 6 bits of each byte match the starting location for the relevant player
|
||||
// in map_tiles; the high 2 bits are the player's initial facing direction.
|
||||
// - If the team has 1 player, only byte [0] is used.
|
||||
// - If the team has 2 players, bytes [1] and [2] are used.
|
||||
// - If the team has 3 players, bytes [3] through [5] are used.
|
||||
/* 010C */ parray<parray<uint8_t, 6>, 2> start_tile_definitions;
|
||||
/* 0118 */ parray<parray<uint8_t, 0x10>, 0x10> alt_maps1[2][0x0A];
|
||||
/* 1518 */ parray<be_float, 0x12> alt_maps_unknown_a3[2][0x0A];
|
||||
/* 1AB8 */ parray<be_float, 0x24> unknown_a5[3];
|
||||
// In the modification_tiles array, the values are:
|
||||
// 10 = blocked by rock (as if the corresponding map_tiles value was 00)
|
||||
// 20 = blocked by fence (as if the corresponding map_tiles value was 00)
|
||||
// 30-34 = teleporters (2 of each value may be present)
|
||||
// 40-44 = traps (one of each type is chosen at random to be a real trap at
|
||||
// battle start time)
|
||||
// 50 = blocked by metal box (appears as improperly-z-buffered teal cube in
|
||||
// preview; behaves like 10 and 20 in game)
|
||||
/* 1C68 */ parray<parray<uint8_t, 0x10>, 0x10> modification_tiles;
|
||||
/* 1D68 */ parray<uint8_t, 0x74> unknown_a6;
|
||||
/* 1DDC */ Rules default_rules;
|
||||
/* 1DEC */ parray<uint8_t, 4> unknown_a7;
|
||||
/* 1DF0 */ ptext<char, 0x14> name;
|
||||
/* 1E04 */ ptext<char, 0x14> location_name;
|
||||
/* 1E18 */ ptext<char, 0x3C> quest_name; // == location_name if not a quest
|
||||
/* 1E54 */ ptext<char, 0x190> description;
|
||||
/* 1FE4 */ be_uint16_t map_x;
|
||||
/* 1FE6 */ be_uint16_t map_y;
|
||||
struct NPCDeck {
|
||||
ptext<char, 0x18> name;
|
||||
parray<be_uint16_t, 0x20> card_ids; // Last one appears to always be FFFF
|
||||
} __attribute__((packed));
|
||||
/* 1FE8 */ NPCDeck npc_decks[3]; // Unused if name[0] == 0
|
||||
struct NPCCharacter {
|
||||
parray<be_uint16_t, 2> unknown_a1;
|
||||
parray<uint8_t, 4> unknown_a2;
|
||||
ptext<char, 0x10> name;
|
||||
parray<be_uint16_t, 0x7E> unknown_a3;
|
||||
} __attribute__((packed));
|
||||
/* 20F0 */ NPCCharacter npc_chars[3]; // Unused if name[0] == 0
|
||||
/* 242C */ parray<uint8_t, 0x14> unknown_a8; // Always FF?
|
||||
/* 2440 */ ptext<char, 0x190> before_message;
|
||||
/* 25D0 */ ptext<char, 0x190> after_message;
|
||||
/* 2760 */ ptext<char, 0x190> dispatch_message; // Usually "You can only dispatch <character>" or blank
|
||||
struct DialogueSet {
|
||||
be_uint16_t unknown_a1;
|
||||
be_uint16_t unknown_a2; // Always 0x0064 if valid, 0xFFFF if unused?
|
||||
ptext<char, 0x40> strings[4];
|
||||
} __attribute__((packed)); // Total size: 0x104 bytes
|
||||
/* 28F0 */ DialogueSet dialogue_sets[3][0x10]; // Up to 0x10 per valid NPC
|
||||
/* 59B0 */ parray<be_uint16_t, 0x10> reward_card_ids;
|
||||
/* 59D0 */ parray<uint8_t, 0x0C> unknown_a9;
|
||||
/* 59DC */ uint8_t unknown_a10;
|
||||
/* 59DD */ parray<uint8_t, 3> unknown_a11;
|
||||
// This array specifies which SC characters can't participate in the quest
|
||||
// (that is, the player is not allowed to choose decks with these SC cards).
|
||||
// The values in this array don't match the SC card IDs, however:
|
||||
// 0000 => Guykild (0005) 000C => Hyze (0117)
|
||||
// 0001 => Kylria (0006) 000D => Rufina (0118)
|
||||
// 0002 => Saligun (0110) 000E => Peko (0119)
|
||||
// 0003 => Relmitos (0111) 000F => Creinu (011A)
|
||||
// 0004 => Kranz (0002) 0010 => Reiz (011B)
|
||||
// 0005 => Sil'fer (0004) 0011 => Lura (0007)
|
||||
// 0006 => Ino'lis (0003) 0012 => Break (0008)
|
||||
// 0007 => Viviana (0112) 0013 => Rio (011C)
|
||||
// 0008 => Teifu (0113) 0014 => Endu (0116)
|
||||
// 0009 => Orland (0001) 0015 => Memoru (011D)
|
||||
// 000A => Stella (0114) 0016 => K.C. (011E)
|
||||
// 000B => Glustar (0115) 0017 => Ohgun (011F)
|
||||
// Unused entries in this array should be set to FFFF.
|
||||
/* 59E0 */ parray<be_uint16_t, 0x18> unavailable_sc_cards;
|
||||
struct EntryState {
|
||||
// Values for player_type:
|
||||
// 00 = Player (selectable by player, COM decks not allowed)
|
||||
// 01 = Player/COM (selectable by player)
|
||||
// 02 = COM (selectable by player, player decks not allowed)
|
||||
// 03 = COM (not selectable by player; uses NPC deck)
|
||||
// 04 = NONE (not selectable by player)
|
||||
// FF = FREE (same as Player/COM, used in free battle mode)
|
||||
uint8_t player_type;
|
||||
// Values for deck_type:
|
||||
// 00 = HERO ONLY
|
||||
// 01 = DARK ONLY
|
||||
// FF = any deck allowed
|
||||
uint8_t deck_type;
|
||||
} __attribute__((packed));
|
||||
/* 5A10 */ parray<EntryState, 4> entry_states;
|
||||
/* 5A18 */
|
||||
|
||||
std::string str(const DataIndex* data_index = nullptr) const;
|
||||
} __attribute__((packed));
|
||||
|
||||
|
||||
|
||||
struct COMDeckDefinition {
|
||||
size_t index;
|
||||
std::string player_name;
|
||||
std::string deck_name;
|
||||
parray<le_uint16_t, 0x1F> card_ids;
|
||||
};
|
||||
|
||||
|
||||
|
||||
class DataIndex {
|
||||
public:
|
||||
DataIndex(const std::string& directory, uint32_t behavior_flags);
|
||||
|
||||
struct CardEntry {
|
||||
CardDefinition def;
|
||||
std::string text;
|
||||
std::vector<std::string> debug_tags; // Empty unless debug == true
|
||||
};
|
||||
|
||||
class MapEntry {
|
||||
public:
|
||||
MapDefinition map;
|
||||
bool is_quest;
|
||||
|
||||
MapEntry(const MapDefinition& map, bool is_quest);
|
||||
MapEntry(const std::string& compressed_data, bool is_quest);
|
||||
|
||||
std::string compressed() const;
|
||||
|
||||
private:
|
||||
mutable std::string compressed_data;
|
||||
};
|
||||
|
||||
const std::string& get_compressed_card_definitions() const;
|
||||
std::shared_ptr<const CardEntry> definition_for_card_id(uint32_t id) const;
|
||||
std::shared_ptr<const CardEntry> definition_for_card_name(
|
||||
const std::string& name) const;
|
||||
std::set<uint32_t> all_card_ids() const;
|
||||
uint64_t card_definitions_mtime() const;
|
||||
|
||||
const std::string& get_compressed_map_list() const;
|
||||
std::shared_ptr<const MapEntry> definition_for_map_number(uint32_t id) const;
|
||||
std::shared_ptr<const MapEntry> definition_for_map_name(
|
||||
const std::string& name) const;
|
||||
std::set<uint32_t> all_map_ids() const;
|
||||
|
||||
size_t num_com_decks() const;
|
||||
std::shared_ptr<const COMDeckDefinition> com_deck(size_t which) const;
|
||||
std::shared_ptr<const COMDeckDefinition> com_deck(const std::string& name) const;
|
||||
std::shared_ptr<const COMDeckDefinition> random_com_deck() const;
|
||||
|
||||
const uint32_t behavior_flags;
|
||||
|
||||
private:
|
||||
std::string compressed_card_definitions;
|
||||
std::unordered_map<uint32_t, std::shared_ptr<CardEntry>> card_definitions;
|
||||
std::unordered_map<std::string, std::shared_ptr<CardEntry>> card_definitions_by_name;
|
||||
uint64_t mtime_for_card_definitions;
|
||||
|
||||
// The compressed map list is generated on demand from the maps map below.
|
||||
// It's marked mutable because the logical consistency of the DataIndex object
|
||||
// is not violated from the caller's perspective even if we don't generate the
|
||||
// compressed map list at load time.
|
||||
mutable std::string compressed_map_list;
|
||||
std::map<uint32_t, std::shared_ptr<MapEntry>> maps;
|
||||
std::unordered_map<std::string, std::shared_ptr<MapEntry>> maps_by_name;
|
||||
|
||||
std::vector<std::shared_ptr<COMDeckDefinition>> com_decks;
|
||||
std::unordered_map<std::string, std::shared_ptr<COMDeckDefinition>> com_decks_by_name;
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Episode3
|
||||
@@ -0,0 +1,286 @@
|
||||
#include "DeckState.hh"
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace Episode3 {
|
||||
|
||||
|
||||
|
||||
NameEntry::NameEntry() {
|
||||
this->clear();
|
||||
}
|
||||
|
||||
void NameEntry::clear() {
|
||||
this->client_id = 0xFF;
|
||||
this->present = 0;
|
||||
this->unused_by_server = 0;
|
||||
this->unused = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
DeckEntry::DeckEntry() {
|
||||
this->clear();
|
||||
}
|
||||
|
||||
void DeckEntry::clear() {
|
||||
this->team_id = 0xFFFFFFFF;
|
||||
this->god_whim_flag = 3;
|
||||
this->unused1 = 0;
|
||||
this->player_level = 0;
|
||||
this->unused2.clear(0);
|
||||
this->card_ids.clear(0xFFFF);
|
||||
}
|
||||
|
||||
|
||||
|
||||
uint8_t index_for_card_ref(uint16_t card_ref) {
|
||||
return card_ref & 0xFF;
|
||||
}
|
||||
|
||||
uint8_t client_id_for_card_ref(uint16_t card_ref) {
|
||||
return (card_ref >> 8) & 0xFF;
|
||||
}
|
||||
|
||||
|
||||
|
||||
uint8_t DeckState::num_drawable_cards() const {
|
||||
return this->card_refs.size() - this->draw_index;
|
||||
}
|
||||
|
||||
bool DeckState::set_card_ref_in_play(uint16_t card_ref) {
|
||||
if (!this->contains_card_ref(card_ref)) {
|
||||
return false;
|
||||
}
|
||||
uint8_t index = index_for_card_ref(card_ref);
|
||||
if (this->entries[index].state == CardState::IN_HAND) {
|
||||
this->entries[index].state = CardState::IN_PLAY;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool DeckState::contains_card_ref(uint16_t card_ref) const {
|
||||
return index_for_card_ref(card_ref) < this->entries.size();
|
||||
}
|
||||
|
||||
void DeckState::disable_loop() {
|
||||
this->loop_enabled = false;
|
||||
}
|
||||
|
||||
void DeckState::disable_shuffle() {
|
||||
this->shuffle_enabled = false;
|
||||
}
|
||||
|
||||
uint16_t DeckState::draw_card() {
|
||||
if (this->num_drawable_cards() == 0) {
|
||||
this->restart();
|
||||
}
|
||||
if (this->num_drawable_cards() == 0) {
|
||||
return 0xFFFF;
|
||||
}
|
||||
|
||||
uint16_t ref = this->card_refs[this->draw_index++];
|
||||
this->entries[index_for_card_ref(ref)].state = CardState::IN_HAND;
|
||||
return ref;
|
||||
}
|
||||
|
||||
bool DeckState::draw_card_by_ref(uint16_t card_ref) {
|
||||
if (card_ref == 0xFFFF) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t index = index_for_card_ref(card_ref);
|
||||
if (index > this->entries.size()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// If the card is discarded, then it should be before the draw index, and we
|
||||
// can just change its state.
|
||||
if (this->entries[index].state == CardState::DISCARDED) {
|
||||
this->entries[index].state = CardState::IN_HAND;
|
||||
return true;
|
||||
|
||||
// If the card is still drawable, we need to move it so it's just in front of
|
||||
// the draw index, then immediately draw it
|
||||
} else if (this->entries[index].state == CardState::DRAWABLE) {
|
||||
ssize_t ref_index;
|
||||
for (ref_index = this->card_refs.size(); ref_index >= 0; ref_index--) {
|
||||
if (this->card_refs[ref_index] == card_ref) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ref_index < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t ref_uindex = ref_index;
|
||||
for (; ref_uindex > this->draw_index; ref_uindex--) {
|
||||
// Note: draw_index is also unsigned, so ref_uindex cannot be zero here
|
||||
this->card_refs[ref_uindex] = this->card_refs[ref_uindex - 1];
|
||||
}
|
||||
this->card_refs[this->draw_index] = card_ref;
|
||||
this->entries[index].state = CardState::IN_HAND;
|
||||
this->draw_index++;
|
||||
return true;
|
||||
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t DeckState::card_id_for_card_ref(uint16_t card_ref) const {
|
||||
if (card_ref == 0xFFFF) {
|
||||
return 0xFFFF;
|
||||
}
|
||||
|
||||
uint8_t index = index_for_card_ref(card_ref);
|
||||
if (index < this->entries.size()) {
|
||||
return this->entries[index].card_id;
|
||||
} else {
|
||||
return 0xFFFF;
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t DeckState::sc_card_id() const {
|
||||
return this->entries[0].card_id;
|
||||
}
|
||||
|
||||
uint16_t DeckState::sc_card_ref() const {
|
||||
return this->card_refs[0];
|
||||
}
|
||||
|
||||
uint16_t DeckState::card_ref_for_index(uint8_t index) const {
|
||||
return this->card_ref_base | index;
|
||||
}
|
||||
|
||||
DeckState::CardState DeckState::state_for_card_ref(uint16_t card_ref) const {
|
||||
uint8_t index = index_for_card_ref(card_ref);
|
||||
return (index < this->entries.size()) ? this->entries[index].state : CardState::INVALID;
|
||||
}
|
||||
|
||||
void DeckState::restart() {
|
||||
// First, if deck loop is on, return all discarded cards to the drawable state
|
||||
if (this->loop_enabled) {
|
||||
for (size_t z = 0; z < this->entries.size(); z++) {
|
||||
if (this->entries[z].state == CardState::DISCARDED) {
|
||||
this->entries[z].state = CardState::DRAWABLE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// For any cards that are still in hand or still in play, move their refs to
|
||||
// the already-drawn part of the deck
|
||||
this->draw_index = 0;
|
||||
for (size_t z = 0; z < this->entries.size(); z++) {
|
||||
if (this->entries[z].state != CardState::DRAWABLE) {
|
||||
this->card_refs[this->draw_index++] = this->card_ref_for_index(z);
|
||||
}
|
||||
}
|
||||
|
||||
// For now-drawable cards, put their refs after the draw index
|
||||
size_t index = this->draw_index;
|
||||
for (size_t z = 0; z < this->entries.size(); z++) {
|
||||
if (this->entries[z].state == CardState::DRAWABLE) {
|
||||
this->card_refs[index++] = this->card_ref_for_index(z);
|
||||
}
|
||||
}
|
||||
|
||||
this->shuffle();
|
||||
}
|
||||
|
||||
void DeckState::do_mulligan() {
|
||||
for (size_t z = 0; z < this->entries.size(); z++) {
|
||||
if (this->entries[z].state == CardState::DISCARDED) {
|
||||
this->entries[z].state = CardState::DRAWABLE;
|
||||
}
|
||||
}
|
||||
this->draw_index = 1;
|
||||
|
||||
if (this->shuffle_enabled) {
|
||||
// Get the next 5 cards from the deck, and put the previous 5 cards after
|
||||
// them (so they will be shuffled back in).
|
||||
for (uint8_t z = 0; z < 5; z++) {
|
||||
uint8_t index = z + this->draw_index;
|
||||
uint16_t temp_ref = this->card_refs[index];
|
||||
this->card_refs[index] = this->card_refs[index + 5];
|
||||
this->card_refs[index + 5] = temp_ref;
|
||||
}
|
||||
|
||||
// Shuffle the deck, except the first 5 cards (which are about to be drawn).
|
||||
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;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool DeckState::set_card_ref_drawable_next(uint16_t card_ref) {
|
||||
if (card_ref == 0xFFFF) {
|
||||
return false;
|
||||
}
|
||||
if (client_id_for_card_ref(card_ref) != this->client_id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t index = index_for_card_ref(card_ref);
|
||||
if (this->entries[index].state == CardState::DRAWABLE) {
|
||||
return false;
|
||||
} else if (this->draw_index < 1) {
|
||||
return false;
|
||||
} else {
|
||||
this->entries[index].state = CardState::DRAWABLE;
|
||||
this->card_refs[--this->draw_index] = card_ref;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool DeckState::set_card_ref_drawable_at_end(uint16_t card_ref) {
|
||||
if (this->set_card_ref_drawable_next(card_ref)) {
|
||||
uint16_t head_card_ref = this->card_refs[this->draw_index];
|
||||
if (this->draw_index < this->card_refs.size() - 1) {
|
||||
for (size_t z = this->draw_index; z < this->card_refs.size() - 1; z++) {
|
||||
this->card_refs[z] = this->card_refs[z + 1];
|
||||
}
|
||||
}
|
||||
this->card_refs[this->card_refs.size() - 1] = head_card_ref;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void DeckState::set_card_discarded(uint16_t card_ref) {
|
||||
uint8_t index = index_for_card_ref(card_ref);
|
||||
if (index < this->entries.size()) {
|
||||
this->entries[index].state = CardState::DISCARDED;
|
||||
}
|
||||
}
|
||||
|
||||
void DeckState::shuffle() {
|
||||
if (this->shuffle_enabled) {
|
||||
size_t max = this->num_drawable_cards();
|
||||
for (size_t z = 0; z < this->card_refs.size(); z++) {
|
||||
// Note: This is the way Sega originally implemented shuffling - they just
|
||||
// do N swaps on the entire array. A more uniform way to do it would be to
|
||||
// instead swap each item with another random item (possibly itself) that
|
||||
// doesn't appear earlier than it in the array, but this is not what Sega
|
||||
// did.
|
||||
uint8_t index1 = this->draw_index + this->random_crypt->next() % max;
|
||||
uint8_t index2 = this->draw_index + this->random_crypt->next() % max;
|
||||
uint16_t temp_ref = this->card_refs[index1];
|
||||
this->card_refs[index1] = this->card_refs[index2];
|
||||
this->card_refs[index2] = temp_ref;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace Episode3
|
||||
@@ -0,0 +1,117 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "../Text.hh"
|
||||
#include "../PSOEncryption.hh"
|
||||
|
||||
namespace Episode3 {
|
||||
|
||||
|
||||
|
||||
struct NameEntry {
|
||||
parray<char, 0x10> name;
|
||||
uint8_t client_id;
|
||||
uint8_t present;
|
||||
uint8_t unused_by_server;
|
||||
uint8_t unused;
|
||||
|
||||
NameEntry();
|
||||
void clear();
|
||||
} __attribute__((packed));
|
||||
|
||||
struct DeckEntry {
|
||||
ptext<char, 0x10> name;
|
||||
le_uint32_t team_id;
|
||||
parray<le_uint16_t, 0x1F> card_ids;
|
||||
// If the following flag is not set to 3, then the God Whim assist effect can
|
||||
// use cards that are hidden from the player during deck building. The client
|
||||
// always sets this to 3, and it's not clear why this even exists.
|
||||
uint8_t god_whim_flag;
|
||||
uint8_t unused1;
|
||||
le_uint16_t player_level;
|
||||
parray<uint8_t, 2> unused2;
|
||||
|
||||
DeckEntry();
|
||||
void clear();
|
||||
} __attribute__((packed));
|
||||
|
||||
uint8_t index_for_card_ref(uint16_t card_ref);
|
||||
uint8_t client_id_for_card_ref(uint16_t card_ref);
|
||||
|
||||
class DeckState {
|
||||
public:
|
||||
enum class CardState {
|
||||
DRAWABLE = 0,
|
||||
STORY_CHARACTER = 1,
|
||||
IN_HAND = 2,
|
||||
IN_PLAY = 3,
|
||||
DISCARDED = 4,
|
||||
INVALID = 5,
|
||||
};
|
||||
|
||||
template <typename CardIDT>
|
||||
DeckState(
|
||||
uint8_t client_id,
|
||||
const parray<CardIDT, 0x1F>& card_ids,
|
||||
std::shared_ptr<PSOV2Encryption> random_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) {
|
||||
for (size_t z = 0; z < card_ids.size(); z++) {
|
||||
auto& e = this->entries[z];
|
||||
e.card_id = card_ids[z];
|
||||
e.deck_index = z;
|
||||
e.state = CardState::DRAWABLE;
|
||||
this->card_refs[z] = this->card_ref_for_index(z);
|
||||
}
|
||||
this->entries[0].state = CardState::STORY_CHARACTER;
|
||||
}
|
||||
|
||||
void disable_loop();
|
||||
void disable_shuffle();
|
||||
|
||||
uint8_t num_drawable_cards() const;
|
||||
bool contains_card_ref(uint16_t card_ref) const;
|
||||
uint16_t card_id_for_card_ref(uint16_t card_ref) const;
|
||||
uint16_t sc_card_id() const;
|
||||
uint16_t sc_card_ref() const;
|
||||
uint16_t card_ref_for_index(uint8_t index) const;
|
||||
CardState state_for_card_ref(uint16_t card_ref) const;
|
||||
|
||||
uint16_t draw_card();
|
||||
bool draw_card_by_ref(uint16_t card_ref);
|
||||
bool set_card_ref_in_play(uint16_t card_ref);
|
||||
bool set_card_ref_drawable_next(uint16_t card_ref);
|
||||
bool set_card_ref_drawable_at_end(uint16_t card_ref);
|
||||
void set_card_discarded(uint16_t card_ref);
|
||||
|
||||
void restart();
|
||||
void shuffle();
|
||||
void do_mulligan();
|
||||
|
||||
private:
|
||||
struct CardEntry {
|
||||
uint16_t card_id;
|
||||
uint8_t deck_index;
|
||||
CardState state;
|
||||
};
|
||||
uint8_t client_id;
|
||||
uint8_t draw_index;
|
||||
uint16_t card_ref_base;
|
||||
bool shuffle_enabled;
|
||||
bool loop_enabled;
|
||||
parray<CardEntry, 31> entries;
|
||||
parray<uint16_t, 31> card_refs;
|
||||
|
||||
std::shared_ptr<PSOV2Encryption> random_crypt;
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Episode3
|
||||
@@ -0,0 +1,95 @@
|
||||
#include "MapState.hh"
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace Episode3 {
|
||||
|
||||
|
||||
|
||||
MapState::MapState() {
|
||||
this->clear();
|
||||
}
|
||||
|
||||
void MapState::clear() {
|
||||
this->width = 0;
|
||||
this->height = 0;
|
||||
for (size_t y = 0; y < this->tiles.size(); y++) {
|
||||
this->tiles[y].clear(0);
|
||||
}
|
||||
for (size_t z = 0; z < 2; z++) {
|
||||
this->start_tile_definitions[z].clear(0);
|
||||
}
|
||||
}
|
||||
|
||||
void MapState::print(FILE* stream) const {
|
||||
fprintf(stream, "[Map: w=%hu h=%hu]\n", this->width.load(), this->height.load());
|
||||
for (size_t y = 0; y < this->height; y++) {
|
||||
fputc(' ', stream);
|
||||
for (size_t x = 0; x < this->width; x++) {
|
||||
fprintf(stream, " %02hhX", this->tiles[y][x]);
|
||||
}
|
||||
fputc('\n', stream);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
MapAndRulesState::MapAndRulesState() {
|
||||
this->clear();
|
||||
}
|
||||
|
||||
void MapAndRulesState::clear() {
|
||||
this->map.clear();
|
||||
this->num_players = 0;
|
||||
this->unused1 = 0;
|
||||
this->unused_by_server = 0;
|
||||
this->num_players_per_team = 0;
|
||||
this->num_team0_players = 0;
|
||||
this->unused2 = 0;
|
||||
this->start_facing_directions = 0;
|
||||
this->unused3 = 0;
|
||||
this->map_number = 0;
|
||||
this->unused4 = 0;
|
||||
this->rules.clear();
|
||||
this->unused5 = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool MapAndRulesState::loc_is_within_bounds(uint8_t x, uint8_t y) const {
|
||||
return (x < this->map.width) && (y < this->map.height);
|
||||
}
|
||||
|
||||
bool MapAndRulesState::tile_is_vacant(uint8_t x, uint8_t y) {
|
||||
if (!this->loc_is_within_bounds(x, y)) {
|
||||
return false;
|
||||
}
|
||||
return (this->map.tiles[y][x] == 1);
|
||||
}
|
||||
|
||||
void MapAndRulesState::set_occupied_bit_for_tile(uint8_t x, uint8_t y) {
|
||||
this->map.tiles[y][x] |= 0x10;
|
||||
}
|
||||
|
||||
void MapAndRulesState::clear_occupied_bit_for_tile(uint8_t x, uint8_t y) {
|
||||
this->map.tiles[y][x] &= 0xEF;
|
||||
}
|
||||
|
||||
|
||||
|
||||
OverlayState::OverlayState() {
|
||||
this->clear();
|
||||
}
|
||||
|
||||
void OverlayState::clear() {
|
||||
for (size_t y = 0; y < this->tiles.size(); y++) {
|
||||
this->tiles[y].clear(0);
|
||||
}
|
||||
this->unused1.clear(0);
|
||||
this->unused2.clear(0);
|
||||
this->unused3.clear(0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace Episode3
|
||||
@@ -0,0 +1,63 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "../Text.hh"
|
||||
#include "DataIndex.hh"
|
||||
|
||||
namespace Episode3 {
|
||||
|
||||
|
||||
|
||||
struct MapState {
|
||||
le_uint16_t width;
|
||||
le_uint16_t height;
|
||||
parray<parray<uint8_t, 0x10>, 0x10> tiles;
|
||||
parray<parray<uint8_t, 6>, 2> start_tile_definitions;
|
||||
|
||||
MapState();
|
||||
void clear();
|
||||
|
||||
void print(FILE* stream) const;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct MapAndRulesState {
|
||||
MapState map;
|
||||
uint8_t num_players;
|
||||
uint8_t unused1;
|
||||
uint8_t unused_by_server;
|
||||
uint8_t num_players_per_team;
|
||||
uint8_t num_team0_players;
|
||||
uint8_t unused2;
|
||||
le_uint16_t start_facing_directions;
|
||||
uint32_t unused3;
|
||||
le_uint32_t map_number;
|
||||
uint32_t unused4;
|
||||
Rules rules;
|
||||
uint32_t unused5;
|
||||
|
||||
MapAndRulesState();
|
||||
void clear();
|
||||
|
||||
bool loc_is_within_bounds(uint8_t x, uint8_t y) const;
|
||||
bool tile_is_vacant(uint8_t x, uint8_t y);
|
||||
|
||||
void set_occupied_bit_for_tile(uint8_t x, uint8_t y);
|
||||
void clear_occupied_bit_for_tile(uint8_t x, uint8_t y);
|
||||
} __attribute__((packed));
|
||||
|
||||
struct OverlayState {
|
||||
parray<parray<uint8_t, 0x10>, 0x10> tiles;
|
||||
parray<le_uint32_t, 5> unused1;
|
||||
parray<le_uint32_t, 0x10> unused2;
|
||||
parray<le_uint16_t, 0x10> unused3;
|
||||
|
||||
OverlayState();
|
||||
void clear();
|
||||
} __attribute__((packed));
|
||||
|
||||
|
||||
|
||||
} // namespace Episode3
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,193 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "../Text.hh"
|
||||
#include "DataIndex.hh"
|
||||
#include "Card.hh"
|
||||
#include "DeckState.hh"
|
||||
#include "PlayerStateSubordinates.hh"
|
||||
|
||||
namespace Episode3 {
|
||||
|
||||
|
||||
|
||||
class ServerBase;
|
||||
class Server;
|
||||
|
||||
class PlayerState : public std::enable_shared_from_this<PlayerState> {
|
||||
public:
|
||||
PlayerState(uint8_t client_id, std::shared_ptr<Server> server);
|
||||
void init();
|
||||
std::shared_ptr<Server> server();
|
||||
std::shared_ptr<const Server> server() const;
|
||||
|
||||
bool draw_cards_allowed() const;
|
||||
void apply_assist_card_effect_on_set(
|
||||
std::shared_ptr<PlayerState> setter_ps);
|
||||
void apply_dice_effects();
|
||||
uint16_t card_ref_for_hand_index(size_t hand_index) const;
|
||||
int16_t compute_attack_or_defense_atk_costs(const ActionState& pa) const;
|
||||
void compute_total_set_cards_cost();
|
||||
size_t count_set_cards() const;
|
||||
size_t count_set_refs() const;
|
||||
void discard_all_assist_cards_from_hand();
|
||||
void discard_all_attack_action_cards_from_hand();
|
||||
void discard_all_item_and_creature_cards_from_hand();
|
||||
void discard_and_redraw_hand();
|
||||
bool discard_card_or_add_to_draw_pile(
|
||||
uint16_t card_ref, bool add_to_draw_pile);
|
||||
void discard_random_hand_card();
|
||||
bool discard_ref_from_hand(uint16_t card_ref);
|
||||
void discard_set_assist_card();
|
||||
bool do_mulligan();
|
||||
void draw_hand(ssize_t override_count = 0);
|
||||
void draw_initial_hand();
|
||||
int32_t error_code_for_client_setting_card(
|
||||
uint16_t card_ref,
|
||||
uint8_t card_index,
|
||||
const Location* loc,
|
||||
uint8_t assist_target_client_id) const;
|
||||
std::vector<uint16_t> get_all_cards_within_range(
|
||||
const parray<uint8_t, 9 * 9>& range,
|
||||
const Location& loc,
|
||||
uint8_t target_team_id) const;
|
||||
uint8_t get_atk_points() const;
|
||||
void get_short_status_for_card_index_in_hand(
|
||||
size_t hand_index, CardShortStatus* stat) const;
|
||||
std::shared_ptr<DeckState> get_deck();
|
||||
uint8_t get_def_points() const;
|
||||
uint8_t get_dice_result(size_t which) const;
|
||||
size_t get_hand_size() const;
|
||||
uint16_t get_sc_card_id() const;
|
||||
std::shared_ptr<Card> get_sc_card();
|
||||
std::shared_ptr<const Card> get_sc_card() const;
|
||||
uint16_t get_sc_card_ref() const;
|
||||
CardType get_sc_card_type() const;
|
||||
std::shared_ptr<Card> get_set_card(size_t set_index);
|
||||
std::shared_ptr<const Card> get_set_card(size_t set_index) const;
|
||||
uint16_t get_set_ref(size_t set_index) const;
|
||||
uint8_t get_team_id() const;
|
||||
ssize_t hand_index_for_card_ref(uint16_t card_ref) const;
|
||||
size_t set_index_for_card_ref(uint16_t card_ref) const;
|
||||
bool is_mulligan_allowed() const;
|
||||
bool is_team_turn() const;
|
||||
void log_discard(uint16_t card_ref, uint16_t reason);
|
||||
bool move_card_to_location_by_card_index(
|
||||
size_t card_index, const Location& new_loc);
|
||||
void move_null_hand_refs_to_end();
|
||||
void on_cards_destroyed();
|
||||
void replace_all_set_assists_with_random_assists();
|
||||
bool replace_assist_card_by_id(uint16_t card_id);
|
||||
bool return_set_card_to_hand2(uint16_t card_ref);
|
||||
bool return_set_card_to_hand1(uint16_t card_ref);
|
||||
uint8_t roll_dice(size_t num_dice);
|
||||
uint8_t roll_dice_with_effects(size_t num_dice);
|
||||
void send_set_card_updates(bool always_send = false);
|
||||
void set_assist_flags_from_assist_effects();
|
||||
bool set_card_from_hand(
|
||||
uint16_t card_ref,
|
||||
uint8_t card_index,
|
||||
const Location* loc,
|
||||
uint8_t assist_target_client_id,
|
||||
bool skip_error_checks_and_atk_sub);
|
||||
void set_initial_location();
|
||||
void set_map_occupied_bit_for_card_on_warp_tile(
|
||||
std::shared_ptr<const Card> card);
|
||||
void set_map_occupied_bits_for_sc_and_creatures();
|
||||
void subtract_def_points(uint8_t cost);
|
||||
bool subtract_or_check_atk_or_def_points_for_action(
|
||||
const ActionState& pa, bool deduct_points);
|
||||
void subtract_atk_points(uint8_t cost);
|
||||
void update_hand_and_equip_state_and_send_6xB4x02_if_needed(
|
||||
bool always_send = false);
|
||||
void set_random_assist_card_from_hand_for_free();
|
||||
void send_6xB4x04_if_needed(bool always_send = false);
|
||||
std::vector<uint16_t> get_card_refs_within_range_from_all_players(
|
||||
const parray<uint8_t, 9 * 9>& range,
|
||||
const Location& loc,
|
||||
CardType type) const;
|
||||
void unknown_80239460();
|
||||
void unknown_802394C4();
|
||||
void unknown_80239528();
|
||||
void handle_before_turn_assist_effects();
|
||||
int16_t get_assist_turns_remaining();
|
||||
bool set_action_cards_for_action_state(const ActionState& pa);
|
||||
void unknown_8023C174();
|
||||
void handle_homesick_assist_effect(std::shared_ptr<Card> card);
|
||||
void apply_main_die_assist_effects(uint8_t* die_value) const;
|
||||
void roll_main_dice();
|
||||
void unknown_8023C110();
|
||||
void compute_team_dice_boost_after_draw_phase();
|
||||
|
||||
private:
|
||||
std::weak_ptr<Server> w_server;
|
||||
|
||||
public:
|
||||
std::shared_ptr<Card> sc_card;
|
||||
std::shared_ptr<Card> set_cards[8];
|
||||
uint8_t client_id;
|
||||
uint16_t num_mulligans_allowed;
|
||||
CardType sc_card_type;
|
||||
uint8_t team_id;
|
||||
uint8_t atk_points;
|
||||
uint8_t def_points;
|
||||
uint8_t atk_points2;
|
||||
uint8_t atk_points2_max;
|
||||
uint8_t atk_bonuses;
|
||||
uint8_t def_bonuses;
|
||||
parray<uint8_t, 2> dice_results;
|
||||
uint8_t unknown_a4;
|
||||
uint8_t dice_max;
|
||||
uint8_t total_set_cards_cost;
|
||||
uint16_t sc_card_id;
|
||||
uint16_t sc_card_ref;
|
||||
|
||||
// This array is unfortunately heterogeneous; specifically:
|
||||
// [0] through [5] are hand refs
|
||||
// [6] is the current assist card ref (which may belong to another player)
|
||||
// [7] is the previous assist card ref
|
||||
// [8] through [15] are set refs
|
||||
parray<uint16_t, 0x10> card_refs;
|
||||
|
||||
std::shared_ptr<DeckState> deck_state;
|
||||
parray<uint16_t, 0x10> discard_log_card_refs;
|
||||
parray<uint16_t, 0x10> discard_log_reasons;
|
||||
uint8_t assist_remaining_turns;
|
||||
uint16_t assist_card_set_number;
|
||||
uint16_t set_assist_card_id;
|
||||
bool god_whim_can_use_hidden_cards;
|
||||
ActionChainWithConds unknown_a12;
|
||||
ActionMetadata unknown_a13;
|
||||
uint32_t unknown_a14;
|
||||
uint32_t assist_flags;
|
||||
uint8_t assist_delay_turns;
|
||||
Direction start_facing_direction;
|
||||
std::shared_ptr<HandAndEquipState> hand_and_equip;
|
||||
|
||||
// Like card_refs above, these arrays are also heterogeneous, but the indices
|
||||
// are not the same as for card_refs! THe indices here are:
|
||||
// [0] is the SC card status
|
||||
// [1] through [6] are hand cards
|
||||
// [7] through [14] are set cards
|
||||
// [15] is the assist card
|
||||
std::shared_ptr<parray<CardShortStatus, 0x10>> card_short_statuses;
|
||||
parray<CardShortStatus, 0x10> prev_card_short_statuses;
|
||||
|
||||
// In these arrays, [0] is the SC card and the rest are the set cards.
|
||||
std::shared_ptr<parray<ActionChainWithConds, 9>> set_card_action_chains;
|
||||
std::shared_ptr<parray<ActionMetadata, 9>> set_card_action_metadatas;
|
||||
parray<ActionChainWithConds, 9> prev_set_card_action_chains;
|
||||
parray<ActionMetadata, 9> prev_set_card_action_metadatas;
|
||||
|
||||
uint32_t num_destroyed_fcs;
|
||||
uint8_t unknown_a16;
|
||||
uint8_t unknown_a17;
|
||||
PlayerStats stats;
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Episode3
|
||||
@@ -0,0 +1,829 @@
|
||||
#include "PlayerState.hh"
|
||||
|
||||
#include "Server.hh"
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
bool Condition::operator==(const Condition& other) const {
|
||||
return (this->type == other.type) &&
|
||||
(this->remaining_turns == other.remaining_turns) &&
|
||||
(this->a_arg_value == other.a_arg_value) &&
|
||||
(this->dice_roll_value == other.dice_roll_value) &&
|
||||
(this->flags == other.flags) &&
|
||||
(this->card_definition_effect_index == other.card_definition_effect_index) &&
|
||||
(this->card_ref == other.card_ref) &&
|
||||
(this->value == other.value) &&
|
||||
(this->condition_giver_card_ref == other.condition_giver_card_ref) &&
|
||||
(this->random_percent == other.random_percent) &&
|
||||
(this->value8 == other.value8) &&
|
||||
(this->order == other.order) &&
|
||||
(this->unknown_a8 == other.unknown_a8);
|
||||
}
|
||||
bool Condition::operator!=(const Condition& other) const {
|
||||
return !this->operator==(other);
|
||||
}
|
||||
|
||||
void Condition::clear() {
|
||||
this->type = ConditionType::NONE;
|
||||
this->remaining_turns = 0;
|
||||
this->a_arg_value = 0;
|
||||
this->dice_roll_value = 0;
|
||||
this->flags = 0;
|
||||
this->card_definition_effect_index = 0;
|
||||
this->card_ref = 0xFFFF;
|
||||
this->value = 0;
|
||||
this->condition_giver_card_ref = 0xFFFF;
|
||||
this->random_percent = 0;
|
||||
this->value8 = 0;
|
||||
this->order = 0;
|
||||
this->unknown_a8 = 0;
|
||||
}
|
||||
|
||||
void Condition::clear_FF() {
|
||||
this->type = ConditionType::INVALID_FF;
|
||||
this->remaining_turns = 0xFF;
|
||||
this->a_arg_value = -1;
|
||||
this->dice_roll_value = 0xFF;
|
||||
this->flags = 0xFF;
|
||||
this->card_definition_effect_index = 0xFF;
|
||||
this->card_ref = 0xFFFF;
|
||||
this->value = -1;
|
||||
this->condition_giver_card_ref = 0xFFFF;
|
||||
this->random_percent = 0xFF;
|
||||
this->value8 = -1;
|
||||
this->order = 0xFF;
|
||||
this->unknown_a8 = 0xFF;
|
||||
}
|
||||
|
||||
std::string Condition::str() const {
|
||||
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 "
|
||||
"percent=%hhu value8=%hd order=%hu a8=%hu]",
|
||||
name_for_condition_type(this->type),
|
||||
this->remaining_turns,
|
||||
this->a_arg_value,
|
||||
this->dice_roll_value,
|
||||
this->flags,
|
||||
this->card_definition_effect_index,
|
||||
this->card_ref.load(),
|
||||
this->value.load(),
|
||||
this->condition_giver_card_ref.load(),
|
||||
this->random_percent,
|
||||
this->value8,
|
||||
this->order,
|
||||
this->unknown_a8);
|
||||
}
|
||||
|
||||
|
||||
|
||||
EffectResult::EffectResult() {
|
||||
this->clear();
|
||||
}
|
||||
|
||||
void EffectResult::clear() {
|
||||
this->attacker_card_ref = 0xFFFF;
|
||||
this->target_card_ref = 0xFFFF;
|
||||
this->value = 0;
|
||||
this->current_hp = 0;
|
||||
this->ap = 0;
|
||||
this->tp = 0;
|
||||
this->flags = 0;
|
||||
this->operation = 0;
|
||||
this->condition_index = 0;
|
||||
this->dice_roll_value = 0;
|
||||
}
|
||||
|
||||
std::string EffectResult::str() const {
|
||||
return string_printf(
|
||||
"EffectResult[att_ref=@%04hX, target_ref=@%04hX, 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(),
|
||||
this->value,
|
||||
this->current_hp,
|
||||
this->ap,
|
||||
this->tp,
|
||||
this->flags,
|
||||
this->operation,
|
||||
this->condition_index,
|
||||
this->dice_roll_value);
|
||||
}
|
||||
|
||||
|
||||
|
||||
CardShortStatus::CardShortStatus() {
|
||||
this->clear();
|
||||
}
|
||||
|
||||
bool CardShortStatus::operator==(const CardShortStatus& other) const {
|
||||
return (this->card_ref == other.card_ref) &&
|
||||
(this->current_hp == other.current_hp) &&
|
||||
(this->card_flags == other.card_flags) &&
|
||||
(this->loc == other.loc) &&
|
||||
(this->unused1 == other.unused1) &&
|
||||
(this->max_hp == other.max_hp) &&
|
||||
(this->unused2 == other.unused2);
|
||||
}
|
||||
bool CardShortStatus::operator!=(const CardShortStatus& other) const {
|
||||
return !this->operator==(other);
|
||||
}
|
||||
|
||||
std::string CardShortStatus::str() const {
|
||||
string loc_s = this->loc.str();
|
||||
return string_printf(
|
||||
"CardShortStatus[ref=@%04hX, cur_hp=%hd, flags=%08" PRIX32 ", loc=%s, "
|
||||
"u1=%04hX, max_hp=%hhd, u2=%hhu]",
|
||||
this->card_ref.load(),
|
||||
this->current_hp.load(),
|
||||
this->card_flags.load(),
|
||||
loc_s.c_str(),
|
||||
this->unused1.load(),
|
||||
this->max_hp,
|
||||
this->unused2);
|
||||
}
|
||||
|
||||
void CardShortStatus::clear() {
|
||||
this->card_ref = 0xFFFF;
|
||||
this->current_hp = 0;
|
||||
this->card_flags = 0;
|
||||
this->loc.clear();
|
||||
this->unused1 = 0xFFFF;
|
||||
this->max_hp = 0;
|
||||
this->unused2 = 0;
|
||||
}
|
||||
|
||||
void CardShortStatus::clear_FF() {
|
||||
this->card_ref = 0xFFFF;
|
||||
this->current_hp = 0xFFFF;
|
||||
this->card_flags = 0xFFFFFFFF;
|
||||
this->loc.clear_FF();
|
||||
this->unused1 = 0xFFFF;
|
||||
this->max_hp = -1;
|
||||
this->unused2 = 0xFF;
|
||||
}
|
||||
|
||||
|
||||
|
||||
ActionState::ActionState() {
|
||||
this->clear();
|
||||
}
|
||||
|
||||
void ActionState::clear() {
|
||||
this->client_id = 0xFFFF;
|
||||
this->unused = 0;
|
||||
this->facing_direction = Direction::RIGHT;
|
||||
this->attacker_card_ref = 0xFFFF;
|
||||
this->defense_card_ref = 0xFFFF;
|
||||
this->original_attacker_card_ref = 0xFFFF;
|
||||
this->target_card_refs.clear(0xFFFF);
|
||||
this->action_card_refs.clear(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);
|
||||
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]",
|
||||
this->client_id.load(),
|
||||
this->unused,
|
||||
name_for_direction(this->facing_direction),
|
||||
this->attacker_card_ref.load(),
|
||||
this->defense_card_ref.load(),
|
||||
target_refs_s.c_str(),
|
||||
action_refs_s.c_str(),
|
||||
this->original_attacker_card_ref.load());
|
||||
}
|
||||
|
||||
|
||||
|
||||
ActionChain::ActionChain() {
|
||||
this->clear();
|
||||
}
|
||||
|
||||
bool ActionChain::operator==(const ActionChain& other) const {
|
||||
return (this->effective_ap == other.effective_ap) &&
|
||||
(this->effective_tp == other.effective_tp) &&
|
||||
(this->ap_effect_bonus == other.ap_effect_bonus) &&
|
||||
(this->damage == other.damage) &&
|
||||
(this->acting_card_ref == other.acting_card_ref) &&
|
||||
(this->unknown_card_ref_a3 == other.unknown_card_ref_a3) &&
|
||||
(this->attack_action_card_refs == other.attack_action_card_refs) &&
|
||||
(this->attack_action_card_ref_count == other.attack_action_card_ref_count) &&
|
||||
(this->attack_medium == other.attack_medium) &&
|
||||
(this->target_card_ref_count == other.target_card_ref_count) &&
|
||||
(this->action_subphase == other.action_subphase) &&
|
||||
(this->strike_count == other.strike_count) &&
|
||||
(this->damage_multiplier == other.damage_multiplier) &&
|
||||
(this->attack_number == other.attack_number) &&
|
||||
(this->tp_effect_bonus == other.tp_effect_bonus) &&
|
||||
(this->unused1 == other.unused1) &&
|
||||
(this->unused2 == other.unused2) &&
|
||||
(this->card_ap == other.card_ap) &&
|
||||
(this->card_tp == other.card_tp) &&
|
||||
(this->flags == other.flags) &&
|
||||
(this->target_card_refs == other.target_card_refs);
|
||||
}
|
||||
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);
|
||||
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, "
|
||||
"tp_bonus=%hhd, u1=%hhu, u2=%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(),
|
||||
attack_action_card_refs_s.c_str(),
|
||||
this->attack_action_card_ref_count,
|
||||
name_for_attack_medium(this->attack_medium),
|
||||
this->target_card_ref_count,
|
||||
name_for_action_subphase(this->action_subphase),
|
||||
this->strike_count,
|
||||
this->damage_multiplier,
|
||||
this->attack_number,
|
||||
this->tp_effect_bonus,
|
||||
this->unused1,
|
||||
this->unused2,
|
||||
this->card_ap,
|
||||
this->card_tp,
|
||||
this->flags.load(),
|
||||
target_card_refs_s.c_str());
|
||||
}
|
||||
|
||||
void ActionChain::clear() {
|
||||
this->effective_ap = 0;
|
||||
this->effective_tp = 0;
|
||||
this->ap_effect_bonus = 0;
|
||||
this->damage = 0;
|
||||
this->acting_card_ref = 0xFFFF;
|
||||
this->unknown_card_ref_a3 = 0xFFFF;
|
||||
this->attack_action_card_ref_count = 0;
|
||||
this->attack_medium = AttackMedium::UNKNOWN;
|
||||
this->target_card_ref_count = 0;
|
||||
this->action_subphase = ActionSubphase::INVALID_FF;
|
||||
this->strike_count = 1;
|
||||
this->damage_multiplier = 1;
|
||||
this->attack_number = 0xFF;
|
||||
this->tp_effect_bonus = 0;
|
||||
this->unused1 = 0;
|
||||
this->unused2 = 0;
|
||||
this->card_ap = 0;
|
||||
this->card_tp = 0;
|
||||
this->flags = 0;
|
||||
this->attack_action_card_refs.clear(0xFFFF);
|
||||
this->target_card_refs.clear(0xFFFF);
|
||||
}
|
||||
|
||||
void ActionChain::clear_FF() {
|
||||
this->effective_ap = -1;
|
||||
this->effective_tp = -1;
|
||||
this->ap_effect_bonus = -1;
|
||||
this->damage = -1;
|
||||
this->acting_card_ref = 0xFFFF;
|
||||
this->unknown_card_ref_a3 = 0xFFFF;
|
||||
this->attack_action_card_refs.clear(0xFFFF);
|
||||
this->attack_action_card_ref_count = 0xFF;
|
||||
this->attack_medium = AttackMedium::INVALID_FF;
|
||||
this->target_card_ref_count = 0xFF;
|
||||
this->action_subphase = ActionSubphase::INVALID_FF;
|
||||
this->strike_count = 0xFF;
|
||||
this->damage_multiplier = -1;
|
||||
this->attack_number = 0xFF;
|
||||
this->tp_effect_bonus = -1;
|
||||
this->unused1 = 0xFF;
|
||||
this->unused2 = 0xFF;
|
||||
this->card_ap = -1;
|
||||
this->card_tp = -1;
|
||||
this->flags = 0xFFFFFFFF;
|
||||
this->target_card_refs.clear(0xFFFF);
|
||||
}
|
||||
|
||||
|
||||
|
||||
ActionChainWithConds::ActionChainWithConds() {
|
||||
this->clear();
|
||||
}
|
||||
|
||||
bool ActionChainWithConds::operator==(const ActionChainWithConds& other) const {
|
||||
return (this->chain == other.chain && this->conditions == other.conditions);
|
||||
}
|
||||
bool ActionChainWithConds::operator!=(const ActionChainWithConds& other) const {
|
||||
return !this->operator==(other);
|
||||
}
|
||||
|
||||
std::string ActionChainWithConds::str() const {
|
||||
string ret = "ActionChainWithConds[chain=";
|
||||
ret += this->chain.str();
|
||||
ret += ", conds=[";
|
||||
for (size_t z = 0; z < this->conditions.size(); z++) {
|
||||
if (this->conditions[z].type != ConditionType::NONE) {
|
||||
if (ret.back() != '=') {
|
||||
ret += ", ";
|
||||
}
|
||||
ret += string_printf("%zu:", z);
|
||||
ret += this->conditions[z].str();
|
||||
}
|
||||
}
|
||||
ret += "]]";
|
||||
return ret;
|
||||
}
|
||||
|
||||
void ActionChainWithConds::clear() {
|
||||
this->chain.effective_ap = 0;
|
||||
this->chain.effective_tp = 0;
|
||||
this->chain.ap_effect_bonus = 0;
|
||||
this->chain.damage = 0;
|
||||
this->clear_inner();
|
||||
}
|
||||
|
||||
void ActionChainWithConds::clear_FF() {
|
||||
this->chain.clear_FF();
|
||||
for (size_t z = 0; z < 9; z++) {
|
||||
this->conditions[z].clear_FF();
|
||||
}
|
||||
}
|
||||
|
||||
void ActionChainWithConds::clear_inner() {
|
||||
this->chain.unknown_card_ref_a3 = 0xFFFF;
|
||||
this->chain.acting_card_ref = 0xFFFF;
|
||||
this->chain.attack_medium = AttackMedium::INVALID_FF;
|
||||
this->chain.flags = 0;
|
||||
this->chain.action_subphase = ActionSubphase::INVALID_FF;
|
||||
this->chain.attack_number = 0xFF;
|
||||
this->reset();
|
||||
this->clear_target_card_refs();
|
||||
this->chain.attack_action_card_ref_count = 0;
|
||||
this->chain.attack_action_card_refs.clear(0xFFFF);
|
||||
}
|
||||
|
||||
void ActionChainWithConds::clear_target_card_refs() {
|
||||
this->chain.target_card_ref_count = 0;
|
||||
this->chain.target_card_refs.clear(0xFFFF);
|
||||
}
|
||||
|
||||
void ActionChainWithConds::reset() {
|
||||
this->chain.effective_ap = 0;
|
||||
this->chain.effective_tp = 0;
|
||||
this->chain.ap_effect_bonus = 0;
|
||||
this->chain.tp_effect_bonus = 0;
|
||||
this->chain.unused1 = 0;
|
||||
this->chain.unused2 = 0;
|
||||
this->chain.damage = 0;
|
||||
this->chain.strike_count = 1;
|
||||
this->chain.damage_multiplier = 1;
|
||||
}
|
||||
|
||||
bool ActionChainWithConds::check_flag(uint32_t flags) const {
|
||||
return (this->chain.flags & flags) != 0;
|
||||
}
|
||||
|
||||
void ActionChainWithConds::clear_flags(uint32_t flags) {
|
||||
this->chain.flags &= ~flags;
|
||||
}
|
||||
|
||||
void ActionChainWithConds::set_flags(uint32_t flags) {
|
||||
this->chain.flags |= flags;
|
||||
}
|
||||
|
||||
void ActionChainWithConds::add_attack_action_card_ref(
|
||||
uint16_t card_ref, shared_ptr<Server> server) {
|
||||
if (card_ref != 0xFFFF) {
|
||||
this->chain.attack_action_card_refs[this->chain.attack_action_card_ref_count++] = card_ref;
|
||||
}
|
||||
this->set_flags(8);
|
||||
this->chain.action_subphase = server->get_current_action_subphase();
|
||||
}
|
||||
|
||||
void ActionChainWithConds::add_target_card_ref(uint16_t card_ref) {
|
||||
if (card_ref != 0xFFFF &&
|
||||
this->chain.target_card_ref_count < this->chain.target_card_refs.size()) {
|
||||
this->chain.target_card_refs[this->chain.target_card_ref_count++] = card_ref;
|
||||
}
|
||||
}
|
||||
|
||||
void ActionChainWithConds::compute_attack_medium(shared_ptr<Server> server) {
|
||||
this->chain.attack_medium = AttackMedium::PHYSICAL;
|
||||
for (size_t z = 0; z < this->chain.attack_action_card_ref_count; z++) {
|
||||
uint16_t card_ref = this->chain.attack_action_card_refs[z];
|
||||
if (card_ref == 0xFFFF) {
|
||||
break;
|
||||
}
|
||||
auto ce = server->definition_for_card_ref(card_ref);
|
||||
if (!ce) {
|
||||
continue;
|
||||
}
|
||||
if (card_class_is_tech_like(ce->def.card_class())) {
|
||||
this->chain.attack_medium = AttackMedium::TECH;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool ActionChainWithConds::get_condition_value(
|
||||
ConditionType cond_type,
|
||||
uint16_t card_ref,
|
||||
uint8_t def_effect_index,
|
||||
uint16_t value,
|
||||
uint16_t* out_value) const {
|
||||
bool any_found = false;
|
||||
uint8_t max_order = 10;
|
||||
for (size_t z = 0; z < 9; z++) {
|
||||
auto& cond = this->conditions[z];
|
||||
if (((cond_type == ConditionType::ANY) || (cond.type == cond_type)) &&
|
||||
((def_effect_index == 0xFF) || (cond.card_definition_effect_index == def_effect_index)) &&
|
||||
((card_ref == 0xFFFF) || (cond.card_ref == card_ref)) &&
|
||||
((value == 0xFFFF) || (cond.value == value))) {
|
||||
if (!any_found || (max_order < cond.order)) {
|
||||
if (!out_value) {
|
||||
return true;
|
||||
}
|
||||
*out_value = cond.value;
|
||||
max_order = cond.order;
|
||||
}
|
||||
any_found = true;
|
||||
}
|
||||
}
|
||||
return any_found;
|
||||
}
|
||||
|
||||
void ActionChainWithConds::set_action_subphase_from_card(
|
||||
shared_ptr<const Card> card) {
|
||||
this->chain.action_subphase = card->server()->get_current_action_subphase();
|
||||
}
|
||||
|
||||
bool ActionChainWithConds::unknown_8024DEC4() const {
|
||||
return this->check_flag(4) ? false : (this->chain.target_card_ref_count != 0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
ActionMetadata::ActionMetadata() {
|
||||
this->clear();
|
||||
}
|
||||
|
||||
bool ActionMetadata::operator==(const ActionMetadata& other) const {
|
||||
return (this->card_ref == other.card_ref) &&
|
||||
(this->target_card_ref_count == other.target_card_ref_count) &&
|
||||
(this->defense_card_ref_count == other.defense_card_ref_count) &&
|
||||
(this->action_subphase == other.action_subphase) &&
|
||||
(this->defense_power == other.defense_power) &&
|
||||
(this->defense_bonus == other.defense_bonus) &&
|
||||
(this->attack_bonus == other.attack_bonus) &&
|
||||
(this->flags == other.flags) &&
|
||||
(this->target_card_refs == other.target_card_refs) &&
|
||||
(this->defense_card_refs == other.defense_card_refs) &&
|
||||
(this->original_attacker_card_refs == other.original_attacker_card_refs);
|
||||
}
|
||||
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);
|
||||
return string_printf(
|
||||
"ActionMetadata[ref=@%04hX, 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(),
|
||||
this->target_card_ref_count,
|
||||
this->defense_card_ref_count,
|
||||
name_for_action_subphase(this->action_subphase),
|
||||
this->defense_power,
|
||||
this->defense_bonus,
|
||||
this->attack_bonus,
|
||||
this->flags.load(),
|
||||
target_card_refs_s.c_str(),
|
||||
defense_card_refs_s.c_str(),
|
||||
original_attacker_card_refs_s.c_str());
|
||||
}
|
||||
|
||||
void ActionMetadata::clear() {
|
||||
this->card_ref = 0xFFFF;
|
||||
this->target_card_ref_count = 0;
|
||||
this->defense_card_ref_count = 0;
|
||||
this->action_subphase = ActionSubphase::INVALID_FF;
|
||||
this->defense_power = 0;
|
||||
this->defense_bonus = 0;
|
||||
this->attack_bonus = 0;
|
||||
this->flags = 0;
|
||||
this->target_card_refs.clear(0xFFFF);
|
||||
this->defense_card_refs.clear(0xFFFF);
|
||||
this->original_attacker_card_refs.clear(0xFFFF);
|
||||
}
|
||||
|
||||
void ActionMetadata::clear_FF() {
|
||||
this->card_ref = 0xFFFF;
|
||||
this->target_card_ref_count = 0xFF;
|
||||
this->defense_card_ref_count = 0xFF;
|
||||
this->action_subphase = ActionSubphase::INVALID_FF;
|
||||
this->defense_power = -1;
|
||||
this->defense_bonus = -1;
|
||||
this->attack_bonus = -1;
|
||||
this->flags = 0xFFFFFFFF;
|
||||
this->target_card_refs.clear(0xFFFF);
|
||||
this->defense_card_refs.clear(0xFFFF);
|
||||
this->original_attacker_card_refs.clear(0xFFFF);
|
||||
}
|
||||
|
||||
bool ActionMetadata::check_flag(uint32_t mask) const {
|
||||
return (this->flags & mask) != 0;
|
||||
}
|
||||
|
||||
void ActionMetadata::set_flags(uint32_t flags) {
|
||||
this->flags |= flags;
|
||||
}
|
||||
|
||||
void ActionMetadata::clear_flags(uint32_t flags) {
|
||||
this->flags &= ~flags;
|
||||
}
|
||||
|
||||
void ActionMetadata::clear_defense_and_attacker_card_refs() {
|
||||
this->defense_card_ref_count = 0;
|
||||
this->defense_card_refs.clear(0xFFFF);
|
||||
this->original_attacker_card_refs.clear(0xFFFF);
|
||||
}
|
||||
|
||||
void ActionMetadata::clear_target_card_refs() {
|
||||
this->target_card_ref_count = 0;
|
||||
this->target_card_refs.clear(0xFFFF);
|
||||
}
|
||||
|
||||
void ActionMetadata::add_target_card_ref(uint16_t card_ref) {
|
||||
if (card_ref != 0xFFFF &&
|
||||
this->target_card_ref_count < this->target_card_refs.size()) {
|
||||
this->target_card_refs[this->target_card_ref_count++] = card_ref;
|
||||
}
|
||||
}
|
||||
|
||||
void ActionMetadata::add_defense_card_ref(
|
||||
uint16_t defense_card_ref,
|
||||
shared_ptr<Card> card,
|
||||
uint16_t original_attacker_card_ref) {
|
||||
if ((defense_card_ref != 0xFFFF) && (this->defense_card_ref_count < 8)) {
|
||||
this->defense_card_refs[this->defense_card_ref_count] = defense_card_ref;
|
||||
this->original_attacker_card_refs[this->defense_card_ref_count] = original_attacker_card_ref;
|
||||
this->defense_card_ref_count++;
|
||||
this->action_subphase = card->server()->get_current_action_subphase();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
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);
|
||||
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]]",
|
||||
this->dice_results[0],
|
||||
this->dice_results[1],
|
||||
this->atk_points,
|
||||
this->def_points,
|
||||
this->atk_points2,
|
||||
this->unknown_a1,
|
||||
this->total_set_cards_cost,
|
||||
this->is_cpu_player,
|
||||
this->assist_flags.load(),
|
||||
hand_card_refs_s.c_str(),
|
||||
this->assist_card_ref.load(),
|
||||
set_card_refs_s.c_str(),
|
||||
this->sc_card_ref.load(),
|
||||
hand_card_refs2_s.c_str(),
|
||||
set_card_refs2_s.c_str(),
|
||||
this->assist_card_ref2.load(),
|
||||
this->assist_card_set_number.load(),
|
||||
this->assist_card_id.load(),
|
||||
this->assist_remaining_turns,
|
||||
this->assist_delay_turns,
|
||||
this->atk_bonuses,
|
||||
this->def_bonuses,
|
||||
this->unused2[0],
|
||||
this->unused2[1]);
|
||||
}
|
||||
|
||||
void HandAndEquipState::clear() {
|
||||
this->dice_results.clear(0);
|
||||
this->atk_points = 0;
|
||||
this->def_points = 0;
|
||||
this->atk_points2 = 0;
|
||||
this->unknown_a1 = 0;
|
||||
this->total_set_cards_cost = 0;
|
||||
this->is_cpu_player = 0;
|
||||
this->assist_flags = 0;
|
||||
this->hand_card_refs.clear(0xFFFF);
|
||||
this->assist_card_ref = 0xFFFF;
|
||||
this->set_card_refs.clear(0xFFFF);
|
||||
this->sc_card_ref = 0xFFFF;
|
||||
this->hand_card_refs2.clear(0xFFFF);
|
||||
this->set_card_refs2.clear(0xFFFF);
|
||||
this->assist_card_ref2 = 0xFFFF;
|
||||
this->assist_card_set_number = 0;
|
||||
this->assist_card_id = 0xFFFF;
|
||||
this->assist_remaining_turns = 0;
|
||||
this->assist_delay_turns = 0;
|
||||
this->atk_bonuses = 0;
|
||||
this->def_bonuses = 0;
|
||||
this->unused2.clear(0);
|
||||
}
|
||||
|
||||
void HandAndEquipState::clear_FF() {
|
||||
this->dice_results.clear(0xFF);
|
||||
this->atk_points = 0xFF;
|
||||
this->def_points = 0xFF;
|
||||
this->atk_points2 = 0xFF;
|
||||
this->unknown_a1 = 0xFF;
|
||||
this->total_set_cards_cost = 0xFF;
|
||||
this->is_cpu_player = 0xFF;
|
||||
this->assist_flags = 0xFFFFFFFF;
|
||||
this->hand_card_refs.clear(0xFFFF);
|
||||
this->assist_card_ref = 0xFFFF;
|
||||
this->set_card_refs.clear(0xFFFF);
|
||||
this->sc_card_ref = 0xFFFF;
|
||||
this->hand_card_refs2.clear(0xFFFF);
|
||||
this->set_card_refs2.clear(0xFFFF);
|
||||
this->assist_card_ref2 = 0xFFFF;
|
||||
this->assist_card_set_number = 0xFFFF;
|
||||
this->assist_card_id = 0xFFFF;
|
||||
this->assist_remaining_turns = 0xFF;
|
||||
this->assist_delay_turns = 0xFF;
|
||||
this->atk_bonuses = 0xFF;
|
||||
this->def_bonuses = 0xFF;
|
||||
this->unused2.clear(0xFF);
|
||||
}
|
||||
|
||||
|
||||
|
||||
PlayerStats::PlayerStats() {
|
||||
this->clear();
|
||||
}
|
||||
|
||||
void PlayerStats::clear() {
|
||||
this->damage_given = 0;
|
||||
this->damage_taken = 0;
|
||||
this->num_opponent_cards_destroyed = 0;
|
||||
this->num_owned_cards_destroyed = 0;
|
||||
this->total_move_distance = 0;
|
||||
this->num_cards_set = 0;
|
||||
this->num_item_or_creature_cards_set = 0;
|
||||
this->num_attack_actions_set = 0;
|
||||
this->num_tech_cards_set = 0;
|
||||
this->num_assist_cards_set = 0;
|
||||
this->defense_actions_set_on_self = 0;
|
||||
this->defense_actions_set_on_ally = 0;
|
||||
this->num_cards_drawn = 0;
|
||||
this->max_attack_damage = 0;
|
||||
this->max_attack_combo_size = 0;
|
||||
this->num_attacks_given = 0;
|
||||
this->num_attacks_taken = 0;
|
||||
this->sc_damage_taken = 0;
|
||||
this->action_card_negated_damage = 0;
|
||||
this->unused = 0;
|
||||
}
|
||||
|
||||
float PlayerStats::score(size_t num_rounds) const {
|
||||
// Note: This formula doesn't match the formula on PSO-World, which is:
|
||||
// 35
|
||||
// + (Attack Damage - Damage Taken)
|
||||
// + (Max Card Combo x 3)
|
||||
// - (Story Character Damage x 1.8)
|
||||
// - (Turns x 2.7)
|
||||
// + (Action Card Negated Damage x 0.8)
|
||||
// I don't know where that formula came from, but this one came from the USA
|
||||
// Ep3 PsoV3.dol, so it's presumably correct. Is the PSO-World formula simply
|
||||
// incorrect, or is it from e.g. the Japanese version, which may have a
|
||||
// different rank calculation function?
|
||||
return 38.0f
|
||||
+ 0.8f * this->action_card_negated_damage
|
||||
- 2.3f * num_rounds
|
||||
- 1.8f * this->sc_damage_taken
|
||||
+ 3.0f * this->max_attack_combo_size
|
||||
+ (this->damage_given - this->damage_taken);
|
||||
}
|
||||
|
||||
uint8_t PlayerStats::rank(size_t num_rounds) const {
|
||||
return this->rank_for_score(this->score(num_rounds));
|
||||
}
|
||||
|
||||
const char* PlayerStats::rank_name(size_t num_rounds) const {
|
||||
return this->name_for_rank(this->rank_for_score(this->score(num_rounds)));
|
||||
}
|
||||
|
||||
constexpr size_t RANK_THRESHOLD_COUNT = 9;
|
||||
static const float RANK_THRESHOLDS[RANK_THRESHOLD_COUNT] = {
|
||||
15.0f, 25.0f, 30.0f, 40.0f, 50.0f, 60.0f, 65.0f, 75.0f, 85.0f};
|
||||
static const char* RANK_NAMES[RANK_THRESHOLD_COUNT + 1] = {
|
||||
"E", "D", "D+", "C", "C+", "B", "B+", "A", "A+", "S"};
|
||||
|
||||
uint8_t PlayerStats::rank_for_score(float score) {
|
||||
size_t rank = 0;
|
||||
while (rank < RANK_THRESHOLD_COUNT && RANK_THRESHOLDS[rank] <= score) {
|
||||
rank++;
|
||||
}
|
||||
return rank;
|
||||
}
|
||||
|
||||
const char* PlayerStats::name_for_rank(uint8_t rank) {
|
||||
if (rank >= RANK_THRESHOLD_COUNT + 1) {
|
||||
throw invalid_argument("invalid rank");
|
||||
}
|
||||
return RANK_NAMES[rank];
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
bool is_card_within_range(
|
||||
const parray<uint8_t, 9 * 9>& range,
|
||||
const Location& anchor_loc,
|
||||
const CardShortStatus& ss) {
|
||||
if (ss.card_ref == 0xFFFF) {
|
||||
return false;
|
||||
}
|
||||
if (range[0] == 2) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ((ss.loc.x < anchor_loc.x - 4) || (ss.loc.x > anchor_loc.x + 4)) {
|
||||
return false;
|
||||
}
|
||||
if ((ss.loc.y < anchor_loc.y - 4) || (ss.loc.y > anchor_loc.y + 4)) {
|
||||
return false;
|
||||
}
|
||||
return (range[(ss.loc.x - anchor_loc.x) + ((ss.loc.y - anchor_loc.y) + 4) * 9 + 4] != 0);
|
||||
}
|
||||
|
||||
vector<uint16_t> get_card_refs_within_range(
|
||||
const parray<uint8_t, 9 * 9>& range,
|
||||
const Location& loc,
|
||||
const parray<CardShortStatus, 0x10>& short_statuses) {
|
||||
vector<uint16_t> ret;
|
||||
if (is_card_within_range(range, loc, short_statuses[0])) {
|
||||
ret.emplace_back(short_statuses[0].card_ref);
|
||||
}
|
||||
for (size_t card_index = 7; card_index < 15; card_index++) {
|
||||
const auto& ss = short_statuses[card_index];
|
||||
if (is_card_within_range(range, loc, ss)) {
|
||||
ret.emplace_back(ss.card_ref);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace Episode3
|
||||
@@ -0,0 +1,281 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "../Text.hh"
|
||||
#include "DataIndex.hh"
|
||||
|
||||
namespace Episode3 {
|
||||
|
||||
|
||||
|
||||
class ServerBase;
|
||||
class Server;
|
||||
class Card;
|
||||
|
||||
struct Condition {
|
||||
ConditionType type;
|
||||
uint8_t remaining_turns;
|
||||
int8_t a_arg_value;
|
||||
uint8_t dice_roll_value;
|
||||
uint8_t flags;
|
||||
uint8_t card_definition_effect_index;
|
||||
le_uint16_t card_ref;
|
||||
le_int16_t value;
|
||||
le_uint16_t condition_giver_card_ref;
|
||||
uint8_t random_percent;
|
||||
int8_t value8;
|
||||
uint8_t order;
|
||||
uint8_t unknown_a8;
|
||||
|
||||
Condition();
|
||||
bool operator==(const Condition& other) const;
|
||||
bool operator!=(const Condition& other) const;
|
||||
|
||||
void clear();
|
||||
void clear_FF();
|
||||
|
||||
std::string str() const;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct EffectResult {
|
||||
le_uint16_t attacker_card_ref;
|
||||
le_uint16_t target_card_ref;
|
||||
int8_t value;
|
||||
int8_t current_hp;
|
||||
int8_t ap;
|
||||
int8_t tp;
|
||||
uint8_t flags;
|
||||
int8_t operation; // May be a negative condition number
|
||||
uint8_t condition_index;
|
||||
uint8_t dice_roll_value;
|
||||
|
||||
EffectResult();
|
||||
bool operator==(const EffectResult& other) const;
|
||||
bool operator!=(const EffectResult& other) const;
|
||||
|
||||
std::string str() const;
|
||||
|
||||
void clear();
|
||||
} __attribute__((packed));
|
||||
|
||||
struct CardShortStatus {
|
||||
le_uint16_t card_ref;
|
||||
le_uint16_t current_hp;
|
||||
le_uint32_t card_flags;
|
||||
Location loc;
|
||||
le_uint16_t unused1;
|
||||
int8_t max_hp;
|
||||
uint8_t unused2;
|
||||
|
||||
CardShortStatus();
|
||||
bool operator==(const CardShortStatus& other) const;
|
||||
bool operator!=(const CardShortStatus& other) const;
|
||||
|
||||
void clear();
|
||||
void clear_FF();
|
||||
|
||||
std::string str() const;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct ActionState {
|
||||
le_uint16_t client_id;
|
||||
uint8_t unused;
|
||||
Direction facing_direction;
|
||||
le_uint16_t attacker_card_ref;
|
||||
le_uint16_t defense_card_ref;
|
||||
parray<le_uint16_t, 4 * 9> target_card_refs;
|
||||
parray<le_uint16_t, 9> action_card_refs;
|
||||
le_uint16_t original_attacker_card_ref;
|
||||
|
||||
ActionState();
|
||||
bool operator==(const ActionState& other) const;
|
||||
bool operator!=(const ActionState& other) const;
|
||||
|
||||
void clear();
|
||||
|
||||
std::string str() const;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct ActionChain {
|
||||
int8_t effective_ap;
|
||||
int8_t effective_tp;
|
||||
int8_t ap_effect_bonus;
|
||||
int8_t damage;
|
||||
le_uint16_t acting_card_ref;
|
||||
le_uint16_t unknown_card_ref_a3;
|
||||
parray<le_uint16_t, 8> attack_action_card_refs;
|
||||
uint8_t attack_action_card_ref_count;
|
||||
AttackMedium attack_medium;
|
||||
uint8_t target_card_ref_count;
|
||||
ActionSubphase action_subphase;
|
||||
uint8_t strike_count;
|
||||
int8_t damage_multiplier;
|
||||
uint8_t attack_number;
|
||||
int8_t tp_effect_bonus;
|
||||
uint8_t unused1;
|
||||
uint8_t unused2;
|
||||
int8_t card_ap;
|
||||
int8_t card_tp;
|
||||
le_uint32_t flags;
|
||||
parray<le_uint16_t, 4 * 9> target_card_refs;
|
||||
|
||||
ActionChain();
|
||||
bool operator==(const ActionChain& other) const;
|
||||
bool operator!=(const ActionChain& other) const;
|
||||
|
||||
void clear();
|
||||
void clear_FF();
|
||||
|
||||
std::string str() const;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct ActionChainWithConds {
|
||||
ActionChain chain;
|
||||
parray<Condition, 9> conditions;
|
||||
|
||||
ActionChainWithConds();
|
||||
bool operator==(const ActionChainWithConds& other) const;
|
||||
bool operator!=(const ActionChainWithConds& other) const;
|
||||
|
||||
void clear();
|
||||
void clear_FF();
|
||||
void clear_inner();
|
||||
void clear_target_card_refs();
|
||||
void reset();
|
||||
|
||||
bool check_flag(uint32_t flags) const;
|
||||
void clear_flags(uint32_t flags);
|
||||
void set_flags(uint32_t flags);
|
||||
|
||||
void add_attack_action_card_ref(uint16_t card_ref, std::shared_ptr<Server> server);
|
||||
void add_target_card_ref(uint16_t card_ref);
|
||||
|
||||
void compute_attack_medium(std::shared_ptr<Server> server);
|
||||
bool get_condition_value(
|
||||
ConditionType cond_type,
|
||||
uint16_t card_ref,
|
||||
uint8_t def_effect_index,
|
||||
uint16_t value,
|
||||
uint16_t* out_value) const;
|
||||
|
||||
void set_action_subphase_from_card(std::shared_ptr<const Card> card);
|
||||
bool unknown_8024DEC4() const;
|
||||
|
||||
std::string str() const;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct ActionMetadata {
|
||||
le_uint16_t card_ref;
|
||||
uint8_t target_card_ref_count;
|
||||
uint8_t defense_card_ref_count;
|
||||
ActionSubphase action_subphase;
|
||||
int8_t defense_power;
|
||||
int8_t defense_bonus;
|
||||
int8_t attack_bonus;
|
||||
le_uint32_t flags;
|
||||
parray<le_uint16_t, 4 * 9> target_card_refs;
|
||||
parray<le_uint16_t, 8> defense_card_refs;
|
||||
parray<le_uint16_t, 8> original_attacker_card_refs;
|
||||
|
||||
ActionMetadata();
|
||||
bool operator==(const ActionMetadata& other) const;
|
||||
bool operator!=(const ActionMetadata& other) const;
|
||||
|
||||
std::string str() const;
|
||||
|
||||
void clear();
|
||||
void clear_FF();
|
||||
|
||||
bool check_flag(uint32_t mask) const;
|
||||
void set_flags(uint32_t flags);
|
||||
void clear_flags(uint32_t flags);
|
||||
|
||||
void clear_defense_and_attacker_card_refs();
|
||||
void clear_target_card_refs();
|
||||
void add_target_card_ref(uint16_t card_ref);
|
||||
void add_defense_card_ref(
|
||||
uint16_t defense_card_ref,
|
||||
std::shared_ptr<Card> card,
|
||||
uint16_t original_attacker_card_ref);
|
||||
} __attribute__((packed));
|
||||
|
||||
struct HandAndEquipState {
|
||||
parray<uint8_t, 2> dice_results;
|
||||
uint8_t atk_points;
|
||||
uint8_t def_points;
|
||||
uint8_t atk_points2; // TODO: rename this to something more appropriate
|
||||
uint8_t unknown_a1;
|
||||
uint8_t total_set_cards_cost;
|
||||
uint8_t is_cpu_player;
|
||||
le_uint32_t assist_flags;
|
||||
parray<le_uint16_t, 6> hand_card_refs;
|
||||
le_uint16_t assist_card_ref;
|
||||
parray<le_uint16_t, 8> set_card_refs;
|
||||
le_uint16_t sc_card_ref;
|
||||
parray<le_uint16_t, 6> hand_card_refs2;
|
||||
parray<le_uint16_t, 8> set_card_refs2;
|
||||
le_uint16_t assist_card_ref2;
|
||||
le_uint16_t assist_card_set_number;
|
||||
le_uint16_t assist_card_id;
|
||||
uint8_t assist_remaining_turns;
|
||||
uint8_t assist_delay_turns;
|
||||
uint8_t atk_bonuses;
|
||||
uint8_t def_bonuses;
|
||||
parray<uint8_t, 2> unused2;
|
||||
|
||||
HandAndEquipState();
|
||||
bool operator==(const HandAndEquipState& other) const;
|
||||
bool operator!=(const HandAndEquipState& other) const;
|
||||
|
||||
void clear();
|
||||
void clear_FF();
|
||||
|
||||
std::string str() const;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct PlayerStats {
|
||||
le_uint16_t damage_given;
|
||||
le_uint16_t damage_taken;
|
||||
le_uint16_t num_opponent_cards_destroyed;
|
||||
le_uint16_t num_owned_cards_destroyed;
|
||||
le_uint16_t total_move_distance;
|
||||
le_uint16_t num_cards_set;
|
||||
le_uint16_t num_item_or_creature_cards_set;
|
||||
le_uint16_t num_attack_actions_set;
|
||||
le_uint16_t num_tech_cards_set;
|
||||
le_uint16_t num_assist_cards_set;
|
||||
le_uint16_t defense_actions_set_on_self;
|
||||
le_uint16_t defense_actions_set_on_ally;
|
||||
le_uint16_t num_cards_drawn;
|
||||
le_uint16_t max_attack_damage;
|
||||
le_uint16_t max_attack_combo_size;
|
||||
le_uint16_t num_attacks_given;
|
||||
le_uint16_t num_attacks_taken;
|
||||
le_uint16_t sc_damage_taken;
|
||||
le_uint16_t action_card_negated_damage;
|
||||
le_uint16_t unused;
|
||||
|
||||
PlayerStats();
|
||||
void clear();
|
||||
|
||||
float score(size_t num_rounds) const;
|
||||
uint8_t rank(size_t num_rounds) const;
|
||||
const char* rank_name(size_t num_rounds) const;
|
||||
|
||||
static uint8_t rank_for_score(float score);
|
||||
static const char* name_for_rank(uint8_t rank);
|
||||
} __attribute__((packed));
|
||||
|
||||
|
||||
|
||||
std::vector<uint16_t> get_card_refs_within_range(
|
||||
const parray<uint8_t, 9 * 9>& range,
|
||||
const Location& loc,
|
||||
const parray<CardShortStatus, 0x10>& short_statuses);
|
||||
|
||||
|
||||
|
||||
} // namespace Episode3
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,232 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "DataIndex.hh"
|
||||
#include "PlayerState.hh"
|
||||
#include "DeckState.hh"
|
||||
#include "AssistServer.hh"
|
||||
|
||||
namespace Episode3 {
|
||||
|
||||
|
||||
|
||||
class Server;
|
||||
|
||||
void compute_effective_range(
|
||||
parray<uint8_t, 9 * 9>& ret,
|
||||
std::shared_ptr<const DataIndex> data_index,
|
||||
uint16_t card_id,
|
||||
const Location& loc,
|
||||
std::shared_ptr<const MapAndRulesState> map_and_rules);
|
||||
|
||||
bool card_linkage_is_valid(
|
||||
std::shared_ptr<const DataIndex::CardEntry> right_def,
|
||||
std::shared_ptr<const DataIndex::CardEntry> left_def,
|
||||
std::shared_ptr<const DataIndex::CardEntry> sc_def,
|
||||
bool has_permission_effect);
|
||||
|
||||
class RulerServer {
|
||||
public:
|
||||
struct MovePath {
|
||||
int32_t length;
|
||||
uint32_t remaining_distance;
|
||||
Location end_loc;
|
||||
parray<Location, 11> step_locs;
|
||||
uint32_t num_occupied_tiles;
|
||||
uint32_t cost;
|
||||
|
||||
MovePath();
|
||||
void add_step(const Location& loc);
|
||||
uint32_t get_cost() const;
|
||||
uint32_t get_length_plus1() const;
|
||||
void reset_totals();
|
||||
bool is_valid() const;
|
||||
};
|
||||
|
||||
explicit RulerServer(std::shared_ptr<Server> server);
|
||||
std::shared_ptr<Server> server();
|
||||
std::shared_ptr<const Server> server() const;
|
||||
|
||||
ActionChainWithConds* action_chain_with_conds_for_card_ref(
|
||||
uint16_t card_ref);
|
||||
const ActionChainWithConds* action_chain_with_conds_for_card_ref(
|
||||
uint16_t card_ref) const;
|
||||
bool any_attack_action_card_is_support_tech_or_support_pb(
|
||||
const ActionState& pa) const;
|
||||
bool card_has_pierce_or_rampage(
|
||||
uint8_t client_id,
|
||||
ConditionType cond_type,
|
||||
bool* out_has_rampage,
|
||||
uint16_t attacker_card_ref,
|
||||
uint16_t action_card_ref,
|
||||
uint8_t def_effect_index,
|
||||
AttackMedium attack_medium) const;
|
||||
bool attack_action_has_rampage_and_not_pierce(
|
||||
const ActionState& pa, uint16_t card_ref) const;
|
||||
bool attack_action_has_pierce_and_not_rampage(
|
||||
const ActionState& pa, uint8_t client_id);
|
||||
bool card_exists_by_status(const CardShortStatus& stat) const;
|
||||
bool card_has_mighty_knuckle(uint32_t card_ref) const;
|
||||
uint16_t card_id_for_card_ref(uint16_t card_ref) const;
|
||||
static bool card_id_is_boss_sc(uint16_t card_id);
|
||||
static bool card_id_is_support_tech_or_support_pb(uint16_t card_id);
|
||||
bool card_ref_can_attack(uint16_t card_ref);
|
||||
bool card_ref_can_move(
|
||||
uint8_t client_id, uint16_t card_ref, bool ignore_atk_points) const;
|
||||
bool card_ref_has_class_usability_condition(
|
||||
uint16_t card_ref) const;
|
||||
bool card_ref_has_free_maneuver(uint16_t card_ref) const;
|
||||
bool card_ref_is_aerial(uint16_t card_ref) const;
|
||||
bool card_ref_is_aerial_or_has_free_maneuver(
|
||||
uint16_t card_ref) const;
|
||||
bool card_ref_is_boss_sc(uint32_t card_ref) const;
|
||||
bool card_ref_or_any_set_card_has_condition_46(
|
||||
uint16_t card_ref) const;
|
||||
bool card_ref_or_sc_has_fixed_range(uint16_t card_ref) const;
|
||||
bool check_move_path_and_get_cost(
|
||||
uint8_t client_id,
|
||||
uint16_t card_ref,
|
||||
parray<uint8_t, 0x100>* visited_map,
|
||||
MovePath* out_path,
|
||||
uint32_t* out_cost) const;
|
||||
bool check_pierce_and_rampage(
|
||||
uint16_t card_ref,
|
||||
ConditionType cond_type,
|
||||
bool* out_has_pierce,
|
||||
uint16_t attacker_card_ref,
|
||||
uint16_t action_card_ref,
|
||||
uint8_t def_effect_index,
|
||||
AttackMedium attack_medium) const;
|
||||
bool check_usability_or_apply_condition_for_card_refs(
|
||||
uint16_t card_ref1,
|
||||
uint16_t card_ref2,
|
||||
uint16_t card_ref3,
|
||||
uint8_t def_effect_index,
|
||||
AttackMedium attack_medium) const;
|
||||
bool check_usability_or_condition_apply(
|
||||
uint8_t client_id1,
|
||||
uint16_t card_id1,
|
||||
uint8_t client_id2,
|
||||
uint16_t card_id2,
|
||||
uint16_t card_id3,
|
||||
uint8_t def_effect_index,
|
||||
bool is_condition_check,
|
||||
AttackMedium attack_medium) const;
|
||||
uint16_t compute_attack_or_defense_costs(
|
||||
const ActionState& pa,
|
||||
bool allow_mighty_knuckle,
|
||||
uint8_t* out_ally_cost) const;
|
||||
bool compute_effective_range_and_target_mode_for_attack(
|
||||
const ActionState& pa,
|
||||
uint16_t* out_effective_card_id,
|
||||
TargetMode* out_effective_target_mode,
|
||||
uint16_t* out_orig_card_ref) const;
|
||||
size_t count_rampage_targets_for_attack(
|
||||
const ActionState& pa, uint8_t client_id) const;
|
||||
bool defense_card_can_apply_to_attack(
|
||||
uint16_t defense_card_ref,
|
||||
uint16_t attacker_card_ref,
|
||||
uint16_t attacker_sc_card_ref) const;
|
||||
bool defense_card_matches_any_attack_card_top_color(
|
||||
const ActionState& pa) const;
|
||||
std::shared_ptr<const DataIndex::CardEntry> definition_for_card_ref(uint16_t card_ref) const;
|
||||
int32_t error_code_for_client_setting_card(
|
||||
uint8_t client_id,
|
||||
uint16_t card_ref,
|
||||
const Location* loc,
|
||||
uint8_t assist_target_client_id) const;
|
||||
bool find_condition_on_card_ref(
|
||||
uint16_t card_ref,
|
||||
ConditionType cond_type,
|
||||
Condition* out_se = nullptr,
|
||||
size_t* out_value_sum = nullptr,
|
||||
bool find_first_instead_of_max = false) const;
|
||||
bool flood_fill_move_path(
|
||||
const ActionChainWithConds& chain,
|
||||
int8_t x,
|
||||
int8_t y,
|
||||
Direction direction,
|
||||
uint8_t max_atk_points,
|
||||
int16_t max_distance,
|
||||
bool is_free_maneuver_or_aerial,
|
||||
bool is_aerial,
|
||||
parray<uint8_t, 0x100>* visited_map,
|
||||
MovePath* path,
|
||||
size_t num_occupied_tiles,
|
||||
size_t num_vacant_tiles) const;
|
||||
uint16_t get_ally_sc_card_ref(uint16_t card_ref) const;
|
||||
std::shared_ptr<const DataIndex::CardEntry> definition_for_card_id(
|
||||
uint32_t card_id) const;
|
||||
uint32_t get_card_id_with_effective_range(
|
||||
uint16_t card_ref, uint16_t card_id_override, TargetMode* out_target_mode) const;
|
||||
uint8_t get_card_ref_max_hp(uint16_t card_ref) const;
|
||||
bool get_creature_summon_area(
|
||||
uint8_t client_id, Location* out_loc, uint8_t* out_region_size) const;
|
||||
std::shared_ptr<HandAndEquipState> get_hand_and_equip_state_for_client_id(
|
||||
uint8_t client_id);
|
||||
std::shared_ptr<const HandAndEquipState> get_hand_and_equip_state_for_client_id(
|
||||
uint8_t client_id) const;
|
||||
bool get_move_path_length_and_cost(
|
||||
uint32_t client_id,
|
||||
uint32_t card_ref,
|
||||
const Location& loc,
|
||||
uint32_t* out_length,
|
||||
uint32_t* out_cost) const;
|
||||
ssize_t get_path_cost(
|
||||
const ActionChainWithConds& chain,
|
||||
ssize_t path_length,
|
||||
ssize_t cost_penalty) const;
|
||||
ActionType get_pending_action_type(const ActionState& pa) const;
|
||||
bool is_attack_valid(const ActionState& pa);
|
||||
bool is_attack_or_defense_valid(const ActionState& pa);
|
||||
bool is_card_ref_in_hand(uint16_t card_ref) const;
|
||||
bool is_defense_valid(const ActionState& pa);
|
||||
void link_objects(
|
||||
std::shared_ptr<MapAndRulesState> map_and_rules,
|
||||
std::shared_ptr<StateFlags> state_flags,
|
||||
std::shared_ptr<AssistServer> assist_server);
|
||||
size_t max_move_distance_for_card_ref(uint32_t card_ref) const;
|
||||
static void offsets_for_direction(
|
||||
const Location& loc, int32_t* out_x_offset, int32_t* out_y_offset);
|
||||
void register_player(
|
||||
uint8_t client_id,
|
||||
std::shared_ptr<HandAndEquipState> hes,
|
||||
std::shared_ptr<parray<CardShortStatus, 0x10>> short_statuses,
|
||||
std::shared_ptr<DeckEntry> deck_entry,
|
||||
std::shared_ptr<parray<ActionChainWithConds, 9>> set_card_action_chains,
|
||||
std::shared_ptr<parray<ActionMetadata, 9>> set_card_action_metadatas);
|
||||
void replace_D1_D2_rarity_cards_with_Attack(
|
||||
parray<le_uint16_t, 0x1F>& card_ids) const;
|
||||
AttackMedium get_attack_medium(const ActionState& pa) const;
|
||||
void set_client_team_id(uint8_t client_id, uint8_t team_id);
|
||||
int32_t set_cost_for_card(uint8_t client_id, uint16_t card_ref) const;
|
||||
const CardShortStatus* short_status_for_card_ref(uint16_t card_ref) const;
|
||||
bool should_allow_attacks_on_current_turn() const;
|
||||
int32_t verify_deck(
|
||||
const parray<le_uint16_t, 0x1F>& card_ids,
|
||||
const parray<uint8_t, 0x2F0>* owned_card_counts = nullptr) const;
|
||||
|
||||
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];
|
||||
std::shared_ptr<MapAndRulesState> map_and_rules;
|
||||
std::shared_ptr<StateFlags> state_flags;
|
||||
std::shared_ptr<AssistServer> assist_server;
|
||||
parray<uint8_t, 4> team_id_for_client_id;
|
||||
int32_t error_code1;
|
||||
int32_t error_code2;
|
||||
int32_t error_code3;
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Episode3
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,306 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "../Text.hh"
|
||||
#include "../CommandFormats.hh"
|
||||
#include "../Channel.hh"
|
||||
#include "AssistServer.hh"
|
||||
#include "CardSpecial.hh"
|
||||
#include "MapState.hh"
|
||||
#include "PlayerState.hh"
|
||||
#include "RulerServer.hh"
|
||||
|
||||
struct Lobby;
|
||||
|
||||
namespace Episode3 {
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* This implementation of Episode 3 battles (contained in all files in the
|
||||
* src/Episode3 directory, except for DataIndex.hh/cc) is derived from Sega's
|
||||
* original server implementation, reverse-engineered from the Episode 3 client
|
||||
* executable. The control flow, function breakdown, and structure definitions
|
||||
* in these files map very closely to how their server implementation was
|
||||
* written; notable differences (due to necessary environment differences or bug
|
||||
* fixes) are described in the comments therein.
|
||||
*
|
||||
* Some debugging functions have been added which are not part of the original
|
||||
* implementation. Notably, this applies to functions like debug message senders
|
||||
* and loggers and all str() functions.
|
||||
*
|
||||
* There are likely undiscovered bugs in this code, some originally written by
|
||||
* Sega, but more written by me as I manually transcribed and updated this code.
|
||||
*/
|
||||
|
||||
// Class ownership levels (classes may only contain weak_ptrs, not shared_ptrs,
|
||||
// to classes at the same or higher level):
|
||||
// - ServerBase
|
||||
// - - Server
|
||||
// - - - RulerServer
|
||||
// - - - - AssistServer
|
||||
// - - - - CardSpecial
|
||||
// - - - - - StateFlags
|
||||
// - - - - - DeckEntry
|
||||
// - - - - - PlayerState
|
||||
// - - - - - - Card
|
||||
// - - - - - - - CardShortStatus
|
||||
// - - - - - - - DeckState
|
||||
// - - - - - - - HandAndEquipState
|
||||
// - - - - - - - MapAndRulesState / OverlayState
|
||||
// - - - - - - - - Everything within DataIndex
|
||||
|
||||
class Server;
|
||||
|
||||
|
||||
|
||||
class ServerBase : public std::enable_shared_from_this<ServerBase> {
|
||||
public:
|
||||
ServerBase(
|
||||
std::shared_ptr<Lobby> lobby,
|
||||
std::shared_ptr<const DataIndex> data_index,
|
||||
uint32_t random_seed,
|
||||
std::shared_ptr<const DataIndex::MapEntry> map_if_tournament);
|
||||
void init();
|
||||
void reset();
|
||||
void recreate_server();
|
||||
|
||||
struct PresenceEntry {
|
||||
uint8_t player_present;
|
||||
uint8_t deck_valid;
|
||||
uint8_t is_cpu_player;
|
||||
PresenceEntry();
|
||||
void clear();
|
||||
} __attribute__((packed));
|
||||
|
||||
std::weak_ptr<Lobby> lobby;
|
||||
std::shared_ptr<const DataIndex> data_index;
|
||||
PrefixedLogger log;
|
||||
uint32_t random_seed;
|
||||
bool is_tournament;
|
||||
std::shared_ptr<const DataIndex::MapEntry> last_chosen_map;
|
||||
|
||||
std::shared_ptr<MapAndRulesState> map_and_rules1;
|
||||
std::shared_ptr<MapAndRulesState> map_and_rules2;
|
||||
std::shared_ptr<DeckEntry> deck_entries[4];
|
||||
std::shared_ptr<Server> server;
|
||||
parray<PresenceEntry, 4> presence_entries;
|
||||
uint8_t num_clients_present;
|
||||
parray<NameEntry, 4> name_entries;
|
||||
parray<uint8_t, 4> name_entries_valid;
|
||||
OverlayState overlay_state;
|
||||
parray<parray<uint8_t, 0x2F0>, 4> client_card_counts;
|
||||
};
|
||||
|
||||
class Server : public std::enable_shared_from_this<Server> {
|
||||
public:
|
||||
explicit Server(std::shared_ptr<ServerBase> base);
|
||||
void init();
|
||||
std::shared_ptr<ServerBase> base();
|
||||
std::shared_ptr<const ServerBase> base() const;
|
||||
|
||||
int8_t get_winner_team_id() const;
|
||||
|
||||
template <typename T>
|
||||
void send(const T& cmd) const {
|
||||
if (cmd.header.size != sizeof(cmd) / 4) {
|
||||
throw std::logic_error("outbound command size field is incorrect");
|
||||
}
|
||||
if (cmd.header.subsubcommand == 0x06) {
|
||||
this->num_6xB4x06_commands_sent++;
|
||||
this->prev_num_6xB4x06_commands_sent = this->num_6xB4x06_commands_sent;
|
||||
if (this->num_6xB4x06_commands_sent > 0x100) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
this->send(&cmd, cmd.header.size * 4);
|
||||
}
|
||||
void send(const void* data, size_t size) const;
|
||||
|
||||
void send_commands_for_joining_spectator(Channel& ch) const;
|
||||
|
||||
__attribute__((format(printf, 2, 3)))
|
||||
void log_debug(const char* fmt, ...) const;
|
||||
|
||||
__attribute__((format(printf, 2, 3)))
|
||||
void send_debug_message_printf(const char* fmt, ...) const;
|
||||
__attribute__((format(printf, 2, 3)))
|
||||
void send_info_message_printf(const char* fmt, ...) const;
|
||||
void send_debug_command_received_message(
|
||||
uint8_t client_id, uint8_t subsubcommand, const char* description) const;
|
||||
void send_debug_command_received_message(
|
||||
uint8_t subsubcommand, const char* description) const;
|
||||
void send_debug_message_if_error_code_nonzero(
|
||||
uint8_t client_id, int32_t error_code) const;
|
||||
|
||||
void send_6xB4x46() const;
|
||||
|
||||
void add_team_exp(uint8_t team_id, int32_t exp);
|
||||
bool advance_battle_phase();
|
||||
void action_phase_after();
|
||||
void draw_phase_before();
|
||||
std::shared_ptr<const DataIndex::CardEntry> definition_for_card_ref(uint16_t card_ref) const;
|
||||
std::shared_ptr<Card> card_for_set_card_ref(uint16_t card_ref);
|
||||
std::shared_ptr<const Card> card_for_set_card_ref(uint16_t card_ref) const;
|
||||
uint16_t card_id_for_card_ref(uint16_t card_ref) const;
|
||||
bool card_ref_is_empty_or_has_valid_card_id(uint16_t card_ref) const;
|
||||
bool check_for_battle_end();
|
||||
void check_for_destroyed_cards_and_send_6xB4x05_6xB4x02();
|
||||
bool check_presence_entry(uint8_t client_id) const;
|
||||
void clear_player_flags_after_dice_phase();
|
||||
void compute_all_map_occupied_bits();
|
||||
void compute_team_dice_boost(uint8_t team_id);
|
||||
void copy_player_states_to_prev_states();
|
||||
std::shared_ptr<const DataIndex::CardEntry> definition_for_card_id(uint16_t card_id) const;
|
||||
void destroy_cards_with_zero_hp();
|
||||
void determine_first_team_turn();
|
||||
void dice_phase_after();
|
||||
void set_phase_before();
|
||||
void draw_phase_after();
|
||||
void dice_phase_before();
|
||||
void end_attack_list_for_client(uint8_t client_id);
|
||||
void end_action_phase();
|
||||
bool enqueue_attack_or_defense(uint8_t client_id, ActionState* pa);
|
||||
BattlePhase get_battle_phase() const;
|
||||
ActionSubphase get_current_action_subphase() const;
|
||||
uint8_t get_current_team_turn() const;
|
||||
std::shared_ptr<PlayerState> get_player_state(uint8_t client_id);
|
||||
std::shared_ptr<const PlayerState> get_player_state(uint8_t client_id) const;
|
||||
uint32_t get_random(uint32_t max);
|
||||
float get_random_float_0_1();
|
||||
uint32_t get_round_num() const;
|
||||
SetupPhase get_setup_phase() const;
|
||||
uint32_t get_should_copy_prev_states_to_current_states() const;
|
||||
bool is_registration_complete() const;
|
||||
void move_phase_after();
|
||||
void action_phase_before();
|
||||
void send_6xB4x1C_names_update();
|
||||
int8_t send_6xB4x33_remove_ally_atk_if_needed(const ActionState& pa);
|
||||
void send_all_state_updates();
|
||||
void send_set_card_updates_and_6xB4x04_if_needed();
|
||||
void set_battle_ended();
|
||||
void set_battle_started();
|
||||
void set_client_id_ready_to_advance_phase(uint8_t client_id);
|
||||
void set_phase_after();
|
||||
void move_phase_before();
|
||||
void set_player_deck_valid(uint8_t client_id);
|
||||
void setup_and_start_battle();
|
||||
void update_battle_state_flags_and_send_6xB4x03_if_needed(
|
||||
bool always_send = false);
|
||||
bool update_registration_phase();
|
||||
void on_server_data_input(const std::string& data);
|
||||
void handle_6xB3x0B_mulligan_hand(const std::string& data);
|
||||
void handle_6xB3x0C_end_mulligan_phase(const std::string& data);
|
||||
void handle_6xB3x0D_end_non_action_phase(const std::string& data);
|
||||
void handle_6xB3x0E_discard_card_from_hand(const std::string& data);
|
||||
void handle_6xB3x0F_set_card_from_hand(const std::string& data);
|
||||
void handle_6xB3x10_move_fc_to_location(const std::string& data);
|
||||
void handle_6xB3x11_enqueue_attack_or_defense(const std::string& data);
|
||||
void handle_6xB3x12_end_attack_list(const std::string& data);
|
||||
void handle_6xB3x13_update_map_during_setup(const std::string& data);
|
||||
void handle_6xB3x14_update_deck_during_setup(const std::string& data);
|
||||
void handle_6xB3x15_unused_hard_reset_server_state(const std::string& data);
|
||||
void handle_6xB3x1B_update_player_name(const std::string& data);
|
||||
void handle_6xB3x1D_start_battle(const std::string& data);
|
||||
void handle_6xB3x21_end_battle(const std::string& data);
|
||||
void handle_6xB3x28_end_defense_list(const std::string& data);
|
||||
void handle_6xB3x2B_ignored(const std::string&);
|
||||
void handle_6xB3x34_subtract_ally_atk_points(const std::string& data);
|
||||
void handle_6xB3x37_client_ready_to_advance_from_starter_roll_phase(const std::string& data);
|
||||
void handle_6xB3x3A_ignored(const std::string& data);
|
||||
void handle_6xB3x40_map_list_request(const std::string& data);
|
||||
void handle_6xB3x41_map_request(const std::string& data);
|
||||
void handle_6xB3x48_end_turn(const std::string& data);
|
||||
void handle_6xB3x49_card_counts(const std::string& data);
|
||||
void compute_losing_team_id_and_add_winner_flags(uint32_t flags);
|
||||
uint32_t get_team_exp(uint8_t team_id) const;
|
||||
uint32_t send_6xB4x06_if_card_ref_invalid(
|
||||
uint16_t card_ref, int16_t negative_value);
|
||||
void unknown_8023EEF4();
|
||||
void execute_bomb_assist_effect();
|
||||
void replace_targets_due_to_destruction_or_conditions(
|
||||
ActionState* as);
|
||||
bool any_target_exists_for_attack(const ActionState& as);
|
||||
uint8_t get_current_team_turn2() const;
|
||||
void unknown_8023EE48();
|
||||
void unknown_8023EE80();
|
||||
void unknown_802402F4();
|
||||
void send_6xB4x39() const;
|
||||
void send_6xB4x05(); // Recomputes the map occupied bits, so can't be const
|
||||
void send_6xB4x02_for_all_players_if_needed(bool always_send = false);
|
||||
void send_6xB4x50_trap_tile_locations() const;
|
||||
|
||||
G_UpdateDecks_GC_Ep3_6xB4x07 prepare_6xB4x07_decks_update() const;
|
||||
G_SetPlayerNames_GC_Ep3_6xB4x1C prepare_6xB4x1C_names_update() const;
|
||||
static std::string prepare_6xB6x41_map_definition(
|
||||
std::shared_ptr<const DataIndex::MapEntry> map);
|
||||
G_SetTrapTileLocations_GC_Ep3_6xB4x50 prepare_6xB4x50_trap_tile_locations() const;
|
||||
|
||||
std::vector<std::shared_ptr<Card>> const_cast_set_cards_v(
|
||||
const std::vector<std::shared_ptr<const Card>>& cards);
|
||||
private:
|
||||
typedef void (Server::*handler_t)(const std::string&);
|
||||
static const std::unordered_map<uint8_t, handler_t> subcommand_handlers;
|
||||
|
||||
std::weak_ptr<ServerBase> w_base;
|
||||
|
||||
public:
|
||||
uint32_t battle_finished;
|
||||
uint32_t battle_in_progress;
|
||||
uint32_t round_num;
|
||||
BattlePhase battle_phase;
|
||||
uint8_t first_team_turn;
|
||||
uint8_t current_team_turn1;
|
||||
SetupPhase setup_phase;
|
||||
RegistrationPhase registration_phase;
|
||||
ActionSubphase action_subphase;
|
||||
uint8_t current_team_turn2;
|
||||
ActionState pending_attacks[0x20];
|
||||
uint32_t num_pending_attacks;
|
||||
parray<uint8_t, 4> client_done_enqueuing_attacks;
|
||||
parray<uint8_t, 4> player_ready_to_end_phase;
|
||||
std::shared_ptr<PSOV2Encryption> random_crypt;
|
||||
uint32_t unknown_a10;
|
||||
uint32_t overall_time_expired;
|
||||
// Note: In the original implementation, this is a uint32_t and is measured in
|
||||
// seconds. In our environment, the simplest implementation uses now(), which
|
||||
// returns microseconds, so we use a uint64_t instead.
|
||||
uint64_t battle_start_usecs;
|
||||
uint32_t should_copy_prev_states_to_current_states;
|
||||
std::shared_ptr<CardSpecial> card_special;
|
||||
std::shared_ptr<StateFlags> state_flags;
|
||||
std::shared_ptr<PlayerState> player_states[4];
|
||||
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];
|
||||
uint32_t unknown_a14;
|
||||
uint32_t unknown_a15;
|
||||
parray<uint32_t, 4> defense_list_ended_for_client;
|
||||
std::shared_ptr<AssistServer> assist_server;
|
||||
uint16_t next_assist_card_set_number;
|
||||
std::shared_ptr<RulerServer> ruler_server;
|
||||
parray<parray<parray<uint8_t, 2>, 2>, 5> warp_positions; // Array indexes are (type, end, x/y)
|
||||
parray<int16_t, 2> team_exp;
|
||||
parray<int16_t, 2> team_dice_boost;
|
||||
parray<uint32_t, 2> team_client_count;
|
||||
parray<uint32_t, 2> team_num_ally_fcs_destroyed;
|
||||
parray<uint32_t, 2> team_num_cards_destroyed;
|
||||
uint32_t hard_reset_flag;
|
||||
uint8_t tournament_flag;
|
||||
parray<uint8_t, 5> num_trap_tiles_of_type;
|
||||
parray<uint8_t, 5> chosen_trap_tile_index_of_type;
|
||||
parray<parray<parray<uint8_t, 2>, 8>, 5> trap_tile_locs;
|
||||
ActionState pb_action_states[4];
|
||||
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;
|
||||
mutable uint32_t prev_num_6xB4x06_commands_sent;
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Episode3
|
||||
@@ -0,0 +1,773 @@
|
||||
#include "Tournament.hh"
|
||||
|
||||
#include <phosg/Random.hh>
|
||||
|
||||
#include "../CommandFormats.hh"
|
||||
#include "../SendCommands.hh"
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace Episode3 {
|
||||
|
||||
|
||||
|
||||
Tournament::PlayerEntry::PlayerEntry(uint32_t serial_number)
|
||||
: serial_number(serial_number), com_deck() { }
|
||||
|
||||
Tournament::PlayerEntry::PlayerEntry(
|
||||
shared_ptr<const COMDeckDefinition> com_deck)
|
||||
: serial_number(0), com_deck(com_deck) { }
|
||||
|
||||
bool Tournament::PlayerEntry::is_com() const {
|
||||
return (this->com_deck != nullptr);
|
||||
}
|
||||
|
||||
bool Tournament::PlayerEntry::is_human() const {
|
||||
return (this->serial_number != 0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Tournament::Team::Team(
|
||||
shared_ptr<Tournament> tournament, size_t index, size_t max_players)
|
||||
: tournament(tournament),
|
||||
index(index),
|
||||
max_players(max_players),
|
||||
name(""),
|
||||
password(""),
|
||||
num_rounds_cleared(0),
|
||||
is_active(true) { }
|
||||
|
||||
string Tournament::Team::str() const {
|
||||
size_t num_human_players = 0;
|
||||
size_t num_com_players = 0;
|
||||
for (const auto& player : this->players) {
|
||||
num_human_players += player.is_human();
|
||||
num_com_players += player.is_com();
|
||||
}
|
||||
|
||||
string ret = string_printf("[Team/%zu %s %zuH/%zuC/%zuP name=%s pass=%s rounds=%zu",
|
||||
this->index, this->is_active ? "active" : "inactive",
|
||||
num_human_players, num_com_players, this->max_players, this->name.c_str(),
|
||||
this->password.c_str(), this->num_rounds_cleared);
|
||||
for (const auto& player : this->players) {
|
||||
if (player.is_human()) {
|
||||
ret += string_printf(" %08" PRIX32, player.serial_number);
|
||||
}
|
||||
}
|
||||
return ret + "]";
|
||||
}
|
||||
|
||||
void Tournament::Team::register_player(
|
||||
uint32_t serial_number,
|
||||
const string& team_name,
|
||||
const string& password) {
|
||||
if (this->players.size() >= this->max_players) {
|
||||
throw runtime_error("team is full");
|
||||
}
|
||||
|
||||
if (!this->name.empty() && (password != this->password)) {
|
||||
throw runtime_error("incorrect password");
|
||||
}
|
||||
|
||||
auto tournament = this->tournament.lock();
|
||||
if (!tournament) {
|
||||
throw runtime_error("tournament has been deleted");
|
||||
}
|
||||
if (!tournament->all_player_serial_numbers.emplace(serial_number).second) {
|
||||
throw runtime_error("player already registered in same tournament");
|
||||
}
|
||||
|
||||
for (const auto& player : this->players) {
|
||||
if (player.is_human() && (player.serial_number == serial_number)) {
|
||||
throw logic_error("player already registered in team but not in tournament");
|
||||
}
|
||||
}
|
||||
|
||||
this->players.emplace_back(serial_number);
|
||||
|
||||
if (this->name.empty()) {
|
||||
this->name = team_name;
|
||||
this->password = password;
|
||||
}
|
||||
}
|
||||
|
||||
bool Tournament::Team::unregister_player(uint32_t serial_number) {
|
||||
size_t index;
|
||||
for (index = 0; index < this->players.size(); index++) {
|
||||
if (this->players[index].is_human() &&
|
||||
(this->players[index].serial_number == serial_number)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (index < this->players.size()) {
|
||||
this->players.erase(this->players.begin() + index);
|
||||
|
||||
if (this->players.empty()) {
|
||||
this->name.clear();
|
||||
this->password.clear();
|
||||
}
|
||||
|
||||
auto tournament = this->tournament.lock();
|
||||
if (!tournament) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// If the tournament has already started, make the team forfeit their game.
|
||||
// If any player withdraws from a team after the registration phase, the
|
||||
// entire team essentially forfeits their entry.
|
||||
if (tournament->get_state() != Tournament::State::REGISTRATION) {
|
||||
// Look through the pending matches to see if this team is involved in any
|
||||
// of them
|
||||
for (auto match : tournament->pending_matches) {
|
||||
if (!match->preceding_a || !match->preceding_b) {
|
||||
throw logic_error("zero-round match is pending after tournament registration phase");
|
||||
}
|
||||
if (match->preceding_a->winner_team.get() == this) {
|
||||
match->set_winner_team(match->preceding_b->winner_team);
|
||||
break;
|
||||
} else if (match->preceding_b->winner_team.get() == this) {
|
||||
match->set_winner_team(match->preceding_a->winner_team);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// If the tournament has not started yet, just remove the player from the
|
||||
// team
|
||||
} else {
|
||||
if (!tournament->all_player_serial_numbers.erase(serial_number)) {
|
||||
throw logic_error("player removed from team but not from tournament");
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool Tournament::Team::has_any_human_players() const {
|
||||
for (const auto& player : this->players) {
|
||||
if (player.is_human()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t Tournament::Team::num_human_players() const {
|
||||
size_t ret = 0;
|
||||
for (const auto& player : this->players) {
|
||||
ret += player.is_human();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
size_t Tournament::Team::num_com_players() const {
|
||||
size_t ret = 0;
|
||||
for (const auto& player : this->players) {
|
||||
ret += player.is_com();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Tournament::Match::Match(
|
||||
shared_ptr<Tournament> tournament,
|
||||
shared_ptr<Match> preceding_a,
|
||||
shared_ptr<Match> preceding_b)
|
||||
: tournament(tournament),
|
||||
preceding_a(preceding_a),
|
||||
preceding_b(preceding_b),
|
||||
winner_team(nullptr),
|
||||
round_num(0) {
|
||||
if (this->preceding_a->round_num != this->preceding_b->round_num) {
|
||||
throw logic_error("preceding matches have different round numbers");
|
||||
}
|
||||
this->round_num = this->preceding_a->round_num + 1;
|
||||
}
|
||||
|
||||
Tournament::Match::Match(
|
||||
shared_ptr<Tournament> tournament,
|
||||
shared_ptr<Team> winner_team)
|
||||
: tournament(tournament),
|
||||
preceding_a(nullptr),
|
||||
preceding_b(nullptr),
|
||||
winner_team(winner_team),
|
||||
round_num(0) { }
|
||||
|
||||
string Tournament::Match::str() const {
|
||||
string winner_str = this->winner_team ? this->winner_team->str() : "(none)";
|
||||
return string_printf("[Match round=%zu winner=%s]", this->round_num, winner_str.c_str());
|
||||
}
|
||||
|
||||
bool Tournament::Match::resolve_if_no_human_players() {
|
||||
if (this->winner_team) {
|
||||
return true;
|
||||
}
|
||||
// If both matches before this one are resolved and neither winner team has
|
||||
// any humans on it, skip this match entirely and just make one team advance
|
||||
// arbitrarily
|
||||
if (this->preceding_a->winner_team &&
|
||||
this->preceding_b->winner_team &&
|
||||
!this->preceding_a->winner_team->has_any_human_players() &&
|
||||
!this->preceding_b->winner_team->has_any_human_players()) {
|
||||
this->set_winner_team((random_object<uint8_t>() & 1)
|
||||
? this->preceding_b->winner_team : this->preceding_a->winner_team);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void Tournament::Match::on_winner_team_set() {
|
||||
auto tournament = this->tournament.lock();
|
||||
if (!tournament) {
|
||||
return;
|
||||
}
|
||||
|
||||
tournament->pending_matches.erase(this->shared_from_this());
|
||||
|
||||
// Resolve the following match if possible (this skips CPU-only matches). If
|
||||
// the following match can't be resolved, mark it pending.
|
||||
auto following = this->following.lock();
|
||||
if (following && !following->resolve_if_no_human_players()) {
|
||||
tournament->pending_matches.emplace(following);
|
||||
}
|
||||
|
||||
// If there are no pending matches, then the tournament is complete
|
||||
if (tournament->pending_matches.empty()) {
|
||||
tournament->current_state = Tournament::State::COMPLETE;
|
||||
}
|
||||
}
|
||||
|
||||
void Tournament::Match::set_winner_team_without_triggers(shared_ptr<Team> team) {
|
||||
if (!this->preceding_a || !this->preceding_b) {
|
||||
throw logic_error("set_winner_team called on zero-round match");
|
||||
}
|
||||
if ((team != this->preceding_a->winner_team) &&
|
||||
(team != this->preceding_b->winner_team)) {
|
||||
throw logic_error("winner team did not participate in match");
|
||||
}
|
||||
|
||||
this->winner_team = team;
|
||||
|
||||
this->winner_team->num_rounds_cleared++;
|
||||
if (this->winner_team == this->preceding_a->winner_team) {
|
||||
this->preceding_b->winner_team->is_active = false;
|
||||
} else {
|
||||
this->preceding_a->winner_team->is_active = false;
|
||||
}
|
||||
}
|
||||
|
||||
void Tournament::Match::set_winner_team(shared_ptr<Team> team) {
|
||||
this->set_winner_team_without_triggers(team);
|
||||
this->on_winner_team_set();
|
||||
}
|
||||
|
||||
shared_ptr<Tournament::Team> Tournament::Match::opponent_team_for_team(
|
||||
shared_ptr<Team> team) const {
|
||||
if (!this->preceding_a || !this->preceding_b) {
|
||||
throw logic_error("zero-round matches do not have opponents");
|
||||
}
|
||||
if (team == this->preceding_a->winner_team) {
|
||||
return this->preceding_b->winner_team;
|
||||
} else if (team == this->preceding_b->winner_team) {
|
||||
return this->preceding_a->winner_team;
|
||||
} else {
|
||||
throw logic_error("team is not registered for this match");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Tournament::Tournament(
|
||||
shared_ptr<const DataIndex> data_index,
|
||||
uint8_t number,
|
||||
const string& name,
|
||||
shared_ptr<const DataIndex::MapEntry> map,
|
||||
const Rules& rules,
|
||||
size_t num_teams,
|
||||
bool is_2v2)
|
||||
: log(string_printf("[Tournament/%02hhX] ", number)),
|
||||
data_index(data_index),
|
||||
number(number),
|
||||
name(name),
|
||||
map(map),
|
||||
rules(rules),
|
||||
num_teams(num_teams),
|
||||
is_2v2(is_2v2),
|
||||
current_state(State::REGISTRATION) {
|
||||
if (this->num_teams < 4) {
|
||||
throw invalid_argument("team count must be 4 or more");
|
||||
}
|
||||
if (this->num_teams > 32) {
|
||||
throw invalid_argument("team count must be 32 or fewer");
|
||||
}
|
||||
if (this->num_teams & (this->num_teams - 1)) {
|
||||
throw invalid_argument("team count must be a power of 2");
|
||||
}
|
||||
}
|
||||
|
||||
Tournament::Tournament(
|
||||
std::shared_ptr<const DataIndex> data_index,
|
||||
uint8_t number,
|
||||
std::shared_ptr<const JSONObject> json)
|
||||
: log(string_printf("[Tournament/%02hhX] ", number)),
|
||||
data_index(data_index),
|
||||
source_json(json),
|
||||
number(number),
|
||||
current_state(State::REGISTRATION) { }
|
||||
|
||||
void Tournament::init() {
|
||||
vector<size_t> team_index_to_rounds_cleared;
|
||||
|
||||
bool is_registration_complete;
|
||||
if (this->source_json) {
|
||||
auto& dict = this->source_json->as_dict();
|
||||
this->name = dict.at("name")->as_string();
|
||||
this->map = this->data_index->definition_for_map_number(dict.at("map_number")->as_int());
|
||||
this->rules = Rules(dict.at("rules"));
|
||||
this->is_2v2 = dict.at("is_2v2")->as_bool();
|
||||
is_registration_complete = dict.at("is_registration_complete")->as_bool();
|
||||
|
||||
for (const auto& team_json : dict.at("teams")->as_list()) {
|
||||
auto& team_dict = team_json->as_dict();
|
||||
auto& team = this->teams.emplace_back(new Team(
|
||||
this->shared_from_this(),
|
||||
this->teams.size(),
|
||||
team_dict.at("max_players")->as_int()));
|
||||
team->name = team_dict.at("name")->as_string();
|
||||
team->password = team_dict.at("password")->as_string();
|
||||
team_index_to_rounds_cleared.emplace_back(team_dict.at("num_rounds_cleared")->as_int());
|
||||
for (const auto& player_json : team_dict.at("player_specs")->as_list()) {
|
||||
if (player_json->is_int()) {
|
||||
uint32_t serial_number = player_json->as_int();
|
||||
team->players.emplace_back(serial_number);
|
||||
this->all_player_serial_numbers.emplace(serial_number);
|
||||
} else {
|
||||
team->players.emplace_back(this->data_index->com_deck(
|
||||
player_json->as_string()));
|
||||
}
|
||||
}
|
||||
}
|
||||
this->num_teams = this->teams.size();
|
||||
|
||||
this->source_json.reset();
|
||||
|
||||
} else {
|
||||
// Create empty teams
|
||||
while (this->teams.size() < this->num_teams) {
|
||||
auto t = make_shared<Team>(
|
||||
this->shared_from_this(), this->teams.size(), this->is_2v2 ? 2 : 1);
|
||||
this->teams.emplace_back(t);
|
||||
}
|
||||
is_registration_complete = false;
|
||||
}
|
||||
|
||||
// Create the match structure
|
||||
while (this->zero_round_matches.size() < this->num_teams) {
|
||||
this->zero_round_matches.emplace_back(make_shared<Match>(
|
||||
this->shared_from_this(), this->teams[this->zero_round_matches.size()]));
|
||||
}
|
||||
|
||||
// Create the bracket matches
|
||||
vector<shared_ptr<Match>> current_round_matches = this->zero_round_matches;
|
||||
while (current_round_matches.size() > 1) {
|
||||
vector<shared_ptr<Match>> next_round_matches;
|
||||
for (size_t z = 0; z < current_round_matches.size(); z += 2) {
|
||||
auto m = make_shared<Match>(
|
||||
this->shared_from_this(),
|
||||
current_round_matches[z],
|
||||
current_round_matches[z + 1]);
|
||||
current_round_matches[z]->following = m;
|
||||
current_round_matches[z + 1]->following = m;
|
||||
next_round_matches.emplace_back(move(m));
|
||||
}
|
||||
current_round_matches = move(next_round_matches);
|
||||
}
|
||||
this->final_match = current_round_matches.at(0);
|
||||
|
||||
// Compute the match state from the teams' states
|
||||
if (is_registration_complete) {
|
||||
this->current_state = State::IN_PROGRESS;
|
||||
|
||||
// Start with all first-round matches in the match queue
|
||||
unordered_set<shared_ptr<Match>> match_queue;
|
||||
for (auto match : this->zero_round_matches) {
|
||||
match_queue.emplace(match->following.lock());
|
||||
}
|
||||
if (match_queue.count(nullptr)) {
|
||||
throw logic_error("null match in match queue");
|
||||
}
|
||||
|
||||
// For each match in the queue, either resolve it from the previous state or
|
||||
// mark it as unresolvable (hence it should be pending when we're done)
|
||||
while (!match_queue.empty()) {
|
||||
auto match_it = match_queue.begin();
|
||||
auto match = *match_it;
|
||||
match_queue.erase(match_it);
|
||||
|
||||
if (!match->preceding_a->winner_team || !match->preceding_b->winner_team) {
|
||||
throw logic_error("preceding matches are not resolved");
|
||||
}
|
||||
size_t& a_rounds_cleared = team_index_to_rounds_cleared[
|
||||
match->preceding_a->winner_team->index];
|
||||
size_t& b_rounds_cleared = team_index_to_rounds_cleared[
|
||||
match->preceding_b->winner_team->index];
|
||||
if (a_rounds_cleared && b_rounds_cleared) {
|
||||
throw runtime_error("both teams won the same match");
|
||||
}
|
||||
if (!a_rounds_cleared && !b_rounds_cleared) {
|
||||
this->pending_matches.emplace(match); // Neither team has won yet
|
||||
} else {
|
||||
if (a_rounds_cleared) {
|
||||
a_rounds_cleared--;
|
||||
match->set_winner_team_without_triggers(match->preceding_a->winner_team);
|
||||
} else {
|
||||
b_rounds_cleared--;
|
||||
match->set_winner_team_without_triggers(match->preceding_b->winner_team);
|
||||
}
|
||||
|
||||
// If both preceding matches of the following match are resolved, put
|
||||
// the following match on the queue since it may be resolvable as well
|
||||
auto following = match->following.lock();
|
||||
if (following &&
|
||||
following->preceding_a->winner_team &&
|
||||
following->preceding_b->winner_team) {
|
||||
match_queue.emplace(following);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!this->final_match->winner_team == this->pending_matches.empty()) {
|
||||
throw logic_error("there must be pending matches if and only if the final match is not resolved");
|
||||
}
|
||||
|
||||
// If all matches are resolved, then the tournament is complete
|
||||
if (this->final_match->winner_team) {
|
||||
this->current_state = State::COMPLETE;
|
||||
}
|
||||
|
||||
} else {
|
||||
// Make all the zero round matches pending (this is needed so that start()
|
||||
// will auto-resolve all-CPU matches in the first round)
|
||||
for (auto m : this->zero_round_matches) {
|
||||
this->pending_matches.emplace(m);
|
||||
}
|
||||
|
||||
this->current_state = State::REGISTRATION;
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<JSONObject> Tournament::json() const {
|
||||
unordered_map<string, shared_ptr<JSONObject>> dict;
|
||||
dict.emplace("name", make_json_str(this->name));
|
||||
dict.emplace("map_number", make_json_int(this->map->map.map_number));
|
||||
dict.emplace("rules", this->rules.json());
|
||||
dict.emplace("is_2v2", make_json_bool(this->is_2v2));
|
||||
dict.emplace("is_registration_complete", make_json_bool(
|
||||
this->current_state != State::REGISTRATION));
|
||||
|
||||
vector<shared_ptr<JSONObject>> teams_list;
|
||||
for (auto team : this->teams) {
|
||||
unordered_map<string, shared_ptr<JSONObject>> team_dict;
|
||||
team_dict.emplace("max_players", make_json_int(team->max_players));
|
||||
vector<shared_ptr<JSONObject>> player_jsons_list;
|
||||
for (const auto& player : team->players) {
|
||||
if (player.is_human()) {
|
||||
player_jsons_list.emplace_back(make_json_int(player.serial_number));
|
||||
} else {
|
||||
player_jsons_list.emplace_back(make_json_str(player.com_deck->deck_name));
|
||||
}
|
||||
}
|
||||
team_dict.emplace("player_specs", make_json_list(move(player_jsons_list)));
|
||||
team_dict.emplace("name", make_json_str(team->name));
|
||||
team_dict.emplace("password", make_json_str(team->password));
|
||||
team_dict.emplace("num_rounds_cleared", make_json_int(team->num_rounds_cleared));
|
||||
teams_list.emplace_back(new JSONObject(move(team_dict)));
|
||||
}
|
||||
dict.emplace("teams", make_json_list(move(teams_list)));
|
||||
return shared_ptr<JSONObject>(new JSONObject(move(dict)));
|
||||
}
|
||||
|
||||
std::shared_ptr<const DataIndex> Tournament::get_data_index() const {
|
||||
return this->data_index;
|
||||
}
|
||||
|
||||
uint8_t Tournament::get_number() const {
|
||||
return this->number;
|
||||
}
|
||||
|
||||
const string& Tournament::get_name() const {
|
||||
return this->name;
|
||||
}
|
||||
|
||||
shared_ptr<const DataIndex::MapEntry> Tournament::get_map() const {
|
||||
return this->map;
|
||||
}
|
||||
|
||||
const Rules& Tournament::get_rules() const {
|
||||
return this->rules;
|
||||
}
|
||||
|
||||
bool Tournament::get_is_2v2() const {
|
||||
return this->is_2v2;
|
||||
}
|
||||
|
||||
Tournament::State Tournament::get_state() const {
|
||||
return this->current_state;
|
||||
}
|
||||
|
||||
const vector<shared_ptr<Tournament::Team>>& Tournament::all_teams() const {
|
||||
return this->teams;
|
||||
}
|
||||
|
||||
shared_ptr<Tournament::Team> Tournament::get_team(size_t index) const {
|
||||
return this->teams.at(index);
|
||||
}
|
||||
|
||||
shared_ptr<Tournament::Team> Tournament::get_winner_team() const {
|
||||
if (this->current_state != State::COMPLETE) {
|
||||
return nullptr;
|
||||
}
|
||||
if (!this->final_match->winner_team) {
|
||||
throw logic_error("tournament is complete but winner is not set");
|
||||
}
|
||||
return this->final_match->winner_team;
|
||||
}
|
||||
|
||||
shared_ptr<Tournament::Match> Tournament::next_match_for_team(
|
||||
shared_ptr<Team> team) const {
|
||||
if (this->current_state == Tournament::State::REGISTRATION) {
|
||||
return nullptr;
|
||||
}
|
||||
for (auto match : this->pending_matches) {
|
||||
if (!match->preceding_a || !match->preceding_b) {
|
||||
throw logic_error("zero-round match is pending after tournament registration phase");
|
||||
}
|
||||
if ((team == match->preceding_a->winner_team) ||
|
||||
(team == match->preceding_b->winner_team)) {
|
||||
return match;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
shared_ptr<Tournament::Match> Tournament::get_final_match() const {
|
||||
return this->final_match;
|
||||
}
|
||||
|
||||
shared_ptr<Tournament::Team> Tournament::team_for_serial_number(
|
||||
uint32_t serial_number) const {
|
||||
if (!this->all_player_serial_numbers.count(serial_number)) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
for (auto team : this->teams) {
|
||||
for (const auto& player : team->players) {
|
||||
if (player.serial_number == serial_number) {
|
||||
return team->is_active ? team : nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw logic_error("serial number registered in tournament but not in any team");
|
||||
}
|
||||
|
||||
const set<uint32_t>& Tournament::get_all_player_serial_numbers() const {
|
||||
return this->all_player_serial_numbers;
|
||||
}
|
||||
|
||||
void Tournament::start() {
|
||||
if (this->current_state != State::REGISTRATION) {
|
||||
throw runtime_error("tournament has already started");
|
||||
}
|
||||
|
||||
this->current_state = State::IN_PROGRESS;
|
||||
|
||||
// Assign names to COM teams, and assign COM decks to all empty slots
|
||||
for (size_t z = 0; z < this->zero_round_matches.size(); z++) {
|
||||
auto m = this->zero_round_matches[z];
|
||||
auto t = m->winner_team;
|
||||
if (t->name.empty()) {
|
||||
t->name = string_printf("COM:%zu", z);
|
||||
}
|
||||
for (const auto& player : t->players) {
|
||||
if (player.is_com()) {
|
||||
throw logic_error("non-human player on team before tournament start");
|
||||
}
|
||||
}
|
||||
if (this->data_index->num_com_decks() < t->max_players - t->players.size()) {
|
||||
throw runtime_error("not enough COM decks to complete team");
|
||||
}
|
||||
// TODO: Don't allow duplicate COM decks, nor duplicate COM SCs on the same
|
||||
// team
|
||||
while (t->players.size() < t->max_players) {
|
||||
t->players.emplace_back(this->data_index->random_com_deck());
|
||||
}
|
||||
}
|
||||
|
||||
// Resolve all possible CPU-only matches
|
||||
for (auto m : this->zero_round_matches) {
|
||||
m->on_winner_team_set();
|
||||
}
|
||||
}
|
||||
|
||||
void Tournament::print_bracket(FILE* stream) const {
|
||||
function<void(shared_ptr<Match>, size_t)> print_match = [&](shared_ptr<Match> m, size_t indent_level) -> void {
|
||||
for (size_t z = 0; z < indent_level; z++) {
|
||||
fputc(' ', stream);
|
||||
fputc(' ', stream);
|
||||
}
|
||||
string match_str = m->str();
|
||||
fprintf(stream, "%s%s\n", match_str.c_str(), this->pending_matches.count(m) ? " (PENDING)" : "");
|
||||
if (m->preceding_a) {
|
||||
print_match(m->preceding_a, indent_level + 1);
|
||||
}
|
||||
if (m->preceding_b) {
|
||||
print_match(m->preceding_b, indent_level + 1);
|
||||
}
|
||||
};
|
||||
fprintf(stream, "Tournament %02hhX: %s\n", this->number, this->name.c_str());
|
||||
string map_name = this->map->map.name;
|
||||
fprintf(stream, " Map: %08" PRIX32 " (%s)\n", this->map->map.map_number.load(), map_name.c_str());
|
||||
string rules_str = this->rules.str();
|
||||
fprintf(stream, " Rules: %s\n", rules_str.c_str());
|
||||
fprintf(stream, " Structure: %s, %zu entries\n", this->is_2v2 ? "2v2" : "1v1", this->num_teams);
|
||||
switch (this->current_state) {
|
||||
case State::REGISTRATION:
|
||||
fprintf(stream, " State: REGISTRATION\n");
|
||||
break;
|
||||
case State::IN_PROGRESS:
|
||||
fprintf(stream, " State: IN_PROGRESS\n");
|
||||
break;
|
||||
case State::COMPLETE:
|
||||
fprintf(stream, " State: COMPLETE\n");
|
||||
break;
|
||||
default:
|
||||
fprintf(stream, " State: UNKNOWN\n");
|
||||
break;
|
||||
}
|
||||
fprintf(stream, " Standings:\n");
|
||||
print_match(this->final_match, 2);
|
||||
fprintf(stream, " Pending matches:\n");
|
||||
for (const auto& match : this->pending_matches) {
|
||||
string match_str = match->str();
|
||||
fprintf(stream, " %s\n", match_str.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
TournamentIndex::TournamentIndex(
|
||||
shared_ptr<const DataIndex> data_index,
|
||||
const string& state_filename,
|
||||
bool skip_load_state)
|
||||
: data_index(data_index), state_filename(state_filename) {
|
||||
if (this->state_filename.empty() || skip_load_state) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto json = JSONObject::parse(load_file(this->state_filename));
|
||||
|
||||
auto& list = json->as_list();
|
||||
if (list.size() != 0x20) {
|
||||
throw runtime_error("tournament JSON list length is incorrect");
|
||||
}
|
||||
for (size_t z = 0; z < 0x20; z++) {
|
||||
if (!list.at(z)->is_null()) {
|
||||
this->tournaments[z].reset(new Tournament(this->data_index, z, list[z]));
|
||||
this->tournaments[z]->init();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TournamentIndex::save() const {
|
||||
if (this->state_filename.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
vector<shared_ptr<JSONObject>> list;
|
||||
for (size_t z = 0; z < 0x20; z++) {
|
||||
if (this->tournaments[z]) {
|
||||
list.emplace_back(this->tournaments[z]->json());
|
||||
} else {
|
||||
list.emplace_back(make_json_null());
|
||||
}
|
||||
}
|
||||
auto json = make_json_list(move(list));
|
||||
save_file(this->state_filename, json->format());
|
||||
}
|
||||
|
||||
vector<shared_ptr<Tournament>> TournamentIndex::all_tournaments() const {
|
||||
vector<shared_ptr<Tournament>> ret;
|
||||
for (size_t z = 0; z < 0x20; z++) {
|
||||
if (this->tournaments[z]) {
|
||||
ret.emplace_back(this->tournaments[z]);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
shared_ptr<Tournament> TournamentIndex::create_tournament(
|
||||
const string& name,
|
||||
shared_ptr<const DataIndex::MapEntry> map,
|
||||
const Rules& rules,
|
||||
size_t num_teams,
|
||||
bool is_2v2) {
|
||||
// Find an unused tournament number
|
||||
uint8_t number;
|
||||
for (number = 0; number < 0x20; number++) {
|
||||
if (!this->tournaments[number]) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (number >= 0x20) {
|
||||
throw runtime_error("all tournament slots are full");
|
||||
}
|
||||
|
||||
auto t = make_shared<Tournament>(
|
||||
this->data_index, number, name, map, rules, num_teams, is_2v2);
|
||||
t->init();
|
||||
this->tournaments[number] = t;
|
||||
return t;
|
||||
}
|
||||
|
||||
void TournamentIndex::delete_tournament(uint8_t number) {
|
||||
this->tournaments[number].reset();
|
||||
}
|
||||
|
||||
shared_ptr<Tournament> TournamentIndex::get_tournament(uint8_t number) const {
|
||||
return this->tournaments[number];
|
||||
}
|
||||
|
||||
shared_ptr<Tournament> TournamentIndex::get_tournament(const string& name) const {
|
||||
for (size_t z = 0; z < 0x20; z++) {
|
||||
if (this->tournaments[z] && (this->tournaments[z]->get_name() == name)) {
|
||||
return this->tournaments[z];
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
shared_ptr<Tournament::Team> TournamentIndex::team_for_serial_number(
|
||||
uint32_t serial_number) const {
|
||||
for (size_t z = 0; z < 0x20; z++) {
|
||||
if (!this->tournaments[z]) {
|
||||
continue;
|
||||
}
|
||||
auto team = this->tournaments[z]->team_for_serial_number(serial_number);
|
||||
if (team) {
|
||||
return team;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace Episode3
|
||||
@@ -0,0 +1,195 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <event2/event.h>
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <unordered_set>
|
||||
#include <string>
|
||||
#include <phosg/JSON.hh>
|
||||
#include <phosg/Strings.hh>
|
||||
|
||||
#include "../Player.hh"
|
||||
|
||||
struct Lobby;
|
||||
|
||||
namespace Episode3 {
|
||||
|
||||
// The comment in Server.hh does not apply to this file (and Tournament.cc).
|
||||
|
||||
|
||||
|
||||
class Tournament : public std::enable_shared_from_this<Tournament> {
|
||||
public:
|
||||
enum class State {
|
||||
REGISTRATION = 0,
|
||||
IN_PROGRESS,
|
||||
COMPLETE,
|
||||
};
|
||||
|
||||
struct PlayerEntry {
|
||||
// Invariant: (serial_number == 0) != (com_deck == nullptr)
|
||||
// (that is, exactly one of the following must be valid)
|
||||
uint32_t serial_number;
|
||||
std::shared_ptr<const COMDeckDefinition> com_deck;
|
||||
|
||||
explicit PlayerEntry(uint32_t serial_number);
|
||||
explicit PlayerEntry(std::shared_ptr<const COMDeckDefinition> com_deck);
|
||||
|
||||
bool is_com() const;
|
||||
bool is_human() const;
|
||||
};
|
||||
|
||||
struct Team : public std::enable_shared_from_this<Team> {
|
||||
std::weak_ptr<Tournament> tournament;
|
||||
size_t index;
|
||||
size_t max_players;
|
||||
|
||||
std::vector<PlayerEntry> players;
|
||||
std::string name;
|
||||
std::string password;
|
||||
size_t num_rounds_cleared;
|
||||
bool is_active;
|
||||
|
||||
Team(
|
||||
std::shared_ptr<Tournament> tournament,
|
||||
size_t index,
|
||||
size_t max_players);
|
||||
std::string str() const;
|
||||
|
||||
void register_player(
|
||||
uint32_t serial_number,
|
||||
const std::string& team_name,
|
||||
const std::string& password);
|
||||
bool unregister_player(uint32_t serial_number);
|
||||
|
||||
bool has_any_human_players() const;
|
||||
size_t num_human_players() const;
|
||||
size_t num_com_players() const;
|
||||
};
|
||||
|
||||
struct Match : public std::enable_shared_from_this<Match> {
|
||||
std::weak_ptr<Tournament> tournament;
|
||||
std::shared_ptr<Match> preceding_a;
|
||||
std::shared_ptr<Match> preceding_b;
|
||||
std::weak_ptr<Match> following;
|
||||
std::shared_ptr<Team> winner_team;
|
||||
size_t round_num;
|
||||
|
||||
Match(
|
||||
std::shared_ptr<Tournament> tournament,
|
||||
std::shared_ptr<Match> preceding_a,
|
||||
std::shared_ptr<Match> preceding_b);
|
||||
Match(
|
||||
std::shared_ptr<Tournament> tournament,
|
||||
std::shared_ptr<Team> winner_team);
|
||||
std::string str() const;
|
||||
|
||||
bool resolve_if_no_human_players();
|
||||
void on_winner_team_set();
|
||||
void set_winner_team(std::shared_ptr<Team> team);
|
||||
void set_winner_team_without_triggers(std::shared_ptr<Team> team);
|
||||
std::shared_ptr<Team> opponent_team_for_team(std::shared_ptr<Team> team) const;
|
||||
};
|
||||
|
||||
Tournament(
|
||||
std::shared_ptr<const DataIndex> data_index,
|
||||
uint8_t number,
|
||||
const std::string& name,
|
||||
std::shared_ptr<const DataIndex::MapEntry> map,
|
||||
const Rules& rules,
|
||||
size_t num_teams,
|
||||
bool is_2v2);
|
||||
Tournament(
|
||||
std::shared_ptr<const DataIndex> data_index,
|
||||
uint8_t number,
|
||||
std::shared_ptr<const JSONObject> json);
|
||||
~Tournament() = default;
|
||||
void init();
|
||||
|
||||
std::shared_ptr<JSONObject> json() const;
|
||||
|
||||
std::shared_ptr<const DataIndex> get_data_index() const;
|
||||
uint8_t get_number() const;
|
||||
const std::string& get_name() const;
|
||||
std::shared_ptr<const DataIndex::MapEntry> get_map() const;
|
||||
const Rules& get_rules() const;
|
||||
bool get_is_2v2() const;
|
||||
State get_state() const;
|
||||
|
||||
const std::vector<std::shared_ptr<Team>>& all_teams() const;
|
||||
std::shared_ptr<Team> get_team(size_t index) const;
|
||||
std::shared_ptr<Team> get_winner_team() const;
|
||||
std::shared_ptr<Match> next_match_for_team(std::shared_ptr<Team> team) const;
|
||||
std::shared_ptr<Match> get_final_match() const;
|
||||
std::shared_ptr<Team> team_for_serial_number(uint32_t serial_number) const;
|
||||
const std::set<uint32_t>& get_all_player_serial_numbers() const;
|
||||
|
||||
void start();
|
||||
|
||||
void print_bracket(FILE* stream) const;
|
||||
|
||||
private:
|
||||
PrefixedLogger log;
|
||||
|
||||
std::shared_ptr<const DataIndex> data_index;
|
||||
std::shared_ptr<const JSONObject> source_json;
|
||||
uint8_t number;
|
||||
std::string name;
|
||||
std::shared_ptr<const DataIndex::MapEntry> map;
|
||||
Rules rules;
|
||||
size_t num_teams;
|
||||
bool is_2v2;
|
||||
State current_state;
|
||||
|
||||
std::set<uint32_t> all_player_serial_numbers;
|
||||
std::unordered_set<std::shared_ptr<Match>> pending_matches;
|
||||
|
||||
// This vector contains all teams in the original starting order of the
|
||||
// tournament (that is, all teams in the first round). The order within this
|
||||
// vector determines which team will play against which other team in the
|
||||
// first round: [0] will play against [1], [2] will play against [3], etc.
|
||||
std::vector<std::shared_ptr<Team>> teams;
|
||||
// The tournament begins with a "zero round", in which each team automatically
|
||||
// "wins" a match, putting them into the first round. This is just to make the
|
||||
// data model easier to manage, so we don't have to have a type of match with
|
||||
// no preceding round.
|
||||
std::vector<std::shared_ptr<Match>> zero_round_matches;
|
||||
std::shared_ptr<Match> final_match;
|
||||
};
|
||||
|
||||
class TournamentIndex {
|
||||
public:
|
||||
explicit TournamentIndex(
|
||||
std::shared_ptr<const DataIndex> data_index,
|
||||
const std::string& state_filename,
|
||||
bool skip_load_state = false);
|
||||
~TournamentIndex() = default;
|
||||
|
||||
void save() const;
|
||||
|
||||
std::vector<std::shared_ptr<Tournament>> all_tournaments() const;
|
||||
|
||||
std::shared_ptr<Tournament> create_tournament(
|
||||
const std::string& name,
|
||||
std::shared_ptr<const DataIndex::MapEntry> map,
|
||||
const Rules& rules,
|
||||
size_t num_teams,
|
||||
bool is_2v2);
|
||||
void delete_tournament(uint8_t number);
|
||||
std::shared_ptr<Tournament> get_tournament(uint8_t number) const;
|
||||
std::shared_ptr<Tournament> get_tournament(const std::string& name) const;
|
||||
|
||||
std::shared_ptr<Tournament::Team> team_for_serial_number(
|
||||
uint32_t serial_number) const;
|
||||
|
||||
private:
|
||||
std::shared_ptr<const DataIndex> data_index;
|
||||
std::string state_filename;
|
||||
std::shared_ptr<Tournament> tournaments[0x20];
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Episode3
|
||||
+51
-19
@@ -8,36 +8,68 @@
|
||||
using namespace std;
|
||||
|
||||
|
||||
FileContentsCache::File::File(const string& name, shared_ptr<const string> contents,
|
||||
uint64_t load_time) : name(name), contents(contents), load_time(load_time) { }
|
||||
|
||||
shared_ptr<const string> FileContentsCache::get(const std::string& name) {
|
||||
return this->get(name, [name]() -> string { return load_file(name); });
|
||||
FileContentsCache::FileContentsCache(uint64_t ttl_usecs) : ttl_usecs(ttl_usecs) { }
|
||||
|
||||
FileContentsCache::File::File(
|
||||
const string& name,
|
||||
string&& data,
|
||||
uint64_t load_time)
|
||||
: name(name), data(new string(move(data))), load_time(load_time) { }
|
||||
|
||||
shared_ptr<const FileContentsCache::File> FileContentsCache::replace(
|
||||
const string& name, string&& data, uint64_t t) {
|
||||
if (t == 0) {
|
||||
t = now();
|
||||
}
|
||||
shared_ptr<File> new_file(new File(name, move(data), t));
|
||||
auto emplace_ret = this->name_to_file.emplace(name, new_file);
|
||||
if (!emplace_ret.second) {
|
||||
emplace_ret.first->second = new_file;
|
||||
}
|
||||
return new_file;
|
||||
}
|
||||
|
||||
shared_ptr<const string> FileContentsCache::get(const char* name) {
|
||||
return this->get(string(name));
|
||||
shared_ptr<const FileContentsCache::File> FileContentsCache::replace(
|
||||
const string& name, const void* data, size_t size, uint64_t t) {
|
||||
string s(reinterpret_cast<const char*>(data), size);
|
||||
return this->replace(name, move(s), t);
|
||||
}
|
||||
|
||||
shared_ptr<const string> FileContentsCache::get(const std::string& name,
|
||||
std::function<std::string()> generate) {
|
||||
FileContentsCache::GetResult FileContentsCache::get_or_load(const std::string& name) {
|
||||
return this->get(name, load_file);
|
||||
}
|
||||
|
||||
FileContentsCache::GetResult FileContentsCache::get_or_load(const char* name) {
|
||||
return this->get_or_load(string(name));
|
||||
}
|
||||
|
||||
shared_ptr<const FileContentsCache::File> FileContentsCache::get_or_throw(
|
||||
const std::string& name) {
|
||||
auto throw_fn = +[](const std::string&) -> string {
|
||||
throw out_of_range("file missing from cache");
|
||||
};
|
||||
return this->get(name, throw_fn).file;
|
||||
}
|
||||
|
||||
shared_ptr<const FileContentsCache::File> FileContentsCache::get_or_throw(
|
||||
const char* name) {
|
||||
return this->get_or_throw(string(name));
|
||||
}
|
||||
|
||||
FileContentsCache::GetResult FileContentsCache::get(const std::string& name,
|
||||
std::function<std::string(const std::string&)> generate) {
|
||||
uint64_t t = now();
|
||||
try {
|
||||
auto& entry = this->name_to_file.at(name);
|
||||
if (t - entry.load_time < 300000000) { // not 5 minutes old? return it
|
||||
return entry.contents;
|
||||
if (this->ttl_usecs && (t - entry->load_time < this->ttl_usecs)) {
|
||||
return {entry, false};
|
||||
}
|
||||
} catch (const out_of_range& e) { }
|
||||
|
||||
shared_ptr<const string> contents(new string(generate()));
|
||||
this->name_to_file.erase(name);
|
||||
this->name_to_file.emplace(piecewise_construct, forward_as_tuple(name),
|
||||
forward_as_tuple(name, contents, t));
|
||||
|
||||
return contents;
|
||||
return {this->replace(name, generate(name)), true};
|
||||
}
|
||||
|
||||
shared_ptr<const string> FileContentsCache::get(const char* name,
|
||||
std::function<std::string()> generate) {
|
||||
FileContentsCache::GetResult FileContentsCache::get(const char* name,
|
||||
std::function<std::string(const std::string&)> generate) {
|
||||
return this->get(string(name), generate);
|
||||
}
|
||||
|
||||
+77
-13
@@ -5,19 +5,20 @@
|
||||
#include <unordered_map>
|
||||
#include <functional>
|
||||
|
||||
#include <phosg/Time.hh>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
class FileContentsCache {
|
||||
private:
|
||||
public:
|
||||
struct File {
|
||||
std::string name;
|
||||
std::shared_ptr<const std::string> contents;
|
||||
shared_ptr<const std::string> data;
|
||||
uint64_t load_time;
|
||||
|
||||
File() = delete;
|
||||
File(const std::string& name, std::shared_ptr<const std::string> contents,
|
||||
uint64_t load_time);
|
||||
File(const std::string& name, std::string&& contents, uint64_t load_time);
|
||||
File(const File&) = delete;
|
||||
File(File&&) = delete;
|
||||
File& operator=(const File&) = delete;
|
||||
@@ -25,22 +26,85 @@ private:
|
||||
~File() = default;
|
||||
};
|
||||
|
||||
public:
|
||||
FileContentsCache() = default;
|
||||
explicit FileContentsCache(uint64_t ttl_usecs);
|
||||
FileContentsCache(const FileContentsCache&) = delete;
|
||||
FileContentsCache(FileContentsCache&&) = delete;
|
||||
FileContentsCache& operator=(const FileContentsCache&) = delete;
|
||||
FileContentsCache& operator=(FileContentsCache&&) = delete;
|
||||
~FileContentsCache() = default;
|
||||
|
||||
std::shared_ptr<const std::string> get(const std::string& name);
|
||||
std::shared_ptr<const std::string> get(const char* name);
|
||||
template <typename NameT>
|
||||
bool delete_key(NameT key) {
|
||||
return this->name_to_file.erase(key);
|
||||
}
|
||||
|
||||
std::shared_ptr<const std::string> get(
|
||||
const std::string& name, std::function<std::string()> generate);
|
||||
std::shared_ptr<const std::string> get(
|
||||
const char* name, std::function<std::string()> generate);
|
||||
std::shared_ptr<const File> replace(
|
||||
const std::string& name, std::string&& data, uint64_t t = 0);
|
||||
std::shared_ptr<const File> replace(
|
||||
const std::string& name, const void* data, size_t size, uint64_t t = 0);
|
||||
|
||||
struct GetResult {
|
||||
std::shared_ptr<const File> file;
|
||||
bool generate_called;
|
||||
};
|
||||
|
||||
GetResult get_or_load(const std::string& name);
|
||||
GetResult get_or_load(const char* name);
|
||||
std::shared_ptr<const File> get_or_throw(const std::string& name);
|
||||
std::shared_ptr<const File> get_or_throw(const char* name);
|
||||
|
||||
GetResult get(
|
||||
const std::string& name, std::function<std::string(const std::string&)> generate);
|
||||
GetResult get(
|
||||
const char* name, std::function<std::string(const std::string&)> generate);
|
||||
|
||||
template <typename T>
|
||||
struct GetObjResult {
|
||||
const T& obj;
|
||||
std::shared_ptr<const File> data;
|
||||
bool generate_called;
|
||||
};
|
||||
|
||||
template <typename T, typename NameT>
|
||||
GetObjResult<T> get_obj_or_load(NameT name) {
|
||||
auto res = this->get_or_load(name);
|
||||
if (res.file->data->size() != sizeof(T)) {
|
||||
throw runtime_error("cached string size is incorrect");
|
||||
}
|
||||
return {*reinterpret_cast<const T*>(res.file->data->data()), res.file, res.generate_called};
|
||||
}
|
||||
template <typename T, typename NameT>
|
||||
GetObjResult<T> get_obj_or_throw(NameT name) {
|
||||
auto res = this->get_or_throw(name);
|
||||
if (res.file->data->size() != sizeof(T)) {
|
||||
throw runtime_error("cached string size is incorrect");
|
||||
}
|
||||
return {*reinterpret_cast<const T*>(res.file->data->data()), res.file, res.generate_called};
|
||||
}
|
||||
template <typename T, typename NameT>
|
||||
GetObjResult<T> get_obj(NameT name, std::function<T(const std::string&)> generate) {
|
||||
uint64_t t = now();
|
||||
try {
|
||||
auto& f = this->name_to_file.at(name);
|
||||
if (f->data->size() != sizeof(T)) {
|
||||
throw runtime_error("cached string size is incorrect");
|
||||
}
|
||||
if (this->ttl_usecs && (t - f->load_time < this->ttl_usecs)) {
|
||||
return {*reinterpret_cast<const T*>(f->data->data()), f, false};
|
||||
}
|
||||
} catch (const out_of_range& e) { }
|
||||
T value = generate(name);
|
||||
auto ret = this->replace_obj(name, value);
|
||||
ret.generate_called = true;
|
||||
return ret;
|
||||
}
|
||||
template <typename T, typename NameT>
|
||||
GetObjResult<T> replace_obj(NameT name, const T& value) {
|
||||
auto cached_value = this->replace(name, &value, sizeof(value));
|
||||
return {*reinterpret_cast<const T*>(cached_value->data->data()), cached_value, false};
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<std::string, File> name_to_file;
|
||||
std::unordered_map<std::string, std::shared_ptr<File>> name_to_file;
|
||||
uint64_t ttl_usecs;
|
||||
};
|
||||
|
||||
+76
-42
@@ -10,6 +10,7 @@
|
||||
#include <resource_file/Emulators/PPC32Emulator.hh>
|
||||
#endif
|
||||
|
||||
#include "Loggers.hh"
|
||||
#include "CommandFormats.hh"
|
||||
|
||||
using namespace std;
|
||||
@@ -26,14 +27,28 @@ bool function_compiler_available() {
|
||||
|
||||
|
||||
|
||||
string CompiledFunctionCode::generate_client_command(
|
||||
const char* name_for_architecture(CompiledFunctionCode::Architecture arch) {
|
||||
switch (arch) {
|
||||
case CompiledFunctionCode::Architecture::POWERPC:
|
||||
return "PowerPC";
|
||||
case CompiledFunctionCode::Architecture::X86:
|
||||
return "x86";
|
||||
default:
|
||||
throw logic_error("invalid architecture");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
template <typename FooterT, typename U16T>
|
||||
string CompiledFunctionCode::generate_client_command_t(
|
||||
const unordered_map<string, uint32_t>& label_writes,
|
||||
const string& suffix) const {
|
||||
S_ExecuteCode_Footer_GC_B2 footer;
|
||||
FooterT footer;
|
||||
footer.num_relocations = this->relocation_deltas.size();
|
||||
footer.unused1.clear();
|
||||
footer.unused1.clear(0);
|
||||
footer.entrypoint_addr_offset = this->entrypoint_offset_offset;
|
||||
footer.unused2.clear();
|
||||
footer.unused2.clear(0);
|
||||
|
||||
StringWriter w;
|
||||
if (!label_writes.empty()) {
|
||||
@@ -56,7 +71,7 @@ string CompiledFunctionCode::generate_client_command(
|
||||
|
||||
footer.relocations_offset = w.size();
|
||||
for (uint16_t delta : this->relocation_deltas) {
|
||||
w.put_u16b(delta);
|
||||
w.put<U16T>(delta);
|
||||
}
|
||||
if (this->relocation_deltas.size() & 1) {
|
||||
w.put_u16(0);
|
||||
@@ -66,39 +81,52 @@ string CompiledFunctionCode::generate_client_command(
|
||||
return move(w.str());
|
||||
}
|
||||
|
||||
string CompiledFunctionCode::generate_client_command(
|
||||
const unordered_map<string, uint32_t>& label_writes,
|
||||
const string& suffix) const {
|
||||
if (this->arch == Architecture::POWERPC) {
|
||||
return this->generate_client_command_t<S_ExecuteCode_Footer_GC_B2, be_uint16_t>(
|
||||
label_writes, suffix);
|
||||
} else if ((this->arch == Architecture::X86) || (this->arch == Architecture::SH4)) {
|
||||
return this->generate_client_command_t<S_ExecuteCode_Footer_DC_PC_XB_BB_B2, le_uint16_t>(
|
||||
label_writes, suffix);
|
||||
} else {
|
||||
throw logic_error("invalid architecture");
|
||||
}
|
||||
}
|
||||
|
||||
bool CompiledFunctionCode::is_big_endian() const {
|
||||
return this->arch == Architecture::POWERPC;
|
||||
}
|
||||
|
||||
|
||||
|
||||
shared_ptr<CompiledFunctionCode> compile_function_code(
|
||||
const string& directory, const string& name, const string& text) {
|
||||
CompiledFunctionCode::Architecture arch,
|
||||
const string& directory,
|
||||
const string& name,
|
||||
const string& text) {
|
||||
#ifndef HAVE_RESOURCE_FILE
|
||||
(void)arch;
|
||||
(void)directory;
|
||||
(void)name;
|
||||
(void)text;
|
||||
throw runtime_error("PowerPC assembler is not available");
|
||||
throw runtime_error("function compiler is not available");
|
||||
|
||||
#else
|
||||
std::unordered_set<string> get_include_stack; // For mutual recursion detection
|
||||
function<string(const string&)> get_include = [&](const string& name) -> string {
|
||||
if (!get_include_stack.emplace(name).second) {
|
||||
throw runtime_error("mutual recursion between includes");
|
||||
}
|
||||
|
||||
string filename = directory + "/" + name + ".inc.s";
|
||||
if (isfile(filename)) {
|
||||
return PPC32Emulator::assemble(load_file(filename), get_include).code;
|
||||
}
|
||||
filename = directory + "/" + name + ".inc.bin";
|
||||
if (isfile(filename)) {
|
||||
return load_file(filename);
|
||||
}
|
||||
throw runtime_error("data not found for include " + name);
|
||||
};
|
||||
|
||||
shared_ptr<CompiledFunctionCode> ret(new CompiledFunctionCode());
|
||||
ret->arch = arch;
|
||||
ret->name = name;
|
||||
ret->index = 0;
|
||||
ret->hide_from_patches_menu = false;
|
||||
|
||||
auto assembled = PPC32Emulator::assemble(text, get_include);
|
||||
ret->code = move(assembled.code);
|
||||
ret->label_offsets = move(assembled.label_offsets);
|
||||
if (arch == CompiledFunctionCode::Architecture::POWERPC) {
|
||||
auto assembled = PPC32Emulator::assemble(text, {directory});
|
||||
ret->code = move(assembled.code);
|
||||
ret->label_offsets = move(assembled.label_offsets);
|
||||
} else if (arch == CompiledFunctionCode::Architecture::X86) {
|
||||
throw runtime_error("x86 assembler is not implemented");
|
||||
}
|
||||
|
||||
set<uint32_t> reloc_indexes;
|
||||
for (const auto& it : ret->label_offsets) {
|
||||
@@ -106,6 +134,8 @@ shared_ptr<CompiledFunctionCode> compile_function_code(
|
||||
reloc_indexes.emplace(it.second / 4);
|
||||
} else if (starts_with(it.first, "newserv_index_")) {
|
||||
ret->index = stoul(it.first.substr(14), nullptr, 16);
|
||||
} else if (it.first == "hide_from_patches_menu") {
|
||||
ret->hide_from_patches_menu = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -133,7 +163,7 @@ shared_ptr<CompiledFunctionCode> compile_function_code(
|
||||
|
||||
FunctionCodeIndex::FunctionCodeIndex(const string& directory) {
|
||||
if (!function_compiler_available()) {
|
||||
log(INFO, "Function compiler is not available");
|
||||
function_compiler_log.info("Function compiler is not available");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -148,7 +178,8 @@ FunctionCodeIndex::FunctionCodeIndex(const string& directory) {
|
||||
try {
|
||||
string path = directory + "/" + filename;
|
||||
string text = load_file(path);
|
||||
auto code = compile_function_code(directory, name, text);
|
||||
auto code = compile_function_code(
|
||||
CompiledFunctionCode::Architecture::POWERPC, directory, name, text);
|
||||
if (code->index != 0) {
|
||||
if (!this->index_to_function.emplace(code->index, code).second) {
|
||||
throw runtime_error(string_printf(
|
||||
@@ -157,17 +188,18 @@ FunctionCodeIndex::FunctionCodeIndex(const string& directory) {
|
||||
}
|
||||
this->name_to_function.emplace(name, code);
|
||||
if (is_patch) {
|
||||
this->menu_item_id_to_patch_function.emplace(next_menu_item_id++, code);
|
||||
code->menu_item_id = next_menu_item_id++;
|
||||
this->menu_item_id_to_patch_function.emplace(code->menu_item_id, code);
|
||||
this->name_to_patch_function.emplace(name, code);
|
||||
}
|
||||
if (code->index) {
|
||||
log(INFO, "Compiled function %02X => %s", code->index, name.c_str());
|
||||
} else {
|
||||
log(INFO, "Compiled function %s", name.c_str());
|
||||
}
|
||||
|
||||
string index_prefix = code->index ? string_printf("%02X => ", code->index) : "";
|
||||
string patch_prefix = is_patch ? string_printf("[%08" PRIX32 "] ", code->menu_item_id) : "";
|
||||
function_compiler_log.info("Compiled function %s%s%s (%s)",
|
||||
index_prefix.c_str(), patch_prefix.c_str(), name.c_str(), name_for_architecture(code->arch));
|
||||
|
||||
} catch (const exception& e) {
|
||||
log(WARNING, "Failed to compile function %s: %s", name.c_str(), e.what());
|
||||
function_compiler_log.warning("Failed to compile function %s: %s", name.c_str(), e.what());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -177,8 +209,10 @@ vector<MenuItem> FunctionCodeIndex::patch_menu() const {
|
||||
ret.emplace_back(PatchesMenuItemID::GO_BACK, u"Go back", u"", 0);
|
||||
for (const auto& it : this->name_to_patch_function) {
|
||||
const auto& fn = it.second;
|
||||
ret.emplace_back(fn->menu_item_id, decode_sjis(fn->name), u"",
|
||||
MenuItem::Flag::REQUIRES_SEND_FUNCTION_CALL);
|
||||
if (!fn->hide_from_patches_menu) {
|
||||
ret.emplace_back(fn->menu_item_id, decode_sjis(fn->name), u"",
|
||||
MenuItem::Flag::REQUIRES_SEND_FUNCTION_CALL);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
@@ -187,11 +221,11 @@ vector<MenuItem> FunctionCodeIndex::patch_menu() const {
|
||||
|
||||
DOLFileIndex::DOLFileIndex(const string& directory) {
|
||||
if (!function_compiler_available()) {
|
||||
log(INFO, "Function compiler is not available");
|
||||
function_compiler_log.info("Function compiler is not available");
|
||||
return;
|
||||
}
|
||||
if (!isdir(directory)) {
|
||||
log(INFO, "DOL file directory is missing");
|
||||
function_compiler_log.info("DOL file directory is missing");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -212,10 +246,10 @@ DOLFileIndex::DOLFileIndex(const string& directory) {
|
||||
|
||||
this->name_to_file.emplace(dol->name, dol);
|
||||
this->item_id_to_file.emplace_back(dol);
|
||||
log(INFO, "Loaded DOL file %s", filename.c_str());
|
||||
function_compiler_log.info("Loaded DOL file %s", filename.c_str());
|
||||
|
||||
} catch (const exception& e) {
|
||||
log(WARNING, "Failed to load DOL file %s: %s", filename.c_str(), e.what());
|
||||
function_compiler_log.warning("Failed to load DOL file %s: %s", filename.c_str(), e.what());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+22
-4
@@ -16,10 +16,16 @@ bool function_compiler_available();
|
||||
|
||||
|
||||
|
||||
// TODO: Support x86 function calls in the future. Currently we only support
|
||||
// PPC32 because I haven't written an appropriate x86 assembler yet.
|
||||
// TODO: Support x86 and SH4 function calls in the future. Currently we only
|
||||
// support PPC32 because I haven't written an appropriate x86 assembler yet.
|
||||
|
||||
struct CompiledFunctionCode {
|
||||
enum class Architecture {
|
||||
POWERPC = 0, // GC
|
||||
X86, // PC, XB, BB
|
||||
SH4, // Dreamcast
|
||||
};
|
||||
Architecture arch;
|
||||
std::string code;
|
||||
std::vector<uint16_t> relocation_deltas;
|
||||
std::unordered_map<std::string, uint32_t> label_offsets;
|
||||
@@ -27,13 +33,23 @@ struct CompiledFunctionCode {
|
||||
std::string name;
|
||||
uint32_t index; // 0 = unused (not registered in index_to_function)
|
||||
uint32_t menu_item_id;
|
||||
bool hide_from_patches_menu;
|
||||
|
||||
bool is_big_endian() const;
|
||||
|
||||
template <typename FooterT, typename U16T>
|
||||
std::string generate_client_command_t(
|
||||
const std::unordered_map<std::string, uint32_t>& label_writes,
|
||||
const std::string& suffix) const;
|
||||
std::string generate_client_command(
|
||||
const std::unordered_map<std::string, uint32_t>& label_writes = {},
|
||||
const std::string& suffix = "") const;
|
||||
};
|
||||
|
||||
const char* name_for_architecture(CompiledFunctionCode::Architecture arch);
|
||||
|
||||
std::shared_ptr<CompiledFunctionCode> compile_function_code(
|
||||
CompiledFunctionCode::Architecture arch,
|
||||
const std::string& directory,
|
||||
const std::string& name,
|
||||
const std::string& text);
|
||||
@@ -41,7 +57,8 @@ std::shared_ptr<CompiledFunctionCode> compile_function_code(
|
||||
|
||||
|
||||
struct FunctionCodeIndex {
|
||||
FunctionCodeIndex(const std::string& directory);
|
||||
FunctionCodeIndex() = default;
|
||||
explicit FunctionCodeIndex(const std::string& directory);
|
||||
|
||||
std::unordered_map<std::string, std::shared_ptr<CompiledFunctionCode>> name_to_function;
|
||||
std::unordered_map<uint32_t, std::shared_ptr<CompiledFunctionCode>> index_to_function;
|
||||
@@ -67,7 +84,8 @@ struct DOLFileIndex {
|
||||
std::vector<std::shared_ptr<DOLFile>> item_id_to_file;
|
||||
std::map<std::string, std::shared_ptr<DOLFile>> name_to_file;
|
||||
|
||||
DOLFileIndex(const std::string& directory);
|
||||
DOLFileIndex() = default;
|
||||
explicit DOLFileIndex(const std::string& directory);
|
||||
|
||||
std::vector<MenuItem> menu() const;
|
||||
inline bool empty() const {
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
#include "GSLArchive.hh"
|
||||
|
||||
#include <phosg/Encoding.hh>
|
||||
#include <phosg/Filesystem.hh>
|
||||
#include <stdexcept>
|
||||
|
||||
#include "Text.hh"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
|
||||
template <typename LongT>
|
||||
struct GSLHeaderEntry {
|
||||
ptext<char, 0x20> filename;
|
||||
LongT offset; // In pages, so actual offset is this * 0x800
|
||||
LongT size;
|
||||
uint64_t unused;
|
||||
} __attribute__((packed));
|
||||
|
||||
template <typename LongT>
|
||||
void GSLArchive::load_t() {
|
||||
StringReader r(*this->data);
|
||||
uint64_t min_data_offset = 0xFFFFFFFFFFFFFFFF;
|
||||
while (r.where() < min_data_offset) {
|
||||
const auto& entry = r.get<GSLHeaderEntry<LongT>>();
|
||||
if (entry.filename.len() == 0) {
|
||||
break;
|
||||
}
|
||||
uint64_t offset = static_cast<uint64_t>(entry.offset) * 0x800;
|
||||
if (offset + entry.size > this->data->size()) {
|
||||
throw runtime_error("GSL entry extends beyond end of data");
|
||||
}
|
||||
this->entries.emplace(entry.filename, Entry{offset, entry.size});
|
||||
}
|
||||
}
|
||||
|
||||
GSLArchive::GSLArchive(shared_ptr<const string> data, bool big_endian)
|
||||
: data(data) {
|
||||
if (big_endian) {
|
||||
this->load_t<be_uint32_t>();
|
||||
} else {
|
||||
this->load_t<le_uint32_t>();
|
||||
}
|
||||
}
|
||||
|
||||
const unordered_map<string, GSLArchive::Entry> GSLArchive::all_entries() const {
|
||||
return this->entries;
|
||||
}
|
||||
|
||||
pair<const void*, size_t> GSLArchive::get(const std::string& name) const {
|
||||
try {
|
||||
const auto& entry = this->entries.at(name);
|
||||
return make_pair(this->data->data() + entry.offset, entry.size);
|
||||
} catch (const out_of_range&) {
|
||||
throw out_of_range("GSL does not contain file: " + name);
|
||||
}
|
||||
}
|
||||
|
||||
string GSLArchive::get_copy(const string& name) const {
|
||||
try {
|
||||
const auto& entry = this->entries.at(name);
|
||||
return this->data->substr(entry.offset, entry.size);
|
||||
} catch (const out_of_range&) {
|
||||
throw out_of_range("GSL does not contain file: " + name);
|
||||
}
|
||||
}
|
||||
|
||||
StringReader GSLArchive::get_reader(const string& name) const {
|
||||
try {
|
||||
const auto& entry = this->entries.at(name);
|
||||
return StringReader(this->data->data() + entry.offset, entry.size);
|
||||
} catch (const out_of_range&) {
|
||||
throw out_of_range("GSL does not contain file: " + name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <memory>
|
||||
#include <phosg/Filesystem.hh>
|
||||
#include <phosg/Strings.hh>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
|
||||
|
||||
class GSLArchive {
|
||||
public:
|
||||
GSLArchive(std::shared_ptr<const std::string> data, bool big_endian);
|
||||
~GSLArchive() = default;
|
||||
|
||||
struct Entry {
|
||||
uint64_t offset;
|
||||
uint32_t size;
|
||||
};
|
||||
const std::unordered_map<std::string, Entry> all_entries() const;
|
||||
|
||||
std::pair<const void*, size_t> get(const std::string& name) const;
|
||||
std::string get_copy(const std::string& name) const;
|
||||
StringReader get_reader(const std::string& name) const;
|
||||
|
||||
private:
|
||||
template <typename LongT>
|
||||
void load_t();
|
||||
|
||||
std::shared_ptr<const std::string> data;
|
||||
|
||||
std::unordered_map<std::string, Entry> entries;
|
||||
};
|
||||
+84
-90
@@ -19,6 +19,7 @@
|
||||
#include <phosg/Random.hh>
|
||||
#include <phosg/Time.hh>
|
||||
|
||||
#include "Loggers.hh"
|
||||
#include "IPFrameInfo.hh"
|
||||
#include "DNSServer.hh"
|
||||
|
||||
@@ -27,7 +28,6 @@ using namespace std;
|
||||
|
||||
|
||||
static const size_t DEFAULT_RESEND_PUSH_USECS = 200000; // 200ms
|
||||
PrefixedLogger IPStackSimulator::log("[IPStackSimulator] ");
|
||||
|
||||
|
||||
|
||||
@@ -147,7 +147,18 @@ static void flush_and_free_bufferevent(struct bufferevent* bev) {
|
||||
IPStackSimulator::IPClient::TCPConnection::TCPConnection()
|
||||
: server_bev(nullptr, flush_and_free_bufferevent),
|
||||
pending_data(evbuffer_new(), evbuffer_free),
|
||||
resend_push_event(nullptr, event_free) { }
|
||||
resend_push_event(nullptr, event_free),
|
||||
awaiting_first_ack(true),
|
||||
server_addr(0),
|
||||
server_port(0),
|
||||
client_port(0),
|
||||
next_client_seq(0),
|
||||
acked_server_seq(0),
|
||||
resend_push_usecs(DEFAULT_RESEND_PUSH_USECS),
|
||||
next_push_max_frame_size(1024),
|
||||
max_frame_size(1024),
|
||||
bytes_received(0),
|
||||
bytes_sent(0) { }
|
||||
|
||||
|
||||
|
||||
@@ -161,8 +172,7 @@ void IPStackSimulator::dispatch_on_listen_accept(
|
||||
void IPStackSimulator::on_listen_accept(struct evconnlistener* listener,
|
||||
evutil_socket_t fd, struct sockaddr*, int) {
|
||||
int listen_fd = evconnlistener_get_fd(listener);
|
||||
this->log(INFO, "Client fd %d connected via fd %d",
|
||||
fd, listen_fd);
|
||||
ip_stack_simulator_log.info("Virtual network fd %d connected via fd %d", fd, listen_fd);
|
||||
|
||||
struct bufferevent *bev = bufferevent_socket_new(this->base.get(), fd,
|
||||
BEV_OPT_CLOSE_ON_FREE | BEV_OPT_DEFER_CALLBACKS);
|
||||
@@ -182,7 +192,7 @@ void IPStackSimulator::dispatch_on_listen_error(
|
||||
|
||||
void IPStackSimulator::on_listen_error(struct evconnlistener* listener) {
|
||||
int err = EVUTIL_SOCKET_ERROR();
|
||||
this->log(ERROR, "Failure on listening socket %d: %d (%s)",
|
||||
ip_stack_simulator_log.error("Failure on listening socket %d: %d (%s)",
|
||||
evconnlistener_get_fd(listener), err, evutil_socket_error_to_string(err));
|
||||
event_base_loopexit(this->base.get(), nullptr);
|
||||
}
|
||||
@@ -202,7 +212,7 @@ void IPStackSimulator::on_client_input(struct bufferevent* bev) {
|
||||
c = this->bev_to_client.at(bev);
|
||||
} catch (const out_of_range&) {
|
||||
size_t bytes = evbuffer_get_length(buf);
|
||||
this->log(ERROR, "Ignoring data received from unregistered client (0x%zX bytes)",
|
||||
ip_stack_simulator_log.warning("Ignoring data received from unregistered virtual network (0x%zX bytes)",
|
||||
bytes);
|
||||
evbuffer_drain(buf, bytes);
|
||||
return;
|
||||
@@ -222,8 +232,7 @@ void IPStackSimulator::on_client_input(struct bufferevent* bev) {
|
||||
try {
|
||||
this->on_client_frame(c, frame);
|
||||
} catch (const exception& e) {
|
||||
if (this->state->ip_stack_debug) {
|
||||
this->log(WARNING, "Failed to process client frame: %s", e.what());
|
||||
if (ip_stack_simulator_log.warning("Failed to process frame: %s", e.what())) {
|
||||
print_data(stderr, frame);
|
||||
}
|
||||
}
|
||||
@@ -238,13 +247,11 @@ void IPStackSimulator::on_client_error(struct bufferevent* bev,
|
||||
short events) {
|
||||
if (events & BEV_EVENT_ERROR) {
|
||||
int err = EVUTIL_SOCKET_ERROR();
|
||||
this->log(WARNING, "Client caused error %d (%s)", err,
|
||||
ip_stack_simulator_log.warning("Virtual network caused error %d (%s)", err,
|
||||
evutil_socket_error_to_string(err));
|
||||
}
|
||||
if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) {
|
||||
this->log(INFO, "Client fd %d disconnected",
|
||||
bufferevent_getfd(bev));
|
||||
|
||||
ip_stack_simulator_log.info("Virtual network fd %d disconnected", bufferevent_getfd(bev));
|
||||
this->bev_to_client.erase(bev);
|
||||
}
|
||||
}
|
||||
@@ -253,17 +260,16 @@ void IPStackSimulator::on_client_error(struct bufferevent* bev,
|
||||
|
||||
void IPStackSimulator::on_client_frame(
|
||||
shared_ptr<IPClient> c, const string& frame) {
|
||||
if (this->state->ip_stack_debug) {
|
||||
fputc('\n', stderr);
|
||||
this->log(INFO, "Client sent frame");
|
||||
if (ip_stack_simulator_log.info("Virtual network sent frame")) {
|
||||
print_data(stderr, frame);
|
||||
fputc('\n', stderr);
|
||||
}
|
||||
this->log_frame(frame);
|
||||
|
||||
FrameInfo fi(frame);
|
||||
if (this->state->ip_stack_debug) {
|
||||
if (ip_stack_simulator_log.should_log(LogLevel::INFO)) {
|
||||
string fi_header = fi.header_str();
|
||||
this->log(INFO, "Frame header: %s", fi_header.c_str());
|
||||
ip_stack_simulator_log.info("Frame header: %s", fi_header.c_str());
|
||||
}
|
||||
|
||||
if (fi.arp) {
|
||||
@@ -371,9 +377,7 @@ void IPStackSimulator::on_client_arp_frame(
|
||||
evbuffer_add(out_buf, &r_arp, sizeof(r_arp));
|
||||
evbuffer_add(out_buf, r_payload, sizeof(r_payload));
|
||||
|
||||
if (this->state->ip_stack_debug) {
|
||||
this->log(INFO, "Sending ARP response");
|
||||
}
|
||||
ip_stack_simulator_log.info("Sending ARP response");
|
||||
|
||||
if (this->pcap_text_log_file) {
|
||||
StringWriter w;
|
||||
@@ -432,9 +436,10 @@ void IPStackSimulator::on_client_udp_frame(
|
||||
|
||||
struct evbuffer* out_buf = bufferevent_get_output(c->bev.get());
|
||||
|
||||
if (this->state->ip_stack_debug) {
|
||||
if (ip_stack_simulator_log.should_log(LogLevel::INFO)) {
|
||||
string remote_str = this->str_for_ipv4_netloc(fi.ipv4->src_addr, fi.udp->src_port);
|
||||
this->log(INFO, "Sending DNS response to %s", remote_str.c_str());
|
||||
ip_stack_simulator_log.info("Sending DNS response to %s", remote_str.c_str());
|
||||
print_data(stderr, r_data);
|
||||
}
|
||||
|
||||
uint16_t frame_size = sizeof(r_ether) + sizeof(r_ipv4) + sizeof(r_udp) + r_data.size();
|
||||
@@ -480,10 +485,8 @@ uint64_t IPStackSimulator::tcp_conn_key_for_client_frame(const FrameInfo& fi) {
|
||||
|
||||
void IPStackSimulator::on_client_tcp_frame(
|
||||
shared_ptr<IPClient> c, const FrameInfo& fi) {
|
||||
if (this->state->ip_stack_debug) {
|
||||
this->log(INFO, "Client sent TCP frame (seq=%08" PRIX32 ", ack=%08" PRIX32 ")",
|
||||
fi.tcp->seq_num.load(), fi.tcp->ack_num.load());
|
||||
}
|
||||
ip_stack_simulator_log.info("Virtual network sent TCP frame (seq=%08" PRIX32 ", ack=%08" PRIX32 ")",
|
||||
fi.tcp->seq_num.load(), fi.tcp->ack_num.load());
|
||||
|
||||
if (fi.tcp->flags & (TCPHeader::Flag::NS | TCPHeader::Flag::CWR |
|
||||
TCPHeader::Flag::ECE | TCPHeader::Flag::URG)) {
|
||||
@@ -556,17 +559,15 @@ void IPStackSimulator::on_client_tcp_frame(
|
||||
conn.next_client_seq = fi.tcp->seq_num + 1;
|
||||
conn.acked_server_seq = random_object<uint32_t>();
|
||||
conn.resend_push_usecs = DEFAULT_RESEND_PUSH_USECS;
|
||||
conn.next_push_max_frame_size = max_frame_size;
|
||||
conn.awaiting_first_ack = true;
|
||||
conn.max_frame_size = max_frame_size;
|
||||
conn.bytes_received = 0;
|
||||
conn.bytes_sent = 0;
|
||||
|
||||
conn_str = this->str_for_tcp_connection(c, conn);
|
||||
if (this->state->ip_stack_debug) {
|
||||
this->log(INFO, "Client opened TCP connection %s (acked_server_seq=%08" PRIX32 ", next_client_seq=%08" PRIX32 ")",
|
||||
conn_str.c_str(), conn.acked_server_seq, conn.next_client_seq);
|
||||
} else {
|
||||
this->log(INFO, "Client opened TCP connection %s",
|
||||
conn_str.c_str());
|
||||
}
|
||||
ip_stack_simulator_log.info("Client opened TCP connection %s (acked_server_seq=%08" PRIX32 ", next_client_seq=%08" PRIX32 ")",
|
||||
conn_str.c_str(), conn.acked_server_seq, conn.next_client_seq);
|
||||
|
||||
} else {
|
||||
// Connection is NOT new; this is probably a resend of an earlier SYN
|
||||
@@ -576,18 +577,14 @@ void IPStackSimulator::on_client_tcp_frame(
|
||||
// TODO: We should check the syn/ack numbers here instead of just assuming
|
||||
// they're correct
|
||||
conn_str = this->str_for_tcp_connection(c, conn);
|
||||
if (this->state->ip_stack_debug) {
|
||||
this->log(INFO, "Client resent SYN for TCP connection %s",
|
||||
conn_str.c_str());
|
||||
}
|
||||
ip_stack_simulator_log.info("Client resent SYN for TCP connection %s",
|
||||
conn_str.c_str());
|
||||
}
|
||||
|
||||
// Send a SYN+ACK (send_tcp_frame always adds the ACK flag)
|
||||
this->send_tcp_frame(c, conn, TCPHeader::Flag::SYN);
|
||||
if (this->state->ip_stack_debug) {
|
||||
this->log(INFO, "Sent SYN+ACK on %s (acked_server_seq=%08" PRIX32 ", next_client_seq=%08" PRIX32 ")",
|
||||
conn_str.c_str(), conn.acked_server_seq, conn.next_client_seq);
|
||||
}
|
||||
ip_stack_simulator_log.info("Sent SYN+ACK on %s (acked_server_seq=%08" PRIX32 ", next_client_seq=%08" PRIX32 ")",
|
||||
conn_str.c_str(), conn.acked_server_seq, conn.next_client_seq);
|
||||
|
||||
} else {
|
||||
// This frame isn't a SYN, so a connection object should already exist
|
||||
@@ -601,9 +598,7 @@ void IPStackSimulator::on_client_tcp_frame(
|
||||
bool conn_valid = true;
|
||||
|
||||
if (fi.tcp->flags & TCPHeader::Flag::ACK) {
|
||||
if (this->state->ip_stack_debug) {
|
||||
this->log(INFO, "Client sent ACK %08" PRIX32, fi.tcp->ack_num.load());
|
||||
}
|
||||
ip_stack_simulator_log.info("Client sent ACK %08" PRIX32, fi.tcp->ack_num.load());
|
||||
if (conn->awaiting_first_ack) {
|
||||
if (fi.tcp->ack_num != conn->acked_server_seq + 1) {
|
||||
throw runtime_error("first ack_num was not acked_server_seq + 1");
|
||||
@@ -613,9 +608,7 @@ void IPStackSimulator::on_client_tcp_frame(
|
||||
|
||||
} else {
|
||||
if (seq_num_greater(fi.tcp->ack_num, conn->acked_server_seq)) {
|
||||
if (this->state->ip_stack_debug) {
|
||||
this->log(INFO, "Advancing acked_server_seq from %08" PRIX32, conn->acked_server_seq);
|
||||
}
|
||||
ip_stack_simulator_log.info("Advancing acked_server_seq from %08" PRIX32, conn->acked_server_seq);
|
||||
uint32_t ack_delta = fi.tcp->ack_num - conn->acked_server_seq;
|
||||
size_t pending_bytes = evbuffer_get_length(conn->pending_data.get());
|
||||
if (pending_bytes < ack_delta) {
|
||||
@@ -625,11 +618,10 @@ void IPStackSimulator::on_client_tcp_frame(
|
||||
evbuffer_drain(conn->pending_data.get(), ack_delta);
|
||||
conn->acked_server_seq += ack_delta;
|
||||
conn->resend_push_usecs = DEFAULT_RESEND_PUSH_USECS;
|
||||
conn->next_push_max_frame_size = conn->max_frame_size;
|
||||
|
||||
if (this->state->ip_stack_debug) {
|
||||
this->log(INFO, "Removed %08" PRIX32 " bytes from pending buffer and advanced acked_server_seq to %08" PRIX32,
|
||||
ack_delta, conn->acked_server_seq);
|
||||
}
|
||||
ip_stack_simulator_log.info("Removed %08" PRIX32 " bytes from pending buffer and advanced acked_server_seq to %08" PRIX32,
|
||||
ack_delta, conn->acked_server_seq);
|
||||
|
||||
} else if (seq_num_less(fi.tcp->ack_num, conn->acked_server_seq)) {
|
||||
throw runtime_error("client sent lower ack num than previous frame");
|
||||
@@ -648,7 +640,7 @@ void IPStackSimulator::on_client_tcp_frame(
|
||||
}
|
||||
|
||||
string conn_str = this->str_for_tcp_connection(c, *conn);
|
||||
this->log(INFO, "Client closed TCP connection %s", conn_str.c_str());
|
||||
ip_stack_simulator_log.info("Client closed TCP connection %s", conn_str.c_str());
|
||||
|
||||
// TODO: Are we supposed to send a response to an RST? Here we do, and the
|
||||
// client probably just ignores it anyway
|
||||
@@ -668,7 +660,8 @@ void IPStackSimulator::on_client_tcp_frame(
|
||||
// server immediately.
|
||||
} else if (fi.payload_size != 0) {
|
||||
|
||||
string conn_str = this->state->ip_stack_debug ? this->str_for_tcp_connection(c, *conn) : "";
|
||||
string conn_str = ip_stack_simulator_log.should_log(LogLevel::WARNING)
|
||||
? this->str_for_tcp_connection(c, *conn) : "";
|
||||
|
||||
size_t payload_skip_bytes;
|
||||
if (fi.tcp->seq_num == conn->next_client_seq) {
|
||||
@@ -688,11 +681,9 @@ void IPStackSimulator::on_client_tcp_frame(
|
||||
// Payload is in the future - we must have missed a data frame. We'll
|
||||
// ignore it (but warn) and send an ACK later, and the client should
|
||||
// retransmit the lost data
|
||||
if (this->state->ip_stack_debug) {
|
||||
this->log(WARNING,
|
||||
"Client sent out-of-order sequence number (expected %08" PRIX32 ", received %08" PRIX32 ", 0x%zX data bytes)",
|
||||
conn->next_client_seq, fi.tcp->seq_num.load(), fi.payload_size);
|
||||
}
|
||||
ip_stack_simulator_log.warning(
|
||||
"Client sent out-of-order sequence number (expected %08" PRIX32 ", received %08" PRIX32 ", 0x%zX data bytes)",
|
||||
conn->next_client_seq, fi.tcp->seq_num.load(), fi.payload_size);
|
||||
payload_skip_bytes = fi.payload_size;
|
||||
}
|
||||
|
||||
@@ -704,14 +695,15 @@ void IPStackSimulator::on_client_tcp_frame(
|
||||
const void* payload = reinterpret_cast<const uint8_t*>(fi.payload) + payload_skip_bytes;
|
||||
size_t payload_size = fi.payload_size - payload_skip_bytes;
|
||||
|
||||
if (this->state->ip_stack_debug) {
|
||||
if (payload_skip_bytes) {
|
||||
this->log(INFO, "Client sent data on TCP connection %s, overlapping existing ack'ed data (0x%zX bytes ignored)",
|
||||
conn_str.c_str(), payload_skip_bytes);
|
||||
} else {
|
||||
this->log(INFO, "Client sent data on TCP connection %s",
|
||||
conn_str.c_str());
|
||||
}
|
||||
bool was_logged;
|
||||
if (payload_skip_bytes) {
|
||||
was_logged = ip_stack_simulator_log.info("Client sent data on TCP connection %s, overlapping existing ack'ed data (0x%zX bytes ignored)",
|
||||
conn_str.c_str(), payload_skip_bytes);
|
||||
} else {
|
||||
was_logged = ip_stack_simulator_log.info("Client sent data on TCP connection %s",
|
||||
conn_str.c_str());
|
||||
}
|
||||
if (was_logged) {
|
||||
print_data(stderr, payload, payload_size);
|
||||
}
|
||||
|
||||
@@ -723,14 +715,16 @@ void IPStackSimulator::on_client_tcp_frame(
|
||||
// Update the sequence number and stats
|
||||
conn->next_client_seq += payload_size;
|
||||
conn->bytes_received += payload_size;
|
||||
if (conn->next_client_seq < payload_size) {
|
||||
ip_stack_simulator_log.warning("Client sequence number has wrapped (next=%08" PRIX32 ", bytes=%zX)",
|
||||
fi.tcp->seq_num.load(), payload_size);
|
||||
}
|
||||
}
|
||||
|
||||
// Send an ACK
|
||||
this->send_tcp_frame(c, *conn);
|
||||
if (this->state->ip_stack_debug) {
|
||||
this->log(INFO, "Sent PSH ACK on %s (acked_server_seq=%08" PRIX32 ", next_client_seq=%08" PRIX32 ", bytes_received=0x%zX)",
|
||||
conn_str.c_str(), conn->acked_server_seq, conn->next_client_seq, conn->bytes_received);
|
||||
}
|
||||
ip_stack_simulator_log.info("Sent PSH ACK on %s (acked_server_seq=%08" PRIX32 ", next_client_seq=%08" PRIX32 ", bytes_received=0x%zX)",
|
||||
conn_str.c_str(), conn->acked_server_seq, conn->next_client_seq, conn->bytes_received);
|
||||
}
|
||||
|
||||
if (conn_valid) {
|
||||
@@ -769,21 +763,22 @@ void IPStackSimulator::open_server_connection(
|
||||
string conn_str = this->str_for_tcp_connection(c, conn);
|
||||
if (port_config->behavior == ServerBehavior::PROXY_SERVER) {
|
||||
if (!this->state->proxy_server.get()) {
|
||||
this->log(ERROR, "TCP connection %s is to non-running proxy server",
|
||||
ip_stack_simulator_log.error("TCP connection %s is to non-running proxy server",
|
||||
conn_str.c_str());
|
||||
flush_and_free_bufferevent(bevs[1]);
|
||||
} else {
|
||||
this->state->proxy_server->connect_client(bevs[1], conn.server_port);
|
||||
this->log(INFO, "Connected TCP connection %s to proxy server",
|
||||
ip_stack_simulator_log.info("Connected TCP connection %s to proxy server",
|
||||
conn_str.c_str());
|
||||
}
|
||||
} else if (this->state->game_server.get()) {
|
||||
this->state->game_server->connect_client(bevs[1], c->ipv4_addr,
|
||||
conn.client_port, port_config->version, port_config->behavior);
|
||||
this->log(INFO, "Connected TCP connection %s to game server",
|
||||
conn.client_port, conn.server_port, port_config->version,
|
||||
port_config->behavior);
|
||||
ip_stack_simulator_log.info("Connected TCP connection %s to game server",
|
||||
conn_str.c_str());
|
||||
} else {
|
||||
this->log(ERROR, "No server available for TCP connection %s",
|
||||
ip_stack_simulator_log.error("No server available for TCP connection %s",
|
||||
conn_str.c_str());
|
||||
flush_and_free_bufferevent(bevs[1]);
|
||||
}
|
||||
@@ -796,12 +791,10 @@ void IPStackSimulator::send_pending_push_frame(
|
||||
return;
|
||||
}
|
||||
|
||||
size_t bytes_to_send = min<size_t>(pending_bytes, conn.max_frame_size);
|
||||
size_t bytes_to_send = min<size_t>(pending_bytes, conn.next_push_max_frame_size);
|
||||
|
||||
if (this->state->ip_stack_debug) {
|
||||
this->log(INFO, "Sending PSH frame with seq_num %08" PRIX32 ", 0x%zX/0x%zX data bytes",
|
||||
conn.acked_server_seq, bytes_to_send, pending_bytes);
|
||||
}
|
||||
ip_stack_simulator_log.info("Sending PSH frame with seq_num %08" PRIX32 ", 0x%zX/0x%zX data bytes",
|
||||
conn.acked_server_seq, bytes_to_send, pending_bytes);
|
||||
|
||||
this->send_tcp_frame(c, conn, TCPHeader::Flag::PSH, conn.pending_data.get(),
|
||||
bytes_to_send);
|
||||
@@ -811,11 +804,14 @@ void IPStackSimulator::send_pending_push_frame(
|
||||
// If the client isn't responding to our PSHes, back off exponentially up to
|
||||
// a limit of 5 seconds between PSH frames. This window is reset when
|
||||
// acked_server_seq changes (that is, when the client has acknowledged any new
|
||||
// data)
|
||||
// data). It seems some situations cause GameCube clients to drop packets more
|
||||
// often; to alleviate this, we also try to resend less data.
|
||||
conn.resend_push_usecs *= 2;
|
||||
if (conn.resend_push_usecs > 5000000) {
|
||||
conn.resend_push_usecs = 5000000;
|
||||
}
|
||||
conn.next_push_max_frame_size = max<size_t>(
|
||||
0x100, conn.next_push_max_frame_size - 0x100);
|
||||
}
|
||||
|
||||
void IPStackSimulator::send_tcp_frame(
|
||||
@@ -887,7 +883,7 @@ void IPStackSimulator::dispatch_on_resend_push(evutil_socket_t, short, void* ctx
|
||||
auto* conn = reinterpret_cast<IPClient::TCPConnection*>(ctx);
|
||||
auto c = conn->client.lock();
|
||||
if (!c.get()) {
|
||||
IPStackSimulator::log(WARNING, "Resend push event triggered for deleted client; ignoring");
|
||||
ip_stack_simulator_log.warning("Resend push event triggered for deleted client; ignoring");
|
||||
} else {
|
||||
c->sim->on_resend_push(c, *conn);
|
||||
}
|
||||
@@ -901,7 +897,7 @@ void IPStackSimulator::dispatch_on_server_input(struct bufferevent*, void* ctx)
|
||||
auto* conn = reinterpret_cast<IPClient::TCPConnection*>(ctx);
|
||||
auto c = conn->client.lock();
|
||||
if (!c.get()) {
|
||||
IPStackSimulator::log(WARNING, "Server input event triggered for deleted client; ignoring");
|
||||
ip_stack_simulator_log.warning("Server input event triggered for deleted client; ignoring");
|
||||
} else {
|
||||
c->sim->on_server_input(c, *conn);
|
||||
}
|
||||
@@ -909,10 +905,8 @@ void IPStackSimulator::dispatch_on_server_input(struct bufferevent*, void* ctx)
|
||||
|
||||
void IPStackSimulator::on_server_input(shared_ptr<IPClient> c, IPClient::TCPConnection& conn) {
|
||||
struct evbuffer* buf = bufferevent_get_input(conn.server_bev.get());
|
||||
if (this->state->ip_stack_debug) {
|
||||
this->log(INFO, "Server input event: 0x%zX bytes to read",
|
||||
evbuffer_get_length(buf));
|
||||
}
|
||||
ip_stack_simulator_log.info("Server input event: 0x%zX bytes to read",
|
||||
evbuffer_get_length(buf));
|
||||
|
||||
evbuffer_add_buffer(conn.pending_data.get(), buf);
|
||||
this->send_pending_push_frame(c, conn);
|
||||
@@ -923,7 +917,7 @@ void IPStackSimulator::dispatch_on_server_error(
|
||||
auto* conn = reinterpret_cast<IPClient::TCPConnection*>(ctx);
|
||||
auto c = conn->client.lock();
|
||||
if (!c.get()) {
|
||||
IPStackSimulator::log(WARNING, "Server error event triggered for deleted client; ignoring");
|
||||
ip_stack_simulator_log.warning("Server error event triggered for deleted client; ignoring");
|
||||
} else {
|
||||
c->sim->on_server_error(c, *conn, events);
|
||||
}
|
||||
@@ -933,7 +927,7 @@ void IPStackSimulator::on_server_error(
|
||||
shared_ptr<IPClient> c, IPClient::TCPConnection& conn, short events) {
|
||||
if (events & BEV_EVENT_ERROR) {
|
||||
int err = EVUTIL_SOCKET_ERROR();
|
||||
this->log(WARNING, "Received error %d from virtual connection (%s)", err,
|
||||
ip_stack_simulator_log.warning("Received error %d from virtual connection (%s)", err,
|
||||
evutil_socket_error_to_string(err));
|
||||
}
|
||||
if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) {
|
||||
@@ -945,7 +939,7 @@ void IPStackSimulator::on_server_error(
|
||||
// Delete the connection object (this also flushes and frees the server
|
||||
// virtual connection bufferevent)
|
||||
string conn_str = this->str_for_tcp_connection(c, conn);
|
||||
this->log(INFO, "Server closed TCP connection %s",
|
||||
ip_stack_simulator_log.info("Server closed TCP connection %s",
|
||||
conn_str.c_str());
|
||||
c->tcp_connections.erase(this->tcp_conn_key_for_connection(conn));
|
||||
}
|
||||
|
||||
@@ -28,7 +28,6 @@ public:
|
||||
static uint32_t connect_address_for_remote_address(uint32_t remote_addr);
|
||||
|
||||
private:
|
||||
static PrefixedLogger log;
|
||||
std::shared_ptr<struct event_base> base;
|
||||
std::shared_ptr<ServerState> state;
|
||||
|
||||
@@ -66,6 +65,7 @@ private:
|
||||
uint32_t next_client_seq;
|
||||
uint32_t acked_server_seq;
|
||||
size_t resend_push_usecs;
|
||||
size_t next_push_max_frame_size;
|
||||
size_t max_frame_size;
|
||||
size_t bytes_received;
|
||||
size_t bytes_sent;
|
||||
|
||||
+68
-51
@@ -8,6 +8,13 @@ using namespace std;
|
||||
|
||||
|
||||
|
||||
uint32_t random_int(shared_ptr<mt19937> rand, uint32_t min, uint32_t max) {
|
||||
uint32_t range = max - min + 1;
|
||||
return min + ((*rand)() % range);
|
||||
}
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/* these items all need some kind of special handling that hasn't been implemented yet.
|
||||
@@ -23,9 +30,9 @@ using namespace std;
|
||||
030D00 = Sorcerer's Right Arm
|
||||
030D01 = S-beat's Arms
|
||||
030D02 = P-arm's Arms
|
||||
030D03 = Delsabre's Right Arm
|
||||
030D03 = Delsaber's Right Arm
|
||||
030D04 = C-bringer's Right Arm
|
||||
030D05 = Delsabre's Left Arm
|
||||
030D05 = Delsaber's Left Arm
|
||||
030D06 = S-red's Arms
|
||||
030D07 = Dragon's Claw
|
||||
030D08 = Hildebear's Head
|
||||
@@ -149,7 +156,7 @@ void player_use_item(shared_ptr<Client> c, size_t item_index) {
|
||||
// ssize_t equipped_shield = -1;
|
||||
// ssize_t equipped_mag = -1;
|
||||
for (size_t y = 0; y < c->game_data.player()->inventory.num_items; y++) {
|
||||
if (c->game_data.player()->inventory.items[y].equip_flags & 0x0008) {
|
||||
if (c->game_data.player()->inventory.items[y].flags & 0x00000008) {
|
||||
if (c->game_data.player()->inventory.items[y].data.data1[0] == 0) {
|
||||
equipped_weapon = y;
|
||||
}
|
||||
@@ -165,7 +172,11 @@ void player_use_item(shared_ptr<Client> c, size_t item_index) {
|
||||
}
|
||||
}
|
||||
|
||||
bool should_delete_item = true;
|
||||
// On PC (and presumably DC), the client sends a 6x29 after this to delete the
|
||||
// used item. On GC and later versions, this does not happen, so we should
|
||||
// delete the item here.
|
||||
bool should_delete_item = (c->version() != GameVersion::DC) &&
|
||||
(c->version() != GameVersion::PC);
|
||||
|
||||
auto& item = c->game_data.player()->inventory.items[item_index];
|
||||
if (item.data.data1w[0] == 0x0203) { // technique disk
|
||||
@@ -220,20 +231,21 @@ void player_use_item(shared_ptr<Client> c, size_t item_index) {
|
||||
}
|
||||
|
||||
if (should_delete_item) {
|
||||
c->game_data.player()->remove_item(item.data.id, 1);
|
||||
// Allow overdrafting meseta if the client is not BB, since the server isn't
|
||||
// informed when meseta is added or removed from the bank.
|
||||
c->game_data.player()->remove_item(item.data.id, 1, c->version() != GameVersion::BB);
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// reads the non-rare item preferences from the config file.
|
||||
CommonItemCreator::CommonItemCreator(
|
||||
const vector<uint32_t>& enemy_item_categories,
|
||||
const vector<uint32_t>& box_item_categories,
|
||||
const vector<vector<uint8_t>>& unit_types) :
|
||||
enemy_item_categories(enemy_item_categories),
|
||||
box_item_categories(box_item_categories),
|
||||
unit_types(unit_types) {
|
||||
CommonItemData::CommonItemData(
|
||||
vector<uint32_t>&& enemy_item_categories,
|
||||
vector<uint32_t>&& box_item_categories,
|
||||
vector<vector<uint8_t>>&& unit_types) :
|
||||
enemy_item_categories(move(enemy_item_categories)),
|
||||
box_item_categories(move(box_item_categories)),
|
||||
unit_types(move(unit_types)) {
|
||||
|
||||
// sanity check the values
|
||||
if (this->enemy_item_categories.size() != 8) {
|
||||
@@ -267,16 +279,21 @@ CommonItemCreator::CommonItemCreator(
|
||||
}
|
||||
}
|
||||
|
||||
int32_t CommonItemCreator::decide_item_type(bool is_box) const {
|
||||
uint32_t determinant = random_object<uint32_t>();
|
||||
CommonItemCreator::CommonItemCreator(
|
||||
std::shared_ptr<const CommonItemData> data,
|
||||
std::shared_ptr<std::mt19937> random)
|
||||
: data(data), random(random) { }
|
||||
|
||||
const auto* v = is_box ? &this->box_item_categories : &this->enemy_item_categories;
|
||||
for (size_t x = 0; x < v->size(); x++) {
|
||||
uint32_t probability = v->at(x);
|
||||
if (probability > determinant) {
|
||||
int32_t CommonItemCreator::decide_item_type(bool is_box) const {
|
||||
uint32_t det = (*this->random)();
|
||||
|
||||
const auto& v = is_box ? this->data->box_item_categories : this->data->enemy_item_categories;
|
||||
for (size_t x = 0; x < v.size(); x++) {
|
||||
uint32_t probability = v.at(x);
|
||||
if (probability > det) {
|
||||
return x;
|
||||
}
|
||||
determinant -= probability;
|
||||
det -= probability;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
@@ -325,25 +342,25 @@ ItemData CommonItemCreator::create_drop_item(bool is_box, uint8_t episode,
|
||||
case 0x00: // material
|
||||
item.data1[0] = 0x03;
|
||||
item.data1[1] = 0x0B;
|
||||
item.data1[2] = random_int(0, 6);
|
||||
item.data1[2] = random_int(this->random, 0, 6);
|
||||
break;
|
||||
|
||||
case 0x01: // equipment
|
||||
switch (random_int(0, 3)) {
|
||||
switch (random_int(this->random, 0, 3)) {
|
||||
case 0x00: // weapon
|
||||
item.data1[1] = random_int(1, 12); // random normal class
|
||||
item.data1[2] = difficulty + random_int(0, 2); // special type
|
||||
item.data1[1] = random_int(this->random, 1, 12); // random normal class
|
||||
item.data1[2] = difficulty + random_int(this->random, 0, 2); // special type
|
||||
if ((item.data1[1] > 0x09) && (item.data1[2] > 0x04)) {
|
||||
item.data1[2] = 0x04; // no special classes above 4
|
||||
}
|
||||
item.data1[4] = 0x80; // untekked
|
||||
if (item.data1[2] < 0x04) {
|
||||
item.data1[4] |= random_int(0, 40); // give a special
|
||||
item.data1[4] |= random_int(this->random, 0, 40); // give a special
|
||||
}
|
||||
for (size_t x = 0, y = 0; (x < 5) && (y < 3); x++) { // percentages
|
||||
if (random_int(0, 10) == 1) { // 1/11 chance of getting each type of percentage
|
||||
if (random_int(this->random, 0, 10) == 1) { // 1/11 chance of getting each type of percentage
|
||||
item.data1[6 + (y * 2)] = x + 1;
|
||||
item.data1[7 + (y * 2)] = random_int(0, 10) * 5;
|
||||
item.data1[7 + (y * 2)] = random_int(this->random, 0, 10) * 5;
|
||||
y++;
|
||||
}
|
||||
}
|
||||
@@ -352,33 +369,33 @@ ItemData CommonItemCreator::create_drop_item(bool is_box, uint8_t episode,
|
||||
case 0x01: // armor
|
||||
item.data1[0] = 0x01;
|
||||
item.data1[1] = 0x01;
|
||||
item.data1[2] = (6 * difficulty) + random_int(0, ((area / 2) + 2) - 1); // standard type based on difficulty and area
|
||||
item.data1[2] = (6 * difficulty) + random_int(this->random, 0, ((area / 2) + 2) - 1); // standard type based on difficulty and area
|
||||
if (item.data1[2] > 0x17) {
|
||||
item.data1[2] = 0x17; // no standard types above 0x17
|
||||
}
|
||||
if (random_int(0, 10) == 0) { // +/-
|
||||
item.data1[4] = random_int(0, 5);
|
||||
item.data1[6] = random_int(0, 2);
|
||||
if (random_int(this->random, 0, 10) == 0) { // +/-
|
||||
item.data1[4] = random_int(this->random, 0, 5);
|
||||
item.data1[6] = random_int(this->random, 0, 2);
|
||||
}
|
||||
item.data1[5] = random_int(0, 4); // slots
|
||||
item.data1[5] = random_int(this->random, 0, 4); // slots
|
||||
break;
|
||||
|
||||
case 0x02: // shield
|
||||
item.data1[0] = 0x01;
|
||||
item.data1[1] = 0x02;
|
||||
item.data1[2] = (5 * difficulty) + random_int(0, ((area / 2) + 2) - 1); // standard type based on difficulty and area
|
||||
item.data1[2] = (5 * difficulty) + random_int(this->random, 0, ((area / 2) + 2) - 1); // standard type based on difficulty and area
|
||||
if (item.data1[2] > 0x14) {
|
||||
item.data1[2] = 0x14; // no standard types above 0x14
|
||||
}
|
||||
if (random_int(0, 10) == 0) { // +/-
|
||||
item.data1[4] = random_int(0, 5);
|
||||
item.data1[6] = random_int(0, 5);
|
||||
if (random_int(this->random, 0, 10) == 0) { // +/-
|
||||
item.data1[4] = random_int(this->random, 0, 5);
|
||||
item.data1[6] = random_int(this->random, 0, 5);
|
||||
}
|
||||
break;
|
||||
|
||||
case 0x03: { // unit
|
||||
const auto& type_table = this->unit_types.at(difficulty);
|
||||
uint8_t type = type_table[random_int(0, type_table.size() - 1)];
|
||||
const auto& type_table = this->data->unit_types.at(difficulty);
|
||||
uint8_t type = type_table[random_int(this->random, 0, type_table.size() - 1)];
|
||||
if (type == 0xFF) {
|
||||
throw out_of_range("no item dropped"); // 0xFF -> no item drops
|
||||
}
|
||||
@@ -393,11 +410,11 @@ ItemData CommonItemCreator::create_drop_item(bool is_box, uint8_t episode,
|
||||
case 0x02: // technique
|
||||
item.data1[0] = 0x03;
|
||||
item.data1[1] = 0x02;
|
||||
item.data1[4] = random_int(0, 18); // tech type
|
||||
item.data1[4] = random_int(this->random, 0, 18); // tech type
|
||||
if ((item.data1[4] != 14) && (item.data1[4] != 17)) { // if not ryuker or reverser, give it a level
|
||||
if (item.data1[4] == 16) { // if not anti, give it a level between 1 and 30
|
||||
if (area > 3) {
|
||||
item.data1[2] = difficulty + random_int(0, ((area - 1) / 2) - 1);
|
||||
item.data1[2] = difficulty + random_int(this->random, 0, ((area - 1) / 2) - 1);
|
||||
} else {
|
||||
item.data1[2] = difficulty;
|
||||
}
|
||||
@@ -405,7 +422,7 @@ ItemData CommonItemCreator::create_drop_item(bool is_box, uint8_t episode,
|
||||
item.data1[2] = 6;
|
||||
}
|
||||
} else {
|
||||
item.data1[2] = (5 * difficulty) + random_int(0, ((area * 3) / 2) - 1); // else between 1 and 7
|
||||
item.data1[2] = (5 * difficulty) + random_int(this->random, 0, ((area * 3) / 2) - 1); // else between 1 and 7
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -419,24 +436,24 @@ ItemData CommonItemCreator::create_drop_item(bool is_box, uint8_t episode,
|
||||
case 0x04: // grinder
|
||||
item.data1[0] = 0x03;
|
||||
item.data1[1] = 0x0A;
|
||||
item.data1[2] = random_int(0, 2); // mono, di, tri
|
||||
item.data1[2] = random_int(this->random, 0, 2); // mono, di, tri
|
||||
break;
|
||||
|
||||
case 0x05: // consumable
|
||||
item.data1[0] = 0x03;
|
||||
item.data1[5] = 0x01;
|
||||
switch (random_int(0, 2)) {
|
||||
switch (random_int(this->random, 0, 2)) {
|
||||
case 0: // antidote / antiparalysis
|
||||
item.data1[1] = 6;
|
||||
item.data1[2] = random_int(0, 1);
|
||||
item.data1[2] = random_int(this->random, 0, 1);
|
||||
break;
|
||||
|
||||
case 1: // telepipe / trap vision
|
||||
item.data1[1] = 7 + random_int(0, 1);
|
||||
item.data1[1] = 7 + random_int(this->random, 0, 1);
|
||||
break;
|
||||
|
||||
case 2: // sol / moon / star atomizer
|
||||
item.data1[1] = 3 + random_int(0, 2);
|
||||
item.data1[1] = 3 + random_int(this->random, 0, 2);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
@@ -444,19 +461,19 @@ ItemData CommonItemCreator::create_drop_item(bool is_box, uint8_t episode,
|
||||
case 0x06: // consumable
|
||||
item.data1[0] = 0x03;
|
||||
item.data1[5] = 0x01;
|
||||
item.data1[1] = random_int(0, 1); // mate or fluid
|
||||
item.data1[1] = random_int(this->random, 0, 1); // mate or fluid
|
||||
if (difficulty == 0) {
|
||||
item.data1[2] = random_int(0, 1); // only mono and di on normal
|
||||
item.data1[2] = random_int(this->random, 0, 1); // only mono and di on normal
|
||||
} else if (difficulty == 3) {
|
||||
item.data1[2] = random_int(1, 2); // only di and tri on ultimate
|
||||
item.data1[2] = random_int(this->random, 1, 2); // only di and tri on ultimate
|
||||
} else {
|
||||
item.data1[2] = random_int(0, 2); // else, any of the three
|
||||
item.data1[2] = random_int(this->random, 0, 2); // else, any of the three
|
||||
}
|
||||
break;
|
||||
|
||||
case 0x07: // meseta
|
||||
item.data1[0] = 0x04;
|
||||
item.data2d = (90 * difficulty) + (random_int(1, 20) * (area * 2)); // meseta amount
|
||||
item.data2d = (90 * difficulty) + (random_int(this->random, 1, 20) * (area * 2)); // meseta amount
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
+17
-5
@@ -3,20 +3,32 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#include <memory>
|
||||
#include <random>
|
||||
|
||||
#include "Lobby.hh"
|
||||
#include "Client.hh"
|
||||
|
||||
|
||||
|
||||
void player_use_item(std::shared_ptr<Client> c, size_t item_index);
|
||||
|
||||
struct CommonItemCreator {
|
||||
struct CommonItemData {
|
||||
std::vector<uint32_t> enemy_item_categories;
|
||||
std::vector<uint32_t> box_item_categories;
|
||||
std::vector<std::vector<uint8_t>> unit_types;
|
||||
|
||||
CommonItemCreator(const std::vector<uint32_t>& enemy_item_categories,
|
||||
const std::vector<uint32_t>& box_item_categories,
|
||||
const std::vector<std::vector<uint8_t>>& unit_types);
|
||||
CommonItemData(
|
||||
std::vector<uint32_t>&& enemy_item_categories,
|
||||
std::vector<uint32_t>&& box_item_categories,
|
||||
std::vector<std::vector<uint8_t>>&& unit_types);
|
||||
};
|
||||
|
||||
struct CommonItemCreator {
|
||||
std::shared_ptr<const CommonItemData> data;
|
||||
std::shared_ptr<std::mt19937> random;
|
||||
|
||||
CommonItemCreator(
|
||||
std::shared_ptr<const CommonItemData> data,
|
||||
std::shared_ptr<std::mt19937> random);
|
||||
|
||||
int32_t decide_item_type(bool is_box) const;
|
||||
ItemData create_drop_item(bool is_box, uint8_t episode, uint8_t difficulty,
|
||||
|
||||
+11
-12
@@ -10,28 +10,27 @@ using namespace std;
|
||||
|
||||
|
||||
|
||||
LevelTable::LevelTable(const string& filename, bool compressed) {
|
||||
|
||||
string data = load_file(filename);
|
||||
LevelTable::LevelTable(shared_ptr<const string> data, bool compressed) {
|
||||
if (compressed) {
|
||||
data = prs_decompress(data);
|
||||
this->data.reset(new string(prs_decompress(*data)));
|
||||
} else {
|
||||
this->data = data;
|
||||
}
|
||||
|
||||
if (data.size() < sizeof(*this)) {
|
||||
if (this->data->size() < sizeof(Table)) {
|
||||
throw invalid_argument("level table size is incorrect");
|
||||
}
|
||||
|
||||
memcpy(this, data.data(), sizeof(*this));
|
||||
this->table = reinterpret_cast<const Table*>(this->data->data());
|
||||
}
|
||||
|
||||
const PlayerStats& LevelTable::base_stats_for_class(uint8_t char_class) const {
|
||||
if (char_class >= 12) {
|
||||
throw out_of_range("invalid character class");
|
||||
}
|
||||
return this->base_stats[char_class];
|
||||
return this->table->base_stats[char_class];
|
||||
}
|
||||
|
||||
const LevelStats& LevelTable::stats_for_level(uint8_t char_class,
|
||||
const LevelTable::LevelStats& LevelTable::stats_for_level(uint8_t char_class,
|
||||
uint8_t level) const {
|
||||
if (char_class >= 12) {
|
||||
throw invalid_argument("invalid character class");
|
||||
@@ -39,15 +38,15 @@ const PlayerStats& LevelTable::base_stats_for_class(uint8_t char_class) const {
|
||||
if (level >= 200) {
|
||||
throw invalid_argument("invalid character level");
|
||||
}
|
||||
return this->levels[char_class][level];
|
||||
return this->table->levels[char_class][level];
|
||||
}
|
||||
|
||||
// Levels up a character by adding the level-up bonuses to the player's stats.
|
||||
void LevelStats::apply(PlayerStats& ps) const {
|
||||
void LevelTable::LevelStats::apply(PlayerStats& ps) const {
|
||||
ps.ata += this->ata;
|
||||
ps.atp += this->atp;
|
||||
ps.dfp += this->dfp;
|
||||
ps.evp += this->evp;
|
||||
ps.hp += this->hp;
|
||||
ps.mst += this->mst;
|
||||
ps.lck += this->lck;
|
||||
}
|
||||
|
||||
+26
-19
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <phosg/Encoding.hh>
|
||||
|
||||
@@ -19,28 +20,34 @@ struct PlayerStats {
|
||||
PlayerStats() noexcept;
|
||||
} __attribute__((packed));
|
||||
|
||||
// information on a single level for a single class
|
||||
struct LevelStats {
|
||||
uint8_t atp; // atp to add on level up
|
||||
uint8_t mst; // mst to add on level up
|
||||
uint8_t evp; // evp to add on level up
|
||||
uint8_t hp; // hp to add on level up
|
||||
uint8_t dfp; // dfp to add on level up
|
||||
uint8_t ata; // ata to add on level up
|
||||
uint8_t unknown[2];
|
||||
le_uint32_t experience; // EXP value of this level
|
||||
class LevelTable { // from PlyLevelTbl.prs
|
||||
public:
|
||||
struct LevelStats {
|
||||
uint8_t atp;
|
||||
uint8_t mst;
|
||||
uint8_t evp;
|
||||
uint8_t hp;
|
||||
uint8_t dfp;
|
||||
uint8_t ata;
|
||||
uint8_t lck;
|
||||
uint8_t tp;
|
||||
le_uint32_t experience;
|
||||
|
||||
void apply(PlayerStats& ps) const;
|
||||
} __attribute__((packed));
|
||||
void apply(PlayerStats& ps) const;
|
||||
} __attribute__((packed));
|
||||
|
||||
// level table format (PlyLevelTbl.prs)
|
||||
struct LevelTable {
|
||||
PlayerStats base_stats[12];
|
||||
le_uint32_t unknown[12];
|
||||
LevelStats levels[12][200];
|
||||
struct Table {
|
||||
PlayerStats base_stats[12];
|
||||
le_uint32_t unknown[12];
|
||||
LevelStats levels[12][200];
|
||||
} __attribute__((packed));
|
||||
|
||||
LevelTable(const std::string& filename, bool compressed);
|
||||
LevelTable(std::shared_ptr<const std::string> data, bool compressed);
|
||||
|
||||
const PlayerStats& base_stats_for_class(uint8_t char_class) const;
|
||||
const LevelStats& stats_for_level(uint8_t char_class, uint8_t level) const;
|
||||
} __attribute__((packed));
|
||||
|
||||
private:
|
||||
std::shared_ptr<const std::string> data;
|
||||
const Table* table;
|
||||
};
|
||||
|
||||
+79
-39
@@ -5,6 +5,7 @@
|
||||
#include <phosg/Filesystem.hh>
|
||||
#include <phosg/Time.hh>
|
||||
|
||||
#include "Loggers.hh"
|
||||
#include "License.hh"
|
||||
|
||||
using namespace std;
|
||||
@@ -40,7 +41,10 @@ string License::str() const {
|
||||
|
||||
|
||||
|
||||
LicenseManager::LicenseManager(const string& filename) : filename(filename) {
|
||||
LicenseManager::LicenseManager() : filename(""), autosave(false) { }
|
||||
|
||||
LicenseManager::LicenseManager(const string& filename)
|
||||
: filename(filename), autosave(true) {
|
||||
try {
|
||||
auto licenses = load_vector_file<License>(this->filename);
|
||||
for (const auto& read_license : licenses) {
|
||||
@@ -57,12 +61,15 @@ LicenseManager::LicenseManager(const string& filename) : filename(filename) {
|
||||
}
|
||||
|
||||
} catch (const cannot_open_file&) {
|
||||
log(WARNING, "File %s does not exist; no licenses are registered",
|
||||
license_log.warning("File %s does not exist; no licenses are registered",
|
||||
this->filename.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void LicenseManager::save() const {
|
||||
if (this->filename.empty()) {
|
||||
throw logic_error("license manager has no filename; cannot save");
|
||||
}
|
||||
auto f = fopen_unique(this->filename, "wb");
|
||||
for (const auto& it : this->serial_number_to_license) {
|
||||
if (it.second->privileges & Privilege::TEMPORARY) {
|
||||
@@ -72,57 +79,77 @@ void LicenseManager::save() const {
|
||||
}
|
||||
}
|
||||
|
||||
void LicenseManager::set_autosave(bool autosave) {
|
||||
this->autosave = autosave;
|
||||
}
|
||||
|
||||
shared_ptr<const License> LicenseManager::verify_pc(uint32_t serial_number,
|
||||
const string& access_key) const {
|
||||
auto& license = this->serial_number_to_license.at(serial_number);
|
||||
if (!license->access_key.eq_n(access_key, 8)) {
|
||||
throw invalid_argument("incorrect access key");
|
||||
}
|
||||
try {
|
||||
auto& license = this->serial_number_to_license.at(serial_number);
|
||||
if (!license->access_key.eq_n(access_key, 8)) {
|
||||
throw incorrect_access_key();
|
||||
}
|
||||
|
||||
if (license->ban_end_time && (license->ban_end_time >= now())) {
|
||||
throw invalid_argument("user is banned");
|
||||
if (license->ban_end_time && (license->ban_end_time >= now())) {
|
||||
throw invalid_argument("user is banned");
|
||||
}
|
||||
return license;
|
||||
} catch (const out_of_range&) {
|
||||
throw missing_license();
|
||||
}
|
||||
return license;
|
||||
}
|
||||
|
||||
shared_ptr<const License> LicenseManager::verify_gc(uint32_t serial_number,
|
||||
const string& access_key) const {
|
||||
auto& license = this->serial_number_to_license.at(serial_number);
|
||||
if (!license->access_key.eq_n(access_key, 12)) {
|
||||
throw invalid_argument("incorrect access key");
|
||||
try {
|
||||
auto& license = this->serial_number_to_license.at(serial_number);
|
||||
if (!license->access_key.eq_n(access_key, 12)) {
|
||||
throw incorrect_access_key();
|
||||
}
|
||||
if (license->ban_end_time && (license->ban_end_time >= now())) {
|
||||
throw invalid_argument("user is banned");
|
||||
}
|
||||
return license;
|
||||
} catch (const out_of_range&) {
|
||||
throw missing_license();
|
||||
}
|
||||
if (license->ban_end_time && (license->ban_end_time >= now())) {
|
||||
throw invalid_argument("user is banned");
|
||||
}
|
||||
return license;
|
||||
}
|
||||
|
||||
shared_ptr<const License> LicenseManager::verify_gc(uint32_t serial_number,
|
||||
const string& access_key, const string& password) const {
|
||||
auto& license = this->serial_number_to_license.at(serial_number);
|
||||
if (!license->access_key.eq_n(access_key, 12)) {
|
||||
throw invalid_argument("incorrect access key");
|
||||
try {
|
||||
auto& license = this->serial_number_to_license.at(serial_number);
|
||||
if (!license->access_key.eq_n(access_key, 12)) {
|
||||
throw incorrect_access_key();
|
||||
}
|
||||
if (license->gc_password != password) {
|
||||
throw incorrect_password();
|
||||
}
|
||||
if (license->ban_end_time && (license->ban_end_time >= now())) {
|
||||
throw invalid_argument("user is banned");
|
||||
}
|
||||
return license;
|
||||
} catch (const out_of_range&) {
|
||||
throw missing_license();
|
||||
}
|
||||
if (license->gc_password != password) {
|
||||
throw invalid_argument("incorrect password");
|
||||
}
|
||||
if (license->ban_end_time && (license->ban_end_time >= now())) {
|
||||
throw invalid_argument("user is banned");
|
||||
}
|
||||
return license;
|
||||
}
|
||||
|
||||
shared_ptr<const License> LicenseManager::verify_bb(const string& username,
|
||||
const string& password) const {
|
||||
auto& license = this->bb_username_to_license.at(username);
|
||||
if (license->bb_password != password) {
|
||||
throw invalid_argument("incorrect password");
|
||||
}
|
||||
try {
|
||||
auto& license = this->bb_username_to_license.at(username);
|
||||
if (license->bb_password != password) {
|
||||
throw incorrect_password();
|
||||
}
|
||||
|
||||
if (license->ban_end_time && (license->ban_end_time >= now())) {
|
||||
throw invalid_argument("user is banned");
|
||||
if (license->ban_end_time && (license->ban_end_time >= now())) {
|
||||
throw invalid_argument("user is banned");
|
||||
}
|
||||
return license;
|
||||
} catch (const out_of_range&) {
|
||||
throw missing_license();
|
||||
}
|
||||
return license;
|
||||
}
|
||||
|
||||
size_t LicenseManager::count() const {
|
||||
@@ -131,16 +158,27 @@ size_t LicenseManager::count() const {
|
||||
|
||||
void LicenseManager::ban_until(uint32_t serial_number, uint64_t end_time) {
|
||||
this->serial_number_to_license.at(serial_number)->ban_end_time = end_time;
|
||||
this->save();
|
||||
if (this->autosave) {
|
||||
this->save();
|
||||
}
|
||||
}
|
||||
|
||||
shared_ptr<const License> LicenseManager::get(uint32_t serial_number) const {
|
||||
try {
|
||||
return this->serial_number_to_license.at(serial_number);
|
||||
} catch (const out_of_range&) {
|
||||
throw missing_license();
|
||||
}
|
||||
}
|
||||
|
||||
void LicenseManager::add(shared_ptr<License> l) {
|
||||
uint32_t serial_number = l->serial_number;
|
||||
this->serial_number_to_license.emplace(serial_number, l);
|
||||
this->serial_number_to_license[l->serial_number] = l;
|
||||
if (!l->username.empty()) {
|
||||
this->bb_username_to_license.emplace(l->username, l);
|
||||
this->bb_username_to_license[l->username] = l;
|
||||
}
|
||||
if (this->autosave) {
|
||||
this->save();
|
||||
}
|
||||
this->save();
|
||||
}
|
||||
|
||||
void LicenseManager::remove(uint32_t serial_number) {
|
||||
@@ -149,7 +187,9 @@ void LicenseManager::remove(uint32_t serial_number) {
|
||||
if (!l->username.empty()) {
|
||||
this->bb_username_to_license.erase(l->username);
|
||||
}
|
||||
this->save();
|
||||
if (this->autosave) {
|
||||
this->save();
|
||||
}
|
||||
}
|
||||
|
||||
vector<License> LicenseManager::snapshot() const {
|
||||
|
||||
+25
-3
@@ -17,6 +17,8 @@ enum Privilege {
|
||||
FREE_JOIN_GAMES = 0x00000040,
|
||||
UNLOCK_GAMES = 0x00000080,
|
||||
|
||||
DEBUG = 0x01000000,
|
||||
|
||||
MODERATOR = 0x00000007,
|
||||
ADMINISTRATOR = 0x0000003F,
|
||||
ROOT = 0x7FFFFFFF,
|
||||
@@ -44,11 +46,30 @@ struct License {
|
||||
std::string str() const;
|
||||
} __attribute__((packed));
|
||||
|
||||
class incorrect_password : public std::invalid_argument {
|
||||
public:
|
||||
incorrect_password() : invalid_argument("incorrect password") { }
|
||||
};
|
||||
|
||||
class incorrect_access_key : public std::invalid_argument {
|
||||
public:
|
||||
incorrect_access_key() : invalid_argument("incorrect access key") { }
|
||||
};
|
||||
|
||||
class missing_license : public std::invalid_argument {
|
||||
public:
|
||||
missing_license() : invalid_argument("missing license") { }
|
||||
};
|
||||
|
||||
class LicenseManager {
|
||||
public:
|
||||
LicenseManager(const std::string& filename);
|
||||
LicenseManager();
|
||||
explicit LicenseManager(const std::string& filename);
|
||||
~LicenseManager() = default;
|
||||
|
||||
void save() const;
|
||||
void set_autosave(bool autosave);
|
||||
|
||||
std::shared_ptr<const License> verify_pc(uint32_t serial_number,
|
||||
const std::string& access_key) const;
|
||||
std::shared_ptr<const License> verify_gc(uint32_t serial_number,
|
||||
@@ -61,6 +82,7 @@ public:
|
||||
|
||||
size_t count() const;
|
||||
|
||||
std::shared_ptr<const License> get(uint32_t serial_number) const;
|
||||
void add(std::shared_ptr<License> l);
|
||||
void remove(uint32_t serial_number);
|
||||
std::vector<License> snapshot() const;
|
||||
@@ -75,9 +97,9 @@ public:
|
||||
const std::string& password, bool temporary);
|
||||
|
||||
protected:
|
||||
void save() const;
|
||||
|
||||
std::string filename;
|
||||
bool autosave;
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
+105
-24
@@ -4,6 +4,7 @@
|
||||
|
||||
#include <phosg/Random.hh>
|
||||
|
||||
#include "Loggers.hh"
|
||||
#include "SendCommands.hh"
|
||||
#include "Text.hh"
|
||||
|
||||
@@ -11,15 +12,27 @@ using namespace std;
|
||||
|
||||
|
||||
|
||||
Lobby::Lobby() : lobby_id(0), min_level(0), max_level(0xFFFFFFFF),
|
||||
next_game_item_id(0x00810000), version(GameVersion::GC), section_id(0),
|
||||
episode(1), difficulty(0), mode(0), rare_seed(random_object<uint32_t>()),
|
||||
event(0), block(0), type(0), leader_id(0), max_clients(12), flags(0) {
|
||||
|
||||
Lobby::Lobby(uint32_t id)
|
||||
: log(string_printf("[Lobby/%" PRIX32 "] ", id), lobby_log.min_level),
|
||||
lobby_id(id),
|
||||
min_level(0),
|
||||
max_level(0xFFFFFFFF),
|
||||
next_game_item_id(0x00810000),
|
||||
version(GameVersion::GC),
|
||||
section_id(0),
|
||||
episode(1),
|
||||
difficulty(0),
|
||||
random_seed(random_object<uint32_t>()),
|
||||
random(new mt19937(this->random_seed)),
|
||||
event(0),
|
||||
block(0),
|
||||
type(0),
|
||||
leader_id(0),
|
||||
max_clients(12),
|
||||
flags(0) {
|
||||
for (size_t x = 0; x < 12; x++) {
|
||||
this->next_item_id[x] = 0x00010000 + 0x00200000 * x;
|
||||
}
|
||||
this->next_drop_item = PlayerInventoryItem();
|
||||
}
|
||||
|
||||
void Lobby::reassign_leader_on_client_departure(size_t leaving_client_index) {
|
||||
@@ -57,20 +70,29 @@ size_t Lobby::count_clients() const {
|
||||
return ret;
|
||||
}
|
||||
|
||||
void Lobby::add_client(shared_ptr<Client> c) {
|
||||
void Lobby::add_client(shared_ptr<Client> c, ssize_t required_client_id) {
|
||||
ssize_t index;
|
||||
if (c->prefer_high_lobby_client_id) {
|
||||
for (index = max_clients - 1; index >= 0; index--) {
|
||||
ssize_t min_client_id = (this->flags & Lobby::Flag::IS_SPECTATOR_TEAM) ? 4 : 0;
|
||||
|
||||
if (required_client_id >= 0) {
|
||||
if (this->clients[required_client_id].get()) {
|
||||
throw out_of_range("required slot is in use");
|
||||
}
|
||||
this->clients[required_client_id] = c;
|
||||
index = required_client_id;
|
||||
|
||||
} else if (c->options.prefer_high_lobby_client_id) {
|
||||
for (index = max_clients - 1; index >= min_client_id; index--) {
|
||||
if (!this->clients[index].get()) {
|
||||
this->clients[index] = c;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (index < 0) {
|
||||
if (index < min_client_id) {
|
||||
throw out_of_range("no space left in lobby");
|
||||
}
|
||||
} else {
|
||||
for (index = 0; index < max_clients; index++) {
|
||||
for (index = min_client_id; index < max_clients; index++) {
|
||||
if (!this->clients[index].get()) {
|
||||
this->clients[index] = c;
|
||||
break;
|
||||
@@ -85,16 +107,15 @@ void Lobby::add_client(shared_ptr<Client> c) {
|
||||
c->lobby_id = this->lobby_id;
|
||||
|
||||
// If there's no one else in the lobby, set the leader id as well
|
||||
if (index == (max_clients - 1) * c->prefer_high_lobby_client_id) {
|
||||
for (index = 0; index < max_clients; index++) {
|
||||
if (this->clients[index].get() && this->clients[index] != c) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (index >= max_clients) {
|
||||
this->leader_id = c->lobby_client_id;
|
||||
size_t leader_index;
|
||||
for (leader_index = 0; leader_index < max_clients; leader_index++) {
|
||||
if (this->clients[leader_index] && (this->clients[leader_index] != c)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (leader_index >= max_clients) {
|
||||
this->leader_id = c->lobby_client_id;
|
||||
}
|
||||
|
||||
// If the lobby is a game and item tracking is enabled, assign the inventory's
|
||||
// item IDs
|
||||
@@ -106,6 +127,29 @@ void Lobby::add_client(shared_ptr<Client> c) {
|
||||
}
|
||||
c->game_data.player()->print_inventory(stderr);
|
||||
}
|
||||
|
||||
// If the lobby is recording a battle record, add the player join event
|
||||
if (this->battle_record) {
|
||||
PlayerLobbyDataDCGC lobby_data;
|
||||
lobby_data.player_tag = 0x00010000;
|
||||
lobby_data.guild_card = c->license->serial_number;
|
||||
lobby_data.name = encode_sjis(c->game_data.player()->disp.name);
|
||||
this->battle_record->add_player(lobby_data,
|
||||
c->game_data.player()->inventory,
|
||||
c->game_data.player()->disp.to_dcpcv3());
|
||||
}
|
||||
|
||||
// Send spectator count notifications if needed
|
||||
if (this->is_game() && (this->flags & Lobby::Flag::EPISODE_3_ONLY)) {
|
||||
if (this->flags & Lobby::Flag::IS_SPECTATOR_TEAM) {
|
||||
auto watched_l = this->watched_lobby.lock();
|
||||
if (watched_l) {
|
||||
send_ep3_update_spectator_count(watched_l);
|
||||
}
|
||||
} else {
|
||||
send_ep3_update_spectator_count(this->shared_from_this());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Lobby::remove_client(shared_ptr<Client> c) {
|
||||
@@ -127,20 +171,47 @@ void Lobby::remove_client(shared_ptr<Client> c) {
|
||||
}
|
||||
|
||||
this->reassign_leader_on_client_departure(c->lobby_client_id);
|
||||
|
||||
// If the lobby is recording a battle record, add the player leave event
|
||||
if (this->battle_record) {
|
||||
this->battle_record->delete_player(c->lobby_client_id);
|
||||
}
|
||||
|
||||
// If the lobby is Episode 3, update the appropriate spectator counts
|
||||
if (this->is_game() && (this->flags & Lobby::Flag::EPISODE_3_ONLY)) {
|
||||
if (this->flags & Lobby::Flag::IS_SPECTATOR_TEAM) {
|
||||
auto watched_l = this->watched_lobby.lock();
|
||||
if (watched_l) {
|
||||
send_ep3_update_spectator_count(watched_l);
|
||||
}
|
||||
} else {
|
||||
send_ep3_update_spectator_count(this->shared_from_this());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Lobby::move_client_to_lobby(shared_ptr<Lobby> dest_lobby,
|
||||
shared_ptr<Client> c) {
|
||||
void Lobby::move_client_to_lobby(
|
||||
shared_ptr<Lobby> dest_lobby,
|
||||
shared_ptr<Client> c,
|
||||
ssize_t required_client_id) {
|
||||
if (dest_lobby.get() == this) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (dest_lobby->count_clients() >= dest_lobby->max_clients) {
|
||||
throw out_of_range("no space left in lobby");
|
||||
if (required_client_id >= 0) {
|
||||
if (dest_lobby->clients[required_client_id]) {
|
||||
throw out_of_range("required slot is in use");
|
||||
}
|
||||
} else {
|
||||
ssize_t min_client_id = (this->flags & Lobby::Flag::IS_SPECTATOR_TEAM) ? 4 : 0;
|
||||
size_t available_slots = dest_lobby->max_clients - min_client_id;
|
||||
if (dest_lobby->count_clients() >= available_slots) {
|
||||
throw out_of_range("no space left in lobby");
|
||||
}
|
||||
}
|
||||
|
||||
this->remove_client(c);
|
||||
dest_lobby->add_client(c);
|
||||
dest_lobby->add_client(c, required_client_id);
|
||||
}
|
||||
|
||||
|
||||
@@ -204,3 +275,13 @@ uint32_t Lobby::generate_item_id(uint8_t client_id) {
|
||||
}
|
||||
return this->next_game_item_id++;
|
||||
}
|
||||
|
||||
unordered_map<uint32_t, shared_ptr<Client>> Lobby::clients_by_serial_number() const {
|
||||
unordered_map<uint32_t, shared_ptr<Client>> ret;
|
||||
for (auto c : this->clients) {
|
||||
if (c) {
|
||||
ret.emplace(c->license->serial_number, c);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
+59
-22
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
#include <random>
|
||||
#include <array>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
@@ -15,30 +16,43 @@
|
||||
#include "RareItemSet.hh"
|
||||
#include "Text.hh"
|
||||
#include "Quest.hh"
|
||||
#include "Items.hh"
|
||||
#include "Episode3/BattleRecord.hh"
|
||||
#include "Episode3/Server.hh"
|
||||
|
||||
struct Lobby {
|
||||
struct Lobby : public std::enable_shared_from_this<Lobby> {
|
||||
enum Flag {
|
||||
GAME = 0x00000001,
|
||||
EPISODE_3_ONLY = 0x00000002,
|
||||
GAME = 0x00000001,
|
||||
EPISODE_3_ONLY = 0x00000002,
|
||||
NON_V1_ONLY = 0x00000004, // DC NTE and DCv1 not allowed
|
||||
PERSISTENT = 0x00000008,
|
||||
|
||||
// Flags used only for games
|
||||
CHEATS_ENABLED = 0x00000100,
|
||||
QUEST_IN_PROGRESS = 0x00000200,
|
||||
JOINABLE_QUEST_IN_PROGRESS = 0x00000400,
|
||||
ITEM_TRACKING_ENABLED = 0x00000800,
|
||||
CHEATS_ENABLED = 0x00000100,
|
||||
QUEST_IN_PROGRESS = 0x00000200,
|
||||
BATTLE_IN_PROGRESS = 0x00000400,
|
||||
JOINABLE_QUEST_IN_PROGRESS = 0x00000800,
|
||||
ITEM_TRACKING_ENABLED = 0x00001000,
|
||||
IS_SPECTATOR_TEAM = 0x00002000, // EPISODE_3_ONLY must also be set
|
||||
SPECTATORS_FORBIDDEN = 0x00004000,
|
||||
BATTLE_MODE = 0x00008000,
|
||||
CHALLENGE_MODE = 0x00010000,
|
||||
SOLO_MODE = 0x00020000,
|
||||
START_BATTLE_PLAYER_IMMEDIATELY = 0x00040000,
|
||||
|
||||
// Flags used only for lobbies
|
||||
PUBLIC = 0x00010000,
|
||||
DEFAULT = 0x00020000,
|
||||
PERSISTENT = 0x00040000,
|
||||
PUBLIC = 0x01000000,
|
||||
DEFAULT = 0x02000000,
|
||||
};
|
||||
|
||||
PrefixedLogger log;
|
||||
|
||||
uint32_t lobby_id;
|
||||
|
||||
uint32_t min_level;
|
||||
uint32_t max_level;
|
||||
|
||||
// item info
|
||||
// Item info
|
||||
struct FloorItem {
|
||||
PlayerInventoryItem inv_item;
|
||||
float x;
|
||||
@@ -46,26 +60,43 @@ struct Lobby {
|
||||
uint8_t area;
|
||||
};
|
||||
std::vector<PSOEnemy> enemies;
|
||||
std::shared_ptr<const RareItemSet> rare_item_set;
|
||||
std::array<uint32_t, 12> next_item_id;
|
||||
uint32_t next_game_item_id;
|
||||
PlayerInventoryItem next_drop_item;
|
||||
std::unordered_map<uint32_t, FloorItem> item_id_to_floor_item;
|
||||
parray<le_uint32_t, 0x20> variations;
|
||||
|
||||
// game config
|
||||
// Game config
|
||||
GameVersion version;
|
||||
uint8_t section_id;
|
||||
uint8_t episode; // 1 = Ep1, 2 = Ep2, 3 = Ep4, 0xFF = Ep3
|
||||
uint8_t difficulty;
|
||||
uint8_t mode;
|
||||
std::u16string password;
|
||||
std::u16string name;
|
||||
uint32_t rare_seed;
|
||||
// This seed is also sent to the client for rare enemy generation
|
||||
uint32_t random_seed;
|
||||
std::shared_ptr<std::mt19937> random;
|
||||
std::shared_ptr<const CommonItemCreator> common_item_creator;
|
||||
|
||||
//EP3_GAME_CONFIG* ep3; // only present if this is an Episode 3 game
|
||||
// Ep3 stuff
|
||||
// There are three kinds of Episode 3 games. All of these types have the flag
|
||||
// EPISODE_3_ONLY; types 2 and 3 additionally have the IS_SPECTATOR_TEAM flag.
|
||||
// 1. Primary games. These are the lobbies where battles may take place.
|
||||
// 2. Watcher games. These lobbies receive all the battle and chat commands
|
||||
// from a primary game. (This the implementation of spectator teams.)
|
||||
// 3. Replay games. These lobbies replay a sequence of battle commands and
|
||||
// chat commands from a previous primary game.
|
||||
// Types 2 and 3 may be distinguished by the presence of the battle_record
|
||||
// field - in replay games, it will be present; in watcher games it will be
|
||||
// absent.
|
||||
std::shared_ptr<Episode3::ServerBase> ep3_server_base; // Only used in primary games
|
||||
std::weak_ptr<Lobby> watched_lobby; // Only used in watcher games
|
||||
std::unordered_set<shared_ptr<Lobby>> watcher_lobbies; // Only used in primary games
|
||||
std::shared_ptr<Episode3::BattleRecord> battle_record; // Not used in watcher games
|
||||
std::shared_ptr<Episode3::BattleRecord> prev_battle_record; // Only used in primary games
|
||||
std::shared_ptr<Episode3::BattleRecordPlayer> battle_player; // Only used in replay games
|
||||
std::shared_ptr<Episode3::Tournament::Match> tournament_match;
|
||||
|
||||
// lobby stuff
|
||||
// Lobby stuff
|
||||
uint8_t event;
|
||||
uint8_t block;
|
||||
uint8_t type; // number to give to PSO for the lobby number
|
||||
@@ -74,8 +105,10 @@ struct Lobby {
|
||||
uint32_t flags;
|
||||
std::shared_ptr<const Quest> loading_quest;
|
||||
std::array<std::shared_ptr<Client>, 12> clients;
|
||||
// Keys in this map are client_id
|
||||
std::unordered_map<size_t, std::weak_ptr<Client>> clients_to_add;
|
||||
|
||||
Lobby();
|
||||
explicit Lobby(uint32_t id);
|
||||
|
||||
inline bool is_game() const {
|
||||
return this->flags & Flag::GAME;
|
||||
@@ -85,11 +118,13 @@ struct Lobby {
|
||||
size_t count_clients() const;
|
||||
bool any_client_loading() const;
|
||||
|
||||
void add_client(std::shared_ptr<Client> c);
|
||||
void add_client(std::shared_ptr<Client> c, ssize_t required_client_id = -1);
|
||||
void remove_client(std::shared_ptr<Client> c);
|
||||
|
||||
void move_client_to_lobby(std::shared_ptr<Lobby> dest_lobby,
|
||||
std::shared_ptr<Client> c);
|
||||
void move_client_to_lobby(
|
||||
std::shared_ptr<Lobby> dest_lobby,
|
||||
std::shared_ptr<Client> c,
|
||||
ssize_t required_client_id = -1);
|
||||
|
||||
std::shared_ptr<Client> find_client(
|
||||
const std::u16string* identifier = nullptr,
|
||||
@@ -101,4 +136,6 @@ struct Lobby {
|
||||
uint32_t generate_item_id(uint8_t client_id);
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
#include "Loggers.hh"
|
||||
|
||||
#include <phosg/Strings.hh>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
|
||||
PrefixedLogger ax_messages_log ("[$ax message] " , LogLevel::USE_DEFAULT);
|
||||
PrefixedLogger channel_exceptions_log("[Channel] " , LogLevel::USE_DEFAULT);
|
||||
PrefixedLogger client_log ("" , LogLevel::USE_DEFAULT);
|
||||
PrefixedLogger command_data_log ("[Commands] " , LogLevel::USE_DEFAULT);
|
||||
PrefixedLogger config_log ("[Config] " , LogLevel::USE_DEFAULT);
|
||||
PrefixedLogger dns_server_log ("[DNSServer] " , LogLevel::USE_DEFAULT);
|
||||
PrefixedLogger function_compiler_log ("[FunctionCompiler] ", LogLevel::USE_DEFAULT);
|
||||
PrefixedLogger ip_stack_simulator_log("[IPStackSimulator] ", LogLevel::USE_DEFAULT);
|
||||
PrefixedLogger license_log ("[LicenseManager] " , LogLevel::USE_DEFAULT);
|
||||
PrefixedLogger lobby_log ("" , LogLevel::USE_DEFAULT);
|
||||
PrefixedLogger patch_index_log ("[PatchFileIndex] " , LogLevel::USE_DEFAULT);
|
||||
PrefixedLogger player_data_log ("" , LogLevel::USE_DEFAULT);
|
||||
PrefixedLogger proxy_server_log ("[ProxyServer] " , LogLevel::USE_DEFAULT);
|
||||
PrefixedLogger replay_log ("[ReplaySession] " , LogLevel::USE_DEFAULT);
|
||||
PrefixedLogger server_log ("[Server] " , LogLevel::USE_DEFAULT);
|
||||
PrefixedLogger static_game_data_log ("[StaticGameData] " , LogLevel::USE_DEFAULT);
|
||||
|
||||
static LogLevel log_level_for_name(const string& name) {
|
||||
static const unordered_map<string, LogLevel> levels({
|
||||
{"debug", LogLevel::DEBUG},
|
||||
{"info", LogLevel::INFO},
|
||||
{"warning", LogLevel::WARNING},
|
||||
{"error", LogLevel::ERROR},
|
||||
{"disabled", LogLevel::DISABLED},
|
||||
});
|
||||
return levels.at(tolower(name));
|
||||
}
|
||||
|
||||
static void set_log_level_from_json(
|
||||
PrefixedLogger& log, shared_ptr<JSONObject> d, const char* json_key) {
|
||||
try {
|
||||
log.min_level = log_level_for_name(d->at(json_key)->as_string());
|
||||
} catch (const JSONObject::key_error&) { }
|
||||
}
|
||||
|
||||
void set_log_levels_from_json(shared_ptr<JSONObject> json) {
|
||||
set_log_level_from_json(ax_messages_log , json, "AXMessages");
|
||||
set_log_level_from_json(channel_exceptions_log, json, "ChannelExceptions");
|
||||
set_log_level_from_json(client_log , json, "Clients");
|
||||
set_log_level_from_json(command_data_log , json, "CommandData");
|
||||
set_log_level_from_json(config_log , json, "Config");
|
||||
set_log_level_from_json(dns_server_log , json, "DNSServer");
|
||||
set_log_level_from_json(function_compiler_log , json, "FunctionCompiler");
|
||||
set_log_level_from_json(ip_stack_simulator_log, json, "IPStackSimulator");
|
||||
set_log_level_from_json(license_log , json, "LicenseManager");
|
||||
set_log_level_from_json(lobby_log , json, "Lobbies");
|
||||
set_log_level_from_json(patch_index_log , json, "PatchFileIndex");
|
||||
set_log_level_from_json(player_data_log , json, "PlayerData");
|
||||
set_log_level_from_json(proxy_server_log , json, "ProxyServer");
|
||||
set_log_level_from_json(replay_log , json, "Replay");
|
||||
set_log_level_from_json(server_log , json, "GameServer");
|
||||
set_log_level_from_json(static_game_data_log , json, "StaticGameData");
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include <phosg/Strings.hh>
|
||||
#include <phosg/JSON.hh>
|
||||
|
||||
|
||||
|
||||
extern PrefixedLogger ax_messages_log;
|
||||
extern PrefixedLogger channel_exceptions_log;
|
||||
extern PrefixedLogger client_log;
|
||||
extern PrefixedLogger command_data_log;
|
||||
extern PrefixedLogger config_log;
|
||||
extern PrefixedLogger dns_server_log;
|
||||
extern PrefixedLogger function_compiler_log;
|
||||
extern PrefixedLogger ip_stack_simulator_log;
|
||||
extern PrefixedLogger license_log;
|
||||
extern PrefixedLogger lobby_log;
|
||||
extern PrefixedLogger patch_index_log;
|
||||
extern PrefixedLogger player_data_log;
|
||||
extern PrefixedLogger proxy_server_log;
|
||||
extern PrefixedLogger replay_log;
|
||||
extern PrefixedLogger server_log;
|
||||
extern PrefixedLogger static_game_data_log;
|
||||
|
||||
void set_log_levels_from_json(std::shared_ptr<JSONObject> json);
|
||||
+935
-239
File diff suppressed because it is too large
Load Diff
+493
-243
@@ -1,37 +1,43 @@
|
||||
#include "Map.hh"
|
||||
|
||||
#include <phosg/Filesystem.hh>
|
||||
#include <phosg/Strings.hh>
|
||||
|
||||
#include "FileContentsCache.hh"
|
||||
#include "Loggers.hh"
|
||||
|
||||
using namespace std;
|
||||
|
||||
extern FileContentsCache file_cache;
|
||||
|
||||
|
||||
BattleParamsIndex::BattleParamsIndex(
|
||||
shared_ptr<const string> data_on_ep1,
|
||||
shared_ptr<const string> data_on_ep2,
|
||||
shared_ptr<const string> data_on_ep4,
|
||||
shared_ptr<const string> data_off_ep1,
|
||||
shared_ptr<const string> data_off_ep2,
|
||||
shared_ptr<const string> data_off_ep4) {
|
||||
this->files[0][0].data = data_on_ep1;
|
||||
this->files[0][1].data = data_on_ep2;
|
||||
this->files[0][2].data = data_on_ep4;
|
||||
this->files[1][0].data = data_off_ep1;
|
||||
this->files[1][1].data = data_off_ep2;
|
||||
this->files[1][2].data = data_off_ep4;
|
||||
|
||||
static void load_battle_param_file(const string& filename, BattleParams* entries) {
|
||||
scoped_fd fd(filename, O_RDONLY);
|
||||
readx(fd, entries, 0x60 * sizeof(BattleParams));
|
||||
for (uint8_t is_solo = 0; is_solo < 2; is_solo++) {
|
||||
for (uint8_t episode = 0; episode < 3; episode++) {
|
||||
auto& file = this->files[is_solo][episode];
|
||||
if (file.data->size() < sizeof(Table)) {
|
||||
throw runtime_error(string_printf(
|
||||
"battle params table size is incorrect (expected %zX bytes, have %zX bytes; is_solo=%hhu, episode=%hhu)",
|
||||
sizeof(Table), file.data->size(), is_solo, episode));
|
||||
}
|
||||
file.table = reinterpret_cast<const Table*>(file.data->data());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
BattleParamTable::BattleParamTable(const char* prefix) {
|
||||
load_battle_param_file(string_printf("%s_on.dat", prefix),
|
||||
&this->entries[0][0][0][0]);
|
||||
load_battle_param_file(string_printf("%s_lab_on.dat", prefix),
|
||||
&this->entries[0][1][0][0]);
|
||||
load_battle_param_file(string_printf("%s_ep4_on.dat", prefix),
|
||||
&this->entries[0][2][0][0]);
|
||||
load_battle_param_file(string_printf("%s.dat", prefix),
|
||||
&this->entries[1][0][0][0]);
|
||||
load_battle_param_file(string_printf("%s_lab.dat", prefix),
|
||||
&this->entries[1][1][0][0]);
|
||||
load_battle_param_file(string_printf("%s_ep4.dat", prefix),
|
||||
&this->entries[1][2][0][0]);
|
||||
}
|
||||
|
||||
const BattleParams& BattleParamTable::get(bool solo, uint8_t episode,
|
||||
uint8_t difficulty, uint8_t monster_type) const {
|
||||
const BattleParamsIndex::Entry& BattleParamsIndex::get(
|
||||
bool solo, uint8_t episode, uint8_t difficulty, uint8_t monster_type) const {
|
||||
if (episode > 3) {
|
||||
throw invalid_argument("incorrect episode");
|
||||
}
|
||||
@@ -41,26 +47,31 @@ const BattleParams& BattleParamTable::get(bool solo, uint8_t episode,
|
||||
if (monster_type > 0x60) {
|
||||
throw invalid_argument("incorrect monster type");
|
||||
}
|
||||
return this->entries[!!solo][episode][difficulty][monster_type];
|
||||
}
|
||||
|
||||
const BattleParams* BattleParamTable::get_subtable(bool solo, uint8_t episode,
|
||||
uint8_t difficulty) const {
|
||||
if (episode > 3) {
|
||||
throw invalid_argument("incorrect episode");
|
||||
}
|
||||
if (difficulty > 4) {
|
||||
throw invalid_argument("incorrect difficulty");
|
||||
}
|
||||
return &this->entries[!!solo][episode][difficulty][0];
|
||||
return this->files[!!solo][episode].table->difficulty[difficulty][monster_type];
|
||||
}
|
||||
|
||||
|
||||
|
||||
PSOEnemy::PSOEnemy() : PSOEnemy(0, 0) { }
|
||||
PSOEnemy::PSOEnemy(uint64_t id) : PSOEnemy(id, 0, 0, 0, "__missing__") { }
|
||||
|
||||
PSOEnemy::PSOEnemy(uint32_t experience, uint32_t rt_index) : unused(0),
|
||||
hit_flags(0), last_hit(0), experience(experience), rt_index(rt_index) { }
|
||||
PSOEnemy::PSOEnemy(
|
||||
uint64_t id,
|
||||
uint16_t source_type,
|
||||
uint32_t experience,
|
||||
uint32_t rt_index,
|
||||
const char* type_name)
|
||||
: id(id),
|
||||
source_type(source_type),
|
||||
hit_flags(0),
|
||||
last_hit(0),
|
||||
experience(experience),
|
||||
rt_index(rt_index),
|
||||
type_name(type_name) { }
|
||||
|
||||
string PSOEnemy::str() const {
|
||||
return string_printf("[Enemy E-%" PRIX64 " \"%s\" source_type=%hX hit=%02hhX/%hu exp=%" PRIu32 " rt_index=%" PRIX32 "]",
|
||||
this->id, this->type_name, this->source_type, this->hit_flags, this->last_hit, this->experience, this->rt_index);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -76,368 +87,607 @@ struct EnemyEntry {
|
||||
uint32_t reserved15;
|
||||
} __attribute__((packed));
|
||||
|
||||
static vector<PSOEnemy> parse_map(uint8_t episode, uint8_t difficulty,
|
||||
const BattleParams* battle_params, const EnemyEntry* map,
|
||||
size_t entry_count, bool alt_enemies) {
|
||||
static uint64_t next_enemy_id = 1;
|
||||
|
||||
vector<PSOEnemy> parse_map(
|
||||
shared_ptr<const BattleParamsIndex> battle_params,
|
||||
bool is_solo,
|
||||
uint8_t episode,
|
||||
uint8_t difficulty,
|
||||
shared_ptr<const string> data,
|
||||
bool alt_enemies) {
|
||||
|
||||
const auto* map = reinterpret_cast<const EnemyEntry*>(data->data());
|
||||
size_t entry_count = data->size() / sizeof(EnemyEntry);
|
||||
if (data->size() != entry_count * sizeof(EnemyEntry)) {
|
||||
throw runtime_error("data size is not a multiple of entry size");
|
||||
}
|
||||
|
||||
vector<PSOEnemy> enemies;
|
||||
enemies.resize(0xB50);
|
||||
size_t num_enemies = 0;
|
||||
|
||||
// TODO: this is some of the nastiest code ever. de-nastify it at your leisure
|
||||
for (size_t y = 0; y < entry_count; y++) {
|
||||
if (enemies.size() >= 0xB50) {
|
||||
break;
|
||||
auto create_clones = [&](size_t count) {
|
||||
for (; count > 0; count--) {
|
||||
enemies.emplace_back(next_enemy_id++);
|
||||
}
|
||||
};
|
||||
|
||||
size_t num_clones = map[y].num_clones;
|
||||
auto get_battle_params = [&](uint8_t type) -> const BattleParamsIndex::Entry& {
|
||||
return battle_params->get(is_solo, episode, difficulty, type);
|
||||
};
|
||||
|
||||
switch (map[y].base) {
|
||||
for (size_t y = 0; y < entry_count; y++) {
|
||||
const auto& e = map[y];
|
||||
size_t num_clones = e.num_clones;
|
||||
|
||||
switch (e.base) {
|
||||
case 0x40: // Hildebear and Hildetorr
|
||||
enemies[num_enemies].rt_index = 0x01 + (map[y].skin & 0x01);
|
||||
enemies[num_enemies].experience = battle_params[0x49 + (map[y].skin & 0x01)].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base,
|
||||
get_battle_params(0x49 + (e.skin & 0x01)).experience,
|
||||
0x01 + (e.skin & 0x01), "Hilde(bear|torr)");
|
||||
break;
|
||||
case 0x41: // Rappies
|
||||
if (episode == 3) { // Del Rappy and Sand Rappy
|
||||
enemies[num_enemies].rt_index = 17 + (map[y].skin & 0x01);
|
||||
if (alt_enemies) {
|
||||
enemies[num_enemies].experience = battle_params[0x17 + (map[y].skin & 0x01)].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base,
|
||||
get_battle_params(0x17 + (e.skin & 0x01)).experience,
|
||||
17 + (e.skin & 0x01), "(Del|Sand) Rappy");
|
||||
} else {
|
||||
enemies[num_enemies].experience = battle_params[0x05 + (map[y].skin & 0x01)].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base,
|
||||
get_battle_params(0x05 + (e.skin & 0x01)).experience,
|
||||
17 + (e.skin & 0x01), "(Del|Sand) Rappy");
|
||||
}
|
||||
} else { // Rag Rappy and Al Rappy (Love for Episode II)
|
||||
if (map[y].skin & 0x01) {
|
||||
enemies[num_enemies].rt_index = 0xFF; // No clue what rappy it could be... yet.
|
||||
if (e.skin & 0x01) {
|
||||
enemies.emplace_back(next_enemy_id++, e.base,
|
||||
get_battle_params(0x18 + (e.skin & 0x01)).experience,
|
||||
0xFF, "Rare Rappy"); // Don't know (yet) which rare Rappy it is
|
||||
} else {
|
||||
enemies[num_enemies].rt_index = 5;
|
||||
enemies.emplace_back(next_enemy_id++, e.base,
|
||||
get_battle_params(0x18 + (e.skin & 0x01)).experience,
|
||||
5, "Rag Rappy");
|
||||
}
|
||||
enemies[num_enemies].experience = battle_params[0x18 + (map[y].skin & 0x01)].experience;
|
||||
}
|
||||
break;
|
||||
case 0x42: // Monest + 30 Mothmants
|
||||
enemies[num_enemies].experience = battle_params[0x01].experience;
|
||||
enemies[num_enemies].rt_index = 4;
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x01).experience, 4, "Monest");
|
||||
for (size_t x = 0; x < 30; x++) {
|
||||
if (num_enemies >= 0xB50) {
|
||||
break;
|
||||
}
|
||||
num_enemies++;
|
||||
enemies[num_enemies].rt_index = 3;
|
||||
enemies[num_enemies].experience = battle_params[0x00].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x00).experience, 3, "Mothmant");
|
||||
}
|
||||
break;
|
||||
case 0x43: // Savage Wolf and Barbarous Wolf
|
||||
enemies[num_enemies].rt_index = 7 + ((map[y].reserved[10] & 0x800000) ? 1 : 0);
|
||||
enemies[num_enemies].experience = battle_params[0x02 + ((map[y].reserved[10] & 0x800000) ? 1 : 0)].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base,
|
||||
get_battle_params(0x02 + ((e.reserved[10] & 0x800000) ? 1 : 0)).experience,
|
||||
7 + ((e.reserved[10] & 0x800000) ? 1 : 0), "(Savage|Barbarous) Wolf");
|
||||
break;
|
||||
case 0x44: // Booma family
|
||||
enemies[num_enemies].rt_index = 9 + (map[y].skin % 3);
|
||||
enemies[num_enemies].experience = battle_params[0x4B + (map[y].skin % 3)].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base,
|
||||
get_battle_params(0x4B + (e.skin % 3)).experience,
|
||||
9 + (e.skin % 3), "(|Go|Gigo)Booma");
|
||||
break;
|
||||
case 0x60: // Grass Assassin
|
||||
enemies[num_enemies].rt_index = 12;
|
||||
enemies[num_enemies].experience = battle_params[0x4E].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x4E).experience, 12, "Grass Assassin");
|
||||
break;
|
||||
case 0x61: // Del Lily, Poison Lily, Nar Lily
|
||||
if ((episode == 2) && (alt_enemies)) {
|
||||
enemies[num_enemies].rt_index = 83;
|
||||
enemies[num_enemies].experience = battle_params[0x25].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x25).experience, 83, "Del Lily");
|
||||
} else {
|
||||
enemies[num_enemies].rt_index = 13 + ((map[y].reserved[10] & 0x800000) ? 1 : 0);
|
||||
enemies[num_enemies].experience = battle_params[0x04 + ((map[y].reserved[10] & 0x800000) ? 1 : 0)].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base,
|
||||
get_battle_params(0x04 + ((e.reserved[10] & 0x800000) ? 1 : 0)).experience,
|
||||
13 + ((e.reserved[10] & 0x800000) ? 1 : 0), "(Poison|Nar) Lily");
|
||||
}
|
||||
break;
|
||||
case 0x62: // Nano Dragon
|
||||
enemies[num_enemies].rt_index = 15;
|
||||
enemies[num_enemies].experience = battle_params[0x1A].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x1A).experience, 15, "Nano Dragon");
|
||||
break;
|
||||
case 0x63: // Shark family
|
||||
enemies[num_enemies].rt_index = 16 + (map[y].skin % 3);
|
||||
enemies[num_enemies].experience = battle_params[0x4F + (map[y].skin % 3)].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base,
|
||||
get_battle_params(0x4F + (e.skin % 3)).experience,
|
||||
16 + (e.skin % 3), "(Evil|Pal|Guil) Shark");
|
||||
break;
|
||||
case 0x64: // Slime + 4 clones
|
||||
enemies[num_enemies].rt_index = 19 + ((map[y].reserved[10] & 0x800000) ? 1 : 0);
|
||||
enemies[num_enemies].experience = battle_params[0x2F + ((map[y].reserved[10] & 0x800000) ? 0 : 1)].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base,
|
||||
get_battle_params(0x2F + ((e.reserved[10] & 0x800000) ? 0 : 1)).experience,
|
||||
19 + ((e.reserved[10] & 0x800000) ? 1 : 0), "Pof?uilly Slime");
|
||||
for (size_t x = 0; x < 4; x++) {
|
||||
if (num_enemies >= 0xB50) {
|
||||
break;
|
||||
}
|
||||
num_enemies++;
|
||||
enemies[num_enemies].rt_index = 19;
|
||||
enemies[num_enemies].experience = battle_params[0x30].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x30).experience, 19, "Pof?uilly Slime clone");
|
||||
}
|
||||
break;
|
||||
case 0x65: // Pan Arms, Migium, Hidoom
|
||||
for (size_t x = 0; x < 3; x++) {
|
||||
enemies[num_enemies + x].rt_index = 21 + x;
|
||||
enemies[num_enemies + x].experience = battle_params[0x31 + x].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x31 + x).experience, 21 + x, "(Pan Arms|Hidoom|Migium)");
|
||||
}
|
||||
num_enemies += 2;
|
||||
break;
|
||||
case 0x80: // Dubchic and Gilchic
|
||||
enemies[num_enemies].experience = battle_params[0x1B + (map[y].skin & 0x01)].experience;
|
||||
if (map[y].skin & 0x01) {
|
||||
enemies[num_enemies].rt_index = 50;
|
||||
case 0x80: // Dubchic and Gillchic
|
||||
if (e.skin & 0x01) {
|
||||
enemies.emplace_back(next_enemy_id++, e.base,
|
||||
get_battle_params(0x1B + (e.skin & 0x01)).experience, 50, "(Dub|Gill)chic");
|
||||
} else {
|
||||
enemies[num_enemies].rt_index = 24;
|
||||
enemies.emplace_back(next_enemy_id++, e.base,
|
||||
get_battle_params(0x1B + (e.skin & 0x01)).experience, 24, "(Dub|Gill)chic");
|
||||
}
|
||||
break;
|
||||
case 0x81: // Garanz
|
||||
enemies[num_enemies].rt_index = 25;
|
||||
enemies[num_enemies].experience = battle_params[0x1D].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x1D).experience, 25, "Garanz");
|
||||
break;
|
||||
case 0x82: // Sinow Beat and Gold
|
||||
enemies[num_enemies].rt_index = 26 + ((map[y].reserved[10] & 0x800000) ? 1 : 0);
|
||||
if (map[y].reserved[10] & 0x800000) {
|
||||
enemies[num_enemies].experience = battle_params[0x13].experience;
|
||||
if (e.reserved[10] & 0x800000) {
|
||||
enemies.emplace_back(next_enemy_id++, e.base,
|
||||
get_battle_params(0x13).experience,
|
||||
26 + ((e.reserved[10] & 0x800000) ? 1 : 0), "Sinow (Beat|Gold)");
|
||||
} else {
|
||||
enemies[num_enemies].experience = battle_params[0x06].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base,
|
||||
get_battle_params(0x06).experience,
|
||||
26 + ((e.reserved[10] & 0x800000) ? 1 : 0), "Sinow (Beat|Gold)");
|
||||
}
|
||||
if (map[y].num_clones == 0) {
|
||||
num_clones = 4; // only if no clone # present
|
||||
if (e.num_clones == 0) {
|
||||
create_clones(4);
|
||||
}
|
||||
break;
|
||||
case 0x83: // Canadine
|
||||
enemies[num_enemies].rt_index = 28;
|
||||
enemies[num_enemies].experience = battle_params[0x07].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x07).experience, 28, "Canadine");
|
||||
break;
|
||||
case 0x84: // Canadine Group
|
||||
enemies[num_enemies].rt_index = 29;
|
||||
enemies[num_enemies].experience = battle_params[0x09].experience;
|
||||
for (size_t x = 1; x < 9; x++) {
|
||||
enemies[num_enemies + x].rt_index = 28;
|
||||
enemies[num_enemies + x].experience = battle_params[0x08].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x09).experience, 29, "Canune");
|
||||
for (size_t x = 0; x < 8; x++) {
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x08).experience, 28, "Canadine");
|
||||
}
|
||||
num_enemies += 8;
|
||||
break;
|
||||
case 0x85: // Dubwitch
|
||||
break;
|
||||
case 0xA0: // Delsaber
|
||||
enemies[num_enemies].rt_index = 30;
|
||||
enemies[num_enemies].experience = battle_params[0x52].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x52).experience, 30, "Delsaber");
|
||||
break;
|
||||
case 0xA1: // Chaos Sorcerer + 2 Bits
|
||||
enemies[num_enemies].rt_index = 31;
|
||||
enemies[num_enemies].experience = battle_params[0x0A].experience;
|
||||
num_enemies += 2;
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x0A).experience, 31, "Chaos Sorcerer");
|
||||
create_clones(2);
|
||||
break;
|
||||
case 0xA2: // Dark Gunner
|
||||
enemies[num_enemies].rt_index = 34;
|
||||
enemies[num_enemies].experience = battle_params[0x1E].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x1E).experience, 34, "Dark Gunner");
|
||||
break;
|
||||
case 0xA4: // Chaos Bringer
|
||||
enemies[num_enemies].rt_index = 36;
|
||||
enemies[num_enemies].experience = battle_params[0x0D].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x0D).experience, 36, "Chaos Bringer");
|
||||
break;
|
||||
case 0xA5: // Dark Belra
|
||||
enemies[num_enemies].rt_index = 37;
|
||||
enemies[num_enemies].experience = battle_params[0x0E].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x0E).experience, 37, "Dark Belra");
|
||||
break;
|
||||
case 0xA6: // Dimenian family
|
||||
enemies[num_enemies].rt_index = 41 + (map[y].skin % 3);
|
||||
enemies[num_enemies].experience = battle_params[0x53 + (map[y].skin % 3)].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base,
|
||||
get_battle_params(0x53 + (e.skin % 3)).experience, 41 + (e.skin % 3), "(|La|So) Dimenian");
|
||||
break;
|
||||
case 0xA7: // Bulclaw + 4 claws
|
||||
enemies[num_enemies].rt_index = 40;
|
||||
enemies[num_enemies].experience = battle_params[0x1F].experience;
|
||||
for (size_t x = 1; x < 5; x++) {
|
||||
enemies[num_enemies + x].rt_index = 38;
|
||||
enemies[num_enemies + x].experience = battle_params[0x20].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x1F).experience, 40, "Bulclaw");
|
||||
for (size_t x = 0; x < 4; x++) {
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x20).experience, 38, "Claw");
|
||||
}
|
||||
num_enemies += 4;
|
||||
break;
|
||||
case 0xA8: // Claw
|
||||
enemies[num_enemies].rt_index = 38;
|
||||
enemies[num_enemies].experience = battle_params[0x20].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x20).experience, 38, "Claw");
|
||||
break;
|
||||
case 0xC0: // Dragon or Gal Gryphon
|
||||
if (episode == 1) {
|
||||
enemies[num_enemies].rt_index = 44;
|
||||
enemies[num_enemies].experience = battle_params[0x12].experience;
|
||||
} else if (episode == 0x02) {
|
||||
enemies[num_enemies].rt_index = 77;
|
||||
enemies[num_enemies].experience = battle_params[0x1E].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x12).experience, 44, "Dragon");
|
||||
} else if (episode == 2) {
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x1E).experience, 77, "Gal Gryphon");
|
||||
}
|
||||
break;
|
||||
case 0xC1: // De Rol Le
|
||||
enemies[num_enemies].rt_index = 45;
|
||||
enemies[num_enemies].experience = battle_params[0x0F].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x0F).experience, 45, "De Rol Le");
|
||||
break;
|
||||
case 0xC2: // Vol Opt form 1
|
||||
break;
|
||||
case 0xC5: // Vol Opt form 2
|
||||
enemies[num_enemies].rt_index = 46;
|
||||
enemies[num_enemies].experience = battle_params[0x25].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x25).experience, 46, "Vol Opt");
|
||||
break;
|
||||
case 0xC8: // Dark Falz + 510 Helpers
|
||||
enemies[num_enemies].rt_index = 47;
|
||||
if (difficulty) {
|
||||
enemies[num_enemies].experience = battle_params[0x38].experience; // Form 2
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x38).experience, 47, "Dark Falz 3"); // Final form
|
||||
} else {
|
||||
enemies[num_enemies].experience = battle_params[0x37].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x37).experience, 47, "Dark Falz 2"); // Second form
|
||||
}
|
||||
for (size_t x = 1; x < 511; x++) {
|
||||
//enemies[num_enemies + x].base = 200;
|
||||
enemies[num_enemies + x].experience = battle_params[0x35].experience;
|
||||
for (size_t x = 0; x < 510; x++) {
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x35).experience, 0, "Darvant");
|
||||
}
|
||||
num_enemies += 510;
|
||||
break;
|
||||
case 0xCA: // Olga Flow
|
||||
enemies[num_enemies].rt_index = 78;
|
||||
enemies[num_enemies].experience = battle_params[0x2C].experience;
|
||||
num_enemies += 512;
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x2C).experience, 78, "Olga Flow");
|
||||
create_clones(0x200);
|
||||
break;
|
||||
case 0xCB: // Barba Ray
|
||||
enemies[num_enemies].rt_index = 73;
|
||||
enemies[num_enemies].experience = battle_params[0x0F].experience;
|
||||
num_enemies += 47;
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x0F).experience, 73, "Barba Ray");
|
||||
create_clones(0x2F);
|
||||
break;
|
||||
case 0xCC: // Gol Dragon
|
||||
enemies[num_enemies].rt_index = 76;
|
||||
enemies[num_enemies].experience = battle_params[0x12].experience;
|
||||
num_enemies += 5;
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x12).experience, 76, "Gol Dragon");
|
||||
create_clones(5);
|
||||
break;
|
||||
case 0xD4: // Sinow Berill & Spigell
|
||||
enemies[num_enemies].rt_index = 62 + ((map[y].reserved[10] & 0x800000) ? 1 : 0);
|
||||
enemies[num_enemies].experience = battle_params[(map[y].reserved[10] & 0x800000) ? 0x13 : 0x06].experience;
|
||||
num_enemies += 4; // Add 4 clones which are never used...
|
||||
case 0xD4: // Sinows Berill & Spigell
|
||||
enemies.emplace_back(next_enemy_id++, e.base,
|
||||
get_battle_params((e.reserved[10] & 0x800000) ? 0x13 : 0x06).experience,
|
||||
62 + ((e.reserved[10] & 0x800000) ? 1 : 0), "Sinow (Berrill|Spigell)");
|
||||
create_clones(4);
|
||||
break;
|
||||
case 0xD5: // Merillia & Meriltas
|
||||
enemies[num_enemies].rt_index = 52 + (map[y].skin & 0x01);
|
||||
enemies[num_enemies].experience = battle_params[0x4B + (map[y].skin & 0x01)].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base,
|
||||
get_battle_params(0x4B + (e.skin & 0x01)).experience,
|
||||
52 + (e.skin & 0x01), "Meril(lia|tas)");
|
||||
break;
|
||||
case 0xD6: // Mericus, Merikle, & Mericarol
|
||||
enemies[num_enemies].rt_index = 56 + (map[y].skin % 3);
|
||||
if (map[y].skin) {
|
||||
enemies[num_enemies].experience = battle_params[0x44 + (map[y].skin % 3)].experience;
|
||||
if (e.skin) {
|
||||
enemies.emplace_back(next_enemy_id++, e.base,
|
||||
get_battle_params(0x44 + (e.skin % 3)).experience, 56 + (e.skin % 3), "Meri(cus|kle|carol)");
|
||||
} else {
|
||||
enemies[num_enemies].experience = battle_params[0x3A].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base,
|
||||
get_battle_params(0x3A).experience, 56 + (e.skin % 3), "Meri(cus|kle|carol)");
|
||||
}
|
||||
break;
|
||||
case 0xD7: // Ul Gibbon and Zol Gibbon
|
||||
enemies[num_enemies].rt_index = 59 + (map[y].skin & 0x01);
|
||||
enemies[num_enemies].experience = battle_params[0x3B + (map[y].skin & 0x01)].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base,
|
||||
get_battle_params(0x3B + (e.skin & 0x01)).experience,
|
||||
59 + (e.skin & 0x01), "(Ul|Zol) Gibbon");
|
||||
break;
|
||||
case 0xD8: // Gibbles
|
||||
enemies[num_enemies].rt_index = 61;
|
||||
enemies[num_enemies].experience = battle_params[0x3D].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x3D).experience, 61, "Gibbles");
|
||||
break;
|
||||
case 0xD9: // Gee
|
||||
enemies[num_enemies].rt_index = 54;
|
||||
enemies[num_enemies].experience = battle_params[0x07].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x07).experience, 54, "Gee");
|
||||
break;
|
||||
case 0xDA: // Gi Gue
|
||||
enemies[num_enemies].rt_index = 55;
|
||||
enemies[num_enemies].experience = battle_params[0x1A].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x1A).experience, 55, "Gi Gue");
|
||||
break;
|
||||
case 0xDB: // Deldepth
|
||||
enemies[num_enemies].rt_index = 71;
|
||||
enemies[num_enemies].experience = battle_params[0x30].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x30).experience, 71, "Deldepth");
|
||||
break;
|
||||
case 0xDC: // Delbiter
|
||||
enemies[num_enemies].rt_index = 72;
|
||||
enemies[num_enemies].experience = battle_params[0x0D].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x0D).experience, 72, "Delbiter");
|
||||
break;
|
||||
case 0xDD: // Dolmolm and Dolmdarl
|
||||
enemies[num_enemies].rt_index = 64 + (map[y].skin & 0x01);
|
||||
enemies[num_enemies].experience = battle_params[0x4F + (map[y].skin & 0x01)].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base,
|
||||
get_battle_params(0x4F + (e.skin & 0x01)).experience,
|
||||
64 + (e.skin & 0x01), "Dolm(olm|darl)");
|
||||
break;
|
||||
case 0xDE: // Morfos
|
||||
enemies[num_enemies].rt_index = 66;
|
||||
enemies[num_enemies].experience = battle_params[0x40].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x40).experience, 66, "Morfos");
|
||||
break;
|
||||
case 0xDF: // Recobox & Recons
|
||||
enemies[num_enemies].rt_index = 67;
|
||||
enemies[num_enemies].experience = battle_params[0x41].experience;
|
||||
for (size_t x = 1; x <= map[y].num_clones; x++) {
|
||||
enemies[num_enemies + x].rt_index = 68;
|
||||
enemies[num_enemies + x].experience = battle_params[0x42].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x41).experience, 67, "Recobox");
|
||||
for (size_t x = 0; x < e.num_clones; x++) {
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x42).experience, 68, "Recon");
|
||||
}
|
||||
break;
|
||||
case 0xE0: // Epsilon, Sinow Zoa and Zele
|
||||
if ((episode == 0x02) && (alt_enemies)) {
|
||||
enemies[num_enemies].rt_index = 84;
|
||||
enemies[num_enemies].experience = battle_params[0x23].experience;
|
||||
num_enemies += 4;
|
||||
if ((episode == 2) && (alt_enemies)) {
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x23).experience, 84, "Epsilon");
|
||||
create_clones(4);
|
||||
} else {
|
||||
enemies[num_enemies].rt_index = 69 + (map[y].skin & 0x01);
|
||||
enemies[num_enemies].experience = battle_params[0x43 + (map[y].skin & 0x01)].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base,
|
||||
get_battle_params(0x43 + (e.skin & 0x01)).experience,
|
||||
69 + (e.skin & 0x01), "Sinow Z(oa|ele)");
|
||||
}
|
||||
break;
|
||||
case 0xE1: // Ill Gill
|
||||
enemies[num_enemies].rt_index = 82;
|
||||
enemies[num_enemies].experience = battle_params[0x26].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x26).experience, 82, "Ill Gill");
|
||||
break;
|
||||
case 0x0110: // Astark
|
||||
enemies[num_enemies].rt_index = 1;
|
||||
enemies[num_enemies].experience = battle_params[0x09].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x09).experience, 1, "Astark");
|
||||
break;
|
||||
case 0x0111: // Satellite Lizard and Yowie
|
||||
enemies[num_enemies].rt_index = 2 + ((map[y].reserved[10] & 0x800000) ? 0 : 1);
|
||||
enemies[num_enemies].experience = battle_params[0x0D + ((map[y].reserved[10] & 0x800000) ? 1 : 0) + (alt_enemies ? 0x10 : 0)].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base,
|
||||
get_battle_params(0x0D + ((e.reserved[10] & 0x800000) ? 1 : 0) + (alt_enemies ? 0x10 : 0)).experience,
|
||||
2 + ((e.reserved[10] & 0x800000) ? 0 : 1), "(Satellite Lizard|Yowie)");
|
||||
break;
|
||||
case 0x0112: // Merissa A/AA
|
||||
enemies[num_enemies].rt_index = 4 + (map[y].skin & 0x01);
|
||||
enemies[num_enemies].experience = battle_params[0x19 + (map[y].skin & 0x01)].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base,
|
||||
get_battle_params(0x19 + (e.skin & 0x01)).experience,
|
||||
4 + (e.skin & 0x01), "Merissa AA?");
|
||||
break;
|
||||
case 0x0113: // Girtablulu
|
||||
enemies[num_enemies].rt_index = 6;
|
||||
enemies[num_enemies].experience = battle_params[0x1F].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base, get_battle_params(0x1F).experience, 6, "Girtablulu");
|
||||
break;
|
||||
case 0x0114: // Zu and Pazuzu
|
||||
enemies[num_enemies].rt_index = 7 + (map[y].skin & 0x01);
|
||||
enemies[num_enemies].experience = battle_params[0x0B + (map[y].skin & 0x01) + (alt_enemies ? 0x14: 0x00)].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base,
|
||||
get_battle_params(0x0B + (e.skin & 0x01) + (alt_enemies ? 0x14: 0x00)).experience,
|
||||
7 + (e.skin & 0x01), "(Pazu)?zu");
|
||||
break;
|
||||
case 0x0115: // Boota family
|
||||
enemies[num_enemies].rt_index = 9 + (map[y].skin % 3);
|
||||
if (map[y].skin & 2) {
|
||||
enemies[num_enemies].experience = battle_params[0x03].experience;
|
||||
if (e.skin & 2) {
|
||||
enemies.emplace_back(next_enemy_id++, e.base,
|
||||
get_battle_params(0x03).experience, 9 + (e.skin % 3), "(|Ba|Ze) Boota");
|
||||
} else {
|
||||
enemies[num_enemies].experience = battle_params[0x00 + (map[y].skin % 3)].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base,
|
||||
get_battle_params(0x00 + (e.skin % 3)).experience,
|
||||
9 + (e.skin % 3), "(|Ba|Ze) Boota");
|
||||
}
|
||||
break;
|
||||
case 0x0116: // Dorphon and Eclair
|
||||
enemies[num_enemies].rt_index = 12 + (map[y].skin & 0x01);
|
||||
enemies[num_enemies].experience = battle_params[0x0F + (map[y].skin & 0x01)].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base,
|
||||
get_battle_params(0x0F + (e.skin & 0x01)).experience,
|
||||
12 + (e.skin & 0x01), "Dorphon (Eclair)?");
|
||||
break;
|
||||
case 0x0117: // Goran family
|
||||
if (map[y].skin & 0x02) {
|
||||
enemies[num_enemies].rt_index = 15;
|
||||
} else if (map[y].skin & 0x01) {
|
||||
enemies[num_enemies].rt_index = 16;
|
||||
} else {
|
||||
enemies[num_enemies].rt_index = 14;
|
||||
}
|
||||
enemies[num_enemies].experience = battle_params[0x11 + (map[y].skin % 3)].experience;
|
||||
enemies.emplace_back(next_enemy_id++, e.base,
|
||||
get_battle_params(0x11 + (e.skin % 3)).experience,
|
||||
(e.skin & 2) ? 15 : ((e.skin & 1) ? 16 : 14), "Goran...");
|
||||
break;
|
||||
case 0x0119: // Saint Million, Shambertin, and Kondrieu
|
||||
if (map[y].reserved[10] & 0x800000) {
|
||||
enemies[num_enemies].rt_index = 21;
|
||||
} else {
|
||||
enemies[num_enemies].rt_index = 19 + (map[y].skin & 0x01);
|
||||
}
|
||||
enemies[num_enemies].experience = battle_params[0x22].experience;
|
||||
case 0x0119: // Saint Million, Shambertin, Kondrieu
|
||||
enemies.emplace_back(next_enemy_id++, e.base,
|
||||
get_battle_params(0x22).experience,
|
||||
(e.reserved[10] & 0x800000) ? 21 : (19 + (e.skin & 0x01)),
|
||||
"(Saint-Million|Shambertin|Kondrieu)");
|
||||
break;
|
||||
default:
|
||||
enemies[num_enemies].experience = 0xFFFFFFFF;
|
||||
log(WARNING, "Unknown enemy type %08" PRIX32 " %08" PRIX32, map[y].base,
|
||||
map[y].skin);
|
||||
enemies.emplace_back(next_enemy_id++, e.base, 0xFFFFFFFF, 0, "__unknown__");
|
||||
static_game_data_log.warning(
|
||||
"(Entry %zu, offset %zX in file) Unknown enemy type %08" PRIX32 " %08" PRIX32,
|
||||
y, y * sizeof(EnemyEntry), e.base, e.skin);
|
||||
break;
|
||||
}
|
||||
if (num_clones) {
|
||||
num_enemies += num_clones;
|
||||
}
|
||||
num_enemies++;
|
||||
}
|
||||
create_clones(num_clones);
|
||||
}
|
||||
|
||||
return enemies;
|
||||
}
|
||||
|
||||
vector<PSOEnemy> load_map(const std::string& filename, uint8_t episode,
|
||||
uint8_t difficulty, const BattleParams* battle_params, bool alt_enemies) {
|
||||
shared_ptr<const string> data = file_cache.get(filename);
|
||||
const EnemyEntry* entries = reinterpret_cast<const EnemyEntry*>(data->data());
|
||||
size_t entry_count = data->size() / sizeof(EnemyEntry);
|
||||
return parse_map(episode, difficulty, battle_params, entries, entry_count,
|
||||
alt_enemies);
|
||||
|
||||
|
||||
SetDataTable::SetDataTable(shared_ptr<const string> data, bool big_endian) {
|
||||
if (big_endian) {
|
||||
this->load_table_t<be_uint32_t>(data);
|
||||
} else {
|
||||
this->load_table_t<le_uint32_t>(data);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename U32T>
|
||||
void SetDataTable::load_table_t(shared_ptr<const string> data) {
|
||||
StringReader r(*data);
|
||||
|
||||
struct Footer {
|
||||
U32T table3_offset;
|
||||
U32T table3_count; // In le_uint16_ts (so *2 for size in bytes)
|
||||
U32T unknown_a3; // == 1
|
||||
U32T unknown_a4; // == 0
|
||||
U32T root_table_offset_offset;
|
||||
U32T unknown_a6; // == 0
|
||||
U32T unknown_a7; // == 0
|
||||
U32T unknown_a8; // == 0
|
||||
} __attribute__((packed));
|
||||
if (r.size() < sizeof(Footer)) {
|
||||
throw runtime_error("set data table is too small");
|
||||
}
|
||||
auto& footer = r.pget<Footer>(r.size() - sizeof(Footer));
|
||||
|
||||
uint32_t root_table_offset = r.pget<U32T>(footer.root_table_offset_offset);
|
||||
auto root_r = r.sub(root_table_offset, footer.root_table_offset_offset - root_table_offset);
|
||||
while (!root_r.eof()) {
|
||||
auto& var1_v = this->entries.emplace_back();
|
||||
uint32_t var1_table_offset = root_r.template get<U32T>();
|
||||
uint32_t var1_table_count = root_r.template get<U32T>();
|
||||
auto var1_r = r.sub(var1_table_offset, var1_table_count * 0x08);
|
||||
while (!var1_r.eof()) {
|
||||
auto& var2_v = var1_v.emplace_back();
|
||||
uint32_t var2_table_offset = var1_r.get<U32T>();
|
||||
uint32_t var2_table_count = var1_r.get<U32T>();
|
||||
auto var2_r = r.sub(var2_table_offset, var2_table_count * 0x0C);
|
||||
while (!var2_r.eof()) {
|
||||
auto& entry = var2_v.emplace_back();
|
||||
entry.name1 = r.pget_cstr(var2_r.get<U32T>());
|
||||
entry.enemy_list_basename = r.pget_cstr(var2_r.get<U32T>());
|
||||
entry.name3 = r.pget_cstr(var2_r.get<U32T>());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SetDataTable::print(FILE* stream) const {
|
||||
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];
|
||||
fprintf(stream, "[%02zX/%02zX/%02zX] %s %s %s\n", a, v1, v2, e.name1.c_str(), e.enemy_list_basename.c_str(), e.name3.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
struct AreaMapFileIndex {
|
||||
const char* name_token;
|
||||
vector<uint32_t> variation1_values;
|
||||
vector<uint32_t> variation2_values;
|
||||
|
||||
AreaMapFileIndex(
|
||||
const char* name_token,
|
||||
vector<uint32_t> variation1_values,
|
||||
vector<uint32_t> variation2_values)
|
||||
: name_token(name_token),
|
||||
variation1_values(variation1_values),
|
||||
variation2_values(variation2_values) { }
|
||||
};
|
||||
|
||||
// These are indexed as [episode][is_solo][area]
|
||||
// (Note that Lobby::episode is 1-3, so we actually use episode - 1)
|
||||
static const vector<vector<vector<AreaMapFileIndex>>> map_file_info = {
|
||||
{ // Episode 1
|
||||
{ // Non-solo
|
||||
{"city00", {}, {0}},
|
||||
{"forest01", {}, {0, 1, 2, 3, 4}},
|
||||
{"forest02", {}, {0, 1, 2, 3, 4}},
|
||||
{"cave01", {0, 1, 2}, {0, 1}},
|
||||
{"cave02", {0, 1, 2}, {0, 1}},
|
||||
{"cave03", {0, 1, 2}, {0, 1}},
|
||||
{"machine01", {0, 1, 2}, {0, 1}},
|
||||
{"machine02", {0, 1, 2}, {0, 1}},
|
||||
{"ancient01", {0, 1, 2}, {0, 1}},
|
||||
{"ancient02", {0, 1, 2}, {0, 1}},
|
||||
{"ancient03", {0, 1, 2}, {0, 1}},
|
||||
{"boss01", {}, {}},
|
||||
{"boss02", {}, {}},
|
||||
{"boss03", {}, {}},
|
||||
{"boss04", {}, {}},
|
||||
{nullptr, {}, {}},
|
||||
},
|
||||
{ // Solo
|
||||
{"city00", {}, {0}},
|
||||
{"forest01", {}, {0, 2, 4}},
|
||||
{"forest02", {}, {0, 3, 4}},
|
||||
{"cave01", {0, 1, 2}, {0}},
|
||||
{"cave02", {0, 1, 2}, {0}},
|
||||
{"cave03", {0, 1, 2}, {0}},
|
||||
{nullptr, {}, {}},
|
||||
{nullptr, {}, {}},
|
||||
{nullptr, {}, {}},
|
||||
{nullptr, {}, {}},
|
||||
{nullptr, {}, {}},
|
||||
{nullptr, {}, {}},
|
||||
{nullptr, {}, {}},
|
||||
{nullptr, {}, {}},
|
||||
{nullptr, {}, {}},
|
||||
{nullptr, {}, {}},
|
||||
},
|
||||
},
|
||||
{ // Episode 2
|
||||
{ // Non-solo
|
||||
{"labo00", {}, {0}},
|
||||
{"ruins01", {0, 1}, {0}},
|
||||
{"ruins02", {0, 1}, {0}},
|
||||
{"space01", {0, 1}, {0}},
|
||||
{"space02", {0, 1}, {0}},
|
||||
{"jungle01", {}, {0, 1, 2}},
|
||||
{"jungle02", {}, {0, 1, 2}},
|
||||
{"jungle03", {}, {0, 1, 2}},
|
||||
{"jungle04", {0, 1}, {0, 1}},
|
||||
{"jungle05", {}, {0, 1, 2}},
|
||||
{"seabed01", {0, 1}, {0, 1}},
|
||||
{"seabed02", {0, 1}, {0, 1}},
|
||||
{"boss05", {}, {}},
|
||||
{"boss06", {}, {}},
|
||||
{"boss07", {}, {}},
|
||||
{"boss08", {}, {}},
|
||||
},
|
||||
{ // Solo
|
||||
{"labo00", {}, {0}},
|
||||
{"ruins01", {0, 1}, {0}},
|
||||
{"ruins02", {0, 1}, {0}},
|
||||
{"space01", {0, 1}, {0}},
|
||||
{"space02", {0, 1}, {0}},
|
||||
{"jungle01", {}, {0, 1, 2}},
|
||||
{"jungle02", {}, {0, 1, 2}},
|
||||
{"jungle03", {}, {0, 1, 2}},
|
||||
{"jungle04", {0, 1}, {0, 1}},
|
||||
{"jungle05", {}, {0, 1, 2}},
|
||||
{"seabed01", {0, 1}, {0}},
|
||||
{"seabed02", {0, 1}, {0}},
|
||||
{"boss05", {}, {}},
|
||||
{"boss06", {}, {}},
|
||||
{"boss07", {}, {}},
|
||||
{"boss08", {}, {}},
|
||||
},
|
||||
},
|
||||
{ // Episode 4
|
||||
{ // Non-solo
|
||||
{"city02", {0}, {0}},
|
||||
{"wilds01", {0}, {0, 1, 2}},
|
||||
{"wilds01", {1}, {0, 1, 2}},
|
||||
{"wilds01", {2}, {0, 1, 2}},
|
||||
{"wilds01", {3}, {0, 1, 2}},
|
||||
{"crater01", {0}, {0, 1, 2}},
|
||||
{"desert01", {0, 1, 2}, {0}},
|
||||
{"desert02", {0}, {0, 1, 2}},
|
||||
{"desert03", {0, 1, 2}, {0}},
|
||||
{"boss09", {0}, {0}},
|
||||
{nullptr, {}, {}},
|
||||
{nullptr, {}, {}},
|
||||
{nullptr, {}, {}},
|
||||
{nullptr, {}, {}},
|
||||
{nullptr, {}, {}},
|
||||
{nullptr, {}, {}},
|
||||
},
|
||||
{ // Solo
|
||||
{"city02", {0}, {0}},
|
||||
{"wilds01", {0}, {0, 1, 2}},
|
||||
{"wilds01", {1}, {0, 1, 2}},
|
||||
{"wilds01", {2}, {0, 1, 2}},
|
||||
{"wilds01", {3}, {0, 1, 2}},
|
||||
{"crater01", {0}, {0, 1, 2}},
|
||||
{"desert01", {0, 1, 2}, {0}},
|
||||
{"desert02", {0}, {0, 1, 2}},
|
||||
{"desert03", {0, 1, 2}, {0}},
|
||||
{"boss09", {0}, {0}},
|
||||
{nullptr, {}, {}},
|
||||
{nullptr, {}, {}},
|
||||
{nullptr, {}, {}},
|
||||
{nullptr, {}, {}},
|
||||
{nullptr, {}, {}},
|
||||
{nullptr, {}, {}},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
void generate_variations(
|
||||
parray<le_uint32_t, 0x20>& variations,
|
||||
shared_ptr<mt19937> random,
|
||||
uint8_t episode,
|
||||
bool is_solo) {
|
||||
const auto& ep_index = map_file_info.at(episode - 1);
|
||||
for (size_t z = 0; z < 0x10; z++) {
|
||||
const AreaMapFileIndex* a = nullptr;
|
||||
if (is_solo) {
|
||||
a = &ep_index.at(true).at(z);
|
||||
}
|
||||
if (!a || !a->name_token) {
|
||||
a = &ep_index.at(false).at(z);
|
||||
}
|
||||
if (!a->name_token) {
|
||||
variations[z * 2 + 0] = 0;
|
||||
variations[z * 2 + 1] = 0;
|
||||
} else {
|
||||
variations[z * 2 + 0] = (a->variation1_values.size() < 2) ? 0 :
|
||||
((*random)() % a->variation1_values.size());
|
||||
variations[z * 2 + 1] = (a->variation2_values.size() < 2) ? 0 :
|
||||
((*random)() % a->variation2_values.size());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vector<string> map_filenames_for_variation(
|
||||
uint8_t episode, bool is_solo, uint8_t area, uint32_t var1, uint32_t var2) {
|
||||
// Map filenames are like map_<name_token>[_VV][_VV][_off]<e|o>[_s].dat
|
||||
// name_token comes from AreaMapFileIndex
|
||||
// _VV are the values from the variation<1|2>_values vector (in contrast to
|
||||
// the values sent in the 64 command, which are INDEXES INTO THAT VECTOR)
|
||||
// _off or _s are used for solo mode (try both - city uses _s whereas levels
|
||||
// use _off apparently)
|
||||
// e|o specifies what kind of data: e = enemies, o = objects
|
||||
const auto& ep_index = map_file_info.at(episode - 1);
|
||||
const AreaMapFileIndex* a = nullptr;
|
||||
if (is_solo) {
|
||||
a = &ep_index.at(true).at(area);
|
||||
}
|
||||
if (!a || !a->name_token) {
|
||||
a = &ep_index.at(false).at(area);
|
||||
}
|
||||
if (!a->name_token) {
|
||||
return vector<string>();
|
||||
}
|
||||
|
||||
string filename = "map_";
|
||||
filename += a->name_token;
|
||||
if (!a->variation1_values.empty()) {
|
||||
filename += string_printf("_%02" PRIX32, a->variation1_values.at(var1));
|
||||
}
|
||||
if (!a->variation2_values.empty()) {
|
||||
filename += string_printf("_%02" PRIX32, a->variation2_values.at(var2));
|
||||
}
|
||||
|
||||
vector<string> ret;
|
||||
if (is_solo) {
|
||||
// Try both _offe.dat and e_s.dat suffixes
|
||||
ret.emplace_back(filename + "_offe.dat");
|
||||
ret.emplace_back(filename + "e_s.dat");
|
||||
} else {
|
||||
ret.emplace_back(filename + "e.dat");
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
+103
-30
@@ -2,52 +2,125 @@
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <phosg/Encoding.hh>
|
||||
#include <random>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "Text.hh"
|
||||
|
||||
|
||||
|
||||
struct BattleParams {
|
||||
uint16_t atp; // attack power
|
||||
uint16_t psv; // perseverance (intelligence?)
|
||||
uint16_t evp; // evasion
|
||||
uint16_t hp; // hit points
|
||||
uint16_t dfp; // defense
|
||||
uint16_t ata; // accuracy
|
||||
uint16_t lck; // luck
|
||||
uint8_t unknown_a1[0x0E];
|
||||
uint32_t experience;
|
||||
uint32_t difficulty;
|
||||
} __attribute__((packed));
|
||||
class BattleParamsIndex {
|
||||
public:
|
||||
struct Entry {
|
||||
le_uint16_t atp; // attack power
|
||||
le_uint16_t psv; // perseverance (intelligence?)
|
||||
le_uint16_t evp; // evasion
|
||||
le_uint16_t hp; // hit points
|
||||
le_uint16_t dfp; // defense
|
||||
le_uint16_t ata; // accuracy
|
||||
le_uint16_t lck; // luck
|
||||
le_uint16_t esp; // ???
|
||||
uint8_t unknown_a1[0x0C];
|
||||
le_uint32_t experience;
|
||||
le_uint32_t difficulty;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct BattleParamTable {
|
||||
BattleParams entries[2][3][4][0x60]; // online/offline, episode, difficulty, monster type
|
||||
struct Table {
|
||||
parray<parray<Entry, 0x60>, 4> difficulty;
|
||||
} __attribute__((packed));
|
||||
|
||||
BattleParamTable(const char* filename_prefix);
|
||||
BattleParamsIndex(
|
||||
std::shared_ptr<const std::string> data_on_ep1, // BattleParamEntry_on.dat
|
||||
std::shared_ptr<const std::string> data_on_ep2, // BattleParamEntry_lab_on.dat
|
||||
std::shared_ptr<const std::string> data_on_ep4, // BattleParamEntry_ep4_on.dat
|
||||
std::shared_ptr<const std::string> data_off_ep1, // BattleParamEntry.dat
|
||||
std::shared_ptr<const std::string> data_off_ep2, // BattleParamEntry_lab.dat
|
||||
std::shared_ptr<const std::string> data_off_ep4); // BattleParamEntry_ep4.dat
|
||||
|
||||
const BattleParams& get(bool solo, uint8_t episode, uint8_t difficulty,
|
||||
const Entry& get(bool solo, uint8_t episode, uint8_t difficulty,
|
||||
uint8_t monster_type) const;
|
||||
const BattleParams* get_subtable(bool solo, uint8_t episode,
|
||||
uint8_t difficulty) const;
|
||||
} __attribute__((packed));
|
||||
|
||||
private:
|
||||
struct LoadedFile {
|
||||
std::shared_ptr<const std::string> data;
|
||||
const Table* table;
|
||||
};
|
||||
|
||||
// online/offline, episode
|
||||
LoadedFile files[2][3];
|
||||
};
|
||||
|
||||
|
||||
|
||||
struct BattleParamIndex {
|
||||
BattleParamTable table_for_episode[3];
|
||||
} __attribute__((packed));
|
||||
|
||||
// an enemy entry as loaded by the game
|
||||
struct PSOEnemy {
|
||||
uint16_t unused;
|
||||
uint64_t id;
|
||||
uint16_t source_type;
|
||||
uint8_t hit_flags;
|
||||
uint8_t last_hit;
|
||||
uint32_t experience;
|
||||
uint32_t rt_index;
|
||||
const char* type_name;
|
||||
|
||||
PSOEnemy();
|
||||
PSOEnemy(uint32_t experience, uint32_t rt_index);
|
||||
explicit PSOEnemy(uint64_t id);
|
||||
PSOEnemy(
|
||||
uint64_t id,
|
||||
uint16_t source_type,
|
||||
uint32_t experience,
|
||||
uint32_t rt_index,
|
||||
const char* type_name);
|
||||
|
||||
std::string str() const;
|
||||
} __attribute__((packed));
|
||||
|
||||
std::vector<PSOEnemy> load_map(const std::string& filename, uint8_t episode,
|
||||
uint8_t difficulty, const BattleParams* bp, bool alt_enemies);
|
||||
std::vector<PSOEnemy> parse_map(
|
||||
std::shared_ptr<const BattleParamsIndex> battle_params,
|
||||
bool is_solo,
|
||||
uint8_t episode,
|
||||
uint8_t difficulty,
|
||||
std::shared_ptr<const std::string> data,
|
||||
bool alt_enemies);
|
||||
|
||||
|
||||
|
||||
// TODO: This class is currently unused. It would be nice if we could use this
|
||||
// to generate variations and link to the corresponding map filenames, but it
|
||||
// seems that SetDataTable.rel files link to map filenames that don't actually
|
||||
// exist in some cases, so we can't just directly use this data structure.
|
||||
class SetDataTable {
|
||||
public:
|
||||
struct SetEntry {
|
||||
std::string name1;
|
||||
std::string enemy_list_basename;
|
||||
std::string name3;
|
||||
};
|
||||
|
||||
SetDataTable(std::shared_ptr<const std::string> data, bool big_endian);
|
||||
|
||||
inline const std::vector<std::vector<std::vector<SetEntry>>> get() const {
|
||||
return this->entries;
|
||||
}
|
||||
|
||||
void print(FILE* stream) const;
|
||||
|
||||
private:
|
||||
template <typename U32T>
|
||||
void load_table_t(std::shared_ptr<const std::string> data);
|
||||
|
||||
// Indexes are [area_id][variation1][variation2]
|
||||
// area_id is cumulative per episode, so Ep2 starts at area_id=18.
|
||||
std::vector<std::vector<std::vector<SetEntry>>> entries;
|
||||
};
|
||||
|
||||
|
||||
|
||||
void generate_variations(
|
||||
parray<le_uint32_t, 0x20>& variations,
|
||||
std::shared_ptr<std::mt19937> random,
|
||||
uint8_t episode,
|
||||
bool is_solo);
|
||||
std::vector<std::string> map_filenames_for_variation(
|
||||
uint8_t episode, bool is_solo, uint8_t area, uint32_t var1, uint32_t var2);
|
||||
void load_map_files();
|
||||
|
||||
+52
-24
@@ -13,15 +13,19 @@
|
||||
// casting values all over the place, so we can't use enum classes either.
|
||||
|
||||
namespace MenuID {
|
||||
constexpr uint32_t MAIN = 0x11000011;
|
||||
constexpr uint32_t INFORMATION = 0x22000022;
|
||||
constexpr uint32_t LOBBY = 0x33000033;
|
||||
constexpr uint32_t GAME = 0x44000044;
|
||||
constexpr uint32_t QUEST = 0x55000055;
|
||||
constexpr uint32_t QUEST_FILTER = 0x66000066;
|
||||
constexpr uint32_t PROXY_DESTINATIONS = 0x77000077;
|
||||
constexpr uint32_t PROGRAMS = 0x88000088;
|
||||
constexpr uint32_t PATCHES = 0x99000099;
|
||||
constexpr uint32_t MAIN = 0x11000011;
|
||||
constexpr uint32_t INFORMATION = 0x22000022;
|
||||
constexpr uint32_t LOBBY = 0x33000033;
|
||||
constexpr uint32_t GAME = 0x44000044;
|
||||
constexpr uint32_t QUEST = 0x55000055;
|
||||
constexpr uint32_t QUEST_FILTER = 0x66000066;
|
||||
constexpr uint32_t PROXY_DESTINATIONS = 0x77000077;
|
||||
constexpr uint32_t PROGRAMS = 0x88000088;
|
||||
constexpr uint32_t PATCHES = 0x99000099;
|
||||
constexpr uint32_t PROXY_OPTIONS = 0xAA0000AA;
|
||||
constexpr uint32_t TOURNAMENTS = 0xBB0000BB;
|
||||
constexpr uint32_t TOURNAMENTS_FOR_SPEC = 0xBB1111BB;
|
||||
constexpr uint32_t TOURNAMENT_ENTRIES = 0xCC0000CC;
|
||||
}
|
||||
|
||||
namespace MainMenuItemID {
|
||||
@@ -37,35 +41,59 @@ namespace MainMenuItemID {
|
||||
|
||||
namespace InformationMenuItemID {
|
||||
constexpr uint32_t GO_BACK = 0x22FFFF22;
|
||||
};
|
||||
}
|
||||
|
||||
namespace ProxyDestinationsMenuItemID {
|
||||
constexpr uint32_t GO_BACK = 0x77FFFF77;
|
||||
};
|
||||
constexpr uint32_t OPTIONS = 0x77EEEE77;
|
||||
}
|
||||
|
||||
namespace ProgramsMenuItemID {
|
||||
constexpr uint32_t GO_BACK = 0x88FFFF88;
|
||||
};
|
||||
}
|
||||
|
||||
namespace PatchesMenuItemID {
|
||||
constexpr uint32_t GO_BACK = 0x99FFFF99;
|
||||
};
|
||||
}
|
||||
|
||||
namespace ProxyOptionsMenuItemID {
|
||||
constexpr uint32_t GO_BACK = 0xAAFFFFAA;
|
||||
constexpr uint32_t CHAT_COMMANDS = 0xAA0000AA;
|
||||
constexpr uint32_t CHAT_FILTER = 0xAA1111AA;
|
||||
constexpr uint32_t INFINITE_HP = 0xAA2222AA;
|
||||
constexpr uint32_t INFINITE_TP = 0xAA3333AA;
|
||||
constexpr uint32_t SWITCH_ASSIST = 0xAA4444AA;
|
||||
constexpr uint32_t BLOCK_EVENTS = 0xAA5555AA;
|
||||
constexpr uint32_t BLOCK_PATCHES = 0xAA6666AA;
|
||||
constexpr uint32_t SAVE_FILES = 0xAA7777AA;
|
||||
constexpr uint32_t RED_NAME = 0xAA8888AA;
|
||||
constexpr uint32_t BLANK_NAME = 0xAA9999AA;
|
||||
constexpr uint32_t SUPPRESS_LOGIN = 0xAAAAAAAA;
|
||||
constexpr uint32_t SKIP_CARD = 0xAABBBBAA;
|
||||
constexpr uint32_t EP3_INFINITE_MESETA = 0xAACCCCAA;
|
||||
}
|
||||
|
||||
|
||||
|
||||
struct MenuItem {
|
||||
enum Flag {
|
||||
INVISIBLE_ON_DC = 0x01,
|
||||
INVISIBLE_ON_PC = 0x02,
|
||||
INVISIBLE_ON_GC = 0x04,
|
||||
INVISIBLE_ON_BB = 0x08,
|
||||
DC_ONLY = INVISIBLE_ON_PC | INVISIBLE_ON_GC | INVISIBLE_ON_BB,
|
||||
PC_ONLY = INVISIBLE_ON_DC | INVISIBLE_ON_GC | INVISIBLE_ON_BB,
|
||||
GC_ONLY = INVISIBLE_ON_DC | INVISIBLE_ON_PC | INVISIBLE_ON_BB,
|
||||
BB_ONLY = INVISIBLE_ON_DC | INVISIBLE_ON_PC | INVISIBLE_ON_GC,
|
||||
REQUIRES_MESSAGE_BOXES = 0x10,
|
||||
REQUIRES_SEND_FUNCTION_CALL = 0x20,
|
||||
REQUIRES_SAVE_DISABLED = 0x40,
|
||||
// For menu items to be visible on DCNTE, they must not have either of the
|
||||
// following two flags. (The INVISIBLE_ON_GCNTE flag behaves similarly.)
|
||||
INVISIBLE_ON_DCNTE = 0x001,
|
||||
INVISIBLE_ON_DC = 0x002,
|
||||
INVISIBLE_ON_PC = 0x004,
|
||||
INVISIBLE_ON_GC_TRIAL_EDITION = 0x008,
|
||||
INVISIBLE_ON_GC = 0x010,
|
||||
INVISIBLE_ON_XB = 0x020,
|
||||
INVISIBLE_ON_BB = 0x040,
|
||||
DC_ONLY = INVISIBLE_ON_PC | INVISIBLE_ON_GC | INVISIBLE_ON_XB | INVISIBLE_ON_BB,
|
||||
PC_ONLY = INVISIBLE_ON_DC | INVISIBLE_ON_GC | INVISIBLE_ON_XB | INVISIBLE_ON_BB,
|
||||
GC_ONLY = INVISIBLE_ON_DC | INVISIBLE_ON_PC | INVISIBLE_ON_XB | INVISIBLE_ON_BB,
|
||||
XB_ONLY = INVISIBLE_ON_DC | INVISIBLE_ON_PC | INVISIBLE_ON_GC | INVISIBLE_ON_BB,
|
||||
BB_ONLY = INVISIBLE_ON_DC | INVISIBLE_ON_PC | INVISIBLE_ON_GC | INVISIBLE_ON_XB,
|
||||
REQUIRES_MESSAGE_BOXES = 0x080,
|
||||
REQUIRES_SEND_FUNCTION_CALL = 0x100,
|
||||
REQUIRES_SAVE_DISABLED = 0x200,
|
||||
};
|
||||
|
||||
uint32_t item_id;
|
||||
|
||||
+337
-190
@@ -23,7 +23,90 @@ void PSOEncryption::decrypt(void* data, size_t size, bool advance) {
|
||||
|
||||
|
||||
|
||||
void PSOPCEncryption::update_stream() {
|
||||
PSOLFGEncryption::PSOLFGEncryption(
|
||||
uint32_t seed, size_t stream_length, size_t end_offset)
|
||||
: stream(stream_length, 0), offset(0), end_offset(end_offset), seed(seed) { }
|
||||
|
||||
uint32_t PSOLFGEncryption::next(bool advance) {
|
||||
if (this->offset == this->end_offset) {
|
||||
this->update_stream();
|
||||
}
|
||||
uint32_t ret = this->stream[this->offset];
|
||||
if (advance) {
|
||||
this->offset++;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename LongT>
|
||||
void PSOLFGEncryption::encrypt_t(void* vdata, size_t size, bool advance) {
|
||||
if (size & 3) {
|
||||
throw invalid_argument("size must be a multiple of 4");
|
||||
}
|
||||
if (!advance && (size != 4)) {
|
||||
throw logic_error("cannot peek-encrypt/decrypt with size > 4");
|
||||
}
|
||||
size >>= 2;
|
||||
|
||||
LongT* data = reinterpret_cast<LongT*>(vdata);
|
||||
for (size_t x = 0; x < size; x++) {
|
||||
data[x] ^= this->next(advance);
|
||||
}
|
||||
}
|
||||
|
||||
void PSOLFGEncryption::encrypt(void* vdata, size_t size, bool advance) {
|
||||
this->encrypt_t<le_uint32_t>(vdata, size, advance);
|
||||
}
|
||||
|
||||
void PSOLFGEncryption::encrypt_big_endian(void* vdata, size_t size, bool advance) {
|
||||
this->encrypt_t<be_uint32_t>(vdata, size, advance);
|
||||
}
|
||||
|
||||
void PSOLFGEncryption::encrypt_both_endian(
|
||||
void* le_vdata, void* be_vdata, size_t size, bool advance) {
|
||||
if (size & 3) {
|
||||
throw invalid_argument("size must be a multiple of 4");
|
||||
}
|
||||
if (!advance && (size != 4)) {
|
||||
throw logic_error("cannot peek-encrypt/decrypt with size > 4");
|
||||
}
|
||||
size >>= 2;
|
||||
|
||||
le_uint32_t* le_data = reinterpret_cast<le_uint32_t*>(le_vdata);
|
||||
be_uint32_t* be_data = reinterpret_cast<be_uint32_t*>(be_vdata);
|
||||
for (size_t x = 0; x < size; x++) {
|
||||
uint32_t key = this->next(advance);
|
||||
le_data[x] ^= key;
|
||||
be_data[x] ^= key;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
PSOV2Encryption::PSOV2Encryption(uint32_t seed)
|
||||
: PSOLFGEncryption(seed, this->STREAM_LENGTH + 1, this->STREAM_LENGTH) {
|
||||
uint32_t esi, ebx, edi, eax, edx, var1;
|
||||
esi = 1;
|
||||
ebx = this->seed;
|
||||
edi = 0x15;
|
||||
this->stream[56] = ebx;
|
||||
this->stream[55] = ebx;
|
||||
while (edi <= 0x46E) {
|
||||
eax = edi;
|
||||
var1 = eax / 55;
|
||||
edx = eax - (var1 * 55);
|
||||
ebx = ebx - esi;
|
||||
edi = edi + 0x15;
|
||||
this->stream[edx] = esi;
|
||||
esi = ebx;
|
||||
ebx = this->stream[edx];
|
||||
}
|
||||
for (size_t x = 0; x < 5; x++) {
|
||||
this->update_stream();
|
||||
}
|
||||
}
|
||||
|
||||
void PSOV2Encryption::update_stream() {
|
||||
uint32_t esi, edi, eax, ebp, edx;
|
||||
edi = 1;
|
||||
edx = 0x18;
|
||||
@@ -45,88 +128,17 @@ void PSOPCEncryption::update_stream() {
|
||||
eax++;
|
||||
edx--;
|
||||
}
|
||||
this->offset = 1;
|
||||
}
|
||||
|
||||
PSOPCEncryption::PSOPCEncryption(uint32_t seed) : offset(1) {
|
||||
uint32_t esi, ebx, edi, eax, edx, var1;
|
||||
esi = 1;
|
||||
ebx = seed;
|
||||
edi = 0x15;
|
||||
this->stream[56] = ebx;
|
||||
this->stream[55] = ebx;
|
||||
while (edi <= 0x46E) {
|
||||
eax = edi;
|
||||
var1 = eax / 55;
|
||||
edx = eax - (var1 * 55);
|
||||
ebx = ebx - esi;
|
||||
edi = edi + 0x15;
|
||||
this->stream[edx] = esi;
|
||||
esi = ebx;
|
||||
ebx = this->stream[edx];
|
||||
}
|
||||
for (size_t x = 0; x < 5; x++) {
|
||||
this->update_stream();
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t PSOPCEncryption::next(bool advance) {
|
||||
if (this->offset == PC_STREAM_LENGTH) {
|
||||
this->update_stream();
|
||||
this->offset = 1;
|
||||
}
|
||||
uint32_t ret = this->stream[this->offset];
|
||||
if (advance) {
|
||||
this->offset++;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void PSOPCEncryption::encrypt(void* vdata, size_t size, bool advance) {
|
||||
if (size & 3) {
|
||||
throw invalid_argument("size must be a multiple of 4");
|
||||
}
|
||||
if (!advance && (size != 4)) {
|
||||
throw logic_error("cannot peek-encrypt/decrypt with size > 4");
|
||||
}
|
||||
size >>= 2;
|
||||
|
||||
le_uint32_t* data = reinterpret_cast<le_uint32_t*>(vdata);
|
||||
for (size_t x = 0; x < size; x++) {
|
||||
data[x] ^= this->next(advance);
|
||||
}
|
||||
PSOEncryption::Type PSOV2Encryption::type() const {
|
||||
return Type::V2;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void PSOGCEncryption::update_stream() {
|
||||
uint32_t r5, r6, r7;
|
||||
r5 = 0;
|
||||
r6 = 489;
|
||||
r7 = 0;
|
||||
|
||||
while (r6 != GC_STREAM_LENGTH) {
|
||||
this->stream[r5++] ^= this->stream[r6++];
|
||||
}
|
||||
|
||||
while (r5 != GC_STREAM_LENGTH) {
|
||||
this->stream[r5++] ^= this->stream[r7++];
|
||||
}
|
||||
|
||||
this->offset = 0;
|
||||
}
|
||||
|
||||
uint32_t PSOGCEncryption::next(bool advance) {
|
||||
if (this->offset == GC_STREAM_LENGTH) {
|
||||
this->update_stream();
|
||||
}
|
||||
uint32_t ret = this->stream[this->offset];
|
||||
if (advance) {
|
||||
this->offset++;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
PSOGCEncryption::PSOGCEncryption(uint32_t seed) : offset(0) {
|
||||
PSOV3Encryption::PSOV3Encryption(uint32_t seed)
|
||||
: PSOLFGEncryption(seed, this->STREAM_LENGTH, this->STREAM_LENGTH) {
|
||||
uint32_t x, y, basekey, source1, source2, source3;
|
||||
basekey = 0;
|
||||
|
||||
@@ -149,7 +161,7 @@ PSOGCEncryption::PSOGCEncryption(uint32_t seed) : offset(0) {
|
||||
source1 = 0;
|
||||
source2 = 1;
|
||||
source3 = this->offset - 1;
|
||||
while (this->offset != GC_STREAM_LENGTH) {
|
||||
while (this->offset != this->STREAM_LENGTH) {
|
||||
this->stream[this->offset++] = (this->stream[source3++] ^ (((this->stream[source1++] << 23) & 0xFF800000) ^ ((this->stream[source2++] >> 9) & 0x007FFFFF)));
|
||||
}
|
||||
|
||||
@@ -158,117 +170,33 @@ PSOGCEncryption::PSOGCEncryption(uint32_t seed) : offset(0) {
|
||||
}
|
||||
}
|
||||
|
||||
void PSOGCEncryption::encrypt(void* vdata, size_t size, bool advance) {
|
||||
if (size & 3) {
|
||||
throw invalid_argument("size must be a multiple of 4");
|
||||
}
|
||||
if (!advance && (size != 4)) {
|
||||
throw logic_error("cannot peek-encrypt/decrypt with size > 4");
|
||||
}
|
||||
size >>= 2;
|
||||
void PSOV3Encryption::update_stream() {
|
||||
uint32_t r5, r6, r7;
|
||||
r5 = 0;
|
||||
r6 = 489;
|
||||
r7 = 0;
|
||||
|
||||
le_uint32_t* data = reinterpret_cast<le_uint32_t*>(vdata);
|
||||
for (size_t x = 0; x < size; x++) {
|
||||
data[x] ^= this->next(advance);
|
||||
while (r6 != this->STREAM_LENGTH) {
|
||||
this->stream[r5++] ^= this->stream[r6++];
|
||||
}
|
||||
|
||||
while (r5 != this->STREAM_LENGTH) {
|
||||
this->stream[r5++] ^= this->stream[r7++];
|
||||
}
|
||||
|
||||
this->offset = 0;
|
||||
}
|
||||
|
||||
PSOEncryption::Type PSOV3Encryption::type() const {
|
||||
return Type::V3;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void PSOBBEncryption::decrypt(void* vdata, size_t size, bool advance) {
|
||||
if (this->state.subtype == Subtype::TFS1) {
|
||||
if (size & 7) {
|
||||
throw invalid_argument("size must be a multiple of 8");
|
||||
}
|
||||
|
||||
le_uint32_t* dwords = reinterpret_cast<le_uint32_t*>(vdata);
|
||||
for (size_t x = 0; x < (size >> 2); x += 2) {
|
||||
for (size_t y = 4; y > 0; y -= 2) {
|
||||
dwords[x] = dwords[x] ^ this->state.initial_keys.as32[y + 1];
|
||||
dwords[x + 1] ^= ((this->state.private_keys.as32[dwords[x] >> 24] +
|
||||
this->state.private_keys.as32[((dwords[x] >> 16) & 0xFF) + 0x100]) ^
|
||||
this->state.private_keys.as32[((dwords[x] >> 8) & 0xFF) + 0x200]) +
|
||||
this->state.private_keys.as32[(dwords[x] & 0xFF) + 0x300];
|
||||
dwords[x + 1] ^= this->state.initial_keys.as32[y];
|
||||
dwords[x] ^= ((this->state.private_keys.as32[dwords[x + 1] >> 24] +
|
||||
this->state.private_keys.as32[((dwords[x + 1] >> 16) & 0xFF) + 0x100]) ^
|
||||
this->state.private_keys.as32[((dwords[x + 1] >> 8) & 0xFF) + 0x200]) +
|
||||
this->state.private_keys.as32[(dwords[x + 1] & 0xFF) + 0x300];
|
||||
}
|
||||
dwords[x] ^= this->state.initial_keys.as32[1];
|
||||
dwords[x + 1] ^= this->state.initial_keys.as32[0];
|
||||
|
||||
uint32_t a = dwords[x];
|
||||
dwords[x] = dwords[x + 1];
|
||||
dwords[x + 1] = a;
|
||||
}
|
||||
|
||||
} else if (this->state.subtype == Subtype::JSD1) {
|
||||
if (size & 1) {
|
||||
throw invalid_argument("size must be a multiple of 2");
|
||||
}
|
||||
if (!advance && (size > 0x100)) {
|
||||
throw logic_error("JSD1 can only peek-decrypt up to 0x100 bytes");
|
||||
}
|
||||
uint8_t* bytes = reinterpret_cast<uint8_t*>(vdata);
|
||||
for (size_t z = 0; z < size; z += 2) {
|
||||
uint8_t a = bytes[z];
|
||||
uint8_t b = bytes[z + 1];
|
||||
bytes[z] = (a & 0x55) | (b & 0xAA);
|
||||
bytes[z + 1] = (a & 0xAA) | (b & 0x55);
|
||||
}
|
||||
for (size_t z = 0; z < size; z++) {
|
||||
bytes[z] ^= this->state.private_keys.as8[this->state.initial_keys.jsd1_stream_offset];
|
||||
if (advance) {
|
||||
this->state.private_keys.as8[this->state.initial_keys.jsd1_stream_offset] -= bytes[z];
|
||||
}
|
||||
this->state.initial_keys.jsd1_stream_offset++;
|
||||
}
|
||||
if (!advance) {
|
||||
this->state.initial_keys.jsd1_stream_offset -= size;
|
||||
}
|
||||
|
||||
} else { // STANDARD or MOCB1
|
||||
if (size & 7) {
|
||||
throw invalid_argument("size must be a multiple of 8");
|
||||
}
|
||||
size_t num_dwords = size >> 2;
|
||||
le_uint32_t* dwords = reinterpret_cast<le_uint32_t*>(vdata);
|
||||
uint32_t edx, ebx, ebp, esi, edi;
|
||||
|
||||
edx = 0;
|
||||
while (edx < num_dwords) {
|
||||
ebx = dwords[edx];
|
||||
ebx = ebx ^ this->state.initial_keys.as32[5];
|
||||
ebp = ((this->state.private_keys.as32[(ebx >> 0x18)] +
|
||||
this->state.private_keys.as32[((ebx >> 0x10) & 0xFF) + 0x100]) ^
|
||||
this->state.private_keys.as32[((ebx >> 0x8) & 0xFF) + 0x200]) +
|
||||
this->state.private_keys.as32[(ebx & 0xFF) + 0x300];
|
||||
ebp = ebp ^ this->state.initial_keys.as32[4];
|
||||
ebp ^= dwords[edx + 1];
|
||||
edi = ((this->state.private_keys.as32[(ebp >> 0x18)] +
|
||||
this->state.private_keys.as32[((ebp >> 0x10) & 0xFF) + 0x100]) ^
|
||||
this->state.private_keys.as32[((ebp >> 0x8) & 0xFF) + 0x200]) +
|
||||
this->state.private_keys.as32[(ebp & 0xFF) + 0x300];
|
||||
edi = edi ^ this->state.initial_keys.as32[3];
|
||||
ebx = ebx ^ edi;
|
||||
esi = ((this->state.private_keys.as32[(ebx >> 0x18)] +
|
||||
this->state.private_keys.as32[((ebx >> 0x10) & 0xFF) + 0x100]) ^
|
||||
this->state.private_keys.as32[((ebx >> 0x8) & 0xFF) + 0x200]) +
|
||||
this->state.private_keys.as32[(ebx & 0xFF) + 0x300];
|
||||
ebp = ebp ^ esi ^ this->state.initial_keys.as32[2];
|
||||
edi = ((this->state.private_keys.as32[(ebp >> 0x18)] +
|
||||
this->state.private_keys.as32[((ebp >> 0x10) & 0xFF) + 0x100]) ^
|
||||
this->state.private_keys.as32[((ebp >> 0x8) & 0xFF) + 0x200]) +
|
||||
this->state.private_keys.as32[(ebp & 0xFF) + 0x300];
|
||||
edi = edi ^ this->state.initial_keys.as32[1];
|
||||
ebp = ebp ^ this->state.initial_keys.as32[0];
|
||||
ebx = ebx ^ edi;
|
||||
dwords[edx] = ebp;
|
||||
dwords[edx + 1] = ebx;
|
||||
edx += 2;
|
||||
}
|
||||
}
|
||||
PSOBBEncryption::PSOBBEncryption(
|
||||
const KeyFile& key, const void* original_seed, size_t seed_size)
|
||||
: state(key) {
|
||||
this->apply_seed(original_seed, seed_size);
|
||||
}
|
||||
|
||||
void PSOBBEncryption::encrypt(void* vdata, size_t size, bool advance) {
|
||||
@@ -368,10 +296,104 @@ void PSOBBEncryption::encrypt(void* vdata, size_t size, bool advance) {
|
||||
}
|
||||
}
|
||||
|
||||
PSOBBEncryption::PSOBBEncryption(
|
||||
const KeyFile& key, const void* original_seed, size_t seed_size)
|
||||
: state(key) {
|
||||
this->apply_seed(original_seed, seed_size);
|
||||
void PSOBBEncryption::decrypt(void* vdata, size_t size, bool advance) {
|
||||
if (this->state.subtype == Subtype::TFS1) {
|
||||
if (size & 7) {
|
||||
throw invalid_argument("size must be a multiple of 8");
|
||||
}
|
||||
|
||||
le_uint32_t* dwords = reinterpret_cast<le_uint32_t*>(vdata);
|
||||
for (size_t x = 0; x < (size >> 2); x += 2) {
|
||||
for (size_t y = 4; y > 0; y -= 2) {
|
||||
dwords[x] = dwords[x] ^ this->state.initial_keys.as32[y + 1];
|
||||
dwords[x + 1] ^= ((this->state.private_keys.as32[dwords[x] >> 24] +
|
||||
this->state.private_keys.as32[((dwords[x] >> 16) & 0xFF) + 0x100]) ^
|
||||
this->state.private_keys.as32[((dwords[x] >> 8) & 0xFF) + 0x200]) +
|
||||
this->state.private_keys.as32[(dwords[x] & 0xFF) + 0x300];
|
||||
dwords[x + 1] ^= this->state.initial_keys.as32[y];
|
||||
dwords[x] ^= ((this->state.private_keys.as32[dwords[x + 1] >> 24] +
|
||||
this->state.private_keys.as32[((dwords[x + 1] >> 16) & 0xFF) + 0x100]) ^
|
||||
this->state.private_keys.as32[((dwords[x + 1] >> 8) & 0xFF) + 0x200]) +
|
||||
this->state.private_keys.as32[(dwords[x + 1] & 0xFF) + 0x300];
|
||||
}
|
||||
dwords[x] ^= this->state.initial_keys.as32[1];
|
||||
dwords[x + 1] ^= this->state.initial_keys.as32[0];
|
||||
|
||||
uint32_t a = dwords[x];
|
||||
dwords[x] = dwords[x + 1];
|
||||
dwords[x + 1] = a;
|
||||
}
|
||||
|
||||
} else if (this->state.subtype == Subtype::JSD1) {
|
||||
if (size & 1) {
|
||||
throw invalid_argument("size must be a multiple of 2");
|
||||
}
|
||||
if (!advance && (size > 0x100)) {
|
||||
throw logic_error("JSD1 can only peek-decrypt up to 0x100 bytes");
|
||||
}
|
||||
uint8_t* bytes = reinterpret_cast<uint8_t*>(vdata);
|
||||
for (size_t z = 0; z < size; z += 2) {
|
||||
uint8_t a = bytes[z];
|
||||
uint8_t b = bytes[z + 1];
|
||||
bytes[z] = (a & 0x55) | (b & 0xAA);
|
||||
bytes[z + 1] = (a & 0xAA) | (b & 0x55);
|
||||
}
|
||||
for (size_t z = 0; z < size; z++) {
|
||||
bytes[z] ^= this->state.private_keys.as8[this->state.initial_keys.jsd1_stream_offset];
|
||||
if (advance) {
|
||||
this->state.private_keys.as8[this->state.initial_keys.jsd1_stream_offset] -= bytes[z];
|
||||
}
|
||||
this->state.initial_keys.jsd1_stream_offset++;
|
||||
}
|
||||
if (!advance) {
|
||||
this->state.initial_keys.jsd1_stream_offset -= size;
|
||||
}
|
||||
|
||||
} else { // STANDARD or MOCB1
|
||||
if (size & 7) {
|
||||
throw invalid_argument("size must be a multiple of 8");
|
||||
}
|
||||
size_t num_dwords = size >> 2;
|
||||
le_uint32_t* dwords = reinterpret_cast<le_uint32_t*>(vdata);
|
||||
uint32_t edx, ebx, ebp, esi, edi;
|
||||
|
||||
edx = 0;
|
||||
while (edx < num_dwords) {
|
||||
ebx = dwords[edx];
|
||||
ebx = ebx ^ this->state.initial_keys.as32[5];
|
||||
ebp = ((this->state.private_keys.as32[(ebx >> 0x18)] +
|
||||
this->state.private_keys.as32[((ebx >> 0x10) & 0xFF) + 0x100]) ^
|
||||
this->state.private_keys.as32[((ebx >> 0x8) & 0xFF) + 0x200]) +
|
||||
this->state.private_keys.as32[(ebx & 0xFF) + 0x300];
|
||||
ebp = ebp ^ this->state.initial_keys.as32[4];
|
||||
ebp ^= dwords[edx + 1];
|
||||
edi = ((this->state.private_keys.as32[(ebp >> 0x18)] +
|
||||
this->state.private_keys.as32[((ebp >> 0x10) & 0xFF) + 0x100]) ^
|
||||
this->state.private_keys.as32[((ebp >> 0x8) & 0xFF) + 0x200]) +
|
||||
this->state.private_keys.as32[(ebp & 0xFF) + 0x300];
|
||||
edi = edi ^ this->state.initial_keys.as32[3];
|
||||
ebx = ebx ^ edi;
|
||||
esi = ((this->state.private_keys.as32[(ebx >> 0x18)] +
|
||||
this->state.private_keys.as32[((ebx >> 0x10) & 0xFF) + 0x100]) ^
|
||||
this->state.private_keys.as32[((ebx >> 0x8) & 0xFF) + 0x200]) +
|
||||
this->state.private_keys.as32[(ebx & 0xFF) + 0x300];
|
||||
ebp = ebp ^ esi ^ this->state.initial_keys.as32[2];
|
||||
edi = ((this->state.private_keys.as32[(ebp >> 0x18)] +
|
||||
this->state.private_keys.as32[((ebp >> 0x10) & 0xFF) + 0x100]) ^
|
||||
this->state.private_keys.as32[((ebp >> 0x8) & 0xFF) + 0x200]) +
|
||||
this->state.private_keys.as32[(ebp & 0xFF) + 0x300];
|
||||
edi = edi ^ this->state.initial_keys.as32[1];
|
||||
ebp = ebp ^ this->state.initial_keys.as32[0];
|
||||
ebx = ebx ^ edi;
|
||||
dwords[edx] = ebp;
|
||||
dwords[edx + 1] = ebx;
|
||||
edx += 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PSOEncryption::Type PSOBBEncryption::type() const {
|
||||
return Type::BB;
|
||||
}
|
||||
|
||||
void PSOBBEncryption::tfs1_scramble(uint32_t* out1, uint32_t* out2) const {
|
||||
@@ -647,9 +669,86 @@ void PSOBBEncryption::apply_seed(const void* original_seed, size_t seed_size) {
|
||||
|
||||
|
||||
|
||||
PSOV2OrV3DetectorEncryption::PSOV2OrV3DetectorEncryption(
|
||||
uint32_t key,
|
||||
const std::unordered_set<uint32_t>& v2_matches,
|
||||
const std::unordered_set<uint32_t>& v3_matches)
|
||||
: key(key), v2_matches(v2_matches), v3_matches(v3_matches) { }
|
||||
|
||||
void PSOV2OrV3DetectorEncryption::encrypt(void* data, size_t size, bool advance) {
|
||||
if (!this->active_crypt) {
|
||||
if (size != 4) {
|
||||
throw logic_error("initial detector decrypt size must be 4");
|
||||
}
|
||||
|
||||
le_uint32_t encrypted = *reinterpret_cast<le_uint32_t*>(data);
|
||||
|
||||
le_uint32_t decrypted_v2 = encrypted;
|
||||
unique_ptr<PSOEncryption> v2_crypt(new PSOV2Encryption(this->key));
|
||||
v2_crypt->decrypt(&decrypted_v2, sizeof(decrypted_v2), false);
|
||||
|
||||
le_uint32_t decrypted_v3 = encrypted;
|
||||
unique_ptr<PSOEncryption> v3_crypt(new PSOV3Encryption(this->key));
|
||||
v3_crypt->decrypt(&decrypted_v3, sizeof(decrypted_v3), false);
|
||||
|
||||
bool v2_match = this->v2_matches.count(decrypted_v2);
|
||||
bool v3_match = this->v3_matches.count(decrypted_v3);
|
||||
if (!v2_match && !v3_match) {
|
||||
throw runtime_error(string_printf(
|
||||
"unable to determine crypt version (input=%08" PRIX32 ", v2=%08" PRIX32 ", v3=%08" PRIX32 ")",
|
||||
encrypted.load(), decrypted_v2.load(), decrypted_v3.load()));
|
||||
} else if (v2_match && v3_match) {
|
||||
throw runtime_error(string_printf(
|
||||
"ambiguous crypt version (v2=%08" PRIX32 ", v3=%08" PRIX32 ")",
|
||||
decrypted_v2.load(), decrypted_v3.load()));
|
||||
} else if (v2_match) {
|
||||
this->active_crypt = move(v2_crypt);
|
||||
} else {
|
||||
this->active_crypt = move(v3_crypt);
|
||||
}
|
||||
}
|
||||
this->active_crypt->encrypt(data, size, advance);
|
||||
}
|
||||
|
||||
PSOEncryption::Type PSOV2OrV3DetectorEncryption::type() const {
|
||||
if (!this->active_crypt) {
|
||||
throw logic_error("detector encryption state is indeterminate");
|
||||
}
|
||||
return this->active_crypt->type();
|
||||
}
|
||||
|
||||
|
||||
|
||||
PSOV2OrV3ImitatorEncryption::PSOV2OrV3ImitatorEncryption(
|
||||
uint32_t key, std::shared_ptr<PSOV2OrV3DetectorEncryption> detector_crypt)
|
||||
: key(key), detector_crypt(detector_crypt) { }
|
||||
|
||||
void PSOV2OrV3ImitatorEncryption::encrypt(void* data, size_t size, bool advance) {
|
||||
if (!this->active_crypt) {
|
||||
auto t = this->detector_crypt->type();
|
||||
if (t == Type::V2) {
|
||||
this->active_crypt.reset(new PSOV2Encryption(this->key));
|
||||
} else if (t == Type::V3) {
|
||||
this->active_crypt.reset(new PSOV3Encryption(this->key));
|
||||
} else {
|
||||
throw logic_error("detector crypt is not V2 or V3");
|
||||
}
|
||||
}
|
||||
this->active_crypt->encrypt(data, size, advance);
|
||||
}
|
||||
|
||||
PSOEncryption::Type PSOV2OrV3ImitatorEncryption::type() const {
|
||||
if (!this->active_crypt) {
|
||||
return this->detector_crypt->type();
|
||||
}
|
||||
return this->active_crypt->type();
|
||||
}
|
||||
|
||||
|
||||
|
||||
PSOBBMultiKeyDetectorEncryption::PSOBBMultiKeyDetectorEncryption(
|
||||
const vector<shared_ptr<const PSOBBEncryption::KeyFile>>& possible_keys,
|
||||
const string& expected_first_data,
|
||||
const unordered_set<string>& expected_first_data,
|
||||
const void* seed,
|
||||
size_t seed_size)
|
||||
: possible_keys(possible_keys),
|
||||
@@ -665,7 +764,7 @@ void PSOBBMultiKeyDetectorEncryption::encrypt(void* data, size_t size, bool adva
|
||||
|
||||
void PSOBBMultiKeyDetectorEncryption::decrypt(void* data, size_t size, bool advance) {
|
||||
if (!this->active_crypt.get()) {
|
||||
if (size != this->expected_first_data.size()) {
|
||||
if (size != 8) {
|
||||
throw logic_error("initial decryption size does not match expected first data size");
|
||||
}
|
||||
|
||||
@@ -675,7 +774,7 @@ void PSOBBMultiKeyDetectorEncryption::decrypt(void* data, size_t size, bool adva
|
||||
*this->active_key, this->seed.data(), this->seed.size()));
|
||||
string test_data(reinterpret_cast<const char*>(data), size);
|
||||
this->active_crypt->decrypt(test_data.data(), test_data.size(), false);
|
||||
if (test_data == this->expected_first_data) {
|
||||
if (this->expected_first_data.count(test_data)) {
|
||||
break;
|
||||
}
|
||||
this->active_key.reset();
|
||||
@@ -688,6 +787,10 @@ void PSOBBMultiKeyDetectorEncryption::decrypt(void* data, size_t size, bool adva
|
||||
this->active_crypt->decrypt(data, size, advance);
|
||||
}
|
||||
|
||||
PSOEncryption::Type PSOBBMultiKeyDetectorEncryption::type() const {
|
||||
return Type::BB;
|
||||
}
|
||||
|
||||
|
||||
|
||||
PSOBBMultiKeyImitatorEncryption::PSOBBMultiKeyImitatorEncryption(
|
||||
@@ -707,6 +810,10 @@ void PSOBBMultiKeyImitatorEncryption::decrypt(void* data, size_t size, bool adva
|
||||
this->ensure_crypt()->decrypt(data, size, advance);
|
||||
}
|
||||
|
||||
PSOEncryption::Type PSOBBMultiKeyImitatorEncryption::type() const {
|
||||
return Type::BB;
|
||||
}
|
||||
|
||||
shared_ptr<PSOBBEncryption> PSOBBMultiKeyImitatorEncryption::ensure_crypt() {
|
||||
if (!this->active_crypt.get()) {
|
||||
auto key = this->detector_crypt->get_active_key();
|
||||
@@ -727,3 +834,43 @@ shared_ptr<PSOBBEncryption> PSOBBMultiKeyImitatorEncryption::ensure_crypt() {
|
||||
}
|
||||
return this->active_crypt;
|
||||
}
|
||||
|
||||
|
||||
|
||||
JSD0Encryption::JSD0Encryption(const void* seed, size_t seed_size) : key(0) {
|
||||
const uint8_t* bytes = reinterpret_cast<const uint8_t*>(seed);
|
||||
for (size_t z = 0; z < seed_size; z++) {
|
||||
this->key ^= bytes[z];
|
||||
}
|
||||
}
|
||||
|
||||
void JSD0Encryption::decrypt(void* data, size_t size, bool) {
|
||||
uint8_t* bytes = reinterpret_cast<uint8_t*>(data);
|
||||
for (size_t z = 0; z < size; z++) {
|
||||
bytes[z] ^= this->key;
|
||||
bytes[z] -= this->key;
|
||||
}
|
||||
}
|
||||
|
||||
void JSD0Encryption::encrypt(void* data, size_t size, bool) {
|
||||
uint8_t* bytes = reinterpret_cast<uint8_t*>(data);
|
||||
for (size_t z = 0; z < size; z++) {
|
||||
bytes[z] += this->key;
|
||||
bytes[z] ^= this->key;
|
||||
}
|
||||
}
|
||||
|
||||
PSOEncryption::Type JSD0Encryption::type() const {
|
||||
return Type::JSD0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void decrypt_trivial_gci_data(void* data, size_t size, uint8_t basis) {
|
||||
uint8_t* bytes = reinterpret_cast<uint8_t*>(data);
|
||||
uint8_t key = basis + 0x80;
|
||||
for (size_t z = 0; z < size; z++) {
|
||||
key = (key * 5) + 1;
|
||||
bytes[z] ^= key;
|
||||
}
|
||||
}
|
||||
|
||||
+114
-21
@@ -12,12 +12,15 @@
|
||||
|
||||
|
||||
|
||||
#define PC_STREAM_LENGTH 56
|
||||
#define GC_STREAM_LENGTH 521
|
||||
#define BB_STREAM_LENGTH 1042
|
||||
|
||||
class PSOEncryption {
|
||||
public:
|
||||
enum class Type {
|
||||
V2 = 0,
|
||||
V3,
|
||||
BB,
|
||||
JSD0,
|
||||
};
|
||||
|
||||
virtual ~PSOEncryption() = default;
|
||||
|
||||
virtual void encrypt(void* data, size_t size, bool advance = true) = 0;
|
||||
@@ -30,38 +33,62 @@ public:
|
||||
this->decrypt(data.data(), data.size(), advance);
|
||||
}
|
||||
|
||||
virtual Type type() const = 0;
|
||||
|
||||
protected:
|
||||
PSOEncryption() = default;
|
||||
};
|
||||
|
||||
class PSOPCEncryption : public PSOEncryption {
|
||||
|
||||
|
||||
class PSOLFGEncryption : public PSOEncryption {
|
||||
public:
|
||||
explicit PSOPCEncryption(uint32_t seed);
|
||||
|
||||
virtual void encrypt(void* data, size_t size, bool advance = true);
|
||||
void encrypt_big_endian(void* data, size_t size, bool advance = true);
|
||||
void encrypt_both_endian(void* le_data, void* be_data, size_t size, bool advance = true);
|
||||
|
||||
protected:
|
||||
void update_stream();
|
||||
uint32_t next(bool advance = true);
|
||||
|
||||
uint32_t stream[PC_STREAM_LENGTH + 1];
|
||||
uint8_t offset;
|
||||
protected:
|
||||
explicit PSOLFGEncryption(uint32_t seed, size_t stream_length, size_t end_offset);
|
||||
|
||||
template <typename LongT>
|
||||
void encrypt_t(void* data, size_t size, bool advance);
|
||||
|
||||
virtual void update_stream() = 0;
|
||||
|
||||
std::vector<uint32_t> stream;
|
||||
size_t offset;
|
||||
size_t end_offset;
|
||||
uint32_t seed;
|
||||
};
|
||||
|
||||
class PSOGCEncryption : public PSOEncryption {
|
||||
class PSOV2Encryption : public PSOLFGEncryption {
|
||||
public:
|
||||
explicit PSOGCEncryption(uint32_t key);
|
||||
explicit PSOV2Encryption(uint32_t seed);
|
||||
|
||||
virtual void encrypt(void* data, size_t size, bool advance = true);
|
||||
virtual Type type() const;
|
||||
|
||||
protected:
|
||||
void update_stream();
|
||||
uint32_t next(bool advance = true);
|
||||
virtual void update_stream();
|
||||
|
||||
uint32_t stream[GC_STREAM_LENGTH];
|
||||
uint16_t offset;
|
||||
static constexpr size_t STREAM_LENGTH = 56;
|
||||
};
|
||||
|
||||
class PSOV3Encryption : public PSOLFGEncryption {
|
||||
public:
|
||||
explicit PSOV3Encryption(uint32_t key);
|
||||
|
||||
virtual Type type() const;
|
||||
|
||||
protected:
|
||||
virtual void update_stream();
|
||||
|
||||
static constexpr size_t STREAM_LENGTH = 521;
|
||||
};
|
||||
|
||||
|
||||
|
||||
class PSOBBEncryption : public PSOEncryption {
|
||||
public:
|
||||
enum Subtype : uint8_t {
|
||||
@@ -97,6 +124,8 @@ public:
|
||||
virtual void encrypt(void* data, size_t size, bool advance = true);
|
||||
virtual void decrypt(void* data, size_t size, bool advance = true);
|
||||
|
||||
virtual Type type() const;
|
||||
|
||||
protected:
|
||||
KeyFile state;
|
||||
|
||||
@@ -104,15 +133,56 @@ protected:
|
||||
void apply_seed(const void* original_seed, size_t seed_size);
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
// The following classes provide support for automatically detecting which type
|
||||
// of encryption a client is using based on their initial response to the server
|
||||
|
||||
class PSOV2OrV3DetectorEncryption : public PSOEncryption {
|
||||
public:
|
||||
PSOV2OrV3DetectorEncryption(
|
||||
uint32_t key,
|
||||
const std::unordered_set<uint32_t>& v2_matches,
|
||||
const std::unordered_set<uint32_t>& v3_matches);
|
||||
|
||||
virtual void encrypt(void* data, size_t size, bool advance = true);
|
||||
|
||||
virtual Type type() const;
|
||||
|
||||
protected:
|
||||
uint32_t key;
|
||||
const std::unordered_set<uint32_t>& v2_matches;
|
||||
const std::unordered_set<uint32_t>& v3_matches;
|
||||
std::unique_ptr<PSOEncryption> active_crypt;
|
||||
};
|
||||
|
||||
class PSOV2OrV3ImitatorEncryption : public PSOEncryption {
|
||||
public:
|
||||
PSOV2OrV3ImitatorEncryption(
|
||||
uint32_t key, std::shared_ptr<PSOV2OrV3DetectorEncryption> client_crypt);
|
||||
|
||||
virtual void encrypt(void* data, size_t size, bool advance = true);
|
||||
|
||||
virtual Type type() const;
|
||||
|
||||
protected:
|
||||
uint32_t key;
|
||||
std::shared_ptr<const PSOV2OrV3DetectorEncryption> detector_crypt;
|
||||
std::shared_ptr<PSOEncryption> active_crypt;
|
||||
};
|
||||
|
||||
|
||||
|
||||
// The following classes provide support for multiple PSOBB private keys, and
|
||||
// the ability to automatically detect which key the client is using based on
|
||||
// the first 8 bytes they send.
|
||||
// the first 8 bytes they send
|
||||
|
||||
class PSOBBMultiKeyDetectorEncryption : public PSOEncryption {
|
||||
public:
|
||||
PSOBBMultiKeyDetectorEncryption(
|
||||
const std::vector<std::shared_ptr<const PSOBBEncryption::KeyFile>>& possible_keys,
|
||||
const std::string& expected_first_data,
|
||||
const std::unordered_set<std::string>& expected_first_data,
|
||||
const void* seed,
|
||||
size_t seed_size);
|
||||
|
||||
@@ -126,11 +196,13 @@ public:
|
||||
return this->seed;
|
||||
}
|
||||
|
||||
virtual Type type() const;
|
||||
|
||||
protected:
|
||||
std::vector<std::shared_ptr<const PSOBBEncryption::KeyFile>> possible_keys;
|
||||
std::shared_ptr<const PSOBBEncryption::KeyFile> active_key;
|
||||
std::shared_ptr<PSOBBEncryption> active_crypt;
|
||||
std::string expected_first_data;
|
||||
const std::unordered_set<std::string>& expected_first_data;
|
||||
std::string seed;
|
||||
};
|
||||
|
||||
@@ -145,6 +217,8 @@ public:
|
||||
virtual void encrypt(void* data, size_t size, bool advance = true);
|
||||
virtual void decrypt(void* data, size_t size, bool advance = true);
|
||||
|
||||
virtual Type type() const;
|
||||
|
||||
protected:
|
||||
std::shared_ptr<PSOBBEncryption> ensure_crypt();
|
||||
|
||||
@@ -153,3 +227,22 @@ protected:
|
||||
std::string seed;
|
||||
bool jsd1_use_detector_seed;
|
||||
};
|
||||
|
||||
|
||||
|
||||
class JSD0Encryption : public PSOEncryption {
|
||||
public:
|
||||
JSD0Encryption(const void* seed, size_t seed_size);
|
||||
|
||||
virtual void encrypt(void* data, size_t size, bool advance = true);
|
||||
virtual void decrypt(void* data, size_t size, bool advance = true);
|
||||
|
||||
virtual Type type() const = 0;
|
||||
|
||||
private:
|
||||
uint8_t key;
|
||||
};
|
||||
|
||||
|
||||
|
||||
void decrypt_trivial_gci_data(void* data, size_t size, uint8_t basis);
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
#include "PSOGCObjectGraph.hh"
|
||||
|
||||
#include "Text.hh"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
|
||||
struct TObjectVTable {
|
||||
be_uint32_t unused_a1;
|
||||
be_uint32_t unused_a2;
|
||||
be_uint32_t destroy;
|
||||
be_uint32_t update;
|
||||
be_uint32_t render;
|
||||
be_uint32_t render_shadow;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct TObject {
|
||||
be_uint32_t type_name_addr;
|
||||
be_uint16_t flags;
|
||||
parray<uint8_t, 2> unused;
|
||||
be_uint32_t prev_sibling_addr;
|
||||
be_uint32_t next_sibling_addr;
|
||||
be_uint32_t parent_addr;
|
||||
be_uint32_t children_head_addr;
|
||||
be_uint32_t vtable_addr;
|
||||
} __attribute__((packed));
|
||||
|
||||
PSOGCObjectGraph::PSOGCObjectGraph(
|
||||
const string& memory_data, uint32_t root_address) {
|
||||
StringReader r(memory_data);
|
||||
this->root = this->parse_object_memo(r, root_address);
|
||||
}
|
||||
|
||||
shared_ptr<PSOGCObjectGraph::VTable> PSOGCObjectGraph::parse_vtable_memo(
|
||||
StringReader& r, uint32_t addr) {
|
||||
try {
|
||||
return this->all_vtables.at(addr);
|
||||
} catch (const out_of_range&) { }
|
||||
|
||||
const auto& vt = r.pget<TObjectVTable>(addr & 0x01FFFFFF);
|
||||
auto ret = this->all_vtables.emplace(addr, new VTable()).first->second;
|
||||
ret->address = addr;
|
||||
ret->destroy_addr = vt.destroy;
|
||||
ret->update_addr = vt.update;
|
||||
ret->render_addr = vt.render;
|
||||
ret->render_shadow_addr = vt.render_shadow;
|
||||
return ret;
|
||||
}
|
||||
|
||||
shared_ptr<PSOGCObjectGraph::Object> PSOGCObjectGraph::parse_object_memo(
|
||||
StringReader& r, uint32_t addr) {
|
||||
try {
|
||||
return this->all_objects.at(addr);
|
||||
} catch (const out_of_range&) { }
|
||||
|
||||
const auto& obj = r.pget<TObject>(addr & 0x01FFFFFF);
|
||||
string type_name = r.pget_cstr(obj.type_name_addr & 0x01FFFFFF);
|
||||
|
||||
auto ret = this->all_objects.emplace(addr, new Object()).first->second;
|
||||
ret->address = addr;
|
||||
ret->flags = obj.flags;
|
||||
ret->type_name = move(type_name);
|
||||
ret->vtable = this->parse_vtable_memo(r, obj.vtable_addr);
|
||||
if (obj.parent_addr) {
|
||||
ret->parent = this->parse_object_memo(r, obj.parent_addr);
|
||||
}
|
||||
if (obj.children_head_addr) {
|
||||
uint32_t child_addr = obj.children_head_addr;
|
||||
while (child_addr) {
|
||||
ret->children.emplace_back(this->parse_object_memo(r, child_addr));
|
||||
child_addr = r.pget<TObject>(child_addr & 0x01FFFFFF).next_sibling_addr;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void PSOGCObjectGraph::print(FILE* stream) const {
|
||||
this->root->print(stream);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void PSOGCObjectGraph::Object::print(FILE* stream, size_t indent_level) const {
|
||||
for (size_t z = 0; z < indent_level; z++) {
|
||||
fputc(' ', stream);
|
||||
fputc(' ', stream);
|
||||
}
|
||||
fprintf(stream, "%s +%04hX @ %08" PRIX32 " (VT %08" PRIX32 ": destroy=%08" PRIX32 " update=%08" PRIX32 " render=%08" PRIX32 " render_shadow=%08" PRIX32 ")\n",
|
||||
this->type_name.c_str(),
|
||||
this->flags,
|
||||
this->address,
|
||||
this->vtable->address,
|
||||
this->vtable->destroy_addr,
|
||||
this->vtable->update_addr,
|
||||
this->vtable->render_addr,
|
||||
this->vtable->render_shadow_addr);
|
||||
for (const auto& child : this->children) {
|
||||
child->print(stream, indent_level + 1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
#include <memory>
|
||||
#include <phosg/Encoding.hh>
|
||||
#include <phosg/Strings.hh>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
|
||||
|
||||
struct PSOGCObjectGraph {
|
||||
PSOGCObjectGraph(const std::string& memory_data, uint32_t root_address);
|
||||
|
||||
void print(FILE* stream) const;
|
||||
|
||||
struct VTable {
|
||||
uint32_t address;
|
||||
uint32_t destroy_addr;
|
||||
uint32_t update_addr;
|
||||
uint32_t render_addr;
|
||||
uint32_t render_shadow_addr;
|
||||
};
|
||||
|
||||
struct Object {
|
||||
uint32_t address;
|
||||
uint16_t flags;
|
||||
std::string type_name;
|
||||
std::shared_ptr<VTable> vtable;
|
||||
std::shared_ptr<Object> parent;
|
||||
std::vector<std::shared_ptr<Object>> children;
|
||||
|
||||
void print(FILE* stream, size_t indent_level = 0) const;
|
||||
};
|
||||
|
||||
std::shared_ptr<Object> root;
|
||||
std::unordered_map<uint32_t, std::shared_ptr<Object>> all_objects;
|
||||
std::unordered_map<uint32_t, std::shared_ptr<VTable>> all_vtables;
|
||||
|
||||
std::shared_ptr<Object> parse_object_memo(StringReader& r, uint32_t addr);
|
||||
std::shared_ptr<VTable> parse_vtable_memo(StringReader& r, uint32_t addr);
|
||||
};
|
||||
@@ -27,6 +27,8 @@ uint16_t PSOCommandHeader::command(GameVersion version) const {
|
||||
return this->dc.command;
|
||||
case GameVersion::GC:
|
||||
return this->gc.command;
|
||||
case GameVersion::XB:
|
||||
return this->xb.command;
|
||||
case GameVersion::PC:
|
||||
case GameVersion::PATCH:
|
||||
return this->pc.command;
|
||||
@@ -45,6 +47,9 @@ void PSOCommandHeader::set_command(GameVersion version, uint16_t command) {
|
||||
case GameVersion::GC:
|
||||
this->gc.command = command;
|
||||
break;
|
||||
case GameVersion::XB:
|
||||
this->xb.command = command;
|
||||
break;
|
||||
case GameVersion::PC:
|
||||
case GameVersion::PATCH:
|
||||
this->pc.command = command;
|
||||
@@ -63,6 +68,8 @@ uint16_t PSOCommandHeader::size(GameVersion version) const {
|
||||
return this->dc.size;
|
||||
case GameVersion::GC:
|
||||
return this->gc.size;
|
||||
case GameVersion::XB:
|
||||
return this->xb.size;
|
||||
case GameVersion::PC:
|
||||
case GameVersion::PATCH:
|
||||
return this->pc.size;
|
||||
@@ -81,6 +88,9 @@ void PSOCommandHeader::set_size(GameVersion version, uint32_t size) {
|
||||
case GameVersion::GC:
|
||||
this->gc.size = size;
|
||||
break;
|
||||
case GameVersion::XB:
|
||||
this->xb.size = size;
|
||||
break;
|
||||
case GameVersion::PC:
|
||||
case GameVersion::PATCH:
|
||||
this->pc.size = size;
|
||||
@@ -99,6 +109,8 @@ uint32_t PSOCommandHeader::flag(GameVersion version) const {
|
||||
return this->dc.flag;
|
||||
case GameVersion::GC:
|
||||
return this->gc.flag;
|
||||
case GameVersion::XB:
|
||||
return this->xb.flag;
|
||||
case GameVersion::PC:
|
||||
case GameVersion::PATCH:
|
||||
return this->pc.flag;
|
||||
@@ -117,6 +129,9 @@ void PSOCommandHeader::set_flag(GameVersion version, uint32_t flag) {
|
||||
case GameVersion::GC:
|
||||
this->gc.flag = flag;
|
||||
break;
|
||||
case GameVersion::XB:
|
||||
this->xb.flag = flag;
|
||||
break;
|
||||
case GameVersion::PC:
|
||||
case GameVersion::PATCH:
|
||||
this->pc.flag = flag;
|
||||
@@ -146,3 +161,59 @@ void check_size_v(size_t size, size_t min_size, size_t max_size) {
|
||||
max_size, size));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
std::string prepend_command_header(
|
||||
GameVersion version,
|
||||
bool encryption_enabled,
|
||||
uint16_t cmd,
|
||||
uint32_t flag,
|
||||
const std::string& data) {
|
||||
StringWriter ret;
|
||||
switch (version) {
|
||||
case GameVersion::DC:
|
||||
case GameVersion::GC:
|
||||
case GameVersion::XB: {
|
||||
PSOCommandHeaderDCV3 header;
|
||||
if (encryption_enabled) {
|
||||
header.size = (sizeof(header) + data.size() + 3) & ~3;
|
||||
} else {
|
||||
header.size = (sizeof(header) + data.size());
|
||||
}
|
||||
header.command = cmd;
|
||||
header.flag = flag;
|
||||
ret.put(header);
|
||||
break;
|
||||
}
|
||||
case GameVersion::PC:
|
||||
case GameVersion::PATCH: {
|
||||
PSOCommandHeaderPC header;
|
||||
if (encryption_enabled) {
|
||||
header.size = (sizeof(header) + data.size() + 3) & ~3;
|
||||
} else {
|
||||
header.size = (sizeof(header) + data.size());
|
||||
}
|
||||
header.command = cmd;
|
||||
header.flag = flag;
|
||||
ret.put(header);
|
||||
break;
|
||||
}
|
||||
case GameVersion::BB: {
|
||||
PSOCommandHeaderBB header;
|
||||
if (encryption_enabled) {
|
||||
header.size = (sizeof(header) + data.size() + 3) & ~3;
|
||||
} else {
|
||||
header.size = (sizeof(header) + data.size());
|
||||
}
|
||||
header.command = cmd;
|
||||
header.flag = flag;
|
||||
ret.put(header);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
throw logic_error("unimplemented game version in prepend_command_header");
|
||||
}
|
||||
ret.write(data);
|
||||
return move(ret.str());
|
||||
}
|
||||
|
||||
+43
-25
@@ -15,7 +15,7 @@ struct PSOCommandHeaderPC {
|
||||
uint8_t flag;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct PSOCommandHeaderDCGC {
|
||||
struct PSOCommandHeaderDCV3 {
|
||||
uint8_t command;
|
||||
uint8_t flag;
|
||||
le_uint16_t size;
|
||||
@@ -28,9 +28,10 @@ struct PSOCommandHeaderBB {
|
||||
} __attribute__((packed));
|
||||
|
||||
union PSOCommandHeader {
|
||||
PSOCommandHeaderDCGC dc;
|
||||
PSOCommandHeaderDCV3 dc;
|
||||
PSOCommandHeaderPC pc;
|
||||
PSOCommandHeaderDCGC gc;
|
||||
PSOCommandHeaderDCV3 gc;
|
||||
PSOCommandHeaderDCV3 xb;
|
||||
PSOCommandHeaderBB bb;
|
||||
|
||||
uint16_t command(GameVersion version) const;
|
||||
@@ -46,47 +47,64 @@ union PSOCommandHeader {
|
||||
PSOCommandHeader();
|
||||
} __attribute__((packed));
|
||||
|
||||
union PSOSubcommand {
|
||||
uint8_t byte[4];
|
||||
le_uint16_t word[2];
|
||||
le_uint32_t dword;
|
||||
} __attribute__((packed));
|
||||
|
||||
// This function is used in a lot of places to check received command sizes and
|
||||
// cast them to the appropriate type
|
||||
template <typename T>
|
||||
const T& check_size_t(
|
||||
const void* data,
|
||||
size_t size,
|
||||
size_t min_size = sizeof(T),
|
||||
size_t max_size = sizeof(T)) {
|
||||
if (size < min_size) {
|
||||
throw std::runtime_error(string_printf(
|
||||
"command too small (expected at least 0x%zX bytes, received 0x%zX bytes)",
|
||||
min_size, size));
|
||||
}
|
||||
if (size > max_size) {
|
||||
throw std::runtime_error(string_printf(
|
||||
"command too large (expected at most 0x%zX bytes, received 0x%zX bytes)",
|
||||
max_size, size));
|
||||
}
|
||||
return *reinterpret_cast<const T*>(data);
|
||||
}
|
||||
template <typename T>
|
||||
const T& check_size_t(
|
||||
const std::string& data,
|
||||
size_t min_size = sizeof(T),
|
||||
size_t max_size = sizeof(T)) {
|
||||
if (data.size() < min_size) {
|
||||
return check_size_t<T>(data.data(), data.size(), min_size, max_size);
|
||||
}
|
||||
template <typename T>
|
||||
T& check_size_t(
|
||||
void* data,
|
||||
size_t size,
|
||||
size_t min_size = sizeof(T),
|
||||
size_t max_size = sizeof(T)) {
|
||||
if (size < min_size) {
|
||||
throw std::runtime_error(string_printf(
|
||||
"command too small (expected at least 0x%zX bytes, received 0x%zX bytes)",
|
||||
min_size, data.size()));
|
||||
min_size, size));
|
||||
}
|
||||
if (data.size() > max_size) {
|
||||
if (size > max_size) {
|
||||
throw std::runtime_error(string_printf(
|
||||
"command too large (expected at most 0x%zX bytes, received 0x%zX bytes)",
|
||||
max_size, data.size()));
|
||||
max_size, size));
|
||||
}
|
||||
return *reinterpret_cast<const T*>(data.data());
|
||||
return *reinterpret_cast<T*>(data);
|
||||
}
|
||||
template <typename T>
|
||||
T& check_size_t(
|
||||
std::string& data,
|
||||
size_t min_size = sizeof(T),
|
||||
size_t max_size = sizeof(T)) {
|
||||
if (data.size() < min_size) {
|
||||
throw std::runtime_error(string_printf(
|
||||
"command too small (expected at least 0x%zX bytes, received 0x%zX bytes)",
|
||||
min_size, data.size()));
|
||||
}
|
||||
if (data.size() > max_size) {
|
||||
throw std::runtime_error(string_printf(
|
||||
"command too large (expected at most 0x%zX bytes, received 0x%zX bytes)",
|
||||
max_size, data.size()));
|
||||
}
|
||||
return *reinterpret_cast<T*>(data.data());
|
||||
return check_size_t<T>(data.data(), data.size(), min_size, max_size);
|
||||
}
|
||||
|
||||
void check_size_v(size_t size, size_t min_size, size_t max_size = 0);
|
||||
|
||||
std::string prepend_command_header(
|
||||
GameVersion version,
|
||||
bool encryption_enabled,
|
||||
uint16_t cmd,
|
||||
uint32_t flag,
|
||||
const std::string& data);
|
||||
|
||||
@@ -0,0 +1,168 @@
|
||||
#include "PatchFileIndex.hh"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <functional>
|
||||
#include <stdexcept>
|
||||
#include <phosg/Filesystem.hh>
|
||||
#include <phosg/Hash.hh>
|
||||
#include <phosg/Strings.hh>
|
||||
|
||||
#include "Loggers.hh"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
|
||||
PatchFileIndex::File::File(PatchFileIndex* index)
|
||||
: index(index), crc32(0), size(0) { }
|
||||
|
||||
std::shared_ptr<const std::string> PatchFileIndex::File::load_data() {
|
||||
if (!this->loaded_data) {
|
||||
string relative_path = join(this->path_directories, "/") + "/" + this->name;
|
||||
string full_path = this->index->root_dir + "/" + relative_path;
|
||||
patch_index_log.info("Loading data for %s", relative_path.c_str());
|
||||
this->loaded_data.reset(new string(load_file(full_path)));
|
||||
this->size = this->loaded_data->size();
|
||||
}
|
||||
return this->loaded_data;
|
||||
}
|
||||
|
||||
|
||||
|
||||
PatchFileIndex::PatchFileIndex(const string& root_dir)
|
||||
: root_dir(root_dir) {
|
||||
|
||||
string metadata_cache_filename = root_dir + "/.metadata-cache.json";
|
||||
shared_ptr<JSONObject> metadata_cache_json;
|
||||
try {
|
||||
string metadata_text = load_file(metadata_cache_filename);
|
||||
metadata_cache_json = JSONObject::parse(metadata_text);
|
||||
patch_index_log.info("Loaded patch metadata cache from %s", metadata_cache_filename.c_str());
|
||||
} catch (const exception& e) {
|
||||
metadata_cache_json = make_json_dict({});
|
||||
patch_index_log.warning("Cannot load patch metadata cache from %s: %s", metadata_cache_filename.c_str(), e.what());
|
||||
}
|
||||
|
||||
// Assuming it's rare for patch files to change, we skip writing the metadata
|
||||
// cache if no files were changed at all (which should usually be the case)
|
||||
bool should_write_metadata_cache = false;
|
||||
shared_ptr<JSONObject> new_metadata_cache_json = make_json_dict({});
|
||||
|
||||
vector<string> path_directories;
|
||||
function<void(const string&)> collect_dir = [&](const string& dir) -> void {
|
||||
path_directories.emplace_back(dir);
|
||||
|
||||
string relative_dirs = join(path_directories, "/");
|
||||
string full_dir_path = root_dir + '/' + relative_dirs;
|
||||
patch_index_log.info("Listing directory %s", full_dir_path.c_str());
|
||||
|
||||
for (const auto& item : list_directory(full_dir_path)) {
|
||||
// Skip invisible files (e.g. .DS_Store on macOS)
|
||||
if (starts_with(item, ".")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
string relative_item_path = relative_dirs + '/' + item;
|
||||
string full_item_path = root_dir + '/' + relative_item_path;
|
||||
if (isdir(full_item_path)) {
|
||||
collect_dir(item);
|
||||
} else if (isfile(full_item_path)) {
|
||||
|
||||
auto st = stat(full_item_path);
|
||||
|
||||
shared_ptr<File> f(new File(this));
|
||||
f->path_directories = path_directories;
|
||||
f->name = item;
|
||||
|
||||
string compute_crc32s_message; // If not empty, should compute crc32s
|
||||
shared_ptr<JSONObject> cache_item_json;
|
||||
try {
|
||||
cache_item_json = metadata_cache_json->at(relative_item_path);
|
||||
auto cache_item = metadata_cache_json->at(relative_item_path)->as_list();
|
||||
uint64_t cached_size = cache_item.at(0)->as_int();
|
||||
uint64_t cached_mtime = cache_item.at(1)->as_int();
|
||||
if (static_cast<uint64_t>(st.st_mtime) != cached_mtime) {
|
||||
throw runtime_error("file has been modified");
|
||||
}
|
||||
if (static_cast<uint64_t>(st.st_size) != cached_size) {
|
||||
throw runtime_error("file size has changed");
|
||||
}
|
||||
f->size = cached_size;
|
||||
f->crc32 = cache_item.at(2)->as_int();
|
||||
for (const auto& chunk_crc32_json : cache_item.at(3)->as_list()) {
|
||||
f->chunk_crcs.emplace_back(chunk_crc32_json->as_int());
|
||||
}
|
||||
|
||||
} catch (const exception& e) {
|
||||
compute_crc32s_message = e.what();
|
||||
}
|
||||
|
||||
if (!compute_crc32s_message.empty()) {
|
||||
auto data = f->load_data(); // Sets f->size
|
||||
f->crc32 = crc32(data->data(), f->size);
|
||||
for (size_t x = 0; x < data->size(); x += 0x4000) {
|
||||
size_t chunk_bytes = min<size_t>(f->size - x, 0x4000);
|
||||
f->chunk_crcs.emplace_back(::crc32(data->data() + x, chunk_bytes));
|
||||
}
|
||||
|
||||
// File was modified or cache item was missing; make a new cache item
|
||||
vector<shared_ptr<JSONObject>> chunk_crcs_item;
|
||||
for (uint32_t chunk_crc : f->chunk_crcs) {
|
||||
chunk_crcs_item.emplace_back(make_json_int(chunk_crc));
|
||||
}
|
||||
vector<shared_ptr<JSONObject>> new_cache_item({
|
||||
make_json_int(f->size),
|
||||
make_json_int(st.st_mtime),
|
||||
make_json_int(f->crc32),
|
||||
make_json_list(move(chunk_crcs_item)),
|
||||
});
|
||||
new_metadata_cache_json->as_dict().emplace(
|
||||
relative_item_path, make_json_list(move(new_cache_item)));
|
||||
|
||||
} else {
|
||||
// File was not modified and cache item was valid; just use the
|
||||
// existing cache item
|
||||
new_metadata_cache_json->as_dict().emplace(
|
||||
relative_item_path, cache_item_json);
|
||||
}
|
||||
|
||||
this->files_by_patch_order.emplace_back(f);
|
||||
this->files_by_name.emplace(relative_item_path, f);
|
||||
if (compute_crc32s_message.empty()) {
|
||||
patch_index_log.info("Added file %s (%" PRIu32 " bytes; %zu chunks; %08" PRIX32 " from cache)",
|
||||
full_item_path.c_str(), f->size, f->chunk_crcs.size(), f->crc32);
|
||||
} else {
|
||||
patch_index_log.info("Added file %s (%" PRIu32 " bytes; %zu chunks; %08" PRIX32 " [%s])",
|
||||
full_item_path.c_str(), f->size, f->chunk_crcs.size(), f->crc32, compute_crc32s_message.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
path_directories.pop_back();
|
||||
};
|
||||
|
||||
collect_dir(".");
|
||||
|
||||
if (should_write_metadata_cache) {
|
||||
try {
|
||||
save_file(metadata_cache_filename, new_metadata_cache_json->serialize());
|
||||
patch_index_log.info("Saved patch metadata cache to %s", metadata_cache_filename.c_str());
|
||||
} catch (const exception& e) {
|
||||
patch_index_log.warning("Cannot save patch metadata cache to %s: %s", metadata_cache_filename.c_str(), e.what());
|
||||
}
|
||||
} else {
|
||||
patch_index_log.info("No files were modified; skipping metadata cache update");
|
||||
}
|
||||
}
|
||||
|
||||
const vector<shared_ptr<PatchFileIndex::File>>&
|
||||
PatchFileIndex::all_files() const {
|
||||
return this->files_by_patch_order;
|
||||
}
|
||||
|
||||
shared_ptr<PatchFileIndex::File> PatchFileIndex::get(
|
||||
const string& filename) const {
|
||||
return this->files_by_name.at(filename);
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
#pragma once
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
|
||||
|
||||
struct PatchFileIndex {
|
||||
explicit PatchFileIndex(const std::string& root_dir);
|
||||
|
||||
struct File {
|
||||
PatchFileIndex* index;
|
||||
std::vector<std::string> path_directories;
|
||||
std::string name;
|
||||
std::shared_ptr<const std::string> loaded_data;
|
||||
std::vector<uint32_t> chunk_crcs;
|
||||
uint32_t crc32;
|
||||
uint32_t size;
|
||||
|
||||
explicit File(PatchFileIndex* index);
|
||||
std::shared_ptr<const std::string> load_data();
|
||||
};
|
||||
|
||||
const std::vector<std::shared_ptr<File>>& all_files() const;
|
||||
std::shared_ptr<File> get(const std::string& filename) const;
|
||||
|
||||
private:
|
||||
std::vector<std::shared_ptr<File>> files_by_patch_order;
|
||||
std::unordered_map<std::string, std::shared_ptr<File>> files_by_name;
|
||||
std::string root_dir;
|
||||
};
|
||||
|
||||
struct PatchFileChecksumRequest {
|
||||
std::shared_ptr<PatchFileIndex::File> file;
|
||||
uint32_t crc32;
|
||||
uint32_t size;
|
||||
bool response_received;
|
||||
|
||||
explicit PatchFileChecksumRequest(std::shared_ptr<PatchFileIndex::File> file)
|
||||
: file(file), crc32(0), size(0), response_received(false) { }
|
||||
inline bool needs_update() const {
|
||||
return !this->response_received ||
|
||||
(this->crc32 != this->file->crc32) ||
|
||||
(this->size != this->file->size);
|
||||
}
|
||||
};
|
||||
+249
-92
@@ -6,10 +6,13 @@
|
||||
|
||||
#include <stdexcept>
|
||||
#include <phosg/Filesystem.hh>
|
||||
#include <phosg/Hash.hh>
|
||||
|
||||
#include "FileContentsCache.hh"
|
||||
#include "Loggers.hh"
|
||||
#include "StaticGameData.hh"
|
||||
#include "Text.hh"
|
||||
#include "Version.hh"
|
||||
#include "StaticGameData.hh"
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -25,10 +28,14 @@ static const string ACCOUNT_FILE_SIGNATURE =
|
||||
|
||||
|
||||
|
||||
static FileContentsCache player_files_cache(300 * 1000 * 1000);
|
||||
|
||||
|
||||
|
||||
PlayerStats::PlayerStats() noexcept
|
||||
: atp(0), mst(0), evp(0), hp(0), dfp(0), ata(0), lck(0) { }
|
||||
|
||||
PlayerDispDataPCGC::PlayerDispDataPCGC() noexcept
|
||||
PlayerDispDataDCPCV3::PlayerDispDataDCPCV3() noexcept
|
||||
: level(0),
|
||||
experience(0),
|
||||
meseta(0),
|
||||
@@ -52,8 +59,8 @@ PlayerDispDataPCGC::PlayerDispDataPCGC() noexcept
|
||||
proportion_x(0),
|
||||
proportion_y(0) { }
|
||||
|
||||
void PlayerDispDataPCGC::enforce_pc_limits() {
|
||||
// PC has fewer classes, so we'll substitute some here
|
||||
void PlayerDispDataDCPCV3::enforce_v2_limits() {
|
||||
// V1/V2 have fewer classes, so we'll substitute some here
|
||||
if (this->char_class == 11) {
|
||||
this->char_class = 0; // FOmar -> HUmar
|
||||
} else if (this->char_class == 10) {
|
||||
@@ -71,7 +78,7 @@ void PlayerDispDataPCGC::enforce_pc_limits() {
|
||||
this->version = 2;
|
||||
}
|
||||
|
||||
PlayerDispDataBB PlayerDispDataPCGC::to_bb() const {
|
||||
PlayerDispDataBB PlayerDispDataDCPCV3::to_bb() const {
|
||||
PlayerDispDataBB bb;
|
||||
bb.stats.atp = this->stats.atp;
|
||||
bb.stats.mst = this->stats.mst;
|
||||
@@ -137,43 +144,43 @@ PlayerDispDataBB::PlayerDispDataBB() noexcept
|
||||
proportion_x(0),
|
||||
proportion_y(0) { }
|
||||
|
||||
PlayerDispDataPCGC PlayerDispDataBB::to_pcgc() const {
|
||||
PlayerDispDataPCGC pcgc;
|
||||
pcgc.stats.atp = this->stats.atp;
|
||||
pcgc.stats.mst = this->stats.mst;
|
||||
pcgc.stats.evp = this->stats.evp;
|
||||
pcgc.stats.hp = this->stats.hp;
|
||||
pcgc.stats.dfp = this->stats.dfp;
|
||||
pcgc.stats.ata = this->stats.ata;
|
||||
pcgc.stats.lck = this->stats.lck;
|
||||
pcgc.unknown_a1 = this->unknown_a1;
|
||||
pcgc.level = this->level;
|
||||
pcgc.experience = this->experience;
|
||||
pcgc.meseta = this->meseta;
|
||||
pcgc.unknown_a2 = this->unknown_a2;
|
||||
pcgc.name_color = this->name_color;
|
||||
pcgc.extra_model = this->extra_model;
|
||||
pcgc.unused = this->unused;
|
||||
pcgc.name_color_checksum = this->name_color_checksum;
|
||||
pcgc.section_id = this->section_id;
|
||||
pcgc.char_class = this->char_class;
|
||||
pcgc.v2_flags = this->v2_flags;
|
||||
pcgc.version = this->version;
|
||||
pcgc.v1_flags = this->v1_flags;
|
||||
pcgc.costume = this->costume;
|
||||
pcgc.skin = this->skin;
|
||||
pcgc.face = this->face;
|
||||
pcgc.head = this->head;
|
||||
pcgc.hair = this->hair;
|
||||
pcgc.hair_r = this->hair_r;
|
||||
pcgc.hair_g = this->hair_g;
|
||||
pcgc.hair_b = this->hair_b;
|
||||
pcgc.proportion_x = this->proportion_x;
|
||||
pcgc.proportion_y = this->proportion_y;
|
||||
pcgc.name = remove_language_marker(this->name);
|
||||
pcgc.config = this->config;
|
||||
pcgc.technique_levels = this->technique_levels;
|
||||
return pcgc;
|
||||
PlayerDispDataDCPCV3 PlayerDispDataBB::to_dcpcv3() const {
|
||||
PlayerDispDataDCPCV3 ret;
|
||||
ret.stats.atp = this->stats.atp;
|
||||
ret.stats.mst = this->stats.mst;
|
||||
ret.stats.evp = this->stats.evp;
|
||||
ret.stats.hp = this->stats.hp;
|
||||
ret.stats.dfp = this->stats.dfp;
|
||||
ret.stats.ata = this->stats.ata;
|
||||
ret.stats.lck = this->stats.lck;
|
||||
ret.unknown_a1 = this->unknown_a1;
|
||||
ret.level = this->level;
|
||||
ret.experience = this->experience;
|
||||
ret.meseta = this->meseta;
|
||||
ret.unknown_a2 = this->unknown_a2;
|
||||
ret.name_color = this->name_color;
|
||||
ret.extra_model = this->extra_model;
|
||||
ret.unused = this->unused;
|
||||
ret.name_color_checksum = this->name_color_checksum;
|
||||
ret.section_id = this->section_id;
|
||||
ret.char_class = this->char_class;
|
||||
ret.v2_flags = this->v2_flags;
|
||||
ret.version = this->version;
|
||||
ret.v1_flags = this->v1_flags;
|
||||
ret.costume = this->costume;
|
||||
ret.skin = this->skin;
|
||||
ret.face = this->face;
|
||||
ret.head = this->head;
|
||||
ret.hair = this->hair;
|
||||
ret.hair_r = this->hair_r;
|
||||
ret.hair_g = this->hair_g;
|
||||
ret.hair_b = this->hair_b;
|
||||
ret.proportion_x = this->proportion_x;
|
||||
ret.proportion_y = this->proportion_y;
|
||||
ret.name = remove_language_marker(this->name);
|
||||
ret.config = this->config;
|
||||
ret.technique_levels = this->technique_levels;
|
||||
return ret;
|
||||
}
|
||||
|
||||
PlayerDispDataBBPreview PlayerDispDataBB::to_preview() const {
|
||||
@@ -202,7 +209,7 @@ PlayerDispDataBBPreview PlayerDispDataBB::to_preview() const {
|
||||
pre.proportion_x = this->proportion_x;
|
||||
pre.proportion_y = this->proportion_y;
|
||||
pre.name = this->name;
|
||||
pre.play_time = 0; // TODO: Store this somewhere and return it here
|
||||
pre.play_time = this->play_time;
|
||||
return pre;
|
||||
}
|
||||
|
||||
@@ -233,6 +240,28 @@ void PlayerDispDataBB::apply_preview(const PlayerDispDataBBPreview& pre) {
|
||||
this->name = pre.name;
|
||||
}
|
||||
|
||||
void PlayerDispDataBB::apply_dressing_room(const PlayerDispDataBBPreview& pre) {
|
||||
this->name_color = pre.name_color;
|
||||
this->extra_model = pre.extra_model;
|
||||
this->name_color_checksum = pre.name_color_checksum;
|
||||
this->section_id = pre.section_id;
|
||||
this->char_class = pre.char_class;
|
||||
this->v2_flags = pre.v2_flags;
|
||||
this->version = pre.version;
|
||||
this->v1_flags = pre.v1_flags;
|
||||
this->costume = pre.costume;
|
||||
this->skin = pre.skin;
|
||||
this->face = pre.face;
|
||||
this->head = pre.head;
|
||||
this->hair = pre.hair;
|
||||
this->hair_r = pre.hair_r;
|
||||
this->hair_g = pre.hair_g;
|
||||
this->hair_b = pre.hair_b;
|
||||
this->proportion_x = pre.proportion_x;
|
||||
this->proportion_y = pre.proportion_y;
|
||||
this->name = pre.name;
|
||||
}
|
||||
|
||||
|
||||
|
||||
PlayerDispDataBBPreview::PlayerDispDataBBPreview() noexcept
|
||||
@@ -261,29 +290,68 @@ PlayerDispDataBBPreview::PlayerDispDataBBPreview() noexcept
|
||||
|
||||
|
||||
|
||||
GuildCardGC::GuildCardGC() noexcept
|
||||
: player_tag(0), serial_number(0), reserved1(1), reserved2(1), section_id(0), char_class(0) { }
|
||||
GuildCardV3::GuildCardV3() noexcept
|
||||
: player_tag(0),
|
||||
guild_card_number(0),
|
||||
present(0),
|
||||
language(0),
|
||||
section_id(0),
|
||||
char_class(0) { }
|
||||
|
||||
GuildCardBB::GuildCardBB() noexcept
|
||||
: serial_number(0), reserved1(1), reserved2(1), section_id(0), char_class(0) { }
|
||||
: guild_card_number(0),
|
||||
present(0),
|
||||
language(0),
|
||||
section_id(0),
|
||||
char_class(0) { }
|
||||
|
||||
void GuildCardBB::clear() {
|
||||
this->guild_card_number = 0;
|
||||
this->name.clear(0);
|
||||
this->team_name.clear(0);
|
||||
this->description.clear(0);
|
||||
this->present = 0;
|
||||
this->language = 0;
|
||||
this->section_id = 0;
|
||||
this->char_class = 0;
|
||||
}
|
||||
|
||||
void GuildCardEntryBB::clear() {
|
||||
this->data.clear();
|
||||
this->unknown_a1.clear(0);
|
||||
}
|
||||
|
||||
uint32_t GuildCardFileBB::checksum() const {
|
||||
return crc32(this, sizeof(*this));
|
||||
}
|
||||
|
||||
|
||||
|
||||
void PlayerBank::load(const string& filename) {
|
||||
*this = load_object_file<PlayerBank>(filename);
|
||||
*this = player_files_cache.get_obj_or_load<PlayerBank>(filename).obj;
|
||||
for (uint32_t x = 0; x < this->num_items; x++) {
|
||||
this->items[x].data.id = 0x0F010000 + x;
|
||||
}
|
||||
}
|
||||
|
||||
void PlayerBank::save(const string& filename) const {
|
||||
save_file(filename, this, sizeof(*this));
|
||||
void PlayerBank::save(const string& filename, bool save_to_filesystem) const {
|
||||
player_files_cache.replace(filename, this, sizeof(*this));
|
||||
if (save_to_filesystem) {
|
||||
save_file(filename, this, sizeof(*this));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
ClientGameData::ClientGameData()
|
||||
: last_play_time_update(0),
|
||||
guild_card_number(0),
|
||||
should_update_play_time(false),
|
||||
bb_player_index(0),
|
||||
should_save(true) { }
|
||||
|
||||
ClientGameData::~ClientGameData() {
|
||||
if (!this->bb_username.empty()) {
|
||||
if (this->account_data.get()) {
|
||||
@@ -380,58 +448,89 @@ void ClientGameData::load_account_data() {
|
||||
shared_ptr<SavedAccountDataBB> data;
|
||||
try {
|
||||
data.reset(new SavedAccountDataBB(
|
||||
load_object_file<SavedAccountDataBB>(filename)));
|
||||
player_files_cache.get_obj_or_load<SavedAccountDataBB>(filename).obj));
|
||||
if (data->signature != ACCOUNT_FILE_SIGNATURE) {
|
||||
throw runtime_error("account data header is incorrect");
|
||||
}
|
||||
player_data_log.info("Loaded account data file %s", filename.c_str());
|
||||
|
||||
} catch (const exception& e) {
|
||||
log(INFO, "[BB/Account] No account data for %s; using default",
|
||||
this->bb_username.c_str());
|
||||
player_data_log.info("Cannot load account data for %s (%s); using default",
|
||||
this->bb_username.c_str(), e.what());
|
||||
player_files_cache.delete_key(filename);
|
||||
data.reset(new SavedAccountDataBB(
|
||||
load_object_file<SavedAccountDataBB>("system/players/default.nsa")));
|
||||
player_files_cache.get_obj_or_load<SavedAccountDataBB>(
|
||||
"system/players/default.nsa").obj));
|
||||
if (data->signature != ACCOUNT_FILE_SIGNATURE) {
|
||||
throw runtime_error("default account data header is incorrect");
|
||||
}
|
||||
player_data_log.info("Loaded default account data file");
|
||||
}
|
||||
|
||||
this->account_data = data;
|
||||
log(INFO, "Loaded account data file %s", filename.c_str());
|
||||
}
|
||||
|
||||
void ClientGameData::save_account_data() const {
|
||||
if (!this->account_data.get()) {
|
||||
throw logic_error("save_account_data called when no account data loaded");
|
||||
}
|
||||
string filename = this->account_data_filename();
|
||||
save_file(filename, this->account_data.get(), sizeof(SavedAccountDataBB));
|
||||
log(INFO, "Saved account data file %s", filename.c_str());
|
||||
player_files_cache.replace(filename, this->account_data.get(), sizeof(SavedAccountDataBB));
|
||||
if (this->should_save) {
|
||||
save_file(filename, this->account_data.get(), sizeof(SavedAccountDataBB));
|
||||
player_data_log.info("Saved account data file %s to filesystem", filename.c_str());
|
||||
} else {
|
||||
player_data_log.info("Saved account data file %s to cache only", filename.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void ClientGameData::load_player_data() {
|
||||
this->last_play_time_update = now();
|
||||
string filename = this->player_data_filename();
|
||||
shared_ptr<SavedPlayerDataBB> data(new SavedPlayerDataBB(
|
||||
load_object_file<SavedPlayerDataBB>(filename)));
|
||||
player_files_cache.get_obj_or_load<SavedPlayerDataBB>(filename).obj));
|
||||
if (data->signature != PLAYER_FILE_SIGNATURE) {
|
||||
player_files_cache.delete_key(filename);
|
||||
throw runtime_error("player data header is incorrect");
|
||||
}
|
||||
this->player_data = data;
|
||||
log(INFO, "Loaded player data file %s", filename.c_str());
|
||||
player_data_log.info("Loaded player data file %s", filename.c_str());
|
||||
}
|
||||
|
||||
void ClientGameData::save_player_data() const {
|
||||
void ClientGameData::save_player_data() {
|
||||
if (!this->player_data.get()) {
|
||||
throw logic_error("save_player_data called when no player data loaded");
|
||||
}
|
||||
if (this->should_update_play_time) {
|
||||
// This is slightly inaccurate, since fractions of a second are truncated
|
||||
// off each time we save. I'm lazy, so insert shrug emoji here.
|
||||
uint64_t t = now();
|
||||
uint64_t seconds = (t - this->last_play_time_update) / 1000000;
|
||||
this->player_data->disp.play_time += seconds;
|
||||
player_data_log.info("Added %" PRIu64 " seconds to play time", seconds);
|
||||
this->last_play_time_update = t;
|
||||
}
|
||||
string filename = this->player_data_filename();
|
||||
save_file(filename, this->player_data.get(), sizeof(SavedPlayerDataBB));
|
||||
log(INFO, "Saved player data file %s", filename.c_str());
|
||||
player_files_cache.replace(filename, this->player_data.get(), sizeof(SavedPlayerDataBB));
|
||||
if (this->should_save) {
|
||||
save_file(filename, this->player_data.get(), sizeof(SavedPlayerDataBB));
|
||||
player_data_log.info("Saved player data file %s to filesystem", filename.c_str());
|
||||
} else {
|
||||
player_data_log.info("Saved player data file %s to cache only", filename.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void ClientGameData::import_player(const PSOPlayerDataPC& pc) {
|
||||
void ClientGameData::import_player(const PSOPlayerDataDCPC& pd) {
|
||||
auto player = this->player();
|
||||
player->inventory = pc.inventory;
|
||||
player->disp = pc.disp.to_bb();
|
||||
player->inventory = pd.inventory;
|
||||
player->disp = pd.disp.to_bb();
|
||||
// TODO: Add these fields to the command structure so we can parse them
|
||||
// info_board = pc.info_board;
|
||||
// blocked_senders = pc.blocked_senders;
|
||||
// auto_reply = pc.auto_reply;
|
||||
// info_board = pd.info_board;
|
||||
// blocked_senders = pd.blocked_senders;
|
||||
// auto_reply = pd.auto_reply;
|
||||
}
|
||||
|
||||
void ClientGameData::import_player(const PSOPlayerDataGC& gc) {
|
||||
void ClientGameData::import_player(const PSOPlayerDataV3& gc) {
|
||||
auto account = this->account();
|
||||
auto player = this->player();
|
||||
player->inventory = gc.inventory;
|
||||
@@ -441,7 +540,7 @@ void ClientGameData::import_player(const PSOPlayerDataGC& gc) {
|
||||
if (gc.auto_reply_enabled) {
|
||||
player->auto_reply = gc.auto_reply;
|
||||
} else {
|
||||
player->auto_reply.clear();
|
||||
player->auto_reply.clear(0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -455,7 +554,7 @@ void ClientGameData::import_player(const PSOPlayerDataBB& bb) {
|
||||
if (bb.auto_reply_enabled) {
|
||||
player->auto_reply = bb.auto_reply;
|
||||
} else {
|
||||
player->auto_reply.clear();
|
||||
player->auto_reply.clear(0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -466,14 +565,14 @@ PlayerBB ClientGameData::export_player_bb() {
|
||||
PlayerBB ret;
|
||||
ret.inventory = player->inventory;
|
||||
ret.disp = player->disp;
|
||||
ret.unknown.clear();
|
||||
ret.unknown.clear(0);
|
||||
ret.option_flags = account->option_flags;
|
||||
ret.quest_data1 = player->quest_data1;
|
||||
ret.bank = player->bank;
|
||||
ret.serial_number = this->serial_number;
|
||||
ret.guild_card_number = this->guild_card_number;
|
||||
ret.name = player->disp.name;
|
||||
ret.team_name = account->team_name;
|
||||
ret.guild_card_desc = player->guild_card_desc;
|
||||
ret.guild_card_description = player->guild_card_description;
|
||||
ret.reserved1 = 0;
|
||||
ret.reserved2 = 0;
|
||||
ret.section_id = player->disp.section_id;
|
||||
@@ -483,10 +582,10 @@ PlayerBB ClientGameData::export_player_bb() {
|
||||
ret.shortcuts = account->shortcuts;
|
||||
ret.auto_reply = player->auto_reply;
|
||||
ret.info_board = player->info_board;
|
||||
ret.unknown5.clear();
|
||||
ret.unknown5.clear(0);
|
||||
ret.challenge_data = player->challenge_data;
|
||||
ret.tech_menu_config = player->tech_menu_config;
|
||||
ret.unknown6.clear();
|
||||
ret.unknown6.clear(0);
|
||||
ret.quest_data2 = player->quest_data2;
|
||||
ret.key_config = account->key_config;
|
||||
return ret;
|
||||
@@ -494,15 +593,49 @@ PlayerBB ClientGameData::export_player_bb() {
|
||||
|
||||
|
||||
|
||||
PlayerLobbyDataPC::PlayerLobbyDataPC() noexcept
|
||||
: player_tag(0), guild_card(0), ip_address(0), client_id(0) { }
|
||||
void PlayerLobbyDataPC::clear() {
|
||||
this->player_tag = 0;
|
||||
this->guild_card = 0;
|
||||
this->ip_address = 0;
|
||||
this->client_id = 0;
|
||||
ptext<char16_t, 0x10> name;
|
||||
}
|
||||
|
||||
PlayerLobbyDataGC::PlayerLobbyDataGC() noexcept
|
||||
: player_tag(0), guild_card(0), ip_address(0), client_id(0) { }
|
||||
void PlayerLobbyDataDCGC::clear() {
|
||||
this->player_tag = 0;
|
||||
this->guild_card = 0;
|
||||
this->ip_address = 0;
|
||||
this->client_id = 0;
|
||||
ptext<char, 0x10> name;
|
||||
}
|
||||
|
||||
PlayerLobbyDataBB::PlayerLobbyDataBB() noexcept
|
||||
: player_tag(0), guild_card(0), ip_address(0), client_id(0), unknown2(0) { }
|
||||
void XBNetworkLocation::clear() {
|
||||
this->internal_ipv4_address = 0;
|
||||
this->external_ipv4_address = 0;
|
||||
this->port = 0;
|
||||
this->mac_address.clear(0);
|
||||
this->unknown_a1.clear(0);
|
||||
this->account_id = 0;
|
||||
this->unknown_a2.clear(0);
|
||||
}
|
||||
|
||||
void PlayerLobbyDataXB::clear() {
|
||||
this->player_tag = 0;
|
||||
this->guild_card = 0;
|
||||
this->netloc.clear();
|
||||
this->client_id = 0;
|
||||
this->name.clear(0);
|
||||
}
|
||||
|
||||
void PlayerLobbyDataBB::clear() {
|
||||
this->player_tag = 0;
|
||||
this->guild_card = 0;
|
||||
this->ip_address = 0;
|
||||
this->unknown_a1.clear(0);
|
||||
this->client_id = 0;
|
||||
this->name.clear(0);
|
||||
this->unknown_a2 = 0;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -510,6 +643,10 @@ PlayerLobbyDataBB::PlayerLobbyDataBB() noexcept
|
||||
constexpr uint32_t MESETA_IDENTIFIER = 0x00040000;
|
||||
|
||||
ItemData::ItemData() {
|
||||
this->clear();
|
||||
}
|
||||
|
||||
void ItemData::clear() {
|
||||
this->data1d[0] = 0;
|
||||
this->data1d[1] = 0;
|
||||
this->data1d[2] = 0;
|
||||
@@ -518,6 +655,11 @@ ItemData::ItemData() {
|
||||
}
|
||||
|
||||
uint32_t ItemData::primary_identifier() const {
|
||||
// The game treats any item starting with 04 as Meseta, and ignores the rest
|
||||
// of data1 (the value is in data2)
|
||||
if (this->data1[0] == 0x04) {
|
||||
return 0x00040000;
|
||||
}
|
||||
if (this->data1[0] == 0x03 && this->data1[1] == 0x02) {
|
||||
return 0x00030200; // Tech disk (data1[2] is level, so omit it)
|
||||
} else if (this->data1[0] == 0x02) {
|
||||
@@ -527,22 +669,34 @@ uint32_t ItemData::primary_identifier() const {
|
||||
}
|
||||
}
|
||||
|
||||
PlayerInventoryItem::PlayerInventoryItem()
|
||||
: equip_flags(0x0000), tech_flag(0x0000), game_flags(0x00000000), data() { }
|
||||
|
||||
PlayerInventoryItem::PlayerInventoryItem(const PlayerBankItem& src)
|
||||
: tech_flag(0x0001), data(src.data) {
|
||||
this->equip_flags = (this->data.data1[0] > 2) ? 0x0044 : 0x0050;
|
||||
PlayerInventoryItem::PlayerInventoryItem() {
|
||||
this->clear();
|
||||
}
|
||||
|
||||
PlayerBankItem::PlayerBankItem()
|
||||
: data(), amount(0), show_flags(0) { }
|
||||
PlayerInventoryItem::PlayerInventoryItem(const PlayerBankItem& src)
|
||||
: present(1), flags(0), data(src.data) { }
|
||||
|
||||
void PlayerInventoryItem::clear() {
|
||||
this->present = 0x00000000;
|
||||
this->flags = 0x00000000;
|
||||
this->data.clear();
|
||||
}
|
||||
|
||||
PlayerBankItem::PlayerBankItem() {
|
||||
this->clear();
|
||||
}
|
||||
|
||||
PlayerBankItem::PlayerBankItem(const PlayerInventoryItem& src)
|
||||
: data(src.data),
|
||||
amount(stack_size_for_item(this->data)),
|
||||
show_flags(1) { }
|
||||
|
||||
void PlayerBankItem::clear() {
|
||||
this->data.clear();
|
||||
this->amount = 0;
|
||||
this->show_flags = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
PlayerInventory::PlayerInventory()
|
||||
@@ -639,18 +793,21 @@ void PlayerBank::add_item(const PlayerBankItem& item) {
|
||||
// TODO: Eliminate code duplication between this function and the parallel
|
||||
// function in PlayerBank
|
||||
PlayerInventoryItem SavedPlayerDataBB::remove_item(
|
||||
uint32_t item_id, uint32_t amount) {
|
||||
uint32_t item_id, uint32_t amount, bool allow_meseta_overdraft) {
|
||||
PlayerInventoryItem ret;
|
||||
|
||||
// If we're removing meseta (signaled by an invalid item ID), then create a
|
||||
// meseta item.
|
||||
if (item_id == 0xFFFFFFFF) {
|
||||
if (amount > this->disp.meseta) {
|
||||
if (amount <= this->disp.meseta) {
|
||||
this->disp.meseta -= amount;
|
||||
} else if (allow_meseta_overdraft) {
|
||||
this->disp.meseta = 0;
|
||||
} else {
|
||||
throw out_of_range("player does not have enough meseta");
|
||||
}
|
||||
ret.data.data1[0] = 0x04;
|
||||
ret.data.data2d = amount;
|
||||
this->disp.meseta -= amount;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
+228
-102
@@ -5,16 +5,17 @@
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
#include <phosg/Encoding.hh>
|
||||
|
||||
#include "LevelTable.hh"
|
||||
#include "Version.hh"
|
||||
#include "Text.hh"
|
||||
#include "Episode3.hh"
|
||||
#include "Episode3/DataIndex.hh"
|
||||
|
||||
|
||||
|
||||
struct ItemData {
|
||||
struct ItemData { // 0x14 bytes
|
||||
union {
|
||||
uint8_t data1[12];
|
||||
le_uint16_t data1w[6];
|
||||
@@ -28,32 +29,34 @@ struct ItemData {
|
||||
} __attribute__((packed));
|
||||
|
||||
ItemData();
|
||||
void clear();
|
||||
|
||||
uint32_t primary_identifier() const;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct PlayerBankItem;
|
||||
|
||||
struct PlayerInventoryItem {
|
||||
le_uint16_t equip_flags;
|
||||
le_uint16_t tech_flag;
|
||||
le_uint32_t game_flags;
|
||||
struct PlayerInventoryItem { // 0x1C bytes
|
||||
le_uint32_t present;
|
||||
le_uint32_t flags; // 8 = equipped
|
||||
ItemData data;
|
||||
|
||||
PlayerInventoryItem();
|
||||
PlayerInventoryItem(const PlayerBankItem&);
|
||||
void clear();
|
||||
} __attribute__((packed));
|
||||
|
||||
struct PlayerBankItem {
|
||||
struct PlayerBankItem { // 0x18 bytes
|
||||
ItemData data;
|
||||
le_uint16_t amount;
|
||||
le_uint16_t show_flags;
|
||||
|
||||
PlayerBankItem();
|
||||
PlayerBankItem(const PlayerInventoryItem&);
|
||||
void clear();
|
||||
} __attribute__((packed));
|
||||
|
||||
struct PlayerInventory {
|
||||
struct PlayerInventory { // 0x34C bytes
|
||||
uint8_t num_items;
|
||||
uint8_t hp_materials_used;
|
||||
uint8_t tp_materials_used;
|
||||
@@ -65,13 +68,13 @@ struct PlayerInventory {
|
||||
size_t find_item(uint32_t item_id);
|
||||
} __attribute__((packed));
|
||||
|
||||
struct PlayerBank {
|
||||
struct PlayerBank { // 0x12C8 bytes
|
||||
le_uint32_t num_items;
|
||||
le_uint32_t meseta;
|
||||
PlayerBankItem items[200];
|
||||
|
||||
void load(const std::string& filename);
|
||||
void save(const std::string& filename) const;
|
||||
void save(const std::string& filename, bool save_to_filesystem) const;
|
||||
|
||||
bool switch_with_file(const std::string& save_filename,
|
||||
const std::string& load_filename);
|
||||
@@ -83,10 +86,23 @@ struct PlayerBank {
|
||||
|
||||
|
||||
|
||||
struct PendingItemTrade {
|
||||
uint8_t other_client_id;
|
||||
bool confirmed; // true if client has sent a D2 command
|
||||
std::vector<ItemData> items;
|
||||
};
|
||||
|
||||
struct PendingCardTrade {
|
||||
uint8_t other_client_id;
|
||||
bool confirmed; // true if client has sent an EE D2 command
|
||||
std::vector<std::pair<uint32_t, uint32_t>> card_to_count;
|
||||
};
|
||||
|
||||
|
||||
|
||||
struct PlayerDispDataBB;
|
||||
|
||||
// PC/GC player appearance and stats data
|
||||
struct PlayerDispDataPCGC { // 0xD0 in size
|
||||
struct PlayerDispDataDCPCV3 { // 0xD0 bytes
|
||||
PlayerStats stats;
|
||||
parray<uint8_t, 0x0A> unknown_a1;
|
||||
le_uint32_t level;
|
||||
@@ -120,9 +136,9 @@ struct PlayerDispDataPCGC { // 0xD0 in size
|
||||
// that has a fixed-size array. If we didn't define this constructor, the
|
||||
// trivial fields in that array's members would be uninitialized, and we could
|
||||
// send uninitialized memory to the client.
|
||||
PlayerDispDataPCGC() noexcept;
|
||||
PlayerDispDataDCPCV3() noexcept;
|
||||
|
||||
void enforce_pc_limits();
|
||||
void enforce_v2_limits();
|
||||
PlayerDispDataBB to_bb() const;
|
||||
} __attribute__((packed));
|
||||
|
||||
@@ -166,7 +182,7 @@ struct PlayerDispDataBB {
|
||||
le_uint32_t meseta;
|
||||
ptext<char, 0x10> guild_card;
|
||||
uint64_t unknown_a2;
|
||||
le_uint32_t name_color;
|
||||
le_uint32_t name_color; // ARGB8888
|
||||
uint8_t extra_model;
|
||||
parray<uint8_t, 0x0F> unused;
|
||||
le_uint32_t name_color_checksum;
|
||||
@@ -185,65 +201,77 @@ struct PlayerDispDataBB {
|
||||
le_uint16_t hair_b;
|
||||
le_float proportion_x;
|
||||
le_float proportion_y;
|
||||
ptext<char16_t, 0x10> name;
|
||||
ptext<char16_t, 0x0C> name;
|
||||
le_uint32_t play_time;
|
||||
uint32_t unknown_a3;
|
||||
parray<uint8_t, 0xE8> config;
|
||||
parray<uint8_t, 0x14> technique_levels;
|
||||
|
||||
PlayerDispDataBB() noexcept;
|
||||
|
||||
inline void enforce_pc_limits() { }
|
||||
PlayerDispDataPCGC to_pcgc() const;
|
||||
inline void enforce_v2_limits() { }
|
||||
PlayerDispDataDCPCV3 to_dcpcv3() const;
|
||||
PlayerDispDataBBPreview to_preview() const;
|
||||
void apply_preview(const PlayerDispDataBBPreview&);
|
||||
void apply_dressing_room(const PlayerDispDataBBPreview&);
|
||||
} __attribute__((packed));
|
||||
|
||||
|
||||
|
||||
struct GuildCardGC {
|
||||
// TODO: Is this the same for XB as it is for GC? (This struct is based on the
|
||||
// GC format)
|
||||
struct GuildCardV3 {
|
||||
le_uint32_t player_tag;
|
||||
le_uint32_t serial_number;
|
||||
le_uint32_t guild_card_number;
|
||||
ptext<char, 0x18> name;
|
||||
ptext<char, 0x6C> desc;
|
||||
uint8_t reserved1; // should be 1
|
||||
uint8_t reserved2; // should be 1
|
||||
ptext<char, 0x6C> description;
|
||||
uint8_t present; // should be 1
|
||||
uint8_t language;
|
||||
uint8_t section_id;
|
||||
uint8_t char_class;
|
||||
|
||||
GuildCardGC() noexcept;
|
||||
GuildCardV3() noexcept;
|
||||
} __attribute__((packed));
|
||||
|
||||
// BB guild card format
|
||||
struct GuildCardBB {
|
||||
le_uint32_t serial_number;
|
||||
le_uint32_t guild_card_number;
|
||||
ptext<char16_t, 0x18> name;
|
||||
ptext<char16_t, 0x10> teamname;
|
||||
ptext<char16_t, 0x58> desc;
|
||||
uint8_t reserved1; // should be 1
|
||||
uint8_t reserved2; // should be 1
|
||||
ptext<char16_t, 0x10> team_name;
|
||||
ptext<char16_t, 0x58> description;
|
||||
uint8_t present; // should be 1 if guild card entry exists
|
||||
uint8_t language;
|
||||
uint8_t section_id;
|
||||
uint8_t char_class;
|
||||
|
||||
GuildCardBB() noexcept;
|
||||
void clear();
|
||||
} __attribute__((packed));
|
||||
|
||||
// an entry in the BB guild card file
|
||||
struct GuildCardEntryBB {
|
||||
GuildCardBB data;
|
||||
parray<uint8_t, 0xB4> unknown;
|
||||
ptext<char16_t, 0x58> comment;
|
||||
parray<uint8_t, 0x4> unknown_a1;
|
||||
|
||||
void clear();
|
||||
} __attribute__((packed));
|
||||
|
||||
// the format of the BB guild card file
|
||||
struct GuildCardFileBB {
|
||||
parray<uint8_t, 0x1F84> unknown_a1;
|
||||
GuildCardEntryBB entry[0x0068]; // that's 104 of them in decimal
|
||||
parray<uint8_t, 0x01AC> unknown_a2;
|
||||
parray<uint8_t, 0x114> unknown_a1;
|
||||
GuildCardBB blocked[0x1C];
|
||||
parray<uint8_t, 0x180> unknown_a2;
|
||||
GuildCardEntryBB entries[0x69];
|
||||
|
||||
uint32_t checksum() const;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct KeyAndTeamConfigBB {
|
||||
parray<uint8_t, 0x0114> unknown_a1; // 0000
|
||||
parray<uint8_t, 0x016C> key_config; // 0114
|
||||
parray<uint8_t, 0x0038> joystick_config; // 0280
|
||||
le_uint32_t serial_number; // 02B8
|
||||
le_uint32_t guild_card_number; // 02B8
|
||||
le_uint32_t team_id; // 02BC
|
||||
le_uint64_t team_info; // 02C0
|
||||
le_uint16_t team_privilege_level; // 02C8
|
||||
@@ -256,69 +284,154 @@ struct KeyAndTeamConfigBB {
|
||||
|
||||
|
||||
struct PlayerLobbyDataPC {
|
||||
le_uint32_t player_tag;
|
||||
le_uint32_t guild_card;
|
||||
be_uint32_t ip_address;
|
||||
le_uint32_t client_id;
|
||||
le_uint32_t player_tag = 0;
|
||||
le_uint32_t guild_card = 0;
|
||||
// There's a strange behavior (bug? "feature"?) in Episode 3 where the start
|
||||
// button does nothing in the lobby (hence you can't "quit game") if the
|
||||
// client's IP address is zero. So, we fill it in with a fake nonzero value to
|
||||
// avoid this behavior, and to be consistent, we make IP addresses fake and
|
||||
// nonzero on all other versions too.
|
||||
be_uint32_t ip_address = 0x7F000001;
|
||||
le_uint32_t client_id = 0;
|
||||
ptext<char16_t, 0x10> name;
|
||||
|
||||
PlayerLobbyDataPC() noexcept;
|
||||
void clear();
|
||||
} __attribute__((packed));
|
||||
|
||||
struct PlayerLobbyDataGC {
|
||||
le_uint32_t player_tag;
|
||||
le_uint32_t guild_card;
|
||||
be_uint32_t ip_address;
|
||||
le_uint32_t client_id;
|
||||
struct PlayerLobbyDataDCGC {
|
||||
le_uint32_t player_tag = 0;
|
||||
le_uint32_t guild_card = 0;
|
||||
be_uint32_t ip_address = 0x7F000001;
|
||||
le_uint32_t client_id = 0;
|
||||
ptext<char, 0x10> name;
|
||||
|
||||
PlayerLobbyDataGC() noexcept;
|
||||
void clear();
|
||||
} __attribute__((packed));
|
||||
|
||||
struct XBNetworkLocation {
|
||||
le_uint32_t internal_ipv4_address = 0x0A0A0A0A;
|
||||
le_uint32_t external_ipv4_address = 0x23232323;
|
||||
le_uint16_t port = 9100;
|
||||
parray<uint8_t, 6> mac_address = 0x77;
|
||||
parray<le_uint32_t, 2> unknown_a1;
|
||||
le_uint64_t account_id = 0xFFFFFFFFFFFFFFFF;
|
||||
parray<le_uint32_t, 4> unknown_a2;
|
||||
|
||||
void clear();
|
||||
} __attribute__((packed));
|
||||
|
||||
struct PlayerLobbyDataXB {
|
||||
le_uint32_t player_tag = 0;
|
||||
le_uint32_t guild_card = 0;
|
||||
XBNetworkLocation netloc;
|
||||
le_uint32_t client_id = 0;
|
||||
ptext<char, 0x10> name;
|
||||
|
||||
void clear();
|
||||
} __attribute__((packed));
|
||||
|
||||
struct PlayerLobbyDataBB {
|
||||
le_uint32_t player_tag;
|
||||
le_uint32_t guild_card;
|
||||
be_uint32_t ip_address; // Guess - the official builds didn't use this, but all other versions have it
|
||||
le_uint32_t player_tag = 0;
|
||||
le_uint32_t guild_card = 0;
|
||||
// This field is a guess; the official builds didn't use this, but all other
|
||||
// versions have it
|
||||
be_uint32_t ip_address = 0x7F000001;
|
||||
parray<uint8_t, 0x10> unknown_a1;
|
||||
le_uint32_t client_id;
|
||||
le_uint32_t client_id = 0;
|
||||
ptext<char16_t, 0x10> name;
|
||||
le_uint32_t unknown2;
|
||||
le_uint32_t unknown_a2 = 0;
|
||||
|
||||
PlayerLobbyDataBB() noexcept;
|
||||
void clear();
|
||||
} __attribute__((packed));
|
||||
|
||||
|
||||
|
||||
struct PSOPlayerDataPC { // For command 61
|
||||
PlayerInventory inventory;
|
||||
PlayerDispDataPCGC disp;
|
||||
struct PlayerChallengeDataV3 {
|
||||
le_uint32_t client_id;
|
||||
struct {
|
||||
le_uint16_t unknown_a1;
|
||||
parray<uint8_t, 2> unknown_a2; // Possibly unused
|
||||
parray<le_uint32_t, 0x17> unknown_a3;
|
||||
struct {
|
||||
parray<uint8_t, 4> unknown_a1;
|
||||
le_uint16_t unknown_a2;
|
||||
parray<uint8_t, 2> unknown_a3;
|
||||
parray<le_uint32_t, 5> unknown_a4;
|
||||
parray<uint8_t, 0x34> unknown_a5;
|
||||
} __attribute__((packed)) unknown_a4; // 0x50 bytes
|
||||
struct {
|
||||
parray<uint8_t, 4> unknown_a1;
|
||||
parray<le_uint32_t, 3> unknown_a2;
|
||||
} __attribute__((packed)) unknown_a5; // 0x10 bytes
|
||||
struct UnknownPair {
|
||||
le_uint32_t unknown_a1;
|
||||
le_uint32_t unknown_a2;
|
||||
} __attribute__((packed));
|
||||
parray<UnknownPair, 3> unknown_a6; // 0x18 bytes
|
||||
parray<uint8_t, 0x28> unknown_a7;
|
||||
} __attribute__((packed)) unknown_a1; // 0x100 bytes
|
||||
// On Episode 3, unknown_a2[0] is win count, [1] is loss count, and [4] is
|
||||
// disconnect count
|
||||
parray<le_uint16_t, 8> unknown_a2;
|
||||
parray<le_uint32_t, 2> unknown_a3;
|
||||
} __attribute__((packed)); // 0x11C bytes
|
||||
|
||||
struct PlayerChallengeDataBB {
|
||||
le_uint32_t client_id;
|
||||
parray<uint8_t, 0x158> unknown_a1;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct PSOPlayerDataGC { // For command 61
|
||||
|
||||
|
||||
template <typename ItemIDT>
|
||||
struct ChoiceSearchConfig {
|
||||
// 0 = enabled, 1 = disabled. Unused for command C3
|
||||
le_uint32_t choice_search_disabled = 0;
|
||||
struct Entry {
|
||||
ItemIDT parent_category_id = 0;
|
||||
ItemIDT category_id = 0;
|
||||
} __attribute__((packed));
|
||||
parray<Entry, 5> entries;
|
||||
} __attribute__((packed));
|
||||
|
||||
|
||||
|
||||
struct PSOPlayerDataDCPC { // For command 61
|
||||
PlayerInventory inventory;
|
||||
PlayerDispDataPCGC disp;
|
||||
parray<uint8_t, 0x134> unknown;
|
||||
PlayerDispDataDCPCV3 disp;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct PSOPlayerDataV3 { // For command 61
|
||||
PlayerInventory inventory;
|
||||
PlayerDispDataDCPCV3 disp;
|
||||
PlayerChallengeDataV3 challenge_data;
|
||||
ChoiceSearchConfig<le_uint16_t> choice_search_config;
|
||||
ptext<char, 0xAC> info_board;
|
||||
parray<le_uint32_t, 0x1E> blocked_senders;
|
||||
le_uint32_t auto_reply_enabled;
|
||||
// The auto-reply message can be up to 0x200 bytes. If it's shorter than that,
|
||||
// the client truncates the command after the first zero byte (rounded up to
|
||||
// the next 4-byte boundary).
|
||||
char auto_reply[0];
|
||||
} __attribute__((packed));
|
||||
|
||||
struct PSOPlayerDataGCEp3 { // For command 61
|
||||
PlayerInventory inventory;
|
||||
PlayerDispDataPCGC disp;
|
||||
parray<uint8_t, 0x134> unknown;
|
||||
PlayerDispDataDCPCV3 disp;
|
||||
PlayerChallengeDataV3 challenge_data;
|
||||
ChoiceSearchConfig<le_uint16_t> choice_search_config;
|
||||
ptext<char, 0xAC> info_board;
|
||||
parray<le_uint32_t, 0x1E> blocked_senders;
|
||||
le_uint32_t auto_reply_enabled;
|
||||
char auto_reply[0xAC];
|
||||
Ep3Config ep3_config;
|
||||
ptext<char, 0xAC> auto_reply;
|
||||
Episode3::PlayerConfig ep3_config;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct PSOPlayerDataBB { // For command 61
|
||||
PlayerInventory inventory;
|
||||
PlayerDispDataBB disp;
|
||||
ptext<char, 0x174> unused;
|
||||
PlayerChallengeDataBB challenge_data;
|
||||
ChoiceSearchConfig<le_uint16_t> choice_search_config;
|
||||
ptext<char16_t, 0xAC> info_board;
|
||||
parray<le_uint32_t, 0x1E> blocked_senders;
|
||||
le_uint32_t auto_reply_enabled;
|
||||
@@ -326,31 +439,31 @@ struct PSOPlayerDataBB { // For command 61
|
||||
} __attribute__((packed));
|
||||
|
||||
struct PlayerBB { // Used in 00E7 command
|
||||
PlayerInventory inventory; // player
|
||||
PlayerDispDataBB disp; // player
|
||||
parray<uint8_t, 0x0010> unknown; // not saved
|
||||
le_uint32_t option_flags; // account
|
||||
parray<uint8_t, 0x0208> quest_data1; // player
|
||||
PlayerBank bank; // player
|
||||
le_uint32_t serial_number; // player
|
||||
ptext<char16_t, 0x18> name; // player
|
||||
ptext<char16_t, 0x10> team_name; // player
|
||||
ptext<char16_t, 0x58> guild_card_desc; // player
|
||||
uint8_t reserved1; // player
|
||||
uint8_t reserved2; // player
|
||||
uint8_t section_id; // player
|
||||
uint8_t char_class; // player
|
||||
le_uint32_t unknown3; // not saved
|
||||
parray<uint8_t, 0x04E0> symbol_chats; // account
|
||||
parray<uint8_t, 0x0A40> shortcuts; // account
|
||||
ptext<char16_t, 0x00AC> auto_reply; // player
|
||||
ptext<char16_t, 0x00AC> info_board; // player
|
||||
parray<uint8_t, 0x001C> unknown5; // not saved
|
||||
parray<uint8_t, 0x0140> challenge_data; // player
|
||||
parray<uint8_t, 0x0028> tech_menu_config; // player
|
||||
parray<uint8_t, 0x002C> unknown6; // not saved
|
||||
parray<uint8_t, 0x0058> quest_data2; // player
|
||||
KeyAndTeamConfigBB key_config; // account
|
||||
PlayerInventory inventory; // 0000-034C; player
|
||||
PlayerDispDataBB disp; // 034C-04DC; player
|
||||
parray<uint8_t, 0x0010> unknown; // 04DC-04EC; not saved
|
||||
le_uint32_t option_flags; // 04EC-04F0; account
|
||||
parray<uint8_t, 0x0208> quest_data1; // 04F0-06F8; player
|
||||
PlayerBank bank; // 06F8-19C0; player
|
||||
le_uint32_t guild_card_number; // 19C0-19C4; player
|
||||
ptext<char16_t, 0x18> name; // 19C4-19F4; player
|
||||
ptext<char16_t, 0x10> team_name; // 19F4-1A14; player
|
||||
ptext<char16_t, 0x58> guild_card_description; // 1A14-1AC4; player
|
||||
uint8_t reserved1; // 1AC4-1AC5; player
|
||||
uint8_t reserved2; // 1AC5-1AC6; player
|
||||
uint8_t section_id; // 1AC6-1AC7; player
|
||||
uint8_t char_class; // 1AC7-1AC8; player
|
||||
le_uint32_t unknown3; // 1AC8-1ACC; not saved
|
||||
parray<uint8_t, 0x04E0> symbol_chats; // 1ACC-1FAC; account
|
||||
parray<uint8_t, 0x0A40> shortcuts; // 1FAC-29EC; account
|
||||
ptext<char16_t, 0x00AC> auto_reply; // 29EC-2B44; player
|
||||
ptext<char16_t, 0x00AC> info_board; // 2B44-2C9C; player
|
||||
parray<uint8_t, 0x001C> unknown5; // 2C9C-2CB8; not saved
|
||||
parray<uint8_t, 0x0140> challenge_data; // 2CB8-2DF8; player
|
||||
parray<uint8_t, 0x0028> tech_menu_config; // 2DF8-2E20; player
|
||||
parray<uint8_t, 0x002C> unknown6; // 2E20-2E4C; not saved
|
||||
parray<uint8_t, 0x0058> quest_data2; // 2E4C-2EA4; player
|
||||
KeyAndTeamConfigBB key_config; // 2EA4-3994; account
|
||||
} __attribute__((packed));
|
||||
|
||||
|
||||
@@ -362,7 +475,7 @@ struct SavedPlayerDataBB { // .nsc file format
|
||||
PlayerBank bank;
|
||||
parray<uint8_t, 0x0140> challenge_data;
|
||||
PlayerDispDataBB disp;
|
||||
ptext<char16_t, 0x0058> guild_card_desc;
|
||||
ptext<char16_t, 0x0058> guild_card_description;
|
||||
ptext<char16_t, 0x00AC> info_board;
|
||||
PlayerInventory inventory;
|
||||
parray<uint8_t, 0x0208> quest_data1;
|
||||
@@ -370,17 +483,22 @@ struct SavedPlayerDataBB { // .nsc file format
|
||||
parray<uint8_t, 0x0028> tech_menu_config;
|
||||
|
||||
void add_item(const PlayerInventoryItem& item);
|
||||
PlayerInventoryItem remove_item(uint32_t item_id, uint32_t amount);
|
||||
PlayerInventoryItem remove_item(
|
||||
uint32_t item_id, uint32_t amount, bool allow_meseta_overdraft);
|
||||
|
||||
void print_inventory(FILE* stream) const;
|
||||
} __attribute__((packed));
|
||||
|
||||
enum AccountFlag {
|
||||
IN_DRESSING_ROOM = 0x00000001,
|
||||
};
|
||||
|
||||
struct SavedAccountDataBB { // .nsa file format
|
||||
ptext<char, 0x40> signature;
|
||||
parray<le_uint32_t, 0x001E> blocked_senders;
|
||||
GuildCardFileBB guild_cards;
|
||||
KeyAndTeamConfigBB key_config;
|
||||
le_uint32_t unused;
|
||||
le_uint32_t newserv_flags;
|
||||
le_uint32_t option_flags;
|
||||
parray<uint8_t, 0x0A40> shortcuts;
|
||||
parray<uint8_t, 0x04E0> symbol_chats;
|
||||
@@ -393,22 +511,29 @@ class ClientGameData {
|
||||
private:
|
||||
std::shared_ptr<SavedAccountDataBB> account_data;
|
||||
std::shared_ptr<SavedPlayerDataBB> player_data;
|
||||
uint64_t last_play_time_update;
|
||||
|
||||
public:
|
||||
uint32_t serial_number;
|
||||
uint32_t guild_card_number;
|
||||
bool should_update_play_time;
|
||||
|
||||
// The following fields are not saved, and are only used in certain situations
|
||||
|
||||
// Null unless the client is within the trade sequence (D0-D4 or EE commands)
|
||||
std::unique_ptr<PendingItemTrade> pending_item_trade;
|
||||
std::unique_ptr<PendingCardTrade> pending_card_trade;
|
||||
|
||||
// Null unless the client is Episode 3 and has sent its config already
|
||||
std::shared_ptr<Ep3Config> ep3_config;
|
||||
std::shared_ptr<Episode3::PlayerConfig> ep3_config;
|
||||
|
||||
// These are only used if the client is BB
|
||||
std::string bb_username;
|
||||
size_t bb_player_index;
|
||||
PlayerInventoryItem identify_result;
|
||||
std::vector<ItemData> shop_contents;
|
||||
bool should_save;
|
||||
|
||||
ClientGameData() : serial_number(0), bb_player_index(0) { }
|
||||
ClientGameData();
|
||||
~ClientGameData();
|
||||
|
||||
std::shared_ptr<SavedAccountDataBB> account(bool should_load = true);
|
||||
@@ -427,10 +552,11 @@ public:
|
||||
void load_account_data();
|
||||
void save_account_data() const;
|
||||
void load_player_data();
|
||||
void save_player_data() const;
|
||||
// Note: This function is not const because it updates the player's play time.
|
||||
void save_player_data();
|
||||
|
||||
void import_player(const PSOPlayerDataPC& pd);
|
||||
void import_player(const PSOPlayerDataGC& pd);
|
||||
void import_player(const PSOPlayerDataDCPC& pd);
|
||||
void import_player(const PSOPlayerDataV3& pd);
|
||||
void import_player(const PSOPlayerDataBB& pd);
|
||||
// Note: this function is not const because it can cause player and account
|
||||
// data to be loaded
|
||||
@@ -450,20 +576,20 @@ DestT convert_player_disp_data(const SrcT&) {
|
||||
}
|
||||
|
||||
template <>
|
||||
inline PlayerDispDataPCGC convert_player_disp_data<PlayerDispDataPCGC>(
|
||||
const PlayerDispDataPCGC& src) {
|
||||
inline PlayerDispDataDCPCV3 convert_player_disp_data<PlayerDispDataDCPCV3>(
|
||||
const PlayerDispDataDCPCV3& src) {
|
||||
return src;
|
||||
}
|
||||
|
||||
template <>
|
||||
inline PlayerDispDataPCGC convert_player_disp_data<PlayerDispDataPCGC, PlayerDispDataBB>(
|
||||
inline PlayerDispDataDCPCV3 convert_player_disp_data<PlayerDispDataDCPCV3, PlayerDispDataBB>(
|
||||
const PlayerDispDataBB& src) {
|
||||
return src.to_pcgc();
|
||||
return src.to_dcpcv3();
|
||||
}
|
||||
|
||||
template <>
|
||||
inline PlayerDispDataBB convert_player_disp_data<PlayerDispDataBB, PlayerDispDataPCGC>(
|
||||
const PlayerDispDataPCGC& src) {
|
||||
inline PlayerDispDataBB convert_player_disp_data<PlayerDispDataBB, PlayerDispDataDCPCV3>(
|
||||
const PlayerDispDataDCPCV3& src) {
|
||||
return src.to_bb();
|
||||
}
|
||||
|
||||
|
||||
+1367
-688
File diff suppressed because it is too large
Load Diff
@@ -9,7 +9,7 @@
|
||||
|
||||
|
||||
|
||||
void process_proxy_command(
|
||||
void on_proxy_command(
|
||||
std::shared_ptr<ServerState> s,
|
||||
ProxyServer::LinkedSession& session,
|
||||
bool from_server,
|
||||
|
||||
+256
-81
@@ -17,11 +17,13 @@
|
||||
#include <iostream>
|
||||
#include <phosg/Encoding.hh>
|
||||
#include <phosg/Filesystem.hh>
|
||||
#include <phosg/Hash.hh>
|
||||
#include <phosg/Network.hh>
|
||||
#include <phosg/Random.hh>
|
||||
#include <phosg/Strings.hh>
|
||||
#include <phosg/Time.hh>
|
||||
|
||||
#include "Loggers.hh"
|
||||
#include "PSOProtocol.hh"
|
||||
#include "SendCommands.hh"
|
||||
#include "ReceiveCommands.hh"
|
||||
@@ -41,8 +43,8 @@ static const uint32_t UNLICENSED_SESSION_TIMEOUT_USECS = 10 * 1000000; // 10 sec
|
||||
ProxyServer::ProxyServer(
|
||||
shared_ptr<struct event_base> base,
|
||||
shared_ptr<ServerState> state)
|
||||
: log("[ProxyServer] "),
|
||||
base(base),
|
||||
: base(base),
|
||||
destroy_sessions_ev(event_new(this->base.get(), -1, EV_TIMEOUT, &ProxyServer::dispatch_destroy_sessions, this), event_free),
|
||||
state(state),
|
||||
next_unlicensed_session_id(0xFF00000000000001) { }
|
||||
|
||||
@@ -59,7 +61,7 @@ ProxyServer::ListeningSocket::ListeningSocket(
|
||||
GameVersion version,
|
||||
const struct sockaddr_storage* default_destination)
|
||||
: server(server),
|
||||
log(string_printf("[ProxyServer:ListeningSocket:%hu] ", port)),
|
||||
log(string_printf("[ProxyServer:ListeningSocket:%hu] ", port), proxy_server_log.min_level),
|
||||
port(port),
|
||||
fd(::listen("", port, SOMAXCONN)),
|
||||
listener(nullptr, evconnlistener_free),
|
||||
@@ -86,7 +88,7 @@ ProxyServer::ListeningSocket::ListeningSocket(
|
||||
this->default_destination.ss_family = 0;
|
||||
}
|
||||
|
||||
this->log(INFO, "Listening on TCP port %hu (%s) on fd %d",
|
||||
this->log.info("Listening on TCP port %hu (%s) on fd %d",
|
||||
this->port, name_for_version(this->version), static_cast<int>(this->fd));
|
||||
}
|
||||
|
||||
@@ -101,7 +103,7 @@ void ProxyServer::ListeningSocket::dispatch_on_listen_error(
|
||||
}
|
||||
|
||||
void ProxyServer::ListeningSocket::on_listen_accept(int fd) {
|
||||
this->log(INFO, "Client connected on fd %d (port %hu, version %s)",
|
||||
this->log.info("Client connected on fd %d (port %hu, version %s)",
|
||||
fd, this->port, name_for_version(this->version));
|
||||
auto* bev = bufferevent_socket_new(this->server->base.get(), fd,
|
||||
BEV_OPT_CLOSE_ON_FREE | BEV_OPT_DEFER_CALLBACKS);
|
||||
@@ -111,7 +113,7 @@ void ProxyServer::ListeningSocket::on_listen_accept(int fd) {
|
||||
|
||||
void ProxyServer::ListeningSocket::on_listen_error() {
|
||||
int err = EVUTIL_SOCKET_ERROR();
|
||||
this->log(ERROR, "Failure on listening socket %d: %d (%s)",
|
||||
this->log.error("Failure on listening socket %d: %d (%s)",
|
||||
evconnlistener_get_fd(this->listener.get()),
|
||||
err, evutil_socket_error_to_string(err));
|
||||
event_base_loopexit(this->server->base.get(), nullptr);
|
||||
@@ -126,14 +128,14 @@ void ProxyServer::connect_client(struct bufferevent* bev, uint16_t server_port)
|
||||
try {
|
||||
version = this->listeners.at(server_port)->version;
|
||||
} catch (const out_of_range&) {
|
||||
this->log(INFO, "Virtual connection received on unregistered port %hu; closing it",
|
||||
proxy_server_log.info("Virtual connection received on unregistered port %hu; closing it",
|
||||
server_port);
|
||||
bufferevent_flush(bev, EV_READ | EV_WRITE, BEV_FINISHED);
|
||||
bufferevent_free(bev);
|
||||
return;
|
||||
}
|
||||
|
||||
this->log(INFO, "Client connected on virtual connection %p (port %hu)", bev,
|
||||
proxy_server_log.info("Client connected on virtual connection %p (port %hu)", bev,
|
||||
server_port);
|
||||
this->on_client_connect(bev, server_port, version, nullptr);
|
||||
}
|
||||
@@ -160,7 +162,7 @@ void ProxyServer::on_client_connect(
|
||||
throw logic_error("linked session already exists for unlicensed client");
|
||||
}
|
||||
auto session = emplace_ret.first->second;
|
||||
session->log(INFO, "Opened linked session");
|
||||
session->log.info("Opened linked session");
|
||||
|
||||
Channel ch(bev, version, nullptr, nullptr, session.get(), "", TerminalFormat::FG_YELLOW, TerminalFormat::FG_GREEN);
|
||||
session->resume(move(ch));
|
||||
@@ -175,7 +177,7 @@ void ProxyServer::on_client_connect(
|
||||
throw logic_error("stale unlinked session exists");
|
||||
}
|
||||
auto session = emplace_ret.first->second;
|
||||
this->log(INFO, "Opened unlinked session");
|
||||
proxy_server_log.info("Opened unlinked session");
|
||||
|
||||
// Note that this should only be set when the linked session is created, not
|
||||
// when it is resumed!
|
||||
@@ -186,21 +188,21 @@ void ProxyServer::on_client_connect(
|
||||
switch (version) {
|
||||
case GameVersion::PATCH:
|
||||
throw logic_error("cannot create unlinked patch session");
|
||||
case GameVersion::DC:
|
||||
case GameVersion::PC:
|
||||
case GameVersion::GC: {
|
||||
case GameVersion::GC:
|
||||
case GameVersion::XB: {
|
||||
uint32_t server_key = random_object<uint32_t>();
|
||||
uint32_t client_key = random_object<uint32_t>();
|
||||
auto cmd = prepare_server_init_contents_dc_pc_gc(
|
||||
false, server_key, client_key);
|
||||
auto cmd = prepare_server_init_contents_console(
|
||||
server_key, client_key, 0);
|
||||
session->channel.send(0x02, 0x00, &cmd, sizeof(cmd));
|
||||
// TODO: Is this actually needed?
|
||||
// bufferevent_flush(session->channel.bev.get(), EV_READ | EV_WRITE, BEV_FLUSH);
|
||||
if (version == GameVersion::PC) {
|
||||
session->channel.crypt_out.reset(new PSOPCEncryption(server_key));
|
||||
session->channel.crypt_in.reset(new PSOPCEncryption(client_key));
|
||||
if ((version == GameVersion::DC) || (version == GameVersion::PC)) {
|
||||
session->channel.crypt_out.reset(new PSOV2Encryption(server_key));
|
||||
session->channel.crypt_in.reset(new PSOV2Encryption(client_key));
|
||||
} else {
|
||||
session->channel.crypt_out.reset(new PSOGCEncryption(server_key));
|
||||
session->channel.crypt_in.reset(new PSOGCEncryption(client_key));
|
||||
session->channel.crypt_out.reset(new PSOV3Encryption(server_key));
|
||||
session->channel.crypt_in.reset(new PSOV3Encryption(client_key));
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -209,16 +211,19 @@ void ProxyServer::on_client_connect(
|
||||
parray<uint8_t, 0x30> client_key;
|
||||
random_data(server_key.data(), server_key.bytes());
|
||||
random_data(client_key.data(), client_key.bytes());
|
||||
auto cmd = prepare_server_init_contents_bb(server_key, client_key, false);
|
||||
auto cmd = prepare_server_init_contents_bb(server_key, client_key, 0);
|
||||
session->channel.send(0x03, 0x00, &cmd, sizeof(cmd));
|
||||
// TODO: Is this actually needed?
|
||||
// bufferevent_flush(session->bev.get(), EV_READ | EV_WRITE, BEV_FLUSH);
|
||||
static const string expected_first_data("\xB4\x00\x93\x00\x00\x00\x00\x00", 8);
|
||||
session->detector_crypt.reset(new PSOBBMultiKeyDetectorEncryption(
|
||||
this->state->bb_private_keys, expected_first_data, cmd.client_key.data(), sizeof(cmd.client_key)));
|
||||
this->state->bb_private_keys,
|
||||
bb_crypt_initial_client_commands,
|
||||
cmd.basic_cmd.client_key.data(),
|
||||
sizeof(cmd.basic_cmd.client_key)));
|
||||
session->channel.crypt_in = session->detector_crypt;
|
||||
session->channel.crypt_out.reset(new PSOBBMultiKeyImitatorEncryption(
|
||||
session->detector_crypt, cmd.server_key.data(), sizeof(cmd.server_key), true));
|
||||
session->detector_crypt,
|
||||
cmd.basic_cmd.server_key.data(),
|
||||
sizeof(cmd.basic_cmd.server_key),
|
||||
true));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
@@ -230,9 +235,12 @@ void ProxyServer::on_client_connect(
|
||||
|
||||
|
||||
ProxyServer::UnlinkedSession::UnlinkedSession(
|
||||
ProxyServer* server, struct bufferevent* bev, uint16_t local_port, GameVersion version)
|
||||
ProxyServer* server,
|
||||
struct bufferevent* bev,
|
||||
uint16_t local_port,
|
||||
GameVersion version)
|
||||
: server(server),
|
||||
log(string_printf("[ProxyServer:UnlinkedSession:%p] ", bev)),
|
||||
log(string_printf("[ProxyServer:UnlinkedSession:%p] ", bev), proxy_server_log.min_level),
|
||||
channel(
|
||||
bev,
|
||||
version,
|
||||
@@ -253,27 +261,55 @@ void ProxyServer::UnlinkedSession::on_input(Channel& ch, uint16_t command, uint3
|
||||
bool should_close_unlinked_session = false;
|
||||
shared_ptr<const License> license;
|
||||
uint32_t sub_version = 0;
|
||||
uint8_t language = 1; // Default = English
|
||||
string character_name;
|
||||
ClientConfigBB client_config;
|
||||
string login_command_bb;
|
||||
string hardware_id;
|
||||
|
||||
try {
|
||||
if (session->version == GameVersion::PC) {
|
||||
if (session->version == GameVersion::DC) {
|
||||
// We should only get a 93 or 9D while the session is unlinked; if we get
|
||||
// anything else, disconnect
|
||||
if (command == 0x93) {
|
||||
const auto& cmd = check_size_t<C_LoginV1_DC_93>(data);
|
||||
license = session->server->state->license_manager->verify_pc(
|
||||
stoul(cmd.serial_number, nullptr, 16), cmd.access_key);
|
||||
sub_version = cmd.sub_version;
|
||||
language = cmd.language;
|
||||
character_name = cmd.name;
|
||||
hardware_id = cmd.hardware_id;
|
||||
client_config.cfg.flags |= Client::Flag::IS_DC_V1;
|
||||
} else if (command == 0x9D) {
|
||||
const auto& cmd = check_size_t<C_Login_DC_PC_GC_9D>(
|
||||
data, sizeof(C_Login_DC_PC_GC_9D), sizeof(C_LoginExtended_DC_GC_9D));
|
||||
license = session->server->state->license_manager->verify_pc(
|
||||
stoul(cmd.serial_number, nullptr, 16), cmd.access_key);
|
||||
sub_version = cmd.sub_version;
|
||||
language = cmd.language;
|
||||
character_name = cmd.name;
|
||||
} else {
|
||||
throw runtime_error("command is not 93 or 9D");
|
||||
}
|
||||
|
||||
} else if (session->version == GameVersion::PC) {
|
||||
// We should only get a 9D while the session is unlinked; if we get
|
||||
// anything else, disconnect
|
||||
if (command != 0x9D) {
|
||||
throw runtime_error("command is not 9D");
|
||||
}
|
||||
const auto& cmd = check_size_t<C_Login_PC_9D>(
|
||||
data, sizeof(C_Login_PC_9D), sizeof(C_LoginExtended_PC_9D));
|
||||
const auto& cmd = check_size_t<C_Login_DC_PC_GC_9D>(
|
||||
data, sizeof(C_Login_DC_PC_GC_9D), sizeof(C_LoginExtended_PC_9D));
|
||||
license = session->server->state->license_manager->verify_pc(
|
||||
stoul(cmd.serial_number, nullptr, 16), cmd.access_key);
|
||||
sub_version = cmd.sub_version;
|
||||
language = cmd.language;
|
||||
character_name = cmd.name;
|
||||
|
||||
} else if (session->version == GameVersion::GC) {
|
||||
// We should only get a 9E while the session is unlinked; if we get
|
||||
// anything else, disconnect
|
||||
// TODO: GCTE will send 9D; we should presumably handle that too, sigh
|
||||
if (command != 0x9E) {
|
||||
throw runtime_error("command is not 9E");
|
||||
}
|
||||
@@ -282,9 +318,13 @@ void ProxyServer::UnlinkedSession::on_input(Channel& ch, uint16_t command, uint3
|
||||
license = session->server->state->license_manager->verify_gc(
|
||||
stoul(cmd.serial_number, nullptr, 16), cmd.access_key);
|
||||
sub_version = cmd.sub_version;
|
||||
language = cmd.language;
|
||||
character_name = cmd.name;
|
||||
client_config.cfg = cmd.client_config.cfg;
|
||||
|
||||
} else if (session->version == GameVersion::XB) {
|
||||
throw runtime_error("xbox licenses are not implemented");
|
||||
|
||||
} else if (session->version == GameVersion::BB) {
|
||||
// We should only get a 93 while the session is unlinked; if we get
|
||||
// anything else, disconnect
|
||||
@@ -292,8 +332,18 @@ void ProxyServer::UnlinkedSession::on_input(Channel& ch, uint16_t command, uint3
|
||||
throw runtime_error("command is not 93");
|
||||
}
|
||||
const auto& cmd = check_size_t<C_Login_BB_93>(data);
|
||||
license = session->server->state->license_manager->verify_bb(
|
||||
cmd.username, cmd.password);
|
||||
try {
|
||||
license = session->server->state->license_manager->verify_bb(
|
||||
cmd.username, cmd.password);
|
||||
} catch (const missing_license&) {
|
||||
if (!session->server->state->allow_unregistered_users) {
|
||||
throw;
|
||||
}
|
||||
shared_ptr<License> l = LicenseManager::create_license_bb(
|
||||
fnv1a32(cmd.username) & 0x7FFFFFFF, cmd.username, cmd.password, true);
|
||||
session->server->state->license_manager->add(l);
|
||||
license = l;
|
||||
}
|
||||
login_command_bb = move(data);
|
||||
|
||||
} else {
|
||||
@@ -301,10 +351,13 @@ void ProxyServer::UnlinkedSession::on_input(Channel& ch, uint16_t command, uint3
|
||||
}
|
||||
|
||||
} catch (const exception& e) {
|
||||
session->log(ERROR, "Failed to process command from unlinked client: %s", e.what());
|
||||
session->log.error("Failed to process command from unlinked client: %s", e.what());
|
||||
should_close_unlinked_session = true;
|
||||
}
|
||||
|
||||
// Note that ch.bev will be moved from when the linked session is resumed, so
|
||||
// we need to retain a copy of it in order to close the unlinked session
|
||||
// afterward.
|
||||
struct bufferevent* session_key = ch.bev.get();
|
||||
|
||||
// If license is non-null, then the client has a password and can be connected
|
||||
@@ -318,7 +371,7 @@ void ProxyServer::UnlinkedSession::on_input(Channel& ch, uint16_t command, uint3
|
||||
shared_ptr<LinkedSession> linked_session;
|
||||
try {
|
||||
linked_session = session->server->id_to_session.at(license->serial_number);
|
||||
linked_session->log(INFO, "Resuming linked session from unlinked session");
|
||||
linked_session->log.info("Resuming linked session from unlinked session");
|
||||
|
||||
} catch (const out_of_range&) {
|
||||
// If there's no open session for this license, then there must be a valid
|
||||
@@ -331,7 +384,7 @@ void ProxyServer::UnlinkedSession::on_input(Channel& ch, uint16_t command, uint3
|
||||
session->version,
|
||||
license,
|
||||
client_config));
|
||||
linked_session->log(INFO, "Opened licensed session for unlinked session based on client config");
|
||||
linked_session->log.info("Opened licensed session for unlinked session based on client config");
|
||||
} else if (session->next_destination.ss_family == AF_INET) {
|
||||
linked_session.reset(new LinkedSession(
|
||||
session->server,
|
||||
@@ -339,16 +392,16 @@ void ProxyServer::UnlinkedSession::on_input(Channel& ch, uint16_t command, uint3
|
||||
session->version,
|
||||
license,
|
||||
session->next_destination));
|
||||
linked_session->log(INFO, "Opened licensed session for unlinked session based on unlinked default destination");
|
||||
linked_session->log.info("Opened licensed session for unlinked session based on unlinked default destination");
|
||||
} else {
|
||||
session->log(ERROR, "Cannot open linked session: no valid destination in client config or unlinked session");
|
||||
session->log.error("Cannot open linked session: no valid destination in client config or unlinked session");
|
||||
}
|
||||
}
|
||||
|
||||
if (linked_session.get()) {
|
||||
session->server->id_to_session.emplace(license->serial_number, linked_session);
|
||||
if (linked_session->version != session->version) {
|
||||
linked_session->log(ERROR, "Linked session has different game version");
|
||||
linked_session->log.error("Linked session has different game version");
|
||||
} else {
|
||||
// Resume the linked session using the unlinked session
|
||||
try {
|
||||
@@ -362,19 +415,19 @@ void ProxyServer::UnlinkedSession::on_input(Channel& ch, uint16_t command, uint3
|
||||
move(session->channel),
|
||||
session->detector_crypt,
|
||||
sub_version,
|
||||
character_name);
|
||||
language,
|
||||
character_name,
|
||||
hardware_id);
|
||||
}
|
||||
} catch (const exception& e) {
|
||||
linked_session->log(ERROR, "Failed to resume linked session: %s", e.what());
|
||||
linked_session->log.error("Failed to resume linked session: %s", e.what());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (should_close_unlinked_session) {
|
||||
session->log(INFO, "Closing session");
|
||||
session->server->bev_to_unlinked_session.erase(session_key);
|
||||
// At this point, (*this) is destroyed! We must be careful not to touch it.
|
||||
session->server->delete_session(session_key);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -383,12 +436,12 @@ void ProxyServer::UnlinkedSession::on_error(Channel& ch, short events) {
|
||||
|
||||
if (events & BEV_EVENT_ERROR) {
|
||||
int err = EVUTIL_SOCKET_ERROR();
|
||||
session->log(WARNING, "Error %d (%s) in unlinked client stream", err,
|
||||
session->log.warning("Error %d (%s) in unlinked client stream", err,
|
||||
evutil_socket_error_to_string(err));
|
||||
}
|
||||
if (events & (BEV_EVENT_ERROR | BEV_EVENT_EOF)) {
|
||||
session->log(WARNING, "Unlinked client has disconnected");
|
||||
session->server->bev_to_unlinked_session.erase(session->channel.bev.get());
|
||||
session->log.info("Client has disconnected");
|
||||
session->server->delete_session(session->channel.bev.get());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -401,7 +454,7 @@ ProxyServer::LinkedSession::LinkedSession(
|
||||
GameVersion version)
|
||||
: server(server),
|
||||
id(id),
|
||||
log(string_printf("[ProxyServer:LinkedSession:%08" PRIX64 "] ", this->id)),
|
||||
log(string_printf("[ProxyServer:LinkedSession:%08" PRIX64 "] ", this->id), proxy_server_log.min_level),
|
||||
timeout_event(event_new(this->server->base.get(), -1, EV_TIMEOUT,
|
||||
&LinkedSession::dispatch_on_timeout, this), event_free),
|
||||
license(nullptr),
|
||||
@@ -422,23 +475,22 @@ ProxyServer::LinkedSession::LinkedSession(
|
||||
TerminalFormat::FG_YELLOW,
|
||||
TerminalFormat::FG_RED),
|
||||
local_port(local_port),
|
||||
close_on_disconnect(false),
|
||||
remote_ip_crc(0),
|
||||
enable_remote_ip_crc_patch(false),
|
||||
version(version),
|
||||
sub_version(0), // This is set during resume()
|
||||
remote_guild_card_number(0),
|
||||
enable_chat_filter(true),
|
||||
switch_assist(false),
|
||||
infinite_hp(false),
|
||||
infinite_tp(false),
|
||||
save_files(false),
|
||||
function_call_return_value(-1),
|
||||
override_section_id(-1),
|
||||
override_lobby_event(-1),
|
||||
override_lobby_number(-1),
|
||||
language(1), // Default = English. This is also set during resume()
|
||||
remote_guild_card_number(-1),
|
||||
next_item_id(0x0F000000),
|
||||
lobby_players(12),
|
||||
lobby_client_id(0) {
|
||||
this->last_switch_enabled_command.subcommand = 0;
|
||||
lobby_client_id(0),
|
||||
leader_client_id(0),
|
||||
area(0),
|
||||
x(0.0),
|
||||
z(0.0),
|
||||
is_in_game(false) {
|
||||
this->last_switch_enabled_command.header.subcommand = 0;
|
||||
memset(this->prev_server_command_bytes, 0, sizeof(this->prev_server_command_bytes));
|
||||
}
|
||||
|
||||
@@ -483,9 +535,13 @@ void ProxyServer::LinkedSession::resume(
|
||||
Channel&& client_channel,
|
||||
shared_ptr<PSOBBMultiKeyDetectorEncryption> detector_crypt,
|
||||
uint32_t sub_version,
|
||||
const string& character_name) {
|
||||
uint8_t language,
|
||||
const string& character_name,
|
||||
const string& hardware_id) {
|
||||
this->sub_version = sub_version;
|
||||
this->language = language;
|
||||
this->character_name = character_name;
|
||||
this->hardware_id = hardware_id;
|
||||
this->resume_inner(move(client_channel), detector_crypt);
|
||||
}
|
||||
|
||||
@@ -499,6 +555,7 @@ void ProxyServer::LinkedSession::resume(
|
||||
|
||||
void ProxyServer::LinkedSession::resume(Channel&& client_channel) {
|
||||
this->sub_version = 0;
|
||||
this->language = 1;
|
||||
this->character_name.clear();
|
||||
this->resume_inner(move(client_channel), nullptr);
|
||||
}
|
||||
@@ -530,24 +587,19 @@ void ProxyServer::LinkedSession::resume_inner(
|
||||
void ProxyServer::LinkedSession::connect() {
|
||||
// Connect to the remote server. The command handlers will do the login steps
|
||||
// and set up forwarding
|
||||
struct sockaddr_storage local_ss;
|
||||
struct sockaddr_in* local_sin = reinterpret_cast<struct sockaddr_in*>(&local_ss);
|
||||
memset(local_sin, 0, sizeof(*local_sin));
|
||||
local_sin->sin_family = AF_INET;
|
||||
const struct sockaddr_in* dest_sin = reinterpret_cast<const sockaddr_in*>(&this->next_destination);
|
||||
const struct sockaddr_in* dest_sin = reinterpret_cast<const sockaddr_in*>(
|
||||
&this->next_destination);
|
||||
if (dest_sin->sin_family != AF_INET) {
|
||||
throw logic_error("ss not AF_INET");
|
||||
throw runtime_error("destination is not AF_INET");
|
||||
}
|
||||
local_sin->sin_port = dest_sin->sin_port;
|
||||
local_sin->sin_addr.s_addr = dest_sin->sin_addr.s_addr;
|
||||
|
||||
string netloc_str = render_sockaddr_storage(local_ss);
|
||||
this->log(INFO, "Connecting to %s", netloc_str.c_str());
|
||||
string netloc_str = render_sockaddr_storage(this->next_destination);
|
||||
this->log.info("Connecting to %s", netloc_str.c_str());
|
||||
|
||||
this->server_channel.set_bufferevent(bufferevent_socket_new(
|
||||
this->server->base.get(), -1, BEV_OPT_CLOSE_ON_FREE | BEV_OPT_DEFER_CALLBACKS));
|
||||
if (bufferevent_socket_connect(this->server_channel.bev.get(),
|
||||
reinterpret_cast<const sockaddr*>(local_sin), sizeof(*local_sin)) != 0) {
|
||||
reinterpret_cast<const sockaddr*>(dest_sin), sizeof(*dest_sin)) != 0) {
|
||||
throw runtime_error(string_printf("failed to connect (%d)", EVUTIL_SOCKET_ERROR()));
|
||||
}
|
||||
|
||||
@@ -567,8 +619,11 @@ ProxyServer::LinkedSession::SavingFile::SavingFile(
|
||||
uint32_t remaining_bytes)
|
||||
: basename(basename),
|
||||
output_filename(output_filename),
|
||||
remaining_bytes(remaining_bytes),
|
||||
f(fopen_unique(this->output_filename, "wb")) { }
|
||||
remaining_bytes(remaining_bytes) {
|
||||
if (!this->output_filename.empty()) {
|
||||
this->f = fopen_unique(this->output_filename, "wb");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -580,7 +635,7 @@ void ProxyServer::LinkedSession::dispatch_on_timeout(
|
||||
|
||||
|
||||
void ProxyServer::LinkedSession::on_timeout() {
|
||||
this->log(INFO, "Session timed out");
|
||||
this->log.info("Session timed out");
|
||||
this->server->delete_session(this->id);
|
||||
}
|
||||
|
||||
@@ -590,16 +645,103 @@ void ProxyServer::LinkedSession::on_error(Channel& ch, short events) {
|
||||
auto* session = reinterpret_cast<LinkedSession*>(ch.context_obj);
|
||||
bool is_server_stream = (&ch == &session->server_channel);
|
||||
|
||||
if (events & BEV_EVENT_CONNECTED) {
|
||||
session->log.info("%s channel connected", is_server_stream ? "Server" : "Client");
|
||||
|
||||
if (is_server_stream && (session->options.override_lobby_event >= 0) &&
|
||||
(
|
||||
((session->version == GameVersion::GC) && !(session->newserv_client_config.cfg.flags & Client::Flag::IS_TRIAL_EDITION)) ||
|
||||
(session->version == GameVersion::XB) ||
|
||||
(session->version == GameVersion::BB)
|
||||
)) {
|
||||
session->client_channel.send(0xDA, session->options.override_lobby_event);
|
||||
}
|
||||
}
|
||||
if (events & BEV_EVENT_ERROR) {
|
||||
int err = EVUTIL_SOCKET_ERROR();
|
||||
session->log(WARNING, "Error %d (%s) in %s stream",
|
||||
session->log.warning("Error %d (%s) in %s stream",
|
||||
err, evutil_socket_error_to_string(err),
|
||||
is_server_stream ? "server" : "client");
|
||||
}
|
||||
if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) {
|
||||
session->log(INFO, "%s has disconnected",
|
||||
session->log.info("%s has disconnected",
|
||||
is_server_stream ? "Server" : "Client");
|
||||
// If the server disconnected, send the client back to the game server so
|
||||
// they're not disconnected completely.
|
||||
if (is_server_stream) {
|
||||
session->send_to_game_server("The server has\ndisconnected.");
|
||||
}
|
||||
session->disconnect();
|
||||
if (session->close_on_disconnect) {
|
||||
session->server->delete_session(session->id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ProxyServer::LinkedSession::clear_lobby_players(size_t num_slots) {
|
||||
this->lobby_players.clear();
|
||||
this->lobby_players.resize(num_slots);
|
||||
this->log.info("Cleared lobby players");
|
||||
}
|
||||
|
||||
void ProxyServer::LinkedSession::send_to_game_server(const char* error_message) {
|
||||
// Delete all the other players
|
||||
for (size_t x = 0; x < this->lobby_players.size(); x++) {
|
||||
if (this->lobby_players[x].guild_card_number == 0) {
|
||||
continue;
|
||||
}
|
||||
uint8_t leaving_id = x;
|
||||
uint8_t leader_id = this->lobby_client_id;
|
||||
S_LeaveLobby_66_69_Ep3_E9 cmd = {leaving_id, leader_id, 1, 0};
|
||||
this->client_channel.send(this->is_in_game ? 0x66 : 0x69, leaving_id, &cmd, sizeof(cmd));
|
||||
}
|
||||
|
||||
string encoded_name = encode_sjis(this->server->state->name);
|
||||
if (this->is_in_game) {
|
||||
send_ship_info(this->client_channel, decode_sjis(string_printf(
|
||||
"You cannot return\nto $C6%s$C7\nwhile in a game.\n\n%s",
|
||||
encoded_name.c_str(), error_message ? error_message : "")));
|
||||
this->disconnect();
|
||||
|
||||
} else {
|
||||
send_ship_info(this->client_channel, decode_sjis(string_printf(
|
||||
"You\'ve returned to\n\tC6%s$C7\n\n%s", encoded_name.c_str(),
|
||||
error_message ? error_message : "")));
|
||||
|
||||
// Restore newserv_client_config, so the login server gets the client flags
|
||||
S_UpdateClientConfig_DC_PC_V3_04 update_client_config_cmd;
|
||||
update_client_config_cmd.player_tag = 0x00010000;
|
||||
update_client_config_cmd.guild_card_number = this->license->serial_number;
|
||||
update_client_config_cmd.cfg = this->newserv_client_config.cfg;
|
||||
this->client_channel.send(0x04, 0x00, &update_client_config_cmd, sizeof(update_client_config_cmd));
|
||||
|
||||
const auto& port_name = version_to_login_port_name.at(static_cast<size_t>(
|
||||
this->version));
|
||||
|
||||
S_Reconnect_19 reconnect_cmd = {{
|
||||
0, this->server->state->name_to_port_config.at(port_name)->port, 0}};
|
||||
|
||||
// If the client is on a virtual connection, we can use any address
|
||||
// here and they should be able to connect back to the game server. If
|
||||
// the client is on a real connection, we'll use the sockname of the
|
||||
// existing connection (like we do in the server 19 command handler).
|
||||
if (this->client_channel.is_virtual_connection) {
|
||||
struct sockaddr_in* dest_sin = reinterpret_cast<struct sockaddr_in*>(&this->next_destination);
|
||||
if (dest_sin->sin_family != AF_INET) {
|
||||
throw logic_error("ss not AF_INET");
|
||||
}
|
||||
reconnect_cmd.address.store_raw(dest_sin->sin_addr.s_addr);
|
||||
} else {
|
||||
const struct sockaddr_in* sin = reinterpret_cast<const struct sockaddr_in*>(
|
||||
&this->client_channel.local_addr);
|
||||
if (sin->sin_family != AF_INET) {
|
||||
throw logic_error("existing connection is not ipv4");
|
||||
}
|
||||
reconnect_cmd.address.store_raw(sin->sin_addr.s_addr);
|
||||
}
|
||||
|
||||
this->client_channel.send(0x19, 0x00, &reconnect_cmd, sizeof(reconnect_cmd));
|
||||
this->close_on_disconnect = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -630,7 +772,7 @@ void ProxyServer::LinkedSession::on_input(Channel& ch, uint16_t command, uint32_
|
||||
size_t bytes_to_save = min<size_t>(data.size(), sizeof(session->prev_server_command_bytes));
|
||||
memcpy(session->prev_server_command_bytes, data.data(), bytes_to_save);
|
||||
}
|
||||
process_proxy_command(
|
||||
on_proxy_command(
|
||||
session->server->state,
|
||||
*session,
|
||||
is_server_stream,
|
||||
@@ -638,7 +780,7 @@ void ProxyServer::LinkedSession::on_input(Channel& ch, uint16_t command, uint32_
|
||||
flag,
|
||||
data);
|
||||
} catch (const exception& e) {
|
||||
session->log(ERROR, "Failed to process command from %s: %s",
|
||||
session->log.error("Failed to process command from %s: %s",
|
||||
is_server_stream ? "server" : "client", e.what());
|
||||
session->disconnect();
|
||||
}
|
||||
@@ -654,6 +796,18 @@ 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) {
|
||||
try {
|
||||
uint64_t session_id = stoull(name, nullptr, 16);
|
||||
return this->id_to_session.at(session_id);
|
||||
} catch (const invalid_argument&) {
|
||||
throw runtime_error("invalid session name");
|
||||
} catch (const out_of_range&) {
|
||||
throw runtime_error("no such session");
|
||||
}
|
||||
}
|
||||
|
||||
shared_ptr<ProxyServer::LinkedSession> ProxyServer::create_licensed_session(
|
||||
shared_ptr<const License> l, uint16_t local_port, GameVersion version,
|
||||
const ClientConfigBB& newserv_client_config) {
|
||||
@@ -663,16 +817,37 @@ shared_ptr<ProxyServer::LinkedSession> ProxyServer::create_licensed_session(
|
||||
if (!emplace_ret.second) {
|
||||
throw runtime_error("session already exists for this license");
|
||||
}
|
||||
session->log(INFO, "Opening licensed session");
|
||||
session->log.info("Opening licensed session");
|
||||
return emplace_ret.first->second;
|
||||
}
|
||||
|
||||
void ProxyServer::delete_session(uint64_t id) {
|
||||
if (this->id_to_session.erase(id)) {
|
||||
this->log(INFO, "Closed LinkedSession:%08" PRIX64, id);
|
||||
proxy_server_log.info("Closed LinkedSession:%08" PRIX64, id);
|
||||
}
|
||||
}
|
||||
|
||||
void ProxyServer::delete_session(struct bufferevent* bev) {
|
||||
auto it = this->bev_to_unlinked_session.find(bev);
|
||||
if (it == this->bev_to_unlinked_session.end()) {
|
||||
throw logic_error("unlinked session exists but is not registered");
|
||||
}
|
||||
it->second->log.info("Closing session");
|
||||
this->unlinked_sessions_to_destroy.emplace(move(it->second));
|
||||
this->bev_to_unlinked_session.erase(it);
|
||||
|
||||
auto tv = usecs_to_timeval(0);
|
||||
event_add(this->destroy_sessions_ev.get(), &tv);
|
||||
}
|
||||
|
||||
void ProxyServer::dispatch_destroy_sessions(evutil_socket_t, short, void* ctx) {
|
||||
reinterpret_cast<ProxyServer*>(ctx)->destroy_sessions();
|
||||
}
|
||||
|
||||
void ProxyServer::destroy_sessions() {
|
||||
this->unlinked_sessions_to_destroy.clear();
|
||||
}
|
||||
|
||||
size_t ProxyServer::delete_disconnected_sessions() {
|
||||
size_t count = 0;
|
||||
for (auto it = this->id_to_session.begin(); it != this->id_to_session.end();) {
|
||||
|
||||
+30
-13
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <event2/event.h>
|
||||
|
||||
#include <deque>
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
@@ -45,6 +46,7 @@ public:
|
||||
Channel server_channel;
|
||||
uint16_t local_port;
|
||||
struct sockaddr_storage next_destination;
|
||||
bool close_on_disconnect;
|
||||
|
||||
uint8_t prev_server_command_bytes[6];
|
||||
uint32_t remote_ip_crc;
|
||||
@@ -52,30 +54,34 @@ public:
|
||||
|
||||
GameVersion version;
|
||||
uint32_t sub_version;
|
||||
uint8_t language;
|
||||
std::string character_name;
|
||||
std::string hardware_id; // Only used for DC sessions
|
||||
std::string login_command_bb;
|
||||
|
||||
uint32_t remote_guild_card_number;
|
||||
ClientOptions options;
|
||||
int64_t remote_guild_card_number;
|
||||
parray<uint8_t, 0x20> remote_client_config_data;
|
||||
ClientConfigBB newserv_client_config;
|
||||
bool enable_chat_filter;
|
||||
bool switch_assist;
|
||||
bool infinite_hp;
|
||||
bool infinite_tp;
|
||||
bool save_files;
|
||||
int64_t function_call_return_value; // -1 = don't block function calls
|
||||
std::deque<bool> should_forward_function_call_return_queue;
|
||||
G_SwitchStateChanged_6x05 last_switch_enabled_command;
|
||||
int16_t override_section_id;
|
||||
int16_t override_lobby_event;
|
||||
int16_t override_lobby_number;
|
||||
PlayerInventoryItem next_drop_item;
|
||||
uint32_t next_item_id;
|
||||
|
||||
struct LobbyPlayer {
|
||||
uint32_t guild_card_number;
|
||||
std::string name;
|
||||
LobbyPlayer() : guild_card_number(0) { }
|
||||
uint8_t section_id;
|
||||
uint8_t char_class;
|
||||
LobbyPlayer() : guild_card_number(0), section_id(0), char_class(0) { }
|
||||
};
|
||||
std::vector<LobbyPlayer> lobby_players;
|
||||
size_t lobby_client_id;
|
||||
size_t leader_client_id;
|
||||
uint16_t area;
|
||||
float x;
|
||||
float z;
|
||||
bool is_in_game;
|
||||
|
||||
std::shared_ptr<PSOBBMultiKeyDetectorEncryption> detector_crypt;
|
||||
|
||||
@@ -121,7 +127,9 @@ public:
|
||||
Channel&& client_channel,
|
||||
std::shared_ptr<PSOBBMultiKeyDetectorEncryption> detector_crypt,
|
||||
uint32_t sub_version,
|
||||
const std::string& character_name);
|
||||
uint8_t language,
|
||||
const std::string& character_name,
|
||||
const std::string& hardware_id);
|
||||
void resume(
|
||||
Channel&& client_channel,
|
||||
std::shared_ptr<PSOBBMultiKeyDetectorEncryption> detector_crypt,
|
||||
@@ -137,18 +145,23 @@ public:
|
||||
static void on_error(Channel& ch, short events);
|
||||
void on_timeout();
|
||||
|
||||
void clear_lobby_players(size_t num_slots);
|
||||
|
||||
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> create_licensed_session(
|
||||
std::shared_ptr<const License> l,
|
||||
uint16_t local_port,
|
||||
GameVersion version,
|
||||
const ClientConfigBB& newserv_client_config);
|
||||
void delete_session(uint64_t id);
|
||||
void delete_session(struct bufferevent* bev);
|
||||
|
||||
size_t delete_disconnected_sessions();
|
||||
|
||||
@@ -195,14 +208,18 @@ private:
|
||||
static void on_error(Channel& ch, short events);
|
||||
};
|
||||
|
||||
PrefixedLogger log;
|
||||
std::shared_ptr<struct event_base> base;
|
||||
std::shared_ptr<struct event> destroy_sessions_ev;
|
||||
std::shared_ptr<ServerState> state;
|
||||
std::map<int, std::shared_ptr<ListeningSocket>> listeners;
|
||||
std::unordered_map<struct bufferevent*, std::shared_ptr<UnlinkedSession>> bev_to_unlinked_session;
|
||||
std::unordered_set<std::shared_ptr<UnlinkedSession>> unlinked_sessions_to_destroy;
|
||||
std::unordered_map<uint64_t, std::shared_ptr<LinkedSession>> id_to_session;
|
||||
uint64_t next_unlicensed_session_id;
|
||||
|
||||
static void dispatch_destroy_sessions(evutil_socket_t, short, void* ctx);
|
||||
void destroy_sessions();
|
||||
|
||||
void on_client_connect(
|
||||
struct bufferevent* bev,
|
||||
uint16_t listen_port,
|
||||
|
||||
+520
-117
@@ -1,13 +1,17 @@
|
||||
#include "Quest.hh"
|
||||
|
||||
#include <algorithm>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <phosg/Filesystem.hh>
|
||||
#include <phosg/Encoding.hh>
|
||||
#include <phosg/Hash.hh>
|
||||
#include <phosg/Random.hh>
|
||||
#include <phosg/Strings.hh>
|
||||
#include <phosg/Tools.hh>
|
||||
|
||||
#include "Loggers.hh"
|
||||
#include "CommandFormats.hh"
|
||||
#include "Compression.hh"
|
||||
#include "PSOEncryption.hh"
|
||||
@@ -17,11 +21,258 @@ using namespace std;
|
||||
|
||||
|
||||
|
||||
// GCI decoding logic
|
||||
|
||||
struct ShuffleTables {
|
||||
uint8_t forward_table[0x100];
|
||||
uint8_t reverse_table[0x100];
|
||||
|
||||
ShuffleTables(PSOV2Encryption& crypt) {
|
||||
for (size_t x = 0; x < 0x100; x++) {
|
||||
this->forward_table[x] = x;
|
||||
}
|
||||
|
||||
int32_t r28 = 0xFF;
|
||||
uint8_t* r31 = &this->forward_table[0xFF];
|
||||
while (r28 >= 0) {
|
||||
uint32_t r3 = this->pseudorand(crypt, r28 + 1);
|
||||
if (r3 >= 0x100) {
|
||||
throw logic_error("bad r3");
|
||||
}
|
||||
uint8_t t = this->forward_table[r3];
|
||||
this->forward_table[r3] = *r31;
|
||||
*r31 = t;
|
||||
|
||||
this->reverse_table[t] = r28;
|
||||
r31--;
|
||||
r28--;
|
||||
}
|
||||
}
|
||||
|
||||
static uint32_t pseudorand(PSOV2Encryption& crypt, uint32_t prev) {
|
||||
return (((prev & 0xFFFF) * ((crypt.next() >> 16) & 0xFFFF)) >> 16) & 0xFFFF;
|
||||
}
|
||||
|
||||
void shuffle(void* vdest, const void* vsrc, size_t size, bool reverse) {
|
||||
uint8_t* dest = reinterpret_cast<uint8_t*>(vdest);
|
||||
const uint8_t* src = reinterpret_cast<const uint8_t*>(vsrc);
|
||||
const uint8_t* table = reverse ? this->reverse_table : this->forward_table;
|
||||
|
||||
for (size_t block_offset = 0; block_offset < (size & 0xFFFFFF00); block_offset += 0x100) {
|
||||
for (size_t z = 0; z < 0x100; z++) {
|
||||
dest[block_offset + table[z]] = src[block_offset + z];
|
||||
}
|
||||
}
|
||||
|
||||
// Any remaining bytes that don't fill an entire block are not shuffled
|
||||
memcpy(&dest[size & 0xFFFFFF00], &src[size & 0xFFFFFF00], size & 0xFF);
|
||||
}
|
||||
};
|
||||
|
||||
struct PSOGCIFileHeader {
|
||||
parray<char, 4> game_id; // 'GPOE', 'GPSP', etc.
|
||||
parray<char, 2> developer_id; // '8P' for Sega
|
||||
parray<uint8_t, 0x3A> remaining_gci_header; // There is a structure for this but we don't use it
|
||||
ptext<char, 0x1C> game_name; // e.g. "PSO EPISODE I & II" or "PSO EPISODE III"
|
||||
be_uint32_t embedded_seed; // Used in some of Ralf's quest packs
|
||||
ptext<char, 0x20> quest_name;
|
||||
parray<uint8_t, 0x2000> banner_and_icon;
|
||||
// data_size specifies the number of bytes in the encrypted section, including
|
||||
// the encrypted header (below) and all encrypted data after it.
|
||||
be_uint32_t data_size;
|
||||
// To compute checksum, set checksum to zero, then compute the CRC32 of all
|
||||
// fields in this struct starting with game_name. (Yes, including the checksum
|
||||
// field, which is temporarily zero.)
|
||||
be_uint32_t checksum;
|
||||
|
||||
bool checksum_correct() const {
|
||||
uint32_t cs = crc32(&this->game_name, sizeof(this->game_name));
|
||||
cs = crc32(&this->embedded_seed, sizeof(this->embedded_seed), cs);
|
||||
cs = crc32(&this->quest_name, sizeof(this->quest_name), cs);
|
||||
cs = crc32(&this->banner_and_icon, sizeof(this->banner_and_icon), cs);
|
||||
cs = crc32(&this->data_size, sizeof(this->data_size), cs);
|
||||
cs = crc32("\0\0\0\0", 4, cs);
|
||||
return (cs == this->checksum);
|
||||
}
|
||||
} __attribute__((packed));
|
||||
|
||||
struct PSOGCIOrVMSFileEncryptedHeader {
|
||||
be_uint32_t round2_seed;
|
||||
// To compute checksum, set checksum to zero, then compute the CRC32 of the
|
||||
// entire data section, including this header struct (but not the unencrypted
|
||||
// header struct).
|
||||
be_uint32_t checksum;
|
||||
le_uint32_t decompressed_size;
|
||||
le_uint32_t round3_seed;
|
||||
// Data follows here.
|
||||
} __attribute__((packed));
|
||||
|
||||
template <bool IsBigEndian>
|
||||
string decrypt_gci_or_vms_v2_data_section(
|
||||
const void* data_section,
|
||||
size_t size,
|
||||
uint32_t seed,
|
||||
bool use_reverse_table) {
|
||||
|
||||
string decrypted(size, '\0');
|
||||
{
|
||||
PSOV2Encryption shuf_crypt(seed);
|
||||
ShuffleTables shuf(shuf_crypt);
|
||||
shuf.shuffle(decrypted.data(), data_section, size, use_reverse_table);
|
||||
}
|
||||
|
||||
PSOV2Encryption crypt(seed);
|
||||
if (IsBigEndian) {
|
||||
auto* be_dwords = reinterpret_cast<be_uint32_t*>(decrypted.data());
|
||||
for (size_t z = 0; z < decrypted.size() / sizeof(be_uint32_t); z++) {
|
||||
be_dwords[z] = crypt.next() - be_dwords[z];
|
||||
}
|
||||
} else {
|
||||
auto* le_dwords = reinterpret_cast<le_uint32_t*>(decrypted.data());
|
||||
for (size_t z = 0; z < decrypted.size() / sizeof(le_uint32_t); z++) {
|
||||
le_dwords[z] = crypt.next() - le_dwords[z];
|
||||
}
|
||||
}
|
||||
|
||||
auto* header = reinterpret_cast<PSOGCIOrVMSFileEncryptedHeader*>(
|
||||
decrypted.data());
|
||||
PSOV2Encryption round2_crypt(header->round2_seed);
|
||||
if (IsBigEndian) {
|
||||
round2_crypt.encrypt_big_endian(
|
||||
decrypted.data() + 4, (decrypted.size() - 4) & (~3));
|
||||
} else {
|
||||
round2_crypt.decrypt(
|
||||
decrypted.data() + 4, (decrypted.size() - 4) & (~3));
|
||||
}
|
||||
|
||||
uint32_t expected_crc = header->checksum;
|
||||
header->checksum = 0;
|
||||
uint32_t actual_crc = crc32(decrypted.data(), decrypted.size());
|
||||
header->checksum = expected_crc;
|
||||
if (expected_crc != actual_crc) {
|
||||
throw runtime_error("incorrect decrypted data section checksum");
|
||||
}
|
||||
|
||||
if (header->decompressed_size & 0xFFF00000) {
|
||||
throw runtime_error("decompressed_size too large");
|
||||
}
|
||||
|
||||
size_t orig_size = decrypted.size();
|
||||
decrypted.resize((orig_size + 3) & (~3));
|
||||
PSOV2Encryption(header->round3_seed).decrypt(
|
||||
decrypted.data() + sizeof(PSOGCIOrVMSFileEncryptedHeader),
|
||||
decrypted.size() - sizeof(PSOGCIOrVMSFileEncryptedHeader));
|
||||
decrypted.resize(orig_size);
|
||||
|
||||
// Some GCI files have decompressed_size fields that are 8 bytes smaller than
|
||||
// the actual decompressed size of the data. They seem to work fine, so we
|
||||
// accept both cases as correct.
|
||||
size_t decompressed_size = prs_decompress_size(
|
||||
decrypted.data() + sizeof(PSOGCIOrVMSFileEncryptedHeader),
|
||||
decrypted.size() - sizeof(PSOGCIOrVMSFileEncryptedHeader));
|
||||
if ((decompressed_size != header->decompressed_size) &&
|
||||
(decompressed_size != header->decompressed_size - 8)) {
|
||||
throw runtime_error(string_printf(
|
||||
"decompressed size (%zu) does not match size in header (%" PRId32 ")",
|
||||
decompressed_size, header->decompressed_size.load()));
|
||||
}
|
||||
|
||||
return decrypted.substr(sizeof(PSOGCIOrVMSFileEncryptedHeader));
|
||||
}
|
||||
|
||||
string decrypt_vms_v1_data_section(const void* data_section, size_t size) {
|
||||
StringReader r(data_section, size);
|
||||
uint32_t expected_decompressed_size = r.get_u32l();
|
||||
uint32_t seed = r.get_u32l();
|
||||
|
||||
string data = r.read(r.remaining());
|
||||
|
||||
size_t orig_size = data.size();
|
||||
data.resize((orig_size + 3) & (~3));
|
||||
PSOV2Encryption(seed).decrypt(data.data(), data.size());
|
||||
data.resize(orig_size);
|
||||
|
||||
size_t actual_decompressed_size = prs_decompress_size(data);
|
||||
if (actual_decompressed_size != expected_decompressed_size) {
|
||||
throw runtime_error(string_printf(
|
||||
"decompressed size (%zu) does not match size in header (%" PRId32 ")",
|
||||
actual_decompressed_size, expected_decompressed_size));
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
template <bool IsBigEndian>
|
||||
string find_seed_and_decrypt_gci_or_vms_v2_data_section(
|
||||
const void* data_section, size_t size, size_t num_threads) {
|
||||
mutex result_lock;
|
||||
string result;
|
||||
uint64_t result_seed = parallel_range<uint64_t>([&](uint64_t seed, size_t) {
|
||||
try {
|
||||
string ret = decrypt_gci_or_vms_v2_data_section<IsBigEndian>(
|
||||
data_section, size, seed, true);
|
||||
lock_guard<mutex> g(result_lock);
|
||||
result = move(ret);
|
||||
return true;
|
||||
} catch (const runtime_error&) {
|
||||
return false;
|
||||
}
|
||||
}, 0, 0x100000000, num_threads);
|
||||
|
||||
if (!result.empty() && (result_seed < 0x100000000)) {
|
||||
static_game_data_log.info("Found seed %08" PRIX64 " to decrypt GCI file",
|
||||
result_seed);
|
||||
return result;
|
||||
} else {
|
||||
throw runtime_error("no seed found");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
struct PSOVMSFileHeader {
|
||||
ptext<char, 0x10> short_desc; // "PSO/DOWNLOAD " or "PSOV2/DOWNLOAD "
|
||||
ptext<char, 0x20> long_desc; // Usually quest name
|
||||
ptext<char, 0x10> creator_id;
|
||||
le_uint16_t num_icons;
|
||||
le_uint16_t animation_speed;
|
||||
le_uint16_t eyecatch_type;
|
||||
le_uint16_t crc;
|
||||
le_uint32_t data_size; // Not including header and icons
|
||||
parray<uint8_t, 0x14> unused;
|
||||
parray<le_uint16_t, 0x10> icon_palette;
|
||||
|
||||
// Variable-length field follows here:
|
||||
// parray<parray<uint8_t, 0x200>, num_icons> icon;
|
||||
|
||||
bool checksum_correct() const {
|
||||
auto add_data = +[](const void* data, size_t size, uint16_t crc) -> uint16_t {
|
||||
const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data);
|
||||
for (size_t z = 0; z < size; z++) {
|
||||
crc ^= (static_cast<uint16_t>(bytes[z]) << 8);
|
||||
for (uint8_t bit = 0; bit < 8; bit++) {
|
||||
if (crc & 0x8000) {
|
||||
crc = (crc << 1) ^ 0x1021;
|
||||
} else {
|
||||
crc = (crc << 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return crc;
|
||||
};
|
||||
|
||||
uint16_t crc = add_data(this, offsetof(PSOVMSFileHeader, crc), 0);
|
||||
crc = add_data("\0\0", 2, crc);
|
||||
crc = add_data(&this->data_size,
|
||||
sizeof(PSOVMSFileHeader) - offsetof(PSOVMSFileHeader, data_size) + this->num_icons * 0x200 + this->data_size, crc);
|
||||
return (crc == this->crc);
|
||||
}
|
||||
} __attribute__((packed));
|
||||
|
||||
|
||||
|
||||
struct PSODownloadQuestHeader {
|
||||
// When sending a DLQ to the client, this is the DECOMPRESSED size. When
|
||||
// reading it from a GCI file, this is the COMPRESSED size.
|
||||
le_uint32_t size;
|
||||
// Note: use PSO PC encryption, even for GC quests.
|
||||
le_uint32_t encryption_seed;
|
||||
} __attribute__((packed));
|
||||
|
||||
@@ -85,7 +336,7 @@ static const char* name_for_episode(uint8_t episode) {
|
||||
|
||||
|
||||
|
||||
struct PSOQuestHeaderDC { // same for dc v1 and v2, thankfully
|
||||
struct PSOQuestHeaderDC { // Same format for DC v1 and v2, thankfully
|
||||
uint32_t start_offset;
|
||||
uint32_t unknown_offset1;
|
||||
uint32_t size;
|
||||
@@ -111,6 +362,8 @@ struct PSOQuestHeaderPC {
|
||||
ptext<char16_t, 0x120> long_description;
|
||||
} __attribute__((packed));
|
||||
|
||||
// TODO: Is the XB quest header format the same as on GC? If not, make a
|
||||
// separate struct; if so, rename this struct to V3.
|
||||
struct PSOQuestHeaderGC {
|
||||
uint32_t start_offset;
|
||||
uint32_t unknown_offset1;
|
||||
@@ -119,24 +372,12 @@ struct PSOQuestHeaderGC {
|
||||
uint8_t is_download;
|
||||
uint8_t unknown1;
|
||||
uint8_t quest_number;
|
||||
uint8_t episode; // 1 = ep2. apparently some quests have 0xFF here, which means ep1 (?)
|
||||
uint8_t episode; // 1 = Ep2. Apparently some quests have 0xFF here, which means ep1 (?)
|
||||
ptext<char, 0x20> name;
|
||||
ptext<char, 0x80> short_description;
|
||||
ptext<char, 0x120> long_description;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct PSOQuestHeaderGCEpisode3 {
|
||||
// there's actually a lot of other important stuff in here but I'm lazy. it
|
||||
// looks like map data, cutscene data, and maybe special cards used during
|
||||
// the quest
|
||||
parray<uint8_t, 0x1DF0> unknown_a1;
|
||||
ptext<char, 0x14> name;
|
||||
ptext<char, 0x14> location;
|
||||
ptext<char, 0x3C> location2;
|
||||
ptext<char, 0x190> description;
|
||||
parray<uint8_t, 0x3A34> unknown_a2;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct PSOQuestHeaderBB {
|
||||
uint32_t start_offset;
|
||||
uint32_t unknown_offset1;
|
||||
@@ -144,7 +385,7 @@ struct PSOQuestHeaderBB {
|
||||
uint32_t unused;
|
||||
uint16_t quest_number; // 0xFFFF for challenge quests
|
||||
uint16_t unused2;
|
||||
uint8_t episode; // 0 = ep1, 1 = ep2, 2 = ep4
|
||||
uint8_t episode; // 0 = Ep1, 1 = Ep2, 2 = Ep4
|
||||
uint8_t max_players;
|
||||
uint8_t joinable_in_progress;
|
||||
uint8_t unknown;
|
||||
@@ -162,40 +403,49 @@ Quest::Quest(const string& bin_filename)
|
||||
episode(0),
|
||||
is_dcv1(false),
|
||||
joinable(false),
|
||||
file_format(FileFormat::BIN_DAT) {
|
||||
file_format(FileFormat::BIN_DAT),
|
||||
has_mnm_extension(false) {
|
||||
|
||||
if (ends_with(bin_filename, ".bin.gci")) {
|
||||
if (ends_with(bin_filename, ".bin.gci") || ends_with(bin_filename, ".mnm.gci")) {
|
||||
this->file_format = FileFormat::BIN_DAT_GCI;
|
||||
this->has_mnm_extension = ends_with(bin_filename, ".mnm.gci");
|
||||
this->file_basename = bin_filename.substr(0, bin_filename.size() - 8);
|
||||
} else if (ends_with(bin_filename, ".bin.dlq")) {
|
||||
} else if (ends_with(bin_filename, ".bin.vms")) {
|
||||
this->file_format = FileFormat::BIN_DAT_VMS;
|
||||
this->file_basename = bin_filename.substr(0, bin_filename.size() - 8);
|
||||
} else if (ends_with(bin_filename, ".bin.dlq") || ends_with(bin_filename, ".mnm.dlq")) {
|
||||
this->file_format = FileFormat::BIN_DAT_DLQ;
|
||||
this->has_mnm_extension = ends_with(bin_filename, ".mnm.dlq");
|
||||
this->file_basename = bin_filename.substr(0, bin_filename.size() - 8);
|
||||
} else if (ends_with(bin_filename, ".qst")) {
|
||||
this->file_format = FileFormat::QST;
|
||||
this->file_basename = bin_filename.substr(0, bin_filename.size() - 4);
|
||||
} else if (ends_with(bin_filename, ".bin")) {
|
||||
} else if (ends_with(bin_filename, ".bin") || ends_with(bin_filename, ".mnm")) {
|
||||
this->file_format = FileFormat::BIN_DAT;
|
||||
this->has_mnm_extension = ends_with(bin_filename, ".mnm");
|
||||
this->file_basename = bin_filename.substr(0, bin_filename.size() - 4);
|
||||
} else if (ends_with(bin_filename, ".bind") || ends_with(bin_filename, ".mnmd")) {
|
||||
this->file_format = FileFormat::BIN_DAT_UNCOMPRESSED;
|
||||
this->has_mnm_extension = ends_with(bin_filename, ".mnmd");
|
||||
this->file_basename = bin_filename.substr(0, bin_filename.size() - 5);
|
||||
} else {
|
||||
throw runtime_error("quest does not have a valid .bin file");
|
||||
throw runtime_error("quest does not have a valid .bin or .mnm file");
|
||||
}
|
||||
|
||||
string basename;
|
||||
{
|
||||
size_t slash_pos = bin_filename.rfind('/');
|
||||
size_t slash_pos = this->file_basename.rfind('/');
|
||||
if (slash_pos != string::npos) {
|
||||
basename = bin_filename.substr(slash_pos + 1);
|
||||
basename = this->file_basename.substr(slash_pos + 1);
|
||||
} else {
|
||||
basename = bin_filename;
|
||||
basename = this->file_basename;
|
||||
}
|
||||
}
|
||||
bool has_short_extension = (this->file_format == FileFormat::BIN_DAT) ||
|
||||
(this->file_format == FileFormat::QST);
|
||||
basename.resize(basename.size() - (has_short_extension ? 4 : 8));
|
||||
|
||||
// quest filenames are like:
|
||||
// Quest filenames are like:
|
||||
// b###-VV.bin for battle mode
|
||||
// c###-VV.bin for challenge mode
|
||||
// e###-gc3.bin for episode 3
|
||||
// e###-gc3.mnm (or .bin) for episode 3
|
||||
// q###-CAT-VV.bin for normal quests
|
||||
|
||||
if (basename.empty()) {
|
||||
@@ -212,19 +462,23 @@ Quest::Quest(const string& bin_filename)
|
||||
throw invalid_argument("filename does not indicate mode");
|
||||
}
|
||||
|
||||
// if the quest category is still unknown, expect 3 tokens (one of them will
|
||||
if (this->category != QuestCategory::EPISODE_3 && this->has_mnm_extension) {
|
||||
throw invalid_argument("non-Ep3 quest has .mnm extension");
|
||||
}
|
||||
|
||||
// If the quest category is still unknown, expect 3 tokens (one of them will
|
||||
// tell us the category)
|
||||
vector<string> tokens = split(basename, '-');
|
||||
if (tokens.size() != (2 + (this->category == QuestCategory::UNKNOWN))) {
|
||||
throw invalid_argument("incorrect filename format");
|
||||
}
|
||||
|
||||
// parse the number out of the first token
|
||||
// Parse the number out of the first token
|
||||
this->internal_id = strtoull(tokens[0].c_str() + 1, nullptr, 10);
|
||||
|
||||
// get the category from the second token if needed
|
||||
// Get the category from the second token if needed
|
||||
if (this->category == QuestCategory::UNKNOWN) {
|
||||
static const unordered_map<std::string, QuestCategory> name_to_category({
|
||||
static const unordered_map<string, QuestCategory> name_to_category({
|
||||
{"ret", QuestCategory::RETRIEVAL},
|
||||
{"ext", QuestCategory::EXTERMINATION},
|
||||
{"evt", QuestCategory::EVENT},
|
||||
@@ -232,7 +486,7 @@ Quest::Quest(const string& bin_filename)
|
||||
{"vr", QuestCategory::VR},
|
||||
{"twr", QuestCategory::TOWER},
|
||||
// Note: This will be overwritten later for Episode 2 & 4 quests - we
|
||||
// haven't parsed the episode from the quest script yet
|
||||
// haven't parsed the episode number from the quest script yet
|
||||
{"gov", QuestCategory::GOVERNMENT_EPISODE_1},
|
||||
{"dl", QuestCategory::DOWNLOAD},
|
||||
{"1p", QuestCategory::SOLO},
|
||||
@@ -241,17 +495,18 @@ Quest::Quest(const string& bin_filename)
|
||||
tokens.erase(tokens.begin() + 1);
|
||||
}
|
||||
|
||||
static const unordered_map<std::string, GameVersion> name_to_version({
|
||||
static const unordered_map<string, GameVersion> name_to_version({
|
||||
{"d1", GameVersion::DC},
|
||||
{"dc", GameVersion::DC},
|
||||
{"pc", GameVersion::PC},
|
||||
{"gc", GameVersion::GC},
|
||||
{"gc3", GameVersion::GC},
|
||||
{"xb", GameVersion::XB},
|
||||
{"bb", GameVersion::BB},
|
||||
});
|
||||
this->version = name_to_version.at(tokens[1]);
|
||||
|
||||
// the rest of the information needs to be fetched from the .bin file's
|
||||
// The rest of the information needs to be fetched from the .bin file's
|
||||
// contents
|
||||
|
||||
auto bin_compressed = this->bin_contents();
|
||||
@@ -289,17 +544,17 @@ Quest::Quest(const string& bin_filename)
|
||||
break;
|
||||
}
|
||||
|
||||
case GameVersion::XB:
|
||||
case GameVersion::GC: {
|
||||
if (this->category == QuestCategory::EPISODE_3) {
|
||||
// these all appear to be the same size
|
||||
if (bin_decompressed.size() != sizeof(PSOQuestHeaderGCEpisode3)) {
|
||||
if (bin_decompressed.size() != sizeof(Episode3::MapDefinition)) {
|
||||
throw invalid_argument("file is incorrect size");
|
||||
}
|
||||
auto* header = reinterpret_cast<const PSOQuestHeaderGCEpisode3*>(bin_decompressed.data());
|
||||
auto* header = reinterpret_cast<const Episode3::MapDefinition*>(bin_decompressed.data());
|
||||
this->joinable = false;
|
||||
this->episode = 0xFF;
|
||||
this->name = decode_sjis(header->name);
|
||||
this->short_description = decode_sjis(header->location2);
|
||||
this->short_description = decode_sjis(header->quest_name);
|
||||
this->long_description = decode_sjis(header->description);
|
||||
} else {
|
||||
if (bin_decompressed.size() < sizeof(PSOQuestHeaderGC)) {
|
||||
@@ -342,7 +597,7 @@ Quest::Quest(const string& bin_filename)
|
||||
}
|
||||
}
|
||||
|
||||
static string basename_for_filename(const std::string& filename) {
|
||||
static string basename_for_filename(const string& filename) {
|
||||
size_t slash_pos = filename.rfind('/');
|
||||
if (slash_pos != string::npos) {
|
||||
return filename.substr(slash_pos + 1);
|
||||
@@ -350,25 +605,44 @@ static string basename_for_filename(const std::string& filename) {
|
||||
return filename;
|
||||
}
|
||||
|
||||
std::string Quest::bin_filename() const {
|
||||
return basename_for_filename(this->file_basename + ".bin");
|
||||
string Quest::bin_filename() const {
|
||||
if (this->category == QuestCategory::EPISODE_3) {
|
||||
return string_printf("m%06" PRId64 "p_e.bin", this->internal_id);
|
||||
} else {
|
||||
return basename_for_filename(this->file_basename + ".bin");
|
||||
}
|
||||
}
|
||||
|
||||
std::string Quest::dat_filename() const {
|
||||
return basename_for_filename(this->file_basename + ".dat");
|
||||
string Quest::dat_filename() const {
|
||||
if (this->category == QuestCategory::EPISODE_3) {
|
||||
throw logic_error("Episode 3 quests do not have .dat files");
|
||||
} else {
|
||||
return basename_for_filename(this->file_basename + ".dat");
|
||||
}
|
||||
}
|
||||
|
||||
shared_ptr<const string> Quest::bin_contents() const {
|
||||
if (!this->bin_contents_ptr) {
|
||||
switch (this->file_format) {
|
||||
case FileFormat::BIN_DAT:
|
||||
this->bin_contents_ptr.reset(new string(load_file(this->file_basename + ".bin")));
|
||||
this->bin_contents_ptr.reset(new string(load_file(
|
||||
this->file_basename + (this->has_mnm_extension ? ".mnm" : ".bin"))));
|
||||
break;
|
||||
case FileFormat::BIN_DAT_UNCOMPRESSED:
|
||||
this->bin_contents_ptr.reset(new string(prs_compress(load_file(
|
||||
this->file_basename + (this->has_mnm_extension ? ".mnmd" : ".bind")))));
|
||||
break;
|
||||
case FileFormat::BIN_DAT_GCI:
|
||||
this->bin_contents_ptr.reset(new string(this->decode_gci(this->file_basename + ".bin.gci")));
|
||||
this->bin_contents_ptr.reset(new string(this->decode_gci(
|
||||
this->file_basename + (this->has_mnm_extension ? ".mnm.gci" : ".bin.gci"))));
|
||||
break;
|
||||
case FileFormat::BIN_DAT_VMS:
|
||||
this->bin_contents_ptr.reset(new string(this->decode_vms(
|
||||
this->file_basename + (this->has_mnm_extension ? ".mnm.vms" : ".bin.vms"))));
|
||||
break;
|
||||
case FileFormat::BIN_DAT_DLQ:
|
||||
this->bin_contents_ptr.reset(new string(this->decode_dlq(this->file_basename + ".bin.dlq")));
|
||||
this->bin_contents_ptr.reset(new string(this->decode_dlq(
|
||||
this->file_basename + (this->has_mnm_extension ? ".mnm.dlq" : ".bin.dlq"))));
|
||||
break;
|
||||
case FileFormat::QST: {
|
||||
auto result = this->decode_qst(this->file_basename + ".qst");
|
||||
@@ -384,14 +658,23 @@ shared_ptr<const string> Quest::bin_contents() const {
|
||||
}
|
||||
|
||||
shared_ptr<const string> Quest::dat_contents() const {
|
||||
if (this->category == QuestCategory::EPISODE_3) {
|
||||
throw logic_error("Episode 3 quests do not have .dat files");
|
||||
}
|
||||
if (!this->dat_contents_ptr) {
|
||||
switch (this->file_format) {
|
||||
case FileFormat::BIN_DAT:
|
||||
this->dat_contents_ptr.reset(new string(load_file(this->file_basename + ".dat")));
|
||||
break;
|
||||
case FileFormat::BIN_DAT_UNCOMPRESSED:
|
||||
this->dat_contents_ptr.reset(new string(prs_compress(load_file(this->file_basename + ".datd"))));
|
||||
break;
|
||||
case FileFormat::BIN_DAT_GCI:
|
||||
this->dat_contents_ptr.reset(new string(this->decode_gci(this->file_basename + ".dat.gci")));
|
||||
break;
|
||||
case FileFormat::BIN_DAT_VMS:
|
||||
this->dat_contents_ptr.reset(new string(this->decode_vms(this->file_basename + ".dat.vms")));
|
||||
break;
|
||||
case FileFormat::BIN_DAT_DLQ:
|
||||
this->dat_contents_ptr.reset(new string(this->decode_dlq(this->file_basename + ".dat.dlq")));
|
||||
break;
|
||||
@@ -408,51 +691,132 @@ shared_ptr<const string> Quest::dat_contents() const {
|
||||
return this->dat_contents_ptr;
|
||||
}
|
||||
|
||||
string Quest::decode_gci(const string& filename) {
|
||||
|
||||
string Quest::decode_gci(
|
||||
const string& filename, ssize_t find_seed_num_threads, int64_t known_seed) {
|
||||
string data = load_file(filename);
|
||||
if (data.size() < 0x2080 + sizeof(PSODownloadQuestHeader)) {
|
||||
throw runtime_error(string_printf(
|
||||
"GCI file is truncated before download quest header (have 0x%zX bytes)", data.size()));
|
||||
}
|
||||
PSODownloadQuestHeader* h = reinterpret_cast<PSODownloadQuestHeader*>(
|
||||
data.data() + 0x2080);
|
||||
|
||||
string compressed_data_with_header = data.substr(0x2088, h->size);
|
||||
|
||||
// For now, we can only load unencrypted quests, unfortunately
|
||||
// TODO: Figure out how GCI encryption works and implement it here.
|
||||
|
||||
// Unlike the DLQ header, this one is stored little-endian. The compressed
|
||||
// data immediately follows this header.
|
||||
struct DecryptedHeader {
|
||||
uint32_t unknown1;
|
||||
uint32_t unknown2;
|
||||
uint32_t decompressed_size;
|
||||
uint32_t unknown4;
|
||||
} __attribute__((packed));
|
||||
if (compressed_data_with_header.size() < sizeof(DecryptedHeader)) {
|
||||
throw runtime_error("GCI file compressed data truncated during header");
|
||||
}
|
||||
DecryptedHeader* dh = reinterpret_cast<DecryptedHeader*>(
|
||||
compressed_data_with_header.data());
|
||||
if (dh->unknown1 || dh->unknown2 || dh->unknown4) {
|
||||
throw runtime_error("GCI file appears to be encrypted");
|
||||
StringReader r(data);
|
||||
const auto& header = r.get<PSOGCIFileHeader>();
|
||||
if (!header.checksum_correct()) {
|
||||
throw runtime_error("GCI file unencrypted header checksum is incorrect");
|
||||
}
|
||||
|
||||
string data_to_decompress = compressed_data_with_header.substr(sizeof(DecryptedHeader));
|
||||
size_t decompressed_bytes = prs_decompress_size(data_to_decompress);
|
||||
|
||||
size_t expected_decompressed_bytes = dh->decompressed_size - 8;
|
||||
if (decompressed_bytes < expected_decompressed_bytes) {
|
||||
throw runtime_error(string_printf(
|
||||
"GCI decompressed data is smaller than expected size (have 0x%zX bytes, expected 0x%zX bytes)",
|
||||
decompressed_bytes, expected_decompressed_bytes));
|
||||
if (header.developer_id[0] != '8' || header.developer_id[1] != 'P') {
|
||||
throw runtime_error("GCI file is not for a Sega game");
|
||||
}
|
||||
if (header.game_id[0] != 'G') {
|
||||
throw runtime_error("GCI file is not for a GameCube game");
|
||||
}
|
||||
if (header.game_id[1] != 'P') {
|
||||
throw runtime_error("GCI file is not for Phantasy Star Online");
|
||||
}
|
||||
|
||||
// The caller expects to get PRS-compressed data when calling bin_contents()
|
||||
// and dat_contents(), so we shouldn't decompress it here.
|
||||
return data_to_decompress;
|
||||
if (header.game_id[2] == 'O') { // Episodes 1&2 (GPO*)
|
||||
const auto& encrypted_header = r.get<PSOGCIOrVMSFileEncryptedHeader>(false);
|
||||
// Unencrypted GCI files appear to always have zeroes in these fields.
|
||||
// Encrypted GCI files are highly unlikely to have zeroes in ALL of these
|
||||
// fields, so assume it's encrypted if any of them are nonzero.
|
||||
if (encrypted_header.round2_seed || encrypted_header.checksum || encrypted_header.round3_seed) {
|
||||
if (known_seed >= 0) {
|
||||
return decrypt_gci_or_vms_v2_data_section<true>(
|
||||
r.getv(header.data_size), header.data_size, known_seed, true);
|
||||
|
||||
} else if (header.embedded_seed != 0) {
|
||||
return decrypt_gci_or_vms_v2_data_section<true>(
|
||||
r.getv(header.data_size), header.data_size, header.embedded_seed, true);
|
||||
|
||||
} else {
|
||||
if (find_seed_num_threads < 0) {
|
||||
throw runtime_error("GCI file appears to be encrypted");
|
||||
}
|
||||
if (find_seed_num_threads == 0) {
|
||||
find_seed_num_threads = thread::hardware_concurrency();
|
||||
}
|
||||
return find_seed_and_decrypt_gci_or_vms_v2_data_section<true>(
|
||||
r.getv(header.data_size), header.data_size, find_seed_num_threads);
|
||||
}
|
||||
|
||||
} else { // Unencrypted GCI format
|
||||
r.skip(sizeof(PSOGCIOrVMSFileEncryptedHeader));
|
||||
string compressed_data = r.readx(header.data_size - sizeof(PSOGCIOrVMSFileEncryptedHeader));
|
||||
size_t decompressed_bytes = prs_decompress_size(compressed_data);
|
||||
|
||||
size_t expected_decompressed_bytes = encrypted_header.decompressed_size - 8;
|
||||
if (decompressed_bytes < expected_decompressed_bytes) {
|
||||
throw runtime_error(string_printf(
|
||||
"GCI decompressed data is smaller than expected size (have 0x%zX bytes, expected 0x%zX bytes)",
|
||||
decompressed_bytes, expected_decompressed_bytes));
|
||||
}
|
||||
|
||||
return compressed_data;
|
||||
}
|
||||
|
||||
} else if (header.game_id[2] == 'S') { // Episode 3
|
||||
// The first 0x10 bytes in the data segment appear to be unused. In most
|
||||
// files I've seen, the last half of it (8 bytes) are duplicates of the
|
||||
// first 8 bytes of the unscrambled, compressed data, though this is the
|
||||
// result of an uninitialized memory bug when the client encodes the file
|
||||
// and not an actual constraint on what should be in these 8 bytes.
|
||||
r.skip(16);
|
||||
// The game treats this field as a 16-byte string (including the \0). The 8
|
||||
// bytes after it appear to be completely unused.
|
||||
if (r.readx(15) != "SONICTEAM,SEGA.") {
|
||||
throw runtime_error("Episode 3 GCI file is not a quest");
|
||||
}
|
||||
r.skip(9);
|
||||
|
||||
data = r.readx(header.data_size - 40);
|
||||
|
||||
// For some reason, Sega decided not to encrypt Episode 3 quest files in the
|
||||
// same way as Episodes 1&2 quest files (see above). Instead, they just
|
||||
// wrote a fairly trivial XOR loop over the first 0x100 bytes, leaving the
|
||||
// remaining bytes completely unencrypted (but still compressed).
|
||||
size_t unscramble_size = min<size_t>(0x100, data.size());
|
||||
decrypt_trivial_gci_data(data.data(), unscramble_size, 0);
|
||||
|
||||
size_t decompressed_size = prs_decompress_size(data);
|
||||
if (decompressed_size != sizeof(Episode3::MapDefinition)) {
|
||||
throw runtime_error(string_printf(
|
||||
"decompressed quest is 0x%zX bytes; expected 0x%zX bytes",
|
||||
decompressed_size, sizeof(Episode3::MapDefinition)));
|
||||
}
|
||||
return data;
|
||||
|
||||
} else {
|
||||
throw runtime_error("unknown game name in GCI header");
|
||||
}
|
||||
}
|
||||
|
||||
string Quest::decode_vms(
|
||||
const string& filename, ssize_t find_seed_num_threads, int64_t known_seed) {
|
||||
string data = load_file(filename);
|
||||
|
||||
StringReader r(data);
|
||||
const auto& header = r.get<PSOVMSFileHeader>();
|
||||
if (!header.checksum_correct()) {
|
||||
throw runtime_error("VMS file unencrypted header checksum is incorrect");
|
||||
}
|
||||
r.skip(header.num_icons * 0x200);
|
||||
|
||||
const void* data_section = r.getv(header.data_size);
|
||||
try {
|
||||
return decrypt_vms_v1_data_section(data_section, header.data_size);
|
||||
} catch (const exception& e) { }
|
||||
|
||||
if (known_seed >= 0) {
|
||||
return decrypt_gci_or_vms_v2_data_section<false>(
|
||||
data_section, header.data_size, known_seed, true);
|
||||
|
||||
} else {
|
||||
if (find_seed_num_threads < 0) {
|
||||
throw runtime_error("GCI file appears to be encrypted");
|
||||
}
|
||||
if (find_seed_num_threads == 0) {
|
||||
find_seed_num_threads = thread::hardware_concurrency();
|
||||
}
|
||||
return find_seed_and_decrypt_gci_or_vms_v2_data_section<false>(
|
||||
data_section, header.data_size, find_seed_num_threads);
|
||||
}
|
||||
}
|
||||
|
||||
string Quest::decode_dlq(const string& filename) {
|
||||
@@ -466,12 +830,11 @@ string Quest::decode_dlq(const string& filename) {
|
||||
data = read_all(f.get());
|
||||
}
|
||||
|
||||
PSOPCEncryption encr(key);
|
||||
|
||||
// The compressed data size does not need to be a multiple of 4, but the PC
|
||||
// The compressed data size does not need to be a multiple of 4, but the V2
|
||||
// encryption (which is used for all download quests, even in V3) requires the
|
||||
// data size to be a multiple of 4. We'll just temporarily stick a few bytes
|
||||
// on the end, then throw them away later if needed.
|
||||
PSOV2Encryption encr(key);
|
||||
size_t original_size = data.size();
|
||||
data.resize((data.size() + 3) & (~3));
|
||||
encr.decrypt(data);
|
||||
@@ -495,6 +858,7 @@ static pair<string, string> decode_qst_t(FILE* f) {
|
||||
string internal_dat_filename;
|
||||
uint32_t bin_file_size = 0;
|
||||
uint32_t dat_file_size = 0;
|
||||
Quest::FileFormat subformat = Quest::FileFormat::QST; // Stand-in for unknown
|
||||
while (!r.eof()) {
|
||||
// Handle BB's implicit 8-byte command alignment
|
||||
static constexpr size_t alignment = sizeof(HeaderT);
|
||||
@@ -505,7 +869,22 @@ static pair<string, string> decode_qst_t(FILE* f) {
|
||||
}
|
||||
|
||||
const auto& header = r.get<HeaderT>();
|
||||
if (header.command == 0x44) {
|
||||
|
||||
if (header.command == 0x44 || header.command == 0x13) {
|
||||
if (subformat == Quest::FileFormat::QST) {
|
||||
subformat = Quest::FileFormat::BIN_DAT;
|
||||
} else if (subformat != Quest::FileFormat::BIN_DAT) {
|
||||
throw runtime_error("QST file contains mixed download and non-download commands");
|
||||
}
|
||||
} else if (header.command == 0xA6 || header.command == 0xA7) {
|
||||
if (subformat == Quest::FileFormat::QST) {
|
||||
subformat = Quest::FileFormat::BIN_DAT_DLQ;
|
||||
} else if (subformat != Quest::FileFormat::BIN_DAT_DLQ) {
|
||||
throw runtime_error("QST file contains mixed download and non-download commands");
|
||||
}
|
||||
}
|
||||
|
||||
if (header.command == 0x44 || header.command == 0xA6) {
|
||||
if (header.size != sizeof(HeaderT) + sizeof(OpenFileT)) {
|
||||
throw runtime_error("qst open file command has incorrect size");
|
||||
}
|
||||
@@ -532,7 +911,7 @@ static pair<string, string> decode_qst_t(FILE* f) {
|
||||
throw runtime_error("qst contains non-bin, non-dat file");
|
||||
}
|
||||
|
||||
} else if (header.command == 0x13) {
|
||||
} else if (header.command == 0x13 || header.command == 0xA7) {
|
||||
if (header.size != sizeof(HeaderT) + sizeof(S_WriteFile_13_A7)) {
|
||||
throw runtime_error("qst write file command has incorrect size");
|
||||
}
|
||||
@@ -557,7 +936,7 @@ static pair<string, string> decode_qst_t(FILE* f) {
|
||||
if (header.flag != dest->size() / 0x400) {
|
||||
throw runtime_error("qst contains chunks out of order");
|
||||
}
|
||||
dest->append(reinterpret_cast<const char*>(cmd.data), cmd.data_size);
|
||||
dest->append(reinterpret_cast<const char*>(cmd.data.data()), cmd.data_size);
|
||||
|
||||
} else {
|
||||
throw runtime_error("invalid command in qst file");
|
||||
@@ -571,26 +950,31 @@ static pair<string, string> decode_qst_t(FILE* f) {
|
||||
throw runtime_error("dat file does not match expected size");
|
||||
}
|
||||
|
||||
if (subformat == Quest::FileFormat::BIN_DAT_DLQ) {
|
||||
bin_contents = Quest::decode_dlq(bin_contents);
|
||||
dat_contents = Quest::decode_dlq(dat_contents);
|
||||
}
|
||||
|
||||
return make_pair(bin_contents, dat_contents);
|
||||
}
|
||||
|
||||
pair<string, string> Quest::decode_qst(const string& filename) {
|
||||
auto f = fopen_unique(filename, "rb");
|
||||
|
||||
// qst files start with an open file command, but the format differs depending
|
||||
// QST files start with an open file command, but the format differs depending
|
||||
// on the PSO version that the qst file is for. We can detect the format from
|
||||
// the first 4 bytes in the file:
|
||||
// - BB: 58 00 44 00
|
||||
// - PC: 3C ?? 44 00
|
||||
// - DC/GC: 44 ?? 3C 00
|
||||
// - BB: 58 00 44 00 or 58 00 A6 00
|
||||
// - PC: 3C 00 44 ?? or 3C 00 A6 ??
|
||||
// - DC/V3: 44 ?? 3C 00 or A6 ?? 3C 00
|
||||
uint32_t signature = freadx<be_uint32_t>(f.get());
|
||||
fseek(f.get(), 0, SEEK_SET);
|
||||
if (signature == 0x58004400) {
|
||||
if (signature == 0x58004400 || signature == 0x5800A600) {
|
||||
return decode_qst_t<PSOCommandHeaderBB, S_OpenFile_BB_44_A6>(f.get());
|
||||
} else if ((signature & 0xFF00FFFF) == 0x3C004400) {
|
||||
return decode_qst_t<PSOCommandHeaderPC, S_OpenFile_PC_GC_44_A6>(f.get());
|
||||
} else if ((signature & 0xFF00FFFF) == 0x44003C00) {
|
||||
return decode_qst_t<PSOCommandHeaderDCGC, S_OpenFile_PC_GC_44_A6>(f.get());
|
||||
} else if ((signature & 0xFFFFFF00) == 0x3C004400 || (signature & 0xFFFFFF00) == 0x3C00A600) {
|
||||
return decode_qst_t<PSOCommandHeaderPC, S_OpenFile_PC_V3_44_A6>(f.get());
|
||||
} else if ((signature & 0xFF00FFFF) == 0x44003C00 || (signature & 0xFF00FFFF) == 0xA6003C00) {
|
||||
return decode_qst_t<PSOCommandHeaderDCV3, S_OpenFile_PC_V3_44_A6>(f.get());
|
||||
} else {
|
||||
throw runtime_error("invalid qst file format");
|
||||
}
|
||||
@@ -598,7 +982,7 @@ pair<string, string> Quest::decode_qst(const string& filename) {
|
||||
|
||||
|
||||
|
||||
QuestIndex::QuestIndex(const std::string& directory) : directory(directory) {
|
||||
QuestIndex::QuestIndex(const string& directory) : directory(directory) {
|
||||
auto filename_set = list_directory(this->directory);
|
||||
vector<string> filenames(filename_set.begin(), filename_set.end());
|
||||
sort(filenames.begin(), filenames.end());
|
||||
@@ -613,8 +997,14 @@ QuestIndex::QuestIndex(const std::string& directory) : directory(directory) {
|
||||
}
|
||||
|
||||
if (ends_with(filename, ".bin") ||
|
||||
ends_with(filename, ".bind") ||
|
||||
ends_with(filename, ".bin.gci") ||
|
||||
ends_with(filename, ".bin.vms") ||
|
||||
ends_with(filename, ".bin.dlq") ||
|
||||
ends_with(filename, ".mnm") ||
|
||||
ends_with(filename, ".mnmd") ||
|
||||
ends_with(filename, ".mnm.gci") ||
|
||||
ends_with(filename, ".mnm.dlq") ||
|
||||
ends_with(filename, ".qst")) {
|
||||
try {
|
||||
shared_ptr<Quest> q(new Quest(full_path));
|
||||
@@ -624,12 +1014,18 @@ QuestIndex::QuestIndex(const std::string& directory) : directory(directory) {
|
||||
make_pair(q->version, q->menu_item_id), q).second) {
|
||||
throw logic_error("duplicate quest menu item id");
|
||||
}
|
||||
log(INFO, "Indexed quest %s (%s-%" PRId64 " => %" PRIu32 ", %s, %s, joinable=%s, dcv1=%s)",
|
||||
ascii_name.c_str(), name_for_version(q->version), q->internal_id,
|
||||
q->menu_item_id, name_for_category(q->category), name_for_episode(q->episode),
|
||||
q->joinable ? "true" : "false", q->is_dcv1 ? "true" : "false");
|
||||
static_game_data_log.info("Indexed quest %s (%s => %s-%" PRId64 " (%" PRIu32 "), %s, %s, joinable=%s, dcv1=%s)",
|
||||
ascii_name.c_str(),
|
||||
filename.c_str(),
|
||||
name_for_version(q->version),
|
||||
q->internal_id,
|
||||
q->menu_item_id,
|
||||
name_for_category(q->category),
|
||||
name_for_episode(q->episode),
|
||||
q->joinable ? "true" : "false",
|
||||
q->is_dcv1 ? "true" : "false");
|
||||
} catch (const exception& e) {
|
||||
log(WARNING, "Failed to parse quest file %s (%s)", filename.c_str(), e.what());
|
||||
static_game_data_log.warning("Failed to parse quest file %s (%s)", filename.c_str(), e.what());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -644,8 +1040,8 @@ shared_ptr<const string> QuestIndex::get_gba(const string& name) const {
|
||||
return this->gba_file_contents.at(name);
|
||||
}
|
||||
|
||||
vector<shared_ptr<const Quest>> QuestIndex::filter(GameVersion version,
|
||||
bool is_dcv1, QuestCategory category) const {
|
||||
vector<shared_ptr<const Quest>> QuestIndex::filter(
|
||||
GameVersion version, bool is_dcv1, QuestCategory category) const {
|
||||
auto it = this->version_menu_item_id_to_quest.lower_bound(make_pair(version, 0));
|
||||
auto end_it = this->version_menu_item_id_to_quest.upper_bound(make_pair(version, 0xFFFFFFFF));
|
||||
|
||||
@@ -666,7 +1062,7 @@ vector<shared_ptr<const Quest>> QuestIndex::filter(GameVersion version,
|
||||
static string create_download_quest_file(const string& compressed_data,
|
||||
size_t decompressed_size, uint32_t encryption_seed = 0) {
|
||||
// Download quest files are like normal (PRS-compressed) quest files, but they
|
||||
// are encrypted with the PSOPC encryption (even on V3 / PSO GC), and a small
|
||||
// are encrypted with PSO V2 encryption (even on V3 / PSO GC), and a small
|
||||
// header (PSODownloadQuestHeader) is prepended to the encrypted data.
|
||||
|
||||
if (encryption_seed == 0) {
|
||||
@@ -680,11 +1076,11 @@ static string create_download_quest_file(const string& compressed_data,
|
||||
data += compressed_data;
|
||||
|
||||
// Add temporary extra bytes if necessary so encryption won't fail - the data
|
||||
// size must be a multiple of 4 for PSO PC encryption.
|
||||
// size must be a multiple of 4 for PSO V2 encryption.
|
||||
size_t original_size = data.size();
|
||||
data.resize((data.size() + 3) & (~3));
|
||||
|
||||
PSOPCEncryption encr(encryption_seed);
|
||||
PSOV2Encryption encr(encryption_seed);
|
||||
encr.encrypt(data.data() + sizeof(PSODownloadQuestHeader),
|
||||
data.size() - sizeof(PSODownloadQuestHeader));
|
||||
data.resize(original_size);
|
||||
@@ -698,6 +1094,12 @@ shared_ptr<Quest> Quest::create_download_quest() const {
|
||||
// this flag, we need to decompress the quest's .bin file, set the flag, then
|
||||
// recompress it again.
|
||||
|
||||
// This function should not be used for Episode 3 quests (they should be sent
|
||||
// to the client as-is, without any encryption or other preprocessing)
|
||||
if (this->category == QuestCategory::EPISODE_3) {
|
||||
throw logic_error("Episode 3 quests cannot be converted to download quests");
|
||||
}
|
||||
|
||||
string decompressed_bin = prs_decompress(*this->bin_contents());
|
||||
|
||||
void* data_ptr = decompressed_bin.data();
|
||||
@@ -714,6 +1116,7 @@ shared_ptr<Quest> Quest::create_download_quest() const {
|
||||
}
|
||||
reinterpret_cast<PSOQuestHeaderPC*>(data_ptr)->is_download = 0x01;
|
||||
break;
|
||||
case GameVersion::XB:
|
||||
case GameVersion::GC:
|
||||
if (decompressed_bin.size() < sizeof(PSOQuestHeaderGC)) {
|
||||
throw runtime_error("bin file is too small for header");
|
||||
@@ -728,8 +1131,8 @@ shared_ptr<Quest> Quest::create_download_quest() const {
|
||||
|
||||
string compressed_bin = prs_compress(decompressed_bin);
|
||||
|
||||
// We'll create a new Quest object with appropriately-processed .bin and .dat
|
||||
// file contents.
|
||||
// Return a new Quest object with appropriately-processed .bin and .dat file
|
||||
// contents
|
||||
shared_ptr<Quest> dlq(new Quest(*this));
|
||||
dlq->bin_contents_ptr.reset(new string(create_download_quest_file(
|
||||
compressed_bin, decompressed_bin.size())));
|
||||
|
||||
+11
-1
@@ -38,7 +38,9 @@ class Quest {
|
||||
public:
|
||||
enum class FileFormat {
|
||||
BIN_DAT = 0,
|
||||
BIN_DAT_UNCOMPRESSED,
|
||||
BIN_DAT_GCI,
|
||||
BIN_DAT_VMS,
|
||||
BIN_DAT_DLQ,
|
||||
QST,
|
||||
};
|
||||
@@ -51,6 +53,7 @@ public:
|
||||
GameVersion version;
|
||||
std::string file_basename; // we append -<version>.<bin/dat> when reading
|
||||
FileFormat file_format;
|
||||
bool has_mnm_extension;
|
||||
std::u16string name;
|
||||
std::u16string short_description;
|
||||
std::u16string long_description;
|
||||
@@ -69,7 +72,14 @@ public:
|
||||
|
||||
std::shared_ptr<Quest> create_download_quest() const;
|
||||
|
||||
static std::string decode_gci(const std::string& filename);
|
||||
static std::string decode_gci(
|
||||
const std::string& filename,
|
||||
ssize_t find_seed_num_threads = -1,
|
||||
int64_t known_seed = -1);
|
||||
static std::string decode_vms(
|
||||
const std::string& filename,
|
||||
ssize_t find_seed_num_threads = -1,
|
||||
int64_t known_seed = -1);
|
||||
static std::string decode_dlq(const std::string& filename);
|
||||
static std::pair<std::string, std::string> decode_qst(const std::string& filename);
|
||||
|
||||
|
||||
+22
-7
@@ -7,18 +7,33 @@ using namespace std;
|
||||
|
||||
|
||||
|
||||
RareItemSet::RareItemSet(const char* filename, uint8_t episode,
|
||||
uint8_t difficulty, uint8_t secid) {
|
||||
scoped_fd fd(filename, O_RDONLY);
|
||||
size_t offset = (episode * 0x6400) + (difficulty * 0x1900) + (secid * 0x0280);
|
||||
preadx(fd, this, sizeof(*this), offset);
|
||||
RareItemSet::RareItemSet(shared_ptr<const string> data) : data(data) {
|
||||
// TODO: Actually parse the GSL here instead of treating it as a blob
|
||||
if (this->data->size() != sizeof(Table) * 10 * 4 * 3) {
|
||||
throw runtime_error("data file size is incorrect");
|
||||
}
|
||||
this->tables = reinterpret_cast<const Table*>(this->data->data());
|
||||
}
|
||||
|
||||
bool sample_rare_item(uint8_t pc) {
|
||||
const RareItemSet::Table& RareItemSet::get_table(
|
||||
uint8_t episode, uint8_t difficulty, uint8_t secid) const {
|
||||
if (episode > 2) {
|
||||
throw logic_error("incorrect episode number");
|
||||
}
|
||||
if (difficulty > 3) {
|
||||
throw logic_error("incorrect difficulty");
|
||||
}
|
||||
if (secid > 10) {
|
||||
throw logic_error("incorrect section id");
|
||||
}
|
||||
return this->tables[(episode * 10 * 4) + (difficulty * 10) + secid];
|
||||
}
|
||||
|
||||
bool RareItemSet::sample(mt19937& random, uint8_t pc) {
|
||||
int8_t shift = ((pc >> 3) & 0x1F) - 4;
|
||||
if (shift < 0) {
|
||||
shift = 0;
|
||||
}
|
||||
uint32_t rate = ((2 << shift) * ((pc & 7) + 7));
|
||||
return (random_object<uint32_t>() < rate);
|
||||
return (random() < rate);
|
||||
}
|
||||
|
||||
+29
-18
@@ -2,26 +2,37 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <random>
|
||||
|
||||
|
||||
struct RareItemDrop {
|
||||
uint8_t probability;
|
||||
uint8_t item_code[3];
|
||||
} __attribute__((packed));
|
||||
|
||||
struct RareItemSet {
|
||||
// TODO: It looks like this structure can actually vary. We see the offsets
|
||||
// 0194 and 01B2 in the unused section, along with the value 1E (number of box
|
||||
// rares). In PSOGC, these all appear to be the same size/format, but that's
|
||||
// probably not strictly required to be the case.
|
||||
// 0x280 in size; describes one difficulty, section ID, and episode
|
||||
RareItemDrop rares[0x65]; // 0000 - 0194 in file
|
||||
uint8_t box_areas[0x1E]; // 0194 - 01B2 in file
|
||||
RareItemDrop box_rares[0x1E]; // 01B2 - 022A in file
|
||||
uint8_t unused[0x56];
|
||||
class RareItemSet {
|
||||
public:
|
||||
struct Table {
|
||||
// TODO: It looks like this structure can actually vary. We see the offsets
|
||||
// 0194 and 01B2 in the unused section, along with the value 1E (number of
|
||||
// box rares). In PSOGC, these all appear to be the same size/format, but
|
||||
// that's probably not strictly required to be the case.
|
||||
// 0x280 in size; describes one difficulty, section ID, and episode
|
||||
struct Drop {
|
||||
uint8_t probability;
|
||||
uint8_t item_code[3];
|
||||
} __attribute__((packed));
|
||||
Drop monster_rares[0x65]; // 0000 - 0194 in file
|
||||
uint8_t box_areas[0x1E]; // 0194 - 01B2 in file
|
||||
Drop box_rares[0x1E]; // 01B2 - 022A in file
|
||||
uint8_t unused[0x56];
|
||||
} __attribute__((packed));
|
||||
|
||||
RareItemSet(const char* filename, uint8_t episode, uint8_t difficulty,
|
||||
uint8_t secid);
|
||||
} __attribute__((packed));
|
||||
RareItemSet(std::shared_ptr<const std::string> data);
|
||||
|
||||
bool sample_rare_item(uint8_t pc);
|
||||
const Table& get_table(uint8_t episode, uint8_t difficulty, uint8_t secid) const;
|
||||
|
||||
static bool sample(std::mt19937& rand, uint8_t probability);
|
||||
|
||||
private:
|
||||
std::shared_ptr<const std::string> data;
|
||||
const Table* tables;
|
||||
};
|
||||
|
||||
+2835
-1193
File diff suppressed because it is too large
Load Diff
+16
-3
@@ -6,8 +6,21 @@
|
||||
|
||||
|
||||
|
||||
void process_connect(std::shared_ptr<ServerState> s, std::shared_ptr<Client> c);
|
||||
void process_disconnect(std::shared_ptr<ServerState> s,
|
||||
std::shared_ptr<Lobby> create_game_generic(
|
||||
std::shared_ptr<ServerState> s,
|
||||
std::shared_ptr<Client> c,
|
||||
const std::u16string& name,
|
||||
const std::u16string& password,
|
||||
uint8_t episode,
|
||||
uint8_t difficulty,
|
||||
uint32_t flags,
|
||||
std::shared_ptr<Lobby> watched_lobby = nullptr,
|
||||
std::shared_ptr<Episode3::BattleRecordPlayer> battle_player = nullptr);
|
||||
|
||||
void on_connect(std::shared_ptr<ServerState> s, std::shared_ptr<Client> c);
|
||||
void on_disconnect(std::shared_ptr<ServerState> s,
|
||||
std::shared_ptr<Client> c);
|
||||
void process_command(std::shared_ptr<ServerState> s, std::shared_ptr<Client> c,
|
||||
void on_command(std::shared_ptr<ServerState> s, std::shared_ptr<Client> c,
|
||||
uint16_t command, uint32_t flag, const std::string& data);
|
||||
void on_command_with_header(std::shared_ptr<ServerState> s,
|
||||
std::shared_ptr<Client> c, std::string& data);
|
||||
|
||||
+890
-731
File diff suppressed because it is too large
Load Diff
@@ -8,7 +8,7 @@
|
||||
|
||||
|
||||
|
||||
void process_subcommand(std::shared_ptr<ServerState> s,
|
||||
void on_subcommand(std::shared_ptr<ServerState> s,
|
||||
std::shared_ptr<Lobby> l, std::shared_ptr<Client> c, uint8_t command,
|
||||
uint8_t flag, const std::string& data);
|
||||
|
||||
|
||||
@@ -0,0 +1,729 @@
|
||||
#include "ReplaySession.hh"
|
||||
|
||||
#include <phosg/Filesystem.hh>
|
||||
#include <phosg/Strings.hh>
|
||||
#include <phosg/Time.hh>
|
||||
|
||||
#include "Loggers.hh"
|
||||
#include "Shell.hh"
|
||||
#include "Server.hh"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
|
||||
ReplaySession::Event::Event(Type type, uint64_t client_id, size_t line_num)
|
||||
: type(type),
|
||||
client_id(client_id),
|
||||
allow_size_disparity(false),
|
||||
complete(false),
|
||||
line_num(line_num) { }
|
||||
|
||||
string ReplaySession::Event::str() const {
|
||||
string ret;
|
||||
if (this->type == Type::CONNECT) {
|
||||
ret = string_printf("Event[%" PRIu64 ", CONNECT", this->client_id);
|
||||
} else if (this->type == Type::DISCONNECT) {
|
||||
ret = string_printf("Event[%" PRIu64 ", DISCONNECT", this->client_id);
|
||||
} else if (this->type == Type::SEND) {
|
||||
ret = string_printf("Event[%" PRIu64 ", SEND %04zX", this->client_id, this->data.size());
|
||||
} else if (this->type == Type::RECEIVE) {
|
||||
ret = string_printf("Event[%" PRIu64 ", RECEIVE %04zX", this->client_id, this->data.size());
|
||||
}
|
||||
if (this->allow_size_disparity) {
|
||||
ret += ", size disparity allowed";
|
||||
}
|
||||
if (this->complete) {
|
||||
ret += ", done";
|
||||
}
|
||||
ret += string_printf(", ev-line %zu]", this->line_num);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
ReplaySession::Client::Client(
|
||||
ReplaySession* session, uint64_t id, uint16_t port, GameVersion version)
|
||||
: id(id),
|
||||
port(port),
|
||||
version(version),
|
||||
channel(
|
||||
this->version,
|
||||
&ReplaySession::dispatch_on_command_received,
|
||||
&ReplaySession::dispatch_on_error,
|
||||
session,
|
||||
string_printf("R-%" PRIX64, this->id)) { }
|
||||
|
||||
string ReplaySession::Client::str() const {
|
||||
return string_printf("Client[%" PRIu64 ", T-%hu, %s]",
|
||||
this->id, this->port, name_for_version(this->version));
|
||||
}
|
||||
|
||||
|
||||
|
||||
shared_ptr<ReplaySession::Event> ReplaySession::create_event(
|
||||
Event::Type type, shared_ptr<Client> c, size_t line_num) {
|
||||
shared_ptr<Event> event(new Event(type, c->id, line_num));
|
||||
if (!this->last_event.get()) {
|
||||
this->first_event = event;
|
||||
} else {
|
||||
this->last_event->next_event = event;
|
||||
}
|
||||
this->last_event = event;
|
||||
if (type == Event::Type::RECEIVE) {
|
||||
c->receive_events.emplace_back(event);
|
||||
}
|
||||
return event;
|
||||
}
|
||||
|
||||
void ReplaySession::check_for_password(shared_ptr<const Event> ev) const {
|
||||
auto version = this->clients.at(ev->client_id)->version;
|
||||
|
||||
auto check_pw = [&](const string& pw) {
|
||||
if (!this->required_password.empty() && !pw.empty() && (pw != this->required_password)) {
|
||||
print_data(stderr, ev->data, 0, nullptr, PrintDataFlags::PRINT_ASCII | PrintDataFlags::OFFSET_16_BITS);
|
||||
throw runtime_error(string_printf("(ev-line %zu) sent password is incorrect", ev->line_num));
|
||||
}
|
||||
};
|
||||
auto check_ak = [&](const string& ak) {
|
||||
if (this->required_access_key.empty() || ak.empty()) {
|
||||
return;
|
||||
}
|
||||
string ref_access_key;
|
||||
if (version == GameVersion::DC || version == GameVersion::PC || version == GameVersion::PATCH) {
|
||||
ref_access_key = this->required_access_key.substr(0, 8);
|
||||
} else {
|
||||
ref_access_key = this->required_access_key;
|
||||
}
|
||||
if (ak != ref_access_key) {
|
||||
print_data(stderr, ev->data, 0, nullptr, PrintDataFlags::PRINT_ASCII | PrintDataFlags::OFFSET_16_BITS);
|
||||
throw runtime_error(string_printf("(ev-line %zu) sent access key is incorrect", ev->line_num));
|
||||
}
|
||||
};
|
||||
auto check_either = [&](const string& s) {
|
||||
try {
|
||||
check_ak(s);
|
||||
} catch (const exception&) {
|
||||
check_pw(s);
|
||||
}
|
||||
};
|
||||
|
||||
const void* cmd_data = ev->data.data() + ((version == GameVersion::BB) ? 8 : 4);
|
||||
size_t cmd_size = ev->data.size() - ((version == GameVersion::BB) ? 8 : 4);
|
||||
|
||||
switch (version) {
|
||||
case GameVersion::PATCH: {
|
||||
const auto& header = check_size_t<PSOCommandHeaderPC>(
|
||||
ev->data, sizeof(PSOCommandHeaderPC), 0xFFFF);
|
||||
if (header.command == 0x04) {
|
||||
check_either(check_size_t<C_Login_Patch_04>(cmd_data, cmd_size).password);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case GameVersion::PC: {
|
||||
const auto& header = check_size_t<PSOCommandHeaderPC>(
|
||||
ev->data, sizeof(PSOCommandHeaderPC), 0xFFFF);
|
||||
if (header.command == 0x03) {
|
||||
check_ak(check_size_t<C_LegacyLogin_PC_V3_03>(cmd_data, cmd_size).access_key2);
|
||||
} else if (header.command == 0x04) {
|
||||
check_ak(check_size_t<C_LegacyLogin_PC_V3_04>(cmd_data, cmd_size).access_key);
|
||||
} else if (header.command == 0x9A) {
|
||||
const auto& cmd = check_size_t<C_Login_DC_PC_V3_9A>(cmd_data, cmd_size);
|
||||
check_ak(cmd.v1_access_key);
|
||||
check_ak(cmd.access_key);
|
||||
check_ak(cmd.access_key2);
|
||||
} else if (header.command == 0x9C) {
|
||||
const auto& cmd = check_size_t<C_Register_DC_PC_V3_9C>(cmd_data, cmd_size);
|
||||
check_ak(cmd.access_key);
|
||||
check_pw(cmd.password);
|
||||
} else if (header.command == 0x9D) {
|
||||
const auto& cmd = check_size_t<C_Login_DC_PC_GC_9D>(cmd_data, cmd_size,
|
||||
sizeof(C_Login_DC_PC_GC_9D), sizeof(C_LoginExtended_PC_9D));
|
||||
check_ak(cmd.v1_access_key);
|
||||
check_ak(cmd.access_key);
|
||||
check_ak(cmd.access_key2);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case GameVersion::DC:
|
||||
case GameVersion::GC:
|
||||
case GameVersion::XB: {
|
||||
const auto& header = check_size_t<PSOCommandHeaderDCV3>(
|
||||
ev->data, sizeof(PSOCommandHeaderDCV3), 0xFFFF);
|
||||
if (header.command == 0x03) {
|
||||
check_ak(check_size_t<C_LegacyLogin_PC_V3_03>(cmd_data, cmd_size).access_key2);
|
||||
} else if (header.command == 0x04) {
|
||||
check_ak(check_size_t<C_LegacyLogin_PC_V3_04>(cmd_data, cmd_size).access_key);
|
||||
} else if (header.command == 0x90) {
|
||||
check_ak(check_size_t<C_LoginV1_DC_PC_V3_90>(cmd_data, cmd_size).access_key);
|
||||
} else if (header.command == 0x93) {
|
||||
const auto& cmd = check_size_t<C_LoginV1_DC_93>(cmd_data, cmd_size,
|
||||
sizeof(C_LoginV1_DC_93), sizeof(C_LoginExtendedV1_DC_93));
|
||||
check_ak(cmd.access_key);
|
||||
} else if (header.command == 0x9A) {
|
||||
const auto& cmd = check_size_t<C_Login_DC_PC_V3_9A>(cmd_data, cmd_size);
|
||||
check_ak(cmd.v1_access_key);
|
||||
check_ak(cmd.access_key);
|
||||
check_ak(cmd.access_key2);
|
||||
} else if (header.command == 0x9C) {
|
||||
const auto& cmd = check_size_t<C_Register_DC_PC_V3_9C>(cmd_data, cmd_size);
|
||||
check_ak(cmd.access_key);
|
||||
check_pw(cmd.password);
|
||||
} else if (header.command == 0x9D) {
|
||||
const auto& cmd = check_size_t<C_Login_DC_PC_GC_9D>(cmd_data, cmd_size,
|
||||
sizeof(C_Login_DC_PC_GC_9D), sizeof(C_LoginExtended_DC_GC_9D));
|
||||
check_ak(cmd.v1_access_key);
|
||||
check_ak(cmd.access_key);
|
||||
check_ak(cmd.access_key2);
|
||||
} else if (header.command == 0x9E) {
|
||||
if (version == GameVersion::GC) {
|
||||
const auto& cmd = check_size_t<C_Login_GC_9E>(cmd_data, cmd_size,
|
||||
sizeof(C_Login_GC_9E), sizeof(C_LoginExtended_GC_9E));
|
||||
check_ak(cmd.access_key);
|
||||
check_ak(cmd.access_key2);
|
||||
} else { // XB
|
||||
const auto& cmd = check_size_t<C_Login_XB_9E>(cmd_data, cmd_size,
|
||||
sizeof(C_Login_XB_9E), sizeof(C_LoginExtended_XB_9E));
|
||||
check_ak(cmd.access_key);
|
||||
check_ak(cmd.access_key2);
|
||||
}
|
||||
} else if (header.command == 0xDB) {
|
||||
const auto& cmd = check_size_t<C_VerifyLicense_V3_DB>(cmd_data, cmd_size);
|
||||
check_ak(cmd.access_key);
|
||||
check_ak(cmd.access_key2);
|
||||
check_pw(cmd.password);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case GameVersion::BB: {
|
||||
const auto& header = check_size_t<PSOCommandHeaderBB>(
|
||||
ev->data, sizeof(PSOCommandHeaderBB), 0xFFFF);
|
||||
if (header.command == 0x04) {
|
||||
check_pw(check_size_t<C_LegacyLogin_BB_04>(cmd_data, cmd_size).password);
|
||||
} else if (header.command == 0x93) {
|
||||
check_pw(check_size_t<C_Login_BB_93>(cmd_data, cmd_size).password);
|
||||
} else if (header.command == 0x9C) {
|
||||
check_pw(check_size_t<C_Register_BB_9C>(cmd_data, cmd_size).password);
|
||||
} else if (header.command == 0x9E) {
|
||||
check_pw(check_size_t<C_LoginExtended_BB_9E>(cmd_data, cmd_size).password);
|
||||
} else if (header.command == 0xDB) {
|
||||
check_pw(check_size_t<C_VerifyLicense_BB_DB>(cmd_data, cmd_size).password);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
throw logic_error("invalid game version");
|
||||
}
|
||||
}
|
||||
|
||||
void ReplaySession::apply_default_mask(shared_ptr<Event> ev) {
|
||||
auto version = this->clients.at(ev->client_id)->version;
|
||||
|
||||
void* cmd_data = ev->data.data() + ((version == GameVersion::BB) ? 8 : 4);
|
||||
size_t cmd_size = ev->data.size() - ((version == GameVersion::BB) ? 8 : 4);
|
||||
void* mask_data = ev->mask.data() + ((version == GameVersion::BB) ? 8 : 4);
|
||||
size_t mask_size = ev->mask.size() - ((version == GameVersion::BB) ? 8 : 4);
|
||||
|
||||
switch (version) {
|
||||
case GameVersion::PATCH: {
|
||||
const auto& header = check_size_t<PSOCommandHeaderPC>(
|
||||
ev->data, sizeof(PSOCommandHeaderPC), 0xFFFF);
|
||||
if (header.command == 0x02) {
|
||||
auto& cmd_mask = check_size_t<S_ServerInit_Patch_02>(mask_data, mask_size);
|
||||
cmd_mask.server_key = 0;
|
||||
cmd_mask.client_key = 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case GameVersion::DC:
|
||||
case GameVersion::PC:
|
||||
case GameVersion::GC:
|
||||
case GameVersion::XB: {
|
||||
uint8_t command;
|
||||
if (version == GameVersion::PC) {
|
||||
command = check_size_t<PSOCommandHeaderPC>(
|
||||
ev->data, sizeof(PSOCommandHeaderPC), 0xFFFF).command;
|
||||
} else { // V3
|
||||
command = check_size_t<PSOCommandHeaderDCV3>(
|
||||
ev->data, sizeof(PSOCommandHeaderDCV3), 0xFFFF).command;
|
||||
}
|
||||
switch (command) {
|
||||
case 0x02:
|
||||
case 0x17:
|
||||
case 0x91:
|
||||
case 0x9B: {
|
||||
auto& mask = check_size_t<S_ServerInitDefault_DC_PC_V3_02_17_91_9B>(
|
||||
mask_data, mask_size, sizeof(S_ServerInitDefault_DC_PC_V3_02_17_91_9B), 0xFFFF);
|
||||
mask.server_key = 0;
|
||||
mask.client_key = 0;
|
||||
break;
|
||||
}
|
||||
case 0x19: {
|
||||
if (mask_size == sizeof(S_ReconnectSplit_19)) {
|
||||
auto& mask = check_size_t<S_ReconnectSplit_19>(mask_data, mask_size);
|
||||
mask.pc_address = 0;
|
||||
mask.gc_address = 0;
|
||||
} else {
|
||||
auto& mask = check_size_t<S_Reconnect_19>(mask_data, mask_size);
|
||||
mask.address = 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 0x41: {
|
||||
if (version == GameVersion::PC) {
|
||||
auto& mask = check_size_t<S_GuildCardSearchResult_PC_41>(mask_data, mask_size);
|
||||
mask.reconnect_command.address = 0;
|
||||
} else if (version == GameVersion::BB) {
|
||||
auto& mask = check_size_t<S_GuildCardSearchResult_BB_41>(mask_data, mask_size);
|
||||
mask.reconnect_command.address = 0;
|
||||
} else { // V3
|
||||
auto& mask = check_size_t<S_GuildCardSearchResult_DC_V3_41>(mask_data, mask_size);
|
||||
mask.reconnect_command.address = 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 0x64: {
|
||||
if (version == GameVersion::PC) {
|
||||
auto& mask = check_size_t<S_JoinGame_PC_64>(mask_data, mask_size);
|
||||
mask.variations.clear(0);
|
||||
mask.rare_seed = 0;
|
||||
} else { // V3
|
||||
auto& mask = check_size_t<S_JoinGame_DC_GC_64>(mask_data, mask_size,
|
||||
sizeof(S_JoinGame_DC_GC_64), sizeof(S_JoinGame_GC_Ep3_64));
|
||||
mask.variations.clear(0);
|
||||
mask.rare_seed = 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 0xB1: {
|
||||
for (size_t x = 4; x < ev->mask.size(); x++) {
|
||||
ev->mask[x] = 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 0xC9: {
|
||||
if (mask_size == 0xCC) {
|
||||
auto& mask = check_size_t<G_ServerVersionStrings_GC_Ep3_6xB4x46>(
|
||||
mask_data, mask_size);
|
||||
mask.version_signature.clear(0);
|
||||
mask.date_str1.clear(0);
|
||||
mask.date_str2.clear(0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 0x6C: {
|
||||
if (version == GameVersion::GC && mask_size >= 0x14) {
|
||||
const auto& cmd = check_size_t<G_MapList_GC_Ep3_6xB6x40>(
|
||||
cmd_data, cmd_size, sizeof(G_MapList_GC_Ep3_6xB6x40), 0xFFFF);
|
||||
if ((cmd.header.header.basic_header.subcommand == 0xB6) &&
|
||||
(cmd.header.subsubcommand == 0x40)) {
|
||||
check_size_t<PSOCommandHeaderDCV3>(ev->mask, sizeof(PSOCommandHeaderDCV3), 0xFFFF).size = 0;
|
||||
auto& mask = check_size_t<G_MapList_GC_Ep3_6xB6x40>(
|
||||
mask_data, mask_size, sizeof(G_MapList_GC_Ep3_6xB6x40), 0xFFFF);
|
||||
mask.header.header.size = 0;
|
||||
mask.compressed_data_size = 0;
|
||||
ev->allow_size_disparity = true;
|
||||
for (size_t z = sizeof(PSOCommandHeaderDCV3) + sizeof(G_MapList_GC_Ep3_6xB6x40); z < ev->mask.size(); z++) {
|
||||
ev->mask[z] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case GameVersion::BB: {
|
||||
uint16_t command = check_size_t<PSOCommandHeaderBB>(
|
||||
ev->data, sizeof(PSOCommandHeaderBB), 0xFFFF).command;
|
||||
switch (command) {
|
||||
case 0x0003: {
|
||||
auto& mask = check_size_t<S_ServerInitDefault_BB_03_9B>(
|
||||
mask_data, mask_size, sizeof(S_ServerInitDefault_BB_03_9B), 0xFFFF);
|
||||
mask.server_key.clear(0);
|
||||
mask.client_key.clear(0);
|
||||
break;
|
||||
}
|
||||
case 0x0019: {
|
||||
auto& mask = check_size_t<S_Reconnect_19>(mask_data, mask_size);
|
||||
mask.address = 0;
|
||||
break;
|
||||
}
|
||||
case 0x0064: {
|
||||
auto& mask = check_size_t<S_JoinGame_BB_64>(mask_data, mask_size);
|
||||
mask.variations.clear(0);
|
||||
mask.rare_seed = 0;
|
||||
break;
|
||||
}
|
||||
case 0x00B1: {
|
||||
for (size_t x = 8; x < ev->mask.size(); x++) {
|
||||
ev->mask[x] = 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 0x00E6: {
|
||||
auto& mask = check_size_t<S_ClientInit_BB_00E6>(mask_data, mask_size);
|
||||
mask.team_id = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
throw logic_error("invalid game version");
|
||||
}
|
||||
}
|
||||
|
||||
ReplaySession::ReplaySession(
|
||||
shared_ptr<struct event_base> base,
|
||||
FILE* input_log,
|
||||
shared_ptr<ServerState> state,
|
||||
const string& required_access_key,
|
||||
const string& required_password)
|
||||
: state(state),
|
||||
required_access_key(required_access_key),
|
||||
required_password(required_password),
|
||||
base(base),
|
||||
commands_sent(0),
|
||||
bytes_sent(0),
|
||||
commands_received(0),
|
||||
bytes_received(0) {
|
||||
shared_ptr<Event> parsing_command = nullptr;
|
||||
|
||||
size_t line_num = 0;
|
||||
size_t num_events = 0;
|
||||
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);
|
||||
}
|
||||
if (line.empty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (parsing_command.get()) {
|
||||
string expected_start = string_printf("%04zX |", parsing_command->data.size());
|
||||
if (starts_with(line, expected_start)) {
|
||||
// Parse out the hex part of the hex/ASCII dump
|
||||
string mask_bytes;
|
||||
string data_bytes = parse_data_string(
|
||||
line.substr(expected_start.size(), 16 * 3 + 1), &mask_bytes);
|
||||
parsing_command->data += data_bytes;
|
||||
parsing_command->mask += mask_bytes;
|
||||
continue;
|
||||
} else {
|
||||
if (parsing_command->type == Event::Type::RECEIVE) {
|
||||
this->apply_default_mask(parsing_command);
|
||||
} else if (parsing_command->type == Event::Type::SEND) {
|
||||
this->check_for_password(parsing_command);
|
||||
}
|
||||
parsing_command = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
if (starts_with(line, "I ")) {
|
||||
// I <pid/ts> - [Server] Client connected: C-%X on fd %d via %d (T-%hu-%s-%s-%s)
|
||||
// I <pid/ts> - [Server] Client connected: C-%X on virtual connection %p via T-%hu-VI
|
||||
size_t offset = line.find(" - [Server] Client connected: C-");
|
||||
if (offset != string::npos) {
|
||||
auto tokens = split(line, ' ');
|
||||
if (tokens.size() != 15) {
|
||||
throw runtime_error(string_printf("(ev-line %zu) client connection message has incorrect token count", line_num));
|
||||
}
|
||||
if (!starts_with(tokens[8], "C-")) {
|
||||
throw runtime_error(string_printf("(ev-line %zu) client connection message missing client ID token", line_num));
|
||||
}
|
||||
auto listen_tokens = split(tokens[14], '-');
|
||||
if (listen_tokens.size() < 4) {
|
||||
throw runtime_error(string_printf("(ev-line %zu) client connection message listening socket token format is incorrect", line_num));
|
||||
}
|
||||
|
||||
shared_ptr<Client> c(new Client(
|
||||
this,
|
||||
stoull(tokens[8].substr(2), nullptr, 16),
|
||||
stoul(listen_tokens[1], nullptr, 10),
|
||||
version_for_name(listen_tokens[2].c_str())));
|
||||
if (!this->clients.emplace(c->id, c).second) {
|
||||
throw runtime_error(string_printf("(ev-line %zu) duplicate client ID in input log", line_num));
|
||||
}
|
||||
this->create_event(Event::Type::CONNECT, c, line_num);
|
||||
num_events++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// I <pid/ts> - [Server] Disconnecting C-%X on fd %d
|
||||
offset = line.find(" - [Server] Client disconnected: C-");
|
||||
if (offset != string::npos) {
|
||||
auto tokens = split(line, ' ');
|
||||
if (tokens.size() < 9) {
|
||||
throw runtime_error(string_printf("(ev-line %zu) client disconnection message has incorrect token count", line_num));
|
||||
}
|
||||
if (!starts_with(tokens[8], "C-")) {
|
||||
throw runtime_error(string_printf("(ev-line %zu) client disconnection message missing client ID token", line_num));
|
||||
}
|
||||
uint64_t client_id = stoul(tokens[8].substr(2), nullptr, 16);
|
||||
try {
|
||||
auto& c = this->clients.at(client_id);
|
||||
if (c->disconnect_event.get()) {
|
||||
throw runtime_error(string_printf("(ev-line %zu) client has multiple disconnect events", line_num));
|
||||
}
|
||||
c->disconnect_event = this->create_event(Event::Type::DISCONNECT, c, line_num);
|
||||
num_events++;
|
||||
} catch (const out_of_range&) {
|
||||
throw runtime_error(string_printf("(ev-line %zu) unknown disconnecting client ID in input log", line_num));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// I <pid/ts> - [Commands] Sending to C-%X (...)
|
||||
// I <pid/ts> - [Commands] Received from C-%X (...)
|
||||
offset = line.find(" - [Commands] Sending to C-");
|
||||
if (offset == string::npos) {
|
||||
offset = line.find(" - [Commands] Received from C-");
|
||||
}
|
||||
if (offset != string::npos) {
|
||||
auto tokens = split(line, ' ');
|
||||
if (tokens.size() < 10) {
|
||||
throw runtime_error(string_printf("(ev-line %zu) command header line too short", line_num));
|
||||
}
|
||||
bool from_client = (tokens[6] == "Received");
|
||||
uint64_t client_id = stoull(tokens[8].substr(2), nullptr, 16);
|
||||
try {
|
||||
parsing_command = this->create_event(
|
||||
from_client ? Event::Type::SEND : Event::Type::RECEIVE,
|
||||
this->clients.at(client_id),
|
||||
line_num);
|
||||
num_events++;
|
||||
} catch (const out_of_range&) {
|
||||
throw runtime_error(string_printf("(ev-line %zu) input log contains command for missing client", line_num));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
replay_log.info("%zu clients in log", this->clients.size());
|
||||
for (const auto& it : this->clients) {
|
||||
string client_str = it.second->str();
|
||||
replay_log.info(" %" PRIu64 " => %s", it.first, client_str.c_str());
|
||||
}
|
||||
|
||||
replay_log.info("%zu events in replay log", num_events);
|
||||
for (auto ev = this->first_event; ev != nullptr; ev = ev->next_event) {
|
||||
string ev_str = ev->str();
|
||||
replay_log.info(" %s", ev_str.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void ReplaySession::start() {
|
||||
this->update_timeout_event();
|
||||
this->execute_pending_events();
|
||||
}
|
||||
|
||||
void ReplaySession::update_timeout_event() {
|
||||
if (!this->timeout_ev.get()) {
|
||||
this->timeout_ev.reset(
|
||||
event_new(this->base.get(), -1, EV_TIMEOUT, this->dispatch_on_timeout, this),
|
||||
event_free);
|
||||
}
|
||||
struct timeval tv = usecs_to_timeval(3000000);
|
||||
event_add(this->timeout_ev.get(), &tv);
|
||||
}
|
||||
|
||||
void ReplaySession::dispatch_on_timeout(evutil_socket_t, short, void*) {
|
||||
throw runtime_error("timeout waiting for next event");
|
||||
}
|
||||
|
||||
void ReplaySession::execute_pending_events() {
|
||||
while (this->first_event) {
|
||||
if (!this->first_event->complete) {
|
||||
auto& c = this->clients.at(this->first_event->client_id);
|
||||
|
||||
auto ev_str = this->first_event->str();
|
||||
replay_log.info("Event: %s", ev_str.c_str());
|
||||
|
||||
switch (this->first_event->type) {
|
||||
case Event::Type::CONNECT: {
|
||||
if (c->channel.connected()) {
|
||||
throw runtime_error(string_printf("(ev-line %zu) connect event on already-connected client", this->first_event->line_num));
|
||||
}
|
||||
|
||||
struct bufferevent* bevs[2];
|
||||
bufferevent_pair_new(this->base.get(), 0, bevs);
|
||||
|
||||
c->channel.set_bufferevent(bevs[0]);
|
||||
this->channel_to_client.emplace(&c->channel, c);
|
||||
|
||||
shared_ptr<const PortConfiguration> port_config;
|
||||
try {
|
||||
port_config = this->state->number_to_port_config.at(c->port);
|
||||
} catch (const out_of_range&) {
|
||||
bufferevent_free(bevs[1]);
|
||||
throw runtime_error(string_printf("(ev-line %zu) client connected to port missing from configuration", this->first_event->line_num));
|
||||
}
|
||||
|
||||
if (port_config->behavior == ServerBehavior::PROXY_SERVER) {
|
||||
// TODO: We should support this at some point in the future
|
||||
throw runtime_error(string_printf("(ev-line %zu) client connected to proxy server", this->first_event->line_num));
|
||||
} else if (this->state->game_server.get()) {
|
||||
this->state->game_server->connect_client(bevs[1], 0x20202020,
|
||||
1025, c->port, port_config->version, port_config->behavior);
|
||||
} else {
|
||||
throw runtime_error(string_printf("(ev-line %zu) no server available for connection", this->first_event->line_num));
|
||||
bufferevent_free(bevs[1]);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case Event::Type::DISCONNECT:
|
||||
this->channel_to_client.erase(&c->channel);
|
||||
c->channel.disconnect();
|
||||
break;
|
||||
case Event::Type::SEND:
|
||||
if (!c->channel.connected()) {
|
||||
throw runtime_error(string_printf("(ev-line %zu) send event attempted on unconnected client", this->first_event->line_num));
|
||||
}
|
||||
c->channel.send(this->first_event->data);
|
||||
this->commands_sent++;
|
||||
this->bytes_sent += this->first_event->data.size();
|
||||
break;
|
||||
case Event::Type::RECEIVE:
|
||||
// Receive events cannot be executed here, since we have to wait for
|
||||
// an incoming command. The existing handlers will take care of it:
|
||||
// on_command_received will be called sometime (hopefully) soon.
|
||||
return;
|
||||
default:
|
||||
throw logic_error("unhandled event type");
|
||||
}
|
||||
this->first_event->complete = true;
|
||||
}
|
||||
|
||||
this->first_event = this->first_event->next_event;
|
||||
if (!this->first_event.get()) {
|
||||
this->last_event = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
// If we get here, then there are no more events to run: we're done.
|
||||
// TODO: We should flush any pending sends on the remaining client here, even
|
||||
// though there are no pending receives (to make sure the last sent commands
|
||||
// don't crash newserv)
|
||||
replay_log.info("Replay complete: %zu commands sent (%zu bytes), %zu commands received (%zu bytes)",
|
||||
this->commands_sent, this->bytes_sent, this->commands_received, this->bytes_received);
|
||||
event_base_loopexit(this->base.get(), nullptr);
|
||||
}
|
||||
|
||||
void ReplaySession::dispatch_on_command_received(
|
||||
Channel& ch, uint16_t command, uint32_t flag, string& data) {
|
||||
ReplaySession* session = reinterpret_cast<ReplaySession*>(ch.context_obj);
|
||||
session->on_command_received(
|
||||
session->channel_to_client.at(&ch), command, flag, data);
|
||||
}
|
||||
|
||||
void ReplaySession::dispatch_on_error(Channel& ch, short events) {
|
||||
ReplaySession* session = reinterpret_cast<ReplaySession*>(ch.context_obj);
|
||||
session->on_error(session->channel_to_client.at(&ch), events);
|
||||
}
|
||||
|
||||
void ReplaySession::on_command_received(
|
||||
shared_ptr<Client> c, uint16_t command, uint32_t flag, string& data) {
|
||||
|
||||
// TODO: Use the iovec form of print_data here instead of
|
||||
// prepend_command_header (which copies the string)
|
||||
string full_command = prepend_command_header(
|
||||
c->version, c->channel.crypt_in.get(), command, flag, data);
|
||||
this->commands_received++;
|
||||
this->bytes_received += full_command.size();
|
||||
|
||||
if (c->receive_events.empty()) {
|
||||
print_data(stderr, full_command, 0, nullptr, PrintDataFlags::PRINT_ASCII | PrintDataFlags::OFFSET_16_BITS);
|
||||
throw runtime_error("received unexpected command for client");
|
||||
}
|
||||
|
||||
auto& ev = c->receive_events.front();
|
||||
if ((full_command.size() != ev->data.size()) && !ev->allow_size_disparity) {
|
||||
replay_log.error("Expected command:");
|
||||
print_data(stderr, ev->data, 0, nullptr, PrintDataFlags::PRINT_ASCII | PrintDataFlags::OFFSET_16_BITS);
|
||||
replay_log.error("Received command:");
|
||||
print_data(stderr, full_command, 0, nullptr, PrintDataFlags::PRINT_ASCII | PrintDataFlags::OFFSET_16_BITS);
|
||||
throw runtime_error(string_printf("(ev-line %zu) received command sizes do not match", ev->line_num));
|
||||
}
|
||||
for (size_t x = 0; x < min<size_t>(full_command.size(), ev->data.size()); x++) {
|
||||
if ((full_command[x] & ev->mask[x]) != (ev->data[x] & ev->mask[x])) {
|
||||
replay_log.error("Expected command:");
|
||||
print_data(stderr, ev->data, 0, nullptr, PrintDataFlags::PRINT_ASCII | PrintDataFlags::OFFSET_16_BITS);
|
||||
replay_log.error("Received command:");
|
||||
print_data(stderr, full_command, 0, ev->data.data(), PrintDataFlags::PRINT_ASCII | PrintDataFlags::OFFSET_16_BITS);
|
||||
throw runtime_error(string_printf("(ev-line %zu) received command data does not match expected data", ev->line_num));
|
||||
}
|
||||
}
|
||||
|
||||
ev->complete = true;
|
||||
c->receive_events.pop_front();
|
||||
|
||||
// If the command is an encryption init, set up encryption on the channel
|
||||
switch (c->version) {
|
||||
case GameVersion::PATCH:
|
||||
if (command == 0x02) {
|
||||
auto& cmd = check_size_t<S_ServerInit_Patch_02>(data);
|
||||
c->channel.crypt_in.reset(new PSOV2Encryption(cmd.server_key));
|
||||
c->channel.crypt_out.reset(new PSOV2Encryption(cmd.client_key));
|
||||
}
|
||||
break;
|
||||
case GameVersion::DC:
|
||||
case GameVersion::PC:
|
||||
case GameVersion::GC:
|
||||
case GameVersion::XB:
|
||||
if (command == 0x02 || command == 0x17 || command == 0x91 || command == 0x9B) {
|
||||
auto& cmd = check_size_t<S_ServerInitDefault_DC_PC_V3_02_17_91_9B>(data,
|
||||
sizeof(S_ServerInitDefault_DC_PC_V3_02_17_91_9B), 0xFFFF);
|
||||
if ((c->version == GameVersion::DC) || (c->version == GameVersion::PC)) {
|
||||
c->channel.crypt_in.reset(new PSOV2Encryption(cmd.server_key));
|
||||
c->channel.crypt_out.reset(new PSOV2Encryption(cmd.client_key));
|
||||
} else { // V3
|
||||
c->channel.crypt_in.reset(new PSOV3Encryption(cmd.server_key));
|
||||
c->channel.crypt_out.reset(new PSOV3Encryption(cmd.client_key));
|
||||
}
|
||||
}
|
||||
break;
|
||||
case GameVersion::BB:
|
||||
if (command == 0x03 || command == 0x9B) {
|
||||
auto& cmd = check_size_t<S_ServerInitDefault_BB_03_9B>(data,
|
||||
sizeof(S_ServerInitDefault_BB_03_9B), 0xFFFF);
|
||||
// TODO: At some point it may matter which BB private key file we use.
|
||||
// Don't just blindly use the first one here.
|
||||
c->channel.crypt_in.reset(new PSOBBEncryption(
|
||||
*this->state->bb_private_keys[0], cmd.server_key.data(), cmd.server_key.size()));
|
||||
c->channel.crypt_out.reset(new PSOBBEncryption(
|
||||
*this->state->bb_private_keys[0], cmd.client_key.data(), cmd.client_key.size()));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw logic_error("unsupported encryption version");
|
||||
}
|
||||
|
||||
this->update_timeout_event();
|
||||
this->execute_pending_events();
|
||||
}
|
||||
|
||||
void ReplaySession::on_error(shared_ptr<Client> c, short events) {
|
||||
if (events & BEV_EVENT_ERROR) {
|
||||
throw runtime_error(string_printf("C-%" PRIX64 " caused stream error", c->id));
|
||||
}
|
||||
if (events & BEV_EVENT_EOF) {
|
||||
if (!c->disconnect_event.get()) {
|
||||
throw runtime_error(string_printf(
|
||||
"C-%" PRIX64 " disconnected, but has no disconnect event", c->id));
|
||||
}
|
||||
if (!c->receive_events.empty()) {
|
||||
throw runtime_error(string_printf(
|
||||
"C-%" PRIX64 " disconnected, but has pending receive events", c->id));
|
||||
}
|
||||
c->disconnect_event->complete = true;
|
||||
this->channel_to_client.erase(&c->channel);
|
||||
c->channel.disconnect();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
#pragma once
|
||||
|
||||
#include <event2/event.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <deque>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "Channel.hh"
|
||||
#include "Version.hh"
|
||||
#include "ServerState.hh"
|
||||
|
||||
|
||||
|
||||
class ReplaySession {
|
||||
public:
|
||||
ReplaySession(
|
||||
std::shared_ptr<struct event_base> base,
|
||||
FILE* input_log,
|
||||
std::shared_ptr<ServerState> state,
|
||||
const std::string& required_access_key = "",
|
||||
const std::string& required_password = "");
|
||||
ReplaySession(const ReplaySession&) = delete;
|
||||
ReplaySession(ReplaySession&&) = delete;
|
||||
ReplaySession& operator=(const ReplaySession&) = delete;
|
||||
ReplaySession& operator=(ReplaySession&&) = delete;
|
||||
~ReplaySession() = default;
|
||||
|
||||
void start();
|
||||
|
||||
private:
|
||||
struct Event {
|
||||
enum class Type {
|
||||
CONNECT = 0,
|
||||
DISCONNECT,
|
||||
SEND,
|
||||
RECEIVE,
|
||||
};
|
||||
Type type;
|
||||
uint64_t client_id;
|
||||
std::string data; // Only used for SEND and RECEIVE
|
||||
std::string mask; // Only used for RECEIVE
|
||||
bool allow_size_disparity;
|
||||
bool complete;
|
||||
size_t line_num;
|
||||
|
||||
std::shared_ptr<Event> next_event;
|
||||
|
||||
Event(Type type, uint64_t client_id, size_t line_num);
|
||||
|
||||
std::string str() const;
|
||||
};
|
||||
|
||||
struct Client {
|
||||
uint64_t id;
|
||||
uint16_t port;
|
||||
GameVersion version;
|
||||
Channel channel;
|
||||
std::deque<std::shared_ptr<Event>> receive_events;
|
||||
std::shared_ptr<Event> disconnect_event;
|
||||
|
||||
Client(ReplaySession* session, uint64_t id, uint16_t port, GameVersion version);
|
||||
|
||||
std::string str() const;
|
||||
};
|
||||
|
||||
std::shared_ptr<ServerState> state;
|
||||
std::string required_access_key;
|
||||
std::string required_password;
|
||||
|
||||
std::unordered_map<uint64_t, std::shared_ptr<Client>> clients;
|
||||
std::unordered_map<Channel*, std::shared_ptr<Client>> channel_to_client;
|
||||
|
||||
std::shared_ptr<Event> first_event;
|
||||
std::shared_ptr<Event> last_event;
|
||||
|
||||
std::shared_ptr<struct event_base> base;
|
||||
std::shared_ptr<struct event> timeout_ev;
|
||||
|
||||
size_t commands_sent;
|
||||
size_t bytes_sent;
|
||||
size_t commands_received;
|
||||
size_t bytes_received;
|
||||
|
||||
std::shared_ptr<ReplaySession::Event> create_event(
|
||||
Event::Type type, std::shared_ptr<Client> c, size_t line_num);
|
||||
void update_timeout_event();
|
||||
|
||||
void apply_default_mask(std::shared_ptr<Event> ev);
|
||||
void check_for_password(std::shared_ptr<const Event> ev) const;
|
||||
|
||||
static void dispatch_on_timeout(evutil_socket_t fd, short events, void* ctx);
|
||||
static void dispatch_on_command_received(
|
||||
Channel& ch, uint16_t command, uint32_t flag, std::string& data);
|
||||
static void dispatch_on_error(Channel& ch, short events);
|
||||
void on_command_received(
|
||||
std::shared_ptr<Client> c, uint16_t command, uint32_t flag, std::string& data);
|
||||
void on_error(std::shared_ptr<Client> c, short events);
|
||||
|
||||
void execute_pending_events();
|
||||
};
|
||||
+2319
-1218
File diff suppressed because it is too large
Load Diff
+172
-29
@@ -6,6 +6,7 @@
|
||||
|
||||
#include <memory>
|
||||
#include <phosg/Strings.hh>
|
||||
#include <unordered_set>
|
||||
|
||||
#include "Client.hh"
|
||||
#include "Lobby.hh"
|
||||
@@ -18,6 +19,10 @@
|
||||
|
||||
|
||||
|
||||
extern const std::unordered_set<uint32_t> v2_crypt_initial_client_commands;
|
||||
extern const std::unordered_set<uint32_t> v3_crypt_initial_client_commands;
|
||||
extern const std::unordered_set<std::string> bb_crypt_initial_client_commands;
|
||||
|
||||
// TODO: Many of these functions should take a Channel& instead of a
|
||||
// shared_ptr<Client>. Refactor functions appropriately.
|
||||
|
||||
@@ -29,9 +34,6 @@
|
||||
// pointer is given but size is accidentally not given (e.g. if the type of
|
||||
// data in the calling function is changed from string to void*).
|
||||
|
||||
void send_command(Channel& ch, uint16_t command, uint32_t flag,
|
||||
const void* data, size_t size);
|
||||
|
||||
void send_command(std::shared_ptr<Client> c, uint16_t command,
|
||||
uint32_t flag, const void* data, size_t size);
|
||||
|
||||
@@ -49,6 +51,18 @@ inline void send_command_excluding_client(std::shared_ptr<Lobby> l,
|
||||
send_command_excluding_client(l, c, command, flag, nullptr, 0);
|
||||
}
|
||||
|
||||
void send_command_if_not_loading(std::shared_ptr<Lobby> l,
|
||||
uint16_t command, uint32_t flag, const void* data, size_t size);
|
||||
inline void send_command_if_not_loading(std::shared_ptr<Lobby> l,
|
||||
uint16_t command, uint32_t flag, const string& data) {
|
||||
send_command_if_not_loading(l, command, flag, data.data(), data.size());
|
||||
}
|
||||
template <typename StructT>
|
||||
inline void send_command_if_not_loading(std::shared_ptr<Lobby> l,
|
||||
uint16_t command, uint32_t flag, const StructT& data) {
|
||||
send_command_if_not_loading(l, command, flag, &data, sizeof(data));
|
||||
}
|
||||
|
||||
void send_command(std::shared_ptr<Lobby> l, uint16_t command, uint32_t flag,
|
||||
const void* data, size_t size);
|
||||
|
||||
@@ -82,6 +96,12 @@ void send_command_vt(std::shared_ptr<TargetT> c, uint16_t command,
|
||||
send_command(c, command, flag, data.data(), data.size() * sizeof(StructT));
|
||||
}
|
||||
|
||||
template <typename StructT>
|
||||
void send_command_vt(Channel& ch, uint16_t command, uint32_t flag,
|
||||
const std::vector<StructT>& data) {
|
||||
ch.send(command, flag, data.data(), data.size() * sizeof(StructT));
|
||||
}
|
||||
|
||||
template <typename TargetT, typename StructT, typename EntryT>
|
||||
void send_command_t_vt(std::shared_ptr<TargetT> c, uint16_t command,
|
||||
uint32_t flag, const StructT& data, const std::vector<EntryT>& array_data) {
|
||||
@@ -91,23 +111,39 @@ void send_command_t_vt(std::shared_ptr<TargetT> c, uint16_t command,
|
||||
send_command(c, command, flag, all_data.data(), all_data.size());
|
||||
}
|
||||
|
||||
void send_command_with_header(std::shared_ptr<Client> c, const void* data,
|
||||
size_t size);
|
||||
void send_command_with_header(Channel& c, const void* data, size_t size);
|
||||
|
||||
|
||||
|
||||
S_ServerInit_DC_PC_GC_02_17_92_9B prepare_server_init_contents_dc_pc_gc(
|
||||
bool initial_connection,
|
||||
uint32_t server_key,
|
||||
uint32_t client_key);
|
||||
S_ServerInit_BB_03 prepare_server_init_contents_bb(
|
||||
enum SendServerInitFlag {
|
||||
IS_INITIAL_CONNECTION = 0x01,
|
||||
USE_SECONDARY_MESSAGE = 0x02,
|
||||
};
|
||||
|
||||
S_ServerInitWithAfterMessage_DC_PC_V3_02_17_91_9B<0xB4>
|
||||
prepare_server_init_contents_console(
|
||||
uint32_t server_key, uint32_t client_key, uint8_t flags);
|
||||
S_ServerInitWithAfterMessage_BB_03_9B<0xB4>
|
||||
prepare_server_init_contents_bb(
|
||||
const parray<uint8_t, 0x30>& server_key,
|
||||
const parray<uint8_t, 0x30>& client_key,
|
||||
bool use_secondary_message);
|
||||
void send_server_init(std::shared_ptr<ServerState> s, std::shared_ptr<Client> c,
|
||||
bool initial_connection, bool use_secondary_message);
|
||||
uint8_t flags);
|
||||
void send_server_init(
|
||||
std::shared_ptr<ServerState> s,
|
||||
std::shared_ptr<Client> c,
|
||||
uint8_t flags);
|
||||
void send_update_client_config(std::shared_ptr<Client> c);
|
||||
|
||||
void send_quest_buffer_overflow(
|
||||
std::shared_ptr<ServerState> s, std::shared_ptr<Client> c);
|
||||
void send_function_call(
|
||||
Channel& ch,
|
||||
uint64_t client_flags,
|
||||
std::shared_ptr<CompiledFunctionCode> code,
|
||||
const std::unordered_map<std::string, uint32_t>& label_writes = {},
|
||||
const std::string& suffix = "",
|
||||
uint32_t checksum_addr = 0,
|
||||
uint32_t checksum_size = 0);
|
||||
void send_function_call(
|
||||
std::shared_ptr<Client> c,
|
||||
std::shared_ptr<CompiledFunctionCode> code,
|
||||
@@ -117,8 +153,11 @@ void send_function_call(
|
||||
uint32_t checksum_size = 0);
|
||||
|
||||
void send_reconnect(std::shared_ptr<Client> c, uint32_t address, uint16_t port);
|
||||
void send_pc_gc_split_reconnect(std::shared_ptr<Client> c, uint32_t address,
|
||||
uint16_t pc_port, uint16_t gc_port);
|
||||
void send_pc_console_split_reconnect(
|
||||
std::shared_ptr<Client> c,
|
||||
uint32_t address,
|
||||
uint16_t pc_port,
|
||||
uint16_t console_port);
|
||||
|
||||
void send_client_init_bb(std::shared_ptr<Client> c, uint32_t error);
|
||||
void send_team_and_key_config_bb(std::shared_ptr<Client> c);
|
||||
@@ -133,22 +172,49 @@ 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::u16string& 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::u16string& text);
|
||||
void send_quest_info(std::shared_ptr<Client> c, const std::u16string& text,
|
||||
bool is_download_quest);
|
||||
void send_lobby_message_box(std::shared_ptr<Client> c, const std::u16string& text);
|
||||
void send_ship_info(std::shared_ptr<Client> c, const std::u16string& text);
|
||||
void send_ship_info(Channel& ch, const std::u16string& text);
|
||||
void send_text_message(Channel& ch, const std::u16string& text);
|
||||
void send_text_message(std::shared_ptr<Client> c, const std::u16string& text);
|
||||
void send_text_message(std::shared_ptr<Lobby> l, const std::u16string& text);
|
||||
void send_text_message(std::shared_ptr<ServerState> l, const std::u16string& text);
|
||||
void send_chat_message(Channel& ch, const std::u16string& text);
|
||||
void send_chat_message(std::shared_ptr<Client> c, uint32_t from_serial_number,
|
||||
const std::u16string& from_name, const std::u16string& text);
|
||||
void send_simple_mail(std::shared_ptr<Client> c, uint32_t from_serial_number,
|
||||
const std::u16string& from_name, const std::u16string& text);
|
||||
|
||||
std::u16string prepare_chat_message(
|
||||
GameVersion version,
|
||||
const std::u16string& from_name,
|
||||
const std::u16string& text,
|
||||
char private_flags);
|
||||
void send_chat_message(
|
||||
Channel& ch,
|
||||
const std::u16string& text,
|
||||
char private_flags);
|
||||
void send_chat_message(
|
||||
std::shared_ptr<Client> c,
|
||||
uint32_t from_guild_card_number,
|
||||
const std::u16string& prepared_data);
|
||||
void send_chat_message(
|
||||
std::shared_ptr<Lobby> l,
|
||||
uint32_t from_guild_card_number,
|
||||
const std::u16string& prepared_data);
|
||||
void send_chat_message(
|
||||
std::shared_ptr<Client> c,
|
||||
uint32_t from_guild_card_number,
|
||||
const std::u16string& from_name,
|
||||
const u16string& text,
|
||||
char private_flags);
|
||||
void send_simple_mail(
|
||||
std::shared_ptr<Client> c,
|
||||
uint32_t from_serial_number,
|
||||
const std::u16string& from_name,
|
||||
const std::u16string& text);
|
||||
|
||||
template <typename TargetT>
|
||||
__attribute__((format(printf, 2, 3))) void send_text_message_printf(
|
||||
@@ -161,6 +227,9 @@ __attribute__((format(printf, 2, 3))) void send_text_message_printf(
|
||||
return send_text_message(t, decoded.c_str());
|
||||
}
|
||||
|
||||
__attribute__((format(printf, 2, 3))) void send_ep3_text_message_printf(
|
||||
std::shared_ptr<ServerState> s, const char* format, ...);
|
||||
|
||||
void send_info_board(std::shared_ptr<Client> c, std::shared_ptr<Lobby> l);
|
||||
|
||||
void send_card_search_result(
|
||||
@@ -169,10 +238,22 @@ void send_card_search_result(
|
||||
std::shared_ptr<Client> result,
|
||||
std::shared_ptr<Lobby> result_lobby);
|
||||
|
||||
void send_guild_card(
|
||||
Channel& ch,
|
||||
uint32_t guild_card_number,
|
||||
const u16string& name,
|
||||
const u16string& team_name,
|
||||
const u16string& description,
|
||||
uint8_t section_id,
|
||||
uint8_t char_class);
|
||||
void send_guild_card(std::shared_ptr<Client> c, std::shared_ptr<Client> source);
|
||||
void send_menu(std::shared_ptr<Client> c, const std::u16string& menu_name,
|
||||
uint32_t menu_id, const std::vector<MenuItem>& items, bool is_info_menu = false);
|
||||
void send_game_menu(std::shared_ptr<Client> c, std::shared_ptr<ServerState> s);
|
||||
void send_game_menu(
|
||||
std::shared_ptr<Client> c,
|
||||
std::shared_ptr<ServerState> s,
|
||||
bool is_spectator_team_list,
|
||||
bool is_tournament_game_list);
|
||||
void send_quest_menu(std::shared_ptr<Client> c, uint32_t menu_id,
|
||||
const std::vector<std::shared_ptr<const Quest>>& quests, bool is_download_menu);
|
||||
void send_quest_menu(std::shared_ptr<Client> c, uint32_t menu_id,
|
||||
@@ -187,6 +268,11 @@ void send_player_leave_notification(std::shared_ptr<Lobby> l,
|
||||
void send_self_leave_notification(std::shared_ptr<Client> c);
|
||||
void send_get_player_info(std::shared_ptr<Client> c);
|
||||
|
||||
void send_execute_item_trade(std::shared_ptr<Client> c,
|
||||
const std::vector<ItemData>& items);
|
||||
void send_execute_card_trade(std::shared_ptr<Client> c,
|
||||
const std::vector<std::pair<uint32_t, uint32_t>>& card_to_count);
|
||||
|
||||
void send_arrow_update(std::shared_ptr<Lobby> l);
|
||||
void send_resume_game(std::shared_ptr<Lobby> l,
|
||||
std::shared_ptr<Client> ready_client);
|
||||
@@ -200,17 +286,23 @@ enum PlayerStatsChange {
|
||||
};
|
||||
|
||||
void send_player_stats_change(std::shared_ptr<Lobby> l, std::shared_ptr<Client> c,
|
||||
PlayerStatsChange which, uint32_t amount);
|
||||
PlayerStatsChange stat, uint32_t amount);
|
||||
void send_player_stats_change(
|
||||
Channel& ch, uint16_t client_id, PlayerStatsChange stat, uint32_t amount);
|
||||
void send_warp(Channel& ch, uint8_t client_id, uint32_t area);
|
||||
void send_warp(std::shared_ptr<Client> c, uint32_t area);
|
||||
|
||||
void send_ep3_change_music(std::shared_ptr<Client> c, uint32_t song);
|
||||
void send_ep3_change_music(Channel& ch, uint32_t song);
|
||||
void send_set_player_visibility(std::shared_ptr<Lobby> l,
|
||||
std::shared_ptr<Client> c, bool visible);
|
||||
void send_revive_player(std::shared_ptr<Lobby> l, std::shared_ptr<Client> c);
|
||||
|
||||
void send_drop_item(Channel& ch, const ItemData& item,
|
||||
bool from_enemy, uint8_t area, float x, float z, uint16_t request_id);
|
||||
void send_drop_item(std::shared_ptr<Lobby> l, const ItemData& item,
|
||||
bool from_enemy, uint8_t area, float x, float z, uint16_t request_id);
|
||||
void send_drop_stacked_item(Channel& ch, const ItemData& item,
|
||||
uint8_t area, float x, float z);
|
||||
void send_drop_stacked_item(std::shared_ptr<Lobby> l, const ItemData& item,
|
||||
uint8_t area, float x, float z);
|
||||
void send_pick_up_item(std::shared_ptr<Lobby> l, std::shared_ptr<Client> c, uint32_t id,
|
||||
@@ -226,11 +318,48 @@ void send_give_experience(std::shared_ptr<Lobby> l, std::shared_ptr<Client> c,
|
||||
uint32_t amount);
|
||||
void send_ep3_card_list_update(
|
||||
std::shared_ptr<ServerState> s, std::shared_ptr<Client> c);
|
||||
void send_ep3_media_update(
|
||||
std::shared_ptr<Client> c,
|
||||
uint32_t type,
|
||||
uint32_t which,
|
||||
const std::string& compressed_data);
|
||||
void send_ep3_rank_update(std::shared_ptr<Client> c);
|
||||
void send_ep3_map_list(
|
||||
std::shared_ptr<ServerState> s, std::shared_ptr<Lobby> l);
|
||||
void send_ep3_map_data(
|
||||
std::shared_ptr<ServerState> s, std::shared_ptr<Lobby> l, uint32_t map_id);
|
||||
void send_ep3_card_battle_table_state(std::shared_ptr<Lobby> l, uint16_t table_number);
|
||||
void send_ep3_set_context_token(std::shared_ptr<Client> c, uint32_t context_token);
|
||||
|
||||
void send_ep3_confirm_tournament_entry(
|
||||
std::shared_ptr<ServerState> s,
|
||||
std::shared_ptr<Client> c,
|
||||
std::shared_ptr<const Episode3::Tournament> t);
|
||||
void send_ep3_tournament_list(
|
||||
std::shared_ptr<ServerState> s,
|
||||
std::shared_ptr<Client> c,
|
||||
bool is_for_spectator_team_create);
|
||||
void send_ep3_tournament_entry_list(
|
||||
std::shared_ptr<Client> c,
|
||||
std::shared_ptr<const Episode3::Tournament> t,
|
||||
bool is_for_spectator_team_create);
|
||||
void send_ep3_tournament_info(
|
||||
std::shared_ptr<Client> c,
|
||||
std::shared_ptr<const Episode3::Tournament> t);
|
||||
void send_ep3_set_tournament_player_decks(
|
||||
std::shared_ptr<Lobby> l,
|
||||
std::shared_ptr<Client> c,
|
||||
std::shared_ptr<const Episode3::Tournament::Match> match);
|
||||
void send_ep3_tournament_match_result(
|
||||
std::shared_ptr<Lobby> l,
|
||||
std::shared_ptr<const Episode3::Tournament::Match> match);
|
||||
|
||||
void send_ep3_tournament_details(
|
||||
std::shared_ptr<Client> c,
|
||||
std::shared_ptr<const Episode3::Tournament> t);
|
||||
void send_ep3_game_details(
|
||||
std::shared_ptr<Client> c, std::shared_ptr<Lobby> l);
|
||||
|
||||
void send_ep3_update_spectator_count(std::shared_ptr<Lobby> l);
|
||||
|
||||
// Pass mask_key = 0 to unmask the command
|
||||
void set_mask_for_ep3_game_command(void* vdata, size_t size, uint8_t mask_key);
|
||||
|
||||
enum class QuestFileType {
|
||||
ONLINE = 0,
|
||||
@@ -239,9 +368,23 @@ enum class QuestFileType {
|
||||
GBA_DEMO,
|
||||
};
|
||||
|
||||
void send_quest_file(std::shared_ptr<Client> c, const std::string& quest_name,
|
||||
const std::string& basename, const std::string& contents,
|
||||
void send_open_quest_file(
|
||||
std::shared_ptr<Client> c,
|
||||
const std::string& quest_name,
|
||||
const std::string& basename,
|
||||
std::shared_ptr<const std::string> contents,
|
||||
QuestFileType type);
|
||||
void send_quest_file_chunk(
|
||||
shared_ptr<Client> c,
|
||||
const string& filename,
|
||||
size_t chunk_index,
|
||||
const void* data,
|
||||
size_t size,
|
||||
bool is_download_quest);
|
||||
void send_quest_barrier_if_all_clients_ready(std::shared_ptr<Lobby> l);
|
||||
|
||||
void send_card_auction_if_all_clients_ready(
|
||||
std::shared_ptr<ServerState> s, std::shared_ptr<Lobby> l);
|
||||
|
||||
void send_server_time(std::shared_ptr<Client> c);
|
||||
|
||||
|
||||
+144
-47
@@ -20,6 +20,7 @@
|
||||
#include <phosg/Strings.hh>
|
||||
#include <phosg/Time.hh>
|
||||
|
||||
#include "Loggers.hh"
|
||||
#include "PSOProtocol.hh"
|
||||
#include "ReceiveCommands.hh"
|
||||
|
||||
@@ -30,23 +31,56 @@ using namespace std::placeholders;
|
||||
|
||||
void Server::disconnect_client(shared_ptr<Client> c) {
|
||||
if (c->channel.is_virtual_connection) {
|
||||
this->log(INFO, "Disconnecting client on virtual connection %p",
|
||||
c->channel.bev.get());
|
||||
server_log.info("Client disconnected: C-%" PRIX64 " on virtual connection %p",
|
||||
c->id, c->channel.bev.get());
|
||||
} else {
|
||||
this->log(INFO, "Disconnecting client on fd %d",
|
||||
bufferevent_getfd(c->channel.bev.get()));
|
||||
server_log.info("Client disconnected: C-%" PRIX64 " on fd %d",
|
||||
c->id, bufferevent_getfd(c->channel.bev.get()));
|
||||
}
|
||||
|
||||
this->channel_to_client.erase(&c->channel);
|
||||
c->channel.disconnect();
|
||||
|
||||
try {
|
||||
process_disconnect(this->state, c);
|
||||
on_disconnect(this->state, c);
|
||||
} catch (const exception& e) {
|
||||
this->log(WARNING, "Error during client disconnect cleanup: %s", e.what());
|
||||
server_log.warning("Error during client disconnect cleanup: %s", e.what());
|
||||
}
|
||||
|
||||
// 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(move(c));
|
||||
}
|
||||
|
||||
void Server::enqueue_destroy_clients() {
|
||||
auto tv = usecs_to_timeval(0);
|
||||
event_add(this->destroy_clients_ev.get(), &tv);
|
||||
}
|
||||
|
||||
void Server::dispatch_destroy_clients(evutil_socket_t, short, void* ctx) {
|
||||
reinterpret_cast<Server*>(ctx)->destroy_clients();
|
||||
}
|
||||
|
||||
void Server::destroy_clients() {
|
||||
for (auto c_it = this->clients_to_destroy.begin();
|
||||
c_it != this->clients_to_destroy.end();
|
||||
c_it = this->clients_to_destroy.erase(c_it)) {
|
||||
auto c = *c_it;
|
||||
// Note: It's important to move the disconnect hooks out of the client here
|
||||
// because the hooks could modify c->disconnect_hooks while it's being
|
||||
// iterated here, which would invalidate these iterators.
|
||||
unordered_map<string, function<void()>> hooks = move(c->disconnect_hooks);
|
||||
for (auto h_it : hooks) {
|
||||
try {
|
||||
h_it.second();
|
||||
} catch (const exception& e) {
|
||||
c->log.warning("Disconnect hook %s failed: %s", h_it.first.c_str(), e.what());
|
||||
}
|
||||
}
|
||||
}
|
||||
// c is destroyed here (process_disconnect should remove any other references
|
||||
// to it, e.g. from Lobby objects)
|
||||
}
|
||||
|
||||
void Server::dispatch_on_listen_accept(
|
||||
@@ -69,62 +103,69 @@ void Server::on_listen_accept(struct evconnlistener* listener,
|
||||
try {
|
||||
listening_socket = &this->listening_sockets.at(listen_fd);
|
||||
} catch (const out_of_range& e) {
|
||||
this->log(WARNING, "Can\'t determine version for socket %d; disconnecting client",
|
||||
server_log.warning("Can\'t determine version for socket %d; disconnecting client",
|
||||
listen_fd);
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
|
||||
this->log(INFO, "Client fd %d connected via fd %d (%s)",
|
||||
fd, listen_fd, listening_socket->name.c_str());
|
||||
|
||||
struct bufferevent *bev = bufferevent_socket_new(this->base.get(), fd,
|
||||
BEV_OPT_CLOSE_ON_FREE | BEV_OPT_DEFER_CALLBACKS);
|
||||
shared_ptr<Client> c(new Client(
|
||||
bev, listening_socket->version, listening_socket->behavior));
|
||||
c->game_data.should_save = this->state->allow_saving;
|
||||
c->channel.on_command_received = Server::on_client_input;
|
||||
c->channel.on_error = Server::on_client_error;
|
||||
c->channel.context_obj = this;
|
||||
this->channel_to_client.emplace(&c->channel, c);
|
||||
|
||||
server_log.info("Client connected: C-%" PRIX64 " on fd %d via %d (%s)",
|
||||
c->id, fd, listen_fd, listening_socket->addr_str.c_str());
|
||||
|
||||
try {
|
||||
process_connect(this->state, c);
|
||||
on_connect(this->state, c);
|
||||
} catch (const exception& e) {
|
||||
this->log(WARNING, "Error during client initialization: %s", e.what());
|
||||
server_log.warning("Error during client initialization: %s", e.what());
|
||||
this->disconnect_client(c);
|
||||
}
|
||||
}
|
||||
|
||||
void Server::connect_client(
|
||||
struct bufferevent* bev, uint32_t address, uint16_t port,
|
||||
GameVersion version, ServerBehavior initial_state) {
|
||||
this->log(INFO, "Client connected on virtual connection %p", bev);
|
||||
|
||||
struct bufferevent* bev, uint32_t address, uint16_t client_port,
|
||||
uint16_t server_port, GameVersion version, ServerBehavior initial_state) {
|
||||
shared_ptr<Client> c(new Client(bev, version, initial_state));
|
||||
c->game_data.should_save = this->state->allow_saving;
|
||||
c->channel.on_command_received = Server::on_client_input;
|
||||
c->channel.on_error = Server::on_client_error;
|
||||
c->channel.context_obj = this;
|
||||
|
||||
server_log.info("Client connected: C-%" PRIX64 " on virtual connection %p via T-%hu-%s-%s-VI",
|
||||
c->id,
|
||||
bev,
|
||||
server_port,
|
||||
name_for_version(version),
|
||||
name_for_server_behavior(initial_state));
|
||||
|
||||
this->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* sin = reinterpret_cast<sockaddr_in*>(&c->channel.remote_addr);
|
||||
sin->sin_family = AF_INET;
|
||||
sin->sin_addr.s_addr = htonl(address);
|
||||
sin->sin_port = htons(port);
|
||||
auto* remote_sin = reinterpret_cast<sockaddr_in*>(&c->channel.remote_addr);
|
||||
remote_sin->sin_family = AF_INET;
|
||||
remote_sin->sin_addr.s_addr = htonl(address);
|
||||
remote_sin->sin_port = htons(client_port);
|
||||
|
||||
try {
|
||||
process_connect(this->state, c);
|
||||
on_connect(this->state, c);
|
||||
} catch (const exception& e) {
|
||||
this->log(WARNING, "Error during client initialization: %s", e.what());
|
||||
server_log.error("Error during client initialization: %s", e.what());
|
||||
this->disconnect_client(c);
|
||||
}
|
||||
}
|
||||
|
||||
void Server::on_listen_error(struct evconnlistener* listener) {
|
||||
int err = EVUTIL_SOCKET_ERROR();
|
||||
this->log(ERROR, "Failure on listening socket %d: %d (%s)",
|
||||
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);
|
||||
}
|
||||
@@ -136,11 +177,15 @@ void Server::on_client_input(Channel& ch, uint16_t command, uint32_t flag, std::
|
||||
if (c->should_disconnect) {
|
||||
server->disconnect_client(c);
|
||||
} else {
|
||||
try {
|
||||
process_command(server->state, c, command, flag, data);
|
||||
} catch (const exception& e) {
|
||||
server->log(WARNING, "Error processing client command: %s", e.what());
|
||||
c->should_disconnect = true;
|
||||
if (server->state->catch_handler_exceptions) {
|
||||
try {
|
||||
on_command(server->state, c, command, flag, data);
|
||||
} catch (const exception& e) {
|
||||
server_log.warning("Error processing client command: %s", e.what());
|
||||
c->should_disconnect = true;
|
||||
}
|
||||
} else {
|
||||
on_command(server->state, c, command, flag, data);
|
||||
}
|
||||
if (c->should_disconnect) {
|
||||
server->disconnect_client(c);
|
||||
@@ -154,7 +199,7 @@ void Server::on_client_error(Channel& ch, short events) {
|
||||
|
||||
if (events & BEV_EVENT_ERROR) {
|
||||
int err = EVUTIL_SOCKET_ERROR();
|
||||
server->log(WARNING, "Client caused error %d (%s)", err,
|
||||
server_log.warning("Client caused error %d (%s)", err,
|
||||
evutil_socket_error_to_string(err));
|
||||
}
|
||||
if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) {
|
||||
@@ -165,41 +210,41 @@ void Server::on_client_error(Channel& ch, short events) {
|
||||
Server::Server(
|
||||
shared_ptr<struct event_base> base,
|
||||
shared_ptr<ServerState> state)
|
||||
: log("[Server] "),
|
||||
base(base),
|
||||
: base(base),
|
||||
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& name,
|
||||
const std::string& addr_str,
|
||||
const string& socket_path,
|
||||
GameVersion version,
|
||||
ServerBehavior behavior) {
|
||||
int fd = ::listen(socket_path, 0, SOMAXCONN);
|
||||
this->log(INFO, "Listening on Unix socket %s (%s) on fd %d (name: %s)",
|
||||
socket_path.c_str(), name_for_version(version), fd, name.c_str());
|
||||
this->add_socket(name, fd, version, behavior);
|
||||
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, behavior);
|
||||
}
|
||||
|
||||
void Server::listen(
|
||||
const std::string& name,
|
||||
const std::string& addr_str,
|
||||
const string& addr,
|
||||
int port,
|
||||
GameVersion version,
|
||||
ServerBehavior behavior) {
|
||||
int fd = ::listen(addr, port, SOMAXCONN);
|
||||
string netloc_str = render_netloc(addr, port);
|
||||
this->log(INFO, "Listening on TCP interface %s (%s) on fd %d (name: %s)",
|
||||
netloc_str.c_str(), name_for_version(version), fd, name.c_str());
|
||||
this->add_socket(name, fd, version, behavior);
|
||||
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, behavior);
|
||||
}
|
||||
|
||||
void Server::listen(const std::string& name, int port, GameVersion version, ServerBehavior behavior) {
|
||||
this->listen(name, "", port, version, behavior);
|
||||
void Server::listen(const std::string& addr_str, int port, GameVersion version, ServerBehavior behavior) {
|
||||
this->listen(addr_str, "", port, version, behavior);
|
||||
}
|
||||
|
||||
Server::ListeningSocket::ListeningSocket(Server* s, const std::string& name,
|
||||
Server::ListeningSocket::ListeningSocket(Server* s, const std::string& addr_str,
|
||||
int fd, GameVersion version, ServerBehavior behavior) :
|
||||
name(name), fd(fd), version(version), behavior(behavior), listener(
|
||||
addr_str(addr_str), fd(fd), version(version), behavior(behavior), listener(
|
||||
evconnlistener_new(s->base.get(), Server::dispatch_on_listen_accept, s,
|
||||
LEV_OPT_REUSEABLE, 0, this->fd), evconnlistener_free) {
|
||||
evconnlistener_set_error_cb(this->listener.get(),
|
||||
@@ -207,12 +252,12 @@ Server::ListeningSocket::ListeningSocket(Server* s, const std::string& name,
|
||||
}
|
||||
|
||||
void Server::add_socket(
|
||||
const std::string& name,
|
||||
const std::string& addr_str,
|
||||
int fd,
|
||||
GameVersion version,
|
||||
ServerBehavior behavior) {
|
||||
this->listening_sockets.emplace(piecewise_construct, forward_as_tuple(fd),
|
||||
forward_as_tuple(this, name, fd, version, behavior));
|
||||
forward_as_tuple(this, addr_str, fd, version, behavior));
|
||||
}
|
||||
|
||||
shared_ptr<Client> Server::get_client() const {
|
||||
@@ -224,3 +269,55 @@ shared_ptr<Client> Server::get_client() const {
|
||||
}
|
||||
return this->channel_to_client.begin()->second;
|
||||
}
|
||||
|
||||
vector<shared_ptr<Client>> Server::get_clients_by_identifier(const string& ident) const {
|
||||
int64_t serial_number_hex = -1;
|
||||
int64_t serial_number_dec = -1;
|
||||
try {
|
||||
serial_number_dec = stoul(ident, nullptr, 10);
|
||||
} catch (const invalid_argument&) { }
|
||||
try {
|
||||
serial_number_hex = stoul(ident, nullptr, 16);
|
||||
} catch (const invalid_argument&) { }
|
||||
u16string u16name = decode_sjis(ident);
|
||||
|
||||
// TODO: It's kind of not great that we do a linear search here, but this is
|
||||
// only used in the shell, so it should be pretty rare.
|
||||
vector<shared_ptr<Client>> results;
|
||||
for (const auto& it : this->channel_to_client) {
|
||||
auto c = it.second;
|
||||
if (c->license && c->license->serial_number == serial_number_dec) {
|
||||
results.emplace_back(move(c));
|
||||
continue;
|
||||
}
|
||||
if (c->license && c->license->serial_number == serial_number_hex) {
|
||||
results.emplace_back(move(c));
|
||||
continue;
|
||||
}
|
||||
if (c->license && c->license->username == ident) {
|
||||
results.emplace_back(move(c));
|
||||
continue;
|
||||
}
|
||||
|
||||
auto p = c->game_data.player(false);
|
||||
if (p && p->disp.name == u16name) {
|
||||
results.emplace_back(move(c));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c->channel.name == ident) {
|
||||
results.emplace_back(move(c));
|
||||
continue;
|
||||
}
|
||||
if (starts_with(c->channel.name, ident + " ")) {
|
||||
results.emplace_back(move(c));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
shared_ptr<struct event_base> Server::get_base() const {
|
||||
return this->base;
|
||||
}
|
||||
|
||||
+16
-7
@@ -21,22 +21,26 @@ public:
|
||||
std::shared_ptr<ServerState> state);
|
||||
virtual ~Server() = default;
|
||||
|
||||
void listen(const std::string& name, const std::string& socket_path, GameVersion version, ServerBehavior initial_state);
|
||||
void listen(const std::string& name, const std::string& addr, int port, GameVersion version, ServerBehavior initial_state);
|
||||
void listen(const std::string& name, int port, GameVersion version, ServerBehavior initial_state);
|
||||
void add_socket(const std::string& name, int fd, GameVersion version, ServerBehavior initial_state);
|
||||
void listen(const std::string& addr_str, const std::string& socket_path, GameVersion version, ServerBehavior initial_state);
|
||||
void listen(const std::string& addr_str, const std::string& addr, int port, GameVersion version, ServerBehavior initial_state);
|
||||
void listen(const std::string& addr_str, int port, GameVersion version, ServerBehavior initial_state);
|
||||
void add_socket(const std::string& addr_str, int fd, GameVersion version, ServerBehavior initial_state);
|
||||
|
||||
void connect_client(struct bufferevent* bev, uint32_t address, uint16_t port,
|
||||
void connect_client(struct bufferevent* bev, uint32_t address,
|
||||
uint16_t client_port, uint16_t server_port,
|
||||
GameVersion version, ServerBehavior initial_state);
|
||||
|
||||
std::shared_ptr<Client> get_client() 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;
|
||||
|
||||
private:
|
||||
PrefixedLogger log;
|
||||
std::shared_ptr<struct event_base> base;
|
||||
std::shared_ptr<struct event> destroy_clients_ev;
|
||||
|
||||
struct ListeningSocket {
|
||||
std::string name;
|
||||
std::string addr_str;
|
||||
int fd;
|
||||
GameVersion version;
|
||||
ServerBehavior behavior;
|
||||
@@ -51,9 +55,14 @@ private:
|
||||
};
|
||||
std::unordered_map<int, ListeningSocket> listening_sockets;
|
||||
std::unordered_map<Channel*, std::shared_ptr<Client>> channel_to_client;
|
||||
std::unordered_set<std::shared_ptr<Client>> clients_to_destroy;
|
||||
|
||||
std::shared_ptr<ServerState> state;
|
||||
|
||||
void enqueue_destroy_clients();
|
||||
static void dispatch_destroy_clients(evutil_socket_t, short, void* ctx);
|
||||
void destroy_clients();
|
||||
|
||||
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);
|
||||
|
||||
+439
-69
@@ -5,7 +5,9 @@
|
||||
#include <string.h>
|
||||
|
||||
#include <phosg/Strings.hh>
|
||||
#include <phosg/Random.hh>
|
||||
|
||||
#include "ReceiveCommands.hh"
|
||||
#include "ServerState.hh"
|
||||
#include "SendCommands.hh"
|
||||
#include "StaticGameData.hh"
|
||||
@@ -17,18 +19,21 @@ using namespace std;
|
||||
ServerShell::ServerShell(
|
||||
shared_ptr<struct event_base> base,
|
||||
shared_ptr<ServerState> state)
|
||||
: Shell(base, state) { }
|
||||
: Shell(base), state(state) { }
|
||||
|
||||
void ServerShell::print_prompt() {
|
||||
fwrite("newserv> ", 9, 1, stdout);
|
||||
fwritex(stdout, Shell::PROMPT);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
shared_ptr<ProxyServer::LinkedSession> ServerShell::get_proxy_session() {
|
||||
shared_ptr<ProxyServer::LinkedSession> ServerShell::get_proxy_session(
|
||||
const string& name) {
|
||||
if (!this->state->proxy_server.get()) {
|
||||
throw runtime_error("the proxy server is disabled");
|
||||
}
|
||||
return this->state->proxy_server->get_session();
|
||||
return name.empty()
|
||||
? this->state->proxy_server->get_session()
|
||||
: this->state->proxy_server->get_session_by_name(name);
|
||||
}
|
||||
|
||||
static void set_boolean(bool* target, const string& args) {
|
||||
@@ -41,13 +46,50 @@ static void set_boolean(bool* target, const string& args) {
|
||||
}
|
||||
}
|
||||
|
||||
static string get_quoted_string(string& s) {
|
||||
string ret;
|
||||
char end_char = (s.at(0) == '\"') ? '\"' : ' ';
|
||||
size_t z = (s.at(0) == '\"') ? 1 : 0;
|
||||
for (; (z < s.size()) && (s[z] != end_char); z++) {
|
||||
if (s[z] == '\\') {
|
||||
if (z + 1 < s.size()) {
|
||||
ret.push_back(s[z + 1]);
|
||||
} else {
|
||||
throw runtime_error("incomplete escape sequence");
|
||||
}
|
||||
} else {
|
||||
ret.push_back(s[z]);
|
||||
}
|
||||
}
|
||||
if (end_char != ' ') {
|
||||
if (z >= s.size()) {
|
||||
throw runtime_error("unterminated quoted string");
|
||||
}
|
||||
s = s.substr(skip_whitespace(s, z + 1));
|
||||
} else {
|
||||
s = s.substr(skip_whitespace(s, z));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void ServerShell::execute_command(const string& command) {
|
||||
// find the entry in the command table and run the command
|
||||
// Find the entry in the command table and run the command
|
||||
size_t command_end = skip_non_whitespace(command, 0);
|
||||
size_t args_begin = skip_whitespace(command, command_end);
|
||||
string command_name = command.substr(0, command_end);
|
||||
string command_args = command.substr(args_begin);
|
||||
|
||||
string session_name;
|
||||
if (command_name == "on") {
|
||||
size_t session_name_end = skip_non_whitespace(command_args, 0);
|
||||
size_t command_begin = skip_whitespace(command_args, session_name_end);
|
||||
command_end = skip_non_whitespace(command_args, command_begin);
|
||||
args_begin = skip_whitespace(command_args, command_end);
|
||||
session_name = command_args.substr(0, session_name_end);
|
||||
command_name = command_args.substr(command_begin, command_end - command_begin);
|
||||
command_args = command_args.substr(args_begin);
|
||||
}
|
||||
|
||||
if (command_name == "exit") {
|
||||
throw exit_shell();
|
||||
|
||||
@@ -61,7 +103,7 @@ Server commands:\n\
|
||||
exit (or ctrl+d)\n\
|
||||
Shut down the server.\n\
|
||||
reload <item> ...\n\
|
||||
Reload data. <item> can be licenses, battle-params, level-table, or quests.\n\
|
||||
Reload data. <item> can be licenses, quests, functions, programs, or ep3.\n\
|
||||
Reloading will not affect items that are in use; for example, if a client\'s\n\
|
||||
license is deleted by reloading, they will not be disconnected immediately.\n\
|
||||
add-license <parameters>\n\
|
||||
@@ -69,9 +111,13 @@ Server commands:\n\
|
||||
bb-username=<username> (BB username)\n\
|
||||
bb-password=<password> (BB password)\n\
|
||||
gc-password=<password> (GC password)\n\
|
||||
access-key=<access-key> (GC/PC access key)\n\
|
||||
serial=<serial-number> (GC/PC serial number; required for all licenses)\n\
|
||||
access-key=<access-key> (DC/GC/PC access key)\n\
|
||||
serial=<serial-number> (decimal serial number; required for all licenses)\n\
|
||||
privileges=<privilege-mask> (can be normal, mod, admin, root, or numeric)\n\
|
||||
update-license <serial-number> <parameters>\n\
|
||||
Update an existing license. <serial-number> specifies which license to\n\
|
||||
update. The options in <parameters> are the same as for the add-license\n\
|
||||
command.\n\
|
||||
delete-license <serial-number>\n\
|
||||
Delete a license from the server.\n\
|
||||
list-licenses\n\
|
||||
@@ -89,12 +135,44 @@ Server commands:\n\
|
||||
Song IDs are 0 through 51; the default song is -1.\n\
|
||||
announce <message>\n\
|
||||
Send an announcement message to all players.\n\
|
||||
create-tournament \"Tournament Name\" \"Map Name\" <num-teams> [options...]\n\
|
||||
Create an Episode 3 tournament. The quotes are required arount the\n\
|
||||
tournament and map names, unless the names contain no spaces.\n\
|
||||
Rules options:\n\
|
||||
2v2: Set team size to 2 players (default is 1 without this option)\n\
|
||||
dice=MIN-MAX: Set minimum and maximum dice rolls\n\
|
||||
overall-time-limit=N: Set battle time limit (in multiples of 5 minutes)\n\
|
||||
phase-time-limit=N: Set phase time limit (in seconds)\n\
|
||||
allowed-cards=ALL/N/NR/NRS: Set rarities of allowed cards\n\
|
||||
deck-shuffle=ON/OFF: Enable/disable deck shuffle\n\
|
||||
deck-loop=ON/OFF: Enable/disable deck loop\n\
|
||||
hp=N: Set Story Character initial HP\n\
|
||||
hp-type=TEAM/PLAYER/COMMON: Set team HP type\n\
|
||||
allow-assists=ON/OFF: Enable/disable assist cards\n\
|
||||
dialogue=ON/OFF: Enable/disable dialogue\n\
|
||||
dice-exchange=ATK/DEF/NONE: Set dice exchange mode\n\
|
||||
dice-boost=ON/OFF: Enable/disable dice boost\n\
|
||||
delete-tournament \"Tournament Name\"\n\
|
||||
Delete a tournament. The quotes are required unless the tournament name\n\
|
||||
contains no spaces.\n\
|
||||
list-tournaments\n\
|
||||
List the names and numbers of all existing tournaments.\n\
|
||||
start-tournament \"Tournament Name\"\n\
|
||||
End registration for a tournament and allow matches to begin. The quotes\n\
|
||||
are required unless the tournament name contains no spaces.\n\
|
||||
tournament-state \"Tournament Name\"\n\
|
||||
Show the current state of a tournament. The quotes are required unless the\n\
|
||||
tournament name contains no spaces.\n\
|
||||
\n\
|
||||
Proxy commands (these will only work when exactly one client is connected):\n\
|
||||
Proxy commands:\n\
|
||||
sc <data>\n\
|
||||
Send a command to the client.\n\
|
||||
Send a command to the client. This command also can be used to send data to\n\
|
||||
a client on the game server.\n\
|
||||
ss <data>\n\
|
||||
Send a command to the server.\n\
|
||||
show-slots\n\
|
||||
Show the player names, Guild Card numbers, and client IDs of all players in\n\
|
||||
the current lobby or game.\n\
|
||||
chat <text>\n\
|
||||
Send a chat message to the server.\n\
|
||||
dchat <data>\n\
|
||||
@@ -145,8 +223,21 @@ Proxy commands (these will only work when exactly one client is connected):\n\
|
||||
responds as if the function was called (with the given return value), but\n\
|
||||
does not send the code to the client. To stop blocking function calls, omit\n\
|
||||
the return value.\n\
|
||||
create-item <data>\n\
|
||||
Create an item as if the client had run the $item command.\n\
|
||||
set-next-item <data>\n\
|
||||
Set the next item to be dropped.\n\
|
||||
close-idle-sessions\n\
|
||||
Closes all sessions that don\'t have a client and server connected.\n\
|
||||
\n\
|
||||
If there are multiple clients connected, or multiple proxy sessions open, many\n\
|
||||
of the above commands will fail since they can\'t determine which session should\n\
|
||||
be affected. To specify a session, prefix the command with `on <ident>`. For\n\
|
||||
game server sessions, <ident> may be the client\'s ID (e.g. C-3), player name,\n\
|
||||
license serial number (specified in hex or in decimal), or BB account username.\n\
|
||||
For proxy sessions, <ident> may only be the session ID (which is generally the\n\
|
||||
same as the client\'s serial number). For example, to send a ping to the proxy\n\
|
||||
session with ID 17205AE4, run the command `on 17205AE4 sc 1D 00 04 00`.\n\
|
||||
");
|
||||
|
||||
|
||||
@@ -162,15 +253,19 @@ Proxy commands (these will only work when exactly one client is connected):\n\
|
||||
if (type == "licenses") {
|
||||
shared_ptr<LicenseManager> lm(new LicenseManager("system/licenses.nsi"));
|
||||
this->state->license_manager = lm;
|
||||
} else if (type == "battle-params") {
|
||||
shared_ptr<BattleParamTable> bpt(new BattleParamTable("system/blueburst/BattleParamEntry"));
|
||||
this->state->battle_params = bpt;
|
||||
} else if (type == "level-table") {
|
||||
shared_ptr<LevelTable> lt(new LevelTable("system/blueburst/PlyLevelTbl.prs", true));
|
||||
this->state->level_table = lt;
|
||||
} else if (type == "quests") {
|
||||
shared_ptr<QuestIndex> qi(new QuestIndex("system/quests"));
|
||||
this->state->quest_index = qi;
|
||||
} else if (type == "functions") {
|
||||
shared_ptr<FunctionCodeIndex> fci(new FunctionCodeIndex("system/ppc"));
|
||||
this->state->function_code_index = fci;
|
||||
} else if (type == "programs") {
|
||||
shared_ptr<DOLFileIndex> dfi(new DOLFileIndex("system/dol"));
|
||||
this->state->dol_file_index = dfi;
|
||||
} else if (type == "programs") {
|
||||
shared_ptr<Episode3::DataIndex> data_index(new Episode3::DataIndex(
|
||||
"system/ep3", this->state->ep3_behavior_flags));
|
||||
this->state->ep3_data_index = data_index;
|
||||
} else {
|
||||
throw invalid_argument("incorrect data type");
|
||||
}
|
||||
@@ -222,7 +317,7 @@ Proxy commands (these will only work when exactly one client is connected):\n\
|
||||
}
|
||||
|
||||
} else {
|
||||
throw invalid_argument("incorrect field");
|
||||
throw invalid_argument("incorrect field: " + token);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -233,6 +328,70 @@ Proxy commands (these will only work when exactly one client is connected):\n\
|
||||
this->state->license_manager->add(l);
|
||||
fprintf(stderr, "license added\n");
|
||||
|
||||
} else if (command_name == "update-license") {
|
||||
auto tokens = split(command_args, ' ');
|
||||
if (tokens.size() < 2) {
|
||||
throw runtime_error("not enough arguments");
|
||||
}
|
||||
uint32_t serial_number = stoul(tokens[0]);
|
||||
tokens.erase(tokens.begin());
|
||||
auto orig_l = this->state->license_manager->get(serial_number);
|
||||
shared_ptr<License> l(new License(*orig_l));
|
||||
|
||||
for (const string& token : tokens) {
|
||||
if (starts_with(token, "bb-username=")) {
|
||||
if (token.size() >= 32) {
|
||||
throw invalid_argument("username too long");
|
||||
}
|
||||
l->username = token.substr(12);
|
||||
|
||||
} else if (starts_with(token, "bb-password=")) {
|
||||
if (token.size() >= 32) {
|
||||
throw invalid_argument("bb-password too long");
|
||||
}
|
||||
l->bb_password = token.substr(12);
|
||||
|
||||
} else if (starts_with(token, "gc-password=")) {
|
||||
if (token.size() > 20) {
|
||||
throw invalid_argument("gc-password too long");
|
||||
}
|
||||
l->gc_password = token.substr(12);
|
||||
|
||||
} else if (starts_with(token, "access-key=")) {
|
||||
if (token.size() > 23) {
|
||||
throw invalid_argument("access-key is too long");
|
||||
}
|
||||
l->access_key = token.substr(11);
|
||||
|
||||
} else if (starts_with(token, "serial=")) {
|
||||
l->serial_number = stoul(token.substr(7));
|
||||
|
||||
} else if (starts_with(token, "privileges=")) {
|
||||
string mask = token.substr(11);
|
||||
if (mask == "normal") {
|
||||
l->privileges = 0;
|
||||
} else if (mask == "mod") {
|
||||
l->privileges = Privilege::MODERATOR;
|
||||
} else if (mask == "admin") {
|
||||
l->privileges = Privilege::ADMINISTRATOR;
|
||||
} else if (mask == "root") {
|
||||
l->privileges = Privilege::ROOT;
|
||||
} else {
|
||||
l->privileges = stoul(mask);
|
||||
}
|
||||
|
||||
} else {
|
||||
throw invalid_argument("incorrect field: " + token);
|
||||
}
|
||||
}
|
||||
|
||||
if (!l->serial_number) {
|
||||
throw invalid_argument("license does not contain serial number");
|
||||
}
|
||||
|
||||
this->state->license_manager->add(l);
|
||||
fprintf(stderr, "license updated\n");
|
||||
|
||||
} else if (command_name == "delete-license") {
|
||||
uint32_t serial_number = stoul(command_args);
|
||||
this->state->license_manager->remove(serial_number);
|
||||
@@ -274,12 +433,136 @@ Proxy commands (these will only work when exactly one client is connected):\n\
|
||||
u16string message16 = decode_sjis(command_args);
|
||||
send_text_message(this->state, message16.c_str());
|
||||
|
||||
} else if (command_name == "create-tournament") {
|
||||
string name = get_quoted_string(command_args);
|
||||
string map_name = get_quoted_string(command_args);
|
||||
auto map = this->state->ep3_data_index->definition_for_map_name(map_name);
|
||||
uint32_t num_teams = stoul(get_quoted_string(command_args), nullptr, 0);
|
||||
Episode3::Rules rules;
|
||||
rules.set_defaults();
|
||||
bool is_2v2 = false;
|
||||
if (!command_args.empty()) {
|
||||
auto tokens = split(command_args, ' ');
|
||||
for (auto& token : tokens) {
|
||||
token = tolower(token);
|
||||
if (token == "2v2") {
|
||||
is_2v2 = true;
|
||||
} else if (starts_with(token, "dice=")) {
|
||||
auto subtokens = split(token.substr(5), '-');
|
||||
if (subtokens.size() != 2) {
|
||||
throw runtime_error("dice option must be of the form dice=X-Y");
|
||||
}
|
||||
rules.min_dice = stoul(subtokens[0]);
|
||||
rules.max_dice = stoul(subtokens[0]);
|
||||
} else if (starts_with(token, "overall-time-limit=")) {
|
||||
uint32_t limit = stoul(token.substr(19));
|
||||
if (limit > 600) {
|
||||
throw runtime_error("overall-time-limit must be 600 or fewer minutes");
|
||||
}
|
||||
if (limit % 5) {
|
||||
throw runtime_error("overall-time-limit must be a multiple of 5 minutes");
|
||||
}
|
||||
rules.overall_time_limit = limit;
|
||||
} else if (starts_with(token, "phase-time-limit=")) {
|
||||
rules.phase_time_limit = stoul(token.substr(17));
|
||||
} else if (starts_with(token, "hp=")) {
|
||||
rules.char_hp = stoul(token.substr(3));
|
||||
} else if (token == "allowed-cards=all") {
|
||||
rules.allowed_cards = Episode3::AllowedCards::ALL;
|
||||
} else if (token == "allowed-cards=n") {
|
||||
rules.allowed_cards = Episode3::AllowedCards::N_ONLY;
|
||||
} else if (token == "allowed-cards=nr") {
|
||||
rules.allowed_cards = Episode3::AllowedCards::N_R_ONLY;
|
||||
} else if (token == "allowed-cards=nrs") {
|
||||
rules.allowed_cards = Episode3::AllowedCards::N_R_S_ONLY;
|
||||
} else if (token == "deck-shuffle=on") {
|
||||
rules.disable_deck_shuffle = 0;
|
||||
} else if (token == "deck-shuffle=off") {
|
||||
rules.disable_deck_shuffle = 1;
|
||||
} else if (token == "deck-loop=on") {
|
||||
rules.disable_deck_loop = 0;
|
||||
} else if (token == "deck-loop=off") {
|
||||
rules.disable_deck_loop = 1;
|
||||
} else if (token == "allow-assists=on") {
|
||||
rules.no_assist_cards = 0;
|
||||
} else if (token == "allow-assists=off") {
|
||||
rules.no_assist_cards = 1;
|
||||
} else if (token == "dialogue=on") {
|
||||
rules.disable_dialogue = 0;
|
||||
} else if (token == "dialogue=off") {
|
||||
rules.disable_dialogue = 1;
|
||||
} else if (token == "dice-boost=on") {
|
||||
rules.disable_dice_boost = 0;
|
||||
} else if (token == "dice-boost=off") {
|
||||
rules.disable_dice_boost = 1;
|
||||
} else if (token == "hp-type=player") {
|
||||
rules.hp_type = Episode3::HPType::DEFEAT_PLAYER;
|
||||
} else if (token == "hp-type=team") {
|
||||
rules.hp_type = Episode3::HPType::DEFEAT_TEAM;
|
||||
} else if (token == "hp-type=common") {
|
||||
rules.hp_type = Episode3::HPType::COMMON_HP;
|
||||
} else if (token == "dice-exchange=atk") {
|
||||
rules.dice_exchange_mode = Episode3::DiceExchangeMode::HIGH_ATK;
|
||||
} else if (token == "dice-exchange=def") {
|
||||
rules.dice_exchange_mode = Episode3::DiceExchangeMode::HIGH_DEF;
|
||||
} else if (token == "dice-exchange=none") {
|
||||
rules.dice_exchange_mode = Episode3::DiceExchangeMode::NONE;
|
||||
} else {
|
||||
throw runtime_error("invalid rules option: " + token);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (rules.check_and_reset_invalid_fields()) {
|
||||
fprintf(stderr, "warning: some rules were invalid and reset to defaults\n");
|
||||
}
|
||||
auto tourn = this->state->ep3_tournament_index->create_tournament(
|
||||
name, map, rules, num_teams, is_2v2);
|
||||
this->state->ep3_tournament_index->save();
|
||||
fprintf(stderr, "created tournament %02hhX\n", tourn->get_number());
|
||||
|
||||
} else if (command_name == "delete-tournament") {
|
||||
string name = get_quoted_string(command_args);
|
||||
auto tourn = this->state->ep3_tournament_index->get_tournament(name);
|
||||
if (tourn) {
|
||||
this->state->ep3_tournament_index->delete_tournament(tourn->get_number());
|
||||
this->state->ep3_tournament_index->save();
|
||||
fprintf(stderr, "tournament deleted\n");
|
||||
} else {
|
||||
fprintf(stderr, "no such tournament exists\n");
|
||||
}
|
||||
|
||||
} else if (command_name == "list-tournaments") {
|
||||
for (const auto& tourn : this->state->ep3_tournament_index->all_tournaments()) {
|
||||
fprintf(stderr, " %s\n", tourn->get_name().c_str());
|
||||
}
|
||||
|
||||
} else if (command_name == "start-tournament") {
|
||||
string name = get_quoted_string(command_args);
|
||||
auto tourn = this->state->ep3_tournament_index->get_tournament(name);
|
||||
if (tourn) {
|
||||
tourn->start();
|
||||
this->state->ep3_tournament_index->save();
|
||||
send_ep3_text_message_printf(this->state, "$C7The tournament\n$C6%s$C7\nhas begun", tourn->get_name().c_str());
|
||||
fprintf(stderr, "tournament started\n");
|
||||
} else {
|
||||
fprintf(stderr, "no such tournament exists\n");
|
||||
}
|
||||
|
||||
} else if (command_name == "tournament-status") {
|
||||
string name = get_quoted_string(command_args);
|
||||
auto tourn = this->state->ep3_tournament_index->get_tournament(name);
|
||||
if (tourn) {
|
||||
tourn->print_bracket(stderr);
|
||||
} else {
|
||||
fprintf(stderr, "no such tournament exists\n");
|
||||
}
|
||||
|
||||
|
||||
|
||||
// PROXY COMMANDS
|
||||
|
||||
} else if ((command_name == "sc") || (command_name == "ss")) {
|
||||
string data = parse_data_string(command_args);
|
||||
string data = parse_data_string(command_args, nullptr, ParseDataFlags::ALLOW_FILES);
|
||||
if (data.size() & 3) {
|
||||
throw invalid_argument("data size is not a multiple of 4");
|
||||
}
|
||||
@@ -289,7 +572,7 @@ Proxy commands (these will only work when exactly one client is connected):\n\
|
||||
|
||||
shared_ptr<ProxyServer::LinkedSession> proxy_session;
|
||||
try {
|
||||
proxy_session = this->get_proxy_session();
|
||||
proxy_session = this->get_proxy_session(session_name);
|
||||
} catch (const exception&) { }
|
||||
|
||||
if (proxy_session.get()) {
|
||||
@@ -300,51 +583,89 @@ Proxy commands (these will only work when exactly one client is connected):\n\
|
||||
}
|
||||
|
||||
} else {
|
||||
if (command_name [1] == 's') {
|
||||
throw runtime_error("cannot send to server in non-proxy session");
|
||||
shared_ptr<Client> c;
|
||||
if (session_name.empty()) {
|
||||
c = this->state->game_server->get_client();
|
||||
} else {
|
||||
auto clients = this->state->game_server->get_clients_by_identifier(
|
||||
session_name);
|
||||
if (clients.empty()) {
|
||||
throw runtime_error("no such client");
|
||||
}
|
||||
if (clients.size() > 1) {
|
||||
throw runtime_error("multiple clients found");
|
||||
}
|
||||
c = move(clients[0]);
|
||||
}
|
||||
|
||||
auto c = this->state->game_server->get_client();
|
||||
send_command_with_header(c, data.data(), data.size());
|
||||
if (c) {
|
||||
if (command_name[1] == 's') {
|
||||
on_command_with_header(this->state, c, data);
|
||||
} else {
|
||||
send_command_with_header(c->channel, data.data(), data.size());
|
||||
}
|
||||
} else {
|
||||
throw runtime_error("no client available");
|
||||
}
|
||||
}
|
||||
|
||||
} else if (command_name == "show-slots") {
|
||||
auto session = this->get_proxy_session(session_name);
|
||||
for (size_t z = 0; z < session->lobby_players.size(); z++) {
|
||||
const auto& player = session->lobby_players[z];
|
||||
if (player.guild_card_number) {
|
||||
auto secid_name = name_for_section_id(player.section_id);
|
||||
fprintf(stderr, " %zu: %" PRIu32 " => %s (%s, %s)\n",
|
||||
z, player.guild_card_number, player.name.c_str(),
|
||||
name_for_char_class(player.char_class), secid_name.c_str());
|
||||
} else {
|
||||
fprintf(stderr, " %zu: (no player)\n", z);
|
||||
}
|
||||
}
|
||||
|
||||
} else if ((command_name == "chat") || (command_name == "dchat")) {
|
||||
auto session = this->get_proxy_session();
|
||||
auto session = this->get_proxy_session(session_name);
|
||||
bool is_dchat = (command_name == "dchat");
|
||||
|
||||
string data(8, '\0');
|
||||
data.push_back('\x09');
|
||||
data.push_back('E');
|
||||
if (command_name == "dchat") {
|
||||
data += parse_data_string(command_args);
|
||||
if (!is_dchat && (session->version == GameVersion::PC || session->version == GameVersion::BB)) {
|
||||
u16string data(4, u'\0');
|
||||
data.push_back(u'\x09');
|
||||
data.push_back(u'E');
|
||||
data += decode_sjis(command_args);
|
||||
data.push_back(u'\0');
|
||||
data.resize((data.size() + 1) & (~1));
|
||||
session->server_channel.send(0x06, 0x00, data.data(), data.size() * sizeof(char16_t));
|
||||
} else {
|
||||
data += command_args;
|
||||
string data(8, '\0');
|
||||
data.push_back('\x09');
|
||||
data.push_back('E');
|
||||
if (is_dchat) {
|
||||
data += parse_data_string(command_args, nullptr, ParseDataFlags::ALLOW_FILES);
|
||||
} else {
|
||||
data += command_args;
|
||||
data.push_back('\0');
|
||||
}
|
||||
data.resize((data.size() + 3) & (~3));
|
||||
session->server_channel.send(0x06, 0x00, data);
|
||||
}
|
||||
data.push_back('\0');
|
||||
data.resize((data.size() + 3) & (~3));
|
||||
|
||||
session->server_channel.send(0x06, 0x00, data);
|
||||
|
||||
} else if (command_name == "marker") {
|
||||
auto session = this->get_proxy_session();
|
||||
auto session = this->get_proxy_session(session_name);
|
||||
session->server_channel.send(0x89, stoul(command_args));
|
||||
|
||||
} else if (command_name == "warp") {
|
||||
auto session = this->get_proxy_session();
|
||||
auto session = this->get_proxy_session(session_name);
|
||||
|
||||
PSOSubcommand cmds[2];
|
||||
cmds[0].word[0] = 0x0294;
|
||||
cmds[0].word[1] = session->lobby_client_id;
|
||||
cmds[1].dword = stoul(command_args);
|
||||
|
||||
session->client_channel.send(0x60, 0x00, &cmds, sizeof(cmds));
|
||||
session->server_channel.send(0x60, 0x00, &cmds, sizeof(cmds));
|
||||
uint8_t area = stoul(command_args);
|
||||
send_warp(session->client_channel, session->lobby_client_id, area);
|
||||
send_warp(session->server_channel, session->lobby_client_id, area);
|
||||
|
||||
} else if ((command_name == "info-board") || (command_name == "info-board-data")) {
|
||||
auto session = this->get_proxy_session();
|
||||
auto session = this->get_proxy_session(session_name);
|
||||
|
||||
string data;
|
||||
if (command_name == "info-board-data") {
|
||||
data += parse_data_string(command_args);
|
||||
data += parse_data_string(command_args, nullptr, ParseDataFlags::ALLOW_FILES);
|
||||
} else {
|
||||
data += command_args;
|
||||
}
|
||||
@@ -354,56 +675,105 @@ Proxy commands (these will only work when exactly one client is connected):\n\
|
||||
session->server_channel.send(0xD9, 0x00, data);
|
||||
|
||||
} else if (command_name == "set-override-section-id") {
|
||||
auto session = this->get_proxy_session();
|
||||
auto session = this->get_proxy_session(session_name);
|
||||
if (command_args.empty()) {
|
||||
session->override_section_id = -1;
|
||||
session->options.override_section_id = -1;
|
||||
} else {
|
||||
session->override_section_id = section_id_for_name(command_args);
|
||||
session->options.override_section_id = section_id_for_name(command_args);
|
||||
}
|
||||
|
||||
} else if (command_name == "set-override-event") {
|
||||
auto session = this->get_proxy_session();
|
||||
auto session = this->get_proxy_session(session_name);
|
||||
if (command_args.empty()) {
|
||||
session->override_lobby_event = -1;
|
||||
session->options.override_lobby_event = -1;
|
||||
} else {
|
||||
session->override_lobby_event = event_for_name(command_args);
|
||||
session->client_channel.send(0xDA, session->override_lobby_event);
|
||||
session->options.override_lobby_event = event_for_name(command_args);
|
||||
if ((session->version != GameVersion::DC) &&
|
||||
(session->version != GameVersion::PC) && (
|
||||
!((session->version == GameVersion::GC) &&
|
||||
(session->newserv_client_config.cfg.flags & Client::Flag::IS_TRIAL_EDITION)))) {
|
||||
session->client_channel.send(0xDA, session->options.override_lobby_event);
|
||||
}
|
||||
}
|
||||
|
||||
} else if (command_name == "set-override-lobby-number") {
|
||||
auto session = this->get_proxy_session();
|
||||
auto session = this->get_proxy_session(session_name);
|
||||
if (command_args.empty()) {
|
||||
session->override_lobby_number = -1;
|
||||
session->options.override_lobby_number = -1;
|
||||
} else {
|
||||
session->override_lobby_number = lobby_type_for_name(command_args);
|
||||
session->options.override_lobby_number = lobby_type_for_name(command_args);
|
||||
}
|
||||
|
||||
} else if (command_name == "set-chat-filter") {
|
||||
auto session = this->get_proxy_session();
|
||||
set_boolean(&session->enable_chat_filter, command_args);
|
||||
auto session = this->get_proxy_session(session_name);
|
||||
set_boolean(&session->options.enable_chat_filter, command_args);
|
||||
|
||||
} else if (command_name == "set-infinite-hp") {
|
||||
auto session = this->get_proxy_session();
|
||||
set_boolean(&session->infinite_hp, command_args);
|
||||
auto session = this->get_proxy_session(session_name);
|
||||
set_boolean(&session->options.infinite_hp, command_args);
|
||||
|
||||
} else if (command_name == "set-infinite-tp") {
|
||||
auto session = this->get_proxy_session();
|
||||
set_boolean(&session->infinite_tp, command_args);
|
||||
auto session = this->get_proxy_session(session_name);
|
||||
set_boolean(&session->options.infinite_tp, command_args);
|
||||
|
||||
} else if (command_name == "set-switch-assist") {
|
||||
auto session = this->get_proxy_session();
|
||||
set_boolean(&session->switch_assist, command_args);
|
||||
auto session = this->get_proxy_session(session_name);
|
||||
set_boolean(&session->options.switch_assist, command_args);
|
||||
|
||||
} else if (command_name == "set-save-files") {
|
||||
auto session = this->get_proxy_session();
|
||||
set_boolean(&session->save_files, command_args);
|
||||
} else if (command_name == "set-save-files" && this->state->proxy_allow_save_files) {
|
||||
auto session = this->get_proxy_session(session_name);
|
||||
set_boolean(&session->options.save_files, command_args);
|
||||
|
||||
} else if (command_name == "set-block-function-calls") {
|
||||
auto session = this->get_proxy_session();
|
||||
auto session = this->get_proxy_session(session_name);
|
||||
if (command_args.empty()) {
|
||||
session->function_call_return_value = -1;
|
||||
session->options.function_call_return_value = -1;
|
||||
} else {
|
||||
session->function_call_return_value = stoul(command_args);
|
||||
session->options.function_call_return_value = stoul(command_args);
|
||||
}
|
||||
|
||||
} else if ((command_name == "create-item") || (command_name == "set-next-item")) {
|
||||
auto session = this->get_proxy_session(session_name);
|
||||
|
||||
if (session->version == GameVersion::BB) {
|
||||
throw runtime_error("proxy session is BB");
|
||||
}
|
||||
if (!session->is_in_game) {
|
||||
throw runtime_error("proxy session is not in a game");
|
||||
}
|
||||
if (session->lobby_client_id != session->leader_client_id) {
|
||||
throw runtime_error("proxy session is not game leader");
|
||||
}
|
||||
|
||||
string data = parse_data_string(command_args, nullptr, ParseDataFlags::ALLOW_FILES);
|
||||
if (data.size() < 2) {
|
||||
throw runtime_error("data too short");
|
||||
}
|
||||
if (data.size() > 16) {
|
||||
throw runtime_error("data too long");
|
||||
}
|
||||
|
||||
PlayerInventoryItem item;
|
||||
item.data.id = random_object<uint32_t>();
|
||||
if (data.size() <= 12) {
|
||||
memcpy(&item.data.data1, data.data(), data.size());
|
||||
} else {
|
||||
memcpy(&item.data.data1, data.data(), 12);
|
||||
memcpy(&item.data.data2, data.data() + 12, data.size() - 12);
|
||||
}
|
||||
|
||||
if (command_name == "set-next-item") {
|
||||
session->next_drop_item = item;
|
||||
|
||||
string name = name_for_item(session->next_drop_item.data, true);
|
||||
send_text_message(session->client_channel, u"$C7Next drop:\n" + decode_sjis(name));
|
||||
|
||||
} else {
|
||||
send_drop_stacked_item(session->client_channel, item.data, session->area, session->x, session->z);
|
||||
send_drop_stacked_item(session->server_channel, item.data, session->area, session->x, session->z);
|
||||
|
||||
string name = name_for_item(item.data, true);
|
||||
send_text_message(session->client_channel, u"$C7Item created:\n" + decode_sjis(name));
|
||||
}
|
||||
|
||||
} else if (command_name == "close-idle-sessions") {
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user