JSONReader.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 javax.json.Json;
  9. import javax.json.JsonArray;
  10. import javax.json.JsonObject;
  11. import javax.json.JsonReader;
  12. import model.GameModel;
  13. import audio.ButtonInstance;
  14. import audio.ObjectInstance;
  15. import audio.Song;
  16. import audio.SongInstance;
  17. public class JSONReader {
  18. public static Song readSong(File f) throws IOException {
  19. if(!f.exists())
  20. throw new FileNotFoundException("CSF File does not exist");
  21. // Get CSF File
  22. InputStream is = new FileInputStream(f);
  23. // Read CSF Content
  24. JsonReader rdr = Json.createReader(is);
  25. JsonObject obj = rdr.readObject();
  26. rdr.close();
  27. is.close();
  28. if(!obj.containsKey("meta") || !obj.containsKey("file") || !obj.containsKey("data"))
  29. throw new IOException("Corrupt CSF File");
  30. // Create new Song
  31. Song s = new Song();
  32. // Read META data
  33. JsonObject meta = obj.getJsonObject("meta");
  34. s.setTitle(meta.getString("title"));
  35. s.setSubtitle(meta.getString("subtitle"));
  36. s.setAuthor(meta.getString("author"));
  37. s.setSampleStart(meta.getInt("sample_start"));
  38. s.setBPM(meta.getInt("BPM"));
  39. //Read FILE data
  40. JsonObject file = obj.getJsonObject("file");
  41. File audio = new File(f.getParent() + File.separator + file.getString("audio"));
  42. if(!audio.exists() || !audio.getName().toLowerCase().endsWith(".mp3"))
  43. throw new FileNotFoundException("Audio file does not exist: " + audio.getPath());
  44. s.setAudio(audio);
  45. File background = new File(f.getParent() + File.separator + file.getString("background"));
  46. if(!background.exists() || !(background.getName().toLowerCase().endsWith(".jpg") || background.getName().toLowerCase().endsWith(".png")))
  47. throw new FileNotFoundException("Background image does not exist: " + background.getPath());
  48. s.setBackground(background);
  49. File banner = new File(f.getParent() + File.separator + file.getString("banner"));
  50. if(!banner.exists() || !(banner.getName().toLowerCase().endsWith(".jpg") || banner.getName().toLowerCase().endsWith(".png")))
  51. throw new FileNotFoundException("Banner image does not exist: " + banner.getPath());
  52. s.setBanner(banner);
  53. s.setFile(f);
  54. //Read Data data
  55. JsonArray data = obj.getJsonArray("data");
  56. for(int i = 0; i < data.size(); i++)
  57. {
  58. s.addSongInstance( readSongInstance(data.getJsonObject(i)) );
  59. }
  60. return s;
  61. }
  62. private static SongInstance readSongInstance(JsonObject obj) throws IOException {
  63. String difficulty = obj.getString("difficulty");
  64. SongInstance si = new SongInstance(difficulty);
  65. JsonArray object = obj.getJsonArray("objects");
  66. for(int i = 0; i < object.size(); i++)
  67. {
  68. si.addObjectInstance( readObjectInstance(object.getJsonObject(i)) );
  69. }
  70. JsonArray buttons = obj.getJsonArray("buttons");
  71. for(int i = 0; i < buttons.size(); i++)
  72. {
  73. si.addButtonInstance( readButtonInstance(buttons.getJsonObject(i)) );
  74. }
  75. return si;
  76. }
  77. private static ObjectInstance readObjectInstance(JsonObject obj)
  78. {
  79. ObjectInstance oi = new ObjectInstance();
  80. oi.setTime(obj.getInt("time"));
  81. oi.setDirection(obj.getInt("direction"));
  82. oi.setButtonID(obj.getInt("button"));
  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. private static ButtonInstance readButtonInstance(JsonObject obj)
  91. {
  92. ButtonInstance bi = new ButtonInstance();
  93. bi.setTime(obj.getInt("time"));
  94. bi.setButtonID(obj.getInt("button"));
  95. Color color = GameModel.colors[obj.getInt("color") % GameModel.colors.length];
  96. bi.setColor(color);
  97. return bi;
  98. }
  99. }