/// <summary> /// Locates link winning target record in given Link Cache. /// </summary> /// <param name="link">Link to resolve</param> /// <param name="cache">Link Cache to resolve against</param> /// <returns>Located Major Record</returns> /// <exception cref="RecordException">If link was not succesful</exception> /// <typeparam name="TMajor">Major Record type of the FormLink</typeparam> public static TMajor Resolve <TMajor>(this IFormLinkGetter link, ILinkCache cache) where TMajor : class, IMajorRecordCommonGetter { if (link.TryResolve <TMajor>(cache, out var majorRecord)) { return(majorRecord); } throw RecordException.Create <TMajor>( message: "Could not resolve record", formKey: link.FormKeyNullable, modKey: link.FormKeyNullable?.ModKey, edid: null); }
/// <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> /// 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 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 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 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); } }