PyObject* plPythonPack::OpenPacked(const char* fileName) { if (!Open()) return nil; std::string pythonName = fileName; pythonName += ".py"; FileOffset::iterator it = fFileOffsets.find(pythonName); if (it != fFileOffsets.end()) { plPackOffsetInfo offsetInfo = (*it).second; hsStream* fPackStream = fPackStreams[offsetInfo.fStreamIndex]; fPackStream->SetPosition(offsetInfo.fOffset); int32_t size = fPackStream->ReadLE32(); if (size > 0) { char *buf = new char[size]; uint32_t readSize = fPackStream->Read(size, buf); hsAssert(readSize <= size, xtl::format("Python PackFile %s: Incorrect amount of data, read %d instead of %d", fileName, readSize, size).c_str()); // let the python marshal make it back into a code object PyObject *pythonCode = PyMarshal_ReadObjectFromString(buf, size); delete [] buf; return pythonCode; } } return nil; }
bool plPythonPack::Open() { if (fPackStreams.size() > 0) return true; // We already tried and it wasn't there if (fPackNotFound) return false; fPackNotFound = true; // Get the names of all the pak files std::vector<plFileName> files = plStreamSource::GetInstance()->GetListOfNames("python", "pak"); std::vector<time_t> modTimes; // the modification time for each of the streams (to resolve duplicate file issues) // grab all the .pak files in the folder for (int curName = 0; curName < files.size(); curName++) { // obtain the stream hsStream *fPackStream = plStreamSource::GetInstance()->GetFile(files[curName]); if (fPackStream) { fPackStream->Rewind(); // make sure we're at the beginning of the file fPackNotFound = false; time_t curModTime = 0; plFileInfo info(files[curName]); if (info.Exists()) curModTime = info.ModifyTime(); modTimes.push_back(curModTime); // read the index data int numFiles = fPackStream->ReadLE32(); uint32_t streamIndex = (uint32_t)(fPackStreams.size()); for (int i = 0; i < numFiles; i++) { // and pack the index into our own data structure plString pythonName = fPackStream->ReadSafeString(); uint32_t offset = fPackStream->ReadLE32(); plPackOffsetInfo offsetInfo; offsetInfo.fOffset = offset; offsetInfo.fStreamIndex = streamIndex; if (fFileOffsets.find(pythonName) != fFileOffsets.end()) { uint32_t index = fFileOffsets[pythonName].fStreamIndex; if (modTimes[index] < curModTime) // is the existing file older then the new one? fFileOffsets[pythonName] = offsetInfo; // yup, so replace it with the new info } else fFileOffsets[pythonName] = offsetInfo; // no conflicts, add the info } fPackStreams.push_back(fPackStream); } } return !fPackNotFound; }
bool plPythonPack::IsPackedFile(const plString& fileName) { if (!Open()) return nil; plString pythonName = fileName + ".py"; FileOffset:: iterator it = fFileOffsets.find(pythonName); if (it != fFileOffsets.end()) return true; return false; }
bool plPythonPack::Open() { if (fPackStreams.size() > 0) return true; // We already tried and it wasn't there if (fPackNotFound) return false; fPackNotFound = true; // Get the names of all the pak files std::vector<std::wstring> files = plStreamSource::GetInstance()->GetListOfNames(L"python", L".pak"); std::vector<time_t> modTimes; // the modification time for each of the streams (to resolve duplicate file issues) // grab all the .pak files in the folder for (int curName = 0; curName < files.size(); curName++) { // obtain the stream hsStream *fPackStream = plStreamSource::GetInstance()->GetFile(files[curName]); if (fPackStream) { fPackStream->Rewind(); // make sure we're at the beginning of the file fPackNotFound = false; char* tempFilename = hsWStringToString(files[curName].c_str()); struct stat buf; time_t curModTime = 0; if (stat(tempFilename,&buf)==0) curModTime = buf.st_mtime; modTimes.push_back(curModTime); delete [] tempFilename; // read the index data int numFiles = fPackStream->ReadLE32(); uint32_t streamIndex = (uint32_t)(fPackStreams.size()); for (int i = 0; i < numFiles; i++) { // and pack the index into our own data structure char* buf = fPackStream->ReadSafeString(); std::string pythonName = buf; // reading a "string" from a hsStream directly into a stl string causes memory loss delete [] buf; uint32_t offset = fPackStream->ReadLE32(); plPackOffsetInfo offsetInfo; offsetInfo.fOffset = offset; offsetInfo.fStreamIndex = streamIndex; if (fFileOffsets.find(pythonName) != fFileOffsets.end()) { uint32_t index = fFileOffsets[pythonName].fStreamIndex; if (modTimes[index] < curModTime) // is the existing file older then the new one? fFileOffsets[pythonName] = offsetInfo; // yup, so replace it with the new info } else fFileOffsets[pythonName] = offsetInfo; // no conflicts, add the info } fPackStreams.push_back(fPackStream); } } return !fPackNotFound; }