How to protect my program's files?

Let's say that I make a game using whatever libraries, SDL2, SFML, Allegro... You name it. But then if I was to release the completed project, in order for it to properly work in another person's computer I would have to include all the sprite textures, music, etc... Alongside the executable right?

How could I make it so that people can not extract or modify the files, or even see them to some degree. Hide it, in other words.

I ask this out of curiosity more than anything because I've seen that in some games many files, such as background music, are stored away in, say, a BGM.dat file.
I guess it would be the same or similar for other files like textures and all that jazz...

Last edited on
The short answer is you can't, at least in absolute terms. Any type of data you store has to be reversible in some manner so that it can be correctly presented to the user. Ways that prevent the user from using data how they want is usually known as "DRM" (although that might not be exactly the correct term here) and it does more harm than good in many cases.

Type in "extract music/texture from [game]" on your favorite search engine, and 99% of the time you'll find some community that has a way to do it, regardless of how hard the developers try to make it not work.

Any crazy custom file format will eventually be decoded.

That being said, you can obfuscate your code. Just gonna put out some ideas here:
- Checksums: Have checksums somewhere in your file. For example, if your file contains 0x01XX2345 byte array , you can dedicate the XX byte to be a checksum of the other bytes. Or add another type of checksum to the end of the file.
https://en.wikipedia.org/wiki/File_verification
This prevents modification of the file unless the user knows exactly what they're doing. It often is good enough to prevent the average joe from doing it, and any dedicated community is going to be able to crack it anyway.
- Basic encryption: Something like XOR encryption will be plenty to prevent "scriptkiddies" from messing with your files. Just have the game itself reverse the XOR encryption when the game loads. No type of encryption is fullproof here, because if the game can decrypt something, by definition so can the person playing the game.
https://en.wikipedia.org/wiki/XOR_cipher

many files, such as background music, are stored away in, say, a BGM.dat file.
First thing to note here is that the extension of a file is meaningless. You can even have no extension! In some games, you can simply change the "custom" extension to ".png", ".wav" or what have you, and you'll be able to view the "actual" file in your file explorer. Other games are more complicated than this, though.

In .NET (on Windows), Visual Studio provides simple ways to embed any type of file into a DLL. This is yet another way to prevent it from being too obvious where the media files are.
https://stackoverflow.com/questions/1890688/can-i-embed-other-files-in-a-dll
A multi-platform solution can do the equivalent of this: Just stack multiple media files contiguously into one file, with an extra data member to say how big each section is for when the data is loaded into the game.

Other links:
https://stackoverflow.com/questions/6048883/how-should-i-stop-users-from-modifying-my-data-files
https://gamedev.stackexchange.com/questions/17674/hide-game-data-from-player
Last edited on
'wadfile' obscurity helps too. That is, you have an image followed by a video clip followed by another image followed by a music clip... all in one file, and only you know how to break it out and use it. If you throw a lightweight (fast!!!) encryption on top of that, your average person will struggle to get it out. Of course, they can still pipe the game's audio and video out to programs that can capture the data and record it to a new file, and they can still reverse engineer it, but you can make it annoying.

A LOT of games have gone the other route and made this stuff more accessible, not less, to allow the players to mod it. A game lives on in replayablity etc and money keeps rolling in if you have an active modding community doing your work for you (keeping people playing by creating content).
Last edited on
Thanks a lot for the answers. Love the amount of detail, links and all that
Topic archived. No new replies allowed.