2025-07-23 15:08:25 +02:00
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
using ImageCatalog;
|
2024-10-14 22:18:03 +02:00
|
|
|
|
using ImageCatalog_2.Services;
|
2025-07-23 17:16:06 +02:00
|
|
|
|
using MaddoShared;
|
2024-10-14 22:18:03 +02:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2026-02-04 23:16:06 +01:00
|
|
|
|
using AutoMapper;
|
2025-07-23 15:08:25 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2025-07-28 14:45:03 +02:00
|
|
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
|
|
|
|
using Microsoft.Extensions.Logging.Console;
|
2026-02-15 11:14:19 +01:00
|
|
|
|
using System.IO;
|
2025-07-28 14:45:03 +02:00
|
|
|
|
using Microsoft.Extensions.Options;
|
2024-10-14 22:18:03 +02:00
|
|
|
|
|
2025-07-28 14:45:03 +02:00
|
|
|
|
namespace ImageCatalog_2;
|
|
|
|
|
|
|
|
|
|
|
|
static class Program
|
2024-10-14 22:18:03 +02:00
|
|
|
|
{
|
2025-07-28 14:45:03 +02:00
|
|
|
|
[DllImport("kernel32.dll", SetLastError = true)]
|
|
|
|
|
|
private static extern bool AllocConsole();
|
2026-02-14 21:14:06 +01:00
|
|
|
|
|
2025-07-28 14:45:03 +02:00
|
|
|
|
[DllImport("kernel32.dll", SetLastError = true)]
|
|
|
|
|
|
static extern IntPtr GetStdHandle(int nStdHandle);
|
2025-07-23 15:08:25 +02:00
|
|
|
|
|
2025-07-28 14:45:03 +02:00
|
|
|
|
private const int STD_OUTPUT_HANDLE = -11;
|
|
|
|
|
|
private const int STD_ERROR_HANDLE = -12;
|
2025-07-23 15:08:25 +02:00
|
|
|
|
|
2025-07-28 14:45:03 +02:00
|
|
|
|
[DllImport("kernel32.dll", SetLastError = true)]
|
|
|
|
|
|
static extern bool SetStdHandle(int nStdHandle, IntPtr handle);
|
2025-07-23 15:08:25 +02:00
|
|
|
|
|
2025-07-28 14:45:03 +02:00
|
|
|
|
[DllImport("kernel32.dll", SetLastError = true)]
|
|
|
|
|
|
static extern IntPtr GetConsoleWindow();
|
2025-07-23 15:08:25 +02:00
|
|
|
|
|
2025-07-28 14:45:03 +02:00
|
|
|
|
[DllImport("kernel32.dll", SetLastError = true)]
|
|
|
|
|
|
static extern bool AttachConsole(int dwProcessId);
|
2025-07-23 15:08:25 +02:00
|
|
|
|
|
2025-07-28 14:45:03 +02:00
|
|
|
|
[DllImport("kernel32.dll", SetLastError = true)]
|
|
|
|
|
|
static extern IntPtr CreateFile(
|
|
|
|
|
|
string lpFileName,
|
|
|
|
|
|
uint dwDesiredAccess,
|
|
|
|
|
|
uint dwShareMode,
|
|
|
|
|
|
IntPtr lpSecurityAttributes,
|
|
|
|
|
|
uint dwCreationDisposition,
|
|
|
|
|
|
uint dwFlagsAndAttributes,
|
|
|
|
|
|
IntPtr hTemplateFile);
|
2025-07-23 15:08:25 +02:00
|
|
|
|
|
2025-07-28 14:45:03 +02:00
|
|
|
|
private const uint GENERIC_WRITE = 0x40000000;
|
|
|
|
|
|
private const uint OPEN_EXISTING = 3;
|
2025-07-23 15:08:25 +02:00
|
|
|
|
|
2025-07-28 14:45:03 +02:00
|
|
|
|
private static void RedirectConsoleOutput()
|
|
|
|
|
|
{
|
|
|
|
|
|
var stdOutHandle = CreateFile("CONOUT$", GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
|
|
|
|
|
|
var safeFileHandle = new Microsoft.Win32.SafeHandles.SafeFileHandle(stdOutHandle, true);
|
|
|
|
|
|
var fileStream = new FileStream(safeFileHandle, FileAccess.Write);
|
|
|
|
|
|
var standardOutput = new StreamWriter(fileStream) { AutoFlush = true };
|
|
|
|
|
|
Console.SetOut(standardOutput);
|
|
|
|
|
|
Console.SetError(standardOutput);
|
|
|
|
|
|
}
|
2026-02-14 21:14:06 +01:00
|
|
|
|
|
2025-07-28 14:45:03 +02:00
|
|
|
|
public static IServiceProvider ServiceProvider { get; private set; }
|
|
|
|
|
|
[STAThread]
|
|
|
|
|
|
static void Main()
|
|
|
|
|
|
{
|
|
|
|
|
|
Application.SetHighDpiMode(HighDpiMode.SystemAware);
|
|
|
|
|
|
Application.EnableVisualStyles();
|
|
|
|
|
|
Application.SetCompatibleTextRenderingDefault(false);
|
2024-10-14 22:18:03 +02:00
|
|
|
|
|
2025-07-28 14:45:03 +02:00
|
|
|
|
AllocConsole();
|
|
|
|
|
|
RedirectConsoleOutput();
|
2026-02-14 21:14:06 +01:00
|
|
|
|
|
2025-07-28 14:45:03 +02:00
|
|
|
|
var serviceCollection = new ServiceCollection();
|
|
|
|
|
|
ConfigureServices(serviceCollection);
|
2024-10-14 22:18:03 +02:00
|
|
|
|
|
2025-07-28 14:45:03 +02:00
|
|
|
|
ServiceProvider = serviceCollection.BuildServiceProvider();
|
2024-10-14 22:18:03 +02:00
|
|
|
|
|
2026-02-15 11:14:19 +01:00
|
|
|
|
// Resolve WPF MainWindow when available, otherwise fall back to WinForms MainForm
|
|
|
|
|
|
var serviceProvider = ServiceProvider;
|
|
|
|
|
|
|
|
|
|
|
|
// NOTE: By default the app will attempt to run the WPF window when available and fall back
|
|
|
|
|
|
// to the WinForms MainForm. If you want to force the WinForms UI for testing, uncomment
|
|
|
|
|
|
// the block below and comment out the WPF branch.
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
// // Force WinForms UI (uncomment to enable)
|
2026-02-16 21:49:46 +01:00
|
|
|
|
//var mainForm = serviceProvider.GetRequiredService<MainForm>();
|
|
|
|
|
|
//// If you want to set the DataModel explicitly on the WinForms form use the lines below
|
|
|
|
|
|
//// var mainViewModel = serviceProvider.GetRequiredService<DataModel>();
|
|
|
|
|
|
//// mainForm.Model = mainViewModel;
|
|
|
|
|
|
//Application.Run(mainForm);
|
2026-02-15 11:14:19 +01:00
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
2026-02-21 16:49:13 +01:00
|
|
|
|
// Create the WPF Application and merge MahApps resources BEFORE constructing the MainWindow
|
|
|
|
|
|
// so InitializeComponent sees the theme resources on first render.
|
|
|
|
|
|
var wpfApp = new System.Windows.Application();
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
wpfApp.Resources.MergedDictionaries.Add(new System.Windows.ResourceDictionary { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml") });
|
|
|
|
|
|
wpfApp.Resources.MergedDictionaries.Add(new System.Windows.ResourceDictionary { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml") });
|
|
|
|
|
|
// Default Light theme (can be replaced at runtime)
|
|
|
|
|
|
wpfApp.Resources.MergedDictionaries.Add(new System.Windows.ResourceDictionary { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Themes/Light.Blue.xaml") });
|
|
|
|
|
|
|
|
|
|
|
|
// Also notify ThemeManager about initial theme so chrome and MahApps brushes are applied
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
ControlzEx.Theming.ThemeManager.Current.ChangeTheme(wpfApp, "Light.Blue");
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
// ignore if ThemeManager API isn't present
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
// If resources fail to load (package not present at runtime), continue silently
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Now resolve the WPF MainWindow so its constructor runs with the application resources available
|
|
|
|
|
|
var wpfMain = serviceProvider.GetService(typeof(ImageCatalog_2.MainWindow)) as ImageCatalog_2.MainWindow;
|
|
|
|
|
|
if (wpfMain is not null)
|
2026-02-16 21:49:46 +01:00
|
|
|
|
{
|
2026-02-21 16:49:13 +01:00
|
|
|
|
wpfApp.Run(wpfMain);
|
|
|
|
|
|
return;
|
2026-02-16 21:49:46 +01:00
|
|
|
|
}
|
2026-02-21 16:49:13 +01:00
|
|
|
|
|
|
|
|
|
|
// Fallback to WinForms UI
|
|
|
|
|
|
var mainForm = serviceProvider.GetRequiredService<MainForm>();
|
|
|
|
|
|
System.Windows.Forms.Application.Run(mainForm);
|
2025-07-28 14:45:03 +02:00
|
|
|
|
}
|
2024-10-14 22:18:03 +02:00
|
|
|
|
|
2025-07-28 14:45:03 +02:00
|
|
|
|
private static void ConfigureServices(ServiceCollection services)
|
|
|
|
|
|
{
|
2026-02-04 23:16:06 +01:00
|
|
|
|
// Register AutoMapper (new AddAutoMapper overload — provide config and marker types)
|
|
|
|
|
|
services.AddAutoMapper(cfg => { }, typeof(Program));
|
|
|
|
|
|
|
2025-07-28 14:45:03 +02:00
|
|
|
|
// Register your services here
|
|
|
|
|
|
services.AddTransient<ITestService, TestService>();
|
2026-02-04 19:48:03 +01:00
|
|
|
|
services.AddTransient<ISettingsService, SettingsService>();
|
2024-10-14 22:18:03 +02:00
|
|
|
|
|
2026-02-14 22:18:56 +01:00
|
|
|
|
services.AddTransient<DataModel>(sp =>
|
|
|
|
|
|
{
|
|
|
|
|
|
// Resolve optional version provider and pass to DataModel
|
|
|
|
|
|
var testService = sp.GetRequiredService<ITestService>();
|
|
|
|
|
|
var settingsService = sp.GetRequiredService<ISettingsService>();
|
2026-02-21 15:53:52 +01:00
|
|
|
|
var imageCreation = sp.GetRequiredService<ImageCreationService>();
|
2026-02-14 22:18:56 +01:00
|
|
|
|
var picSettings = sp.GetRequiredService<PicSettings>();
|
|
|
|
|
|
var mapper = sp.GetRequiredService<IMapper>();
|
|
|
|
|
|
var logger = sp.GetRequiredService<ILogger<DataModel>>();
|
|
|
|
|
|
var versionProvider = sp.GetService<MaddoShared.IVersionProvider>();
|
|
|
|
|
|
|
|
|
|
|
|
return new DataModel(testService, settingsService, imageCreation, picSettings, mapper, logger, versionProvider);
|
|
|
|
|
|
});
|
2024-10-14 23:05:18 +02:00
|
|
|
|
|
2026-02-21 15:53:52 +01:00
|
|
|
|
services.AddTransient<ImageCreationService>();
|
|
|
|
|
|
services.AddTransient<ImageCreatorGDI>();
|
|
|
|
|
|
services.AddTransient<ImageCreatorImageSharp>();
|
2026-02-15 00:14:04 +01:00
|
|
|
|
services.AddTransient<ImageCreatorMapper>();
|
|
|
|
|
|
|
|
|
|
|
|
// Register IImageCreator to be resolved via ImageCreatorMapper which selects concrete implementation at call time
|
|
|
|
|
|
services.AddTransient<IImageCreator>(sp => sp.GetRequiredService<ImageCreatorMapper>());
|
2025-07-28 09:15:45 +02:00
|
|
|
|
|
2026-02-14 20:38:51 +01:00
|
|
|
|
// Register a ParametriSetup singleton that persists user preferences in LocalApplicationData
|
|
|
|
|
|
var userPrefsPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
|
|
|
|
|
"ImageCatalog", "userprefs.xml");
|
|
|
|
|
|
services.AddSingleton(new ParametriSetup(userPrefsPath));
|
2025-07-28 14:45:03 +02:00
|
|
|
|
services.AddSingleton<PicSettings>();
|
2026-02-14 21:14:06 +01:00
|
|
|
|
|
2025-07-28 14:45:03 +02:00
|
|
|
|
// Register your forms
|
|
|
|
|
|
services.AddTransient<MainForm>();
|
2026-02-15 11:14:19 +01:00
|
|
|
|
// Register WPF MainWindow so it can be resolved with the existing DataModel
|
|
|
|
|
|
services.AddTransient<ImageCatalog_2.MainWindow>();
|
2025-07-28 14:45:03 +02:00
|
|
|
|
|
2026-02-14 21:14:06 +01:00
|
|
|
|
// Version provider for UI and logging
|
|
|
|
|
|
services.AddSingleton<MaddoShared.IVersionProvider, MaddoShared.VersionProvider>();
|
|
|
|
|
|
|
2025-07-28 14:45:03 +02:00
|
|
|
|
services.AddLogging(configure =>
|
|
|
|
|
|
{
|
|
|
|
|
|
configure.AddCustomFormatter();
|
|
|
|
|
|
configure.AddConsole();
|
|
|
|
|
|
configure.SetMinimumLevel(LogLevel.Debug);
|
|
|
|
|
|
});
|
2024-10-14 22:18:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-28 14:45:03 +02:00
|
|
|
|
|
2026-02-14 21:14:06 +01:00
|
|
|
|
public static class ConsoleLoggerExtensions
|
|
|
|
|
|
{
|
|
|
|
|
|
public static ILoggingBuilder AddCustomFormatter(
|
|
|
|
|
|
this ILoggingBuilder builder) =>
|
|
|
|
|
|
builder
|
|
|
|
|
|
.AddConsole(options => options.FormatterName = nameof(CustomLoggingFormatter))
|
|
|
|
|
|
.AddConsoleFormatter<CustomLoggingFormatter, ConsoleFormatterOptions>()
|
|
|
|
|
|
.AddFilter("LuckyPennySoftware.AutoMapper.License", LogLevel.None);
|
|
|
|
|
|
}
|
|
|
|
|
|
public sealed class CustomLoggingFormatter : ConsoleFormatter, IDisposable
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IDisposable _optionsReloadToken;
|
|
|
|
|
|
private ConsoleFormatterOptions _formatterOptions;
|
|
|
|
|
|
public CustomLoggingFormatter(IOptionsMonitor<ConsoleFormatterOptions> options)
|
2025-07-28 14:45:03 +02:00
|
|
|
|
// Case insensitive
|
2026-02-14 21:14:06 +01:00
|
|
|
|
: base(nameof(CustomLoggingFormatter)) =>
|
|
|
|
|
|
(_optionsReloadToken, _formatterOptions) =
|
|
|
|
|
|
(options.OnChange(ReloadLoggerOptions), options.CurrentValue);
|
|
|
|
|
|
private void ReloadLoggerOptions(ConsoleFormatterOptions options) =>
|
|
|
|
|
|
_formatterOptions = options;
|
|
|
|
|
|
|
|
|
|
|
|
public override void Write<TState>(
|
|
|
|
|
|
in LogEntry<TState> logEntry,
|
2026-02-15 11:14:19 +01:00
|
|
|
|
IExternalScopeProvider? scopeProvider,
|
|
|
|
|
|
TextWriter? textWriter)
|
2026-02-14 21:14:06 +01:00
|
|
|
|
{
|
|
|
|
|
|
string? message =
|
|
|
|
|
|
logEntry.Formatter?.Invoke(
|
|
|
|
|
|
logEntry.State, logEntry.Exception);
|
|
|
|
|
|
|
|
|
|
|
|
if (message is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
textWriter.WriteLine($"{message}");
|
|
|
|
|
|
}
|
|
|
|
|
|
public void Dispose() => _optionsReloadToken?.Dispose();
|
|
|
|
|
|
}
|