예제 #1
0
        static async Task <CompareResult> Compare(string source, string firstOperator = null)
        {
            return(await Task.Run(() =>
            {
                //Prepare the best match and all the weights
                CompareResult bestMatch = new CompareResult();
                WeightList weightList = new WeightList()
                {
                    Minimum = globalMinimum
                };
                weightList.Load(minimums);

                using (var sourceIcon = new MagickImage(source))
                {
                    //We should check the first operator first. This will be our baseline to beat.
                    if (!string.IsNullOrEmpty(firstOperator))
                    {
                        var firstopResult = CheckOperator(sourceIcon, firstOperator);
                        if (firstopResult.weight >= weightList.GetWeight(firstOperator))
                        {
                            bestMatch = firstopResult;
                        }
                    }

                    //Iterate over every file and check the shrink
                    foreach (string file in Directory.EnumerateFiles(cache, "*.shrink.png"))
                    {
                        //Get the name and skip if we did them first
                        string operatorName = Path.GetFileNameWithoutExtension(file).Replace(".shrink", "").ToUpperInvariant();
                        if (operatorName == firstOperator)
                        {
                            continue;
                        }

                        //Prepare their minimum
                        double minimum = weightList.GetWeight(operatorName);

                        //Get their result.
                        var result = CheckOperator(sourceIcon, operatorName);
                        if (result.weight > minimum && result.weight >= bestMatch.weight)
                        {
                            result.minimum = minimum;
                            bestMatch = result;
                        }
                    }
                }

                return bestMatch;
            }));
        }
예제 #2
0
        /// <summary>
        /// Recaches all the icons
        /// </summary>
        /// <returns></returns>
        static async Task RecacheAsync()
        {
            //Make sure cache exists
            if (!Directory.Exists(cache))
            {
                Directory.CreateDirectory(cache);
            }

            WeightList weightList = new WeightList();

            if (File.Exists(minimums))
            {
                weightList.Load(minimums);
            }

            //Get all the operators
            JObject response = await GetOperators();

            foreach (var x in response)
            {
                //Download their image and crop their images.
                string name       = x.Key;
                string url        = x.Value["img"].ToString();
                string path       = Path.Combine(cache, name + ".png");
                string pathShrink = Path.Combine(cache, name + ".shrink.png");

                Console.WriteLine("Processing " + name);

                //Download file
                await DownloadFileAsync(new Uri(url), path);

                //Shrink the file
                using (var source = new MagickImage(path))
                {
                    source.Crop(new MagickGeometry(19, 21, 89, 89));
                    source.Scale(22, 22);
                    source.RePage();
                    source.Write(pathShrink);
                }

                //Add to the cache
                if (!weightList.Contains(name))
                {
                    weightList.Add(name, 0.5);
                }
            }

            //Save the file
            weightList.Save(minimums);
        }