diff --git a/YAMDCC.ConfigEditor/packages.lock.json b/YAMDCC.ConfigEditor/packages.lock.json index 22c70ac..a694e4f 100644 --- a/YAMDCC.ConfigEditor/packages.lock.json +++ b/YAMDCC.ConfigEditor/packages.lock.json @@ -104,8 +104,8 @@ "ec-inspect": { "type": "Project", "dependencies": { - "YAMDCC.Common": "[1.0.3-release, )", - "YAMDCC.IPC": "[2.1.3-release, )" + "YAMDCC.Common": "[1.0.4-dev, )", + "YAMDCC.ECAccess": "[1.0.4-dev, )" } }, "Updater": { @@ -114,7 +114,7 @@ "MarkedNet": "[2.1.4, )", "Newtonsoft.Json": "[13.0.3, )", "TaskScheduler": "[2.11.0, )", - "YAMDCC.Common": "[1.0.3-release, )" + "YAMDCC.Common": "[1.0.4-dev, )" } }, "yamdcc.common": { @@ -132,8 +132,8 @@ "yamdccsvc": { "type": "Project", "dependencies": { - "YAMDCC.Common": "[1.0.3-release, )", - "YAMDCC.ECAccess": "[1.0.3-release, )", + "YAMDCC.Common": "[1.0.4-dev, )", + "YAMDCC.ECAccess": "[1.0.4-dev, )", "YAMDCC.IPC": "[2.1.3-release, )" } } diff --git a/YAMDCC.ECInspector/ECValue.cs b/YAMDCC.ECInspector/ECValue.cs deleted file mode 100644 index 3f8f3d8..0000000 --- a/YAMDCC.ECInspector/ECValue.cs +++ /dev/null @@ -1,30 +0,0 @@ -// This file is part of YAMDCC (Yet Another MSI Dragon Center Clone). -// Copyright © Sparronator9999 and Contributors 2025. -// -// YAMDCC is free software: you can redistribute it and/or modify it -// under the terms of the GNU General Public License as published by the Free -// Software Foundation, either version 3 of the License, or (at your option) -// any later version. -// -// YAMDCC is distributed in the hope that it will be useful, but -// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for -// more details. -// -// You should have received a copy of the GNU General Public License along with -// YAMDCC. If not, see . - -namespace YAMDCC.ECInspector; - -internal struct ECValue -{ - /// - /// The EC value itself. - /// - public int Value; - - /// - /// How many EC polls it's been since was last updated. - /// - public int Age; -} diff --git a/YAMDCC.ECInspector/Program.cs b/YAMDCC.ECInspector/Program.cs index 145cec3..6ac2bad 100644 --- a/YAMDCC.ECInspector/Program.cs +++ b/YAMDCC.ECInspector/Program.cs @@ -15,120 +15,82 @@ // YAMDCC. If not, see . using System; -using System.ServiceProcess; using System.Threading; using YAMDCC.Common; -using YAMDCC.IPC; +using YAMDCC.ECAccess; namespace YAMDCC.ECInspector; -internal sealed class Program +internal static class Program { - private static readonly NamedPipeClient IPCClient = - new("YAMDCC-Server"); - - private static readonly Mutex LogMutex = new(false); - - private static readonly ECValue[] ECValues = new ECValue[256]; + private static readonly EC _EC = new(); private static int Main(string[] args) { if (!Utils.IsAdmin()) { - Console.WriteLine(Strings.GetString("NoAdmin")); + Console.WriteLine("ERROR: admin privileges required"); return 255; } - // check that YAMDCC service is running - using (ServiceController yamdccSvc = new("yamdccsvc")) + if (args.Length > 0 && args[0].Length > 0) { - try + switch (args[0][0]) { - if (yamdccSvc.Status == ServiceControllerStatus.Stopped) - { - Console.WriteLine(Strings.GetString("SvcStopped")); - return 1; - } - } - catch - { - Console.WriteLine(Strings.GetString("SvcNotFound")); - return 1; + case 'v': + case 'V': + Console.WriteLine(Utils.GetVerString()); + return 0; + case 'h': + case 'H': + break; + case 'd': + case 'D': + return DumpEC(1000, 1) ? 0 : 1; + case 'm': + case 'M': + return DumpEC(1000, -1) ? 0 : 1; + default: + Console.WriteLine($"ERROR: unknown command: {args[0]}"); + break; } } - - if (args.Length == 0) + else { - Console.WriteLine(Strings.GetString("NoCmd")); - Help(); - return 2; - } - switch (args[0]) - { - case "--version": - case "-v": - Console.WriteLine(Utils.GetVerString()); - return 0; - case "--help": - case "-h": - Help(); - return 0; - case "--dump": - case "-d": - if (ConnectService()) - { - DumpEC(1000, 1); - return 0; - } - return 3; - case "--monitor": - case "-m": - if (ConnectService()) - { - DumpEC(1000, -1); - return 0; - } - return 3; - case "": - Console.WriteLine(Strings.GetString("NoCmd")); - Help(); - return 2; - default: - Console.WriteLine(Strings.GetString("BadCmd", args[0])); - Help(); - return 2; + Console.WriteLine("ERROR: no command specified"); } + Help(); + return 1; } private static void Help() { - Console.WriteLine(Strings.GetString("Help", - Environment.OSVersion, Utils.GetVerString(), - Utils.GetRevision(), AppDomain.CurrentDomain.FriendlyName)); + Console.WriteLine("\nYAMDCC EC inspection utility\n\n" + + $"OS version: {Environment.OSVersion}\n" + + $"App version: {Utils.GetVerString()}\n" + + $"Revision (git): {Utils.GetRevision()}\n\n" + + $"Usage: {AppDomain.CurrentDomain.FriendlyName} []\n\n" + + "Commands:\n" + + " help Print this help screen\n" + + " version Print the program version\n" + + " dump Dump all EC registers\n" + + " monitor Dump EC and monitor for changes"); } - private static bool ConnectService() + private static bool DumpEC(int interval, int loops) { - IPCClient.ServerMessage += ServerMessage; - IPCClient.Error += IPCError; - - IPCClient.Start(); - if (!IPCClient.WaitForConnection(5000)) + if (!_EC.LoadDriver()) { - Console.WriteLine(Strings.GetString("SvcConnErr")); return false; } - return true; - } - private static void DumpEC(int interval, int loops) - { + ECValue[] values = new ECValue[256]; for (int i = 0; i <= byte.MaxValue; i++) { - ECValues[i] = new ECValue(); + values[i] = new ECValue(); } - Console.Clear(); + Console.Clear(); Console.SetCursorPosition(0, 0); // write heading @@ -137,17 +99,16 @@ private static void DumpEC(int interval, int loops) { Console.Write($" 0{i:X}"); } - Console.WriteLine(); - Console.WriteLine("|".PadLeft(5, '-').PadRight(53, '-')); + Console.WriteLine("\n-----|".PadRight(53, '-')); for (int i = 0; i < 16; i++) { Console.WriteLine($" {i:X}0 |"); } - Console.WriteLine("\nPress Ctrl+C to exit"); + Console.WriteLine("Press Ctrl+C to exit"); Console.CursorVisible = false; - Console.CancelKeyPress += CancelKey; + Console.CancelKeyPress += new ConsoleCancelEventHandler(CancelKey); int j = 0; while (true) @@ -157,54 +118,31 @@ private static void DumpEC(int interval, int loops) break; } - byte i = byte.MaxValue; - do + // TODO: optimise out jumping all over the place? + // (leftover from when we used YAMDCC service for EC access) + for (int i = 0; i < values.Length; i++) { - i++; - IPCClient.PushMessage(new ServiceCommand(Command.ReadECByte, i)); - } - while (i < byte.MaxValue); - - Thread.Sleep(interval); - j++; - } - - Console.CursorVisible = true; - IPCClient.Stop(); - } - - private static void CancelKey(object sender, ConsoleCancelEventArgs e) - { - Console.CursorVisible = true; - IPCClient.Stop(); - } - - private static void ServerMessage(object sender, PipeMessageEventArgs e) - { - if (LogMutex.WaitOne()) - { - try - { - object[] args = e.Message.Value; - if (e.Message.Response == Response.ReadResult && - args.Length == 2 && args[0] is byte reg && args[1] is byte value) + if (_EC.ReadByte((byte)i, out byte value)) { - int lowBits = reg & 0x0F, - hiBits = (reg & 0xF0) >> 4; + int lowBits = i & 0x0F, + hiBits = (i & 0xF0) >> 4; + + // keep the default console colour in case it was + // changed with e.g. the `color` command ConsoleColor original = Console.ForegroundColor; // write hex value Console.SetCursorPosition(6 + lowBits * 3, 4 + hiBits); - if (ECValues[reg].Value == value) + if (values[i].Value == value) { - ECValues[reg].Age++; + values[i].Age++; Console.ForegroundColor = ConsoleColor.DarkRed; } else { - ECValues[reg].Value = value; - ECValues[reg].Age = 0; + values[i].Value = value; + values[i].Age = 0; Console.ForegroundColor = ConsoleColor.Green; } @@ -216,7 +154,7 @@ private static void ServerMessage(object sender, PipeMessageEventArgs 126) + if (value < 32 || value > 126) { // unprintable non-extended ASCII char Console.Write('.'); @@ -226,20 +164,36 @@ private static void ServerMessage(object sender, PipeMessageEventArgs e) + private struct ECValue { - throw e.Exception; + /// + /// The EC value itself. + /// + public int Value; + + /// + /// How many EC polls it's been since was last updated. + /// + public int Age; } } diff --git a/YAMDCC.ECInspector/Strings.cs b/YAMDCC.ECInspector/Strings.cs deleted file mode 100644 index 869d30b..0000000 --- a/YAMDCC.ECInspector/Strings.cs +++ /dev/null @@ -1,56 +0,0 @@ -// This file is part of YAMDCC (Yet Another MSI Dragon Center Clone). -// Copyright © Sparronator9999 and Contributors 2023-2025. -// -// YAMDCC is free software: you can redistribute it and/or modify it -// under the terms of the GNU General Public License as published by the Free -// Software Foundation, either version 3 of the License, or (at your option) -// any later version. -// -// YAMDCC is distributed in the hope that it will be useful, but -// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for -// more details. -// -// You should have received a copy of the GNU General Public License along with -// YAMDCC. If not, see . - -using System.Globalization; -using System.Resources; - -namespace YAMDCC.ECInspector; - -/// -/// A resource class for retrieving strings. -/// -internal static class Strings -{ - private static ResourceManager resMan; - - /// - /// Gets a string from the underlying resource file, and - /// replaces format objects with their string representation. - /// - /// - /// The name of the string to find. - /// - /// - /// The objects to format the string with. - /// - /// - /// - /// The formatted string corresponding to - /// the specified string name, if found. - /// - /// if the string couldn't be found. - /// - public static string GetString(string name, params object[] args) - { - CultureInfo ci = CultureInfo.InvariantCulture; - resMan ??= new ResourceManager(typeof(Strings)); - - string temp = resMan.GetString(name, ci); - return temp is null - ? null - : string.Format(ci, temp, args); - } -} diff --git a/YAMDCC.ECInspector/Strings.resx b/YAMDCC.ECInspector/Strings.resx deleted file mode 100644 index 3f3d128..0000000 --- a/YAMDCC.ECInspector/Strings.resx +++ /dev/null @@ -1,158 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - ERROR: unknown command: {0} - - - - YAMDCC EC inspection utility - -OS version: {0} -App version: {1} -Revision (git): {2} - -Usage: {3} <command> [<args>] - -Commands: - --help, -h Print this help screen - --version, -v Print the program version - --dump, -d Dump all EC registers - --monitor, -m Dump EC and monitor for changes - --interval, -i <seconds> EC polling interval - - - ERROR: please re-run this program as admin. - - - ERROR: no command specified - - - - ERROR: failed to connect to YAMDCC service! - - - ERROR: the YAMDCC service is not installed. -Please run the YAMDCC config editor to install the service. - - - ERROR: the YAMDCC service is not running. -Please run the YAMDCC config editor to start the service. - - \ No newline at end of file diff --git a/YAMDCC.ECInspector/YAMDCC.ECInspector.csproj b/YAMDCC.ECInspector/YAMDCC.ECInspector.csproj index 369c513..c1a9b4f 100644 --- a/YAMDCC.ECInspector/YAMDCC.ECInspector.csproj +++ b/YAMDCC.ECInspector/YAMDCC.ECInspector.csproj @@ -26,14 +26,6 @@ - - - - - - - - Designer - + diff --git a/YAMDCC.ECInspector/app.manifest b/YAMDCC.ECInspector/app.manifest index 125f9da..e7a7341 100644 --- a/YAMDCC.ECInspector/app.manifest +++ b/YAMDCC.ECInspector/app.manifest @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/YAMDCC.ECInspector/ec.ico b/YAMDCC.ECInspector/ec.ico new file mode 100644 index 0000000..8f5b27b Binary files /dev/null and b/YAMDCC.ECInspector/ec.ico differ diff --git a/YAMDCC.ECInspector/packages.lock.json b/YAMDCC.ECInspector/packages.lock.json index a9b38e3..5cbef6f 100644 --- a/YAMDCC.ECInspector/packages.lock.json +++ b/YAMDCC.ECInspector/packages.lock.json @@ -2,98 +2,11 @@ "version": 1, "dependencies": { ".NETFramework,Version=v4.8": { - "MessagePack": { - "type": "Transitive", - "resolved": "3.1.3", - "contentHash": "UiNv3fknvPzh5W+S0VV96R17RBZQQU71qgmsMnjjRZU2rtQM/XcTnOB+klT2dA6T1mxjnNKYrEm164AoXvGmYg==", - "dependencies": { - "MessagePack.Annotations": "3.1.3", - "MessagePackAnalyzer": "3.1.3", - "Microsoft.Bcl.AsyncInterfaces": "8.0.0", - "Microsoft.NET.StringTools": "17.11.4", - "System.Collections.Immutable": "8.0.0", - "System.Memory": "4.5.5", - "System.Runtime.CompilerServices.Unsafe": "6.0.0", - "System.Threading.Tasks.Extensions": "4.5.4" - } - }, - "MessagePack.Annotations": { - "type": "Transitive", - "resolved": "3.1.3", - "contentHash": "XTy4njgTAf6UVBKFj7c7ad5R0WVKbvAgkbYZy4f00kplzX2T3VOQ34AUke/Vn/QgQZ7ETdd34/IDWS3KBInSGA==" - }, - "MessagePackAnalyzer": { - "type": "Transitive", - "resolved": "3.1.3", - "contentHash": "19u1oVNv2brCs5F/jma8O8CnsKMMpYwNqD0CAEDEzvqwDTAhqC9r7xHZP4stPb3APs/ryO/zVn7LvjoEHfvs7Q==" - }, - "Microsoft.Bcl.AsyncInterfaces": { - "type": "Transitive", - "resolved": "8.0.0", - "contentHash": "3WA9q9yVqJp222P3x1wYIGDAkpjAku0TMUaaQV22g6L67AI0LdOIrVS7Ht2vJfLHGSPVuqN94vIr15qn+HEkHw==", - "dependencies": { - "System.Threading.Tasks.Extensions": "4.5.4" - } - }, - "Microsoft.NET.StringTools": { - "type": "Transitive", - "resolved": "17.11.4", - "contentHash": "mudqUHhNpeqIdJoUx2YDWZO/I9uEDYVowan89R6wsomfnUJQk6HteoQTlNjZDixhT2B4IXMkMtgZtoceIjLRmA==", - "dependencies": { - "System.Memory": "4.5.5", - "System.Runtime.CompilerServices.Unsafe": "6.0.0" - } - }, - "System.Buffers": { - "type": "Transitive", - "resolved": "4.5.1", - "contentHash": "Rw7ijyl1qqRS0YQD/WycNst8hUUMgrMH4FCn1nNm27M4VxchZ1js3fVjQaANHO5f3sN4isvP4a+Met9Y4YomAg==" - }, - "System.Collections.Immutable": { - "type": "Transitive", - "resolved": "8.0.0", - "contentHash": "AurL6Y5BA1WotzlEvVaIDpqzpIPvYnnldxru8oXJU2yFxFUy3+pNXjXd1ymO+RA0rq0+590Q8gaz2l3Sr7fmqg==", - "dependencies": { - "System.Memory": "4.5.5", - "System.Runtime.CompilerServices.Unsafe": "6.0.0" - } - }, - "System.Memory": { - "type": "Transitive", - "resolved": "4.5.5", - "contentHash": "XIWiDvKPXaTveaB7HVganDlOCRoj03l+jrwNvcge/t8vhGYKvqV+dMv6G4SAX2NoNmN0wZfVPTAlFwZcZvVOUw==", - "dependencies": { - "System.Buffers": "4.5.1", - "System.Numerics.Vectors": "4.5.0", - "System.Runtime.CompilerServices.Unsafe": "4.5.3" - } - }, - "System.Numerics.Vectors": { - "type": "Transitive", - "resolved": "4.5.0", - "contentHash": "QQTlPTl06J/iiDbJCiepZ4H//BVraReU4O4EoRw1U02H5TLUIT7xn3GnDp9AXPSlJUDyFs4uWjWafNX6WrAojQ==" - }, - "System.Runtime.CompilerServices.Unsafe": { - "type": "Transitive", - "resolved": "6.0.0", - "contentHash": "/iUeP3tq1S0XdNNoMz5C9twLSrM/TH+qElHkXWaPvuNOt+99G75NrV0OS2EqHx5wMN7popYjpc8oTjC1y16DLg==" - }, - "System.Threading.Tasks.Extensions": { - "type": "Transitive", - "resolved": "4.5.4", - "contentHash": "zteT+G8xuGu6mS+mzDzYXbzS7rd3K6Fjb9RiZlYlJPam2/hU7JCBZBVEcywNuR+oZ1ncTvc/cq0faRr3P01OVg==", - "dependencies": { - "System.Runtime.CompilerServices.Unsafe": "4.5.3" - } - }, "yamdcc.common": { "type": "Project" }, - "yamdcc.ipc": { - "type": "Project", - "dependencies": { - "MessagePack": "[3.1.3, )" - } + "yamdcc.ecaccess": { + "type": "Project" } }, ".NETFramework,Version=v4.8/win-x64": {},