ctr_Repair/trunk/ConsoleDataMigration/sources/ConsoleRestore/Updater.cpp
N2614 103d7a2613 必要な時のみac接続するように
git-svn-id: file:///Volumes/Transfer/gigaleak_20231201/2020-05-23%20-%20ctr.7z%20+%20svn_v1.068.zip/ctr/svn/ctr_Repair@616 385bec56-5757-e545-9c3a-d8741f4650f1
2012-01-27 08:31:17 +00:00

198 lines
6.5 KiB
C++

/*---------------------------------------------------------------------------*
Project: Horizon
File: Updater.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 <nn/os.h>
#include <nn/Handle.h>
#include <nn/dbg.h>
#include <nn/nim.h>
#include <nn/cfg.h>
#include <nn/cfg/CTR/cfg_ApiInit.h>
#include <nn/ac/private/ac.h>
#include <nn/ac/CTR/private/ac_InternalApi.h>
#include "CommonLogger.h"
#include "Util.h"
namespace ConsoleRestore
{
using namespace ES_NAMESPACE;
using namespace EC_NAMESPACE;
/* -------------------------------------------------------------------
GetCustomerSupportCode
-------------------------------------------------------------------- */
#define NIM_UPDATER_RESULT_CHECK(result) \
do { \
if (result.IsFailure()) \
{ \
ECCustomerSupportCode csc; \
NN_UTIL_PANIC_IF_FAILED( \
nn::nim::Updater::GetCustomerSupportCode(&csc)); \
COMMON_LOGGER("CSCode: %d\n", csc); \
COMMON_LOGGER_RESULT_IF_FAILED(result); \
NN_DBG_PRINT_RESULT(result); \
COMMON_LOGGER_DETAIL("Result = %08X\n", result.GetPrintableBits()); \
s_Result = result; \
goto LABEL_FINALIZE; \
} \
} while(0)
namespace
{
nn::Result s_Result = nn::ResultSuccess();
const size_t UPDATER_THREAD_STACK_SIZE = 0x1000;
nn::os::Thread s_UpdaterThread;
nn::os::StackBuffer<UPDATER_THREAD_STACK_SIZE> s_UpdaterThreadStack;
u64 s_Progress = 0;
}
void UpdateThreadFunc()
{
nn::Result result;
COMMON_LOGGER_DETAIL("********************UpdateThreadFunc Start********************\n");
nn::cfg::CTR::init::Initialize();
nn::cfg::CfgCountryCode country;
nn::cfg::CfgRegionCode region;
const char *regionStr;
const char *countryStr;
NN_UNUSED_VAR(regionStr);
NN_UNUSED_VAR(countryStr);
country = nn::cfg::GetCountry();
region = nn::cfg::GetRegion();
countryStr = nn::cfg::GetCountryCodeA2(country);
regionStr = nn::cfg::GetRegionCodeA3(region);
COMMON_LOGGER("[Updater] country:%2d:%s\n", country, countryStr);
COMMON_LOGGER("[Updater] region :%2d:%s\n", region, regionStr);
/* -------------------------------------------------------------------
Initialize
-------------------------------------------------------------------- */
COMMON_LOGGER_DETAIL("[Updater] nn::nim::InitializeForUpdater\n");
result = nn::nim::InitializeForUpdater();
NIM_UPDATER_RESULT_CHECK(result);
if(!nn::ac::IsConnected())
{
COMMON_LOGGER_DETAIL("[Updater] InitializeInternal\n");
result = common::InitializeNetwork();
NIM_UPDATER_RESULT_CHECK(result);
}
/* -------------------------------------------------------------------
StartNetworkUpdate
-------------------------------------------------------------------- */
COMMON_LOGGER_DETAIL("[Updater] nn::nim::Updater::StartNetworkUpdate()\n");
result = nn::nim::Updater::StartNetworkUpdate();
NIM_UPDATER_RESULT_CHECK(result);
/* -------------------------------------------------------------------
GetProgress
-------------------------------------------------------------------- */
COMMON_LOGGER_DETAIL("[Updater] nn::nim::Updater::GetProgress()\n");
while(true)
{
nn::nim::NetworkUpdateProgress progress;
NIM_UPDATER_RESULT_CHECK(nn::nim::Updater::GetProgress(&progress));
NIM_UPDATER_RESULT_CHECK(progress.lastResult);
COMMON_LOGGER_DETAIL("\x1b[1A\x1b[K");
COMMON_LOGGER_DETAIL("Downloading %2lld/%2lld %8lld/%8lld (%d)\n",
progress.downloadedTitleNum,
progress.totalTitleNum,
progress.currentDownloadedSize,
progress.currentTotalSize,
static_cast<s32>(progress.state));
// ゼロ除算を防ぐ
if(progress.totalTitleNum == 0)
{
progress.totalTitleNum++;
progress.downloadedTitleNum++;
}
if(progress.state > nn::nim::CTR::NUP_STATE_CHECKING)
{
s_Progress = progress.downloadedTitleNum * 100 / progress.totalTitleNum;
}
if (progress.state == nn::nim::NUP_STATE_NO_NEED)
{
COMMON_LOGGER("[Updater] No need to NetworkUpdate\n");
s_Progress = 100;
break;
}
if (progress.state == nn::nim::NUP_STATE_FINISHED)
{
COMMON_LOGGER("[Updater] Finished NetworkUpdate\n");
s_Progress = 100;
break;
}
nn::os::Thread::Sleep(nn::fnd::TimeSpan::FromMilliSeconds(300));
}
LABEL_FINALIZE:
/* -------------------------------------------------------------------
Finalize
-------------------------------------------------------------------- */
COMMON_LOGGER_DETAIL("[Updater] nn::nim::FinalizeForUpdater\n");
result = nn::nim::FinalizeForUpdater();
NIM_UPDATER_RESULT_CHECK(result);
COMMON_LOGGER_DETAIL("[Updater] Finish nim Updater demo.\n");
}
void StartFGNetworkUpdate()
{
COMMON_LOGGER_DETAIL("Start FGNetworkUpdate\n");
s_Result = nn::ResultSuccess();
s_UpdaterThread.Start(UpdateThreadFunc, s_UpdaterThreadStack);
}
void FinishFGNetworkUpdate()
{
COMMON_LOGGER_DETAIL("Finalize FGNetworkUpdate\n");
s_UpdaterThread.Join();
s_UpdaterThread.Finalize();
}
bool IsNetworkUpdateFinished()
{
return s_UpdaterThread.IsValid() && !s_UpdaterThread.IsAlive();
}
u32 GetUpdateProgress()
{
return s_Progress;
}
nn::Result GetUpdateResult()
{
return s_Result;
}
}