EngineConfig.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. }
  165. return true;
  166. }
  167. bool EngineConfig::LoadSoundConfig(const JSONValue& jsound)
  168. {
  169. if (!jsound.IsObject())
  170. return false;
  171. for (JSONObject::ConstIterator i = jsound.Begin(); i != jsound.End(); ++i)
  172. {
  173. String key = i->first_.ToLower();
  174. const JSONValue& jvalue = i->second_;
  175. if (key == "enabled")
  176. engineConfig_["Sound"] = GetBoolValue(jvalue, true);
  177. else if (key == "interpolation")
  178. engineConfig_["SoundInterpolation"] = GetBoolValue(jvalue, true);
  179. else if (key == "stereo")
  180. engineConfig_["SoundStereo"] = GetBoolValue(jvalue, true);
  181. else if (key == "bufferms")
  182. engineConfig_["SoundBuffer"] = GetIntValue(jvalue, 100);
  183. else if (key == "mixrate")
  184. engineConfig_["SoundMixRate"] = GetIntValue(jvalue, 44100);
  185. }
  186. return true;
  187. }
  188. bool EngineConfig::LoadInputConfig(const JSONValue& jinput)
  189. {
  190. if (!jinput.IsObject())
  191. return false;
  192. for (JSONObject::ConstIterator i = jinput.Begin(); i != jinput.End(); ++i)
  193. {
  194. String key = i->first_.ToLower();
  195. const JSONValue& jvalue = i->second_;
  196. if (key == "touchemulation")
  197. engineConfig_["TouchEmulation"] = GetBoolValue(jvalue, false);
  198. }
  199. return true;
  200. }
  201. bool EngineConfig::LoadDesktopConfig(JSONValue root)
  202. {
  203. const JSONValue& jdesktop = root["desktop"];
  204. if (!jdesktop.IsObject())
  205. return false;
  206. const JSONValue& jengine = jdesktop["engine"];
  207. if (jengine.IsObject())
  208. LoadEngineConfig(jengine);
  209. const JSONValue& jgraphics = jdesktop["graphics"];
  210. if (jgraphics.IsObject())
  211. LoadGraphicsConfig(jgraphics);
  212. const JSONValue& jwindow = jdesktop["window"];
  213. if (jwindow.IsObject())
  214. LoadWindowConfig(jwindow);
  215. const JSONValue& jsound = jdesktop["sound"];
  216. if (jsound.IsObject())
  217. LoadSoundConfig(jsound);
  218. const JSONValue& jinput = jdesktop["input"];
  219. if (jinput.IsObject())
  220. LoadInputConfig(jinput);
  221. return true;
  222. }
  223. bool EngineConfig::LoadFromJSON(const String& json)
  224. {
  225. engineConfig_.Clear();
  226. JSONValue jroot;
  227. if (!JSONFile::ParseJSON(json, jroot))
  228. {
  229. LOGERRORF("EngineConfig::LoadFromJSON - Unable to parse config file JSON: %s", engineConfigFilename_.CString());
  230. return false;
  231. }
  232. if (!jroot.IsObject())
  233. return false;
  234. if (!LoadDesktopConfig(jroot))
  235. return false;
  236. return true;
  237. }
  238. bool EngineConfig::LoadFromFile(Context *context, const String& filename)
  239. {
  240. FileSystem* fileSystem = context->GetSubsystem<FileSystem>();
  241. if (!fileSystem->FileExists(filename))
  242. return false;
  243. engineConfigFilename_ = filename;
  244. SharedPtr<File> file(new File(context));
  245. if (!file->Open(filename))
  246. {
  247. LOGERRORF("EngineConfig::LoadFromFile - Unable to open config file %s", filename.CString());
  248. return false;
  249. }
  250. String json;
  251. file->ReadText(json);
  252. return LoadFromJSON(json);
  253. }
  254. void EngineConfig::ApplyConfig(VariantMap& settings)
  255. {
  256. VariantMap::ConstIterator itr = engineConfig_.Begin();
  257. while (itr != engineConfig_.End())
  258. {
  259. settings.InsertNew(itr->first_, itr->second_);
  260. itr++;
  261. }
  262. }
  263. }