Skip to content

Commit

Permalink
Further improve colored output. Add extension methods to clean up the…
Browse files Browse the repository at this point in the history
… code.
  • Loading branch information
Gwindalmir committed Sep 4, 2021
1 parent 3dbfeb8 commit 26a45de
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 39 deletions.
5 changes: 3 additions & 2 deletions SEWorkshopTool/SpaceGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ protected override void AuthenticateWorkshop()
"Authentication to mod.io required." + Environment.NewLine +
"This will create an OAuth2 token for your account." + Environment.NewLine +
"This is a one-time process, and does NOT require your password." + Environment.NewLine +
$"Your email and token will be saved in {config.FilePath}." + Environment.NewLine +
$"Your email and token will be saved in {config.FilePath}." + Environment.NewLine);
ProgramBase.ConsoleWriteColored(ConsoleColor.White,
"Protect this file." + Environment.NewLine);
System.Console.Write("Enter the email associated with your mod.io account: ");
email = System.Console.ReadLine();
Expand Down Expand Up @@ -164,7 +165,7 @@ private void ManuallyAddDLCs()

if (obj == null)
{
MySandboxGame.Log.WriteLineAndConsole(string.Format(Constants.ERROR_Reflection, "MyDLC.ctor"));
MySandboxGame.Log.WriteLineError(string.Format(Constants.ERROR_Reflection, "MyDLC.ctor"));
return;
}

Expand Down
12 changes: 12 additions & 0 deletions WorkshopToolCommon/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ public static void WriteLineToConsole(this MyLog log, string msg)
{
log.WriteLineAndConsole(msg);
}

public static void WriteLineError(this MyLog log, string msg)
{
ProgramBase.ConsoleWriteColored(ConsoleColor.Red, () =>
log.WriteLineAndConsole(msg));
}

public static void WriteLineWarning(this MyLog log, string msg)
{
ProgramBase.ConsoleWriteColored(ConsoleColor.Yellow, () =>
log.WriteLineAndConsole(msg));
}
}

public static class WorkshopIdHelper
Expand Down
36 changes: 16 additions & 20 deletions WorkshopToolCommon/GameBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,9 @@ public virtual int InitGame(string[] args)

if (!SteamAPI.IsSteamRunning())
{
ProgramBase.ConsoleWriteColored(ConsoleColor.Yellow, () =>
{
MySandboxGame.Log.WriteLineAndConsole("ERROR: * Steam not detected. Is Steam running and not as Admin? *");
MySandboxGame.Log.WriteLineAndConsole("* Only compile testing is available. *");
MySandboxGame.Log.WriteLineAndConsole("");
});
MySandboxGame.Log.WriteLineWarning("ERROR: * Steam not detected. Is Steam running and not as Admin? *");
MySandboxGame.Log.WriteLineWarning("* Only compile testing is available. *");
MySandboxGame.Log.WriteLineAndConsole("");

if (options.Download || (options.Upload && !options.Compile))
return Cleanup(3);
Expand Down Expand Up @@ -264,7 +261,7 @@ public virtual int InitGame(string[] args)
if (initmethod != null)
initmethod.Invoke(m_game, null);
else
MySandboxGame.Log.WriteLineAndConsole(string.Format(Constants.ERROR_Reflection, "InitModAPI"));
MySandboxGame.Log.WriteLineError(string.Format(Constants.ERROR_Reflection, "InitModAPI"));
}
#endif
ReplaceMethods();
Expand Down Expand Up @@ -363,14 +360,14 @@ void ReplaceMethod(Type sourceType, string sourceMethod, BindingFlags sourceBind
if (methodtoreplace != null && methodtoinject != null)
MethodUtil.ReplaceMethod(methodtoreplace, methodtoinject);
else
MySandboxGame.Log.WriteLineAndConsole(string.Format(Constants.ERROR_Reflection, sourceMethod));
MySandboxGame.Log.WriteLineError(string.Format(Constants.ERROR_Reflection, sourceMethod));
}

