* Fixed a couple bugs in GridManager and improved it slightly

* Teleport example is more reliable and works in all cases now, due to a lot of hairy ugly code being added to it
* LLUUID constructor throws an exception on incorrect string lengths now

git-svn-id: http://libopenmetaverse.googlecode.com/svn/trunk@404 52acb1d6-8a22-11de-b505-999d5b087335
This commit is contained in:
John Hurliman
2006-10-27 09:22:48 +00:00
parent 15df12902f
commit e27df4da69
4 changed files with 101 additions and 55 deletions

View File

@@ -489,7 +489,10 @@ namespace libsecondlife
if (TeleportTimeout)
{
if (OnTeleport != null) { OnTeleport("Teleport timed out.", TeleportStat); }
TeleportMessage = "Teleport timed out.";
TeleportStat = TeleportStatus.Failed;
if (OnTeleport != null) { OnTeleport(TeleportMessage, TeleportStat); }
}
else
{

View File

@@ -73,13 +73,20 @@ namespace libsecondlife
/// </summary>
public class GridManager
{
/// <summary>
/// Triggered when a new region is discovered through GridManager
/// </summary>
public event GridRegionCallback OnRegionAdd;
/// <summary>A dictionary of all the regions, indexed by region ID</summary>
public Dictionary<LLUUID, GridRegion> Regions;
public Dictionary<string, GridRegion> Regions;
/// <summary>Current direction of the sun</summary>
public LLVector3 SunDirection;
private SecondLife Client;
private GridRegionCallback OnRegionAdd;
// This is used for BeginGetGridRegion
private GridRegionCallback OnRegionAddInternal;
private string BeginGetGridRegionName;
/// <summary>
/// Constructor
@@ -88,7 +95,7 @@ namespace libsecondlife
public GridManager(SecondLife client)
{
Client = client;
Regions = new Dictionary<LLUUID, GridRegion>();
Regions = new Dictionary<string, GridRegion>();
SunDirection = new LLVector3();
Client.Network.RegisterCallback(PacketType.MapBlockReply, new PacketCallback(MapBlockReplyHandler));
@@ -169,13 +176,13 @@ namespace libsecondlife
/// <param name="grc"></param>
public void BeginGetGridRegion(string name, GridRegionCallback grc)
{
OnRegionAdd = grc;
OnRegionAddInternal = grc;
name = name.ToLower();
BeginGetGridRegionName = name.ToLower();
if (Regions.ContainsKey(name))
if (Regions.ContainsKey(BeginGetGridRegionName))
{
OnRegionAdd(Regions[name]);
OnRegionAdd(Regions[BeginGetGridRegionName]);
}
else
{
@@ -183,7 +190,7 @@ namespace libsecondlife
map.AgentData.AgentID = Client.Network.AgentID;
map.AgentData.SessionID = Client.Network.SessionID;
map.NameData.Name = Helpers.StringToField(name);
map.NameData.Name = Helpers.StringToField(BeginGetGridRegionName);
Client.Network.SendPacket(map);
}
@@ -230,26 +237,33 @@ namespace libsecondlife
foreach (MapBlockReplyPacket.DataBlock block in map.Data)
{
region = new GridRegion();
region.X = block.X;
region.Y = block.Y;
region.Name = Helpers.FieldToString(block.Name);
region.RegionFlags = block.RegionFlags;
region.WaterHeight = block.WaterHeight;
region.Agents = block.Agents;
region.Access = block.Access;
region.MapImageID = block.MapImageID;
region.RegionHandle = Helpers.UIntsToLong((uint)region.X * (uint)256, (uint)region.Y * (uint)256);
if (region.Name != "" && region.X != 0 && region.Y != 0)
if (block.X != 0 && block.Y != 0)
{
Regions[region.Name.ToLower()] = region;
}
region = new GridRegion();
if (OnRegionAdd != null)
{
OnRegionAdd(region);
region.X = block.X;
region.Y = block.Y;
region.Name = Helpers.FieldToString(block.Name);
region.RegionFlags = block.RegionFlags;
region.WaterHeight = block.WaterHeight;
region.Agents = block.Agents;
region.Access = block.Access;
region.MapImageID = block.MapImageID;
region.RegionHandle = Helpers.UIntsToLong((uint)region.X * (uint)256, (uint)region.Y * (uint)256);
lock (Regions)
{
Regions[region.Name.ToLower()] = region;
}
if (OnRegionAddInternal != null && BeginGetGridRegionName == region.Name.ToLower())
{
OnRegionAddInternal(region);
}
else if (OnRegionAdd != null)
{
OnRegionAdd(region);
}
}
}
}

View File

@@ -61,7 +61,7 @@ namespace libsecondlife
if (val.Length == 36) val = val.Replace("-", "");
if (val.Length != 32) return;
if (val.Length != 32) throw new Exception("Malformed data passed to LLUUID constructor: " + val);
for(int i = 0; i < 16; ++i)
{

View File

@@ -1,15 +1,15 @@
using System;
using System.Collections.Generic;
using libsecondlife;
namespace Teleport
{
class Teleport
{
protected SecondLife client;
protected SecondLife Client;
protected bool DoneTeleporting = false;
protected ulong RegionHandle = 0;
protected string Sim = "";
static void Main(string[] args)
{
@@ -35,16 +35,20 @@ namespace Teleport
Console.WriteLine("Will attempt a teleport to " + sim + " {" + x + "," + y + "," + z + "}...");
Console.WriteLine();
Teleport app = new Teleport();
Teleport app = new Teleport(sim);
bool success = app.Connect(args[0], args[1], args[2]);
if (success)
{
while (app.client.Network.CurrentSim.Region.Name == null)
// Get the current sim name
while (app.Client.Network.CurrentSim.Region.Name == null)
{
System.Threading.Thread.Sleep(100);
}
if (sim.ToLower() == app.client.Network.CurrentSim.Region.Name.ToLower())
Console.WriteLine("Starting in " + app.Client.Network.CurrentSim.Region.Name);
if (sim.ToLower() == app.Client.Network.CurrentSim.Region.Name.ToLower())
{
Console.WriteLine("TODO: Add the ability to teleport somewhere in the local region. " +
"Exiting for now, please specify a region other than the current one");
@@ -58,23 +62,25 @@ namespace Teleport
}
}
protected Teleport()
protected Teleport(string sim)
{
Sim = sim;
try
{
client = new SecondLife();
Client = new SecondLife();
}
catch (Exception e)
{
// Error initializing the client
Console.WriteLine();
Console.WriteLine(e.ToString());
}
}
}
protected bool Connect(string FirstName, string LastName, string Password)
{
Console.WriteLine("Attempting to connect and login to SecondLife.");
Console.WriteLine("Attempting to connect and login to Second Life.");
// Setup Login to Second Life
Dictionary<string, object> loginParams = NetworkManager.DefaultLoginValues(FirstName,
@@ -83,17 +89,15 @@ namespace Teleport
Dictionary<string, object> loginReply = new Dictionary<string,object>();
// Login
if (!client.Network.Login(loginParams))
if (!Client.Network.Login(loginParams))
{
// Login failed
Console.WriteLine("Error logging in: " + client.Network.LoginError);
Console.WriteLine("Error logging in: " + Client.Network.LoginError);
return false;
}
// Login was successful
Console.WriteLine("Login was successful.");
Console.WriteLine("AgentID: " + client.Network.AgentID);
Console.WriteLine("SessionID: " + client.Network.SessionID);
Console.WriteLine("Login was successful");
return true;
}
@@ -101,33 +105,49 @@ namespace Teleport
protected void Disconnect()
{
// Logout of Second Life
Console.WriteLine("Request logout");
client.Network.Logout();
Console.WriteLine("Requesting logout");
Client.Network.Logout();
}
protected void doStuff(string sim, LLVector3 coords)
{
// Load up the list of estate simulators, incase we get a request to teleport to one.
// This doesn't block, and there's no easy way to know when it's done, so we'll wait a bit
// and hope for the best?
client.Grid.AddEstateSims();
Client.Grid.OnRegionAdd += new GridRegionCallback(GridRegionHandler);
Console.WriteLine("Caching estate sims...");
Client.Grid.AddEstateSims();
System.Threading.Thread.Sleep(3000);
Console.WriteLine();
Console.WriteLine("Okay, hopefully all the initial connect stuff is done, trying now...");
client.Self.OnTeleport += new TeleportCallback(Avatar_OnTeleportMessage);
if (RegionHandle == 0)
{
Client.Grid.BeginGetGridRegion(sim, new GridRegionCallback(GridRegionHandler));
int start = Environment.TickCount;
while (RegionHandle == 0)
{
Client.Tick();
if (Environment.TickCount - start > 10000)
{
Console.WriteLine("Region handle lookup failed");
Disconnect();
return;
}
}
}
Client.Self.OnTeleport += new TeleportCallback(Avatar_OnTeleportMessage);
DoneTeleporting = false;
client.Self.Teleport(sim, coords);
Client.Self.Teleport(RegionHandle, coords);
while (!DoneTeleporting)
{
client.Tick();
Client.Tick();
}
}
protected void Avatar_OnTeleportMessage(string message, TeleportStatus status)
private void Avatar_OnTeleportMessage(string message, TeleportStatus status)
{
Console.WriteLine(message);
@@ -136,5 +156,14 @@ namespace Teleport
DoneTeleporting = true;
}
}
private void GridRegionHandler(GridRegion region)
{
if (region.Name.ToLower() == Sim.ToLower())
{
RegionHandle = region.RegionHandle;
Console.WriteLine("Resolved " + Sim + " to region handle " + RegionHandle);
}
}
}
}