-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiloop.lua
308 lines (283 loc) · 7.67 KB
/
multiloop.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
local mp = require("mp")
local assdraw = require("mp.assdraw")
local mpopts = require("mp.options")
local options = {
keybind = "M",
message_duration = 1
}
local positions = {}
local ab = {}
looping = false
-- Start MIT licensed code
local write, writeIndent, writers, refCount;
persistence =
{
store = function (path, ...)
local file, e = io.open(path, "w");
if not file then
return error(e);
end
local n = select("#", ...);
-- Count references
local objRefCount = {}; -- Stores reference that will be exported
for i = 1, n do
refCount(objRefCount, (select(i,...)));
end;
-- Export Objects with more than one ref and assign name
-- First, create empty tables for each
local objRefNames = {};
local objRefIdx = 0;
file:write("-- Persistent Data\n");
file:write("local multiRefObjects = {\n");
for obj, count in pairs(objRefCount) do
if count > 1 then
objRefIdx = objRefIdx + 1;
objRefNames[obj] = objRefIdx;
file:write("{};"); -- table objRefIdx
end;
end;
file:write("\n} -- multiRefObjects\n");
-- Then fill them (this requires all empty multiRefObjects to exist)
for obj, idx in pairs(objRefNames) do
for k, v in pairs(obj) do
file:write("multiRefObjects["..idx.."][");
write(file, k, 0, objRefNames);
file:write("] = ");
write(file, v, 0, objRefNames);
file:write(";\n");
end;
end;
-- Create the remaining objects
for i = 1, n do
file:write("local ".."obj"..i.." = ");
write(file, (select(i,...)), 0, objRefNames);
file:write("\n");
end
-- Return them
if n > 0 then
file:write("return obj1");
for i = 2, n do
file:write(" ,obj"..i);
end;
file:write("\n");
else
file:write("return\n");
end;
if type(path) == "string" then
file:close();
end;
end;
load = function (path)
local f, e;
if type(path) == "string" then
f, e = loadfile(path);
else
f, e = path:read('*a')
end
if f then
return f();
else
return nil, e;
end;
end;
}
-- Private methods
-- write thing (dispatcher)
write = function (file, item, level, objRefNames)
writers[type(item)](file, item, level, objRefNames);
end;
-- write indent
writeIndent = function (file, level)
for i = 1, level do
file:write("\t");
end;
end;
-- recursively count references
refCount = function (objRefCount, item)
-- only count reference types (tables)
if type(item) == "table" then
-- Increase ref count
if objRefCount[item] then
objRefCount[item] = objRefCount[item] + 1;
else
objRefCount[item] = 1;
-- If first encounter, traverse
for k, v in pairs(item) do
refCount(objRefCount, k);
refCount(objRefCount, v);
end;
end;
end;
end;
-- Format items for the purpose of restoring
writers = {
["nil"] = function (file, item)
file:write("nil");
end;
["number"] = function (file, item)
file:write(tostring(item));
end;
["string"] = function (file, item)
file:write(string.format("%q", item));
end;
["boolean"] = function (file, item)
if item then
file:write("true");
else
file:write("false");
end
end;
["table"] = function (file, item, level, objRefNames)
local refIdx = objRefNames[item];
if refIdx then
-- Table with multiple references
file:write("multiRefObjects["..refIdx.."]");
else
-- Single use table
file:write("{\n");
for k, v in pairs(item) do
writeIndent(file, level+1);
file:write("[");
write(file, k, level+1, objRefNames);
file:write("] = ");
write(file, v, level+1, objRefNames);
file:write(";\n");
end
writeIndent(file, level);
file:write("}");
end;
end;
["function"] = function (file, item)
-- Does only work for "normal" functions, not those
-- with upvalues or c functions
local dInfo = debug.getinfo(item, "uS");
if dInfo.nups > 0 then
file:write("nil --[[functions with upvalue not supported]]");
elseif dInfo.what ~= "Lua" then
file:write("nil --[[non-lua function not supported]]");
else
local r, s = pcall(string.dump,item);
if r then
file:write(string.format("loadstring(%q)", s));
else
file:write("nil --[[function could not be dumped]]");
end
end
end;
["thread"] = function (file, item)
file:write("nil --[[thread]]\n");
end;
["userdata"] = function (file, item)
file:write("nil --[[userdata]]\n");
end;
}
-- End MIT licensed code
function file_exists(name)
local f=io.open(name,"r")
if f~=nil then io.close(f) return true else return false end
end
local message
message = function(text, duration)
local ass = mp.get_property_osd("osd-ass-cc/0")
ass = ass .. text
return mp.osd_message(ass, duration or options.message_duration)
end
function drawMenu()
local window_w, window_h = mp.get_osd_size()
local ass = assdraw.ass_new()
ass:new_event()
ass:append(" \\N\\N")
ass:append("{\\b1}Multiloop{\\b0}\\N")
ass:append("{\\b1}1{\\b0} set start time\\N")
ass:append("{\\b1}2{\\b0} set end time\\N")
ass:append("{\\b1}d{\\b0} duplicate last a-b point\\N")
ass:append("{\\b1}s{\\b0} save positions to file\\N")
ass:append("{\\b1}l{\\b0} loop / end loop\\N")
ass:append("{\\b1}ESC{\\b0} hide\\N")
mp.set_osd_ass(window_w, window_h, ass.text)
end
function clearMenu()
local window_w, window_h = mp.get_osd_size()
mp.set_osd_ass(window_w, window_h, "")
mp.osd_message("", 0)
end
function setStartTime()
local start_time = mp.get_property_number("time-pos")
clearMenu()
if #ab == 0 then
table.insert(ab, start_time)
else
ab = {}
table.insert(ab, start_time)
end
message("start time set")
end
function setEndTime()
if #ab == 1 then
local end_time = mp.get_property_number("time-pos")
table.insert(ab, end_time)
table.insert(positions, ab)
ab = {}
message("end time set")
else
message("you have to set a start time")
end
drawMenu()
end
function duplicateLastPoint()
table.insert(positions, positions[#positions])
message("point added!")
end
function savePositions()
if #ab == 1 then
message("ab point without an end time")
elseif #positions == 0 then
message("nothing to save")
else
local fn = mp.get_property("filename/no-ext")
persistence.store(fn .. ".mab", positions)
message("saved to file")
end
end
p_count = 1
function on_timepos_change()
if mp.get_property_number("time-pos") >= positions[p_count][2] then
if p_count ~= #positions then
p_count = p_count + 1
else
p_count = 1
end
mp.set_property_native("time-pos", positions[p_count][1])
end
end
function loop()
if looping == false then
looping = true
clearMenu()
if #positions ~= 0 then
mp.set_property_native("time-pos", positions[p_count][1])
mp.observe_property("time-pos", "string", on_timepos_change)
else
message("you have to set some points first!")
drawMenu()
end
else
looping = false
mp.unobserve_property(on_timepos_change)
drawMenu()
end
end
function main()
mp.add_forced_key_binding("1", "1", setStartTime)
mp.add_forced_key_binding("2", "2", setEndTime)
mp.add_forced_key_binding("d", "d", duplicateLastPoint)
mp.add_forced_key_binding("s", "s", savePositions)
mp.add_forced_key_binding("l", "l", loop)
mp.add_forced_key_binding("ESC", "ESC", clearMenu)
local fn = mp.get_property("path"):match("(.+)%..+$") .. ".mab"
if file_exists(fn) == true then
positions = persistence.load(fn)
end
drawMenu()
end
mp.add_key_binding(options.keybind, "display-multiloop", main)