Skip to content
This repository was archived by the owner on Mar 15, 2024. It is now read-only.

Commit 2abaedd

Browse files
committed
feat(core): bootstrap result
1 parent dc731a4 commit 2abaedd

File tree

1 file changed

+39
-28
lines changed

1 file changed

+39
-28
lines changed

Core/Bootstrap.cs

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,56 @@ public static class Bootstrap
1818
private static readonly CancellationTokenSource CancellationTokenSource = new();
1919

2020
[UnmanagedCallersOnly]
21-
public static void InitializeSourceSharp()
22-
=> Initialize();
21+
public static int InitializeSourceSharp() => Initialize();
2322

2423
[UnmanagedCallersOnly]
2524
public static void ShutdownSourceSharp() => CancellationTokenSource.Cancel(false);
2625

27-
public static void InitializeTest() => Initialize();
26+
public static int InitializeTest() => Initialize();
2827

29-
private static void Initialize()
28+
private static int Initialize()
3029
{
31-
// bin
32-
var dir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)!;
33-
// root
34-
var parent = Directory.GetParent(dir)!.Name;
35-
// config
36-
var path = Path.Combine(parent, "configs", "core.json");
30+
try
31+
{
32+
// bin
33+
var dir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)!;
34+
// root
35+
var parent = Directory.GetParent(dir)!.Name;
36+
// config
37+
var path = Path.Combine(parent, "configs", "core.json");
3738

3839
#if DEBUG
39-
if (!File.Exists(path))
40-
{
41-
path = Path.Combine(dir, "config.json");
42-
}
40+
if (!File.Exists(path))
41+
{
42+
path = Path.Combine(dir, "config.json");
43+
}
4344
#endif
4445

45-
var services = new ServiceCollection();
46-
var configuration = new ConfigurationBuilder().AddJsonFile(path).Build();
46+
var services = new ServiceCollection();
47+
var configuration = new ConfigurationBuilder().AddJsonFile(path).Build();
4748

48-
ConfigureServices(services, configuration);
49+
ConfigureServices(services, configuration);
4950

50-
var serviceProvider = services.BuildServiceProvider(options: new ServiceProviderOptions
51-
{
52-
ValidateOnBuild = true,
53-
ValidateScopes = true
54-
});
51+
var serviceProvider = services.BuildServiceProvider(options: new()
52+
{
53+
ValidateOnBuild = true,
54+
ValidateScopes = true
55+
});
56+
57+
Boot(serviceProvider);
5558

56-
Boot(serviceProvider);
59+
return 0;
60+
}
61+
catch (Exception ex)
62+
{
63+
var color = Console.ForegroundColor;
64+
Console.ForegroundColor = ConsoleColor.Cyan;
65+
Console.WriteLine($"{DateTime.Now:yyyy/MM/dd HH:mm:ss} [SourceSharp] Failed to init SourceSharp.");
66+
Console.ForegroundColor = color;
67+
Console.WriteLine(ex.ToString());
68+
Console.WriteLine(Environment.NewLine);
69+
return 1;
70+
}
5771
}
5872

5973
private static void ConfigureServices(IServiceCollection services, IConfiguration configuration)
@@ -75,7 +89,6 @@ private static void ConfigureServices(IServiceCollection services, IConfiguratio
7589

7690
private static void Boot(IServiceProvider services)
7791
{
78-
7992
// Init IModuleBase
8093
foreach (var module in services.GetAllServices<IModuleBase>())
8194
{
@@ -84,13 +97,11 @@ private static void Boot(IServiceProvider services)
8497

8598
// Plugin Manager should be the LAST!
8699
services.GetRequiredService<IPluginManager>().Initialize();
100+
101+
// export caller invoker
87102
Invoker.Initialize(services);
88103

89104
Task.Run(async () => await SignalThread(services));
90-
91-
#if DEBUG
92-
Console.WriteLine("Boot!");
93-
#endif
94105
}
95106

96107
private static async Task SignalThread(IServiceProvider services)

0 commit comments

Comments
 (0)