Bladeren bron

added better json parsing, removed annoying console.log spam for some wallpapers

UwU 5 dagen geleden
bovenliggende
commit
2ff248ffd2

+ 2 - 2
src/WallpaperEngine/Application/WallpaperApplication.cpp

@@ -283,7 +283,7 @@ void WallpaperApplication::loadBackgrounds () {
 
 WallpaperApplication::ProjectSource WallpaperApplication::openProjectSource (const std::string& path) const {
     auto container = this->setupAssetLocator (path);
-    auto json = WallpaperEngine::Data::JSON::JSON::parse (container->readString ("project.json"));
+    auto json = WallpaperEngine::Data::JSON::JSON::parseAsset (container->readString ("project.json"));
 
     if (json.contains ("file") || !json.contains ("dependency")) {
 	return { std::move (container), std::move (json), std::nullopt };
@@ -305,7 +305,7 @@ WallpaperApplication::ProjectSource WallpaperApplication::openProjectSource (con
     }
 
     auto baseContainer = this->setupAssetLocator (baseDir.string (), presetDir);
-    auto baseJson = WallpaperEngine::Data::JSON::JSON::parse (baseContainer->readString ("project.json"));
+    auto baseJson = WallpaperEngine::Data::JSON::JSON::parseAsset (baseContainer->readString ("project.json"));
 
     return { std::move (baseContainer), std::move (baseJson), json.optional ("preset") };
 }

+ 63 - 0
src/WallpaperEngine/Data/JSON.cpp

@@ -2,6 +2,8 @@
 
 #include "WallpaperEngine/Data/Parsers/UserSettingParser.h"
 
+#include <cctype>
+
 using namespace WallpaperEngine::Data::JSON;
 using namespace WallpaperEngine::Data::Model;
 using namespace WallpaperEngine::Data::Parsers;
@@ -17,3 +19,64 @@ UserSettingUniquePtr JsonExtensions::color (const std::string& key, const Proper
 
     return UserSettingParser::parse (value, properties, true);
 }
+
+JSON JsonExtensions::parseAsset (const std::string& content) {
+    std::string cleaned;
+    cleaned.reserve (content.size ());
+
+    const auto length = content.size ();
+    size_t i = 0;
+
+    while (i < length) {
+	const char c = content[i];
+
+	if (c == '"') {
+	    const auto start = i++;
+
+	    while (i < length && content[i] != '"') {
+		i += content[i] == '\\' ? 2 : 1;
+	    }
+
+	    i = std::min (i + 1, length);
+	    cleaned.append (content, start, i - start);
+	    continue;
+	}
+
+	if (c == '/' && i + 1 < length && (content[i + 1] == '/' || content[i + 1] == '*')) {
+	    const auto start = i;
+	    const bool block = content[i + 1] == '*';
+
+	    i += 2;
+
+	    if (block) {
+		const auto end = content.find ("*/", i);
+		i = end == std::string::npos ? length : end + 2;
+	    } else {
+		while (i < length && content[i] != '\n') {
+		    i++;
+		}
+	    }
+
+	    cleaned.append (content, start, i - start);
+	    continue;
+	}
+
+	if (c == ',') {
+	    auto next = i + 1;
+
+	    while (next < length && std::isspace (static_cast<unsigned char> (content[next]))) {
+		next++;
+	    }
+
+	    if (next < length && (content[next] == ']' || content[next] == '}')) {
+		i++;
+		continue;
+	    }
+	}
+
+	cleaned.push_back (c);
+	i++;
+    }
+
+    return JSON::parse (cleaned, nullptr, true, true);
+}

+ 6 - 0
src/WallpaperEngine/Data/JSON.h

@@ -168,6 +168,12 @@ public:
 	return operator glm::vec<length, typename GlmVecTraits<T>::type, qualifier> ();
     }
 
+    /**
+     * Parses a wallpaper/asset file the way WE's own loader accepts it: trailing commas
+     * before ] or } and comments are allowed (workshop effect.json files ship with them)
+     */
+    [[nodiscard]] static base_type parseAsset (const std::string& content);
+
 private:
     [[nodiscard]] const base_type& base () const { return *static_cast<const base_type*> (this); }
 };

+ 1 - 1
src/WallpaperEngine/Data/Parsers/EffectParser.cpp

