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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
###############################################################
#
# generateDEF.pl
#
# Parses a .map file and generates a .def file
# for exporting functions from a dll.
#
# Note: Map Exports must be enabled in the project properties
#
###############################################################
my @baselist;
my @output;
my $baseLen = 63;
######################################
# Adds tabs for better formatting
sub Add_Tabs
{
my $name = shift;
my $num = int( ($baseLen - length( $name )) / 8 );
for( $i = 0; $i < $num; $i++ )
{
push( @output, "\t" );
}
}
######################################
# Open, validate, and read a file (not implemented yet)
sub Read_File
{
my $name = shift;
my @file = shift;
# read in the file
if ( open(INFILE, "$name" ) )
{
@file = <INFILE>;
close( INFILE );
}
else
{
print( "Error opening file $name\n" );
exit 1;
}
}
#####################################
# Start of script
print( "Valve Software - make360def.pl\n" );
print( "Copyright (c) 1996-2006, Valve LLC, All rights reserved.\n" );
my $filename = $ARGV[0];
my $numArgs = 1;
my @lines = ();
my @deflines = ();
if ( $ARGV[0] =~ /-check$/i )
{
$numArgs = 3;
$check = 1;
$filename = $ARGV[1];
$defname = $ARGV[2];
}
elsif ( $ARGV[0] =~ /-checkauto/i )
{
$defname = $ARGV[1];
$check = 2;
}
if ( @ARGV < $numArgs )
{
print( "ERROR: Missing filename(s)\n" );
exit 1;
}
if ( $check == 1 )
{
# swap filenames if necessary
if ( $filename =~ /.def/ )
{
my $temp = $filename;
$filename = $defname;
$defname = $temp;
}
# validate extensions
unless ( $filename =~ /.map/ && $defname =~ /.def/ )
{
print( "ERROR: Invalid file extensions. -check requires a .map file and a .def file.\n" );
exit 1;
}
# read in the def file
if ( open(INFILE, "$defname" ) )
{
@deflines = <INFILE>;
close( INFILE );
}
else
{
print( "ERROR: Couldn't open file $defname.\n" );
exit 1;
}
}
elsif ( $check == 2 )
{
# read in the def file
if ( open(INFILE, "$defname" ) )
{
@deflines = <INFILE>;
close( INFILE );
}
else
{
print( "ERROR: Couldn't open file $defname.\n" );
exit 1;
}
# validate that the first export is CreateInterface*
# validate that the export ordinals are sequential and ordered
my $line;
my $start = false;
my $idx = 1;
for ( @deflines )
{
$line = $_;
if ( $line =~ /@(\d+)[ |\n]/ )
{
if ( $1 == 1 )
{
unless ( $line =~ /CreateInterface/ )
{
# first export must be CreateInterface*
$line =~ /\s+([\S]*)/;
print( "**************************************************\n" );
print( " ERROR: First export must be CreateInterface*. \n" );
print( " Export \"", $1, "\" found instead! \n" );
print( " This is a FATAL ERROR with the def file. \n" );
print( " Please contact an Xbox 360 engineer immediately.\n" );
print( "**************************************************\n" );
exit 1;
}
}
if ( $1 != $idx )
{
# exports are out of order
print( "**************************************************\n" );
print( " ERROR: Def file exports are not sequential \n" );
print( " This may cause unexpected behavior at runtime. \n" );
print( " Please contact an Xbox 360 engineer immediately.\n" );
print( "**************************************************\n" );
exit 1;
}
++$idx;
}
}
exit 0;
}
# Get the base name
$filename =~ /(\w+(\.360)?).map/;
my $basename = $1;
# Read in the source file
if ( open(INFILE, "$filename" ) )
{
@lines = <INFILE>;
close( INFILE );
}
else
{
print( "ERROR: Couldn't open file $filename.\n" );
exit 1;
}
# Delete the lines up to the exports section
my $len = 0;
my $exportsFound = 0;
for( @lines )
{
$len++;
if( /^ Exports$/ )
{
splice( @lines, 0, $len+3 );
$exportsFound = 1;
}
}
if ( $exportsFound == 0 )
{
print( "ERROR: No Exports section found in $filename. " );
print( "Relink the project with 'Map Exports' enabled.\n" );
exit 1;
}
if ( $check == 1 )
{
# Check for exports in the map that aren't in the def
print( "make360def: Comparing $filename and $defname\n" );
# strip the first 2 lines from the def
splice( @deflines, 0, 2 );
my $defEntryCt = $#deflines + 1;
my $defMatches = 0;
# for each line in the map
for( @lines )
{
my $found = 0;
# Pull the export name from the map line
my $mapline;
if ( /(\d+)\s+(\S+)/ )
{
$mapline = $2;
}
else
{
# ignore this line
next;
}
# for each line in the def
for( @deflines )
{
/(\S+)/;
if ( $1 =~ /^\Q$mapline\E$/ )
{
$found = 1;
$defMatches++;
last;
}
}
if ( $found == 0 )
{
print( "ERROR: New export found in $filename, " );
print( "so the map file and def file are out of sync. " );
print( "You must relink the project to generate a new def file.\n" );
exit 1;
}
}
# Make sure all the def lines were matched
if ( $defMatches != $defEntryCt )
{
print( "ERROR: An export was removed from $filename, " );
print( "so the map file and def file are out of sync. " );
print( "You must relink the project to generate a new def file.\n" );
exit 1;
}
print( "make360def: Comparison complete, files match.\n" );
exit 0;
}
# start the def file
print( "make360def: Generating $basename.def\n" );
push( @output, "LIBRARY\t$basename.dll\n" );
push( @output, "EXPORTS\n" );
# process each line in the export section
my $interfacePrefix = "0000000000";
for( @lines )
{
if ( /(\d+)\s+(\S+)/ )
{
my $func = $2;
if ( $func =~ /CreateInterface/ )
{
# Force createInterface to sort first
$func = join( '', $interfacePrefix, $func );
}
push( @baselist, $func );
}
}
# sort the list
my @sortedlist = sort {uc($a) cmp uc($b)} @baselist;
#my @sortedlist = @baselist;
my $ordinal = 1;
for( @sortedlist )
{
my $func = $_;
if ( /$interfacePrefix(.*)/ )
{
# Strip the added characters
$func = $1;
}
push( @output, "\t$func\t" );
Add_Tabs( $func );
push( @output, "\@$ordinal\n" );
$ordinal++;
}
# write the def file
print( "make360def: Saving $basename.def.\n" );
open ( OUTFILE, ">$basename.def" );
print OUTFILE @output;
close ( OUTFILE );
print( "make360def: Finished.\n" );
exit 0;
|