Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a strongly typed structure for accessing files in an XNA Content Project?

Preamble:
I'm working with an XNA Content project to hold all of the various textures (and possibly other resources) that I'm using as part of developing a game.

The default method of loading images from the Content project into an XNA texture object involves the use of hard coded string literals to point to the various files.

I would like to automate the projection of the directory/file tree inside the content project into an object hierarchy to avoid using string literals directly, and to gain the benefits of using strongly typed objects.


Example:
Instead of using
Texture2D tex = Content.Load("Textures/defaultTexture32");
I'd much prefer
Texture2D tex = Content.Load(Content.Textures.defaultTexture32);


Question:
Is there an already existing solution to this problem? (I couldn't find anything with Google)


Extra details:
I'm fairly certain this can be done through a T4 template; probably in conjunction with the DTE tools. I've made an initial attempt to do this, but I keep hitting blocks due to my inexperience with both tool sets, but I've worked with T4MVC in the past which does something similar; unfortunately it projects class structure rather than the file system and is not easily adapted.

I do not require the solution to use T4 or DTE, they simply seem as though they're likely to be part of a solution.

Only including files that are part of the VS project (rather than the entire on-disk file system) would be preferable, but not necessary.

The ability to additionally filter by file types, etc. would be an extra special bonus.

For anyone that doesn't immediately see the benefits of doing this; consider what would happen if you renamed or deleted the file. The application would continue to compile fine, but it would crash at runtime. Perhaps not until a very special set of circumstances are met for a certain file to be accessed. If all the file names are projected into an object structure (which is regenerated every time you build the project, or perhaps even every time you modify) then you will get compile-time errors pointing out the missing resources and possibly avoid a lot of future pain.

Thanks for your time.

like image 513
Mir Avatar asked Aug 14 '11 18:08

Mir


1 Answers

Here is a T4-template which will read all the files in a "Textures" folder from your project-directory. Then they'll be written into a class as strings, you can just change Directory.GetFiles() if you wish to limit the file-search.

After adding/removing files you can click "Transform All Templates" in Solution Explorer to generate a new class.

Hope this helps!

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<#@ import namespace="System.IO" #>
<# var files = Directory.GetFiles(Host.ResolvePath("Textures"), "*.*"); #>
namespace Content
{
    class Textures
    {
<#      foreach (string fileName in files) { #>
        public const string <#= Path.GetFileNameWithoutExtension(fileName) #> = @"Textures/<#= Path.GetFileNameWithoutExtension(fileName) #>";
<#      } #>
    }
}
like image 87
Ronny Karlsson Avatar answered Nov 09 '22 15:11

Ronny Karlsson