Angel 3.2
A 2D Game Prototyping Engine
Preferences.cpp
1 
2 // Copyright (C) 2008-2013, Shane Liesegang
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of the copyright holder nor the names of any
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
29 
30 #include "stdafx.h"
31 #include "../Infrastructure/Preferences.h"
32 
33 #include "../Infrastructure/Log.h"
34 #include "../Util/FileUtil.h"
35 
36 #if defined(WIN32)
37  #pragma warning(disable:4503)
38 #endif
39 
40 Preferences* Preferences::s_Preferences = NULL;
41 
43 {
44  if (s_Preferences == NULL)
45  {
46  s_Preferences = new Preferences();
47  }
48  return *s_Preferences;
49 }
50 
51 Preferences::Preferences()
52 {
53  //nothing to do here at the moment
54 }
55 
57 {
58  StringList written;
59  hashmap_ns::hash_map<String, LoadedVariableMap>::iterator it = _preferenceVariables.begin();
60  while (it != _preferenceVariables.end())
61  {
62  written.push_back(it->first + " = {");
63  LoadedVariableMap::iterator lvmIt = it->second.begin();
64  while (lvmIt != it->second.end())
65  {
66  if (lvmIt->second._setAs == 2)
67  {
68  written.push_back("\t" + lvmIt->first + " = \"" + lvmIt->second._string + "\",");
69  }
70  else
71  {
72  written.push_back("\t" + lvmIt->first + " = " + lvmIt->second._string + ",");
73  }
74  lvmIt++;
75  }
76  written.push_back("}\n");
77  it++;
78  }
79  WriteLinesToFile(GetUserPrefsPath(), written);
80 }
81 
83 {
84  return "Config/defaults.lua";
85 }
86 
88 {
89  MakeDirectories(GetStorageDirectory());
90  return GetStorageDirectory() + "user_prefs.lua";
91 }
92 
93 int Preferences::GetInt(const String& category, const String& name)
94 {
95  hashmap_ns::hash_map<String, LoadedVariableMap>::iterator it = _preferenceVariables.find(category);
96  if (it == _preferenceVariables.end())
97  {
98  return LoadedVariable()._int;
99  }
100  LoadedVariableMap::iterator lvmIt = it->second.find(name);
101  if (lvmIt == it->second.end())
102  {
103  return LoadedVariable()._int;
104  }
105 
106  return lvmIt->second._int;
107 }
108 
109 float Preferences::GetFloat(const String& category, const String& name)
110 {
111  hashmap_ns::hash_map<String, LoadedVariableMap>::iterator it = _preferenceVariables.find(category);
112  if (it == _preferenceVariables.end())
113  {
114  return LoadedVariable()._float;
115  }
116  LoadedVariableMap::iterator lvmIt = it->second.find(name);
117  if (lvmIt == it->second.end())
118  {
119  return LoadedVariable()._float;
120  }
121 
122  return lvmIt->second._float;
123 }
124 
125 String Preferences::GetString(const String& category, const String& name)
126 {
127  hashmap_ns::hash_map<String, LoadedVariableMap>::iterator it = _preferenceVariables.find(category);
128  if (it == _preferenceVariables.end())
129  {
130  return LoadedVariable()._string;
131  }
132  LoadedVariableMap::iterator lvmIt = it->second.find(name);
133  if (lvmIt == it->second.end())
134  {
135  return LoadedVariable()._string;
136  }
137 
138  return lvmIt->second._string;
139 }
140 
141 LoadedVariableMap Preferences::GetTable(const String& category)
142 {
143  LoadedVariableMap forReturn;
144 
145  hashmap_ns::hash_map<String, LoadedVariableMap>::iterator it = _preferenceVariables.find(category);
146  if (it == _preferenceVariables.end())
147  {
148  return forReturn;
149  }
150 
151  LoadedVariableMap::iterator lvmIt = it->second.begin();
152  while (lvmIt != it->second.end())
153  {
154  forReturn[lvmIt->first] = LoadedVariable(lvmIt->second._string);
155  lvmIt++;
156  }
157 
158  return forReturn;
159 }
160 
161 void Preferences::SetInt(const String& category, const String& name, int val)
162 {
163  if (category == "" || name == "")
164  {
165  sysLog.Log("ERROR: Null strings passed for category or name in preference setting.");
166  }
167  hashmap_ns::hash_map<String, LoadedVariableMap>::iterator it = _preferenceVariables.find(category);
168  if (it == _preferenceVariables.end())
169  {
170  _preferenceVariables[category] = LoadedVariableMap();
171  }
172  _preferenceVariables[category][name] = LoadedVariable(val);
173 }
174 
175 void Preferences::SetFloat(const String& category, const String& name, float val)
176 {
177  if (category == "" || name == "")
178  {
179  sysLog.Log("ERROR: Null strings passed for category or name in preference setting.");
180  }
181  hashmap_ns::hash_map<String, LoadedVariableMap>::iterator it = _preferenceVariables.find(category);
182  if (it == _preferenceVariables.end())
183  {
184  _preferenceVariables[category] = LoadedVariableMap();
185  }
186  _preferenceVariables[category][name] = LoadedVariable(val);
187 }
188 
189 void Preferences::SetString(const String& category, const String& name, const String& val)
190 {
191  if (category == "" || name == "")
192  {
193  sysLog.Log("ERROR: Null strings passed for category or name in preference setting.");
194  }
195  hashmap_ns::hash_map<String, LoadedVariableMap>::iterator it = _preferenceVariables.find(category);
196  if (it == _preferenceVariables.end())
197  {
198  _preferenceVariables[category] = LoadedVariableMap();
199  }
200  _preferenceVariables[category][name] = LoadedVariable(val);
201 }
202 
203 int Preferences::OverrideInt(const String& category, const String& name, int val)
204 {
205  hashmap_ns::hash_map<String, LoadedVariableMap>::iterator it = _preferenceVariables.find(category);
206  if (it == _preferenceVariables.end())
207  {
208  return val;
209  }
210  LoadedVariableMap::iterator lvmIt = it->second.find(name);
211  if (lvmIt == it->second.end())
212  {
213  return val;
214  }
215 
216  return lvmIt->second._int;
217 }
218 
219 float Preferences::OverrideFloat(const String& category, const String& name, float val)
220 {
221  hashmap_ns::hash_map<String, LoadedVariableMap>::iterator it = _preferenceVariables.find(category);
222  if (it == _preferenceVariables.end())
223  {
224  return val;
225  }
226  LoadedVariableMap::iterator lvmIt = it->second.find(name);
227  if (lvmIt == it->second.end())
228  {
229  return val;
230  }
231 
232  return lvmIt->second._float;
233 }
234 
235 String Preferences::OverrideString(const String& category, const String& name, String val)
236 {
237  hashmap_ns::hash_map<String, LoadedVariableMap>::iterator it = _preferenceVariables.find(category);
238  if (it == _preferenceVariables.end())
239  {
240  return val;
241  }
242  LoadedVariableMap::iterator lvmIt = it->second.find(name);
243  if (lvmIt == it->second.end())
244  {
245  return val;
246  }
247 
248  return lvmIt->second._string;
249 }
250