Files
libremetaverse/Programs/examples/TestClient/Commands/Prims/DownloadTextureCommand.cs

90 lines
3.1 KiB
C#
Raw Normal View History

using System;
using System.IO;
using System.Threading;
using OpenMetaverse.Assets;
namespace OpenMetaverse.TestClient
{
public class DownloadTextureCommand : Command
{
UUID TextureID;
AutoResetEvent DownloadHandle = new AutoResetEvent(false);
AssetTexture Asset;
TextureRequestState resultState;
public DownloadTextureCommand(TestClient testClient)
{
Name = "downloadtexture";
Description = "Downloads the specified texture. " +
"Usage: downloadtexture [texture-uuid] [discardlevel]";
Category = CommandCategory.Inventory;
}
public override string Execute(string[] args, UUID fromAgentID)
{
if (args.Length != 1 && args.Length != 2)
return "Usage: downloadtexture [texture-uuid] [discardlevel]";
TextureID = UUID.Zero;
DownloadHandle.Reset();
Asset = null;
if (UUID.TryParse(args[0], out TextureID))
{
int discardLevel = 0;
if (args.Length > 1)
{
2022-02-25 19:38:11 -06:00
if (!int.TryParse(args[1], out discardLevel))
return "Usage: downloadtexture [texture-uuid] [discardlevel]";
}
Client.Assets.RequestImage(TextureID, ImageType.Normal, Assets_OnImageReceived);
2025-01-13 07:44:05 -06:00
if (DownloadHandle.WaitOne(TimeSpan.FromMinutes(2), false))
{
if (resultState == TextureRequestState.Finished)
{
if (Asset != null && Asset.Decode())
{
try { File.WriteAllBytes(Asset.AssetID + ".jp2", Asset.AssetData); }
catch (Exception ex) { Logger.Log(ex.Message, Helpers.LogLevel.Error, Client, ex); }
2022-02-25 19:38:11 -06:00
return $"Saved {Asset.AssetID}.jp2 ({Asset.Image.Width}x{Asset.Image.Height})";
}
else
{
2022-02-25 19:38:11 -06:00
return "Failed to decode texture " + TextureID;
}
}
else if (resultState == TextureRequestState.NotFound)
{
2022-02-25 19:38:11 -06:00
return "Simulator reported texture not found: " + TextureID;
}
else
{
return "Download failed for texture " + TextureID + " " + resultState;
}
}
else
{
return "Timed out waiting for texture download";
}
}
else
{
return "Usage: downloadtexture [texture-uuid]";
}
}
private void Assets_OnImageReceived(TextureRequestState state, AssetTexture asset)
{
resultState = state;
Asset = asset;
DownloadHandle.Set();
}
}
}