-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathauto-save-state.lua
74 lines (59 loc) · 2.05 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
71
72
73
-- Runs write-watch-later-config periodically
local options = require 'mp.options'
local o = { save_interval = 60 }
options.read_options(o)
local function save()
if mp.get_property_bool("resume-playback") then
mp.command("write-watch-later-config")
end
end
local function save_on_file_loaded()
if mp.get_property_number("playlist-pos") == 0 then
return -- no point saving here
end
save()
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
-- This function runs on file-loaded, registers two callback functions, and
-- then they run delete-watch-later-config when appropriate.
local function delete_watch_later(event)
local path = mp.get_property("path")
-- Temporarily disables save-position-on-quit while eof-reached is true, so
-- state isn't saved at EOF when keep-open=yes
local function eof_reached(_, eof)
if not can_delete then
return
elseif eof then
print("Deleting state (eof-reached)")
mp.commandv("delete-watch-later-config", path)
mp.set_property("save-position-on-quit", "no")
else
mp.set_property("save-position-on-quit", "yes")
end
end
local function end_file(event)
mp.unregister_event(end_file)
mp.unobserve_property(eof_reached)
if not can_delete then
can_delete = true
elseif event["reason"] == "eof" or event["reason"] == "stop" then
print("Deleting state (end-file "..event["reason"]..")")
mp.commandv("delete-watch-later-config", path)
end
end
mp.observe_property("eof-reached", "bool", eof_reached)
mp.register_event("end-file", end_file)
end
mp.set_property("save-position-on-quit", "yes")
can_delete = true
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", delete_watch_later)
mp.register_event("file-loaded", save_on_file_loaded)