CSPackageWriter.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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/File.h>
  23. #include <Atomic/IO/FileSystem.h>
  24. #include <Atomic/Core/StringUtils.h>
  25. #include "../JSBind.h"
  26. #include "../JSBFunction.h"
  27. #include "../JSBModule.h"
  28. #include "../JSBPackage.h"
  29. #include "../JSBEnum.h"
  30. #include "../JSBClass.h"
  31. #include "CSTypeHelper.h"
  32. #include "CSModuleWriter.h"
  33. #include "CSPackageWriter.h"
  34. namespace ToolCore
  35. {
  36. CSPackageWriter::CSPackageWriter(JSBPackage *package) : JSBPackageWriter(package)
  37. {
  38. }
  39. void CSPackageWriter::GenNativeFunctionSignature(JSBFunction* function, String& sig)
  40. {
  41. JSBClass* klass = function->GetClass();
  42. Vector<JSBFunctionType*>& parameters = function->GetParameters();
  43. Vector<String> args;
  44. if (!function->IsConstructor())
  45. {
  46. args.Push(ToString("%s* self", klass->GetNativeName().CString()));
  47. }
  48. if (parameters.Size())
  49. {
  50. for (unsigned int i = 0; i < parameters.Size(); i++)
  51. {
  52. JSBFunctionType* ptype = parameters.At(i);
  53. // ignore "Context" parameters
  54. if (ptype->type_->asClassType())
  55. {
  56. JSBClassType* classType = ptype->type_->asClassType();
  57. JSBClass* klass = classType->class_;
  58. if (klass->GetName() == "Context")
  59. {
  60. continue;
  61. }
  62. args.Push(ToString("%s* %s", klass->GetNativeName().CString(), ptype->name_.CString()));
  63. }
  64. else
  65. {
  66. args.Push(CSTypeHelper::GetNativeTypeString(ptype) + " " + ptype->name_);
  67. }
  68. }
  69. }
  70. if (function->GetReturnClass() && function->GetReturnClass()->IsNumberArray())
  71. {
  72. args.Push(ToString("%s* returnValue", function->GetReturnClass()->GetNativeName().CString()));
  73. }
  74. sig.Join(args, ", ");
  75. }
  76. void CSPackageWriter::GenerateNativeHeader()
  77. {
  78. String source = "// This file was autogenerated by AtomicTool, changes will be lost\n\n";
  79. source += "#pragma once\n\nnamespace Atomic\n{\n}\n";
  80. /*
  81. if (package_->name_ != "Atomic")
  82. {
  83. source += "#include \"../../Atomic/Native/CSPackageAtomic.h\"\n";
  84. source += "using namespace Atomic;\n";
  85. }
  86. // enum includes
  87. PODVector<JSBHeader*> enumHeaders;
  88. for (unsigned i = 0; i < package_->modules_.Size(); i++)
  89. {
  90. Vector<SharedPtr<JSBEnum>> enums = package_->modules_[i]->GetEnums();
  91. for (unsigned j = 0; j < enums.Size(); j++)
  92. {
  93. JSBHeader* header = enums[j]->GetHeader();
  94. if (!enumHeaders.Contains(header))
  95. enumHeaders.Push(header);
  96. }
  97. }
  98. for (unsigned i = 0; i < enumHeaders.Size(); i++)
  99. {
  100. JSBHeader* header = enumHeaders[i];
  101. String headerPath = GetPath(header->GetFilePath());
  102. String headerfile = GetFileNameAndExtension(header->GetFilePath());
  103. JSBind* jsbind = header->GetSubsystem<JSBind>();
  104. headerPath.Replace(jsbind->GetSourceRootFolder() + "Source/", "");
  105. source.AppendWithFormat("#include <%s%s>\n", headerPath.CString(), headerfile.CString());
  106. }
  107. // forward declarations of package classes
  108. source += ToString("namespace %s\n{\n\n", package_->GetNamespace().CString());
  109. Indent();
  110. PODVector<JSBClass*>& allClasses = package_->GetAllClasses();
  111. for (unsigned i = 0; i < allClasses.Size(); i++)
  112. {
  113. JSBClass* cls = allClasses[i];
  114. source += IndentLine(ToString("class %s;\n", cls->GetNativeName().CString()));
  115. }
  116. Dedent();
  117. source += "\n}\n";
  118. */
  119. JSBind* jsbind = package_->GetSubsystem<JSBind>();
  120. String filepath = jsbind->GetDestNativeFolder() + "/CSPackage" + package_->name_ + ".h";
  121. File file(package_->GetContext());
  122. file.Open(filepath, FILE_WRITE);
  123. file.Write(source.CString(), source.Length());
  124. file.Close();
  125. }
  126. void CSPackageWriter::GenerateNativeSource()
  127. {
  128. GenerateNativeHeader();
  129. String source = "// This file was autogenerated by AtomicTool, changes will be lost\n\n";
  130. String defineGuard = package_->GetPlatformDefineGuard();
  131. if (defineGuard.Length())
  132. {
  133. source += ToString("%s\n\n", defineGuard.CString());
  134. }
  135. const char* packageName = package_->name_.CString();
  136. String packageHeader = "CSPackage" + package_->name_ + ".h";
  137. source += ToString("#include \"%s\"\n", packageHeader.CString());
  138. if (package_->name_ != "Atomic")
  139. source += "using namespace Atomic;\n";
  140. source += ToString("namespace %s\n{\n", packageName);
  141. // end of namespace
  142. source += "\n}\n";
  143. if (defineGuard.Length())
  144. {
  145. source += "\n#endif\n";
  146. }
  147. JSBind* jsbind = package_->GetSubsystem<JSBind>();
  148. String filepath = jsbind->GetDestNativeFolder() + "/CSPackage" + package_->name_ + ".cpp";
  149. File file(package_->GetContext());
  150. file.Open(filepath, FILE_WRITE);
  151. file.Write(source.CString(), source.Length());
  152. file.Close();
  153. }
  154. void CSPackageWriter::GenerateManagedSource()
  155. {
  156. }
  157. void CSPackageWriter::GenerateSource()
  158. {
  159. GenerateNativeSource();
  160. GenerateManagedSource();
  161. for (unsigned i = 0; i < package_->modules_.Size(); i++)
  162. {
  163. CSModuleWriter writer(package_->modules_[i]);
  164. writer.GenerateSource();
  165. }
  166. }
  167. }