CSModuleWriter.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 "../JSBind.h"
  10. #include "../JSBPackage.h"
  11. #include "../JSBModule.h"
  12. #include "../JSBEnum.h"
  13. #include "../JSBClass.h"
  14. #include "../JSBFunction.h"
  15. #include "CSClassWriter.h"
  16. #include "CSModuleWriter.h"
  17. namespace ToolCore
  18. {
  19. CSModuleWriter::CSModuleWriter(JSBModule *module) : JSBModuleWriter(module)
  20. {
  21. }
  22. void CSModuleWriter::WriteIncludes(String& source)
  23. {
  24. Vector<String>& includes = module_->includes_;
  25. for (unsigned i = 0; i < includes.Size(); i++)
  26. {
  27. if (includes[i].StartsWith("<"))
  28. source.AppendWithFormat("#include %s\n", includes[i].CString());
  29. else
  30. source.AppendWithFormat("#include \"%s\"\n", includes[i].CString());
  31. }
  32. Vector<JSBHeader*> allheaders;
  33. HashMap<StringHash, SharedPtr<JSBEnum> >::Iterator eitr = module_->enums_.Begin();
  34. while (eitr != module_->enums_.End())
  35. {
  36. allheaders.Push(eitr->second_->GetHeader());
  37. eitr++;
  38. }
  39. HashMap<StringHash, SharedPtr<JSBClass> >::Iterator citr = module_->classes_.Begin();
  40. while (citr != module_->classes_.End())
  41. {
  42. allheaders.Push(citr->second_->GetHeader());
  43. citr++;
  44. }
  45. Vector<JSBHeader*> included;
  46. for (unsigned i = 0; i < allheaders.Size(); i++)
  47. {
  48. JSBHeader* header = allheaders.At(i);
  49. if (included.Contains(header))
  50. continue;
  51. String headerPath = GetPath(header->GetFilePath());
  52. String headerfile = GetFileNameAndExtension(header->GetFilePath());
  53. JSBind* jsbind = header->GetSubsystem<JSBind>();
  54. headerPath.Replace(jsbind->GetSourceRootFolder() + "Source/", "");
  55. source.AppendWithFormat("#include <%s%s>\n", headerPath.CString(), headerfile.CString());
  56. included.Push(header);
  57. }
  58. }
  59. void CSModuleWriter::GenerateNativeSource()
  60. {
  61. String source = "// This file was autogenerated by JSBind, changes will be lost\n";
  62. source += "#ifdef ATOMIC_PLATFORM_WINDOWS\n";
  63. source += "#pragma warning(disable: 4244) // possible loss of data\n";
  64. source += "#define ATOMIC_EXPORT_API __declspec(dllexport)\n";
  65. source += "#else\n";
  66. source += "#define ATOMIC_EXPORT_API\n";
  67. source += "#endif\n";
  68. if (module_->Requires("3D"))
  69. {
  70. source += "#ifdef ATOMIC_3D\n";
  71. }
  72. WriteIncludes(source);
  73. source += "\n#include <AtomicSharp/AtomicSharp.h>\n";
  74. String ns = module_->GetPackage()->GetNamespace();
  75. source += "\n\nusing namespace " + ns + ";\n\n";
  76. source += "\n\nextern \"C\" \n{\n \n";
  77. source += "// Begin Class Declarations\n";
  78. source += "// End Class Declarations\n\n";
  79. source += "// Begin Classes\n";
  80. Vector<SharedPtr<JSBClass>> classes = module_->classes_.Values();
  81. for (unsigned i = 0; i < classes.Size(); i++)
  82. {
  83. CSClassWriter clsWriter(classes[i]);
  84. clsWriter.GenerateNativeSource(source);
  85. }
  86. source += "// End Classes\n\n";
  87. // end Atomic namespace
  88. source += "\n}\n";
  89. if (module_->Requires("3D"))
  90. {
  91. source += "#endif //ATOMIC_3D\n";
  92. }
  93. JSBind* jsbind = module_->GetSubsystem<JSBind>();
  94. String filepath = jsbind->GetDestNativeFolder() + "/CSModule" + module_->name_ + ".cpp";
  95. File file(module_->GetContext());
  96. file.Open(filepath, FILE_WRITE);
  97. file.Write(source.CString(), source.Length());
  98. file.Close();
  99. }
  100. String CSModuleWriter::GetManagedPrimitiveType(JSBPrimitiveType* ptype)
  101. {
  102. if (ptype->kind_ == JSBPrimitiveType::Bool)
  103. return "bool";
  104. if (ptype->kind_ == JSBPrimitiveType::Int && ptype->isUnsigned_)
  105. return "uint";
  106. else if (ptype->kind_ == JSBPrimitiveType::Int)
  107. return "int";
  108. if (ptype->kind_ == JSBPrimitiveType::Float)
  109. return "float";
  110. return "int";
  111. }
  112. void CSModuleWriter::GenerateManagedClasses(String& source)
  113. {
  114. Vector<SharedPtr<JSBClass>> classes = module_->classes_.Values();
  115. for (unsigned i = 0; i < classes.Size(); i++)
  116. {
  117. JSBClass* klass = classes.At(i);
  118. if (klass->IsNumberArray())
  119. continue;
  120. CSClassWriter clsWriter(klass);
  121. clsWriter.GenerateManagedSource(source);
  122. }
  123. }
  124. void CSModuleWriter::GenerateManagedEnumsAndConstants(String& source)
  125. {
  126. Vector<SharedPtr<JSBEnum>> enums = module_->enums_.Values();
  127. Indent();
  128. for (unsigned i = 0; i < enums.Size(); i++)
  129. {
  130. JSBEnum* jenum = enums[i];
  131. source += "\n";
  132. String line = "public enum " + jenum->GetName() + "\n";
  133. source += IndentLine(line);
  134. source += IndentLine("{\n");
  135. HashMap<String, String>& values = jenum->GetValues();
  136. HashMap<String, String>::ConstIterator itr = values.Begin();
  137. Indent();
  138. while (itr != values.End())
  139. {
  140. String name = (*itr).first_;
  141. String value = (*itr).second_;
  142. if (value.Length())
  143. {
  144. line = name + " = " + value;
  145. }
  146. else
  147. {
  148. line = name;
  149. }
  150. itr++;
  151. if (itr != values.End())
  152. line += ",";
  153. line += "\n";
  154. source += IndentLine(line);
  155. }
  156. Dedent();
  157. source += IndentLine("}\n");
  158. }
  159. // constants
  160. HashMap<String, JSBModule::Constant>& constants = module_->GetConstants();
  161. if (constants.Size())
  162. {
  163. source += "\n";
  164. String line = "public static partial class Constants\n";
  165. source += IndentLine(line);
  166. source += IndentLine("{\n");
  167. const Vector<String>& constantsName = constants.Keys();
  168. Indent();
  169. for (unsigned i = 0; i < constantsName.Size(); i++)
  170. {
  171. const String& cname = constantsName.At(i);
  172. JSBModule::Constant& constant = constants[cname];
  173. String managedType = GetManagedPrimitiveType(constant.type);
  174. String value = constant.value;
  175. //static const unsigned M_MIN_UNSIGNED = 0x00000000;
  176. // /static const unsigned M_MAX_UNSIGNED = 0xffffffff;
  177. if (value == "M_MAX_UNSIGNED")
  178. value = "0xffffffff";
  179. String line = "public const " + managedType + " " + cname + " = " + value;
  180. if (managedType == "float" && !line.EndsWith("f"))
  181. line += "f";
  182. line += ";\n";
  183. source += IndentLine(line);
  184. }
  185. Dedent();
  186. source += "\n";
  187. line = "}\n";
  188. source += IndentLine(line);
  189. }
  190. source += "\n";
  191. Dedent();
  192. }
  193. void CSModuleWriter::GenerateManagedModuleClass(String& sourceOut)
  194. {
  195. Indent();
  196. String source;
  197. String line = ToString("public static partial class %sModule\n", module_->GetName().CString());
  198. source += IndentLine(line);
  199. source += IndentLine("{\n");
  200. Indent();
  201. source += IndentLine("public static void Initialize()\n");
  202. source += IndentLine("{\n");
  203. Indent();
  204. Vector<SharedPtr<JSBClass>> classes = module_->classes_.Values();
  205. for (unsigned i = 0; i < classes.Size(); i++)
  206. {
  207. JSBClass* klass = classes.At(i);
  208. JSBPackage* package = module_->GetPackage();
  209. if (klass->IsNumberArray() || klass->IsAbstract())
  210. continue;
  211. line = ToString("NativeCore.nativeClassIDToManagedConstructor [ %s.csb_%s_%s_GetClassIDStatic ()] = (IntPtr x) => {\n",
  212. klass->GetName().CString(), package->GetName().CString(), klass->GetName().CString());
  213. source += IndentLine(line);
  214. Indent();
  215. source += IndentLine(ToString("return new %s (x);\n", klass->GetName().CString()));
  216. Dedent();
  217. source += IndentLine("};\n");
  218. }
  219. Dedent();
  220. source += IndentLine("}\n");
  221. Dedent();
  222. source += IndentLine("}\n");
  223. sourceOut += source;
  224. Dedent();
  225. }
  226. void CSModuleWriter::GenerateManagedSource()
  227. {
  228. String source = "// Autogenerated";
  229. String moduleName = module_->GetPackage()->GetName();
  230. source += "\nusing System;\nusing System.Collections.Generic;\nusing System.Runtime.InteropServices;\n";
  231. if (moduleName == "Atomic")
  232. moduleName = "AtomicEngine";
  233. if (moduleName != "AtomicEngine")
  234. {
  235. source += "using AtomicEngine;\n";
  236. }
  237. source += "\n\n";
  238. source += "namespace " + moduleName + "\n";
  239. source += "{\n";
  240. GenerateManagedEnumsAndConstants(source);
  241. GenerateManagedModuleClass(source);
  242. GenerateManagedClasses(source);
  243. source += "}\n";
  244. JSBind* jsbind = module_->GetSubsystem<JSBind>();
  245. String filepath = jsbind->GetDestScriptFolder() + "/CSModule" + module_->name_ + ".cs";
  246. File file(module_->GetContext());
  247. file.Open(filepath, FILE_WRITE);
  248. file.Write(source.CString(), source.Length());
  249. file.Close();
  250. }
  251. void CSModuleWriter::GenerateSource()
  252. {
  253. GenerateNativeSource();
  254. GenerateManagedSource();
  255. }
  256. }