ScriptTimer.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using OpenMetaverse;
  30. namespace OpenSim.Region.ScriptEngine.Shared.Api.Plugins
  31. {
  32. public class ScriptTimer
  33. {
  34. public class TimerInfo
  35. {
  36. public UUID itemID;
  37. public uint localID;
  38. public long interval;
  39. public long next;
  40. public TimerInfo Clone()
  41. {
  42. return (TimerInfo)this.MemberwiseClone();
  43. }
  44. }
  45. public AsyncCommandManager m_CmdManager;
  46. public int TimersCount
  47. {
  48. get
  49. {
  50. lock (TimerListLock)
  51. return Timers.Count;
  52. }
  53. }
  54. public ScriptTimer(AsyncCommandManager CmdManager)
  55. {
  56. m_CmdManager = CmdManager;
  57. }
  58. //
  59. // TIMER
  60. //
  61. private static string MakeTimerKey(uint localID, UUID itemID)
  62. {
  63. return localID.ToString() + itemID.ToString();
  64. }
  65. private readonly Dictionary<string,TimerInfo> Timers = new();
  66. private readonly object TimerListLock = new();
  67. private List<TimerInfo> TimersCache = null;
  68. public void SetTimerEvent(uint _localID, UUID _itemID, double sec)
  69. {
  70. if (sec == 0.0) // Disabling timer
  71. {
  72. UnSetTimerEvents(_localID, _itemID);
  73. return;
  74. }
  75. string key = MakeTimerKey(_localID, _itemID);
  76. long intervalTicks = (long)(sec * TimeSpan.TicksPerSecond);
  77. lock (TimerListLock)
  78. {
  79. if (Timers.TryGetValue(key, out TimerInfo ts))
  80. {
  81. ts.interval = intervalTicks;
  82. ts.next = DateTime.UtcNow.Ticks + ts.interval;
  83. }
  84. else
  85. {
  86. ts = new TimerInfo()
  87. {
  88. localID = _localID,
  89. itemID = _itemID,
  90. interval = intervalTicks,
  91. next = DateTime.UtcNow.Ticks + intervalTicks
  92. };
  93. Timers[key] = ts;
  94. TimersCache = null;
  95. }
  96. }
  97. }
  98. public void UnSetTimerEvents(uint m_localID, UUID m_itemID)
  99. {
  100. // Remove from timer
  101. string key = MakeTimerKey(m_localID, m_itemID);
  102. lock (TimerListLock)
  103. {
  104. if (Timers.Remove(key, out TimerInfo ts))
  105. {
  106. m_CmdManager.m_ScriptEngine.CancelScriptEvent(ts.itemID, "timer");
  107. TimersCache = null;
  108. }
  109. }
  110. }
  111. public void CheckTimerEvents()
  112. {
  113. List<TimerInfo> tvals;
  114. lock (TimerListLock)
  115. {
  116. if (Timers.Count == 0)
  117. return;
  118. tvals = TimersCache ?? new List<TimerInfo>(Timers.Values);
  119. }
  120. long now = DateTime.UtcNow.Ticks;
  121. foreach (TimerInfo ts in tvals)
  122. {
  123. // Time has passed?
  124. if (ts.next <= now)
  125. {
  126. // Add it to queue
  127. m_CmdManager.m_ScriptEngine.PostScriptEvent(ts.itemID,
  128. new EventParams("timer", new Object[0],
  129. new DetectParams[0]));
  130. // set next interval
  131. ts.next = now + ts.interval;
  132. }
  133. }
  134. }
  135. public Object[] GetSerializationData(UUID itemID)
  136. {
  137. List<Object> data = new();
  138. List<TimerInfo> tvals;
  139. lock (TimerListLock)
  140. {
  141. if (Timers.Count == 0)
  142. return new object[0];
  143. tvals = TimersCache ?? new List<TimerInfo>(Timers.Values);
  144. }
  145. long now = DateTime.UtcNow.Ticks;
  146. foreach (TimerInfo ts in tvals)
  147. {
  148. if (ts.itemID.Equals(itemID))
  149. {
  150. data.Add(ts.interval);
  151. data.Add(ts.next - now);
  152. }
  153. }
  154. return data.ToArray();
  155. }
  156. public void CreateFromData(uint localID, UUID itemID, UUID objectID, Object[] data)
  157. {
  158. int idx = 0;
  159. long now = DateTime.UtcNow.Ticks;
  160. while (idx < data.Length)
  161. {
  162. TimerInfo ts = new()
  163. {
  164. localID = localID,
  165. itemID = itemID,
  166. interval = (long)data[idx],
  167. next = now + (long)data[idx+1]
  168. };
  169. idx += 2;
  170. string tskey = MakeTimerKey(localID, itemID);
  171. lock (TimerListLock)
  172. {
  173. Timers.Add(tskey, ts);
  174. TimersCache = null;
  175. }
  176. }
  177. }
  178. public List<TimerInfo> GetTimersInfo()
  179. {
  180. List<TimerInfo> retList = new();
  181. List<TimerInfo> tvals;
  182. lock (TimerListLock)
  183. {
  184. if (Timers.Count == 0)
  185. return retList;
  186. tvals = TimersCache ?? new List<TimerInfo>(Timers.Values);
  187. }
  188. foreach (TimerInfo i in tvals)
  189. retList.Add(i.Clone());
  190. return retList;
  191. }
  192. }
  193. }