29 using System.Collections.Generic;
30 using System.Reflection;
35 using System.Drawing.Imaging;
40 using OpenSim.Framework;
41 using OpenSim.Region.Framework.Interfaces;
42 using OpenSim.Region.Framework.Scenes;
43 using OpenSim.Services.Interfaces;
44 using OpenSim.Server.Base;
46 using OpenMetaverse.StructuredData;
55 [Extension(Path =
"/OpenSim/RegionModules", NodeName =
"RegionModule", Id =
"MapImageServiceModule")]
59 private static readonly ILog m_log =
60 LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
61 private static string LogHeader =
"[MAP IMAGE SERVICE MODULE]:";
63 private bool m_enabled =
false;
66 private Dictionary<UUID, Scene> m_scenes =
new Dictionary<UUID, Scene>();
68 private int m_refreshtime = 0;
69 private int m_lastrefresh = 0;
70 private System.Timers.Timer m_refreshTimer;
72 #region ISharedRegionModule
74 public Type ReplaceableInterface {
get {
return null; } }
75 public string Name {
get {
return "MapImageServiceModule"; } }
85 IConfig moduleConfig = source.Configs[
"Modules"];
86 if (moduleConfig != null)
88 string name = moduleConfig.GetString(
"MapImageService",
"");
93 IConfig config = source.Configs[
"MapImageService"];
97 int refreshminutes = Convert.ToInt32(config.GetString(
"RefreshTime"));
98 if (refreshminutes < 0)
100 m_log.WarnFormat(
"[MAP IMAGE SERVICE MODULE]: Negative refresh time given in config. Module disabled.");
104 string service = config.GetString(
"LocalServiceModule", string.Empty);
105 if (service ==
string.Empty)
107 m_log.WarnFormat(
"[MAP IMAGE SERVICE MODULE]: No service dll given in config. Unable to proceed.");
113 if (m_MapService == null)
115 m_log.WarnFormat(
"[MAP IMAGE SERVICE MODULE]: Unable to load LocalServiceModule from {0}. MapService module disabled. Please fix the configuration.", service);
120 if(refreshminutes > 0)
122 m_refreshtime = refreshminutes * 60 * 1000;
124 m_refreshTimer =
new System.Timers.Timer();
125 m_refreshTimer.Enabled =
true;
126 m_refreshTimer.AutoReset =
true;
127 m_refreshTimer.Interval = m_refreshtime;
128 m_refreshTimer.Elapsed +=
new ElapsedEventHandler(HandleMaptileRefresh);
131 m_log.InfoFormat(
"[MAP IMAGE SERVICE MODULE]: enabled with refresh time {0} min and service object {1}",
132 refreshminutes, service);
136 m_log.InfoFormat(
"[MAP IMAGE SERVICE MODULE]: enabled with no refresh and service object {0}", service);
152 m_scenes[scene.RegionInfo.RegionID] = scene;
170 m_scenes.Remove(scene.RegionInfo.RegionID);
173 #endregion ISharedRegionModule
178 private void HandleMaptileRefresh(
object sender, EventArgs ea)
183 if (m_lastrefresh > 0 && Util.EnvironmentTickCountSubtract(m_lastrefresh) < m_refreshtime)
186 m_log.DebugFormat(
"[MAP IMAGE SERVICE MODULE]: map refresh!");
189 foreach (
IScene scene
in m_scenes.Values)
193 UploadMapTile(scene);
197 m_log.WarnFormat(
"[MAP IMAGE SERVICE MODULE]: something bad happened {0}", ex.Message);
202 m_lastrefresh = Util.EnvironmentTickCount();
209 m_log.WarnFormat(
"{0} Cannot upload null image", LogHeader);
213 m_log.DebugFormat(
"{0} Upload maptile for {1}", LogHeader, scene.Name);
221 ConvertAndUploadMaptile(scene, mapTile,
230 for (uint xx = 0; xx < mapTile.Width; xx += Constants.RegionSize)
232 for (uint yy = 0; yy < mapTile.Height; yy += Constants.RegionSize)
237 Rectangle rect =
new Rectangle(
241 using (Bitmap subMapTile = mapTile.Clone(rect, mapTile.PixelFormat))
243 ConvertAndUploadMaptile(scene, subMapTile,
258 m_log.DebugFormat(
"{0}: upload maptile for {1}", LogHeader, scene.RegionInfo.RegionName);
262 if (tileGenerator == null)
264 m_log.WarnFormat(
"{0} Cannot upload map tile without an ImageGenerator", LogHeader);
268 using (Bitmap mapTile = tileGenerator.CreateMapTile())
275 UploadMapTile(scene, mapTile);
279 private void ConvertAndUploadMaptile(
IScene scene, Image tileImage, uint locX, uint locY,
string regionName)
281 byte[] jpgData = Utils.EmptyBytes;
283 using (MemoryStream stream =
new MemoryStream())
285 tileImage.Save(stream, ImageFormat.Jpeg);
286 jpgData = stream.ToArray();
288 if (jpgData !=
Utils.EmptyBytes)
290 string reason = string.Empty;
291 if (!m_MapService.AddMapTile((
int)locX, (
int)locY, jpgData, scene.
RegionInfo.
ScopeID, out reason))
293 m_log.DebugFormat(
"{0} Unable to upload tile image for {1} at {2}-{3}: {4}", LogHeader,
294 regionName, locX, locY, reason);
299 m_log.WarnFormat(
"{0} Tile image generation failed for region {1}", LogHeader, regionName);
void RemoveRegion(Scene scene)
OpenSim.Server.Handlers.Simulation.Utils Utils
void AddRegion(Scene scene)
string Name
The name of this scene.
void UploadMapTile(IScene scene, Bitmap mapTile)
Interactive OpenSim region server
void Initialise(IConfigSource source)
void UploadMapTile(IScene scene)
Upload a new maptile
void RegionLoaded(Scene scene)
This will be called once for every scene loaded. In a shared module this will be multiple times in on...
void Close()
This is the inverse to Initialise. After a Close(), this instance won't be usable anymore...
uint RegionLocY
The y co-ordinate of this region in map tiles (e.g. 1000). Coordinate is scaled as world coordinates ...
uint RegionLocX
The x co-ordinate of this region in map tiles (e.g. 1000). Coordinate is scaled as world coordinates ...