Example #1
0
        public IEnumerable<Element> EnumerateElements(SubRecord sr, Dictionary<int, Conditional> conditions)
        {
            if (sr == null)
            {
                return new Element[0];
            }

            return sr.EnumerateElements(conditions);
        }
Example #2
0
        /// <summary>
        /// </summary>
        /// <param name="sr">
        /// </param>
        /// <param name="rawData">
        ///     Retain raw data instead of converting to more usuable form
        /// </param>
        /// <returns>
        ///     The System.Collections.Generic.IEnumerable`1[T -&gt; TESVSnip.Element].
        /// </returns>
        public IEnumerable<Element> EnumerateElements(SubRecord sr, bool rawData)
        {
            if (sr == null)
            {
                return new Element[0];
            }

            return sr.EnumerateElements(rawData);
        }
Example #3
0
        /// <summary>
        /// </summary>
        /// <param name="sr">
        /// </param>
        /// <param name="rawData">
        /// Retain raw data instead of converting to more usuable form
        /// </param>
        /// <returns>
        /// The System.Collections.Generic.IEnumerable`1[T -&gt; TESVSnip.Element].
        /// </returns>
        internal IEnumerable <Element> EnumerateElements(SubRecord sr, bool rawData)
        {
            if (sr == null)
            {
                return(new Element[0]);
            }

            return(sr.EnumerateElements(rawData));
        }
Example #4
0
        internal IEnumerable <Element> EnumerateElements(SubRecord sr, Dictionary <int, Conditional> conditions)
        {
            if (sr == null)
            {
                return(new Element[0]);
            }

            return(sr.EnumerateElements(conditions));
        }
Example #5
0
        public override bool Evaluate(Record r, SubRecord sr)
        {
            bool any = false;

            foreach (bool value in sr.EnumerateElements().Where(x => x.Structure.name == this.Record.name).Select(x => this.Evaluate(r, sr, x)))
            {
                if (!value)
                {
                    return(false);
                }

                any = true;
            }

            return(any);
        }
Example #6
0
        public bool AddMaster(string masterName)
        {
            Record brcTES4 = this.Records.OfType <Record>().FirstOrDefault(x => x.Name == "TES4");

            if (brcTES4 == null)
            {
                throw new ApplicationException("Plugin lacks a valid TES4 record. Cannot continue.");
            }

            // find existing if already present
            foreach (var mast in brcTES4.SubRecords.Where(x => x.Name == "MAST"))
            {
                var path = mast.GetStrData();
                if (string.Compare(path, masterName, true) == 0)
                {
                    return(false);
                }
            }

            int idx = brcTES4.SubRecords.IndexOf(brcTES4.SubRecords.FirstOrDefault(x => x.Name == "INTV"));

            if (idx < 0)
            {
                idx = brcTES4.SubRecords.Count;
            }

            var sbrMaster = new SubRecord();

            sbrMaster      = new SubRecord();
            sbrMaster.Name = "DATA";
            sbrMaster.SetData(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 });
            brcTES4.InsertRecord(idx, sbrMaster);

            sbrMaster      = new SubRecord();
            sbrMaster.Name = "MAST";
            int intCount = Encoding.Instance.GetByteCount(masterName);
            var bteData  = new byte[intCount + 1];

            Array.Copy(Encoding.Instance.GetBytes(masterName), bteData, intCount);
            sbrMaster.SetData(bteData);
            brcTES4.InsertRecord(idx, sbrMaster);

            // Fix Masters
            // Update IDs for current record to be +1
            return(true);
        }
Example #7
0
        public override bool Evaluate(Record r, SubRecord sr)
        {
            if (this.Type == BatchCondRecordType.Create)
            {
                if (sr == null)
                {
                    // guess the best insert location
                    int             idx     = -1;
                    var             records = r.GetStructures();
                    RecordStructure rs;
                    if (records.TryGetValue(r.Name, out rs))
                    {
                        for (int i = Array.FindIndex(rs.subrecords, structure => structure.name == this.Record.name) - 1; i >= 0; --i)
                        {
                            var srsname = rs.subrecords[i].name;
                            idx = r.SubRecords.IndexOf(r.SubRecords.FirstOrDefault(x => x.Name == srsname));
                        }
                    }

                    sr = new SubRecord(this.Record);
                    if (idx < 0)
                    {
                        r.SubRecords.Add(sr);
                    }
                    else
                    {
                        r.SubRecords.Insert(idx + 1, sr);
                    }
                }
            }
            else if (this.Type == BatchCondRecordType.Delete)
            {
                while (sr != null)
                {
                    r.SubRecords.Remove(sr);
                    sr = r.SubRecords.FirstOrDefault(x => x.Name == this.Record.name);
                }
            }

            return(true);
        }
