Norbert's Game Engine v200
Loading...
Searching...
No Matches
eWorld Class Reference

#include <World.hpp>

Inheritance diagram for eWorld:
eObject eBase

Public Member Functions

bool Initialize ()
 
void Update ()
 
void Render ()
 
void Unload ()
 
template<typename T >
bool InsertDoer (eString name)
 
template<typename T >
T * SpawnDoer (eString name)
 
eDoer * GetDoer (eString name)
 
void Save (eString outfile)
 
void Load (eString filepath, const bool forceload=false)
 
virtual eString OnSave_info ()
 
virtual void OnSave (EDR::Archive *archive)
 
virtual void OnLoad_info (eString &info)
 
virtual void OnLoad (EDR::Archive *archive)
 
eString GetFilepath ()
 
- Public Member Functions inherited from eObject
void Sleep ()
 
void Awake ()
 
bool IsAwake ()
 
- Public Member Functions inherited from eBase
template<typename T >
T * Cast (eBase *obj)
 Cast class to base.
 

Static Public Member Functions

static eFactory< eDoer > & Factory ()
 
template<typename T >
static void RegisterPropertyType (ePropertyType type)
 
static void PopPropertyTypes ()
 
static void INIT ()
 

Additional Inherited Members

- Protected Attributes inherited from eObject
bool bAwake = false
 

Detailed Description

World class for managing doers

Definition at line 26 of file World.hpp.

Member Function Documentation

◆ Factory()

eFactory< eDoer > & eWorld::Factory ( )
static

Definition at line 102 of file World.cpp.

103{
104 return mFactory;
105}

◆ GetDoer()

eDoer * eWorld::GetDoer ( eString name)

Definition at line 96 of file World.cpp.

97{
98 if (!mDoers.Find(name)) return nullptr;
99 return mDoers[name];
100}

◆ GetFilepath()

eString eWorld::GetFilepath ( )

Definition at line 353 of file World.cpp.

354{
355 return mFilepath;
356}

◆ INIT()

void eWorld::INIT ( )
static

Definition at line 359 of file World.cpp.

360{
361 RegisterPropertyType<int>( { stp_int, stp_int_ });
362 RegisterPropertyType<int8>( { stp_int8, stp_int8_ });
363 RegisterPropertyType<int16>( { stp_int16, stp_int16_ });
364 RegisterPropertyType<int32>( { stp_int32, stp_int32_ });
365 RegisterPropertyType<int64>( { stp_int64, stp_int64_ });
366 RegisterPropertyType<uint>( { stp_uint, stp_uint_ });
367 RegisterPropertyType<uint8>( { stp_uint8, stp_uint8_ });
368 RegisterPropertyType<uint16>( { stp_uint16, stp_uint16_ });
369 RegisterPropertyType<uint32>( { stp_uint32, stp_uint32_ });
370 RegisterPropertyType<uint64>( { stp_uint64, stp_uint64_ });
371 RegisterPropertyType<real3>( { stp_Real3, stp_Real3_ });
372 RegisterPropertyType<real6>( { stp_Real6, stp_Real6_ });
373 RegisterPropertyType<bool>( { stp_Bool, stp_Bool_ });
374 RegisterPropertyType<vec2>( { stp_Vec2, stp_Vec2_ });
375 RegisterPropertyType<vec3>( { stp_Vec3, stp_Vec3_ });
376 RegisterPropertyType<vec4>( { stp_Vec4, stp_Vec4_ });
377 RegisterPropertyType<eString>( { stp_String, stp_String_ });
378}

◆ Initialize()

bool eWorld::Initialize ( )
virtual

Reimplemented from eObject.

Definition at line 13 of file World.cpp.

