Skip to content

Commit

Permalink
Cleanup and reorganization.
Browse files Browse the repository at this point in the history
Move tag parsing to ProcessedOptions. Started work on adding options to specify tags to add and remove separately from the entire list.
  • Loading branch information
Gwindalmir committed Sep 4, 2021
1 parent 26a45de commit 528c355
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 44 deletions.
18 changes: 6 additions & 12 deletions WorkshopToolCommon/GameBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public virtual int InitGame(string[] args)
else
{
if (options.Ids == null &&
options.ModPaths == null &&
options.Mods == null &&
options.Blueprints == null &&
#if SE
options.IngameScripts == null &&
Expand Down Expand Up @@ -528,7 +528,7 @@ static System.Threading.Tasks.Task<bool> UploadMods(ProcessedOptions options)
List<string> itemPaths;
// Process mods
itemPaths = GetGlobbedPaths(TestPathAndMakeAbsolute(WorkshopType.Mod, options.ModPaths));
itemPaths = GetGlobbedPaths(TestPathAndMakeAbsolute(WorkshopType.Mod, options.Mods));
if (!ProcessItemsUpload(WorkshopType.Mod, itemPaths, options))
success = false;
Expand Down Expand Up @@ -577,13 +577,7 @@ static bool ProcessItemsUpload(WorkshopType type, List<string> paths, ProcessedO
pathname = paths[idx];
}

var tags = options.Tags.ToArray();

// If user comma-separated the tags, split them
if(tags != null && tags.Length == 1)
{
tags = tags[0].Split(',', ';');
}
var tags = options.Tags?.ToArray();

if (!string.IsNullOrEmpty(options.Thumbnail) &&
!Path.IsPathRooted(options.Thumbnail))
Expand Down Expand Up @@ -630,7 +624,7 @@ 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, 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 @@ -709,7 +703,7 @@ static System.Threading.Tasks.Task<bool> DownloadMods(ProcessedOptions options)
options.Collections.ForEach(s => items.AddRange(WorkshopHelper.GetCollectionDetails(ulong.Parse(s))));
items.AddRange(WorkshopHelper.GetItemDetails(options.Ids));
options.ModPaths = CombineCollectionWithList(WorkshopType.Mod, items, options.ModPaths);
options.Mods = CombineCollectionWithList(WorkshopType.Mod, items, options.Mods);
options.Blueprints = CombineCollectionWithList(WorkshopType.Blueprint, items, options.Blueprints);
#if SE
options.IngameScripts = CombineCollectionWithList(WorkshopType.IngameScript, items, options.IngameScripts);
Expand All @@ -718,7 +712,7 @@ static System.Threading.Tasks.Task<bool> DownloadMods(ProcessedOptions options)
options.Scenarios = CombineCollectionWithList(WorkshopType.Scenario, items, options.Scenarios);
}
if (!ProcessItemsDownload(WorkshopType.Mod, options.ModPaths, options))
if (!ProcessItemsDownload(WorkshopType.Mod, options.Mods, options))
success = false;
if (!ProcessItemsDownload(WorkshopType.Blueprint, options.Blueprints, options))
success = false;
Expand Down
75 changes: 66 additions & 9 deletions WorkshopToolCommon/Options/ProcessedOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,60 @@ public class ProcessedOptions
public bool DryRun { get; set; }
public bool Compile { get; set; }
public IList<string> Collections { get; set; }
public IList<string> ModPaths { get; set; }
public IList<string> Mods { get; set; }
public IList<string> Blueprints { get; set; }
public IList<string> Scenarios { get; set; }
public IList<string> Worlds { get; set; }
public IList<string> IngameScripts { get; set; }
public IList<string> ExcludeExtensions { get; set; }
public IList<string> IgnorePaths { get; set; }
public IList<string> Tags { get; set; }

IList<string> _tags;
public IList<string> Tags
{
get => _tags;
set
{
// If user comma-separated the tags, split them
if (value != null)
{
var tags = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
value.ForEach(s => s.Split(',', ';').ForEach(t => tags.Add(t)));
_tags = tags.ToList();
}
}
}

IList<string> _tagsToAdd;
public IList<string> TagsToAdd
{
get => _tagsToAdd;
set
{
if (value != null)
{
var tags = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
value.ForEach(s => s.Split(',', ';').ForEach(t => tags.Add(t)));
_tagsToAdd = tags.ToList();
}
}
}

IList<string> _tagsToRemove;
public IList<string> TagsToRemove
{
get => _tagsToAdd;
set
{
if (value != null)
{
var tags = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
value.ForEach(s => s.Split(',', ';').ForEach(t => tags.Add(t)));
_tagsToAdd = tags.ToList();
}
}
}

public IList<string> DLCs { get; set; }
public IList<ulong> Dependencies { get; set; }
public string Thumbnail { get; set; }
Expand All @@ -45,7 +91,7 @@ public class ProcessedOptions
public bool Clear { get; set; }
public IEnumerable<string> Files { get; set; }

// TODO
// Currently relies on LegacyOptions
public bool ListDLCs { get; set; }

public ProcessedOptions(DownloadVerb options)
Expand All @@ -66,14 +112,16 @@ public ProcessedOptions(UploadVerb options)
Changelog = options.Changelog;
Visibility = options.Visibility;

ModPaths = options.ModPaths?.ToList();
Mods = options.Mods?.ToList();
Blueprints = options.Blueprints?.ToList();
Scenarios = options.Scenarios?.ToList();
IngameScripts = options.IngameScripts?.ToList();

ExcludeExtensions = options.ExcludeExtensions?.ToList();
IgnorePaths = options.IgnorePaths?.ToList();
Tags = options.Tags?.ToList();
TagsToAdd = options.TagsToAdd?.ToList();
TagsToRemove = options.TagsToRemove?.ToList();
DLCs = options.DLCs?.ToList();
Dependencies = options.Dependencies?.ToList();
}
Expand All @@ -82,7 +130,7 @@ public ProcessedOptions(CompileVerb options)
: this((OptionBase)options)
{
Compile = true;
ModPaths = options.ModPaths?.ToList();
Mods = options.Mods?.ToList();
IngameScripts = options.IngameScripts?.ToList();
}

