Commit f126245e authored by Jack Andersen's avatar Jack Andersen

New code style refactor

parent a1e22426
---
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
......@@ -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
......@@ -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
This diff is collapsed.
......@@ -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
......@@ -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
......@@ -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
......@@ -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
This diff is collapsed.
......@@ -2,9 +2,7 @@
#include <string>
namespace kabufuda
{
namespace kabufuda {
std::string WideToUTF8(std::wstring_view src);
std::wstring UTF8ToWide(std::string_view src);
}
} // namespace kabufuda
......@@ -8,5 +8,4 @@
#endif
#include "windows.h"
void* memmem(const void *haystack, size_t hlen, const void *needle, size_t nlen);
void* memmem(const void* haystack, size_t hlen, const void* needle, size_t nlen);
#include "kabufuda/AsyncIO.hpp"
namespace kabufuda
{
namespace kabufuda {
AsyncIO::AsyncIO(SystemStringView filename, bool truncate)
{
m_fd = open(filename.data(), O_RDWR | O_CREAT | (truncate ? O_TRUNC : 0));
AsyncIO::AsyncIO(SystemStringView filename, bool truncate) {
m_fd = open(filename.data(), O_RDWR | O_CREAT | (truncate ? O_TRUNC : 0));
}
AsyncIO::~AsyncIO()
{
if (*this)
{
aio_cancel(m_fd, nullptr);
close(m_fd);
}
AsyncIO::~AsyncIO() {
if (*this) {
aio_cancel(m_fd, nullptr);
close(m_fd);
}
}
AsyncIO::AsyncIO(AsyncIO&& other)
{
m_fd = other.m_fd;
other.m_fd = -1;
m_queue = std::move(other.m_queue);
m_maxBlock = other.m_maxBlock;
AsyncIO::AsyncIO(AsyncIO&& other) {
m_fd = other.m_fd;
other.m_fd = -1;
m_queue = std::move(other.m_queue);
m_maxBlock = other.m_maxBlock;
}
AsyncIO& AsyncIO::operator=(AsyncIO&& other)
{
if (*this)
{
aio_cancel(m_fd, nullptr);
close(m_fd);
}
m_fd = other.m_fd;
other.m_fd = -1;
m_queue = std::move(other.m_queue);
m_maxBlock = other.m_maxBlock;
return *this;
AsyncIO& AsyncIO::operator=(AsyncIO&& other) {
if (*this) {
aio_cancel(m_fd, nullptr);
close(m_fd);
}
m_fd = other.m_fd;
other.m_fd = -1;
m_queue = std::move(other.m_queue);
m_maxBlock = other.m_maxBlock;
return *this;
}
void AsyncIO::_waitForOperation(size_t qIdx) const
{
auto& aio = const_cast<AsyncIO*>(this)->m_queue[qIdx];
if (aio.first.aio_fildes == 0)
return;
const struct aiocb* aiop = &aio.first;
struct timespec ts = {2, 0};
while (aio_suspend(&aiop, 1, &ts) && errno == EINTR) {}
if (aio_error(&aio.first) != EINPROGRESS)
aio.second = aio_return(&aio.first);
aio.first.aio_fildes = 0;
void AsyncIO::_waitForOperation(size_t qIdx) const {
auto& aio = const_cast<AsyncIO*>(this)->m_queue[qIdx];
if (aio.first.aio_fildes == 0)
return;
const struct aiocb* aiop = &aio.first;
struct timespec ts = {2, 0};
while (aio_suspend(&aiop, 1, &ts) && errno == EINTR) {}
if (aio_error(&aio.first) != EINPROGRESS)
aio.second = aio_return(&aio.first);
aio.first.aio_fildes = 0;
}
bool AsyncIO::asyncRead(size_t qIdx, void* buf, size_t length, off_t offset)
{
struct aiocb& aio = m_queue[qIdx].first;
if (aio.aio_fildes)
{
bool AsyncIO::asyncRead(size_t qIdx, void* buf, size_t length, off_t offset) {
struct aiocb& aio = m_queue[qIdx].first;
if (aio.aio_fildes) {
#ifndef NDEBUG
fprintf(stderr, "WARNING: synchronous kabufuda fallback, check access polling\n");
fprintf(stderr, "WARNING: synchronous kabufuda fallback, check access polling\n");
#endif
_waitForOperation(qIdx);
}
memset(&aio, 0, sizeof(struct aiocb));
aio.aio_fildes = m_fd;
aio.aio_offset = offset;
aio.aio_buf = buf;
aio.aio_nbytes = length;
m_maxBlock = std::max(m_maxBlock, qIdx + 1);
return aio_read(&aio) == 0;
_waitForOperation(qIdx);
}
memset(&aio, 0, sizeof(struct aiocb));
aio.aio_fildes = m_fd;
aio.aio_offset = offset;
aio.aio_buf = buf;
aio.aio_nbytes = length;
m_maxBlock = std::max(m_maxBlock, qIdx + 1);
return aio_read(&aio) == 0;
}
bool AsyncIO::asyncWrite(size_t qIdx, const void* buf, size_t length, off_t offset)
{
struct aiocb& aio = m_queue[qIdx].first;
if (aio.aio_fildes)
{
bool AsyncIO::asyncWrite(size_t qIdx, const void* buf, size_t length, off_t offset) {
struct aiocb& aio = m_queue[qIdx].first;
if (aio.aio_fildes) {
#ifndef NDEBUG
fprintf(stderr, "WARNING: synchronous kabufuda fallback, check access polling\n");
fprintf(stderr, "WARNING: synchronous kabufuda fallback, check access polling\n");
#endif
_waitForOperation(qIdx);
}
memset(&aio, 0, sizeof(struct aiocb));
aio.aio_fildes = m_fd;
aio.aio_offset = offset;
aio.aio_buf = const_cast<void*>(buf);
aio.aio_nbytes = length;
m_maxBlock = std::max(m_maxBlock, qIdx + 1);
return aio_write(&aio) == 0;
_waitForOperation(qIdx);
}
memset(&aio, 0, sizeof(struct aiocb));
aio.aio_fildes = m_fd;
aio.aio_offset = offset;
aio.aio_buf = const_cast<void*>(buf);
aio.aio_nbytes = length;
m_maxBlock = std::max(m_maxBlock, qIdx + 1);
return aio_write(&aio) == 0;
}
ECardResult AsyncIO::pollStatus(size_t qIdx, SizeReturn* szRet) const
{
auto& aio = const_cast<AsyncIO*>(this)->m_queue[qIdx];
ECardResult AsyncIO::pollStatus(size_t qIdx, SizeReturn* szRet) const {
auto& aio = const_cast<AsyncIO*>(this)->m_queue[qIdx];
if (aio.first.aio_fildes == 0) {
if (szRet)
*szRet = aio.second;
return ECardResult::READY;
}
switch (aio_error(&aio.first)) {
case 0:
aio.second = aio_return(&aio.first);
aio.first.aio_fildes = 0;
if (szRet)
*szRet = aio.second;
return ECardResult::READY;
case EINPROGRESS:
return ECardResult::BUSY;
default:
aio.second = aio_return(&aio.first);
aio.first.aio_fildes = 0;
if (szRet)
*szRet = aio.second;
return ECardResult::IOERROR;
}
}
ECardResult AsyncIO::pollStatus() const {
ECardResult result = ECardResult::READY;
for (auto it = const_cast<AsyncIO*>(this)->m_queue.begin();
it