# (XNA) Possible to hide, compress, or rename .XNB files?

I'm fairly new to XNA (only a week into C# and XNA at this point) but I have been developing games for a while now, and the program I used did not require any/many external files in creating an executable. I am perfectly fine with external files (for the most part) but reading around, it seems as if .XNB files are easily accessed for others to pull your resources out of.

After some thinking, I was wondering if it was possible to do any of these 3 things: 1. Hide the .XNB files within the .exe in which case it would likely create them in an external location only during run-time (wouldn't really solve the main issue, but still)? 2. Compress many of the .XNB binaries into a single binary (such as 1 for sounds, 1 for sprites, etc.) 3. (This one is one simply for my own OCD) Change the extension name from .XNB to something of my choosing? For example, to .DAT instead to make it less of a direct indicator I used XNA and/or these files can be accessed with an .XNB ripper?

None of these would particularly solve my initial problem, but I still wonder if they are possible. Anyhow, thanks in advance guys!

To answer the three of your questions with one answer: sure you could, but you wouldn't be able to use the default content manager.

However, you can create custom content processors to output file data in a format that only you would immediately know. This would still allow you to use the default XNA content manager (this.Content in any Game-derived class) as well as allow you to protect your assets in any way you want. The generated content files would still have the .xnb extension, but they would not have the typical .xnb format. (You may be able to change the extension, but I'm not sure.)

• Alright thank you for the answer! Would you happen to know if there is a tutorial for this process somewhere? Or will I have to figure this out on my own. Being so new, I'm not sure the second option will happen for a while! – BlackAfricano Aug 1 '14 at 3:41
• If you click on the link I gave you, Microsoft has a bunch of pages on creating your own processors and at least one example project. Plus a quick Google search revealed quite a few examples posted on blogs. – fastinvsqrt Aug 1 '14 at 3:58
• To expand on the answer above, you can load textures and other resources into their XNA representation from a filestream, as you would usually load any file. You can then use Texture2D.FromStream() to simply load the files into a texture variable. This way, you can compress/hide the files any way you choose. Here's a pastebin of the contentManager i'm using for my engine. pastebin.com/0j6TAdHh – Lex Webb Aug 1 '14 at 16:14
• Awesome answers thanks you guys! Thanks for being so welcoming to a new member. Mainly, just the knowledge that it was possible is helpful for me but the examples are great. – BlackAfricano Aug 3 '14 at 0:13
• If you want to get really advanced, you could use the content pipeline to compile your content at run-time from hard-coded variables, and then change the name of the output file or hide it or whatever you want. Presumably, you would then use the content pipeline again later in the game to unhide/rename the files and deserialize them. This solution is very security-friendly, but it would require your players to install the content pipeline with your game instead of just the XNA runtime. – Michael Hoffmann Aug 4 '14 at 7:25

Yes, you can implement your own content manager.

public class ZipContentManager : ContentManager
{
private ZipFile zipFile = null;

public ZipContentManager(IServiceProvider services, string zipFile) : base(services, "")
{
this.zipFile = new ZipFile(zipFile);
}

protected override System.IO.Stream OpenStream(string assetName)
{
var entry = zipFile.GetEntry(assetName.Replace('\\','/') + ".xnb");
if (entry != null)
{
return zipFile.GetInputStream(entry);
}
return null;
}
}