Beispiel #1
0
        public static string GetString(this ModDataDictionary d, string key, string defaultValue = null)
        {
            string result = defaultValue;

            d.TryGetValue(key, out result);
            return(result);
        }
 /// <summary>Read a container preference from a mod data dictionary.</summary>
 /// <param name="data">The mod data dictionary to read.</param>
 /// <param name="key">The dictionary key to read.</param>
 private static AutomateContainerPreference ReadPreferenceField(this ModDataDictionary data, string key)
 {
     data.TryGetValue(key, out string rawValue);
     return(rawValue switch
     {
         nameof(AutomateContainerPreference.Allow) => AutomateContainerPreference.Allow,
         nameof(AutomateContainerPreference.Prefer) => AutomateContainerPreference.Prefer,
         nameof(AutomateContainerPreference.Disable) => AutomateContainerPreference.Disable,
         _ => AutomateContainerPreference.Allow
     });
Beispiel #3
0
        /// <summary>Increment a long integer field from the mod data dictionary.</summary>
        /// <param name="data">The mod data dictionary to update.</param>
        /// <param name="key">The dictionary key to write.</param>
        /// <param name="amount">Amount to increment by.</param>
        public static ModDataDictionary IncrementField(this ModDataDictionary data, string key, long amount)
        {
            if (data.TryGetValue(key, out var rawValue))
            {
                var num = long.Parse(rawValue);
                data[key] = Math.Max(num + amount, 0).ToString();
            }

            return(data);
        }
Beispiel #4
0
        /// <summary>Increment a double-precision field from the mod data dictionary.</summary>
        /// <param name="data">The mod data dictionary to update.</param>
        /// <param name="key">The dictionary key to write.</param>
        /// <param name="amount">Amount to increment by.</param>
        public static ModDataDictionary IncrementField(this ModDataDictionary data, string key, double amount)
        {
            if (data.TryGetValue(key, out var rawValue))
            {
                var num = double.Parse(rawValue);
                data[key] = (num + amount).ToString(CultureInfo.InvariantCulture);
            }

            return(data);
        }
Beispiel #5
0
        public static bool GetBool(this ModDataDictionary d, string key, bool defaultValue = false)
        {
            string strVal = null;

            if (d.TryGetValue(key, out strVal))
            {
                return(strVal == "1" || strVal == "T");
            }
            return(defaultValue);
        }
Beispiel #6
0
        public static int GetInt(this ModDataDictionary d, string key, int defaultValue = 0)
        {
            int    result = defaultValue;
            string strVal;

            if (d.TryGetValue(key, out strVal))
            {
                int.TryParse(strVal, out result);
            }
            return(result);
        }
        /****
        ** Custom
        ****/
        /// <summary>Read a value from the mod data with custom parsing if it exists and can be parsed, else get the default value.</summary>
        /// <typeparam name="T">The value type.</typeparam>
        /// <param name="data">The mod data dictionary.</param>
        /// <param name="key">The data key within the <paramref name="data"/>.</param>
        /// <param name="parse">Parse the raw value.</param>
        /// <param name="default">The default value if the field is missing or invalid.</param>
        /// <param name="suppressError">Whether to return the default value if <paramref name="parse"/> throws an exception; else rethrow it.</param>
        public static T GetCustom <T>(this ModDataDictionary data, string key, Func <string, T> parse, T @default = default, bool suppressError = true)
        {
            if (!data.TryGetValue(key, out string raw))
            {
                return(@default);
            }

            try
            {
                return(parse(raw));
            }
            catch when(suppressError)
            {
                return(@default);
            }
        }
 /****
 ** Float
 ****/
 /// <summary>Read a float value from the mod data if it exists and is valid, else get the default value.</summary>
 /// <param name="data">The mod data dictionary.</param>
 /// <param name="key">The data key within the <paramref name="data"/>.</param>
 /// <param name="default">The default value if the field is missing or invalid.</param>
 /// <param name="min">The minimum value to consider valid, or <c>null</c> to allow any value.</param>
 public static float GetFloat(this ModDataDictionary data, string key, float @default = 0, float?min = null)
 {
     return(data.TryGetValue(key, out string raw) && float.TryParse(raw, out float value) && value >= min
         ? value
         : @default);
 }
 /*********
 ** Public methods
 *********/
 /****
 ** Bool
 ****/
 /// <summary>Read a boolean value from the mod data if it exists and is valid, else get the default value.</summary>
 /// <param name="data">The mod data dictionary.</param>
 /// <param name="key">The data key within the <paramref name="data"/>.</param>
 /// <param name="default">The default value if the field is missing or invalid.</param>
 public static bool GetBool(this ModDataDictionary data, string key, bool @default = false)
 {
     return(data.TryGetValue(key, out string raw) && bool.TryParse(raw, out bool value)
         ? value
         : @default);
 }
 /****
 ** Int
 ****/
 /// <summary>Read an integer value from the mod data if it exists and is valid, else get the default value.</summary>
 /// <param name="data">The mod data dictionary.</param>
 /// <param name="key">The data key within the <paramref name="data"/>.</param>
 /// <param name="default">The default value if the field is missing or invalid.</param>
 /// <param name="min">The minimum value to consider valid, or <c>null</c> to allow any value.</param>
 public static int GetInt(this ModDataDictionary data, string key, int @default = 0, int?min = null)
 {
     return(data.TryGetValue(key, out string raw) && int.TryParse(raw, out int value) && value >= min
         ? value
         : @default);
 }
 /// <summary>Read a field from the mod data dictionary.</summary>
 /// <param name="data">The mod data dictionary to read.</param>
 /// <param name="key">The dictionary key to read.</param>
 /// <param name="defaultValue">The default value to return if the data field isn't set.</param>
 public static string ReadField(this ModDataDictionary data, string key, string defaultValue = null)
 {
     return(data.TryGetValue(key, out string rawValue)
         ? rawValue
         : defaultValue);
 }
 /*********
 ** Public fields
 *********/
 /// <summary>Read a field from the mod data dictionary.</summary>
 /// <typeparam name="T">The field type.</typeparam>
 /// <param name="data">The mod data dictionary to read.</param>
 /// <param name="key">The dictionary key to read.</param>
 /// <param name="parse">Convert the raw string value into the expected type.</param>
 /// <param name="defaultValue">The default value to return if the data field isn't set.</param>
 public static T ReadField <T>(this ModDataDictionary data, string key, Func <string, T> parse, T defaultValue = default)
 {
     return(data.TryGetValue(key, out string rawValue)
         ? parse(rawValue)
         : defaultValue);
 }