JSBPackage.cpp 8.7 KB

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