You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
2.7 KiB

3 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <wx/tarstrm.h>
  20. #include <wx/wfstream.h>
  21. #include <wx/zstream.h>
  22. #include <asset_archive.h>
  23. ASSET_ARCHIVE::ASSET_ARCHIVE( const wxString& aFilePath, bool aLoadNow ) :
  24. m_filePath( aFilePath )
  25. {
  26. if( aLoadNow )
  27. Load();
  28. }
  29. bool ASSET_ARCHIVE::Load()
  30. {
  31. // We don't support hot-reloading yet
  32. if( !m_fileInfoCache.empty() )
  33. return false;
  34. wxFFileInputStream zipFile( m_filePath );
  35. if( !zipFile.IsOk() )
  36. return false;
  37. wxZlibInputStream stream( zipFile, wxZLIB_GZIP );
  38. wxTarInputStream tarStream( stream );
  39. wxTarEntry* entry;
  40. // Avoid realloc while reading: we're not going to get better than 2:1 compression
  41. m_cache.resize( 2 * zipFile.GetLength() );
  42. size_t offset = 0;
  43. while( ( entry = tarStream.GetNextEntry() ) != nullptr )
  44. {
  45. if( entry->IsDir() )
  46. {
  47. delete entry;
  48. continue;
  49. }
  50. size_t length = entry->GetSize();
  51. FILE_INFO fi;
  52. fi.offset = offset;
  53. fi.length = length;
  54. if( offset + length > m_cache.size() )
  55. m_cache.resize( m_cache.size() * 2 );
  56. tarStream.Read( &m_cache[offset], length );
  57. m_fileInfoCache[entry->GetName()] = fi;
  58. offset += length;
  59. delete entry;
  60. }
  61. m_cache.resize( offset );
  62. return true;
  63. }
  64. long ASSET_ARCHIVE::GetFileContents( const wxString& aFilePath, const unsigned char* aDest,
  65. size_t aMaxLen )
  66. {
  67. wxFAIL_MSG( wxS( "Unimplemented" ) );
  68. return 0;
  69. }
  70. long ASSET_ARCHIVE::GetFilePointer( const wxString& aFilePath, const unsigned char** aDest )
  71. {
  72. if( aFilePath.IsEmpty() )
  73. return -1;
  74. wxASSERT( aDest );
  75. if( !m_fileInfoCache.count( aFilePath ) )
  76. return -1;
  77. const FILE_INFO& fi = m_fileInfoCache.at( aFilePath );
  78. *aDest = &m_cache[fi.offset];
  79. return fi.length;
  80. }