EngineConfig.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Core/Context.h"
  23. #include "../IO/Log.h"
  24. #include "../IO/File.h"
  25. #include "../IO/FileSystem.h"
  26. #include "../Resource/JSONFile.h"
  27. #include "../Graphics/GraphicsDefs.h"
  28. #include "EngineConfig.h"
  29. namespace Atomic
  30. {
  31. VariantMap EngineConfig::engineConfig_;
  32. String EngineConfig::engineConfigFilename_;
  33. bool EngineConfig::GetBoolValue(const JSONValue& jvalue, bool defaultValue)
  34. {
  35. bool value = defaultValue;
  36. if (jvalue.IsBool())
  37. value = jvalue.GetBool();
  38. return value;
  39. }
  40. int EngineConfig::GetIntValue(const JSONValue& jvalue, int defaultValue)
  41. {
  42. int value = defaultValue;
  43. if (jvalue.IsNumber())
  44. value = jvalue.GetInt();
  45. return value;
  46. }
  47. String EngineConfig::GetStringValue(const JSONValue& jvalue, const String& defaultValue)
  48. {
  49. String value = defaultValue;
  50. if (jvalue.IsString())
  51. value = jvalue.GetString();
  52. return value;
  53. }
  54. bool EngineConfig::LoadEngineConfig(const JSONValue& jengine)
  55. {
  56. if (!jengine.IsObject())
  57. return false;
  58. for (JSONObject::ConstIterator i = jengine.Begin(); i != jengine.End(); ++i)
  59. {
  60. String key = i->first_.ToLower();
  61. const JSONValue& jvalue = i->second_;
  62. if (key == "workerthreads")
  63. engineConfig_["WorkerThreads"] = GetBoolValue(jvalue, true);
  64. else if (key == "logquiet")
  65. engineConfig_["LogQuiet"] = GetBoolValue(jvalue, false);
  66. else if (key == "loglevel")
  67. engineConfig_["LogLevel"] = GetIntValue(jvalue, 1);
  68. }
  69. return true;
  70. }
  71. bool EngineConfig::LoadGraphicsConfig(const JSONValue& jgraphics)
  72. {
  73. if (!jgraphics.IsObject())
  74. return false;
  75. for (JSONObject::ConstIterator i = jgraphics.Begin(); i != jgraphics.End(); ++i)
  76. {
  77. String key = i->first_.ToLower();
  78. const JSONValue& jvalue = i->second_;
  79. if (key == "headless")
  80. engineConfig_["Headless"] = GetBoolValue(jvalue, false);
  81. else if (key == "framelimiter")
  82. engineConfig_["FrameLimiter"] = GetBoolValue(jvalue, true);
  83. else if (key == "flushgpu")
  84. engineConfig_["FlushGPU"] = GetBoolValue(jvalue, false);
  85. else if (key == "forcegl2")
  86. engineConfig_["ForceGL2"] = GetBoolValue(jvalue, false);
  87. else if (key == "orientations")
  88. engineConfig_["Orientations"] = GetStringValue(jvalue, "LandscapeLeft LandscapeRight");
  89. else if (key == "vsync")
  90. engineConfig_["VSync"] = GetBoolValue(jvalue, false);
  91. else if (key == "triplebuffer")
  92. engineConfig_["TripleBuffer"] = GetBoolValue(jvalue, false);
  93. else if (key == "multisample")
  94. engineConfig_["Multisample"] = GetIntValue(jvalue, 1);
  95. else if (key == "renderpath")
  96. {
  97. String renderPath = GetStringValue(jvalue, "forward").ToLower();
  98. if (renderPath == "forward")
  99. engineConfig_["RenderPath"] = "RenderPaths/Forward.xml";
  100. else if (renderPath == "prepass")
  101. engineConfig_["RenderPath"] = "RenderPaths/Prepass.xml";
  102. else if (renderPath == "deferred")
  103. engineConfig_["RenderPath"] = "RenderPaths/Deferred.xml";
  104. }
  105. else if (key == "shadows")
  106. engineConfig_["Shadows"] = GetBoolValue(jvalue, true);
  107. else if (key == "lowqualityshadows")
  108. engineConfig_["LowQualityShadows"] = GetBoolValue(jvalue, false);
  109. else if (key == "materialquality")
  110. {
  111. String quality = GetStringValue(jvalue, "high").ToLower();
  112. if (quality == "high")
  113. engineConfig_["MaterialQuality"] = QUALITY_HIGH;
  114. else if (quality == "medium")
  115. engineConfig_["MaterialQuality"] = QUALITY_MEDIUM;
  116. else if (quality == "low")
  117. engineConfig_["MaterialQuality"] = QUALITY_LOW;
  118. }
  119. else if (key == "texturequality")
  120. {
  121. String quality = GetStringValue(jvalue, "high").ToLower();
  122. if (quality == "high")
  123. engineConfig_["TextureQuality"] = QUALITY_HIGH;
  124. else if (quality == "medium")
  125. engineConfig_["TextureQuality"] = QUALITY_MEDIUM;
  126. else if (quality == "low")
  127. engineConfig_["TextureQuality"] = QUALITY_LOW;
  128. }
  129. else if (key == "texturefiltermode")
  130. {
  131. String mode = GetStringValue(jvalue, "trilinear").ToLower();
  132. if (mode == "trilinear")
  133. engineConfig_["TextureFilterMode"] = FILTER_TRILINEAR;
  134. else if (mode == "bilinear")
  135. engineConfig_["TextureFilterMode"] = FILTER_BILINEAR;
  136. else if (mode == "nearest")
  137. engineConfig_["TextureFilterMode"] = FILTER_NEAREST;
  138. else if (mode == "anisotropic")
  139. engineConfig_["TextureFilterMode"] = FILTER_ANISOTROPIC;
  140. }
  141. else if (key == "textureanisotropy")
  142. {
  143. engineConfig_["TextureAnisotropy"] = GetIntValue(jvalue, 4);
  144. }
  145. }
  146. return true;
  147. }
  148. bool EngineConfig::LoadWindowConfig(const JSONValue& jwindow)
  149. {
  150. if (!jwindow.IsObject())
  151. return false;
  152. for (JSONObject::ConstIterator i = jwindow.Begin(); i != jwindow.End(); ++i)
  153. {
  154. String key = i->first_.ToLower();
  155. const JSONValue& jvalue = i->second_;
  156. if (key == "title")
  157. engineConfig_["WindowTitle"] = GetStringValue(jvalue, "Atomic");
  158. else if (key == "fullscreen")
  159. engineConfig_["FullScreen"] = GetBoolValue(jvalue, false);
  160. else if (key == "borderless")
  161. engineConfig_["Borderless"] = GetBoolValue(jvalue, false);
  162. else if (key == "resizable")
  163. engineConfig_["WindowResizable"] = GetBoolValue(jvalue, false);
  164. else if (key == "width")
  165. engineConfig_["WindowWidth"] = GetIntValue(jvalue, false);
  166. else if (key == "height")
  167. engineConfig_["WindowHeight"] = GetIntValue(jvalue, false);
  168. else if (key == "positionx")
  169. engineConfig_["WindowPositionX"] = GetIntValue(jvalue, false);
  170. else if (key == "positiony")
  171. engineConfig_["WindowPositionY"] = GetIntValue(jvalue, false);
  172. }
  173. return true;
  174. }
  175. bool EngineConfig::LoadSoundConfig(const JSONValue& jsound)
  176. {
  177. if (!jsound.IsObject())
  178. return false;
  179. for (JSONObject::ConstIterator i = jsound.Begin(); i != jsound.End(); ++i)
  180. {
  181. String key = i->first_.ToLower();
  182. const JSONValue& jvalue = i->second_;
  183. if (key == "enabled")
  184. engineConfig_["Sound"] = GetBoolValue(jvalue, true);
  185. else if (key == "interpolation")
  186. engineConfig_["SoundInterpolation"] = GetBoolValue(jvalue, true);
  187. else if (key == "stereo")
  188. engineConfig_["SoundStereo"] = GetBoolValue(jvalue, true);
  189. else if (key == "bufferms")
  190. engineConfig_["SoundBuffer"] = GetIntValue(jvalue, 100);
  191. else if (key == "mixrate")
  192. engineConfig_["SoundMixRate"] = GetIntValue(jvalue, 44100);
  193. }
  194. return true;
  195. }
  196. bool EngineConfig::LoadInputConfig(const JSONValue& jinput)
  197. {
  198. if (!jinput.IsObject())
  199. return false;
  200. for (JSONObject::ConstIterator i = jinput.Begin(); i != jinput.End(); ++i)
  201. {
  202. String key = i->first_.ToLower();
  203. const JSONValue& jvalue = i->second_;
  204. if (key == "touchemulation")
  205. engineConfig_["TouchEmulation"] = GetBoolValue(jvalue, false);
  206. }
  207. return true;
  208. }
  209. bool EngineConfig::LoadDesktopConfig(JSONValue root)
  210. {
  211. const JSONValue& jdesktop = root["desktop"];
  212. if (!jdesktop.IsObject())
  213. return false;
  214. const JSONValue& jengine = jdesktop["engine"];
  215. if (jengine.IsObject())
  216. LoadEngineConfig(jengine);
  217. const JSONValue& jgraphics = jdesktop["graphics"];
  218. if (jgraphics.IsObject())
  219. LoadGraphicsConfig(jgraphics);
  220. const JSONValue& jwindow = jdesktop["window"];
  221. if (jwindow.IsObject())
  222. LoadWindowConfig(jwindow);
  223. const JSONValue& jsound = jdesktop["sound"];
  224. if (jsound.IsObject())
  225. LoadSoundConfig(jsound);
  226. const JSONValue& jinput = jdesktop["input"];
  227. if (jinput.IsObject())
  228. LoadInputConfig(jinput);
  229. return true;
  230. }
  231. bool EngineConfig::LoadFromJSON(const String& json)
  232. {
  233. engineConfig_.Clear();
  234. JSONValue jroot;
  235. if (!JSONFile::ParseJSON(json, jroot))
  236. {
  237. LOGERRORF("EngineConfig::LoadFromJSON - Unable to parse config file JSON: %s", engineConfigFilename_.CString());
  238. return false;
  239. }
  240. if (!jroot.IsObject())
  241. return false;
  242. if (!LoadDesktopConfig(jroot))
  243. return false;
  244. return true;
  245. }
  246. bool EngineConfig::LoadFromFile(Context *context, const String& filename)
  247. {
  248. FileSystem* fileSystem = context->GetSubsystem<FileSystem>();
  249. if (!fileSystem->FileExists(filename))
  250. return false;
  251. engineConfigFilename_ = filename;
  252. SharedPtr<File> file(new File(context));
  253. if (!file->Open(filename))
  254. {
  255. LOGERRORF("EngineConfig::LoadFromFile - Unable to open config file %s", filename.CString());
  256. return false;
  257. }
  258. String json;
  259. file->ReadText(json);
  260. return LoadFromJSON(json);
  261. }
  262. void EngineConfig::ApplyConfig(VariantMap& settings, bool overwrite)
  263. {
  264. VariantMap::ConstIterator itr = engineConfig_.Begin();
  265. if (overwrite)
  266. {
  267. while (itr != engineConfig_.End())
  268. {
  269. settings[itr->first_] = itr->second_;
  270. itr++;
  271. }
  272. }
  273. else
  274. {
  275. while (itr != engineConfig_.End())
  276. {
  277. settings.InsertNew(itr->first_, itr->second_);
  278. itr++;
  279. }
  280. }
  281. }
  282. }