some changes blob
This commit is contained in:
parent
e2cc99dfb7
commit
d39ee60729
84
README.md
84
README.md
@ -1,75 +1,52 @@
|
|||||||
# rust_mc_proto
|
# rust_mc_proto
|
||||||
lightweight minecraft packets protocol support in pure rust \
|
Lightweight minecraft packets protocol support in pure rust \
|
||||||
has compression (`MinecraftConnection::set_compression`) \
|
Has compression (`MinecraftConnection::set_compression`) \
|
||||||
all types of packets you can find on [wiki.vg](https://wiki.vg/) \
|
This crate can be used for a server on rust idk -_-
|
||||||
[crates](https://crates.io/crates/rust_mc_proto)
|
|
||||||
[github](https://github.com/MeexReay/rust_mc_proto)
|
|
||||||
|
|
||||||
## setup
|
## Setup
|
||||||
|
|
||||||
stable
|
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
rust_mc_proto = "0.1.16"
|
rust_mc_proto = "0.1.16" # stable version
|
||||||
|
rust_mc_proto = { git = "https://github.com/MeexReay/rust_mc_proto" } # unstable version
|
||||||
```
|
```
|
||||||
|
|
||||||
unstable
|
Features:
|
||||||
|
- `atomic_compression` (default)
|
||||||
|
|
||||||
```toml
|
## How to use
|
||||||
rust_mc_proto = { git = "https://github.com/MeexReay/rust_mc_proto" }
|
|
||||||
```
|
|
||||||
|
|
||||||
features:
|
For reference:
|
||||||
- atomic_compression (default)
|
|
||||||
|
|
||||||
## how to use it
|
|
||||||
|
|
||||||
for reference:
|
|
||||||
```rust
|
```rust
|
||||||
pub type MCConn<T> = MinecraftConnection<T>;
|
pub type MCConn<T> = MinecraftConnection<T>;
|
||||||
pub type MCConnTcp = MinecraftConnection<TcpStream>;
|
pub type MCConnTcp = MinecraftConnection<TcpStream>;
|
||||||
```
|
```
|
||||||
|
|
||||||
example of receiving motd:
|
Example of receiving motd:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
use rust_mc_proto::{Packet, ProtocolError, MCConnTcp, DataBufferReader, DataBufferWriter};
|
use rust_mc_proto::{Packet, ProtocolError, MCConnTcp, DataBufferReader, DataBufferWriter};
|
||||||
|
|
||||||
/*
|
fn send_handshake(
|
||||||
|
conn: &mut MCConnTcp,
|
||||||
Example of receiving motd from the server
|
protocol_version: u16,
|
||||||
Sends handshake, status request and receiving one
|
server_address: &str,
|
||||||
|
server_port: u16,
|
||||||
*/
|
next_state: u8
|
||||||
|
) -> Result<(), ProtocolError> {
|
||||||
fn send_handshake(conn: &mut MCConnTcp,
|
conn.write_packet(&Packet::build(0x00, |packet| {
|
||||||
protocol_version: u16,
|
packet.write_u16_varint(protocol_version)?;
|
||||||
server_address: &str,
|
packet.write_string(server_address)?;
|
||||||
server_port: u16,
|
packet.write_unsigned_short(server_port)?;
|
||||||
next_state: u8) -> Result<(), ProtocolError> {
|
packet.write_u8_varint(next_state)
|
||||||
let mut packet = Packet::empty(0x00);
|
})?)
|
||||||
|
|
||||||
packet.write_u16_varint(protocol_version)?;
|
|
||||||
packet.write_string(server_address)?;
|
|
||||||
packet.write_unsigned_short(server_port)?;
|
|
||||||
packet.write_u8_varint(next_state)?;
|
|
||||||
|
|
||||||
conn.write_packet(&packet)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn send_status_request(conn: &mut MCConnTcp) -> Result<(), ProtocolError> {
|
fn send_status_request(conn: &mut MCConnTcp) -> Result<(), ProtocolError> {
|
||||||
let packet = Packet::empty(0x00);
|
conn.write_packet(&Packet::empty(0x00))
|
||||||
conn.write_packet(&packet)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_status_response(conn: &mut MCConnTcp) -> Result<String, ProtocolError> {
|
fn read_status_response(conn: &mut MCConnTcp) -> Result<String, ProtocolError> {
|
||||||
let mut packet = conn.read_packet()?;
|
conn.read_packet()?.read_string()
|
||||||
|
|
||||||
packet.read_string()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@ -84,6 +61,11 @@ fn main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
[more examples](https://github.com/MeexReay/rust_mc_proto/tree/main/examples)
|
[More examples](https://github.com/MeexReay/rust_mc_proto/tree/main/examples)
|
||||||
|
|
||||||
this crate can be used for a server on rust idk -_-
|
### Contributing
|
||||||
|
|
||||||
|
If you would like to contribute to the project, feel free to fork the repository and submit a pull request.
|
||||||
|
|
||||||
|
### License
|
||||||
|
This project is licensed under the WTFPL License
|
@ -7,34 +7,27 @@ use rust_mc_proto::{Packet, ProtocolError, MCConnTcp, DataBufferReader, DataBuff
|
|||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
fn send_handshake(conn: &mut MCConnTcp,
|
fn send_handshake(
|
||||||
protocol_version: u16,
|
conn: &mut MCConnTcp,
|
||||||
server_address: &str,
|
protocol_version: u16,
|
||||||
server_port: u16,
|
server_address: &str,
|
||||||
next_state: u8) -> Result<(), ProtocolError> {
|
server_port: u16,
|
||||||
let mut packet = Packet::empty(0x00);
|
next_state: u8
|
||||||
|
) -> Result<(), ProtocolError> {
|
||||||
packet.write_u16_varint(protocol_version)?;
|
conn.write_packet(&Packet::build(0x00, |packet| {
|
||||||
packet.write_string(server_address)?;
|
packet.write_u16_varint(protocol_version)?;
|
||||||
packet.write_unsigned_short(server_port)?;
|
packet.write_string(server_address)?;
|
||||||
packet.write_u8_varint(next_state)?;
|
packet.write_unsigned_short(server_port)?;
|
||||||
|
packet.write_u8_varint(next_state)
|
||||||
conn.write_packet(&packet)?;
|
})?)
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn send_status_request(conn: &mut MCConnTcp) -> Result<(), ProtocolError> {
|
fn send_status_request(conn: &mut MCConnTcp) -> Result<(), ProtocolError> {
|
||||||
let packet = Packet::empty(0x00);
|
conn.write_packet(&Packet::empty(0x00))
|
||||||
conn.write_packet(&packet)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_status_response(conn: &mut MCConnTcp) -> Result<String, ProtocolError> {
|
fn read_status_response(conn: &mut MCConnTcp) -> Result<String, ProtocolError> {
|
||||||
let mut packet = conn.read_packet()?;
|
conn.read_packet()?.read_string()
|
||||||
|
|
||||||
packet.read_string()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -87,66 +87,66 @@ pub trait DataBufferReader {
|
|||||||
size_varint!(usize, self)
|
size_varint!(usize, self)
|
||||||
}
|
}
|
||||||
/// Read VarInt as u8 with size in bytes (varint, size)
|
/// Read VarInt as u8 with size in bytes (varint, size)
|
||||||
fn read_u8_varint_size(&mut self) -> Result<(u8, u8), ProtocolError> {
|
fn read_u8_varint_size(&mut self) -> Result<(u8, usize), ProtocolError> {
|
||||||
size_varint!(u8, self)
|
size_varint!(u8, self)
|
||||||
}
|
}
|
||||||
/// Read VarInt as u16 with size in bytes (varint, size)
|
/// Read VarInt as u16 with size in bytes (varint, size)
|
||||||
fn read_u16_varint_size(&mut self) -> Result<(u16, u16), ProtocolError> {
|
fn read_u16_varint_size(&mut self) -> Result<(u16, usize), ProtocolError> {
|
||||||
size_varint!(u16, self)
|
size_varint!(u16, self)
|
||||||
}
|
}
|
||||||
/// Read VarInt as u32 with size in bytes (varint, size)
|
/// Read VarInt as u32 with size in bytes (varint, size)
|
||||||
fn read_u32_varint_size(&mut self) -> Result<(u32, u32), ProtocolError> {
|
fn read_u32_varint_size(&mut self) -> Result<(u32, usize), ProtocolError> {
|
||||||
size_varint!(u32, self)
|
size_varint!(u32, self)
|
||||||
}
|
}
|
||||||
/// Read VarInt as u64 with size in bytes (varint, size)
|
/// Read VarInt as u64 with size in bytes (varint, size)
|
||||||
fn read_u64_varint_size(&mut self) -> Result<(u64, u64), ProtocolError> {
|
fn read_u64_varint_size(&mut self) -> Result<(u64, usize), ProtocolError> {
|
||||||
size_varint!(u64, self)
|
size_varint!(u64, self)
|
||||||
}
|
}
|
||||||
/// Read VarInt as u128 with size in bytes (varint, size)
|
/// Read VarInt as u128 with size in bytes (varint, size)
|
||||||
fn read_u128_varint_size(&mut self) -> Result<(u128, u128), ProtocolError> {
|
fn read_u128_varint_size(&mut self) -> Result<(u128, usize), ProtocolError> {
|
||||||
size_varint!(u128, self)
|
size_varint!(u128, self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Read VarInt as isize with size in bytes (varint, size)
|
/// Read VarInt as isize with size in bytes (varint, size)
|
||||||
fn read_isize_varint_size(&mut self) -> Result<(isize, isize), ProtocolError> {
|
fn read_isize_varint_size(&mut self) -> Result<(isize, usize), ProtocolError> {
|
||||||
Ok({
|
Ok({
|
||||||
let i = self.read_usize_varint_size()?;
|
let i = self.read_usize_varint_size()?;
|
||||||
(i.0.zigzag(), i.1.zigzag())
|
(i.0.zigzag(), i.1)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
/// Read VarInt as i8 with size in bytes (varint, size)
|
/// Read VarInt as i8 with size in bytes (varint, size)
|
||||||
fn read_i8_varint_size(&mut self) -> Result<(i8, i8), ProtocolError> {
|
fn read_i8_varint_size(&mut self) -> Result<(i8, usize), ProtocolError> {
|
||||||
Ok({
|
Ok({
|
||||||
let i = self.read_u8_varint_size()?;
|
let i = self.read_u8_varint_size()?;
|
||||||
(i.0.zigzag(), i.1.zigzag())
|
(i.0.zigzag(), i.1)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
/// Read VarInt as i16 with size in bytes (varint, size)
|
/// Read VarInt as i16 with size in bytes (varint, size)
|
||||||
fn read_i16_varint_size(&mut self) -> Result<(i16, i16), ProtocolError> {
|
fn read_i16_varint_size(&mut self) -> Result<(i16, usize), ProtocolError> {
|
||||||
Ok({
|
Ok({
|
||||||
let i = self.read_u16_varint_size()?;
|
let i = self.read_u16_varint_size()?;
|
||||||
(i.0.zigzag(), i.1.zigzag())
|
(i.0.zigzag(), i.1)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
/// Read VarInt as i32 with size in bytes (varint, size)
|
/// Read VarInt as i32 with size in bytes (varint, size)
|
||||||
fn read_i32_varint_size(&mut self) -> Result<(i32, i32), ProtocolError> {
|
fn read_i32_varint_size(&mut self) -> Result<(i32, usize), ProtocolError> {
|
||||||
Ok({
|
Ok({
|
||||||
let i = self.read_u32_varint_size()?;
|
let i = self.read_u32_varint_size()?;
|
||||||
(i.0.zigzag(), i.1.zigzag())
|
(i.0.zigzag(), i.1)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
/// Read VarInt as i64 with size in bytes (varint, size)
|
/// Read VarInt as i64 with size in bytes (varint, size)
|
||||||
fn read_i64_varint_size(&mut self) -> Result<(i64, i64), ProtocolError> {
|
fn read_i64_varint_size(&mut self) -> Result<(i64, usize), ProtocolError> {
|
||||||
Ok({
|
Ok({
|
||||||
let i = self.read_u64_varint_size()?;
|
let i = self.read_u64_varint_size()?;
|
||||||
(i.0.zigzag(), i.1.zigzag())
|
(i.0.zigzag(), i.1)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
/// Read VarInt as i128 with size in bytes (varint, size)
|
/// Read VarInt as i128 with size in bytes (varint, size)
|
||||||
fn read_i128_varint_size(&mut self) -> Result<(i128, i128), ProtocolError> {
|
fn read_i128_varint_size(&mut self) -> Result<(i128, usize), ProtocolError> {
|
||||||
Ok({
|
Ok({
|
||||||
let i = self.read_u128_varint_size()?;
|
let i = self.read_u128_varint_size()?;
|
||||||
(i.0.zigzag(), i.1.zigzag())
|
(i.0.zigzag(), i.1)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@ macro_rules! size_varint {
|
|||||||
($type:ty, $self:expr) => {{
|
($type:ty, $self:expr) => {{
|
||||||
let mut shift: $type = 0;
|
let mut shift: $type = 0;
|
||||||
let mut decoded: $type = 0;
|
let mut decoded: $type = 0;
|
||||||
let mut size: $type = 0;
|
let mut size: usize = 0;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let next = DataBufferReader::read_byte($self).or(Err(ProtocolError::VarIntError))?;
|
let next = DataBufferReader::read_byte($self).or(Err(ProtocolError::VarIntError))?;
|
||||||
|
Loading…
Reference in New Issue
Block a user