ctr_Repair/trunk/PersonalDataEraser/main.cpp
N2614 028bc99c35 洞窟物語があるかどうか表示するように
git-svn-id: file:///Volumes/Transfer/gigaleak_20231201/2020-05-23%20-%20ctr.7z%20+%20svn_v1.068.zip/ctr/svn/ctr_Repair@711 385bec56-5757-e545-9c3a-d8741f4650f1
2012-06-21 04:10:18 +00:00

521 lines
16 KiB
C++

/*---------------------------------------------------------------------------*
Project: Horizon
File: main.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.h>
#include <nn/fnd.h>
#include <nn/am/am_LocalImportControl.h>
#include <nn/am/am_ApiSystemMenu.h>
#include <nn/am/am_Result.h>
#include <nn/cfg/CTR/cfg_Api.h>
#include <nn/cfg/CTR/cfg_ApiInit.h>
#include <nn/cfg/CTR/cfg_ApiSys.h>
#include <nn/fs/CTR/fs_ArchiveTypesForSystem.h>
#include <nn/fs/CTR/MPCore/fs_FileSystemBasePrivate.h>
#include <nn/nim.h>
#include <nn/ndm.h>
#include <vector>
#include <string>
#include "demo.h"
#include "ResFont.h"
#include "HeapManager.h"
#define UTIL_RETURN_IF_NOT_AM_NOT_FOUND(result) \
if(result.IsFailure()) \
{ \
if(result != nn::am::ResultNotFound()) \
{ \
return result; \
} \
nn::dbg::PrintResult(result); \
} \
#define RED 1.0f, 0.0f, 0.0f
#define GREEN 0.0f, 1.0f, 0.0f
#define YELLOW 1.0f, 1.0f, 0.0f
#define WHITE 1.0f, 1.0f, 1.0f
#define BLACK 0.0f, 0.0f, 0.0f
namespace {
const char* const NAND_TWL_ARCHIVE_NAME = "twln:";
const char* const NAND_TWL_KENJ_SAVE_DATA_PATHNAME = "twln:/title/00030004/4b454e4a/data/Public.sav";
const nn::ProgramId NAND_TWL_KENJ_PROGRAM_ID = 0x000480044b454e4aULL;
const nn::ProgramId NAND_TWL_KCVJ_PROGRAM_ID = 0x000480044B43564AULL;
// 乱数生成クラス
// 線形合同法を用いて乱数を生成する。
class Random
{
private:
nn::util::Int64<u64> m_x; //!< 乱数値
nn::util::Int64<u64> m_mul; //!< 乗数
nn::util::Int64<u64> m_add; //!< 加算する数
public:
Random(u64 seed = 0)
{
SetSeed(seed);
}
void SetSeed(u64 seed)
{
m_x = seed;
m_mul = (1566083941LL << 32) + 1812433253LL;
m_add = 2531011;
}
u32 Get32(u32 max = 0xFFFFFFFFU)
{
m_x = m_mul * m_x + m_add;
if (max != 0)
{
return (u32)(((m_x >> 32) * max) >> 32);
}
return 0;
}
};
nn::Result DeleteKENJ(void);
bool KCVJExists();
const size_t ERASE_THREAD_STACK_SIZE = 0x1000;
nn::os::Thread s_EraseThread;
nn::os::StackBuffer<ERASE_THREAD_STACK_SIZE> s_EraseThreadStack;
demo::RenderSystemDrawing s_RenderSystem;
bool s_IsAppExist = false;
bool s_IsFinish = false;
bool s_IsSaveDataCleanSuccess = false;
bool s_IsAppDeleteSuccess = false;
bool s_IsCreditCardExist = false;
bool s_IsKCVJExist = false;
nn::Result s_KENJResult = nn::ResultSuccess();
nn::Result s_CreditCardResult = nn::ResultSuccess();
// グラフィックスに割り当てるメモリ
const size_t s_GxHeapSize = 0x800000;
void EraseThreadFunc(void)
{
// ほぼ日健康手帳を消去する
s_KENJResult = DeleteKENJ();
// CTR本体のクレジットカード情報を削除する
nn::nim::CreditCardInfo info;
nn::Result result = nn::nim::Shop::LoadCreditCardFromSystemSaveData(&info);
if(result.IsFailure())
{
s_IsCreditCardExist = false;
}
else
{
s_IsCreditCardExist = true;
s_CreditCardResult = nn::nim::Shop::DeleteCreditCardOnSystemSaveData();
}
// 洞窟物語(KCVJ)の存在を確認する
s_IsKCVJExist = KCVJExists();
s_IsFinish = true;
}
nn::Result FillRandamDataToKENJSaveData(void)
{
nn::Result result;
nn::fs::FileStream file;
s64 file_size;
// セーブデータオープン
result = file.TryInitialize( NAND_TWL_KENJ_SAVE_DATA_PATHNAME, nn::fs::OPEN_MODE_WRITE );
if( result.IsFailure() )
{
return result;
}
// セーブデータファイルのサイズを読む
result = file.TryGetSize( &file_size );
if( result.IsFailure() )
{
file.Finalize();
return result;
}
nn::fnd::DateTime tm;
nn::os::Tick tick;
Random rand;
u64 seed;
s32 sizeResult = 0;
// バッファの確保
void *buf = std::malloc( file_size );
NN_TPANIC_IF_NULL_( buf );
// 乱数のシードを設定
nn::fnd::DateTimeParameters param = tm.GetNow().GetParameters();
seed = tm.DateToDays( param.year, param.month, param.day );
seed = (u64)(seed * 86400 + param.hour * 3600 + param.minute * 60 + param.second);
seed = (u64)(seed ^ tick.GetSystemCurrent());
rand.SetSeed( seed );
// バッファを乱数で埋める
u8 *p = (u8 *)buf;
for (s32 loopSizeFile = 0; loopSizeFile < file_size; loopSizeFile++)
{
p[loopSizeFile] = (u8)rand.Get32(0xff);
}
// 書き込み
result = file.TryWrite(&sizeResult, buf, file_size);
file.Finalize();
return result;
}
nn::Result DeleteKENJ(void)
{
nn::Result result;
// アプリが存在するかどうか確かめる
nn::am::ProgramInfo info;
result = nn::am::GetProgramInfos(&info, nn::fs::MEDIA_TYPE_NAND, &NAND_TWL_KENJ_PROGRAM_ID, 1);
if(result == nn::am::ResultNotFound())
{
// 存在しない場合は削除成功として返す
return nn::ResultSuccess();
}
// TWl領域をマウントする
result = nn::fs::MountSpecialArchive( NAND_TWL_ARCHIVE_NAME, nn::fs::CTR::ARCHIVE_TYPE_TWL_NAND );
if( result.IsFailure() )
{
NN_LOG("Twl NAND Mount Failed...\n");
return result;
}
// アプリ存在フラグを上げる
s_IsAppExist = true;
// ほぼ日健康手帳のセーブデータを乱数で埋める
result = FillRandamDataToKENJSaveData();
if (result.IsFailure())
{
nn::fs::Unmount("twln:");
return result;
}
else
{
s_IsSaveDataCleanSuccess = true;
}
// ほぼ日健康手帳を消す
result = nn::am::DeleteUserProgram(nn::fs::MEDIA_TYPE_NAND, NAND_TWL_KENJ_PROGRAM_ID);
if (result.IsSuccess())
{
s_IsAppDeleteSuccess = true;
}
UTIL_RETURN_IF_NOT_AM_NOT_FOUND(result);
nn::fs::Unmount( "twln:" );
return result;
}
bool KCVJExists()
{
// アプリが存在するかどうか確かめる
nn::am::ProgramInfo info;
nn::Result result = nn::am::GetProgramInfos(&info, nn::fs::MEDIA_TYPE_NAND, &NAND_TWL_KCVJ_PROGRAM_ID, 1);
if(result == nn::am::ResultNotFound())
{
return false;
}
else
{
return true;
}
}
void DrawResultText()
{
s_RenderSystem.SetFontSize(8.0f);
s_RenderSystem.SetColor( YELLOW );
s_RenderSystem.DrawText(10.0f, 20.0f, "SaveData Cleaner");
s_RenderSystem.SetColor( WHITE );
s_RenderSystem.DrawText(10.0f, 60.0f, "Initialcode : KENJ");
s_RenderSystem.DrawText(10.0f, 180.0f, "Credit Card :");
s_RenderSystem.DrawText(10.0f, 200.0f, "KCVJ :");
if( s_KENJResult.IsSuccess() )
{
if( s_IsAppExist )
{
s_RenderSystem.SetColor( GREEN );
s_RenderSystem.DrawText(10.0f, 75.0f, "application found");
s_RenderSystem.SetColor( WHITE );
s_RenderSystem.DrawText(10.0f, 120.0f, "Private Save :");
s_RenderSystem.DrawText(10.0f, 135.0f, "Public Save :");
s_RenderSystem.DrawText(10.0f, 150.0f, "application :");
s_RenderSystem.SetColor( YELLOW );
s_RenderSystem.DrawText(130.0f, 120.0f, "not existed");
s_RenderSystem.SetColor( GREEN );
s_RenderSystem.DrawText(130.0f, 135.0f, "cleanup succeeded");
s_RenderSystem.DrawText(130.0f, 150.0f, "cleanup succeeded");
}
else
{
s_RenderSystem.SetColor( YELLOW );
s_RenderSystem.DrawText(10.0f, 70.0f, "application not found");
}
}
else
{
s_RenderSystem.SetColor( WHITE );
s_RenderSystem.DrawText(10.0f, 120.0f, "Private Save :");
s_RenderSystem.DrawText(10.0f, 135.0f, "Public Save :");
s_RenderSystem.DrawText(10.0f, 150.0f, "application :");
s_RenderSystem.SetColor( YELLOW );
s_RenderSystem.DrawText(130.0f, 120.0f, "not existed");
// アプリある?
if( s_IsAppExist )
{
s_RenderSystem.SetColor( GREEN );
s_RenderSystem.DrawText(10.0f, 75.0f, "application found");
}
else
{
s_RenderSystem.SetColor( RED );
s_RenderSystem.DrawText(10.0f, 75.0f, "application not found");
}
// セーブデータの乱数埋めでエラー?
if( s_IsSaveDataCleanSuccess )
{
s_RenderSystem.SetColor( GREEN );
s_RenderSystem.DrawText(130.0f, 135.0f, "cleanup succeeded");
}
else
{
s_RenderSystem.SetColor( RED );
s_RenderSystem.DrawText(130.0f, 135.0f, "cleanup failed");
}
// アプリ削除でエラー?
if( s_IsAppDeleteSuccess )
{
s_RenderSystem.SetColor( GREEN );
s_RenderSystem.DrawText(130.0f, 150.0f, "cleanup failed");
}
else
{
s_RenderSystem.SetColor( RED );
s_RenderSystem.DrawText(130.0f, 150.0f, "cleanup failed");
}
}
if (s_IsCreditCardExist)
{
if (s_CreditCardResult.IsSuccess())
{
s_RenderSystem.SetColor(GREEN);
s_RenderSystem.DrawText(130.0f, 180.0f, "cleanup succeeded");
}
else
{
s_RenderSystem.SetColor(RED);
s_RenderSystem.DrawText(130.0f, 180.0f, "cleanup failed");
}
}
else
{
s_RenderSystem.SetColor(YELLOW);
s_RenderSystem.DrawText(130.0f, 180.0f, "data not found");
}
if(s_IsKCVJExist)
{
s_RenderSystem.SetColor( RED );
s_RenderSystem.DrawText(130.0f, 200.0f, "application found");
}
else
{
s_RenderSystem.SetColor( YELLOW );
s_RenderSystem.DrawText(130.0f, 200.0f, "application not found");
}
}
void DrawKENJResultUpper(const f32 red, const f32 green, const f32 blue)
{
s_RenderSystem.SetColor(red, green, blue);
s_RenderSystem.FillRectangle(0, 0, NN_GX_DISPLAY0_HEIGHT, NN_GX_DISPLAY0_WIDTH / 3);
s_RenderSystem.SetColor(BLACK);
s_RenderSystem.DrawText(0, 0, "KENJ Result");
}
void DrawCreditCardResultUpper(const f32 red, const f32 green, const f32 blue)
{
s_RenderSystem.SetColor(red, green, blue);
s_RenderSystem.FillRectangle(0, NN_GX_DISPLAY0_WIDTH / 3, NN_GX_DISPLAY0_HEIGHT, NN_GX_DISPLAY0_WIDTH / 3);
s_RenderSystem.SetColor(BLACK);
s_RenderSystem.DrawText(0, NN_GX_DISPLAY0_WIDTH / 3, "Credit Card Result");
}
void DrawKCVJResultUpper(const f32 red, const f32 green, const f32 blue)
{
s_RenderSystem.SetColor(red, green, blue);
s_RenderSystem.FillRectangle(0, NN_GX_DISPLAY0_WIDTH / 3 * 2, NN_GX_DISPLAY0_HEIGHT, NN_GX_DISPLAY0_WIDTH / 3);
s_RenderSystem.SetColor(BLACK);
s_RenderSystem.DrawText(0, NN_GX_DISPLAY0_WIDTH / 3 * 2, "KCVJ Result");
}
}
extern "C" void nnMain(void)
{
// os の初期化
nn::os::Initialize();
// fs の初期化
nn::fs::Initialize();
// appletの初期化
nn::applet::Enable( false );
// cfg の初期化
nn::cfg::CTR::Initialize();
// am の初期化
nn::am::InitializeForSystemMenu();
// nimの初期化
nn::nim::InitializeForShop();
// ndmの初期化
NN_UTIL_PANIC_IF_FAILED(
nn::ndm::Initialize()
);
NN_UTIL_PANIC_IF_FAILED(
nn::ndm::SuspendScheduler()
);
// ヒープの確保
common::HeapManager::GetHeap()->Initialize(nn::os::GetDeviceMemoryAddress(), nn::os::GetDeviceMemorySize(), nn::os::ALLOCATE_OPTION_LINEAR);
// RenderSystem の準備
uptr heapForGx = reinterpret_cast<uptr>(common::HeapManager::GetHeap()->Allocate(s_GxHeapSize));
s_RenderSystem.Initialize(heapForGx, s_GxHeapSize);
// ResFontの初期化
common::InitializeResFont();
s_EraseThread.Start(EraseThreadFunc, s_EraseThreadStack);
s_RenderSystem.SetClearColor(NN_GX_DISPLAY0, BLACK, 0);
s_RenderSystem.SetClearColor(NN_GX_DISPLAY1, BLACK, 0);
for(;;)
{
if( s_IsFinish )
{
// 初期化
s_RenderSystem.SetRenderTarget(NN_GX_DISPLAY0);
s_RenderSystem.Clear();
// 上画面の色変更
// ほぼ日健康手帳の結果
if( s_KENJResult.IsSuccess() )
{
if( !s_IsAppExist )
{
DrawKENJResultUpper(YELLOW);
}
else
{
DrawKENJResultUpper(GREEN);
}
}
else
{
DrawKENJResultUpper(RED);
}
// クレジットカードの結果
if (!s_IsCreditCardExist)
{
DrawCreditCardResultUpper(YELLOW);
}
else
{
if (s_CreditCardResult.IsSuccess())
{
DrawCreditCardResultUpper(GREEN);
}
else
{
DrawCreditCardResultUpper(RED);
}
}
// 洞窟物語の結果
if(s_IsKCVJExist)
{
DrawKCVJResultUpper(RED);
}
else
{
DrawKCVJResultUpper(YELLOW);
}
// バッファへ
s_RenderSystem.SwapBuffers();
// 初期化
s_RenderSystem.SetRenderTarget(NN_GX_DISPLAY1);
s_RenderSystem.Clear();
// 変更
DrawResultText();
common::DrawResFont(NN_GX_DISPLAY1);
// バッファへ
s_RenderSystem.SwapBuffers();
}
nn::os::Thread::Sleep(nn::fnd::TimeSpan::FromMilliSeconds(10));
if ( nn::applet::IsExpectedToCloseApplication() )
{
nn::applet::PrepareToCloseApplication();
nn::applet::CloseApplication();
}
}
}