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 ()
 Creates an in-memory only configuration. More...
 
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

- Public Types inherited from Leviathan::ThreadSafeGeneric< MutexType >
using LockT = typename LockTypeResolver< MutexType >::LType
 
- Protected Attributes inherited from Leviathan::ThreadSafeGeneric< MutexType >
MutexType ObjectsLock
 

Detailed Description

Definition at line 24 of file GameConfiguration.h.

Constructor & Destructor Documentation

◆ GameConfiguration() [1/2]

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() [2/2]

DLLEXPORT GameConfiguration::GameConfiguration ( )

Creates an in-memory only configuration.

Definition at line 15 of file GameConfiguration.cpp.

15  : GameVars(new NamedVars()), InMemory(true)
16 {
17  staticaccess = this;
18 }

◆ ~GameConfiguration()

DLLEXPORT Leviathan::GameConfiguration::~GameConfiguration ( )

Definition at line 20 of file GameConfiguration.cpp.

21 {
22  if(GameVars)
23  Release();
24 
25  staticaccess = NULL;
26 }
DLLEXPORT void Release()
Tries to save the changes.

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 106 of file GameConfiguration.cpp.

107 {
108  VerifyLock(guard);
109 
110  return GameVars;
111 }
FORCE_INLINE void VerifyLock(RecursiveLock &guard) const
Definition: ThreadSafe.h:144

◆ Get()

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

Definition at line 28 of file GameConfiguration.cpp.

29 {
30  return staticaccess;
31 }

◆ 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 35 of file GameConfiguration.cpp.

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 }
DLLEXPORT bool LoadVarsFromFile(const std::string &file, LErrorReporter *errorreport)
Definition: NamedVars.cpp:1241
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:111

◆ MarkModified()

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

Call this when you have changed variables.

Definition at line 101 of file GameConfiguration.cpp.

102 {
103  Modified = true;
104 }

◆ Release()

DLLEXPORT void Leviathan::GameConfiguration::Release ( )

Tries to save the changes.

Definition at line 64 of file GameConfiguration.cpp.

65 {
66  SaveCheck();
67 
68  // We can now delete our variables //
69  GUARD_LOCK();
70  SAFE_DELETE(GameVars);
71 }
DLLEXPORT void SaveCheck()
Saves current values (if marked as unsaved)
#define SAFE_DELETE(x)
Definition: Define.h:147
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ SaveCheck()

DLLEXPORT void Leviathan::GameConfiguration::SaveCheck ( )

Saves current values (if marked as unsaved)

Definition at line 73 of file GameConfiguration.cpp.

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 }
DLLEXPORT std::vector< std::shared_ptr< NamedVariableList > > * GetVec()
Definition: NamedVars.cpp:1246
static DLLEXPORT bool WriteToFile(const std::string &data, const std::string &filename)
Definition: FileSystem.cpp:456
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ 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 113 of file GameConfiguration.cpp.

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 }
Non-template class for working with all types of DataBlocks.
Definition: DataBlock.h:425
bool ShouldAddValueIfNotFoundOrWrongType(const std::string &name)
Definition: NamedVars.h:376
DLLEXPORT void AddVar(NamedVariableList *newvaluetoadd)
Definition: NamedVars.cpp:1198
DLLEXPORT void MarkModified(Lock &guard)
Call this when you have changed variables.

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