Exemplo n.º 1
0
 public GroupEditor(GroupRecord gr)
 {
     this.gr = gr;
     this.InitializeComponent();
     Icon = Resources.fosnip;
     this.cmbGroupType.ContextMenu = new ContextMenu();
     this.cmbGroupType.SelectedIndex = (int)gr.groupType;
     this.tbRecType.Text = gr.ContentsType;
     byte[] data = gr.GetData();
     this.tbX.Text = TypeConverter.h2ss(data[2], data[3]).ToString();
     this.tbY.Text = TypeConverter.h2ss(data[0], data[1]).ToString();
     this.tbBlock.Text = TypeConverter.h2i(data[0], data[1], data[2], data[3]).ToString();
     this.tbParent.Text = TypeConverter.h2i(data[0], data[1], data[2], data[3]).ToString("X8");
     this.tbDateStamp.Text = gr.dateStamp.ToString("X8");
     this.tbFlags.Text = gr.flags.ToString("X8");
 }
Exemplo n.º 2
0
        /// <summary>
        /// Actually perform the copy
        /// </summary>
        /// <param name="src"></param>
        /// <returns></returns>
        public int CopyRecordsTo(BaseRecord[] src)
        {
            int count = 0;

            PrepMasters(src);

            var dstRec = this.Clone(src.Where(x => !LooseGroups.Contains(x.Name)), true).ToArray();
            if (dstRec.All(x => x is Record))
            {
                // put records into appropriate groups
                var groups = plugin.Records.OfType<GroupRecord>().ToList();
                var lookup = dstRec.GroupBy(r => r.Name)
                    .Select(g => new { key = g.Key, value = g.ToArray() })
                    .ToLookup(k => k.key, v => v.value);
                foreach (var kvp in lookup)
                {
                    if (LooseGroups.Contains(kvp.Key))
                    {
                        plugin.AddRecords(dstRec);
                    }
                    else
                    {
                        var gr = groups.FirstOrDefault(x => x.ContentsType == kvp.Key);
                        if (gr == null)
                        {
                            gr = new GroupRecord(kvp.Key);
                            plugin.AddRecord(gr);
                        }

                        foreach (var list in kvp)
                        {
                            gr.AddRecords(list);
                        }
                    }
                }
            }
            else
            {
                plugin.AddRecords(dstRec);
            }

            // handle loose groups by creating copy of parent groups
            foreach (var srcRec in src.Where(x => LooseGroups.Contains(x.Name)))
            {
                var dstnodes = new Stack<BaseRecord>();

                dstnodes.Push(this.Clone(srcRec, true));
                for (var n = srcRec.Parent; n is GroupRecord; n = n.Parent)
                {
                    dstnodes.Push(n.Clone(recursive: false));
                }

                var par = plugin as IGroupRecord;
                foreach (var baseRecord in dstnodes)
                {
                    if (par == null)
                    {
                        break;
                    }

                    if (baseRecord is GroupRecord)
                    {
                        var gr = baseRecord as GroupRecord;
                        var pargr = par.Records.OfType<GroupRecord>().FirstOrDefault(x => x.IsEquivalent(gr));
                        if (pargr != null)
                        {
                            par = pargr;
                            continue;
                        }
                    }

                    par.AddRecord(baseRecord);
                    par = baseRecord as IGroupRecord;
                }

                count += dstnodes.Count;
            }
            return count;
        }
