Leviathan  0.8.0.0
Leviathan game engine
Leviathan::GameConfiguration Class Reference

#include <GameConfiguration.h>

+ Inheritance diagram for Leviathan::GameConfiguration:

Public Member Functions

DLLEXPORT GameConfiguration (const std::string &configfile)
 
DLLEXPORT ~GameConfiguration ()
 
DLLEXPORT bool Init (std::function< void(Lock &guard, GameConfiguration *configobj)> sanitycheckcallback)
 
DLLEXPORT void Release ()
 Tries to save the changes. More...
 
DLLEXPORT void SaveCheck ()
 Saves current values (if marked as unsaved) More...
 
DLLEXPORT NamedVarsAccessVariables (Lock &guard)
 
DLLEXPORT void MarkModified (Lock &guard)
 Call this when you have changed variables. More...
 
DLLEXPORT void VerifyGlobalVariables (Lock &guard)
 
- Public Member Functions inherited from Leviathan::ThreadSafeGeneric< MutexType >
DLLEXPORT ThreadSafeGeneric ()
 
DLLEXPORT ~ThreadSafeGeneric ()
 
FORCE_INLINE void VerifyLock (RecursiveLock &guard) const
 
FORCE_INLINE void VerifyLock (Lock &lockit) const
 

Static Public Member Functions

static DLLEXPORT GameConfigurationGet ()
 

Additional Inherited Members

- Protected Attributes inherited from Leviathan::ThreadSafeGeneric< MutexType >
MutexType ObjectsLock
 

Detailed Description

Definition at line 17 of file GameConfiguration.h.

Constructor & Destructor Documentation

◆ GameConfiguration()

DLLEXPORT Leviathan::GameConfiguration::GameConfiguration ( const std::string &  configfile)

Definition at line 9 of file GameConfiguration.cpp.

9  :
10  GameConfigFile(configfile)
11 {
12  staticaccess = this;
13 }

◆ ~GameConfiguration()

DLLEXPORT Leviathan::GameConfiguration::~GameConfiguration ( )

Definition at line 15 of file GameConfiguration.cpp.

15  {
16  staticaccess = NULL;
17 }

Member Function Documentation

◆ AccessVariables()

DLLEXPORT NamedVars * Leviathan::GameConfiguration::AccessVariables ( Lock guard)

Gets the values

Note
you need to have locked this object while and after calling this
Precondition
Call GAMECONFIGURATION_GET_VARIABLEACCESS(variables); to use this

Definition at line 88 of file GameConfiguration.cpp.

88  {
89  VerifyLock(guard);
90 
91  return GameVars;
92 }
FORCE_INLINE void VerifyLock(RecursiveLock &guard) const
Definition: ThreadSafe.h:112

◆ Get()

DLLEXPORT GameConfiguration * Leviathan::GameConfiguration::Get ( )
static

Definition at line 19 of file GameConfiguration.cpp.

19  {
20  return staticaccess;
21 }

◆ Init()

DLLEXPORT bool Leviathan::GameConfiguration::Init ( std::function< void(Lock &guard, GameConfiguration *configobj)>  sanitycheckcallback)

This function loads the files from the defined file and calls the argument function afterwards to verify that all requires values are set

Definition at line 25 of file GameConfiguration.cpp.

27 {
28  GUARD_LOCK();
29 
30  GameVars = new NamedVars();
31 
32  if(!GameVars->LoadVarsFromFile(GameConfigFile, Logger::Get())){
33 
34  if(boost::filesystem::exists(GameConfigFile)){
35  // Unknown error //
36  Logger::Get()->Error("GameConfiguration: Unknown error from LoadVarsFromFile");
37  return false;
38  }
39  // Ignore missing file
40  }
41 
42  // First verify the global variables //
43  VerifyGlobalVariables(guard);
44 
45  // Call the checking function //
46  sanitycheckcallback(guard, this);
47  return true;
48 }
DLLEXPORT bool LoadVarsFromFile(const std::string &file, LErrorReporter *errorreport)
Definition: NamedVars.cpp:1236
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
DLLEXPORT void VerifyGlobalVariables(Lock &guard)
DLLEXPORT void Error(const std::string &data) override
Definition: Logger.cpp:177
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

◆ MarkModified()

DLLEXPORT void Leviathan::GameConfiguration::MarkModified ( Lock guard)

Call this when you have changed variables.

Definition at line 83 of file GameConfiguration.cpp.

83  {
84 
85  Modified = true;
86 }

◆ Release()

DLLEXPORT void Leviathan::GameConfiguration::Release ( )

Tries to save the changes.

Definition at line 50 of file GameConfiguration.cpp.

50  {
51  SaveCheck();
52 
53  // We can now delete our variables //
54  GUARD_LOCK();
55  SAFE_DELETE(GameVars);
56 }
DLLEXPORT void SaveCheck()
Saves current values (if marked as unsaved)
#define SAFE_DELETE(x)
Definition: Define.h:116
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

◆ SaveCheck()

DLLEXPORT void Leviathan::GameConfiguration::SaveCheck ( )

Saves current values (if marked as unsaved)

Definition at line 58 of file GameConfiguration.cpp.

58  {
59 
60  std::string newfilecontents = "";
61  // Writing to file doesn't need locking //
62  {
63  GUARD_LOCK();
64  // If not modified we don't need to save anything //
65  if(!Modified)
66  return;
67 
68  // Write the variables to the file //
69  auto vec = GameVars->GetVec();
70 
71  for(size_t i = 0; i < vec->size(); i++){
72 
73  newfilecontents += vec->at(i)->ToText()+"\n";
74  }
75 
76  // No longer needs to save modified values //
77  Modified = false;
78  }
79 
80  FileSystem::WriteToFile(newfilecontents, GameConfigFile);
81 }
DLLEXPORT std::vector< std::shared_ptr< NamedVariableList > > * GetVec()
Definition: NamedVars.cpp:1241
static DLLEXPORT bool WriteToFile(const std::string &data, const std::string &filename)
Definition: FileSystem.cpp:462
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

◆ VerifyGlobalVariables()

DLLEXPORT void Leviathan::GameConfiguration::VerifyGlobalVariables ( Lock guard)

Verifies that the global default values are added properly

Note
This doesn't need to be called manually as it is called by Init

Definition at line 94 of file GameConfiguration.cpp.

94  {
95 
96  // Socket unbind control //
97  if(GameVars->ShouldAddValueIfNotFoundOrWrongType<bool>("DisableSocketUnbind")){
98  // Add new //
99  GameVars->AddVar("DisableSocketUnbind", new VariableBlock(false));
100  MarkModified(guard);
101  }
102 
103 
104 }
Non-template class for working with all types of DataBlocks.
Definition: DataBlock.h:425
bool ShouldAddValueIfNotFoundOrWrongType(const std::string &name)
Definition: NamedVars.h:372
DLLEXPORT void AddVar(NamedVariableList *newvaluetoadd)
Definition: NamedVars.cpp:1193
DLLEXPORT void MarkModified(Lock &guard)
Call this when you have changed variables.

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