h1-mod/src/client/component/materials.cpp

166 lines
4.3 KiB
C++
Raw Normal View History

2022-03-15 19:27:49 -04:00
#include <std_include.hpp>
#include "loader/component_loader.hpp"
#include "materials.hpp"
#include "console.hpp"
#include "game/game.hpp"
#include "game/dvars.hpp"
#include <utils/hook.hpp>
#include <utils/memory.hpp>
#include <utils/io.hpp>
#include <utils/string.hpp>
#include <utils/image.hpp>
#include <utils/concurrency.hpp>
namespace materials
{
namespace
{
utils::hook::detour db_material_streaming_fail_hook;
utils::hook::detour material_register_handle_hook;
struct material_data_t
{
std::unordered_map<std::string, game::Material*> materials;
std::unordered_map<std::string, std::string> images;
};
2022-03-17 18:56:15 -04:00
char constant_table[0x20] = {};
2022-03-15 19:27:49 -04:00
utils::concurrency::container<material_data_t> material_data;
game::GfxImage* setup_image(game::GfxImage* image, const utils::image& raw_image)
{
image->imageFormat = 0x1000003;
image->resourceSize = -1;
D3D11_SUBRESOURCE_DATA data{};
data.SysMemPitch = raw_image.get_width() * 4;
data.SysMemSlicePitch = data.SysMemPitch * raw_image.get_height();
data.pSysMem = raw_image.get_buffer();
game::Image_Setup(image, raw_image.get_width(), raw_image.get_height(), image->depth, image->numElements,
image->imageFormat, DXGI_FORMAT_R8G8B8A8_UNORM, image->name, &data);
return image;
}
game::Material* create_material(const std::string& name, const std::string& data)
{
2022-03-27 20:21:24 -04:00
const auto white = material_register_handle_hook.invoke<game::Material*>("white");
2022-03-15 19:27:49 -04:00
const auto material = utils::memory::get_allocator()->allocate<game::Material>();
const auto texture_table = utils::memory::get_allocator()->allocate<game::MaterialTextureDef>();
const auto image = utils::memory::get_allocator()->allocate<game::GfxImage>();
std::memcpy(material, white, sizeof(game::Material));
std::memcpy(texture_table, white->textureTable, sizeof(game::MaterialTextureDef));
std::memcpy(image, white->textureTable->u.image, sizeof(game::GfxImage));
2022-03-17 18:56:15 -04:00
material->constantTable = &constant_table;
2022-03-15 19:27:49 -04:00
material->name = utils::memory::get_allocator()->duplicate_string(name);
image->name = material->name;
material->textureTable = texture_table;
material->textureTable->u.image = setup_image(image, data);
return material;
}
game::Material* load_material(const std::string& name)
{
return material_data.access<game::Material*>([&](material_data_t& data_) -> game::Material*
{
if (const auto i = data_.materials.find(name); i != data_.materials.end())
{
return i->second;
}
std::string data{};
if (const auto i = data_.images.find(name); i != data_.images.end())
{
data = i->second;
}
if (data.empty()
&& !utils::io::read_file(utils::string::va("h1-mod/materials/%s.png", name.data()), &data)
&& !utils::io::read_file(utils::string::va("data/materials/%s.png", name.data()), &data))
{
2022-03-17 18:56:15 -04:00
data_.materials[name] = nullptr;
2022-03-15 19:27:49 -04:00
return nullptr;
}
const auto material = create_material(name, data);
data_.materials[name] = material;
return material;
});
}
game::Material* try_load_material(const std::string& name)
{
2022-03-15 20:56:00 -04:00
if (name == "white")
{
return nullptr;
}
2022-03-15 19:27:49 -04:00
try
{
return load_material(name);
}
catch (const std::exception& e)
{
console::error("Failed to load material %s: %s\n", name.data(), e.what());
}
return nullptr;
}
game::Material* material_register_handle_stub(const char* name)
{
auto result = try_load_material(name);
if (result == nullptr)
{
result = material_register_handle_hook.invoke<game::Material*>(name);
}
return result;
}
2022-03-17 18:56:15 -04:00
int db_material_streaming_fail_stub(game::Material* material)
2022-03-15 19:27:49 -04:00
{
2022-03-17 18:56:15 -04:00
if (material->constantTable == &constant_table)
2022-03-15 19:27:49 -04:00
{
2022-03-17 18:56:15 -04:00
return 0;
2022-03-15 19:27:49 -04:00
}
2022-03-17 18:56:15 -04:00
return db_material_streaming_fail_hook.invoke<int>(material);
2022-03-15 19:27:49 -04:00
}
}
void add(const std::string& name, const std::string& data)
{
material_data.access([&](material_data_t& data_)
{
data_.images[name] = data;
});
}
class component final : public component_interface
{
public:
void post_unpack() override
{
if (game::environment::is_dedi())
{
return;
}
material_register_handle_hook.create(game::Material_RegisterHandle, material_register_handle_stub);
db_material_streaming_fail_hook.create(SELECT_VALUE(0x1401D3180, 0x1402C6260), db_material_streaming_fail_stub);
}
};
}
REGISTER_COMPONENT(materials::component)