JSONReader.java 3.6 KB

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