Camtech075

ELF Method helper

Sep 17th, 2011
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.PrintStream;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.util.Scanner;
  7. import java.io.FileOutputStream;
  8.  
  9. public class Song_Helper {
  10.  
  11. public static String toHex(long a) {
  12.  
  13. //Laziness, it's easier to write
  14. //toHex(asdf) than "asdf.whatever"
  15.  
  16. String result = Long.toString(a, 16);
  17.  
  18. return result.toUpperCase();
  19.  
  20. }
  21.  
  22. public static void main(String[] args) throws IOException {
  23.  
  24. FileInputStream songReader = new FileInputStream(new File("song.gba"));
  25. FileOutputStream songWriter = new FileOutputStream("song.dmp");
  26. PrintStream dataWriter = new PrintStream("data.txt");
  27.  
  28. Scanner inputScanner = new Scanner(System.in);
  29.  
  30. final int TOTAL_LENGTH = songReader.available();
  31.  
  32. final int SONG_BEGIN = 0x1B3BB8;
  33. final int HEADER_CMD = 0x0A;
  34. final int END_TRACK_CMD = 0xB1;
  35. //final int LOOP_TRACK_CMD = 0xB2;
  36. //final int VOLUME_CMD = 0xBC;
  37. //final int SET_INST_CMD = 0xBD;
  38. //A lot of the commented code is just leftover. They may
  39. //be used for an update in the possibly near future.
  40.  
  41. songReader.skip(SONG_BEGIN);
  42.  
  43. final int SONG_LENGTH = songReader.available();
  44.  
  45. int first, second, third, fourth;
  46. int tracks;
  47. int tracknum = 1;
  48.  
  49. System.out.print("How many tracks? ");
  50. tracks = inputScanner.nextInt();
  51. //System.out.print("\nWrite to where? 0x");
  52. //int writeOffset = inputScanner.nextInt(16);
  53. //System.out.print("\n Where is the instrument map?");
  54.  
  55. dataWriter.println("Track " + tracknum + " is at 0x" + toHex(SONG_BEGIN));
  56.  
  57. int trackArray[] = new int[tracks];
  58. int headerOffset;
  59.  
  60. int currentOffset = TOTAL_LENGTH - songReader.available();
  61.  
  62. final boolean WORD_ALIGNED = currentOffset%4 == 0;
  63. //I know this is ugly, but I couldn't think of
  64. //a better (read: easier for me to find) way
  65. //to organize all these variables.
  66.  
  67. while (tracknum < tracks)
  68. {
  69. int single = songReader.read(); //Yeah, yeah, sloppy. It's a failsafe as far as I'm concerned.
  70. songWriter.write(single);
  71. int trackOffset = currentOffset + 1;
  72. currentOffset = TOTAL_LENGTH - songReader.available();
  73.  
  74. if(single == END_TRACK_CMD)
  75. {
  76. trackArray[tracknum] = currentOffset;
  77. ++tracknum;
  78. dataWriter.println("Track " + tracknum + " is at offset 0x" + toHex(trackOffset));
  79. }
  80. }
  81. currentOffset = TOTAL_LENGTH - songReader.available();
  82.  
  83. do
  84. {
  85. songReader.read();
  86. currentOffset = TOTAL_LENGTH - songReader.available();
  87. } while (/*!WORD_ALIGNED*/ songReader.read() != tracks);
  88.  
  89. headerOffset = currentOffset;
  90.  
  91. dataWriter.println("\nHeader is at offset 0x" + toHex(headerOffset));
  92. System.out.println("\nDone.\n");
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment