diff --git a/core/variant.cpp b/core/variant.cpp index c6a55b10e6c..8af2ca7f8ad 100644 --- a/core/variant.cpp +++ b/core/variant.cpp @@ -33,7 +33,7 @@ #include "scene/gui/control.h" #include "io/marshalls.h" #include "core_string_names.h" - +#include "variant_parser.h" String Variant::get_type_name(Variant::Type p_type) { @@ -2973,132 +2973,11 @@ void Variant::construct_from_string(const String& p_string,Variant& r_value,Obje } -String Variant::get_construct_string(ObjectDeConstruct p_obj_deconstruct,void *p_deconstruct_ud) const { +String Variant::get_construct_string() const { - switch( type ) { + String vars; + VariantWriter::write_to_string(*this,vars); - case NIL: return "null"; - case BOOL: return _data._bool ? "true" : "false"; - case INT: return String::num(_data._int); - case REAL: return String::num(_data._real); - case STRING: return "\""+reinterpret_cast(_data._mem)->c_escape()+"\""; - case VECTOR2: return "Vector2("+operator Vector2()+")"; - case RECT2: return "Rect2("+operator Rect2()+")"; - case MATRIX32: return "Matrix32("+operator Matrix32()+")"; - case VECTOR3: return "Vector3("+operator Vector3()+")"; - case PLANE: return "Plane("+operator Plane()+")"; - //case QUAT: - case _AABB: return "AABB("+operator AABB()+")"; - case QUAT: return "Quat("+operator Quat()+")"; - case MATRIX3: return "Matrix3("+operator Matrix3()+")"; - case TRANSFORM: return "Transform("+operator Transform()+")"; - case NODE_PATH: return "@\""+String(operator NodePath()).c_escape()+"\""; - case INPUT_EVENT: return "InputEvent()"; - case COLOR: return "Color("+String::num( operator Color().r)+","+String::num( operator Color().g)+","+String::num( operator Color().b)+","+String::num( operator Color().a)+")" ; - case DICTIONARY: { - - const Dictionary &d =*reinterpret_cast(_data._mem); - //const String *K=NULL; - String str="{"; - List keys; - d.get_key_list(&keys); - - Vector<_VariantStrPair> pairs; - - for(List::Element *E=keys.front();E;E=E->next()) { - - _VariantStrPair sp; - sp.key=E->get().get_construct_string(p_obj_deconstruct,p_deconstruct_ud); - sp.value=d[E->get()].get_construct_string(p_obj_deconstruct,p_deconstruct_ud); - pairs.push_back(sp); - } - - pairs.sort(); - - for(int i=0;i0) - str+=", "; - str+="("+pairs[i].key+":"+pairs[i].value+")"; - } - str+="}"; - - return str; - } break; - case VECTOR3_ARRAY: { - - DVector vec = operator DVector(); - String str="Vector3Array(["; - for(int i=0;i0) - str+=", "; - str+=Variant( vec[i] ).get_construct_string(); - } - return str+"])"; - } break; - case STRING_ARRAY: { - - DVector vec = operator DVector(); - String str="StringArray(["; - for(int i=0;i0) - str+=", "; - str=str+=Variant( vec[i] ).get_construct_string(); - } - return str+"])"; - } break; - case INT_ARRAY: { - - DVector vec = operator DVector(); - String str="IntArray(["; - for(int i=0;i0) - str+=", "; - str=str+itos(vec[i]); - } - return str+"])"; - } break; - case REAL_ARRAY: { - - DVector vec = operator DVector(); - String str="FloatArray(["; - for(int i=0;i0) - str+=", "; - str=str+rtos(vec[i]); - } - return str+"])"; - } break; - case ARRAY: { - - Array arr = operator Array(); - String str="["; - for (int i=0; iget_type()+".new()"; - } - } else - return "null"; - - } break; - default: { - return "["+get_type_name(type)+"]"; - } - } + return vars; } diff --git a/core/variant.h b/core/variant.h index e75a2b1c920..198f4934767 100644 --- a/core/variant.h +++ b/core/variant.h @@ -425,7 +425,7 @@ public: typedef String (*ObjectDeConstruct)(const Variant& p_object,void *ud); typedef void (*ObjectConstruct)(const String& p_text,void *ud,Variant& r_value); - String get_construct_string(ObjectDeConstruct p_obj_deconstruct=NULL,void *p_deconstruct_ud=NULL) const; + String get_construct_string() const; static void construct_from_string(const String& p_string,Variant& r_value,ObjectConstruct p_obj_construct=NULL,void *p_construct_ud=NULL); void operator=(const Variant& p_variant); // only this is enough for all the other types diff --git a/core/variant_parser.cpp b/core/variant_parser.cpp index 82ab8ab690a..43938c7594a 100644 --- a/core/variant_parser.cpp +++ b/core/variant_parser.cpp @@ -19,6 +19,20 @@ bool VariantParser::StreamFile::is_eof() const { } +CharType VariantParser::StreamString::get_char() { + + if (pos>=s.length()) + return 0; + else + return s[pos++]; +} + +bool VariantParser::StreamString::is_utf8() const { + return false; +} +bool VariantParser::StreamString::is_eof() const { + return pos>s.length(); +} ///////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/core/variant_parser.h b/core/variant_parser.h index 2bfb244fc82..831876705da 100644 --- a/core/variant_parser.h +++ b/core/variant_parser.h @@ -32,6 +32,19 @@ public: }; + struct StreamString : public Stream { + + String s; + int pos; + + virtual CharType get_char(); + virtual bool is_utf8() const; + virtual bool is_eof() const; + + StreamString() { pos=0; } + + }; + typedef Error (*ParseResourceFunc)(void* p_self, Stream* p_stream,Ref& r_res,int &line,String &r_err_str); struct ResourceParser { diff --git a/modules/gdscript/gd_functions.cpp b/modules/gdscript/gd_functions.cpp index fd92c8a9ec9..28eb7d54dec 100644 --- a/modules/gdscript/gd_functions.cpp +++ b/modules/gdscript/gd_functions.cpp @@ -33,6 +33,7 @@ #include "gd_script.h" #include "func_ref.h" #include "os/os.h" +#include "variant_parser.h" const char *GDFunctions::get_func_name(Function p_func) { @@ -607,7 +608,9 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va } break; case VAR_TO_STR: { VALIDATE_ARG_COUNT(1); - r_ret=p_args[0]->get_construct_string(); + String vars; + VariantWriter::write_to_string(*p_args[0],vars); + r_ret=vars; } break; case STR_TO_VAR: { VALIDATE_ARG_COUNT(1); @@ -618,7 +621,21 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va r_ret=Variant(); return; } - Variant::construct_from_string(*p_args[0],r_ret); + + VariantParser::StreamString ss; + ss.s=*p_args[0]; + + String errs; + int line; + Error err = VariantParser::parse(&ss,r_ret,errs,line); + + if (err!=OK) { + r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.argument=0; + r_error.expected=Variant::STRING; + r_ret=Variant(); + } + } break; case GEN_RANGE: {