From 8c80e79720174ab4ce6d16a528db14827383446e Mon Sep 17 00:00:00 2001 From: Nahuel Rocchetti Date: Sat, 29 Jun 2024 23:30:03 -0300 Subject: [PATCH] SpeechEnhancer -> SPXConverter --- .../{SpeechEnhancer.cs => SPXConverter.cs} | 123 ++---------------- ...hEnhancer.cs.meta => SPXConverter.cs.meta} | 0 2 files changed, 14 insertions(+), 109 deletions(-) rename Assets/Editor/OpenTS2/{SpeechEnhancer.cs => SPXConverter.cs} (50%) rename Assets/Editor/OpenTS2/{SpeechEnhancer.cs.meta => SPXConverter.cs.meta} (100%) diff --git a/Assets/Editor/OpenTS2/SpeechEnhancer.cs b/Assets/Editor/OpenTS2/SPXConverter.cs similarity index 50% rename from Assets/Editor/OpenTS2/SpeechEnhancer.cs rename to Assets/Editor/OpenTS2/SPXConverter.cs index 7c20a35..ee0c685 100644 --- a/Assets/Editor/OpenTS2/SpeechEnhancer.cs +++ b/Assets/Editor/OpenTS2/SPXConverter.cs @@ -17,14 +17,21 @@ using UnityEngine; namespace OpenTS2 { - public class SpeechEnhancer + public class SPXConverter { - [MenuItem("OpenTS2/Other/Improve Speech Using Tasks")] - private static void EnhanceSpeechTasks() + [MenuItem("OpenTS2/Experiments/Convert all SPX to WAV")] + private static void ConvertSPX() { + var baseGameOnly = false; + var compressed = false; + if (!EditorUtility.DisplayDialog("SPX to WAV", "This operation will convert ALL SPX resources to WAV. This will take a while and use a lot of resources. Proceed?", "Yes", "No")) + return; + baseGameOnly = EditorUtility.DisplayDialog("SPX to WAV", "Which products do you want to convert?", "Base-Game only", "All products"); + compressed = EditorUtility.DisplayDialog("SPX to WAV", "Do you want to compress the resulting packages?", "Yes", "No"); Core.CoreInitialized = false; Core.InitializeCore(); - EPManager.Instance.InstalledProducts = (int)ProductFlags.BaseGame; + if (baseGameOnly) + EPManager.Instance.InstalledProducts = (int)ProductFlags.BaseGame; var epManager = EPManager.Instance; var products = epManager.GetInstalledProducts(); var contentManager = ContentManager.Instance; @@ -72,7 +79,7 @@ namespace OpenTS2 { Debug.Log($"Starting work on {package}"); var newPackage = new DBPFFile(); - newPackage.FilePath = Path.Combine("Enhanced Speech", package); + newPackage.FilePath = Path.Combine("SPX to WAV", package); var entriesToDoForThisPackage = new List(); foreach (var spx in speechResources) { @@ -102,7 +109,7 @@ namespace OpenTS2 { var spxFile = new SPXFile(entry.GetBytes()); if (spxFile != null && spxFile.DecompressedData != null && spxFile.DecompressedData.Length > 0) - newPackage.Changes.Set(spxFile.DecompressedData, entry.TGI, false); + newPackage.Changes.Set(spxFile.DecompressedData, entry.TGI, compressed); } catch (Exception e) { @@ -125,109 +132,7 @@ namespace OpenTS2 } }, TaskScheduler.Default); } - }).Start(); - } - - - [MenuItem("OpenTS2/Other/Improve Speech")] - private static void EnhanceSpeech() - { - Core.CoreInitialized = false; - Core.InitializeCore(); - EPManager.Instance.InstalledProducts = (int)ProductFlags.BaseGame; - var epManager = EPManager.Instance; - var products = epManager.GetInstalledProducts(); - var contentManager = ContentManager.Instance; - - foreach(var product in products) - { - var packages = Filesystem.GetPackagesInDirectory(Path.Combine(Filesystem.GetDataPathForProduct(product), "Res/Sound")); - contentManager.AddPackages(packages); - } - - var audioResources = contentManager.ResourceMap.Where(x => x.Key.TypeID == TypeIDs.AUDIO); - var speechResources = new List(); - var speechPackages = new HashSet(); - Debug.Log($"Found {audioResources.Count()} Audio Resources."); - Debug.Log($"Looking for speech resources..."); - - new Thread(() => - { - foreach (var audioResource in audioResources) - { - try - { - var audioData = audioResource.Value.GetBytes(); - var magic = Encoding.UTF8.GetString(audioData, 0, 2); - if (magic == "SP") - { - speechResources.Add(audioResource.Value); - var packageFileName = Path.GetFileName(audioResource.Value.Package.FilePath); - speechPackages.Add(packageFileName); - } - } - catch (Exception) { } - } - - Debug.Log($"Found {speechResources.Count} SPX audio resources, in {speechPackages.Count} packages."); - var packagesStr = "Packages:"; - foreach(var package in speechPackages) - { - packagesStr += $"\n{package}"; - } - Debug.Log(packagesStr); - - GC.Collect(); - foreach(var package in speechPackages) - { - Debug.Log($"Starting work on {package}"); - var newPackage = new DBPFFile(); - newPackage.FilePath = Path.Combine("Enhanced Speech", package); - var entriesToDoForThisPackage = new List(); - foreach (var spx in speechResources) - { - try - { - if (spx.Package == null) continue; - if (Path.GetFileName(spx.Package.FilePath) != package) continue; - entriesToDoForThisPackage.Add(spx); - //var asset = spx.GetAsset(); - //newPackage.Changes.Set(asset.GetWAVData(), asset.TGI, true); - } - catch (Exception e) - { - Debug.LogError(e); - } - } - Debug.Log($"Will convert {entriesToDoForThisPackage.Count} Entries"); - var centry = 0; - foreach(var entry in entriesToDoForThisPackage) - { - centry += 1; - if (centry % 500 == 0) - Debug.Log($"Progress: {centry}/{entriesToDoForThisPackage.Count}"); - try - { - var spxFile = new SPXFile(entry.GetBytes()); - if (spxFile != null && spxFile.DecompressedData != null && spxFile.DecompressedData.Length > 0) - newPackage.Changes.Set(spxFile.DecompressedData, entry.TGI, false); - } - catch (Exception e) { - Debug.LogError(e); - } - } - try - { - Debug.Log("Writing to disk..."); - newPackage.WriteToFile(); - Debug.Log($"Completed {package}!"); - GC.Collect(); - } - catch (Exception e) - { - Debug.LogError(e); - } - } + Debug.Log("All SPX has been converted to WAV! Resulting packages have been written to the SPX to WAV folder."); }).Start(); } } diff --git a/Assets/Editor/OpenTS2/SpeechEnhancer.cs.meta b/Assets/Editor/OpenTS2/SPXConverter.cs.meta similarity index 100% rename from Assets/Editor/OpenTS2/SpeechEnhancer.cs.meta rename to Assets/Editor/OpenTS2/SPXConverter.cs.meta