/// <summary> /// Duplicates a given record (giving it a new FormID) adding it to the group and returning it. /// </summary> /// <param name="group">Group to add to</param> /// <param name="source">Source record to duplicate</param> /// <returns>Duplicated and added record</returns> public static TMajor DuplicateInAsNewRecord <TMajor, TMajorGetter, TSharedParent>(this IGroupCommon <TMajor> group, TMajorGetter source) where TMajor : class, IMajorRecordInternal, TSharedParent where TMajorGetter : TSharedParent where TSharedParent : IMajorRecordGetter { try { var newRec = (source.Duplicate(group.SourceMod.GetNextFormKey()) as TMajor) !; group.Add(newRec); return(newRec); } catch (Exception ex) { throw RecordException.Enrich <TMajor>(ex, source.FormKey, source.EditorID); } }
/// <summary> /// Takes in an existing record definition, and either returns the existing override definition /// from the Group, or copies the given record, inserts it, and then returns it as an override. /// </summary> /// <param name="group">Group to retrieve and/or insert from</param> /// <param name="major">Major record to query and potentially copy</param> /// <returns>Existing override record, or a copy of the given record that has already been inserted into the group</returns> public static TMajor GetOrAddAsOverride <TMajor, TMajorGetter>(this IGroup <TMajor> group, TMajorGetter major) where TMajor : class, IMajorRecordInternal, TMajorGetter where TMajorGetter : class, IMajorRecordGetter { try { if (group.RecordCache.TryGetValue(major.FormKey, out var existingMajor)) { return(existingMajor); } var mask = OverrideMaskRegistrations.Get <TMajor>(); existingMajor = (major.DeepCopy(mask as MajorRecord.TranslationMask) as TMajor) !; group.RecordCache.Set(existingMajor); return(existingMajor); } catch (Exception ex) { throw RecordException.Enrich <TMajor>(ex, major.FormKey, major.EditorID); } }
/// <summary> /// Takes in a FormLink, and either returns the existing override definition /// from the Group, or attempts to link and copy the given record, inserting it, and then returning it as an override. /// </summary> /// <param name="group">Group to retrieve and/or insert from</param> /// <param name="link">Link to query and add</param> /// <param name="cache">Cache to query link against</param> /// <param name="rec">Retrieved record if successful</param> /// <returns>True if a record was retrieved</returns> public static bool TryGetOrAddAsOverride <TMajor, TMajorGetter>(this IGroup <TMajor> group, IFormLinkGetter <TMajorGetter> link, ILinkCache cache, [MaybeNullWhen(false)] out TMajor rec) where TMajor : class, IMajorRecordInternal, TMajorGetter where TMajorGetter : class, IMajorRecordGetter { try { if (group.RecordCache.TryGetValue(link.FormKey, out rec)) { return(true); } if (!link.TryResolve <TMajorGetter>(cache, out var getter)) { rec = default; return(false); } rec = GetOrAddAsOverride(group, getter); return(true); } catch (Exception ex) { throw RecordException.Enrich <TMajor>(ex, link.FormKey, edid: null); } }