-
Notifications
You must be signed in to change notification settings - Fork 5
/
auto-save-state.lua
70 lines (57 loc) · 1.96 KB
/
auto-save-state.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
-- Runs write-watch-later-config periodically
local options = require 'mp.options'
local msg = require 'mp.msg'
o = {
save_interval = 60,
percent_pos = 99,
}
options.read_options(o)
local can_delete = true
local can_save = true
local path = nil -- only set after file success load, reset to nil when file unload.
local function reset()
path = nil
end
-- set vars when file success load
local function init()
path = mp.get_property("path")
end
local function save()
if not can_save then return end
local watch_later_list = mp.get_property("watch-later-options", {})
if mp.get_property_bool("save-position-on-quit") then
msg.debug("saving state")
if not watch_later_list:find("start") then
mp.commandv("change-list", "watch-later-options", "append", "start")
end
mp.command("write-watch-later-config")
end
end
local function save_if_pause(_, pause)
if pause then save() end
end
local function pause_timer_while_paused(_, pause)
if pause then timer:stop() else timer:resume() end
end
-- save watch-later-config when file unloading
local function save_or_delete()
if not can_delete then return end
local eof = mp.get_property_bool("eof-reached")
local percent_pos = mp.get_property_number("percent-pos")
if eof or percent_pos and (percent_pos == 0 or percent_pos >= o.percent_pos) then
can_delete = true
if path ~= nil then
msg.debug("deleting state: percent_pos=0 or eof")
mp.commandv("delete-watch-later-config", path)
end
elseif path ~= nil then
save()
end
reset()
end
mp.register_script_message("skip-delete-state", function() can_delete = false end)
timer = mp.add_periodic_timer(o.save_interval, save)
mp.observe_property("pause", "bool", pause_timer_while_paused)
mp.observe_property("pause", "bool", save_if_pause)
mp.register_event("file-loaded", init)
mp.add_hook("on_unload", 50, save_or_delete) -- after mpv saving state