Compare commits

...

4 commits

Author SHA1 Message Date
Ell
05818f1c74 3.0.7 2022-09-25 23:33:36 +02:00
Ell
ff4b7d6f01 updated readme 2022-09-25 23:32:35 +02:00
Ell
99b746fc42 Merge remote-tracking branch 'origin/main' into main 2022-09-25 23:29:11 +02:00
Ell
d1012c8a28 updated to .net 6 2022-09-25 23:29:02 +02:00
10 changed files with 537 additions and 541 deletions

View file

@ -1,8 +1,8 @@
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Contentless {
namespace Contentless;
public class Config {
[JsonProperty("exclude")]
@ -12,7 +12,7 @@ namespace Contentless {
public bool LogSkipped = true;
[JsonProperty("overrides")]
public Dictionary<string, Override> Overrides = new Dictionary<string, Override>();
public Dictionary<string, Override> Overrides = new();
}
@ -23,9 +23,8 @@ namespace Contentless {
[JsonProperty("processor")]
public string Processor;
[JsonProperty("processorParams")]
public Dictionary<string, string> ProcessorParams = new Dictionary<string, string>();
public Dictionary<string, string> ProcessorParams = new();
[JsonProperty("copy")]
public bool Copy;
}
}

View file

@ -2,7 +2,8 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<RollForward>Major</RollForward>
</PropertyGroup>
<PropertyGroup>

View file

@ -10,15 +10,15 @@
<repository type="git" url="https://github.com/Ellpeck/Contentless" />
<readme>README.md</readme>
<icon>Logo.png</icon>
<version>3.0.6</version>
<version>3.0.7</version>
<dependencies>
<group targetFramework="net5.0" />
<group targetFramework="net6.0" />
</dependencies>
</metadata>
<files>
<file src="_._" target="lib/net5.0/"/>
<file src="_._" target="lib/net6.0/" />
<file src="Contentless.targets" target="build/Contentless.targets" />
<file src="bin\Debug\net5.0\**\*" target="tools/"/>
<file src="bin\Debug\net6.0\**\*" target="tools/" />
<file src="../README.md" target="" />
<file src="../Logo.png" target="" />
</files>

View file

@ -1,7 +1,8 @@
using System;
using Microsoft.Xna.Framework.Content.Pipeline;
namespace Contentless {
namespace Contentless;
public class ImporterInfo {
public readonly ContentImporterAttribute Importer;
@ -17,4 +18,3 @@ namespace Contentless {
}
}
}

View file

@ -1,6 +1,7 @@
using System.Text.RegularExpressions;
namespace Contentless {
namespace Contentless;
public class OverrideInfo {
public readonly Regex Regex;
@ -12,4 +13,3 @@ namespace Contentless {
}
}
}

View file

@ -6,9 +6,9 @@ using System.Reflection;
using System.Text.RegularExpressions;
using Microsoft.Xna.Framework.Content.Pipeline;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Contentless {
namespace Contentless;
public static class Program {
public static void Main(string[] args) {
@ -24,25 +24,24 @@ namespace Contentless {
}
Console.WriteLine($"Using content file {contentFile}");
var content = ReadContent(contentFile);
var content = Program.ReadContent(contentFile);
// load config
var config = new Config();
var configFile = new FileInfo(Path.Combine(contentFile.DirectoryName, "Contentless.json"));
var configFile = new FileInfo(Path.Combine(contentFile.DirectoryName!, "Contentless.json"));
if (configFile.Exists) {
using (var stream = configFile.OpenText()) {
using var stream = configFile.OpenText();
try {
config = JsonConvert.DeserializeObject<Config>(stream.ReadToEnd());
Console.WriteLine($"Using config from {configFile}");
} catch (Exception e) {
Console.WriteLine($"Error loading config from {configFile}: {e}");
}
}
} else {
Console.WriteLine("Using default config");
}
var excluded = config.ExcludedFiles.Select(MakeFileRegex).ToArray();
var overrides = GetOverrides(config.Overrides).ToArray();
var excluded = config.ExcludedFiles.Select(Program.MakeFileRegex).ToArray();
var overrides = Program.GetOverrides(config.Overrides).ToArray();
// load any references to be able to include custom content types as well
foreach (var line in content) {
@ -59,7 +58,7 @@ namespace Contentless {
}
// load content importers
var (importers, processors) = GetContentData();
var (importers, processors) = Program.GetContentData();
Console.WriteLine($"Found possible importer types {string.Join(", ", importers)}");
Console.WriteLine($"Found possible processor types {string.Join(", ", processors)}");
@ -68,7 +67,7 @@ namespace Contentless {
// is the file the content or config file?
if (file.Name == contentFile.Name || file.Name == configFile.Name)
continue;
var relative = GetRelativePath(contentFile.DirectoryName, file.FullName).Replace("\\", "/");
var relative = Program.GetRelativePath(contentFile.DirectoryName, file.FullName).Replace("\\", "/");
// is the file in an excluded directory?
if (excluded.Any(e => e.IsMatch(relative))) {
@ -78,7 +77,7 @@ namespace Contentless {
}
// is the file already in the content file?
if (HasEntry(content, relative)) {
if (Program.HasEntry(content, relative)) {
if (config.LogSkipped)
Console.WriteLine($"Skipping file {relative} as it is already part of the content file");
continue;
@ -89,13 +88,13 @@ namespace Contentless {
Dictionary<string, string> processorParams = null;
// override importers
var over = GetOverrideFor(relative, overrides);
var over = Program.GetOverrideFor(relative, overrides);
if (over != null) {
processorParams = over.Override.ProcessorParams;
// copy special case
if (over.Override.Copy) {
CopyFile(content, relative);
Program.CopyFile(content, relative);
changed = true;
continue;
}
@ -118,8 +117,7 @@ namespace Contentless {
}
// normal importers
if (importer == null)
importer = GetImporterFor(relative, importers);
importer ??= Program.GetImporterFor(relative, importers);
if (importer != null && processor == null)
processor = processors.Find(p => p == importer.Importer.DefaultProcessor);
@ -129,7 +127,7 @@ namespace Contentless {
continue;
}
AddFile(content, relative, importer.Type.Name, processor, processorParams);
Program.AddFile(content, relative, importer.Type.Name, processor, processorParams);
changed = true;
}
@ -166,7 +164,7 @@ namespace Contentless {
private static IEnumerable<OverrideInfo> GetOverrides(Dictionary<string, Override> config) {
foreach (var entry in config)
yield return new OverrideInfo(MakeFileRegex(entry.Key), entry.Value);
yield return new OverrideInfo(Program.MakeFileRegex(entry.Key), entry.Value);
}
private static OverrideInfo GetOverrideFor(string file, IEnumerable<OverrideInfo> overrides) {
@ -196,16 +194,13 @@ namespace Contentless {
private static List<string> ReadContent(FileInfo file) {
var content = new List<string>();
using (var stream = file.OpenText()) {
string line;
while ((line = stream.ReadLine()) != null) {
using var stream = file.OpenText();
while (stream.ReadLine() is {} line)
content.Add(line);
}
}
return content;
}
private static void AddFile(List<string> content, string relative, string importer, string processor, Dictionary<string, string> processorParams) {
private static void AddFile(ICollection<string> content, string relative, string importer, string processor, Dictionary<string, string> processorParams) {
content.Add($"#begin {relative}");
content.Add($"/importer:{importer}");
content.Add($"/processor:{processor}");
@ -218,7 +213,7 @@ namespace Contentless {
Console.WriteLine($"Adding file {relative} with importer {importer} and processor {processor}");
}
private static void CopyFile(List<string> content, string relative) {
private static void CopyFile(ICollection<string> content, string relative) {
content.Add($"#begin {relative}");
content.Add($"/copy:{relative}");
content.Add("");
@ -236,4 +231,3 @@ namespace Contentless {
}
}
}

View file

@ -1,16 +1,18 @@
![The Contentless logo](https://raw.githubusercontent.com/Ellpeck/Contentless/main/Logo.png)
**Contentless** is tool for MonoGame that automatically handles adding assets to the Content Pipeline project so you don't have to use their interface to add every content file manually.
**Contentless** is tool for MonoGame that automatically handles adding assets to the Content Pipeline project, so you don't have to use their interface to add every content file manually.
# How to use
To use Contentless, you first have to add it to your project, either through your NuGet package manager or by adding it to your `.csproj` file as follows. Keep in mind to update the `Version` to the most recent one. You can find the package on the [NuGet website](https://www.nuget.org/packages/Contentless/) as well.
```xml
<ItemGroup>
<PackageReference Include="Contentless" Version="VERSION" />
</ItemGroup>
```
Next, you need to find the reference to your `Content.mgcb` file in your `.csproj` file or create one if there isn't already one present. The reference's type should be `MonoGameContentReference` so that Contentless can identify it correctly. If you're using the [MonoGame Content Builder](https://www.nuget.org/packages/MonoGame.Content.Builder.Task) alongside Contentless, this setting should already be applied.
```xml
<ItemGroup>
<MonoGameContentReference Include="Content\Content.mgcb" />
</ItemGroup>
@ -19,7 +21,7 @@ Next, you need to find the reference to your `Content.mgcb` file in your `.cspro
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:
If you want to change the way Contentless works, you can use a configuration file. To do so, 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:
```json5
{
// The list of files that should be excluded. Can use regex.
@ -63,8 +65,8 @@ To add a configuration file to Contentless, simply create a file named `Contentl
For an example of a config in use, see the [test config](https://github.com/Ellpeck/Contentless/blob/main/Test/Content/Contentless.json).
# What it does
When running Contentless and supplying the location of a MonoGame Content Pipeline project (`Content.mgcb`), it scans all of the files in the project's directory as well as its subdirectories. For each file, it checks if the `Content.mgcb` file already contains any references to that file. If no references are found, then a new reference to the file is added.
When running Contentless and supplying the location of a MonoGame Content Pipeline project (`Content.mgcb`), it scans all the files in the project's directory as well as its subdirectories. For each file, it checks if the `Content.mgcb` file already contains any references to that file. If no references are found, then a new reference to the file is added.
Contentless figures out which importer and processor to register for any given file by generating a list of all of the importers and processors that are available, both inside of MonoGame, and inside of References added to the `Content.mgcb` file. This process is similar to what occurs when adding an existing file through MonoGame's Content Pipeline tool. If Contentless sets the wrong importer or processor for any file, the user can simply open `Content.mgcb` in MonoGame's Content Pipeline tool and edit it manually.
Contentless figures out which importer and processor to register for any given file by generating a list of all the importers and processors that are available, both inside of MonoGame, and inside of References added to the `Content.mgcb` file. This process is similar to what occurs when adding an existing file through MonoGame's Content Pipeline tool. If Contentless sets the wrong importer or processor for any file, the user can simply open `Content.mgcb` in MonoGame's Content Pipeline tool and edit it manually.
As Contentless never changes any existing content of a `Content.mgcb` file, all changes that are made to it by hand or using the Content Pipeline tool will not be overridden.

View file

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>