@sanctumterra/raknet 1.2.71 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (204) hide show
  1. package/.github/workflows/publish.yml +84 -0
  2. package/.gitignore +7 -0
  3. package/Cargo.toml +13 -0
  4. package/package.json +47 -13
  5. package/src/binarystream/binarystream.rs +313 -0
  6. package/src/binarystream/endianess.rs +5 -0
  7. package/src/binarystream/mod.rs +6 -0
  8. package/src/client/client.rs +201 -0
  9. package/src/client/framer.rs +391 -0
  10. package/src/client/mod.rs +5 -0
  11. package/src/lib.rs +68 -0
  12. package/src/main.rs +46 -0
  13. package/src/packets/ack.rs +82 -0
  14. package/src/packets/connected_ping.rs +27 -0
  15. package/src/packets/connected_pong.rs +29 -0
  16. package/src/packets/connection_request.rs +34 -0
  17. package/src/packets/connection_request_accepted.rs +55 -0
  18. package/src/packets/frameset.rs +40 -0
  19. package/src/packets/mod.rs +34 -0
  20. package/src/packets/nack.rs +82 -0
  21. package/src/packets/new_incomming_connection.rs +49 -0
  22. package/src/packets/open_connection_reply_one.rs +48 -0
  23. package/src/packets/open_connection_reply_two.rs +43 -0
  24. package/src/packets/open_connection_request_one.rs +56 -0
  25. package/src/packets/open_connection_request_two.rs +34 -0
  26. package/src/packets/packet.rs +45 -0
  27. package/src/packets/packet_types.rs +62 -0
  28. package/src/packets/types/address.rs +75 -0
  29. package/src/packets/types/data_type.rs +6 -0
  30. package/src/packets/types/flags.rs +6 -0
  31. package/src/packets/types/frame.rs +74 -0
  32. package/src/packets/types/mod.rs +14 -0
  33. package/src/packets/types/priority.rs +5 -0
  34. package/src/packets/types/reliability.rs +45 -0
  35. package/src/packets/unconnected_ping.rs +41 -0
  36. package/src/socket/mod.rs +3 -0
  37. package/src/socket/socket.rs +60 -0
  38. package/src_client/client/client-events.ts +38 -0
  39. package/src_client/client/client.ts +146 -0
  40. package/src_client/client/client_options.ts +17 -0
  41. package/src_client/client/index.ts +3 -0
  42. package/src_client/index.ts +4 -0
  43. package/src_client/proto/decorators/create.ts +87 -0
  44. package/{dist/proto/decorators/index.d.ts → src_client/proto/decorators/index.ts} +0 -1
  45. package/src_client/proto/decorators/serialize.ts +17 -0
  46. package/src_client/proto/enums/flags.ts +6 -0
  47. package/{dist/proto/enums/index.d.ts → src_client/proto/enums/index.ts} +0 -1
  48. package/src_client/proto/enums/packet.ts +121 -0
  49. package/src_client/proto/enums/priority.ts +4 -0
  50. package/src_client/proto/enums/reliability.ts +10 -0
  51. package/{dist/proto/index.d.ts → src_client/proto/index.ts} +0 -1
  52. package/src_client/proto/packets/ack.ts +82 -0
  53. package/src_client/proto/packets/base-packet.ts +38 -0
  54. package/src_client/proto/packets/connected-ping.ts +9 -0
  55. package/src_client/proto/packets/connected-pong.ts +10 -0
  56. package/src_client/proto/packets/connection-request-accepted.ts +14 -0
  57. package/src_client/proto/packets/connection-request.ts +11 -0
  58. package/src_client/proto/packets/frameset.ts +15 -0
  59. package/{dist/proto/packets/index.d.ts → src_client/proto/packets/index.ts} +1 -1
  60. package/src_client/proto/packets/nack.ts +84 -0
  61. package/src_client/proto/packets/new-incoming-connection.ts +14 -0
  62. package/src_client/proto/packets/open-connection-reply-one.ts +22 -0
  63. package/src_client/proto/packets/open-connection-reply-two.ts +14 -0
  64. package/src_client/proto/packets/open-connection-request-one.ts +12 -0
  65. package/src_client/proto/packets/open-connection-request-two.ts +13 -0
  66. package/src_client/proto/packets/types/address.ts +106 -0
  67. package/src_client/proto/packets/types/data-type.ts +38 -0
  68. package/src_client/proto/packets/types/frame.ts +114 -0
  69. package/{dist/proto/packets/types/index.d.ts → src_client/proto/packets/types/index.ts} +0 -1
  70. package/src_client/proto/packets/types/magic.ts +14 -0
  71. package/src_client/proto/packets/types/mtu.ts +19 -0
  72. package/src_client/proto/packets/types/sys-address.ts +35 -0
  73. package/src_client/proto/packets/unconnected-ping.ts +12 -0
  74. package/src_client/proto/packets/unconnected-pong.ts +13 -0
  75. package/src_client/proto/types/advertisement.ts +40 -0
  76. package/{dist/proto/types/index.d.ts → src_client/proto/types/index.ts} +0 -1
  77. package/src_client/proto/types/valid.ts +58 -0
  78. package/tsconfig.json +19 -14
  79. package/dist/client/client-events.d.ts +0 -21
  80. package/dist/client/client-events.d.ts.map +0 -1
  81. package/dist/client/client-events.js +0 -2
  82. package/dist/client/client-options.d.ts +0 -12
  83. package/dist/client/client-options.d.ts.map +0 -1
  84. package/dist/client/client-options.js +0 -13
  85. package/dist/client/client.d.ts +0 -28
  86. package/dist/client/client.d.ts.map +0 -1
  87. package/dist/client/client.js +0 -223
  88. package/dist/client/framer.d.ts +0 -38
  89. package/dist/client/framer.d.ts.map +0 -1
  90. package/dist/client/framer.js +0 -311
  91. package/dist/client/index.d.ts +0 -4
  92. package/dist/client/index.d.ts.map +0 -1
  93. package/dist/client/index.js +0 -19
  94. package/dist/index.d.ts +0 -5
  95. package/dist/index.d.ts.map +0 -1
  96. package/dist/index.js +0 -20
  97. package/dist/proto/decorators/create.d.ts +0 -8
  98. package/dist/proto/decorators/create.d.ts.map +0 -1
  99. package/dist/proto/decorators/create.js +0 -79
  100. package/dist/proto/decorators/index.d.ts.map +0 -1
  101. package/dist/proto/decorators/index.js +0 -18
  102. package/dist/proto/decorators/serialize.d.ts +0 -4
  103. package/dist/proto/decorators/serialize.d.ts.map +0 -1
  104. package/dist/proto/decorators/serialize.js +0 -12
  105. package/dist/proto/enums/flags.d.ts +0 -7
  106. package/dist/proto/enums/flags.d.ts.map +0 -1
  107. package/dist/proto/enums/flags.js +0 -10
  108. package/dist/proto/enums/index.d.ts.map +0 -1
  109. package/dist/proto/enums/index.js +0 -20
  110. package/dist/proto/enums/packet.d.ts +0 -122
  111. package/dist/proto/enums/packet.d.ts.map +0 -1
  112. package/dist/proto/enums/packet.js +0 -125
  113. package/dist/proto/enums/priority.d.ts +0 -5
  114. package/dist/proto/enums/priority.d.ts.map +0 -1
  115. package/dist/proto/enums/priority.js +0 -8
  116. package/dist/proto/enums/reliability.d.ts +0 -11
  117. package/dist/proto/enums/reliability.d.ts.map +0 -1
  118. package/dist/proto/enums/reliability.js +0 -14
  119. package/dist/proto/index.d.ts.map +0 -1
  120. package/dist/proto/index.js +0 -20
  121. package/dist/proto/packets/ack.d.ts +0 -10
  122. package/dist/proto/packets/ack.d.ts.map +0 -1
  123. package/dist/proto/packets/ack.js +0 -89
  124. package/dist/proto/packets/base-packet.d.ts +0 -26
  125. package/dist/proto/packets/base-packet.d.ts.map +0 -1
  126. package/dist/proto/packets/base-packet.js +0 -37
  127. package/dist/proto/packets/connected-ping.d.ts +0 -5
  128. package/dist/proto/packets/connected-ping.d.ts.map +0 -1
  129. package/dist/proto/packets/connected-ping.js +0 -27
  130. package/dist/proto/packets/connected-pong.d.ts +0 -6
  131. package/dist/proto/packets/connected-pong.d.ts.map +0 -1
  132. package/dist/proto/packets/connected-pong.js +0 -32
  133. package/dist/proto/packets/connection-request-accepted.d.ts +0 -10
  134. package/dist/proto/packets/connection-request-accepted.d.ts.map +0 -1
  135. package/dist/proto/packets/connection-request-accepted.js +0 -48
  136. package/dist/proto/packets/connection-request.d.ts +0 -7
  137. package/dist/proto/packets/connection-request.d.ts.map +0 -1
  138. package/dist/proto/packets/connection-request.js +0 -37
  139. package/dist/proto/packets/frameset.d.ts +0 -11
  140. package/dist/proto/packets/frameset.d.ts.map +0 -1
  141. package/dist/proto/packets/frameset.js +0 -37
  142. package/dist/proto/packets/index.d.ts.map +0 -1
  143. package/dist/proto/packets/index.js +0 -31
  144. package/dist/proto/packets/nack.d.ts +0 -8
  145. package/dist/proto/packets/nack.d.ts.map +0 -1
  146. package/dist/proto/packets/nack.js +0 -88
  147. package/dist/proto/packets/new-incoming-connection.d.ts +0 -9
  148. package/dist/proto/packets/new-incoming-connection.d.ts.map +0 -1
  149. package/dist/proto/packets/new-incoming-connection.js +0 -43
  150. package/dist/proto/packets/open-connection-reply-one.d.ts +0 -8
  151. package/dist/proto/packets/open-connection-reply-one.d.ts.map +0 -1
  152. package/dist/proto/packets/open-connection-reply-one.js +0 -43
  153. package/dist/proto/packets/open-connection-reply-two.d.ts +0 -10
  154. package/dist/proto/packets/open-connection-reply-two.d.ts.map +0 -1
  155. package/dist/proto/packets/open-connection-reply-two.js +0 -48
  156. package/dist/proto/packets/open-connection-request-one.d.ts +0 -7
  157. package/dist/proto/packets/open-connection-request-one.d.ts.map +0 -1
  158. package/dist/proto/packets/open-connection-request-one.js +0 -38
  159. package/dist/proto/packets/open-connection-request-two.d.ts +0 -9
  160. package/dist/proto/packets/open-connection-request-two.d.ts.map +0 -1
  161. package/dist/proto/packets/open-connection-request-two.js +0 -43
  162. package/dist/proto/packets/types/address.d.ts +0 -48
  163. package/dist/proto/packets/types/address.d.ts.map +0 -1
  164. package/dist/proto/packets/types/address.js +0 -99
  165. package/dist/proto/packets/types/data-type.d.ts +0 -21
  166. package/dist/proto/packets/types/data-type.d.ts.map +0 -1
  167. package/dist/proto/packets/types/data-type.js +0 -29
  168. package/dist/proto/packets/types/frame.d.ts +0 -23
  169. package/dist/proto/packets/types/frame.d.ts.map +0 -1
  170. package/dist/proto/packets/types/frame.js +0 -94
  171. package/dist/proto/packets/types/index.d.ts.map +0 -1
  172. package/dist/proto/packets/types/index.js +0 -22
  173. package/dist/proto/packets/types/magic.d.ts +0 -7
  174. package/dist/proto/packets/types/magic.d.ts.map +0 -1
  175. package/dist/proto/packets/types/magic.js +0 -14
  176. package/dist/proto/packets/types/mtu.d.ts +0 -9
  177. package/dist/proto/packets/types/mtu.d.ts.map +0 -1
  178. package/dist/proto/packets/types/mtu.js +0 -18
  179. package/dist/proto/packets/types/sys-address.d.ts +0 -11
  180. package/dist/proto/packets/types/sys-address.d.ts.map +0 -1
  181. package/dist/proto/packets/types/sys-address.js +0 -32
  182. package/dist/proto/packets/unconnected-ping.d.ts +0 -7
  183. package/dist/proto/packets/unconnected-ping.d.ts.map +0 -1
  184. package/dist/proto/packets/unconnected-ping.js +0 -38
  185. package/dist/proto/packets/unconnected-pong.d.ts +0 -8
  186. package/dist/proto/packets/unconnected-pong.d.ts.map +0 -1
  187. package/dist/proto/packets/unconnected-pong.js +0 -43
  188. package/dist/proto/types/advertisement.d.ts +0 -15
  189. package/dist/proto/types/advertisement.d.ts.map +0 -1
  190. package/dist/proto/types/advertisement.js +0 -17
  191. package/dist/proto/types/index.d.ts.map +0 -1
  192. package/dist/proto/types/index.js +0 -18
  193. package/dist/proto/types/valid.d.ts +0 -4
  194. package/dist/proto/types/valid.d.ts.map +0 -1
  195. package/dist/proto/types/valid.js +0 -2
  196. package/dist/tests/test.d.ts +0 -2
  197. package/dist/tests/test.d.ts.map +0 -1
  198. package/dist/tests/test.js +0 -49
  199. package/dist/utils/Logger.d.ts +0 -14
  200. package/dist/utils/Logger.d.ts.map +0 -1
  201. package/dist/utils/Logger.js +0 -98
  202. package/dist/utils/index.d.ts +0 -2
  203. package/dist/utils/index.d.ts.map +0 -1
  204. package/dist/utils/index.js +0 -17
