UserSettingParser.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "UserSettingParser.h"
  2. #include "WallpaperEngine/Data/Model/UserSetting.h"
  3. #include "WallpaperEngine/Data/Model/Property.h"
  4. using namespace WallpaperEngine::Data::Parsers;
  5. using namespace WallpaperEngine::Data::Builders;
  6. UserSettingUniquePtr UserSettingParser::parse (const json& data, const Properties& properties) {
  7. auto value = std::make_unique <DynamicValue> ();
  8. PropertySharedPtr property;
  9. std::optional<ConditionInfo> condition;
  10. auto valueIt = data;
  11. std::string content = data.dump ();
  12. if (data.is_object ()) {
  13. const auto user = data.optional ("user");
  14. const auto script = data.optional ("script");
  15. valueIt = data.require ("value", "User setting must have a value");
  16. // TODO: PARSE SCRIPT VALUES
  17. if (script.has_value () && !script->is_null ()) {
  18. sLog.error ("Found user setting with script value: ", script.value ().dump ());
  19. }
  20. if (user.has_value () && !user->is_null ()) {
  21. std::string source;
  22. if (const auto& it = *user; it.is_string ()) {
  23. source = it;
  24. } else {
  25. condition = ConditionInfo {
  26. .name = it.require <std::string> ("name", "Name for conditional setting must be present"),
  27. .condition = it.require <std::string> ("condition", "Condition for conditional setting must be present"),
  28. };
  29. source = condition.value ().name;
  30. }
  31. if (const auto propertyIt = properties.find (source); propertyIt != properties.end ()) {
  32. property = propertyIt->second;
  33. }
  34. }
  35. }
  36. // actual value parsing
  37. if (valueIt.is_string ()) {
  38. std::string str = valueIt;
  39. // TODO: VALIDATE THIS IS RIGHT?
  40. if (int size = VectorBuilder::preparseSize (str); size == 2) {
  41. value->update (static_cast<glm::vec2> (valueIt));
  42. } else if (size == 3) {
  43. value->update (static_cast<glm::vec3> (valueIt));
  44. } else {
  45. value->update (static_cast<glm::vec4> (valueIt));
  46. }
  47. } else if (valueIt.is_number_integer ()) {
  48. value->update (valueIt.get <int> ());
  49. } else if (valueIt.is_number_float ()) {
  50. value->update (valueIt.get <float> ());
  51. } else if (valueIt.is_boolean ()) {
  52. value->update (valueIt.get <bool> ());
  53. } else if (valueIt.is_null ()) {
  54. // null value with no connection to property
  55. value->update ();
  56. }
  57. // TODO: This might need to be removed if it causes issues with default values
  58. // Connect to property if one is specified (this allows property overrides to propagate)
  59. if (property != nullptr) {
  60. value->connect (property.get());
  61. }
  62. return std::make_unique <UserSetting> (UserSetting {
  63. .value = std::move (value),
  64. .property = property,
  65. .condition = condition,
  66. });
  67. }