2011-02-23 10:52:22 +00:00
|
|
|
/*
|
2008-01-07 19:13:47 +00:00
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
2002-10-12 11:37:38 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
2008-01-07 19:13:47 +00:00
|
|
|
* of the License, or (at your option) any later version.
|
2002-10-12 11:37:38 +00:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
2010-02-12 13:34:04 +00:00
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2002-10-12 11:37:38 +00:00
|
|
|
*
|
|
|
|
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* The Original Code is: all of this file.
|
|
|
|
*
|
|
|
|
* Contributor(s): none yet.
|
|
|
|
*
|
2008-01-07 19:13:47 +00:00
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
2002-10-12 11:37:38 +00:00
|
|
|
*/
|
|
|
|
|
2011-02-27 20:40:57 +00:00
|
|
|
/** \file blender/blenkernel/intern/packedFile.c
|
|
|
|
* \ingroup bke
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
2002-11-25 12:02:15 +00:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
#ifndef WIN32
|
|
|
|
#include <unistd.h>
|
|
|
|
#else
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
#include <string.h>
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
|
|
#include "DNA_image_types.h"
|
2012-12-27 15:07:19 +00:00
|
|
|
#include "DNA_ID.h"
|
2013-01-27 14:28:45 +00:00
|
|
|
#include "DNA_packedFile_types.h"
|
2002-10-12 11:37:38 +00:00
|
|
|
#include "DNA_sound_types.h"
|
|
|
|
#include "DNA_vfont_types.h"
|
|
|
|
|
|
|
|
#include "BLI_blenlib.h"
|
2011-01-07 18:36:47 +00:00
|
|
|
#include "BLI_utildefines.h"
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2012-08-03 22:12:57 +00:00
|
|
|
#include "BKE_font.h"
|
2002-10-12 11:37:38 +00:00
|
|
|
#include "BKE_global.h"
|
|
|
|
#include "BKE_image.h"
|
2012-08-03 22:12:57 +00:00
|
|
|
#include "BKE_main.h"
|
2002-10-12 11:37:38 +00:00
|
|
|
#include "BKE_packedFile.h"
|
2009-06-30 19:20:45 +00:00
|
|
|
#include "BKE_report.h"
|
2012-08-03 22:12:57 +00:00
|
|
|
#include "BKE_sound.h"
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2009-09-06 13:20:05 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define open _open
|
|
|
|
#define close _close
|
|
|
|
#define read _read
|
|
|
|
#define write _write
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2009-06-30 19:20:45 +00:00
|
|
|
int seekPackedFile(PackedFile *pf, int offset, int whence)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
|
|
|
int oldseek = -1, seek = 0;
|
|
|
|
|
|
|
|
if (pf) {
|
|
|
|
oldseek = pf->seek;
|
2012-04-28 06:31:57 +00:00
|
|
|
switch (whence) {
|
2012-05-06 17:22:54 +00:00
|
|
|
case SEEK_CUR:
|
|
|
|
seek = oldseek + offset;
|
|
|
|
break;
|
|
|
|
case SEEK_END:
|
|
|
|
seek = pf->size + offset;
|
|
|
|
break;
|
|
|
|
case SEEK_SET:
|
|
|
|
seek = offset;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
oldseek = -1;
|
2013-07-21 08:16:37 +00:00
|
|
|
break;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
if (seek < 0) {
|
|
|
|
seek = 0;
|
2012-03-24 06:18:31 +00:00
|
|
|
}
|
|
|
|
else if (seek > pf->size) {
|
2002-10-12 11:37:38 +00:00
|
|
|
seek = pf->size;
|
|
|
|
}
|
|
|
|
pf->seek = seek;
|
|
|
|
}
|
|
|
|
|
|
|
|
return(oldseek);
|
|
|
|
}
|
|
|
|
|
2009-06-30 19:20:45 +00:00
|
|
|
void rewindPackedFile(PackedFile *pf)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
|
|
|
seekPackedFile(pf, 0, SEEK_SET);
|
|
|
|
}
|
|
|
|
|
2009-06-30 19:20:45 +00:00
|
|
|
int readPackedFile(PackedFile *pf, void *data, int size)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
|
|
|
if ((pf != NULL) && (size >= 0) && (data != NULL)) {
|
|
|
|
if (size + pf->seek > pf->size) {
|
|
|
|
size = pf->size - pf->seek;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size > 0) {
|
|
|
|
memcpy(data, ((char *) pf->data) + pf->seek, size);
|
2012-03-24 06:18:31 +00:00
|
|
|
}
|
|
|
|
else {
|
2002-10-12 11:37:38 +00:00
|
|
|
size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
pf->seek += size;
|
2012-03-24 06:18:31 +00:00
|
|
|
}
|
|
|
|
else {
|
2002-10-12 11:37:38 +00:00
|
|
|
size = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return(size);
|
|
|
|
}
|
|
|
|
|
2009-06-30 19:20:45 +00:00
|
|
|
int countPackedFiles(Main *bmain)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
|
|
|
Image *ima;
|
|
|
|
VFont *vf;
|
2009-08-09 21:16:39 +00:00
|
|
|
bSound *sound;
|
2009-06-30 19:20:45 +00:00
|
|
|
int count = 0;
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2012-07-07 22:51:57 +00:00
|
|
|
/* let's check if there are packed files... */
|
2012-05-06 17:22:54 +00:00
|
|
|
for (ima = bmain->image.first; ima; ima = ima->id.next)
|
2012-03-24 06:18:31 +00:00
|
|
|
if (ima->packedfile)
|
2002-10-12 11:37:38 +00:00
|
|
|
count++;
|
|
|
|
|
2012-05-06 17:22:54 +00:00
|
|
|
for (vf = bmain->vfont.first; vf; vf = vf->id.next)
|
2012-03-24 06:18:31 +00:00
|
|
|
if (vf->packedfile)
|
2002-10-12 11:37:38 +00:00
|
|
|
count++;
|
|
|
|
|
2012-05-06 17:22:54 +00:00
|
|
|
for (sound = bmain->sound.first; sound; sound = sound->id.next)
|
2012-03-24 06:18:31 +00:00
|
|
|
if (sound->packedfile)
|
2009-08-09 21:16:39 +00:00
|
|
|
count++;
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2009-06-30 19:20:45 +00:00
|
|
|
return count;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
2009-06-30 19:20:45 +00:00
|
|
|
void freePackedFile(PackedFile *pf)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2012-03-24 06:18:31 +00:00
|
|
|
if (pf) {
|
2002-10-12 11:37:38 +00:00
|
|
|
MEM_freeN(pf->data);
|
|
|
|
MEM_freeN(pf);
|
|
|
|
}
|
2009-06-30 19:20:45 +00:00
|
|
|
else
|
|
|
|
printf("freePackedFile: Trying to free a NULL pointer\n");
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
2012-08-21 10:39:02 +00:00
|
|
|
|
2012-08-21 10:44:10 +00:00
|
|
|
PackedFile *dupPackedFile(const PackedFile *pf_src)
|
2012-08-21 10:39:02 +00:00
|
|
|
{
|
|
|
|
PackedFile *pf_dst;
|
|
|
|
|
|
|
|
pf_dst = MEM_dupallocN(pf_src);
|
|
|
|
pf_dst->data = MEM_dupallocN(pf_src->data);
|
|
|
|
|
|
|
|
return pf_dst;
|
|
|
|
}
|
|
|
|
|
2009-06-30 19:20:45 +00:00
|
|
|
PackedFile *newPackedFileMemory(void *mem, int memlen)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2009-06-30 19:20:45 +00:00
|
|
|
PackedFile *pf = MEM_callocN(sizeof(*pf), "PackedFile");
|
2002-10-12 11:37:38 +00:00
|
|
|
pf->data = mem;
|
|
|
|
pf->size = memlen;
|
|
|
|
|
|
|
|
return pf;
|
|
|
|
}
|
|
|
|
|
2011-10-08 11:02:58 +00:00
|
|
|
PackedFile *newPackedFile(ReportList *reports, const char *filename, const char *basepath)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2009-06-30 19:20:45 +00:00
|
|
|
PackedFile *pf = NULL;
|
2002-10-12 11:37:38 +00:00
|
|
|
int file, filelen;
|
2011-11-26 04:07:38 +00:00
|
|
|
char name[FILE_MAX];
|
2009-06-30 19:20:45 +00:00
|
|
|
void *data;
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2009-10-15 08:39:47 +00:00
|
|
|
/* render result has no filename and can be ignored
|
|
|
|
* any other files with no name can be ignored too */
|
2012-05-06 17:22:54 +00:00
|
|
|
if (filename[0] == '\0')
|
2009-10-15 08:39:47 +00:00
|
|
|
return NULL;
|
|
|
|
|
2008-01-01 18:16:10 +00:00
|
|
|
//XXX waitcursor(1);
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2012-07-07 22:51:57 +00:00
|
|
|
/* convert relative filenames to absolute filenames */
|
|
|
|
|
2011-10-19 23:10:54 +00:00
|
|
|
BLI_strncpy(name, filename, sizeof(name));
|
2011-10-08 11:02:58 +00:00
|
|
|
BLI_path_abs(name, basepath);
|
2012-07-07 22:51:57 +00:00
|
|
|
|
|
|
|
/* open the file
|
|
|
|
* and create a PackedFile structure */
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2012-05-06 17:22:54 +00:00
|
|
|
file = BLI_open(name, O_BINARY | O_RDONLY, 0);
|
2013-08-04 17:30:47 +00:00
|
|
|
if (file < 0) {
|
2012-10-21 14:02:30 +00:00
|
|
|
BKE_reportf(reports, RPT_ERROR, "Unable to pack file, source path '%s' not found", name);
|
2012-03-24 06:18:31 +00:00
|
|
|
}
|
|
|
|
else {
|
2011-10-22 15:35:49 +00:00
|
|
|
filelen = BLI_file_descriptor_size(file);
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
if (filelen == 0) {
|
2012-07-07 22:51:57 +00:00
|
|
|
/* MEM_mallocN complains about MEM_mallocN(0, "bla");
|
|
|
|
* we don't care.... */
|
2002-10-12 11:37:38 +00:00
|
|
|
data = MEM_mallocN(1, "packFile");
|
2012-03-24 06:18:31 +00:00
|
|
|
}
|
|
|
|
else {
|
2002-10-12 11:37:38 +00:00
|
|
|
data = MEM_mallocN(filelen, "packFile");
|
|
|
|
}
|
|
|
|
if (read(file, data, filelen) == filelen) {
|
|
|
|
pf = newPackedFileMemory(data, filelen);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(file);
|
|
|
|
}
|
|
|
|
|
2008-01-01 18:16:10 +00:00
|
|
|
//XXX waitcursor(0);
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
return (pf);
|
|
|
|
}
|
|
|
|
|
2012-12-27 15:07:19 +00:00
|
|
|
/* no libraries for now */
|
2009-06-30 19:20:45 +00:00
|
|
|
void packAll(Main *bmain, ReportList *reports)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
|
|
|
Image *ima;
|
2012-08-03 22:12:57 +00:00
|
|
|
VFont *vfont;
|
2009-08-09 21:16:39 +00:00
|
|
|
bSound *sound;
|
2013-01-27 14:28:45 +00:00
|
|
|
int tot = 0;
|
2010-10-10 21:39:24 +00:00
|
|
|
|
2012-05-06 17:22:54 +00:00
|
|
|
for (ima = bmain->image.first; ima; ima = ima->id.next) {
|
|
|
|
if (ima->packedfile == NULL && ima->id.lib == NULL) {
|
|
|
|
if (ima->source == IMA_SRC_FILE) {
|
2011-10-08 11:02:58 +00:00
|
|
|
ima->packedfile = newPackedFile(reports, ima->name, ID_BLEND_PATH(bmain, &ima->id));
|
2013-01-27 14:28:45 +00:00
|
|
|
tot ++;
|
2010-10-10 21:39:24 +00:00
|
|
|
}
|
2012-03-24 06:18:31 +00:00
|
|
|
else if (ELEM(ima->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE)) {
|
2012-10-13 15:44:50 +00:00
|
|
|
BKE_reportf(reports, RPT_WARNING, "Image '%s' skipped, movies and image sequences not supported",
|
|
|
|
ima->id.name + 2);
|
2010-10-10 21:39:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2013-01-27 14:28:45 +00:00
|
|
|
for (vfont = bmain->vfont.first; vfont; vfont = vfont->id.next) {
|
|
|
|
if (vfont->packedfile == NULL && vfont->id.lib == NULL && BKE_vfont_is_builtin(vfont) == FALSE) {
|
2012-08-03 22:12:57 +00:00
|
|
|
vfont->packedfile = newPackedFile(reports, vfont->name, bmain->name);
|
2013-01-27 14:28:45 +00:00
|
|
|
tot ++;
|
|
|
|
}
|
|
|
|
}
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2013-01-27 14:28:45 +00:00
|
|
|
for (sound = bmain->sound.first; sound; sound = sound->id.next) {
|
|
|
|
if (sound->packedfile == NULL && sound->id.lib == NULL) {
|
2011-10-08 11:02:58 +00:00
|
|
|
sound->packedfile = newPackedFile(reports, sound->name, bmain->name);
|
2013-01-27 14:28:45 +00:00
|
|
|
tot++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tot == 0)
|
2013-03-27 13:28:51 +00:00
|
|
|
BKE_report(reports, RPT_INFO, "No new files have been packed");
|
2013-01-27 14:28:45 +00:00
|
|
|
else
|
|
|
|
BKE_reportf(reports, RPT_INFO, "Packed %d files", tot);
|
|
|
|
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-19 23:10:54 +00:00
|
|
|
#if 0
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
// attempt to create a function that generates an unique filename
|
|
|
|
// this will work when all funtions in fileops.c understand relative filenames...
|
|
|
|
|
2009-09-14 16:52:06 +00:00
|
|
|
static char *find_new_name(char *name)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2011-11-26 04:07:38 +00:00
|
|
|
char tempname[FILE_MAX];
|
2009-06-30 19:20:45 +00:00
|
|
|
char *newname;
|
2011-10-19 23:10:54 +00:00
|
|
|
size_t len;
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
if (fop_exists(name)) {
|
|
|
|
for (number = 1; number <= 999; number++) {
|
2012-01-11 12:33:51 +00:00
|
|
|
BLI_snprintf(tempname, sizeof(tempname), "%s.%03d", name, number);
|
2012-05-06 17:22:54 +00:00
|
|
|
if (!fop_exists(tempname)) {
|
2002-10-12 11:37:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-05-06 17:22:54 +00:00
|
|
|
len = strlen(tempname) + 1;
|
2011-10-19 23:10:54 +00:00
|
|
|
newname = MEM_mallocN(len, "find_new_name");
|
|
|
|
memcpy(newname, tempname, len * sizeof(char));
|
|
|
|
return newname;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
2011-10-19 23:10:54 +00:00
|
|
|
#endif
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2010-11-19 02:14:18 +00:00
|
|
|
int writePackedFile(ReportList *reports, const char *filename, PackedFile *pf, int guimode)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
|
|
|
int file, number, remove_tmp = FALSE;
|
|
|
|
int ret_value = RET_OK;
|
2011-11-26 04:07:38 +00:00
|
|
|
char name[FILE_MAX];
|
|
|
|
char tempname[FILE_MAX];
|
2012-05-06 17:22:54 +00:00
|
|
|
/* void *data; */
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2010-04-24 10:08:07 +00:00
|
|
|
if (guimode) {} //XXX waitcursor(1);
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2011-10-19 23:10:54 +00:00
|
|
|
BLI_strncpy(name, filename, sizeof(name));
|
2010-10-18 06:41:16 +00:00
|
|
|
BLI_path_abs(name, G.main->name);
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
if (BLI_exists(name)) {
|
|
|
|
for (number = 1; number <= 999; number++) {
|
2011-10-19 23:10:54 +00:00
|
|
|
BLI_snprintf(tempname, sizeof(tempname), "%s.%03d_", name, number);
|
2012-05-06 17:22:54 +00:00
|
|
|
if (!BLI_exists(tempname)) {
|
2011-10-22 15:35:49 +00:00
|
|
|
if (BLI_copy(name, tempname) == RET_OK) {
|
2002-10-12 11:37:38 +00:00
|
|
|
remove_tmp = TRUE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-07 22:51:57 +00:00
|
|
|
/* make sure the path to the file exists... */
|
Giant commit!
A full detailed description of this will be done later... is several days
of work. Here's a summary:
Render:
- Full cleanup of render code, removing *all* globals and bad level calls
all over blender. Render module is now not called abusive anymore
- API-fied calls to rendering
- Full recode of internal render pipeline. Is now rendering tiles by
default, prepared for much smarter 'bucket' render later.
- Each thread now can render a full part
- Renders were tested with 4 threads, goes fine, apart from some lookup
tables in softshadow and AO still
- Rendering is prepared to do multiple layers and passes
- No single 32 bits trick in render code anymore, all 100% floats now.
Writing images/movies
- moved writing images to blender kernel (bye bye 'schrijfplaatje'!)
- made a new Movie handle system, also in kernel. This will enable much
easier use of movies in Blender
PreviewRender:
- Using new render API, previewrender (in buttons) now uses regular render
code to generate images.
- new datafile 'preview.blend.c' has the preview scenes in it
- previews get rendered in exact displayed size (1 pixel = 1 pixel)
3D Preview render
- new; press Pkey in 3d window, for a panel that continuously renders
(pkey is for games, i know... but we dont do that in orange now!)
- this render works nearly identical to buttons-preview render, so it stops
rendering on any event (mouse, keyboard, etc)
- on moving/scaling the panel, the render code doesn't recreate all geometry
- same for shifting/panning view
- all other operations (now) regenerate the full render database still.
- this is WIP... but big fun, especially for simple scenes!
Compositor
- Using same node system as now in use for shaders, you can composit images
- works pretty straightforward... needs much more options/tools and integration
with rendering still
- is not threaded yet, nor is so smart to only recalculate changes... will be
done soon!
- the "Render Result" node will get all layers/passes as output sockets
- The "Output" node renders to a builtin image, which you can view in the Image
window. (yes, output nodes to render-result, and to files, is on the list!)
The Bad News
- "Unified Render" is removed. It might come back in some stage, but this
system should be built from scratch. I can't really understand this code...
I expect it is not much needed, especially with advanced layer/passes
control
- Panorama render, Field render, Motion blur, is not coded yet... (I had to
recode every single feature in render, so...!)
- Lens Flare is also not back... needs total revision, might become composit
effect though (using zbuffer for visibility)
- Part render is gone! (well, thats obvious, its default now).
- The render window is only restored with limited functionality... I am going
to check first the option to render to a Image window, so Blender can become
a true single-window application. :)
For example, the 'Spare render buffer' (jkey) doesnt work.
- Render with border, now default creates a smaller image
- No zbuffers are written yet... on the todo!
- Scons files and MSVC will need work to get compiling again
OK... thats what I can quickly recall. Now go compiling!
2006-01-23 22:05:47 +00:00
|
|
|
BLI_make_existing_file(name);
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2012-03-20 02:17:37 +00:00
|
|
|
file = BLI_open(name, O_BINARY + O_WRONLY + O_CREAT + O_TRUNC, 0666);
|
2013-08-04 17:30:47 +00:00
|
|
|
if (file < 0) {
|
|
|
|
BKE_reportf(reports, RPT_ERROR, "Error creating file '%s'", name);
|
|
|
|
ret_value = RET_ERROR;
|
|
|
|
}
|
|
|
|
else {
|
2002-10-12 11:37:38 +00:00
|
|
|
if (write(file, pf->data, pf->size) != pf->size) {
|
2012-10-21 14:02:30 +00:00
|
|
|
BKE_reportf(reports, RPT_ERROR, "Error writing file '%s'", name);
|
2002-10-12 11:37:38 +00:00
|
|
|
ret_value = RET_ERROR;
|
|
|
|
}
|
2013-08-04 17:30:47 +00:00
|
|
|
else {
|
2013-01-27 14:28:45 +00:00
|
|
|
BKE_reportf(reports, RPT_INFO, "Saved packed file to: %s", name);
|
2013-08-04 17:30:47 +00:00
|
|
|
}
|
2013-01-27 14:28:45 +00:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
close(file);
|
2012-03-24 06:18:31 +00:00
|
|
|
}
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
if (remove_tmp) {
|
|
|
|
if (ret_value == RET_ERROR) {
|
2008-04-12 15:03:47 +00:00
|
|
|
if (BLI_rename(tempname, name) != 0) {
|
2012-10-21 14:02:30 +00:00
|
|
|
BKE_reportf(reports, RPT_ERROR, "Error restoring temp file (check files '%s' '%s')", tempname, name);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
2012-03-24 06:18:31 +00:00
|
|
|
}
|
|
|
|
else {
|
2013-03-05 03:53:22 +00:00
|
|
|
if (BLI_delete(tempname, false, false) != 0) {
|
2009-06-30 19:20:45 +00:00
|
|
|
BKE_reportf(reports, RPT_ERROR, "Error deleting '%s' (ignored)", tempname);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (guimode) {} //XXX waitcursor(0);
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
return (ret_value);
|
|
|
|
}
|
|
|
|
|
2012-03-03 20:19:11 +00:00
|
|
|
/*
|
|
|
|
* This function compares a packed file to a 'real' file.
|
|
|
|
* It returns an integer indicating if:
|
|
|
|
*
|
|
|
|
* PF_EQUAL - the packed file and original file are identical
|
|
|
|
* PF_DIFFERENT - the packed file and original file differ
|
|
|
|
* PF_NOFILE - the original file doens't exist
|
|
|
|
*/
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2010-11-19 02:14:18 +00:00
|
|
|
int checkPackedFile(const char *filename, PackedFile *pf)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
int ret_val, i, len, file;
|
|
|
|
char buf[4096];
|
2011-11-26 04:07:38 +00:00
|
|
|
char name[FILE_MAX];
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2011-10-19 23:10:54 +00:00
|
|
|
BLI_strncpy(name, filename, sizeof(name));
|
2010-10-18 06:41:16 +00:00
|
|
|
BLI_path_abs(name, G.main->name);
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2013-06-25 14:48:30 +00:00
|
|
|
if (BLI_stat(name, &st)) {
|
2002-10-12 11:37:38 +00:00
|
|
|
ret_val = PF_NOFILE;
|
2012-03-24 06:18:31 +00:00
|
|
|
}
|
|
|
|
else if (st.st_size != pf->size) {
|
2002-10-12 11:37:38 +00:00
|
|
|
ret_val = PF_DIFFERS;
|
2012-03-24 06:18:31 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-07-07 22:51:57 +00:00
|
|
|
/* we'll have to compare the two... */
|
|
|
|
|
2012-03-20 02:17:37 +00:00
|
|
|
file = BLI_open(name, O_BINARY | O_RDONLY, 0);
|
2002-10-12 11:37:38 +00:00
|
|
|
if (file < 0) {
|
|
|
|
ret_val = PF_NOFILE;
|
2012-03-24 06:18:31 +00:00
|
|
|
}
|
|
|
|
else {
|
2002-10-12 11:37:38 +00:00
|
|
|
ret_val = PF_EQUAL;
|
2012-07-07 22:51:57 +00:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
for (i = 0; i < pf->size; i += sizeof(buf)) {
|
|
|
|
len = pf->size - i;
|
|
|
|
if (len > sizeof(buf)) {
|
|
|
|
len = sizeof(buf);
|
|
|
|
}
|
2012-07-07 22:51:57 +00:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
if (read(file, buf, len) != len) {
|
2012-07-07 22:51:57 +00:00
|
|
|
/* read error ... */
|
2002-10-12 11:37:38 +00:00
|
|
|
ret_val = PF_DIFFERS;
|
|
|
|
break;
|
2012-03-24 06:18:31 +00:00
|
|
|
}
|
|
|
|
else {
|
2002-10-12 11:37:38 +00:00
|
|
|
if (memcmp(buf, ((char *)pf->data) + i, len)) {
|
|
|
|
ret_val = PF_DIFFERS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-08-01 06:27:40 +00:00
|
|
|
|
|
|
|
close(file);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return(ret_val);
|
|
|
|
}
|
|
|
|
|
2012-03-03 20:19:11 +00:00
|
|
|
/* unpackFile() looks at the existing files (abs_name, local_name) and a packed file.
|
|
|
|
*
|
|
|
|
* It returns a char *to the existing file name / new file name or NULL when
|
2012-04-25 06:06:40 +00:00
|
|
|
* there was an error or when the user decides to cancel the operation.
|
2012-03-03 20:19:11 +00:00
|
|
|
*/
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2011-10-19 23:10:54 +00:00
|
|
|
char *unpackFile(ReportList *reports, const char *abs_name, const char *local_name, PackedFile *pf, int how)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2011-10-19 23:10:54 +00:00
|
|
|
char *newname = NULL;
|
|
|
|
const char *temp = NULL;
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2011-11-26 04:07:38 +00:00
|
|
|
// char newabs[FILE_MAX];
|
|
|
|
// char newlocal[FILE_MAX];
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
if (pf != NULL) {
|
|
|
|
switch (how) {
|
|
|
|
case -1:
|
|
|
|
case PF_KEEP:
|
|
|
|
break;
|
2006-12-20 17:57:56 +00:00
|
|
|
case PF_REMOVE:
|
2012-05-06 17:22:54 +00:00
|
|
|
temp = abs_name;
|
2006-12-20 17:57:56 +00:00
|
|
|
break;
|
2002-10-12 11:37:38 +00:00
|
|
|
case PF_USE_LOCAL:
|
2012-07-07 22:51:57 +00:00
|
|
|
/* if file exists use it */
|
2002-10-12 11:37:38 +00:00
|
|
|
if (BLI_exists(local_name)) {
|
|
|
|
temp = local_name;
|
|
|
|
break;
|
|
|
|
}
|
2013-07-21 08:16:37 +00:00
|
|
|
/* else create it */
|
|
|
|
/* fall-through */
|
2002-10-12 11:37:38 +00:00
|
|
|
case PF_WRITE_LOCAL:
|
2009-06-30 19:20:45 +00:00
|
|
|
if (writePackedFile(reports, local_name, pf, 1) == RET_OK) {
|
2002-10-12 11:37:38 +00:00
|
|
|
temp = local_name;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PF_USE_ORIGINAL:
|
2012-07-07 22:51:57 +00:00
|
|
|
/* if file exists use it */
|
2002-10-12 11:37:38 +00:00
|
|
|
if (BLI_exists(abs_name)) {
|
2013-01-27 14:28:45 +00:00
|
|
|
BKE_reportf(reports, RPT_INFO, "Use existing file (instead of packed): %s", abs_name);
|
2002-10-12 11:37:38 +00:00
|
|
|
temp = abs_name;
|
|
|
|
break;
|
|
|
|
}
|
2013-07-21 08:16:37 +00:00
|
|
|
/* else create it */
|
|
|
|
/* fall-through */
|
2002-10-12 11:37:38 +00:00
|
|
|
case PF_WRITE_ORIGINAL:
|
2009-06-30 19:20:45 +00:00
|
|
|
if (writePackedFile(reports, abs_name, pf, 1) == RET_OK) {
|
2002-10-12 11:37:38 +00:00
|
|
|
temp = abs_name;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("unpackFile: unknown return_value %d\n", how);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (temp) {
|
2012-05-06 17:22:54 +00:00
|
|
|
newname = BLI_strdup(temp);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-19 23:10:54 +00:00
|
|
|
return newname;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-30 19:20:45 +00:00
|
|
|
int unpackVFont(ReportList *reports, VFont *vfont, int how)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2011-11-26 04:07:38 +00:00
|
|
|
char localname[FILE_MAX], fi[FILE_MAXFILE];
|
2009-06-30 19:20:45 +00:00
|
|
|
char *newname;
|
2002-10-12 11:37:38 +00:00
|
|
|
int ret_value = RET_ERROR;
|
|
|
|
|
|
|
|
if (vfont != NULL) {
|
2013-03-05 06:26:10 +00:00
|
|
|
BLI_split_file_part(vfont->name, fi, sizeof(fi));
|
2011-10-19 23:10:54 +00:00
|
|
|
BLI_snprintf(localname, sizeof(localname), "//fonts/%s", fi);
|
2009-06-30 19:20:45 +00:00
|
|
|
newname = unpackFile(reports, vfont->name, localname, vfont->packedfile, how);
|
2002-10-12 11:37:38 +00:00
|
|
|
if (newname != NULL) {
|
|
|
|
ret_value = RET_OK;
|
|
|
|
freePackedFile(vfont->packedfile);
|
2011-02-13 10:52:18 +00:00
|
|
|
vfont->packedfile = NULL;
|
2011-10-19 23:10:54 +00:00
|
|
|
BLI_strncpy(vfont->name, newname, sizeof(vfont->name));
|
2002-10-12 11:37:38 +00:00
|
|
|
MEM_freeN(newname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ret_value);
|
|
|
|
}
|
|
|
|
|
2010-11-13 13:44:45 +00:00
|
|
|
int unpackSound(Main *bmain, ReportList *reports, bSound *sound, int how)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2006-12-21 22:30:30 +00:00
|
|
|
char localname[FILE_MAXDIR + FILE_MAX], fi[FILE_MAX];
|
2009-06-30 19:20:45 +00:00
|
|
|
char *newname;
|
2002-10-12 11:37:38 +00:00
|
|
|
int ret_value = RET_ERROR;
|
2009-08-09 21:16:39 +00:00
|
|
|
|
|
|
|
if (sound != NULL) {
|
2013-03-05 06:26:10 +00:00
|
|
|
BLI_split_file_part(sound->name, fi, sizeof(fi));
|
2011-10-19 23:10:54 +00:00
|
|
|
BLI_snprintf(localname, sizeof(localname), "//sounds/%s", fi);
|
2009-08-09 21:16:39 +00:00
|
|
|
newname = unpackFile(reports, sound->name, localname, sound->packedfile, how);
|
2002-10-12 11:37:38 +00:00
|
|
|
if (newname != NULL) {
|
2011-10-19 23:10:54 +00:00
|
|
|
BLI_strncpy(sound->name, newname, sizeof(sound->name));
|
2002-10-12 11:37:38 +00:00
|
|
|
MEM_freeN(newname);
|
|
|
|
|
2009-08-09 21:16:39 +00:00
|
|
|
freePackedFile(sound->packedfile);
|
2011-02-13 10:52:18 +00:00
|
|
|
sound->packedfile = NULL;
|
2009-08-09 21:16:39 +00:00
|
|
|
|
2010-11-13 13:44:45 +00:00
|
|
|
sound_load(bmain, sound);
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
ret_value = RET_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return(ret_value);
|
|
|
|
}
|
|
|
|
|
2009-06-30 19:20:45 +00:00
|
|
|
int unpackImage(ReportList *reports, Image *ima, int how)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2006-12-21 22:30:30 +00:00
|
|
|
char localname[FILE_MAXDIR + FILE_MAX], fi[FILE_MAX];
|
2009-06-30 19:20:45 +00:00
|
|
|
char *newname;
|
2002-10-12 11:37:38 +00:00
|
|
|
int ret_value = RET_ERROR;
|
|
|
|
|
2012-03-19 20:47:17 +00:00
|
|
|
if (ima != NULL && ima->name[0]) {
|
2013-03-05 06:26:10 +00:00
|
|
|
BLI_split_file_part(ima->name, fi, sizeof(fi));
|
2011-10-19 23:10:54 +00:00
|
|
|
BLI_snprintf(localname, sizeof(localname), "//textures/%s", fi);
|
2009-06-30 19:20:45 +00:00
|
|
|
newname = unpackFile(reports, ima->name, localname, ima->packedfile, how);
|
2002-10-12 11:37:38 +00:00
|
|
|
if (newname != NULL) {
|
|
|
|
ret_value = RET_OK;
|
|
|
|
freePackedFile(ima->packedfile);
|
2006-12-20 17:57:56 +00:00
|
|
|
ima->packedfile = NULL;
|
2011-10-19 23:10:54 +00:00
|
|
|
BLI_strncpy(ima->name, newname, sizeof(ima->name));
|
2002-10-12 11:37:38 +00:00
|
|
|
MEM_freeN(newname);
|
2006-12-20 17:57:56 +00:00
|
|
|
BKE_image_signal(ima, NULL, IMA_SIGNAL_RELOAD);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return(ret_value);
|
|
|
|
}
|
|
|
|
|
2012-12-27 15:07:19 +00:00
|
|
|
int unpackLibraries(Main *bmain, ReportList *reports)
|
|
|
|
{
|
|
|
|
Library *lib;
|
|
|
|
char *newname;
|
|
|
|
int ret_value = RET_ERROR;
|
|
|
|
|
|
|
|
for (lib = bmain->library.first; lib; lib = lib->id.next) {
|
|
|
|
if (lib->packedfile && lib->name[0]) {
|
|
|
|
|
|
|
|
newname = unpackFile(reports, lib->filepath, lib->filepath, lib->packedfile, PF_WRITE_ORIGINAL);
|
|
|
|
if (newname != NULL) {
|
|
|
|
ret_value = RET_OK;
|
|
|
|
|
2013-01-12 11:59:22 +00:00
|
|
|
printf("Unpacked .blend library: %s\n", newname);
|
2012-12-27 15:07:19 +00:00
|
|
|
|
|
|
|
freePackedFile(lib->packedfile);
|
|
|
|
lib->packedfile = NULL;
|
|
|
|
|
|
|
|
MEM_freeN(newname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return(ret_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void packLibraries(Main *bmain, ReportList *reports)
|
|
|
|
{
|
|
|
|
Library *lib;
|
|
|
|
|
2013-01-12 11:59:22 +00:00
|
|
|
/* test for relativenss */
|
|
|
|
for (lib = bmain->library.first; lib; lib = lib->id.next)
|
2013-03-04 19:27:51 +00:00
|
|
|
if (!BLI_path_is_rel(lib->name))
|
2013-01-12 11:59:22 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
if (lib) {
|
|
|
|
BKE_reportf(reports, RPT_ERROR, "Cannot pack absolute file: '%s'", lib->name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-27 15:07:19 +00:00
|
|
|
for (lib = bmain->library.first; lib; lib = lib->id.next)
|
|
|
|
if (lib->packedfile == NULL)
|
|
|
|
lib->packedfile = newPackedFile(reports, lib->name, bmain->name);
|
|
|
|
}
|
|
|
|
|
2009-06-30 19:20:45 +00:00
|
|
|
void unpackAll(Main *bmain, ReportList *reports, int how)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
|
|
|
Image *ima;
|
|
|
|
VFont *vf;
|
2009-08-09 21:16:39 +00:00
|
|
|
bSound *sound;
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2012-05-06 17:22:54 +00:00
|
|
|
for (ima = bmain->image.first; ima; ima = ima->id.next)
|
2012-03-24 06:18:31 +00:00
|
|
|
if (ima->packedfile)
|
2009-06-30 19:20:45 +00:00
|
|
|
unpackImage(reports, ima, how);
|
|
|
|
|
2012-05-06 17:22:54 +00:00
|
|
|
for (vf = bmain->vfont.first; vf; vf = vf->id.next)
|
2012-03-24 06:18:31 +00:00
|
|
|
if (vf->packedfile)
|
2009-06-30 19:20:45 +00:00
|
|
|
unpackVFont(reports, vf, how);
|
|
|
|
|
2012-05-06 17:22:54 +00:00
|
|
|
for (sound = bmain->sound.first; sound; sound = sound->id.next)
|
2012-03-24 06:18:31 +00:00
|
|
|
if (sound->packedfile)
|
2010-11-13 13:44:45 +00:00
|
|
|
unpackSound(bmain, reports, sound, how);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
2009-06-30 19:20:45 +00:00
|
|
|
|
2013-01-27 14:28:45 +00:00
|
|
|
/* ID should be not NULL, return 1 if there's a packed file */
|
2013-03-09 05:35:49 +00:00
|
|
|
bool BKE_pack_check(ID *id)
|
2013-01-27 14:28:45 +00:00
|
|
|
{
|
|
|
|
if (GS(id->name) == ID_IM) {
|
|
|
|
Image *ima = (Image *)id;
|
|
|
|
return ima->packedfile != NULL;
|
|
|
|
}
|
|
|
|
if (GS(id->name) == ID_VF) {
|
|
|
|
VFont *vf = (VFont *)id;
|
|
|
|
return vf->packedfile != NULL;
|
|
|
|
}
|
|
|
|
if (GS(id->name) == ID_SO) {
|
|
|
|
bSound *snd = (bSound *)id;
|
|
|
|
return snd->packedfile != NULL;
|
|
|
|
}
|
|
|
|
if (GS(id->name) == ID_LI) {
|
|
|
|
Library *li = (Library *)id;
|
|
|
|
return li->packedfile != NULL;
|
|
|
|
}
|
2013-03-09 05:35:49 +00:00
|
|
|
return false;
|
2013-01-27 14:28:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ID should be not NULL */
|
|
|
|
void BKE_unpack_id(Main *bmain, ID *id, ReportList *reports, int how)
|
|
|
|
{
|
|
|
|
if (GS(id->name) == ID_IM) {
|
|
|
|
Image *ima = (Image *)id;
|
|
|
|
if (ima->packedfile)
|
|
|
|
unpackImage(reports, ima, how);
|
|
|
|
}
|
|
|
|
if (GS(id->name) == ID_VF) {
|
|
|
|
VFont *vf = (VFont *)id;
|
|
|
|
if (vf->packedfile)
|
|
|
|
unpackVFont(reports, vf, how);
|
|
|
|
}
|
|
|
|
if (GS(id->name) == ID_SO) {
|
|
|
|
bSound *snd = (bSound *)id;
|
|
|
|
if (snd->packedfile)
|
|
|
|
unpackSound(bmain, reports, snd, how);
|
|
|
|
}
|
|
|
|
if (GS(id->name) == ID_LI) {
|
|
|
|
Library *li = (Library *)id;
|
|
|
|
BKE_reportf(reports, RPT_ERROR, "Cannot unpack individual Library file, '%s'", li->name);
|
|
|
|
}
|
|
|
|
}
|