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