JSBModule.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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/Log.h>
  8. #include <Atomic/IO/File.h>
  9. #include <Atomic/IO/FileSystem.h>
  10. #include <Atomic/Resource/JSONFile.h>
  11. #include "Atomic/Core/ProcessUtils.h"
  12. #include "JSBind.h"
  13. #include "JSBPackage.h"
  14. #include "JSBModule.h"
  15. #include "JSBHeader.h"
  16. #include "JSBClass.h"
  17. #include "JSBEnum.h"
  18. #include "JSBModuleWriter.h"
  19. #include "JSBType.h"
  20. #include "JavaScript/JSModuleWriter.h"
  21. #include "CSharp/CSModuleWriter.h"
  22. namespace ToolCore
  23. {
  24. JSBModule::JSBModule(Context* context, JSBPackage* package) : Object(context),
  25. package_(package),
  26. dotNetModule_(false)
  27. {
  28. }
  29. JSBModule::~JSBModule()
  30. {
  31. }
  32. Vector<SharedPtr<JSBClass>> JSBModule::GetClasses()
  33. {
  34. return classes_.Values();
  35. }
  36. Vector<SharedPtr<JSBEnum>> JSBModule::GetEnums()
  37. {
  38. return enums_.Values();
  39. }
  40. void JSBModule::PreprocessHeaders()
  41. {
  42. for (unsigned i = 0; i < headers_.Size(); i++)
  43. {
  44. headers_[i]->VisitPreprocess();
  45. }
  46. }
  47. void JSBModule::VisitHeaders()
  48. {
  49. for (unsigned i = 0; i < headers_.Size(); i++)
  50. {
  51. headers_[i]->VisitHeader();
  52. }
  53. // validate that all classes found
  54. for (unsigned i = 0; i < classnames_.Size(); i++)
  55. {
  56. JSBClass* cls = GetClass(classnames_[i]);
  57. if (!cls)
  58. {
  59. ErrorExit(ToString("Module class not found %s", classnames_[i].CString()));
  60. }
  61. }
  62. ProcessOverloads();
  63. ProcessExcludes();
  64. ProcessTypeScriptDecl();
  65. ProcessHaxeDecl();
  66. }
  67. void JSBModule::PreprocessClasses()
  68. {
  69. HashMap<StringHash, SharedPtr<JSBClass> >::Iterator itr;
  70. for (itr = classes_.Begin(); itr != classes_.End(); itr++)
  71. {
  72. itr->second_->Preprocess();
  73. }
  74. }
  75. void JSBModule::ProcessClasses()
  76. {
  77. HashMap<StringHash, SharedPtr<JSBClass> >::Iterator itr;
  78. for (itr = classes_.Begin(); itr != classes_.End(); itr++)
  79. {
  80. itr->second_->Process();
  81. }
  82. }
  83. void JSBModule::PostProcessClasses()
  84. {
  85. HashMap<StringHash, SharedPtr<JSBClass> >::Iterator itr;
  86. for (itr = classes_.Begin(); itr != classes_.End(); itr++)
  87. {
  88. itr->second_->PostProcess();
  89. }
  90. }
  91. void JSBModule::ProcessOverloads()
  92. {
  93. // overloads
  94. JSONValue root = moduleJSON_->GetRoot();
  95. JSONValue overloads = root.Get("overloads");
  96. if (overloads.IsObject())
  97. {
  98. Vector<String> childNames = overloads.GetObject().Keys();
  99. for (unsigned j = 0; j < childNames.Size(); j++)
  100. {
  101. String classname = childNames.At(j);
  102. JSBClass* klass = GetClass(classname);
  103. if (!klass)
  104. {
  105. ErrorExit("Bad overload klass");
  106. }
  107. JSONValue classoverloads = overloads.Get(classname);
  108. Vector<String> functionNames = classoverloads.GetObject().Keys();
  109. for (unsigned k = 0; k < functionNames.Size(); k++)
  110. {
  111. JSONValue _sig = classoverloads.Get(functionNames[k]);
  112. if (!_sig.IsArray())
  113. {
  114. ErrorExit("Bad overload defintion");
  115. }
  116. JSONArray sig = _sig.GetArray();
  117. Vector<String> values;
  118. for (unsigned x = 0; x < sig.Size(); x++)
  119. {
  120. values.Push(sig[x].GetString());
  121. }
  122. JSBFunctionSignature* fo = new JSBFunctionSignature(functionNames[k], values);
  123. klass->AddFunctionOverride(fo);
  124. }
  125. }
  126. }
  127. }
  128. void JSBModule::ProcessExcludes()
  129. {
  130. // excludes
  131. JSONValue root = moduleJSON_->GetRoot();
  132. JSONValue excludes = root.Get("excludes");
  133. if (excludes.IsObject())
  134. {
  135. Vector<String> childNames = excludes.GetObject().Keys();
  136. for (unsigned j = 0; j < childNames.Size(); j++)
  137. {
  138. String classname = childNames.At(j);
  139. JSBClass* klass = GetClass(classname);
  140. if (!klass)
  141. {
  142. ErrorExit("Bad exclude klass");
  143. }
  144. JSONValue classexcludes = excludes.Get(classname);
  145. Vector<String> functionNames = classexcludes.GetObject().Keys();
  146. for (unsigned k = 0; k < functionNames.Size(); k++)
  147. {
  148. JSONValue _sig = classexcludes.Get(functionNames[k]);
  149. if (!_sig.IsArray())
  150. {
  151. ErrorExit("Bad exclude defintion");
  152. }
  153. JSONArray sig = _sig.GetArray();
  154. Vector<String> values;
  155. for (unsigned x = 0; x < sig.Size(); x++)
  156. {
  157. values.Push(sig[x].GetString());
  158. }
  159. JSBFunctionSignature* fe = new JSBFunctionSignature(functionNames[k], values);
  160. klass->AddFunctionExclude(fe);
  161. }
  162. }
  163. }
  164. }
  165. void JSBModule::ProcessTypeScriptDecl()
  166. {
  167. // TypeScript declarations
  168. JSONValue root = moduleJSON_->GetRoot();
  169. JSONValue decl = root.Get("typescript_decl");
  170. if (decl.IsObject())
  171. {
  172. Vector<String> childNames = decl.GetObject().Keys();
  173. for (unsigned j = 0; j < childNames.Size(); j++)
  174. {
  175. String classname = childNames.At(j);
  176. JSBClass* klass = GetClass(classname);
  177. if (!klass)
  178. {
  179. ErrorExit("Bad TypeScript decl klass");
  180. }
  181. JSONArray classdecl = decl.Get(classname).GetArray();
  182. for (unsigned k = 0; k < classdecl.Size(); k++)
  183. {
  184. klass->AddTypeScriptDecl(classdecl[k].GetString());
  185. }
  186. }
  187. }
  188. }
  189. void JSBModule::ProcessHaxeDecl()
  190. {
  191. // Haxe declarations
  192. JSONValue root = moduleJSON_->GetRoot();
  193. JSONValue decl = root.Get("haxe_decl");
  194. if (decl.IsObject())
  195. {
  196. Vector<String> childNames = decl.GetObject().Keys();
  197. for (unsigned j = 0; j < childNames.Size(); j++)
  198. {
  199. String classname = childNames.At(j);
  200. JSBClass* klass = GetClass(classname);
  201. if (!klass)
  202. {
  203. ErrorExit("Bad Haxe decl class");
  204. }
  205. JSONArray classdecl = decl.Get(classname).GetArray();
  206. for (unsigned k = 0; k < classdecl.Size(); k++)
  207. {
  208. klass->AddHaxeDecl(classdecl[k].GetString());
  209. }
  210. }
  211. }
  212. }
  213. void JSBModule::ScanHeaders()
  214. {
  215. JSBind* jsbind = GetSubsystem<JSBind>();
  216. FileSystem* fs = GetSubsystem<FileSystem>();
  217. const String& sourceRoot = jsbind->GetSourceRootFolder();
  218. for (unsigned i = 0; i < sourceDirs_.Size(); i++)
  219. {
  220. const String& dir = sourceRoot + sourceDirs_[i] + "/";
  221. Vector<String> fileNames;
  222. fs->ScanDir(fileNames, dir, "*.h", SCAN_FILES, false);
  223. for (unsigned k = 0; k < fileNames.Size(); k++)
  224. {
  225. String filepath = dir + fileNames[k];
  226. SharedPtr<JSBHeader> header(new JSBHeader(context_, this, filepath));
  227. // Parse the C++ header
  228. header->Parse();
  229. headers_.Push(header);
  230. }
  231. }
  232. }
  233. JSBClass* JSBModule::GetClass(const String& name)
  234. {
  235. if (classes_.Contains(name))
  236. return classes_[name];
  237. return 0;
  238. }
  239. void JSBModule::RegisterClass(String name)
  240. {
  241. String nativeName = name;
  242. if (classnames_.Contains(name))
  243. {
  244. if (classRenames_.Contains(name))
  245. {
  246. name = classRenames_[name];
  247. }
  248. if (JSBPackage::GetClassAllPackages(nativeName))
  249. {
  250. ErrorExit(ToString("Class collision: %s", name.CString()));
  251. }
  252. JSBClass* cls = new JSBClass(context_, this, name, nativeName);
  253. classes_[nativeName] = cls;
  254. package_->RegisterClass(cls);
  255. }
  256. }
  257. void JSBModule::RegisterEnum(JSBEnum* jenum)
  258. {
  259. if (JSBPackage::GetClassAllPackages(jenum->GetName()))
  260. {
  261. ErrorExit(ToString("Enum collision: %s", jenum->GetName().CString()));
  262. }
  263. enums_[jenum->GetName()] = jenum;
  264. }
  265. JSBEnum* JSBModule::GetEnum(const String& name)
  266. {
  267. if (enums_.Contains(name))
  268. {
  269. return enums_[name];
  270. }
  271. return 0;
  272. }
  273. bool JSBModule::ContainsConstant(const String& constantName)
  274. {
  275. return constants_.Contains(constantName);
  276. }
  277. void JSBModule::RegisterConstant(const String& constantName, const String& value, unsigned type, bool isUnsigned)
  278. {
  279. // MAX_CASCADE_SPLITS is defined differently for desktop/mobile
  280. if (constantName == "MAX_CASCADE_SPLITS" && JSBPackage::ContainsConstantAllPackages(constantName))
  281. {
  282. return;
  283. }
  284. if (JSBPackage::ContainsConstantAllPackages(constantName))
  285. {
  286. ErrorExit(ToString("Constant collision: %s", constantName.CString()));
  287. }
  288. Constant c;
  289. c.type = new JSBPrimitiveType(type, isUnsigned);
  290. c.value = value;
  291. constants_[constantName] = c;
  292. }
  293. bool JSBModule::Load(const String& jsonFilename)
  294. {
  295. JSBind* jsbind = GetSubsystem<JSBind>();
  296. LOGINFOF("Loading Module: %s", jsonFilename.CString());
  297. SharedPtr<File> jsonFile(new File(context_, jsonFilename));
  298. if (!jsonFile->IsOpen())
  299. {
  300. LOGERRORF("Unable to open module json: %s", jsonFilename.CString());
  301. return false;
  302. }
  303. moduleJSON_ = new JSONFile(context_);
  304. if (!moduleJSON_->BeginLoad(*jsonFile))
  305. {
  306. LOGERRORF("Unable to parse module json: %s", jsonFilename.CString());
  307. return false;
  308. }
  309. JSONValue root = moduleJSON_->GetRoot();
  310. name_ = root.Get("name").GetString();
  311. JSONValue requires = root.Get("requires");
  312. if (requires.IsArray())
  313. {
  314. for (unsigned j = 0; j < requires.GetArray().Size(); j++)
  315. {
  316. requirements_.Push(requires[j].GetString());
  317. }
  318. }
  319. JSONArray classes = root.Get("classes").GetArray();
  320. for (unsigned i = 0; i < classes.Size(); i++)
  321. {
  322. classnames_.Push(classes[i].GetString());
  323. }
  324. JSONValue classes_rename = root.Get("classes_rename");
  325. if (classes_rename.IsObject())
  326. {
  327. Vector<String> childNames = classes_rename.GetObject().Keys();
  328. for (unsigned j = 0; j < childNames.Size(); j++)
  329. {
  330. String classname = childNames.At(j);
  331. String crename = classes_rename.Get(classname).GetString();
  332. classRenames_[classname] = crename;
  333. }
  334. }
  335. JSONValue includes = root.Get("includes");
  336. if (includes.IsArray())
  337. {
  338. for (unsigned j = 0; j < includes.GetArray().Size(); j++)
  339. {
  340. includes_.Push(includes.GetArray()[j].GetString());
  341. }
  342. }
  343. JSONValue sources = root.Get("sources");
  344. for (unsigned i = 0; i < sources.GetArray().Size(); i++)
  345. {
  346. sourceDirs_.Push(sources.GetArray()[i].GetString());
  347. }
  348. if (name_ == "Graphics")
  349. {
  350. #ifdef _MSC_VER
  351. if (jsbind->GetPlatform() == "ANDROID" || jsbind->GetPlatform() == "WEB")
  352. {
  353. sourceDirs_.Push("Source/Atomic/Graphics/OpenGL");
  354. }
  355. else
  356. {
  357. #ifdef ATOMIC_D3D11
  358. sourceDirs_.Push("Source/Atomic/Graphics/Direct3D11");
  359. #else
  360. sourceDirs_.Push("Source/Atomic/Graphics/Direct3D9");
  361. #endif
  362. }
  363. #else
  364. sourceDirs_.Push("Source/Atomic/Graphics/OpenGL");
  365. #endif
  366. }
  367. ScanHeaders();
  368. return true;
  369. }
  370. }