diff options
Diffstat (limited to 'ExportBot/Commands/ExportCommand.cs')
-rw-r--r-- | ExportBot/Commands/ExportCommand.cs | 209 |
1 files changed, 209 insertions, 0 deletions
diff --git a/ExportBot/Commands/ExportCommand.cs b/ExportBot/Commands/ExportCommand.cs new file mode 100644 index 0000000..2baac1a --- /dev/null +++ b/ExportBot/Commands/ExportCommand.cs | |||
@@ -0,0 +1,209 @@ | |||
1 | using System; | ||
2 | using System.Collections.Generic; | ||
3 | using System.IO; | ||
4 | using System.Xml; | ||
5 | using System.Text; | ||
6 | using System.Threading; | ||
7 | using libsecondlife; | ||
8 | using libsecondlife.Packets; | ||
9 | |||
10 | namespace libsecondlife.TestClient | ||
11 | { | ||
12 | public class ExportCommand : Command | ||
13 | { | ||
14 | AutoResetEvent GotPermissionsEvent = new AutoResetEvent(false); | ||
15 | LLObject.ObjectPropertiesFamily Properties; | ||
16 | bool GotPermissions = false; | ||
17 | LLUUID SelectedObject = LLUUID.Zero; | ||
18 | |||
19 | Dictionary<LLUUID, Primitive> PrimsWaiting = new Dictionary<LLUUID, Primitive>(); | ||
20 | AutoResetEvent AllPropertiesReceived = new AutoResetEvent(false); | ||
21 | |||
22 | public ExportCommand(TestClient testClient) | ||
23 | { | ||
24 | testClient.Objects.OnObjectPropertiesFamily += new ObjectManager.ObjectPropertiesFamilyCallback(Objects_OnObjectPropertiesFamily); | ||
25 | testClient.Objects.OnObjectProperties += new ObjectManager.ObjectPropertiesCallback(Objects_OnObjectProperties); | ||
26 | testClient.Avatars.OnPointAt += new AvatarManager.PointAtCallback(Avatars_OnPointAt); | ||
27 | |||
28 | Name = "export"; | ||
29 | Description = "Exports an object to an xml file. Usage: export uuid outputfile.xml"; | ||
30 | } | ||
31 | |||
32 | public override string Execute(string[] args, LLUUID fromAgentID) | ||
33 | { | ||
34 | if (args.Length != 2 && !(args.Length == 1 && SelectedObject != LLUUID.Zero)) | ||
35 | return "Usage: export uuid outputfile.xml"; | ||
36 | |||
37 | LLUUID id; | ||
38 | uint localid = 0; | ||
39 | int count = 0; | ||
40 | string file; | ||
41 | |||
42 | if (args.Length == 2) | ||
43 | { | ||
44 | file = args[1]; | ||
45 | if (!LLUUID.TryParse(args[0], out id)) | ||
46 | return "Usage: export uuid outputfile.xml"; | ||
47 | } | ||
48 | else | ||
49 | { | ||
50 | file = args[0]; | ||
51 | id = SelectedObject; | ||
52 | } | ||
53 | |||
54 | lock (Client.SimPrims) | ||
55 | { | ||
56 | if (Client.SimPrims.ContainsKey(Client.Network.CurrentSim)) | ||
57 | { | ||
58 | foreach (Primitive prim in Client.SimPrims[Client.Network.CurrentSim].Values) | ||
59 | { | ||
60 | if (prim.ID == id) | ||
61 | { | ||
62 | if (prim.ParentID != 0) | ||
63 | localid = prim.ParentID; | ||
64 | else | ||
65 | localid = prim.LocalID; | ||
66 | |||
67 | break; | ||
68 | } | ||
69 | } | ||
70 | } | ||
71 | } | ||
72 | |||
73 | if (localid != 0) | ||
74 | { | ||
75 | // Check for export permission first | ||
76 | Client.Objects.RequestObjectPropertiesFamily(Client.Network.CurrentSim, id); | ||
77 | GotPermissionsEvent.WaitOne(8000, false); | ||
78 | |||
79 | if (!GotPermissions) | ||
80 | { | ||
81 | return "Couldn't fetch permissions for the requested object, try again"; | ||
82 | } | ||
83 | else | ||
84 | { | ||
85 | GotPermissions = false; | ||
86 | if (Properties.OwnerID != Client.Network.AgentID && | ||
87 | Properties.OwnerID != Client.MasterKey && | ||
88 | Client.Network.AgentID != Client.Self.ID) | ||
89 | { | ||
90 | return "That object is owned by " + Properties.OwnerID + ", we don't have permission " + | ||
91 | "to export it"; | ||
92 | } | ||
93 | } | ||
94 | |||
95 | try | ||
96 | { | ||
97 | XmlWriterSettings settings = new XmlWriterSettings(); | ||
98 | settings.Indent = true; | ||
99 | XmlWriter writer = XmlWriter.Create(file, settings); | ||
100 | |||
101 | try | ||
102 | { | ||
103 | List<Primitive> prims = new List<Primitive>(); | ||
104 | |||
105 | lock (Client.SimPrims) | ||
106 | { | ||
107 | if (Client.SimPrims.ContainsKey(Client.Network.CurrentSim)) | ||
108 | { | ||
109 | foreach (Primitive prim in Client.SimPrims[Client.Network.CurrentSim].Values) | ||
110 | { | ||
111 | if (prim.LocalID == localid || prim.ParentID == localid) | ||
112 | { | ||
113 | prims.Add(prim); | ||
114 | count++; | ||
115 | } | ||
116 | } | ||
117 | } | ||
118 | } | ||
119 | |||
120 | bool complete = RequestObjectProperties(prims, 250); | ||
121 | |||
122 | //Serialize it! | ||
123 | Helpers.PrimListToXml(prims, writer); | ||
124 | |||
125 | if (!complete) { | ||
126 | Console.WriteLine("Warning: Unable to retrieve full properties for:"); | ||
127 | foreach (LLUUID uuid in PrimsWaiting.Keys) | ||
128 | Console.WriteLine(uuid); | ||
129 | } | ||
130 | } | ||
131 | finally | ||
132 | { | ||
133 | writer.Close(); | ||
134 | } | ||
135 | } | ||
136 | catch (Exception e) | ||
137 | { | ||
138 | string ret = "Failed to write to " + file + ":" + e.ToString(); | ||
139 | if (ret.Length > 1000) | ||
140 | { | ||
141 | ret = ret.Remove(1000); | ||
142 | } | ||
143 | return ret; | ||
144 | } | ||
145 | return "Exported " + count + " prims to " + file; | ||
146 | } | ||
147 | else | ||
148 | { | ||
149 | return "Couldn't find UUID " + id.ToString() + " in the " + | ||
150 | Client.SimPrims[Client.Network.CurrentSim].Count + | ||
151 | "objects currently indexed in the current simulator"; | ||
152 | } | ||
153 | } | ||
154 | |||
155 | private bool RequestObjectProperties(List<Primitive> objects, int msPerRequest) | ||
156 | { | ||
157 | // Create an array of the local IDs of all the prims we are requesting properties for | ||
158 | uint[] localids = new uint[objects.Count]; | ||
159 | |||
160 | lock (PrimsWaiting) | ||
161 | { | ||
162 | PrimsWaiting.Clear(); | ||
163 | |||
164 | for (int i = 0; i < objects.Count; ++i) | ||
165 | { | ||
166 | localids[i] = objects[i].LocalID; | ||
167 | PrimsWaiting.Add(objects[i].ID, objects[i]); | ||
168 | } | ||
169 | } | ||
170 | |||
171 | Client.Objects.SelectObjects(Client.Network.CurrentSim, localids); | ||
172 | |||
173 | return AllPropertiesReceived.WaitOne(2000 + msPerRequest * objects.Count, false); | ||
174 | } | ||
175 | |||
176 | void Avatars_OnPointAt(LLUUID sourceID, LLUUID targetID, LLVector3d targetPos, | ||
177 | MainAvatar.PointAtType pointType, float duration, LLUUID id) | ||
178 | { | ||
179 | if (sourceID == Client.MasterKey) | ||
180 | { | ||
181 | //Client.DebugLog("Master is now selecting " + targetID.ToStringHyphenated()); | ||
182 | SelectedObject = targetID; | ||
183 | } | ||
184 | } | ||
185 | |||
186 | void Objects_OnObjectPropertiesFamily(Simulator simulator, LLObject.ObjectPropertiesFamily properties) | ||
187 | { | ||
188 | Properties = properties; | ||
189 | GotPermissions = true; | ||
190 | GotPermissionsEvent.Set(); | ||
191 | } | ||
192 | |||
193 | void Objects_OnObjectProperties(Simulator simulator, LLObject.ObjectProperties properties) | ||
194 | { | ||
195 | lock (PrimsWaiting) | ||
196 | { | ||
197 | Primitive prim; | ||
198 | if (PrimsWaiting.TryGetValue(properties.ObjectID, out prim)) | ||
199 | { | ||
200 | prim.Properties = properties; | ||
201 | } | ||
202 | PrimsWaiting.Remove(properties.ObjectID); | ||
203 | |||
204 | if (PrimsWaiting.Count == 0) | ||
205 | AllPropertiesReceived.Set(); | ||
206 | } | ||
207 | } | ||
208 | } | ||
209 | } | ||