Getting the content type from ContentManager in XNA

634 views Asked by At

I'm building a content editor for an XNA game and I've got my Content Readers and Writers ready. In my editor, I'll be iterating over all the files in my folder to display a list of objects in my editor window. I've got the things set up, but I'm stuck with what I'll do after getting the file list of my content folder.

All the files have the XNB extension, and the only (non-hacky) way to read them is to use the XNA Content Reader. But I'd like to know the content type of the files (is it a map or a game object template or anything from lots of other types that I've defined) in advance without loading them all one by one and trying to load each with each of the possible content types, it is practically impossible (or let's say, worst programming practice ever).

How can I get something like this functionality:

ContentManager.ContentTypeOf(string assetPath); that returns a type, so I can know in advance what to load (and not load) where. It won't be a good idea to load all the maps of the game just to edit a single object template. There must be a practical way to distinguish the types of the content files.

4

There are 4 answers

0
Can Poyrazoğlu On BEST ANSWER

Ok, after not finding any trivial solution with actually reading the files, I've decided to organize the files into a folder hierarchy, and I now know what file is what. It does the job at least. May not be the most feasible solution, but, it works.

1
Andrew Russell On

This is pretty easy, just load it as type Object:

Type type = content.Load<Object>("MyContent").GetType();

The only downside is that you do have to load the entire asset to figure out its type.

To implement a different lifetime policy for content loaded this way, consider using a separate ContentManager instance, or even deriving your own class from ContentManager and look into the protected ReadAsset method.

4
Blau On

You can access to the xnb to get the content reader name.

Here you are the xnb format specification file

0
ThoWoi On

Maybe a little late, but I had the same problem.

I just used my own content processor, adding an empty file to the output directory "[modelfilename].model". And enumerated in my code all *.model files, then loading the model by just the filename without the extension. You could also easily put more information in that file, if needed.

Code for the custom content processor:

 string Name = Path.GetFileNameWithoutExtension(context.OutputFilename);
 FileStream fs = File.Create(Path.GetDirectoryName(context.OutputFilename) + @"\" + Name + @".model");
 fs.Close();