Exemplo n.º 3
0
        public static void SanitizePlugin(Plugin plugin)
        {
            // performance update to prevent lists from updating currently selected record
            bool oldHoldUpdates = BaseRecord.HoldUpdates;
            try
            {
                BaseRecord.HoldUpdates = true;
                if (plugin == null)
                {
                    throw new ApplicationException("Cannot select plugin");
                }

                var hdr = plugin.Records.OfType<Rec>().FirstOrDefault(x => x.Name .StartsWith("TES"));
                if (hdr == null)
                {
                    throw new ApplicationException(Resources.PluginLacksAValidTes4RecordCannotContinue);
                }

                var toParse = new Queue<BaseRecord>(plugin.Records.OfType<BaseRecord>().Where(x => !x.Equals(hdr)));
                plugin.Clear();
                plugin.AddRecord(hdr);

                var groups = new Dictionary<string, GroupRecord>();

                foreach (string s in SanitizeOrder)
                {
                    var gr = new GroupRecord(s);
                    plugin.AddRecord(gr);
                    groups[s] = gr;
                }

                bool looseGroupsWarning = false;
                bool unknownRecordsWarning = false;
                while (toParse.Count > 0)
                {
                    var r = toParse.Dequeue();
                    if (r is GroupRecord)
                    {
                        var gr = (GroupRecord)r;
                        if (gr.ContentsType == "CELL" || gr.ContentsType == "WRLD" || gr.ContentsType == "DIAL")
                        {
                            var gr2 = groups[gr.ContentsType];
                            foreach (BaseRecord r2 in gr.Records)
                            {
                                gr2.AddRecord(r2);
                            }

                            gr.Clear();
                        }
                        else
                        {
                            foreach (BaseRecord r2 in gr.Records)
                            {
                                toParse.Enqueue(r2);
                            }

                            gr.Clear();
                        }
                    }
                    else if (r is Record)
                    {
                        var r2 = (Record)r;
                        if (LooseGroups.Contains(r2.Name))
                        {
                            looseGroupsWarning = true;
                            plugin.AddRecord(r2);
                        }
                        else
                        {
                            if (groups.ContainsKey(r2.Name))
                            {
                                groups[r2.Name].AddRecord(r2);
                            }
                            else
                            {
                                unknownRecordsWarning = true;
                                plugin.AddRecord(r2);
                            }
                        }
                    }
                }

                foreach (GroupRecord gr2 in groups.Values)
                {
                    if (gr2.Records.Count == 0)
                    {
                        plugin.DeleteRecord(gr2);
                    }
                }

                if (looseGroupsWarning)
                {
                    Alerts.Show(Resources.CannotSanitizeLooseGroups);
                }

                if (unknownRecordsWarning)
                {
                    Alerts.Show(Resources.CannotSanitizeUnknownRecords);
                }

                plugin.InvalidateCache();

                plugin.UpdateRecordCount();

                /* int reccount = -1 + plugin.Records.Cast<Rec>().Sum(r => sanitizeCountRecords(r));
                var tes4 = plugin.Records.OfType<Record>().FirstOrDefault(x => x.Name .StartsWith("TES"));
                if (tes4 != null)
                {
                    if (tes4.SubRecords.Count > 0 && tes4.SubRecords[0].Name == "HEDR" && tes4.SubRecords[0].Size >= 8)
                    {
                        byte[] data = tes4.SubRecords[0].GetData();
                        byte[] reccountbytes = TypeConverter.si2h(reccount);
                        for (int i = 0; i < 4; i++) data[4 + i] = reccountbytes[i];
                        tes4.SubRecords[0].SetData(data);
                    }
                } */
            }
            finally
            {
                BaseRecord.HoldUpdates = oldHoldUpdates;
            }
        }
Exemplo n.º 4
0
        internal GroupRecord(uint Size, BinaryReader br, FalloutSnip.Domain.Data.DomainDefinition define, Func<string, bool> recFilter, bool filterAll)
        {
            Name = "GRUP";
            this.data = br.ReadBytes(4);
            this.groupType = br.ReadUInt32();
            this.dateStamp = br.ReadUInt32();
            string contentType = this.groupType == 0 ? Encoding.Instance.GetString(this.data) : string.Empty;
            if (define.RecSize >= 16)
            {
                this.flags = br.ReadUInt32();
            }

            uint amountRead = 0;
            while (amountRead < Size - (define.RecSize+8))
            {
                string s = ReadRecName(br);
                uint recsize = br.ReadUInt32();
                if (s == "GRUP")
                {
                    try
                    {
                        bool skip = filterAll || (recFilter != null && !recFilter(contentType));
                        var gr = new GroupRecord(recsize, br, define, recFilter, skip);
                        if (!filterAll)
                        {
                            this.AddRecord(gr);
                        }
                    }
                    catch (Exception e)
                    {
                        Alerts.Show(e.Message);
                    }
                    finally
                    {
                        amountRead += recsize;
                    }
                }
                else
                {
                    bool skip = filterAll || (recFilter != null && !recFilter(contentType));
                    if (skip)
                    {
                        long size = recsize + define.RecSize;

                        // if ((br.ReadUInt32() & 0x00040000) > 0) size += 4;
                        br.BaseStream.Position += size; // just read past the data
                        amountRead += (uint)(recsize + (define.RecSize+8));
                    }
                    else
                    {
                        try
                        {
                            var r = new Record(s, recsize, br, define);
                            this.AddRecord(r);
                        }
                        catch (Exception e)
                        {
                            Alerts.Show(e.Message);
                        }
                        finally
                        {
                            amountRead += (uint)(recsize + (define.RecSize+8));
                        }
                    }
                }
            }

            this.UpdateShortDescription();
            if (amountRead != (Size - (define.RecSize+8)))
            {
                throw new TESParserException(
                    string.Format("Record block did not match the size specified in the group header! Header Size={0:D} Group Size={1:D}", Size - (define.RecSize+8), amountRead));
            }
        }
Exemplo n.º 5
0
 public bool IsEquivalent(GroupRecord other)
 {
     return this.GroupType == other.GroupType && ByteArrayCompare(this.GetReadonlyData(), other.GetReadonlyData());
 }
Exemplo n.º 6
0
        private GroupRecord(GroupRecord gr, bool recursive)
        {
            Name = "GRUP";
            this.data = (byte[])gr.data.Clone();
            this.groupType = gr.groupType;
            this.dateStamp = gr.dateStamp;
            this.flags = gr.flags;
            if (recursive)
            {
                this.records = new List<Rec>(gr.records.Count);
                for (int i = 0; i < gr.records.Count; i++)
                {
                    this.AddRecord(gr.records[i].Clone());
                }
            }

            Name = gr.Name;
            this.UpdateShortDescription();
        }
Exemplo n.º 7
0
 public static DialogResult Display(GroupRecord gr)
 {
     var ge = new GroupEditor(gr);
     return ge.ShowDialog();
 }