Example #8
0
        public bool Evaluate(Record r, SubRecord sr, Element se)
        {
            if (se == null)
            {
                return(false);
            }

            var value = sr.GetCompareValue(se);
            int diff  = ValueComparer.Compare(value, this.Value);

            switch (this.Type)
            {
            case BatchCondElementType.Set:
                break;

            case BatchCondElementType.Add:
                break;

            case BatchCondElementType.Subtract:
                break;

            case BatchCondElementType.Multiply:
                break;

            case BatchCondElementType.Divide:
                break;

            case BatchCondElementType.Clear:
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            return(false);
        }
Example #9
0
        public bool AddMaster(string masterName)
        {
            Record brcTES4 = this.Records.OfType <Record>().FirstOrDefault(x => x.Name.StartsWith("TES"));

            if (brcTES4 == null)
            {
                throw new ApplicationException("Plugin lacks a valid TES4 record. Cannot continue.");
            }

            // find existing if already present
            foreach (var mast in brcTES4.SubRecords.Where(x => x.Name == "MAST"))
            {
                var path = mast.GetStrData();
                if (string.Compare(path, masterName, true) == 0)
                {
                    return(false);
                }
            }

            int idx = brcTES4.SubRecords.IndexOf(brcTES4.SubRecords.FirstOrDefault(x => x.Name == "INTV"));

            if (idx < 0)
            {
                idx = brcTES4.SubRecords.Count;
            }

            var sbrMaster = new SubRecord();

            sbrMaster.Name = "DATA";
            sbrMaster.SetData(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 });
            brcTES4.InsertRecord(idx, sbrMaster);

            sbrMaster      = new SubRecord();
            sbrMaster.Name = "MAST";
            int intCount = Encoding.Instance.GetByteCount(masterName);
            var bteData  = new byte[intCount + 1];

            Array.Copy(Encoding.Instance.GetBytes(masterName), bteData, intCount);
            sbrMaster.SetData(bteData);
            brcTES4.InsertRecord(idx, sbrMaster);

            int masterIdx = brcTES4.SubRecords.Count(x => x.Name == "MAST") - 1;

            // Update IDs for current record to be +1
            // Fix FormID and references now
            foreach (var rec in EnumerateRecords(null).Select(x => x.Value)
                     .SkipWhile(x => RecordLayout.NoNewCopyTypes.Contains(x.Name)))
            {
                if ((rec.FormID >> 24) >= masterIdx)
                {
                    rec.FormID = (rec.FormID & 0x00FFFFFF) | (((rec.FormID >> 24) + 1) << 24);
                }

                // now handle child references
                rec.MatchRecordStructureToRecord();
                foreach (var elem in rec.SubRecords
                         .SelectMany(sr => sr.EnumerateElements())
                         .Where(elem => elem.Structure != null && elem.Structure.type == ElementValueType.FormID))
                {
                    var value = elem.GetValue <uint>();
                    if ((value >> 24) >= masterIdx)
                    {
                        elem.AssignValue <uint>((value & 0x00FFFFFF) | (((value >> 24) + 1) << 24));
                    }
                }
            }

            return(true);
        }
Example #10
0
 private SubRecord(SubRecord sr)
 {
     this.Owner = null;
     Name       = sr.Name;
     this.Data  = (byte[])sr.Data.Clone();
 }
