Complete base API

This commit is contained in:
Phillip Stephens 2016-06-30 02:53:13 -07:00
parent 2a9c18534a
commit 4d9bb795b9
13 changed files with 488 additions and 46 deletions

View File

@ -1,9 +1,9 @@
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.0)
project(kabufuda) project(kabufuda)
if(NOT TARGET urde) if(NOT TARGET hecl)
if(NOT MSVC) if(NOT MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
endif() endif()
endif() endif()
@ -20,6 +20,4 @@ add_library(kabufuda STATIC
include/kabufuda/SRAM.hpp lib/kabufuda/SRAM.cpp include/kabufuda/SRAM.hpp lib/kabufuda/SRAM.cpp
include/kabufuda/WideStringConvert.hpp lib/kabufuda/WideStringConvert.cpp) include/kabufuda/WideStringConvert.hpp lib/kabufuda/WideStringConvert.cpp)
if (NOT TARGET hecl)
add_subdirectory(test) add_subdirectory(test)
endif()

View File

@ -863,7 +863,9 @@ RECURSIVE = YES
# Note that relative paths are relative to the directory from which doxygen is # Note that relative paths are relative to the directory from which doxygen is
# run. # run.
EXCLUDE = EXCLUDE = include/utf8proc.h \
include/kabufuda/Util.hpp \
include/kabufuda/SRAM.hpp
# The EXCLUDE_SYMLINKS tag can be used to select whether or not files or # The EXCLUDE_SYMLINKS tag can be used to select whether or not files or
# directories that are symbolic links (a Unix file system feature) are excluded # directories that are symbolic links (a Unix file system feature) are excluded

View File

@ -31,7 +31,7 @@ class BlockAllocationTable
public: public:
explicit BlockAllocationTable(uint32_t blockCount = (uint32_t(ECardSize::Card2043Mb) * MbitToBlocks)); explicit BlockAllocationTable(uint32_t blockCount = (uint32_t(ECardSize::Card2043Mb) * MbitToBlocks));
BlockAllocationTable(uint8_t data[BlockSize]); BlockAllocationTable(uint8_t data[BlockSize]);
~BlockAllocationTable() {} ~BlockAllocationTable();
uint16_t getNextBlock(uint16_t block) const; uint16_t getNextBlock(uint16_t block) const;
uint16_t nextFreeBlock(uint16_t maxBlock, uint16_t startingBlock) const; uint16_t nextFreeBlock(uint16_t maxBlock, uint16_t startingBlock) const;

View File

