CSFunctionWriter.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Atomic/IO/FileSystem.h>
  23. #include "../JSBind.h"
  24. #include "../JSBModule.h"
  25. #include "../JSBPackage.h"
  26. #include "../JSBEnum.h"
  27. #include "../JSBClass.h"
  28. #include "../JSBFunction.h"
  29. #include "CSTypeHelper.h"
  30. #include "CSFunctionWriter.h"
  31. /*
  32. *
  33. C# getters/setters
  34. local instance storage so we're not constantly creating managed Vector3, etc
  35. Vector2/Vector3/BoundingBox, etc C# structs so assign by value
  36. Object lifetime
  37. C# enum of module types for type info?
  38. C# version of push class instance?
  39. new instance from C# needs constructor
  40. wrapping does not, wrapping doesn't use constructors at all (JS needs this for prototype)
  41. Store GCHandle to keep an object alive (Component, UI) C# side?
  42. typedef const void* ClassID;
  43. which changed based on address, so need register at startup
  44. so at package startup time, need to setup mapping between
  45. IntPtr and C# class, we also need to be able to new a class
  46. instance with existing native or create a native when new'ing from C#
  47. IntPtr to RefCounted native side is the "ID", like JSHeapPtr
  48. Lifetime:
  49. // you cannot derive from native engine classes, other than script components
  50. a C# instance can be new'd, handed to native, stored in native, the C# side could be GC'd
  51. future access to this instance would be a new instance
  52. */
  53. /*
  54. // struct marshal Vector2, Vector3, BoundingBox, etc
  55. // RefCounted*
  56. // primitive bool, int, uint, float, double
  57. // String
  58. RefCounted* csb_Node_Constructor()
  59. {
  60. return new Node(NETCore::GetContext());
  61. }
  62. void csb_Node_GetPosition(Node* self, Vector3* out)
  63. {
  64. *out = self->GetPosition();
  65. }
  66. void csb_Node_SetPosition(Node* self, Vector3*__arg0)
  67. {
  68. self->SetPosition(*__arg0);
  69. }
  70. void csb_Node_SetPosition(Node* self, Vector3*__arg0)
  71. {
  72. self->SetPosition(*__arg0);
  73. }
  74. bool csb_Audio_Play(Audio* self)
  75. {
  76. bool retValue = self->Play();
  77. return retValue;
  78. }
  79. const RefCounted* csb_Node_GetParent(Node* self)
  80. {
  81. const RefCounted* retValue = self->GetParent();
  82. return RefCounted;
  83. }
  84. RefCounted* csb_ObjectAnimation_Constructor()
  85. {
  86. return new ObjectAnimation(NETCore::GetContext());
  87. }
  88. */
  89. namespace ToolCore
  90. {
  91. CSFunctionWriter::CSFunctionWriter(JSBFunction *function) : JSBFunctionWriter(function)
  92. {
  93. }
  94. void CSFunctionWriter::WriteNativeParameterMarshal(String& source)
  95. {
  96. }
  97. void CSFunctionWriter::WriteNativeConstructor(String& source)
  98. {
  99. }
  100. void CSFunctionWriter::GenNativeCallParameters(String& sig)
  101. {
  102. JSBClass* klass = function_->GetClass();
  103. Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  104. Vector<String> args;
  105. if (parameters.Size())
  106. {
  107. for (unsigned int i = 0; i < parameters.Size(); i++)
  108. {
  109. JSBFunctionType* ptype = parameters.At(i);
  110. // ignore "Context" parameters
  111. if (ptype->type_->asClassType())
  112. {
  113. JSBClassType* classType = ptype->type_->asClassType();
  114. JSBClass* klass = classType->class_;
  115. if (klass->GetName() == "Context")
  116. {
  117. continue;
  118. }
  119. if (klass->IsNumberArray())
  120. args.Push(ToString("*%s", ptype->name_.CString()));
  121. else
  122. args.Push(ToString("%s", ptype->name_.CString()));
  123. }
  124. else
  125. {
  126. args.Push(ToString("%s", ptype->name_.CString()));
  127. }
  128. }
  129. }
  130. sig.Join(args, ", ");
  131. }
  132. void CSFunctionWriter::WriteNativeFunction(String& source)
  133. {
  134. JSBClass* klass = function_->GetClass();
  135. JSBPackage* package = klass->GetPackage();
  136. String fname = function_->IsConstructor() ? "Constructor" : function_->GetName();
  137. String returnType;
  138. String functionSig = CSTypeHelper::GetNativeFunctionSignature(function_, returnType);
  139. String line;
  140. line = ToString("ATOMIC_EXPORT_API %s %s\n",
  141. returnType.CString(), functionSig.CString());
  142. source += IndentLine(line);
  143. source += IndentLine("{\n");
  144. Indent();
  145. source += "\n";
  146. bool returnValue = false;
  147. bool sharedPtrReturn = false;
  148. String returnStatement;
  149. if (returnType == "const char*")
  150. {
  151. returnValue = true;
  152. source += IndentLine("static String returnValue;\n");
  153. returnStatement = "returnValue = ";
  154. }
  155. else if (function_->GetReturnClass() && function_->GetReturnClass()->IsNumberArray())
  156. {
  157. returnStatement = "*returnValue = ";
  158. }
  159. else if (function_->GetReturnClass() && function_->GetReturnType()->isSharedPtr_)
  160. {
  161. returnStatement = ToString("SharedPtr<%s> returnValue = ", function_->GetReturnClass()->GetNativeName().CString());
  162. sharedPtrReturn = true;
  163. }
  164. else
  165. {
  166. if (returnType != "void")
  167. {
  168. returnStatement = "return ";
  169. }
  170. }
  171. String callSig;
  172. GenNativeCallParameters(callSig);
  173. if (!function_->isConstructor_)
  174. {
  175. if (function_->IsStatic())
  176. {
  177. line = ToString("%s%s::%s(%s);\n", returnStatement.CString(), klass->GetNativeName().CString(), function_->GetName().CString(), callSig.CString());
  178. }
  179. else
  180. {
  181. line = ToString("%sself->%s(%s);\n", returnStatement.CString(), function_->GetName().CString(), callSig.CString());
  182. }
  183. }
  184. else
  185. {
  186. if (klass->IsAbstract())
  187. {
  188. line = "return 0; // Abstract Class\n";
  189. }
  190. else if (klass->IsObject())
  191. {
  192. if (callSig.Length())
  193. line = ToString("return new %s(NETCore::GetContext(), %s);\n", klass->GetNativeName().CString(), callSig.CString());
  194. else
  195. line = ToString("return new %s(NETCore::GetContext());\n", klass->GetNativeName().CString());
  196. }
  197. else
  198. {
  199. line = ToString("return new %s(%s);\n", klass->GetNativeName().CString(), callSig.CString());
  200. }
  201. }
  202. source += IndentLine(line);
  203. if (sharedPtrReturn)
  204. {
  205. source += IndentLine("returnValue->AddRef();\n");
  206. source += IndentLine("return returnValue;\n");
  207. }
  208. else if (returnType == "const char*")
  209. {
  210. source += IndentLine("return returnValue.CString();\n");
  211. }
  212. Dedent();
  213. source += IndentLine("}\n");
  214. source += "\n";
  215. }
  216. void CSFunctionWriter::GenerateNativeSource(String& sourceOut)
  217. {
  218. String source = "";
  219. WriteNativeFunction(source);
  220. sourceOut += source;
  221. }
  222. // MANAGED----------------------------------------------------------------------------------------
  223. void CSFunctionWriter::WriteDefaultStructParameters(String& source)
  224. {
  225. for (unsigned i = 0; i < defaultStructParameters_.Size(); i++)
  226. {
  227. const DefaultStructParameter& dparm = defaultStructParameters_[i];
  228. String line = ToString("if (default(%s).Equals(%s)) %s = %s;\n",
  229. dparm.type.CString(), dparm.parameterName.CString(), dparm.parameterName.CString(),
  230. dparm.assignment.CString());
  231. source += IndentLine(line);
  232. }
  233. }
  234. void CSFunctionWriter::WriteManagedPInvokeFunctionSignature(String& source)
  235. {
  236. source += "\n";
  237. // CoreCLR has pinvoke security demand code commented out, so we do not (currently) need this optimization:
  238. // https://github.com/dotnet/coreclr/issues/1605
  239. // line = "[SuppressUnmanagedCodeSecurity]\n";
  240. // source += IndentLine(line);
  241. String line = "[DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]\n";
  242. source += IndentLine(line);
  243. JSBClass* klass = function_->GetClass();
  244. JSBPackage* package = klass->GetPackage();
  245. String returnType = CSTypeHelper::GetPInvokeTypeString(function_->GetReturnType());
  246. if (returnType == "string")
  247. returnType = "IntPtr";
  248. if (function_->IsConstructor())
  249. returnType = "IntPtr";
  250. Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  251. Vector<String> args;
  252. if (!function_->IsConstructor() && !function_->IsStatic())
  253. {
  254. args.Push("IntPtr self");
  255. }
  256. if (parameters.Size())
  257. {
  258. for (unsigned int i = 0; i < parameters.Size(); i++)
  259. {
  260. JSBFunctionType* ptype = parameters.At(i);
  261. String name = ptype->name_;
  262. if (name == "object")
  263. name = "_object";
  264. else if (name == "readonly")
  265. name = "readOnly";
  266. else if (name == "params")
  267. name = "parameters";
  268. // ignore "Context" parameters
  269. if (ptype->type_->asClassType())
  270. {
  271. JSBClassType* classType = ptype->type_->asClassType();
  272. JSBClass* klass = classType->class_;
  273. if (klass->GetName() == "Context")
  274. {
  275. continue;
  276. }
  277. if (klass->IsNumberArray())
  278. {
  279. args.Push("ref " + klass->GetName() + " " + name);
  280. }
  281. else
  282. {
  283. args.Push("IntPtr " + name);
  284. }
  285. }
  286. else
  287. {
  288. args.Push(CSTypeHelper::GetPInvokeTypeString(ptype) + " " + name);
  289. }
  290. }
  291. }
  292. if (function_->GetReturnClass())
  293. {
  294. JSBClass* retClass = function_->GetReturnClass();
  295. if (retClass->IsNumberArray())
  296. {
  297. args.Push("ref " + retClass->GetName() + " retValue");
  298. }
  299. }
  300. String pstring;
  301. pstring.Join(args, ", ");
  302. String fname = function_->IsConstructor() ? "Constructor" : function_->GetName();
  303. line = ToString("private static extern %s csb_%s_%s_%s(%s);\n",
  304. returnType.CString(), package->GetName().CString(), klass->GetName().CString(),
  305. fname.CString(), pstring.CString());
  306. source += IndentLine(line);
  307. source += "\n";
  308. }
  309. void CSFunctionWriter::GenManagedFunctionParameters(String& sig)
  310. {
  311. // generate args
  312. Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  313. if (parameters.Size())
  314. {
  315. for (unsigned int i = 0; i < parameters.Size(); i++)
  316. {
  317. JSBFunctionType* ptype = parameters.At(i);
  318. // ignore "Context" parameters
  319. if (ptype->type_->asClassType())
  320. {
  321. JSBClassType* classType = ptype->type_->asClassType();
  322. JSBClass* klass = classType->class_;
  323. if (klass->GetName() == "Context")
  324. {
  325. continue;
  326. }
  327. }
  328. sig += CSTypeHelper::GetManagedTypeString(ptype);
  329. String init = ptype->initializer_;
  330. if (init.Length())
  331. {
  332. init = MapDefaultParameter(ptype);
  333. if (init.Length())
  334. sig += " = " + init;
  335. }
  336. if (i + 1 != parameters.Size())
  337. sig += ", ";
  338. }
  339. }
  340. }
  341. void CSFunctionWriter::WriteManagedConstructor(String& source)
  342. {
  343. JSBClass* klass = function_->GetClass();
  344. JSBPackage* package = klass->GetPackage();
  345. if (klass->GetName() == "RefCounted")
  346. return;
  347. // wrapping constructor
  348. String line;
  349. line = ToString("public %s (IntPtr native) : base (native)\n", klass->GetName().CString());
  350. source += IndentLine(line);
  351. source += IndentLine("{\n");
  352. source += IndentLine("}\n\n");
  353. String sig;
  354. GenManagedFunctionParameters(sig);
  355. line = ToString("public %s (%s)\n", klass->GetName().CString(), sig.CString());
  356. source += IndentLine(line);
  357. source += IndentLine("{\n");
  358. Indent();
  359. WriteDefaultStructParameters(source);
  360. source += IndentLine("if (nativeInstance == IntPtr.Zero)\n");
  361. source += IndentLine("{\n");
  362. Indent();
  363. source += IndentLine(ToString("var classType = typeof(%s);\n", klass->GetName().CString()));
  364. source += IndentLine("var thisType = this.GetType();\n");
  365. source += IndentLine("if ( thisType == classType || thisType.BaseType == classType)\n");
  366. source += IndentLine("{\n");
  367. Indent();
  368. String callSig;
  369. GenPInvokeCallParameters(callSig);
  370. source += IndentLine("IntPtr nativeInstanceOverride = NativeCore.NativeContructorOverride;\n");
  371. line = ToString("nativeInstance = NativeCore.RegisterNative (nativeInstanceOverride != IntPtr.Zero ? nativeInstanceOverride : csb_%s_%s_Constructor(%s), this);\n",
  372. package->GetName().CString(), klass->GetName().CString(), callSig.CString());
  373. source += IndentLine(line);
  374. Dedent();
  375. source += IndentLine("}\n");
  376. Dedent();
  377. source += IndentLine("}\n");
  378. Dedent();
  379. source += IndentLine("}\n");
  380. }
  381. void CSFunctionWriter::GenPInvokeCallParameters(String& sig)
  382. {
  383. // generate args
  384. Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  385. if (parameters.Size())
  386. {
  387. for (unsigned int i = 0; i < parameters.Size(); i++)
  388. {
  389. JSBFunctionType* ptype = parameters.At(i);
  390. // ignore "Context" parameters
  391. if (ptype->type_->asClassType())
  392. {
  393. JSBClassType* classType = ptype->type_->asClassType();
  394. JSBClass* klass = classType->class_;
  395. if (klass->GetName() == "Context")
  396. {
  397. continue;
  398. }
  399. }
  400. String name = ptype->name_;
  401. if (name == "object")
  402. name = "_object";
  403. else if (name == "readonly")
  404. name = "readOnly";
  405. else if (name == "params")
  406. name = "parameters";
  407. if (ptype->type_->asClassType())
  408. {
  409. JSBClass* pclass = ptype->type_->asClassType()->class_;
  410. if (pclass->IsNumberArray())
  411. {
  412. sig += "ref " + name;
  413. }
  414. else
  415. {
  416. sig += name + " == null ? IntPtr.Zero : " + name + ".nativeInstance";
  417. }
  418. }
  419. else
  420. {
  421. sig += name;
  422. }
  423. if (i + 1 != parameters.Size())
  424. sig += ", ";
  425. }
  426. }
  427. // data marshaller
  428. if (function_->GetReturnType() && !CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
  429. {
  430. if (function_->GetReturnClass()->IsNumberArray())
  431. {
  432. if (sig.Length())
  433. sig += ", ";
  434. JSBClass* klass = function_->GetClass();
  435. sig += ToString("ref %s%sReturnValue", klass->GetName().CString(), function_->GetName().CString());
  436. }
  437. }
  438. }
  439. void CSFunctionWriter::WriteManagedFunction(String& source)
  440. {
  441. JSBClass* klass = function_->GetClass();
  442. JSBPackage* package = klass->GetPackage();
  443. String sig;
  444. String returnType = CSTypeHelper::GetManagedTypeString(function_->GetReturnType());
  445. GenManagedFunctionParameters(sig);
  446. String line = "public ";
  447. if (function_->IsStatic())
  448. {
  449. line += "static ";
  450. }
  451. bool marked = false;
  452. JSBClass* baseClass = klass->GetBaseClass();
  453. if (baseClass)
  454. {
  455. JSBFunction* override = baseClass->MatchFunction(function_, true);
  456. if (override)
  457. {
  458. marked = true;
  459. if (override->IsVirtual())
  460. line += "override ";
  461. else
  462. line += "new ";
  463. }
  464. }
  465. if (!marked && function_->IsVirtual())
  466. line += "virtual ";
  467. line += ToString("%s %s (%s)\n", returnType.CString(), function_->GetName().CString(), sig.CString());
  468. source += IndentLine(line);
  469. source += IndentLine("{\n");
  470. Indent();
  471. WriteDefaultStructParameters(source);
  472. line.Clear();
  473. if (function_->GetReturnType())
  474. {
  475. if (function_->GetReturnType()->type_->asStringType() || function_->GetReturnType()->type_->asStringHashType())
  476. {
  477. line += "return System.Runtime.InteropServices.Marshal.PtrToStringAnsi(";
  478. }
  479. else if (CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
  480. line += "return ";
  481. else
  482. {
  483. if (function_->GetReturnClass())
  484. {
  485. if (!function_->GetReturnClass()->IsNumberArray())
  486. line += "IntPtr retNativeInstance = ";
  487. }
  488. }
  489. }
  490. String callSig;
  491. GenPInvokeCallParameters(callSig);
  492. String nativeInstance;
  493. if (!function_->IsStatic())
  494. nativeInstance = "nativeInstance";
  495. line += ToString("csb_%s_%s_%s(%s",
  496. package->GetName().CString(), klass->GetName().CString(), function_->GetName().CString(), nativeInstance.CString());
  497. if (callSig.Length())
  498. {
  499. if (nativeInstance.Length())
  500. line += ", " + callSig;
  501. else
  502. line += callSig;
  503. }
  504. if (function_->GetReturnType())
  505. {
  506. if (function_->GetReturnType()->type_->asStringType() || function_->GetReturnType()->type_->asStringHashType())
  507. line += ")";
  508. }
  509. line += ");\n";
  510. source += IndentLine(line);
  511. if (function_->GetReturnType() && !CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
  512. {
  513. if (function_->GetReturnType()->type_->asClassType())
  514. {
  515. JSBClass* retClass = function_->GetReturnClass();
  516. JSBClass* klass = function_->GetClass();
  517. if (retClass->IsNumberArray())
  518. {
  519. line = ToString("return %s%sReturnValue;", klass->GetName().CString(), function_->GetName().CString());
  520. }
  521. else
  522. {
  523. line = ToString("return retNativeInstance == IntPtr.Zero ? null : NativeCore.WrapNative<%s> (retNativeInstance);", retClass->GetName().CString());
  524. }
  525. source += IndentLine(line);
  526. source+= "\n";
  527. }
  528. }
  529. Dedent();
  530. source += IndentLine("}\n");
  531. }
  532. void CSFunctionWriter::GenerateManagedSource(String& sourceOut)
  533. {
  534. String source = "";
  535. Indent();
  536. Indent();
  537. if (function_->GetDocString().Length())
  538. {
  539. // monodocer -assembly:NETCore.dll -path:en -pretty
  540. // mdoc export-html -o htmldocs en
  541. source += IndentLine("/// <summary>\n");
  542. if (function_->GetDocString().Contains('\n'))
  543. source += IndentLine("/* " + function_->GetDocString() + "*/\n");
  544. else
  545. source += IndentLine("/// " + function_->GetDocString() + "\n");
  546. source += IndentLine("/// </summary>\n");
  547. }
  548. if (function_->IsConstructor())
  549. WriteManagedConstructor(source);
  550. else
  551. WriteManagedFunction(source);
  552. WriteManagedPInvokeFunctionSignature(source);
  553. // data marshaller
  554. if (function_->GetReturnType() && !CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
  555. {
  556. if (function_->GetReturnClass())
  557. {
  558. JSBClass* retClass = function_->GetReturnClass();
  559. if (retClass->IsNumberArray())
  560. {
  561. JSBClass* klass = function_->GetClass();
  562. String managedType = CSTypeHelper::GetManagedTypeString(function_->GetReturnType());
  563. String marshal = "private ";
  564. if (function_->IsStatic())
  565. marshal += "static ";
  566. marshal += managedType + " ";
  567. marshal += ToString("%s%sReturnValue = new %s();\n", klass->GetName().CString(), function_->GetName().CString(), managedType.CString());
  568. sourceOut += IndentLine(marshal);
  569. }
  570. }
  571. }
  572. Dedent();
  573. Dedent();
  574. sourceOut += source;
  575. }
  576. void CSFunctionWriter::GenerateSource(String& sourceOut)
  577. {
  578. }
  579. String CSFunctionWriter::MapDefaultParameter(JSBFunctionType* parameter)
  580. {
  581. String init = parameter->initializer_;
  582. if (!init.Length())
  583. return init;
  584. if (parameter->type_->asClassType())
  585. {
  586. if (init == "0")
  587. return "null";
  588. }
  589. if (parameter->type_->asEnumType())
  590. {
  591. return parameter->type_->asEnumType()->enum_->GetName() + "." + init;
  592. }
  593. if (function_->class_->GetPackage()->ContainsConstant(init))
  594. return "Constants." + init;
  595. if (init == "true" || init == "false")
  596. return init;
  597. if (init == "0.0f")
  598. return init;
  599. if (init == "1.0f")
  600. return init;
  601. if (init == "0.1f")
  602. return init;
  603. if (init == "0")
  604. return init;
  605. if (init == "3")
  606. return init;
  607. if (init == "-1")
  608. return init;
  609. if (init == "\"\\t\"")
  610. return init;
  611. if (init == "NULL")
  612. return "null";
  613. if (init == "M_MAX_UNSIGNED")
  614. return "0xffffffff";
  615. if (init == "String::EMPTY")
  616. return "\"\"";
  617. // this kind of sucks, can't define const structs
  618. // and default parameters need to be const :/
  619. DefaultStructParameter dparm;
  620. dparm.parameterName = parameter->name_;
  621. if (init == "Vector3::ZERO")
  622. {
  623. dparm.type = "Vector3";
  624. dparm.assignment = "Vector3.Zero";
  625. defaultStructParameters_.Push(dparm);
  626. return "default(Vector3)";
  627. }
  628. if (init == "Vector3::ONE")
  629. {
  630. dparm.type = "Vector3";
  631. dparm.assignment = "Vector3.One";
  632. defaultStructParameters_.Push(dparm);
  633. return "default(Vector3)";
  634. }
  635. if (init == "Vector3::UP")
  636. {
  637. dparm.type = "Vector3";
  638. dparm.assignment = "Vector3.Up";
  639. defaultStructParameters_.Push(dparm);
  640. return "default(Vector3)";
  641. }
  642. if (init == "IntVector2::ZERO")
  643. {
  644. dparm.type = "IntVector2";
  645. dparm.assignment = "IntVector2.Zero";
  646. defaultStructParameters_.Push(dparm);
  647. return "default(IntVector2)";
  648. }
  649. if (init == "Quaternion::IDENTITY")
  650. {
  651. dparm.type = "Quaternion";
  652. dparm.assignment = "Quaternion.Identity";
  653. defaultStructParameters_.Push(dparm);
  654. return "default(Quaternion)";
  655. }
  656. return String::EMPTY;
  657. }
  658. }