diff --git a/tools/MapleInvalidItemIdFetcher/src/maplenoitemidfetcher/MapleNoItemIdFetcher.java b/src/main/java/tools/mapletools/NoItemIdFetcher.java similarity index 55% rename from tools/MapleInvalidItemIdFetcher/src/maplenoitemidfetcher/MapleNoItemIdFetcher.java rename to src/main/java/tools/mapletools/NoItemIdFetcher.java index 1647758035..602d5420c2 100644 --- a/tools/MapleInvalidItemIdFetcher/src/maplenoitemidfetcher/MapleNoItemIdFetcher.java +++ b/src/main/java/tools/mapletools/NoItemIdFetcher.java @@ -1,70 +1,35 @@ -/* - This file is part of the HeavenMS MapleStory Server - Copyleft (L) 2016 - 2019 RonanLana +package tools.mapletools; - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as - published by the Free Software Foundation version 3 as published by - the Free Software Foundation. You may not use, modify or distribute - this program under any other version of the GNU Affero General Public - License. +import provider.wz.WZFiles; - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . -*/ -package maplenoitemidfetcher; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.PrintWriter; +import java.io.*; +import java.nio.charset.StandardCharsets; import java.sql.Connection; -import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import java.util.*; /** - * * @author RonanLana - * + *

* This application finds inexistent itemids within the drop data from * the Maplestory database specified in the URL below. This program * assumes all itemids uses 7 digits. - * + *

* A file is generated listing all the inexistent ids. */ -public class MapleNoItemIdFetcher { - static String host = "jdbc:mysql://localhost:3306/cosmic"; - static String driver = "com.mysql.jdbc.Driver"; - static String username = "cosmic_server"; - static String password = "snailshell"; +public class NoItemIdFetcher { + private static final File OUTPUT_FILE = ToolConstants.getOutputFile("no_item_id_report.txt"); + private static final Connection con = SimpleDatabaseConnection.getConnection(); - static String wzPath = "../../wz"; - static String newFile = "lib/result.txt"; + private static final Set existingIds = new HashSet<>(); + private static final Set nonExistingIds = new HashSet<>(); - static Connection con = null; - static PrintWriter printWriter = null; - static InputStreamReader fileReader = null; - static BufferedReader bufferedReader = null; - static byte status = 0; - static int itemId = -1; - - static Set existingIds = new HashSet<>(); - static Set nonExistingIds = new HashSet<>(); + private static PrintWriter printWriter = null; + private static BufferedReader bufferedReader = null; + private static byte status = 0; + private static int itemId = -1; private static String getName(String token) { int i, j; @@ -79,42 +44,39 @@ public class MapleNoItemIdFetcher { token.getChars(i, j, dest, 0); d = new String(dest); - return(d); + return (d); } private static void forwardCursor(int st) { String line = null; try { - while(status >= st && (line = bufferedReader.readLine()) != null) { + while (status >= st && (line = bufferedReader.readLine()) != null) { simpleToken(line); } - } - catch(Exception e) { + } catch (Exception e) { e.printStackTrace(); } } private static void simpleToken(String token) { - if(token.contains("/imgdir")) { + if (token.contains("/imgdir")) { status -= 1; - } - else if(token.contains("imgdir")) { + } else if (token.contains("imgdir")) { status += 1; } } private static void translateToken(String token) { String d; - - if(token.contains("/imgdir")) { + + if (token.contains("/imgdir")) { status -= 1; - } - else if(token.contains("imgdir")) { - if(status == 1) { //getting ItemId + } else if (token.contains("imgdir")) { + if (status == 1) { //getting ItemId d = getName(token); itemId = Integer.parseInt(d.substring(1, 8)); - + existingIds.add(itemId); forwardCursor(status); } @@ -127,36 +89,34 @@ public class MapleNoItemIdFetcher { // This will reference one line at a time String line = null; - try { - fileReader = new InputStreamReader(new FileInputStream(file), "UTF-8"); + try { + InputStreamReader fileReader = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8); bufferedReader = new BufferedReader(fileReader); - + status = 0; try { - while((line = bufferedReader.readLine()) != null) { + while ((line = bufferedReader.readLine()) != null) { translateToken(line); } - } catch(NumberFormatException npe) { + } catch (NumberFormatException npe) { // second criteria, itemid is on the name of the file - + try { itemId = Integer.parseInt(file.getName().substring(0, 7)); existingIds.add(itemId); - } catch(NumberFormatException npe2) {} + } catch (NumberFormatException npe2) { + } } bufferedReader.close(); fileReader.close(); - } - - catch(FileNotFoundException ex) { + } catch (FileNotFoundException ex) { System.out.println("Unable to open file '" + file.getName() + "'"); - } - catch(IOException ex) { + } catch (IOException ex) { System.out.println("Error reading file '" + file.getName() + "'"); } } - + private static void readEquipDataDirectory(String dirPath) { File[] folders = new File(dirPath).listFiles(); //If this pathname does not denote a directory, then listFiles() returns null. @@ -164,21 +124,22 @@ public class MapleNoItemIdFetcher { for (File folder : folders) { // enter all subfolders if (folder.isDirectory()) { System.out.println("Reading '" + dirPath + "/" + folder.getName() + "'..."); - + try { File[] files = folder.listFiles(); - + for (File file : files) { // enter all XML files under subfolders if (file.isFile()) { itemId = Integer.parseInt(file.getName().substring(0, 8)); existingIds.add(itemId); } } - } catch (NumberFormatException nfe) {} + } catch (NumberFormatException nfe) { + } } } } - + private static void readItemDataDirectory(String dirPath) { File[] folders = new File(dirPath).listFiles(); //If this pathname does not denote a directory, then listFiles() returns null. @@ -186,9 +147,9 @@ public class MapleNoItemIdFetcher { for (File folder : folders) { // enter all subfolders if (folder.isDirectory()) { System.out.println("Reading '" + dirPath + "/" + folder.getName() + "'..."); - + File[] files = folder.listFiles(); - + for (File file : files) { // enter all XML files under subfolders if (file.isFile()) { readItemDataFile(file); @@ -197,13 +158,13 @@ public class MapleNoItemIdFetcher { } } } - + private static void evaluateDropsFromTable(String table) throws SQLException { PreparedStatement ps = con.prepareStatement("SELECT DISTINCT itemid FROM " + table + ";"); ResultSet rs = ps.executeQuery(); - while(rs.next()) { - if(!existingIds.contains(rs.getInt(1))) { + while (rs.next()) { + if (!existingIds.contains(rs.getInt(1))) { nonExistingIds.add(rs.getInt(1)); } } @@ -211,64 +172,45 @@ public class MapleNoItemIdFetcher { rs.close(); ps.close(); } - + private static void evaluateDropsFromDb() { try { System.out.println("Evaluating item data on DB..."); - - Class.forName(driver).newInstance(); - con = DriverManager.getConnection(host, username, password); - + evaluateDropsFromTable("drop_data"); evaluateDropsFromTable("reactordrops"); - - if(!nonExistingIds.isEmpty()) { + + if (!nonExistingIds.isEmpty()) { List list = new ArrayList<>(nonExistingIds); Collections.sort(list); - - for(Integer i : list) { + + for (Integer i : list) { printWriter.println(i); } } - + System.out.println("Inexistent itemid count: " + nonExistingIds.size()); System.out.println("Total itemid count: " + existingIds.size()); - - con.close(); - } - - catch(ClassNotFoundException e) { - System.out.println("Error: could not find class"); - System.out.println(e.getMessage()); - } - catch(InstantiationException e) { - System.out.println("Error: instantiation failure"); - System.out.println(e.getMessage()); - } - - catch(SQLException e) { - e.printStackTrace(); - } - - catch(Exception e) { + con.close(); + } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { try { - printWriter = new PrintWriter(newFile, "UTF-8"); - + printWriter = new PrintWriter(OUTPUT_FILE, StandardCharsets.UTF_8); + existingIds.add(0); // meso itemid - readEquipDataDirectory(wzPath + "/Character.wz"); - readItemDataDirectory(wzPath + "/Item.wz"); - + readEquipDataDirectory(WZFiles.CHARACTER.getFilePath()); + readItemDataDirectory(WZFiles.ITEM.getFilePath()); + evaluateDropsFromDb(); - + printWriter.close(); - } catch(Exception e) { + } catch (Exception e) { e.printStackTrace(); } } -} +} \ No newline at end of file diff --git a/tools/MapleInvalidItemIdFetcher/lib/result.txt b/tools/MapleInvalidItemIdFetcher/lib/result.txt deleted file mode 100644 index e69de29bb2..0000000000