#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 #include 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 ()); 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 () : fallback; } bool isPassthroughImage (const Object& object) { return object.is () && object.as ()->model != nullptr && object.as ()->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 (objects.size ()); std::vector linked (count, -1); std::vector> 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 (); } // 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& 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 (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 (); } else if (index != 0) { time = static_cast (index) / static_cast (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 () & 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 parseCameraPaths (const JSON& camera, const Project& project) { std::vector 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 (), 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 () && influence != nullptr && influence->is_number () && influence->get () != 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 (); return std::make_unique ( 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 ("center", "Camera must have a center position"), .eye = camera.require ("eye", "Camera must have an eye position"), .up = camera.require ("up", "Camera must have an up position"), }, .paths = parseCameraPaths (camera, project), .projection = { .width = perspective || projection.optional ("auto", false) ? 0 : projection.require ("width", "Projection must have a width"), .height = perspective || projection.optional ("auto", false) ? 0 : projection.require ("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