14{
15 bool rtVl = true;
16
17 int amount = mDoers.Size();
18 for (int i = 0; i < amount; i++)
19 {
20 eDoer* it2 = mDoers[i];
21 for (auto& it3 : it2->GetModules().Geta())
22 {
23 auto& it4 = it3.second;
24 it4->SetParent(it2);
25 if (!it4->Initialize())
26 {
27 rtVl = false;
28 eConsole::PrintLog("ERROR::eWorld::Initialize", "Module '" + it3.first.GetStrg() + "' inside '" + mDoers.IdAt(i).GetStrg() + "' returned false!");
30 }
31 it4->Awake();
32 }
33 it2->SetWorld(this);
34 if (!it2->Initialize())
35 {
36 rtVl = false;
37 eConsole::PrintLog("ERROR::eWorld::Initialize", "Doer '" + mDoers.IdAt(i).GetStrg() + "' returned false!");
39 }
40 it2->Awake();
41 }
42 return rtVl;
43}
static void WriteToDisk()
Write the console text to a crash file called 'CRASH.log'.
Definition Console.cpp:40
static void PrintLog(eString head, eString msg)
Print a log.
Definition Console.cpp:22
Definition Doer.hpp:20
const strg & GetStrg() const
Return text as std::string.
Definition String.cpp:70

◆ InsertDoer()

template<typename T >
bool eWorld::InsertDoer ( eString name)
inline

Definition at line 35 of file World.hpp.

36 {
37 if (mDoers.Find(name)) return false;
38 mDoers.Push(std::make_unique<T>(), name);
39 mDoers[name]->mClassName = typeid(T).name();
40 return true;
41 }

◆ Load()

void eWorld::Load ( eString filepath,
const bool forceload = false )

Definition at line 207 of file World.cpp.

