using System;
using System.IO;
using System.Collections.Generic;
namespace OpenMetaverse.TestClient
{
///
/// Example of how to put a new script in your inventory
///
public class UploadScriptCommand : Command
{
///
/// The default constructor for TestClient commands
///
///
public UploadScriptCommand(TestClient testClient)
{
Name = "uploadscript";
Description = "Upload a local .lsl file file into your inventory.";
Category = CommandCategory.Inventory;
}
///
/// The default override for TestClient commands
///
///
///
///
public override string Execute(string[] args, UUID fromAgentID)
{
if (args.Length < 1)
return "Usage: uploadscript filename.lsl";
string file = String.Empty;
for (int ct = 0; ct < args.Length; ct++)
file = String.Format("{0}{1} ", file, args[ct]);
file = file.TrimEnd();
if (!File.Exists(file))
return String.Format("Filename '{0}' does not exist", file);
string ret = String.Format("Filename: {0}", file);
try
{
using (StreamReader reader = new StreamReader(file))
{
string body = reader.ReadToEnd();
string desc = String.Format("{0} created by OpenMetaverse TestClient {1}", file, DateTime.Now);
// create the asset
Client.Inventory.RequestCreateItem(Client.Inventory.FindFolderForType(AssetType.LSLText), file, desc, AssetType.LSLText, UUID.Random(), InventoryType.LSL, PermissionMask.All,
delegate(bool success, InventoryItem item)
{
if (success)
// upload the asset
Client.Inventory.RequestUpdateScriptAgentInventory(EncodeScript(body), item.UUID, true, new InventoryManager.ScriptUpdatedCallback(delegate(bool uploadSuccess, string uploadStatus, bool compileSuccess, List compileMessages, UUID itemid, UUID assetid)
{
if (uploadSuccess)
ret += String.Format(" Script successfully uploaded, ItemID {0} AssetID {1}", itemid, assetid);
if (compileSuccess)
ret += " compilation successful";
}));
});
}
return ret;
}
catch (System.Exception e)
{
Logger.Log(e.ToString(), Helpers.LogLevel.Error, Client);
return String.Format("Error creating script for {0}", ret);
}
}
///
/// Encodes the script text for uploading
///
///
public static byte[] EncodeScript(string body)
{
// Assume this is a string, add 1 for the null terminator ?
byte[] stringBytes = System.Text.Encoding.UTF8.GetBytes(body);
byte[] assetData = new byte[stringBytes.Length]; //+ 1];
Array.Copy(stringBytes, 0, assetData, 0, stringBytes.Length);
return assetData;
}
}
}