예제 #1
0
        /// <summary>
        /// Sets or update a value in the ini file.
        /// </summary>
        /// <typeparam name="T">The type of value to return.</typeparam>
        /// <param name="section">The section the value belongs to.</param>
        /// <param name="key">The key to the value.</param>
        /// <param name="val">The value.</param>
        public void SetValue <T>(string section, string key, T val)
        {
            object oVal = val;

            if (typeof(T) == typeof(string))
            {
                WriteValue(section, key, (string)oVal);
            }
            else if (typeof(T) == typeof(bool))
            {
                WriteValue(section, key, HQTools.ValToString((bool)oVal));
            }
            else if (typeof(T) == typeof(int))
            {
                WriteValue(section, key, HQTools.ValToString((int)oVal));
            }
            else if (typeof(T) == typeof(float))
            {
                WriteValue(section, key, HQTools.ValToString((float)oVal));
            }
            else if (typeof(T) == typeof(double))
            {
                WriteValue(section, key, HQTools.ValToString((double)oVal));
            }
            else
            {
                WriteValue(section, key, val.ToString());
            }
        }
        /// <summary>
        /// Saves the content of the log to a file and clears the log.
        /// </summary>
        /// <param name="fileSuffix">A suffix to append to the date/time in the filename.</param>
        /// <returns>True if file was written successfully, false if something went wrong.</returns>
        public bool SaveToFileAndClear(string fileSuffix = null)
        {
            DeleteOldLogFiles();

            if (!HQTools.CreateDirectoryIfMissing(HQTools.PATH_LOGS))
            {
                Clear(); return(false);
            }

            string filePath = HQTools.PATH_LOGS + string.Format("{0:yyyy-MM-dd (hh-mm-ss.fff)}", DateTime.Now);

            if (!string.IsNullOrEmpty(fileSuffix))
            {
                filePath += " " + fileSuffix;
            }
            filePath += ".txt";

            try
            {
                File.WriteAllLines(filePath, LogLines.ToArray(), Encoding.UTF8);
                Clear();
                return(true);
            }
            catch (Exception) // Failed to save log file (no write access to the Logs directory?)
            {
                Clear();
                return(false);
            }
        }
예제 #3
0
        /// <summary>
        /// Gets a value from the ini file.
        /// </summary>
        /// <typeparam name="T">The type of value to return.</typeparam>
        /// <param name="section">The section the value belongs to.</param>
        /// <param name="key">The key to the value.</param>
        /// <param name="defaultValue">The default value to return if no value was found. Optional.</param>
        /// <returns>The value, or defaultValue if the value doesn't exist.</returns>
        public T GetValue <T>(string section, string key, T defaultValue = default(T))
        {
            if (!ValueExists(section, key))
            {
                if ((defaultValue == null) && (typeof(T) == typeof(string)))
                {
                    return((T)Convert.ChangeType("", typeof(T)));
                }
                return(defaultValue);
            }

            object val = ReadValue(section, key) ?? "";

            try
            {
                if (typeof(T) == typeof(string))
                {
                    val = val.ToString();
                }
                else if (typeof(T) == typeof(bool))
                {
                    val = HQTools.StringToBool((string)val);
                }
                else if (typeof(T) == typeof(int))
                {
                    val = HQTools.StringToInt((string)val);
                }
                else if (typeof(T) == typeof(float))
                {
                    val = HQTools.StringToFloat((string)val);
                }
                else if (typeof(T) == typeof(double))
                {
                    val = HQTools.StringToDouble((string)val);
                }
                else if (typeof(T) == typeof(Coordinates))
                {
                    val = new Coordinates(val.ToString());
                }
                else if (typeof(T) == typeof(MinMaxI))
                {
                    val = new MinMaxI(val.ToString());
                }
                else if (typeof(T) == typeof(MinMaxD))
                {
                    val = new MinMaxD(val.ToString());
                }
                else if (typeof(T).IsEnum)
                {
                    val = Enum.Parse(typeof(T), val.ToString(), true);
                }

                return((T)Convert.ChangeType(val, typeof(T)));
            }
            catch (Exception)
            {
                return(default(T));
            }
        }
예제 #4
0
        /// <summary>
        /// Constructor. Parses the X and Y coordinates from a string (format is "1.2345,6.7890")
        /// </summary>
        /// <param name="coordinatesString">The string containing the coordinates.</param>
        public Coordinates(string coordinatesString)
        {
            try
            {
                string[] xAndYStrings = coordinatesString.Split(',');

                X = HQTools.StringToDouble(xAndYStrings[0]);
                Y = HQTools.StringToDouble(xAndYStrings[1]);
            }
            catch (Exception)
            {
                X = 0; Y = 0;
            }
        }
