From ac36bb4f4a656cd844ddf38a9ce62b70acc51371 Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Wed, 22 Jul 2015 08:59:40 -1000 Subject: [PATCH] added support for wchar_t file paths and more stable windows builds --- CMakeLists.txt | 4 ++++ include/Athena/DNA.hpp | 8 ++++---- include/Athena/FileReader.hpp | 6 ++++++ include/Athena/FileWriter.hpp | 6 ++++++ src/Athena/Compression.cpp | 2 +- src/Athena/FileReader.cpp | 25 ++++++++++++++++++++++--- src/Athena/FileWriter.cpp | 31 ++++++++++++++++++++++++++++++- 7 files changed, 73 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1fbb827..9f6e84b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,7 +18,9 @@ set(ATHENA_VERSION add_subdirectory(extern) include_directories(include ${LZO_INCLUDE_DIR} ${ZLIB_INCLUDE_DIR}) +if (NOT WIN32) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") +endif() if(WIN32) list(APPEND CORE_EXTRA src/win32_largefilewrapper.c) @@ -108,7 +110,9 @@ add_library(AthenaWiiSave include/md5.h include/sha1.h ) +if(NOT WIN32) set_source_files_properties(src/aes.cpp PROPERTIES COMPILE_FLAGS -maes) +endif() add_library(AthenaZelda src/Athena/ALTTPFile.cpp diff --git a/include/Athena/DNA.hpp b/include/Athena/DNA.hpp index f4479e6..0cc0f7c 100644 --- a/include/Athena/DNA.hpp +++ b/include/Athena/DNA.hpp @@ -53,16 +53,16 @@ struct DNA using Vector = std::vector; template - using Buffer = struct Buffer; + using Buffer = struct Athena::io::Buffer; template - using String = struct String; + using String = struct Athena::io::String; template - using WString = struct WString; + using WString = struct Athena::io::WString; template - using WStringAsString = struct WStringAsString; + using WStringAsString = struct Athena::io::WStringAsString; template struct Seek {}; diff --git a/include/Athena/FileReader.hpp b/include/Athena/FileReader.hpp index e3acec0..461c1c2 100644 --- a/include/Athena/FileReader.hpp +++ b/include/Athena/FileReader.hpp @@ -14,6 +14,9 @@ class FileReader : public IStreamReader { public: FileReader(const std::string& filename, atInt32 cacheSize = (32 * 1024)); +#if _WIN32 + FileReader(const std::wstring& filename, atInt32 cacheSize = (32 * 1024)); +#endif virtual ~FileReader(); inline const std::string& filename() const {return m_filename;} @@ -31,6 +34,9 @@ public: void setCacheSize(const atInt32 blockSize); protected: std::string m_filename; +#if _WIN32 + std::wstring m_wfilename; +#endif FILE* m_fileHandle; std::unique_ptr m_cacheData; atInt32 m_blockSize; diff --git a/include/Athena/FileWriter.hpp b/include/Athena/FileWriter.hpp index 04faceb..e09c2fb 100644 --- a/include/Athena/FileWriter.hpp +++ b/include/Athena/FileWriter.hpp @@ -12,6 +12,9 @@ class FileWriter : public IStreamWriter { public: FileWriter(const std::string& filename, bool overwrite = true); +#if _WIN32 + FileWriter(const std::wstring& filename, bool overwrite = true); +#endif virtual ~FileWriter(); void open(bool overwrite = true); @@ -25,6 +28,9 @@ public: private: std::string m_filename; +#if _WIN32 + std::wstring m_wfilename; +#endif FILE* m_fileHandle; atUint8 m_currentByte; atUint64 m_bytePosition; diff --git a/src/Athena/Compression.cpp b/src/Athena/Compression.cpp index 4f9ff95..433841a 100644 --- a/src/Athena/Compression.cpp +++ b/src/Athena/Compression.cpp @@ -97,7 +97,7 @@ atInt32 decompressLZO(const atUint8* source, const atInt32 sourceSize, atUint8* int srcSize = sourceSize; lzo_uint size = dstSize; int result = lzo1x_decompress_safe(source, srcSize, dst, &size, NULL); - dstSize -= size; + dstSize -= (atInt32)size; return result; } diff --git a/src/Athena/FileReader.cpp b/src/Athena/FileReader.cpp index 3fea0ce..f517f3a 100644 --- a/src/Athena/FileReader.cpp +++ b/src/Athena/FileReader.cpp @@ -20,6 +20,18 @@ FileReader::FileReader(const std::string& filename, atInt32 cacheSize) setCacheSize(cacheSize); } +#if _WIN32 +FileReader::FileReader(const std::wstring& filename, atInt32 cacheSize) + : m_wfilename(filename), + m_fileHandle(nullptr), + m_cacheData(nullptr), + m_offset(0) +{ + open(); + setCacheSize(cacheSize); +} +#endif + FileReader::~FileReader() { if (isOpen()) @@ -28,7 +40,14 @@ FileReader::~FileReader() void FileReader::open() { +#if _WIN32 + if (m_wfilename.size()) + m_fileHandle = _wfopen(m_wfilename.c_str(), L"rb"); + else + m_fileHandle = fopen(m_filename.c_str(), "rb"); +#else m_fileHandle = fopen(m_filename.c_str(), "rb"); +#endif if (!m_fileHandle) { @@ -85,7 +104,7 @@ void FileReader::seek(atInt64 pos, SeekOrigin origin) { fseeko64(m_fileHandle, block * m_blockSize, SEEK_SET); fread(m_cacheData.get(), 1, m_blockSize, m_fileHandle); - m_curBlock = block; + m_curBlock = (atInt32)block; } } else if (fseeko64(m_fileHandle, pos, (int)origin) != 0) @@ -142,7 +161,7 @@ atUint64 FileReader::readUBytesToBuf(void* buf, atUint64 len) { fseeko64(m_fileHandle, block * m_blockSize, SEEK_SET); fread(m_cacheData.get(), 1, m_blockSize, m_fileHandle); - m_curBlock = block; + m_curBlock = (atInt32)block; } cacheSize = rem; @@ -165,7 +184,7 @@ void FileReader::setCacheSize(const atInt32 blockSize) m_blockSize = blockSize; if (m_blockSize > length()) - m_blockSize = length(); + m_blockSize = (atInt32)length(); m_curBlock = -1; if (m_blockSize > 0) diff --git a/src/Athena/FileWriter.cpp b/src/Athena/FileWriter.cpp index 6d97371..7376d47 100644 --- a/src/Athena/FileWriter.cpp +++ b/src/Athena/FileWriter.cpp @@ -18,6 +18,16 @@ FileWriter::FileWriter(const std::string& filename, bool overwrite) open(overwrite); } +#if _WIN32 +FileWriter::FileWriter(const std::wstring& filename, bool overwrite) + : m_wfilename(filename), + m_fileHandle(NULL), + m_bytePosition(0) +{ + open(overwrite); +} +#endif + FileWriter::~FileWriter() { if (isOpen()) @@ -26,10 +36,29 @@ FileWriter::~FileWriter() void FileWriter::open(bool overwrite) { +#if _WIN32 + if (m_wfilename.size()) + { + if (overwrite) + m_fileHandle = _wfopen(m_wfilename.c_str(), L"w+b"); + else + m_fileHandle = _wfopen(m_wfilename.c_str(), L"r+b"); + } + else + { + if (overwrite) + m_fileHandle = fopen(m_filename.c_str(), "w+b"); + else + m_fileHandle = fopen(m_filename.c_str(), "r+b"); + } +#else if (overwrite) m_fileHandle = fopen(m_filename.c_str(), "w+b"); else - m_fileHandle = fopen(m_filename.c_str(), "r+b"); + m_fileHandle = fopen(m_filename.c_str(), "r+b"); +#endif + + if (!m_fileHandle) {