Expand Down Expand Up @@ -123,13 +171,19 @@ public ProcessedOptions(LegacyOptions options)
Changelog = options.Changelog;
Visibility = options.Visibility;

ModPaths = options.ModPaths?.ToList();
Mods = options.ModPaths?.ToList();
Blueprints = options.Blueprints?.ToList();
Scenarios = options.Scenarios?.ToList();
IngameScripts = options.IngameScripts?.ToList();

var allids = new HashSet<ulong>();
ModPaths?.ForEach(s => allids.Add(ulong.Parse(s)));
Mods?.ForEach(s =>
{
ulong val;
if (ulong.TryParse(s, out val))
allids.Add(val);
});

Blueprints?.ForEach(s => allids.Add(ulong.Parse(s)));
Scenarios?.ForEach(s => allids.Add(ulong.Parse(s)));
IngameScripts?.ForEach(s => allids.Add(ulong.Parse(s)));
Expand Down Expand Up @@ -198,9 +252,12 @@ public ProcessedOptions(LegacyOptions options)
result.ExcludeExtensions = options.ExcludeExtensions;
result.IgnorePaths = options.IgnorePaths;
result.IngameScripts = options.IngameScripts;
result.ModPaths = options.ModPaths;
result.Mods = options.Mods;
result.Scenarios = options.Scenarios;
result.Tags = options.Tags;
result.TagsToAdd = options.TagsToAdd;
result.TagsToRemove = options.TagsToRemove;

