LightShareModule.cs 11 KB

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