Skip to content

Commit

Permalink
chore: Minor namespaces change
Browse files Browse the repository at this point in the history
  • Loading branch information
Hidden Space committed Oct 18, 2024
1 parent 678ff71 commit d30dcb0
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 188 deletions.
9 changes: 4 additions & 5 deletions Source/SteamGameTimeBooster/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace SteamGameTimeBooster
namespace SteamGameTimeBooster;

public static class EncryptionConstants
{
public static class EncryptionConstants
{
public const string EncryptedDataFilePath = "sgtb-session.encrypted";
}
public const string EncryptedDataFilePath = "sgtb-session.encrypted";
}
15 changes: 7 additions & 8 deletions Source/SteamGameTimeBooster/DataTransferObjects/SteamSession.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
namespace SteamGameTimeBooster.DataTransferObjects
namespace SteamGameTimeBooster.DataTransferObjects;

public sealed class SteamSession
{
public sealed class SteamSession
{
public string UserName { get; set; } = string.Empty;
public string SessionId { get; set; } = string.Empty;
public string SteamLoginSecure { get; set; } = string.Empty;
public string UserName { get; set; } = string.Empty;
public string SessionId { get; set; } = string.Empty;
public string SteamLoginSecure { get; set; } = string.Empty;

public bool SimpleValidate() => !string.IsNullOrEmpty(UserName) && !string.IsNullOrEmpty(SessionId) && !string.IsNullOrEmpty(SteamLoginSecure);
}
public bool SimpleValidate() => !string.IsNullOrEmpty(UserName) && !string.IsNullOrEmpty(SessionId) && !string.IsNullOrEmpty(SteamLoginSecure);
}
27 changes: 13 additions & 14 deletions Source/SteamGameTimeBooster/Helpers/ConsoleHelper.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
namespace SteamGameTimeBooster.Helpers
namespace SteamGameTimeBooster.Helpers;

public static class ConsoleHelper
{
public static class ConsoleHelper
public static void WriteColored(string text, ConsoleColor color)
{
public static void WriteColored(string text, ConsoleColor color)
{
ConsoleColor originalColor = Console.ForegroundColor;
Console.ForegroundColor = color;
Console.Write(" ");
Console.Write(text);
Console.ForegroundColor = originalColor;
}
ConsoleColor originalColor = Console.ForegroundColor;
Console.ForegroundColor = color;
Console.Write(" ");
Console.Write(text);
Console.ForegroundColor = originalColor;
}

public static void WriteColoredLine(string text, ConsoleColor color)
{
WriteColored(text + Environment.NewLine, color);
}
public static void WriteColoredLine(string text, ConsoleColor color)
{
WriteColored(text + Environment.NewLine, color);
}
}
91 changes: 45 additions & 46 deletions Source/SteamGameTimeBooster/Managers/ProcessManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,71 @@
using System.Diagnostics;
using static SteamGameTimeBooster.Helpers.ConsoleHelper;

namespace SteamGameTimeBooster.Managers
namespace SteamGameTimeBooster.Managers;

internal sealed class ProcessManager
{
internal sealed class ProcessManager
{
public static readonly ProcessManager Instance = new();
public static readonly ProcessManager Instance = new();

private readonly List<Process> _runningProcesses = [];
private readonly List<Process> _runningProcesses = [];

private ProcessManager() { }
private ProcessManager() { }

public async Task StartGameProcess(int gameId, List<Game> userGames, TimeSpan duration, CancellationToken token)
public async Task StartGameProcess(int gameId, List<Game> userGames, TimeSpan duration, CancellationToken token)
{
string appId = userGames.First(x => x.AppId == gameId).AppId.ToString();
Process process = new()
{
string appId = userGames.First(x => x.AppId == gameId).AppId.ToString();
Process process = new()
{
StartInfo = new ProcessStartInfo("steam-idle.exe", appId)
{
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true,
UseShellExecute = false
}
};

try
StartInfo = new ProcessStartInfo("steam-idle.exe", appId)
{
process.Start();
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true,
UseShellExecute = false
}
};

lock (_runningProcesses) { _runningProcesses.Add(process); }
try
{
process.Start();

WriteColoredLine($"▶️ Started process for game {appId}.", ConsoleColor.Green);
lock (_runningProcesses) { _runningProcesses.Add(process); }

await Task.Delay(duration, token);
WriteColoredLine($"▶️ Started process for game {appId}.", ConsoleColor.Green);

if (!process.HasExited)
{
process.Kill();
await Task.Delay(duration, token);

Console.WriteLine();
WriteColoredLine($"⏹️ Process for game {appId} has been terminated after {duration.TotalMinutes} minute(s).", ConsoleColor.Magenta);
}
}
catch (Exception ex)
{
WriteColoredLine($"❌ Error with game {appId}: {ex.Message}.", ConsoleColor.Red);
}
finally
if (!process.HasExited)
{
lock (_runningProcesses) { _runningProcesses.Remove(process); }
process.Dispose();
process.Kill();

Console.WriteLine();
WriteColoredLine($"⏹️ Process for game {appId} has been terminated after {duration.TotalMinutes} minute(s).", ConsoleColor.Magenta);
}
}
catch (Exception ex)
{
WriteColoredLine($"❌ Error with game {appId}: {ex.Message}.", ConsoleColor.Red);
}
finally
{
lock (_runningProcesses) { _runningProcesses.Remove(process); }
process.Dispose();
}
}

public void KillAllProcesses()
public void KillAllProcesses()
{
lock (_runningProcesses)
{
lock (_runningProcesses)
foreach (Process process in _runningProcesses)
{
foreach (Process process in _runningProcesses)
if (!process.HasExited)
{
if (!process.HasExited)
{
process.Kill();
WriteColoredLine($"⏹️ Terminated process ID {process.Id}.", ConsoleColor.Magenta);
}
process.Kill();
WriteColoredLine($"⏹️ Terminated process ID {process.Id}.", ConsoleColor.Magenta);
}
_runningProcesses.Clear();
}
_runningProcesses.Clear();
}
}
}
Loading

0 comments on commit d30dcb0

Please sign in to comment.