motd: fix crash, cleanup code

This commit is contained in:
FutureRave 2022-11-06 12:55:56 +00:00
parent 2aea5f776f
commit cb8f092c83

View File

@ -15,7 +15,7 @@ namespace motd
{ {
std::string motd_resource = utils::nt::load_resource(DW_MOTD); std::string motd_resource = utils::nt::load_resource(DW_MOTD);
std::future<std::optional<std::string>> motd_future; std::future<std::optional<std::string>> motd_future;
std::string marketing_featured_msg; std::string marketing_featured;
} }
std::string get_text() std::string get_text()
@ -33,11 +33,11 @@ namespace motd
utils::hook::detour marketing_get_message_hook; utils::hook::detour marketing_get_message_hook;
bool marketing_get_message_stub(int controller, int location, char* message, int max_length) bool marketing_get_message_stub(int /*controller_index*/, int /*location-id*/, char* message_text, int message_text_length)
{ {
if (marketing_featured_msg.empty()) return false; if (marketing_featured.empty()) return false;
strncpy_s(message, marketing_featured_msg.length() + 1, marketing_featured_msg.data(), max_length); strncpy_s(message_text, message_text_length, marketing_featured.data(), _TRUNCATE);
return true; return true;
} }
@ -48,26 +48,37 @@ namespace motd
void post_load() override void post_load() override
{ {
motd_future = utils::http::get_data_async("https://xlabs.dev/s1/motd.txt"); motd_future = utils::http::get_data_async("https://xlabs.dev/s1/motd.txt");
std::thread([]() std::thread([]
{
auto data = utils::http::get_data("https://xlabs.dev/s1/motd.png");
if (data.has_value())
{ {
auto data = utils::http::get_data("https://xlabs.dev/s1/motd.png"); images::override_texture("iotd_image", data.value());
if (data) }
{
images::override_texture("iotd_image", data.value());
}
auto featured_optional = utils::http::get_data("https://xlabs.dev/s1/featured_msg.json"); auto featured_optional = utils::http::get_data("https://xlabs.dev/s1/featured_msg.json");
if (featured_optional) if (featured_optional.has_value())
{ {
marketing_featured_msg = featured_optional.value(); marketing_featured = featured_optional.value();
} }
}).detach(); }).detach();
} }
void post_unpack() override void post_unpack() override
{ {
marketing_get_message_hook.create(0x140126930, marketing_get_message_stub); // not sure why but in s1x, client doesnt ask for maketing messages from demonware even though marketing_active set to true if (!game::environment::is_mp())
{
return;
}
// Not sure why but in S1x, client doesn't ask for maketing messages from demonware even though marketing_active set to true
marketing_get_message_hook.create(0x140126930, marketing_get_message_stub);
}
void pre_destroy() override
{
marketing_get_message_hook.clear();
} }
}; };
} }