2 * blenkernel/packedFile.c - (cleaned up mar-01 nzc)
6 * ***** BEGIN GPL LICENSE BLOCK *****
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23 * All rights reserved.
25 * The Original Code is: all of this file.
27 * Contributor(s): none yet.
29 * ***** END GPL LICENSE BLOCK *****
46 #include "MEM_guardedalloc.h"
48 #include "DNA_image_types.h"
49 #include "DNA_sound_types.h"
50 #include "DNA_vfont_types.h"
51 #include "DNA_packedFile_types.h"
52 #include "DNA_scene_types.h"
54 #include "BLI_blenlib.h"
56 #include "BKE_utildefines.h"
57 #include "BKE_global.h"
59 #include "BKE_screen.h"
60 #include "BKE_sound.h"
61 #include "BKE_image.h"
63 #include "BKE_packedFile.h"
64 #include "BKE_report.h"
66 int seekPackedFile(PackedFile *pf, int offset, int whence)
68 int oldseek = -1, seek = 0;
74 seek = oldseek + offset;
77 seek = pf->size + offset;
87 } else if (seek > pf->size) {
96 void rewindPackedFile(PackedFile *pf)
98 seekPackedFile(pf, 0, SEEK_SET);
101 int readPackedFile(PackedFile *pf, void *data, int size)
103 if ((pf != NULL) && (size >= 0) && (data != NULL)) {
104 if (size + pf->seek > pf->size) {
105 size = pf->size - pf->seek;
109 memcpy(data, ((char *) pf->data) + pf->seek, size);
122 int countPackedFiles(Main *bmain)
129 // let's check if there are packed files...
130 for(ima=bmain->image.first; ima; ima=ima->id.next)
134 for(vf=bmain->vfont.first; vf; vf=vf->id.next)
139 for(sample=samples->first; sample; sample=sample->id.next)
140 if(sample->packedfile)
146 void freePackedFile(PackedFile *pf)
153 printf("freePackedFile: Trying to free a NULL pointer\n");
156 PackedFile *newPackedFileMemory(void *mem, int memlen)
158 PackedFile *pf = MEM_callocN(sizeof(*pf), "PackedFile");
165 PackedFile *newPackedFile(ReportList *reports, char *filename)
167 PackedFile *pf = NULL;
169 char name[FILE_MAXDIR+FILE_MAXFILE];
174 // convert relative filenames to absolute filenames
176 strcpy(name, filename);
177 BLI_convertstringcode(name, G.sce);
180 // and create a PackedFile structure
182 file= open(name, O_BINARY|O_RDONLY);
184 BKE_reportf(reports, RPT_ERROR, "Can't open file: %s", name);
186 filelen = BLI_filesize(file);
189 // MEM_mallocN complains about MEM_mallocN(0, "bla");
191 data = MEM_mallocN(1, "packFile");
193 data = MEM_mallocN(filelen, "packFile");
195 if (read(file, data, filelen) == filelen) {
196 pf = newPackedFileMemory(data, filelen);
207 void packAll(Main *bmain, ReportList *reports)
213 for(ima=bmain->image.first; ima; ima=ima->id.next)
214 if(ima->packedfile == NULL)
215 ima->packedfile = newPackedFile(reports, ima->name);
217 for(vf=bmain->vfont.first; vf; vf=vf->id.next)
218 if(vf->packedfile == NULL)
219 vf->packedfile = newPackedFile(reports, vf->name);
222 for(sample=samples->first; sample; sample=sample->id.next)
223 if(sample->packedfile == NULL)
224 sound_set_packedfile(sample, newPackedFile(reports, sample->name));
230 // attempt to create a function that generates an unique filename
231 // this will work when all funtions in fileops.c understand relative filenames...
233 char *find_new_name(char *name)
235 char tempname[FILE_MAXDIR + FILE_MAXFILE];
238 if (fop_exists(name)) {
239 for (number = 1; number <= 999; number++) {
240 sprintf(tempname, "%s.%03d", name, number);
241 if (! fop_exists(tempname)) {
247 newname = mallocN(strlen(tempname) + 1, "find_new_name");
248 strcpy(newname, tempname);
255 int writePackedFile(ReportList *reports, char *filename, PackedFile *pf, int guimode)
257 int file, number, remove_tmp = FALSE;
258 int ret_value = RET_OK;
259 char name[FILE_MAXDIR + FILE_MAXFILE];
260 char tempname[FILE_MAXDIR + FILE_MAXFILE];
263 if (guimode); //XXX waitcursor(1);
265 strcpy(name, filename);
266 BLI_convertstringcode(name, G.sce);
268 if (BLI_exists(name)) {
269 for (number = 1; number <= 999; number++) {
270 sprintf(tempname, "%s.%03d_", name, number);
271 if (! BLI_exists(tempname)) {
272 if (BLI_copy_fileops(name, tempname) == RET_OK) {
280 // make sure the path to the file exists...
281 BLI_make_existing_file(name);
283 file = open(name, O_BINARY + O_WRONLY + O_CREAT + O_TRUNC, 0666);
285 if (write(file, pf->data, pf->size) != pf->size) {
286 BKE_reportf(reports, RPT_ERROR, "Error writing file: %s", name);
287 ret_value = RET_ERROR;
291 BKE_reportf(reports, RPT_ERROR, "Error creating file: %s", name);
292 ret_value = RET_ERROR;
296 if (ret_value == RET_ERROR) {
297 if (BLI_rename(tempname, name) != 0) {
298 BKE_reportf(reports, RPT_ERROR, "Error restoring tempfile. Check files: '%s' '%s'", tempname, name);
301 if (BLI_delete(tempname, 0, 0) != 0) {
302 BKE_reportf(reports, RPT_ERROR, "Error deleting '%s' (ignored)", tempname);
307 if(guimode); //XXX waitcursor(0);
314 This function compares a packed file to a 'real' file.
315 It returns an integer indicating if:
317 PF_EQUAL - the packed file and original file are identical
318 PF_DIFFERENT - the packed file and original file differ
319 PF_NOFILE - the original file doens't exist
323 int checkPackedFile(char *filename, PackedFile *pf)
326 int ret_val, i, len, file;
328 char name[FILE_MAXDIR + FILE_MAXFILE];
330 strcpy(name, filename);
331 BLI_convertstringcode(name, G.sce);
333 if (stat(name, &st)) {
335 } else if (st.st_size != pf->size) {
336 ret_val = PF_DIFFERS;
338 // we'll have to compare the two...
340 file = open(name, O_BINARY | O_RDONLY);
346 for (i = 0; i < pf->size; i += sizeof(buf)) {
348 if (len > sizeof(buf)) {
352 if (read(file, buf, len) != len) {
354 ret_val = PF_DIFFERS;
357 if (memcmp(buf, ((char *)pf->data) + i, len)) {
358 ret_val = PF_DIFFERS;
371 unpackFile() looks at the existing files (abs_name, local_name) and a packed file.
373 It returns a char *to the existing file name / new file name or NULL when
374 there was an error or when the user desides to cancel the operation.
378 char *unpackFile(ReportList *reports, char *abs_name, char *local_name, PackedFile *pf, int how)
380 char menu[6 *(FILE_MAXDIR + FILE_MAXFILE + 100)];
381 char line[FILE_MAXDIR + FILE_MAXFILE + 100];
382 char *newname = NULL, *temp = NULL;
384 // char newabs[FILE_MAXDIR + FILE_MAXFILE];
385 // char newlocal[FILE_MAXDIR + FILE_MAXFILE];
396 // if file exists use it
397 if (BLI_exists(local_name)) {
401 // else fall through and create it
403 if (writePackedFile(reports, local_name, pf, 1) == RET_OK) {
407 case PF_USE_ORIGINAL:
408 // if file exists use it
409 if (BLI_exists(abs_name)) {
413 // else fall through and create it
414 case PF_WRITE_ORIGINAL:
415 if (writePackedFile(reports, abs_name, pf, 1) == RET_OK) {
420 printf("unpackFile: unknown return_value %d\n", how);
425 newname = MEM_mallocN(strlen(temp) + 1, "unpack_file newname");
426 strcpy(newname, temp);
434 int unpackVFont(ReportList *reports, VFont *vfont, int how)
436 char localname[FILE_MAXDIR + FILE_MAXFILE], fi[FILE_MAXFILE];
438 int ret_value = RET_ERROR;
441 strcpy(localname, vfont->name);
442 BLI_splitdirstring(localname, fi);
444 sprintf(localname, "//fonts/%s", fi);
446 newname = unpackFile(reports, vfont->name, localname, vfont->packedfile, how);
447 if (newname != NULL) {
449 freePackedFile(vfont->packedfile);
450 vfont->packedfile = 0;
451 strcpy(vfont->name, newname);
459 int unpackSample(ReportList *reports, bSample *sample, int how)
461 char localname[FILE_MAXDIR + FILE_MAX], fi[FILE_MAX];
463 int ret_value = RET_ERROR;
466 if (sample != NULL) {
467 strcpy(localname, sample->name);
468 BLI_splitdirstring(localname, fi);
469 sprintf(localname, "//samples/%s", fi);
471 newname = unpackFile(reports, sample->name, localname, sample->packedfile, how);
472 if (newname != NULL) {
473 strcpy(sample->name, newname);
476 pf = sample->packedfile;
477 // because samples and sounds can point to the
478 // same packedfile we have to check them all
479 sound_set_packedfile(sample, NULL);
489 int unpackImage(ReportList *reports, Image *ima, int how)
491 char localname[FILE_MAXDIR + FILE_MAX], fi[FILE_MAX];
493 int ret_value = RET_ERROR;
496 strcpy(localname, ima->name);
497 BLI_splitdirstring(localname, fi);
498 sprintf(localname, "//textures/%s", fi);
500 newname = unpackFile(reports, ima->name, localname, ima->packedfile, how);
501 if (newname != NULL) {
503 freePackedFile(ima->packedfile);
504 ima->packedfile = NULL;
505 strcpy(ima->name, newname);
507 BKE_image_signal(ima, NULL, IMA_SIGNAL_RELOAD);
514 void unpackAll(Main *bmain, ReportList *reports, int how)
520 for(ima=bmain->image.first; ima; ima=ima->id.next)
522 unpackImage(reports, ima, how);
524 for(vf=bmain->vfont.first; vf; vf=vf->id.next)
526 unpackVFont(reports, vf, how);
529 for(sample=samples->first; sample; sample=sample->id.next)
530 if(sample->packedfile)
531 unpackSample(reports, sample, how);