Skip to content

Commit

Permalink
more net8 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Aytackydln committed Jan 8, 2024
1 parent a161d9d commit 2eb0c2a
Show file tree
Hide file tree
Showing 18 changed files with 56 additions and 171 deletions.
2 changes: 1 addition & 1 deletion Project-Aurora/AuroraCommon/AuroraCommon.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>
<TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
Expand Down
3 changes: 1 addition & 2 deletions Project-Aurora/AuroraCommon/Data/MemorySharedArray.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System.Collections;
using System.IO.MemoryMappedFiles;
using System.Runtime.InteropServices;
using Common.Data;

namespace Common;
namespace Common.Data;

public class MemorySharedArray<T> : SignaledMemoryObject, IEnumerable<T> where T : struct
{
Expand Down
8 changes: 4 additions & 4 deletions Project-Aurora/AuroraCommon/Devices/DeviceConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ public ObservableCollection<string> EnabledDevices
{"Razer", "Razer (RGB.NET)"},
};

private static List<string> DefaultEnabledDevices => new()
{
private static List<string> DefaultEnabledDevices =>
[
"Corsair (RGB.NET)",
"Logitech (RGB.NET)",
"OpenRGB (RGB.NET)",
};
"OpenRGB (RGB.NET)"
];

public VariableRegistry VarRegistry { get; set; } = new();

Expand Down
11 changes: 3 additions & 8 deletions Project-Aurora/AuroraCommon/Devices/RGBNet/CurrentDevices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@

namespace Common.Devices.RGBNet;

