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