convert_sql.pl 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #!/usr/bin/perl
  2. # Mob Database:
  3. # --i=../db/pre-re/mob_db.txt --o=../sql-files/mob_db.sql --t=pre --m=mob --table=mob_db
  4. # --i=../db/re/mob_db.txt --o=../sql-files/mob_db_re.sql --t=re --m=mob --table=mob_db_re
  5. #
  6. # Mob Skill Database:
  7. # --i=../db/pre-re/mob_skill_db.txt --o=../sql-files/mob_skill_db.sql --t=pre --m=mob_skill --table=mob_skill_db
  8. # --i=../db/re/mob_skill_db.txt --o=../sql-files/mob_skill_db_re.sql --t=re --m=mob_skill --table=mob_skill_db_re
  9. #
  10. # List of options:
  11. # convert_sql.pl --help
  12. use strict;
  13. use warnings;
  14. use Getopt::Long;
  15. use File::Basename;
  16. my $sFilein = "";
  17. my $sFileout = "";
  18. my $sTarget = "";
  19. my $sType = "";
  20. my $sHelp = 0;
  21. my $sTable = "";
  22. my $db;
  23. my $nb_columns;
  24. my @str_col = (); #Use basic escape.
  25. my @str_col2 = (); #Use second escape (currently for scripts).
  26. my $line_format;
  27. my $create_table;
  28. my @defaults = ();
  29. Main();
  30. sub GetArgs {
  31. GetOptions(
  32. 'i=s' => \$sFilein, #Output file name.
  33. 'o=s' => \$sFileout, #Input file name.
  34. 't=s' => \$sTarget, #Renewal setting: pre-re, re.
  35. 'm=s' => \$sType, #Database: mob, mob_skill.
  36. 'table=s' => \$sTable, #Table name.
  37. 'help!' => \$sHelp,
  38. ) or $sHelp=1; #Display help if invalid options are supplied.
  39. my $sValidTarget = "re|pre";
  40. my $sValidType = "mob|mob_skill";
  41. if( $sHelp ) {
  42. print "Incorrect option specified. Available options:\n"
  43. ."\t --o=filename => Output file name. \n"
  44. ."\t --i=filename => Input file name. \n"
  45. ."\t --table=tablename => Table name to create. \n"
  46. ."\t --t=target => Specify target ([$sValidTarget]). \n"
  47. ."\t --m=type => Specify type ([$sValidType]). \n";
  48. exit;
  49. }
  50. unless($sFilein or $sFileout){
  51. print "ERROR: Filename_in and Filename_out are required to continue.\n";
  52. exit;
  53. }
  54. unless($sTarget =~ /$sValidTarget/i){
  55. print "ERROR: Incorrect target specified. Available targets:\n"
  56. ."\t --t => Target (specify which kind of table_struct to build [$sValidTarget]).\n";
  57. exit;
  58. }
  59. unless($sType =~ /$sValidType/i){
  60. print "ERROR: Incorrect type specified. Available types:\n"
  61. ."\t --m => Type (specify which data entry to use [$sValidType]).\n";
  62. exit;
  63. }
  64. }
  65. sub Main {
  66. GetArgs();
  67. my($filename, $dir, $suffix) = fileparse($0);
  68. chdir $dir; #put ourself like was called in tool folder
  69. BuildDataForType($sTarget,$sType);
  70. ConvertFile($sFilein,$sFileout,$sType);
  71. print "Conversion ended.\n";
  72. }
  73. sub ConvertFile { my($sFilein,$sFileout,$sType)=@_;
  74. my $sFHout;
  75. my %hAEgisName = ();
  76. print "Starting ConvertFile with: \n\t filein=$sFilein \n\t fileout=$sFileout \n";
  77. open FHIN,"$sFilein" or die "ERROR: Can't read or locate $sFilein.\n";
  78. open $sFHout,">$sFileout" or die "ERROR: Can't write $sFileout.\n";
  79. printf $sFHout ("%s\n",$create_table);
  80. while(my $ligne=<FHIN>) {
  81. my $sWasCom = 0;
  82. if ($ligne =~ /^\s*$/ ) {
  83. print $sFHout "\n";
  84. next;
  85. }
  86. if ($ligne =~ /[^\r\n]+/) {
  87. $ligne = $&;
  88. if ($ligne =~ /^\/\//) {
  89. printf $sFHout ("#");
  90. $ligne = substr($ligne, 2);
  91. $sWasCom = 1;
  92. }
  93. my @champ = ();
  94. if ($sType =~ /mob_skill/i ) {
  95. if ($ligne =~ $line_format ) { @champ = ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19); }
  96. }
  97. elsif ($sType =~ /mob/i) { @champ = split(",",$ligne); }
  98. if ($#champ != $nb_columns - 1) { #Can't parse, it's a real comment.
  99. printf $sFHout ("%s\n", $ligne);
  100. } else {
  101. printf $sFHout ("REPLACE INTO `%s` VALUES (", $db);
  102. for (my $i=0; $i<$#champ; $i++) {
  103. printField($sFHout,$champ[$i],",",$i);
  104. }
  105. printField($sFHout,$champ[$#champ],");\n",$#champ);
  106. }
  107. }
  108. }
  109. print $sFHout "\n";
  110. }
  111. sub printField { my ($sFHout,$str, $suffix, $idCol) = @_;
  112. # Remove first { and last } .
  113. if ($str =~ /{.*}/) {
  114. $str = substr($&,1,-1);
  115. }
  116. # If nothing, put NULL.
  117. if ($str eq "") {
  118. my $sDef;
  119. if(scalar(@defaults)) { $sDef = $defaults[$idCol]; } #Use default in array.
  120. else { $sDef = "NULL" unless($sDef); } #Let SQL handle the default.
  121. print $sFHout "$sDef$suffix";
  122. } else {
  123. my $flag = 0;
  124. # Search if it's a string column?
  125. foreach my $col (@str_col) {
  126. if ($col == $idCol) {
  127. $flag |= 1;
  128. last;
  129. }
  130. }
  131. foreach my $col (@str_col2) {
  132. if ($col == $idCol) {
  133. $flag |= 2;
  134. last;
  135. }
  136. }
  137. if ($flag & 3) {
  138. # String column, so escape , remove trailing and add '' .
  139. my $string;
  140. $string = escape($str,"'","\\'") if($flag & 1) ;
  141. $string =~ s/\s+$//; #Remove trailing spaces.
  142. $string =~ s/^\s+//; #Remove leading spaces.
  143. $string = escape($string,'\\\"','\\\\\"') if($flag & 2) ;
  144. printf $sFHout ("'%s'%s", $string, $suffix);
  145. } else {
  146. # Not a string column.
  147. printf $sFHout ("%s%s", $str,$suffix);
  148. }
  149. }
  150. }
  151. sub escape { my ($str,$sregex,$sreplace) = @_;
  152. my @str_splitted = split($sregex, $str);
  153. my $result = "";
  154. for (my $i=0; $i<=$#str_splitted; $i++) {
  155. if ($i == 0) {
  156. $result = $str_splitted[0];
  157. } else {
  158. $result = $result.$sreplace.$str_splitted[$i];
  159. }
  160. }
  161. return $result
  162. }
  163. sub BuildDataForType{ my($sTarget,$sType) = @_;
  164. print "Starting BuildDataForType with: \n\t Target=$sTarget, Type=$sType \n";
  165. if($sType =~ /mob_skill/i) { #Same format for Pre-Renewal and Renewal.
  166. $db = $sTable;
  167. if($sTarget =~ /Pre/i){
  168. $db = "mob_skill_db" unless($db);
  169. }else{
  170. $db = "mob_skill_db_re" unless($db);
  171. }
  172. $nb_columns = 19;
  173. @str_col = (1,2,8,9,10,11,17,18);
  174. $line_format = "([^\,]*),"x($nb_columns-1)."([^\,]*)";
  175. $create_table =
  176. "#
  177. # Table structure for table `$db`
  178. #
  179. DROP TABLE IF EXISTS `$db`;
  180. CREATE TABLE IF NOT EXISTS `$db` (
  181. `MOB_ID` smallint(6) NOT NULL,
  182. `INFO` text NOT NULL,
  183. `STATE` text NOT NULL,
  184. `SKILL_ID` smallint(6) NOT NULL,
  185. `SKILL_LV` tinyint(4) NOT NULL,
  186. `RATE` smallint(4) NOT NULL,
  187. `CASTTIME` mediumint(9) NOT NULL,
  188. `DELAY` int(9) NOT NULL,
  189. `CANCELABLE` text NOT NULL,
  190. `TARGET` text NOT NULL,
  191. `CONDITION` text NOT NULL,
  192. `CONDITION_VALUE` text,
  193. `VAL1` mediumint(9) DEFAULT NULL,
  194. `VAL2` mediumint(9) DEFAULT NULL,
  195. `VAL3` mediumint(9) DEFAULT NULL,
  196. `VAL4` mediumint(9) DEFAULT NULL,
  197. `VAL5` mediumint(9) DEFAULT NULL,
  198. `EMOTION` text,
  199. `CHAT` text
  200. ) ENGINE=MyISAM;
  201. ";
  202. }
  203. elsif($sType =~ /mob/i) { #Same format for Pre-Renewal and Renewal.
  204. $db = $sTable;
  205. if($sTarget =~ /Pre/i){
  206. $db = "mob_db" unless($db);
  207. }else{
  208. $db = "mob_db_re" unless($db);
  209. }
  210. $nb_columns = 57;
  211. @str_col = (1,2,3);
  212. $line_format = "([^\,]*),"x($nb_columns-1);
  213. $create_table =
  214. "#
  215. # Table structure for table `$db`
  216. #
  217. DROP TABLE IF EXISTS `$db`;
  218. CREATE TABLE `$db` (
  219. `ID` mediumint(9) unsigned NOT NULL default '0',
  220. `Sprite` text NOT NULL,
  221. `kName` text NOT NULL,
  222. `iName` text NOT NULL,
  223. `LV` tinyint(6) unsigned NOT NULL default '0',
  224. `HP` int(9) unsigned NOT NULL default '0',
  225. `SP` mediumint(9) unsigned NOT NULL default '0',
  226. `EXP` mediumint(9) unsigned NOT NULL default '0',
  227. `JEXP` mediumint(9) unsigned NOT NULL default '0',
  228. `Range1` tinyint(4) unsigned NOT NULL default '0',
  229. `ATK1` smallint(6) unsigned NOT NULL default '0',
  230. `ATK2` smallint(6) unsigned NOT NULL default '0',
  231. `DEF` smallint(6) unsigned NOT NULL default '0',
  232. `MDEF` smallint(6) unsigned NOT NULL default '0',
  233. `STR` smallint(6) unsigned NOT NULL default '0',
  234. `AGI` smallint(6) unsigned NOT NULL default '0',
  235. `VIT` smallint(6) unsigned NOT NULL default '0',
  236. `INT` smallint(6) unsigned NOT NULL default '0',
  237. `DEX` smallint(6) unsigned NOT NULL default '0',
  238. `LUK` smallint(6) unsigned NOT NULL default '0',
  239. `Range2` tinyint(4) unsigned NOT NULL default '0',
  240. `Range3` tinyint(4) unsigned NOT NULL default '0',
  241. `Scale` tinyint(4) unsigned NOT NULL default '0',
  242. `Race` tinyint(4) unsigned NOT NULL default '0',
  243. `Element` tinyint(4) unsigned NOT NULL default '0',
  244. `Mode` int(11) unsigned NOT NULL default '0',
  245. `Speed` smallint(6) unsigned NOT NULL default '0',
  246. `aDelay` smallint(6) unsigned NOT NULL default '0',
  247. `aMotion` smallint(6) unsigned NOT NULL default '0',
  248. `dMotion` smallint(6) unsigned NOT NULL default '0',
  249. `MEXP` mediumint(9) unsigned NOT NULL default '0',
  250. `MVP1id` int(10) unsigned NOT NULL default '0',
  251. `MVP1per` smallint(9) unsigned NOT NULL default '0',
  252. `MVP2id` int(10) unsigned NOT NULL default '0',
  253. `MVP2per` smallint(9) unsigned NOT NULL default '0',
  254. `MVP3id` int(10) unsigned NOT NULL default '0',
  255. `MVP3per` smallint(9) unsigned NOT NULL default '0',
  256. `Drop1id` int(10) unsigned NOT NULL default '0',
  257. `Drop1per` smallint(9) unsigned NOT NULL default '0',
  258. `Drop2id` int(10) unsigned NOT NULL default '0',
  259. `Drop2per` smallint(9) unsigned NOT NULL default '0',
  260. `Drop3id` int(10) unsigned NOT NULL default '0',
  261. `Drop3per` smallint(9) unsigned NOT NULL default '0',
  262. `Drop4id` int(10)) unsigned NOT NULL default '0',
  263. `Drop4per` smallint(9) unsigned NOT NULL default '0',
  264. `Drop5id` int(10) unsigned NOT NULL default '0',
  265. `Drop5per` smallint(9) unsigned NOT NULL default '0',
  266. `Drop6id` int(10) unsigned NOT NULL default '0',
  267. `Drop6per` smallint(9) unsigned NOT NULL default '0',
  268. `Drop7id` int(10) unsigned NOT NULL default '0',
  269. `Drop7per` smallint(9) unsigned NOT NULL default '0',
  270. `Drop8id` int(10) unsigned NOT NULL default '0',
  271. `Drop8per` smallint(9) unsigned NOT NULL default '0',
  272. `Drop9id` int(10) unsigned NOT NULL default '0',
  273. `Drop9per` smallint(9) unsigned NOT NULL default '0',
  274. `DropCardid` int(10) unsigned NOT NULL default '0',
  275. `DropCardper` smallint(9) unsigned NOT NULL default '0',
  276. PRIMARY KEY (`ID`)
  277. ) ENGINE=MyISAM;
  278. ";
  279. }
  280. }