233 lines
No EOL
8.3 KiB
C#
233 lines
No EOL
8.3 KiB
C#
using System.Runtime.InteropServices;
|
|
using Catalog.Communication.DependencyInjection;
|
|
using ImageCatalog;
|
|
using ImageCatalog_2.Services;
|
|
using MaddoShared;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using AutoMapper;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Microsoft.Extensions.Logging.Console;
|
|
using System.IO;
|
|
using Microsoft.Extensions.Options;
|
|
using Avalonia;
|
|
|
|
namespace ImageCatalog_2;
|
|
|
|
static class Program
|
|
{
|
|
#if WINDOWS
|
|
[DllImport("kernel32.dll", SetLastError = true)]
|
|
private static extern bool AllocConsole();
|
|
|
|
[DllImport("kernel32.dll", SetLastError = true)]
|
|
private static extern bool FreeConsole();
|
|
|
|
[DllImport("kernel32.dll", SetLastError = true)]
|
|
static extern IntPtr GetStdHandle(int nStdHandle);
|
|
|
|
private const int STD_OUTPUT_HANDLE = -11;
|
|
private const int STD_ERROR_HANDLE = -12;
|
|
|
|
[DllImport("kernel32.dll", SetLastError = true)]
|
|
static extern bool SetStdHandle(int nStdHandle, IntPtr handle);
|
|
|
|
[DllImport("kernel32.dll", SetLastError = true)]
|
|
static extern IntPtr GetConsoleWindow();
|
|
|
|
[DllImport("kernel32.dll", SetLastError = true)]
|
|
static extern bool AttachConsole(int dwProcessId);
|
|
|
|
private const int ATTACH_PARENT_PROCESS = -1;
|
|
|
|
[DllImport("kernel32.dll", SetLastError = true)]
|
|
private static extern bool GenerateConsoleCtrlEvent(uint dwCtrlEvent, uint dwProcessGroupId);
|
|
|
|
[DllImport("kernel32.dll", SetLastError = true)]
|
|
private static extern bool SetConsoleCtrlHandler(ConsoleCtrlDelegate? handlerRoutine, bool add);
|
|
|
|
[DllImport("kernel32.dll", SetLastError = true)]
|
|
static extern IntPtr CreateFile(
|
|
string lpFileName,
|
|
uint dwDesiredAccess,
|
|
uint dwShareMode,
|
|
IntPtr lpSecurityAttributes,
|
|
uint dwCreationDisposition,
|
|
uint dwFlagsAndAttributes,
|
|
IntPtr hTemplateFile);
|
|
|
|
private const uint GENERIC_WRITE = 0x40000000;
|
|
private const uint OPEN_EXISTING = 3;
|
|
private const uint CTRL_C_EVENT = 0;
|
|
|
|
private delegate bool ConsoleCtrlDelegate(uint ctrlType);
|
|
|
|
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);
|
|
}
|
|
|
|
internal static bool TrySendConsoleInterrupt(int processId)
|
|
{
|
|
_ = processId;
|
|
return false;
|
|
}
|
|
#endif
|
|
|
|
public static IServiceProvider ServiceProvider { get; private set; } = default!;
|
|
|
|
public static Avalonia.AppBuilder BuildAvaloniaApp()
|
|
=> Avalonia.AppBuilder.Configure<AvaloniaApp>()
|
|
.UsePlatformDetect()
|
|
.LogToTrace();
|
|
|
|
[STAThread]
|
|
static int Main(string[] args)
|
|
{
|
|
#if WINDOWS
|
|
if (CommandLineOperationRunner.IsHeadlessRequest(args))
|
|
{
|
|
AttachConsole(ATTACH_PARENT_PROCESS);
|
|
}
|
|
else
|
|
{
|
|
AllocConsole();
|
|
}
|
|
|
|
RedirectConsoleOutput();
|
|
#endif
|
|
|
|
var serviceCollection = new ServiceCollection();
|
|
ConfigureServices(serviceCollection);
|
|
|
|
ServiceProvider = serviceCollection.BuildServiceProvider();
|
|
|
|
if (CommandLineOperationRunner.IsHeadlessRequest(args))
|
|
{
|
|
return CommandLineOperationRunner.RunAsync(ServiceProvider, args ?? Array.Empty<string>())
|
|
.GetAwaiter()
|
|
.GetResult();
|
|
}
|
|
|
|
BuildAvaloniaApp().StartWithClassicDesktopLifetime(args ?? Array.Empty<string>());
|
|
return 0;
|
|
}
|
|
|
|
private static void ConfigureServices(ServiceCollection services)
|
|
{
|
|
services.AddAutoMapper(cfg => { }, typeof(Program));
|
|
|
|
services.AddTransient<ITestService, TestService>();
|
|
services.AddTransient<ISettingsService, SettingsService>();
|
|
|
|
services.AddTransient<DataModel>(sp =>
|
|
{
|
|
var testService = sp.GetRequiredService<ITestService>();
|
|
var settingsService = sp.GetRequiredService<ISettingsService>();
|
|
var imageCreation = sp.GetRequiredService<ImageCreationService>();
|
|
var aiExtractionService = sp.GetRequiredService<IAiExtractionService>();
|
|
var imageProcessingCoordinator = sp.GetRequiredService<IImageProcessingCoordinator>();
|
|
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, aiExtractionService, imageProcessingCoordinator, picSettings, mapper, logger, versionProvider);
|
|
});
|
|
|
|
services.AddTransient<IAiExtractionService, AiExtractionService>();
|
|
services.AddTransient<IImageProcessingCoordinator, ImageProcessingCoordinator>();
|
|
services.AddTransient<ImageCreationService>();
|
|
services.AddTransient<IImageCreator, ImageCreatorImageSharp>();
|
|
|
|
var userPrefsPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
|
"ImageCatalog", "userprefs.xml");
|
|
services.AddSingleton(new ParametriSetup(userPrefsPath));
|
|
services.AddSingleton<PickerPreferenceService>();
|
|
services.AddSingleton<PicSettings>();
|
|
|
|
services.AddCatalogCommunication(options =>
|
|
{
|
|
options.BaseUri = new Uri("https://www.regalamiunsorriso.it/");
|
|
options.AdminPageBasePath = "admin/pg_RUS";
|
|
options.ReceiveFilePath = "ReceiveFile.abl";
|
|
options.RequestTimeout = TimeSpan.FromSeconds(30);
|
|
options.RetryCount = 2;
|
|
options.RetryBaseDelay = TimeSpan.FromMilliseconds(250);
|
|
});
|
|
|
|
services.AddTransient<AvaloniaMainWindow>();
|
|
|
|
services.AddSingleton<MaddoShared.IVersionProvider, MaddoShared.VersionProvider>();
|
|
|
|
services.AddLogging(configure =>
|
|
{
|
|
configure.AddCustomFormatter();
|
|
configure.AddConsole();
|
|
configure.SetMinimumLevel(LogLevel.Debug);
|
|
});
|
|
}
|
|
}
|
|
|
|
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)
|
|
// Case insensitive
|
|
: 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,
|
|
IExternalScopeProvider? scopeProvider,
|
|
TextWriter? textWriter)
|
|
{
|
|
if (textWriter is null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
string? message =
|
|
logEntry.Formatter?.Invoke(
|
|
logEntry.State, logEntry.Exception);
|
|
|
|
if (message is null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var timestamp = DateTimeOffset.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
|
|
var level = logEntry.LogLevel.ToString().ToUpperInvariant();
|
|
var category = logEntry.Category ?? "App";
|
|
|
|
var line = $"{timestamp} [{level}] {category}: {message}";
|
|
textWriter.WriteLine(line);
|
|
System.Diagnostics.Debug.WriteLine(line);
|
|
|
|
if (logEntry.Exception is not null)
|
|
{
|
|
var exceptionText = logEntry.Exception.ToString();
|
|
textWriter.WriteLine(exceptionText);
|
|
System.Diagnostics.Debug.WriteLine(exceptionText);
|
|
}
|
|
}
|
|
public void Dispose() => _optionsReloadToken?.Dispose();
|
|
} |