Configuration.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. StringVector Configuration::GetArrayValue(const JSONArray & jarray, const StringVector & defaultValue)
  53. {
  54. StringVector value;
  55. for (JSONArray::ConstIterator it = jarray.Begin(); it != jarray.End(); it++)
  56. {
  57. if (it->IsString())
  58. {
  59. value.Push(it->GetString());
  60. }
  61. }
  62. return value;
  63. }
  64. Configuration::Configuration() :
  65. isLoaded_(false)
  66. {
  67. }
  68. bool Configuration::LoadFromFile(Context *context, const String& filename)
  69. {
  70. FileSystem* fileSystem = context->GetSubsystem<FileSystem>();
  71. if (!fileSystem->FileExists(filename))
  72. return false;
  73. filename_ = filename;
  74. SharedPtr<File> file(new File(context));
  75. if (!file->Open(filename))
  76. {
  77. LOGERRORF("Configuration::LoadFromFile - Unable to open config file %s", filename.CString());
  78. return false;
  79. }
  80. String json;
  81. file->ReadText(json);
  82. return LoadFromJSON(json);
  83. }
  84. void Configuration::ApplyConfig(VariantMap& settings, bool overwrite)
  85. {
  86. if (!isLoaded_) {
  87. LOGERROR("Configuration::ApplyConfig - Applying a config that has not yet been populated");
  88. return;
  89. }
  90. VariantMap::ConstIterator itr = valueMap_.Begin();
  91. if (overwrite)
  92. {
  93. while (itr != valueMap_.End())
  94. {
  95. settings[itr->first_] = itr->second_;
  96. itr++;
  97. }
  98. }
  99. else
  100. {
  101. while (itr != valueMap_.End())
  102. {
  103. settings.InsertNew(itr->first_, itr->second_);
  104. itr++;
  105. }
  106. }
  107. }
  108. bool Configuration::LoadFromJSON(const String& json)
  109. {
  110. Clear();
  111. JSONValue jroot;
  112. if (!JSONFile::ParseJSON(json, jroot))
  113. {
  114. LOGERRORF("Configuration::LoadFromJSON - Unable to parse config file JSON: %s", filename_.CString());
  115. return false;
  116. }
  117. if (!jroot.IsObject())
  118. return false;
  119. if (!LoadDesktopConfig(jroot))
  120. return false;
  121. isLoaded_ = true;
  122. return true;
  123. }
  124. void Configuration::Clear()
  125. {
  126. valueMap_.Clear();
  127. }
  128. }