CSModuleWriter.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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 "../JSBind.h"
  25. #include "../JSBPackage.h"
  26. #include "../JSBModule.h"
  27. #include "../JSBEnum.h"
  28. #include "../JSBClass.h"
  29. #include "../JSBFunction.h"
  30. #include "CSTypeHelper.h"
  31. #include "CSClassWriter.h"
  32. #include "CSModuleWriter.h"
  33. namespace ToolCore
  34. {
  35. CSModuleWriter::CSModuleWriter(JSBModule *module) : JSBModuleWriter(module)
  36. {
  37. }
  38. void CSModuleWriter::WriteIncludes(String& source)
  39. {
  40. Vector<String>& includes = module_->includes_;
  41. for (unsigned i = 0; i < includes.Size(); i++)
  42. {
  43. if (includes[i].StartsWith("<"))
  44. source.AppendWithFormat("#include %s\n", includes[i].CString());
  45. else
  46. source.AppendWithFormat("#include \"%s\"\n", includes[i].CString());
  47. }
  48. Vector<JSBHeader*> allheaders;
  49. HashMap<StringHash, SharedPtr<JSBEnum> >::Iterator eitr = module_->enums_.Begin();
  50. while (eitr != module_->enums_.End())
  51. {
  52. allheaders.Push(eitr->second_->GetHeader());
  53. eitr++;
  54. }
  55. HashMap<StringHash, SharedPtr<JSBClass> >::Iterator citr = module_->classes_.Begin();
  56. while (citr != module_->classes_.End())
  57. {
  58. allheaders.Push(citr->second_->GetHeader());
  59. citr++;
  60. }
  61. Vector<JSBHeader*> included;
  62. for (unsigned i = 0; i < allheaders.Size(); i++)
  63. {
  64. JSBHeader* header = allheaders.At(i);
  65. if (included.Contains(header))
  66. continue;
  67. String headerPath = GetPath(header->GetFilePath());
  68. String headerfile = GetFileNameAndExtension(header->GetFilePath());
  69. JSBind* jsbind = header->GetSubsystem<JSBind>();
  70. headerPath.Replace(jsbind->GetSourceRootFolder() + "Source/", "");
  71. source.AppendWithFormat("#include <%s%s>\n", headerPath.CString(), headerfile.CString());
  72. included.Push(header);
  73. }
  74. source += "\n#include <Atomic/Script/ScriptVector.h>\n";
  75. source += ToString("\n#include \"CSPackage%s.h\"\n", module_->GetPackage()->GetName().CString());
  76. }
  77. void CSModuleWriter::GenerateNativeSource()
  78. {
  79. String source = "// This file was autogenerated by JSBind, changes will be lost\n";
  80. String moduleGuard = module_->GetModuleDefineGuard();
  81. if (moduleGuard.Length())
  82. {
  83. source += ToString("\n%s\n", moduleGuard.CString());
  84. }
  85. source += "#ifdef ATOMIC_PLATFORM_WINDOWS\n";
  86. source += "#pragma warning(disable: 4244) // possible loss of data\n";
  87. source += "#define ATOMIC_EXPORT_API __declspec(dllexport)\n";
  88. source += "#else\n";
  89. source += "#define ATOMIC_EXPORT_API\n";
  90. source += "#endif\n";
  91. if (module_->Requires("3D"))
  92. {
  93. source += "#ifdef ATOMIC_3D\n";
  94. }
  95. WriteIncludes(source);
  96. source += "\n#include <AtomicNET/NETNative/NETCore.h>\n";
  97. String ns = module_->GetPackage()->GetNamespace();
  98. source += "\n\nusing namespace " + ns + ";\n\n";
  99. source += "\n\nextern \"C\" \n{\n \n";
  100. source += "// Begin Class Declarations\n";
  101. source += "// End Class Declarations\n\n";
  102. source += "// Begin Classes\n";
  103. Vector<SharedPtr<JSBClass>> classes = module_->classes_.Values();
  104. for (unsigned i = 0; i < classes.Size(); i++)
  105. {
  106. String classGuard = module_->GetClassDefineGuard(classes[i]->GetNativeName());
  107. if (classGuard.Length())
  108. {
  109. source += ToString("\n%s\n\n", classGuard.CString());
  110. }
  111. CSClassWriter clsWriter(classes[i]);
  112. clsWriter.GenerateNativeSource(source);
  113. if (classGuard.Length())
  114. {
  115. source += "#endif\n\n";
  116. }
  117. }
  118. source += "// End Classes\n\n";
  119. // end Atomic namespace
  120. source += "\n}\n";
  121. if (module_->Requires("3D"))
  122. {
  123. source += "#endif //ATOMIC_3D\n";
  124. }
  125. if (moduleGuard.Length())
  126. {
  127. source += "\n#endif\n";
  128. }
  129. JSBind* jsbind = module_->GetSubsystem<JSBind>();
  130. String filepath = jsbind->GetDestNativeFolder() + "/CSModule" + module_->name_ + ".cpp";
  131. File file(module_->GetContext());
  132. file.Open(filepath, FILE_WRITE);
  133. file.Write(source.CString(), source.Length());
  134. file.Close();
  135. }
  136. String CSModuleWriter::GetManagedPrimitiveType(JSBPrimitiveType* ptype)
  137. {
  138. if (ptype->kind_ == JSBPrimitiveType::Bool)
  139. return "bool";
  140. if (ptype->kind_ == JSBPrimitiveType::Int && ptype->isUnsigned_)
  141. return "uint";
  142. else if (ptype->kind_ == JSBPrimitiveType::Int)
  143. return "int";
  144. if (ptype->kind_ == JSBPrimitiveType::Float)
  145. return "float";
  146. return "int";
  147. }
  148. void CSModuleWriter::GenerateManagedClasses(String& source)
  149. {
  150. Vector<SharedPtr<JSBClass>> classes = module_->classes_.Values();
  151. for (unsigned i = 0; i < classes.Size(); i++)
  152. {
  153. JSBClass* klass = classes.At(i);
  154. if (klass->IsNumberArray())
  155. continue;
  156. CSClassWriter clsWriter(klass);
  157. clsWriter.GenerateManagedSource(source);
  158. }
  159. }
  160. void CSModuleWriter::GenerateManagedEnumsAndConstants(String& source)
  161. {
  162. Vector<SharedPtr<JSBEnum>> enums = module_->enums_.Values();
  163. Indent();
  164. for (unsigned i = 0; i < enums.Size(); i++)
  165. {
  166. JSBEnum* jenum = enums[i];
  167. source += "\n";
  168. String line = "public enum " + jenum->GetName() + "\n";
  169. source += IndentLine(line);
  170. source += IndentLine("{\n");
  171. HashMap<String, String>& values = jenum->GetValues();
  172. HashMap<String, String>::ConstIterator itr = values.Begin();
  173. Indent();
  174. while (itr != values.End())
  175. {
  176. String name = (*itr).first_;
  177. String value = (*itr).second_;
  178. // BodyType2D enum order is assigned in RigidBody2D.h in incorrect order
  179. if (jenum->GetName() == "BodyType2D")
  180. {
  181. if (name == "BT_KINEMATIC")
  182. value = "1";
  183. else if (name == "BT_DYNAMIC")
  184. value = "2";
  185. }
  186. if (value.Length())
  187. {
  188. line = name + " = " + value;
  189. }
  190. else
  191. {
  192. line = name;
  193. }
  194. itr++;
  195. if (itr != values.End())
  196. line += ",";
  197. line += "\n";
  198. source += IndentLine(line);
  199. }
  200. Dedent();
  201. source += IndentLine("}\n");
  202. }
  203. // constants
  204. HashMap<String, JSBModule::Constant>& constants = module_->GetConstants();
  205. if (constants.Size())
  206. {
  207. source += "\n";
  208. String line = "public static partial class Constants\n";
  209. source += IndentLine(line);
  210. source += IndentLine("{\n");
  211. const Vector<String>& constantsName = constants.Keys();
  212. Indent();
  213. for (unsigned i = 0; i < constantsName.Size(); i++)
  214. {
  215. const String& cname = constantsName.At(i);
  216. JSBModule::Constant& constant = constants[cname];
  217. String managedType = GetManagedPrimitiveType(constant.type);
  218. String value = constant.value;
  219. if (!value.Length())
  220. continue;
  221. //static const unsigned M_MIN_UNSIGNED = 0x00000000;
  222. // /static const unsigned M_MAX_UNSIGNED = 0xffffffff;
  223. if (cname == "M_MIN_INT")
  224. value = "int.MinValue";
  225. if (cname == "M_INFINITY")
  226. value = "float.MaxValue";
  227. if (value == "M_MAX_UNSIGNED")
  228. value = "0xffffffff";
  229. // Input stuff
  230. if (module_->GetName() == "Input")
  231. {
  232. if (cname.StartsWith("KEY_"))
  233. {
  234. if (value.Length() == 1 && (IsAlpha(value[0]) || IsDigit(value[0])))
  235. value = "'" + value + "'";
  236. }
  237. // https://raw.githubusercontent.com/flibitijibibo/SDL2-CS/master/src/SDL2.cs
  238. if (value.StartsWith("SDL_BUTTON_") || value.StartsWith("SDL_HAT_"))
  239. {
  240. value = "(int) SDL." + value;
  241. }
  242. else if (value.StartsWith("SDLK_"))
  243. {
  244. value = "(int) SDL.SDL_Keycode." + value;
  245. }
  246. else if (value.StartsWith("SDL_SCANCODE_"))
  247. {
  248. value = "(int) SDL.SDL_Scancode." + value;
  249. }
  250. else if (value.StartsWith("SDL_CONTROLLER_BUTTON_"))
  251. {
  252. value = "(int) SDL.SDL_GameControllerButton." + value;
  253. }
  254. else if (value.StartsWith("SDL_CONTROLLER_AXIS_"))
  255. {
  256. value = "(int) SDL.SDL_GameControllerAxis." + value;
  257. }
  258. }
  259. String line = "public const " + managedType + " " + cname + " = " + value;
  260. if (managedType == "float" && !line.EndsWith("f") && IsDigit(line[line.Length()-1]))
  261. line += "f";
  262. line += ";\n";
  263. source += IndentLine(line);
  264. }
  265. Dedent();
  266. source += "\n";
  267. line = "}\n";
  268. source += IndentLine(line);
  269. }
  270. source += "\n";
  271. Dedent();
  272. }
  273. void CSModuleWriter::GenerateManagedModuleClass(String& sourceOut)
  274. {
  275. Indent();
  276. String source;
  277. String line = ToString("public static partial class %sModule\n", module_->GetName().CString());
  278. source += IndentLine(line);
  279. source += IndentLine("{\n");
  280. Indent();
  281. source += IndentLine("public static void Initialize()\n");
  282. source += IndentLine("{\n");
  283. Indent();
  284. Vector<SharedPtr<JSBClass>> classes = module_->classes_.Values();
  285. for (unsigned i = 0; i < classes.Size(); i++)
  286. {
  287. JSBClass* klass = classes.At(i);
  288. JSBPackage* package = module_->GetPackage();
  289. if (klass->IsNumberArray() || klass->IsAbstract())
  290. continue;
  291. const char* className = klass->GetName().CString();
  292. String classGuard = module_->GetClassDefineGuard(classes[i]->GetNativeName(), "csharp");
  293. if (classGuard.Length())
  294. {
  295. source += ToString("\n%s\n", classGuard.CString());
  296. }
  297. line = ToString("new NativeType(%s.csb_%s_%s_GetClassIDStatic (), ", className, package->GetName().CString(), className);
  298. line += ToString("typeof(%s), (IntPtr x) => { return new %s (x); } );\n",className, className);
  299. source += IndentLine(line);
  300. if (classGuard.Length())
  301. {
  302. source += ToString("#endif\n", classGuard.CString());
  303. }
  304. }
  305. Dedent();
  306. source += IndentLine("}\n");
  307. Dedent();
  308. source += IndentLine("}\n");
  309. sourceOut += source;
  310. Dedent();
  311. }
  312. void CSModuleWriter::GenerateManagedSource()
  313. {
  314. String source = "// Autogenerated";
  315. String moduleName = module_->GetPackage()->GetNamespace();
  316. source += "\nusing System;\nusing System.Collections.Generic;\nusing System.Runtime.InteropServices;\nusing static System.Reflection.IntrospectionExtensions;\n";
  317. if (moduleName == "Atomic")
  318. moduleName = "AtomicEngine";
  319. if (moduleName != "AtomicEngine")
  320. {
  321. source += "using AtomicEngine;\n";
  322. }
  323. source += "\n\n";
  324. source += "namespace " + moduleName + "\n";
  325. source += "{\n";
  326. GenerateManagedEnumsAndConstants(source);
  327. GenerateManagedModuleClass(source);
  328. GenerateManagedClasses(source);
  329. source += "}\n";
  330. JSBind* jsbind = module_->GetSubsystem<JSBind>();
  331. String filepath = jsbind->GetDestScriptFolder() + "/CSModule" + module_->name_ + ".cs";
  332. File file(module_->GetContext());
  333. file.Open(filepath, FILE_WRITE);
  334. file.Write(source.CString(), source.Length());
  335. file.Close();
  336. }
  337. void CSModuleWriter::GenerateSource()
  338. {
  339. GenerateNativeSource();
  340. GenerateManagedSource();
  341. }
  342. }