JSBEvent.cpp 6.2 KB

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