mirror of
				https://github.com/SoftFever/OrcaSlicer.git
				synced 2025-10-30 20:21:12 -06:00 
			
		
		
		
	Fix for opening issues with archive files.
This commit is contained in:
		
							parent
							
								
									0ee0b546df
								
							
						
					
					
						commit
						8376d14267
					
				
					 7 changed files with 113 additions and 100 deletions
				
			
		|  | @ -17,7 +17,7 @@ | |||
| 
 | ||||
| #include <expat.h> | ||||
| #include <Eigen/Dense> | ||||
| #include <miniz.h> | ||||
| #include "miniz_extension.hpp" | ||||
| 
 | ||||
| // VERSION NUMBERS
 | ||||
| // 0 : .3mf, files saved by older slic3r or other applications. No version definition in them.
 | ||||
|  | @ -188,24 +188,6 @@ bool is_valid_object_type(const std::string& type) | |||
| 
 | ||||
| namespace Slic3r { | ||||
| 
 | ||||
| namespace { | ||||
| void close_archive_reader(mz_zip_archive *arch) { | ||||
|     if (arch) { | ||||
|         FILE *f = mz_zip_get_cfile(arch); | ||||
|         mz_zip_reader_end(arch); | ||||
|         if (f) fclose(f); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| void close_archive_writer(mz_zip_archive *arch) { | ||||
|     if (arch) { | ||||
|         FILE *f = mz_zip_get_cfile(arch); | ||||
|         mz_zip_writer_end(arch); | ||||
|         if (f) fclose(f); | ||||
|     } | ||||
| } | ||||
| } | ||||
| 
 | ||||
|     // Base class with error messages management
 | ||||
|     class _3MF_Base | ||||
|     { | ||||
|  | @ -522,10 +504,7 @@ void close_archive_writer(mz_zip_archive *arch) { | |||
|         mz_zip_archive archive; | ||||
|         mz_zip_zero_struct(&archive); | ||||
| 
 | ||||
|         FILE *f = boost::nowide::fopen(filename.c_str(), "rb"); | ||||
|         auto res = mz_bool(f != nullptr && mz_zip_reader_init_cfile(&archive, f, -1, 0)); | ||||
|         if (res == 0) | ||||
|         { | ||||
|         if (!open_zip_reader(&archive, filename)) { | ||||
|             add_error("Unable to open the file"); | ||||
|             return false; | ||||
|         } | ||||
|  | @ -549,7 +528,7 @@ void close_archive_writer(mz_zip_archive *arch) { | |||
|                     // valid model name -> extract model
 | ||||
|                     if (!_extract_model_from_archive(archive, stat)) | ||||
|                     { | ||||
|                         close_archive_reader(&archive); | ||||
|                         close_zip_reader(&archive); | ||||
|                         add_error("Archive does not contain a valid model"); | ||||
|                         return false; | ||||
|                     } | ||||
|  | @ -585,7 +564,7 @@ void close_archive_writer(mz_zip_archive *arch) { | |||
|                     // extract slic3r model config file
 | ||||
|                     if (!_extract_model_config_from_archive(archive, stat, model)) | ||||
|                     { | ||||
|                         close_archive_reader(&archive); | ||||
|                         close_zip_reader(&archive); | ||||
|                         add_error("Archive does not contain a valid model config"); | ||||
|                         return false; | ||||
|                     } | ||||
|  | @ -593,7 +572,7 @@ void close_archive_writer(mz_zip_archive *arch) { | |||
|             } | ||||
|         } | ||||
| 
 | ||||
|         close_archive_reader(&archive); | ||||
|         close_zip_reader(&archive); | ||||
| 
 | ||||
|         for (const IdToModelObjectMap::value_type& object : m_objects) | ||||
|         { | ||||
|  | @ -1659,10 +1638,7 @@ void close_archive_writer(mz_zip_archive *arch) { | |||
|         mz_zip_archive archive; | ||||
|         mz_zip_zero_struct(&archive); | ||||
| 
 | ||||
|         FILE *f = boost::nowide::fopen(filename.c_str(), "wb"); | ||||
|         auto res = mz_bool(f != nullptr && mz_zip_writer_init_cfile(&archive, f, 0)); | ||||
|         if (res == 0) | ||||
|         { | ||||
|         if (!open_zip_writer(&archive, filename)) { | ||||
|             add_error("Unable to open the file"); | ||||
|             return false; | ||||
|         } | ||||
|  | @ -1671,7 +1647,7 @@ void close_archive_writer(mz_zip_archive *arch) { | |||
|         // The content of this file is the same for each PrusaSlicer 3mf.
 | ||||
|         if (!_add_content_types_file_to_archive(archive)) | ||||
|         { | ||||
|             close_archive_writer(&archive); | ||||
|             close_zip_writer(&archive); | ||||
|             boost::filesystem::remove(filename); | ||||
|             return false; | ||||
|         } | ||||
|  | @ -1681,7 +1657,7 @@ void close_archive_writer(mz_zip_archive *arch) { | |||
|         // The relationshis file contains a reference to the geometry file "3D/3dmodel.model", the name was chosen to be compatible with CURA.
 | ||||
|         if (!_add_relationships_file_to_archive(archive)) | ||||
|         { | ||||
|             close_archive_writer(&archive); | ||||
|             close_zip_writer(&archive); | ||||
|             boost::filesystem::remove(filename); | ||||
|             return false; | ||||
|         } | ||||
|  | @ -1691,7 +1667,7 @@ void close_archive_writer(mz_zip_archive *arch) { | |||
|         IdToObjectDataMap objects_data; | ||||
|         if (!_add_model_file_to_archive(archive, model, objects_data)) | ||||
|         { | ||||
|             close_archive_writer(&archive); | ||||
|             close_zip_writer(&archive); | ||||
|             boost::filesystem::remove(filename); | ||||
|             return false; | ||||
|         } | ||||
|  | @ -1701,7 +1677,7 @@ void close_archive_writer(mz_zip_archive *arch) { | |||
|         // The index differes from the index of an object ID of an object instance of a 3MF file!
 | ||||
|         if (!_add_layer_height_profile_file_to_archive(archive, model)) | ||||
|         { | ||||
|             close_archive_writer(&archive); | ||||
|             close_zip_writer(&archive); | ||||
|             boost::filesystem::remove(filename); | ||||
|             return false; | ||||
|         } | ||||
|  | @ -1711,7 +1687,7 @@ void close_archive_writer(mz_zip_archive *arch) { | |||
|         // The index differes from the index of an object ID of an object instance of a 3MF file!
 | ||||
|         if (!_add_sla_support_points_file_to_archive(archive, model)) | ||||
|         { | ||||
|             close_archive_writer(&archive); | ||||
|             close_zip_writer(&archive); | ||||
|             boost::filesystem::remove(filename); | ||||
|             return false; | ||||
|         } | ||||
|  | @ -1722,7 +1698,7 @@ void close_archive_writer(mz_zip_archive *arch) { | |||
|         { | ||||
|             if (!_add_print_config_file_to_archive(archive, *config)) | ||||
|             { | ||||
|                 close_archive_writer(&archive); | ||||
|                 close_zip_writer(&archive); | ||||
|                 boost::filesystem::remove(filename); | ||||
|                 return false; | ||||
|             } | ||||
|  | @ -1734,20 +1710,20 @@ void close_archive_writer(mz_zip_archive *arch) { | |||
|         // is stored here as well.
 | ||||
|         if (!_add_model_config_file_to_archive(archive, model, objects_data)) | ||||
|         { | ||||
|             close_archive_writer(&archive); | ||||
|             close_zip_writer(&archive); | ||||
|             boost::filesystem::remove(filename); | ||||
|             return false; | ||||
|         } | ||||
| 
 | ||||
|         if (!mz_zip_writer_finalize_archive(&archive)) | ||||
|         { | ||||
|             close_archive_writer(&archive); | ||||
|             close_zip_writer(&archive); | ||||
|             boost::filesystem::remove(filename); | ||||
|             add_error("Unable to finalize the archive"); | ||||
|             return false; | ||||
|         } | ||||
| 
 | ||||
|         close_archive_writer(&archive); | ||||
|         close_zip_writer(&archive); | ||||
| 
 | ||||
|         return true; | ||||
|     } | ||||
|  |  | |||
|  | @ -16,7 +16,7 @@ | |||
| #include <boost/filesystem/operations.hpp> | ||||
| #include <boost/algorithm/string.hpp> | ||||
| #include <boost/nowide/fstream.hpp> | ||||
| #include <miniz.h> | ||||
| #include "miniz_extension.hpp" | ||||
| 
 | ||||
| #if 0 | ||||
| // Enable debugging and assert in this file.
 | ||||
|  | @ -712,37 +712,19 @@ bool load_amf_file(const char *path, DynamicPrintConfig *config, Model *model) | |||
|     return result; | ||||
| } | ||||
| 
 | ||||
| namespace { | ||||
| void close_archive_reader(mz_zip_archive *arch) { | ||||
|     if (arch) { | ||||
|         FILE *f = mz_zip_get_cfile(arch); | ||||
|         mz_zip_reader_end(arch); | ||||
|         if (f) fclose(f); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| void close_archive_writer(mz_zip_archive *arch) { | ||||
|     if (arch) { | ||||
|         FILE *f = mz_zip_get_cfile(arch); | ||||
|         mz_zip_writer_end(arch); | ||||
|         if (f) fclose(f); | ||||
|     } | ||||
| } | ||||
| } | ||||
| 
 | ||||
| bool extract_model_from_archive(mz_zip_archive& archive, const mz_zip_archive_file_stat& stat, DynamicPrintConfig* config, Model* model, unsigned int& version) | ||||
| { | ||||
|     if (stat.m_uncomp_size == 0) | ||||
|     { | ||||
|         printf("Found invalid size\n"); | ||||
|         close_archive_reader(&archive); | ||||
|         close_zip_reader(&archive); | ||||
|         return false; | ||||
|     } | ||||
| 
 | ||||
|     XML_Parser parser = XML_ParserCreate(nullptr); // encoding
 | ||||
|     if (!parser) { | ||||
|         printf("Couldn't allocate memory for parser\n"); | ||||
|         close_archive_reader(&archive); | ||||
|         close_zip_reader(&archive); | ||||
|         return false; | ||||
|     } | ||||
| 
 | ||||
|  | @ -755,7 +737,7 @@ bool extract_model_from_archive(mz_zip_archive& archive, const mz_zip_archive_fi | |||
|     if (parser_buffer == nullptr) | ||||
|     { | ||||
|         printf("Unable to create buffer\n"); | ||||
|         close_archive_reader(&archive); | ||||
|         close_zip_reader(&archive); | ||||
|         return false; | ||||
|     } | ||||
| 
 | ||||
|  | @ -763,14 +745,14 @@ bool extract_model_from_archive(mz_zip_archive& archive, const mz_zip_archive_fi | |||
|     if (res == 0) | ||||
|     { | ||||
|         printf("Error while reading model data to buffer\n"); | ||||
|         close_archive_reader(&archive); | ||||
|         close_zip_reader(&archive); | ||||
|         return false; | ||||
|     } | ||||
| 
 | ||||
|     if (!XML_ParseBuffer(parser, (int)stat.m_uncomp_size, 1)) | ||||
|     { | ||||
|         printf("Error (%s) while parsing xml file at line %d\n", XML_ErrorString(XML_GetErrorCode(parser)), XML_GetCurrentLineNumber(parser)); | ||||
|         close_archive_reader(&archive); | ||||
|         close_zip_reader(&archive); | ||||
|         return false; | ||||
|     } | ||||
| 
 | ||||
|  | @ -792,10 +774,7 @@ bool load_amf_archive(const char *path, DynamicPrintConfig *config, Model *model | |||
|     mz_zip_archive archive; | ||||
|     mz_zip_zero_struct(&archive); | ||||
| 
 | ||||
|     FILE * f = boost::nowide::fopen(path, "rb"); | ||||
|     auto res = mz_bool(f == nullptr ? MZ_FALSE : MZ_TRUE); | ||||
|     res = res && mz_zip_reader_init_cfile(&archive, f, -1, 0); | ||||
|     if (res == MZ_FALSE) | ||||
|     if (!open_zip_reader(&archive, path)) | ||||
|     { | ||||
|         printf("Unable to init zip reader\n"); | ||||
|         return false; | ||||
|  | @ -813,7 +792,7 @@ bool load_amf_archive(const char *path, DynamicPrintConfig *config, Model *model | |||
|             { | ||||
|                 if (!extract_model_from_archive(archive, stat, config, model, version)) | ||||
|                 { | ||||
|                     close_archive_reader(&archive); | ||||
|                     close_zip_reader(&archive); | ||||
|                     printf("Archive does not contain a valid model"); | ||||
|                     return false; | ||||
|                 } | ||||
|  | @ -834,7 +813,7 @@ bool load_amf_archive(const char *path, DynamicPrintConfig *config, Model *model | |||
|     } | ||||
| #endif // forward compatibility
 | ||||
| 
 | ||||
|     close_archive_reader(&archive); | ||||
|     close_zip_reader(&archive); | ||||
|     return true; | ||||
| } | ||||
| 
 | ||||
|  | @ -874,10 +853,7 @@ bool store_amf(const char *path, Model *model, const DynamicPrintConfig *config) | |||
|     mz_zip_archive archive; | ||||
|     mz_zip_zero_struct(&archive); | ||||
| 
 | ||||
|     FILE *f = boost::nowide::fopen(export_path.c_str(), "wb"); | ||||
|     auto res = mz_bool(f != nullptr && mz_zip_writer_init_cfile(&archive, f, 0)); | ||||
|     if (res == 0) | ||||
|         return false; | ||||
|     if (!open_zip_writer(&archive, export_path)) return false; | ||||
| 
 | ||||
|     std::stringstream stream; | ||||
|     // https://en.cppreference.com/w/cpp/types/numeric_limits/max_digits10
 | ||||
|  | @ -1039,20 +1015,19 @@ bool store_amf(const char *path, Model *model, const DynamicPrintConfig *config) | |||
| 
 | ||||
|     if (!mz_zip_writer_add_mem(&archive, internal_amf_filename.c_str(), (const void*)out.data(), out.length(), MZ_DEFAULT_COMPRESSION)) | ||||
|     { | ||||
|         close_archive_writer(&archive); | ||||
|         if (f) fclose(f); | ||||
|         close_zip_writer(&archive); | ||||
|         boost::filesystem::remove(export_path); | ||||
|         return false; | ||||
|     } | ||||
| 
 | ||||
|     if (!mz_zip_writer_finalize_archive(&archive)) | ||||
|     { | ||||
|         close_archive_writer(&archive); | ||||
|         close_zip_writer(&archive); | ||||
|         boost::filesystem::remove(export_path); | ||||
|         return false; | ||||
|     } | ||||
| 
 | ||||
|     close_archive_writer(&archive); | ||||
|     close_zip_writer(&archive); | ||||
| 
 | ||||
|     return true; | ||||
| } | ||||
|  |  | |||
|  | @ -5,7 +5,7 @@ | |||
| #include <boost/nowide/convert.hpp> | ||||
| #include <boost/nowide/cstdio.hpp> | ||||
| 
 | ||||
| #include <miniz.h> | ||||
| #include "miniz_extension.hpp" | ||||
| 
 | ||||
| #include <Eigen/Geometry> | ||||
| 
 | ||||
|  | @ -300,11 +300,10 @@ bool load_prus(const char *path, Model *model) | |||
|     mz_zip_archive archive; | ||||
|     mz_zip_zero_struct(&archive); | ||||
| 
 | ||||
|     FILE *f = boost::nowide::fopen(path, "rb"); | ||||
|     auto res = mz_bool(f != nullptr && mz_zip_reader_init_cfile(&archive, f, -1, 0)); | ||||
|     size_t  n_models_initial = model->objects.size(); | ||||
|     mz_bool res              = MZ_FALSE; | ||||
|     try { | ||||
|         if (res == MZ_FALSE) | ||||
|         if (!open_zip_reader(&archive, path)) | ||||
|             throw std::runtime_error(std::string("Unable to init zip reader for ") + path); | ||||
|         std::vector<char>           scene_xml_data; | ||||
|         // For grouping multiple STLs into a single ModelObject for multi-material prints.
 | ||||
|  | @ -329,13 +328,11 @@ bool load_prus(const char *path, Model *model) | |||
|             } | ||||
|         } | ||||
|     } catch (std::exception &ex) { | ||||
|         mz_zip_reader_end(&archive); | ||||
|         if(f) fclose(f); | ||||
|         close_zip_reader(&archive); | ||||
|         throw ex; | ||||
|     } | ||||
| 
 | ||||
|     mz_zip_reader_end(&archive); | ||||
|     if(f) fclose(f); | ||||
|     close_zip_reader(&archive); | ||||
|     return model->objects.size() > n_models_initial; | ||||
| } | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 tamasmeszaros
						tamasmeszaros