gd_script.cpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690
  1. /*************************************************************************/
  2. /* gd_script.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "gd_script.h"
  30. #include "globals.h"
  31. #include "global_constants.h"
  32. #include "gd_compiler.h"
  33. #include "os/file_access.h"
  34. #include "io/file_access_encrypted.h"
  35. #include "os/os.h"
  36. ///////////////////////////
  37. GDNativeClass::GDNativeClass(const StringName& p_name) {
  38. name=p_name;
  39. }
  40. /*void GDNativeClass::call_multilevel(const StringName& p_method,const Variant** p_args,int p_argcount){
  41. }*/
  42. bool GDNativeClass::_get(const StringName& p_name,Variant &r_ret) const {
  43. bool ok;
  44. int v = ObjectTypeDB::get_integer_constant(name, p_name, &ok);
  45. if (ok) {
  46. r_ret=v;
  47. return true;
  48. } else {
  49. return false;
  50. }
  51. }
  52. void GDNativeClass::_bind_methods() {
  53. ObjectTypeDB::bind_method(_MD("new"),&GDNativeClass::_new);
  54. }
  55. Variant GDNativeClass::_new() {
  56. Object *o = instance();
  57. if (!o) {
  58. ERR_EXPLAIN("Class type: '"+String(name)+"' is not instantiable.");
  59. ERR_FAIL_COND_V(!o,Variant());
  60. }
  61. Reference *ref = o->cast_to<Reference>();
  62. if (ref) {
  63. return REF(ref);
  64. } else {
  65. return o;
  66. }
  67. }
  68. Object *GDNativeClass::instance() {
  69. return ObjectTypeDB::instance(name);
  70. }
  71. GDInstance* GDScript::_create_instance(const Variant** p_args,int p_argcount,Object *p_owner,bool p_isref,Variant::CallError& r_error) {
  72. /* STEP 1, CREATE */
  73. GDInstance* instance = memnew( GDInstance );
  74. instance->base_ref=p_isref;
  75. instance->members.resize(member_indices.size());
  76. instance->script=Ref<GDScript>(this);
  77. instance->owner=p_owner;
  78. instance->owner->set_script_instance(instance);
  79. /* STEP 2, INITIALIZE AND CONSRTUCT */
  80. instances.insert(instance->owner);
  81. initializer->call(instance,p_args,p_argcount,r_error);
  82. if (r_error.error!=Variant::CallError::CALL_OK) {
  83. instance->script=Ref<GDScript>();
  84. instance->owner->set_script_instance(NULL);
  85. instances.erase(p_owner);
  86. ERR_FAIL_COND_V(r_error.error!=Variant::CallError::CALL_OK, NULL); //error constructing
  87. }
  88. //@TODO make thread safe
  89. return instance;
  90. }
  91. Variant GDScript::_new(const Variant** p_args,int p_argcount,Variant::CallError& r_error) {
  92. /* STEP 1, CREATE */
  93. if (!valid) {
  94. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  95. return Variant();
  96. }
  97. r_error.error=Variant::CallError::CALL_OK;
  98. REF ref;
  99. Object *owner=NULL;
  100. GDScript *_baseptr=this;
  101. while (_baseptr->_base) {
  102. _baseptr=_baseptr->_base;
  103. }
  104. if (_baseptr->native.ptr()) {
  105. owner=_baseptr->native->instance();
  106. } else {
  107. owner=memnew( Reference ); //by default, no base means use reference
  108. }
  109. Reference *r=owner->cast_to<Reference>();
  110. if (r) {
  111. ref=REF(r);
  112. }
  113. GDInstance* instance = _create_instance(p_args,p_argcount,owner,r!=NULL,r_error);
  114. if (!instance) {
  115. if (ref.is_null()) {
  116. memdelete(owner); //no owner, sorry
  117. }
  118. return Variant();
  119. }
  120. if (ref.is_valid()) {
  121. return ref;
  122. } else {
  123. return owner;
  124. }
  125. }
  126. bool GDScript::can_instance() const {
  127. //return valid; //any script in GDscript can instance
  128. return valid || (!tool && !ScriptServer::is_scripting_enabled());
  129. }
  130. StringName GDScript::get_instance_base_type() const {
  131. if (native.is_valid())
  132. return native->get_name();
  133. if (base.is_valid())
  134. return base->get_instance_base_type();
  135. return StringName();
  136. }
  137. struct _GDScriptMemberSort {
  138. int index;
  139. StringName name;
  140. _FORCE_INLINE_ bool operator<(const _GDScriptMemberSort& p_member) const { return index < p_member.index; }
  141. };
  142. #ifdef TOOLS_ENABLED
  143. void GDScript::_placeholder_erased(PlaceHolderScriptInstance *p_placeholder) {
  144. placeholders.erase(p_placeholder);
  145. }
  146. /*
  147. void GDScript::_update_placeholder(PlaceHolderScriptInstance *p_placeholder) {
  148. List<PropertyInfo> plist;
  149. GDScript *scr=this;
  150. Map<StringName,Variant> default_values;
  151. while(scr) {
  152. Vector<_GDScriptMemberSort> msort;
  153. for(Map<StringName,PropertyInfo>::Element *E=scr->member_info.front();E;E=E->next()) {
  154. _GDScriptMemberSort ms;
  155. ERR_CONTINUE(!scr->member_indices.has(E->key()));
  156. ms.index=scr->member_indices[E->key()].index;
  157. ms.name=E->key();
  158. msort.push_back(ms);
  159. }
  160. msort.sort();
  161. msort.invert();
  162. for(int i=0;i<msort.size();i++) {
  163. plist.push_front(scr->member_info[msort[i].name]);
  164. if (scr->member_default_values.has(msort[i].name))
  165. default_values[msort[i].name]=scr->member_default_values[msort[i].name];
  166. else {
  167. Variant::CallError err;
  168. default_values[msort[i].name]=Variant::construct(scr->member_info[msort[i].name].type,NULL,0,err);
  169. }
  170. }
  171. scr=scr->_base;
  172. }
  173. p_placeholder->update(plist,default_values);
  174. }*/
  175. #endif
  176. bool GDScript::get_property_default_value(const StringName& p_property, Variant &r_value) const {
  177. #ifdef TOOLS_ENABLED
  178. //for (const Map<StringName,Variant>::Element *I=member_default_values.front();I;I=I->next()) {
  179. // print_line("\t"+String(String(I->key())+":"+String(I->get())));
  180. //}
  181. const Map<StringName,Variant>::Element *E=member_default_values_cache.find(p_property);
  182. if (E) {
  183. r_value=E->get();
  184. return true;
  185. }
  186. if (base_cache.is_valid()) {
  187. return base_cache->get_property_default_value(p_property,r_value);
  188. }
  189. #endif
  190. return false;
  191. }
  192. ScriptInstance* GDScript::instance_create(Object *p_this) {
  193. if (!tool && !ScriptServer::is_scripting_enabled()) {
  194. #ifdef TOOLS_ENABLED
  195. //instance a fake script for editing the values
  196. //plist.invert();
  197. /*print_line("CREATING PLACEHOLDER");
  198. for(List<PropertyInfo>::Element *E=plist.front();E;E=E->next()) {
  199. print_line(E->get().name);
  200. }*/
  201. PlaceHolderScriptInstance *si = memnew( PlaceHolderScriptInstance(GDScriptLanguage::get_singleton(),Ref<Script>(this),p_this) );
  202. placeholders.insert(si);
  203. //_update_placeholder(si);
  204. _update_exports();
  205. return si;
  206. #else
  207. return NULL;
  208. #endif
  209. }
  210. GDScript *top=this;
  211. while(top->_base)
  212. top=top->_base;
  213. if (top->native.is_valid()) {
  214. if (!ObjectTypeDB::is_type(p_this->get_type_name(),top->native->get_name())) {
  215. if (ScriptDebugger::get_singleton()) {
  216. GDScriptLanguage::get_singleton()->debug_break_parse(get_path(),0,"Script inherits from native type '"+String(top->native->get_name())+"', so it can't be instanced in object of type: '"+p_this->get_type()+"'");
  217. }
  218. ERR_EXPLAIN("Script inherits from native type '"+String(top->native->get_name())+"', so it can't be instanced in object of type: '"+p_this->get_type()+"'");
  219. ERR_FAIL_V(NULL);
  220. }
  221. }
  222. Variant::CallError unchecked_error;
  223. return _create_instance(NULL,0,p_this,p_this->cast_to<Reference>(),unchecked_error);
  224. }
  225. bool GDScript::instance_has(const Object *p_this) const {
  226. return instances.has((Object*)p_this);
  227. }
  228. bool GDScript::has_source_code() const {
  229. return source!="";
  230. }
  231. String GDScript::get_source_code() const {
  232. return source;
  233. }
  234. void GDScript::set_source_code(const String& p_code) {
  235. if (source==p_code)
  236. return;
  237. source=p_code;
  238. #ifdef TOOLS_ENABLED
  239. source_changed_cache=true;
  240. //print_line("SC CHANGED "+get_path());
  241. #endif
  242. }
  243. #ifdef TOOLS_ENABLED
  244. void GDScript::_update_exports_values(Map<StringName,Variant>& values, List<PropertyInfo> &propnames) {
  245. if (base_cache.is_valid()) {
  246. base_cache->_update_exports_values(values,propnames);
  247. }
  248. for(Map<StringName,Variant>::Element *E=member_default_values_cache.front();E;E=E->next()) {
  249. values[E->key()]=E->get();
  250. }
  251. for (List<PropertyInfo>::Element *E=members_cache.front();E;E=E->next()) {
  252. propnames.push_back(E->get());
  253. }
  254. }
  255. #endif
  256. bool GDScript::_update_exports() {
  257. #ifdef TOOLS_ENABLED
  258. bool changed=false;
  259. if (source_changed_cache) {
  260. //print_line("updating source for "+get_path());
  261. source_changed_cache=false;
  262. changed=true;
  263. String basedir=path;
  264. if (basedir=="")
  265. basedir=get_path();
  266. if (basedir!="")
  267. basedir=basedir.get_base_dir();
  268. GDParser parser;
  269. Error err = parser.parse(source,basedir,true,path);
  270. if (err==OK) {
  271. const GDParser::Node* root = parser.get_parse_tree();
  272. ERR_FAIL_COND_V(root->type!=GDParser::Node::TYPE_CLASS,false);
  273. const GDParser::ClassNode *c = static_cast<const GDParser::ClassNode*>(root);
  274. if (base_cache.is_valid()) {
  275. base_cache->inheriters_cache.erase(get_instance_ID());
  276. base_cache=Ref<GDScript>();
  277. }
  278. if (c->extends_used && String(c->extends_file)!="" && String(c->extends_file) != get_path()) {
  279. String path = c->extends_file;
  280. if (path.is_rel_path()) {
  281. String base = get_path();
  282. if (base=="" || base.is_rel_path()) {
  283. ERR_PRINT(("Could not resolve relative path for parent class: "+path).utf8().get_data());
  284. } else {
  285. path=base.get_base_dir().plus_file(path);
  286. }
  287. }
  288. if (path!=get_path()) {
  289. Ref<GDScript> bf = ResourceLoader::load(path);
  290. if (bf.is_valid()) {
  291. //print_line("parent is: "+bf->get_path());
  292. base_cache=bf;
  293. bf->inheriters_cache.insert(get_instance_ID());
  294. //bf->_update_exports(p_instances,true,false);
  295. }
  296. } else {
  297. ERR_PRINT(("Path extending itself in "+path).utf8().get_data());
  298. }
  299. }
  300. members_cache.clear();;
  301. member_default_values_cache.clear();
  302. for(int i=0;i<c->variables.size();i++) {
  303. if (c->variables[i]._export.type==Variant::NIL)
  304. continue;
  305. members_cache.push_back(c->variables[i]._export);
  306. //print_line("found "+c->variables[i]._export.name);
  307. member_default_values_cache[c->variables[i].identifier]=c->variables[i].default_value;
  308. }
  309. _signals.clear();
  310. for(int i=0;i<c->_signals.size();i++) {
  311. _signals[c->_signals[i].name]=c->_signals[i].arguments;
  312. }
  313. }
  314. } else {
  315. //print_line("unchaged is "+get_path());
  316. }
  317. if (base_cache.is_valid()) {
  318. if (base_cache->_update_exports()) {
  319. changed = true;
  320. }
  321. }
  322. if (/*changed &&*/ placeholders.size()) { //hm :(
  323. //print_line("updating placeholders for "+get_path());
  324. //update placeholders if any
  325. Map<StringName,Variant> values;
  326. List<PropertyInfo> propnames;
  327. _update_exports_values(values,propnames);
  328. for (Set<PlaceHolderScriptInstance*>::Element *E=placeholders.front();E;E=E->next()) {
  329. E->get()->update(propnames,values);
  330. }
  331. }
  332. return changed;
  333. #endif
  334. return false;
  335. }
  336. void GDScript::update_exports() {
  337. #ifdef TOOLS_ENABLED
  338. _update_exports();
  339. Set<ObjectID> copy=inheriters_cache; //might get modified
  340. //print_line("update exports for "+get_path()+" ic: "+itos(copy.size()));
  341. for(Set<ObjectID>::Element *E=copy.front();E;E=E->next()) {
  342. Object *id=ObjectDB::get_instance(E->get());
  343. if (!id)
  344. continue;
  345. GDScript *s=id->cast_to<GDScript>();
  346. if (!s)
  347. continue;
  348. s->update_exports();
  349. }
  350. #endif
  351. }
  352. void GDScript::_set_subclass_path(Ref<GDScript>& p_sc,const String& p_path) {
  353. p_sc->path=p_path;
  354. for(Map<StringName,Ref<GDScript> >::Element *E=p_sc->subclasses.front();E;E=E->next()) {
  355. _set_subclass_path(E->get(),p_path);
  356. }
  357. }
  358. Error GDScript::reload() {
  359. ERR_FAIL_COND_V(instances.size(),ERR_ALREADY_IN_USE);
  360. String basedir=path;
  361. if (basedir=="")
  362. basedir=get_path();
  363. if (basedir!="")
  364. basedir=basedir.get_base_dir();
  365. valid=false;
  366. GDParser parser;
  367. Error err = parser.parse(source,basedir,false,path);
  368. if (err) {
  369. if (ScriptDebugger::get_singleton()) {
  370. GDScriptLanguage::get_singleton()->debug_break_parse(get_path(),parser.get_error_line(),"Parser Error: "+parser.get_error());
  371. }
  372. _err_print_error("GDScript::reload",path.empty()?"built-in":(const char*)path.utf8().get_data(),parser.get_error_line(),("Parse Error: "+parser.get_error()).utf8().get_data(),ERR_HANDLER_SCRIPT);
  373. ERR_FAIL_V(ERR_PARSE_ERROR);
  374. }
  375. bool can_run = ScriptServer::is_scripting_enabled() || parser.is_tool_script();
  376. GDCompiler compiler;
  377. err = compiler.compile(&parser,this);
  378. if (err) {
  379. if (can_run) {
  380. if (ScriptDebugger::get_singleton()) {
  381. GDScriptLanguage::get_singleton()->debug_break_parse(get_path(),compiler.get_error_line(),"Parser Error: "+compiler.get_error());
  382. }
  383. _err_print_error("GDScript::reload",path.empty()?"built-in":(const char*)path.utf8().get_data(),compiler.get_error_line(),("Compile Error: "+compiler.get_error()).utf8().get_data(),ERR_HANDLER_SCRIPT);
  384. ERR_FAIL_V(ERR_COMPILATION_FAILED);
  385. } else {
  386. return err;
  387. }
  388. }
  389. valid=true;
  390. for(Map<StringName,Ref<GDScript> >::Element *E=subclasses.front();E;E=E->next()) {
  391. _set_subclass_path(E->get(),path);
  392. }
  393. #ifdef TOOLS_ENABLED
  394. /*for (Set<PlaceHolderScriptInstance*>::Element *E=placeholders.front();E;E=E->next()) {
  395. _update_placeholder(E->get());
  396. }*/
  397. #endif
  398. return OK;
  399. }
  400. String GDScript::get_node_type() const {
  401. return ""; // ?
  402. }
  403. ScriptLanguage *GDScript::get_language() const {
  404. return GDScriptLanguage::get_singleton();
  405. }
  406. Variant GDScript::call(const StringName& p_method,const Variant** p_args,int p_argcount,Variant::CallError &r_error) {
  407. GDScript *top=this;
  408. while(top) {
  409. Map<StringName,GDFunction*>::Element *E=top->member_functions.find(p_method);
  410. if (E) {
  411. if (!E->get()->is_static()) {
  412. WARN_PRINT(String("Can't call non-static function: '"+String(p_method)+"' in script.").utf8().get_data());
  413. }
  414. return E->get()->call(NULL,p_args,p_argcount,r_error);
  415. }
  416. top=top->_base;
  417. }
  418. //none found, regular
  419. return Script::call(p_method,p_args,p_argcount,r_error);
  420. }
  421. bool GDScript::_get(const StringName& p_name,Variant &r_ret) const {
  422. {
  423. const GDScript *top=this;
  424. while(top) {
  425. {
  426. const Map<StringName,Variant>::Element *E=top->constants.find(p_name);
  427. if (E) {
  428. r_ret= E->get();
  429. return true;
  430. }
  431. }
  432. {
  433. const Map<StringName,Ref<GDScript> >::Element *E=subclasses.find(p_name);
  434. if (E) {
  435. r_ret=E->get();
  436. return true;
  437. }
  438. }
  439. top=top->_base;
  440. }
  441. if (p_name==GDScriptLanguage::get_singleton()->strings._script_source) {
  442. r_ret=get_source_code();
  443. return true;
  444. }
  445. }
  446. return false;
  447. }
  448. bool GDScript::_set(const StringName& p_name, const Variant& p_value) {
  449. if (p_name==GDScriptLanguage::get_singleton()->strings._script_source) {
  450. set_source_code(p_value);
  451. reload();
  452. } else
  453. return false;
  454. return true;
  455. }
  456. void GDScript::_get_property_list(List<PropertyInfo> *p_properties) const {
  457. p_properties->push_back( PropertyInfo(Variant::STRING,"script/source",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR) );
  458. }
  459. void GDScript::_bind_methods() {
  460. ObjectTypeDB::bind_native_method(METHOD_FLAGS_DEFAULT,"new",&GDScript::_new,MethodInfo("new"));
  461. ObjectTypeDB::bind_method(_MD("get_as_byte_code"),&GDScript::get_as_byte_code);
  462. }
  463. Vector<uint8_t> GDScript::get_as_byte_code() const {
  464. GDTokenizerBuffer tokenizer;
  465. return tokenizer.parse_code_string(source);
  466. };
  467. Error GDScript::load_byte_code(const String& p_path) {
  468. Vector<uint8_t> bytecode;
  469. if (p_path.ends_with("gde")) {
  470. FileAccess *fa = FileAccess::open(p_path,FileAccess::READ);
  471. ERR_FAIL_COND_V(!fa,ERR_CANT_OPEN);
  472. FileAccessEncrypted *fae = memnew( FileAccessEncrypted );
  473. ERR_FAIL_COND_V(!fae,ERR_CANT_OPEN);
  474. Vector<uint8_t> key;
  475. key.resize(32);
  476. for(int i=0;i<key.size();i++) {
  477. key[i]=script_encryption_key[i];
  478. }
  479. Error err = fae->open_and_parse(fa,key,FileAccessEncrypted::MODE_READ);
  480. ERR_FAIL_COND_V(err,err);
  481. bytecode.resize(fae->get_len());
  482. fae->get_buffer(bytecode.ptr(),bytecode.size());
  483. memdelete(fae);
  484. } else {
  485. bytecode = FileAccess::get_file_as_array(p_path);
  486. }
  487. ERR_FAIL_COND_V(bytecode.size()==0,ERR_PARSE_ERROR);
  488. path=p_path;
  489. String basedir=path;
  490. if (basedir=="")
  491. basedir=get_path();
  492. if (basedir!="")
  493. basedir=basedir.get_base_dir();
  494. valid=false;
  495. GDParser parser;
  496. Error err = parser.parse_bytecode(bytecode,basedir,get_path());
  497. if (err) {
  498. _err_print_error("GDScript::load_byte_code",path.empty()?"built-in":(const char*)path.utf8().get_data(),parser.get_error_line(),("Parse Error: "+parser.get_error()).utf8().get_data(),ERR_HANDLER_SCRIPT);
  499. ERR_FAIL_V(ERR_PARSE_ERROR);
  500. }
  501. GDCompiler compiler;
  502. err = compiler.compile(&parser,this);
  503. if (err) {
  504. _err_print_error("GDScript::load_byte_code",path.empty()?"built-in":(const char*)path.utf8().get_data(),compiler.get_error_line(),("Compile Error: "+compiler.get_error()).utf8().get_data(),ERR_HANDLER_SCRIPT);
  505. ERR_FAIL_V(ERR_COMPILATION_FAILED);
  506. }
  507. valid=true;
  508. for(Map<StringName,Ref<GDScript> >::Element *E=subclasses.front();E;E=E->next()) {
  509. _set_subclass_path(E->get(),path);
  510. }
  511. return OK;
  512. }
  513. Error GDScript::load_source_code(const String& p_path) {
  514. DVector<uint8_t> sourcef;
  515. Error err;
  516. FileAccess *f=FileAccess::open(p_path,FileAccess::READ,&err);
  517. if (err) {
  518. ERR_FAIL_COND_V(err,err);
  519. }
  520. int len = f->get_len();
  521. sourcef.resize(len+1);
  522. DVector<uint8_t>::Write w = sourcef.write();
  523. int r = f->get_buffer(w.ptr(),len);
  524. f->close();
  525. memdelete(f);
  526. ERR_FAIL_COND_V(r!=len,ERR_CANT_OPEN);
  527. w[len]=0;
  528. String s;
  529. if (s.parse_utf8((const char*)w.ptr())) {
  530. ERR_EXPLAIN("Script '"+p_path+"' contains invalid unicode (utf-8), so it was not loaded. Please ensure that scripts are saved in valid utf-8 unicode.");
  531. ERR_FAIL_V(ERR_INVALID_DATA);
  532. }
  533. source=s;
  534. #ifdef TOOLS_ENABLED
  535. source_changed_cache=true;
  536. #endif
  537. //print_line("LSC :"+get_path());
  538. path=p_path;
  539. return OK;
  540. }
  541. const Map<StringName,GDFunction*>& GDScript::debug_get_member_functions() const {
  542. return member_functions;
  543. }
  544. StringName GDScript::debug_get_member_by_index(int p_idx) const {
  545. for(const Map<StringName,MemberInfo>::Element *E=member_indices.front();E;E=E->next()) {
  546. if (E->get().index==p_idx)
  547. return E->key();
  548. }
  549. return "<error>";
  550. }
  551. Ref<GDScript> GDScript::get_base() const {
  552. return base;
  553. }
  554. bool GDScript::has_script_signal(const StringName& p_signal) const {
  555. if (_signals.has(p_signal))
  556. return true;
  557. if (base.is_valid()) {
  558. return base->has_script_signal(p_signal);
  559. }
  560. #ifdef TOOLS_ENABLED
  561. else if (base_cache.is_valid()){
  562. return base_cache->has_script_signal(p_signal);
  563. }
  564. #endif
  565. return false;
  566. }
  567. void GDScript::get_script_signal_list(List<MethodInfo> *r_signals) const {
  568. for(const Map<StringName,Vector<StringName> >::Element *E=_signals.front();E;E=E->next()) {
  569. MethodInfo mi;
  570. mi.name=E->key();
  571. for(int i=0;i<E->get().size();i++) {
  572. PropertyInfo arg;
  573. arg.name=E->get()[i];
  574. mi.arguments.push_back(arg);
  575. }
  576. r_signals->push_back(mi);
  577. }
  578. if (base.is_valid()) {
  579. base->get_script_signal_list(r_signals);
  580. }
  581. #ifdef TOOLS_ENABLED
  582. else if (base_cache.is_valid()){
  583. base_cache->get_script_signal_list(r_signals);
  584. }
  585. #endif
  586. }
  587. GDScript::GDScript() {
  588. _static_ref=this;
  589. valid=false;
  590. subclass_count=0;
  591. initializer=NULL;
  592. _base=NULL;
  593. _owner=NULL;
  594. tool=false;
  595. #ifdef TOOLS_ENABLED
  596. source_changed_cache=false;
  597. #endif
  598. }
  599. GDScript::~GDScript() {
  600. for (Map<StringName,GDFunction*>::Element *E=member_functions.front();E;E=E->next()) {
  601. memdelete( E->get() );
  602. }
  603. }
  604. //////////////////////////////
  605. // INSTANCE //
  606. //////////////////////////////
  607. bool GDInstance::set(const StringName& p_name, const Variant& p_value) {
  608. //member
  609. {
  610. const Map<StringName,GDScript::MemberInfo>::Element *E = script->member_indices.find(p_name);
  611. if (E) {
  612. if (E->get().setter) {
  613. const Variant *val=&p_value;
  614. Variant::CallError err;
  615. call(E->get().setter,&val,1,err);
  616. if (err.error==Variant::CallError::CALL_OK) {
  617. return true; //function exists, call was successful
  618. }
  619. }
  620. else
  621. members[E->get().index] = p_value;
  622. return true;
  623. }
  624. }
  625. GDScript *sptr=script.ptr();
  626. while(sptr) {
  627. Map<StringName,GDFunction*>::Element *E = sptr->member_functions.find(GDScriptLanguage::get_singleton()->strings._set);
  628. if (E) {
  629. Variant name=p_name;
  630. const Variant *args[2]={&name,&p_value};
  631. Variant::CallError err;
  632. Variant ret = E->get()->call(this,(const Variant**)args,2,err);
  633. if (err.error==Variant::CallError::CALL_OK && ret.get_type()==Variant::BOOL && ret.operator bool())
  634. return true;
  635. }
  636. sptr = sptr->_base;
  637. }
  638. return false;
  639. }
  640. bool GDInstance::get(const StringName& p_name, Variant &r_ret) const {
  641. const GDScript *sptr=script.ptr();
  642. while(sptr) {
  643. {
  644. const Map<StringName,GDScript::MemberInfo>::Element *E = script->member_indices.find(p_name);
  645. if (E) {
  646. if (E->get().getter) {
  647. Variant::CallError err;
  648. r_ret=const_cast<GDInstance*>(this)->call(E->get().getter,NULL,0,err);
  649. if (err.error==Variant::CallError::CALL_OK) {
  650. return true;
  651. }
  652. }
  653. r_ret=members[E->get().index];
  654. return true; //index found
  655. }
  656. }
  657. {
  658. const Map<StringName,Variant>::Element *E = script->constants.find(p_name);
  659. if (E) {
  660. r_ret=E->get();
  661. return true; //index found
  662. }
  663. }
  664. {
  665. const Map<StringName,GDFunction*>::Element *E = sptr->member_functions.find(GDScriptLanguage::get_singleton()->strings._get);
  666. if (E) {
  667. Variant name=p_name;
  668. const Variant *args[1]={&name};
  669. Variant::CallError err;
  670. Variant ret = const_cast<GDFunction*>(E->get())->call(const_cast<GDInstance*>(this),(const Variant**)args,1,err);
  671. if (err.error==Variant::CallError::CALL_OK && ret.get_type()!=Variant::NIL) {
  672. r_ret=ret;
  673. return true;
  674. }
  675. }
  676. }
  677. sptr = sptr->_base;
  678. }
  679. return false;
  680. }
  681. Variant::Type GDInstance::get_property_type(const StringName& p_name,bool *r_is_valid) const {
  682. const GDScript *sptr=script.ptr();
  683. while(sptr) {
  684. if (sptr->member_info.has(p_name)) {
  685. if (r_is_valid)
  686. *r_is_valid=true;
  687. return sptr->member_info[p_name].type;
  688. }
  689. sptr = sptr->_base;
  690. }
  691. if (r_is_valid)
  692. *r_is_valid=false;
  693. return Variant::NIL;
  694. }
  695. void GDInstance::get_property_list(List<PropertyInfo> *p_properties) const {
  696. // exported members, not doen yet!
  697. const GDScript *sptr=script.ptr();
  698. List<PropertyInfo> props;
  699. while(sptr) {
  700. const Map<StringName,GDFunction*>::Element *E = sptr->member_functions.find(GDScriptLanguage::get_singleton()->strings._get_property_list);
  701. if (E) {
  702. Variant::CallError err;
  703. Variant ret = const_cast<GDFunction*>(E->get())->call(const_cast<GDInstance*>(this),NULL,0,err);
  704. if (err.error==Variant::CallError::CALL_OK) {
  705. if (ret.get_type()!=Variant::ARRAY) {
  706. ERR_EXPLAIN("Wrong type for _get_property list, must be an array of dictionaries.");
  707. ERR_FAIL();
  708. }
  709. Array arr = ret;
  710. for(int i=0;i<arr.size();i++) {
  711. Dictionary d = arr[i];
  712. ERR_CONTINUE(!d.has("name"));
  713. ERR_CONTINUE(!d.has("type"));
  714. PropertyInfo pinfo;
  715. pinfo.type = Variant::Type( d["type"].operator int());
  716. ERR_CONTINUE(pinfo.type<0 || pinfo.type>=Variant::VARIANT_MAX );
  717. pinfo.name = d["name"];
  718. ERR_CONTINUE(pinfo.name=="");
  719. if (d.has("hint"))
  720. pinfo.hint=PropertyHint(d["hint"].operator int());
  721. if (d.has("hint_string"))
  722. pinfo.hint_string=d["hint_string"];
  723. if (d.has("usage"))
  724. pinfo.usage=d["usage"];
  725. props.push_back(pinfo);
  726. }
  727. }
  728. }
  729. //instance a fake script for editing the values
  730. Vector<_GDScriptMemberSort> msort;
  731. for(Map<StringName,PropertyInfo>::Element *E=sptr->member_info.front();E;E=E->next()) {
  732. _GDScriptMemberSort ms;
  733. ERR_CONTINUE(!sptr->member_indices.has(E->key()));
  734. ms.index=sptr->member_indices[E->key()].index;
  735. ms.name=E->key();
  736. msort.push_back(ms);
  737. }
  738. msort.sort();
  739. msort.invert();
  740. for(int i=0;i<msort.size();i++) {
  741. props.push_front(sptr->member_info[msort[i].name]);
  742. }
  743. #if 0
  744. if (sptr->member_functions.has("_get_property_list")) {
  745. Variant::CallError err;
  746. GDFunction *f = const_cast<GDFunction*>(sptr->member_functions["_get_property_list"]);
  747. Variant plv = f->call(const_cast<GDInstance*>(this),NULL,0,err);
  748. if (plv.get_type()!=Variant::ARRAY) {
  749. ERR_PRINT("_get_property_list: expected array returned");
  750. } else {
  751. Array pl=plv;
  752. for(int i=0;i<pl.size();i++) {
  753. Dictionary p = pl[i];
  754. PropertyInfo pinfo;
  755. if (!p.has("name")) {
  756. ERR_PRINT("_get_property_list: expected 'name' key of type string.")
  757. continue;
  758. }
  759. if (!p.has("type")) {
  760. ERR_PRINT("_get_property_list: expected 'type' key of type integer.")
  761. continue;
  762. }
  763. pinfo.name=p["name"];
  764. pinfo.type=Variant::Type(int(p["type"]));
  765. if (p.has("hint"))
  766. pinfo.hint=PropertyHint(int(p["hint"]));
  767. if (p.has("hint_string"))
  768. pinfo.hint_string=p["hint_string"];
  769. if (p.has("usage"))
  770. pinfo.usage=p["usage"];
  771. props.push_back(pinfo);
  772. }
  773. }
  774. }
  775. #endif
  776. sptr = sptr->_base;
  777. }
  778. //props.invert();
  779. for (List<PropertyInfo>::Element *E=props.front();E;E=E->next()) {
  780. p_properties->push_back(E->get());
  781. }
  782. }
  783. void GDInstance::get_method_list(List<MethodInfo> *p_list) const {
  784. const GDScript *sptr=script.ptr();
  785. while(sptr) {
  786. for (Map<StringName,GDFunction*>::Element *E = sptr->member_functions.front();E;E=E->next()) {
  787. MethodInfo mi;
  788. mi.name=E->key();
  789. mi.flags|=METHOD_FLAG_FROM_SCRIPT;
  790. for(int i=0;i<E->get()->get_argument_count();i++)
  791. mi.arguments.push_back(PropertyInfo(Variant::NIL,"arg"+itos(i)));
  792. p_list->push_back(mi);
  793. }
  794. sptr = sptr->_base;
  795. }
  796. }
  797. bool GDInstance::has_method(const StringName& p_method) const {
  798. const GDScript *sptr=script.ptr();
  799. while(sptr) {
  800. const Map<StringName,GDFunction*>::Element *E = sptr->member_functions.find(p_method);
  801. if (E)
  802. return true;
  803. sptr = sptr->_base;
  804. }
  805. return false;
  806. }
  807. Variant GDInstance::call(const StringName& p_method,const Variant** p_args,int p_argcount,Variant::CallError &r_error) {
  808. //printf("calling %ls:%i method %ls\n", script->get_path().c_str(), -1, String(p_method).c_str());
  809. GDScript *sptr=script.ptr();
  810. while(sptr) {
  811. Map<StringName,GDFunction*>::Element *E = sptr->member_functions.find(p_method);
  812. if (E) {
  813. return E->get()->call(this,p_args,p_argcount,r_error);
  814. }
  815. sptr = sptr->_base;
  816. }
  817. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  818. return Variant();
  819. }
  820. void GDInstance::call_multilevel(const StringName& p_method,const Variant** p_args,int p_argcount) {
  821. GDScript *sptr=script.ptr();
  822. Variant::CallError ce;
  823. while(sptr) {
  824. Map<StringName,GDFunction*>::Element *E = sptr->member_functions.find(p_method);
  825. if (E) {
  826. E->get()->call(this,p_args,p_argcount,ce);
  827. }
  828. sptr = sptr->_base;
  829. }
  830. }
  831. void GDInstance::_ml_call_reversed(GDScript *sptr,const StringName& p_method,const Variant** p_args,int p_argcount) {
  832. if (sptr->_base)
  833. _ml_call_reversed(sptr->_base,p_method,p_args,p_argcount);
  834. Variant::CallError ce;
  835. Map<StringName,GDFunction*>::Element *E = sptr->member_functions.find(p_method);
  836. if (E) {
  837. E->get()->call(this,p_args,p_argcount,ce);
  838. }
  839. }
  840. void GDInstance::call_multilevel_reversed(const StringName& p_method,const Variant** p_args,int p_argcount) {
  841. if (script.ptr()) {
  842. _ml_call_reversed(script.ptr(),p_method,p_args,p_argcount);
  843. }
  844. }
  845. void GDInstance::notification(int p_notification) {
  846. //notification is not virutal, it gets called at ALL levels just like in C.
  847. Variant value=p_notification;
  848. const Variant *args[1]={&value };
  849. GDScript *sptr=script.ptr();
  850. while(sptr) {
  851. Map<StringName,GDFunction*>::Element *E = sptr->member_functions.find(GDScriptLanguage::get_singleton()->strings._notification);
  852. if (E) {
  853. Variant::CallError err;
  854. E->get()->call(this,args,1,err);
  855. if (err.error!=Variant::CallError::CALL_OK) {
  856. //print error about notification call
  857. }
  858. }
  859. sptr = sptr->_base;
  860. }
  861. }
  862. Ref<Script> GDInstance::get_script() const {
  863. return script;
  864. }
  865. ScriptLanguage *GDInstance::get_language() {
  866. return GDScriptLanguage::get_singleton();
  867. }
  868. GDInstance::GDInstance() {
  869. owner=NULL;
  870. base_ref=false;
  871. }
  872. GDInstance::~GDInstance() {
  873. if (script.is_valid() && owner) {
  874. script->instances.erase(owner);
  875. }
  876. }
  877. /************* SCRIPT LANGUAGE **************/
  878. /************* SCRIPT LANGUAGE **************/
  879. /************* SCRIPT LANGUAGE **************/
  880. /************* SCRIPT LANGUAGE **************/
  881. /************* SCRIPT LANGUAGE **************/
  882. GDScriptLanguage *GDScriptLanguage::singleton=NULL;
  883. String GDScriptLanguage::get_name() const {
  884. return "GDScript";
  885. }
  886. /* LANGUAGE FUNCTIONS */
  887. void GDScriptLanguage::_add_global(const StringName& p_name,const Variant& p_value) {
  888. if (globals.has(p_name)) {
  889. //overwrite existing
  890. global_array[globals[p_name]]=p_value;
  891. return;
  892. }
  893. globals[p_name]=global_array.size();
  894. global_array.push_back(p_value);
  895. _global_array=global_array.ptr();
  896. }
  897. void GDScriptLanguage::add_global_constant(const StringName& p_variable,const Variant& p_value) {
  898. _add_global(p_variable,p_value);
  899. }
  900. void GDScriptLanguage::init() {
  901. //populate global constants
  902. int gcc=GlobalConstants::get_global_constant_count();
  903. for(int i=0;i<gcc;i++) {
  904. _add_global(StaticCString::create(GlobalConstants::get_global_constant_name(i)),GlobalConstants::get_global_constant_value(i));
  905. }
  906. _add_global(StaticCString::create("PI"),Math_PI);
  907. //populate native classes
  908. List<StringName> class_list;
  909. ObjectTypeDB::get_type_list(&class_list);
  910. for(List<StringName>::Element *E=class_list.front();E;E=E->next()) {
  911. StringName n = E->get();
  912. String s = String(n);
  913. if (s.begins_with("_"))
  914. n=s.substr(1,s.length());
  915. if (globals.has(n))
  916. continue;
  917. Ref<GDNativeClass> nc = memnew( GDNativeClass(E->get()) );
  918. _add_global(n,nc);
  919. }
  920. //populate singletons
  921. List<Globals::Singleton> singletons;
  922. Globals::get_singleton()->get_singletons(&singletons);
  923. for(List<Globals::Singleton>::Element *E=singletons.front();E;E=E->next()) {
  924. _add_global(E->get().name,E->get().ptr);
  925. }
  926. }
  927. String GDScriptLanguage::get_type() const {
  928. return "GDScript";
  929. }
  930. String GDScriptLanguage::get_extension() const {
  931. return "gd";
  932. }
  933. Error GDScriptLanguage::execute_file(const String& p_path) {
  934. // ??
  935. return OK;
  936. }
  937. void GDScriptLanguage::finish() {
  938. }
  939. void GDScriptLanguage::profiling_start() {
  940. #ifdef DEBUG_ENABLED
  941. if (lock) {
  942. lock->lock();
  943. }
  944. SelfList<GDFunction> *elem=function_list.first();
  945. while(elem) {
  946. elem->self()->profile.call_count=0;
  947. elem->self()->profile.self_time=0;
  948. elem->self()->profile.total_time=0;
  949. elem->self()->profile.frame_call_count=0;
  950. elem->self()->profile.frame_self_time=0;
  951. elem->self()->profile.frame_total_time=0;
  952. elem->self()->profile.last_frame_call_count=0;
  953. elem->self()->profile.last_frame_self_time=0;
  954. elem->self()->profile.last_frame_total_time=0;
  955. elem=elem->next();
  956. }
  957. profiling=true;
  958. if (lock) {
  959. lock->unlock();
  960. }
  961. #endif
  962. }
  963. void GDScriptLanguage::profiling_stop() {
  964. #ifdef DEBUG_ENABLED
  965. if (lock) {
  966. lock->lock();
  967. }
  968. profiling=false;
  969. if (lock) {
  970. lock->unlock();
  971. }
  972. #endif
  973. }
  974. int GDScriptLanguage::profiling_get_accumulated_data(ProfilingInfo *p_info_arr,int p_info_max) {
  975. int current=0;
  976. #ifdef DEBUG_ENABLED
  977. if (lock) {
  978. lock->lock();
  979. }
  980. SelfList<GDFunction> *elem=function_list.first();
  981. while(elem) {
  982. if (current>=p_info_max)
  983. break;
  984. p_info_arr[current].call_count=elem->self()->profile.call_count;
  985. p_info_arr[current].self_time=elem->self()->profile.self_time;
  986. p_info_arr[current].total_time=elem->self()->profile.total_time;
  987. p_info_arr[current].signature=elem->self()->profile.signature;
  988. elem=elem->next();
  989. current++;
  990. }
  991. if (lock) {
  992. lock->unlock();
  993. }
  994. #endif
  995. return current;
  996. }
  997. int GDScriptLanguage::profiling_get_frame_data(ProfilingInfo *p_info_arr,int p_info_max) {
  998. int current=0;
  999. #ifdef DEBUG_ENABLED
  1000. if (lock) {
  1001. lock->lock();
  1002. }
  1003. SelfList<GDFunction> *elem=function_list.first();
  1004. while(elem) {
  1005. if (current>=p_info_max)
  1006. break;
  1007. if (elem->self()->profile.last_frame_call_count>0) {
  1008. p_info_arr[current].call_count=elem->self()->profile.last_frame_call_count;
  1009. p_info_arr[current].self_time=elem->self()->profile.last_frame_self_time;
  1010. p_info_arr[current].total_time=elem->self()->profile.last_frame_total_time;
  1011. p_info_arr[current].signature=elem->self()->profile.signature;
  1012. //print_line(String(elem->self()->profile.signature)+": "+itos(elem->self()->profile.last_frame_call_count));
  1013. current++;
  1014. }
  1015. elem=elem->next();
  1016. }
  1017. if (lock) {
  1018. lock->unlock();
  1019. }
  1020. #endif
  1021. return current;
  1022. }
  1023. void GDScriptLanguage::frame() {
  1024. // print_line("calls: "+itos(calls));
  1025. calls=0;
  1026. #ifdef DEBUG_ENABLED
  1027. if (profiling) {
  1028. if (lock) {
  1029. lock->lock();
  1030. }
  1031. SelfList<GDFunction> *elem=function_list.first();
  1032. while(elem) {
  1033. elem->self()->profile.last_frame_call_count=elem->self()->profile.frame_call_count;
  1034. elem->self()->profile.last_frame_self_time=elem->self()->profile.frame_self_time;
  1035. elem->self()->profile.last_frame_total_time=elem->self()->profile.frame_total_time;
  1036. elem->self()->profile.frame_call_count=0;
  1037. elem->self()->profile.frame_self_time=0;
  1038. elem->self()->profile.frame_total_time=0;
  1039. elem=elem->next();
  1040. }
  1041. if (lock) {
  1042. lock->unlock();
  1043. }
  1044. }
  1045. #endif
  1046. }
  1047. /* EDITOR FUNCTIONS */
  1048. void GDScriptLanguage::get_reserved_words(List<String> *p_words) const {
  1049. static const char *_reserved_words[]={
  1050. // operators
  1051. "and",
  1052. "in",
  1053. "not",
  1054. "or",
  1055. // types and values
  1056. "false",
  1057. "float",
  1058. "int",
  1059. "null",
  1060. "PI",
  1061. "self",
  1062. "true",
  1063. // functions
  1064. "assert",
  1065. "breakpoint",
  1066. "class",
  1067. "extends",
  1068. "func",
  1069. "preload",
  1070. "setget",
  1071. "signal",
  1072. "tool",
  1073. "yield",
  1074. // var
  1075. "const",
  1076. "enum",
  1077. "export",
  1078. "onready",
  1079. "static",
  1080. "var",
  1081. // control flow
  1082. "break",
  1083. "continue",
  1084. "if",
  1085. "elif",
  1086. "else",
  1087. "for",
  1088. "pass",
  1089. "return",
  1090. "while",
  1091. 0};
  1092. const char **w=_reserved_words;
  1093. while (*w) {
  1094. p_words->push_back(*w);
  1095. w++;
  1096. }
  1097. for(int i=0;i<GDFunctions::FUNC_MAX;i++) {
  1098. p_words->push_back(GDFunctions::get_func_name(GDFunctions::Function(i)));
  1099. }
  1100. }
  1101. GDScriptLanguage::GDScriptLanguage() {
  1102. calls=0;
  1103. ERR_FAIL_COND(singleton);
  1104. singleton=this;
  1105. strings._init = StaticCString::create("_init");
  1106. strings._notification = StaticCString::create("_notification");
  1107. strings._set= StaticCString::create("_set");
  1108. strings._get= StaticCString::create("_get");
  1109. strings._get_property_list= StaticCString::create("_get_property_list");
  1110. strings._script_source=StaticCString::create("script/source");
  1111. _debug_parse_err_line=-1;
  1112. _debug_parse_err_file="";
  1113. #ifdef NO_THREADS
  1114. lock=NULL;
  1115. #else
  1116. lock = Mutex::create();
  1117. #endif
  1118. profiling=false;
  1119. script_frame_time=0;
  1120. _debug_call_stack_pos=0;
  1121. int dmcs=GLOBAL_DEF("debug/script_max_call_stack",1024);
  1122. if (ScriptDebugger::get_singleton()) {
  1123. //debugging enabled!
  1124. _debug_max_call_stack = dmcs;
  1125. if (_debug_max_call_stack<1024)
  1126. _debug_max_call_stack=1024;
  1127. _call_stack = memnew_arr( CallLevel, _debug_max_call_stack+1 );
  1128. } else {
  1129. _debug_max_call_stack=0;
  1130. _call_stack=NULL;
  1131. }
  1132. }
  1133. GDScriptLanguage::~GDScriptLanguage() {
  1134. if (lock) {
  1135. memdelete(lock);
  1136. lock=NULL;
  1137. }
  1138. if (_call_stack) {
  1139. memdelete_arr(_call_stack);
  1140. }
  1141. singleton=NULL;
  1142. }
  1143. /*************** RESOURCE ***************/
  1144. RES ResourceFormatLoaderGDScript::load(const String &p_path, const String& p_original_path, Error *r_error) {
  1145. if (r_error)
  1146. *r_error=ERR_FILE_CANT_OPEN;
  1147. GDScript *script = memnew( GDScript );
  1148. Ref<GDScript> scriptres(script);
  1149. if (p_path.ends_with(".gde") || p_path.ends_with(".gdc")) {
  1150. script->set_script_path(p_original_path); // script needs this.
  1151. script->set_path(p_original_path);
  1152. Error err = script->load_byte_code(p_path);
  1153. if (err!=OK) {
  1154. ERR_FAIL_COND_V(err!=OK, RES());
  1155. }
  1156. } else {
  1157. Error err = script->load_source_code(p_path);
  1158. if (err!=OK) {
  1159. ERR_FAIL_COND_V(err!=OK, RES());
  1160. }
  1161. script->set_script_path(p_original_path); // script needs this.
  1162. script->set_path(p_original_path);
  1163. //script->set_name(p_path.get_file());
  1164. script->reload();
  1165. }
  1166. if (r_error)
  1167. *r_error=OK;
  1168. return scriptres;
  1169. }
  1170. void ResourceFormatLoaderGDScript::get_recognized_extensions(List<String> *p_extensions) const {
  1171. p_extensions->push_back("gd");
  1172. p_extensions->push_back("gdc");
  1173. p_extensions->push_back("gde");
  1174. }
  1175. bool ResourceFormatLoaderGDScript::handles_type(const String& p_type) const {
  1176. return (p_type=="Script" || p_type=="GDScript");
  1177. }
  1178. String ResourceFormatLoaderGDScript::get_resource_type(const String &p_path) const {
  1179. String el = p_path.extension().to_lower();
  1180. if (el=="gd" || el=="gdc" || el=="gde")
  1181. return "GDScript";
  1182. return "";
  1183. }
  1184. Error ResourceFormatSaverGDScript::save(const String &p_path,const RES& p_resource,uint32_t p_flags) {
  1185. Ref<GDScript> sqscr = p_resource;
  1186. ERR_FAIL_COND_V(sqscr.is_null(),ERR_INVALID_PARAMETER);
  1187. String source = sqscr->get_source_code();
  1188. Error err;
  1189. FileAccess *file = FileAccess::open(p_path,FileAccess::WRITE,&err);
  1190. if (err) {
  1191. ERR_FAIL_COND_V(err,err);
  1192. }
  1193. file->store_string(source);
  1194. if (file->get_error()!=OK && file->get_error()!=ERR_FILE_EOF) {
  1195. memdelete(file);
  1196. return ERR_CANT_CREATE;
  1197. }
  1198. file->close();
  1199. memdelete(file);
  1200. return OK;
  1201. }
  1202. void ResourceFormatSaverGDScript::get_recognized_extensions(const RES& p_resource,List<String> *p_extensions) const {
  1203. if (p_resource->cast_to<GDScript>()) {
  1204. p_extensions->push_back("gd");
  1205. }
  1206. }
  1207. bool ResourceFormatSaverGDScript::recognize(const RES& p_resource) const {
  1208. return p_resource->cast_to<GDScript>()!=NULL;
  1209. }