I am building a game editor for an XNA game that I'm building that would accept JPEG/PNG, and output a XNB file that has Texture2D content in it.
I have my custom serializers for my custom etc and I'm dug everything down into invoking ContentCompiler using Reflection etc, gone into the real dirty bits, and I CAN actually get that part working for my own formats. But I now need to serialize to the regular Texture2D format that XNA know how to compile (in Visual Studio when building project) and load (in the game with the most used Content.Load method. I don't want to make my own image file format for this obviously. When I try to compile a Texture2D, it doesn't give me an error, it creates the XNB file, but the file contents are not image data, it's just some 3-4 KBs of overhead (which I think are the other, non-image properties of a Texture2D object).
Here is the code I use for compiling:
protected void InitializeWriter()
{
Type compilerType = typeof(ContentCompiler);
cc = compilerType.GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)[0].Invoke(null) as ContentCompiler;
compileMethod = compilerType.GetMethod("Compile", BindingFlags.NonPublic | BindingFlags.Instance);
}
internal void Write(string objectName, string path, object objectToWrite)
{
if (basePath == null)
{
throw new InvalidOperationException("The base path to write has not been set.");
}
if (cc == null) { InitializeWriter(); }
string fullPath = Path.Combine(basePath, path, objectName + ".xnb");
using (FileStream fs = File.Create(fullPath))
{
compileMethod.Invoke(cc, new object[]{
fs, objectToWrite, TargetPlatform.Windows, GraphicsProfile.Reach, false/*true*/, fullPath, fullPath
});
}
}
I have my own content processors and this code works with them flawlessly. This just calls the ContentCompiler's Compile method, but using Reflection (as it's an internal class).
I don't understand why Texture2D serializes, but without the actual bitmap data.
EDIT Sorry, it does NOT compile Texture2D (it complains about cyclic reference of my GraphicsDevice), if I try to compile a Bitmap object, it compiles with no errors but does not serialize the actual bitmap data)