wait for responses before sending chains of function calls

This commit is contained in:
Martin Michelsen
2023-06-07 00:32:46 -07:00
parent e1f584984f
commit d60404ff0a
6 changed files with 104 additions and 73 deletions
+36 -35
View File
@@ -260,27 +260,21 @@ static void proxy_command_auction(shared_ptr<ServerState>,
static void server_command_patch(shared_ptr<ServerState> s, shared_ptr<Lobby>,
shared_ptr<Client> c, const std::u16string& args) {
string basename = encode_sjis(args);
auto send_call = [s, basename, wc = weak_ptr<Client>(c)]() {
auto c = wc.lock();
if (!c) {
return;
}
try {
try {
prepare_client_for_patches(s, c, [s, wc = weak_ptr<Client>(c), basename]() {
auto c = wc.lock();
if (!c) {
return;
}
// Note: We can't look this up outside of the closure because
// c->specific_version can change during prepare_client_for_patches
auto fn = s->function_code_index->name_and_specific_version_to_patch_function.at(
string_printf("%s-%08" PRIX32, basename.c_str(), c->specific_version));
send_function_call(c, fn);
} catch (const out_of_range&) {
send_text_message(c, u"Invalid patch name");
}
};
send_cache_patch_if_needed(s, c);
if (c->version() == GameVersion::GC &&
c->specific_version == default_specific_version_for_version(GameVersion::GC, -1)) {
send_function_call(c, s->function_code_index->name_to_function.at("VersionDetect"));
c->on_version_detect_response = send_call;
} else {
send_call();
c->function_call_response_queue.emplace_back(empty_function_call_response_handler);
});
} catch (const out_of_range&) {
send_text_message(c, u"Invalid patch name");
}
}
@@ -307,26 +301,33 @@ static void proxy_command_patch(shared_ptr<ServerState> s,
}
};
// This mirrors the implementation in send_cache_patch_if_needed
auto send_version_detect_or_send_call = [s, basename, &session, send_call]() {
if (session.version == GameVersion::GC &&
session.newserv_client_config.cfg.specific_version == default_specific_version_for_version(GameVersion::GC, -1)) {
send_function_call(
session.client_channel,
session.newserv_client_config.cfg.flags,
s->function_code_index->name_to_function.at("VersionDetect"));
session.function_call_return_handler_queue.emplace_back(send_call);
} else {
send_call(session.newserv_client_config.cfg.specific_version, 0);
}
};
// This mirrors the implementation in prepare_client_for_patches
if (!(session.newserv_client_config.cfg.flags & Client::Flag::SEND_FUNCTION_CALL_NO_CACHE_PATCH)) {
send_function_call(
session.client_channel, session.newserv_client_config.cfg.flags, s->function_code_index->name_to_function.at("CacheClearFix-Phase1"), {}, "", 0, 0, 0x7F2634EC);
send_function_call(
session.client_channel, session.newserv_client_config.cfg.flags, s->function_code_index->name_to_function.at("CacheClearFix-Phase2"));
session.function_call_return_handler_queue.emplace_back(empty_patch_return_handler);
session.function_call_return_handler_queue.emplace_back(empty_patch_return_handler);
session.newserv_client_config.cfg.flags |= Client::Flag::SEND_FUNCTION_CALL_NO_CACHE_PATCH;
}
if (session.version == GameVersion::GC &&
session.newserv_client_config.cfg.specific_version == default_specific_version_for_version(GameVersion::GC, -1)) {
send_function_call(
session.client_channel,
session.newserv_client_config.cfg.flags,
s->function_code_index->name_to_function.at("VersionDetect"));
session.function_call_return_handler_queue.emplace_back(send_call);
session.client_channel, session.newserv_client_config.cfg.flags, s->function_code_index->name_to_function.at("CacheClearFix-Phase1"), {}, "", 0, 0, 0x7F2734EC);
session.function_call_return_handler_queue.emplace_back([s, session_p = &session, send_version_detect_or_send_call](uint32_t, uint32_t) -> void {
send_function_call(
session_p->client_channel, session_p->newserv_client_config.cfg.flags, s->function_code_index->name_to_function.at("CacheClearFix-Phase2"));
session_p->function_call_return_handler_queue.emplace_back([session_p, send_version_detect_or_send_call](uint32_t, uint32_t) -> void {
session_p->newserv_client_config.cfg.flags |= Client::Flag::SEND_FUNCTION_CALL_NO_CACHE_PATCH;
send_version_detect_or_send_call();
});
});
} else {
send_call(session.newserv_client_config.cfg.specific_version, 0);
send_version_detect_or_send_call();
}
}