PersonTheCat

Untitled

Apr 23rd, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.10 KB | None | 0 0
  1. package personthecat.modpackfixer;
  2.  
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.nio.charset.StandardCharsets;
  7. import java.nio.file.Files;
  8. import java.nio.file.Path;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import java.util.stream.Stream;
  12.  
  13. public class Main
  14. {
  15.     static String path = System.getProperty("user.dir");
  16.     static String filesToFixPath = path + "/assets/files to fix/";
  17.     static File oldLangFile = new File(path + "/assets/old.lang");
  18.     static File newLangFile = new File(path + "/assets/new.lang");
  19.     static List<String> newLines = new ArrayList<>();
  20.    
  21.     public static void main(String[] args)
  22.     {
  23.         createKeyFile();
  24.         fixAllFiles();
  25.     }
  26.    
  27.     private static void createKeyFile()
  28.     {
  29.         File keyFile = new File(path + "/assets/keys.txt");
  30.        
  31.         try
  32.         {
  33.             keyFile.createNewFile();
  34.         }
  35.        
  36.         catch (IOException e) {System.err.println("Could not create new file.");}
  37.        
  38.         String oldLangKeys = reformatString(getFullContent(oldLangFile));
  39.         String newLangKeys = reformatString(getFullContent(newLangFile));
  40.        
  41.         String[] oldLangLines = ignoreComments(oldLangKeys.split(System.getProperty("line.separator")));
  42.         String[] newLangLines = ignoreComments(newLangKeys.split(System.getProperty("line.separator")));
  43.  
  44.         addStringsToArray(oldLangLines, newLines, 1); //Add translation.
  45.         addStringsToArray(oldLangLines, newLines, 0); //Add original keys.
  46.        
  47.         removeMatchlessAndNoneLines();
  48.         removeDuplicateLines();
  49.        
  50.         addNewNamesToArrayElements(newLangLines);
  51.        
  52.         writeLinesToFile(keyFile, newLines.toArray(new String[newLines.size()]));
  53.     }
  54.    
  55.     private static void fixAllFiles()
  56.     {
  57.         System.out.println("Preparing to fix all files...");
  58.  
  59.         try
  60.         {
  61.             Stream<Path> files = Files.walk(new File(filesToFixPath).toPath());
  62.            
  63.             files.forEach(path ->
  64.             {
  65.                 File file = path.toFile();
  66.                
  67.                 if (file.isFile() && !file.toString().contains(".nbt") && !file.toString().contains(".rcst"))
  68.                 {
  69.                     System.out.println("Fixing this file: " + file);
  70.                    
  71.                     String content = getFullContent(file);
  72.                    
  73.                     for (String s : newLines)
  74.                     {
  75.                         String[] split = s.split(",");
  76.                        
  77.                         if (split.length == 3)
  78.                         {
  79.                             content = content.replaceAll(split[1], split[2]);
  80.                         }                  
  81.                     }
  82.                    
  83.                     writeLinesToFile(file, content);
  84.                 }
  85.             });
  86.            
  87.             files.close();
  88.         }
  89.        
  90.         catch (IOException e) {}
  91.        
  92.         System.out.println("Done fixing files.");
  93.     }
  94.    
  95.     private static void addNewNamesToArrayElements(String[] newElements)
  96.     {
  97.         for (int i = 0; i < newLines.size(); i++)
  98.         {
  99.             String x = newLines.get(i);
  100.             String existingTranslationKey = x.split(",")[0];
  101.  
  102.             for (String y : newElements)
  103.             {
  104.                 String newRegistryName = y.split("=")[0];
  105.                 String translationKey = y.split("=")[1];
  106.                
  107.                 if (existingTranslationKey.equals(translationKey) && x.split(",").length < 4)
  108.                 {                  
  109.                     System.out.println("Finalizing this line: " + x);
  110.                    
  111.                     newLines.set(i, x + newRegistryName + ",");
  112.                 }
  113.                
  114.                 if (x.split(",").length >= 4) System.out.println("Found a multiple entries for " + translationKey);
  115.             }
  116.         }
  117.     }
  118.    
  119.     private static String getFullContent(File file)
  120.     {
  121.         String fullContent = null;
  122.        
  123.         try
  124.         {
  125.             fullContent = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
  126.         }
  127.        
  128.         catch (IOException e) {System.err.println("Could not load file: " + path);}
  129.        
  130.         return fullContent;
  131.     }
  132.    
  133.     private static String reformatString(String input)
  134.     {
  135.         input = input.replaceAll("tile.", "").replaceAll(".name", "").replaceAll("item.", "");
  136.        
  137.         input = input.replaceAll(".alpha", ":0");
  138.         input = input.replaceAll(".bravo", ":1");
  139.         input = input.replaceAll(".charlie", ":2");
  140.         input = input.replaceAll(".delta", ":3");
  141.         input = input.replaceAll(".echo", ":4");
  142.         input = input.replaceAll(".fox", ":5");
  143.         input = input.replaceAll(".golf", ":6");
  144.         input = input.replaceAll(".hotel", ":7");
  145.         input = input.replaceAll(".india", ":8");
  146.         input = input.replaceAll(".juliet", ":9");
  147.         input = input.replaceAll(".kilo", ":10");
  148.         input = input.replaceAll(".lima", ":11");
  149.         input = input.replaceAll(".mike", ":12");
  150.         input = input.replaceAll(".november", ":13");
  151.         input = input.replaceAll(".oscar", ":14");
  152.         input = input.replaceAll(".papa", ":15");
  153.        
  154.         return input;
  155.     }
  156.    
  157.     private static void addStringsToArray(String[] strings, List<String> array, int index)
  158.     {
  159.         for (int i = 0; i < strings.length; i++)
  160.         {
  161.             String currentLine = strings[i].split("=")[index];
  162.            
  163.             try
  164.             {
  165.                 array.set(i, array.get(i) + currentLine + ",");
  166.             }
  167.            
  168.             catch (IndexOutOfBoundsException e) {array.add(currentLine + ",");}
  169.         }
  170.     }
  171.    
  172.     private static void writeLinesToFile(File file, String... lines)
  173.     {
  174.         try
  175.         {
  176.             FileWriter writer = new FileWriter(file);
  177.            
  178.             for (String line : lines)
  179.             {
  180.                 if (!line.startsWith("none"))
  181.                 {
  182.                     writer.write(line + System.getProperty("line.separator")); 
  183.                 }              
  184.             }
  185.            
  186.             writer.close();
  187.         }
  188.        
  189.         catch (IOException e) {System.err.println("Could not write new file");}
  190.     }
  191.    
  192.     private static String[] ignoreComments(String[] input)
  193.     {
  194.         List<String> newList = new ArrayList<>();
  195.        
  196.         for (String s : input)
  197.         {
  198.             if (s.contains("=") && s.split("=").length > 1)
  199.             {
  200.                 newList.add(s);
  201.             }
  202.         }
  203.        
  204.         return newList.toArray(new String[newList.size()]);
  205.     }
  206.    
  207.     private static void removeMatchlessAndNoneLines()
  208.     {
  209.         List<String> newList = new ArrayList<>();
  210.        
  211.         for (String s : newLines)
  212.         {
  213.             if (s.split(",").length > 1 && !s.startsWith("none")) newList.add(s);
  214.         }
  215.        
  216.         newLines = newList;
  217.     }
  218.    
  219.     private static void removeDuplicateLines()
  220.     {
  221.         List<String> newList = new ArrayList<>();
  222.        
  223.         for (int i = 0; i < newLines.size(); i++)
  224.         {
  225.             boolean canAdd = true;
  226.            
  227.             for (int j = 0; j < newLines.size(); j++)
  228.             {
  229.                 if (!newLines.get(i).equals(newLines.get(j)))
  230.                 {                  
  231.                     if (newLines.get(i).split(",")[0].equals(newLines.get(j).split(",")[0]))
  232.                     {
  233.                         canAdd = false;
  234.                     }
  235.                 }
  236.             }
  237.            
  238.             if (canAdd) newList.add(newLines.get(i));
  239.         }
  240.        
  241.         newLines = newList;
  242.     }
  243. }
Advertisement
Add Comment
Please, Sign In to add comment