Skip to content

Commit

Permalink
port: mod: add support for sequence replacements
Browse files Browse the repository at this point in the history
  • Loading branch information
fgsfdsfgs committed Sep 27, 2023
1 parent 8a342a0 commit 466c773
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 10 deletions.
2 changes: 2 additions & 0 deletions port/include/mod.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ s32 modTextureLoad(u16 num, void *dst, u32 dstSize);
s32 modAnimationLoadDescriptor(u16 num, struct animtableentry *anim);
void *modAnimationLoadData(u16 num);

void *modSequenceLoad(u16 num, u32 *outSize);

#endif
25 changes: 25 additions & 0 deletions port/src/mod.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#define MOD_TEXTURES_DIR "textures"
#define MOD_ANIMATIONS_DIR "animations"
#define MOD_SEQUENCES_DIR "sequences"

extern struct stagemusic g_StageTracks[];
extern struct stageallocation g_StageAllocations8Mb[];
Expand Down Expand Up @@ -437,6 +438,30 @@ s32 modTextureLoad(u16 num, void *dst, u32 dstSize)
return ret;
}

void *modSequenceLoad(u16 num, u32 *outSize)
{
static s32 dirExists = -1;
if (dirExists < 0) {
dirExists = (fsFileSize(MOD_SEQUENCES_DIR "/") >= 0);
}

if (!dirExists) {
return NULL;
}

char path[FS_MAXPATH + 1];
snprintf(path, sizeof(path), MOD_SEQUENCES_DIR "/%04x.bin", num);
if (fsFileSize(path) > 0) {
void *ret = fsFileLoad(path, outSize);
if (ret) {
sysLogPrintf(LOG_NOTE, "mod: loaded external sequence %04x", num);
return ret;
}
}

return NULL;
}

void *modAnimationLoadData(u16 num)
{
char path[FS_MAXPATH + 1];
Expand Down
55 changes: 45 additions & 10 deletions src/lib/snd.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
#include "data.h"
#include "types.h"
#ifndef PLATFORM_N64
#include "system.h"
#include "preprocess.h"
#include "mod.h"
#endif

#define MAX_SEQ_SIZE_4MB 1024 * 14
Expand Down Expand Up @@ -1637,22 +1639,55 @@ bool seqPlay(struct seqinstance *seq, s32 tracknum)
return false;
}

binlen = ALIGN16(g_SeqTable->entries[seq->tracknum].binlen) + 0x40;
#ifndef PLATFORM_N64
// try to load external replacement, which can be either compressed or not
u32 extlen = 0;
u8 *extseq = modSequenceLoad(seq->tracknum, &extlen);
if (extseq) {
if (extlen > 2 && rzipIs1173(extseq)) {
// sequence is compressed; uncompress
binlen = ((u32)extseq[2] << 16) | ((u32)extseq[3] << 8) | (u32)extseq[4];
binlen = ALIGN16(binlen) + 0x40;
if (binlen >= g_SeqBufferSize) {
return false;
}
ziplen = ALIGN16(extlen);
binstart = seq->data;
zipstart = binstart + binlen - ziplen;
ziplen = rzipInflate(zipstart, binstart, scratch);
} else {
// sequence is uncompressed; just load as is
binlen = ALIGN16(extlen) + 0x40;
if (binlen >= g_SeqBufferSize) {
return false;
}
ziplen = extlen;
binstart = seq->data;
zipstart = NULL;
dmaExec(binstart, (romptr_t)extseq, extlen);
}
sysMemFree(extseq);
} else
#endif
{
binlen = ALIGN16(g_SeqTable->entries[seq->tracknum].binlen) + 0x40;

if (binlen >= g_SeqBufferSize) {
return false;
}
if (binlen >= g_SeqBufferSize) {
return false;
}

ziplen = ALIGN16(g_SeqTable->entries[seq->tracknum].ziplen);
ziplen = ALIGN16(g_SeqTable->entries[seq->tracknum].ziplen);
#if VERSION < VERSION_NTSC_1_0
if (seq->data);
if (seq->data);
#endif
binstart = seq->data;
zipstart = binstart + binlen - ziplen;

dmaExec(zipstart, g_SeqTable->entries[seq->tracknum].romaddr, ziplen);
binstart = seq->data;
zipstart = binstart + binlen - ziplen;

dmaExec(zipstart, g_SeqTable->entries[seq->tracknum].romaddr, ziplen);
ziplen = rzipInflate(zipstart, binstart, scratch);
}

ziplen = rzipInflate(zipstart, binstart, scratch);
#ifndef PLATFORM_N64
preprocessALCMidiHdr(binstart, ziplen);
#endif
Expand Down

0 comments on commit 466c773

Please sign in to comment.