Leviathan  0.8.0.0
Leviathan game engine
Leviathan::KeyConfiguration Class Reference

Holds key configuration for an application. More...

#include <KeyConfiguration.h>

+ Inheritance diagram for Leviathan::KeyConfiguration:

Public Member Functions

DLLEXPORT KeyConfiguration (const std::string &configfile)
 
DLLEXPORT KeyConfiguration ()
 Creates an in-memory only configuration. More...
 
DLLEXPORT ~KeyConfiguration ()
 
DLLEXPORT bool Init (std::function< void(Lock &guard, KeyConfiguration *checkfrom)> functocheck)
 Loads the defined keys from a file. More...
 
DLLEXPORT void Release ()
 Saves all keys if modified. More...
 
DLLEXPORT bool AddKeyIfMissing (Lock &guard, const std::string &name, const std::vector< std::string > &defaultkeys)
 Adds a key if one with the name isn't defined. More...
 
DLLEXPORT void MarkAsChanged ()
 Marks current keys as changed and that the configuration should be saved to a file. More...
 
DLLEXPORT void Save (Lock &guard)
 Saves current keys. More...
 
DLLEXPORT std::shared_ptr< std::vector< GKey > > ResolveControlNameToKey (const std::string &controlkey)
 Resolves a control key string ("WalkForward") to a key. More...
 
DLLEXPORT const std::vector< GKey > & ResolveControlNameToKeyVector (const std::string &controlkey)
 Resolve variant for getting a reference to the vector of keys. More...
 
DLLEXPORT GKey ResolveControlNameToFirstKey (const std::string &controlkey)
 Resolve variant for getting the first binding or throwing. More...
 
DLLEXPORT std::string ResolveKeyToControlName (const GKey &key)
 
- 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
 

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

Holds key configuration for an application.

Definition at line 15 of file KeyConfiguration.h.

Constructor & Destructor Documentation

◆ KeyConfiguration() [1/2]

DLLEXPORT KeyConfiguration::KeyConfiguration ( const std::string &  configfile)
Parameters
configfileThe file which is used for saving and loading keys

Definition at line 8 of file KeyConfiguration.cpp.

8  :
9  KeyStorageFile(configfile)
10 {}

◆ KeyConfiguration() [2/2]

DLLEXPORT KeyConfiguration::KeyConfiguration ( )

Creates an in-memory only configuration.

Definition at line 13 of file KeyConfiguration.cpp.

13 : InMemory(true) {}

◆ ~KeyConfiguration()

DLLEXPORT KeyConfiguration::~KeyConfiguration ( )

Definition at line 15 of file KeyConfiguration.cpp.

16 {
17  // The keys should already be saved by now //
18 }

Member Function Documentation

◆ AddKeyIfMissing()

DLLEXPORT bool KeyConfiguration::AddKeyIfMissing ( Lock guard,
const std::string &  name,
const std::vector< std::string > &  defaultkeys 
)

Adds a key if one with the name isn't defined.

Returns
True if added

Definition at line 73 of file KeyConfiguration.cpp.

75 {
76  if(defaultkeys.empty()) {
77  LOG_WARNING("KeyConfiguration: AddKeyIfMissing: defaultkeys is empty, not adding one "
78  "with name: " +
79  name);
80  return false;
81  }
82 
83  auto iter = KeyConfigurations.find(name);
84  if(iter != KeyConfigurations.end())
85  return false;
86 
87  auto keys = std::make_shared<std::vector<GKey>>();
88 
89  for(const auto& key : defaultkeys)
90  keys->push_back(GKey::GenerateKeyFromString(key));
91 
92  KeyConfigurations[name] = keys;
93 
94  Marked = true;
95  return true;
96 }
#define LOG_WARNING(x)
Definition: Define.h:83
static Key< T > GenerateKeyFromString(const std::string &representation)
https://wiki.libsdl.org/SDL_Keycode for key names
Definition: Key.h:150

◆ Init()

DLLEXPORT bool KeyConfiguration::Init ( std::function< void(Lock &guard, KeyConfiguration *checkfrom)>  functocheck)

Loads the defined keys from a file.

the function argument is called to verify that all required keys are defined and it can add missing keys

Definition at line 20 of file KeyConfiguration.cpp.

22 {
23  GUARD_LOCK();
24 
25  // Load the values from the file //
26  if(!InMemory) {
27  std::vector<std::shared_ptr<NamedVariableList>> tmpvalues;
28 
29  if(FileSystem::LoadDataDump(KeyStorageFile, tmpvalues, Logger::Get())) {
30  // Create keys from the values //
31 
32  for(auto iter = tmpvalues.begin(); iter != tmpvalues.end(); ++iter) {
33  // Get name for storing //
34  auto name = (*iter)->GetName();
35  // Try to create a key //
36  auto keys = std::make_shared<std::vector<GKey>>();
37 
38  std::vector<VariableBlock*>& values = (*iter)->GetValues();
39 
40  for(size_t i = 0; i < values.size(); i++) {
41  // Parse a key //
42  if(values[i]->IsConversionAllowedNonPtr<std::string>()) {
43  keys->push_back(
44  GKey::GenerateKeyFromString(values[i]->operator std::string()));
45  }
46  }
47 
48  // Assign to the map //
49  KeyConfigurations[name] = keys;
50  }
51  }
52  }
53 
54  // Call the function with a pointer to this for it to verify loaded keys //
55  if(functocheck)
56  functocheck(guard, this);
57 
58  if(Marked)
59  Save(guard);
60 
61  return true;
62 }
DLLEXPORT void Save(Lock &guard)
Saves current keys.
static Key< T > GenerateKeyFromString(const std::string &representation)
https://wiki.libsdl.org/SDL_Keycode for key names
Definition: Key.h:150
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
static DLLEXPORT bool LoadDataDump(const std::string &file, std::vector< std::shared_ptr< NamedVariableList >> &vec, LErrorReporter *errorreport)
Definition: FileSystem.cpp:308
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ MarkAsChanged()

