mirror of
https://github.com/rvtr/ctr_Repair.git
synced 2025-10-31 13:51:08 -04:00
967 lines
31 KiB
C++
967 lines
31 KiB
C++
/*---------------------------------------------------------------------------*
|
||
Project: Horizon
|
||
File: main.cpp
|
||
|
||
Copyright (C)2010 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.
|
||
|
||
*---------------------------------------------------------------------------*/
|
||
|
||
#include <wchar.h>
|
||
#include <string.h>
|
||
#include <stdlib.h>
|
||
#include <stdio.h>
|
||
#include <nn.h>
|
||
//#include <nn/os.h>
|
||
#include <nn/applet.h>
|
||
#include <nn/ndm.h>
|
||
#include <nn/ndm/ndm_DebugControl.h>
|
||
#include <nn/fs/ctr/mpcore/fs_FileSystemBasePrivate.h>
|
||
#include <nn/fs/fs_Parameters.h>
|
||
//#include <nn/fs/fs_ApiProcessInfo.h>
|
||
#include "../../../common/savefile/savefile.h"
|
||
#include "../../../common/savefile/savedata.h"
|
||
#include "../../../common/savefile/membak.h"
|
||
#include "./screen/screen.h"
|
||
#include "../../../common/common.h"
|
||
|
||
nn::fnd::ExpHeap appHeap;
|
||
uptr heapForGx;
|
||
//Gui gui;
|
||
myResult errRes;
|
||
|
||
|
||
//char strBuff[128];
|
||
//bool exist;
|
||
|
||
SaveData savedata;
|
||
nn::os::LightEvent ejectEvnt(false);
|
||
nn::os::LightEvent insEvnt(false);
|
||
MemBak exsave;
|
||
|
||
//バックアップの状態
|
||
bool Active,Formatted;
|
||
bool exActive,exFormatted;
|
||
tArcInfo arcInfo,arcInfo_ex;//アーカイブ情報
|
||
|
||
//プロダクトコードの一致
|
||
bool isAgreePCode;
|
||
//ベリファイ結果
|
||
//bool failveri;
|
||
|
||
//Top画面エラー表示
|
||
int tmerr;
|
||
|
||
//結果
|
||
typedef enum {
|
||
SUCCESS
|
||
,ERROR
|
||
,CANCEL
|
||
,INSEXIT
|
||
}RetCode;
|
||
|
||
//挿抜イベント時に設定
|
||
typedef enum {
|
||
InEx_None
|
||
,InEx_InsCard
|
||
,InEx_EjcCard
|
||
}tIsInsEject;
|
||
|
||
tIsInsEject isInsEject;
|
||
|
||
extern u8 scr_evnt;
|
||
void WaitUI();
|
||
void PutError(ErcDev dev,int cd=0);
|
||
|
||
|
||
//終了処理
|
||
void endfunc()
|
||
{
|
||
|
||
nn::fs::UnregisterCardEjectedEvent();
|
||
nn::fs::UnregisterCardInsertedEvent();
|
||
ejectEvnt.Finalize();
|
||
insEvnt.Finalize();
|
||
|
||
savedata.Finalize();
|
||
exsave.Finalize();
|
||
|
||
ScrFinalize();
|
||
|
||
}
|
||
|
||
//エラー停止
|
||
void failstop()
|
||
{
|
||
NN_LOG("fail %d,stop\n",errRes);
|
||
finish();
|
||
}
|
||
|
||
char sts[64];
|
||
void PutError(ErcDev dev,int cd)
|
||
{
|
||
nn::Result res;
|
||
switch (dev)
|
||
{
|
||
case ERC_DEV_CARD: res = savedata.LastNnResult;break;
|
||
case ERC_DEV_OUT: res = exsave.LastNnResult;break;
|
||
}
|
||
GetErrorStr(dev,res,cd,sts);
|
||
scr_Status(sts,COLOR_RED);
|
||
}
|
||
|
||
|
||
//-------------------------------------------------- Verifi
|
||
wchar_t file_pathw[MAX_PATH_LENGTH];
|
||
char file_path[MAX_PATH_LENGTH];
|
||
//ドライバの仕様で速度的に4バイトアラインした方がいい(SDK 0.14.23 時点)
|
||
char fileBuffer[512] NN_ATTRIBUTE_ALIGN(4);//検証が512単位なので
|
||
char fileBuffer_ex[512] NN_ATTRIBUTE_ALIGN(4);
|
||
#define FILEBUFF_SIZE sizeof(fileBuffer)
|
||
|
||
bool Verifi()
|
||
{
|
||
myResult res;
|
||
int ct,i,msize,rsize;
|
||
s64 total=0;
|
||
|
||
//セーブデータのマウント
|
||
res = savedata.Mount();
|
||
if(res != RESULT_OK){
|
||
PutError(ERC_DEV_CARD);
|
||
return false;
|
||
}
|
||
|
||
//ファイル数の再取得..ファイル多いと時間かかる
|
||
//arcInfoの保持があやしい or 出来ない場合に有効にする
|
||
/*
|
||
scr_Status("Ready to Verifi",COLOR_YELLO);
|
||
//情報ファイル
|
||
LogAdd("Directry Info");
|
||
if(savedata.GetInfo(&arcInfo)==false)
|
||
{
|
||
savedata.Unmount();
|
||
PutError(ERC_DEV_CARD);
|
||
return ERROR;
|
||
}
|
||
*/
|
||
|
||
//出力デバイスのマウント
|
||
res = exsave.Mount();
|
||
if(res != RESULT_OK){
|
||
PutError(ERC_DEV_OUT);
|
||
return false;
|
||
}
|
||
|
||
//情報ファイル
|
||
if (exsave.GetInfo(&arcInfo_ex) == false)
|
||
{
|
||
savedata.Unmount();
|
||
exsave.Unmount();
|
||
return false;
|
||
}//todo:将来、構造体を変更したらバージョンチェック入れる
|
||
|
||
//フォーマット情報の比較
|
||
if ((arcInfo.DirEntry != arcInfo_ex.DirEntry) ||
|
||
(arcInfo.FileEntry != arcInfo_ex.FileEntry) ||
|
||
(arcInfo.Dup != arcInfo_ex.Dup))
|
||
{
|
||
PutError(ERC_DEV_OUT,ERC_VERIFI);
|
||
savedata.Unmount();
|
||
exsave.Unmount();
|
||
return false;
|
||
}
|
||
|
||
ct =0;
|
||
if (arcInfo.FileCount == 0)//ファイルが無い
|
||
{
|
||
savedata.Unmount();
|
||
exsave.Unmount();
|
||
return true;//フォーマット一致のみ
|
||
}else{
|
||
scr_Status("file compairing",COLOR_YELLO);
|
||
savedata.ResetPath();
|
||
exsave.ResetPath();
|
||
s64 fsize;
|
||
res = RESULT_OK;
|
||
while(res==RESULT_OK){
|
||
res = savedata.GetPath(file_pathw);
|
||
if (res != RESULT_OK)
|
||
{
|
||
if (res == RESULT_DIR_LEVEL_OVER){
|
||
break;
|
||
}
|
||
if (res == RESULT_PATH_LENGTH_OVER){
|
||
break;
|
||
}
|
||
res = RESULT_FAIL_OPEN;
|
||
break;
|
||
}
|
||
if (file_pathw[0] == 0)break;//root then end
|
||
if (ct>=arcInfo.FileCount)
|
||
{//カウンタ壊れ、メモリフローの可能性
|
||
res = RESULT_FAIL;
|
||
break;
|
||
}
|
||
scr_CountPerMax(ct,arcInfo.FileCount);
|
||
if (savedata.Open(file_pathw)==false)
|
||
{
|
||
res = RESULT_FAIL_OPEN;
|
||
break;
|
||
}
|
||
fsize = savedata.FileSize;
|
||
total += fsize;
|
||
if (exsave.Open(file_pathw)==false)
|
||
{
|
||
res = RESULT_FAIL_OPENW;
|
||
//savedata.Close();
|
||
break;
|
||
}
|
||
if (fsize != exsave.FileSize)//file size
|
||
{
|
||
res = RESULT_FAIL;
|
||
break;
|
||
}
|
||
msize=fsize;
|
||
if(msize <= FILEBUFF_SIZE ){//一回で読めるサイズならゲージは出さない
|
||
scr_CountPerMax2(0,0,-1);
|
||
}
|
||
while(fsize)
|
||
{
|
||
if(msize > FILEBUFF_SIZE ){
|
||
scr_CountPerMax2(msize-fsize,msize,0);
|
||
}
|
||
if (fsize > FILEBUFF_SIZE)
|
||
{
|
||
rsize = FILEBUFF_SIZE;
|
||
fsize -= FILEBUFF_SIZE;
|
||
}else{
|
||
rsize = fsize;
|
||
fsize = 0;
|
||
}
|
||
if(savedata.Read(fileBuffer,rsize) != rsize )
|
||
{
|
||
//中身のデータが作成サイズに満たない場合に検証エラーとなる対策
|
||
//読めないブロックは置き換える
|
||
//検証は512単位でされるので読めてるとこまで置き換えないよう
|
||
//リードサイズは512にしておく
|
||
if(nn::fs::ResultVerificationFailed::Includes(savedata.LastNnResult))
|
||
{
|
||
for (i = 0; i < rsize; i++)
|
||
{
|
||
fileBuffer[i] = 0;//0埋め
|
||
}
|
||
}else{
|
||
res = RESULT_FAIL_READ;
|
||
break;
|
||
}
|
||
}
|
||
if(exsave.Read(fileBuffer_ex,rsize) != rsize )
|
||
{
|
||
res = RESULT_FAIL;
|
||
break;
|
||
}
|
||
}
|
||
savedata.Close();
|
||
exsave.Close();
|
||
if (res != RESULT_OK)break;
|
||
//verifi
|
||
for (i = 0;i<rsize;i++)if (fileBuffer[i] != fileBuffer_ex[i])
|
||
{
|
||
res = RESULT_FAIL;
|
||
break;
|
||
}
|
||
ct++;
|
||
}
|
||
savedata.Close();
|
||
exsave.Close();
|
||
}
|
||
|
||
savedata.Unmount();
|
||
exsave.Unmount();
|
||
scr_CountPerMax(ct,arcInfo.FileCount);
|
||
scr_CountPerMax2(0,0,total);
|
||
|
||
if (res != RESULT_OK){
|
||
PutError(ERC_DEV_OTHER,ERC_VERIFI);
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
//---------------------------------------------------------------------- from CARD
|
||
RetCode Card2Sd()
|
||
{
|
||
myResult res;
|
||
int ct=0,msize,rsize;
|
||
s64 total=0;
|
||
|
||
scr_BackupYesNo();//実行確認
|
||
WaitUI();//入力待ち
|
||
if(isInsEject != InEx_None )
|
||
{//挿抜?
|
||
//isInsEject = InEx_None;
|
||
return INSEXIT;//挿抜による中断
|
||
}
|
||
if (scr_evnt != EVNT_YES)return CANCEL;//キャンセル
|
||
scr_Backup();//画面表示
|
||
scr_Status("Check CARD",COLOR_YELLO);
|
||
//セーブデータのマウント
|
||
res = savedata.Mount();
|
||
if(res != RESULT_OK){
|
||
PutError(ERC_DEV_CARD);
|
||
return ERROR;
|
||
}
|
||
|
||
if(savedata.GetInfo(&arcInfo)==false)
|
||
{
|
||
savedata.Unmount();
|
||
PutError(ERC_DEV_CARD);
|
||
return ERROR;
|
||
}
|
||
//strcpy(arcInfo.Pcode,savedata.PrdCode);
|
||
|
||
//サイズチェック
|
||
if (arcInfo.total > MEM_BKUP_SIZE)
|
||
{
|
||
savedata.Unmount();
|
||
PutError(ERC_DEV_OUT,ERC_WRITE_NS);//容量オーバ
|
||
return ERROR;
|
||
}
|
||
//カウント異常なら実行エラー
|
||
if (arcInfo.FileCount > arcInfo.FileEntry)
|
||
{
|
||
savedata.Unmount();
|
||
PutError(ERC_DEV_OTHER,ERC_EXEC);
|
||
return ERROR;
|
||
}
|
||
|
||
scr_Status("BackUp Check",COLOR_YELLO);
|
||
if(exsave.IsExist()){
|
||
if(exsave.Delete()==false)
|
||
{
|
||
savedata.Unmount();
|
||
PutError(ERC_DEV_OUT);
|
||
return ERROR;
|
||
}
|
||
}
|
||
|
||
scr_Status("Create Backup",COLOR_YELLO);
|
||
res = exsave.Create(arcInfo.DirEntry+1,arcInfo.FileEntry+1);
|
||
if (res != RESULT_OK){
|
||
savedata.Unmount();
|
||
PutError(ERC_DEV_OUT);
|
||
return ERROR;
|
||
}
|
||
|
||
if (arcInfo.FileCount == 0)//ファイルが無い
|
||
{
|
||
scr_Status("no files",COLOR_YELLO);
|
||
savedata.Unmount();
|
||
exsave.Unmount();
|
||
return SUCCESS;//フォーマットのみ
|
||
}else{
|
||
scr_Status("file reading",COLOR_SKY);
|
||
savedata.ResetPath();
|
||
s64 fsize;
|
||
res = RESULT_OK;
|
||
while(res==RESULT_OK){
|
||
res = savedata.GetPath(file_pathw);
|
||
if (res != RESULT_OK)
|
||
{
|
||
if (res == RESULT_DIR_LEVEL_OVER){
|
||
PutError(ERC_DEV_CARD,ERC_DIRDEPTH);
|
||
break;
|
||
}
|
||
if (res == RESULT_PATH_LENGTH_OVER){
|
||
PutError(ERC_DEV_CARD,ERC_PATH);
|
||
break;
|
||
}
|
||
res = RESULT_FAIL_OPEN;
|
||
break;
|
||
}
|
||
if (file_pathw[0] == 0)break;//root then end
|
||
if (ct>=arcInfo.FileCount)
|
||
{//カウンタ壊れ、メモリフローの可能性
|
||
res = RESULT_FAIL;
|
||
break;
|
||
}
|
||
|
||
scr_CountPerMax(ct,arcInfo.FileCount);
|
||
if (savedata.Open(file_pathw)==false)
|
||
{
|
||
res = RESULT_FAIL_OPEN;
|
||
break;
|
||
}
|
||
fsize = savedata.FileSize;
|
||
total += fsize;
|
||
if (exsave.OpenW(file_pathw,fsize)==false)
|
||
{
|
||
res = RESULT_FAIL_OPENW;
|
||
//savedata.Close();
|
||
break;
|
||
}
|
||
|
||
msize=fsize;
|
||
while(fsize)
|
||
{
|
||
if(msize > FILEBUFF_SIZE * 2){
|
||
scr_CountPerMax2(msize-fsize,msize,0);
|
||
}
|
||
if (fsize>FILEBUFF_SIZE)
|
||
{
|
||
rsize = FILEBUFF_SIZE;
|
||
fsize -= FILEBUFF_SIZE;
|
||
}else{
|
||
rsize = fsize;
|
||
fsize = 0;
|
||
}
|
||
if(savedata.Read(fileBuffer,rsize) != rsize )
|
||
{
|
||
//中身のデータが作成サイズに満たない場合に検証エラーとなる対策
|
||
//読めないブロックは置き換える
|
||
//検証は512単位でされるので読めてるとこまで置き換えないよう
|
||
//リードサイズは512にしておく
|
||
if(nn::fs::ResultVerificationFailed::Includes(savedata.LastNnResult))
|
||
{
|
||
for (int i = 0; i < rsize; i++)
|
||
{
|
||
fileBuffer[i] = 0;//0埋め
|
||
}
|
||
}else{
|
||
res = RESULT_FAIL_READ;
|
||
break;
|
||
}
|
||
}
|
||
if(exsave.Write(fileBuffer,rsize) != rsize )
|
||
{
|
||
res = RESULT_FAIL_WRITE;
|
||
break;
|
||
}
|
||
}
|
||
savedata.Close();
|
||
exsave.CloseW();
|
||
//todo: ゲージ表示
|
||
ct++;
|
||
}
|
||
savedata.Close();
|
||
exsave.CloseW();
|
||
}
|
||
|
||
if (res == RESULT_OK){
|
||
if (ct != arcInfo.FileCount)//ファイル数に満たないパス検索終了
|
||
{//ディレクトリ情報かパス取得で失敗してると思われる
|
||
PutError(ERC_DEV_OTHER,ERC_EXEC);
|
||
return ERROR;
|
||
}
|
||
//フォーマット情報を記録
|
||
//Exsaveに2重化が無いので情報取得APIでは不足 - 2011.1 現在
|
||
//プロダクトコードを追加(2011.1.26)
|
||
//バージョンを追加(2011.1.27)
|
||
if (exsave.OpenSysW())
|
||
{
|
||
strcpy(arcInfo.Pcode,savedata.PrdCode);
|
||
if(exsave.WriteSys(&arcInfo) ==false)
|
||
{
|
||
res = RESULT_FAIL_WRITE;
|
||
}
|
||
}else{
|
||
res = RESULT_FAIL_OPENW;
|
||
}
|
||
exsave.CloseSysW();
|
||
}
|
||
savedata.Unmount();
|
||
//exsave.Commit(); Nand,SDはコミット入れる
|
||
exsave.Unmount();
|
||
scr_CountPerMax(ct,arcInfo.FileCount);
|
||
if(total>0)scr_CountPerMax2(0,0,total);
|
||
|
||
//結果
|
||
switch(res)
|
||
{
|
||
case RESULT_OK:
|
||
if (Verifi())
|
||
{
|
||
scr_Status("Complate",COLOR_WHITE);
|
||
return SUCCESS;
|
||
}
|
||
//PutError(ERC_DEV_OTHER,ERC_VERIFI);
|
||
break;
|
||
case RESULT_FAIL_WRITE:
|
||
case RESULT_FAIL_OPENW:
|
||
//本体メモリなので容量以外でエラーはない
|
||
PutError(ERC_DEV_OUT,ERC_WRITE_NS);//容量オーバ
|
||
break;
|
||
case RESULT_FAIL_READ:
|
||
case RESULT_FAIL_OPEN:
|
||
PutError(ERC_DEV_CARD);
|
||
break;
|
||
}
|
||
exsave.Delete();//バックアップを削除
|
||
return ERROR;
|
||
}
|
||
bool mkdir;
|
||
//---------------------------------------------------------------- SD->CARD
|
||
RetCode Sd2Card()
|
||
{
|
||
myResult res;
|
||
int ct=0,msize,rsize;
|
||
s64 total = 0;
|
||
|
||
|
||
scr_RestoreYesNo();//実行確認
|
||
WaitUI();//入力待ち
|
||
if(isInsEject != InEx_None ){//挿抜?
|
||
//isInsEject = InEx_None;
|
||
return INSEXIT;//挿抜による中断
|
||
}
|
||
|
||
if (scr_evnt != EVNT_YES)return CANCEL;//キャンセル
|
||
scr_Restore();//画面表示
|
||
scr_Status("Check Backup",COLOR_YELLO);
|
||
//保存先のマウント
|
||
res = exsave.Mount();
|
||
if(res != RESULT_OK){
|
||
PutError(ERC_DEV_OUT);
|
||
return ERROR;
|
||
}
|
||
|
||
//セーブデータのフォーマット&マウント
|
||
scr_Status("Card savedata format",COLOR_YELLO);
|
||
res = savedata.Format(&arcInfo);//_exの方がいいかも
|
||
if(res != RESULT_OK){
|
||
exsave.Unmount();
|
||
PutError(ERC_DEV_CARD);
|
||
return ERROR;
|
||
}
|
||
//パラメータはバックアップ時
|
||
//Top画面開始時、挿入イベントで取得
|
||
if (arcInfo.FileCount == 0)//ファイルが無い
|
||
{
|
||
savedata.Unmount();
|
||
exsave.Unmount();
|
||
return SUCCESS;//フォーマットのみ
|
||
}else{
|
||
scr_Status("file writing",COLOR_PARPL);
|
||
exsave.ResetPath();
|
||
s64 fsize;
|
||
res = RESULT_OK;
|
||
while(res==RESULT_OK){
|
||
res = exsave.GetPath(file_pathw);
|
||
if (res != RESULT_OK)
|
||
{
|
||
if (res == RESULT_DIR_LEVEL_OVER)break;
|
||
if (res == RESULT_PATH_LENGTH_OVER)break;
|
||
res = RESULT_FAIL_OPEN;
|
||
break;
|
||
}
|
||
if (file_pathw[0] == 0)break;//root then end
|
||
if (ct>=arcInfo.FileCount)
|
||
{//カウンタ壊れ、メモリフローの可能性
|
||
res = RESULT_FAIL;
|
||
break;
|
||
}
|
||
|
||
scr_CountPerMax(ct,arcInfo.FileCount);
|
||
if (exsave.Open(file_pathw)==false)
|
||
{
|
||
res = RESULT_FAIL_OPEN;
|
||
break;
|
||
}
|
||
fsize = exsave.FileSize;
|
||
total += fsize;
|
||
|
||
int rev = 0;
|
||
while(1){
|
||
if (savedata.OpenC(file_pathw,fsize,&mkdir)==false)
|
||
{
|
||
if(mkdir)//ディレクトリのみ作成
|
||
{//深い場合に作成が多いと画面が止まるので
|
||
rev++;
|
||
if (rev & 1)scr_MessOnCount2("Create Directry -");
|
||
else scr_MessOnCount2("Create Directry |");
|
||
|
||
}else{
|
||
res = RESULT_FAIL_OPENW;
|
||
//exsave.Close();
|
||
break;
|
||
}
|
||
}else break;
|
||
}
|
||
if (res != RESULT_OK)break;
|
||
|
||
msize = fsize;
|
||
int itvl = 0;
|
||
while(fsize)
|
||
{
|
||
if(msize > FILEBUFF_SIZE * 2){
|
||
scr_CountPerMax2(msize-fsize,msize,0);
|
||
if (++itvl & 1)scr_Status("file copying |",COLOR_PARPL);
|
||
else scr_Status("file copying -",COLOR_PARPL);
|
||
}else scr_MessOnCount2("");
|
||
|
||
if (fsize>FILEBUFF_SIZE)
|
||
{
|
||
rsize = FILEBUFF_SIZE;
|
||
fsize -= FILEBUFF_SIZE;
|
||
}else{
|
||
rsize = fsize;
|
||
fsize = 0;
|
||
}
|
||
if(exsave.Read(fileBuffer,rsize) != rsize )
|
||
{
|
||
res = RESULT_FAIL_READ;
|
||
break;
|
||
}
|
||
if(savedata.Write(fileBuffer,rsize) != rsize )
|
||
{
|
||
res = RESULT_FAIL_WRITE;
|
||
break;
|
||
}
|
||
}
|
||
savedata.CloseW();
|
||
exsave.Close();
|
||
ct++;
|
||
}
|
||
savedata.Close();
|
||
exsave.CloseW();
|
||
}
|
||
savedata.Commit();
|
||
savedata.Unmount();
|
||
exsave.Unmount();
|
||
scr_CountPerMax(ct,arcInfo.FileCount);
|
||
scr_CountPerMax2(0,0,total);
|
||
|
||
//結果
|
||
switch(res){
|
||
case RESULT_OK:
|
||
if (Verifi()==false)return ERROR;
|
||
if(exsave.Delete()){//バックアップを削除
|
||
scr_Status("Complate",COLOR_WHITE);
|
||
return SUCCESS;
|
||
}
|
||
case RESULT_FAIL_WRITE:
|
||
case RESULT_FAIL_OPENW:
|
||
PutError(ERC_DEV_CARD);
|
||
return ERROR;
|
||
case RESULT_FAIL_READ:
|
||
case RESULT_FAIL_OPEN:
|
||
PutError(ERC_DEV_OUT);
|
||
return ERROR;
|
||
}
|
||
//その他、実行時エラー
|
||
PutError(ERC_DEV_OTHER);
|
||
return ERROR;
|
||
}
|
||
|
||
//--------------------------------------------------------------- セーブデータの情報取得
|
||
//呼ぶ前に tmerr のクリアを忘れない事
|
||
void CheckSaveDataState()
|
||
{
|
||
myResult myres;
|
||
|
||
Formatted = false;
|
||
Active = false;
|
||
|
||
if (savedata.GetPrdCode())
|
||
{
|
||
Active = true;//カード
|
||
myres = savedata.IsExist();//セーブマウント
|
||
//NN_LOG("%d",savedata.LastNnResult.GetDescription());
|
||
if (myres == RESULT_OK)
|
||
{
|
||
Formatted=true;
|
||
}else{
|
||
tmerr |= SDATA_ERRPUT_MEDIA;
|
||
}
|
||
}
|
||
scr_PrdCode(savedata.PrdCode);
|
||
//プロダクトコードのチェック
|
||
isAgreePCode = strcmp(savedata.PrdCode,arcInfo.Pcode)==0;
|
||
}
|
||
|
||
void CheckExSaveState()
|
||
{
|
||
exActive = true;//メモリなので常にtrue
|
||
//if (exActive){
|
||
exFormatted = exsave.IsExist() && exsave.GetInfo(&arcInfo);
|
||
//todo:変更時はバージョンチェック入れる
|
||
exsave.Unmount();
|
||
//}else{
|
||
////arcInfo.Pcode[0] = 0;
|
||
// exFormatted = false;
|
||
//}
|
||
if (exFormatted==false) arcInfo.Pcode[0] = 0;
|
||
scr_PrdCodeEx(arcInfo.Pcode);
|
||
//プロダクトコードのチェック
|
||
isAgreePCode = strcmp(savedata.PrdCode,arcInfo.Pcode)==0;
|
||
}
|
||
|
||
|
||
//---------------------------------------------------------------- 入力待ち
|
||
void WaitUI()
|
||
{
|
||
tmerr = 0;
|
||
scr_Draw();//画面更新
|
||
scr_evnt = EVNT_NONE;
|
||
while(scr_evnt == EVNT_NONE)
|
||
{
|
||
nn::os::Thread::Yield();//スレッド実行
|
||
//scr_GetEvnt();//入力イベント
|
||
|
||
CheckSysBreak();//中断処理
|
||
|
||
//挿抜を検知したら中断して抜ける
|
||
//トップ以外ではトップメニューへ戻るようする
|
||
if (ejectEvnt.TryWait()){
|
||
CheckSaveDataState();
|
||
isInsEject = InEx_EjcCard;
|
||
return;
|
||
}
|
||
if (insEvnt.TryWait()){
|
||
CheckSaveDataState();
|
||
isInsEject = InEx_InsCard;
|
||
return;
|
||
}
|
||
}
|
||
ScrClr();//画面消去
|
||
nn::os::Thread::Sleep(nn::fnd::TimeSpan::FromMilliSeconds(500));//チャタ対策
|
||
}
|
||
|
||
tColStr mess_ReadError = {"[Read] is failed",COLOR_RED};
|
||
tColStr mess_WriteError = {"[Write] is failed",COLOR_RED};
|
||
tColStr mess_ReadOk = {"[Read] is Success",COLOR_GREEN};
|
||
tColStr mess_WriteOk = {"[Write] is Success",COLOR_GREEN};
|
||
tColStr mess_PullOut = {"Pull Out",COLOR_RED};
|
||
tColStr mess_ChkDev = {"check device",COLOR_RED};
|
||
tColStr mess_Insexit = {"Detect Insert or pull out",COLOR_RED};
|
||
tColStr mess_WhenExc = {"When you exchange device",COLOR_WHITE};
|
||
tColStr mess_PoffSys = {"PowerOff or return Top Menu",COLOR_WHITE};
|
||
|
||
//---------------------- main loop --------------------------
|
||
void nnMain()
|
||
{
|
||
int i;
|
||
myResult mres;
|
||
RetCode retc;
|
||
//tColStr *colstr[4];
|
||
//colstr[2] = &mess_WhenExc;
|
||
//colstr[3] = &mess_PoffSys;
|
||
isInsEject = InEx_None;
|
||
|
||
//NN_LOG("Start\n");
|
||
nn::os::Initialize();
|
||
nn::fs::Initialize();
|
||
|
||
//DEA-SUPにて推奨のフリーズ暫定対策:無線デーモンを停止 (2011.3.1 現在)
|
||
//ただし、スリープ時は止まらない
|
||
//心配なら本体スイッチで切っとく
|
||
nn::ndm::Initialize();
|
||
nn::ndm::SuspendScheduler();
|
||
|
||
//中断処理の準備
|
||
InitSysBreak((uptr)endfunc);
|
||
// グラフィックスライブラリの初期化は、以降で行わなければならない
|
||
// 他、アプリケーションの初期化処理
|
||
|
||
nn::applet::DisableSleep();//スリープ非対応
|
||
|
||
//Heap
|
||
appHeap.Initialize(nn::os::GetDeviceMemoryAddress(),
|
||
nn::os::GetDeviceMemorySize(), nn::os::ALLOCATE_OPTION_LINEAR);
|
||
const u32 GxHeapSize = 0x800000;
|
||
heapForGx = reinterpret_cast<uptr>(appHeap.Allocate(GxHeapSize));
|
||
//画面初期化
|
||
if (ScrInitialize(heapForGx,GxHeapSize) == false)failstop();
|
||
ejectEvnt.ClearSignal();
|
||
insEvnt.ClearSignal();
|
||
nn::fs::RegisterCardEjectedEvent(&ejectEvnt);
|
||
nn::fs::RegisterCardInsertedEvent(&insEvnt);
|
||
|
||
// スリープ要求に対する返答を有効にする
|
||
// また、蓋の状態チェックを行い蓋が閉じられているならスリープ要求が発生する
|
||
//nn::applet::EnableSleep(true);
|
||
|
||
extern u8 scr_evnt;
|
||
while(1)
|
||
{
|
||
tmerr = 0;
|
||
CheckSaveDataState(); //セーブデータの状態取得
|
||
CheckExSaveState(); //同バックアップ
|
||
if (isAgreePCode==false) tmerr |= SDATA_ERRPUT_PCODE;
|
||
// scr_TopMenu(Formatted,Active,exFormatted,exActive,isAgreePCode,failveri);
|
||
scr_TopMenu(Formatted,Active,exFormatted,exActive,tmerr);
|
||
|
||
|
||
WaitUI();//入力待ち
|
||
if (isInsEject != InEx_None){
|
||
ScrClr();//画面消去
|
||
isInsEject= InEx_None;
|
||
}else
|
||
switch(scr_evnt)
|
||
{
|
||
case EVNT_PUSH_B:
|
||
case EVNT_NONE:
|
||
break;
|
||
case EVNT_PUSH_Y:
|
||
break;
|
||
case EVNT_SEL_READ://リード
|
||
NN_LOG("select read\n");
|
||
retc = Card2Sd();
|
||
switch (retc){
|
||
case SUCCESS:
|
||
//CheckExSaveState(); //状態の更新
|
||
scr_ResultQuit("Success",COLOR_GREEN);//成功とQuitボタン
|
||
break;
|
||
case INSEXIT:
|
||
//scr_InsExitQuit();//挿抜検知表示 & Quit
|
||
ScrClr();//画面消去
|
||
scr_Backup();
|
||
{
|
||
if (isInsEject == InEx_EjcCard)PutError(ERC_DEV_CARD,ERC_DEVICE);
|
||
else PutError(ERC_DEV_OTHER);
|
||
scr_ResultQuit("Break",COLOR_RED);//エラーとQuitボタン
|
||
}
|
||
break;
|
||
case CANCEL:
|
||
//scr_ResultQuit("Canseled",COLOR_YELLO);//キャンセル
|
||
break;
|
||
default://errors
|
||
scr_ResultQuit("Failed",COLOR_RED);//エラーとQuitボタン
|
||
break;
|
||
}
|
||
if (retc == CANCEL)break;
|
||
//isInsEject = InEx_None;
|
||
while(1){
|
||
WaitUI();
|
||
if (isInsEject != InEx_None){
|
||
isInsEject = InEx_None;
|
||
}
|
||
if(scr_evnt==EVNT_QUIT)break;
|
||
if(scr_evnt==EVNT_PUSH_B)break;
|
||
}
|
||
break;
|
||
case EVNT_SEL_WRITE://リストア
|
||
NN_LOG("select write\n");
|
||
retc = Sd2Card();
|
||
switch (retc){
|
||
case SUCCESS:
|
||
//CheckSaveDataState(); //状態の更新
|
||
//CheckExSaveState();
|
||
scr_ResultQuit("Success",COLOR_GREEN);//成功とQuitボタン
|
||
break;
|
||
case INSEXIT:
|
||
//scr_InsExitQuit();//挿抜検知表示 & Quit
|
||
ScrClr();//画面消去
|
||
scr_Backup();
|
||
{
|
||
if (isInsEject == InEx_EjcCard)PutError(ERC_DEV_CARD,ERC_DEVICE);
|
||
else PutError(ERC_DEV_OTHER);
|
||
scr_ResultQuit("Break",COLOR_RED);//エラーとQuitボタン
|
||
}
|
||
break;
|
||
case CANCEL:
|
||
//scr_ResultQuit("Canseled",COLOR_YELLO);//キャンセル
|
||
break;
|
||
default://errors
|
||
scr_ResultQuit("Failed",COLOR_RED);//エラーとQuitボタン
|
||
break;
|
||
}
|
||
if (retc == CANCEL)break;
|
||
while(1){
|
||
WaitUI();
|
||
if (isInsEject != InEx_None){
|
||
isInsEject= InEx_None;
|
||
}
|
||
if(scr_evnt==EVNT_QUIT)break;
|
||
if(scr_evnt==EVNT_PUSH_B)break;
|
||
}
|
||
break;
|
||
/* case EVNT_SEL_END:
|
||
NN_LOG("select end\n");
|
||
finish();
|
||
break;
|
||
case EVNT_NO:
|
||
NN_LOG("select NO\n");
|
||
break;
|
||
case EVNT_YES:
|
||
NN_LOG("select YES\n");
|
||
break;
|
||
*/
|
||
//old debug key
|
||
case EVNT_PUSH_L:// ------------------------------- 表示の切り替え
|
||
case EVNT_PUSH_DOWN_Y:// ---------------------------------- 表示の切り替え
|
||
break;
|
||
|
||
case EVNT_PUSH_LEFT_X:// ------------------------------------- LEFT + X
|
||
//バックアップ削除
|
||
scr_DelConf();//実行確認
|
||
WaitUI();
|
||
if (scr_evnt != EVNT_YES)break;
|
||
exsave.Delete();
|
||
//CheckExSaveState(); //状態更新
|
||
break;
|
||
|
||
case EVNT_PUSH_R:// ------------------------------------------- R
|
||
//CARD(NAND)へテストファイル書き込み
|
||
//デバグモード時のみ
|
||
bool flg;
|
||
scr_ConfirmDbg("dummy savedata write OK?");//実行確認
|
||
WaitUI();
|
||
if (scr_evnt != EVNT_YES)break;
|
||
for ( i =0;i<FILEBUFF_SIZE;i++)fileBuffer[i] = (char)i;
|
||
|
||
//セーブデータのフォーマット&マウント
|
||
arcInfo.FileEntry = 10;
|
||
arcInfo.DirEntry = 10;
|
||
arcInfo.Dup = false;
|
||
mres = savedata.Format(&arcInfo);
|
||
if(mres != RESULT_OK)break;
|
||
|
||
i= 2;
|
||
while(i){
|
||
sprintf(file_path,"dir%d/file%d",i,i);
|
||
mbstowcs(file_pathw,file_path,510);
|
||
if (savedata.OpenC(file_pathw,FILEBUFF_SIZE,&flg)==false)break;
|
||
if(savedata.Write(fileBuffer,FILEBUFF_SIZE)!=FILEBUFF_SIZE)break;
|
||
savedata.CloseW();
|
||
i--;
|
||
}
|
||
i=2;
|
||
while(i){
|
||
sprintf(file_path,"dir%d/dir%d/file%d",i,i,i);
|
||
mbstowcs(file_pathw,file_path,510);
|
||
if (savedata.OpenC(file_pathw,FILEBUFF_SIZE,&flg)==false)break;
|
||
if(savedata.Write(fileBuffer,FILEBUFF_SIZE)!=FILEBUFF_SIZE)break;
|
||
savedata.CloseW();
|
||
|
||
sprintf(file_path,"dir%d/dir%d/file%d",i,i,i+2);
|
||
mbstowcs(file_pathw,file_path,510);
|
||
if (savedata.OpenC(file_pathw,FILEBUFF_SIZE,&flg)==false)break;
|
||
if(savedata.Write(fileBuffer,FILEBUFF_SIZE)!=FILEBUFF_SIZE)break;
|
||
savedata.CloseW();
|
||
i--;
|
||
}
|
||
savedata.CloseW();
|
||
savedata.Commit();
|
||
savedata.Unmount();
|
||
//CheckSaveDataState(); //カード状態更新
|
||
break;
|
||
default:
|
||
failstop();
|
||
break;
|
||
}
|
||
}//while()
|
||
|
||
}
|
||
|
||
/*---------------------------------------------------------------------------*
|
||
End of file
|
||
*---------------------------------------------------------------------------*/
|
||
|