private void addMasterToolStripMenuItem_Click(object sender, EventArgs e) { using (var amfNewMaster = new AddMasterForm()) { if (amfNewMaster.ShowDialog(this) == DialogResult.OK) { Plugin plugin = GetPluginFromNode(PluginTree.SelectedRecord); if (plugin == null) { MainView.PostStatusWarning(Resources.NoPluginSelectedCannotContinue); return; } try { if (plugin.AddMaster(amfNewMaster.MasterName)) { PluginList.FixMasters(); RebuildSelection(); } } catch (ApplicationException ex) { MessageBox.Show(this, ex.Message, Resources.Missing_Record, MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }
private void contextMenuRecordAddMaster_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e) { if (e.ClickedItem.Tag == null) { return; } try { var nodes = e.ClickedItem.Tag as object[]; var src = nodes[0] as Plugin; var dst = nodes[1] as Plugin; if (src != null && dst != null) { if (src.AddMaster(dst.Name)) { PluginList.FixMasters(); } } } catch { } }
/// <summary> /// Search through records and determine what masters to add prior to replacement /// </summary> /// <param name="records"></param> void PrepMasters(IEnumerable <BaseRecord> records) { // ensure masters are added bool changed = false; foreach (var key in plugin.GetMasters()) { this.masters.Add(key, key); } var recordList = records.SelectMany(x => x.Enumerate()).OfType <Record>().ToList(); // use 0xFF as temporary master placeholder uint newid = (plugin.GetNewFormID(increment: false) & 0x00FFFFFF) | 0xFF000000; // build list of records getting new ids. So we know if need to masters for references if (!Override) { foreach (var sn in recordList.Where(rec => !NoNewCopyTypes.Contains(rec.Name))) // skip loose references { var p = sn.GetPlugin(); PluginInfo info; if (!pluginMap.TryGetValue(p, out info)) { pluginMap.Add(p, info = new PluginInfo() { Masters = p.GetMasters() }); } info.References[sn.FormID] = newid++; // not an override } } foreach (var rec in recordList) { var p = rec.GetPlugin(); PluginInfo info; if (!pluginMap.TryGetValue(p, out info)) { pluginMap.Add(p, info = new PluginInfo() { Masters = p.GetMasters() }); } // now handle child references rec.MatchRecordStructureToRecord(); var ids = rec.SubRecords.SelectMany(sr => sr.EnumerateElements()) .Where(elem => elem.Structure != null && elem.Structure.type == ElementValueType.FormID) .Select(elem => TypeConverter.h2i(elem.Data)).Distinct().ToList(); ids.Add(rec.FormID); ids.Sort(); // processing in sort order should keep master orders slightly more sane foreach (var id in ids) { if (info.References.ContainsKey(id)) { continue; } var idx = id >> 24; var masterName = idx >= info.Masters.Length ? p.Name : info.Masters[idx]; if (!masters.ContainsKey(masterName)) { plugin.AddMaster(masterName); this.masters.Add(masterName, masterName); changed = true; } var newidx = this.masters.FindIndex(masterName); if (newidx < 0) { newidx = 0xFF; } info.References[id] = (id & 0x00FFFFFF) | (uint)(newidx << 24); } } // fix up the new references after all masters have been added var masterId = (uint)(this.masters.Count << 24); foreach (var info in this.pluginMap.Values) { foreach ( var kvp in info.References.Where(kvp => ((kvp.Value & 0xFF000000) == 0xFF000000)).ToArray()) { info.References[kvp.Key] = (kvp.Value & 0x00FFFFFF) | masterId; } } plugin.UpdateNextFormID(newid & 0x00FFFFFF); if (changed) { PluginList.FixMasters(); } }