* Working on Wii Port

This commit is contained in:
Antidote 2013-09-08 20:36:54 -07:00
parent baf16a6fbd
commit 740fbd4273
12 changed files with 415 additions and 48 deletions

View File

@ -52,6 +52,17 @@ public:
*/
BinaryReader(const std::string& filename);
/*! \brief Sets the target file
*
* \sa Endian
* \param filepath The path to write to.
*/
void setFilepath(const std::string& filepath);
/*! \brief Returns the target file
*
*/
std::string filepath() const;
/*! \brief Reads a Int16 and swaps to proper endianness depending on platform
* and Stream settings, and advances the current position

View File

@ -47,12 +47,25 @@ public:
* \param stream The stream to write data to
*/
BinaryWriter(const Stream& stream);
/*! \brief This constructor creates an instance from a file on disk.
*
* \param filename The file to create the stream from
*/
BinaryWriter(const std::string& filename);
/*! \brief Sets the target file
*
* \sa Endian
* \param filepath The path to write to.
*/
void setFilepath(const std::string& filepath);
/*! \brief Returns the target file
*
*/
std::string filepath() const;
/*! \brief Saves the file to the specified file.
*
* \param filename If not empty, the filename to save to
@ -145,6 +158,7 @@ public:
* \param str The string to write to the buffer
*/
void writeString(const std::string& str);
protected:
Int8 readByte();
Int8* readBytes(Int64);

View File

@ -176,19 +176,19 @@ public:
*
* \return Int64 The length of the stream.
*/
Int64 length();
Int64 length() const;
/*! \brief Returns the current position in the stream.
*
* \return Int64 The current position in the stream.
*/
Int64 position();
Int64 position() const;
/*! \brief Returns whether or not the stream is at the end.
*
* \return bool True if at end; False otherwise.
*/
bool atEnd();
bool atEnd() const;
/*! \brief Sets whether the Stream resizes when the at the end of the buffer.
*

View File

@ -40,11 +40,11 @@ public:
*/
enum TextMode
{
Open, //!< The file is opened if it exists.
Create, //!< Create the file if it does not exist.
Open = 0x01, //!< The file is opened if it exists.
Create = 0x02, //!< Create the file if it does not exist.
OpenOrCreate = Open|Create, //!< If the file does not exist when opening the file it is created
Truncate, //!< All the data currently that is in the file is erased.
Append //!< After opening the file the current line is set to the end of the buffer
Truncate = 0x04, //!< All the data currently that is in the file is erased.
Append = 0x08 //!< After opening the file the current line is set to the end of the buffer
};
/*! \enum AccessMode
@ -57,8 +57,9 @@ public:
ReadWrite //!< The Stream can be read from or written to.
};
TextStream();
/*! \brief This constructor opens the file and loads all the lines. */
TextStream(const std::string& filename, TextMode fileMode = Open, AccessMode accessMode = ReadWrite);
TextStream(const std::string& filename, Uint32 fileMode = Open, AccessMode accessMode = ReadWrite);
/*! \brief Creates a new buffer and saves all lines to the specified file.
* \param filename The file, including path to save to.
@ -144,14 +145,20 @@ public:
*
* \return TextMode The mode to set.
*/
TextMode textMode() const;
Uint32 textMode() const;
/*! \brief Empties the stream.
*
*/
void truncate();
bool isOpenForReading() const;
bool isOpenForWriting() const;
private:
void loadLines();
std::string m_filename;
TextMode m_textmode;
Uint32 m_textmode;
AccessMode m_accessmode;
std::vector<std::string> m_lines;

View File

@ -17,6 +17,7 @@
#define __UTILITY_H__
#include <string>
#include <vector>
#include "Types.hpp"
namespace zelda
@ -27,9 +28,9 @@ namespace utility
bool isEmpty(Int8*, Uint32);
Uint16 swapU16(Uint16 val );
Int16 swap16 (Int16 val );
Uint32 swapU32(Uint32 val);
Int32 swap32 (Int32 val );
Int16 swap16 (Int16 val );
Uint32 swapU32(Uint32 val);
Int32 swap32 (Int32 val );
Uint64 swapU64(Uint64 val);
Int64 swap64 (Int64 val);
@ -40,6 +41,14 @@ bool isSystemBigEndian();
void fillRandom(Uint8 * rndArea, Uint8 count);
std::vector<std::string> split(const std::string &s, char delim);
void tolower(std::string& str);
void toupper(std::string& str);
std::string sprintf(const char* fmt, ...);
bool parseBool(const std::string& boolean, bool &valid);
int countChar(const std::string& str, const char chr, int& lastOccur);
} // utility
} // zelda

