convert_sql.pl 13 KB

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