reformat overrides and allow the option of adding processor parameters

This commit is contained in:
Ellpeck 2020-06-25 11:53:25 +02:00
parent 2b16973913
commit a2bf711912
7 changed files with 83 additions and 70 deletions

View file

@ -5,14 +5,27 @@ using Newtonsoft.Json.Linq;
namespace Contentless { namespace Contentless {
public class Config { public class Config {
[JsonProperty(PropertyName = "exclude")] [JsonProperty("exclude")]
public string[] ExcludedFiles = {"bin/", "obj/"}; public string[] ExcludedFiles = {"bin/", "obj/"};
[JsonProperty(PropertyName = "logSkipped")] [JsonProperty("logSkipped")]
public bool LogSkipped = true; public bool LogSkipped = true;
[JsonProperty(PropertyName = "overrides")] [JsonProperty("overrides")]
public Dictionary<string, JToken> Overrides = new Dictionary<string, JToken>(); public Dictionary<string, Override> Overrides = new Dictionary<string, Override>();
}
public class Override {
[JsonProperty("importer")]
public string Importer;
[JsonProperty("processor")]
public string Processor;
[JsonProperty("processorParams")]
public Dictionary<string, string> ProcessorParams = new Dictionary<string, string>();
[JsonProperty("copy")]
public bool Copy;
} }
} }

View file

@ -12,9 +12,5 @@ namespace Contentless {
this.Type = type; this.Type = type;
} }
public override string ToString() {
return this.Type.Name;
}
} }
} }

View file

