Context.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. //
  2. // Copyright (c) 2008-2017 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. // ATOMIC BEGIN
  25. #include "../Core/Profiler.h"
  26. // ATOMIC END
  27. #include "../IO/Log.h"
  28. #ifndef MINI_URHO
  29. #include <SDL.h>
  30. #ifdef ATOMIC_IK
  31. #include <ik/log.h>
  32. #include <ik/memory.h>
  33. #endif
  34. #endif
  35. #include "../DebugNew.h"
  36. namespace Atomic
  37. {
  38. #ifndef MINI_URHO
  39. // Keeps track of how many times SDL was initialised so we know when to call SDL_Quit().
  40. static int sdlInitCounter = 0;
  41. // Keeps track of how many times IK was initialised
  42. static int ikInitCounter = 0;
  43. // Reroute all messages from the ik library to the Atomic log
  44. static void HandleIKLog(const char* msg)
  45. {
  46. ATOMIC_LOGINFOF("[IK] %s", msg);
  47. }
  48. #endif
  49. void EventReceiverGroup::BeginSendEvent()
  50. {
  51. ++inSend_;
  52. }
  53. void EventReceiverGroup::EndSendEvent()
  54. {
  55. assert(inSend_ > 0);
  56. --inSend_;
  57. if (inSend_ == 0 && dirty_)
  58. {
  59. /// \todo Could be optimized by erase-swap, but this keeps the receiver order
  60. for (unsigned i = receivers_.Size() - 1; i < receivers_.Size(); --i)
  61. {
  62. if (!receivers_[i])
  63. receivers_.Erase(i);
  64. }
  65. dirty_ = false;
  66. }
  67. }
  68. void EventReceiverGroup::Add(Object* object)
  69. {
  70. if (object)
  71. receivers_.Push(object);
  72. }
  73. void EventReceiverGroup::Remove(Object* object)
  74. {
  75. if (inSend_ > 0)
  76. {
  77. PODVector<Object*>::Iterator i = receivers_.Find(object);
  78. if (i != receivers_.End())
  79. {
  80. (*i) = 0;
  81. dirty_ = true;
  82. }
  83. }
  84. else
  85. receivers_.Remove(object);
  86. }
  87. void RemoveNamedAttribute(HashMap<StringHash, Vector<AttributeInfo> >& attributes, StringHash objectType, const char* name)
  88. {
  89. HashMap<StringHash, Vector<AttributeInfo> >::Iterator i = attributes.Find(objectType);
  90. if (i == attributes.End())
  91. return;
  92. Vector<AttributeInfo>& infos = i->second_;
  93. for (Vector<AttributeInfo>::Iterator j = infos.Begin(); j != infos.End(); ++j)
  94. {
  95. if (!j->name_.Compare(name, true))
  96. {
  97. infos.Erase(j);
  98. break;
  99. }
  100. }
  101. // If the vector became empty, erase the object type from the map
  102. if (infos.Empty())
  103. attributes.Erase(i);
  104. }
  105. Context::Context() :
  106. eventHandler_(0),
  107. // ATOMIC BEGIN
  108. editorContext_(false)
  109. // ATOMIC END
  110. {
  111. #ifdef __ANDROID__
  112. // Always reset the random seed on Android, as the Urho3D library might not be unloaded between runs
  113. SetRandomSeed(1);
  114. #endif
  115. // Set the main thread ID (assuming the Context is created in it)
  116. Thread::SetMainThread();
  117. }
  118. Context::~Context()
  119. {
  120. // Remove subsystems that use SDL in reverse order of construction, so that Graphics can shut down SDL last
  121. /// \todo Context should not need to know about subsystems
  122. RemoveSubsystem("Audio");
  123. RemoveSubsystem("UI");
  124. RemoveSubsystem("Input");
  125. RemoveSubsystem("Renderer");
  126. RemoveSubsystem("Graphics");
  127. subsystems_.Clear();
  128. factories_.Clear();
  129. // Delete allocated event data maps
  130. for (PODVector<VariantMap*>::Iterator i = eventDataMaps_.Begin(); i != eventDataMaps_.End(); ++i)
  131. delete *i;
  132. eventDataMaps_.Clear();
  133. }
  134. // ATOMIC BEGIN
  135. SharedPtr<Object> Context::CreateObject(StringHash objectType, const XMLElement& source)
  136. {
  137. HashMap<StringHash, SharedPtr<ObjectFactory> >::ConstIterator i = factories_.Find(objectType);
  138. if (i != factories_.End())
  139. return i->second_->CreateObject(source);
  140. else
  141. return SharedPtr<Object>();
  142. }
  143. // ATOMIC END
  144. void Context::RegisterFactory(ObjectFactory* factory)
  145. {
  146. if (!factory)
  147. return;
  148. factories_[factory->GetType()] = factory;
  149. }
  150. void Context::RegisterFactory(ObjectFactory* factory, const char* category)
  151. {
  152. if (!factory)
  153. return;
  154. RegisterFactory(factory);
  155. if (String::CStringLength(category))
  156. objectCategories_[category].Push(factory->GetType());
  157. }
  158. void Context::RegisterSubsystem(Object* object)
  159. {
  160. if (!object)
  161. return;
  162. subsystems_[object->GetType()] = object;
  163. }
  164. void Context::RemoveSubsystem(StringHash objectType)
  165. {
  166. HashMap<StringHash, SharedPtr<Object> >::Iterator i = subsystems_.Find(objectType);
  167. if (i != subsystems_.End())
  168. subsystems_.Erase(i);
  169. }
  170. void Context::RegisterAttribute(StringHash objectType, const AttributeInfo& attr)
  171. {
  172. // None or pointer types can not be supported
  173. if (attr.type_ == VAR_NONE || attr.type_ == VAR_VOIDPTR || attr.type_ == VAR_PTR)
  174. {
  175. ATOMIC_LOGWARNING("Attempt to register unsupported attribute type " + Variant::GetTypeName(attr.type_) + " to class " +
  176. GetTypeName(objectType));
  177. return;
  178. }
  179. attributes_[objectType].Push(attr);
  180. if (attr.mode_ & AM_NET)
  181. networkAttributes_[objectType].Push(attr);
  182. }
  183. void Context::RemoveAttribute(StringHash objectType, const char* name)
  184. {
  185. RemoveNamedAttribute(attributes_, objectType, name);
  186. RemoveNamedAttribute(networkAttributes_, objectType, name);
  187. }
  188. void Context::UpdateAttributeDefaultValue(StringHash objectType, const char* name, const Variant& defaultValue)
  189. {
  190. AttributeInfo* info = GetAttribute(objectType, name);
  191. if (info)
  192. info->defaultValue_ = defaultValue;
  193. }
  194. VariantMap& Context::GetEventDataMap()
  195. {
  196. unsigned nestingLevel = eventSenders_.Size();
  197. while (eventDataMaps_.Size() < nestingLevel + 1)
  198. eventDataMaps_.Push(new VariantMap());
  199. VariantMap& ret = *eventDataMaps_[nestingLevel];
  200. ret.Clear();
  201. return ret;
  202. }
  203. #ifndef MINI_URHO
  204. bool Context::RequireSDL(unsigned int sdlFlags)
  205. {
  206. // Always increment, the caller must match with ReleaseSDL(), regardless of
  207. // what happens.
  208. ++sdlInitCounter;
  209. // Need to call SDL_Init() at least once before SDL_InitSubsystem()
  210. if (sdlInitCounter == 1)
  211. {
  212. ATOMIC_LOGDEBUG("Initialising SDL");
  213. if (SDL_Init(0) != 0)
  214. {
  215. ATOMIC_LOGERRORF("Failed to initialise SDL: %s", SDL_GetError());
  216. return false;
  217. }
  218. }
  219. Uint32 remainingFlags = sdlFlags & ~SDL_WasInit(0);
  220. if (remainingFlags != 0)
  221. {
  222. if (SDL_InitSubSystem(remainingFlags) != 0)
  223. {
  224. ATOMIC_LOGERRORF("Failed to initialise SDL subsystem: %s", SDL_GetError());
  225. return false;
  226. }
  227. }
  228. return true;
  229. }
  230. void Context::ReleaseSDL()
  231. {
  232. --sdlInitCounter;
  233. if (sdlInitCounter == 0)
  234. {
  235. ATOMIC_LOGDEBUG("Quitting SDL");
  236. SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
  237. SDL_Quit();
  238. }
  239. if (sdlInitCounter < 0)
  240. ATOMIC_LOGERROR("Too many calls to Context::ReleaseSDL()!");
  241. }
  242. #ifdef ATOMIC_IK
  243. void Context::RequireIK()
  244. {
  245. // Always increment, the caller must match with ReleaseSDL(), regardless of
  246. // what happens.
  247. ++ikInitCounter;
  248. if (ikInitCounter == 1)
  249. {
  250. ATOMIC_LOGDEBUG("Initialising Inverse Kinematics library");
  251. ik_memory_init();
  252. ik_log_init(IK_LOG_NONE);
  253. ik_log_register_listener(HandleIKLog);
  254. }
  255. }
  256. void Context::ReleaseIK()
  257. {
  258. --ikInitCounter;
  259. if (ikInitCounter == 0)
  260. {
  261. ATOMIC_LOGDEBUG("De-initialising Inverse Kinematics library");
  262. ik_log_unregister_listener(HandleIKLog);
  263. ik_log_deinit();
  264. ik_memory_deinit();
  265. }
  266. if (ikInitCounter < 0)
  267. ATOMIC_LOGERROR("Too many calls to Context::ReleaseIK()");
  268. }
  269. #endif
  270. #endif
  271. void Context::CopyBaseAttributes(StringHash baseType, StringHash derivedType)
  272. {
  273. // Prevent endless loop if mistakenly copying attributes from same class as derived
  274. if (baseType == derivedType)
  275. {
  276. ATOMIC_LOGWARNING("Attempt to copy base attributes to itself for class " + GetTypeName(baseType));
  277. return;
  278. }
  279. const Vector<AttributeInfo>* baseAttributes = GetAttributes(baseType);
  280. if (baseAttributes)
  281. {
  282. for (unsigned i = 0; i < baseAttributes->Size(); ++i)
  283. {
  284. const AttributeInfo& attr = baseAttributes->At(i);
  285. attributes_[derivedType].Push(attr);
  286. if (attr.mode_ & AM_NET)
  287. networkAttributes_[derivedType].Push(attr);
  288. }
  289. }
  290. }
  291. Object* Context::GetSubsystem(StringHash type) const
  292. {
  293. HashMap<StringHash, SharedPtr<Object> >::ConstIterator i = subsystems_.Find(type);
  294. if (i != subsystems_.End())
  295. return i->second_;
  296. else
  297. return 0;
  298. }
  299. const Variant& Context::GetGlobalVar(StringHash key) const
  300. {
  301. VariantMap::ConstIterator i = globalVars_.Find(key);
  302. return i != globalVars_.End() ? i->second_ : Variant::EMPTY;
  303. }
  304. void Context::SetGlobalVar(StringHash key, const Variant& value)
  305. {
  306. globalVars_[key] = value;
  307. }
  308. Object* Context::GetEventSender() const
  309. {
  310. if (!eventSenders_.Empty())
  311. return eventSenders_.Back();
  312. else
  313. return 0;
  314. }
  315. const String& Context::GetTypeName(StringHash objectType) const
  316. {
  317. // ATOMIC BEGIN
  318. // Search factories to find the hash-to-name mapping
  319. HashMap<StringHash, SharedPtr<ObjectFactory> >::ConstIterator i = factories_.Find(objectType);
  320. return i != factories_.End() ? i->second_->GetFactoryTypeName() : String::EMPTY;
  321. // ATOMIC END
  322. }
  323. AttributeInfo* Context::GetAttribute(StringHash objectType, const char* name)
  324. {
  325. HashMap<StringHash, Vector<AttributeInfo> >::Iterator i = attributes_.Find(objectType);
  326. if (i == attributes_.End())
  327. return 0;
  328. Vector<AttributeInfo>& infos = i->second_;
  329. for (Vector<AttributeInfo>::Iterator j = infos.Begin(); j != infos.End(); ++j)
  330. {
  331. if (!j->name_.Compare(name, true))
  332. return &(*j);
  333. }
  334. return 0;
  335. }
  336. void Context::AddEventReceiver(Object* receiver, StringHash eventType)
  337. {
  338. SharedPtr<EventReceiverGroup>& group = eventReceivers_[eventType];
  339. if (!group)
  340. group = new EventReceiverGroup();
  341. group->Add(receiver);
  342. }
  343. void Context::AddEventReceiver(Object* receiver, Object* sender, StringHash eventType)
  344. {
  345. SharedPtr<EventReceiverGroup>& group = specificEventReceivers_[sender][eventType];
  346. if (!group)
  347. group = new EventReceiverGroup();
  348. group->Add(receiver);
  349. }
  350. void Context::RemoveEventSender(Object* sender)
  351. {
  352. HashMap<Object*, HashMap<StringHash, SharedPtr<EventReceiverGroup> > >::Iterator i = specificEventReceivers_.Find(sender);
  353. if (i != specificEventReceivers_.End())
  354. {
  355. for (HashMap<StringHash, SharedPtr<EventReceiverGroup> >::Iterator j = i->second_.Begin(); j != i->second_.End(); ++j)
  356. {
  357. for (PODVector<Object*>::Iterator k = j->second_->receivers_.Begin(); k != j->second_->receivers_.End(); ++k)
  358. {
  359. Object* receiver = *k;
  360. if (receiver)
  361. receiver->RemoveEventSender(sender);
  362. }
  363. }
  364. specificEventReceivers_.Erase(i);
  365. }
  366. }
  367. void Context::RemoveEventReceiver(Object* receiver, StringHash eventType)
  368. {
  369. EventReceiverGroup* group = GetEventReceivers(eventType);
  370. if (group)
  371. group->Remove(receiver);
  372. }
  373. void Context::RemoveEventReceiver(Object* receiver, Object* sender, StringHash eventType)
  374. {
  375. EventReceiverGroup* group = GetEventReceivers(sender, eventType);
  376. if (group)
  377. group->Remove(receiver);
  378. }
  379. void Context::BeginSendEvent(Object* sender, StringHash eventType)
  380. {
  381. eventSenders_.Push(sender);
  382. }
  383. void Context::EndSendEvent()
  384. {
  385. eventSenders_.Pop();
  386. }
  387. }