/// <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> /// <param name="edid">EditorID to drive the FormID assignment off any persistance systems</param> /// <returns>Duplicated and added record</returns> public static TMajor DuplicateInAsNewRecord <TMajor, TMajorGetter>(this IGroupCommon <TMajor> group, TMajorGetter source, string?edid) where TMajor : IMajorRecordInternal, TMajorGetter where TMajorGetter : IMajorRecordGetter, IBinaryItem { try { var newRec = group.AddNew(edid); var mask = OverrideMixIns.AddAsOverrideMasks.GetValueOrDefault(typeof(TMajor)); newRec.DeepCopyIn(source, mask as MajorRecord.TranslationMask); group.RecordCache.Set(newRec); return(newRec); } catch (Exception ex) { throw RecordException.Factory(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 IGroupCommon <TMajor> group, TMajorGetter major) where TMajor : class, IMajorRecordInternal, TMajorGetter where TMajorGetter : class, IMajorRecordGetter, IBinaryItem { try { if (group.RecordCache.TryGetValue(major.FormKey, out var existingMajor)) { return(existingMajor); } var mask = AddAsOverrideMasks.GetValueOrDefault(typeof(TMajor)); existingMajor = (major.DeepCopy(mask as MajorRecord.TranslationMask) as TMajor) !; group.RecordCache.Set(existingMajor); return(existingMajor); } catch (Exception ex) { throw RecordException.Factory(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 IGroupCommon <TMajor> group, IFormLink <TMajorGetter> link, ILinkCache cache, [MaybeNullWhen(false)] out TMajor rec) where TMajor : class, IMajorRecordInternal, TMajorGetter where TMajorGetter : class, IMajorRecordGetter, IBinaryItem { 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.Factory(ex, link.FormKey, edid: null); } }