Eugene Chefranov
Eugene Chefranov

Reputation: 171

C# and hex offset range

Sorry for a newbie question. I have a game save file which contains some information about game character. I want to change some stats, for example money or skills and I know where is the data but I don't understand how to work with hex range offset. For example, I know that skill points are between 4863-4866 and How will be correct to read and to write new values?

enter image description here

My attempts in code:

        int skill = 0;

        using (BinaryReader br = new BinaryReader(File.OpenRead("Player.chr")))
        {
            br.BaseStream.Position = 0x12FF; // read position
            label1.Text = br.ReadInt32().ToString();
        }

        using (BinaryWriter bw = new BinaryWriter(File.OpenWrite("Player.chr")))
        {
            bw.Seek(0x12FF, SeekOrigin.Begin); // go to position
            bw.Write(skill + 10); // plus 10 skill points
        }

Upvotes: 0

Views: 254

Answers (2)

Dmitry
Dmitry

Reputation: 327

If you want to increase value of skill, try correctly read in a variable "skill":

        int skill = 0;

        using (BinaryReader br = new BinaryReader(File.OpenRead("Player.chr")))
        {
            bw.Seek(0x12FF, SeekOrigin.Begin);
            skill  = br.ReadInt32();
        }


        using (BinaryWriter bw = new BinaryWriter(File.OpenWrite("Player.chr")))
        {
            bw.Seek(0x12FF, SeekOrigin.Begin); // go to position
            bw.Write(skill + 10); // plus 10 skill points
        }

It's code replace old value "skill" in file.

Upvotes: 1

Eric
Eric

Reputation: 2265

Try this:

byte[] data = File.ReadAllBytes("Player.chr");
data[0x12FF] += 10;
File.WriteAllBytes("Player.chr", data);

There's a caveat: This is manipulating a byte (8-bit). If your target is something like an int (16/32-bit), you'll have to adjust accordingly.

Upvotes: 0

Related Questions