private async Task Run(bool fastShutdown) { Console.WriteLine($"{(fastShutdown ? "Fast" : "Slow")} shutdown started"); if (!fastShutdown) { // Shutdown the Scheduler await QuartzScheduler.Dispose(); // Shutdown the DiscordBot await DiscordBot.Dispose(); } else { // Create a backup of the current config.json just in case File.Copy(Boot.LOCAL_CONFIGURATION, Boot.LOCAL_CONFIGURATION_AUTOMATIC_BACKUP, true); } // Shutdown Twitter TwitterManager.Dispose(); // Shutdown DigitalOcean DoApi.Dispose(); // Shutdown S3 S3Api.Dispose(); // Shutdown BCAT BcatApi.Dispose(); // Shutdown DAuth DAuthApi.Dispose(); // Shutdown the HandlerMapper HandlerMapper.Dispose(); // Shutdown the KeysetManager KeysetManager.Dispose(); // Shutdown anything app-specific ShutdownAppSpecificItems(); // Save the configuration Configuration.LoadedConfiguration.Write(); if (!fastShutdown) { // Wait a little while await Task.Delay(1000 *ShutdownWaitTime); } Console.WriteLine("Shutdown complete"); }
public static async Task <Dictionary <string, byte[]> > DownloadAllData(Topic topic, string titleId, string passphrase, string targetFolder) { // Create the target folder System.IO.Directory.CreateDirectory(targetFolder); // Create a Dictionary Dictionary <string, byte[]> dataDict = new Dictionary <string, byte[]>(); // Loop over every directory foreach (Nintendo.Bcat.Directory directory in topic.Directories) { // Create the local directory path string localDirectory = Path.Combine(targetFolder, directory.Name); // Create the folder System.IO.Directory.CreateDirectory(localDirectory); // Loop over every data foreach (Nintendo.Bcat.Data data in directory.Data) { // Download the file byte[] rawData = await BcatApi.DownloadContainerAndDecrypt(data.Url, titleId, passphrase); // Add this to the Dictionary dataDict.Add(directory.Name + "/" + data.Name, rawData); // Construct the path string path = Path.Combine(targetFolder, directory.Name, data.Name); // Write out the file File.WriteAllBytes(path, rawData); } } return(dataDict); }
public async Task Execute(IJobExecutionContext context) { try { // Log that we're about to begin a check await DiscordBot.LoggingChannel.SendMessageAsync("**[BCAT]** Beginning check"); // Download the latest Topic Topic topic = await BcatApi.GetDataTopic(Program.TITLE_ID, Program.PASSPHRASE); // Create the target folder name string targetFolder = string.Format(Program.LOCAL_OLD_DATA_DIRECTORY, DateTime.Now.ToString(Program.FOLDER_DATE_TIME_FORMAT)); // Check if this the first run if (!Configuration.LoadedConfiguration.FirstRunCompleted) { // Log that this is the first run of BCAT await DiscordBot.LoggingChannel.SendMessageAsync("**[BCAT]** First run"); // Download all data Dictionary <string, byte[]> downloadedData = await BcatCheckerUtils.DownloadAllData(topic, Program.TITLE_ID, Program.PASSPHRASE, targetFolder); // Loop over all data foreach (KeyValuePair <string, byte[]> pair in downloadedData) { // Get the FileType FileType fileType = FileTypeExtensions.GetTypeFromName(pair.Key); // Check if this is a container if (fileType.IsContainer()) { // Create a Container instance Nintendo.SmashUltimate.Bcat.Container container = CreateSmashContainerInstance(fileType, pair.Value); // Add this to the container cache ContainerCache.AddFile(container, Path.GetFileName(pair.Key), pair.Value); } else { // Write this file out to the common file cache File.WriteAllBytes(Path.Combine(Program.LOCAL_COMMON_CACHE_DIRECTORY, Path.GetFileName(pair.Key)), pair.Value); } } // Save the configuration Configuration.LoadedConfiguration.FirstRunCompleted = true; Configuration.LoadedConfiguration.Write(); // Write out the topic File.WriteAllBytes(Program.LOCAL_LAST_TOPIC, MessagePackSerializer.Serialize(topic)); // Log that we're about to begin a check await DiscordBot.LoggingChannel.SendMessageAsync("**[BCAT]** First run complete"); return; } // Load the old Topic Topic oldTopic = MessagePackSerializer.Deserialize <Topic>(File.ReadAllBytes(Program.LOCAL_LAST_TOPIC)); #if DEBUG if (!Configuration.LoadedConfiguration.IsProduction) { /*foreach (Bcat.Directory dir in oldTopic.Directories) * { * if (dir.Name == "line_news") * { * Data dbgData = dir.Data.FirstOrDefault(); * if (dbgData == null) * { * continue; * } * dir.Data.Remove(dbgData); * //dbgData.Digest = "deadbeef"; * } * }*/ } #endif // Get the differences List <KeyValuePair <DifferenceType, string> > differences = BcatCheckerUtils.GetTopicChanges(oldTopic, topic); // Check if there aren't any if (differences.Count == 0) { // Nothing to do here goto finished; } // Download all data Dictionary <string, byte[]> data = await BcatCheckerUtils.DownloadAllData(topic, Program.TITLE_ID, Program.PASSPHRASE, targetFolder); // Loop over every difference foreach (KeyValuePair <DifferenceType, string> pair in differences) { // Log the difference await DiscordBot.LoggingChannel.SendMessageAsync("**[BCAT]** diff: ``" + pair.Value + "`` (" + pair.Key.ToString() + ")"); // Get the FileType FileType fileType = FileTypeExtensions.GetTypeFromName(pair.Value); // Declare a variable to hold the method parameters object[] parameters; if (pair.Key == DifferenceType.Added) { // Get the raw file byte[] rawFile = data[pair.Value]; // Check if this is a Container if (fileType.IsContainer()) { // Create a Container instance Nintendo.SmashUltimate.Bcat.Container addedContainer = CreateSmashContainerInstance(fileType, data[pair.Value]); // Add this to the container cache ContainerCache.AddFile(addedContainer, Path.GetFileName(pair.Value), rawFile); // Set the method parameters parameters = new object[] { addedContainer }; } else { // Write this file out to the common file cache File.WriteAllBytes(Path.Combine(Program.LOCAL_COMMON_CACHE_DIRECTORY, Path.GetFileName(pair.Value)), data[pair.Value]); // Set the method parameters parameters = new object[] { rawFile }; } } else if (pair.Key == DifferenceType.Changed) { // Get the raw file byte[] rawFile = data[pair.Value]; // Check if this is a Container if (fileType.IsContainer()) { // Create a Container instance Nintendo.SmashUltimate.Bcat.Container addedContainer = CreateSmashContainerInstance(fileType, data[pair.Value]); // Overwrite this to the container cache Nintendo.SmashUltimate.Bcat.Container previousContainer = ContainerCache.OverwriteFile(addedContainer, Path.GetFileName(pair.Value), rawFile); // Set the method parameters parameters = new object[] { previousContainer, addedContainer }; } else { // Construct the commoon cache path string path = Path.Combine(Program.LOCAL_COMMON_CACHE_DIRECTORY, Path.GetFileName(pair.Value)); // Load the old file byte[] previousRawFile = File.ReadAllBytes(path); // Write this file out to the common file cache File.WriteAllBytes(path, data[pair.Value]); // Set the method parameters parameters = new object[] { previousRawFile, rawFile }; } } else // Removed { // TODO: discord print continue; } // Call every difference handler await BcatCheckerUtils.CallDifferenceHandlers((int)fileType, pair.Key, parameters); } // Write out the Topic File.WriteAllBytes(Program.LOCAL_LAST_TOPIC, MessagePackSerializer.Serialize(topic)); finished: await DiscordBot.LoggingChannel.SendMessageAsync("**[BCAT]** Check complete"); } catch (Exception exception) { // Notify the logging channel await DiscordUtil.HandleException(exception, $"in ``BcatCheckerJob``"); } }
public async Task Execute(IJobExecutionContext context) { try { if (Configuration.LoadedConfiguration.FirstRunCompleted) { throw new Exception("Attempting to do first run more than once"); } foreach (KeyValuePair <RomType, BcatPair> bcatPairEntry in Program.BcatPairs) { // Log that we're about to begin a check await DiscordBot.LoggingChannel.SendMessageAsync("**[BCAT]** Beginning topic download for " + bcatPairEntry.Key.ToString()); // Download the latest Topic Topic topic = await BcatApi.GetDataTopic(bcatPairEntry.Value.TitleId, bcatPairEntry.Value.Passphrase); // Create the target folder name string targetFolder = string.Format(Program.LOCAL_OLD_DATA_DIRECTORY, DateTime.Now.ToString(Program.FOLDER_DATE_TIME_FORMAT), bcatPairEntry.Key.ToString()); // Download all data Dictionary <string, byte[]> downloadedData = await BcatCheckerUtils.DownloadAllData(topic, bcatPairEntry.Value.TitleId, bcatPairEntry.Value.Passphrase, targetFolder); // Loop over all data foreach (KeyValuePair <string, byte[]> dataPair in downloadedData) { // Get the FileType FileType fileType = FileTypeExtensions.GetTypeFromFilePath(dataPair.Key); // Populate the FileCache directories based on the FileType string path; switch (fileType) { case FileType.VersusSetting: path = string.Format(FileCache.VERSUS_SETTING_PATH, bcatPairEntry.Key.ToString()); break; case FileType.CoopSetting: // Only write the CoopSetting file once if (bcatPairEntry.Key != RomType.NorthAmerica) { continue; } path = FileCache.COOP_SETTING_PATH; break; case FileType.FestivalByaml: // Deserialize the byaml to get the ID dynamic byaml = ByamlLoader.GetByamlDynamic(dataPair.Value); // Generate the path path = string.Format(FileCache.FESTIVAL_SETTING_PATH, bcatPairEntry.Key.ToString(), byaml["FestivalId"]); break; default: continue; } // Create the directories if needed System.IO.Directory.CreateDirectory(Path.GetDirectoryName(path)); // Write the file File.WriteAllBytes(path, dataPair.Value); } // Write out the topic File.WriteAllBytes(string.Format(Program.LOCAL_LAST_TOPIC, bcatPairEntry.Key.ToString()), MessagePackSerializer.Serialize(topic)); // Set the last data download directory (Configuration.LoadedConfiguration as JelonzoBotConfiguration).LastDownloadPaths[bcatPairEntry.Key] = targetFolder; // Log that the first run is done await DiscordBot.LoggingChannel.SendMessageAsync($"**[BCAT]** First run complete for {bcatPairEntry.Key.ToString()}"); } // Save the configuration Configuration.LoadedConfiguration.FirstRunCompleted = true; Configuration.LoadedConfiguration.Write(); // Initialize the FileCache FileCache.Initialize(); } catch (Exception exception) { // Notify the logging channel await DiscordUtil.HandleException(exception, $"in ``BcatCheckerJob``"); } await QuartzScheduler.ScheduleJob <BcatCheckerJob>("Normal", Configuration.LoadedConfiguration.JobSchedules["Bcat"]); }
static async Task Main(string[] args) { // Wait for the debugger to attach if requested if (args.Length > 0 && args[0].ToLower() == "--waitfordebugger") { Console.WriteLine("Waiting for debugger..."); while (!Debugger.IsAttached) { await Task.Delay(1000); } Console.WriteLine("Debugger attached!"); } // Get the type of the Configuration Type configType = TypeUtils.GetSubclassOfType <Configuration>(); // Declare variable to hold the configuration Configuration configuration; // Check if the config file exists if (!File.Exists(LOCAL_CONFIGURATION)) { // Create a new dummy Configuration configuration = (Configuration)Activator.CreateInstance(TypeUtils.GetSubclassOfType <Configuration>()); configuration.SetDefaults(); // Write out the default config configuration.Write(LOCAL_CONFIGURATION); Console.WriteLine("Wrote default configuration to " + LOCAL_CONFIGURATION); return; } // Create the Exception logs directory System.IO.Directory.CreateDirectory(LOCAL_EXCEPTION_LOGS_DIRECTORY); // Load the Configuration Configuration.Load(configType, LOCAL_CONFIGURATION); // Initialize the Localizer Localizer.Initialize(); // Initialize the HandlerMapper HandlerMapper.Initialize(); // Initialize the KeysetManager KeysetManager.Initialize(); // Initialize DAuth DAuthApi.Initialize(); // Initialize BCAT BcatApi.Initialize(); // Initialize S3 S3Api.Initialize(); // Initialize DigitalOcean DoApi.Initialize(); // Initialize Twitter TwitterManager.Initialize(); // Initialize the DiscordBot await DiscordBot.Initialize(); // Initialize the Scheduler await QuartzScheduler.Initialize(); // Wait for the bot to fully initialize while (!DiscordBot.IsReady) { await Task.Delay(1000); } // Print out to the logging channel that we're initialized await DiscordBot.LoggingChannel.SendMessageAsync("\\*\\*\\* **Initialized**"); // Schedule the BootHousekeepingJob await QuartzScheduler.ScheduleJob(TypeUtils.GetSubclassOfType <BootHousekeepingJob>(), "Immediate"); // Register the SIGTERM handler AssemblyLoadContext.Default.Unloading += async x => { // Run Shutdown in fast mode await Shutdown.CreateAndRun(true); }; await Task.Delay(-1); }
public async Task Execute(IJobExecutionContext context) { try { foreach (KeyValuePair <RomType, BcatPair> bcatPairEntry in Program.BcatPairs) { // Log that we're about to begin a check await DiscordBot.LoggingChannel.SendMessageAsync("**[BCAT]** Beginning topic download for " + bcatPairEntry.Key.ToString()); // Download the latest Topic Topic topic = await BcatApi.GetDataTopic(bcatPairEntry.Value.TitleId, bcatPairEntry.Value.Passphrase); // Create the target folder name string targetFolder = string.Format(Program.LOCAL_OLD_DATA_DIRECTORY, DateTime.Now.ToString(Program.FOLDER_DATE_TIME_FORMAT), bcatPairEntry.Key.ToString()); // Format the old Topic path string oldTopicPath = string.Format(Program.LOCAL_LAST_TOPIC, bcatPairEntry.Key.ToString()); // Load the old Topic Topic oldTopic = MessagePackSerializer.Deserialize <Topic>(File.ReadAllBytes(oldTopicPath)); #if DEBUG if (!Configuration.LoadedConfiguration.IsProduction) { /*foreach (Nintendo.Bcat.Directory dir in oldTopic.Directories) * { * if (dir.Name == "fesdata") * { * Data dbgData = dir.Data.Where(x => x.Name == "Festival.byaml").FirstOrDefault(); * dbgData.Digest = "deadbeef"; * } * }*/ } #endif // Get the differences List <KeyValuePair <DifferenceType, string> > differences = BcatCheckerUtils.GetTopicChanges(oldTopic, topic); // Check if there aren't any if (differences.Count == 0) { // Nothing to do here goto finished; } // Download all data Dictionary <string, byte[]> data = await BcatCheckerUtils.DownloadAllData(topic, bcatPairEntry.Value.TitleId, bcatPairEntry.Value.Passphrase, targetFolder); // Loop over every difference foreach (KeyValuePair <DifferenceType, string> differencePair in differences) { // Log the difference await DiscordBot.LoggingChannel.SendMessageAsync("**[BCAT]** diff: ``" + differencePair.Value + "`` (" + differencePair.Key.ToString() + ")"); // Get the FileType FileType fileType = FileTypeExtensions.GetTypeFromFilePath(differencePair.Value); // Create a ByamlSerializer instance ByamlSerializer serializer = new ByamlSerializer(new ByamlSerializerSettings() { ByteOrder = ByteOrder.LittleEndian, SupportPaths = false, Version = ByamlVersion.Version1 }); // Create the difference handler parameters based off the FileType and difference type object[] parameters; if (differencePair.Key != DifferenceType.Removed) { // Get the raw file byte[] rawFile = data[differencePair.Value]; // Deserialize the object from byaml if necessary object deserializedObject; using (MemoryStream memoryStream = new MemoryStream(rawFile)) { switch (fileType) { case FileType.VersusSetting: deserializedObject = serializer.Deserialize <VersusSetting>(memoryStream); break; case FileType.CoopSetting: deserializedObject = serializer.Deserialize <CoopSetting>(memoryStream); break; case FileType.FestivalByaml: deserializedObject = serializer.Deserialize <FestivalSetting>(memoryStream); break; default: deserializedObject = data[differencePair.Value]; break; } } if (differencePair.Key == DifferenceType.Added) { parameters = new object[] { bcatPairEntry.Key, data, deserializedObject, rawFile }; } else // Changed { // Get the previous file object previousFile; switch (fileType) { case FileType.VersusSetting: previousFile = FileCache.GetVersusSettingForRomType(bcatPairEntry.Key); break; case FileType.CoopSetting: previousFile = FileCache.GetCoopSetting(); break; case FileType.FestivalByaml: previousFile = FileCache.GetLatestFestivalSettingForRomType(bcatPairEntry.Key); break; default: // Construct the previous file path string previousPath = Path.Combine((Configuration.LoadedConfiguration as JelonzoBotConfiguration).LastDownloadPaths[bcatPairEntry.Key], differencePair.Value.Replace('/', Path.DirectorySeparatorChar)); // Load it previousFile = File.ReadAllBytes(previousPath);; break; } // Create the parameters parameters = new object[] { bcatPairEntry.Key, data, previousFile, deserializedObject, rawFile }; } } else { parameters = new object[] { bcatPairEntry.Key, data }; } // Call every difference handler await BcatCheckerUtils.CallDifferenceHandlers((int)fileType, differencePair.Key, parameters); } // Write out the Topic File.WriteAllBytes(oldTopicPath, MessagePackSerializer.Serialize(topic)); // Set the last download path (Configuration.LoadedConfiguration as JelonzoBotConfiguration).LastDownloadPaths[bcatPairEntry.Key] = targetFolder; finished: await DiscordBot.LoggingChannel.SendMessageAsync("**[BCAT]** Check complete for " + bcatPairEntry.Key.ToString()); } } catch (Exception exception) { // Notify the logging channel await DiscordUtil.HandleException(exception, $"in ``BcatCheckerJob``"); } }