Crunchy-Downloader/CRD/Utils/Files/CfgManager.cs
Elwador dc570bf420 - Added **toggle to also download description audio** for selected dubs
- Added **automatic history backups** retained for up to 5 days
- Improved **season tab** to display series more effectively
- Improved **history saving** for increased data safety
- Removed **"None" option** from the hardsub selection popup
2025-11-06 20:44:03 +01:00

369 lines
No EOL
14 KiB
C#

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using CRD.Downloader.Crunchyroll;
using Newtonsoft.Json;
namespace CRD.Utils.Files;
public class CfgManager{
private static string workingDirectory = AppContext.BaseDirectory;
public static readonly string PathCrToken = Path.Combine(workingDirectory, "config", "cr_token.json");
public static readonly string PathCrDownloadOptions = Path.Combine(workingDirectory, "config", "settings.json");
public static readonly string PathCrHistory = Path.Combine(workingDirectory, "config", "history.json");
public static readonly string PathWindowSettings = Path.Combine(workingDirectory, "config", "windowSettings.json");
private static readonly string ExecutableExtension = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".exe" : string.Empty;
public static readonly string PathFFMPEG = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? Path.Combine(workingDirectory, "lib", "ffmpeg.exe") :
File.Exists(Path.Combine(workingDirectory, "lib", "ffmpeg")) ? Path.Combine(workingDirectory, "lib", "ffmpeg") : "ffmpeg";
public static readonly string PathMKVMERGE = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? Path.Combine(workingDirectory, "lib", "mkvmerge.exe") :
File.Exists(Path.Combine(workingDirectory, "lib", "mkvmerge")) ? Path.Combine(workingDirectory, "lib", "mkvmerge") : "mkvmerge";
public static readonly string PathMP4Decrypt = Path.Combine(workingDirectory, "lib", "mp4decrypt" + ExecutableExtension);
public static readonly string PathShakaPackager = Path.Combine(workingDirectory, "lib", "shaka-packager" + ExecutableExtension);
public static readonly string PathWIDEVINE_DIR = Path.Combine(workingDirectory, "widevine");
public static readonly string PathVIDEOS_DIR = Path.Combine(workingDirectory, "video");
public static readonly string PathENCODING_PRESETS_DIR = Path.Combine(workingDirectory, "presets");
public static readonly string PathTEMP_DIR = Path.Combine(workingDirectory, "temp");
public static readonly string PathFONTS_DIR = Path.Combine(workingDirectory, "fonts");
public static readonly string PathLogFile = Path.Combine(workingDirectory, "logfile.txt");
private static StreamWriter logFile;
private static bool isLogModeEnabled = false;
static CfgManager(){
AppDomain.CurrentDomain.ProcessExit += OnProcessExit;
}
private static void OnProcessExit(object? sender, EventArgs e){
DisableLogMode();
}
public static void EnableLogMode(){
if (!isLogModeEnabled){
try{
var fileStream = new FileStream(PathLogFile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
logFile = new StreamWriter(fileStream);
logFile.AutoFlush = true;
Console.SetError(logFile);
isLogModeEnabled = true;
Console.Error.WriteLine("Log mode enabled.");
} catch (Exception e){
Console.Error.WriteLine($"Couldn't enable logging: {e}");
}
}
}
public static void DisableLogMode(){
if (isLogModeEnabled){
try{
logFile.Close();
StreamWriter standardError = new StreamWriter(Console.OpenStandardError());
standardError.AutoFlush = true;
Console.SetError(standardError);
isLogModeEnabled = false;
Console.Error.WriteLine("Log mode disabled.");
} catch (Exception e){
Console.Error.WriteLine($"Couldn't disable logging: {e}");
}
}
}
public static void WriteCrSettings(){
WriteJsonToFile(PathCrDownloadOptions, CrunchyrollManager.Instance.CrunOptions);
}
// public static void WriteTokenToYamlFile(CrToken token, string filePath){
// // Convert the object to YAML
// var serializer = new SerializerBuilder()
// .WithNamingConvention(UnderscoredNamingConvention.Instance) // Ensure consistent naming convention
// .Build();
// var yaml = serializer.Serialize(token);
//
// string dirPath = Path.GetDirectoryName(filePath) ?? string.Empty;
//
// if (!Directory.Exists(dirPath)){
// Directory.CreateDirectory(dirPath);
// }
//
// if (!File.Exists(filePath)){
// using (var fileStream = File.Create(filePath)){
// }
// }
//
// // Write the YAML to a file
// File.WriteAllText(filePath, yaml);
// }
public static void UpdateSettingsFromFile<T>(T options, string filePath) where T : class{
if (options == null){
throw new ArgumentNullException(nameof(options));
}
string dirPath = Path.GetDirectoryName(filePath) ?? string.Empty;
if (!Directory.Exists(dirPath)){
Directory.CreateDirectory(dirPath);
}
if (!File.Exists(filePath)){
// Create the file if it doesn't exist
using (var fileStream = File.Create(filePath)){
}
return;
}
var input = File.ReadAllText(filePath);
if (string.IsNullOrWhiteSpace(input)){
return;
}
// Deserialize JSON into a dictionary to get top-level properties
var propertiesPresentInJson = GetTopLevelPropertiesInJson(input);
// Deserialize JSON into the provided options object type
var loadedOptions = JsonConvert.DeserializeObject<T>(input);
if (loadedOptions == null){
return;
}
foreach (PropertyInfo property in typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance)){
// Use the JSON property name if present, otherwise use the property name
string jsonPropertyName = property.Name;
var jsonPropertyAttribute = property.GetCustomAttribute<JsonPropertyAttribute>();
if (jsonPropertyAttribute != null){
jsonPropertyName = jsonPropertyAttribute.PropertyName ?? property.Name;
}
if (propertiesPresentInJson.Contains(jsonPropertyName)){
// Update the target property
var value = property.GetValue(loadedOptions);
var targetProperty = options.GetType().GetProperty(property.Name);
if (targetProperty != null && targetProperty.CanWrite){
targetProperty.SetValue(options, value);
}
}
}
}
private static HashSet<string> GetTopLevelPropertiesInJson(string jsonContent){
var properties = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
using (var reader = new JsonTextReader(new StringReader(jsonContent))){
while (reader.Read()){
if (reader.TokenType == JsonToken.PropertyName){
properties.Add(reader.Value?.ToString() ?? string.Empty);
}
}
}
return properties;
}
public static void UpdateHistoryFile(){
if (!CrunchyrollManager.Instance.CrunOptions.History){
return;
}
WriteJsonToFileCompressed(PathCrHistory, CrunchyrollManager.Instance.HistoryList);
}
private static readonly ConcurrentDictionary<string, object> _pathLocks =
new(OperatingSystem.IsWindows()
? StringComparer.OrdinalIgnoreCase
: StringComparer.Ordinal);
public static void WriteJsonToFileCompressed(string pathToFile, object obj, int keepBackups = 5){
string? directoryPath = Path.GetDirectoryName(pathToFile);
if (string.IsNullOrEmpty(directoryPath))
directoryPath = Environment.CurrentDirectory;
Directory.CreateDirectory(directoryPath);
string key = Path.GetFullPath(pathToFile);
object gate = _pathLocks.GetOrAdd(key, _ => new object());
lock (gate){
string tmp = Path.Combine(
directoryPath,
"." + Path.GetFileName(pathToFile) + "." + Guid.NewGuid().ToString("N") + ".tmp");
try{
var fso = new FileStreamOptions{
Mode = FileMode.CreateNew,
Access = FileAccess.Write,
Share = FileShare.None,
BufferSize = 64 * 1024,
Options = FileOptions.WriteThrough
};
using (var fs = new FileStream(tmp, fso))
using (var gzip = new GZipStream(fs, CompressionLevel.Optimal, leaveOpen: false))
using (var sw = new StreamWriter(gzip))
using (var jw = new JsonTextWriter(sw){ Formatting = Formatting.Indented }){
var serializer = new JsonSerializer();
serializer.Serialize(jw, obj);
}
if (File.Exists(pathToFile)){
string backupPath = GetDailyBackupPath(pathToFile, DateTime.Today);
File.Replace(tmp, pathToFile, backupPath, ignoreMetadataErrors: true);
PruneBackups(pathToFile, keepBackups);
} else{
File.Move(tmp, pathToFile, overwrite: true);
}
} catch (Exception ex){
try{
if (File.Exists(tmp)) File.Delete(tmp);
} catch{
/* ignore */
}
Console.Error.WriteLine($"An error occurred writing {pathToFile}: {ex.Message}");
}
}
}
private static string GetDailyBackupPath(string pathToFile, DateTime date){
string dir = Path.GetDirectoryName(pathToFile)!;
string name = Path.GetFileName(pathToFile);
string backupName = $".{name}.{date:yyyy-MM-dd}.bak";
return Path.Combine(dir, backupName);
}
private static void PruneBackups(string pathToFile, int keep){
string dir = Path.GetDirectoryName(pathToFile)!;
string name = Path.GetFileName(pathToFile);
// Backups: .<name>.YYYY-MM-DD.bak
string glob = $".{name}.*.bak";
var rx = new Regex(@"^\." + Regex.Escape(name) + @"\.(\d{4}-\d{2}-\d{2})\.bak$", RegexOptions.CultureInvariant);
var datedBackups = new List<(string Path, DateTime Date)>();
foreach (var path in Directory.EnumerateFiles(dir, glob, SearchOption.TopDirectoryOnly)){
string file = Path.GetFileName(path);
var m = rx.Match(file);
if (!m.Success) continue;
if (DateTime.TryParseExact(m.Groups[1].Value, "yyyy-MM-dd", CultureInfo.InvariantCulture,
DateTimeStyles.None, out var d)){
datedBackups.Add((path, d));
}
}
// Newest first
foreach (var old in datedBackups
.OrderByDescending(x => x.Date)
.Skip(Math.Max(keep, 0))){
try{
File.Delete(old.Path);
} catch(Exception ex){
Console.Error.WriteLine("[Backup] - Failed to delete old backups: " + ex.Message);
}
}
}
private static object fileLock = new object();
public static void WriteJsonToFile(string pathToFile, object obj){
try{
// Check if the directory exists; if not, create it.
string directoryPath = Path.GetDirectoryName(pathToFile);
if (!Directory.Exists(directoryPath)){
Directory.CreateDirectory(directoryPath);
}
lock (fileLock){
using (var fileStream = new FileStream(pathToFile, FileMode.Create, FileAccess.Write, FileShare.None))
using (var streamWriter = new StreamWriter(fileStream))
using (var jsonWriter = new JsonTextWriter(streamWriter){ Formatting = Formatting.Indented }){
var serializer = new JsonSerializer();
serializer.Serialize(jsonWriter, obj);
}
}
} catch (Exception ex){
Console.Error.WriteLine($"An error occurred: {ex.Message}");
}
}
public static string? DecompressJsonFile(string pathToFile){
try{
var fso = new FileStreamOptions{
Mode = FileMode.Open,
Access = FileAccess.Read,
Share = FileShare.ReadWrite | FileShare.Delete,
Options = FileOptions.SequentialScan
};
using var fs = new FileStream(pathToFile, fso);
Span<byte> hdr = stackalloc byte[2];
int read = fs.Read(hdr);
fs.Position = 0;
bool looksGzip = read >= 2 && hdr[0] == 0x1F && hdr[1] == 0x8B;
if (looksGzip){
using var gzip = new GZipStream(fs, CompressionMode.Decompress, leaveOpen: false);
using var sr = new StreamReader(gzip, Encoding.UTF8, detectEncodingFromByteOrderMarks: true);
return sr.ReadToEnd();
} else{
using var sr = new StreamReader(fs, Encoding.UTF8, detectEncodingFromByteOrderMarks: true);
return sr.ReadToEnd();
}
} catch (FileNotFoundException){
return null;
} catch (Exception ex){
Console.Error.WriteLine($"Read failed for {pathToFile}: {ex.Message}");
return null;
}
}
public static bool CheckIfFileExists(string filePath){
string dirPath = Path.GetDirectoryName(filePath) ?? string.Empty;
return Directory.Exists(dirPath) && File.Exists(filePath);
}
public static T? ReadJsonFromFile<T>(string pathToFile) where T : class{
try{
if (!File.Exists(pathToFile)){
throw new FileNotFoundException($"The file at path {pathToFile} does not exist.");
}
lock (fileLock){
using (var fileStream = new FileStream(pathToFile, FileMode.Open, FileAccess.Read))
using (var streamReader = new StreamReader(fileStream))
using (var jsonReader = new JsonTextReader(streamReader)){
var serializer = new JsonSerializer();
return serializer.Deserialize<T>(jsonReader);
}
}
} catch (Exception ex){
Console.Error.WriteLine($"An error occurred while reading the JSON file: {ex.Message}");
return null;
}
}
}