2008-03-12 22:46:53 +00:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
2009-07-10 17:16:47 +00:00
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
2008-07-21 21:12:59 +00:00
|
|
|
using OpenMetaverse;
|
2009-07-10 17:16:47 +00:00
|
|
|
using OpenMetaverse.Assets;
|
2008-03-12 22:46:53 +00:00
|
|
|
|
2008-07-21 21:12:59 +00:00
|
|
|
namespace OpenMetaverse.TestClient
|
2008-03-12 22:46:53 +00:00
|
|
|
{
|
|
|
|
|
public class CreateNotecardCommand : Command
|
|
|
|
|
{
|
2009-07-10 17:16:47 +00:00
|
|
|
const int NOTECARD_CREATE_TIMEOUT = 10 * 1000;
|
|
|
|
|
const int NOTECARD_FETCH_TIMEOUT = 10 * 1000;
|
|
|
|
|
|
2008-03-12 22:46:53 +00:00
|
|
|
public CreateNotecardCommand(TestClient testClient)
|
|
|
|
|
{
|
|
|
|
|
Name = "createnotecard";
|
|
|
|
|
Description = "Creates a notecard from a local text file.";
|
2008-07-25 08:55:36 +00:00
|
|
|
Category = CommandCategory.Inventory;
|
2008-03-12 22:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
2008-07-25 05:15:05 +00:00
|
|
|
public override string Execute(string[] args, UUID fromAgentID)
|
2008-03-12 22:46:53 +00:00
|
|
|
{
|
2008-08-21 01:19:06 +00:00
|
|
|
if (args.Length < 1)
|
2008-03-12 22:46:53 +00:00
|
|
|
return "Usage: createnotecard filename.txt";
|
|
|
|
|
|
2009-07-10 17:16:47 +00:00
|
|
|
UUID notecardItemID = UUID.Zero, notecardAssetID = UUID.Zero;
|
|
|
|
|
bool success = false;
|
|
|
|
|
string message = String.Empty;
|
|
|
|
|
AutoResetEvent notecardEvent = new AutoResetEvent(false);
|
|
|
|
|
|
|
|
|
|
#region File Loading
|
|
|
|
|
|
2008-03-12 22:46:53 +00:00
|
|
|
string file = String.Empty;
|
2008-08-21 01:19:06 +00:00
|
|
|
for (int ct = 0; ct < args.Length; ct++)
|
|
|
|
|
file = file + args[ct] + " ";
|
|
|
|
|
file = file.TrimEnd();
|
2008-03-12 22:46:53 +00:00
|
|
|
|
|
|
|
|
if (!File.Exists(file))
|
|
|
|
|
return String.Format("Filename '{0}' does not exist", file);
|
|
|
|
|
|
2009-07-10 17:16:47 +00:00
|
|
|
StreamReader reader = new StreamReader(file);
|
2008-03-12 22:46:53 +00:00
|
|
|
string body = reader.ReadToEnd();
|
|
|
|
|
|
2009-07-10 17:16:47 +00:00
|
|
|
#endregion File Loading
|
|
|
|
|
|
|
|
|
|
// Notecard creation
|
|
|
|
|
AssetNotecard notecard = new AssetNotecard();
|
|
|
|
|
notecard.BodyText = body;
|
|
|
|
|
notecard.Encode();
|
2008-05-04 07:43:02 +00:00
|
|
|
|
2009-07-10 17:16:47 +00:00
|
|
|
Client.Inventory.RequestCreateItem(Client.Inventory.FindFolderForType(AssetType.Notecard),
|
|
|
|
|
file, file + " created by OpenMetaverse TestClient " + DateTime.Now, AssetType.Notecard,
|
|
|
|
|
UUID.Random(), InventoryType.Notecard, PermissionMask.All,
|
|
|
|
|
delegate(bool createSuccess, InventoryItem item)
|
|
|
|
|
{
|
|
|
|
|
if (createSuccess)
|
2008-08-21 01:19:06 +00:00
|
|
|
{
|
2009-07-10 17:16:47 +00:00
|
|
|
Client.Inventory.RequestUploadNotecardAsset(notecard.AssetData, item.UUID,
|
|
|
|
|
delegate(bool uploadSuccess, string status, UUID itemID, UUID assetID)
|
|
|
|
|
{
|
|
|
|
|
notecardItemID = itemID;
|
|
|
|
|
notecardAssetID = assetID;
|
|
|
|
|
success = uploadSuccess;
|
|
|
|
|
message = status ?? "Unknown error uploading notecard asset";
|
|
|
|
|
notecardEvent.Set();
|
|
|
|
|
});
|
2008-03-12 22:46:53 +00:00
|
|
|
}
|
2009-07-10 17:16:47 +00:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
message = "Notecard item creation failed";
|
|
|
|
|
notecardEvent.Set();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
2008-03-12 22:46:53 +00:00
|
|
|
|
2009-07-10 17:16:47 +00:00
|
|
|
notecardEvent.WaitOne(NOTECARD_CREATE_TIMEOUT, false);
|
|
|
|
|
|
|
|
|
|
if (success)
|
2008-03-12 22:46:53 +00:00
|
|
|
{
|
2009-07-10 17:16:47 +00:00
|
|
|
Logger.Log("Notecard successfully created, ItemID " + notecardItemID + " AssetID " + notecardAssetID, Helpers.LogLevel.Info);
|
|
|
|
|
return DownloadNotecard(notecardItemID, notecardAssetID);
|
2008-03-12 22:46:53 +00:00
|
|
|
}
|
2009-07-10 17:16:47 +00:00
|
|
|
else
|
|
|
|
|
return "Notecard creation failed: " + message;
|
2008-03-12 22:46:53 +00:00
|
|
|
}
|
2009-07-10 17:16:47 +00:00
|
|
|
|
|
|
|
|
string DownloadNotecard(UUID itemID, UUID assetID)
|
2008-03-12 22:46:53 +00:00
|
|
|
{
|
2009-07-10 17:16:47 +00:00
|
|
|
UUID transferID = UUID.Zero;
|
|
|
|
|
AutoResetEvent assetDownloadEvent = new AutoResetEvent(false);
|
|
|
|
|
byte[] notecardData = null;
|
|
|
|
|
string error = "Timeout";
|
|
|
|
|
|
|
|
|
|
AssetManager.AssetReceivedCallback assetCallback =
|
|
|
|
|
delegate(AssetDownload transfer, Asset asset)
|
|
|
|
|
{
|
|
|
|
|
if (transfer.ID == transferID)
|
|
|
|
|
{
|
|
|
|
|
if (transfer.Success)
|
|
|
|
|
notecardData = transfer.AssetData;
|
|
|
|
|
else
|
|
|
|
|
error = transfer.Status.ToString();
|
|
|
|
|
assetDownloadEvent.Set();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Client.Assets.OnAssetReceived += assetCallback;
|
|
|
|
|
|
|
|
|
|
transferID = Client.Assets.RequestInventoryAsset(assetID, itemID, UUID.Zero, Client.Self.AgentID, AssetType.Notecard, true);
|
|
|
|
|
|
|
|
|
|
assetDownloadEvent.WaitOne(NOTECARD_FETCH_TIMEOUT, false);
|
|
|
|
|
|
|
|
|
|
Client.Assets.OnAssetReceived -= assetCallback;
|
|
|
|
|
|
|
|
|
|
if (notecardData != null)
|
|
|
|
|
return Encoding.UTF8.GetString(notecardData);
|
|
|
|
|
else
|
|
|
|
|
return "Error downloading notecard asset: " + error;
|
2008-03-12 22:46:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
2008-08-21 01:19:06 +00:00
|
|
|
}
|