开发者

Using T4 Templates with VS2010 Express and XNA

开发者 https://www.devze.com 2023-03-05 06:31 出处:网络
I\'m using VS2010 express to create a game built with xna. I\'m trying to use t4 templates (to produce strongly-typed classes of content locations, thus using Level1Location = Content.Levels.Level1 in

I'm using VS2010 express to create a game built with xna. I'm trying to use t4 templates (to produce strongly-typed classes of content locations, thus using Level1Location = Content.Levels.Level1 instead of Level1Location = @"Content\Levels\Level1".

I've read that T4 templates aren't properly set up in the express editions, but that if i create a file with extension .tt it should work. However when i create a .tt file in my XNA class library i get the following warning (and no code file):

The custom tool 'TextTemplatingFileGenerator' failed. Could not load file or assembly 'Microsoft.VisualStudio.ServicesProxy, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

I've searched and searched and can't find anything useful. Has anyone encountered this problem before? Anyone know if a solution?

I've also tried changing the custom tool to TextTemplatingFilePreprocessor as suggested, however i get the same error.

EDIT: I've discovered that the issue is that it's in an XNA project / library. It works fine in a normal class so my work around is to add a project to the solution just for the template. The question is still op开发者_StackOverflow中文版en though, can you get it to work within a XNA project?


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;
        }
    }
#>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号