aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/Console/ConsoleUtil.cs
blob: bfa05a21efe1e0240d0175b8b2baf800058cd708 (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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
 * Copyright (c) Contributors, http://opensimulator.org/
 * See CONTRIBUTORS.TXT for a full list of copyright holders.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSimulator Project nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using log4net;
using OpenMetaverse;

namespace OpenSim.Framework.Console
{
    public class ConsoleUtil
    {
    //    private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public const int LocalIdNotFound = 0;

        /// <summary>
        /// Used by modules to display stock co-ordinate help, though possibly this should be under some general section
        /// rather than in each help summary.
        /// </summary>
        public const string CoordHelp
    = @"Each component of the coord is comma separated.  There must be no spaces between the commas.
    If you don't care about the z component you can simply omit it.
    If you don't care about the x or y components then you can leave them blank (though a comma is still required)
    If you want to specify the maximum value of a component then you can use ~ instead of a number
    If you want to specify the minimum value of a component then you can use -~ instead of a number
    e.g.
    show object pos 20,20,20 to 40,40,40
    delete object pos 20,20 to 40,40
    show object pos ,20,20 to ,40,40
    delete object pos ,,30 to ,,~
    show object pos ,,-~ to ,,30";

        public const string MinRawConsoleVectorValue = "-~";
        public const string MaxRawConsoleVectorValue = "~";

        public const string VectorSeparator = ",";
        public static char[] VectorSeparatorChars = VectorSeparator.ToCharArray();

        /// <summary>
        /// Check if the given file path exists.
        /// </summary>
        /// <remarks>If not, warning is printed to the given console.</remarks>
        /// <returns>true if the file does not exist, false otherwise.</returns>
        /// <param name='console'></param>
        /// <param name='path'></param>
        public static bool CheckFileDoesNotExist(ICommandConsole console, string path)
        {
            if (File.Exists(path))
            {
                console.OutputFormat("File {0} already exists.  Please move or remove it.", path);
                return false;
            }

            return true;
        }

        /// <summary>
        /// Try to parse a console UUID from the console.
        /// </summary>
        /// <remarks>
        /// Will complain to the console if parsing fails.
        /// </remarks>
        /// <returns></returns>
        /// <param name='console'>If null then no complaint is printed.</param>
        /// <param name='rawUuid'></param>
        /// <param name='uuid'></param>
        public static bool TryParseConsoleUuid(ICommandConsole console, string rawUuid, out UUID uuid)
        {
            if (!UUID.TryParse(rawUuid, out uuid))
            {
                if (console != null)
                    console.OutputFormat("ERROR: {0} is not a valid uuid", rawUuid);

                return false;
            }

            return true;
        }

        public static bool TryParseConsoleLocalId(ICommandConsole console, string rawLocalId, out uint localId)
        {
            if (!uint.TryParse(rawLocalId, out localId))
            {
                if (console != null)
                    console.OutputFormat("ERROR: {0} is not a valid local id", localId);

                return false;
            }

            if (localId == 0)
            {
                if (console != null)
                    console.OutputFormat("ERROR: {0} is not a valid local id - it must be greater than 0", localId);

                return false;
            }

            return true;
        }

        /// <summary>
        /// Tries to parse the input as either a UUID or a local ID.
        /// </summary>
        /// <returns>true if parsing succeeded, false otherwise.</returns>
        /// <param name='console'></param>
        /// <param name='rawId'></param>
        /// <param name='uuid'></param>
        /// <param name='localId'>
        /// Will be set to ConsoleUtil.LocalIdNotFound if parsing result was a UUID or no parse succeeded.
        /// </param>
        public static bool TryParseConsoleId(ICommandConsole console, string rawId, out UUID uuid, out uint localId)
        {
            if (TryParseConsoleUuid(null, rawId, out uuid))
            {
                localId = LocalIdNotFound;
                return true;
            }

            if (TryParseConsoleLocalId(null, rawId, out localId))
            {
                return true;
            }

            if (console != null)
                console.OutputFormat("ERROR: {0} is not a valid UUID or local id", rawId);

            return false;
        }

        /// <summary>
        /// Convert a console input to a bool, automatically complaining if a console is given.
        /// </summary>
        /// <param name='console'>Can be null if no console is available.</param>
        /// <param name='rawConsoleVector'>/param>
        /// <param name='vector'></param>
        /// <returns></returns>
        public static bool TryParseConsoleBool(ICommandConsole console, string rawConsoleString, out bool b)
        {
            if (!bool.TryParse(rawConsoleString, out b))
            {
                if (console != null)
                    console.OutputFormat("ERROR: {0} is not a true or false value", rawConsoleString);

                return false;
            }

            return true;
        }

        /// <summary>
        /// Convert a console input to an int, automatically complaining if a console is given.
        /// </summary>
        /// <param name='console'>Can be null if no console is available.</param>
        /// <param name='rawConsoleInt'>/param>
        /// <param name='i'></param>
        /// <returns></returns>
        public static bool TryParseConsoleInt(ICommandConsole console, string rawConsoleInt, out int i)
        {
            if (!int.TryParse(rawConsoleInt, out i))
            {
                if (console != null)
                    console.OutputFormat("ERROR: {0} is not a valid integer", rawConsoleInt);

                return false;
            }

            return true;
        }

        /// <summary>
        /// Convert a console input to a float, automatically complaining if a console is given.
        /// </summary>
        /// <param name='console'>Can be null if no console is available.</param>
        /// <param name='rawConsoleInput'>/param>
        /// <param name='i'></param>
        /// <returns></returns>
        public static bool TryParseConsoleFloat(ICommandConsole console, string rawConsoleInput, out float i)
        {
            if (!float.TryParse(rawConsoleInput, out i))
            {
                if (console != null)
                    console.OutputFormat("ERROR: {0} is not a valid float", rawConsoleInput);

                return false;
            }

            return true;
        }

        /// <summary>
        /// Convert a console input to a double, automatically complaining if a console is given.
        /// </summary>
        /// <param name='console'>Can be null if no console is available.</param>
        /// <param name='rawConsoleInput'>/param>
        /// <param name='i'></param>
        /// <returns></returns>
        public static bool TryParseConsoleDouble(ICommandConsole console, string rawConsoleInput, out double i)
        {
            if (!double.TryParse(rawConsoleInput, out i))
            {
                if (console != null)
                    console.OutputFormat("ERROR: {0} is not a valid double", rawConsoleInput);

                return false;
            }

            return true;
        }

        /// <summary>
        /// Convert a console integer to a natural int, automatically complaining if a console is given.
        /// </summary>
        /// <param name='console'>Can be null if no console is available.</param>
        /// <param name='rawConsoleInt'>/param>
        /// <param name='i'></param>
        /// <returns></returns>
        public static bool TryParseConsoleNaturalInt(ICommandConsole console, string rawConsoleInt, out int i)
        {
            if (TryParseConsoleInt(console, rawConsoleInt, out i))
            {
                if (i < 0)
                {
                    if (console != null)
                        console.OutputFormat("ERROR: {0} is not a positive integer", rawConsoleInt);

                    return false;
                }

                return true;
            }

            return false;
        }

        /// <summary>
        /// Convert a minimum vector input from the console to an OpenMetaverse.Vector3
        /// </summary>
        /// <param name='rawConsoleVector'>/param>
        /// <param name='vector'></param>
        /// <returns></returns>
        public static bool TryParseConsoleMinVector(string rawConsoleVector, out Vector3 vector)
        {
            return TryParseConsoleVector(rawConsoleVector, c => float.MinValue.ToString(), out vector);
        }

        /// <summary>
        /// Convert a maximum vector input from the console to an OpenMetaverse.Vector3
        /// </summary>
        /// <param name='rawConsoleVector'>/param>
        /// <param name='vector'></param>
        /// <returns></returns>
        public static bool TryParseConsoleMaxVector(string rawConsoleVector, out Vector3 vector)
        {
            return TryParseConsoleVector(rawConsoleVector, c => float.MaxValue.ToString(), out vector);
        }

        /// <summary>
        /// Convert a vector input from the console to an OpenMetaverse.Vector3
        /// </summary>
        /// <param name='rawConsoleVector'>
        /// A string in the form <x>,<y>,<z> where there is no space between values.
        /// Any component can be missing (e.g. ,,40).  blankComponentFunc is invoked to replace the blank with a suitable value
        /// Also, if the blank component is at the end, then the comma can be missed off entirely (e.g. 40,30 or 40)
        /// The strings "~" and "-~" are valid in components.  The first substitutes float.MaxValue whilst the second is float.MinValue
        /// Other than that, component values must be numeric.
        /// </param>
        /// <param name='blankComponentFunc'>
        /// Behaviour if component is blank.  If null then conversion fails on a blank component.
        /// </param>
        /// <param name='vector'></param>
        /// <returns></returns>
        public static bool TryParseConsoleVector(
            string rawConsoleVector, Func<string, string> blankComponentFunc, out Vector3 vector)
        {
            return Vector3.TryParse(CookVector(rawConsoleVector, 3, blankComponentFunc), out vector);
        }

        /// <summary>
        /// Convert a vector input from the console to an OpenMetaverse.Vector2
        /// </summary>
        /// <param name='rawConsoleVector'>
        /// A string in the form <x>,<y> where there is no space between values.
        /// Any component can be missing (e.g. ,40).  blankComponentFunc is invoked to replace the blank with a suitable value
        /// Also, if the blank component is at the end, then the comma can be missed off entirely (e.g. 40)
        /// The strings "~" and "-~" are valid in components.  The first substitutes float.MaxValue whilst the second is float.MinValue
        /// Other than that, component values must be numeric.
        /// </param>
        /// <param name='blankComponentFunc'>
        /// Behaviour if component is blank.  If null then conversion fails on a blank component.
        /// </param>
        /// <param name='vector'></param>
        /// <returns></returns>
        public static bool TryParseConsole2DVector(
            string rawConsoleVector, Func<string, string> blankComponentFunc, out Vector2 vector)
        {
            // We don't use Vector2.TryParse() for now because for some reason it expects an input with 3 components
            // rather than 2.
            string cookedVector = CookVector(rawConsoleVector, 2, blankComponentFunc);

            if (cookedVector == null)
            {
                vector = Vector2.Zero;

                return false;
            }
            else
            {
                string[] cookedComponents = cookedVector.Split(VectorSeparatorChars);

                vector = new Vector2(float.Parse(cookedComponents[0]), float.Parse(cookedComponents[1]));

                return true;
            }

            //return Vector2.TryParse(CookVector(rawConsoleVector, 2, blankComponentFunc), out vector);
        }

        /// <summary>
        /// Convert a raw console vector into a vector that can be be parsed by the relevant OpenMetaverse.TryParse()
        /// </summary>
        /// <param name='rawConsoleVector'></param>
        /// <param name='dimensions'></param>
        /// <param name='blankComponentFunc'></param>
        /// <returns>null if conversion was not possible</returns>
        private static string CookVector(
            string rawConsoleVector, int dimensions, Func<string, string> blankComponentFunc)
        {
            List<string> components = rawConsoleVector.Split(VectorSeparatorChars).ToList();

            if (components.Count < 1 || components.Count > dimensions)
                return null;

            if (components.Count < dimensions)
            {
                if (blankComponentFunc == null)
                    return null;
                else
                    for (int i = components.Count; i < dimensions; i++)
                        components.Add("");
            }

            List<string> cookedComponents
                = components.ConvertAll<string>(
                    c =>
                    {
                        if (c == "")
                            return blankComponentFunc.Invoke(c);
                        else if (c == MaxRawConsoleVectorValue)
                            return float.MaxValue.ToString();
                        else if (c == MinRawConsoleVectorValue)
                            return float.MinValue.ToString();
                        else
                            return c;
                    });

            return string.Join(VectorSeparator, cookedComponents.ToArray());
        }
    }
}