-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathccut.rb
208 lines (182 loc) · 4.81 KB
/
ccut.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
require 'fileutils'
require 'pathname'
require_relative 'idx'
require_relative 'rewrite'
require_relative 'term_colors'
OUTDIR = Pathname.new("Source")
FileUtils.mkdir_p OUTDIR
TARGETS = [
[0x410990, "appfat"],
[0x4599E0, "automap"],
[0x435DE0, "capture"],
[0x47AFD0, "codec"],
[0x414980, "control"],
[0x485990, "cursor"],
[0x466F10, "dead"],
[0x44E3D0, "debug"],
[0x490C20, "diablo"],
[0x49AE30, "drlg_l1"],
[0x47c790, "drlg_l2"],
[0x43a370, "drlg_l3"],
[0x4674C0, "effects"],
[0x47AAE0, "encrypt"],
[0x48E700, "engine"],
[0x45C280, "error"],
[0x466750, "gamemenu"],
[0x4119D0, "gendung"],
[0x479D70, "gmenu"],
[0x44E5C0, "help"],
[0x42D180, "init"],
[0x41E310, "interfac"],
[0x44EF20, "inv"],
[0x420580, "items"],
[0x437B50, "lighting"],
[0x43ADB0, "loadsave"],
[0x43A170, "mainmenu"],
[0x4129D0, "minitext"],
[0x43AE00, "missiles"],
[0x401000, "monster"],
[0x482290, "movie"],
[0x41f1a8, "mpqapi"],
[0x494540, "msg"],
[0x41C0B0, "multi"],
[0x41B390, "nthread"],
[0x45C890, "objects"],
[0x468420, "pack"],
[0x48DD10, "palette"],
[0x413020, "path"],
[0x436940, "pfile"],
[0x468CD0, "player"],
[0x41BBF0, "plrmsg"],
[0x493F10, "portals"],
[0x478720, "quests"],
[0x4880B0, "scrollrt"],
[0x4364A0, "setmaps"],
[0x4846A0, "sha"],
[0x413B70, "sound"],
[0x45B6D0, "spells"],
[0x42E330, "stores"],
[0x484BF0, "sync"],
[0x482730, "themes"],
[0x44A530, "town"],
[0x4579F0, "towners"],
[0x468130, "track"],
[0x47B670, "trigs"],
[0x43AB00, "wave"],
[0x4A048C, "render"],
[0x4A6B50, nil],
[0xFFFFFF, nil]
].sort_by { |d| d[0] }
cur_target = -1
addr = nil
was_addr = false
$out_file = nil
def set_out_file(fn)
$out_file.close if $out_file
$stderr.puts "--- Output to #{fn}".cyan
$out_file = fn && (OUTDIR + "#{fn}").open("w")
end
init_target = Proc.new {
target_name = TARGETS[cur_target][1]
set_out_file target_name && "#{target_name}.cpp"
if target_name
$out_file.puts '#include "all.h"'
$out_file.puts ''
end
}
source_file = File.open(ARGV[0])
header_file = File.open(ARGV[1])
# Read source
stage = nil
source_file.each_line do |l|
no_comments = l.gsub(/\s*\/\/.*$/, "")
if !no_comments.strip.empty?
if was_addr
was_addr = false
$stderr.puts "#{addr.to_s(16)}: #{l}"
end
end
if l =~ /^\/\/----- \(([A-F0-9]+)\)/
addr = $1.to_i(16)
was_addr = true
stage = :code
if addr >= TARGETS[cur_target + 1][0]
cur_target += 1
init_target.call
end
elsif l =~ /^\/\/ ([A-F0-9]+): using guessed type/
next
else
sl = l.strip
if sl == "// Function declarations"
set_out_file "functions.h"
$out_file.puts '#include "types.h"'
stage = :functions
elsif sl == "// Data declarations"
set_out_file "data.h"
$out_file.puts '#include "types.h"'
stage = :data
end
end
if $out_file
l = rewrite_generic l
if stage == :code
l = rewrite_line_idx l
elsif stage == :data
if l =~ /^[ {}}]/
# Outputting initializers
next
end
if no_comments =~ /^(.*?)\s*(?:=|;)/
l = "extern #{$1};"
$stderr.puts l
end
end
$out_file.puts l
end
end
# Read header
type_no = nil
types = []
names = {}
VALID_NAMES = /Struct\d*$|Data$|NODE$|Monster|^D[A-Z]|^T[A-Z]|^Inv|^_plr|^_ui|^Coord|^SHA1/
INVALID_NAMES = /^MACRO_/
header_file.each_line do |l|
if l =~ /^\/\* (\d+) \*\//
type_no = $1.to_i
elsif l =~ /^#pragma/
next
elsif type_no
l = l.gsub(/\b(__unaligned )?__declspec\(align\((\d+)\)\)\s*/, "")
unaligned, align = $1, $2
if l =~ /^(struct|enum)\s+([\w:$]+)\s*$/
type, name = $1, $2
if name =~ INVALID_NAMES || (type == "struct" && name !~ VALID_NAMES)
$stderr.puts "Skip #{name}".brown
type_no = nil
next
else
$stderr.puts "#{type} #{name}"
end
names[name] = [type, type_no]
elsif l =~ /^typedef|union/
type_no = nil
next
end
# l = l.gsub(/\b_(BYTE|WORD|DWORD)\b/, "\\1")
(types[type_no] ||= []) << l
end
end
set_out_file "types.h"
$out_file.puts '#include "defs.h"'
$out_file.puts "// Forward declarations"
names.each do |name, data|
$out_file.puts "#{data[0]} #{name};"
end
$out_file.puts "// Types"
types.each_with_index do |lines, num|
next if !lines
lines.each do |l|
$out_file.puts l
end
end