Fix compile errors in libnbt

This commit is contained in:
zontreck 2024-07-30 23:37:27 -07:00
parent 7744e18d8c
commit 8a2282b2c2
16 changed files with 479 additions and 14 deletions

View file

@ -1,6 +1,7 @@
#include "Accountant.h"
#include "Tag.h"
#include "CompoundTag.h"
#include "ListTag.h"
#include <iostream>
using namespace nbt;

View file

@ -2,6 +2,10 @@
#define NBTACCOUNTANT_H
#include "Tag.h" // Assuming this includes definitions for Tag, CompoundTag, and ListTag
#include "../utils/ByteLayer.h"
#include "../utils/StringBuilder.h"
#include "../utils/StringReader.h"
#include "../types/dynamic.h"
namespace nbt
{

View file

@ -2,7 +2,10 @@
#define BYTEARRAYTAG_H
#include "Tag.h"
#include "../utils/ByteLayer.h" // Assuming ByteLayer is a class you have
#include "../utils/ByteLayer.h"
#include "../utils/StringBuilder.h"
#include "../utils/StringReader.h"
#include "../types/dynamic.h"
#include <vector>
#include <string>
#include <sstream>

View file

@ -6,6 +6,7 @@
#include "../utils/ByteLayer.h"
#include "../utils/StringBuilder.h"
#include "../utils/StringReader.h"
#include "../types/dynamic.h"
using namespace libac;

View file

@ -62,7 +62,7 @@ namespace nbt {
}
dynamic CompoundTag::getValue() const {
return dynamic(); // Returning a default dynamic instance
return new dynamic(0); // Returning a default dynamic instance
}
void CompoundTag::prettyPrint(int indent, bool recurse) const {
@ -88,23 +88,23 @@ namespace nbt {
if (!firstEntry) {
builder.append(",\n");
}
Tag::writeStringifiedNamedTag(tag, builder, indent);
Tag::writeStringifiedNamedTag(*tag, builder, indent);
firstEntry = false;
}
builder.append("\n" + std::string(indent - 1, '\t') + "}");
}
void CompoundTag::readStringifiedValue(StringReader& reader) {
reader.expect("{");
reader.expect('{');
while (reader.peek() != "}") {
while (reader.peek() != '}') {
Tag* tag = Tag::readStringifiedNamedTag(reader);
put(tag->getKey(), tag);
if (reader.peek() == ",") reader.next();
if (reader.peek() == ',') reader.next();
}
reader.expect("}");
reader.expect('}');
}
Tag* CompoundTag::operator[](const std::string& key) const {
@ -112,10 +112,6 @@ namespace nbt {
return (it != value.end()) ? it->second : nullptr;
}
void CompoundTag::operator[]=(const std::string& key, Tag* tag) {
put(key, tag);
}
void CompoundTag::addAll(const std::map<std::string, Tag*>& other) {
value.insert(other.begin(), other.end());
for (auto& [key, tag] : other) {

View file

@ -5,9 +5,10 @@
#include <map>
#include <string>
#include "Tag.h"
#include "ByteLayer.h"
#include "StringBuilder.h"
#include "StringReader.h"
#include "../utils/ByteLayer.h"
#include "../utils/StringBuilder.h"
#include "../utils/StringReader.h"
#include "../types/dynamic.h"
namespace nbt {

View file

@ -2,6 +2,10 @@
#define DOUBLE_TAG_H
#include "Tag.h"
#include "../utils/ByteLayer.h"
#include "../utils/StringBuilder.h"
#include "../utils/StringReader.h"
#include "../types/dynamic.h"
namespace nbt
{

View file

@ -3,6 +3,9 @@
#include "Tag.h"
#include "../utils/ByteLayer.h"
#include "../utils/StringBuilder.h"
#include "../utils/StringReader.h"
#include "../types/dynamic.h"
using namespace std;

View file

@ -2,6 +2,10 @@
#define FLOAT_TAG_H
#include "Tag.h"
#include "../utils/ByteLayer.h"
#include "../utils/StringBuilder.h"
#include "../utils/StringReader.h"
#include "../types/dynamic.h"
namespace nbt
{

104
src/nbt/IntArrayTag.cpp Normal file
View file

@ -0,0 +1,104 @@
#include "IntArrayTag.h"
#include <iostream>
#include <iomanip>
#include <sstream>
#include <stdexcept>
namespace nbt
{
IntArrayTag::IntArrayTag() : Tag()
{
// Default constructor
}
IntArrayTag::IntArrayTag(const std::vector<int> &value) : Tag(), value(value)
{
// Constructor with initial value
}
void IntArrayTag::readValue(ByteLayer &data)
{
int len = data.readInt();
value.clear();
for (int i = 0; i < len; i++)
{
value.push_back(data.readInt());
}
}
void IntArrayTag::writeValue(ByteLayer &data) const
{
data.writeInt(static_cast<int>(value.size()));
for (int i : value)
{
data.writeInt(i);
}
}
TagType IntArrayTag::getTagType() const
{
return TagType::IntArray;
}
dynamic IntArrayTag::getValue() const
{
return dynamic(value);
}
void IntArrayTag::setValue(const dynamic &val)
{
value = val.get<vector<int>>();
}
void IntArrayTag::prettyPrint(int indent, bool recurse) const
{
std::cout << std::setw(indent) << std::setfill('\t') << ""
<< Tag::getCanonicalName(getTagType()) << ": [";
for (size_t i = 0; i < value.size(); i++)
{
std::cout << value[i];
if (i < value.size() - 1)
{
std::cout << ", ";
}
}
std::cout << "]";
}
void IntArrayTag::writeStringifiedValue(StringBuilder &builder, int indent, bool isList) const
{
builder << (isList ? std::string(indent, '\t') : "") << "[I; ";
for (size_t i = 0; i < value.size(); i++)
{
builder << value[i] << "I";
if (i < value.size() - 1)
{
builder << ", ";
}
}
builder << "]";
}
void IntArrayTag::readStringifiedValue(StringReader &reader)
{
std::string temp;
reader.expect('[');
reader.expect('I');
reader.expect(';');
value.clear();
while (reader.peek() != ']')
{
uint8_t num = stoi(reader.readNumber());
value.push_back(num);
reader.expect('I');
if (reader.peek() == ',')
reader.next();
}
reader.expect(']');
}
}

40
src/nbt/IntArrayTag.h Normal file
View file

@ -0,0 +1,40 @@
#ifndef INTARRAYTAG_H
#define INTARRAYTAG_H
#include "Tag.h"
#include "../utils/ByteLayer.h"
#include "../utils/StringBuilder.h"
#include "../utils/StringReader.h"
#include "../types/dynamic.h"
#include <vector>
#include <string>
#include <sstream>
using namespace libac;
using namespace std;
namespace nbt
{
class IntArrayTag : public Tag
{
public:
IntArrayTag();
IntArrayTag(const std::vector<int> &value);
// Override functions from the Tag class
void readValue(ByteLayer &data) override;
void writeValue(ByteLayer &data) const override;
TagType getTagType() const override;
dynamic getValue() const;
void setValue(const dynamic &val) override;
void prettyPrint(int indent, bool recurse) const override;
void writeStringifiedValue(StringBuilder &builder, int indent, bool isList) const override;
void readStringifiedValue(StringReader &reader) override;
private:
vector<int> value;
};
}
#endif // INTARRAYTAG_H

63
src/nbt/IntTag.cpp Normal file
View file

@ -0,0 +1,63 @@
#include <string>
#include <iostream>
#include <iomanip>
#include "IntTag.h"
using namespace libac;
using namespace std;
namespace nbt
{
IntTag::IntTag() : Tag(), value(0.0)
{
}
IntTag::IntTag(int val) : Tag(), value(val)
{
}
void IntTag::readValue(ByteLayer &data)
{
value = data.readInt();
}
void IntTag::writeValue(ByteLayer &data) const
{
data.writeInt(value);
}
TagType IntTag::getTagType() const
{
return TagType::Int;
}
dynamic IntTag::getValue() const
{
return dynamic(value);
}
void IntTag::setValue(const dynamic &val)
{
value = val.get<int>();
}
void IntTag::prettyPrint(int indent, bool recurse) const
{
cout << setw(indent) << setfill('\t') << Tag::getCanonicalName(getTagType()) << ": " << value;
}
void IntTag::writeStringifiedValue(StringBuilder &builder, int indent, bool isList) const
{
builder << (isList ? std::string(indent, '\t') : "") << value << "i";
}
void IntTag::readStringifiedValue(StringReader &reader)
{
float val = stof(reader.readNumber());
value = val;
if(reader.peek() == 'i')
reader.expect('i');
}
}

34
src/nbt/IntTag.h Normal file
View file

@ -0,0 +1,34 @@
#ifndef INT_TAG_H
#define INT_TAG_H
#include "Tag.h"
#include "../utils/ByteLayer.h"
#include "../utils/StringBuilder.h"
#include "../utils/StringReader.h"
#include "../types/dynamic.h"
namespace nbt
{
class IntTag : public Tag
{
public:
IntTag();
IntTag(int val);
~IntTag();
// Function overrides
void readValue(ByteLayer &data) override;
void writeValue(ByteLayer &data) const override;
TagType getTagType() const override;
dynamic getValue() const override;
void setValue(const dynamic &val) override;
void prettyPrint(int indent, bool recurse) const override;
void writeStringifiedValue(StringBuilder &builder, int indent, bool isList) const override;
void readStringifiedValue(StringReader &reader) override;
private:
int value;
};
}
#endif

149
src/nbt/ListTag.cpp Normal file
View file

@ -0,0 +1,149 @@
#include "ListTag.h"
#include "Tag.h"
#include <iostream>
#include <string>
#include <bits/stdc++.h>
using namespace std;
namespace nbt {
ListTag::ListTag() : Tag() {
// Constructor implementation if needed
}
TagType ListTag::getListTagType() const {
if (value.empty()) {
return TagType::End;
}
return value[0]->getTagType();
}
void ListTag::readValue(ByteLayer &data) {
TagType type = (TagType) data.readByte();
int size = data.readInt();
for (int i = 0; i < size; ++i) {
Tag* tag = Tag::makeTagOfType(type);
tag->readValue(data);
add(tag);
}
}
void ListTag::writeValue(ByteLayer &data) const {
TagType type = TagType::End;
if (!value.empty()) {
type = value[0]->getTagType();
}
data.writeByte(static_cast<int>(type));
data.writeInt(static_cast<int>(value.size()));
for (const auto& tag : value) {
tag->writeValue(data);
}
}
void ListTag::add(Tag* tag) {
TagType type = TagType::End;
if (!value.empty()) {
type = value[0]->getTagType();
}
if (type == TagType::End || type == tag->getTagType()) {
value.push_back(tag);
tag->updateParent(this);
}
}
Tag* ListTag::get(int index) const {
if (index >= 0 && index < static_cast<int>(value.size())) {
return value[index];
} else {
return new EndTag(); // Return a new EndTag instance if out of range
}
}
void ListTag::remove(Tag* tag) {
auto it = std::find(value.begin(), value.end(), tag);
if (it != value.end()) {
(*it)->updateParent(nullptr);
value.erase(it);
}
}
void ListTag::removeAt(int index) {
if (index >= 0 && index < static_cast<int>(value.size())) {
value[index]->updateParent(nullptr);
value.erase(value.begin() + index);
}
}
int ListTag::indexOf(Tag* tag) const {
auto it = find(value.begin(), value.end(), tag);
return it != value.end() ? std::distance(value.begin(), it) : -1;
}
TagType ListTag::getTagType() const {
return TagType::List;
}
void ListTag::setValue(const dynamic &val) {
// Implementation if needed
}
dynamic ListTag::getValue() const {
return new dynamic(0); // Return a default dynamic instance if needed
}
int ListTag::size() const {
return static_cast<int>(value.size());
}
void ListTag::clear() {
for (auto& tag : value) {
tag->updateParent(nullptr);
tag->setParentTagType(TagType::End);
}
value.clear();
}
void ListTag::prettyPrint(int indent, bool recurse) const {
std::string indentStr(indent, '\t');
std::cout << indentStr << Tag::getCanonicalName(getTagType()) << ": [" << value.size() << " entries]" << std::endl;
std::cout << indentStr << "[" << std::endl;
for (const auto& tag : value) {
tag->prettyPrint(indent + 1, true);
}
}
void ListTag::endPrettyPrint(int indent) const {
std::cout << std::string(indent, '\t') << "]" << std::endl;
}
void ListTag::writeStringifiedValue(StringBuilder &builder, int indent, bool isList) const {
builder.append(std::string(isList ? indent - 1 : 0, '\t') + "[\n");
bool firstTag = true;
for (const auto& tag : value) {
if (!firstTag) {
builder.append(",\n");
}
firstTag = false;
tag->writeStringifiedValue(builder, indent + 1, true);
}
builder.append("\n" + std::string(indent - 1, '\t') + "]");
}
void ListTag::readStringifiedValue(StringReader &reader) {
reader.expect('[');
while (reader.peek() != ']') {
TagType type = Tag::getStringifiedTagType(reader);
Tag* newTag = Tag::makeTagOfType(type);
newTag->readStringifiedValue(reader);
add(newTag);
if (reader.peek() == ',') reader.next();
}
reader.expect(']');
}
}

43
src/nbt/ListTag.h Normal file
View file

@ -0,0 +1,43 @@
#ifndef LISTTAG_H
#define LISTTAG_H
#include <vector>
#include <string>
#include "Tag.h"
#include "../utils/ByteLayer.h"
#include "../utils/StringBuilder.h"
#include "../utils/StringReader.h"
#include "../types/dynamic.h"
#include "EndTag.h"
namespace nbt {
class ListTag : public Tag {
public:
ListTag();
TagType getListTagType() const;
void readValue(ByteLayer &data) override;
void writeValue(ByteLayer &data) const override;
void add(Tag* tag);
Tag* get(int index) const;
void remove(Tag* tag);
void removeAt(int index);
int indexOf(Tag* tag) const;
TagType getTagType() const override;
void setValue(const dynamic &val) override;
dynamic getValue() const override;
int size() const;
void clear();
void prettyPrint(int indent, bool recurse) const override;
void endPrettyPrint(int indent) const;
void writeStringifiedValue(StringBuilder &builder, int indent, bool isList) const override;
void readStringifiedValue(StringReader &reader) override;
private:
std::vector<Tag*> value;
};
}
#endif // LISTTAG_H

View file

@ -2,6 +2,11 @@
#include "EndTag.h"
#include "ByteTag.h"
#include "ByteArrayTag.h"
#include "DoubleTag.h"
#include "FloatTag.h"
#include "CompoundTag.h"
#include "IntArrayTag.h"
#include "IntTag.h"
#include <cctype>
#include <algorithm>
@ -25,6 +30,16 @@ namespace nbt
return new ByteTag();
case TagType::ByteArray:
return new ByteArrayTag();
case TagType::Double:
return new DoubleTag();
case TagType::Float:
return new FloatTag();
case TagType::Compound:
return new CompoundTag();
case TagType::IntArray:
return new IntArrayTag();
case TagType::Int:
return new IntTag();
default:
// default handling
return nullptr;