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

111 lines
2.6 KiB
C++
Raw Normal View History

2022-02-27 07:53:44 -05:00
#include <STDInclude.hpp>
2017-01-19 16:23:59 -05:00
2017-02-04 14:45:27 -05:00
#define NEWS_MOTD_DEFAULT "Welcome to IW4x Multiplayer!"
2017-01-19 16:23:59 -05:00
namespace Components
{
bool News::Terminate;
std::thread News::Thread;
bool News::unitTest()
{
bool result = true;
if (News::Thread.joinable())
{
Logger::Debug("Awaiting thread termination...");
2017-01-19 16:23:59 -05:00
News::Thread.join();
2017-02-04 14:45:27 -05:00
if (!strcmp(Localization::Get("MPUI_MOTD_TEXT"), NEWS_MOTD_DEFAULT))
2017-01-19 16:23:59 -05:00
{
Logger::Print("Failed to fetch motd!\n");
result = false;
}
else
{
Logger::Debug("Successfully fetched motd");
2017-01-19 16:23:59 -05:00
}
}
return result;
}
const char* News::GetNewsText()
{
return Localization::Get("MPUI_MOTD_TEXT");
}
News::News()
{
if (ZoneBuilder::IsEnabled() || Dedicated::IsEnabled()) return; // Maybe also dedi?
2017-01-19 16:23:59 -05:00
Dvar::Register<bool>("g_firstLaunch", true, Game::DVAR_ARCHIVE, "");
Dvar::Register<int>("cl_updateoldversion", REVISION, REVISION, REVISION, Game::DVAR_INIT, "Current version number.");
2017-01-19 16:23:59 -05:00
2022-08-24 10:38:14 -04:00
UIScript::Add("checkFirstLaunch", []([[maybe_unused]] const UIScript::Token& token, [[maybe_unused]] const Game::uiInfo_s* info)
{
if (Dvar::Var("g_firstLaunch").get<bool>())
{
Command::Execute("openmenu menu_first_launch", false);
//Dvar::Var("g_firstLaunch").set(false);
}
});
2022-08-24 10:38:14 -04:00
UIScript::Add("visitWebsite", []([[maybe_unused]] const UIScript::Token& token, [[maybe_unused]] const Game::uiInfo_s* info)
{
2017-06-11 15:25:00 -04:00
Utils::OpenUrl(Utils::Cache::GetStaticUrl(""));
});
2017-01-19 16:23:59 -05:00
Localization::Set("MPUI_CHANGELOG_TEXT", "Loading...");
2017-02-04 14:45:27 -05:00
Localization::Set("MPUI_MOTD_TEXT", NEWS_MOTD_DEFAULT);
2017-01-19 16:23:59 -05:00
// make newsfeed (ticker) menu items not cut off based on safe area
Utils::Hook::Nop(0x63892D, 5);
// hook for getting the news ticker string
Utils::Hook::Nop(0x6388BB, 2); // skip the "if (item->text[0] == '@')" localize check
Utils::Hook(0x6388C1, News::GetNewsText, HOOK_CALL).install()->quick();
2017-07-03 09:40:32 -04:00
if (!Utils::IsWineEnvironment() && !Loader::IsPerformingUnitTests())
2017-01-19 16:23:59 -05:00
{
News::Terminate = false;
2017-01-25 08:34:53 -05:00
News::Thread = std::thread([]()
2017-01-19 16:23:59 -05:00
{
2017-02-04 15:52:49 -05:00
Changelog::LoadChangelog();
2017-06-04 14:51:18 -04:00
if (News::Terminate) return;
2017-02-04 14:26:46 -05:00
2017-01-25 08:34:53 -05:00
std::string data = Utils::Cache::GetFile("/iw4/motd.txt");
if (!data.empty())
{
Localization::Set("MPUI_MOTD_TEXT", data);
}
2017-01-19 16:23:59 -05:00
2017-07-03 09:40:32 -04:00
if (!Loader::IsPerformingUnitTests() && !News::Terminate)
2017-01-25 08:34:53 -05:00
{
while (!News::Terminate)
{
2017-01-25 08:34:53 -05:00
// Sleep for 3 minutes
for (int i = 0; i < 180 && !News::Terminate; ++i)
{
std::this_thread::sleep_for(1s);
2017-01-19 16:23:59 -05:00
}
}
}
});
}
}
void News::preDestroy()
2017-01-19 16:23:59 -05:00
{
News::Terminate = true;
if (News::Thread.joinable())
{
News::Thread.join();
}
News::Thread = std::thread();
2017-01-19 16:23:59 -05:00
}
}