【发布时间】:2024-01-14 21:33:01
【问题描述】:
编辑:我没有正确解释自己。再试一次。
我正在编写一个保存游戏编辑器。编辑器在保存游戏中加载,检查版本。版本信息保存在各种“数据”语句中,一旦检查版本,就会加载正确的偏移量。然后将偏移量分配给变量。 下面是两个版本的游戏(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!!)
在 python 中,我使用了字典,然后检查了版本(例如 CJ031223),它会将正确的偏移量复制到另一个字典(或列表)中,从而允许我访问数据。
注意:我将为球队和球员使用课程,我不关心这个。我正在寻找在 c++ 中执行此操作的最佳方法。
【问题讨论】:
-
这样的结构在 Python 中也是次优的。单个玩家的状态应该是单个类,而不是键/值对的集合。你不需要在 Python 中使用字典来序列化数据结构(这就是它的目的)。您使用 pickle 或其他一些序列化机制
-
使用 JSON 库。
-
一般来说,最高效的容器是
std::unordered_map代替 Pythondicts,std::vector代替lists。问题是这些容器中包含的所有类型都必须是同质的,这在 Python 中是不正确的。 -
xkcd.com/138(对不起,我无法抗拒)