blob: c7d8d9b34465c64a3388a746460430b2d2df0392 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
#include "shared/pch.hh"
#include "shared/world/item_registry.hh"
#include "core/math/crc64.hh"
#include "shared/world/voxel_registry.hh"
static std::uint64_t registry_checksum = 0U;
std::unordered_map<std::string, item_id> item_registry::names = {};
std::vector<std::unique_ptr<Item>> item_registry::items = {};
static void recalculate_checksum(void)
{
registry_checksum = 0U;
for(const auto& item : item_registry::items) {
registry_checksum = item->get_checksum(registry_checksum);
}
}
Item* item_registry::register_item(const ItemBuilder& builder)
{
assert(builder.get_name().size());
assert(nullptr == find(builder.get_name()));
const auto id = static_cast<item_id>(1 + items.size());
std::unique_ptr<Item> item(builder.build(id));
names.emplace(std::string(builder.get_name()), id);
items.push_back(std::move(item));
recalculate_checksum();
return items.back().get();
}
Item* item_registry::find(std::string_view name)
{
const auto it = names.find(std::string(name));
if(it == names.end()) {
return nullptr;
}
return items[it->second - 1].get();
}
Item* item_registry::find(const item_id item)
{
if(item == NULL_ITEM_ID || item > items.size()) {
return nullptr;
}
return items[item - 1].get();
}
void item_registry::purge(void)
{
registry_checksum = 0U;
items.clear();
names.clear();
}
std::uint64_t item_registry::get_checksum(void)
{
return registry_checksum;
}
|