XNA extending an existing Content type
- by Maarten
We are doing a game in XNA that reacts to music. We need to do some offline processing of the music data and therefore we need a custom type containing the Song and some additional data:
// Project AudioGameLibrary
namespace AudioGameLibrary
{
public class GameTrack
{
public Song Song;
public string Extra;
}
}
We've added a Content Pipeline extension:
// Project GameTrackProcessor
namespace GameTrackProcessor
{
[ContentSerializerRuntimeType("AudioGameLibrary.GameTrack, AudioGameLibrary")]
public class GameTrackContent
{
public SongContent SongContent;
public string Extra;
}
[ContentProcessor(DisplayName = "GameTrack Processor")]
public class GameTrackProcessor : ContentProcessor<AudioContent, GameTrackContent>
{
public GameTrackProcessor(){}
public override GameTrackContent Process(AudioContent input, ContentProcessorContext context)
{
return new GameTrackContent()
{
SongContent = new SongProcessor().Process(input, context),
Extra = "Some extra data" // Here we can do our processing on 'input'
};
}
}
}
Both the Library and the Pipeline extension are added to the Game Solution and references are also added. When trying to use this extension to load "gametrack.mp3" we run into problems however:
// Project AudioGame
protected override void LoadContent()
{
AudioGameLibrary.GameTrack gameTrack = Content.Load<AudioGameLibrary.GameTrack>("gametrack");
MediaPlayer.Play(gameTrack.Song);
}
The error message:
Error loading "gametrack". File contains Microsoft.Xna.Framework.Media.Song but trying to load as AudioGameLibrary.GameTrack.
AudioGame contains references to both AudioGameLibrary and GameTrackProcessor. Are we maybe missing other references?
EDIT
Selecting the correct content processor helped, it loads the audio file correctly. However, when I try to process some data, e.g:
public override GameTrackContent Process(AudioContent input, ContentProcessorContext context)
{
int count = input.Data.Count; // With this commented out it works fine
return new GameTrackContent()
{
SongContent = new SongProcessor().Process(input, context)
};
}
It crashes with the following error:
Managed Debugging Assistant 'PInvokeStackImbalance' has detected a problem in 'C:\Users\Maarten\Documents\Visual Studio 2010\Projects\AudioGame\DebugPipeline\bin\Debug\DebugPipeline.exe'.
Additional Information: A call to PInvoke function 'Microsoft.Xna.Framework.Content.Pipeline!Microsoft.Xna.Framework.Content.Pipeline.UnsafeNativeMethods+AudioHelper::OpenAudioFile' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
Information from logger right before crash:
Using "BuildContent" task from assembly "Microsoft.Xna.Framework.Content.Pipel
ine, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553".
Task "BuildContent"
Building gametrack.mp3 -> bin\x86\Debug\Content\gametrack.xnb
Rebuilding because asset is new
Importing gametrack.mp3 with Microsoft.Xna.Framework.Content.Pipeline.Mp3Imp
orter
Im experiencing exactly this:
http://forums.create.msdn.com/forums/t/75996.aspx