Although i've posted this answer, i'm still looking for a simpler way of doing this (i.e. tt file in the XNA project its self)
In case anyone finds this page, here's my work around:
Create a new (non-XNA) class library project.
Add a text file, renamed with the .tt extension.
Write your T4 code (see below).
In your XNA project, add an existing item, navigate to the created .cs file, and add as link.
Then, to ensure we always have the updated cs file, rightclick the XNA project and click project dependencies, then tick your class library project containing the .tt file.
Using the template code below, you can do things like Content.Load(Content.MyGameContent.Graphics.Textures.AwesomeTexture); You can also get folder names as strings with Content.MyGameContent.Graphics.Textures for example, thanks to a funky implicit string conversion operator.
<#@ template language="c#" hostspecific="true" #>
<#@ assembly name="EnvDTE100" #>
<#@ assembly name="EnvDTE" #>
<#@ assembly name="System" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="EnvDTE100" #>
<#@ import namespace="EnvDTE" #>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Collections.Generic" #>
namespace Content
{
<#
var serviceProvider = this.Host as IServiceProvider;
var dte = serviceProvider.GetService(typeof(DTE)) as DTE;
foreach (Project Proj in dte.Solution.Projects)
{
bool IsContentProj = false;
foreach(Property Prop in Proj.Properties)
{
if(Prop.Name == "Microsoft.Xna.GameStudio.ContentProject.ContentRootDirectoryExtender.ContentRootDirectory")
{
IsContentProj = true;
}
}
if (IsContentProj)
{
#>
public static class <#=Proj.Name #>
{
<#
foreach(ProjectItem PI in Proj.ProjectItems)
{
GenerateProjectItemClass(PI, true, " ");
}
#>
}
<#
}
}
#>
}
<#+
void GenerateProjectItemClass(ProjectItem Item, bool Static, string Indent)
{
const string FolderItemKind = "{6BB5F8EF-4483-11D3-8BCF-00C04F8EC28C}";
const string FileItemKind = "{6BB5F8EE-4483-11D3-8BCF-00C04F8EC28C}";
string ClassName = Path.GetDirectoryName(Item.FileNames[0]).Substring(Path.GetDirectoryName(Item.FileNames[0]).LastIndexOf(Path.DirectorySeparatorChar) + 1);
int ContentRootLength = Path.GetDirectoryName(Item.ContainingProject.FileName).Length;
string RelativeLocation = Path.ChangeExtension(Path.GetFullPath(Item.FileNames[0]).Substring(ContentRootLength + 1), null);
switch(Item.Kind)
{
case FolderItemKind:
#>
<#=Indent#>public class <#=ClassName #>Class
<#=Indent#>{
<#=Indent#> private const string Location = @"<#= RelativeLocation #>";
<#=Indent#> public static implicit operator string(<#=ClassName #>Class MyClass)
<#=Indent#> {
<#=Indent#> return Location;
<#=Indent#> }
<#+
foreach(ProjectItem ChildItem in Item.ProjectItems)
GenerateProjectItemClass(ChildItem, false, Indent + " ");
#>
<#=Indent#>}
<#=Indent#>
<#=Indent#>public <#= Static ? "static " : " " #><#=ClassName#>Class <#=ClassName#> = new <#=ClassName#>Class();
<#=Indent#>
<#+
break;
case FileItemKind:
#>
<#=Indent#>public <#= Static ? "static " : " " #>string <#= Path.GetFileNameWithoutExtension(Item.FileNames[0]) #> = @"<#=RelativeLocation #>";
<#+
break;
}
}
#>