//
// MusicDownloadManager::downloadSong from 0x10058ed00 to 0x10058f18d (1165 bytes)
//
// 5 xrefs:
// > CustomSongWidget::onDownload @ 0x10061bc17
// > CustomSongWidget::startDownload @ 0x10061f165
// > CustomSongWidget::loadSongInfoFinished @ 0x10062038a
// > CustomSongWidget::loadSongInfoFinished @ 0x1006204bc
// > CustomSongWidget::processNextMultiAsset @ 0x1006212d8
//
// This function is hookable on this platform!
//
// Using BromaIDA 8.0.0 @ https://github.com/Stazzical/BromaIDA
// Using bindings at commit ba9f177b at Thu Jul 16 22:52:06 2026 from https://github.com/geode-sdk/bindings
//
void __fastcall MusicDownloadManager::downloadSong(MusicDownloadManager *a1, int a2)
{
double v2; // xmm0_8
cocos2d::CCString *v4; // rax
const char *CString; // r14
unsigned int v6; // r12d
__int64 v7; // rdi
MusicDownloadManager *v8; // rdi
char *v9; // r12
SongInfoObject *SongInfoObject; // rax
std::string *p_m_songUrl; // rax
cocos2d::CCString *v12; // rax
const char *v13; // rax
cocos2d::CCString *v14; // rax
size_t v15; // rax
size_t v16; // r13
char *v17; // r14
const char *v18; // rsi
size_t v19; // r12
const char *v20; // r14
char *v21; // r12
const std::string::value_type *data; // rsi
cocos2d::CCString *v23; // rax
const char *v24; // rax
cocos2d::extension::CCHttpClient *Instance; // rax
char v26; // [rsp+0h] [rbp-70h]
char v27; // [rsp+0h] [rbp-70h]
const char *v28; // [rsp+0h] [rbp-70h]
const char *__src; // [rsp+8h] [rbp-68h]
__int128 __dst; // [rsp+10h] [rbp-60h] BYREF
void *__p; // [rsp+20h] [rbp-50h]
std::string v32; // [rsp+30h] [rbp-40h] BYREF
if ( a2 >= 10000001 )
{
MusicDownloadManager::downloadCustomSong(a1, a2);
return;
}
v4 = (cocos2d::CCString *)sub_1007AB580("d_%i", v26);
CString = cocos2d::CCString::getCString(v4);
if ( !MusicDownloadManager::getDLObject(a1, CString) )
{
if ( (unsigned int)sub_100720070(a1->m_musicDownloadDelegates) )
{
v6 = 0;
do
{
v7 = *(_QWORD *)(sub_1007200C0(a1->m_musicDownloadDelegates, v6) + 320);
if ( v7 )
(*(void (__fastcall **)(__int64, _QWORD))(*(_QWORD *)v7 + 16LL))(v7, (unsigned int)a2);
++v6;
}
while ( v6 < (unsigned int)sub_100720070(a1->m_musicDownloadDelegates) );
}
*(_WORD *)&v32.__r_.__value_.__l.0 = 0;
v8 = (MusicDownloadManager *)qword_1009A2C38;
if ( !qword_1009A2C38 )
{
v9 = (char *)operator new(0x230u);
cocos2d::CCNode::CCNode((__int64)v9);
*(_QWORD *)v9 = off_100970158;
*((_QWORD *)v9 + 40) = off_1009704D8;
v2 = 0.0;
*((_OWORD *)v9 + 28) = 0;
*(_OWORD *)(v9 + 328) = 0;
*(_OWORD *)(v9 + 344) = 0;
*((_DWORD *)v9 + 90) = 0;
*((_OWORD *)v9 + 23) = 0;
*((_OWORD *)v9 + 24) = 0;
*((_OWORD *)v9 + 25) = 0;
v9[416] = 0;
*(_OWORD *)(v9 + 424) = 0;
*((_QWORD *)v9 + 55) = v9 + 448;
*((_OWORD *)v9 + 30) = 0;
*((_OWORD *)v9 + 31) = 0;
*((_QWORD *)v9 + 58) = 0;
*(_QWORD *)(v9 + 471) = 0;
*((_DWORD *)v9 + 128) = 1065353216;
*(_OWORD *)(v9 + 520) = 0;
*(_OWORD *)(v9 + 536) = 0;
*((_DWORD *)v9 + 138) = 1065353216;
qword_1009A2C38 = (__int64)v9;
MusicDownloadManager::init((MusicDownloadManager *)v9);
v8 = (MusicDownloadManager *)qword_1009A2C38;
}
SongInfoObject = MusicDownloadManager::getSongInfoObject(v8, a2);
if ( SongInfoObject )
{
if ( (SongInfoObject->m_songUrl.m_data.m_short.sizex2 & 1) != 0 )
{
sub_100006B70(
&__dst,
SongInfoObject->m_songUrl.m_data.m_long.longStorage,
SongInfoObject->m_songUrl.m_data.m_long.size,
v2);
}
else
{
p_m_songUrl = &SongInfoObject->m_songUrl;
__p = p_m_songUrl->m_data.m_long.longStorage;
__dst = *(_OWORD *)&p_m_songUrl->m_data.m_short.sizex2;
}
if ( (*(_BYTE *)&v32.__r_.__value_.__s.0 & 1) != 0 )
operator delete(v32.__r_.__value_.__l.__data_);
v32.__r_.__value_.__r.__words[2] = (std::string::size_type)__p;
v2 = *(double *)&__dst;
*(_OWORD *)&v32.__r_.__value_.__l.0 = __dst;
}
if ( (*(_BYTE *)&v32.__r_.__value_.__s.0 & 1) != 0 )
{
if ( v32.__r_.__value_.__l.__size_ )
goto LABEL_23;
}
else if ( *(_BYTE *)&v32.__r_.__value_.__l.0 >> 1 )
{
goto LABEL_23;
}
v12 = (cocos2d::CCString *)sub_1007AB580("https://www.newgrounds.com/audio/download/%i", v27);
v13 = cocos2d::CCString::getCString(v12);
std::string::assign(&v32, v13);
LABEL_23:
v14 = (cocos2d::CCString *)sub_1007AB580("%i.mp3", (char)CString);
__src = cocos2d::CCString::getCString(v14);
v15 = strlen(__src);
if ( v15 >= 0xFFFFFFFFFFFFFFF8LL )
sub_100006BF0(v2);
v16 = v15;
if ( v15 >= 0x17 )
{
v19 = 26;
if ( (v15 | 7) != 0x17 )
v19 = (v15 | 7) + 1;
v18 = __src;
v17 = (char *)operator new(v19);
__p = v17;
*(_QWORD *)&__dst = v19 | 1;
}
else
{
LOBYTE(__dst) = 2 * v15;
v17 = (char *)&__dst + 1;
v18 = __src;
if ( !v15 )
goto LABEL_31;
}
memmove(v17, v18, v16);
LABEL_31:
v17[v16] = 0;
v20 = v28;
v21 = (char *)operator new(0xD8u);
cocos2d::CCObject::CCObject((__int64)v21);
*(_QWORD *)v21 = off_1008B9708;
*(_OWORD *)(v21 + 168) = 0;
*((_QWORD *)v21 + 23) = 0;
*((_OWORD *)v21 + 4) = 0;
*((_OWORD *)v21 + 5) = 0;
*((_OWORD *)v21 + 6) = 0;
*((_OWORD *)v21 + 7) = 0;
*((_QWORD *)v21 + 16) = 0;
*((_DWORD *)v21 + 14) = 5;
*((_DWORD *)v21 + 48) = 0;
v21[196] = 0;
*((_QWORD *)v21 + 25) = 0x7800000000LL;
*((_DWORD *)v21 + 52) = 0;
*(_OWORD *)(v21 + 136) = 0;
*(_OWORD *)(v21 + 152) = 0;
if ( (*(_BYTE *)&v32.__r_.__value_.__s.0 & 1) != 0 )
data = v32.__r_.__value_.__l.__data_;
else
data = v32.__r_.__value_.__s.__data_;
std::string::assign((std::string *)(v21 + 64), data);
*((_DWORD *)v21 + 14) = 0;
*((_QWORD *)v21 + 17) = a1;
*((_QWORD *)v21 + 18) = MusicDownloadManager::onDownloadSongCompleted;
*((_QWORD *)v21 + 19) = 0;
cocos2d::CCObject::retain(a1);
v23 = (cocos2d::CCString *)sub_1007AB580("%i", (char)v28);
v24 = cocos2d::CCString::getCString(v23);
std::string::assign((std::string *)(v21 + 112), v24);
Instance = cocos2d::extension::CCHttpClient::getInstance();
cocos2d::extension::CCHttpClient::send(Instance, (cocos2d::extension::CCHttpRequest *)v21);
cocos2d::CCObject::release((cocos2d::CCObject *)v21);
MusicDownloadManager::addDLToActive(a1, v20, (cocos2d::CCObject *)v21);
if ( (__dst & 1) != 0 )
operator delete(__p);
if ( (*(_BYTE *)&v32.__r_.__value_.__s.0 & 1) != 0 )
operator delete(v32.__r_.__value_.__l.__data_);
}
}