Files
libremetaverse/Programs/examples/TestClient/Commands/Inventory/UploadScriptCommand.cs

79 lines
3.0 KiB
C#
Raw Normal View History

using System;
using System.IO;
using System.Collections.Generic;
2019-10-30 20:46:28 -05:00
using System.Linq;
namespace OpenMetaverse.TestClient
{
/// <summary>
/// Example of how to put a new script in your inventory
/// </summary>
public class UploadScriptCommand : Command
{
/// <summary>
/// The default constructor for TestClient commands
/// </summary>
/// <param name="testClient"></param>
public UploadScriptCommand(TestClient testClient)
{
Name = "uploadscript";
Description = "Upload a local .lsl file file into your inventory.";
Category = CommandCategory.Inventory;
}
/// <summary>
/// The default override for TestClient commands
/// </summary>
/// <param name="args"></param>
2019-10-30 20:54:41 -05:00
/// <param name="fromAgentId"></param>
/// <returns></returns>
2019-10-30 20:54:41 -05:00
public override string Execute(string[] args, UUID fromAgentId)
{
if (args.Length < 1)
return "Usage: uploadscript filename.lsl";
2019-10-30 20:54:41 -05:00
var file = args.Aggregate(string.Empty, (current, t) => $"{current}{t} ");
file = file.TrimEnd();
if (!File.Exists(file))
2019-10-30 20:46:28 -05:00
return $"Filename '{file}' does not exist";
2019-10-30 20:54:41 -05:00
var ret = $"Filename: {file}";
try
{
2019-10-30 20:54:41 -05:00
using (var reader = new StreamReader(file))
{
2019-10-30 20:54:41 -05:00
var body = reader.ReadToEnd();
var desc = $"{file} created by OpenMetaverse TestClient {DateTime.Now}";
// create the asset
2019-10-30 20:54:41 -05:00
Client.Inventory.RequestCreateItem(Client.Inventory.FindFolderForType(AssetType.LSLText),
file, desc, AssetType.LSLText, UUID.Random(),
InventoryType.LSL, PermissionMask.All, (success, item) =>
{
if (success)
// upload the asset
Client.Inventory.RequestUpdateScriptAgentInventory(
System.Text.Encoding.UTF8.GetBytes(body), item.UUID, true,
(uploadSuccess, uploadStatus, compileSuccess, compileMessages, itemId, assetId) =>
{
if (uploadSuccess)
ret += $" Script successfully uploaded, ItemID {itemId} AssetID {assetId}";
if (compileSuccess)
ret += " compilation successful";
});
});
}
return ret;
}
2019-10-30 20:54:41 -05:00
catch (Exception e)
{
Logger.Log(e.ToString(), Helpers.LogLevel.Error, Client);
2019-10-30 20:46:28 -05:00
return $"Error creating script for {ret}";
}
}
}
}