diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Region/GridInterfaces/Local/LocalAssetServer.cs | 311 |
1 files changed, 311 insertions, 0 deletions
diff --git a/OpenSim/Region/GridInterfaces/Local/LocalAssetServer.cs b/OpenSim/Region/GridInterfaces/Local/LocalAssetServer.cs new file mode 100644 index 0000000..54d43a1 --- /dev/null +++ b/OpenSim/Region/GridInterfaces/Local/LocalAssetServer.cs | |||
@@ -0,0 +1,311 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://www.openmetaverse.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
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 OpenSim Project 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 THE DEVELOPERS ``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 THE CONTRIBUTORS 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 | using System; | ||
29 | using System.IO; | ||
30 | using System.Threading; | ||
31 | using Db4objects.Db4o; | ||
32 | using Db4objects.Db4o.Query; | ||
33 | using libsecondlife; | ||
34 | using OpenSim.Framework.Console; | ||
35 | using OpenSim.Framework.Interfaces; | ||
36 | using OpenSim.Framework.Types; | ||
37 | using OpenSim.Framework.Utilities; | ||
38 | |||
39 | namespace OpenSim.Region.GridInterfaces.Local | ||
40 | { | ||
41 | public class LocalAssetPlugin : IAssetPlugin | ||
42 | { | ||
43 | public LocalAssetPlugin() | ||
44 | { | ||
45 | |||
46 | } | ||
47 | |||
48 | public IAssetServer GetAssetServer() | ||
49 | { | ||
50 | return (new LocalAssetServer()); | ||
51 | } | ||
52 | } | ||
53 | |||
54 | public class LocalAssetServer : IAssetServer | ||
55 | { | ||
56 | private IAssetReceiver _receiver; | ||
57 | private BlockingQueue<ARequest> _assetRequests; | ||
58 | private IObjectContainer db; | ||
59 | private Thread _localAssetServerThread; | ||
60 | |||
61 | public LocalAssetServer() | ||
62 | { | ||
63 | bool yapfile; | ||
64 | this._assetRequests = new BlockingQueue<ARequest>(); | ||
65 | yapfile = File.Exists("assets.yap"); | ||
66 | |||
67 | MainLog.Instance.Verbose( "Local Asset Server class created"); | ||
68 | try | ||
69 | { | ||
70 | db = Db4oFactory.OpenFile("assets.yap"); | ||
71 | MainLog.Instance.Verbose( "Db4 Asset database creation"); | ||
72 | } | ||
73 | catch (Exception e) | ||
74 | { | ||
75 | db.Close(); | ||
76 | MainLog.Instance.WriteLine(LogPriority.MEDIUM, "Db4 Asset server :Constructor - Exception occured"); | ||
77 | MainLog.Instance.Warn(e.ToString()); | ||
78 | } | ||
79 | if (!yapfile) | ||
80 | { | ||
81 | this.SetUpAssetDatabase(); | ||
82 | } | ||
83 | this._localAssetServerThread = new Thread(new ThreadStart(RunRequests)); | ||
84 | this._localAssetServerThread.IsBackground = true; | ||
85 | this._localAssetServerThread.Start(); | ||
86 | |||
87 | } | ||
88 | |||
89 | public void SetReceiver(IAssetReceiver receiver) | ||
90 | { | ||
91 | this._receiver = receiver; | ||
92 | } | ||
93 | |||
94 | public void RequestAsset(LLUUID assetID, bool isTexture) | ||
95 | { | ||
96 | ARequest req = new ARequest(); | ||
97 | req.AssetID = assetID; | ||
98 | req.IsTexture = isTexture; | ||
99 | this._assetRequests.Enqueue(req); | ||
100 | } | ||
101 | |||
102 | public void UpdateAsset(AssetBase asset) | ||
103 | { | ||
104 | |||
105 | } | ||
106 | |||
107 | public void UploadNewAsset(AssetBase asset) | ||
108 | { | ||
109 | AssetStorage store = new AssetStorage(); | ||
110 | store.Data = asset.Data; | ||
111 | store.Name = asset.Name; | ||
112 | store.UUID = asset.FullID; | ||
113 | db.Set(store); | ||
114 | db.Commit(); | ||
115 | } | ||
116 | |||
117 | public void SetServerInfo(string ServerUrl, string ServerKey) | ||
118 | { | ||
119 | |||
120 | } | ||
121 | public void Close() | ||
122 | { | ||
123 | if (db != null) | ||
124 | { | ||
125 | MainLog.Instance.Verbose( "Closing local asset server database"); | ||
126 | db.Close(); | ||
127 | } | ||
128 | } | ||
129 | |||
130 | private void RunRequests() | ||
131 | { | ||
132 | while (true) | ||
133 | { | ||
134 | byte[] idata = null; | ||
135 | bool found = false; | ||
136 | AssetStorage foundAsset = null; | ||
137 | ARequest req = this._assetRequests.Dequeue(); | ||
138 | IObjectSet result = db.Query(new AssetUUIDQuery(req.AssetID)); | ||
139 | if (result.Count > 0) | ||
140 | { | ||
141 | foundAsset = (AssetStorage)result.Next(); | ||
142 | found = true; | ||
143 | } | ||
144 | |||
145 | AssetBase asset = new AssetBase(); | ||
146 | if (found) | ||
147 | { | ||
148 | asset.FullID = foundAsset.UUID; | ||
149 | asset.Type = foundAsset.Type; | ||
150 | asset.InvType = foundAsset.Type; | ||
151 | asset.Name = foundAsset.Name; | ||
152 | idata = foundAsset.Data; | ||
153 | } | ||
154 | else | ||
155 | { | ||
156 | asset.FullID = LLUUID.Zero; | ||
157 | } | ||
158 | asset.Data = idata; | ||
159 | _receiver.AssetReceived(asset, req.IsTexture); | ||
160 | } | ||
161 | |||
162 | } | ||
163 | |||
164 | private void SetUpAssetDatabase() | ||
165 | { | ||
166 | try | ||
167 | { | ||
168 | |||
169 | MainLog.Instance.Verbose( "Setting up asset database"); | ||
170 | |||
171 | AssetBase Image = new AssetBase(); | ||
172 | Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000001"); | ||
173 | Image.Name = "Bricks"; | ||
174 | this.LoadAsset(Image, true, "bricks.jp2"); | ||
175 | AssetStorage store = new AssetStorage(); | ||
176 | store.Data = Image.Data; | ||
177 | store.Name = Image.Name; | ||
178 | store.UUID = Image.FullID; | ||
179 | db.Set(store); | ||
180 | db.Commit(); | ||
181 | |||
182 | Image = new AssetBase(); | ||
183 | Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000002"); | ||
184 | Image.Name = "Plywood"; | ||
185 | this.LoadAsset(Image, true, "plywood.jp2"); | ||
186 | store = new AssetStorage(); | ||
187 | store.Data = Image.Data; | ||
188 | store.Name = Image.Name; | ||
189 | store.UUID = Image.FullID; | ||
190 | db.Set(store); | ||
191 | db.Commit(); | ||
192 | |||
193 | Image = new AssetBase(); | ||
194 | Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000003"); | ||
195 | Image.Name = "Rocks"; | ||
196 | this.LoadAsset(Image, true, "rocks.jp2"); | ||
197 | store = new AssetStorage(); | ||
198 | store.Data = Image.Data; | ||
199 | store.Name = Image.Name; | ||
200 | store.UUID = Image.FullID; | ||
201 | db.Set(store); | ||
202 | db.Commit(); | ||
203 | |||
204 | Image = new AssetBase(); | ||
205 | Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000004"); | ||
206 | Image.Name = "Granite"; | ||
207 | this.LoadAsset(Image, true, "granite.jp2"); | ||
208 | store = new AssetStorage(); | ||
209 | store.Data = Image.Data; | ||
210 | store.Name = Image.Name; | ||
211 | store.UUID = Image.FullID; | ||
212 | db.Set(store); | ||
213 | db.Commit(); | ||
214 | |||
215 | Image = new AssetBase(); | ||
216 | Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000005"); | ||
217 | Image.Name = "Hardwood"; | ||
218 | this.LoadAsset(Image, true, "hardwood.jp2"); | ||
219 | store = new AssetStorage(); | ||
220 | store.Data = Image.Data; | ||
221 | store.Name = Image.Name; | ||
222 | store.UUID = Image.FullID; | ||
223 | db.Set(store); | ||
224 | db.Commit(); | ||
225 | |||
226 | Image = new AssetBase(); | ||
227 | Image.FullID = new LLUUID("00000000-0000-0000-5005-000000000005"); | ||
228 | Image.Name = "Prim Base Texture"; | ||
229 | this.LoadAsset(Image, true, "plywood.jp2"); | ||
230 | store = new AssetStorage(); | ||
231 | store.Data = Image.Data; | ||
232 | store.Name = Image.Name; | ||
233 | store.UUID = Image.FullID; | ||
234 | db.Set(store); | ||
235 | db.Commit(); | ||
236 | |||
237 | Image = new AssetBase(); | ||
238 | Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000006"); | ||
239 | Image.Name = "Map Base Texture"; | ||
240 | this.LoadAsset(Image, true, "map_base.jp2"); | ||
241 | store = new AssetStorage(); | ||
242 | store.Data = Image.Data; | ||
243 | store.Name = Image.Name; | ||
244 | store.UUID = Image.FullID; | ||
245 | db.Set(store); | ||
246 | db.Commit(); | ||
247 | |||
248 | Image = new AssetBase(); | ||
249 | Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000007"); | ||
250 | Image.Name = "Map Texture"; | ||
251 | this.LoadAsset(Image, true, "map1.jp2"); | ||
252 | store = new AssetStorage(); | ||
253 | store.Data = Image.Data; | ||
254 | store.Name = Image.Name; | ||
255 | store.UUID = Image.FullID; | ||
256 | db.Set(store); | ||
257 | db.Commit(); | ||
258 | |||
259 | Image = new AssetBase(); | ||
260 | Image.FullID = new LLUUID("66c41e39-38f9-f75a-024e-585989bfab73"); | ||
261 | Image.Name = "Shape"; | ||
262 | this.LoadAsset(Image, false, "base_shape.dat"); | ||
263 | store = new AssetStorage(); | ||
264 | store.Data = Image.Data; | ||
265 | store.Name = Image.Name; | ||
266 | store.UUID = Image.FullID; | ||
267 | db.Set(store); | ||
268 | db.Commit(); | ||
269 | } | ||
270 | catch (Exception e) | ||
271 | { | ||
272 | Console.WriteLine("exception loading default assets into database"); | ||
273 | Console.WriteLine(e.Message); | ||
274 | } | ||
275 | |||
276 | } | ||
277 | |||
278 | private void LoadAsset(AssetBase info, bool image, string filename) | ||
279 | { | ||
280 | //should request Asset from storage manager | ||
281 | //but for now read from file | ||
282 | |||
283 | string dataPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "assets"); //+ folder; | ||
284 | string fileName = Path.Combine(dataPath, filename); | ||
285 | FileInfo fInfo = new FileInfo(fileName); | ||
286 | long numBytes = fInfo.Length; | ||
287 | FileStream fStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); | ||
288 | byte[] idata = new byte[numBytes]; | ||
289 | BinaryReader br = new BinaryReader(fStream); | ||
290 | idata = br.ReadBytes((int)numBytes); | ||
291 | br.Close(); | ||
292 | fStream.Close(); | ||
293 | info.Data = idata; | ||
294 | //info.loaded=true; | ||
295 | } | ||
296 | } | ||
297 | public class AssetUUIDQuery : Predicate | ||
298 | { | ||
299 | private LLUUID _findID; | ||
300 | |||
301 | public AssetUUIDQuery(LLUUID find) | ||
302 | { | ||
303 | _findID = find; | ||
304 | } | ||
305 | public bool Match(AssetStorage asset) | ||
306 | { | ||
307 | return (asset.UUID == _findID); | ||
308 | } | ||
309 | } | ||
310 | |||
311 | } | ||