-
Notifications
You must be signed in to change notification settings - Fork 85
/
ListScriptFunctions-[0.2].lua
491 lines (431 loc) · 18.2 KB
/
ListScriptFunctions-[0.2].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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Open Broadcaster Software®️
OBS > Tools > Scripts
@midnight-studios
Stopwatch
***************************************************************************************************************************************
Version 0.2
Published / Released: 2023-07-13 16:11
NEW FEATURES
-
OPTIMIZATION
-
USER EXPERIENCE & FEATURE ENHANCEMENTS
-
BUGS
- fixed an issue where certain file names are incorrectly validated.
***************************************************************************************************************************************
]]
--Globals
obs = obslua
luafileTitle = "Index Script Functions"
gversion = 0.2
luafile = ""
obsurl = ""
icon="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAqCAYAAADS4VmSAAAInklEQVRYhcVYeWwcVxn/3ps3O7vey7ub+Nht4nWSmq5zOtRJnAYqVUL8UYFAKWoRLaqEhBBIIFIpVPQPkAIUCEHiEH8gFIQoAlFxSM0f4RBqlDSpQIHU1EnsOFkfcezsxvYe3pnZmdn30DfHeux47QRxfLI1xzu+6/d+3zdLzpz5AwwODvywo2Pz5+A/KJzz6YmJqWeHh0cuhUJBwhgDIcR9CsiNGzf7d+zYNlKr1UqSJF1mjEmWZRFJkgQuwDWUUmg0LEKpJAjBzQWhlOA4ASDuO04opbikIYTIKUogDQDGtWujfVevjk5GoxEcJwDgWYH3wEzTiuGNYZinrlz5+9c8KyWJ2srxGQ3gvAGEUCCEoDL72vTCfYdiWRbs3Jn7VDrd9RMACORy77moafqjk5PTaiwWIb4g2HeMc27gDaU0Wq0uARopSU64PB24yFEo3HtoXpfH7ciAYRggy6zdF+X0wMCe86qqvbdYLPJ4PE45TnQiINyoADQaDSrLDAKBAJFlRgIBmciy8+/cM9+9vMa4cw0EZNufFXkmZP/+/Xtfa2sLw9JSjRNf+NAaJxeMgSRJfqAQ/x4PCk9c7qXDL21toU8cOXLolCzLoGk692ygCCI3Am6em7r8kL0fvi0E1yNm1pJwuO3Y0NDgMVRhGAZ35zo5cBQ/sKPrRECAk+K1pb09fmpgYM8ziBXTNO0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VI5jIxcOygeQvD3HU845zXfSvNh9kHJ5yefx7ZcWzcHq2TVr2NNbGBEH2YfFLvMF4v3RrZsyfw8HG572vc5/d8U9IBzzocXFxfP/gtqQwz9/J6x4gAAAABJRU5ErkJggg=="
desc = [[
<hr/><center><h2>]] .. luafileTitle ..[[</h2>(Version: %s)</center>
<br><center><img width=32 height=42 src=']] .. icon .. [['/></center>
<br><center><a href="https://github.com/midnight-studios/obs-lua/blob/main/]] .. luafile ..[[">Find it on GitHub</a></center>
<br><p>Index all functions found in LUA Script file</p>
<p>
<ol>
<li><tt>`Index functions names`</tt></li>
<li><tt>`Includes comment with line number where function was read.`</tt></li>
</ol></p>
<p>Find help on the <a href="https://obsproject.com/forum/resources/]] .. obsurl ..[[">OBS Forum Thread</a>.</p>
<hr/>]]
-- global context information
local ctx = {
propsDef = nil, -- property definition
propsDefSrc = nil, -- property definition (source scene)
propsSet = nil, -- property settings (model)
propsVal = {}, -- property values
propsValSrc = nil, -- property values (first source scene)
}
write_file = false;
formats={
["lua"]={
name="Lua",
single="%-%-[^%[%[\n\r%]%]]+",
multi="%-%-%[%[.-%]%]"
},
}
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description:
Credit:
Modified:
function:
type:
input type:
returns:
----------------------------------------------------------------------------------------------------------------------------------------
]]
function script_description()
return string.format( desc, tostring( gversion ) )
end
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description:
Credit:
Modified:
function:
type:
input type:
returns:
----------------------------------------------------------------------------------------------------------------------------------------
]]
local function pre_dump( input )
if type( input ) == "table" then
local str = "{ ";
for key, value in pairs( input ) do
if type( key ) ~= "number" then key = "'" .. key .. "'" end;
str = str .. "[" .. key .. "] = " .. pre_dump( value ) .. ",";
end;
return str .. "} ";
else
return tostring( input );
end;
end
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description:
Credit:
Modified:
function:
type:
input type:
returns:
----------------------------------------------------------------------------------------------------------------------------------------
]]
local function log( name, msg )
if msg ~= nil then
msg = " > " .. tostring( msg );
else
msg = "";
end;
obs.script_log( obs.LOG_DEBUG, tostring( name ) .. msg );
end
--[[
----------------------------------------------------------
helper function: set status message
----------------------------------------------------------
]]
local function statusMessage( message )
log( "message", pre_dump( message ) );
obs.obs_data_set_string( ctx.propsSet, "statusMessage", string.format( "%s", message ) )
obs.obs_properties_apply_settings( ctx.propsDef, ctx.propsSet )
return true
end
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description:
Credit:
Modified:
function:
type:
input type:
returns:
----------------------------------------------------------------------------------------------------------------------------------------
]]
function get_filenames( path )
local filenames = {}
local dir = obs.os_opendir( path )
local entry
repeat
entry = obs.os_readdir(dir)
if entry then
local ext = obs.os_get_path_extension( entry.d_name )
if ext == ".lua" then
local filename = string.gsub( entry.d_name, ext, "" )
table.insert( filenames, filename )
end
end
until not entry
obs.os_closedir( dir )
return filenames
end
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description:
Credit:
Modified:
function:
type:
input type:
returns:
----------------------------------------------------------------------------------------------------------------------------------------
]]
function lookup_functions(fileName)
if fileName ~= nil then
local f = io.open(fileName, "r")
local txt = f:read("*a")
f:close()
local extension = fileName:match("^.+%.(.+)$")
if not extension then
return nil
end
extension = extension:lower()
if formats[extension] then
local functionNames = {}
local lineNumbers = {}
local functionCount = 0
local lineNumber = 1
for line in txt:gmatch("[^\r\n]+") do
local functionName = line:match("function%s+([%w_]+)%(")
if functionName then
local variables = line:match("%b()")
if variables then
variables = variables:sub(2, -2) -- Remove the parentheses
table.insert(functionNames, functionName .. "(" .. variables .. ")")
else
table.insert(functionNames, functionName)
end
table.insert(lineNumbers, lineNumber)
functionCount = functionCount + 1
end
lineNumber = lineNumber + 1
end
return functionCount, lineNumbers, functionNames
else
return 0, {}, {}
end
else
return 0, {}, {}
end
end
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description:
Credit:
Modified:
function:
type:
input type:
returns:
----------------------------------------------------------------------------------------------------------------------------------------
]]
function calculate_table_item_len(tbl)
local maxLen = 0
local longestItem = nil
local minLen = math.huge
local shortestItem = nil
for _, value in pairs(tbl) do
if type(value) == "string" then
local length = #value
if length > maxLen then
maxLen = length
longestItem = value
end
if length < minLen then
minLen = length
shortestItem = value
end
end
end
return longestItem, maxLen, shortestItem, minLen
end
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description:
Credit:
Modified:
function:
type:
input type:
returns:
----------------------------------------------------------------------------------------------------------------------------------------
]]
function get_contents()
local path = obs.obs_data_get_string(ctx.propsSet, "path")
local file = obs.obs_data_get_string(ctx.propsSet, "file")
local inputfile = path .. "/" .. file .. ".lua"
local contents = ""
local list = {}
local listLineNumbers = {}
if obs.os_file_exists(inputfile) then
local functionCount, lineNumbers, functions = lookup_functions(inputfile);
contents = "Total Functions: " .. functionCount .. "\n\n"
for i, functionName in ipairs( functions ) do
list[i] = functionName;
listLineNumbers[i] = lineNumbers[i]
end
end
local longestItem, maxLen, shortestItem, minLen = calculate_table_item_len( list )
local line_data
for key, value in pairs( list ) do
if key < #list then
newLine = "\n"
else
newLine = ""
end
line_data = key .. ")" .. string.rep(" ", ((string.len(tostring(functionCount)) - string.len(tostring(key))) + 1)) .. list[key] .. string.rep(" ", #longestItem - #value) .. " -- Line: " .. listLineNumbers[key] .. newLine;
contents = contents .. line_data
end
return contents
end
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description:
Credit:
Modified:
function:
type:
input type:
returns:
----------------------------------------------------------------------------------------------------------------------------------------
]]
function write_to_file( start_write )
write_file = start_write and true or false
path = obs.obs_data_get_string( ctx.propsSet, "path" )
file = obs.obs_data_get_string( ctx.propsSet, "file" )
local unique_stamp = string.format('%s%s' .. os.date("%d.%m.%Y.%H.%M.%S") .. '.%s%s',' - Function Index ', '(', string.sub(math.floor( (obs.os_gettime_ns()/100000000) ), 5, 7), ')')
local inputfile = path .. "/" .. file .. ".lua"
local outputfile = path .. "/" .. file .. unique_stamp .. ".lua"
if obs.os_file_exists(inputfile) then
f = get_contents(inputfile)
else
statusMessage('File does not exist! (' .. inputfile .. ')')
end
if f ~= nil then
newfile = io.open(outputfile, "w")
newfile:write(f)
newfile:close()
statusMessage( string.format( "%s", f ) )
end
write_file = false;
return true
end
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description:
Credit:
Modified:
function:
type:
input type:
returns:
----------------------------------------------------------------------------------------------------------------------------------------
]]
function property_onchange( props, property, settings )
local notice = ""
local file = obs.obs_data_get_string( settings, "file" )
local path = obs.obs_data_get_string( settings, "path" )
local file_prop = obs.obs_properties_get( props, "file" )
local notice_prop = obs.obs_properties_get( props, "statusMessage" )
local filenames = get_filenames( path )
local has_file_list = (table.getn( filenames ) > 0)
local button = obs.obs_properties_get( props, "action_button" )
local inputfile = path .. "/" .. file .. ".lua"
local has_file = obs.os_file_exists(inputfile)
obs.obs_property_set_visible( obs.obs_properties_get( props, "action_button" ), false )
obs.obs_property_set_visible( obs.obs_properties_get( props, "action_button" ), has_file )
obs.obs_property_list_clear( file_prop )
obs.obs_property_list_add_string( file_prop, 'Select', 'select' )
if path ~= "" then
if has_file_list then
notice = "Folder selected, please select the file from the dropdown list."
else
notice = "The selected folder does not contain any valid lua files.\nPlease select a valid directory."
end
else
notice = "Please select a folder containing your lua files."
end
if file ~= "select" then
if has_file then
notice = "File ready for processing, please click the 'Execute' button."
else
notice = "Cannot find the file you selected. \nPlease check that the file exist."
end
end
if has_file_list then
for i,v in pairs( filenames ) do
obs.obs_property_list_add_string( file_prop, v, v )
end
end
if not write_file then
obs.obs_data_set_string( settings, "statusMessage", notice )
else
end
return true
end
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description:
Credit:
Modified:
function:
type:
input type:
returns:
----------------------------------------------------------------------------------------------------------------------------------------
]]
function script_properties()
ctx.propsDef = obs.obs_properties_create()
local notice = obs.obs_properties_add_text( ctx.propsDef, "statusMessage", "Notice:", obs.OBS_TEXT_MULTILINE )
local p_a = obs.obs_properties_add_path( ctx.propsDef, "path", "Select Folder", obs.OBS_PATH_DIRECTORY, nil, nil)
local p_b = obs.obs_properties_add_list( ctx.propsDef, "file", "<i>Select file</i>", obs.OBS_COMBO_TYPE_LIST, obs.OBS_COMBO_FORMAT_STRING )
obs.obs_property_list_add_string( p_b, 'Select', 'select' )
obs.obs_property_set_long_description( p_b, "\nSelect the file to remove the comments.\n" )
local filenames = get_filenames( path )
if table.getn( filenames ) > 0 then
for i,v in pairs( filenames ) do
obs.obs_property_list_add_string( p_b, v, v )
end
end
obs.obs_properties_add_button( ctx.propsDef, "action_button", "Execute", write_to_file )
obs.obs_property_set_modified_callback( p_a, property_onchange )
obs.obs_property_set_modified_callback( p_b, property_onchange )
-- Calls the callback once to set-up current visibility
obs.obs_properties_apply_settings( ctx.propsDef, ctx.propsSet )
return ctx.propsDef
end
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description:
Credit:
Modified:
function:
type:
input type:
returns:
----------------------------------------------------------------------------------------------------------------------------------------
]]
function script_update( settings )
ctx.propsSet = settings
ctx.propsVal.statusMessage = obs.obs_data_get_string( settings, "statusMessage" )
end
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description:
Credit:
Modified:
function:
type:
input type:
returns:
----------------------------------------------------------------------------------------------------------------------------------------
]]
function script_defaults( settings )
obs.obs_data_set_default_string( settings, "path", "" )
obs.obs_data_set_default_string( settings, "file", "Select" )
obs.obs_data_set_default_string( settings, "statusMessage", "To get started, please select the file folder." )
obs.obs_data_set_default_string( settings, "statusMessage", "" )
end
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description:
Credit:
Modified:
function:
type:
input type:
returns:
----------------------------------------------------------------------------------------------------------------------------------------
]]
function script_load( settings )
-- clear status message
--obs.obs_data_set_string( settings, "statusMessage", "" );
end