From cddab53ae575f70491af9d5d17b5f14ab93a82d5 Mon Sep 17 00:00:00 2001 From: NoobNotFound Date: Mon, 2 Dec 2024 17:36:09 +0530 Subject: [PATCH] adding logging support --- Emerald.CoreX/Store/Modrinth/ModrinthStore.cs | 2 +- Emerald.CoreX/Store/Modrinth/Stores.cs | 2 +- Emerald/App.xaml.cs | 46 +++++++++++++-- Emerald/Emerald.csproj | 2 + Emerald/Helpers/Extensions.cs | 1 + .../Services/ServiceProviderLocator.cs | 59 +++++++++++++++++++ Emerald/Views/Settings/AppearancePage.xaml.cs | 23 ++++++-- Emerald/Views/Settings/GeneralPage.xaml.cs | 54 ++++++----------- 8 files changed, 141 insertions(+), 48 deletions(-) create mode 100644 Emerald/Helpers/Services/ServiceProviderLocator.cs diff --git a/Emerald.CoreX/Store/Modrinth/ModrinthStore.cs b/Emerald.CoreX/Store/Modrinth/ModrinthStore.cs index d905ee27..540498bb 100644 --- a/Emerald.CoreX/Store/Modrinth/ModrinthStore.cs +++ b/Emerald.CoreX/Store/Modrinth/ModrinthStore.cs @@ -19,7 +19,7 @@ public abstract class ModrinthStore : IModrinthStore protected readonly ILogger _logger; protected readonly string _projectType; protected readonly FileDownloader _fileDownloader; - public MinecraftPath MCPath { get; } + public MinecraftPath MCPath { get; set; } public Category[] Categories { get; private set; } = []; /// diff --git a/Emerald.CoreX/Store/Modrinth/Stores.cs b/Emerald.CoreX/Store/Modrinth/Stores.cs index 9f2b48b0..04853813 100644 --- a/Emerald.CoreX/Store/Modrinth/Stores.cs +++ b/Emerald.CoreX/Store/Modrinth/Stores.cs @@ -191,7 +191,7 @@ public ModpackStore(ILogger logger) : this(new MinecraftPath(MinecraftPath.GetOS public override async Task DownloadItemAsync(ItemFile file, string projectType, IProgress? progress = null, CancellationToken cancellationToken = default) { - // Implement modpack-specific download logic + // Implement modpack-specific download logic. needs to be extracted to use them. await base.DownloadItemAsync(file, "modpacks", progress, cancellationToken); } } diff --git a/Emerald/App.xaml.cs b/Emerald/App.xaml.cs index 94af9297..16414ea8 100644 --- a/Emerald/App.xaml.cs +++ b/Emerald/App.xaml.cs @@ -1,6 +1,12 @@ +using CommonServiceLocator; +using Serilog; +using Serilog.Sinks.File; using Uno.Resizetizer; +using Uno.UI.HotDesign; +using Microsoft.Extensions.DependencyInjection; +using Emerald.CoreX.Store.Modrinth; -namespace Emerald; +namespace Emerald; public partial class App : Application { /// @@ -14,24 +20,49 @@ public App() public Window? MainWindow { get; private set; } protected IHost? Host { get; private set; } + public void ConfigureServices(IServiceCollection services) + { + + services.AddTransient(provider => new ModStore(typeof(ModStore).Log())); + services.AddTransient(provider => new PluginStore(typeof(PluginStore).Log())); + services.AddTransient(provider => new ResourcePackStore(typeof(ResourcePackStore).Log())); + services.AddTransient(provider => new ResourcePackStore(typeof(ShaderStore).Log())); + services.AddTransient(provider => new ModpackStore(typeof(ModpackStore).Log())); + + } protected override void OnLaunched(LaunchActivatedEventArgs args) { + + var logPath = Path.Combine( + Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), + "Emerald", + "logs", + "app_.log"); + + var builder = this.CreateBuilder(args) .Configure(host => host #if DEBUG // Switch to Development environment when running in DEBUG .UseEnvironment(Environments.Development) #endif + + //Enable Logging + .UseSerilog(true, configureLogger: x=> x + .MinimumLevel.Debug() + .WriteTo.File(logPath, + rollingInterval: RollingInterval.Day, + retainedFileCountLimit: 7, + outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] ({SourceContext}.{Method}) {Message}{NewLine}{Exception}")) + .ConfigureServices((context, services) => { - // TODO: Register your services - //services.AddSingleton(); + ConfigureServices(services); }) ); - MainWindow = builder.Window; - + MainWindow = builder.Window; #if DEBUG MainWindow.UseStudio(); #endif @@ -39,6 +70,11 @@ protected override void OnLaunched(LaunchActivatedEventArgs args) Host = builder.Build(); + //Help me. + ServiceLocator.SetLocatorProvider(() => new Emerald.Helpers.Services.ServiceProviderLocator(Host!.Services)); + + this.Log().LogInformation("New Instance was created. Logs are being saved at: {logPath}",logPath); + // Do not repeat app initialization when the Window already has content, // just ensure that the window is active if (MainWindow.Content is not Frame rootFrame) diff --git a/Emerald/Emerald.csproj b/Emerald/Emerald.csproj index 17931f69..9372b37a 100644 --- a/Emerald/Emerald.csproj +++ b/Emerald/Emerald.csproj @@ -39,8 +39,10 @@ Lottie; Hosting; Toolkit; + Logging; Mvvm; Localization; + LoggingSerilog; ThemeService; diff --git a/Emerald/Helpers/Extensions.cs b/Emerald/Helpers/Extensions.cs index 04854cfd..736b98e6 100644 --- a/Emerald/Helpers/Extensions.cs +++ b/Emerald/Helpers/Extensions.cs @@ -80,6 +80,7 @@ public static ContentDialog ToContentDialog(this UIElement content, string title } : content, RequestedTheme = (ElementTheme)Settings.SettingsSystem.Settings.App.Appearance.Theme }; + App.Current.Log().LogInformation("Created ContentDialog with title: {title}", title); return dialog; } diff --git a/Emerald/Helpers/Services/ServiceProviderLocator.cs b/Emerald/Helpers/Services/ServiceProviderLocator.cs new file mode 100644 index 00000000..c0207531 --- /dev/null +++ b/Emerald/Helpers/Services/ServiceProviderLocator.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using CommonServiceLocator; + +namespace Emerald.Helpers.Services; + +//Bruh I can't get uno services to work. help me. + +public class ServiceProviderLocator : IServiceLocator +{ + private readonly IServiceProvider _serviceProvider; + + public ServiceProviderLocator(IServiceProvider serviceProvider) + { + _serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); + } + + public object GetService(Type serviceType) + { + return _serviceProvider.GetService(serviceType) + ?? throw new InvalidOperationException($"Service of type {serviceType.FullName} not found."); + } + + public object GetInstance(Type serviceType) + { + return GetService(serviceType); + } + + public object GetInstance(Type serviceType, string key) + { + // For simplicity, ignoring `key`. + return GetInstance(serviceType); + } + + public IEnumerable GetAllInstances(Type serviceType) + { + // I asked AI and told me this + var services = (IEnumerable)_serviceProvider.GetService(typeof(IEnumerable<>).MakeGenericType(serviceType)); + return services ?? Array.Empty(); + } + + public TService GetInstance() + { + return (TService)GetInstance(typeof(TService)); + } + + public TService GetInstance(string key) + { + return (TService)GetInstance(typeof(TService), key); + } + + public IEnumerable GetAllInstances() + { + return (IEnumerable)GetAllInstances(typeof(TService)); + } +} diff --git a/Emerald/Views/Settings/AppearancePage.xaml.cs b/Emerald/Views/Settings/AppearancePage.xaml.cs index 99a6bea7..595e3367 100644 --- a/Emerald/Views/Settings/AppearancePage.xaml.cs +++ b/Emerald/Views/Settings/AppearancePage.xaml.cs @@ -12,17 +12,14 @@ using Microsoft.UI.Xaml.Input; using Microsoft.UI.Xaml.Media; using Microsoft.UI.Xaml.Navigation; +using Uno.Logging; using Windows.Foundation; using Windows.Foundation.Collections; using Windows.UI; using SS = Emerald.Helpers.Settings.SettingsSystem; -// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238 - namespace Emerald.Views.Settings; -/// -/// An empty page that can be used on its own or navigated to within a Frame. -/// + public sealed partial class AppearancePage : Page { @@ -78,23 +75,31 @@ public sealed partial class AppearancePage : Page Color.FromArgb(255, 126, 115, 95) }; + public AppearancePage() { InitializeComponent(); + this.Log().Info("Initializing AppearancePage..."); + if (SS.Settings.App.Appearance.MicaTintColor == (int)Helpers.Settings.Enums.MicaTintColor.CustomColor) { if (SS.Settings.App.Appearance.CustomMicaTintColor != null) { + this.Log().Info("Custom Mica Tint Color found. Processing..."); var c = SS.Settings.App.Appearance.CustomMicaTintColor; var cl = Color.FromArgb((byte)c.Value.A, (byte)c.Value.R, (byte)c.Value.G, (byte)c.Value.B); if (TintColorsList.Contains(cl)) + { GVColorList.SelectedIndex = TintColorsList.IndexOf(cl); + this.Log().Info($"Custom color matched: {cl}. Selected index: {GVColorList.SelectedIndex}"); + } else { TintColorsList.Add(cl); GVColorList.SelectedIndex = TintColorsList.Count - 1; + this.Log().Info($"Added new custom color: {cl}. Updated selected index: {GVColorList.SelectedIndex}"); } } } @@ -103,13 +108,16 @@ public AppearancePage() private void GVColorList_SelectionChanged(object sender, SelectionChangedEventArgs e) { var c = TintColorsList[GVColorList.SelectedIndex]; - SS.Settings.App.Appearance.MicaTintColor = (int)Helpers.Settings.Enums.MicaTintColor.CustomColor; SS.Settings.App.Appearance.CustomMicaTintColor = (c.A, c.R, c.G, c.B); + + this.Log().Info($"Selected tint color changed to: {c}"); + } private void CustomTintColor_Click(object sender, Microsoft.UI.Xaml.RoutedEventArgs e) { + var c = SS.Settings.App.Appearance.CustomMicaTintColor; var cp = new ColorPicker() { @@ -134,12 +142,15 @@ private void CustomTintColor_Click(object sender, Microsoft.UI.Xaml.RoutedEventA if (TintColorsList.Contains(cl)) { GVColorList.SelectedIndex = TintColorsList.IndexOf(cl); + this.Log().Info($"Color already exists. Selected existing color: {cl}"); } else { TintColorsList.Add(cl); GVColorList.SelectedIndex = TintColorsList.Count - 1; + this.Log().Info($"Added new color: {cl}. Updated selected index: {GVColorList.SelectedIndex}"); } + }; _ = d.ShowAsync(); diff --git a/Emerald/Views/Settings/GeneralPage.xaml.cs b/Emerald/Views/Settings/GeneralPage.xaml.cs index 5f0e5161..a8569390 100644 --- a/Emerald/Views/Settings/GeneralPage.xaml.cs +++ b/Emerald/Views/Settings/GeneralPage.xaml.cs @@ -30,45 +30,29 @@ public GeneralPage() { this.InitializeComponent(); } - private void btnChangeMPath_Click(object sender, RoutedEventArgs e) + private async void btnChangeMPath_Click(object sender, RoutedEventArgs e) { - //MinecraftPath mcP; - //string path = ""; - //async void Start() - //{ - // var fop = new FolderPicker - // { - // CommitButtonText = "Select".Localize() - // }; - // WinRT.Interop.InitializeWithWindow.Initialize(fop, WinRT.Interop.WindowNative.GetWindowHandle(App.Current.MainWindow)); - // var f = await fop.PickSingleFolderAsync(); + this.Log().LogInformation("Choosing MC path"); + string path; - // if (f != null) - // path = f.Path; - // else - // return; + var fop = new FolderPicker + { + CommitButtonText = "Select".Localize() + }; + fop.FileTypeFilter.Add("*"); - // Try(); + var f = await fop.PickSingleFolderAsync(); - // async void Try() - // { - // try - // { - // mcP = new(path); - // SS.Settings.Minecraft.Path = path; - // App.Current.Launcher.InitializeLauncher(mcP); - // } - // catch - // { - // var r = await MessageBox.Show("Error".Localize(), "MCPathFailed".Localize().Replace("{Path}", path), MessageBoxButtons.Custom, "Yes".Localize(), "SetDifMCPath".Localize()); - // if (r == MessageBoxResults.Yes) - // Try(); - // else - // Start(); - // } - // } - //} - //Start(); + if (f != null) + path = f.Path; + else + { + this.Log().LogInformation("User did not select a MC path"); + return; + } + + this.Log().LogInformation("New Minecraft path: {path}",path); + SS.Settings.Minecraft.Path = path; } private void btnRamPlus_Click(object sender, RoutedEventArgs e) =>