Torque3D Documentation / _generateds / propertyParsing.cpp

propertyParsing.cpp

Engine/source/console/propertyParsing.cpp

More...

Namespaces:

namespace

Public Variables

Detailed Description

Public Variables

ExprEvalState gEvalState 
  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23#include "core/strings/stringFunctions.h"
 24#include "core/strings/stringUnit.h"
 25#include "console/consoleInternal.h"
 26#include "core/color.h"
 27#include "console/consoleTypes.h"
 28#include "math/mPoint2.h"
 29#include "math/mPoint3.h"
 30#include "math/mPoint4.h"
 31#include "math/mRect.h"
 32#include "math/mBox.h"
 33#include "math/mQuat.h"
 34#include "math/mAngAxis.h"
 35#include "math/mMatrix.h"
 36// Property system includes:
 37#include "console/propertyParsing.h"
 38
 39extern ExprEvalState gEvalState;
 40
 41namespace PropertyInfo
 42{
 43   //-----------------------------------------------------------------------------
 44   // Bool
 45   //-----------------------------------------------------------------------------
 46   bool default_scan(const String &data, bool & result)
 47   {
 48      result = dAtob(data.c_str());
 49      return true;
 50   }
 51
 52   bool default_print(String & result, bool const & data)
 53   {
 54      if(data)
 55         result = String("1");
 56      else
 57         result = String("0");
 58      return true;
 59   }
 60
 61   //-----------------------------------------------------------------------------
 62   // F32/U32/S32
 63   //-----------------------------------------------------------------------------
 64   bool default_scan(const String &data, F32 & result)
 65   {
 66      result = dAtof(data.c_str());
 67      return true;
 68   }
 69
 70   bool default_print(String & result, F32 const & data)
 71   {
 72      result = String::ToString(data);
 73      return true;
 74   }
 75
 76   bool default_scan(const String &data, U32 & result)
 77   {
 78      result = dAtoi(data.c_str());
 79      return true;
 80   }
 81
 82   bool default_print(String & result, U32 const & data)
 83   {
 84      result = String::ToString(data);
 85      return true;
 86   }
 87
 88   bool default_scan(const String &data, S32 & result)
 89   {
 90      result = dAtoi(data.c_str());
 91      return true;
 92   }
 93
 94   bool default_print(String & result, S32 const & data)
 95   {
 96      result = String::ToString(data);
 97      return true;
 98   }
 99
100   //-----------------------------------------------------------------------------
101   // Basic Vector Types
102   //-----------------------------------------------------------------------------
103   template <typename T>
104   inline void default_vector_scan(const String &data, Vector<T> & result)
105   {
106      result.clear();
107      for(S32 i = 0; i < StringUnit::getUnitCount(data, " \t\n"); i++)
108         result.push_back(dAtof(StringUnit::getUnit(data, i, " \t\n")));
109   }
110
111   template <typename T>
112   inline void default_vector_print(String & result, Vector<T> const & data)
113   {
114      result = String("");
115      S32 items = data.size();
116      for(S32 i = 0; i < items; i++)
117      {
118         result += String::ToString(data[i]);
119         if(i < items-1)
120            result += String(" ");
121      }
122   }
123
124   bool default_scan(const String &data, Vector<F32> & result)
125   {
126      default_vector_scan(data,result);
127      return true;
128   }
129
130   bool default_print(String & result, Vector<F32> const & data)
131   {
132      default_vector_print<F32>(result,data);
133      return true;
134   }
135
136   bool default_scan(const String &data, Vector<U32> & result)
137   {
138      default_vector_scan(data,result);
139      return true;
140   }
141
142   bool default_print(String & result, Vector<U32> const & data)
143   {
144      default_vector_print<U32>(result,data);
145      return true;
146   }
147
148   bool default_scan(const String &data, Vector<S32> & result)
149   {
150      default_vector_scan(data,result);
151      return true;
152   }
153
154   bool default_print(String & result, Vector<S32> const & data)
155   {
156      default_vector_print<S32>(result,data);
157      return true;
158   }
159
160   //-----------------------------------------------------------------------------
161   // Math - Points
162   //-----------------------------------------------------------------------------
163   bool default_scan(const String &data, Point2F & result)
164   {
165      dSscanf(data.c_str(),"%g %g",&result.x,&result.y);
166      return true;
167   }
168
169   bool default_print(String & result, Point2F const & data)
170   {
171      result = String::ToString("%g %g",data.x,data.y);
172      return true;
173   }
174
175   bool default_scan(const String &data, Point2I & result)
176   {
177      // Handle passed as floating point from script
178      if(data.find('.') != String::NPos)
179      {
180         Point2F tempResult;
181         dSscanf(data.c_str(),"%f %f",&tempResult.x,&tempResult.y);
182         result.x = mFloor(tempResult.x);
183         result.y = mFloor(tempResult.y);
184      }
185      else
186         dSscanf(data.c_str(),"%d %d",&result.x,&result.y);
187      return true;
188   }
189
190   bool default_print(String & result, Point2I const & data)
191   {
192      result = String::ToString("%d %d",data.x,data.y);
193      return true;
194   }
195
196   bool default_scan(const String &data, Point3F & result)
197   {
198      dSscanf(data.c_str(),"%g %g %g",&result.x,&result.y,&result.z);
199      return true;
200   }
201
202   bool default_print(String & result, Point3F const & data)
203   {
204      result = String::ToString("%g %g %g",data.x,data.y,data.z);
205      return true;
206   }
207
208   bool default_scan(const String &data, Point3I & result)
209   {
210      // Handle passed as floating point from script
211      if(data.find('.') != String::NPos)
212      {
213         Point3F tempResult;
214         dSscanf(data.c_str(),"%f %f %f",&tempResult.x,&tempResult.y,&tempResult.z);
215         result.x = mFloor(tempResult.x);
216         result.y = mFloor(tempResult.y);
217         result.z = mFloor(tempResult.z);
218      }
219      else
220         dSscanf(data.c_str(),"%d %d %d",&result.x,&result.y,&result.z);
221      return true;
222   }
223
224   bool default_print(String & result, Point3I const & data)
225   {
226      result = String::ToString("%d %d %d",data.x,data.y,data.z);
227      return true;
228   }
229
230   bool default_scan(const String &data, Point4F & result)
231   {
232      dSscanf(data.c_str(),"%g %g %g %g",&result.x,&result.y,&result.z,&result.w);
233      return true;
234   }
235
236   bool default_print(String & result, Point4F const & data)
237   {
238      result = String::ToString("%g %g %g %g",data.x,data.y,data.z,data.w);
239      return true;
240   }
241
242   bool default_scan(const String &data, Point4I & result)
243   {
244      // Handle passed as floating point from script
245      if(data.find('.') != String::NPos)
246      {
247         Point4F tempResult;
248         dSscanf(data.c_str(),"%f %f %f %f",&tempResult.x,&tempResult.y,&tempResult.z,&tempResult.w);
249         result.x = mFloor(tempResult.x);
250         result.y = mFloor(tempResult.y);
251         result.z = mFloor(tempResult.z);
252         result.w = mFloor(tempResult.w);
253      }
254      else
255         dSscanf(data.c_str(),"%d %d %d %d",&result.x,&result.y,&result.z,&result.w);
256      return true;      
257   }
258
259   bool default_print(String & result, const Point4I & data)
260   {
261      result = String::ToString("%d %d %d %d", data.x, data.y, data.z, data.w);
262      return true;
263   }
264
265   //-----------------------------------------------------------------------------
266   // Math - Rectangles and boxes
267   //-----------------------------------------------------------------------------
268   bool default_scan( const String &data, RectI & result )
269   {
270      // Handle passed as floating point from script
271      if(data.find('.') != String::NPos)
272      {
273         RectF tempResult;
274         dSscanf(data.c_str(),"%f %f %f %f",&tempResult.point.x,&tempResult.point.y,&tempResult.extent.x,&tempResult.extent.y);
275         result.point.x = mFloor(tempResult.point.x);
276         result.point.y = mFloor(tempResult.point.y);
277         result.extent.x = mFloor(tempResult.extent.x);
278         result.extent.y = mFloor(tempResult.extent.y);
279      }
280      else
281         dSscanf(data.c_str(),"%d %d %d %d",&result.point.x,&result.point.y,&result.extent.x,&result.extent.y);
282      return true;
283   }
284   bool default_print( String & result, const RectI & data )
285   {
286      result = String::ToString("%i %i %i %i",data.point.x,data.point.y,data.extent.x,data.extent.y);
287      return true;
288   }
289
290   bool default_scan(const String &data, RectF & result)
291   {
292      dSscanf(data.c_str(),"%g %g %g %g",&result.point.x,&result.point.y,&result.extent.x,&result.extent.y);
293      return true;
294   }
295
296   bool default_print(String & result, const RectF & data)
297   {
298      result = String::ToString("%g %g %g %g",data.point.x,data.point.y,data.extent.x,data.extent.y);
299      return true;
300   }
301
302   bool default_scan(const String &data, Box3F & result)
303   {
304      dSscanf(data.c_str(),"%g %g %g %g %g %g",
305         &result.minExtents.x,&result.minExtents.y,&result.minExtents.z,
306         &result.maxExtents.x,&result.maxExtents.y,&result.maxExtents.z);
307      return true;
308   }
309
310   bool default_print(String & result, const Box3F & data)
311   {
312      result = String::ToString("%g %g %g %g %g %g",
313         data.minExtents.x,data.minExtents.y,data.minExtents.z,
314         data.maxExtents.x,data.maxExtents.y,data.maxExtents.z);
315      return true;
316   }
317
318   //-----------------------------------------------------------------------------
319
320   bool default_scan( const String &data, AngAxisF & result )
321   {
322      if(StringUnit::getUnitCount(data," ") < 4)
323         return false;
324
325      dSscanf(data.c_str(),"%g %g %g %g", &result.axis.x,&result.axis.y,&result.axis.z,&result.angle);
326      result.angle = mDegToRad(result.angle);
327      return true;
328   }
329
330   bool default_print( String & result, const AngAxisF & data )
331   {
332      F32 angle = mRadToDeg(data.angle);
333      angle = mFmod(angle + 360.0f,360.0f);
334      result = String::ToString("%g %g %g %g", data.axis.x, data.axis.y, data.axis.z, angle);
335      return true;
336   }
337
338   bool default_scan( const String &data, QuatF & result )
339   {
340      if(StringUnit::getUnitCount(data," ") < 4)
341         return false;
342
343      dSscanf(data.c_str(),"%g %g %g %g", &result.x,&result.y,&result.z,&result.w);
344      return true;
345   }
346
347   bool default_print( String & result, const QuatF & data )
348   {
349      result = String::ToString("%g %g %g %g", data.x, data.y, data.z, data.w);
350      return true;
351   }
352
353   bool default_scan( const String &data, MatrixF & result )
354   {
355      if(StringUnit::getUnitCount(data," ") < 16)
356         return false;
357
358      F32* m = result;
359      dSscanf(data.c_str(),"%g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g", 
360         &m[result.idx(0,0)], &m[result.idx(0,1)], &m[result.idx(0,2)], &m[result.idx(0,3)], 
361         &m[result.idx(1,0)], &m[result.idx(1,1)], &m[result.idx(1,2)], &m[result.idx(1,3)], 
362         &m[result.idx(2,0)], &m[result.idx(2,1)], &m[result.idx(2,2)], &m[result.idx(2,3)], 
363         &m[result.idx(3,0)], &m[result.idx(3,1)], &m[result.idx(3,2)], &m[result.idx(3,3)]);         
364      return true;
365   }
366
367   bool default_print( String & result, const MatrixF & data )
368   {
369      const F32* m = data;
370      result = String::ToString("%g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g",
371         m[data.idx(0,0)], m[data.idx(0,1)], m[data.idx(0,2)], m[data.idx(0,3)], 
372         m[data.idx(1,0)], m[data.idx(1,1)], m[data.idx(1,2)], m[data.idx(1,3)], 
373         m[data.idx(2,0)], m[data.idx(2,1)], m[data.idx(2,2)], m[data.idx(2,3)], 
374         m[data.idx(3,0)], m[data.idx(3,1)], m[data.idx(3,2)], m[data.idx(3,3)]);
375      return true;
376   }
377
378   //-----------------------------------------------------------------------------
379   // Colors
380   //-----------------------------------------------------------------------------
381   bool default_scan(const String &data, LinearColorF & result)
382   {
383      if(StringUnit::getUnitCount(data," ") == 3)
384      {
385         dSscanf(data.c_str(),"%g %g %g",&result.red,&result.green,&result.blue);
386         result.alpha = 1.0f;
387      }
388      else
389         dSscanf(data.c_str(),"%g %g %g %g",&result.red,&result.green,&result.blue,&result.alpha);
390      return true;
391   }
392
393   bool default_print(String & result, LinearColorF const & data)
394   {
395      if(data.alpha == 1.0f)
396         result = String::ToString("%g %g %g",data.red,data.green,data.blue);
397      else
398         result = String::ToString("%g %g %g %g",data.red,data.green,data.blue,data.alpha);
399      return true;
400   }
401
402   bool default_scan(const String &data, ColorI & result)
403   {
404      if(StringUnit::getUnitCount(data," ") == 3)
405      {
406         S32 r,g,b;
407         dSscanf(data.c_str(),"%i %i %i",&r,&g,&b);
408         result.set(r,g,b);
409      }
410      else
411      {
412         S32 r,g,b,a;
413         dSscanf(data.c_str(),"%i %i %i %i",&r,&g,&b,&a);
414         result.set(r,g,b,a);
415      }
416      return true;
417   }
418
419   bool default_print(String & result, const ColorI & data)
420   {
421      if(data.alpha == 255)
422         result = String::ToString("%d %d %d",data.red,data.green,data.blue);
423      else
424         result = String::ToString("%d %d %d %d",data.red,data.green,data.blue,data.alpha);
425      return true;
426   }
427
428   //-----------------------------------------------------------------------------
429   // String
430   //-----------------------------------------------------------------------------
431   bool default_scan(const String &data, String & result)
432   {
433      result = data;
434      return true;
435   }
436
437   bool default_print(String & result, const String & data)
438   {
439      result = data;
440      return true;
441   }
442
443   //-----------------------------------------------------------------------------
444   // FileName
445   //-----------------------------------------------------------------------------
446   bool default_scan(const String &data, FileName & result)
447   {
448      char buffer[1024];
449
450      if(data.c_str()[0] == '$')
451      {
452         dStrncpy(buffer, data.c_str(), sizeof(buffer) - 1);
453         buffer[sizeof(buffer)-1] = 0;
454      }
455      else if (!Con::expandScriptFilename(buffer, sizeof(buffer), data))
456      {
457         Con::warnf("(TypeFilename) illegal filename detected: %s", data.c_str());
458         return false;
459      }
460      result = String(buffer);
461      return true;
462   }
463
464   bool default_print(String & result, const FileName & data)
465   {
466      result = data;
467      return true;
468   }
469
470   //-----------------------------------------------------------------------------
471   // SimObject
472   //-----------------------------------------------------------------------------
473   bool default_scan(const String &data, SimObject * & result)
474   {
475      result = Sim::findObject(data);
476      return result != NULL;
477   }
478
479   bool default_print(String & result, SimObject * const & data)
480   {
481      if(data)
482      {
483         if(String(data->getName()).isEmpty())
484            result = data->getIdString();
485         else
486            result = data->getName();
487         return true;
488      }
489      return false;
490   }
491
492   //-----------------------------------------------------------------------------
493   // Print scan ints of various sizes as hex
494   //-----------------------------------------------------------------------------
495
496   //-------
497   // 16 bit
498   //-------
499
500   bool hex_scan(const String & string, U32 & hex)
501   {
502      dSscanf(string.c_str(),"%i", &hex);
503      return true;
504   }
505
506   bool hex_print(String & string, const U32 & hex)
507   {
508      string = String::ToString("0x%X",hex);
509      return true;
510   }
511
512   bool hex_scan(const String & string, S32 & hex)
513   {
514      dSscanf(string.c_str(),"%i", &hex);
515      return true;
516   }
517
518   bool hex_print(String & string, const S32 & hex)
519   {
520      string = String::ToString("0x%X",hex);
521      return true;
522   }
523
524   //-------
525   // 16 bit
526   //-------
527
528   bool hex_scan(const String & string, U16 & hex)
529   {
530      U32 tmp;
531      bool ret = hex_scan(string,tmp);
532      hex = tmp;
533      return ret;
534   }
535
536   bool hex_print(String & string, const U16 & hex)
537   {
538      U32 tmp = hex;
539      return hex_print(string,tmp);
540   }
541
542   bool hex_scan(const String & string, S16 & hex)
543   {
544      S32 tmp;
545      bool ret = hex_scan(string,tmp);
546      hex = tmp;
547      return ret;
548   }
549
550   bool hex_print(String & string, const S16 & hex)
551   {
552      U32 tmp = hex;
553      return hex_print(string,tmp);
554   }
555
556   //-------
557   //  8 bit
558   //-------
559
560   bool hex_scan(const String & string, U8 & hex)
561   {
562      U32 tmp;
563      bool ret = hex_scan(string,tmp);
564      hex = tmp;
565      return ret;
566   }
567
568   bool hex_print(String & string, const U8 & hex)
569   {
570      U32 tmp = hex;
571      return hex_print(string,tmp);
572   }
573
574   bool hex_scan(const String & string, S8 & hex)
575   {
576      S32 tmp;
577      bool ret = hex_scan(string,tmp);
578      hex = tmp;
579      return ret;
580   }
581
582   bool hex_print(String & string, const S8 & hex)
583   {
584      U32 tmp = hex;
585      return hex_print(string,tmp);
586   }
587
588}
589