corrade-vassal – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 1 /*
2 * Copyright (c) 2006-2014, openmetaverse.org
3 * All rights reserved.
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 *
8 * - Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 * - Neither the name of the openmetaverse.org nor the names
11 * of its contributors may be used to endorse or promote products derived from
12 * this software without specific prior written permission.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
25 */
26  
27 using System;
28 using System.Collections.Generic;
29 using System.Text;
30  
31 namespace OpenMetaverse
32 {
33 /// <summary>
34 /// A Name Value pair with additional settings, used in the protocol
35 /// primarily to transmit avatar names and active group in object packets
36 /// </summary>
37 public struct NameValue
38 {
39 #region Enums
40  
41 /// <summary>Type of the value</summary>
42 public enum ValueType
43 {
44 /// <summary>Unknown</summary>
45 Unknown = -1,
46 /// <summary>String value</summary>
47 String,
48 /// <summary></summary>
49 F32,
50 /// <summary></summary>
51 S32,
52 /// <summary></summary>
53 VEC3,
54 /// <summary></summary>
55 U32,
56 /// <summary>Deprecated</summary>
57 [Obsolete]
58 CAMERA,
59 /// <summary>String value, but designated as an asset</summary>
60 Asset,
61 /// <summary></summary>
62 U64
63 }
64  
65 /// <summary>
66 ///
67 /// </summary>
68 public enum ClassType
69 {
70 /// <summary></summary>
71 Unknown = -1,
72 /// <summary></summary>
73 ReadOnly,
74 /// <summary></summary>
75 ReadWrite,
76 /// <summary></summary>
77 Callback
78 }
79  
80 /// <summary>
81 ///
82 /// </summary>
83 public enum SendtoType
84 {
85 /// <summary></summary>
86 Unknown = -1,
87 /// <summary></summary>
88 Sim,
89 /// <summary></summary>
90 DataSim,
91 /// <summary></summary>
92 SimViewer,
93 /// <summary></summary>
94 DataSimViewer
95 }
96  
97 #endregion Enums
98  
99  
100 /// <summary></summary>
101 public string Name;
102 /// <summary></summary>
103 public ValueType Type;
104 /// <summary></summary>
105 public ClassType Class;
106 /// <summary></summary>
107 public SendtoType Sendto;
108 /// <summary></summary>
109 public object Value;
110  
111  
112 private static readonly string[] TypeStrings = new string[]
113 {
114 "STRING",
115 "F32",
116 "S32",
117 "VEC3",
118 "U32",
119 "ASSET",
120 "U64"
121 };
122 private static readonly string[] ClassStrings = new string[]
123 {
124 "R", // Read-only
125 "RW", // Read-write
126 "CB" // Callback
127 };
128 private static readonly string[] SendtoStrings = new string[]
129 {
130 "S", // Sim
131 "DS", // Data Sim
132 "SV", // Sim Viewer
133 "DSV" // Data Sim Viewer
134 };
135 private static readonly char[] Separators = new char[]
136 {
137 ' ',
138 '\n',
139 '\t',
140 '\r'
141 };
142  
143 /// <summary>
144 /// Constructor that takes all the fields as parameters
145 /// </summary>
146 /// <param name="name"></param>
147 /// <param name="valueType"></param>
148 /// <param name="classType"></param>
149 /// <param name="sendtoType"></param>
150 /// <param name="value"></param>
151 public NameValue(string name, ValueType valueType, ClassType classType, SendtoType sendtoType, object value)
152 {
153 Name = name;
154 Type = valueType;
155 Class = classType;
156 Sendto = sendtoType;
157 Value = value;
158 }
159  
160 /// <summary>
161 /// Constructor that takes a single line from a NameValue field
162 /// </summary>
163 /// <param name="data"></param>
164 public NameValue(string data)
165 {
166 int i;
167  
168 // Name
169 i = data.IndexOfAny(Separators);
170 if (i < 1)
171 {
172 Name = String.Empty;
173 Type = ValueType.Unknown;
174 Class = ClassType.Unknown;
175 Sendto = SendtoType.Unknown;
176 Value = null;
177 return;
178 }
179 Name = data.Substring(0, i);
180 data = data.Substring(i + 1);
181  
182 // Type
183 i = data.IndexOfAny(Separators);
184 if (i > 0)
185 {
186 Type = GetValueType(data.Substring(0, i));
187 data = data.Substring(i + 1);
188  
189 // Class
190 i = data.IndexOfAny(Separators);
191 if (i > 0)
192 {
193 Class = GetClassType(data.Substring(0, i));
194 data = data.Substring(i + 1);
195  
196 // Sendto
197 i = data.IndexOfAny(Separators);
198 if (i > 0)
199 {
200 Sendto = GetSendtoType(data.Substring(0, 1));
201 data = data.Substring(i + 1);
202 }
203 }
204 }
205  
206 // Value
207 Type = ValueType.String;
208 Class = ClassType.ReadOnly;
209 Sendto = SendtoType.Sim;
210 Value = null;
211 SetValue(data);
212 }
213  
214 public static string NameValuesToString(NameValue[] values)
215 {
216 if (values == null || values.Length == 0)
217 return String.Empty;
218  
219 StringBuilder output = new StringBuilder();
220  
221 for (int i = 0; i < values.Length; i++)
222 {
223 NameValue value = values[i];
224  
225 if (value.Value != null)
226 {
227 string newLine = (i < values.Length - 1) ? "\n" : String.Empty;
228 output.AppendFormat("{0} {1} {2} {3} {4}{5}", value.Name, TypeStrings[(int)value.Type],
229 ClassStrings[(int)value.Class], SendtoStrings[(int)value.Sendto], value.Value.ToString(), newLine);
230 }
231 }
232  
233 return output.ToString();
234 }
235  
236 private void SetValue(string value)
237 {
238 switch (Type)
239 {
240 case ValueType.Asset:
241 case ValueType.String:
242 Value = value;
243 break;
244 case ValueType.F32:
245 {
246 float temp;
247 Utils.TryParseSingle(value, out temp);
248 Value = temp;
249 break;
250 }
251 case ValueType.S32:
252 {
253 int temp;
254 Int32.TryParse(value, out temp);
255 Value = temp;
256 break;
257 }
258 case ValueType.U32:
259 {
260 uint temp;
261 UInt32.TryParse(value, out temp);
262 Value = temp;
263 break;
264 }
265 case ValueType.U64:
266 {
267 ulong temp;
268 UInt64.TryParse(value, out temp);
269 Value = temp;
270 break;
271 }
272 case ValueType.VEC3:
273 {
274 Vector3 temp;
275 Vector3.TryParse(value, out temp);
276 Value = temp;
277 break;
278 }
279 default:
280 Value = null;
281 break;
282 }
283 }
284  
285 private static ValueType GetValueType(string value)
286 {
287 ValueType type = ValueType.Unknown;
288  
289 for (int i = 0; i < TypeStrings.Length; i++)
290 {
291 if (value == TypeStrings[i])
292 {
293 type = (ValueType)i;
294 break;
295 }
296 }
297  
298 if (type == ValueType.Unknown)
299 type = ValueType.String;
300  
301 return type;
302 }
303  
304 private static ClassType GetClassType(string value)
305 {
306 ClassType type = ClassType.Unknown;
307  
308 for (int i = 0; i < ClassStrings.Length; i++)
309 {
310 if (value == ClassStrings[i])
311 {
312 type = (ClassType)i;
313 break;
314 }
315 }
316  
317 if (type == ClassType.Unknown)
318 type = ClassType.ReadOnly;
319  
320 return type;
321 }
322  
323 private static SendtoType GetSendtoType(string value)
324 {
325 SendtoType type = SendtoType.Unknown;
326  
327 for (int i = 0; i < SendtoStrings.Length; i++)
328 {
329 if (value == SendtoStrings[i])
330 {
331 type = (SendtoType)i;
332 break;
333 }
334 }
335  
336 if (type == SendtoType.Unknown)
337 type = SendtoType.Sim;
338  
339 return type;
340 }
341 }
342 }