// Returns argument for chaining
private int Cleanup(int errorCode = 0)
{
if (errorCode != 0)
MySandboxGame.Log.WriteLineAndConsole("Check the log file above for error details.");
MySandboxGame.Log.WriteLineError("Check the log file above for error details.");

CleanupSandbox();
return errorCode;
Expand Down Expand Up @@ -451,7 +448,7 @@ protected virtual void InitSandbox(string[] args)
if (initWorkshopMethod != null)
initWorkshopMethod.Invoke(m_game, null);
else
MySandboxGame.Log.WriteLineAndConsole(string.Format(Constants.ERROR_Reflection, "InitSteamWorkshop"));
MySandboxGame.Log.WriteLineError(string.Format(Constants.ERROR_Reflection, "InitSteamWorkshop"));
}
catch (Exception ex)
{
Expand Down Expand Up @@ -602,7 +599,7 @@ static bool ProcessItemsUpload(WorkshopType type, List<string> paths, ProcessedO
if (File.Exists(options.DescriptionFile))
description = File.ReadAllText(options.DescriptionFile);
else
MySandboxGame.Log.WriteLineAndConsole(string.Format("Unable to set description, file does not exist: {0}", options.DescriptionFile));
MySandboxGame.Log.WriteLineWarning(string.Format("Unable to set description, file does not exist: {0}", options.DescriptionFile));
}

// Read the changelog from a file, if detected
Expand Down Expand Up @@ -634,7 +631,6 @@ static bool ProcessItemsUpload(WorkshopType type, List<string> paths, ProcessedO
}

var mod = new Uploader(type, pathname, tags, options.ExcludeExtensions.ToArray(), options.IgnorePaths.ToArray(), options.Compile, options.DryRun, false, options.Visibility, options.Force, options.Thumbnail, options.DLCs.ToArray(), options.Dependencies.ToArray(), description, changelog);
var mod = new Uploader(type, pathname, (UploadVerb)options, tags, description, changelog);
if (options.UpdateOnly && ((IMod)mod).ModId == 0)
{
MySandboxGame.Log.WriteLineAndConsole(string.Format("--update-only passed, skipping: {0}", mod.Title));
Expand All @@ -646,7 +642,7 @@ static bool ProcessItemsUpload(WorkshopType type, List<string> paths, ProcessedO
{
if (!SteamAPI.IsSteamRunning())
{
MySandboxGame.Log.WriteLineAndConsole("Cannot publish, Steam not detected!");
MySandboxGame.Log.WriteLineError("Cannot publish, Steam not detected!");
return false;
}

Expand All @@ -657,14 +653,14 @@ static bool ProcessItemsUpload(WorkshopType type, List<string> paths, ProcessedO
else
{
success = false;
MySandboxGame.Log.WriteLineAndConsole(string.Format("Error occurred: {0}", mod.Title));
MySandboxGame.Log.WriteLineError(string.Format("Error occurred: {0}", mod.Title));
}
}
else
{
if (((IMod)mod).ModId == 0)
{
MySandboxGame.Log.WriteLineAndConsole(string.Format("Mod not published, skipping: {0}", mod.Title));
MySandboxGame.Log.WriteLineWarning(string.Format("Mod not published, skipping: {0}", mod.Title));
success = false;
}
else
Expand All @@ -682,7 +678,7 @@ static bool ProcessItemsUpload(WorkshopType type, List<string> paths, ProcessedO
}
else
{
MySandboxGame.Log.WriteLineAndConsole(string.Format("Skipping {0}: {1}", type.ToString(), mod.Title));
MySandboxGame.Log.WriteLineWarning(string.Format("Skipping {0}: {1}", type.ToString(), mod.Title));
success = false;
}

Expand Down Expand Up @@ -791,7 +787,7 @@ static bool ProcessItemsDownload(WorkshopType type, IEnumerable<string> paths, P
loopsuccess = MyWorkshop.DownloadBlueprintBlocking(item, null);
#endif
if (!loopsuccess)
MySandboxGame.Log.WriteLineAndConsole(string.Format("Download of {0} FAILED!", item.Id));
MySandboxGame.Log.WriteLineError(string.Format("Download of {0} FAILED!", item.Id));
else
success = true;
}
Expand All @@ -804,7 +800,7 @@ static bool ProcessItemsDownload(WorkshopType type, IEnumerable<string> paths, P
{
loopsuccess = MyWorkshop.DownloadScriptBlocking(item);
if (!loopsuccess)
MySandboxGame.Log.WriteLineAndConsole(string.Format("Download of {0} FAILED!", item.Id));
MySandboxGame.Log.WriteLineError(string.Format("Download of {0} FAILED!", item.Id));
else
success = true;
}
Expand All @@ -825,7 +821,7 @@ static bool ProcessItemsDownload(WorkshopType type, IEnumerable<string> paths, P
loopsuccess = MyWorkshop.TryCreateWorldInstanceBlocking(item, pathinfo, out path, false);
if (!loopsuccess)
{
MySandboxGame.Log.WriteLineAndConsole(string.Format("Download of {0} FAILED!", item.Id));
MySandboxGame.Log.WriteLineError(string.Format("Download of {0} FAILED!", item.Id));
}
else
{
Expand All @@ -847,7 +843,7 @@ static bool ProcessItemsDownload(WorkshopType type, IEnumerable<string> paths, P
}
else
{
MySandboxGame.Log.WriteLineAndConsole("Download FAILED!");
MySandboxGame.Log.WriteLineError("Download FAILED!");
return false;
}

Expand Down
5 changes: 5 additions & 0 deletions WorkshopToolCommon/ProgramBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public static void ConsoleWriteColored(ConsoleColor color, Action<string> output
ConsoleWriteColored(color, () => outputMethod?.Invoke(message));
}

public static void ConsoleWriteColored(ConsoleColor color, string message)
{
ConsoleWriteColored(color, () => Console.Out.WriteLine(message));
}

public static void ConsoleWriteColored(ConsoleColor color, Action outputMethod)
{
// Don't colorize output if the terminal is non-interactive, or any output streams are redirected.
Expand Down
40 changes: 23 additions & 17 deletions WorkshopToolCommon/Uploader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ private void SetupReflection()

if (_compileMethod == null)
{
MySandboxGame.Log.WriteLineAndConsole(string.Format(Constants.ERROR_Reflection, "LoadScripts"));
MySandboxGame.Log.WriteLineError(string.Format(Constants.ERROR_Reflection, "LoadScripts"));
}
}
}
Expand Down Expand Up @@ -272,7 +272,7 @@ private void SetupReflection()

if (_publishMethod == null)
{
MySandboxGame.Log.WriteLineAndConsole(string.Format(Constants.ERROR_Reflection, "PublishItemBlocking"));
MySandboxGame.Log.WriteLineError(string.Format(Constants.ERROR_Reflection, "PublishItemBlocking"));
}

if (_globalIgnoredExtensions == null)
Expand All @@ -292,7 +292,7 @@ private void SetupReflection()
}
catch (Exception ex)
{
MySandboxGame.Log.WriteLineAndConsole(string.Format(Constants.ERROR_Reflection, "m_publishSuccess"));
MySandboxGame.Log.WriteLineError(string.Format(Constants.ERROR_Reflection, "m_publishSuccess"));
MySandboxGame.Log.WriteLine(ex.Message);
}
#endif
Expand Down Expand Up @@ -370,33 +370,37 @@ public bool Compile()
}

if( errorCount > 0)
MySandboxGame.Log.WriteLineAndConsole(string.Format("There are {0} compile errors:", errorCount));
MySandboxGame.Log.WriteLineError(string.Format("There are {0} compile errors:", errorCount));
if (warningCount > 0)
MySandboxGame.Log.WriteLineAndConsole(string.Format("There are {0} compile warnings:", warningCount));
MySandboxGame.Log.WriteLineWarning(string.Format("There are {0} compile warnings:", warningCount));

// Output raw message, which is usually in msbuild friendly format, for automated tools
foreach (var error in errors)
{
var color = error.Severity == TErrorSeverity.Warning ? ConsoleColor.Yellow : ConsoleColor.Red;
ProgramBase.ConsoleWriteColored(color, () =>
#if SE
System.Console.WriteLine(error.Message);
System.Console.Error.WriteLine(error.Message)
#else
System.Console.WriteLine(error.Text);
System.Console.Error.WriteLine(error.Text)
#endif

);
}
#if SE
MyDefinitionErrors.Clear(); // Clear old ones, so next mod starts fresh
#endif

if (errorCount > 0)
{
MySandboxGame.Log.WriteLineAndConsole("Compilation FAILED!");
MySandboxGame.Log.WriteLineError("Compilation FAILED!");
return false;
}
}
MySandboxGame.Log.WriteLineAndConsole("Compilation successful!");
}
else
{
MySandboxGame.Log.WriteLineAndConsole(string.Format(Constants.ERROR_Reflection, "LoadScripts"));
MySandboxGame.Log.WriteLineError(string.Format(Constants.ERROR_Reflection, "LoadScripts"));
}
}
#if SE
Expand All @@ -419,7 +423,9 @@ public bool Compile()
int errors = 0;
foreach (var msg in messages)
{
MySandboxGame.Log.WriteLineAndConsole(msg.Text);
var color = msg.IsError ? ConsoleColor.Red : ConsoleColor.Gray;
ProgramBase.ConsoleWriteColored(color, () =>
MySandboxGame.Log.WriteLineAndConsole(msg.Text));

if (msg.IsError)
errors++;
Expand Down Expand Up @@ -449,7 +455,7 @@ public bool Publish()

if(!Directory.Exists(m_modPath))
{
MySandboxGame.Log.WriteLineAndConsole(string.Format("Directory does not exist {0}. Wrong option?", m_modPath ?? string.Empty));
MySandboxGame.Log.WriteLineWarning(string.Format("Directory does not exist {0}. Wrong option?", m_modPath ?? string.Empty));
return false;
}

Expand Down Expand Up @@ -505,15 +511,15 @@ public bool Publish()
}
else
{
MySandboxGame.Log.WriteLineAndConsole(string.Format(Constants.ERROR_Reflection, "PublishItemBlocking"));
MySandboxGame.Log.WriteLineError(string.Format(Constants.ERROR_Reflection, "PublishItemBlocking"));
}

// SE libraries don't support updating dependencies, so we have to do that separately
WorkshopHelper.PublishDependencies(m_modId, m_deps, m_depsToRemove);
}
if (((IMod)this).ModId == 0 || !PublishSuccess)
{
MySandboxGame.Log.WriteLineAndConsole("Upload/Publish FAILED!");
MySandboxGame.Log.WriteLineError("Upload/Publish FAILED!");
return false;
}
else
Expand Down Expand Up @@ -577,8 +583,8 @@ bool FillPropertiesFromPublished()
MyDebug.AssertDebug(owner == MyGameService.UserId);
if (owner != MyGameService.UserId)
{
MySandboxGame.Log.WriteLineAndConsole(string.Format("Owner mismatch! Mod owner: {0}; Current user: {1}", owner, MyGameService.UserId));
MySandboxGame.Log.WriteLineAndConsole("Upload/Publish FAILED!");
MySandboxGame.Log.WriteLineError(string.Format("Owner mismatch! Mod owner: {0}; Current user: {1}", owner, MyGameService.UserId));
MySandboxGame.Log.WriteLineError("Upload/Publish FAILED!");
return false;
}
return true;
Expand Down Expand Up @@ -826,7 +832,7 @@ public bool UpdatePreviewFileOrTags(ulong modId, MyWorkshopItemPublisher publish
MySandboxGame.Log.WriteLineAndConsole(string.Format("Updated thumbnail: {0}", Title));
}
else
MySandboxGame.Log.WriteLineAndConsole(string.Format("Error during publishing: {0}", (object)result));
MySandboxGame.Log.WriteLineError(string.Format("Error during publishing: {0}", (object)result));
resetEvent.Set();
});

Expand Down

0 comments on commit 26a45de

Please sign in to comment.