From b1c574e2abe528643ffa20d0d7996a5638728920 Mon Sep 17 00:00:00 2001 From: Maurice Heumann Date: Sun, 16 May 2021 10:18:03 +0200 Subject: [PATCH] Support motd image --- src/client/component/images.cpp | 47 ++++++++++++++++++++++------ src/client/component/images.hpp | 6 ++++ src/client/component/motd.cpp | 9 ++++++ src/common/utils/binary_resource.cpp | 5 +++ src/common/utils/binary_resource.hpp | 1 + 5 files changed, 58 insertions(+), 10 deletions(-) create mode 100644 src/client/component/images.hpp diff --git a/src/client/component/images.cpp b/src/client/component/images.cpp index a5e6849..ee75293 100644 --- a/src/client/component/images.cpp +++ b/src/client/component/images.cpp @@ -1,17 +1,20 @@ #include #include "loader/component_loader.hpp" #include "game/game.hpp" +#include "images.hpp" #include #include #include #include +#include namespace images { namespace { utils::hook::detour load_texture_hook; + utils::concurrency::container> overriden_textures; static_assert(sizeof(game::GfxImage) == 104); static_assert(offsetof(game::GfxImage, name) == (sizeof(game::GfxImage) - sizeof(void*))); @@ -20,10 +23,16 @@ namespace images std::optional load_image(game::GfxImage* image) { - printf("Loading: %s\n", image->name); - std::string data{}; - if(!utils::io::read_file(utils::string::va("s1x/images/%s.png", image->name), &data)) + overriden_textures.access([&](const std::unordered_map& textures) + { + if (const auto i = textures.find(image->name); i != textures.end()) + { + data = i->second; + } + }); + + if (data.empty() && !utils::io::read_file(utils::string::va("s1x/images/%s.png", image->name), &data)) { return {}; } @@ -34,7 +43,7 @@ namespace images std::optional load_raw_image_from_file(game::GfxImage* image) { const auto image_file = load_image(image); - if(!image_file) + if (!image_file) { return {}; } @@ -51,7 +60,7 @@ namespace images texture->GetDesc(&desc); texture->GetDevice(&device); - if(device) + if (device) { device->GetImmediateContext(&context); device->Release(); @@ -59,7 +68,7 @@ namespace images auto _ = gsl::finally([&]() { - if(context) + if (context) { context->Release(); } @@ -95,7 +104,7 @@ namespace images bool load_custom_texture(game::GfxImage* image) { const auto raw_image = load_raw_image_from_file(image); - if(!raw_image) + if (!raw_image) { return false; } @@ -113,20 +122,38 @@ namespace images void load_texture_stub(game::GfxImageLoadDef** load_def, game::GfxImage* image) { - if(!load_custom_texture(image)) + printf("Loading: %s\n", image->name); + + try { - load_texture_hook.invoke(load_def, image); + if (load_custom_texture(image)) + { + return; + } } + catch(std::exception&) + { + } + + load_texture_hook.invoke(load_def, image); } } + void override_texture(std::string name, std::string data) + { + overriden_textures.access([&](std::unordered_map& textures) + { + textures[std::move(name)] = std::move(data); + }); + } + class component final : public component_interface { public: void post_unpack() override { if (!game::environment::is_mp()) return; - + load_texture_hook.create(0x1405A21F0, load_texture_stub); } }; diff --git a/src/client/component/images.hpp b/src/client/component/images.hpp new file mode 100644 index 0000000..a1c8134 --- /dev/null +++ b/src/client/component/images.hpp @@ -0,0 +1,6 @@ +#pragma once + +namespace images +{ + void override_texture(std::string name, std::string data); +} diff --git a/src/client/component/motd.cpp b/src/client/component/motd.cpp index af60e2a..2204553 100644 --- a/src/client/component/motd.cpp +++ b/src/client/component/motd.cpp @@ -1,6 +1,7 @@ #include #include "loader/component_loader.hpp" #include "motd.hpp" +#include "images.hpp" #include #include @@ -35,6 +36,14 @@ namespace motd void post_load() override { motd_future = utils::http::get_data_async("https://xlabs.dev/s1/motd.txt"); + std::thread([]() + { + auto data = utils::http::get_data("https://xlabs.dev/s1/motd.png"); + if(data) + { + images::override_texture("iotd_image", data.value()); + } + }).detach(); } }; } diff --git a/src/common/utils/binary_resource.cpp b/src/common/utils/binary_resource.cpp index 5ac0e24..90983f0 100644 --- a/src/common/utils/binary_resource.cpp +++ b/src/common/utils/binary_resource.cpp @@ -66,4 +66,9 @@ namespace utils return this->path_; } + + const std::string& binary_resource::get_data() const + { + return this->resource_; + } } diff --git a/src/common/utils/binary_resource.hpp b/src/common/utils/binary_resource.hpp index a379897..e35b3df 100644 --- a/src/common/utils/binary_resource.hpp +++ b/src/common/utils/binary_resource.hpp @@ -10,6 +10,7 @@ namespace utils binary_resource(int id, std::string file); std::string get_extracted_file(); + const std::string& get_data() const; private: std::string resource_;