3 min read

Extending the Content Pipeline - Part 4

Building the Importer

Now that we’ve got the level information set up, we will start working on our content pipeline extension. Our first step will be to create the Content Importer, which has the job of reading in the information from the file. Once again, we will create a new class to do this. XNA provides a template for a content importer which will make this extremely easy to do. Go to your content pipeline extension project (I’ve called mine ‘LevelContentPipelineExtension’) and add a new class by right-clicking on the project and choosing Add > New Item. In the dialog that appears, choose the Content Importer Template, not the Class template, and give the importer a name. (If you don’t see that template, you may need to choose XNA Game Studio 4.0 on the left side, under Installed Templates.)

I have called mine “LevelImporter.cs”. Once you click on OK, the new file will be created and opened.

There are only two real things we need to do with the importer. First, there is a line above the class definition that starts with [ContentImporter(. We need to change the information on this line. The first part, which says “.abc”, indicates the extension of the filename that, if a file is added to the project’s content, and it has this extension, it should import it with this importer by default. Change this to be whatever extension you are using for your files. For this tutorial, I am going to create my files with an extension of .level, so I replaced .abc with .level. Next is the display name, which is what the importer is called in XNA Game Studio’s GUI. We’ll look at this a little more later on. You can call this whatever you want. I’ve called mine “Level Importer”. The last thing that we need to do is set the name of the default processor, which is the last thing on that line. This is the class name of the processor that it should use by default. We haven’t created this yet, so you’ll need to decide now what you’re going to call that class!) I’ve replaced this in mine with LevelProcessor. So the whole line should now look something like:

[ContentImporter(".level", DisplayName = "Level Importer", DefaultProcessor = "LevelProcessor")]

The other thing that we will need to do is actually import the data from the file. In our case, this is going to be pretty simple. In fact, we are just going to read in the entire file and put it into a string. This can be done with a single line of code, shown below. Add the following code to the middle of the Import() method, and replace everything that is currently in there:

return System.IO.File.ReadAllText(filename);

So now the completed importer class should look something like this:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;

using TImport = System.String;

namespace LevelContentPipelineExtension
{
    [ContentImporter(".level", DisplayName = "Level Importer", DefaultProcessor = "LevelProcessor")]
    public class LevelImporter : ContentImporter<TImport>
    {
        public override TImport Import(string filename, ContentImporterContext context)
        {
            return System.IO.File.ReadAllText(filename);
        }
    }
}

The importer is pretty simple to do. This is mostly because the task of the importer is to read in the data. It isn’t actually supposed to do any processing–that’s the job of the processor, which we will do next. Of course, you aren’t just limited to reading in a file here. For instance, you could read some information in a file and connect to a database or something. There’s a lot of things that you can do in the importer, but keep in mind that it is a really good idea to separate the importing from the processing, so you should avoid doing any actual processing here. Anyway, the importer is done, and it is time to move on to the processor!