Skip to content

Commit

Permalink
Merge pull request #74 from Amulet-Team/pybind
Browse files Browse the repository at this point in the history
C++ Rewrite
  • Loading branch information
gentlegiantJGC authored Jul 15, 2024
2 parents a6178c2 + 8dbe6f4 commit a07bd6f
Show file tree
Hide file tree
Showing 141 changed files with 10,698 additions and 12,059 deletions.
6 changes: 0 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,3 @@ temp
*.vs/
*.vcxproj*
*.sln

# C++ files
*.c
!**/_cpp/**/*.c
*.cpp
!**/_cpp/**/*.cpp
3 changes: 2 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
recursive-include src *.pyi py.typed *.py *.pyx *.pxd *.cpp *.hpp
prune tests
recursive-include src *.pyi py.typed *.py *.cpp *.hpp
14 changes: 7 additions & 7 deletions docs_source/getting_started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,32 +52,32 @@
bedrock_named_tag: NamedTag
# Load binary NBT
named_tag = amulet_nbt.load(
named_tag = amulet_nbt.read_nbt(
"the/path/to/your/binary/nbt/file",
preset=java_encoding,
compressed=True, # These inputs must be specified as keyword inputs like this.
) # from a file
named_tag = amulet_nbt.load(
named_tag = amulet_nbt.read_nbt(
"the/path/to/your/binary/nbt/file",
compressed=True, # These inputs must be specified as keyword inputs like this.
little_endian=False, # If you do not define them they will default to these values
string_encoding=mutf8_encoding
) # from a file
named_tag = amulet_nbt.load(b'<nbt file bytes>') # from a bytes object
named_tag = amulet_nbt.read_nbt(b'<nbt file bytes>') # from a bytes object
# Note that Java Edition usually uses compressed modified UTF-8.
java_named_tag = amulet_nbt.load(
java_named_tag = amulet_nbt.read_nbt(
"the/path/to/your/binary/java/nbt/file",
string_encoding=mutf8_encoding
)
# Bedrock edition data is stored in little endian format and uses non-compressed UTF-8 but can also have arbitrary bytes.
bedrock_named_tag = amulet_nbt.load(
bedrock_named_tag = amulet_nbt.read_nbt(
"the/path/to/your/binary/bedrock/nbt/file",
preset=bedrock_encoding,
compressed=False,
)
bedrock_named_tag = amulet_nbt.load(
bedrock_named_tag = amulet_nbt.read_nbt(
"the/path/to/your/binary/bedrock/nbt/file",
compressed=False,
little_endian=True,
Expand Down Expand Up @@ -117,7 +117,7 @@
# You can also parse the stringified NBT format used in Java commands.
tag = amulet_nbt.from_snbt('{key1: "value", key2: 0b, key3: 0.0f}')
tag = amulet_nbt.read_snbt('{key1: "value", key2: 0b, key3: 0.0f}')
# tag should look like this
# TAG_Compound(
# key1: TAG_String("value"),
Expand Down
23 changes: 5 additions & 18 deletions docs_source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -186,24 +186,11 @@

These are functions to load the binary and stringified NBT formats.

.. autofunction:: amulet_nbt.load
.. autofunction:: amulet_nbt.load_array
.. autofunction:: amulet_nbt.read_nbt
.. autofunction:: amulet_nbt.read_nbt_array
.. autoclass:: amulet_nbt.ReadOffset
:members:
.. autofunction:: amulet_nbt.from_snbt


############
Exceptions
############

.. autoexception:: amulet_nbt.NBTError
.. autoexception:: amulet_nbt.NBTLoadError
:show-inheritance:
.. autoexception:: amulet_nbt.NBTFormatError
:show-inheritance:
.. autoexception:: amulet_nbt.SNBTParseError
:show-inheritance:
.. autofunction:: amulet_nbt.read_snbt


#################
Expand All @@ -217,7 +204,7 @@ These are instances of a class storing C++ functions to encode and decode string
:inherited-members:
:undoc-members:

They can be passed to the string_encoding argument in to_nbt, save_to, load and load_array to control the string encoding behaviour.
They can be passed to the string_encoding argument in to_nbt, save_to, read_nbt and read_nbt_array to control the string encoding behaviour.

The usual string encoding scheme is called UTF-8.

Expand All @@ -237,7 +224,7 @@ Java Edition uses a modified version of UTF-8 implemented by the Java programmin
Encoding Presets
##################

The string encoding and endianness can be defined separatly but for simplicity the following presets have been defined.
The string encoding and endianness can be defined separately but for simplicity the following presets have been defined.

.. autodata:: amulet_nbt.java_encoding
.. autodata:: amulet_nbt.bedrock_encoding
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
requires = [
"setuptools >= 42",
"wheel",
"cython ~= 3.0",
"versioneer",
"numpy ~= 1.17"
"pybind11 ~= 2.12",
]
build-backend = "setuptools.build_meta"
7 changes: 5 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ platforms = any
[options]
package_dir=
=src
packages = find_namespace:
packages = find:
zip_safe = False
python_requires = ~=3.11
install_requires =
numpy ~= 1.17
numpy >= 1.17, < 3.0

[options.packages.find]
where=src
Expand All @@ -30,6 +30,9 @@ amulet_nbt =
py.typed
*.pyd
*.so
*.dylib
**/*.hpp
cpp/**/*.cpp


[options.extras_require]
Expand Down
30 changes: 23 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from setuptools import setup
from Cython.Build import cythonize
from setuptools import setup, Extension
import versioneer
import numpy
import sysconfig
from distutils import ccompiler
import sys
import pybind11
import glob

if (sysconfig.get_config_var("CXX") or ccompiler.get_default_compiler()).split()[
0
Expand All @@ -20,8 +20,24 @@
setup(
version=versioneer.get_version(),
cmdclass=versioneer.get_cmdclass(),
include_dirs=[numpy.get_include()],
ext_modules=cythonize(
f"src/**/*.pyx", language_level=3, aliases={"CPPCARGS": CompileArgs}
),
libraries=[
(
"amulet_nbt",
dict(
sources=glob.glob("src/amulet_nbt/cpp/**/*.cpp", recursive=True),
include_dirs=["src/amulet_nbt/include"],
cflags=CompileArgs,
),
)
],
ext_modules=[
Extension(
name="amulet_nbt._nbt",
sources=glob.glob("src/amulet_nbt/pybind/**/*.cpp", recursive=True),
include_dirs=["src/amulet_nbt/include", pybind11.get_include()],
libraries=["amulet_nbt"],
define_macros=[("PYBIND11_DETAILED_ERROR_MESSAGES", None)],
extra_compile_args=CompileArgs,
)
],
)
193 changes: 89 additions & 104 deletions src/amulet_nbt/__init__.pyx → src/amulet_nbt/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,94 @@
import re
from typing import Union

from typing import TypeAlias
from . import _version

__version__ = _version.get_versions()["version"]
__major__ = int(re.match(r"\d+", __version__)[0])
__major__ = int(re.match(r"\d+", __version__)[0]) # type: ignore

del re
del _version


def get_include() -> str:
import os

return os.path.join(__path__[0], "include")


from ._nbt import (
# Abstract classes
AbstractBaseTag,
AbstractBaseImmutableTag,
AbstractBaseMutableTag,
AbstractBaseNumericTag,
AbstractBaseIntTag,
AbstractBaseFloatTag,
AbstractBaseArrayTag,
# Tag classes
ByteTag,
ShortTag,
IntTag,
LongTag,
FloatTag,
DoubleTag,
ByteArrayTag,
StringTag,
ListTag,
CompoundTag,
IntArrayTag,
LongArrayTag,
# Tag class aliases
ByteTag as TAG_Byte,
ShortTag as TAG_Short,
IntTag as TAG_Int,
LongTag as TAG_Long,
FloatTag as TAG_Float,
DoubleTag as TAG_Double,
ByteArrayTag as TAG_Byte_Array,
StringTag as TAG_String,
ListTag as TAG_List,
CompoundTag as TAG_Compound,
IntArrayTag as TAG_Int_Array,
LongArrayTag as TAG_Long_Array,
NamedTag,
read_nbt,
read_nbt_array,
ReadOffset,
read_snbt,
StringEncoding,
mutf8_encoding,
utf8_encoding,
utf8_escape_encoding,
EncodingPreset,
java_encoding,
bedrock_encoding,
)

SNBTType: TypeAlias = str
IntType: TypeAlias = ByteTag | ShortTag | IntTag | LongTag
FloatType: TypeAlias = FloatTag | DoubleTag
NumberType: TypeAlias = ByteTag | ShortTag | IntTag | LongTag | FloatTag | DoubleTag
ArrayType: TypeAlias = ByteArrayTag | IntArrayTag | LongArrayTag
AnyNBT: TypeAlias = (
ByteTag
| ShortTag
| IntTag
| LongTag
| FloatTag
| DoubleTag
| ByteArrayTag
| StringTag
| ListTag
| CompoundTag
| IntArrayTag
| LongArrayTag
)


__all__ = [
"__version__",
"__major__",
"get_include",
"AbstractBaseTag",
"AbstractBaseImmutableTag",
"AbstractBaseMutableTag",
Expand Down Expand Up @@ -38,14 +121,10 @@
"CompoundTag",
"TAG_Compound",
"NamedTag",
"load",
"load_array",
"read_nbt",
"read_nbt_array",
"ReadOffset",
"from_snbt",
"NBTError",
"NBTLoadError",
"NBTFormatError",
"SNBTParseError",
"read_snbt",
"SNBTType",
"IntType",
"FloatType",
Expand All @@ -60,97 +139,3 @@
"java_encoding",
"bedrock_encoding",
]

from ._tag.abc import (
AbstractBaseTag,
AbstractBaseImmutableTag,
AbstractBaseMutableTag,
)
from ._tag.numeric import AbstractBaseNumericTag

# Types
from ._tag.int import (
AbstractBaseIntTag,
ByteTag,
ShortTag,
IntTag,
LongTag,
ByteTag as TAG_Byte,
ShortTag as TAG_Short,
IntTag as TAG_Int,
LongTag as TAG_Long,
)
from ._tag.float import (
AbstractBaseFloatTag,
FloatTag,
DoubleTag,
FloatTag as TAG_Float,
DoubleTag as TAG_Double,
)
from ._tag.array import (
AbstractBaseArrayTag,
ByteArrayTag,
IntArrayTag,
LongArrayTag,
ByteArrayTag as TAG_Byte_Array,
IntArrayTag as TAG_Int_Array,
LongArrayTag as TAG_Long_Array,
)
from ._tag.string import (
StringTag,
StringTag as TAG_String,
)

from ._tag.named_tag import NamedTag

from ._tag.list import (
ListTag,
ListTag as TAG_List,
)
from ._tag.compound import (
CompoundTag,
CompoundTag as TAG_Compound,
)

# Load functions
from amulet_nbt._nbt_encoding._binary import load, load_array, ReadOffset
from amulet_nbt._nbt_encoding._string import from_snbt

from ._errors import NBTError, NBTLoadError, NBTFormatError, SNBTParseError

from ._string_encoding.encoding import (
mutf8_encoding,
utf8_encoding,
utf8_escape_encoding,
StringEncoding,
)
from ._nbt_encoding._binary.encoding_preset import (
java_encoding,
bedrock_encoding,
EncodingPreset,
)

SNBTType = str

IntType = Union[ByteTag, ShortTag, IntTag, LongTag]

FloatType = Union[FloatTag, DoubleTag]

NumberType = Union[ByteTag, ShortTag, IntTag, LongTag, FloatTag, DoubleTag]

ArrayType = Union[ByteArrayTag, IntArrayTag, LongArrayTag]

AnyNBT = Union[
ByteTag,
ShortTag,
IntTag,
LongTag,
FloatTag,
DoubleTag,
ByteArrayTag,
StringTag,
ListTag,
CompoundTag,
IntArrayTag,
LongArrayTag,
]
Loading

0 comments on commit a07bd6f

Please sign in to comment.