JSONReader.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package audio.io;
  2. import java.awt.Color;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.lang.reflect.Field;
  9. import javax.json.Json;
  10. import javax.json.JsonArray;
  11. import javax.json.JsonObject;
  12. import javax.json.JsonReader;
  13. import audio.ObjectInstance;
  14. import audio.Song;
  15. import audio.SongInstance;
  16. public class JSONReader {
  17. public static Song readSong(File f) throws IOException {
  18. if(!f.exists())
  19. throw new FileNotFoundException("CSF File does not exist");
  20. // Get CSF File
  21. InputStream is = new FileInputStream(f);
  22. // Read CSF Content
  23. JsonReader rdr = Json.createReader(is);
  24. JsonObject obj = rdr.readObject();
  25. rdr.close();
  26. if(!obj.containsKey("meta") || !obj.containsKey("file") || !obj.containsKey("data"))
  27. throw new IOException("Corrupt CSF File");
  28. // Create new Song
  29. Song s = new Song();
  30. // Read META data
  31. JsonObject meta = obj.getJsonObject("meta");
  32. s.setTitle(meta.getString("title"));
  33. s.setSubtitle(meta.getString("subtitle"));
  34. s.setAuthor(meta.getString("author"));
  35. s.setCreator(meta.getString("creator"));
  36. s.setBPM(meta.getInt("BPM"));
  37. //Read FILE data
  38. JsonObject file = obj.getJsonObject("file");
  39. File audio = new File(file.getString("audio"));
  40. if(!audio.exists() || !audio.getName().endsWith(".mp3"))
  41. throw new FileNotFoundException("Audio file does not exist");
  42. s.setAudio(audio);
  43. File background = new File(file.getString("background"));
  44. if(!background.exists() || !(background.getName().endsWith(".jpg") || background.getName().endsWith(".png")))
  45. throw new FileNotFoundException("Background image does not exist");
  46. s.setBackground(background);
  47. File banner = new File(file.getString("banner"));
  48. if(!banner.exists() || !(banner.getName().endsWith(".jpg") || banner.getName().endsWith(".png")))
  49. throw new FileNotFoundException("Banner image does not exist");
  50. s.setBanner(banner);
  51. s.setFile(f);
  52. //Read Data data
  53. JsonArray data = obj.getJsonArray("data");
  54. for(int i = 0; i < data.size(); i++)
  55. {
  56. s.addSongInstance( readSongInstance(data.getJsonObject(i)) );
  57. }
  58. return s;
  59. }
  60. private static SongInstance readSongInstance(JsonObject obj) throws IOException {
  61. String difficulty = obj.getString("difficulty");
  62. SongInstance si = new SongInstance(difficulty);
  63. JsonArray object = obj.getJsonArray("objects");
  64. for(int i = 0; i < object.size(); i++)
  65. {
  66. si.addObjectInstance( readObjectInstance(object.getJsonObject(i)) );
  67. }
  68. return si;
  69. }
  70. private static ObjectInstance readObjectInstance(JsonObject obj) throws IOException
  71. {
  72. ObjectInstance oi = new ObjectInstance();
  73. oi.setTime(obj.getInt("time"));
  74. oi.setDirection(obj.getInt("direction"));
  75. Color color;
  76. try {
  77. Field field = Color.class.getField(obj.getString("color").toLowerCase());
  78. color = (Color)field.get(null);
  79. } catch (Exception e) {
  80. throw new IOException("Cannot read property color from " + obj);
  81. }
  82. oi.setColor(color);
  83. if(obj.containsKey("hold") && obj.getBoolean("hold"))
  84. {
  85. oi.setHold(obj.getBoolean("hold"));
  86. oi.setLength(obj.getInt("length"));
  87. }
  88. return oi;
  89. }
  90. }