wavelength 1.0.0
Loading...
Searching...
No Matches
rndloader.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <list>
5#include <memory>
6#include <vector>
7
8#include "os/binstream.h"
9#include "os/filepath.h"
10#include "os/mem.h"
11#include "os/string.h"
12#include "rnd/rndobject.h"
13
21class RndLoader {
22public:
29
35 class Callback {
36 public:
46 virtual void SetObjectCount([[maybe_unused]] int nObjects) {
47 }
48
58 virtual int ShouldLoad(RndObject *pExisting, const char *pszName, const char *pszClass) = 0;
59
61 virtual ~Callback() {
62 }
63 };
64
71 public:
79 ObjectLoader(RndObject *pObject, int nLoad, int nDelete)
80 : mObject(pObject), mLoad(nLoad), mDelete(nDelete) {
81 }
82
95 void Load(BinStream &stream, int nVersion);
96
105 void Release();
106
108 int GetLoad() const {
109 return mLoad;
110 }
111
114 return mObject;
115 }
116
117 private:
118 RndObject *mObject;
119 int mLoad;
120 int mDelete;
121 };
122
124 typedef void (RndLoader::*State)(float fDeadlineMs);
125
138 RndLoader(const char *pszFile, int nFlags, Callback *pCallback, BinStream *pStream);
139
147
154 void *operator new(size_t nSize) {
155 return PoolMemAlloc(static_cast<int>(nSize), "Loader", 0);
156 }
157
163 void operator delete(void *pBlock) {
164 PoolMemFree(pBlock);
165 }
166
174 bool IsLoaded() const;
175
183 void Poll(float fDeadlineMs);
184
194 void LoadFile(float fDeadlineMs);
195
205 void RemapClassName(char *pszClass);
206
217 void PreloadHeader(float fDeadlineMs);
218
231 void CreateObjects(float fDeadlineMs);
232
244 void LoadResources(float fDeadlineMs);
245
254 void LoadObjects(float fDeadlineMs);
255
263 void DeleteObjects(float fDeadlineMs);
264
272 void DoneLoading(float fDeadlineMs);
273
283 void Finish();
284
286 const String &GetFile() const {
287 return mFile;
288 }
289
291 const std::list<RndObject *> &GetObjects() const {
292 return mObjects;
293 }
294
296 std::list<RndObject *> &GetObjects() {
297 return mObjects;
298 }
299
300private:
301 State mState;
302 int mOwnsStream;
303 String mFile;
304 int mFlags;
305 String mDir;
306 BinStream *mStream;
307 std::unique_ptr<BinStream> mOwnedStream;
308 std::list<RndObject *> mObjects;
310 int mVersion;
311 int mRemaining;
312 std::vector<FilePath> mTextures;
313 std::list<ObjectLoader> mObjectLoaders;
314 std::list<ObjectLoader>::iterator mCurLoader;
315 Callback *mCallback;
316};
Stream of raw bytes, the base of the memory, file, and network streams.
Definition binstream.h:22
Interface a loader consults before it creates each object of its file.
Definition rndloader.h:35
virtual void SetObjectCount(int nObjects)
Receive the number of objects of the file before the first is created.
Definition rndloader.h:46
virtual ~Callback()
Destroy the callback.
Definition rndloader.h:61
virtual int ShouldLoad(RndObject *pExisting, const char *pszName, const char *pszClass)=0
Decide whether the loader loads an object of its file.
void Release()
Delete the object when the loader created it only to skip it.
int GetLoad() const
Definition rndloader.h:108
ObjectLoader(RndObject *pObject, int nLoad, int nDelete)
Construct the record of one object.
Definition rndloader.h:79
RndObject * GetObject() const
Definition rndloader.h:113
void Load(BinStream &stream, int nVersion)
Load the record of the object, or skip it.
Flags
Behaviour bits of a load, as RndManager::AddLoader() takes them.
Definition rndloader.h:24
@ kDeleteObjects
Definition rndloader.h:27
@ kAsync
Definition rndloader.h:25
@ kReportExisting
Definition rndloader.h:26
void LoadResources(float fDeadlineMs)
Read the texture files of the file and start reading them, then start LoadObjects().
const String & GetFile() const
Definition rndloader.h:286
std::list< RndObject * > & GetObjects()
Definition rndloader.h:296
void DoneLoading(float fDeadlineMs)
The final stage, which does nothing.
RndLoader(const char *pszFile, int nFlags, Callback *pCallback, BinStream *pStream)
Create a loader for a file.
void LoadObjects(float fDeadlineMs)
Load or skip the record of each object while the stream has data, then start DeleteObjects().
void Finish()
End the load unless it has ended, releasing the stream it opened.
void DeleteObjects(float fDeadlineMs)
Delete the objects created only to be skipped, then end the load.
void PreloadHeader(float fDeadlineMs)
Wait for the stream, read the version and the object count, then start CreateObjects().
~RndLoader()
End the load, delete the loaded objects when kDeleteObjects is set, and leave the queue.
const std::list< RndObject * > & GetObjects() const
Definition rndloader.h:291
void Poll(float fDeadlineMs)
Advance the load until a deadline.
void(RndLoader::* State)(float fDeadlineMs)
A stage of the load, which works until a deadline on the system clock in milliseconds.
Definition rndloader.h:124
void LoadFile(float fDeadlineMs)
Open a stream on the file unless one was given, then start PreloadHeader().
bool IsLoaded() const
Report whether the load has ended.
void RemapClassName(char *pszClass)
Rewrite a class name of an older version to the name the registry knows.
void CreateObjects(float fDeadlineMs)
Read the object table, find or create each object, and record what to do with its record.
Base of every object the renderer can load, resolve by name, and serialise.
Definition rndobject.h:20
Growable text buffer that is also a PrnStream.
Definition string.h:19
void * PoolMemAlloc(int nSize, const char *pszTag, int nAlign)
Allocate a block from the pool heaps and bill it to a tag.
void PoolMemFree(void *pBlock)
Return a block PoolMemAlloc() handed out to the pool heap that owns it.