From a2bf71191289f8e29c2c7bcffefc616eec4aeed4 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Thu, 25 Jun 2020 11:53:25 +0200 Subject: [PATCH] reformat overrides and allow the option of adding processor parameters --- Contentless/Config.cs | 21 +++++++++++++--- Contentless/ImporterInfo.cs | 4 --- Contentless/OverrideInfo.cs | 8 +++--- Contentless/Program.cs | 46 +++++++++++++++-------------------- README.md | 28 +++++++++++++++------ Test/Content/Content.mgcb | 23 +++--------------- Test/Content/Contentless.json | 23 +++++++++++++++--- 7 files changed, 83 insertions(+), 70 deletions(-) diff --git a/Contentless/Config.cs b/Contentless/Config.cs index 82a1c45..eda738f 100644 --- a/Contentless/Config.cs +++ b/Contentless/Config.cs @@ -5,14 +5,27 @@ using Newtonsoft.Json.Linq; namespace Contentless { public class Config { - [JsonProperty(PropertyName = "exclude")] + [JsonProperty("exclude")] public string[] ExcludedFiles = {"bin/", "obj/"}; - [JsonProperty(PropertyName = "logSkipped")] + [JsonProperty("logSkipped")] public bool LogSkipped = true; - [JsonProperty(PropertyName = "overrides")] - public Dictionary Overrides = new Dictionary(); + [JsonProperty("overrides")] + public Dictionary Overrides = new Dictionary(); + + } + + public class Override { + + [JsonProperty("importer")] + public string Importer; + [JsonProperty("processor")] + public string Processor; + [JsonProperty("processorParams")] + public Dictionary ProcessorParams = new Dictionary(); + [JsonProperty("copy")] + public bool Copy; } } \ No newline at end of file diff --git a/Contentless/ImporterInfo.cs b/Contentless/ImporterInfo.cs index 9ca6630..4eedfe3 100644 --- a/Contentless/ImporterInfo.cs +++ b/Contentless/ImporterInfo.cs @@ -12,9 +12,5 @@ namespace Contentless { this.Type = type; } - public override string ToString() { - return this.Type.Name; - } - } } \ No newline at end of file diff --git a/Contentless/OverrideInfo.cs b/Contentless/OverrideInfo.cs index eb7463e..bfb6900 100644 --- a/Contentless/OverrideInfo.cs +++ b/Contentless/OverrideInfo.cs @@ -4,13 +4,11 @@ namespace Contentless { public class OverrideInfo { public readonly Regex Regex; - public readonly string Importer; - public readonly string Processor; + public readonly Override Override; - public OverrideInfo(Regex regex, string importer, string processor) { + public OverrideInfo(Regex regex, Override over) { this.Regex = regex; - this.Importer = importer; - this.Processor = processor; + this.Override = over; } } diff --git a/Contentless/Program.cs b/Contentless/Program.cs index b5d70ea..5d8b273 100644 --- a/Contentless/Program.cs +++ b/Contentless/Program.cs @@ -86,29 +86,32 @@ namespace Contentless { ImporterInfo importer = null; string processor = null; + Dictionary processorParams = null; // override importers var over = GetOverrideFor(relative, overrides); if (over != null) { + processorParams = over.Override.ProcessorParams; + // copy special case - if (over.Importer == "Copy" || over.Processor == "Copy") { + if (over.Override.Copy) { CopyFile(content, relative); changed = true; continue; } - if (!string.IsNullOrEmpty(over.Importer) && over.Importer != "Auto") { - importer = importers.Find(i => i.Type.Name == over.Importer); + if (!string.IsNullOrEmpty(over.Override.Importer)) { + importer = importers.Find(i => i.Type.Name == over.Override.Importer); if (importer == null) { - Console.WriteLine($"Override importer {over.Importer} not found for file {relative}"); + Console.WriteLine($"Override importer {over.Override.Importer} not found for file {relative}"); continue; } } - if (!string.IsNullOrEmpty(over.Processor) && over.Processor != "Auto") { - processor = processors.Find(p => p == over.Processor); + if (!string.IsNullOrEmpty(over.Override.Processor)) { + processor = processors.Find(p => p == over.Override.Processor); if (processor == null) { - Console.WriteLine($"Override processor {over.Processor} not found for file {relative}"); + Console.WriteLine($"Override processor {over.Override.Processor} not found for file {relative}"); continue; } } @@ -126,7 +129,7 @@ namespace Contentless { continue; } - AddFile(content, relative, importer, processor); + AddFile(content, relative, importer.Type.Name, processor, processorParams); changed = true; } @@ -154,29 +157,16 @@ namespace Contentless { if (processor != null) processors.Add(type.Name); } - } catch (Exception) { + } catch { // ignored } } return (importers, processors); } - private static IEnumerable GetOverrides(Dictionary config) { - foreach (var entry in config) { - var regex = MakeFileRegex(entry.Key); - if (entry.Value.Type == JTokenType.Array) { - var arr = (JArray) entry.Value; - if (arr.Count != 2) { - Console.WriteLine("The override config " + entry.Key + " is invalid: The array needs to contain exactly two entries"); - } else { - yield return new OverrideInfo(regex, arr[0].ToString(), arr[1].ToString()); - } - } else if (entry.Value.Type == JTokenType.String) { - yield return new OverrideInfo(regex, entry.Value.ToString(), null); - } else { - Console.WriteLine("The override config " + entry.Key + " is invalid: Should be an array or a string"); - } - } + private static IEnumerable GetOverrides(Dictionary config) { + foreach (var entry in config) + yield return new OverrideInfo(MakeFileRegex(entry.Key), entry.Value); } private static OverrideInfo GetOverrideFor(string file, IEnumerable overrides) { @@ -215,10 +205,14 @@ namespace Contentless { return content; } - private static void AddFile(List content, string relative, ImporterInfo importer, string processor) { + private static void AddFile(List content, string relative, string importer, string processor, Dictionary processorParams) { content.Add($"#begin {relative}"); content.Add($"/importer:{importer}"); content.Add($"/processor:{processor}"); + if (processorParams != null) { + foreach (var kv in processorParams) + content.Add($"/processorParam:{kv.Key}={kv.Value}"); + } content.Add($"/build:{relative}"); content.Add(""); Console.WriteLine($"Adding file {relative} with importer {importer} and processor {processor}"); diff --git a/README.md b/README.md index cd87cde..8b9e2ea 100644 --- a/README.md +++ b/README.md @@ -30,18 +30,32 @@ To add a configuration file to Contentless, simply create a file named `Contentl // If any files that were skipped without errors should be logged (Files that already have entries or files that were ignored) // Default: true "logSkipped": true, - // The list of files that should use a different importer than the one that Contentless automatically determined. Can use regex - // Specifying a string as the value represents an override importer, specifying an array with two entries represents the override importer and override processor + // The list of files that should use a different importer or processor than the one that Contentless automatically determined. Can use regex. // Default: {} "overrides": { // Example: Make all files matching the regex ".json" use the importer "JsonImporter" - ".json": "JsonImporter", - // Example: Specifying "Copy" as the importer sets the file's Build Mode to "Copy" instead of "Build" - ".txt": "Copy", + ".json": { + "importer": "JsonImporter" + }, // Example: Specifying both an importer and a processor - ".ogg": ["OggImporter", "SongProcessor"], + ".ogg": { + "importer": "OggImporter", + "processor": "SongProcessor" + }, // Example: Only specifying a processor - ".wav": ["Auto", "SoundEffectProcessor"] + ".wav": { + "processor": "SoundEffectProcessor" + }, + // Example: Setting a file to the Copy build action + ".txt": { + "copy": true + }, + // Example: Adding processor parameters for files + "TestFile.png": { + "processorParams": { + "TextureFormat": "Compressed" + } + } } } ``` diff --git a/Test/Content/Content.mgcb b/Test/Content/Content.mgcb index 5f5b102..3ed48f1 100644 --- a/Test/Content/Content.mgcb +++ b/Test/Content/Content.mgcb @@ -17,11 +17,6 @@ #begin Json/Copy.json /copy:Json/Copy.json -#begin Json/Test.json -/importer:JsonContentImporter -/processor:FontTextureProcessor -/build:Json/Test.json - #begin Locale/Interface.xml /importer:XmlImporter /processor:FontTextureProcessor @@ -30,30 +25,18 @@ #begin Textures/Icons.png /importer:TextureImporter /processor:TextureProcessor +/processorParam:TextureFormat=Compressed /build:Textures/Icons.png #begin Textures/Inside.png /importer:TextureImporter /processor:TextureProcessor +/processorParam:TextureFormat=Compressed /build:Textures/Inside.png -#begin Tiled/Map.tmx -/importer:TiledMapImporter -/processor:TiledMapProcessor -/build:Tiled/Map.tmx - #begin Tiled/Tiles.png /importer:TextureImporter /processor:TextureProcessor +/processorParam:TextureFormat=Compressed /build:Tiled/Tiles.png -#begin Tiled/Tileset.tsx -/importer:TiledMapTilesetImporter -/processor:TiledMapTilesetProcessor -/build:Tiled/Tileset.tsx - -#begin Textures/Exclude.png -/importer:TextureImporter -/processor:TextureProcessor -/build:Textures/Exclude.png - diff --git a/Test/Content/Contentless.json b/Test/Content/Contentless.json index 6356079..63ff0e0 100644 --- a/Test/Content/Contentless.json +++ b/Test/Content/Contentless.json @@ -6,9 +6,24 @@ ], "logSkipped": false, "overrides": { - "Copy.*": "Copy", - ".json": ["JsonContentImporter", "FontTextureProcessor"], - ".ogg": ["OggImporter", "SongProcessor"], - ".xml": ["", "FontTextureProcessor"] + "Copy.*": { + "copy": true + }, + ".json": { + "importer": "JsonContentImporter", + "processor": "FontTextureProcessor" + }, + ".ogg": { + "importer": "OggImporter", + "processor": "SongProcessor" + }, + ".xml": { + "processor": "FontTextureProcessor" + }, + ".png": { + "processorParams": { + "TextureFormat": "Compressed" + } + } } } \ No newline at end of file