diff options
Diffstat (limited to 'player/lua/auto_profiles.lua')
-rw-r--r-- | player/lua/auto_profiles.lua | 67 |
1 files changed, 45 insertions, 22 deletions
diff --git a/player/lua/auto_profiles.lua b/player/lua/auto_profiles.lua index 6856138d97..a0f580298b 100644 --- a/player/lua/auto_profiles.lua +++ b/player/lua/auto_profiles.lua @@ -14,6 +14,12 @@ local pending_hooks = {} -- as set (keys only, meaningless values) -- profile the condition is evaluated for. local current_profile = nil +-- Cached set of all top-level mpv properities. Only used for extra validation. +local property_set = {} +for _, property in pairs(mp.get_property_native("property-list")) do + property_set[property] = true +end + local function evaluate(profile) msg.verbose("Re-evaluating auto profile " .. profile.name) @@ -25,11 +31,8 @@ local function evaluate(profile) -- errors can be "normal", e.g. in case properties are unavailable msg.verbose("Profile condition error on evaluating: " .. res) res = false - elseif type(res) ~= "boolean" then - msg.verbose("Profile condition did not return a boolean, but " - .. type(res) .. ".") - res = false end + res = not not res if res ~= profile.status then if res == true then msg.info("Applying auto profile: " .. profile.name) @@ -87,8 +90,16 @@ function get(name, default) -- Normally, we use the cached value only if not watched_properties[name] then watched_properties[name] = true + local res, err = mp.get_property_native(name) + -- Property has to not exist and the toplevel of property in the name must also + -- not have an existing match in the property set for this to be considered an error. + -- This allows things like user-data/test to still work. + if err == "property not found" and property_set[name:match("^([^/]+)")] == nil then + msg.error("Property '" .. name .. "' was not found.") + return default + end + cached_properties[name] = res mp.observe_property(name, "native", on_property_change) - cached_properties[name] = mp.get_property_native(name) end -- The first time the property is read we need add it to the -- properties_to_profiles table, which will be used to mark the profile @@ -136,21 +147,25 @@ setmetatable(p, { }) local function compile_cond(name, s) - -- (pre 5.2 ignores the extra arguments) - local chunk, err = load("return " .. s, "profile " .. name .. " condition", - "t", evil_magic) + local code, chunkname = "return " .. s, "profile " .. name .. " condition" + local chunk, err + if setfenv then -- lua 5.1 + chunk, err = loadstring(code, chunkname) + if chunk then + setfenv(chunk, evil_magic) + end + else -- lua 5.2 + chunk, err = load(code, chunkname, "t", evil_magic) + end if not chunk then msg.error("Profile '" .. name .. "' condition: " .. err) chunk = function() return false end end - if setfenv then - setfenv(chunk, evil_magic) - end return chunk end -local function load_profiles() - for i, v in ipairs(mp.get_property_native("profile-list")) do +local function load_profiles(profiles_property) + for _, v in ipairs(profiles_property) do local cond = v["profile-cond"] if cond and #cond > 0 then local profile = { @@ -159,7 +174,7 @@ local function load_profiles() properties = {}, status = nil, dirty = true, -- need re-evaluate - has_restore_opt = v["profile-restore"] ~= "default" + has_restore_opt = v["profile-restore"] and v["profile-restore"] ~= "default" } profiles[#profiles + 1] = profile have_dirty_profiles = true @@ -167,17 +182,25 @@ local function load_profiles() end end -load_profiles() +mp.observe_property("profile-list", "native", function (_, profiles_property) + profiles = {} + watched_properties = {} + cached_properties = {} + properties_to_profiles = {} + mp.unobserve_property(on_property_change) -if #profiles < 1 and mp.get_property("load-auto-profiles") == "auto" then - -- make it exit immediately - _G.mp_event_loop = function() end - return -end + load_profiles(profiles_property) + + if #profiles < 1 and mp.get_property("load-auto-profiles") == "auto" then + -- make it exit immediately + _G.mp_event_loop = function() end + return + end + + on_idle() -- re-evaluate all profiles immediately +end) mp.register_idle(on_idle) for _, name in ipairs({"on_load", "on_preloaded", "on_before_start_file"}) do mp.add_hook(name, 50, on_hook) end - -on_idle() -- re-evaluate all profiles immediately |