CEffect.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include "CEffect.h"
  2. #include <utility>
  3. #include "WallpaperEngine/FileSystem/FileSystem.h"
  4. using namespace WallpaperEngine::Core::Objects;
  5. CEffect::CEffect (
  6. std::string name,
  7. std::string description,
  8. std::string group,
  9. std::string preview):
  10. m_name (std::move(name)),
  11. m_description (std::move(description)),
  12. m_group (std::move(group)),
  13. m_preview (std::move(preview))
  14. {
  15. }
  16. CEffect* CEffect::fromJSON (json data)
  17. {
  18. json::const_iterator file_it = data.find ("file");
  19. if (file_it == data.end ())
  20. {
  21. throw std::runtime_error ("Object effect must have a file");
  22. }
  23. json content = json::parse (WallpaperEngine::FileSystem::loadFullFile ((*file_it).get <std::string> ().c_str ()));
  24. json::const_iterator name_it = content.find ("name");
  25. json::const_iterator description_it = content.find ("description");
  26. json::const_iterator group_it = content.find ("group");
  27. json::const_iterator preview_it = content.find ("preview");
  28. json::const_iterator passes_it = content.find ("passes");
  29. json::const_iterator dependencies_it = content.find ("dependencies");
  30. if (name_it == content.end ())
  31. {
  32. throw std::runtime_error ("Effect must have a name");
  33. }
  34. if (description_it == content.end ())
  35. {
  36. throw std::runtime_error ("Effect must have a description");
  37. }
  38. if (group_it == content.end ())
  39. {
  40. throw std::runtime_error ("Effect must have a group");
  41. }
  42. if (preview_it == content.end ())
  43. {
  44. throw std::runtime_error ("Effect must have a preview");
  45. }
  46. if (passes_it == content.end ())
  47. {
  48. throw std::runtime_error ("Effect must have a pass list");
  49. }
  50. if (dependencies_it == content.end ())
  51. {
  52. throw std::runtime_error ("Effect must have dependencies");
  53. }
  54. CEffect* effect = new CEffect (
  55. *name_it,
  56. *description_it,
  57. *group_it,
  58. *preview_it
  59. );
  60. json::const_iterator cur = (*passes_it).begin ();
  61. json::const_iterator end = (*passes_it).end ();
  62. for (; cur != end; cur ++)
  63. {
  64. json::const_iterator materialfile = (*cur).find ("material");
  65. if (materialfile == (*cur).end ())
  66. {
  67. throw std::runtime_error ("Effect pass must have a material file");
  68. }
  69. effect->insertMaterial (
  70. Images::CMaterial::fromFile ((*materialfile).get <std::string> ().c_str ())
  71. );
  72. }
  73. cur = (*dependencies_it).begin ();
  74. end = (*dependencies_it).end ();
  75. for (; cur != end; cur ++)
  76. {
  77. effect->insertDependency (*cur);
  78. }
  79. return effect;
  80. }
  81. std::vector<std::string>* CEffect::getDependencies ()
  82. {
  83. return &this->m_dependencies;
  84. }
  85. std::vector<Images::CMaterial*>* CEffect::getMaterials ()
  86. {
  87. return &this->m_materials;
  88. }
  89. void CEffect::insertDependency (const std::string& dep)
  90. {
  91. this->m_dependencies.push_back (dep);
  92. }
  93. void CEffect::insertMaterial (Images::CMaterial* material)
  94. {
  95. this->m_materials.push_back (material);
  96. }