DLLEXPORT void KeyConfiguration::MarkAsChanged ( )

Marks current keys as changed and that the configuration should be saved to a file.

Useful during initially checking that all keys exist

Definition at line 98 of file KeyConfiguration.cpp.

99 {
100  Marked = true;
101 }

◆ Release()

DLLEXPORT void KeyConfiguration::Release ( )

Saves all keys if modified.

Definition at line 64 of file KeyConfiguration.cpp.

65 {
66  GUARD_LOCK();
67 
68  // Save all the keys //
69  if(Marked)
70  Save(guard);
71 }
DLLEXPORT void Save(Lock &guard)
Saves current keys.
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ ResolveControlNameToFirstKey()

DLLEXPORT GKey KeyConfiguration::ResolveControlNameToFirstKey ( const std::string &  controlkey)

Resolve variant for getting the first binding or throwing.

Exceptions
InvalidArgumentif controlkey not found

Definition at line 169 of file KeyConfiguration.cpp.

170 {
171  GUARD_LOCK();
172  auto iter = KeyConfigurations.find(controlkey);
173 
174  if(iter != KeyConfigurations.end())
175  return iter->second->at(0);
176 
177  throw InvalidArgument("Key with name \"" + controlkey + "\" not found");
178 }
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ ResolveControlNameToKey()

DLLEXPORT std::shared_ptr< std::vector< GKey > > KeyConfiguration::ResolveControlNameToKey ( const std::string &  controlkey)

Resolves a control key string ("WalkForward") to a key.

Definition at line 140 of file KeyConfiguration.cpp.

142 {
143  GUARD_LOCK();
144  auto iter = KeyConfigurations.find(controlkey);
145 
146  if(iter != KeyConfigurations.end())
147  return iter->second;
148 
149  return NULL;
150 }
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ ResolveControlNameToKeyVector()

DLLEXPORT const std::vector< GKey > & KeyConfiguration::ResolveControlNameToKeyVector ( const std::string &  controlkey)

Resolve variant for getting a reference to the vector of keys.

Exceptions
InvalidArgumentif controlkey not found

Definition at line 152 of file KeyConfiguration.cpp.

154 {
155  GUARD_LOCK();
156  auto iter = KeyConfigurations.find(controlkey);
157 
158  if(iter != KeyConfigurations.end()) {
159 
160  if(!iter->second)
161  throw InvalidArgument("Key with name \"" + controlkey + "\" is nullptr");
162 
163  return *iter->second;
164  }
165 
166  throw InvalidArgument("Key with name \"" + controlkey + "\" not found");
167 }
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ ResolveKeyToControlName()

DLLEXPORT std::string KeyConfiguration::ResolveKeyToControlName ( const GKey key)

Checks which configuration key string matches input key

Warning
this is quite costly function

Definition at line 180 of file KeyConfiguration.cpp.

181 {
182  GUARD_LOCK();
183  // We need to loop through all the keys and see if any of them match //
184  for(auto iter = KeyConfigurations.begin(); iter != KeyConfigurations.end(); ++iter) {
185 
186  for(size_t i = 0; i < iter->second->size(); i++) {
187  // We'll use the partial match function //
188  if(iter->second->at(i).Match(key, false))
189  return iter->first;
190  }
191  }
192  // Not found, return an empty string //
193  return "";
194 }
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ Save()

DLLEXPORT void KeyConfiguration::Save ( Lock guard)

Saves current keys.

Definition at line 103 of file KeyConfiguration.cpp.

104 {
105  if(InMemory)
106  return;
107 
108  // Skip if not given a file //
109  if(KeyStorageFile.size() == 0)
110  return;
111 
112  // First generate a string for this //
113  std::string savedata = "";
114 
115  // Loop through all keys and create string representations from them //
116  for(auto iter = KeyConfigurations.begin(); iter != KeyConfigurations.end(); ++iter) {
117 
118  // Print the name //
119  savedata += iter->first + " = [";
120 
121  // Create a list of data //
122  for(size_t i = 0; i < iter->second->size(); i++) {
123 
124  if(i != 0)
125  savedata += ", ";
126  auto strrepresentation = iter->second->at(i).GenerateStringFromKey();
127 
128  savedata += "[\"" + strrepresentation + "\"]";
129  }
130 
131  // Add the line end //
132  savedata += "];\n";
133  }
134 
135 
136  // Save the file //
137  FileSystem::WriteToFile(savedata, KeyStorageFile);
138 }
static DLLEXPORT bool WriteToFile(const std::string &data, const std::string &filename)
Definition: FileSystem.cpp:461

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