@@ -71,6 +71,8 @@ local use_socket = false
7171local socket_port = 0
7272local socket_poll = 1000
7373local debug_logs = false
74+ local is_obs_loaded = false
75+ local is_script_loaded = false
7476
7577local ZoomState = {
7678 None = 0 ,
@@ -81,7 +83,9 @@ local ZoomState = {
8183local zoom_state = ZoomState .None
8284
8385local 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
8589
8690-- Define the mouse cursor functions for each platform
8791if ffi .os == " Windows" then
@@ -601,12 +605,14 @@ function refresh_sceneitem(find_newest)
601605 end
602606
603607 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 )
608611 source_width = monitor_info .width
609612 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" )
610616 end
611617 else
612618 log (" Using source size: " .. source_width .. " , " .. source_height )
@@ -1034,6 +1040,7 @@ end
10341040function stop_server ()
10351041 if socket_server ~= nil then
10361042 log (" Socket server stopped" )
1043+ obs .timer_remove (on_socket_timer )
10371044 socket_server :close ()
10381045 socket_server = nil
10391046 socket_mouse = nil
@@ -1061,16 +1068,34 @@ end
10611068
10621069function on_frontend_event (event )
10631070 if event == obs .OBS_FRONTEND_EVENT_SCENE_CHANGED then
1064- log (" Scene changed" )
1071+ log (" OBS Scene changed" )
10651072 -- If the scene changes we attempt to find a new source with the same name in this new scene
10661073 -- 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 )
10671083 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
10681090 end
10691091end
10701092
10711093function on_update_transform ()
10721094 -- 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+
10741099 return true
10751100end
10761101
@@ -1136,7 +1161,8 @@ function log_current_settings()
11361161 debug_logs = debug_logs
11371162 }
11381163
1139- log (" OBS Version: " .. string.format (" %.1f" , major ))
1164+ log (" OBS Version: " .. string.format (" %.1f" , major ) .. " ." .. minor )
1165+ log (" Platform: " .. ffi .os )
11401166 log (" Current settings:" )
11411167 log (format_table (settings ))
11421168end
@@ -1362,11 +1388,14 @@ function script_load(settings)
13621388
13631389 source_name = " "
13641390 use_socket = false
1391+ is_script_loaded = true
13651392end
13661393
13671394function script_unload ()
1395+ is_script_loaded = false
1396+
13681397 -- 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
13701399 local transitions = obs .obs_frontend_get_transitions ()
13711400 if transitions ~= nil then
13721401 for i , s in pairs (transitions ) do
@@ -1447,6 +1476,7 @@ function script_update(settings)
14471476 local old_dh = monitor_override_dh
14481477 local old_socket = use_socket
14491478 local old_port = socket_port
1479+ local old_poll = socket_poll
14501480
14511481 -- Update the settings
14521482 source_name = obs .obs_data_get_string (settings , " source" )
@@ -1474,7 +1504,7 @@ function script_update(settings)
14741504 debug_logs = obs .obs_data_get_bool (settings , " debug_logs" )
14751505
14761506 -- 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
14781508 refresh_sceneitem (true )
14791509 end
14801510
@@ -1489,7 +1519,9 @@ function script_update(settings)
14891519 monitor_override_sy ~= old_sy or
14901520 monitor_override_w ~= old_dw or
14911521 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
14931525 end
14941526
14951527 if old_socket ~= use_socket then
@@ -1498,6 +1530,9 @@ function script_update(settings)
14981530 else
14991531 stop_server ()
15001532 end
1533+ elseif use_socket and (old_poll ~= socket_poll or old_port ~= socket_port ) then
1534+ stop_server ()
1535+ start_server ()
15011536 end
15021537end
15031538
@@ -1518,4 +1553,4 @@ function populate_zoom_sources(list)
15181553
15191554 obs .source_list_release (sources )
15201555 end
1521- end
1556+ end
0 commit comments