208{
209 if (!EDR::FileExists(filepath.Get()))
210 {
211 eConsole::PrintLog("ERROR::eWorld::Load", "World file '" + filepath.GetStrg() + "' could not be found!");
212 return;
213 }
214
215 mFilepath = filepath;
216
217 EDR::Archive* archive = EDR::OpenArchive(filepath.Get());
218
219 EDR::ZSTAT _INFO = EDR::GetArchiveFileContents(archive, "info");
220 strg INFO(_INFO.c, _INFO.size);
221 INFO = EDR::ReadBinaryToString(INFO);
222 delete[] _INFO.c;
223
224 const uint16 INFENV = static_cast<uint16>(EDR::GetIntI(INFO, "EngineVersion"));
225 if (INFENV != NGE_ENGINE_INFO_VERSION)
226 {
227 eConsole::PrintLog("WARNING::eWorld::Load", "World was created using a different engine version that may be incompatible!");
228 eConsole::PrintLine(TEXT("World: v") + eString::ToString(INFENV));
229 eConsole::PrintLine(TEXT("Engine: v") + eString::ToString(NGE_ENGINE_INFO_VERSION));
230 }
231
232 const strg COMPNAME = EDR::GetStringI(INFO, "Compiler");
233 if (COMPNAME != NGE_COMPILER_NAME)
234 {
235 eConsole::PrintLog("ERROR::eWorld::Load", "Invalid runtime library!");
236 eConsole::PrintLine(TEXT("World: ") + COMPNAME);
237 eConsole::PrintLine(TEXT("Engine: ") + TEXT(NGE_COMPILER_NAME));
239 archive = EDR::CloseArchive(archive);
240 return;
241 }
242
243 const int COMPV = EDR::GetIntI(INFO, "CompilerVersion");
244 if(COMPV != NGE_COMPILER_VERSION)
245 {
246 eConsole::PrintLog("WARNING::eWorld::Load", "Different compiler version detected!");
247 eConsole::PrintLine(TEXT("World: ") + eString::ToString(COMPV));
248 eConsole::PrintLine(TEXT("Engine: ") + eString::ToString(NGE_COMPILER_VERSION));
249 }
250
251 eString _inftmp = INFO;
252 OnLoad_info(_inftmp);
253 INFO = _inftmp.GetStrg();
254
255 const uint doera = EDR::GroupItemAmountI(INFO, "DOERS");
256 for (uint i = 0; i < doera; i++)
257 {
258 eString doer_name = EDR::GroupItemNameI(INFO, "DOERS", i);
259 doer_name.Replace('|', '-');
260 EDR::ZSTAT _doer_info = EDR::GetArchiveFileContents(archive, TEXT(doer_name + ".doer").Get());
261 strg doer_info(_doer_info.c, _doer_info.size);
262 doer_info = EDR::ReadBinaryToString(doer_info);
263 doer_name.Replace('-', ' ');
264 delete[] _doer_info.c;
265
266 eString classname = EDR::GetStringI(doer_info, "class");
267 classname.Replace('|', ' ');
268
269 if (!mFactory.ClassExists(classname))
270 {
271 eConsole::PrintLog("ERROR::eWorld::Load", TEXT("Class '") + classname + TEXT("' was not registered!"));
272 if (!forceload)
273 {
275 archive = EDR::CloseArchive(archive);
276 return;
277 }
278 }
279
280 mDoers.Push(mFactory.CreateObject(classname), doer_name);
281 eDoer* obj = mDoers.Get(doer_name);
282 obj->mClassName = classname;
283
284 for (auto& it : obj->Properties().Geta().Geta())
285 {
286 eString prop_name = it.first;
287 prop_name.Replace(' ', '|');
288 if (!mPropTypes.Find(it.second.mType))
289 {
290 eConsole::PrintLog("WARNING::eWorld::Load", "Property type '" + it.second.mType.GetStrg() + "' was not registered!");
291 continue;
292 }
293 mPropTypes[it.second.mType].mOnLoad(it.second.mValue, EDR::GetStringGI(doer_info, prop_name.GetStrg(), "PROPS"));
294 }
295
296 const uint taga = static_cast<uint>(EDR::GetIntGI(doer_info, "a", "TAGS"));
297 for (uint f = 0; f < taga; f++)
298 {
299 eString tagvl = EDR::GetStringGI(doer_info, "t" + std::to_string(f), "TAGS");
300 tagvl.Replace('|', ' ');
301 obj->Tags().Push(tagvl);
302 }
303
304 for (auto& it : obj->GetModules().Geta())
305 {
306 eString mod_name = it.first;
307 mod_name.Replace(' ', '|');
308
309 for (auto& prop : it.second->Properties().Geta().Geta())
310 {
311 if (!mPropTypes.Find(prop.second.mType))
312 {
313 eConsole::PrintLog("WARNING::eWorld::Load", "Property type '" + prop.second.mType.GetStrg() + "' was not registered!");
314 continue;
315 }
316 eString prop_name = prop.first;
317 prop_name.Replace(' ', '|');
318 mPropTypes[prop.second.mType].mOnLoad(prop.second.mValue, EDR::GetStringGI(doer_info, prop_name.GetStrg(), mod_name.GetStrg()));
319 }
320 }
321 }
322
323 OnLoad(archive);
324
325 archive = EDR::CloseArchive(archive);
326}
static void PrintLine(eString msg)
Print the string in a new line.
Definition Console.cpp:16
std::vector< std::pair< eString, T > > & Geta()
Returns collection.
Definition Map.hpp:96
bool Find(eString name)
Returns true if item with such a name exists!
Definition Map.hpp:129
void Replace(char th, char withMe)
Replaces a character with another one.
Definition String.cpp:132
const char * Get()
Return text as c-string.
Definition String.cpp:64
Archive * OpenArchive(const char *directory)
Open ZIP archive.
Definition EDR.cpp:462
strg GetStringI(strg text, strg token, const bool binary=false)
Return the tokens value from memory as a std::string.
Definition EDR.cpp:168
strg ReadBinaryToString(strg &content)
Read binary text from memory.
Definition EDR.cpp:450
int GetIntGI(strg file, strg token, strg group, const bool binary=false)
From memory.
Definition EDR.cpp:1161
strg GetStringGI(strg file, strg token, strg group, const bool binary=false)
From memory.
Definition EDR.cpp:1103
uint GroupItemAmountI(strg file, strg group, const bool binary=false)
From memory.
Definition EDR.cpp:1005
ZSTAT GetArchiveFileContents(Archive *z, const char *name)
Returns a files content from an archive. WARNING: RELEASE contents AFTER USE!! MEMORY LEAK!...
Definition EDR.cpp:537
int GetIntI(strg text, strg token, const bool binary=false)
Return the tokens value from memory as an int.
Definition EDR.cpp:214
Archive * CloseArchive(Archive *z)
Closes archive. Changes to archive only take effect once this is called. Memory of written files must...
Definition EDR.cpp:604
strg GroupItemNameI(strg file, strg group, uint place, const bool binary=false)
From memory.
Definition EDR.cpp:1069

◆ OnLoad()

void eWorld::OnLoad ( EDR::Archive * archive)
virtual

Definition at line 343 of file World.cpp.

344{
345 return;
346}

◆ OnLoad_info()

void eWorld::OnLoad_info ( eString & info)
virtual

