Leviathan  0.8.0.0
Leviathan game engine
DataStore.cpp
Go to the documentation of this file.
1 // ------------------------------------ //
2 #include "DataStore.h"
3 
6 #include "FileSystem.h"
8 
9 using namespace Leviathan;
10 // ------------------------------------ //
12 {
13  Load();
14 
15  // TODO: move these to the class definition
16  // set default values //
17  TickTime = 0;
18  TickCount = 0;
19  FrameTime = 0;
20  FPS = 0;
21 
22  FrameTimeMin = 0;
23  FrameTimeMax = 0;
24  FrameTimeAverage = 0;
25 
26 
27  FPSMin = 0;
28  FPSMax = 0;
29  FPSAverage = 0;
30 
31  // set font multiplier default //
32  FontSizeMultiplier = 1;
33 }
35 {
36  LEVIATHAN_ASSERT(man, "this shouldn't be called with false");
37 
38  Staticaccess = this;
39 
40  Load();
41 
42  TickCount = 0;
43 
44  // set default values //
45  TickTime = 0;
46  TickCount = 0;
47  FrameTime = 0;
48  FPS = 0;
49 
50  FrameTimeMin = 0;
51  FrameTimeMax = 0;
52  FrameTimeAverage = 0;
53 
54 
55  FPSMin = 0;
56  FPSMax = 0;
57  FPSAverage = 0;
58 
59  // set font multiplier default //
60  FontSizeMultiplier = 1;
61 
62  // register data indexes for use in Gui stuff //
63  // moved directly to object file processor
64 }
66 {
67  Save();
68 }
69 
70 DataStore* Leviathan::DataStore::Staticaccess = NULL;
71 
73 {
74  return Staticaccess;
75 }
76 // ------------------------------------ //
77 void Leviathan::DataStore::Load()
78 {
79  // load //
80  std::vector<std::shared_ptr<NamedVariableList>> tempvec;
81  FileSystem::LoadDataDump(GetPersistStorageFile(), tempvec, Logger::Get());
82 
83  Values.SetVec(tempvec);
84 
85  // All loaded from file will also be saved again //
86  Persistencestates.resize(tempvec.size(), true);
87 }
88 
89 void Leviathan::DataStore::Save()
90 {
91  std::string tosave = "";
92  std::vector<std::shared_ptr<NamedVariableList>>* tempvec = Values.GetVec();
93 
94  for(unsigned int i = 0; i < Persistencestates.size(); i++) {
95  if(Persistencestates[i]) {
96 
97  try {
98 
99  tosave += tempvec->at(i)->ToText(0);
100 
101  } catch(const InvalidType& e) {
102 
103  Logger::Get()->Error("DataStore: failed to serialize value \"" +
104  tempvec->at(i)->GetName() + "\": ");
105  e.PrintToLog();
106  continue;
107  }
108 
109  if(i + 1 < Persistencestates.size()) {
110  tosave += "\n";
111  }
112  }
113  }
114 
115  FileSystem::WriteToFile(tosave, GetPersistStorageFile());
116 }
117 // ------------------------------------ //
119  const std::string& name, VariableBlock& receiver) const
120 {
121  return Values.GetValue(name, receiver);
122 }
123 
124 DLLEXPORT size_t Leviathan::DataStore::GetValueCount(const std::string& name) const
125 {
126  return Values.GetValueCount(name);
127 }
128 
130  const std::string& name, std::vector<const VariableBlock*>& receiver) const
131 {
132  return Values.GetValues(name, receiver);
133 }
134 // ------------------------------------ //
135 void Leviathan::DataStore::SetPersistance(unsigned int index, bool toset)
136 {
137 
138  Persistencestates[index] = toset;
139 }
140 void DataStore::SetPersistance(const std::string& name, bool toset)
141 {
142  auto index = Values.Find(name);
143 
144  if(!Values.IsIndexValid(index))
145  throw Exception("SetPersitance called for non-existent value");
146 
147  Persistencestates[index] = toset;
148 }
149 int DataStore::GetPersistance(unsigned int index) const
150 {
151 
152  return Persistencestates[index];
153 }
154 int DataStore::GetPersistance(const std::string& name) const
155 {
156  auto index = Values.Find(name);
157 
158  if(!Values.IsIndexValid(index))
159  throw Exception("GetPersistance called for non-existent value");
160 
161  return Persistencestates[index];
162 }
163 // ------------------------------------ //
165  const std::string& name, const VariableBlock& value1)
166 {
167  // use variable holder to do this //
168  Values.SetValue(name, value1);
169 
170  // send update to value listeners //
171  ValueUpdate(name);
172 
173  return true;
174 }
175 
176 DLLEXPORT bool Leviathan::DataStore::SetValue(const std::string& name, VariableBlock* value1)
177 {
178  // use variable holder to do this //
179  Values.SetValue(name, value1);
180 
181  // send update to value listeners //
182  ValueUpdate(name);
183 
184  return true;
185 }
186 
188  const std::string& name, const std::vector<VariableBlock*>& values)
189 {
190  // use variable holder to do this //
191  this->Values.SetValue(name, values);
192 
193  // send update to value listeners //
194  ValueUpdate(name);
195 
196  return true;
197 }
198 
200 {
201  // send update to value listeners //
202  ValueUpdate(nameandvalues.GetName());
203 
204  return true;
205 }
206 
207 // ----------------------------------------------- //
208 DLLEXPORT void Leviathan::DataStore::AddVar(std::shared_ptr<NamedVariableList> values)
209 {
210  if(Values.Find(values->GetName()) > Values.GetVariableCount()) {
211  // can add new //
212  this->Values.AddVar(values);
213 
214  // don't forget to add persistence //
215  Persistencestates.push_back(false);
216  }
217 }
218 
220 {
221  if(index >= Values.GetVariableCount())
222  return;
223 
224  // remove from store //
225  Values.Remove(index);
226  // don't forget to remove from persistence states //
227  Persistencestates.erase(Persistencestates.begin() + index);
228 }
229 
230 DLLEXPORT void Leviathan::DataStore::Remove(const std::string& name)
231 {
232  // call overload //
233  Remove(Values.Find(name));
234 }
235 // ----------------------------------------------- //
236 DLLEXPORT int Leviathan::DataStore::GetVariableType(const std::string& name) const
237 {
238  return Values.GetVariableType(name);
239 }
240 
241 DLLEXPORT int Leviathan::DataStore::GetVariableTypeOfAll(const std::string& name) const
242 {
243  return Values.GetVariableTypeOfAll(name);
244 }
245 // ------------------------------------ //
247  AutoUpdateableObject* object, DataListener* listen)
248 {
249  // set into the map //
250 
251  DataListenHolder* tmpptre = Listeners[object].get();
252 
253  if(tmpptre == NULL) {
254  // new required //
255  // add back to map //
256  Listeners[object] = std::make_shared<DataListenHolder>();
257 
258  // recurse to use the new object //
259  return RegisterListener(object, listen);
260  }
261 
262  // can add new one //
263  tmpptre->HandledListeners.push_back(listen);
264 }
265 
267  AutoUpdateableObject* object, int valueid, const std::string& name, bool all)
268 {
269  if(all) {
270  // just erase the bulk //
271  Listeners.erase(object);
272  return;
273  }
274 
275  // get pointer to block //
276  DataListenHolder* tmpptre = Listeners[object].get();
277 
278  if(tmpptre == NULL) {
279  return;
280  }
281 
282  // erase the wanted ones //
283  for(size_t i = 0; i < tmpptre->HandledListeners.size(); i++) {
284  if(tmpptre->HandledListeners[i]->ListenIndex == valueid) {
285  // check name if wanted //
286  if(name.size() == 0 || name == tmpptre->HandledListeners[i]->VarName) {
287  // erase //
288 
289  SAFE_DELETE(tmpptre->HandledListeners[i]);
290  tmpptre->HandledListeners.erase(tmpptre->HandledListeners.begin() + i);
291 
292  break;
293  }
294  }
295  }
296 }
297 // ------------------------------------ //
299 {
300  return TickTime;
301 }
302 
304 {
305  return TickCount;
306 }
307 
309 {
310  return TicksBehind;
311 }
312 
314 {
315  return FrameTime;
316 }
317 
318 void Leviathan::DataStore::ValueUpdate(int index)
319 {
320  std::shared_ptr<NamedVariableList> updatedval(nullptr);
321 
322  for(auto iter = Listeners.begin(); iter != Listeners.end(); ++iter) {
323  // iterate held indexes //
324 
325  for(size_t i = 0; i < iter->second->HandledListeners.size(); i++) {
326  // check for match //
327 
328  if(iter->second->HandledListeners[i]->ListenIndex == index) {
329  // check is value fine or not //
330  if(updatedval.get() == NULL) {
331  // create //
332  updatedval = std::make_shared<NamedVariableList>(
333  Convert::ToString(index), new IntBlock(GetValueFromValIndex(index)));
334  }
335 
336  // send update //
337  iter->first->OnUpdate(updatedval);
338  }
339  }
340  }
341 }
342 
343 void Leviathan::DataStore::ValueUpdate(const std::string& name)
344 {
345  std::shared_ptr<NamedVariableList> updatedval(nullptr);
346 
347  for(auto iter = Listeners.begin(); iter != Listeners.end(); ++iter) {
348  // iterate held indexes //
349  for(size_t i = 0; i < iter->second->HandledListeners.size(); i++) {
350  // check for match //
351  if(iter->second->HandledListeners[i]->VarName == name) {
352  // check is value fine or not //
353  if(updatedval.get() == NULL) {
354  // create //
355  updatedval = std::shared_ptr<NamedVariableList>(new NamedVariableList(name,
356  new VariableBlock(
357  Values.GetValue(name)->GetBlockConst()->AllocateNewFromThis())));
358  }
359 
360  // send update //
361  iter->first->OnUpdate(updatedval);
362  }
363  }
364  }
365 }
366 
367 
368 
370 {
371  Height = newval;
372  ValueUpdate(DATAINDEX_HEIGHT);
373 }
374 
376 {
377  Width = newval;
378  ValueUpdate(DATAINDEX_WIDTH);
379 }
380 
382 {
383  Gui = newval;
384  // ValueUpdate(4);
385 }
386 
388 {
389  FPS = newval;
390  ValueUpdate(4);
391 }
392 
394 {
395  FrameTime = newval;
396  ValueUpdate(3);
397 }
398 
400 {
401  TickCount = newval;
402  ValueUpdate(2);
403 }
404 
406 {
407  TickTime = newval;
408  ValueUpdate(1);
409 }
410 
412 {
413  TicksBehind = value;
414 }
415 
417 {
418  return Gui;
419 }
420 
422 {
423  return Width;
424 }
425 
427 {
428  return Height;
429 }
430 
432 {
433  return FPS;
434 }
435 
437 {
438  switch(valindex) {
439  case DATAINDEX_TICKTIME: {
440  return TickTime;
441  } break;
442  case DATAINDEX_TICKCOUNT: {
443  return TickCount;
444  } break;
445  case DATAINDEX_FRAMETIME: {
446  return FrameTime;
447  } break;
448  case DATAINDEX_FPS: {
449  return FPS;
450  } break;
451  case DATAINDEX_HEIGHT: {
452  return Height;
453  } break;
455  return FrameTimeMax;
456  } break;
458  return FrameTimeMin;
459  } break;
461  return FrameTimeAverage;
462  } break;
463  case DATAINDEX_FPS_MIN: {
464  return FPSMin;
465  } break;
466  case DATAINDEX_FPS_MAX: {
467  return FPSMax;
468  } break;
469  case DATAINDEX_FPS_AVERAGE: {
470  return FPSAverage;
471  } break;
472  }
473  return -1;
474 }
475 
476 
478 {
479  return FrameTimeMin;
480 }
481 
483 {
484  return FrameTimeMax;
485 }
486 
488 {
489  return FrameTimeAverage;
490 }
491 
493 {
494  return FPSMax;
495 }
496 
498 {
499  return FPSMax;
500 }
501 
503 {
504  return FPSAverage;
505 }
506 
508 {
509  FrameTimeMin = newval;
510  ValueUpdate(DATAINDEX_FRAMETIME_MIN);
511 }
512 
514 {
515  FrameTimeMax = newval;
516  ValueUpdate(DATAINDEX_FRAMETIME_MAX);
517 }
518 
520 {
521  FrameTimeAverage = newval;
522  ValueUpdate(DATAINDEX_FRAMETIME_AVERAGE);
523 }
524 
526 {
527  FPSMin = newval;
528  ValueUpdate(DATAINDEX_FPS_MIN);
529 }
530 
532 {
533  FPSMax = newval;
534  ValueUpdate(DATAINDEX_FPS_MAX);
535 }
536 
538 {
539  FPSAverage = newval;
540  ValueUpdate(DATAINDEX_FPS_AVERAGE);
541 }
542 
544 {
545  return FontSizeMultiplier;
546 }
547 
549 {
550  FontSizeMultiplier = newval;
551 }
552 // ------------------------------------ //
554 {
555 
556  std::string logName = StringOperations::RemoveEnding<std::string>(
557  StringOperations::RemoveExtension(Logger::Get()->GetLogFile(), false), "Log");
558 
559  return logName + "Persist.txt";
560 }
561 // ----------------------------------------------- //
562 // DataListener
564 {
565  ListenIndex = -1;
566  ListenOnIndex = false;
567  VarName = "";
568 }
569 
571  int index, bool onindex, const std::string& var)
572 {
573 
574  ListenIndex = index;
575  ListenOnIndex = onindex;
576  VarName = var;
577 }
578 // ------------------------------------ //
DLLEXPORT int GetFontSizeMultiplier() const
Definition: DataStore.cpp:543
#define DATAINDEX_FRAMETIME
Definition: DataStore.h:13
DLLEXPORT int GetHeight() const
Definition: DataStore.cpp:426
#define DATAINDEX_FRAMETIME_MIN
Definition: DataStore.h:19
DLLEXPORT void SetFPS(int newval)
Definition: DataStore.cpp:387
hosts one or more VariableBlocks keeping only one name for all of them
Definition: NamedVars.h:27
#define DATAINDEX_TICKTIME
Definition: DataStore.h:11
DLLEXPORT void SetFPSAverage(int newval)
Definition: DataStore.cpp:537
DLLEXPORT int GetFPSMax() const
Definition: DataStore.cpp:497
DLLEXPORT void SetFPSMax(int newval)
Definition: DataStore.cpp:531
DLLEXPORT int GetTicksBehind() const
Definition: DataStore.cpp:308
DLLEXPORT int GetFPS() const
Definition: DataStore.cpp:431
DLLEXPORT int GetFPSMin() const
Definition: DataStore.cpp:492
DLLEXPORT void SetFontSizeMultiplier(int newval)
Definition: DataStore.cpp:548
Non-template class for working with all types of DataBlocks.
Definition: DataBlock.h:425
#define DATAINDEX_TICKCOUNT
Definition: DataStore.h:12
DLLEXPORT DataStore()
Definition: DataStore.cpp:11
DLLEXPORT bool IsIndexValid(size_t index) const
Definition: NamedVars.h:232
DLLEXPORT int GetFrameTime() const
Definition: DataStore.cpp:313
DLLEXPORT int GetTickTime() const
Definition: DataStore.cpp:298
DLLEXPORT int GetFPSAverage() const
Definition: DataStore.cpp:502
static const StringTypeN RemoveExtension(const StringTypeN &filepath, bool delpath=true)
DLLEXPORT int GetFrameTimeMin() const
Definition: DataStore.cpp:477
static DLLEXPORT std::string GetPersistStorageFile()
Definition: DataStore.cpp:553
DLLEXPORT int GetWidth() const
Definition: DataStore.cpp:421
size_t Find(const std::string &name) const
Definition: NamedVars.h:367
Base class for all exceptions thrown by Leviathan.
Definition: Exceptions.h:10
DLLEXPORT void SetFrameTimeAverage(int newval)
Definition: DataStore.cpp:519
#define DATAINDEX_FRAMETIME_AVERAGE
Definition: DataStore.h:20
#define DATAINDEX_FPS_AVERAGE
Definition: DataStore.h:24
#define DATAINDEX_FPS
Definition: DataStore.h:14
DLLEXPORT int GetPersistance(unsigned int index) const
Definition: DataStore.cpp:149
DLLEXPORT int GetFrameTimeMax() const
Definition: DataStore.cpp:482
DLLEXPORT void SetFPSMin(int newval)
Definition: DataStore.cpp:525
#define DATAINDEX_HEIGHT
Definition: DataStore.h:16
DLLEXPORT void SetFrameTimeMax(int newval)
Definition: DataStore.cpp:513
#define DATAINDEX_FPS_MIN
Definition: DataStore.h:22
static DLLEXPORT DataStore * Get()
Definition: DataStore.cpp:72
DLLEXPORT void SetFrameTimeMin(int newval)
Definition: DataStore.cpp:507
DLLEXPORT ~DataStore()
Definition: DataStore.cpp:65
DLLEXPORT void SetHeight(int newval)
Definition: DataStore.cpp:369
#define LEVIATHAN_ASSERT(x, msg)
Definition: Define.h:98
static std::string ToString(const T &val)
Definition: Convert.h:72
DLLEXPORT bool SetValue(const std::string &name, const VariableBlock &value1)
Definition: DataStore.cpp:164
DLLEXPORT void SetTicksBehind(int value)
Definition: DataStore.cpp:411
DataBlock< int > IntBlock
Definition: DataBlock.h:382
DLLEXPORT void SetPersistance(unsigned int index, bool toset)
Definition: DataStore.cpp:135
DLLEXPORT void RegisterListener(AutoUpdateableObject *object, DataListener *listen)
Definition: DataStore.cpp:246
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
DLLEXPORT int GetGUiActive() const
Definition: DataStore.cpp:416
DLLEXPORT void RemoveListener(AutoUpdateableObject *object, int valueid, const std::string &name="", bool all=false)
Definition: DataStore.cpp:266
DLLEXPORT void Remove(size_t index)
Definition: DataStore.cpp:219
DLLEXPORT void SetGUiActive(int newval)
Definition: DataStore.cpp:381
DLLEXPORT bool GetValues(const std::string &name, std::vector< const VariableBlock *> &receiver) const
Definition: DataStore.cpp:129
#define DATAINDEX_FPS_MAX
Definition: DataStore.h:23
#define DATAINDEX_FRAMETIME_MAX
Definition: DataStore.h:18
DLLEXPORT int GetTickCount() const
Definition: DataStore.cpp:303
static DLLEXPORT bool LoadDataDump(const std::string &file, std::vector< std::shared_ptr< NamedVariableList >> &vec, LErrorReporter *errorreport)
Definition: FileSystem.cpp:304
#define DLLEXPORT
Definition: Include.h:84
DLLEXPORT int GetValueFromValIndex(int valindex) const
Definition: DataStore.cpp:436
DLLEXPORT void SetFrameTime(int newval)
Definition: DataStore.cpp:393
DLLEXPORT int GetVariableType(const std::string &name) const
Definition: DataStore.cpp:236
DLLEXPORT const VariableBlock * GetValue(const std::string &name) const
DLLEXPORT void SetTickTime(int newval)
Definition: DataStore.cpp:405
DLLEXPORT size_t GetValueCount(const std::string &name) const
Definition: DataStore.cpp:124
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
#define SAFE_DELETE(x)
Definition: Define.h:147
DLLEXPORT int GetVariableTypeOfAll(const std::string &name) const
Definition: DataStore.cpp:241
DLLEXPORT void Error(const std::string &data) override
Definition: Logger.cpp:177
DLLEXPORT DataListener()
Definition: DataStore.cpp:563
std::vector< DataListener * > HandledListeners
Definition: DataStore.h:51
DLLEXPORT void AddVar(std::shared_ptr< NamedVariableList > values)
Definition: DataStore.cpp:208
#define DATAINDEX_WIDTH
Definition: DataStore.h:15
static DLLEXPORT bool WriteToFile(const std::string &data, const std::string &filename)
Definition: FileSystem.cpp:456
std::string GetName() const
Definition: NamedVars.h:139
DLLEXPORT void SetTickCount(int newval)
Definition: DataStore.cpp:399
DLLEXPORT int GetFrameTimeAverage() const
Definition: DataStore.cpp:487
virtual DLLEXPORT void PrintToLog() const noexcept
Definition: Exceptions.cpp:35
DLLEXPORT void SetWidth(int newval)
Definition: DataStore.cpp:375