server: 0009-setup-pairs

TICKET-258: ygol.<key>=<value> tokens after slot 11 go into the duel registry before special.lua, into the replay pairs block and to every joiner as STOC 0x43; argv sized from the token count.

Replayed from Tools/YGOLegacy/patches/0009-setup-pairs.patch in the Unity repo.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
donnyandClaude Fable 5.1 committed 2026-10-14 20:58:57 -07:00
1 parent 00a715f181
commit 76e359f6cf
7 files changed
+94 -8

No files matched your search

+15 -5
View File
@@ -144,6 +144,7 @@ int main(int argc, char* argv[]) {
ygo::game_info.time_limit = 180;
std::memset(ygo::pre_seed, 0, sizeof(ygo::pre_seed));
std::memset(ygo::pre_seed_specified, 0, sizeof(ygo::pre_seed_specified));
ygo::setup_pairs.clear(); // YGOL setup pairs
if (argc == 2) {
int code = atoi(argv[1]);
ygo::mainGame = &_game;
@@ -186,28 +187,37 @@ int main(int argc, char* argv[]) {
ygo::game_info.draw_count = atoi(argv[10]);
ygo::game_info.time_limit = atoi(argv[11]);
ygo::replay_mode = atoi(argv[12]);
for (int i = 13; (i < argc && i < (13 + MAX_MATCH_COUNT)) ; ++i)
// YGOL setup pairs: ygol.* tokens never reach the seeds; the n-th other token is seed n, as before
for (int i = 13, seed = 0; i < argc; ++i)
{
if (ygo::IsSetupPairToken(argv[i])) {
if (!ygo::AddSetupPair(argv[i]))
std::fprintf(stderr, "Dropped setup pair: %s\n", argv[i]);
continue;
}
if (seed >= MAX_MATCH_COUNT)
continue;
auto ok = Base64::Decode(
reinterpret_cast<const unsigned char*>(argv[i]),
strlen(argv[i]),
reinterpret_cast<unsigned char*>(ygo::pre_seed[i - 13]),
reinterpret_cast<unsigned char*>(ygo::pre_seed[seed]),
SEED_COUNT * sizeof(uint32_t)
);
if(ok) {
// check if it isn't all zero
bool all_zero = true;
for (int j = 0; j < SEED_COUNT; ++j) {
if (ygo::pre_seed[i - 13][j] != 0) {
if (ygo::pre_seed[seed][j] != 0) {
all_zero = false;
break;
}
}
if (!all_zero)
ygo::pre_seed_specified[i - 13] = 1;
ygo::pre_seed_specified[seed] = 1;
}
else
std::fprintf(stderr, "Failed to decode seed %d: %s\n", i - 13, argv[i]);
std::fprintf(stderr, "Failed to decode seed %d: %s\n", seed, argv[i]);
++seed;
}
}
ygo::mainGame = &_game;
+32
View File
@@ -557,5 +557,37 @@ size_t NetServer::CreateStampedPacket(unsigned char* src, int src_size, unsigned
std::memcpy(pdst, src, src_size);
return sizeof(dst_player_type) + src_size;
}
// YGOL setup pairs
std::vector<std::string> setup_pairs;
bool IsSetupPairToken(const char* token) {
return std::strncmp(token, "ygol.", 5) == 0;
}
bool AddSetupPair(const char* token) {
const auto len = std::strlen(token);
const char* eq = std::strchr(token, '=');
if(!eq || eq - token <= 5 || len > SETUP_PAIR_MAX_LEN || setup_pairs.size() >= SETUP_PAIRS_MAX)
return false;
setup_pairs.emplace_back(token, len);
return true;
}
size_t WriteSetupPairsBlock(unsigned char* dst) {
auto pdst = dst;
BufferIO::Write<uint16_t>(pdst, (uint16_t)setup_pairs.size());
for(const auto& pair : setup_pairs) {
BufferIO::Write<uint16_t>(pdst, (uint16_t)pair.size());
std::memcpy(pdst, pair.data(), pair.size());
pdst += pair.size();
}
return pdst - dst;
}
void NetServer::SendSetupPairs(DuelPlayer* dp) {
if(setup_pairs.empty())
return;
unsigned char buf[sizeof(uint16_t) + SETUP_PAIRS_BLOCK_MAX];
auto pbuf = buf;
BufferIO::Write<uint16_t>(pbuf, FEATURE_SENDER_SERVER);
const auto size = sizeof(uint16_t) + WriteSetupPairsBlock(pbuf);
SendBufferToPlayer(dp, STOC_SETUP_PAIRS, buf, size);
}
}
+1
View File
@@ -43,6 +43,7 @@ public:
static void HandleCTOSPacket(DuelPlayer* dp, unsigned char* data, size_t len);
static size_t CreateChatPacket(unsigned char* src, int src_size, unsigned char* dst, uint16_t dst_player_type);
static size_t CreateStampedPacket(unsigned char* src, int src_size, unsigned char* dst, uint16_t dst_player_type); // YGOL feature messages
static void SendSetupPairs(DuelPlayer* dp); // YGOL setup pairs
static inline bool ShouldHideFacedownCode(uint8_t position) {
return (position & POS_FACEDOWN) != 0 && (position & POS_REVEAL) == 0;
}
+13
View File
@@ -4,6 +4,8 @@
#include <cstdint>
#include <cstring>
#include <type_traits>
#include <string>
#include <vector>
struct bufferevent;
struct event;
@@ -226,6 +228,16 @@ constexpr uint16_t FEATURE_PROTOCOL = 1;
constexpr uint32_t FEATURE_APPEARANCE = 0x1;
constexpr uint32_t FEATURE_DIALOGUE = 0x2;
constexpr uint16_t FEATURE_SENDER_SERVER = 0xff;
// YGOL setup pairs: ordered ygol.<key>=<value> tokens after slot 11, never interpreted
constexpr uint32_t FEATURE_SETUP_PAIRS = 0x4;
constexpr size_t SETUP_PAIRS_MAX = 64;
constexpr size_t SETUP_PAIR_MAX_LEN = 512;
constexpr size_t SETUP_PAIRS_BLOCK_MAX = sizeof(uint16_t) + SETUP_PAIRS_MAX * (sizeof(uint16_t) + SETUP_PAIR_MAX_LEN);
constexpr uint32_t REPLAY_YGOL_SETUP_PAIRS = 0x40000000; // replay flag: uint32_t length + the block, before the names
extern std::vector<std::string> setup_pairs;
bool IsSetupPairToken(const char* token);
bool AddSetupPair(const char* token);
size_t WriteSetupPairsBlock(unsigned char* dst);
class DuelMode;
@@ -357,6 +369,7 @@ inline bool IsFeatureMessage(unsigned char pktType) {
#define STOC_CAPABILITY 0x40 // uint16_t + CTOS_Capability
#define STOC_APPEARANCE 0x41 // uint16_t + CTOS_Appearance
#define STOC_DIALOGUE 0x42 // uint16_t + CTOS_Dialogue
#define STOC_SETUP_PAIRS 0x43 // uint16_t (0xff) + uint16_t count, then per pair uint16_t length + "key=value"
// STOC_GAME_MSG header
#define MSG_WAITING 3
+5 -1
View File
@@ -11,7 +11,11 @@
namespace ygo {
YGOSERVER_API int start_server(const char* args) {
int argc = 1;
char** argv = new char* [13];
size_t tokens = 0; // YGOL setup pairs: argv sized from the token count, not a fixed 12
for (size_t k = 0; args[k]; ++k)
if (args[k] != ' ' && (k == 0 || args[k - 1] == ' '))
++tokens;
char** argv = new char* [tokens + 1];
const char* server_name = "ygoserver";
argv[0] = new char[strlen(server_name) + 1];
strcpy(argv[0], server_name);
+14 -1
View File
@@ -68,7 +68,7 @@ void SingleDuel::Dialogue(DuelPlayer* dp, unsigned char* pdata, int len) {
void SingleDuel::SendServerCapability(DuelPlayer* dp) {
CTOS_Capability cap;
cap.protocol = FEATURE_PROTOCOL;
cap.flags = FEATURE_APPEARANCE | FEATURE_DIALOGUE;
cap.flags = FEATURE_APPEARANCE | FEATURE_DIALOGUE | FEATURE_SETUP_PAIRS; // YGOL setup pairs
unsigned char buf[SIZE_STOC_FEATURE];
const auto size = NetServer::CreateStampedPacket((unsigned char*)&cap, sizeof cap, buf, FEATURE_SENDER_SERVER);
NetServer::SendBufferToPlayer(dp, STOC_CAPABILITY, buf, size);
@@ -199,6 +199,7 @@ void SingleDuel::JoinGame(DuelPlayer* dp, unsigned char* pdata, bool is_creater)
}
NetServer::SendPacketToPlayer(dp, STOC_JOIN_GAME, scjg);
SendServerCapability(dp); // YGOL feature messages: the host build speaks first
NetServer::SendSetupPairs(dp); // YGOL setup pairs
NetServer::SendPacketToPlayer(dp, STOC_TYPE_CHANGE, sctc);
if(players[0]) {
STOC_HS_PlayerEnter scpe;
@@ -645,8 +646,16 @@ void SingleDuel::TPResult(DuelPlayer* dp, unsigned char tp) {
for (auto& x : rh.seed_sequence)
x = rd();
mtrandom rnd(rh.seed_sequence, SEED_COUNT);
if(!setup_pairs.empty()) // YGOL setup pairs
rh.base.flag |= REPLAY_YGOL_SETUP_PAIRS;
last_replay.BeginRecord();
last_replay.WriteHeader(rh);
if(!setup_pairs.empty()) {
unsigned char block[SETUP_PAIRS_BLOCK_MAX];
const auto size = WriteSetupPairsBlock(block);
last_replay.WriteInt32((int32_t)size, false);
last_replay.WriteData(block, size, false);
}
last_replay.WriteData(players[0]->name, 40, false);
last_replay.WriteData(players[1]->name, 40, false);
if(!host_info.no_shuffle_deck) {
@@ -682,6 +691,10 @@ void SingleDuel::TPResult(DuelPlayer* dp, unsigned char tp) {
}
set_registry_value(pduel, "player_type_0", swapped ? "1" : "0");
set_registry_value(pduel, "player_type_1", swapped ? "0" : "1");
for(const auto& pair : setup_pairs) { // YGOL setup pairs: before special.lua, every duel of a series
const auto eq = pair.find('=');
set_registry_value(pduel, pair.substr(0, eq).c_str(), pair.c_str() + eq + 1);
}
set_player_info(pduel, 0, host_info.start_lp, host_info.start_hand, host_info.draw_count);
set_player_info(pduel, 1, host_info.start_lp, host_info.start_hand, host_info.draw_count);
preload_script(pduel, "./script/patches/entry.lua");
+14 -1
View File
@@ -66,7 +66,7 @@ void TagDuel::Dialogue(DuelPlayer* dp, unsigned char* pdata, int len) {
void TagDuel::SendServerCapability(DuelPlayer* dp) {
CTOS_Capability cap;
cap.protocol = FEATURE_PROTOCOL;
cap.flags = FEATURE_APPEARANCE | FEATURE_DIALOGUE;
cap.flags = FEATURE_APPEARANCE | FEATURE_DIALOGUE | FEATURE_SETUP_PAIRS; // YGOL setup pairs
unsigned char buf[SIZE_STOC_FEATURE];
const auto size = NetServer::CreateStampedPacket((unsigned char*)&cap, sizeof cap, buf, FEATURE_SENDER_SERVER);
NetServer::SendBufferToPlayer(dp, STOC_CAPABILITY, buf, size);
@@ -191,6 +191,7 @@ void TagDuel::JoinGame(DuelPlayer* dp, unsigned char* pdata, bool is_creater) {
}
NetServer::SendPacketToPlayer(dp, STOC_JOIN_GAME, scjg);
SendServerCapability(dp); // YGOL feature messages: the host build speaks first
NetServer::SendSetupPairs(dp); // YGOL setup pairs
NetServer::SendPacketToPlayer(dp, STOC_TYPE_CHANGE, sctc);
for(int i = 0; i < 4; ++i)
if(players[i]) {
@@ -615,8 +616,16 @@ void TagDuel::TPResult(DuelPlayer* dp, unsigned char tp) {
for (auto& x : rh.seed_sequence)
x = rd();
mtrandom rnd(rh.seed_sequence, SEED_COUNT);
if(!setup_pairs.empty()) // YGOL setup pairs
rh.base.flag |= REPLAY_YGOL_SETUP_PAIRS;
last_replay.BeginRecord();
last_replay.WriteHeader(rh);
if(!setup_pairs.empty()) {
unsigned char block[SETUP_PAIRS_BLOCK_MAX];
const auto size = WriteSetupPairsBlock(block);
last_replay.WriteInt32((int32_t)size, false);
last_replay.WriteData(block, size, false);
}
last_replay.WriteData(players[0]->name, 40, false);
last_replay.WriteData(players[1]->name, 40, false);
last_replay.WriteData(players[2]->name, 40, false);
@@ -653,6 +662,10 @@ void TagDuel::TPResult(DuelPlayer* dp, unsigned char tp) {
}
set_registry_value(pduel, "player_type_0", swapped ? "1" : "0");
set_registry_value(pduel, "player_type_1", swapped ? "0" : "1");
for(const auto& pair : setup_pairs) { // YGOL setup pairs: before special.lua, every duel of a series
const auto eq = pair.find('=');
set_registry_value(pduel, pair.substr(0, eq).c_str(), pair.c_str() + eq + 1);
}
set_player_info(pduel, 0, host_info.start_lp, host_info.start_hand, host_info.draw_count);
set_player_info(pduel, 1, host_info.start_lp, host_info.start_hand, host_info.draw_count);
preload_script(pduel, "./script/patches/entry.lua");