using System; using System.Collections.Generic; using System.IO; using System.Runtime.InteropServices; using System.Text; using System.Text.Json; using System.Threading; using System.Threading.Tasks; using Discord.WebSocket; using PluginManager.Items; namespace PluginManager.Others; /// /// A special class with functions /// public static class Functions { /// /// The location for the Resources folder /// public static readonly string dataFolder = @"./Data/Resources/"; /// /// Get the Operating system you are runnin on /// /// An Operating system public static OperatingSystem GetOperatingSystem() { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) return OperatingSystem.WINDOWS; if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) return OperatingSystem.LINUX; if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) return OperatingSystem.MAC_OS; return OperatingSystem.UNKNOWN; } public static List GetArguments(SocketMessage message) { var command = new Command(message); return command.Arguments; } /// /// Copy one Stream to another /// /// The base stream /// The destination stream /// The buffer to read /// The progress /// The cancellation token /// Triggered if any is empty /// Triggered if is less then or equal to 0 /// Triggered if is not readable /// Triggered in is not writable public static async Task CopyToOtherStreamAsync(this Stream stream, Stream destination, int bufferSize, IProgress? progress = null, CancellationToken cancellationToken = default) { if (stream == null) throw new ArgumentNullException(nameof(stream)); if (destination == null) throw new ArgumentNullException(nameof(destination)); if (bufferSize <= 0) throw new ArgumentOutOfRangeException(nameof(bufferSize)); if (!stream.CanRead) throw new InvalidOperationException("The stream is not readable."); if (!destination.CanWrite) throw new ArgumentException("Destination stream is not writable", nameof(destination)); var buffer = new byte[bufferSize]; long totalBytesRead = 0; int bytesRead; while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0) { await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false); totalBytesRead += bytesRead; progress?.Report(totalBytesRead); } } /// /// Save to JSON file /// /// The class type /// The file path /// The values /// public static async Task SaveToJsonFile(string file, T Data) { var str = new MemoryStream(); await JsonSerializer.SerializeAsync(str, Data, typeof(T), new JsonSerializerOptions { WriteIndented = true }); await File.WriteAllBytesAsync(file, str.ToArray()); } /// /// Convert json text or file to some kind of data /// /// The data type /// The file or json text /// public static async Task ConvertFromJson(string input) { Stream text; if (File.Exists(input)) text = new MemoryStream(await File.ReadAllBytesAsync(input)); else text = new MemoryStream(Encoding.ASCII.GetBytes(input)); text.Position = 0; var obj = await JsonSerializer.DeserializeAsync(text); text.Close(); return (obj ?? default)!; } }