LeviLamina
Loading...
Searching...
No Matches
BlockTypeRegistry.h
1#pragma once
2
3#include "ll/api/utils/StringUtils.h"
4#include "mc/_HeaderOutputPredefine.h"
5#include "mc/common/SharedPtr.h"
6#include "mc/common/WeakPtr.h"
7#include "mc/world/level/block/BlockType.h"
8#include "mc/world/level/block/registry/BlockTypeRegistryModificationsLock.h"
9
10// auto generated inclusion list
11#include "mc/deps/core/sem_ver/SemVersion.h"
12#include "mc/deps/core/string/HashedString.h"
13#include "mc/deps/core/utility/Owner.h"
14#include "mc/platform/brstd/function_ref.h"
15#include "mc/util/BaseGameVersion.h"
16
17// auto generated forward declare list
18// clang-format off
19class Block;
20class BlockType;
22class Experiments;
25namespace VoxelShapes { class VoxelShapeRegistry; }
26// clang-format on
27
28class BlockTypeRegistry {
29public:
30 // BlockTypeRegistry inner types declare
31 // clang-format off
34 struct DirectAccessBlocks;
36 // clang-format on
37
38 // BlockTypeRegistry inner types define
39 enum class LookupByNameImplResolve : int {
40 BlockType = 0,
41 Block = 1,
42 };
43
44 struct BlockComplexAliasBlockState {
45 public:
46 BlockComplexAliasBlockState(HashedString const& stateName, int value) noexcept
47 : stateName(stateName),
48 value(value) {}
49
50 public:
51 // member variables
52 // NOLINTBEGIN
53 ::HashedString stateName;
54 int value;
55 // NOLINTEND
56
57 public:
58 // destructor thunk
59 // NOLINTBEGIN
60 MCFOLD void $dtor();
61 // NOLINTEND
62 };
63
65 public:
66 // member variables
67 // NOLINTBEGIN
68 ::ll::TypedStorage<8, 64, ::std::function<::Block const*(int)>> mCallback;
69 ::ll::TypedStorage<8, 32, ::BaseGameVersion> mMinRequiredVersion;
70 ::ll::TypedStorage<8, 24, ::SemVersion> mFlattenedInBlocksJsonVersion;
71 ::ll::TypedStorage<4, 4, int> mStartVariant;
72 // NOLINTEND
73 };
74
75 struct DirectAccessBlocks {
76 public:
77 // member variables
78 // NOLINTBEGIN
79 ::ll::TypedStorage<8, 8, ::BlockType const&> mAirBlock;
80 // NOLINTEND
81
82 public:
83 // prevent constructor by default
84 DirectAccessBlocks& operator=(DirectAccessBlocks const&);
85 DirectAccessBlocks(DirectAccessBlocks const&);
86 DirectAccessBlocks();
87 };
88
90 public:
91 // member variables
92 // NOLINTBEGIN
93 ::ll::TypedStorage<8, 8, ::BlockType const*> mBlockType;
94 ::ll::TypedStorage<8, 8, ::Block const*> mBlock;
95 // NOLINTEND
96 };
97
98 using BlockAliasLookupMap = ::std::unordered_map<::HashedString, ::HashedString>;
99
100 using BlockComplexAliasCallback = ::std::function<::Block const*(int)>;
101
102 using BlockComplexAliasLookupMap = ::entt::dense_map<::HashedString, ::BlockTypeRegistry::BlockComplexAliasContent>;
103
104 using BlockComplexAliasPostSplitBlockNames = ::std::vector<::std::reference_wrapper<::HashedString const>>;
105
106 using BlockComplexAliasPostSplitBlockNamesList =
107 ::std::vector<::std::vector<::std::reference_wrapper<::HashedString const>>>;
108
109 using BlockComplexAliasPostSplitBlockNamesLookupMap = ::entt::dense_map<uint64, uint64>;
110
111 using BlockLookupMap = ::std::map<::HashedString, ::std::unique_ptr<::BlockType>>;
112
113 using BlockNameHashToHashedStringMap = ::entt::dense_map<uint64, ::HashedString>;
114
115public:
116 // member variables
117 // NOLINTBEGIN
118 ::ll::TypedStorage<8, 16, ::std::map<::HashedString, ::std::unique_ptr<::BlockType>>> mBlockLookupMap;
119 ::ll::TypedStorage<8, 64, ::std::unordered_map<::HashedString, ::HashedString>> mBlockAliasLookupMap;
120 ::ll::TypedStorage<8, 72, ::entt::dense_map<::HashedString, ::BlockTypeRegistry::BlockComplexAliasContent>>
121 mBlockComplexAliasLookupMap;
122 ::ll::TypedStorage<8, 24, ::std::vector<::std::vector<::std::reference_wrapper<::HashedString const>>>>
123 mBlockComplexAliasPostSplitBlockNamesList;
124 ::ll::TypedStorage<8, 72, ::entt::dense_map<uint64, uint64>> mBlockComplexAliasPostSplitBlockNamesLookupMap;
125 ::ll::TypedStorage<8, 16, ::std::map<::HashedString, ::HashedString>> mBlockComplexAliasPreSplitBlockNamesLookupMap;
126 ::ll::TypedStorage<8, 16, ::std::set<::std::string>> mKnownNamespaces;
127 ::ll::TypedStorage<8, 72, ::entt::dense_map<uint64, ::HashedString>> mBlockNameHashToStringMap;
128 ::ll::TypedStorage<8, 8, ::std::unique_ptr<::BlockTypeRegistry::DirectAccessBlocks>> mDirectAccessBlocks;
129 ::ll::TypedStorage<8, 16, ::std::weak_ptr<::VoxelShapes::VoxelShapeRegistry>> mShapeRegistry;
130 ::ll::TypedStorage<8, 16, ::std::shared_ptr<::BlockTypeRegistryRWLock>> mRWLock;
131 // NOLINTEND
132
133public:
134 [[nodiscard]] static ::BlockTypeRegistry& get() { return mBlockTypeRegistry().mValue; }
135
137 template <class TBlockType, class... Args>
138 TBlockType& registerBlock(::HashedString const& blockName, Args&&... args) {
139 ::std::string nameId = blockName.getString();
140
142
143 auto* block = new TBlockType(nameId, ::std::forward<Args>(args)...);
144
145 // Registration keys on the lowercased full name; the part before ':' is the namespace.
146 ::std::string lowered = ll::string_utils::toLowerCase(block->mNameInfo->mFullName->getString());
147 auto const colon = lowered.find(':');
148
149 mKnownNamespaces->emplace(lowered.substr(0, colon));
150 mBlockLookupMap->try_emplace(::HashedString{lowered}, std::unique_ptr<::BlockType>{block});
151
152 ::HashedString hashed{lowered};
153 mBlockNameHashToStringMap->operator[](hashed.getHash()) = hashed;
154
155 return *block;
156 }
157
158 ::Block const* lookupByName(
159 ::HashedString const& name,
160 ::std::vector<::BlockTypeRegistry::BlockComplexAliasBlockState> const& states,
161 bool logNotFound
162 ) const {
163 auto result = _lookupByNameImpl(name, 0, ::BlockTypeRegistry::LookupByNameImplResolve::Block, logNotFound);
164 if (result.mBlock == nullptr) {
165 return nullptr;
166 }
167
168 return _lookupByNameImplSetNewBlockStates(*result.mBlock, states);
169 }
170
171 ::BlockType const* lookupByName(::HashedString const& name, bool logNotFound = false) const {
172 return _lookupByNameImpl(name, 0, ::BlockTypeRegistry::LookupByNameImplResolve::BlockType, logNotFound)
173 .mBlockType;
174 }
175
176public:
177 // member functions
178 // NOLINTBEGIN
179 MCAPI BlockTypeRegistry();
180
181 MCAPI ::BlockTypeRegistry::LookupByNameImplReturnType _lookupByNameImpl(
182 ::HashedString const& name,
183 int data,
184 ::BlockTypeRegistry::LookupByNameImplResolve resolve,
185 bool logNotFound
186 ) const;
187
188 MCAPI ::Block const* _lookupByNameImplSetNewBlockStates(
189 ::Block const& block,
190 ::std::vector<::BlockTypeRegistry::BlockComplexAliasBlockState> const& states
191 ) const;
192
193#ifdef LL_PLAT_C
194 MCAPI void addBlocksToValidator(::LinkedAssetValidator& validator, ::BaseGameVersion const& baseGameVersion) const;
195#endif
196
197 MCAPI void checkBlockPermutationCap() const;
198
199 MCAPI uint64 computeBlockTypeRegistryChecksum(::BaseGameVersion const& worldBaseGameVersion) const;
200
201 MCAPI void finalizeBlockComponentStorage();
202
203#ifdef LL_PLAT_C
204 MCAPI void finalizeBlockComponentStorageForRendering(::brstd::function_ref<void(::BlockType&)> finalizer);
205#endif
206
207 MCAPI void finalizeBlockCustomComponentEvents(::ServerScriptManager const& scriptManager);
208
209#ifdef LL_PLAT_C
210 MCFOLD void forEachBlockDEPRECATED(::brstd::function_ref<bool(::BlockType&)> callback);
211#endif
212
213 MCAPI void forEachBlockMutable(::brstd::function_ref<bool(::Block&)> callback);
214
215 MCFOLD void forEachBlockType(::brstd::function_ref<bool(::BlockType const&)> callback) const;
216
217 MCAPI ::HashedString const& getBlockNameFromNameHash(uint64 hash) const;
218
219 MCAPI ::std::vector<::std::reference_wrapper<::HashedString const>> const&
220 getComplexAliasPostSplitBlockNames(::HashedString const& oldName) const;
221
222#ifdef LL_PLAT_C
223 MCAPI ::HashedString const* getComplexAliasPreSplitBlockName(::HashedString const& postSplitName) const;
224#endif
225
226 MCAPI ::Block const& getDefaultBlockState(::HashedString const& name, bool logNotFound = false) const;
227
228 MCAPI ::BlockTypeRegistry::DirectAccessBlocks const& getDirectAccessBlocks() const;
229
230#ifdef LL_PLAT_C
231 MCAPI int getStartVariantForComplexAliasBlock(::HashedString const& blockName) const;
232#endif
233
234 MCAPI void initHardCodedBlockComponents(::Experiments const& experiments);
235
236 MCAPI void initRWLock();
237
238 MCAPI bool isComplexAliasBlock(::HashedString const& blockName) const;
239
240#ifdef LL_PLAT_C
241 MCAPI bool isExpectFlattenedInBlocksJson(::HashedString const& blockName, ::SemVersion const& currentVersion) const;
242#endif
243
244 MCAPI void prepareBlocks(uint latestUpdaterVersion);
245
246 MCAPI void registerAlias(::HashedString const& alias, ::HashedString const& name);
247
248 MCAPI void registerComplexAlias(
249 ::HashedString const& alias,
250 ::std::function<::Block const*(int)> callback,
251 ::std::vector<::std::reference_wrapper<::HashedString const>> const& postSplitBlockNames,
252 ::BaseGameVersion const& minRequiredVersion,
253 ::SemVersion const& blocksJsonFormatVersion,
254 ::HashedString defaultNewBlockName,
255 int startVariant
256 );
257
258 MCAPI void unregisterBlock(::HashedString const& name);
259
260 MCAPI void unregisterBlocks();
261
262 MCAPI ~BlockTypeRegistry();
263 // NOLINTEND
264
265public:
266 // static variables
267 // NOLINTBEGIN
268 MCAPI static ::Bedrock::Owner<::BlockTypeRegistry>& mBlockTypeRegistry();
269 // NOLINTEND
270
271public:
272 // constructor thunks
273 // NOLINTBEGIN
274 MCAPI void* $ctor();
275 // NOLINTEND
276
277public:
278 // destructor thunk
279 // NOLINTBEGIN
280 MCAPI void $dtor();
281 // NOLINTEND
282};
Definition BlockTypeRegistryModificationsLock.h:10
Definition BlockTypeRegistryRWLock.h:8
Definition BlockTypeRegistry.h:64
TBlockType & registerBlock(::HashedString const &blockName, Args &&... args)
Constructs a new block of type TBlockType and registers it under blockName.
Definition BlockTypeRegistry.h:138
Definition BlockType.h:79
Definition Block.h:41
Definition Experiments.h:14
Definition HashedString.h:5
Definition LinkedAssetValidator.h:8
Definition ServerScriptManager.h:78
Definition VoxelShapeRegistry.h:17
Definition BlockTypeRegistry.h:44
Definition BlockTypeRegistry.h:75
Definition BlockTypeRegistry.h:89