result.Worlds = options.Worlds;
return result;
}
Expand All @@ -212,7 +269,7 @@ public ProcessedOptions(LegacyOptions options)
result.Force = options.Force;
result.ModIO = options.ModIO;
result.IngameScripts = options.IngameScripts;
result.ModPaths = options.ModPaths;
result.Mods = options.Mods;
return result;
}
}
Expand Down
2 changes: 1 addition & 1 deletion WorkshopToolCommon/Options/UGCOptionsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Phoenix.WorkshopTool.Options
public abstract class UGCOptionBase : OptionBase
{
[Option("mods", Group = "workshop", HelpText = "List of folder names of mods, use quotes if spaces")]
public IEnumerable<string> ModPaths { get; set; }
public IEnumerable<string> Mods { get; set; }
#if SE
[Option("scripts", Group = "workshop", HelpText = "List of folder names of scripts")]
#endif
Expand Down
6 changes: 6 additions & 0 deletions WorkshopToolCommon/Options/UploadVerb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ public class UploadVerb : UGCOptionBase
[Option("dependencies", HelpText = "Specify dependencies to other mods (modids only). Use 0 to remove all.", MetaValue = "<id>[,<id>]")]
public IEnumerable<ulong> Dependencies { get; set; }

internal bool TagsSpecified => Tags != null || TagsToAdd != null || TagsToRemove != null;

[Option("add-tag", HelpText = "List of workshop categories/tags to add")]
public IEnumerable<string> TagsToAdd { get; set; }

[Option("remove-tag", HelpText = "List of workshop categories/tags to remove")]
public IEnumerable<string> TagsToRemove { get; set; }

[Option("tags", HelpText = "List of workshop mod categories/tags to use (overwrites previous, default: keep)")]
public IEnumerable<string> Tags { get; set; }
Expand Down
43 changes: 21 additions & 22 deletions WorkshopToolCommon/Uploader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class Uploader : IMod
public string Title { get { return m_title; } }
public string ModPath { get { return m_modPath; } }

public Uploader(WorkshopType type, string path, string[] tags = null, string[] ignoredExtensions = null, string[] ignoredPaths = null, bool compile = false, bool dryrun = false, bool development = false, PublishedFileVisibility? visibility = null, bool force = false, string previewFilename = null, string[] dlcs = null, ulong[] deps = null, string description = null, string changelog = null)
public Uploader(WorkshopType type, string path, Options.UploadVerb options, string description = null, string changelog = null)
{
m_modPath = path;

Expand All @@ -108,11 +108,11 @@ public Uploader(WorkshopType type, string path, string[] tags = null, string[] i
// Fill defaults before assigning user-defined ones
FillPropertiesFromPublished();

m_compile = compile;
m_dryrun = dryrun;
m_compile = options.Compile;
m_dryrun = options.DryRun;

if (visibility != null)
m_visibility = visibility;
if (options.Visibility != null)
m_visibility = options.Visibility;

if (string.IsNullOrEmpty(m_title))
m_title = Path.GetFileName(path);
Expand All @@ -121,28 +121,28 @@ public Uploader(WorkshopType type, string path, string[] tags = null, string[] i
m_changelog = changelog;

m_type = type;
m_isDev = development;
m_force = force;
m_isDev = false;
m_force = options.Force;

if(previewFilename != null)
m_previewFilename = previewFilename;
if(options.Thumbnail != null)
m_previewFilename = options.Thumbnail;
#if SE
var mappedlc = MapDLCStringsToInts(dlcs);
var mappedlc = MapDLCStringsToInts(options.DLCs);

// If user specified "0" or "none" for DLCs, remove all of them
if (dlcs != null)
if (options.DLCs != null)
m_dlcs = mappedlc;
#endif
if (tags != null)
m_tags = tags;
if (options.Tags != null)
m_tags = options.Tags.ToArray();

if (deps != null)
if (options.Dependencies != null)
{
// Any dependencies that existed, but weren't specified, will be removed
if (m_deps != null)
m_depsToRemove = m_deps.Except(deps).ToArray();
m_depsToRemove = m_deps.Except(options.Dependencies).ToArray();

m_deps = deps;
m_deps = options.Dependencies.ToArray();
}

// This file list should match the PublishXXXAsync methods in MyWorkshop
Expand All @@ -162,15 +162,14 @@ public Uploader(WorkshopType type, string path, string[] tags = null, string[] i
break;
}

if ( ignoredExtensions != null )
if ( options.ExcludeExtensions != null )
{
ignoredExtensions = ignoredExtensions.Select(s => "." + s.TrimStart(new[] { '.', '*' })).ToArray();
ignoredExtensions.ForEach(s => m_ignoredExtensions.Add(s));
options.ExcludeExtensions.Select(s => "." + s.TrimStart(new[] { '.', '*' })).ForEach(s => m_ignoredExtensions.Add(s));
}

if (ignoredPaths != null)
if (options.IgnorePaths != null)
{
ignoredPaths.ForEach(s => m_ignoredPaths.Add(s));
options.IgnorePaths.ForEach(s => m_ignoredPaths.Add(s));
}

// Start with the parent file, if it exists. This is at %AppData%\SpaceEngineers\Mods.
Expand All @@ -190,7 +189,7 @@ public Uploader(WorkshopType type, string path, string[] tags = null, string[] i
}

#if SE
private uint[] MapDLCStringsToInts(string[] stringdlcs)
private uint[] MapDLCStringsToInts(IEnumerable<string> stringdlcs)
{
var dlcs = new HashSet<uint>();

Expand Down
15 changes: 15 additions & 0 deletions WorkshopToolCommon/WorkshopHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ public static void PublishDependencies(WorkshopId[] modId, ulong[] dependenciesT
}
}
#endif
public static List<MyWorkshopItem> GetItemDetails(ICollection<ulong> ids)
{
var results = new List<MyWorkshopItem>();
#if SE
var workshopids = new List<WorkshopId>();
foreach (var id in ids)
workshopids.Add(new WorkshopId(id, MyGameService.GetDefaultUGC().ServiceName));

MyWorkshop.GetItemsBlockingUGC(workshopids, results);
#else
MyWorkshop.GetItemsBlocking(ids, results);
#endif
return results;
}

#region Collections
public static IEnumerable<MySubscribedItem> GetCollectionDetails(ulong modid)
{
Expand Down

0 comments on commit 528c355

Please sign in to comment.