PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Can you store a .txt in resources and read from it?

I'd like to store information inside a text file, but I don't want it to be edittable by regular users after the game has been compiled. So I was wondering if it was possible to store a text file as a resource and read from it? I've looked up and down the MSDN docs and can't find anything on it. I do however assume it's possible, since I was able to do this very easily using reflection in C#. I just don't know where to find the information I'm looking for.

I did find string tables interesting, but not quite what I'm looking for (unless I misinterpreted their use). Basically what I'd like to do is store text files as a resource (so they're compiled with the executable) that contain game information (ie; a map layout) and read from them.. but I certainly don't want users editting them either *flashback from HEX editor days of good 'ol Diablo*

I am, as always, open to other suggestions.

Thanks in advance.
Could you load the text file as a custom resource and then use this code to load into memory.

Code:

HGLOBAL handle = NULL;
HRSC rsc = NULL;
HMODULE thismodule = GetModuleHandle (NULL);

rsc = FindResource(thismodule, _T("myfile.txt"), RT_RCDATA);

if(!rsc)
{
//Failed...
}

handle = LoadResource(thismodule, rsc);

if(!handle)
{
//Failed...
}



Any good?
Couldn't you use a resource editor to pull out these files?
gamefreaks:
That looks like what I'm looking for. I'll have to crack open MSDN and take a look. If anything it should be a step in the right direction, thanks.. :)

sponge:
I guess.. can they? Heh. I'm not too familiar with C++, that's why I asked for other suggestions if there were better alternatives. Certainly compiling a text file as a resource is better than just letting any 'ol user crack it open with textpad.
I've used resource editors on Windows to extract graphics, strings, and even change the structure of menus.
Sponge is right, although putting the file inside the exe is better than just leaving it as a txt file, a determined 'hacker' will just fire up the exe in a hex editor and get the text that way.

Doom has some of its data programmed into the exe as data tables but someone simply created DeHacked to hack the exe.

Anyway, it's quite hard to hide text without encoding or compressing it.
(Maybe use WinZip to compress the text file, and then zlib to decompress it in memory?)
I'm not too worried about it. Hiding it from "regular" users is good enough, and it's all I really want ATM. If I was making a multiplayer game of sorts I'd probably take extreme lengths, but it's not too big of a deal with a single player game.
But now we know ;) I'd agree, if zipping it, or hell, even ROT13 were easy to implement, it'd be a quick, easy, additional step.
Two things...

(1) Storing a text file as a resource is of course doable. Just treat it as a block of memory and do what you want with it, no big deal. Some simple code for you:

HRSRC hResInfo = FindResource(hInstance, MAKEINTRESOURCE(IDR_MY_TEXT_FILE), TEXT("TEXTFILES"));
HGLOBAL hRes = LoadResource(hInstance, hResInfo);
void* pvRes = LockResource(hRes);
unsigned char* my_text_file = (unsigned char*)pvRes;

Now you have a pointer to your block of memory, have at it!

(2) If you want to put just a little bit of protection into it, one simple thing to implement is to alternate shifting byte values up and down. For instance, if you have the word SCORE in your file, "encrypt" it as TBPQF. You can probably throw together a quick-and-dirty little utility to do this to a text file in no time, and getting it back to it's original form is obviously not a big deal. It's better than nothing I suppose, but not by much. To make it just a little harder, come up with a pattern, say ten variations long, each variation is a shift +- X where X is some value. So, maybe +3,-1,+5,+9,+2,-3,-4,+9,-9,-7 is your pattern rather than +1,-1 like I first said.

Again, I'm not suggesting rocket science by any stretch, but it makes it just a little harder, and will fairly effectively keep out anyone that's not capable of looking at compiled code and reverse-engineering it.
Thanks for the suggestions and help, I think I can handle it from here.

Just one more question. :) Do I need to delete the loaded resource after I'm done using it? I think I read that memory allocated for resources is automatically reclaimed when the executable they're in is exitted, but I certainly don't need the resource for the entire duration of the program.
wyrd: Since we're working on similar projects, I understand where you're coming from. ;) What I did was store all my data in an external file and do all kinds of encryption and compression so it will also be hard to hex-edit. It's one option you can also consider besides storing it as a resource. There are many types of encryption/compression code available on the net for C++.

- Erick
My understanding is that the code as I've written it does not require you to free anything afterwards, and in fact I'm pretty sure there is no UnlockResource() method on PocketPC. Someone will of course correct me if they know otherwise, but locking a resource I think doesn't allocate any memory, just gives you a pointer to it.

Note also that I believe as I wrote it, you cannot modify the data you have the pointer to. To do that, I think you would have to malloc some memory and memcpy your resource data to your allocated memory, which you of course WOULD have to free at the end.
egarayblas:
What sort of encryption/compression tools do you recommend?

fzammetti:
Good enough, thanks again. :)
Yeah according to the WinCE API documentation, you can only free certain types of resource, (not the custome type) and the HGLOBAL pointer is only returned to be compatible with earlier versions, so no need to call HeapFree() on it.