csharp_script.cpp 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107
  1. /*************************************************************************/
  2. /* csharp_script.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "csharp_script.h"
  31. #include <mono/metadata/threads.h>
  32. #include "os/file_access.h"
  33. #include "os/os.h"
  34. #include "os/thread.h"
  35. #include "project_settings.h"
  36. #ifdef TOOLS_ENABLED
  37. #include "editor/bindings_generator.h"
  38. #include "editor/csharp_project.h"
  39. #include "editor/editor_node.h"
  40. #include "editor/godotsharp_editor.h"
  41. #include "utils/string_utils.h"
  42. #endif
  43. #include "godotsharp_dirs.h"
  44. #include "mono_gd/gd_mono_class.h"
  45. #include "mono_gd/gd_mono_marshal.h"
  46. #include "signal_awaiter_utils.h"
  47. #define CACHED_STRING_NAME(m_var) (CSharpLanguage::get_singleton()->get_string_names().m_var)
  48. #ifdef TOOLS_ENABLED
  49. static bool _create_project_solution_if_needed() {
  50. String sln_path = GodotSharpDirs::get_project_sln_path();
  51. String csproj_path = GodotSharpDirs::get_project_csproj_path();
  52. if (!FileAccess::exists(sln_path) || !FileAccess::exists(csproj_path)) {
  53. // A solution does not yet exist, create a new one
  54. CRASH_COND(GodotSharpEditor::get_singleton() == NULL);
  55. return GodotSharpEditor::get_singleton()->call("_create_project_solution");
  56. }
  57. return true;
  58. }
  59. #endif
  60. CSharpLanguage *CSharpLanguage::singleton = NULL;
  61. String CSharpLanguage::get_name() const {
  62. return "C#";
  63. }
  64. String CSharpLanguage::get_type() const {
  65. return "CSharpScript";
  66. }
  67. String CSharpLanguage::get_extension() const {
  68. return "cs";
  69. }
  70. Error CSharpLanguage::execute_file(const String &p_path) {
  71. // ??
  72. return OK;
  73. }
  74. #ifdef TOOLS_ENABLED
  75. void gdsharp_editor_init_callback() {
  76. EditorNode *editor = EditorNode::get_singleton();
  77. editor->add_child(memnew(GodotSharpEditor(editor)));
  78. }
  79. #endif
  80. void CSharpLanguage::init() {
  81. gdmono = memnew(GDMono);
  82. gdmono->initialize();
  83. #ifdef MONO_GLUE_DISABLED
  84. WARN_PRINT("This binary is built with `mono_glue=no` and cannot be used for scripting");
  85. #endif
  86. #if defined(TOOLS_ENABLED) && defined(DEBUG_METHODS_ENABLED)
  87. if (gdmono->get_editor_tools_assembly() != NULL) {
  88. List<String> cmdline_args = OS::get_singleton()->get_cmdline_args();
  89. BindingsGenerator::handle_cmdline_args(cmdline_args);
  90. }
  91. #endif
  92. #ifdef TOOLS_ENABLED
  93. EditorNode::add_init_callback(&gdsharp_editor_init_callback);
  94. #endif
  95. }
  96. void CSharpLanguage::finish() {
  97. finalizing = true;
  98. #ifdef TOOLS_ENABLED
  99. // Must be here, to avoid StringName leaks
  100. if (BindingsGenerator::singleton) {
  101. memdelete(BindingsGenerator::singleton);
  102. BindingsGenerator::singleton = NULL;
  103. }
  104. #endif
  105. // Release gchandle bindings before finalizing mono runtime
  106. gchandle_bindings.clear();
  107. if (gdmono) {
  108. memdelete(gdmono);
  109. gdmono = NULL;
  110. }
  111. finalizing = false;
  112. }
  113. void CSharpLanguage::get_reserved_words(List<String> *p_words) const {
  114. static const char *_reserved_words[] = {
  115. // Reserved keywords
  116. "abstract",
  117. "as",
  118. "base",
  119. "bool",
  120. "break",
  121. "byte",
  122. "case",
  123. "catch",
  124. "char",
  125. "checked",
  126. "class",
  127. "const",
  128. "continue",
  129. "decimal",
  130. "default",
  131. "delegate",
  132. "do",
  133. "double",
  134. "else",
  135. "enum",
  136. "event",
  137. "explicit",
  138. "extern",
  139. "false",
  140. "finally",
  141. "fixed",
  142. "float",
  143. "for",
  144. "forech",
  145. "goto",
  146. "if",
  147. "implicit",
  148. "in",
  149. "int",
  150. "interface",
  151. "internal",
  152. "is",
  153. "lock",
  154. "long",
  155. "namespace",
  156. "new",
  157. "null",
  158. "object",
  159. "operator",
  160. "out",
  161. "override",
  162. "params",
  163. "private",
  164. "protected",
  165. "public",
  166. "readonly",
  167. "ref",
  168. "return",
  169. "sbyte",
  170. "sealed",
  171. "short",
  172. "sizeof",
  173. "stackalloc",
  174. "static",
  175. "string",
  176. "struct",
  177. "switch",
  178. "this",
  179. "throw",
  180. "true",
  181. "try",
  182. "typeof",
  183. "uint",
  184. "ulong",
  185. "unchecked",
  186. "unsafe",
  187. "ushort",
  188. "using",
  189. "virtual",
  190. "volatile",
  191. "void",
  192. "while",
  193. // Contextual keywords. Not reserved words, but I guess we should include
  194. // them because this seems to be used only for syntax highlighting.
  195. "add",
  196. "ascending",
  197. "by",
  198. "descending",
  199. "dynamic",
  200. "equals",
  201. "from",
  202. "get",
  203. "global",
  204. "group",
  205. "in",
  206. "into",
  207. "join",
  208. "let",
  209. "on",
  210. "orderby",
  211. "partial",
  212. "remove",
  213. "select",
  214. "set",
  215. "value",
  216. "var",
  217. "where",
  218. "yield",
  219. 0
  220. };
  221. const char **w = _reserved_words;
  222. while (*w) {
  223. p_words->push_back(*w);
  224. w++;
  225. }
  226. }
  227. void CSharpLanguage::get_comment_delimiters(List<String> *p_delimiters) const {
  228. p_delimiters->push_back("//"); // single-line comment
  229. p_delimiters->push_back("/* */"); // delimited comment
  230. }
  231. void CSharpLanguage::get_string_delimiters(List<String> *p_delimiters) const {
  232. p_delimiters->push_back("' '"); // character literal
  233. p_delimiters->push_back("\" \""); // regular string literal
  234. p_delimiters->push_back("@\" \""); // verbatim string literal
  235. }
  236. Ref<Script> CSharpLanguage::get_template(const String &p_class_name, const String &p_base_class_name) const {
  237. String script_template = "using " BINDINGS_NAMESPACE ";\n"
  238. "using System;\n"
  239. "\n"
  240. "public class %CLASS_NAME% : %BASE_CLASS_NAME%\n"
  241. "{\n"
  242. " // Member variables here, example:\n"
  243. " // private int a = 2;\n"
  244. " // private string b = \"textvar\";\n"
  245. "\n"
  246. " public override void _Ready()\n"
  247. " {\n"
  248. " // Called every time the node is added to the scene.\n"
  249. " // Initialization here\n"
  250. " \n"
  251. " }\n"
  252. "\n"
  253. "// public override void _Process(float delta)\n"
  254. "// {\n"
  255. "// // Called every frame. Delta is time since last frame.\n"
  256. "// // Update game logic here.\n"
  257. "// \n"
  258. "// }\n"
  259. "}\n";
  260. script_template = script_template.replace("%BASE_CLASS_NAME%", p_base_class_name)
  261. .replace("%CLASS_NAME%", p_class_name);
  262. Ref<CSharpScript> script;
  263. script.instance();
  264. script->set_source_code(script_template);
  265. script->set_name(p_class_name);
  266. return script;
  267. }
  268. bool CSharpLanguage::is_using_templates() {
  269. return true;
  270. }
  271. void CSharpLanguage::make_template(const String &p_class_name, const String &p_base_class_name, Ref<Script> &p_script) {
  272. String src = p_script->get_source_code();
  273. src = src.replace("%BASE%", p_base_class_name)
  274. .replace("%CLASS%", p_class_name)
  275. .replace("%TS%", _get_indentation());
  276. p_script->set_source_code(src);
  277. }
  278. Script *CSharpLanguage::create_script() const {
  279. return memnew(CSharpScript);
  280. }
  281. bool CSharpLanguage::has_named_classes() const {
  282. return false;
  283. }
  284. bool CSharpLanguage::supports_builtin_mode() const {
  285. return false;
  286. }
  287. static String variant_type_to_managed_name(const String &p_var_type_name) {
  288. if (p_var_type_name.empty())
  289. return "object";
  290. if (!ClassDB::class_exists(p_var_type_name)) {
  291. Variant::Type var_types[] = {
  292. Variant::BOOL,
  293. Variant::INT,
  294. Variant::REAL,
  295. Variant::STRING,
  296. Variant::VECTOR2,
  297. Variant::RECT2,
  298. Variant::VECTOR3,
  299. Variant::TRANSFORM2D,
  300. Variant::PLANE,
  301. Variant::QUAT,
  302. Variant::AABB,
  303. Variant::BASIS,
  304. Variant::TRANSFORM,
  305. Variant::COLOR,
  306. Variant::NODE_PATH,
  307. Variant::_RID
  308. };
  309. for (int i = 0; i < sizeof(var_types) / sizeof(Variant::Type); i++) {
  310. if (p_var_type_name == Variant::get_type_name(var_types[i]))
  311. return p_var_type_name;
  312. }
  313. if (p_var_type_name == "String")
  314. return "string"; // I prefer this one >:[
  315. // TODO these will be rewritten later into custom containers
  316. if (p_var_type_name == "Array")
  317. return "object[]";
  318. if (p_var_type_name == "Dictionary")
  319. return "Dictionary<object, object>";
  320. if (p_var_type_name == "PoolByteArray")
  321. return "byte[]";
  322. if (p_var_type_name == "PoolIntArray")
  323. return "int[]";
  324. if (p_var_type_name == "PoolRealArray")
  325. return "float[]";
  326. if (p_var_type_name == "PoolStringArray")
  327. return "string[]";
  328. if (p_var_type_name == "PoolVector2Array")
  329. return "Vector2[]";
  330. if (p_var_type_name == "PoolVector3Array")
  331. return "Vector3[]";
  332. if (p_var_type_name == "PoolColorArray")
  333. return "Color[]";
  334. return "object";
  335. }
  336. return p_var_type_name;
  337. }
  338. String CSharpLanguage::make_function(const String &p_class, const String &p_name, const PoolStringArray &p_args) const {
  339. #ifdef TOOLS_ENABLED
  340. // FIXME
  341. // - Due to Godot's API limitation this just appends the function to the end of the file
  342. // - Use fully qualified name if there is ambiguity
  343. String s = "private void " + p_name + "(";
  344. for (int i = 0; i < p_args.size(); i++) {
  345. const String &arg = p_args[i];
  346. if (i > 0)
  347. s += ", ";
  348. s += variant_type_to_managed_name(arg.get_slice(":", 1)) + " " + escape_csharp_keyword(arg.get_slice(":", 0));
  349. }
  350. s += ")\n{\n // Replace with function body\n}\n";
  351. return s;
  352. #else
  353. return String();
  354. #endif
  355. }
  356. String CSharpLanguage::_get_indentation() const {
  357. #ifdef TOOLS_ENABLED
  358. if (Engine::get_singleton()->is_editor_hint()) {
  359. bool use_space_indentation = EDITOR_DEF("text_editor/indent/type", 0);
  360. if (use_space_indentation) {
  361. int indent_size = EDITOR_DEF("text_editor/indent/size", 4);
  362. String space_indent = "";
  363. for (int i = 0; i < indent_size; i++) {
  364. space_indent += " ";
  365. }
  366. return space_indent;
  367. }
  368. }
  369. #endif
  370. return "\t";
  371. }
  372. void CSharpLanguage::frame() {
  373. const Ref<MonoGCHandle> &task_scheduler_handle = GDMonoUtils::mono_cache.task_scheduler_handle;
  374. if (task_scheduler_handle.is_valid()) {
  375. MonoObject *task_scheduler = task_scheduler_handle->get_target();
  376. if (task_scheduler) {
  377. GDMonoUtils::GodotTaskScheduler_Activate thunk = CACHED_METHOD_THUNK(GodotTaskScheduler, Activate);
  378. ERR_FAIL_NULL(thunk);
  379. MonoObject *ex;
  380. thunk(task_scheduler, &ex);
  381. if (ex) {
  382. mono_print_unhandled_exception(ex);
  383. ERR_FAIL();
  384. }
  385. }
  386. }
  387. }
  388. struct CSharpScriptDepSort {
  389. // must support sorting so inheritance works properly (parent must be reloaded first)
  390. bool operator()(const Ref<CSharpScript> &A, const Ref<CSharpScript> &B) const {
  391. if (A == B)
  392. return false; // shouldn't happen but..
  393. GDMonoClass *I = B->base;
  394. while (I) {
  395. if (I == A->script_class) {
  396. // A is a base of B
  397. return true;
  398. }
  399. I = I->get_parent_class();
  400. }
  401. return false; // not a base
  402. }
  403. };
  404. void CSharpLanguage::reload_all_scripts() {
  405. #ifdef DEBUG_ENABLED
  406. #ifndef NO_THREADS
  407. lock->lock();
  408. #endif
  409. List<Ref<CSharpScript> > scripts;
  410. SelfList<CSharpScript> *elem = script_list.first();
  411. while (elem) {
  412. if (elem->self()->get_path().is_resource_file()) {
  413. scripts.push_back(Ref<CSharpScript>(elem->self())); //cast to gdscript to avoid being erased by accident
  414. }
  415. elem = elem->next();
  416. }
  417. #ifndef NO_THREADS
  418. lock->unlock();
  419. #endif
  420. //as scripts are going to be reloaded, must proceed without locking here
  421. scripts.sort_custom<CSharpScriptDepSort>(); //update in inheritance dependency order
  422. for (List<Ref<CSharpScript> >::Element *E = scripts.front(); E; E = E->next()) {
  423. E->get()->load_source_code(E->get()->get_path());
  424. E->get()->reload(true);
  425. }
  426. #endif
  427. }
  428. void CSharpLanguage::reload_tool_script(const Ref<Script> &p_script, bool p_soft_reload) {
  429. (void)p_script; // UNUSED
  430. #ifdef TOOLS_ENABLED
  431. MonoReloadNode::get_singleton()->restart_reload_timer();
  432. reload_assemblies_if_needed(p_soft_reload);
  433. #endif
  434. }
  435. #ifdef TOOLS_ENABLED
  436. void CSharpLanguage::reload_assemblies_if_needed(bool p_soft_reload) {
  437. if (gdmono->is_runtime_initialized()) {
  438. GDMonoAssembly *proj_assembly = gdmono->get_project_assembly();
  439. String name = ProjectSettings::get_singleton()->get("application/config/name");
  440. if (name.empty()) {
  441. name = "UnnamedProject";
  442. }
  443. if (proj_assembly) {
  444. String proj_asm_path = proj_assembly->get_path();
  445. if (!FileAccess::exists(proj_assembly->get_path())) {
  446. // Maybe it wasn't loaded from the default path, so check this as well
  447. proj_asm_path = GodotSharpDirs::get_res_temp_assemblies_dir().plus_file(name);
  448. if (!FileAccess::exists(proj_asm_path))
  449. return; // No assembly to load
  450. }
  451. if (FileAccess::get_modified_time(proj_asm_path) <= proj_assembly->get_modified_time())
  452. return; // Already up to date
  453. } else {
  454. if (!FileAccess::exists(GodotSharpDirs::get_res_temp_assemblies_dir().plus_file(name)))
  455. return; // No assembly to load
  456. }
  457. }
  458. #ifndef NO_THREADS
  459. lock->lock();
  460. #endif
  461. List<Ref<CSharpScript> > scripts;
  462. SelfList<CSharpScript> *elem = script_list.first();
  463. while (elem) {
  464. if (elem->self()->get_path().is_resource_file()) {
  465. scripts.push_back(Ref<CSharpScript>(elem->self())); //cast to CSharpScript to avoid being erased by accident
  466. }
  467. elem = elem->next();
  468. }
  469. #ifndef NO_THREADS
  470. lock->unlock();
  471. #endif
  472. //when someone asks you why dynamically typed languages are easier to write....
  473. Map<Ref<CSharpScript>, Map<ObjectID, List<Pair<StringName, Variant> > > > to_reload;
  474. //as scripts are going to be reloaded, must proceed without locking here
  475. scripts.sort_custom<CSharpScriptDepSort>(); //update in inheritance dependency order
  476. for (List<Ref<CSharpScript> >::Element *E = scripts.front(); E; E = E->next()) {
  477. to_reload.insert(E->get(), Map<ObjectID, List<Pair<StringName, Variant> > >());
  478. if (!p_soft_reload) {
  479. //save state and remove script from instances
  480. Map<ObjectID, List<Pair<StringName, Variant> > > &map = to_reload[E->get()];
  481. while (E->get()->instances.front()) {
  482. Object *obj = E->get()->instances.front()->get();
  483. //save instance info
  484. List<Pair<StringName, Variant> > state;
  485. if (obj->get_script_instance()) {
  486. obj->get_script_instance()->get_property_state(state);
  487. Ref<MonoGCHandle> gchandle = CAST_CSHARP_INSTANCE(obj->get_script_instance())->gchandle;
  488. if (gchandle.is_valid())
  489. gchandle->release();
  490. map[obj->get_instance_id()] = state;
  491. obj->set_script(RefPtr());
  492. }
  493. }
  494. //same thing for placeholders
  495. while (E->get()->placeholders.size()) {
  496. Object *obj = E->get()->placeholders.front()->get()->get_owner();
  497. //save instance info
  498. List<Pair<StringName, Variant> > state;
  499. if (obj->get_script_instance()) {
  500. obj->get_script_instance()->get_property_state(state);
  501. map[obj->get_instance_id()] = state;
  502. obj->set_script(RefPtr());
  503. }
  504. }
  505. for (Map<ObjectID, List<Pair<StringName, Variant> > >::Element *F = E->get()->pending_reload_state.front(); F; F = F->next()) {
  506. map[F->key()] = F->get(); //pending to reload, use this one instead
  507. }
  508. E->get()->_clear();
  509. }
  510. }
  511. if (gdmono->reload_scripts_domain() != OK)
  512. return;
  513. for (Map<Ref<CSharpScript>, Map<ObjectID, List<Pair<StringName, Variant> > > >::Element *E = to_reload.front(); E; E = E->next()) {
  514. Ref<CSharpScript> scr = E->key();
  515. scr->exports_invalidated = true;
  516. scr->reload(p_soft_reload);
  517. scr->update_exports();
  518. //restore state if saved
  519. for (Map<ObjectID, List<Pair<StringName, Variant> > >::Element *F = E->get().front(); F; F = F->next()) {
  520. Object *obj = ObjectDB::get_instance(F->key());
  521. if (!obj)
  522. continue;
  523. if (!p_soft_reload) {
  524. //clear it just in case (may be a pending reload state)
  525. obj->set_script(RefPtr());
  526. }
  527. obj->set_script(scr.get_ref_ptr());
  528. if (!obj->get_script_instance()) {
  529. //failed, save reload state for next time if not saved
  530. if (!scr->pending_reload_state.has(obj->get_instance_id())) {
  531. scr->pending_reload_state[obj->get_instance_id()] = F->get();
  532. }
  533. continue;
  534. }
  535. for (List<Pair<StringName, Variant> >::Element *G = F->get().front(); G; G = G->next()) {
  536. obj->get_script_instance()->set(G->get().first, G->get().second);
  537. }
  538. scr->pending_reload_state.erase(obj->get_instance_id()); //as it reloaded, remove pending state
  539. }
  540. //if instance states were saved, set them!
  541. }
  542. if (Engine::get_singleton()->is_editor_hint())
  543. EditorNode::get_singleton()->get_property_editor()->update_tree();
  544. }
  545. #endif
  546. void CSharpLanguage::get_recognized_extensions(List<String> *p_extensions) const {
  547. p_extensions->push_back("cs");
  548. }
  549. #ifdef TOOLS_ENABLED
  550. Error CSharpLanguage::open_in_external_editor(const Ref<Script> &p_script, int p_line, int p_col) {
  551. return GodotSharpEditor::get_singleton()->open_in_external_editor(p_script, p_line, p_col);
  552. }
  553. bool CSharpLanguage::overrides_external_editor() {
  554. return GodotSharpEditor::get_singleton()->overrides_external_editor();
  555. }
  556. #endif
  557. void CSharpLanguage::thread_enter() {
  558. #if 0
  559. if (mono->is_runtime_initialized()) {
  560. GDMonoUtils::attach_current_thread();
  561. }
  562. #endif
  563. }
  564. void CSharpLanguage::thread_exit() {
  565. #if 0
  566. if (mono->is_runtime_initialized()) {
  567. GDMonoUtils::detach_current_thread();
  568. }
  569. #endif
  570. }
  571. bool CSharpLanguage::debug_break_parse(const String &p_file, int p_line, const String &p_error) {
  572. // Break because of parse error
  573. if (ScriptDebugger::get_singleton() && Thread::get_caller_id() == Thread::get_main_id()) {
  574. // TODO
  575. //_debug_parse_err_line = p_line;
  576. //_debug_parse_err_file = p_file;
  577. //_debug_error = p_error;
  578. ScriptDebugger::get_singleton()->debug(this, false);
  579. return true;
  580. } else {
  581. return false;
  582. }
  583. }
  584. bool CSharpLanguage::debug_break(const String &p_error, bool p_allow_continue) {
  585. if (ScriptDebugger::get_singleton() && Thread::get_caller_id() == Thread::get_main_id()) {
  586. // TODO
  587. //_debug_parse_err_line = -1;
  588. //_debug_parse_err_file = "";
  589. //_debug_error = p_error;
  590. ScriptDebugger::get_singleton()->debug(this, p_allow_continue);
  591. return true;
  592. } else {
  593. return false;
  594. }
  595. }
  596. void CSharpLanguage::set_language_index(int p_idx) {
  597. ERR_FAIL_COND(lang_idx != -1);
  598. lang_idx = p_idx;
  599. }
  600. CSharpLanguage::CSharpLanguage() {
  601. ERR_FAIL_COND(singleton);
  602. singleton = this;
  603. finalizing = false;
  604. gdmono = NULL;
  605. #ifdef NO_THREADS
  606. lock = NULL;
  607. gchandle_bind_lock = NULL;
  608. #else
  609. lock = Mutex::create();
  610. script_bind_lock = Mutex::create();
  611. #endif
  612. lang_idx = -1;
  613. }
  614. CSharpLanguage::~CSharpLanguage() {
  615. finish();
  616. if (lock) {
  617. memdelete(lock);
  618. lock = NULL;
  619. }
  620. if (script_bind_lock) {
  621. memdelete(script_bind_lock);
  622. script_bind_lock = NULL;
  623. }
  624. singleton = NULL;
  625. }
  626. void *CSharpLanguage::alloc_instance_binding_data(Object *p_object) {
  627. #ifdef DEBUG_ENABLED
  628. // I don't trust you
  629. if (p_object->get_script_instance())
  630. CRASH_COND(NULL != CAST_CSHARP_INSTANCE(p_object->get_script_instance()));
  631. #endif
  632. StringName type_name = p_object->get_class_name();
  633. // ¯\_(ツ)_/¯
  634. const ClassDB::ClassInfo *classinfo = ClassDB::classes.getptr(type_name);
  635. while (classinfo && !classinfo->exposed)
  636. classinfo = classinfo->inherits_ptr;
  637. ERR_FAIL_NULL_V(classinfo, NULL);
  638. type_name = classinfo->name;
  639. GDMonoClass *type_class = GDMonoUtils::type_get_proxy_class(type_name);
  640. ERR_FAIL_NULL_V(type_class, NULL);
  641. MonoObject *mono_object = GDMonoUtils::create_managed_for_godot_object(type_class, type_name, p_object);
  642. ERR_FAIL_NULL_V(mono_object, NULL);
  643. // Tie managed to unmanaged
  644. Reference *ref = Object::cast_to<Reference>(p_object);
  645. if (ref) {
  646. // Unsafe refcount increment. The managed instance also counts as a reference.
  647. // This way if the unmanaged world has no references to our owner
  648. // but the managed instance is alive, the refcount will be 1 instead of 0.
  649. // See: _GodotSharp::_dispose_object(Object *p_object)
  650. ref->reference();
  651. }
  652. Ref<MonoGCHandle> gchandle = MonoGCHandle::create_strong(mono_object);
  653. #ifndef NO_THREADS
  654. script_bind_lock->lock();
  655. #endif
  656. void *data = (void *)gchandle_bindings.insert(p_object, gchandle);
  657. #ifndef NO_THREADS
  658. script_bind_lock->unlock();
  659. #endif
  660. return data;
  661. }
  662. void CSharpLanguage::free_instance_binding_data(void *p_data) {
  663. if (GDMono::get_singleton() == NULL) {
  664. #ifdef DEBUG_ENABLED
  665. CRASH_COND(!gchandle_bindings.empty());
  666. #endif
  667. // Mono runtime finalized, all the gchandle bindings were already released
  668. return;
  669. }
  670. if (finalizing)
  671. return; // inside CSharpLanguage::finish(), all the gchandle bindings are released there
  672. #ifndef NO_THREADS
  673. script_bind_lock->lock();
  674. #endif
  675. Map<Object *, Ref<MonoGCHandle> >::Element *data = (Map<Object *, Ref<MonoGCHandle> >::Element *)p_data;
  676. // Set the native instance field to IntPtr.Zero, if not yet garbage collected
  677. MonoObject *mono_object = data->value()->get_target();
  678. if (mono_object) {
  679. CACHED_FIELD(GodotObject, ptr)->set_value_raw(mono_object, NULL);
  680. }
  681. gchandle_bindings.erase(data);
  682. #ifndef NO_THREADS
  683. script_bind_lock->unlock();
  684. #endif
  685. }
  686. void CSharpInstance::_ml_call_reversed(MonoObject *p_mono_object, GDMonoClass *p_klass, const StringName &p_method, const Variant **p_args, int p_argcount) {
  687. GDMonoClass *base = p_klass->get_parent_class();
  688. if (base && base != script->native)
  689. _ml_call_reversed(p_mono_object, base, p_method, p_args, p_argcount);
  690. GDMonoMethod *method = p_klass->get_method(p_method, p_argcount);
  691. if (method) {
  692. method->invoke(p_mono_object, p_args);
  693. }
  694. }
  695. CSharpInstance *CSharpInstance::create_for_managed_type(Object *p_owner, CSharpScript *p_script, const Ref<MonoGCHandle> &p_gchandle) {
  696. CSharpInstance *instance = memnew(CSharpInstance);
  697. Reference *ref = Object::cast_to<Reference>(p_owner);
  698. instance->base_ref = ref != NULL;
  699. instance->script = Ref<CSharpScript>(p_script);
  700. instance->owner = p_owner;
  701. instance->gchandle = p_gchandle;
  702. if (instance->base_ref)
  703. instance->_reference_owner_unsafe();
  704. p_script->instances.insert(p_owner);
  705. return instance;
  706. }
  707. MonoObject *CSharpInstance::get_mono_object() const {
  708. #ifdef DEBUG_ENABLED
  709. CRASH_COND(gchandle.is_null());
  710. #endif
  711. return gchandle->get_target();
  712. }
  713. bool CSharpInstance::set(const StringName &p_name, const Variant &p_value) {
  714. ERR_FAIL_COND_V(!script.is_valid(), false);
  715. GDMonoClass *top = script->script_class;
  716. while (top && top != script->native) {
  717. GDMonoField *field = script->script_class->get_field(p_name);
  718. if (field) {
  719. MonoObject *mono_object = get_mono_object();
  720. ERR_EXPLAIN("Reference has been garbage collected?");
  721. ERR_FAIL_NULL_V(mono_object, false);
  722. field->set_value(mono_object, p_value);
  723. return true;
  724. }
  725. top = top->get_parent_class();
  726. }
  727. // Call _set
  728. Variant name = p_name;
  729. const Variant *args[2] = { &name, &p_value };
  730. MonoObject *mono_object = get_mono_object();
  731. top = script->script_class;
  732. while (top && top != script->native) {
  733. GDMonoMethod *method = top->get_method(CACHED_STRING_NAME(_set), 2);
  734. if (method) {
  735. MonoObject *ret = method->invoke(mono_object, args);
  736. if (ret && GDMonoMarshal::unbox<MonoBoolean>(ret) == true)
  737. return true;
  738. }
  739. top = top->get_parent_class();
  740. }
  741. return false;
  742. }
  743. bool CSharpInstance::get(const StringName &p_name, Variant &r_ret) const {
  744. ERR_FAIL_COND_V(!script.is_valid(), false);
  745. GDMonoClass *top = script->script_class;
  746. while (top && top != script->native) {
  747. GDMonoField *field = top->get_field(p_name);
  748. if (field) {
  749. MonoObject *mono_object = get_mono_object();
  750. ERR_EXPLAIN("Reference has been garbage collected?");
  751. ERR_FAIL_NULL_V(mono_object, false);
  752. MonoObject *value = field->get_value(mono_object);
  753. r_ret = GDMonoMarshal::mono_object_to_variant(value, field->get_type());
  754. return true;
  755. }
  756. // Call _get
  757. GDMonoMethod *method = top->get_method(CACHED_STRING_NAME(_get), 1);
  758. if (method) {
  759. Variant name = p_name;
  760. const Variant *args[1] = { &name };
  761. MonoObject *ret = method->invoke(get_mono_object(), args);
  762. if (ret) {
  763. r_ret = GDMonoMarshal::mono_object_to_variant(ret);
  764. return true;
  765. }
  766. }
  767. top = top->get_parent_class();
  768. }
  769. return false;
  770. }
  771. void CSharpInstance::get_property_list(List<PropertyInfo> *p_properties) const {
  772. for (Map<StringName, PropertyInfo>::Element *E = script->member_info.front(); E; E = E->next()) {
  773. p_properties->push_back(E->value());
  774. }
  775. }
  776. Variant::Type CSharpInstance::get_property_type(const StringName &p_name, bool *r_is_valid) const {
  777. if (script->member_info.has(p_name)) {
  778. if (r_is_valid)
  779. *r_is_valid = true;
  780. return script->member_info[p_name].type;
  781. }
  782. if (r_is_valid)
  783. *r_is_valid = false;
  784. return Variant::NIL;
  785. }
  786. bool CSharpInstance::has_method(const StringName &p_method) const {
  787. if (!script.is_valid())
  788. return false;
  789. GDMonoClass *top = script->script_class;
  790. while (top && top != script->native) {
  791. if (top->has_method(p_method)) {
  792. return true;
  793. }
  794. top = top->get_parent_class();
  795. }
  796. return false;
  797. }
  798. Variant CSharpInstance::call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  799. MonoObject *mono_object = get_mono_object();
  800. ERR_FAIL_NULL_V(mono_object, Variant());
  801. if (!script.is_valid())
  802. return Variant();
  803. GDMonoClass *top = script->script_class;
  804. while (top && top != script->native) {
  805. GDMonoMethod *method = top->get_method(p_method, p_argcount);
  806. if (method) {
  807. MonoObject *return_value = method->invoke(mono_object, p_args);
  808. if (return_value) {
  809. return GDMonoMarshal::mono_object_to_variant(return_value, method->get_return_type());
  810. } else {
  811. return Variant();
  812. }
  813. }
  814. top = top->get_parent_class();
  815. }
  816. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  817. return Variant();
  818. }
  819. void CSharpInstance::call_multilevel(const StringName &p_method, const Variant **p_args, int p_argcount) {
  820. if (script.is_valid()) {
  821. MonoObject *mono_object = get_mono_object();
  822. ERR_FAIL_NULL(mono_object);
  823. _call_multilevel(mono_object, p_method, p_args, p_argcount);
  824. }
  825. }
  826. void CSharpInstance::_call_multilevel(MonoObject *p_mono_object, const StringName &p_method, const Variant **p_args, int p_argcount) {
  827. GDMonoClass *top = script->script_class;
  828. while (top && top != script->native) {
  829. GDMonoMethod *method = top->get_method(p_method, p_argcount);
  830. if (method)
  831. method->invoke(p_mono_object, p_args);
  832. top = top->get_parent_class();
  833. }
  834. }
  835. void CSharpInstance::call_multilevel_reversed(const StringName &p_method, const Variant **p_args, int p_argcount) {
  836. if (script.is_valid()) {
  837. MonoObject *mono_object = get_mono_object();
  838. ERR_FAIL_NULL(mono_object);
  839. _ml_call_reversed(mono_object, script->script_class, p_method, p_args, p_argcount);
  840. }
  841. }
  842. void CSharpInstance::_reference_owner_unsafe() {
  843. #ifdef DEBUG_ENABLED
  844. CRASH_COND(!base_ref);
  845. #endif
  846. // Unsafe refcount increment. The managed instance also counts as a reference.
  847. // This way if the unmanaged world has no references to our owner
  848. // but the managed instance is alive, the refcount will be 1 instead of 0.
  849. // See: _unreference_owner_unsafe()
  850. // May not me referenced yet, so we must use init_ref() instead of reference()
  851. Object::cast_to<Reference>(owner)->init_ref();
  852. }
  853. void CSharpInstance::_unreference_owner_unsafe() {
  854. #ifdef DEBUG_ENABLED
  855. CRASH_COND(!base_ref);
  856. #endif
  857. // Called from CSharpInstance::mono_object_disposed() or ~CSharpInstance()
  858. // Unsafe refcount decrement. The managed instance also counts as a reference.
  859. // See: _reference_owner_unsafe()
  860. if (Object::cast_to<Reference>(owner)->unreference()) {
  861. memdelete(owner);
  862. owner = NULL;
  863. }
  864. }
  865. void CSharpInstance::mono_object_disposed() {
  866. if (base_ref)
  867. _unreference_owner_unsafe();
  868. }
  869. void CSharpInstance::refcount_incremented() {
  870. CRASH_COND(!base_ref);
  871. Reference *ref_owner = Object::cast_to<Reference>(owner);
  872. if (ref_owner->reference_get_count() > 1) { // The managed side also holds a reference, hence 1 instead of 0
  873. // The reference count was increased after the managed side was the only one referencing our owner.
  874. // This means the owner is being referenced again by the unmanaged side,
  875. // so the owner must hold the managed side alive again to avoid it from being GCed.
  876. // Release the current weak handle and replace it with a strong handle.
  877. uint32_t strong_gchandle = MonoGCHandle::make_strong_handle(gchandle->get_target());
  878. gchandle->release();
  879. gchandle->set_handle(strong_gchandle);
  880. }
  881. }
  882. bool CSharpInstance::refcount_decremented() {
  883. CRASH_COND(!base_ref);
  884. Reference *ref_owner = Object::cast_to<Reference>(owner);
  885. int refcount = ref_owner->reference_get_count();
  886. if (refcount == 1) { // The managed side also holds a reference, hence 1 instead of 0
  887. // If owner owner is no longer referenced by the unmanaged side,
  888. // the managed instance takes responsibility of deleting the owner when GCed.
  889. // Release the current strong handle and replace it with a weak handle.
  890. uint32_t weak_gchandle = MonoGCHandle::make_weak_handle(gchandle->get_target());
  891. gchandle->release();
  892. gchandle->set_handle(weak_gchandle);
  893. return false;
  894. }
  895. ref_dying = (refcount == 0);
  896. return ref_dying;
  897. }
  898. ScriptInstance::RPCMode CSharpInstance::get_rpc_mode(const StringName &p_method) const {
  899. GDMonoClass *top = script->script_class;
  900. while (top && top != script->native) {
  901. GDMonoMethod *method = top->get_method(p_method);
  902. if (method) { // TODO should we reject static methods?
  903. // TODO cache result
  904. if (method->has_attribute(CACHED_CLASS(RemoteAttribute)))
  905. return RPC_MODE_REMOTE;
  906. if (method->has_attribute(CACHED_CLASS(SyncAttribute)))
  907. return RPC_MODE_SYNC;
  908. if (method->has_attribute(CACHED_CLASS(MasterAttribute)))
  909. return RPC_MODE_MASTER;
  910. if (method->has_attribute(CACHED_CLASS(SlaveAttribute)))
  911. return RPC_MODE_SLAVE;
  912. }
  913. top = top->get_parent_class();
  914. }
  915. return RPC_MODE_DISABLED;
  916. }
  917. ScriptInstance::RPCMode CSharpInstance::get_rset_mode(const StringName &p_variable) const {
  918. GDMonoClass *top = script->script_class;
  919. while (top && top != script->native) {
  920. GDMonoField *field = top->get_field(p_variable);
  921. if (field) { // TODO should we reject static fields?
  922. // TODO cache result
  923. if (field->has_attribute(CACHED_CLASS(RemoteAttribute)))
  924. return RPC_MODE_REMOTE;
  925. if (field->has_attribute(CACHED_CLASS(SyncAttribute)))
  926. return RPC_MODE_SYNC;
  927. if (field->has_attribute(CACHED_CLASS(MasterAttribute)))
  928. return RPC_MODE_MASTER;
  929. if (field->has_attribute(CACHED_CLASS(SlaveAttribute)))
  930. return RPC_MODE_SLAVE;
  931. }
  932. top = top->get_parent_class();
  933. }
  934. return RPC_MODE_DISABLED;
  935. }
  936. void CSharpInstance::notification(int p_notification) {
  937. MonoObject *mono_object = get_mono_object();
  938. if (p_notification == Object::NOTIFICATION_PREDELETE) {
  939. if (mono_object != NULL) { // otherwise it was collected, and the finalizer already called NOTIFICATION_PREDELETE
  940. call_notification_no_check(mono_object, p_notification);
  941. // Set the native instance field to IntPtr.Zero
  942. CACHED_FIELD(GodotObject, ptr)->set_value_raw(mono_object, NULL);
  943. }
  944. return;
  945. }
  946. call_notification_no_check(mono_object, p_notification);
  947. }
  948. void CSharpInstance::call_notification_no_check(MonoObject *p_mono_object, int p_notification) {
  949. Variant value = p_notification;
  950. const Variant *args[1] = { &value };
  951. _call_multilevel(p_mono_object, CACHED_STRING_NAME(_notification), args, 1);
  952. }
  953. Ref<Script> CSharpInstance::get_script() const {
  954. return script;
  955. }
  956. ScriptLanguage *CSharpInstance::get_language() {
  957. return CSharpLanguage::get_singleton();
  958. }
  959. CSharpInstance::CSharpInstance() {
  960. owner = NULL;
  961. base_ref = false;
  962. ref_dying = false;
  963. }
  964. CSharpInstance::~CSharpInstance() {
  965. if (gchandle.is_valid()) {
  966. gchandle->release(); // Make sure it's released
  967. }
  968. if (base_ref && !ref_dying) { // it may be called from the owner's destructor
  969. #ifdef DEBUG_ENABLED
  970. CRASH_COND(!owner); // dunno, just in case
  971. #endif
  972. _unreference_owner_unsafe();
  973. }
  974. if (script.is_valid() && owner) {
  975. #ifndef NO_THREADS
  976. CSharpLanguage::singleton->lock->lock();
  977. #endif
  978. #ifdef DEBUG_ENABLED
  979. // CSharpInstance must not be created unless it's going to be added to the list for sure
  980. Set<Object *>::Element *match = script->instances.find(owner);
  981. CRASH_COND(!match);
  982. script->instances.erase(match);
  983. #else
  984. script->instances.erase(owner);
  985. #endif
  986. #ifndef NO_THREADS
  987. CSharpLanguage::singleton->lock->unlock();
  988. #endif
  989. }
  990. }
  991. #ifdef TOOLS_ENABLED
  992. void CSharpScript::_placeholder_erased(PlaceHolderScriptInstance *p_placeholder) {
  993. placeholders.erase(p_placeholder);
  994. }
  995. #endif
  996. #ifdef TOOLS_ENABLED
  997. void CSharpScript::_update_exports_values(Map<StringName, Variant> &values, List<PropertyInfo> &propnames) {
  998. if (base_cache.is_valid()) {
  999. base_cache->_update_exports_values(values, propnames);
  1000. }
  1001. for (Map<StringName, Variant>::Element *E = exported_members_defval_cache.front(); E; E = E->next()) {
  1002. values[E->key()] = E->get();
  1003. }
  1004. for (List<PropertyInfo>::Element *E = exported_members_cache.front(); E; E = E->next()) {
  1005. propnames.push_back(E->get());
  1006. }
  1007. }
  1008. #endif
  1009. bool CSharpScript::_update_exports() {
  1010. #ifdef TOOLS_ENABLED
  1011. if (!valid)
  1012. return false;
  1013. bool changed = false;
  1014. if (exports_invalidated) {
  1015. exports_invalidated = false;
  1016. changed = true;
  1017. member_info.clear();
  1018. exported_members_cache.clear();
  1019. exported_members_defval_cache.clear();
  1020. // We are creating a temporary new instance of the class here to get the default value
  1021. // TODO Workaround. Should be replaced with IL opcodes analysis
  1022. MonoObject *tmp_object = mono_object_new(SCRIPTS_DOMAIN, script_class->get_raw());
  1023. if (tmp_object) {
  1024. CACHED_FIELD(GodotObject, ptr)->set_value_raw(tmp_object, tmp_object); // FIXME WTF is this workaround
  1025. GDMonoMethod *ctor = script_class->get_method(CACHED_STRING_NAME(dotctor), 0);
  1026. MonoObject *ex = NULL;
  1027. ctor->invoke(tmp_object, NULL, &ex);
  1028. if (ex) {
  1029. ERR_PRINT("Exception thrown from constructor of temporary MonoObject:");
  1030. mono_print_unhandled_exception(ex);
  1031. tmp_object = NULL;
  1032. ERR_FAIL_V(false);
  1033. }
  1034. } else {
  1035. ERR_PRINT("Failed to create temporary MonoObject");
  1036. return false;
  1037. }
  1038. GDMonoClass *top = script_class;
  1039. while (top && top != native) {
  1040. const Vector<GDMonoField *> &fields = top->get_all_fields();
  1041. for (int i = fields.size() - 1; i >= 0; i--) {
  1042. GDMonoField *field = fields[i];
  1043. if (field->is_static()) {
  1044. if (field->has_attribute(CACHED_CLASS(ExportAttribute)))
  1045. ERR_PRINTS("Cannot export field because it is static: " + top->get_full_name() + "." + field->get_name());
  1046. continue;
  1047. }
  1048. String name = field->get_name();
  1049. StringName cname = name;
  1050. if (member_info.has(cname))
  1051. continue;
  1052. ManagedType field_type = field->get_type();
  1053. Variant::Type type = GDMonoMarshal::managed_to_variant_type(field_type);
  1054. if (field->has_attribute(CACHED_CLASS(ExportAttribute))) {
  1055. // Field has Export attribute
  1056. MonoObject *attr = field->get_attribute(CACHED_CLASS(ExportAttribute));
  1057. PropertyHint hint;
  1058. String hint_string;
  1059. if (type == Variant::NIL) {
  1060. ERR_PRINTS("Unknown type of exported field: " + top->get_full_name() + "." + field->get_name());
  1061. continue;
  1062. } else if (type == Variant::INT && field_type.type_encoding == MONO_TYPE_VALUETYPE && mono_class_is_enum(field_type.type_class->get_raw())) {
  1063. type = Variant::INT;
  1064. hint = PROPERTY_HINT_ENUM;
  1065. Vector<MonoClassField *> fields = field_type.type_class->get_enum_fields();
  1066. for (int i = 0; i < fields.size(); i++) {
  1067. if (i > 0)
  1068. hint_string += ",";
  1069. hint_string += mono_field_get_name(fields[i]);
  1070. }
  1071. } else if (type == Variant::OBJECT && CACHED_CLASS(GodotReference)->is_assignable_from(field_type.type_class)) {
  1072. hint = PROPERTY_HINT_RESOURCE_TYPE;
  1073. hint_string = NATIVE_GDMONOCLASS_NAME(field_type.type_class);
  1074. } else {
  1075. hint = PropertyHint(CACHED_FIELD(ExportAttribute, hint)->get_int_value(attr));
  1076. hint_string = CACHED_FIELD(ExportAttribute, hintString)->get_string_value(attr);
  1077. }
  1078. PropertyInfo prop_info = PropertyInfo(type, name, hint, hint_string, PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE);
  1079. member_info[cname] = prop_info;
  1080. exported_members_cache.push_front(prop_info);
  1081. if (tmp_object) {
  1082. exported_members_defval_cache[cname] = GDMonoMarshal::mono_object_to_variant(field->get_value(tmp_object));
  1083. }
  1084. } else {
  1085. member_info[cname] = PropertyInfo(type, name, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_SCRIPT_VARIABLE);
  1086. }
  1087. }
  1088. top = top->get_parent_class();
  1089. }
  1090. }
  1091. if (placeholders.size()) {
  1092. // Update placeholders if any
  1093. Map<StringName, Variant> values;
  1094. List<PropertyInfo> propnames;
  1095. _update_exports_values(values, propnames);
  1096. for (Set<PlaceHolderScriptInstance *>::Element *E = placeholders.front(); E; E = E->next()) {
  1097. E->get()->update(propnames, values);
  1098. }
  1099. }
  1100. return changed;
  1101. #endif
  1102. return false;
  1103. }
  1104. void CSharpScript::_clear() {
  1105. tool = false;
  1106. valid = false;
  1107. base = NULL;
  1108. native = NULL;
  1109. script_class = NULL;
  1110. }
  1111. Variant CSharpScript::call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  1112. GDMonoClass *top = script_class;
  1113. while (top && top != native) {
  1114. GDMonoMethod *method = top->get_method(p_method, p_argcount);
  1115. if (method && method->is_static()) {
  1116. MonoObject *result = method->invoke(NULL, p_args);
  1117. if (result) {
  1118. return GDMonoMarshal::mono_object_to_variant(result, method->get_return_type());
  1119. } else {
  1120. return Variant();
  1121. }
  1122. }
  1123. top = top->get_parent_class();
  1124. }
  1125. // No static method found. Try regular instance calls
  1126. return Script::call(p_method, p_args, p_argcount, r_error);
  1127. }
  1128. void CSharpScript::_resource_path_changed() {
  1129. String path = get_path();
  1130. if (!path.empty()) {
  1131. name = get_path().get_file().get_basename();
  1132. }
  1133. }
  1134. bool CSharpScript::_get(const StringName &p_name, Variant &r_ret) const {
  1135. if (p_name == CSharpLanguage::singleton->string_names._script_source) {
  1136. r_ret = get_source_code();
  1137. return true;
  1138. }
  1139. return false;
  1140. }
  1141. bool CSharpScript::_set(const StringName &p_name, const Variant &p_value) {
  1142. if (p_name == CSharpLanguage::singleton->string_names._script_source) {
  1143. set_source_code(p_value);
  1144. reload();
  1145. return true;
  1146. }
  1147. return false;
  1148. }
  1149. void CSharpScript::_get_property_list(List<PropertyInfo> *p_properties) const {
  1150. p_properties->push_back(PropertyInfo(Variant::STRING, CSharpLanguage::singleton->string_names._script_source, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  1151. }
  1152. void CSharpScript::_bind_methods() {
  1153. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "new", &CSharpScript::_new, MethodInfo(Variant::OBJECT, "new"));
  1154. }
  1155. Ref<CSharpScript> CSharpScript::create_for_managed_type(GDMonoClass *p_class) {
  1156. // This method should not fail
  1157. CRASH_COND(!p_class);
  1158. Ref<CSharpScript> script = memnew(CSharpScript);
  1159. script->name = p_class->get_name();
  1160. script->script_class = p_class;
  1161. script->native = GDMonoUtils::get_class_native_base(script->script_class);
  1162. CRASH_COND(script->native == NULL);
  1163. GDMonoClass *base = script->script_class->get_parent_class();
  1164. if (base != script->native)
  1165. script->base = base;
  1166. #ifdef DEBUG_ENABLED
  1167. // For debug builds, we must fetch from all native base methods as well.
  1168. // Native base methods must be fetched before the current class.
  1169. // Not needed if the script class itself is a native class.
  1170. if (script->script_class != script->native) {
  1171. GDMonoClass *native_top = script->native;
  1172. while (native_top) {
  1173. native_top->fetch_methods_with_godot_api_checks(script->native);
  1174. if (native_top == CACHED_CLASS(GodotObject))
  1175. break;
  1176. native_top = native_top->get_parent_class();
  1177. }
  1178. }
  1179. #endif
  1180. script->script_class->fetch_methods_with_godot_api_checks(script->native);
  1181. // Need to fetch method from base classes as well
  1182. GDMonoClass *top = script->script_class;
  1183. while (top && top != script->native) {
  1184. top->fetch_methods_with_godot_api_checks(script->native);
  1185. top = top->get_parent_class();
  1186. }
  1187. return script;
  1188. }
  1189. bool CSharpScript::can_instance() const {
  1190. #ifdef TOOLS_ENABLED
  1191. if (Engine::get_singleton()->is_editor_hint()) {
  1192. if (get_path().find("::") == -1) { // Ignore if built-in script. Can happen if the file is deleted...
  1193. if (_create_project_solution_if_needed()) {
  1194. CSharpProject::add_item(GodotSharpDirs::get_project_csproj_path(),
  1195. "Compile",
  1196. ProjectSettings::get_singleton()->globalize_path(get_path()));
  1197. } else {
  1198. ERR_PRINTS("Cannot add " + get_path() + " to the C# project because it could not be created.");
  1199. }
  1200. }
  1201. }
  1202. #endif
  1203. return valid || (!tool && !ScriptServer::is_scripting_enabled());
  1204. }
  1205. StringName CSharpScript::get_instance_base_type() const {
  1206. if (native)
  1207. return native->get_name();
  1208. else
  1209. return StringName();
  1210. }
  1211. CSharpInstance *CSharpScript::_create_instance(const Variant **p_args, int p_argcount, Object *p_owner, bool p_isref, Variant::CallError &r_error) {
  1212. /* STEP 1, CREATE */
  1213. CSharpInstance *instance = memnew(CSharpInstance);
  1214. instance->base_ref = p_isref;
  1215. instance->script = Ref<CSharpScript>(this);
  1216. instance->owner = p_owner;
  1217. instance->owner->set_script_instance(instance);
  1218. if (instance->base_ref)
  1219. instance->_reference_owner_unsafe();
  1220. /* STEP 2, INITIALIZE AND CONSTRUCT */
  1221. MonoObject *mono_object = mono_object_new(SCRIPTS_DOMAIN, script_class->get_raw());
  1222. if (!mono_object) {
  1223. instance->script = Ref<CSharpScript>();
  1224. instance->owner->set_script_instance(NULL);
  1225. r_error.error = Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL;
  1226. ERR_EXPLAIN("Failed to allocate memory for the object");
  1227. ERR_FAIL_V(NULL);
  1228. }
  1229. #ifndef NO_THREADS
  1230. CSharpLanguage::singleton->lock->lock();
  1231. #endif
  1232. instances.insert(instance->owner);
  1233. #ifndef NO_THREADS
  1234. CSharpLanguage::singleton->lock->unlock();
  1235. #endif
  1236. CACHED_FIELD(GodotObject, ptr)->set_value_raw(mono_object, instance->owner);
  1237. // Construct
  1238. GDMonoMethod *ctor = script_class->get_method(CACHED_STRING_NAME(dotctor), p_argcount);
  1239. ctor->invoke(mono_object, p_args, NULL);
  1240. // Tie managed to unmanaged
  1241. instance->gchandle = MonoGCHandle::create_strong(mono_object);
  1242. /* STEP 3, PARTY */
  1243. //@TODO make thread safe
  1244. return instance;
  1245. }
  1246. Variant CSharpScript::_new(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  1247. if (!valid) {
  1248. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1249. return Variant();
  1250. }
  1251. r_error.error = Variant::CallError::CALL_OK;
  1252. REF ref;
  1253. Object *owner = NULL;
  1254. ERR_FAIL_NULL_V(native, Variant());
  1255. owner = ClassDB::instance(NATIVE_GDMONOCLASS_NAME(native));
  1256. Reference *r = Object::cast_to<Reference>(owner);
  1257. if (r) {
  1258. ref = REF(r);
  1259. }
  1260. CSharpInstance *instance = _create_instance(p_args, p_argcount, owner, r != NULL, r_error);
  1261. if (!instance) {
  1262. if (ref.is_null()) {
  1263. memdelete(owner); //no owner, sorry
  1264. }
  1265. return Variant();
  1266. }
  1267. if (ref.is_valid()) {
  1268. return ref;
  1269. } else {
  1270. return owner;
  1271. }
  1272. }
  1273. ScriptInstance *CSharpScript::instance_create(Object *p_this) {
  1274. if (!valid)
  1275. return NULL;
  1276. if (!tool && !ScriptServer::is_scripting_enabled()) {
  1277. #ifdef TOOLS_ENABLED
  1278. PlaceHolderScriptInstance *si = memnew(PlaceHolderScriptInstance(CSharpLanguage::get_singleton(), Ref<Script>(this), p_this));
  1279. placeholders.insert(si);
  1280. _update_exports();
  1281. return si;
  1282. #else
  1283. return NULL;
  1284. #endif
  1285. }
  1286. if (native) {
  1287. String native_name = native->get_name();
  1288. if (!ClassDB::is_parent_class(p_this->get_class_name(), native_name)) {
  1289. if (ScriptDebugger::get_singleton()) {
  1290. CSharpLanguage::get_singleton()->debug_break_parse(get_path(), 0, "Script inherits from native type '" + native_name + "', so it can't be instanced in object of type: '" + p_this->get_class() + "'");
  1291. }
  1292. ERR_EXPLAIN("Script inherits from native type '" + native_name + "', so it can't be instanced in object of type: '" + p_this->get_class() + "'");
  1293. ERR_FAIL_V(NULL);
  1294. }
  1295. }
  1296. Variant::CallError unchecked_error;
  1297. return _create_instance(NULL, 0, p_this, Object::cast_to<Reference>(p_this), unchecked_error);
  1298. }
  1299. bool CSharpScript::instance_has(const Object *p_this) const {
  1300. #ifndef NO_THREADS
  1301. CSharpLanguage::singleton->lock->lock();
  1302. #endif
  1303. bool ret = instances.has((Object *)p_this);
  1304. #ifndef NO_THREADS
  1305. CSharpLanguage::singleton->lock->unlock();
  1306. #endif
  1307. return ret;
  1308. }
  1309. bool CSharpScript::has_source_code() const {
  1310. return !source.empty();
  1311. }
  1312. String CSharpScript::get_source_code() const {
  1313. return source;
  1314. }
  1315. void CSharpScript::set_source_code(const String &p_code) {
  1316. if (source == p_code)
  1317. return;
  1318. source = p_code;
  1319. #ifdef TOOLS_ENABLED
  1320. source_changed_cache = true;
  1321. #endif
  1322. }
  1323. bool CSharpScript::has_method(const StringName &p_method) const {
  1324. return script_class->has_method(p_method);
  1325. }
  1326. Error CSharpScript::reload(bool p_keep_state) {
  1327. #ifndef NO_THREADS
  1328. CSharpLanguage::singleton->lock->lock();
  1329. #endif
  1330. bool has_instances = instances.size();
  1331. #ifndef NO_THREADS
  1332. CSharpLanguage::singleton->lock->unlock();
  1333. #endif
  1334. ERR_FAIL_COND_V(!p_keep_state && has_instances, ERR_ALREADY_IN_USE);
  1335. GDMonoAssembly *project_assembly = GDMono::get_singleton()->get_project_assembly();
  1336. if (project_assembly) {
  1337. script_class = project_assembly->get_object_derived_class(name);
  1338. if (!script_class) {
  1339. ERR_PRINTS("Cannot find class " + name + " for script " + get_path());
  1340. }
  1341. #ifdef DEBUG_ENABLED
  1342. else if (OS::get_singleton()->is_stdout_verbose()) {
  1343. OS::get_singleton()->print(String("Found class " + script_class->get_namespace() + "." +
  1344. script_class->get_name() + " for script " + get_path() + "\n")
  1345. .utf8());
  1346. }
  1347. #endif
  1348. valid = script_class != NULL;
  1349. if (script_class) {
  1350. tool = script_class->has_attribute(CACHED_CLASS(ToolAttribute));
  1351. native = GDMonoUtils::get_class_native_base(script_class);
  1352. CRASH_COND(native == NULL);
  1353. GDMonoClass *base_class = script_class->get_parent_class();
  1354. if (base_class != native)
  1355. base = base_class;
  1356. #ifdef DEBUG_ENABLED
  1357. // For debug builds, we must fetch from all native base methods as well.
  1358. // Native base methods must be fetched before the current class.
  1359. // Not needed if the script class itself is a native class.
  1360. if (script_class != native) {
  1361. GDMonoClass *native_top = native;
  1362. while (native_top) {
  1363. native_top->fetch_methods_with_godot_api_checks(native);
  1364. if (native_top == CACHED_CLASS(GodotObject))
  1365. break;
  1366. native_top = native_top->get_parent_class();
  1367. }
  1368. }
  1369. #endif
  1370. script_class->fetch_methods_with_godot_api_checks(native);
  1371. // Need to fetch method from base classes as well
  1372. GDMonoClass *top = script_class;
  1373. while (top && top != native) {
  1374. top->fetch_methods_with_godot_api_checks(native);
  1375. top = top->get_parent_class();
  1376. }
  1377. }
  1378. return OK;
  1379. }
  1380. return ERR_FILE_MISSING_DEPENDENCIES;
  1381. }
  1382. ScriptLanguage *CSharpScript::get_language() const {
  1383. return CSharpLanguage::get_singleton();
  1384. }
  1385. bool CSharpScript::get_property_default_value(const StringName &p_property, Variant &r_value) const {
  1386. #ifdef TOOLS_ENABLED
  1387. const Map<StringName, Variant>::Element *E = exported_members_defval_cache.find(p_property);
  1388. if (E) {
  1389. r_value = E->get();
  1390. return true;
  1391. }
  1392. if (base_cache.is_valid()) {
  1393. return base_cache->get_property_default_value(p_property, r_value);
  1394. }
  1395. #endif
  1396. return false;
  1397. }
  1398. void CSharpScript::update_exports() {
  1399. #ifdef TOOLS_ENABLED
  1400. _update_exports();
  1401. #endif
  1402. }
  1403. Ref<Script> CSharpScript::get_base_script() const {
  1404. // TODO search in metadata file once we have it, not important any way?
  1405. return Ref<Script>();
  1406. }
  1407. void CSharpScript::get_script_property_list(List<PropertyInfo> *p_list) const {
  1408. for (Map<StringName, PropertyInfo>::Element *E = member_info.front(); E; E = E->next()) {
  1409. p_list->push_back(E->value());
  1410. }
  1411. }
  1412. int CSharpScript::get_member_line(const StringName &p_member) const {
  1413. // TODO omnisharp
  1414. return -1;
  1415. }
  1416. Error CSharpScript::load_source_code(const String &p_path) {
  1417. PoolVector<uint8_t> sourcef;
  1418. Error err;
  1419. FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
  1420. ERR_FAIL_COND_V(err != OK, err);
  1421. int len = f->get_len();
  1422. sourcef.resize(len + 1);
  1423. PoolVector<uint8_t>::Write w = sourcef.write();
  1424. int r = f->get_buffer(w.ptr(), len);
  1425. f->close();
  1426. memdelete(f);
  1427. ERR_FAIL_COND_V(r != len, ERR_CANT_OPEN);
  1428. w[len] = 0;
  1429. String s;
  1430. if (s.parse_utf8((const char *)w.ptr())) {
  1431. 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.");
  1432. ERR_FAIL_V(ERR_INVALID_DATA);
  1433. }
  1434. source = s;
  1435. #ifdef TOOLS_ENABLED
  1436. source_changed_cache = true;
  1437. #endif
  1438. return OK;
  1439. }
  1440. StringName CSharpScript::get_script_name() const {
  1441. return name;
  1442. }
  1443. CSharpScript::CSharpScript() :
  1444. script_list(this) {
  1445. _clear();
  1446. #ifdef TOOLS_ENABLED
  1447. source_changed_cache = false;
  1448. exports_invalidated = true;
  1449. #endif
  1450. _resource_path_changed();
  1451. #ifdef DEBUG_ENABLED
  1452. #ifndef NO_THREADS
  1453. CSharpLanguage::get_singleton()->lock->lock();
  1454. #endif
  1455. CSharpLanguage::get_singleton()->script_list.add(&script_list);
  1456. #ifndef NO_THREADS
  1457. CSharpLanguage::get_singleton()->lock->unlock();
  1458. #endif
  1459. #endif // DEBUG_ENABLED
  1460. }
  1461. CSharpScript::~CSharpScript() {
  1462. #ifdef DEBUG_ENABLED
  1463. #ifndef NO_THREADS
  1464. CSharpLanguage::get_singleton()->lock->lock();
  1465. #endif
  1466. CSharpLanguage::get_singleton()->script_list.remove(&script_list);
  1467. #ifndef NO_THREADS
  1468. CSharpLanguage::get_singleton()->lock->unlock();
  1469. #endif
  1470. #endif // DEBUG_ENABLED
  1471. }
  1472. /*************** RESOURCE ***************/
  1473. RES ResourceFormatLoaderCSharpScript::load(const String &p_path, const String &p_original_path, Error *r_error) {
  1474. if (r_error)
  1475. *r_error = ERR_FILE_CANT_OPEN;
  1476. // TODO ignore anything inside bin/ and obj/ in tools builds?
  1477. CSharpScript *script = memnew(CSharpScript);
  1478. Ref<CSharpScript> scriptres(script);
  1479. #if defined(DEBUG_ENABLED) || defined(TOOLS_ENABLED)
  1480. Error err = script->load_source_code(p_path);
  1481. ERR_FAIL_COND_V(err != OK, RES());
  1482. #endif
  1483. script->set_path(p_original_path);
  1484. #ifndef TOOLS_ENABLED
  1485. #ifdef DEBUG_ENABLED
  1486. // User is responsible for thread attach/detach
  1487. ERR_EXPLAIN("Thread is not attached");
  1488. CRASH_COND(mono_domain_get() == NULL);
  1489. #endif
  1490. #else
  1491. if (Engine::get_singleton()->is_editor_hint() && mono_domain_get() == NULL) {
  1492. CRASH_COND(Thread::get_caller_id() == Thread::get_main_id());
  1493. // Thread is not attached, but we will make an exception in this case
  1494. // because this may be called by one of the editor's worker threads.
  1495. // Attach this thread temporarily to reload the script.
  1496. MonoThread *mono_thread = mono_thread_attach(SCRIPTS_DOMAIN);
  1497. CRASH_COND(mono_thread == NULL);
  1498. script->reload();
  1499. mono_thread_detach(mono_thread);
  1500. } else // just reload it normally
  1501. #endif
  1502. script->reload();
  1503. if (r_error)
  1504. *r_error = OK;
  1505. return scriptres;
  1506. }
  1507. void ResourceFormatLoaderCSharpScript::get_recognized_extensions(List<String> *p_extensions) const {
  1508. p_extensions->push_back("cs");
  1509. }
  1510. bool ResourceFormatLoaderCSharpScript::handles_type(const String &p_type) const {
  1511. return p_type == "Script" || p_type == CSharpLanguage::get_singleton()->get_type();
  1512. }
  1513. String ResourceFormatLoaderCSharpScript::get_resource_type(const String &p_path) const {
  1514. return p_path.get_extension().to_lower() == "cs" ? CSharpLanguage::get_singleton()->get_type() : "";
  1515. }
  1516. Error ResourceFormatSaverCSharpScript::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  1517. Ref<CSharpScript> sqscr = p_resource;
  1518. ERR_FAIL_COND_V(sqscr.is_null(), ERR_INVALID_PARAMETER);
  1519. String source = sqscr->get_source_code();
  1520. #ifdef TOOLS_ENABLED
  1521. if (!FileAccess::exists(p_path)) {
  1522. // The file does not yet exists, let's assume the user just created this script
  1523. if (_create_project_solution_if_needed()) {
  1524. CSharpProject::add_item(GodotSharpDirs::get_project_csproj_path(),
  1525. "Compile",
  1526. ProjectSettings::get_singleton()->globalize_path(p_path));
  1527. } else {
  1528. ERR_PRINTS("Cannot add " + p_path + " to the C# project because it could not be created.");
  1529. }
  1530. }
  1531. #endif
  1532. Error err;
  1533. FileAccess *file = FileAccess::open(p_path, FileAccess::WRITE, &err);
  1534. ERR_FAIL_COND_V(err, err);
  1535. file->store_string(source);
  1536. if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) {
  1537. memdelete(file);
  1538. return ERR_CANT_CREATE;
  1539. }
  1540. file->close();
  1541. memdelete(file);
  1542. if (ScriptServer::is_reload_scripts_on_save_enabled()) {
  1543. CSharpLanguage::get_singleton()->reload_tool_script(p_resource, false);
  1544. }
  1545. return OK;
  1546. }
  1547. void ResourceFormatSaverCSharpScript::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const {
  1548. if (Object::cast_to<CSharpScript>(p_resource.ptr())) {
  1549. p_extensions->push_back("cs");
  1550. }
  1551. }
  1552. bool ResourceFormatSaverCSharpScript::recognize(const RES &p_resource) const {
  1553. return Object::cast_to<CSharpScript>(p_resource.ptr()) != NULL;
  1554. }
  1555. CSharpLanguage::StringNameCache::StringNameCache() {
  1556. _signal_callback = StaticCString::create("_signal_callback");
  1557. _set = StaticCString::create("_set");
  1558. _get = StaticCString::create("_get");
  1559. _notification = StaticCString::create("_notification");
  1560. _script_source = StaticCString::create("script/source");
  1561. dotctor = StaticCString::create(".ctor");
  1562. }