I'm obfuscating spritesheets. I've already encrypted the .txt file dictionaries. Now I'd like to perform binary serialization on the image files. Texture2D does not allow for this though, probably because it's coupled to the ContentManager and by extension the Graphicsdevice.

The solution I'd like to go for would be serializing the image files in a single wrapper object in the obfuscation code, and deserializing it when the game is loading. However, Content.Load<Texture2D>() will only take the asset name as parameter. Is there a workaround, or am I looking at this problem the wrong way?

  • 1
    \$\begingroup\$ You could always write your own Content Pipeline extension. \$\endgroup\$ May 5, 2014 at 13:57
  • 2
    \$\begingroup\$ You can always do Texture2D.fromStream. Load up the encrypted file, decrypt it, throw it into a memory stream, and finally load it into the Texture2D via to fromStream method. Curious why this is deemed necessary though? There is little to gain in client side security. Considering its C# they can decompile your program and use it to decrypt the assets, most likely rather trivially. \$\endgroup\$ May 5, 2014 at 14:20
  • \$\begingroup\$ @VaughanHilts Will that work? I'm under the impression you can only add types - not modify the available formats of existing types. \$\endgroup\$ May 5, 2014 at 14:25
  • \$\begingroup\$ @ClassicThunder Well... you defeat bog-standard XNB readers. So it raises the bar to people who can decompile. And then, if you obfuscate your assembly, it raises the bar even further. Further still if you only target, say, iOS. So it's not completely without merit. \$\endgroup\$ May 5, 2014 at 14:30
  • \$\begingroup\$ @AndrewRussell More than doubting the effectivness, I doubt the need. \$\endgroup\$ May 5, 2014 at 14:43

1 Answer 1


Probably the ideal extension point for something like this is to inherit from ContentManager. Here is a blog post (for XNA) that explains how to use ReadAsset to change the caching policy (not what you want).

What you probably want to do is inherit from ContentManager and override OpenStream. Then simply implement obfuscation at the stream level (ie: obfuscate the entire asset file).

Alternately you could just override Load and handle Texture2D explicitly to do whatever you want. You could, for instance, call ReadAsset<ObfuscatedTexture2D>(...) and then construct a Texture2D instance yourself and return that.

If you're creating the texture yourself, you'll need a graphics device to load it onto. It can be accessed through the ContentManager.ServiceProvider property. Like so:

var gds = ServiceProvider.GetService(typeof(IGraphicsDeviceService)) as IGraphicsDeviceService;
var graphicsDevice = gds.GraphicsDevice;

(Note: This will work in XNA. It should work in MonoGame, but if not... use the source.)


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .