1
0

LightShareModule.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 System.Reflection;
  30. using OpenMetaverse;
  31. using OpenSim.Framework;
  32. using OpenSim.Region.CoreModules.Framework.InterfaceCommander;
  33. using OpenSim.Region.Framework.Interfaces;
  34. using OpenSim.Region.Framework.Scenes;
  35. using log4net;
  36. using Nini.Config;
  37. using Mono.Addins;
  38. namespace OpenSim.Region.CoreModules.World.LightShare
  39. {
  40. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "LightShareModule")]
  41. public class LightShareModule : INonSharedRegionModule, ILightShareModule, ICommandableModule
  42. {
  43. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. private readonly Commander m_commander = new Commander("windlight");
  45. private Scene m_scene;
  46. private static bool m_enableWindlight;
  47. #region ICommandableModule Members
  48. public ICommander CommandInterface
  49. {
  50. get { return m_commander; }
  51. }
  52. #endregion
  53. #region INonSharedRegionModule Members
  54. public void Initialise(IConfigSource config)
  55. {
  56. try
  57. {
  58. m_enableWindlight = config.Configs["LightShare"].GetBoolean("enable_windlight", false);
  59. }
  60. catch (Exception)
  61. {
  62. m_log.Debug("[WINDLIGHT]: ini failure for enable_windlight - using default");
  63. }
  64. m_log.DebugFormat("[WINDLIGHT]: windlight module {0}", (m_enableWindlight ? "enabled" : "disabled"));
  65. }
  66. public void AddRegion(Scene scene)
  67. {
  68. if (!m_enableWindlight)
  69. return;
  70. m_scene = scene;
  71. m_scene.RegisterModuleInterface<ILightShareModule>(this);
  72. m_scene.EventManager.OnPluginConsole += EventManager_OnPluginConsole;
  73. m_scene.EventManager.OnMakeRootAgent += EventManager_OnMakeRootAgent;
  74. m_scene.EventManager.OnSaveNewWindlightProfile += EventManager_OnSaveNewWindlightProfile;
  75. m_scene.EventManager.OnSendNewWindlightProfileTargeted += EventManager_OnSendNewWindlightProfileTargeted;
  76. m_scene.LoadWindlightProfile();
  77. InstallCommands();
  78. }
  79. public void RemoveRegion(Scene scene)
  80. {
  81. if (!m_enableWindlight)
  82. return;
  83. m_scene.EventManager.OnPluginConsole -= EventManager_OnPluginConsole;
  84. m_scene.EventManager.OnMakeRootAgent -= EventManager_OnMakeRootAgent;
  85. m_scene.EventManager.OnSaveNewWindlightProfile -= EventManager_OnSaveNewWindlightProfile;
  86. m_scene.EventManager.OnSendNewWindlightProfileTargeted -= EventManager_OnSendNewWindlightProfileTargeted;
  87. m_scene = null;
  88. }
  89. public void Close()
  90. {
  91. }
  92. public string Name
  93. {
  94. get { return "LightShareModule"; }
  95. }
  96. public void RegionLoaded(Scene scene)
  97. {
  98. }
  99. public Type ReplaceableInterface
  100. {
  101. get { return null; }
  102. }
  103. #endregion
  104. public static bool EnableWindlight
  105. {
  106. get
  107. {
  108. return m_enableWindlight;
  109. }
  110. set
  111. {
  112. }
  113. }
  114. #region events
  115. private List<byte[]> compileWindlightSettings(RegionLightShareData wl)
  116. {
  117. byte[] mBlock = new Byte[249];
  118. int pos = 0;
  119. wl.waterColor.ToBytes(mBlock, 0); pos += 12;
  120. Utils.FloatToBytes(wl.waterFogDensityExponent).CopyTo(mBlock, pos); pos += 4;
  121. Utils.FloatToBytes(wl.underwaterFogModifier).CopyTo(mBlock, pos); pos += 4;
  122. wl.reflectionWaveletScale.ToBytes(mBlock, pos); pos += 12;
  123. Utils.FloatToBytes(wl.fresnelScale).CopyTo(mBlock, pos); pos += 4;
  124. Utils.FloatToBytes(wl.fresnelOffset).CopyTo(mBlock, pos); pos += 4;
  125. Utils.FloatToBytes(wl.refractScaleAbove).CopyTo(mBlock, pos); pos += 4;
  126. Utils.FloatToBytes(wl.refractScaleBelow).CopyTo(mBlock, pos); pos += 4;
  127. Utils.FloatToBytes(wl.blurMultiplier).CopyTo(mBlock, pos); pos += 4;
  128. wl.bigWaveDirection.ToBytes(mBlock, pos); pos += 8;
  129. wl.littleWaveDirection.ToBytes(mBlock, pos); pos += 8;
  130. wl.normalMapTexture.ToBytes(mBlock, pos); pos += 16;
  131. wl.horizon.ToBytes(mBlock, pos); pos += 16;
  132. Utils.FloatToBytes(wl.hazeHorizon).CopyTo(mBlock, pos); pos += 4;
  133. wl.blueDensity.ToBytes(mBlock, pos); pos += 16;
  134. Utils.FloatToBytes(wl.hazeDensity).CopyTo(mBlock, pos); pos += 4;
  135. Utils.FloatToBytes(wl.densityMultiplier).CopyTo(mBlock, pos); pos += 4;
  136. Utils.FloatToBytes(wl.distanceMultiplier).CopyTo(mBlock, pos); pos += 4;
  137. wl.sunMoonColor.ToBytes(mBlock, pos); pos += 16;
  138. Utils.FloatToBytes(wl.sunMoonPosition).CopyTo(mBlock, pos); pos += 4;
  139. wl.ambient.ToBytes(mBlock, pos); pos += 16;
  140. Utils.FloatToBytes(wl.eastAngle).CopyTo(mBlock, pos); pos += 4;
  141. Utils.FloatToBytes(wl.sunGlowFocus).CopyTo(mBlock, pos); pos += 4;
  142. Utils.FloatToBytes(wl.sunGlowSize).CopyTo(mBlock, pos); pos += 4;
  143. Utils.FloatToBytes(wl.sceneGamma).CopyTo(mBlock, pos); pos += 4;
  144. Utils.FloatToBytes(wl.starBrightness).CopyTo(mBlock, pos); pos += 4;
  145. wl.cloudColor.ToBytes(mBlock, pos); pos += 16;
  146. wl.cloudXYDensity.ToBytes(mBlock, pos); pos += 12;
  147. Utils.FloatToBytes(wl.cloudCoverage).CopyTo(mBlock, pos); pos += 4;
  148. Utils.FloatToBytes(wl.cloudScale).CopyTo(mBlock, pos); pos += 4;
  149. wl.cloudDetailXYDensity.ToBytes(mBlock, pos); pos += 12;
  150. Utils.FloatToBytes(wl.cloudScrollX).CopyTo(mBlock, pos); pos += 4;
  151. Utils.FloatToBytes(wl.cloudScrollY).CopyTo(mBlock, pos); pos += 4;
  152. Utils.UInt16ToBytes(wl.maxAltitude).CopyTo(mBlock, pos); pos += 2;
  153. mBlock[pos] = Convert.ToByte(wl.cloudScrollXLock); pos++;
  154. mBlock[pos] = Convert.ToByte(wl.cloudScrollYLock); pos++;
  155. mBlock[pos] = Convert.ToByte(wl.drawClassicClouds); pos++;
  156. List<byte[]> param = new List<byte[]>();
  157. param.Add(mBlock);
  158. return param;
  159. }
  160. public void SendProfileToClient(IClientAPI client)
  161. {
  162. SendProfileToClient(client, m_scene.RegionInfo.WindlightSettings);
  163. }
  164. public void SendProfileToClient(IClientAPI client, RegionLightShareData wl)
  165. {
  166. if (m_enableWindlight)
  167. {
  168. if (m_scene.RegionInfo.WindlightSettings.valid)
  169. {
  170. List<byte[]> param = compileWindlightSettings(wl);
  171. client.SendGenericMessage("Windlight", UUID.Random(), param);
  172. }
  173. else
  174. {
  175. List<byte[]> param = new List<byte[]>();
  176. client.SendGenericMessage("WindlightReset", UUID.Random(), param);
  177. }
  178. }
  179. }
  180. private void EventManager_OnMakeRootAgent(ScenePresence presence)
  181. {
  182. m_log.Debug("[WINDLIGHT]: Sending windlight scene to new client");
  183. SendProfileToClient(presence.ControllingClient);
  184. }
  185. private void EventManager_OnSendNewWindlightProfileTargeted(RegionLightShareData wl, UUID pUUID)
  186. {
  187. IClientAPI client;
  188. m_scene.TryGetClient(pUUID, out client);
  189. SendProfileToClient(client, wl);
  190. }
  191. private void EventManager_OnSaveNewWindlightProfile()
  192. {
  193. m_scene.ForEachRootClient(SendProfileToClient);
  194. }
  195. #endregion
  196. #region ICommandableModule Members
  197. private void InstallCommands()
  198. {
  199. Command wlload = new Command("load", CommandIntentions.COMMAND_NON_HAZARDOUS, HandleLoad, "Load windlight profile from the database and broadcast");
  200. Command wlenable = new Command("enable", CommandIntentions.COMMAND_NON_HAZARDOUS, HandleEnable, "Enable the windlight plugin");
  201. Command wldisable = new Command("disable", CommandIntentions.COMMAND_NON_HAZARDOUS, HandleDisable, "Disable the windlight plugin");
  202. m_commander.RegisterCommand("load", wlload);
  203. m_commander.RegisterCommand("enable", wlenable);
  204. m_commander.RegisterCommand("disable", wldisable);
  205. m_scene.RegisterModuleCommander(m_commander);
  206. }
  207. private void HandleLoad(Object[] args)
  208. {
  209. if (!m_enableWindlight)
  210. {
  211. m_log.InfoFormat("[WINDLIGHT]: Cannot load windlight profile, module disabled. Use 'windlight enable' first.");
  212. }
  213. else
  214. {
  215. m_log.InfoFormat("[WINDLIGHT]: Loading Windlight profile from database");
  216. m_scene.LoadWindlightProfile();
  217. m_log.InfoFormat("[WINDLIGHT]: Load complete");
  218. }
  219. }
  220. private void HandleDisable(Object[] args)
  221. {
  222. m_log.InfoFormat("[WINDLIGHT]: Plugin now disabled");
  223. m_enableWindlight = false;
  224. }
  225. private void HandleEnable(Object[] args)
  226. {
  227. m_log.InfoFormat("[WINDLIGHT]: Plugin now enabled");
  228. m_enableWindlight = true;
  229. }
  230. /// <summary>
  231. /// Processes commandline input. Do not call directly.
  232. /// </summary>
  233. /// <param name="args">Commandline arguments</param>
  234. private void EventManager_OnPluginConsole(string[] args)
  235. {
  236. if (args[0] == "windlight")
  237. {
  238. if (args.Length == 1)
  239. {
  240. m_commander.ProcessConsoleCommand("add", new string[0]);
  241. return;
  242. }
  243. string[] tmpArgs = new string[args.Length - 2];
  244. int i;
  245. for (i = 2; i < args.Length; i++)
  246. {
  247. tmpArgs[i - 2] = args[i];
  248. }
  249. m_commander.ProcessConsoleCommand(args[1], tmpArgs);
  250. }
  251. }
  252. #endregion
  253. }
  254. }