Ejemplo n.º 1
0
        public static async Task <List <ScannedEmulator> > SearchForEmulators(DirectoryInfoBase path, List <EmulatorDefinition> definitions, CancellationTokenSource cancelToken = null)
        {
            return(await Task.Run(() =>
            {
                logger.Info($"Looking for emulators in {path}, using {definitions.Count} definitions.");
                var emulators = new Dictionary <EmulatorDefinition, List <ScannedEmulatorProfile> >();

                var fileEnumerator = new SafeFileEnumerator(path, "*.exe", SearchOption.AllDirectories);
                foreach (var file in fileEnumerator)
                {
                    if (cancelToken?.IsCancellationRequested == true)
                    {
                        return null;
                    }

                    if (file.Attributes.HasFlag(FileAttributes.Directory))
                    {
                        continue;
                    }

                    foreach (var definition in definitions)
                    {
                        foreach (var defProfile in definition.Profiles)
                        {
                            var reqMet = true;
                            var folder = Path.GetDirectoryName(file.FullName);
                            var regex = new Regex(defProfile.ExecutableLookup, RegexOptions.IgnoreCase);
                            if (regex.IsMatch(file.Name))
                            {
                                if (defProfile.RequiredFiles?.Any() == true)
                                {
                                    foreach (var reqFile in defProfile.RequiredFiles)
                                    {
                                        if (!File.Exists(Path.Combine(folder, reqFile)))
                                        {
                                            reqMet = false;
                                            break;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                reqMet = false;
                            }

                            if (reqMet)
                            {
                                var emuProfile = new ScannedEmulatorProfile()
                                {
                                    Name = defProfile.Name,
                                    Arguments = defProfile.DefaultArguments,
                                    Executable = file.FullName,
                                    WorkingDirectory = folder,
                                    ImageExtensions = defProfile.ImageExtensions,
                                    ProfileDefinition = defProfile
                                };

                                if (!emulators.ContainsKey(definition))
                                {
                                    emulators.Add(definition, new List <ScannedEmulatorProfile>());
                                }

                                emulators[definition].Add(emuProfile);
                            }
                        }
                    }
                }

                var result = new List <ScannedEmulator>();
                foreach (var key in emulators.Keys)
                {
                    result.Add(new ScannedEmulator(key.Name, emulators[key]));
                }

                return result;
            }));
        }
Ejemplo n.º 2
0
        public static List <ScannedEmulator> SearchForEmulators(DirectoryInfoBase path, List <EmulatorDefinition> definitions)
        {
            var watch = new System.Diagnostics.Stopwatch();

            watch.Start();

            logger.Info($"Looking for emulators in {path}, using {definitions.Count} definitions.");
            var emulators = new Dictionary <EmulatorDefinition, List <ScannedEmulatorProfile> >();

            var fileEnumerator = new SafeFileEnumerator(path, "*.exe", SearchOption.AllDirectories);

            foreach (var file in fileEnumerator)
            {
                if (file.Attributes.HasFlag(FileAttributes.Directory))
                {
                    continue;
                }

                foreach (var definition in definitions)
                {
                    foreach (var defProfile in definition.Profiles)
                    {
                        var reqMet = true;
                        var folder = Path.GetDirectoryName(file.FullName);
                        var regex  = new Regex(defProfile.ExecutableLookup, RegexOptions.IgnoreCase);
                        if (regex.IsMatch(file.Name))
                        {
                            if (defProfile.RequiredFiles?.Any() == true)
                            {
                                foreach (var reqFile in defProfile.RequiredFiles)
                                {
                                    if (!File.Exists(Path.Combine(folder, reqFile)))
                                    {
                                        reqMet = false;
                                        break;
                                    }
                                }
                            }
                        }
                        else
                        {
                            reqMet = false;
                        }

                        if (reqMet)
                        {
                            var emuProfile = new ScannedEmulatorProfile()
                            {
                                Name              = defProfile.Name,
                                Arguments         = defProfile.DefaultArguments,
                                Executable        = file.FullName,
                                WorkingDirectory  = folder,
                                ImageExtensions   = defProfile.ImageExtensions,
                                ProfileDefinition = defProfile
                            };

                            if (!emulators.ContainsKey(definition))
                            {
                                emulators.Add(definition, new List <ScannedEmulatorProfile>());
                            }

                            emulators[definition].Add(emuProfile);
                        }
                    }
                }
            }

            var result = new List <ScannedEmulator>();

            foreach (var key in emulators.Keys)
            {
                result.Add(new ScannedEmulator(key.Name, emulators[key]));
            }

            watch.Stop();
            Console.WriteLine(watch.ElapsedMilliseconds / 1000);

            return(result);
        }