JSBEvent.cpp 6.2 KB

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