public static T GetOrCreate <T>(this MechComponentDef target, Func <T> factory) where T : ICustom { var result = target.GetComponent <T>(); if ((result is ExtendedDetails.ExtendedDetails ed) && ed.Def != target.Description) { ed.Def = target.Description; } return(result ?? target.AddComponent(factory.Invoke())); }
internal static void AddTemplatedExtendedDetail( MechComponentDef componentDef, IEnumerable <string> elements, string elementTemplate, string descriptionTemplate, string identifier, UnitType unityType = UnitType.UNDEFINED) { var elementsText = string.Join("", elements.Where(x => x != null).Select(x => elementTemplate.Replace("{{element}}", x)).ToArray()); var text = descriptionTemplate.Replace("{{elements}}", elementsText); var extended = componentDef.GetComponent <ExtendedDetails>() ?? componentDef.AddComponent(new ExtendedDetails(componentDef)); var detail = new ExtendedDetail { UnitType = unityType, Index = -1, Text = text, Identifier = identifier }; extended.AddDetail(detail); }