Debian Codegraph @

src/pt2_module_loader.c

raw symbols SCIP
1 // for finding memory leaks in debug mode with Visual Studio
2 #if defined _DEBUG && defined _MSC_VER
3 #include <crtdbg.h>
4 #endif
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <stdint.h>
9 #include <stdbool.h>
10 #include <string.h>
11 #ifdef _WIN32
12 #include <io.h>
13 #else
14 #include <unistd.h>
15 #endif
16 #include <fcntl.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include "pt2_mouse.h"
20 #include "pt2_sampler.h"
21 #include "pt2_replayer.h"
22 #include "pt2_textout.h"
23 #include "pt2_audio.h"
24 #include "pt2_helpers.h"
25 #include "pt2_visuals.h"
26 #include "pt2_sample_loader.h"
27 #include "pt2_config.h"
28 #include "modloaders/pt2_load_mod15.h"
29 #include "modloaders/pt2_load_mod31.h"
30 #include "modloaders/pt2_xpk_unpack.h"
31 #include "modloaders/pt2_pp_unpack.h"
32 #include "pt2_askbox.h"
33 #include "pt2_posed.h"
34
35 static void fixZeroesInString(char *str, uint32_t maxLength); // converts zeroes to spaces in a string, up until the last zero found
36
37 module_t *modLoad(UNICHAR *fileName)
38 {
39 uint32_t powerPackerID;
40
41 uint8_t *modBuffer = NULL;
42 module_t *newMod = NULL;
43
44 FILE *f = UNICHAR_FOPEN(fileName, "rb");
45 if (f == NULL)
46 {
47 displayErrorMsg("FILE I/O ERROR !");
48 goto loadError;
49 }
50
51 fseek(f, 0, SEEK_END);
52 uint32_t filesize = ftell(f);
53 rewind(f);
54
55 // check if mod is a powerpacker mod
56 fread(&powerPackerID, 4, 1, f);
57 if (powerPackerID == 0x30325850) // "PX20"
58 {
59 displayErrorMsg("ENCRYPTED MOD !");
60 goto loadError;
61 }
62 else if (powerPackerID == 0x30325050) // "PP20"
63 {
64 modBuffer = unpackPP(f, &filesize);
65 if (modBuffer == NULL)
66 {
67 displayErrorMsg("PP UNPACK ERROR !");
68 goto loadError; // error msg is set in unpackPPModule()
69 }
70 }
71 else
72 {
73 if (detectXPK(f))
74 {
75 if (!unpackXPK(f, &filesize, &modBuffer))
76 {
77 displayErrorMsg("XPK UNPACK ERROR");
78 goto loadError;
79 }
80
81 if (modBuffer == NULL)
82 {
83 statusOutOfMemory();
84 goto loadError;
85 }
86
87 fclose(f);
88 }
89 else
90 {
91 modBuffer = (uint8_t *)malloc(filesize);
92 if (modBuffer == NULL)
93 {
94 statusOutOfMemory();
95 goto loadError;
96 }
97
98 rewind(f);
99 fread(modBuffer, 1, filesize, f);
100 fclose(f);
101 }
102 }
103
104 // read module is in now in modBuffer, time to load...
105
106 bool isMod31 = detectMod31(modBuffer, filesize);
107 if (isMod31)
108 newMod = loadMod31(modBuffer, filesize);
109 else
110 newMod = loadMod15(modBuffer, filesize);
111
112 free(modBuffer);
113 modBuffer = NULL;
114
115 if (newMod == NULL)
116 {
117 // error message is shown in the mod loader
118 goto loadError;
119 }
120
121 // module is loaded, do some sanitation...
122
123 newMod->header.name[20] = '\0';
124
125 // convert illegal song name characters to space
126 for (int32_t i = 0; i < 20; i++)
127 {
128 char tmpChar = newMod->header.name[i];
129 if ((tmpChar < ' ' || tmpChar > '~') && tmpChar != '\0')
130 tmpChar = ' ';
131
132 newMod->header.name[i] = tmpChar;
133 }
134
135 fixZeroesInString(newMod->header.name, 20);
136
137 moduleSample_t *s = newMod->samples;
138 for (int32_t i = 0; i < MOD_SAMPLES; i++, s++)
139 {
140 s->text[22] = '\0';
141
142 // convert illegal sample name characters to space
143 for (int32_t j = 0; j < 22; j++)
144 {
145 char tmpChar = s->text[j];
146 if ((tmpChar < ' ' || tmpChar > '~') && tmpChar != '\0')
147 tmpChar = ' ';
148
149 s->text[j] = tmpChar;
150 }
151
152 fixZeroesInString(s->text, 22);
153
154 if (s->length > config.maxSampleLength)
155 s->length = config.maxSampleLength;
156
157 if ((uint8_t)s->volume > 64)
158 s->volume = 64;
159
160 if (s->loopLength < 2)
161 s->loopLength = 2;
162
163 // we don't support samples bigger than 65534 (or 128kB) bytes, disable uncompatible loops
164 if (s->loopStart > config.maxSampleLength || s->loopStart+s->loopLength > config.maxSampleLength)
165 {
166 s->loopStart = 0;
167 s->loopLength = 2;
168 }
169
170 // some modules are broken like this, adjust sample length if possible (this is ok if we have room)
171 if (s->length > 0 && s->loopLength > 2 && s->loopStart+s->loopLength > s->length)
172 {
173 int32_t loopOverflowVal = (s->loopStart + s->loopLength) - s->length;
174 if (s->length+loopOverflowVal <= config.maxSampleLength)
175 {
176 s->length += loopOverflowVal; // this is safe, we're allocating 65534 bytes per sample slot
177 }
178 else
179 {
180 s->loopStart = 0;
181 s->loopLength = 2;
182 }
183 }
184
185 // clear first two bytes of non-looping samples to prevent beep after sample has been played
186 if (s->length >= 2 && s->loopStart+s->loopLength <= 2)
187 {
188 newMod->sampleData[s->offset+0] = 0;
189 newMod->sampleData[s->offset+1] = 0;
190 }
191 }
192
193 initializeModuleChannels(newMod);
194 return newMod;
195
196 loadError:
197 if (modBuffer != NULL)
198 free(modBuffer);
199
200 return NULL;
201 }
202
203 static void fixZeroesInString(char *str, uint32_t maxLength)
204 {
205 int32_t i;
206
207 for (i = maxLength-1; i >= 0; i--)
208 {
209 if (str[i] != '\0')
210 break;
211 }
212
213 // convert zeroes to spaces
214 if (i > 0)
215 {
216 for (int32_t j = 0; j < i; j++)
217 {
218 if (str[j] == '\0')
219 str[j] = ' ';
220 }
221 }
222 }
223
224 void setupLoadedMod(void)
225 {
226 for (uint8_t i = 0; i < MOD_SAMPLES; i++)
227 fillSampleRedoBuffer(i);
228
229 modSetPos(0, 0);
230 modSetPattern(0); // set pattern to 00 instead of first order's pattern
231
232 posEdClearNames();
233
234 // setup GUI text pointers
235 editor.currEditPatternDisp = &song->currPattern;
236 editor.currPosDisp = &song->currPos;
237 editor.currPatternDisp = &song->header.patternTable[0];
238 editor.currPosEdPattDisp = &song->header.patternTable[0];
239 editor.currLengthDisp = &song->header.songLength;
240
241 // calculate MOD size
242 ui.updateSongSize = true;
243
244 editor.muted[0] = false;
245 editor.muted[1] = false;
246 editor.muted[2] = false;
247 editor.muted[3] = false;
248
249 editor.editMoveAdd = 1;
250 editor.currSample = 0;
251
252 editor.playbackSeconds = editor.playbackSecondsFrac = 0;
253 editor.modLoaded = true;
254 editor.blockMarkFlag = false;
255 editor.sampleZero = false;
256 editor.hiLowInstr = 0;
257
258 // disable LED filter after module load (real PT doesn't do this)
259 setLEDFilter(false);
260
261 updateWindowTitle(MOD_NOT_MODIFIED);
262
263 editor.timingMode = TEMPO_MODE_CIA;
264 updateReplayerTimingMode();
265
266 modSetSpeed(6);
267 modSetTempo(song->header.initialTempo, false); // 125 for normal MODs, custom value for certain STK/UST MODs
268
269 updateCurrSample();
270 editor.samplePos = 0;
271 updateSamplePos();
272 }
273
274 void loadModFromArg(char *arg)
275 {
276 statusAllRight();
277
278 uint32_t filenameLen = (uint32_t)strlen(arg);
279
280 UNICHAR *filenameU = (UNICHAR *)calloc(filenameLen + 2, sizeof (UNICHAR));
281 if (filenameU == NULL)
282 {
283 statusOutOfMemory();
284 return;
285 }
286
287 #ifdef _WIN32
288 MultiByteToWideChar(CP_UTF8, 0, arg, -1, filenameU, filenameLen);
289 #else
290 strcpy(filenameU, arg);
291 #endif
292
293 module_t *newSong = modLoad(filenameU);
294 if (newSong != NULL)
295 {
296 song->loaded = false;
297 modFree();
298 song = newSong;
299 setupLoadedMod();
300 song->loaded = true;
301 }
302 else
303 {
304 editor.errorMsgActive = true;
305 editor.errorMsgBlock = true;
306 editor.errorMsgCounter = 0;
307
308 // status/error message is set in the mod loader
309 setErrPointer();
310 }
311
312 free(filenameU);
313 }
314
315 static bool testExtension(char *ext, uint8_t extLen, char *fullPath)
316 {
317 // checks for EXT.filename and filename.EXT
318
319 extLen++; // add one to length (dot)
320
321 char *fileName = strrchr(fullPath, DIR_DELIMITER);
322 if (fileName != NULL)
323 fileName++;
324 else
325 fileName = fullPath;
326
327 uint32_t fileNameLen = (uint32_t)strlen(fileName);
328 if (fileNameLen >= extLen)
329 {
330 char begStr[8], endStr[8];
331
332 sprintf(begStr, "%s.", ext);
333 if (!_strnicmp(begStr, fileName, extLen))
334 return true;
335
336 sprintf(endStr, ".%s", ext);
337 if (!_strnicmp(endStr, fileName + (fileNameLen - extLen), extLen))
338 return true;
339 }
340
341 return false;
342 }
343
344 void loadDroppedFile(char *fullPath, uint32_t fullPathLen, bool autoPlay, bool songModifiedCheck)
345 {
346 // don't allow drag n' drop if the tracker is busy
347 if (ui.pointerMode == POINTER_MODE_MSG1 || diskop.isFilling ||
348 editor.mod2WavOngoing || editor.pat2SmpOngoing ||
349 ui.samplerFiltersBoxShown || ui.samplerVolBoxShown || ui.samplingBoxShown)
350 {
351 return;
352 }
353
354 char *ansiName = (char *)calloc(fullPathLen + 10, sizeof (char));
355 if (ansiName == NULL)
356 {
357 statusOutOfMemory();
358 return;
359 }
360
361 UNICHAR *fullPathU = (UNICHAR *)calloc(fullPathLen + 2, sizeof (UNICHAR));
362 if (fullPathU == NULL)
363 {
364 free(ansiName);
365 statusOutOfMemory();
366 return;
367 }
368
369 #ifdef _WIN32
370 MultiByteToWideChar(CP_UTF8, 0, fullPath, -1, fullPathU, fullPathLen);
371 #else
372 strcpy(fullPathU, fullPath);
373 #endif
374
375 unicharToAnsi(ansiName, fullPathU, fullPathLen);
376
377 // make a new pointer point to filename (strip path)
378 char *fileName = strrchr(ansiName, DIR_DELIMITER);
379 if (fileName != NULL)
380 fileName++;
381 else
382 fileName = ansiName;
383
384 // check if the file extension is a module (FIXME: check module by content instead..?)
385 bool isMod = false;
386 if (testExtension("MOD", 3, fileName)) isMod = true;
387 else if (testExtension("M15", 3, fileName)) isMod = true;
388 else if (testExtension("STK", 3, fileName)) isMod = true;
389 else if (testExtension("NST", 3, fileName)) isMod = true;
390 else if (testExtension("UST", 3, fileName)) isMod = true;
391 else if (testExtension("PP", 2, fileName)) isMod = true;
392 else if (testExtension("NT", 2, fileName)) isMod = true;
393
394 if (isMod)
395 {
396 if (songModifiedCheck && song->modified)
397 {
398 if (!askBox(ASKBOX_YES_NO, "SONG IS UNSAVED !"))
399 goto DropExit;
400 }
401
402 module_t *newSong = modLoad(fullPathU);
403 if (newSong != NULL)
404 {
405 uint8_t oldMode = editor.currMode;
406 uint8_t oldPlayMode = editor.playMode;
407
408 modStop();
409 modFree();
410
411 song = newSong;
412 setupLoadedMod();
413 song->loaded = true;
414
415 statusAllRight();
416
417 if (autoPlay)
418 {
419 editor.playMode = PLAY_MODE_NORMAL;
420 editor.currMode = MODE_PLAY;
421
422 // start normal playback
423 modPlay(DONT_SET_PATTERN, 0, 0);
424
425 pointerSetMode(POINTER_MODE_PLAY, DO_CARRY);
426 }
427 else if (oldMode == MODE_PLAY || oldMode == MODE_RECORD)
428 {
429 // use last mode
430 editor.playMode = oldPlayMode;
431 if (oldPlayMode == PLAY_MODE_PATTERN || oldMode == MODE_RECORD)
432 modPlay(0, 0, 0);
433 else
434 modPlay(DONT_SET_PATTERN, 0, 0);
435 editor.currMode = oldMode;
436
437 if (oldMode == MODE_RECORD)
438 pointerSetMode(POINTER_MODE_RECORD, DO_CARRY);
439 else
440 pointerSetMode(POINTER_MODE_PLAY, DO_CARRY);
441 }
442 else
443 {
444 // stop playback
445 editor.playMode = PLAY_MODE_NORMAL;
446 editor.currMode = MODE_IDLE;
447 editor.songPlaying = false;
448 pointerSetMode(POINTER_MODE_IDLE, DO_CARRY);
449 }
450
451 displayMainScreen();
452 }
453 else
454 {
455 editor.errorMsgActive = true;
456 editor.errorMsgBlock = true;
457 editor.errorMsgCounter = 0;
458 setErrPointer(); // status/error message is set in the mod loader
459 }
460 }
461 else
462 {
463 loadSample(fullPathU, fileName);
464 }
465
466 DropExit:
467 free(ansiName);
468 free(fullPathU);
469 }