Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

154 Zeilen
3.8 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. #if defined HAVE_CONFIG_H
  12. # include "config.h"
  13. #endif
  14. #include <cstring>
  15. #include <cstdlib>
  16. #include <time.h>
  17. #include "core.h"
  18. namespace lol
  19. {
  20. // This is needed to map MessageService::Send() to the JS when building the HTML.
  21. #if EMSCRIPTEN
  22. extern "C"
  23. {
  24. int C_Send(const char* message) { return (int)MessageService::Send(MessageBucket::AppIn, message); }
  25. //NOT IMPLEMENTED
  26. //bool C_FetchFirst(String& message);
  27. }
  28. #endif //EMSCRIPTEN
  29. /*
  30. * The global g_renderer object, initialised by Video::Init
  31. */
  32. MessageService *g_messageservice = nullptr;
  33. //-----------------------------------------------------------------------------
  34. // Ctor/Dtor
  35. //----
  36. MessageService::MessageService()
  37. {
  38. }
  39. MessageService::~MessageService()
  40. {
  41. m_bucket.Empty();
  42. }
  43. //Setup/Destroy
  44. void MessageService::Setup()
  45. {
  46. g_messageservice = new MessageService();
  47. g_messageservice->m_bucket.Resize(MessageBucket::Max);
  48. }
  49. void MessageService::Destroy()
  50. {
  51. delete g_messageservice;
  52. g_messageservice = nullptr;
  53. }
  54. //-----------------------------------------------------------------------------
  55. bool MessageService::Send(MessageBucket id, const String& message)
  56. {
  57. if (g_messageservice)
  58. {
  59. ASSERT(0 <= id && id < g_messageservice->m_bucket.Count());
  60. return g_messageservice->Send(id, message.C());
  61. }
  62. return false;
  63. }
  64. bool MessageService::Send(MessageBucket id, const char* message)
  65. {
  66. if (g_messageservice)
  67. {
  68. ASSERT(0 <= id && id < g_messageservice->m_bucket.Count());
  69. MessageService& g = *g_messageservice;
  70. Array<MessageList>& bucket = g.m_bucket[id];
  71. bucket << MessageList(time(nullptr), String(message));
  72. return true;
  73. }
  74. return false;
  75. }
  76. //----
  77. bool MessageService::FetchFirst(MessageBucket id, String& message)
  78. {
  79. if (g_messageservice)
  80. {
  81. ASSERT(0 <= id && id < g_messageservice->m_bucket.Count());
  82. time_t timestamp;
  83. return g_messageservice->FetchFirst(id, message, timestamp);
  84. }
  85. return false;
  86. }
  87. bool MessageService::FetchFirst(MessageBucket id, String& message, time_t& timestamp)
  88. {
  89. if (g_messageservice)
  90. {
  91. ASSERT(0 <= id && id < g_messageservice->m_bucket.Count());
  92. MessageService& g = *g_messageservice;
  93. Array<MessageList>& bucket = g.m_bucket[id];
  94. if (bucket.Count())
  95. {
  96. message = bucket[0].m_message;
  97. timestamp = bucket[0].m_timestamp;
  98. bucket.Remove(0);
  99. return true;
  100. }
  101. }
  102. return false;
  103. }
  104. //----
  105. bool MessageService::FetchAll(MessageBucket id, String& message)
  106. {
  107. if (g_messageservice)
  108. {
  109. ASSERT(0 <= id && id < g_messageservice->m_bucket.Count());
  110. time_t timestamp;
  111. return g_messageservice->FetchAll(id, message, timestamp);
  112. }
  113. return false;
  114. }
  115. bool MessageService::FetchAll(MessageBucket id, String& message, time_t& first_timestamp)
  116. {
  117. if (g_messageservice)
  118. {
  119. ASSERT(0 <= id && id < g_messageservice->m_bucket.Count());
  120. MessageService& g = *g_messageservice;
  121. Array<MessageList>& bucket = g.m_bucket[id];
  122. message = String("");
  123. if (bucket.Count())
  124. {
  125. first_timestamp = bucket[0].m_timestamp;
  126. for (int i = 0; i < bucket.Count(); ++i)
  127. message += bucket[i].m_message;
  128. bucket.Empty();
  129. return true;
  130. }
  131. }
  132. return false;
  133. }
  134. } /* namespace lol */