| 1 | #include <ctype.h> |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <stdint.h> |
| 5 | #include <stdbool.h> |
| 6 | #include <string.h> |
| 7 | #include <sys/stat.h> |
| 8 | #include "pt2_mouse.h" |
| 9 | #include "pt2_textout.h" |
| 10 | #include "pt2_helpers.h" |
| 11 | #include "pt2_sampler.h" |
| 12 | #include "pt2_config.h" |
| 13 | #include "pt2_askbox.h" |
| 14 | |
| 15 | bool modSave(char *fileName) |
| 16 | { |
| 17 | FILE *f = fopen(fileName, "wb"); |
| 18 | if (f == NULL) |
| 19 | { |
| 20 | displayErrorMsg("FILE I/O ERROR !"); |
| 21 | return false; |
| 22 | } |
| 23 | |
| 24 | fwrite(song->header.name, 1, 20, f); |
| 25 | |
| 26 | for (int32_t i = 0; i < MOD_SAMPLES; i++) |
| 27 | { |
| 28 | moduleSample_t *s = &song->samples[i]; |
| 29 | |
| 30 | fwrite(s->text, 1, 22, f); |
| 31 | |
| 32 | uint16_t length = SWAP16(s->length >> 1); |
| 33 | fwrite(&length, sizeof (int16_t), 1, f); |
| 34 | |
| 35 | fputc(s->fineTune & 0xF, f); |
| 36 | fputc(((uint8_t)s->volume > 64) ? 64 : s->volume, f); |
| 37 | |
| 38 | int32_t loopStart = s->loopStart; |
| 39 | int32_t loopLength = s->loopLength; |
| 40 | |
| 41 | if (loopLength < 2) |
| 42 | loopLength = 2; |
| 43 | |
| 44 | if (loopStart+loopLength <= 2 || loopStart+loopLength > s->length) |
| 45 | { |
| 46 | loopStart = 0; |
| 47 | loopLength = 2; |
| 48 | } |
| 49 | |
| 50 | uint16_t loopStart16 = SWAP16(loopStart >> 1); |
| 51 | uint16_t loopLength16 = SWAP16(loopLength >> 1); |
| 52 | |
| 53 | fwrite(&loopStart16, sizeof (int16_t), 1, f); |
| 54 | fwrite(&loopLength16, sizeof (int16_t), 1, f); |
| 55 | } |
| 56 | |
| 57 | fputc((uint8_t)song->header.songLength, f); |
| 58 | fputc(0x7F, f); // ProTracker puts 0x7F at this place (restartPos/tempo in other trackers) |
| 59 | |
| 60 | for (int32_t i = 0; i < 128; i++) |
| 61 | fputc((uint8_t)song->header.patternTable[i], f); |
| 62 | |
| 63 | int32_t numPatterns = 0; |
| 64 | for (int32_t i = 0; i < 128; i++) |
| 65 | { |
| 66 | if (song->header.patternTable[i] > numPatterns) |
| 67 | numPatterns = song->header.patternTable[i]; |
| 68 | } |
| 69 | |
| 70 | numPatterns++; |
| 71 | if (numPatterns > MAX_PATTERNS) |
| 72 | numPatterns = MAX_PATTERNS; |
| 73 | |
| 74 | fwrite((numPatterns <= 64) ? "M.K." : "M!K!", 1, 4, f); |
| 75 | |
| 76 | for (int32_t i = 0; i < numPatterns; i++) |
| 77 | { |
| 78 | note_t *note = song->patterns[i]; |
| 79 | for (int32_t j = 0; j < MOD_ROWS * PAULA_VOICES; j++, note++) |
| 80 | { |
| 81 | fputc((note->sample & 0xF0) | (note->period >> 8), f); |
| 82 | fputc(note->period & 0xFF, f); |
| 83 | fputc(((note->sample << 4) & 0xF0) | (note->command & 0x0F), f); |
| 84 | fputc(note->param, f); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | for (int32_t i = 0; i < MOD_SAMPLES; i++) |
| 89 | { |
| 90 | moduleSample_t *s = &song->samples[i]; |
| 91 | const int8_t *smpPtr8 = &song->sampleData[s->offset]; |
| 92 | |
| 93 | // clear first two bytes of non-looping samples (prevents stuck beep) |
| 94 | if (s->length >= 2 && s->loopStart+s->loopLength == 2) |
| 95 | { |
| 96 | fputc(0, f); |
| 97 | fputc(0, f); |
| 98 | |
| 99 | fwrite(&smpPtr8[2], 1, s->length-2, f); |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | fwrite(smpPtr8, 1, s->length, f); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | fclose(f); |
| 108 | |
| 109 | displayMsg("MODULE SAVED !"); |
| 110 | setMsgPointer(); |
| 111 | |
| 112 | diskop.cached = false; |
| 113 | if (ui.diskOpScreenShown) |
| 114 | ui.updateDiskOpFileList = true; |
| 115 | |
| 116 | updateWindowTitle(MOD_NOT_MODIFIED); |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | bool saveModule(bool checkIfFileExist, bool giveNewFreeFilename) |
| 121 | { |
| 122 | char fileName[128], tmpBuffer[64]; |
| 123 | struct stat statBuffer; |
| 124 | |
| 125 | memset(tmpBuffer, 0, sizeof (tmpBuffer)); |
| 126 | memset(fileName, 0, sizeof (fileName)); |
| 127 | |
| 128 | if (config.modDot) |
| 129 | { |
| 130 | // extension.filename |
| 131 | if (song->header.name[0] == '\0') |
| 132 | { |
| 133 | strcat(fileName, "mod.untitled"); |
| 134 | } |
| 135 | else |
| 136 | { |
| 137 | strcat(fileName, "mod."); |
| 138 | for (int32_t i = 4; i < 20+4; i++) |
| 139 | { |
| 140 | fileName[i] = (char)tolower(song->header.name[i-4]); |
| 141 | if (fileName[i] == '\0') break; |
| 142 | sanitizeFilenameChar(&fileName[i]); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | else |
| 147 | { |
| 148 | // filename.extension |
| 149 | if (song->header.name[0] == '\0') |
| 150 | { |
| 151 | strcat(fileName, "untitled.mod"); |
| 152 | } |
| 153 | else |
| 154 | { |
| 155 | for (int32_t i = 0; i < 20; i++) |
| 156 | { |
| 157 | fileName[i] = (char)tolower(song->header.name[i]); |
| 158 | if (fileName[i] == '\0') break; |
| 159 | sanitizeFilenameChar(&fileName[i]); |
| 160 | } |
| 161 | strcat(fileName, ".mod"); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | if (giveNewFreeFilename && stat(fileName, &statBuffer) == 0) |
| 166 | { |
| 167 | for (int32_t i = 1; i <= 999; i++) |
| 168 | { |
| 169 | if (config.modDot) |
| 170 | { |
| 171 | // extension.filename |
| 172 | if (song->header.name[0] == '\0') |
| 173 | { |
| 174 | sprintf(fileName, "mod.untitled-%d", i); |
| 175 | } |
| 176 | else |
| 177 | { |
| 178 | for (int32_t j = 0; j < 20; j++) |
| 179 | { |
| 180 | tmpBuffer[j] = (char)tolower(song->header.name[j]); |
| 181 | if (tmpBuffer[j] == '\0') break; |
| 182 | sanitizeFilenameChar(&tmpBuffer[j]); |
| 183 | } |
| 184 | sprintf(fileName, "mod.%s-%d", tmpBuffer, i); |
| 185 | } |
| 186 | } |
| 187 | else |
| 188 | { |
| 189 | // filename.extension |
| 190 | if (song->header.name[0] == '\0') |
| 191 | { |
| 192 | sprintf(fileName, "untitled-%d.mod", i); |
| 193 | } |
| 194 | else |
| 195 | { |
| 196 | for (int32_t j = 0; j < 20; j++) |
| 197 | { |
| 198 | tmpBuffer[j] = (char)tolower(song->header.name[j]); |
| 199 | if (tmpBuffer[j] == '\0') break; |
| 200 | sanitizeFilenameChar(&tmpBuffer[j]); |
| 201 | } |
| 202 | sprintf(fileName, "%s-%d.mod", tmpBuffer, i); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | if (stat(fileName, &statBuffer) != 0) |
| 207 | break; // this filename can be used |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | if (checkIfFileExist && stat(fileName, &statBuffer) == 0) |
| 212 | { |
| 213 | if (!askBox(ASKBOX_YES_NO, "OVERWRITE FILE ?")) |
| 214 | return false; |
| 215 | } |
| 216 | |
| 217 | return modSave(fileName); |
| 218 | } |