CSClassWriter.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. // LICENSE: Atomic Game Engine Editor and Tools EULA
  4. // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
  5. // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
  6. //
  7. #include <Atomic/IO/FileSystem.h>
  8. #include "../JSBind.h"
  9. #include "../JSBModule.h"
  10. #include "../JSBPackage.h"
  11. #include "../JSBEnum.h"
  12. #include "../JSBClass.h"
  13. #include "../JSBFunction.h"
  14. #include "CSTypeHelper.h"
  15. #include "CSClassWriter.h"
  16. #include "CSFunctionWriter.h"
  17. namespace ToolCore
  18. {
  19. CSClassWriter::CSClassWriter(JSBClass *klass) : JSBClassWriter(klass)
  20. {
  21. }
  22. void CSClassWriter::WriteNativeFunctions(String& source)
  23. {
  24. for (unsigned i = 0; i < klass_->functions_.Size(); i++)
  25. {
  26. JSBFunction* function = klass_->functions_.At(i);
  27. if (function->Skip())
  28. continue;
  29. if (function->IsDestructor())
  30. continue;
  31. if (OmitFunction(function))
  32. continue;
  33. CSFunctionWriter writer(function);
  34. writer.GenerateNativeSource(source);
  35. }
  36. }
  37. void CSClassWriter::GenerateNativeSource(String& sourceOut)
  38. {
  39. String source = "";
  40. if (klass_->IsNumberArray())
  41. return;
  42. JSBPackage* package = klass_->GetPackage();
  43. source.AppendWithFormat("ClassID csb_%s_%s_GetClassIDStatic()\n{\n", package->GetName().CString(),klass_->GetName().CString());
  44. source.AppendWithFormat(" return %s::GetClassIDStatic();\n}\n\n", klass_->GetNativeName().CString());
  45. WriteNativeFunctions(source);
  46. sourceOut += source;
  47. }
  48. void CSClassWriter::WriteManagedProperties(String& sourceOut)
  49. {
  50. String source;
  51. if (klass_->HasProperties())
  52. {
  53. Vector<String> pnames;
  54. klass_->GetPropertyNames(pnames);
  55. for (unsigned j = 0; j < pnames.Size(); j++)
  56. {
  57. JSBProperty* prop = klass_->GetProperty(pnames[j]);
  58. JSBFunctionType* fType = NULL;
  59. JSBFunctionType* getType = NULL;
  60. JSBFunctionType* setType = NULL;
  61. if (OmitFunction(prop->getter_) || OmitFunction(prop->setter_))
  62. continue;
  63. if (prop->getter_ && !prop->getter_->Skip())
  64. {
  65. fType = getType = prop->getter_->GetReturnType();
  66. }
  67. if (prop->setter_ && !prop->setter_->Skip())
  68. {
  69. setType = prop->setter_->GetParameters()[0];
  70. if (!fType)
  71. fType = setType;
  72. //else if (fType->type_ != setType->type_)
  73. // continue;
  74. }
  75. if (!fType)
  76. continue;
  77. String type = CSTypeHelper::GetManagedTypeString(fType, false);
  78. String line = ToString("public %s %s\n", type.CString(), prop->name_.CString());
  79. source += IndentLine(line);
  80. source += IndentLine("{\n");
  81. Indent();
  82. if (prop->getter_)
  83. {
  84. source += IndentLine("get\n");
  85. source += IndentLine("{\n");
  86. Indent();
  87. source += IndentLine(ToString("return %s();\n", prop->getter_->GetName().CString()));
  88. Dedent();
  89. source += IndentLine("}\n");
  90. }
  91. if (prop->setter_)
  92. {
  93. source += IndentLine("set\n");
  94. source += IndentLine("{\n");
  95. Indent();
  96. source += IndentLine(ToString("%s(value);\n", prop->setter_->GetName().CString()));
  97. Dedent();
  98. source += IndentLine("}\n");
  99. }
  100. Dedent();
  101. source += IndentLine("}\n\n");
  102. }
  103. }
  104. sourceOut += source;
  105. }
  106. bool CSClassWriter::OmitFunction(JSBFunction* function)
  107. {
  108. if (!function)
  109. return false;
  110. // We need to rename GetType
  111. if (function->GetName() == "GetType")
  112. return true;
  113. // avoid vector type for now
  114. if (function->GetReturnType() && function->GetReturnType()->type_->asVectorType())
  115. return true;
  116. Vector<JSBFunctionType*>& parameters = function->GetParameters();
  117. for (unsigned i = 0; i < parameters.Size(); i++)
  118. {
  119. if (parameters[i]->type_->asVectorType())
  120. return true;
  121. }
  122. return false;
  123. }
  124. void CSClassWriter::GenerateManagedSource(String& sourceOut)
  125. {
  126. String source = "";
  127. if (klass_->IsNumberArray())
  128. return;
  129. Indent();
  130. source += "\n";
  131. String line;
  132. if (klass_->GetBaseClass())
  133. line = "public partial class " + klass_->GetName() + " : " + klass_->GetBaseClass()->GetName() + "\n";
  134. else
  135. line = "public partial class " + klass_->GetName() + "\n";
  136. source += IndentLine(line);
  137. source += IndentLine("{\n");
  138. Indent();
  139. WriteManagedProperties(source);
  140. Indent();
  141. JSBPackage* package = klass_->GetPackage();
  142. line = "[DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]\n";
  143. source += IndentLine(line);
  144. line = ToString("public static extern IntPtr csb_%s_%s_GetClassIDStatic();\n", package->GetName().CString(),klass_->GetName().CString());
  145. source += IndentLine(line);
  146. source += "\n";
  147. Dedent();
  148. // managed functions
  149. for (unsigned i = 0; i < klass_->functions_.Size(); i++)
  150. {
  151. JSBFunction* function = klass_->functions_.At(i);
  152. if (function->Skip())
  153. continue;
  154. if (function->IsDestructor())
  155. continue;
  156. if (OmitFunction(function))
  157. continue;
  158. CSFunctionWriter fwriter(function);
  159. fwriter.GenerateManagedSource(source);
  160. }
  161. Dedent();
  162. source += IndentLine("}\n");
  163. Dedent();
  164. sourceOut += source;
  165. }
  166. void CSClassWriter::GenerateSource(String& sourceOut)
  167. {
  168. }
  169. }