Skip to content

DX Song Search DTAFunction #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/GlobalSymbols.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ typedef struct _GlobalSymbols
Symbol rb3e_get_album;
Symbol rb3e_get_origin;
Symbol rb3e_get_genre;
Symbol rb3e_search_song_name;

// modifiers
Symbol forceHopos;
Expand Down
103 changes: 103 additions & 0 deletions source/DTAFunctions.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,28 @@
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include "ports.h"
#include "GlobalSymbols.h"
#include "config.h"
#include "net_events.h"
#include "rb3/Data.h"
#include "rb3/SongMetadata.h"
#include "rb3/BandSongMgr.h"
#include "MusicLibrary.h"
#include "rb3enhanced.h"
#include "rb3/RockCentralGateway.h"

typedef struct _song_list_vector {
int *start;
int *end;
int unk1;
} song_list_vector;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This struct layout might cause issues on Wii, as Backbone's modifications to STLPort define a different layout for std::vector's members.


static song_list_vector dta_song_list = { 0 };
Copy link

@DarkRTA DarkRTA May 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should probably go on the stack and be recreated every time. as of right now deleting a song will cause a use-after-free next time you search.

i'm not familiar enough with this codebase to give any specific guidance here so im deferring to @InvoxiPlayGames

Copy link

@DarkRTA DarkRTA May 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

turns out this isnt a uaf since its just a vector of song ids, but it should prob be fixed anyway as it'll have song ids that no longer exist after deleting a song

extern char *strtok(char *s, const char *delim);
extern char *strstr(const char *haystack, const char *needle);

DataNode *PrintToDebugger(DataNode *node, DataArray *args)
{
Expand Down Expand Up @@ -287,6 +301,94 @@ DataNode *DTAGetOrigin(DataNode *node, DataArray *args)
return node;
}

static void sanitize_string(const char *input, char *output, size_t out_size)
{
size_t j = 0;
size_t i;
for (i = 0; input[i] && j + 1 < out_size; ++i) {
unsigned char c = input[i];
if (isalnum(c) || isspace(c)) {
output[j++] = (char)tolower(c);
}
}
while (j > 0 && isspace((unsigned char)output[j - 1])) --j;
output[j] = '\0';
}

DataNode *DTA_SearchSongName(DataNode *node, DataArray *args) {
DataNode *firstArg;
Symbol sym_failed;
const char *needle;
int *list;
int count;
int i, j;
int matchedByTitle = 0, allFound = 0;
SongMetadata *md;
char cleanNeedle[64], cleanTitle[256], cleanArtist[256], tempBuf[64];
char *tokens[16], *tok;
int tokenCount;

SymbolConstruct(&sym_failed, "rb3e_search_failed");
node->type = SYMBOL; node->value.string = sym_failed.sym;

firstArg = DataNodeEvaluate(&args->mNodes->n[1]);
if (firstArg->type != STRING_VALUE) return node;
needle = *((const char **)firstArg->value.object);

sanitize_string(needle, cleanNeedle, sizeof(cleanNeedle));

if (!dta_song_list.start)
SongMgrGetRankedSongs((BandSongMgr *)PORT_THESONGMGR, &dta_song_list, 0, 0);
list = dta_song_list.start;
count = (int)(dta_song_list.end - list);

tokenCount = 0;
strncpy(tempBuf, cleanNeedle, sizeof(tempBuf)); tempBuf[sizeof(tempBuf)-1] = '\0';
tok = strtok(tempBuf, " \t");
while (tok && tokenCount < 16) { tokens[tokenCount++] = tok; tok = strtok(NULL, " \t"); }

for (i = 0; i < count; ++i) {
md = GetMetadata((BandSongMgr *)PORT_THESONGMGR, list[i]);
if (!md) continue;

sanitize_string(md->title.buf, cleanTitle, sizeof(cleanTitle));
sanitize_string(md->artist.buf, cleanArtist, sizeof(cleanArtist));

if (strcmp(cleanTitle, cleanNeedle) == 0) matchedByTitle = 0;
else if (strcmp(cleanArtist, cleanNeedle) == 0) matchedByTitle = 1;
else {
allFound = 1;
for (j = 0; j < tokenCount; ++j) {
if (strstr(cleanTitle, tokens[j])) { if (j==0) matchedByTitle = 0; }
else if (strstr(cleanArtist, tokens[j])) { if (j==0) matchedByTitle = 1; }
else { allFound = 0; break; }
}
if (!allFound) continue;
}

ExecuteDTA(PORT_ROCKCENTRALGATEWAY, "{{music_library get view_settings_provider} refresh_all_settings}");
ExecuteDTA(PORT_ROCKCENTRALGATEWAY, "{song_select_filter_panel filter_enter}");
ExecuteDTA(PORT_ROCKCENTRALGATEWAY, "{{music_library get view_settings_provider} select_setting 1}");
{
char buf[80];
sprintf(buf, "{{music_library get view_settings_provider} select_setting_option %d}", matchedByTitle);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line of code can potentially blow up the stack

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added more to buffer as per your rec

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm now

ExecuteDTA(PORT_ROCKCENTRALGATEWAY, buf);
}
ExecuteDTA(PORT_ROCKCENTRALGATEWAY, "{music_library report_sort_and_filters}");
ExecuteDTA(PORT_ROCKCENTRALGATEWAY, "{song_select_filter_panel filter_exit}");

if (matchedByTitle) {
MusicLibrarySelectHeading(md->artist.buf);
} else {
MusicLibrarySelectSong(md->shortname.sym);
}
node->value.string = md->shortname.sym;
return node;
}

return node;
}

#ifdef RB3E_XBOX
// this function is inlined on the Xbox version, so we re-create it
void DataRegisterFunc(Symbol name, DTAFunction_t func)
Expand All @@ -312,5 +414,6 @@ void AddDTAFunctions()
DataRegisterFunc(globalSymbols.rb3e_get_album, DTAGetAlbum);
DataRegisterFunc(globalSymbols.rb3e_get_origin, DTAGetOrigin);
DataRegisterFunc(globalSymbols.rb3e_get_genre, DTAGetGenre);
DataRegisterFunc(globalSymbols.rb3e_search_song_name, DTA_SearchSongName);
RB3E_MSG("Added DTA functions!", NULL);
}
1 change: 1 addition & 0 deletions source/GlobalSymbols.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ void InitGlobalSymbols()
SymbolConstruct(&globalSymbols.rb3e_get_album, "rb3e_get_album");
SymbolConstruct(&globalSymbols.rb3e_get_origin, "rb3e_get_origin");
SymbolConstruct(&globalSymbols.rb3e_get_genre, "rb3e_get_genre");
SymbolConstruct(&globalSymbols.rb3e_search_song_name, "rb3e_search_song_name");

SymbolConstruct(&globalSymbols.blackBackground, "mod_black_background");
SymbolConstruct(&globalSymbols.colorShuffle, "mod_color_shuffle");
Expand Down