Skip to content

Commit e07e082

Browse files
author
arch
committed
bakup lua testcode
1 parent dbd22f8 commit e07e082

File tree

1 file changed

+369
-0
lines changed

1 file changed

+369
-0
lines changed
Lines changed: 369 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,369 @@
1+
json = require "json"
2+
3+
-- global var
4+
processHandleMTFG = nil
5+
processHandleConfigDir = nil
6+
processHandleLogFile = nil
7+
logfileExist = false
8+
updateCounter = 0
9+
scriptIdx = 0
10+
mtfgVersion = "0.0.0"
11+
status = "MTFG not running"
12+
13+
function exists(file)
14+
return os.rename(file, file)
15+
end
16+
17+
function get_platform()
18+
if ofs.ExtensionDir():find("^/home/") ~= nil then
19+
local home = os.getenv( "HOME" )
20+
print("User Home: ", home)
21+
if exists(home.."/miniconda3/envs/funscript-editor") then
22+
return "Linux, Conda"
23+
elseif exists(home.."/anaconda3/envs/funscript-editor") then
24+
return "Linux, Conda"
25+
else
26+
return "Linux, Python"
27+
end
28+
else
29+
return "Windows"
30+
end
31+
end
32+
33+
platform = get_platform()
34+
35+
function start_funscript_generator()
36+
if processHandleMTFG then
37+
print('MTFG already running')
38+
return
39+
end
40+
41+
scriptIdx = ofs.ActiveIdx()
42+
local tmpFile = ofs.ExtensionDir() .. "/funscript_actions.json"
43+
local video = player.CurrentVideo()
44+
local script = ofs.Script(scriptIdx)
45+
local currentTimeMs = player.CurrentTime() * 1000
46+
47+
print("tmpFile: ", tmpFile)
48+
print("video: ", video)
49+
print("currentScriptIdx: ", scriptIdx)
50+
print("currentTimeMs: ", currentTimeMs)
51+
52+
local next_action = ofs.ClosestActionAfter(script, currentTimeMs / 1000)
53+
if next_action and script.actions[next_action].at < (currentTimeMs + 500) then
54+
next_action = ofs.ClosestActionAfter(script, script.actions[next_action].at / 1000)
55+
end
56+
57+
print("nextAction: ", next_action and tostring(script.actions[next_action].at) or "nil")
58+
59+
local cmd = ""
60+
local args = {}
61+
62+
if platform == "Windows" then
63+
cmd = ofs.ExtensionDir() .. "/funscript-editor/funscript-editor.exe"
64+
elseif platform == "Linux, Python" then
65+
cmd = "/usr/bin/python3"
66+
table.insert(args, ofs.ExtensionDir() .. "/Python-Funscript-Editor/funscript-editor.py")
67+
elseif platform == "Linux, Conda" then
68+
cmd = "/usr/bin/bash"
69+
table.insert(args, ofs.ExtensionDir() .. "/Python-Funscript-Editor/conda_wrapper.sh")
70+
else
71+
print("ERROR: Platform Not Implemented (", platform, ")")
72+
end
73+
74+
table.insert(args, "--generator")
75+
table.insert(args, "-s")
76+
table.insert(args, tostring(currentTimeMs))
77+
table.insert(args, "-i")
78+
table.insert(args, video)
79+
table.insert(args, "-o")
80+
table.insert(args, tmpFile)
81+
82+
if next_action then
83+
table.insert(args, "-e")
84+
table.insert(args, tostring(script.actions[next_action].at))
85+
end
86+
87+
print("cmd: ", cmd)
88+
print("args: ", table.unpack(args))
89+
90+
processHandleMTFG = ofs.CreateProcess(cmd, table.unpack(args))
91+
92+
status = "MTFG running"
93+
end
94+
95+
96+
function import_funscript_generator_csv_result()
97+
status = "MTFG not running"
98+
local tmpFile = ofs.ExtensionDir() .. "/funscript_actions.csv"
99+
local f = io.open(tmpFile)
100+
if not f then
101+
print('Funscript Generator csv output file not found')
102+
return
103+
end
104+
105+
script = ofs.Script(scriptIdx)
106+
local k = 1
107+
for line in f:lines() do
108+
-- first line is header
109+
if k > 1 then
110+
for at, pos in string.gmatch(line, "(%w+);(%w+)") do
111+
ofs.AddAction(script, at, pos, true)
112+
end
113+
end
114+
k = k + 1
115+
end
116+
f:close()
117+
118+
-- save changes
119+
ofs.Commit(script)
120+
121+
-- delete csv file
122+
os.remove(tmpFile)
123+
end
124+
125+
function import_funscript_generator_json_result()
126+
status = "MTFG not running"
127+
local tmpFile = ofs.ExtensionDir() .. "/funscript_actions.json"
128+
local f = io.open(tmpFile)
129+
if not f then
130+
print('Funscript Generator json output file not found')
131+
return
132+
end
133+
134+
local content = f:read("*a")
135+
f:close()
136+
json_body = json.decode(content)
137+
actions = json_body["actions"]
138+
139+
script = ofs.Script(scriptIdx)
140+
141+
for metric, actions_metric in pairs(actions) do
142+
print('metric', metric)
143+
for _, action in pairs(actions_metric) do
144+
ofs.AddAction(script, action["at"], action["pos"], true)
145+
end
146+
end
147+
148+
-- save changes
149+
ofs.Commit(script)
150+
151+
-- delete json file
152+
os.remove(tmpFile)
153+
end
154+
155+
156+
function invert_selected()
157+
local script = ofs.Script(ofs.ActiveIdx())
158+
for idx, action in ipairs(script.actions) do
159+
if action.selected then
160+
action.pos = clamp(100 - action.pos, 0, 100)
161+
end
162+
end
163+
ofs.Commit(script)
164+
end
165+
166+
167+
function align_bottom_points(align_value)
168+
local script = ofs.Script(ofs.ActiveIdx())
169+
170+
-- get min value in selection if no align_value was given
171+
if align_value < 0 then
172+
align_value = 99
173+
for idx, action in ipairs(script.actions) do
174+
if action.selected then
175+
if action.pos < align_value then
176+
align_value = action.pos
177+
end
178+
end
179+
end
180+
end
181+
182+
-- align bottom points
183+
for idx, action in ipairs(script.actions) do
184+
if action.selected then
185+
local bottom_point = true
186+
187+
local next_action = ofs.ClosestActionAfter(script, action.at / 1000)
188+
if next_action then
189+
if script.actions[next_action].pos <= action.pos then
190+
bottom_point = false
191+
end
192+
end
193+
194+
local prev_action = ofs.ClosestActionBefore(script, action.at / 1000)
195+
if prev_action then
196+
if script.actions[prev_action].pos <= action.pos then
197+
bottom_point = false
198+
end
199+
end
200+
201+
if bottom_point then
202+
action.pos = align_value
203+
end
204+
end
205+
end
206+
207+
ofs.Commit(script)
208+
end
209+
210+
211+
function align_top_points(align_value)
212+
local script = ofs.Script(ofs.ActiveIdx())
213+
214+
-- get max value in selection if no align_value was given
215+
if align_value < 0 then
216+
align_value = 1
217+
for idx, action in ipairs(script.actions) do
218+
if action.selected then
219+
if action.pos > align_value then
220+
align_value = action.pos
221+
end
222+
end
223+
end
224+
end
225+
226+
-- align top points
227+
for idx, action in ipairs(script.actions) do
228+
if action.selected then
229+
local top_point = true
230+
231+
local next_action = ofs.ClosestActionAfter(script, action.at / 1000)
232+
if next_action then
233+
if script.actions[next_action].pos >= action.pos then
234+
top_point = false
235+
end
236+
end
237+
238+
local prev_action = ofs.ClosestActionBefore(script, action.at / 1000)
239+
if prev_action then
240+
if script.actions[prev_action].pos >= action.pos then
241+
top_point = false
242+
end
243+
end
244+
245+
if top_point then
246+
action.pos = align_value
247+
end
248+
end
249+
end
250+
251+
ofs.Commit(script)
252+
end
253+
254+
255+
function init()
256+
print("Detected OS: ", platform)
257+
ofs.Bind("start_funscript_generator", "execute the funcript generator")
258+
local version_file_path = ""
259+
if platform == "Windows" then
260+
version_file_path = ofs.ExtensionDir().."\\funscript-editor\\funscript_editor\\VERSION.txt"
261+
else
262+
local mtfg_repo_path = ofs.ExtensionDir().."/Python-Funscript-Editor"
263+
local cmd = "git -C "..mtfg_repo_path.." describe --tags `git -C "..mtfg_repo_path.." rev-list --tags --max-count=1` > "..mtfg_repo_path.."/funscript_editor/VERSION.txt"
264+
-- print("cmd: ", cmd)
265+
os.execute(cmd)
266+
version_file_path = mtfg_repo_path.."/funscript_editor/VERSION.txt"
267+
end
268+
local f = io.open(version_file_path)
269+
if f then
270+
for line in f:lines() do
271+
if string.find(string.lower(line), "v") then
272+
mtfgVersion = string.lower(line):gsub("v", "")
273+
end
274+
end
275+
f:close()
276+
end
277+
end
278+
279+
280+
function update(delta)
281+
updateCounter = updateCounter + 1
282+
if processHandleMTFG and not ofs.IsProcessAlive(processHandleMTFG) then
283+
print('funscript generator completed import result')
284+
processHandleMTFG = nil
285+
import_funscript_generator_json_result()
286+
end
287+
if math.fmod(updateCounter, 1000) == 1 then
288+
local logfile = ""
289+
if platform == "Windows" then
290+
logfile = "C:/Temp/funscript_editor.log"
291+
else
292+
logfile = "/tmp/funscript_editor.log"
293+
end
294+
local f = io.open(logfile)
295+
if f then
296+
logfileExist = true
297+
f:close()
298+
else
299+
logfileExist = false
300+
end
301+
end
302+
end
303+
304+
305+
function gui()
306+
ofs.Text("Status: "..status)
307+
ofs.Text("Version: "..mtfgVersion)
308+
ofs.Text("Action:")
309+
310+
ofs.SameLine()
311+
if not processHandleMTFG then
312+
313+
if ofs.Button("Start MTFG") then
314+
start_funscript_generator()
315+
end
316+
else
317+
if ofs.Button("Kill MTFG") then
318+
if platform == "Windows" then
319+
os.execute("taskkill /f /im funscript-editor.exe")
320+
else
321+
os.execute("pkill -f funscript-editor.py")
322+
end
323+
end
324+
end
325+
326+
ofs.SameLine()
327+
if ofs.Button("Open Config") then
328+
if platform == "Windows" then
329+
processHandleConfigDir = ofs.CreateProcess("explorer.exe", ofs.ExtensionDir().."\\funscript-editor\\funscript_editor\\config")
330+
else
331+
local cmd = '/usr/bin/dbus-send --session --print-reply --dest=org.freedesktop.FileManager1 --type=method_call /org/freedesktop/FileManager1 org.freedesktop.FileManager1.ShowItems array:string:"file://'
332+
..ofs.ExtensionDir()..'/Python-Funscript-Editor/funscript_editor/config/" string:""'
333+
-- print("cmd: ", cmd)
334+
os.execute(cmd)
335+
end
336+
end
337+
338+
if logfileExist then
339+
if platform == "Windows" then
340+
-- ofs.SameLine()
341+
-- if ofs.Button("Open Log") then
342+
-- processHandleLogFile = ofs.CreateProcess("notepad.exe", "C:/Temp/funscript_editor.log")
343+
-- end
344+
else
345+
ofs.SameLine()
346+
if ofs.Button("Open Log") then
347+
processHandleLogFile = ofs.CreateProcess("/usr/bin/xdg-open", "/tmp/funscript_editor.log")
348+
end
349+
end
350+
end
351+
352+
ofs.Separator()
353+
ofs.Text("Post-Processing:")
354+
ofs.SameLine()
355+
if ofs.Button("Invert") then
356+
invert_selected()
357+
end
358+
359+
ofs.SameLine()
360+
if ofs.Button("Align Bottom Points") then
361+
align_bottom_points(-1)
362+
end
363+
364+
ofs.SameLine()
365+
if ofs.Button("Align Top Points") then
366+
align_top_points(-1)
367+
end
368+
369+
end

0 commit comments

Comments
 (0)