aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/WearableItem.cs
blob: b2cb7e1b6d2fb3166500e6814afd04c16b3e3ac8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text.RegularExpressions;
using OpenMetaverse;

namespace OpenSim.Framework
{
    public class WearableItem
    {
        public string WearableName = "";
        public WearableType WearType = WearableType.Invalid;

        public string ItemInfo = "Created Wearable";

        public SortedList<int, VisualSetting> VisualSettings = new SortedList<int, VisualSetting>();
        // public LLObject.TextureEntry TextureEntry = null;
        //public byte[] TextureEntry = null;

        public List<string> TextureStrings = new List<string>();

        //permissions
        public uint BaseMask = 0;
        public uint OwnerMask = 0;
        public uint GroupMask = 0;
        public uint EveryoneMask = 0;
        public uint NextOwnerMask = 0;

        public UUID CreatorID = UUID.Zero;
        public UUID OwnerID = UUID.Zero;
        public UUID LastOwnerID = UUID.Zero;
        public UUID GroupID = UUID.Zero;

        //sale
        public string SaleType = "not";
        public int SalePrice = 10;

        private string BuildString = "";


        public WearableItem(string wearableName, WearableType type)
        {
            WearableName = wearableName;
            WearType = type;
        }

        public WearableItem(string wearableName)
        {
            WearableName = wearableName;
            WearType = ConvertNameToType(WearableName);
        }

        public WearableItem(WearableType type)
        {
            WearType = type;
            WearableName = Enum.GetName(typeof(WearableType), type).ToLower();
        }

        public WearableItem()
        {
        }

        public void AddVisualSetting(VisualSetting setting)
        {
            if (!VisualSettings.ContainsKey(setting.VisualParam.ParamID))
            {
                VisualSettings.Add(setting.VisualParam.ParamID, setting);
            }
        }

        public bool TryGetSetting(string paramName, out VisualSetting paramSetting)
        {
            foreach (VisualSetting setting in VisualSettings.Values)
            {
                if (setting.VisualParam.Name == paramName)
                {
                    paramSetting = setting;
                    return true;
                }
            }

            paramSetting = null;
            return false;
        }

        public bool SetParamValue(string paramName, float value)
        {
            VisualSetting paramSetting;
            if (TryGetSetting(paramName, out paramSetting))
            {
                if ((value >= paramSetting.VisualParam.MinValue) && (value <= paramSetting.VisualParam.MaxValue))
                {
                    paramSetting.Value = value;
                    return true;
                }
            }
            return false;
        }

        public void RandomiseValues()
        {
            foreach (VisualSetting setting in VisualSettings.Values)
            {
                //int randNum = Util.RandomClass.Next(0, 1000);
                float range = setting.VisualParam.MaxValue - setting.VisualParam.MinValue;
                // float val =((float) randNum )/ ((float)(1000.0f / range));
                float val = (float)OpenSim.Framework.Util.RandomClass.NextDouble() * range * 0.2f;
                setting.Value = setting.VisualParam.MinValue + (range / 2) + val;
            }
        }

        public WearableType ConvertNameToType(string name)
        {
            return (WearableType)Enum.Parse(typeof(WearableType), name, true);
        }

        public string ToAssetFormat()
        {
            BuildString = "LLWearable version 22\n";
            BuildString += "New Item \n";
            BuildString += ItemInfo + "\n";


            AddSectionStart("permissions");
            AddTabbedNameValueLine("base_mask", BaseMask.ToString("00000000"));
            AddTabbedNameValueLine("owner_mask", OwnerMask.ToString("00000000"));
            AddTabbedNameValueLine("group_mask", GroupMask.ToString("00000000"));
            AddTabbedNameValueLine("everyone_mask", EveryoneMask.ToString("00000000"));
            AddTabbedNameValueLine("next_owner_mask", NextOwnerMask.ToString("00000000"));
            AddTabbedNameValueLine("creator_id", CreatorID.ToString());
            AddTabbedNameValueLine("owner_id", OwnerID.ToString());
            AddTabbedNameValueLine("last_owner_id", LastOwnerID.ToString());
            AddTabbedNameValueLine("group_id", GroupID.ToString());
            AddSectionEnd();

            AddSectionStart("sale_info");
            AddTabbedNameValueLine("sale_type", SaleType.ToString());
            AddTabbedNameValueLine("sale_price", SalePrice.ToString());
            AddSectionEnd();

            AddNameValueLine("type", ((byte)WearType).ToString());
            AddNameValueLine("parameters", VisualSettings.Count.ToString());

            foreach (KeyValuePair<int, VisualSetting> kp in VisualSettings)
            {
                AddNameValueLine(kp.Key.ToString(), kp.Value.Value.ToString(CultureInfo.InvariantCulture));
            }
            if (TextureStrings.Count == 0)
            {
                AddNameValueLine("textures", "0"); //todo output texture entry
            }
            else
            {
                AddNameValueLine("textures", TextureStrings.Count.ToString());
                for (int i = 0; i < TextureStrings.Count; i++)
                {
                    BuildString += TextureStrings[i] + "\n";
                }
                BuildString += "\n";

            }

            return BuildString;
        }

