Skip to content

Commit e5bd066

Browse files
committed
added readme file
1 parent facb50d commit e5bd066

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed

.github/assets/logo_dark.png

21 KB
Loading

.github/assets/logo_light.png

21.3 KB
Loading

README.md

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
![banner](.github/assets/logo_dark.png#gh-dark-mode-only)
2+
![banner](.github/assets/logo_light.png#gh-light-mode-only)
3+
4+
# Paklet
5+
6+
[![license](https://img.shields.io/github/license/machinemc/paklet?style=for-the-badge&color=657185)](LICENSE)
7+
![release](https://img.shields.io/github/v/release/machinemc/paklet?style=for-the-badge&color=edb228)
8+
9+
Paklet is annotation based Java library for simple and fast packet crafting.
10+
11+
# Table of contents
12+
* [Features](#features)
13+
* [Usage](#usage)
14+
* [Creating a packet class](#creating-a-packet-class)
15+
* [Custom Serializer](#custom-serializer)
16+
* [Packet Crafting](#packet-crafting)
17+
* [Importing](#importing)
18+
* [License](#license)
19+
20+
### Features
21+
* Simplicity – The annotation system is straightforward and easy to use
22+
* Flexibility – You can individually customize the serialization of each packet field or even the whole packet
23+
* Expandable – Paklet can be easily extended to automate the serialization of custom types.
24+
* Speed – Bytecode manipulation in the background ensures the packet serialization and construction is fast
25+
26+
### Usage
27+
28+
#### Creating a packet class
29+
30+
All classes annotated with `Packet` are considered packets. All their fields, if they are not static, transient, or annotated with `@Ignore`
31+
are automatically serialized by default serializers if not specified otherwise with `@SerializeWith` annotation.
32+
```java
33+
@Packet(0x00)
34+
public class TestPacket {
35+
36+
private String[] data;
37+
38+
}
39+
```
40+
Together with `@Ignore` and `@SerializeWith`, Paklet offers one more modifier `@Optional` for nullable fields.
41+
Default serializers can be found in `Serializers` class.
42+
43+
Some types can be annotated with additional metadata, Paklet offers `@Length` and `@FixedLength` supported for
44+
String Bitset, Collection, and array types.
45+
46+
Both modifiers and metadata can be also used for parameters e.g. `List<@SerializeWith(VarLongSerializer.class) Long>`.
47+
48+
> [!NOTE]
49+
> Paklet also offers `@VarIntSerializer` and `@VarLongSerializer` compatible with the Minecraft Java Protocol.
50+
51+
For more examples see tests of the `paklet-core` module.
52+
53+
#### Custom Serializer
54+
55+
Paklet allows simple way of creating custom serializers. To create a custom serializer, implement `Serializer<T>`.
56+
57+
```java
58+
@Supports({Integer.class, int.class})
59+
public class MyCustomSerializer implements Serializer<Integer> {
60+
61+
@Override
62+
public void serialize(DataVisitor visitor, Integer value) {
63+
// serialization
64+
}
65+
66+
@Override
67+
public Integer deserialize(DataVisitor visitor) {
68+
// deserialization
69+
}
70+
71+
}
72+
```
73+
74+
`@Supports` annotation can specify which types the serializer supports, if more complex rule for choosing the types is needed
75+
(e.g. array types), the array can stay empty and custom `SerializationRule` needs to be implemented.
76+
77+
All serializers annotated with `@DefaultSerializer` are automatically registered.
78+
79+
Serialization context of current field can be accessed using `Serializer.context()`.
80+
81+
For more examples see `Serializers` class with default serializers provided by Paklet.
82+
83+
#### Packet Crafting
84+
85+
To read and write packets, instance of `PacketFactory` is required. The default one is provided in the core Paklet module in
86+
a form of `PacketFactoryBuilder`. To create an instance, you need to provide serializer provider and serializer that will be used
87+
to prefix packet length.
88+
89+
```java
90+
SerializerProvider serializerProvider = SerializerProviderBuilder.create().loadProvided().loadDefaults().build();
91+
PacketFactory packetFactory = PacketFactoryBuilder.create(new Serializers.Integer(), serializerProvider).loadDefaults().build();
92+
```
93+
94+
`SerializerProviderBuilder` allows you to simply load all serializers annotated with `@DefaultSerializer` and default serializers provided
95+
by Paklet. `PacketFactoryBuilder` then takes the created serializer provider, integer serializer for prefixing packet length and can
96+
automatically load all the classes marked with `@Packet`. Both serializer provider and packet factory builder then allows individual
97+
registration of packets, serializers, and serialization rules. Serializers that have no argument constructor do not have to be registered
98+
and will be automatically resolved during runtime.
99+
100+
For more examples see tests of the `paklet-core` module.
101+
102+
### Importing
103+
104+
#### API and Annotation Processor
105+
```kotlin
106+
repositories {
107+
maven {
108+
url = uri("http://www.machinemc.org/releases")
109+
isAllowInsecureProtocol = true
110+
}
111+
}
112+
113+
implementation("org.machinemc:paklet-api:VERSION")
114+
annotationProcessor("org.machinemc:paklet-processor:VERSION")
115+
```
116+
#### Implementation
117+
```kotlin
118+
implementation("org.machinemc:paklet-core:VERSION")
119+
```
120+
121+
#### Gradle Plugin
122+
> [!NOTE]
123+
> Gradle plugin is a key feature of Paklet. It modifies bytecode of compiled packet classes by adding hidden getters and setters which are later
124+
> used by generated packet readers and writers to ensure no reflection is used during the serialization to achieve higher speeds.
125+
> If unused, the packet serialization will be noticeably slower.
126+
```kotlin
127+
buildscript {
128+
repositories {
129+
maven {
130+
url = uri("http://www.machinemc.org/releases")
131+
isAllowInsecureProtocol = true
132+
}
133+
}
134+
dependencies {
135+
classpath("org.machinemc:paklet-plugin:VERSION")
136+
}
137+
}
138+
139+
apply<PakletPlugin>()
140+
```
141+
142+
### License
143+
Paklet is free software licensed under the [MIT license](LICENSE).

0 commit comments

Comments
 (0)