Browse Source

Add TryParseEulerAngle utility

Checks if euler string, delimited by commas, contains 3 components after
splitting.
habeebweeb 3 years ago
parent
commit
d8c6505ad1
1 changed files with 20 additions and 0 deletions
  1. 20 0
      src/MeidoPhotoStudio.Converter/MultipleMaids/ConversionUtility.cs

+ 20 - 0
src/MeidoPhotoStudio.Converter/MultipleMaids/ConversionUtility.cs

@@ -16,5 +16,25 @@ namespace MeidoPhotoStudio.Converter.MultipleMaids
             var data = vector3.Split(',');
             var data = vector3.Split(',');
             return new(float.Parse(data[0]), float.Parse(data[1]), float.Parse(data[2]));
             return new(float.Parse(data[0]), float.Parse(data[1]), float.Parse(data[2]));
         }
         }
+
+        /// <summary>
+        /// Checks if the string has 3 euler angle components delimited by commas before parsing
+        /// </summary>
+        /// <param name="euler">Euler angle string in the form "x,y,z"</param>
+        /// <param name="result">Resulting angle as a <c>Quaternion</c></param>
+        /// <returns>Whether or not the euler string can be safely parsed</returns>
+        public static bool TryParseEulerAngle(string euler, out Quaternion result)
+        {
+            result = Quaternion.identity;
+
+            var data = euler.Split(',');
+
+            if (data.Length != 3) return false;
+
+            try { result = Quaternion.Euler(float.Parse(data[0]), float.Parse(data[1]), float.Parse(data[2])); }
+            catch { return false; }
+
+            return true;
+        }
     }
     }
 }
 }