From 6a623ceec6668d3714bd7248cb2127975f0a42e5 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Mon, 28 Oct 2019 23:43:48 +0100 Subject: [PATCH] added overrides --- Contentless/Config.cs | 3 +++ Contentless/Program.cs | 33 +++++++++++++++++++++++++++++++-- README.md | 10 ++++++++-- Test/Content/Content.mgcb | 9 +++++---- Test/Content/Contentless.json | 5 ++++- Test/Content/Copy/Test.json | 3 --- Test/Content/Json/Test.json | 3 +++ 7 files changed, 54 insertions(+), 12 deletions(-) delete mode 100644 Test/Content/Copy/Test.json create mode 100644 Test/Content/Json/Test.json diff --git a/Contentless/Config.cs b/Contentless/Config.cs index 734e76b..9a416ee 100644 --- a/Contentless/Config.cs +++ b/Contentless/Config.cs @@ -9,5 +9,8 @@ namespace Contentless { [JsonProperty(PropertyName = "logSkipped")] public bool LogSkipped = true; + [JsonProperty(PropertyName = "overrides")] + public string[][] Overrides = { }; + } } \ No newline at end of file diff --git a/Contentless/Program.cs b/Contentless/Program.cs index bd28dfb..f922544 100644 --- a/Contentless/Program.cs +++ b/Contentless/Program.cs @@ -40,7 +40,8 @@ namespace Contentless { } else { Console.WriteLine("Using default config"); } - var excluded = Array.ConvertAll(config.ExcludedFiles, s => new Regex(s.Replace(".", "[.]").Replace("*", ".*").Replace("?", "."))); + var excluded = config.ExcludedFiles.Select(MakeFileRegex).ToArray(); + var overrides = config.Overrides.Select(e => (MakeFileRegex(e[0]), e[1])).ToArray(); // load any references to be able to include custom content types as well foreach (var line in content) { @@ -81,7 +82,23 @@ namespace Contentless { continue; } - var importer = GetImporterFor(relative, importers); + ImporterInfo importer = null; + + // override importers + var over = GetOverrideImporterFor(relative, overrides); + if (over != null) { + importer = Array.Find(importers, i => i.Type.Name == over); + if (importer == null) { + Console.WriteLine($"Override importer {over} not found for file {relative}"); + continue; + } + } + + // normal importers + if (importer == null) + importer = GetImporterFor(relative, importers); + + // no importer found :( if (importer == null) { Console.WriteLine($"No importer found for file {relative}"); continue; @@ -112,6 +129,14 @@ namespace Contentless { } } + private static string GetOverrideImporterFor(string file, (Regex, string)[] overrides) { + foreach (var (regex, value) in overrides) { + if (regex.IsMatch(file)) + return value; + } + return null; + } + private static ImporterInfo GetImporterFor(string file, ImporterInfo[] importers) { var extension = Path.GetExtension(file); foreach (var importer in importers) { @@ -156,6 +181,10 @@ namespace Contentless { return path.Replace(relativeTo, ""); } + private static Regex MakeFileRegex(string s) { + return new Regex(s.Replace(".", "[.]").Replace("*", ".*").Replace("?", ".")); + } + } } \ No newline at end of file diff --git a/README.md b/README.md index aef3626..8100285 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Next, add Contentless to your build process by adding the following task to your Contentless will now automatically add any content files from your `Content` directory and subdirectories to your `Content.mgcb` file if they haven't already been added either manually or by Contentless. No existing items' configurations will be overridden, so you can still use the Content Pipeline tool to modify any settings as well. # Configuring -To add a configuration file to Contentless, simply create a file named `Contentless.json` in the same directory as your `Content.mgcb` file. You can use the config to change several options. For reference, here is a configuration file with the default values that are used if no config is supplied: +To add a configuration file to Contentless, simply create a file named `Contentless.json` in the same directory as your `Content.mgcb` file. You can use the config to change several options. For reference, here is a configuration file with the default values that are used if no config is supplied, along with some documentation: ```json5 { // The list of files that should be excluded. Can use regex @@ -23,7 +23,13 @@ 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) - "logSkipped": true + "logSkipped": true, + // The list of files that should use a different importer than the one + // that Contentless automatically determined. Can use regex + "overrides": [ + // Entries are arrays containing the file regex and importer, e.g. + // [".json", "JsonImporter"] + ] } ``` diff --git a/Test/Content/Content.mgcb b/Test/Content/Content.mgcb index 8607763..da83b36 100644 --- a/Test/Content/Content.mgcb +++ b/Test/Content/Content.mgcb @@ -14,6 +14,11 @@ #---------------------------------- Content ---------------------------------# +#begin Json/Test.json +/importer:JsonContentImporter +/processor:JsonContentProcessor +/build:Json/Test.json + #begin Locale/Interface.xml /importer:XmlImporter /processor:PassThroughProcessor @@ -64,8 +69,4 @@ /importer:TiledMapTilesetImporter /processor:TiledMapTilesetProcessor /build:Tiled/Tileset.tsx -#begin Copy/Test.json -/importer:TexturePackerJsonImporter -/processor:TexturePackerProcessor -/build:Copy/Test.json diff --git a/Test/Content/Contentless.json b/Test/Content/Contentless.json index e878528..8977084 100644 --- a/Test/Content/Contentless.json +++ b/Test/Content/Contentless.json @@ -4,5 +4,8 @@ "bin/", "Ex*.png" ], - "logSkipped": false + "logSkipped": false, + "overrides": [ + [".json", "JsonContentImporter"] + ] } \ No newline at end of file diff --git a/Test/Content/Copy/Test.json b/Test/Content/Copy/Test.json deleted file mode 100644 index 544b7b4..0000000 --- a/Test/Content/Copy/Test.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - -} \ No newline at end of file diff --git a/Test/Content/Json/Test.json b/Test/Content/Json/Test.json new file mode 100644 index 0000000..abc605f --- /dev/null +++ b/Test/Content/Json/Test.json @@ -0,0 +1,3 @@ +{ + "test": "test!" +} \ No newline at end of file