]> git.mxchange.org Git - flightgear.git/blobdiff - src/Scripting/NasalSys.cxx
Refactor FG_HOME init, so we can log sooner.
[flightgear.git] / src / Scripting / NasalSys.cxx
index 24b78b4e4d609a12383b02921b1472079315f700..6ae142a75ed123adecc74afaa6bce954b2eaf8a6 100644 (file)
@@ -3,6 +3,10 @@
 #  include "config.h"
 #endif
 
+#ifdef HAVE_WINDOWS_H
+#include <windows.h>
+#endif
+
 #ifdef HAVE_SYS_TIME_H
 #  include <sys/time.h>  // gettimeofday
 #endif
@@ -29,6 +33,7 @@
 #include "NasalCanvas.hxx"
 #include "NasalClipboard.hxx"
 #include "NasalCondition.hxx"
+#include "NasalString.hxx"
 
 #include <Main/globals.hxx>
 #include <Main/util.hxx>
@@ -37,6 +42,8 @@
 
 using std::map;
 
+void postinitNasalGUI(naRef globals, naContext c);
+
 static FGNasalSys* nasalSys = 0;
 
 // Listener class for loading Nasal modules on demand
@@ -98,6 +105,7 @@ FGNasalSys::FGNasalSys()
     nasalSys = this;
     _context = 0;
     _globals = naNil();
+    _string = naNil();
     _gcHash = naNil();
     _nextGCKey = 0; // Any value will do
     _callCount = 0;
@@ -152,6 +160,7 @@ FGNasalSys::~FGNasalSys()
 
     naFreeContext(_context);
     _globals = naNil();
+    _string = naNil();
 }
 
 bool FGNasalSys::parseAndRun(const char* sourceCode)
@@ -309,6 +318,27 @@ static naRef f_print(naContext c, naRef me, int argc, naRef* args)
     return naNum(buf.length());
 }
 
+// logprint() extension function.  Same as above, all arguments after the
+// first argument are concatenated. Argument 0 is the log-level, matching
+// sgDebugPriority.
+static naRef f_logprint(naContext c, naRef me, int argc, naRef* args)
+{
+  if (argc < 1)
+    naRuntimeError(c, "no prioirty argument to logprint()");
+  
+  naRef priority = args[0];
+  string buf;
+  int n = argc;
+  for(int i=1; i<n; i++) {
+    naRef s = naStringValue(c, args[i]);
+    if(naIsNil(s)) continue;
+    buf += naStr_data(s);
+  }
+  SG_LOG(SG_NASAL, (sgDebugPriority)(int) priority.num, buf);
+  return naNum(buf.length());
+}
+
+
 // fgcommand() extension function.  Executes a named command via the
 // FlightGear command manager.  Takes a single property node name as
 // an argument.
@@ -499,6 +529,7 @@ static struct { const char* name; naCFunction func; } funcs[] = {
     { "getprop",   f_getprop },
     { "setprop",   f_setprop },
     { "print",     f_print },
+    { "logprint",  f_logprint },
     { "_fgcommand", f_fgcommand },
     { "settimer",  f_settimer },
     { "_setlistener", f_setlistener },
@@ -545,8 +576,6 @@ void FGNasalSys::init()
         hashset(_globals, funcs[i].name,
                 naNewFunc(_context, naNewCCode(_context, funcs[i].func)));
 
-
-  
     // And our SGPropertyNode wrapper
     hashset(_globals, "props", genPropsModule());
 
@@ -556,9 +585,14 @@ void FGNasalSys::init()
     _gcHash = naNewHash(_context);
     hashset(_globals, "__gcsave", _gcHash);
 
+    // Add string methods
+    _string = naInit_string(_context);
+    naSave(_context, _string);
+    initNasalString(_globals, _string, _context, _gcHash);
+
     initNasalPositioned(_globals, _context, _gcHash);
-    initNasalCanvas(_globals, _context, _gcHash);
     NasalClipboard::init(this);
+    initNasalCanvas(_globals, _context, _gcHash);
     initNasalCondition(_globals, _context, _gcHash);
   
     // Now load the various source files in the Nasal directory
@@ -585,6 +619,7 @@ void FGNasalSys::init()
   
     // now Nasal modules are loaded, we can do some delayed work
     postinitNasalPositioned(_globals, _context);
+    postinitNasalGUI(_globals, _context);
 }
 
 void FGNasalSys::update(double)