Plasma Engine  2.0
Loading...
Searching...
No Matches
WorldReader.h
1#pragma once
2
3#include <Core/World/World.h>
4#include <Foundation/IO/MemoryStream.h>
5#include <Foundation/IO/Stream.h>
6#include <Foundation/Time/Time.h>
7#include <Foundation/Types/UniquePtr.h>
8
10class plProgress;
11class plProgressRange;
12
14{
15 plGameObjectHandle m_hParent;
16
17 plDynamicArray<plGameObject*>* m_pCreatedRootObjectsOut = nullptr;
18 plDynamicArray<plGameObject*>* m_pCreatedChildObjectsOut = nullptr;
19 const plUInt16* m_pOverrideTeamID = nullptr;
20
21 bool m_bForceDynamic = false;
22
25
33
35 plUInt32 m_uiCustomRandomSeedRootValue = 0;
36
37 plTime m_MaxStepTime = plTime::MakeZero();
38
39 plProgress* m_pProgress = nullptr;
40};
41
46class PL_CORE_DLL plWorldReader
47{
48public:
55 {
56 public:
57 enum class StepResult
58 {
59 Continue,
60 ContinueNextFrame,
61 Finished,
62 };
63
64 virtual ~InstantiationContextBase() = default;
65
68 virtual StepResult Step() = 0;
69
71 virtual void Cancel() = 0;
72 };
73
76
85 plResult ReadWorldDescription(plStreamReader& inout_stream, bool bWarningOnUnkownSkip = true);
86
101 plUniquePtr<InstantiationContextBase> InstantiateWorld(plWorld& ref_world, const plUInt16* pOverrideTeamID = nullptr, plTime maxStepTime = plTime::MakeZero(), plProgress* pProgress = nullptr);
102
118 plUniquePtr<InstantiationContextBase> InstantiatePrefab(plWorld& ref_world, const plTransform& rootTransform, const plPrefabInstantiationOptions& options);
119
121 plStreamReader& GetStream() const { return *m_pStream; }
122
124 plGameObjectHandle ReadGameObjectHandle();
125
127 void ReadComponentHandle(plComponentHandle& out_hComponent);
128
132 plUInt32 GetComponentTypeVersion(const plRTTI* pRtti) const;
133
135 void ClearAndCompact();
136
138 plUInt64 GetHeapMemoryUsage() const;
139
140 using FindComponentTypeCallback = plDelegate<const plRTTI*(plStringView sTypeName)>;
141
151
152 plUInt32 GetRootObjectCount() const;
153 plUInt32 GetChildObjectCount() const;
154
155 static void SetMaxStepTime(InstantiationContextBase* pContext, plTime maxStepTime);
156 static plTime GetMaxStepTime(InstantiationContextBase* pContext);
157
158private:
159 struct GameObjectToCreate
160 {
161 plGameObjectDesc m_Desc;
162 plString m_sGlobalKey;
163 plUInt32 m_uiParentHandleIdx;
164 };
165
166 void ReadGameObjectDesc(GameObjectToCreate& godesc);
167 void ReadComponentTypeInfo(plUInt32 uiComponentTypeIdx);
168 void ReadComponentDataToMemStream(bool warningOnUnknownSkip = true);
169 void ClearHandles();
170 plUniquePtr<InstantiationContextBase> Instantiate(plWorld& world, bool bUseTransform, const plTransform& rootTransform, const plPrefabInstantiationOptions& options);
171
172 plStreamReader* m_pStream = nullptr;
173 plWorld* m_pWorld = nullptr;
174
175 plUInt8 m_uiVersion = 0;
176 plDynamicArray<plGameObjectHandle> m_IndexToGameObjectHandle;
177
178 plDynamicArray<GameObjectToCreate> m_RootObjectsToCreate;
179 plDynamicArray<GameObjectToCreate> m_ChildObjectsToCreate;
180
181 struct ComponentTypeInfo
182 {
183 const plRTTI* m_pRtti = nullptr;
184 plDynamicArray<plComponentHandle> m_ComponentIndexToHandle;
185 plUInt32 m_uiNumComponents = 0;
186 };
187
188 plDynamicArray<ComponentTypeInfo> m_ComponentTypes;
189 plHashTable<const plRTTI*, plUInt32> m_ComponentTypeVersions;
190 plDefaultMemoryStreamStorage m_ComponentCreationStream;
191 plDefaultMemoryStreamStorage m_ComponentDataStream;
192 plUInt64 m_uiTotalNumComponents = 0;
193
194 plUniquePtr<plStringDeduplicationReadContext> m_pStringDedupReadContext;
195
196 class InstantiationContext : public InstantiationContextBase
197 {
198 public:
199 InstantiationContext(plWorldReader& ref_worldReader, bool bUseTransform, const plTransform& rootTransform, const plPrefabInstantiationOptions& options);
200 ~InstantiationContext();
201
202 virtual StepResult Step() override;
203 virtual void Cancel() override;
204
205 template <bool UseTransform>
206 bool CreateGameObjects(const plDynamicArray<GameObjectToCreate>& objects, plGameObjectHandle hParent, plDynamicArray<plGameObject*>* out_pCreatedObjects, plTime endTime);
207
208 bool CreateComponents(plTime endTime);
209 bool DeserializeComponents(plTime endTime);
210 bool AddComponentsToBatch(plTime endTime);
211
212 void SetMaxStepTime(plTime stepTime);
213 plTime GetMaxStepTime() const;
214
215 private:
216 void BeginNextProgressStep(plStringView sName);
217 void SetSubProgressCompletion(double fCompletion);
218
219 friend class plWorldReader;
220 plWorldReader& m_WorldReader;
221
222 bool m_bUseTransform = false;
223 plTransform m_RootTransform;
224
226
227 plComponentInitBatchHandle m_hComponentInitBatch;
228
229 // Current state
230 struct Phase
231 {
232 enum Enum
233 {
234 Invalid = -1,
235 CreateRootObjects,
236 CreateChildObjects,
237 CreateComponents,
238 DeserializeComponents,
239 AddComponentsToBatch,
240 InitComponents,
241
242 Count
243 };
244 };
245
246 Phase::Enum m_Phase = Phase::Invalid;
247 plUInt32 m_uiCurrentIndex = 0; // object or component
248 plUInt32 m_uiCurrentComponentTypeIndex = 0;
249 plUInt64 m_uiCurrentNumComponentsProcessed = 0;
250 plMemoryStreamReader m_CurrentReader;
251
252 plUniquePtr<plProgressRange> m_pOverallProgressRange;
253 plUniquePtr<plProgressRange> m_pSubProgressRange;
254 };
255};
Definition Declarations.h:363
The default implementation for memory stream storage.
Definition MemoryStream.h:161
Definition DynamicArray.h:81
Definition HashTable.h:333
A reader which can access a memory stream.
Definition MemoryStream.h:259
Manages the way a progress bar is subdivided and advanced.
Definition Progress.h:36
plProgressRange is the preferred method to inform the system of progress.
Definition Progress.h:97
This class holds information about reflected types. Each instance represents one type that is known t...
Definition RTTI.h:30
Interface for binary in (read) streams.
Definition Stream.h:22
This class to restore strings written to a stream using a plStringDeduplicationWriteContext.
Definition StringDeduplicationContext.h:53
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
A class to use together with plHashedString for quick comparisons with temporary strings that need no...
Definition HashedString.h:151
A Unique ptr manages an object and destroys that object when it goes out of scope....
Definition UniquePtr.h:10
A world encapsulates a scene graph of game objects and various component managers and their component...
Definition World.h:22
A context object is returned from InstantiateWorld or InstantiatePrefab if a maxStepTime greater than...
Definition WorldReader.h:55
StepResult
Definition WorldReader.h:58
virtual void Cancel()=0
\Brief Cancel the instantiation. This might lead to inconsistent states and must be used with care.
Reads a world description from a stream. Allows to instantiate that world multiple times in different...
Definition WorldReader.h:47
static FindComponentTypeCallback s_FindComponentTypeCallback
An optional callback to redirect the lookup of a component type name to an plRTTI type.
Definition WorldReader.h:150
plStreamReader & GetStream() const
Gives access to the stream of data. Use this inside component deserialization functions to read data.
Definition WorldReader.h:121
constexpr PL_FORCE_INLINE T Step(T value, T edge)
Returns 0, if value < edge, and 1, if value >= edge.
Definition Math_inl.h:255
A handle to a component.
Definition Declarations.h:138
A generic delegate class which supports static functions and member functions.
Definition Delegate.h:76
Describes the initial state of a game object.
Definition GameObjectDesc.h:11
A handle to a game object.
Definition Declarations.h:76
Definition WorldReader.h:14
plTempHashedString m_ReplaceNamedRootWithParent
If the prefab has a single root node with this non-empty name, rather than creating a new object,...
Definition WorldReader.h:24
RandomSeedMode
Definition WorldReader.h:27
@ CompletelyRandom
plWorld::CreateObject() will assign a random value to this object
@ FixedFromSerialization
Keep deserialized random seed value.
@ CustomRootValue
Use the given seed root value to assign a deterministic (but different) value to each game object.
@ DeterministicFromParent
plWorld::CreateObject() will either derive a deterministic value from the parent object,...
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54
The time class encapsulates a double value storing the time in seconds.
Definition Time.h:12
PL_ALWAYS_INLINE static constexpr plTime MakeZero()
Creates an instance of plTime that was initialized with zero.
Definition Time.h:42