README.md edited online with Bitbucket
[swg-src.git] / tools / custView.pl
blob322359bbeda0598f1f0dea71349300bb68fe96f2
1 # ======================================================================
3 # custView.pl
4 # Copyright 2003 Sony Online Entertainment, Inc.
5 # All Rights Reserved.
7 # ======================================================================
9 # Used to output customization data assignments for objects in a
10 # SWG database. Can view customization values for a list of object
11 # ids or for all object ids in the database.
13 # Perl modules DBI and DBD::Oracle must be installed to run this tool. See www.perl.com for info on getting these modules.
14 # You must have PERL5LIB (or PERLLIB) set to your mapping for //depot/swg/current/tools/perllib.
16 use strict;
18 use Customization;
19 use DBI;
21 # ======================================================================
22 # Globals
23 # ======================================================================
25 my $modeIsView = 0;
26 my $modeIsChange = 0;
28 my $userName = "";
29 my $password = "changeme";
30 my $databaseName = "swodb";
32 my $debug = 0;
34 my $showAll = 0;
35 my $useOldFormat = 0;
36 my $useFormatVersion = 1;
38 my $defaultFilename = '../dsrc/sku.0/sys.shared/compiled/game/customization/customization_id_manager.mif';
39 my $customizationIdManagerFilename = $defaultFilename;
41 my $dbHandle;
43 my $dumpFileName;
44 my $dumpFileEntryNumber = 1;
46 # ======================================================================
48 sub printHelp
50 print "Calling syntax:\n";
51 print "\n";
52 print "show help:\n";
53 print "\t$0 -h\n";
54 print "\n";
55 print "view human-readable customization from database:\n";
56 print "\t$0 -V -u <database username> [-p <database password>] [-D <database>] [-d]\n";
57 print "\t [-m <pathToCustomizationIdManagerMifFile>] [-f format]\n";
58 print "\t [-a | [objectId [objectId...]]]\n";
59 print "\n";
60 print "change customization data in the database:\n";
61 print "\t$0 -C -u <database username> [-p <database password>] [-D <database>] [-d]\n";
62 print "\t -1 <path to 1-line format dump file> [-e <entry number within dump file>] objectId\n";
63 print "\n";
64 print "Option description:\n";
65 print "\t-V: major operating mode: view database info.\n";
66 print "\t-C: major operating mode: change database entry.\n";
67 print "\t-u: specifies the name of the user for the Oracle database.\n";
68 print "\t-p: [optional] specifies the password for the user of the Oracle database. Default: changeme.\n";
69 print "\t-D: [optional] specifies the database to attach to. Default: swodb.\n";
70 print "\t-d: [optional] turn on extensive debug-level output.\n";
71 print "\t-m: [optional] specify path to CustomizationIdManager's mif version of initialization file.\n";
72 print "\t Default: $defaultFilename\n";
73 print "\t-a: [optional] list customization variables for all objects in the database that have any customization info.\n";
74 print "\t-f: [optional] customization string format: format = 1 (for new packed format version 1), old2 (for old unpacked format version 2).\n";
75 print "\t Default: 1.\n";
76 print "\t-h: [optional] print this help info.\n";
77 print "\t-1: filename containing 1-line dump format (one entry per line) of Oracle select dump(appearance_data) data.\n";
78 print "\t-e: [optional] specifies the 1-based entry count to use if the dump file has multiple lines. Default: 1.\n";
81 # ======================================================================
83 sub collectOptions
85 my $showHelp = 0;
86 my $exitCode = 0;
88 while (defined($ARGV[0]) && ($ARGV[0] =~ m/^-(.*)$/))
90 if ($1 eq 'u')
92 # Grab username.
93 if (@ARGV < 2)
95 print "-u option missing <database username> specification.\n";
96 $exitCode = 1;
97 last;
100 $userName = $ARGV[1];
101 print "<username: $userName>\n" if $debug;
103 # Skip past arg.
104 shift @ARGV;
106 elsif ($1 eq 'p')
108 # Grab password.
109 if (@ARGV < 2)
111 print "-p option missing <password> specification.\n";
112 $exitCode = 1;
113 next;
116 $password = $ARGV[1];
117 print "<password: $password>\n" if $debug;
119 # Skip past arg.
120 shift @ARGV;
122 elsif ($1 eq 'D')
124 # Grab database name.
125 if (@ARGV < 2)
127 print "-D option missing <database> specification.\n";
128 $exitCode = 1;
129 next;
132 $databaseName = $ARGV[1];
133 print "<database: $databaseName>\n" if $debug;
135 # Skip past arg.
136 shift @ARGV;
138 elsif ($1 eq 'C')
140 $modeIsChange = 1;
141 print "major mode is change\n" if $debug;
143 elsif ($1 eq 'd')
145 $debug = 1;
146 $Customization::Debug = 1;
148 elsif ($1 eq 'a')
150 $showAll = 1;
152 elsif ($1 eq 'h')
154 $showHelp = 1;
156 elsif ($1 eq 'm')
158 # Grab commit count.
159 if (@ARGV < 2)
161 print "-m option missing <CustomizationIdManager MIF file> specification.\n";
162 $exitCode = 1;
163 next;
166 $customizationIdManagerFilename = $ARGV[1];
167 print "<customizationIdManagerFilename: $customizationIdManagerFilename\n" if $debug;
169 # Skip past arg.
170 shift @ARGV;
172 elsif ($1 eq 'f')
174 # Grab commit count.
175 if (@ARGV < 2)
177 print "-f option missing <format> specification.\n";
178 $exitCode = 1;
179 next;
182 if ($ARGV[1] =~ s/^old//i)
184 $useOldFormat = 1;
186 print "<useOldFormat: $useOldFormat>\n" if $debug;
188 $useFormatVersion = $ARGV[1];
189 print "<useFormatVersion: $useFormatVersion>\n" if $debug;
191 # Skip past arg.
192 shift @ARGV;
194 elsif ($1 eq 'V')
196 $modeIsView = 1;
197 print "major mode is viewing\n" if $debug;
199 elsif ($1 eq '1')
201 $dumpFileName = $ARGV[1];
202 shift @ARGV;
204 print "dumpFileName=[$dumpFileName]\n" if $debug;
206 elsif ($1 eq 'e')
208 $dumpFileEntryNumber = $ARGV[1];
209 shift @ARGV;
210 print "dumpFileEntryNumber=$dumpFileEntryNumber\n" if $debug;
212 else
214 warn "unknown option [$1].";
215 $exitCode = 1;
218 # Process next argument.
219 shift @ARGV;
222 # Check for missing options.
223 if (!$showHelp && (length $userName < 1))
225 print "missing -u username option.\n";
226 $exitCode = 1;
229 # Make sure show all or command line args exist.
230 if (!$showHelp && (!$showAll && (@ARGV < 1)))
232 print "must specify one or more object IDs or -a option for all objects.\n";
233 $exitCode = 1;
236 # Show help as needed.
237 if ($showHelp || ($exitCode != 0))
239 printHelp();
240 exit($exitCode);
244 sub getVariableInfo(\%$)
246 my $variableInfoRef = shift;
247 my $customizationString = shift;
249 if ($useOldFormat)
251 if ($useFormatVersion == 2)
253 getVariableInfoFromOldString(%$variableInfoRef, $customizationString);
255 else
257 die "The only old version format supported is version 2, user specified [$useFormatVersion].";
260 else
262 if ($useFormatVersion == 1)
264 getVariableInfoFromNewString(%$variableInfoRef, $customizationString);
266 else
268 die "New version format [$useFormatVersion] unsupported.";
273 # ======================================================================
275 sub printView($\%)
277 my $objectId = shift;
278 my $variableInfoRef = shift;
280 print "object id: $objectId\n";
281 dumpVariableInfo(%$variableInfoRef);
283 print "\n";
286 # ======================================================================
288 sub handleRow(\@)
290 my $rowRef = shift;
292 # Validate row entry count.
293 die "Returned row has " . @$rowRef . "entries, expecting 2." if (@$rowRef != 2);
295 # Retrieve object id and old customization data.
296 my $objectId = $$rowRef[0];
297 my $customizationString = $$rowRef[1];
298 print "<row: id=[$objectId]: string length [" . (length $customizationString) . "]>\n" if $debug;
300 my %variableInfo = ();
302 my $success = getVariableInfo(%variableInfo, $customizationString);
303 die "getVariableInfo() failed for object id [$objectId]." if !$success;
305 printView($objectId, %variableInfo);
308 # ======================================================================
310 sub doView
312 if ($showAll)
314 print "<viewing: all>\n" if $debug;
316 # Prepare the SELECT statement: grab all non-empty appearance_data and associated object ids.
317 my $statementHandle = $dbHandle->prepare("SELECT object_id, appearance_data FROM tangible_objects WHERE LENGTH(appearance_data) > 0") or die $dbHandle->errstr;
318 $statementHandle->execute() or die $statementHandle->errstr;
320 while (my @row = $statementHandle->fetchrow_array)
322 handleRow(@row);
325 else
327 print "<viewing: [@ARGV]>\n" if $debug;
329 # Prepare the SELECT statement: grab all non-empty appearance_data and associated object ids.
330 my $statementHandle = $dbHandle->prepare("SELECT object_id, appearance_data FROM tangible_objects WHERE (LENGTH(appearance_data) > 0) AND object_id = ?") or die $dbHandle->errstr;
332 for (; defined($ARGV[0]); shift @ARGV)
334 $statementHandle->execute($ARGV[0]) or die $statementHandle->errstr;
335 my @row = $statementHandle->fetchrow_array;
337 if (!@row)
339 print "object id [$ARGV[0]] has no customization data.\n";
340 next;
343 handleRow(@row);
348 # ----------------------------------------------------------------------
350 sub convertOracleDumpToString
352 my $oracleDumpString = shift;
353 my $newString = "";
355 # remove header info.
356 $oracleDumpString =~ s/^\s*(\d+)?.*:\s*//;
357 my $objectId = $1;
359 while (length($oracleDumpString) > 0)
361 if ($oracleDumpString =~ s/^(\d+)(\s*,\s*)?//)
363 $newString .= chr($1);
365 else
367 print STDERR "convertOracleDumpToString: ", length($oracleDumpString), " characters remain: [val=", ord(substr($oracleDumpString,0,1)), "].\n";
368 return $newString;
372 return $newString;
375 # ----------------------------------------------------------------------
377 sub extractAppearanceDataFromFile
379 my $dumpFile;
380 open($dumpFile, '< ' . $dumpFileName) or die "failed to open dump file [$dumpFileName]: $!";
382 my $entry = 1;
383 while (<$dumpFile>)
385 # Check if we're dealing with the proper entry.
386 if ($entry == $dumpFileEntryNumber)
388 chomp;
389 my $appearanceData = convertOracleDumpToString($_);
391 close($dumpFile) or die "Failed to close dump file: $!";
392 return $appearanceData;
395 # Increment loop.
396 ++$entry;
399 close($dumpFile) or die "Failed to close dump file: $!";
400 die "Entry [$dumpFileEntryNumber] does not exist in file [$dumpFileName] with [$entry] entries.";
403 # ----------------------------------------------------------------------
405 sub updateAppearanceData
407 # Retrieve args.
408 my $objectId = shift;
409 my $appearanceData = shift;
411 # Execute query.
412 my $rowCount = $dbHandle->do("UPDATE tangible_objects SET appearance_data=? WHERE object_id=$objectId", undef, $appearanceData) or die $dbHandle->errstr;
413 $dbHandle->commit() or die $dbHandle->errstr;
414 print "[$rowCount] rows updated.\n";
417 # ----------------------------------------------------------------------
419 sub doChange
421 # Get the line.
422 my $appearanceData = extractAppearanceDataFromFile();
423 die "Could not extract appearance data from 1-line dump file.\n" if !defined($appearanceData);
425 # Update the database.
426 die "Expecting objectId at end of line.\n" if (@ARGV != 1);
428 my $objectId = $ARGV[0];
429 updateAppearanceData($objectId, $appearanceData);
432 # ----------------------------------------------------------------------
434 sub connectDatabase
436 # Open the database connection.
437 $dbHandle = DBI->connect("dbi:Oracle:$databaseName", $userName, $password, { RaiseError => 1, AutoCommit => 0 });
438 error("failed to open database: [$DBI::errstr]") if !defined($dbHandle);
439 print "<connection: opened connection to database [$databaseName] as user [$userName] successfully.>\n" if $debug;
442 # ----------------------------------------------------------------------
444 sub disconnectDatabase
446 # Close the database connection.
447 my $returnCode = $dbHandle->disconnect or warn $dbHandle->errstr;
448 print "<disconnect: return code $returnCode>\n" if $debug;
451 # ======================================================================
452 # Program Starts Here
453 # ======================================================================
455 collectOptions();
457 connectDatabase();
459 if ($modeIsView)
461 Customization::initializeCustomization($customizationIdManagerFilename);
462 doView();
464 elsif ($modeIsChange)
466 doChange();
468 else
470 die "Major mode is neither viewing or changing.\n";
473 disconnectDatabase();
476 # ======================================================================