Cleanup: nuke G.main out of BKE PackedFile code.

This commit is contained in:
2018-05-31 11:07:14 +02:00
parent 84a9647f22
commit 24d1829243
6 changed files with 45 additions and 37 deletions

View File

@@ -52,21 +52,25 @@ void packAll(struct Main *bmain, struct ReportList *reports, bool verbose);
void packLibraries(struct Main *bmain, struct ReportList *reports); void packLibraries(struct Main *bmain, struct ReportList *reports);
/* unpack */ /* unpack */
char *unpackFile(struct ReportList *reports, const char *abs_name, const char *local_name, struct PackedFile *pf, int how); char *unpackFile(
int unpackVFont(struct ReportList *reports, struct VFont *vfont, int how); struct ReportList *reports, const char *ref_file_name,
const char *abs_name, const char *local_name, struct PackedFile *pf, int how);
int unpackVFont(struct Main *bmain, struct ReportList *reports, struct VFont *vfont, int how);
int unpackSound(struct Main *bmain, struct ReportList *reports, struct bSound *sound, int how); int unpackSound(struct Main *bmain, struct ReportList *reports, struct bSound *sound, int how);
int unpackImage(struct ReportList *reports, struct Image *ima, int how); int unpackImage(struct Main *bmain, struct ReportList *reports, struct Image *ima, int how);
void unpackAll(struct Main *bmain, struct ReportList *reports, int how); void unpackAll(struct Main *bmain, struct ReportList *reports, int how);
int unpackLibraries(struct Main *bmain, struct ReportList *reports); int unpackLibraries(struct Main *bmain, struct ReportList *reports);
int writePackedFile(struct ReportList *reports, const char *filename, struct PackedFile *pf, int guimode); int writePackedFile(
struct ReportList *reports, const char *ref_file_name,
const char *filename, struct PackedFile *pf, const bool guimode);
/* free */ /* free */
void freePackedFile(struct PackedFile *pf); void freePackedFile(struct PackedFile *pf);
/* info */ /* info */
int countPackedFiles(struct Main *bmain); int countPackedFiles(struct Main *bmain);
int checkPackedFile(const char *filename, struct PackedFile *pf); int checkPackedFile(const char *ref_file_name, const char *filename, struct PackedFile *pf);
/* read */ /* read */
int seekPackedFile(struct PackedFile *pf, int offset, int whence); int seekPackedFile(struct PackedFile *pf, int offset, int whence);

View File

