WallpaperParser.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. #include "WallpaperParser.h"
  2. #include "ObjectParser.h"
  3. #include "WallpaperEngine/Data/Model/Model.h"
  4. #include "WallpaperEngine/Data/Model/Object.h"
  5. #include "WallpaperEngine/Data/Model/Project.h"
  6. #include "WallpaperEngine/Data/Model/Wallpaper.h"
  7. #include "WallpaperEngine/FileSystem/Container.h"
  8. #include "WallpaperEngine/Logging/Log.h"
  9. #include <algorithm>
  10. #include <sstream>
  11. using namespace WallpaperEngine::Data::Parsers;
  12. namespace {
  13. // vectors in camera path files are "x y z" strings read with atof, missing numbers are 0
  14. glm::vec3 parsePathVector (const JSON& transform, const char* key) {
  15. glm::vec3 result (0.0f);
  16. const auto it = transform.find (key);
  17. if (it == transform.end () || !it->is_string ()) {
  18. return result;
  19. }
  20. std::istringstream stream (it->get<std::string> ());
  21. for (int component = 0; component < 3 && stream >> result[component]; component++) { }
  22. return result;
  23. }
  24. float numberOr (const JSON& data, const char* key, const float fallback) {
  25. const auto it = data.find (key);
  26. return it != data.end () && it->is_number () ? it->get<float> () : fallback;
  27. }
  28. bool isPassthroughImage (const Object& object) {
  29. return object.is<Image> () && object.as<Image> ()->model != nullptr && object.as<Image> ()->model->passthrough;
  30. }
  31. // WE links an object to its parent while creating the objects in array order and resolves parents further down the
  32. // array in a second pass (sub_140186C90), each time dropping the link when passthrough layers would nest deeper than 3:
  33. // the passthrough ancestors plus the longest passthrough chain from the object down (sub_1401DE470, sub_1401DE750)
  34. void dropDeepPassthroughParents (ObjectList& objects) {
  35. const int count = static_cast<int> (objects.size ());
  36. std::vector<int> linked (count, -1);
  37. std::vector<std::vector<int>> children (count);
  38. const auto ancestorsDepth = [&] (int index) {
  39. int depth = 0;
  40. for (int steps = 0; index != -1 && steps < count; steps++, index = linked[index]) {
  41. depth += isPassthroughImage (*objects[index]) ? 1 : 0;
  42. }
  43. return depth;
  44. };
  45. const auto chainDepth = [&] (const auto& self, int index, int level) -> int {
  46. const int own = isPassthroughImage (*objects[index]) ? 1 : 0;
  47. int depth = own;
  48. if (level < count) {
  49. for (const int child : children[index]) {
  50. depth = std::max (depth, self (self, child, level + 1) + own);
  51. }
  52. }
  53. return depth;
  54. };
  55. const auto link = [&] (int index, int parent) {
  56. if (linked[index] != -1) {
  57. std::erase (children[linked[index]], index);
  58. }
  59. linked[index] = parent;
  60. children[parent].push_back (index);
  61. };
  62. const auto tooDeep
  63. = [&] (int index, int parent) { return ancestorsDepth (parent) + chainDepth (chainDepth, index, 0) > 3; };
  64. const auto drop = [&] (int index) {
  65. sLog.out (
  66. "Object ", objects[index]->id, " loses its parent ", objects[index]->parent.value (),
  67. ": passthrough layers nest deeper than 3"
  68. );
  69. objects[index]->parent = std::nullopt;
  70. };
  71. for (int index = 0; index < count; index++) {
  72. if (!objects[index]->parent.has_value ()) {
  73. continue;
  74. }
  75. int parent = -1;
  76. for (int other = 0; other <= index; other++) {
  77. if (objects[other]->id == objects[index]->parent.value ()) {
  78. parent = other;
  79. break;
  80. }
  81. }
  82. if (parent == index || (parent != -1 && tooDeep (index, parent))) {
  83. drop (index);
  84. } else if (parent != -1) {
  85. link (index, parent);
  86. }
  87. }
  88. for (int index = 0; index < count; index++) {
  89. if (!objects[index]->parent.has_value ()) {
  90. continue;
  91. }
  92. int parent = -1;
  93. for (int other = 0; other < count; other++) {
  94. if (other != index && objects[other]->id == objects[index]->parent.value ()) {
  95. parent = other;
  96. break;
  97. }
  98. }
  99. if (parent == -1 || linked[index] == parent) {
  100. continue;
  101. }
  102. // a parent below the object is a cycle, left to the scene's own guards
  103. bool below = false;
  104. for (int current = parent, steps = 0; current != -1 && steps < count; current = linked[current], steps++) {
  105. below = below || current == index;
  106. }
  107. if (below) {
  108. continue;
  109. }
  110. if (tooDeep (index, parent)) {
  111. if (linked[index] != -1) {
  112. std::erase (children[linked[index]], index);
  113. linked[index] = -1;
  114. }
  115. drop (index);
  116. } else {
  117. link (index, parent);
  118. }
  119. }
  120. }
  121. bool isDisabled (const JSON& data) {
  122. const auto it = data.find ("disabled");
  123. return it != data.end () && it->is_boolean () && it->get<bool> ();
  124. }
  125. // one file listed under "camera" "paths" (sub_140198E20), every enabled path goes into the list
  126. void parseCameraPathFile (const std::string& filename, const Project& project, std::vector<CameraPath>& paths) {
  127. JSON file;
  128. try {
  129. file = JSON::parseAsset (project.assetLocator->readString (filename));
  130. } catch (const std::exception& e) {
  131. sLog.error ("Cannot load camera path file ", filename, ": ", e.what ());
  132. return;
  133. }
  134. const auto list = file.find ("paths");
  135. if (list == file.end () || !list->is_array ()) {
  136. return;
  137. }
  138. for (const auto& path : *list) {
  139. if (!path.is_object ()) {
  140. break;
  141. }
  142. const auto transforms = path.find ("transforms");
  143. // WE stops at the first path without transforms
  144. if (transforms == path.end () || !transforms->is_array () || transforms->empty ()) {
  145. break;
  146. }
  147. if (isDisabled (path)) {
  148. continue;
  149. }
  150. CameraPath result { .keys = {}, .duration = numberOr (path, "duration", 0.0f) };
  151. const int count = static_cast<int> (transforms->size ());
  152. for (int index = 0; index < count; index++) {
  153. const auto& transform = (*transforms)[index];
  154. if (!transform.is_object () || isDisabled (transform)) {
  155. continue;
  156. }
  157. // without a timestamp the keys spread evenly over the duration
  158. float time = 0.0f;
  159. if (const auto stamp = transform.find ("timestamp"); stamp != transform.end () && stamp->is_number ()) {
  160. time = stamp->get<float> ();
  161. } else if (index != 0) {
  162. time = static_cast<float> (index) / static_cast<float> (count - 1) * result.duration;
  163. }
  164. result.keys.push_back (
  165. CameraPathKey {
  166. .time = time,
  167. .eye = parsePathVector (transform, "eye"),
  168. .center = parsePathVector (transform, "center"),
  169. .up = parsePathVector (transform, "up"),
  170. .zoom = numberOr (transform, "zoom", 1.0f),
  171. }
  172. );
  173. }
  174. paths.push_back (std::move (result));
  175. }
  176. }
  177. decltype (SceneData::lightConfig) parseLightConfig (const JSON& general) {
  178. decltype (SceneData::lightConfig) config;
  179. const auto it = general.find ("lightconfig");
  180. if (it == general.end () || !it->is_object ()) {
  181. return config;
  182. }
  183. const auto count = [&it] (const char* key, const int mask) {
  184. const auto value = it->find (key);
  185. return value != it->end () && value->is_number () ? value->get<int> () & mask : 0;
  186. };
  187. config.point = count ("point", 0xF);
  188. config.spot = count ("spot", 0xF);
  189. config.tube = count ("tube", 0xF);
  190. config.directional = count ("directional", 0xF);
  191. config.spotCookie = count ("spotcookie", 3);
  192. config.spotShadow = count ("spotshadow", 3);
  193. config.spotShadowCookie = count ("spotshadowcookie", 3);
  194. config.directionalShadow = count ("directionalshadow", 3);
  195. config.pointShadow = count ("pointshadow", 3);
  196. return config;
  197. }
  198. std::vector<CameraPath> parseCameraPaths (const JSON& camera, const Project& project) {
  199. std::vector<CameraPath> paths;
  200. const auto list = camera.find ("paths");
  201. if (list == camera.end () || !list->is_array ()) {
  202. return paths;
  203. }
  204. for (const auto& file : *list) {
  205. if (file.is_string ()) {
  206. parseCameraPathFile (file.get<std::string> (), project, paths);
  207. }
  208. }
  209. return paths;
  210. }
  211. // a general member's value the way sub_14010D850 reads it: {"value": ...} objects unwrapped, nullptr when missing
  212. const JSON* generalValue (const JSON& general, const char* name) {
  213. const auto it = general.find (name);
  214. if (it == general.end () || !it->is_object ()) {
  215. return it == general.end () ? nullptr : &*it;
  216. }
  217. const auto value = it->find ("value");
  218. return value == it->end () ? nullptr : &*value;
  219. }
  220. SceneData::EngineProperties parseEngineProperties (const JSON& general, const JSON& objects, bool perspective) {
  221. SceneData::EngineProperties result { .volume = false, .alignment = !perspective, .cameraParallax = false };
  222. for (const auto& object : objects) {
  223. if (!object.is_object ()) {
  224. continue;
  225. }
  226. const auto model = object.find ("model");
  227. const auto sound = object.find ("sound");
  228. if ((model == object.end () || model->is_null ()) && sound != object.end () && !sound->is_null ()) {
  229. result.volume = true;
  230. }
  231. }
  232. const auto* parallax = generalValue (general, "cameraparallax");
  233. const auto* influence = generalValue (general, "cameraparallaxmouseinfluence");
  234. result.cameraParallax = parallax != nullptr && parallax->is_boolean () && parallax->get<bool> ()
  235. && influence != nullptr && influence->is_number () && influence->get<float> () != 0.0f;
  236. return result;
  237. }
  238. } // namespace
  239. WallpaperUniquePtr WallpaperParser::parse (const JSON& file, Project& project) {
  240. switch (project.type) {
  241. case Project::Type_Scene:
  242. return parseScene (file, project);
  243. case Project::Type_Video:
  244. return parseVideo (file, project);
  245. case Project::Type_Web:
  246. return parseWeb (file, project);
  247. default:
  248. sLog.exception ("Unexpected project type value found... This is likely a bug");
  249. }
  250. }
  251. SceneUniquePtr WallpaperParser::parseScene (const JSON& file, Project& project) {
  252. const auto scene = JSON::parseAsset (project.assetLocator->readString (file));
  253. const auto camera = scene.require ("camera", "Scenes must have a camera section");
  254. const auto general = scene.require ("general", "Scenes must have a general section");
  255. // null (or missing) means a perspective camera, a 3D scene
  256. const auto projectionIt = general.find ("orthogonalprojection");
  257. const bool perspective = projectionIt == general.end () || projectionIt->is_null ();
  258. const JSON projection = perspective ? JSON::object () : JSON (*projectionIt);
  259. const auto objects = scene.require ("objects", "Scenes must have an objects section");
  260. const auto& properties = project.properties;
  261. // particles read it while the objects get parsed below
  262. project.sceneVersion = scene.optional ("version", 0);
  263. // missing keys use the scene constructor's defaults (sub_140186C90); spritesheetrefreshsync is a plain bool
  264. const auto syncIt = general.find ("spritesheetrefreshsync");
  265. const bool spriteSheetRefreshSync = syncIt != general.end () && syncIt->is_boolean () && syncIt->get<bool> ();
  266. return std::make_unique <Scene> (
  267. WallpaperData {
  268. .filename = "",
  269. .project = project
  270. }, SceneData {
  271. .colors = {
  272. .ambient = general.user ("ambientcolor", properties, glm::vec3 (0.0f)),
  273. .skylight = general.user ("skylightcolor", properties, glm::vec3 (0.0f)),
  274. .clear = general.user ("clearcolor", properties, glm::vec3 (1.0f)),
  275. },
  276. .fog = {
  277. .distanceEnabled = general.user ("fogdistance", properties, false),
  278. .distanceColor = general.user ("fogdistancecolor", properties, glm::vec3 (0.0f)),
  279. .distanceStart = general.user ("fogdistancestart", properties, 1.0f),
  280. .distanceEnd = general.user ("fogdistanceend", properties, 5.0f),
  281. .distanceStartDensity = general.user ("fogdistancestartdensity", properties, 0.0f),
  282. .distanceEndDensity = general.user ("fogdistanceenddensity", properties, 1.0f),
  283. .heightEnabled = general.user ("fogheight", properties, false),
  284. .heightColor = general.user ("fogheightcolor", properties, glm::vec3 (0.0f)),
  285. .heightStart = general.user ("fogheightstart", properties, 1.0f),
  286. .heightEnd = general.user ("fogheightend", properties, -3.0f),
  287. .heightStartDensity = general.user ("fogheightstartdensity", properties, 0.0f),
  288. .heightEndDensity = general.user ("fogheightenddensity", properties, 1.0f),
  289. },
  290. .lightConfig = parseLightConfig (general),
  291. .camera = {
  292. .fade = general.user ("camerafade", properties, false),
  293. .preview = general.optional ("camerapreview", false),
  294. .bloom = {
  295. .enabled = general.user ("bloom", properties, false),
  296. .strength = general.user ("bloomstrength", properties, 2.0f),
  297. .threshold = general.user ("bloomthreshold", properties, 0.65f),
  298. .hdr = general.user ("hdr", properties, false),
  299. .hdrStrength = general.user ("bloomhdrstrength", properties, 2.0f),
  300. .hdrThreshold = general.user ("bloomhdrthreshold", properties, 1.0f),
  301. .hdrFeather = general.user ("bloomhdrfeather", properties, 0.1f),
  302. .hdrScatter = general.user ("bloomhdrscatter", properties, 1.619f),
  303. .hdrIterations = general.user ("bloomhdriterations", properties, 8),
  304. .tint = general.user ("bloomtint", properties, glm::vec3 (1.0f)),
  305. },
  306. .parallax = {
  307. .enabled = general.user ("cameraparallax", properties, false),
  308. .amount = general.user ("cameraparallaxamount", properties, 0.5f),
  309. .delay = general.user ("cameraparallaxdelay", properties, 0.1f),
  310. .mouseInfluence = general.user ("cameraparallaxmouseinfluence", properties, 0.5f),
  311. },
  312. .shake = {
  313. .enabled = general.user ("camerashake", properties, false),
  314. .amplitude = general.user ("camerashakeamplitude", properties, 0.5f),
  315. .roughness = general.user ("camerashakeroughness", properties, 1.0f),
  316. .speed = general.user ("camerashakespeed", properties, 3.0f),
  317. },
  318. .configuration = {
  319. .center = camera.require <glm::vec3> ("center", "Camera must have a center position"),
  320. .eye = camera.require <glm::vec3> ("eye", "Camera must have an eye position"),
  321. .up = camera.require <glm::vec3> ("up", "Camera must have an up position"),
  322. },
  323. .paths = parseCameraPaths (camera, project),
  324. .projection = {
  325. .width = perspective || projection.optional ("auto", false) ? 0 : projection.require <int> ("width", "Projection must have a width"),
  326. .height = perspective || projection.optional ("auto", false) ? 0 : projection.require <int> ("height", "Projection must have a height"),
  327. .isAuto = perspective || projection.optional ("auto", false),
  328. .isPerspective = perspective,
  329. .nearz = general.user ("nearz", properties, 0.1f),
  330. .farz = general.user ("farz", properties, 10000.0f),
  331. .fov = general.user ("fov", properties, 50.0f),
  332. .perspectiveOverrideFov = general.user ("perspectiveoverridefov", properties, 95.0f),
  333. .zoom = general.user ("zoom", properties, 1.0f),
  334. }
  335. },
  336. .transparentSorting = general.user ("transparentsorting", properties, false),
  337. .spriteSheetRefreshSync = spriteSheetRefreshSync,
  338. .physics = {
  339. .gravityDirection = general.user ("gravitydirection", properties, glm::vec3 (0.0f, -1.0f, 0.0f)),
  340. .gravityStrength = general.user ("gravitystrength", properties, 1.0f),
  341. .windEnabled = general.user ("windenabled", properties, false),
  342. .windDirection = general.user ("winddirection", properties, glm::vec3 (0.70710677f, 0.70710677f, 0.0f)),
  343. .windStrength = general.user ("windstrength", properties, 1.0f),
  344. },
  345. .engineProperties = parseEngineProperties (general, objects, perspective),
  346. .objects = parseObjects (objects, project),
  347. }
  348. );
  349. }
  350. VideoUniquePtr WallpaperParser::parseVideo (const JSON& file, Project& project) {
  351. return std::make_unique<Video> (WallpaperData { .filename = file, .project = project });
  352. }
  353. WebUniquePtr WallpaperParser::parseWeb (const JSON& file, Project& project) {
  354. return std::make_unique<Web> (WallpaperData {
  355. .filename = file,
  356. .project = project,
  357. });
  358. }
  359. ObjectList WallpaperParser::parseObjects (const JSON& objects, const Project& project) {
  360. ObjectList result = {};
  361. for (const auto& cur : objects) {
  362. result.emplace_back (ObjectParser::parse (cur, project));
  363. }
  364. dropDeepPassthroughParents (result);
  365. return result;
  366. }