Context.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. //
  2. // Copyright (c) 2008-2016 the Urho3D project.
  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 "../Precompiled.h"
  23. #include "../Core/Context.h"
  24. #include "../Core/EventProfiler.h"
  25. #include "../IO/Log.h"
  26. #include "../DebugNew.h"
  27. namespace Atomic
  28. {
  29. void RemoveNamedAttribute(HashMap<StringHash, Vector<AttributeInfo> >& attributes, StringHash objectType, const char* name)
  30. {
  31. HashMap<StringHash, Vector<AttributeInfo> >::Iterator i = attributes.Find(objectType);
  32. if (i == attributes.End())
  33. return;
  34. Vector<AttributeInfo>& infos = i->second_;
  35. for (Vector<AttributeInfo>::Iterator j = infos.Begin(); j != infos.End(); ++j)
  36. {
  37. if (!j->name_.Compare(name, true))
  38. {
  39. infos.Erase(j);
  40. break;
  41. }
  42. }
  43. // If the vector became empty, erase the object type from the map
  44. if (infos.Empty())
  45. attributes.Erase(i);
  46. }
  47. Context::Context() :
  48. eventHandler_(0),
  49. // ATOMIC BEGIN
  50. editorContext_(false)
  51. // ATOMIC END
  52. {
  53. #ifdef __ANDROID__
  54. // Always reset the random seed on Android, as the Urho3D library might not be unloaded between runs
  55. SetRandomSeed(1);
  56. #endif
  57. // Set the main thread ID (assuming the Context is created in it)
  58. Thread::SetMainThread();
  59. }
  60. Context::~Context()
  61. {
  62. // Remove subsystems that use SDL in reverse order of construction, so that Graphics can shut down SDL last
  63. /// \todo Context should not need to know about subsystems
  64. RemoveSubsystem("Audio");
  65. RemoveSubsystem("UI");
  66. RemoveSubsystem("Input");
  67. RemoveSubsystem("Renderer");
  68. RemoveSubsystem("Graphics");
  69. subsystems_.Clear();
  70. factories_.Clear();
  71. // Delete allocated event data maps
  72. for (PODVector<VariantMap*>::Iterator i = eventDataMaps_.Begin(); i != eventDataMaps_.End(); ++i)
  73. delete *i;
  74. eventDataMaps_.Clear();
  75. }
  76. // ATOMIC BEGIN
  77. SharedPtr<Object> Context::CreateObject(StringHash objectType, const XMLElement& source)
  78. {
  79. HashMap<StringHash, SharedPtr<ObjectFactory> >::ConstIterator i = factories_.Find(objectType);
  80. if (i != factories_.End())
  81. return i->second_->CreateObject(source);
  82. else
  83. return SharedPtr<Object>();
  84. }
  85. // ATOMIC END
  86. void Context::RegisterFactory(ObjectFactory* factory)
  87. {
  88. if (!factory)
  89. return;
  90. factories_[factory->GetType()] = factory;
  91. }
  92. void Context::RegisterFactory(ObjectFactory* factory, const char* category)
  93. {
  94. if (!factory)
  95. return;
  96. RegisterFactory(factory);
  97. if (String::CStringLength(category))
  98. objectCategories_[category].Push(factory->GetType());
  99. }
  100. void Context::RegisterSubsystem(Object* object)
  101. {
  102. if (!object)
  103. return;
  104. subsystems_[object->GetType()] = object;
  105. }
  106. void Context::RemoveSubsystem(StringHash objectType)
  107. {
  108. HashMap<StringHash, SharedPtr<Object> >::Iterator i = subsystems_.Find(objectType);
  109. if (i != subsystems_.End())
  110. subsystems_.Erase(i);
  111. }
  112. void Context::RegisterAttribute(StringHash objectType, const AttributeInfo& attr)
  113. {
  114. // None or pointer types can not be supported
  115. if (attr.type_ == VAR_NONE || attr.type_ == VAR_VOIDPTR || attr.type_ == VAR_PTR)
  116. {
  117. ATOMIC_LOGWARNING("Attempt to register unsupported attribute type " + Variant::GetTypeName(attr.type_) + " to class " +
  118. GetTypeName(objectType));
  119. return;
  120. }
  121. attributes_[objectType].Push(attr);
  122. if (attr.mode_ & AM_NET)
  123. networkAttributes_[objectType].Push(attr);
  124. }
  125. void Context::RemoveAttribute(StringHash objectType, const char* name)
  126. {
  127. RemoveNamedAttribute(attributes_, objectType, name);
  128. RemoveNamedAttribute(networkAttributes_, objectType, name);
  129. }
  130. void Context::UpdateAttributeDefaultValue(StringHash objectType, const char* name, const Variant& defaultValue)
  131. {
  132. AttributeInfo* info = GetAttribute(objectType, name);
  133. if (info)
  134. info->defaultValue_ = defaultValue;
  135. }
  136. VariantMap& Context::GetEventDataMap()
  137. {
  138. unsigned nestingLevel = eventSenders_.Size();
  139. while (eventDataMaps_.Size() < nestingLevel + 1)
  140. eventDataMaps_.Push(new VariantMap());
  141. VariantMap& ret = *eventDataMaps_[nestingLevel];
  142. ret.Clear();
  143. return ret;
  144. }
  145. void Context::CopyBaseAttributes(StringHash baseType, StringHash derivedType)
  146. {
  147. // Prevent endless loop if mistakenly copying attributes from same class as derived
  148. if (baseType == derivedType)
  149. {
  150. ATOMIC_LOGWARNING("Attempt to copy base attributes to itself for class " + GetTypeName(baseType));
  151. return;
  152. }
  153. const Vector<AttributeInfo>* baseAttributes = GetAttributes(baseType);
  154. if (baseAttributes)
  155. {
  156. for (unsigned i = 0; i < baseAttributes->Size(); ++i)
  157. {
  158. const AttributeInfo& attr = baseAttributes->At(i);
  159. attributes_[derivedType].Push(attr);
  160. if (attr.mode_ & AM_NET)
  161. networkAttributes_[derivedType].Push(attr);
  162. }
  163. }
  164. }
  165. Object* Context::GetSubsystem(StringHash type) const
  166. {
  167. HashMap<StringHash, SharedPtr<Object> >::ConstIterator i = subsystems_.Find(type);
  168. if (i != subsystems_.End())
  169. return i->second_;
  170. else
  171. return 0;
  172. }
  173. const Variant& Context::GetGlobalVar(StringHash key) const
  174. {
  175. VariantMap::ConstIterator i = globalVars_.Find(key);
  176. return i != globalVars_.End() ? i->second_ : Variant::EMPTY;
  177. }
  178. void Context::SetGlobalVar(StringHash key, const Variant& value)
  179. {
  180. globalVars_[key] = value;
  181. }
  182. Object* Context::GetEventSender() const
  183. {
  184. if (!eventSenders_.Empty())
  185. return eventSenders_.Back();
  186. else
  187. return 0;
  188. }
  189. const String& Context::GetTypeName(StringHash objectType) const
  190. {
  191. // ATOMIC BEGIN
  192. // Search factories to find the hash-to-name mapping
  193. HashMap<StringHash, SharedPtr<ObjectFactory> >::ConstIterator i = factories_.Find(objectType);
  194. return i != factories_.End() ? i->second_->GetFactoryTypeName() : String::EMPTY;
  195. // ATOMIC END
  196. }
  197. AttributeInfo* Context::GetAttribute(StringHash objectType, const char* name)
  198. {
  199. HashMap<StringHash, Vector<AttributeInfo> >::Iterator i = attributes_.Find(objectType);
  200. if (i == attributes_.End())
  201. return 0;
  202. Vector<AttributeInfo>& infos = i->second_;
  203. for (Vector<AttributeInfo>::Iterator j = infos.Begin(); j != infos.End(); ++j)
  204. {
  205. if (!j->name_.Compare(name, true))
  206. return &(*j);
  207. }
  208. return 0;
  209. }
  210. void Context::AddEventReceiver(Object* receiver, StringHash eventType)
  211. {
  212. eventReceivers_[eventType].Insert(receiver);
  213. }
  214. void Context::AddEventReceiver(Object* receiver, Object* sender, StringHash eventType)
  215. {
  216. specificEventReceivers_[sender][eventType].Insert(receiver);
  217. }
  218. void Context::RemoveEventSender(Object* sender)
  219. {
  220. HashMap<Object*, HashMap<StringHash, HashSet<Object*> > >::Iterator i = specificEventReceivers_.Find(sender);
  221. if (i != specificEventReceivers_.End())
  222. {
  223. for (HashMap<StringHash, HashSet<Object*> >::Iterator j = i->second_.Begin(); j != i->second_.End(); ++j)
  224. {
  225. for (HashSet<Object*>::Iterator k = j->second_.Begin(); k != j->second_.End(); ++k)
  226. (*k)->RemoveEventSender(sender);
  227. }
  228. specificEventReceivers_.Erase(i);
  229. }
  230. }
  231. void Context::RemoveEventReceiver(Object* receiver, StringHash eventType)
  232. {
  233. HashSet<Object*>* group = GetEventReceivers(eventType);
  234. if (group)
  235. group->Erase(receiver);
  236. }
  237. void Context::RemoveEventReceiver(Object* receiver, Object* sender, StringHash eventType)
  238. {
  239. HashSet<Object*>* group = GetEventReceivers(sender, eventType);
  240. if (group)
  241. group->Erase(receiver);
  242. }
  243. void Context::BeginSendEvent(Object* sender, StringHash eventType)
  244. {
  245. #ifdef ATOMIC_PROFILING
  246. if (EventProfiler::IsActive())
  247. {
  248. EventProfiler* eventProfiler = GetSubsystem<EventProfiler>();
  249. if (eventProfiler)
  250. eventProfiler->BeginBlock(eventType);
  251. }
  252. #endif
  253. eventSenders_.Push(sender);
  254. }
  255. void Context::EndSendEvent()
  256. {
  257. eventSenders_.Pop();
  258. #ifdef ATOMIC_PROFILING
  259. if (EventProfiler::IsActive())
  260. {
  261. EventProfiler* eventProfiler = GetSubsystem<EventProfiler>();
  262. if (eventProfiler)
  263. eventProfiler->EndBlock();
  264. }
  265. #endif
  266. }
  267. }