1- json = require " json"
1+ json = require " json.lua"
2+ -- MTFG LUA Wrappper Version 2.0.0
23
34-- global var
45processHandleMTFG = nil
@@ -13,6 +14,7 @@ multiaxis = false
1314tmpFileName = " funscript_actions.json"
1415tmpFileExists = false
1516enableLogs = false
17+ filterSimilarTimestamps = true
1618scriptNames = {}
1719scriptNamesCount = 0
1820scriptAssignment = {x = {idx = 1 }, y = {idx = 1 }, roll = {idx = 1 }}
3941
4042platform = get_platform ()
4143
42- function start_funscript_generator ()
44+ function binding . start_funscript_generator ()
4345 if processHandleMTFG then
4446 print (' MTFG already running' )
4547 return
@@ -56,12 +58,12 @@ function start_funscript_generator()
5658 print (" currentScriptIdx: " , scriptIdx )
5759 print (" currentTimeMs: " , currentTimeMs )
5860
59- local next_action = ofs . ClosestActionAfter ( script , currentTimeMs / 1000 )
60- if next_action and script . actions [ next_action ] .at < (currentTimeMs + 500 ) then
61- next_action = ofs . ClosestActionAfter ( script , script . actions [ next_action ] .at / 1000 )
61+ local next_action , _ = script : closestActionAfter ( currentTimeMs / 1000.0 )
62+ if next_action and ( next_action .at * 1000 ) < (currentTimeMs + 500 ) then
63+ next_action , _ = script : closestActionAfter ( next_action .at )
6264 end
6365
64- print (" nextAction: " , next_action and tostring (script . actions [ next_action ] .at ) or " nil" )
66+ print (" nextAction: " , next_action and tostring (next_action .at ) or " nil" )
6567
6668 local cmd = " "
6769 local args = {}
@@ -97,47 +99,18 @@ function start_funscript_generator()
9799
98100 if next_action then
99101 table.insert (args , " -e" )
100- table.insert (args , tostring (script . actions [ next_action ] .at ))
102+ table.insert (args , tostring (next_action .at * 1000.0 ))
101103 end
102104
103105 print (" cmd: " , cmd )
104106 print (" args: " , table.unpack (args ))
105107
106- processHandleMTFG = ofs . CreateProcess (cmd , table.unpack (args ))
108+ processHandleMTFG = Process . new (cmd , table.unpack (args ))
107109
108110 status = " MTFG running"
109111end
110112
111113
112- function import_funscript_generator_csv_result ()
113- status = " MTFG not running"
114- local tmpFile = ofs .ExtensionDir () .. " /" .. tmpFileName
115- local f = io.open (tmpFile )
116- if not f then
117- print (' Funscript Generator csv output file not found' )
118- return
119- end
120-
121- script = ofs .Script (scriptIdx )
122- local k = 1
123- for line in f :lines () do
124- -- first line is header
125- if k > 1 then
126- for at , pos in string.gmatch (line , " (%w+);(%w+)" ) do
127- ofs .AddAction (script , at , pos , true )
128- end
129- end
130- k = k + 1
131- end
132- f :close ()
133-
134- -- save changes
135- ofs .Commit (script )
136-
137- -- delete csv file
138- os.remove (tmpFile )
139- end
140-
141114function import_funscript_generator_json_result ()
142115 status = " MTFG not running"
143116 local tmpFile = ofs .ExtensionDir () .. " /" .. tmpFileName
@@ -152,18 +125,24 @@ function import_funscript_generator_json_result()
152125 json_body = json .decode (content )
153126 actions = json_body [" actions" ]
154127
128+ local filtered = 0
155129 if multiaxis then
156130 local i = 1
157- while ofs .Script ( i ) do
158- name = ofs .ScriptTitle (i )
131+ while i <= ofs .ScriptCount ( ) do
132+ name = ofs .ScriptName (i )
159133 for k ,v in pairs (scriptAssignment ) do
160134 if name and name == scriptNames [v .idx ] then
161135 if actions [k ] then
162136 script = ofs .Script (i )
163137 for _ , action in pairs (actions [k ]) do
164- ofs .AddAction (script , action [" at" ], action [" pos" ], true )
138+ local closest_action , _ = script :closestAction (action [" at" ])
139+ if filterSimilarTimestamps and closest_action and math.abs (closest_action .at - (action [" at" ]/ 1000.0 )) < 0.01 then
140+ filtered = filtered + 1
141+ else
142+ script .actions :add (Action .new (action [" at" ]/ 1000.0 , action [" pos" ], true ))
143+ end
165144 end
166- ofs . Commit ( script )
145+ script : commit ( )
167146 end
168147 end
169148 end
@@ -173,18 +152,24 @@ function import_funscript_generator_json_result()
173152 script = ofs .Script (scriptIdx )
174153
175154 for metric , actions_metric in pairs (actions ) do
176- print (' add ' , metric , ' to ' , ofs .ScriptTitle (scriptIdx ))
155+ print (' add ' , metric , ' to ' , ofs .ScriptName (scriptIdx ))
177156 for _ , action in pairs (actions_metric ) do
178- ofs .AddAction (script , action [" at" ], action [" pos" ], true )
157+ local closest_action , _ = script :closestAction (action [" at" ]/ 1000.0 )
158+ if filterSimilarTimestamps and closest_action and math.abs (closest_action .at - (action [" at" ]/ 1000.0 )) < 0.01 then
159+ filtered = filtered + 1
160+ else
161+ script .actions :add (Action .new (action [" at" ]/ 1000.0 , action [" pos" ], true ))
162+ end
179163 end
180164 end
181165
182- -- save changes
183- ofs .Commit (script )
166+ script :commit ()
167+ end
168+
169+ if filterSimilarTimestamps then
170+ print (' filtered timestamps' , filtered )
184171 end
185172
186- -- delete json file
187- -- os.remove(tmpFile)
188173end
189174
190175
@@ -195,12 +180,13 @@ function invert_selected()
195180 action .pos = clamp (100 - action .pos , 0 , 100 )
196181 end
197182 end
198- ofs . Commit ( script )
183+ script : commit ( )
199184end
200185
201186
202187function align_bottom_points (align_value )
203188 local script = ofs .Script (ofs .ActiveIdx ())
189+ script :sort ()
204190
205191 -- get min value in selection if no align_value was given
206192 if align_value < 0 then
@@ -214,21 +200,23 @@ function align_bottom_points(align_value)
214200 end
215201 end
216202
203+ print (' align bottom points to' , align_value )
204+
217205 -- align bottom points
218206 for idx , action in ipairs (script .actions ) do
219207 if action .selected then
220208 local bottom_point = true
221209
222- local next_action = ofs . ClosestActionAfter ( script , action .at / 1000 )
210+ local next_action , _ = script : closestActionAfter ( action .at )
223211 if next_action then
224- if script . actions [ next_action ] .pos <= action .pos then
212+ if next_action .pos <= action .pos then
225213 bottom_point = false
226214 end
227215 end
228216
229- local prev_action = ofs . ClosestActionBefore ( script , action .at / 1000 )
217+ local prev_action , _ = script : closestActionBefore ( action .at )
230218 if prev_action then
231- if script . actions [ prev_action ] .pos <= action .pos then
219+ if prev_action .pos <= action .pos then
232220 bottom_point = false
233221 end
234222 end
@@ -239,12 +227,13 @@ function align_bottom_points(align_value)
239227 end
240228 end
241229
242- ofs . Commit ( script )
230+ script : commit ( )
243231end
244232
245233
246234function align_top_points (align_value )
247235 local script = ofs .Script (ofs .ActiveIdx ())
236+ script :sort ()
248237
249238 -- get max value in selection if no align_value was given
250239 if align_value < 0 then
@@ -258,21 +247,23 @@ function align_top_points(align_value)
258247 end
259248 end
260249
250+ print (' align top points to' , align_value )
251+
261252 -- align top points
262253 for idx , action in ipairs (script .actions ) do
263254 if action .selected then
264255 local top_point = true
265256
266- local next_action = ofs . ClosestActionAfter ( script , action .at / 1000 )
257+ local next_action , _ = script : closestActionAfter ( action .at )
267258 if next_action then
268- if script . actions [ next_action ] .pos >= action .pos then
259+ if next_action .pos >= action .pos then
269260 top_point = false
270261 end
271262 end
272263
273- local prev_action = ofs . ClosestActionBefore ( script , action .at / 1000 )
264+ local prev_action , _ = script : closestActionBefore ( action .at )
274265 if prev_action then
275- if script . actions [ prev_action ] .pos >= action .pos then
266+ if prev_action .pos >= action .pos then
276267 top_point = false
277268 end
278269 end
@@ -283,13 +274,12 @@ function align_top_points(align_value)
283274 end
284275 end
285276
286- ofs . Commit ( script )
277+ script : commit ( )
287278end
288279
289280
290281function init ()
291282 print (" Detected OS: " , platform )
292- ofs .Bind (" start_funscript_generator" , " execute the funcript generator" )
293283 local version_file_path = " "
294284 if platform == " Windows" then
295285 version_file_path = ofs .ExtensionDir ().. " \\ funscript-editor\\ funscript_editor\\ VERSION.txt"
@@ -338,8 +328,8 @@ function update_script_names()
338328 scriptNamesCount = 0
339329 scriptNames = {' ignore' }
340330 scriptNamesCount = scriptNamesCount + 1
341- while ofs .Script ( i ) do
342- name = ofs .ScriptTitle (i )
331+ while i <= ofs .ScriptCount ( ) do
332+ name = ofs .ScriptName (i )
343333 if not is_empty (name ) then
344334 table.insert (scriptNames , name )
345335 scriptNamesCount = scriptNamesCount + 1
362352
363353function update (delta )
364354 updateCounter = updateCounter + 1
365- if processHandleMTFG and not ofs . IsProcessAlive ( processHandleMTFG ) then
355+ if processHandleMTFG and not processHandleMTFG : alive ( ) then
366356 print (' funscript generator completed import result' )
367357 processHandleMTFG = nil
368358 import_funscript_generator_json_result ()
@@ -384,7 +374,7 @@ function gui()
384374 if not processHandleMTFG then
385375
386376 if ofs .Button (" Start MTFG" ) then
387- start_funscript_generator ()
377+ binding . start_funscript_generator ()
388378 end
389379 else
390380 if ofs .Button (" Kill MTFG" ) then
@@ -399,7 +389,7 @@ function gui()
399389 ofs .SameLine ()
400390 if ofs .Button (" Open Config" ) then
401391 if platform == " Windows" then
402- processHandleConfigDir = ofs . CreateProcess (" explorer.exe" , ofs .ExtensionDir ().. " \\ funscript-editor\\ funscript_editor\\ config" )
392+ processHandleConfigDir = Process . new (" explorer.exe" , ofs .ExtensionDir ().. " \\ funscript-editor\\ funscript_editor\\ config" )
403393 else
404394 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://'
405395 .. ofs .ExtensionDir ().. ' /Python-Funscript-Editor/funscript_editor/config/" string:""'
@@ -412,12 +402,12 @@ function gui()
412402 if platform == " Windows" then
413403 ofs .SameLine ()
414404 if ofs .Button (" Open Log" ) then
415- processHandleLogFile = ofs . CreateProcess (" notepad.exe" , " C:/Temp/funscript_editor.log" )
405+ processHandleLogFile = Process . new (" notepad.exe" , " C:/Temp/funscript_editor.log" )
416406 end
417407 else
418408 ofs .SameLine ()
419409 if ofs .Button (" Open Log" ) then
420- processHandleLogFile = ofs . CreateProcess (" /usr/bin/xdg-open" , " /tmp/funscript_editor.log" )
410+ processHandleLogFile = Process . new (" /usr/bin/xdg-open" , " /tmp/funscript_editor.log" )
421411 end
422412 end
423413 end
@@ -450,20 +440,23 @@ function gui()
450440
451441 ofs .Separator ()
452442
453- ofs .Text (" Post-Processing:" )
454- ofs .SameLine ()
455- if ofs .Button (" Invert" ) then
456- invert_selected ()
457- end
443+ local enable_post_processing = true
444+ if enable_post_processing then
445+ ofs .Text (" Post-Processing:" )
446+ ofs .SameLine ()
447+ if ofs .Button (" Invert" ) then
448+ invert_selected ()
449+ end
458450
459- ofs .SameLine ()
460- if ofs .Button (" Align Bottom Points" ) then
461- align_bottom_points (- 1 )
462- end
451+ ofs .SameLine ()
452+ if ofs .Button (" Align Bottom Points" ) then
453+ align_bottom_points (- 1 )
454+ end
463455
464- ofs .SameLine ()
465- if ofs .Button (" Align Top Points" ) then
466- align_top_points (- 1 )
456+ ofs .SameLine ()
457+ if ofs .Button (" Align Top Points" ) then
458+ align_top_points (- 1 )
459+ end
467460 end
468461
469462end
0 commit comments