2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* cp_loader_it.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:11:45 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2020-08-13 20:58:13 +00:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2020-08-13 20:58:13 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "cp_loader_it.h"
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool CPLoader_IT::can_load_song() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool CPLoader_IT::can_load_sample() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool CPLoader_IT::can_load_instrument() {
|
|
|
|
return true;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
CPLoader::Error CPLoader_IT::load_song(const char *p_file, CPSong *p_song, bool p_sampleset) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
song = p_song;
|
|
|
|
|
|
|
|
if (file->open(p_file, CPFileAccessWrapper::READ) != CPFileAccessWrapper::OK)
|
2014-02-10 01:10:30 +00:00
|
|
|
return CPLoader::FILE_CANNOT_OPEN;
|
2017-03-18 23:36:26 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
Error err;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
char aux_identifier[4];
|
|
|
|
file->get_byte_array((uint8_t *)aux_identifier, 4);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (aux_identifier[0] != 'I' ||
|
|
|
|
aux_identifier[1] != 'M' ||
|
|
|
|
aux_identifier[2] != 'P' ||
|
|
|
|
aux_identifier[3] != 'M') {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
CP_PRINTERR("IT CPLoader CPSong: Failed Identifier");
|
|
|
|
return FILE_UNRECOGNIZED;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (p_sampleset) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
song->reset(false, true, true, false);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if ((err = load_header(true))) {
|
2014-02-10 01:10:30 +00:00
|
|
|
file->close();
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if ((err = load_samples())) {
|
2014-02-10 01:10:30 +00:00
|
|
|
file->close();
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if ((err = load_instruments())) {
|
2014-02-10 01:10:30 +00:00
|
|
|
file->close();
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FILE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
song->reset();
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if ((err = load_header(false))) {
|
2014-02-10 01:10:30 +00:00
|
|
|
file->close();
|
|
|
|
return err;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
|
|
|
|
if ((err = load_orders())) {
|
2014-02-10 01:10:30 +00:00
|
|
|
file->close();
|
|
|
|
return err;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
|
|
|
|
if ((err = load_patterns())) {
|
2014-02-10 01:10:30 +00:00
|
|
|
file->close();
|
|
|
|
return err;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
|
|
|
|
if ((err = load_samples())) {
|
2014-02-10 01:10:30 +00:00
|
|
|
file->close();
|
|
|
|
return err;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
|
|
|
|
if ((err = load_effects())) {
|
2014-02-10 01:10:30 +00:00
|
|
|
file->close();
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if ((err = load_instruments())) {
|
2014-02-10 01:10:30 +00:00
|
|
|
file->close();
|
|
|
|
return err;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
|
|
|
|
if ((err = load_message())) {
|
2014-02-10 01:10:30 +00:00
|
|
|
file->close();
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
file->close();
|
|
|
|
return FILE_OK;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
CPLoader::Error CPLoader_IT::load_sample(const char *p_file, CPSample *p_sample) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (file->open(p_file, CPFileAccessWrapper::READ) != CPFileAccessWrapper::OK)
|
2014-02-10 01:10:30 +00:00
|
|
|
return CPLoader::FILE_CANNOT_OPEN;
|
2017-03-18 23:36:26 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
p_sample->reset();
|
2017-03-18 23:36:26 +00:00
|
|
|
CPLoader::Error res = load_sample(p_sample);
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
file->close();
|
2017-03-18 23:36:26 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return res;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
CPLoader::Error CPLoader_IT::load_instrument(const char *p_file, CPSong *p_song, int p_instr_idx) {
|
|
|
|
|
|
|
|
CP_FAIL_INDEX_V(p_instr_idx, CPSong::MAX_INSTRUMENTS, CPLoader::FILE_CANNOT_OPEN);
|
|
|
|
|
|
|
|
if (file->open(p_file, CPFileAccessWrapper::READ) != CPFileAccessWrapper::OK)
|
2014-02-10 01:10:30 +00:00
|
|
|
return CPLoader::FILE_CANNOT_OPEN;
|
2017-03-18 23:36:26 +00:00
|
|
|
|
|
|
|
p_song->get_instrument(p_instr_idx)->reset();
|
|
|
|
|
|
|
|
int samples = 0;
|
|
|
|
CPLoader::Error res = load_instrument(p_song->get_instrument(p_instr_idx), &samples);
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (res) {
|
|
|
|
file->close();
|
|
|
|
return res;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
char exchange[CPSong::MAX_SAMPLES];
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < CPSong::MAX_SAMPLES; i++)
|
|
|
|
exchange[i] = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < samples; i++) {
|
|
|
|
|
|
|
|
file->seek(554 + i * 80); //i think this should work?! seems to.. but i'm not sure
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
/* find free sample */
|
2017-03-18 23:36:26 +00:00
|
|
|
|
|
|
|
int free_idx = -1;
|
|
|
|
for (int s = 0; s < CPSong::MAX_SAMPLES; s++) {
|
|
|
|
|
|
|
|
if (p_song->get_sample(s)->get_sample_data().is_null()) {
|
|
|
|
free_idx = s;
|
2014-02-10 01:10:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
if (free_idx == -1)
|
2014-02-10 01:10:30 +00:00
|
|
|
break; //can't seem to be able to load more samples
|
2017-03-18 23:36:26 +00:00
|
|
|
|
|
|
|
exchange[i] = free_idx;
|
|
|
|
res = load_sample(p_song->get_sample(free_idx));
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (res) {
|
2017-03-18 23:36:26 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
file->close();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < CPNote::NOTES; i++) {
|
|
|
|
|
|
|
|
int smp = song->get_instrument(p_instr_idx)->get_sample_number(i);
|
|
|
|
|
|
|
|
if (smp >= CPSong::MAX_SAMPLES)
|
2014-02-10 01:10:30 +00:00
|
|
|
continue;
|
2017-03-18 23:36:26 +00:00
|
|
|
|
|
|
|
if (smp < 0)
|
2014-02-10 01:10:30 +00:00
|
|
|
continue;
|
2017-03-18 23:36:26 +00:00
|
|
|
|
|
|
|
smp = exchange[smp];
|
|
|
|
|
|
|
|
song->get_instrument(p_instr_idx)->set_sample_number(i, smp);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
file->close();
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
CPLoader_IT::CPLoader_IT(CPFileAccessWrapper *p_file) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
file = p_file;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|