Merge pull request #218 from diamante0018/clean-up-renderer

Avoid some bad dvar look ups per frame
This commit is contained in:
Dss0 2022-04-12 23:43:24 +02:00 committed by GitHub
commit 5e62e43438
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 85 additions and 42 deletions

View File

@ -11,6 +11,10 @@ namespace Components
std::recursive_mutex Friends::Mutex; std::recursive_mutex Friends::Mutex;
std::vector<Friends::Friend> Friends::FriendsList; std::vector<Friends::Friend> Friends::FriendsList;
Dvar::Var Friends::UIStreamFriendly;
Dvar::Var Friends::CLAnonymous;
Dvar::Var Friends::CLNotifyFriendState;
void Friends::SortIndividualList(std::vector<Friends::Friend>* list) void Friends::SortIndividualList(std::vector<Friends::Friend>* list)
{ {
std::stable_sort(list->begin(), list->end(), [](Friends::Friend const& friend1, Friends::Friend const& friend2) std::stable_sort(list->begin(), list->end(), [](Friends::Friend const& friend1, Friends::Friend const& friend2)
@ -111,8 +115,8 @@ namespace Components
Friends::SortList(); Friends::SortList();
const auto notify = Dvar::Var("cl_notifyFriendState").get<bool>(); const auto notify = Friends::CLNotifyFriendState.get<bool>();
if (gotOnline && (!notify || (notify && !Game::CL_IsCgameInitialized())) && !Dvar::Var("ui_streamFriendly").get<bool>()) if (gotOnline && (!notify || (notify && !Game::CL_IsCgameInitialized())) && !Friends::UIStreamFriendly.get<bool>())
{ {
Game::Material* material = Friends::CreateAvatar(user); Game::Material* material = Friends::CreateAvatar(user);
Toast::Show(material, entry->name, "is playing IW4x", 3000, [material]() Toast::Show(material, entry->name, "is playing IW4x", 3000, [material]()
@ -124,7 +128,7 @@ namespace Components
void Friends::UpdateState(bool force) void Friends::UpdateState(bool force)
{ {
if (Dvar::Var("cl_anonymous").get<bool>() || Friends::IsInvisible() || !Steam::Enabled()) return; if (Friends::CLAnonymous.get<bool>() || Friends::IsInvisible() || !Steam::Enabled()) return;
if (force) if (force)
{ {
@ -228,7 +232,7 @@ namespace Components
void Friends::SetPresence(const std::string& key, const std::string& value) void Friends::SetPresence(const std::string& key, const std::string& value)
{ {
if (Steam::Proxy::ClientFriends && Steam::Proxy::SteamUtils && !Dvar::Var("cl_anonymous").get<bool>() && !Friends::IsInvisible() && Steam::Enabled()) if (Steam::Proxy::ClientFriends && Steam::Proxy::SteamUtils && !Friends::CLAnonymous.get<bool>() && !Friends::IsInvisible() && Steam::Enabled())
{ {
Friends::SetRawPresence(key.data(), value.data()); Friends::SetRawPresence(key.data(), value.data());
} }
@ -576,10 +580,15 @@ namespace Components
{ {
Friends::LoggedOn = false; Friends::LoggedOn = false;
if (Dedicated::IsEnabled() || ZoneBuilder::IsEnabled() || Monitor::IsEnabled()) return; if (Dedicated::IsEnabled() || ZoneBuilder::IsEnabled() || Monitor::IsEnabled())
return;
Dvar::Register<bool>("cl_anonymous", false, Game::DVAR_ARCHIVE, "Enable invisible mode for Steam"); Dvar::OnInit([]
Dvar::Register<bool>("cl_notifyFriendState", true, Game::DVAR_ARCHIVE, "Update friends about current game status"); {
Friends::UIStreamFriendly = Dvar::Register<bool>("ui_streamFriendly", false, Game::DVAR_ARCHIVE, "Stream friendly UI");
Friends::CLAnonymous = Dvar::Register<bool>("cl_anonymous", false, Game::DVAR_ARCHIVE, "Enable invisible mode for Steam");
Friends::CLNotifyFriendState = Dvar::Register<bool>("cl_notifyFriendState", true, Game::DVAR_ARCHIVE, "Update friends about current game status");
});
Command::Add("addFriend", [](Command::Params* params) Command::Add("addFriend", [](Command::Params* params)
{ {
@ -712,11 +721,11 @@ namespace Components
Friends::InitialState = Steam::Proxy::SteamFriends->GetFriendPersonaState(Steam::Proxy::SteamUser_->GetSteamID()); Friends::InitialState = Steam::Proxy::SteamFriends->GetFriendPersonaState(Steam::Proxy::SteamUser_->GetSteamID());
} }
if (Dvar::Var("cl_anonymous").get<bool>() || Friends::IsInvisible() || !Steam::Enabled()) if (Friends::CLAnonymous.get<bool>() || Friends::IsInvisible() || !Steam::Enabled())
{ {
if (Steam::Proxy::ClientFriends) if (Steam::Proxy::ClientFriends)
{ {
for (auto id : Friends::GetAppIdList()) for (const auto id : Friends::GetAppIdList())
{ {
Steam::Proxy::ClientFriends.invoke<void>("ClearRichPresence", id); Steam::Proxy::ClientFriends.invoke<void>("ClearRichPresence", id);
} }

View File

@ -25,6 +25,10 @@ namespace Components
static bool IsInvisible(); static bool IsInvisible();
static Dvar::Var UIStreamFriendly;
static Dvar::Var CLAnonymous;
static Dvar::Var CLNotifyFriendState;
private: private:
#pragma pack(push, 4) #pragma pack(push, 4)
struct FriendRichPresenceUpdate struct FriendRichPresenceUpdate

View File

@ -858,9 +858,6 @@ namespace Components
}); });
#endif #endif
// Dvars
Dvar::Register<bool>("ui_streamFriendly", false, Game::DVAR_ARCHIVE, "Stream friendly UI");
// Debug patches // Debug patches
#ifdef DEBUG #ifdef DEBUG
// ui_debugMode 1 // ui_debugMode 1

View File

@ -18,7 +18,6 @@ namespace Components
static void OnDeviceRecoveryEnd(Utils::Slot<Scheduler::Callback> callback); static void OnDeviceRecoveryEnd(Utils::Slot<Scheduler::Callback> callback);
static void OnDeviceRecoveryBegin(Utils::Slot<Scheduler::Callback> callback); static void OnDeviceRecoveryBegin(Utils::Slot<Scheduler::Callback> callback);
private: private:
static void FrameStub(); static void FrameStub();

View File

@ -4,6 +4,9 @@ namespace Components
{ {
ServerInfo::Container ServerInfo::PlayerContainer; ServerInfo::Container ServerInfo::PlayerContainer;
Game::dvar_t** ServerInfo::CGScoreboardHeight;
Game::dvar_t** ServerInfo::CGScoreboardWidth;
unsigned int ServerInfo::GetPlayerCount() unsigned int ServerInfo::GetPlayerCount()
{ {
return ServerInfo::PlayerContainer.playerList.size(); return ServerInfo::PlayerContainer.playerList.size();
@ -74,22 +77,24 @@ namespace Components
void ServerInfo::DrawScoreboardInfo(int localClientNum) void ServerInfo::DrawScoreboardInfo(int localClientNum)
{ {
Game::Font_s* font = Game::R_RegisterFont("fonts/bigfont", 0); Game::Font_s* font = Game::R_RegisterFont("fonts/bigfont", 0);
void* cxt = Game::ScrPlace_GetActivePlacement(localClientNum); const auto* cxt = Game::ScrPlace_GetActivePlacement(localClientNum);
std::string addressText = Network::Address(*Game::connectedHost).getString(); auto addressText = Network::Address(*Game::connectedHost).getString();
if (addressText == "0.0.0.0:0" || addressText == "loopback") addressText = "Listen Server";
// get x positions if (addressText == "0.0.0.0:0" || addressText == "loopback")
float fontSize = 0.35f; addressText = "Listen Server";
float y = (480.0f - Dvar::Var("cg_scoreboardHeight").get<float>()) * 0.5f;
y += Dvar::Var("cg_scoreboardHeight").get<float>() + 6.0f;
float x = 320.0f - Dvar::Var("cg_scoreboardWidth").get<float>() * 0.5f; // Get x positions
float x2 = 320.0f + Dvar::Var("cg_scoreboardWidth").get<float>() * 0.5f; auto y = (480.0f - (*ServerInfo::CGScoreboardHeight)->current.value) * 0.5f;
y += (*ServerInfo::CGScoreboardHeight)->current.value + 6.0f;
// draw only when stream friendly ui is not enabled const auto x = 320.0f - (*ServerInfo::CGScoreboardWidth)->current.value * 0.5f;
if (!Dvar::Var("ui_streamFriendly").get<bool>()) const auto x2 = 320.0f + (*ServerInfo::CGScoreboardWidth)->current.value * 0.5f;
// Draw only when stream friendly ui is not enabled
if (!Friends::UIStreamFriendly.get<bool>())
{ {
constexpr auto fontSize = 0.35f;
Game::UI_DrawText(cxt, reinterpret_cast<const char*>(0x7ED3F8), 0x7FFFFFFF, font, x, y, 0, 0, fontSize, reinterpret_cast<float*>(0x747F34), 3); Game::UI_DrawText(cxt, reinterpret_cast<const char*>(0x7ED3F8), 0x7FFFFFFF, font, x, y, 0, 0, fontSize, reinterpret_cast<float*>(0x747F34), 3);
Game::UI_DrawText(cxt, addressText.data(), 0x7FFFFFFF, font, x2 - Game::UI_TextWidth(addressText.data(), 0, font, fontSize), y, 0, 0, fontSize, reinterpret_cast<float*>(0x747F34), 3); Game::UI_DrawText(cxt, addressText.data(), 0x7FFFFFFF, font, x2 - Game::UI_TextWidth(addressText.data(), 0, font, fontSize), y, 0, 0, fontSize, reinterpret_cast<float*>(0x747F34), 3);
} }
@ -172,7 +177,9 @@ namespace Components
ServerInfo::ServerInfo() ServerInfo::ServerInfo()
{ {
ServerInfo::PlayerContainer.currentPlayer = 0; ServerInfo::PlayerContainer.currentPlayer = 0;
ServerInfo::PlayerContainer.playerList.clear();
ServerInfo::CGScoreboardHeight = reinterpret_cast<Game::dvar_t**>(0x9FD070);
ServerInfo::CGScoreboardWidth = reinterpret_cast<Game::dvar_t**>(0x9FD0AC);
// Draw IP and hostname on the scoreboard // Draw IP and hostname on the scoreboard
Utils::Hook(0x4FC6EA, ServerInfo::DrawScoreboardStub, HOOK_CALL).install()->quick(); Utils::Hook(0x4FC6EA, ServerInfo::DrawScoreboardStub, HOOK_CALL).install()->quick();

View File

@ -28,6 +28,9 @@ namespace Components
Network::Address target; Network::Address target;
}; };
static Game::dvar_t** CGScoreboardHeight;
static Game::dvar_t** CGScoreboardWidth;
static Container PlayerContainer; static Container PlayerContainer;
static void ServerStatus(UIScript::Token); static void ServerStatus(UIScript::Token);

View File

@ -14,6 +14,11 @@ namespace Components
std::vector<unsigned int> ServerList::VisibleList; std::vector<unsigned int> ServerList::VisibleList;
Dvar::Var ServerList::UIServerSelected;
Dvar::Var ServerList::UIServerSelectedMap;
Dvar::Var ServerList::NETServerQueryLimit;
Dvar::Var ServerList::NETServerFrames;
std::vector<ServerList::ServerInfo>* ServerList::GetList() std::vector<ServerList::ServerInfo>* ServerList::GetList()
{ {
if (ServerList::IsOnlineList()) if (ServerList::IsOnlineList())
@ -154,13 +159,13 @@ namespace Components
if (info) if (info)
{ {
Dvar::Var("ui_serverSelected").set(true); ServerList::UIServerSelected.set(true);
Dvar::Var("ui_serverSelectedMap").set(info->mapname); ServerList::UIServerSelectedMap.set(info->mapname);
Dvar::Var("ui_serverSelectedGametype").set(info->gametype); Dvar::Var("ui_serverSelectedGametype").set(info->gametype);
} }
else else
{ {
Dvar::Var("ui_serverSelected").set(false); ServerList::UIServerSelected.set(false);
} }
} }
@ -621,8 +626,11 @@ namespace Components
void ServerList::Frame() void ServerList::Frame()
{ {
static Utils::Time::Interval frameLimit; static Utils::Time::Interval frameLimit;
int interval = static_cast<int>(1000.0f / Dvar::Var("net_serverFrames").get<int>()); const auto interval = static_cast<int>(1000.0f / ServerList::NETServerFrames.get<int>());
if (!frameLimit.elapsed(std::chrono::milliseconds(interval))) return;
if (!frameLimit.elapsed(std::chrono::milliseconds(interval)))
return;
frameLimit.update(); frameLimit.update();
std::lock_guard<std::recursive_mutex> _(ServerList::RefreshContainer.mutex); std::lock_guard<std::recursive_mutex> _(ServerList::RefreshContainer.mutex);
@ -638,7 +646,7 @@ namespace Components
} }
} }
int requestLimit = Dvar::Var("net_serverQueryLimit").get<int>(); auto requestLimit = ServerList::NETServerQueryLimit.get<int>();
for (unsigned int i = 0; i < ServerList::RefreshContainer.servers.size() && requestLimit > 0; ++i) for (unsigned int i = 0; i < ServerList::RefreshContainer.servers.size() && requestLimit > 0; ++i)
{ {
ServerList::Container::ServerContainer* server = &ServerList::RefreshContainer.servers[i]; ServerList::Container::ServerContainer* server = &ServerList::RefreshContainer.servers[i];
@ -734,11 +742,15 @@ namespace Components
Dvar::OnInit([]() Dvar::OnInit([]()
{ {
Dvar::Register<bool>("ui_serverSelected", false, Game::dvar_flag::DVAR_NONE, "Whether a server has been selected in the serverlist"); ServerList::UIServerSelected = Dvar::Register<bool>("ui_serverSelected", false,
Dvar::Register<const char*>("ui_serverSelectedMap", "mp_afghan", Game::dvar_flag::DVAR_NONE, "Map of the selected server"); Game::dvar_flag::DVAR_NONE, "Whether a server has been selected in the serverlist");
ServerList::UIServerSelectedMap = Dvar::Register<const char*>("ui_serverSelectedMap", "mp_afghan",
Game::dvar_flag::DVAR_NONE, "Map of the selected server");
Dvar::Register<int>("net_serverQueryLimit", 1, 1, 10, Dedicated::IsEnabled() ? Game::dvar_flag::DVAR_NONE : Game::dvar_flag::DVAR_ARCHIVE, "Amount of server queries per frame"); ServerList::NETServerQueryLimit = Dvar::Register<int>("net_serverQueryLimit", 1,
Dvar::Register<int>("net_serverFrames", 30, 1, 60, Dedicated::IsEnabled() ? Game::dvar_flag::DVAR_NONE : Game::dvar_flag::DVAR_ARCHIVE, "Amount of server query frames per second"); 1, 10, Dedicated::IsEnabled() ? Game::dvar_flag::DVAR_NONE : Game::dvar_flag::DVAR_ARCHIVE, "Amount of server queries per frame");
ServerList::NETServerFrames = Dvar::Register<int>("net_serverFrames", 30,
1, 60, Dedicated::IsEnabled() ? Game::dvar_flag::DVAR_NONE : Game::dvar_flag::DVAR_ARCHIVE, "Amount of server query frames per second");
}); });
// Fix ui_netsource dvar // Fix ui_netsource dvar
@ -794,6 +806,7 @@ namespace Components
UIScript::Add("RefreshFilter", ServerList::UpdateVisibleList); UIScript::Add("RefreshFilter", ServerList::UpdateVisibleList);
UIScript::Add("RefreshServers", ServerList::Refresh); UIScript::Add("RefreshServers", ServerList::Refresh);
UIScript::Add("JoinServer", [](UIScript::Token) UIScript::Add("JoinServer", [](UIScript::Token)
{ {
ServerList::ServerInfo* info = ServerList::GetServer(ServerList::CurrentServer); ServerList::ServerInfo* info = ServerList::GetServer(ServerList::CurrentServer);
@ -803,6 +816,7 @@ namespace Components
Party::Connect(info->addr); Party::Connect(info->addr);
} }
}); });
UIScript::Add("ServerSort", [](UIScript::Token token) UIScript::Add("ServerSort", [](UIScript::Token token)
{ {
int key = token.get<int>(); int key = token.get<int>();
@ -820,6 +834,7 @@ namespace Components
Logger::Print("Sorting server list by token: %d\n", ServerList::SortKey); Logger::Print("Sorting server list by token: %d\n", ServerList::SortKey);
ServerList::SortList(); ServerList::SortList();
}); });
UIScript::Add("CreateListFavorite", [](UIScript::Token) UIScript::Add("CreateListFavorite", [](UIScript::Token)
{ {
ServerList::ServerInfo* info = ServerList::GetCurrentServer(); ServerList::ServerInfo* info = ServerList::GetCurrentServer();
@ -829,10 +844,12 @@ namespace Components
ServerList::StoreFavourite(info->addr.getString()); ServerList::StoreFavourite(info->addr.getString());
} }
}); });
UIScript::Add("CreateFavorite", [](UIScript::Token) UIScript::Add("CreateFavorite", [](UIScript::Token)
{ {
ServerList::StoreFavourite(Dvar::Var("ui_favoriteAddress").get<std::string>()); ServerList::StoreFavourite(Dvar::Var("ui_favoriteAddress").get<std::string>());
}); });
UIScript::Add("CreateCurrentServerFavorite", [](UIScript::Token) UIScript::Add("CreateCurrentServerFavorite", [](UIScript::Token)
{ {
if (Game::CL_IsCgameInitialized()) if (Game::CL_IsCgameInitialized())
@ -844,6 +861,7 @@ namespace Components
} }
} }
}); });
UIScript::Add("DeleteFavorite", [](UIScript::Token) UIScript::Add("DeleteFavorite", [](UIScript::Token)
{ {
ServerList::ServerInfo* info = ServerList::GetCurrentServer(); ServerList::ServerInfo* info = ServerList::GetCurrentServer();
@ -854,17 +872,18 @@ namespace Components
}; };
}); });
#ifdef _DEBUG
Command::Add("playerCount", [](Command::Params*) Command::Add("playerCount", [](Command::Params*)
{ {
int count = 0; auto count = 0;
for (auto server : ServerList::OnlineList) for (const auto& server : ServerList::OnlineList)
{ {
count += server.clients; count += server.clients;
} }
Logger::Print("There are %d players playing.\n", count); Logger::Print("There are %d players playing.\n", count);
}); });
#endif
// Add required ownerDraws // Add required ownerDraws
UIScript::AddOwnerDraw(220, ServerList::UpdateSource); UIScript::AddOwnerDraw(220, ServerList::UpdateSource);
UIScript::AddOwnerDraw(253, ServerList::UpdateGameType); UIScript::AddOwnerDraw(253, ServerList::UpdateGameType);

View File

@ -138,5 +138,10 @@ namespace Components
static std::vector<ServerInfo> FavouriteList; static std::vector<ServerInfo> FavouriteList;
static std::vector<unsigned int> VisibleList; static std::vector<unsigned int> VisibleList;
static Dvar::Var UIServerSelected;
static Dvar::Var UIServerSelectedMap;
static Dvar::Var NETServerQueryLimit;
static Dvar::Var NETServerFrames;
}; };
} }

View File

@ -921,7 +921,7 @@ namespace Game
typedef int(__cdecl * UI_TextWidth_t)(const char *text, int maxChars, Font_s *font, float scale); typedef int(__cdecl * UI_TextWidth_t)(const char *text, int maxChars, Font_s *font, float scale);
extern UI_TextWidth_t UI_TextWidth; extern UI_TextWidth_t UI_TextWidth;
typedef void(__cdecl * UI_DrawText_t)(void* scrPlace, const char *text, int maxChars, Font_s *font, float x, float y, int horzAlign, int vertAlign, float scale, const float *color, int style); typedef void(__cdecl * UI_DrawText_t)(const ScreenPlacement* scrPlace, const char* text, int maxChars, Font_s* font, float x, float y, int horzAlign, int vertAlign, float scale, const float* color, int style);
extern UI_DrawText_t UI_DrawText; extern UI_DrawText_t UI_DrawText;
typedef Font_s* (__cdecl* UI_GetFontHandle_t)(ScreenPlacement* scrPlace, int fontEnum, float scale); typedef Font_s* (__cdecl* UI_GetFontHandle_t)(ScreenPlacement* scrPlace, int fontEnum, float scale);