LightShareModule.cs 12 KB

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