git » fp-git.git » commit 007dd25

binded level importing

author ecalot
2006-07-16 21:06:10 UTC
committer ecalot
2006-07-16 21:06:10 UTC
parent 0cde66166ae3bdc9c643ef91c44eae068db44a62

binded level importing

PR/src/include/other.h +3 -0
PR/src/include/plv.h +1 -0
PR/src/lib/formats/mid.c +2 -1
PR/src/lib/formats/plv.c +85 -4
PR/src/lib/object/level/level_common.c +23 -19
PR/src/lib/object/object.c +4 -4

diff --git a/PR/src/include/other.h b/PR/src/include/other.h
index 4b843fb..6f8f12e 100644
--- a/PR/src/include/other.h
+++ b/PR/src/include/other.h
@@ -50,4 +50,7 @@ int objectOtherBinarySet(void* o,tResource* res);
 void* objectLevelCreate(tBinary content,int number,const char* datfile,const char* name,const char* desc,const char* datAuthor,int *error);
 int objectLevelWrite(void* o, const char* file, int optionflag, const char* backupExtension,int popVersion);
 
+void* objectLevelPop1Read(const char* file,int *result);
+int objectLevelPop1Set(void* o,tResource* res);
+
 #endif
diff --git a/PR/src/include/plv.h b/PR/src/include/plv.h
index 850dd53..053aa5c 100644
--- a/PR/src/include/plv.h
+++ b/PR/src/include/plv.h
@@ -67,6 +67,7 @@ int mFormatExportPlv(const unsigned char* data, const char *vFileext,unsigned lo
 
 
 int writePlv(const char* file, tBinary content, int popversion, const char* datfile, int level, const char* filename, const char* desc, const char* title, const char* vDatAuthor, int optionflag,const char* backupExtension);
+int readPlv(const char* file, tBinary* content, int *number, char** datfile, char** name, char** desc, char** datAuthor);
 
 /* Weekdays and months */
 #define DATE_WEEKDAYS "Sun\0Mon\0Tue\0Wed\0Thu\0Fri\0Sat"
diff --git a/PR/src/lib/formats/mid.c b/PR/src/lib/formats/mid.c
index 5d768c2..066f3df 100644
--- a/PR/src/lib/formats/mid.c
+++ b/PR/src/lib/formats/mid.c
@@ -43,7 +43,7 @@ int writeMid(const char* file, tBinary* snd, int optionflag, const char* backupE
 	/* Mid files are saved as raw */
 	return writeData(snd->data,0,file,snd->size,optionflag,backupExtension)?PR_RESULT_SUCCESS:PR_RESULT_ERR_FILE_NOT_WRITE_ACCESS;
 }
-
+#if 0
 int mFormatImportMid(tResource *res) {
 	unsigned char* file;
 
@@ -56,4 +56,5 @@ int mFormatImportMid(tResource *res) {
 	mWriteFileInDatFile(res);
 	return 1; /* true */
 }
+#endif
 
diff --git a/PR/src/lib/formats/plv.c b/PR/src/lib/formats/plv.c
index 3d0e066..b1552c2 100644
--- a/PR/src/lib/formats/plv.c
+++ b/PR/src/lib/formats/plv.c
@@ -40,9 +40,11 @@ plv.c: Princed Resources : PLV prince level files support
 */
 
 /* Includes */
-#include "plv.h"
-#include "disk.h"
 #include "dat.h"
+#include "disk.h"
+#include "memory.h" /* freeBinary */
+#include "plv.h"
+#include <stdlib.h> /* malloc */
 #include <string.h>
 #include <time.h>
 
@@ -169,7 +171,84 @@ int writePlv(const char* file, tBinary content, int popversion, const char* datf
 
 extern FILE* outputStream;
 
-int readPlv(tResource *res) {
+int readPlv(const char* file, tBinary* content, int *number, char** datfile, char** name, char** desc, char** datAuthor) {
+
+/*
+ * PLV 1 SPECS:
+ * bytes offset description                     content
+ *     7      0 HAS FILE TAG OF 8 LETTERS       "POP_LVL"
+ *     1      7 POP VERS                        0x01
+ *     1      8 PLV VERS                        0x01
+ *     1      9 LEV NUM
+ *     4     10 FIELD-PAIR ( NAME / CONTENT ) COUNT
+ *     4     14 BLOCK 1: LEVEL SIZE (B1)        2306 (including the checksum)
+ *    B1     18 BLOCK 1: LEVEL CODE
+ *     4  18+B1 BLOCK 2: USER DATA SIZE VALUE IN BYTES (B2)
+ *    B2  22+B1 BLOCK 2: LEVEL CODE NEXT, REST OF FILE
+ *
+ * Total size of file B1+B2+22.
+ * All values are unsigned and in the Intel x86 architecture
+ */
+	
+	char magic[PLV_HEADER_MAGIC_SIZE];
+	unsigned char popVersion, plvVersion, levelNumber,checksum;
+	long fieldPair,block1Size,block2Size;
+	FILE* fd;
+	int ok;
+
+	content->isCopy=1;
+	content->data=NULL;
+	content->size=0;
+	
+	fd=fopen(file,"rb");
+	if (!fd) return PR_RESULT_ERR_FILE_NOT_READ_ACCESS;
+
+	/* Read headers */
+	ok=fread(magic,PLV_HEADER_MAGIC_SIZE,1,fd);
+	ok=ok&&!strncmp(magic,PLV_HEADER_MAGIC,PLV_HEADER_MAGIC_SIZE);
+	ok=ok&&freadchar(&popVersion,fd);
+	ok=ok&&freadchar(&plvVersion,fd);
+	ok=ok&&freadchar(&levelNumber,fd);
+	ok=ok&&freadlong(&fieldPair,fd);
+	ok=ok&&freadlong(&block1Size,fd);
+	ok=ok&&freadchar(&checksum,fd);
+
+	/* TODO: validate checksum */
+
+	/* Check data */
+	if (!ok) {
+		fclose(fd);
+		return PR_RESULT_ERR_FILE_NOT_READ_ACCESS; /* TODO: use a bad format code */
+	}
+
+	/* Read data */
+	content->isCopy=0;
+	content->size=block1Size;
+	ok=(content->data=malloc(block1Size))?1:0;
+	ok=ok&&fread(content->data,1,block1Size,fd);
+	ok=ok&&freadlong(&block2Size,fd);
+	/* TODO: finish reading this block */
+	/* check: Total size of file B1+B2+22. */
+
+	*number=levelNumber;
+	*datfile=NULL;
+	*name=NULL;
+	*desc=NULL;
+	*datAuthor=NULL;
+	
+	fclose(fd);
+
+	/* Check data */
+	if (!ok) {
+		freeBinary(*content);
+		content->isCopy=1;
+		content->data=NULL;
+		content->size=0;
+		return PR_RESULT_ERR_FILE_NOT_READ_ACCESS; /* TODO: use a bad format code */
+	}
+
+
+#if 0
 	/* declare variables */
 	unsigned char* pos;
 	unsigned char* posAux;
@@ -197,6 +276,8 @@ int readPlv(tResource *res) {
 	res->content.data=pos;
 	mWriteFileInDatFileIgnoreChecksum(res);
 	res->content.data=posAux;
+#endif
 
-	return 1; /* true */
+	return PR_RESULT_SUCCESS;
 }
+
diff --git a/PR/src/lib/object/level/level_common.c b/PR/src/lib/object/level/level_common.c
index 2cff295..698f398 100644
--- a/PR/src/lib/object/level/level_common.c
+++ b/PR/src/lib/object/level/level_common.c
@@ -40,8 +40,8 @@ level.c: Princed Resources : Common POP level object implementation
 #include "dat.h"    /* WriteDat */
 #include "disk.h"   /* getFileNameFromPath */
 #include "plv.h"
-#include <stdlib.h>
 #include "types.h"  /* tResources */
+#include <stdlib.h>
 
 /***************************************************************\
 |                         Level Object                         |
@@ -50,10 +50,10 @@ level.c: Princed Resources : Common POP level object implementation
 typedef struct {
 	tBinary content;
 	int number;
-	const char* datfile;
-	const char* name;
-	const char* desc;
-	const char* datAuthor;
+	char* datfile;
+	char* name;
+	char* desc;
+	char* datAuthor;
 }tPop1Level;
 
 void* objectLevelCreate(tBinary content,int number,const char* datfile,const char* name,const char* desc,const char* datAuthor,int *error) {
@@ -64,10 +64,10 @@ void* objectLevelCreate(tBinary content,int number,const char* datfile,const cha
 	r=(tPop1Level*)malloc(sizeof(tPop1Level));
 	r->content=content;
 	r->number=number;
-	r->datfile=datfile;
-	r->name=name;
-	r->desc=desc;
-	r->datAuthor=datAuthor;
+	r->datfile=(char*)datfile;
+	r->name=(char*)name;
+	r->desc=(char*)desc;
+	r->datAuthor=(char*)datAuthor;
 	return (void*)r;
 }
 
@@ -76,21 +76,25 @@ int objectLevelWrite(void* o, const char* file, int optionflag, const char* back
 	return writePlv(file,b->content,version,b->datfile,b->number,getFileNameFromPath(file),b->desc,b->name,b->datAuthor,optionflag,backupExtension);
 }
 
-/*
-void* objLevelRead(const char* file,int *result) {
-	tBinary o=mLoadFileArray(file);
-	if (o.size<0) {
-		*result=o.size;
+void* objectLevelPop1Read(const char* file,int *result) { /* TODO: generalize to POP2 */
+	tPop1Level* r;
+
+	r=(tPop1Level*)malloc(sizeof(tPop1Level));
+
+	*result=readPlv(file,&r->content,&r->number,&r->datfile,&r->name,&r->desc,&r->datAuthor);
+
+	if (*result!=PR_RESULT_SUCCESS) {
+		free(r);
 		return NULL;
 	}
-	return objectLevelPop1Create(o,result);
+
+	return (void*)r;
 }
 
-int objLevelSet(void* o,tResource* res) {
-	tBinary* bin=o;
-	res->content=*bin;
+int objectLevelPop1Set(void* o,tResource* res) {
+	tPop1Level* r=o;
+	res->content=r->content;
 	mWriteFileInDatFile(res);
 	return PR_RESULT_SUCCESS;
 }
-*/
 
diff --git a/PR/src/lib/object/object.c b/PR/src/lib/object/object.c
index ac17f7b..83690d1 100644
--- a/PR/src/lib/object/object.c
+++ b/PR/src/lib/object/object.c
@@ -194,8 +194,8 @@ tColor* objectPaletteGetColorArray(tObject pal) {
 
 void objectSet(tObject o,int *result,tResource* res) {
 	switch (o.type) {
-		case eResTypeLevelPop1:
-			/*result=objLevelSet(o.obj,res); TODO */
+		case eResTypeLevelPop1: /* TODO: pop2 */
+			*result=objectLevelPop1Set(o.obj,res);
 			break;
 		case eResTypeImage16:
 			*result=objectImage16Set(o.obj,res);
@@ -225,8 +225,8 @@ tObject objectRead(const char* file,tResource* res,int *result) {
 	tObject o;
 	switch (res->type) {
 		case eResTypeLevelPop1:
-			/*o.obj=objectLevelPop1Read(file,res.content,result); TODO */
-			*result=-1; /* TODO: unsupported object */
+			/* TODO: pop2 */
+			o.obj=objectLevelPop1Read(file,result);
 			break;
 		case eResTypeImage16:
 			o.obj=objectImage16Read(file,*res->palette /*TODO: check because sometimes it is not initialized */,result);