From 31e54c16373a0b67153f547d1f3a91f5790f2ce3 Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Wed, 10 Jun 2026 00:29:28 +0200 Subject: [PATCH] Code cleanup --- Kernel.cs | 6 +-- Logging/ILogger.cs | 20 +++----- Logging/InMemoryLogger.cs | 53 ++++++++++----------- Logging/LogEntry.cs | 10 +--- Logging/LogSeverity.cs | 16 ++----- Logging/ProxyLogger.cs | 56 ++++++++++------------ Processing/ProcessManager.cs | 24 +++++----- Processing/ProcessManiest.cs | 48 +++++++++---------- UI/CLI/Commands/StopGuiCommand.cs | 3 -- UI/CLI/Commands/ViewProcessLogs.cs | 59 +++++++++++------------ Utils/ConcurrentHashSet.cs | 75 +++++++++++++++++++++++------- 11 files changed, 184 insertions(+), 186 deletions(-) diff --git a/Kernel.cs b/Kernel.cs index 4d17d62..b04c15c 100644 --- a/Kernel.cs +++ b/Kernel.cs @@ -1,7 +1,5 @@ global using Sys = Cosmos.Kernel.System; -using Cosmos.Kernel.Core; -using RemSox.Logging; using RemSox.Processes; using RemSox.Processing; using RemSox.Processing.IPC; @@ -37,7 +35,7 @@ public class Kernel : Sys.Kernel Sys.Mouse.MouseManager.Initialize(); Sys.Keyboard.KeyboardManager.Initialize(); - ProcessManager.SpawnProcess(); + _ = ProcessManager.SpawnProcess(); } protected override void Run() @@ -47,7 +45,7 @@ public class Kernel : Sys.Kernel if (!desktopRunning && !cliRunning) { - ProcessManager.SpawnProcess(); + _ = ProcessManager.SpawnProcess(); return; } diff --git a/Logging/ILogger.cs b/Logging/ILogger.cs index 11b34c4..6c9378a 100644 --- a/Logging/ILogger.cs +++ b/Logging/ILogger.cs @@ -1,20 +1,14 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; +namespace RemSox.Logging; -namespace RemSox.Logging +public interface ILogger { - public interface ILogger - { - void Log(string message, LogSeverity severity); + void Log(string message, LogSeverity severity); - void LogInfo(string message); + void LogInfo(string message); - void LogError(string message); + void LogError(string message); - void LogWarning(string message); + void LogWarning(string message); - IEnumerable GetLogs(int? count = null); - } + IEnumerable GetLogs(int? count = null); } \ No newline at end of file diff --git a/Logging/InMemoryLogger.cs b/Logging/InMemoryLogger.cs index 4469631..9026511 100644 --- a/Logging/InMemoryLogger.cs +++ b/Logging/InMemoryLogger.cs @@ -1,39 +1,34 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; using System.Collections.Concurrent; -namespace RemSox.Logging +namespace RemSox.Logging; + +public class InMemoryLogger : ILogger { - public class InMemoryLogger : ILogger + private readonly ConcurrentBag logs = []; + + public void Log(string message, LogSeverity severity) { - readonly ConcurrentBag logs = []; + logs.Add(new LogEntry(message, severity, DateTimeOffset.UtcNow)); + } - public void Log(string message, LogSeverity severity) - { - logs.Add(new LogEntry(message, severity, DateTimeOffset.UtcNow)); - } + public void LogError(string message) + { + Log(message, LogSeverity.Error); + } - public void LogError(string message) - { - Log(message, LogSeverity.Error); - } + public void LogInfo(string message) + { + Log(message, LogSeverity.Info); + } - public void LogInfo(string message) - { - Log(message, LogSeverity.Info); - } + public void LogWarning(string message) + { + Log(message, LogSeverity.Warning); + } - public void LogWarning(string message) - { - Log(message, LogSeverity.Warning); - } - - public IEnumerable GetLogs(int? count = null) - { - IEnumerable orderedLogs = logs.OrderBy(l => l.Timestamp); - return count.HasValue ? orderedLogs.TakeLast(count.Value) : orderedLogs; - } + public IEnumerable GetLogs(int? count = null) + { + IEnumerable orderedLogs = logs.OrderBy(l => l.Timestamp); + return count.HasValue ? orderedLogs.TakeLast(count.Value) : orderedLogs; } } \ No newline at end of file diff --git a/Logging/LogEntry.cs b/Logging/LogEntry.cs index 0231972..91d0a10 100644 --- a/Logging/LogEntry.cs +++ b/Logging/LogEntry.cs @@ -1,9 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; +namespace RemSox.Logging; -namespace RemSox.Logging -{ - public record LogEntry(string Message, LogSeverity Severity, DateTimeOffset Timestamp); -} \ No newline at end of file +public record LogEntry(string Message, LogSeverity Severity, DateTimeOffset Timestamp); \ No newline at end of file diff --git a/Logging/LogSeverity.cs b/Logging/LogSeverity.cs index c0ee16d..7dbaaf1 100644 --- a/Logging/LogSeverity.cs +++ b/Logging/LogSeverity.cs @@ -1,14 +1,8 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; +namespace RemSox.Logging; -namespace RemSox.Logging +public enum LogSeverity { - public enum LogSeverity - { - Info, - Warning, - Error - } + Info, + Warning, + Error } \ No newline at end of file diff --git a/Logging/ProxyLogger.cs b/Logging/ProxyLogger.cs index 4750c26..ce4eb29 100644 --- a/Logging/ProxyLogger.cs +++ b/Logging/ProxyLogger.cs @@ -1,38 +1,32 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; +namespace RemSox.Logging; -namespace RemSox.Logging +public class ProxyLogger(IEnumerable loggers) : ILogger { - public class ProxyLogger(IEnumerable loggers) : ILogger + public void Log(string message, LogSeverity severity) { - public void Log(string message, LogSeverity severity) + foreach (ILogger logger in loggers) { - foreach (var logger in loggers) - { - logger.Log(message, severity); - } - } - - public void LogError(string message) - { - Log(message, LogSeverity.Error); - } - - public void LogInfo(string message) - { - Log(message, LogSeverity.Info); - } - - public void LogWarning(string message) - { - Log(message, LogSeverity.Warning); - } - - public IEnumerable GetLogs(int? count = null) - { - return loggers.SelectMany(logger => logger.GetLogs(count / loggers.Count())).OrderBy(entry => entry.Timestamp); + logger.Log(message, severity); } } + + public void LogError(string message) + { + Log(message, LogSeverity.Error); + } + + public void LogInfo(string message) + { + Log(message, LogSeverity.Info); + } + + public void LogWarning(string message) + { + Log(message, LogSeverity.Warning); + } + + public IEnumerable GetLogs(int? count = null) + { + return loggers.SelectMany(logger => logger.GetLogs(count / loggers.Count())).OrderBy(entry => entry.Timestamp); + } } \ No newline at end of file diff --git a/Processing/ProcessManager.cs b/Processing/ProcessManager.cs index 74fa716..4012e98 100644 --- a/Processing/ProcessManager.cs +++ b/Processing/ProcessManager.cs @@ -33,7 +33,7 @@ public static class ProcessManager InMemoryLogger processLogger = new(); ProxyLogger proxyLogger = new([logger, processLogger]); - processLoggers.TryAdd(id, processLogger); + _ = processLoggers.TryAdd(id, processLogger); T process = new() { @@ -41,7 +41,7 @@ public static class ProcessManager Logger = proxyLogger }; - processesByType.AddOrUpdate(typeof(T), _ => [id], (_, set) => + _ = processesByType.AddOrUpdate(typeof(T), _ => [id], (_, set) => { set.Add(id); return set; @@ -59,24 +59,26 @@ public static class ProcessManager } finally { - processes.TryRemove(id, out _); + _ = processes.TryRemove(id, out _); - if (processesByType.TryGetValue(typeof(T), out var set)) + if (processesByType.TryGetValue(typeof(T), out ConcurrentHashSet? set)) { - set.TryRemove(id); + _ = set.TryRemove(id); if (set.Count == 0) - processesByType.TryRemove(typeof(T), out _); + { + _ = processesByType.TryRemove(typeof(T), out _); + } } WindowManager.CloseWindowsForProcess(id); logger.Log($"Process {process.Name} (ID: {process.Id}) has stopped.", LogSeverity.Info); - processLoggers.TryRemove(id, out _); + _ = processLoggers.TryRemove(id, out _); } }); - processes.TryAdd(id, (process, thread)); + _ = processes.TryAdd(id, (process, thread)); thread.Start(); logger.Log($"Spawned process {process.Name} of type {typeof(T).Name} with ID {id}.", LogSeverity.Info); @@ -106,7 +108,7 @@ public static class ProcessManager entry.Process.RequestStop(); logger.Log($"Waiting for process {entry.Process.Name} (ID: {entry.Process.Id}) to stop.", LogSeverity.Info); - await Task.Run(() => entry.Thread.Join()); + await Task.Run(entry.Thread.Join); } public static void StopAllProcesses() @@ -131,12 +133,12 @@ public static class ProcessManager public static bool IsProcessRunning() where T : Process { - return processesByType.TryGetValue(typeof(T), out var set) && set.Count > 0; + return processesByType.TryGetValue(typeof(T), out ConcurrentHashSet? set) && set.Count > 0; } public static IEnumerable GetProcessesOfType() where T : Process { - if (processesByType.TryGetValue(typeof(T), out var set)) + if (processesByType.TryGetValue(typeof(T), out ConcurrentHashSet? set)) { foreach (int processId in set) { diff --git a/Processing/ProcessManiest.cs b/Processing/ProcessManiest.cs index 6f9c588..9433893 100644 --- a/Processing/ProcessManiest.cs +++ b/Processing/ProcessManiest.cs @@ -1,36 +1,30 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; using RemSox.Processes; -using RemSox.Processing; -namespace RemSox.Processing +namespace RemSox.Processing; + +public static class ProcessManifest { - public static class ProcessManifest + [Flags] + public enum ProcessManifestFlags { - [Flags] - public enum ProcessManifestFlags - { - None = 0, - Singleton = 1 << 0, - System = 1 << 1 - } + None = 0, + Singleton = 1 << 0, + System = 1 << 1 + } - private static readonly Dictionary map = new() - { - { typeof(DesktopProcess), ProcessManifestFlags.Singleton | ProcessManifestFlags.System }, - { typeof(CliProcess), ProcessManifestFlags.Singleton | ProcessManifestFlags.System } - }; + private static readonly Dictionary map = new() + { + { typeof(DesktopProcess), ProcessManifestFlags.Singleton | ProcessManifestFlags.System }, + { typeof(CliProcess), ProcessManifestFlags.Singleton | ProcessManifestFlags.System } + }; - public static bool HasFlag(ProcessManifestFlags flag) where T : Process - { - return map.TryGetValue(typeof(T), out var flags) && flags.HasFlag(flag); - } + public static bool HasFlag(ProcessManifestFlags flag) where T : Process + { + return map.TryGetValue(typeof(T), out ProcessManifestFlags flags) && flags.HasFlag(flag); + } - public static bool HasFlag(Type t, ProcessManifestFlags flag) - { - return map.TryGetValue(t, out var flags) && flags.HasFlag(flag); - } + public static bool HasFlag(Type t, ProcessManifestFlags flag) + { + return map.TryGetValue(t, out ProcessManifestFlags flags) && flags.HasFlag(flag); } } \ No newline at end of file diff --git a/UI/CLI/Commands/StopGuiCommand.cs b/UI/CLI/Commands/StopGuiCommand.cs index 9bc2dbb..4b70360 100644 --- a/UI/CLI/Commands/StopGuiCommand.cs +++ b/UI/CLI/Commands/StopGuiCommand.cs @@ -1,8 +1,5 @@ -using Cosmos.Kernel.System.Graphics; -using Org.BouncyCastle.Bcpg; using RemSox.Processes; using RemSox.Processing; -using RemSox.UI.GUI.Windows; namespace RemSox.UI.CLI.Commands; diff --git a/UI/CLI/Commands/ViewProcessLogs.cs b/UI/CLI/Commands/ViewProcessLogs.cs index 74415b9..cd9531a 100644 --- a/UI/CLI/Commands/ViewProcessLogs.cs +++ b/UI/CLI/Commands/ViewProcessLogs.cs @@ -1,44 +1,39 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; using RemSox.Logging; using RemSox.Processing; -namespace RemSox.UI.CLI.Commands +namespace RemSox.UI.CLI.Commands; + +public class ViewProcessLogs : ICommand { - public class ViewProcessLogs : ICommand + public string Name => "logs"; + + public string Description => "View logs for a process"; + + public void Execute(string? arguments, Action printLine) { - public string Name => "logs"; - - public string Description => "View logs for a process"; - - public void Execute(string? arguments, Action printLine) + if (int.TryParse(arguments, out int processId)) { - if (int.TryParse(arguments, out int processId)) - { - IEnumerable logs = ProcessManager.GetProcessLogs(processId); - PrintLogs(logs, printLine); - } - else - { - IEnumerable logs = ProcessManager.GetLogs(); - PrintLogs(logs, printLine); - } + IEnumerable logs = ProcessManager.GetProcessLogs(processId); + PrintLogs(logs, printLine); + } + else + { + IEnumerable logs = ProcessManager.GetLogs(); + PrintLogs(logs, printLine); + } + } + + private static void PrintLogs(IEnumerable logs, Action printLine) + { + if (!logs.Any()) + { + printLine("No logs found!"); + return; } - private static void PrintLogs(IEnumerable logs, Action printLine) + foreach (LogEntry log in logs) { - if (!logs.Any()) - { - printLine("No logs found!"); - return; - } - - foreach (LogEntry log in logs) - { - printLine($"[{log.Timestamp:HH:mm:ss}] [{log.Severity}] {log.Message}"); - } + printLine($"[{log.Timestamp:HH:mm:ss}] [{log.Severity}] {log.Message}"); } } } \ No newline at end of file diff --git a/Utils/ConcurrentHashSet.cs b/Utils/ConcurrentHashSet.cs index 680b1fa..9d7eaeb 100644 --- a/Utils/ConcurrentHashSet.cs +++ b/Utils/ConcurrentHashSet.cs @@ -1,8 +1,5 @@ -using System; using System.Collections; using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; /// /// Represents a thread-safe hash set, backed by a . @@ -92,17 +89,27 @@ public partial class ConcurrentHashSet : // ------------------------------------------------------------------------- /// Removes all elements from the set. - public void Clear() => _dictionary.Clear(); + public void Clear() + { + _dictionary.Clear(); + } /// Determines whether the set contains the specified element. public bool Contains(T item) { - if (item is null) throw new ArgumentNullException(nameof(item)); + if (item is null) + { + throw new ArgumentNullException(nameof(item)); + } + return _dictionary.ContainsKey(item); } /// Returns an enumerator that iterates through the elements of the set. - public IEnumerator GetEnumerator() => _dictionary.Keys.GetEnumerator(); + public IEnumerator GetEnumerator() + { + return _dictionary.Keys.GetEnumerator(); + } /// /// Returns the element from the set if it already exists, or adds and returns @@ -112,10 +119,13 @@ public partial class ConcurrentHashSet : /// The existing element if found; otherwise after it was added. public T GetOrAdd(T item) { - if (item is null) throw new ArgumentNullException(nameof(item)); + if (item is null) + { + throw new ArgumentNullException(nameof(item)); + } // TryAdd is atomic; if it fails the item was already present. - _dictionary.TryAdd(item, DummyValue); + _ = _dictionary.TryAdd(item, DummyValue); // Because ConcurrentDictionary keys are de-duplicated by the comparer, // we need to retrieve the canonical key that is actually stored. @@ -123,7 +133,9 @@ public partial class ConcurrentHashSet : foreach (T key in _dictionary.Keys) { if (_dictionary.Comparer.Equals(key, item)) + { return key; + } } // Fallback — should not happen in practice. @@ -134,13 +146,20 @@ public partial class ConcurrentHashSet : /// Adds the specified element to the set. Duplicate elements are silently ignored. /// This overload exists to support collection initializer syntax (new ConcurrentHashSet<T> { item }). /// - public void Add(T item) => TryAdd(item); + public void Add(T item) + { + _ = TryAdd(item); + } /// Attempts to add the specified element to the set. /// if the element was added; if it was already present. public bool TryAdd(T item) { - if (item is null) throw new ArgumentNullException(nameof(item)); + if (item is null) + { + throw new ArgumentNullException(nameof(item)); + } + return _dictionary.TryAdd(item, DummyValue); } @@ -148,15 +167,25 @@ public partial class ConcurrentHashSet : /// if the element was removed; if it was not found. public bool TryRemove(T item) { - if (item is null) throw new ArgumentNullException(nameof(item)); + if (item is null) + { + throw new ArgumentNullException(nameof(item)); + } + return _dictionary.TryRemove(item, out _); } /// Copies the elements of the set to a new array. - public T[] ToArray() => [.. _dictionary.Keys]; + public T[] ToArray() + { + return [.. _dictionary.Keys]; + } /// Returns a (non-thread-safe) snapshot of the current elements. - public HashSet ToHashSet() => new(_dictionary.Keys, _dictionary.Comparer); + public HashSet ToHashSet() + { + return new(_dictionary.Keys, _dictionary.Comparer); + } // ------------------------------------------------------------------------- // ICollection explicit implementation @@ -164,9 +193,15 @@ public partial class ConcurrentHashSet : bool ICollection.IsReadOnly => false; - void ICollection.Add(T item) => Add(item); + void ICollection.Add(T item) + { + Add(item); + } - bool ICollection.Contains(T item) => Contains(item); + bool ICollection.Contains(T item) + { + return Contains(item); + } void ICollection.CopyTo(T[] array, int index) { @@ -176,7 +211,10 @@ public partial class ConcurrentHashSet : Array.Copy(snapshot, 0, array, index, snapshot.Length); } - bool ICollection.Remove(T item) => TryRemove(item); + bool ICollection.Remove(T item) + { + return TryRemove(item); + } // ------------------------------------------------------------------------- // ICollection (non-generic) explicit implementation @@ -210,5 +248,8 @@ public partial class ConcurrentHashSet : // IEnumerable explicit implementation // ------------------------------------------------------------------------- - IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } } \ No newline at end of file