|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using HASS.Agent.AutoImport.Models; |
| 8 | +using HASS.Agent.Shared.Functions; |
| 9 | +using Newtonsoft.Json; |
| 10 | +using Serilog; |
| 11 | + |
| 12 | +namespace HASS.Agent.AutoImport.Functions |
| 13 | +{ |
| 14 | + internal static class HelperFunctions |
| 15 | + { |
| 16 | + /// <summary> |
| 17 | + /// Prints initial info to the console |
| 18 | + /// </summary> |
| 19 | + internal static void PrepareConsole() |
| 20 | + { |
| 21 | + Console.Title = "HASS.Agent AutoImport"; |
| 22 | + Environment.ExitCode = -1; |
| 23 | + |
| 24 | + Console.WriteLine(""); |
| 25 | + Console.WriteLine($"HASS.Agent AutoImport [{Variables.Version}]"); |
| 26 | + Console.WriteLine(); |
| 27 | + Console.WriteLine("[https://github.com/LAB02-Research/HASS.Agent.AutoImport]"); |
| 28 | + Console.WriteLine(); |
| 29 | + Console.WriteLine("This tool will attempt to parse and import all shortcuts from the configured folder."); |
| 30 | + Console.WriteLine("Released under the MIT license, and is completely free to use and distribute."); |
| 31 | + Console.WriteLine(); |
| 32 | + Console.WriteLine(); |
| 33 | + } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Checks whether the process is currently running under the current user |
| 37 | + /// </summary> |
| 38 | + /// <param name="process"></param> |
| 39 | + /// <returns></returns> |
| 40 | + internal static bool IsProcessActiveUnderCurrentUser(string process) |
| 41 | + { |
| 42 | + try |
| 43 | + { |
| 44 | + if (process.Contains('.')) process = Path.GetFileNameWithoutExtension(process); |
| 45 | + var isRunning = false; |
| 46 | + var currentUser = Environment.UserName; |
| 47 | + |
| 48 | + var procs = Process.GetProcessesByName(process); |
| 49 | + |
| 50 | + foreach (var proc in procs) |
| 51 | + { |
| 52 | + try |
| 53 | + { |
| 54 | + if (isRunning) continue; |
| 55 | + var owner = SharedHelperFunctions.GetProcessOwner(proc, false); |
| 56 | + if (owner != currentUser) continue; |
| 57 | + |
| 58 | + isRunning = true; |
| 59 | + } |
| 60 | + finally |
| 61 | + { |
| 62 | + proc.Dispose(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // done |
| 67 | + return isRunning; |
| 68 | + } |
| 69 | + catch (Exception ex) |
| 70 | + { |
| 71 | + Log.Fatal(ex, "[PROCESS] [{process}] Error while determining if process is running: {msg}", process, ex.Message); |
| 72 | + return false; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Attempts to kill the process, running under the current user |
| 78 | + /// </summary> |
| 79 | + /// <param name="process"></param> |
| 80 | + /// <returns></returns> |
| 81 | + internal static bool CloseProcess(string process) |
| 82 | + { |
| 83 | + try |
| 84 | + { |
| 85 | + if (process.Contains('.')) process = Path.GetFileNameWithoutExtension(process); |
| 86 | + var isKilled = false; |
| 87 | + var currentUser = Environment.UserName; |
| 88 | + |
| 89 | + var procs = Process.GetProcessesByName(process); |
| 90 | + |
| 91 | + foreach (var proc in procs) |
| 92 | + { |
| 93 | + try |
| 94 | + { |
| 95 | + if (isKilled) continue; |
| 96 | + var owner = SharedHelperFunctions.GetProcessOwner(proc, false); |
| 97 | + if (owner != currentUser) continue; |
| 98 | + |
| 99 | + proc.Kill(); |
| 100 | + isKilled = true; |
| 101 | + } |
| 102 | + finally |
| 103 | + { |
| 104 | + proc.Dispose(); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + // done |
| 109 | + return isKilled; |
| 110 | + } |
| 111 | + catch (Exception ex) |
| 112 | + { |
| 113 | + Log.Fatal(ex, "[PROCESS] [{process}] Error while closing process: {msg}", process, ex.Message); |
| 114 | + return false; |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments