DynamicTextureModule.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 OpenSim 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. */
  28. using System;
  29. using System.Collections.Generic;
  30. using libsecondlife;
  31. using Nini.Config;
  32. using OpenSim.Framework;
  33. using OpenSim.Region.Environment.Interfaces;
  34. using OpenSim.Region.Environment.Scenes;
  35. namespace OpenSim.Region.Environment.Modules
  36. {
  37. public class DynamicTextureModule : IRegionModule, IDynamicTextureManager
  38. {
  39. private Dictionary<LLUUID, Scene> RegisteredScenes = new Dictionary<LLUUID, Scene>();
  40. private Dictionary<string, IDynamicTextureRender> RenderPlugins =
  41. new Dictionary<string, IDynamicTextureRender>();
  42. private Dictionary<LLUUID, DynamicTextureUpdater> Updaters = new Dictionary<LLUUID, DynamicTextureUpdater>();
  43. public void Initialise(Scene scene, IConfigSource config)
  44. {
  45. if (!RegisteredScenes.ContainsKey(scene.RegionInfo.RegionID))
  46. {
  47. RegisteredScenes.Add(scene.RegionInfo.RegionID, scene);
  48. scene.RegisterModuleInterface<IDynamicTextureManager>(this);
  49. }
  50. }
  51. public void PostInitialise()
  52. {
  53. }
  54. public void Close()
  55. {
  56. }
  57. public string Name
  58. {
  59. get { return "DynamicTextureModule"; }
  60. }
  61. public bool IsSharedModule
  62. {
  63. get { return true; }
  64. }
  65. public void RegisterRender(string handleType, IDynamicTextureRender render)
  66. {
  67. if (!RenderPlugins.ContainsKey(handleType))
  68. {
  69. RenderPlugins.Add(handleType, render);
  70. }
  71. }
  72. public void ReturnData(LLUUID id, byte[] data)
  73. {
  74. if (Updaters.ContainsKey(id))
  75. {
  76. DynamicTextureUpdater updater = Updaters[id];
  77. if (RegisteredScenes.ContainsKey(updater.SimUUID))
  78. {
  79. Scene scene = RegisteredScenes[updater.SimUUID];
  80. updater.DataReceived(data, scene);
  81. }
  82. }
  83. }
  84. public LLUUID AddDynamicTextureURL(LLUUID simID, LLUUID primID, string contentType, string url,
  85. string extraParams, int updateTimer)
  86. {
  87. if (RenderPlugins.ContainsKey(contentType))
  88. {
  89. //Console.WriteLine("dynamic texture being created: " + url + " of type " + contentType);
  90. DynamicTextureUpdater updater = new DynamicTextureUpdater();
  91. updater.SimUUID = simID;
  92. updater.PrimID = primID;
  93. updater.ContentType = contentType;
  94. updater.Url = url;
  95. updater.UpdateTimer = updateTimer;
  96. updater.UpdaterID = LLUUID.Random();
  97. updater.Params = extraParams;
  98. if (!Updaters.ContainsKey(updater.UpdaterID))
  99. {
  100. Updaters.Add(updater.UpdaterID, updater);
  101. }
  102. RenderPlugins[contentType].AsyncConvertUrl(updater.UpdaterID, url, extraParams);
  103. return updater.UpdaterID;
  104. }
  105. return LLUUID.Zero;
  106. }
  107. public LLUUID AddDynamicTextureData(LLUUID simID, LLUUID primID, string contentType, string data,
  108. string extraParams, int updateTimer)
  109. {
  110. if (RenderPlugins.ContainsKey(contentType))
  111. {
  112. DynamicTextureUpdater updater = new DynamicTextureUpdater();
  113. updater.SimUUID = simID;
  114. updater.PrimID = primID;
  115. updater.ContentType = contentType;
  116. updater.BodyData = data;
  117. updater.UpdateTimer = updateTimer;
  118. updater.UpdaterID = LLUUID.Random();
  119. updater.Params = extraParams;
  120. if (!Updaters.ContainsKey(updater.UpdaterID))
  121. {
  122. Updaters.Add(updater.UpdaterID, updater);
  123. }
  124. RenderPlugins[contentType].AsyncConvertData(updater.UpdaterID, data, extraParams);
  125. return updater.UpdaterID;
  126. }
  127. return LLUUID.Zero;
  128. }
  129. public class DynamicTextureUpdater
  130. {
  131. public LLUUID SimUUID;
  132. public LLUUID UpdaterID;
  133. public string ContentType;
  134. public string Url;
  135. public string BodyData;
  136. public LLUUID PrimID;
  137. public int UpdateTimer;
  138. public LLUUID LastAssetID;
  139. public string Params;
  140. public DynamicTextureUpdater()
  141. {
  142. LastAssetID = LLUUID.Zero;
  143. UpdateTimer = 0;
  144. BodyData = null;
  145. }
  146. public void DataReceived(byte[] data, Scene scene)
  147. {
  148. //TODO delete the last asset(data), if it was a dynamic texture
  149. byte[] assetData = new byte[data.Length];
  150. Array.Copy(data, assetData, data.Length);
  151. AssetBase asset = new AssetBase();
  152. asset.FullID = LLUUID.Random();
  153. asset.Data = assetData;
  154. asset.Name = "DynamicImage" + Util.RandomClass.Next(1, 10000);
  155. asset.Type = 0;
  156. asset.Description = "dynamic image";
  157. asset.Local = false;
  158. asset.Temporary = true;
  159. scene.AssetCache.AddAsset(asset);
  160. LastAssetID = asset.FullID;
  161. SceneObjectPart part = scene.GetSceneObjectPart(PrimID);
  162. part.Shape.Textures = new LLObject.TextureEntry(asset.FullID);
  163. part.ScheduleFullUpdate();
  164. }
  165. }
  166. }
  167. }