public DecryptionEngine(PsvInformation psvInformation, LoggingService loggingService, StringProcessor stringProcessor) { _stringProcessor = stringProcessor; _psvInformation = psvInformation; _loggingService = loggingService; }
/// <summary> /// Main entry point. /// </summary> public async Task StartAsync() { // Preps the required services and information. try { _services = await Initialize.ConfigureServicesAsync().ConfigureAwait(false); } catch (Exception e) { Console.WriteLine(e); Console.ReadKey(); Environment.Exit(-1); } _logger = _services.GetRequiredService <LoggingService>(); _psvInfo = _services.GetRequiredService <PsvInformation>(); _util = _services.GetRequiredService <DecryptionEngine>(); // Informs the user whereabouts of the courses and output. _logger.Log(LogLevel.Information, $"Psv Directory: {_psvInfo.DirectoryPath}"); _logger.Log(LogLevel.Information, $"Courses Directory: {_psvInfo.CoursesPath}"); _logger.Log(LogLevel.Information, $"Output: {_psvInfo.Output}"); var courseNameBuilder = new StringBuilder(); foreach (string directory in _psvInfo.CoursesSubDirectories) { courseNameBuilder.AppendLine(Path.GetFileName(directory)); } _logger.Log(LogLevel.Information, $"Found {_psvInfo.CoursesSubDirectories.Length} courses..." + Environment.NewLine + courseNameBuilder); // Ready to begin decryption. _logger.Log(LogLevel.Warning, "Press any key to start decryption..."); Console.ReadKey(); var sw = Stopwatch.StartNew(); try { await _util.StartAsync().ConfigureAwait(false); } catch (Exception ex) { _logger.LogException(LogLevel.Error, ex); } sw.Stop(); _logger.Log(LogLevel.Information, $"Finished after {sw.Elapsed}."); _logger.Log(LogLevel.Information, "Press any key to exit."); Console.ReadKey(); }
/// <summary> /// Configures the required services and information for course infos. /// </summary> /// <exception cref="FileNotFoundException">Throws when Psv database cannot be found.</exception> /// <exception cref="DirectoryNotFoundException">Throws when Psv courses cannot be found.</exception> public static Task <IServiceProvider> ConfigureServicesAsync() { string envVar = Environment.GetEnvironmentVariable("localappdata"); string folderPath = string.IsNullOrEmpty(envVar) ? Environment.GetEnvironmentVariable("psv") : Path.Combine(envVar, "pluralsight"); string filePath = Path.Combine(folderPath, "pluralsight.db"); string coursesPath = Path.Combine(folderPath, "courses"); string output = Path.Combine(AppContext.BaseDirectory, "output"); if (!Directory.Exists(folderPath) || !File.Exists(filePath)) { throw new FileNotFoundException( $"Psv directory or database cannot be found ({folderPath}) Is PS viewer installed or is \"psv\" environment variable declared?"); } var subDirectories = Directory.GetDirectories(coursesPath); if (!Directory.Exists(coursesPath) || !subDirectories.Any()) { throw new DirectoryNotFoundException( "Psv courses not found. Did you not download any courses first?"); } if (!Directory.Exists(output)) { Directory.CreateDirectory(output); } var fileInfo = new PsvInformation { DirectoryPath = folderPath, FilePath = filePath, CoursesPath = coursesPath, CoursesSubDirectories = subDirectories, Output = output }; var collection = new ServiceCollection() .AddDbContext <PsvContext>() .AddSingleton <LoggingService>() .AddSingleton <DecryptionEngine>() .AddSingleton <StringProcessor>() .AddSingleton(fileInfo) .AddLogging(); var services = collection.BuildServiceProvider(); return(Task.FromResult <IServiceProvider>(services)); }
/// <summary> /// Configures the required services and information for course infos. /// </summary> /// <exception cref="FileNotFoundException">Throws when Psv database cannot be found.</exception> /// <exception cref="DirectoryNotFoundException">Throws when Psv courses cannot be found.</exception> public static async Task <IServiceProvider> ConfigureServicesAsync() { // Build/read config var config = new Config(); if (File.Exists("config.json")) { string configString = await File.ReadAllTextAsync("config.json").ConfigureAwait(false); config = JsonConvert.DeserializeObject <Config>(configString); } else { string configString = JsonConvert.SerializeObject(config, Formatting.Indented); await File.WriteAllTextAsync("config.json", configString).ConfigureAwait(false); } // Set-up required information string envVar = Environment.GetEnvironmentVariable("localappdata"); string folderPath = string.IsNullOrEmpty(envVar) ? Environment.GetEnvironmentVariable("psv") : Path.Combine(envVar, "pluralsight"); string filePath = Path.Combine(folderPath, "pluralsight.db"); string coursesPath = Path.Combine(folderPath, "courses"); string output = Path.Combine(AppContext.BaseDirectory, "output"); if (!Directory.Exists(folderPath) || !File.Exists(filePath)) { throw new FileNotFoundException( $"Psv directory or database cannot be found ({folderPath}) Is PS viewer installed or is \"psv\" environment variable declared?"); } var subDirectories = Directory.GetDirectories(coursesPath); if (!Directory.Exists(coursesPath) || !subDirectories.Any()) { throw new DirectoryNotFoundException( "Psv courses not found. Did you not download any courses first?"); } if (!Directory.Exists(output)) { Directory.CreateDirectory(output); } var fileInfo = new PsvInformation { DirectoryPath = folderPath, FilePath = filePath, CoursesPath = coursesPath, CoursesSubDirectories = subDirectories, Output = output }; var collection = new ServiceCollection() .AddDbContext <PsvContext>() .AddSingleton <LoggingService>() .AddSingleton <DecryptionEngine>() .AddSingleton <StringProcessor>() .AddSingleton(config) .AddSingleton(fileInfo) .AddLogging() .BuildServiceProvider(); return(collection); }