0

Edit: I didn't explain myself properly. Try again.

I am writing a save game editor. The editor loads in the save game, checks the version. The version information was held in various "Data" statements, once the version was checked the correct offsets were loaded. The offsets were then assigned to variables. Below is an example of offsets for two versions of the game (Sensible World of Soccer):

Swos_Beta_09 = {  # _update: 95/96
'num_players_pos': 56717,       # - Number of players in the team
'team_name_pos': 55429,         # - Team Name
'mgr_forename_pos': 54987,      # - Managers forename
'mgr_surname_pos': 54996,       # - Managers Surname
'mgr_fullname_pos': 55460,      # - Managers Full Name (Yes it is stored twice!!)
'plr1_Position_pos': 55500,     # - Position of the first player in the file. This is then 38 bytes of data
'money_pos': 54742,             # - Money
'tact_pos': 88374,              # - First Tactic
'first_team': 90635,            # - First team in the file
'CJOffset': 90614,              # - File position of the version number
'CJ': "CJ281112",               # - This is the version we are expecting
'version_pos': "Sensible World of Soccer v0.09 - 28/11/1994 11.00am"  # - And this is the version

Swos_Release_v10 =                 # _update: 95/96
'num_players_pos': 56719,       # - Number of players in the team
'team_name_pos': 55431,         # - Team Name
'mgr_forename_pos': 54989,      # - Managers forename
'mgr_surname_pos': 54998,       # - Managers Surname
'mgr_fullname_pos': 55462,      # - Managers Full Name (Yes it is stored twice!!)

In python I used dictionaries, and then once checked the version (CJ031223 for example) it copied the correct offsets into another dictionary (or list) allowing me to access the data.

note: I will be using classes for the teams, and the players, I am not concerned with that here. I am looking for the best way to do this in c++ please.

Mark Green
  • 331
  • 1
  • 3
  • 5
  • Such a structure would be sub-optimal in Python too. A single player's status should be a single class, not a collection of key/value pairs. You don't need use a dictionary in Python to serialize data structures (that's what this is about). You use pickle or some other serialization mechanism – Panagiotis Kanavos Feb 28 '18 at 13:42
  • Use a JSON library. – llllllllll Feb 28 '18 at 13:43
  • In general, the most efficient containers would be [`std::unordered_map`](http://en.cppreference.com/w/cpp/container/unordered_map) in place of Python `dict`s, and [`std::vector`](http://en.cppreference.com/w/cpp/container/vector) in place of `list`s. The catch being that all of the types contained in these containers must be the homogeneous, which is not true in Python. – 0x5453 Feb 28 '18 at 13:43
  • 2
    https://xkcd.com/138/ (Sorry, I coudn't resist) – Arnav Borborah Feb 28 '18 at 13:43
  • Possible duplicate of [Is it possible to serialize and deserialize a class in C++?](https://stackoverflow.com/questions/234724/is-it-possible-to-serialize-and-deserialize-a-class-in-c) – Panagiotis Kanavos Feb 28 '18 at 13:45
  • As for what's "best", using what criteria? `pickle` is easy to use but not the fastest if you want concurrent updates or storing a lot of data. It's not human-readable either. You can use a serialization library or provide your own serialization operator overrides – Panagiotis Kanavos Feb 28 '18 at 13:46
  • In any case, both in Python and C++, serialization shouldn't mean using the wrong classes/objects. – Panagiotis Kanavos Feb 28 '18 at 13:48
  • A structure (aka class), or _maybe_ an array (aka vector) of offsets. with functions to access the data in a type-safe manner. In c++, the data file would have probably been in a different format to start with, so your code will end up being messy because of the backwards compatibility issue. Using json for storing the offsets is a good idea.. – Michaël Roy Feb 28 '18 at 13:48

1 Answers1

0

Have a look at unordered_map for a cpp "dictionary" equivalent. For a json parser, you could do worse than boost's json parser.

Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
stack user
  • 835
  • 1
  • 9
  • 28