Is it possible to decompile a .xnb file? I've lost my shader code, but only have my .xnb file left.

• I'd be very surprised if you get back anything like the orignal source as shaders are heavily optimised during compilation (not just compressed into XNB format. I'd re-write it. That said, here's the XNB file format – George Duckett Feb 20 '12 at 11:12

It's unlikely you'll be able to retrieve the source from the xnb file as it has undergone compilation into a binary format during the XNA pipeline stages. While it's theoretically not impossible, in my opinion you'd probably be much better off rewriting the shader..

The best you can get is a disassembly, which may help you recreate the source code. (Unless someone can point you to an actual HLSL decompiler. I've never seen one.)

If you happen to be using XNA 3.1 you could simply load it into an Effect object via the content system and then call Effect.Disassemble on it.

XNA 4.0 removes this - so it will be much harder:

First of all you need the actual effect data. Probably the easiest way to get it would be to use reflection to get access to the private Effect.pCachedEffectData member which (I think) contains it.

Once you have the binary data, you'll want to disassemble it. I can't figure out a way to use fxc.exe to disassemble the data directly. But perhaps you can install XNA 3.1 and use that. Or use Direct3D and D3DDisassemble - which is, I suspect, what XNA 3.1 uses internally.

• Can you show me how to write the code to use Disassemble? I've tried but it complains that it can't find the file. – Neil Knight Feb 20 '12 at 19:24
• @NeilKnight Not really. But perhaps ask in a fresh question and provide more details. – Andrew Russell Feb 22 '12 at 1:07