2006-02-18 13:58:09 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2003-12-01 14:35:49 +00:00
|
|
|
#include <simgear/nasal/nasal.h>
|
|
|
|
#include <simgear/props/props.hxx>
|
|
|
|
|
|
|
|
#include <Main/globals.hxx>
|
|
|
|
|
|
|
|
#include "NasalSys.hxx"
|
|
|
|
|
|
|
|
// Implementation of a Nasal wrapper for the SGPropertyNode class,
|
|
|
|
// using the Nasal "ghost" (er... Garbage collection Handle for
|
|
|
|
// OutSide Thingy) facility.
|
|
|
|
//
|
|
|
|
// Note that these functions appear in Nasal with prepended
|
|
|
|
// underscores. They work on the low-level "ghost" objects and aren't
|
|
|
|
// intended for use from user code, but from Nasal code you will find
|
|
|
|
// in props.nas. That is where the Nasal props.Node class is defined,
|
|
|
|
// which provides a saner interface along the lines of SGPropertyNode.
|
|
|
|
|
|
|
|
static void propNodeGhostDestroy(void* ghost)
|
|
|
|
{
|
|
|
|
SGPropertyNode_ptr* prop = (SGPropertyNode_ptr*)ghost;
|
|
|
|
delete prop;
|
|
|
|
}
|
|
|
|
|
|
|
|
naGhostType PropNodeGhostType = { propNodeGhostDestroy };
|
|
|
|
|
|
|
|
static naRef propNodeGhostCreate(naContext c, SGPropertyNode* n)
|
|
|
|
{
|
2003-12-08 02:05:10 +00:00
|
|
|
if(!n) return naNil();
|
2003-12-01 14:35:49 +00:00
|
|
|
SGPropertyNode_ptr* ghost = new SGPropertyNode_ptr(n);
|
|
|
|
return naNewGhost(c, &PropNodeGhostType, ghost);
|
|
|
|
}
|
|
|
|
|
|
|
|
naRef FGNasalSys::propNodeGhost(SGPropertyNode* handle)
|
|
|
|
{
|
|
|
|
return propNodeGhostCreate(_context, handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define NASTR(s) s ? naStr_fromdata(naNewString(c),(char*)(s),strlen(s)) : naNil()
|
|
|
|
|
|
|
|
//
|
|
|
|
// Standard header for the extension functions. It turns the "ghost"
|
|
|
|
// found in arg[0] into a SGPropertyNode_ptr*, and then "unwraps" the
|
|
|
|
// vector found in the second argument into a normal-looking args
|
|
|
|
// array. This allows the Nasal handlers to do things like:
|
|
|
|
// Node.getChild = func { _getChild(me.ghost, arg) }
|
|
|
|
//
|
2005-04-18 19:49:13 +00:00
|
|
|
#define NODEARG() \
|
|
|
|
if(argc < 2 || !naIsGhost(args[0]) || \
|
|
|
|
naGhost_type(args[0]) != &PropNodeGhostType) \
|
|
|
|
naRuntimeError(c, "bad argument to props function"); \
|
|
|
|
SGPropertyNode_ptr* node = (SGPropertyNode_ptr*)naGhost_ptr(args[0]); \
|
|
|
|
naRef argv = args[1]
|
|
|
|
|
|
|
|
static naRef f_getType(naContext c, naRef me, int argc, naRef* args)
|
2003-12-01 14:35:49 +00:00
|
|
|
{
|
|
|
|
NODEARG();
|
|
|
|
char* t = "unknown";
|
|
|
|
switch((*node)->getType()) {
|
|
|
|
case SGPropertyNode::NONE: t = "NONE"; break;
|
|
|
|
case SGPropertyNode::ALIAS: t = "ALIAS"; break;
|
|
|
|
case SGPropertyNode::BOOL: t = "BOOL"; break;
|
|
|
|
case SGPropertyNode::INT: t = "INT"; break;
|
|
|
|
case SGPropertyNode::LONG: t = "LONG"; break;
|
|
|
|
case SGPropertyNode::FLOAT: t = "FLOAT"; break;
|
|
|
|
case SGPropertyNode::DOUBLE: t = "DOUBLE"; break;
|
|
|
|
case SGPropertyNode::STRING: t = "STRING"; break;
|
|
|
|
case SGPropertyNode::UNSPECIFIED: t = "UNSPECIFIED"; break;
|
|
|
|
}
|
|
|
|
return NASTR(t);
|
|
|
|
}
|
|
|
|
|
2007-01-12 18:00:56 +00:00
|
|
|
static naRef f_getAttribute(naContext c, naRef me, int argc, naRef* args)
|
|
|
|
{
|
|
|
|
NODEARG();
|
|
|
|
naRef val = naVec_get(argv, 0);
|
|
|
|
char *a = naStr_data(val);
|
|
|
|
SGPropertyNode::Attribute attr;
|
|
|
|
if(!a) a = "";
|
|
|
|
if(!strcmp(a, "READ")) attr = SGPropertyNode::READ;
|
|
|
|
else if(!strcmp(a, "WRITE")) attr = SGPropertyNode::WRITE;
|
|
|
|
else if(!strcmp(a, "ARCHIVE")) attr = SGPropertyNode::ARCHIVE;
|
|
|
|
else if(!strcmp(a, "TRACE_READ")) attr = SGPropertyNode::TRACE_READ;
|
|
|
|
else if(!strcmp(a, "TRACE_WRITE")) attr = SGPropertyNode::TRACE_WRITE;
|
|
|
|
else if(!strcmp(a, "USERARCHIVE")) attr = SGPropertyNode::USERARCHIVE;
|
|
|
|
else if(!strcmp(a, "TIED")) {
|
|
|
|
return naNum((*node)->isTied());
|
|
|
|
} else {
|
|
|
|
naRuntimeError(c, "props.getAttribute() with invalid attribute");
|
|
|
|
return naNil();
|
|
|
|
}
|
|
|
|
return naNum((*node)->getAttribute(attr));
|
|
|
|
}
|
|
|
|
|
|
|
|
static naRef f_setAttribute(naContext c, naRef me, int argc, naRef* args)
|
|
|
|
{
|
|
|
|
NODEARG();
|
|
|
|
naRef val = naVec_get(argv, 0);
|
|
|
|
char *a = naStr_data(val);
|
|
|
|
SGPropertyNode::Attribute attr;
|
|
|
|
if(!a) a = "";
|
|
|
|
if(!strcmp(a, "READ")) attr = SGPropertyNode::READ;
|
|
|
|
else if(!strcmp(a, "WRITE")) attr = SGPropertyNode::WRITE;
|
|
|
|
else if(!strcmp(a, "ARCHIVE")) attr = SGPropertyNode::ARCHIVE;
|
|
|
|
else if(!strcmp(a, "TRACE_READ")) attr = SGPropertyNode::TRACE_READ;
|
|
|
|
else if(!strcmp(a, "TRACE_WRITE")) attr = SGPropertyNode::TRACE_WRITE;
|
|
|
|
else if(!strcmp(a, "USERARCHIVE")) attr = SGPropertyNode::USERARCHIVE;
|
|
|
|
else {
|
|
|
|
naRuntimeError(c, "props.setAttribute() with invalid attribute");
|
|
|
|
return naNil();
|
|
|
|
}
|
|
|
|
naRef ret = naNum((*node)->getAttribute(attr));
|
|
|
|
(*node)->setAttribute(attr, naTrue(naVec_get(argv, 1)) ? true : false);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-04-18 19:49:13 +00:00
|
|
|
static naRef f_getName(naContext c, naRef me, int argc, naRef* args)
|
2003-12-01 14:35:49 +00:00
|
|
|
{
|
|
|
|
NODEARG();
|
|
|
|
return NASTR((*node)->getName());
|
|
|
|
}
|
|
|
|
|
2005-04-18 19:49:13 +00:00
|
|
|
static naRef f_getIndex(naContext c, naRef me, int argc, naRef* args)
|
2003-12-01 14:35:49 +00:00
|
|
|
{
|
|
|
|
NODEARG();
|
|
|
|
return naNum((*node)->getIndex());
|
|
|
|
}
|
|
|
|
|
2005-04-18 19:49:13 +00:00
|
|
|
static naRef f_getValue(naContext c, naRef me, int argc, naRef* args)
|
2003-12-01 14:35:49 +00:00
|
|
|
{
|
|
|
|
NODEARG();
|
|
|
|
switch((*node)->getType()) {
|
|
|
|
case SGPropertyNode::BOOL: case SGPropertyNode::INT:
|
|
|
|
case SGPropertyNode::LONG: case SGPropertyNode::FLOAT:
|
|
|
|
case SGPropertyNode::DOUBLE:
|
|
|
|
return naNum((*node)->getDoubleValue());
|
|
|
|
case SGPropertyNode::STRING:
|
|
|
|
case SGPropertyNode::UNSPECIFIED:
|
|
|
|
return NASTR((*node)->getStringValue());
|
2006-08-08 18:32:17 +00:00
|
|
|
default:
|
|
|
|
return naNil();
|
2003-12-01 14:35:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-18 19:49:13 +00:00
|
|
|
static naRef f_setValue(naContext c, naRef me, int argc, naRef* args)
|
2003-12-01 14:35:49 +00:00
|
|
|
{
|
|
|
|
NODEARG();
|
2005-04-18 19:49:13 +00:00
|
|
|
naRef val = naVec_get(argv, 0);
|
2003-12-01 14:35:49 +00:00
|
|
|
if(naIsString(val)) (*node)->setStringValue(naStr_data(val));
|
2006-10-17 19:58:33 +00:00
|
|
|
else {
|
|
|
|
naRef n = naNumValue(val);
|
|
|
|
if(naIsNil(n))
|
|
|
|
naRuntimeError(c, "props.setValue() with non-number");
|
|
|
|
(*node)->setDoubleValue(naNumValue(val).num);
|
|
|
|
}
|
2003-12-01 14:35:49 +00:00
|
|
|
return naNil();
|
|
|
|
}
|
|
|
|
|
2005-04-18 19:49:13 +00:00
|
|
|
static naRef f_setIntValue(naContext c, naRef me, int argc, naRef* args)
|
2003-12-01 14:35:49 +00:00
|
|
|
{
|
|
|
|
NODEARG();
|
2003-12-16 05:39:04 +00:00
|
|
|
// Original code:
|
2005-04-18 19:49:13 +00:00
|
|
|
// int iv = (int)naNumValue(naVec_get(argv, 0)).num;
|
2003-12-16 05:39:04 +00:00
|
|
|
|
|
|
|
// Junk to pacify the gcc-2.95.3 optimizer:
|
2005-04-18 19:49:13 +00:00
|
|
|
naRef tmp0 = naVec_get(argv, 0);
|
2003-12-16 05:39:04 +00:00
|
|
|
naRef tmp1 = naNumValue(tmp0);
|
2006-10-17 19:58:33 +00:00
|
|
|
if(naIsNil(tmp1))
|
|
|
|
naRuntimeError(c, "props.setIntValue() with non-number");
|
2003-12-16 05:39:04 +00:00
|
|
|
double tmp2 = tmp1.num;
|
|
|
|
int iv = (int)tmp2;
|
|
|
|
|
2003-12-01 14:35:49 +00:00
|
|
|
(*node)->setIntValue(iv);
|
|
|
|
return naNil();
|
|
|
|
}
|
|
|
|
|
2005-04-18 19:49:13 +00:00
|
|
|
static naRef f_setBoolValue(naContext c, naRef me, int argc, naRef* args)
|
2003-12-01 14:35:49 +00:00
|
|
|
{
|
|
|
|
NODEARG();
|
2005-04-18 19:49:13 +00:00
|
|
|
naRef val = naVec_get(argv, 0);
|
2003-12-01 14:35:49 +00:00
|
|
|
(*node)->setBoolValue(naTrue(val) ? true : false);
|
|
|
|
return naNil();
|
|
|
|
}
|
|
|
|
|
2005-04-18 19:49:13 +00:00
|
|
|
static naRef f_setDoubleValue(naContext c, naRef me, int argc, naRef* args)
|
2003-12-01 14:35:49 +00:00
|
|
|
{
|
|
|
|
NODEARG();
|
2006-10-17 19:58:33 +00:00
|
|
|
naRef r = naNumValue(naVec_get(argv, 0));
|
|
|
|
if(naIsNil(r))
|
|
|
|
naRuntimeError(c, "props.setDoubleValue() with non-number");
|
|
|
|
(*node)->setDoubleValue(r.num);
|
2003-12-01 14:35:49 +00:00
|
|
|
return naNil();
|
|
|
|
}
|
|
|
|
|
2005-04-18 19:49:13 +00:00
|
|
|
static naRef f_getParent(naContext c, naRef me, int argc, naRef* args)
|
2003-12-01 14:35:49 +00:00
|
|
|
{
|
|
|
|
NODEARG();
|
|
|
|
SGPropertyNode* n = (*node)->getParent();
|
|
|
|
if(!n) return naNil();
|
|
|
|
return propNodeGhostCreate(c, n);
|
|
|
|
}
|
|
|
|
|
2005-04-18 19:49:13 +00:00
|
|
|
static naRef f_getChild(naContext c, naRef me, int argc, naRef* args)
|
2003-12-01 14:35:49 +00:00
|
|
|
{
|
|
|
|
NODEARG();
|
2005-04-18 19:49:13 +00:00
|
|
|
naRef child = naVec_get(argv, 0);
|
2003-12-01 14:35:49 +00:00
|
|
|
if(!naIsString(child)) return naNil();
|
2005-04-18 19:49:13 +00:00
|
|
|
naRef idx = naNumValue(naVec_get(argv, 1));
|
2006-01-27 20:43:40 +00:00
|
|
|
bool create = naTrue(naVec_get(argv, 2));
|
2003-12-29 22:52:21 +00:00
|
|
|
SGPropertyNode* n;
|
2006-05-23 18:55:38 +00:00
|
|
|
try {
|
|
|
|
if(naIsNil(idx) || !naIsNum(idx)) {
|
|
|
|
n = (*node)->getChild(naStr_data(child), create);
|
|
|
|
} else {
|
|
|
|
n = (*node)->getChild(naStr_data(child), (int)idx.num, create);
|
|
|
|
}
|
|
|
|
} catch (const string& err) {
|
|
|
|
naRuntimeError(c, (char *)err.c_str());
|
|
|
|
return naNil();
|
2003-12-29 22:52:21 +00:00
|
|
|
}
|
2003-12-01 14:35:49 +00:00
|
|
|
if(!n) return naNil();
|
|
|
|
return propNodeGhostCreate(c, n);
|
|
|
|
}
|
|
|
|
|
2005-04-18 19:49:13 +00:00
|
|
|
static naRef f_getChildren(naContext c, naRef me, int argc, naRef* args)
|
2003-12-01 14:35:49 +00:00
|
|
|
{
|
|
|
|
NODEARG();
|
|
|
|
naRef result = naNewVector(c);
|
2005-04-18 19:49:13 +00:00
|
|
|
if(naIsNil(argv) || naVec_size(argv) == 0) {
|
2003-12-01 14:35:49 +00:00
|
|
|
// Get all children
|
|
|
|
for(int i=0; i<(*node)->nChildren(); i++)
|
|
|
|
naVec_append(result, propNodeGhostCreate(c, (*node)->getChild(i)));
|
|
|
|
} else {
|
|
|
|
// Get all children of a specified name
|
2005-04-18 19:49:13 +00:00
|
|
|
naRef name = naVec_get(argv, 0);
|
2003-12-01 14:35:49 +00:00
|
|
|
if(!naIsString(name)) return naNil();
|
2006-05-23 18:55:38 +00:00
|
|
|
try {
|
|
|
|
vector<SGPropertyNode_ptr> children
|
|
|
|
= (*node)->getChildren(naStr_data(name));
|
|
|
|
for(unsigned int i=0; i<children.size(); i++)
|
|
|
|
naVec_append(result, propNodeGhostCreate(c, children[i]));
|
|
|
|
} catch (const string& err) {
|
|
|
|
naRuntimeError(c, (char *)err.c_str());
|
|
|
|
return naNil();
|
|
|
|
}
|
2003-12-01 14:35:49 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2005-04-18 19:49:13 +00:00
|
|
|
static naRef f_removeChild(naContext c, naRef me, int argc, naRef* args)
|
2003-12-01 14:35:49 +00:00
|
|
|
{
|
|
|
|
NODEARG();
|
2005-04-18 19:49:13 +00:00
|
|
|
naRef child = naVec_get(argv, 0);
|
|
|
|
naRef index = naVec_get(argv, 1);
|
2003-12-01 14:35:49 +00:00
|
|
|
if(!naIsString(child) || !naIsNum(index)) return naNil();
|
2006-05-23 18:55:38 +00:00
|
|
|
try {
|
|
|
|
(*node)->removeChild(naStr_data(child), (int)index.num, false);
|
|
|
|
} catch (const string& err) {
|
|
|
|
naRuntimeError(c, (char *)err.c_str());
|
|
|
|
}
|
2003-12-01 14:35:49 +00:00
|
|
|
return naNil();
|
|
|
|
}
|
|
|
|
|
2005-10-23 16:10:32 +00:00
|
|
|
static naRef f_removeChildren(naContext c, naRef me, int argc, naRef* args)
|
|
|
|
{
|
|
|
|
NODEARG();
|
|
|
|
naRef result = naNewVector(c);
|
|
|
|
if(naIsNil(argv) || naVec_size(argv) == 0) {
|
|
|
|
// Remove all children
|
|
|
|
for(int i = (*node)->nChildren() - 1; i >=0; i--)
|
|
|
|
naVec_append(result, propNodeGhostCreate(c, (*node)->removeChild(i)));
|
|
|
|
} else {
|
|
|
|
// Remove all children of a specified name
|
|
|
|
naRef name = naVec_get(argv, 0);
|
|
|
|
if(!naIsString(name)) return naNil();
|
2006-05-23 18:55:38 +00:00
|
|
|
try {
|
|
|
|
vector<SGPropertyNode_ptr> children
|
|
|
|
= (*node)->removeChildren(naStr_data(name), false);
|
|
|
|
for(unsigned int i=0; i<children.size(); i++)
|
|
|
|
naVec_append(result, propNodeGhostCreate(c, children[i]));
|
|
|
|
} catch (const string& err) {
|
|
|
|
naRuntimeError(c, (char *)err.c_str());
|
|
|
|
return naNil();
|
|
|
|
}
|
2005-10-23 16:10:32 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2005-04-18 19:49:13 +00:00
|
|
|
static naRef f_getNode(naContext c, naRef me, int argc, naRef* args)
|
2003-12-01 14:35:49 +00:00
|
|
|
{
|
|
|
|
NODEARG();
|
2005-04-18 19:49:13 +00:00
|
|
|
naRef path = naVec_get(argv, 0);
|
|
|
|
bool create = naTrue(naVec_get(argv, 1));
|
2003-12-01 14:35:49 +00:00
|
|
|
if(!naIsString(path)) return naNil();
|
2006-05-23 18:55:38 +00:00
|
|
|
SGPropertyNode* n;
|
|
|
|
try {
|
|
|
|
n = (*node)->getNode(naStr_data(path), create);
|
|
|
|
} catch (const string& err) {
|
|
|
|
naRuntimeError(c, (char *)err.c_str());
|
|
|
|
return naNil();
|
|
|
|
}
|
2003-12-01 14:35:49 +00:00
|
|
|
return propNodeGhostCreate(c, n);
|
|
|
|
}
|
|
|
|
|
2005-04-18 19:49:13 +00:00
|
|
|
static naRef f_new(naContext c, naRef me, int argc, naRef* args)
|
2003-12-01 14:35:49 +00:00
|
|
|
{
|
|
|
|
return propNodeGhostCreate(c, new SGPropertyNode());
|
|
|
|
}
|
|
|
|
|
2005-04-18 19:49:13 +00:00
|
|
|
static naRef f_globals(naContext c, naRef me, int argc, naRef* args)
|
2003-12-01 14:35:49 +00:00
|
|
|
{
|
|
|
|
return propNodeGhostCreate(c, globals->get_props());
|
|
|
|
}
|
|
|
|
|
2004-12-18 11:59:33 +00:00
|
|
|
static struct {
|
2003-12-01 14:35:49 +00:00
|
|
|
naCFunction func;
|
|
|
|
char* name;
|
|
|
|
} propfuncs[] = {
|
|
|
|
{ f_getType, "_getType" },
|
2007-01-12 18:00:56 +00:00
|
|
|
{ f_getAttribute, "_getAttribute" },
|
|
|
|
{ f_setAttribute, "_setAttribute" },
|
2003-12-01 14:35:49 +00:00
|
|
|
{ f_getName, "_getName" },
|
|
|
|
{ f_getIndex, "_getIndex" },
|
|
|
|
{ f_getValue, "_getValue" },
|
|
|
|
{ f_setValue, "_setValue" },
|
|
|
|
{ f_setIntValue, "_setIntValue" },
|
|
|
|
{ f_setBoolValue, "_setBoolValue" },
|
|
|
|
{ f_setDoubleValue, "_setDoubleValue" },
|
|
|
|
{ f_getParent, "_getParent" },
|
|
|
|
{ f_getChild, "_getChild" },
|
|
|
|
{ f_getChildren, "_getChildren" },
|
|
|
|
{ f_removeChild, "_removeChild" },
|
2005-10-23 16:10:32 +00:00
|
|
|
{ f_removeChildren, "_removeChildren" },
|
2003-12-01 14:35:49 +00:00
|
|
|
{ f_getNode, "_getNode" },
|
|
|
|
{ f_new, "_new" },
|
|
|
|
{ f_globals, "_globals" },
|
|
|
|
{ 0, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
naRef FGNasalSys::genPropsModule()
|
|
|
|
{
|
|
|
|
naRef namespc = naNewHash(_context);
|
|
|
|
for(int i=0; propfuncs[i].name; i++)
|
|
|
|
hashset(namespc, propfuncs[i].name,
|
|
|
|
naNewFunc(_context, naNewCCode(_context, propfuncs[i].func)));
|
|
|
|
return namespc;
|
|
|
|
}
|