@@ -11,7 +11,7 @@ using namespace WallpaperEngine::Data::Parsers;
 using namespace WallpaperEngine::Data::Model;
 
 EffectUniquePtr EffectParser::load (const Project& project, const std::string& filename) {
-    const auto effectJson = JSON::parse (project.assetLocator->readString (filename));
+    const auto effectJson = JSON::parseAsset (project.assetLocator->readString (filename));
 
     return parse (effectJson, project);
 }

+ 1 - 1
src/WallpaperEngine/Data/Parsers/MaterialParser.cpp

@@ -10,7 +10,7 @@ using namespace WallpaperEngine::Data::Parsers;
 using namespace WallpaperEngine::Data::Model;
 
 MaterialUniquePtr MaterialParser::load (const Project& project, const std::string& filename) {
-    const auto materialJson = JSON::parse (project.assetLocator->readString (filename));
+    const auto materialJson = JSON::parseAsset (project.assetLocator->readString (filename));
 
     return parse (materialJson, filename, project);
 }

+ 1 - 1
src/WallpaperEngine/Data/Parsers/ModelParser.cpp

@@ -11,7 +11,7 @@ using namespace WallpaperEngine::Data::Parsers;
 using namespace WallpaperEngine::Data::Model;
 
 ModelUniquePtr ModelParser::load (const Project& project, const std::string& filename) {
-    const auto model = JSON::parse (project.assetLocator->readString (filename));
+    const auto model = JSON::parseAsset (project.assetLocator->readString (filename));
 
     return parse (model, project, filename);
 }

+ 1 - 1
src/WallpaperEngine/Data/Parsers/ObjectParser.cpp

