diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Framework/Communications/Cache/SQLAssetServer.cs | 297 |
1 files changed, 297 insertions, 0 deletions
diff --git a/OpenSim/Framework/Communications/Cache/SQLAssetServer.cs b/OpenSim/Framework/Communications/Cache/SQLAssetServer.cs new file mode 100644 index 0000000..50653d8 --- /dev/null +++ b/OpenSim/Framework/Communications/Cache/SQLAssetServer.cs | |||
@@ -0,0 +1,297 @@ | |||
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 System.Reflection; | ||
32 | using libsecondlife; | ||
33 | using Nini.Config; | ||
34 | using OpenSim.Framework.Console; | ||
35 | using OpenSim.Framework.Interfaces; | ||
36 | using OpenSim.Framework.Types; | ||
37 | using OpenSim.Framework.Utilities; | ||
38 | |||
39 | namespace OpenSim.Framework.Communications.Caches | ||
40 | { | ||
41 | |||
42 | public class SQLAssetServer : IAssetServer | ||
43 | { | ||
44 | private IAssetReceiver _receiver; | ||
45 | private BlockingQueue<ARequest> _assetRequests; | ||
46 | private Thread _localAssetServerThread; | ||
47 | protected IAssetProvider m_plugin; | ||
48 | |||
49 | |||
50 | public SQLAssetServer() | ||
51 | { | ||
52 | _assetRequests = new BlockingQueue<ARequest>(); | ||
53 | AddPlugin("OpenSim.Framework.Data.SQLite.dll"); | ||
54 | this.SetUpAssetDatabase(); | ||
55 | |||
56 | this._localAssetServerThread = new Thread(new ThreadStart(RunRequests)); | ||
57 | this._localAssetServerThread.IsBackground = true; | ||
58 | this._localAssetServerThread.Start(); | ||
59 | |||
60 | } | ||
61 | |||
62 | public void AddPlugin(string FileName) | ||
63 | { | ||
64 | //MainLog.Instance.Verbose("SQLAssetServer", "AssetStorage: Attempting to load " + FileName); | ||
65 | Assembly pluginAssembly = Assembly.LoadFrom(FileName); | ||
66 | |||
67 | foreach (Type pluginType in pluginAssembly.GetTypes()) | ||
68 | { | ||
69 | if (!pluginType.IsAbstract) | ||
70 | { | ||
71 | Type typeInterface = pluginType.GetInterface("IAssetProvider", true); | ||
72 | |||
73 | if (typeInterface != null) | ||
74 | { | ||
75 | IAssetProvider plug = (IAssetProvider)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString())); | ||
76 | m_plugin = plug; | ||
77 | m_plugin.Initialise("AssetStorage.db", ""); | ||
78 | |||
79 | //MainLog.Instance.Verbose("AssetStorage: Added IAssetProvider Interface"); | ||
80 | } | ||
81 | |||
82 | typeInterface = null; | ||
83 | } | ||
84 | } | ||
85 | |||
86 | pluginAssembly = null; | ||
87 | } | ||
88 | |||
89 | public void SetReceiver(IAssetReceiver receiver) | ||
90 | { | ||
91 | this._receiver = receiver; | ||
92 | } | ||
93 | |||
94 | public void FetchAsset(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 | m_plugin.UpdateAsset(asset); | ||
105 | m_plugin.CommitAssets(); | ||
106 | } | ||
107 | |||
108 | public void CreateAsset(AssetBase asset) | ||
109 | { | ||
110 | m_plugin.CreateAsset(asset); | ||
111 | m_plugin.CommitAssets(); | ||
112 | } | ||
113 | |||
114 | public void SetServerInfo(string ServerUrl, string ServerKey) | ||
115 | { | ||
116 | |||
117 | } | ||
118 | public void Close() | ||
119 | { | ||
120 | m_plugin.CommitAssets(); | ||
121 | } | ||
122 | |||
123 | private void RunRequests() | ||
124 | { | ||
125 | |||
126 | while (true) | ||
127 | { | ||
128 | ARequest req = this._assetRequests.Dequeue(); | ||
129 | |||
130 | m_plugin.FetchAsset(req.AssetID); | ||
131 | |||
132 | AssetBase asset = m_plugin.FetchAsset(req.AssetID); | ||
133 | if (asset != null) | ||
134 | { | ||
135 | _receiver.AssetReceived(asset, req.IsTexture); | ||
136 | } | ||
137 | else | ||
138 | { | ||
139 | _receiver.AssetNotFound(req.AssetID); | ||
140 | } | ||
141 | |||
142 | } | ||
143 | |||
144 | } | ||
145 | |||
146 | private void SetUpAssetDatabase() | ||
147 | { | ||
148 | MainLog.Instance.Verbose("Setting up asset database"); | ||
149 | |||
150 | AssetBase Image = new AssetBase(); | ||
151 | Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000001"); | ||
152 | Image.Name = "Bricks"; | ||
153 | this.LoadAsset(Image, true, "bricks.jp2"); | ||
154 | m_plugin.CreateAsset(Image); | ||
155 | |||
156 | Image = new AssetBase(); | ||
157 | Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000002"); | ||
158 | Image.Name = "Plywood"; | ||
159 | this.LoadAsset(Image, true, "plywood.jp2"); | ||
160 | m_plugin.CreateAsset(Image); | ||
161 | |||
162 | Image = new AssetBase(); | ||
163 | Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000003"); | ||
164 | Image.Name = "Rocks"; | ||
165 | this.LoadAsset(Image, true, "rocks.jp2"); | ||
166 | m_plugin.CreateAsset(Image); | ||
167 | |||
168 | Image = new AssetBase(); | ||
169 | Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000004"); | ||
170 | Image.Name = "Granite"; | ||
171 | this.LoadAsset(Image, true, "granite.jp2"); | ||
172 | m_plugin.CreateAsset(Image); | ||
173 | |||
174 | Image = new AssetBase(); | ||
175 | Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000005"); | ||
176 | Image.Name = "Hardwood"; | ||
177 | this.LoadAsset(Image, true, "hardwood.jp2"); | ||
178 | m_plugin.CreateAsset(Image); | ||
179 | |||
180 | Image = new AssetBase(); | ||
181 | Image.FullID = new LLUUID("00000000-0000-0000-5005-000000000005"); | ||
182 | Image.Name = "Prim Base Texture"; | ||
183 | this.LoadAsset(Image, true, "plywood.jp2"); | ||
184 | m_plugin.CreateAsset(Image); | ||
185 | |||
186 | Image = new AssetBase(); | ||
187 | Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000006"); | ||
188 | Image.Name = "Map Base Texture"; | ||
189 | this.LoadAsset(Image, true, "map_base.jp2"); | ||
190 | m_plugin.CreateAsset(Image); | ||
191 | |||
192 | Image = new AssetBase(); | ||
193 | Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000007"); | ||
194 | Image.Name = "Map Texture"; | ||
195 | this.LoadAsset(Image, true, "map1.jp2"); | ||
196 | m_plugin.CreateAsset(Image); | ||
197 | |||
198 | Image = new AssetBase(); | ||
199 | Image.FullID = new LLUUID("00000000-0000-1111-9999-000000000010"); | ||
200 | Image.Name = "Female Body Texture"; | ||
201 | this.LoadAsset(Image, true, "femalebody.jp2"); | ||
202 | m_plugin.CreateAsset(Image); | ||
203 | |||
204 | Image = new AssetBase(); | ||
205 | Image.FullID = new LLUUID("00000000-0000-1111-9999-000000000011"); | ||
206 | Image.Name = "Female Bottom Texture"; | ||
207 | this.LoadAsset(Image, true, "femalebottom.jp2"); | ||
208 | m_plugin.CreateAsset(Image); | ||
209 | |||
210 | Image = new AssetBase(); | ||
211 | Image.FullID = new LLUUID("00000000-0000-1111-9999-000000000012"); | ||
212 | Image.Name = "Female Face Texture"; | ||
213 | this.LoadAsset(Image, true, "femaleface.jp2"); | ||
214 | m_plugin.CreateAsset(Image); | ||
215 | |||
216 | Image = new AssetBase(); | ||
217 | Image.FullID = new LLUUID("77c41e39-38f9-f75a-024e-585989bbabbb"); | ||
218 | Image.Name = "Skin"; | ||
219 | Image.Type = 13; | ||
220 | Image.InvType = 13; | ||
221 | this.LoadAsset(Image, false, "base_skin.dat"); | ||
222 | m_plugin.CreateAsset(Image); | ||
223 | |||
224 | Image = new AssetBase(); | ||
225 | Image.FullID = new LLUUID("66c41e39-38f9-f75a-024e-585989bfab73"); | ||
226 | Image.Name = "Shape"; | ||
227 | Image.Type = 13; | ||
228 | Image.InvType = 13; | ||
229 | this.LoadAsset(Image, false, "base_shape.dat"); | ||
230 | m_plugin.CreateAsset(Image); | ||
231 | |||
232 | Image = new AssetBase(); | ||
233 | Image.FullID = new LLUUID("00000000-38f9-1111-024e-222222111110"); | ||
234 | Image.Name = "Shirt"; | ||
235 | Image.Type = 5; | ||
236 | Image.InvType = 18; | ||
237 | this.LoadAsset(Image, false, "newshirt.dat"); | ||
238 | m_plugin.CreateAsset(Image); | ||
239 | |||
240 | Image = new AssetBase(); | ||
241 | Image.FullID = new LLUUID("00000000-38f9-1111-024e-222222111120"); | ||
242 | Image.Name = "Shirt"; | ||
243 | Image.Type = 5; | ||
244 | Image.InvType = 18; | ||
245 | this.LoadAsset(Image, false, "newpants.dat"); | ||
246 | m_plugin.CreateAsset(Image); | ||
247 | |||
248 | string filePath = Path.Combine(Util.configDir(), "OpenSimAssetSet.xml"); | ||
249 | if (File.Exists(filePath)) | ||
250 | { | ||
251 | XmlConfigSource source = new XmlConfigSource(filePath); | ||
252 | ReadAssetDetails(source); | ||
253 | } | ||
254 | |||
255 | m_plugin.CommitAssets(); | ||
256 | } | ||
257 | |||
258 | protected void ReadAssetDetails(IConfigSource source) | ||
259 | { | ||
260 | AssetBase newAsset = null; | ||
261 | for (int i = 0; i < source.Configs.Count; i++) | ||
262 | { | ||
263 | newAsset = new AssetBase(); | ||
264 | newAsset.FullID = new LLUUID(source.Configs[i].GetString("assetID", LLUUID.Random().ToStringHyphenated())); | ||
265 | newAsset.Name = source.Configs[i].GetString("name", ""); | ||
266 | newAsset.Type = (sbyte)source.Configs[i].GetInt("assetType", 0); | ||
267 | newAsset.InvType = (sbyte)source.Configs[i].GetInt("inventoryType", 0); | ||
268 | string fileName = source.Configs[i].GetString("fileName", ""); | ||
269 | if (fileName != "") | ||
270 | { | ||
271 | this.LoadAsset(newAsset, false, fileName); | ||
272 | m_plugin.CreateAsset(newAsset); | ||
273 | } | ||
274 | } | ||
275 | } | ||
276 | |||
277 | private void LoadAsset(AssetBase info, bool image, string filename) | ||
278 | { | ||
279 | //should request Asset from storage manager | ||
280 | //but for now read from file | ||
281 | |||
282 | string dataPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "assets"); //+ folder; | ||
283 | string fileName = Path.Combine(dataPath, filename); | ||
284 | FileInfo fInfo = new FileInfo(fileName); | ||
285 | long numBytes = fInfo.Length; | ||
286 | FileStream fStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); | ||
287 | byte[] idata = new byte[numBytes]; | ||
288 | BinaryReader br = new BinaryReader(fStream); | ||
289 | idata = br.ReadBytes((int)numBytes); | ||
290 | br.Close(); | ||
291 | fStream.Close(); | ||
292 | info.Data = idata; | ||
293 | //info.loaded=true; | ||
294 | } | ||
295 | } | ||
296 | |||
297 | } | ||