JSBPackage.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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/Log.h>
  23. #include <Atomic/IO/File.h>
  24. #include <Atomic/IO/FileSystem.h>
  25. #include <Atomic/Resource/JSONFile.h>
  26. #include "JSBind.h"
  27. #include "JSBEvent.h"
  28. #include "JSBClass.h"
  29. #include "JSBModule.h"
  30. #include "JSBPackage.h"
  31. #include "JSBPackageWriter.h"
  32. namespace ToolCore
  33. {
  34. Vector<SharedPtr<JSBPackage> > JSBPackage::allPackages_;
  35. JSBPackage::JSBPackage(Context* context) : Object(context)
  36. {
  37. // by default we bind for both JavaScript and C#
  38. bindingTypes_.Push(JAVASCRIPT);
  39. bindingTypes_.Push(CSHARP);
  40. }
  41. JSBPackage::~JSBPackage()
  42. {
  43. }
  44. void JSBPackage::PreprocessModules()
  45. {
  46. for (unsigned i = 0; i < modules_.Size(); i++)
  47. {
  48. modules_[i]->PreprocessHeaders();
  49. }
  50. }
  51. void JSBPackage::ProcessModules()
  52. {
  53. for (unsigned i = 0; i < modules_.Size(); i++)
  54. {
  55. modules_[i]->VisitHeaders();
  56. }
  57. for (unsigned i = 0; i < modules_.Size(); i++)
  58. {
  59. modules_[i]->PreprocessClasses();
  60. }
  61. for (unsigned i = 0; i < modules_.Size(); i++)
  62. {
  63. modules_[i]->ProcessClasses();
  64. }
  65. for (unsigned i = 0; i < modules_.Size(); i++)
  66. {
  67. modules_[i]->PostProcessClasses();
  68. }
  69. for (unsigned i = 0; i < modules_.Size(); i++)
  70. {
  71. JSBEvent::ScanModuleEvents(modules_[i]);
  72. }
  73. }
  74. void JSBPackage::GenerateSource(JSBPackageWriter& packageWriter)
  75. {
  76. packageWriter.GenerateSource();
  77. packageWriter.PostProcess();
  78. }
  79. JSBClass* JSBPackage::GetClass(const String& name, bool includeInterfaces)
  80. {
  81. for (unsigned i = 0; i < modules_.Size(); i++)
  82. {
  83. JSBClass* cls = modules_[i]->GetClass(name, includeInterfaces);
  84. if (cls)
  85. return cls;
  86. }
  87. return 0;
  88. }
  89. PODVector<JSBClass*> JSBPackage::GetAllClasses(bool includeInterfaces)
  90. {
  91. PODVector<JSBClass*> retVector;
  92. for (unsigned i = 0; i < allClasses_.Size(); i++)
  93. {
  94. if (!includeInterfaces && allClasses_[i]->IsInterface())
  95. continue;
  96. retVector.Push(allClasses_[i]);
  97. }
  98. return retVector;
  99. }
  100. JSBClass* JSBPackage::GetClassAllPackages(const String& name, bool includeInterfaces)
  101. {
  102. for (unsigned i = 0; i < allPackages_.Size(); i++)
  103. {
  104. JSBClass* cls = allPackages_[i]->GetClass(name, includeInterfaces);
  105. if (cls)
  106. return cls;
  107. }
  108. return 0;
  109. }
  110. JSBEvent* JSBPackage::GetEvent(const String& eventID, const String& eventName)
  111. {
  112. for (unsigned i = 0; i < modules_.Size(); i++)
  113. {
  114. JSBEvent* event = modules_[i]->GetEvent(eventID, eventName);
  115. if (event)
  116. return event;
  117. }
  118. return 0;
  119. }
  120. JSBEvent* JSBPackage::GetEventAllPackages(const String& eventID, const String& eventName)
  121. {
  122. for (unsigned i = 0; i < allPackages_.Size(); i++)
  123. {
  124. JSBEvent* event = allPackages_[i]->GetEvent(eventID, eventName);
  125. if (event)
  126. return event;
  127. }
  128. return 0;
  129. }
  130. JSBEnum* JSBPackage::GetEnum(const String& name)
  131. {
  132. for (unsigned i = 0; i < modules_.Size(); i++)
  133. {
  134. JSBEnum* cls = modules_[i]->GetEnum(name);
  135. if (cls)
  136. return cls;
  137. }
  138. return 0;
  139. }
  140. JSBEnum* JSBPackage::GetEnumAllPackages(const String& name)
  141. {
  142. for (unsigned i = 0; i < allPackages_.Size(); i++)
  143. {
  144. JSBEnum* cls = allPackages_[i]->GetEnum(name);
  145. if (cls)
  146. return cls;
  147. }
  148. return 0;
  149. }
  150. bool JSBPackage::ContainsConstant(const String& constantName)
  151. {
  152. for (unsigned i = 0; i < modules_.Size(); i++)
  153. if (modules_[i]->ContainsConstant(constantName))
  154. return true;
  155. return false;
  156. }
  157. bool JSBPackage::ContainsConstantAllPackages(const String& constantName)
  158. {
  159. for (unsigned i = 0; i < allPackages_.Size(); i++)
  160. {
  161. if (allPackages_[i]->ContainsConstant(constantName))
  162. return true;
  163. }
  164. return false;
  165. }
  166. bool JSBPackage::Load(const String& packageFolder)
  167. {
  168. ATOMIC_LOGINFOF("Loading Package: %s", packageFolder.CString());
  169. JSBind* jsbind = GetSubsystem<JSBind>();
  170. SharedPtr<File> jsonFile(new File(context_, packageFolder + "Package.json"));
  171. if (!jsonFile->IsOpen())
  172. {
  173. ATOMIC_LOGERRORF("Unable to open package json: %s", (packageFolder + "Package.json").CString());
  174. return false;
  175. }
  176. SharedPtr<JSONFile> packageJSON(new JSONFile(context_));
  177. if (!packageJSON->BeginLoad(*jsonFile))
  178. {
  179. ATOMIC_LOGERRORF("Unable to parse package json: %s", (packageFolder + "Package.json").CString());
  180. return false;
  181. }
  182. JSONValue& root = packageJSON->GetRoot();
  183. // first load dependencies
  184. JSONValue deps = root.Get("dependencies");
  185. if (deps.IsArray())
  186. {
  187. for (unsigned i = 0; i < deps.GetArray().Size(); i++)
  188. {
  189. String dpackageFolder = AddTrailingSlash(deps.GetArray()[i].GetString());
  190. SharedPtr<JSBPackage> depPackage (new JSBPackage(context_));
  191. if (!depPackage->Load(jsbind->GetSourceRootFolder() + dpackageFolder))
  192. {
  193. ATOMIC_LOGERRORF("Unable to load package dependency: %s", dpackageFolder.CString());
  194. return false;
  195. }
  196. }
  197. }
  198. JSONArray jplatforms = root["platforms"].GetArray();
  199. for (unsigned i = 0; i < jplatforms.Size(); i++)
  200. {
  201. platforms_.Push(jplatforms[i].GetString());
  202. }
  203. JSONValue jmodulesExclude = root.Get("moduleExclude");
  204. if (jmodulesExclude.IsObject())
  205. {
  206. Vector<String> platforms = jmodulesExclude.GetObject().Keys();
  207. for (unsigned i = 0; i < platforms.Size(); i++)
  208. {
  209. const String& platform = platforms[i];
  210. if (!moduleExcludes_.Contains(platform))
  211. {
  212. moduleExcludes_[platform] = Vector<String>();
  213. }
  214. JSONValue mods = jmodulesExclude.Get(platform);
  215. if (mods.IsArray())
  216. {
  217. for (unsigned j = 0; j < mods.GetArray().Size(); j++)
  218. {
  219. moduleExcludes_[platform].Push(mods.GetArray()[j].GetString());
  220. }
  221. }
  222. }
  223. }
  224. JSONValue dnmodules = root.Get("dotnetModules");
  225. Vector<String> dotNetModules;
  226. if (dnmodules.IsArray())
  227. {
  228. for (unsigned i = 0; i < dnmodules.GetArray().Size(); i++)
  229. {
  230. String moduleName = dnmodules.GetArray()[i].GetString();
  231. dotNetModules.Push(moduleName);
  232. }
  233. }
  234. name_ = root.Get("name").GetString();
  235. namespace_ = root.Get("namespace").GetString();
  236. JSONValue modules = root.Get("modules");
  237. for (unsigned i = 0; i < modules.GetArray().Size(); i++)
  238. {
  239. String moduleName = modules.GetArray()[i].GetString();
  240. SharedPtr<JSBModule> module(new JSBModule(context_, this));
  241. if (!module->Load(packageFolder + moduleName + ".json"))
  242. {
  243. ATOMIC_LOGERRORF("Unable to load module json: %s", (packageFolder + moduleName + ".json").CString());
  244. return false;
  245. }
  246. if (dotNetModules.Contains(moduleName))
  247. {
  248. module->SetDotNetModule(true);
  249. }
  250. modules_.Push(module);
  251. }
  252. // bindings to generate
  253. JSONValue bindings = root.Get("bindings");
  254. if (bindings.IsArray())
  255. {
  256. bindingTypes_.Clear();
  257. for (unsigned i = 0; i < bindings.GetArray().Size(); i++)
  258. {
  259. String binding = bindings.GetArray()[i].GetString();
  260. if (binding.ToUpper() == "CSHARP")
  261. {
  262. bindingTypes_.Push(CSHARP);
  263. }
  264. else if (binding.ToUpper() == "JAVASCRIPT")
  265. {
  266. bindingTypes_.Push(JAVASCRIPT);
  267. }
  268. }
  269. }
  270. allPackages_.Push(SharedPtr<JSBPackage>(this));
  271. PreprocessModules();
  272. ProcessModules();
  273. return true;
  274. }
  275. String JSBPackage::GetPlatformDefineGuard() const
  276. {
  277. if (!platforms_.Size())
  278. return String::EMPTY;
  279. StringVector defines;
  280. for (unsigned i = 0; i < platforms_.Size(); i++)
  281. {
  282. const String& platform = platforms_[i];
  283. if (platform.ToLower() == "windows")
  284. defines.Push("defined(ATOMIC_PLATFORM_WINDOWS)");
  285. else if (platform.ToLower() == "macosx")
  286. defines.Push("defined(ATOMIC_PLATFORM_OSX)");
  287. else if (platform.ToLower() == "linux")
  288. defines.Push("defined(ATOMIC_PLATFORM_LINUX)");
  289. else if (platform.ToLower() == "android")
  290. defines.Push("defined(ATOMIC_PLATFORM_ANDROID)");
  291. else if (platform.ToLower() == "ios")
  292. defines.Push("defined(ATOMIC_PLATFORM_IOS)");
  293. else if (platform.ToLower() == "web")
  294. defines.Push("defined(ATOMIC_PLATFORM_WEB)");
  295. else
  296. {
  297. ATOMIC_LOGERRORF("Unknown package platform: %s", platform.CString());
  298. }
  299. }
  300. if (!defines.Size())
  301. return String::EMPTY;
  302. String defineString = "#if " + String::Joined(defines, " || ");
  303. return defineString;
  304. }
  305. }