Add ILogger, GlaxLogger
Renamed variables to match private Added try-catch clauses to websocket requests
This commit is contained in:
parent
5f4af37c34
commit
208e3b7958
@ -1,4 +1,5 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using OBSBlur.Window;
|
||||
using OBSWebsocketDotNet;
|
||||
using OBSWebsocketDotNet.Communication;
|
||||
@ -9,35 +10,37 @@ namespace OBSBlur.OBS;
|
||||
|
||||
public class Blur
|
||||
{
|
||||
WindowManager windowManager = new ();
|
||||
private OBSWebsocket _websocket = new ();
|
||||
private string currentScene;
|
||||
private string[] enabledScenes;
|
||||
private string[] blurPrograms;
|
||||
private Dictionary<IntPtr, uint> windowHandleSceneItems = new();
|
||||
private readonly WindowManager _windowManager = new ();
|
||||
private readonly OBSWebsocket _websocket = new ();
|
||||
private string _currentObsScene = "";
|
||||
private readonly List<string> _enabledObsScenes = new();
|
||||
private readonly List<string> _blurPrograms = new();
|
||||
private readonly Dictionary<IntPtr, uint> _windowHandleSceneItems = new();
|
||||
private readonly ILogger? _logger;
|
||||
|
||||
public Blur(string obsUrl, string obsPassword, string[] enabledScenes, string[] blurPrograms)
|
||||
public Blur(string obsUrl, string obsPassword, string[] enabledObsScenes, string[] blurPrograms, ILogger? logger = null)
|
||||
{
|
||||
this.enabledScenes = enabledScenes;
|
||||
this.blurPrograms = blurPrograms;
|
||||
this._logger = logger;
|
||||
this._enabledObsScenes.AddRange(enabledObsScenes);
|
||||
this._blurPrograms.AddRange(blurPrograms);
|
||||
_websocket.CurrentProgramSceneChanged += WebsocketOnCurrentProgramSceneChanged;
|
||||
_websocket.Connected += WebsocketOnConnected;
|
||||
_websocket.Disconnected += WebsocketOnDisconnected;
|
||||
|
||||
_websocket.ConnectAsync(obsUrl, obsPassword);
|
||||
|
||||
windowManager.WindowsChanged += WindowManagerOnWindowsChanged;
|
||||
windowManager.WindowZOrderChanged += WindowManagerOnWindowZOrderChanged;
|
||||
_windowManager.WindowsChanged += WindowManagerOnWindowsChanged;
|
||||
_windowManager.WindowZOrderChanged += WindowManagerOnWindowZOrderChanged;
|
||||
}
|
||||
|
||||
private void UpdateBlurs()
|
||||
{
|
||||
if (!enabledScenes.Contains(currentScene))
|
||||
if (!_enabledObsScenes.Contains(_currentObsScene))
|
||||
return;
|
||||
if (!_websocket.IsConnected)
|
||||
return;
|
||||
WindowInfo[] windowInfos = windowManager.WindowInfos;
|
||||
IntPtr[] zOrder = windowManager.WindowZOrder;
|
||||
WindowInfo[] windowInfos = _windowManager.WindowInfos;
|
||||
IntPtr[] zOrder = _windowManager.WindowZOrder;
|
||||
|
||||
bool maximixedWindowReached = false;
|
||||
|
||||
@ -57,22 +60,22 @@ public class Blur
|
||||
maximixedWindowReached = true;
|
||||
}
|
||||
|
||||
foreach(IntPtr blurredWindow in windowHandleSceneItems.Keys.ToArray())
|
||||
foreach(IntPtr blurredWindow in _windowHandleSceneItems.Keys.ToArray())
|
||||
if(windowInfos.All(w => w.WindowHandle != blurredWindow))
|
||||
DeleteBlur(blurredWindow);
|
||||
}
|
||||
|
||||
private bool GetBlurWindow(WindowInfo windowInfo)
|
||||
{
|
||||
foreach(string program in blurPrograms)
|
||||
if (windowInfo.ProcessInfo.ProcessName.Contains(program))
|
||||
foreach(string program in _blurPrograms)
|
||||
if (windowInfo.ProcessInfo.ProcessName.Contains(program, StringComparison.OrdinalIgnoreCase))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
private void BlurWindow(WindowInfo windowInfo)
|
||||
{
|
||||
if(windowHandleSceneItems.ContainsKey(windowInfo.WindowHandle))
|
||||
if(_windowHandleSceneItems.ContainsKey(windowInfo.WindowHandle))
|
||||
MoveBlur(windowInfo);
|
||||
else
|
||||
AddBlur(windowInfo);
|
||||
@ -80,27 +83,32 @@ public class Blur
|
||||
|
||||
private void AddBlur(WindowInfo windowInfo)
|
||||
{
|
||||
Dictionary<string, object> request = new()
|
||||
Dictionary<string, object> duplicateSceneItemRequest = new()
|
||||
{
|
||||
{"sceneName", currentScene},
|
||||
{"sceneName", _currentObsScene},
|
||||
{"sceneItemId", GetBlurSource()}
|
||||
};
|
||||
try
|
||||
{
|
||||
JObject response = _websocket.SendRequest("DuplicateSceneItem", JObject.FromObject(request));
|
||||
windowHandleSceneItems.Add(windowInfo.WindowHandle, response["sceneItemId"]!.Value<uint>());
|
||||
JObject response = _websocket.SendRequest("DuplicateSceneItem", JObject.FromObject(duplicateSceneItemRequest));
|
||||
if (!response.ContainsKey("sceneItemId"))
|
||||
{
|
||||
_logger?.LogWarning("Request DuplicateSceneItem: Response did not include 'sceneItemId'.");
|
||||
return;
|
||||
}
|
||||
_windowHandleSceneItems.Add(windowInfo.WindowHandle, response["sceneItemId"]!.Value<uint>());
|
||||
MoveBlur(windowInfo);
|
||||
}
|
||||
catch (ErrorResponseException e)
|
||||
{
|
||||
Console.WriteLine("No Scene Item with name 'Blur' found.");
|
||||
_logger?.LogError(e, "Request DuplicateSceneItem");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private void MoveBlur(WindowInfo windowInfo)
|
||||
{
|
||||
if (!windowHandleSceneItems.ContainsKey(windowInfo.WindowHandle))
|
||||
if (!_windowHandleSceneItems.ContainsKey(windowInfo.WindowHandle))
|
||||
return;
|
||||
|
||||
SceneItemTransformInfo info = new()
|
||||
@ -113,35 +121,59 @@ public class Blur
|
||||
Alignnment = 5
|
||||
};
|
||||
|
||||
Dictionary<string, object> request = new()
|
||||
Dictionary<string, object> setSceneItemTransformRequest = new()
|
||||
{
|
||||
{"sceneName", currentScene},
|
||||
{"sceneItemId", windowHandleSceneItems[windowInfo.WindowHandle]},
|
||||
{"sceneName", _currentObsScene},
|
||||
{"sceneItemId", _windowHandleSceneItems[windowInfo.WindowHandle]},
|
||||
{"sceneItemTransform", info}
|
||||
};
|
||||
_websocket.SendRequest("SetSceneItemTransform", JObject.FromObject(request));
|
||||
|
||||
|
||||
Dictionary<string, object> request2 = new()
|
||||
try
|
||||
{
|
||||
{"sceneName", currentScene},
|
||||
{"sceneItemId", windowHandleSceneItems[windowInfo.WindowHandle]},
|
||||
_websocket.SendRequest("SetSceneItemTransform", JObject.FromObject(setSceneItemTransformRequest));
|
||||
}
|
||||
catch (ErrorResponseException e)
|
||||
{
|
||||
_logger?.LogError(e, "Request SetSceneItemTransform");
|
||||
throw;
|
||||
}
|
||||
|
||||
|
||||
Dictionary<string, object> setSceneItemEnabledRequest = new()
|
||||
{
|
||||
{"sceneName", _currentObsScene},
|
||||
{"sceneItemId", _windowHandleSceneItems[windowInfo.WindowHandle]},
|
||||
{"sceneItemEnabled", true}
|
||||
};
|
||||
_websocket.SendRequest("SetSceneItemEnabled", JObject.FromObject(request2));
|
||||
try
|
||||
{
|
||||
_websocket.SendRequest("SetSceneItemEnabled", JObject.FromObject(setSceneItemEnabledRequest));
|
||||
}
|
||||
catch (ErrorResponseException e)
|
||||
{
|
||||
_logger?.LogError(e, "Request SetSceneItemEnabled");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private void DeleteBlur(IntPtr windowHandle)
|
||||
{
|
||||
if (!windowHandleSceneItems.ContainsKey(windowHandle))
|
||||
if (!_windowHandleSceneItems.TryGetValue(windowHandle, out uint sceneItemId))
|
||||
return;
|
||||
Dictionary<string, object> request = new()
|
||||
Dictionary<string, object> removeSceneItemRequest = new()
|
||||
{
|
||||
{"sceneName", currentScene},
|
||||
{"sceneItemId", windowHandleSceneItems[windowHandle]}
|
||||
{"sceneName", _currentObsScene},
|
||||
{"sceneItemId", sceneItemId}
|
||||
};
|
||||
_websocket.SendRequest("RemoveSceneItem", JObject.FromObject(request));
|
||||
windowHandleSceneItems.Remove(windowHandle);
|
||||
try
|
||||
{
|
||||
_websocket.SendRequest("RemoveSceneItem", JObject.FromObject(removeSceneItemRequest));
|
||||
_windowHandleSceneItems.Remove(windowHandle);
|
||||
}
|
||||
catch (ErrorResponseException e)
|
||||
{
|
||||
_logger?.LogError(e, "Request RemoveSceneItem");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private void DeleteBlur(WindowInfo windowInfo)
|
||||
@ -153,17 +185,19 @@ public class Blur
|
||||
{
|
||||
Dictionary<string, string> request = new()
|
||||
{
|
||||
{"sceneName", currentScene},
|
||||
{"sceneName", _currentObsScene},
|
||||
{"sourceName", "Blur"}
|
||||
};
|
||||
try
|
||||
{
|
||||
JObject response = _websocket.SendRequest("GetSceneItemId", JObject.FromObject(request));
|
||||
if (!response.ContainsKey("sceneItemId"))
|
||||
throw new KeyNotFoundException("sceneItemId not in response.");
|
||||
return response["sceneItemId"]!.Value<uint>();
|
||||
}
|
||||
catch (ErrorResponseException e)
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine("No Scene Item with name 'Blur' found.");
|
||||
_logger?.LogError(e, "Request GetSceneItemId");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
@ -172,47 +206,44 @@ public class Blur
|
||||
{
|
||||
|
||||
uint i = 0;
|
||||
string prnt = "";
|
||||
string prnt = $"Z-order changed\n{"Z",3} | {"hWnd",8} | {"PID",8} | {"Name",17} | {"Window Title",-17} | {"Vis",-8} | {"State",-13} | BBox\n";
|
||||
foreach (IntPtr windowHandle in neworder)
|
||||
{
|
||||
WindowInfo windowInfo = windowManager.WindowInfos.FirstOrDefault(w => w.WindowHandle == windowHandle);
|
||||
WindowInfo windowInfo = _windowManager.WindowInfos.FirstOrDefault(w => w.WindowHandle == windowHandle);
|
||||
if (windowInfo.WindowHandle is 0x0)
|
||||
continue;
|
||||
prnt += $"{++i,3} | {windowInfo}\n";
|
||||
}
|
||||
|
||||
Console.WriteLine($"Z-order changed {DateTime.UtcNow:O}");
|
||||
Console.WriteLine($"{"Z",3} | {"hWnd",8} | {"PID",8} | {"Name",17} | {"Window Title",-17} | {"Vis",-8} | {"State",-13} | Rectangle");
|
||||
Console.WriteLine(prnt);
|
||||
|
||||
_logger?.LogInformation(prnt);
|
||||
UpdateBlurs();
|
||||
}
|
||||
|
||||
private void WindowManagerOnWindowsChanged(WindowInfo[] before, WindowInfo[] after)
|
||||
{
|
||||
string prnt = "";
|
||||
string prnt = "Window changed\n";
|
||||
foreach (WindowInfo windowInfo in after)
|
||||
prnt += $"{windowInfo}\n";
|
||||
Console.WriteLine($"Windows changed {DateTime.UtcNow:O}");
|
||||
Console.WriteLine(prnt);
|
||||
_logger?.LogInformation(prnt);
|
||||
UpdateBlurs();
|
||||
}
|
||||
|
||||
private void WebsocketOnDisconnected(object? sender, ObsDisconnectionInfo e)
|
||||
{
|
||||
Console.WriteLine($"Obs Disconnected {DateTime.UtcNow:O}");
|
||||
_logger?.LogInformation("Obs Disconnected");
|
||||
}
|
||||
|
||||
private void WebsocketOnConnected(object? sender, EventArgs e)
|
||||
{
|
||||
currentScene = _websocket.GetCurrentProgramScene();
|
||||
Console.WriteLine($"Obs Connected. Current Scene {currentScene} {DateTime.UtcNow:O}");
|
||||
_currentObsScene = _websocket.GetCurrentProgramScene();
|
||||
_logger?.LogInformation($"Obs Connected. Current Scene {_currentObsScene}");
|
||||
UpdateBlurs();
|
||||
}
|
||||
|
||||
private void WebsocketOnCurrentProgramSceneChanged(object? sender, ProgramSceneChangedEventArgs e)
|
||||
{
|
||||
currentScene = e.SceneName;
|
||||
Console.WriteLine($"Obs Scene Changed -> {currentScene} {DateTime.UtcNow:O}");
|
||||
_currentObsScene = e.SceneName;
|
||||
_logger?.LogInformation($"Obs Scene Changed -> {_currentObsScene}");
|
||||
UpdateBlurs();
|
||||
}
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="GlaxLogger" Version="1.0.7.2" />
|
||||
<PackageReference Include="obs-websocket-dotnet" Version="5.0.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
|
@ -1,8 +1,9 @@
|
||||
using OBSBlur.OBS;
|
||||
using GlaxLogger;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using OBSBlur.OBS;
|
||||
|
||||
|
||||
|
||||
Blur _ = new ("ws://localhost:4444", "", new []{"Desktop"}, new []{ "Discord", "VRCX" });
|
||||
Logger logger = new (filteredLevel: LogLevel.Trace, consoleOut: Console.Out);
|
||||
Blur _ = new ("ws://localhost:4444", "", new []{"Desktop"}, new []{ "Discord", "VRCX", "thunderbird", "Signal" }, logger);
|
||||
|
||||
while (true)
|
||||
Thread.Sleep(100);
|
Loading…
x
Reference in New Issue
Block a user