diff --git a/libsecondlife-cs/Avatar.cs b/libsecondlife-cs/Avatar.cs index d0da6d88..190f49d3 100644 --- a/libsecondlife-cs/Avatar.cs +++ b/libsecondlife-cs/Avatar.cs @@ -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 { diff --git a/libsecondlife-cs/GridManager.cs b/libsecondlife-cs/GridManager.cs index f49adc2d..c0d37354 100644 --- a/libsecondlife-cs/GridManager.cs +++ b/libsecondlife-cs/GridManager.cs @@ -73,13 +73,20 @@ namespace libsecondlife /// public class GridManager { + /// + /// Triggered when a new region is discovered through GridManager + /// + public event GridRegionCallback OnRegionAdd; + /// A dictionary of all the regions, indexed by region ID - public Dictionary Regions; + public Dictionary Regions; /// Current direction of the sun public LLVector3 SunDirection; private SecondLife Client; - private GridRegionCallback OnRegionAdd; + // This is used for BeginGetGridRegion + private GridRegionCallback OnRegionAddInternal; + private string BeginGetGridRegionName; /// /// Constructor @@ -88,7 +95,7 @@ namespace libsecondlife public GridManager(SecondLife client) { Client = client; - Regions = new Dictionary(); + Regions = new Dictionary(); SunDirection = new LLVector3(); Client.Network.RegisterCallback(PacketType.MapBlockReply, new PacketCallback(MapBlockReplyHandler)); @@ -169,13 +176,13 @@ namespace libsecondlife /// 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); + } } } } diff --git a/libsecondlife-cs/Types.cs b/libsecondlife-cs/Types.cs index 49835c35..31ed34c2 100644 --- a/libsecondlife-cs/Types.cs +++ b/libsecondlife-cs/Types.cs @@ -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) { diff --git a/libsecondlife-cs/examples/Teleport/Teleport.cs b/libsecondlife-cs/examples/Teleport/Teleport.cs index 203f1fe7..f6f4f457 100644 --- a/libsecondlife-cs/examples/Teleport/Teleport.cs +++ b/libsecondlife-cs/examples/Teleport/Teleport.cs @@ -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 loginParams = NetworkManager.DefaultLoginValues(FirstName, @@ -83,17 +89,15 @@ namespace Teleport Dictionary loginReply = new Dictionary(); // 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); + } + } } }