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