1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-06 17:41:04 +00:00

Move client code out of ItemDefManager (#15967)

This commit is contained in:
cx384 2025-04-04 18:58:14 +02:00 committed by GitHub
parent a6d4cd7c15
commit 52b974184d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 195 additions and 163 deletions

View file

@ -7,14 +7,6 @@
#include "nodedef.h"
#include "tool.h"
#include "inventory.h"
#if CHECK_CLIENT_BUILD()
#include "client/mapblock_mesh.h"
#include "client/mesh.h"
#include "client/wieldmesh.h"
#include "client/client.h"
#include "client/texturesource.h"
#endif
#include "log.h"
#include "settings.h"
#include "util/serialize.h"
@ -360,34 +352,10 @@ void ItemDefinition::deSerialize(std::istream &is, u16 protocol_version)
class CItemDefManager: public IWritableItemDefManager
{
#if CHECK_CLIENT_BUILD()
struct ClientCached
{
video::ITexture *inventory_texture;
ItemMesh wield_mesh;
Palette *palette;
ClientCached():
inventory_texture(NULL),
palette(NULL)
{}
~ClientCached() {
if (wield_mesh.mesh)
wield_mesh.mesh->drop();
}
DISABLE_CLASS_COPY(ClientCached);
};
#endif
public:
CItemDefManager()
{
#if CHECK_CLIENT_BUILD()
m_main_thread = std::this_thread::get_id();
#endif
clear();
}
@ -435,94 +403,6 @@ public:
return m_item_definitions.find(name) != m_item_definitions.cend();
}
#if CHECK_CLIENT_BUILD()
protected:
ClientCached* createClientCachedDirect(const ItemStack &item, Client *client) const
{
// This is not thread-safe
sanity_check(std::this_thread::get_id() == m_main_thread);
const ItemDefinition &def = item.getDefinition(this);
std::string inventory_image = item.getInventoryImage(this);
std::string inventory_overlay = item.getInventoryOverlay(this);
std::string cache_key = def.name;
if (!inventory_image.empty())
cache_key += "/" + inventory_image;
if (!inventory_overlay.empty())
cache_key += ":" + inventory_overlay;
// Skip if already in cache
auto it = m_clientcached.find(cache_key);
if (it != m_clientcached.end())
return it->second.get();
infostream << "Lazily creating item texture and mesh for \""
<< cache_key << "\"" << std::endl;
ITextureSource *tsrc = client->getTextureSource();
// Create new ClientCached
auto cc = std::make_unique<ClientCached>();
cc->inventory_texture = NULL;
if (!inventory_image.empty())
cc->inventory_texture = tsrc->getTexture(inventory_image);
getItemMesh(client, item, &(cc->wield_mesh));
cc->palette = tsrc->getPalette(def.palette_image);
// Put in cache
ClientCached *ptr = cc.get();
m_clientcached[cache_key] = std::move(cc);
return ptr;
}
public:
// Get item inventory texture
virtual video::ITexture* getInventoryTexture(const ItemStack &item,
Client *client) const
{
ClientCached *cc = createClientCachedDirect(item, client);
if (!cc)
return nullptr;
return cc->inventory_texture;
}
// Get item wield mesh
virtual ItemMesh* getWieldMesh(const ItemStack &item, Client *client) const
{
ClientCached *cc = createClientCachedDirect(item, client);
if (!cc)
return nullptr;
return &(cc->wield_mesh);
}
// Get item palette
virtual Palette* getPalette(const ItemStack &item, Client *client) const
{
ClientCached *cc = createClientCachedDirect(item, client);
if (!cc)
return nullptr;
return cc->palette;
}
virtual video::SColor getItemstackColor(const ItemStack &stack,
Client *client) const
{
// Look for direct color definition
const std::string &colorstring = stack.metadata.getString("color", 0);
video::SColor directcolor;
if (!colorstring.empty() && parseColorString(colorstring, directcolor, true))
return directcolor;
// See if there is a palette
Palette *palette = getPalette(stack, client);
const std::string &index = stack.metadata.getString("palette_index", 0);
if (palette && !index.empty())
return (*palette)[mystoi(index, 0, 255)];
// Fallback color
return get(stack.name).color;
}
#endif
void applyTextureOverrides(const std::vector<TextureOverride> &overrides)
{
infostream << "ItemDefManager::applyTextureOverrides(): Applying "
@ -666,12 +546,6 @@ private:
std::map<std::string, ItemDefinition*> m_item_definitions;
// Aliases
StringMap m_aliases;
#if CHECK_CLIENT_BUILD()
// The id of the thread that is allowed to use irrlicht directly
std::thread::id m_main_thread;
// Cached textures and meshes
mutable std::unordered_map<std::string, std::unique_ptr<ClientCached>> m_clientcached;
#endif
};
IWritableItemDefManager* createItemDefManager()