mirror of
https://github.com/Crunchy-DL/Crunchy-Downloader.git
synced 2026-01-11 20:10:26 +00:00
Add - Added **Include Crunchyroll Artists (Music)** in history to settings for expanded tracking Add - Added **filters to history tab** to hide series or artists for a cleaner view Add - Added a **toggle to include featured music** in series search results Chg - Made small changes to **sync timing** to more accurately detect delays Chg - Migrated settings to json file Fix - Fixed a **sync timing issue** with longer comparison videos to ensure proper synchronization Fix - Fixed issues with artist urls
372 lines
No EOL
15 KiB
C#
372 lines
No EOL
15 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.IO.Compression;
|
|
using System.Reflection;
|
|
using System.Runtime.InteropServices;
|
|
using CRD.Downloader;
|
|
using CRD.Downloader.Crunchyroll;
|
|
using CRD.Utils.Structs.Crunchyroll;
|
|
using Newtonsoft.Json;
|
|
using YamlDotNet.RepresentationModel;
|
|
using YamlDotNet.Serialization;
|
|
using YamlDotNet.Serialization.NamingConventions;
|
|
|
|
namespace CRD.Utils.Files;
|
|
|
|
public class CfgManager{
|
|
private static string WorkingDirectory = AppContext.BaseDirectory;
|
|
|
|
public static readonly string PathCrTokenOld = Path.Combine(WorkingDirectory, "config", "cr_token.yml");
|
|
public static readonly string PathCrDownloadOptionsOld = Path.Combine(WorkingDirectory, "config", "settings.yml");
|
|
|
|
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;
|
|
}
|
|
|
|
|
|
#region YAML OLD
|
|
|
|
public static void UpdateSettingsFromFileYAML(CrDownloadOptionsYaml options){
|
|
string dirPath = Path.GetDirectoryName(PathCrDownloadOptionsOld) ?? string.Empty;
|
|
|
|
if (!Directory.Exists(dirPath)){
|
|
Directory.CreateDirectory(dirPath);
|
|
}
|
|
|
|
if (!File.Exists(PathCrDownloadOptionsOld)){
|
|
using (var fileStream = File.Create(PathCrDownloadOptionsOld)){
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
var input = File.ReadAllText(PathCrDownloadOptionsOld);
|
|
|
|
if (input.Length <= 0){
|
|
return;
|
|
}
|
|
|
|
var deserializer = new DeserializerBuilder()
|
|
.WithNamingConvention(UnderscoredNamingConvention.Instance)
|
|
.IgnoreUnmatchedProperties()
|
|
.Build();
|
|
|
|
var propertiesPresentInYaml = GetTopLevelPropertiesInYaml(input);
|
|
var loadedOptions = deserializer.Deserialize<CrDownloadOptionsYaml>(new StringReader(input));
|
|
var instanceOptions = options;
|
|
|
|
foreach (PropertyInfo property in typeof(CrDownloadOptionsYaml).GetProperties()){
|
|
var yamlMemberAttribute = property.GetCustomAttribute<YamlMemberAttribute>();
|
|
// var jsonMemberAttribute = property.GetCustomAttribute<JsonPropertyAttribute>();
|
|
string yamlPropertyName = yamlMemberAttribute?.Alias ?? property.Name;
|
|
|
|
if (propertiesPresentInYaml.Contains(yamlPropertyName)){
|
|
PropertyInfo? instanceProperty = instanceOptions.GetType().GetProperty(property.Name);
|
|
if (instanceProperty != null && instanceProperty.CanWrite){
|
|
instanceProperty.SetValue(instanceOptions, property.GetValue(loadedOptions));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private static HashSet<string> GetTopLevelPropertiesInYaml(string yamlContent){
|
|
var reader = new StringReader(yamlContent);
|
|
var yamlStream = new YamlStream();
|
|
yamlStream.Load(reader);
|
|
|
|
var properties = new HashSet<string>();
|
|
|
|
if (yamlStream.Documents.Count > 0 && yamlStream.Documents[0].RootNode is YamlMappingNode rootNode){
|
|
foreach (var entry in rootNode.Children){
|
|
if (entry.Key is YamlScalarNode scalarKey){
|
|
properties.Add(scalarKey.Value);
|
|
}
|
|
}
|
|
}
|
|
|
|
return properties;
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
public static void UpdateHistoryFile(){
|
|
if (!CrunchyrollManager.Instance.CrunOptions.History){
|
|
return;
|
|
}
|
|
|
|
WriteJsonToFileCompressed(PathCrHistory, CrunchyrollManager.Instance.HistoryList);
|
|
}
|
|
|
|
private static object fileLock = new object();
|
|
|
|
public static void WriteJsonToFileCompressed(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))
|
|
using (var gzipStream = new GZipStream(fileStream, CompressionLevel.Optimal))
|
|
using (var streamWriter = new StreamWriter(gzipStream))
|
|
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 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))
|
|
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{
|
|
using (var fileStream = new FileStream(pathToFile, FileMode.Open, FileAccess.Read)){
|
|
// Check if the file is compressed
|
|
if (IsFileCompressed(fileStream)){
|
|
// Reset the stream position to the beginning
|
|
fileStream.Position = 0;
|
|
using (var gzipStream = new GZipStream(fileStream, CompressionMode.Decompress))
|
|
using (var streamReader = new StreamReader(gzipStream)){
|
|
return streamReader.ReadToEnd();
|
|
}
|
|
}
|
|
|
|
// If not compressed, read the file as is
|
|
fileStream.Position = 0;
|
|
using (var streamReader = new StreamReader(fileStream)){
|
|
return streamReader.ReadToEnd();
|
|
}
|
|
}
|
|
} catch (Exception ex){
|
|
Console.Error.WriteLine($"An error occurred: {ex.Message}");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private static bool IsFileCompressed(FileStream fileStream){
|
|
// Check the first two bytes for the GZip header
|
|
var buffer = new byte[2];
|
|
fileStream.Read(buffer, 0, 2);
|
|
return buffer[0] == 0x1F && buffer[1] == 0x8B;
|
|
}
|
|
|
|
|
|
public static bool CheckIfFileExists(string filePath){
|
|
string dirPath = Path.GetDirectoryName(filePath) ?? string.Empty;
|
|
|
|
return Directory.Exists(dirPath) && File.Exists(filePath);
|
|
}
|
|
|
|
// public static T DeserializeFromFile<T>(string filePath){
|
|
// var deserializer = new DeserializerBuilder()
|
|
// .Build();
|
|
//
|
|
// using (var reader = new StreamReader(filePath)){
|
|
// return deserializer.Deserialize<T>(reader);
|
|
// }
|
|
// }
|
|
|
|
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;
|
|
}
|
|
}
|
|
} |