public class CurrentDevices
[method: JsonConstructor]
public class CurrentDevices(List<RemappableDevice> devices)
{
[JsonConstructor]
public CurrentDevices(List<RemappableDevice> devices)
{
Devices = devices;
}

public List<RemappableDevice> Devices { get; }
public List<RemappableDevice> Devices { get; } = devices;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class DeviceMappingConfig
private static readonly string ConfigPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Aurora", "DeviceMappings.json");

[JsonPropertyName("d")]
public List<DeviceRemap> Devices { get; } = new();
public List<DeviceRemap> Devices { get; } = [];

private DeviceMappingConfig()
{
Expand Down
27 changes: 9 additions & 18 deletions Project-Aurora/AuroraCommon/Devices/RGBNet/RemappableDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,16 @@

namespace Common.Devices.RGBNet;

public class RemappableDevice
[method: JsonConstructor]
public class RemappableDevice(string deviceId, string deviceSummary, List<LedId> rgbNetLeds, SimpleColor calibration, bool remapEnabled)
{
public string DeviceId { get; }
public string DeviceSummary { get; }
public string DeviceId { get; } = deviceId;
public string DeviceSummary { get; } = deviceSummary;

// $"[{rgbDevice.DeviceInfo.DeviceType}] {rgbDevice.DeviceInfo.DeviceName}"
public List<LedId> RgbNetLeds { get; }

public SimpleColor Calibration { get; }

public bool RemapEnabled { get; }
public List<LedId> RgbNetLeds { get; } = rgbNetLeds;

public SimpleColor Calibration { get; } = calibration;

[JsonConstructor]
public RemappableDevice(string deviceId, string deviceSummary, List<LedId> rgbNetLeds, SimpleColor calibration, bool remapEnabled)
{
DeviceId = deviceId;
DeviceSummary = deviceSummary;
RgbNetLeds = rgbNetLeds;
Calibration = calibration;
RemapEnabled = remapEnabled;
}
public bool RemapEnabled { get; } = remapEnabled;
}
15 changes: 4 additions & 11 deletions Project-Aurora/AuroraCommon/Devices/RGBNet/RemappableLed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,9 @@

namespace Common.Devices.RGBNet;

public record struct RemappableLed
public record struct RemappableLed(LedId LedId, DeviceKeys RemappedKey, SimpleColor ColorOverride)
{
public LedId LedId;
public DeviceKeys RemappedKey;
public SimpleColor ColorOverride;

public RemappableLed(LedId ledId, DeviceKeys remappedKey, SimpleColor colorOverride)
{
LedId = ledId;
RemappedKey = remappedKey;
ColorOverride = colorOverride;
}
public LedId LedId = LedId;
public DeviceKeys RemappedKey = RemappedKey;
public SimpleColor ColorOverride = ColorOverride;
}
17 changes: 2 additions & 15 deletions Project-Aurora/AuroraCommon/SimpleColor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,12 @@

namespace Common;

public readonly record struct SimpleColor
[method: JsonConstructor]
public readonly record struct SimpleColor(byte R, byte G, byte B, byte A = 255)
{
public static readonly SimpleColor White = new(255, 255, 255);
public static readonly SimpleColor Black = new(0, 0, 0);

public byte R { get; }
public byte G { get; }
public byte B { get; }
public byte A { get; }

[JsonConstructor]
public SimpleColor(byte r, byte g, byte b, byte a = 255)
{
R = r;
G = g;
B = b;
A = a;
}

public int ToArgb() => (A << 24) | (R << 16) | (G << 8) | B;

public static explicit operator Color(SimpleColor color)
Expand Down
7 changes: 2 additions & 5 deletions Project-Aurora/AuroraCommon/Utils/AssemblyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ public static class AssemblyExtensions
{
public static IEnumerable<Type> GetLoadableTypes(this Assembly assembly)
{
Type?[] types;
try
{
types = assembly.GetTypes();
return assembly.GetTypes();
}
catch (ReflectionTypeLoadException e)
{
types = e.Types;
return e.Types.Where(type => type != null);
}

return types.Where(type => type != null);
}
}
17 changes: 0 additions & 17 deletions Project-Aurora/AuroraCommon/Utils/CommonColorUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,23 +322,6 @@ public static Color CloneColor(Color clr)
return Color.FromArgb(clr.ToArgb());
}

public static bool NearlyEqual(double a, double b, double epsilon) {
const double minNormal = 2.2250738585072014E-308d;
double absA = Math.Abs(a);
double absB = Math.Abs(b);
double diff = Math.Abs(a - b);

if (a.Equals(b)) { // shortcut, handles infinities
return true;
}
if (a == 0 || b == 0 || absA + absB < minNormal) {
// a or b is zero or both are extremely close to it
// relative error is less meaningful here
return diff < epsilon * minNormal;
} // use relative error
return diff / (absA + absB) < epsilon;
}

public static Color FastColorTransparent(byte r, byte g, byte b)
{
var brightness = Math.Max(b, Math.Max(g, r));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,4 @@ namespace Common.Utils;
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(DeviceMappingConfig))]
[JsonSerializable(typeof(DeviceConfig))]
public partial class CommonSourceGenerationContext : JsonSerializerContext
{

}
public partial class CommonSourceGenerationContext : JsonSerializerContext;
6 changes: 3 additions & 3 deletions Project-Aurora/AuroraCommon/Utils/MemorySharedEventThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Common.Utils;

internal static class MemorySharedEventThread
{
private static readonly List<HandlesAndThread> HandleThreads = new();
private static readonly List<HandlesAndThread> HandleThreads = [];

internal static void AddObject(SignaledMemoryObject o)
{
Expand Down Expand Up @@ -34,14 +34,14 @@ private class HandlesAndThread
private CancellationTokenSource _cancellation = new();
private Thread _thread = new(() => { });

private Action[] _actions = { () => { } };
private Action[] _actions = [() => { }];
private WaitHandle[] _handles;

private readonly SemaphoreSlim _semaphore = new(1);

internal HandlesAndThread()
{
_handles = new[] { _cancellation.Token.WaitHandle };
_handles = [_cancellation.Token.WaitHandle];
_thread.Start();
}

Expand Down
21 changes: 6 additions & 15 deletions Project-Aurora/AuroraCommon/Utils/PluginCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,8 @@

namespace Common.Utils;

public class PluginCompiler
public class PluginCompiler(ILogger logger, string path)
{
private readonly ILogger _logger;
private readonly string _compilerPath;

public PluginCompiler(ILogger logger, string compilerPath)
{
_logger = logger;
_compilerPath = compilerPath;
}

public async Task Compile(string scriptPath)
{
var scriptChangeTime = File.GetLastWriteTime(scriptPath);
Expand All @@ -22,12 +13,12 @@ public async Task Compile(string scriptPath)

if (scriptChangeTime < dllCompileTime)
{
_logger.Information("Script {Script} is up to date", scriptPath);
logger.Information("Script {Script} is up to date", scriptPath);
return;
}

_logger.Information("Compiling script: {Script}", scriptPath);
var compilerPath = Path.Combine(_compilerPath, "PluginCompiler.exe");
logger.Information("Compiling script: {Script}", scriptPath);
var compilerPath = Path.Combine(path, "PluginCompiler.exe");
var compilerProc = new ProcessStartInfo
{
WorkingDirectory = Path.GetDirectoryName(Environment.ProcessPath),
Expand All @@ -42,7 +33,7 @@ public async Task Compile(string scriptPath)

process.ErrorDataReceived += (_, args) =>
{
_logger.Error("Compiler: {}", args.Data);
logger.Error("Compiler: {}", args.Data);
};

try
Expand All @@ -51,7 +42,7 @@ public async Task Compile(string scriptPath)
}
catch (Exception e)
{
_logger.Error(e, "Could not load script: {Script}", scriptPath);
logger.Error(e, "Could not load script: {Script}", scriptPath);
}
}
}
51 changes: 1 addition & 50 deletions Project-Aurora/AuroraCommon/Utils/TypeUtils.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
using System.Reflection;

namespace Common.Utils;
namespace Common.Utils;

public static class TypeUtils
{
//Solution from http://stackoverflow.com/questions/1749966/c-sharp-how-to-determine-whether-a-type-is-a-number
public static bool IsNumericType(this object o)
{
return IsNumericType(o.GetType());
}

public static bool IsNumericType(Type type)
{
switch (Type.GetTypeCode(type))
Expand All @@ -30,45 +22,4 @@ public static bool IsNumericType(Type type)
return false;
}
}

/// <summary>
/// Inspects all types in all current assemblies and returns a dictionary containing all types that have the specified custom attribute.
/// </summary>
/// <typeparam name="T">The type of custom attribute to fetch.</typeparam>
/// <returns>A dictionary containing the type and the custom attribute assigned to it.</returns>
public static IEnumerable<KeyValuePair<Type, T>> GetTypesWithCustomAttribute<T>() where T : Attribute {
return AppDomain.CurrentDomain.GetAssemblies() // Get all the assemblies
.SelectMany(assembly => {
try { return assembly.GetLoadableTypes(); } // Attempt to get all types in these assemblies
catch (ReflectionTypeLoadException e) { return e.Types.Where(t => t != null); } // If there was an error loading some of them, return only the successful ones
})
.ToDictionary(type => type, type => (T)Attribute.GetCustomAttribute(type, typeof(T))) // Attempt to get the specified attibute of them
.Where(kvp => kvp.Value != null); // Filter out any where the attribute is null (i.e. doesn't exist)
}


/// <summary>Gets the generic argument types of the given interface for the given type.</summary>
/// <param name="type">The type to check interfaces on.</param>
/// <param name="interfaceType">The type of interface whose generic parameters to fetch.</param>
/// <returns>An array containing all the types of generic parameters defined for this type on the given interface, or null if interface not found.</returns>
public static Type[] GetGenericInterfaceTypes(this Type type, Type interfaceType) =>
type.GetInterfaces()
.Where(i => i.IsGenericType)
.SingleOrDefault(i => i.GetGenericTypeDefinition() == interfaceType)?
.GetGenericArguments();

/// <summary>Gets the generic argument types of the given parent type for this type.
/// Searches the parent heirarchy until <paramref name="parentType"/> is found.</summary>
/// <param name="type">The type to check parent types for.</param>
/// <param name="parentType">Searches the parent types of <paramref name="type"/> until this generic type is found, returning this type's type parameters.</param>
/// <returns></returns>
public static Type[] GetGenericParentTypes(this Type type, Type parentType) {
var curType = type;
while (curType != null) {
if (curType.IsGenericType && curType.GetGenericTypeDefinition() == parentType)
return curType.GetGenericArguments();
curType = curType.BaseType;
}
return null;
}
}
1 change: 1 addition & 0 deletions Project-Aurora/AuroraDeviceManager/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using AuroraDeviceManager.Devices;
using AuroraDeviceManager.Utils;
using Common;
using Common.Data;
using Color = System.Drawing.Color;

{
Expand Down
4 changes: 2 additions & 2 deletions Project-Aurora/Project-Aurora/Modules/Inputs/InputEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public sealed class InputEvents : IInputEvents
private static readonly Keys[] WinKeys = { Keys.LWin, Keys.RWin };
public bool Windows => WinKeys.Any(PressedKeys.Contains);

private delegate IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
private delegate nint WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
// ReSharper disable once PrivateFieldCanBeConvertedToLocalVariable to keep reference for garbage collector
private readonly WndProc? _fnWndProcHook;
private readonly nint _originalWndProc;
Expand All @@ -84,7 +84,7 @@ public InputEvents()
User32.SetWindowLongPtr(_hWnd, -4, newLong);
}

private IntPtr Hook(IntPtr hwnd, uint msg, IntPtr wparam, IntPtr lparam)
private nint Hook(IntPtr hwnd, uint msg, IntPtr wparam, IntPtr lparam)
{
const int wmInput = 0x00FF;

Expand Down
Loading

0 comments on commit 2eb0c2a

Please sign in to comment.