From 6ecec8e3d6fe56403f0fd4097d5a5958b958f491 Mon Sep 17 00:00:00 2001 From: David Rose Date: Thu, 6 Jan 2005 22:38:21 +0000 Subject: [PATCH] correctly move and rename palettes when groups change directories --- pandatool/src/egg-palettize/eggPalettize.cxx | 10 ++ pandatool/src/palettizer/eggFile.cxx | 2 +- pandatool/src/palettizer/imageFile.cxx | 42 ++++- pandatool/src/palettizer/imageFile.h | 6 +- pandatool/src/palettizer/paletteImage.cxx | 175 ++++++++++++++----- pandatool/src/palettizer/paletteImage.h | 3 + pandatool/src/palettizer/palettizer.cxx | 6 +- 7 files changed, 184 insertions(+), 60 deletions(-) diff --git a/pandatool/src/egg-palettize/eggPalettize.cxx b/pandatool/src/egg-palettize/eggPalettize.cxx index 71e378e7d9..ac78ccdee3 100644 --- a/pandatool/src/egg-palettize/eggPalettize.cxx +++ b/pandatool/src/egg-palettize/eggPalettize.cxx @@ -781,7 +781,17 @@ run() { if (okflag) { pal->generate_images(_redo_all); + + if (_redo_eggs) { + // generate_images() might have made a few more stale egg files + // (particularly if a texture palette changed filenames). + if (!pal->read_stale_eggs(false)) { + okflag = false; + } + } + } + if (okflag) { if (!pal->write_eggs()) { okflag = false; } diff --git a/pandatool/src/palettizer/eggFile.cxx b/pandatool/src/palettizer/eggFile.cxx index f852818031..193950b601 100644 --- a/pandatool/src/palettizer/eggFile.cxx +++ b/pandatool/src/palettizer/eggFile.cxx @@ -496,7 +496,7 @@ has_data() const { // Function: EggFile::had_data // Access: Public // Description: Returns true if the EggData for this EggFile has ever -// been loaded. +// been loaded in this session. //////////////////////////////////////////////////////////////////// bool EggFile:: had_data() const { diff --git a/pandatool/src/palettizer/imageFile.cxx b/pandatool/src/palettizer/imageFile.cxx index 4d08bf41d7..7f66e1fd2a 100644 --- a/pandatool/src/palettizer/imageFile.cxx +++ b/pandatool/src/palettizer/imageFile.cxx @@ -52,13 +52,27 @@ ImageFile() { // ImageFile that's used to read and write the shadow // palette image, which is used to keep a working copy // of the palette. +// +// Returns true if the filename changes from what it was +// previously, false otherwise. //////////////////////////////////////////////////////////////////// -void ImageFile:: +bool ImageFile:: make_shadow_image(const string &basename) { - _properties._color_type = pal->_shadow_color_type; - _properties._alpha_type = pal->_shadow_alpha_type; + bool any_changed = false; + + if (_properties._color_type != pal->_shadow_color_type || + _properties._alpha_type != pal->_shadow_alpha_type) { - set_filename(pal->_shadow_dirname, basename); + _properties._color_type = pal->_shadow_color_type; + _properties._alpha_type = pal->_shadow_alpha_type; + any_changed = true; + } + + if (set_filename(pal->_shadow_dirname, basename)) { + any_changed = true; + } + + return any_changed; } //////////////////////////////////////////////////////////////////// @@ -163,8 +177,11 @@ update_properties(const TextureProperties &properties) { // extension appropriate to the image file type // specified in _color_type (and _alpha_type) is // automatically applied. +// +// Returns true if the filename changes from what it was +// previously, false otherwise. //////////////////////////////////////////////////////////////////// -void ImageFile:: +bool ImageFile:: set_filename(PaletteGroup *group, const string &basename) { // Synthesize the directory name based on the map_dirname set to the // palettizer, and the group's dirname. @@ -191,7 +208,7 @@ set_filename(PaletteGroup *group, const string &basename) { ++pi; } - set_filename(dirname, basename); + return set_filename(dirname, basename); } //////////////////////////////////////////////////////////////////// @@ -202,9 +219,15 @@ set_filename(PaletteGroup *group, const string &basename) { // extension appropriate to the image file type // specified in _color_type (and _alpha_type) is // automatically applied. +// +// Returns true if the filename changes from what it was +// previously, false otherwise. //////////////////////////////////////////////////////////////////// -void ImageFile:: +bool ImageFile:: set_filename(const string &dirname, const string &basename) { + Filename orig_filename = _filename; + Filename orig_alpha_filename = _alpha_filename; + _filename = Filename(dirname, basename); // Since we use set_extension() here, if the file already contains a @@ -226,7 +249,12 @@ set_filename(const string &dirname, const string &basename) { _alpha_filename = _filename.get_fullpath_wo_extension() + "_a."; _alpha_filename.set_extension (_properties._alpha_type->get_suggested_extension()); + } else { + _alpha_filename = Filename(); } + + return (_filename != orig_filename || + _alpha_filename != orig_alpha_filename); } //////////////////////////////////////////////////////////////////// diff --git a/pandatool/src/palettizer/imageFile.h b/pandatool/src/palettizer/imageFile.h index eb447bc853..82479f1d5c 100644 --- a/pandatool/src/palettizer/imageFile.h +++ b/pandatool/src/palettizer/imageFile.h @@ -41,7 +41,7 @@ class ImageFile : public TypedWritable { public: ImageFile(); - void make_shadow_image(const string &basename); + bool make_shadow_image(const string &basename); bool is_size_known() const; int get_x_size() const; @@ -53,8 +53,8 @@ public: void clear_basic_properties(); void update_properties(const TextureProperties &properties); - void set_filename(PaletteGroup *group, const string &basename); - void set_filename(const string &dirname, const string &basename); + bool set_filename(PaletteGroup *group, const string &basename); + bool set_filename(const string &dirname, const string &basename); const Filename &get_filename() const; const Filename &get_alpha_filename() const; int get_alpha_file_channel() const; diff --git a/pandatool/src/palettizer/paletteImage.cxx b/pandatool/src/palettizer/paletteImage.cxx index 20c12d051f..0fbeac99d0 100644 --- a/pandatool/src/palettizer/paletteImage.cxx +++ b/pandatool/src/palettizer/paletteImage.cxx @@ -177,54 +177,7 @@ PaletteImage(PalettePage *page, int index) : _new_image = true; _got_image = false; - // Build up the basename for the palette image, based on the - // supplied image pattern. - string::iterator si = pal->_generated_image_pattern.begin(); - while (si != pal->_generated_image_pattern.end()) { - if ((*si) == '%') { - // Some keycode. - ++si; - if (si != pal->_generated_image_pattern.end()) { - switch (*si) { - case '%': - _basename += '%'; - break; - - case 'g': - _basename += page->get_group()->get_name(); - break; - - case 'p': - _basename += page->get_name(); - break; - - case 'i': - _basename += format_string(index + 1); - break; - - default: - _basename += '%'; - _basename += (*si); - } - ++si; - } - } else { - // A literal character. - _basename += (*si); - ++si; - } - } - - // We must end the basename with a dot, so that it does not appear - // to have a filename extension. Otherwise, an embedded dot in the - // group's name would make everything following appear to be an - // extension, which would get lost in the set_filename() call. - if (_basename.empty() || _basename[_basename.length() - 1] != '.') { - _basename += '.'; - } - - set_filename(page->get_group(), _basename); - _shadow_image.make_shadow_image(_basename); + setup_filename(); } //////////////////////////////////////////////////////////////////// @@ -592,6 +545,9 @@ update_image(bool redo_all) { remove_image(); } + // Check the filename too. + update_filename(); + // Do we need to update? bool needs_update = _new_image || !exists() || @@ -658,6 +614,129 @@ update_image(bool redo_all) { release_image(); } +//////////////////////////////////////////////////////////////////// +// Function: PaletteImage::update_filename +// Access: Public +// Description: Changes the image filename to match the current +// naming scheme, assuming something has changed since +// the image was created. Returns true if the image +// filename changes (which means update_image() should +// be called). +//////////////////////////////////////////////////////////////////// +bool PaletteImage:: +update_filename() { + Filename orig_filename = _filename; + Filename orig_alpha_filename = _alpha_filename; + Filename orig_shadow_filename = _shadow_image.get_filename(); + + if (setup_filename()) { + nout << "Renaming " << FilenameUnifier::make_user_filename(orig_filename) + << " to " << FilenameUnifier::make_user_filename(_filename) << "\n"; + + if (!orig_filename.empty() && orig_filename.exists()) { + nout << "Deleting " << FilenameUnifier::make_user_filename(orig_filename) << "\n"; + orig_filename.unlink(); + } + if (!orig_alpha_filename.empty() && orig_alpha_filename.exists()) { + nout << "Deleting " << FilenameUnifier::make_user_filename(orig_alpha_filename) << "\n"; + orig_alpha_filename.unlink(); + } + if (!orig_shadow_filename.empty() && orig_shadow_filename.exists()) { + nout << "Deleting " << FilenameUnifier::make_user_filename(orig_shadow_filename) << "\n"; + orig_shadow_filename.unlink(); + } + _new_image = true; + + // Since the palette filename has changed, we need to mark all of + // the egg files that referenced the old filename as stale. + + // Marking egg files stale at this late point can cause minor + // problems; because we might do this, it's necessary for + // eggPalettize.cxx to call read_stale_eggs() twice. + Placements::iterator pi; + for (pi = _placements.begin(); pi != _placements.end(); ++pi) { + TexturePlacement *placement = (*pi); + placement->mark_eggs_stale(); + } + + return true; + } + + return false; +} + + +//////////////////////////////////////////////////////////////////// +// Function: PaletteImage::setup_filename +// Access: Private +// Description: Sets up the image's filename (and that of the +// _shadow_pal) according to the specified properties. +// +// Returns true if the filename changes from what it was +// previously, false otherwise. +//////////////////////////////////////////////////////////////////// +bool PaletteImage:: +setup_filename() { + // Build up the basename for the palette image, based on the + // supplied image pattern. + _basename = string(); + + string::iterator si = pal->_generated_image_pattern.begin(); + while (si != pal->_generated_image_pattern.end()) { + if ((*si) == '%') { + // Some keycode. + ++si; + if (si != pal->_generated_image_pattern.end()) { + switch (*si) { + case '%': + _basename += '%'; + break; + + case 'g': + _basename += _page->get_group()->get_name(); + break; + + case 'p': + _basename += _page->get_name(); + break; + + case 'i': + _basename += format_string(_index + 1); + break; + + default: + _basename += '%'; + _basename += (*si); + } + ++si; + } + } else { + // A literal character. + _basename += (*si); + ++si; + } + } + + // We must end the basename with a dot, so that it does not appear + // to have a filename extension. Otherwise, an embedded dot in the + // group's name would make everything following appear to be an + // extension, which would get lost in the set_filename() call. + if (_basename.empty() || _basename[_basename.length() - 1] != '.') { + _basename += '.'; + } + + bool any_changed = false; + + if (set_filename(_page->get_group(), _basename)) { + any_changed = true; + } + + if (_shadow_image.make_shadow_image(_basename)) { + any_changed = true; + } + + return any_changed; +} //////////////////////////////////////////////////////////////////// // Function: PaletteImage::find_hole diff --git a/pandatool/src/palettizer/paletteImage.h b/pandatool/src/palettizer/paletteImage.h index df0dee62f8..be0415b3c0 100644 --- a/pandatool/src/palettizer/paletteImage.h +++ b/pandatool/src/palettizer/paletteImage.h @@ -62,7 +62,10 @@ public: void setup_shadow_image(); void update_image(bool redo_all); + bool update_filename(); + private: + bool setup_filename(); bool find_hole(int &x, int &y, int x_size, int y_size) const; TexturePlacement *find_overlap(int x, int y, int x_size, int y_size) const; void get_image(); diff --git a/pandatool/src/palettizer/palettizer.cxx b/pandatool/src/palettizer/palettizer.cxx index 0276333862..fa5c9d8f81 100644 --- a/pandatool/src/palettizer/palettizer.cxx +++ b/pandatool/src/palettizer/palettizer.cxx @@ -361,6 +361,7 @@ read_txa_file(istream &txa_file, const string &txa_filename) { for (gi = _groups.begin(); gi != _groups.end(); ++gi) { PaletteGroup *group = (*gi).second; group->clear_depends(); + group->set_dirname(""); } // Also reset _shadow_color_type. @@ -581,6 +582,9 @@ process_all(bool force_texture_read, const Filename &state_filename) { texture->pre_txa_file(); _txa_file.match_texture(texture); texture->post_txa_file(); + + // We need to do this to avoid bloating memory. + texture->release_source_image(); } // And now, assign each texture to an appropriate group or groups. @@ -689,7 +693,7 @@ read_stale_eggs(bool redo_all) { EggFiles::iterator ei; for (ei = _egg_files.begin(); ei != _egg_files.end(); ++ei) { EggFile *egg_file = (*ei).second; - if (!egg_file->has_data() && + if (!egg_file->had_data() && (egg_file->is_stale() || redo_all)) { if (!egg_file->read_egg(_noabs)) { invalid_eggs.push_back(ei);