Definition at line 338 of file World.cpp.

339{
340 return;
341}

◆ OnSave()

void eWorld::OnSave ( EDR::Archive * archive)
virtual

Definition at line 333 of file World.cpp.

334{
335 return;
336}

◆ OnSave_info()

eString eWorld::OnSave_info ( )
virtual

Definition at line 328 of file World.cpp.

329{
330 return TEXT("");
331}

◆ PopPropertyTypes()

void eWorld::PopPropertyTypes ( )
static

Definition at line 348 of file World.cpp.

349{
350 mPropTypes.Popa();
351}
void Popa()
Clear the collection.
Definition Map.hpp:65

◆ RegisterPropertyType()

template<typename T >
static void eWorld::RegisterPropertyType ( ePropertyType type)
inlinestatic

Definition at line 79 of file World.hpp.

80 {
81 strg name(typeid(T).name());
82 if (mPropTypes.Find(name)) return;
83 mPropTypes.Push(type, name);
84 }
void Push(T vl, eString id="")
Push an item to the collection.
Definition Map.hpp:19

◆ Render()

void eWorld::Render ( )
virtual

Reimplemented from eObject.

Definition at line 76 of file World.cpp.

77{
78 int amount = mDoers.Size();
79 for (int i = 0; i < amount; i++)
80 {
81 eDoer* it2 = mDoers[i];
82 for (auto& it3 : it2->GetModules().Geta())
83 {
84 auto& it4 = it3.second;
85 if (it4->bCanRender) it4->Render();
86 }
87 if (it2->bCanRender) it2->Render();
88 }
89}

◆ Save()

void eWorld::Save ( eString outfile)

Definition at line 107 of file World.cpp.

108{
109 mFilepath = outfile;
110
111 if (EDR::FileExists(outfile.Get()))
112 EDR::RemoveFile(outfile.Get());
113
114 EDR::Archive* archive = EDR::CreateArchive(outfile.Get());
115
116 // Info file
117 eString infOut = TEXT("NGE WORLD FILE\n");
118 infOut += TEXT("EngineVersion=") + eString::ToString(NGE_ENGINE_INFO_VERSION) + TEXT("\n");
119 infOut += TEXT("Compiler=") + TEXT(NGE_COMPILER_NAME) + TEXT("\nCompilerVersion=") + eString::ToString(NGE_COMPILER_VERSION) + TEXT("\n");
120
121 infOut += "<DOERS>\n";
122 for (auto& it : mDoers.Geta())
123 {
124 eString name = it.first;
125 name.Replace(' ', '|');
126 infOut += TEXT("<") + name + TEXT(">\n");
127 }
128 infOut += "</DOERS>\n";
129
130 infOut += OnSave_info();
131
132 strg OUTME = infOut.GetStrg();
133 OUTME = EDR::WriteBinaryToString(OUTME);
134 EDR::AddBinaryToArchive(archive, "info", OUTME.c_str(), OUTME.size());
135
136 // Doers
137 std::vector<strg> TMPSVSTOR;
138 for (auto& it : mDoers.Geta())
139 {
140 auto& obj = it.second;
141
142 eString name = it.first;
143 name.Replace(' ', '|');
144
145 eString objclass = obj->mClassName;
146 objclass.Replace(' ', '|');
147 eString OUT = TEXT("class=") + objclass + TEXT("\n");
148
149 OUT += TEXT("<PROPS>\n");
150 for (auto& prop : obj->Properties().Geta().Geta())
151 {
152 if (!mPropTypes.Find(prop.second.mType))
153 {
154 eConsole::PrintLog("WARNING::eWorld::Save", "Property type '" + prop.second.mType.GetStrg() + "' was not registered!");
155 continue;
156 }
157 eString pname = prop.first;
158 pname.Replace(' ', '|');
159 OUT += pname + TEXT("=") + mPropTypes[prop.second.mType].mOnSave(prop.second.mValue) + TEXT("\n");
160 }
161 OUT += TEXT("</PROPS>\n");
162
163 OUT += TEXT("<TAGS>\n");
164 const uint64 tsize = obj->Tags().GetCollection().size();
165 OUT += TEXT("a=") + eString::ToString(tsize) + TEXT("\n");
166 for(uint64 i = 0; i < tsize; i++)
167 {
168 auto& tag = obj->Tags().GetCollection()[i];
169 eString fintag = tag;
170 fintag.Replace(' ', '|');
171 OUT += TEXT("t") + eString::ToString(i) + TEXT("=") + fintag + TEXT("\n");
172 }
173 OUT += TEXT("</TAGS>\n");
174
175 for (auto& it : obj->GetModules().Geta())
176 {
177 eString modname = it.first;
178 modname.Replace(' ', '|');
179 OUT += TEXT("<") + modname + TEXT(">\n");
180 for (auto& prop : it.second->Properties().Geta().Geta())
181 {
182 if (!mPropTypes.Find(prop.second.mType))
183 {
184 eConsole::PrintLog("WARNING::eWorld::Save", "Property type '" + prop.second.mType.GetStrg() + "' was not registered!");
185 continue;
186 }
187 eString pname = prop.first;
188 pname.Replace(' ', '|');
189 OUT += pname + TEXT("=") + mPropTypes[prop.second.mType].mOnSave(prop.second.mValue) + TEXT("\n");
190 }
191 OUT += TEXT("</") + modname + TEXT(">\n");
192 }
193
194 TMPSVSTOR.push_back(OUT.GetStrg());
195 strg& curstorspc = TMPSVSTOR[TMPSVSTOR.size() - 1];
196 curstorspc = EDR::WriteBinaryToString(curstorspc);
197 name.Replace('|', '-');
198 strg outfname = name.GetStrg() + ".doer";
199 EDR::AddBinaryToArchive(archive, outfname.c_str(), curstorspc.c_str(), curstorspc.size());
200 }
201
202 OnSave(archive);
203
204 archive = EDR::CloseArchive(archive);
205}
strg WriteBinaryToString(strg &content)
Write binary text to memory.
Definition EDR.cpp:440
void AddBinaryToArchive(Archive *z, const char *filename, const char *content, uint64 size)
Adds binary file to archive.
Definition EDR.cpp:501
void RemoveFile(const char *filePath)
Removes a file from the drive.
Definition EDR.cpp:371
Archive * CreateArchive(const char *directory)
Creates ZIP archive.
Definition EDR.cpp:476

