Example #1
0
        public static JJ2DataFile Open(string path, bool strictParser)
        {
            using (Stream s = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read))
                using (BinaryReader r = new BinaryReader(s)) {
                    JJ2DataFile j2d = new JJ2DataFile();

                    uint magic = r.ReadUInt32();
                    if (magic != 0x42494C50 /*PLIB*/)
                    {
                        throw new InvalidOperationException("Invalid magic string");
                    }

                    uint signature = r.ReadUInt32();
                    if (signature != 0xBEBAADDE)
                    {
                        throw new InvalidOperationException("Invalid signature");
                    }

                    uint version = r.ReadUInt32();

                    uint recordedSize = r.ReadUInt32();
                    if (strictParser && s.Length != recordedSize)
                    {
                        throw new InvalidOperationException("Unexpected file size");
                    }

                    uint recordedCRC             = r.ReadUInt32();
                    int  headerBlockPackedSize   = r.ReadInt32();
                    int  headerBlockUnpackedSize = r.ReadInt32();

                    JJ2Block headerBlock = new JJ2Block(s, headerBlockPackedSize, headerBlockUnpackedSize);

                    try {
                        while (true)
                        {
                            string name = headerBlock.ReadString(32, true);

                            uint type             = headerBlock.ReadUInt32();
                            uint offset           = headerBlock.ReadUInt32();
                            uint fileCRC          = headerBlock.ReadUInt32();
                            int  filePackedSize   = headerBlock.ReadInt32();
                            int  fileUnpackedSize = headerBlock.ReadInt32();

                            //Console.WriteLine(name + " | " + type.ToString("X") + " | " + fileUnpackedSize + " | " + offset);

                            s.Position = offset;
                            JJ2Block fileBlock = new JJ2Block(s, filePackedSize, fileUnpackedSize);
                            byte[]   data      = fileBlock.AsByteArray();
                        }
                    } catch (EndOfStreamException) {
                        // End of file list
                    }

                    // ToDo: Extract files, but it's not needed for now...

                    return(j2d);
                }
        }
Example #2
0
        public static JJ2Tileset Open(string path, bool strictParser)
        {
            using (Stream s = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read)) {
                // Skip copyright notice
                s.Seek(180, SeekOrigin.Current);

                JJ2Tileset tileset = new JJ2Tileset();

                JJ2Block headerBlock = new JJ2Block(s, 262 - 180);

                uint magic = headerBlock.ReadUInt32();
                if (magic != 0x454C4954 /*TILE*/)
                {
                    throw new InvalidOperationException("Invalid magic string");
                }

                uint signature = headerBlock.ReadUInt32();
                if (signature != 0xAFBEADDE)
                {
                    throw new InvalidOperationException("Invalid signature");
                }

                tileset.name = headerBlock.ReadString(32, true);

                ushort versionNum = headerBlock.ReadUInt16();
                tileset.version = (versionNum <= 512 ? JJ2Version.BaseGame : JJ2Version.TSF);

                int recordedSize = headerBlock.ReadInt32();
                if (strictParser && s.Length != recordedSize)
                {
                    throw new InvalidOperationException("Unexpected file size");
                }

                // Get the CRC; would check here if it matches if we knew what variant it is AND what it applies to
                // Test file across all CRC32 variants + Adler had no matches to the value obtained from the file
                // so either the variant is something else or the CRC is not applied to the whole file but on a part
                int recordedCRC = headerBlock.ReadInt32();

                // Read the lengths, uncompress the blocks and bail if any block could not be uncompressed
                // This could look better without all the copy-paste, but meh.
                int infoBlockPackedSize    = headerBlock.ReadInt32();
                int infoBlockUnpackedSize  = headerBlock.ReadInt32();
                int imageBlockPackedSize   = headerBlock.ReadInt32();
                int imageBlockUnpackedSize = headerBlock.ReadInt32();
                int alphaBlockPackedSize   = headerBlock.ReadInt32();
                int alphaBlockUnpackedSize = headerBlock.ReadInt32();
                int maskBlockPackedSize    = headerBlock.ReadInt32();
                int maskBlockUnpackedSize  = headerBlock.ReadInt32();

                JJ2Block infoBlock  = new JJ2Block(s, infoBlockPackedSize, infoBlockUnpackedSize);
                JJ2Block imageBlock = new JJ2Block(s, imageBlockPackedSize, imageBlockUnpackedSize);
                JJ2Block alphaBlock = new JJ2Block(s, alphaBlockPackedSize, alphaBlockUnpackedSize);
                JJ2Block maskBlock  = new JJ2Block(s, maskBlockPackedSize, maskBlockUnpackedSize);

                tileset.LoadMetadata(infoBlock);
                tileset.LoadImageData(imageBlock, alphaBlock);
                tileset.LoadMaskData(maskBlock);

                return(tileset);
            }
        }