package org.filehandler; import java.io.*; import java.util.Scanner; import java.util.StringTokenizer; public class Main { // fields private static String newline = System.getProperty("line.separator"); private static String keyInput = "not set yet"; //private static boolean gUI = true; private static boolean mainMenu = true; private static boolean serverListMenu = false; private static boolean deleteServerMenu = false; private static boolean addServerMenu = true; //private static String [] serverListLineArray = new String[99]; private static String [] serverNamesArray = new String[99]; private static String [] serverAddressArray = new String[99]; public void main(String[] args) throws Exception { /* while (gUI == true) { new GUI(); return; } */ while (mainMenu == true | serverListMenu == true | deleteServerMenu == true | addServerMenu == true) { cls(); while (mainMenu == true) { mainMenu(); } while (serverListMenu == true) { serverListMenu(); } while (deleteServerMenu == true) { deleteServerMenu(); } while (addServerMenu == true) { addServerMenu(); } } System.out.println("Thank you for using my program."); System.exit(0); } // Shows the main menu, let the user choose and run methods based on choice. public void mainMenu() throws IOException { // Show the menu System.out.println("-------------------------------------------\\"); System.out.println("Menu /\n----\n\nCurrent server is: " + getCurrentServer() + "\n\nPlease choose an action:\n\ns = Go to WoW server list.\n\nr = Restore realmlist.wtf to original \n using realmlist_backup.wtf. \n\nx = Exit\n"); System.out.println("-------------------------------------------/"); System.out.print("Choose action: "); scanKeyboard(); cls(); System.out.println(""); //Act on menu choice: if (keyInput.equals("s")) { mainMenu = false; serverListMenu = true; } if (keyInput.equals("r")) { System.out.println("Looking for realmlist_backup.wtf..."); if (!fileCheck(wowDir() + "realmlist_backup.wtf")) { System.out.println("Error: realmlist_backup.wtf not found!\n"); } else { System.out.println("realmlist_back.wtf found."); System.out.println("Restoring original realmlist.wtf..."); deleteFile(wowDir() + "realmlist.wtf"); renameFile(wowDir() + "realmlist_backup.wtf", wowDir() + "realmlist.wtf"); System.out.println("Original realmlist.wtf restored succesfully.\n"); } } if (keyInput.equals("x")) { mainMenu = false; } } public void serverListMenu() throws Exception { System.out.println("------------------------------------------------------------\\"); System.out.println("Server List Menu /\n----------------\n\nCurrent server is: " + getCurrentServer() + "\n\nPlease choose which server you want to set WoW to connect to:\n(Will backup original realmlist.wtf to realmlist_backup.wtf).\n\n"); readServerList(); if (serverNamesArray[0] != null) { for (int i=0; serverNamesArray[i] != null; i++) { System.out.println(i + ". " + serverNamesArray[i] + " (" + serverAddressArray[i] + ")"); } }else { System.out.println("|No servers in server list! |\n|Press 'a' followed by enter to add some|"); } System.out.println(""); System.out.println("a = Add a server to the list"); System.out.println("d = Delete a server from the list"); System.out.println("x = Back to main menu."); System.out.println("------------------------------------------------------------/"); System.out.print("Choose server number: "); scanKeyboard(); cls(); System.out.println(""); readServerList(); for (int i=0; serverNamesArray[i] != null; i++) { String iString = Integer.toString(i); if (keyInput.equals(iString)) { changeWoWServer(i); } } if (keyInput.equals("a")){ serverListMenu = false; mainMenu = false; addServerMenu = true; } if (keyInput.equals("d")){ serverListMenu = false; mainMenu = false; deleteServerMenu = true; } if (keyInput.equals("x")) { serverListMenu = false; mainMenu = true; } } public void deleteServerMenu() throws IOException { System.out.println("-----------------------------------------------------\\"); System.out.println("Delete Server Menu /\n-------------------\n\nPlease choose which server you want to delete from the list:\n"); readServerList(); if (serverNamesArray[0] != null) { for (int i=0; serverNamesArray[i] != null; i++) { System.out.println(i + ". " + serverNamesArray[i] + " (" + serverAddressArray[i] + ")"); } }else { System.out.println("No servers in server list!"); } System.out.println(""); System.out.println("x = Back to server list menu."); System.out.println("-----------------------------------------------------/"); System.out.print("Choose server number: "); scanKeyboard(); cls(); System.out.println(""); if (isOnlyNumbers(keyInput)) { int lineToDelete = Integer.parseInt(keyInput); System.out.println("Are you sure you want to add the following server:\n\nName: " + serverNamesArray[lineToDelete] + "\nAddress: " + serverAddressArray[lineToDelete] + "\n\n[y/n] "); scanKeyboard(); cls(); if (keyInput.equals("y")|keyInput.equals("Y")) { deleteFromServerList(lineToDelete); } } if (keyInput.equals("x")){ serverListMenu = true; mainMenu = false; deleteServerMenu = false; } } public void addServerMenu() throws IOException { System.out.println("-----------------------------------------------------\\"); System.out.println("Add Server Menu /\n----------------\n\nPlease write the server you want to add to the list.\n\nType it as [Server Name],[Server Address]\n\nExamples:\nJohn's Server,wowserver.john.com\n or\nUltimate server,83.28.110.98\n\nThe current servers in the list is:\n"); readServerList(); if (serverNamesArray[0] != null) { for (int i=0; serverNamesArray[i] != null; i++) { System.out.println(i + ". " + serverNamesArray[i] + " (" + serverAddressArray[i] + ")"); } }else { System.out.println("No servers in server list yet.\nGo on and write one and press enter."); } System.out.println(""); System.out.println("x = Back to server list menu."); System.out.println("-----------------------------------------------------/"); System.out.print("Write new server: "); scanKeyboard(); cls(); System.out.println(""); if (keyInput.equals("x")){ serverListMenu = true; mainMenu = false; deleteServerMenu = false; addServerMenu = false; keyInput = ""; }else { String serverString = keyInput; //if input contains "," write it to server list if (!(serverString.indexOf(",") == (-1))) { StringTokenizer st = new StringTokenizer(keyInput, ","); String newServerName = st.nextToken(); String newServerAddress = st.nextToken(); System.out.println("Are you sure you want to add the following server:\n\nName: " + newServerName + "\nAddress: " + newServerAddress + "\n\n[y/n] "); scanKeyboard(); cls(); if (keyInput.equals("y")|keyInput.equals("Y")) { writeToServerList(serverString); } }else { System.out.println("Error! your input \"" + serverString + "\" does not contain any \",\"!\n"); } } } public void writeToServerList(String serverString) { try { String realmListText = readTextFile(".\\WoWPrivateServerList.txt").trim(); if (realmListText.equals("")) { writeFile(serverString, ".\\WoWPrivateServerList.txt"); }else { writeFile((realmListText.trim() + newline + serverString), ".\\WoWPrivateServerList.txt"); } } catch (IOException e) { System.out.println("oops, the following error occured:" + e); } } public void deleteFromServerList(int lineNumber) throws IOException { String [] serverListArray = new String [99]; // Read all lines into array, except the line we want to delete System.out.println("Deleteing server: " + serverNamesArray[lineNumber] + " (" + serverAddressArray[lineNumber] + ")"); BufferedReader in = new BufferedReader(new FileReader("WoWPrivateServerList.txt")); for (int i=0; in.ready(); i++) { if (i != lineNumber) { serverListArray[i] = in.readLine(); }else { in.readLine(); } } in.close(); //Wipe file clean writeFile("", "WoWPrivateServerList.txt"); //Write the resulting array back into the file. BufferedWriter out = new BufferedWriter(new FileWriter("WoWPrivateServerList.txt", true)); for (int i=0; i < serverListArray.length; i++) { if (serverListArray[i] != null) { out.write(serverListArray[i] ); out.newLine(); } } out.close(); //The list was made shorter, so we need to flush the arrays containing the list to update them. for (int i=0; i < 98; i++) { serverNamesArray[i] = null; serverAddressArray[i] = null; } System.out.println("Done."); } public String getCurrentServer() throws IOException { String realmListText = readTextFile(wowDir() + "realmlist.wtf"); int addressStartIndex = realmListText.lastIndexOf("set realmlist " , realmListText.length()); int addressEndIndex = realmListText.indexOf("\n" , addressStartIndex); String currentServer = realmListText.substring((addressStartIndex + 14), addressEndIndex); return currentServer; } public void changeWoWServer(int serverListNumber) { String wowDirectory = wowDir(); if (!fileCheck(wowDirectory + "realmlist_backup.wtf")) { System.out.println("Making backup of realmlist.wtf..."); renameFile(wowDir() + "realmlist.wtf",wowDir() + "realmlist_backup.wtf"); System.out.println("Done.\n"); } else { System.out.println("- realmlist_backup.wtf already exists, skipping backup...\n"); } System.out.println("Changing realmlist.wtf to \"set realmlist " + serverAddressArray[serverListNumber] + "\""); writeFile("set realmlist " + serverAddressArray[serverListNumber] + newline, wowDir() + "realmlist.wtf"); System.out.println("Done.\n"); } // Overwrites a file "filename" with a new file which contains "input". private void writeFile(String input, String fileName) { try { BufferedWriter out = new BufferedWriter(new FileWriter(fileName, false)); out.write(input); out.close(); } catch (IOException e){ } } /*/ Overwrites a file "filename" with a new file which contains "input". private static void appendToFile(String input, String fileName) { try { BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true)); out.write(input); out.newLine(); out.close(); } catch (IOException e){ } } */ // Reads the list of servers from "WoWPrivateServerList.txt". // Reads "Server name, Server address" on each line. // In this example serverNamesArray[0] will contain "Server Name" and // serverAddressArray[0] will contain "Server address". public void readServerList() { String serverList = new String(); try { BufferedReader in = new BufferedReader(new FileReader("WoWPrivateServerList.txt")); // read the first record of the database for (int i=0; (serverList = in.readLine()) != null; i++) { if (!(serverList.indexOf(",") == (-1))) { StringTokenizer st = new StringTokenizer(serverList, ","); serverNamesArray[i] = st.nextToken(); serverAddressArray[i] = st.nextToken(); } } } catch (IOException e) { // catch io errors from FileInputStream or readLine() System.out.println("Uh oh, got an IOException error: " + e.getMessage()); } } // Gets World of Warcraft directory from the Windows Registry Database (via reg.exe) and replaces all \ with \\ public String wowDir() { RegistryFetcher rf = new RegistryFetcher(); String wowDir = rf.getWoWFolderPath().replaceAll("\\\\", "\\\\\\\\"); // This replaces all \ with \\ return wowDir; } // checks if a file "filepath" exists, returns true if it does. public boolean fileCheck(String filePath) { File f = new File(filePath); return f.exists(); } // copies a file "inputfilename" to "outputfilename" public void copyFile(String inputFilename, String outputFilename) throws IOException { File inputFile = new File(inputFilename); File outputFile = new File(outputFilename); FileReader in = new FileReader(inputFile); FileWriter out = new FileWriter(outputFile); int c; while ((c = in.read()) != -1) out.write(c); in.close(); out.close(); } // renames a file "inputFilename" to "outputFilename" public void renameFile(String inputFilename, String outputFilename) { // Construct the file object. Does NOT create a file on disk! File f = new File(inputFilename); // Renames the inputFilename to outputFilename. (fx test.txt to test.txt.bak) // Renaming requires a File object for the target. f.renameTo(new File(outputFilename)); } public void deleteFile(String fileName) { String fileNameStripped = fileName.replaceAll("\\\\\\\\", "\\\\"); try { // Construct a File object for the file to be deleted. File target = new File(fileName); if (!target.exists()) { System.err.println("File " + fileNameStripped + " not found!"); return; } // Quick, now, delete it immediately: if (!target.delete()) System.err.println("Failed to delete " + fileNameStripped); return; } catch (SecurityException e) { System.err.println("Unable to delete " + fileNameStripped + "(" + e.getMessage() + ")"); } } public String readTextFile(String fullPathFilename) throws IOException { StringBuffer sb = new StringBuffer(8192); BufferedReader reader = new BufferedReader(new FileReader(fullPathFilename)); char[] chars = new char[8192]; while( (reader.read(chars)) > -1){ sb.append(String.valueOf(chars)); } reader.close(); return sb.toString(); } public boolean isOnlyNumbers(String s) { int i = 0; for (int j = 0;j < s.length();j++) { if (!Character.isDigit(s.charAt(j))) i++; } if (i==0) { return true; }else{ return false; } } // prompts the user for keyboard input and returns that input public void scanKeyboard() { Scanner scan = new Scanner(System.in); keyInput = scan.nextLine(); } public void cls() throws IOException { for (int i = 0; i < 100; i++) System.out.println(); } }