-
Notifications
You must be signed in to change notification settings - Fork 14
/
dump_df_globals.rb
278 lines (258 loc) · 9.65 KB
/
dump_df_globals.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
require 'metasm'
# metasm script to dump the global table added by Toady for 0.44.1
# the table begins by dd 0x12345678 0x87654321 (32-bit) or dd 0x12345678 0x12345678 0x87654321 0x87654321 (64-bit)
# then a succession of [<ptr to string with global name> <ptr to global variable>]
dump_fmt = 'xml'
dump_fmt = 'idc' if ARGV.delete '--idc'
dump_size = ARGV.delete '--size'
Dfhack_names = {
"point" => "selection_rect",
"menuposition" => "ui_menu_width",
"itemmade" => "created_item_type",
"itemmade_subtype" => "created_item_subtype",
"itemmade_subcat1" => "created_item_mattype",
"itemmade_subcat2" => "created_item_matindex",
"itemmade_number" => "created_item_count",
"mainview" => "map_renderer",
"title2" => "title_spaced",
"event_flow" => "flows",
"plot_event" => "timed_events",
"buildjob_type" => "ui_building_assign_type",
"buildjob_selected" => "ui_building_assign_is_marked",
"buildjob_unit" => "ui_building_assign_units",
"buildjob_item" => "ui_building_assign_items",
"looklist" => "ui_look_list",
"year" => "cur_year",
"season_count" => "cur_year_tick",
"precise_phase" => "cur_year_tick_advmode",
"season_timer" => "cur_season_tick",
"season" => "cur_season",
"cur_weather" => "current_weather",
"assignbuildingjobs" => "process_jobs",
"assigndesjobs" => "process_dig",
"paused" => "pause_state",
"modeunit" => "ui_selected_unit",
"modeview" => "ui_unit_view_mode",
"modepage" => "ui_look_cursor",
"modeitem" => "ui_building_item_cursor",
"addingtask" => "ui_workshop_in_add",
"modejob" => "ui_workshop_job_cursor",
"buildjob_assignroom" => "ui_building_in_assign",
"buildjob_sizeroom" => "ui_building_in_resize",
"addingtask_sub" => "ui_lever_target_type",
"buildjob_sizerad" => "ui_building_resize_radius",
"scrollx" => "window_x",
"scrolly" => "window_y",
"scrollz" => "window_z",
"DEBUG_CONTINUOUS" => "debug_nopause",
"DEBUG_NOMOOD" => "debug_nomoods",
"DEBUG_SAFEDWARVES" => "debug_combat",
"DEBUG_NOANIMALS" => "debug_wildlife",
"DEBUG_NOTHIRST" => "debug_nodrink",
"DEBUG_NOHUNGER" => "debug_noeat",
"DEBUG_NOSLEEP" => "debug_nosleep",
"DEBUG_VISIBLEAMBUSHERS" => "debug_showambush",
"DEBUG_QUICKMODE_MINING" => "debug_fastmining",
"DEBUG_NEVERBERSERK" => "debug_noberserk",
"DEBUG_MEGAFAST" => "debug_turbospeed",
"gamemode_cansave" => "save_on_exit",
"standingorder_butcher" => "standing_orders_auto_butcher",
"standingorder_collect_web" => "standing_orders_auto_collect_webs",
"standingorder_fishery" => "standing_orders_auto_fishery",
"standingorder_kiln" => "standing_orders_auto_kiln",
"standingorder_kitchen" => "standing_orders_auto_kitchen",
"standingorder_loom" => "standing_orders_auto_loom",
"standingorder_other" => "standing_orders_auto_other",
"standingorder_slaughter" => "standing_orders_auto_slaughter",
"standingorder_smelter" => "standing_orders_auto_smelter",
"standingorder_tan" => "standing_orders_auto_tan",
"standingorder_gatherrefuse_chasm_bones" => "standing_orders_dump_bones",
"standingorder_gatherrefuse_chasm_corpses" => "standing_orders_dump_corpses",
"standingorder_gatherrefuse_chasm_strand_tissue" => "standing_orders_dump_hair",
"standingorder_gatherrefuse_chasm_othernonmetal" => "standing_orders_dump_other",
"standingorder_gatherrefuse_chasm_shell" => "standing_orders_dump_shells",
"standingorder_gatherrefuse_chasm_skins" => "standing_orders_dump_skins",
"standingorder_gatherrefuse_chasm_skulls" => "standing_orders_dump_skulls",
"standingorder_allharvest" => "standing_orders_farmer_harvest",
"standingorder_autoforbid_other_items" => "standing_orders_forbid_other_dead_items",
"standingorder_autoforbid_other_corpse" => "standing_orders_forbid_other_nohunt",
"standingorder_autoforbid_your_corpse" => "standing_orders_forbid_own_dead",
"standingorder_autoforbid_your_items" => "standing_orders_forbid_own_dead_items",
"standingorder_autoforbid_projectile" => "standing_orders_forbid_used_ammo",
"standingorder_gatheranimals" => "standing_orders_gather_animals",
"standingorder_gatherbodies" => "standing_orders_gather_bodies",
"standingorder_gatherfood" => "standing_orders_gather_food",
"standingorder_gatherfurniture" => "standing_orders_gather_furniture",
"standingorder_gatherstone" => "standing_orders_gather_minerals",
"standingorder_gatherrefuse" => "standing_orders_gather_refuse",
"standingorder_gatherrefuse_outside" => "standing_orders_gather_refuse_outside",
"standingorder_gatherrefuse_outside_vermin" => "standing_orders_gather_vermin_remains",
"standingorder_gatherwood" => "standing_orders_gather_wood",
"standingorder_mixfoods" => "standing_orders_mix_food",
"standingorder_dyed_clothes" => "standing_orders_use_dyed_cloth",
"standingorder_zone_drinking" => "standing_orders_zoneonly_drink",
"standingorder_zone_fishing" => "standing_orders_zoneonly_fish",
"option_exceptions" => "standing_orders_job_cancel_announce",
"next_art_imagechunk_global_id" => "art_image_chunk_next_id",
"next_civ_global_id" => "entity_next_id",
"next_histeventcol_global_id" => "hist_event_collection_next_id",
"next_histevent_global_id" => "hist_event_next_id",
"next_histfig_global_id" => "hist_figure_next_id",
"next_nem_global_id" => "nemesis_next_id",
"next_unitchunk_global_id" => "unit_chunk_next_id",
}
Dfhack_noname = {
'linelim' => true,
'line' => true,
'linechar' => true,
'modeseason' => true,
'selectingtaskobject' => true,
'buildjob_mastering' => true,
'buildjob_give_to' => true,
'buildjob_popback' => true,
'buildjob_building' => true,
'buildjob_flowx' => true,
'buildjob_flowy' => true,
'buildjob_flowz' => true,
'buildjob_item1' => true,
'squadcount' => true,
'modestation' => true,
'unitprintstack' => true,
'unitprintstack_cur' => true,
'unitprintstack_start' => true,
'unitprintstack_clear' => true,
'olookx' => true,
'olooky' => true,
'olookz' => true,
'oscrollx' => true,
'oscrolly' => true,
'oscrollz' => true,
'page' => true,
'dunginv_relitem' => true,
'dunginv' => true,
'dunginv_inv' => true,
'dunginv_depth' => true,
'dunginv_flag' => true,
'dunginv_index' => true,
'throwitem' => true,
'dung_target' => true,
'dung_targlist' => true,
'dung_attackmode' => true,
'dung_dodgemode' => true,
'dung_chargedefendmode' => true,
'dung_swimmode' => true,
'dung_buildinginteract' => true,
'soul_next_id' => true,
'task_next_id' => true,
'manucomp' => true,
'manucomp2' => true,
'filecomp_buffer' => true,
'filecomp_buffer2' => true,
'filecomp_buffer_aux' => true,
'filecomp_buffer2_aux' => true,
'DEBUG_SHOW_RIVER' => true,
'DEBUG_FASTCAVEIN' => true,
'DEBUG_ALWAYSHAPPY' => true,
'DEBUG_DISABLEHUMANCARAVAN' => true,
'DEBUG_GAMELOG' => true,
'mt_index' => true,
'mt_cur_buffer' => true,
'mt_virtual_buffer' => true,
'mt_buffer' => true,
'mt_virtual_seed_type' => true,
'game.external_flag' => true,
}
def dfhack_names(n)
if dn = Dfhack_names[n]
return dn
elsif Dfhack_noname[n]
else
case n
when /^index[12]_\d+$/
# discard
when /^next_(.*)_global_id/
$1 + '_next_id'
#when /^standingorder/
else
n
end
end
end
ENV['METASM_NODECODE_RELOCS'] = '1'
dump_raw = true if ARGV.delete '--raw'
binpath = ARGV.shift || 'Dwarf Fortress.exe'
dasm = Metasm::AutoExe.decode_file(binpath).disassembler
if dasm.cpu.size == 64
bits = 64
else
bits = 32
end
MAGIC1 = "\x78\x56\x34\x12"
MAGIC2 = "\x21\x43\x65\x87"
MAGIC1.force_encoding('BINARY') rescue nil
MAGIC2.force_encoding('BINARY') rescue nil
table_start = dasm.pattern_scan(MAGIC1).find { |off|
dasm.read_raw_data(off, 8) == MAGIC1 + MAGIC2 or
dasm.read_raw_data(off, 16) == MAGIC1 + MAGIC1 + MAGIC2 + MAGIC2
}
if not table_start
abort "Cannot find magic bytes"
end
$stderr.puts "Global table starts at #{Metasm::Expression[table_start]}"
extended = false
if bits == 64
MAGIC3 = "\xef\xcd\xab\x89"
MAGIC3.force_encoding('BINARY') rescue nil
extended = (dasm.read_raw_data(table_start + 16, 8) == MAGIC3 + MAGIC3)
$stderr.puts "Extended global table detected" if extended
end
elems = extended ? 3 : 2
off = table_start + elems*bits/8
global = {}
if extended
global_size = {}
end
global[off] = 'global_table'
while true
ptr_str = dasm.decode_dword(off)
off += bits/8
ptr_var = dasm.decode_dword(off)
off += bits/8
if extended
size = dasm.decode_dword(off)
off += bits/8
end
break if ptr_str == 0
name = dasm.decode_strz(ptr_str)
global[ptr_var] = name
if extended
global_size[ptr_var] = size
$stderr.puts "#{name} #{size}\n"
end
end
ptr_prev = 0
out = []
global.sort.reverse.map do |ptr_var, name|
name = dfhack_names(name) if not dump_raw
if not name
ptr_prev = ptr_var
next;
end
case dump_fmt
when 'xml'
if not dump_size
out << "<global-address name='#{name}' value='0x#{'%08x' % ptr_var}'/>"
else
if extended
size = global_size[ptr_var] || 0
else
size = ptr_prev > 0 ? (ptr_prev-ptr_var) : 0
end
out << "<global-address name='#{name}' value='0x#{'%08x' % ptr_var}' size='0x#{'%08x' % size}'/>"
ptr_prev = ptr_var
end
when 'idc'
out << ('MakeName(0x%08X, "%s");' % [ptr_var, '_' + name.gsub(/[^\w]/, '_')])
end
end
puts out