Configuration.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "Configuration.h"
  29. namespace Atomic
  30. {
  31. bool Configuration::GetBoolValue(const JSONValue& jvalue, bool defaultValue)
  32. {
  33. bool value = defaultValue;
  34. if (jvalue.IsBool())
  35. value = jvalue.GetBool();
  36. return value;
  37. }
  38. int Configuration::GetIntValue(const JSONValue& jvalue, int defaultValue)
  39. {
  40. int value = defaultValue;
  41. if (jvalue.IsNumber())
  42. value = jvalue.GetInt();
  43. return value;
  44. }
  45. String Configuration::GetStringValue(const JSONValue& jvalue, const String& defaultValue)
  46. {
  47. String value = defaultValue;
  48. if (jvalue.IsString())
  49. value = jvalue.GetString();
  50. return value;
  51. }
  52. Configuration::Configuration() :
  53. isLoaded_(false)
  54. {
  55. }
  56. bool Configuration::LoadFromFile(Context *context, const String& filename)
  57. {
  58. FileSystem* fileSystem = context->GetSubsystem<FileSystem>();
  59. if (!fileSystem->FileExists(filename))
  60. return false;
  61. filename_ = filename;
  62. SharedPtr<File> file(new File(context));
  63. if (!file->Open(filename))
  64. {
  65. LOGERRORF("Configuration::LoadFromFile - Unable to open config file %s", filename.CString());
  66. return false;
  67. }
  68. String json;
  69. file->ReadText(json);
  70. return LoadFromJSON(json);
  71. }
  72. void Configuration::ApplyConfig(VariantMap& settings, bool overwrite)
  73. {
  74. if (!isLoaded_) {
  75. LOGERROR("Configuration::ApplyConfig - Applying a config that has not yet been populated");
  76. return;
  77. }
  78. VariantMap::ConstIterator itr = valueMap_.Begin();
  79. if (overwrite)
  80. {
  81. while (itr != valueMap_.End())
  82. {
  83. settings[itr->first_] = itr->second_;
  84. itr++;
  85. }
  86. }
  87. else
  88. {
  89. while (itr != valueMap_.End())
  90. {
  91. settings.InsertNew(itr->first_, itr->second_);
  92. itr++;
  93. }
  94. }
  95. }
  96. bool Configuration::LoadFromJSON(const String& json)
  97. {
  98. Clear();
  99. JSONValue jroot;
  100. if (!JSONFile::ParseJSON(json, jroot))
  101. {
  102. LOGERRORF("Configuration::LoadFromJSON - Unable to parse config file JSON: %s", filename_.CString());
  103. return false;
  104. }
  105. if (!jroot.IsObject())
  106. return false;
  107. if (!LoadDesktopConfig(jroot))
  108. return false;
  109. isLoaded_ = true;
  110. return true;
  111. }
  112. void Configuration::Clear()
  113. {
  114. valueMap_.Clear();
  115. }
  116. }