コード例 #1
0
ファイル: PKColor.cs プロジェクト: Redth/PassKitSharp
        public static PKColor Parse(string rgbString)
        {
            var color = new PKColor();

            rgbString = rgbString.Trim().Replace(" ", "");

            rgbString = Regex.Replace(rgbString, "rgb\\s+?\\(", "", RegexOptions.Singleline | RegexOptions.IgnoreCase);

            rgbString = rgbString.Replace(")", "");

            var match = Regex.Match(rgbString, "(?<red>[0-9]+),(?<green>[0-9]+),(?<blue>[0-9]+)", RegexOptions.Singleline | RegexOptions.IgnoreCase);

            if (match != null)
            {
                int red = 0;
                int green = 0;
                int blue = 0;

                if (match.Groups != null && match.Groups["red"] != null)
                    int.TryParse(match.Groups["red"].Value, out red);

                if (match.Groups != null && match.Groups["green"] != null)
                    int.TryParse(match.Groups["green"].Value, out green);

                if (match.Groups != null && match.Groups["blue"] != null)
                    int.TryParse(match.Groups["blue"].Value, out blue);

                color.Red = red;
                color.Green = green;
                color.Blue = blue;
            }

            return color;
        }
コード例 #2
0
ファイル: PKColor.cs プロジェクト: blueskaie/PdfMaker
        public static PKColor Parse(string rgbString)
        {
            var color = new PKColor();

            rgbString = rgbString.Trim().Replace(" ", "");

            rgbString = Regex.Replace(rgbString, "rgb\\s+?\\(", "", RegexOptions.Singleline | RegexOptions.IgnoreCase);

            rgbString = rgbString.Replace(")", "");

            var match = Regex.Match(rgbString, "(?<red>[0-9]+),(?<green>[0-9]+),(?<blue>[0-9]+)", RegexOptions.Singleline | RegexOptions.IgnoreCase);

            if (match != null)
            {
                int red   = 0;
                int green = 0;
                int blue  = 0;

                if (match.Groups != null && match.Groups["red"] != null)
                {
                    int.TryParse(match.Groups["red"].Value, out red);
                }

                if (match.Groups != null && match.Groups["green"] != null)
                {
                    int.TryParse(match.Groups["green"].Value, out green);
                }

                if (match.Groups != null && match.Groups["blue"] != null)
                {
                    int.TryParse(match.Groups["blue"].Value, out blue);
                }

                color.Red   = red;
                color.Green = green;
                color.Blue  = blue;
            }

            return(color);
        }
