Leviathan  0.8.0.0
Leviathan game engine
KeyConfiguration.cpp
Go to the documentation of this file.
1 // ------------------------------------ //
2 #include "KeyConfiguration.h"
3 
5 #include "FileSystem.h"
6 using namespace Leviathan;
7 // ------------------------------------ //
8 DLLEXPORT KeyConfiguration::KeyConfiguration(const std::string& configfile) :
9  KeyStorageFile(configfile)
10 {}
11 
14 
16 {
17  // The keys should already be saved by now //
18 }
19 // ------------------------------------ //
21  std::function<void(Lock& guard, KeyConfiguration* checkfrom)> functocheck)
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 }
63 
65 {
66  GUARD_LOCK();
67 
68  // Save all the keys //
69  if(Marked)
70  Save(guard);
71 }
72 // ------------------------------------ //
74  Lock& guard, const std::string& name, const std::vector<std::string>& defaultkeys)
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 }
97 
99 {
100  Marked = true;
101 }
102 // ------------------------------------ //
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 }
139 // ------------------------------------ //
140 DLLEXPORT std::shared_ptr<std::vector<GKey>> KeyConfiguration::ResolveControlNameToKey(
141  const std::string& controlkey)
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 }
151 
153  const std::string& controlkey)
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 }
168 
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 }
179 
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 }
195 // ------------------------------------ //
Holds key configuration for an application.
DLLEXPORT void Save(Lock &guard)
Saves current keys.
DLLEXPORT std::string ResolveKeyToControlName(const GKey &key)
#define LOG_WARNING(x)
Definition: Define.h:82
static Key< T > GenerateKeyFromString(const std::string &representation)
https://wiki.libsdl.org/SDL_Keycode for key names
Definition: Key.h:150
DLLEXPORT void MarkAsChanged()
Marks current keys as changed and that the configuration should be saved to a file.
DLLEXPORT void Release()
Saves all keys if modified.
DLLEXPORT GKey ResolveControlNameToFirstKey(const std::string &controlkey)
Resolve variant for getting the first binding or throwing.
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
DLLEXPORT KeyConfiguration()
Creates an in-memory only configuration.
static DLLEXPORT bool LoadDataDump(const std::string &file, std::vector< std::shared_ptr< NamedVariableList >> &vec, LErrorReporter *errorreport)
Definition: FileSystem.cpp:308
#define DLLEXPORT
Definition: Include.h:115
DLLEXPORT const std::vector< GKey > & ResolveControlNameToKeyVector(const std::string &controlkey)
Resolve variant for getting a reference to the vector of keys.
DLLEXPORT std::shared_ptr< std::vector< GKey > > ResolveControlNameToKey(const std::string &controlkey)
Resolves a control key string ("WalkForward") to a key.
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
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
DLLEXPORT bool Init(std::function< void(Lock &guard, KeyConfiguration *checkfrom)> functocheck)
Loads the defined keys from a file.
DLLEXPORT bool AddKeyIfMissing(Lock &guard, const std::string &name, const std::vector< std::string > &defaultkeys)
Adds a key if one with the name isn&#39;t defined.