15#include "nlua_commodity.h"
16#include "nlua_faction.h"
18#include "nlua_outfit.h"
21#include "nlua_system.h"
44 size_t name_len,
int keynum )
47 xmlw_attr( writer,
"name",
"%s", name );
49 char *encoded = base64_encode_to_cstr( name, name_len );
50 xmlw_attr( writer,
"name_base64",
"%s", encoded );
54 xmlw_attr( writer,
"keynum",
"1" );
71 const char *name,
size_t name_len,
const char *value,
74 xmlw_startElem( writer,
"data" );
76 xmlw_attr( writer,
"type",
"%s", type );
78 xmlw_str( writer,
"%s", value );
80 xmlw_endElem( writer );
97 size_t name_len,
const Commodity *
c,
int keynum )
100 if (
c->name == NULL )
103 xmlw_startElem( writer,
"data" );
105 xmlw_attr( writer,
"type", COMMODITY_METATABLE );
108 xmlw_attr( writer,
"temp",
"%d",
c->istemp );
109 xmlw_startElem( writer,
"commodity" );
111 xmlw_endElem( writer );
113 xmlw_str( writer,
"%s",
c->name );
114 xmlw_endElem( writer );
126 xmlr_attr_int_def( node,
"temp", istemp, 0 );
130 xmlNodePtr cur = node->xmlChildrenNode;
133 xml_onlyNodes( cur );
134 if ( xml_isNode( cur,
"commodity" ) )
136 }
while ( xml_nextNode( cur ) );
154 size_t name_len,
const char *start,
const char *dest,
157 xmlw_startElem( writer,
"data" );
159 xmlw_attr( writer,
"type", JUMP_METATABLE );
161 xmlw_attr( writer,
"dest",
"%s", dest );
162 xmlw_str( writer,
"%s", start );
164 xmlw_endElem( writer );
180 const char *name, *str, *data;
182 size_t len, name_len;
192 switch ( lua_type( L, -2 ) ) {
195 name = lua_tolstring( L, -2, &name_len );
201 lua_pushvalue( L, -2 );
202 name = lua_tolstring( L, -1, &name_len );
216 switch ( lua_type( L, -1 ) ) {
220 xmlw_startElem( writer,
"data" );
221 xmlw_attr( writer,
"type",
"table" );
224 while ( lua_next( L, -2 ) != 0 ) {
230 xmlw_endElem( writer );
235 nxml_saveData( writer,
"number", name, name_len, lua_tostring( L, -1 ),
243 if ( lua_toboolean( L, -1 ) )
248 nxml_saveData( writer,
"bool", name, name_len, buf, keynum );
254 data = lua_tolstring( L, -1, &len );
256 nxml_saveData( writer,
"string", name, name_len, lua_tostring( L, -1 ),
259 char *encoded = base64_encode_to_cstr( data, len );
260 nxml_saveData( writer,
"string_base64", name, name_len, encoded,
275 WARN( _(
"Failed to save invalid spob." ) );
281 nxml_saveData( writer, SYSTEM_METATABLE, name, name_len, ss->name,
284 WARN( _(
"Failed to save invalid system." ) );
295 nxml_saveData( writer, FACTION_METATABLE, name, name_len, str,
304 nxml_saveData( writer, SHIP_METATABLE, name, name_len, str, keynum );
309 snprintf( buf,
sizeof( buf ),
"%" PRId64, t );
310 nxml_saveData( writer, TIME_METATABLE, name, name_len, buf, keynum );
317 if ( ( ss == NULL ) || ( dest == NULL ) )
318 WARN( _(
"Failed to save invalid jump." ) );
325 WARN( _(
"Failed to save invalid commodity." ) );
333 nxml_saveData( writer, OUTFIT_METATABLE, name, name_len, str, keynum );
338 xmlw_startElem( writer,
"data" );
339 xmlw_attr( writer,
"type", VECTOR_METATABLE );
341 xmlw_attr( writer,
"x",
"%.16e", vec->
x );
342 xmlw_attr( writer,
"y",
"%.16e", vec->
y );
343 xmlw_attr( writer,
"mod",
"%.16e", vec->
mod );
344 xmlw_attr( writer,
"angle",
"%.16e", vec->
angle );
345 xmlw_endElem( writer );
378 nlua_getenv(
naevL, env,
"mem" );
380 lua_pushnil(
naevL );
382 while ( lua_next(
naevL, -2 ) != 0 ) {
403 char *name, *type, *buf, *num, *data;
407 node = parent->xmlChildrenNode;
410 if ( xml_isNode( node,
"data" ) ) {
412 xmlr_attr_strd( node,
"name", name );
413 xmlr_attr_strd( node,
"type", type );
415 xmlr_attr_strd( node,
"keynum", num );
417 lua_pushnumber( L, strtod( name, NULL ) );
419 }
else if ( name != NULL )
420 lua_pushstring( L, name );
422 xmlr_attr_strd( node,
"name_base64", name );
423 data = base64_decode_cstr( &len, name );
424 lua_pushlstring( L, data, len );
430 if ( strcmp( type,
"table" ) == 0 ) {
431 xmlr_attr_strd( node,
"name", buf );
438 }
else if ( strcmp( type,
"number" ) == 0 )
439 lua_pushnumber( L, xml_getFloat( node ) );
440 else if ( strcmp( type,
"bool" ) == 0 )
441 lua_pushboolean( L, xml_getInt( node ) );
442 else if ( strcmp( type,
"string" ) == 0 )
443 lua_pushstring( L, xml_get( node ) );
444 else if ( strcmp( type,
"string_base64" ) == 0 ) {
445 data = base64_decode_cstr( &len, xml_get( node ) );
446 lua_pushlstring( L, data, len );
448 }
else if ( strcmp( type, SPOB_METATABLE ) == 0 ) {
453 WARN( _(
"Failed to load nonexistent spob '%s'" ),
457 }
else if ( strcmp( type, SYSTEM_METATABLE ) == 0 ) {
458 StarSystem *ss =
system_get( xml_get( node ) );
462 WARN( _(
"Failed to load nonexistent system '%s'" ),
466 }
else if ( strcmp( type, FACTION_METATABLE ) == 0 ) {
468 }
else if ( strcmp( type, SHIP_METATABLE ) == 0 )
470 else if ( strcmp( type, TIME_METATABLE ) == 0 ) {
472 }
else if ( strcmp( type, JUMP_METATABLE ) == 0 ) {
473 StarSystem *ss =
system_get( xml_get( node ) );
474 xmlr_attr_strd( node,
"dest", buf );
476 if ( ( ss != NULL ) && ( dest != NULL ) ) {
477 LuaJump lj = { .srcid = ss->id, .destid = dest->id };
480 WARN( _(
"Failed to load nonexistent jump from '%s' to '%s'" ),
481 xml_get( node ), buf );
485 }
else if ( strcmp( type, COMMODITY_METATABLE ) == 0 )
487 else if ( strcmp( type, OUTFIT_METATABLE ) == 0 )
489 else if ( strcmp( type, VECTOR_METATABLE ) == 0 ) {
491 xmlr_attr_float( node,
"x", vec.
x );
492 xmlr_attr_float( node,
"y", vec.
y );
493 xmlr_attr_float( node,
"mod", vec.
mod );
494 xmlr_attr_float( node,
"angle", vec.
angle );
502 WARN( _(
"Unknown Lua data type!" ) );
508 lua_settable( L, -3 );
518 }
while ( xml_nextNode( node ) );
534 nlua_getenv(
naevL, env,
"mem" );
551 for (
size_t i = 0; i < len; i++ ) {
552 if ( buf[i] ==
'\0' || ( buf[i] < 0x20 && buf[i] !=
'\t' &&
553 buf[i] !=
'\n' && buf[i] !=
'\r' ) )
556 return u8_isvalid( buf, len );
Commodity * commodity_get(const char *name)
Gets a commodity by name.
int faction_isFaction(int f)
Checks whether or not a faction is valid.
const char * faction_name(int f)
Gets a factions "real" (internal) name.
int faction_get(const char *name)
Gets a faction ID by name.
int missions_saveTempCommodity(xmlTextWriterPtr writer, const Commodity *c)
Saves a temporary commodity's defintion into the current node.
Commodity * missions_loadTempCommodity(xmlNodePtr cur)
Loads a temporary commodity.
Commodity * lua_tocommodity(lua_State *L, int ind)
Lua bindings to interact with commodities.
Commodity ** lua_pushcommodity(lua_State *L, Commodity *commodity)
Pushes a commodity on the stack.
int lua_iscommodity(lua_State *L, int ind)
Checks to see if ind is a commodity.
LuaFaction * lua_pushfaction(lua_State *L, LuaFaction faction)
Pushes a faction on the stack.
int lua_isfaction(lua_State *L, int ind)
Checks to see if ind is a faction.
LuaFaction lua_tofaction(lua_State *L, int ind)
Gets faction at index.
LuaJump * lua_pushjump(lua_State *L, LuaJump jump)
Pushes a jump on the stack.
int lua_isjump(lua_State *L, int ind)
Checks to see if ind is a jump.
LuaJump * lua_tojump(lua_State *L, int ind)
This module allows you to handle the jumps from Lua.
const Outfit ** lua_pushoutfit(lua_State *L, const Outfit *outfit)
Pushes a outfit on the stack.
int lua_isoutfit(lua_State *L, int ind)
Checks to see if ind is a outfit.
const Outfit * lua_tooutfit(lua_State *L, int ind)
Lua bindings to interact with outfits.
const Ship ** lua_pushship(lua_State *L, const Ship *ship)
Pushes a ship on the stack.
int lua_isship(lua_State *L, int ind)
Checks to see if ind is a ship.
const Ship * lua_toship(lua_State *L, int ind)
Lua bindings to interact with ships.
LuaSpob lua_tospob(lua_State *L, int ind)
This module allows you to handle the spobs from Lua.
LuaSpob * lua_pushspob(lua_State *L, LuaSpob spob)
Pushes a spob on the stack.
int lua_isspob(lua_State *L, int ind)
Checks to see if ind is a spob.
LuaSystem * lua_pushsystem(lua_State *L, LuaSystem sys)
Pushes a system on the stack.
LuaSystem lua_tosystem(lua_State *L, int ind)
Lua system module.
int lua_issystem(lua_State *L, int ind)
Checks to see if ind is a system.
ntime_t * lua_pushtime(lua_State *L, ntime_t time)
Pushes a time on the stack.
int lua_istime(lua_State *L, int ind)
Checks to see if ind is a time.
ntime_t * lua_totime(lua_State *L, int ind)
Bindings for interacting with the time.
int lua_isvector(lua_State *L, int ind)
Checks to see if ind is a vector.
vec2 * lua_tovector(lua_State *L, int ind)
Represents a 2D vector in Lua.
vec2 * lua_pushvector(lua_State *L, vec2 vec)
Pushes a vector on the stack.
int nxml_persistLua(nlua_env env, xmlTextWriterPtr writer)
Persists all the nxml Lua data.
static Commodity * nxml_loadCommodity(xmlNodePtr node)
Reverse of nxml_saveCommodity.
static int nxml_persistDataNode(lua_State *L, xmlTextWriterPtr writer)
Persists the node on the top of the stack and pops it.
int nxml_unpersistLua(nlua_env env, xmlNodePtr parent)
Unpersists Lua data into a table named "mem".
static int nxml_saveData(xmlTextWriterPtr writer, const char *type, const char *name, size_t name_len, const char *value, int keynum)
Persists Lua data.
static int nxml_unpersistDataNode(lua_State *L, xmlNodePtr parent)
Unpersists Lua data.
static int nxml_saveCommodity(xmlTextWriterPtr writer, const char *name, size_t name_len, const Commodity *c, int keynum)
Commodity-specific nxml_saveData derivative.
static int nxml_saveNameAttribute(xmlTextWriterPtr writer, const char *name, size_t name_len, int keynum)
Persists the key of a key/value pair.
static int nxml_saveJump(xmlTextWriterPtr writer, const char *name, size_t name_len, const char *start, const char *dest, int keynum)
Jump-specific nxml_saveData derivative.
static int nxml_canWriteString(const char *buf, size_t len)
Checks whether saving the given string (from lua_tolstring) can be saved into an XML document without...
const Outfit * outfit_get(const char *name)
Gets an outfit by name.
const Ship * ship_get(const char *name)
Gets a ship based on its name.
Spob * spob_get(const char *spobname)
Gets a spob based on its name.
StarSystem * system_getIndex(int id)
Get the system by its index.
int spob_index(const Spob *p)
Gets the ID of a spob.
StarSystem * system_get(const char *sysname)
Get the system from its name.
Spob * spob_getIndex(int ind)
Gets spob by index.
int system_index(const StarSystem *sys)
Gets the index of a star system.
A ship outfit, depends radically on the type.
Represents a Space Object (SPOB), including and not limited to planets, stations, wormholes,...