Repository navigation
Expand file tree
/
Copy pathlib.rs
More file actions
139 lines (126 loc) · 5.35 KB
/
Copy pathlib.rs
File metadata and controls
139 lines (126 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//! # `rad-core`
//!
//! Core zero-copy Minecraft protocol, packet framing, and shared primitives for the
//! **RadByte** ecosystem (`RadProxy`, `RadLimbo`, `RadServer`).
//!
//! ## Architecture & Extensibility
//! - [`types`] — Primitive wire types (`VarInt`, `VarLong`, strings, `Uuid`) and the
//! [`Decode`](types::Decode) / [`Encode`](types::Encode) traits over [`bytes::Buf`] / [`bytes::BufMut`].
//! - [`codec`] — Zero-copy packet framing ([`RawPacket`](codec::RawPacket), [`decode_frame`](codec::decode_frame),
//! [`encode_packet`](codec::encode_packet)).
//! - [`packet`] — Strongly-typed packets grouped by [`ConnectionState`](state::ConnectionState) via the
//! [`Packet`](packet::Packet) trait. Easy to extend with custom or new version packets.
//! - [`text`] — Ergonomic builder for Minecraft JSON [`TextComponent`](text::TextComponent).
//! - [`state`] — Protocol [`ConnectionState`](state::ConnectionState) enum and handshake transitions.
//! - [`error`] — Unified [`Error`](error::Error) and [`Result`](error::Result) types.
//!
//! ## Quick Example
//! ```
//! use bytes::BytesMut;
//! use rad_core::prelude::*;
//! use rad_core::packet::handshake::Handshake;
//!
//! let mut buf = BytesMut::new();
//! let hs = Handshake {
//! protocol_version: VarInt(PROTOCOL_VERSION),
//! server_address: "play.radbyte.xyz".into(),
//! server_port: 25565,
//! next_state: ConnectionState::Status,
//! };
//!
//! encode_packet(&hs, &mut buf).unwrap();
//!
//! let frame = decode_frame(&mut buf, DEFAULT_MAX_PACKET_SIZE).unwrap().unwrap();
//! let decoded: Handshake = frame.decode_as().unwrap();
//! assert_eq!(decoded, hs);
//! ```
#![warn(missing_docs)]
/// Target Minecraft Java Edition version string.
pub const MINECRAFT_VERSION: &str = "26.3";
/// Target Minecraft Java Edition network protocol version (`26.3`).
pub const PROTOCOL_VERSION: i32 = 777;
pub mod app;
pub mod client;
pub mod packet;
pub mod protocol;
pub mod registry_provider;
pub mod security;
pub mod version_package;
pub use app::{api_runtime, auto_update, cli, console_ui, error, i18n, radon, setup_ui, text};
pub use protocol::{codec, nbt, state, types, version};
pub use security::{crypto, forwarding};
pub use version::GameVersion;
/// Convenient re-exports of the most commonly used `rad-core` types and functions.
pub mod prelude {
pub use crate::codec::{
CompressionConfig, DEFAULT_MAX_PACKET_SIZE, DEFAULT_MAX_UNCOMPRESSED_SIZE, RawPacket,
decode_frame, decode_frame_with_compression, encode_frame, encode_frame_with_compression,
encode_frame_with_compression_level, encode_packet, encode_packet_with_compression,
};
pub use crate::crypto::{MinecraftCipher, offline_player_uuid};
pub use crate::error::{Error, Result};
pub use crate::forwarding::{
ForwardingData, VELOCITY_FORWARDING_CHANNEL, decode_velocity_forwarding,
encode_velocity_forwarding,
};
pub use crate::nbt::{NbtTag, decode_network_nbt, encode_network_nbt};
pub use crate::packet::{Packet, PacketDirection};
pub use crate::radon::{RadonManifest, RadonTarget};
pub use crate::state::ConnectionState;
pub use crate::text::TextComponent;
pub use crate::types::{BlockPos, Decode, Encode, VarInt, VarLong, read_string, write_string};
pub use crate::version::{GameVersion, ProtocolVersion};
pub use crate::{MINECRAFT_VERSION, PROTOCOL_VERSION};
}
/// Loads a PNG server icon from `path_str` and returns a `data:image/png;base64,...` URI if present.
#[must_use]
pub fn load_favicon_data_uri(path_str: &str) -> Option<String> {
use base64::{Engine as _, engine::general_purpose::STANDARD};
let trimmed = path_str.trim();
if trimmed.is_empty() {
return None;
}
let path = std::path::Path::new(trimmed);
if !path.is_file() {
return None;
}
let bytes = std::fs::read(path).ok()?;
Some(format!("data:image/png;base64,{}", STANDARD.encode(bytes)))
}
/// Enables Linux TCP Fast Open (`TCP_FASTOPEN`) on a listening TCP socket descriptor.
#[cfg(target_os = "linux")]
pub fn enable_tcp_fastopen(fd: std::os::fd::BorrowedFd<'_>) {
let qlen: libc::c_int = 128;
// SAFETY: `fd` is a valid borrowed socket descriptor and `qlen` is a valid `c_int` option value.
unsafe {
let _ = libc::setsockopt(
std::os::fd::AsRawFd::as_raw_fd(&fd),
libc::IPPROTO_TCP,
libc::TCP_FASTOPEN,
(&raw const qlen).cast::<libc::c_void>(),
std::mem::size_of::<libc::c_int>() as libc::socklen_t,
);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn load_favicon_data_uri_handles_missing_empty_and_valid_file() {
assert_eq!(load_favicon_data_uri(""), None);
assert_eq!(load_favicon_data_uri(" "), None);
assert_eq!(
load_favicon_data_uri("nonexistent_radbyte_favicon_12345.png"),
None
);
let dir = std::env::temp_dir().join("radbyte_favicon_test");
let _ = std::fs::create_dir_all(&dir);
let file_path = dir.join("icon.png");
let bytes = [0x89u8, b'P', b'N', b'G', 0x0d, 0x0a, 0x1a, 0x0a];
std::fs::write(&file_path, bytes).unwrap();
let uri = load_favicon_data_uri(file_path.to_str().unwrap()).unwrap();
assert!(uri.starts_with("data:image/png;base64,"));
let _ = std::fs::remove_file(&file_path);
let _ = std::fs::remove_dir(&dir);
}
}