Skip to content

Commit

Permalink
Add verb support.
Browse files Browse the repository at this point in the history
Colorize various output messages that are important, such as warnings, errors, log file, and if an update is available.
  • Loading branch information
Gwindalmir committed Sep 4, 2021
1 parent 87a2798 commit bf87e01
Show file tree
Hide file tree
Showing 10 changed files with 430 additions and 39 deletions.
67 changes: 48 additions & 19 deletions WorkshopToolCommon/GameBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Diagnostics;
using VRage;
using CommandLine;
using Phoenix.WorkshopTool.Options;
#if SE
using ParallelTasks;
#else
Expand Down Expand Up @@ -116,7 +117,7 @@ private static string ResolveFromRoot(string assemblyname, string ext, string ro

private void HandleInputError()
{
Console.WriteLine("You have an error in one or more of your arguments.");
ProgramBase.ConsoleWriteColored(ConsoleColor.Red, () => Console.Error.WriteLine("You have an error in one or more of your arguments."));
if (Debugger.IsAttached)
{
Console.WriteLine("Press any key to exit.");
Expand All @@ -126,12 +127,34 @@ private void HandleInputError()

public virtual int InitGame(string[] args)
{
var options = new Options();
var parser = new CommandLine.Parser(with => with.HelpWriter = Console.Error);
ProcessedOptions options = default(ProcessedOptions);
var parser = new CommandLine.Parser(with => with.HelpWriter = null);

var result = parser.ParseArguments<Options>(args).WithNotParsed(l => HandleInputError());

if (result.Tag == ParserResultType.Parsed)
var result = parser.ParseArguments<DownloadVerb, UploadVerb, CloudVerb>(args)
.WithParsed(o => options = (ProcessedOptions)o)
.WithNotParsed(l =>
{
parser.ParseArguments<LegacyOptions>(args)
.WithParsed(o =>
{
options = (ProcessedOptions)o;
ProgramBase.ConsoleWriteColored(ConsoleColor.Yellow, () => Console.Error.WriteLine("You are using the legacy command-line arguments."));
string newargs = null;
if (options.Upload)
newargs = parser.FormatCommandLine((UploadVerb)options);
else if (options.Download)
newargs = parser.FormatCommandLine((DownloadVerb)options);
else if (options.Type == typeof(CloudVerb))
newargs = parser.FormatCommandLine((CloudVerb)options);
if (newargs != null)
ProgramBase.ConsoleWriteColored(ConsoleColor.Yellow, () => Console.Error.WriteLine($"Use this instead: {Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName)} {newargs}"));
})
.WithNotParsed(e => HandleInputError());
});

if (options != default(ProcessedOptions))
{
if (options.ModPaths == null &&
options.Blueprints == null &&
Expand All @@ -142,9 +165,9 @@ public virtual int InitGame(string[] args)
options.Worlds == null &&
options.Collections == null)
{
if (!options.ClearSteamCloud && !options.ListDLCs)
if (!options.Clear && !options.ListCloud && !options.ListDLCs)
{
Console.WriteLine(CommandLine.Text.HelpText.AutoBuild<Options>(result, null, null).ToString());
Console.WriteLine(CommandLine.Text.HelpText.AutoBuild(result, null, null).ToString());
return Cleanup(1);
}
}
Expand Down Expand Up @@ -185,15 +208,17 @@ public virtual int InitGame(string[] args)
if (options.Download)
return Cleanup(3);

options.Upload = false;
// TODO
//options.Upload = false;
}

MySandboxGame.Log.WriteLineAndConsole($"{AppName} {Assembly.GetExecutingAssembly().GetName().Version}");

ProgramBase.CheckForUpdate(MySandboxGame.Log.WriteLineAndConsole);

MySandboxGame.Log.WriteLineToConsole(string.Empty);
MySandboxGame.Log.WriteLineAndConsole($"Log file: {MySandboxGame.Log.GetFilePath()}");
ProgramBase.ConsoleWriteColored(ConsoleColor.Gray, () =>
MySandboxGame.Log.WriteLineAndConsole($"Log file: {MySandboxGame.Log.GetFilePath()}"));
MySandboxGame.Log.WriteLineToConsole(string.Empty);

// Make sure file paths are properly rooted based on the user's current directory at launch
Expand Down Expand Up @@ -228,8 +253,8 @@ public virtual int InitGame(string[] args)

if (options.Download)
Task = DownloadMods(options);
else if (options.ClearSteamCloud)
Task = ClearSteamCloud(options.DeleteSteamCloudFiles.ToArray(), options.Force);
else if (options.Clear)
Task = ClearSteamCloud(options.Files.ToArray(), options.Force);
else if (options.ListDLCs)
Task = System.Threading.Tasks.Task<bool>.Factory.StartNew(()=> { ListDLCs(); return true; });
else
Expand Down Expand Up @@ -260,6 +285,10 @@ public virtual int InitGame(string[] args)
if (!Task.Result)
return Cleanup(-1);
}
else
{
ProgramBase.ConsoleWriteColored(ConsoleColor.Yellow, () => Console.Error.WriteLine(CommandLine.Text.HelpText.AutoBuild(result).ToString()));
}

return Cleanup();
}
Expand Down Expand Up @@ -474,7 +503,7 @@ private System.Threading.Tasks.Task<bool> ClearSteamCloud(string [] filesToDelet
#endregion

#region Upload
static System.Threading.Tasks.Task<bool> UploadMods(Options options)
static System.Threading.Tasks.Task<bool> UploadMods(ProcessedOptions options)
{
MySandboxGame.Log.WriteLineAndConsole(string.Empty);

Expand Down Expand Up @@ -517,7 +546,7 @@ static System.Threading.Tasks.Task<bool> UploadMods(Options options)
return Task;
}

static bool ProcessItemsUpload(WorkshopType type, List<string> paths, Options options)
static bool ProcessItemsUpload(WorkshopType type, List<string> paths, ProcessedOptions options)
{
bool success = true;
for (int idx = 0; idx < paths.Count; idx++)
Expand Down Expand Up @@ -588,7 +617,7 @@ static bool ProcessItemsUpload(WorkshopType type, List<string> paths, Options op
}
}

var mod = new Uploader(type, pathname, tags, options.ExcludeExtensions.ToArray(), options.IgnorePaths.ToArray(), options.Compile, options.DryRun, options.Development, options.Visibility, options.Force, options.Thumbnail, options.DLCs.ToArray(), options.Dependencies.ToArray(), description, changelog);
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);
if (options.UpdateOnly && ((IMod)mod).ModId == 0)
{
MySandboxGame.Log.WriteLineAndConsole(string.Format("--update-only passed, skipping: {0}", mod.Title));
Expand Down Expand Up @@ -647,7 +676,7 @@ static bool ProcessItemsUpload(WorkshopType type, List<string> paths, Options op
#endregion Upload

#region Download
static System.Threading.Tasks.Task<bool> DownloadMods(Options options)
static System.Threading.Tasks.Task<bool> DownloadMods(ProcessedOptions options)
{
// Get PublishItemBlocking internal method via reflection
MySandboxGame.Log.WriteLineAndConsole(string.Empty);
Expand Down Expand Up @@ -695,9 +724,9 @@ static System.Threading.Tasks.Task<bool> DownloadMods(Options options)
return Task;
}

static bool ProcessItemsDownload(WorkshopType type, IEnumerable<string> paths, Options options)
static bool ProcessItemsDownload(WorkshopType type, IEnumerable<string> paths, ProcessedOptions options)
{
if (paths == null)
if (paths == null || paths?.Count() == 0 )
return true;

var width = Console.IsOutputRedirected ? 256 : Console.WindowWidth;
Expand Down Expand Up @@ -855,7 +884,7 @@ static bool ProcessItemsDownload(WorkshopType type, IEnumerable<string> paths, O
#region Pathing
static string[] TestPathAndMakeAbsolute(WorkshopType type, IEnumerable<string> pathsin)
{
var paths = pathsin.ToArray();
var paths = pathsin?.ToArray();
for (int idx = 0; paths != null && idx < paths.Length; idx++)
{
// If the passed in path doesn't exist, and is relative, try to match it with the expected data directory
Expand Down
21 changes: 21 additions & 0 deletions WorkshopToolCommon/Options/CloudVerb.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using CommandLine;
using System;
using System.Collections.Generic;
using System.Text;

namespace Phoenix.WorkshopTool.Options
{
[Verb("cloud", isDefault: false, HelpText = "Interact with saved files on the Steam Cloud.")]
public class CloudVerb : OptionBase
{
[Option('l', "list", Default = true, SetName = "list", HelpText = "List files stored in the cloud.")]
public bool List { get; set; }

[Option("clear", Default = false, SetName = "clear", HelpText = "Clear Steam Cloud (WARNING!). THIS WILL DELETE YOUR STEAM CLOUD FOR SE! Use with --force to actually delete.")]
public bool Clear { get; set; }

[Option("delete", SetName = "delete", HelpText = "Delete individual file or files from the Steam Cloud")]
public IEnumerable<string> Files { get; set; }

}
}
17 changes: 17 additions & 0 deletions WorkshopToolCommon/Options/DownloadVerb.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using CommandLine;
using System;
using System.Collections.Generic;
using System.Text;

namespace Phoenix.WorkshopTool.Options
{
public class DownloadVerb : UGCOptionBase
{
[Option("ids", Min = 1, SetName = "mod", HelpText = "Workshop IDs of items or collections to download")]
public IEnumerable<ulong> Ids { get; set; }

[Option('E', "no-extract", Default = false, HelpText = "Don't automatically extract downloaded workshop items to AppData.")]
public bool Extract { get; set; }

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Phoenix.WorkshopTool
{
public sealed class Options
public sealed class LegacyOptions : Options.OptionBase
{
private const string OptionSet = "MainFunctions";

Expand Down Expand Up @@ -38,9 +38,6 @@ public sealed class Options
[Option("ignore", HelpText = "List of paths to exclude from upload")]
public IEnumerable<string> IgnorePaths { get; set; }

[Option('f', "force", Default = false, HelpText = "Force operation. USE WITH CAUTION! (not valid everywhere)")]
public bool Force { get; set; }

[Option('m', "mods", HelpText = "List of directories of mods to upload; or Workshop ID of mods to download (when in download mode), use quotes if spaces")]
public IEnumerable<string> ModPaths { get; set; }

Expand All @@ -52,11 +49,11 @@ public sealed class Options

[Option('w', "worlds", HelpText = "List of directories of worlds to upload; or Workshop ID of worlds to download (when in download mode)")]
public IEnumerable<string> Worlds { get; set; }

#if SE
[Option('i', "scripts", HelpText = "List of directories of scripts to upload; or Workshop ID of scripts to download (when in download mode)")]
public IEnumerable<string> IngameScripts { get; set; }
#endif
public IEnumerable<string> IngameScripts { get; set; }

[Option('t', "tags", HelpText = "List of workshop mod categories/tags to use (removes previous, default is keep existing)")]
public IEnumerable<string> Tags { get; set; }

Expand All @@ -71,28 +68,18 @@ public sealed class Options

[Option("deletecloudfile", HelpText = "Delete individual file or files from the Steam Cloud")]
public IEnumerable<string> DeleteSteamCloudFiles { get; set; }

#if SE
[Option("listdlc", HelpText = "List available DLCs", SetName = "dlc")]
#endif
public bool ListDLCs { get; set; }

#if SE
[Option("dlc", HelpText = "Add DLC dependency to mod, accepts numeric ID or name. Use 0 or None to remove all DLC.")]
#endif
public IEnumerable<string> DLCs { get; set; }

#if SE
[Option("modio", HelpText = "Use mod.io by default.")]
#endif
public bool ModIO { get; set; } = false;

[Option("dependencies", HelpText = "Specify dependencies to other mods (modids only). Use 0 to remove all.")]
public IEnumerable<ulong> Dependencies { get; set; }

[Option("appdata", Default = "%AppData%\\SpaceEngineers", HelpText = "Specify custom AppData location")]
public string AppData { get; set; }

[Option("description", HelpText = "File containing the description to set for workshop item")]
public string DescriptionFile { get; set; }

Expand Down
21 changes: 21 additions & 0 deletions WorkshopToolCommon/Options/OptionBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using CommandLine;
using System;
using System.Collections.Generic;
using System.Text;

namespace Phoenix.WorkshopTool.Options
{
public abstract class OptionBase
{
#if SE
[Option("modio", HelpText = "Use mod.io by default.")]
#endif
public bool ModIO { get; set; } = false;

[Option("appdata", Default = "%AppData%\\SpaceEngineers", HelpText = "Specify custom AppData location")]
public string AppData { get; set; }

[Option('f', "force", Default = false, HelpText = "Force operation. USE WITH CAUTION! (not valid everywhere)")]
public bool Force { get; set; }
}
}
Loading

0 comments on commit bf87e01

Please sign in to comment.