@ -59,9 +59,10 @@ class Card
char m_game[5] = {'\0'}; char m_game[5] = {'\0'};
char m_maker[3] = {'\0'}; char m_maker[3] = {'\0'};
void swapEndian(); void _swapEndian();
void updateDirAndBat(); void _updateDirAndBat();
void updateChecksum(); void _updateChecksum();
File* _fileFromHandle(const std::unique_ptr<IFileHandle>& fh) const;
public: public:
Card(); Card();
Card(const Card& other); Card(const Card& other);
@ -73,50 +74,162 @@ public:
* @param filename * @param filename
*/ */
std::unique_ptr<IFileHandle> openFile(const char* filename); std::unique_ptr<IFileHandle> openFile(const char* filename);
/** /**
* @brief createFile * @brief createFile
* @param filename * @param filename
* @return * @return
*/ */
std::unique_ptr<IFileHandle> createFile(const char* filename, size_t size); std::unique_ptr<IFileHandle> createFile(const char* filename, size_t size);
/**
* @brief deleteFile
* @param fh
*/
void deleteFile(const std::unique_ptr<IFileHandle>& fh); void deleteFile(const std::unique_ptr<IFileHandle>& fh);
/**
* @brief write
* @param fh
* @param buf
* @param size
*/
void write(const std::unique_ptr<IFileHandle>& fh, const void* buf, size_t size); void write(const std::unique_ptr<IFileHandle>& fh, const void* buf, size_t size);
/**
* @brief read
* @param fh
* @param dst
* @param size
*/
void read(const std::unique_ptr<IFileHandle>& fh, void* dst, size_t size); void read(const std::unique_ptr<IFileHandle>& fh, void* dst, size_t size);
/**
* @brief seek
* @param fh
* @param pos
* @param whence
*/
void seek(const std::unique_ptr<IFileHandle>& fh, int32_t pos, SeekOrigin whence); void seek(const std::unique_ptr<IFileHandle>& fh, int32_t pos, SeekOrigin whence);
/**
* @brief Returns the current offset of the specified file
* @param fh The file to retrieve the offset from
* @return The offset or -1 if an invalid handle is passed
*/
int32_t tell(const std::unique_ptr<IFileHandle>& fh);
/**
* @brief setPublic
* @param fh
* @param pub
*/
void setPublic(const std::unique_ptr<IFileHandle>& fh, bool pub);
/**
* @brief isPublic
* @param fh
* @return
*/
bool isPublic(const std::unique_ptr<IFileHandle>& fh) const;
/**
* @brief setCanCopy
* @param fh
* @param copy
*/
void setCanCopy(const std::unique_ptr<IFileHandle>& fh, bool copy) const;
/**
* @brief canCopy
* @param fh
* @return
*/
bool canCopy(const std::unique_ptr<IFileHandle>& fh) const;
/**
* @brief setCanMove
* @param fh
* @param move
*/
void setCanMove(const std::unique_ptr<IFileHandle>& fh, bool move);
/**
* @brief canMove
* @param fh
* @return
*/
bool canMove(const std::unique_ptr<IFileHandle>& fh) const;
void setBannerFormat(const std::unique_ptr<IFileHandle>& fh, EImageFormat fmt);
EImageFormat bannerFormat(const std::unique_ptr<IFileHandle>& fh) const;
void setIconAnimationType(const std::unique_ptr<IFileHandle>& fh, EAnimationType type);
EAnimationType iconAnimationType(const std::unique_ptr<IFileHandle>& fh) const;
void setIconFormat(const std::unique_ptr<IFileHandle>& fh, uint32_t idx, EImageFormat fmt);
EImageFormat iconFormat(const std::unique_ptr<IFileHandle>& fh, uint32_t idx) const;
void setIconSpeed(const std::unique_ptr<IFileHandle>& fh, uint32_t idx, EAnimationSpeed speed);
EAnimationSpeed iconSpeed(const std::unique_ptr<IFileHandle>& fh, uint32_t idx) const;
void setIconAddress(const std::unique_ptr<IFileHandle>& fh, uint32_t addr);
int32_t iconAddress(const std::unique_ptr<IFileHandle>& fh) const;
void setCommentAddress(const std::unique_ptr<IFileHandle>& fh, uint32_t addr);
int32_t commentAddress(const std::unique_ptr<IFileHandle>& fh) const;
/**
* @brief Copies a file from the current Card instance to a specified Card instance
* @param fh The file to copy
* @param dest The destination Card instance
* @return True if successful, false otherwise
*/
bool copyFileTo(const std::unique_ptr<IFileHandle>&fh, Card& dest);
/**
* @brief moveFileTo
* @param fh
* @param dest
* @return
*/
bool moveFileTo(const std::unique_ptr<IFileHandle>&fh, Card& dest);
/** /**
* @brief Sets the current game, if not null any openFile requests will only return files that match this game * @brief Sets the current game, if not null any openFile requests will only return files that match this game
* @param game The target game id, e.g "GM8E" * @param game The target game id, e.g "GM8E"
* @sa openFile * @sa openFile
*/ */
void setGame(const char* game); void setGame(const char* game);
/** /**
* @brief Returns the currently selected game * @brief Returns the currently selected game
* @return The selected game, or nullptr * @return The selected game, or nullptr
*/ */
const uint8_t* getGame() const; const uint8_t* getGame() const;
/** /**
* @brief Sets the current maker, if not null any openFile requests will only return files that match this maker * @brief Sets the current maker, if not null any openFile requests will only return files that match this maker
* @param maker The target maker id, e.g "01" * @param maker The target maker id, e.g "01"
* @sa openFile * @sa openFile
*/ */
void setMaker(const char* maker); void setMaker(const char* maker);
/** /**
* @brief Returns the currently selected maker * @brief Returns the currently selected maker
* @return The selected maker, or nullptr * @return The selected maker, or nullptr
*/ */
const uint8_t* getMaker() const; const uint8_t* getMaker() const;
/** /**
* @brief Retrieves the format assigned serial in two 32bit parts * @brief Retrieves the format assigned serial in two 32bit parts
* @param s0 * @param s0
* @param s1 * @param s1
*/ */
void getSerial(uint32_t* s0, uint32_t* s1); void getSerial(uint32_t* s0, uint32_t* s1);
/** /**
* @brief Retrieves * @brief Retrieves the checksum values of the Card system header
* @param checksum * @param checksum The checksum of the system header
* @param inverse * @param inverse The inverser checksum of the system header
*/ */
void getChecksum(uint16_t* checksum, uint16_t* inverse); void getChecksum(uint16_t* checksum, uint16_t* inverse);
/** /**
* @brief Formats the memory card and assigns a new serial * @brief Formats the memory card and assigns a new serial
* @param size The desired size of the file @sa ECardSize * @param size The desired size of the file @sa ECardSize
@ -125,11 +238,15 @@ public:
void format(EDeviceId deviceId, ECardSize size = ECardSize::Card2043Mb, EEncoding encoding = EEncoding::ASCII); void format(EDeviceId deviceId, ECardSize size = ECardSize::Card2043Mb, EEncoding encoding = EEncoding::ASCII);
/** /**
* @brief getSizeMbit * @brief Returns the size of the file in Megabits from a file on disk, useful for determining filesize ahead of time.
* @return * @return Size of file in Megabits
*/ */
static uint32_t getSizeMbitFromFile(const SystemString& filename); static uint32_t getSizeMbitFromFile(const SystemString& filename);
/**
* @brief Writes any changes to the Card instance immediately to disk. <br />
* <b>Note:</b> <i>Under normal circumstances there is no need to call this function.</i>
*/
void commit(); void commit();
operator bool() const; operator bool() const;

