| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440 |
- #include "WallpaperParser.h"
- #include "ObjectParser.h"
- #include "WallpaperEngine/Data/Model/Model.h"
- #include "WallpaperEngine/Data/Model/Object.h"
- #include "WallpaperEngine/Data/Model/Project.h"
- #include "WallpaperEngine/Data/Model/Wallpaper.h"
- #include "WallpaperEngine/FileSystem/Container.h"
- #include "WallpaperEngine/Logging/Log.h"
- #include <algorithm>
- #include <sstream>
- using namespace WallpaperEngine::Data::Parsers;
- namespace {
- // vectors in camera path files are "x y z" strings read with atof, missing numbers are 0
- glm::vec3 parsePathVector (const JSON& transform, const char* key) {
- glm::vec3 result (0.0f);
- const auto it = transform.find (key);
- if (it == transform.end () || !it->is_string ()) {
- return result;
- }
- std::istringstream stream (it->get<std::string> ());
- for (int component = 0; component < 3 && stream >> result[component]; component++) { }
- return result;
- }
- float numberOr (const JSON& data, const char* key, const float fallback) {
- const auto it = data.find (key);
- return it != data.end () && it->is_number () ? it->get<float> () : fallback;
- }
- bool isPassthroughImage (const Object& object) {
- return object.is<Image> () && object.as<Image> ()->model != nullptr && object.as<Image> ()->model->passthrough;
- }
- // WE links an object to its parent while creating the objects in array order and resolves parents further down the
- // array in a second pass (sub_140186C90), each time dropping the link when passthrough layers would nest deeper than 3:
- // the passthrough ancestors plus the longest passthrough chain from the object down (sub_1401DE470, sub_1401DE750)
- void dropDeepPassthroughParents (ObjectList& objects) {
- const int count = static_cast<int> (objects.size ());
- std::vector<int> linked (count, -1);
- std::vector<std::vector<int>> children (count);
- const auto ancestorsDepth = [&] (int index) {
- int depth = 0;
- for (int steps = 0; index != -1 && steps < count; steps++, index = linked[index]) {
- depth += isPassthroughImage (*objects[index]) ? 1 : 0;
- }
- return depth;
- };
- const auto chainDepth = [&] (const auto& self, int index, int level) -> int {
- const int own = isPassthroughImage (*objects[index]) ? 1 : 0;
- int depth = own;
- if (level < count) {
- for (const int child : children[index]) {
- depth = std::max (depth, self (self, child, level + 1) + own);
- }
- }
- return depth;
- };
- const auto link = [&] (int index, int parent) {
- if (linked[index] != -1) {
- std::erase (children[linked[index]], index);
- }
- linked[index] = parent;
- children[parent].push_back (index);
- };
- const auto tooDeep
- = [&] (int index, int parent) { return ancestorsDepth (parent) + chainDepth (chainDepth, index, 0) > 3; };
- const auto drop = [&] (int index) {
- sLog.out (
- "Object ", objects[index]->id, " loses its parent ", objects[index]->parent.value (),
- ": passthrough layers nest deeper than 3"
- );
- objects[index]->parent = std::nullopt;
- };
- for (int index = 0; index < count; index++) {
- if (!objects[index]->parent.has_value ()) {
- continue;
- }
- int parent = -1;
- for (int other = 0; other <= index; other++) {
- if (objects[other]->id == objects[index]->parent.value ()) {
- parent = other;
- break;
- }
- }
- if (parent == index || (parent != -1 && tooDeep (index, parent))) {
- drop (index);
- } else if (parent != -1) {
- link (index, parent);
- }
- }
- for (int index = 0; index < count; index++) {
- if (!objects[index]->parent.has_value ()) {
- continue;
- }
- int parent = -1;
- for (int other = 0; other < count; other++) {
- if (other != index && objects[other]->id == objects[index]->parent.value ()) {
- parent = other;
- break;
- }
- }
- if (parent == -1 || linked[index] == parent) {
- continue;
- }
- // a parent below the object is a cycle, left to the scene's own guards
- bool below = false;
- for (int current = parent, steps = 0; current != -1 && steps < count; current = linked[current], steps++) {
- below = below || current == index;
- }
- if (below) {
- continue;
- }
- if (tooDeep (index, parent)) {
- if (linked[index] != -1) {
- std::erase (children[linked[index]], index);
- linked[index] = -1;
- }
- drop (index);
- } else {
- link (index, parent);
- }
- }
- }
- bool isDisabled (const JSON& data) {
- const auto it = data.find ("disabled");
- return it != data.end () && it->is_boolean () && it->get<bool> ();
- }
- // one file listed under "camera" "paths" (sub_140198E20), every enabled path goes into the list
- void parseCameraPathFile (const std::string& filename, const Project& project, std::vector<CameraPath>& paths) {
- JSON file;
- try {
- file = JSON::parseAsset (project.assetLocator->readString (filename));
- } catch (const std::exception& e) {
- sLog.error ("Cannot load camera path file ", filename, ": ", e.what ());
- return;
- }
- const auto list = file.find ("paths");
- if (list == file.end () || !list->is_array ()) {
- return;
- }
- for (const auto& path : *list) {
- if (!path.is_object ()) {
- break;
- }
- const auto transforms = path.find ("transforms");
- // WE stops at the first path without transforms
- if (transforms == path.end () || !transforms->is_array () || transforms->empty ()) {
- break;
- }
- if (isDisabled (path)) {
- continue;
- }
- CameraPath result { .keys = {}, .duration = numberOr (path, "duration", 0.0f) };
- const int count = static_cast<int> (transforms->size ());
- for (int index = 0; index < count; index++) {
- const auto& transform = (*transforms)[index];
- if (!transform.is_object () || isDisabled (transform)) {
- continue;
- }
- // without a timestamp the keys spread evenly over the duration
- float time = 0.0f;
- if (const auto stamp = transform.find ("timestamp"); stamp != transform.end () && stamp->is_number ()) {
- time = stamp->get<float> ();
- } else if (index != 0) {
- time = static_cast<float> (index) / static_cast<float> (count - 1) * result.duration;
- }
- result.keys.push_back (
- CameraPathKey {
- .time = time,
- .eye = parsePathVector (transform, "eye"),
- .center = parsePathVector (transform, "center"),
- .up = parsePathVector (transform, "up"),
- .zoom = numberOr (transform, "zoom", 1.0f),
- }
- );
- }
- paths.push_back (std::move (result));
- }
- }
- decltype (SceneData::lightConfig) parseLightConfig (const JSON& general) {
- decltype (SceneData::lightConfig) config;
- const auto it = general.find ("lightconfig");
- if (it == general.end () || !it->is_object ()) {
- return config;
- }
- const auto count = [&it] (const char* key, const int mask) {
- const auto value = it->find (key);
- return value != it->end () && value->is_number () ? value->get<int> () & mask : 0;
- };
- config.point = count ("point", 0xF);
- config.spot = count ("spot", 0xF);
- config.tube = count ("tube", 0xF);
- config.directional = count ("directional", 0xF);
- config.spotCookie = count ("spotcookie", 3);
- config.spotShadow = count ("spotshadow", 3);
- config.spotShadowCookie = count ("spotshadowcookie", 3);
- config.directionalShadow = count ("directionalshadow", 3);
- config.pointShadow = count ("pointshadow", 3);
- return config;
- }
- std::vector<CameraPath> parseCameraPaths (const JSON& camera, const Project& project) {
- std::vector<CameraPath> paths;
- const auto list = camera.find ("paths");
- if (list == camera.end () || !list->is_array ()) {
- return paths;
- }
- for (const auto& file : *list) {
- if (file.is_string ()) {
- parseCameraPathFile (file.get<std::string> (), project, paths);
- }
- }
- return paths;
- }
- // a general member's value the way sub_14010D850 reads it: {"value": ...} objects unwrapped, nullptr when missing
- const JSON* generalValue (const JSON& general, const char* name) {
- const auto it = general.find (name);
- if (it == general.end () || !it->is_object ()) {
- return it == general.end () ? nullptr : &*it;
- }
- const auto value = it->find ("value");
- return value == it->end () ? nullptr : &*value;
- }
- SceneData::EngineProperties parseEngineProperties (const JSON& general, const JSON& objects, bool perspective) {
- SceneData::EngineProperties result { .volume = false, .alignment = !perspective, .cameraParallax = false };
- for (const auto& object : objects) {
- if (!object.is_object ()) {
- continue;
- }
- const auto model = object.find ("model");
- const auto sound = object.find ("sound");
- if ((model == object.end () || model->is_null ()) && sound != object.end () && !sound->is_null ()) {
- result.volume = true;
- }
- }
- const auto* parallax = generalValue (general, "cameraparallax");
- const auto* influence = generalValue (general, "cameraparallaxmouseinfluence");
- result.cameraParallax = parallax != nullptr && parallax->is_boolean () && parallax->get<bool> ()
- && influence != nullptr && influence->is_number () && influence->get<float> () != 0.0f;
- return result;
- }
- } // namespace
- WallpaperUniquePtr WallpaperParser::parse (const JSON& file, Project& project) {
- switch (project.type) {
- case Project::Type_Scene:
- return parseScene (file, project);
- case Project::Type_Video:
- return parseVideo (file, project);
- case Project::Type_Web:
- return parseWeb (file, project);
- default:
- sLog.exception ("Unexpected project type value found... This is likely a bug");
- }
- }
- SceneUniquePtr WallpaperParser::parseScene (const JSON& file, Project& project) {
- const auto scene = JSON::parseAsset (project.assetLocator->readString (file));
- const auto camera = scene.require ("camera", "Scenes must have a camera section");
- const auto general = scene.require ("general", "Scenes must have a general section");
- // null (or missing) means a perspective camera, a 3D scene
- const auto projectionIt = general.find ("orthogonalprojection");
- const bool perspective = projectionIt == general.end () || projectionIt->is_null ();
- const JSON projection = perspective ? JSON::object () : JSON (*projectionIt);
- const auto objects = scene.require ("objects", "Scenes must have an objects section");
- const auto& properties = project.properties;
- // particles read it while the objects get parsed below
- project.sceneVersion = scene.optional ("version", 0);
- // missing keys use the scene constructor's defaults (sub_140186C90); spritesheetrefreshsync is a plain bool
- const auto syncIt = general.find ("spritesheetrefreshsync");
- const bool spriteSheetRefreshSync = syncIt != general.end () && syncIt->is_boolean () && syncIt->get<bool> ();
- return std::make_unique <Scene> (
- WallpaperData {
- .filename = "",
- .project = project
- }, SceneData {
- .colors = {
- .ambient = general.user ("ambientcolor", properties, glm::vec3 (0.0f)),
- .skylight = general.user ("skylightcolor", properties, glm::vec3 (0.0f)),
- .clear = general.user ("clearcolor", properties, glm::vec3 (1.0f)),
- },
- .fog = {
- .distanceEnabled = general.user ("fogdistance", properties, false),
- .distanceColor = general.user ("fogdistancecolor", properties, glm::vec3 (0.0f)),
- .distanceStart = general.user ("fogdistancestart", properties, 1.0f),
- .distanceEnd = general.user ("fogdistanceend", properties, 5.0f),
- .distanceStartDensity = general.user ("fogdistancestartdensity", properties, 0.0f),
- .distanceEndDensity = general.user ("fogdistanceenddensity", properties, 1.0f),
- .heightEnabled = general.user ("fogheight", properties, false),
- .heightColor = general.user ("fogheightcolor", properties, glm::vec3 (0.0f)),
- .heightStart = general.user ("fogheightstart", properties, 1.0f),
- .heightEnd = general.user ("fogheightend", properties, -3.0f),
- .heightStartDensity = general.user ("fogheightstartdensity", properties, 0.0f),
- .heightEndDensity = general.user ("fogheightenddensity", properties, 1.0f),
- },
- .lightConfig = parseLightConfig (general),
- .camera = {
- .fade = general.user ("camerafade", properties, false),
- .preview = general.optional ("camerapreview", false),
- .bloom = {
- .enabled = general.user ("bloom", properties, false),
- .strength = general.user ("bloomstrength", properties, 2.0f),
- .threshold = general.user ("bloomthreshold", properties, 0.65f),
- .hdr = general.user ("hdr", properties, false),
- .hdrStrength = general.user ("bloomhdrstrength", properties, 2.0f),
- .hdrThreshold = general.user ("bloomhdrthreshold", properties, 1.0f),
- .hdrFeather = general.user ("bloomhdrfeather", properties, 0.1f),
- .hdrScatter = general.user ("bloomhdrscatter", properties, 1.619f),
- .hdrIterations = general.user ("bloomhdriterations", properties, 8),
- .tint = general.user ("bloomtint", properties, glm::vec3 (1.0f)),
- },
- .parallax = {
- .enabled = general.user ("cameraparallax", properties, false),
- .amount = general.user ("cameraparallaxamount", properties, 0.5f),
- .delay = general.user ("cameraparallaxdelay", properties, 0.1f),
- .mouseInfluence = general.user ("cameraparallaxmouseinfluence", properties, 0.5f),
- },
- .shake = {
- .enabled = general.user ("camerashake", properties, false),
- .amplitude = general.user ("camerashakeamplitude", properties, 0.5f),
- .roughness = general.user ("camerashakeroughness", properties, 1.0f),
- .speed = general.user ("camerashakespeed", properties, 3.0f),
- },
- .configuration = {
- .center = camera.require <glm::vec3> ("center", "Camera must have a center position"),
- .eye = camera.require <glm::vec3> ("eye", "Camera must have an eye position"),
- .up = camera.require <glm::vec3> ("up", "Camera must have an up position"),
- },
- .paths = parseCameraPaths (camera, project),
- .projection = {
- .width = perspective || projection.optional ("auto", false) ? 0 : projection.require <int> ("width", "Projection must have a width"),
- .height = perspective || projection.optional ("auto", false) ? 0 : projection.require <int> ("height", "Projection must have a height"),
- .isAuto = perspective || projection.optional ("auto", false),
- .isPerspective = perspective,
- .nearz = general.user ("nearz", properties, 0.1f),
- .farz = general.user ("farz", properties, 10000.0f),
- .fov = general.user ("fov", properties, 50.0f),
- .perspectiveOverrideFov = general.user ("perspectiveoverridefov", properties, 95.0f),
- .zoom = general.user ("zoom", properties, 1.0f),
- }
- },
- .transparentSorting = general.user ("transparentsorting", properties, false),
- .spriteSheetRefreshSync = spriteSheetRefreshSync,
- .physics = {
- .gravityDirection = general.user ("gravitydirection", properties, glm::vec3 (0.0f, -1.0f, 0.0f)),
- .gravityStrength = general.user ("gravitystrength", properties, 1.0f),
- .windEnabled = general.user ("windenabled", properties, false),
- .windDirection = general.user ("winddirection", properties, glm::vec3 (0.70710677f, 0.70710677f, 0.0f)),
- .windStrength = general.user ("windstrength", properties, 1.0f),
- },
- .engineProperties = parseEngineProperties (general, objects, perspective),
- .objects = parseObjects (objects, project),
- }
- );
- }
- VideoUniquePtr WallpaperParser::parseVideo (const JSON& file, Project& project) {
- return std::make_unique<Video> (WallpaperData { .filename = file, .project = project });
- }
- WebUniquePtr WallpaperParser::parseWeb (const JSON& file, Project& project) {
- return std::make_unique<Web> (WallpaperData {
- .filename = file,
- .project = project,
- });
- }
- ObjectList WallpaperParser::parseObjects (const JSON& objects, const Project& project) {
- ObjectList result = {};
- for (const auto& cur : objects) {
- result.emplace_back (ObjectParser::parse (cur, project));
- }
- dropDeepPassthroughParents (result);
- return result;
- }
|