CSModuleWriter.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 += "#endif\n";
  65. if (module_->Requires("3D"))
  66. {
  67. source += "#ifdef ATOMIC_3D\n";
  68. }
  69. WriteIncludes(source);
  70. source += "\n#include <AtomicSharp/AtomicSharp.h>\n";
  71. String ns = module_->GetPackage()->GetNamespace();
  72. source += "\n\nusing namespace " + ns + ";\n\n";
  73. source += "\n\nextern \"C\" \n{\n \n";
  74. source += "// Begin Class Declarations\n";
  75. source += "// End Class Declarations\n\n";
  76. source += "// Begin Classes\n";
  77. Vector<SharedPtr<JSBClass>> classes = module_->classes_.Values();
  78. for (unsigned i = 0; i < classes.Size(); i++)
  79. {
  80. CSClassWriter clsWriter(classes[i]);
  81. clsWriter.GenerateNativeSource(source);
  82. }
  83. source += "// End Classes\n\n";
  84. // end Atomic namespace
  85. source += "\n}\n";
  86. if (module_->Requires("3D"))
  87. {
  88. source += "#endif //ATOMIC_3D\n";
  89. }
  90. JSBind* jsbind = module_->GetSubsystem<JSBind>();
  91. String filepath = jsbind->GetDestNativeFolder() + "/CSModule" + module_->name_ + ".cpp";
  92. File file(module_->GetContext());
  93. file.Open(filepath, FILE_WRITE);
  94. file.Write(source.CString(), source.Length());
  95. file.Close();
  96. }
  97. String CSModuleWriter::GetManagedPrimitiveType(JSBPrimitiveType* ptype)
  98. {
  99. if (ptype->kind_ == JSBPrimitiveType::Bool)
  100. return "bool";
  101. if (ptype->kind_ == JSBPrimitiveType::Int && ptype->isUnsigned_)
  102. return "uint";
  103. else if (ptype->kind_ == JSBPrimitiveType::Int)
  104. return "int";
  105. if (ptype->kind_ == JSBPrimitiveType::Float)
  106. return "float";
  107. return "int";
  108. }
  109. void CSModuleWriter::GenerateManagedClasses(String& source)
  110. {
  111. Vector<SharedPtr<JSBClass>> classes = module_->classes_.Values();
  112. for (unsigned i = 0; i < classes.Size(); i++)
  113. {
  114. JSBClass* klass = classes.At(i);
  115. if (klass->IsNumberArray())
  116. continue;
  117. CSClassWriter clsWriter(klass);
  118. clsWriter.GenerateManagedSource(source);
  119. }
  120. }
  121. void CSModuleWriter::GenerateManagedEnumsAndConstants(String& source)
  122. {
  123. Vector<SharedPtr<JSBEnum>> enums = module_->enums_.Values();
  124. Indent();
  125. for (unsigned i = 0; i < enums.Size(); i++)
  126. {
  127. JSBEnum* jenum = enums[i];
  128. source += "\n";
  129. String line = "public enum " + jenum->GetName() + "\n";
  130. source += IndentLine(line);
  131. source += IndentLine("{\n");
  132. HashMap<String, String>& values = jenum->GetValues();
  133. HashMap<String, String>::ConstIterator itr = values.Begin();
  134. Indent();
  135. while (itr != values.End())
  136. {
  137. String name = (*itr).first_;
  138. String value = (*itr).second_;
  139. if (value.Length())
  140. {
  141. line = name + " = " + value;
  142. }
  143. else
  144. {
  145. line = name;
  146. }
  147. itr++;
  148. if (itr != values.End())
  149. line += ",";
  150. line += "\n";
  151. source += IndentLine(line);
  152. }
  153. Dedent();
  154. source += IndentLine("}\n");
  155. }
  156. // constants
  157. HashMap<String, JSBModule::Constant>& constants = module_->GetConstants();
  158. if (constants.Size())
  159. {
  160. source += "\n";
  161. String line = "public static partial class Constants\n";
  162. source += IndentLine(line);
  163. source += IndentLine("{\n");
  164. const Vector<String>& constantsName = constants.Keys();
  165. Indent();
  166. for (unsigned i = 0; i < constantsName.Size(); i++)
  167. {
  168. const String& cname = constantsName.At(i);
  169. JSBModule::Constant& constant = constants[cname];
  170. String managedType = GetManagedPrimitiveType(constant.type);
  171. String value = constant.value;
  172. //static const unsigned M_MIN_UNSIGNED = 0x00000000;
  173. // /static const unsigned M_MAX_UNSIGNED = 0xffffffff;
  174. if (value == "M_MAX_UNSIGNED")
  175. value = "0xffffffff";
  176. String line = "public const " + managedType + " " + cname + " = " + value;
  177. if (managedType == "float" && !line.EndsWith("f"))
  178. line += "f";
  179. line += ";\n";
  180. source += IndentLine(line);
  181. }
  182. Dedent();
  183. source += "\n";
  184. line = "}\n";
  185. source += IndentLine(line);
  186. }
  187. source += "\n";
  188. Dedent();
  189. }
  190. void CSModuleWriter::GenerateManagedModuleClass(String& sourceOut)
  191. {
  192. Indent();
  193. String source;
  194. String line = ToString("public static partial class %sModule\n", module_->GetName().CString());
  195. source += IndentLine(line);
  196. source += IndentLine("{\n");
  197. Indent();
  198. source += IndentLine("public static void Initialize()\n");
  199. source += IndentLine("{\n");
  200. Indent();
  201. Vector<SharedPtr<JSBClass>> classes = module_->classes_.Values();
  202. for (unsigned i = 0; i < classes.Size(); i++)
  203. {
  204. JSBClass* klass = classes.At(i);
  205. JSBPackage* package = module_->GetPackage();
  206. if (klass->IsNumberArray() || klass->IsAbstract())
  207. continue;
  208. line = ToString("NativeCore.nativeClassIDToManagedConstructor [ %s.csb_%s_%s_GetClassIDStatic ()] = (IntPtr x) => {\n",
  209. klass->GetName().CString(), package->GetName().CString(), klass->GetName().CString());
  210. source += IndentLine(line);
  211. Indent();
  212. source += IndentLine(ToString("return new %s (x);\n", klass->GetName().CString()));
  213. Dedent();
  214. source += IndentLine("};\n");
  215. }
  216. Dedent();
  217. source += IndentLine("}\n");
  218. Dedent();
  219. source += IndentLine("}\n");
  220. sourceOut += source;
  221. Dedent();
  222. }
  223. void CSModuleWriter::GenerateManagedSource()
  224. {
  225. String source = "// Autogenerated";
  226. String moduleName = module_->GetPackage()->GetName();
  227. source += "\nusing System;\nusing System.Collections.Generic;\nusing System.Runtime.InteropServices;\n";
  228. if (moduleName == "Atomic")
  229. moduleName = "AtomicEngine";
  230. if (moduleName != "AtomicEngine")
  231. {
  232. source += "using AtomicEngine;\n";
  233. }
  234. source += "\n\n";
  235. source += "namespace " + moduleName + "\n";
  236. source += "{\n";
  237. GenerateManagedEnumsAndConstants(source);
  238. GenerateManagedModuleClass(source);
  239. GenerateManagedClasses(source);
  240. source += "}\n";
  241. JSBind* jsbind = module_->GetSubsystem<JSBind>();
  242. String filepath = jsbind->GetDestScriptFolder() + "/CSModule" + module_->name_ + ".cs";
  243. File file(module_->GetContext());
  244. file.Open(filepath, FILE_WRITE);
  245. file.Write(source.CString(), source.Length());
  246. file.Close();
  247. }
  248. void CSModuleWriter::GenerateSource()
  249. {
  250. GenerateNativeSource();
  251. GenerateManagedSource();
  252. }
  253. }