Support motd image

This commit is contained in:
Maurice Heumann 2021-05-16 10:18:03 +02:00
parent 5a67d0a9c5
commit b1c574e2ab
5 changed files with 58 additions and 10 deletions

View File

@ -1,17 +1,20 @@
#include <std_include.hpp>
#include "loader/component_loader.hpp"
#include "game/game.hpp"
#include "images.hpp"
#include <utils/hook.hpp>
#include <utils/string.hpp>
#include <utils/image.hpp>
#include <utils/io.hpp>
#include <utils/concurrency.hpp>
namespace images
{
namespace
{
utils::hook::detour load_texture_hook;
utils::concurrency::container<std::unordered_map<std::string, std::string>> 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<std::string> 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<std::string, std::string>& 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<utils::image> 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<std::string, std::string>& 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);
}
};

View File

@ -0,0 +1,6 @@
#pragma once
namespace images
{
void override_texture(std::string name, std::string data);
}

View File

@ -1,6 +1,7 @@
#include <std_include.hpp>
#include "loader/component_loader.hpp"
#include "motd.hpp"
#include "images.hpp"
#include <utils/hook.hpp>
#include <utils/http.hpp>
@ -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();
}
};
}

View File

@ -66,4 +66,9 @@ namespace utils
return this->path_;
}
const std::string& binary_resource::get_data() const
{
return this->resource_;
}
}

View File

@ -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_;