|
| 1 | +<div align="center"> |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +</div> |
| 10 | + |
| 11 | +> ⚠️ **Compression & Linkage Notice** |
| 12 | +> |
| 13 | +> SwiftNBT utilizes the system native **zlib** library for maximum performance when handling GZip/Zlib compressed data (standard for Minecraft). |
| 14 | +> |
| 15 | +> **For Linux/Server-Side Swift:** Ensure `zlib1g-dev` is installed on your machine. |
| 16 | +> **For iOS/macOS:** It works out of the box using the system SDKs. |
| 17 | +
|
| 18 | +**SwiftNBT** is a lightweight, zero-dependency (other than system zlib) parser for the **Named Binary Tag (NBT)** format used by Minecraft. It is designed to be robust, type-safe, and incredibly easy to use with modern Swift syntax. |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +## How to Use: Installation |
| 23 | + |
| 24 | +### Swift Package Manager (SPM) |
| 25 | + |
| 26 | +You can add SwiftNBT to your project via Xcode or your `Package.swift` file. |
| 27 | + |
| 28 | +**In `Package.swift`:** |
| 29 | + |
| 30 | +```swift |
| 31 | +dependencies: [ |
| 32 | + .package(url: "https://github.com/arnaudrmt/SwiftNBT.git", from: "1.0.0") |
| 33 | +], |
| 34 | +targets: [ |
| 35 | + .target( |
| 36 | + name: "YourTarget", |
| 37 | + dependencies: ["SwiftNBT"] |
| 38 | + ) |
| 39 | +] |
| 40 | +``` |
| 41 | + |
| 42 | +**In Xcode:** |
| 43 | +1. Go to **File > Add Package Dependencies...** |
| 44 | +2. Paste the repository URL. |
| 45 | +3. Select the version and add it to your target. |
| 46 | + |
| 47 | +--- |
| 48 | + |
| 49 | +## API Usage Examples |
| 50 | + |
| 51 | +### 1. Parsing Raw Data (Base64 or Bytes) |
| 52 | + |
| 53 | +The most common use case is decoding a Base64 string that contains GZipped NBT data. |
| 54 | + |
| 55 | +```swift |
| 56 | +import SwiftNBT |
| 57 | + |
| 58 | +let base64String = "H4sIAAAAAAAA/..." // Your data |
| 59 | +guard let data = Data(base64Encoded: base64String) else { return } |
| 60 | + |
| 61 | +do { |
| 62 | + // The parser automatically handles GZip decompression |
| 63 | + let rootTag = try NBTParser.parse(data) |
| 64 | + |
| 65 | + print("Parsing successful!") |
| 66 | + rootTag.printTree() // Debug helper to see the structure |
| 67 | +} catch { |
| 68 | + print("Error: \(error)") |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +### 2. Accessing Data (The Easy Way) |
| 73 | + |
| 74 | +Forget large `switch` statements. Use the built-in optional helpers and subscripts to navigate the NBT tree effortlessly. |
| 75 | + |
| 76 | +```swift |
| 77 | +// Accessing a nested path: root -> tag -> display -> Name |
| 78 | +if let itemName = rootTag["tag"]?["display"]?["Name"]?.string { |
| 79 | + print("Item Name: \(itemName)") |
| 80 | +} |
| 81 | + |
| 82 | +// Accessing lists and arrays |
| 83 | +if let inventoryList = rootTag["i"]?.array { |
| 84 | + for (index, item) in inventoryList.enumerated() { |
| 85 | + let count = item["Count"]?.byte ?? 0 |
| 86 | + let id = item["id"]?.short ?? 0 |
| 87 | + |
| 88 | + print("Slot \(index): ID \(id) x\(count)") |
| 89 | + } |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +### 3. Extracting Values |
| 94 | + |
| 95 | +SwiftNBT provides computed properties to safely cast values. |
| 96 | + |
| 97 | +```swift |
| 98 | +let tag: NBTTag = ... |
| 99 | + |
| 100 | +// Safe casting (returns nil if type mismatches) |
| 101 | +let myInt = tag.int // Works for Byte, Short, and Int types |
| 102 | +let myDouble = tag.double // Works for Float and Double types |
| 103 | +let myString = tag.string |
| 104 | +``` |
| 105 | + |
| 106 | +--- |
| 107 | + |
| 108 | +## Features |
| 109 | + |
| 110 | +* **Auto-Decompression:** Automatically detects and handles GZip and ZLib headers using the native system `zlib`, ensuring 100% compatibility with Minecraft data (RFC 1952). |
| 111 | +* **Type Safety:** Built on a comprehensive `NBTTag` enum that strictly represents every Minecraft data type (Byte, Short, Int, Long, Float, Double, Arrays, Lists, Compounds). |
| 112 | +* **Syntactic Sugar:** Navigate complex NBT trees effortlessly using dictionary-like subscripts (e.g., `tag["display"]["Name"]`) and safe optional casting. |
| 113 | +* **Debug Tools:** Includes a handy `.printTree()` method to visualize the NBT structure hierarchy in the console. |
| 114 | +* **Lightweight:** Zero external dependencies (other than system libraries), keeping your build times fast and binary size small. |
| 115 | + |
| 116 | +--- |
| 117 | + |
| 118 | +## Contributing & Maintenance |
| 119 | + |
| 120 | +This library was built to provide a stable, long-term solution for the Swift Minecraft community. **It is here to stay and will be actively maintained.** |
| 121 | + |
| 122 | +We welcome contributions! |
| 123 | +* **Found a bug?** Please open an [Issue](https://github.com/arnaudrmt/SwiftNBT/issues). |
| 124 | +* **Have an improvement?** Feel free to fork the repo and submit a Pull Request. |
| 125 | + |
| 126 | +You don't have to worry about this library becoming deprecated or deleted; we depend on it for our own production apps. |
0 commit comments