convert_sql.pl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. my %hAEgisName = ();
  80. print "Starting ConvertFile with: \n\t filein=$sFilein \n\t fileout=$sFileout \n";
  81. open FHIN,"$sFilein" or die "ERROR: Can't read or locate $sFilein.\n";
  82. open $sFHout,">$sFileout" or die "ERROR: Can't write $sFileout.\n";
  83. printf $sFHout ("%s\n",$create_table);
  84. while(my $ligne=<FHIN>) {
  85. my $sWasCom = 0;
  86. if ($ligne =~ /^\s*$/ ) {
  87. print $sFHout "\n";
  88. next;
  89. }
  90. if ($ligne =~ /[^\r\n]+/) {
  91. $ligne = $&;
  92. if ($ligne =~ /^\/\//) {
  93. printf $sFHout ("#");
  94. $ligne = substr($ligne, 2);
  95. $sWasCom = 1;
  96. }
  97. my @champ = ();
  98. if ($sType =~ /mob_skill/i ) {
  99. if ($ligne =~ $line_format ) { @champ = ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19); }
  100. }
  101. elsif ($sType =~ /mob/i) { @champ = split(",",$ligne); }
  102. elsif ($sType =~ /item/i ) {
  103. 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); }
  104. }
  105. if ($#champ != $nb_columns - 1) { #Can't parse, it's a real comment.
  106. printf $sFHout ("%s\n", $ligne);
  107. } else {
  108. printf $sFHout ("REPLACE INTO `%s` VALUES (", $db);
  109. if( $sType =~ /item/i and $sWasCom == 0){ #check if aegis name is duplicate, (only for not com)
  110. $hAEgisName{$champ[1]}++;
  111. if($hAEgisName{$champ[1]} > 1){
  112. print "Warning, aegisName=$champ[1] multiple occurence found, val=$hAEgisName{$champ[1]}, line=$ligne\n" ;
  113. $champ[1] = $champ[1]."_"x($hAEgisName{$champ[1]}-1);
  114. print "Converted into '$champ[1]'\n" ;
  115. }
  116. }
  117. for (my $i=0; $i<$#champ; $i++) {
  118. printField($sFHout,$champ[$i],",",$i);
  119. }
  120. printField($sFHout,$champ[$#champ],");\n",$#champ);
  121. }
  122. }
  123. }
  124. print $sFHout "\n";
  125. }
  126. sub printField { my ($sFHout,$str, $suffix, $idCol) = @_;
  127. # Remove first { and last } .
  128. if ($str =~ /{.*}/) {
  129. $str = substr($&,1,-1);
  130. }
  131. # If nothing, put NULL.
  132. if ($str eq "") {
  133. my $sDef;
  134. if(scalar(@defaults)) { $sDef = $defaults[$idCol]; } #Use default in array.
  135. else { $sDef = "NULL" unless($sDef); } #Let SQL handle the default.
  136. print $sFHout "$sDef$suffix";
  137. } else {
  138. my $flag = 0;
  139. # Search if it's a string column?
  140. foreach my $col (@str_col) {
  141. if ($col == $idCol) {
  142. $flag |= 1;
  143. last;
  144. }
  145. }
  146. foreach my $col (@str_col2) {
  147. if ($col == $idCol) {
  148. $flag |= 2;
  149. last;
  150. }
  151. }
  152. if ($flag & 3) {
  153. # String column, so escape , remove trailing and add '' .
  154. my $string;
  155. $string = escape($str,"'","\\'") if($flag & 1) ;
  156. $string =~ s/\s+$//; #Remove trailing spaces.
  157. $string =~ s/^\s+//; #Remove leading spaces.
  158. $string = escape($string,'\\\"','\\\\\"') if($flag & 2) ;
  159. printf $sFHout ("'%s'%s", $string, $suffix);
  160. } else {
  161. # Not a string column.
  162. printf $sFHout ("%s%s", $str,$suffix);
  163. }
  164. }
  165. }
  166. sub escape { my ($str,$sregex,$sreplace) = @_;
  167. my @str_splitted = split($sregex, $str);
  168. my $result = "";
  169. for (my $i=0; $i<=$#str_splitted; $i++) {
  170. if ($i == 0) {
  171. $result = $str_splitted[0];
  172. } else {
  173. $result = $result.$sreplace.$str_splitted[$i];
  174. }
  175. }
  176. return $result
  177. }
  178. sub BuildDataForType{ my($sTarget,$sType) = @_;
  179. print "Starting BuildDataForType with: \n\t Target=$sTarget, Type=$sType \n";
  180. if($sType =~ /item/i) {
  181. if($sTarget =~ /Pre/i){
  182. $db = $sTable;
  183. $db = "item_db" unless($db);
  184. $nb_columns = 22;
  185. @str_col = (1,2,19,20,21);
  186. @str_col2 = (19,20,21);
  187. $line_format = "([^\,]*),"x($nb_columns-3)."(\{.*\}),"x(2)."(\{.*\})"; #Last 3 columns are scripts.
  188. $create_table =
  189. "#
  190. # Table structure for table `$db`
  191. #
  192. DROP TABLE IF EXISTS `$db`;
  193. CREATE TABLE `$db` (
  194. `id` smallint(5) unsigned NOT NULL DEFAULT '0',
  195. `name_english` varchar(50) NOT NULL DEFAULT '',
  196. `name_japanese` varchar(50) NOT NULL DEFAULT '',
  197. `type` tinyint(2) unsigned NOT NULL DEFAULT '0',
  198. `price_buy` mediumint(8) unsigned DEFAULT NULL,
  199. `price_sell` mediumint(8) unsigned DEFAULT NULL,
  200. `weight` smallint(5) unsigned NOT NULL DEFAULT '0',
  201. `attack` smallint(5) unsigned DEFAULT NULL,
  202. `defence` smallint(5) unsigned DEFAULT NULL,
  203. `range` tinyint(2) unsigned DEFAULT NULL,
  204. `slots` tinyint(2) unsigned DEFAULT NULL,
  205. `equip_jobs` int(10) unsigned DEFAULT NULL,
  206. `equip_upper` tinyint(2) unsigned DEFAULT NULL,
  207. `equip_genders` tinyint(1) unsigned DEFAULT NULL,
  208. `equip_locations` mediumint(7) unsigned DEFAULT NULL,
  209. `weapon_level` tinyint(1) unsigned DEFAULT NULL,
  210. `equip_level` tinyint(3) unsigned DEFAULT NULL,
  211. `refineable` tinyint(1) unsigned DEFAULT NULL,
  212. `view` smallint(5) unsigned DEFAULT NULL,
  213. `script` text,
  214. `equip_script` text,
  215. `unequip_script` text,
  216. PRIMARY KEY (`id`),
  217. UNIQUE INDEX `UniqueAegisName` (`name_english`)
  218. ) ENGINE=MyISAM;
  219. ";
  220. #NOTE: These do not match the table struct defaults.
  221. @defaults = ('0','\'\'','\'\'','0','NULL','NULL',0,'NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL');
  222. }
  223. elsif($sTarget =~ /Re/i){
  224. $db = $sTable;
  225. $db = "item_db_re" unless($db);
  226. $nb_columns = 22;
  227. @str_col = (1,2,7,16,19,20,21);
  228. @str_col2 = (19,20,21);
  229. $line_format = "([^\,]*),"x($nb_columns-3)."(\{.*\}),"x(2)."(\{.*\})"; #Last 3 columns are scripts.
  230. $create_table =
  231. "#
  232. # Table structure for table `$db`
  233. #
  234. DROP TABLE IF EXISTS `$db`;
  235. CREATE TABLE `$db` (
  236. `id` smallint(5) unsigned NOT NULL DEFAULT '0',
  237. `name_english` varchar(50) NOT NULL DEFAULT '',
  238. `name_japanese` varchar(50) NOT NULL DEFAULT '',
  239. `type` tinyint(2) unsigned NOT NULL DEFAULT '0',
  240. `price_buy` mediumint(8) unsigned DEFAULT NULL,
  241. `price_sell` mediumint(8) unsigned DEFAULT NULL,
  242. `weight` smallint(5) unsigned NOT NULL DEFAULT '0',
  243. `atk:matk` varchar(11) DEFAULT NULL,
  244. `defence` smallint(5) unsigned DEFAULT NULL,
  245. `range` tinyint(2) unsigned DEFAULT NULL,
  246. `slots` tinyint(2) unsigned DEFAULT NULL,
  247. `equip_jobs` int(10) unsigned DEFAULT NULL,
  248. `equip_upper` tinyint(2) unsigned DEFAULT NULL,
  249. `equip_genders` tinyint(1) unsigned DEFAULT NULL,
  250. `equip_locations` mediumint(7) unsigned DEFAULT NULL,
  251. `weapon_level` tinyint(1) unsigned DEFAULT NULL,
  252. `equip_level` varchar(10) DEFAULT NULL,
  253. `refineable` tinyint(1) unsigned DEFAULT NULL,
  254. `view` smallint(5) unsigned DEFAULT NULL,
  255. `script` text,
  256. `equip_script` text,
  257. `unequip_script` text,
  258. PRIMARY KEY (`id`),
  259. UNIQUE INDEX `UniqueAegisName` (`name_english`)
  260. ) ENGINE=MyISAM;
  261. ";
  262. #NOTE: These do not match the table struct defaults.
  263. @defaults = ('0','\'\'','\'\'','0','NULL','NULL',0,'NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL','NULL');
  264. }
  265. }
  266. elsif($sType =~ /mob_skill/i) { #Same format for Pre-Renewal and Renewal.
  267. $db = $sTable;
  268. if($sTarget =~ /Pre/i){
  269. $db = "mob_skill_db" unless($db);
  270. }else{
  271. $db = "mob_skill_db_re" unless($db);
  272. }
  273. $nb_columns = 19;
  274. @str_col = (1,2,8,9,10,11,17,18);
  275. $line_format = "([^\,]*),"x($nb_columns-1)."([^\,]*)";
  276. $create_table =
  277. "#
  278. # Table structure for table `$db`
  279. #
  280. DROP TABLE IF EXISTS `$db`;
  281. CREATE TABLE IF NOT EXISTS `$db` (
  282. `MOB_ID` smallint(6) NOT NULL,
  283. `INFO` text NOT NULL,
  284. `STATE` text NOT NULL,
  285. `SKILL_ID` smallint(6) NOT NULL,
  286. `SKILL_LV` tinyint(4) NOT NULL,
  287. `RATE` smallint(4) NOT NULL,
  288. `CASTTIME` mediumint(9) NOT NULL,
  289. `DELAY` int(9) NOT NULL,
  290. `CANCELABLE` text NOT NULL,
  291. `TARGET` text NOT NULL,
  292. `CONDITION` text NOT NULL,
  293. `CONDITION_VALUE` text,
  294. `VAL1` mediumint(9) DEFAULT NULL,
  295. `VAL2` mediumint(9) DEFAULT NULL,
  296. `VAL3` mediumint(9) DEFAULT NULL,
  297. `VAL4` mediumint(9) DEFAULT NULL,
  298. `VAL5` mediumint(9) DEFAULT NULL,
  299. `EMOTION` text,
  300. `CHAT` text
  301. ) ENGINE=MyISAM;
  302. ";
  303. }
  304. elsif($sType =~ /mob/i) { #Same format for Pre-Renewal and Renewal.
  305. $db = $sTable;
  306. if($sTarget =~ /Pre/i){
  307. $db = "mob_db" unless($db);
  308. }else{
  309. $db = "mob_db_re" unless($db);
  310. }
  311. $nb_columns = 57;
  312. @str_col = (1,2,3);
  313. $line_format = "([^\,]*),"x($nb_columns-1);
  314. $create_table =
  315. "#
  316. # Table structure for table `$db`
  317. #
  318. DROP TABLE IF EXISTS `$db`;
  319. CREATE TABLE `$db` (
  320. `ID` mediumint(9) unsigned NOT NULL default '0',
  321. `Sprite` text NOT NULL,
  322. `kName` text NOT NULL,
  323. `iName` text NOT NULL,
  324. `LV` tinyint(6) unsigned NOT NULL default '0',
  325. `HP` int(9) unsigned NOT NULL default '0',
  326. `SP` mediumint(9) unsigned NOT NULL default '0',
  327. `EXP` mediumint(9) unsigned NOT NULL default '0',
  328. `JEXP` mediumint(9) unsigned NOT NULL default '0',
  329. `Range1` tinyint(4) unsigned NOT NULL default '0',
  330. `ATK1` smallint(6) unsigned NOT NULL default '0',
  331. `ATK2` smallint(6) unsigned NOT NULL default '0',
  332. `DEF` smallint(6) unsigned NOT NULL default '0',
  333. `MDEF` smallint(6) unsigned NOT NULL default '0',
  334. `STR` smallint(6) unsigned NOT NULL default '0',
  335. `AGI` smallint(6) unsigned NOT NULL default '0',
  336. `VIT` smallint(6) unsigned NOT NULL default '0',
  337. `INT` smallint(6) unsigned NOT NULL default '0',
  338. `DEX` smallint(6) unsigned NOT NULL default '0',
  339. `LUK` smallint(6) unsigned NOT NULL default '0',
  340. `Range2` tinyint(4) unsigned NOT NULL default '0',
  341. `Range3` tinyint(4) unsigned NOT NULL default '0',
  342. `Scale` tinyint(4) unsigned NOT NULL default '0',
  343. `Race` tinyint(4) unsigned NOT NULL default '0',
  344. `Element` tinyint(4) unsigned NOT NULL default '0',
  345. `Mode` int(11) unsigned NOT NULL default '0',
  346. `Speed` smallint(6) unsigned NOT NULL default '0',
  347. `aDelay` smallint(6) unsigned NOT NULL default '0',
  348. `aMotion` smallint(6) unsigned NOT NULL default '0',
  349. `dMotion` smallint(6) unsigned NOT NULL default '0',
  350. `MEXP` mediumint(9) unsigned NOT NULL default '0',
  351. `MVP1id` smallint(5) unsigned NOT NULL default '0',
  352. `MVP1per` smallint(9) unsigned NOT NULL default '0',
  353. `MVP2id` smallint(5) unsigned NOT NULL default '0',
  354. `MVP2per` smallint(9) unsigned NOT NULL default '0',
  355. `MVP3id` smallint(5) unsigned NOT NULL default '0',
  356. `MVP3per` smallint(9) unsigned NOT NULL default '0',
  357. `Drop1id` smallint(5) unsigned NOT NULL default '0',
  358. `Drop1per` smallint(9) unsigned NOT NULL default '0',
  359. `Drop2id` smallint(5) unsigned NOT NULL default '0',
  360. `Drop2per` smallint(9) unsigned NOT NULL default '0',
  361. `Drop3id` smallint(5) unsigned NOT NULL default '0',
  362. `Drop3per` smallint(9) unsigned NOT NULL default '0',
  363. `Drop4id` smallint(5) unsigned NOT NULL default '0',
  364. `Drop4per` smallint(9) unsigned NOT NULL default '0',
  365. `Drop5id` smallint(5) unsigned NOT NULL default '0',
  366. `Drop5per` smallint(9) unsigned NOT NULL default '0',
  367. `Drop6id` smallint(5) unsigned NOT NULL default '0',
  368. `Drop6per` smallint(9) unsigned NOT NULL default '0',
  369. `Drop7id` smallint(5) unsigned NOT NULL default '0',
  370. `Drop7per` smallint(9) unsigned NOT NULL default '0',
  371. `Drop8id` smallint(5) unsigned NOT NULL default '0',
  372. `Drop8per` smallint(9) unsigned NOT NULL default '0',
  373. `Drop9id` smallint(5) unsigned NOT NULL default '0',
  374. `Drop9per` smallint(9) unsigned NOT NULL default '0',
  375. `DropCardid` smallint(5) unsigned NOT NULL default '0',
  376. `DropCardper` smallint(9) unsigned NOT NULL default '0',
  377. PRIMARY KEY (`ID`)
  378. ) ENGINE=MyISAM;
  379. ";
  380. }
  381. }