You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
3.9 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2013 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
  5. // 2013 Sam Hocevar <sam@hocevar.net>
  6. // This program is free software; you can redistribute it and/or
  7. // modify it under the terms of the Do What The Fuck You Want To
  8. // Public License, Version 2, as published by Sam Hocevar. See
  9. // http://www.wtfpl.net/ for more details.
  10. //
  11. #include <lol/engine-internal.h>
  12. #include <cstring>
  13. #include <cstdlib>
  14. #include <time.h>
  15. namespace lol
  16. {
  17. // This is needed to map MessageService::Send() to the JS when building the HTML.
  18. #if EMSCRIPTEN
  19. extern "C"
  20. {
  21. int C_Send(const char* message) { return (int)MessageService::Send(MessageBucket::AppIn, message); }
  22. //NOT IMPLEMENTED
  23. //bool C_FetchFirst(String& message);
  24. }
  25. #endif //EMSCRIPTEN
  26. /*
  27. * The global g_renderer object, initialised by Video::Init
  28. */
  29. MessageService *g_messageservice = nullptr;
  30. //-----------------------------------------------------------------------------
  31. // Ctor/Dtor
  32. //----
  33. MessageService::MessageService()
  34. {
  35. }
  36. MessageService::~MessageService()
  37. {
  38. m_bucket.empty();
  39. }
  40. //Setup/Destroy
  41. void MessageService::Setup()
  42. {
  43. g_messageservice = new MessageService();
  44. g_messageservice->m_bucket.resize(MessageBucket::MAX);
  45. }
  46. void MessageService::Destroy()
  47. {
  48. delete g_messageservice;
  49. g_messageservice = nullptr;
  50. }
  51. //-----------------------------------------------------------------------------
  52. bool MessageService::Send(MessageBucket id, const String& message)
  53. {
  54. if (g_messageservice)
  55. {
  56. ASSERT(0 <= id.ToScalar() && id.ToScalar() < g_messageservice->m_bucket.count());
  57. return g_messageservice->Send(id, message.C());
  58. }
  59. return false;
  60. }
  61. bool MessageService::Send(MessageBucket id, const char* message)
  62. {
  63. if (g_messageservice)
  64. {
  65. ASSERT(0 <= id.ToScalar() && id.ToScalar() < g_messageservice->m_bucket.count());
  66. MessageService& g = *g_messageservice;
  67. array<MessageList>& bucket = g.m_bucket[id.ToScalar()];
  68. bucket << MessageList(time(nullptr), String(message));
  69. return true;
  70. }
  71. return false;
  72. }
  73. //----
  74. bool MessageService::FetchFirst(MessageBucket id, String& message)
  75. {
  76. if (g_messageservice)
  77. {
  78. ASSERT(0 <= id.ToScalar() && id.ToScalar() < g_messageservice->m_bucket.count());
  79. time_t timestamp;
  80. return g_messageservice->FetchFirst(id, message, timestamp);
  81. }
  82. return false;
  83. }
  84. bool MessageService::FetchFirst(MessageBucket id, String& message, time_t& timestamp)
  85. {
  86. if (g_messageservice)
  87. {
  88. ASSERT(0 <= id.ToScalar() && id.ToScalar() < g_messageservice->m_bucket.count());
  89. MessageService& g = *g_messageservice;
  90. array<MessageList>& bucket = g.m_bucket[id.ToScalar()];
  91. if (bucket.count())
  92. {
  93. message = bucket[0].m_message;
  94. timestamp = bucket[0].m_timestamp;
  95. bucket.remove(0);
  96. return true;
  97. }
  98. }
  99. return false;
  100. }
  101. //----
  102. bool MessageService::FetchAll(MessageBucket id, String& message)
  103. {
  104. if (g_messageservice)
  105. {
  106. ASSERT(0 <= id.ToScalar() && id.ToScalar() < g_messageservice->m_bucket.count());
  107. time_t timestamp;
  108. return g_messageservice->FetchAll(id, message, timestamp);
  109. }
  110. return false;
  111. }
  112. bool MessageService::FetchAll(MessageBucket id, String& message, time_t& first_timestamp)
  113. {
  114. if (g_messageservice)
  115. {
  116. ASSERT(0 <= id.ToScalar() && id.ToScalar() < g_messageservice->m_bucket.count());
  117. MessageService& g = *g_messageservice;
  118. array<MessageList>& bucket = g.m_bucket[id.ToScalar()];
  119. message = String("");
  120. if (bucket.count())
  121. {
  122. first_timestamp = bucket[0].m_timestamp;
  123. for (int i = 0; i < bucket.count(); ++i)
  124. message += bucket[i].m_message;
  125. bucket.empty();
  126. return true;
  127. }
  128. }
  129. return false;
  130. }
  131. } /* namespace lol */