@@ -291,7 +291,8 @@ static char *find_new_name(char *name)
} }
#endif #endif
int writePackedFile(ReportList *reports, const char *filename, PackedFile *pf, int guimode) int writePackedFile(
ReportList *reports, const char *ref_file_name, const char *filename, PackedFile *pf, const bool guimode)
{ {
int file, number; int file, number;
int ret_value = RET_OK; int ret_value = RET_OK;
@@ -303,7 +304,7 @@ int writePackedFile(ReportList *reports, const char *filename, PackedFile *pf, i
if (guimode) {} //XXX waitcursor(1); if (guimode) {} //XXX waitcursor(1);
BLI_strncpy(name, filename, sizeof(name)); BLI_strncpy(name, filename, sizeof(name));
BLI_path_abs(name, G.main->name); BLI_path_abs(name, ref_file_name);
if (BLI_exists(name)) { if (BLI_exists(name)) {
for (number = 1; number <= 999; number++) { for (number = 1; number <= 999; number++) {
@@ -363,7 +364,7 @@ int writePackedFile(ReportList *reports, const char *filename, PackedFile *pf, i
* - PF_DIFFERENT: the packed file and original file differ * - PF_DIFFERENT: the packed file and original file differ
* - PF_NOFILE: the original file doens't exist * - PF_NOFILE: the original file doens't exist
*/ */
int checkPackedFile(const char *filename, PackedFile *pf) int checkPackedFile(const char *ref_file_name, const char *filename, PackedFile *pf)
{ {
BLI_stat_t st; BLI_stat_t st;
int ret_val, i, len, file; int ret_val, i, len, file;
@@ -371,7 +372,7 @@ int checkPackedFile(const char *filename, PackedFile *pf)
char name[FILE_MAX]; char name[FILE_MAX];
BLI_strncpy(name, filename, sizeof(name)); BLI_strncpy(name, filename, sizeof(name));
BLI_path_abs(name, G.main->name); BLI_path_abs(name, ref_file_name);
if (BLI_stat(name, &st) == -1) { if (BLI_stat(name, &st) == -1) {
ret_val = PF_NOFILE; ret_val = PF_NOFILE;
@@ -423,7 +424,9 @@ int checkPackedFile(const char *filename, PackedFile *pf)
* *
* \warning 'abs_name' may be relative still! (use a "//" prefix) be sure to run #BLI_path_abs on it first. * \warning 'abs_name' may be relative still! (use a "//" prefix) be sure to run #BLI_path_abs on it first.
*/ */
char *unpackFile(ReportList *reports, const char *abs_name, const char *local_name, PackedFile *pf, int how) char *unpackFile(
ReportList *reports, const char *ref_file_name,
const char *abs_name, const char *local_name, PackedFile *pf, int how)
{ {
char *newname = NULL; char *newname = NULL;
const char *temp = NULL; const char *temp = NULL;
@@ -441,7 +444,7 @@ char *unpackFile(ReportList *reports, const char *abs_name, const char *local_na
char temp_abs[FILE_MAX]; char temp_abs[FILE_MAX];
BLI_strncpy(temp_abs, local_name, sizeof(temp_abs)); BLI_strncpy(temp_abs, local_name, sizeof(temp_abs));
BLI_path_abs(temp_abs, G.main->name); BLI_path_abs(temp_abs, ref_file_name);
/* if file exists use it */ /* if file exists use it */
if (BLI_exists(temp_abs)) { if (BLI_exists(temp_abs)) {
@@ -452,7 +455,7 @@ char *unpackFile(ReportList *reports, const char *abs_name, const char *local_na
ATTR_FALLTHROUGH; ATTR_FALLTHROUGH;
} }
case PF_WRITE_LOCAL: case PF_WRITE_LOCAL:
if (writePackedFile(reports, local_name, pf, 1) == RET_OK) { if (writePackedFile(reports, ref_file_name, local_name, pf, 1) == RET_OK) {
temp = local_name; temp = local_name;
} }
break; break;
@@ -461,7 +464,7 @@ char *unpackFile(ReportList *reports, const char *abs_name, const char *local_na
char temp_abs[FILE_MAX]; char temp_abs[FILE_MAX];
BLI_strncpy(temp_abs, abs_name, sizeof(temp_abs)); BLI_strncpy(temp_abs, abs_name, sizeof(temp_abs));
BLI_path_abs(temp_abs, G.main->name); BLI_path_abs(temp_abs, ref_file_name);
/* if file exists use it */ /* if file exists use it */
if (BLI_exists(temp_abs)) { if (BLI_exists(temp_abs)) {
@@ -473,7 +476,7 @@ char *unpackFile(ReportList *reports, const char *abs_name, const char *local_na
ATTR_FALLTHROUGH; ATTR_FALLTHROUGH;
} }
case PF_WRITE_ORIGINAL: case PF_WRITE_ORIGINAL:
if (writePackedFile(reports, abs_name, pf, 1) == RET_OK) { if (writePackedFile(reports, ref_file_name, abs_name, pf, 1) == RET_OK) {
temp = abs_name; temp = abs_name;
} }
break; break;
@@ -531,7 +534,7 @@ static void unpack_generate_paths(
} }
} }
int unpackVFont(ReportList *reports, VFont *vfont, int how) int unpackVFont(Main *bmain, ReportList *reports, VFont *vfont, int how)
{ {
char localname[FILE_MAX], absname[FILE_MAX]; char localname[FILE_MAX], absname[FILE_MAX];
char *newname; char *newname;
@@ -539,7 +542,7 @@ int unpackVFont(ReportList *reports, VFont *vfont, int how)
if (vfont != NULL) { if (vfont != NULL) {
unpack_generate_paths(vfont->name, (ID *)vfont, absname, localname, sizeof(absname), sizeof(localname)); unpack_generate_paths(vfont->name, (ID *)vfont, absname, localname, sizeof(absname), sizeof(localname));
newname = unpackFile(reports, absname, localname, vfont->packedfile, how); newname = unpackFile(reports, bmain->name, absname, localname, vfont->packedfile, how);
if (newname != NULL) { if (newname != NULL) {
ret_value = RET_OK; ret_value = RET_OK;
freePackedFile(vfont->packedfile); freePackedFile(vfont->packedfile);
@@ -560,7 +563,7 @@ int unpackSound(Main *bmain, ReportList *reports, bSound *sound, int how)
if (sound != NULL) { if (sound != NULL) {
unpack_generate_paths(sound->name, (ID *)sound, absname, localname, sizeof(absname), sizeof(localname)); unpack_generate_paths(sound->name, (ID *)sound, absname, localname, sizeof(absname), sizeof(localname));
newname = unpackFile(reports, absname, localname, sound->packedfile, how); newname = unpackFile(reports, bmain->name, absname, localname, sound->packedfile, how);
if (newname != NULL) { if (newname != NULL) {
BLI_strncpy(sound->name, newname, sizeof(sound->name)); BLI_strncpy(sound->name, newname, sizeof(sound->name));
MEM_freeN(newname); MEM_freeN(newname);
@@ -577,7 +580,7 @@ int unpackSound(Main *bmain, ReportList *reports, bSound *sound, int how)
return(ret_value); return(ret_value);
} }
int unpackImage(ReportList *reports, Image *ima, int how) int unpackImage(Main *bmain, ReportList *reports, Image *ima, int how)
{ {
int ret_value = RET_ERROR; int ret_value = RET_ERROR;
@@ -588,7 +591,7 @@ int unpackImage(ReportList *reports, Image *ima, int how)
ImagePackedFile *imapf = ima->packedfiles.last; ImagePackedFile *imapf = ima->packedfiles.last;
unpack_generate_paths(imapf->filepath, (ID *)ima, absname, localname, sizeof(absname), sizeof(localname)); unpack_generate_paths(imapf->filepath, (ID *)ima, absname, localname, sizeof(absname), sizeof(localname));
newname = unpackFile(reports, absname, localname, imapf->packedfile, how); newname = unpackFile(reports, bmain->name, absname, localname, imapf->packedfile, how);
if (newname != NULL) { if (newname != NULL) {
ImageView *iv; ImageView *iv;
@@ -634,7 +637,7 @@ int unpackLibraries(Main *bmain, ReportList *reports)
for (lib = bmain->library.first; lib; lib = lib->id.next) { for (lib = bmain->library.first; lib; lib = lib->id.next) {
if (lib->packedfile && lib->name[0]) { if (lib->packedfile && lib->name[0]) {
newname = unpackFile(reports, lib->filepath, lib->filepath, lib->packedfile, PF_WRITE_ORIGINAL); newname = unpackFile(reports, bmain->name, lib->filepath, lib->filepath, lib->packedfile, PF_WRITE_ORIGINAL);
if (newname != NULL) { if (newname != NULL) {
ret_value = RET_OK; ret_value = RET_OK;
@@ -678,11 +681,11 @@ void unpackAll(Main *bmain, ReportList *reports, int how)
for (ima = bmain->image.first; ima; ima = ima->id.next) for (ima = bmain->image.first; ima; ima = ima->id.next)
if (BKE_image_has_packedfile(ima)) if (BKE_image_has_packedfile(ima))
unpackImage(reports, ima, how); unpackImage(bmain, reports, ima, how);
for (vf = bmain->vfont.first; vf; vf = vf->id.next) for (vf = bmain->vfont.first; vf; vf = vf->id.next)
if (vf->packedfile) if (vf->packedfile)
unpackVFont(reports, vf, how); unpackVFont(bmain, reports, vf, how);
for (sound = bmain->sound.first; sound; sound = sound->id.next) for (sound = bmain->sound.first; sound; sound = sound->id.next)
if (sound->packedfile) if (sound->packedfile)
@@ -727,7 +730,7 @@ void BKE_unpack_id(Main *bmain, ID *id, ReportList *reports, int how)
{ {
Image *ima = (Image *)id; Image *ima = (Image *)id;
if (BKE_image_has_packedfile(ima)) { if (BKE_image_has_packedfile(ima)) {
unpackImage(reports, ima, how); unpackImage(bmain, reports, ima, how);
} }
break; break;
} }
@@ -735,7 +738,7 @@ void BKE_unpack_id(Main *bmain, ID *id, ReportList *reports, int how)
{ {
VFont *vf = (VFont *)id; VFont *vf = (VFont *)id;
if (vf->packedfile) { if (vf->packedfile) {
unpackVFont(reports, vf, how); unpackVFont(bmain, reports, vf, how);
} }
break; break;
} }

View File

@@ -2823,7 +2823,7 @@ static int image_unpack_exec(bContext *C, wmOperator *op)
/* XXX unpackImage frees image buffers */ /* XXX unpackImage frees image buffers */
ED_preview_kill_jobs(CTX_wm_manager(C), CTX_data_main(C)); ED_preview_kill_jobs(CTX_wm_manager(C), CTX_data_main(C));
unpackImage(op->reports, ima, method); unpackImage(CTX_data_main(C), op->reports, ima, method);
WM_event_add_notifier(C, NC_IMAGE | NA_EDITED, ima); WM_event_add_notifier(C, NC_IMAGE | NA_EDITED, ima);

View File

@@ -232,6 +232,7 @@ void apply_keyb_grid(int shift, int ctrl, float *val, float fac1, float fac2, fl
void unpack_menu(bContext *C, const char *opname, const char *id_name, const char *abs_name, const char *folder, struct PackedFile *pf) void unpack_menu(bContext *C, const char *opname, const char *id_name, const char *abs_name, const char *folder, struct PackedFile *pf)
{ {
Main *bmain = CTX_data_main(C);
PointerRNA props_ptr; PointerRNA props_ptr;
uiPopupMenu *pup; uiPopupMenu *pup;
uiLayout *layout; uiLayout *layout;
@@ -253,7 +254,7 @@ void unpack_menu(bContext *C, const char *opname, const char *id_name, const cha
BLI_split_file_part(abs_name, fi, sizeof(fi)); BLI_split_file_part(abs_name, fi, sizeof(fi));
BLI_snprintf(local_name, sizeof(local_name), "//%s/%s", folder, fi); BLI_snprintf(local_name, sizeof(local_name), "//%s/%s", folder, fi);
if (!STREQ(abs_name, local_name)) { if (!STREQ(abs_name, local_name)) {
switch (checkPackedFile(local_name, pf)) { switch (checkPackedFile(bmain->name, local_name, pf)) {
case PF_NOFILE: case PF_NOFILE:
BLI_snprintf(line, sizeof(line), IFACE_("Create %s"), local_name); BLI_snprintf(line, sizeof(line), IFACE_("Create %s"), local_name);
uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, 0, &props_ptr); uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, 0, &props_ptr);
@@ -286,7 +287,7 @@ void unpack_menu(bContext *C, const char *opname, const char *id_name, const cha
} }
} }
switch (checkPackedFile(abs_name, pf)) { switch (checkPackedFile(bmain->name, abs_name, pf)) {
case PF_NOFILE: case PF_NOFILE:
BLI_snprintf(line, sizeof(line), IFACE_("Create %s"), abs_name); BLI_snprintf(line, sizeof(line), IFACE_("Create %s"), abs_name);
//uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_WRITE_ORIGINAL); //uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_WRITE_ORIGINAL);

View File

@@ -64,9 +64,9 @@
#include "MEM_guardedalloc.h" #include "MEM_guardedalloc.h"
static void rna_ImagePackedFile_save(ImagePackedFile *imapf, ReportList *reports) static void rna_ImagePackedFile_save(ImagePackedFile *imapf, Main *bmain, ReportList *reports)
{ {
if (writePackedFile(reports, imapf->filepath, imapf->packedfile, 0) != RET_OK) { if (writePackedFile(reports, bmain->name, imapf->filepath, imapf->packedfile, 0) != RET_OK) {
BKE_reportf(reports, RPT_ERROR, "Could not save packed file to disk as '%s'", BKE_reportf(reports, RPT_ERROR, "Could not save packed file to disk as '%s'",
imapf->filepath); imapf->filepath);
} }
@@ -177,7 +177,7 @@ static void rna_Image_pack(
WM_event_add_notifier(C, NC_IMAGE | NA_EDITED, image); WM_event_add_notifier(C, NC_IMAGE | NA_EDITED, image);
} }
static void rna_Image_unpack(Image *image, ReportList *reports, int method) static void rna_Image_unpack(Image *image, Main *bmain, ReportList *reports, int method)
{ {
if (!BKE_image_has_packedfile(image)) { if (!BKE_image_has_packedfile(image)) {
BKE_report(reports, RPT_ERROR, "Image not packed"); BKE_report(reports, RPT_ERROR, "Image not packed");
@@ -188,7 +188,7 @@ static void rna_Image_unpack(Image *image, ReportList *reports, int method)
} }
else { else {
/* reports its own error on failure */ /* reports its own error on failure */
unpackImage(reports, image, method); unpackImage(bmain, reports, image, method);
} }
} }
@@ -302,7 +302,7 @@ void RNA_api_image_packed_file(StructRNA *srna)
func = RNA_def_function(srna, "save", "rna_ImagePackedFile_save"); func = RNA_def_function(srna, "save", "rna_ImagePackedFile_save");
RNA_def_function_ui_description(func, "Save the packed file to its filepath"); RNA_def_function_ui_description(func, "Save the packed file to its filepath");
RNA_def_function_flag(func, FUNC_USE_REPORTS); RNA_def_function_flag(func, FUNC_USE_MAIN | FUNC_USE_REPORTS);
} }
void RNA_api_image(StructRNA *srna) void RNA_api_image(StructRNA *srna)
@@ -332,7 +332,7 @@ void RNA_api_image(StructRNA *srna)
func = RNA_def_function(srna, "unpack", "rna_Image_unpack"); func = RNA_def_function(srna, "unpack", "rna_Image_unpack");
RNA_def_function_ui_description(func, "Save an image packed in the .blend file to disk"); RNA_def_function_ui_description(func, "Save an image packed in the .blend file to disk");
RNA_def_function_flag(func, FUNC_USE_REPORTS); RNA_def_function_flag(func, FUNC_USE_MAIN | FUNC_USE_REPORTS);
RNA_def_enum(func, "method", rna_enum_unpack_method_items, PF_USE_LOCAL, "method", "How to unpack"); RNA_def_enum(func, "method", rna_enum_unpack_method_items, PF_USE_LOCAL, "method", "How to unpack");
func = RNA_def_function(srna, "reload", "rna_Image_reload"); func = RNA_def_function(srna, "reload", "rna_Image_reload");

View File

@@ -43,14 +43,14 @@ static void rna_VectorFont_pack(VFont *vfont, Main *bmain, ReportList *reports)
vfont->packedfile = newPackedFile(reports, vfont->name, ID_BLEND_PATH(bmain, &vfont->id)); vfont->packedfile = newPackedFile(reports, vfont->name, ID_BLEND_PATH(bmain, &vfont->id));
} }
static void rna_VectorFont_unpack(VFont *vfont, ReportList *reports, int method) static void rna_VectorFont_unpack(VFont *vfont, Main *bmain, ReportList *reports, int method)
{ {
if (!vfont->packedfile) { if (!vfont->packedfile) {
BKE_report(reports, RPT_ERROR, "Font not packed"); BKE_report(reports, RPT_ERROR, "Font not packed");
} }
else { else {
/* reports its own error on failure */ /* reports its own error on failure */
unpackVFont(reports, vfont, method); unpackVFont(bmain, reports, vfont, method);
} }
} }
@@ -62,11 +62,11 @@ void RNA_api_vfont(StructRNA *srna)
func = RNA_def_function(srna, "pack", "rna_VectorFont_pack"); func = RNA_def_function(srna, "pack", "rna_VectorFont_pack");
RNA_def_function_ui_description(func, "Pack the font into the current blend file"); RNA_def_function_ui_description(func, "Pack the font into the current blend file");
RNA_def_function_flag(func, FUNC_USE_REPORTS | FUNC_USE_MAIN); RNA_def_function_flag(func, FUNC_USE_MAIN | FUNC_USE_REPORTS);
func = RNA_def_function(srna, "unpack", "rna_VectorFont_unpack"); func = RNA_def_function(srna, "unpack", "rna_VectorFont_unpack");
RNA_def_function_ui_description(func, "Unpack the font to the samples filename"); RNA_def_function_ui_description(func, "Unpack the font to the samples filename");
RNA_def_function_flag(func, FUNC_USE_REPORTS); RNA_def_function_flag(func, FUNC_USE_MAIN | FUNC_USE_REPORTS);
RNA_def_enum(func, "method", rna_enum_unpack_method_items, PF_USE_LOCAL, "method", "How to unpack"); RNA_def_enum(func, "method", rna_enum_unpack_method_items, PF_USE_LOCAL, "method", "How to unpack");
} }