        public void SaveToFile(string fileName)
        {
            File.WriteAllText(fileName, this.ToAssetFormat());
        }

        public void AddSectionStart(string sectionName)
        {
            BuildString += "\t" + sectionName + " 0\n";
            BuildString += "\t{\n";
        }

        public void AddSectionEnd()
        {
            BuildString += "\t}\n";
        }

        private void AddTabbedNameValueLine(string name, string value)
        {
            BuildString += "\t\t";
            BuildString += name + "\t";
            BuildString += value + "\n";
        }

        private void AddNameValueLine(string name, string value)
        {
            // BuildString += "\t\t";
            BuildString += name + " ";
            BuildString += value + "\n";
        }

        #region Static Methods
        public static List<VisualParam> FindParamsForWearable(string wearableName)
        {
            List<VisualParam> wearableParams = new List<VisualParam>();
            foreach (VisualParam param in VisualParams.Params.Values)
            {
                if (param.Wearable == wearableName)
                {
                    wearableParams.Add(param);
                }
            }

            return wearableParams;
        }

        public static WearableItem Create(string wearableTypeName)
        {
            WearableItem wearableItem = new WearableItem(wearableTypeName);
            List<VisualParam> typeParams = FindParamsForWearable(wearableTypeName);
            foreach (VisualParam param in typeParams)
            {
                wearableItem.AddVisualSetting(new VisualSetting(param));
            }
            return wearableItem;
        }

        public static WearableItem CreateFromAsset(string assetData)
        {
            UUID creatorID = UUID.Zero;
            UUID ownerID = UUID.Zero;
            UUID lastOwnerID = UUID.Zero;
            UUID groupID = UUID.Zero;

            char[] newlineDelimiter = { '\n' };
            string[] lines = assetData.Split(newlineDelimiter);

            WearableItem wearableObject = null;
            Regex r = new Regex("[\t ]+");
            bool reachedParams = false;
            bool reachedTextures = false;
            foreach (string line in lines)
            {
                string trimLine = line.Trim();
                // Console.WriteLine("line : " + trimLine);

                string[] splitLine = r.Split(trimLine);
                if (splitLine.Length > 1)
                {
                    switch (splitLine[0])
                    {
                        case "textures":
                            reachedParams = false;
                            reachedTextures = true;
                            break;

                        case "type":
                            string wearableTypeName = Enum.GetName(typeof(WearableType), (WearableType)Convert.ToInt32(splitLine[1]));
                            wearableObject = Create(wearableTypeName.ToLower());
                            break;

                        case "parameters":
                            reachedParams = true;
                            break;

                        case "creator_id":
                            creatorID = new UUID(splitLine[1]);
                            break;

                        case "owner_id":
                            ownerID = new UUID(splitLine[1]);
                            break;

                        case "last_owner_id":
                            lastOwnerID = new UUID(splitLine[1]);
                            break;

                        case "group_id":
                            groupID = new UUID(splitLine[1]);
                            break;

                        default:
                            if ((wearableObject != null) && (reachedParams))
                            {
                                int id = Convert.ToInt32(splitLine[0]);
                                if (wearableObject.VisualSettings.ContainsKey(id))
                                {

                                    wearableObject.VisualSettings[id].Value = Convert.ToSingle(splitLine[1], CultureInfo.InvariantCulture);
                                }
                            }
                            else if ((wearableObject != null) && (reachedTextures))
                            {
                                wearableObject.TextureStrings.Add(line);
                            }
                            break;
                    }
                }
            }

            if (wearableObject != null)
            {
                wearableObject.CreatorID = creatorID;
                wearableObject.OwnerID = ownerID;
                wearableObject.LastOwnerID = lastOwnerID;
                wearableObject.GroupID = groupID;
            }

            return wearableObject;
        }
        #endregion

        #region Nested Class
        public class VisualSetting
        {
            public VisualParam VisualParam;
            public float Value = 0;

            public VisualSetting(VisualParam param, float value)
            {
                VisualParam = param;
                Value = value;
            }

            public VisualSetting(VisualParam param)
            {
                VisualParam = param;
                Value = param.DefaultValue;
            }
        }
        #endregion
    }
}