/// <summary>Write a boolean value into the mod data, or remove it if it matches the <paramref name="default"/>.</summary> /// <param name="data">The mod data dictionary.</param> /// <param name="key">The data key within the <paramref name="data"/>.</param> /// <param name="value">The value to write.</param> /// <param name="default">The default value if the field is missing or invalid. If the value matches the default, it won't be written to the data to avoid unneeded serialization and network sync.</param> public static void SetBool(this ModDataDictionary data, string key, bool value, bool @default = false) { if (value == @default) { data.Remove(key); } else { data[key] = value.ToString(CultureInfo.InvariantCulture); } }
/// <summary>Write an integer value into the mod data, or remove it if it matches the <paramref name="default"/>.</summary> /// <param name="data">The mod data dictionary.</param> /// <param name="key">The data key within the <paramref name="data"/>.</param> /// <param name="value">The value to write.</param> /// <param name="default">The default value if the field is missing or invalid. If the value matches the default, it won't be written to the data to avoid unneeded serialization and network sync.</param> /// <param name="min">The minimum value to consider valid, or <c>null</c> to allow any value.</param> public static void SetInt(this ModDataDictionary data, string key, int value, int @default = 0, int?min = null) { if (value == @default || value <= min) { data.Remove(key); } else { data[key] = value.ToString(CultureInfo.InvariantCulture); } }
/// <summary>Write a field to a mod data dictionary, or remove it if null.</summary> /// <param name="data">The mod data dictionary to update.</param> /// <param name="key">The dictionary key to write.</param> /// <param name="value">The value to write, or <c>null</c> to remove it.</param> public static ModDataDictionary WriteField(this ModDataDictionary data, string key, string value) { if (string.IsNullOrWhiteSpace(value)) { data.Remove(key); } else { data[key] = value; } return(data); }
/// <summary>Write a value into the mod data with custom serialization, or remove it if it serializes to null or an empty string.</summary> /// <typeparam name="T">The value type.</typeparam> /// <param name="data">The mod data dictionary.</param> /// <param name="key">The field key.</param> /// <param name="value">The value to save.</param> /// <param name="serialize">Serialize the value to its string representation.</param> public static void SetCustom <T>(this ModDataDictionary data, string key, T value, Func <T, string> serialize = null) { string serialized = serialize != null ? serialize(value) : value?.ToString(); if (string.IsNullOrWhiteSpace(serialized)) { data.Remove(key); } else { data[key] = serialized; } }
public static void SetFloat(this ModDataDictionary data, string key, float value, float @default = 0, float?min = null, float?max = null) { if (value < min) { value = min.Value; } if (value > max) { value = max.Value; } if (value == @default) { data.Remove(key); } else { data[key] = value.ToString(CultureInfo.InvariantCulture); } }