public static void ReplaceCaptions(CaptionModifierFile modifier, string captionFile) { var compiledCaptions = new ValveResourceFormat.ClosedCaptions.ClosedCaptions(); compiledCaptions.Read(captionFile); var captionCompiler = new ClosedCaptions(); captionCompiler.Version = compiledCaptions.Version; foreach (var caption in compiledCaptions) { captionCompiler.Add(caption.Hash, caption.Text); } Console.WriteLine($"Successfully read {captionCompiler.Captions.Count} compiled captions.\n"); var result = modifier.ModifyCaptions(captionCompiler); Console.WriteLine($"Made the following modifications:\n" + $"{result.deleteCount} deletions.\n" + $"{result.replaceCount} replacements.\n" + $"{result.additionCount} additions."); #pragma warning disable CS8604 // Possible null reference argument. //var outputPath = Path.Combine(Path.GetDirectoryName(captionFile), $"{Path.GetFileNameWithoutExtension(captionFile)}_new{Path.GetExtension(captionFile)}"); var outputPath = Path.Combine(Directory.GetCurrentDirectory(), $"closecaption_{modifier.FileName}.dat"); #pragma warning restore CS8604 // Possible null reference argument. WriteCaptionFile(captionCompiler, outputPath); }
public void AddAllToClosedCaptions(ClosedCaptions captions) { foreach (var rule in Rules) { captions.Add(rule.Caption); } }
void Save(string language, string addOn) { string captionFile = System.IO.Path.Combine(Steam.SteamData.GetHLAInstallFolder(), Steam.SteamData.HLAWIPAddonGamePath, addOn, Steam.SteamData.CaptionFolder, string.Format(Steam.SteamData.CaptionFormat, language + "_custom")); ClosedCaptions captionsList = new ClosedCaptions(); ClosedCaptions workingCCList = new ClosedCaptions(); //First load in all the game captions for the selected language. string gameCaptionFile = System.IO.Path.Combine(Steam.SteamData.GetHLAInstallFolder(), @"game\hlvr", Steam.SteamData.CaptionFolder, string.Format(Steam.SteamData.CaptionFormat, language)); using (var stream = new FileStream(gameCaptionFile, FileMode.Open, FileAccess.Read)) { captionsList.Read(stream); } foreach (var caption in Captions) { workingCCList.Add(caption.Caption); captionsList.Add(caption.Caption); } captionsList.Write(captionFile); string workingCaptionFile = System.IO.Path.Combine(Steam.SteamData.GetHLAInstallFolder(), Steam.SteamData.HLAWIPAddonGamePath, addOn, Steam.SteamData.CaptionFolder, string.Format(workingCCFileFormat, language + "_custom")); workingCCList.Write(workingCaptionFile); MessageBox.Show("Captions saved as: " + captionFile + "\r\nWorking caption file: " + workingCaptionFile); }
public (int replaceCount, int deleteCount, int additionCount) ModifyCaptions(ClosedCaptions captions) { var additions = new List <CaptionModifierRule>(); int replaceCount = 0; int deleteCount = 0; int additionCount = 0; foreach (var rule in Rules) { bool isAddition = true; foreach (var caption in captions.Captions) { if (caption.SoundEventHash == rule.Caption.SoundEventHash) { // Deleting actually means nullifying the string until we figure out // why changing blocks messes up captions if (rule.ModificationType == ModificationType.Delete) { if (!caption.IsBlank) { // What's the best way to erase a caption file but keep the hash there? // can we just remove the caption entirely? caption.Definition = ""; //caption.Definition = new string('b', (caption.Length - 2) / 2); //Console.WriteLine(Encoding.Unicode.GetBytes(new string('\0', (caption.Length - 2))).Length); //caption.Definition = "\0"; deleteCount++; } } else { caption.Definition = rule.Caption.Definition; replaceCount++; } isAddition = false; } } if (isAddition) { additions.Add(rule); additionCount++; } } // Add the additions after the loops have ended foreach (var addition in additions) { captions.Insert(0, addition.Caption); } return(replaceCount, deleteCount, additionCount); }
public static void WriteCaptionFile(ClosedCaptions captions, string filename) { // Need a better way to clear the file if (File.Exists(filename)) { File.Delete(filename); } captions.Write(filename); Console.WriteLine("Wrote new caption file:"); Console.WriteLine(Path.GetFullPath(filename)); }
public static void CustomCaptionFile(CaptionModifierFile modifier) { var customCaptions = new ClosedCaptions(); modifier.AddAllToClosedCaptions(customCaptions); Console.WriteLine($"Added {modifier.Rules.Count} captions.\n"); var outputPath = Path.Combine(Directory.GetCurrentDirectory(), $"closecaption_{modifier.FileName}.dat"); WriteCaptionFile(customCaptions, outputPath); }
public static void AddTestToAll(string captionFile) { Console.WriteLine("Adding \" TEST\" to all"); var compiledCaptions = new ValveResourceFormat.ClosedCaptions.ClosedCaptions(); compiledCaptions.Read(captionFile); var captionCompiler = new ClosedCaptions(); foreach (var caption in compiledCaptions) { captionCompiler.Add(caption.Hash, caption.Text + " TEST"); } #pragma warning disable CS8604 // Possible null reference argument. var outputPath = Path.Combine(Path.GetDirectoryName(captionFile), $"{Path.GetFileNameWithoutExtension(captionFile)}_new{Path.GetExtension(captionFile)}"); #pragma warning restore CS8604 // Possible null reference argument. WriteCaptionFile(captionCompiler, outputPath); }
void LoadCaptionData() { string addonFolder = Steam.SteamData.GetHLAAddOnFolder(AddOn); Captions = new ObservableCollection <ClosedCaptionDependencyObject>(); SoundEvents = new ObservableCollection <Soundevent>(); hashToName = new Dictionary <uint, Soundevent>(); foreach (var eventFiles in new DirectoryInfo(addonFolder).GetFiles("*." + Steam.SteamData.SoundEventsExtension, SearchOption.AllDirectories)) { foreach (var soundName in AAT.AddonHelper.DeserializeFile(eventFiles.FullName)) { SoundEvents.Add(soundName); hashToName.Add(ValveResourceFormat.Crc32.Compute(Encoding.UTF8.GetBytes(soundName.EventName)), soundName); } } //Look for and load any caption file. string targetPath = System.IO.Path.Combine(Steam.SteamData.GetHLAInstallFolder(), Steam.SteamData.HLAWIPAddonGamePath, AddOn, Steam.SteamData.CaptionFolder); if (Directory.Exists(targetPath)) { foreach (var captionFiles in new DirectoryInfo(targetPath).GetFiles(string.Format(workingCCFileFormat, SelectedLanguage + "_custom"), SearchOption.AllDirectories)) { var closedCaptions = new ClosedCaptions(); using (var stream = captionFiles.OpenRead()) { closedCaptions.Read(stream); } foreach (var caption in closedCaptions.Captions) { var cap = new ClosedCaptionDependencyObject(caption); if (hashToName.ContainsKey(caption.SoundEventHash)) { cap.SoundEvent = hashToName[caption.SoundEventHash]; } Captions.Add(cap); } } } }
public static void CloneCaptionFile(string captionFile) { var compiledCaptions = new ValveResourceFormat.ClosedCaptions.ClosedCaptions(); compiledCaptions.Read(captionFile); PrintClosedCaptionData(compiledCaptions); var captionCompiler = new ClosedCaptions(); captionCompiler.Version = compiledCaptions.Version; foreach (var caption in compiledCaptions) { captionCompiler.Add(caption.Hash, caption.Text); } #pragma warning disable CS8604 // Possible null reference argument. var outputPath = Path.Combine(Path.GetDirectoryName(captionFile), $"{Path.GetFileNameWithoutExtension(captionFile)}_new{Path.GetExtension(captionFile)}"); #pragma warning restore CS8604 // Possible null reference argument. WriteCaptionFile(captionCompiler, outputPath); }