@@ -384,7 +384,7 @@ ParticleUniquePtr ObjectParser::parseParticle (const JSON& it, const Project& pr
 	if (!particleFile.empty ()) {
 	    try {
 		particleJson
-		    = WallpaperEngine::Data::JSON::JSON::parse (project.assetLocator->readString (particleFile));
+		    = WallpaperEngine::Data::JSON::JSON::parseAsset (project.assetLocator->readString (particleFile));
 	    } catch (std::runtime_error& e) {
 		sLog.error ("Cannot load particle file: ", particleFile, " - ", e.what ());
 	    }

+ 1 - 1
src/WallpaperEngine/Data/Parsers/WallpaperParser.cpp

@@ -22,7 +22,7 @@ WallpaperUniquePtr WallpaperParser::parse (const JSON& file, Project& project) {
 }
 
 SceneUniquePtr WallpaperParser::parseScene (const JSON& file, Project& project) {
-    const auto scene = JSON::parse (project.assetLocator->readString (file));
+    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");
     const auto projection

+ 0 - 10
src/WallpaperEngine/Render/Objects/CImage.cpp

@@ -2451,16 +2451,6 @@ CImage::ResolvedTransform CImage::updateGeometryBuffers () {
     // position from before whatever moved it (parallax, a script, an attachment point it follows, ...)
     this->updateScenePosition (origin, size, scale, sceneWidth, sceneHeight);
 
-    if (!this->m_transformDiagnosticLogged) {
-	this->m_transformDiagnosticLogged = true;
-	sLog.out (
-	    "Transform for ", this->getImage ().name, " (", this->getId (), "): resolvedOrigin=(", origin.x, ",",
-	    origin.y, ") resolvedScale=", scale.x, " resolvedAngleDeg=", glm::degrees (transform.angle), " size=(",
-	    size.x, ",", size.y, ") m_pos=(", this->m_pos.x, ",", this->m_pos.y, ",", this->m_pos.z, ",",
-	    this->m_pos.w, ") sceneWidth=", sceneWidth, " sceneHeight=", sceneHeight
-	);
-    }
-
     if (this->m_pos != this->m_lastUploadedPos || size != this->m_lastUploadedGeometrySize) {
 	this->uploadGeometryBuffers (size);
 	// puppet vertices bake m_pos/scale in directly (see updatePuppetPositionBuffer), so they need

+ 0 - 1
src/WallpaperEngine/Render/Objects/CImage.h

@@ -182,7 +182,6 @@ private:
     mutable bool m_puppetDrawDiagnosticLogged = false;
     mutable bool m_puppetDrawErrorChecked = false;
     bool m_puppetPositionDiagnosticLogged = false;
-    bool m_transformDiagnosticLogged = false;
     mutable std::set<int> m_attachmentDiagnosticLogged = {};
     mutable std::set<int> m_finalOriginLogged = {};
     std::vector<GLfloat> m_puppetRawPositions = {};

+ 0 - 12
src/WallpaperEngine/Render/Objects/CText.cpp

@@ -1038,18 +1038,6 @@ void CText::render () {
     m_modelViewProjectionScreen = getScene ().getCamera ().getProjection () * getScene ().getCamera ().getLookAt () * model;
     m_modelViewProjectionScreenInverse = glm::inverse (m_modelViewProjectionScreen);
 
-    if (!m_debugLogged) {
-	m_debugLogged = true;
-	sLog.out (
-	    "[text-debug] '", m_text.name, "' pointSize=", m_text.pointSize->value->getFloat (), " scale=", scale.x, ",",
-	    scale.y, " pixelSize=", pixelSize, " quadSize=", m_quadSize.x, ",", m_quadSize.y,
-	    " scaledHalf=", scaledHalfWidth, ",", scaledHalfHeight, " size=", m_text.size.x, ",", m_text.size.y,
-	    " padding=", m_text.padding.x, ",", m_text.padding.y, " align=", m_text.alignment, "/", m_text.verticalalign, " origin=", origin.x,
-	    ",", origin.y, " offset=", offsetX, ",", offsetY, " scene=", scene_w, ",", scene_h, " gl_origin=",
-	    gl_origin.x, ",", gl_origin.y, " effects=", m_text.effects.size (), " passes=", m_passes.size ()
-	);
-    }
-
     glColorMask (true, true, true, true);
     glDisable (GL_DEPTH_TEST);
 

+ 0 - 1
src/WallpaperEngine/Render/Objects/CText.h

@@ -115,6 +115,5 @@ private:
 
     bool m_valid = false;
     bool m_initialized = false;
-    bool m_debugLogged = false;
 };
 } // namespace WallpaperEngine::Render::Objects

+ 3 - 3
src/WallpaperEngine/Render/Shaders/ShaderUnit.cpp

@@ -340,7 +340,7 @@ void ShaderUnit::preprocessRequires () {
 	    continue;
 	}
 
-	sLog.out ("Resolving require module: ", moduleName, " in shader ", this->m_file);
+	sLog.debug ("Resolving require module: ", moduleName, " in shader ", this->m_file);
 
 	std::string moduleCode = this->resolveRequireModule (moduleName);
 
@@ -751,7 +751,7 @@ void ShaderUnit::parseComboConfiguration (const std::string& content, const int
     // TODO: SUPPORT REQUIRES SO WE PROPERLY FOLLOW THE REQUIRED CHAIN
     JSON data;
     try {
-	data = JSON::parse (content);
+	data = JSON::parseAsset (content);
     } catch (const std::exception& e) {
 	sLog.error ("Cannot parse combo metadata in shader ", this->m_file, ": ", e.what ());
 	return;
@@ -787,7 +787,7 @@ void ShaderUnit::parseParameterConfiguration (
 ) {
     JSON data;
     try {
-	data = JSON::parse (content);
+	data = JSON::parseAsset (content);
     } catch (const std::exception& e) {
 	sLog.error ("Cannot parse parameter metadata for ", name, " in shader ", this->m_file, ": ", e.what ());
 	return;

+ 6 - 1
src/WallpaperEngine/Scripting/ConsoleObject.cpp

@@ -1,5 +1,7 @@
 #include "ConsoleObject.h"
 
+#include <cstdlib>
+
 #include "EngineObject.h"
 #include "ScriptEngine.h"
 #include "WallpaperEngine/Data/Utils/ScopeGuard.h"
@@ -7,8 +9,11 @@
 
 using namespace WallpaperEngine::Scripting;
 
+// wallpaper scripts often console.log every frame, WE only shows that in its editor console
 JSValue console_log (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
-    if (argc < 1) {
+    static const bool enabled = std::getenv ("LWE_SCRIPT_LOG") != nullptr;
+
+    if (!enabled || argc < 1) {
 	return JS_UNDEFINED;
     }