From b676f5553419ea5edbbe87c4fc82cfaa1a4eea00 Mon Sep 17 00:00:00 2001 From: Sarp Eren EGILMEZ <2348.sarp.egilmez.2006@gmail.com> Date: Mon, 23 Oct 2023 18:49:52 +0300 Subject: [PATCH 1/4] Move settings and cleanup libraries --- TODO.md | 6 +- src/App.cs | 36 +++-- ...es.en.Designer.cs => Messages.Designer.cs} | 20 ++- .../{Messages.en.resx => Messages.resx} | 3 + src/Properties/Messages.tr.Designer.cs | 6 +- src/Properties/Messages.tr.resx | 3 + src/Properties/Settings.Designer.cs | 81 +++++++++++ src/Properties/Settings.resx | 126 ++++++++++++++++++ src/TYM.csproj | 18 ++- 9 files changed, 266 insertions(+), 33 deletions(-) rename src/Properties/{Messages.en.Designer.cs => Messages.Designer.cs} (94%) rename src/Properties/{Messages.en.resx => Messages.resx} (99%) create mode 100644 src/Properties/Settings.Designer.cs create mode 100644 src/Properties/Settings.resx diff --git a/TODO.md b/TODO.md index 34c7454..1f50b82 100644 --- a/TODO.md +++ b/TODO.md @@ -2,9 +2,9 @@ ## General -- [x] Optimize code and fragment info methods -- [ ] Localization -- [ ] ~~Add web link caching with hash checking~~ +- [x] Move settings to embedded resource +- [x] Remove unused libraries +- [ ] Add comments ## CLI Args diff --git a/src/App.cs b/src/App.cs index 817f918..9f6f624 100644 --- a/src/App.cs +++ b/src/App.cs @@ -1,5 +1,4 @@ using CommandLine; -using Newtonsoft.Json; using SixLabors.ImageSharp.Processing.Processors.Transforms; using System.Net.Http.Headers; @@ -68,35 +67,39 @@ public static class Logger { public enum LogLevel { + Verbose, Info, - Warn, + Warning, Error } private static readonly Dictionary LevelColors = new() { - { LogLevel.Info, 4 }, // Blue - { LogLevel.Warn, 3 }, // Yellow - { LogLevel.Error, 1 } // Red + { LogLevel.Verbose, 7 }, // White + { LogLevel.Info, 4 }, // Blue + { LogLevel.Warning, 3 }, // Yellow + { LogLevel.Error, 1 } // Red }; private static readonly Dictionary LevelPrefix= new() { + { LogLevel.Verbose, Messages.LogCategory_Verbose }, { LogLevel.Info, Messages.LogCategory_Info }, - { LogLevel.Warn, Messages.LogCategory_Warning }, + { LogLevel.Warning, Messages.LogCategory_Warning }, { LogLevel.Error, Messages.LogCategory_Error } }; private static readonly Dictionary LevelExitCodes = new() { + { LogLevel.Verbose, 0 }, { LogLevel.Info, 0 }, - { LogLevel.Warn, 0 }, + { LogLevel.Warning, 0 }, { LogLevel.Error, 1 } }; public static void LogMsg(LogLevel Level, string Message) { - Console.WriteLine($"\u001b[3{LevelColors[Level]}m{LevelPrefix[Level]}:\u001b[37m {Message}\u001b[39m"); + Console.WriteLine($"\u001b[3{LevelColors[Level]}m{LevelPrefix[Level]}{((LevelPrefix[Level].Length > 0) ? ": " : "")}\u001b[37m{Message}\u001b[39m"); } public static void LogExit(LogLevel Level, string Message) @@ -129,23 +132,14 @@ public App(string[] Arguments) private void Run(Options CommandLineOptions) { - Assembly ExecutingAssembly = Assembly.GetExecutingAssembly(); - Stream SettingsStream = ExecutingAssembly.GetManifestResourceStream("TYM.settings.json"); - string SettingsContent = new StreamReader(SettingsStream).ReadToEnd(); - - dynamic Settings = JsonConvert.DeserializeObject(SettingsContent); - if (CommandLineOptions.ListResamplers) { - Console.WriteLine("\u001b[32mAvailable resampling algorithms:"); - Console.Write("\x1b[33m"); - typeof(KnownResamplers).GetProperties().ToList().ForEach(x => Console.WriteLine(x.Name)); - Console.WriteLine("\x1b[39m"); - + LogMsg(LogLevel.Info, Messages.Message_AvailableResamplers); + typeof(KnownResamplers).GetProperties().ToList().ForEach(x => LogMsg(LogLevel.Verbose, x.Name)); Environment.Exit(0); } - string DownloadDirectory = Path.Combine(Path.GetTempPath(), (string)Settings["tempDirectoryName"]); + string DownloadDirectory = Path.Combine(Path.GetTempPath(), Settings.tempDirectoryName); if (CommandLineOptions.ClearCache) { Directory.Delete(DownloadDirectory, true); @@ -164,7 +158,7 @@ private void Run(Options CommandLineOptions) { HttpClient Client = new(); - List SupportedMimeTypes = ((string[])Settings["supportedImageFormats"]).ToList(); + List SupportedMimeTypes = Settings.supportedImageFormats.Split(",").Select(x => x = $"image/{x}").ToList(); SupportedMimeTypes.ForEach(x => Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(x))); Task GetTask = Client.GetAsync(WebURI); diff --git a/src/Properties/Messages.en.Designer.cs b/src/Properties/Messages.Designer.cs similarity index 94% rename from src/Properties/Messages.en.Designer.cs rename to src/Properties/Messages.Designer.cs index fbf22c7..4584834 100644 --- a/src/Properties/Messages.en.Designer.cs +++ b/src/Properties/Messages.Designer.cs @@ -60,6 +60,15 @@ internal Messages() { } } + /// + /// Looks up a localized string similar to Clear downloaded cache folder.. + /// + internal static string Args_ClearCache { + get { + return ResourceManager.GetString("Args.ClearCache", resourceCulture); + } + } + /// /// Looks up a localized string similar to Path to the image file. Also supports web links. (DANGER: USE WEB LINKS AT YOUR OWN RISK!). /// @@ -187,7 +196,7 @@ internal static string Error_ResamplerError { } /// - /// Looks up a localized string similar to Server returned code %1!. + /// Looks up a localized string similar to Server returned code %s!. /// internal static string Error_ResponseCode { get { @@ -231,6 +240,15 @@ internal static string LogCategory_Info { } } + /// + /// Looks up a localized string similar to . + /// + internal static string LogCategory_Verbose { + get { + return ResourceManager.GetString("LogCategory.Verbose", resourceCulture); + } + } + /// /// Looks up a localized string similar to WARNING. /// diff --git a/src/Properties/Messages.en.resx b/src/Properties/Messages.resx similarity index 99% rename from src/Properties/Messages.en.resx rename to src/Properties/Messages.resx index ce867fa..8b6f523 100644 --- a/src/Properties/Messages.en.resx +++ b/src/Properties/Messages.resx @@ -177,6 +177,9 @@ INFO + + + WARNING diff --git a/src/Properties/Messages.tr.Designer.cs b/src/Properties/Messages.tr.Designer.cs index 2635c53..d63ae27 100644 --- a/src/Properties/Messages.tr.Designer.cs +++ b/src/Properties/Messages.tr.Designer.cs @@ -22,14 +22,14 @@ namespace TYM.Properties { [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - internal class Messages___Copy { + internal class Messages_tr { private static global::System.Resources.ResourceManager resourceMan; private static global::System.Globalization.CultureInfo resourceCulture; [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] - internal Messages___Copy() { + internal Messages_tr() { } /// @@ -39,7 +39,7 @@ internal Messages___Copy() { internal static global::System.Resources.ResourceManager ResourceManager { get { if (object.ReferenceEquals(resourceMan, null)) { - global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("TYM.Properties.Messages - Copy", typeof(Messages___Copy).Assembly); + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("TYM.Properties.Messages.tr", typeof(Messages_tr).Assembly); resourceMan = temp; } return resourceMan; diff --git a/src/Properties/Messages.tr.resx b/src/Properties/Messages.tr.resx index 2b96bdc..c124dfb 100644 --- a/src/Properties/Messages.tr.resx +++ b/src/Properties/Messages.tr.resx @@ -177,6 +177,9 @@ BILGI + + + UYARI diff --git a/src/Properties/Settings.Designer.cs b/src/Properties/Settings.Designer.cs new file mode 100644 index 0000000..78e4249 --- /dev/null +++ b/src/Properties/Settings.Designer.cs @@ -0,0 +1,81 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace TYM.Properties { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Settings { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Settings() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("TYM.Properties.Settings", typeof(Settings).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to jpeg,bmp,gif,png,tiff,webp. + /// + internal static string supportedImageFormats { + get { + return ResourceManager.GetString("supportedImageFormats", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to TYMwebfetch. + /// + internal static string tempDirectoryName { + get { + return ResourceManager.GetString("tempDirectoryName", resourceCulture); + } + } + } +} diff --git a/src/Properties/Settings.resx b/src/Properties/Settings.resx new file mode 100644 index 0000000..428280d --- /dev/null +++ b/src/Properties/Settings.resx @@ -0,0 +1,126 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + jpeg,bmp,gif,png,tiff,webp + + + TYMwebfetch + + \ No newline at end of file diff --git a/src/TYM.csproj b/src/TYM.csproj index f9193c0..10da548 100644 --- a/src/TYM.csproj +++ b/src/TYM.csproj @@ -63,7 +63,6 @@ - @@ -73,10 +72,15 @@ True True - + True True - Messages.en.resx + Messages.resx + + + True + True + Settings.resx @@ -85,9 +89,13 @@ Messages.tr.Designer.cs ResXFileCodeGenerator - + + ResXFileCodeGenerator + Messages.Designer.cs + + ResXFileCodeGenerator - Messages.en.Designer.cs + Settings.Designer.cs From 1318a6e996a71a0097a9b7c88fd4f60c8d83a52e Mon Sep 17 00:00:00 2001 From: Sarp Eren EGILMEZ <2348.sarp.egilmez.2006@gmail.com> Date: Mon, 23 Oct 2023 18:56:20 +0300 Subject: [PATCH 2/4] Added cli option to open cache folder in explorer --- src/App.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/App.cs b/src/App.cs index 9f6f624..8821480 100644 --- a/src/App.cs +++ b/src/App.cs @@ -1,6 +1,6 @@ using CommandLine; using SixLabors.ImageSharp.Processing.Processors.Transforms; - +using System.Diagnostics; using System.Net.Http.Headers; using System.Reflection; @@ -61,6 +61,9 @@ public class Options [Option('c', "clear", Required = false, Default = false, HelpText = "Clear downloaded cache folder.")] public bool ClearCache { get; set; } + + [Option('o', "open-cache-folder", Required = false, Default = false, HelpText = "Open downloaded cache folder in explorer.")] + public bool OpenCacheFolder { get; set; } } public static class Logger @@ -140,6 +143,15 @@ private void Run(Options CommandLineOptions) } string DownloadDirectory = Path.Combine(Path.GetTempPath(), Settings.tempDirectoryName); + if (CommandLineOptions.OpenCacheFolder && Directory.Exists(DownloadDirectory)) + { + Process.Start(new ProcessStartInfo() + { + Arguments = DownloadDirectory, + FileName = "explorer.exe" + }); + } + if (CommandLineOptions.ClearCache) { Directory.Delete(DownloadDirectory, true); From 9cd610543e86f8c35ec2d92891537cd285b9e196 Mon Sep 17 00:00:00 2001 From: Sarp Eren EGILMEZ <2348.sarp.egilmez.2006@gmail.com> Date: Mon, 23 Oct 2023 19:16:23 +0300 Subject: [PATCH 3/4] Revert cli option to retain cross-platform stability --- TODO.md | 2 +- src/App.cs | 11 ----------- 2 files changed, 1 insertion(+), 12 deletions(-) diff --git a/TODO.md b/TODO.md index 1f50b82..ec7d470 100644 --- a/TODO.md +++ b/TODO.md @@ -8,4 +8,4 @@ ## CLI Args -- [ ] Open web cache directory in explorer \ No newline at end of file +- [ ] ~~Open web cache directory in explorer~~ \ No newline at end of file diff --git a/src/App.cs b/src/App.cs index 8821480..62fbcc2 100644 --- a/src/App.cs +++ b/src/App.cs @@ -62,8 +62,6 @@ public class Options [Option('c', "clear", Required = false, Default = false, HelpText = "Clear downloaded cache folder.")] public bool ClearCache { get; set; } - [Option('o', "open-cache-folder", Required = false, Default = false, HelpText = "Open downloaded cache folder in explorer.")] - public bool OpenCacheFolder { get; set; } } public static class Logger @@ -143,15 +141,6 @@ private void Run(Options CommandLineOptions) } string DownloadDirectory = Path.Combine(Path.GetTempPath(), Settings.tempDirectoryName); - if (CommandLineOptions.OpenCacheFolder && Directory.Exists(DownloadDirectory)) - { - Process.Start(new ProcessStartInfo() - { - Arguments = DownloadDirectory, - FileName = "explorer.exe" - }); - } - if (CommandLineOptions.ClearCache) { Directory.Delete(DownloadDirectory, true); From 8953accedf1f7a202f4eb6f54bfd92b9e2ff4fee Mon Sep 17 00:00:00 2001 From: Sarp Eren EGILMEZ <2348.sarp.egilmez.2006@gmail.com> Date: Mon, 23 Oct 2023 19:20:03 +0300 Subject: [PATCH 4/4] Update TODO and change version string --- CHANGELOG.md | 8 +------- TODO.md | 6 ------ src/TYM.csproj | 2 +- 3 files changed, 2 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eef98fa..ce2e7ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,12 +2,6 @@ ## General -- [x] Optimize code and fragment info methods -- [x] Fix workflows (publishing) - [x] Move settings to embedded resource -- [x] Optimize web requests -- [x] Create console logging utility +- [x] Remove unused libraries -## CLI Args - -- [x] Clear web download cache diff --git a/TODO.md b/TODO.md index ec7d470..ddd66d7 100644 --- a/TODO.md +++ b/TODO.md @@ -2,10 +2,4 @@ ## General -- [x] Move settings to embedded resource -- [x] Remove unused libraries - [ ] Add comments - -## CLI Args - -- [ ] ~~Open web cache directory in explorer~~ \ No newline at end of file diff --git a/src/TYM.csproj b/src/TYM.csproj index 10da548..d639043 100644 --- a/src/TYM.csproj +++ b/src/TYM.csproj @@ -20,7 +20,7 @@ en GPL-3.0-only True - 1.0.1 + 1.0.2 $(AssemblyVersion) res\icon.ico res\project_icon.png