corrade-vassal – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 1 using System;
2 using System.IO;
3 using System.Collections.Generic;
4 using System.Threading;
5 using OpenMetaverse;
6  
7 namespace OpenMetaverse.TestClient
8 {
9 /// <summary>
10 /// Request the raw terrain file from the simulator, save it as a file.
11 ///
12 /// Can only be used by the Estate Owner
13 /// </summary>
14 public class DownloadTerrainCommand : Command
15 {
16 /// <summary>
17 /// Create a Synchronization event object
18 /// </summary>
19 private static AutoResetEvent xferTimeout = new AutoResetEvent(false);
20  
21 /// <summary>A string we use to report the result of the request with.</summary>
22 private static System.Text.StringBuilder result = new System.Text.StringBuilder();
23  
24 private static string fileName;
25  
26 /// <summary>
27 /// Download a simulators raw terrain data and save it to a file
28 /// </summary>
29 /// <param name="testClient"></param>
30 public DownloadTerrainCommand(TestClient testClient)
31 {
32 Name = "downloadterrain";
33 Description = "Download the RAW terrain file for this estate. Usage: downloadterrain [timeout]";
34 Category = CommandCategory.Simulator;
35 }
36  
37 /// <summary>
38 /// Execute the application
39 /// </summary>
40 /// <param name="args">arguments passed to this module</param>
41 /// <param name="fromAgentID">The ID of the avatar sending the request</param>
42 /// <returns></returns>
43 public override string Execute(string[] args, UUID fromAgentID)
44 {
45 int timeout = 120000; // default the timeout to 2 minutes
46 fileName = Client.Network.CurrentSim.Name + ".raw";
47  
48 if (args.Length > 0 && int.TryParse(args[0], out timeout) != true)
49 return "Usage: downloadterrain [timeout]";
50  
51 // Create a delegate which will be fired when the simulator receives our download request
52 // Starts the actual transfer request
53 EventHandler<InitiateDownloadEventArgs> initiateDownloadDelegate =
54 delegate(object sender, InitiateDownloadEventArgs e)
55 {
56 Client.Assets.RequestAssetXfer(e.SimFileName, false, false, UUID.Zero, AssetType.Unknown, false);
57 };
58  
59 // Subscribe to the event that will tell us the status of the download
60 Client.Assets.XferReceived += new EventHandler<XferReceivedEventArgs>(Assets_XferReceived);
61 // subscribe to the event which tells us when the simulator has received our request
62 Client.Assets.InitiateDownload += initiateDownloadDelegate;
63  
64 // configure request to tell the simulator to send us the file
65 List<string> parameters = new List<string>();
66 parameters.Add("download filename");
67 parameters.Add(fileName);
68 // send the request
69 Client.Estate.EstateOwnerMessage("terrain", parameters);
70  
71 // wait for (timeout) seconds for the request to complete (defaults 2 minutes)
72 if (!xferTimeout.WaitOne(timeout, false))
73 {
74 result.Append("Timeout while waiting for terrain data");
75 }
76  
77 // unsubscribe from events
78 Client.Assets.InitiateDownload -= initiateDownloadDelegate;
79 Client.Assets.XferReceived -= new EventHandler<XferReceivedEventArgs>(Assets_XferReceived);
80  
81 // return the result
82 return result.ToString();
83 }
84  
85 /// <summary>
86 /// Handle the reply to the OnXferReceived event
87 /// </summary>
88 private void Assets_XferReceived(object sender, XferReceivedEventArgs e)
89 {
90 if (e.Xfer.Success)
91 {
92 // set the result message
93 result.AppendFormat("Terrain file {0} ({1} bytes) downloaded successfully, written to {2}", e.Xfer.Filename, e.Xfer.Size, fileName);
94  
95 // write the file to disk
96 FileStream stream = new FileStream(fileName, FileMode.Create);
97 BinaryWriter w = new BinaryWriter(stream);
98 w.Write(e.Xfer.AssetData);
99 w.Close();
100  
101 // tell the application we've gotten the file
102 xferTimeout.Set();
103 }
104 }
105 }
106 }