diff options
Diffstat (limited to 'src/AssetManagement.cs')
-rw-r--r-- | src/AssetManagement.cs | 455 |
1 files changed, 455 insertions, 0 deletions
diff --git a/src/AssetManagement.cs b/src/AssetManagement.cs new file mode 100644 index 0000000..f919476 --- /dev/null +++ b/src/AssetManagement.cs | |||
@@ -0,0 +1,455 @@ | |||
1 | /* | ||
2 | * | ||
3 | Copyright (c) OpenSim project, http://osgrid.org/ | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the <organization> nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY <copyright holder> ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | * | ||
27 | */ | ||
28 | |||
29 | using System; | ||
30 | using System.Net; | ||
31 | using System.Collections.Generic; | ||
32 | using libsecondlife; | ||
33 | using System.Collections; | ||
34 | using libsecondlife.Packets; | ||
35 | using libsecondlife.AssetSystem; | ||
36 | using System.IO; | ||
37 | |||
38 | namespace OpenSim | ||
39 | { | ||
40 | /// <summary> | ||
41 | /// Asset and Image management | ||
42 | /// </summary> | ||
43 | public class AssetManagement | ||
44 | { | ||
45 | public Dictionary<libsecondlife.LLUUID,AssetInfo> Assets; | ||
46 | public Dictionary<libsecondlife.LLUUID,TextureImage> Textures; | ||
47 | |||
48 | public ArrayList AssetRequests = new ArrayList(); //should change to a generic | ||
49 | public ArrayList TextureRequests = new ArrayList(); | ||
50 | //public ArrayList uploads=new ArrayList(); | ||
51 | private Server _server; | ||
52 | private InventoryManager _inventoryManager; | ||
53 | private System.Text.Encoding _enc = System.Text.Encoding.ASCII; | ||
54 | |||
55 | /// <summary> | ||
56 | /// | ||
57 | /// </summary> | ||
58 | /// <param name="_server"></param> | ||
59 | public AssetManagement(Server server, InventoryManager inventoryManager) | ||
60 | { | ||
61 | this._server = server; | ||
62 | this._inventoryManager = inventoryManager; | ||
63 | Textures = new Dictionary<libsecondlife.LLUUID,TextureImage> (); | ||
64 | Assets = new Dictionary<libsecondlife.LLUUID,AssetInfo> (); | ||
65 | this.initialise(); | ||
66 | } | ||
67 | |||
68 | /// <summary> | ||
69 | /// | ||
70 | /// </summary> | ||
71 | private void initialise() | ||
72 | { | ||
73 | //Shape and skin base assets | ||
74 | AssetInfo Asset = new AssetInfo(); | ||
75 | Asset.filename = "base_shape.dat"; | ||
76 | Asset.FullID = new LLUUID("66c41e39-38f9-f75a-024e-585989bfab73"); | ||
77 | this.LoadAsset(Asset, false); | ||
78 | this.Assets.Add(Asset.FullID, Asset); | ||
79 | |||
80 | Asset = new AssetInfo(); | ||
81 | Asset.filename = "base_skin.dat"; | ||
82 | Asset.FullID = new LLUUID("e0ee49b5a4184df8d3c9a65361fe7f49"); | ||
83 | this.LoadAsset(Asset, false); | ||
84 | this.Assets.Add(Asset.FullID, Asset); | ||
85 | |||
86 | //our test images | ||
87 | //Change these filenames to images you want to use. | ||
88 | TextureImage Image = new TextureImage(); | ||
89 | Image.filename = "testpic2.jp2"; | ||
90 | Image.FullID = new LLUUID("00000000-0000-0000-5005-000000000005"); | ||
91 | Image.Name = "test Texture"; | ||
92 | this.LoadAsset(Image, true); | ||
93 | this.Textures.Add(Image.FullID, Image); | ||
94 | |||
95 | Image = new TextureImage(); | ||
96 | Image.filename = "map_base.jp2"; | ||
97 | Image.FullID = new LLUUID("00000000-0000-0000-7007-000000000006"); | ||
98 | this.LoadAsset(Image, true); | ||
99 | this.Textures.Add(Image.FullID, Image); | ||
100 | |||
101 | Image = new TextureImage(); | ||
102 | Image.filename = "map1.jp2"; | ||
103 | Image.FullID = new LLUUID("00000000-0000-0000-7009-000000000008"); | ||
104 | this.LoadAsset(Image, true); | ||
105 | this.Textures.Add(Image.FullID, Image); | ||
106 | } | ||
107 | |||
108 | /// <summary> | ||
109 | /// | ||
110 | /// </summary> | ||
111 | /// <param name="UserInfo"></param> | ||
112 | /// <param name="AssetID"></param> | ||
113 | /// <param name="TransferRequest"></param> | ||
114 | #region AssetRegion | ||
115 | |||
116 | public void AddAssetRequest(UserAgentInfo userInfo, LLUUID assetID, TransferRequestPacket transferRequest) | ||
117 | { | ||
118 | |||
119 | if(!this.Assets.ContainsKey(assetID)) | ||
120 | { | ||
121 | //not found asset | ||
122 | return; | ||
123 | } | ||
124 | AssetInfo info = this.Assets[assetID]; | ||
125 | //for now as it will be only skin or shape request just send back the asset | ||
126 | TransferInfoPacket Transfer = new TransferInfoPacket(); | ||
127 | Transfer.TransferInfo.ChannelType = 2; | ||
128 | Transfer.TransferInfo.Status = 0; | ||
129 | Transfer.TransferInfo.TargetType = 0; | ||
130 | Transfer.TransferInfo.Params = transferRequest.TransferInfo.Params; | ||
131 | Transfer.TransferInfo.Size = info.data.Length; | ||
132 | Transfer.TransferInfo.TransferID = transferRequest.TransferInfo.TransferID; | ||
133 | |||
134 | _server.SendPacket(Transfer, true, userInfo); | ||
135 | |||
136 | TransferPacketPacket TransferPacket = new TransferPacketPacket(); | ||
137 | TransferPacket.TransferData.Packet = 0; | ||
138 | TransferPacket.TransferData.ChannelType = 2; | ||
139 | TransferPacket.TransferData.TransferID=transferRequest.TransferInfo.TransferID; | ||
140 | if(info.data.Length>1000) //but needs to be less than 2000 at the moment | ||
141 | { | ||
142 | byte[] chunk = new byte[1000]; | ||
143 | Array.Copy(info.data,chunk,1000); | ||
144 | TransferPacket.TransferData.Data = chunk; | ||
145 | TransferPacket.TransferData.Status = 0; | ||
146 | _server.SendPacket(TransferPacket,true,userInfo); | ||
147 | |||
148 | TransferPacket = new TransferPacketPacket(); | ||
149 | TransferPacket.TransferData.Packet = 1; | ||
150 | TransferPacket.TransferData.ChannelType = 2; | ||
151 | TransferPacket.TransferData.TransferID = transferRequest.TransferInfo.TransferID; | ||
152 | byte[] chunk1 = new byte[(info.data.Length-1000)]; | ||
153 | Array.Copy(info.data, 1000, chunk1, 0, chunk1.Length); | ||
154 | TransferPacket.TransferData.Data = chunk1; | ||
155 | TransferPacket.TransferData.Status = 1; | ||
156 | _server.SendPacket(TransferPacket, true, userInfo); | ||
157 | } | ||
158 | else | ||
159 | { | ||
160 | TransferPacket.TransferData.Status = 1; //last packet? so set to 1 | ||
161 | TransferPacket.TransferData.Data = info.data; | ||
162 | _server.SendPacket(TransferPacket, true, userInfo); | ||
163 | } | ||
164 | |||
165 | } | ||
166 | |||
167 | public void CreateNewInventorySet(ref AvatarData Avata,UserAgentInfo UserInfo) | ||
168 | { | ||
169 | //Create Folders | ||
170 | LLUUID BaseFolder = Avata.BaseFolder; | ||
171 | _inventoryManager.CreateNewFolder(UserInfo, Avata.InventoryFolder); | ||
172 | _inventoryManager.CreateNewFolder(UserInfo, BaseFolder); | ||
173 | |||
174 | //Give a copy of default shape | ||
175 | AssetInfo Base = this.Assets[new LLUUID("66c41e39-38f9-f75a-024e-585989bfab73")]; | ||
176 | AssetInfo Shape = this.CloneAsset(UserInfo.AgentID, Base); | ||
177 | |||
178 | Shape.filename = ""; | ||
179 | Shape.Name = "Default Shape"; | ||
180 | Shape.Description = "Default Shape"; | ||
181 | Shape.InvType = 18; | ||
182 | Shape.Type=13; | ||
183 | //Shape.Type = libsecondlife.AssetSystem.Asset.ASSET_TYPE_WEARABLE_BODY; | ||
184 | |||
185 | byte[] Agentid = _enc.GetBytes(UserInfo.AgentID.ToStringHyphenated()); | ||
186 | Array.Copy(Agentid, 0, Shape.data, 294, Agentid.Length); | ||
187 | this.Assets.Add(Shape.FullID, Shape); | ||
188 | |||
189 | Avata.Wearables[0].ItemID = _inventoryManager.AddToInventory(UserInfo, BaseFolder, Shape); | ||
190 | Avata.Wearables[0].AssetID = Shape.FullID; | ||
191 | |||
192 | //Give copy of default skin | ||
193 | Base = this.Assets[new LLUUID("e0ee49b5a4184df8d3c9a65361fe7f49")]; | ||
194 | AssetInfo Skin=this.CloneAsset(UserInfo.AgentID, Base); | ||
195 | |||
196 | Skin.filename = ""; | ||
197 | Skin.Name = "Default Skin"; | ||
198 | Skin.Description = "Default Skin"; | ||
199 | Skin.InvType = 18; | ||
200 | Skin.Type=13; | ||
201 | //Skin.Type = libsecondlife.AssetSystem.Asset.ASSET_TYPE_WEARABLE_BODY; | ||
202 | |||
203 | Array.Copy(Agentid,0,Skin.data,238,Agentid.Length); | ||
204 | this.Assets.Add(Skin.FullID, Skin); | ||
205 | |||
206 | Avata.Wearables[1].ItemID = _inventoryManager.AddToInventory(UserInfo, BaseFolder, Skin); | ||
207 | Avata.Wearables[1].AssetID = Skin.FullID; | ||
208 | |||
209 | //give a copy of test texture | ||
210 | TextureImage Texture = this.CloneImage(UserInfo.AgentID, Textures[new LLUUID("00000000-0000-0000-5005-000000000005")]); | ||
211 | this.Textures.Add(Texture.FullID, Texture); | ||
212 | _inventoryManager.AddToInventory(UserInfo, BaseFolder, Texture); | ||
213 | |||
214 | } | ||
215 | |||
216 | |||
217 | private void LoadAsset(AssetBase info, bool Image) | ||
218 | { | ||
219 | WebRequest AssetLoad = WebRequest.Create(Globals.Instance.AssetURL + "getasset/" + Globals.Instance.AssetSendKey + "/" + info.FullID + "/data"); | ||
220 | WebResponse AssetResponse = AssetLoad.GetResponse(); | ||
221 | byte[] idata = new byte[(int)AssetResponse.ContentLength]; | ||
222 | BinaryReader br = new BinaryReader(AssetResponse.GetResponseStream()); | ||
223 | idata = br.ReadBytes((int)AssetResponse.ContentLength); | ||
224 | br.Close(); | ||
225 | AssetResponse.Close(); | ||
226 | info.data = idata; | ||
227 | } | ||
228 | |||
229 | public AssetInfo CloneAsset(LLUUID NewOwner, AssetInfo SourceAsset) | ||
230 | { | ||
231 | AssetInfo NewAsset = new AssetInfo(); | ||
232 | NewAsset.data = new byte[SourceAsset.data.Length]; | ||
233 | Array.Copy(SourceAsset.data, NewAsset.data, SourceAsset.data.Length); | ||
234 | NewAsset.FullID = LLUUID.Random(); | ||
235 | NewAsset.Type = SourceAsset.Type; | ||
236 | NewAsset.InvType = SourceAsset.InvType; | ||
237 | return(NewAsset); | ||
238 | } | ||
239 | #endregion | ||
240 | |||
241 | #region TextureRegion | ||
242 | public void AddTextureRequest(UserAgentInfo userInfo, LLUUID imageID) | ||
243 | { | ||
244 | |||
245 | if(!this.Textures.ContainsKey(imageID)) | ||
246 | { | ||
247 | //not found image so send back image not in data base message | ||
248 | ImageNotInDatabasePacket im_not = new ImageNotInDatabasePacket(); | ||
249 | im_not.ImageID.ID=imageID; | ||
250 | _server.SendPacket(im_not, true, userInfo); | ||
251 | return; | ||
252 | } | ||
253 | TextureImage imag = this.Textures[imageID]; | ||
254 | TextureRequest req = new TextureRequest(); | ||
255 | req.RequestUser = userInfo; | ||
256 | req.RequestImage = imageID; | ||
257 | req.ImageInfo = imag; | ||
258 | |||
259 | if(imag.data.LongLength>600) //should be bigger or smaller? | ||
260 | { | ||
261 | //over 600 bytes so split up file | ||
262 | req.NumPackets = 1 + (int)(imag.data.Length-600+999)/1000; | ||
263 | } | ||
264 | else | ||
265 | { | ||
266 | req.NumPackets = 1; | ||
267 | } | ||
268 | |||
269 | this.TextureRequests.Add(req); | ||
270 | |||
271 | } | ||
272 | |||
273 | public void AddTexture(LLUUID imageID, string name, byte[] data) | ||
274 | { | ||
275 | |||
276 | } | ||
277 | public void DoWork(ulong time) | ||
278 | { | ||
279 | if(this.TextureRequests.Count == 0) | ||
280 | { | ||
281 | //no requests waiting | ||
282 | return; | ||
283 | } | ||
284 | int num; | ||
285 | //should be running in its own thread but for now is called by timer | ||
286 | if(this.TextureRequests.Count < 5) | ||
287 | { | ||
288 | //lower than 5 so do all of them | ||
289 | num = this.TextureRequests.Count; | ||
290 | } | ||
291 | else | ||
292 | { | ||
293 | num=5; | ||
294 | } | ||
295 | TextureRequest req; | ||
296 | for(int i = 0; i < num; i++) | ||
297 | { | ||
298 | req=(TextureRequest)this.TextureRequests[i]; | ||
299 | |||
300 | if(req.PacketCounter == 0) | ||
301 | { | ||
302 | //first time for this request so send imagedata packet | ||
303 | if(req.NumPackets == 1) | ||
304 | { | ||
305 | //only one packet so send whole file | ||
306 | ImageDataPacket im = new ImageDataPacket(); | ||
307 | im.ImageID.Packets = 1; | ||
308 | im.ImageID.ID = req.ImageInfo.FullID; | ||
309 | im.ImageID.Size = (uint)req.ImageInfo.data.Length; | ||
310 | im.ImageData.Data = req.ImageInfo.data; | ||
311 | im.ImageID.Codec = 2; | ||
312 | _server.SendPacket(im, true, req.RequestUser); | ||
313 | req.PacketCounter++; | ||
314 | req.ImageInfo.last_used = time; | ||
315 | //System.Console.WriteLine("sent texture: "+req.image_info.FullID); | ||
316 | } | ||
317 | else | ||
318 | { | ||
319 | //more than one packet so split file up | ||
320 | ImageDataPacket im = new ImageDataPacket(); | ||
321 | im.ImageID.Packets = (ushort)req.NumPackets; | ||
322 | im.ImageID.ID = req.ImageInfo.FullID; | ||
323 | im.ImageID.Size = (uint)req.ImageInfo.data.Length; | ||
324 | im.ImageData.Data = new byte[600]; | ||
325 | Array.Copy(req.ImageInfo.data, 0, im.ImageData.Data, 0, 600); | ||
326 | im.ImageID.Codec = 2; | ||
327 | _server.SendPacket(im, true, req.RequestUser); | ||
328 | req.PacketCounter++; | ||
329 | req.ImageInfo.last_used = time; | ||
330 | //System.Console.WriteLine("sent first packet of texture: | ||
331 | } | ||
332 | } | ||
333 | else | ||
334 | { | ||
335 | //send imagepacket | ||
336 | //more than one packet so split file up | ||
337 | ImagePacketPacket im = new ImagePacketPacket(); | ||
338 | im.ImageID.Packet = (ushort)req.PacketCounter; | ||
339 | im.ImageID.ID = req.ImageInfo.FullID; | ||
340 | int size = req.ImageInfo.data.Length - 600 - 1000*(req.PacketCounter - 1); | ||
341 | if(size > 1000) size = 1000; | ||
342 | im.ImageData.Data = new byte[size]; | ||
343 | Array.Copy(req.ImageInfo.data, 600 + 1000*(req.PacketCounter - 1), im.ImageData.Data, 0, size); | ||
344 | _server.SendPacket(im, true, req.RequestUser); | ||
345 | req.PacketCounter++; | ||
346 | req.ImageInfo.last_used = time; | ||
347 | //System.Console.WriteLine("sent a packet of texture: "+req.image_info.FullID); | ||
348 | } | ||
349 | } | ||
350 | |||
351 | //remove requests that have been completed | ||
352 | for(int i = 0; i < num; i++) | ||
353 | { | ||
354 | req=(TextureRequest)this.TextureRequests[i]; | ||
355 | if(req.PacketCounter == req.NumPackets) | ||
356 | { | ||
357 | this.TextureRequests.Remove(req); | ||
358 | } | ||
359 | } | ||
360 | } | ||
361 | |||
362 | public void RecieveTexture(Packet pack) | ||
363 | { | ||
364 | |||
365 | } | ||
366 | |||
367 | public TextureImage CloneImage(LLUUID newOwner, TextureImage source) | ||
368 | { | ||
369 | TextureImage newImage = new TextureImage(); | ||
370 | newImage.data = new byte[source.data.Length]; | ||
371 | Array.Copy(source.data,newImage.data,source.data.Length); | ||
372 | newImage.filename = source.filename; | ||
373 | newImage.FullID = LLUUID.Random(); | ||
374 | newImage.Name = source.Name; | ||
375 | return(newImage); | ||
376 | } | ||
377 | |||
378 | #endregion | ||
379 | } | ||
380 | |||
381 | public class AssetRequest | ||
382 | { | ||
383 | public UserAgentInfo RequestUser; | ||
384 | public LLUUID RequestImage; | ||
385 | public AssetInfo asset_inf; | ||
386 | public long data_pointer = 0; | ||
387 | public int num_packets = 0; | ||
388 | public int packet_counter = 0; | ||
389 | |||
390 | public AssetRequest() | ||
391 | { | ||
392 | |||
393 | } | ||
394 | } | ||
395 | public class AssetInfo:AssetBase | ||
396 | { | ||
397 | //public byte[] data; | ||
398 | //public LLUUID Full_ID; | ||
399 | public bool loaded; | ||
400 | public ulong last_used; //need to add a tick/time counter and keep record | ||
401 | // of how often images are requested to unload unused ones. | ||
402 | |||
403 | public AssetInfo() | ||
404 | { | ||
405 | |||
406 | } | ||
407 | } | ||
408 | |||
409 | public class AssetBase | ||
410 | { | ||
411 | public byte[] data; | ||
412 | public LLUUID FullID; | ||
413 | public sbyte Type; | ||
414 | public sbyte InvType; | ||
415 | public string Name; | ||
416 | public string Description; | ||
417 | public string filename; | ||
418 | |||
419 | public AssetBase() | ||
420 | { | ||
421 | |||
422 | } | ||
423 | } | ||
424 | public class TextureRequest | ||
425 | { | ||
426 | public UserAgentInfo RequestUser; | ||
427 | public LLUUID RequestImage; | ||
428 | public TextureImage ImageInfo; | ||
429 | public long DataPointer = 0; | ||
430 | public int NumPackets = 0; | ||
431 | public int PacketCounter = 0; | ||
432 | |||
433 | public TextureRequest() | ||
434 | { | ||
435 | |||
436 | } | ||
437 | } | ||
438 | public class TextureImage: AssetBase | ||
439 | { | ||
440 | //any need for this class now most has been moved into AssetBase? | ||
441 | //public byte[] data; | ||
442 | //public LLUUID Full_ID; | ||
443 | //public string name; | ||
444 | public bool loaded; | ||
445 | public ulong last_used; //need to add a tick/time counter and keep record | ||
446 | // of how often images are requested to unload unused ones. | ||
447 | |||
448 | public TextureImage() | ||
449 | { | ||
450 | |||
451 | } | ||
452 | } | ||
453 | |||
454 | |||
455 | } | ||