ImportConfig.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 "JSONFile.h"
  27. #include "../Graphics/GraphicsDefs.h"
  28. #include "ImportConfig.h"
  29. namespace Atomic
  30. {
  31. VariantMap ImportConfig::importConfig_;
  32. String ImportConfig::importConfigFilename_;
  33. bool ImportConfig::GetBoolValue(const JSONValue& jvalue, bool defaultValue)
  34. {
  35. bool value = defaultValue;
  36. if (jvalue.IsBool())
  37. value = jvalue.GetBool();
  38. return value;
  39. }
  40. bool ImportConfig::LoadFromJSON(const String& json)
  41. {
  42. importConfig_.Clear();
  43. JSONValue jroot;
  44. if (!JSONFile::ParseJSON(json, jroot))
  45. {
  46. LOGERRORF("ImportConfig::LoadFromJSON - Unable to parse config file JSON: %s", importConfigFilename_.CString());
  47. return false;
  48. }
  49. if (!jroot.IsObject())
  50. return false;
  51. if (!LoadDesktopConfig(jroot))
  52. return false;
  53. return true;
  54. }
  55. bool ImportConfig::LoadFromFile(Context *context, const String& filename)
  56. {
  57. FileSystem* fileSystem = context->GetSubsystem<FileSystem>();
  58. if (!fileSystem->FileExists(filename))
  59. return false;
  60. importConfigFilename_ = filename;
  61. SharedPtr<File> file(new File(context));
  62. if (!file->Open(filename))
  63. {
  64. LOGERRORF("ImportConfig::LoadFromFile - Unable to open config file %s", filename.CString());
  65. return false;
  66. }
  67. String json;
  68. file->ReadText(json);
  69. return LoadFromJSON(json);
  70. }
  71. void ImportConfig::ApplyConfig(VariantMap& settings, bool overwrite)
  72. {
  73. VariantMap::ConstIterator itr = importConfig_.Begin();
  74. if (overwrite)
  75. {
  76. while (itr != importConfig_.End())
  77. {
  78. settings[itr->first_] = itr->second_;
  79. itr++;
  80. }
  81. }
  82. else
  83. {
  84. while (itr != importConfig_.End())
  85. {
  86. settings.InsertNew(itr->first_, itr->second_);
  87. itr++;
  88. }
  89. }
  90. }
  91. bool ImportConfig::LoadAIFlagsDefaultConfig(const JSONValue& jflags)
  92. {
  93. if (!jflags.IsObject())
  94. return false;
  95. for (JSONObject::ConstIterator i = jflags.Begin(); i != jflags.End(); ++i)
  96. {
  97. String key = i->first_;
  98. const JSONValue& jvalue = i->second_;
  99. if (key == "convertToLeftHanded")
  100. importConfig_["aiProcess_ConvertToLeftHanded"] = GetBoolValue(jvalue, true);
  101. else if (key == "joinIdenticalVertices")
  102. importConfig_["aiProcess_JoinIdenticalVertices"] = GetBoolValue(jvalue, true);
  103. else if (key == "triangulate")
  104. importConfig_["aiProcess_Triangulate"] = GetBoolValue(jvalue, true);
  105. else if (key == "genSmoothNormals")
  106. importConfig_["aiProcess_GenSmoothNormals"] = GetBoolValue(jvalue, true);
  107. else if (key == "limitBoneWeights")
  108. importConfig_["aiProcess_LimitBoneWeights"] = GetBoolValue(jvalue, true);
  109. else if (key == "improveCacheLocality")
  110. importConfig_["aiProcess_ImproveCacheLocality"] = GetBoolValue(jvalue, true);
  111. else if (key == "fixInFacingNormals")
  112. importConfig_["aiProcess_FixInfacingNormals"] = GetBoolValue(jvalue, true);
  113. else if (key == "fixInfacingNormals")
  114. importConfig_["aiProcess_FixInfacingNormals"] = GetBoolValue(jvalue, true);
  115. else if (key == "findInvalidData")
  116. importConfig_["aiProcess_FindInvalidData"] = GetBoolValue(jvalue, true);
  117. else if (key == "genUVCoords")
  118. importConfig_["aiProcess_GenUVCoords"] = GetBoolValue(jvalue, true);
  119. else if (key == "findInstances")
  120. importConfig_["aiProcess_FindInstances"] = GetBoolValue(jvalue, true);
  121. else if (key == "optimizeMeshes")
  122. importConfig_["aiProcess_OptimizeMeshes"] = GetBoolValue(jvalue, true);
  123. }
  124. return true;
  125. }
  126. bool ImportConfig::LoadDesktopConfig(JSONValue root)
  127. {
  128. const JSONValue& jdesktop = root["desktop"];
  129. if (!jdesktop.IsObject())
  130. return false;
  131. const JSONValue& jflags = jdesktop["aiFlagsDefault"];
  132. if (jflags.IsObject())
  133. LoadAIFlagsDefaultConfig(jflags);
  134. return true;
  135. }
  136. }