[Add] Saving lua active mission states. Fixed Makefile to build pluto.

This commit is contained in:
Allanis 2013-05-23 17:58:23 +01:00
parent 62542c14c1
commit 1d2a0cfed4
3 changed files with 74 additions and 6 deletions

View File

@ -18,13 +18,14 @@ OBJS := $(OBJS:%.c=%.o)
# CFLAGS # CFLAGS
CLUA = -I../lib/lua CLUA = -I../lib/lua
CPLUTO = -I../lib/pluto
CSDL = $(shell sdl-config --cflags) CSDL = $(shell sdl-config --cflags)
CXML = $(shell xml2-config --cflags) CXML = $(shell xml2-config --cflags)
CTTF = $(shell freetype-config --cflags) CTTF = $(shell freetype-config --cflags)
CAL = -lopenal CAL = -lopenal
CVORBIS = CVORBIS =
CGL = CGL =
CFLAGS = $(CLUA) $(CSDL) $(CXML) $(CTTF) $(CGL) $(CAL) $(CVORBIS) $(VERSION) -D$(OS) CFLAGS = $(CLUA) $(CPLUTO) $(CSDL) $(CXML) $(CTTF) $(CGL) $(CAL) $(CVORBIS) $(VERSION) -D$(OS) -fgnu89-inline
ifdef DEBUG ifdef DEBUG
CFLAGS += -W -Wall -g3 -DDEBUG -DLUA_USE_APICHECK -std=c99 CFLAGS += -W -Wall -g3 -DDEBUG -DLUA_USE_APICHECK -std=c99
else else
@ -33,6 +34,7 @@ endif
# LDFLAGS. # LDFLAGS.
LDLUA = ../lib/lua/liblua.a LDLUA = ../lib/lua/liblua.a
LDPLUTO = ../lib/pluto/pluto.a
LDSDL = $(shell sdl-config --libs) -lSDL_image LDSDL = $(shell sdl-config --libs) -lSDL_image
LDXML = $(shell xml2-config --libs) LDXML = $(shell xml2-config --libs)
LDTTF = $(shell freetype-config --libs) LDTTF = $(shell freetype-config --libs)
@ -40,7 +42,7 @@ LDGL = -lGL
LDAL = -lopenal LDAL = -lopenal
LDVORBIS = -lvorbisfile LDVORBIS = -lvorbisfile
LDPNG = -lpng LDPNG = -lpng
LDFLAGS = -lm $(LDLUA) $(LDSDL) $(LDXML) $(LDTTF) $(LDGL) $(LDPNG) $(LDAL) $(LDVORBIS) LDFLAGS = -lm $(LDLUA) $(LDPLUTO) $(LDSDL) $(LDXML) $(LDTTF) $(LDGL) $(LDPNG) $(LDAL) $(LDVORBIS)
# This is just for gstat to run some analysis on performance. # This is just for gstat to run some analysis on performance.
ifdef DEBUG ifdef DEBUG
@ -58,16 +60,19 @@ DATAFILES = $(VERSIONFILE) $(DATA_AI) $(DATA_GFX) $(DATA_XML) $(DATA_SND) $(DATA
# TARGETS. # TARGETS.
%.o: %.c %.h %.o: %.c %.h
@gcc -c $(CFLAGS) -o $@ $< @$(CC) -c $(CFLAGS) -o $@ $<
@echo -e "\tCC $@" @echo -e "\tCC $@"
all: utils data lua $(OBJS) all: utils data lua pluto $(OBJS)
@gcc $(LDFLAGS) -o $(APPNAME) $(OBJS) ../lib/lua/liblua.a @$(CC) $(LDFLAGS) -o $(APPNAME) $(OBJS) ../lib/lua/liblua.a ../lib/pluto/pluto.a
@echo "\tLD $(APPNAME)" @echo "\tLD $(APPNAME)"
lua: lua:
@if [ ! -e ../lib/lua/liblua.a ]; then (cd ../lib/lua; $(MAKE) a); fi @if [ ! -e ../lib/lua/liblua.a ]; then (cd ../lib/lua; $(MAKE) a); fi
pluto:
@if [ ! -e ../lib/pluto/pluto.a ]; then (cd ../lib/pluto; $(MAKE)); fi
pack: ../src/pack.c ../utils/pack/main.c pack: ../src/pack.c ../utils/pack/main.c
@(cd ../utils/pack; $(MAKE)) @(cd ../utils/pack; $(MAKE))
@ -92,4 +97,5 @@ clean:
@(cd ../utils/mkspr; $(MAKE) clean) @(cd ../utils/mkspr; $(MAKE) clean)
@echo -e "\tCleaning Lua" @echo -e "\tCleaning Lua"
@(cd ../lib/lua; $(MAKE) clean) @(cd ../lib/lua; $(MAKE) clean)
@(cd ../lib/pluto; $(MAKE) clean)

View File

@ -2,9 +2,11 @@
#include <string.h> #include <string.h>
#include <malloc.h> #include <malloc.h>
//#include "pluto.h"
#include "lua.h" #include "lua.h"
#include "lauxlib.h" #include "lauxlib.h"
#include "lualib.h" #include "lualib.h"
#include "pluto.h"
#include "rng.h" #include "rng.h"
#include "lephisto.h" #include "lephisto.h"
@ -410,7 +412,55 @@ void missions_cleanup(void) {
mission_cleanup(&player_missions[i]); mission_cleanup(&player_missions[i]);
} }
typedef struct MBuf_ {
char* data;
int len, alloc; // Size of each data chunk, chunks to alloc when growing.
int ndata, mdata; // Buffer real length, memory length.
} MBuf;
MBuf* mbuf_create(int len, int alloc) {
MBuf* buf;
buf = malloc(sizeof(MBuf));
buf->data = 0;
buf->ndata = buf->mdata = 0;
buf->len = len;
buf->alloc = alloc;
return buf;
}
void mbuf_free(MBuf* buf) {
if(buf->data != NULL) free(buf->data);
buf->ndata = buf->mdata = 0;
free(buf);
}
static int mission_writeLua(lua_State* L, const void* p, size_t sz, void* ud) {
int i;
MBuf* buf;
(void)L;
buf = (MBuf*)ud;
i = buf->ndata * buf->len + sz - buf->mdata*buf->len;
if(i = 0) {
// Need more memory.
buf->mdata += (i / (buf->len*buf->alloc) + 1) * buf->len * buf->alloc;
buf->data = realloc(buf->data, buf->mdata*buf->len);
}
memcpy(&buf->data[buf->ndata * buf->len], p, sz);
buf->ndata += sz;
return 0;
}
int missions_save(xmlTextWriterPtr writer) { int missions_save(xmlTextWriterPtr writer) {
MBuf* buf;
char* data;
int i, j; int i, j;
xmlw_startElem(writer, "missions"); xmlw_startElem(writer, "missions");
@ -431,7 +481,15 @@ int missions_save(xmlTextWriterPtr writer) {
xmlw_elem(writer, "cargo", "%u", player_missions[i].cargo[j]); xmlw_elem(writer, "cargo", "%u", player_missions[i].cargo[j]);
xmlw_endElem(writer); // Cargo. xmlw_endElem(writer); // Cargo.
// TODO: Save lua data. xmlw_startElem(writer, "lua");
buf = mbuf_create(1, 128);
lua_pushvalue(player_missions[i].L, LUA_GLOBALSINDEX);
pluto_persist(player_missions[i].L, mission_writeLua, buf);
data = base64_encode(&j, buf->data, buf->ndata);
mbuf_free(buf);
xmlw_raw(writer, data, j);
free(data);
xmlw_endElem(writer); // Lua.
xmlw_endElem(writer); // Mission. xmlw_endElem(writer); // Mission.
} }

View File

@ -55,6 +55,10 @@
if(xmlTextWriterWriteFormatElement(w, (xmlChar*)n, str, ## args) < 0) { \ if(xmlTextWriterWriteFormatElement(w, (xmlChar*)n, str, ## args) < 0) { \
ERR("xmlw: Unable to write format element"); return -1; } ERR("xmlw: Unable to write format element"); return -1; }
#define xmlw_raw(w,b,l) \
if(xmlTextWriterWriteRawLen(w, (xmlChar*)b, l) < 0) { \
ERR("xmlw: unable to write raw element"); return -1; }
#define xmlw_attr(w, str, val...) \ #define xmlw_attr(w, str, val...) \
if(xmlTextWriterWriteFormatAttribute(w, (xmlChar*)str, ## val) < 0) { \ if(xmlTextWriterWriteFormatAttribute(w, (xmlChar*)str, ## val) < 0) { \
ERR("xmlw: Unable to write element attribute"); return -1; } ERR("xmlw: Unable to write element attribute"); return -1; }