From 23441ec8676f6d804692fe1e49e7fea2bec55341 Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Sat, 2 Jan 2016 21:07:03 -0300 Subject: Added var2bytes and bytes2var to convet any variable to bytes and back. Closes #2075 --- modules/gdscript/gd_functions.cpp | 66 +++++++++++++++++++++++++++++++++++++++ modules/gdscript/gd_functions.h | 2 ++ modules/gdscript/gd_tokenizer.cpp | 2 +- 3 files changed, 69 insertions(+), 1 deletion(-) (limited to 'modules/gdscript') diff --git a/modules/gdscript/gd_functions.cpp b/modules/gdscript/gd_functions.cpp index 11bb3a859a..e015ddb65e 100644 --- a/modules/gdscript/gd_functions.cpp +++ b/modules/gdscript/gd_functions.cpp @@ -34,6 +34,7 @@ #include "func_ref.h" #include "os/os.h" #include "variant_parser.h" +#include "io/marshalls.h" const char *GDFunctions::get_func_name(Function p_func) { @@ -94,6 +95,8 @@ const char *GDFunctions::get_func_name(Function p_func) { "printraw", "var2str", "str2var", + "var2bytes", + "bytes2var", "range", "load", "inst2dict", @@ -638,6 +641,57 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va r_ret=Variant(); } + } break; + case VAR_TO_BYTES: { + VALIDATE_ARG_COUNT(1); + + ByteArray barr; + int len; + Error err = encode_variant(*p_args[0],NULL,len); + if (err) { + r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.argument=0; + r_error.expected=Variant::NIL; + r_ret=Variant(); + return; + } + + barr.resize(len); + { + ByteArray::Write w = barr.write(); + encode_variant(*p_args[0],w.ptr(),len); + + } + r_ret=barr; + } break; + case BYTES_TO_VAR: { + VALIDATE_ARG_COUNT(1); + if (p_args[0]->get_type()!=Variant::RAW_ARRAY) { + r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.argument=0; + r_error.expected=Variant::RAW_ARRAY; + r_ret=Variant(); + return; + } + + ByteArray varr=*p_args[0]; + Variant ret; + { + ByteArray::Read r=varr.read(); + Error err = decode_variant(ret,r.ptr(),varr.size(),NULL); + if (err!=OK) { + ERR_PRINT("Not enough bytes for decoding.."); + r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.argument=0; + r_error.expected=Variant::RAW_ARRAY; + r_ret=Variant(); + return; + } + + } + + r_ret=ret; + } break; case GEN_RANGE: { @@ -1358,6 +1412,18 @@ MethodInfo GDFunctions::get_info(Function p_func) { mi.return_val.type=Variant::NIL; return mi; } break; + case VAR_TO_BYTES: { + MethodInfo mi("var2bytes",PropertyInfo(Variant::NIL,"var")); + mi.return_val.type=Variant::RAW_ARRAY; + return mi; + + } break; + case BYTES_TO_VAR: { + + MethodInfo mi("bytes2var:Variant",PropertyInfo(Variant::RAW_ARRAY,"bytes")); + mi.return_val.type=Variant::NIL; + return mi; + } break; case GEN_RANGE: { MethodInfo mi("range",PropertyInfo(Variant::NIL,"...")); diff --git a/modules/gdscript/gd_functions.h b/modules/gdscript/gd_functions.h index e6ef8b9ec8..8c88472567 100644 --- a/modules/gdscript/gd_functions.h +++ b/modules/gdscript/gd_functions.h @@ -89,6 +89,8 @@ public: TEXT_PRINTRAW, VAR_TO_STR, STR_TO_VAR, + VAR_TO_BYTES, + BYTES_TO_VAR, GEN_RANGE, RESOURCE_LOAD, INST2DICT, diff --git a/modules/gdscript/gd_tokenizer.cpp b/modules/gdscript/gd_tokenizer.cpp index 4280c47edf..377ef06694 100644 --- a/modules/gdscript/gd_tokenizer.cpp +++ b/modules/gdscript/gd_tokenizer.cpp @@ -1046,7 +1046,7 @@ void GDTokenizerText::advance(int p_amount) { ////////////////////////////////////////////////////////////////////////////////////////////////////// -#define BYTECODE_VERSION 9 +#define BYTECODE_VERSION 10 Error GDTokenizerBuffer::set_code_buffer(const Vector & p_buffer) { -- cgit v1.2.3