New code style refactor

This commit is contained in:
Jack Andersen 2018-12-07 19:20:24 -10:00
parent a1e2242691
commit f126245eef
22 changed files with 1906 additions and 2181 deletions

View File

@ -1,5 +1,5 @@
---
IndentWidth: 4
BasedOnStyle: LLVM
ColumnLimit: 120
UseTab: Never
---
@ -8,7 +8,6 @@ DerivePointerAlignment: false
PointerAlignment: Left
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
BreakBeforeBraces: Allman
IndentCaseLabels: false
AllowShortBlocksOnASingleLine: true
AlignOperands: true
@ -24,6 +23,6 @@ NamespaceIndentation: None
BinPackArguments: true
BinPackParameters: true
SortIncludes: false
AccessModifierOffset: -4
AccessModifierOffset: -2
ConstructorInitializerIndentWidth: 0
ConstructorInitializerAllOnOneLineOrOnePerLine: true

View File

@ -11,40 +11,38 @@ using SizeReturn = DWORD;
#include "Util.hpp"
#include <vector>
namespace kabufuda
{
namespace kabufuda {
class AsyncIO
{
class AsyncIO {
#ifndef _WIN32
int m_fd = -1;
std::vector<std::pair<struct aiocb, SizeReturn>> m_queue;
int m_fd = -1;
std::vector<std::pair<struct aiocb, SizeReturn>> m_queue;
#else
HANDLE m_fh = INVALID_HANDLE_VALUE;
std::vector<std::pair<OVERLAPPED, SizeReturn>> m_queue;
HANDLE m_fh = INVALID_HANDLE_VALUE;
std::vector<std::pair<OVERLAPPED, SizeReturn>> m_queue;
#endif
void _waitForOperation(size_t qIdx) const;
size_t m_maxBlock = 0;
void _waitForOperation(size_t qIdx) const;
size_t m_maxBlock = 0;
public:
AsyncIO() = default;
AsyncIO(SystemStringView filename, bool truncate = false);
~AsyncIO();
AsyncIO(AsyncIO&& other);
AsyncIO& operator=(AsyncIO&& other);
AsyncIO(const AsyncIO* other) = delete;
AsyncIO& operator=(const AsyncIO& other) = delete;
void resizeQueue(size_t queueSz) { m_queue.resize(queueSz); }
bool asyncRead(size_t qIdx, void* buf, size_t length, off_t offset);
bool asyncWrite(size_t qIdx, const void* buf, size_t length, off_t offset);
ECardResult pollStatus(size_t qIdx, SizeReturn* szRet = nullptr) const;
ECardResult pollStatus() const;
void waitForCompletion() const;
AsyncIO() = default;
AsyncIO(SystemStringView filename, bool truncate = false);
~AsyncIO();
AsyncIO(AsyncIO&& other);
AsyncIO& operator=(AsyncIO&& other);
AsyncIO(const AsyncIO* other) = delete;
AsyncIO& operator=(const AsyncIO& other) = delete;
void resizeQueue(size_t queueSz) { m_queue.resize(queueSz); }
bool asyncRead(size_t qIdx, void* buf, size_t length, off_t offset);
bool asyncWrite(size_t qIdx, const void* buf, size_t length, off_t offset);
ECardResult pollStatus(size_t qIdx, SizeReturn* szRet = nullptr) const;
ECardResult pollStatus() const;
void waitForCompletion() const;
#ifndef _WIN32
operator bool() const { return m_fd != -1; }
operator bool() const { return m_fd != -1; }
#else
operator bool() const { return m_fh != INVALID_HANDLE_VALUE; }
operator bool() const { return m_fh != INVALID_HANDLE_VALUE; }
#endif
};
}
} // namespace kabufuda

View File

@ -2,39 +2,36 @@
#include "Constants.hpp"
namespace kabufuda
{
class BlockAllocationTable
{
friend class Card;
namespace kabufuda {
class BlockAllocationTable {
friend class Card;
#pragma pack(push, 4)
union {
struct
{
uint16_t m_checksum;
uint16_t m_checksumInv;
uint16_t m_updateCounter;
uint16_t m_freeBlocks;
uint16_t m_lastAllocated;
uint16_t m_map[0xFFB];
};
uint8_t __raw[BlockSize];
union {
struct {
uint16_t m_checksum;
uint16_t m_checksumInv;
uint16_t m_updateCounter;
uint16_t m_freeBlocks;
uint16_t m_lastAllocated;
uint16_t m_map[0xFFB];
};
uint8_t __raw[BlockSize];
};
#pragma pack(pop)
void swapEndian();
void updateChecksum();
bool valid() const;
void swapEndian();
void updateChecksum();
bool valid() const;
public:
explicit BlockAllocationTable(uint32_t blockCount = (uint32_t(ECardSize::Card2043Mb) * MbitToBlocks));
BlockAllocationTable(uint8_t data[BlockSize]);
~BlockAllocationTable() = default;
explicit BlockAllocationTable(uint32_t blockCount = (uint32_t(ECardSize::Card2043Mb) * MbitToBlocks));
BlockAllocationTable(uint8_t data[BlockSize]);
~BlockAllocationTable() = default;
uint16_t getNextBlock(uint16_t block) const;
uint16_t nextFreeBlock(uint16_t maxBlock, uint16_t startingBlock) const;
bool clear(uint16_t first, uint16_t count);
uint16_t allocateBlocks(uint16_t count, uint16_t maxBlocks);
uint16_t numFreeBlocks() const { return m_freeBlocks; }
uint16_t getNextBlock(uint16_t block) const;
uint16_t nextFreeBlock(uint16_t maxBlock, uint16_t startingBlock) const;
bool clear(uint16_t first, uint16_t count);
uint16_t allocateBlocks(uint16_t count, uint16_t maxBlocks);
uint16_t numFreeBlocks() const { return m_freeBlocks; }
};
}
} // namespace kabufuda

View File

@ -13,316 +13,309 @@
#define CARD_FILENAME_MAX 32
#define CARD_ICON_MAX 8
namespace kabufuda
{
namespace kabufuda {
class FileHandle {
friend class Card;
uint32_t idx = -1;
int32_t offset = 0;
FileHandle(uint32_t idx) : idx(idx) {}
class FileHandle
{
friend class Card;
uint32_t idx = -1;
int32_t offset = 0;
FileHandle(uint32_t idx) : idx(idx) {}
public:
FileHandle() = default;
uint32_t getFileNo() const { return idx; }
operator bool() const { return getFileNo() != -1; }
FileHandle() = default;
uint32_t getFileNo() const { return idx; }
operator bool() const { return getFileNo() != -1; }
};
struct ProbeResults
{
ECardResult x0_error;
uint32_t x4_cardSize; /* in megabits */
uint32_t x8_sectorSize; /* in bytes */
struct ProbeResults {
ECardResult x0_error;
uint32_t x4_cardSize; /* in megabits */
uint32_t x8_sectorSize; /* in bytes */
};
struct CardStat
{
/* read-only (Set by Card::getStatus) */
char x0_fileName[CARD_FILENAME_MAX];
uint32_t x20_length;
uint32_t x24_time; /* seconds since 01/01/2000 midnight */
uint8_t x28_gameName[4];
uint8_t x2c_company[2];
struct CardStat {
/* read-only (Set by Card::getStatus) */
char x0_fileName[CARD_FILENAME_MAX];
uint32_t x20_length;
uint32_t x24_time; /* seconds since 01/01/2000 midnight */
uint8_t x28_gameName[4];
uint8_t x2c_company[2];
/* read/write (Set by Card::getStatus/Card::setStatus) */
uint8_t x2e_bannerFormat;
uint8_t x2f___padding;
uint32_t x30_iconAddr; /* offset to the banner, bannerTlut, icon, iconTlut data set. */
uint16_t x34_iconFormat;
uint16_t x36_iconSpeed;
uint32_t x38_commentAddr; /* offset to the pair of 32 byte character strings. */
/* read/write (Set by Card::getStatus/Card::setStatus) */
uint8_t x2e_bannerFormat;
uint8_t x2f___padding;
uint32_t x30_iconAddr; /* offset to the banner, bannerTlut, icon, iconTlut data set. */
uint16_t x34_iconFormat;
uint16_t x36_iconSpeed;
uint32_t x38_commentAddr; /* offset to the pair of 32 byte character strings. */
/* read-only (Set by Card::getStatus) */
uint32_t x3c_offsetBanner;
uint32_t x40_offsetBannerTlut;
uint32_t x44_offsetIcon[CARD_ICON_MAX];
uint32_t x64_offsetIconTlut;
uint32_t x68_offsetData;
/* read-only (Set by Card::getStatus) */
uint32_t x3c_offsetBanner;
uint32_t x40_offsetBannerTlut;
uint32_t x44_offsetIcon[CARD_ICON_MAX];
uint32_t x64_offsetIconTlut;
uint32_t x68_offsetData;
uint32_t GetFileLength() const { return x20_length; }
uint32_t GetTime() const { return x24_time; }
EImageFormat GetBannerFormat() const { return EImageFormat(x2e_bannerFormat & 0x3); }
void SetBannerFormat(EImageFormat fmt) { x2e_bannerFormat = (x2e_bannerFormat & ~0x3) | uint8_t(fmt); }
EImageFormat GetIconFormat(int idx) const { return EImageFormat((x34_iconFormat >> (idx * 2)) & 0x3); }
void SetIconFormat(EImageFormat fmt, int idx)
{
x34_iconFormat &= ~(0x3 << (idx * 2));
x34_iconFormat |= uint16_t(fmt) << (idx * 2);
}
void SetIconSpeed(EAnimationSpeed sp, int idx)
{
x36_iconSpeed &= ~(0x3 << (idx * 2));
x36_iconSpeed |= uint16_t(sp) << (idx * 2);
}
uint32_t GetIconAddr() const { return x30_iconAddr; }
void SetIconAddr(uint32_t addr) { x30_iconAddr = addr; }
uint32_t GetCommentAddr() const { return x38_commentAddr; }
void SetCommentAddr(uint32_t addr) { x38_commentAddr = addr; }
uint32_t GetFileLength() const { return x20_length; }
uint32_t GetTime() const { return x24_time; }
EImageFormat GetBannerFormat() const { return EImageFormat(x2e_bannerFormat & 0x3); }
void SetBannerFormat(EImageFormat fmt) { x2e_bannerFormat = (x2e_bannerFormat & ~0x3) | uint8_t(fmt); }
EImageFormat GetIconFormat(int idx) const { return EImageFormat((x34_iconFormat >> (idx * 2)) & 0x3); }
void SetIconFormat(EImageFormat fmt, int idx) {
x34_iconFormat &= ~(0x3 << (idx * 2));
x34_iconFormat |= uint16_t(fmt) << (idx * 2);
}
void SetIconSpeed(EAnimationSpeed sp, int idx) {
x36_iconSpeed &= ~(0x3 << (idx * 2));
x36_iconSpeed |= uint16_t(sp) << (idx * 2);
}
uint32_t GetIconAddr() const { return x30_iconAddr; }
void SetIconAddr(uint32_t addr) { x30_iconAddr = addr; }
uint32_t GetCommentAddr() const { return x38_commentAddr; }
void SetCommentAddr(uint32_t addr) { x38_commentAddr = addr; }
};
class Card
{
class Card {
#pragma pack(push, 4)
struct CardHeader
{
uint8_t m_serial[12];
uint64_t m_formatTime;
int32_t m_sramBias;
uint32_t m_sramLanguage;
uint32_t m_unknown;
uint16_t m_deviceId; /* 0 for Slot A, 1 for Slot B */
uint16_t m_sizeMb;
uint16_t m_encoding;
uint8_t __padding[468];
uint16_t m_updateCounter;
uint16_t m_checksum;
uint16_t m_checksumInv;
void _swapEndian();
};
union {
CardHeader m_ch;
uint8_t __raw[BlockSize];
};
CardHeader m_tmpCh;
struct CardHeader {
uint8_t m_serial[12];
uint64_t m_formatTime;
int32_t m_sramBias;
uint32_t m_sramLanguage;
uint32_t m_unknown;
uint16_t m_deviceId; /* 0 for Slot A, 1 for Slot B */
uint16_t m_sizeMb;
uint16_t m_encoding;
uint8_t __padding[468];
uint16_t m_updateCounter;
uint16_t m_checksum;
uint16_t m_checksumInv;
void _swapEndian();
};
union {
CardHeader m_ch;
uint8_t __raw[BlockSize];
};
CardHeader m_tmpCh;
#pragma pack(pop)
SystemString m_filename;
AsyncIO m_fileHandle;
Directory m_dirs[2];
BlockAllocationTable m_bats[2];
Directory m_tmpDirs[2];
BlockAllocationTable m_tmpBats[2];
uint8_t m_currentDir;
uint8_t m_currentBat;
SystemString m_filename;
AsyncIO m_fileHandle;
Directory m_dirs[2];
BlockAllocationTable m_bats[2];
Directory m_tmpDirs[2];
BlockAllocationTable m_tmpBats[2];
uint8_t m_currentDir;
uint8_t m_currentBat;
uint16_t m_maxBlock;
char m_game[5] = {'\0'};
char m_maker[3] = {'\0'};
uint16_t m_maxBlock;
char m_game[5] = {'\0'};
char m_maker[3] = {'\0'};
void _updateDirAndBat(const Directory& dir, const BlockAllocationTable& bat);
void _updateChecksum();
File* _fileFromHandle(const FileHandle& fh) const;
void _deleteFile(File& f, BlockAllocationTable& bat);
void _updateDirAndBat(const Directory& dir, const BlockAllocationTable& bat);
void _updateChecksum();
File* _fileFromHandle(const FileHandle& fh) const;
void _deleteFile(File& f, BlockAllocationTable& bat);
bool m_dirty = false;
bool m_opened = false;
ECardResult _pumpOpen();
bool m_dirty = false;
bool m_opened = false;
ECardResult _pumpOpen();
public:
Card();
/**
* @brief Card
* @param other
*/
Card(const Card& other) = delete;
Card& operator=(const Card& other) = delete;
Card(Card&& other);
Card& operator=(Card&& other);
Card();
/**
* @brief Card
* @param other
*/
Card(const Card& other) = delete;
Card& operator=(const Card& other) = delete;
Card(Card&& other);
Card& operator=(Card&& other);
/**
* @brief Card
* @param filepath
* @param game
* @param maker
*/
Card(const char* game = nullptr, const char* maker = nullptr);
~Card();
/**
* @brief Card
* @param filepath
* @param game
* @param maker
*/
Card(const char* game = nullptr, const char* maker = nullptr);
~Card();
/**
* @brief openFile
* @param filename
*/
ECardResult openFile(const char* filename, FileHandle& handleOut);
/**
* @brief openFile
* @param filename
*/
ECardResult openFile(const char* filename, FileHandle& handleOut);
/**
* @brief openFile
* @param fileno
*/
ECardResult openFile(uint32_t fileno, FileHandle& handleOut);
/**
* @brief openFile
* @param fileno
*/
ECardResult openFile(uint32_t fileno, FileHandle& handleOut);
/**
* @brief createFile
* @param filename
* @return
*/
ECardResult createFile(const char* filename, size_t size, FileHandle& handleOut);
/**
* @brief createFile
* @param filename
* @return
*/
ECardResult createFile(const char* filename, size_t size, FileHandle& handleOut);
/**
* @brief closeFile
* @param fh FileHandle to close
* @return
*/
ECardResult closeFile(FileHandle& fh);
/**
* @brief closeFile
* @param fh FileHandle to close
* @return
*/
ECardResult closeFile(FileHandle& fh);
/**
* @brief firstFile
* @return
*/
FileHandle firstFile();
/**
* @brief firstFile
* @return
*/
FileHandle firstFile();
/**
* @brief nextFile
* @param cur
* @return
*/
FileHandle nextFile(const FileHandle& cur);
/**
* @brief nextFile
* @param cur
* @return
*/
FileHandle nextFile(const FileHandle& cur);
/**
* @brief getFilename
* @param fh
* @return
*/
const char* getFilename(const FileHandle& fh);
/**
* @brief getFilename
* @param fh
* @return
*/
const char* getFilename(const FileHandle& fh);
/**
* @brief deleteFile
* @param fh
*/
void deleteFile(const FileHandle& fh);
/**
* @brief deleteFile
* @param fh
*/
void deleteFile(const FileHandle& fh);
/**
* @brief deleteFile
* @param filename
*/
ECardResult deleteFile(const char* filename);
/**
* @brief deleteFile
* @param filename
*/
ECardResult deleteFile(const char* filename);
/**
* @brief deleteFile
* @param fileno
*/
ECardResult deleteFile(uint32_t fileno);
/**
* @brief deleteFile
* @param fileno
*/
ECardResult deleteFile(uint32_t fileno);
/**
* @brief renameFile
* @param oldName
* @param newName
*/
ECardResult renameFile(const char* oldName, const char* newName);
/**
* @brief renameFile
* @param oldName
* @param newName
*/
ECardResult renameFile(const char* oldName, const char* newName);
/**
* @brief write
* @param fh
* @param buf
* @param size
*/
ECardResult asyncWrite(FileHandle& fh, const void* buf, size_t size);
/**
* @brief write
* @param fh
* @param buf
* @param size
*/
ECardResult asyncWrite(FileHandle& fh, const void* buf, size_t size);
/**
* @brief read
* @param fh
* @param dst
* @param size
*/
ECardResult asyncRead(FileHandle& fh, void* dst, size_t size);
/**
* @brief read
* @param fh
* @param dst
* @param size
*/
ECardResult asyncRead(FileHandle& fh, void* dst, size_t size);
/**
* @brief seek
* @param fh
* @param pos
* @param whence
*/
void seek(FileHandle& fh, int32_t pos, SeekOrigin whence);
/**
* @brief seek
* @param fh
* @param pos
* @param whence
*/
void seek(FileHandle& 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 FileHandle& fh);
/**
* @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 FileHandle& fh);
/**
* @brief setPublic
* @param fh
* @param pub
*/
void setPublic(const FileHandle& fh, bool pub);
/**
* @brief setPublic
* @param fh
* @param pub
*/
void setPublic(const FileHandle& fh, bool pub);
/**
* @brief isPublic
* @param fh
* @return
*/
bool isPublic(const FileHandle& fh) const;
/**
* @brief isPublic
* @param fh
* @return
*/
bool isPublic(const FileHandle& fh) const;
/**
* @brief setCanCopy
* @param fh
* @param copy
*/
void setCanCopy(const FileHandle& fh, bool copy) const;
/**
* @brief setCanCopy
* @param fh
* @param copy
*/
void setCanCopy(const FileHandle& fh, bool copy) const;
/**
* @brief canCopy
* @param fh
* @return
*/
bool canCopy(const FileHandle& fh) const;
/**
* @brief canCopy
* @param fh
* @return
*/
bool canCopy(const FileHandle& fh) const;
/**
* @brief setCanMove
* @param fh
* @param move
*/
void setCanMove(const FileHandle& fh, bool move);
/**
* @brief setCanMove
* @param fh
* @param move
*/
void setCanMove(const FileHandle& fh, bool move);
/**
* @brief canMove
* @param fh
* @return
*/
bool canMove(const FileHandle& fh) const;
/**
* @brief canMove
* @param fh
* @return
*/
bool canMove(const FileHandle& fh) const;
/**
* @brief getStatus
* @param fh Handle of requested file
* @param statOut Structure to fill with file stat
* @return NOFILE or READY
*/
ECardResult getStatus(const FileHandle& fh, CardStat& statOut) const;
/**
* @brief getStatus
* @param fh Handle of requested file
* @param statOut Structure to fill with file stat
* @return NOFILE or READY
*/
ECardResult getStatus(const FileHandle& fh, CardStat& statOut) const;
/**
* @brief getStatus
* @param fileNo Number of requested file
* @param statOut Structure to fill with file stat
* @return NOFILE or READY
*/
ECardResult getStatus(uint32_t fileNo, CardStat& statOut) const;
/**
* @brief getStatus
* @param fileNo Number of requested file
* @param statOut Structure to fill with file stat
* @return NOFILE or READY
*/
ECardResult getStatus(uint32_t fileNo, CardStat& statOut) const;
/**
* @brief setStatus
* @param fh Handle of requested file
* @param statOut Structure to access for file stat
* @return NOFILE or READY
*/
ECardResult setStatus(const FileHandle& fh, const CardStat& stat);
/**
* @brief setStatus
* @param fh Handle of requested file
* @param statOut Structure to access for file stat
* @return NOFILE or READY
*/
ECardResult setStatus(const FileHandle& fh, const CardStat& stat);
/**
* @brief setStatus
* @param fileNo Number of requested file
* @param statOut Structure to access for file stat
* @return NOFILE or READY
*/
ECardResult setStatus(uint32_t fileNo, const CardStat& stat);
/**
* @brief setStatus
* @param fileNo Number of requested file
* @param statOut Structure to access for file stat
* @return NOFILE or READY
*/
ECardResult setStatus(uint32_t fileNo, const CardStat& stat);
#if 0 // TODO: Async-friendly implementations
/**
@ -342,98 +335,97 @@ public:
bool moveFileTo(FileHandle& fh, Card& dest);
#endif
/**
* @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"
* @sa openFile
*/
void setCurrentGame(const char* 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"
* @sa openFile
*/
void setCurrentGame(const char* game);
/**
* @brief Returns the currently selected game
* @return The selected game, or nullptr
*/
const uint8_t* getCurrentGame() const;
/**
* @brief Returns the currently selected game
* @return The selected game, or nullptr
*/
const uint8_t* getCurrentGame() const;
/**
* @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"
* @sa openFile
*/
void setCurrentMaker(const char* 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"
* @sa openFile
*/
void setCurrentMaker(const char* maker);
/**
* @brief Returns the currently selected maker
* @return The selected maker, or nullptr
*/
const uint8_t* getCurrentMaker() const;
/**
* @brief Returns the currently selected maker
* @return The selected maker, or nullptr
*/
const uint8_t* getCurrentMaker() const;
/**
* @brief Retrieves the format assigned serial
* @param serial
*/
void getSerial(uint64_t& serial);
/**
* @brief Retrieves the format assigned serial
* @param serial
*/
void getSerial(uint64_t& serial);
/**
* @brief Retrieves the checksum values of the Card system header
* @param checksum The checksum of the system header
* @param inverse The inverser checksum of the system header
*/
void getChecksum(uint16_t& checksum, uint16_t& inverse);
/**
* @brief Retrieves the checksum values of the Card system header
* @param checksum The checksum of the system header
* @param inverse The inverser checksum of the system header
*/
void getChecksum(uint16_t& checksum, uint16_t& inverse);
/**
* @brief Retrieves the available storage and directory space
* @param bytesNotUsed Number of free bytes out
* @param filesNotUsed Number of free files out
*/
void getFreeBlocks(int32_t& bytesNotUsed, int32_t& filesNotUsed);
/**
* @brief Retrieves the available storage and directory space
* @param bytesNotUsed Number of free bytes out
* @param filesNotUsed Number of free files out
*/
void getFreeBlocks(int32_t& bytesNotUsed, int32_t& filesNotUsed);
/**
* @brief Formats the memory card and assigns a new serial
* @param size The desired size of the file @sa ECardSize
* @param encoding The desired encoding @sa EEncoding
*/
void format(ECardSlot deviceId, ECardSize size = ECardSize::Card2043Mb, EEncoding encoding = EEncoding::ASCII);
/**
* @brief Formats the memory card and assigns a new serial
* @param size The desired size of the file @sa ECardSize
* @param encoding The desired encoding @sa EEncoding
*/
void format(ECardSlot deviceId, ECardSize size = ECardSize::Card2043Mb, EEncoding encoding = EEncoding::ASCII);
/**
* @brief Returns basic stats about a card image without opening a handle
* @return ProbeResults structure
*/
static ProbeResults probeCardFile(SystemStringView filename);
/**
* @brief Returns basic stats about a card image without opening a handle
* @return ProbeResults structure
*/
static ProbeResults probeCardFile(SystemStringView 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();
/**
* @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();
/**
* @brief Opens card image (does nothing if currently open path matches)
*/
bool open(SystemStringView filepath);
/**
* @brief Opens card image (does nothing if currently open path matches)
*/
bool open(SystemStringView filepath);
/**
* @brief Commits changes to disk and closes host file
*/
void close();
/**
* @brief Commits changes to disk and closes host file
*/
void close();
/**
* @brief Access host filename of card
*/
SystemStringView cardFilename() const { return m_filename; }
/**
* @brief Access host filename of card
*/
SystemStringView cardFilename() const { return m_filename; }
/**
* @brief Gets card-scope error state
* @return READY, BROKEN, or NOCARD
*/
ECardResult getError() const;
/**
* @brief Gets card-scope error state
* @return READY, BROKEN, or NOCARD
*/
ECardResult getError() const;
/**
* @brief Block caller until any asynchronous I/O operations have completed
*/
void waitForCompletion() const;
/**
* @brief Block caller until any asynchronous I/O operations have completed
*/
void waitForCompletion() const;
operator bool() const { return getError() == ECardResult::READY; }
operator bool() const { return getError() == ECardResult::READY; }
};
}
} // namespace kabufuda

View File

@ -3,8 +3,7 @@
#include <stdint.h>
#include "Util.hpp"
namespace kabufuda
{
namespace kabufuda {
uint32_t constexpr BlockSize = 0x2000;
uint32_t constexpr MaxFiles = 127;
uint32_t constexpr FSTBlocks = 5;
@ -14,64 +13,50 @@ uint32_t constexpr BATSize = 0xFFB;
/**
* @brief The EPermissions enum
*/
enum class EPermissions : uint8_t
{
Public = (1 << 2),
NoCopy = (1 << 3),
NoMove = (1 << 4),
Global = (1 << 5),
Company = (1 << 6)
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 EImageFormat : uint8_t {
None,
C8,
RGB5A3,
};
enum class EAnimationType
{
Loop = 0,
Bounce = 2,
enum class EAnimationType {
Loop = 0,
Bounce = 2,
};
enum class EAnimationSpeed
{
End,
Fast,
Middle,
Slow,
enum class EAnimationSpeed {
End,
Fast,
Middle,
Slow,
};
enum class SeekOrigin
{
Begin,
Current,
End
};
enum class SeekOrigin { Begin, Current, End };
/**
* @brief The ECardSlot enum
*/
enum class ECardSlot : uint16_t
{
SlotA,
SlotB
};
enum class ECardSlot : uint16_t { SlotA, SlotB };
/**
* @brief The ECardSize enum
*/
enum class ECardSize : uint16_t
{
Card59Mb = 0x04,
Card123Mb = 0x08,
Card251Mb = 0x10,
Card507Mb = 0x20,
Card1019Mb = 0x40,
Card2043Mb = 0x80
enum class ECardSize : uint16_t {
Card59Mb = 0x04,
Card123Mb = 0x08,
Card251Mb = 0x10,
Card507Mb = 0x20,
Card1019Mb = 0x40,
Card2043Mb = 0x80
};
static constexpr uint32_t BannerWidth = 96;
@ -82,10 +67,8 @@ static constexpr uint32_t IconHeight = 32;
/**
* @brief The EEncoding enum
*/
enum class EEncoding : uint16_t
{
ASCII, /**< Standard ASCII Encoding */
SJIS /**< SJIS Encoding for japanese */
enum class EEncoding : uint16_t {
ASCII, /**< Standard ASCII Encoding */
SJIS /**< SJIS Encoding for japanese */
};
}
} // namespace kabufuda

View File

@ -2,41 +2,37 @@
#include "File.hpp"
namespace kabufuda
{
class Directory
{
friend class Card;
namespace kabufuda {
class Directory {
friend class Card;
#pragma pack(push, 4)
union {
struct
{
File m_files[MaxFiles];
uint8_t __padding[0x3a];
uint16_t m_updateCounter;
uint16_t m_checksum;
uint16_t m_checksumInv;
};
uint8_t __raw[BlockSize];
union {
struct {
File m_files[MaxFiles];
uint8_t __padding[0x3a];
uint16_t m_updateCounter;
uint16_t m_checksum;
uint16_t m_checksumInv;
};
uint8_t __raw[BlockSize];
};
#pragma pack(pop)
void swapEndian();
void updateChecksum();
bool valid() const;
void swapEndian();
void updateChecksum();
bool valid() const;
public:
Directory();
Directory(uint8_t data[BlockSize]);
~Directory() = default;
Directory();
Directory(uint8_t data[BlockSize]);
~Directory() = default;
bool hasFreeFile() const;
int32_t numFreeFiles() const;
File* getFirstFreeFile(const char* game, const char* maker, const char* filename);
File* getFirstNonFreeFile(uint32_t start, const char* game, const char* maker);
File* getFile(const char* game, const char* maker, const char* filename);
File* getFile(uint32_t idx);
int32_t indexForFile(File* f);
bool hasFreeFile() const;
int32_t numFreeFiles() const;
File* getFirstFreeFile(const char* game, const char* maker, const char* filename);
File* getFirstNonFreeFile(uint32_t start, const char* game, const char* maker);
File* getFile(const char* game, const char* maker, const char* filename);
File* getFile(uint32_t idx);
int32_t indexForFile(File* f);
};
}
} // namespace kabufuda

View File

@ -2,44 +2,40 @@
#include "Constants.hpp"
namespace kabufuda
{
class File
{
friend class IFileHandle;
friend class Directory;
friend class Card;
namespace kabufuda {
class File {
friend class IFileHandle;
friend class Directory;
friend class Card;
#pragma pack(push, 4)
union {
struct
{
uint8_t m_game[4];
uint8_t m_maker[2];
uint8_t m_reserved;
uint8_t m_bannerFlags;
char m_filename[0x20];
uint32_t m_modifiedTime;
uint32_t m_iconAddress;
uint16_t m_iconFmt;
uint16_t m_animSpeed;
EPermissions m_permissions;
int8_t m_copyCounter;
uint16_t m_firstBlock;
uint16_t m_blockCount;
uint16_t m_reserved2;
uint32_t m_commentAddr;
};
uint8_t __raw[0x40];
union {
struct {
uint8_t m_game[4];
uint8_t m_maker[2];
uint8_t m_reserved;
uint8_t m_bannerFlags;
char m_filename[0x20];
uint32_t m_modifiedTime;
uint32_t m_iconAddress;
uint16_t m_iconFmt;
uint16_t m_animSpeed;
EPermissions m_permissions;
int8_t m_copyCounter;
uint16_t m_firstBlock;
uint16_t m_blockCount;
uint16_t m_reserved2;
uint32_t m_commentAddr;
};
uint8_t __raw[0x40];
};
#pragma pack(pop)
void swapEndian();
void swapEndian();
public:
File();
File(char data[0x40]);
File(const char* filename);
~File() = default;
File();
File(char data[0x40]);
File(const char* filename);
~File() = default;
};
}
} // namespace kabufuda

View File

@ -25,50 +25,46 @@ must not be misrepresented as being the original software.
distribution.
-------------------------------------------------------------*/
namespace kabufuda
{
#pragma pack(push,1)
union SRAMFlags
{
uint8_t Hex;
struct
{
uint8_t : 2;
uint8_t sound : 1; // Audio settings; 0 = Mono, 1 = Stereo
uint8_t initialized : 1; // if 0, displays prompt to set language on boot and asks user to set options and time/date
uint8_t : 2;
uint8_t boot_menu : 1; // if 1, skips logo animation and boots into the system menu regardless of if there is a disc inserted
uint8_t progressive : 1; // if 1, automatically displays Progressive Scan prompt in games that support it
};
namespace kabufuda {
#pragma pack(push, 1)
union SRAMFlags {
uint8_t Hex;
struct {
uint8_t : 2;
uint8_t sound : 1; // Audio settings; 0 = Mono, 1 = Stereo
uint8_t initialized : 1; // if 0, displays prompt to set language on boot and asks user to set options and time/date
uint8_t : 2;
uint8_t boot_menu : 1; // if 1, skips logo animation and boots into the system menu regardless of if there is a
// disc inserted
uint8_t progressive : 1; // if 1, automatically displays Progressive Scan prompt in games that support it
};
};
union SRAM
{
uint8_t p_SRAM[64];
struct // Stored configuration value from the system SRAM area
{
uint16_t checksum; // Holds the block checksum.
uint16_t checksum_inv; // Holds the inverse block checksum
uint32_t ead0; // Unknown attribute
uint32_t ead1; // Unknown attribute
uint32_t counter_bias; // Bias value for the realtime clock
int8_t display_offsetH; // Pixel offset for the VI
uint8_t ntd; // Unknown attribute
uint8_t lang; // Language of system
SRAMFlags flags; // Device and operations flag
union SRAM {
uint8_t p_SRAM[64];
struct // Stored configuration value from the system SRAM area
{
uint16_t checksum; // Holds the block checksum.
uint16_t checksum_inv; // Holds the inverse block checksum
uint32_t ead0; // Unknown attribute
uint32_t ead1; // Unknown attribute
uint32_t counter_bias; // Bias value for the realtime clock
int8_t display_offsetH; // Pixel offset for the VI
uint8_t ntd; // Unknown attribute
uint8_t lang; // Language of system
SRAMFlags flags; // Device and operations flag
// Stored configuration value from the extended SRAM area
uint8_t flash_id[2][12]; // flash_id[2][12] 96bit memorycard unlock flash ID
uint32_t wirelessKbd_id; // Device ID of last connected wireless keyboard
uint16_t wirelessPad_id[4]; // 16-bit device ID of last connected pad.
uint8_t dvderr_code; // last non-recoverable error from DVD interface
uint8_t __padding0; // reserved
uint8_t flashID_chksum[2]; // 8-bit checksum of unlock flash ID
uint32_t __padding1; // padding
};
// Stored configuration value from the extended SRAM area
uint8_t flash_id[2][12]; // flash_id[2][12] 96bit memorycard unlock flash ID
uint32_t wirelessKbd_id; // Device ID of last connected wireless keyboard
uint16_t wirelessPad_id[4]; // 16-bit device ID of last connected pad.
uint8_t dvderr_code; // last non-recoverable error from DVD interface
uint8_t __padding0; // reserved
uint8_t flashID_chksum[2]; // 8-bit checksum of unlock flash ID
uint32_t __padding1; // padding
};
};
#pragma pack(pop)
extern const SRAM g_SRAM;
}
} // namespace kabufuda

View File

@ -39,77 +39,68 @@
#ifndef ENABLE_BITWISE_ENUM
#define ENABLE_BITWISE_ENUM(type) \
constexpr type operator|(type a, type b) \
{ \
using T = std::underlying_type_t<type>; \
return type(static_cast<T>(a) | static_cast<T>(b)); \
} \
constexpr 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)); \
}
constexpr type operator|(type a, type b) { \
using T = std::underlying_type_t<type>; \
return type(static_cast<T>(a) | static_cast<T>(b)); \
} \
constexpr 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 {
/* Type-sensitive byte swappers */
template <typename T>
static inline T bswap16(T val)
{
static inline T bswap16(T val) {
#if __GNUC__
return __builtin_bswap16(val);
return __builtin_bswap16(val);
#elif _WIN32
return _byteswap_ushort(val);
return _byteswap_ushort(val);
#else
return (val = (val << 8) | ((val >> 8) & 0xFF));
return (val = (val << 8) | ((val >> 8) & 0xFF));
#endif
}
template <typename T>
static inline T bswap32(T val)
{
static inline T bswap32(T val) {
#if __GNUC__
return __builtin_bswap32(val);
return __builtin_bswap32(val);
#elif _WIN32
return _byteswap_ulong(val);
return _byteswap_ulong(val);
#else
val = (val & 0x0000FFFF) << 16 | (val & 0xFFFF0000) >> 16;
val = (val & 0x00FF00FF) << 8 | (val & 0xFF00FF00) >> 8;
return val;
val = (val & 0x0000FFFF) << 16 | (val & 0xFFFF0000) >> 16;
val = (val & 0x00FF00FF) << 8 | (val & 0xFF00FF00) >> 8;
return val;
#endif
}
template <typename T>
static inline T bswap64(T val)
{
static inline T bswap64(T val) {
#if __GNUC__
return __builtin_bswap64(val);
return __builtin_bswap64(val);
#elif _WIN32
return _byteswap_uint64(val);
return _byteswap_uint64(val);
#else
return ((val & 0xFF00000000000000ULL) >> 56) | ((val & 0x00FF000000000000ULL) >> 40) |
((val & 0x0000FF0000000000ULL) >> 24) | ((val & 0x000000FF00000000ULL) >> 8) |
((val & 0x00000000FF000000ULL) << 8) | ((val & 0x0000000000FF0000ULL) << 24) |
((val & 0x000000000000FF00ULL) << 40) | ((val & 0x00000000000000FFULL) << 56);
return ((val & 0xFF00000000000000ULL) >> 56) | ((val & 0x00FF000000000000ULL) >> 40) |
((val & 0x0000FF0000000000ULL) >> 24) | ((val & 0x000000FF00000000ULL) >> 8) |
((val & 0x00000000FF000000ULL) << 8) | ((val & 0x0000000000FF0000ULL) << 24) |
((val & 0x000000000000FF00ULL) << 40) | ((val & 0x00000000000000FFULL) << 56);
#endif
}
@ -120,15 +111,13 @@ static inline int32_t SBig(int32_t val) { return bswap32(val); }
static inline uint32_t SBig(uint32_t val) { return bswap32(val); }
static inline int64_t SBig(int64_t val) { return bswap64(val); }
static inline uint64_t SBig(uint64_t val) { return bswap64(val); }
static inline float SBig(float val)
{
int32_t ival = bswap32(*((int32_t*)(&val)));
return *((float*)(&ival));
static inline float SBig(float val) {
int32_t ival = bswap32(*((int32_t*)(&val)));
return *((float*)(&ival));
}
static inline double SBig(double val)
{
int64_t ival = bswap64(*((int64_t*)(&val)));
return *((double*)(&ival));
static inline double SBig(double val) {
int64_t ival = bswap64(*((int64_t*)(&val)));
return *((double*)(&ival));
}
#ifndef SBIG
#define SBIG(q) (((q)&0x000000FF) << 24 | ((q)&0x0000FF00) << 8 | ((q)&0x00FF0000) >> 8 | ((q)&0xFF000000) >> 24)
@ -152,15 +141,13 @@ static inline int32_t SLittle(int32_t val) { return bswap32(val); }
static inline uint32_t SLittle(uint32_t val) { return bswap32(val); }
static inline int64_t SLittle(int64_t val) { return bswap64(val); }
static inline uint64_t SLittle(uint64_t val) { return bswap64(val); }
static inline float SLittle(float val)
{
int32_t ival = bswap32(*((int32_t*)(&val)));
return *((float*)(&ival));
static inline float SLittle(float val) {
int32_t ival = bswap32(*((int32_t*)(&val)));
return *((float*)(&ival));
}
static inline double SLittle(double val)
{
int64_t ival = bswap64(*((int64_t*)(&val)));
return *((double*)(&ival));
static inline double SLittle(double val) {
int64_t ival = bswap64(*((int64_t*)(&val)));
return *((double*)(&ival));
}
#ifndef SLITTLE
#define SLITTLE(q) (((q)&0x000000FF) << 24 | ((q)&0x0000FF00) << 8 | ((q)&0x00FF0000) >> 8 | ((q)&0xFF000000) >> 24)
@ -186,28 +173,28 @@ typedef std::wstring SystemString;
typedef std::wstring_view SystemStringView;
static inline void ToLower(SystemString& str) { std::transform(str.begin(), str.end(), str.begin(), towlower); }
static inline void ToUpper(SystemString& str) { std::transform(str.begin(), str.end(), str.begin(), towupper); }
class SystemUTF8Conv
{
std::string m_utf8;
class SystemUTF8Conv {
std::string m_utf8;
public:
explicit SystemUTF8Conv(SystemStringView str) : m_utf8(WideToUTF8(str)) {}
std::string_view str() const { return m_utf8; }
const char* c_str() const { return m_utf8.c_str(); }
std::string operator+(std::string_view other) const { return m_utf8 + other.data(); }
explicit SystemUTF8Conv(SystemStringView str) : m_utf8(WideToUTF8(str)) {}
std::string_view str() const { return m_utf8; }
const char* c_str() const { return m_utf8.c_str(); }
std::string operator+(std::string_view other) const { return m_utf8 + other.data(); }