diff options
author | MW | 2009-01-12 15:37:30 +0000 |
---|---|---|
committer | MW | 2009-01-12 15:37:30 +0000 |
commit | 3c9d57c11b5173a7a86ec9ed290603a142b60407 (patch) | |
tree | ad7e179fd5ad3b2ce201ec38eb92af0c9914878e /OpenSim/Framework | |
parent | Caught exceptions in some console commands (diff) | |
download | opensim-SC_OLD-3c9d57c11b5173a7a86ec9ed290603a142b60407.zip opensim-SC_OLD-3c9d57c11b5173a7a86ec9ed290603a142b60407.tar.gz opensim-SC_OLD-3c9d57c11b5173a7a86ec9ed290603a142b60407.tar.bz2 opensim-SC_OLD-3c9d57c11b5173a7a86ec9ed290603a142b60407.tar.xz |
Added WearableItem class to the framework. This class can be used to: load from disk, create from code, or modify a existing Wearable item. Its not currently used by anything in trunk but is a handy utility class.
I'll be writing some instructions on its use as part of a more general article on various methods of creating better initial avatars for new users. Which will include some code samples, but as each grid has its own requirements, there will be no out of the box solution included.
Diffstat (limited to 'OpenSim/Framework')
-rw-r--r-- | OpenSim/Framework/WearableItem.cs | 328 |
1 files changed, 328 insertions, 0 deletions
diff --git a/OpenSim/Framework/WearableItem.cs b/OpenSim/Framework/WearableItem.cs new file mode 100644 index 0000000..c4aa9bd --- /dev/null +++ b/OpenSim/Framework/WearableItem.cs | |||
@@ -0,0 +1,328 @@ | |||
1 | using System; | ||
2 | using System.Collections.Generic; | ||
3 | using System.Globalization; | ||
4 | using System.IO; | ||
5 | using System.Text.RegularExpressions; | ||
6 | using OpenMetaverse; | ||
7 | |||
8 | namespace OpenSim.Framework | ||
9 | { | ||
10 | public class WearableItem | ||
11 | { | ||
12 | public string WearableName = ""; | ||
13 | public WearableType WearType = WearableType.Invalid; | ||
14 | |||
15 | public string ItemInfo = "Created Wearable"; | ||
16 | |||
17 | public SortedList<int, VisualSetting> VisualSettings = new SortedList<int, VisualSetting>(); | ||
18 | // public LLObject.TextureEntry TextureEntry = null; | ||
19 | //public byte[] TextureEntry = null; | ||
20 | |||
21 | public List<string> TextureStrings = new List<string>(); | ||
22 | |||
23 | //permissions | ||
24 | public uint BaseMask = 0; | ||
25 | public uint OwnerMask = 0; | ||
26 | public uint GroupMask = 0; | ||
27 | public uint EveryoneMask = 0; | ||
28 | public uint NextOwnerMask = 0; | ||
29 | |||
30 | public UUID CreatorID = UUID.Zero; | ||
31 | public UUID OwnerID = UUID.Zero; | ||
32 | public UUID LastOwnerID = UUID.Zero; | ||
33 | public UUID GroupID = UUID.Zero; | ||
34 | |||
35 | //sale | ||
36 | public string SaleType = "not"; | ||
37 | public int SalePrice = 10; | ||
38 | |||
39 | private string BuildString = ""; | ||
40 | |||
41 | |||
42 | public WearableItem(string wearableName, WearableType type) | ||
43 | { | ||
44 | WearableName = wearableName; | ||
45 | WearType = type; | ||
46 | } | ||
47 | |||
48 | public WearableItem(string wearableName) | ||
49 | { | ||
50 | WearableName = wearableName; | ||
51 | WearType = ConvertNameToType(WearableName); | ||
52 | } | ||
53 | |||
54 | public WearableItem(WearableType type) | ||
55 | { | ||
56 | WearType = type; | ||
57 | WearableName = Enum.GetName(typeof(WearableType), type).ToLower(); | ||
58 | } | ||
59 | |||
60 | public WearableItem() | ||
61 | { | ||
62 | } | ||
63 | |||
64 | public void AddVisualSetting(VisualSetting setting) | ||
65 | { | ||
66 | if (!VisualSettings.ContainsKey(setting.VisualParam.ParamID)) | ||
67 | { | ||
68 | VisualSettings.Add(setting.VisualParam.ParamID, setting); | ||
69 | } | ||
70 | } | ||
71 | |||
72 | public bool TryGetSetting(string paramName, out VisualSetting paramSetting) | ||
73 | { | ||
74 | foreach (VisualSetting setting in VisualSettings.Values) | ||
75 | { | ||
76 | if (setting.VisualParam.Name == paramName) | ||
77 | { | ||
78 | paramSetting = setting; | ||
79 | return true; | ||
80 | } | ||
81 | } | ||
82 | |||
83 | paramSetting = null; | ||
84 | return false; | ||
85 | } | ||
86 | |||
87 | public bool SetParamValue(string paramName, float value) | ||
88 | { | ||
89 | VisualSetting paramSetting; | ||
90 | if (TryGetSetting(paramName, out paramSetting)) | ||
91 | { | ||
92 | if ((value >= paramSetting.VisualParam.MinValue) && (value <= paramSetting.VisualParam.MaxValue)) | ||
93 | { | ||
94 | paramSetting.Value = value; | ||
95 | return true; | ||
96 | } | ||
97 | } | ||
98 | return false; | ||
99 | } | ||
100 | |||
101 | public void RandomiseValues() | ||
102 | { | ||
103 | foreach (VisualSetting setting in VisualSettings.Values) | ||
104 | { | ||
105 | //int randNum = Util.RandomClass.Next(0, 1000); | ||
106 | float range = setting.VisualParam.MaxValue - setting.VisualParam.MinValue; | ||
107 | // float val =((float) randNum )/ ((float)(1000.0f / range)); | ||
108 | float val = (float)OpenSim.Framework.Util.RandomClass.NextDouble() * range * 0.2f; | ||
109 | setting.Value = setting.VisualParam.MinValue + (range / 2) + val; | ||
110 | } | ||
111 | } | ||
112 | |||
113 | public WearableType ConvertNameToType(string name) | ||
114 | { | ||
115 | return (WearableType)Enum.Parse(typeof(WearableType), name, true); | ||
116 | } | ||
117 | |||
118 | public string ToAssetFormat() | ||
119 | { | ||
120 | BuildString = "LLWearable version 22\n"; | ||
121 | BuildString += "New Item \n"; | ||
122 | BuildString += ItemInfo + "\n"; | ||
123 | |||
124 | |||
125 | AddSectionStart("permissions"); | ||
126 | AddTabbedNameValueLine("base_mask", BaseMask.ToString("00000000")); | ||
127 | AddTabbedNameValueLine("owner_mask", OwnerMask.ToString("00000000")); | ||
128 | AddTabbedNameValueLine("group_mask", GroupMask.ToString("00000000")); | ||
129 | AddTabbedNameValueLine("everyone_mask", EveryoneMask.ToString("00000000")); | ||
130 | AddTabbedNameValueLine("next_owner_mask", NextOwnerMask.ToString("00000000")); | ||
131 | AddTabbedNameValueLine("creator_id", CreatorID.ToString()); | ||
132 | AddTabbedNameValueLine("owner_id", OwnerID.ToString()); | ||
133 | AddTabbedNameValueLine("last_owner_id", LastOwnerID.ToString()); | ||
134 | AddTabbedNameValueLine("group_id", GroupID.ToString()); | ||
135 | AddSectionEnd(); | ||
136 | |||
137 | AddSectionStart("sale_info"); | ||
138 | AddTabbedNameValueLine("sale_type", SaleType.ToString()); | ||
139 | AddTabbedNameValueLine("sale_price", SalePrice.ToString()); | ||
140 | AddSectionEnd(); | ||
141 | |||
142 | AddNameValueLine("type", ((byte)WearType).ToString()); | ||
143 | AddNameValueLine("parameters", VisualSettings.Count.ToString()); | ||
144 | |||
145 | foreach (KeyValuePair<int, VisualSetting> kp in VisualSettings) | ||
146 | { | ||
147 | AddNameValueLine(kp.Key.ToString(), kp.Value.Value.ToString(CultureInfo.InvariantCulture)); | ||
148 | } | ||
149 | if (TextureStrings.Count == 0) | ||
150 | { | ||
151 | AddNameValueLine("textures", "0"); //todo output texture entry | ||
152 | } | ||
153 | else | ||
154 | { | ||
155 | AddNameValueLine("textures", TextureStrings.Count.ToString()); | ||
156 | for (int i = 0; i < TextureStrings.Count; i++) | ||
157 | { | ||
158 | BuildString += TextureStrings[i] + "\n"; | ||
159 | } | ||
160 | BuildString += "\n"; | ||
161 | |||
162 | } | ||
163 | |||
164 | return BuildString; | ||
165 | } | ||
166 | |||
167 | public void SaveToFile(string fileName) | ||
168 | { | ||
169 | File.WriteAllText(fileName, this.ToAssetFormat()); | ||
170 | } | ||
171 | |||
172 | public void AddSectionStart(string sectionName) | ||
173 | { | ||
174 | BuildString += "\t" + sectionName + " 0\n"; | ||
175 | BuildString += "\t{\n"; | ||
176 | } | ||
177 | |||
178 | public void AddSectionEnd() | ||
179 | { | ||
180 | BuildString += "\t}\n"; | ||
181 | } | ||
182 | |||
183 | private void AddTabbedNameValueLine(string name, string value) | ||
184 | { | ||
185 | BuildString += "\t\t"; | ||
186 | BuildString += name + "\t"; | ||
187 | BuildString += value + "\n"; | ||
188 | } | ||
189 | |||
190 | private void AddNameValueLine(string name, string value) | ||
191 | { | ||
192 | // BuildString += "\t\t"; | ||
193 | BuildString += name + " "; | ||
194 | BuildString += value + "\n"; | ||
195 | } | ||
196 | |||
197 | #region Static Methods | ||
198 | public static List<VisualParam> FindParamsForWearable(string wearableName) | ||
199 | { | ||
200 | List<VisualParam> wearableParams = new List<VisualParam>(); | ||
201 | foreach (VisualParam param in VisualParams.Params.Values) | ||
202 | { | ||
203 | if (param.Wearable == wearableName) | ||
204 | { | ||
205 | wearableParams.Add(param); | ||
206 | } | ||
207 | } | ||
208 | |||
209 | return wearableParams; | ||
210 | } | ||
211 | |||
212 | public static WearableItem Create(string wearableTypeName) | ||
213 | { | ||
214 | WearableItem wearableItem = new WearableItem(wearableTypeName); | ||
215 | List<VisualParam> typeParams = FindParamsForWearable(wearableTypeName); | ||
216 | foreach (VisualParam param in typeParams) | ||
217 | { | ||
218 | wearableItem.AddVisualSetting(new VisualSetting(param)); | ||
219 | } | ||
220 | return wearableItem; | ||
221 | } | ||
222 | |||
223 | public static WearableItem CreateFromAsset(string assetData) | ||
224 | { | ||
225 | UUID creatorID = UUID.Zero; | ||
226 | UUID ownerID = UUID.Zero; | ||
227 | UUID lastOwnerID = UUID.Zero; | ||
228 | UUID groupID = UUID.Zero; | ||
229 | |||
230 | char[] newlineDelimiter = { '\n' }; | ||
231 | string[] lines = assetData.Split(newlineDelimiter); | ||
232 | |||
233 | WearableItem wearableObject = null; | ||
234 | Regex r = new Regex("[\t ]+"); | ||
235 | bool reachedParams = false; | ||
236 | bool reachedTextures = false; | ||
237 | foreach (string line in lines) | ||
238 | { | ||
239 | string trimLine = line.Trim(); | ||
240 | // Console.WriteLine("line : " + trimLine); | ||
241 | |||
242 | string[] splitLine = r.Split(trimLine); | ||
243 | if (splitLine.Length > 1) | ||
244 | { | ||
245 | switch (splitLine[0]) | ||
246 | { | ||
247 | case "textures": | ||
248 | reachedParams = false; | ||
249 | reachedTextures = true; | ||
250 | break; | ||
251 | |||
252 | case "type": | ||
253 | string wearableTypeName = Enum.GetName(typeof(WearableType), (WearableType)Convert.ToInt32(splitLine[1])); | ||
254 | wearableObject = Create(wearableTypeName.ToLower()); | ||
255 | break; | ||
256 | |||
257 | case "parameters": | ||
258 | reachedParams = true; | ||
259 | break; | ||
260 | |||
261 | case "creator_id": | ||
262 | creatorID = new UUID(splitLine[1]); | ||
263 | break; | ||
264 | |||
265 | case "owner_id": | ||
266 | ownerID = new UUID(splitLine[1]); | ||
267 | break; | ||
268 | |||
269 | case "last_owner_id": | ||
270 | lastOwnerID = new UUID(splitLine[1]); | ||
271 | break; | ||
272 | |||
273 | case "group_id": | ||
274 | groupID = new UUID(splitLine[1]); | ||
275 | break; | ||
276 | |||
277 | default: | ||
278 | if ((wearableObject != null) && (reachedParams)) | ||
279 | { | ||
280 | int id = Convert.ToInt32(splitLine[0]); | ||
281 | if (wearableObject.VisualSettings.ContainsKey(id)) | ||
282 | { | ||
283 | |||
284 | wearableObject.VisualSettings[id].Value = Convert.ToSingle(splitLine[1], CultureInfo.InvariantCulture); | ||
285 | } | ||
286 | } | ||
287 | else if ((wearableObject != null) && (reachedTextures)) | ||
288 | { | ||
289 | wearableObject.TextureStrings.Add(line); | ||
290 | } | ||
291 | break; | ||
292 | } | ||
293 | } | ||
294 | } | ||
295 | |||
296 | if (wearableObject != null) | ||
297 | { | ||
298 | wearableObject.CreatorID = creatorID; | ||
299 | wearableObject.OwnerID = ownerID; | ||
300 | wearableObject.LastOwnerID = lastOwnerID; | ||
301 | wearableObject.GroupID = groupID; | ||
302 | } | ||
303 | |||
304 | return wearableObject; | ||
305 | } | ||
306 | #endregion | ||
307 | |||
308 | #region Nested Class | ||
309 | public class VisualSetting | ||
310 | { | ||
311 | public VisualParam VisualParam; | ||
312 | public float Value = 0; | ||
313 | |||
314 | public VisualSetting(VisualParam param, float value) | ||
315 | { | ||
316 | VisualParam = param; | ||
317 | Value = value; | ||
318 | } | ||
319 | |||
320 | public VisualSetting(VisualParam param) | ||
321 | { | ||
322 | VisualParam = param; | ||
323 | Value = param.DefaultValue; | ||
324 | } | ||
325 | } | ||
326 | #endregion | ||
327 | } | ||
328 | } | ||