corrade-vassal – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 1 using System;
2 using System.IO;
3 using System.Collections.Generic;
4  
5 namespace OpenMetaverse.TestClient
6 {
7 /// <summary>
8 /// Example of how to put a new script in your inventory
9 /// </summary>
10 public class UploadScriptCommand : Command
11 {
12 /// <summary>
13 /// The default constructor for TestClient commands
14 /// </summary>
15 /// <param name="testClient"></param>
16 public UploadScriptCommand(TestClient testClient)
17 {
18 Name = "uploadscript";
19 Description = "Upload a local .lsl file file into your inventory.";
20 Category = CommandCategory.Inventory;
21 }
22  
23 /// <summary>
24 /// The default override for TestClient commands
25 /// </summary>
26 /// <param name="args"></param>
27 /// <param name="fromAgentID"></param>
28 /// <returns></returns>
29 public override string Execute(string[] args, UUID fromAgentID)
30 {
31 if (args.Length < 1)
32 return "Usage: uploadscript filename.lsl";
33  
34 string file = String.Empty;
35 for (int ct = 0; ct < args.Length; ct++)
36 file = String.Format("{0}{1} ", file, args[ct]);
37 file = file.TrimEnd();
38  
39 if (!File.Exists(file))
40 return String.Format("Filename '{0}' does not exist", file);
41  
42 string ret = String.Format("Filename: {0}", file);
43  
44 try
45 {
46 using (StreamReader reader = new StreamReader(file))
47 {
48 string body = reader.ReadToEnd();
49 string desc = String.Format("{0} created by OpenMetaverse TestClient {1}", file, DateTime.Now);
50 // create the asset
51 Client.Inventory.RequestCreateItem(Client.Inventory.FindFolderForType(AssetType.LSLText), file, desc, AssetType.LSLText, UUID.Random(), InventoryType.LSL, PermissionMask.All,
52 delegate(bool success, InventoryItem item)
53 {
54 if (success)
55 // upload the asset
56 Client.Inventory.RequestUpdateScriptAgentInventory(EncodeScript(body), item.UUID, true, new InventoryManager.ScriptUpdatedCallback(delegate(bool uploadSuccess, string uploadStatus, bool compileSuccess, List<string> compileMessages, UUID itemid, UUID assetid)
57 {
58 if (uploadSuccess)
59 ret += String.Format(" Script successfully uploaded, ItemID {0} AssetID {1}", itemid, assetid);
60 if (compileSuccess)
61 ret += " compilation successful";
62  
63 }));
64 });
65 }
66 return ret;
67  
68 }
69 catch (System.Exception e)
70 {
71 Logger.Log(e.ToString(), Helpers.LogLevel.Error, Client);
72 return String.Format("Error creating script for {0}", ret);
73 }
74 }
75 /// <summary>
76 /// Encodes the script text for uploading
77 /// </summary>
78 /// <param name="body"></param>
79 public static byte[] EncodeScript(string body)
80 {
81 // Assume this is a string, add 1 for the null terminator ?
82 byte[] stringBytes = System.Text.Encoding.UTF8.GetBytes(body);
83 byte[] assetData = new byte[stringBytes.Length]; //+ 1];
84 Array.Copy(stringBytes, 0, assetData, 0, stringBytes.Length);
85 return assetData;
86 }
87 }
88  
89 }