JSONReader.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. /*
  46. File background = new File(f.getParent() + File.separator + file.getString("background"));
  47. if(!background.exists() || !(background.getName().toLowerCase().endsWith(".jpg") || background.getName().toLowerCase().endsWith(".png")))
  48. throw new FileNotFoundException("Background image does not exist: " + background.getPath());
  49. s.setBackground(background);*/
  50. s.setBackground(null);
  51. /*
  52. File banner = new File(f.getParent() + File.separator + file.getString("banner"));
  53. if(!banner.exists() || !(banner.getName().toLowerCase().endsWith(".jpg") || banner.getName().toLowerCase().endsWith(".png")))
  54. throw new FileNotFoundException("Banner image does not exist: " + banner.getPath());
  55. s.setBanner(banner);*/
  56. s.setBanner(null);
  57. s.setFile(f);
  58. //Read Data data
  59. JsonArray data = obj.getJsonArray("data");
  60. for(int i = 0; i < data.size(); i++)
  61. {
  62. s.addSongInstance( readSongInstance(data.getJsonObject(i)) );
  63. }
  64. return s;
  65. }
  66. private static SongInstance readSongInstance(JsonObject obj) throws IOException {
  67. String difficulty = obj.getString("difficulty");
  68. SongInstance si = new SongInstance(difficulty);
  69. JsonArray object = obj.getJsonArray("objects");
  70. for(int i = 0; i < object.size(); i++)
  71. {
  72. si.addObjectInstance( readObjectInstance(object.getJsonObject(i)) );
  73. }
  74. JsonArray buttons = obj.getJsonArray("buttons");
  75. for(int i = 0; i < buttons.size(); i++)
  76. {
  77. si.addButtonInstance( readButtonInstance(buttons.getJsonObject(i)) );
  78. }
  79. return si;
  80. }
  81. private static ObjectInstance readObjectInstance(JsonObject obj)
  82. {
  83. ObjectInstance oi = new ObjectInstance();
  84. oi.setTime(obj.getInt("time"));
  85. oi.setDirection(obj.getInt("direction"));
  86. oi.setButtonID(obj.getInt("button"));
  87. if(obj.containsKey("hold") && obj.getBoolean("hold"))
  88. {
  89. oi.setHold(obj.getBoolean("hold"));
  90. oi.setLength(obj.getInt("length"));
  91. }
  92. return oi;
  93. }
  94. private static ButtonInstance readButtonInstance(JsonObject obj)
  95. {
  96. ButtonInstance bi = new ButtonInstance();
  97. bi.setTime(obj.getInt("time"));
  98. bi.setButtonID(obj.getInt("button"));
  99. Color color = GameModel.colors[obj.getInt("color") % GameModel.colors.length];
  100. bi.setColor(color);
  101. return bi;
  102. }
  103. }