From f1fc84243dc1df49c3deae5449ebbc3962b5f0a4 Mon Sep 17 00:00:00 2001 From: ririxi Date: Mon, 3 Feb 2025 20:09:20 +0100 Subject: [PATCH] fix: use new PlatformAPI in exp features --- jsHelper/expFeatures.js | 40 +++++++++++++++++++++++++++++----------- src/apply/apply.go | 8 ++++++++ 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/jsHelper/expFeatures.js b/jsHelper/expFeatures.js index a9475518d4..f8eff46ba3 100644 --- a/jsHelper/expFeatures.js +++ b/jsHelper/expFeatures.js @@ -132,16 +132,33 @@ (function waitForRemoteConfigResolver() { // Don't show options if hooks aren't patched/loaded - if (!hooksPatched || (!Spicetify.RemoteConfigResolver && !Spicetify.Platform?.RemoteConfiguration)) { + if (!hooksPatched || (!Spicetify.RemoteConfigResolver && !Spicetify.Platform?.RemoteConfigDebugAPI && !Spicetify.Platform?.RemoteConfiguration)) { setTimeout(waitForRemoteConfigResolver, 500); return; } let remoteConfiguration = Spicetify.RemoteConfigResolver?.value.remoteConfiguration || Spicetify.Platform?.RemoteConfiguration; - let setOverrides = () => {}; + const setOverrides = async (overrides) => { + if (Spicetify.Platform?.RemoteConfigDebugAPI) { + for (const [name, value] of Object.entries(overrides)) { + const feature = overrideList[name]; + const type = feature.values ? "enum" : typeof value === "number" ? "number" : "boolean"; + await Spicetify.Platform.RemoteConfigDebugAPI.setOverride( + { + source: "web", + type, + name, + }, + value + ); + } + } else if (Spicetify.RemoteConfigResolver?.value?.setOverrides) { + Spicetify.RemoteConfigResolver.value.setOverrides(Spicetify.createInternalMap?.(overrides)); + } + }; - (function waitForResolver() { - if (!Spicetify.RemoteConfigResolver) { + (async function waitForResolver() { + if (!Spicetify.RemoteConfigResolver && !Spicetify.Platform?.RemoteConfigDebugAPI) { isFallback = true; notice.innerText = "⚠️ Using fallback mode. Some features may not work."; setTimeout(waitForResolver, 500); @@ -149,8 +166,7 @@ } isFallback = false; notice.remove(); - remoteConfiguration = Spicetify.RemoteConfigResolver.value.remoteConfiguration; - setOverrides = Spicetify.RemoteConfigResolver.value.setOverrides; + remoteConfiguration = Spicetify?.RemoteConfigResolver?.remoteConfiguration ?? (await Spicetify.Platform?.RemoteConfigDebugAPI.getProperties()); })(); for (const key of Object.keys(overrideList)) { @@ -165,7 +181,7 @@ localStorage.setItem("spicetify-exp-features", JSON.stringify(overrideList)); featureMap[name] = value; - setOverrides(Spicetify.createInternalMap?.(featureMap)); + setOverrides({ [name]: value }); } function createSlider(name, desc, defaultVal) { @@ -254,8 +270,12 @@ ${Spicetify.SVGIcons.search} for (const name of Object.keys(overrideList)) { if (!prevSessionOverrideList.includes(name) && remoteConfiguration.values.has(name)) { - changeValue(name, remoteConfiguration.values.get(name)); - console.log(name, remoteConfiguration.values.get(name), overrideList[name]); + const currentValue = remoteConfiguration.values.get(name); + overrideList[name].value = currentValue; + localStorage.setItem("spicetify-exp-features", JSON.stringify(overrideList)); + + featureMap[name] = currentValue; + setOverrides({ [name]: currentValue }); } const feature = overrideList[name]; @@ -269,8 +289,6 @@ ${Spicetify.SVGIcons.search} } content.appendChild(resetButton); - - setOverrides(Spicetify.createInternalMap?.(featureMap)); })(); await new Promise((res) => Spicetify.Events.webpackLoaded.on(res)); diff --git a/src/apply/apply.go b/src/apply/apply.go index e6d9d4fc50..945f67c6bc 100644 --- a/src/apply/apply.go +++ b/src/apply/apply.go @@ -413,12 +413,20 @@ func insertExpFeatures(jsPath string, flags Flag) { return fmt.Sprintf("%s%s=Spicetify.expFeatureOverride(%s);%s", submatches[1], submatches[2], submatches[2], submatches[3]) }) + // utils.ReplaceOnce( + // &content, + // `(\w+\.fromJSON)(\s*=\s*function\b[^{]*{[^}]*})`, + // func(submatches ...string) string { + // return fmt.Sprintf("%s=Spicetify.createInternalMap%s", submatches[1], submatches[2]) + // }) + utils.ReplaceOnce( &content, `(([\w$.]+\.fromJSON)\(\w+\)+;)(return ?[\w{}().,]+[\w$]+\.Provider,)(\{value:\{localConfiguration)`, func(submatches ...string) string { return fmt.Sprintf("%sSpicetify.createInternalMap=%s;%sSpicetify.RemoteConfigResolver=%s", submatches[1], submatches[2], submatches[3], submatches[4]) }) + return content }) }