/*---------------------------------------------------------------------------* Project: Horizon File: TitleDownloader.cpp Copyright 2009 Nintendo. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Rev$ *---------------------------------------------------------------------------*/ #include #include #include #include #include "FileName.h" #include "CommonLogger.h" #include "HeapManager.h" #include "TitleDownloader.h" #include "Shop.h" #include "SdReaderWriter.h" namespace { bit8 s_buffer1[400 * 1024]; const size_t TITLE_DOWNLOADER_STACK_SIZE = 0x1000; nn::os::Thread s_TitleDownloaderThread; nn::os::StackBuffer s_TitleDownloaderThreadStack; nn::fs::MediaType GetMediaType(const ES_NAMESPACE::ESTitleId titleId) { return (nn::CTR::IsTwlApp(titleId)) ? nn::fs::MEDIA_TYPE_NAND : nn::fs::MEDIA_TYPE_SDMC; } const char *GetAttribute(EC_NAMESPACE::ECNameValuePair *attributes, u32 nAttributes, const char *attributeName) { for(int i=0; inEntries == 1) { *entry = &(titleCatalog->entries[0]); } else { return nn::MakeStatusResult(nn::Result::SUMMARY_NOT_FOUND, nn::Result::MODULE_COMMON, nn::Result::DESCRIPTION_NOT_FOUND); } } return result; } nn::Result GetTitleConfig(const ES_NAMESPACE::ESTitleId titleId, nn::nim::TitleConfig *titleConfig) { EC_NAMESPACE::ECTitleCatalogEntry *entry; NN_UTIL_RETURN_IF_FAILED(GetEntry(titleId, &entry)); titleConfig->titleId=titleId; titleConfig->version=std::strtoull(GetAttribute(entry->attributes, entry->nAttributes, "Version"), NULL, 10); titleConfig->ratingAge=0; titleConfig->media=GetMediaType(titleId); NN_LOG("titleId : 0x%016llx\n", titleConfig->titleId); NN_LOG("version : %lld\n" , titleConfig->version); NN_LOG("ratingAge : %d\n" , titleConfig->ratingAge); NN_LOG("media : %d\n" , titleConfig->media); return nn::ResultSuccess(); } } // namespace namespace ConsoleRestore { nn::Result TitleDownloader::m_Result = nn::ResultSuccess(); void TitleDownloaderThreadFunc() { TitleDownloader TwlTitleDownloader; TwlTitleDownloader.Start(); } void StartTitleDownload() { s_TitleDownloaderThread.Start(TitleDownloaderThreadFunc, s_TitleDownloaderThreadStack); } bool DownloadTitleFinished() { return s_TitleDownloaderThread.IsValid() && !s_TitleDownloaderThread.IsAlive(); } bool DownloadTitleSucceeded() { return TitleDownloader::m_Result.IsSuccess() && GetShopOperationSingleResult().IsSuccess(); } TitleDownloader::TitleDownloader() : m_TwlTiteNum(0) { for(u32 i = 0; i < TWL_IMPORTABLE_TITLE_MAX; i++) { m_ProgramIdList[i] = 0; } } TitleDownloader::~TitleDownloader() { } nn::Result WaitCancelled() { nn::nim::TitleProgress progress; while(true) { // キャンセルがResultとして返ってくる / ダウンロード終了まで待つ NN_UTIL_RETURN_IF_FAILED(nn::nim::Shop::GetProgress(&progress)); if(progress.lastResult==nn::nim::ResultCancelRequested() || progress.state==nn::nim::TITLE_STATE_FINISHED) { break; } nn::os::Thread::Sleep(nn::fnd::TimeSpan::FromMilliSeconds(100)); } return nn::ResultSuccess(); } void WaitShopOperationAndFinalize() { while (!IsShopOperationSingleFinished()) { nn::os::Thread::Sleep(nn::fnd::TimeSpan::FromMilliSeconds(100)); } FinalizeShopOperationSingle(); } void TitleDownloader::Start() { nn::Result result; result = ListUp(); if(result.IsFailure()) { return; } for(u8 i = 0; i < m_TwlTiteNum; i++) { StartShopOperationSingle(SHOP_OPERATION_CONNECT_WITHOUT_CLOSE); WaitShopOperationAndFinalize(); nn::nim::TitleConfig config; result = GetTitleConfig(m_ProgramIdList[i], &config); COMMON_LOGGER_RESULT_IF_FAILED(result); if (result.IsSuccess()) { StartShopOperationSingle(SHOP_OPERATION_DOWNLOAD_TITLE, config); WaitShopOperationAndFinalize(); } } } nn::Result TitleDownloader::ListUp() { COMMON_LOGGER("Read TwlTitle List.\n"); size_t heapSize = common::HeapManager::GetHeap()->GetAllocatableSize(); char* titleListBuf = reinterpret_cast (common::HeapManager::GetHeap()->Allocate(heapSize)); size_t readSize = 0; if (titleListBuf != NULL) { common::SdReaderWriter sdReader; m_Result = sdReader.ReadBufWithCmac(common::TWL_TITLELIST_PATHNAME, titleListBuf, heapSize, &readSize); COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(m_Result); if (m_Result.IsSuccess()) { u32 listHead = 0; for (u32 i = 0; i < readSize; i++) { if (titleListBuf[i] == '\n') { char ProgramIdStr[32]; char *error; std::memcpy(ProgramIdStr, &titleListBuf[listHead], i - listHead); m_ProgramIdList[m_TwlTiteNum] = std::strtoull(ProgramIdStr, &error, 16); m_TwlTiteNum++; NN_LOG("%016llx\n", m_ProgramIdList[m_TwlTiteNum - 1]); listHead = i + 1; } } } common::HeapManager::GetHeap()->Free(titleListBuf); } else { m_Result = nn::Result(nn::Result::LEVEL_FATAL, nn::Result::SUMMARY_OUT_OF_RESOURCE, nn::Result::MODULE_COMMON, nn::Result::DESCRIPTION_OUT_OF_MEMORY); } return m_Result; } }