using System; using System.IO; using System.Collections.Generic; using System.Threading; using OpenMetaverse; namespace OpenMetaverse.TestClient { /// /// Request the raw terrain file from the simulator, save it as a file. /// /// Can only be used by the Estate Owner /// public class DownloadTerrainCommand : Command { /// /// Create a Synchronization event object /// private static AutoResetEvent xferTimeout = new AutoResetEvent(false); /// A string we use to report the result of the request with. private static System.Text.StringBuilder result = new System.Text.StringBuilder(); private static string fileName; /// /// Download a simulators raw terrain data and save it to a file /// /// public DownloadTerrainCommand(TestClient testClient) { Name = "downloadterrain"; Description = "Download the RAW terrain file for this estate. Usage: downloadterrain [timeout]"; Category = CommandCategory.Simulator; } /// /// Execute the application /// /// arguments passed to this module /// The ID of the avatar sending the request /// public override string Execute(string[] args, UUID fromAgentID) { int timeout = 120000; // default the timeout to 2 minutes fileName = Client.Network.CurrentSim.Name + ".raw"; if (args.Length > 0 && int.TryParse(args[0], out timeout) != true) return "Usage: downloadterrain [timeout]"; // Create a delegate which will be fired when the simulator receives our download request // Starts the actual transfer request EventHandler initiateDownloadDelegate = delegate(object sender, InitiateDownloadEventArgs e) { Client.Assets.RequestAssetXfer(e.SimFileName, false, false, UUID.Zero, AssetType.Unknown, false); }; // Subscribe to the event that will tell us the status of the download Client.Assets.XferReceived += new EventHandler(Assets_XferReceived); // subscribe to the event which tells us when the simulator has received our request Client.Assets.InitiateDownload += initiateDownloadDelegate; // configure request to tell the simulator to send us the file List parameters = new List(); parameters.Add("download filename"); parameters.Add(fileName); // send the request Client.Estate.EstateOwnerMessage("terrain", parameters); // wait for (timeout) seconds for the request to complete (defaults 2 minutes) if (!xferTimeout.WaitOne(timeout, false)) { result.Append("Timeout while waiting for terrain data"); } // unsubscribe from events Client.Assets.InitiateDownload -= initiateDownloadDelegate; Client.Assets.XferReceived -= new EventHandler(Assets_XferReceived); // return the result return result.ToString(); } /// /// Handle the reply to the OnXferReceived event /// private void Assets_XferReceived(object sender, XferReceivedEventArgs e) { if (e.Xfer.Success) { // set the result message result.AppendFormat("Terrain file {0} ({1} bytes) downloaded successfully, written to {2}", e.Xfer.Filename, e.Xfer.Size, fileName); // write the file to disk FileStream stream = new FileStream(fileName, FileMode.Create); BinaryWriter w = new BinaryWriter(stream); w.Write(e.Xfer.AssetData); w.Close(); // tell the application we've gotten the file xferTimeout.Set(); } } } }