Example #1
0
        private TES4LoadedRecord FetchTES4(FileStream stream)
        {
            byte[]           recordHeader = stream.Read(TES4LoadedRecord.RECORD_HEADER_SIZE);
            int              recordSize   = PHPFunction.UnpackV(recordHeader.Skip(4).Take(4).ToArray());//Throw away the first four bytes.
            int              recordFlags  = PHPFunction.UnpackV(recordHeader.Skip(8).Take(4).ToArray());
            int              recordFormid = PHPFunction.UnpackV(recordHeader.Skip(12).Take(4).ToArray());
            TES4LoadedRecord tes4record   = new TES4LoadedRecord(this, TES4RecordType.TES4, recordFormid, recordSize, recordFlags);

            tes4record.Load(stream, new TES4RecordLoadScheme(new string[] { "MAST" }));
            return(tes4record);
        }
        public TES4LoadedRecord FindByEDID(string edid, bool throwNotFoundException)
        {
            string           lowerEdid = edid.ToLower();
            TES4LoadedRecord record    = this.edidIndex.Search(lowerEdid);

            if (record != null)
            {
                return(record);
            }
            if (throwNotFoundException)
            {
                throw new RecordNotFoundException("EDID " + edid + " not found.");
            }
            return(null);
        }
        /*
         * @throws InvalidESFileException
         */
        public IEnumerable <ITES4Record> Load(FileStream fileContents, TES4File file, TES4GrupLoadScheme scheme, bool isTopLevelGrup)
        {
            long startPosition = fileContents.Position;

            byte[] headerBytes  = fileContents.Read(GRUP_HEADER_SIZE);
            string headerString = TES4File.ISO_8859_1.Value.GetString(headerBytes);

            if (headerString.Substring(0, 4) != "GRUP")
            {
                throw new InvalidESFileException("Invalid GRUP magic, found " + headerString.Substring(0, 4));
            }

            this.Size = PHPFunction.UnpackV(headerBytes.Skip(4).Take(4).ToArray());
            if (isTopLevelGrup)
            {
                this.Type = TES4RecordType.First(headerString.Substring(8, 4));
            }

            long end = startPosition + this.Size;

            while (fileContents.Position < end)
            {
                //Ineffective lookahead, but oh well
                byte[] nextEntryTypeBytes = new byte[4];
                int    bytesRead          = fileContents.Read(nextEntryTypeBytes);
                if (bytesRead == 0)
                {
                    break;
                }
                fileContents.Seek(-4, SeekOrigin.Current);
                string nextEntryType = TES4File.ISO_8859_1.Value.GetString(nextEntryTypeBytes);
                switch (nextEntryType)
                {
                case "GRUP":
                {
                    TES4Grup nestedGrup = new TES4Grup();
                    foreach (var subrecord in nestedGrup.Load(fileContents, file, scheme, false))
                    {
                        yield return(subrecord);
                    }
                    break;
                }

                default:
                {
                    byte[]         recordHeaderBytes = fileContents.Read(TES4LoadedRecord.RECORD_HEADER_SIZE);
                    string         recordTypeString  = TES4File.ISO_8859_1.Value.GetString(recordHeaderBytes.Take(4).ToArray());
                    TES4RecordType recordType        = TES4RecordType.First(recordTypeString);
                    int            recordSize        = PHPFunction.UnpackV(recordHeaderBytes.Skip(4).Take(4).ToArray());
                    int            recordFlags       = PHPFunction.UnpackV(recordHeaderBytes.Skip(8).Take(4).ToArray());
                    int            recordFormid      = PHPFunction.UnpackV(recordHeaderBytes.Skip(12).Take(4).ToArray());
                    if (scheme.ShouldLoad(recordType))
                    {
                        TES4LoadedRecord record = new TES4LoadedRecord(file, recordType, recordFormid, recordSize, recordFlags);
                        record.Load(fileContents, scheme.GetRulesFor(recordType));
                        this.records.Add(record);
                        yield return(record);
                    }
                    else
                    {
                        fileContents.Seek(recordSize, SeekOrigin.Current);
                    }
                    break;
                }
                }
            }
        }