Advertisement
Guest User

item_db.pl

a guest
Jun 16th, 2013
870
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6.71 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use strict;
  3. use Getopt::Long;
  4.  
  5. my $sFilein = "";
  6. my $sFileout = "";
  7. my $sTarget = "";
  8. my $sHelp = 0;
  9. my $stable = "";
  10.  
  11. my $db;
  12. my $nb_columns;
  13. my @str_col = ();
  14. my $line_format;
  15. my $create_table;
  16. my @defaults = ();
  17.  
  18. Main();
  19.  
  20. sub GetArgs {
  21.     GetOptions(
  22.     'i=s' => \$sFilein, #output file
  23.     'o=s' => \$sFileout, #input file
  24.     't=s'   => \$sTarget, # re/pre-re
  25.     'table=s' => \$stable,
  26.     'help!' => \$sHelp,
  27.     ) or $sHelp=1; #Display help if invalid options are supplied.
  28.     my $sValidTarget = "Re|Pre";
  29.  
  30.     if( $sHelp ) {
  31.         print "ERROR: Incorect option specified. Available options:\n"
  32.             ."\t --o=filename => output filename \n"
  33.             ."\t --i=filename => intput filename \n"
  34.             ."\t --table=tablename => tablename to create \n"
  35.             ."\t --t=type => specify target type ([$sValidTarget]) \n";
  36.         exit;
  37.     }
  38.     unless($sFilein or $sFileout){
  39.         print "ERROR: Filename_in and Filename_out are required to continue.\n";
  40.         exit;
  41.     }
  42.     unless($sTarget =~ /$sValidTarget/i){
  43.         print "ERROR: Incorect target specified. Available targets:\n"
  44.             ."\t --target => target (specify which setup to run [$sValidTarget])\n";
  45.         exit;
  46.     }
  47. }
  48.  
  49. sub Main {
  50.     GetArgs();
  51.     BuildDataForType($sTarget);
  52.     ConvertFile($sFilein,$sFileout);
  53.     print "Conversion ended.\n";
  54. }
  55.  
  56. sub ConvertFile { my($sFilein,$sFileout)=@_;
  57.     my $sFHout;
  58.     print "Starting ConvertFile with: \n\t filein=$sFilein \n\t fileout=$sFileout \n";
  59.     open FHIN,"$sFilein" or die "ERROR: Can't read or locate $sFilein.\n";
  60.     open $sFHout,">$sFileout" or die "ERROR: Can't write $sFileout.\n";
  61.    
  62.     printf $sFHout ("%s\n",$create_table);
  63.     while(my $ligne=<FHIN>)
  64.     {
  65.         if ($ligne =~ /[^\r\n]+/)
  66.         {
  67.             $ligne = $&;
  68.             if ($ligne =~ /^\/\//)
  69.             {
  70.                 printf $sFHout ("#");
  71.                 $ligne = substr($ligne, 2);
  72.             }
  73.             my @champ = ();
  74.             if ($ligne =~ $line_format) {
  75.                 @champ = ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22);
  76.             }
  77.             if ($#champ != $nb_columns - 1)
  78.             {
  79.                 # Can't parse, it's a real comment
  80.                 printf $sFHout ("%s\n", $ligne);
  81.             } else {
  82.                 printf $sFHout ("REPLACE INTO `%s` VALUES (", $db);
  83.                 for (my $i=0; $i<$#champ; $i++)
  84.                 {
  85.                     printField($sFHout,$champ[$i],",",$i);
  86.                 }
  87.                 printField($sFHout,$champ[$#champ],");\n",$#champ);
  88.             }
  89.         }
  90.     }
  91.     print $sFHout "\n";
  92. }
  93.  
  94. sub printField { my ($sFHout,$str, $suffix, $idCol) = @_;
  95.     # Remove first { and last }
  96.     if ($str =~ /{.*}/)
  97.     {
  98.         $str = substr($&,1,-1);
  99.     }
  100.     # If nothing, put NULL
  101.     if ($str eq "") {
  102.         my $sDef;
  103.         if(scalar(@defaults)) { $sDef = $defaults[$idCol]; } #Use default in array.
  104.         else { $sDef = "NULL" unless($sDef); } #Let SQL handle the default.
  105.         print $sFHout "$sDef$suffix";
  106.     } else {
  107.         my $flag = 0;
  108.         # Search if it's a string column?
  109.         foreach my $col (@str_col)
  110.         {
  111.             if ($col == $idCol)
  112.             {
  113.                 $flag = 1;
  114.                 last;
  115.             }
  116.         }
  117.         if ($flag == 1)
  118.         {
  119.             # String column, so escape , remove trailing and add ''
  120.             my $string = escape($str);
  121.             $string =~ s/\s+$//; #Remove trailing spaces.
  122.             $string =~ s/^\s+//; #Remove leading spaces.
  123.             printf $sFHout ("'%s'%s", $string, $suffix);
  124.         } else {
  125.             # Not a string column.
  126.             printf $sFHout ("%s%s", $str,$suffix);
  127.         }
  128.     }
  129. }
  130.  
  131. sub escape { my ($str) = @_;
  132.     my @str_splitted = split("'", $str);
  133.     my $result = "";
  134.     for (my $i=0; $i<=$#str_splitted; $i++)
  135.     {
  136.         if ($i == 0) {
  137.             $result = @str_splitted[0];
  138.         } else {
  139.             $result = $result."\\'".@str_splitted[$i];
  140.         }
  141.     }
  142.     return $result
  143. }
  144.  
  145. sub BuildDataForType{ my($sType) = @_;
  146.     print "Starting BuildDataForType with: \n\t type=$sTarget \n";
  147.    
  148.     if($sType =~ /Pre/i){
  149.         $db = $stable;
  150.         $db = "item_db" unless($db);
  151.         $nb_columns = 22;
  152.         @str_col = (1,2,7,19,20,21);
  153.         $line_format = "([^\,]*),"x($nb_columns-3)."(\{.*\}),"x(2)."(\{.*\})"; #Last 3 columns are scripts.
  154.         $create_table = "
  155. #
  156. # Table structure for table `$db`
  157. #
  158. DROP TABLE IF EXISTS `$db`;
  159. CREATE TABLE `$db` (
  160.  `id` smallint(5) unsigned NOT NULL default '0',
  161.  `name_english` varchar(50) NOT NULL default '',
  162.  `name_japanese` varchar(50) NOT NULL default '',
  163.  `type` tinyint(2) unsigned NOT NULL default '0',
  164.  `price_buy` mediumint(10) unsigned default NULL,
  165.  `price_sell` mediumint(10) unsigned default NULL,
  166.  `weight` smallint(5) unsigned NOT NULL default '0',
  167.  `attack` smallint(3) unsigned default NULL,
  168.  `defence` tinyint(3) unsigned default NULL,
  169.  `range` tinyint(2) unsigned default NULL,
  170.  `slots` tinyint(2) unsigned default NULL,
  171.  `equip_jobs` int(12) unsigned default NULL,
  172.  `equip_upper` tinyint(8) unsigned default NULL,
  173.  `equip_genders` tinyint(2) unsigned default NULL,
  174.  `equip_locations` smallint(4) unsigned default NULL,
  175.  `weapon_level` tinyint(2) unsigned default NULL,
  176.  `equip_level` tinyint(3) unsigned default NULL,
  177.  `refineable` tinyint(1) unsigned default NULL,
  178.  `view` smallint(3) unsigned default NULL,
  179.  `script` text,
  180.  `equip_script` text,
  181.  `unequip_script` text,
  182.  PRIMARY KEY  (`id`)
  183. ) ENGINE=MyISAM;
  184. ";
  185.     #NOTE: These do not match the table struct defaults.
  186.     @defaults = ('0','\'\'','\'\'','0','NULL','NULL','0','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL');
  187.     }
  188.     elsif($sType =~ /Re/i){
  189.         $db = $stable;
  190.         $db = "item_db_re" unless($db);
  191.         $nb_columns = 22;
  192.         @str_col = (1,2,7,16,19,20,21);
  193.         $line_format = "([^\,]*),"x($nb_columns-3)."(\{.*\}),"x(2)."(\{.*\})"; #Last 3 columns are scripts.
  194.         $create_table = "
  195. #
  196. # Table structure for table `$db`
  197. #
  198.  
  199. DROP TABLE IF EXISTS `$db`;
  200. CREATE TABLE `$db` (
  201. `id` smallint(5) unsigned NOT NULL default '0',
  202. `name_english` varchar(50) NOT NULL default '',
  203. `name_japanese` varchar(50) NOT NULL default '',
  204. `type` tinyint(2) unsigned NOT NULL default '0',
  205. `price_buy` mediumint(10) unsigned default NULL,
  206. `price_sell` mediumint(10) unsigned default NULL,
  207. `weight` smallint(5) unsigned NOT NULL default '0',
  208. `atk:matk` varchar(11) default '',
  209. `defence` smallint(5) NULL default NULL,
  210. `range` tinyint(2) unsigned default NULL,
  211. `slots` tinyint(2) unsigned default NULL,
  212. `equip_jobs` int(12) unsigned default NULL,
  213. `equip_upper` tinyint(8) unsigned default NULL,
  214. `equip_genders` tinyint(2) unsigned default NULL,
  215. `equip_locations` smallint(4) unsigned default NULL,
  216. `weapon_level` tinyint(2) unsigned default NULL,
  217. `equip_level` varchar(10) default '',
  218. `refineable` tinyint(1) unsigned default NULL,
  219. `view` smallint(3) unsigned default NULL,
  220. `script` text,
  221. `equip_script` text,
  222. `unequip_script` text,
  223. PRIMARY KEY (`id`)
  224. ) ENGINE=MyISAM;
  225. ";
  226.     #NOTE: These do not match the table struct defaults.
  227.     @defaults = ('0','\'\'','\'\'','0','NULL','NULL','0','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL');
  228.     }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement