From 06a1730b8a73a0c296c2b58207eebc63dd42fb39 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Mon, 19 Feb 2018 22:48:13 +0100 Subject: [PATCH] - hooking in the PathExpander. --- src/sound/timidity++/common.cpp | 68 ++++++++++++++++++++++++----- src/sound/timidity++/common.h | 5 ++- src/sound/timidity++/configfile.cpp | 4 +- src/sound/timidity++/instrum.cpp | 7 +-- src/sound/timidity++/instrum.h | 2 +- src/sound/timidity++/smplfile.cpp | 8 ++-- src/sound/timidity++/sndfont.cpp | 5 ++- 7 files changed, 75 insertions(+), 24 deletions(-) diff --git a/src/sound/timidity++/common.cpp b/src/sound/timidity++/common.cpp index 1995a1f29..5345dcd1d 100644 --- a/src/sound/timidity++/common.cpp +++ b/src/sound/timidity++/common.cpp @@ -30,10 +30,14 @@ #include #include "m_random.h" #include "common.h" +#include "pathexpander.h" +#include "cmdlib.h" namespace TimidityPlus { +static PathExpander tppPathExpander; + /* This'll allocate memory or die. */ void *safe_malloc(size_t count) @@ -174,17 +178,50 @@ int PathList::pathcmp(const char *p1, const char *p2, int ignore_case) return c1 - c2; } -std::pair PathList::openFile(const char *name) +FileReader *PathList::tryOpenPath(const char *name, bool ismain) +{ + FileReader *fp; + int lumpnum; + + if (ismain) + { + tppPathExpander.openmode = PathExpander::OM_FILEORLUMP; + tppPathExpander.clearPathlist(); +#ifdef _WIN32 + tppPathExpander.addToPathlist("C:\\TIMIDITY"); + tppPathExpander.addToPathlist("\\TIMIDITY"); + tppPathExpander.addToPathlist(progdir); +#else + tppPathExpander.addToPathlist("/usr/local/lib/timidity"); + tppPathExpander.addToPathlist("/etc/timidity"); + tppPathExpander.addToPathlist("/etc"); +#endif + } + + if (!(fp = tppPathExpander.openFileReader(name, &lumpnum))) + return NULL; + + if (ismain) + { + if (lumpnum > 0) + { + tppPathExpander.openmode = PathExpander::OM_LUMP; + tppPathExpander.clearPathlist(); // when reading from a PK3 we don't want to use any external path + } + else + { + tppPathExpander.openmode = PathExpander::OM_FILE; + } + } + return fp; +} + +std::pair PathList::openFile(const char *name, bool ismainfile) { if (name && *name) { /* First try the given name */ - FileReader *fr = new FileReader; - if (fr->Open(name)) - { - return std::make_pair(fr, std::string(name)); - } if (!isAbsPath(name)) { @@ -197,10 +234,20 @@ std::pair PathList::openFile(const char *name) s += '/'; } s += name; - if (fr->Open(s.c_str())) return std::make_pair(fr, s); + auto fr = tryOpenPath(name, ismainfile); + if (fr!= nullptr) return std::make_pair(fr, s); } } - delete fr; + else + { + // an absolute path is never looked up. + FileReader *fr = new FileReader; + if (fr->Open(name)) + { + return std::make_pair(fr, std::string(name)); + } + delete fr; + } } return std::make_pair(nullptr, std::string()); } @@ -218,9 +265,10 @@ int PathList::isAbsPath(const char *name) return 0; } -struct timidity_file *open_file(const char *name, int decompress, int noise_mode, PathList &pathList) + +struct timidity_file *open_file(const char *name, bool ismainfile, PathList &pathList) { - auto file = pathList.openFile(name); + auto file = pathList.openFile(name, ismainfile); if (!file.first) return nullptr; auto tf = new timidity_file; tf->url = file.first; diff --git a/src/sound/timidity++/common.h b/src/sound/timidity++/common.h index bef061a00..61b289ae4 100644 --- a/src/sound/timidity++/common.h +++ b/src/sound/timidity++/common.h @@ -37,6 +37,7 @@ class PathList std::vector paths; int isAbsPath(const char *name); + FileReader *tryOpenPath(const char *name, bool ismain); public: PathList(); @@ -47,7 +48,7 @@ public: paths.resize(1); } static int pathcmp(const char *p1, const char *p2, int ignore_case); - std::pair openFile(const char *name); + std::pair openFile(const char *name, bool ismainfile); }; @@ -66,7 +67,7 @@ enum }; -extern struct timidity_file *open_file(const char *name, int decompress, int noise_mode, PathList &); +extern struct timidity_file *open_file(const char *name, bool, PathList &); extern void close_file(struct timidity_file *tf); extern void skip(struct timidity_file *tf, size_t len); extern char *tf_gets(char *buff, int n, struct timidity_file *tf); diff --git a/src/sound/timidity++/configfile.cpp b/src/sound/timidity++/configfile.cpp index 9e96deb0f..043aaca43 100644 --- a/src/sound/timidity++/configfile.cpp +++ b/src/sound/timidity++/configfile.cpp @@ -617,7 +617,7 @@ char *Instruments::expand_variables(char *string, MBlockList *varbuf, const char } -int Instruments::read_config_file(const char *name, int self, int allow_missing_file) +int Instruments::read_config_file(const char *name, int self, int allow_missing_file, bool ismainfile) { struct timidity_file *tf; char buf[1024], *tmp, *w[MAXWORDS + 1], *cp; @@ -637,7 +637,7 @@ int Instruments::read_config_file(const char *name, int self, int allow_missing_ return READ_CONFIG_RECURSION; } - tf = open_file(name, 1, allow_missing_file ? OF_NORMAL : OF_VERBOSE, pathlist); + tf = open_file(name, ismainfile, pathlist); if (tf == NULL) return allow_missing_file ? READ_CONFIG_FILE_NOT_FOUND : READ_CONFIG_ERROR; diff --git a/src/sound/timidity++/instrum.cpp b/src/sound/timidity++/instrum.cpp index 3339a885d..b1aeced75 100644 --- a/src/sound/timidity++/instrum.cpp +++ b/src/sound/timidity++/instrum.cpp @@ -54,7 +54,7 @@ Instruments::Instruments() bool Instruments::load(const char *config) { - if (read_config_file(config, 0, 0)) + if (read_config_file(config, 0, 0, true)) { init_load_soundfont(); set_default_instrument(); @@ -615,7 +615,7 @@ Instrument *Instruments::load_gus_instrument(char *name, ToneBank *bank, int dr, return ip; } /* Open patch file */ - if (!(tf = open_file(name, 2, OF_NORMAL, pathlist))) { + if (!(tf = open_file(name, false, pathlist))) { #ifdef PATCH_EXT_LIST int name_len, ext_len; static const char *patch_ext[] = PATCH_EXT_LIST; @@ -633,7 +633,8 @@ Instrument *Instruments::load_gus_instrument(char *name, ToneBank *bank, int dr, continue; /* duplicated ext. */ strcpy((char *)tmp, name); strcat((char *)tmp, patch_ext[i]); - if ((tf = open_file((char *)tmp, 1, OF_NORMAL, pathlist))) { + if ((tf = open_file((char *)tmp, false, pathlist))) + { noluck = 0; break; } diff --git a/src/sound/timidity++/instrum.h b/src/sound/timidity++/instrum.h index f8bd51982..560c36b5d 100644 --- a/src/sound/timidity++/instrum.h +++ b/src/sound/timidity++/instrum.h @@ -546,7 +546,7 @@ public: void PrecacheInstruments(const uint16_t *instruments, int count); - int read_config_file(const char *name, int self, int allow_missing_file); + int read_config_file(const char *name, int self, int allow_missing_file, bool ismainfile = false); void set_default_instrument() { diff --git a/src/sound/timidity++/smplfile.cpp b/src/sound/timidity++/smplfile.cpp index cc556bf4c..dfc3d5997 100644 --- a/src/sound/timidity++/smplfile.cpp +++ b/src/sound/timidity++/smplfile.cpp @@ -278,7 +278,7 @@ int Instruments::import_wave_discriminant(char *sample_file) struct timidity_file *tf; char buf[12]; - if ((tf = open_file(sample_file, 1, OF_NORMAL, pathlist)) == NULL) + if ((tf = open_file(sample_file, false, pathlist)) == NULL) return 1; if (tf_read(buf, 12, 1, tf) != 1 || memcmp(&buf[0], "RIFF", 4) != 0 || memcmp(&buf[8], "WAVE", 4) != 0) @@ -309,7 +309,7 @@ int Instruments::import_wave_load(char *sample_file, Instrument *inst) WAVSamplerChunk samplerc = {0,}; GeneralInstrumentInfo instc; - if ((tf = open_file(sample_file, 1, OF_NORMAL, pathlist)) == NULL) + if ((tf = open_file(sample_file, false, pathlist)) == NULL) return 1; if (tf_read(buf, 12, 1, tf) != 1 || memcmp(&buf[0], "RIFF", 4) != 0 || memcmp(&buf[8], "WAVE", 4) != 0) @@ -553,7 +553,7 @@ int Instruments::import_aiff_discriminant(char *sample_file) struct timidity_file *tf; char buf[12]; - if ((tf = open_file(sample_file, 1, OF_NORMAL, pathlist)) == NULL) + if ((tf = open_file(sample_file, false, pathlist)) == NULL) return 1; if (tf_read(buf, 12, 1, tf) != 1 || memcmp(&buf[0], "FORM", 4) != 0 || memcmp(&buf[8], "AIF", 3) != 0 @@ -593,7 +593,7 @@ int Instruments::import_aiff_load(char *sample_file, Instrument *inst) AIFFLoopInfo loop_info = {0,0,0}; AIFFMarkerData *marker_data; - if ((tf = open_file(sample_file, 1, OF_NORMAL, pathlist)) == NULL) + if ((tf = open_file(sample_file, false, pathlist)) == NULL) return 1; if (tf_read(buf, 12, 1, tf) != 1 || memcmp(&buf[0], "FORM", 4) != 0 || memcmp(&buf[8], "AIF", 3) != 0 diff --git a/src/sound/timidity++/sndfont.cpp b/src/sound/timidity++/sndfont.cpp index 75326fe13..e7f069d4e 100644 --- a/src/sound/timidity++/sndfont.cpp +++ b/src/sound/timidity++/sndfont.cpp @@ -265,7 +265,7 @@ void Instruments::init_sf(SFInsts *rec) SFInfo sfinfo; int i; - if ((rec->tf = open_file(rec->fname, 1, OF_VERBOSE, pathlist)) == NULL) { + if ((rec->tf = open_file(rec->fname, false, pathlist)) == NULL) { ctl_cmsg(CMSG_ERROR, VERB_NORMAL, "Can't open soundfont file %s", rec->fname); end_soundfont(rec); @@ -362,7 +362,8 @@ Instrument *Instruments::try_load_soundfont(SFInsts *rec, int order, int bank,in if (rec->tf == NULL) { if (rec->fname == NULL) return NULL; - if ((rec->tf = open_file(rec->fname, 1, OF_VERBOSE, pathlist)) == NULL) { + if ((rec->tf = open_file(rec->fname, false, pathlist)) == NULL) + { ctl_cmsg(CMSG_ERROR, VERB_NORMAL, "Can't open soundfont file %s", rec->fname); end_soundfont(rec);