View File

@ -1,6 +1,6 @@
CONFIG += staticlib
TEMPLATE=lib
DESTDIR = ./
DESTDIR = ./lib
CONFIG(debug, debug|release){
DEFINES += DEBUG
@ -9,7 +9,7 @@ CONFIG(debug, debug|release){
CONFIG(release, release|debug){
DEFINES -= DEBUG
TARGET=zelda
unix:TARGET=zelda
}
QMAKE_CXXFLAGS += -std=c++0x

146
libzelda_wii.cbp Normal file
View File

@ -0,0 +1,146 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="libzelda_wii" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="lib/zeldad" prefix_auto="1" extension_auto="1" />
<Option working_dir="" />
<Option object_output="obj/Debug/" />
<Option type="2" />
<Option compiler="gcc" />
<Option createDefFile="1" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="lib/libzelda" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
<Target title="Debug Wii">
<Option output="lib/zeldawiid" prefix_auto="1" extension_auto="1" />
<Option working_dir="" />
<Option object_output="obj/Debug/" />
<Option type="2" />
<Option compiler="wii_compiler" />
<Option createDefFile="1" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release Wii">
<Option output="lib/libzeldawii" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="wii_compiler" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-std=c++0x" />
<Add option="-Wall" />
<Add directory="include" />
</Compiler>
<Unit filename="include/ALTTPEnums.hpp" />
<Unit filename="include/ALTTPFile.hpp" />
<Unit filename="include/ALTTPFileReader.hpp" />
<Unit filename="include/ALTTPFileWriter.hpp" />
<Unit filename="include/ALTTPQuest.hpp" />
<Unit filename="include/ALTTPStructs.hpp" />
<Unit filename="include/BinaryReader.hpp" />
<Unit filename="include/BinaryWriter.hpp" />
<Unit filename="include/Compression.hpp" />
<Unit filename="include/Exception.hpp" />
<Unit filename="include/FileNotFoundException.hpp" />
<Unit filename="include/IOException.hpp" />
<Unit filename="include/InvalidOperationException.hpp" />
<Unit filename="include/MCFile.hpp" />
<Unit filename="include/MCFileReader.hpp" />
<Unit filename="include/MCFileWriter.hpp" />
<Unit filename="include/Mainpage.hpp" />
<Unit filename="include/SSFile.hpp" />
<Unit filename="include/SSFileReader.hpp" />
<Unit filename="include/SSFileWriter.hpp" />
<Unit filename="include/SSQuest.hpp" />
<Unit filename="include/Stream.hpp" />
<Unit filename="include/TextStream.hpp" />
<Unit filename="include/Types.hpp" />
<Unit filename="include/WiiBanner.hpp" />
<Unit filename="include/WiiFile.hpp" />
<Unit filename="include/WiiImage.hpp" />
<Unit filename="include/WiiSave.hpp" />
<Unit filename="include/WiiSaveReader.hpp" />
<Unit filename="include/WiiSaveWriter.hpp" />
<Unit filename="include/ZQuestFile.hpp" />
<Unit filename="include/ZQuestFileReader.hpp" />
<Unit filename="include/ZQuestFileWriter.hpp" />
<Unit filename="include/aes.h" />
<Unit filename="include/bn.h" />
<Unit filename="include/ec.h" />
<Unit filename="include/md5.h" />
<Unit filename="include/sha1.h" />
<Unit filename="include/utf8.h" />
<Unit filename="include/utf8/checked.h" />
<Unit filename="include/utf8/core.h" />
<Unit filename="include/utf8/unchecked.h" />
<Unit filename="include/utility.hpp" />
<Unit filename="src/ALTTPFile.cpp" />
<Unit filename="src/ALTTPFileReader.cpp" />
<Unit filename="src/ALTTPFileWriter.cpp" />
<Unit filename="src/ALTTPQuest.cpp" />
<Unit filename="src/BinaryReader.cpp" />
<Unit filename="src/BinaryWriter.cpp" />
<Unit filename="src/Compression.cpp" />
<Unit filename="src/MCFile.cpp" />
<Unit filename="src/MCFileReader.cpp" />
<Unit filename="src/MCFileWriter.cpp" />
<Unit filename="src/SSFile.cpp" />
<Unit filename="src/SSFileReader.cpp" />
<Unit filename="src/SSFileWriter.cpp" />
<Unit filename="src/SSQuest.cpp" />
<Unit filename="src/Stream.cpp" />
<Unit filename="src/TextStream.cpp" />
<Unit filename="src/WiiBanner.cpp" />
<Unit filename="src/WiiFile.cpp" />
<Unit filename="src/WiiImage.cpp" />
<Unit filename="src/WiiSave.cpp" />
<Unit filename="src/WiiSaveReader.cpp" />
<Unit filename="src/WiiSaveWriter.cpp" />
<Unit filename="src/ZQuestFile.cpp" />
<Unit filename="src/ZQuestFileReader.cpp" />
<Unit filename="src/ZQuestFileWriter.cpp" />
<Unit filename="src/aes.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="src/bn.cpp" />
<Unit filename="src/ec.cpp" />
<Unit filename="src/md5.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="src/sha1.cpp" />
<Unit filename="src/utility.cpp" />
<Extensions>
<envvars />
<code_completion />
<lib_finder disable_auto="1" />
<debugger />
</Extensions>
</Project>
</CodeBlocks_project_file>

View File

@ -24,6 +24,10 @@
#include <vector>
#include <iostream>
#ifdef HW_RVL
#include <malloc.h>
#endif // HW_RVL
namespace zelda
{
namespace io
@ -55,7 +59,11 @@ BinaryReader::BinaryReader(const std::string& filename)
fseek(in, 0, SEEK_END);
length = ftell(in);
fseek(in, 0, SEEK_SET);
#ifdef HW_RVL
m_data = (Uint8*)memalign(32, length);
#else
m_data = new Uint8[length];
#endif
Uint32 done = 0;
Uint32 blocksize = BLOCKSZ;
@ -101,7 +109,7 @@ Int16 BinaryReader::readInt16()
if (m_position + sizeof(Int16) > m_length)
throw error::IOException("BinaryReader::readInt16 -> Position outside stream bounds");
Int16 ret = *(Int16*)(m_data + m_position);
m_position += 2;
m_position += sizeof(Int16);
if ((!utility::isSystemBigEndian() && m_endian == BigEndian) || (utility::isSystemBigEndian() && m_endian == LittleEndian))
ret = utility::swap16(ret);
@ -118,7 +126,7 @@ Uint16 BinaryReader::readUInt16()
if (m_position + sizeof(Uint16) > m_length)
throw error::IOException("BinaryReader::readUint16 -> Position outside stream bounds");
Uint16 ret = *(Uint16*)(m_data + m_position);
m_position += 2;
m_position += sizeof(Uint16);
if ((!utility::isSystemBigEndian() && m_endian == BigEndian) || (utility::isSystemBigEndian() && m_endian == LittleEndian))
ret = utility::swapU16(ret);

View File

@ -25,6 +25,10 @@
#include <vector>
#include <iostream>
#ifdef HW_RVL
#include <malloc.h>
#endif // HW_RVL
namespace zelda
{
namespace io
@ -44,12 +48,27 @@ BinaryWriter::BinaryWriter(const std::string& filename)
m_length = 0x10;
m_bitPosition = 0;
m_position = 0;
#ifdef HW_RVL
m_data = (Uint8*)memalign(32, m_length);
#else
m_data = new Uint8[m_length];
#endif
if (!m_data)
throw error::IOException("BinaryWriter::BinaryWriter -> Could not allocate memory!");
memset(m_data, 0, m_length);
}
void BinaryWriter::setFilepath(const std::string& filepath)
{
m_filepath = filepath;
}
std::string BinaryWriter::filepath() const
{
return m_filepath;
}
void BinaryWriter::save(const std::string& filename)
{
if (filename.empty() && m_filepath.empty())

View File

@ -19,6 +19,10 @@
#include <string.h>
#include <sstream>
#ifdef HW_RVL
#include <malloc.h>
#endif // HW_RVL
namespace zelda
{
namespace io
@ -49,7 +53,12 @@ Stream::Stream(const Uint8* data, Uint64 length) :
m_data = (Uint8*)data;
else
{
#ifdef HW_RVL
m_data = (Uint8*)memalign(32, m_length);
#else
m_data = new Uint8[m_length];
#endif
memset(m_data, 0, m_length);
}
}
@ -59,7 +68,11 @@ Stream::Stream(Int64 length) :
m_position(0),
m_length(length)
{
#ifdef HW_RVL
m_data = (Uint8*)memalign(32, m_length);
#else
m_data = new Uint8[m_length];
#endif
memset(m_data, 0, m_length);
}
@ -68,6 +81,7 @@ Stream::Stream(Stream* stream)
if (m_data)
delete[] m_data;
m_data = NULL;
m_data = stream->m_data;
m_position = stream->m_position;
m_length = stream->m_length;
@ -76,7 +90,11 @@ Stream::Stream(Stream* stream)
Stream::~Stream()
{
if (m_data)
#ifdef HW_RVL
free(m_data);
#else
delete[] m_data;
#endif
m_data = NULL;
m_position = 0;
@ -196,7 +214,13 @@ Int8* Stream::readBytes(Int64 length)
if (m_position + length > m_length)
throw error::IOException("Stream::readBytes -> Position passed stream bounds: " + m_position);
Int8* ret = new Int8[length];
Int8* ret;
#ifdef HW_RVL
ret = (Int8*)memalign(32, length);
#else
ret = new Int8[length];
#endif
memcpy(ret, (const Int8*)(m_data + m_position), length);
m_position += length;
return ret;
@ -250,13 +274,22 @@ void Stream::resize(Uint64 newSize)
throw error::InvalidOperationException("Stream::resize() -> New size cannot be less to the old size.");
// Allocate and copy new buffer
Uint8* newArray = new Uint8[newSize];
#ifdef HW_RVL
Uint8* newArray = (Uint8*)memalign(32, newSize);
#else
Uint8* newArray = new Uint8[newSize];
#endif
memset(newArray, 0, newSize);
memcpy(newArray, m_data, m_length);
// Delete the old one
delete[] m_data;
#ifdef HW_RVL
free(m_data);
#else
delete[] m_data;
#endif
// Swap the pointer and size out for the new ones.
m_data = newArray;
@ -266,7 +299,11 @@ void Stream::resize(Uint64 newSize)
void Stream::setData(const Uint8* data, Uint64 length)
{
if (m_data)
#ifdef HW_RVL
free(m_data);
#else
delete[] m_data;
#endif
m_data = (Uint8*)data;
m_length = length;
@ -281,17 +318,17 @@ Uint8* Stream::data() const
return ret;
}
Int64 Stream::length()
Int64 Stream::length() const
{
return m_length;
}
Int64 Stream::position()
Int64 Stream::position() const
{
return m_position;
}
bool Stream::atEnd()
bool Stream::atEnd() const
{
return m_position >= m_length;
}

View File

@ -23,7 +23,7 @@ namespace zelda
{
namespace io
{
TextStream::TextStream(const std::string& filename, TextMode fileMode, AccessMode accessMode) :
TextStream::TextStream(const std::string& filename, Uint32 fileMode, AccessMode accessMode) :
m_filename(filename),
m_textmode(fileMode),
m_accessmode(accessMode),
@ -69,6 +69,10 @@ TextStream::TextStream(const std::string& filename, TextMode fileMode, AccessMod
loadLines();
}
TextStream::TextStream()
: Stream()
{}
void TextStream::save(const std::string& filename)
{
if (m_accessmode != WriteOnly && m_accessmode != ReadWrite)
@ -78,7 +82,10 @@ void TextStream::save(const std::string& filename)
// We need a new buffer to write the new lines
if (m_data)
{
delete[] m_data;
m_data = NULL;
}
m_position = 0;
m_length = 1;
@ -90,6 +97,8 @@ void TextStream::save(const std::string& filename)
for (std::string s : m_lines)
writeBytes((Int8*)s.c_str(), s.size());
writeByte('\n');
FILE* out = fopen(m_filename.c_str(), "wb");
if(out)
@ -117,14 +126,14 @@ void TextStream::writeLine(const std::string& str)
{
if (m_accessmode != WriteOnly && m_accessmode != ReadWrite)
throw error::InvalidOperationException("TextStream::writeLine -> Stream not open for writing");
else if (m_currentLine > m_lines.size())
else if (m_currentLine >= m_lines.size())
{
m_lines.push_back(str);
m_lines.push_back(str + "\n");
m_currentLine++;
return;
}
m_lines[m_currentLine++] = str;
m_lines[m_currentLine++] = str + "\n";
}
void TextStream::writeLines(std::vector<std::string> strings)
@ -211,11 +220,16 @@ void TextStream::setTextMode(TextMode mode)
m_textmode = mode;
}
TextStream::TextMode TextStream::textMode() const
Uint32 TextStream::textMode() const
{
return m_textmode;
}
void TextStream::truncate()
{
m_lines.clear();
}
bool TextStream::isOpenForReading() const
{
return ((m_accessmode == ReadOnly || m_accessmode == ReadWrite) && m_accessmode != WriteOnly);
@ -229,35 +243,43 @@ bool TextStream::isOpenForWriting() const
// PRIVATE FUNCTIONS
void TextStream::loadLines()
{
while (!atEnd())
try
{
std::string line;
Uint8 c;
for (;;)
{
c = readByte();
if (c == '\r' || c == '\n')
while (!atEnd())
{
std::string line;
Uint8 c;
for (;;)
{
m_currentLine++;
line.push_back(c);
if (*(Uint8*)(m_data + m_position + 1) == '\n')
c = readByte();
if (c == '\r' || c == '\n')
{
m_currentLine++;
line.push_back(c);
if (*(Uint8*)(m_data + m_position + 1) == '\n')
{
line.push_back('\n');
m_position++; // advance position past the new line character
}
break;
}
if (c == '\0')
{
line.push_back('\n');
m_position++; // advance position past the new line character
break;
}
break;
line.push_back(c);
}
if (c == '\0')
{
line.push_back('\n');
break;
}
line.push_back(c);
m_lines.push_back(line);
}
m_lines.push_back(line);
}
catch(...)
{
// The stream MAY throw an out of range error but we're not concerned with it
}
}

View File

@ -17,6 +17,9 @@
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <sstream>
#include <algorithm>
#include <cstdarg>
namespace zelda
{
@ -111,5 +114,96 @@ double swapDouble(double val)
return (double)((Uint64)retVal);
}
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems)
{
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim))
elems.push_back(item);
return elems;
}
std::vector<std::string> split(const std::string &s, char delim)
{
std::vector<std::string> elems;
split(s, delim, elems);
return elems;
}
void tolower(std::string& str)
{
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
}
void toupper(std::string& str)
{
std::transform(str.begin(), str.end(), str.begin(), ::toupper);
}
std::string sprintf(const char* fmt, ...)
{
int size = 512;
char* buffer = 0;
buffer = new char[size];
va_list vl;
va_start(vl, fmt);
int nsize = vsnprintf(buffer, size, fmt, vl);
if(size<=nsize)
{ //fail delete buffer and try again
delete[] buffer;
buffer = 0;
buffer = new char[nsize+1]; //+1 for /0
nsize = vsnprintf(buffer, size, fmt, vl);
}
std::string ret(buffer);
va_end(vl);
delete[] buffer;
return ret;
}
bool parseBool(const std::string& boolean, bool &valid)
{
std::string val = boolean;
// compare must be case insensitive
// This is the cleanest solution since I only need to do it once
std::transform(val.begin(), val.end(), val.begin(), ::tolower);
// Check for true first
if (!val.compare("true") || !val.compare("1") || !val.compare("yes"))
return (valid = true);
// Now false
if (!val.compare("false") || !val.compare("0") || !val.compare("no"))
{
valid = true;
return false;
}
// Well that could've gone better
return (valid = false);
}
int countChar(const std::string &str, const char chr, int &lastOccur)
{
int ret = 0;
int index = 0;
for (char c : str)
{
if (c == chr)
{
lastOccur = index;
ret++;
}
index++;
}
return ret;
}
} // utility
} // zelda