Skip to content

Commit

Permalink
Classlibrarycommon & ControlReference code cleanup resharper (#452)
Browse files Browse the repository at this point in the history
* ClassLibraryCommon code cleanup resharper

* ControlReference code cleanup
  • Loading branch information
jdahlblom authored Jan 31, 2024
1 parent 0bb571e commit 5aafe7a
Show file tree
Hide file tree
Showing 35 changed files with 303 additions and 323 deletions.
82 changes: 35 additions & 47 deletions Source/ClassLibraryCommon/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public enum EmulationMode
public static class Common
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private static int _emulationModesFlag = 0;
private static int _emulationModesFlag;


/// <summary>
Expand Down Expand Up @@ -111,21 +111,16 @@ public static string GetHex(int i, bool includePrefix = true, bool lowercase = t

public static string RemoveCurlyBrackets(string s)
{
if (string.IsNullOrEmpty(s))
{
return null;
}

return s.Replace("{", "").Replace("}", "");
return string.IsNullOrEmpty(s) ? null : s.Replace("{", "").Replace("}", "");
}

public static string RemoveLControl(string keySequence)
{
return true switch
{
_ when keySequence.Contains(@"RMENU + LCONTROL") => keySequence.Replace(@"+ LCONTROL", string.Empty),
_ when keySequence.Contains(@"LCONTROL + RMENU") => keySequence.Replace(@"LCONTROL +", string.Empty),
_ => keySequence,
_ when keySequence.Contains("RMENU + LCONTROL") => keySequence.Replace("+ LCONTROL", string.Empty),
_ when keySequence.Contains("LCONTROL + RMENU") => keySequence.Replace("LCONTROL +", string.Empty),
_ => keySequence
};
}

Expand All @@ -145,18 +140,17 @@ _ when keySequence.Contains(@"LCONTROL + RMENU") => keySequence.Replace(@"LCONTR
new GamingPanelSkeleton(GamingPanelVendorEnum.Elgato, GamingPanelEnum.StreamDeckXL),
new GamingPanelSkeleton(GamingPanelVendorEnum.Elgato, GamingPanelEnum.StreamDeckXLRev2),
new GamingPanelSkeleton(GamingPanelVendorEnum.CockpitMaster, GamingPanelEnum.CDU737),
new GamingPanelSkeleton(GamingPanelVendorEnum.Elgato, GamingPanelEnum.StreamDeckPlus),
new GamingPanelSkeleton(GamingPanelVendorEnum.Elgato, GamingPanelEnum.StreamDeckPlus)
};

private static void ValidateEmulationModeFlag()
{
if (IsEmulationModesFlagSet(EmulationMode.KeyboardEmulationOnly))
if (!IsEmulationModesFlagSet(EmulationMode.KeyboardEmulationOnly)) return;

if (IsEmulationModesFlagSet(EmulationMode.DCSBIOSOutputEnabled) ||
IsEmulationModesFlagSet(EmulationMode.DCSBIOSInputEnabled))
{
if (IsEmulationModesFlagSet(EmulationMode.DCSBIOSOutputEnabled) ||
IsEmulationModesFlagSet(EmulationMode.DCSBIOSInputEnabled))
{
throw new Exception($"Invalid emulation modes flag : {_emulationModesFlag}");
}
throw new Exception($"Invalid emulation modes flag : {_emulationModesFlag}");
}
}

Expand Down Expand Up @@ -185,7 +179,7 @@ public static bool IsEmulationModesFlagSet(EmulationMode flagValue)

public static void ClearEmulationModesFlag(EmulationMode flagValue)
{
_emulationModesFlag &= ~((int)flagValue);
_emulationModesFlag &= ~(int)flagValue;
}

public static void ResetEmulationModesFlag()
Expand Down Expand Up @@ -222,17 +216,17 @@ public static string GetMd5Hash(string input)
var md5 = MD5.Create();

// Convert the input string to a byte array and compute the hash.
byte[] data = md5.ComputeHash(Encoding.UTF8.GetBytes(input));
var data = md5.ComputeHash(Encoding.UTF8.GetBytes(input));

// Create a new Stringbuilder to collect the bytes
// and create a string.
var sBuilder = new StringBuilder();

// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for (int i = 0; i < data.Length; i++)
foreach (var t in data)
{
sBuilder.Append(data[i].ToString("x2"));
sBuilder.Append(t.ToString("x2"));
}

// Return the hexadecimal string.
Expand Down Expand Up @@ -281,20 +275,19 @@ public static string GetRelativePath(string relativeTo, string path)

public static IEnumerable<T> FindVisualChildren<T>(DependencyObject dependencyObject) where T : DependencyObject
{
if (dependencyObject != null)
if (dependencyObject == null) yield break;

for (var i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject); i++)
{
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject); i++)
var child = VisualTreeHelper.GetChild(dependencyObject, i);
if (child is T o)
{
var child = VisualTreeHelper.GetChild(dependencyObject, i);
if (child is T o)
{
yield return o;
}

foreach (var childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
yield return o;
}

foreach (var childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
}
}
Expand All @@ -303,25 +296,20 @@ public static IEnumerable<T> FindVisualChildren<T>(DependencyObject dependencyOb
public static T FindVisualParent<T>(DependencyObject child) where T : DependencyObject
{
if (child == null)
{
return (null);
}
DependencyObject parentObj = VisualTreeHelper.GetParent(child);

//we've reached the end of the tree
if (parentObj == null)
{
return null;
}
var parentObj = VisualTreeHelper.GetParent(child);

// check if the parent matches the type we are requested
if (parentObj is T parent)
return parentObj switch
{
return parent;
}

// here, To find the next parent in the tree. we are using recursion until we found the requested type or reached to the end of tree.
return FindVisualParent<T>(parentObj);
//we've reached the end of the tree
null => null,
// check if the parent matches the type we are requested
T parent => parent,
// here, To find the next parent in the tree. we are using recursion until we found the requested type or reached to the end of tree.
_ => FindVisualParent<T>(parentObj)
};
}

//^[A-Za-z0-9 . \t]*(MouseEnter){1}(\s\+\=)\s*
Expand Down
4 changes: 2 additions & 2 deletions Source/ClassLibraryCommon/CommonEnums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public enum CopyContentType
/// </summary>
public enum APIModeEnum
{
keybd_event = 0,
KeybdEvent = 0,
SendInput = 1
}

Expand All @@ -40,7 +40,7 @@ public enum GamingPanelVendorEnum
Saitek = 0x6A3,
MadCatz = 0x0738,
Elgato = 0xFD9,
CockpitMaster = 0x0483,
CockpitMaster = 0x0483
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using System;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace ClassLibraryCommon.CustomControls
{
/// <summary>
/// Interaction logic for UserControlSpinningWheel.xaml
/// </summary>
public partial class UserControlSpinningWheel : UserControl, IDisposable
public partial class UserControlSpinningWheel : IDisposable
{
public static readonly DependencyProperty DoSpinProperty = DependencyProperty.Register(
nameof(DoSpin), typeof(bool), typeof(UserControlSpinningWheel), new PropertyMetadata(default(bool)));
Expand All @@ -31,7 +30,7 @@ public UserControlSpinningWheel()

if (DarkMode.DarkModeEnabled)
{
ImageConnected.Source = new BitmapImage(new Uri(@"/ClassLibraryCommon;component/Images/gear-image-darkmode.png", UriKind.Relative));
ImageConnected.Source = new BitmapImage(new Uri("/ClassLibraryCommon;component/Images/gear-image-darkmode.png", UriKind.Relative));
}
_stopGearTimer.Elapsed += TimerStopRotation;
}
Expand Down
64 changes: 32 additions & 32 deletions Source/ClassLibraryCommon/DCSAircraft.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class DCSAircraft
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

private static readonly object Lock = new();
private static List<DCSAircraft> ModulesList = new();
private static List<DCSAircraft> _modulesList = new();

public static DCSAircraft SelectedAircraft { get; set; }

Expand All @@ -43,15 +43,15 @@ public string LuaFilename

public bool IsMetaModule
{
get => JSONFilename.Contains("MetadataEnd") || JSONFilename.Contains("MetadataStart") || JSONFilename.Contains("CommonData");
get => JSONFilename.Contains("MetadataEnd") || JSONFilename.Contains("MetadataStart") || JSONFilename.Contains("CommonData");
}

/// <summary>
/// This is not exact science
/// </summary>
public string ModuleLuaName
{
get => JSONFilename.Replace(".json", "").Replace("-","_").Replace(" ", "_");
get => JSONFilename.Replace(".json", "").Replace("-", "_").Replace(" ", "_");
}

public string DCSName
Expand All @@ -76,7 +76,7 @@ public static List<DCSAircraft> Modules
{
lock (Lock)
{
return ModulesList;
return _modulesList;
}
}
}
Expand All @@ -87,7 +87,7 @@ public static bool HasDCSBIOSModules
{
lock (Lock)
{
return ModulesList.Count - 3 > 0; // Three modules are not DCS-BIOS
return _modulesList.Count - 3 > 0; // Three modules are not DCS-BIOS
}
}
}
Expand All @@ -98,7 +98,7 @@ public static int DCSBIOSModulesCount
{
lock (Lock)
{
return ModulesList.Count - 3; // Three modules are not DCS-BIOS
return _modulesList.Count - 3; // Three modules are not DCS-BIOS
}
}
}
Expand All @@ -107,21 +107,21 @@ private static void AddInternalModules()
{
lock (Lock)
{
if (!ModulesList.Exists(o => o.ID == 1))
if (!_modulesList.Exists(o => o.ID == 1))
{
var module = new DCSAircraft(1, "NoFrameLoadedYet", "NOFRAMELOADEDYET");
ModulesList.Add(module);
_modulesList.Add(module);
}

if (!ModulesList.Exists(o => o.ID == 2))
if (!_modulesList.Exists(o => o.ID == 2))
{
var module = new DCSAircraft(2, "Key Emulation", "KEYEMULATOR");
ModulesList.Add(module);
_modulesList.Add(module);
}
if (!ModulesList.Exists(o => o.ID == 3))
if (!_modulesList.Exists(o => o.ID == 3))
{
var module = new DCSAircraft(3, "Key Emulation with SRS support", "KEYEMULATOR_SRS");
ModulesList.Add(module);
_modulesList.Add(module);
}
}
}
Expand All @@ -130,7 +130,7 @@ public static void FillModulesListFromDcsBios(string dcsbiosJsonFolder, bool loa
{
lock (Lock)
{
ModulesList.Clear();
_modulesList.Clear();
if (loadInternalModules)
{
AddInternalModules();
Expand All @@ -143,7 +143,7 @@ public static void FillModulesListFromDcsBios(string dcsbiosJsonFolder, bool loa
LogErrorAndThrowException($"Failed to find {dcsbiosConfigFile} in base directory.");
return;
}

var result = Common.CheckJSONDirectory(dcsbiosJsonFolder);
if (result.Item1 == false && result.Item2 == false)
{
Expand All @@ -157,33 +157,35 @@ public static void FillModulesListFromDcsBios(string dcsbiosJsonFolder, bool loa

lock (Lock)
{
ModulesList.Add(new DCSAircraft(500, "MetadataEnd", "MetadataEnd.json"));
ModulesList.Add(new DCSAircraft(501, "MetadataStart", "MetadataStart.json"));
ModulesList.Add(new DCSAircraft(502, "CommonData", "CommonData.json"));
_modulesList.Add(new DCSAircraft(500, "MetadataEnd", "MetadataEnd.json"));
_modulesList.Add(new DCSAircraft(501, "MetadataStart", "MetadataStart.json"));
_modulesList.Add(new DCSAircraft(502, "CommonData", "CommonData.json"));
}

// A-10C|5|A-10C Thunderbolt/II
foreach (var s in stringArray)
{
if (!s.StartsWith("--") && s.Contains('|'))
if (s.StartsWith("--") || !s.Contains('|'))
{
var parts = s.Split(new [] { "|" }, StringSplitOptions.RemoveEmptyEntries);
continue;
}

var parts = s.Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries);

var json = parts[0]+ ".json";
var id = int.Parse(parts[1]);
var properName = parts[2];

lock (Lock)
{
ModulesList.Add(new DCSAircraft(id, properName, json));
}
var json = parts[0] + ".json";
var id = int.Parse(parts[1]);
var properName = parts[2];

lock (Lock)
{
_modulesList.Add(new DCSAircraft(id, properName, json));
}
}

lock (Lock)
{
ModulesList = ModulesList.OrderBy(o => o.Description).ToList();
_modulesList = _modulesList.OrderBy(o => o.Description).ToList();
}
}

Expand Down Expand Up @@ -527,11 +529,9 @@ public static DCSAircraft GetBackwardCompatible(string oldEnumValue)
{
return Modules.Find(o => o.ID == moduleNumber);
}
else
{
LogErrorAndThrowException("Failed to determine profile ID (null) in your bindings file.");
return null; //just to avoid compilation problem "error CS0161 not all code paths return a value"
}

LogErrorAndThrowException("Failed to determine profile ID (null) in your bindings file.");
return null; //just to avoid compilation problem "error CS0161 not all code paths return a value"
}
}
}
Loading

0 comments on commit 5aafe7a

Please sign in to comment.