Example #11
0
        internal Record(string name, uint dataSizeParam, SnipStreamWrapper snipStreamWrapper, bool oblivion)  //internal Record(string name, uint dataSize, BinaryReader recordReader, bool oblivion)
        {
            bool compressed = false;
            uint amountRead = 0;
            uint realSize   = 0;

            //MemoryStream stream = null;
            //BinaryReader dataReader = null;
            SubRecord record = null;

            //long ws;

            try
            {
                this.dataSize = dataSizeParam;

                this.SubRecords = new AdvancedList <SubRecord>(1)
                {
                    AllowSorting = false
                };

                //if (snipStreamWrapper.SnipStream.Position >= 1330432)
                //    Name = name;

                Name = name;
                RecordsTace.AddRecordToRecordsList(Name);
                this.Flags1 = snipStreamWrapper.ReadUInt32(); //recordReader.ReadUInt32();
                this.FormID = snipStreamWrapper.ReadUInt32(); //recordReader.ReadUInt32();
                this.Flags2 = snipStreamWrapper.ReadUInt32(); //recordReader.ReadUInt32();
                if (!oblivion)
                {
                    this.Flags3 = snipStreamWrapper.ReadUInt32(); //recordReader.ReadUInt32();
                }

                //if (this.FormID == 496431)
                //    Name = name;

                compressed = (this.Flags1 & 0x00040000) != 0;
                amountRead = 0;
                realSize   = dataSizeParam;

                if (!compressed)
                {
                    realSize = dataSizeParam;
                }

                if (compressed)
                {
                    realSize = snipStreamWrapper.ReadUInt32(); // recordReader.ReadUInt32();
                    // snipStreamWrapper.JumpTo(-4,SeekOrigin.Current);
                    //if (realSize > 0)
                    dataSizeParam -= 4;
                    RecordsTace.AddRecordToCompressedRecordsList(Name);
                }

#if DEBUGCOMPRESSREALSIZE
                if (compressed & realSize == 0)
                {
                    Clipboard.SetText(name + " - " + this.FormID.ToString(CultureInfo.InvariantCulture));
                }
#endif

                //using (var stream = new MemoryStream(recordReader.ReadBytes((int) dataSize)))
                //{
                //using (var dataReader = compressed ? ZLib.Decompress(stream, (int) realSize) : new BinaryReader(stream))
                //{.
                try
                {
                    //if (dataSize == 0)
                    //    throw new TESParserException("Record.Record: ZLib inflate error. Output buffer is empty.");

                    if (dataSizeParam > 0) //dawnguard.esm at position 6.812.369 at a dataSize == 0 - Record=NAVM
                    {
                        ZLibWrapper.CopyStreamToInputBuffer(snipStreamWrapper.SnipStream, dataSizeParam);
                        //stream = new MemoryStream(recordReader.ReadBytes((int) dataSize));
                        //dataReader = compressed ? ZLib.Decompress(stream, out compressLevel, (int)realSize) : new BinaryReader(stream);
                        if (compressed & realSize > 0)
                        {
                            //Clipboard.SetText(Name + realSize.ToString(CultureInfo.InvariantCulture));
                            ZLib.Decompress(compressLevel: out compressLevel, expectedSize: (int)realSize);
                            //Array.Copy();
                        }
                        else
                        {
                            ZLibWrapper.CopyInputBufferToOutputBuffer(dataSizeParam);
                        }
                    }
                    else
                    {
                        ZLibWrapper.ResetBufferSizeAndPosition();
                    }
                }
                catch (Exception ex)
                {
                    throw new TESParserException("Record.Record: ZLib error" + Environment.NewLine +
                                                 "Message: " + ex.Message +
                                                 Environment.NewLine +
                                                 "StackTrace: " + ex.StackTrace);
                }

                if (compressed & dataSizeParam > 0)          //dawnguard.esm at position 6.812.369 at a dataSize == 0 - Record=NAVM
                {
                    if (ZLibWrapper.OutputBufferLength <= 0) //if (dataReader == null)
                    {
                        throw new TESParserException("Record.Record: ZLib inflate error. Output buffer is empty.");
                    }
                }

                while (ZLibWrapper.OutputBufferPosition < ZLibWrapper.OutputBufferLength)
                //while (dataReader.BaseStream.Position < dataReader.BaseStream.Length)
                {
                    var  type = "XXXX";
                    uint size = 0;
                    if (realSize == 0)     //compressed &
                    {
                        type = "????";     //ReadRecName(ZLibWrapper.Read4Bytes());
                        size = dataSizeParam;
                        //realSize = dataSizeParam;
                        //this.dataSize = dataSizeParam;
                    }
                    else
                    {
                        type = ReadRecName(ZLibWrapper.Read4Bytes());     //var type = ReadRecName(dataReader);

                        if (type == "XXXX")
                        {
                            ZLibWrapper.ReadUInt16();                     //dataReader.ReadUInt16();
                            size = ZLibWrapper.ReadUInt32();              //dataReader.ReadUInt32();
                            type = ReadRecName(ZLibWrapper.Read4Bytes()); //ReadRecName(dataReader);
                            ZLibWrapper.ReadUInt16();                     //dataReader.ReadUInt16();
                        }
                        else
                        {
                            size = ZLibWrapper.ReadUInt16();     //dataReader.ReadUInt16();
                        }
                    }
                    record = new SubRecord(this, type, snipStreamWrapper, size);
                    //record = new SubRecord(this, type, dataReader, size); //var record = new SubRecord(this, type, dataReader, size);
                    this.SubRecords.Add(record);
                    amountRead += (uint)record.Size2;
                }

                //} //using (var dataReader = compressed ? ZLib.Decompress(stream, (int) realSize) : new BinaryReader(stream))
                //if (dataReader != null)
                //{
                //    dataReader.Close();
                //    dataReader.Dispose();
                //    dataReader = null;
                //}

                if ((compressed & realSize != 0) | (!compressed))
                {
                    if (amountRead > realSize)
                    {
                        Debug.Print(
                            " * ERROR: SUB-RECORD {0} DATA DOESN'T MATCH THE SIZE SPECIFIED IN THE HEADER: DATA-SIZE={1} REAL-SIZE={2} AMOUNT-READ={3}",
                            name, dataSizeParam, realSize, amountRead);
                        throw new TESParserException(
                                  string.Format(
                                      "Subrecord block did not match the size specified in the record header: ExpectedSize={0} ReadSize={1} DataSize={2}",
                                      realSize, amountRead, dataSizeParam));
                    }
                }


                this.descNameOverride = this.DefaultDescriptiveName;
                this.UpdateShortDescription();

                // br.BaseStream.Position+=Size;

                //} //using (var stream = new MemoryStream(recordReader.ReadBytes((int) dataSize)))
            }
            catch (Exception ex)
            {
                string errMsg =
                    "Message: " + ex.Message +
                    Environment.NewLine +
                    Environment.NewLine +
                    "StackTrace: " + ex.StackTrace +
                    Environment.NewLine +
                    Environment.NewLine +
                    "Source: " + ex.Source +
                    Environment.NewLine +
                    Environment.NewLine +
                    "GetType: " + ex.GetType().ToString();

                System.Windows.Forms.Clipboard.SetDataObject(errMsg, true);

                // Create an EventLog instance and assign its source.
                EventLog myLog = new EventLog();
                myLog.Source = "ThreadException";
                myLog.WriteEntry(errMsg);

                MessageBox.Show(errMsg, "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
            finally
            {
                //if (stream != null)
                //{
                //    stream.Close();
                //    stream.Dispose();
                //}
            }
        }
Example #12
0
 public override bool Match(Record r, SubRecord sr, Element se)
 {
     return(false);
 }
Example #13
0
 public IEnumerable<Element> EnumerateElements(SubRecord sr)
 {
     return this.EnumerateElements(sr, false);
 }
Example #14
0
 public override bool Match(Record r, SubRecord sr)
 {
     return(this.Type == SearchCondRecordType.Exists ^ sr == null);
 }
Example #15
0
        public override bool Match(Record r, SubRecord sr, Element se)
        {
            if (this.Type == SearchCondElementType.Exists && se != null)
            {
                return(true);
            }

            if (this.Type == SearchCondElementType.Missing && se == null)
            {
                return(true);
            }

            if (se == null)
            {
                return(false);
            }

            var value = sr.GetCompareValue(se);
            int diff  = ValueComparer.Compare(value, this.Value);

            switch (this.Type)
            {
            case SearchCondElementType.Equal:
                return(diff == 0);

            case SearchCondElementType.Not:
                return(diff != 0);

            case SearchCondElementType.Greater:
                return(diff > 0);

            case SearchCondElementType.Less:
                return(diff < 0);

            case SearchCondElementType.GreaterEqual:
                return(diff >= 0);

            case SearchCondElementType.LessEqual:
                return(diff <= 0);

            case SearchCondElementType.StartsWith:
                if (diff == 0)
                {
                    return(true);
                }

                if (value != null && this.Value != null)
                {
                    return(value.ToString().StartsWith(this.Value.ToString(), StringComparison.CurrentCultureIgnoreCase));
                }

                break;

            case SearchCondElementType.EndsWith:
                if (diff == 0)
                {
                    return(true);
                }

                if (value != null && this.Value != null)
                {
                    return(value.ToString().EndsWith(this.Value.ToString(), StringComparison.CurrentCultureIgnoreCase));
                }

                break;

            case SearchCondElementType.Contains:
                if (diff == 0)
                {
                    return(true);
                }

                if (value != null && this.Value != null)
                {
                    return(value.ToString().IndexOf(this.Value.ToString(), StringComparison.CurrentCultureIgnoreCase) >= 0);
                }

                break;
            }

            return(false);
        }
Example #16
0
        internal Record(string name, uint dataSize, BinaryReader recordReader, bool oblivion)
        {
            this.dataSize = dataSize;

            this.SubRecords = new AdvancedList <SubRecord>(1)
            {
                AllowSorting = false
            };
            Name        = name;
            this.Flags1 = recordReader.ReadUInt32();
            this.FormID = recordReader.ReadUInt32();
            this.Flags2 = recordReader.ReadUInt32();
            if (!oblivion)
            {
                this.Flags3 = recordReader.ReadUInt32();
            }

            var  compressed = (this.Flags1 & 0x00040000) != 0;
            uint amountRead = 0;

            var realSize = dataSize;

            if (compressed)
            {
                realSize  = recordReader.ReadUInt32();
                dataSize -= 4;
            }

            using (var stream = new MemoryStream(recordReader.ReadBytes((int)dataSize)))
            {
                using (var dataReader = compressed ? ZLib.Decompress(stream, (int)realSize) : new BinaryReader(stream))
                {
                    while (dataReader.BaseStream.Position < dataReader.BaseStream.Length)
                    {
                        var  type = ReadRecName(dataReader);
                        uint size;
                        if (type == "XXXX")
                        {
                            dataReader.ReadUInt16();
                            size = dataReader.ReadUInt32();
                            type = ReadRecName(dataReader);
                            dataReader.ReadUInt16();
                        }
                        else
                        {
                            size = dataReader.ReadUInt16();
                        }

                        var record = new SubRecord(this, type, dataReader, size);
                        this.SubRecords.Add(record);
                        amountRead += (uint)record.Size2;
                    }
                }
            }

            if (amountRead > realSize)
            {
                Debug.Print(" * ERROR: SUB-RECORD {0} DATA DOESN'T MATCH THE SIZE SPECIFIED IN THE HEADER: DATA-SIZE={1} REAL-SIZE={2} AMOUNT-READ={3}", name, dataSize, realSize, amountRead);
                throw new TESParserException(
                          string.Format("Subrecord block did not match the size specified in the record header: ExpectedSize={0} ReadSize={1} DataSize={2}", realSize, amountRead, dataSize));
            }

            this.descNameOverride = this.DefaultDescriptiveName;
            this.UpdateShortDescription();

            // br.BaseStream.Position+=Size;
        }
Example #17
0
        internal Record(string name, uint dataSize, BinaryReader recordReader, TESVSnip.Domain.Data.DomainDefinition define)
        {
            this.dataSize = dataSize;

            int estimatedCount = Math.Max( Math.Min(16, (int)dataSize/10), 0 );
            SubRecords = new AdvancedList<SubRecord>(estimatedCount) { AllowSorting = false };

            Name = name;
            Flags1 = recordReader.ReadUInt32();
            FormID = recordReader.ReadUInt32();
            if (define.RecSize >= 12)
                Flags2 = recordReader.ReadUInt32();
            if (define.RecSize >= 16)
                Flags3 = recordReader.ReadUInt32();

            bool compressed = (Flags1 & 0x00040000) != 0;
            uint amountRead = 0;

            uint realSize = dataSize;
            if (compressed)
            {
                realSize = recordReader.ReadUInt32();
                dataSize -= 4;
            }

            using (var stream = new MemoryStream(recordReader.ReadBytes((int)dataSize),false))
            using (var br = new BinaryReader(stream))
            {
                var dataReader = compressed 
                    ? Decompressor.Decompress(br, (int)dataSize, (int)realSize, out compressLevel)
                    : br;
                {
                    while (true)
                    {
                        long left = dataReader.BaseStream.Length - dataReader.BaseStream.Position;
                        if (left < 4)
                        {
                            break;
                        }
                        string type = ReadRecName(dataReader);
                        uint size;
                        if (type == "XXXX")
                        {
                            dataReader.ReadUInt16();
                            size = dataReader.ReadUInt32();
                            type = ReadRecName(dataReader);
                            dataReader.ReadUInt16();
                        }
                        else
                        {
                            size = define.HEDRRecSize == 2 ? dataReader.ReadUInt16() : dataReader.ReadUInt32();
                        }

                        var record = new SubRecord(this, type, dataReader, size);
                        SubRecords.Add(record);
                        amountRead += (uint)record.Size2;
                    }
                }
            }

            if (amountRead > realSize)
            {
                Debug.Print(
                    " * ERROR: SUB-RECORD {0} DATA DOESN'T MATCH THE SIZE SPECIFIED IN THE HEADER: DATA-SIZE={1} REAL-SIZE={2} AMOUNT-READ={3}",
                    name, dataSize, realSize, amountRead);
                throw new TESParserException(
                    string.Format(
                        "Subrecord block did not match the size specified in the record header: ExpectedSize={0} ReadSize={1} DataSize={2}",
                        realSize, amountRead, dataSize));
            }

            descNameOverride = DefaultDescriptiveName;
            UpdateShortDescription();

            // br.BaseStream.Position+=Size;
        }
Example #18
0
 internal IEnumerable <Element> EnumerateElements(SubRecord sr)
 {
     return(this.EnumerateElements(sr, false));
 }