Advertisement
Foxscotch

Untitled

Nov 23rd, 2015
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.22 KB | None | 0 0
  1. package main;
  2.  
  3.  
  4. import java.util.*;
  5. import java.util.regex.*;
  6. import java.util.zip.*;
  7. import java.io.*;
  8. import java.nio.file.*;
  9. import java.nio.file.attribute.*;
  10.  
  11. import com.google.gson.*;
  12.  
  13.  
  14. public class CommandFinder {
  15.     /* JSON should look roughly like this:
  16.      *
  17.      * {
  18.      *   "Addon_Name": {
  19.      *     "filename": {
  20.      *       "cmdName": [
  21.      *         "argument1",
  22.      *         "argument2"
  23.      *       ]
  24.      *     }
  25.      *   }
  26.      * }
  27.      *
  28.      */
  29.    
  30.    
  31.     HashMap<String, HashMap<String, HashMap<String, ArrayList<String>>>> output = new HashMap<>();
  32.    
  33.     static String regexPattern= "\\s*function serverCmd([\\w\\d]+)\\s*\\(([%\\w\\d,\\s]+)*\\)";
  34.     static Pattern regex = Pattern.compile(regexPattern, Pattern.CASE_INSENSITIVE);
  35.    
  36.     Path path;
  37.     ArrayList<String> fileList = new ArrayList<String>();
  38.    
  39.     static Gson json = new GsonBuilder().setPrettyPrinting().create();
  40.    
  41.    
  42.     public CommandFinder(String addonPath) {
  43.         path = Paths.get(addonPath);
  44.        
  45.         try {
  46.             Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
  47.                 @Override
  48.                 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
  49.                     if (file.toString().endsWith(".zip")) {
  50.                         fileList.add(file.toString());
  51.                     }                  
  52.                     return FileVisitResult.CONTINUE;
  53.                 }
  54.             });
  55.         }
  56.         catch (IOException e) {
  57.             System.out.println("Something totally bad just happened: " + e.getMessage());
  58.         }
  59.     }
  60.    
  61.     public void searchFiles() {
  62.         for (String fileName : fileList) {
  63.             try {
  64.                 ZipFile zip = new ZipFile(fileName);
  65.                 Enumeration<? extends ZipEntry> entries = zip.entries();
  66.                
  67.                 String addOnName = fileName.substring(fileName.lastIndexOf("\\") + 1);
  68.                 addOnName = addOnName.substring(0, addOnName.length() - 4);
  69.                 HashMap<String, HashMap<String, ArrayList<String>>> addOnMap = new HashMap<>();
  70.                 output.put(addOnName, addOnMap);
  71.                
  72.                 while (entries.hasMoreElements()) {
  73.                     try {
  74.                         ZipEntry entry = entries.nextElement();
  75.                         String entryName = entry.getName();
  76.                         if (entryName.endsWith(".cs")) {
  77.                             InputStream zipIn = zip.getInputStream(entry);
  78.                             BufferedReader reader = new BufferedReader(new InputStreamReader(zipIn, "UTF-8"));
  79.                            
  80.                             HashMap<String, ArrayList<String>> fileMap = new HashMap<>();
  81.                             addOnMap.put(entryName.substring(0, entryName.length() - 3), fileMap);
  82.                            
  83.                             String line = null;
  84.                             while ((line = reader.readLine()) != null) {
  85.                                 ArrayList<String> argList = new ArrayList<>();
  86.                                 Matcher matcher = regex.matcher(line);
  87.                                
  88.                                 if (matcher.matches()) {
  89.                                     String cmdName = matcher.group(1);
  90.                                     fileMap.put(cmdName, argList);
  91.                                    
  92.                                     for (String argument : matcher.group(2).split(",")) {
  93.                                         argList.add(argument.trim());
  94.                                     }
  95.                                 }
  96.                             }
  97.                            
  98.                            
  99.                             reader.close();
  100.                             zipIn.close();
  101.                         }
  102.                     }
  103.                     catch (ZipException e) {
  104.                         System.out.println("Error with zip entry: " + e.getMessage());
  105.                     }
  106.                 }
  107.                
  108.                 zip.close();
  109.             }
  110.             catch (ZipException e) {
  111.                 System.out.format("Error opening zip file %s: %s",
  112.                         fileName.substring(fileName.lastIndexOf("\\") + 1),
  113.                         e.getMessage());
  114.                 System.out.println();
  115.             }
  116.             catch (IOException e) {
  117.                 System.out.format("Something bad happened: %s: %s",
  118.                         fileName.substring(fileName.lastIndexOf("\\") + 1),
  119.                         e.getMessage());
  120.                 System.out.println();
  121.             }
  122.         }
  123.     }
  124.    
  125.     public void cleanOutput() {
  126.         for (String addOnName : output.keySet()) {
  127.             HashMap<String, HashMap<String, ArrayList<String>>> addOnMap = output.get(addOnName);
  128.            
  129.             Iterator<String> addOnIter = addOnMap.keySet().iterator();
  130.             while (addOnIter.hasNext()) {
  131.                 String fileName = addOnIter.next();
  132.                
  133.                 HashMap<String, ArrayList<String>> fileMap = addOnMap.get(fileName);
  134.                
  135.                 Iterator<String> fileIter = fileMap.keySet().iterator();
  136.                 while (fileIter.hasNext()) {
  137.                     String command = fileIter.next();
  138.                     ArrayList<String> cmdList = fileMap.get(command);
  139.                     if (cmdList.isEmpty())
  140.                         fileIter.remove();
  141.                 }
  142.                
  143.                 if (fileMap.isEmpty())
  144.                     addOnIter.remove();
  145.             }
  146.         }
  147.        
  148.     }
  149.    
  150.     public void writeJson() {
  151.         try {
  152.             Writer writer = new FileWriter("server_commands.json");
  153.             json.toJson(output, writer);
  154.             writer.close();
  155.         }
  156.         catch (IOException e) {
  157.             System.out.println("Couldn't write the JSON file, for some reason: " + e.getMessage());
  158.             e.printStackTrace();
  159.         }
  160.     }
  161.    
  162.     public static void main(String[] args) {
  163.         double originalTime = System.nanoTime() / 1000000000.0;
  164.        
  165.         CommandFinder cmds = new CommandFinder(System.getProperty("user.dir") + "/Add-Ons");
  166.         cmds.searchFiles();
  167.         cmds.cleanOutput();
  168.         cmds.writeJson();
  169.        
  170.         double newTime = System.nanoTime() / 1000000000.0;
  171.         double difference = newTime - originalTime;
  172.        
  173.         System.out.println("Results can be found in server_commands.json.");
  174.         System.out.println("Time taken: " + difference);
  175.         System.out.print("Press enter to exit.");
  176.         try {
  177.             System.in.read();
  178.         }
  179.         catch (IOException e) {
  180.             System.out.println("For some reason, trying to read input did not work...");
  181.             System.out.println(e.getMessage());
  182.         }
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement