/*************************************************************************/ /* gd_script.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* http://www.godotengine.org */ /*************************************************************************/ /* Copyright (c) 2007-2016 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 "gd_script.h" #include "globals.h" #include "global_constants.h" #include "gd_compiler.h" #include "os/file_access.h" #include "io/file_access_encrypted.h" #include "os/os.h" /////////////////////////// GDNativeClass::GDNativeClass(const StringName& p_name) { name=p_name; } /*void GDNativeClass::call_multilevel(const StringName& p_method,const Variant** p_args,int p_argcount){ }*/ bool GDNativeClass::_get(const StringName& p_name,Variant &r_ret) const { bool ok; int v = ObjectTypeDB::get_integer_constant(name, p_name, &ok); if (ok) { r_ret=v; return true; } else { return false; } } void GDNativeClass::_bind_methods() { ObjectTypeDB::bind_method(_MD("new"),&GDNativeClass::_new); } Variant GDNativeClass::_new() { Object *o = instance(); if (!o) { ERR_EXPLAIN("Class type: '"+String(name)+"' is not instantiable."); ERR_FAIL_COND_V(!o,Variant()); } Reference *ref = o->cast_to(); if (ref) { return REF(ref); } else { return o; } } Object *GDNativeClass::instance() { return ObjectTypeDB::instance(name); } GDInstance* GDScript::_create_instance(const Variant** p_args,int p_argcount,Object *p_owner,bool p_isref,Variant::CallError& r_error) { /* STEP 1, CREATE */ GDInstance* instance = memnew( GDInstance ); instance->base_ref=p_isref; instance->members.resize(member_indices.size()); instance->script=Ref(this); instance->owner=p_owner; #ifdef DEBUG_ENABLED //needed for hot reloading for(Map::Element *E=member_indices.front();E;E=E->next()) { instance->member_indices_cache[E->key()]=E->get().index; } #endif instance->owner->set_script_instance(instance); /* STEP 2, INITIALIZE AND CONSRTUCT */ instances.insert(instance->owner); initializer->call(instance,p_args,p_argcount,r_error); if (r_error.error!=Variant::CallError::CALL_OK) { instance->script=Ref(); instance->owner->set_script_instance(NULL); instances.erase(p_owner); ERR_FAIL_COND_V(r_error.error!=Variant::CallError::CALL_OK, NULL); //error constructing } //@TODO make thread safe return instance; } Variant GDScript::_new(const Variant** p_args,int p_argcount,Variant::CallError& r_error) { /* STEP 1, CREATE */ if (!valid) { r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD; return Variant(); } r_error.error=Variant::CallError::CALL_OK; REF ref; Object *owner=NULL; GDScript *_baseptr=this; while (_baseptr->_base) { _baseptr=_baseptr->_base; } ERR_FAIL_COND_V(_baseptr->native.is_null(), Variant()); owner=_baseptr->native->instance(); Reference *r=owner->cast_to(); if (r) { ref=REF(r); } GDInstance* instance = _create_instance(p_args,p_argcount,owner,r!=NULL,r_error); if (!instance) { if (ref.is_null()) { memdelete(owner); //no owner, sorry } return Variant(); } if (ref.is_valid()) { return ref; } else { return owner; } } bool GDScript::can_instance() const { //return valid; //any script in GDscript can instance return valid || (!tool && !ScriptServer::is_scripting_enabled()); } StringName GDScript::get_instance_base_type() const { if (native.is_valid()) return native->get_name(); if (base.is_valid()) return base->get_instance_base_type(); return StringName(); } struct _GDScriptMemberSort { int index; StringName name; _FORCE_INLINE_ bool operator<(const _GDScriptMemberSort& p_member) const { return index < p_member.index; } }; #ifdef TOOLS_ENABLED void GDScript::_placeholder_erased(PlaceHolderScriptInstance *p_placeholder) { placeholders.erase(p_placeholder); } /* void GDScript::_update_placeholder(PlaceHolderScriptInstance *p_placeholder) { List plist; GDScript *scr=this; Map default_values; while(scr) { Vector<_GDScriptMemberSort> msort; for(Map::Element *E=scr->member_info.front();E;E=E->next()) { _GDScriptMemberSort ms; ERR_CONTINUE(!scr->member_indices.has(E->key())); ms.index=scr->member_indices[E->key()].index; ms.name=E->key(); msort.push_back(ms); } msort.sort(); msort.invert(); for(int i=0;imember_info[msort[i].name]); if (scr->member_default_values.has(msort[i].name)) default_values[msort[i].name]=scr->member_default_values[msort[i].name]; else { Variant::CallError err; default_values[msort[i].name]=Variant::construct(scr->member_info[msort[i].name].type,NULL,0,err); } } scr=scr->_base; } p_placeholder->update(plist,default_values); }*/ #endif bool GDScript::get_property_default_value(const StringName& p_property, Variant &r_value) const { #ifdef TOOLS_ENABLED //for (const Map::Element *I=member_default_values.front();I;I=I->next()) { // print_line("\t"+String(String(I->key())+":"+String(I->get()))); //} const Map::Element *E=member_default_values_cache.find(p_property); if (E) { r_value=E->get(); return true; } if (base_cache.is_valid()) { return base_cache->get_property_default_value(p_property,r_value); } #endif return false; } ScriptInstance* GDScript::instance_create(Object *p_this) { if (!tool && !ScriptServer::is_scripting_enabled()) { #ifdef TOOLS_ENABLED //instance a fake script for editing the values //plist.invert(); /*print_line("CREATING PLACEHOLDER"); for(List::Element *E=plist.front();E;E=E->next()) { print_line(E->get().name); }*/ PlaceHolderScriptInstance *si = memnew( PlaceHolderScriptInstance(GDScriptLanguage::get_singleton(),Ref