-
Notifications
You must be signed in to change notification settings - Fork 84
/
StopWatch-[5.7.1].lua
9770 lines (8921 loc) · 403 KB
/
StopWatch-[5.7.1].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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Open Broadcaster Software®️
OBS > Tools > Scripts
@midnight-studios
Stopwatch
***************************************************************************************************************************************
Version 5.7.1
Published / Released: 2024-01-04 08:49
NEW FEATURES
-
OPTIMIZATION
-
USER EXPERIENCE & FEATURE ENHANCEMENTS
-
BUGS
- Fixed hour input limit for count down mode
- Fixed minute input limit for count down mode
***************************************************************************************************************************************
]]
--Globals
obs = obslua;
gversion = "5.7.1";
luafile = "StopWatch.lua";
obsurl = "comprehensive-stopwatch-countdown-timer.1364/";
patch_notes = "Patch Notes";
icon="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABUAAAAVCAYAAACpF6WWAAAENElEQVQ4jY1UTUgjZxh+ksl/JuMkMYb4F40bNZqK0KJFqBZqS9ddyl76dyhdKPRQShH2sNDSnnopCz11D10KS/dSKNiDoD2I7KXFQ0XSSGpM1llFMYn5mZiMY2IymfIOhgazXfaDj5n53u975vme531fnaqqeMHxJYCvAOgAlABcAyA1jxLO1tYW1tbWoL+Kd3x8jGg0imw2C0VRWkMEYgNgBeAFYKTFRqOh7aVnE9xwFTSZTGJ7exszMzPQ6XSQZRk8z9P7YrVa/Y5hmKLBYHCpqirW63Wcn5/j7OwMHo9HA6bvNqY2mw1Op1N70qaTkxPkcjmbLMsDZrN5hOO4NxuNhlMUxTFiSCA0FEW5GQ6H/wmHwzfamDavUKlUYDKZAoFA4Gue52/r9f/9v6OjQ5uKojwpFAr3RFF8UCwWjW63OzQ/P/9yGyiBnZ6eEtN3eZ7/9XJZrlQqP2cymcf5fL4QDAbHdTrd2yzLXvd4PD9yHHdLEISFXC7nsdvtuTb3c7kcEokEJiYmhliWtaiqWs5ms4f1el0lE2lOTU0hn8/DYrF09vb23jebze9JkvRXNBqdMpvNaIJaLh1tHScAzpvsSd+joyOkUimEQiFNa4vFAlEU4Xa7HwYCgduFQuHRxsbGx5p+qqq+o/7/SF7uQSaTwcHBgZYdgiBMqKqa2dnZ8S8tLaFcLicIIR6PjzU13Qew+gzPKNEj9JJOp5tag+O41/v7+x/v7u7+sLOzc8BxHN1icXR0dMXlcn3xQhW1v7+PSCSC6enptxwOx3WWZRcbjcbTjY2NAJ1nWRYGgwHj4+OqoigFYnr/UlPlClYFwJ1arVYjU8bGxhZ8Pt9KMxiLxd5gGEbTlTSv1WqQJOmJw+G4RqCfPYfkN4qiFDs7O9HT0/Nqa4BhmKd2u10DrFaruLi4oJmncibQSUCrLHJabDlHzItGo1E7FIvFvg+FQjMmkykkCMK9eDwOivl8PvqhBspxXJAOEujfz2HazzBMdXh4OJNMJoupVGre7/cbBEGor6+vY2RkROsLlwY6jUajS5KkSGvtf0oVemUeAPiDgsFgUHMeQJ3MmZycxNzcnMZWkiT4/f67FJRl+UFrmcYB/N7y3UyLSHOBzNjb20MgEMDg4CC6urqwublJZo12d3ffVRRFEQTh4TNTqlQqaawoTShOVdOsqMPDQ8zOzmqFQK3PZrO91NPTs2U0GkmWG4lEYrWt9cViMSwvL1Ntvw9gRafT/aTX6z8AwFKcuhU5zjDMkNfr/XZgYCBKgMfHx3eSyeSqw+Fob9LEipxMp9MRp9P5uclkWuB5/hOKWa3Wvb6+vjLP8wNer5fXUkRRLkql0ofZbPY3ug019TZQ6jKU0AzD7Iqi+Josy6+4XK6P7Hb7LbvdPkS5SXpXKpU/ZVn+5ezs7FG9Xi9brVZNLr1ej38BVDs6EbSfFQsAAAAASUVORK5CYII=";
desc =
[[
<hr/><center><h2>Advanced Timer</h2>( Version: %s )</center>
<br><center><img width=38 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>
<center><a href="https://obsproject.com/forum/resources/]] .. obsurl ..[[updates">]] .. patch_notes ..[[</a></center>
<br><p>The Properties for this script will adjust visibility as needed. Some advanced properties will only be visible if the Layout setting is set to "Advanced". If the Layout setting is set to "Basic" any defined values will still be active, so ensure you define those correctly.</p><p>Find help on the <a href="https://obsproject.com/forum/resources/]] .. obsurl ..[[">OBS Forum Thread</a>.</p><hr/>
]];
debug_file = ""
debug_file_name = "Debug Log"
text_prefix = "";
text_suffix = "";
text_prefix_validated = "";
text_suffix_validated = "";
last_text = "";
custom_time_format = "";
timer_source = "";
timer_activation_source = "";
countdown_type = "";
backup_folder = "";
import_list = "";
longtimetext_s = "";
longtimetext_p = "";
last_split_data = "";
split_source = "";
active_source = "";
timer_expire_event = "";
cycle_source_children = "";
stop_text = "";
toggle_mili_trigger = "";
sec_add_1 = "";
sec_add_2 = "";
sec_add_3 = "";
sec_sub_1 = "";
sec_sub_2 = "";
sec_sub_3 = "";
output_file_name = "-backup($date_stamp).json";
font_normal = "#ffffff";
font_dimmed = "#bfbbbf";
font_highlight = "#fffdcf";
add_limit_note_source = "";
sub_limit_note_source = "";
note_source_marker_a = "";
note_source_marker_b = "";
audio_marker_a = "";
audio_marker_b = "";
current_count_direction = "UP";
count_orientation = "NORMAL";
timer_activation = 1;
timer_reset = 0;
debug_entry = 0;
debug_entry = 0;
add_limit_note_source_visible = 0;
sub_limit_note_source_visible = 0;
sources_loaded = 0;
timer_manipulation = 1;
sec_add_limit = 0;
sec_add_limit_used = 0;
sec_sub_limit_used = 0;
sec_sub_limit = 0;
total_sources = 0;
sw_days_saved = 0;
sw_hours_saved = 0;
sw_minutes_saved = 0;
sw_seconds_saved = 0;
sw_milliseconds_saved = 0;
split_type = 2;
current_seconds = 0;
cycle_direction = 1;
default_seconds = 0;
split_count = 0;
timer_year = 0;
timer_month = 0;
timer_day = 0;
timer_hours = 0;
timer_minutes = 0;
timer_seconds = 0;
timer_mode = 0;
last_timer_mode = 0;
timer_format = 1;
start_recording = 1;
media_playback_limit = 0;
enable_marker_notes = 1;
orig_time = 0;
time_frequency = 0;
completed_cycles = 0;
ns_last = 0;
cycle_index = 1;
timer_cycle = 10; --milliseconds
split_itm = {};
connectedHandlers = {};
presuf = {
text_marker_a = "",
text_marker_b = ""
}
ignore_list = {
"",
"none",
"None",
"Select",
"select",
"list"
};
split_data = nil;
hour_format = nil;
minute_format = nil;
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)
};
props = nil;
enable_property_toggle_mili_trigger = true;
set_visible_properties = false;
enable_property_color_normal = true;
enable_property_color_marker_a = true;
enable_property_color_marker_b = true;
enable_property_split_source = true;
enable_property_trigger_options = true;
enable_property_start_recording = true;
enable_property_text_prefix = true;
enable_property_text_suffix = true;
enable_property_timer_manipulation = true;
enable_property_timer_activation = true;
enable_property_debug = true;
enable_property_backup = true;
timer_event_active = false;
timer_mode_changed = false;
debug_enabled = false; -- careful, may use more system memory
script_ready = false;
set_timer_activated = false;
color_normal_updated = false;
activated = false;
prevent_callback = false;
timer_active = false;
reset_activated = false;
active_source_force_visible = false;
split_startpause = false;
disable_script = false;
enable_direction_toggle = false;
show_mili = true;
timer_expired = true;
mili_toggle_triggered = false;
direction_changed = false;
prevent_negative_time = false;
record_timer_set = false;
media = { -- table start
text_marker_a = "",
text_marker_b = "",
source_name_audio_marker_a = "",
source_name_audio_marker_b = "",
source_name_audio_marker_end = "",
note_source_marker_a = "",
note_source_marker_b = "",
note_source_unversioned_id_marker_a = "";
note_source_unversioned_id_marker_b = "";
note_marker_a = "",
note_marker_b = "",
activated_marker_a = false,
activated_marker_b = false,
activated_media_marker_a = false,
activated_media_marker_b = false,
activated_time_marker_a = 0,
activated_time_marker_b = 0,
cycle_direction_marker_a = 2;
cycle_direction_marker_b = 2;
cycle_index_marker_a = 1; -- index from 1-based table
cycle_index_marker_b = 1; -- index from 1-based table
cycle_index_note_marker_a = 1; -- index from 1-based table
cycle_index_note_marker_b = 1; -- index from 1-based table
current_seconds_marker_a = 0,
current_seconds_marker_b = 0,
duration_marker_a = 0,
duration_marker_b = 0,
reset_text_marker_a = 0,
reset_text_marker_b = 0,
hide_note_marker_a = 0,
hide_note_marker_b = 0,
duration_marker_end = 0,
last_orientation_marker_a = "NORMAL";
last_orientation_marker_b = "NORMAL";
media_ended_marker_a = false,
media_ended_marker_b = false,
last_state_marker_a = obs.OBS_MEDIA_STATE_NONE,
last_state_marker_b = obs.OBS_MEDIA_STATE_NONE
}; -- table end
selected_source_list = {};
hotkey_id_reset = obs.OBS_INVALID_HOTKEY_ID;
hotkey_id_start = obs.OBS_INVALID_HOTKEY_ID;
hotkey_id_pause = obs.OBS_INVALID_HOTKEY_ID;
hotkey_id_split = obs.OBS_INVALID_HOTKEY_ID;
hotkey_id_mili = obs.OBS_INVALID_HOTKEY_ID;
hotkey_id_direction = obs.OBS_INVALID_HOTKEY_ID;
hotkey_id_sec_add_1 = obs.OBS_INVALID_HOTKEY_ID;
hotkey_id_sec_add_2 = obs.OBS_INVALID_HOTKEY_ID;
hotkey_id_sec_add_3 = obs.OBS_INVALID_HOTKEY_ID;
hotkey_id_sec_sub_1 = obs.OBS_INVALID_HOTKEY_ID;
hotkey_id_sec_sub_2 = obs.OBS_INVALID_HOTKEY_ID;
hotkey_id_sec_sub_3 = obs.OBS_INVALID_HOTKEY_ID;
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description: Function to detect the operating system
Credit:
Modified:
function:
type:
input type:
returns:
----------------------------------------------------------------------------------------------------------------------------------------
]]
function detectOS()
local os = os.getenv("OS")
if os and os:lower():match("windows") then
return "Windows"
else
return "Mac"
end
end
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description: Assign OS type to variable
Credit:
Modified:
function:
type:
input type:
returns:
----------------------------------------------------------------------------------------------------------------------------------------
]]
osType = detectOS()
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description: Compatibility Sequence Variable. Assign operating system specific variable
Credit:
Modified:
function:
type:
input type:
returns:
----------------------------------------------------------------------------------------------------------------------------------------
]]
if osType == "Windows" then
text_source_unversioned_id = "text_gdiplus"
text_source_id = "text_gdiplus_v2"
media = { -- table start
color_normal = 4294967295, -- 0xFFFFFFFF
color_marker_a = 4256749, -- 0x40f3ed
color_marker_b = 329050 -- 0x05055a
}
else
text_source_unversioned_id = "text_ft2_source"
text_source_id = "text_ft2_source_v2"
media = { -- table start
color_normal = 4294967295,
color_marker_a = 4282446829,
color_marker_b = 4278519130
}
end
required_sources = {
"ffmpeg_source",
text_source_id,
"color_source_v3"
}
--[[
deque implementation - First item in is firs item out
With a deque, removing items from both ends is fast regardless of the size of the deque.
Deque.new(): Creates a new deque.
Initially, the first index is set to 0 and the last index is set to -1, meaning the deque is empty.
pushleft(value): Adds a new value to the front of the deque. It does this by decreasing the first
index by 1 and storing the value at this new index.
popright(): Removes a value from the end of the deque.
It does this by taking the value at the last index, removing this value from the deque, and decreasing
the last index by 1. The function then returns the removed value.
Whenever a new item is used, it's added to the deque with pushleft().
When required it removes the oldest item with popright().
]]
local Deque = {} -- Declare the Deque table to act as the class
Deque.__index = Deque -- Set the __index of Deque to itself to allow method lookups.
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description: This function creates a new Deque instance (i.e., an object)
Constructor for creating a new instance of Deque
Initializes an empty Deque with the first index at 0 and the last index at -1.
Credit:
Modified:
function:
type:
input type:
returns:
----------------------------------------------------------------------------------------------------------------------------------------
]]
function Deque.new()
-- Sets the first index at 0 and the last index at -1 indicating an empty Deque
-- The metatable is set to Deque, so methods can be invoked on this instance
return setmetatable({first = 0, last = -1}, Deque)
end
--[[
----------------------------------------------------------------------------------------------------------------------------------------
We create a new instance of Deque to store items.
----------------------------------------------------------------------------------------------------------------------------------------
]]
media["used_note_source_marker_a"] = Deque.new()
media["used_note_source_marker_b"] = Deque.new()
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description: This method adds a value to the start (left) of the Deque
Method to add a value to the front (left) of the Deque.
Credit:
Modified:
function:
type:
input type:
returns:
----------------------------------------------------------------------------------------------------------------------------------------
]]
function Deque:pushleft(value)
-- We decrease the index for the 'first' element.
local first = self.first - 1
-- Assign the new index as our new 'first' index.
self.first = first
-- Assign the passed value at the new 'first' position.
self[first] = value
end
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description: This method removes a value from the end (right) of the Deque and returns it
This method also returns the removed value.
Credit:
Modified:
function:
type:
input type:
returns:
----------------------------------------------------------------------------------------------------------------------------------------
]]
function Deque:popright()
-- We store the 'last' index in a local variable.
local last = self.last
-- If 'first' index is greater than 'last', then the Deque is empty.
if self.first > last then error("deque is empty") end
-- We store the value at the 'last' index in a local variable.
local value = self[last]
-- Remove the value from the Deque.
self[last] = nil
-- Decrease the 'last' index by one, moving it one step to the left.
self.last = last - 1
-- Return the removed value.
return value
end
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description:
Credit:
Modified:
function:
type:
input type:
returns:
----------------------------------------------------------------------------------------------------------------------------------------
]]
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description: A function named script_description returns the description shown to
the user
Credit: OBS
Modified: User dependent
function: Script Description
type: OBS Core
input type: data
returns: string
----------------------------------------------------------------------------------------------------------------------------------------
]]
function script_description()
debug_log( 'script_description() -- function variable names: ' )
return string.format( desc, tostring( gversion ) );
end
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description: Get the name of this script
Credit: midnight-studios, et al
Modified:
function: regular expression
type: Support
input type: string
returns: string
----------------------------------------------------------------------------------------------------------------------------------------
]]
local function filename()
local str = debug.getinfo(2).source:sub(2);
return str:match("^.*/(.*).lua$") or str;
end
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description: Dumps input to string, if input is a table it returns the expanded table
Credit: et al
Modified: yes
function:
type: Support (debug tool)
input type: variable
returns: string
----------------------------------------------------------------------------------------------------------------------------------------
]]
local function pre_dump( input )
if type( input ) ~= "table" then
return tostring( input )
else
local tbl = {}
for key, value in pairs( input ) do
local keyStr = ( type( key ) ~= "number" ) and "'" .. key .. "'" or tostring( key )
tbl[#tbl + 1] = "[" .. keyStr .. "] = " .. "'" .. pre_dump( value ) .. "'"
end
return "{ " .. table.concat( tbl, ", " ) .. " }"
end
end
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description: Use this to create a Script Log Output used in testing
Credit: et al
Modified: No
function:
type: Support (debug tool)
input type: string
returns: print(string)
----------------------------------------------------------------------------------------------------------------------------------------
]]
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
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description:
Credit:
Modified:
function:
type:
input type:
returns:
----------------------------------------------------------------------------------------------------------------------------------------
]]
function debug_log( content )
if disable_script then return; end;
if not debug_enabled or not enable_property_debug then
return
end
if debug_file == "" then
debug_file = create_debug_file( debug_file_name, content )
else
update_debug_file( debug_file, content )
end
end
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description:
Credit:
Modified:
function:
type:
input type:
returns:
----------------------------------------------------------------------------------------------------------------------------------------
]]
function create_debug_file( input_file_name, content )
if disable_script then return; end;
content = content or string.format( "%s [%s]\n", "Debug Information", os.date("%Y-%m-%d_%H.%M.%S"))
local file_name = string.format( "%s-%s[%s]%s", filename(), input_file_name, os.date("%Y-%m-%d_%H.%M.%S"), ".txt");
-- set output path as the script path by default
local script_path = script_path();
local output_path = script_path .. file_name;
-- if specified output path exists, then set this as the new output path
output_path = script_path .. file_name;
output_path = output_path:gsub( [[\]], "/" );
log( "create_debug_file", output_path )
-- Open file in write mode, this will create the file if it does not exist
local file = io.open( output_path, "w" )
-- If the file has been opened successfully
if file then
-- Write content to the file
file:write( content )
-- Close the file
file:close()
else
-- Print error message
print("Failed to open file " .. file_name .. " for writing")
end
return output_path;
end
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description:
Credit:
Modified:
function:
type:
input type:
returns:
----------------------------------------------------------------------------------------------------------------------------------------
]]
function rewrite_line_debug_file( filename, line_num, content )
if disable_script then return; end;
-- Store all lines in memory
local lines = {}
-- Open the file in read mode
local file = io.open(filename, 'r')
-- If the file has been opened successfully
if file then
-- Loop over all lines in the file
for line in file:lines() do
table.insert(lines, line)
end
-- Close the file
file:close()
else
-- Print error message
print("Failed to open file " .. filename .. " for reading")
end
-- Replace the line at the specified line number
if line_num <= #lines then
lines[line_num] = content
end
-- Open the file in write mode
file = io.open(filename, 'w')
-- If the file has been opened successfully
if file then
for i = 1, #lines do
-- Only add a newline if it's not the last line
if i < #lines then
file:write(lines[i] .. "\n")
else
file:write(lines[i])
end
end
-- Close the file
file:close()
else
-- Print error message
print("Failed to open file " .. filename .. " for writing")
end
end
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description:
Credit:
Modified:
function:
type:
input type:
returns:
----------------------------------------------------------------------------------------------------------------------------------------
]]
function update_debug_file( filename, content )
if disable_script then return; end;
if filename ~= nil then
debug_entry = debug_entry + 1
content = tostring(debug_entry) .. ") " .. string.rep( " ", string.len(debug_entry) ) .. content
-- Open file in append mode
local file = io.open( filename, "a" )
-- If the file has been opened successfully
if file then
-- Write new content on a new line
file:write( "\n" .. content )
-- Close the file
file:close()
else
-- Print error message
print("Failed to open file " .. tostring(filename) .. " for appending. Could not add content: " .. pre_dump(content))
end
-- adds unnecessary processing
--rewrite_line_debug_file( filename, 2, "Last item entered: " .. content )
else
--print("Failed " .. tostring(filename) .. " Does not exist. Could not add content: " .. pre_dump(content))
end
end
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description: Builds a table by splitting a string by defined character or sequence of characters marking
the beginning or end of a unit of data. That which delimits, that separates.
Credit: midnight-studios, et al
Modified:
function: breaks string into sections by a reference that is returned in a table
type:
input type: string, delimiter
returns: table
----------------------------------------------------------------------------------------------------------------------------------------
]]
local function explode_alt( str, delim )
debug_log( 'explode_alt(' .. pre_dump(str) .. ", " .. pre_dump(delim) .. ') -- function variable names: str, delim ' )
local tbl, index;
tbl = {};
index = 0;
if( #str == 1 ) then return {str} end; -- returns a table with the input string as the only value
while true do
local trace_index = string.find( str, delim, index, true ); -- find the next d in the string
if trace_index ~= nil then -- if "not not" found then..
table.insert( tbl, string.sub( str, index, trace_index - 1 ) ); -- Save it in our array.
index = trace_index + 1; -- save just after where we found it for searching next time.
else
table.insert( tbl, string.sub( str, index ) ); -- Save what's left in our array.
break; -- Break at end, as it should be, according to the lua manual.
end;
end;
return tbl;
end
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description: Builds a table by splitting a string by defined character or sequence of characters marking
the beginning or end of a unit of data. That which delimits, that separates.
Credit: midnight-studios, et al
Modified:
function: breaks string into sections by a reference that is returned in a table
type:
input type: string, delimiter
returns: table
----------------------------------------------------------------------------------------------------------------------------------------
]]
local function explode( str, div )
debug_log( 'explode(' .. pre_dump(str) .. ", " .. pre_dump(delim) .. ') -- function variable names: str, delim ' )
if ( div == nil or div == '' ) or ( str == nil or str == '' ) then return {} end
local pos, arr = 0, {}
for st, sp in function() return string.find(str, div, pos, true) end do
table.insert(arr, string.sub(str, pos, st - 1))
pos = sp + 1
end
table.insert(arr, string.sub(str, pos))
return arr
end
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description: Gives you an iterator that moves through an
ordinary table (eg. string keys) but sorted
into key sequence.
It does that by copying the table keys into
a temporary table and sorting that.
Possibly being string referenced the list
will be compiled chronologically, thus the
list names (values) may appear unordered and
random. To reorganise and arrange the list
alphabetically we will use pairsByKeys().
This will make it easier for the user to review
and select the desired item from the list.
Credit: https://github.com/nickgammon/mushclient/blob/master/lua/pairsbykeys.lua
https://github.com/nickgammon/mushclient/tree/master/lua
If you need to sort keys other than strings, see:
See: http://lua-users.org/wiki/SortedIteration
Modified: Yes, minor changes
function: support: This prints the math functions in key order
type: sort table
input type: table, function (optional)
returns: table
----------------------------------------------------------------------------------------------------------------------------------------
]]
local function pairsByKeys( tbl, input_function )
debug_log( 'pairsByKeys(' .. pre_dump( tbl ) .. ", " .. pre_dump( input_function ) .. ')')
if type( tbl ) ~= "table" then return tbl end
-- instead of creating a new table and inserting all items into it, just get the keys
local keys = {}
for k in pairs(tbl) do keys[#keys+1] = k end
table.sort( keys, input_function )
local i = 0
local iter = function ()
i = i + 1
if keys[i] == nil then
return nil
else
-- use keys[i] to get value from tbl
return keys[i], tbl[keys[i]]
end
end
return iter
end
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description:
Credit:
Modified:
function:
type:
input type:
returns:
----------------------------------------------------------------------------------------------------------------------------------------
]]
function reverseTable( tbl )
if type( tbl ) ~= "table" then return nil, "Provided variable is not valid Table"; end
-- Determine the base of the table (0 or 1)
local base = (tbl[0] ~= nil) and 0 or 1
-- Determine the number of elements in the table
local n = base
while tbl[n] ~= nil do
n = n + 1
end
n = n - 1
-- Reverse the table
for i = base, base + math.floor((n - base) / 2) do
tbl[i], tbl[n - i + base] = tbl[n - i + base], tbl[i]
end
end
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description:
Credit:
Modified:
function:
type:
input type:
returns:
----------------------------------------------------------------------------------------------------------------------------------------
]]
function getTableBase( tbl )
if type( tbl ) ~= "table" then return nil, "Provided variable is not valid Table"; end
-- Check if index 0 is non-nil
if tbl[0] ~= nil then
return 0
elseif tbl[1] ~= nil then
return 1
else
return nil, "Table is empty or does not have consecutive integer keys starting from 0 or 1";
end
end
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description:
Credit:
Modified:
function:
type:
input type:
returns:
----------------------------------------------------------------------------------------------------------------------------------------
]]
function tableHasValue( table )
debug_log( 'tableHasValue(' .. pre_dump( table ) .. ') -- function variable names: table ' )
return next(table) ~= nil
end
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description: Provides the length of a table
(how many items the table contains)
Credit: midnight-studios, et al
Modified: Author
function: Create a table with unique items
type: Support
input type: table
returns: integer
----------------------------------------------------------------------------------------------------------------------------------------
]]
local function tablelength( tbl )
debug_log( 'tablelength(' .. pre_dump(tbl) .. ') -- function variable names: tbl ' )
local count = 0;
if type( tbl ) == "table" then -- if the input table is not of type table return 0
for _ in pairs( tbl ) do count = count + 1 end;
end;
return count;
end
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description: Remove duplicated values from table
Credit: midnight-studios, et al
Modified: Author
function: Create a table with unique items
type: Support
input type: table, string
returns: bool
----------------------------------------------------------------------------------------------------------------------------------------
]]
local function tableHasKey( tbl, key )
debug_log( 'tableHasKey(' .. pre_dump(tbl) .. ", " .. pre_dump(key) .. ') -- function variable names: tbl, key ' )
if type( tbl ) ~= "table" or key == nil then return false end; -- if the input table is not of type table return bool(false)
return tbl[key] ~= nil;
end
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description: Remove duplicated values from table
Credit: midnight-studios, et al
Modified: Author
function: Create a table with unique items
type: Support
input type: table, string
returns: bool
----------------------------------------------------------------------------------------------------------------------------------------
]]
local function in_table( tbl, input_value )
debug_log( 'in_table(' .. pre_dump( tbl ) .. ", " .. pre_dump(input_value) .. ') -- function variable names: tbl, input_value ' )
if type( tbl ) ~= "table" or input_value == nil then return false end; -- if the input table is not of type table return bool(false)
for _, value in pairs( tbl ) do
if value == input_value then
return true
end;
end;
return false
end
--[[
----------------------------------------------------------
Description: Function to connect a signal handler and store its information
https://obsproject.com/docs/reference-sources.html?highlight=media_started
attach event listener callback [source_signal]: Called when media has ended.
Credit:
Modified:
function:
type:
input type:
returns:
----------------------------------------------------------
]]
function connectSignalHandler( source, signal, callback )
local sh = obs.obs_source_get_signal_handler( source )
if sh then
local sourceId = obs.obs_source_get_id( source )
-- Check if the signal handler is already connected for this source
if not connectedHandlers[sourceId] then
connectedHandlers[sourceId] = {}
end
local isAlreadyConnected = false
for _, handler in ipairs( connectedHandlers[sourceId] ) do
if handler.signal == signal and handler.callback == callback then
isAlreadyConnected = true
break
end
end
if not isAlreadyConnected then
-- Store the connected signal handler in the table
table.insert( connectedHandlers[sourceId], {signal = signal, callback = callback} )
-- Connect the signal handler
obs.signal_handler_connect( sh, signal, callback )
end
end
end
--[[
----------------------------------------------------------
Description:
Credit:
Modified:
function:
type:
input type:
returns:
----------------------------------------------------------
]]
function refresh_properties()
debug_log( 'refresh_properties() -- function variable names: ' )
return true;
end
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description:
Credit:
Modified:
function:
type:
input type:
returns:
----------------------------------------------------------------------------------------------------------------------------------------
]]
function checkTimeString( str )
debug_log( 'checkTimeString(' .. pre_dump( str ) .. ') -- function variable names: str' )
if str == nil then return false end
-- Pattern to match the format '00:00:00'
local pattern = "^%d%d:%d%d:%d%d$"
-- Check if the string matches the pattern
if string.match( str, pattern ) then
return true
else
return false
end
end
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description:
Credit:
Modified:
function:
type:
input type:
returns:
----------------------------------------------------------------------------------------------------------------------------------------
]]
function removeDuplicates( tbl )
debug_log( 'removeDuplicates(' .. pre_dump( tbl ) .. ') -- function variable names: tbl' )
if type( tbl ) ~= "table" or tbl == nil then return tbl end; -- if the input table is not of type table return input
local seen = {}
local result = {}
for _, value in ipairs(tbl) do
if not seen[value] then
table.insert(result, value)
seen[value] = true
end
end
return result
end
--[[
----------------------------------------------------------------------------------------------------------------------------------------
Description: A function named script_update will be called when settings are changed
Credit:
Modified:
function: Called upon settings initialization and modification
type:
input type:
returns: