I'm struggling to figure out the 'right' way of working with my custom content. I have an application that allows you to load a texture and then define simple point-based shapes (rectangles, triangles, polygons, etc) on top of that texture. This could be useful for UV mapping someday. The application then writes this data, with a reference to the texture (Texture2D int marked as serializable) to a binary data file. Now, I'm trying to write a ContentImporter and ContentProcessor so that these files can simply be dropped into my Content project and usable in a game.
The methods to load and save data exist statically on the data class, and are working 100% in the original application, but not in my ContentImporter. I have tried using my static method to load the data, as well as loading it manually - It would seem that the Deserialize method of the BinaryFormatter is returning null.
// The TImport type
using TImport = Wrath.Content.TextureShapes;
// First Method
public override TImport Import(string filename, ContentImporterContext context)
{
TextureShapes shapes = TextureShapes.Load(filename);
if (shapes == null)
{
context.Logger.LogImportantMessage("Cannot import " + filename);
return null;
}
context.AddDependency(shapes.Texture);
return shapes;
}
// Second Method
public override TImport Import(string filename, ContentImporterContext context)
{
TextureShapes shapes = null;
FileStream file = null;
try
{
file = File.Open(filename, FileMode.Open);
BinaryFormatter binary = new BinaryFormatter();
shapes = (TextureShapes)binary.Deserialize(file);
file.Close();
}
catch
{
if (file != null)
file.Close();
shapes = null;
}
if (shapes == null)
{
context.Logger.LogImportantMessage("Cannot import " + filename);
return null;
}
context.AddDependency(shapes.Texture);
return shapes;
}
The file is already in a binary format, so I could just load it as usual in my game, but I thought that using the Content Pipeline is a 'better' way to load things in the game.
What am I doing wrong? Is there a better way to do this? Suggestions?