ctr_Repair/trunk/CardSaveData/common/savefile/sdmcwo.cpp

179 lines
5.5 KiB
C++

/*
Horizon/tools/SaveDataFiler で読めるファイルを作成
ライトのみアーカイブ使用
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <cstdio>
#include <iomanip>
#include <wchar.h>
#include <string.h>
#include <nn/fs.h>
#include "sdmcwo.h"
const wchar_t *SDMC_ROOT_DIR_NAME = L"/filer";
//保存先:日時をディレクトリ名
//-------- nandf_util.cpp 参照
std::wstring Sdmcwo::GetDateName()
{
nn::fnd::DateTime tm = nn::fnd::DateTime::GetNow();
std::wostringstream name;
name << std::setw(4) << std::setfill(L'0') << tm.GetYear()
<< std::setw(2) << std::setfill(L'0') << tm.GetMonth()
<< std::setw(2) << std::setfill(L'0') << tm.GetDay()
<< std::setw(2) << std::setfill(L'0') << tm.GetHour()
<< std::setw(2) << std::setfill(L'0') << tm.GetMinute()
<< std::setw(2) << std::setfill(L'0') << tm.GetSecond();
//char型 :表示で使う
//wcstombs(DirName, name.str().c_str(), 14);
//DirName[14] = 0;
wcscpy(DirName,name.str().c_str());
return name.str();
}
bit64 ChangeId(bit64 id, bit64 key)
{
return id ^ key ^ 0xce8a4d52f7105339;
}
//----------------------------------------------------------
bool Sdmcwo::DelDir(const wchar_t *dir)
{
//if (created==false)return true;//未作成ならなにもしない
if (Mount() == RESULT_FAIL_MOUNT)return false;
bool res = DeleteDir(dir);
Unmount();
return res;
}
//直前に作成したDateTimeフォルダ削除
bool Sdmcwo::Delete()
{
return DelDir(latestPath_w);
}
//全削除
bool Sdmcwo::DeleteAll()
{
std::wostringstream woss;
woss << devName << SDMC_ROOT_DIR_NAME;//sdmc:/filer
bool res = DelDir(woss.str().c_str());
if(res)created=false;
else NN_LOG("Deldir fail %d\n",LastNnResult.GetDescription());
return res;
}
//保存先のディレクトリ作成
//成功時はマウント状態、クラスタサイズを設定
bool Sdmcwo::Create()
{
s64 size,size2;
std::wostringstream woss;
created = false;
if (Mount() == RESULT_FAIL_MOUNT)return false;
woss << devName << SDMC_ROOT_DIR_NAME;//sdmc:/filer
if (CreateDir(woss.str().c_str()))
{
woss << L"/" << L"UserSaveData";//sdmc:/filer/UserSaveData
if ( CreateDir(woss.str().c_str()) )
{
DateDirName = GetDateName();
woss << L"/" << DateDirName;//sdmc:/filer/UserSaveData/YearMtDtHrMtSc
if ( CreateDir(woss.str().c_str()) )
{
wcscpy(latestPath_w,woss.str().c_str());
created = true;//日時ディレクトリ存在フラグ
//コピー先ルート
woss << L"/" << "00000000";//sdmc:/filer/UserSaveData/YearMtDtHrMtSc/00000000
if (nn::fs::ResultAlreadyExists::Includes(LastNnResult))
{//既にあれば混じらないよう削除(時計合ってれば起こらないはず)
nn::fs::TryDeleteDirectoryRecursively(woss.str().c_str());//保存先ディレクトリ
wcscpy(pathw_w,woss.str().c_str());
wcscat(pathw_w,L".vnf");
nn::fs::TryDeleteFile(pathw_w);//追記型ファイル
}
GetFreeSize(&size);
if ( CreateDir(woss.str().c_str()) )
{
GetFreeSize(&size2);
FreeSize = size2;
if ((size-size2)<0) ClasterSize = 0;
else ClasterSize = size-size2;//クラスタサイズ
wcscpy(rootPath_w,woss.str().c_str());//格納先パス保存
SetRootPath(rootPath_w);
return true;
}
}
}
}
Unmount();
return false;
}
//パス名エラーで作成できない場合の格納先
bool Sdmcwo::OpenVnfW(wchar_t *path,s64 size)
{
tVnf vnf;
bool res=false;
//パス名&オフセット用ファイル
SetRootPath(latestPath_w);//基底パスを変更
if (OpenAdd(L"/00000000.vnf"))//追記で開く
{
wcscpy(vnf.path,path);//ヘッダ格納
vnf.size = size;
res = (Write((char*)&vnf,sizeof(tVnf)) == sizeof(tVnf));
}
SetRootPath(rootPath_w);//基底パスを戻す
return res;
}
char buff[32768*2];
//情報ファイル
//
bool Sdmcwo::WriteSys(tArcInfo *ifo)
{//SaveFilerで読むためのファイル
if (Mount() == RESULT_FAIL_MOUNT)return false;
bool res = false;
SetRootPath(latestPath_w);//基底パスを変更
// FormatParameter を保存
if(OpenW( L"/00000000.dat" ))
{
FormatParameters Fparam;
Fparam.m_MaxDir = ifo->DirEntry;
Fparam.m_MaxFile = ifo->FileEntry;
Fparam.m_Duplicate = ifo->Dup;
if ( Write((char*)&Fparam, sizeof(FormatParameters)) == sizeof(FormatParameters))
{ //AdditionalInfoを保存
CloseW();
if(OpenW( L"/00000000_.dat"))
{
AdditionalInfo Ainfo;
Ainfo.m_Version = 0;
Ainfo.m_Id = ChangeId(Ainfo.m_Id, static_cast<bit64>(std::wcstoll(DateDirName.c_str(), NULL, 10)));
res = Write((char*)&Ainfo, sizeof(AdditionalInfo)) == sizeof(AdditionalInfo);
}
}
}
CloseW();
Unmount();
SetRootPath(rootPath_w);//基底パスを戻す
return res;
}