Skip to content

Commit

Permalink
Refactor Code (#12)
Browse files Browse the repository at this point in the history
* Added run scripts

* Removed obsolete "new" keyword

* Removed some obsolete code

* Added comment for possible bug when compiling using NativeAOT

* Fixed invalid null return on non-nullable type

* Removed redundant disposal

* Fixed namespace

* Refactored class

* Refactored class

* Refactored class

* Fix possible NullPointerException

* Removed redundant suppression expressions

* Refactored class

* Fixed typos

* Renamed LogLevel fields

* Refactored class

* Refactored class

* Disabled unused field warning
  • Loading branch information
parzival-space authored Aug 9, 2024
1 parent e831338 commit aeb5e21
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 73 deletions.
17 changes: 17 additions & 0 deletions .run/Build & Install - Linux.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Build &amp; Install - Linux" type="ShConfigurationType">
<option name="SCRIPT_TEXT" value="" />
<option name="INDEPENDENT_SCRIPT_PATH" value="true" />
<option name="SCRIPT_PATH" value="./build.sh" />
<option name="SCRIPT_OPTIONS" value="" />
<option name="INDEPENDENT_SCRIPT_WORKING_DIRECTORY" value="true" />
<option name="SCRIPT_WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="INDEPENDENT_INTERPRETER_PATH" value="true" />
<option name="INTERPRETER_PATH" value="/bin/bash" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="EXECUTE_IN_TERMINAL" value="true" />
<option name="EXECUTE_SCRIPT_FILE" value="true" />
<envs />
<method v="2" />
</configuration>
</component>
17 changes: 17 additions & 0 deletions .run/Build & Install - Windows.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Build &amp; Install - Windows" type="ShConfigurationType">
<option name="SCRIPT_TEXT" value="" />
<option name="INDEPENDENT_SCRIPT_PATH" value="false" />
<option name="SCRIPT_PATH" value="./build.ps1" />
<option name="SCRIPT_OPTIONS" value="" />
<option name="INDEPENDENT_SCRIPT_WORKING_DIRECTORY" value="true" />
<option name="SCRIPT_WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="INDEPENDENT_INTERPRETER_PATH" value="true" />
<option name="INTERPRETER_PATH" value="powershell.exe" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="EXECUTE_IN_TERMINAL" value="true" />
<option name="EXECUTE_SCRIPT_FILE" value="true" />
<envs />
<method v="2" />
</configuration>
</component>
6 changes: 3 additions & 3 deletions FaderSyncPlugin/GoXLR/UtilitySingleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public static class UtilitySingleton
private static readonly Logger Log = new Logger(typeof(Plugin), Module.Name);

private static Utility? _utility;
private static Thread? _connectionThread = null;
private static Thread? _connectionThread;

private static async void Connect()
{
Expand All @@ -17,7 +17,7 @@ private static async void Connect()

if (_utility.IsConnectionAlive())
{
Log.Info($"Connected to GoXLR Utility v{_utility.Status?["config"]?["daemon_version"]}");
Log.Info($"Connected to GoXLR Utility v{_utility.Status["config"]?["daemon_version"]}");
}
else
{
Expand All @@ -30,7 +30,7 @@ public static Utility GetInstance()
_utility ??= new Utility();
if (_utility.IsConnectionAlive()) return _utility;

_utility.OnException += (sender, exception) =>
_utility.OnException += (_, exception) =>
{
Log.Error($"Something internally went wrong in the GoXLR Utility API Client: {exception.Message}");
};
Expand Down
6 changes: 3 additions & 3 deletions FaderSyncPlugin/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace FaderSync;

public static class Module
{
public static readonly string Name = Assembly.GetExecutingAssembly()?.GetName()?.Name!;
public static readonly string Name = Assembly.GetExecutingAssembly().GetName().Name!;

public static readonly string Version = Assembly.GetExecutingAssembly()?
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion!
public static readonly string Version = Assembly.GetExecutingAssembly()
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion
.Split('+')[0]!; // remove commit hash
}
28 changes: 15 additions & 13 deletions FaderSyncPlugin/OBS/GoXlrChannelSyncFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private static unsafe void Tick(void* data, float seconds)
var channelName = Marshal.PtrToStringUTF8((IntPtr)context->ChannelName);

var target = Obs.obs_filter_get_parent(context->Source);
var systemVolume = utility.Status?["mixers"]?[deviceSerial ?? ""]?["levels"]?["volumes"]?[channelName ?? ""]?
var systemVolume = utility.Status["mixers"]?[deviceSerial ?? ""]?["levels"]?["volumes"]?[channelName ?? ""]?
.GetValue<int>() ?? 0;

// Ok, the GoXLR seems to decrease the volume by 1dB for every (on average) 4.85 volume steps, it
Expand All @@ -106,12 +106,12 @@ private static unsafe void Tick(void* data, float seconds)
utilityBase += count * 0.115f;
}

// Now we convert this into a OBS value...
// Now we convert this into an OBS value...
var obsVolume = (float)Math.Pow(10, -utilityBase / 20f);

// check if channel is muted
var isMuted = false;
var faderStatus = (JsonObject)utility.Status?["mixers"]?[deviceSerial ?? ""]?["fader_status"];
var faderStatus = (JsonObject?)utility.Status["mixers"]?[deviceSerial ?? ""]?["fader_status"];
if (faderStatus != null)
foreach (var faderEntry in faderStatus)
{
Expand All @@ -134,7 +134,7 @@ private static unsafe void Tick(void* data, float seconds)
[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
private static unsafe void GetDefaults(obs_data* settings)
{
// Todo: implement
// do nothing
}

[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
Expand Down Expand Up @@ -186,7 +186,7 @@ private static unsafe void GetDefaults(obs_data* settings)
sChannelLineOutId = "LineOut"u8.ToArray()
)
{
// Create the Serial Dropdown..
// Create the Serial Dropdown...
var deviceList = ObsProperties.obs_properties_add_list(properties, (sbyte*)sDeviceSerialId, (sbyte*)sDeviceSerialDescription,
obs_combo_type.OBS_COMBO_TYPE_LIST, obs_combo_format.OBS_COMBO_FORMAT_STRING);

Expand All @@ -206,9 +206,9 @@ private static unsafe void GetDefaults(obs_data* settings)
ObsProperties.obs_property_list_add_string(channelList, (sbyte*)sChannelMicMonitor, (sbyte*)sChannelMicMonitorId);
ObsProperties.obs_property_list_add_string(channelList, (sbyte*)sChannelLineOut, (sbyte*)sChannelLineOutId);

// Before we Proceed, we need to fetch a list of the available GoXLRs on the System..
// Before we Proceed, we need to fetch a list of the available GoXLRs on the System...
var utility = UtilitySingleton.GetInstance();
var mixers = (JsonObject)utility.Status?["mixers"];
var mixers = (JsonObject?)utility.Status["mixers"];
var locatedDevices = new ArrayList();
var forcedSerial = false;

Expand All @@ -222,22 +222,22 @@ private static unsafe void GetDefaults(obs_data* settings)
// Get an initial count of devices which we'll use for stuff later!
var locatedDeviceCount = locatedDevices.Count;

// If the user has perviously configured a GoXLR but it's not currently attached to the Utility, we need to
// If the user has previously configured a GoXLR, but it's not currently attached to the Utility, we need to
// force the serial into the list to prevent arbitrary device switching later on. We'll also flag this as a
// forced entry so we can appropriately label it.
// forced entry, so we can appropriately label it.
if (serial != "" && !locatedDevices.Contains(serial)) {
locatedDevices.Add(serial);
forcedSerial = true;
}

if (locatedDevices.Count == 0) {
// We're in some kind of error state. Either the utility connection is broken or there are no GoXLRs attached, and the
// user hasn't previously defined a GoXLR. In this case we'll forcably add the 'Error' serial to the list so we can
// user hasn't previously defined a GoXLR. In this case we'll forcibly add the 'Error' serial to the list, so we can
// display the problem to the user in the drop-down.
locatedDevices.Add(deviceSerialError);
}

// Start filling out the list..
// Start filling out the list...
foreach (var located in locatedDevices) {
fixed (byte* sSerial = Encoding.UTF8.GetBytes((string)located)) {
if (located.Equals(deviceSerialError) && mixers == null) {
Expand All @@ -254,8 +254,8 @@ private static unsafe void GetDefaults(obs_data* settings)

// Has this device been forced into the located list due to it being disconnected?
if (forcedSerial && located.Equals(serial)) {
// We can do a *LOT* better than this and potentially check WHY it's disconnected..
title = String.Format("{0} - Disconnected", located);
// We can do a *LOT* better than this and potentially check WHY it's disconnected...
title = $"{located} - Disconnected";
}
fixed(byte* sTitle = Encoding.UTF8.GetBytes(title)) {
ObsProperties.obs_property_list_add_string(deviceList, (sbyte*)sTitle, (sbyte*)sSerial);
Expand Down Expand Up @@ -283,6 +283,7 @@ public static unsafe void Update(void* data, obs_data* settings)
}
}

#pragma warning disable CS0649 // Field is never assigned to, and will always have its default value
private unsafe struct FilterContext
{
public obs_source* Source;
Expand All @@ -292,4 +293,5 @@ private unsafe struct FilterContext
public sbyte* DeviceSerial;
public sbyte* ChannelName;
}
#pragma warning restore CS0649 // Field is never assigned to, and will always have its default value
}
16 changes: 8 additions & 8 deletions FaderSyncPlugin/OBS/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ public class Logger

public enum LogLevel
{
ERROR = ObsBase.LOG_ERROR,
WARNING = ObsBase.LOG_WARNING,
INFO = ObsBase.LOG_INFO,
DEBUG = ObsBase.LOG_DEBUG,
Error = ObsBase.LOG_ERROR,
Warning = ObsBase.LOG_WARNING,
Info = ObsBase.LOG_INFO,
Debug = ObsBase.LOG_DEBUG,
}

private static unsafe void Log(LogLevel level, string text)
Expand All @@ -40,8 +40,8 @@ public Logger(MemberInfo loggerClass)
_className = loggerClass.Name;
}

public void Error(string message) => Log(LogLevel.ERROR, $"<{_className}> {message}");
public void Warning(string message) => Log(LogLevel.WARNING, $"<{_className}> {message}");
public void Info(string message) => Log(LogLevel.INFO, $"<{_className}> {message}");
public void Debug(string message) => Log(LogLevel.DEBUG, $"<{_className}> {message}");
public void Error(string message) => Log(LogLevel.Error, $"<{_className}> {message}");
public void Warning(string message) => Log(LogLevel.Warning, $"<{_className}> {message}");
public void Info(string message) => Log(LogLevel.Info, $"<{_className}> {message}");
public void Debug(string message) => Log(LogLevel.Debug, $"<{_className}> {message}");
}
13 changes: 6 additions & 7 deletions FaderSyncPlugin/OBS/Plugin.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices;
using FaderSync.GoXLR;
using ObsInterop;

Expand All @@ -13,7 +12,7 @@ public static class Plugin
CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })]
public static unsafe void obs_module_set_pointer(obs_module* obsModulePointer)
{

// do nothing, needs to exist for OBS to load
}

[UnmanagedCallersOnly(EntryPoint = "obs_module_ver",
Expand All @@ -29,7 +28,7 @@ public static uint obs_module_ver()

[UnmanagedCallersOnly(EntryPoint = "obs_module_load",
CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })]
public static unsafe bool obs_module_load()
public static bool obs_module_load()
{
Log.Info($"Loading {Module.Name} v{Module.Version}");

Expand Down Expand Up @@ -62,14 +61,14 @@ public static void obs_module_unload()
CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })]
public static unsafe void obs_module_set_locale(char* locale)
{

// TODO: add locale support
}

[UnmanagedCallersOnly(EntryPoint = "obs_module_free_locale",
CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })]
public static unsafe void obs_module_free_locale()
public static void obs_module_free_locale()
{

// do nothing, needs to exist for OBS to load
}
}
}
5 changes: 2 additions & 3 deletions UtilityClient/Native/SocketClient.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.IO.Pipes;
using System.Text;

namespace GoXLRUtilityClient.client;
namespace GoXLRUtilityClient.Native;

public class SocketClient : IDisposable
{
Expand Down Expand Up @@ -41,7 +41,7 @@ public string ReadMessage()
// read message length
byte[] lengthBytes;
try { lengthBytes = _reader.ReadBytes(4); }
catch (IOException) { return null; }
catch (IOException) { return ""; }

if (BitConverter.IsLittleEndian) Array.Reverse(lengthBytes);
var messageLength = BitConverter.ToUInt32(lengthBytes);
Expand All @@ -65,6 +65,5 @@ public void Dispose()

_reader.Dispose();
_writer.Dispose();
_client.Dispose();
}
}
Loading

0 comments on commit aeb5e21

Please sign in to comment.