Skip to content

Commit

Permalink
Allows selecting group objects as "sources" (#49)
Browse files Browse the repository at this point in the history
- Also fixes race condition with async tasks adding items to dropdowns
  • Loading branch information
RecklessBoon authored Feb 19, 2024
1 parent 87b80d3 commit cd576c4
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 32 deletions.
2 changes: 1 addition & 1 deletion ExtensionManifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"author": "Macro Deck",
"repository": "https://github.com/SuchByte/Macro-Deck-OBS-WebSocket-Plugin",
"packageId": "SuchByte.OBS-WebSocketPlugin",
"version": "2.1.2",
"version": "2.1.3",
"target-plugin-api-version": 40,
"target-macro-deck-version": "2.13.0",
"dll": "OBS-WebSocket Plugin.dll"
Expand Down
93 changes: 68 additions & 25 deletions GUI/SourceVisibilityConfigView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
using SuchByte.OBSWebSocketPlugin.Models.Action;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Windows.Media.Audio;
using Windows.UI.Composition.Scenes;

namespace SuchByte.OBSWebSocketPlugin.GUI
{
Expand Down Expand Up @@ -73,8 +76,20 @@ public override bool OnActionSave()
return true;
}

private CancellationTokenSource LoadingScenesTokenSource;
private TaskCompletionSource LoadingScenes;
private void LoadScenes()
{
if ((LoadingScenes?.Task.IsCompleted ?? true) != true)
{
LoadingScenesTokenSource.Cancel();
try
{
LoadingScenes.Task.Wait(LoadingScenesTokenSource.Token);
}
catch (OperationCanceledException) { /* do nothing */ }
}

var conn = (this as IConnDepConfigs).Conn;
if (conn == null) return;

Expand All @@ -89,28 +104,54 @@ private void LoadScenes()
this.scenesBox.Text = String.Empty;

var self = this;
_ = Task.Run(async () =>
LoadingScenesTokenSource = new CancellationTokenSource();
var task = Task.Run(async () =>
{
var sceneListResponse = await conn.OBS.ScenesRequests.GetSceneListAsync();
if (LoadingScenesTokenSource.Token.IsCancellationRequested) return;
foreach (JObject scene in sceneListResponse.Scenes)
{
var name = scene["sceneName"]?.ToString();
if (!name.Equals(String.Empty))
{
scenesBox.Invoke((MethodInvoker)delegate { scenesBox.Items.Add(name); });
}
var response = await conn.OBS.SceneItemsRequests.GetSceneItemListAsync(name);
foreach (JObject item in response.SceneItems)
{
if (item["isGroup"].Value<bool?>() == true)
{
var groupName = item["sourceName"]?.ToString();
scenesBox.Invoke((MethodInvoker)delegate { scenesBox.Items.Add(groupName); });
}
}
}
self.Invoke((MethodInvoker)delegate
{
scenesBox.Text = config?.SceneName;
LoadSources();
});
});
}, LoadingScenesTokenSource.Token);
LoadingScenes = new TaskCompletionSource(task);
}

private CancellationTokenSource LoadingSourcesTokenSource;
private TaskCompletionSource LoadingSources;
private void LoadSources()
{
if ((LoadingSources?.Task.IsCompleted ?? true) != true)
{
LoadingSourcesTokenSource.Cancel();
try
{
LoadingSources.Task.Wait(LoadingSourcesTokenSource.Token);
} catch (OperationCanceledException) { /* do nothing */ }
}

var conn = (this as IConnDepConfigs).Conn;
if (conn == null) return;

Expand All @@ -126,38 +167,40 @@ private void LoadSources()

var self = this;
var sceneName = scenesBox.Text;
_ = Task.Run(async () =>

LoadingSourcesTokenSource = new CancellationTokenSource();
var task = Task.Run(async () =>
{
var response = await conn.OBS.SceneItemsRequests.GetSceneItemListAsync(sceneName);
if (response != null)
{
await RecurseSourceResponse(conn, response.SceneItems);
}
self.Invoke((MethodInvoker)delegate
{
sourcesBox.Text = config?.SourceName;
});
});
}
private async Task RecurseSourceResponse(Connection conn, JObject[] sceneItems)
{
foreach (JObject item in sceneItems)
{
if (item["isGroup"].Value<bool?>() == true)
if (LoadingSourcesTokenSource.Token.IsCancellationRequested) return;
var sceneItems = response?.SceneItems;
if (response == null)
{
var sub = await conn.OBS.SceneItemsRequests.GetGroupSceneItemListAsync(item["sourceName"]?.ToString());
await RecurseSourceResponse(conn, sub.SceneItems);
var group_response = await conn.OBS.SceneItemsRequests.GetGroupSceneItemListAsync(sceneName);
sceneItems = group_response?.SceneItems;
}
else
if (LoadingSourcesTokenSource.Token.IsCancellationRequested) return;
if ((sceneItems?.Length ?? 0) > 0)
{
var name = item["sourceName"]?.ToString();
if (!String.IsNullOrEmpty(name))
foreach (JObject item in sceneItems)
{
sourcesBox.Invoke((MethodInvoker)delegate { sourcesBox.Items.Add(name); });
var name = item["sourceName"]?.ToString();
if (!String.IsNullOrEmpty(name))
{
sourcesBox.Invoke((MethodInvoker)delegate { sourcesBox.Items.Add(name); });
}
}
}
}
self.Invoke((MethodInvoker)delegate
{
sourcesBox.Text = config?.SourceName;
});
}, LoadingSourcesTokenSource.Token);
LoadingSources = new TaskCompletionSource(task);
}

private void LoadConfig()
Expand Down
2 changes: 1 addition & 1 deletion Macro-Deck-OBS-WebSocket.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Authors>Macro Deck, RecklessBoon</Authors>
<Company>Macro Deck</Company>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Version>2.1.2</Version>
<Version>2.1.3</Version>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<NeutralLanguage>en</NeutralLanguage>
<PackageProjectUrl>https://github.com/RecklessBoon/Macro-Deck-OBS-WebSocket-Plugin</PackageProjectUrl>
Expand Down
19 changes: 14 additions & 5 deletions Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,23 @@ private void OnSceneItemVisibilityChanged(object sender, OBSWebSocket5.Events.Sc
{
if (sender is OBSWebSocket obs)
{
var sceneItems = await obs.SceneItemsRequests.GetSceneItemListAsync(args.SceneName);
var sceneItemName = args.SceneItemId.ToString();
foreach (var item in sceneItems.SceneItems)
var sceneItemsResponse = await obs.SceneItemsRequests.GetSceneItemListAsync(args.SceneName);
var sceneItems = sceneItemsResponse?.SceneItems;
if (sceneItemsResponse == null)
{
if (item["sceneItemId"]!.ToString().Equals(args.SceneItemId.ToString()) && item["sourceName"] != null)
var groupSceneItemsResponse = await obs.SceneItemsRequests.GetGroupSceneItemListAsync(args.SceneName);
sceneItems = groupSceneItemsResponse?.SceneItems;
}
if ((sceneItems?.Length ?? 0) > 0) {
foreach (var item in sceneItems)
{
sceneItemName = item["sourceName"].ToString();
break;
if (item["sceneItemId"]!.ToString().Equals(args.SceneItemId.ToString()) && item["sourceName"] != null)
{
sceneItemName = item["sourceName"].ToString();
break;
}
}
}
connection.SetVariable(args.SceneName + "_" + sceneItemName, args.SceneItemEnabled ? "True" : "False");
Expand Down

0 comments on commit cd576c4

Please sign in to comment.