View File

@ -2,6 +2,7 @@
#define __KABU_CONSTANTS_HPP__ #define __KABU_CONSTANTS_HPP__
#include <stdint.h> #include <stdint.h>
#include "Util.hpp"
namespace kabufuda namespace kabufuda
{ {
@ -16,13 +17,34 @@ uint32_t constexpr BATSize = 0xFFB;
*/ */
enum class EPermissions : uint8_t enum class EPermissions : uint8_t
{ {
Public = (1 << 2),
NoCopy = (1 << 3),
NoMove = (1 << 4),
Global = (1 << 5),
Company = (1 << 6)
};
ENABLE_BITWISE_ENUM(EPermissions)
enum class EImageFormat : uint8_t
{
None,
C8,
RGB5A3,
}; };
/** enum class EAnimationType
* @brief The EBannerFlags enum
*/
enum class EBannerFlags : uint8_t
{ {
Loop = 0,
Bounce = 2,
};
enum class EAnimationSpeed
{
End,
Fast,
Middle,
Slow,
}; };
enum class SeekOrigin enum class SeekOrigin
@ -54,6 +76,11 @@ enum class ECardSize : uint16_t
Card2043Mb = 0x80 Card2043Mb = 0x80
}; };
static constexpr uint32_t BannerWidth = 96;
static constexpr uint32_t BannerHeight = 64;
static constexpr uint32_t IconWidth = 32;
static constexpr uint32_t IconHeight = 32;
/** /**
* @brief The EEncoding enum * @brief The EEncoding enum
*/ */
@ -63,4 +90,5 @@ enum class EEncoding : uint16_t
SJIS /**< SJIS Encoding for japanese */ SJIS /**< SJIS Encoding for japanese */
}; };
} }
#endif // __KABU_CONSTANTS_HPP__ #endif // __KABU_CONSTANTS_HPP__

View File

@ -32,7 +32,7 @@ public:
Directory(uint8_t data[BlockSize]); Directory(uint8_t data[BlockSize]);
Directory(const Directory& other); Directory(const Directory& other);
void operator=(const Directory& other); void operator=(const Directory& other);
~Directory() {} ~Directory();
File* getFirstFreeFile(const char* game, const char* maker, const char* filename); File* getFirstFreeFile(const char* game, const char* maker, const char* filename);
File* getFile(const char* game, const char* maker, const char* filename); File* getFile(const char* game, const char* maker, const char* filename);

