Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
去除isLittleEndian参数,默认为true
Browse files Browse the repository at this point in the history
新增NBT docs
  • Loading branch information
twoone3 committed Feb 6, 2022
1 parent 853f2d9 commit 9a181b5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
19 changes: 19 additions & 0 deletions docs/Class/NBT.md
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
!> 此页待完善

# NBT
Named Binary Tag
## init(type, value)
1. type: 数据类型
value: 数据
例: NBT('Int',3) NBT('Compound')
2. type: SNBT
value: SNBT字符串
例: NBT('SNBT', snbt)
3. type: Binary
value: bytes
例: NBT('Binary', bytes)

## getType() -> str
## toBinary() -> bytes
## toJson(indentation:int = 4) -> bytes
## toSNBT() -> str
## append(value:mc.NBT) -> None
37 changes: 28 additions & 9 deletions mod/PyNBT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,34 @@ struct PyNBT {
}
Py_RETURN_NOTIMPLEMENTED;
}
//有三种模式
// 1. a1数据类型 a2数据
// NBT('Int',3) NBT('Compound')
// 2. SNBT模式 a2填SNBT字符串
// NBT('SNBT', snbt)
// 3. 二进制模式 a2填bytes
// NBT('Binary', bytes)
static int init(PyObject* self, PyObject* args, PyObject* kwds) {
const char* type_str = "";
PyObject* value = nullptr;
Py_KEYWORDS_LIST("type", "value");
//此处可能晦涩难懂 return nullptr,-1;
Py_PARSE_WITH_KEYWORDS("s|O", &type_str, &value), -1;
if (type_str == "SNBT"sv) {
try {
PyNBT_RAW(self) = CompoundTag::fromSNBT(PyUnicodeToStr(value));
}
catch (const std::exception& e) {
Py_RETURN_ERROR(e.what()), -1;
}
return 0;
}
else if (type_str == "Binary"sv) {
Py_ssize_t len;
char* buffer;
PyBytes_AsStringAndSize(value, &buffer, &len);
PyNBT_RAW(self) = CompoundTag::fromBinaryNBT(buffer, len);
return 0;
}
auto type = magic_enum::enum_cast<Tag::Type>(type_str);
if (!type)
Py_RETURN_ERROR_FORMAT("Invalied NBT type %s", type_str), -1;
Expand Down Expand Up @@ -83,7 +105,7 @@ struct PyNBT {
break;
case Tag::ByteArray:
PyNBT_RAW(self) = ByteArrayTag::create();
//PyNBT_RAW(self)->asByteArrayTag()->value() = PyLong_AsLong(value);
//TODO: enable to construct ByteArray
break;
case Tag::String:
PyNBT_RAW(self) = StringTag::create();
Expand All @@ -105,9 +127,7 @@ struct PyNBT {
return subtype->tp_alloc(&PyNBT_Type, 0);
}
static void dealloc(PyObject* self) {

PyNBT_RAW(self)
.~unique_ptr();
PyNBT_RAW(self).~unique_ptr();
Py_TYPE(self)->tp_free(self);
}
static Py_ssize_t length(PyObject* self) {
Expand Down Expand Up @@ -164,10 +184,9 @@ struct PyNBT {
return ToPyObject(magic_enum::enum_name(thiz->getTagType()));
}
Py_METHOD_DEFINE(toBinary) {
bool is_little_endian = true;
Py_PARSE("|b", &is_little_endian);
PyNBT_VALUE;
return ToPyObject(thiz->asCompoundTag()->toBinaryNBT(is_little_endian));
auto data = thiz->asCompoundTag()->toBinaryNBT();
return PyBytes_FromStringAndSize(data.c_str(), data.length());
}
Py_METHOD_DEFINE(toJson) {
int indentatiton = 4;
Expand All @@ -194,7 +213,7 @@ struct PyNBT {

inline static PyMethodDef methods[] {
Py_METHOD_NOARGS(getType),
Py_METHOD_VARARGS(toBinary),
Py_METHOD_NOARGS(toBinary),
Py_METHOD_VARARGS(toJson),
Py_METHOD_NOARGS(toSNBT),
Py_METHOD_VARARGS(append),
Expand Down

0 comments on commit 9a181b5

Please sign in to comment.