ctr_Repair/trunk/CardSaveDataMover/ImpRun/main.cpp

295 lines
10 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*---------------------------------------------------------------------------*
Project: Horizon
File: main.cpp
Copyright (C)2009 Nintendo Co., Ltd. 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/srv.h>
#include <nn/fs.h>
//#include <nn/ns/CTR/ns_ApiShell.h>
//#include <nn/ns/CTR/ns_Shell.h>
#include <nn/am/am_ApiLocalImporter.h>
#include <nn/am/am_ApiSystemMenu.h>
#include <nn/applet.h>
#include <nn/ndm.h>
#include "demo.h"
#include "../SaveDataTool/ver.h"
#include <stdio.h>
#include "sleep.h"
#include "test_data.h"
demo::RenderSystemDrawing s_RenderSystem;
nn::fnd::ExpHeap appHeap;
uptr heapForGx;
namespace
{
bit8 readBuf[64 * 1024];
}
bool ImportFile(nn::fs::MediaType mediaType, wchar_t* filename)
{
nn::Result result;
nn::fs::FileOutputStream* stream;
result = nn::am::BeginImportProgram(&stream, mediaType);
if (result.IsSuccess())
{
//NN_LOG("Importing: %ls...", filename);
nn::fs::FileInputStream in(filename);
while(s32 read = in.Read(readBuf, sizeof(readBuf)))
{
stream->Write(readBuf, read);
}
result = nn::am::EndImportProgram(stream);
}
return result.IsSuccess();
}
//終了処理
void finish(void)
{
SleepHandler::Finalize();//SLEEP
nn::hid::Finalize();
// nngxFinalize 関数呼び出しと VSync 割り込みが同時に発生すると
// 画面が暗転したまま止まる不具合を回避するために VSync 待ちを行う
nngxWaitVSync(NN_GX_DISPLAY_BOTH);
s_RenderSystem.Finalize();
nn::fs::Finalize();
appHeap.Free(reinterpret_cast<void*>(heapForGx));
appHeap.Finalize();
nn::os::SetDeviceMemorySize(0);
nn::os::SetHeapSize(0);
NN_LOG("End\n");
nn::applet::PrepareToCloseApplication();
nn::applet::CloseApplication();
}
nn::hid::PadReader *hpr;
//---------------------------------------------------------------- 入力待ち
nn::hid::PadStatus padStatus;
u32 WaitKey(u32 mask)
{
while(1){
hpr->ReadLatest(&padStatus);
if(padStatus.trigger & mask)return padStatus.trigger;
// 終了要求をチェック
if ( nn::applet::IsExpectedToCloseApplication() )
{
finish();
}
// アプリケーションの遷移処理を行う
// HOMEボタン処理グラフィックスライブラリが初期化済みでなければならない
if ( nn::applet::IsExpectedToProcessHomeButton() )
{
// スリープ要求を REJECT するように
nn::applet::DisableSleep();
nn::applet::ProcessHomeButton();
nn::applet::AppletWakeupState wState = nn::applet::WaitForStarting();
NN_UNUSED_VAR(wState);
// HOME メニューからの中断やバッテリー不足の要因により
// アプリ終了と判断された場合、アプリのメインループを抜けて終了処理へ
if ( nn::applet::IsExpectedToCloseApplication() )
{
// 描画権は渡されていない
finish();
}
//nn::applet::EnableSleep(true);
// グラフィックの設定を戻す
nngxUpdateState(NN_GX_STATE_ALL);
nngxValidateState(NN_GX_STATE_ALL,GL_TRUE);
}
// 電源ボタン処理(グラフィックスライブラリが初期化済みでなければならない)
if ( nn::applet::IsExpectedToProcessPowerButton() )
{
nn::applet::DisableSleep();
nn::applet::ProcessPowerButton();
nn::applet::AppletWakeupState wState = nn::applet::WaitForStarting();
NN_UNUSED_VAR(wState);
// アプリ終了と判断された場合、アプリのメインループを抜けて終了処理へ
if ( nn::applet::IsExpectedToCloseApplication() )
{
// 描画権は渡されていない
finish();
}
// nn::applet::EnableSleep(true);
// グラフィックの設定を戻す
nngxUpdateState(NN_GX_STATE_ALL);
nngxValidateState(NN_GX_STATE_ALL,GL_TRUE);
}
}
}
char ver[16];
void nnMain( void )
{
nn::Result result;
nn::os::Initialize();
nn::fs::Initialize();
//DEA-SUPにて推奨のフリーズ暫定対策無線デーモンを停止 (2011.3.1 現在)
//ただし、スリープ時の"いつのまに通信"は止まらない
//無線は使わないが念の為、本体スイッチで切っとくのが確実
nn::ndm::SuspendScheduler();
SleepHandler::Initialize();
// アプレットライブラリに関する機能を有効にする
nn::applet::Enable(false);
// gx, snd, dsp 各ライブラリの Initialize 関数は applet::Enable() の後に呼ばれなければならない
// 特に、gx の初期化である nngxInitialize 関数は、Enable() 直後の終了判定の後に行われるべき
// ロゴ表示時に電源ボタンが押されたなど、既に終了条件が立っていた場合、ここでアプリを終了させる
if ( nn::applet::IsExpectedToCloseApplication() )
{
SleepHandler::Finalize();
result = nn::applet::PrepareToCloseApplication();
NN_UTIL_PANIC_IF_FAILED( result );
nn::applet::CloseApplication();
}
// グラフィックスライブラリの初期化は、以降で行わなければならない
// 他、アプリケーションの初期化処理
nn::hid::Initialize();
nn::hid::PadReader padReader;
hpr = &padReader;
//result = nn::ns::CTR::InitializeForShell();
//if (result.IsSuccess())
result = nn::am::InitializeForLocalImporter();
nn::applet::DisableSleep();//スリープ非対応
const size_t ROMFS_BUFFER_SIZE = 1024 * 64;
static char buffer[ROMFS_BUFFER_SIZE];
// ヒープの確保
appHeap.Initialize(nn::os::GetDeviceMemoryAddress(), nn::os::GetDeviceMemorySize(), nn::os::ALLOCATE_OPTION_LINEAR);
const u32 s_GxHeapSize = 0x800000;
// RenderSystem の準備
heapForGx = reinterpret_cast<uptr>(appHeap.Allocate(s_GxHeapSize));
s_RenderSystem.Initialize(heapForGx, s_GxHeapSize);
s_RenderSystem.SetRenderTarget(NN_GX_DISPLAY0);
s_RenderSystem.Clear();
s_RenderSystem.SetFontSize(12);
s_RenderSystem.SetColor(1.0, 1.0, 1.0,1.0);
s_RenderSystem.DrawText(10,20,"CTR Card Savedata Mover Setup");
sprintf(ver,"version %.2f",VERSION);
s_RenderSystem.DrawText(10,40,ver);
if (result.IsFailure()){//初期化に失敗
s_RenderSystem.SetColor(1.0, 0.0, 0.0,1.0);
s_RenderSystem.DrawText(10,70,"Initialize Error");
s_RenderSystem.DrawText(10,90,"check *.desc file");
s_RenderSystem.DrawText(10,110,"Push B : Quit");
s_RenderSystem.SwapBuffers();
while(1){
padReader.ReadLatest(&padStatus);
if(padStatus.trigger & nn::hid::BUTTON_B)break;
}
finish();
}
nn::fs::MountRom(16, 16, buffer, ROMFS_BUFFER_SIZE);
s_RenderSystem.SetColor(1.0, 1.0, 1.0,1.0);
s_RenderSystem.DrawText(10,20,"CTR Card Savedata Mover Setup");
sprintf(ver,"version %.2f",VERSION);
s_RenderSystem.DrawText(10,40,ver);
s_RenderSystem.DrawText(80,100,"Push X : Inport");
s_RenderSystem.DrawText(80,140,"Push Y : Delete");
s_RenderSystem.DrawText(80,160,"Push R : Test Data");
s_RenderSystem.SwapBuffers();
u32 trg = WaitKey(nn::hid::BUTTON_X | nn::hid::BUTTON_Y | nn::hid::BUTTON_R);
if (trg & nn::hid::BUTTON_R)TestMain();//テストデータへ
// CIA のプログラム ID を取得する。
nn::am::ProgramInfo programInfo;
if (nn::am::GetProgramInfoFromCia(&programInfo, ROMFS_IMPORTEE_PATH).IsFailure())
{
s_RenderSystem.Clear();
s_RenderSystem.SetColor(1.0, 0.0, 0.0,1.0);
s_RenderSystem.DrawText(10,50,"cia infomation Error");
s_RenderSystem.DrawText(10,70,"Push B : Quit");
s_RenderSystem.SwapBuffers();
while(1){
padReader.ReadLatest(&padStatus);
if(padStatus.trigger & nn::hid::BUTTON_B)break;
}
} else
{
s_RenderSystem.Clear();
if(padStatus.trigger & nn::hid::BUTTON_X)
s_RenderSystem.DrawText(50,70,"Inporting");
else s_RenderSystem.DrawText(50,70,"Delete");
s_RenderSystem.SwapBuffers();
// 既に存在するものをインポートするとエラーが返ってくるので、あらかじめ消しておく。
nn::am::DeleteProgram(nn::fs::MEDIA_TYPE_NAND, programInfo.id);
// タイトル鍵が違うケースに対応するために、チケットも消しておく。
nn::am::DeleteTicket(programInfo.id);
if(padStatus.trigger & nn::hid::BUTTON_X)
{
// CIA をインポート
if (ImportFile(nn::fs::MEDIA_TYPE_NAND, ROMFS_IMPORTEE_PATH)==false)
{
s_RenderSystem.Clear();
s_RenderSystem.SetColor(1.0, 0.0, 0.0,1.0);
s_RenderSystem.DrawText(50,50,"Impoprt fail");
s_RenderSystem.SwapBuffers();
}
}
s_RenderSystem.Clear();
s_RenderSystem.DrawText(50,50,"Complate");
}
s_RenderSystem.DrawText(10,80,"Push Power Button & Power Off");
s_RenderSystem.SwapBuffers();
WaitKey(0);
}