View File

@ -21,10 +21,10 @@ class File
uint8_t m_bannerFlags; uint8_t m_bannerFlags;
char m_filename[0x20]; char m_filename[0x20];
uint32_t m_modifiedTime; uint32_t m_modifiedTime;
uint32_t m_imageOffset; uint32_t m_iconAddress;
uint16_t m_iconFmt; uint16_t m_iconFmt;
uint16_t m_animSpeed; uint16_t m_animSpeed;
uint8_t m_permissions; EPermissions m_permissions;
int8_t m_copyCounter; int8_t m_copyCounter;
uint16_t m_firstBlock; uint16_t m_firstBlock;
uint16_t m_blockCount; uint16_t m_blockCount;

View File

@ -33,6 +33,38 @@
#undef bswap32 #undef bswap32
#undef bswap64 #undef bswap64
#ifndef ENABLE_BITWISE_ENUM
#define ENABLE_BITWISE_ENUM(type)\
inline type operator|(type a, type b)\
{\
using T = std::underlying_type_t<type>;\
return type(static_cast<T>(a) | static_cast<T>(b));\
}\
inline type operator&(type a, type b)\
{\
using T = std::underlying_type_t<type>;\
return type(static_cast<T>(a) & static_cast<T>(b));\
}\
inline type& operator|=(type& a, const type& b)\
{\
using T = std::underlying_type_t<type>;\
a = type(static_cast<T>(a) | static_cast<T>(b));\
return a;\
}\
inline type& operator&=(type& a, const type& b)\
{\
using T = std::underlying_type_t<type>;\
a = type(static_cast<T>(a) & static_cast<T>(b));\
return a;\
}\
inline type operator~(const type& key)\
{\
using T = std::underlying_type_t<type>;\
return type(~static_cast<T>(key));\
}
#endif
namespace kabufuda namespace kabufuda
{ {

View File

@ -41,6 +41,9 @@ BlockAllocationTable::BlockAllocationTable(uint32_t blockCount)
updateChecksum(); updateChecksum();
} }
BlockAllocationTable::~BlockAllocationTable()
{}
uint16_t BlockAllocationTable::getNextBlock(uint16_t block) const uint16_t BlockAllocationTable::getNextBlock(uint16_t block) const
{ {
if ((block < FSTBlocks) || (block > (BATSize - FSTBlocks))) if ((block < FSTBlocks) || (block > (BATSize - FSTBlocks)))

View File

@ -33,7 +33,7 @@ FileHandle::~FileHandle()
{} {}
void Card::swapEndian() void Card::_swapEndian()
{ {
m_formatTime = SBig(m_formatTime); m_formatTime = SBig(m_formatTime);
m_sramBias = SBig(m_sramBias); m_sramBias = SBig(m_sramBias);
@ -77,7 +77,7 @@ Card::Card(const SystemString& filename, const char* game, const char* maker)
{ {
fread(__raw, 1, BlockSize, m_fileHandle); fread(__raw, 1, BlockSize, m_fileHandle);
m_maxBlock = m_sizeMb * MbitToBlocks; m_maxBlock = m_sizeMb * MbitToBlocks;
swapEndian(); _swapEndian();
fread(m_dir.__raw, 1, BlockSize, m_fileHandle); fread(m_dir.__raw, 1, BlockSize, m_fileHandle);
fread(m_dirBackup.__raw, 1, BlockSize, m_fileHandle); fread(m_dirBackup.__raw, 1, BlockSize, m_fileHandle);
fread(m_bat.__raw, 1, BlockSize, m_fileHandle); fread(m_bat.__raw, 1, BlockSize, m_fileHandle);
@ -144,7 +144,7 @@ std::unique_ptr<IFileHandle> Card::openFile(const char* filename)
return nullptr; return nullptr;
} }
void Card::updateDirAndBat() void Card::_updateDirAndBat()
{ {
Directory updateDir = *m_currentDir; Directory updateDir = *m_currentDir;
updateDir.m_updateCounter++; updateDir.m_updateCounter++;
@ -157,16 +157,29 @@ void Card::updateDirAndBat()
std::swap(m_currentBat, m_previousBat); std::swap(m_currentBat, m_previousBat);
} }
void Card::updateChecksum() void Card::_updateChecksum()
{ {
swapEndian(); _swapEndian();
calculateChecksumBE(reinterpret_cast<uint16_t*>(__raw), 0xFE, &m_checksum, &m_checksumInv); calculateChecksumBE(reinterpret_cast<uint16_t*>(__raw), 0xFE, &m_checksum, &m_checksumInv);
swapEndian(); _swapEndian();
}
File* Card::_fileFromHandle(const std::unique_ptr<IFileHandle> &fh) const
{
if (!fh)
return nullptr;
FileHandle* handle = dynamic_cast<FileHandle*>(fh.get());
if (!handle)
return nullptr;
File* file = m_currentDir->getFile(handle->game, handle->maker, handle->filename);
return file;
} }
std::unique_ptr<IFileHandle> Card::createFile(const char* filename, size_t size) std::unique_ptr<IFileHandle> Card::createFile(const char* filename, size_t size)
{ {
updateDirAndBat(); _updateDirAndBat();
File* f = m_currentDir->getFirstFreeFile(m_game, m_maker, filename); File* f = m_currentDir->getFirstFreeFile(m_game, m_maker, filename);
uint16_t block = m_currentBat->allocateBlocks(uint16_t(size / BlockSize), m_maxBlock); uint16_t block = m_currentBat->allocateBlocks(uint16_t(size / BlockSize), m_maxBlock);
if (f && block != 0xFFFF) if (f && block != 0xFFFF)
@ -183,7 +196,7 @@ std::unique_ptr<IFileHandle> Card::createFile(const char* filename, size_t size)
void Card::deleteFile(const std::unique_ptr<IFileHandle> &fh) void Card::deleteFile(const std::unique_ptr<IFileHandle> &fh)
{ {
updateDirAndBat(); _updateDirAndBat();
if (!fh) if (!fh)
return; return;
FileHandle* f = dynamic_cast<FileHandle*>(fh.get()); FileHandle* f = dynamic_cast<FileHandle*>(fh.get());
@ -321,6 +334,236 @@ void Card::seek(const std::unique_ptr<IFileHandle> &fh, int32_t pos, SeekOrigin
} }
} }
int32_t Card::tell(const std::unique_ptr<IFileHandle> &fh)
{
if (!fh)
return -1;
FileHandle* handle = dynamic_cast<FileHandle*>(fh.get());
if (!handle)
return -1;
return handle->offset;
}
void Card::setPublic(const std::unique_ptr<IFileHandle>& fh, bool pub)
{
File* file = _fileFromHandle(fh);
if (!file)
return;
if (pub)
file->m_permissions |= EPermissions::Public;
else
file->m_permissions &= ~EPermissions::Public;
}
bool Card::isPublic(const std::unique_ptr<IFileHandle> &fh) const
{
File* file = _fileFromHandle(fh);
if (!file)
return false;
return bool(file->m_permissions & EPermissions::Public);
}
void Card::setCanCopy(const std::unique_ptr<IFileHandle> &fh, bool copy) const
{
File* file = _fileFromHandle(fh);
if (!file)
return;
if (copy)
file->m_permissions &= ~EPermissions::NoCopy;
else
file->m_permissions |= EPermissions::NoCopy;
}
bool Card::canCopy(const std::unique_ptr<IFileHandle> &fh) const
{
File* file = _fileFromHandle(fh);
if (!file)
return false;
return !bool(file->m_permissions & EPermissions::NoCopy);
}
void Card::setCanMove(const std::unique_ptr<IFileHandle> &fh, bool move)
{
File* file = _fileFromHandle(fh);
if (!file)
return;
if (move)
file->m_permissions &= ~EPermissions::NoMove;
else
file->m_permissions |= EPermissions::NoMove;
}
bool Card::canMove(const std::unique_ptr<IFileHandle> &fh) const
{
File* file = _fileFromHandle(fh);
if (!file)
return false;
return !bool(file->m_permissions & EPermissions::NoMove);
}
void Card::setBannerFormat(const std::unique_ptr<IFileHandle>& fh, EImageFormat fmt)
{
File* file = _fileFromHandle(fh);
if (!file)
return;
file->m_bannerFlags = (file->m_bannerFlags & ~3) | (uint8_t(fmt));
}
EImageFormat Card::bannerFormat(const std::unique_ptr<IFileHandle> &fh) const
{
File* file = _fileFromHandle(fh);
if (!file)
return EImageFormat::None;
return EImageFormat(file->m_bannerFlags & 3);
}
void Card::setIconAnimationType(const std::unique_ptr<IFileHandle> &fh, EAnimationType type)
{
File* file = _fileFromHandle(fh);
if (!file)
return;
file->m_bannerFlags = (file->m_bannerFlags & ~4) | uint8_t(type);
}
EAnimationType Card::iconAnimationType(const std::unique_ptr<IFileHandle> &fh) const
{
File* file = _fileFromHandle(fh);
if (!file)
return EAnimationType::Loop;
return EAnimationType(file->m_bannerFlags & 4);
}
void Card::setIconFormat(const std::unique_ptr<IFileHandle> &fh, uint32_t idx, EImageFormat fmt)
{
File* file = _fileFromHandle(fh);
if (!file)
return;
file->m_iconFmt = (file->m_iconFmt & ~(3 << (2 * idx))) | (uint16_t(fmt) << (2 * idx));
}
EImageFormat Card::iconFormat(const std::unique_ptr<IFileHandle> &fh, uint32_t idx) const
{
File* file = _fileFromHandle(fh);
if (!file)
return EImageFormat::None;
return EImageFormat(file->m_iconFmt >> (2 * (idx)) & 3);
}
void Card::setIconSpeed(const std::unique_ptr<IFileHandle> &fh, uint32_t idx, EAnimationSpeed speed)
{
File* file = _fileFromHandle(fh);
if (!file)
return;
file->m_animSpeed = (file->m_animSpeed & ~(3 << (2 * idx))) | (uint16_t(speed) << (2 * idx));
}
EAnimationSpeed Card::iconSpeed(const std::unique_ptr<IFileHandle> &fh, uint32_t idx) const
{
File* file = _fileFromHandle(fh);
if (!file)
return EAnimationSpeed::End;
return EAnimationSpeed((file->m_animSpeed >> (2 * (idx))) & 3);
}
void Card::setIconAddress(const std::unique_ptr<IFileHandle> &fh, uint32_t addr)
{
File* file = _fileFromHandle(fh);
if (!file)
return;
file->m_iconAddress = addr;
}
int32_t Card::iconAddress(const std::unique_ptr<IFileHandle> &fh) const
{
File* file = _fileFromHandle(fh);
if (!file)
return -1;
return file->m_iconAddress;
}
void Card::setCommentAddress(const std::unique_ptr<IFileHandle> &fh, uint32_t addr)
{
File* file = _fileFromHandle(fh);
if (!file)
return;
file->m_commentAddr = addr;
}
int32_t Card::commentAddress(const std::unique_ptr<IFileHandle> &fh) const
{
File* file = _fileFromHandle(fh);
if (!file)
return -1;
return file->m_commentAddr;
}
bool Card::copyFileTo(const std::unique_ptr<IFileHandle> &fh, Card &dest)
{
if (!fh)
return false;
/* Do a self test to avoid adding a file to itself */
if (this == &dest)
return false;
/* Check to make sure dest does not already contain fh */
if (dest._fileFromHandle(fh) != nullptr)
return false;
/* Now to add fh */
File* toCopy = _fileFromHandle(fh);
if (!toCopy)
return false;
/* Try to allocate a new file */
std::unique_ptr<IFileHandle> tmpHandle = dest.createFile(toCopy->m_filename, toCopy->m_blockCount * BlockSize);
if (!tmpHandle)
return false;
/* Now copy the file information over */
File* copyDest = dest._fileFromHandle(tmpHandle);
File copyTmp = *copyDest;
*copyDest = *toCopy;
copyDest->m_firstBlock = copyTmp.m_firstBlock;
copyDest->m_copyCounter++;
/* Finally lets get the data copied over! */
uint32_t len = toCopy->m_blockCount * BlockSize;
uint32_t oldPos = tell(fh);
seek(fh, 0, SeekOrigin::Begin);
while (len > 0)
{
uint8_t tmp[BlockSize];
read(fh, tmp, BlockSize);
dest.write(tmpHandle, tmp, BlockSize);
len -= BlockSize;
}
seek(fh, oldPos, SeekOrigin::Begin);
return true;
}
bool Card::moveFileTo(const std::unique_ptr<IFileHandle> &fh, Card &dest)
{
if (copyFileTo(fh, dest))
{
deleteFile(fh);
return true;
}
return false;
}
void Card::setGame(const char* game) void Card::setGame(const char* game)
{ {
if (game == nullptr) if (game == nullptr)
@ -399,7 +642,7 @@ void Card::format(EDeviceId id, ECardSize size, EEncoding encoding)
m_sizeMb = uint16_t(size); m_sizeMb = uint16_t(size);
m_maxBlock = m_sizeMb * MbitToBlocks; m_maxBlock = m_sizeMb * MbitToBlocks;
m_encoding = uint16_t(encoding); m_encoding = uint16_t(encoding);
updateChecksum(); _updateChecksum();
m_dir = Directory(); m_dir = Directory();
m_dirBackup = m_dir; m_dirBackup = m_dir;
m_bat = BlockAllocationTable(uint32_t(size) * MbitToBlocks); m_bat = BlockAllocationTable(uint32_t(size) * MbitToBlocks);
@ -416,9 +659,9 @@ void Card::format(EDeviceId id, ECardSize size, EEncoding encoding)
if (m_fileHandle) if (m_fileHandle)
{ {
swapEndian(); _swapEndian();
fwrite(__raw, 1, BlockSize, m_fileHandle); fwrite(__raw, 1, BlockSize, m_fileHandle);
swapEndian(); _swapEndian();
Directory tmpDir = m_dir; Directory tmpDir = m_dir;
tmpDir.swapEndian(); tmpDir.swapEndian();
fwrite(tmpDir.__raw, 1, BlockSize, m_fileHandle); fwrite(tmpDir.__raw, 1, BlockSize, m_fileHandle);
@ -453,9 +696,9 @@ void Card::commit()
{ {
rewind(m_fileHandle); rewind(m_fileHandle);
swapEndian(); _swapEndian();
fwrite(__raw, 1, BlockSize, m_fileHandle); fwrite(__raw, 1, BlockSize, m_fileHandle);
swapEndian(); _swapEndian();
Directory tmpDir = m_dir; Directory tmpDir = m_dir;
tmpDir.updateChecksum(); tmpDir.updateChecksum();
tmpDir.swapEndian(); tmpDir.swapEndian();
@ -482,7 +725,7 @@ Card::operator bool() const
uint16_t ckSum, ckSumInv; uint16_t ckSum, ckSumInv;
Card tmp = *this; Card tmp = *this;
tmp.swapEndian(); tmp._swapEndian();
calculateChecksumBE(reinterpret_cast<const uint16_t*>(tmp.__raw), 0xFE, &ckSum, &ckSumInv); calculateChecksumBE(reinterpret_cast<const uint16_t*>(tmp.__raw), 0xFE, &ckSum, &ckSumInv);
if (SBig(ckSum) != m_checksum || SBig(ckSumInv) != m_checksumInv) if (SBig(ckSum) != m_checksum || SBig(ckSumInv) != m_checksumInv)
return false; return false;

View File

@ -53,6 +53,10 @@ void Directory::operator=(const Directory& other)
memcpy(__raw, other.__raw, BlockSize); memcpy(__raw, other.__raw, BlockSize);
} }
Directory::~Directory()
{
}
File* Directory::getFirstFreeFile(const char* game, const char* maker, const char* filename) File* Directory::getFirstFreeFile(const char* game, const char* maker, const char* filename)
{ {
for (uint16_t i = 0 ; i < 127 ; i++) for (uint16_t i = 0 ; i < 127 ; i++)

View File

@ -3,6 +3,11 @@
namespace kabufuda namespace kabufuda
{ {
File::File()
{
memset(__raw, 0xFF, 0x40);
}
File::File(char data[]) File::File(char data[])
{ {
memcpy(__raw, data, 0x40); memcpy(__raw, data, 0x40);
@ -10,7 +15,7 @@ File::File(char data[])
File::File(const char* filename) File::File(const char* filename)
{ {
memset(__raw, 0xFF, 0x40); memset(__raw, 0, 0x40);
memset(m_filename, 0, 32); memset(m_filename, 0, 32);
size_t len = strlen(filename); size_t len = strlen(filename);
len = std::min<size_t>(len, 32); len = std::min<size_t>(len, 32);
@ -19,7 +24,7 @@ File::File(const char* filename)
void File::swapEndian() void File::swapEndian()
{ {
m_modifiedTime = SBig(m_modifiedTime); m_modifiedTime = SBig(m_modifiedTime);
m_imageOffset = SBig(m_imageOffset); m_iconAddress = SBig(m_iconAddress);
m_iconFmt = SBig(m_iconFmt); m_iconFmt = SBig(m_iconFmt);
m_animSpeed = SBig(m_animSpeed); m_animSpeed = SBig(m_animSpeed);
m_firstBlock = SBig(m_firstBlock); m_firstBlock = SBig(m_firstBlock);
@ -27,9 +32,4 @@ void File::swapEndian()
m_reserved2 = SBig(m_reserved2); m_reserved2 = SBig(m_reserved2);
m_commentAddr = SBig(m_commentAddr); m_commentAddr = SBig(m_commentAddr);
} }
File::File()
{
memset(__raw, 0xFF, 0x40);
}
} }

View File

@ -6,16 +6,28 @@ int main()
kabufuda::Card mc{_S("test.USA.raw"), "GM8E", "01"}; kabufuda::Card mc{_S("test.USA.raw"), "GM8E", "01"};
if (!mc) if (!mc)
mc.format(kabufuda::EDeviceId::SlotA, kabufuda::ECardSize::Card2043Mb); mc.format(kabufuda::EDeviceId::SlotA, kabufuda::ECardSize::Card2043Mb);
kabufuda::Card mc2{_S("test2.USA.raw"), "GM8E", "01"};
if (!mc2)
mc2.format(kabufuda::EDeviceId::SlotA, kabufuda::ECardSize::Card2043Mb);
std::unique_ptr<kabufuda::IFileHandle> f = mc.openFile("MetroidPrime B"); std::unique_ptr<kabufuda::IFileHandle> f = mc.openFile("MetroidPrime B");
if (!f) if (!f)
{
f = mc.createFile("MetroidPrime B", kabufuda::BlockSize); f = mc.createFile("MetroidPrime B", kabufuda::BlockSize);
mc.setPublic(f, true);
mc.setCanCopy(f, true);
mc.setCanMove(f, true);
mc.setIconAddress(f, mc.commentAddress(f) + 64);
}
if (f) if (f)
{ {
mc.setBannerFormat(f, kabufuda::EImageFormat::C8);
mc.setIconFormat(f, 0, kabufuda::EImageFormat::C8);
mc.setIconSpeed(f, 0, kabufuda::EAnimationSpeed::Middle);
const char* test = "Metroid Prime B is Cool"; const char* test = "Metroid Prime B is Cool";
size_t len = strlen(test); size_t len = strlen(test);
uint8_t data[kabufuda::BlockSize] = {};
mc.write(f, data, kabufuda::BlockSize);
mc.seek(f, 0, kabufuda::SeekOrigin::Begin); mc.seek(f, 0, kabufuda::SeekOrigin::Begin);
mc.write(f, test, len + 1); mc.write(f, test, len + 1);
uint16_t derp = 1234; uint16_t derp = 1234;
@ -24,7 +36,10 @@ int main()
mc.seek(f, -2, kabufuda::SeekOrigin::Current); mc.seek(f, -2, kabufuda::SeekOrigin::Current);
mc.read(f, &derp, 2); mc.read(f, &derp, 2);
std::cout << derp << std::endl; std::cout << derp << std::endl;
mc.deleteFile(f); if (mc.copyFileTo(f, mc2))
printf("Copy succeeded!\n");
else
printf("Copy failed...\n");
} }
return 0; return 0;
} }