1
0
mirror of https://github.com/AvengeMedia/DankMaterialShell.git synced 2026-04-14 17:52:10 -04:00

slight efficiency improvement

This commit is contained in:
bbedward
2025-08-16 17:55:57 -04:00
parent c560d2d964
commit d8082f70b3

View File

@@ -151,6 +151,12 @@ Singleton {
return return
} }
const extractedJson = extractJsonFromText(matugenCollector.text) const extractedJson = extractJsonFromText(matugenCollector.text)
if (!extractedJson) {
ToastService.wallpaperErrorStatus = "error"
ToastService.showError("Wallpaper Processing Failed: Invalid JSON extracted from matugen output.")
console.log("Raw matugen output:", matugenCollector.text)
return
}
try { try {
root.matugenColors = JSON.parse(extractedJson) root.matugenColors = JSON.parse(extractedJson)
root.colorsUpdated() root.colorsUpdated()
@@ -320,49 +326,56 @@ Singleton {
systemThemeRestoreProcess.running = true systemThemeRestoreProcess.running = true
} }
// Returns the first complete JSON substring (object or array) or null.
function extractJsonFromText(text) { function extractJsonFromText(text) {
if (!text) return null if (!text) return null;
// Try to find JSON object boundaries const start = text.search(/[{\[]/);
const firstBrace = text.indexOf('{') if (start === -1) return null;
if (firstBrace === -1) return null
const open = text[start];
// Find matching closing brace by counting depth const pairs = { '{': '}', '[': ']' };
let depth = 0 const close = pairs[open];
let inString = false if (!close) return null;
let escapeNext = false
let inString = false;
for (let i = firstBrace; i < text.length; i++) { let escape = false;
const ch = text[i] const stack = [open];
if (escapeNext) { for (let i = start + 1; i < text.length; i++) {
escapeNext = false const ch = text[i];
continue
if (inString) {
if (escape) {
escape = false;
} else if (ch === '\\') {
escape = true;
} else if (ch === '"') {
inString = false;
}
continue;
} }
if (ch === '\\') {
escapeNext = true
continue
}
if (ch === '"') { if (ch === '"') {
inString = !inString inString = true;
continue continue;
} }
if (ch === '{' || ch === '[') {
if (!inString) { stack.push(ch);
if (ch === '{') depth++ continue;
else if (ch === '}') { }
depth-- if (ch === '}' || ch === ']') {
if (depth === 0) { const last = stack.pop();
// Found matching closing brace if (!last || pairs[last] !== ch) {
return text.substring(firstBrace, i + 1) return null;
} }
if (stack.length === 0) {
return text.slice(start, i + 1);
} }
} }
} }
return null return null;
} }
Process { Process {