mirror of
https://github.com/Ellpeck/Contentless.git
synced 2024-11-22 07:23:30 +01:00
allow override processors to be specified as well
This commit is contained in:
parent
9f7e4e78f4
commit
2142f63a2d
6 changed files with 90 additions and 47 deletions
|
@ -1,5 +1,6 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
namespace Contentless {
|
namespace Contentless {
|
||||||
public class Config {
|
public class Config {
|
||||||
|
@ -11,7 +12,7 @@ namespace Contentless {
|
||||||
public bool LogSkipped = true;
|
public bool LogSkipped = true;
|
||||||
|
|
||||||
[JsonProperty(PropertyName = "overrides")]
|
[JsonProperty(PropertyName = "overrides")]
|
||||||
public Dictionary<string, string> Overrides = new Dictionary<string, string>();
|
public Dictionary<string, JToken> Overrides = new Dictionary<string, JToken>();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
17
Contentless/OverrideInfo.cs
Normal file
17
Contentless/OverrideInfo.cs
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
|
namespace Contentless {
|
||||||
|
public class OverrideInfo {
|
||||||
|
|
||||||
|
public readonly Regex Regex;
|
||||||
|
public readonly string Importer;
|
||||||
|
public readonly string Processor;
|
||||||
|
|
||||||
|
public OverrideInfo(Regex regex, string importer, string processor) {
|
||||||
|
this.Regex = regex;
|
||||||
|
this.Importer = importer;
|
||||||
|
this.Processor = processor;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,6 +6,7 @@ using System.Reflection;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using Microsoft.Xna.Framework.Content.Pipeline;
|
using Microsoft.Xna.Framework.Content.Pipeline;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
namespace Contentless {
|
namespace Contentless {
|
||||||
public static class Program {
|
public static class Program {
|
||||||
|
@ -41,7 +42,7 @@ namespace Contentless {
|
||||||
Console.WriteLine("Using default config");
|
Console.WriteLine("Using default config");
|
||||||
}
|
}
|
||||||
var excluded = config.ExcludedFiles.Select(MakeFileRegex).ToArray();
|
var excluded = config.ExcludedFiles.Select(MakeFileRegex).ToArray();
|
||||||
var overrides = config.Overrides.Select((e, i) => (MakeFileRegex(e.Key), e.Value)).ToArray();
|
var overrides = GetOverrides(config.Overrides).ToArray();
|
||||||
|
|
||||||
// load any references to be able to include custom content types as well
|
// load any references to be able to include custom content types as well
|
||||||
foreach (var line in content) {
|
foreach (var line in content) {
|
||||||
|
@ -60,6 +61,8 @@ namespace Contentless {
|
||||||
// load content importers
|
// load content importers
|
||||||
var importers = GetContentImporters().ToArray();
|
var importers = GetContentImporters().ToArray();
|
||||||
Console.WriteLine($"Found possible importer types {string.Join(", ", importers.AsEnumerable())}");
|
Console.WriteLine($"Found possible importer types {string.Join(", ", importers.AsEnumerable())}");
|
||||||
|
var processors = GetContentProcessors().ToArray();
|
||||||
|
Console.WriteLine($"Found possible processor types {string.Join(", ", processors.AsEnumerable())}");
|
||||||
|
|
||||||
var changed = false;
|
var changed = false;
|
||||||
foreach (var file in contentFile.Directory.EnumerateFiles("*", SearchOption.AllDirectories)) {
|
foreach (var file in contentFile.Directory.EnumerateFiles("*", SearchOption.AllDirectories)) {
|
||||||
|
@ -83,35 +86,46 @@ namespace Contentless {
|
||||||
}
|
}
|
||||||
|
|
||||||
ImporterInfo importer = null;
|
ImporterInfo importer = null;
|
||||||
|
string processor = null;
|
||||||
|
|
||||||
// override importers
|
// override importers
|
||||||
var over = GetOverrideImporterFor(relative, overrides);
|
var over = GetOverrideFor(relative, overrides);
|
||||||
if (over != null) {
|
if (over != null) {
|
||||||
// copy special case
|
// copy special case
|
||||||
if (over == "Copy") {
|
if (over.Importer == "Copy") {
|
||||||
CopyFile(content, relative);
|
CopyFile(content, relative);
|
||||||
changed = true;
|
changed = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
importer = Array.Find(importers, i => i.Type.Name == over);
|
importer = Array.Find(importers, i => i.Type.Name == over.Importer);
|
||||||
if (importer == null) {
|
if (importer == null) {
|
||||||
Console.WriteLine($"Override importer {over} not found for file {relative}");
|
Console.WriteLine($"Override importer {over.Importer} not found for file {relative}");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (over.Processor != null) {
|
||||||
|
processor = Array.Find(processors, p => p == over.Processor);
|
||||||
|
if (processor == null) {
|
||||||
|
Console.WriteLine($"Override processor {over.Processor} not found for file {relative}");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// normal importers
|
// normal importers
|
||||||
if (importer == null)
|
if (importer == null)
|
||||||
importer = GetImporterFor(relative, importers);
|
importer = GetImporterFor(relative, importers);
|
||||||
|
if (processor == null)
|
||||||
|
processor = Array.Find(processors, p => p == importer.Importer.DefaultProcessor);
|
||||||
|
|
||||||
// no importer found :(
|
// no importer found :(
|
||||||
if (importer == null) {
|
if (importer == null || processor == null) {
|
||||||
Console.WriteLine($"No importer found for file {relative}");
|
Console.WriteLine($"No importer or processor found for file {relative}");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
AddFile(content, relative, importer);
|
AddFile(content, relative, importer, processor);
|
||||||
changed = true;
|
changed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,10 +150,38 @@ namespace Contentless {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string GetOverrideImporterFor(string file, IEnumerable<(Regex, string)> overrides) {
|
private static IEnumerable<string> GetContentProcessors() {
|
||||||
foreach (var (regex, value) in overrides) {
|
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) {
|
||||||
if (regex.IsMatch(file))
|
foreach (var type in assembly.GetTypes()) {
|
||||||
return value;
|
var processor = type.GetCustomAttribute(typeof(ContentProcessorAttribute), true);
|
||||||
|
if (processor != null)
|
||||||
|
yield return type.Name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static IEnumerable<OverrideInfo> GetOverrides(Dictionary<string, JToken> 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 OverrideInfo GetOverrideFor(string file, IEnumerable<OverrideInfo> overrides) {
|
||||||
|
foreach (var over in overrides) {
|
||||||
|
if (over.Regex.IsMatch(file))
|
||||||
|
return over;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -172,13 +214,13 @@ namespace Contentless {
|
||||||
return content;
|
return content;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void AddFile(List<string> content, string relative, ImporterInfo importer) {
|
private static void AddFile(List<string> content, string relative, ImporterInfo importer, string processor) {
|
||||||
content.Add($"#begin {relative}");
|
content.Add($"#begin {relative}");
|
||||||
content.Add($"/importer:{importer.Type.Name}");
|
content.Add($"/importer:{importer}");
|
||||||
content.Add($"/processor:{importer.Importer.DefaultProcessor}");
|
content.Add($"/processor:{processor}");
|
||||||
content.Add($"/build:{relative}");
|
content.Add($"/build:{relative}");
|
||||||
content.Add("");
|
content.Add("");
|
||||||
Console.WriteLine($"Adding file {relative} with importer {importer.Type.Name} and processor {importer.Importer.DefaultProcessor}");
|
Console.WriteLine($"Adding file {relative} with importer {importer} and processor {processor}");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void CopyFile(List<string> content, string relative) {
|
private static void CopyFile(List<string> content, string relative) {
|
||||||
|
|
|
@ -31,12 +31,15 @@ To add a configuration file to Contentless, simply create a file named `Contentl
|
||||||
// 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 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": "JsonImporter",
|
||||||
// Example: Specifying "Copy" as the importer sets the file's Build Mode to "Copy" instead of "Build"
|
// Example: Specifying "Copy" as the importer sets the file's Build Mode to "Copy" instead of "Build"
|
||||||
".txt": "Copy"
|
".txt": "Copy",
|
||||||
|
// Example: Specifying both an importer and a processor
|
||||||
|
".ogg": ["OggImporter", "SongProcessor"]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
|
@ -13,6 +13,13 @@
|
||||||
/reference:..\bin\Debug\netcoreapp2.2\MonoGame.Extended.Content.Pipeline.dll
|
/reference:..\bin\Debug\netcoreapp2.2\MonoGame.Extended.Content.Pipeline.dll
|
||||||
|
|
||||||
#---------------------------------- Content ---------------------------------#
|
#---------------------------------- Content ---------------------------------#
|
||||||
|
#begin 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
|
||||||
|
@ -22,25 +29,11 @@
|
||||||
#begin Textures/Icons.png
|
#begin Textures/Icons.png
|
||||||
/importer:TextureImporter
|
/importer:TextureImporter
|
||||||
/processor:TextureProcessor
|
/processor:TextureProcessor
|
||||||
/processorParam:ColorKeyColor=255,0,255,255
|
|
||||||
/processorParam:ColorKeyEnabled=True
|
|
||||||
/processorParam:GenerateMipmaps=False
|
|
||||||
/processorParam:PremultiplyAlpha=True
|
|
||||||
/processorParam:ResizeToPowerOfTwo=False
|
|
||||||
/processorParam:MakeSquare=False
|
|
||||||
/processorParam:TextureFormat=Color
|
|
||||||
/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:ColorKeyColor=255,0,255,255
|
|
||||||
/processorParam:ColorKeyEnabled=True
|
|
||||||
/processorParam:GenerateMipmaps=False
|
|
||||||
/processorParam:PremultiplyAlpha=True
|
|
||||||
/processorParam:ResizeToPowerOfTwo=False
|
|
||||||
/processorParam:MakeSquare=False
|
|
||||||
/processorParam:TextureFormat=Color
|
|
||||||
/build:Textures/Inside.png
|
/build:Textures/Inside.png
|
||||||
|
|
||||||
#begin Tiled/Map.tmx
|
#begin Tiled/Map.tmx
|
||||||
|
@ -51,24 +44,10 @@
|
||||||
#begin Tiled/Tiles.png
|
#begin Tiled/Tiles.png
|
||||||
/importer:TextureImporter
|
/importer:TextureImporter
|
||||||
/processor:TextureProcessor
|
/processor:TextureProcessor
|
||||||
/processorParam:ColorKeyColor=255,0,255,255
|
|
||||||
/processorParam:ColorKeyEnabled=True
|
|
||||||
/processorParam:GenerateMipmaps=False
|
|
||||||
/processorParam:PremultiplyAlpha=True
|
|
||||||
/processorParam:ResizeToPowerOfTwo=False
|
|
||||||
/processorParam:MakeSquare=False
|
|
||||||
/processorParam:TextureFormat=Color
|
|
||||||
/build:Tiled/Tiles.png
|
/build:Tiled/Tiles.png
|
||||||
|
|
||||||
#begin Tiled/Tileset.tsx
|
#begin Tiled/Tileset.tsx
|
||||||
/importer:TiledMapTilesetImporter
|
/importer:TiledMapTilesetImporter
|
||||||
/processor:TiledMapTilesetProcessor
|
/processor:TiledMapTilesetProcessor
|
||||||
/build:Tiled/Tileset.tsx
|
/build:Tiled/Tileset.tsx
|
||||||
#begin Json/Copy.json
|
|
||||||
/copy:Json/Copy.json
|
|
||||||
|
|
||||||
#begin Json/Test.json
|
|
||||||
/importer:JsonContentImporter
|
|
||||||
/processor:JsonContentProcessor
|
|
||||||
/build:Json/Test.json
|
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
"logSkipped": false,
|
"logSkipped": false,
|
||||||
"overrides": {
|
"overrides": {
|
||||||
"Copy.*": "Copy",
|
"Copy.*": "Copy",
|
||||||
".json": "JsonContentImporter"
|
".json": ["JsonContentImporter", "FontTextureProcessor"],
|
||||||
|
".ogg": ["OggImporter", "SongProcessor"]
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue