Author Topic: How to ignore right number of characters using ifstream::ignore (C++)  (Read 5634 times)

Offline Vagabond

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1015
I am trying to load the map part of a saved game into memory (SGAME0.OP2). Saved game files contain some header data not present in map files. I don't really need the info in the saved game header, so it could just be ignored, or loaded into a variable.

Source Material Location: \Outpost2SVN\OllyDbg\InternalData\SavedGame and Map.txt

Code: [Select]
Outpost 2 .op2 File Format (Saved Games)
----------------------------------------

Offset Size Description
------ ---- -----------

0x0 0x19 "OUTPOST 2.00 SAVED GAME", 0x1A, 0 - String must match exactly or else load error
---------------------------
Note: The following is repeated as an array of 0x74 (116 decimal) elements
0x19+i*0x1E0 4 **TODO** Figure this out
0x1D+i*0x1E0 0x1DC **TODO** Figure this out
---------------------------
Note: From here, the format matches that of .map files

So, I'm using the following:

Code: [Select]
ifstream file(filename, ios::in | ios::binary);

if (saveFile)
}
    file.ignore(0x19);
    file.ignore(0x74);
}

file.read((char*)&mapHeader, sizeof(mapHeader));

When I load a map file, the code works fine. When trying to load a save file, it appears the map header data isn't being loaded properly. I'm assuming that my ignore commands are not placing the ifstream at the proper position to start reading the map data, but I'm not very familiar with doing this.

Any help would be appreciated!

Offline Sirbomber

  • Hero Member
  • *****
  • Posts: 3238
Your math is wrong.  Read the documentation again.  The structure after the header is an array of 0x74 elements with two structures each of size 0x1E0, not a single structure of size 0x74.
"As usual, colonist opinion is split between those who think the plague is a good idea, and those who are dying from it." - Outpost Evening Star

Outpost 2 Coding 101 Tutorials

Offline Vagabond

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1015
Thanks Sirbomber,

I set it to:

Code: [Select]
file.ignore(0x1E025);

And now, all the map data is reading in properly.

-Brett

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4955
I see my comment in the other thread was superfluous. Glad to see you got this working.

Offline Vagabond

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1015
I ended up using HXD to determine the offset of 0x1E025 for loading the map data from the save file. You can basically search for the width and height of the map which appears near the beginning of the map portion.

I cannot get the math to work to get 0x1E025 from the structure sizes listed in the file snippet earlier in the thread.

If someone has time to break it down for me for my own knowledge on the subject, it would be appreciated.

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4955
You're right, the math doesn't seem to work out. Looks like an error in the description of the file format.  :-[

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4955
I've updated the text file describing the saved game and map file formats. It was missing a section in the header of saved game files. I also filled out some details on the meaning of the previously unknown fields.

Code: [Select]
Offset  Size    Description
------  ----    -----------
0x0     0x19    "OUTPOST 2.00 SAVED GAME", 0x1A, 0 - String must match exactly or else load error
---------------------------
0x19    0x1086C fileDialogData
0x10885 0xD7A0  sheetData[0x73]  (array of 0x73 (115) UnitTypeInfo objects, each loading 0x1E0 bytes of data)
        Note: Technically each object loads data through a virtual function call, so each object could load a different amount of data. However, there are no overrides, so they all call the same base class function, and hence all load the same data.
          4       int requiredTechNum
          0x1DC   struct[] unitTypeInfo.playerInfo[]
---------------------------
0x1E025 ...     From here, the format matches that of .map files

Offline Vagabond

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1015
Thanks for the update.

Since I'm new to HXD and memory offset, I was worried the problem would be that I didn't know how to sum a range of hexadecimal values (at least after sirbomber's first correction).  :-\

Good to see someone break my dozen long commit streak into the repo. :)

-Brett