コード例 #3
0
        static void ParsePassJson(PassKit p, ZipEntry e, Dictionary <string, string> discoveredHashes)
        {
            JObject json = null;

            try
            {
                using (var ms = new MemoryStream())
                {
                    e.Extract(ms);

                    ms.Position = 0;

                    var sha1 = CalculateSHA1(ms.GetBuffer(), Encoding.UTF8);
                    if (!discoveredHashes.ContainsKey(e.FileName.ToLower()))
                    {
                        discoveredHashes.Add(e.FileName.ToLower(), sha1);
                    }

                    using (var sr = new StreamReader(ms))
                        json = JObject.Parse(sr.ReadToEnd());
                }
            }
            catch { }

            if (json["passTypeIdentifier"] == null)
            {
                throw new MissingFieldException("PassKit must include a passTypeIdentifier field!");
            }

            if (json["formatVersion"] == null)
            {
                throw new MissingFieldException("PassKit must include a formatVersion field!");
            }

            if (json["organizationName"] == null)
            {
                throw new MissingFieldException("PassKit must include a organizationName field!");
            }

            if (json["serialNumber"] == null)
            {
                throw new MissingFieldException("PassKit must include a serialNumber field!");
            }

            if (json["teamIdentifier"] == null)
            {
                throw new MissingFieldException("PassKit must include a teamIdentifier field!");
            }

            if (json["description"] == null)
            {
                throw new MissingFieldException("PassKit must include a description field!");
            }

            p.PassTypeIdentifier = json["passTypeIdentifier"].Value <string>();
            p.FormatVersion      = json["formatVersion"].Value <int>();
            p.OrganizationName   = json["organizationName"].Value <string>();
            p.SerialNumber       = json["serialNumber"].Value <string>();
            p.TeamIdentifier     = json["teamIdentifier"].Value <string>();
            p.Description        = json["description"].Value <string>();


            if (json["webServiceURL"] != null)
            {
                p.WebServiceURL = json["webServiceURL"].ToString();
            }

            if (json["authenticationToken"] != null)
            {
                p.AuthenticationToken = json["authenticationToken"].ToString();
            }

            if (json["suppressStripShine"] != null)
            {
                p.SuppressStripShine = json["suppressStripShine"].Value <bool>();
            }

            if (json["foregroundColor"] != null)
            {
                p.ForegroundColor = PKColor.Parse(json["foregroundColor"].ToString());
            }

            if (json["backgroundColor"] != null)
            {
                p.BackgroundColor = PKColor.Parse(json["backgroundColor"].ToString());
            }

            if (json["labelColor"] != null)
            {
                p.LabelColor = PKColor.Parse(json["labelColor"].ToString());
            }

            if (json["logoText"] != null)
            {
                p.LogoText = json["logoText"].Value <string>();
            }

            if (json["relevantDate"] != null)
            {
                p.RelevantDate = json["relevantDate"].Value <DateTime>();
            }

            if (json["associatedStoreIdentifiers"] != null)
            {
                if (p.AssociatedStoreIdentifiers == null)
                {
                    p.AssociatedStoreIdentifiers = new List <string>();
                }

                var idarr = (JArray)json["associatedStoreIdentifiers"];
                foreach (var ida in idarr)
                {
                    p.AssociatedStoreIdentifiers.Add(ida.ToString());
                }
            }

            if (json["locations"] != null && json["locations"] is JArray)
            {
                foreach (JObject loc in (JArray)json["locations"])
                {
                    var pkl = new PKLocation();

                    if (loc["latitude"] == null)
                    {
                        throw new MissingFieldException("PassKit Location must include a latitude field!");
                    }

                    if (loc["longitude"] == null)
                    {
                        throw new MissingFieldException("PassKit Location must include a longitude field!");
                    }

                    pkl.Latitude  = loc["latitude"].Value <double>();
                    pkl.Longitude = loc["longitude"].Value <double>();

                    if (loc["altitude"] != null)
                    {
                        pkl.Altitude = loc["altitude"].Value <double>();
                    }

                    if (loc["relevantText"] != null)
                    {
                        pkl.RelevantText = loc["relevantText"].Value <string>();
                    }

                    if (p.Locations == null)
                    {
                        p.Locations = new PKLocationList();
                    }

                    p.Locations.Add(pkl);
                }
            }

            if (json["barcode"] != null)
            {
                var bc = json["barcode"] as JObject;

                if (p.Barcode == null)
                {
                    p.Barcode = new PKBarcode();
                }

                if (bc["message"] == null)
                {
                    throw new MissingFieldException("PassKit Barcode must include a message field!");
                }

                if (bc["format"] == null)
                {
                    throw new MissingFieldException("PassKit Barcode must include a format field!");
                }

                if (bc["messageEncoding"] == null)
                {
                    throw new MissingFieldException("PassKit Barcode must include a messageEncoding field!");
                }

                if (bc["altText"] != null)
                {
                    p.Barcode.AltText = bc["altText"].ToString();
                }

                p.Barcode.Message         = bc["message"].ToString();
                p.Barcode.MessageEncoding = bc["messageEncoding"].ToString();
                p.Barcode.Format          = (PKBarcodeFormat)Enum.Parse(typeof(PKBarcodeFormat), bc["format"].ToString());
            }

            if (json["eventTicket"] != null)
            {
                p.PassType = PKPassType.EventTicket;
                ParsePassSet(p, json["eventTicket"] as JObject);
            }

            if (json["boardingPass"] != null)
            {
                p.PassType = PKPassType.BoardingPass;
                ParsePassSet(p, json["boardingPass"] as JObject);
            }

            if (json["coupon"] != null)
            {
                p.PassType = PKPassType.Coupon;
                ParsePassSet(p, json["coupon"] as JObject);
            }

            if (json["generic"] != null)
            {
                p.PassType = PKPassType.Generic;
                ParsePassSet(p, json["generic"] as JObject);
            }

            if (json["storeCard"] != null)
            {
                p.PassType = PKPassType.StoreCard;
                ParsePassSet(p, json["storeCard"] as JObject);
            }
        }