Example #1
0
        /// <summary>
        /// Tries to parse a key value line.
        /// </summary>
        /// <param name="line">The line (e.g. '"editorversion" "400"').</param>
        /// <param name="key">The key that was found.</param>
        /// <param name="value">The value that was found.</param>
        /// <returns>True if successful else false.</returns>
        private bool TryParsekeyValue(string line, out string key, out object value)
        {
            key   = "";
            value = null;

            if (!line.Contains('"'))
            {
                return(false);
            }
            if (line.Count(c => c == '"') != 4)
            {
                return(false);                                // fixes 'ep1_c17_03.vmf' "rulescript" with newline in the value.
            }
            int idx = line.IndexOf('"', 1);

            key = line.Substring(1, idx - 1);
            string rawvalue = line.Substring(idx + 3, line.Length - idx - 4);

            if (rawvalue.Length == 0)
            {
                return(false);
            }

            string rawvalue_string = rawvalue;

            rawvalue = rawvalue.Replace("--", "-");                                         // fixes 'd2_coast_05.vmf' with "-8 --64 -64" double negative.
            rawvalue = System.Text.RegularExpressions.Regex.Replace(rawvalue, @"\s+", " "); // fixes 'd2_prison_07.vmf' having two spaces in a vector declaration.

            int   vi;
            float vf;

            // detect plane definition.
            if (rawvalue[0] == '(')
            {
                string[]   values = rawvalue.Replace("(", "").Replace(")", "").Split(' ');
                VmfVector3 p1     = new VmfVector3(float.Parse(values[0], CultureInfo.InvariantCulture), float.Parse(values[1], CultureInfo.InvariantCulture), float.Parse(values[2], CultureInfo.InvariantCulture));
                VmfVector3 p2     = new VmfVector3(float.Parse(values[3], CultureInfo.InvariantCulture), float.Parse(values[4], CultureInfo.InvariantCulture), float.Parse(values[5], CultureInfo.InvariantCulture));
                VmfVector3 p3     = new VmfVector3(float.Parse(values[6], CultureInfo.InvariantCulture), float.Parse(values[7], CultureInfo.InvariantCulture), float.Parse(values[8], CultureInfo.InvariantCulture));
                value = new VmfPlane(p1, p2, p3);
                return(true);
            }
            // detect uv definition.
            else if (rawvalue[0] == '[' && rawvalue[rawvalue.Length - 1] != ']')
            {
                string[] values = rawvalue.Replace("[", "").Replace("]", "").Split(' ');
                value = new VmfAxis(new VmfVector3(float.Parse(values[0], CultureInfo.InvariantCulture), float.Parse(values[1], CultureInfo.InvariantCulture), float.Parse(values[2], CultureInfo.InvariantCulture)), float.Parse(values[3], CultureInfo.InvariantCulture), float.Parse(values[4], CultureInfo.InvariantCulture));
                return(true);
            }
            // detect vector3 definition.
            else if (rawvalue.Count(c => c == ' ') == 2 && rawvalue.All(c => " -.0123456789".Contains(c)))
            {
                string[] values = rawvalue.Split(' ');
                value = new VmfVector3(float.Parse(values[0], CultureInfo.InvariantCulture), float.Parse(values[1], CultureInfo.InvariantCulture), float.Parse(values[2], CultureInfo.InvariantCulture));
                return(true);
            }
            // detect vector4 definition.
            else if (rawvalue.Count(c => c == ' ') == 3 && rawvalue.All(c => " -.0123456789".Contains(c)))
            {
                string[] values = rawvalue.Split(' ');
                value = new VmfVector4(float.Parse(values[0], CultureInfo.InvariantCulture), float.Parse(values[1], CultureInfo.InvariantCulture), float.Parse(values[2], CultureInfo.InvariantCulture), float.Parse(values[3], CultureInfo.InvariantCulture));
                return(true);
            }
            // detect alternate vector3 definition.
            else if (rawvalue.Count(c => c == ' ') == 2 && rawvalue.All(c => " e-.0123456789[]".Contains(c))) // "e" exponent occurs here fixing 'ep2_outland_07_barn.vmf'.
            {
                string[] values = rawvalue.Replace("[", "").Replace("]", "").Split(' ');
                value = new VmfVector3(float.Parse(values[0], CultureInfo.InvariantCulture), float.Parse(values[1], CultureInfo.InvariantCulture), float.Parse(values[2], CultureInfo.InvariantCulture));
                return(true);
            }
            // detect floating point value.
            else if (rawvalue.Contains('.') && float.TryParse(rawvalue, out vf))
            {
                value = vf;
                return(true);
            }
            // detect integer value.
            else if (Int32.TryParse(rawvalue, out vi))
            {
                value = vi;
                return(true);
            }
            // probably a string value.
            else
            {
                value = rawvalue_string;
                return(true);
            }
        }
Example #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="VmfPlane"/> class.
 /// </summary>
 /// <param name="p1">The first point of the plane definition.</param>
 /// <param name="p2">The second point of the plane definition.</param>
 /// <param name="p3">The third point of the plane definition.</param>
 public VmfPlane(VmfVector3 p1, VmfVector3 p2, VmfVector3 p3)
 {
     P1 = p1;
     P2 = p2;
     P3 = p3;
 }
Example #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="VmfAxis"/> class.
 /// </summary>
 /// <param name="vector">The vector.</param>
 /// <param name="translation">The translation.</param>
 /// <param name="scale">The scale.</param>
 public VmfAxis(VmfVector3 vector, float translation, float scale)
 {
     Vector      = vector;
     Translation = translation;
     Scale       = scale;
 }