CSPackageWriter.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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/File.h>
  8. #include <Atomic/IO/FileSystem.h>
  9. #include <Atomic/Core/StringUtils.h>
  10. #include "../JSBind.h"
  11. #include "../JSBFunction.h"
  12. #include "../JSBModule.h"
  13. #include "../JSBPackage.h"
  14. #include "../JSBEnum.h"
  15. #include "../JSBClass.h"
  16. #include "CSTypeHelper.h"
  17. #include "CSModuleWriter.h"
  18. #include "CSPackageWriter.h"
  19. namespace ToolCore
  20. {
  21. CSPackageWriter::CSPackageWriter(JSBPackage *package) : JSBPackageWriter(package)
  22. {
  23. }
  24. void CSPackageWriter::GenerateNativeFunctionThunk(String& sourceOut)
  25. {
  26. String source, line;
  27. source += IndentLine(ToString("\n\ntypedef struct AtomicNET%sThunk_s\n", package_->GetName().CString()));
  28. source += IndentLine("{\n");
  29. Indent();
  30. PODVector<JSBClass*>& allClasses = package_->GetAllClasses();
  31. for (unsigned i = 0; i < allClasses.Size(); i++)
  32. {
  33. JSBClass* cls = allClasses[i];
  34. PODVector<JSBFunction*>& functions = cls->GetFunctions();
  35. for (unsigned j = 0; j < functions.Size(); j++)
  36. {
  37. JSBFunction* function = functions[j];
  38. if (!CSTypeHelper::OmitFunction(function))
  39. {
  40. JSBClass* cls = function->GetClass();
  41. JSBPackage* package = cls->GetPackage();
  42. line = ToString("%s_%s_%s_Function ", package->GetName().CString(),
  43. cls->GetName().CString(),
  44. function->IsConstructor() ? "Constructor" : function->GetName().CString());
  45. line += ToString("__%s_%s_%s;\n", package->GetName().CString(),
  46. cls->GetName().CString(),
  47. function->IsConstructor() ? "Constructor" : function->GetName().CString());
  48. source += IndentLine(line);
  49. }
  50. }
  51. }
  52. Dedent();
  53. const char* packageName = package_->GetName().CString();
  54. source += IndentLine(ToString("\n} AtomicNET%sThunk_t;\n",packageName));
  55. source += IndentLine(ToString("extern AtomicNET%sThunk_t AtomicNET%sThunk;\n", packageName, packageName));
  56. source += IndentLine(ToString("extern bool AtomicNET%sThunkEnabled;\n", packageName));
  57. sourceOut += source;
  58. }
  59. void CSPackageWriter::GenNativeFunctionSignature(JSBFunction* function, String& sig)
  60. {
  61. JSBClass* klass = function->GetClass();
  62. Vector<JSBFunctionType*>& parameters = function->GetParameters();
  63. Vector<String> args;
  64. if (!function->IsConstructor())
  65. {
  66. args.Push(ToString("%s* self", klass->GetNativeName().CString()));
  67. }
  68. if (parameters.Size())
  69. {
  70. for (unsigned int i = 0; i < parameters.Size(); i++)
  71. {
  72. JSBFunctionType* ptype = parameters.At(i);
  73. // ignore "Context" parameters
  74. if (ptype->type_->asClassType())
  75. {
  76. JSBClassType* classType = ptype->type_->asClassType();
  77. JSBClass* klass = classType->class_;
  78. if (klass->GetName() == "Context")
  79. {
  80. continue;
  81. }
  82. args.Push(ToString("%s* %s", klass->GetNativeName().CString(), ptype->name_.CString()));
  83. }
  84. else
  85. {
  86. args.Push(CSTypeHelper::GetNativeTypeString(ptype) + " " + ptype->name_);
  87. }
  88. }
  89. }
  90. if (function->GetReturnClass() && function->GetReturnClass()->IsNumberArray())
  91. {
  92. args.Push(ToString("%s* returnValue", function->GetReturnClass()->GetNativeName().CString()));
  93. }
  94. sig.Join(args, ", ");
  95. }
  96. void CSPackageWriter::GenerateNativeFunctionTypeDefs(String& sourceOut)
  97. {
  98. // call typedefs
  99. Indent();
  100. String source, line;
  101. PODVector<JSBClass*>& allClasses = package_->GetAllClasses();
  102. for (unsigned i = 0; i < allClasses.Size(); i++)
  103. {
  104. JSBClass* cls = allClasses[i];
  105. PODVector<JSBFunction*>& functions = cls->GetFunctions();
  106. for (unsigned j = 0; j < functions.Size(); j++)
  107. {
  108. JSBFunction* function = functions[j];
  109. String returnType;
  110. line = CSTypeHelper::GetNativeFunctionSignature(function, returnType, true);
  111. if (line.Length())
  112. {
  113. line += ";\n";
  114. source += IndentLine(line);
  115. }
  116. }
  117. }
  118. Indent();
  119. sourceOut += source;
  120. }
  121. void CSPackageWriter::GenerateNativeHeader()
  122. {
  123. String source = "// This file was autogenerated by AtomicTool, changes will be lost\n\n";
  124. source += "#pragma once\n\n";
  125. if (package_->name_ != "Atomic")
  126. {
  127. source += "#include \"../../Atomic/Native/CSPackageAtomic.h\"\n";
  128. source += "using namespace Atomic;\n";
  129. }
  130. // enum includes
  131. PODVector<JSBHeader*> enumHeaders;
  132. for (unsigned i = 0; i < package_->modules_.Size(); i++)
  133. {
  134. Vector<SharedPtr<JSBEnum>> enums = package_->modules_[i]->GetEnums();
  135. for (unsigned j = 0; j < enums.Size(); j++)
  136. {
  137. JSBHeader* header = enums[j]->GetHeader();
  138. if (!enumHeaders.Contains(header))
  139. enumHeaders.Push(header);
  140. }
  141. }
  142. for (unsigned i = 0; i < enumHeaders.Size(); i++)
  143. {
  144. JSBHeader* header = enumHeaders[i];
  145. String headerPath = GetPath(header->GetFilePath());
  146. String headerfile = GetFileNameAndExtension(header->GetFilePath());
  147. JSBind* jsbind = header->GetSubsystem<JSBind>();
  148. headerPath.Replace(jsbind->GetSourceRootFolder() + "Source/", "");
  149. source.AppendWithFormat("#include <%s%s>\n", headerPath.CString(), headerfile.CString());
  150. }
  151. // forward declarations of package classes
  152. source += ToString("namespace %s\n{\n\n", package_->GetNamespace().CString());
  153. Indent();
  154. PODVector<JSBClass*>& allClasses = package_->GetAllClasses();
  155. for (unsigned i = 0; i < allClasses.Size(); i++)
  156. {
  157. JSBClass* cls = allClasses[i];
  158. source += IndentLine(ToString("class %s;\n", cls->GetNativeName().CString()));
  159. }
  160. Dedent();
  161. GenerateNativeFunctionTypeDefs(source);
  162. GenerateNativeFunctionThunk(source);
  163. source += "\n}\n";
  164. JSBind* jsbind = package_->GetSubsystem<JSBind>();
  165. String filepath = jsbind->GetDestNativeFolder() + "/CSPackage" + package_->name_ + ".h";
  166. File file(package_->GetContext());
  167. file.Open(filepath, FILE_WRITE);
  168. file.Write(source.CString(), source.Length());
  169. file.Close();
  170. }
  171. void CSPackageWriter::GenerateNativeThunkInit(String& sourceOut)
  172. {
  173. const char* packageName = package_->GetName().CString();
  174. String source, line;
  175. for (unsigned i = 0; i < package_->modules_.Size(); i++)
  176. {
  177. JSBModule* module = package_->modules_[i];
  178. const char* moduleName = module->GetName().CString();
  179. source.AppendWithFormat("extern \"C\" void csb_package_%s_init_%s_thunk ();\n", packageName, moduleName);
  180. }
  181. source.AppendWithFormat("\nvoid csb_package_init_%s_thunk ()\n{\n\n", packageName);
  182. Indent();
  183. for (unsigned i = 0; i < package_->modules_.Size(); i++)
  184. {
  185. JSBModule* module = package_->modules_[i];
  186. const char* moduleName = module->GetName().CString();
  187. line = ToString("csb_package_%s_init_%s_thunk ();\n", packageName, moduleName);
  188. source += IndentLine(line);
  189. }
  190. Dedent();
  191. source.Append("\n}\n");
  192. // Thunk Setter
  193. String thunkType = ToString("AtomicNET%sThunk_t", packageName);
  194. source += ToString("extern \"C\" void csb_package_set_%s_thunk (const %s *thunkIn)\n{\n", packageName, thunkType.CString());
  195. Indent();
  196. source += IndentLine(ToString("AtomicNET%sThunk = *thunkIn;\n", packageName));
  197. source += IndentLine(ToString("AtomicNET%sThunkEnabled = true;\n", packageName));
  198. Dedent();
  199. source += ToString("\n}\n");
  200. sourceOut += source;
  201. }
  202. void CSPackageWriter::GenerateNativeSource()
  203. {
  204. GenerateNativeHeader();
  205. String source = "// This file was autogenerated by AtomicTool, changes will be lost\n\n";
  206. const char* packageName = package_->name_.CString();
  207. String packageHeader = "CSPackage" + package_->name_ + ".h";
  208. source += ToString("#include \"%s\"\n", packageHeader.CString());
  209. if (package_->name_ != "Atomic")
  210. source += "using namespace Atomic;\n";
  211. // begin namespace
  212. source += ToString("using namespace %s;\n", packageName);
  213. source += ToString("namespace %s\n{\n", packageName);
  214. // thunk
  215. source += ToString("AtomicNET%sThunk_t AtomicNET%sThunk;\n", packageName, packageName);
  216. source += ToString("bool AtomicNET%sThunkEnabled = false;\n", packageName);
  217. // end of namespace
  218. source += "\n}\n";
  219. GenerateNativeThunkInit(source);
  220. JSBind* jsbind = package_->GetSubsystem<JSBind>();
  221. String filepath = jsbind->GetDestNativeFolder() + "/CSPackage" + package_->name_ + ".cpp";
  222. File file(package_->GetContext());
  223. file.Open(filepath, FILE_WRITE);
  224. file.Write(source.CString(), source.Length());
  225. file.Close();
  226. }
  227. void CSPackageWriter::GenerateManagedSource()
  228. {
  229. }
  230. void CSPackageWriter::GenerateSource()
  231. {
  232. GenerateNativeSource();
  233. GenerateManagedSource();
  234. for (unsigned i = 0; i < package_->modules_.Size(); i++)
  235. {
  236. CSModuleWriter writer(package_->modules_[i]);
  237. writer.GenerateSource();
  238. }
  239. }
  240. }