@@ -0,0 +1,84 @@
1
+ name: Build & Publish
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - develop
7
+ - main
8
+
9
+ jobs:
10
+ build:
11
+ needs: [lint]
12
+ permissions:
13
+ id-token: write
14
+ contents: read
15
+ runs-on: ubuntu-latest
16
+ strategy:
17
+ matrix:
18
+ os: [ubuntu-latest, windows-latest, macos-latest]
19
+
20
+ steps:
21
+ - name: Checkout repository
22
+ uses: actions/checkout@v4
23
+
24
+ - name: Setup Rust toolchain
25
+ uses: actions-rs/toolchain@v1
26
+ with:
27
+ toolchain: stable
28
+ override: true
29
+
30
+ - name: Install Rust dependencies
31
+ run: |
32
+ rustup target add x86_64-pc-windows-msvc
33
+ rustup target add x86_64-apple-darwin
34
+ rustup target add aarch64-apple-darwin
35
+ rustup target add x86_64-unknown-linux-gnu
36
+ rustup target add aarch64-unknown-linux-gnu
37
+ rustup target add aarch64-linux-android
38
+ rustup target add x86_64-linux-android
39
+
40
+ - name: Set up Node.js
41
+ uses: actions/setup-node@v4
42
+ with:
43
+ node-version: '20.x'
44
+ registry-url: 'https://registry.npmjs.org'
45
+
46
+ - name: Install dependencies
47
+ run: npm install
48
+
49
+ - name: NAPI build
50
+ run: npm run build:rust
51
+
52
+ - name: TypeScript build
53
+ run: npm run build:ts
54
+
55
+ lint:
56
+ runs-on: ubuntu-latest
57
+ steps:
58
+ - uses: actions/checkout@v4
59
+ - name: Set up Node.js for linting
60
+ uses: actions/setup-node@v4
61
+ with:
62
+ node-version: '20.x'
63
+ - name: Install dependencies for linting
64
+ run: npm install
65
+ - name: Run lint
66
+ run: npm run lint
67
+
68
+ publish:
69
+ needs: build # Ensures this job runs after the build job completes successfully.
70
+ runs-on: ubuntu-latest
71
+ steps:
72
+ - name: Checkout repository for publishing
73
+ uses: actions/checkout@v4
74
+
75
+ - name: Set up Node.js for publishing
76
+ uses: actions/setup-node@v4
77
+ with:
78
+ node-version: '20.x'
79
+ registry-url: 'https://registry.npmjs.org'
80
+
81
+ - name: Publish package
82
+ run: npm publish --access=public
83
+ env:
84
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
package/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ /node_modules
2
+ /target
3
+ /build
4
+ test.ts
5
+ Cargo.lock
6
+ package-lock.json
7
+ dist/
package/Cargo.toml ADDED
@@ -0,0 +1,13 @@
1
+ [package]
2
+ name = "raknet"
3
+ version = "0.1.0"
4
+ edition = "2021"
5
+
6
+ [lib]
7
+ crate-type = ["cdylib"]
8
+
9
+ [dependencies]
10
+ chrono = "0.4.38"
11
+ rand = "0.8.5"
12
+ napi = "2"
13
+ napi-derive = "2"
package/package.json CHANGED
@@ -1,25 +1,59 @@
1
1
  {
2
2
  "name": "@sanctumterra/raknet",
3
- "version": "1.2.71",
4
- "description": "",
3
+ "version": "1.3.1",
5
4
  "main": "dist/index.js",
5
+ "types": "dist/index.d.ts",
6
6
  "scripts": {
7
- "build": "tsc",
8
- "format": "npx @biomejs/biome format ./src",
9
- "lint": "npx @biomejs/biome lint ./src",
10
- "lint-write": "npx @biomejs/biome lint --write ./src",
11
- "format-write": "npx @biomejs/biome format --write ./src"
7
+ "build:rust": "napi build --platform --release build",
8
+ "build:ts": "tsc",
9
+ "dev": "npm run build:rust && npm run build:ts",
10
+ "format": "npx @biomejs/biome format ./src_client",
11
+ "lint": "npx @biomejs/biome lint ./src_client",
12
+ "lint:write": "npx @biomejs/biome lint --write ./src_client",
13
+ "format:write": "npx @biomejs/biome format --write ./src_client",
14
+ "test": "echo \"Error: no test specified\" && exit 1"
12
15
  },
13
- "keywords": [],
16
+ "keywords": [
17
+ "raknet",
18
+ "napi",
19
+ "rust",
20
+ "native"
21
+ ],
14
22
  "author": "",
15
23
  "license": "ISC",
16
- "dependencies": {
17
- "reflect-metadata": "^0.2.2",
18
- "@serenityjs/emitter": "^0.5.2",
19
- "@serenityjs/binarystream": "^2.6.6"
24
+ "description": "RakNet implementation in Rust with Node.js bindings",
25
+ "napi": {
26
+ "name": "raknet",
27
+ "triples": {
28
+ "defaults": true,
29
+ "additional": [
30
+ "x86_64-pc-windows-msvc",
31
+ "x86_64-apple-darwin",
32
+ "aarch64-apple-darwin",
33
+ "x86_64-unknown-linux-gnu",
34
+ "aarch64-unknown-linux-gnu"
35
+ ]
36
+ }
20
37
  },
21
38
  "devDependencies": {
22
39
  "@biomejs/biome": "^1.9.4",
23
- "@types/node": "^22.8.0"
40
+ "@napi-rs/cli": "^2.18.4",
41
+ "@types/node": "^22.9.0",
42
+ "typescript": "^5.6.3"
43
+ },
44
+ "engines": {
45
+ "node": ">= 20"
46
+ },
47
+ "files": [
48
+ "index.js",
49
+ "index.d.ts",
50
+ "build",
51
+ "dist",
52
+ ""
53
+ ],
54
+ "dependencies": {
55
+ "@serenityjs/binarystream": "^2.6.6",
56
+ "@serenityjs/emitter": "^0.5.2",
57
+ "reflect-metadata": "^0.2.2"
24
58
  }
25
59
  }
@@ -0,0 +1,313 @@
1
+ use std::error::Error;
2
+
3
+ use super::Endianness;
4
+
5
+ #[derive(Debug)]
6
+ pub enum BinaryStreamError {
7
+ OutOfBounds,
8
+ InvalidLength,
9
+ }
10
+
11
+ impl std::fmt::Display for BinaryStreamError {
12
+ fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
13
+ write!(f, "{:?}", self)
14
+ }
15
+ }
16
+
17
+ impl Error for BinaryStreamError {}
18
+
19
+ #[derive(Debug)]
20
+ pub struct BinaryStream {
21
+ pub binary: Vec<u8>,
22
+ offset: u32
23
+ }
24
+
25
+ impl BinaryStream {
26
+ pub fn new(binary: Option<Vec<u8>>, offset: Option<u32>) -> Self {
27
+ let bin = match binary {
28
+ Some(buffer) => buffer,
29
+ None => vec![],
30
+ };
31
+
32
+ let offset = match offset {
33
+ Some(offset) => offset,
34
+ None => 0,
35
+ };
36
+
37
+ BinaryStream {
38
+ binary: bin,
39
+ offset,
40
+ }
41
+ }
42
+
43
+ pub fn from(binary: Vec<u8>, offset: Option<u32>) -> Self {
44
+ let offset = match offset {
45
+ Some(offset) => offset,
46
+ None => 0,
47
+ };
48
+
49
+ BinaryStream {
50
+ binary: binary,
51
+ offset,
52
+ }
53
+ }
54
+
55
+ pub fn read(&mut self, length: u32) -> Result<Vec<u8>, BinaryStreamError> {
56
+ if length > self.binary.len() as u32 {
57
+ return Err(BinaryStreamError::InvalidLength);
58
+ }
59
+
60
+ if self.offset + length > self.binary.len() as u32 {
61
+ return Err(BinaryStreamError::OutOfBounds);
62
+ }
63
+
64
+ let start = self.offset as usize;
65
+ let end = (self.offset + length) as usize;
66
+ self.offset += length;
67
+
68
+ Ok(self.binary[start..end].to_vec())
69
+ }
70
+
71
+ pub fn write(&mut self, data: Vec<u8>) {
72
+ self.binary.extend(data);
73
+ }
74
+
75
+ pub fn read_remaining(&mut self) -> Vec<u8> {
76
+ let start = self.offset as usize;
77
+ let end = self.binary.len();
78
+ self.offset = end as u32;
79
+
80
+ self.binary[start..end].to_vec()
81
+ }
82
+
83
+ pub fn skip(&mut self, length: u32) {
84
+ self.offset += length;
85
+ }
86
+
87
+ pub fn cursor_at_end(&self) -> bool {
88
+ self.offset == self.binary.len() as u32
89
+ }
90
+
91
+ pub fn cursor_at_start(&self) -> bool {
92
+ self.offset == 0
93
+ }
94
+
95
+ pub fn get_offset(&self) -> u32 {
96
+ self.offset
97
+ }
98
+
99
+ pub fn read_bytes(&mut self, length: u32) -> Result<Vec<u8>, BinaryStreamError> {
100
+ Ok(self.read(length)?)
101
+ }
102
+
103
+ pub fn write_bytes(&mut self, data: Vec<u8>) {
104
+ self.write(data);
105
+ }
106
+
107
+ pub fn read_u8(&mut self) -> Result<u8, BinaryStreamError> {
108
+ let bytes = self.read(1)?;
109
+ Ok(bytes[0])
110
+ }
111
+
112
+ pub fn write_u8(&mut self, value: u8) {
113
+ self.write(vec![value]);
114
+ }
115
+
116
+ pub fn read_magic(&mut self) -> Result<Vec<u8>, BinaryStreamError> {
117
+ let magic = self.read(16)?;
118
+ Ok(magic)
119
+ }
120
+
121
+ pub fn write_magic(&mut self, magic: Vec<u8>) {
122
+ self.write(magic);
123
+ }
124
+
125
+ pub fn read_u16(&mut self, endianness: Option<Endianness>) -> Result<u16, BinaryStreamError> {
126
+ let bytes = self.read(2)?;
127
+ Ok(match endianness.unwrap_or(Endianness::Big) {
128
+ Endianness::Big => u16::from_be_bytes(bytes.try_into().unwrap()),
129
+ Endianness::Little => u16::from_le_bytes(bytes.try_into().unwrap()),
130
+ })
131
+ }
132
+
133
+ pub fn write_u16(&mut self, value: u16, endianness: Option<Endianness>) {
134
+ let bytes = match endianness.unwrap_or(Endianness::Big) {
135
+ Endianness::Big => value.to_be_bytes(),
136
+ Endianness::Little => value.to_le_bytes(),
137
+ };
138
+ self.write(bytes.to_vec());
139
+ }
140
+
141
+ pub fn read_u64(&mut self, endianness: Option<Endianness>) -> Result<u64, BinaryStreamError> {
142
+ let bytes = self.read(8)?;
143
+ let bytes: [u8; 8] = bytes.try_into()
144
+ .map_err(|_| BinaryStreamError::InvalidLength)?;
145
+
146
+ Ok(match endianness.unwrap_or(Endianness::Little) {
147
+ Endianness::Big => u64::from_be_bytes(bytes),
148
+ Endianness::Little => u64::from_le_bytes(bytes),
149
+ })
150
+ }
151
+
152
+ pub fn write_u64(&mut self, value: u64, endianness: Option<Endianness>) {
153
+ let bytes = match endianness.unwrap_or(Endianness::Little) {
154
+ Endianness::Big => value.to_be_bytes(),
155
+ Endianness::Little => value.to_le_bytes(),
156
+ };
157
+ self.write(bytes.to_vec());
158
+ }
159
+
160
+ pub fn read_u32(&mut self, endianness: Option<Endianness>) -> Result<u32, BinaryStreamError> {
161
+ let endian = match endianness {
162
+ Some(endian) => endian,
163
+ None => Endianness::Big,
164
+ };
165
+
166
+ let bytes = match self.read(4) {
167
+ Ok(bytes) => bytes,
168
+ Err(err) => return Err(err)
169
+ };
170
+
171
+ match endian {
172
+ Endianness::Big => Ok(u32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]])),
173
+ Endianness::Little => Ok(u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]])),
174
+ }
175
+ }
176
+
177
+ pub fn write_u32(&mut self, value: u32, endianness: Option<Endianness>) {
178
+ let endian = match endianness {
179
+ Some(endian) => endian,
180
+ None => Endianness::Big,
181
+ };
182
+ match endian {
183
+ Endianness::Big => self.write(value.to_be_bytes().to_vec()),
184
+ Endianness::Little => self.write(value.to_le_bytes().to_vec()),
185
+ }
186
+ }
187
+
188
+ pub fn read_bool(&mut self) -> Result<bool, BinaryStreamError> {
189
+ let value = self.read_u8()?;
190
+ Ok(value != 0)
191
+ }
192
+
193
+ pub fn write_bool(&mut self, value: bool) {
194
+ self.write_u8(if value { 1 } else { 0 });
195
+ }
196
+
197
+ pub fn read_u24(&mut self, endianness: Option<Endianness>) -> Result<u32, BinaryStreamError> {
198
+ let endian = match endianness {
199
+ Some(endian) => endian,
200
+ None => Endianness::Big,
201
+ };
202
+
203
+ let bytes = match self.read(3) {
204
+ Ok(bytes) => bytes,
205
+ Err(err) => return Err(err)
206
+ };
207
+
208
+ match endian {
209
+ Endianness::Big => Ok(u32::from_be_bytes([0, bytes[0], bytes[1], bytes[2]])),
210
+ Endianness::Little => Ok(u32::from_le_bytes([bytes[0], bytes[1], bytes[2], 0])),
211
+ }
212
+ }
213
+
214
+ pub fn write_u24(&mut self, value: u32, endianness: Option<Endianness>) {
215
+ let endian = match endianness {
216
+ Some(endian) => endian,
217
+ None => Endianness::Big,
218
+ };
219
+
220
+ match endian {
221
+ Endianness::Big => self.write(value.to_be_bytes()[1..].to_vec()),
222
+ Endianness::Little => self.write(value.to_le_bytes()[..3].to_vec()),
223
+ }
224
+ }
225
+
226
+ pub fn read_i16(&mut self, endianness: Option<Endianness>) -> Result<i16, BinaryStreamError> {
227
+ let endian = match endianness {
228
+ Some(endian) => endian,
229
+ None => Endianness::Big,
230
+ };
231
+
232
+ let bytes = match self.read(2) {
233
+ Ok(bytes) => bytes,
234
+ Err(err) => return Err(err)
235
+ };
236
+
237
+ match endian {
238
+ Endianness::Big => Ok(i16::from_be_bytes([bytes[0], bytes[1]])),
239
+ Endianness::Little => Ok(i16::from_le_bytes([bytes[0], bytes[1]])),
240
+ }
241
+ }
242
+
243
+ pub fn write_i16(&mut self, value: i16, endianness: Option<Endianness>) {
244
+ let endian = match endianness {
245
+ Some(endian) => endian,
246
+ None => Endianness::Big,
247
+ };
248
+
249
+ match endian {
250
+ Endianness::Big => self.write(value.to_be_bytes().to_vec()),
251
+ Endianness::Little => self.write(value.to_le_bytes().to_vec()),
252
+ }
253
+ }
254
+
255
+ pub fn read_i64(&mut self, endianness: Option<Endianness>) -> Result<i64, BinaryStreamError> {
256
+ let bytes = self.read(8)?;
257
+ let bytes: [u8; 8] = bytes.try_into()
258
+ .map_err(|_| BinaryStreamError::InvalidLength)?;
259
+ Ok(match endianness.unwrap_or(Endianness::Big) {
260
+ Endianness::Big => i64::from_be_bytes(bytes),
261
+ Endianness::Little => i64::from_le_bytes(bytes),
262
+ })
263
+ }
264
+
265
+ pub fn write_i64(&mut self, value: i64, endianness: Option<Endianness>) {
266
+ let endian = match endianness {
267
+ Some(endian) => endian,
268
+ None => Endianness::Big,
269
+ };
270
+
271
+ match endian {
272
+ Endianness::Big => self.write(value.to_be_bytes().to_vec()),
273
+ Endianness::Little => self.write(value.to_le_bytes().to_vec()),
274
+ }
275
+ }
276
+
277
+ pub fn read_long(&mut self, endianness: Option<Endianness>) -> Result<i64, BinaryStreamError> {
278
+ Ok(self.read_i64(Some(endianness.unwrap_or(Endianness::Big)))?)
279
+ }
280
+
281
+ pub fn write_long(&mut self, value: i64, endianness: Option<Endianness>) {
282
+ self.write_i64(value, Some(endianness.unwrap_or(Endianness::Big)));
283
+ }
284
+
285
+ pub fn read_short(&mut self, endianness: Option<Endianness>) -> Result<u16, BinaryStreamError> {
286
+ let bytes = self.read(2)?;
287
+
288
+ match endianness.unwrap_or(Endianness::Big) {
289
+ Endianness::Big => Ok((bytes[0] as u16) << 8 | bytes[1] as u16),
290
+ Endianness::Little => Ok((bytes[1] as u16) << 8 | bytes[0] as u16),
291
+ }
292
+ }
293
+
294
+ pub fn write_short(&mut self, value: u16, endianness: Option<Endianness>) {
295
+ match endianness.unwrap_or(Endianness::Big) {
296
+ Endianness::Big => {
297
+ self.write(vec![(value >> 8) as u8, (value & 0xFF) as u8]);
298
+ },
299
+ Endianness::Little => {
300
+ self.write(vec![(value & 0xFF) as u8, (value >> 8) as u8]);
301
+ }
302
+ }
303
+ }
304
+
305
+ pub fn read_byte(&mut self) -> Result<u8, BinaryStreamError> {
306
+ let byte = self.read(1)?;
307
+ Ok(byte[0])
308
+ }
309
+
310
+ pub fn write_byte(&mut self, value: u8) {
311
+ self.write(vec![value]);
312
+ }
313
+ }
@@ -0,0 +1,5 @@
1
+ #[derive(Debug)]
2
+ pub enum Endianness {
3
+ Big,
4
+ Little,
5
+ }
@@ -0,0 +1,6 @@
1
+ pub mod binarystream;
2
+ pub mod endianess;
3
+
4
+ pub use binarystream::BinaryStream;
5
+ pub use binarystream::BinaryStreamError;
6
+ pub use endianess::Endianness;