c# - Using T4 Templates with VS2010 Express and XNA -


i'm using vs2010 express create game built . i'm trying use templates (to produce strongly-typed classes of content locations, using level1location = content.levels.level1 instead of level1location = @"content\levels\level1".

i've read t4 templates aren't set in express editions, if create file extension .tt should work. when create .tt file in xna class library following warning (and no code file):

the custom tool 'texttemplatingfilegenerator' failed. not load file or assembly 'microsoft.visualstudio.servicesproxy, version=10.0.0.0, culture=neutral, publickeytoken=b03f5f7f11d50a3a' or 1 of dependencies. system cannot find file specified.

i've searched , searched , can't find useful. has encountered problem before? know if solution?

i've tried changing custom tool texttemplatingfilepreprocessor suggested, same error.

edit: i've discovered issue it's in xna project / library. works fine in normal class work around add project solution template. question still open though, can work within xna project?

although i've posted answer, i'm still looking simpler way of doing (i.e. tt file in xna project self)

in case finds page, here's work around:

create new (non-xna) class library project.

add text file, renamed .tt extension.

write t4 code (see below).

in xna project, add existing item, navigate created .cs file, , add link.

then, ensure have updated cs file, rightclick xna project , click project dependencies, tick class library project containing .tt file.

using template code below, can things content.load(content.mygamecontent.graphics.textures.awesometexture); can folder names strings content.mygamecontent.graphics.textures example, 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 iserviceprovider; var dte = serviceprovider.getservice(typeof(dte)) 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;         }     } #> 

Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -