Upgraded to Pr 1.0 beta
[fp-git.git] / PR / src / lib / actions / export.c
1 /*  Princed V3 - Prince of Persia Level Editor for PC Version
2     Copyright (C) 2003 Princed Development Team
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
18     The authors of this program may be contacted at http://forum.princed.com.ar
19 */
20
21 /*
22 extract.c: Princed Resources : DAT Extractor
23 ¯¯¯¯¯¯¯¯¯
24  Copyright 2003, 2004 Princed Development Team
25   Created: 24 Aug 2003
26
27   Author: Enrique Calot <ecalot.cod@princed.com.ar>
28   Version: 1.01 (2003-Oct-23)
29   Version: 1.20 (2004-Mar-07)
30   Version: 1.30 (2004-Mar-15)
31
32  Note:
33   DO NOT remove this copyright notice
34 */
35
36 #include <stdio.h>
37 #include <string.h>
38 #include "pr.h"
39
40 #include "extract.h"
41 #include "dat.h"
42 #include "disk.h"
43 #include "memory.h"
44
45 #include "bmp.h"
46 #include "wav.h"
47 #include "pal.h"
48 #include "plv.h"
49 #include "mid.h"
50
51 extern FILE* outputStream;
52
53 /***************************************************************\
54 |                  I M P L E M E N T A T I O N                  |
55 \***************************************************************/
56
57 #define initializePaletteList \
58 for (id=0;id<MAX_RES_COUNT;id++) {\
59         if (r[id]!=NULL) {\
60                 r[id]->palAux=NULL;\
61         }\
62 }
63
64 /***************************************************************\
65 |                    M A I N   E X T R A C T                    |
66 \***************************************************************/
67
68
69 /*
70         Extracts a dat file
71         For parameter documentation, see pr.c
72 */
73
74 int extract(const char* vFiledat,const char* vDirExt, tResource* r[], int optionflag, const char* vDatFileName, const char* vDatAuthor,const char* backupExtension) {
75         char               vFileext[MAX_FILENAME_SIZE];
76         int                indexNumber;
77         int                ok;
78         long int           id;
79         tImage             image; /* this is used to make a persistent palette */
80         unsigned char*     data;
81         unsigned long  int size;
82         unsigned short int numberOfItems;
83         unsigned short int paletteId=0;
84
85         /* Initialize abstract variables to read this new DAT file */
86         if (ok=mReadBeginDatFile(&numberOfItems,vFiledat)) return ok;
87
88         /* Initializes the palette list */
89         initializePaletteList;
90
91         /* main loop */
92         ok=1;
93         for (indexNumber=0;ok&&(indexNumber<numberOfItems);indexNumber++) {
94                 id=mReadGetFileInDatFile(indexNumber,&data,&size);
95
96                 if (id<0) return -3; /* Read error */
97                 if (id==0xFFFF) continue; /* Tammo Jan Bug fix */
98                 if (id>=MAX_RES_COUNT) return -3; /* A file with an ID out of range will be treated as invalid */
99
100                 /* set resource information on this index entry */
101                 if (mReadInitResource(r+id,data,size)) return -2;
102
103                 if ((r[id]->type==RES_TYPE_PALETTE)||isInThePartialList(r[id]->path,id)) { /* If the resource was specified or is a palette, do the tasks */
104                         if (!(hasFlag(unknown_flag))) { /* If unknown flag is set do nothing but generate the unknown.xml file */
105                                 if (hasFlag(raw_flag)) r[id]->type=0; /* If "extract as raw" is set, type is 0 */
106
107                                 /* get save file name (if unknown document it in the xml) */
108                                 getFileName(vFileext,vDirExt,r[id],(unsigned short)id,vFiledat,vDatFileName,optionflag,backupExtension);
109
110                                 switch (r[id]->type) {
111                                         case RES_TYPE_LEVEL:
112                                                 ok=ok&&mFormatExportPlv(data,vFileext,size,r[id]->number,vDatFileName,r[id]->name,r[id]->desc,vDatAuthor,optionflag,backupExtension);
113                                                 break;
114                                         case RES_TYPE_BINARY: /* Binary files */
115                                         case RES_TYPE_RAW: /* Raw files */
116                                                 ok=ok&&writeData(data,1,vFileext,size,optionflag,backupExtension); /* Ignore checksum */
117                                                 break;
118                                         case RES_TYPE_PALETTE: /* save and remember palette file */
119                                                 /* This will remember the palette for the next images */
120                                                 r[id]->palAux=getMemory(size);
121                                                 memcpy(r[id]->palAux,data,size);
122                                                 if (!paletteId) { /* In case there is no loaded palettes load immediately the first found palette to clear garbage */
123                                                         mLoadPalette(data,image);
124                                                         paletteId=id;
125                                                 }
126                                                 /* This will export the palette */
127                                                 if (isInThePartialList(r[id]->path,id))  /* If the palette was specified extract it */
128                                                         ok=ok&&mFormatExportPal(data,vFileext,size,optionflag,backupExtension);
129                                                 break;
130                                         case RES_TYPE_PCSPEAKER: /* save pcs file */
131                                         case RES_TYPE_MIDI:     /* save midi file */
132                                                 ok=ok&&mFormatExportMid(data,vFileext,size,optionflag,backupExtension);
133                                                 break;
134                                         case RES_TYPE_WAVE: /* save wav file */
135                                                 ok=ok&&mFormatExportWav(data,vFileext,size,optionflag,backupExtension);
136                                                 break;
137                                         case RES_TYPE_IMAGE: /* save image */
138                                                 /* Palette handling */
139                                                 if (r[id]->palette!=paletteId) { /* The palette isn't the already loaded */
140                                                         if (r[id]->palette) { /* We need a palette */
141                                                                 /*
142                                                                         We need a palette and it is not the palette we have loaded in memory
143                                                                         So a new palette is going to be loaded.
144                                                                 */
145                                                                 if ((r[r[id]->palette]->palAux)!=NULL) { /* If this palette wasn't loaded, it becomes loaded */
146                                                                         mLoadPalette(r[r[id]->palette]->palAux,image);
147                                                                         paletteId=r[id]->palette; /* sets the new palette loaded */
148                                                                 }
149                                                         }
150                                                 }
151                                                 /* Export bitmap */
152                                                 ok=ok&&mFormatExportBmp(data,vFileext,size,image,optionflag,backupExtension);
153                                                 break;
154                                 }
155                                 /* Verbose information */
156                                 if (hasFlag(verbose_flag)) {
157                                         if (ok) {
158                                                 fprintf(outputStream,PR_TEXT_EXPORT_WORKING,getFileNameFromPath(vFileext));
159                                         } else {
160                                                 fprintf(outputStream,PR_TEXT_EXPORT_ERROR,getFileNameFromPath(vFileext));
161                                         }
162                                 }
163                         } else {
164                                 /* if the dat file is unknown, add it in the xml */
165                                 getFileName(vFileext,vDirExt,r[id],(unsigned short)id,vFiledat,vDatFileName,optionflag,backupExtension);
166                         }
167                         //freeAllocation(data);
168                 }
169         }
170
171         /* Free allocated resources, dynamic strings and the index */
172         for (id=0;id<MAX_RES_COUNT;id++) {
173                 if (r[id]!=NULL) {
174                         freeAllocation(r[id]->palAux);
175                         freeAllocation(r[id]->desc);
176                         freeAllocation(r[id]->name);
177                         freeAllocation(r[id]->path);
178                         free(r[id]);
179                 }
180         }
181         mReadCloseDatFile();
182
183         /* Close unknownXML */
184         endUnknownXml(optionflag,backupExtension);
185         return ok-1;
186 }
187