import "pkg:/source/utils/AssertUtils.bs" import "pkg:/source/utils/RegistryUtils.bs" import "pkg:/source/utils/StringUtils.bs" import "pkg:/source/utils/Types.bs" function Init() m.typeMapping = { "boolean": "boolean" "string": "string" "number": "integer" "radio": "string" "array": "array" "assocarray": "assocarray" } m.disableSavingToRegistry = false LoadPreferencesFile() AddNodeFields() LoadUserPreferencesFromRegistry() AddNodeFieldsObservers() end function function LoadPreferencesFile() m.top.prefsModel = StringUtils.ParseJsonSafe(ReadAsciiFile("libpkg:/config/preferences.json5")) end function function AddNodeFields() fields = {} prefsModel = m.top.prefsModel for each child in prefsModel AddNodeFieldsFromPreference(child, fields) end for ' We keep reference to fields specific to preferences, to avoid default fields like focusedChild m.top.preferenceFields = fields end function function AddNodeFieldsFromPreference(pref as object, fields as object) if pref.key <> invalid and pref.type <> invalid fieldType = m.typeMapping[pref.type] m.top.addField(pref.key, fieldType, false) fields[pref.key] = true end if if pref.children <> invalid for each child in pref.children AddNodeFieldsFromPreference(child, fields) end for end if end function function AddNodeFieldsObservers() prefsFields = m.top.preferenceFields for each key in prefsFields m.top.observeField(key, FuncName(SaveUserPreferencesToRegistry)) end for end function function LoadUserPreferencesFromRegistry() ' Step 1: Load user preferences from registry userPrefs = RegistryUtils.Read(RegistryUtils.USER_PREFERENCES) if userPrefs = invalid userPrefs = {} else userPrefs = StringUtils.ParseJsonSafe(userPrefs) end if ' Step 2: Migrate existing preferences to new format (if needed) isDirty = MigrateExistingPreferences(userPrefs) ' Step 3: Apply defaults to user preferences (if needed) isDirty = ApplyDefaultsToUserPreferences(userPrefs) or isDirty ' Step 4: set field values in node for each key in userPrefs m.top[key] = userPrefs[key] end for ' Step 5: Save user preferences (if needed) if isDirty SaveUserPreferencesToRegistry() end if end function function SaveUserPreferencesToRegistry() as void if m.disableSavingToRegistry return end if userPrefs = {} prefsFields = m.top.preferenceFields for each key in prefsFields userPrefs[key] = m.top[key] end for userPrefs.__version = m.top.__version RegistryUtils.Write(RegistryUtils.USER_PREFERENCES, FormatJson(userPrefs)) end function function MigrateExistingPreferences(userPrefs as object) as boolean isDirty = false ' v0 -> v1 if ValidInt(userPrefs.__version) = 0 instances = RegistryUtils.Read(RegistryUtils.INVIDIOUS_INSTANCES) if not StringUtils.IsNullOrEmpty(instances) currentInstance = userPrefs["invidious.instance"] if StringUtils.IsNullOrEmpty(currentInstance) instances = StringUtils.ParseJsonSafe(instances) if IsArray(instances) and instances.Count() > 0 instance = instances[0] if not StringUtils.IsNullOrEmpty(instance) userPrefs["invidious.instance"] = instance end if end if end if RegistryUtils.Delete(RegistryUtils.INVIDIOUS_INSTANCES) end if userPrefs.__version = 1 isDirty = true end if if userPrefs.__version = 1 if ValidString(userPrefs["playback.preferred_quality"]) = "medium" userPrefs["playback.preferred_quality"] = "480p,360p" end if userPrefs.__version = 2 isDirty = true end if if userPrefs.__version = 2 if IsArray(userPrefs["misc.home_screen_layout"]) homeScreenLayout = userPrefs["misc.home_screen_layout"] hasRecommended = false for each item in homeScreenLayout if item.id = "recommended" hasRecommended = true exit for end if end for if not hasRecommended homeScreenLayout.Unshift({ id: "recommended" enabled: true }) end if end if userPrefs.__version = 3 isDirty = true end if if userPrefs.__version = 3 if IsString(userPrefs["playback.backend"]) backend = userPrefs["playback.backend"] userPrefs.Delete("playback.backend") userPrefs["backend.selected"] = backend end if userPrefs.__version = 4 isDirty = true end if if userPrefs.__version = 4 if IsString(userPrefs["dev.dev_video_player"]) userPrefs.Delete("dev.dev_video_player") end if userPrefs.__version = 5 isDirty = true end if if userPrefs.__version = 5 filteredLayout = [] homeScreenLayout = ValidArray(userPrefs["misc.home_screen_layout"]) for each item in homeScreenLayout if item.id = "trending" or item.id = "trending_music" or item.id = "trending_gaming" or item.id = "trending_movies" continue for end if filteredLayout.Push(item) end for userPrefs["misc.home_screen_layout"] = filteredLayout userPrefs.__version = 6 isDirty = true end if ' TODO:P2 handle case where saved preferences version is higher than current version (e.g. user downgraded) #if DEBUG Assert.AreEqual(m.top.__version, userPrefs.__version, "Preferences version mismatch.") #end if return isDirty end function function ApplyDefaultsToUserPreferences(userPrefs as object) as boolean isDirty = false prefsModel = m.top.prefsModel for each child in prefsModel isDirty = ApplyToUserPreferences(userPrefs, child) or isDirty end for return isDirty end function function ApplyToUserPreferences(userPrefs as object, model as object) as boolean isDirty = false if model.children <> invalid for each child in model.children isDirty = ApplyToUserPreferences(userPrefs, child) or isDirty end for end if if model.defaultValue <> invalid and (not userPrefs.DoesExist(model.key) or userPrefs[model.key] = invalid) userPrefs[model.key] = model.defaultValue isDirty = true end if return isDirty end function function GetUserPreferences(_unused as dynamic) as object userPrefs = {} prefsFields = m.top.preferenceFields for each key in prefsFields userPrefs[key] = m.top[key] end for userPrefs.__version = m.top.__version return userPrefs end function function ApplyUserPreferences(preferences as object) as void if preferences = invalid return end if ' TODO:P2 user input validation m.disableSavingToRegistry = true m.top.setFields(preferences) m.disableSavingToRegistry = false SaveUserPreferencesToRegistry() end function function ClearUserPreferences(_unused as dynamic) as void userPrefs = {} ApplyDefaultsToUserPreferences(userPrefs) ApplyUserPreferences(userPrefs) end function