CSClassWriter.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. CSFunctionWriter writer(function);
  32. writer.GenerateNativeSource(source);
  33. }
  34. }
  35. void CSClassWriter::GenerateNativeSource(String& sourceOut)
  36. {
  37. String source = "";
  38. if (klass_->IsNumberArray())
  39. return;
  40. source.AppendWithFormat("ClassID csb_%s_GetClassID()\n{\n", klass_->GetNativeName().CString());
  41. source.AppendWithFormat("return %s::GetClassIDStatic();\n}\n", klass_->GetNativeName().CString());
  42. WriteNativeFunctions(source);
  43. sourceOut += source;
  44. }
  45. void CSClassWriter::WriteManagedProperties(String& sourceOut)
  46. {
  47. String source;
  48. if (klass_->HasProperties())
  49. {
  50. Vector<String> pnames;
  51. klass_->GetPropertyNames(pnames);
  52. for (unsigned j = 0; j < pnames.Size(); j++)
  53. {
  54. JSBProperty* prop = klass_->GetProperty(pnames[j]);
  55. JSBFunctionType* fType = NULL;
  56. JSBFunctionType* getType = NULL;
  57. JSBFunctionType* setType = NULL;
  58. if (prop->getter_ && !prop->getter_->Skip())
  59. {
  60. fType = getType = prop->getter_->GetReturnType();
  61. }
  62. if (prop->setter_ && !prop->setter_->Skip())
  63. {
  64. setType = prop->setter_->GetParameters()[0];
  65. if (!fType)
  66. fType = setType;
  67. }
  68. String type = CSTypeHelper::GetManagedTypeString(fType, false);
  69. String line = ToString("public %s %s\n", type.CString(), prop->name_.CString());
  70. source += IndentLine(line);
  71. source += IndentLine("{\n");
  72. Indent();
  73. if (prop->getter_)
  74. {
  75. source += IndentLine("get\n");
  76. source += IndentLine("{\n");
  77. Indent();
  78. source += IndentLine(ToString("return %s();\n", prop->getter_->GetName().CString()));
  79. Dedent();
  80. source += IndentLine("}\n");
  81. }
  82. if (prop->setter_)
  83. {
  84. source += IndentLine("set\n");
  85. source += IndentLine("{\n");
  86. Indent();
  87. source += IndentLine(ToString("%s(value);\n", prop->setter_->GetName().CString()));
  88. Dedent();
  89. source += IndentLine("}\n");
  90. }
  91. Dedent();
  92. source += IndentLine("}\n\n");
  93. }
  94. }
  95. sourceOut += source;
  96. }
  97. void CSClassWriter::GenerateManagedSource(String& sourceOut)
  98. {
  99. String source = "";
  100. if (klass_->IsNumberArray())
  101. return;
  102. Indent();
  103. source += "\n";
  104. String line;
  105. if (klass_->GetBaseClass())
  106. line = "public partial class " + klass_->GetName() + " : " + klass_->GetBaseClass()->GetName() + "\n";
  107. else
  108. line = "public partial class " + klass_->GetName() + "\n";
  109. source += IndentLine(line);
  110. source += IndentLine("{\n");
  111. Indent();
  112. WriteManagedProperties(source);
  113. // managed functions
  114. for (unsigned i = 0; i < klass_->functions_.Size(); i++)
  115. {
  116. JSBFunction* function = klass_->functions_.At(i);
  117. if (function->Skip())
  118. continue;
  119. if (function->IsDestructor())
  120. continue;
  121. CSFunctionWriter fwriter(function);
  122. fwriter.GenerateManagedSource(source);
  123. }
  124. Dedent();
  125. source += IndentLine("}\n");
  126. Dedent();
  127. sourceOut += source;
  128. }
  129. void CSClassWriter::GenerateSource(String& sourceOut)
  130. {
  131. }
  132. }