JSBModule.cpp 11 KB

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