Skip to content

Commit

Permalink
adding logging support
Browse files Browse the repository at this point in the history
  • Loading branch information
NoobNotFound committed Dec 2, 2024
1 parent f59453a commit cddab53
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 48 deletions.
2 changes: 1 addition & 1 deletion Emerald.CoreX/Store/Modrinth/ModrinthStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; } = [];

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Emerald.CoreX/Store/Modrinth/Stores.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public ModpackStore(ILogger logger) : this(new MinecraftPath(MinecraftPath.GetOS
public override async Task DownloadItemAsync(ItemFile file, string projectType,
IProgress<double>? 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);
}
}
46 changes: 41 additions & 5 deletions Emerald/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
Expand All @@ -14,31 +20,61 @@ 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<IMyService, MyService>();
ConfigureServices(services);
})
);
MainWindow = builder.Window;


MainWindow = builder.Window;
#if DEBUG
MainWindow.UseStudio();
#endif
MainWindow.SetWindowIcon();

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)
Expand Down
2 changes: 2 additions & 0 deletions Emerald/Emerald.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@
Lottie;
Hosting;
Toolkit;
Logging;
Mvvm;
Localization;
LoggingSerilog;
ThemeService;
</UnoFeatures>
</PropertyGroup>
Expand Down
1 change: 1 addition & 0 deletions Emerald/Helpers/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
59 changes: 59 additions & 0 deletions Emerald/Helpers/Services/ServiceProviderLocator.cs
Original file line number Diff line number Diff line change
@@ -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<object> GetAllInstances(Type serviceType)
{
// I asked AI and told me this
var services = (IEnumerable<object>)_serviceProvider.GetService(typeof(IEnumerable<>).MakeGenericType(serviceType));
return services ?? Array.Empty<object>();
}

public TService GetInstance<TService>()
{
return (TService)GetInstance(typeof(TService));
}

public TService GetInstance<TService>(string key)
{
return (TService)GetInstance(typeof(TService), key);
}

public IEnumerable<TService> GetAllInstances<TService>()
{
return (IEnumerable<TService>)GetAllInstances(typeof(TService));
}
}
23 changes: 17 additions & 6 deletions Emerald/Views/Settings/AppearancePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>

public sealed partial class AppearancePage : Page
{

Expand Down Expand Up @@ -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}");
}
}
}
Expand All @@ -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()
{
Expand All @@ -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();
Expand Down
54 changes: 19 additions & 35 deletions Emerald/Views/Settings/GeneralPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down

0 comments on commit cddab53

Please sign in to comment.