Leviathan  0.8.0.0
Leviathan game engine
GameConfiguration.cpp
Go to the documentation of this file.
1 // ------------------------------------ //
2 #include "GameConfiguration.h"
3 
4 #include "FileSystem.h"
5 
6 #include <boost/filesystem.hpp>
7 using namespace Leviathan;
8 // ------------------------------------ //
10  GameConfigFile(configfile)
11 {
12  staticaccess = this;
13 }
14 
15 DLLEXPORT GameConfiguration::GameConfiguration() : GameVars(new NamedVars()), InMemory(true)
16 {
17  staticaccess = this;
18 }
19 
21 {
22  if(GameVars)
23  Release();
24 
25  staticaccess = NULL;
26 }
27 
29 {
30  return staticaccess;
31 }
32 
33 GameConfiguration* Leviathan::GameConfiguration::staticaccess = NULL;
34 // ------------------------------------ //
36  std::function<void(Lock& guard, GameConfiguration* configobj)> sanitycheckcallback)
37 {
38  GUARD_LOCK();
39 
40  if(!InMemory) {
41 
42  GameVars = new NamedVars();
43 
44  if(!GameVars->LoadVarsFromFile(GameConfigFile, Logger::Get())) {
45 
46  if(boost::filesystem::exists(GameConfigFile)) {
47  // Unknown error //
48  Logger::Get()->Error("GameConfiguration: Unknown error from LoadVarsFromFile");
49  return false;
50  }
51  // Ignore missing file
52  }
53  }
54 
55  // First verify the global variables //
56  VerifyGlobalVariables(guard);
57 
58  // Call the checking function //
59  if(sanitycheckcallback)
60  sanitycheckcallback(guard, this);
61  return true;
62 }
63 
65 {
66  SaveCheck();
67 
68  // We can now delete our variables //
69  GUARD_LOCK();
70  SAFE_DELETE(GameVars);
71 }
72 // ------------------------------------ //
74 {
75  if(InMemory)
76  return;
77 
78  std::string newfilecontents = "";
79  // Writing to file doesn't need locking //
80  {
81  GUARD_LOCK();
82  // If not modified we don't need to save anything //
83  if(!Modified)
84  return;
85 
86  // Write the variables to the file //
87  auto vec = GameVars->GetVec();
88 
89  for(size_t i = 0; i < vec->size(); i++) {
90 
91  newfilecontents += vec->at(i)->ToText() + "\n";
92  }
93 
94  // No longer needs to save modified values //
95  Modified = false;
96  }
97 
98  FileSystem::WriteToFile(newfilecontents, GameConfigFile);
99 }
100 
102 {
103  Modified = true;
104 }
105 // ------------------------------------ //
107 {
108  VerifyLock(guard);
109 
110  return GameVars;
111 }
112 // ------------------------------------ //
114 {
115  // Socket unbind control //
116  if(GameVars->ShouldAddValueIfNotFoundOrWrongType<bool>("DisableSocketUnbind")) {
117  // Add new //
118  GameVars->AddVar("DisableSocketUnbind", new VariableBlock(false));
119  MarkModified(guard);
120  }
121 }
DLLEXPORT void Release()
Tries to save the changes.
Non-template class for working with all types of DataBlocks.
Definition: DataBlock.h:425
DLLEXPORT void SaveCheck()
Saves current values (if marked as unsaved)
DLLEXPORT NamedVars * AccessVariables(Lock &guard)
static DLLEXPORT GameConfiguration * Get()
DLLEXPORT void MarkModified(Lock &guard)
Call this when you have changed variables.
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
DLLEXPORT void VerifyGlobalVariables(Lock &guard)
#define DLLEXPORT
Definition: Include.h:115
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
DLLEXPORT GameConfiguration()
Creates an in-memory only configuration.
DLLEXPORT bool Init(std::function< void(Lock &guard, GameConfiguration *configobj)> sanitycheckcallback)
#define SAFE_DELETE(x)
Definition: Define.h:116
DLLEXPORT void Error(const std::string &data) override
Definition: Logger.cpp:177
static DLLEXPORT bool WriteToFile(const std::string &data, const std::string &filename)
Definition: FileSystem.cpp:461
#define GUARD_LOCK()
Definition: ThreadSafe.h:97
std::unique_lock< std::mutex > Lock
Definition: ThreadSafe.h:16