iw4x-client/src/Components/Modules/Playlist.cpp

185 lines
5.5 KiB
C++
Raw Normal View History

2016-07-11 11:14:58 -04:00
#include "STDInclude.hpp"
namespace Components
{
std::string Playlist::CurrentPlaylistBuffer;
std::string Playlist::ReceivedPlaylistBuffer;
2016-11-02 13:44:20 -04:00
std::map<const void*, std::string> Playlist::MapRelocation;
2016-07-11 11:14:58 -04:00
void Playlist::LoadPlaylist()
{
// Check if playlist already loaded
if (Utils::Hook::Get<bool>(0x1AD3680)) return;
// Don't load playlists when dedi and no party
if (Dedicated::IsEnabled() && !Dvar::Var("party_enable").Get<bool>())
2016-07-11 11:14:58 -04:00
{
Utils::Hook::Set<bool>(0x1AD3680, true); // Set received to true
Dvar::Var("xblive_privateserver").Set(true);
return;
}
Dvar::Var("xblive_privateserver").Set(false);
std::string playlistFilename = Dvar::Var("playlistFilename").Get<char*>();
FileSystem::File playlist(playlistFilename);
if (playlist.Exists())
{
Logger::Print("Parsing playlist '%s'...\n", playlist.GetName().data());
2016-11-02 13:44:20 -04:00
Game::Playlist_ParsePlaylists(playlist.GetBuffer().data());
2016-07-11 11:14:58 -04:00
Utils::Hook::Set<bool>(0x1AD3680, true); // Playlist loaded
}
else
{
Logger::Print("Unable to load playlist '%s'!\n", playlist.GetName().data());
}
}
DWORD Playlist::StorePlaylistStub(const char** buffer)
{
2016-11-02 13:44:20 -04:00
Playlist::MapRelocation.clear();
2016-07-11 11:14:58 -04:00
Playlist::CurrentPlaylistBuffer = *buffer;
return Utils::Hook::Call<DWORD(const char**)>(0x4C0350)(buffer);
}
void Playlist::PlaylistRequest(Network::Address address, std::string data)
{
Logger::Print("Received playlist request, sending currently stored buffer.\n");
std::string compressedList = Utils::Compression::ZLib::Compress(Playlist::CurrentPlaylistBuffer);
Proto::Party::Playlist list;
list.set_hash(Utils::Cryptography::JenkinsOneAtATime::Compute(compressedList));
list.set_buffer(compressedList);
Network::SendCommand(address, "playlistResponse", list.SerializeAsString());
}
void Playlist::PlaylistReponse(Network::Address address, std::string data)
{
if (Party::PlaylistAwaiting())
{
if (address == Party::Target())
{
Proto::Party::Playlist list;
if (!list.ParseFromString(data))
{
Party::PlaylistError(fmt::sprintf("Received playlist response from %s, but it is invalid.", address.GetCString()));
Playlist::ReceivedPlaylistBuffer.clear();
return;
}
else
{
// Generate buffer and hash
std::string compressedData(list.buffer());
unsigned int hash = Utils::Cryptography::JenkinsOneAtATime::Compute(compressedData);
//Validate hashes
if (hash != list.hash())
{
Party::PlaylistError(fmt::sprintf("Received playlist response from %s, but the checksum did not match (%X != %X).", address.GetCString(), list.hash(), hash));
Playlist::ReceivedPlaylistBuffer.clear();
return;
}
// Decompress buffer
Playlist::ReceivedPlaylistBuffer = Utils::Compression::ZLib::Decompress(compressedData);
// Load and continue connection
Logger::Print("Received playlist, loading and continuing connection...\n");
2016-11-02 13:44:20 -04:00
Game::Playlist_ParsePlaylists(Playlist::ReceivedPlaylistBuffer.data());
2016-07-11 11:14:58 -04:00
Party::PlaylistContinue();
}
}
else
{
Logger::Print("Received playlist from someone else than our target host, ignoring it.\n");
}
}
else
{
Logger::Print("Received stray playlist response, ignoring it.\n");
}
}
2016-11-02 13:44:20 -04:00
void Playlist::MapNameCopy(char *dest, const char *src, int destsize)
{
Utils::Hook::Call<void(char*, const char*, int)>(0x4D6F80)(dest, src, destsize);
Playlist::MapRelocation[dest] = src;
}
void Playlist::SetMapName(const char* cvar, const char* value)
{
auto i = Playlist::MapRelocation.find(value);
if (i != Playlist::MapRelocation.end())
{
value = i->second.data();
}
Game::Dvar_SetStringByName(cvar, value);
}
2016-11-02 14:19:09 -04:00
int Playlist::GetMapIndex(const char* mapname)
{
auto i = Playlist::MapRelocation.find(mapname);
if (i != Playlist::MapRelocation.end())
{
mapname = i->second.data();
}
return Game::Live_GetMapIndex(mapname);
}
2016-07-11 11:14:58 -04:00
Playlist::Playlist()
{
// Default playlists
Utils::Hook::Set<char*>(0x60B06E, "playlists_default.info");
// disable playlist download function
Utils::Hook::Set<BYTE>(0x4D4790, 0xC3);
// Load playlist, but don't delete it
Utils::Hook::Nop(0x4D6EBB, 5);
Utils::Hook::Nop(0x4D6E67, 5);
Utils::Hook::Nop(0x4D6E71, 2);
// playlist dvar 'validity check'
Utils::Hook::Set<BYTE>(0x4B1170, 0xC3);
// disable playlist checking
Utils::Hook::Set<BYTE>(0x5B69E9, 0xEB); // too new
Utils::Hook::Set<BYTE>(0x5B696E, 0xEB); // too old
//Got playlists is true
//Utils::Hook::Set<bool>(0x1AD3680, true);
2016-11-02 14:19:09 -04:00
Utils::Hook(0x497DB5, Playlist::GetMapIndex, HOOK_CALL).Install()->Quick();
2016-11-02 13:44:20 -04:00
Utils::Hook(0x42A19D, Playlist::MapNameCopy, HOOK_CALL).Install()->Quick();
Utils::Hook(0x4A6FEE, Playlist::SetMapName, HOOK_CALL).Install()->Quick();
2016-07-11 11:14:58 -04:00
// Store playlist buffer on load
Utils::Hook(0x42961C, Playlist::StorePlaylistStub, HOOK_CALL).Install()->Quick();
//if (Dedicated::IsDedicated())
{
// Custom playlist loading
Utils::Hook(0x420B5A, Playlist::LoadPlaylist, HOOK_JUMP).Install()->Quick();
// disable playlist.ff loading function
Utils::Hook::Set<BYTE>(0x4D6E60, 0xC3);
}
Network::Handle("getPlaylist", PlaylistRequest);
Network::Handle("playlistResponse", PlaylistReponse);
}
Playlist::~Playlist()
{
2016-11-02 13:44:20 -04:00
Playlist::MapRelocation.clear();
2016-07-11 11:14:58 -04:00
Playlist::CurrentPlaylistBuffer.clear();
Playlist::ReceivedPlaylistBuffer.clear();
}
}