Example #1
0
        private void FavoritesToFnisListToolStripMenuItemClick(object sender, EventArgs e)
        {
            // warn to scare curious users
            var dialogResult = MessageBox.Show(
                Resources.CommonString_ScareMessage,
                Resources.CommonString_Warning,
                MessageBoxButtons.YesNo);
            if (dialogResult == DialogResult.No)
            {
                return;
            }

            // start logging
            Program.Log.Write("\nGenerate FNIS List from Favorites: " + DateTime.Now.ToString("u"));

            // locate favorites
            var favoritesJsonFilePath = this.skyrimDirectory + Resources.PoserHotkeysDataPath + Resources.FavoritesJsonName;
            if (!File.Exists(favoritesJsonFilePath))
            {
                var errorMessage = "Unable to locate favorites at " + favoritesJsonFilePath;
                MessageBox.Show(errorMessage, Resources.CommonString_Error, MessageBoxButtons.OK);
                Program.Log.Write(errorMessage);
                return;
            }

            // deserialize favorites
            var favoritesJsonData = File.ReadAllText(favoritesJsonFilePath);
            var favorites = JsonUtilFavorites.Deserialize(favoritesJsonData);

            // setup output paths
            var fnisOutputDirectory = this.skyrimDirectory + Resources.AnimationsDataPath + "PoserHotkeys\\";
            var fnisOutputFilePath = fnisOutputDirectory + "FNIS_PoserHotkeys_List.txt";
            var jsonOutputDirectory = this.skyrimDirectory + Resources.PoserHotkeysDataPath;

            // ensure output directory exists
            if (!Directory.Exists(fnisOutputDirectory))
            {
                Program.Log.Write("Creating directory: " + fnisOutputDirectory);
                Directory.CreateDirectory(fnisOutputDirectory);
            }

            // generate data
            var fnisLines = new ConcurrentBag<string>();
            using (var pleaseWaitMessage = new FormPleaseWaitMessage())
            {
                pleaseWaitMessage.Show(this);
                pleaseWaitMessage.Update();
                try
                {
                    // read fnis animation defintions
                    var availablePosers = this.checkedListBoxPosers.Items.OfType<Poser>();
                    var posersList = availablePosers as IList<Poser> ?? availablePosers.ToList();
                    Parallel.ForEach(
                        posersList,
                        poser =>
                            {
                                poser.PopulateAnimationEventDictionary();
                            });

                    Program.Log.Write("FNIS AnimationEvent dictionaries populated.");
                    Program.Log.Write("Attempting to match " + favorites.stringList.favorites.Count + " favorites.");

                    // compile line matches
                    Parallel.ForEach(
                        favorites.stringList.favorites,
                        favorite =>
                            {
                                Parallel.ForEach(
                                    this.posers,
                                    poser =>
                                        {
                                            if (poser.FnisLines.ContainsKey(favorite))
                                            {
                                                fnisLines.Add(poser.FnisLines[favorite]);
                                            }
                                        });
                            });

                    Program.Log.Write("Successfully matched " + fnisLines.Count + " favorites.");

                    // write results to file
                    Program.Log.Write("Overwritting " + fnisOutputFilePath);
                    File.WriteAllLines(fnisOutputFilePath, fnisLines);

                    // generate json for favorites
                    var favoritesPoser = new Poser(Resources.FavoritesPoserName, fnisOutputDirectory);
                    favoritesPoser.PopulatePacks(false);
                    WriteJson(new List<Poser> { favoritesPoser }, jsonOutputDirectory);
                }
                catch (Exception exception)
                {
                    Program.Log.Write("Fatal Error: " + exception.Message);
                    throw;
                }
                pleaseWaitMessage.Close();
            }

            // show summary
            var summaryMessage =
                String.Format(
                    $"Generated {fnisLines.Count} animation events."
                    + $"\n\nWrote FNIS data to:\n{fnisOutputFilePath}"
                    + $"\n\nWrote json data to:\n{jsonOutputDirectory + Resources.FavoritesPoserName}.json"
                    + "\n\nYou may now run FNIS Generator for Modders.");
            MessageBox.Show(summaryMessage, Resources.CommonString_Summary, MessageBoxButtons.OK);
            this.buttonQuit.Select();
        }