Merged in Governor-Tarkin/swg-src (pull request #17)
[swg-src.git] / tools / updateCustomizationData.pl
blob2442cda8b8fe0381b174c5f26cf4328a81be6eb4
1 #!/usr/bin/perl
3 use strict;
5 use Perforce;
7 # ----------------------------------------------------------------------
9 sub appendNonCommentContents
11 my $sourceFileName = shift;
12 my $targetFileName = shift;
14 # Only append if the source file exists.
15 if (-f $sourceFileName)
17 # Append the non-comment, non-empty lines of $sourceFileName to $targetFileName.
18 my $sourceFileHandle;
19 open($sourceFileHandle, "< $sourceFileName") or die "failed to open file name [$sourceFileName] for reading: $!";
20 print "Appending contents of file [$sourceFileName] to file [$targetFileName].\n";
22 my $targetFileHandle;
23 open($targetFileHandle, ">> $targetFileName") or die "failed to open file name [$targetFileName] for appending: $!";
25 while (<$sourceFileHandle>)
27 chomp();
29 # Skip comment lines or blank lines.
30 next if (m/^\s*\#/) or (m/^\s*$/);
32 # Append to the target file.
33 print $targetFileHandle "$_\n";
36 close($targetFileHandle) or die "failed to close append file [$targetFileName]: $!";
37 close($sourceFileHandle) or die "failed to close source file [$sourceFileName]: $!";
41 # ----------------------------------------------------------------------
43 sub sortUnique
45 my ($inputFileName, $outputFileName) = @_;
47 open(my $inputFile, '< ' . $inputFileName) or die ("failed to open [$inputFileName]: $!");
48 my @lines = <$inputFile>;
49 close($inputFile) or die ("failed to close [$inputFileName]: $!");
51 open(my $outputFile, '> ' . $outputFileName) or die ("failed to open [$outputFileName]: $!");
53 my %dataHash = map { $_ => 1 } @lines;
54 print $outputFile sort keys %dataHash;
56 close($outputFile) or die ("failed to close [$outputFileName]: $!");
59 # ----------------------------------------------------------------------
61 die "usage: updateCustomizationData.pl [-s <skipToStep>] branchName" if @ARGV < 1;
63 # ----------------------------------------------------------------------
65 # Process args.
66 my $skipToStep = 0;
67 my $debug = 0;
69 if ($ARGV[0] eq '-d')
71 shift;
72 $debug = 1;
75 if ($ARGV[0] eq '-s')
77 shift;
78 die "-s <skipToStep> requires a step number. I don't understand [$ARGV[0]]" if !($ARGV[0] =~ m/^\d+$/);
79 $skipToStep = $ARGV[0];
80 shift;
82 elsif ($ARGV[0] =~ m/^-/)
84 die "unsupported option $ARGV[0]";
87 # Get branch.
88 my $branch = shift;
89 $branch =~ m/^(current|test|live|x1|x2|s0|s1|s2|s3|s4|s5|s6|s7|s8|s9)$/ or die "specified branch is unknown.";
90 print "Using branch: $branch\n";
92 # Checkout customization-related files.
93 my $commandOutput;
94 my $scriptName;
96 print "Step 1: Checking out customization-related files.\n";
97 $commandOutput = `p4 edit //depot/swg/$branch/dsrc/sku.0/sys.shared/compiled/game/customization/*.mif //depot/swg/$branch/data/sku.0/sys.shared/compiled/game/customization/*.iff`;
98 die "p4 edit failed on customization files: [$?] [$commandOutput]" if ($? != 0);
100 # Generate treefile lookup table.
101 my $treefileLookupDataFileName;
103 if ($skipToStep <= 2)
105 print "Step 2: Generating treefile lookup table.\n";
106 $scriptName = Perforce::findOnDiskFileName("//depot/swg/$branch/tools/buildTreeFileLookupTable.pl");
107 $commandOutput = `perl $scriptName $branch`;
108 $commandOutput =~ m/treefile lookup pathname: \[([^\]]+)\]/ or die "failed to find treefile lookup table name from output:\n$commandOutput";
109 $treefileLookupDataFileName = $1;
111 else
113 print "Looking for treefile lookup table:\n" if $debug;
114 my @filenameChoices = glob "treefile-xlat-$branch-*.dat";
115 die "Could not find treefile lookup table matching filename 'treefile-xlat-$branch-*.dat': $!" if (@filenameChoices < 1);
117 # Use the highest-numbered change list.
118 my $changelist = 0;
120 foreach my $filename (@filenameChoices)
122 print "\tchecking $filename\n" if $debug;
123 if ($filename =~ m/treefile-xlat-$branch-(\d+)/)
125 if ($1 > $changelist)
127 $changelist = $1;
128 $treefileLookupDataFileName = $filename;
133 die "Failed to find a suitable treefile lookup table, can't skip step 2" if !defined($treefileLookupDataFileName);
135 print "Using treefile lookup table: $treefileLookupDataFileName\n";
138 # Collect raw customization info.
139 my $unoptimizedCustomizationInfoFileName = "custinfo-raw.dat";
141 if ($skipToStep <= 3)
143 print "Step 3a: Collecting unoptimized customization variable and linkage info from all assets.\n";
144 $scriptName = Perforce::findOnDiskFileName("//depot/swg/$branch/tools/collectAssetCustomizationInfo.pl");
145 `perl $scriptName -b $branch -t $treefileLookupDataFileName > $unoptimizedCustomizationInfoFileName`;
146 die "Failed to collect unoptimized assets [$?]" if ($? != 0);
148 # Append contents of force_add_variable_usage.dat to the raw data.
149 print "Step 3b: Appending forced variable usage information.\n";
150 my $forceAddInfoFileName = Perforce::findOnDiskFileName("//depot/swg/$branch/dsrc/sku.0/sys.shared/compiled/game/customization/force_add_variable_usage.dat");
151 appendNonCommentContents($forceAddInfoFileName, $unoptimizedCustomizationInfoFileName);
154 # Optimize the customization info.
155 my $optimizedCustomizationInfoFileName = "custinfo-raw-optimized.dat";
156 my $optimizedUniqueCustomizationInfoFileName = "custinfo-raw-optimized-unique.dat";
157 my $artAssetReportFileName = "art-asset-customization-report.log";
159 $scriptName = Perforce::findOnDiskFileName("//depot/swg/$branch/tools/buildAssetCustomizationManagerData.pl");
161 if ($skipToStep <= 4)
163 print "Step 4a: Optimizing customization variable info.\n";
164 my $debugFlag = $debug ? '-d' : '';
165 print "Executing: [perl $scriptName $debugFlag -a $artAssetReportFileName -i $unoptimizedCustomizationInfoFileName -r -t $treefileLookupDataFileName]\n" if $debug;
166 $commandOutput = `perl $scriptName $debugFlag -a $artAssetReportFileName -i $unoptimizedCustomizationInfoFileName -r -t $treefileLookupDataFileName`;
167 if ($? != 0)
169 die "Failed to optimize customization info: [$?]\n$commandOutput" if ($? != 0);
171 else
173 print "output begin:\n$commandOutput\noutput end\n" if $debug;
176 # Remove duplicate lines from the custoimzation info.
177 print "Step 4b: Removing duplicate entries from optimized customization info.\n";
178 sortUnique($optimizedCustomizationInfoFileName, $optimizedUniqueCustomizationInfoFileName);
181 # Build asset_customization_manager.mif and customization_id_manager.mif data.
182 my $acmMifFileName = Perforce::findOnDiskFileName("//depot/swg/$branch/dsrc/sku.0/sys.shared/compiled/game/customization/asset_customization_manager.mif");
183 die "Failed to find asset_customization_manager.mif's location on disk" if !defined($acmMifFileName) || !length($acmMifFileName);
185 my $cimMifFileName = Perforce::findOnDiskFileName("//depot/swg/$branch/dsrc/sku.0/sys.shared/compiled/game/customization/customization_id_manager.mif");
186 die "Failed to find customization_id_manager.mif's location on disk" if !defined($cimMifFileName) || !length($cimMifFileName);
188 if ($skipToStep <= 5)
190 print "Step 5: Building asset customization manager and customization id manager data.\n";
191 $commandOutput = `perl $scriptName -i $optimizedUniqueCustomizationInfoFileName -o $acmMifFileName -m $cimMifFileName -t $treefileLookupDataFileName`;
192 die "Failed to build asset customization info from optimized asset info: [$?]\n$commandOutput" if ($? != 0);
195 # Miff the output.
196 my $acmIffFileName = Perforce::findOnDiskFileName("//depot/swg/$branch/data/sku.0/sys.shared/compiled/game/customization/asset_customization_manager.iff");
197 die "Failed to find asset_customization_manager.iff's location on disk" if !defined($acmIffFileName) || !length($acmIffFileName);
199 my $cimIffFileName = Perforce::findOnDiskFileName("//depot/swg/$branch/data/sku.0/sys.shared/compiled/game/customization/customization_id_manager.iff");
200 die "Failed to find customization_id_manager.iff's location on disk" if !defined($cimIffFileName) || !length($cimIffFileName);
202 if ($skipToStep <= 6)
204 print "Step 6a: Running miff on optimized asset customization manager data.\n";
205 $commandOutput = `miff -i $acmMifFileName -o $acmIffFileName 2>&1`;
206 die "Failed to miff -i $acmMifFileName -o $acmIffFileName: [$?] [$commandOutput]" if ($? != 0);
208 print "Step 6b: Running miff on customization id manager file.\n";
209 $commandOutput = `miff -i $cimMifFileName -o $cimIffFileName 2>&1`;
210 die "Failed to miff -i $cimMifFileName -o $cimIffFileName: [$?] [$commandOutput]" if ($? != 0);
213 print "Done. User is responsible for checking in changes to perforce and cleaning up temporary files.\n";