/// <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); }
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 == "TES4"); 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) { MessageBox.Show(Resources.CannotSanitizeLooseGroups, Resources.WarningText); } if (unknownRecordsWarning) { MessageBox.Show(Resources.CannotSanitizeUnknownRecords, Resources.WarningText); } 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 == "TES4"); * 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; } }
private void newToolStripMenuItem_Click(object sender, EventArgs e) { if (!ValidateMakeChange()) return; Plugin p = new Plugin(); Record r = new Record(); r.Name = "TES4"; SubRecord sr = new SubRecord(); sr.Name = "HEDR"; sr.SetData(new byte[] { 0xD7, 0xA3, 0x70, 0x3F, 0xFA, 0x56, 0x0C, 0x00, 0x19, 0xEA, 0x07, 0xFF }); r.AddRecord(sr); sr = new SubRecord(); sr.Name = "CNAM"; sr.SetData(System.Text.Encoding.ASCII.GetBytes("Default\0")); r.AddRecord(sr); p.AddRecord(r); TreeNode tn = new TreeNode(p.Name); tn.Tag = p; TreeNode tn2 = new TreeNode(r.DescriptiveName); tn2.Tag = r; tn.Nodes.Add(tn2); PluginTree.Nodes.Add(tn); UpdateStringEditor(); }