@ -4,13 +4,11 @@ namespace Contentless {
public class OverrideInfo { public class OverrideInfo {
public readonly Regex Regex; public readonly Regex Regex;
public readonly string Importer; public readonly Override Override;
public readonly string Processor;
public OverrideInfo(Regex regex, string importer, string processor) { public OverrideInfo(Regex regex, Override over) {
this.Regex = regex; this.Regex = regex;
this.Importer = importer; this.Override = over;
this.Processor = processor;
} }
} }

View file

@ -86,29 +86,32 @@ namespace Contentless {
ImporterInfo importer = null; ImporterInfo importer = null;
string processor = null; string processor = null;
Dictionary<string, string> processorParams = null;
// override importers // override importers
var over = GetOverrideFor(relative, overrides); var over = GetOverrideFor(relative, overrides);
if (over != null) { if (over != null) {
processorParams = over.Override.ProcessorParams;
// copy special case // copy special case
if (over.Importer == "Copy" || over.Processor == "Copy") { if (over.Override.Copy) {
CopyFile(content, relative); CopyFile(content, relative);
changed = true; changed = true;
continue; continue;
} }
if (!string.IsNullOrEmpty(over.Importer) && over.Importer != "Auto") { if (!string.IsNullOrEmpty(over.Override.Importer)) {
importer = importers.Find(i => i.Type.Name == over.Importer); importer = importers.Find(i => i.Type.Name == over.Override.Importer);
if (importer == null) { 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; continue;
} }
} }
if (!string.IsNullOrEmpty(over.Processor) && over.Processor != "Auto") { if (!string.IsNullOrEmpty(over.Override.Processor)) {
processor = processors.Find(p => p == over.Processor); processor = processors.Find(p => p == over.Override.Processor);
if (processor == null) { 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; continue;
} }
} }
@ -126,7 +129,7 @@ namespace Contentless {
continue; continue;
} }
AddFile(content, relative, importer, processor); AddFile(content, relative, importer.Type.Name, processor, processorParams);
changed = true; changed = true;
} }
@ -154,29 +157,16 @@ namespace Contentless {
if (processor != null) if (processor != null)
processors.Add(type.Name); processors.Add(type.Name);
} }
} catch (Exception) { } catch {
// ignored // ignored
} }
} }
return (importers, processors); return (importers, processors);
} }
private static IEnumerable<OverrideInfo> GetOverrides(Dictionary<string, JToken> config) { private static IEnumerable<OverrideInfo> GetOverrides(Dictionary<string, Override> config) {
foreach (var entry in config) { foreach (var entry in config)
var regex = MakeFileRegex(entry.Key); yield return new OverrideInfo(MakeFileRegex(entry.Key), entry.Value);
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 OverrideInfo GetOverrideFor(string file, IEnumerable<OverrideInfo> overrides) { private static OverrideInfo GetOverrideFor(string file, IEnumerable<OverrideInfo> overrides) {
@ -215,10 +205,14 @@ namespace Contentless {
return content; return content;
} }
private static void AddFile(List<string> content, string relative, ImporterInfo importer, string processor) { private static void AddFile(List<string> content, string relative, string importer, string processor, Dictionary<string, string> processorParams) {
content.Add($"#begin {relative}"); content.Add($"#begin {relative}");
content.Add($"/importer:{importer}"); content.Add($"/importer:{importer}");
content.Add($"/processor:{processor}"); 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($"/build:{relative}");
content.Add(""); content.Add("");
Console.WriteLine($"Adding file {relative} with importer {importer} and processor {processor}"); Console.WriteLine($"Adding file {relative} with importer {importer} and processor {processor}");

View file

@ -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) // If any files that were skipped without errors should be logged (Files that already have entries or files that were ignored)
// Default: true // Default: true
"logSkipped": true, "logSkipped": true,
// The list of files that should use a different importer than the one that Contentless automatically determined. Can use regex // The list of files that should use a different importer or processor 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
// Default: {} // Default: {}
"overrides": { "overrides": {
// Example: Make all files matching the regex ".json" use the importer "JsonImporter" // Example: Make all files matching the regex ".json" use the importer "JsonImporter"
".json": "JsonImporter", ".json": {
// Example: Specifying "Copy" as the importer sets the file's Build Mode to "Copy" instead of "Build" "importer": "JsonImporter"
".txt": "Copy", },
// Example: Specifying both an importer and a processor // Example: Specifying both an importer and a processor
".ogg": ["OggImporter", "SongProcessor"], ".ogg": {
"importer": "OggImporter",
"processor": "SongProcessor"
},
// Example: Only specifying a processor // 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"
}
}
} }
} }
``` ```

View file

@ -17,11 +17,6 @@
#begin Json/Copy.json #begin Json/Copy.json
/copy:Json/Copy.json /copy:Json/Copy.json
#begin Json/Test.json
/importer:JsonContentImporter
/processor:FontTextureProcessor
/build:Json/Test.json
#begin Locale/Interface.xml #begin Locale/Interface.xml
/importer:XmlImporter /importer:XmlImporter
/processor:FontTextureProcessor /processor:FontTextureProcessor
@ -30,30 +25,18 @@
#begin Textures/Icons.png #begin Textures/Icons.png
/importer:TextureImporter /importer:TextureImporter
/processor:TextureProcessor /processor:TextureProcessor
/processorParam:TextureFormat=Compressed
/build:Textures/Icons.png /build:Textures/Icons.png
#begin Textures/Inside.png #begin Textures/Inside.png
/importer:TextureImporter /importer:TextureImporter
/processor:TextureProcessor /processor:TextureProcessor
/processorParam:TextureFormat=Compressed
/build:Textures/Inside.png /build:Textures/Inside.png
#begin Tiled/Map.tmx
/importer:TiledMapImporter
/processor:TiledMapProcessor
/build:Tiled/Map.tmx
#begin Tiled/Tiles.png #begin Tiled/Tiles.png
/importer:TextureImporter /importer:TextureImporter
/processor:TextureProcessor /processor:TextureProcessor
/processorParam:TextureFormat=Compressed
/build:Tiled/Tiles.png /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

View file

@ -6,9 +6,24 @@
], ],
"logSkipped": false, "logSkipped": false,
"overrides": { "overrides": {
"Copy.*": "Copy", "Copy.*": {
".json": ["JsonContentImporter", "FontTextureProcessor"], "copy": true
".ogg": ["OggImporter", "SongProcessor"], },
".xml": ["", "FontTextureProcessor"] ".json": {
"importer": "JsonContentImporter",
"processor": "FontTextureProcessor"
},
".ogg": {
"importer": "OggImporter",
"processor": "SongProcessor"
},
".xml": {
"processor": "FontTextureProcessor"
},
".png": {
"processorParams": {
"TextureFormat": "Compressed"
}
}
} }
} }