2022-06-27 11:46:13 +02:00
// Copyright 2022, FOSS-VG Developers and Contributers
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// version 3 along with this program.
// If not, see https://www.gnu.org/licenses/agpl-3.0.en.html
2022-06-24 12:15:34 +02:00
// information taken from https://wiki.vg/NBT
// This is an attempt at creating a uniform model for all NBT tags to allow for a uniform interface based on subclassing a single NBT tag super class.
2022-06-25 13:37:57 +02:00
// NBT tags have a type, optionally a name which consists of the name size and the name string, optionally content type, and optionally a payload which can consist of optionally content type, optionally a content size,
// and the stored content. The format in which they are stored is as follows: <type><name size><name><payload>. All numbers are stored in big endian representation.
2022-06-24 12:15:34 +02:00
// All tag types:
2022-06-28 14:25:32 +02:00
// generic representation: Tag(uint8:tag_type, String:name, uint16:name_size, byte[]:content, int32:size)
2022-06-25 13:37:57 +02:00
// None (compound end): Tag( 0, "", 0, None, 0) => used to determine the end of a compound tag, only the type gets stored
2022-06-28 14:25:32 +02:00
// int8: Tag( 1, String:name, uint16:name_size, int8:content, 1) => a single signed byte, size not stored
2022-06-26 01:23:58 +02:00
// int16: Tag( 2, String:name, uint16:name_size, int16:content, 2) => 16 bit signed integer, size not stored
// int32: Tag( 3, String:name, uint16:name_size, int32:content, 4) => 32 bit signed integer, size not stored
// int64: Tag( 4, String:name, uint16:name_size, int64:content, 8) => 64 bit signed integer, size not stored
2022-08-04 07:42:40 +02:00
// float: Tag( 5, String:name, uint16:name_size, float:content, 4) => 32 bit IEEE754 floating point number, size not stored
// double: Tag( 6, String:name, uint16:name_size, double:content, 8) => 64 bit IEEE754 floating point number, size not stored
2022-06-28 14:25:32 +02:00
// int8[]: Tag( 7, String:name, uint16:name_size, int8[]:content, int32:size) => content stored prefixed with size
// String: Tag( 8, String:name, uint16:name_size, String:content, uint16:size) => Java style modified UTF-8 string, content stored prefixed with size
2022-06-25 13:37:57 +02:00
// Tag[] (list): Tag<Tag:type>( 9, String:name, uint16:name_size, Tag[]:content, int32:size) => list of tags of the same type with tag type and name information omitted prefixed by (in order) content type and size
2022-06-26 01:23:58 +02:00
// Tag[] (compound): Tag(10, String:name, uint16:name_size, Tag[]:content, int32:size) => list of tags, last tag is always an end tag, size not stored
// int32[]: Tag(11, String:name, uint16:name_size, int32[]:content,int32:size) => list of 32 bit signed integers prefixed with its size, endianness not verified at this point
// int64[]: Tag(12, String:name, uint16:name_size, int64[]:content,int32:size) => list of 64 bit signed integers prefixed with its size, endianness not verified at this point
2022-06-27 04:50:32 +02:00
# pragma once
# include <cstdint>
# include <vector>
2022-07-20 08:38:04 +02:00
# include <tinyutf8/tinyutf8.h>
2022-08-02 03:35:08 +02:00
# include "error.hpp"
2022-06-27 04:50:32 +02:00
namespace NBT {
2022-06-28 15:19:47 +02:00
namespace helper {
2022-06-28 16:51:52 +02:00
ErrorOr < int8_t > readInt8 ( uint8_t data [ ] , uint64_t dataSize , uint64_t currentPosition ) ;
ErrorOr < int16_t > readInt16 ( uint8_t data [ ] , uint64_t dataSize , uint64_t currentPosition ) ;
ErrorOr < int32_t > readInt32 ( uint8_t data [ ] , uint64_t dataSize , uint64_t currentPosition ) ;
ErrorOr < int64_t > readInt64 ( uint8_t data [ ] , uint64_t dataSize , uint64_t currentPosition ) ;
2022-08-04 07:42:40 +02:00
ErrorOr < float > readFloat ( uint8_t data [ ] , uint64_t dataSize , uint64_t currentPosition ) ;
ErrorOr < double > readDouble ( uint8_t data [ ] , uint64_t dataSize , uint64_t currentPosition ) ;
2022-06-28 16:51:52 +02:00
ErrorOr < std : : vector < int8_t > > readInt8Array ( uint8_t data [ ] , uint64_t dataSize , uint64_t currentPosition ) ;
2022-07-20 08:38:04 +02:00
ErrorOr < tiny_utf8 : : string > readString ( uint8_t data [ ] , uint64_t dataSize , uint64_t currentPosition ) ;
2022-06-28 16:51:52 +02:00
ErrorOr < std : : vector < int32_t > > readInt32Array ( uint8_t data [ ] , uint64_t dataSize , uint64_t currentPosition ) ;
ErrorOr < std : : vector < int64_t > > readInt64Array ( uint8_t data [ ] , uint64_t dataSize , uint64_t currentPosition ) ;
2022-07-02 02:08:32 +02:00
void writeInt8 ( std : : vector < uint8_t > * destination , int8_t data ) ;
void writeInt16 ( std : : vector < uint8_t > * destination , int16_t data ) ;
void writeInt32 ( std : : vector < uint8_t > * destination , int32_t data ) ;
void writeInt64 ( std : : vector < uint8_t > * destination , int64_t data ) ;
2022-08-04 07:42:40 +02:00
void writeFloat ( std : : vector < uint8_t > * destination , float data ) ;
void writeDouble ( std : : vector < uint8_t > * destination , double data ) ;
2022-07-02 02:08:32 +02:00
void writeInt8Array ( std : : vector < uint8_t > * destination , std : : vector < int8_t > data ) ;
2022-07-06 12:57:32 +02:00
void writeInt8Array ( std : : vector < uint8_t > * destination , int8_t data [ ] , uint32_t dataSize ) ;
2022-07-28 13:45:04 +02:00
void writeString ( std : : vector < uint8_t > * destination , tiny_utf8 : : string data ) ;
2022-07-02 02:08:32 +02:00
void writeInt32Array ( std : : vector < uint8_t > * destination , std : : vector < int32_t > data ) ;
2022-07-06 14:58:02 +02:00
void writeInt32Array ( std : : vector < uint8_t > * destination , int32_t data [ ] , uint32_t dataSize ) ;
2022-07-02 02:08:32 +02:00
void writeInt64Array ( std : : vector < uint8_t > * destination , std : : vector < int64_t > data ) ;
2022-07-06 14:58:02 +02:00
void writeInt64Array ( std : : vector < uint8_t > * destination , int64_t data [ ] , uint32_t dataSize ) ;
2022-06-28 15:19:47 +02:00
}
2022-08-03 20:31:12 +02:00
//Generic parent class to make declaration easier
template < typename T >
class Tag {
public :
uint8_t tagType ;
tiny_utf8 : : string name ;
uint16_t nameSize ;
T content ;
int32_t size ;
Tag ( ) { }
Tag ( uint8_t tagType , tiny_utf8 : : string name , uint16_t nameSize , T content , uint32_t size ) ;
} ;
class End : public Tag < uint8_t > {
public :
End ( ) ;
} ;
class Byte : public Tag < int8_t > {
public :
Byte ( tiny_utf8 : : string name , uint16_t nameSize , int8_t content ) ;
Byte ( uint8_t data [ ] ) ;
bool validate ( uint8_t data [ ] ) ;
} ;
2022-06-28 16:51:52 +02:00
bool validateRawNBTData ( uint8_t data [ ] , int length ) ;
2022-06-27 04:50:32 +02:00
}