void SFFile::LoadSample(SFSample *sample) { FileReader *fp = open_filereader(Filename, openmode, NULL); DWORD i; if (fp == NULL) { return; } sample->InMemoryData = new float[sample->End - sample->Start + 1]; fp->Seek(SampleDataOffset + sample->Start * 2, SEEK_SET); // Load 16-bit sample data. for (i = 0; i < sample->End - sample->Start; ++i) { SWORD samp; *fp >> samp; sample->InMemoryData[i] = samp / 32768.f; } if (SampleDataLSBOffset != 0) { // Load lower 8 bits of 24-bit sample data. fp->Seek(SampleDataLSBOffset + sample->Start, SEEK_SET); for (i = 0; i < sample->End - sample->Start; ++i) { BYTE samp; *fp >> samp; sample->InMemoryData[i] = ((((SDWORD(sample->InMemoryData[i] * 32768) << 8) | samp) << 8) >> 8) / 8388608.f; } }
sf_count_t SndFileDecoder::file_seek(sf_count_t offset, int whence, void *user_data) { FileReader *reader = reinterpret_cast<SndFileDecoder*>(user_data)->Reader; if(reader->Seek((long)offset, whence) != 0) return -1; return reader->Tell(); }
//========================================================================== // // RenameNerve // // Renames map headers and map name pictures in nerve.wad so as to load it // alongside Doom II and offer both episodes without causing conflicts. // MD5 checksum for NERVE.WAD: 967d5ae23daf45196212ae1b605da3b0 // //========================================================================== void FWadCollection::RenameNerve () { if (gameinfo.gametype != GAME_Doom) return; bool found = false; BYTE cksum[16]; static const BYTE nerve[16] = { 0x96, 0x7d, 0x5a, 0xe2, 0x3d, 0xaf, 0x45, 0x19, 0x62, 0x12, 0xae, 0x1b, 0x60, 0x5d, 0xa3, 0xb0 }; size_t nervesize = 3819855; // NERVE.WAD's file size int w = IWAD_FILENUM; while (++w < GetNumWads()) { FileReader *fr = GetFileReader(w); if (fr == NULL) { continue; } if (fr->GetLength() != (long)nervesize) { // Skip MD5 computation when there is a // cheaper way to know this is not the file continue; } fr->Seek(0, SEEK_SET); MD5Context md5; md5.Update(fr, fr->GetLength()); md5.Final(cksum); if (memcmp(nerve, cksum, 16) == 0) { found = true; break; } } if (!found) return; for (int i = GetFirstLump(w); i <= GetLastLump(w); i++) { // Only rename the maps from NERVE.WAD assert(LumpInfo[i].wadnum == w); if (LumpInfo[i].lump->dwName == MAKE_ID('C', 'W', 'I', 'L')) { LumpInfo[i].lump->Name[0] = 'N'; } else if (LumpInfo[i].lump->dwName == MAKE_ID('M', 'A', 'P', '0')) { LumpInfo[i].lump->Name[6] = LumpInfo[i].lump->Name[4]; LumpInfo[i].lump->Name[5] = '0'; LumpInfo[i].lump->Name[4] = 'L'; LumpInfo[i].lump->dwName = MAKE_ID('L', 'E', 'V', 'E'); } } }
// Read complete name table. std::vector<std::string> ReadUMXNameTable(FileReader &file, const UMXFileHeader &fileHeader) { std::vector<std::string> names; if(!file.Seek(fileHeader.nameOffset)) { return names; } names.reserve(fileHeader.nameCount); for(uint32 i = 0; i < fileHeader.nameCount && file.CanRead(4); i++) { names.push_back(ReadUMXNameTableEntry(file, fileHeader.packageVersion)); } return names; }
void FZipLump::SetLumpAddress() { // This file is inside a zip and has not been opened before. // Position points to the start of the local file header, which we must // read and skip so that we can get to the actual file data. FZipLocalFileHeader localHeader; int skiplen; FileReader *file = Owner->Reader; file->Seek(Position, SEEK_SET); file->Read(&localHeader, sizeof(localHeader)); skiplen = LittleShort(localHeader.NameLength) + LittleShort(localHeader.ExtraLength); Position += sizeof(localHeader) + skiplen; Flags &= ~LUMPFZIP_NEEDFILESTART; }
off_t MPG123Decoder::file_lseek(void *handle, off_t offset, int whence) { FileReader *reader = reinterpret_cast<MPG123Decoder*>(handle)->Reader; if(whence == SEEK_CUR) { if(offset < 0 && reader->Tell()+offset < 0) return -1; } else if(whence == SEEK_END) { if(offset < 0 && reader->GetLength()+offset < 0) return -1; } if(reader->Seek(offset, whence) != 0) return -1; return reader->Tell(); }
bool FPatchTexture::Check(FileReader & file) { if (file.GetLength() < 13) return false; // minimum length of a valid Doom patch BYTE *data = new BYTE[file.GetLength()]; file.Seek(0, SEEK_SET); file.Read(data, file.GetLength()); const patch_t * foo = (const patch_t *)data; int height = LittleShort(foo->height); int width = LittleShort(foo->width); bool gapAtStart=true; if (height > 0 && height < 2048 && width > 0 && width <= 2048 && width < file.GetLength()/4) { // The dimensions seem like they might be valid for a patch, so // check the column directory for extra security. At least one // column must begin exactly at the end of the column directory, // and none of them must point past the end of the patch. bool gapAtStart = true; int x; for (x = 0; x < width; ++x) { DWORD ofs = LittleLong(foo->columnofs[x]); if (ofs == (DWORD)width * 4 + 8) { gapAtStart = false; } else if (ofs >= (DWORD)(file.GetLength())) // Need one byte for an empty column (but there's patches that don't know that!) { delete [] data; return false; } } delete [] data; return !gapAtStart; } delete [] data; return false; }
off_t MPG123Decoder::file_lseek(void *handle, off_t offset, int whence) { MPG123Decoder *self = reinterpret_cast<MPG123Decoder*>(handle); FileReader *reader = self->Reader; if(whence == SEEK_SET) offset += self->StartOffset; else if(whence == SEEK_CUR) { if(offset < 0 && reader->Tell()+offset < self->StartOffset) return -1; } else if(whence == SEEK_END) { if(offset < 0 && reader->GetLength()+offset < self->StartOffset) return -1; } if(reader->Seek(offset, whence) != 0) return -1; return reader->Tell() - self->StartOffset; }
virtual long Seek(long offset, int origin) { switch (origin) { case SEEK_SET: offset += StartPos; break; case SEEK_END: offset += StartPos + Length; break; case SEEK_CUR: offset += (long)mReader->Tell(); break; } if (offset < StartPos || offset > StartPos + Length) return -1; // out of scope if (mReader->Seek(offset, FileReader::SeekSet) == 0) { FilePos = offset; return 0; } return -1; }
int FPNGTexture::CopyTrueColorPixels(FBitmap *bmp, int x, int y, int rotate, FCopyInfo *inf) { // Parse pre-IDAT chunks. I skip the CRCs. Is that bad? PalEntry pe[256]; DWORD len, id; FileReader *lump; static char bpp[] = {1, 0, 3, 1, 2, 0, 4}; int pixwidth = Width * bpp[ColorType]; int transpal = false; if (SourceLump >= 0) { lump = new FWadLump(Wads.OpenLumpNum(SourceLump)); } else { lump = new FileReader(SourceFile.GetChars()); } lump->Seek(33, SEEK_SET); for(int i = 0; i < 256; i++) // default to a gray map pe[i] = PalEntry(255,i,i,i); lump->Read(&len, 4); lump->Read(&id, 4); while (id != MAKE_ID('I','D','A','T') && id != MAKE_ID('I','E','N','D')) { len = BigLong((unsigned int)len); switch (id) { default: lump->Seek (len, SEEK_CUR); break; case MAKE_ID('P','L','T','E'): for(int i = 0; i < PaletteSize; i++) { (*lump) >> pe[i].r >> pe[i].g >> pe[i].b; } break; case MAKE_ID('t','R','N','S'): for(DWORD i = 0; i < len; i++) { (*lump) >> pe[i].a; if (pe[i].a != 0 && pe[i].a != 255) transpal = true; } break; } lump->Seek(4, SEEK_CUR); // Skip CRC lump->Read(&len, 4); id = MAKE_ID('I','E','N','D'); lump->Read(&id, 4); } BYTE * Pixels = new BYTE[pixwidth * Height]; lump->Seek (StartOfIDAT, SEEK_SET); lump->Read(&len, 4); lump->Read(&id, 4); M_ReadIDAT (lump, Pixels, Width, Height, pixwidth, BitDepth, ColorType, Interlace, BigLong((unsigned int)len)); delete lump; switch (ColorType) { case 0: case 3: bmp->CopyPixelData(x, y, Pixels, Width, Height, 1, Width, rotate, pe, inf); break; case 2: bmp->CopyPixelDataRGB(x, y, Pixels, Width, Height, 3, pixwidth, rotate, CF_RGB, inf); break; case 4: bmp->CopyPixelDataRGB(x, y, Pixels, Width, Height, 2, pixwidth, rotate, CF_IA, inf); transpal = -1; break; case 6: bmp->CopyPixelDataRGB(x, y, Pixels, Width, Height, 4, pixwidth, rotate, CF_RGBA, inf); transpal = -1; break; default: break; } delete[] Pixels; return transpal; }
FPNGTexture::FPNGTexture (FileReader &lump, int lumpnum, const FString &filename, int width, int height, uint8_t depth, uint8_t colortype, uint8_t interlace) : FTexture(NULL, lumpnum), SourceFile(filename), Pixels(0), Spans(0), BitDepth(depth), ColorType(colortype), Interlace(interlace), HaveTrans(false), PaletteMap(0), PaletteSize(0), StartOfIDAT(0) { union { uint32_t palette[256]; uint8_t pngpal[256][3]; } p; uint8_t trans[256]; uint32_t len, id; int i; if (lumpnum == -1) fr = &lump; else fr = nullptr; UseType = TEX_MiscPatch; LeftOffset = 0; TopOffset = 0; bMasked = false; Width = width; Height = height; CalcBitSize (); memset(trans, 255, 256); // Parse pre-IDAT chunks. I skip the CRCs. Is that bad? lump.Seek(33, SEEK_SET); lump.Read(&len, 4); lump.Read(&id, 4); while (id != MAKE_ID('I','D','A','T') && id != MAKE_ID('I','E','N','D')) { len = BigLong((unsigned int)len); switch (id) { default: lump.Seek (len, SEEK_CUR); break; case MAKE_ID('g','r','A','b'): // This is like GRAB found in an ILBM, except coordinates use 4 bytes { uint32_t hotx, hoty; int ihotx, ihoty; lump.Read(&hotx, 4); lump.Read(&hoty, 4); ihotx = BigLong((int)hotx); ihoty = BigLong((int)hoty); if (ihotx < -32768 || ihotx > 32767) { Printf ("X-Offset for PNG texture %s is bad: %d (0x%08x)\n", Wads.GetLumpFullName (lumpnum), ihotx, ihotx); ihotx = 0; } if (ihoty < -32768 || ihoty > 32767) { Printf ("Y-Offset for PNG texture %s is bad: %d (0x%08x)\n", Wads.GetLumpFullName (lumpnum), ihoty, ihoty); ihoty = 0; } LeftOffset = ihotx; TopOffset = ihoty; } break; case MAKE_ID('P','L','T','E'): PaletteSize = MIN<int> (len / 3, 256); lump.Read (p.pngpal, PaletteSize * 3); if (PaletteSize * 3 != (int)len) { lump.Seek (len - PaletteSize * 3, SEEK_CUR); } for (i = PaletteSize - 1; i >= 0; --i) { p.palette[i] = MAKERGB(p.pngpal[i][0], p.pngpal[i][1], p.pngpal[i][2]); } break; case MAKE_ID('t','R','N','S'): lump.Read (trans, len); HaveTrans = true; // Save for colortype 2 NonPaletteTrans[0] = uint16_t(trans[0] * 256 + trans[1]); NonPaletteTrans[1] = uint16_t(trans[2] * 256 + trans[3]); NonPaletteTrans[2] = uint16_t(trans[4] * 256 + trans[5]); break; case MAKE_ID('a','l','P','h'): bAlphaTexture = true; bMasked = true; break; } lump.Seek(4, SEEK_CUR); // Skip CRC lump.Read(&len, 4); id = MAKE_ID('I','E','N','D'); lump.Read(&id, 4); } StartOfIDAT = lump.Tell() - 8; switch (colortype) { case 4: // Grayscale + Alpha bMasked = true; // intentional fall-through case 0: // Grayscale if (!bAlphaTexture) { if (colortype == 0 && HaveTrans && NonPaletteTrans[0] < 256) { bMasked = true; PaletteSize = 256; PaletteMap = new uint8_t[256]; memcpy (PaletteMap, GrayMap, 256); PaletteMap[NonPaletteTrans[0]] = 0; } else { PaletteMap = GrayMap; } } break; case 3: // Paletted PaletteMap = new uint8_t[PaletteSize]; GPalette.MakeRemap (p.palette, PaletteMap, trans, PaletteSize); for (i = 0; i < PaletteSize; ++i) { if (trans[i] == 0) { bMasked = true; PaletteMap[i] = 0; } } break; case 6: // RGB + Alpha bMasked = true; break; case 2: // RGB bMasked = HaveTrans; break; } }
void FPNGTexture::MakeTexture () { FileReader *lump; if (SourceLump >= 0) { lump = new FWadLump(Wads.OpenLumpNum(SourceLump)); } else { lump = fr;// new FileReader(SourceFile.GetChars()); } Pixels = new uint8_t[Width*Height]; if (StartOfIDAT == 0) { memset (Pixels, 0x99, Width*Height); } else { uint32_t len, id; lump->Seek (StartOfIDAT, SEEK_SET); lump->Read(&len, 4); lump->Read(&id, 4); if (ColorType == 0 || ColorType == 3) /* Grayscale and paletted */ { M_ReadIDAT (lump, Pixels, Width, Height, Width, BitDepth, ColorType, Interlace, BigLong((unsigned int)len)); if (Width == Height) { if (PaletteMap != NULL) { FlipSquareBlockRemap (Pixels, Width, Height, PaletteMap); } else { FlipSquareBlock (Pixels, Width, Height); } } else { uint8_t *newpix = new uint8_t[Width*Height]; if (PaletteMap != NULL) { FlipNonSquareBlockRemap (newpix, Pixels, Width, Height, Width, PaletteMap); } else { FlipNonSquareBlock (newpix, Pixels, Width, Height, Width); } uint8_t *oldpix = Pixels; Pixels = newpix; delete[] oldpix; } } else /* RGB and/or Alpha present */ { int bytesPerPixel = ColorType == 2 ? 3 : ColorType == 4 ? 2 : 4; uint8_t *tempix = new uint8_t[Width * Height * bytesPerPixel]; uint8_t *in, *out; int x, y, pitch, backstep; M_ReadIDAT (lump, tempix, Width, Height, Width*bytesPerPixel, BitDepth, ColorType, Interlace, BigLong((unsigned int)len)); in = tempix; out = Pixels; // Convert from source format to paletted, column-major. // Formats with alpha maps are reduced to only 1 bit of alpha. switch (ColorType) { case 2: // RGB pitch = Width * 3; backstep = Height * pitch - 3; for (x = Width; x > 0; --x) { for (y = Height; y > 0; --y) { if (!HaveTrans) { *out++ = RGB256k.RGB[in[0]>>2][in[1]>>2][in[2]>>2]; } else { if (in[0] == NonPaletteTrans[0] && in[1] == NonPaletteTrans[1] && in[2] == NonPaletteTrans[2]) { *out++ = 0; } else { *out++ = RGB256k.RGB[in[0]>>2][in[1]>>2][in[2]>>2]; } } in += pitch; } in -= backstep; } break; case 4: // Grayscale + Alpha pitch = Width * 2; backstep = Height * pitch - 2; if (PaletteMap != NULL) { for (x = Width; x > 0; --x) { for (y = Height; y > 0; --y) { *out++ = in[1] < 128 ? 0 : PaletteMap[in[0]]; in += pitch; } in -= backstep; } } else { for (x = Width; x > 0; --x) { for (y = Height; y > 0; --y) { *out++ = in[1] < 128 ? 0 : in[0]; in += pitch; } in -= backstep; } } break; case 6: // RGB + Alpha pitch = Width * 4; backstep = Height * pitch - 4; for (x = Width; x > 0; --x) { for (y = Height; y > 0; --y) { *out++ = in[3] < 128 ? 0 : RGB256k.RGB[in[0]>>2][in[1]>>2][in[2]>>2]; in += pitch; } in -= backstep; } break; } delete[] tempix; }
bool CSoundFile::ReadITQ(FileReader &file, ModLoadingFlags loadFlags) //------------------------------------------------------------------ { file.Rewind(); ITFileHeader fileHeader; if(!file.ReadConvertEndianness(fileHeader) || (memcmp(fileHeader.id, "ITQM", 4)) || fileHeader.insnum > 0xFF || fileHeader.smpnum >= MAX_SAMPLES || !file.CanRead(fileHeader.ordnum + (fileHeader.insnum + fileHeader.smpnum + fileHeader.patnum) * 4)) { return false; } else if(loadFlags == onlyVerifyHeader) { return true; } InitializeGlobals(); bool interpretModPlugMade = false; // OpenMPT crap at the end of file file.Seek(file.GetLength() - 4); size_t mptStartPos = file.ReadUint32LE(); if(mptStartPos >= file.GetLength() || mptStartPos < 0x100) { mptStartPos = file.GetLength(); } if(!memcmp(fileHeader.id, "tpm.", 4)) { // Legacy MPTM files (old 1.17.02.xx releases) ChangeModTypeTo(MOD_TYPE_MPT); } else { if(mptStartPos <= file.GetLength() - 3 && fileHeader.cwtv > 0x888 && fileHeader.cwtv <= 0xFFF) { file.Seek(mptStartPos); ChangeModTypeTo(file.ReadMagic("228") ? MOD_TYPE_MPT : MOD_TYPE_IT); } else { ChangeModTypeTo(MOD_TYPE_IT); } if(GetType() == MOD_TYPE_IT) { // Which tracker was used to made this? if((fileHeader.cwtv & 0xF000) == 0x5000) { // OpenMPT Version number (Major.Minor) // This will only be interpreted as "made with ModPlug" (i.e. disable compatible playback etc) if the "reserved" field is set to "OMPT" - else, compatibility was used. m_dwLastSavedWithVersion = (fileHeader.cwtv & 0x0FFF) << 16; if(!memcmp(fileHeader.reserved, "OMPT", 4)) interpretModPlugMade = true; } else if(fileHeader.cmwt == 0x888 || fileHeader.cwtv == 0x888) { // OpenMPT 1.17 and 1.18 (raped IT format) // Exact version number will be determined later. interpretModPlugMade = true; } else if(fileHeader.cwtv == 0x0217 && fileHeader.cmwt == 0x0200 && !memcmp(fileHeader.reserved, "\0\0\0\0", 4)) { if(memchr(fileHeader.chnpan, 0xFF, sizeof(fileHeader.chnpan)) != NULL) { // ModPlug Tracker 1.16 (semi-raped IT format) m_dwLastSavedWithVersion = MAKE_VERSION_NUMERIC(1, 16, 00, 00); madeWithTracker = "ModPlug tracker 1.09 - 1.16"; } else { // OpenMPT 1.17 disguised as this in compatible mode, // but never writes 0xFF in the pan map for unused channels (which is an invalid value). m_dwLastSavedWithVersion = MAKE_VERSION_NUMERIC(1, 17, 00, 00); madeWithTracker = "OpenMPT 1.17 (compatibility export)"; } interpretModPlugMade = true; } else if(fileHeader.cwtv == 0x0214 && fileHeader.cmwt == 0x0202 && !memcmp(fileHeader.reserved, "\0\0\0\0", 4)) { // ModPlug Tracker b3.3 - 1.09, instruments 557 bytes apart m_dwLastSavedWithVersion = MAKE_VERSION_NUMERIC(1, 09, 00, 00); madeWithTracker = "ModPlug tracker b3.3 - 1.09"; interpretModPlugMade = true; } } else // case: type == MOD_TYPE_MPT { if (fileHeader.cwtv >= verMptFileVerLoadLimit) { AddToLog(str_LoadingIncompatibleVersion); return false; } else if (fileHeader.cwtv > verMptFileVer) { AddToLog(str_LoadingMoreRecentVersion); } } } if(GetType() == MOD_TYPE_IT) mptStartPos = file.GetLength(); // Read row highlights if((fileHeader.special & ITFileHeader::embedPatternHighlights)) { // MPT 1.09, 1.07 and most likely other old MPT versions leave this blank (0/0), but have the "special" flag set. // Newer versions of MPT and OpenMPT 1.17 *always* write 4/16 here. // Thus, we will just ignore those old versions. if(m_dwLastSavedWithVersion == 0 || m_dwLastSavedWithVersion >= MAKE_VERSION_NUMERIC(1, 17, 03, 02)) { m_nDefaultRowsPerBeat = fileHeader.highlight_minor; m_nDefaultRowsPerMeasure = fileHeader.highlight_major; } #ifdef _DEBUG if((fileHeader.highlight_minor | fileHeader.highlight_major) == 0) { Log("IT Header: Row highlight is 0"); } #endif } m_SongFlags.set(SONG_LINEARSLIDES, (fileHeader.flags & ITFileHeader::linearSlides) != 0); m_SongFlags.set(SONG_ITOLDEFFECTS, (fileHeader.flags & ITFileHeader::itOldEffects) != 0); m_SongFlags.set(SONG_ITCOMPATGXX, (fileHeader.flags & ITFileHeader::itCompatGxx) != 0); m_SongFlags.set(SONG_EMBEDMIDICFG, (fileHeader.flags & ITFileHeader::reqEmbeddedMIDIConfig) || (fileHeader.special & ITFileHeader::embedMIDIConfiguration)); m_SongFlags.set(SONG_EXFILTERRANGE, (fileHeader.flags & ITFileHeader::extendedFilterRange) != 0); mpt::String::Read<mpt::String::spacePadded>(songName, fileHeader.songname); // Global Volume m_nDefaultGlobalVolume = fileHeader.globalvol << 1; if(m_nDefaultGlobalVolume > MAX_GLOBAL_VOLUME) m_nDefaultGlobalVolume = MAX_GLOBAL_VOLUME; if(fileHeader.speed) m_nDefaultSpeed = fileHeader.speed; m_nDefaultTempo = std::max(uint8(32), fileHeader.tempo); // Tempo 31 is possible. due to conflicts with the rest of the engine, let's just clamp it to 32. m_nSamplePreAmp = std::min(fileHeader.mv, uint8(128)); // Reading Channels Pan Positions for(CHANNELINDEX i = 0; i < 64; i++) if(fileHeader.chnpan[i] != 0xFF) { ChnSettings[i].Reset(); ChnSettings[i].nVolume = Clamp(fileHeader.chnvol[i], uint8(0), uint8(64)); if(fileHeader.chnpan[i] & 0x80) ChnSettings[i].dwFlags.set(CHN_MUTE); uint8 n = fileHeader.chnpan[i] & 0x7F; if(n <= 64) ChnSettings[i].nPan = n * 4; if(n == 100) ChnSettings[i].dwFlags.set(CHN_SURROUND); } // Reading orders file.Seek(sizeof(ITFileHeader)); if(GetType() == MOD_TYPE_IT) { Order.ReadAsByte(file, fileHeader.ordnum); } else { if(fileHeader.cwtv > 0x88A && fileHeader.cwtv <= 0x88D) { Order.Deserialize(file); } else { Order.ReadAsByte(file, fileHeader.ordnum); // Replacing 0xFF and 0xFE with new corresponding indexes Order.Replace(0xFE, Order.GetIgnoreIndex()); Order.Replace(0xFF, Order.GetInvalidPatIndex()); } } // Reading instrument, sample and pattern offsets std::vector<uint32> insPos, smpPos, patPos; file.ReadVectorLE(insPos, fileHeader.insnum); file.ReadVectorLE(smpPos, fileHeader.smpnum); file.ReadVectorLE(patPos, fileHeader.patnum); // Find the first parapointer. // This is used for finding out whether the edit history is actually stored in the file or not, // as some early versions of Schism Tracker set the history flag, but didn't save anything. // We will consider the history invalid if it ends after the first parapointer. uint32 minPtr = Util::MaxValueOfType(minPtr); for(uint16 n = 0; n < fileHeader.insnum; n++) { if(insPos[n] > 0) { minPtr = std::min(minPtr, insPos[n]); } } for(uint16 n = 0; n < fileHeader.smpnum; n++) { if(smpPos[n] > 0) { minPtr = std::min(minPtr, smpPos[n]); } } for(uint16 n = 0; n < fileHeader.patnum; n++) { if(patPos[n] > 0) { minPtr = std::min(minPtr, patPos[n]); } } if(fileHeader.special & ITFileHeader::embedSongMessage) { minPtr = std::min(minPtr, fileHeader.msgoffset); } // Reading IT Edit History Info // This is only supposed to be present if bit 1 of the special flags is set. // However, old versions of Schism and probably other trackers always set this bit // even if they don't write the edit history count. So we have to filter this out... // This is done by looking at the parapointers. If the history data end after // the first parapointer, we assume that it's actually no history data. if(fileHeader.special & ITFileHeader::embedEditHistory) { const uint16 nflt = file.ReadUint16LE(); if(file.CanRead(nflt * sizeof(ITHistoryStruct)) && file.GetPosition() + nflt * sizeof(ITHistoryStruct) <= minPtr) { m_FileHistory.reserve(nflt); for(size_t n = 0; n < nflt; n++) { FileHistory mptHistory; ITHistoryStruct itHistory; file.ReadConvertEndianness(itHistory); itHistory.ConvertToMPT(mptHistory); m_FileHistory.push_back(mptHistory); } } else { // Oops, we were not supposed to read this. file.SkipBack(2); } } else if(fileHeader.highlight_major == 0 && fileHeader.highlight_minor == 0 && fileHeader.cmwt == 0x0214 && fileHeader.cwtv == 0x0214 && !memcmp(fileHeader.reserved, "\0\0\0\0", 4) && (fileHeader.special & (ITFileHeader::embedEditHistory | ITFileHeader::embedPatternHighlights)) == 0) { // Another non-conforming application is unmo3 < v2.4.0.1, which doesn't set the special bit // at all, but still writes the two edit history length bytes (zeroes)... if(file.ReadUint16LE() != 0) { // These were not zero bytes -> We're in the wrong place! file.SkipBack(2); madeWithTracker = "UNMO3"; } } // Reading MIDI Output & Macros if(m_SongFlags[SONG_EMBEDMIDICFG] && file.Read(m_MidiCfg)) { m_MidiCfg.Sanitize(); } // Ignore MIDI data. Fixes some files like denonde.it that were made with old versions of Impulse Tracker (which didn't support Zxx filters) and have Zxx effects in the patterns. if(fileHeader.cwtv < 0x0214) { MemsetZero(m_MidiCfg.szMidiSFXExt); MemsetZero(m_MidiCfg.szMidiZXXExt); m_SongFlags.set(SONG_EMBEDMIDICFG); } if(file.ReadMagic("MODU")) { madeWithTracker = "BeRoTracker"; } // Read pattern names: "PNAM" FileReader patNames; if(file.ReadMagic("PNAM")) { patNames = file.GetChunk(file.ReadUint32LE()); } m_nChannels = GetModSpecifications().channelsMin; // Read channel names: "CNAM" if(file.ReadMagic("CNAM")) { FileReader chnNames = file.GetChunk(file.ReadUint32LE()); const CHANNELINDEX readChns = std::min(MAX_BASECHANNELS, static_cast<CHANNELINDEX>(chnNames.GetLength() / MAX_CHANNELNAME)); m_nChannels = readChns; for(CHANNELINDEX i = 0; i < readChns; i++) { chnNames.ReadString<mpt::String::maybeNullTerminated>(ChnSettings[i].szName, MAX_CHANNELNAME); } } // Read mix plugins information if(file.CanRead(9)) { LoadMixPlugins(file); } // Read Song Message if(fileHeader.special & ITFileHeader::embedSongMessage) { if(fileHeader.msglength > 0 && file.Seek(fileHeader.msgoffset)) { // Generally, IT files should use CR for line endings. However, ChibiTracker uses LF. One could do... // if(itHeader.cwtv == 0x0214 && itHeader.cmwt == 0x0214 && itHeader.reserved == ITFileHeader::chibiMagic) --> Chibi detected. // But we'll just use autodetection here: songMessage.Read(file, fileHeader.msglength, SongMessage::leAutodetect); } } // Reading Instruments m_nInstruments = 0; if(fileHeader.flags & ITFileHeader::instrumentMode) { m_nInstruments = std::min(fileHeader.insnum, INSTRUMENTINDEX(MAX_INSTRUMENTS - 1)); } for(INSTRUMENTINDEX i = 0; i < GetNumInstruments(); i++) { if(insPos[i] > 0 && file.Seek(insPos[i]) && file.CanRead(fileHeader.cmwt < 0x200 ? sizeof(ITOldInstrument) : sizeof(ITInstrument))) { ModInstrument *instrument = AllocateInstrument(i + 1); if(instrument != nullptr) { ITInstrToMPT(file, *instrument, fileHeader.cmwt); // MIDI Pitch Wheel Depth is a global setting in IT. Apply it to all instruments. instrument->midiPWD = fileHeader.pwd; } } } // In order to properly compute the position, in file, of eventual extended settings // such as "attack" we need to keep the "real" size of the last sample as those extra // setting will follow this sample in the file FileReader::off_t lastSampleOffset = 0; if(fileHeader.smpnum > 0) { lastSampleOffset = smpPos[fileHeader.smpnum - 1] + sizeof(ITSample); } //// #ITQ // Reading Samples m_nSamples = std::min(fileHeader.smpnum, SAMPLEINDEX(MAX_SAMPLES - 1)); size_t nbytes = 0; // size of sample data in file for(SAMPLEINDEX i = 0; i < GetNumSamples(); i++) { ITQSample sampleHeader; if(smpPos[i] > 0 && file.Seek(smpPos[i]) && file.ReadConvertEndianness(sampleHeader)) { if(!memcmp(sampleHeader.id, "ITQS", 4)) { size_t sampleOffset = sampleHeader.ConvertToMPT(Samples[i + 1]); mpt::String::Read<mpt::String::spacePadded>(m_szNames[i + 1], sampleHeader.name); if((loadFlags & loadSampleData) && file.Seek(sampleOffset)) { Samples[i+1].originalSize = sampleHeader.nbytes; sampleHeader.GetSampleFormatITQ(fileHeader.cwtv).ReadSample(Samples[i + 1], file); lastSampleOffset = std::max(lastSampleOffset, file.GetPosition()); } } } } m_nSamples = std::max(SAMPLEINDEX(1), GetNumSamples()); m_nMinPeriod = 8; m_nMaxPeriod = 0xF000; PATTERNINDEX numPats = std::min(static_cast<PATTERNINDEX>(patPos.size()), GetModSpecifications().patternsMax); if(numPats != patPos.size()) { // Hack: Notify user here if file contains more patterns than what can be read. AddToLog(mpt::String::Print(str_PatternSetTruncationNote, patPos.size(), numPats)); } if(!(loadFlags & loadPatternData)) { numPats = 0; } // Checking for number of used channels, which is not explicitely specified in the file. for(PATTERNINDEX pat = 0; pat < numPats; pat++) { if(patPos[pat] == 0 || !file.Seek(patPos[pat])) continue; uint16 len = file.ReadUint16LE(); ROWINDEX numRows = file.ReadUint16LE(); if(numRows < GetModSpecifications().patternRowsMin || numRows > GetModSpecifications().patternRowsMax || !file.Skip(4)) continue; FileReader patternData = file.GetChunk(len); ROWINDEX row = 0; std::vector<uint8> chnMask(GetNumChannels()); while(row < numRows && patternData.AreBytesLeft()) { uint8 b = patternData.ReadUint8(); if(!b) { row++; continue; } CHANNELINDEX ch = (b & IT_bitmask_patternChanField_c); // 0x7f We have some data grab a byte keeping only 7 bits if(ch) { ch = (ch - 1);// & IT_bitmask_patternChanMask_c; // 0x3f mask of the byte again, keeping only 6 bits } if(ch >= chnMask.size()) { chnMask.resize(ch + 1, 0); } if(b & IT_bitmask_patternChanEnabled_c) // 0x80 check if the upper bit is enabled. { chnMask[ch] = patternData.ReadUint8(); // set the channel mask for this channel. } // Channel used if(chnMask[ch] & 0x0F) // if this channel is used set m_nChannels { if(ch >= GetNumChannels() && ch < MAX_BASECHANNELS) { m_nChannels = ch + 1; } } // Now we actually update the pattern-row entry the note,instrument etc. // Note if(chnMask[ch] & 1) patternData.Skip(1); // Instrument if(chnMask[ch] & 2) patternData.Skip(1); // Volume if(chnMask[ch] & 4) patternData.Skip(1); // Effect if(chnMask[ch] & 8) patternData.Skip(2); } } // Compute extra instruments settings position if(lastSampleOffset > 0) { file.Seek(lastSampleOffset); } // Load instrument and song extensions. LoadExtendedInstrumentProperties(file, &interpretModPlugMade); if(interpretModPlugMade) { m_nMixLevels = mixLevels_original; } // We need to do this here, because if there no samples (so lastSampleOffset = 0), we need to look after the last pattern (sample data normally follows pattern data). // And we need to do this before reading the patterns because m_nChannels might be modified by LoadExtendedSongProperties. *sigh* LoadExtendedSongProperties(GetType(), file, &interpretModPlugMade); m_nTempoMode = tempo_mode_modern; // Reading Patterns Patterns.ResizeArray(std::max(MAX_PATTERNS, numPats)); for(PATTERNINDEX pat = 0; pat < numPats; pat++) { if(patPos[pat] == 0 || !file.Seek(patPos[pat])) { // Empty 64-row pattern if(Patterns.Insert(pat, 64)) { AddToLog(mpt::String::Print("Allocating patterns failed starting from pattern %1", pat)); break; } // Now (after the Insert() call), we can read the pattern name. CopyPatternName(Patterns[pat], patNames); continue; } uint16 len = file.ReadUint16LE(); ROWINDEX numRows = file.ReadUint16LE(); if(numRows < GetModSpecifications().patternRowsMin || numRows > GetModSpecifications().patternRowsMax || !file.Skip(4) || Patterns.Insert(pat, numRows)) continue; FileReader patternData = file.GetChunk(len); // Now (after the Insert() call), we can read the pattern name. CopyPatternName(Patterns[pat], patNames); std::vector<uint8> chnMask(GetNumChannels()); std::vector<ModCommand> lastValue(GetNumChannels(), ModCommand::Empty()); ModCommand *m = Patterns[pat]; ROWINDEX row = 0; while(row < numRows && patternData.AreBytesLeft()) { uint8 b = patternData.ReadUint8(); if(!b) { row++; m += GetNumChannels(); continue; } CHANNELINDEX ch = b & IT_bitmask_patternChanField_c; // 0x7f if(ch) { ch = (ch - 1); //& IT_bitmask_patternChanMask_c; // 0x3f } if(ch >= chnMask.size()) { chnMask.resize(ch + 1, 0); lastValue.resize(ch + 1, ModCommand::Empty()); ASSERT(chnMask.size() <= GetNumChannels()); } if(b & IT_bitmask_patternChanEnabled_c) // 0x80 { chnMask[ch] = patternData.ReadUint8(); } // Now we grab the data for this particular row/channel. if((chnMask[ch] & 0x10) && (ch < m_nChannels)) { m[ch].note = lastValue[ch].note; } if((chnMask[ch] & 0x20) && (ch < m_nChannels)) { m[ch].instr = lastValue[ch].instr; } if((chnMask[ch] & 0x40) && (ch < m_nChannels)) { m[ch].volcmd = lastValue[ch].volcmd; m[ch].vol = lastValue[ch].vol; } if((chnMask[ch] & 0x80) && (ch < m_nChannels)) { m[ch].command = lastValue[ch].command; m[ch].param = lastValue[ch].param; } if(chnMask[ch] & 1) // Note { uint8 note = patternData.ReadUint8(); if(ch < m_nChannels) { if(note < 0x80) note++; if(!(GetType() & MOD_TYPE_MPT)) { if(note > NOTE_MAX && note < 0xFD) note = NOTE_FADE; else if(note == 0xFD) note = NOTE_NONE; } m[ch].note = note; lastValue[ch].note = note; } } if(chnMask[ch] & 2) { uint8 instr = patternData.ReadUint8(); if(ch < m_nChannels) { m[ch].instr = instr; lastValue[ch].instr = instr; } } if(chnMask[ch] & 4) { uint8 vol = patternData.ReadUint8(); if(ch < m_nChannels) { // 0-64: Set Volume if(vol <= 64) { m[ch].volcmd = VOLCMD_VOLUME; m[ch].vol = vol; } else // 128-192: Set Panning if(vol >= 128 && vol <= 192) { m[ch].volcmd = VOLCMD_PANNING; m[ch].vol = vol - 128; } else // 65-74: Fine Volume Up if(vol < 75) { m[ch].volcmd = VOLCMD_FINEVOLUP; m[ch].vol = vol - 65; } else // 75-84: Fine Volume Down if(vol < 85) { m[ch].volcmd = VOLCMD_FINEVOLDOWN; m[ch].vol = vol - 75; } else // 85-94: Volume Slide Up if(vol < 95) { m[ch].volcmd = VOLCMD_VOLSLIDEUP; m[ch].vol = vol - 85; } else // 95-104: Volume Slide Down if(vol < 105) { m[ch].volcmd = VOLCMD_VOLSLIDEDOWN; m[ch].vol = vol - 95; } else // 105-114: Pitch Slide Up if(vol < 115) { m[ch].volcmd = VOLCMD_PORTADOWN; m[ch].vol = vol - 105; } else // 115-124: Pitch Slide Down if(vol < 125) { m[ch].volcmd = VOLCMD_PORTAUP; m[ch].vol = vol - 115; } else // 193-202: Portamento To if(vol >= 193 && vol <= 202) { m[ch].volcmd = VOLCMD_TONEPORTAMENTO; m[ch].vol = vol - 193; } else // 203-212: Vibrato depth if(vol >= 203 && vol <= 212) { m[ch].volcmd = VOLCMD_VIBRATODEPTH; m[ch].vol = vol - 203; // Old versions of ModPlug saved this as vibrato speed instead, so let's fix that. if(m[ch].vol && m_dwLastSavedWithVersion && m_dwLastSavedWithVersion <= MAKE_VERSION_NUMERIC(1, 17, 02, 54)) m[ch].volcmd = VOLCMD_VIBRATOSPEED; } else // 213-222: Unused (was velocity) // 223-232: Offset if(vol >= 223 && vol <= 232) { m[ch].volcmd = VOLCMD_OFFSET; m[ch].vol = vol - 223; } lastValue[ch].volcmd = m[ch].volcmd; lastValue[ch].vol = m[ch].vol; } } // Reading command/param if(chnMask[ch] & 8) { uint8 cmd = patternData.ReadUint8(); uint8 param = patternData.ReadUint8(); if(ch < m_nChannels) { if(cmd) { m[ch].command = cmd; m[ch].param = param; S3MConvert(m[ch], true); lastValue[ch].command = m[ch].command; lastValue[ch].param = m[ch].param; } } } } } UpgradeModFlags(); if(!m_dwLastSavedWithVersion && fileHeader.cwtv == 0x0888) { // There are some files with OpenMPT extensions, but the "last saved with" field contains 0. // Was there an OpenMPT version that wrote 0 there, or are they hacked? m_dwLastSavedWithVersion = MAKE_VERSION_NUMERIC(1, 17, 00, 00); } if(m_dwLastSavedWithVersion && madeWithTracker.empty()) { madeWithTracker = "OpenMPT " + MptVersion::ToStr(m_dwLastSavedWithVersion); if(memcmp(fileHeader.reserved, "OMPT", 4) && (fileHeader.cwtv & 0xF000) == 0x5000) { madeWithTracker += " (compatibility export)"; } else if(MptVersion::IsTestBuild(m_dwLastSavedWithVersion)) { madeWithTracker += " (test build)"; } } else { switch(fileHeader.cwtv >> 12) { case 0: if(!madeWithTracker.empty()) { // BeRoTracker has been detected above. } else if(fileHeader.cwtv == 0x0214 && fileHeader.cmwt == 0x0200 && fileHeader.flags == 9 && fileHeader.special == 0 && fileHeader.highlight_major == 0 && fileHeader.highlight_minor == 0 && fileHeader.insnum == 0 && fileHeader.patnum + 1 == fileHeader.ordnum && fileHeader.globalvol == 128 && fileHeader.mv == 100 && fileHeader.speed == 1 && fileHeader.sep == 128 && fileHeader.pwd == 0 && fileHeader.msglength == 0 && fileHeader.msgoffset == 0 && !memcmp(fileHeader.reserved, "\0\0\0\0", 4)) { madeWithTracker = "OpenSPC conversion"; } else if(fileHeader.cwtv == 0x0214 && fileHeader.cmwt == 0x0200 && !memcmp(fileHeader.reserved, "\0\0\0\0", 4)) { // ModPlug Tracker 1.00a5, instruments 560 bytes apart m_dwLastSavedWithVersion = MAKE_VERSION_NUMERIC(1, 00, 00, A5); madeWithTracker = "ModPlug tracker 1.00a5"; interpretModPlugMade = true; } else if(fileHeader.cwtv == 0x0214 && fileHeader.cmwt == 0x0214 && !memcmp(fileHeader.reserved, "CHBI", 4)) { madeWithTracker = "ChibiTracker"; } else if(fileHeader.cwtv == 0x0214 && fileHeader.cmwt == 0x0214 && !(fileHeader.special & 3) && !memcmp(fileHeader.reserved, "\0\0\0\0", 4) && !strcmp(Samples[1].filename, "XXXXXXXX.YYY")) { madeWithTracker = "CheeseTracker"; } else { if(fileHeader.cmwt > 0x0214) { madeWithTracker = "Impulse Tracker 2.15"; } else if(fileHeader.cwtv > 0x0214) { // Patched update of IT 2.14 (0x0215 - 0x0217 == p1 - p3) // p4 (as found on modland) adds the ITVSOUND driver, but doesn't seem to change // anything as far as file saving is concerned. madeWithTracker = mpt::String::Print("Impulse Tracker 2.14p%1", fileHeader.cwtv - 0x0214); } else { madeWithTracker = mpt::String::Print("Impulse Tracker %1.%2", (fileHeader.cwtv & 0x0F00) >> 8, mpt::fmt::hex0<2>((fileHeader.cwtv & 0xFF))); } } break; case 1: madeWithTracker = GetSchismTrackerVersion(fileHeader.cwtv); break; case 6: madeWithTracker = "BeRoTracker"; break; case 7: madeWithTracker = mpt::String::Print("ITMCK %1.%2.%3", (fileHeader.cwtv >> 8) & 0x0F, (fileHeader.cwtv >> 4) & 0x0F, fileHeader.cwtv & 0x0F); break; } } if(GetType() == MOD_TYPE_IT) { // Set appropriate mod flags if the file was not made with MPT. if(!interpretModPlugMade) { SetModFlag(MSF_MIDICC_BUGEMULATION, false); SetModFlag(MSF_OLDVOLSWING, false); SetModFlag(MSF_COMPATIBLE_PLAY, true); } } else { //START - mpt specific: //Using member cwtv on pifh as the version number. const uint16 version = fileHeader.cwtv; if(version > 0x889 && file.Seek(mptStartPos)) { std::istringstream iStrm(std::string(file.GetRawData(), file.BytesLeft())); if(version >= 0x88D) { srlztn::SsbRead ssb(iStrm); ssb.BeginRead("mptm", MptVersion::num); ssb.ReadItem(GetTuneSpecificTunings(), "0", 1, &ReadTuningCollection); ssb.ReadItem(*this, "1", 1, &ReadTuningMap); ssb.ReadItem(Order, "2", 1, &ReadModSequenceOld); ssb.ReadItem(Patterns, FileIdPatterns, strlen(FileIdPatterns), &ReadModPatterns); ssb.ReadItem(Order, FileIdSequences, strlen(FileIdSequences), &ReadModSequences); if(ssb.m_Status & srlztn::SNT_FAILURE) { AddToLog("Unknown error occured while deserializing file."); } } else //Loading for older files. { if(GetTuneSpecificTunings().Deserialize(iStrm)) { AddToLog("Error occured - loading failed while trying to load tune specific tunings."); } else { ReadTuningMap(iStrm, *this); } } } //version condition(MPT) } return true; }
void FWadCollection::AddFile (const char *filename, FileReader *wadinfo) { int startlump; bool isdir = false; if (wadinfo == NULL) { // Does this exist? If so, is it a directory? if (!DirEntryExists(filename, &isdir)) { Printf(TEXTCOLOR_RED "%s: File or Directory not found\n", filename); PrintLastError(); return; } if (!isdir) { try { wadinfo = new FileReader(filename); } catch (CRecoverableError &err) { // Didn't find file Printf (TEXTCOLOR_RED "%s\n", err.GetMessage()); PrintLastError (); return; } } } if (!batchrun) Printf (" adding %s", filename); startlump = NumLumps; FResourceFile *resfile; if (!isdir) resfile = FResourceFile::OpenResourceFile(filename, wadinfo); else resfile = FResourceFile::OpenDirectory(filename); if (resfile != NULL) { uint32_t lumpstart = LumpInfo.Size(); resfile->SetFirstLump(lumpstart); for (uint32_t i=0; i < resfile->LumpCount(); i++) { FResourceLump *lump = resfile->GetLump(i); FWadCollection::LumpRecord *lump_p = &LumpInfo[LumpInfo.Reserve(1)]; lump_p->lump = lump; lump_p->wadnum = Files.Size(); } if (static_cast<int>(Files.Size()) == GetIwadNum() && gameinfo.gametype == GAME_Strife && gameinfo.flags & GI_SHAREWARE) { resfile->FindStrifeTeaserVoices(); } Files.Push(resfile); for (uint32_t i=0; i < resfile->LumpCount(); i++) { FResourceLump *lump = resfile->GetLump(i); if (lump->Flags & LUMPF_EMBEDDED) { FString path; path.Format("%s:%s", filename, lump->FullName.GetChars()); FileReader *embedded = lump->NewReader(); AddFile(path, embedded); } } if (hashfile) { uint8_t cksum[16]; char cksumout[33]; memset(cksumout, 0, sizeof(cksumout)); FileReader *reader = wadinfo; if (reader != NULL) { MD5Context md5; reader->Seek(0, SEEK_SET); md5.Update(reader, reader->GetLength()); md5.Final(cksum); for (size_t j = 0; j < sizeof(cksum); ++j) { sprintf(cksumout + (j * 2), "%02X", cksum[j]); } fprintf(hashfile, "file: %s, hash: %s, size: %ld\n", filename, cksumout, reader->GetLength()); } else fprintf(hashfile, "file: %s, Directory structure\n", filename); for (uint32_t i = 0; i < resfile->LumpCount(); i++) { FResourceLump *lump = resfile->GetLump(i); if (!(lump->Flags & LUMPF_EMBEDDED)) { reader = lump->NewReader(); MD5Context md5; md5.Update(reader, lump->LumpSize); md5.Final(cksum); for (size_t j = 0; j < sizeof(cksum); ++j) { sprintf(cksumout + (j * 2), "%02X", cksum[j]); } fprintf(hashfile, "file: %s, lump: %s, hash: %s, size: %d\n", filename, lump->FullName.IsNotEmpty() ? lump->FullName.GetChars() : lump->Name, cksumout, lump->LumpSize); delete reader; } } } return; } }
void FSavegameManager::ReadSaveStrings() { if (SaveGames.Size() == 0) { void *filefirst; findstate_t c_file; FString filter; LastSaved = LastAccessed = -1; quickSaveSlot = nullptr; filter = G_BuildSaveName("*." SAVEGAME_EXT, -1); filefirst = I_FindFirst(filter.GetChars(), &c_file); if (filefirst != ((void *)(-1))) { do { // I_FindName only returns the file's name and not its full path FString filepath = G_BuildSaveName(I_FindName(&c_file), -1); FResourceFile *savegame = FResourceFile::OpenResourceFile(filepath, true, true); if (savegame != nullptr) { bool oldVer = false; bool missing = false; FResourceLump *info = savegame->FindLump("info.json"); if (info == nullptr) { // savegame info not found. This is not a savegame so leave it alone. delete savegame; continue; } void *data = info->CacheLump(); FSerializer arc(nullptr); if (arc.OpenReader((const char *)data, info->LumpSize)) { int savever = 0; arc("Save Version", savever); FString engine = arc.GetString("Engine"); FString iwad = arc.GetString("Game WAD"); FString title = arc.GetString("Title"); if (engine.Compare(GAMESIG) != 0 || savever > SAVEVER) { // different engine or newer version: // not our business. Leave it alone. delete savegame; continue; } if (savever < MINSAVEVER) { // old, incompatible savegame. List as not usable. oldVer = true; } else if (iwad.CompareNoCase(Wads.GetWadName(Wads.GetIwadNum())) == 0) { missing = !G_CheckSaveGameWads(arc, false); } else { // different game. Skip this. delete savegame; continue; } FSaveGameNode *node = new FSaveGameNode; node->Filename = filepath; node->bOldVersion = oldVer; node->bMissingWads = missing; node->SaveTitle = title; InsertSaveNode(node); delete savegame; } } else // check for old formats. { FileReader file; if (file.OpenFile(filepath)) { PNGHandle *png; char sig[16]; char title[OLDSAVESTRINGSIZE + 1]; bool oldVer = true; bool addIt = false; bool missing = false; // ZDoom 1.23 betas 21-33 have the savesig first. // Earlier versions have the savesig second. // Later versions have the savegame encapsulated inside a PNG. // // Old savegame versions are always added to the menu so // the user can easily delete them if desired. title[OLDSAVESTRINGSIZE] = 0; if (nullptr != (png = M_VerifyPNG(file))) { char *ver = M_GetPNGText(png, "ZDoom Save Version"); if (ver != nullptr) { // An old version if (!M_GetPNGText(png, "Title", title, OLDSAVESTRINGSIZE)) { strncpy(title, I_FindName(&c_file), OLDSAVESTRINGSIZE); } addIt = true; delete[] ver; } delete png; } else { file.Seek(0, FileReader::SeekSet); if (file.Read(sig, 16) == 16) { if (strncmp(sig, "ZDOOMSAVE", 9) == 0) { if (file.Read(title, OLDSAVESTRINGSIZE) == OLDSAVESTRINGSIZE) { addIt = true; } } else { memcpy(title, sig, 16); if (file.Read(title + 16, OLDSAVESTRINGSIZE - 16) == OLDSAVESTRINGSIZE - 16 && file.Read(sig, 16) == 16 && strncmp(sig, "ZDOOMSAVE", 9) == 0) { addIt = true; } } } } if (addIt) { FSaveGameNode *node = new FSaveGameNode; node->Filename = filepath; node->bOldVersion = true; node->bMissingWads = false; node->SaveTitle = title; InsertSaveNode(node); } } } } while (I_FindNext(filefirst, &c_file) == 0); I_FindClose(filefirst); } } }
MapData *P_OpenMapData(const char * mapname, bool justcheck) { MapData * map = new MapData; FileReader * wadReader = nullptr; bool externalfile = !strnicmp(mapname, "file:", 5); if (externalfile) { mapname += 5; if (!FileExists(mapname)) { delete map; return NULL; } map->resource = FResourceFile::OpenResourceFile(mapname, true); wadReader = map->resource->GetReader(); } else { FString fmt; int lump_wad; int lump_map; int lump_name = -1; // Check for both *.wad and *.map in order to load Build maps // as well. The higher one will take precedence. // Names with more than 8 characters will only be checked as .wad and .map. if (strlen(mapname) <= 8) lump_name = Wads.CheckNumForName(mapname); fmt.Format("maps/%s.wad", mapname); lump_wad = Wads.CheckNumForFullName(fmt); fmt.Format("maps/%s.map", mapname); lump_map = Wads.CheckNumForFullName(fmt); if (lump_name > lump_wad && lump_name > lump_map && lump_name != -1) { int lumpfile = Wads.GetLumpFile(lump_name); int nextfile = Wads.GetLumpFile(lump_name+1); map->lumpnum = lump_name; if (lumpfile != nextfile) { // The following lump is from a different file so whatever this is, // it is not a multi-lump Doom level so let's assume it is a Build map. map->MapLumps[0].Reader = Wads.ReopenLumpReader(lump_name); if (!P_IsBuildMap(map)) { delete map; return NULL; } return map; } // This case can only happen if the lump is inside a real WAD file. // As such any special handling for other types of lumps is skipped. map->MapLumps[0].Reader = Wads.ReopenLumpReader(lump_name); strncpy(map->MapLumps[0].Name, Wads.GetLumpFullName(lump_name), 8); map->Encrypted = Wads.IsEncryptedFile(lump_name); map->InWad = true; if (map->Encrypted) { // If it's encrypted, then it's a Blood file, presumably a map. if (!P_IsBuildMap(map)) { delete map; return NULL; } return map; } int index = 0; if (stricmp(Wads.GetLumpFullName(lump_name + 1), "TEXTMAP") != 0) { for(int i = 1;; i++) { // Since levels must be stored in WADs they can't really have full // names and for any valid level lump this always returns the short name. const char * lumpname = Wads.GetLumpFullName(lump_name + i); try { index = GetMapIndex(mapname, index, lumpname, !justcheck); } catch(...) { delete map; throw; } if (index == -2) { delete map; return NULL; } if (index == ML_BEHAVIOR) map->HasBehavior = true; // The next lump is not part of this map anymore if (index < 0) break; map->MapLumps[index].Reader = Wads.ReopenLumpReader(lump_name + i); strncpy(map->MapLumps[index].Name, lumpname, 8); } } else { map->isText = true; map->MapLumps[1].Reader = Wads.ReopenLumpReader(lump_name + 1); for(int i = 2;; i++) { const char * lumpname = Wads.GetLumpFullName(lump_name + i); if (lumpname == NULL) { I_Error("Invalid map definition for %s", mapname); } else if (!stricmp(lumpname, "ZNODES")) { index = ML_GLZNODES; } else if (!stricmp(lumpname, "BLOCKMAP")) { // there is no real point in creating a blockmap but let's use it anyway index = ML_BLOCKMAP; } else if (!stricmp(lumpname, "REJECT")) { index = ML_REJECT; } else if (!stricmp(lumpname, "DIALOGUE")) { index = ML_CONVERSATION; } else if (!stricmp(lumpname, "BEHAVIOR")) { index = ML_BEHAVIOR; map->HasBehavior = true; } else if (!stricmp(lumpname, "ENDMAP")) { break; } else continue; map->MapLumps[index].Reader = Wads.ReopenLumpReader(lump_name + i); strncpy(map->MapLumps[index].Name, lumpname, 8); } } return map; } else { if (lump_map > lump_wad) { lump_wad = lump_map; } if (lump_wad == -1) { delete map; return NULL; } map->lumpnum = lump_wad; auto reader = Wads.ReopenLumpReader(lump_wad); map->resource = FResourceFile::OpenResourceFile(Wads.GetLumpFullName(lump_wad), reader, true); wadReader = map->resource->GetReader(); } } uint32_t id; // Although we're using the resource system, we still want to be sure we're // reading from a wad file. wadReader->Seek(0, FileReader::SeekSet); wadReader->Read(&id, sizeof(id)); if (id == IWAD_ID || id == PWAD_ID) { char maplabel[9]=""; int index=0; map->MapLumps[0].Reader = map->resource->GetLump(0)->NewReader(); strncpy(map->MapLumps[0].Name, map->resource->GetLump(0)->Name, 8); for(uint32_t i = 1; i < map->resource->LumpCount(); i++) { const char* lumpname = map->resource->GetLump(i)->Name; if (i == 1 && !strnicmp(lumpname, "TEXTMAP", 8)) { map->isText = true; map->MapLumps[ML_TEXTMAP].Reader = map->resource->GetLump(i)->NewReader(); strncpy(map->MapLumps[ML_TEXTMAP].Name, lumpname, 8); for(int i = 2;; i++) { lumpname = map->resource->GetLump(i)->Name; if (!strnicmp(lumpname, "ZNODES",8)) { index = ML_GLZNODES; } else if (!strnicmp(lumpname, "BLOCKMAP",8)) { // there is no real point in creating a blockmap but let's use it anyway index = ML_BLOCKMAP; } else if (!strnicmp(lumpname, "REJECT",8)) { index = ML_REJECT; } else if (!strnicmp(lumpname, "DIALOGUE",8)) { index = ML_CONVERSATION; } else if (!strnicmp(lumpname, "BEHAVIOR",8)) { index = ML_BEHAVIOR; map->HasBehavior = true; } else if (!strnicmp(lumpname, "ENDMAP",8)) { return map; } else continue; map->MapLumps[index].Reader = map->resource->GetLump(i)->NewReader(); strncpy(map->MapLumps[index].Name, lumpname, 8); } } if (i>0) { try { index = GetMapIndex(maplabel, index, lumpname, !justcheck); } catch(...) { delete map; throw; } if (index == -2) { delete map; return NULL; } if (index == ML_BEHAVIOR) map->HasBehavior = true; // The next lump is not part of this map anymore if (index < 0) break; } else { strncpy(maplabel, lumpname, 8); maplabel[8]=0; } map->MapLumps[index].Reader = map->resource->GetLump(i)->NewReader(); strncpy(map->MapLumps[index].Name, lumpname, 8); } } else { // This is a Build map and not subject to WAD consistency checks. //map->MapLumps[0].Size = wadReader->GetLength(); if (!P_IsBuildMap(map)) { delete map; return NULL; } } return map; }
FPNGTexture::FPNGTexture (FileReader &lump, int lumpnum, const FString &filename, int width, int height, BYTE depth, BYTE colortype, BYTE interlace) : FTexture(NULL, lumpnum), SourceFile(filename), Pixels(0), Spans(0), BitDepth(depth), ColorType(colortype), Interlace(interlace), PaletteMap(0), PaletteSize(0), StartOfIDAT(0) { union { DWORD palette[256]; BYTE pngpal[256][3]; }; BYTE trans[256]; bool havetRNS = false; DWORD len, id; int i; UseType = TEX_MiscPatch; LeftOffset = 0; TopOffset = 0; bMasked = false; Width = width; Height = height; CalcBitSize (); memset (trans, 255, 256); // Parse pre-IDAT chunks. I skip the CRCs. Is that bad? lump.Seek (33, SEEK_SET); lump >> len >> id; while (id != MAKE_ID('I','D','A','T') && id != MAKE_ID('I','E','N','D')) { len = BigLong((unsigned int)len); switch (id) { default: lump.Seek (len, SEEK_CUR); break; case MAKE_ID('g','r','A','b'): // This is like GRAB found in an ILBM, except coordinates use 4 bytes { DWORD hotx, hoty; int ihotx, ihoty; lump >> hotx >> hoty; ihotx = BigLong((int)hotx); ihoty = BigLong((int)hoty); if (ihotx < -32768 || ihotx > 32767) { Printf ("X-Offset for PNG texture %s is bad: %d (0x%08x)\n", Wads.GetLumpFullName (lumpnum), ihotx, ihotx); ihotx = 0; } if (ihoty < -32768 || ihoty > 32767) { Printf ("Y-Offset for PNG texture %s is bad: %d (0x%08x)\n", Wads.GetLumpFullName (lumpnum), ihoty, ihoty); ihoty = 0; } LeftOffset = (int)ihotx; TopOffset = (int)ihoty; } break; case MAKE_ID('P','L','T','E'): PaletteSize = MIN<int> (len / 3, 256); lump.Read (pngpal, PaletteSize * 3); if (PaletteSize * 3 != (int)len) { lump.Seek (len - PaletteSize * 3, SEEK_CUR); } for (i = PaletteSize - 1; i >= 0; --i) { palette[i] = MAKERGB(pngpal[i][0], pngpal[i][1], pngpal[i][2]); } break; case MAKE_ID('t','R','N','S'): lump.Read (trans, len); havetRNS = true; break; case MAKE_ID('a','l','P','h'): bAlphaTexture = true; bMasked = true; break; } lump >> len >> len; // Skip CRC id = MAKE_ID('I','E','N','D'); lump >> id; } StartOfIDAT = lump.Tell() - 8; switch (colortype) { case 4: // Grayscale + Alpha bMasked = true; // intentional fall-through case 0: // Grayscale if (!bAlphaTexture) { if (colortype == 0 && havetRNS && trans[0] != 0) { bMasked = true; PaletteSize = 256; PaletteMap = new BYTE[256]; memcpy (PaletteMap, GrayMap, 256); PaletteMap[trans[0]] = 0; } else { PaletteMap = GrayMap; } } break; case 3: // Paletted PaletteMap = new BYTE[PaletteSize]; GPalette.MakeRemap (palette, PaletteMap, trans, PaletteSize); for (i = 0; i < PaletteSize; ++i) { if (trans[i] == 0) { bMasked = true; PaletteMap[i] = 0; } } break; case 6: // RGB + Alpha bMasked = true; break; } }