JSBEvent.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 "JSBind.h"
  24. #include "JSBHeader.h"
  25. #include "JSBModule.h"
  26. #include "JSBPackage.h"
  27. #include "JSBEvent.h"
  28. using namespace Atomic;
  29. namespace ToolCore
  30. {
  31. JSBEvent::JSBEvent(Context* context, JSBModule* module, const String& eventID, const String& eventName, const String& eventComment) :
  32. Object(context),
  33. module_(module),
  34. header_(0),
  35. eventID_(eventID),
  36. eventName_(eventName),
  37. eventComment_(eventComment)
  38. {
  39. }
  40. JSBEvent::~JSBEvent()
  41. {
  42. }
  43. JSBPackage* JSBEvent::GetPackage()
  44. {
  45. return module_->GetPackage();
  46. }
  47. String JSBEvent::GetScriptEventName(BindingLanguage language) const
  48. {
  49. if (language == BINDINGLANGUAGE_JAVASCRIPT) {
  50. if (eventName_ == "WidgetEvent")
  51. {
  52. return "UIWidgetEvent";
  53. }
  54. if (eventName_ == "WidgetDeleted")
  55. {
  56. return "UIWidgetDeletedEvent";
  57. }
  58. }
  59. if (eventName_.EndsWith("Event"))
  60. return eventName_;
  61. return eventName_ + "Event";
  62. }
  63. unsigned JSBEvent::GetEventHash() const
  64. {
  65. return StringHash(eventName_).Value();
  66. }
  67. bool JSBEvent::ScanModuleEvents(JSBModule* module)
  68. {
  69. const Vector<SharedPtr<JSBHeader>>& headers = module->GetHeaders();
  70. for (unsigned i = 0; i < headers.Size(); i++)
  71. {
  72. JSBHeader* header = headers.At(i);
  73. String source = header->GetSource();
  74. if (!source.Contains("ATOMIC_EVENT"))
  75. continue;
  76. StringVector lines = source.Split('\n');
  77. SharedPtr<JSBEvent> curEvent;
  78. String eventID;
  79. String eventName;
  80. String eventComment;
  81. for (unsigned j = 0; j < lines.Size(); j++)
  82. {
  83. String line = lines[j].Trimmed();
  84. line.Replace("\r", "");
  85. // Note: processes ATOMIC_EVENT decl in commented blocks
  86. if (line.StartsWith("ATOMIC_EVENT"))
  87. {
  88. // First check to see if there is a line comment above this and try to capture it
  89. eventComment = NULL;
  90. if (j > 0) {
  91. String prevLine = lines[j-1].Trimmed();
  92. if (prevLine.StartsWith(("//")))
  93. {
  94. prevLine.Replace("//", "");
  95. eventComment = prevLine.Trimmed();
  96. }
  97. }
  98. StringVector parts = line.Split('(');
  99. if (parts.Size() != 2)
  100. {
  101. ATOMIC_LOGWARNINGF("Unable to parse native event splitting '(' : %s : %s", header->GetFilePath().CString(), line.CString());
  102. continue;
  103. }
  104. parts = parts[1].Split(',');
  105. if (parts.Size() != 2)
  106. {
  107. ATOMIC_LOGWARNINGF("Unable to parse native even splitting ',' : %s : %s", header->GetFilePath().CString(), line.CString());
  108. continue;
  109. }
  110. eventID = parts[0].Trimmed();
  111. eventName = parts[1].Trimmed();
  112. eventName.Replace(")", "");
  113. curEvent = new JSBEvent(module->GetContext(), module, eventID, eventName, eventComment);
  114. }
  115. if (line.Contains("}") && curEvent.NotNull())
  116. {
  117. module->RegisterEvent(curEvent);
  118. curEvent = 0;
  119. }
  120. // ATOMIC_PARAM(P_TYPE, Type); // enum UI_EVENT_TYPE
  121. if (line.StartsWith("ATOMIC_PARAM"))
  122. {
  123. if (curEvent.Null())
  124. {
  125. ATOMIC_LOGWARNINGF("WARNING: Event param outside of current event: %s", line.CString());
  126. continue;
  127. }
  128. line.Replace("ATOMIC_PARAM", "");
  129. line.Replace("(", "");
  130. line.Replace(")", "");
  131. StringVector parts = line.Split(';');
  132. if (parts.Size() != 2)
  133. {
  134. ATOMIC_LOGWARNINGF("Unable to parse native event param splitting ';' : %s : %s", header->GetFilePath().CString(), line.CString());
  135. continue;
  136. }
  137. String typeInfo;
  138. String enumTypeName;
  139. String formalComment;
  140. // comment contains type
  141. if (parts[1].Contains("//"))
  142. {
  143. String comment = parts[1];
  144. comment.Replace("//", "");
  145. comment = comment.Trimmed();
  146. StringVector typeVector = comment.Split(' ');
  147. int commentStart = 1;
  148. if (typeVector.Size() > 0)
  149. {
  150. typeInfo = typeVector[0];
  151. if (typeInfo == "enum" && typeVector.Size() > 1)
  152. {
  153. if (JSBPackage::GetEnumAllPackages(typeVector[1]))
  154. {
  155. enumTypeName = typeVector[1];
  156. }
  157. commentStart = 1;
  158. }
  159. // Check to see if there are any comments following and try to capture them
  160. if (typeVector.Size() > commentStart + 1)
  161. {
  162. formalComment = "";
  163. for (int i = commentStart; i < typeVector.Size(); i++) {
  164. formalComment.Append(" ");
  165. formalComment.Append(typeVector[i]);
  166. }
  167. }
  168. }
  169. }
  170. if (!typeInfo.Length())
  171. {
  172. ATOMIC_LOGWARNINGF("Could not get type info for event param : %s : (%s) file: %s",
  173. curEvent->GetEventID().CString(), line.CString(), header->GetFilePath().CString());
  174. continue;
  175. }
  176. // ID and Name parse
  177. parts = parts[0].Split(',');
  178. if (parts.Size() != 2)
  179. {
  180. ATOMIC_LOGWARNINGF("Unable to parse native event param splitting ',' : %s : %s", header->GetFilePath().CString(), line.CString());
  181. continue;
  182. }
  183. typeInfo.Replace("*", "");
  184. EventParam param;
  185. param.paramID_ = parts[0].Trimmed();
  186. param.paramName_ = parts[1].Trimmed();
  187. param.typeInfo_ = typeInfo;
  188. param.enumTypeName_ = enumTypeName;
  189. param.comment_ = formalComment;
  190. curEvent->parameters_.Push(param);
  191. }
  192. }
  193. }
  194. return true;
  195. }
  196. }