CString TitanicEngine::getSavegameName(int slot) { // Try and open up the savegame for access Common::InSaveFile *in = g_system->getSavefileManager()->openForLoading( generateSaveName(slot)); if (in) { // Read in the savegame header data CompressedFile file; file.open(in); TitanicSavegameHeader header; bool isValid = CProjectItem::readSavegameHeader(&file, header); if (header._thumbnail) { header._thumbnail->free(); delete header._thumbnail; } file.close(); if (isValid) // Set the name text return header._saveName; } return CString(); }
void CPetLoadSave::resetSlots() { for (int idx = 0; idx < SAVEGAME_SLOTS_COUNT; ++idx) { _slotNames[idx].setText(EMPTY); _slotInUse[idx] = false; // Try and open up the savegame for access Common::InSaveFile *in = g_system->getSavefileManager()->openForLoading( g_vm->generateSaveName(idx)); if (in) { // Read in the savegame header data CompressedFile file; file.open(in); TitanicSavegameHeader header; if (CProjectItem::readSavegameHeader(&file, header)) { _slotInUse[idx] = true; _slotNames[idx].setText(header._saveName); } if (header._thumbnail) { header._thumbnail->free(); delete header._thumbnail; } file.close(); } } highlightSlot(_savegameSlotNum); }
MeshVersion getMeshVersion(const std::string& filename) { CompressedFile file; file.open(filename, ios::in | ios::binary); if (file.is_open()) { MeshVersion version; file.read(reinterpret_cast<char*>(&version), sizeof(MeshVersion)); file.close(); return version; } return MeshVersion{}; }
// Removes specified files from zip void ZipFile::Remove(String* FileSpec, Boolean RecurseSubdirectories) { ZipFile* tempZipFile; OpenFileForUnzip(); // Just wrapping in a try/catch/finally to make sure temp zip file gets removed try { Regex* filePattern = GetFilePatternRegularExpression(FileSpec, caseSensitive); IEnumerator* filesEnum = files->GetEnumerator(); CompressedFile* file; // Create temporary zip file to hold remove results tempZipFile = CreateTempZipFile(); // Loop through compressed file collection while (filesEnum->MoveNext()) { file = static_cast<CompressedFile*>(filesEnum->Current); // We copy all files into destination zip file except those user requested to be removed... if (!filePattern->IsMatch(GetSearchFileName(FileSpec, file->get_FileName(), RecurseSubdirectories))) CopyFileInZip(file, this, tempZipFile, S"Remove Zip File"); } // Now make the temp file the new zip file Close(); tempZipFile->Close(); File::Delete(fileName); File::Move(tempZipFile->fileName, fileName); } catch (CompressionException* ex) { // We just rethrow any errors back to user throw ex; } catch (Exception* ex) { throw ex; } __finally { // If everything went well, temp zip will no longer exist, but just in case we tidy up and delete the temp file... DeleteTempZipFile(tempZipFile); } // We'll reload the file list after remove if requested... if (autoRefresh) Refresh(); }
void Mesh::saveToMemory(std::vector<char>& mem) { CompressedFile file; file.open(mem, ios::out | ios::binary); if (file.is_open()) { file.write(reinterpret_cast<const char*>(&SupportedMeshVersion), sizeof(MeshVersion)); for (const auto& mesh : m_subMeshes) { mesh.save(file); } file.close(); } }
void CProjectItem::loadGame(int slotId) { CompressedFile file; // Clear any existing project contents and call preload code preLoad(); clear(); // Open either an existing savegame slot or the new game template if (slotId >= 0) { Common::InSaveFile *saveFile = g_system->getSavefileManager()->openForLoading( g_vm->generateSaveName(slotId)); file.open(saveFile); } else { Common::File *newFile = new Common::File(); if (!newFile->open("newgame.st")) error("Could not open newgame.st"); file.open(newFile); } // Load the savegame header in TitanicSavegameHeader header; readSavegameHeader(&file, header); delete header._thumbnail; // Load the contents in CProjectItem *newProject = loadData(&file); file.IsClassStart(); getGameManager()->load(&file); file.close(); // Clear existing project clear(); // Detach each item under the loaded project, and re-attach them // to the existing project instance (this) CTreeItem *item; while ((item = newProject->getFirstChild()) != nullptr) { item->detach(); item->addUnder(this); } // Loaded project instance is no longer needed newProject->destroyAll(); // Post-load processing postLoad(); }
void CProjectItem::saveGame(int slotId, const CString &desc) { CompressedFile file; Common::OutSaveFile *saveFile = g_system->getSavefileManager()->openForSaving( g_vm->generateSaveName(slotId), false); file.open(saveFile); // Signal the game is being saved preSave(); // Write out the savegame header TitanicSavegameHeader header; header._saveName = desc; writeSavegameHeader(&file, header); // Save the contents out saveData(&file, this); // Save the game manager data _gameManager->save(&file); // Close the file and signal that the saving has finished file.close(); postSave(); }
void Mesh::load(ModelResources& modelResources) { CompressedFile file; file.open(m_filename, ios::in | ios::binary); if (file.is_open()) { MeshVersion version; file.read(reinterpret_cast<char*>(&version), sizeof(MeshVersion)); while (!file.eof()) { m_subMeshes.emplace_back(SubMesh()); if (!m_subMeshes.back().load(modelResources, file)) { m_subMeshes.pop_back(); } } file.close(); } }
// This internal function copies each file into a temporary zip that matches file specification, then does this for each subdirectory if requested void ZipFile::UpdateFilesInZip(ZipFile* TempZipFile, String* FileSpec, String* CurrDirectory, int RootPathLength, UpdateOption UpdateWhen, Boolean AddNewFiles, Boolean RecurseSubdirectories, PathInclusion AddPathMethod, char* Password) { IEnumerator* filesEnum = Directory::GetFiles(CurrDirectory, FileSpec)->GetEnumerator(); String* fullFileName; String* adjustedFileName; CompressedFile* file; DateTime lastUpdate; bool updateFile; while (filesEnum->MoveNext()) { fullFileName = filesEnum->Current->ToString(); adjustedFileName = GetAdjustedFileName(fullFileName, RootPathLength, AddPathMethod); if (file = files->get_Item(adjustedFileName)) { lastUpdate = File::GetLastWriteTime(fullFileName); switch (UpdateWhen) { case UpdateOption::Never: updateFile = false; break; case UpdateOption::Always: updateFile = true; break; case UpdateOption::ZipFileIsNewer: updateFile = (DateTime::Compare(file->get_FileDateTime(), lastUpdate) > 0); break; case UpdateOption::DiskFileIsNewer: updateFile = (DateTime::Compare(file->get_FileDateTime(), lastUpdate) < 0); break; default: updateFile = false; break; } if (updateFile) { // Need to update compressed file from disk, so we add the it the new temporary archive AddFileToZip(TempZipFile, this, fullFileName, adjustedFileName, Password, S"Update Zip File"); } else { // Otherwise we just copy the compressed file from the original archive CopyFileInZip(file, this, TempZipFile, S"Update Zip File"); } } else if (AddNewFiles) { // This was a new file so we just add it the new zip, if requested... AddFileToZip(TempZipFile, this, fullFileName, adjustedFileName, Password, S"Update Zip File"); } } if (RecurseSubdirectories) { IEnumerator* dirsEnum = Directory::GetDirectories(CurrDirectory, "*")->GetEnumerator(); while (dirsEnum->MoveNext()) UpdateFilesInZip(TempZipFile, FileSpec, dirsEnum->Current->ToString(), RootPathLength, UpdateWhen, AddNewFiles, RecurseSubdirectories, AddPathMethod, Password); } }
// Extracts specified files from zip void ZipFile::Extract(String* FileSpec, String* DestPath, UpdateOption OverwriteWhen, Boolean RecurseSubdirectories, PathInclusion CreatePathMethod) { // Any file spec in destination path will be ignored DestPath = JustPath(DestPath); char* pszPassword = NULL; char* pszFileName = NULL; OpenFileForUnzip(); // Just wrapping in a try/catch/finally to make sure unmanaged code allocations always get released try { Regex* filePattern = GetFilePatternRegularExpression(FileSpec, caseSensitive); IEnumerator* filesEnum = files->GetEnumerator(); CompressedFile* file; String* sourceFileName; String* destFileName; bool writeFile; int err; // Get ANSI password, if one was provided if (password) if (password->get_Length()) pszPassword = StringToCharBuffer(password); // Loop through compressed file collection while (filesEnum->MoveNext()) { file = static_cast<CompressedFile*>(filesEnum->Current); sourceFileName = file->get_FileName(); if (filePattern->IsMatch(GetSearchFileName(FileSpec, sourceFileName, RecurseSubdirectories))) { pszFileName = StringToCharBuffer(sourceFileName); err = unzLocateFile(hUnzipFile, pszFileName, (caseSensitive ? 1 : 2)); free(pszFileName); pszFileName = NULL; // We should find file in zip file if it was in our compressed file collection if (err != Z_OK) throw new CompressionException(String::Concat(S"Extract Zip File Error: Compressed file \"", sourceFileName, "\" cannot be found in zip file!")); // Open compressed file for unzipping if (pszPassword) err = unzOpenCurrentFilePassword(hUnzipFile, pszPassword); else err = unzOpenCurrentFile(hUnzipFile); if (err != Z_OK) throw new CompressionException(S"Extract Zip File", err); // Get full destination file name switch (CreatePathMethod) { case PathInclusion::FullPath: destFileName = sourceFileName; break; case PathInclusion::NoPath: destFileName = String::Concat(DestPath, Path::GetFileName(sourceFileName)); break; case PathInclusion::RelativePath: destFileName = String::Concat(DestPath, sourceFileName); break; } // Make sure destination directory exists Directory::CreateDirectory(JustPath(destFileName)); // See if destination file already exists if (File::Exists(destFileName)) { DateTime lastUpdate = File::GetLastWriteTime(destFileName); switch (OverwriteWhen) { case UpdateOption::Never: writeFile = false; break; case UpdateOption::Always: writeFile = true; break; case UpdateOption::ZipFileIsNewer: writeFile = (DateTime::Compare(file->get_FileDateTime(), lastUpdate) > 0); break; case UpdateOption::DiskFileIsNewer: writeFile = (DateTime::Compare(file->get_FileDateTime(), lastUpdate) < 0); break; default: writeFile = false; break; } } else writeFile = true; if (writeFile) { System::Byte buffer[] = new System::Byte[BufferSize]; System::Byte __pin * destBuff = &buffer[0]; // pin buffer so it can be safely passed into unmanaged code... FileStream* fileStream = File::Create(destFileName); int read; __int64 total = 0, len = -1; // Send initial progress event len = file->get_UncompressedSize(); CurrentFile(destFileName, sourceFileName); FileProgress(0, len); // Read initial buffer read = unzReadCurrentFile(hUnzipFile, destBuff, buffer->get_Length()); if (read < 0) throw new CompressionException(S"Extract Zip File", read); while (read) { // Write data to file stream, fileStream->Write(buffer, 0, read); // Raise progress event total += read; FileProgress(total, len); // Read next buffer from source file stream read = unzReadCurrentFile(hUnzipFile, destBuff, buffer->get_Length()); if (read < 0) throw new CompressionException(S"Extract Zip File", read); } fileStream->Close(); } // Close compressed file unzCloseCurrentFile(hUnzipFile); } } } catch (CompressionException* ex) { // We just rethrow any errors back to user throw ex; } catch (Exception* ex) { throw ex; } __finally { if (pszPassword) free(pszPassword); if (pszFileName) free(pszFileName); } }
CompressionTest::CompressionTest(const std::string& testFile, CompressionTypes type) { ifstream in; in.open(testFile, ios::binary); std::vector<char> inBuf; in.seekg(0, ios::end); size_t inSize = static_cast<size_t>(in.tellg()); inBuf.resize(inSize); in.seekg(0, ios::beg); in.read(&inBuf[0], static_cast<streamsize>(inSize)); in.close(); CompressedFile outCompress; outCompress.open(testFile + ".compressed", ios::out | ios::binary, type); outCompress.write(&inBuf[0], static_cast<streamsize>(inSize)); outCompress.close(); CompressedFile inCompress; inCompress.open(testFile + ".compressed", ios::in | ios::binary); std::vector<char> resInBuf; inCompress.seekg(0, ios::end); size_t inCompressSize = inCompress.tellg(); inCompress.seekg(0, ios::beg); resInBuf.resize(inCompressSize); inCompress.read(&resInBuf[0], static_cast<streamsize>(inCompressSize)); inCompress.close(); ofstream out; out.open(testFile + ".result", ios::binary); out.write(&resInBuf[0], static_cast<streamsize>(inCompressSize)); out.close(); // is the original file the same size as the resulting file ASSERT(inSize == inCompressSize); // is the content identical for (size_t i = 0; i < inSize; ++i) { ASSERT(inBuf[i] == resInBuf[i]); } }