예제 #5
0
        /// <summary>
        /// Constructor. Parses the min and max coordinates from a string (format is "1.2345,6.7890")
        /// </summary>
        /// <param name="minMaxString">The string containing the min and max value.</param>
        public MinMaxI(string minMaxString)
        {
            try
            {
                string[] minAndMaxString = minMaxString.Split(',');

                int val1 = HQTools.StringToInt(minAndMaxString[0]);
                int val2 = HQTools.StringToInt(minAndMaxString[1]);

                Min = Math.Min(val1, val2);
                Max = Math.Max(val1, val2);
            }
            catch (Exception)
            {
                Min = 0; Max = 0;
            }
        }
예제 #6
0
        /// <summary>
        /// Converts a string array to an array of type T.
        /// </summary>
        /// <typeparam name="T">The type of the source array.</typeparam>
        /// <param name="sourceArray">The source string array.</param>
        /// <returns>The converted array</returns>
        private T[] ConvertArray <T>(string[] sourceArray)
        {
            try
            {
                T[] arr = new T[sourceArray.Length];

                for (int i = 0; i < sourceArray.Length; i++)
                {
                    object o = default(T);

                    if (typeof(T) == typeof(bool))
                    {
                        o = HQTools.StringToBool(sourceArray[i]);
                    }
                    else if (typeof(T) == typeof(int))
                    {
                        o = HQTools.StringToInt(sourceArray[i]);
                    }
                    else if (typeof(T) == typeof(double))
                    {
                        o = HQTools.StringToDouble(sourceArray[i]);
                    }
                    else if (typeof(T) == typeof(float))
                    {
                        o = HQTools.StringToFloat(sourceArray[i]);
                    }
                    else if (typeof(T).IsEnum)
                    {
                        o = Enum.Parse(typeof(T), sourceArray[i].ToString(), true);
                    }

                    arr[i] = (T)Convert.ChangeType(o, typeof(T));
                }

                return(arr);
            }
            catch (Exception)
            {
                return(new T[0]);
            }
        }
예제 #7
0
 /// <summary>
 /// Replaces all instance of "$KEY$" in a Lua script by value.
 /// </summary>
 /// <param name="lua">The Lua script.</param>
 /// <param name="key">The key to replace, without the dollar signs.</param>
 /// <param name="value">The value to replace the key with.</param>
 /// <param name="stringFormat">The string format string to use when converting the value to a string.</param>
 public static void ReplaceKey(ref string lua, string key, double value, string stringFormat = null)
 {
     ReplaceKey(ref lua, key, HQTools.ValToString(value, stringFormat));
 }
예제 #8
0
 /// <summary>
 /// Replaces all instance of "$KEY$" in a Lua script by value.
 /// </summary>
 /// <param name="lua">The Lua script.</param>
 /// <param name="key">The key to replace, without the dollar signs.</param>
 /// <param name="value">The value to replace the key with.</param>
 public static void ReplaceKey(ref string lua, string key, bool value)
 {
     ReplaceKey(ref lua, key, HQTools.ValToString(value).ToLowerInvariant());
 }
예제 #9
0
 /// <summary>
 /// Returns the MinMax value as a string.
 /// </summary>
 /// <param name="stringFormat">A format string.</param>
 /// <returns>A string</returns>
 public string ToString(string stringFormat)
 { return HQTools.ValToString(Min, stringFormat) + "," + HQTools.ValToString(Max, stringFormat); }
예제 #10
0
 /// <summary>
 /// Returns a random value between Min and Max.
 /// </summary>
 /// <returns>A random value.</returns>
 public int GetValue() { return HQTools.RandomInt(Min, Max + 1); }
예제 #11
0
 public static Coordinates CreateRandomInaccuracy(MinMaxD minMax)
 {
     return(FromAngleInDegrees(HQTools.RandomDouble(360.0)) * minMax.GetValue());
 }
예제 #12
0
 public static Coordinates Lerp(Coordinates pt1, Coordinates pt2, double value)
 {
     return(new Coordinates(HQTools.Lerp(pt1.X, pt2.X, value), HQTools.Lerp(pt1.Y, pt2.Y, value)));
 }
예제 #13
0
 public string ToString(string format)
 {
     return(HQTools.ValToString(X, format) + "," + HQTools.ValToString(Y, format));
 }
예제 #14
0
 public override string ToString()
 {
     return(HQTools.ValToString(X) + "," + HQTools.ValToString(Y));
 }
예제 #15
0
 /// <summary>
 /// Returns a random value between Min and Max.
 /// </summary>
 /// <returns>A random value.</returns>
 public double GetValue()
 {
     return(HQTools.RandomDouble(Min, Max));
 }