public static void AssignTargetName(string inputFolder, string outputFolder, DriverIdentityData targetIdentity)
 {
     if (targetIdentity != null)
     {
         foreach (var inputFile in Directory.GetFiles(inputFolder))
         {
             AssignTargetNameToFile(inputFile, outputFolder, targetIdentity);
         }
     }
 }
        public static void PortToBFWAV(string targetDriverFile)
        {
            DriverIdentityData[] driverIdentities = DeserializeDriverIdentityData();
            DriverIdentityData   targetIdentity   = FindTargetDriverIdentity(targetDriverFile, driverIdentities);

            Converter.ConvertFilesToBFWAV(GlobalDirectory.inputFolder, GlobalDirectory.bfwavTempFolder);
            RenameToFriendlyAlias(GlobalDirectory.bfwavTempFolder, GlobalDirectory.friendlyTempFolder, "bfwav", driverIdentities);

            AssignTargetName(GlobalDirectory.friendlyTempFolder, GlobalDirectory.outputFolder, targetIdentity);
        }
        public static DriverIdentityData[] DeserializeDriverIdentityData()
        {
            string[]             driverIdentityFiles = Directory.GetFiles(GlobalDirectory.identitiesDirectory);
            DriverIdentityData[] driverIdentities    = new DriverIdentityData[driverIdentityFiles.Length];

            for (int i = 0; i < driverIdentityFiles.Length; i++)
            {
                StreamReader reader = new StreamReader(driverIdentityFiles[i]);
                driverIdentities[i] = JsonConvert.DeserializeObject <DriverIdentityData>(reader.ReadToEnd());
                reader.Close();
            }
            return(driverIdentities);
        }
        public static DriverIdentityData FindTargetDriverIdentity(string targetDriverFile, DriverIdentityData[] driverIdentities)
        {
            string             driverName     = string.Empty;
            string             driverCode     = string.Empty;
            DriverIdentityData targetIdentity = null;

            //Get driver name and code
            foreach (DriverIdentityData driverIdentity in driverIdentities)
            {
                if (driverIdentity.fileName.Equals(targetDriverFile, StringComparison.OrdinalIgnoreCase))
                {
                    targetIdentity = driverIdentity;
                    break;
                }
            }
            return(targetIdentity);
        }
        public static void GenerateDriverIdentityData()
        {
            string fileInfoDirectoryU  = GlobalDirectory.fileInfoDirectoryU;
            string fileInfoDirectoryDX = GlobalDirectory.fileInfoDirectoryDX;

            string[] fileInfoFilepathsU = Directory.GetFiles(fileInfoDirectoryU);

            for (int i = 0; i < fileInfoFilepathsU.Length; i++)
            {
                string driverFile = Path.GetFileName(fileInfoFilepathsU[i]);

                string fileInfoPathU  = fileInfoFilepathsU[i];
                string fileInfoPathDX = fileInfoDirectoryDX + driverFile;

                //Deserialise FileInfo
                FileInfoData dataU  = JsonConvert.DeserializeObject <FileInfoData>(File.ReadAllText(fileInfoPathU));
                FileInfoData dataDX = JsonConvert.DeserializeObject <FileInfoData>(File.ReadAllText(fileInfoPathDX));

                //Get driverCode
                string driverName = string.Empty;
                string driverCode = string.Empty;
                string bfwavName  = Path.GetFileNameWithoutExtension(driverFile);

                foreach (FileInfoData.FileInfoElement elementDX in dataDX.elements)
                {
                    if (driverName == string.Empty && elementDX.fileName.StartsWith("pSE_HORN", StringComparison.OrdinalIgnoreCase))
                    {
                        string[] words = elementDX.fileName.Split('_');
                        driverName = words[2];
                        if (elementDX.fileName.Contains("Mii"))
                        {
                            driverName += "_" + words[3];
                        }
                        continue;
                    }
                    if (driverCode == string.Empty && elementDX.fileName.StartsWith("pVO_", StringComparison.OrdinalIgnoreCase) && !elementDX.fileName.StartsWith("pVO_M_", StringComparison.OrdinalIgnoreCase))
                    {
                        driverCode = elementDX.fileName.Split('_')[1];
                        continue;
                    }
                }

                //Create new identity
                DriverIdentityData identityData = new DriverIdentityData(bfwavName, driverName, driverCode, dataDX.elements.Count);

                //Find matching checksums and add them to the same element in the identity with a user friendly name
                foreach (FileInfoData.FileInfoElement elementU in dataU.elements)
                {
                    foreach (FileInfoData.FileInfoElement elementDX in dataDX.elements)
                    {
                        if (elementU.checksumMD5 == elementDX.checksumMD5)
                        {
                            string friendlyName = AliasData.GetFriendlyVoiceAlias(elementDX.fileName, identityData.driverName, identityData.driverCode);

                            identityData.AddElement(friendlyName, elementU.fileName, elementDX.fileName, elementU.bfwavFileSize, elementU.checksumMD5);
                        }
                    }

                    //Add exception for unlocking, as these files cannot be found in the DX version
                    if (elementU.fileName.StartsWith("pVO_N_"))
                    {
                        string friendlyName = AliasData.GetFriendlyVoiceAlias(elementU.fileName, identityData.driverName, identityData.driverCode);
                        identityData.AddElement(friendlyName, elementU.fileName, elementU.fileName, elementU.bfwavFileSize, elementU.checksumMD5);
                        identityData.voiceclipCount++;
                    }
                }


                if (identityData.elementCount != identityData.voiceclipCount)
                {
                    throw new System.Exception($"{ identityData.driverName }: The number of elements ({ identityData.elementCount }) did not match the number of voice clips ({ identityData.voiceclipCount })");
                }

                //Serialise the final JSON file for use in voice porting!
                string       json   = JsonConvert.SerializeObject(identityData, Formatting.Indented);
                StreamWriter writer = new StreamWriter($"{GlobalDirectory.identitiesDirectory}{driverFile}");
                writer.Write(json);
                writer.Close();
            }
        }
        public static void PortToBARS(string targetDriverFile)
        {
            DriverIdentityData[] driverIdentities = DeserializeDriverIdentityData();
            DriverIdentityData   targetIdentity   = FindTargetDriverIdentity(targetDriverFile, driverIdentities);

            Converter.ConvertFilesToBFWAV(GlobalDirectory.inputFolder, GlobalDirectory.bfwavTempFolder);
            RenameToFriendlyAlias(GlobalDirectory.bfwavTempFolder, GlobalDirectory.friendlyTempFolder, "bfwav", driverIdentities);

            bool menuFileCreated = false;

            //if contains menu, pack that and delete
            string menuFilepath = GlobalDirectory.friendlyTempFolder + "SELECT_DRIVER.bfwav";

            if (File.Exists(menuFilepath))
            {
                AssignTargetNameToFile(menuFilepath, GlobalDirectory.finalTempFolder, targetIdentity);

                string menuParam = GlobalDirectory.menuParamsDirectory + targetIdentity.fileName + "_param.bin";
                File.Copy(menuParam, GlobalDirectory.finalTempFolder + "_param.bin");

                string menuFileName = targetIdentity.fileName;

                if (targetIdentity.fileName == "AnimalBoyA")
                {
                    menuFileName = menuFileName.Replace("Boy", "Girl");
                    System.Windows.MessageBox.Show($"For some reason both Villagers use the same menu sound file. For this reason, your file has been output as the Female Villager file.");
                }

                if (Directory.GetFiles(GlobalDirectory.finalTempFolder).Length > 1)
                {
                    Uwizard.SARC.pack(GlobalDirectory.finalTempFolder, GlobalDirectory.outputFolder + "SNDG_M_" + menuFileName + ".bars", 0x020);
                    menuFileCreated = true;
                }
                else
                {
                    System.Windows.MessageBox.Show($"There were no compatible menu files for {targetIdentity.fileName} found in the input folder");
                }

                foreach (string file in Directory.GetFiles(GlobalDirectory.finalTempFolder))
                {
                    File.Delete(file);
                }
            }

            bool unlockFileCreated = false;

            //if contains unlock, pack that and delete
            string unlockFilepath = GlobalDirectory.friendlyTempFolder + "UNLOCK_DRIVER.bfwav";

            if (File.Exists(unlockFilepath))
            {
                AssignTargetNameToFile(unlockFilepath, GlobalDirectory.finalTempFolder, targetIdentity);

                string menuParam = GlobalDirectory.menuParamsDirectory + targetIdentity.fileName + "_param.bin";
                File.Copy(menuParam, GlobalDirectory.finalTempFolder + "_param.bin");

                if (Directory.GetFiles(GlobalDirectory.finalTempFolder).Length > 1)
                {
                    Uwizard.SARC.pack(GlobalDirectory.finalTempFolder, GlobalDirectory.outputFolder + "SNDG_N_" + targetIdentity.fileName + ".bars", 0x020);
                    unlockFileCreated = true;
                }
                else
                {
                    System.Windows.MessageBox.Show($"There were no compatible unlock files for {targetIdentity.fileName} found in the input folder");
                }

                foreach (string file in Directory.GetFiles(GlobalDirectory.finalTempFolder))
                {
                    File.Delete(file);
                }
            }

            //pack everything else

            AssignTargetName(GlobalDirectory.friendlyTempFolder, GlobalDirectory.finalTempFolder, targetIdentity);

            string param = GlobalDirectory.driverParamsDirectory + targetIdentity.fileName + "_param.bin";

            File.Copy(param, GlobalDirectory.finalTempFolder + "_param.bin");

            //Add a condition for unlock and menu
            if (Directory.GetFiles(GlobalDirectory.finalTempFolder).Length > 1)
            {
                Uwizard.SARC.pack(GlobalDirectory.finalTempFolder, GlobalDirectory.outputFolder + "SNDG_" + targetIdentity.fileName + ".bars", 0x020);
            }
            else if (!menuFileCreated && !unlockFileCreated)
            {
                System.Windows.MessageBox.Show($"There were no compatible driver files for {targetIdentity.fileName} found in the input folder");
            }
        }
        public static void AssignTargetNameToFile(string inputFile, string outputFolder, DriverIdentityData targetIdentity)
        {
            //Get the U name for the friendly temp file and add name/code
            if (targetIdentity != null)
            {
                string file = Path.GetFileNameWithoutExtension(inputFile);

                foreach (var element in targetIdentity.elements)
                {
                    if (file == element.userFriendlyName && element.uName != null)
                    {
                        string destination = outputFolder + element.uName + ".bfwav";
                        //outputFiles.Add(value);
                        if (!File.Exists(destination))
                        {
                            File.Move(inputFile, destination);
                        }
                    }
                }
            }
        }