JSBEvent.cpp 6.5 KB

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