@@ -71,6 +71,8 @@ local use_socket = false
71
71
local socket_port = 0
72
72
local socket_poll = 1000
73
73
local debug_logs = false
74
+ local is_obs_loaded = false
75
+ local is_script_loaded = false
74
76
75
77
local ZoomState = {
76
78
None = 0 ,
@@ -81,7 +83,9 @@ local ZoomState = {
81
83
local zoom_state = ZoomState .None
82
84
83
85
local version = obs .obs_get_version_string ()
84
- local major = tonumber (version :match (" (%d+%.%d+)" )) or 0
86
+ local m1 , m2 = version :match (" (%d+%.%d+)%.(%d+)" )
87
+ local major = tonumber (m1 ) or 0
88
+ local minor = tonumber (m2 ) or 0
85
89
86
90
-- Define the mouse cursor functions for each platform
87
91
if ffi .os == " Windows" then
@@ -601,12 +605,14 @@ function refresh_sceneitem(find_newest)
601
605
end
602
606
603
607
if source_width == 0 or source_height == 0 then
604
- log (" ERROR: Something went wrong determining source size." ..
605
- " Try using the 'Set manual source position' option and adding override values" )
606
-
607
- if monitor_info ~= nil then
608
+ if monitor_info ~= nil and monitor_info .width > 0 and monitor_info .height > 0 then
609
+ log (" WARNING: Something went wrong determining source size.\n " ..
610
+ " Using source size from info: " .. monitor_info .width .. " , " .. monitor_info .height )
608
611
source_width = monitor_info .width
609
612
source_height = monitor_info .height
613
+ else
614
+ log (" ERROR: Something went wrong determining source size.\n " ..
615
+ " Try using the 'Set manual source position' option and adding override values" )
610
616
end
611
617
else
612
618
log (" Using source size: " .. source_width .. " , " .. source_height )
@@ -1034,6 +1040,7 @@ end
1034
1040
function stop_server ()
1035
1041
if socket_server ~= nil then
1036
1042
log (" Socket server stopped" )
1043
+ obs .timer_remove (on_socket_timer )
1037
1044
socket_server :close ()
1038
1045
socket_server = nil
1039
1046
socket_mouse = nil
@@ -1061,16 +1068,34 @@ end
1061
1068
1062
1069
function on_frontend_event (event )
1063
1070
if event == obs .OBS_FRONTEND_EVENT_SCENE_CHANGED then
1064
- log (" Scene changed" )
1071
+ log (" OBS Scene changed" )
1065
1072
-- If the scene changes we attempt to find a new source with the same name in this new scene
1066
1073
-- TODO: There probably needs to be a way for users to specify what source they want to use in each scene
1074
+ -- Scene change can happen before OBS has completely loaded, so we check for that here
1075
+ if is_obs_loaded then
1076
+ refresh_sceneitem (true )
1077
+ end
1078
+ elseif event == obs .OBS_FRONTEND_EVENT_FINISHED_LOADING then
1079
+ log (" OBS Loaded" )
1080
+ -- Once loaded we perform our initial lookup
1081
+ is_obs_loaded = true
1082
+ monitor_info = get_monitor_info (source )
1067
1083
refresh_sceneitem (true )
1084
+ elseif event == obs .OBS_FRONTEND_EVENT_SCRIPTING_SHUTDOWN then
1085
+ log (" OBS Shutting down" )
1086
+ -- Add a fail-safe for unloading the script during shutdown
1087
+ if is_script_loaded then
1088
+ script_unload ()
1089
+ end
1068
1090
end
1069
1091
end
1070
1092
1071
1093
function on_update_transform ()
1072
1094
-- Update the crop/size settings based on whatever the source in the current scene looks like
1073
- refresh_sceneitem (true )
1095
+ if is_obs_loaded then
1096
+ refresh_sceneitem (true )
1097
+ end
1098
+
1074
1099
return true
1075
1100
end
1076
1101
@@ -1136,7 +1161,8 @@ function log_current_settings()
1136
1161
debug_logs = debug_logs
1137
1162
}
1138
1163
1139
- log (" OBS Version: " .. string.format (" %.1f" , major ))
1164
+ log (" OBS Version: " .. string.format (" %.1f" , major ) .. " ." .. minor )
1165
+ log (" Platform: " .. ffi .os )
1140
1166
log (" Current settings:" )
1141
1167
log (format_table (settings ))
1142
1168
end
@@ -1362,11 +1388,14 @@ function script_load(settings)
1362
1388
1363
1389
source_name = " "
1364
1390
use_socket = false
1391
+ is_script_loaded = true
1365
1392
end
1366
1393
1367
1394
function script_unload ()
1395
+ is_script_loaded = false
1396
+
1368
1397
-- Clean up the memory usage
1369
- if major > 29.0 then -- 29.0 seems to crash if you do this, so we ignore it as the script is closing anyway
1398
+ if major > 29.1 or ( major == 29.1 and minor > 2 ) then -- 29.1.2 and below seems to crash if you do this, so we ignore it as the script is closing anyway
1370
1399
local transitions = obs .obs_frontend_get_transitions ()
1371
1400
if transitions ~= nil then
1372
1401
for i , s in pairs (transitions ) do
@@ -1447,6 +1476,7 @@ function script_update(settings)
1447
1476
local old_dh = monitor_override_dh
1448
1477
local old_socket = use_socket
1449
1478
local old_port = socket_port
1479
+ local old_poll = socket_poll
1450
1480
1451
1481
-- Update the settings
1452
1482
source_name = obs .obs_data_get_string (settings , " source" )
@@ -1474,7 +1504,7 @@ function script_update(settings)
1474
1504
debug_logs = obs .obs_data_get_bool (settings , " debug_logs" )
1475
1505
1476
1506
-- Only do the expensive refresh if the user selected a new source
1477
- if source_name ~= old_source_name then
1507
+ if source_name ~= old_source_name and is_obs_loaded then
1478
1508
refresh_sceneitem (true )
1479
1509
end
1480
1510
@@ -1489,7 +1519,9 @@ function script_update(settings)
1489
1519
monitor_override_sy ~= old_sy or
1490
1520
monitor_override_w ~= old_dw or
1491
1521
monitor_override_h ~= old_dh then
1492
- monitor_info = get_monitor_info (source )
1522
+ if is_obs_loaded then
1523
+ monitor_info = get_monitor_info (source )
1524
+ end
1493
1525
end
1494
1526
1495
1527
if old_socket ~= use_socket then
@@ -1498,6 +1530,9 @@ function script_update(settings)
1498
1530
else
1499
1531
stop_server ()
1500
1532
end
1533
+ elseif use_socket and (old_poll ~= socket_poll or old_port ~= socket_port ) then
1534
+ stop_server ()
1535
+ start_server ()
1501
1536
end
1502
1537
end
1503
1538
@@ -1518,4 +1553,4 @@ function populate_zoom_sources(list)
1518
1553
1519
1554
obs .source_list_release (sources )
1520
1555
end
1521
- end
1556
+ end
0 commit comments