diff options
Diffstat (limited to 'src/Texture_manager.cs')
-rw-r--r-- | src/Texture_manager.cs | 238 |
1 files changed, 238 insertions, 0 deletions
diff --git a/src/Texture_manager.cs b/src/Texture_manager.cs new file mode 100644 index 0000000..6d3f1db --- /dev/null +++ b/src/Texture_manager.cs | |||
@@ -0,0 +1,238 @@ | |||
1 | /* | ||
2 | Copyright (c) OpenSim project, http://osgrid.org/ | ||
3 | |||
4 | * Copyright (c) <year>, <copyright holder> | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions are met: | ||
9 | * * Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * * Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * * Neither the name of the <organization> nor the | ||
15 | * names of its contributors may be used to endorse or promote products | ||
16 | * derived from this software without specific prior written permission. | ||
17 | * | ||
18 | * THIS SOFTWARE IS PROVIDED BY <copyright holder> ``AS IS'' AND ANY | ||
19 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
21 | * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY | ||
22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | using System; | ||
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 | |||
39 | namespace OpenSim | ||
40 | { | ||
41 | /// <summary> | ||
42 | /// Description of Texture_manager. | ||
43 | /// </summary> | ||
44 | public class TextureManager | ||
45 | { | ||
46 | public Dictionary<libsecondlife.LLUUID,TextureImage> textures; | ||
47 | public ArrayList requests=new ArrayList(); //should change to a generic | ||
48 | public ArrayList uploads=new ArrayList(); | ||
49 | private Server server; | ||
50 | |||
51 | public TextureManager(Server serve) | ||
52 | { | ||
53 | server=serve; | ||
54 | textures=new Dictionary<libsecondlife.LLUUID,TextureImage> (); | ||
55 | this.initialise(); | ||
56 | } | ||
57 | |||
58 | public void AddRequest(User_Agent_info user, LLUUID image_id) | ||
59 | { | ||
60 | |||
61 | if(!this.textures.ContainsKey(image_id)) | ||
62 | { | ||
63 | //not found image so send back image not in data base message | ||
64 | ImageNotInDatabasePacket im_not=new ImageNotInDatabasePacket(); | ||
65 | im_not.ImageID.ID=image_id; | ||
66 | server.SendPacket(im_not,true,user); | ||
67 | return; | ||
68 | } | ||
69 | TextureImage imag=this.textures[image_id]; | ||
70 | TextureRequest req=new TextureRequest(); | ||
71 | req.RequestUser=user; | ||
72 | req.RequestImage=image_id; | ||
73 | req.image_info=imag; | ||
74 | |||
75 | if(imag.data.LongLength>1000) //should be bigger or smaller? | ||
76 | { | ||
77 | //over 1000 bytes so split up file | ||
78 | req.num_packets=(int)imag.data.LongLength/1000; | ||
79 | req.num_packets++; | ||
80 | } | ||
81 | else | ||
82 | { | ||
83 | req.num_packets=1; | ||
84 | } | ||
85 | |||
86 | this.requests.Add(req); | ||
87 | |||
88 | } | ||
89 | |||
90 | public void AddTexture(LLUUID image_id, string name, byte[] data) | ||
91 | { | ||
92 | |||
93 | } | ||
94 | public void DoWork(ulong time) | ||
95 | { | ||
96 | if(this.requests.Count==0) | ||
97 | { | ||
98 | //no requests waiting | ||
99 | return; | ||
100 | } | ||
101 | int num; | ||
102 | //should be running in its own thread but for now is called by timer | ||
103 | if(this.requests.Count<5) | ||
104 | { | ||
105 | //lower than 5 so do all of them | ||
106 | num=this.requests.Count; | ||
107 | } | ||
108 | else | ||
109 | { | ||
110 | num=5; | ||
111 | } | ||
112 | TextureRequest req; | ||
113 | for(int i=0; i<num; i++) | ||
114 | { | ||
115 | req=(TextureRequest)this.requests[i]; | ||
116 | |||
117 | if(req.packet_counter==0) | ||
118 | { | ||
119 | //first time for this request so send imagedata packet | ||
120 | if(req.num_packets==1) | ||
121 | { | ||
122 | //only one packet so send whole file | ||
123 | ImageDataPacket im=new ImageDataPacket(); | ||
124 | im.ImageID.Packets=1; | ||
125 | im.ImageID.ID=req.image_info.Full_ID; | ||
126 | im.ImageID.Size=(uint)req.image_info.data.Length; | ||
127 | im.ImageData.Data=req.image_info.data; | ||
128 | im.ImageID.Codec=2; | ||
129 | server.SendPacket(im,true,req.RequestUser); | ||
130 | req.packet_counter++; | ||
131 | req.image_info.last_used=time; | ||
132 | System.Console.WriteLine("sent texture: "+req.image_info.Full_ID); | ||
133 | } | ||
134 | else | ||
135 | { | ||
136 | //more than one packet so split file up | ||
137 | } | ||
138 | } | ||
139 | else | ||
140 | { | ||
141 | //send imagepacket | ||
142 | |||
143 | } | ||
144 | } | ||
145 | |||
146 | //remove requests that have been completed | ||
147 | for(int i=0; i<num; i++) | ||
148 | { | ||
149 | req=(TextureRequest)this.requests[i]; | ||
150 | if(req.packet_counter==req.num_packets) | ||
151 | { | ||
152 | this.requests.Remove(req); | ||
153 | } | ||
154 | } | ||
155 | } | ||
156 | |||
157 | public void RecieveTexture(Packet pack) | ||
158 | { | ||
159 | |||
160 | } | ||
161 | |||
162 | private void initialise() | ||
163 | { | ||
164 | |||
165 | TextureImage im=new TextureImage(); | ||
166 | im.filename="testpic2.jp2"; | ||
167 | im.Full_ID=new LLUUID("00000000-0000-0000-5005-000000000005"); | ||
168 | im.Name="test Texture"; | ||
169 | this.LoadImage(im); | ||
170 | this.textures.Add(im.Full_ID,im); | ||
171 | |||
172 | //Change these filenames to images you want to use. | ||
173 | im=new TextureImage(); | ||
174 | im.filename="map_base.jp2"; | ||
175 | im.Full_ID=new LLUUID("00000000-0000-0000-7007-000000000006"); | ||
176 | this.LoadImage(im); | ||
177 | this.textures.Add(im.Full_ID,im); | ||
178 | |||
179 | im=new TextureImage(); | ||
180 | im.filename="map1.jp2"; | ||
181 | im.Full_ID=new LLUUID("00000000-0000-0000-7009-000000000008"); | ||
182 | this.LoadImage(im); | ||
183 | this.textures.Add(im.Full_ID,im); | ||
184 | |||
185 | } | ||
186 | private void LoadImage(TextureImage im) | ||
187 | { | ||
188 | //should request Image from StorageManager | ||
189 | //but for now read from file | ||
190 | |||
191 | string data_path=System.AppDomain.CurrentDomain.BaseDirectory + @"\textures\"; | ||
192 | string filename=data_path+@im.filename; | ||
193 | FileInfo fInfo = new FileInfo(filename); | ||
194 | |||
195 | long numBytes = fInfo.Length; | ||
196 | |||
197 | FileStream fStream = new FileStream(filename, FileMode.Open, FileAccess.Read); | ||
198 | byte[] idata=new byte[numBytes]; | ||
199 | BinaryReader br = new BinaryReader(fStream); | ||
200 | idata= br.ReadBytes((int)numBytes); | ||
201 | br.Close(); | ||
202 | fStream.Close(); | ||
203 | im.data=idata; | ||
204 | im.loaded=true; | ||
205 | } | ||
206 | |||
207 | } | ||
208 | |||
209 | public class TextureRequest | ||
210 | { | ||
211 | public User_Agent_info RequestUser; | ||
212 | public LLUUID RequestImage; | ||
213 | public TextureImage image_info; | ||
214 | public long data_pointer=0; | ||
215 | public int num_packets=0; | ||
216 | public int packet_counter=0; | ||
217 | |||
218 | public TextureRequest() | ||
219 | { | ||
220 | |||
221 | } | ||
222 | } | ||
223 | public class TextureImage: AssetBase | ||
224 | { | ||
225 | //public byte[] data; | ||
226 | //public LLUUID Full_ID; | ||
227 | //public string name; | ||
228 | public string filename; | ||
229 | public bool loaded; | ||
230 | public ulong last_used; //need to add a tick/time counter and keep record | ||
231 | // of how often images are requested to unload unused ones. | ||
232 | |||
233 | public TextureImage() | ||
234 | { | ||
235 | |||
236 | } | ||
237 | } | ||
238 | } | ||