ctr_Repair/trunk/CardSaveData/common/savefile/membak.h

194 lines
5.0 KiB
C++

/*---------------------------------------------------------------------------*
Project: Horizon
File: irp.h
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$
*---------------------------------------------------------------------------*/
#ifndef MEMBAK_H_
#define MEMBAK_H_
#include <nn/types.h>
#include <nn/Result.h>
#include "../my_defs.h"
#include "savefile.h"
#include <nn/math.h>
//メモリ上の仮想バックアップ容量
//SDK更新でアプリ領域が減る可能性あるので無理しない
#define MEM_BKUP_SIZE 10*1024*1024
class MemBak
{
public:
s64 FileSize;
NN_PADDING4;
nn::Result LastNnResult;
private:
struct EntryHeader
{
s16 nameSize;
NN_PADDING2;
s32 dataSize;
s32 nextOffset;
EntryHeader* GetNext()
{
return reinterpret_cast<EntryHeader*>(
reinterpret_cast<bit8*>(this) + nextOffset);
}
bit8* GetDataPointer()
{
return reinterpret_cast<bit8*>(this + 1) + nameSize;
}
const wchar_t* GetName() const
{
return reinterpret_cast<const wchar_t*>(
reinterpret_cast<const bit8*>(this + 1));
}
void SetName(const wchar_t* name)
{
s32 len = std::wcslen(name);
nameSize = len * 2 + 2;
std::memcpy(this + 1, name, nameSize);
}
void SetSize(size_t size)
{
dataSize = size;
}
s32 GetSize() const
{
return dataSize;
}
void SetNext()
{
nextOffset = nn::math::RoundUp(sizeof(*this) + nameSize + dataSize, 4);
}
bool Match(const wchar_t* p)
{
return std::wcscmp(p, GetName()) == 0;
}
bool IsEnd() const
{
return nameSize == 0;
}
};
struct FileState
{
EntryHeader* pE;
s32 pos;
void Init(EntryHeader* p)
{
pE = p;
pos = 0;
}
void Set(const wchar_t* name, size_t size)
{
pE->SetName(name);
pE->SetSize(size);
pE->SetNext();
pE->GetNext()->nameSize = 0;
}
s32 GetSize() const { return pE->GetSize(); }
EntryHeader* GetNext()
{
return pE->GetNext();
}
bit8* GetCurrentPointer()
{
return pE->GetDataPointer() + pos;
}
s32 GetRemain() const
{
return pE->dataSize - pos;
}
void Advance(s32 size)
{
pos += size;
}
s32 Write(const void* p, size_t size)
{
s32 remain = GetRemain();
if( remain <= 0 )
{
return 0;
}
s32 copySize = nn::math::Min<s32>(remain, size);
std::memcpy(GetCurrentPointer(), p, copySize);
Advance(copySize);
return copySize;
}
s32 Read(void* p, size_t size)
{
s32 remain = GetRemain();
if( remain <= 0 )
{
return 0;
}
s32 copySize = nn::math::Min<s32>(remain, size);
std::memcpy(p, GetCurrentPointer(), copySize);
Advance(copySize);
return copySize;
}
};
tArcInfo m_ArcInfo;
FileState m_ForRead;
FileState m_ForWrite;
bool m_Exists;
bool m_IsMounted;
NN_PADDING2;
bit8* m_pBuffer;
size_t m_pBufferSize;
bit8* m_pBufferPos;
EntryHeader* m_pFind;
u32 m_NumDirEntry;
u32 m_NumFileEntry;
public:
MemBak();
~MemBak();
void Finalize();
myResult Create(u32 entryDir,u32 entryFile);
// myResult CreateSys();
myResult Mount();
// myResult MountSys();
bool IsExist();
void Unmount();
bool Delete();
void CloseW();
bool OpenW(wchar_t *path,s64 size);
bool OpenSysW();//暫定
void CloseSysW();//暫定
// bool OpenSysR();//暫定
// void CloseSysR();//暫定
s32 Write(char *buffer,size_t size);
bool WriteSys(tArcInfo *pinfo);
// bool ReadSys(tArcInfo *pinfo);
void ResetPath();
s32 Read(char *buffer,size_t size);
void Close();
bool Open(wchar_t *path);
myResult GetPath(wchar_t *path);
bool GetInfo(tArcInfo *pinfo);
};
#endif