From: TimePath Date: Sun, 8 Apr 2018 11:31:48 +0000 (+1000) Subject: Sounds: cache _Sound_fixpath() result, reduces fexists calls X-Git-Tag: xonotic-v0.8.5~2194 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=2aad6fa52ab8d956b98da333215caa3a6d9ee0bf;p=xonotic%2Fxonotic-data.pk3dir.git Sounds: cache _Sound_fixpath() result, reduces fexists calls --- diff --git a/qcsrc/common/sounds/sound.qh b/qcsrc/common/sounds/sound.qh index 8c4aecbda..036c08670 100644 --- a/qcsrc/common/sounds/sound.qh +++ b/qcsrc/common/sounds/sound.qh @@ -92,40 +92,55 @@ const float VOL_MUFFLED = 0.35; } \ } MACRO_END +string _Sound_fixpath(string base) +{ + if (base == "") return string_null; +#ifdef SVQC + return strcat(base, ".wav"); // let the client engine decide +#else +#define extensions(x) \ + x(wav) \ + x(ogg) \ + x(flac) \ + /**/ +#define tryext(ext) { \ + string s = strcat(base, "." #ext); \ + if (fexists(strcat("sound/", s))) { \ + return s; \ + } \ + } + extensions(tryext); + LOG_WARNF("Missing sound: \"%s\"", strcat("sound/", base)); +#undef tryext +#undef extensions + return string_null; +#endif +} + CLASS(Sound, Object) ATTRIB(Sound, m_id, int, 0); ATTRIB(Sound, sound_str, string()); + ATTRIB(Sound, sound_str_, string); CONSTRUCTOR(Sound, string() path) { CONSTRUCT(Sound); this.sound_str = path; } - #define Sound_fixpath(this) _Sound_fixpath((this).sound_str()) - string _Sound_fixpath(string base) - { - if (base == "") return string_null; -#ifdef SVQC - return strcat(base, ".wav"); // let the client engine decide -#else - #define extensions(x) \ - x(wav) \ - x(ogg) \ - x(flac) \ - /**/ - #define tryext(ext) { string s = strcat(base, "." #ext); if (fexists(strcat("sound/", s))) return s; } - extensions(tryext); - LOG_WARNF("Missing sound: \"%s\"", strcat("sound/", base)); - #undef tryext - #undef extensions - return string_null; -#endif - } METHOD(Sound, sound_precache, void(Sound this)) { TC(Sound, this); - string s = Sound_fixpath(this); + string s = _Sound_fixpath(this.sound_str()); if (!s) return; profile(sprintf("precache_sound(\"%s\")", s)); precache_sound(s); + strcpy(this.sound_str_, s); } ENDCLASS(Sound) + +entity _Sound_fixpath_this; +string _Sound_fixpath_cached; +#define Sound_fixpath(this) ( \ + _Sound_fixpath_this = (this), \ + _Sound_fixpath_cached = _Sound_fixpath_this.sound_str_, \ + _Sound_fixpath_cached ? _Sound_fixpath_cached : _Sound_fixpath(_Sound_fixpath_this.sound_str()) \ +)