Suyo

PDDLC SLOT CHANGER v0.5

Mar 10th, 2012
845
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.83 KB | None | 0 0
  1. /* PDDLC SLOT CHANGER v0.5
  2.  * Written by Suyo (ProjectDIVA.net)
  3.  *
  4.  * This program allows users to change the slots of decrypted DLC
  5.  * for Hatsune Miku: Project DIVA 2nd, Hatsune Miku: Project DIVA
  6.  * 2nd# and Hatsune Miku: Project DIVA extend.
  7.  *
  8.  * Currently supported: Songs, Modules, Wall Objects
  9.  *
  10.  * This program is free software. It comes without any warranty, to
  11.  * the extent permitted by applicable law. You can redistribute it
  12.  * and/or modify it under the terms of the Do What The Fuck You Want
  13.  * To Public License, Version 2, as published by Sam Hocevar. See
  14.  * http://sam.zoy.org/wtfpl/COPYING for more details. */
  15.  
  16. package net.projectdiva.slotchange;
  17. import java.awt.Color;
  18. import java.awt.event.ActionEvent;
  19. import java.awt.event.ActionListener;
  20. import java.io.DataOutputStream;
  21. import java.io.File;
  22. import java.io.FileInputStream;
  23. import java.io.FileOutputStream;
  24. import java.util.ArrayList;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.Map;
  28.  
  29. import javax.swing.JButton;
  30. import javax.swing.JFileChooser;
  31. import javax.swing.JFrame;
  32. import javax.swing.JLabel;
  33. import javax.swing.JTextField;
  34. import javax.swing.event.DocumentEvent;
  35. import javax.swing.event.DocumentListener;
  36. import javax.swing.text.AttributeSet;
  37. import javax.swing.text.BadLocationException;
  38. import javax.swing.text.Document;
  39. import javax.swing.text.PlainDocument;
  40.  
  41. public class SlotChanger {
  42.  
  43.     public static Map<Character,List<Object>> dlctypes = new HashMap<Character,List<Object>>();
  44.    
  45.     public static char dlctype = '-';
  46.     public static int currentslot = 0;
  47.     public static String currentname = "";
  48.     public int chosenslot = 0;
  49.     public JFrame fenster;
  50.    
  51.     public static int[] fileArray;
  52.     public static File ifile;
  53.    
  54.     public JButton button1;
  55.     public JButton button2;
  56.     public JTextField text1;
  57.     public IntTextField text2;
  58.     public JTextField text3;
  59.     public JLabel label1;
  60.     public JLabel label2;
  61.     public JLabel label3;
  62.     public JLabel label4;
  63.     public JLabel label5;
  64.     public JLabel label6;
  65.     public JLabel label7;
  66.     public JLabel label8;
  67.    
  68.     public SlotChanger() {
  69.         fenster = new JFrame("PD Slot Changer v0.5");
  70.         fenster.setSize(300,300);
  71.         fenster.setLocation(300,300);
  72.         fenster.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  73.         fenster.setLayout(null);
  74.        
  75.         button1 = new JButton("Open DLC");
  76.         button2 = new JButton("Process!");
  77.         text1 = new JTextField();
  78.         text2 = new IntTextField(chosenslot,2);
  79.         text3 = new JTextField();
  80.         label1 = new JLabel();
  81.         label2 = new JLabel();
  82.         label3 = new JLabel();
  83.         label4 = new JLabel();
  84.         label5 = new JLabel();
  85.         label6 = new JLabel();
  86.         label7 = new JLabel();
  87.         label8 = new JLabel();
  88.  
  89.         button1.setBounds(5,5,100,30);
  90.         button1.addActionListener(new ActionListener() {
  91.             public void actionPerformed(ActionEvent arg0) {
  92.                 final JFileChooser fc = new JFileChooser();
  93.                 int returnVal = fc.showOpenDialog(fc);
  94.                 if (returnVal == JFileChooser.APPROVE_OPTION) {
  95.                     label8.setText("Loading... please wait!");
  96.                     label8.setVisible(true);
  97.                     fenster.paint(fenster.getGraphics());
  98.                     ifile = fc.getSelectedFile();
  99.                     if (readFile(ifile)) {
  100.                         text1.setText(currentname);
  101.                         text2.setValue(currentslot);
  102.                         label4.setText("Current Slot: "+currentslot);
  103.                         String printdlc = "unknown";
  104.                         if (dlctypes.get(dlctype) != null) printdlc = (String) dlctypes.get(dlctype).get(0);
  105.                         label5.setText("DLC Type: "+dlctype+" ("+printdlc+")");
  106.                         label6.setText("DLC Size: "+ifile.length()+" bytes");
  107.                         button2.setEnabled(true);
  108.                         label8.setText("Done!");
  109.                     } else {
  110.                         button2.setEnabled(false);
  111.                         label8.setText("There was an error...");
  112.                     }
  113.                 } else {
  114.                     return;
  115.                 }
  116.             }
  117.         });
  118.         button2.setBounds(187,5,100,30);
  119.         button2.addActionListener(new ActionListener() {
  120.             public void actionPerformed(ActionEvent arg0) {
  121.                 label8.setText("Processing... please wait!");
  122.                 label8.setVisible(true);
  123.                 fenster.paint(fenster.getGraphics());
  124.                 if (changeSlot(text3.getText(),text2.getValue())) {
  125.                     label8.setText("Done!");
  126.                 } else {
  127.                     label8.setText("There was an error...");
  128.                 }
  129.             }
  130.         });
  131.         button2.setEnabled(false);
  132.         text1.setText(currentname);
  133.         text1.setBounds(105,60,182,25);
  134.         text2.setValue(currentslot);
  135.         text2.setBounds(105,110,182,25);
  136.         text2.getDocument().addDocumentListener(new DocumentListener() {
  137.           public void changedUpdate(DocumentEvent e) {
  138.             update();
  139.           }
  140.           public void removeUpdate(DocumentEvent e) {
  141.             update();
  142.           }
  143.           public void insertUpdate(DocumentEvent e) {
  144.             update();
  145.           }
  146.  
  147.           public void update() {
  148.               String fileslot=text2.getText();
  149.               while (fileslot.length()<3) fileslot="0"+fileslot;
  150.               text3.setText(fileslot+".EDAT");
  151.              
  152.               int slot = text2.getValue();
  153.               if (slot<1) {
  154.                   label8.setText("WARNING: Slots < 1 will not work!");
  155.                   label8.setVisible(true);
  156.               } else if (slot > Integer.parseInt(dlctypes.get(dlctype).get(4).toString())) {
  157.                   label8.setText("WARNING: Slots > "+dlctypes.get(dlctype).get(4)+" will not work!");
  158.                   label8.setVisible(true);
  159.               } else if (slot > Integer.parseInt(dlctypes.get(dlctype).get(5).toString())) {
  160.                   label8.setText("WARNING: Slots > "+dlctypes.get(dlctype).get(5)+" will only work on 2nd!");
  161.                   label8.setVisible(true);
  162.               } else label8.setVisible(false);
  163.           }
  164.         });
  165.         String fileslot=new Integer(currentslot).toString();
  166.         while (fileslot.length()<3) fileslot="0"+fileslot;
  167.         text3.setText(fileslot+".EDAT");
  168.         text3.setBounds(105,160,182,25);
  169.        
  170.         label1.setText("Name:");
  171.         label1.setBounds(5,60,100,25);
  172.         label2.setText("(name changing coming soon)");
  173.         label2.setBounds(105,85,182,25);
  174.         label3.setText("Slot:");
  175.         label3.setBounds(5,110,100,25);
  176.         label4.setText("Current Slot: "+currentslot);
  177.         label4.setBounds(105,135,182,25);
  178.         label7.setText("Output as:");
  179.         label7.setBounds(5,160,100,25);
  180.        
  181.         String printdlc = "unknown";
  182.         if (dlctypes.get(dlctype) != null) printdlc = (String) dlctypes.get(dlctype).get(0);
  183.         label5.setText("DLC Type: "+dlctype+" ("+printdlc+")");
  184.         label5.setBounds(5,222,282,25);
  185.         label6.setText("DLC Size: - bytes");
  186.         label6.setBounds(5,242,282,25);
  187.        
  188.         label8.setText("WARNING:");
  189.         label8.setForeground(Color.red);
  190.         label8.setVisible(false);
  191.         label8.setBounds(5,200,282,25);
  192.  
  193.         fenster.getContentPane().add(button1);
  194.         fenster.getContentPane().add(button2);
  195.         fenster.getContentPane().add(text1);
  196.         fenster.getContentPane().add(text2);
  197.         fenster.getContentPane().add(text3);
  198.         fenster.getContentPane().add(label1);
  199.         fenster.getContentPane().add(label2);
  200.         fenster.getContentPane().add(label3);
  201.         fenster.getContentPane().add(label4);
  202.         fenster.getContentPane().add(label5);
  203.         fenster.getContentPane().add(label6);
  204.         fenster.getContentPane().add(label7);
  205.         fenster.getContentPane().add(label8);
  206.  
  207.         //fenster.pack();
  208.         fenster.setVisible(true);
  209.     }
  210.    
  211.     /**
  212.      * @param args
  213.      */
  214.     public static void main(String[] args) {
  215.         System.out.println("### PDDLC SLOT CHANGER v0.5");
  216.         System.out.println("Written by Suyo (ProjectDIVA.net)");
  217.         System.out.println("----------------------------------------");
  218.        
  219.         System.out.println("Initializing Variables...");
  220.         // The offsets are actually not necessary, but it maybe be useful in the future.
  221.         List<Object> temp = new ArrayList<Object>();
  222.         temp.add("Song");       // DLC Type Name
  223.         temp.add(0x1800);       // DLC Type Offset
  224.         temp.add(0x1803);       // Slot Offset
  225.         temp.add(0x2000);       // Name Offset
  226.         temp.add(25);           // Max Slot 2nd
  227.         temp.add(25);           // Max Slot extend
  228.         dlctypes.put('B',temp);
  229.        
  230.         temp = new ArrayList<Object>();
  231.         temp.add("Module");
  232.         temp.add(0x1800);
  233.         temp.add(0x1803);
  234.         temp.add(0x2000);
  235.         temp.add(89);
  236.         temp.add(50);
  237.         dlctypes.put('C',temp);
  238.        
  239.         temp = new ArrayList<Object>();
  240.         temp.add("Wall Object");
  241.         temp.add(0x1800);
  242.         temp.add(0x1803);
  243.         temp.add(0x2000);
  244.         temp.add(25);
  245.         temp.add(25);
  246.         dlctypes.put('H',temp);
  247.        
  248.         System.out.println("\nInitializing GUI...");
  249.         SlotChanger s = new SlotChanger();
  250.         s.button1.getActionListeners()[0].actionPerformed(null);
  251.     }
  252.    
  253.     public static boolean readFile(File ifile) {
  254.         FileInputStream fis;
  255.        
  256.         if (!ifile.exists() || !ifile.isFile()) {
  257.             System.out.println(ifile.getName()+": does not exist!");
  258.             return false;
  259.         }
  260.        
  261.         try {
  262.             System.out.println("Opening "+ifile.getName()+"...");
  263.             fis = new FileInputStream(ifile);
  264.             fileArray = new int[(int) ifile.length()];
  265.          
  266.             int i=0;
  267.             int offs=0;
  268.          
  269.             System.out.println("Reading file... this could take a while.");
  270.             while(i != -1) { /* -1 is a constant for 'EOF' : end of file. */
  271.                 i=fis.read();
  272.                 if (i!=-1) fileArray[offs] = i;
  273.                 offs++;
  274.             }
  275.             fis.close();
  276.         } catch (Exception e) {
  277.             System.out.println(ifile.getName()+": Error while opening!");
  278.             e.printStackTrace();
  279.             return false;
  280.         }
  281.        
  282.         int off;
  283.         dlctype='-';
  284.         for (Map.Entry<Character,List<Object>> entry : dlctypes.entrySet()) {
  285.             off = Integer.parseInt(entry.getValue().get(1).toString());
  286.             if ((char) fileArray[off] == entry.getKey()) {
  287.                 dlctype = entry.getKey();
  288.                 break;
  289.             }
  290.         }
  291.         if (dlctype=='-') {
  292.             System.out.println("Unknown DLC Type!");
  293.             return false;
  294.         }
  295.        
  296.         off = Integer.parseInt(dlctypes.get(dlctype).get(2).toString());
  297.         currentslot = Integer.parseInt(""+((char) fileArray[off])+((char) fileArray[off+1])+((char) fileArray[off+2]));
  298.         currentname = "";
  299.         off = Integer.parseInt(dlctypes.get(dlctype).get(3).toString());
  300.         while (fileArray[off]!=0) {
  301.             currentname += (char) fileArray[off];
  302.             off++;
  303.         }
  304.         System.out.println("DLC Type: "+dlctype);
  305.         System.out.println("Current Slot: "+currentslot);
  306.         System.out.println("Current Name: "+currentname);
  307.         return true;
  308.     }
  309.    
  310.     public static boolean changeSlot(String ofilen,int newslot) {      
  311.         FileInputStream fis;
  312.         File ofile = new File(ofilen);
  313.        
  314.         if (!ifile.exists() || !ifile.isFile()) {
  315.             System.out.println(ifile.getName()+": does not exist!");
  316.             return false;
  317.         }
  318.         if (ofile.exists()) {
  319.             System.out.println(ofilen+": exists already! Please rename it before running this!");
  320.             return false;
  321.         }
  322.        
  323.         int[] fileArray;
  324.         try {
  325.             System.out.println("Opening "+ifile.getName()+"...");
  326.             fis = new FileInputStream(ifile);
  327.             fileArray = new int[(int) ifile.length()];
  328.          
  329.             int i=0;
  330.             int offs=0;
  331.          
  332.             System.out.println("Reading file... this could take a while.");
  333.             while(i != -1) { /* -1 is a constant for 'EOF' : end of file. */
  334.                 i=fis.read();
  335.                 if (i!=-1) fileArray[offs] = i;
  336.                 offs++;
  337.             }
  338.             fis.close();
  339.         } catch (Exception e) {
  340.             System.out.println(ifile.getName()+": Error while opening!");
  341.             e.printStackTrace();
  342.             return false;
  343.         }
  344.        
  345.         System.out.println("\nDLC Type: "+((char) fileArray[0x1800]));
  346.         System.out.println("Current Slot: "+((char) fileArray[0x1803])+((char) fileArray[0x1804])+((char) fileArray[0x1805]));
  347.        
  348.         String newslots = ""+newslot;
  349.         while (newslots.length()<3) newslots = "0" + newslots;
  350.         fileArray[0x1803] = newslots.charAt(0);
  351.         fileArray[0x1804] = newslots.charAt(1);
  352.         fileArray[0x1805] = newslots.charAt(2);
  353.        
  354.         try {
  355.             System.out.println("\nOpening "+ofilen+"...");
  356.             FileOutputStream fos = new FileOutputStream(ofile);
  357.             DataOutputStream dos = new DataOutputStream(fos);
  358.             System.out.println("Writing file... this could take a while.");
  359.             for (int i: fileArray) {
  360.                 dos.writeByte(i);
  361.             }
  362.             dos.close();
  363.         } catch (Exception e) {
  364.             System.out.println(ofilen+": Error while writing!");
  365.             e.printStackTrace();
  366.             return false;
  367.         }
  368.        
  369.         System.out.println("\nSuccess! We're done here.");
  370.         dlctype = (char) fileArray[0x1800];
  371.         currentslot = newslot;
  372.         return true;
  373.     }
  374. }
  375.  
  376. class IntTextField extends JTextField {
  377.       /**
  378.      *
  379.      */
  380.     private static final long serialVersionUID = 1L;
  381.     public IntTextField(int defval, int size) {
  382.         super("" + defval, size);
  383.       }
  384.  
  385.       protected Document createDefaultModel() {
  386.         return new IntTextDocument();
  387.       }
  388.  
  389.       public boolean isValid() {
  390.         try {
  391.           Integer.parseInt(getText());
  392.           return true;
  393.         } catch (Exception e) {
  394.           return false;
  395.         }
  396.       }
  397.  
  398.       public int getValue() {
  399.         try {
  400.           return Integer.parseInt(getText());
  401.         } catch (NumberFormatException e) {
  402.           return 0;
  403.         }
  404.       }
  405.      
  406.       public void setValue(int val) {
  407.           setText(Integer.toString(val));
  408.       }
  409.      
  410.      
  411.      
  412.      
  413.       class IntTextDocument extends PlainDocument {
  414.         /**
  415.          *
  416.          */
  417.         private static final long serialVersionUID = 1L;
  418.  
  419.         public void insertString(int offs, String str, AttributeSet a)
  420.             throws BadLocationException {
  421.           if (str == null)
  422.             return;
  423.           String oldString = getText(0, getLength());
  424.           String newString = oldString.substring(0, offs) + str
  425.               + oldString.substring(offs);
  426.           try {
  427.             Integer.parseInt(newString + "0");
  428.             super.insertString(offs, str, a);
  429.           } catch (NumberFormatException e) {
  430.           }
  431.         }
  432.       }
  433.  
  434.     }
Add Comment
Please, Sign In to add comment