Begin Tag's structure
parent
80afbd9e9b
commit
78db7d5dc9
@ -1,4 +1,9 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
|
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
|
||||||
|
<component name="CidrRootsConfiguration">
|
||||||
|
<excludeRoots>
|
||||||
|
<file path="$PROJECT_DIR$/cmake-build-debug" />
|
||||||
|
</excludeRoots>
|
||||||
|
</component>
|
||||||
</project>
|
</project>
|
@ -0,0 +1,34 @@
|
|||||||
|
#include "AbstractNamedTag.h"
|
||||||
|
|
||||||
|
#include <QtEndian>
|
||||||
|
#include <QtCore/QDataStream>
|
||||||
|
|
||||||
|
AbstractNamedTag::AbstractNamedTag(const quint8 id) : AbstractTag(id) {}
|
||||||
|
|
||||||
|
const bool AbstractNamedTag::readFromData(QDataStream &data) {
|
||||||
|
quint16 size;
|
||||||
|
|
||||||
|
data >> size;
|
||||||
|
size = qFromBigEndian(size);
|
||||||
|
|
||||||
|
char *rawName = new char[size];
|
||||||
|
if (data.readRawData(rawName, size) == -1) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_name = QString::fromUtf8(rawName);
|
||||||
|
return readPayloadFromData(data);
|
||||||
|
}
|
||||||
|
const bool AbstractNamedTag::writeToData(QDataStream &data) const {
|
||||||
|
const QByteArray rawName = m_name.toUtf8();
|
||||||
|
|
||||||
|
data << qToBigEndian(rawName.size()) << rawName.constData();
|
||||||
|
return writePayloadToData(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
const QString &AbstractNamedTag::name() const {
|
||||||
|
return m_name;
|
||||||
|
}
|
||||||
|
void AbstractNamedTag::setName(const QString &name) {
|
||||||
|
m_name = name;
|
||||||
|
}
|
@ -0,0 +1,53 @@
|
|||||||
|
#ifndef NBTMODIFIER_ABSTRACTNAMEDTAG_H
|
||||||
|
#define NBTMODIFIER_ABSTRACTNAMEDTAG_H
|
||||||
|
|
||||||
|
#include "AbstractTag.h"
|
||||||
|
|
||||||
|
#include <QtCore/QString>
|
||||||
|
|
||||||
|
class AbstractNamedTag : public AbstractTag {
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* Get the tag name
|
||||||
|
*
|
||||||
|
* @return The tag name
|
||||||
|
*/
|
||||||
|
const QString& name() const;
|
||||||
|
/**
|
||||||
|
* Set the tag name
|
||||||
|
*
|
||||||
|
* @param name The new tag name
|
||||||
|
*/
|
||||||
|
void setName (const QString &name);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
explicit AbstractNamedTag(const quint8 id);
|
||||||
|
|
||||||
|
virtual const bool readFromData(QDataStream &data) override;
|
||||||
|
virtual const bool writeToData(QDataStream &data) const override;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read a tag payload from a data stream
|
||||||
|
*
|
||||||
|
* @param data The data stream
|
||||||
|
*
|
||||||
|
* @return True if read is success, otherwise false
|
||||||
|
*/
|
||||||
|
virtual const bool readPayloadFromData(QDataStream &data) = 0;
|
||||||
|
/**
|
||||||
|
* Write a tag payload to a data stream
|
||||||
|
*
|
||||||
|
* @param data The data stream
|
||||||
|
*
|
||||||
|
* @return True if write is success, otherwise false
|
||||||
|
*/
|
||||||
|
virtual const bool writePayloadToData(QDataStream &data) const = 0;
|
||||||
|
|
||||||
|
private:
|
||||||
|
/**
|
||||||
|
* The tag name
|
||||||
|
*/
|
||||||
|
QString m_name;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif //NBTMODIFIER_ABSTRACTNAMEDTAG_H
|
@ -0,0 +1,41 @@
|
|||||||
|
#include "AbstractTag.h"
|
||||||
|
|
||||||
|
#include "TagEnd.h"
|
||||||
|
#include "TagByte.h"
|
||||||
|
|
||||||
|
#include <QtCore/QDataStream>
|
||||||
|
|
||||||
|
AbstractTag::AbstractTag(const quint8 id) : m_id(id) {}
|
||||||
|
|
||||||
|
const quint8 AbstractTag::id() const {
|
||||||
|
return m_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
AbstractTag *AbstractTag::fromData(QDataStream &data, bool *ok) {
|
||||||
|
quint8 id;
|
||||||
|
data >> id;
|
||||||
|
|
||||||
|
AbstractTag *tag = nullptr;
|
||||||
|
switch (id) {
|
||||||
|
case TagEnd::ID:
|
||||||
|
tag = new TagEnd();
|
||||||
|
|
||||||
|
case TagByte::ID:
|
||||||
|
tag = new TagByte();
|
||||||
|
}
|
||||||
|
if (tag == nullptr) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
*ok = tag->readFromData(data);
|
||||||
|
return tag;
|
||||||
|
}
|
||||||
|
QByteArray AbstractTag::toData(bool *ok) {
|
||||||
|
QByteArray data;
|
||||||
|
QDataStream stream(data);
|
||||||
|
|
||||||
|
stream << id();
|
||||||
|
*ok = writeToData(stream);
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
@ -0,0 +1,50 @@
|
|||||||
|
#ifndef NBTMODIFIER_ABSTRACTTAG_H
|
||||||
|
#define NBTMODIFIER_ABSTRACTTAG_H
|
||||||
|
|
||||||
|
#include <QtGlobal>
|
||||||
|
|
||||||
|
class QDataStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface for all NBT tags
|
||||||
|
*/
|
||||||
|
class AbstractTag {
|
||||||
|
public:
|
||||||
|
const quint8 id() const;
|
||||||
|
|
||||||
|
static AbstractTag *fromData(QDataStream &data, bool *ok = nullptr);
|
||||||
|
QByteArray toData(bool *ok = nullptr);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
/**
|
||||||
|
* Create a new tag
|
||||||
|
*
|
||||||
|
* @param id
|
||||||
|
*/
|
||||||
|
explicit AbstractTag(const quint8 id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read a tag from a data stream
|
||||||
|
*
|
||||||
|
* @param data The data stream
|
||||||
|
*
|
||||||
|
* @return True if read is success, otherwise false
|
||||||
|
*/
|
||||||
|
virtual const bool readFromData(QDataStream &data) = 0;
|
||||||
|
/**
|
||||||
|
* Write a tag to a data stream
|
||||||
|
*
|
||||||
|
* @param data The data stream
|
||||||
|
*
|
||||||
|
* @return True if write is success, otherwise false
|
||||||
|
*/
|
||||||
|
virtual const bool writeToData(QDataStream &data) const = 0;
|
||||||
|
|
||||||
|
private:
|
||||||
|
/**
|
||||||
|
* The tag's ID
|
||||||
|
*/
|
||||||
|
const quint8 m_id;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif //NBTMODIFIER_ABSTRACTTAG_H
|
@ -0,0 +1,14 @@
|
|||||||
|
#include "TagByte.h"
|
||||||
|
|
||||||
|
#include <QtCore/QDataStream>
|
||||||
|
|
||||||
|
TagByte::TagByte(const qint8 value) : AbstractNamedTag(ID), m_value(value) {}
|
||||||
|
|
||||||
|
bool const TagByte::readPayloadFromData(QDataStream &data) {
|
||||||
|
data >> m_value;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool const TagByte::writePayloadToData(QDataStream &data) const {
|
||||||
|
data << m_value;
|
||||||
|
return true;
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
#ifndef NBTMODIFIER_TAGBYTE_H
|
||||||
|
#define NBTMODIFIER_TAGBYTE_H
|
||||||
|
|
||||||
|
#include "AbstractNamedTag.h"
|
||||||
|
|
||||||
|
class TagByte : public AbstractNamedTag {
|
||||||
|
public:
|
||||||
|
static constexpr quint8 ID = 1;
|
||||||
|
|
||||||
|
TagByte(const qint8 value = 0);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual bool const readPayloadFromData(QDataStream &data) override;
|
||||||
|
virtual bool const writePayloadToData(QDataStream &data) const override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
qint8 m_value;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif //NBTMODIFIER_TAGBYTE_H
|
@ -0,0 +1,11 @@
|
|||||||
|
#include "TagEnd.h"
|
||||||
|
|
||||||
|
TagEnd::TagEnd() : AbstractTag(ID) {
|
||||||
|
}
|
||||||
|
|
||||||
|
bool const TagEnd::readFromData(QDataStream &data) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool const TagEnd::writeToData(QDataStream &data) const {
|
||||||
|
return true;
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
#ifndef NBTMODIFIER_TAGEND_H
|
||||||
|
#define NBTMODIFIER_TAGEND_H
|
||||||
|
|
||||||
|
#include "AbstractTag.h"
|
||||||
|
|
||||||
|
class TagEnd : public AbstractTag {
|
||||||
|
public:
|
||||||
|
static constexpr quint8 ID = 0;
|
||||||
|
|
||||||
|
explicit TagEnd();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual bool const readFromData(QDataStream &data) override;
|
||||||
|
virtual bool const writeToData(QDataStream &data) const override;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif //NBTMODIFIER_TAGEND_H
|
Loading…
Reference in New Issue