1- using WinFormsApp1 . Managers ;
1+ using Microsoft . Extensions . Hosting ;
2+ using Microsoft . Extensions . Logging ;
3+ using Microsoft . Extensions . DependencyInjection ;
4+ using WinFormsApp1 . Extensions ;
25using WinFormsApp1 . Interfaces ;
36
47namespace WinFormsApp1
58{
9+ /// <summary>
10+ /// Simple class to use for logger type argument
11+ /// </summary>
12+ internal class ApplicationHost
13+ {
14+ }
15+
616 internal static class Program
717 {
818 /// <summary>
9- /// The main entry point for the application.
19+ /// The main entry point for the application.
1020 /// </summary>
1121 [ STAThread ]
12- static void Main ( )
22+ static void Main ( string [ ] args )
1323 {
14- // To customize application configuration such as set high DPI settings or default font,
15- // see https://aka.ms/applicationconfiguration.
16- ApplicationConfiguration . Initialize ( ) ;
17-
18- // Initialize the event-driven architecture
19- var serviceContainer = InitializeServices ( ) ;
20-
21- // Create and run the main form with dependency injection
22- var mainForm = new Form1 ( serviceContainer ) ;
23- Application . Run ( mainForm ) ;
24+ // Configure WinForms
25+ Application . SetHighDpiMode ( HighDpiMode . SystemAware ) ;
26+ Application . EnableVisualStyles ( ) ;
27+ Application . SetCompatibleTextRenderingDefault ( false ) ;
28+
29+ // Create and configure the host builder
30+ var builder = Host . CreateApplicationBuilder ( args ) ;
31+
32+ // Configure services
33+ builder . Services . AddGameServices ( ) ;
34+ builder . Services . AddGameLogging ( ) ;
35+ builder . Services . AddGameConfiguration ( ) ;
36+
37+ // Configure additional host settings
38+ builder . Logging . SetMinimumLevel ( LogLevel . Information ) ;
39+
40+ // Build the host
41+ using var host = builder . Build ( ) ;
42+
43+ try
44+ {
45+ // Get the logger
46+ var logger = host . Services . GetRequiredService < ILogger < ApplicationHost > > ( ) ;
47+ logger . LogInformation ( "Starting Realm of Aethermoor application" ) ;
48+
49+ // Initialize all managers
50+ InitializeManagers ( host . Services , logger ) ;
51+
52+ // Create and run the main form
53+ using var scope = host . Services . CreateScope ( ) ;
54+ var mainForm = scope . ServiceProvider . GetRequiredService < Form1 > ( ) ;
55+
56+ logger . LogInformation ( "Starting WinForms application" ) ;
57+ Application . Run ( mainForm ) ;
58+
59+ logger . LogInformation ( "WinForms application closed" ) ;
60+ }
61+ catch ( Exception ex )
62+ {
63+ // Log any unhandled exceptions
64+ var logger = host . Services . GetService < ILogger < ApplicationHost > > ( ) ;
65+ logger ? . LogCritical ( ex , "Application terminated unexpectedly" ) ;
66+ throw ;
67+ }
2468 }
2569
26- private static GameServiceContainer InitializeServices ( )
70+ private static void InitializeManagers ( IServiceProvider serviceProvider , ILogger logger )
2771 {
28- var serviceContainer = new GameServiceContainer ( ) ;
29-
30- // Register core services
31- var eventManager = new EventManager ( ) ;
32- serviceContainer . RegisterSingleton < IEventManager > ( eventManager ) ;
72+ try
73+ {
74+ logger . LogInformation ( "Initializing game managers..." ) ;
3375
34- // Register managers in dependency order
35- var playerManager = new PlayerManager ( eventManager ) ;
36- serviceContainer . RegisterSingleton < IPlayerManager > ( playerManager ) ;
76+ // Initialize managers in dependency order
77+ var playerManager = serviceProvider . GetRequiredService < IPlayerManager > ( ) ;
78+ var gameManager = serviceProvider . GetRequiredService < IGameManager > ( ) ;
79+ var combatManager = serviceProvider . GetRequiredService < ICombatManager > ( ) ;
80+ var inventoryManager = serviceProvider . GetRequiredService < IInventoryManager > ( ) ;
81+ var locationManager = serviceProvider . GetRequiredService < ILocationManager > ( ) ;
82+ var skillManager = serviceProvider . GetRequiredService < ISkillManager > ( ) ;
3783
38- var gameManager = new GameManager ( eventManager , playerManager ) ;
39- serviceContainer . RegisterSingleton < IGameManager > ( gameManager ) ;
84+ // Initialize all managers
85+ if ( playerManager is IBaseManager playerBaseManager )
86+ playerBaseManager . Initialize ( ) ;
4087
41- var combatManager = new CombatManager ( eventManager , playerManager ) ;
42- serviceContainer . RegisterSingleton < ICombatManager > ( combatManager ) ;
88+ if ( gameManager is IBaseManager gameBaseManager )
89+ gameBaseManager . Initialize ( ) ;
4390
44- var inventoryManager = new InventoryManager ( eventManager , playerManager ) ;
45- serviceContainer . RegisterSingleton < IInventoryManager > ( inventoryManager ) ;
91+ if ( combatManager is IBaseManager combatBaseManager )
92+ combatBaseManager . Initialize ( ) ;
4693
47- var locationManager = new LocationManager ( eventManager , inventoryManager ) ;
48- serviceContainer . RegisterSingleton < ILocationManager > ( locationManager ) ;
94+ if ( inventoryManager is IBaseManager inventoryBaseManager )
95+ inventoryBaseManager . Initialize ( ) ;
4996
50- var skillManager = new SkillManager ( eventManager , playerManager ) ;
51- serviceContainer . RegisterSingleton < ISkillManager > ( skillManager ) ;
97+ if ( locationManager is IBaseManager locationBaseManager )
98+ locationBaseManager . Initialize ( ) ;
5299
53- // Initialize all managers
54- playerManager . Initialize ( ) ;
55- gameManager . Initialize ( ) ;
56- combatManager . Initialize ( ) ;
57- inventoryManager . Initialize ( ) ;
58- locationManager . Initialize ( ) ;
59- skillManager . Initialize ( ) ;
100+ if ( skillManager is IBaseManager skillBaseManager )
101+ skillBaseManager . Initialize ( ) ;
60102
61- return serviceContainer ;
103+ logger . LogInformation ( "All game managers initialized successfully" ) ;
104+ }
105+ catch ( Exception ex )
106+ {
107+ logger . LogError ( ex , "Error initializing game managers" ) ;
108+ throw ;
109+ }
62110 }
63111 }
64112}
0 commit comments