From 3a157273da7de6caa098ac4af406e37be02a1171 Mon Sep 17 00:00:00 2001 From: MW Date: Sat, 3 Feb 2007 19:51:52 +0000 Subject: Added Basic Inventory support --- AssetManagement.cs | 460 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 460 insertions(+) create mode 100644 AssetManagement.cs (limited to 'AssetManagement.cs') diff --git a/AssetManagement.cs b/AssetManagement.cs new file mode 100644 index 0000000..adc8bd5 --- /dev/null +++ b/AssetManagement.cs @@ -0,0 +1,460 @@ +/* + * +Copyright (c) OpenSim project, http://sim.opensecondlife.org/ +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* * Neither the name of the nor the +* names of its contributors may be used to endorse or promote products +* derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY +* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +* DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY +* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* + */ + +using System; +using System.Collections.Generic; +using libsecondlife; +using System.Collections; +using libsecondlife.Packets; +using libsecondlife.AssetSystem; +using System.IO; + +namespace OpenSim +{ + /// + /// Asset and Image management + /// + public class AssetManagement + { + public Dictionary Assets; + public Dictionary Textures; + + public ArrayList AssetRequests=new ArrayList(); //should change to a generic + public ArrayList TextureRequests=new ArrayList(); + // public ArrayList uploads=new ArrayList(); + private Server server; + public InventoryManager InventoryManager; + private System.Text.Encoding enc = System.Text.Encoding.ASCII; + + /// + /// + /// + /// + public AssetManagement(Server server) + { + this.server=server; + Textures=new Dictionary (); + Assets=new Dictionary (); + this.initialise(); + } + + /// + /// + /// + private void initialise() + { + //Shape and skin base assets + AssetInfo Asset=new AssetInfo(); + Asset.filename="base_shape.dat"; + Asset.Full_ID=new LLUUID("66c41e39-38f9-f75a-024e-585989bfab73"); + this.LoadAsset(Asset); + this.Assets.Add(Asset.Full_ID,Asset); + + Asset=new AssetInfo(); + Asset.filename="base_skin.dat"; + Asset.Full_ID=new LLUUID("e0ee49b5a4184df8d3c9a65361fe7f49"); + this.LoadAsset(Asset); + this.Assets.Add(Asset.Full_ID,Asset); + + //our test images + //Change these filenames to images you want to use. + TextureImage Image=new TextureImage(); + Image.filename="testpic2.jp2"; + Image.Full_ID=new LLUUID("00000000-0000-0000-5005-000000000005"); + Image.Name="test Texture"; + this.LoadImage(Image); + this.Textures.Add(Image.Full_ID,Image); + + Image=new TextureImage(); + Image.filename="map_base.jp2"; + Image.Full_ID=new LLUUID("00000000-0000-0000-7007-000000000006"); + this.LoadImage(Image); + this.Textures.Add(Image.Full_ID,Image); + + Image=new TextureImage(); + Image.filename="map1.jp2"; + Image.Full_ID=new LLUUID("00000000-0000-0000-7009-000000000008"); + this.LoadImage(Image); + this.Textures.Add(Image.Full_ID,Image); + } + + /// + /// + /// + /// + /// + /// + #region AssetRegion + + public void AddAssetRequest(User_Agent_info UserInfo, LLUUID AssetID, TransferRequestPacket TransferRequest) + { + + if(!this.Assets.ContainsKey(AssetID)) + { + //not found asset + return; + } + AssetInfo info=this.Assets[AssetID]; + //for now as it will be only skin or shape request just send back the asset + TransferInfoPacket Transfer=new TransferInfoPacket(); + Transfer.TransferInfo.ChannelType=2; + Transfer.TransferInfo.Status=0; + Transfer.TransferInfo.TargetType=0; + Transfer.TransferInfo.Params=TransferRequest.TransferInfo.Params; + Transfer.TransferInfo.Size=info.data.Length; + Transfer.TransferInfo.TransferID=TransferRequest.TransferInfo.TransferID; + + server.SendPacket(Transfer,true,UserInfo); + + TransferPacketPacket TransferPacket=new TransferPacketPacket(); + TransferPacket.TransferData.Packet=0; + TransferPacket.TransferData.ChannelType=2; + TransferPacket.TransferData.TransferID=TransferRequest.TransferInfo.TransferID; + if(info.data.Length>1000) //but needs to be less than 2000 at the moment + { + byte[] chunk=new byte[1000]; + Array.Copy(info.data,chunk,1000); + TransferPacket.TransferData.Data=chunk; + TransferPacket.TransferData.Status=0; + server.SendPacket(TransferPacket,true,UserInfo); + + TransferPacket=new TransferPacketPacket(); + TransferPacket.TransferData.Packet=1; + TransferPacket.TransferData.ChannelType=2; + TransferPacket.TransferData.TransferID=TransferRequest.TransferInfo.TransferID; + byte[] chunk1=new byte[(info.data.Length-1000)]; + Array.Copy(info.data,1000,chunk1,0,chunk1.Length); + TransferPacket.TransferData.Data=chunk1; + TransferPacket.TransferData.Status=1; + server.SendPacket(TransferPacket,true,UserInfo); + } + else + { + TransferPacket.TransferData.Status=1; //last packet? so set to 1 + TransferPacket.TransferData.Data=info.data; + server.SendPacket(TransferPacket,true,UserInfo); + } + + } + + public void CreateNewInventorySet(ref AvatarData Avata,User_Agent_info UserInfo) + { + //Create Folders + LLUUID BaseFolder=Avata.BaseFolder; + InventoryManager.CreateNewFolder(UserInfo,Avata.InventoryFolder); + InventoryManager.CreateNewFolder(UserInfo, BaseFolder); + + //Give a copy of default shape + AssetInfo Base=this.Assets[new LLUUID("66c41e39-38f9-f75a-024e-585989bfab73")]; + AssetInfo Shape=this.CloneAsset(UserInfo.AgentID,Base); + + Shape.filename=""; + Shape.Name="Default Shape"; + Shape.Description="Default Shape"; + Shape.InvType=18; + Shape.Type=libsecondlife.AssetSystem.Asset.ASSET_TYPE_WEARABLE_BODY; + + byte[] Agentid=enc.GetBytes(UserInfo.AgentID.ToStringHyphenated()); + Array.Copy(Agentid,0,Shape.data,294,Agentid.Length); + this.Assets.Add(Shape.Full_ID,Shape); + + Avata.Wearables[0].ItemID=InventoryManager.AddToInventory(UserInfo,BaseFolder,Shape); + Avata.Wearables[0].AssetID=Shape.Full_ID; + + //Give copy of default skin + Base=this.Assets[new LLUUID("e0ee49b5a4184df8d3c9a65361fe7f49")]; + AssetInfo Skin=this.CloneAsset(UserInfo.AgentID,Base); + + Skin.filename=""; + Skin.Name="Default Skin"; + Skin.Description="Default Skin"; + Skin.InvType=18; + Skin.Type=libsecondlife.AssetSystem.Asset.ASSET_TYPE_WEARABLE_BODY; + + Array.Copy(Agentid,0,Skin.data,238,Agentid.Length); + this.Assets.Add(Skin.Full_ID,Skin); + + Avata.Wearables[1].ItemID=InventoryManager.AddToInventory(UserInfo,BaseFolder,Skin); + Avata.Wearables[1].AssetID=Skin.Full_ID; + + //give a copy of test texture + TextureImage Texture=this.CloneImage(UserInfo.AgentID,Textures[new LLUUID("00000000-0000-0000-5005-000000000005")]); + this.Textures.Add(Texture.Full_ID,Texture); + InventoryManager.AddToInventory(UserInfo,BaseFolder,Texture); + + } + + + private void LoadAsset(AssetInfo info) + { + //should request Asset from storage manager + //but for now read from file + + string data_path = System.AppDomain.CurrentDomain.BaseDirectory + @"\assets\"; + string filename=data_path+@info.filename; + FileInfo fInfo = new FileInfo(filename); + + long numBytes = fInfo.Length; + + FileStream fStream = new FileStream(filename, FileMode.Open, FileAccess.Read); + byte[] idata=new byte[numBytes]; + BinaryReader br = new BinaryReader(fStream); + idata= br.ReadBytes((int)numBytes); + br.Close(); + fStream.Close(); + info.data=idata; + info.loaded=true; + } + + public AssetInfo CloneAsset(LLUUID NewOwner, AssetInfo SourceAsset) + { + AssetInfo NewAsset=new AssetInfo(); + NewAsset.data=new byte[SourceAsset.data.Length]; + Array.Copy(SourceAsset.data,NewAsset.data,SourceAsset.data.Length); + NewAsset.Full_ID=LLUUID.Random(); + NewAsset.Type=SourceAsset.Type; + NewAsset.InvType=SourceAsset.InvType; + return(NewAsset); + } + #endregion + + #region TextureRegion + public void AddTextureRequest(User_Agent_info user, LLUUID image_id) + { + + if(!this.Textures.ContainsKey(image_id)) + { + //not found image so send back image not in data base message + ImageNotInDatabasePacket im_not=new ImageNotInDatabasePacket(); + im_not.ImageID.ID=image_id; + server.SendPacket(im_not,true,user); + return; + } + TextureImage imag=this.Textures[image_id]; + TextureRequest req=new TextureRequest(); + req.RequestUser=user; + req.RequestImage=image_id; + req.image_info=imag; + + if(imag.data.LongLength>1000) //should be bigger or smaller? + { + //over 1000 bytes so split up file + req.num_packets=(int)imag.data.LongLength/1000; + req.num_packets++; + } + else + { + req.num_packets=1; + } + + this.TextureRequests.Add(req); + + } + + public void AddTexture(LLUUID image_id, string name, byte[] data) + { + + } + public void DoWork(ulong time) + { + if(this.TextureRequests.Count==0) + { + //no requests waiting + return; + } + int num; + //should be running in its own thread but for now is called by timer + if(this.TextureRequests.Count<5) + { + //lower than 5 so do all of them + num=this.TextureRequests.Count; + } + else + { + num=5; + } + TextureRequest req; + for(int i=0; i