t7x/src/client/component/party.cpp

328 lines
8.5 KiB
C++
Raw Normal View History

2022-10-28 14:34:10 -04:00
#include <std_include.hpp>
#include "loader/component_loader.hpp"
#include "game/game.hpp"
2023-03-08 16:00:41 -05:00
#include "game/utils.hpp"
2022-10-28 14:34:10 -04:00
2022-10-29 14:21:13 -04:00
#include "party.hpp"
2022-10-28 14:34:10 -04:00
#include "network.hpp"
2022-10-28 16:16:14 -04:00
#include "scheduler.hpp"
2023-03-08 16:00:41 -05:00
#include "workshop.hpp"
2022-10-28 14:34:10 -04:00
#include <utils/hook.hpp>
2023-02-12 04:10:54 -05:00
#include <utils/string.hpp>
2022-10-28 16:16:14 -04:00
#include <utils/info_string.hpp>
#include <utils/cryptography.hpp>
2022-10-29 14:21:13 -04:00
#include <utils/concurrency.hpp>
2022-10-28 16:16:14 -04:00
2023-03-08 16:00:41 -05:00
2022-10-28 14:34:10 -04:00
namespace party
{
namespace
{
2023-02-05 04:19:00 -05:00
std::atomic_bool is_connecting_to_dedi{false};
2022-10-29 14:21:13 -04:00
game::netadr_t connect_host{{}, {}, game::NA_BAD, {}};
2022-10-28 16:16:14 -04:00
2022-10-29 14:21:13 -04:00
struct server_query
2022-10-28 14:34:10 -04:00
{
2023-02-05 10:15:29 -05:00
bool sent{false};
game::netadr_t host{};
std::string challenge{};
query_callback callback{};
std::chrono::high_resolution_clock::time_point query_time{};
2022-10-29 14:21:13 -04:00
};
2022-10-28 14:34:10 -04:00
2022-10-29 14:21:13 -04:00
utils::concurrency::container<std::vector<server_query>>& get_server_queries()
{
static utils::concurrency::container<std::vector<server_query>> server_queries;
return server_queries;
2022-10-28 16:16:14 -04:00
}
2023-03-08 16:00:41 -05:00
void connect_to_lobby(const game::netadr_t& addr, const std::string& mapname, const std::string& gamemode,
const std::string& pub_id)
2022-10-28 16:16:14 -04:00
{
2023-03-08 16:00:41 -05:00
workshop::load_usermap_mod_if_needed(pub_id);
2022-10-28 14:34:10 -04:00
game::XSESSION_INFO info{};
2023-03-08 16:00:41 -05:00
game::CL_ConnectFromLobby(0, &info, &addr, 1, 0, mapname.data(), gamemode.data(), pub_id.data());
2022-10-28 16:16:14 -04:00
}
void launch_mode(const game::eModes mode)
{
2022-12-13 13:11:42 -05:00
scheduler::once([=]
2022-10-28 16:16:14 -04:00
{
2023-03-04 10:03:27 -05:00
const auto local_client = *reinterpret_cast<DWORD*>(0x14342155C_g);
2022-12-13 13:11:42 -05:00
const auto current_mode = game::Com_SessionMode_GetMode();
game::Com_SwitchMode(local_client, current_mode, mode, 6);
}, scheduler::main);
2022-10-28 16:16:14 -04:00
}
void connect_to_lobby_with_mode(const game::netadr_t& addr, const game::eModes mode, const std::string& mapname,
2023-03-08 16:00:41 -05:00
const std::string& gametype, const std::string& pub_id, const bool was_retried = false)
2022-10-28 16:16:14 -04:00
{
if (game::Com_SessionMode_IsMode(mode))
{
2023-03-08 16:00:41 -05:00
connect_to_lobby(addr, mapname, gametype, pub_id);
2022-10-28 16:16:14 -04:00
return;
}
if (!was_retried)
{
scheduler::once([=]
{
2023-03-08 16:00:41 -05:00
connect_to_lobby_with_mode(addr, mode, mapname, gametype, pub_id, true);
2022-10-28 16:16:14 -04:00
}, scheduler::main, 5s);
launch_mode(mode);
}
}
2022-10-29 14:21:13 -04:00
game::LobbyMainMode convert_mode(const game::eModes mode)
{
switch (mode)
{
case game::MODE_CAMPAIGN:
return game::LOBBY_MAINMODE_CP;
case game::MODE_MULTIPLAYER:
return game::LOBBY_MAINMODE_MP;
case game::MODE_ZOMBIES:
return game::LOBBY_MAINMODE_ZM;
default:
return game::LOBBY_MAINMODE_INVALID;
}
}
void connect_to_session(const game::netadr_t& addr, const std::string& hostname, const uint64_t xuid,
const game::eModes mode)
{
const auto LobbyJoin_Begin = reinterpret_cast<bool(*)(int actionId, game::ControllerIndex_t controllerIndex,
game::LobbyType sourceLobbyType,
2023-03-04 09:42:44 -05:00
game::LobbyType targetLobbyType)>(0x141ED94D0_g);
2022-10-29 14:21:13 -04:00
if (!LobbyJoin_Begin(0, game::CONTROLLER_INDEX_FIRST, game::LOBBY_TYPE_PRIVATE, game::LOBBY_TYPE_PRIVATE))
{
return;
}
auto& join = *game::s_join;
auto& host = join.hostList[0];
memset(&host, 0, sizeof(host));
host.info.netAdr = addr;
host.info.xuid = xuid;
2023-02-12 04:10:54 -05:00
utils::string::copy(host.info.name, hostname.data());
2022-10-29 14:21:13 -04:00
host.lobbyType = game::LOBBY_TYPE_PRIVATE;
host.lobbyParams.networkMode = game::LOBBY_NETWORKMODE_LIVE;
host.lobbyParams.mainMode = convert_mode(mode);
host.retryCount = 0;
host.retryTime = game::Sys_Milliseconds();
join.potentialHost = host;
join.hostCount = 1;
join.processedCount = 1;
join.state = game::JOIN_SOURCE_STATE_ASSOCIATING;
join.startTime = game::Sys_Milliseconds();
/*join.targetLobbyType = game::LOBBY_TYPE_PRIVATE;
join.sourceLobbyType = game::LOBBY_TYPE_PRIVATE;
join.controllerIndex = game::CONTROLLER_INDEX_FIRST;
join.joinType = game::JOIN_TYPE_NORMAL;
join.joinResult = game::JOIN_RESULT_INVALID;
join.isFinalized = false;*/
// LobbyJoinSource_Finalize
join.isFinalized = true;
}
void handle_connect_query_response(const bool success, const game::netadr_t& target,
2023-02-05 10:15:29 -05:00
const utils::info_string& info, uint32_t ping)
2022-10-29 14:21:13 -04:00
{
if (!success)
{
return;
}
is_connecting_to_dedi = info.get("dedicated") == "1";
2022-10-29 14:21:13 -04:00
const auto gamename = info.get("gamename");
if (gamename != "T7"s)
{
const auto str = "Invalid gamename.";
printf("%s\n", str);
return;
}
const auto mapname = info.get("mapname");
if (mapname.empty())
{
const auto str = "Invalid map.";
printf("%s\n", str);
return;
}
const auto gametype = info.get("gametype");
if (gametype.empty())
{
const auto str = "Invalid gametype.";
printf("%s\n", str);
return;
}
//const auto hostname = info.get("sv_hostname");
const auto playmode = info.get("playmode");
const auto mode = static_cast<game::eModes>(std::atoi(playmode.data()));
2022-10-29 14:21:13 -04:00
//const auto xuid = strtoull(info.get("xuid").data(), nullptr, 16);
scheduler::once([=]
{
2023-03-08 16:00:41 -05:00
const auto publisher_id = workshop::get_usermap_publisher_id(mapname);
if (workshop::check_valid_publisher_id(mapname, publisher_id))
{
2023-03-08 16:00:41 -05:00
if (is_connecting_to_dedi)
{
game::Com_SessionMode_SetGameMode(game::MODE_GAME_MATCHMAKING_PLAYLIST);
}
2023-03-08 16:00:41 -05:00
//connect_to_session(target, hostname, xuid, mode);
connect_to_lobby_with_mode(target, mode, mapname, gametype, publisher_id);
}
2022-10-29 14:21:13 -04:00
}, scheduler::main);
}
void connect_stub(const char* address)
{
const auto target = network::address_from_string(address);
if (target.type == game::NA_BAD)
{
return;
}
connect_host = target;
query_server(target, handle_connect_query_response);
}
2023-02-05 10:15:29 -05:00
void send_server_query(server_query& query)
{
query.sent = true;
query.query_time = std::chrono::high_resolution_clock::now();
query.challenge = utils::cryptography::random::get_challenge();
network::send(query.host, "getInfo", query.challenge);
}
game::netadr_t get_connected_server()
{
constexpr auto local_client_num = 0ull;
2023-03-04 09:42:44 -05:00
const auto address = *reinterpret_cast<uint64_t*>(0x1453D8BB8_g) + (0x25780 * local_client_num) + 0x10;
return *reinterpret_cast<game::netadr_t*>(address);
}
2022-10-29 14:21:13 -04:00
2023-02-26 04:04:38 -05:00
void handle_info_response(const game::netadr_t& target, const network::data_view& data)
2022-10-29 14:21:13 -04:00
{
2023-02-26 04:04:38 -05:00
bool found_query = false;
server_query query{};
2022-10-29 14:21:13 -04:00
2023-02-26 04:04:38 -05:00
const utils::info_string info{data};
2022-10-28 14:34:10 -04:00
2023-02-26 04:04:38 -05:00
get_server_queries().access([&](std::vector<server_query>& server_queries)
2022-10-28 14:34:10 -04:00
{
2023-02-26 04:04:38 -05:00
for (auto i = server_queries.begin(); i != server_queries.end(); ++i)
2022-10-28 16:16:14 -04:00
{
2023-02-26 04:04:38 -05:00
if (i->host == target && i->challenge == info.get("challenge"))
2022-10-29 14:21:13 -04:00
{
2023-02-26 04:04:38 -05:00
found_query = true;
query = std::move(*i);
i = server_queries.erase(i);
break;
2022-10-29 14:21:13 -04:00
}
2022-10-28 16:16:14 -04:00
}
2022-10-29 14:21:13 -04:00
});
2022-10-28 16:16:14 -04:00
2023-02-26 04:04:38 -05:00
if (found_query)
2022-10-29 14:21:13 -04:00
{
2023-02-26 04:04:38 -05:00
const auto ping = std::chrono::high_resolution_clock::now() - query.query_time;
const auto ping_ms = std::chrono::duration_cast<std::chrono::milliseconds>(ping).count();
2022-10-28 16:16:14 -04:00
2023-02-26 04:04:38 -05:00
query.callback(true, query.host, info, static_cast<uint32_t>(ping_ms));
}
}
2023-02-05 10:15:29 -05:00
2023-02-26 04:04:38 -05:00
void cleanup_queried_servers()
{
std::vector<server_query> removed_queries{};
2023-02-05 10:15:29 -05:00
2023-02-26 04:04:38 -05:00
get_server_queries().access([&](std::vector<server_query>& server_queries)
{
size_t sent_queries = 0;
2023-02-05 10:15:29 -05:00
2023-02-26 04:04:38 -05:00
const auto now = std::chrono::high_resolution_clock::now();
for (auto i = server_queries.begin(); i != server_queries.end();)
{
if (!i->sent)
{
if (++sent_queries < 10)
2022-10-29 14:21:13 -04:00
{
2023-02-26 04:04:38 -05:00
send_server_query(*i);
2022-10-29 14:21:13 -04:00
}
2022-10-28 16:16:14 -04:00
2023-02-26 04:04:38 -05:00
++i;
continue;
2022-10-29 14:21:13 -04:00
}
2022-10-28 16:16:14 -04:00
2023-02-26 04:04:38 -05:00
if ((now - i->query_time) < 2s)
{
++i;
continue;
}
removed_queries.emplace_back(std::move(*i));
i = server_queries.erase(i);
2022-10-29 14:21:13 -04:00
}
2023-02-26 04:04:38 -05:00
});
const utils::info_string empty{};
for (const auto& query : removed_queries)
{
query.callback(false, query.host, empty, 0);
}
}
}
void query_server(const game::netadr_t& host, query_callback callback)
{
server_query query{};
query.sent = false;
query.host = host;
query.callback = std::move(callback);
get_server_queries().access([&](std::vector<server_query>& server_queries)
{
server_queries.emplace_back(std::move(query));
});
}
struct component final : client_component
{
void post_unpack() override
{
2023-03-04 09:42:44 -05:00
utils::hook::jump(0x141EE5FE0_g, &connect_stub);
2023-02-26 04:04:38 -05:00
network::on("infoResponse", handle_info_response);
scheduler::loop(cleanup_queried_servers, scheduler::async, 200ms);
2023-02-05 10:15:29 -05:00
}
void pre_destroy() override
{
get_server_queries().access([](std::vector<server_query>& s)
{
s = {};
});
2022-10-28 14:34:10 -04:00
}
};
}
REGISTER_COMPONENT(party::component)