OpenSimMirror/GridManager.cs

373 lines
12 KiB
C#
Raw Permalink Normal View History

2007-02-02 17:24:44 +00:00
/*
2007-02-03 19:51:52 +00:00
Copyright (c) OpenSim project, http://sim.opensecondlife.org/
2007-02-02 17:24:44 +00:00
* Copyright (c) <year>, <copyright holder>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY <copyright holder> ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using libsecondlife;
using System.Collections;
using libsecondlife.Packets;
using libsecondlife.AssetSystem;
using System.IO;
using System.Xml;
namespace OpenSim
{
/// <summary>
/// Description of GridManager.
/// </summary>
public class GridManager
{
2007-02-04 11:36:35 +00:00
private Server _server;
private System.Text.Encoding _enc = System.Text.Encoding.ASCII;
private AgentManager _agentManager;
public Dictionary<ulong,RegionInfo> Grid;
2007-02-02 17:24:44 +00:00
/// <summary>
///
/// </summary>
/// <param name="serve"></param>
/// <param name="agentManager"></param>
2007-02-04 11:36:35 +00:00
public GridManager(Server server, AgentManager agentManager)
2007-02-02 17:24:44 +00:00
{
2007-02-04 11:36:35 +00:00
Grid = new Dictionary<ulong, RegionInfo>();
_server = server;
_agentManager = agentManager;
2007-02-02 17:24:44 +00:00
LoadGrid();
}
/// <summary>
///
/// </summary>
/// <param name="UserInfo"></param>
2007-02-04 11:36:35 +00:00
public void RequestMapLayer(UserAgentInfo userInfo)
2007-02-02 17:24:44 +00:00
{
//send a layer covering the 800,800 - 1200,1200 area
2007-02-04 11:36:35 +00:00
MapLayerReplyPacket MapReply = new MapLayerReplyPacket();
MapReply.AgentData.AgentID = userInfo.AgentID;
MapReply.AgentData.Flags = 0;
MapReply.LayerData = new MapLayerReplyPacket.LayerDataBlock[1];
MapReply.LayerData[0] = new MapLayerReplyPacket.LayerDataBlock();
MapReply.LayerData[0].Bottom = 800;
MapReply.LayerData[0].Left = 800;
MapReply.LayerData[0].Top = 1200;
MapReply.LayerData[0].Right = 1200;
MapReply.LayerData[0].ImageID = new LLUUID("00000000-0000-0000-7007-000000000006");
_server.SendPacket(MapReply, true, userInfo);
2007-02-02 17:24:44 +00:00
}
/// <summary>
///
/// </summary>
/// <param name="UserInfo"></param>
/// <param name="MinX"></param>
/// <param name="MinY"></param>
/// <param name="MaxX"></param>
/// <param name="MaxY"></param>
2007-02-04 11:36:35 +00:00
public void RequestMapBlock(UserAgentInfo userInfo, int minX, int minY,int maxX,int maxY)
2007-02-02 17:24:44 +00:00
{
2007-02-04 11:36:35 +00:00
foreach (KeyValuePair<ulong, RegionInfo> RegionPair in this.Grid)
2007-02-02 17:24:44 +00:00
{
//check Region is inside the requested area
2007-02-04 11:36:35 +00:00
RegionInfo Region = RegionPair.Value;
if(((Region.X > minX) && (Region.X < maxX)) && ((Region.Y > minY) && (Region.Y < maxY)))
2007-02-02 17:24:44 +00:00
{
2007-02-04 11:36:35 +00:00
MapBlockReplyPacket MapReply = new MapBlockReplyPacket();
MapReply.AgentData.AgentID = userInfo.AgentID;
MapReply.AgentData.Flags = 0;
MapReply.Data = new MapBlockReplyPacket.DataBlock[1];
MapReply.Data[0] = new MapBlockReplyPacket.DataBlock();
MapReply.Data[0].MapImageID = Region.ImageID;
MapReply.Data[0].X = Region.X;
MapReply.Data[0].Y = Region.Y;
MapReply.Data[0].WaterHeight = Region.WaterHeight;
MapReply.Data[0].Name = _enc.GetBytes( Region.Name);
MapReply.Data[0].RegionFlags = 72458694;
MapReply.Data[0].Access = 13;
MapReply.Data[0].Agents = 1;
_server.SendPacket(MapReply, true, userInfo);
2007-02-02 17:24:44 +00:00
}
}
}
/// <summary>
///
/// </summary>
/// <param name="UserInfo"></param>
/// <param name="Request"></param>
2007-02-04 11:36:35 +00:00
public void RequestTeleport(UserAgentInfo userInfo, TeleportLocationRequestPacket request)
2007-02-02 17:24:44 +00:00
{
2007-02-04 11:36:35 +00:00
if(Grid.ContainsKey(request.Info.RegionHandle))
2007-02-02 17:24:44 +00:00
{
2007-02-04 11:36:35 +00:00
RegionInfo Region = Grid[request.Info.RegionHandle];
libsecondlife.Packets.TeleportStartPacket TeleportStart = new TeleportStartPacket();
TeleportStart.Info.TeleportFlags = 16;
_server.SendPacket(TeleportStart, true, userInfo);
2007-02-02 17:24:44 +00:00
2007-02-04 11:36:35 +00:00
libsecondlife.Packets.TeleportFinishPacket Teleport = new TeleportFinishPacket();
Teleport.Info.AgentID = userInfo.AgentID;
Teleport.Info.RegionHandle = request.Info.RegionHandle;
Teleport.Info.SimAccess = 13;
Teleport.Info.SeedCapability = new byte[0];
2007-02-02 17:24:44 +00:00
2007-02-04 11:36:35 +00:00
System.Net.IPAddress oIP = System.Net.IPAddress.Parse(Region.IPAddress.Address);
byte[] byteIP = oIP.GetAddressBytes();
2007-02-02 17:24:44 +00:00
uint ip=(uint)byteIP[3]<<24;
ip+=(uint)byteIP[2]<<16;
ip+=(uint)byteIP[1]<<8;
ip+=(uint)byteIP[0];
2007-02-04 11:36:35 +00:00
Teleport.Info.SimIP = ip;
Teleport.Info.SimPort = Region.IPAddress.Port;
Teleport.Info.LocationID = 4;
Teleport.Info.TeleportFlags = 1 << 4;;
_server.SendPacket(Teleport, true, userInfo);
2007-02-02 17:24:44 +00:00
2007-02-04 11:36:35 +00:00
this._agentManager.RemoveAgent(userInfo);
2007-02-02 17:24:44 +00:00
}
}
/// <summary>
///
/// </summary>
private void LoadGrid()
{
//should connect to a space server to see what grids there are
//but for now we read static xml files
2007-02-04 11:36:35 +00:00
ulong CurrentHandle = 0;
bool Login = true;
2007-02-02 17:24:44 +00:00
XmlDocument doc = new XmlDocument();
try {
2007-02-04 11:36:35 +00:00
doc.Load(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Grid.ini" ));
2007-02-02 17:24:44 +00:00
}
catch ( Exception e)
{
Console.WriteLine(e.Message);
return;
}
try
{
XmlNode root = doc.FirstChild;
if (root.Name != "Root")
throw new Exception("Error: Invalid File. Missing <Root>");
XmlNode nodes = root.FirstChild;
if (nodes.Name != "Grid")
throw new Exception("Error: Invalid File. <project> first child should be <Grid>");
if (nodes.HasChildNodes) {
foreach( XmlNode xmlnc in nodes.ChildNodes)
{
2007-02-04 11:36:35 +00:00
if(xmlnc.Name == "Region")
2007-02-02 17:24:44 +00:00
{
string xmlAttri;
2007-02-04 11:36:35 +00:00
RegionInfo Region = new RegionInfo();
if(xmlnc.Attributes["Name"] != null)
2007-02-02 17:24:44 +00:00
{
2007-02-04 11:36:35 +00:00
xmlAttri = ((XmlAttribute)xmlnc.Attributes.GetNamedItem("Name")).Value;
Region.Name = xmlAttri+" \0";
2007-02-02 17:24:44 +00:00
}
2007-02-04 11:36:35 +00:00
if(xmlnc.Attributes["ImageID"] != null)
2007-02-02 17:24:44 +00:00
{
2007-02-04 11:36:35 +00:00
xmlAttri = ((XmlAttribute)xmlnc.Attributes.GetNamedItem("ImageID")).Value;
Region.ImageID = new LLUUID(xmlAttri);
2007-02-02 17:24:44 +00:00
}
2007-02-04 11:36:35 +00:00
if(xmlnc.Attributes["IP_Address"] != null)
2007-02-02 17:24:44 +00:00
{
2007-02-04 11:36:35 +00:00
xmlAttri = ((XmlAttribute)xmlnc.Attributes.GetNamedItem("IP_Address")).Value;
Region.IPAddress.Address = xmlAttri;
2007-02-02 17:24:44 +00:00
}
2007-02-04 11:36:35 +00:00
if(xmlnc.Attributes["IP_Port"] != null)
2007-02-02 17:24:44 +00:00
{
2007-02-04 11:36:35 +00:00
xmlAttri = ((XmlAttribute)xmlnc.Attributes.GetNamedItem("IP_Port")).Value;
Region.IPAddress.Port = Convert.ToUInt16(xmlAttri);
2007-02-02 17:24:44 +00:00
}
2007-02-04 11:36:35 +00:00
if(xmlnc.Attributes["Location_X"] != null)
2007-02-02 17:24:44 +00:00
{
2007-02-04 11:36:35 +00:00
xmlAttri = ((XmlAttribute)xmlnc.Attributes.GetNamedItem("Location_X")).Value;
Region.X = Convert.ToUInt16(xmlAttri);
2007-02-02 17:24:44 +00:00
}
2007-02-04 11:36:35 +00:00
if(xmlnc.Attributes["Location_Y"] != null)
2007-02-02 17:24:44 +00:00
{
2007-02-04 11:36:35 +00:00
xmlAttri = ((XmlAttribute)xmlnc.Attributes.GetNamedItem("Location_Y")).Value;
Region.Y = Convert.ToUInt16(xmlAttri);
2007-02-02 17:24:44 +00:00
}
2007-02-04 11:36:35 +00:00
this.Grid.Add(Region.Handle, Region);
2007-02-02 17:24:44 +00:00
}
2007-02-04 11:36:35 +00:00
if(xmlnc.Name == "CurrentRegion")
2007-02-02 17:24:44 +00:00
{
string xmlAttri;
2007-02-04 11:36:35 +00:00
uint Rx = 0, Ry = 0;
if(xmlnc.Attributes["RegionHandle"] != null)
2007-02-02 17:24:44 +00:00
{
2007-02-04 11:36:35 +00:00
xmlAttri = ((XmlAttribute)xmlnc.Attributes.GetNamedItem("RegionHandle")).Value;
CurrentHandle = Convert.ToUInt64(xmlAttri);
2007-02-02 17:24:44 +00:00
}
else
{
2007-02-04 11:36:35 +00:00
if(xmlnc.Attributes["Region_X"] != null)
2007-02-02 17:24:44 +00:00
{
2007-02-04 11:36:35 +00:00
xmlAttri = ((XmlAttribute)xmlnc.Attributes.GetNamedItem("Region_X")).Value;
Rx = Convert.ToUInt32(xmlAttri);
2007-02-02 17:24:44 +00:00
}
2007-02-04 11:36:35 +00:00
if(xmlnc.Attributes["Region_Y"] != null)
2007-02-02 17:24:44 +00:00
{
2007-02-04 11:36:35 +00:00
xmlAttri = ((XmlAttribute)xmlnc.Attributes.GetNamedItem("Region_Y")).Value;
Ry = Convert.ToUInt32(xmlAttri);
2007-02-02 17:24:44 +00:00
}
}
2007-02-04 11:36:35 +00:00
if(xmlnc.Attributes["LoginServer"] != null)
2007-02-02 17:24:44 +00:00
{
2007-02-04 11:36:35 +00:00
xmlAttri = ((XmlAttribute)xmlnc.Attributes.GetNamedItem("LoginServer")).Value;
Login = Convert.ToBoolean(xmlAttri);
2007-02-02 17:24:44 +00:00
}
2007-02-04 11:36:35 +00:00
if(CurrentHandle == 0)
2007-02-02 17:24:44 +00:00
{
//no RegionHandle set
//so check for Region X and Y
2007-02-04 11:36:35 +00:00
if((Rx > 0) && (Ry > 0))
2007-02-02 17:24:44 +00:00
{
2007-02-04 11:36:35 +00:00
CurrentHandle = Helpers.UIntsToLong((Rx*256), (Ry*256));
2007-02-02 17:24:44 +00:00
}
else
{
//seems to be no Region location set
// so set default
2007-02-04 11:36:35 +00:00
CurrentHandle = 1096213093147648;
2007-02-02 17:24:44 +00:00
}
}
}
}
//finished loading grid, now set Globals to current region
2007-02-04 11:36:35 +00:00
if(CurrentHandle != 0)
2007-02-02 17:24:44 +00:00
{
if(Grid.ContainsKey(CurrentHandle))
{
2007-02-04 11:36:35 +00:00
RegionInfo Region = Grid[CurrentHandle];
Globals.Instance.RegionHandle = Region.Handle;
Globals.Instance.RegionName = Region.Name;
Globals.Instance.IpPort = Region.IPAddress.Port;
Globals.Instance.LoginSever = Login;
2007-02-02 17:24:44 +00:00
}
}
}
}
catch ( Exception e)
{
Console.WriteLine(e.Message);
return;
}
}
}
public class RegionInfo
{
public RegionIP IPAddress;
public string Name;
public ushort x;
public ushort y;
public ulong handle;
public LLUUID ImageID;
public uint Flags;
public byte WaterHeight;
public ushort X
{
get
{
return(x);
}
set
{
2007-02-04 11:36:35 +00:00
x = value;
Handle = Helpers.UIntsToLong((((uint)x)*256), (((uint)y)*256));
2007-02-02 17:24:44 +00:00
}
}
public ushort Y
{
get
{
return(y);
}
set
{
2007-02-04 11:36:35 +00:00
y = value;
Handle = Helpers.UIntsToLong((((uint)x)*256), (((uint)y)*256));
2007-02-02 17:24:44 +00:00
}
}
public ulong Handle
{
get
{
2007-02-04 11:36:35 +00:00
if(handle > 0)
2007-02-02 17:24:44 +00:00
{
return(handle);
}
else
{
2007-02-04 11:36:35 +00:00
return(Helpers.UIntsToLong((((uint)x)*256), (((uint)y)*256)));
2007-02-02 17:24:44 +00:00
}
}
set
{
2007-02-04 11:36:35 +00:00
handle = value;
2007-02-02 17:24:44 +00:00
}
}
public RegionInfo()
{
2007-02-04 11:36:35 +00:00
this.IPAddress = new RegionIP();
2007-02-02 17:24:44 +00:00
}
}
public class RegionIP
{
public string Address;
public ushort Port;
public RegionIP()
{
}
}
}