ProjectParser.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include <algorithm>
  2. #include "ProjectParser.h"
  3. #include "WallpaperEngine/Debugging/Profiling.h"
  4. #include "WallpaperEngine/Logging/Log.h"
  5. #include "WallpaperParser.h"
  6. #include "Localization.h"
  7. #include "PropertyParser.h"
  8. #include "WallpaperEngine/Data/Model/Property.h"
  9. #include "WallpaperEngine/Data/Model/Wallpaper.h"
  10. #include "WallpaperEngine/FileSystem/Container.h"
  11. using namespace WallpaperEngine::Data::Parsers;
  12. static int backgroundId = 0;
  13. ProjectUniquePtr ProjectParser::parse (const JSON& data, AssetLocatorUniquePtr container) {
  14. const auto general = data.optional ("general");
  15. const auto workshopId = data.optional ("workshopid");
  16. auto actualWorkshopId = std::to_string (--backgroundId);
  17. // read as text here, a number or an object would otherwise fail later without saying which key it was
  18. const auto mainFile = data.require<std::string> ("file", "Project's main file missing");
  19. if (workshopId.has_value ()) {
  20. if (workshopId->is_number ()) {
  21. actualWorkshopId = std::to_string (workshopId->get<int> ());
  22. } else if (workshopId->is_string ()) {
  23. actualWorkshopId = workshopId->get<std::string> ();
  24. } else {
  25. sLog.error ("Invalid workshop id: ", workshopId->dump ());
  26. }
  27. }
  28. auto result = std::make_unique<Project> (Project {
  29. .title = data.optional<std::string> ("title", ""),
  30. .type = determineType (data, mainFile),
  31. .workshopId = actualWorkshopId,
  32. .supportsAudioProcessing = general.has_value () && general.value ().optional ("supportsaudioprocessing", false),
  33. .properties = parseProperties (general),
  34. .assetLocator = std::move (container),
  35. });
  36. {
  37. LWE_ZONE_TEXT ("parse wallpaper", mainFile);
  38. result->wallpaper = WallpaperParser::parse (JSON (mainFile), *result);
  39. }
  40. return result;
  41. }
  42. // sub_14011D7D0 / sub_14011E520: Wallpaper Engine never reads the "type" key, it works the type out of the main file's
  43. // extension (.json .pkg .gif scene, .html web, .exe application, .mp4 .wmv .avi .m4v .mov .webm .mkv video) and writes
  44. // that into the key. The declared type is only used here for a file it has no rule for.
  45. std::optional<Project::Type> ProjectParser::typeOfFile (const std::string& file) {
  46. const auto dot = file.rfind ('.');
  47. if (dot == std::string::npos) {
  48. return std::nullopt;
  49. }
  50. auto extension = file.substr (dot);
  51. std::ranges::transform (extension, extension.begin (), tolower);
  52. if (extension == ".json" || extension == ".pkg" || extension == ".gif") {
  53. return Project::Type_Scene;
  54. }
  55. if (extension == ".html") {
  56. return Project::Type_Web;
  57. }
  58. if (extension == ".mp4" || extension == ".wmv" || extension == ".avi" || extension == ".m4v" || extension == ".mov"
  59. || extension == ".webm" || extension == ".mkv") {
  60. return Project::Type_Video;
  61. }
  62. return std::nullopt;
  63. }
  64. Project::Type ProjectParser::determineType (const JSON& data, const std::string& file) {
  65. const auto declared = data.optional<std::string> ("type");
  66. const auto derived = typeOfFile (file);
  67. if (derived.has_value ()) {
  68. if (declared.has_value ()) {
  69. auto name = *declared;
  70. std::ranges::transform (name, name.begin (), tolower);
  71. // a wrong declaration is reported, the file decides like in WE
  72. if (const auto known = typeOfName (name); !known.has_value () || *known != *derived) {
  73. sLog.error (
  74. "Project type '", *declared, "' does not match its main file '", file, "', using the file's"
  75. );
  76. }
  77. }
  78. return *derived;
  79. }
  80. if (!declared.has_value ()) {
  81. sLog.exception ("Project type missing and the main file '", file, "' does not tell it");
  82. }
  83. auto name = *declared;
  84. std::ranges::transform (name, name.begin (), tolower);
  85. return parseType (name);
  86. }
  87. std::optional<Project::Type> ProjectParser::typeOfName (const std::string& type) {
  88. if (type == "scene") {
  89. return Project::Type_Scene;
  90. }
  91. if (type == "video") {
  92. return Project::Type_Video;
  93. }
  94. if (type == "web") {
  95. return Project::Type_Web;
  96. }
  97. return std::nullopt;
  98. }
  99. Project::Type ProjectParser::parseType (const std::string& type) {
  100. if (type == "scene") {
  101. return Project::Type_Scene;
  102. }
  103. if (type == "video") {
  104. return Project::Type_Video;
  105. }
  106. if (type == "web") {
  107. return Project::Type_Web;
  108. }
  109. sLog.exception ("Unsupported project type ", type);
  110. }
  111. Properties ProjectParser::parseProperties (const std::optional<JSON>& data) {
  112. if (!data.has_value ()) {
  113. return {};
  114. }
  115. const auto properties = data.value ().optional ("properties");
  116. if (!properties.has_value ()) {
  117. return {};
  118. }
  119. Properties result = {};
  120. const Localization localization (data);
  121. for (const auto& cur : properties.value ().items ()) {
  122. const auto& property = PropertyParser::parse (cur.value (), cur.key ());
  123. if (property == nullptr) {
  124. continue;
  125. }
  126. property->text = localization.resolve (property->text);
  127. if (auto* combo = dynamic_cast<PropertyCombo*> (property.get ()); combo != nullptr) {
  128. combo->relabel ([&localization] (const std::string& label) { return localization.resolve (label); });
  129. }
  130. result.emplace (cur.key (), property);
  131. }
  132. return result;
  133. }