/*************************************************************************/ /* object.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* http://www.godotengine.org */ /*************************************************************************/ /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ /* "Software"), to deal in the Software without restriction, including */ /* without limitation the rights to use, copy, modify, merge, publish, */ /* distribute, sublicense, and/or sell copies of the Software, and to */ /* permit persons to whom the Software is furnished to do so, subject to */ /* the following conditions: */ /* */ /* The above copyright notice and this permission notice shall be */ /* included in all copies or substantial portions of the Software. */ /* */ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "object.h" #include "print_string.h" #include "object_type_db.h" #include "script_language.h" #include "message_queue.h" #include "core_string_names.h" #include "translation.h" #include "os/os.h" #ifdef DEBUG_ENABLED struct _ObjectDebugLock { Object *obj; _ObjectDebugLock(Object *p_obj) { obj=p_obj; obj->_lock_index.ref(); } ~_ObjectDebugLock() { obj->_lock_index.unref(); } }; #define OBJ_DEBUG_LOCK _ObjectDebugLock _debug_lock(this); #else #define OBJ_DEBUG_LOCK #endif Array convert_property_list(const List * p_list) { Array va; for (const List::Element *E=p_list->front();E;E=E->next()) { const PropertyInfo &pi = E->get(); Dictionary d; d["name"]=pi.name; d["type"]=pi.type; d["hint"]=pi.hint; d["hint_string"]=pi.hint_string; d["usage"]=pi.usage; va.push_back(d); } return va; } MethodInfo::MethodInfo() { id=0; flags=METHOD_FLAG_NORMAL; } MethodInfo::MethodInfo(const String& p_name) { id=0; name=p_name; flags=METHOD_FLAG_NORMAL; } MethodInfo::MethodInfo(const String& p_name, const PropertyInfo& p_param1) { id=0; name=p_name; arguments.push_back( p_param1 ); flags=METHOD_FLAG_NORMAL; } MethodInfo::MethodInfo(const String& p_name, const PropertyInfo& p_param1,const PropertyInfo& p_param2) { id=0; name=p_name; arguments.push_back( p_param1 ); arguments.push_back( p_param2 ); flags=METHOD_FLAG_NORMAL; } MethodInfo::MethodInfo(const String& p_name, const PropertyInfo& p_param1,const PropertyInfo& p_param2,const PropertyInfo& p_param3) { id=0; name=p_name; arguments.push_back( p_param1 ); arguments.push_back( p_param2 ); arguments.push_back( p_param3 ); flags=METHOD_FLAG_NORMAL; } MethodInfo::MethodInfo(const String& p_name, const PropertyInfo& p_param1,const PropertyInfo& p_param2,const PropertyInfo& p_param3,const PropertyInfo& p_param4) { id=0; name=p_name; arguments.push_back( p_param1 ); arguments.push_back( p_param2 ); arguments.push_back( p_param3 ); arguments.push_back( p_param4 ); flags=METHOD_FLAG_NORMAL; } MethodInfo::MethodInfo(const String& p_name, const PropertyInfo& p_param1,const PropertyInfo& p_param2,const PropertyInfo& p_param3,const PropertyInfo& p_param4,const PropertyInfo& p_param5) { id=0; name=p_name; arguments.push_back( p_param1 ); arguments.push_back( p_param2 ); arguments.push_back( p_param3 ); arguments.push_back( p_param4 ); arguments.push_back( p_param5 ); flags=METHOD_FLAG_NORMAL; } MethodInfo::MethodInfo(Variant::Type ret) { id=0; flags=METHOD_FLAG_NORMAL; return_val.type=ret; } MethodInfo::MethodInfo(Variant::Type ret,const String& p_name) { id=0; name=p_name; flags=METHOD_FLAG_NORMAL; return_val.type=ret; } MethodInfo::MethodInfo(Variant::Type ret,const String& p_name, const PropertyInfo& p_param1) { id=0; name=p_name; arguments.push_back( p_param1 ); flags=METHOD_FLAG_NORMAL; return_val.type=ret; } MethodInfo::MethodInfo(Variant::Type ret,const String& p_name, const PropertyInfo& p_param1,const PropertyInfo& p_param2) { id=0; name=p_name; arguments.push_back( p_param1 ); arguments.push_back( p_param2 ); flags=METHOD_FLAG_NORMAL; return_val.type=ret; } MethodInfo::MethodInfo(Variant::Type ret,const String& p_name, const PropertyInfo& p_param1,const PropertyInfo& p_param2,const PropertyInfo& p_param3) { id=0; name=p_name; arguments.push_back( p_param1 ); arguments.push_back( p_param2 ); arguments.push_back( p_param3 ); flags=METHOD_FLAG_NORMAL; return_val.type=ret; } MethodInfo::MethodInfo(Variant::Type ret,const String& p_name, const PropertyInfo& p_param1,const PropertyInfo& p_param2,const PropertyInfo& p_param3,const PropertyInfo& p_param4) { id=0; name=p_name; arguments.push_back( p_param1 ); arguments.push_back( p_param2 ); arguments.push_back( p_param3 ); arguments.push_back( p_param4 ); flags=METHOD_FLAG_NORMAL; return_val.type=ret; } MethodInfo::MethodInfo(Variant::Type ret,const String& p_name, const PropertyInfo& p_param1,const PropertyInfo& p_param2,const PropertyInfo& p_param3,const PropertyInfo& p_param4,const PropertyInfo& p_param5) { id=0; name=p_name; arguments.push_back( p_param1 ); arguments.push_back( p_param2 ); arguments.push_back( p_param3 ); arguments.push_back( p_param4 ); arguments.push_back( p_param5 ); flags=METHOD_FLAG_NORMAL; return_val.type=ret; } Object::Connection::operator Variant() const { Dictionary d; d["source"]=source; d["signal"]=signal; d["target"]=target; d["method"]=method; d["flags"]=flags; d["binds"]=binds; return d; } bool Object::Connection::operator<(const Connection& p_conn) const { if (source==p_conn.source) { if (signal == p_conn.signal) { if (target == p_conn.target) { return method < p_conn.method; } else { return target < p_conn.target; } } else return signal < p_conn.signal; } else { return source *p_parents) { } void Object::_get_valid_parents_static(List *p_parents) { } #if 0 //old style set, deprecated void Object::set(const String& p_name, const Variant& p_value) { _setv(p_name,p_value); //if (!_use_builtin_script()) // return; bool success; ObjectTypeDB::set_property(this,p_name,p_value,success); if (success) { return; } if (p_name=="__meta__") { metadata=p_value; } else if (p_name=="script/script") { set_script(p_value); } else if (script_instance) { script_instance->set(p_name,p_value); } } #endif void Object::set(const StringName& p_name, const Variant& p_value, bool *r_valid) { #ifdef TOOLS_ENABLED _edited=true; #endif if (script_instance) { if (script_instance->set(p_name,p_value)) { if (r_valid) *r_valid=true; return; } } //try built-in setgetter { if (ObjectTypeDB::set_property(this,p_name,p_value)) { if (r_valid) *r_valid=true; return; } } if (p_name==CoreStringNames::get_singleton()->_script) { set_script(p_value); if (r_valid) *r_valid=true; return; } else if (p_name==CoreStringNames::get_singleton()->_meta) { //set_meta(p_name,p_value); metadata=p_value; if (r_valid) *r_valid=true; return; } else { //something inside the object... :| bool success = _setv(p_name,p_value); if (success) { if (r_valid) *r_valid=true; return; } setvar(p_name,p_value,r_valid); } } Variant Object::get(const StringName& p_name, bool *r_valid) const{ Variant ret; if (script_instance) { if (script_instance->get(p_name,ret)) { if (r_valid) *r_valid=true; return ret; } } //try built-in setgetter { if (ObjectTypeDB::get_property(const_cast(this),p_name,ret)) { if (r_valid) *r_valid=true; return ret; } } if (p_name==CoreStringNames::get_singleton()->_script) { ret = get_script(); if (r_valid) *r_valid=true; return ret; } else if (p_name==CoreStringNames::get_singleton()->_meta) { ret = metadata; if (r_valid) *r_valid=true; return ret; } else { //something inside the object... :| bool success = _getv(p_name,ret); if (success) { if (r_valid) *r_valid=true; return ret; } //if nothing else, use getvar return getvar(p_name,r_valid); } } #if 0 //old style get, deprecated Variant Object::get(const String& p_name) const { Variant ret=_getv(p_name); if (ret.get_type()!=Variant::NIL) return ret; bool success; ObjectTypeDB::get_property(const_cast(this),p_name,ret,success); if (success) { return ret; } if (p_name=="__meta__") return metadata; else if (p_name=="script/script") return script; if (script_instance) { return script_instance->get(p_name); } return Variant(); } #endif void Object::get_property_list(List *p_list,bool p_reversed) const { if (script_instance && p_reversed) { p_list->push_back( PropertyInfo(Variant::NIL,"Script Variables",PROPERTY_HINT_NONE,String(),PROPERTY_USAGE_CATEGORY)); script_instance->get_property_list(p_list); } _get_property_listv(p_list,p_reversed); if (!_use_builtin_script()) return; if (!is_type("Script")) // can still be set, but this is for userfriendlyness p_list->push_back( PropertyInfo( Variant::OBJECT, "script/script", PROPERTY_HINT_RESOURCE_TYPE, "Script",PROPERTY_USAGE_DEFAULT|PROPERTY_USAGE_STORE_IF_NONZERO)); if (!metadata.empty()) p_list->push_back( PropertyInfo( Variant::DICTIONARY, "__meta__", PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR|PROPERTY_USAGE_STORE_IF_NONZERO)); if (script_instance && !p_reversed) { p_list->push_back( PropertyInfo(Variant::NIL,"Script Variables",PROPERTY_HINT_NONE,String(),PROPERTY_USAGE_CATEGORY)); script_instance->get_property_list(p_list); } } void Object::get_method_list(List *p_list) const { ObjectTypeDB::get_method_list(get_type_name(),p_list); if (script_instance) { script_instance->get_method_list(p_list); } } Variant Object::_call_bind(const Variant** p_args, int p_argcount, Variant::CallError& r_error) { if (p_argcount<1) { r_error.error=Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; r_error.argument=0; return Variant(); } if (p_args[0]->get_type()!=Variant::STRING) { r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument=0; r_error.expected=Variant::STRING; return Variant(); } StringName method = *p_args[0]; return call(method,&p_args[1],p_argcount-1,r_error); } Variant Object::_call_deferred_bind(const Variant** p_args, int p_argcount, Variant::CallError& r_error) { if (p_argcount<1) { r_error.error=Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; r_error.argument=0; return Variant(); } if (p_args[0]->get_type()!=Variant::STRING) { r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument=0; r_error.expected=Variant::STRING; return Variant(); } r_error.error=Variant::CallError::CALL_OK; StringName signal = *p_args[0]; Variant v[VARIANT_ARG_MAX]; for(int i=0;i_free) { #ifdef DEBUG_ENABLED if (cast_to()) { ERR_EXPLAIN("Can't 'free' a reference."); ERR_FAIL(); return; } if (_lock_index.get()>1) { ERR_EXPLAIN("Object is locked and can't be freed."); ERR_FAIL(); return; } #endif //must be here, must be before everything, memdelete(this); return; } //Variant ret; OBJ_DEBUG_LOCK Variant::CallError error; if (script_instance) { script_instance->call_multilevel(p_method,p_args,p_argcount); //_test_call_error(p_method,error); } MethodBind *method=ObjectTypeDB::get_method(get_type_name(),p_method); if (method) { method->call(this,p_args,p_argcount,error); _test_call_error(p_method,error); } } void Object::call_multilevel_reversed(const StringName& p_method,const Variant** p_args,int p_argcount) { MethodBind *method=ObjectTypeDB::get_method(get_type_name(),p_method); Variant::CallError error; OBJ_DEBUG_LOCK if (method) { method->call(this,p_args,p_argcount,error); _test_call_error(p_method,error); } //Variant ret; if (script_instance) { script_instance->call_multilevel_reversed(p_method,p_args,p_argcount); //_test_call_error(p_method,error); } } bool Object::has_method(const StringName& p_method) const { if (p_method==CoreStringNames::get_singleton()->_free) { return true; } if (script_instance && script_instance->has_method(p_method)) { return true; } MethodBind *method=ObjectTypeDB::get_method(get_type_name(),p_method); if (method) { return true; } return false; } Variant Object::getvar(const Variant& p_key, bool *r_valid) const { if (r_valid) *r_valid=false; return Variant(); } void Object::setvar(const Variant& p_key, const Variant& p_value,bool *r_valid) { if (r_valid) *r_valid=false; } Variant Object::callv(const StringName& p_method,const Array& p_args) { if (p_args.size()==0) { return call(p_method); } Vector args; args.resize(p_args.size()); Vector argptrs; argptrs.resize(p_args.size()); for(int i=0;i_free) { #ifdef DEBUG_ENABLED if (cast_to()) { ERR_EXPLAIN("Can't 'free' a reference."); ERR_FAIL_V(Variant()); } #endif //must be here, must be before everything, memdelete(this); return Variant(); } VARIANT_ARGPTRS; int argc=0; for(int i=0;iget_type()==Variant::NIL) break; argc++; } Variant::CallError error; Variant ret; if (script_instance) { ret = script_instance->call(p_name,argptr,argc,error); if (_test_call_error(p_name,error)) return ret; } MethodBind *method=ObjectTypeDB::get_method(get_type_name(),p_name); if (method) { Variant ret = method->call(this,argptr,argc,error); if (_test_call_error(p_name,error)) return ret; return ret; } else { } return Variant(); #else VARIANT_ARGPTRS; int argc=0; for(int i=0;iget_type()==Variant::NIL) break; argc++; } Variant::CallError error; Variant ret = call(p_name,argptr,argc,error); return ret; #endif } void Object::call_multilevel(const StringName& p_name, VARIANT_ARG_DECLARE) { #if 0 if (p_name==CoreStringNames::get_singleton()->_free) { #ifdef DEBUG_ENABLED if (cast_to()) { ERR_EXPLAIN("Can't 'free' a reference."); ERR_FAIL(); return; } #endif //must be here, must be before everything, memdelete(this); return; } VARIANT_ARGPTRS; int argc=0; for(int i=0;iget_type()==Variant::NIL) break; argc++; } Variant::CallError error; if (script_instance) { script_instance->call(p_name,argptr,argc,error); _test_call_error(p_name,error); } MethodBind *method=ObjectTypeDB::get_method(get_type_name(),p_name); if (method) { method->call(this,argptr,argc,error); _test_call_error(p_name,error); } #else VARIANT_ARGPTRS; int argc=0; for(int i=0;iget_type()==Variant::NIL) break; argc++; } //Variant::CallError error; call_multilevel(p_name,argptr,argc); #endif } Variant Object::call(const StringName& p_method,const Variant** p_args,int p_argcount,Variant::CallError &r_error) { if (p_method==CoreStringNames::get_singleton()->_free) { //free must be here, before anything, always ready #ifdef DEBUG_ENABLED if (p_argcount!=0) { r_error.argument=0; r_error.error=Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS; return Variant(); } if (cast_to()) { r_error.argument=0; r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD; ERR_EXPLAIN("Can't 'free' a reference."); ERR_FAIL_V(Variant()); } if (_lock_index.get()>1) { r_error.argument=0; r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD; ERR_EXPLAIN("Object is locked and can't be freed."); ERR_FAIL_V(Variant()); } #endif //must be here, must be before everything, memdelete(this); r_error.error=Variant::CallError::CALL_OK; return Variant(); } Variant ret; OBJ_DEBUG_LOCK if (script_instance) { ret = script_instance->call(p_method,p_args,p_argcount,r_error); //force jumptable switch(r_error.error) { case Variant::CallError::CALL_OK: return ret; case Variant::CallError::CALL_ERROR_INVALID_METHOD: break; case Variant::CallError::CALL_ERROR_INVALID_ARGUMENT: case Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: case Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS: return ret; case Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL: {} } } MethodBind *method=ObjectTypeDB::get_method(get_type_name(),p_method); if (method) { ret=method->call(this,p_args,p_argcount,r_error); } else { r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD; } return ret; } void Object::notification(int p_notification,bool p_reversed) { _notificationv(p_notification,p_reversed); if (script_instance) { script_instance->notification(p_notification); } } void Object::_changed_callback(Object *p_changed,const char *p_prop) { } void Object::add_change_receptor( Object *p_receptor ) { change_receptors.insert(p_receptor); } void Object::remove_change_receptor( Object *p_receptor ) { change_receptors.erase(p_receptor); } void Object::property_list_changed_notify() { _change_notify(); } void Object::cancel_delete() { _predelete_ok=true; } void Object::set_script(const RefPtr& p_script) { if (script==p_script) return; if (script_instance) { memdelete(script_instance); script_instance=NULL; } script=p_script; Ref