git » fp-git.git » commit bd977d7

added support for pop2 importing

author ecalot
2005-06-16 01:44:53 UTC
committer ecalot
2005-06-16 01:44:53 UTC
parent 8160cdcdb699501e2b26368652def09702796e13

added support for pop2 importing

PR/src/include/common.h +1 -1
PR/src/include/pr.h +1 -0
PR/src/include/tree.h +1 -2
PR/src/include/unknown.h +1 -2
PR/src/lib/actions/import.c +1 -3
PR/src/lib/layers/dat.c +75 -8
PR/src/lib/layers/reslist.c +7 -3
PR/src/lib/xml/parse.c +4 -0
PR/src/lib/xml/search.c +7 -0
PR/src/lib/xml/tree.c +4 -4
PR/src/lib/xml/unknown.c +4 -4
PR/src/xml/resources.xml +1 -1

diff --git a/PR/src/include/common.h b/PR/src/include/common.h
index ec7319b..8ed48cb 100644
--- a/PR/src/include/common.h
+++ b/PR/src/include/common.h
@@ -65,7 +65,7 @@ common.h: Princed Resources : Defines and prototypes common to all PR code
 \***************************************************************/
 
 #define PR_URL                    "http://www.princed.com.ar"
-#define PR_VERSION                "v1.1"
+#define PR_VERSION                "v1.2-dev"
 #define PR_COPY                   "(c) Copyright 2003 - 2005 Princed Development Team"
 
 /***************************************************************\
diff --git a/PR/src/include/pr.h b/PR/src/include/pr.h
index b7a7ce0..335f47e 100644
--- a/PR/src/include/pr.h
+++ b/PR/src/include/pr.h
@@ -58,6 +58,7 @@ typedef struct tTag {
 	char* index;
 	char* version;
 	char* number;
+	char* flags;
 }tTag;
 
 /***************************************************************\
diff --git a/PR/src/include/tree.h b/PR/src/include/tree.h
index 8cd44ab..e8e8117 100644
--- a/PR/src/include/tree.h
+++ b/PR/src/include/tree.h
@@ -39,7 +39,7 @@ resources.h: Princed Resources : Resource Handler headers
 #define RES_XML_UNKNOWN_PATH  "%s/unknown/%s/"
 #define RES_XML_UNKNOWN_FILES "%s%03d.%s"
 #define RES_XML_UNKNOWN_START "<?xml version=\"1.0\" ?>\n<resources version=\"generated\">\n <folder name=\"unknown\" path=\"%s\" file=\"%s\" palette=\"%d\" paletteindex=\"%s\">\n"
-#define RES_XML_UNKNOWN_ITEM  "  <item value=\"%d\" index=\"%s\" path=\""RES_XML_UNKNOWN_FILES"\" itemtype=\"%s\">Unknown %s %d</item>\n"
+#define RES_XML_UNKNOWN_ITEM  "  <item value=\"%d\" index=\"%s\" path=\""RES_XML_UNKNOWN_FILES"\" type=\"%s\" flags=\"0x%lx\">Unknown %s %d</item>\n"
 #define RES_XML_UNKNOWN_END   " </folder>\n</resources>\n"
 
 /* File extensions */
@@ -47,7 +47,6 @@ resources.h: Princed Resources : Resource Handler headers
 #define RES_FILE_EXTENSIONS   {"raw","plv","bmp","wav","mid","bin","pal","pcs"}
 
 /* Includes */
-#include <stdio.h>
 #include "parse.h"
 #include "reslist.h"
 
diff --git a/PR/src/include/unknown.h b/PR/src/include/unknown.h
index 8cd44ab..e8e8117 100644
--- a/PR/src/include/unknown.h
+++ b/PR/src/include/unknown.h
@@ -39,7 +39,7 @@ resources.h: Princed Resources : Resource Handler headers
 #define RES_XML_UNKNOWN_PATH  "%s/unknown/%s/"
 #define RES_XML_UNKNOWN_FILES "%s%03d.%s"
 #define RES_XML_UNKNOWN_START "<?xml version=\"1.0\" ?>\n<resources version=\"generated\">\n <folder name=\"unknown\" path=\"%s\" file=\"%s\" palette=\"%d\" paletteindex=\"%s\">\n"
