18#include "nlua_commodity.h"
19#include "nlua_faction.h"
21#include "nlua_outfit.h"
24#include "nlua_system.h"
51 xmlw_attr( writer,
"name",
"%s", name );
53 char *encoded = base64_encode_to_cstr( name, name_len );
54 xmlw_attr( writer,
"name_base64",
"%s", encoded );
58 xmlw_attr(writer,
"keynum",
"1");
73static int nxml_saveData( xmlTextWriterPtr writer,
const char *type,
const char *name,
size_t name_len,
74 const char *value,
int keynum )
76 xmlw_startElem(writer,
"data");
78 xmlw_attr(writer,
"type",
"%s",type);
80 xmlw_str(writer,
"%s",value);
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 );
114 xmlw_str( writer,
"%s",
c->name );
115 xmlw_endElem( writer );
127 xmlr_attr_int_def( node,
"temp", istemp, 0);
131 xmlNodePtr cur = node->xmlChildrenNode;
135 if ( xml_isNode( cur,
"commodity" ) )
137 }
while ( xml_nextNode( cur ) );
153static int nxml_saveJump( xmlTextWriterPtr writer,
const char *name,
size_t name_len,
const char *start,
154 const char *dest,
int keynum )
156 xmlw_startElem(writer,
"data");
158 xmlw_attr(writer,
"type",JUMP_METATABLE);
160 xmlw_attr(writer,
"dest",
"%s",dest);
161 xmlw_str(writer,
"%s",start);
163 xmlw_endElem(writer);
179 const char *name, *str, *data;
181 size_t len, name_len;
190 switch (lua_type(L, -2)) {
193 name = lua_tolstring( L, -2, &name_len );
200 name = lua_tolstring( L, -1, &name_len );
214 switch (lua_type(L, -1)) {
218 xmlw_startElem(writer,
"data");
219 xmlw_attr(writer,
"type",
"table");
222 while (lua_next(L, -2) != 0) {
228 xmlw_endElem(writer);
233 nxml_saveData( writer,
"number", name, name_len, lua_tostring( L, -1 ), keynum );
240 if (lua_toboolean(L,-1)) buf[0] =
'1';
243 nxml_saveData( writer,
"bool", name, name_len, buf, keynum );
249 data = lua_tolstring( L, -1, &len );
251 nxml_saveData( writer,
"string", name, name_len, lua_tostring( L, -1 ), keynum );
253 char *encoded = base64_encode_to_cstr( data, len );
254 nxml_saveData( writer,
"string_base64", name, name_len, encoded, keynum );
267 WARN(_(
"Failed to save invalid spob."));
274 nxml_saveData( writer, SYSTEM_METATABLE, name, name_len, ss->name, keynum );
276 WARN(_(
"Failed to save invalid system."));
287 nxml_saveData( writer, FACTION_METATABLE, name, name_len, str, keynum );
296 nxml_saveData( writer, SHIP_METATABLE, name, name_len, str, keynum );
302 snprintf( buf,
sizeof(buf),
"%"PRId64, t );
303 nxml_saveData( writer, TIME_METATABLE, name, name_len, buf, keynum );
311 if ((ss == NULL) || (dest == NULL))
312 WARN(_(
"Failed to save invalid jump."));
314 nxml_saveJump( writer, name, name_len, ss->name, dest->name, keynum );
319 WARN( _(
"Failed to save invalid commodity.") );
328 nxml_saveData( writer, OUTFIT_METATABLE, name, name_len, str, keynum );
334 xmlw_startElem( writer,
"data" );
335 xmlw_attr( writer,
"type", VECTOR_METATABLE );
337 xmlw_attr( writer,
"x",
"%.16e", vec->
x );
338 xmlw_attr( writer,
"y",
"%.16e", vec->
y );
339 xmlw_attr( writer,
"mod",
"%.16e", vec->
mod );
340 xmlw_attr( writer,
"angle",
"%.16e", vec->
angle );
341 xmlw_endElem( writer );
372 nlua_getenv(naevL, env,
"mem");
376 while (lua_next(naevL, -2) != 0) {
397 char *name, *type, *buf, *num, *data;
401 node = parent->xmlChildrenNode;
404 if (xml_isNode(node,
"data")) {
406 xmlr_attr_strd(node,
"name",name);
407 xmlr_attr_strd(node,
"type",type);
409 xmlr_attr_strd(node,
"keynum",num);
411 lua_pushnumber(L, strtod( name, NULL ));
414 else if ( name != NULL )
415 lua_pushstring(L, name);
417 xmlr_attr_strd( node,
"name_base64", name );
418 data = base64_decode_cstr( &len, name );
419 lua_pushlstring( L, data, len );
425 if (strcmp(type,
"table")==0) {
426 xmlr_attr_strd(node,
"name",buf);
434 else if (strcmp(type,
"number")==0)
435 lua_pushnumber(L,xml_getFloat(node));
436 else if (strcmp(type,
"bool")==0)
437 lua_pushboolean(L,xml_getInt(node));
438 else if (strcmp(type,
"string")==0)
439 lua_pushstring(L,xml_get(node));
440 else if (strcmp( type,
"string_base64" ) == 0) {
441 data = base64_decode_cstr( &len, xml_get( node ) );
442 lua_pushlstring( L, data, len );
445 else if (strcmp(type,SPOB_METATABLE)==0) {
451 WARN(_(
"Failed to load nonexistent spob '%s'"), xml_get(node));
455 else if (strcmp(type,SYSTEM_METATABLE)==0) {
460 WARN(_(
"Failed to load nonexistent system '%s'"), xml_get(node));
464 else if (strcmp(type,FACTION_METATABLE)==0) {
467 else if (strcmp(type,SHIP_METATABLE)==0)
469 else if (strcmp(type,TIME_METATABLE)==0) {
472 else if (strcmp(type,JUMP_METATABLE)==0) {
474 xmlr_attr_strd(node,
"dest",buf);
476 if ((ss != NULL) && (dest != NULL)) {
477 LuaJump lj = {.srcid = ss->id, .destid = dest->id};
481 WARN(_(
"Failed to load nonexistent jump from '%s' to '%s'"), xml_get(node), buf);
486 else if (strcmp(type,COMMODITY_METATABLE)==0)
488 else if (strcmp(type,OUTFIT_METATABLE)==0)
490 else if (strcmp(type, VECTOR_METATABLE)==0) {
492 xmlr_attr_float( node,
"x", vec.
x );
493 xmlr_attr_float( node,
"y", vec.
y );
494 xmlr_attr_float( node,
"mod", vec.
mod );
495 xmlr_attr_float( node,
"angle", vec.
angle );
502 WARN(_(
"Unknown Lua data type!"));
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'
553 || ( buf[ i ] < 0x20 && buf[ i ] !=
'\t' && 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.
Header file with generic functions and naev-specifics.
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,...