cp_loader_it.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*************************************************************************/
  2. /* cp_loader_it.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "cp_loader_it.h"
  31. bool CPLoader_IT::can_load_song() {
  32. return true;
  33. }
  34. bool CPLoader_IT::can_load_sample() {
  35. return true;
  36. }
  37. bool CPLoader_IT::can_load_instrument() {
  38. return true;
  39. }
  40. CPLoader::Error CPLoader_IT::load_song(const char *p_file, CPSong *p_song, bool p_sampleset) {
  41. song = p_song;
  42. if (file->open(p_file, CPFileAccessWrapper::READ) != CPFileAccessWrapper::OK)
  43. return CPLoader::FILE_CANNOT_OPEN;
  44. Error err;
  45. char aux_identifier[4];
  46. file->get_byte_array((uint8_t *)aux_identifier, 4);
  47. if (aux_identifier[0] != 'I' ||
  48. aux_identifier[1] != 'M' ||
  49. aux_identifier[2] != 'P' ||
  50. aux_identifier[3] != 'M') {
  51. CP_PRINTERR("IT CPLoader CPSong: Failed Identifier");
  52. return FILE_UNRECOGNIZED;
  53. }
  54. if (p_sampleset) {
  55. song->reset(false, true, true, false);
  56. if ((err = load_header(true))) {
  57. file->close();
  58. return err;
  59. }
  60. if ((err = load_samples())) {
  61. file->close();
  62. return err;
  63. }
  64. if ((err = load_instruments())) {
  65. file->close();
  66. return err;
  67. }
  68. return FILE_OK;
  69. }
  70. song->reset();
  71. if ((err = load_header(false))) {
  72. file->close();
  73. return err;
  74. }
  75. if ((err = load_orders())) {
  76. file->close();
  77. return err;
  78. }
  79. if ((err = load_patterns())) {
  80. file->close();
  81. return err;
  82. }
  83. if ((err = load_samples())) {
  84. file->close();
  85. return err;
  86. }
  87. if ((err = load_effects())) {
  88. file->close();
  89. return err;
  90. }
  91. if ((err = load_instruments())) {
  92. file->close();
  93. return err;
  94. }
  95. if ((err = load_message())) {
  96. file->close();
  97. return err;
  98. }
  99. file->close();
  100. return FILE_OK;
  101. }
  102. CPLoader::Error CPLoader_IT::load_sample(const char *p_file, CPSample *p_sample) {
  103. if (file->open(p_file, CPFileAccessWrapper::READ) != CPFileAccessWrapper::OK)
  104. return CPLoader::FILE_CANNOT_OPEN;
  105. p_sample->reset();
  106. CPLoader::Error res = load_sample(p_sample);
  107. file->close();
  108. return res;
  109. }
  110. CPLoader::Error CPLoader_IT::load_instrument(const char *p_file, CPSong *p_song, int p_instr_idx) {
  111. CP_FAIL_INDEX_V(p_instr_idx, CPSong::MAX_INSTRUMENTS, CPLoader::FILE_CANNOT_OPEN);
  112. if (file->open(p_file, CPFileAccessWrapper::READ) != CPFileAccessWrapper::OK)
  113. return CPLoader::FILE_CANNOT_OPEN;
  114. p_song->get_instrument(p_instr_idx)->reset();
  115. int samples = 0;
  116. CPLoader::Error res = load_instrument(p_song->get_instrument(p_instr_idx), &samples);
  117. if (res) {
  118. file->close();
  119. return res;
  120. }
  121. char exchange[CPSong::MAX_SAMPLES];
  122. for (int i = 0; i < CPSong::MAX_SAMPLES; i++)
  123. exchange[i] = 0;
  124. for (int i = 0; i < samples; i++) {
  125. file->seek(554 + i * 80); //i think this should work?! seems to.. but i'm not sure
  126. /* find free sample */
  127. int free_idx = -1;
  128. for (int s = 0; s < CPSong::MAX_SAMPLES; s++) {
  129. if (p_song->get_sample(s)->get_sample_data().is_null()) {
  130. free_idx = s;
  131. break;
  132. }
  133. }
  134. if (free_idx == -1)
  135. break; //can't seem to be able to load more samples
  136. exchange[i] = free_idx;
  137. res = load_sample(p_song->get_sample(free_idx));
  138. if (res) {
  139. file->close();
  140. return res;
  141. }
  142. }
  143. for (int i = 0; i < CPNote::NOTES; i++) {
  144. int smp = song->get_instrument(p_instr_idx)->get_sample_number(i);
  145. if (smp >= CPSong::MAX_SAMPLES)
  146. continue;
  147. if (smp < 0)
  148. continue;
  149. smp = exchange[smp];
  150. song->get_instrument(p_instr_idx)->set_sample_number(i, smp);
  151. }
  152. file->close();
  153. return res;
  154. }
  155. CPLoader_IT::CPLoader_IT(CPFileAccessWrapper *p_file) {
  156. file = p_file;
  157. }