-#define RES_XML_UNKNOWN_ITEM  "  <item value=\"%d\" index=\"%s\" path=\""RES_XML_UNKNOWN_FILES"\" itemtype=\"%s\">Unknown %s %d</item>\n"
+#define RES_XML_UNKNOWN_ITEM  "  <item value=\"%d\" index=\"%s\" path=\""RES_XML_UNKNOWN_FILES"\" type=\"%s\" flags=\"0x%lx\">Unknown %s %d</item>\n"
 #define RES_XML_UNKNOWN_END   " </folder>\n</resources>\n"
 
 /* File extensions */
@@ -47,7 +47,6 @@ resources.h: Princed Resources : Resource Handler headers
 #define RES_FILE_EXTENSIONS   {"raw","plv","bmp","wav","mid","bin","pal","pcs"}
 
 /* Includes */
-#include <stdio.h>
 #include "parse.h"
 #include "reslist.h"
 
diff --git a/PR/src/lib/actions/import.c b/PR/src/lib/actions/import.c
index 4652790..2cb146a 100644
--- a/PR/src/lib/actions/import.c
+++ b/PR/src/lib/actions/import.c
@@ -106,6 +106,7 @@ int fullCompile(const char* vFiledat, const char* vDirExt, tResourceList* r, int
 		/* remember only id and type */
 		newRes.id=res->id;
 		newRes.type=res->type;
+		newRes.flags=res->flags;
 	
 		if (hasFlag(raw_flag)) newRes.type=0; /* compile from raw */
 		getFileName(vFileext,vDirExt,res,vFiledat,vDatFileName,optionflag,backupExtension);
@@ -130,9 +131,6 @@ int fullCompile(const char* vFiledat, const char* vDirExt, tResourceList* r, int
 	/* Close file. If empty, don't save */
 	mWriteCloseDatFile(!ok,optionflag,backupExtension);
 
-	/* Free allocated resources and dynamic strings */
-	/*freeResources;*/
-
 	if (hasFlag(verbose_flag)) fprintf(outputStream,PR_TEXT_IMPORT_DONE,ok,error);
 	return error;
 }
diff --git a/PR/src/lib/layers/dat.c b/PR/src/lib/layers/dat.c
index bc62f12..220b14e 100644
--- a/PR/src/lib/layers/dat.c
+++ b/PR/src/lib/layers/dat.c
@@ -84,7 +84,7 @@ int checkSum(const unsigned char* data,int size) {
 #define dat_readCursorGetId(r)        (array2short(r.currentRecord))
 #define dat_readCursorGetOffset(r)    (array2long(r.currentRecord+2))
 #define dat_readCursorGetSize(r)      (array2short(r.currentRecord+6))
-#define dat_readCursorGetFlags(r)     ((r.popVersion==pop1)?(1<<31):(r.currentRecord[8]<<16|r.currentRecord[9]<<8|r.currentRecord[10]))
+#define dat_readCursorGetFlags(r)     ((r.popVersion==pop1)?(1<<24):(r.currentRecord[8]<<16|r.currentRecord[9]<<8|r.currentRecord[10]))
 #define dat_readCursorGetVersion(r)     (r.popVersion)
 
 /* the cursor move functions */
@@ -409,6 +409,7 @@ void dat_write(const tResource* res,unsigned char checksum) {
 
 	/* remember only indexed values */
 	insert.id=res->id;
+	insert.flags=res->flags;
 	insert.size=res->size;
 	insert.offset=((unsigned long)ftell(writeDatFile));
 
@@ -462,22 +463,88 @@ void mWriteCloseDatFile(int dontSave,int optionflag, const char* backupExtension
 	unsigned short int size2=2;
 	unsigned long  int size1=ftell(writeDatFile);
 	const tResource*   res;
+	char               index[5];
 
 	/* Write index */
 	fwriteshort(&totalItems,writeDatFile); /* Junk total items count to reserve 2 bytes */
 
 	resourceListStartIteration(&resIndex);
-	while ((res=resourceListGetElement(&resIndex))) {
-		totalItems++;
-		fwriteshort(&(res->id.value),writeDatFile);
-		fwritelong(&(res->offset),writeDatFile);
-		fwriteshort(&(res->size),writeDatFile);
+	res=resourceListGetElement(&resIndex);
+	if (res) {
+		if (!strncmp(res->id.index,"POP1",4)) { /* POP1 */
+			do {
+				totalItems++;
+				printf("Adding item id (%s,%d)\n",res->id.index,res->id.value);
+				fwriteshort(&(res->id.value),writeDatFile);
+				fwritelong(&(res->offset),writeDatFile);
+				fwriteshort(&(res->size),writeDatFile);
+			} while ((res=resourceListGetElement(&resIndex)));
+			size2+=totalItems<<3;
+
+		} else { /* POP2 */
+			unsigned long int flags=res->flags;
+			int numberOfSlaveItems=0;
+			int slaveCountPos=size1; /* first value is a junked place */
+
+			/* first step: read the list to create the master index */
+			strcpy(index,"a");
+			do {
+				if (strncmp(res->id.index,index,4)) {
+					fwrite(res->id.index,4,1,writeDatFile);
+					fwriteshort(&totalItems,writeDatFile); /* Junk (I) */
+
+					strncpy(index,res->id.index,5);
+					flags=res->flags;
+				}
+			} while ((res=resourceListGetElement(&resIndex)));
+			/* second step: read the list to create the slave indexes */
+			resourceListStartIteration(&resIndex);
+			res=resourceListGetElement(&resIndex);
+			strcpy(index,"a");
+			do {
+				unsigned char v;
+				if (strncmp(res->id.index,index,4)) {
+					int relativePos=ftell(writeDatFile)-size1;
+					/* go to the master index to write the beginning of this new index */
+					fseek(writeDatFile,size1+6+6*(totalItems++),SEEK_SET);
+					fwriteshort(&relativePos,writeDatFile); /* overwrite junk (I) */
+					/* go to the last junk (II) and write the right value */
+					fseek(writeDatFile,slaveCountPos,SEEK_SET);
+					fwriteshort(&numberOfSlaveItems,writeDatFile); /* overwrite junk (II) */
+					numberOfSlaveItems=0;
+					/* return to the end of the file to keep writing */
+					fseek(writeDatFile,0,SEEK_END);
+					slaveCountPos=ftell(writeDatFile); /* remember the juni (II) place */
+					fwriteshort(&numberOfSlaveItems,writeDatFile); /* junk (II) */
+
+					strncpy(index,res->id.index,5);
+				}
+				/* write slave index content */
+				fwriteshort(&(res->id.value),writeDatFile);
+				fwritelong(&(res->offset),writeDatFile);
+				fwriteshort(&(res->size),writeDatFile);
+				/* this is the flag writen in endian safe */
+				v=(res->flags>>16)&&0xff;
+				fwritechar(&v,writeDatFile);
+				v=(res->flags>>8)&&0xff;
+				fwritechar(&v,writeDatFile);
+				v=(res->flags)&&0xff;
+				fwritechar(&v,writeDatFile);
+
+				numberOfSlaveItems++;
+			} while ((res=resourceListGetElement(&resIndex)));
+			size2=ftell(writeDatFile)-size1;
+			fseek(writeDatFile,slaveCountPos,SEEK_SET);
+			fwriteshort(&numberOfSlaveItems,writeDatFile); /* overwrite junk (II) */
+		}
+	} else {
+		dontSave=1;
 	}
 
-	size2+=totalItems<<3;
+	/* Write totalItems */
 	fseek(writeDatFile,size1,SEEK_SET);
 	fwriteshort(&totalItems,writeDatFile); /* Definitive total items count */
-
+	
 	/* Write first 6 bytes header */
 	fseek(writeDatFile,0,SEEK_SET);
 	fwritelong(&size1,writeDatFile);
diff --git a/PR/src/lib/layers/reslist.c b/PR/src/lib/layers/reslist.c
index 66fa6d1..73e52d1 100644
--- a/PR/src/lib/layers/reslist.c
+++ b/PR/src/lib/layers/reslist.c
@@ -40,11 +40,15 @@ reslist.c: Princed Resources : Ordered Read-Only list implementarion
 /* resource list layer (that uses the abstract list layer primitives) */
 
 int resIdCmp(tResourceId a,tResourceId b) {
-	/* the number has the priority */
+	/* the index has the priority */
+	int c=strncmp(a.index,b.index,5);
+	if (c>0) return GT;
+	if (c<0) return LT;
+
+	/* at this point, the indexes are the same, so let's compare the number */
 	if (a.value>b.value) return GT;
 	if (a.value<b.value) return LT;
-	/* at this point, the numbers are the same, so the index name is the comparation */
-	return strncmp(a.index,b.index,4);	
+	return EQ;
 }
 
 int resCmp(const void* a,const void* b) {
diff --git a/PR/src/lib/xml/parse.c b/PR/src/lib/xml/parse.c
index e318156..f11f5c7 100644
--- a/PR/src/lib/xml/parse.c
+++ b/PR/src/lib/xml/parse.c
@@ -93,6 +93,7 @@ tTag* getTagStructure() {
 	t->paletteindex=NULL;
 	t->version=NULL;
 	t->number=NULL;
+	t->flags=NULL;
 
 	return t;
 }
@@ -113,6 +114,7 @@ void freeTagStructure(tTag* t) {
 	freeAllocation(t->paletteindex);
 	freeAllocation(t->version);
 	freeAllocation(t->number);
+	freeAllocation(t->flags);
 	free(t);
 }
 
@@ -142,6 +144,7 @@ int attribFill(char* attr,char* val, tTag* t) {
 	FillAttr(t->version,"version");
 	FillAttr(t->number,"levelnumber"); /* levelnumber is a number alias */
 	FillAttr(t->number,"number");
+	FillAttr(t->flags,"flags");
 
 	return PR_RESULT_ERR_XML_ATTR;
 }
@@ -362,6 +365,7 @@ tTag* makeTree(char** p,char* name, int* error,tTag* father) {
 		TotalInheritance(type);
 		TotalInheritance(file);
 		TotalInheritance(index);
+		TotalInheritance(flags);
 		/* PartialConcatInheritance(tag->path,father->path,tag->value); */
 		if ((tag->value==NULL)||(tag->path!=NULL)) {
 			/* Make sure paths do exist */
diff --git a/PR/src/lib/xml/search.c b/PR/src/lib/xml/search.c
index 306eb68..a5d4398 100644
--- a/PR/src/lib/xml/search.c
+++ b/PR/src/lib/xml/search.c
@@ -67,6 +67,7 @@ void workTag(const tTag* t,void* pass) {
 	const char* datFile=((tPassWork*)pass)->datFile;
 	tResourceList* r=((tPassWork*)pass)->r;
 	tResource res;
+	char* end;
 
 	/* Skipping conditions */
 	if (!equalsIgnoreCase(t->file,datFile))   return; /* If it doesn't belong to the given dat file */
@@ -94,6 +95,12 @@ void workTag(const tTag* t,void* pass) {
 	
 	/* Copy number, title, desc and path */
 	keepIntAttribute(number,unsigned char);    /* Transforms the char* levelnumer/number attribute into a char value, if error, demo level is used */
+	if (t->flags) {
+		res.flags=strtol(t->flags,&end,0);
+		if (*end) return;
+	} else {
+		res.flags=0;
+	}
 	keepStringAttribute(desc);  /* only for plv */
 	keepStringAttribute(name);  /* only for plv */
 	keepStringAttribute(path);
diff --git a/PR/src/lib/xml/tree.c b/PR/src/lib/xml/tree.c
index 2b47d5a..c576f32 100644
--- a/PR/src/lib/xml/tree.c
+++ b/PR/src/lib/xml/tree.c
@@ -84,7 +84,7 @@ char* toLower(const char* txt) {
 	return ret;			
 }
 
-void AddToUnknownXml(const char* vFiledatWithPath,tResourceId id,const char* ext,char type,const char* vDirExt,tResourceId pal,const char* vFiledat,int optionflag,int count) {
+void AddToUnknownXml(const char* vFiledatWithPath,tResourceId id,const char* ext,tResourceType type,const char* vDirExt,tResourceId pal,const char* vFiledat,int optionflag,int count, unsigned long int flags) {
 	/* Open file if not open */
 	if (unknownXmlFile==NULL) {
 		char xmlFile[MAX_FILENAME_SIZE];
@@ -94,7 +94,7 @@ void AddToUnknownXml(const char* vFiledatWithPath,tResourceId id,const char* ext
 		if (!writeOpen(xmlFile,&unknownXmlFile,optionflag)) return;
 
 		/* Save headers */
-		if (type==6) pal=id;
+		if (type==RES_TYPE_PALETTE) pal=id;
 		fprintf(unknownXmlFile,RES_XML_UNKNOWN_START,
 			vFiledat,vFiledatWithPath,pal.value,toLower(pal.index)
 		);
@@ -102,7 +102,7 @@ void AddToUnknownXml(const char* vFiledatWithPath,tResourceId id,const char* ext
 
 	/* Write item */
 	fprintf(unknownXmlFile,RES_XML_UNKNOWN_ITEM,
-		id.value,toLower(id.index),getExtDesc(type),count,ext,getExtDesc(type),getExtDesc(type),count
+		id.value,toLower(id.index),getExtDesc(type),count,ext,getExtDesc(type),flags,getExtDesc(type),count
 	); /* To the xml output */
 }
 
@@ -128,7 +128,7 @@ void getFileName(char* vFileext,const char* vDirExt,const tResource* r,const cha
 
 		/* set filename */
 		sprintf(vFileext,RES_XML_UNKNOWN_PATH""RES_XML_UNKNOWN_FILES,vDirExt,vDatFileName,getExtDesc(pos),typeCount[pos],extarray[pos]);
-		AddToUnknownXml(vDatFileName,r->id,extarray[pos],r->type,vDirExt,r->palette,vFiledat,optionflag,typeCount[pos]);
+		AddToUnknownXml(vDatFileName,r->id,extarray[pos],r->type,vDirExt,r->palette,vFiledat,optionflag,typeCount[pos],r->flags);
 	} else {
 		/* set filename */
 		sprintf(vFileext,"%s/%s",vDirExt,r->path);
diff --git a/PR/src/lib/xml/unknown.c b/PR/src/lib/xml/unknown.c
index 2b47d5a..c576f32 100644
--- a/PR/src/lib/xml/unknown.c
+++ b/PR/src/lib/xml/unknown.c
@@ -84,7 +84,7 @@ char* toLower(const char* txt) {
 	return ret;			
 }
 
-void AddToUnknownXml(const char* vFiledatWithPath,tResourceId id,const char* ext,char type,const char* vDirExt,tResourceId pal,const char* vFiledat,int optionflag,int count) {
+void AddToUnknownXml(const char* vFiledatWithPath,tResourceId id,const char* ext,tResourceType type,const char* vDirExt,tResourceId pal,const char* vFiledat,int optionflag,int count, unsigned long int flags) {
 	/* Open file if not open */
 	if (unknownXmlFile==NULL) {
 		char xmlFile[MAX_FILENAME_SIZE];
@@ -94,7 +94,7 @@ void AddToUnknownXml(const char* vFiledatWithPath,tResourceId id,const char* ext
 		if (!writeOpen(xmlFile,&unknownXmlFile,optionflag)) return;
 
 		/* Save headers */
-		if (type==6) pal=id;
+		if (type==RES_TYPE_PALETTE) pal=id;
 		fprintf(unknownXmlFile,RES_XML_UNKNOWN_START,
 			vFiledat,vFiledatWithPath,pal.value,toLower(pal.index)
 		);
@@ -102,7 +102,7 @@ void AddToUnknownXml(const char* vFiledatWithPath,tResourceId id,const char* ext
 
 	/* Write item */
 	fprintf(unknownXmlFile,RES_XML_UNKNOWN_ITEM,
-		id.value,toLower(id.index),getExtDesc(type),count,ext,getExtDesc(type),getExtDesc(type),count
+		id.value,toLower(id.index),getExtDesc(type),count,ext,getExtDesc(type),flags,getExtDesc(type),count
 	); /* To the xml output */
 }
 
@@ -128,7 +128,7 @@ void getFileName(char* vFileext,const char* vDirExt,const tResource* r,const cha
 
 		/* set filename */
 		sprintf(vFileext,RES_XML_UNKNOWN_PATH""RES_XML_UNKNOWN_FILES,vDirExt,vDatFileName,getExtDesc(pos),typeCount[pos],extarray[pos]);
-		AddToUnknownXml(vDatFileName,r->id,extarray[pos],r->type,vDirExt,r->palette,vFiledat,optionflag,typeCount[pos]);
+		AddToUnknownXml(vDatFileName,r->id,extarray[pos],r->type,vDirExt,r->palette,vFiledat,optionflag,typeCount[pos],r->flags);
 	} else {
 		/* set filename */
 		sprintf(vFileext,"%s/%s",vDirExt,r->path);
diff --git a/PR/src/xml/resources.xml b/PR/src/xml/resources.xml
index 0fcd554..d807dbe 100644
--- a/PR/src/xml/resources.xml
+++ b/PR/src/xml/resources.xml
@@ -1409,7 +1409,7 @@ resources.xml: Princed Resources : Resource tree
  <!--
       pop2: bird.dat
   -->
- <folder index="pahs" name="the pop2 bird guard" file="bird.dat" type="image" path="bird" palette="750" paletteindex="clap">
+ <folder index="pahs" name="the pop2 bird guard" file="bird.dat" type="image" path="bird" palette="750" paletteindex="clap" flags="0x400000">
   <item value="750" index="slap" path="bird.pal" type="palette">The bird palette</item>
   <folder path="fighting">
    <item value="751" path="frame01.bmp" type="image">Frame 01 (751)</item>