summaryrefslogtreecommitdiff
path: root/wii
diff options
context:
space:
mode:
authorTreeki <treeki@gmail.com>2010-10-14 03:15:41 +0200
committerTreeki <treeki@gmail.com>2010-10-14 03:15:41 +0200
commitfdf8cfec2b795393d7ee901abaf747575067068b (patch)
tree1eb3a65765c1c43c4ea91530462cd15d54f995d1 /wii
parent7213ca723a65dff8ebb0c6c08669695217e60453 (diff)
downloadLayoutStudio-fdf8cfec2b795393d7ee901abaf747575067068b.tar.gz
LayoutStudio-fdf8cfec2b795393d7ee901abaf747575067068b.zip
bugfixes; working U8 archive support for reading/writing
Diffstat (limited to 'wii')
-rw-r--r--wii/archiveu8.cpp243
-rw-r--r--wii/archiveu8.h24
-rw-r--r--wii/common.h13
-rw-r--r--wii/filesystem.cpp13
-rw-r--r--wii/stringtablebuilder.cpp25
-rw-r--r--wii/stringtablebuilder.h22
6 files changed, 335 insertions, 5 deletions
diff --git a/wii/archiveu8.cpp b/wii/archiveu8.cpp
index aeb3aac..5e0ad5d 100644
--- a/wii/archiveu8.cpp
+++ b/wii/archiveu8.cpp
@@ -18,3 +18,246 @@
#include "archiveu8.h"
+
+
+
+
+WiiArchiveU8::WiiArchiveU8() { }
+
+WiiArchiveU8::WiiArchiveU8(QDataStream &stream) {
+ U8ReadInfo info;
+ info.startPos = stream.device()->pos();
+
+ quint32 magic;
+ stream >> (quint32&)magic;
+
+ if (magic != 0x55AA382D)
+ qWarning() << "WiiArchiveU8: tried to load an archive without the U8 magic";
+
+ quint32 fstStart, fstSize, dataOffset;
+ stream >> (quint32&)fstStart;
+ stream >> (quint32&)fstSize;
+ stream >> (quint32&)dataOffset;
+
+ // read the FST
+ stream.device()->seek(info.startPos + fstStart);
+
+ // read the root node
+ quint32 rootNodeLastChild;
+
+ stream.skipRawData(8); // ignore type, nameOffset and dataOffset
+ stream >> (quint32&)rootNodeLastChild;
+
+ // but before we do this, read the string table
+ qint64 savePos = stream.device()->pos();
+ stream.device()->seek(savePos + ((rootNodeLastChild - 1) * 12));
+
+ int stringTableLength = fstSize - (rootNodeLastChild * 12);
+
+ QByteArray rawStringTable;
+ rawStringTable.resize(stringTableLength);
+ stream.readRawData(rawStringTable.data(), stringTableLength);
+
+ info.stringTable = QString::fromAscii(rawStringTable.constData(), stringTableLength);
+
+ // now read the root node
+ stream.device()->seek(savePos);
+
+ qDebug() << "Reading root node: last child is" << rootNodeLastChild;
+ info.currentNode = 1;
+ this->readDir(stream, this->root, rootNodeLastChild, info);
+}
+
+
+void WiiArchiveU8::readDir(QDataStream &in, WiiDirectory &dir, int lastChild, U8ReadInfo &info) {
+ // read every node in this directory
+
+ while (info.currentNode < lastChild) {
+ info.currentNode++;
+
+ //qDebug() << "Reading @ pos" << in.device()->pos();
+
+ quint32 value, dataOffset, size;
+ in >> (quint32&)value;
+ in >> (quint32&)dataOffset;
+ in >> (quint32&)size;
+
+ int nameOffset = value & 0xFFFFFF;
+ int type = value >> 24;
+
+ WiiFSObject *newObj;
+ if (type == 0)
+ newObj = new WiiFile;
+ else if (type == 1)
+ newObj = new WiiDirectory;
+ else
+ qFatal("WiiArchiveU8::readDir: Unknown fs obj type %d", type);
+
+ // get the name
+ int nameSize = info.stringTable.indexOf(QChar::Null, nameOffset) - nameOffset;
+ newObj->name = info.stringTable.mid(nameOffset, nameSize);
+
+ qDebug() << "Reading node" << info.currentNode << newObj->name << "- type:" << type << "size:" << size << "offset:" << dataOffset;
+
+ // read the contents
+ if (newObj->isFile()) {
+ // get the file data
+ qint64 savePos = in.device()->pos();
+ in.device()->seek(info.startPos + dataOffset);
+
+ WiiFile *file = (WiiFile*)newObj;
+ file->data.resize(size);
+ in.readRawData(file->data.data(), size);
+
+ in.device()->seek(savePos);
+
+ } else if (newObj->isDirectory()) {
+ // read the children
+ this->readDir(in, *((WiiDirectory*)newObj), size, info);
+
+ }
+
+ qDebug() << "Adding" << newObj->name << "to" << dir.name;
+ dir.addChild(newObj);
+ };
+}
+
+
+
+void WiiArchiveU8::writeToDataStream(QDataStream &out) {
+ U8WriteInfo info;
+
+ // first off, before we do anything else, create the string table
+ this->addNodeToStringTable(this->root, info.stringTableBuilder);
+
+ QByteArray stringTable = info.stringTableBuilder.pack();
+
+ // calculate various fun offsets/sizes
+ int nodeCount = 0;
+ this->countNode(this->root, &nodeCount);
+
+ info.startPos = out.device()->pos();
+
+ quint32 fstStart = 0x20;
+ quint32 nodeDataSize = nodeCount * 12;
+ quint32 stringTableSize = stringTable.length();
+ quint32 fstSize = nodeDataSize + stringTableSize;
+ quint32 dataOffset = AlignUp(fstStart + fstSize, 0x20);
+
+ qDebug() << "Writing: fstStart" << fstStart << "nodeCount" << nodeCount;
+ qDebug() << "nodeDataSize" << nodeDataSize << "stringTableSize" << stringTableSize;
+ qDebug() << "fstSize" << fstSize << "dataOffset" << dataOffset;
+
+ // now write the header
+ out << (quint32)0x55AA382D;
+ out << (quint32)fstStart;
+ out << (quint32)fstSize;
+ out << (quint32)dataOffset;
+
+ WritePadding(0x10, out);
+
+ // write root node
+ info.currentNode = 1; // root node is 1
+ info.currentRecursionLevel = 0;
+ info.nextDataOffset = dataOffset;
+
+ out << (quint32)(0x01000000 << info.stringTableBuilder.add(""));
+ out << (quint32)0;
+ out << (quint32)nodeCount;
+
+ this->writeDir(out, this->root, info);
+
+ // write string table
+ out.writeRawData(stringTable.constData(), stringTable.length());
+
+ // write data (after padding)
+ WritePadding(dataOffset - fstSize - fstStart, out);
+
+ this->writeNodeData(out, this->root);
+
+ // looks like we are finally done
+}
+
+
+void WiiArchiveU8::addNodeToStringTable(WiiFSObject &node, WiiStringTableBuilder &table) {
+ table.add(node.name);
+
+ if (node.isDirectory()) {
+ WiiDirectory *dir = (WiiDirectory*)&node;
+
+ foreach (WiiFSObject *p, dir->children)
+ this->addNodeToStringTable(*p, table);
+ }
+}
+
+
+void WiiArchiveU8::countNode(WiiFSObject &node, int *countPtr) {
+ (*countPtr)++;
+
+ if (node.isDirectory()) {
+ WiiDirectory *dir = (WiiDirectory*)&node;
+
+ foreach (WiiFSObject *p, dir->children)
+ this->countNode(*p, countPtr);
+ }
+}
+
+
+void WiiArchiveU8::writeDir(QDataStream &out, WiiDirectory &dir, U8WriteInfo &info) {
+ foreach (WiiFSObject *p, dir.children) {
+ info.currentNode++;
+
+ if (p->isDirectory()) {
+ // write directory
+ WiiDirectory *thisDir = (WiiDirectory*)p;
+
+ out << (quint32)(0x01000000 | info.stringTableBuilder.add(thisDir->name));
+ out << (quint32)info.currentRecursionLevel;
+
+ qint64 lastChildFieldPos = out.device()->pos();
+ out << (quint32)0; // placeholder
+
+ info.currentRecursionLevel++;
+
+ this->writeDir(out, *thisDir, info);
+
+ info.currentRecursionLevel--;
+
+ // write lastChild field
+ qint64 dirEndPos = out.device()->pos();
+
+ out.device()->seek(lastChildFieldPos);
+ out << (quint32)info.currentNode;
+ out.device()->seek(dirEndPos);
+
+ } else if (p->isFile()) {
+ // write file
+ WiiFile *thisFile = (WiiFile*)p;
+
+ out << (quint32)info.stringTableBuilder.add(thisFile->name);
+ out << (quint32)info.nextDataOffset;
+ out << (quint32)thisFile->data.size();
+
+ info.nextDataOffset = AlignUp(info.nextDataOffset + thisFile->data.size(), 0x20);
+ }
+ }
+}
+
+
+void WiiArchiveU8::writeNodeData(QDataStream &out, WiiFSObject &node) {
+ if (node.isDirectory()) {
+ // write all the children's data
+ WiiDirectory *thisDir = (WiiDirectory*)&node;
+
+ foreach (WiiFSObject *p, thisDir->children)
+ this->writeNodeData(out, *p);
+
+ } else if (node.isFile()) {
+ // write this file's data
+ WiiFile *thisFile = (WiiFile*)&node;
+
+ int len = thisFile->data.length();
+ out.writeRawData(thisFile->data.constData(), len);
+ WritePadding(AlignUp(len, 0x20) - len, out);
+ }
+}
diff --git a/wii/archiveu8.h b/wii/archiveu8.h
index 66f3fbe..6f54ba2 100644
--- a/wii/archiveu8.h
+++ b/wii/archiveu8.h
@@ -20,8 +20,22 @@
#include "common.h"
#include "filesystem.h"
+#include "stringtablebuilder.h"
+struct U8ReadInfo {
+ qint64 startPos;
+ QString stringTable;
+ int currentNode;
+};
+
+struct U8WriteInfo {
+ qint64 startPos;
+ WiiStringTableBuilder stringTableBuilder;
+ int currentRecursionLevel;
+ int currentNode;
+ int nextDataOffset;
+};
class WiiArchiveU8 {
@@ -32,6 +46,16 @@ public:
WiiDirectory root;
void writeToDataStream(QDataStream &out);
+
+private:
+ void readDir(QDataStream &in, WiiDirectory &dir, int lastChild, U8ReadInfo &info);
+
+ void addNodeToStringTable(WiiFSObject &node, WiiStringTableBuilder &table);
+ void countNode(WiiFSObject &node, int *countPtr);
+
+ void writeDir(QDataStream &out, WiiDirectory &dir, U8WriteInfo &info);
+
+ void writeNodeData(QDataStream &out, WiiFSObject &node);
};
#endif // WIIARCHIVEU8_H
diff --git a/wii/common.h b/wii/common.h
index aa7dce0..34f1526 100644
--- a/wii/common.h
+++ b/wii/common.h
@@ -10,6 +10,15 @@
#include <QtCore/QDebug>
#include <QtCore/QVector>
+inline quint32 AlignUp(quint32 value, quint32 alignTo) {
+ return (value + alignTo - 1) & ~(alignTo - 1);
+}
+
+inline quint32 AlignDown(quint32 value, quint32 alignTo) {
+ return value & ~(alignTo - 1);
+}
+
+
inline quint32 BitExtract(quint32 value, int count, int start) {
// this function relies on heavy compiler optimisation to be efficient :p
quint32 mask = 0;
@@ -31,6 +40,10 @@ inline quint32 BitInsert(quint32 value, int newValue, int count, int start) {
return value;
}
+inline void WritePadding(int num, QDataStream &out) {
+ for (int i = 0; i < num; i++)
+ out << (quint8)0;
+}
diff --git a/wii/filesystem.cpp b/wii/filesystem.cpp
index 9926109..09fe207 100644
--- a/wii/filesystem.cpp
+++ b/wii/filesystem.cpp
@@ -39,7 +39,7 @@ void WiiFSObject::unlinkFromParent() {
}
bool WiiFSObject::nameIsEqual(QString check) {
- return (bool)(QString::compare(this->name, check, Qt::CaseInsensitive));
+ return (bool)(QString::compare(this->name, check, Qt::CaseInsensitive) == 0);
}
bool WiiFSObject::isFile() { return false; }
@@ -82,7 +82,7 @@ WiiFSObject *WiiDirectory::resolvePath(QString path) {
// special case: handle absolute paths
if (pathComponents.at(0) == "") {
while (currentDir->parent != 0)
- currentDir = currentDir->parent;
+ currentDir = (WiiDirectory*)currentDir->parent;
pathComponents.removeFirst();
}
@@ -90,14 +90,15 @@ WiiFSObject *WiiDirectory::resolvePath(QString path) {
// now we can loop through the path
while (!pathComponents.isEmpty()) {
QString next = pathComponents.takeFirst();
+ WiiFSObject *nextObj;
// get the next object in the path
if (next == ".") {
- next = currentDir;
+ nextObj = currentDir;
} else if (next == "..") {
- next = currentDir->parent;
+ nextObj = currentDir->parent;
} else {
- WiiFSObject *nextObj = currentDir->findByName(next, false);
+ nextObj = currentDir->findByName(next, false);
}
if (nextObj == 0) {
@@ -113,6 +114,7 @@ WiiFSObject *WiiDirectory::resolvePath(QString path) {
// verify that this object is a directory
if (!nextObj->isDirectory()) {
qWarning() << "Failed to resolve path" << path << ": component" << next << "is not a directory";
+ return 0;
}
// ok, this has to be a directory, so let's just continue
@@ -120,6 +122,7 @@ WiiFSObject *WiiDirectory::resolvePath(QString path) {
}
// we should not reach here
+ qWarning() << "Failed to resolve path" << path << ": unknown error";
return 0;
}
diff --git a/wii/stringtablebuilder.cpp b/wii/stringtablebuilder.cpp
new file mode 100644
index 0000000..679247c
--- /dev/null
+++ b/wii/stringtablebuilder.cpp
@@ -0,0 +1,25 @@
+#include "stringtablebuilder.h"
+
+WiiStringTableBuilder::WiiStringTableBuilder() {
+ m_nextOffset = 0;
+ m_data = "";
+}
+
+quint32 WiiStringTableBuilder::add(QString str) {
+ if (m_lookup.contains(str))
+ return m_lookup.value(str);
+
+ quint32 added = m_nextOffset;
+ m_lookup.insert(str, added);
+
+ m_data.append(str.toAscii());
+ m_data.append('\0');
+
+ m_nextOffset = m_data.length();
+
+ return added;
+}
+
+QByteArray WiiStringTableBuilder::pack() {
+ return m_data;
+}
diff --git a/wii/stringtablebuilder.h b/wii/stringtablebuilder.h
new file mode 100644
index 0000000..f416a37
--- /dev/null
+++ b/wii/stringtablebuilder.h
@@ -0,0 +1,22 @@
+#ifndef STRINGTABLEBUILDER_H
+#define STRINGTABLEBUILDER_H
+
+#include "common.h"
+#include <QtCore/QString>
+#include <QtCore/QHash>
+#include <QtCore/QByteArray>
+
+class WiiStringTableBuilder {
+public:
+ WiiStringTableBuilder();
+
+ quint32 add(QString str);
+ QByteArray pack();
+
+protected:
+ int m_nextOffset;
+ QByteArray m_data;
+ QHash<QString, quint32> m_lookup;
+};
+
+#endif // STRINGTABLEBUILDER_H