csharp_script.cpp 53 KB

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