Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MonoGame automatic content.mgcb
const fs = require('fs');
// Leave out audio for faster load times
const leaveOutAudio = false;
const assetsPath = `${__dirname}/../Content/Assets`;
const contentPath = `${__dirname}/../Content/Content.mgcb`;
// ---
function parseDirectory(path, output) {
output = output || [];
var items = fs.readdirSync(path);
items.forEach(item => {
var obj = { relativePath: `${path.replace(`${__dirname}/../Content/`, "")}/${item}`, path: `${path}/${item}`, name: item };
output.push(obj);
if (fs.lstatSync(obj.path).isDirectory()) {
output = parseDirectory(obj.path, output);
}
});
return output;
}
console.log("Writing to Content.mgcb ...");
var resourceFiles = parseDirectory(`${assetsPath}`);
var template = `
#----------------------------- Global Properties ----------------------------#
/outputDir:bin/$(Platform)
/intermediateDir:obj/$(Platform)
/platform:DesktopGL
/config:
/profile:Reach
/compress:False
#-------------------------------- References --------------------------------#
#---------------------------------- Content ---------------------------------#
`;
var contents = template;
// Simple Template
var simpleTemplate = (path) => `#begin ${path}
/copy:${path}`;
// PNG
var pngTemplate = (path) => `#begin ${path}
/importer:TextureImporter
/processor:TextureProcessor
/build:${path}`;
resourceFiles.filter(r => r.name.endsWith(".png")).forEach(png => {
contents += `\n\n${pngTemplate(png.relativePath)}`;
});
// Sprite Font
var spritefontTemplate = (path) => `#begin ${path}
/importer:FontDescriptionImporter
/processor:FontDescriptionProcessor
/processorParam:PremultiplyAlpha=True
/processorParam:TextureFormat=Compressed
/build:${path}`;
resourceFiles.filter(r => r.name.endsWith(".spritefont")).forEach(spritefont => {
contents += `\n\n${spritefontTemplate(spritefont.relativePath)}`;
});
// Font
resourceFiles.filter(r => r.name.endsWith(".ttf")).forEach(font => {
contents += `\n\n${simpleTemplate(font.relativePath)}`;
});
// Effect
var effectTemplate = (path) => `#begin ${path}
/importer:EffectImporter
/processor:EffectProcessor
/processorParam:DebugMode=Auto
/build:${path}`;
resourceFiles.filter(r => r.name.endsWith(".fx")).forEach(effect => {
contents += `\n\n${effectTemplate(effect.relativePath)}`;
});
// Music
// var wavTemplate = (path) => `#begin ${path}
// /importer:WavImporter
// /processor:SoundEffectProcessor
// /processorParam:Quality=Best
// /build:${path}`;
// resourceFiles.filter(r => r.name.endsWith(".wav")).forEach(wav => {
// contents += `\n\n${wavTemplate(wav.relativePath)}`;
// });
if (!leaveOutAudio) {
// resourceFiles.filter(r => r.name.endsWith(".wav")).forEach(wav => {
// contents += `\n\n${simpleTemplate(wav.relativePath)}`;
// });
resourceFiles.filter(r => r.path.includes("Music") && r.name.endsWith(".ogg")).forEach(mus => {
contents += `\n\n${simpleTemplate(mus.relativePath)}`;
});
}
// SFX
var oggTemplate = (path) => `#begin ${path}
/importer:OggImporter
/processor:SoundEffectProcessor
/processorParam:Quality=Medium
/build:${path}`;
if (!leaveOutAudio) {
resourceFiles.filter(r => r.path.includes("Sfx") && r.name.endsWith(".ogg")).forEach(ogg => {
contents += `\n\n${oggTemplate(ogg.relativePath)}`;
});
}
// resourceFiles.filter(r => r.name.endsWith(".ogg")).forEach(ogg => {
// contents += `\n\n${simpleTemplate(ogg.relativePath)}`;
// });
// JSON
// json
resourceFiles.filter(r => r.name.endsWith(".json")).forEach(json => {
contents += `\n\n${simpleTemplate(json.relativePath)}`;
});
// Done!
fs.writeFileSync(contentPath, contents);
console.log("Successfully wrote to Content.mgcb!");
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<PublishReadyToRun>false</PublishReadyToRun>
<TieredCompilation>false</TieredCompilation>
</PropertyGroup>
<PropertyGroup>
<ApplicationManifest>app.manifest</ApplicationManifest>
<ApplicationIcon>Icon.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Content\Assets\Scenes\**" />
<Compile Remove="Content\bin\**" />
<Compile Remove="Content\obj\**" />
<EmbeddedResource Remove="Content\Assets\Scenes\**" />
<EmbeddedResource Remove="Content\bin\**" />
<EmbeddedResource Remove="Content\obj\**" />
<None Remove="Content\Assets\Scenes\**" />
<None Remove="Content\bin\**" />
<None Remove="Content\obj\**" />
</ItemGroup>
<ItemGroup>
<None Remove="Icon.ico" />
<None Remove="Icon.bmp" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Icon.ico" />
<EmbeddedResource Include="Icon.bmp" />
</ItemGroup>
<ItemGroup>
<MonoGameContentReference Include="Content\Content.mgcb" />
</ItemGroup>
<ItemGroup>
<TrimmerRootAssembly Include="Microsoft.Xna.Framework.Content.ContentTypeReader" Visible="false" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.0.1641" />
<PackageReference Include="MonoGame.Content.Builder.Task" Version="3.8.0.1641" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.Windows.Forms.VisualStudio.15.0">
<HintPath>..\..\..\..\..\..\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\System.Windows.Forms.VisualStudio.15.0.dll</HintPath>
</Reference>
</ItemGroup>
<Target Name="BeforeBuildContent" BeforeTargets="RunContentBuilder">
<Message Text="Starting the Project!" Importance="high" />
<Exec Command="node .\Process\BuildContent.js"></Exec>
</Target>
<Target Name="AfterBuildContent" AfterTargets="RunContentBuilder">
<Message Text="Done!" Importance="high" />
</Target>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment