Created
February 27, 2011 16:49
-
-
Save robfe/846318 to your computer and use it in GitHub Desktop.
Strongly typed access to Content files for XNA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<#@ template language="C#" hostspecific="True" debug="True" #> | |
<#@ import namespace="System.IO"#> | |
<#@ import namespace="System.Linq"#> | |
<#@ import namespace="System.Collections.Generic"#> | |
<#@ import namespace="System.Runtime.Remoting.Messaging" #> | |
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #> | |
<#@ assembly name="System.Core.dll"#> | |
<#@ output extension=".Generated.cs" #> | |
<# | |
//edit the following path if it's a bad guess: | |
string path = Host.ResolvePath("..\\"+CallContext.GetData("NamespaceHint")+"Content"); | |
Dictionary<string, string> types = new Dictionary<string, string> | |
{ | |
{"png", "Texture2D"}, | |
{"wav", "SoundEffect"}, | |
{"spriteFont", "SpriteFont"}, | |
}; | |
Dictionary<string, string> names = new Dictionary<string, string> | |
{ | |
{"Texture2D", "Images"}, | |
{"SoundEffect", "SoundEffects"}, | |
{"SpriteFont", "Fonts"}, | |
}; | |
var resources = from t in types | |
from f in Directory.GetFiles(path, "*."+t.Key, SearchOption.AllDirectories) | |
let p = f.Substring(path.Length+1).Split('.')[0] | |
select new { | |
Path=p, | |
Type=t.Value, | |
Name=p.Replace("\\", "_") | |
}; | |
var files = resources.Select(x=>"//"+x.Type+": "+x.Path); | |
#> | |
using Microsoft.Xna.Framework.Audio; | |
using Microsoft.Xna.Framework.Content; | |
using Microsoft.Xna.Framework.Graphics; | |
namespace <#=CallContext.GetData("NamespaceHint")#> | |
{ | |
public static partial class Resources | |
{ | |
public static void Load(ContentManager content) | |
{ | |
<# | |
foreach(var r in resources) | |
{ | |
#> | |
<#=names[r.Type]+"."+r.Name#> = content.Load<<#=r.Type#>>(@"<#=r.Path#>"); | |
<# | |
} | |
#> | |
CustomLoad(content); | |
} | |
static partial void CustomLoad(ContentManager content); | |
<# | |
foreach(var name in names) | |
{ | |
#> | |
public static partial class <#=name.Value#> | |
{ | |
<# | |
foreach(var r in resources.Where(x=>x.Type==name.Key)) | |
{ | |
#> | |
public static <#=r.Type#> <#=r.Name#> { get; set; } | |
<# | |
} | |
#> | |
} | |
<# | |
} | |
#> | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment