1
0

LightShareModule.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 (client == null)
  167. return;
  168. if (m_enableWindlight)
  169. {
  170. if (m_scene.RegionInfo.WindlightSettings.valid)
  171. {
  172. List<byte[]> param = compileWindlightSettings(wl);
  173. client.SendGenericMessage("Windlight", UUID.Random(), param);
  174. }
  175. else
  176. {
  177. List<byte[]> param = new List<byte[]>();
  178. client.SendGenericMessage("WindlightReset", UUID.Random(), param);
  179. }
  180. }
  181. }
  182. private void EventManager_OnMakeRootAgent(ScenePresence presence)
  183. {
  184. if (m_enableWindlight && m_scene.RegionInfo.WindlightSettings.valid)
  185. m_log.Debug("[WINDLIGHT]: Sending windlight scene to new client");
  186. SendProfileToClient(presence.ControllingClient);
  187. }
  188. private void EventManager_OnSendNewWindlightProfileTargeted(RegionLightShareData wl, UUID pUUID)
  189. {
  190. IClientAPI client;
  191. m_scene.TryGetClient(pUUID, out client);
  192. SendProfileToClient(client, wl);
  193. }
  194. private void EventManager_OnSaveNewWindlightProfile()
  195. {
  196. m_scene.ForEachRootClient(SendProfileToClient);
  197. }
  198. #endregion
  199. #region ICommandableModule Members
  200. private void InstallCommands()
  201. {
  202. Command wlload = new Command("load", CommandIntentions.COMMAND_NON_HAZARDOUS, HandleLoad, "Load windlight profile from the database and broadcast");
  203. Command wlenable = new Command("enable", CommandIntentions.COMMAND_NON_HAZARDOUS, HandleEnable, "Enable the windlight plugin");
  204. Command wldisable = new Command("disable", CommandIntentions.COMMAND_NON_HAZARDOUS, HandleDisable, "Disable the windlight plugin");
  205. m_commander.RegisterCommand("load", wlload);
  206. m_commander.RegisterCommand("enable", wlenable);
  207. m_commander.RegisterCommand("disable", wldisable);
  208. m_scene.RegisterModuleCommander(m_commander);
  209. }
  210. private void HandleLoad(Object[] args)
  211. {
  212. if (!m_enableWindlight)
  213. {
  214. m_log.InfoFormat("[WINDLIGHT]: Cannot load windlight profile, module disabled. Use 'windlight enable' first.");
  215. }
  216. else
  217. {
  218. m_log.InfoFormat("[WINDLIGHT]: Loading Windlight profile from database");
  219. m_scene.LoadWindlightProfile();
  220. m_log.InfoFormat("[WINDLIGHT]: Load complete");
  221. }
  222. }
  223. private void HandleDisable(Object[] args)
  224. {
  225. m_log.InfoFormat("[WINDLIGHT]: Plugin now disabled");
  226. m_enableWindlight = false;
  227. }
  228. private void HandleEnable(Object[] args)
  229. {
  230. m_log.InfoFormat("[WINDLIGHT]: Plugin now enabled");
  231. m_enableWindlight = true;
  232. }
  233. /// <summary>
  234. /// Processes commandline input. Do not call directly.
  235. /// </summary>
  236. /// <param name="args">Commandline arguments</param>
  237. private void EventManager_OnPluginConsole(string[] args)
  238. {
  239. if (args[0] == "windlight")
  240. {
  241. if (args.Length == 1)
  242. {
  243. m_commander.ProcessConsoleCommand("add", new string[0]);
  244. return;
  245. }
  246. string[] tmpArgs = new string[args.Length - 2];
  247. int i;
  248. for (i = 2; i < args.Length; i++)
  249. {
  250. tmpArgs[i - 2] = args[i];
  251. }
  252. m_commander.ProcessConsoleCommand(args[1], tmpArgs);
  253. }
  254. }
  255. #endregion
  256. }
  257. }