using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using AmazonInternal.ThirdParty; using UnityEngine; public class AmazonUtils { /// /// Compares two versions to see which is greater. /// /// Version to compare against second param /// Version to compare against first param /// -1 if the first version is smaller, 1 if the first version is greater, 0 if they are equal public static int CompareVersions (string a, string b) { var versionA = VersionStringToInts (a); var versionB = VersionStringToInts (b); for (var i = 0; i < Mathf.Max (versionA.Length, versionB.Length); i++) { if (VersionPiece (versionA, i) < VersionPiece (versionB, i)) return -1; if (VersionPiece (versionA, i) > VersionPiece (versionB, i)) return 1; } return 0; } private static int VersionPiece (IList versionInts, int pieceIndex) { return pieceIndex < versionInts.Count ? versionInts[pieceIndex] : 0; } private static int[] VersionStringToInts (string version) { if( version == null ) return new int[] { 0,0,0 }; int piece; string[] split = version.Split ('.'); return split.Select (v => int.TryParse (v, NumberStyles.Any, CultureInfo.InvariantCulture, out piece) ? piece : 0).ToArray (); } public static Uri UrlFromString (string url) { if (string.IsNullOrEmpty (url)) return null; try { return new Uri (url); } catch { Debug.LogError ("Invalid URL: " + url); return null; } } public static string EncodeArgs (params string[] args) { return Json.Serialize (args); } // Will return a non-null array of strings with at least 'min' non-null string values at the front. public static string[] DecodeArgs (string argsJson, int min) { var err = false; var args = Json.Deserialize (argsJson) as List; if (args == null) { Debug.LogError ("Invalid JSON data: " + argsJson); args = new List (); err = true; } if (args.Count < min) { if (!err) // Don't double up the error messages for invalid JSON Debug.LogError ("Missing one or more values: " + argsJson + " (expected " + min + ")"); while (args.Count < min) args.Add (""); } return args.Select (v => v.ToString ()).ToArray (); } public static string InvariantCultureToString (object obj) { return string.Format (CultureInfo.InvariantCulture, "{0}", obj); } }