JSBModule.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include <Atomic/IO/Log.h>
  2. #include <Atomic/IO/File.h>
  3. #include <Atomic/IO/FileSystem.h>
  4. #include <Atomic/Resource/JSONFile.h>
  5. #include "Atomic/Core/ProcessUtils.h"
  6. #include "JSBind.h"
  7. #include "JSBPackage.h"
  8. #include "JSBModule.h"
  9. #include "JSBHeader.h"
  10. #include "JSBClass.h"
  11. #include "JSBEnum.h"
  12. namespace ToolCore
  13. {
  14. JSBModule::JSBModule(Context* context, JSBPackage* package) : Object(context),
  15. package_(package)
  16. {
  17. }
  18. JSBModule::~JSBModule()
  19. {
  20. }
  21. void JSBModule::PreprocessHeaders()
  22. {
  23. for (unsigned i = 0; i < headers_.Size(); i++)
  24. {
  25. headers_[i]->VisitPreprocess();
  26. }
  27. }
  28. void JSBModule::ScanHeaders()
  29. {
  30. JSBind* jsbind = GetSubsystem<JSBind>();
  31. FileSystem* fs = GetSubsystem<FileSystem>();
  32. const String& sourceRoot = jsbind->GetSourceRootFolder();
  33. for (unsigned i = 0; i < sourceDirs_.Size(); i++)
  34. {
  35. const String& dir = sourceRoot + sourceDirs_[i] + "/";
  36. Vector<String> fileNames;
  37. fs->ScanDir(fileNames, dir, "*.h", SCAN_FILES, false);
  38. for (unsigned k = 0; k < fileNames.Size(); k++)
  39. {
  40. String filepath = dir + fileNames[k];
  41. SharedPtr<JSBHeader> header(new JSBHeader(context_, this, filepath));
  42. // Parse the C++ header
  43. header->Parse();
  44. headers_.Push(header);
  45. }
  46. }
  47. }
  48. JSBClass* JSBModule::GetClass(const String& name)
  49. {
  50. if (classes_.Contains(name))
  51. return classes_[name];
  52. return 0;
  53. }
  54. void JSBModule::RegisterClass(String name)
  55. {
  56. String nativeName = name;
  57. if (classnames_.Contains(name))
  58. {
  59. if (classRenames_.Contains(name))
  60. {
  61. name = classRenames_[name];
  62. }
  63. if (JSBPackage::GetClassAllPackages(nativeName))
  64. {
  65. ErrorExit(ToString("Class collision: %s", name.CString()));
  66. }
  67. JSBClass* cls = new JSBClass(context_, this, name, nativeName);
  68. classes_[nativeName] = cls;
  69. LOGINFOF("Registered Class: %s", cls->GetName().CString());
  70. }
  71. }
  72. void JSBModule::RegisterEnum(JSBEnum* jenum)
  73. {
  74. if (JSBPackage::GetClassAllPackages(jenum->GetName()))
  75. {
  76. ErrorExit(ToString("Enum collision: %s", jenum->GetName().CString()));
  77. }
  78. enums_[jenum->GetName()] = jenum;
  79. LOGINFOF("Registered Enum: %s", jenum->GetName().CString());
  80. }
  81. JSBEnum* JSBModule::GetEnum(const String& name)
  82. {
  83. if (enums_.Contains(name))
  84. {
  85. return enums_[name];
  86. }
  87. return 0;
  88. }
  89. bool JSBModule::ContainsConstant(const String& constantName)
  90. {
  91. return constants_.Contains(constantName);
  92. }
  93. void JSBModule::RegisterConstant(const String& constantName)
  94. {
  95. if (JSBPackage::ContainsConstantAllPackages(constantName))
  96. {
  97. ErrorExit(ToString("Constant collision: %s", constantName.CString()));
  98. }
  99. constants_.Push(constantName);
  100. }
  101. bool JSBModule::Load(const String& jsonFilename)
  102. {
  103. LOGINFOF("Loading Module: %s", jsonFilename.CString());
  104. SharedPtr<File> jsonFile(new File(context_, jsonFilename));
  105. if (!jsonFile->IsOpen())
  106. {
  107. LOGERRORF("Unable to open module json: %s", jsonFilename.CString());
  108. return false;
  109. }
  110. SharedPtr<JSONFile> moduleJSON(new JSONFile(context_));
  111. if (!moduleJSON->BeginLoad(*jsonFile))
  112. {
  113. LOGERRORF("Unable to parse module json: %s", jsonFilename.CString());
  114. return false;
  115. }
  116. JSONValue root = moduleJSON->GetRoot();
  117. name_ = root.GetString("name");
  118. JSONValue classes = root.GetChild("classes");
  119. for (unsigned i = 0; i < classes.GetSize(); i++)
  120. {
  121. classnames_.Push(classes.GetString(i));
  122. }
  123. JSONValue classes_rename = root.GetChild("classes_rename");
  124. if (classes_rename.IsObject())
  125. {
  126. Vector<String> childNames = classes_rename.GetValueNames();
  127. for (unsigned j = 0; j < childNames.Size(); j++)
  128. {
  129. String classname = childNames.At(j);
  130. String crename = classes_rename.GetString(classname);
  131. classRenames_[classname] = crename;
  132. }
  133. }
  134. JSONValue sources = root.GetChild("sources");
  135. for (unsigned i = 0; i < sources.GetSize(); i++)
  136. {
  137. sourceDirs_.Push(sources.GetString(i));
  138. }
  139. ScanHeaders();
  140. return true;
  141. }
  142. }