[...]
I have a file which is a saved game from Imperialism 2 (really old game).
So instead of opening it in the Hex editor and as a little programming
project I decided to build a save game editor.
In short:
You write down how much paper and bronze you have in the game.
Load the save game in the app and than type it into two text boxes the
noted values.
The application searches for this two values in the save game and
displays the values as int in another set of text boxes.
In this second set of text boxes you can change the value to what ever
you want (from 1 to 9000).
Than hit save and the app writes the changed values to the save game
file.
I hope that this enough information.
Probably it is.
My first comment: you run the risk of completely corrupting the data file
with this approach. There's no guarantee that the sequence of bytes that
corresponds to the data you're trying to change doesn't also appear
elsewhere in the file. So, either you'll change the first appearance, in
which case you may or may not change the data you want, or you'll change
all appearances, in which case you'll change the data you want, but also
possibly data you don't want to change.
If you want this to be reliable (and maybe in this case it's not that
important
), you need to be doing more parsing of the file than just
searching for specific numbers. You really ought to incorporate logic in
your program that knows more about the structure of the file and has a
reliable way of finding exactly the position in the file where the data
you want is.
The added benefit to that approach would be that you wouldn't have to tell
the program the original values in the first place. It would just display
them to the user.
If you insist on pursuing the original strategy, then most of what we've
written so far still applies, except that you probably don't really need
to be operating in hexadecimal. As long as the numbers you want to change
are stored as literally the actual numbers you see in the game, then you
can do everything using decimal representation. The general steps would
be:
-- Use int.Parse() on the text entered in the text boxes
-- Use the BitConverter class to convert the int values to an array of
bytes
-- Use the FileStream class to read bytes from the file, comparing
them to the bytes representing the data you're looking for
-- When you find a match, use the FileStream class to write the new
bytes you want (you'll have to use the Position property or Seek() method
to set the file cursor back to the right place for writing)
Hope that helps.
Pete