Storing an exe in a string?

Started by
5 comments, last by iMalc 14 years, 5 months ago
I have a feeling that this is a really silly thing to do.. but it seems to work (at least on my computer). I have an inbuilt automatic updater for my game. The way it currently works is it downloads the files from the server (including the exe) using libcurl and then stores all that data in a vector of strings. Once it's all downloaded it saves these strings as files and restarts the game. Now, to my suprise this actually works. At least on my computer. It downloaded, restarted and then opened back up with a brand new updated version. Sound files, texture files and even the executable were all perfectly updated. Then other players of my game started getting weird problems after using the automatic updater and I began to think that perhaps the data was being corrupted somehow. So my question is simply: Can you store a file (executables in particular) in a string or should I use some other data type? Cheers Ben.
--------------------------Check out my free, top-down multiplayer shooter: Subvein
Advertisement
std::strings can hold any binary data, so there should be no corruption occuring there.

You have to be careful of things like C-style string functions, which would terminate processing on the data as soon as a null byte was reached (although you would probably notice this pretty quickly on binary data from an executable!)

Why aren't you just writing the data straight to a file? How are you updating the currently executing file?
I'm not writing directly to a file because I want to do it all at once. It would be a pain if half the files weer updated and then the game crashed or the user closed it or something. By saving it all to RAM I can ensure that it all quickly gets replaced just as the game closes. My game is only 14MB so this doesn't take up any space or anything.
--------------------------Check out my free, top-down multiplayer shooter: Subvein
Quote:Original post by bencelot
Then other players of my game started getting weird problems after using the automatic updater and I began to think that perhaps the data was being corrupted somehow.

You should probably have some kind of checksum to make sure patches are downloaded/installed correctly. Take a hash (something like md5 would be fine and you should be able to find code for easily) of all of the local files, then compare that against a hash calculated when you generated/uploaded the patch. If they differ you've got a broken patch somehow.

Bonus points if your patcher can then either revert to a previous working version or redownload the patch. This is why you'll often see auto updaters written as a seperate program so that if something goes wrong it'll be unchanged and can repair any damage.
Quote:Original post by bencelot
I'm not writing directly to a file because I want to do it all at once. It would be a pain if half the files weer updated and then the game crashed or the user closed it or something. By saving it all to RAM I can ensure that it all quickly gets replaced just as the game closes. My game is only 14MB so this doesn't take up any space or anything.

An alternative approach which might be more robust would be to download everything to a temp file and then move the files to their final location when everything is finished.

Better yet, if you install things to a sub-dir like so:

\YourGame  \Version1_0  \Version1_1  \Temp


Then you can download into temp and just rename it to 'Version1_2' when you're finished. That should be an atomic operation so there's no chance of something going wrong half way through. Then when you've verified that Version1_2 is correct via hashes you can delete the old versions.
Why not write to a file in the windows temp directory? Then if the download is interrupted, it doesn't really matter. Or, better - cleanup gracefully and delete the partial file.
Quote:Original post by bencelot
Can you store a file (executables in particular) in a string or should I use some other data type?
Sure you can!

I've got the world in a string... sittin on a rainbow...
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement