Index154

Spelunky 2 Biome Skin Randomizer

May 14th, 2021 (edited)
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.76 KB | None | 0 0
  1. package randomizer;
  2.  
  3. import java.awt.Graphics;
  4. import java.awt.image.BufferedImage;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.Random;
  10. import java.util.Scanner;
  11.  
  12. import javax.imageio.ImageIO;
  13.  
  14. import org.apache.commons.io.FileUtils;
  15.  
  16. // Alternate version of the program that moves everything right into the pack folder and deletes the previous mod!
  17. public class RunAndMove {
  18.    
  19.     // Set filepath
  20.     static String path = "E:\\User\\Documents\\Game Stuff\\Spelunky 2\\Randomizing\\Biome skins";
  21.     static String dest_path = "D:\\Program Files (x86)\\Steam\\steamapps\\common\\Spelunky 2\\Mods\\Packs\\Cool_tilesets";
  22.     static String alreadyUsed = "";
  23.     static String blockedSkins = "";
  24.    
  25.     public static void main(String[] args) throws IOException {
  26.        
  27.         // Clean the output folder first
  28.         System.out.println("Cleaning pack folder...\n");
  29.         File directory = new File(dest_path);
  30.         FileUtils.cleanDirectory(directory);
  31.        
  32.         // Read the list of tilesets
  33.         // Create array
  34.         List<String> skinGroups = new ArrayList<String>();
  35.        
  36.         //Start the scanner
  37.         File f = new File(path + "\\source_files\\folder_list.txt");
  38.         Scanner s = new Scanner(f);
  39.         System.out.println("Reading folder list...\n");
  40.        
  41.         //Go through the lines and save them into the array
  42.         while(s.hasNext()) {
  43.             String line = s.nextLine();
  44.             if(!line.substring(0,2).equals("//")){
  45.                 skinGroups.add(line);
  46.             }
  47.         }
  48.         s.close();
  49.        
  50.         // Read the list of globally blocked skins
  51.         //Start the scanner
  52.         File fNew = new File(path + "\\source_files\\blacklist.txt");
  53.         Scanner sNew = new Scanner(fNew);
  54.         System.out.println("Reading blacklist...");
  55.        
  56.         //Go through the lines and save them into the array
  57.         while(sNew.hasNext()) {
  58.             String lineNew = sNew.nextLine();
  59.             if(!lineNew.substring(0,2).equals("//")){
  60.                 blockedSkins = blockedSkins + "," + lineNew + ",";
  61.             }
  62.         }
  63.         sNew.close();
  64.         System.out.println("Skin blacklist:\n" + blockedSkins + "\n");
  65.        
  66.        
  67.         // Call the actual function once for each skin pack
  68.         for(int x = 0; x < skinGroups.size(); x++) {
  69.             randomize(skinGroups.get(x));
  70.         }
  71.        
  72.         System.out.println("\nCompletely done!");
  73.        
  74.     }
  75.    
  76.     public static void combineImages(File source, File dest) throws IOException {
  77.         // Load source images
  78.         BufferedImage base = ImageIO.read(dest);
  79.         BufferedImage overlay = ImageIO.read(source);
  80.  
  81.         // Create the new image, canvas size is the max. of both image sizes
  82.         int w = Math.max(base.getWidth(), overlay.getWidth());
  83.         int h = Math.max(base.getHeight(), overlay.getHeight());
  84.         BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
  85.  
  86.         // Paint both images, preserving the alpha channels
  87.         Graphics g = combined.getGraphics();
  88.         g.drawImage(base, 0, 0, null);
  89.         g.drawImage(overlay, 0, 0, null);
  90.         g.dispose();
  91.  
  92.         // Overwrite the first image (used in output)
  93.         ImageIO.write(combined, "PNG", dest);
  94.     }
  95.    
  96.     // Function that basically does everything
  97.     public static void randomize(String biomeName) throws IOException {
  98.        
  99.         // Create skins array
  100.         List<String> skinList = new ArrayList<String>();
  101.        
  102.         //Start the scanner with our specified folder path from the main
  103.         File f2 = new File(path + "\\source_files\\" + biomeName + "_skins\\skin_list.txt");
  104.         Scanner s2 = new Scanner(f2);
  105.        
  106.         //Go through the lines and save them into the array unless they are commented out or have already been used or are on the blocklist
  107.         while(s2.hasNext()) {
  108.             String line2 = s2.nextLine();
  109.             if(!line2.substring(0,2).equals("//") && !alreadyUsed.contains("," + line2 + ",") && !blockedSkins.contains("," + line2 + ",")){
  110.                 skinList.add(line2);
  111.             }
  112.         }
  113.         s2.close();
  114.         System.out.println("\n" + biomeName + " skin list:\n" + skinList);
  115.        
  116.        
  117.         // Create file names array
  118.         List<String> targetFiles = new ArrayList<String>();
  119.        
  120.         //Start the scanner with our specified folder path from the main
  121.         File f3 = new File(path + "\\source_files\\" + biomeName + "_skins\\target_filenames.txt");
  122.         Scanner s3 = new Scanner(f3);
  123.        
  124.         //Go through the lines and save them into the array unless they are commented out
  125.         while(s3.hasNext()) {
  126.             String line3 = s3.nextLine();
  127.             if(!line3.substring(0,2).equals("//")){
  128.                 targetFiles.add(line3);
  129.             }
  130.         }
  131.         s3.close();
  132.        
  133.        
  134.         // Pick random skin from the first array
  135.         Random random = new Random();
  136.         String chosenSkin = skinList.get(random.nextInt(skinList.size()));
  137.        
  138.         // Save the chosen skin to the list of already used ones
  139.         alreadyUsed = alreadyUsed + "," + chosenSkin + ",";
  140.        
  141.         // Copy the correct files to the output folder
  142.         System.out.println("Copying " + biomeName + " files...");
  143.         for(int i = 0; i < targetFiles.size(); i++) {
  144.             // If the filename needs to be exact then do it differently!
  145.             if(targetFiles.get(i).contains("exact=")) {
  146.                 // Modify the filename first
  147.                 File source = new File(path + "\\source_files\\" + biomeName + "_skins\\" + chosenSkin + "\\" + targetFiles.get(i).substring(6) + ".png");
  148.                 File dest = new File(dest_path + "\\" + targetFiles.get(i).substring(6) + ".png");
  149.                
  150.                 // If the destination file already exists then combine it with the new one
  151.                 if(dest.isFile()) {
  152.                     combineImages(source, dest);
  153.                 } else {
  154.                     FileUtils.copyFile(source, dest);
  155.                 }
  156.                
  157.             } else {
  158.                 File source = new File(path + "\\source_files\\" + biomeName + "_skins\\" + chosenSkin + "\\" + targetFiles.get(i) + chosenSkin + ".png");
  159.                 File dest = new File(dest_path + "\\" + targetFiles.get(i) + biomeName + ".png");
  160.                
  161.                 // If the destination file already exists, combine it with the new one
  162.                 if(dest.isFile()) {
  163.                     combineImages(source, dest);
  164.                 } else {
  165.                     FileUtils.copyFile(source, dest);
  166.                 }
  167.             }
  168.            
  169.         }
  170.        
  171.     }
  172.    
  173. }
Add Comment
Please, Sign In to add comment