Skip to content

Commit c002a00

Browse files
author
LAB02 Research
committed
2022.1.0
Initial version
1 parent baeadf4 commit c002a00

File tree

11 files changed

+882
-0
lines changed

11 files changed

+882
-0
lines changed

src/HASS.Agent.AutoImport.sln

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.4.33205.214
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HASS.Agent.AutoImport", "HASS.Agent.AutoImport\HASS.Agent.AutoImport.csproj", "{FF6C1526-A450-4879-A987-D322F5E23F9A}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{FF6C1526-A450-4879-A987-D322F5E23F9A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{FF6C1526-A450-4879-A987-D322F5E23F9A}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{FF6C1526-A450-4879-A987-D322F5E23F9A}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{FF6C1526-A450-4879-A987-D322F5E23F9A}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
VisualSVNWorkingCopyRoot = .
24+
SolutionGuid = {1B6D3ECD-2A0D-498C-8868-D6235629FAD0}
25+
EndGlobalSection
26+
EndGlobal
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0-windows10.0.17763.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>disable</Nullable>
8+
<Version>2022.1.0</Version>
9+
<Authors>LAB02 Research</Authors>
10+
<Company>LAB02 Research</Company>
11+
<ApplicationIcon>hassagent.ico</ApplicationIcon>
12+
</PropertyGroup>
13+
14+
<ItemGroup>
15+
<Content Include="hassagent.ico" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<PackageReference Include="HASS.Agent.Shared" Version="2022.24.0" />
20+
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
21+
<PackageReference Include="securifybv.ShellLink" Version="0.1.0" />
22+
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
23+
</ItemGroup>
24+
25+
</Project>

0 commit comments

Comments
 (0)