From 01eaab1f01d24bb50d3e608b503bfd2c142ce113 Mon Sep 17 00:00:00 2001 From: bnnm Date: Mon, 2 Nov 2020 01:07:39 +0100 Subject: [PATCH] Unity plugin titles and improve TXTP handling --- cli/vgmstream_cli.c | 7 ++++++- fb2k/foo_vgmstream.cpp | 31 ++++++------------------------- src/meta/txtp.c | 4 ++++ src/plugins.c | 32 +++++++++++++++++++++++++------- src/plugins.h | 1 + src/vgmstream.h | 4 ++++ winamp/in_vgmstream.c | 42 ++++++++++++++---------------------------- 7 files changed, 60 insertions(+), 61 deletions(-) diff --git a/cli/vgmstream_cli.c b/cli/vgmstream_cli.c index 86092680..4f3aee36 100644 --- a/cli/vgmstream_cli.c +++ b/cli/vgmstream_cli.c @@ -386,11 +386,16 @@ static void print_tags(cli_config* cfg) { static void print_title(VGMSTREAM* vgmstream, cli_config* cfg) { char title[1024]; + vgmstream_title_t tcfg = {0}; if (!cfg->show_title) return; - vgmstream_get_title(title, sizeof(title), cfg->infilename, vgmstream, NULL); + tcfg.force_title = 0; + tcfg.subsong_range = 0; + tcfg.remove_extension = 0; + + vgmstream_get_title(title, sizeof(title), cfg->infilename, vgmstream, &tcfg); printf("title: %s\n", title); } diff --git a/fb2k/foo_vgmstream.cpp b/fb2k/foo_vgmstream.cpp index fcb4ef56..ea73207f 100644 --- a/fb2k/foo_vgmstream.cpp +++ b/fb2k/foo_vgmstream.cpp @@ -429,8 +429,7 @@ void input_vgmstream::get_subsong_info(t_uint32 p_subsong, pfc::string_base & ti int num_samples = vgmstream_get_samples(infostream); *length_in_ms = num_samples*1000LL / infostream->sample_rate; - char temp[1024]; - describe_vgmstream(infostream, temp, 1024); + describe_vgmstream(infostream, temp, sizeof(temp)); description = temp; } } @@ -438,30 +437,12 @@ void input_vgmstream::get_subsong_info(t_uint32 p_subsong, pfc::string_base & ti /* infostream gets added with index 0 (other) or 1 (current) */ if (infostream && title) { - const char *p = filename + strlen(filename); - while (*p != '\\' && p >= filename) p--; - p++; - const char *e = filename + strlen(filename); - while (*e != '.' && e >= filename) e--; - title.set_string(p, e - p); /* name without ext */ + vgmstream_title_t tcfg = {0}; + tcfg.remove_extension = 1; - const char* info_name = infostream->stream_name; - int info_streams = infostream->num_streams; - int info_subsong = infostream->stream_index; - if (info_subsong == 0) - info_subsong = 1; - - /* show number if file has more than 1 subsong */ - if (info_streams > 1) { - sprintf(temp,"#%d",info_subsong); - title += temp; - } - - /* show name if file has subsongs (implicitly shows also for TXTP) */ - if (info_name[0] != '\0' && info_streams > 0) { - sprintf(temp," (%s)",info_name); - title += temp; - } + const char* filename_str = filename; + vgmstream_get_title(temp, sizeof(temp), filename_str, infostream, &tcfg); + title = temp; } // and only close if was querying a new subsong diff --git a/src/meta/txtp.c b/src/meta/txtp.c index 9a98e881..3cbb59de 100644 --- a/src/meta/txtp.c +++ b/src/meta/txtp.c @@ -178,6 +178,10 @@ VGMSTREAM* init_vgmstream_txtp(STREAMFILE* sf) { /* should result in a final, single vgmstream possibly containing multiple vgmstreams */ vgmstream = txtp->vgmstream[0]; + /* flags for title config */ + vgmstream->config.is_txtp = 1; + vgmstream->config.is_mini_txtp = (get_streamfile_size(sf) == 0); + clean_txtp(txtp, 0); return vgmstream; diff --git a/src/plugins.c b/src/plugins.c index 085c3dad..f9199a3c 100644 --- a/src/plugins.c +++ b/src/plugins.c @@ -68,6 +68,7 @@ void vgmstream_get_title(char* buf, int buf_len, const char* filename, VGMSTREAM char* pos2; char temp[1024]; + buf[0] = '\0'; /* name without path */ pos = strrchr(filename, '\\'); @@ -80,23 +81,31 @@ void vgmstream_get_title(char* buf, int buf_len, const char* filename, VGMSTREAM strncpy(buf, pos, buf_len); /* name without extension */ - pos2 = strrchr(buf, '.'); - if (pos2) - pos2[0] = '\0'; + if (cfg->remove_extension) { + pos2 = strrchr(buf, '.'); + if (pos2 && strlen(pos2) < 15) /* too big extension = file name probably has a dot in the middle */ + pos2[0] = '\0'; + } { const char* stream_name = vgmstream->stream_name; int total_subsongs = vgmstream->num_streams; int target_subsong = vgmstream->stream_index; //int is_first = vgmstream->stream_index == 0; - //int is_txtp = ; //todo don't show number/name for txtp but show for mini-txtp int show_name; + /* special considerations for TXTP: + * - full txtp: don't show subsong number, nor name (assumes one names .txtp as wanted) + * - mini txtp: don't show subsong number, but show name (assumes one choses song #n in filename, but wants title) + */ + int full_txtp = vgmstream->config.is_txtp && !vgmstream->config.is_mini_txtp; + int mini_txtp = vgmstream->config.is_mini_txtp; + if (target_subsong == 0) target_subsong = 1; /* show number if file has more than 1 subsong */ - if (total_subsongs > 1) { + if (total_subsongs > 1 && !(full_txtp || mini_txtp)) { if (cfg && cfg->subsong_range) snprintf(temp, sizeof(temp), "%s#1~%i", buf, total_subsongs); else @@ -105,13 +114,19 @@ void vgmstream_get_title(char* buf, int buf_len, const char* filename, VGMSTREAM } /* show name for some cases */ - show_name = (total_subsongs > 0 && (!cfg || !cfg->subsong_range)) || - (cfg && cfg->force_title); + show_name = (total_subsongs > 0) && (!cfg || !cfg->subsong_range); + if (full_txtp) + show_name = 0; + if (cfg && cfg->force_title) + show_name = 1; + if (stream_name[0] != '\0' && show_name) { snprintf(temp, sizeof(temp), "%s (%s)", buf, stream_name); strncpy(buf, temp, buf_len); } } + + buf[buf_len - 1] = '\0'; } @@ -174,6 +189,9 @@ static void load_default_config(play_config_t* def, play_config_t* tcfg) { copy_time(&def->trim_begin_set, &def->trim_begin, &def->trim_begin_s, &tcfg->trim_begin_set, &tcfg->trim_begin, &tcfg->trim_begin_s); copy_time(&def->trim_end_set, &def->trim_end, &def->trim_end_s, &tcfg->trim_end_set, &tcfg->trim_end, &tcfg->trim_end_s); copy_time(&def->body_time_set, &def->body_time, &def->body_time_s, &tcfg->body_time_set, &tcfg->body_time, &tcfg->body_time_s); + + def->is_mini_txtp = tcfg->is_mini_txtp; + def->is_txtp = tcfg->is_txtp; } static void load_player_config(play_config_t* def, vgmstream_cfg_t* vcfg) { diff --git a/src/plugins.h b/src/plugins.h index fff652e2..b31b4862 100644 --- a/src/plugins.h +++ b/src/plugins.h @@ -71,6 +71,7 @@ void vgmstream_set_play_forever(VGMSTREAM* vgmstream, int enabled); typedef struct { int force_title; int subsong_range; + int remove_extension; } vgmstream_title_t; /* get a simple title for plugins */ diff --git a/src/vgmstream.h b/src/vgmstream.h index cb0afe38..8b572ebb 100644 --- a/src/vgmstream.h +++ b/src/vgmstream.h @@ -821,6 +821,9 @@ typedef struct { int fade_time_set; int pad_end_set; + /* for lack of a better place... */ + int is_txtp; + int is_mini_txtp; } play_config_t; @@ -843,6 +846,7 @@ typedef struct { int32_t play_duration; /* total samples that the stream lasts (after applying all config) */ int32_t play_position; /* absolute sample where stream is */ + } play_state_t; diff --git a/winamp/in_vgmstream.c b/winamp/in_vgmstream.c index cc62007f..808fd7f6 100644 --- a/winamp/in_vgmstream.c +++ b/winamp/in_vgmstream.c @@ -150,7 +150,8 @@ static void wa_ichar_to_char(char *dst, size_t dstsize, const in_char *wsrc) { //int size_needed = WideCharToMultiByte(CP_UTF8,0, src,-1, NULL,0, NULL, NULL); WideCharToMultiByte(CP_UTF8,0, wsrc,-1, dst,dstsize, NULL, NULL); #else - strcpy(dst,wsrc); + strncpy(dst, wsrc, dstsize); + dst[dstsize - 1] = '\0'; #endif } @@ -160,7 +161,8 @@ static void wa_char_to_ichar(in_char *wdst, size_t wdstsize, const char *src) { //int size_needed = MultiByteToWideChar(CP_UTF8,0, src,-1, NULL,0); MultiByteToWideChar(CP_UTF8,0, src,-1, wdst,wdstsize); #else - strcpy(wdst,src); + strncpy(wdst, src, wdstsize); + wdst[wdstsize - 1] = '\0'; #endif } @@ -891,43 +893,27 @@ static void build_extension_list(char *winamp_list, int winamp_list_size) { /* unicode utils */ static void get_title(in_char* dst, int dst_size, const in_char* fn, VGMSTREAM* infostream) { - in_char* basename; - in_char buffer[PATH_LIMIT]; in_char filename[PATH_LIMIT]; - //int stream_index = 0; + char buffer[PATH_LIMIT]; + char filename_utf8[PATH_LIMIT]; parse_fn_string(fn, NULL, filename,PATH_LIMIT); //parse_fn_int(fn, wa_L("$s"), &stream_index); - basename = (in_char*)filename + wa_strlen(filename); /* find end */ - while (*basename != '\\' && basename >= filename) /* and find last "\" */ - basename--; - basename++; - wa_strcpy(dst,basename); + wa_ichar_to_char(filename_utf8, PATH_LIMIT, filename); /* infostream gets added at first with index 0, then once played it re-adds proper numbers */ if (infostream) { - const char* info_name = infostream->stream_name; - int info_streams = infostream->num_streams; - int info_subsong = infostream->stream_index; + vgmstream_title_t tcfg = {0}; int is_first = infostream->stream_index == 0; - /* show number if file has more than 1 subsong */ - if (info_streams > 1) { - if (is_first) - wa_snprintf(buffer,PATH_LIMIT, wa_L("#1~%i"), info_streams); - else - wa_snprintf(buffer,PATH_LIMIT, wa_L("#%i"), info_subsong); - wa_strcat(dst,buffer); - } + tcfg.force_title = settings.force_title; + tcfg.subsong_range = is_first; + tcfg.remove_extension = 1; - /* show name if file has subsongs (implicitly shows also for TXTP) */ - if (info_name[0] != '\0' && ((info_streams > 0 && !is_first) || info_streams == 1 || settings.force_title)) { - in_char stream_name[PATH_LIMIT]; - wa_char_to_ichar(stream_name, PATH_LIMIT, info_name); - wa_snprintf(buffer,PATH_LIMIT, wa_L(" (%s)"), stream_name); - wa_strcat(dst,buffer); - } + vgmstream_get_title(buffer, sizeof(buffer), filename_utf8, infostream, &tcfg); + + wa_char_to_ichar(dst, dst_size, buffer); } }