NETCore.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include <Atomic/Math/MathDefs.h>
  2. #include <Atomic/Core/ProcessUtils.h>
  3. #include <Atomic/Core/Thread.h>
  4. #include <Atomic/IO/Log.h>
  5. #include <Atomic/Script/ScriptVariantMap.h>
  6. #include "NETCore.h"
  7. #include "NETEventDispatcher.h"
  8. namespace Atomic
  9. {
  10. SharedPtr<Context> NETCore::csContext_;
  11. NETCoreEventDispatchFunction NETCore::eventDispatch_ = nullptr;
  12. NETCoreUpdateDispatchFunction NETCore::updateDispatch_ = nullptr;
  13. NETCoreRefCountedDeletedFunction NETCore::refCountedDeleted_ = nullptr;
  14. NETCoreThrowManagedExceptionFunction NETCore::throwManagedException_ = nullptr;
  15. NETCore::NETCore(Context* context, NETCoreDelegates* delegates) :
  16. Object(context)
  17. {
  18. assert (!csContext_);
  19. csContext_ = context;
  20. eventDispatch_ = delegates->eventDispatch;
  21. updateDispatch_ = delegates->updateDispatch;
  22. refCountedDeleted_ = delegates->refCountedDeleted;
  23. throwManagedException_ = delegates->throwManagedException;
  24. NETEventDispatcher* dispatcher = new NETEventDispatcher(context_);
  25. context_->RegisterSubsystem(dispatcher);
  26. context_->AddGlobalEventListener(dispatcher);
  27. RefCounted::AddRefCountedDeletedFunction(OnRefCountedDeleted);
  28. }
  29. NETCore::~NETCore()
  30. {
  31. RefCounted::RemoveRefCountedDeletedFunction(OnRefCountedDeleted);
  32. assert (!csContext_);
  33. }
  34. bool NETCore::EnsureMainThread(const String& throwMsg)
  35. {
  36. if (!Thread::IsMainThread())
  37. {
  38. if (throwMsg.Length())
  39. {
  40. NETCore::ThrowManagedException(throwMsg.CString());
  41. }
  42. return false;
  43. }
  44. return true;
  45. }
  46. void NETCore::OnRefCountedDeleted(RefCounted* ref)
  47. {
  48. if (csContext_.Null())
  49. return;
  50. if (refCountedDeleted_)
  51. refCountedDeleted_(ref);
  52. }
  53. void NETCore::RegisterNETEventType(unsigned eventType)
  54. {
  55. NETEventDispatcher* dispatcher = csContext_->GetSubsystem<NETEventDispatcher>();
  56. dispatcher->RegisterNETEvent(StringHash(eventType));
  57. }
  58. void NETCore::Shutdown()
  59. {
  60. assert (csContext_);
  61. csContext_->RemoveGlobalEventListener(csContext_->GetSubsystem<NETEventDispatcher>());
  62. csContext_->RemoveSubsystem(NETEventDispatcher::GetTypeStatic());
  63. eventDispatch_ = nullptr;
  64. csContext_ = nullptr;
  65. eventDispatch_ = nullptr;
  66. updateDispatch_ = nullptr;
  67. refCountedDeleted_ = nullptr;
  68. throwManagedException_ = nullptr;
  69. }
  70. }