Merged in Governor-Tarkin/swg-src (pull request #17)
[swg-src.git] / tools / makeAsynchronousLoadData.pl
blob6c4ec72a648cb8910e8f362d06ee0f0f752fd121
1 #!/usr/bin/perl
3 use strict;
4 use warnings;
6 die "usage: perl makeAsynchronousLoadData sourceFile.log outputFile.mif oldFileList.txt\n" if (@ARGV != 3);
8 # global variables
9 my @children = ();
10 my @files = ();
11 my $counter = 0;
12 my %files;
13 my %fileExtensionIndex;
14 my %extensionOffset;
15 my %extensionIndex;
16 my %children;
18 # command line arguments
19 my $inFile = shift;
20 my $outFile = shift;
21 my $fileList = shift;
23 sub numerically
25 return $a <=> $b;
28 sub extensionOffset_numerically
30 return $extensionOffset{$a} <=> $extensionOffset{$b};
33 sub next_parent
35 if (@children)
37 my $count = @children;
38 my $parent = shift(@children);
39 $children{$parent} = join("\n int32 ", "", $count, $parent, @children) . "\n";
40 @children = ();
44 # process the old file table to minimize differential patching
45 open(FILES, $fileList) || die "could not open file $fileList for reading\n";
46 while (<FILES>)
48 chomp();
49 $files{$_} = $counter;
50 $counter += 2 + length($_) + 1;
51 push(@files, $_);
53 # now check the extension
54 my $extension = $_;
55 $extension =~ s/^.*\.//;
56 my $extensionIndex = $extensionIndex{$extension};
57 if (!defined($extensionIndex{$extension}))
59 $extensionIndex{$extension} = scalar keys %extensionOffset;
60 $extensionOffset{$extension} = $counter - length($extension) - 1;
61 $extensionIndex = $extensionIndex{$extension};
63 $fileExtensionIndex{$_} = $extensionIndex;
65 close(FILES);
67 open(FILES, $inFile) || die "could not open file $inFile for reading\n";
69 my $opening = 0;
71 while (<FILES>)
73 # 20030711155304:Viewer:reportLog:TF::open(M) d:/work/swg/test/data/sku.0/sys.client/built/game/appearance/path_arrow.msh @ d:/work/swg/test/data/sku.0/sys.client/built/game/appearance/path_arrow.msh, [size=2109]
74 s/\d+\:Viewer\:reportLog\://;
76 # check if this is an opening line, which separates primary assets
77 if (/^opening/)
79 next_parent() if ($opening);
80 $opening = 1;
82 next if (!$opening);
83 next if (!/TF::open/);
84 chomp;
86 # clean up the file name
87 s/^.*TF::open\([A-Z]\) //;
88 s/ @.*//;
89 s#\\#/#g;
90 s#//#/#g;
91 tr/A-Z/a-z/;
92 s#^.*/appearance/#appearance/#;
94 # check for it in the file list
95 my $index = $files{$_};
96 if (!defined($index))
98 $files{$_} = $counter;
99 $index = $counter;
100 $counter += 2 + length($_) + 1;
101 push(@files, $_);
103 push(@children, $index) if (scalar(grep(/^$index$/, @children)) == 0);
105 # now if the extension is known
106 my $extension = $_;
107 $extension =~ s/^.*\.//;
108 my $extensionIndex = $extensionIndex{$extension};
109 if (!defined($extensionIndex))
111 $extensionIndex{$extension} = scalar keys %extensionOffset;
112 $extensionOffset{$extension} = $counter - length($extension) - 1;
113 $extensionIndex = $extensionIndex{$extension};
116 # remember the extension table index for this file
117 $fileExtensionIndex{$_} = $extensionIndex;
120 # finish the last primary asset
121 next_parent();
123 close(FILES);
125 # write out the new file table to minimize the diff the next time the async data is updated
126 open(FILES, ">" . $fileList) || die "could not open file $fileList for writing\n";
127 foreach (@files)
129 print FILES $_, "\n";
131 close(FILES);
133 # write out the mif data
134 open(OUTPUT, ">$outFile");
135 select(OUTPUT);
137 print "form \"ASYN\"\n";
138 print "{\n";
139 print " form \"0001\"\n";
140 print " {\n";
141 print " chunk \"NAME\"\n";
142 print " {\n";
144 foreach (@files)
146 print " int8 0\n";
147 die "no extension for $_\n" if (!defined($fileExtensionIndex{$_}));
148 print " int8 $fileExtensionIndex{$_}\n";
149 print " cstring \"$_\"\n";
152 print " }\n";
155 print " chunk \"EXTN\"\n";
156 print " {\n";
157 foreach (sort extensionOffset_numerically keys %extensionOffset)
159 print " int32 $extensionOffset{$_} // $_\n";
161 print " }\n";
163 print " chunk \"LOAD\"\n";
164 print " {\n";
166 foreach (sort numerically keys %children)
168 print $children{$_};
171 print " }\n";
172 print " }\n";
173 print "}\n";
175 select;
176 close(OUTPUT);