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