NQualityOfLife.Utils.Common.AddCustomFormatterForType - Niilo007/RimWorld-NQoL GitHub Wiki

AddCustomFormatterForType

Related:


C#

/// <summary>Add a custom <paramref name="formatter"/> for the given <see cref="Type"/> (<typeparamref name="T"/>)<para>This formatter is then used in the <see cref="FormatAsString{T}(IList{T}, FormatMode, DisplayModeFlags, Func{T, string}, Func{T, string})"/> method among others</para></summary>
/// <typeparam name="T"></typeparam>
/// <param name="formatter"><see cref="object"/> can be <see langword="null"/>, the <paramref name="formatter"/> must not return <see langword="null"/></param>
/// <exception cref="ArgumentNullException"><paramref name="formatter"/> is <see langword="null"/></exception>
/// <remarks>Note that if the <see cref="Type"/> (<typeparamref name="T"/>) implements <see cref="IFormatMode{T}"/>, That implementation supersedes the custom formatter</remarks>
/// <inheritdoc cref="ListExtensions.AddOrSet{K, V}(IDictionary{K, V}, K, V)"/>
[Authors("Niina", 22, 7, 2025)]
public static byte AddCustomFormatterForType<T>(Func<object?, DisplayModeFlags, string> formatter)
{

}

Usage

This method is used to add custom formatters to be used in various helper methods inside NQualityOfLife


Errors

NQoL [W] - Utils.Common:AddCustomFormatterForType('Func formatter == System.Func[System.Object,NQualityOfLife.Utils.Common+DisplayModeFlags,System.String]') - The custom formatter for: 'Line' already existed, so it was replaced!


Examples

Used In Example:

private UnOrderedList<Line> _drawnLines = [];

//...

NQualityOfLife.Utils.Common.AddCustomFormatterForType<System.Windows.Shapes.Line>(formatter: (object? o, NQualityOfLife.Utils.Common.DisplayModeFlags flags) => {
    if (o is System.Windows.Shapes.Line l)
    {
        return $"{nameof(System.Windows.Shapes.Line)}{{({l.X1.RoundToInt()}, {l.Y1.RoundToInt()})<->({l.X2.RoundToInt()}, {l.Y2.RoundToInt()})}}";
    }
    throw new NotImplementedException("Passed object was not of the expected type!");
});

//...

DebugText.Text = _drawnLines.AsParam(nameof(_drawnLines));

Output from _drawnLines.AsParam(nameof(_drawnLines)):

UnOrderedList(5){Line{(50, 50)<->(227, 227)}, Line{(227, 227)<->(398, 122)}, Line{(0, 0)<->(796, 244)}, Line{(796, 244)<->(796, 0)}, Line{(796, 0)<->(0, 0)}} _drawnLines

If we don't add the formatter using AddCustomFormatterForType, we get this output:

UnOrderedList(5){System.Windows.Shapes.Line, System.Windows.Shapes.Line, System.Windows.Shapes.Line, System.Windows.Shapes.Line, System.Windows.Shapes.Line} _drawnLines


⚠️ **GitHub.com Fallback** ⚠️