protected override IEnumerable<Achievement> GetUnlockedAchievements(StatisAnalysisSession statisAnalysisSession, IEnumerable<Achievement> availableAchievements)
        {
            var unlockedAchievements = new ConcurrentBag<Achievement>();
            var tasks = new Task[availableAchievements.Count()];
            var i = 0;

            foreach (var uncompletedAchievement in availableAchievements)
            {
                var a = uncompletedAchievement;

                tasks[i++] = Task.Factory.StartNew(() =>
                {
                    /*   Technically we create a lot of objects all the time.
                     *   It's possible that these objects could have a lifespan longer than just a session.
                     *   However maintaining state is always a PITA */
                    var achievement = (StaticAnalysisAchievementBase)Activator.CreateInstance(a.AchievementType);

                    if (achievement.IsAchievementUnlocked(statisAnalysisSession))
                    {
                        a.CodeOrigin = achievement.AchievementCodeOrigin;
                        a.IsCompleted = true;
                        unlockedAchievements.Add(a);
                    }
                });
            }

            Task.WaitAll(tasks);

            return unlockedAchievements;
        }
        protected override IEnumerable <Achievement> GetUnlockedAchievements(StatisAnalysisSession statisAnalysisSession, IEnumerable <Achievement> availableAchievements)
        {
            var unlockedAchievements = new ConcurrentBag <Achievement>();
            var tasks = new Task[availableAchievements.Count()];
            var i     = 0;

            foreach (var uncompletedAchievement in availableAchievements)
            {
                var a = uncompletedAchievement;

                tasks[i++] = Task.Factory.StartNew(() =>
                {
                    /*   Technically we create a lot of objects all the time.
                     *   It's possible that these objects could have a lifespan longer than just a session.
                     *   However maintaining state is always a PITA */
                    var achievement = (StaticAnalysisAchievementBase)Activator.CreateInstance(a.AchievementType);

                    if (achievement.IsAchievementUnlocked(statisAnalysisSession))
                    {
                        a.CodeOrigin  = achievement.AchievementCodeOrigin;
                        a.IsCompleted = true;
                        unlockedAchievements.Add(a);
                    }
                });
            }

            Task.WaitAll(tasks);

            return(unlockedAchievements);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Detects the achievement.
        /// </summary>
        /// <param name="statisAnalysisSession">The detection session.</param>
        /// <returns><c>true</c> if the ChallengeRunner returned 'OK'; otherwise <c>false</c>.</returns>
        public override bool IsAchievementUnlocked(StatisAnalysisSession statisAnalysisSession)
        {
            if (string.IsNullOrEmpty(statisAnalysisSession.StaticAnalysisManifest.ActiveProjectOutputDirectory))
            {
                return false;
            }

            var dlls = new List<string>();

            // Some directory listing hackery
            dlls.AddRange(GetOutputFiles(statisAnalysisSession, "*.dll"));
            dlls.AddRange(GetOutputFiles(statisAnalysisSession, "*.exe"));

            // If the Strokes.Challenges.Student-dll isn't in the build output,
            // this project can with certainty be said to not be a challenge-solve attempt.
            if (!dlls.Any(dll => dll.Contains("Strokes.Challenges.dll")))
                return false;

            var processStartInfo = new ProcessStartInfo();
            processStartInfo.UseShellExecute = false;
            processStartInfo.WorkingDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            processStartInfo.FileName = Path.Combine(processStartInfo.WorkingDirectory, "Strokes.ChallengeRunner.exe");
            processStartInfo.CreateNoWindow = true;
            processStartInfo.RedirectStandardOutput = true;
            processStartInfo.RedirectStandardError = true;
            processStartInfo.Arguments = string.Join(" ",
                statisAnalysisSession.StaticAnalysisManifest.ActiveProjectOutputDirectory.Replace(" ", "*"), ChallengeRunner);

            var process = Process.Start(processStartInfo);

            var output = process.StandardOutput.ReadToEnd();
            var error = process.StandardError.ReadToEnd();

            return output == "OK";
        }
Ejemplo n.º 4
0
        private static IEnumerable <string> GetOutputFiles(StatisAnalysisSession statisAnalysisSession, string searchPattern)
        {
            var directory = statisAnalysisSession.StaticAnalysisManifest.ActiveProjectOutputDirectory;

            return(Directory.GetFiles(directory, searchPattern, SearchOption.AllDirectories)
                   .Where(file => file.IndexOf("vshost") < 0));
        }
Ejemplo n.º 5
0
 protected override AbstractAchievementVisitor CreateVisitor(StatisAnalysisSession statisAnalysisSession)
 {
     return new Visitor()
     {
         MethodToFind = methodName,
         Requirements = RequiredOverloads
     };
 }
Ejemplo n.º 6
0
        public override bool IsAchievementUnlocked(StatisAnalysisSession statisAnalysisSession)
        {
            // Return out if there are no files to check achievements in.
            if (!statisAnalysisSession.StaticAnalysisManifest.ChangedFiles.Any())
            {
                return(false);
            }

            // Obtain a session object and the codebase type declarations
            var nrefactorySession = statisAnalysisSession.GetSessionObjectOfType <NRefactorySession>();

            NRefactoryContext = new NRefactoryContext()
            {
                CodebaseDeclarations = nrefactorySession.GetCodebaseDeclarations(statisAnalysisSession.StaticAnalysisManifest),
                InvokedSystemTypes   = nrefactorySession.GetSystemInvocations(statisAnalysisSession.StaticAnalysisManifest)
            };

            // Have the concrete implementation create it's visitor
            var visitor = CreateVisitor(statisAnalysisSession);

            // Parse all files in the changed files collection for achievements
            foreach (var filename in statisAnalysisSession.StaticAnalysisManifest.ChangedFiles)
            {
                // Obtain a parser from the nrefactorySession.
                // This parser is shared context between all concrete achievement implementations.
                var compilationUnit = nrefactorySession.GetCompilationUnit(filename);

                // Pass concrete visitor into the AST created by the parser
                compilationUnit.AcceptVisitor(visitor, null);

                // Call OnParsingCompleted on the visitor to give it a last chance to unlock achievements.
                visitor.OnParsingCompleted();

                // Check if the visitor declared the concrete achievement as unlocked.
                if (visitor.IsAchievementUnlocked)
                {
                    AchievementCodeOrigin = visitor.CodeOrigin;
                    if (AchievementCodeOrigin != null)
                    {
                        AchievementCodeOrigin.FileName = filename;
                    }

                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 7
0
        public IEnumerable <Achievement> PerformStaticAnalysis(StaticAnalysisManifest staticAnalysisManifest, bool onlyUnlockable)
        {
            IEnumerable <Achievement> unlockedAchievements = Enumerable.Empty <Achievement>();

            using (var statisAnalysisSession = new StatisAnalysisSession(staticAnalysisManifest))
            {
                OnStaticAnalysisStarted(this, new EventArgs());
                var stopwatch = new Stopwatch();
                stopwatch.Start();

                var allAchievements = onlyUnlockable
                                    ? AchievementRepository.GetUnlockableAchievements()
                                    : AchievementRepository.GetAchievements();

                var availableStaticAnalysisAchievements = allAchievements.Where
                                                          (
                    a => typeof(StaticAnalysisAchievementBase).IsAssignableFrom(a.AchievementType)
                                                          ).ToList();

                unlockedAchievements = GetUnlockedAchievements
                                       (
                    statisAnalysisSession, availableStaticAnalysisAchievements
                                       ).ToList();

                stopwatch.Stop();
                OnStaticAnalysisCompleted(this, new StaticAnalysisEventArgs
                {
                    AchievementsTested  = availableStaticAnalysisAchievements.Count,
                    ElapsedMilliseconds = (int)stopwatch.ElapsedMilliseconds
                });
            }

            if (unlockedAchievements.Any())
            {
                foreach (var completedAchievement in unlockedAchievements)
                {
                    AchievementRepository.MarkAchievementAsCompleted(completedAchievement);
                }

                OnAchievementsUnlocked(this, new AchievementEventArgs
                {
                    UnlockedAchievements = unlockedAchievements
                });
            }

            return(unlockedAchievements);
        }
Ejemplo n.º 8
0
        public override bool IsAchievementUnlocked(StatisAnalysisSession statisAnalysisSession)
        {
            // Return out if there are no files to check achievements in.
            if (!statisAnalysisSession.StaticAnalysisManifest.ChangedFiles.Any())
            {
                return false;
            }

            // Obtain a session object and the codebase type declarations
            var nrefactorySession = statisAnalysisSession.GetSessionObjectOfType<NRefactorySession>();
            NRefactoryContext = new NRefactoryContext()
                                    {
                                        CodebaseDeclarations = nrefactorySession.GetCodebaseDeclarations(statisAnalysisSession.StaticAnalysisManifest),
                                        InvokedSystemTypes = nrefactorySession.GetSystemInvocations(statisAnalysisSession.StaticAnalysisManifest)
                                    };

            // Have the concrete implementation create it's visitor
            var visitor = CreateVisitor(statisAnalysisSession);

            // Parse all files in the changed files collection for achievements
            foreach (var filename in statisAnalysisSession.StaticAnalysisManifest.ChangedFiles)
            {
                // Obtain a parser from the nrefactorySession.
                // This parser is shared context between all concrete achievement implementations.
                var compilationUnit = nrefactorySession.GetCompilationUnit(filename);

                // Pass concrete visitor into the AST created by the parser
                compilationUnit.AcceptVisitor(visitor, null);

                // Call OnParsingCompleted on the visitor to give it a last chance to unlock achievements.
                visitor.OnParsingCompleted();

                // Check if the visitor declared the concrete achievement as unlocked.
                if (visitor.IsAchievementUnlocked)
                {
                    AchievementCodeOrigin = visitor.CodeOrigin;
                    if (AchievementCodeOrigin != null)
                    {
                        AchievementCodeOrigin.FileName = filename;
                    }

                    return true;
                }
            }

            return false;
        }
        public IEnumerable<Achievement> PerformStaticAnalysis(StaticAnalysisManifest staticAnalysisManifest, bool onlyUnlockable)
        {
            IEnumerable<Achievement> unlockedAchievements = Enumerable.Empty<Achievement>();

            using (var statisAnalysisSession = new StatisAnalysisSession(staticAnalysisManifest))
            {
                OnStaticAnalysisStarted(this, new EventArgs());
                var stopwatch = new Stopwatch();
                stopwatch.Start();

                var allAchievements = onlyUnlockable
                                    ? AchievementRepository.GetUnlockableAchievements()
                                    : AchievementRepository.GetAchievements();

                var availableStaticAnalysisAchievements = allAchievements.Where
                (
                    a => typeof(StaticAnalysisAchievementBase).IsAssignableFrom(a.AchievementType)
                ).ToList();

                unlockedAchievements = GetUnlockedAchievements
                (
                    statisAnalysisSession, availableStaticAnalysisAchievements
                ).ToList();

                stopwatch.Stop();
                OnStaticAnalysisCompleted(this, new StaticAnalysisEventArgs
                {
                    AchievementsTested = availableStaticAnalysisAchievements.Count,
                    ElapsedMilliseconds = (int)stopwatch.ElapsedMilliseconds
                });
            }

            if (unlockedAchievements.Any())
            {
                foreach (var completedAchievement in unlockedAchievements)
                {
                    AchievementRepository.MarkAchievementAsCompleted(completedAchievement);
                }

                OnAchievementsUnlocked(this, new AchievementEventArgs
                {
                    UnlockedAchievements = unlockedAchievements
                });
            }

            return unlockedAchievements;
        }
        protected override IEnumerable<Achievement> GetUnlockedAchievements(StatisAnalysisSession statisAnalysisSession, IEnumerable<Achievement> availableAchievements)
        {
            var unlockedAchievements = new List<Achievement>();
            foreach (var uncompletedAchievement in availableAchievements)
            {
                var a = uncompletedAchievement;

                var achievement = (StaticAnalysisAchievementBase)Activator.CreateInstance(a.AchievementType);

                if (achievement.IsAchievementUnlocked(statisAnalysisSession))
                {
                    a.CodeOrigin = achievement.AchievementCodeOrigin;
                    a.IsCompleted = true;
                    unlockedAchievements.Add(a);
                }
            }

            return unlockedAchievements;
        }
Ejemplo n.º 11
0
        protected virtual TestableChallengeResult TestChallenge(StatisAnalysisSession statisAnalysisSession)
        {
            if (string.IsNullOrEmpty(statisAnalysisSession.StaticAnalysisManifest.ActiveProjectOutputDirectory))
            {
                return(null);
            }

            var dlls = new List <string>();

            // Some directory listing hackery
            dlls.AddRange(GetOutputFiles(statisAnalysisSession, "*.dll"));
            dlls.AddRange(GetOutputFiles(statisAnalysisSession, "*.exe"));

            var challengeRunner = typeof(TRunner).FullName;

            var processStartInfo = new ProcessStartInfo();

            processStartInfo.UseShellExecute        = false;
            processStartInfo.WorkingDirectory       = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            processStartInfo.FileName               = Path.Combine(processStartInfo.WorkingDirectory, "Strokes.ChallengeRunner.exe");
            processStartInfo.CreateNoWindow         = true;
            processStartInfo.RedirectStandardOutput = true;
            processStartInfo.RedirectStandardError  = true;
            processStartInfo.Arguments              = string.Join(" ",
                                                                  statisAnalysisSession.StaticAnalysisManifest.ActiveProjectOutputDirectory.Replace(" ", "*"), challengeRunner
                                                                  );

            var process = Process.Start(processStartInfo);
            var error   = process.StandardError.ReadToEnd();

            // Create an XML Serializer so we can read a testable challenge result from the process' standard output.
            var serializer = new XmlSerializer(typeof(TestableChallengeResult));

            try
            {
                return((TestableChallengeResult)serializer.Deserialize(process.StandardOutput));
            }
            catch
            {
                return(null);
            }
        }
Ejemplo n.º 12
0
        protected override IEnumerable <Achievement> GetUnlockedAchievements(StatisAnalysisSession statisAnalysisSession, IEnumerable <Achievement> availableAchievements)
        {
            var unlockedAchievements = new List <Achievement>();

            foreach (var uncompletedAchievement in availableAchievements)
            {
                var a = uncompletedAchievement;

                var achievement = (StaticAnalysisAchievementBase)Activator.CreateInstance(a.AchievementType);

                if (achievement.IsAchievementUnlocked(statisAnalysisSession))
                {
                    a.CodeOrigin  = achievement.AchievementCodeOrigin;
                    a.IsCompleted = true;
                    unlockedAchievements.Add(a);
                }
            }

            return(unlockedAchievements);
        }
 public abstract bool IsAchievementUnlocked(StatisAnalysisSession statisAnalysisSession);
Ejemplo n.º 14
0
 protected override AbstractAchievementVisitor CreateVisitor(StatisAnalysisSession statisAnalysisSession)
 {
     return new Visitor();
 }
Ejemplo n.º 15
0
 protected override AbstractAchievementVisitor CreateVisitor(StatisAnalysisSession statisAnalysisSession)
 {
     return(new Visitor(() => TestChallenge(statisAnalysisSession)));
 }
Ejemplo n.º 16
0
 protected abstract AbstractAchievementVisitor CreateVisitor(StatisAnalysisSession statisAnalysisSession);
 protected abstract IEnumerable<Achievement> GetUnlockedAchievements(
     StatisAnalysisSession statisAnalysisSession,
     IEnumerable<Achievement> availableAchievements
 );
Ejemplo n.º 18
0
 protected override AbstractAchievementVisitor CreateVisitor(StatisAnalysisSession statisAnalysisSession)
 {
     return(new Visitor(NRefactoryContext, VerifyArgumentUsage, systemType, methodName));
 }
 protected override AbstractAchievementVisitor CreateVisitor(StatisAnalysisSession statisAnalysisSession)
 {
     return new Visitor(NRefactoryContext.CodebaseDeclarations);
 }
Ejemplo n.º 20
0
 protected override AbstractAchievementVisitor CreateVisitor(StatisAnalysisSession statisAnalysisSession)
 {
     return(new Visitor(NRefactoryContext.CodebaseDeclarations));
 }
Ejemplo n.º 21
0
 protected abstract AbstractAchievementVisitor CreateVisitor(StatisAnalysisSession statisAnalysisSession);
Ejemplo n.º 22
0
        /// <summary>
        /// Gets the output files for a given session, filtered by the given search pattern.
        /// </summary>
        /// <param name="statisAnalysisSession">The detection session.</param>
        /// <param name="searchPattern">The search pattern.</param>
        /// <returns>A list of file paths.</returns>
        private static IEnumerable<string> GetOutputFiles(StatisAnalysisSession statisAnalysisSession, string searchPattern)
        {
            var directory = statisAnalysisSession.StaticAnalysisManifest.ActiveProjectOutputDirectory;

            return Directory.GetFiles(directory, searchPattern, SearchOption.AllDirectories)
                            .Where(file => file.IndexOf("vshost") < 0);
        }
Ejemplo n.º 23
0
 protected abstract IEnumerable <Achievement> GetUnlockedAchievements
 (
     StatisAnalysisSession statisAnalysisSession,
     IEnumerable <Achievement> availableAchievements
 );
Ejemplo n.º 24
0
 protected override AbstractAchievementVisitor CreateVisitor(StatisAnalysisSession statisAnalysisSession)
 {
     return new Visitor(NRefactoryContext, VerifyArgumentUsage, systemType, methodName);
 }
Ejemplo n.º 25
0
 protected override AbstractAchievementVisitor CreateVisitor(StatisAnalysisSession statisAnalysisSession)
 {
     return(new Visitor());
 }
Ejemplo n.º 26
0
 public abstract bool IsAchievementUnlocked(StatisAnalysisSession statisAnalysisSession);