Files
libremetaverse/Programs/examples/TestClient/Commands/Prims/DownloadTextureCommand.cs
John Hurliman 81e6342d36 Removing LL prefix from all basic types
git-svn-id: http://libopenmetaverse.googlecode.com/svn/trunk@1998 52acb1d6-8a22-11de-b505-999d5b087335
2008-07-25 05:15:05 +00:00

88 lines
3.1 KiB
C#

using System;
using System.IO;
using System.Threading;
using OpenMetaverse;
namespace OpenMetaverse.TestClient
{
public class DownloadTextureCommand : Command
{
UUID TextureID;
AutoResetEvent DownloadHandle = new AutoResetEvent(false);
ImageDownload Image;
AssetTexture Asset;
public DownloadTextureCommand(TestClient testClient)
{
Name = "downloadtexture";
Description = "Downloads the specified texture. " +
"Usage: downloadtexture [texture-uuid]";
testClient.Assets.OnImageReceiveProgress += new AssetManager.ImageReceiveProgressCallback(Assets_OnImageReceiveProgress);
testClient.Assets.OnImageReceived += new AssetManager.ImageReceivedCallback(Assets_OnImageReceived);
}
public override string Execute(string[] args, UUID fromAgentID)
{
if (args.Length != 1)
return "Usage: downloadtexture [texture-uuid]";
TextureID = UUID.Zero;
DownloadHandle.Reset();
Image = null;
Asset = null;
if (UUID.TryParse(args[0], out TextureID))
{
Client.Assets.RequestImage(TextureID, ImageType.Normal);
if (DownloadHandle.WaitOne(120 * 1000, false))
{
if (Image != null && Image.Success)
{
if (Asset != null && Asset.Decode())
{
try { File.WriteAllBytes(Image.ID.ToString() + ".jp2", Asset.AssetData); }
catch (Exception ex) { Logger.Log(ex.Message, Helpers.LogLevel.Error, Client, ex); }
return String.Format("Saved {0}.jp2 ({1}x{2})", Image.ID, Asset.Image.Width, Asset.Image.Height);
}
else
{
return "Failed to decode texture " + TextureID.ToString();
}
}
else if (Image != null && Image.NotFound)
{
return "Simulator reported texture not found: " + TextureID.ToString();
}
else
{
return "Download failed for texture " + TextureID.ToString();
}
}
else
{
return "Timed out waiting for texture download";
}
}
else
{
return "Usage: downloadtexture [texture-uuid]";
}
}
private void Assets_OnImageReceived(ImageDownload image, AssetTexture asset)
{
Image = image;
Asset = asset;
DownloadHandle.Set();
}
private void Assets_OnImageReceiveProgress(UUID image, int recieved, int total)
{
Console.WriteLine(String.Format("Texture {0}: Received {1} / {2}", image, recieved, total));
}
}
}