Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added logger and added color handling for the different types of messages #11

Merged
merged 3 commits into from
Mar 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions SunshineGameFinder/Logger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace SunshineGameFinder
{
internal class Logger
{
public static void Log(string message)
{
Log(message, LogLevel.Information);
}

public static void Log(string message, LogLevel level)
{
switch (level)
{
case LogLevel.Error:
Console.ForegroundColor = ConsoleColor.Red;
break;
case LogLevel.Warning:
Console.ForegroundColor = ConsoleColor.Yellow;
break;
case LogLevel.Success:
Console.ForegroundColor = ConsoleColor.Green;
break;
default:
Console.ForegroundColor = ConsoleColor.White;
break;
}

Console.WriteLine(message);

JasonGNZ marked this conversation as resolved.
Show resolved Hide resolved
}
}

internal enum LogLevel
{
Error,
Warning,
Information,
Success
}

}
21 changes: 11 additions & 10 deletions SunshineGameFinder/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// See https://aka.ms/new-console-template for more information
// See https://aka.ms/new-console-template for more information
using Newtonsoft.Json;
using SunshineGameFinder;
using System.CommandLine;
Expand Down Expand Up @@ -35,32 +35,32 @@

if (!File.Exists(sunshineAppsJson))
{
Console.WriteLine($"Could not find Sunshine Apps config at specified path: {sunshineAppsJson}");
Logger.Log($"Could not find Sunshine Apps config at specified path: {sunshineAppsJson}", LogLevel.Error);
return;
}
var sunshineAppInstance = Newtonsoft.Json.JsonConvert.DeserializeObject<SunshineConfig>(File.ReadAllText(sunshineAppsJson));
foreach (var platformDir in gameDirs)
{
Console.WriteLine($"Scanning for games in {platformDir}...");
Logger.Log("Scanning for games in { platformDir}...");
var di = new DirectoryInfo(platformDir);
if (!di.Exists)
{
Console.WriteLine($"Directory for platform {di.Name} does not exist, skipping...");
Logger.Log($"Directory for platform {di.Name} does not exist, skipping...", LogLevel.Warning);
continue;
}
foreach (var gameDir in di.GetDirectories())
{
Console.WriteLine($"Looking for game exe in {gameDir}...");
Logger.Log($"Looking for game exe in {gameDir}...");
var gameName = gameDir.Name;
if (exclusionWords.Any(ew => gameName.Contains(ew)))
{
Console.WriteLine($"Skipping {gameName} as it was an excluded word match...");
Logger.Log($"Skipping {gameName} as it was an excluded word match...");
continue;
}
var exe = Directory.GetFiles(gameDir.FullName, "*.exe", SearchOption.AllDirectories).FirstOrDefault(exefile => !exeExclusionWords.Any(ew => new FileInfo(exefile).Name.Contains(ew)));
if (string.IsNullOrEmpty(exe))
{
Console.WriteLine($"EXE could not be found for game '{gameName}'");
Logger.Log($"EXE could not be found for game '{gameName}'", LogLevel.Warning);
continue;
}

Expand Down Expand Up @@ -89,17 +89,18 @@
workingdir = ""
};
}
Console.WriteLine($"Adding new game to Sunshine apps: {gameName} - {exe}");
Logger.Log($"Adding new game to Sunshine apps: {gameName} - {exe}");
sunshineAppInstance.apps.Add(existingApp);
}
else
{
Console.WriteLine($"Found existing Sunshine app for {gameName} already!: " + (existingApp.cmd ?? existingApp.detached.FirstOrDefault() ?? existingApp.name).Trim());
Logger.Log($"Found existing Sunshine app for {gameName} already!: " + (existingApp.cmd ?? existingApp.detached.FirstOrDefault() ?? existingApp.name).Trim());
}
}
}
Console.WriteLine("Complete!");
Logger.Log("Finding Games Completed");
File.WriteAllText(sunshineAppsJson, JsonConvert.SerializeObject(sunshineAppInstance, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }));
Logger.Log("Saving Changes!", LogLevel.Success);

}, addlDirectoriesOption, addlExeExclusionWords, sunshineConfigLocationOption);
rootCommand.Invoke(args);