/// <summary> /// Detects the achievement. /// </summary> /// <param name="detectionSession">The detection session.</param> /// <returns><c>true</c> if the ChallengeRunner returned 'OK'; otherwise <c>false</c>.</returns> public override bool DetectAchievement(DetectionSession detectionSession) { if (string.IsNullOrEmpty(detectionSession.BuildInformation.ActiveProjectOutputDirectory)) { return false; } var dlls = new List<string>(); // Some directory listing hackery dlls.AddRange(GetOutputFiles(detectionSession, "*.dll")); dlls.AddRange(GetOutputFiles(detectionSession, "*.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(" ", detectionSession.BuildInformation.ActiveProjectOutputDirectory.Replace(" ", "*"), ChallengeRunner); var process = Process.Start(processStartInfo); var output = process.StandardOutput.ReadToEnd(); var error = process.StandardError.ReadToEnd(); return output == "OK"; }
/// <summary> /// Gets the output files for a given session, filtered by the given search pattern. /// </summary> /// <param name="detectionSession">The detection session.</param> /// <param name="searchPattern">The search pattern.</param> /// <returns>A list of file paths.</returns> private static IEnumerable <string> GetOutputFiles(DetectionSession detectionSession, string searchPattern) { var directory = detectionSession.BuildInformation.ActiveProjectOutputDirectory; return(Directory.GetFiles(directory, searchPattern, SearchOption.AllDirectories) .Where(file => file.IndexOf("vshost") < 0)); }
protected override AbstractAchievementVisitor CreateVisitor(DetectionSession detectionSession) { return(new Visitor() { MethodToFind = methodName, Requirements = RequiredOverloads }); }
protected override AbstractAchievementVisitor CreateVisitor(DetectionSession detectionSession) { return new Visitor() { MethodToFind = methodName, Requirements = RequiredOverloads }; }
public override bool DetectAchievement(DetectionSession detectionSession) { var nrefactorySession = detectionSession.GetSessionObjectOfType <NRefactorySession>(); var filename = detectionSession.BuildInformation.ActiveFile; var parser = nrefactorySession.GetParser(filename); var specials = parser.Lexer.SpecialTracker.RetrieveSpecials(); return(specials.OfType <Comment>().Any(a => a.CommentType == CommentType.Block)); }
public override bool DetectAchievement(DetectionSession detectionSession) { var nrefactorySession = detectionSession.GetSessionObjectOfType<NRefactorySession>(); var filename = detectionSession.BuildInformation.ActiveFile; var parser = nrefactorySession.GetParser(filename); var specials = parser.Lexer.SpecialTracker.RetrieveSpecials(); return specials.OfType<Comment>().Any(a => a.CommentType == CommentType.Block); }
public override bool DetectAchievement(DetectionSession detectionSession) { // Return out if there are no files to check achievements in. if (!detectionSession.BuildInformation.ChangedFiles.Any()) { return(false); } // Obtain a session object and the codebase type declarations var nrefactorySession = detectionSession.GetSessionObjectOfType <NRefactorySession>(); CodebaseTypeDeclarations = nrefactorySession.GetCodebaseTypeDeclarations(detectionSession.BuildInformation); // Have the concrete implementation create it's visitor var visitor = CreateVisitor(detectionSession); // Parse all files in the changed files collection for achievements foreach (var filename in detectionSession.BuildInformation.ChangedFiles) { // Obtain a parser from the nrefactorySession. // This parser is shared context between all concrete achievement implementations. var parser = nrefactorySession.GetParser(filename); // Pass concrete visitor into the AST created by the parser 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) { AchievementCodeLocation = visitor.CodeLocation; if (AchievementCodeLocation != null) { AchievementCodeLocation.FileName = filename; } return(true); } } return(false); }
public override bool DetectAchievement(DetectionSession detectionSession) { // Return out if there are no files to check achievements in. if (!detectionSession.BuildInformation.ChangedFiles.Any()) { return false; } // Obtain a session object and the codebase type declarations var nrefactorySession = detectionSession.GetSessionObjectOfType<NRefactorySession>(); CodebaseTypeDeclarations = nrefactorySession.GetCodebaseTypeDeclarations(detectionSession.BuildInformation); // Have the concrete implementation create it's visitor var visitor = CreateVisitor(detectionSession); // Parse all files in the changed files collection for achievements foreach (var filename in detectionSession.BuildInformation.ChangedFiles) { // Obtain a parser from the nrefactorySession. // This parser is shared context between all concrete achievement implementations. var parser = nrefactorySession.GetParser(filename); // Pass concrete visitor into the AST created by the parser 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) { AchievementCodeLocation = visitor.CodeLocation; if (AchievementCodeLocation != null) { AchievementCodeLocation.FileName = filename; } return true; } } return false; }
/// <summary> /// Detects the achievement. /// </summary> /// <param name="detectionSession">The detection session.</param> /// <returns><c>true</c> if the ChallengeRunner returned 'OK'; otherwise <c>false</c>.</returns> public override bool DetectAchievement(DetectionSession detectionSession) { if (string.IsNullOrEmpty(detectionSession.BuildInformation.ActiveProjectOutputDirectory)) { return(false); } var dlls = new List <string>(); // Some directory listing hackery dlls.AddRange(GetOutputFiles(detectionSession, "*.dll")); dlls.AddRange(GetOutputFiles(detectionSession, "*.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(" ", detectionSession.BuildInformation.ActiveProjectOutputDirectory.Replace(" ", "*"), ChallengeRunner); var process = Process.Start(processStartInfo); var output = process.StandardOutput.ReadToEnd(); var error = process.StandardError.ReadToEnd(); return(output == "OK"); }
protected abstract AbstractAchievementVisitor CreateVisitor(DetectionSession detectionSession);
public void TestAchievements() { ObjectFactory.Configure(a => a.For<IAchievementRepository>().Singleton().Use<AppDataXmlCompletedAchievementsRepository>()); var achievementRepository = ObjectFactory.GetInstance<IAchievementRepository>(); achievementRepository.LoadFromAssembly(typeof(NRefactoryAchievement).Assembly); var achievementTests = GetType().Assembly.GetTypes().Where(a => a.GetCustomAttributes(typeof (ExpectUnlockAttribute), true).Length > 0); foreach(var test in achievementTests) { var sourceFile = Path.GetFullPath("TestCases/" + test.Name + ".cs"); var buildInformation = new BuildInformation() { ActiveFile = sourceFile, ActiveProject = null, ActiveProjectOutputDirectory = Path.GetDirectoryName(sourceFile), ChangedFiles = new []{sourceFile} }; var expectedAchievements = test.GetCustomAttributes(typeof(ExpectUnlockAttribute), true).Select(a => ((ExpectUnlockAttribute)a).ExpectedAchievementType).ToList(); using (var detectionSession = new DetectionSession(buildInformation)) { var achievements = achievementRepository.GetAchievements().ToList(); var tasks = new Task[achievements.Count()]; var i = 0; var unlockedAchievements = new List<Type>(); foreach (var uncompletedAchievement in achievements) { var a = uncompletedAchievement; tasks[i++] = Task.Factory.StartNew(() => { var achievementType = a.AchievementType; var achievement = (AchievementBase)Activator.CreateInstance(achievementType); var achievementUnlocked = achievement.DetectAchievement(detectionSession); if (achievementUnlocked) { a.CodeLocation = achievement.AchievementCodeLocation; a.IsCompleted = true; unlockedAchievements.Add(achievementType); } }); } Task.WaitAll(tasks); //Test that expected achievements unlocked foreach(var expectedAchievement in expectedAchievements) { Assert.IsTrue(unlockedAchievements.Contains(expectedAchievement), Path.GetFileName(sourceFile) + " did not unlock expected achievement: " + expectedAchievement.FullName); } //Test that only expected achievements unlocked var unexpectedAchievements = unlockedAchievements.Except(expectedAchievements).Except(_globallyIgnoredAchievements).ToList(); Assert.IsTrue(unexpectedAchievements.Count() == 0, Path.GetFileName(sourceFile) + " unlocks unexpected achievements: " + string.Join(", ", unexpectedAchievements.Select(a => a.Name))); } } }
protected override AbstractAchievementVisitor CreateVisitor(DetectionSession detectionSession) { //Pass typeDeclarations into the constructor return(new Visitor(CodebaseTypeDeclarations)); }
protected override AbstractAchievementVisitor CreateVisitor(DetectionSession detectionSession) { //Pass typeDeclarations into the constructor return new Visitor(CodebaseTypeDeclarations); }
/// <summary> /// Gets the output files for a given session, filtered by the given search pattern. /// </summary> /// <param name="detectionSession">The detection session.</param> /// <param name="searchPattern">The search pattern.</param> /// <returns>A list of file paths.</returns> private static IEnumerable<string> GetOutputFiles(DetectionSession detectionSession, string searchPattern) { var directory = detectionSession.BuildInformation.ActiveProjectOutputDirectory; return Directory.GetFiles(directory, searchPattern, SearchOption.AllDirectories) .Where(file => file.IndexOf("vshost") < 0); }
public abstract bool DetectAchievement(DetectionSession detectionSession);
protected override AbstractAchievementVisitor CreateVisitor(DetectionSession detectionSession) { return(new Visitor()); }
public void TestAchievements() { ObjectFactory.Configure(a => a.For <IAchievementRepository>().Singleton().Use <AppDataXmlCompletedAchievementsRepository>()); var achievementRepository = ObjectFactory.GetInstance <IAchievementRepository>(); achievementRepository.LoadFromAssembly(typeof(NRefactoryAchievement).Assembly); var achievementTests = GetType().Assembly.GetTypes().Where(a => a.GetCustomAttributes(typeof(ExpectUnlockAttribute), true).Length > 0); foreach (var test in achievementTests) { var sourceFile = Path.GetFullPath("TestCases/" + test.Name + ".cs"); var buildInformation = new BuildInformation() { ActiveFile = sourceFile, ActiveProject = null, ActiveProjectOutputDirectory = Path.GetDirectoryName(sourceFile), ChangedFiles = new [] { sourceFile } }; var expectedAchievements = test.GetCustomAttributes(typeof(ExpectUnlockAttribute), true).Select(a => ((ExpectUnlockAttribute)a).ExpectedAchievementType).ToList(); using (var detectionSession = new DetectionSession(buildInformation)) { var achievements = achievementRepository.GetAchievements().ToList(); var tasks = new Task[achievements.Count()]; var i = 0; var unlockedAchievements = new List <Type>(); foreach (var uncompletedAchievement in achievements) { var a = uncompletedAchievement; tasks[i++] = Task.Factory.StartNew(() => { var achievementType = a.AchievementType; var achievement = (AchievementBase)Activator.CreateInstance(achievementType); var achievementUnlocked = achievement.DetectAchievement(detectionSession); if (achievementUnlocked) { a.CodeLocation = achievement.AchievementCodeLocation; a.IsCompleted = true; unlockedAchievements.Add(achievementType); } }); } Task.WaitAll(tasks); //Test that expected achievements unlocked foreach (var expectedAchievement in expectedAchievements) { Assert.IsTrue(unlockedAchievements.Contains(expectedAchievement), Path.GetFileName(sourceFile) + " did not unlock expected achievement: " + expectedAchievement.FullName); } //Test that only expected achievements unlocked var unexpectedAchievements = unlockedAchievements.Except(expectedAchievements).Except(_globallyIgnoredAchievements).ToList(); Assert.IsTrue(unexpectedAchievements.Count() == 0, Path.GetFileName(sourceFile) + " unlocks unexpected achievements: " + string.Join(", ", unexpectedAchievements.Select(a => a.Name))); } } }
protected override AbstractAchievementVisitor CreateVisitor(DetectionSession detectionSession) { return new Visitor(); }
/// <summary> /// Dispatches handling of achievement detection in the file(s) specified /// in the passed BuildInformation object. /// /// This method is detection method agnostic. It simply forwards the /// BuildInformation object to all implementations of the Achievement class. /// </summary> /// <param name="buildInformation"> /// Objects specifying documents to parse for achievements. /// </param> public static bool Dispatch(BuildInformation buildInformation) { AchievementContext.OnAchievementDetectionStarting(null, new EventArgs()); var unlockedAchievements = new List<Achievement>(); var achievementDescriptorRepository = ObjectFactory.GetInstance<IAchievementRepository>(); using (var detectionSession = new DetectionSession(buildInformation)) { var unlockableAchievements = achievementDescriptorRepository.GetUnlockableAchievements(); var stopWatch = new Stopwatch(); stopWatch.Start(); var tasks = new Task[unlockableAchievements.Count()]; var i = 0; foreach (var uncompletedAchievement in unlockableAchievements) { var a = uncompletedAchievement; tasks[i++] = Task.Factory.StartNew(() => { var achievement = (AchievementBase)Activator.CreateInstance(a.AchievementType); var achievementUnlocked = achievement.DetectAchievement(detectionSession); if (achievementUnlocked) { a.CodeLocation = achievement.AchievementCodeLocation; a.IsCompleted = true; unlockedAchievements.Add(a); } }); } Task.WaitAll(tasks); stopWatch.Stop(); OnDetectionCompleted(null, new DetectionCompletedEventArgs() { AchievementsTested = unlockableAchievements.Count(), ElapsedMilliseconds = (int)stopWatch.ElapsedMilliseconds }); } if (unlockedAchievements.Count() > 0) { foreach (var completedAchievement in unlockedAchievements.Where(a => a != null)) { achievementDescriptorRepository.MarkAchievementAsCompleted(completedAchievement); } AchievementContext.OnAchievementsUnlocked(null, unlockedAchievements); return true; } return false; }