◆ SpawnDoer()

template<typename T >
T * eWorld::SpawnDoer ( eString name)
inline

Definition at line 44 of file World.hpp.

45 {
46 if (!InsertDoer<T>(name)) return nullptr;
47 eDoer* it = mDoers[name];
48 for (auto& it2 : it->GetModules().Geta())
49 {
50 auto& it3 = it2.second;
51 if (!it3->Initialize())
52 {
53 eConsole::PrintLog("ERROR::eWorld::SpawnDoer", "Module '" + it2.first.GetStrg() + "' inside '" + name.GetStrg() + "' returned false!");
55 }
56 it3->Awake();
57 }
58 if (!it->Initialize())
59 {
60 eConsole::PrintLog("ERROR::eWorld::SpawnDoer", "Doer '" + name.GetStrg() + "' returned false!");
62 }
63 it->Awake();
64 return reinterpret_cast<T*>(it);
65 }

◆ Unload()

void eWorld::Unload ( )
virtual

Reimplemented from eObject.

Definition at line 91 of file World.cpp.

92{
93 mDoers.Popa();
94}

◆ Update()

void eWorld::Update ( )
virtual

Reimplemented from eObject.

Definition at line 45 of file World.cpp.

46{
47 std::vector<eString> toDestroy;
48
49 int amount = mDoers.Size();
50 for (int i = 0; i < amount; i++)
51 {
52 eDoer* it2 = mDoers[i];
53 if (it2->IsAwake() && it2->bCanUpdate)
54 {
55 for (auto& it3 : it2->GetModules().Geta())
56 {
57 auto& it4 = it3.second;
58 if (it4->IsAwake() && it4->bCanUpdate)
59 {
60 it4->Alarm().Update();
61 it4->Update();
62 }
63 }
64 it2->Alarm().Update();
65 it2->Update();
66
67 if (it2->ShouldDestroy())
68 toDestroy.push_back(mDoers.IdAt(i));
69 }
70 }
71
72 for (auto& it : toDestroy)
73 mDoers.Pop(it);
74}

The documentation for this class was generated from the following files: