-
Notifications
You must be signed in to change notification settings - Fork 3
/
remember-props.lua
84 lines (58 loc) · 1.72 KB
/
remember-props.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
74
75
76
77
78
79
80
81
82
83
84
--[[
When a property changes, it saves it to restore on next start.
Saved values are not file-specific.
List of properties to save is configured in `script-opts/remember-props.conf` file:
```
props=one,two,three
```
]]--
local mp = require("mp")
local options = require("mp.options")
local utils = require("mp.utils")
local data_file_path = (mp.command_native({'expand-path', '~~/saved-props.json'}))
local function split(inputstr, sep)
local result = {}
for str in string.gmatch(inputstr, "([^" .. sep .. "]+)") do
table.insert(result, str)
end
return result
end
local script_options = {
props = ""
}
options.read_options(script_options, "remember-props")
script_options.props = split(script_options.props, ",")
local function read_data_file()
local json_file = io.open(data_file_path, 'a+')
local result = utils.parse_json(json_file:read("*all"))
if result == nil then
result = {}
end
json_file:close()
return result
end
local saved_data = read_data_file()
local function save_data_file()
local file = io.open(data_file_path, 'w+')
if file == nil then
return
end
local content, ret = utils.format_json(saved_data)
if ret ~= error and content ~= nil then
file:write(content)
end
file:close()
end
local function init()
for _, prop_name in ipairs(script_options.props) do
local saved_value = saved_data[prop_name]
if saved_value ~= nil then
mp.set_property_native(prop_name, saved_value)
end
mp.observe_property(prop_name, "native", function(_, prop_value)
saved_data[prop_name] = mp.get_property_native(prop_name)
save_data_file()
end)
end
end
init()