| 1 | /**************************************************************************/ |
| 2 | /* stream_peer.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "stream_peer.h" |
| 32 | |
| 33 | #include "core/io/marshalls.h" |
| 34 | |
| 35 | Error StreamPeer::_put_data(const Vector<uint8_t> &p_data) { |
| 36 | int len = p_data.size(); |
| 37 | if (len == 0) { |
| 38 | return OK; |
| 39 | } |
| 40 | const uint8_t *r = p_data.ptr(); |
| 41 | return put_data(&r[0], len); |
| 42 | } |
| 43 | |
| 44 | Array StreamPeer::_put_partial_data(const Vector<uint8_t> &p_data) { |
| 45 | Array ret; |
| 46 | |
| 47 | int len = p_data.size(); |
| 48 | if (len == 0) { |
| 49 | ret.push_back(OK); |
| 50 | ret.push_back(0); |
| 51 | return ret; |
| 52 | } |
| 53 | |
| 54 | const uint8_t *r = p_data.ptr(); |
| 55 | int sent; |
| 56 | Error err = put_partial_data(&r[0], len, sent); |
| 57 | |
| 58 | if (err != OK) { |
| 59 | sent = 0; |
| 60 | } |
| 61 | ret.push_back(err); |
| 62 | ret.push_back(sent); |
| 63 | return ret; |
| 64 | } |
| 65 | |
| 66 | Array StreamPeer::_get_data(int p_bytes) { |
| 67 | Array ret; |
| 68 | |
| 69 | Vector<uint8_t> data; |
| 70 | data.resize(p_bytes); |
| 71 | if (data.size() != p_bytes) { |
| 72 | ret.push_back(ERR_OUT_OF_MEMORY); |
| 73 | ret.push_back(Vector<uint8_t>()); |
| 74 | return ret; |
| 75 | } |
| 76 | |
| 77 | uint8_t *w = data.ptrw(); |
| 78 | Error err = get_data(&w[0], p_bytes); |
| 79 | |
| 80 | ret.push_back(err); |
| 81 | ret.push_back(data); |
| 82 | return ret; |
| 83 | } |
| 84 | |
| 85 | Array StreamPeer::_get_partial_data(int p_bytes) { |
| 86 | Array ret; |
| 87 | |
| 88 | Vector<uint8_t> data; |
| 89 | data.resize(p_bytes); |
| 90 | if (data.size() != p_bytes) { |
| 91 | ret.push_back(ERR_OUT_OF_MEMORY); |
| 92 | ret.push_back(Vector<uint8_t>()); |
| 93 | return ret; |
| 94 | } |
| 95 | |
| 96 | uint8_t *w = data.ptrw(); |
| 97 | int received; |
| 98 | Error err = get_partial_data(&w[0], p_bytes, received); |
| 99 | |
| 100 | if (err != OK) { |
| 101 | data.clear(); |
| 102 | } else if (received != data.size()) { |
| 103 | data.resize(received); |
| 104 | } |
| 105 | |
| 106 | ret.push_back(err); |
| 107 | ret.push_back(data); |
| 108 | return ret; |
| 109 | } |
| 110 | |
| 111 | void StreamPeer::set_big_endian(bool p_big_endian) { |
| 112 | big_endian = p_big_endian; |
| 113 | } |
| 114 | |
| 115 | bool StreamPeer::is_big_endian_enabled() const { |
| 116 | return big_endian; |
| 117 | } |
| 118 | |
| 119 | void StreamPeer::put_u8(uint8_t p_val) { |
| 120 | put_data((const uint8_t *)&p_val, 1); |
| 121 | } |
| 122 | |
| 123 | void StreamPeer::put_8(int8_t p_val) { |
| 124 | put_data((const uint8_t *)&p_val, 1); |
| 125 | } |
| 126 | |
| 127 | void StreamPeer::put_u16(uint16_t p_val) { |
| 128 | #ifdef BIG_ENDIAN_ENABLED |
| 129 | if (!big_endian) { |
| 130 | p_val = BSWAP16(p_val); |
| 131 | } |
| 132 | #else |
| 133 | if (big_endian) { |
| 134 | p_val = BSWAP16(p_val); |
| 135 | } |
| 136 | #endif |
| 137 | uint8_t buf[2]; |
| 138 | encode_uint16(p_val, buf); |
| 139 | put_data(buf, 2); |
| 140 | } |
| 141 | |
| 142 | void StreamPeer::put_16(int16_t p_val) { |
| 143 | #ifdef BIG_ENDIAN_ENABLED |
| 144 | if (!big_endian) { |
| 145 | p_val = BSWAP16(p_val); |
| 146 | } |
| 147 | #else |
| 148 | if (big_endian) { |
| 149 | p_val = BSWAP16(p_val); |
| 150 | } |
| 151 | #endif |
| 152 | uint8_t buf[2]; |
| 153 | encode_uint16(p_val, buf); |
| 154 | put_data(buf, 2); |
| 155 | } |
| 156 | |
| 157 | void StreamPeer::put_u32(uint32_t p_val) { |
| 158 | #ifdef BIG_ENDIAN_ENABLED |
| 159 | if (!big_endian) { |
| 160 | p_val = BSWAP32(p_val); |
| 161 | } |
| 162 | #else |
| 163 | if (big_endian) { |
| 164 | p_val = BSWAP32(p_val); |
| 165 | } |
| 166 | #endif |
| 167 | uint8_t buf[4]; |
| 168 | encode_uint32(p_val, buf); |
| 169 | put_data(buf, 4); |
| 170 | } |
| 171 | |
| 172 | void StreamPeer::put_32(int32_t p_val) { |
| 173 | #ifdef BIG_ENDIAN_ENABLED |
| 174 | if (!big_endian) { |
| 175 | p_val = BSWAP32(p_val); |
| 176 | } |
| 177 | #else |
| 178 | if (big_endian) { |
| 179 | p_val = BSWAP32(p_val); |
| 180 | } |
| 181 | #endif |
| 182 | uint8_t buf[4]; |
| 183 | encode_uint32(p_val, buf); |
| 184 | put_data(buf, 4); |
| 185 | } |
| 186 | |
| 187 | void StreamPeer::put_u64(uint64_t p_val) { |
| 188 | #ifdef BIG_ENDIAN_ENABLED |
| 189 | if (!big_endian) { |
| 190 | p_val = BSWAP64(p_val); |
| 191 | } |
| 192 | #else |
| 193 | if (big_endian) { |
| 194 | p_val = BSWAP64(p_val); |
| 195 | } |
| 196 | #endif |
| 197 | uint8_t buf[8]; |
| 198 | encode_uint64(p_val, buf); |
| 199 | put_data(buf, 8); |
| 200 | } |
| 201 | |
| 202 | void StreamPeer::put_64(int64_t p_val) { |
| 203 | #ifdef BIG_ENDIAN_ENABLED |
| 204 | if (!big_endian) { |
| 205 | p_val = BSWAP64(p_val); |
| 206 | } |
| 207 | #else |
| 208 | if (big_endian) { |
| 209 | p_val = BSWAP64(p_val); |
| 210 | } |
| 211 | #endif |
| 212 | uint8_t buf[8]; |
| 213 | encode_uint64(p_val, buf); |
| 214 | put_data(buf, 8); |
| 215 | } |
| 216 | |
| 217 | void StreamPeer::put_half(float p_val) { |
| 218 | uint8_t buf[2]; |
| 219 | |
| 220 | encode_half(p_val, buf); |
| 221 | uint16_t *p16 = (uint16_t *)buf; |
| 222 | #ifdef BIG_ENDIAN_ENABLED |
| 223 | if (!big_endian) { |
| 224 | *p16 = BSWAP16(*p16); |
| 225 | } |
| 226 | #else |
| 227 | if (big_endian) { |
| 228 | *p16 = BSWAP16(*p16); |
| 229 | } |
| 230 | #endif |
| 231 | |
| 232 | put_data(buf, 2); |
| 233 | } |
| 234 | |
| 235 | void StreamPeer::put_float(float p_val) { |
| 236 | uint8_t buf[4]; |
| 237 | |
| 238 | encode_float(p_val, buf); |
| 239 | #ifdef BIG_ENDIAN_ENABLED |
| 240 | if (!big_endian) { |
| 241 | uint32_t *p32 = (uint32_t *)buf; |
| 242 | *p32 = BSWAP32(*p32); |
| 243 | } |
| 244 | #else |
| 245 | if (big_endian) { |
| 246 | uint32_t *p32 = (uint32_t *)buf; |
| 247 | *p32 = BSWAP32(*p32); |
| 248 | } |
| 249 | #endif |
| 250 | |
| 251 | put_data(buf, 4); |
| 252 | } |
| 253 | |
| 254 | void StreamPeer::put_double(double p_val) { |
| 255 | uint8_t buf[8]; |
| 256 | |
| 257 | encode_double(p_val, buf); |
| 258 | #ifdef BIG_ENDIAN_ENABLED |
| 259 | if (!big_endian) { |
| 260 | uint64_t *p64 = (uint64_t *)buf; |
| 261 | *p64 = BSWAP64(*p64); |
| 262 | } |
| 263 | #else |
| 264 | if (big_endian) { |
| 265 | uint64_t *p64 = (uint64_t *)buf; |
| 266 | *p64 = BSWAP64(*p64); |
| 267 | } |
| 268 | #endif |
| 269 | |
| 270 | put_data(buf, 8); |
| 271 | } |
| 272 | |
| 273 | void StreamPeer::put_string(const String &p_string) { |
| 274 | CharString cs = p_string.ascii(); |
| 275 | put_u32(cs.length()); |
| 276 | put_data((const uint8_t *)cs.get_data(), cs.length()); |
| 277 | } |
| 278 | |
| 279 | void StreamPeer::put_utf8_string(const String &p_string) { |
| 280 | CharString cs = p_string.utf8(); |
| 281 | put_u32(cs.length()); |
| 282 | put_data((const uint8_t *)cs.get_data(), cs.length()); |
| 283 | } |
| 284 | |
| 285 | void StreamPeer::put_var(const Variant &p_variant, bool p_full_objects) { |
| 286 | int len = 0; |
| 287 | Vector<uint8_t> buf; |
| 288 | encode_variant(p_variant, nullptr, len, p_full_objects); |
| 289 | buf.resize(len); |
| 290 | put_32(len); |
| 291 | encode_variant(p_variant, buf.ptrw(), len, p_full_objects); |
| 292 | put_data(buf.ptr(), buf.size()); |
| 293 | } |
| 294 | |
| 295 | uint8_t StreamPeer::get_u8() { |
| 296 | uint8_t buf[1] = {}; |
| 297 | get_data(buf, 1); |
| 298 | return buf[0]; |
| 299 | } |
| 300 | |
| 301 | int8_t StreamPeer::get_8() { |
| 302 | uint8_t buf[1] = {}; |
| 303 | get_data(buf, 1); |
| 304 | return int8_t(buf[0]); |
| 305 | } |
| 306 | |
| 307 | uint16_t StreamPeer::get_u16() { |
| 308 | uint8_t buf[2]; |
| 309 | get_data(buf, 2); |
| 310 | |
| 311 | uint16_t r = decode_uint16(buf); |
| 312 | #ifdef BIG_ENDIAN_ENABLED |
| 313 | if (!big_endian) { |
| 314 | r = BSWAP16(r); |
| 315 | } |
| 316 | #else |
| 317 | if (big_endian) { |
| 318 | r = BSWAP16(r); |
| 319 | } |
| 320 | #endif |
| 321 | |
| 322 | return r; |
| 323 | } |
| 324 | |
| 325 | int16_t StreamPeer::get_16() { |
| 326 | uint8_t buf[2]; |
| 327 | get_data(buf, 2); |
| 328 | |
| 329 | uint16_t r = decode_uint16(buf); |
| 330 | #ifdef BIG_ENDIAN_ENABLED |
| 331 | if (!big_endian) { |
| 332 | r = BSWAP16(r); |
| 333 | } |
| 334 | #else |
| 335 | if (big_endian) { |
| 336 | r = BSWAP16(r); |
| 337 | } |
| 338 | #endif |
| 339 | |
| 340 | return int16_t(r); |
| 341 | } |
| 342 | |
| 343 | uint32_t StreamPeer::get_u32() { |
| 344 | uint8_t buf[4]; |
| 345 | get_data(buf, 4); |
| 346 | |
| 347 | uint32_t r = decode_uint32(buf); |
| 348 | #ifdef BIG_ENDIAN_ENABLED |
| 349 | if (!big_endian) { |
| 350 | r = BSWAP32(r); |
| 351 | } |
| 352 | #else |
| 353 | if (big_endian) { |
| 354 | r = BSWAP32(r); |
| 355 | } |
| 356 | #endif |
| 357 | |
| 358 | return r; |
| 359 | } |
| 360 | |
| 361 | int32_t StreamPeer::get_32() { |
| 362 | uint8_t buf[4]; |
| 363 | get_data(buf, 4); |
| 364 | |
| 365 | uint32_t r = decode_uint32(buf); |
| 366 | #ifdef BIG_ENDIAN_ENABLED |
| 367 | if (!big_endian) { |
| 368 | r = BSWAP32(r); |
| 369 | } |
| 370 | #else |
| 371 | if (big_endian) { |
| 372 | r = BSWAP32(r); |
| 373 | } |
| 374 | #endif |
| 375 | |
| 376 | return int32_t(r); |
| 377 | } |
| 378 | |
| 379 | uint64_t StreamPeer::get_u64() { |
| 380 | uint8_t buf[8]; |
| 381 | get_data(buf, 8); |
| 382 | |
| 383 | uint64_t r = decode_uint64(buf); |
| 384 | #ifdef BIG_ENDIAN_ENABLED |
| 385 | if (!big_endian) { |
| 386 | r = BSWAP64(r); |
| 387 | } |
| 388 | #else |
| 389 | if (big_endian) { |
| 390 | r = BSWAP64(r); |
| 391 | } |
| 392 | #endif |
| 393 | |
| 394 | return r; |
| 395 | } |
| 396 | |
| 397 | int64_t StreamPeer::get_64() { |
| 398 | uint8_t buf[8]; |
| 399 | get_data(buf, 8); |
| 400 | |
| 401 | uint64_t r = decode_uint64(buf); |
| 402 | #ifdef BIG_ENDIAN_ENABLED |
| 403 | if (!big_endian) { |
| 404 | r = BSWAP64(r); |
| 405 | } |
| 406 | #else |
| 407 | if (big_endian) { |
| 408 | r = BSWAP64(r); |
| 409 | } |
| 410 | #endif |
| 411 | |
| 412 | return int64_t(r); |
| 413 | } |
| 414 | |
| 415 | float StreamPeer::get_half() { |
| 416 | uint8_t buf[2]; |
| 417 | get_data(buf, 2); |
| 418 | |
| 419 | #ifdef BIG_ENDIAN_ENABLED |
| 420 | if (!big_endian) { |
| 421 | uint16_t *p16 = (uint16_t *)buf; |
| 422 | *p16 = BSWAP16(*p16); |
| 423 | } |
| 424 | #else |
| 425 | if (big_endian) { |
| 426 | uint16_t *p16 = (uint16_t *)buf; |
| 427 | *p16 = BSWAP16(*p16); |
| 428 | } |
| 429 | #endif |
| 430 | |
| 431 | return decode_half(buf); |
| 432 | } |
| 433 | |
| 434 | float StreamPeer::get_float() { |
| 435 | uint8_t buf[4]; |
| 436 | get_data(buf, 4); |
| 437 | |
| 438 | #ifdef BIG_ENDIAN_ENABLED |
| 439 | if (!big_endian) { |
| 440 | uint32_t *p32 = (uint32_t *)buf; |
| 441 | *p32 = BSWAP32(*p32); |
| 442 | } |
| 443 | #else |
| 444 | if (big_endian) { |
| 445 | uint32_t *p32 = (uint32_t *)buf; |
| 446 | *p32 = BSWAP32(*p32); |
| 447 | } |
| 448 | #endif |
| 449 | |
| 450 | return decode_float(buf); |
| 451 | } |
| 452 | |
| 453 | double StreamPeer::get_double() { |
| 454 | uint8_t buf[8]; |
| 455 | get_data(buf, 8); |
| 456 | |
| 457 | #ifdef BIG_ENDIAN_ENABLED |
| 458 | if (!big_endian) { |
| 459 | uint64_t *p64 = (uint64_t *)buf; |
| 460 | *p64 = BSWAP64(*p64); |
| 461 | } |
| 462 | #else |
| 463 | if (big_endian) { |
| 464 | uint64_t *p64 = (uint64_t *)buf; |
| 465 | *p64 = BSWAP64(*p64); |
| 466 | } |
| 467 | #endif |
| 468 | |
| 469 | return decode_double(buf); |
| 470 | } |
| 471 | |
| 472 | String StreamPeer::get_string(int p_bytes) { |
| 473 | if (p_bytes < 0) { |
| 474 | p_bytes = get_32(); |
| 475 | } |
| 476 | ERR_FAIL_COND_V(p_bytes < 0, String()); |
| 477 | |
| 478 | Vector<char> buf; |
| 479 | Error err = buf.resize(p_bytes + 1); |
| 480 | ERR_FAIL_COND_V(err != OK, String()); |
| 481 | err = get_data((uint8_t *)&buf[0], p_bytes); |
| 482 | ERR_FAIL_COND_V(err != OK, String()); |
| 483 | buf.write[p_bytes] = 0; |
| 484 | return buf.ptr(); |
| 485 | } |
| 486 | |
| 487 | String StreamPeer::get_utf8_string(int p_bytes) { |
| 488 | if (p_bytes < 0) { |
| 489 | p_bytes = get_32(); |
| 490 | } |
| 491 | ERR_FAIL_COND_V(p_bytes < 0, String()); |
| 492 | |
| 493 | Vector<uint8_t> buf; |
| 494 | Error err = buf.resize(p_bytes); |
| 495 | ERR_FAIL_COND_V(err != OK, String()); |
| 496 | err = get_data(buf.ptrw(), p_bytes); |
| 497 | ERR_FAIL_COND_V(err != OK, String()); |
| 498 | |
| 499 | return String::utf8((const char *)buf.ptr(), buf.size()); |
| 500 | } |
| 501 | |
| 502 | Variant StreamPeer::get_var(bool p_allow_objects) { |
| 503 | int len = get_32(); |
| 504 | Vector<uint8_t> var; |
| 505 | Error err = var.resize(len); |
| 506 | ERR_FAIL_COND_V(err != OK, Variant()); |
| 507 | err = get_data(var.ptrw(), len); |
| 508 | ERR_FAIL_COND_V(err != OK, Variant()); |
| 509 | |
| 510 | Variant ret; |
| 511 | err = decode_variant(ret, var.ptr(), len, nullptr, p_allow_objects); |
| 512 | ERR_FAIL_COND_V_MSG(err != OK, Variant(), "Error when trying to decode Variant."); |
| 513 | |
| 514 | return ret; |
| 515 | } |
| 516 | |
| 517 | void StreamPeer::_bind_methods() { |
| 518 | ClassDB::bind_method(D_METHOD("put_data", "data"), &StreamPeer::_put_data); |
| 519 | ClassDB::bind_method(D_METHOD("put_partial_data", "data"), &StreamPeer::_put_partial_data); |
| 520 | |
| 521 | ClassDB::bind_method(D_METHOD("get_data", "bytes"), &StreamPeer::_get_data); |
| 522 | ClassDB::bind_method(D_METHOD("get_partial_data", "bytes"), &StreamPeer::_get_partial_data); |
| 523 | |
| 524 | ClassDB::bind_method(D_METHOD("get_available_bytes"), &StreamPeer::get_available_bytes); |
| 525 | |
| 526 | ClassDB::bind_method(D_METHOD("set_big_endian", "enable"), &StreamPeer::set_big_endian); |
| 527 | ClassDB::bind_method(D_METHOD("is_big_endian_enabled"), &StreamPeer::is_big_endian_enabled); |
| 528 | |
| 529 | ClassDB::bind_method(D_METHOD("put_8", "value"), &StreamPeer::put_8); |
| 530 | ClassDB::bind_method(D_METHOD("put_u8", "value"), &StreamPeer::put_u8); |
| 531 | ClassDB::bind_method(D_METHOD("put_16", "value"), &StreamPeer::put_16); |
| 532 | ClassDB::bind_method(D_METHOD("put_u16", "value"), &StreamPeer::put_u16); |
| 533 | ClassDB::bind_method(D_METHOD("put_32", "value"), &StreamPeer::put_32); |
| 534 | ClassDB::bind_method(D_METHOD("put_u32", "value"), &StreamPeer::put_u32); |
| 535 | ClassDB::bind_method(D_METHOD("put_64", "value"), &StreamPeer::put_64); |
| 536 | ClassDB::bind_method(D_METHOD("put_u64", "value"), &StreamPeer::put_u64); |
| 537 | ClassDB::bind_method(D_METHOD("put_half", "value"), &StreamPeer::put_half); |
| 538 | ClassDB::bind_method(D_METHOD("put_float", "value"), &StreamPeer::put_float); |
| 539 | ClassDB::bind_method(D_METHOD("put_double", "value"), &StreamPeer::put_double); |
| 540 | ClassDB::bind_method(D_METHOD("put_string", "value"), &StreamPeer::put_string); |
| 541 | ClassDB::bind_method(D_METHOD("put_utf8_string", "value"), &StreamPeer::put_utf8_string); |
| 542 | ClassDB::bind_method(D_METHOD("put_var", "value", "full_objects"), &StreamPeer::put_var, DEFVAL(false)); |
| 543 | |
| 544 | ClassDB::bind_method(D_METHOD("get_8"), &StreamPeer::get_8); |
| 545 | ClassDB::bind_method(D_METHOD("get_u8"), &StreamPeer::get_u8); |
| 546 | ClassDB::bind_method(D_METHOD("get_16"), &StreamPeer::get_16); |
| 547 | ClassDB::bind_method(D_METHOD("get_u16"), &StreamPeer::get_u16); |
| 548 | ClassDB::bind_method(D_METHOD("get_32"), &StreamPeer::get_32); |
| 549 | ClassDB::bind_method(D_METHOD("get_u32"), &StreamPeer::get_u32); |
| 550 | ClassDB::bind_method(D_METHOD("get_64"), &StreamPeer::get_64); |
| 551 | ClassDB::bind_method(D_METHOD("get_u64"), &StreamPeer::get_u64); |
| 552 | ClassDB::bind_method(D_METHOD("get_half"), &StreamPeer::get_half); |
| 553 | ClassDB::bind_method(D_METHOD("get_float"), &StreamPeer::get_float); |
| 554 | ClassDB::bind_method(D_METHOD("get_double"), &StreamPeer::get_double); |
| 555 | ClassDB::bind_method(D_METHOD("get_string", "bytes"), &StreamPeer::get_string, DEFVAL(-1)); |
| 556 | ClassDB::bind_method(D_METHOD("get_utf8_string", "bytes"), &StreamPeer::get_utf8_string, DEFVAL(-1)); |
| 557 | ClassDB::bind_method(D_METHOD("get_var", "allow_objects"), &StreamPeer::get_var, DEFVAL(false)); |
| 558 | |
| 559 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "big_endian"), "set_big_endian", "is_big_endian_enabled"); |
| 560 | } |
| 561 | |
| 562 | //////////////////////////////// |
| 563 | |
| 564 | Error StreamPeerExtension::get_data(uint8_t *r_buffer, int p_bytes) { |
| 565 | Error err; |
| 566 | int received = 0; |
| 567 | if (GDVIRTUAL_CALL(_get_data, r_buffer, p_bytes, &received, err)) { |
| 568 | return err; |
| 569 | } |
| 570 | WARN_PRINT_ONCE("StreamPeerExtension::_get_data is unimplemented!"); |
| 571 | return FAILED; |
| 572 | } |
| 573 | |
| 574 | Error StreamPeerExtension::get_partial_data(uint8_t *r_buffer, int p_bytes, int &r_received) { |
| 575 | Error err; |
| 576 | if (GDVIRTUAL_CALL(_get_partial_data, r_buffer, p_bytes, &r_received, err)) { |
| 577 | return err; |
| 578 | } |
| 579 | WARN_PRINT_ONCE("StreamPeerExtension::_get_partial_data is unimplemented!"); |
| 580 | return FAILED; |
| 581 | } |
| 582 | |
| 583 | Error StreamPeerExtension::put_data(const uint8_t *p_data, int p_bytes) { |
| 584 | Error err; |
| 585 | int sent = 0; |
| 586 | if (GDVIRTUAL_CALL(_put_data, p_data, p_bytes, &sent, err)) { |
| 587 | return err; |
| 588 | } |
| 589 | WARN_PRINT_ONCE("StreamPeerExtension::_put_data is unimplemented!"); |
| 590 | return FAILED; |
| 591 | } |
| 592 | |
| 593 | Error StreamPeerExtension::put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) { |
| 594 | Error err; |
| 595 | if (GDVIRTUAL_CALL(_put_partial_data, p_data, p_bytes, &r_sent, err)) { |
| 596 | return err; |
| 597 | } |
| 598 | WARN_PRINT_ONCE("StreamPeerExtension::_put_partial_data is unimplemented!"); |
| 599 | return FAILED; |
| 600 | } |
| 601 | |
| 602 | void StreamPeerExtension::_bind_methods() { |
| 603 | GDVIRTUAL_BIND(_get_data, "r_buffer", "r_bytes", "r_received"); |
| 604 | GDVIRTUAL_BIND(_get_partial_data, "r_buffer", "r_bytes", "r_received"); |
| 605 | GDVIRTUAL_BIND(_put_data, "p_data", "p_bytes", "r_sent"); |
| 606 | GDVIRTUAL_BIND(_put_partial_data, "p_data", "p_bytes", "r_sent"); |
| 607 | GDVIRTUAL_BIND(_get_available_bytes); |
| 608 | } |
| 609 | |
| 610 | //////////////////////////////// |
| 611 | |
| 612 | void StreamPeerBuffer::_bind_methods() { |
| 613 | ClassDB::bind_method(D_METHOD("seek", "position"), &StreamPeerBuffer::seek); |
| 614 | ClassDB::bind_method(D_METHOD("get_size"), &StreamPeerBuffer::get_size); |
| 615 | ClassDB::bind_method(D_METHOD("get_position"), &StreamPeerBuffer::get_position); |
| 616 | ClassDB::bind_method(D_METHOD("resize", "size"), &StreamPeerBuffer::resize); |
| 617 | ClassDB::bind_method(D_METHOD("set_data_array", "data"), &StreamPeerBuffer::set_data_array); |
| 618 | ClassDB::bind_method(D_METHOD("get_data_array"), &StreamPeerBuffer::get_data_array); |
| 619 | ClassDB::bind_method(D_METHOD("clear"), &StreamPeerBuffer::clear); |
| 620 | ClassDB::bind_method(D_METHOD("duplicate"), &StreamPeerBuffer::duplicate); |
| 621 | |
| 622 | ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "data_array"), "set_data_array", "get_data_array"); |
| 623 | } |
| 624 | |
| 625 | Error StreamPeerBuffer::put_data(const uint8_t *p_data, int p_bytes) { |
| 626 | if (p_bytes <= 0 || !p_data) { |
| 627 | return OK; |
| 628 | } |
| 629 | |
| 630 | if (pointer + p_bytes > data.size()) { |
| 631 | data.resize(pointer + p_bytes); |
| 632 | } |
| 633 | |
| 634 | uint8_t *w = data.ptrw(); |
| 635 | memcpy(&w[pointer], p_data, p_bytes); |
| 636 | |
| 637 | pointer += p_bytes; |
| 638 | return OK; |
| 639 | } |
| 640 | |
| 641 | Error StreamPeerBuffer::put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) { |
| 642 | r_sent = p_bytes; |
| 643 | return put_data(p_data, p_bytes); |
| 644 | } |
| 645 | |
| 646 | Error StreamPeerBuffer::get_data(uint8_t *p_buffer, int p_bytes) { |
| 647 | int recv; |
| 648 | get_partial_data(p_buffer, p_bytes, recv); |
| 649 | if (recv != p_bytes) { |
| 650 | return ERR_INVALID_PARAMETER; |
| 651 | } |
| 652 | |
| 653 | return OK; |
| 654 | } |
| 655 | |
| 656 | Error StreamPeerBuffer::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received) { |
| 657 | if (!p_bytes) { |
| 658 | r_received = 0; |
| 659 | return OK; |
| 660 | } |
| 661 | |
| 662 | if (pointer + p_bytes > data.size()) { |
| 663 | r_received = data.size() - pointer; |
| 664 | if (r_received <= 0) { |
| 665 | r_received = 0; |
| 666 | return OK; //you got 0 |
| 667 | } |
| 668 | } else { |
| 669 | r_received = p_bytes; |
| 670 | } |
| 671 | |
| 672 | const uint8_t *r = data.ptr(); |
| 673 | memcpy(p_buffer, r + pointer, r_received); |
| 674 | |
| 675 | pointer += r_received; |
| 676 | // FIXME: return what? OK or ERR_* |
| 677 | // return OK for now so we don't maybe return garbage |
| 678 | return OK; |
| 679 | } |
| 680 | |
| 681 | int StreamPeerBuffer::get_available_bytes() const { |
| 682 | return data.size() - pointer; |
| 683 | } |
| 684 | |
| 685 | void StreamPeerBuffer::seek(int p_pos) { |
| 686 | ERR_FAIL_COND(p_pos < 0); |
| 687 | ERR_FAIL_COND(p_pos > data.size()); |
| 688 | pointer = p_pos; |
| 689 | } |
| 690 | |
| 691 | int StreamPeerBuffer::get_size() const { |
| 692 | return data.size(); |
| 693 | } |
| 694 | |
| 695 | int StreamPeerBuffer::get_position() const { |
| 696 | return pointer; |
| 697 | } |
| 698 | |
| 699 | void StreamPeerBuffer::resize(int p_size) { |
| 700 | data.resize(p_size); |
| 701 | } |
| 702 | |
| 703 | void StreamPeerBuffer::set_data_array(const Vector<uint8_t> &p_data) { |
| 704 | data = p_data; |
| 705 | pointer = 0; |
| 706 | } |
| 707 | |
| 708 | Vector<uint8_t> StreamPeerBuffer::get_data_array() const { |
| 709 | return data; |
| 710 | } |
| 711 | |
| 712 | void StreamPeerBuffer::clear() { |
| 713 | data.clear(); |
| 714 | pointer = 0; |
| 715 | } |
| 716 | |
| 717 | Ref<StreamPeerBuffer> StreamPeerBuffer::duplicate() const { |
| 718 | Ref<StreamPeerBuffer> spb; |
| 719 | spb.instantiate(); |
| 720 | spb->data = data; |
| 721 | return spb; |
| 722 | } |